Ticket #6712: 6712.02.screen-notifications.patch
File 6712.02.screen-notifications.patch, 36.7 KB (added by , 8 years ago) |
---|
-
src/bp-activity/bp-activity-notifications.php
11 11 defined( 'ABSPATH' ) || exit; 12 12 13 13 /** 14 * Register screen notification actions with the Notifications component. 15 * 16 * @since 2.6.0 17 */ 18 function bp_activity_register_notification_actions() { 19 // Set labels 20 bp_notifications_set_prop( 21 'settings_component_label', 22 _x( 'Activity', 'Settings > Notifications component label', 'buddypress' ), 23 'labels', 24 'activity' 25 ); 26 27 // Register actions. 28 bp_notifications_set_action( 'activity', array( 29 'action' => 'new_at_mention', 30 'settings_label' => sprintf( __( 'A member mentions you in an update using "@%s"', 'buddypress' ), bp_core_get_username( bp_displayed_user_id() ) ) 31 ) ); 32 33 /** 34 * Fires after default activity notification actions are registered. 35 * 36 * @since 2.6.0 37 */ 38 do_action( 'bp_activity_register_notification_actions' ); 39 } 40 41 /** 14 42 * Format notifications related to activity. 15 43 * 16 44 * @since 1.5.0 -
src/bp-activity/classes/class-bp-activity-component.php
121 121 'has_directory' => true, 122 122 'directory_title' => _x( 'Site-Wide Activity', 'component directory title', 'buddypress' ), 123 123 'notification_callback' => 'bp_activity_format_notifications', 124 'notification_action_callback' => 'bp_activity_register_notification_actions', 124 125 'search_string' => __( 'Search Activity...', 'buddypress' ), 125 126 'global_tables' => $global_tables, 126 127 'meta_tables' => $meta_tables, -
src/bp-core/classes/class-bp-component.php
205 205 * Set up component global variables. 206 206 * 207 207 * @since 1.5.0 208 * @since 2.6.0 Added 'notification_action_callback' parameter to $args. 208 209 * 209 210 * @uses apply_filters() Calls 'bp_{@link bp_Component::name}_id'. 210 211 * @uses apply_filters() Calls 'bp_{@link bp_Component::name}_slug'. … … 223 224 * 'Search Groups...'. 224 225 * @type array $global_tables Optional. An array of database table names. 225 226 * @type array $meta_tables Optional. An array of metadata table names. 227 * @type callable $notification_action_callback Optional. The callable function that registers the component's 228 * notification actions. 226 229 * } 227 230 */ 228 231 public function setup_globals( $args = array() ) { … … 235 238 $default_root_slug = isset( buddypress()->pages->{$this->id}->slug ) ? buddypress()->pages->{$this->id}->slug : ''; 236 239 237 240 $r = wp_parse_args( $args, array( 238 'slug' => $this->id, 239 'root_slug' => $default_root_slug, 240 'has_directory' => false, 241 'directory_title' => '', 242 'notification_callback' => '', 243 'search_string' => '', 244 'global_tables' => '', 245 'meta_tables' => '', 241 'slug' => $this->id, 242 'root_slug' => $default_root_slug, 243 'has_directory' => false, 244 'directory_title' => '', 245 'notification_callback' => '', 246 'notification_action_callback' => '', 247 'search_string' => '', 248 'global_tables' => '', 249 'meta_tables' => '', 246 250 ) ); 247 251 248 252 /** … … 252 256 * 253 257 * @param string $value Slug to use in permalink URI chunk. 254 258 */ 255 $this->slug 259 $this->slug = apply_filters( 'bp_' . $this->id . '_slug', $r['slug'] ); 256 260 257 261 /** 258 262 * Filters the slug used for root directory. … … 261 265 * 262 266 * @param string $value Root directory slug. 263 267 */ 264 $this->root_slug 268 $this->root_slug = apply_filters( 'bp_' . $this->id . '_root_slug', $r['root_slug'] ); 265 269 266 270 /** 267 271 * Filters the component's top-level directory if available. … … 270 274 * 271 275 * @param bool $value Whether or not there is a top-level directory. 272 276 */ 273 $this->has_directory 277 $this->has_directory = apply_filters( 'bp_' . $this->id . '_has_directory', $r['has_directory'] ); 274 278 275 279 /** 276 280 * Filters the component's directory title. … … 279 283 * 280 284 * @param string $value Title to use for the directory. 281 285 */ 282 $this->directory_title 286 $this->directory_title = apply_filters( 'bp_' . $this->id . '_directory_title', $r['directory_title'] ); 283 287 284 288 /** 285 289 * Filters the placeholder text for search inputs for component. … … 288 292 * 289 293 * @param string $value Name to use in search input placeholders. 290 294 */ 291 $this->search_string 295 $this->search_string = apply_filters( 'bp_' . $this->id . '_search_string', $r['search_string'] ); 292 296 293 297 /** 294 298 * Filters the callable function that formats the component's notifications. … … 299 303 */ 300 304 $this->notification_callback = apply_filters( 'bp_' . $this->id . '_notification_callback', $r['notification_callback'] ); 301 305 306 /** 307 * Filters the callable function that registers the component's notification actions. 308 * 309 * @since 2.6.0 310 * 311 * @param string $value Function callback. 312 */ 313 $this->notification_action_callback = apply_filters( 'bp_' . $this->id . '_notification_action_callback', $r['notification_action_callback'] ); 314 302 315 // Set the global table names, if applicable. 303 316 if ( ! empty( $r['global_tables'] ) ) { 304 317 $this->register_global_tables( $r['global_tables'] ); -
src/bp-forums/bp-forums-loader.php
70 70 'slug' => BP_FORUMS_SLUG, 71 71 'root_slug' => isset( $bp->pages->forums->slug ) ? $bp->pages->forums->slug : BP_FORUMS_SLUG, 72 72 'has_directory' => true, 73 'notification_callback' => 'messages_format_notifications',74 73 'search_string' => __( 'Search Forums...', 'buddypress' ), 75 74 ); 76 75 -
src/bp-friends/bp-friends-notifications.php
14 14 defined( 'ABSPATH' ) || exit; 15 15 16 16 /** 17 * Register screen notification actions with the Notifications component. 18 * 19 * @since 2.6.0 20 */ 21 function bp_friends_register_notification_actions() { 22 // Set labels 23 bp_notifications_set_prop( 24 'settings_component_label', 25 _x( 'Friends', 'Settings > Notifications component label', 'buddypress' ), 26 'labels', 27 'friends' 28 ); 29 30 // Register actions. 31 bp_notifications_set_action( 'friends', array( 32 'action' => 'friendship_requested', 33 'settings_label' => __( 'A member sends you a friendship request', 'buddypress' ) 34 ) ); 35 36 bp_notifications_set_action( 'friends', array( 37 'action' => 'friendship_accepted', 38 'settings_label' => __( 'A member accepts your friendship request', 'buddypress' ) 39 ) ); 40 41 /** 42 * Fires after default friend notification actions are registered. 43 * 44 * @since 2.6.0 45 */ 46 do_action( 'bp_friends_register_notification_actions' ); 47 } 48 49 /** 17 50 * Notification formatting callback for bp-friends notifications. 18 51 * 19 52 * @since 1.0.0 -
src/bp-friends/classes/class-bp-friends-component.php
107 107 'has_directory' => false, 108 108 'search_string' => __( 'Search Friends...', 'buddypress' ), 109 109 'notification_callback' => 'friends_format_notifications', 110 'global_tables' => $global_tables 110 'notification_action_callback' => 'bp_friends_register_notification_actions', 111 'global_tables' => $global_tables 111 112 ); 112 113 113 114 parent::setup_globals( $args ); -
src/bp-groups/bp-groups-notifications.php
294 294 /** Notifications *************************************************************/ 295 295 296 296 /** 297 * Register screen notification actions with the Notifications component. 298 * 299 * @since 2.6.0 300 */ 301 function bp_groups_register_notification_actions() { 302 // Set labels 303 bp_notifications_set_prop( 304 'settings_component_label', 305 _x( 'Groups', 'Settings > Notifications component label', 'buddypress' ), 306 'labels', 307 'groups' 308 ); 309 310 // Register actions. 311 bp_notifications_set_action( 'groups', array( 312 'action' => 'group_invite', 313 'settings_label' => __( 'A member invites you to join a group', 'buddypress' ) 314 ) ); 315 316 bp_notifications_set_action( 'groups', array( 317 'action' => 'member_promoted_to_admin', 318 'settings_label' => __( 'You are promoted to a group administrator', 'buddypress' ) 319 ) ); 320 321 bp_notifications_set_action( 'groups', array( 322 'action' => 'member_promoted_to_mod', 323 'settings_label' => __( 'You are promoted to a group moderator', 'buddypress' ) 324 ) ); 325 326 bp_notifications_set_action( 'groups', array( 327 'action' => 'new_membership_request', 328 'settings_label' => __( 'A member requests to join a private group for which you are an admin', 'buddypress' ) 329 ) ); 330 331 bp_notifications_set_action( 'groups', array( 332 'action' => 'membership_request_accepted', 333 'settings_label' => __( 'Your group membership request is accepted', 'buddypress' ) 334 ) ); 335 336 bp_notifications_set_action( 'groups', array( 337 'action' => 'membership_request_rejected', 338 'settings_label' => __( 'Your group membership request is declined', 'buddypress' ) 339 ) ); 340 341 /** 342 * Fires after default friend notification actions are registered. 343 * 344 * @since 2.6.0 345 */ 346 do_action( 'bp_groups_register_notification_actions' ); 347 } 348 349 /** 297 350 * Format notifications for the Groups component. 298 351 * 299 352 * @since 1.0.0 -
src/bp-groups/classes/class-bp-groups-component.php
166 166 'has_directory' => true, 167 167 'directory_title' => _x( 'Groups', 'component directory title', 'buddypress' ), 168 168 'notification_callback' => 'groups_format_notifications', 169 'search_string' => _x( 'Search Groups...', 'Component directory search', 'buddypress' ), 170 'global_tables' => $global_tables, 171 'meta_tables' => $meta_tables, 169 'notification_action_callback' => 'bp_groups_register_notification_actions', 170 'search_string' => _x( 'Search Groups...', 'Component directory search', 'buddypress' ), 171 'global_tables' => $global_tables, 172 'meta_tables' => $meta_tables, 172 173 ); 173 174 174 175 parent::setup_globals( $args ); -
src/bp-messages/bp-messages-notifications.php
11 11 defined( 'ABSPATH' ) || exit; 12 12 13 13 /** 14 * Register screen notification actions with the Notifications component. 15 * 16 * @since 2.6.0 17 */ 18 function bp_messages_register_notification_actions() { 19 // Set labels 20 bp_notifications_set_prop( 21 'settings_component_label', 22 _x( 'Messages', 'Settings > Notifications component label', 'buddypress' ), 23 'labels', 24 'messages' 25 ); 26 27 // Register actions. 28 bp_notifications_set_action( 'messages', array( 29 'action' => 'new_message', 30 'settings_label' => __( 'A member sends you a new message', 'buddypress' ) 31 ) ); 32 33 /** 34 * Fires after default message notification actions are registered. 35 * 36 * @since 2.6.0 37 */ 38 do_action( 'bp_messages_register_notification_actions' ); 39 } 40 41 /** 14 42 * Format notifications for the Messages component. 15 43 * 16 44 * @since 1.0.0 -
src/bp-messages/classes/class-bp-messages-component.php
120 120 'slug' => BP_MESSAGES_SLUG, 121 121 'has_directory' => false, 122 122 'notification_callback' => 'messages_format_notifications', 123 'search_string' => __( 'Search Messages...', 'buddypress' ), 124 'global_tables' => $global_tables, 125 'meta_tables' => $meta_tables 123 'notification_action_callback' => 'bp_messages_register_notification_actions', 124 'search_string' => __( 'Search Messages...', 'buddypress' ), 125 'global_tables' => $global_tables, 126 'meta_tables' => $meta_tables 126 127 ) ); 127 128 } 128 129 -
src/bp-notifications/bp-notifications-functions.php
63 63 } 64 64 } 65 65 66 // See if the user has disabled notifications for this component or action. 67 if ( bp_is_active( 'settings' ) ) { 68 // Eek. 69 // @see https://buddypress.trac.wordpress.org/ticket/6663#comment:4 70 if ( 'profile' === $r['component_name'] && bp_is_active( 'xprofile' ) ) { 71 $r['component_name'] = 'xprofile'; 72 } 73 74 $did_user_disable_component = bp_get_user_meta( $r['user_id'], "notifications_{$r['component_name']}_enabled", true ); 75 if ( '' !== $did_user_disable_component && 0 == $did_user_disable_component ) { 76 return false; 77 } 78 79 $did_user_disable_action = bp_get_user_meta( $r['user_id'], "notifications_{$r['component_name']}_{$r['component_action']}_enabled", true ); 80 if ( '' !== $did_user_disable_action && 0 == $did_user_disable_action ) { 81 return false; 82 } 83 } 84 66 85 // Setup the new notification. 67 86 $notification = new BP_Notifications_Notification; 68 87 $notification->user_id = $r['user_id']; … … 643 662 return apply_filters( 'bp_notifications_get_registered_components', $component_names, $active_components ); 644 663 } 645 664 665 /** 666 * Initialize registered notification actions. 667 * 668 * This loops through each component to fetch registered notification actions. 669 * This function is designed to be run when you need to fetch these actions in 670 * your code. 671 * 672 * @since 2.6.0 673 */ 674 function bp_notifications_init_registered_actions() { 675 // Load BuddyPress. 676 $bp = buddypress(); 677 678 // We've already done this! 679 if ( ! empty( $bp->notifications->actions ) ) { 680 return; 681 } 682 683 // Setup return value. 684 $component_names = array(); 685 686 // Get the active components. 687 $active_components = array_keys( $bp->active_components ); 688 689 // Loop through components, look for callbacks and run them. 690 foreach ( $active_components as $component ) { 691 // The extended profile component is identified in the active_components array as 'xprofile'. 692 // However, the extended profile child object has the key 'profile' in the $bp global. 693 if ( 'xprofile' == $component ) { 694 $component = 'profile'; 695 } 696 697 if ( ! empty( $bp->$component->notification_action_callback ) && is_callable( $bp->$component->notification_action_callback ) ) { 698 call_user_func( $bp->$component->notification_action_callback ); 699 } 700 } 701 } 702 703 /** 704 * Register a notification action. 705 * 706 * Used primarily to output a user's "Settings > Notifications" page, but 707 * could be used for other things. 708 * 709 * You should run this function after 'bp_setup_globals'. 710 * 711 * @since 2.6.0 712 * 713 * @param array $args { 714 * Array of arguments to register for the notification action. 715 * @type string $action The action name. 716 * @type string $settings_label The label for the action. Used on a user's Settings page. 717 * } 718 * @param string $component The component that the action belongs to. 719 * @return bool 720 */ 721 function bp_notifications_set_action( $component = '', $args = array() ) { 722 if ( ! did_action( 'bp_setup_globals' ) ) { 723 _doing_it_wrong( __FUNCTION__, "Function must be called after the 'bp_setup_globals' hook." ); 724 return false; 725 } 726 727 if ( empty( $component ) || empty( $args ) ) { 728 return false; 729 } 730 731 if ( ! isset( buddypress()->$component->notification_callback ) ) { 732 return false; 733 } 734 735 $action = $args['action']; 736 unset( $args['action'] ); 737 738 if ( isset( buddypress()->notifications->actions[$component][$action] ) ) { 739 return false; 740 } 741 742 buddypress()->notifications->actions[$component][$action] = $args; 743 744 return true; 745 } 746 747 /** 748 * Set an arbitrary value in the buddypress()->notifications object. 749 * 750 * Useful for devs to stash various items. 751 * 752 * @since 2.6.0 753 * 754 * @param string $prop The property name. 755 * @param mixed $value The value to set for the property. 756 * @param string $object The object this property should be assigned to. Optional. 757 * @param string $component The component attached with this property. Optional. 758 * If filled in, this will prefix the $prop name. 759 * @return bool 760 */ 761 function bp_notifications_set_prop( $prop = '', $value = '', $object = '', $component = '' ) { 762 if ( ! did_action( 'bp_setup_globals' ) ) { 763 _doing_it_wrong( __FUNCTION__, "Function must be called after the 'bp_setup_globals' hook." ); 764 return false; 765 } 766 767 if ( empty( $prop ) || empty( $value ) ) { 768 return false; 769 } 770 771 if ( ! empty( $component ) ) { 772 $prop = "{$component}_{$prop}"; 773 } 774 775 $prop = sanitize_key( $prop ); 776 $object = sanitize_key( $object ); 777 778 if ( ! empty( $object ) ) { 779 if ( empty( buddypress()->notifications->$object ) ) { 780 buddypress()->notifications->$object = new stdClass; 781 } 782 783 buddypress()->notifications->$object->$prop = $value; 784 } else { 785 buddypress()->notifications->$prop = $value; 786 } 787 788 return true; 789 } 790 646 791 /** Meta **********************************************************************/ 647 792 648 793 /** -
src/bp-notifications/bp-notifications-screens.php
68 68 * @since 1.9.0 69 69 */ 70 70 function bp_notifications_screen_settings() { 71 // Add hook to display notification content. 72 add_action( 'bp_template_content', 'bp_notifications_screen_content' ); 71 73 74 /** 75 * Fires right before the loading of the settings screen notifications screen template file. 76 * 77 * @since 2.6.0 78 */ 79 do_action( 'bp_notifications_screen_settings' ); 80 81 /** 82 * Filters the template to load for the Notifications settings screen. 83 * 84 * @since 2.6.0 85 * 86 * @param string $template Path to the XProfile change avatar template to load. 87 */ 88 bp_core_load_template( apply_filters( 'bp_settings_screen_notifications', '/members/single/home' ) ); 89 } 90 91 /** 92 * Output the screen notification content via a template part. 93 * 94 * @since 2.6.0 95 */ 96 function bp_notifications_screen_content() { 97 bp_get_template_part( 'members/single/settings/screen-notifications' ); 72 98 } -
new file src/bp-notifications/bp-notifications-settings.php
new file mode 100644
- + 1 <?php 2 /** 3 * BuddyPress Notifications Settings Functions. 4 * 5 * @package BuddyPress 6 * @subpackage NotificationsSettings 7 * @since 2.6.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** 14 * Output the notification component settings title. 15 * 16 * @since 2.6.0 17 * 18 * @param string $component The component to retrieve the settings title for. 19 * @param string $element The element to wrap the title around. Default: h3. 20 */ 21 function bp_notifications_component_settings_title( $component = '', $element = 'h3' ) { 22 echo bp_notifications_get_component_settings_title( $component, $element ); 23 } 24 25 /** 26 * Return the notification component settings title. 27 * 28 * @since 2.6.0 29 * 30 * @param string $component The component to retrieve the settings title for. 31 * @param string $element The element to wrap the title around. Default: h3. 32 * @return string 33 */ 34 function bp_notifications_get_component_settings_title( $component = '', $element = 'h3' ) { 35 // Load registered notification actions if we haven't already done so. 36 if ( empty( buddypress()->notifications->actions ) ) { 37 bp_notifications_init_registered_actions(); 38 } 39 40 /** 41 * Filter the element used for the notification component settings title. 42 * 43 * @since 2.6.0 44 * 45 * @var string $element The element to use. Default: h3. 46 */ 47 $element = apply_filters( 'bp_notifications_component_settings_title_element', $element ); 48 $element = sanitize_key( $element ); 49 50 // Fallback title. 51 $title = ucfirst( sanitize_key( $component ) ); 52 53 // Get registered component label if available. 54 $key = "{$component}_settings_component_label"; 55 if ( isset( buddypress()->notifications->labels->$key ) ) { 56 $title = esc_html( buddypress()->notifications->labels->$key ); 57 } 58 59 /** 60 * Filter the notification component settings title. 61 * 62 * @since 2.6.0 63 * 64 * @var string $title The component settings title. 65 * @var string $component The component name. 66 */ 67 $title = apply_filters( 'bp_notifications_component_settings_title', $title, $component ); 68 69 return "<{$element}>{$title}</{$element}>"; 70 } 71 72 /** 73 * Handles the saving of user screen notification settings. 74 * 75 * @since 2.6.0 76 */ 77 function bp_notifications_settings_action_screen_notifications() { 78 // Bail if not a POST action 79 if ( false === bp_is_post_request() ) { 80 return; 81 } 82 83 // Bail if not in settings 84 if ( ! bp_is_user_settings() || ! bp_is_current_action( 'screen-notifications' ) ) { 85 return; 86 } 87 88 check_admin_referer( 'bp_settings_screen_notifications' ); 89 90 // Load our registered notifications actions. 91 bp_notifications_init_registered_actions(); 92 93 // Save settings. 94 foreach ( (array) bp_notifications_get_registered_components() as $component ) { 95 if ( empty( $_POST[ $component ] ) ) { 96 continue; 97 } 98 99 // Fudge xprofile component. 100 if ( 'xprofile' === $component ) { 101 $_POST[ 'xprofile' ] = $_POST[ 'profile' ]; 102 } 103 104 // Disable notifications for this component. 105 if ( ! isset( $_POST[ $component ]['enabled'] ) ) { 106 bp_update_user_meta( bp_displayed_user_id(), "notifications_{$component}_enabled", 0 ); 107 continue; 108 109 // Re-enable them. 110 } else { 111 bp_delete_user_meta( bp_displayed_user_id(), "notifications_{$component}_enabled" ); 112 } 113 114 // Per-action handling. 115 foreach( $_POST[ $component ] as $action => $val ) { 116 if ( 'enabled' === $action ) { 117 continue; 118 } 119 120 if ( isset( buddypress()->notifications->actions[$component][$action] ) ) { 121 // Disable notifications for this action. 122 if ( 0 === (int) $val) { 123 bp_update_user_meta( bp_displayed_user_id(), "notifications_{$component}_{$action}_enabled", 0 ); 124 continue; 125 } else { 126 bp_delete_user_meta( bp_displayed_user_id(), "notifications_{$component}_{$action}_enabled" ); 127 } 128 } 129 } 130 } 131 132 // Switch feedback for super admins 133 if ( bp_is_my_profile() ) { 134 bp_core_add_message( __( 'Your screen notification settings have been saved.', 'buddypress' ), 'success' ); 135 } else { 136 bp_core_add_message( __( "This user's screen notification settings have been saved.", 'buddypress' ), 'success' ); 137 } 138 139 /** 140 * Fires after screen notification settings are saved, and before redirect. 141 * 142 * @since 2.6.0 143 */ 144 do_action( 'bp_notifications_settings_action_after_save' ); 145 146 bp_core_redirect( bp_displayed_user_domain() . bp_get_settings_slug() . '/screen-notifications/' ); 147 die(); 148 } 149 add_action( 'bp_actions', 'bp_notifications_settings_action_screen_notifications' ); 150 No newline at end of file -
src/bp-notifications/classes/class-bp-notifications-component.php
17 17 */ 18 18 class BP_Notifications_Component extends BP_Component { 19 19 20 /* 21 * Notification actions holder. 22 * 23 * @since 2.6.0 24 * 25 * @var array 26 */ 27 public $actions = array(); 28 29 /** 30 * Notification labels holder. 31 * 32 * @since 2.6.0 33 * 34 * @var object 35 */ 36 public $labels; 37 20 38 /** 21 39 * Start the notifications component creation process. 22 40 * … … 31 49 'adminbar_myaccount_order' => 30 32 50 ) 33 51 ); 52 53 $this->setup_hooks(); 34 54 } 35 55 36 56 /** … … 52 72 'cache', 53 73 ); 54 74 75 // @todo if ( bp_is_active( 'settings' ) && bp_is_user_settings() ) ? 76 if ( bp_is_active( 'settings' ) ) { 77 $includes[] = 'settings'; 78 } 79 55 80 if ( ! buddypress()->do_autoload ) { 56 81 $includes[] = 'classes'; 57 82 } … … 91 116 'global_tables' => $global_tables, 92 117 ); 93 118 119 $this->labels = new stdClass; 120 94 121 parent::setup_globals( $args ); 95 122 } 96 123 97 124 /** 125 * Add custom hooks. 126 * 127 * @since 2.6.0 128 */ 129 public function setup_hooks() { 130 add_filter( 'bp_settings_admin_nav', array( $this, 'setup_settings_admin_nav' ), 1 ); 131 } 132 133 /** 98 134 * Set up component navigation. 99 135 * 100 136 * @since 1.9.0 … … 171 207 'user_has_access' => $access, 172 208 ); 173 209 210 /** 211 * The Settings > Notifications nav item can only be set up after the Settings 212 * component has run its own nav routine. 213 */ 214 add_action( 'bp_settings_setup_nav', array( $this, 'setup_settings_nav' ) ); 215 174 216 parent::setup_nav( $main_nav, $sub_nav ); 175 217 } 176 218 177 219 /** 220 * Set up the Settings > Notifications nav item. 221 * 222 * Loaded in a separate method because the Settings component may not be 223 * loaded in time during the BP_Notifications_Component::setup_nav() method. 224 * 225 * @since 2.6.0 226 */ 227 public function setup_settings_nav() { 228 if ( ! bp_is_active( 'settings' ) ) { 229 return; 230 } 231 232 // Determine user to use. 233 if ( bp_displayed_user_domain() ) { 234 $user_domain = bp_displayed_user_domain(); 235 } elseif ( bp_loggedin_user_domain() ) { 236 $user_domain = bp_loggedin_user_domain(); 237 } else { 238 return; 239 } 240 241 // Get the settings slug. 242 $settings_slug = bp_get_settings_slug(); 243 244 bp_core_new_subnav_item( array( 245 'name' => _x( 'Notifications', 'Notification settings sub nav', 'buddypress' ), 246 'slug' => 'screen-notifications', 247 'parent_url' => trailingslashit( $user_domain . $settings_slug ), 248 'parent_slug' => $settings_slug, 249 'screen_function' => 'bp_notifications_screen_settings', 250 'position' => 25, 251 'user_has_access' => bp_core_can_edit_settings() 252 ) ); 253 } 254 255 /** 178 256 * Set up the component entries in the WordPress Admin Bar. 179 257 * 180 258 * @since 1.9.0 … … 242 320 } 243 321 244 322 /** 323 * Adds "Settings > Notifications" subnav item under the "Settings" adminbar menu. 324 * 325 * @since 2.6.0 326 * 327 * @param array $wp_admin_nav The settings adminbar nav array. 328 * @return array 329 */ 330 public function setup_settings_admin_nav( $wp_admin_nav ) { 331 332 // Setup the logged in user variables. 333 $settings_link = trailingslashit( bp_loggedin_user_domain() . bp_get_settings_slug() ); 334 335 // Add the "Notifications" subnav item. 336 $wp_admin_nav[] = array( 337 'parent' => 'my-account-' . buddypress()->settings->id, 338 'id' => 'my-account-' . buddypress()->settings->id . '-screennotifications', 339 'title' => _x( 'Notifications', 'My Account Settings sub nav', 'buddypress' ), 340 'href' => trailingslashit( $settings_link . 'screen-notifications' ) 341 ); 342 343 return $wp_admin_nav; 344 } 345 346 /** 245 347 * Set up the title for pages and <title>. 246 348 * 247 349 * @since 1.9.0 -
new file src/bp-templates/bp-legacy/buddypress/members/single/settings/screen-notifications.php
new file mode 100644
- + 1 <?php 2 /** 3 * BuddyPress - Members Settings Screen Notifications 4 * 5 * @since 2.6.0 6 * 7 * @package BuddyPress 8 * @subpackage bp-legacy 9 */ 10 11 /** This action is documented in bp-templates/bp-legacy/buddypress/members/single/settings/profile.php */ 12 do_action( 'bp_before_member_settings_template' ); ?> 13 14 <form action="<?php echo bp_displayed_user_domain() . bp_get_settings_slug() . '/screen-notifications'; ?>" method="post" class="standard-form" id="settings-form"> 15 16 <?php 17 $components = bp_notifications_get_registered_components(); 18 19 // Fudge xprofile component. 20 $profile = array_search( 'xprofile', $components ); 21 if ( false !== $profile ) { 22 $components[] = 'profile'; 23 unset( $components[ $profile ] ); 24 } 25 26 sort( $components, SORT_STRING ); 27 28 // Initialize registered notification actions. 29 bp_notifications_init_registered_actions(); 30 31 foreach ( $components as $component ) : 32 $enabled = bp_get_user_meta( bp_loggedin_user_id(), "notifications_{$component}_enabled", true ); 33 if ( '' === $enabled ) { 34 $enabled = 1; 35 } 36 37 $action_count = ! empty( buddypress()->notifications->actions[$component] ) ? count( buddypress()->notifications->actions[$component] ) : 0; 38 ?> 39 <?php bp_notifications_component_settings_title( $component, 'h2' ); ?> 40 41 <label for="<?php esc_attr_e( "{$component}-notification-enable" ); ?>"> 42 <input type="checkbox" name="<?php esc_attr_e( "{$component}[enabled]" ); ?>" data-component="<?php esc_attr_e( $component ); ?>" id="<?php esc_attr_e( "{$component}-notification-enable" ); ?>" value="1" <?php checked( $enabled, 1, true ) ?>/> 43 44 <?php esc_html_e( 'Enable screen notifications for this component', 'buddypress' ); ?> 45 </label> 46 47 <?php if ( ! empty( buddypress()->notifications->actions[$component] ) ) : ?> 48 49 <table class="notification-settings" id="<?php esc_attr_e( "{$component}-screen-notification-settings" ); ?>"> 50 <thead> 51 <tr> 52 <th class="icon"> </th> 53 <th> </th> 54 <th class="yes"><?php _e( 'Yes', 'buddypress' ) ?></th> 55 <th class="no"><?php _e( 'No', 'buddypress' )?></th> 56 </tr> 57 </thead> 58 59 <tbody> 60 <?php 61 foreach ( buddypress()->notifications->actions[$component] as $action => $val ) : 62 $action = sanitize_key( $action ); 63 64 $enabled = bp_get_user_meta( bp_loggedin_user_id(), "notifications_{$component}_{$action}_enabled", true ); 65 if ( '' === $enabled ) { 66 $enabled = 1; 67 } 68 ?> 69 70 <tr id="<?php esc_html_e( "{$component}-{$action}" ); ?>" class="<?php esc_attr_e( "{$component}-screen-notification-singular" ); ?>"> 71 <td> </td> 72 <td><?php esc_html_e( $val['settings_label' ] ); ?></td> 73 <td class="yes"><input type="radio" name="<?php esc_attr_e( "{$component}[{$action}]" ); ?>" id="<?php esc_attr_e( "{$component}-notification-{$action}-yes" ); ?>" value="1" <?php checked( $enabled, 1, true ) ?>/><label for="<?php esc_attr_e( "{$component}-notification-{$action}-yes" ); ?>" class="bp-screen-reader-text"><?php _e( 'Yes', 'buddypress' ); ?></label></td> 74 <td class="no"><input type="radio" name="<?php esc_attr_e( "{$component}[{$action}]" ); ?>" id="<?php esc_attr_e( "{$component}-notification-{$action}-no" ); ?>" value="0" <?php checked( $enabled, 0, true ) ?>/><label for="<?php esc_attr_e( "{$component}-notification-{$action}-no" ); ?>" class="bp-screen-reader-text"><?php _e( 'No', 'buddypress' ); ?></label></td> 75 </tr> 76 77 <?php 78 endforeach; 79 ?> 80 </tbody> 81 </table> 82 83 <?php endif; ?> 84 85 <?php endforeach; ?> 86 87 <script type="text/javascript"> 88 // This will be moved to bp-legacy's buddypress.js file. Or maybe not? 89 jQuery(function($){ 90 $('#settings-form input[type=checkbox]').each(function( index, elem ) { 91 toggleSettingsComponentFields( $(this) ); 92 }); 93 94 $('#settings-form').on('click', 'input[type=checkbox]', function(e) { 95 toggleSettingsComponentFields( $(this) ); 96 }); 97 98 function toggleSettingsComponentFields( elem ) { 99 var component = $( elem ).data( 'component' ); 100 101 if ( elem.is(':checked') ) { 102 $('#' + component + '-screen-notification-settings').fadeIn('fast'); 103 } else { 104 $('#' + component + '-screen-notification-settings').fadeOut('fast'); 105 } 106 } 107 }); 108 </script> 109 110 <?php 111 112 /** 113 * Fires before the display of the submit button for user notification saving. 114 * 115 * @since 2.6.0 116 */ 117 do_action( 'bp_members_screen_notification_settings_before_submit' ); ?> 118 119 <div class="submit"> 120 <input type="submit" name="screen-notifications-submit" value="<?php esc_attr_e( 'Save Changes', 'buddypress' ); ?>" id="submit" class="auto" /> 121 </div> 122 123 <?php 124 125 /** 126 * Fires after the display of the submit button for user notification saving. 127 * 128 * @since 2.6.0 129 */ 130 do_action( 'bp_members_screen_notification_settings_after_submit' ); ?> 131 132 <?php wp_nonce_field( 'bp_settings_screen_notifications' ); ?> 133 134 </form> 135 136 <?php 137 138 /** This action is documented in bp-templates/bp-legacy/buddypress/members/single/settings/profile.php */ 139 do_action( 'bp_after_member_settings_template' ); 140 No newline at end of file -
src/bp-xprofile/bp-xprofile-notifications.php
7 7 * @since 1.0.0 8 8 */ 9 9 10 /** Notifications *************************************************************/ 10 /** 11 * Register screen notification actions with the Notifications component. 12 * 13 * @since 2.6.0 14 */ 15 function bp_xprofile_register_notification_actions() { 16 // Set labels 17 bp_notifications_set_prop( 18 'settings_component_label', 19 _x( 'Profile', 'Settings > Notifications component label', 'buddypress' ), 20 'labels', 21 'xprofile' 22 ); 23 24 /** 25 * Fires after default extended profile notification actions are registered. 26 * 27 * @since 2.6.0 28 */ 29 do_action( 'bp_xprofile_register_notification_actions' ); 30 } 11 31 12 32 /** 13 33 * Format notifications for the extended profile (Xprofile) component. -
tests/phpunit/testcases/notifications/functions.php
294 294 $found2 = $wpdb->get_col( $query ); 295 295 $this->assertEmpty( $found2 ); 296 296 } 297 298 /** 299 * @group notification_settings 300 */ 301 public function test_notifications_disabled_by_component_action_by_user() { 302 $u = $this->factory->user->create(); 303 304 // Disable notifications for the 'new_at_mention' action. 305 bp_update_user_meta( $u, 'notifications_activity_new_at_mention_enabled', 0 ); 306 307 $n1 = $this->factory->notification->create( array( 308 'component_name' => 'activity', 309 'component_action' => 'new_at_mention', 310 'item_id' => 99, 311 'user_id' => $u, 312 ) ); 313 314 // This should pass through. 315 $n2 = $this->factory->notification->create( array( 316 'component_name' => 'activity', 317 'component_action' => 'kwyjibo', 318 'item_id' => 99, 319 'user_id' => $u, 320 ) ); 321 322 $this->assertFalse( $n1 ); 323 $this->assertNotFalse( $n2 ); 324 } 325 326 /** 327 * @group notification_settings 328 */ 329 public function test_notifications_disabled_by_component_name_by_user() { 330 $u = $this->factory->user->create(); 331 332 // Disable notifications for the 'activity' component. 333 bp_update_user_meta( $u, 'notifications_activity_enabled', 0 ); 334 335 $n1 = $this->factory->notification->create( array( 336 'component_name' => 'activity', 337 'component_action' => 'kwyjibo', 338 'item_id' => 99, 339 'user_id' => $u, 340 ) ); 341 342 $n2 = $this->factory->notification->create( array( 343 'component_name' => 'activity', 344 'component_action' => 'new_at_mention', 345 'item_id' => 99, 346 'user_id' => $u, 347 ) ); 348 349 // This should pass through. 350 $n3 = $this->factory->notification->create( array( 351 'component_name' => 'messages', 352 'component_action' => 'new_message', 353 'item_id' => 99, 354 'user_id' => $u, 355 ) ); 356 357 $this->assertFalse( $n1 ); 358 $this->assertFalse( $n2 ); 359 $this->assertNotFalse( $n3 ); 360 } 297 361 }