Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- remove_filter( 'bp_core_get_notifications_for_user', $renderable, $user_id, $format );
- /**
- * Get notifications for a specific user.
- *
- * @since 1.9.0
- *
- * @param int $user_id ID of the user whose notifications are being fetched.
- * @param string $format Format of the returned values. 'string' returns HTML,
- * while 'object' returns a structured object for parsing.
- * @return mixed Object or array on success, false on failure.
- */
- function bp_notifications_get_notifications_for_user2( $user_id, $format = 'string' ) {
- // Setup local variables.
- $bp = buddypress();
- // Get notifications (out of the cache, or query if necessary).
- $notifications = bp_notifications_get_all_notifications_for_user( $user_id );
- $grouped_notifications = array(); // Notification groups.
- $renderable = array(); // Renderable notifications.
- // Group notifications by component and component_action and provide totals.
- for ( $i = 0, $count = count( $notifications ); $i < $count; ++$i ) {
- $notification = $notifications[$i];
- $grouped_notifications[$notification->component_name][$notification->component_action][] = $notification;
- }
- // Bail if no notification groups.
- if ( empty( $grouped_notifications ) ) {
- return false;
- }
- // Calculate a renderable output for each notification type.
- foreach ( $grouped_notifications as $component_name => $action_arrays ) {
- // We prefer that extended profile component-related notifications use
- // the component_name of 'xprofile'. However, the extended profile child
- // object in the $bp object is keyed as 'profile', which is where we need
- // to look for the registered notification callback.
- if ( 'xprofile' == $component_name ) {
- $component_name = 'profile';
- }
- // Skip if group is empty.
- if ( empty( $action_arrays ) ) {
- continue;
- }
- // Loop through each actionable item and try to map it to a component.
- foreach ( (array) $action_arrays as $component_action_name => $component_action_items ) {
- // Get the number of actionable items.
- $action_item_count = count( $component_action_items );
- // Skip if the count is less than 1.
- if ( $action_item_count < 1 ) {
- continue;
- }
- // Callback function exists.
- if ( isset( $bp->{$component_name}->notification_callback ) && is_callable( $bp->{$component_name}->notification_callback ) ) {
- // Function should return an object.
- if ( 'object' === $format ) {
- // Retrieve the content of the notification using the callback.
- $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,
- 'array',
- $component_action_items[0]->id
- );
- // Create the object to be returned.
- $notification_object = $component_action_items[0];
- // Minimal backpat with non-compatible notification
- // callback functions.
- if ( is_string( $content ) ) {
- $notification_object->content = $content;
- $notification_object->href = bp_loggedin_user_domain();
- } else {
- $notification_object->content = $content['text'];
- $notification_object->href = $content['link'];
- }
- $renderable[] = $notification_object;
- // Return an array of content strings.
- } else {
- $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 );
- $renderable[] = $content;
- }
- // @deprecated format_notification_function - 1.5
- } elseif ( isset( $bp->{$component_name}->format_notification_function ) && function_exists( $bp->{$component_name}->format_notification_function ) ) {
- $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 );
- // Allow non BuddyPress components to hook in.
- } else {
- // The array to reference with apply_filters_ref_array().
- $ref_array = array(
- $component_action_name,
- $component_action_items[0]->item_id,
- $component_action_items[0]->secondary_item_id,
- $action_item_count,
- $format,
- $component_action_name, // Duplicated so plugins can check the canonical action name.
- $component_name,
- $component_action_items[0]->id
- );
- // Function should return an object.
- if ( 'object' === $format ) {
- /**
- * Filters the notification content for notifications created by plugins.
- *
- * If your plugin extends the {@link BP_Component} class, you should use the
- * 'notification_callback' parameter in your extended
- * {@link BP_Component::setup_globals()} method instead.
- *
- * @since 1.9.0
- * @since 2.6.0 Added $component_action_name, $component_name, $id as parameters.
- *
- * @param string $content Component action. Deprecated. Do not do checks against this! Use
- * the 6th parameter instead - $component_action_name.
- * @param int $item_id Notification item ID.
- * @param int $secondary_item_id Notification secondary item ID.
- * @param int $action_item_count Number of notifications with the same action.
- * @param string $format Format of return. Either 'string' or 'object'.
- * @param string $component_action_name Canonical notification action.
- * @param string $component_name Notification component ID.
- * @param int $id Notification ID.
- *
- * @return string|array If $format is 'string', return a string of the notification content.
- * If $format is 'object', return an array formatted like:
- * array( 'text' => 'CONTENT', 'link' => 'LINK' )
- */
- $content = apply_filters_ref_array( 'bp_notifications_get_notifications_for_user2', $ref_array );
- // Create the object to be returned.
- $notification_object = $component_action_items[0];
- // Minimal backpat with non-compatible notification
- // callback functions.
- if ( is_string( $content ) ) {
- $notification_object->content = $content;
- $notification_object->href = bp_loggedin_user_domain();
- } else {
- $notification_object->content = $content['text'];
- $notification_object->href = $content['link'];
- }
- $renderable[] = $notification_object;
- // Return an array of content strings.
- } else {
- /** This filters is documented in bp-notifications/bp-notifications-functions.php */
- $renderable[] = apply_filters_ref_array( 'bp_notifications_get_notifications_for_user2', $ref_array );
- }
- }
- }
- }
- // If renderable is empty array, set to false.
- if ( empty( $renderable ) ) {
- $renderable = false;
- }
- /**
- * Filters the final array of notifications to be displayed for a user.
- *
- * @since 1.6.0
- *
- * @param array|bool $renderable Array of notifications to render or false if no notifications.
- * @param int $user_id ID of the user whose notifications are being displayed.
- * @param string $format Display format requested for the notifications.
- */
- return apply_filters( 'bp_core_get_notifications_for_user2', $renderable, $user_id, $format );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement