Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.57 KB | None | 0 0
  1. remove_filter( 'bp_core_get_notifications_for_user', $renderable, $user_id, $format );
  2.  
  3. /**
  4.  * Get notifications for a specific user.
  5.  *
  6.  * @since 1.9.0
  7.  *
  8.  * @param int    $user_id ID of the user whose notifications are being fetched.
  9.  * @param string $format  Format of the returned values. 'string' returns HTML,
  10.  *                        while 'object' returns a structured object for parsing.
  11.  * @return mixed Object or array on success, false on failure.
  12.  */
  13. function bp_notifications_get_notifications_for_user2( $user_id, $format = 'string' ) {
  14.  
  15.     // Setup local variables.
  16.     $bp = buddypress();
  17.  
  18.     // Get notifications (out of the cache, or query if necessary).
  19.     $notifications         = bp_notifications_get_all_notifications_for_user( $user_id );
  20.     $grouped_notifications = array(); // Notification groups.
  21.     $renderable            = array(); // Renderable notifications.
  22.  
  23.     // Group notifications by component and component_action and provide totals.
  24.     for ( $i = 0, $count = count( $notifications ); $i < $count; ++$i ) {
  25.         $notification = $notifications[$i];
  26.         $grouped_notifications[$notification->component_name][$notification->component_action][] = $notification;
  27.     }
  28.  
  29.     // Bail if no notification groups.
  30.     if ( empty( $grouped_notifications ) ) {
  31.         return false;
  32.     }
  33.  
  34.     // Calculate a renderable output for each notification type.
  35.     foreach ( $grouped_notifications as $component_name => $action_arrays ) {
  36.  
  37.         // We prefer that extended profile component-related notifications use
  38.         // the component_name of 'xprofile'. However, the extended profile child
  39.         // object in the $bp object is keyed as 'profile', which is where we need
  40.         // to look for the registered notification callback.
  41.         if ( 'xprofile' == $component_name ) {
  42.             $component_name = 'profile';
  43.         }
  44.  
  45.         // Skip if group is empty.
  46.         if ( empty( $action_arrays ) ) {
  47.             continue;
  48.         }
  49.  
  50.         // Loop through each actionable item and try to map it to a component.
  51.         foreach ( (array) $action_arrays as $component_action_name => $component_action_items ) {
  52.  
  53.             // Get the number of actionable items.
  54.             $action_item_count = count( $component_action_items );
  55.  
  56.             // Skip if the count is less than 1.
  57.             if ( $action_item_count < 1 ) {
  58.                 continue;
  59.             }
  60.  
  61.             // Callback function exists.
  62.             if ( isset( $bp->{$component_name}->notification_callback ) && is_callable( $bp->{$component_name}->notification_callback ) ) {
  63.  
  64.                 // Function should return an object.
  65.                 if ( 'object' === $format ) {
  66.  
  67.                     // Retrieve the content of the notification using the callback.
  68.                     $content = call_user_func(
  69.                         $bp->{$component_name}->notification_callback,
  70.                         $component_action_name,
  71.                         $component_action_items[0]->item_id,
  72.                         $component_action_items[0]->secondary_item_id,
  73.                         $action_item_count,
  74.                         'array',
  75.                         $component_action_items[0]->id
  76.                     );
  77.  
  78.                     // Create the object to be returned.
  79.                     $notification_object = $component_action_items[0];
  80.  
  81.                     // Minimal backpat with non-compatible notification
  82.                     // callback functions.
  83.                     if ( is_string( $content ) ) {
  84.                         $notification_object->content = $content;
  85.                         $notification_object->href    = bp_loggedin_user_domain();
  86.                     } else {
  87.                         $notification_object->content = $content['text'];
  88.                         $notification_object->href    = $content['link'];
  89.                     }
  90.  
  91.                     $renderable[] = $notification_object;
  92.  
  93.                 // Return an array of content strings.
  94.                 } else {
  95.                 $content = call_user_func( $bp->{$component_name}->notification_callback, $component_action_name, $component_action_items[0]->item_id, $component_action_items[0]->secondary_item_id, $action_item_count, 'string', $component_action_items[0]->id );
  96.                 $renderable[] = $content;
  97.                 }
  98.  
  99.             // @deprecated format_notification_function - 1.5
  100.             } elseif ( isset( $bp->{$component_name}->format_notification_function ) && function_exists( $bp->{$component_name}->format_notification_function ) ) {
  101.                 $renderable[] = call_user_func( $bp->{$component_name}->format_notification_function, $component_action_name, $component_action_items[0]->item_id, $component_action_items[0]->secondary_item_id, $action_item_count );
  102.  
  103.             // Allow non BuddyPress components to hook in.
  104.             } else {
  105.  
  106.                 // The array to reference with apply_filters_ref_array().
  107.                 $ref_array = array(
  108.                     $component_action_name,
  109.                     $component_action_items[0]->item_id,
  110.                     $component_action_items[0]->secondary_item_id,
  111.                     $action_item_count,
  112.                     $format,
  113.                     $component_action_name, // Duplicated so plugins can check the canonical action name.
  114.                     $component_name,
  115.                     $component_action_items[0]->id
  116.                 );
  117.  
  118.                 // Function should return an object.
  119.                 if ( 'object' === $format ) {
  120.  
  121.                     /**
  122.                      * Filters the notification content for notifications created by plugins.
  123.                      *
  124.                      * If your plugin extends the {@link BP_Component} class, you should use the
  125.                      * 'notification_callback' parameter in your extended
  126.                      * {@link BP_Component::setup_globals()} method instead.
  127.                      *
  128.                      * @since 1.9.0
  129.                      * @since 2.6.0 Added $component_action_name, $component_name, $id as parameters.
  130.                      *
  131.                      * @param string $content               Component action. Deprecated. Do not do checks against this! Use
  132.                      *                                      the 6th parameter instead - $component_action_name.
  133.                      * @param int    $item_id               Notification item ID.
  134.                      * @param int    $secondary_item_id     Notification secondary item ID.
  135.                      * @param int    $action_item_count     Number of notifications with the same action.
  136.                      * @param string $format                Format of return. Either 'string' or 'object'.
  137.                      * @param string $component_action_name Canonical notification action.
  138.                      * @param string $component_name        Notification component ID.
  139.                      * @param int    $id                    Notification ID.
  140.                      *
  141.                      * @return string|array If $format is 'string', return a string of the notification content.
  142.                      *                      If $format is 'object', return an array formatted like:
  143.                      *                      array( 'text' => 'CONTENT', 'link' => 'LINK' )
  144.                      */
  145.                     $content = apply_filters_ref_array( 'bp_notifications_get_notifications_for_user2', $ref_array );
  146.  
  147.                     // Create the object to be returned.
  148.                     $notification_object = $component_action_items[0];
  149.  
  150.                     // Minimal backpat with non-compatible notification
  151.                     // callback functions.
  152.                     if ( is_string( $content ) ) {
  153.                         $notification_object->content = $content;
  154.                         $notification_object->href    = bp_loggedin_user_domain();
  155.                     } else {
  156.                         $notification_object->content = $content['text'];
  157.                         $notification_object->href    = $content['link'];
  158.                     }
  159.  
  160.                     $renderable[] = $notification_object;
  161.  
  162.                 // Return an array of content strings.
  163.                 } else {
  164.  
  165.                     /** This filters is documented in bp-notifications/bp-notifications-functions.php */
  166.                     $renderable[] = apply_filters_ref_array( 'bp_notifications_get_notifications_for_user2', $ref_array );
  167.                 }
  168.             }
  169.         }
  170.     }
  171.  
  172.     // If renderable is empty array, set to false.
  173.     if ( empty( $renderable ) ) {
  174.         $renderable = false;
  175.     }
  176.  
  177.     /**
  178.      * Filters the final array of notifications to be displayed for a user.
  179.      *
  180.      * @since 1.6.0
  181.      *
  182.      * @param array|bool $renderable Array of notifications to render or false if no notifications.
  183.      * @param int        $user_id    ID of the user whose notifications are being displayed.
  184.      * @param string     $format     Display format requested for the notifications.
  185.      */
  186.     return apply_filters( 'bp_core_get_notifications_for_user2', $renderable, $user_id, $format );
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement