Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/22/2015 06:02:55 AM (9 years ago)
Author:
tw2113
Message:

More docs cleanup for Notifications component.

See #6404.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-notifications/bp-notifications-functions.php

    r10284 r10303  
    3030 * }
    3131 * @return int|bool ID of the newly created notification on success, false
    32  *         on failure.
     32 *                  on failure.
    3333 */
    3434function bp_notifications_add_notification( $args = array() ) {
     
    4545    ), 'notifications_add_notification' );;
    4646
    47     // Check for existing duplicate notifications
     47    // Check for existing duplicate notifications.
    4848    if ( ! $r['allow_duplicate'] ) {
    49         // date_notified, allow_duplicate don't count toward
    50         // duplicate status
     49        // Date_notified, allow_duplicate don't count toward
     50        // duplicate status.
    5151        $existing = BP_Notifications_Notification::get( array(
    5252            'user_id'           => $r['user_id'],
     
    6363    }
    6464
    65     // Setup the new notification
     65    // Setup the new notification.
    6666    $notification                    = new BP_Notifications_Notification;
    6767    $notification->user_id           = $r['user_id'];
     
    7373    $notification->is_new            = $r['is_new'];
    7474
    75     // Save the new notification
     75    // Save the new notification.
    7676    return $notification->save();
    7777}
     
    8383 *
    8484 * @param int $id ID of the notification.
    85  *
    86  * @return BP_Notifications_Notification
     85 * @return BP_Notifications_Notification Notification object for ID specified.
    8786 */
    8887function bp_notifications_get_notification( $id ) {
     
    9695 *
    9796 * @param int $id ID of the notification to delete.
    98  *
    9997 * @return bool True on success, false on failure.
    10098 */
     
    116114 * @param int      $id     ID of the notification.
    117115 * @param int|bool $is_new 0 for read, 1 for unread.
    118  *
    119116 * @return bool True on success, false on failure.
    120117 */
     
    136133 *
    137134 * @param int $user_id ID of the user whose notifications are being fetched.
    138  *
    139  * @return array
     135 * @return array $notifications Array of notifications for user.
    140136 */
    141137function bp_notifications_get_all_notifications_for_user( $user_id = 0 ) {
    142138
    143     // Default to displayed user if no ID is passed
     139    // Default to displayed user if no ID is passed.
    144140    if ( empty( $user_id ) ) {
    145141        $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
    146142    }
    147143
    148     // Get notifications out of the cache, or query if necessary
     144    // Get notifications out of the cache, or query if necessary.
    149145    $notifications = wp_cache_get( 'all_for_user_' . $user_id, 'bp_notifications' );
    150146    if ( false === $notifications ) {
     
    174170 * @param string $format  Format of the returned values. 'string' returns HTML,
    175171 *                        while 'object' returns a structured object for parsing.
    176  *
    177172 * @return mixed Object or array on success, false on failure.
    178173 */
    179174function bp_notifications_get_notifications_for_user( $user_id, $format = 'string' ) {
    180175
    181     // Setup local variables
     176    // Setup local variables.
    182177    $bp = buddypress();
    183178
    184     // Get notifications (out of the cache, or query if necessary)
     179    // Get notifications (out of the cache, or query if necessary).
    185180    $notifications         = bp_notifications_get_all_notifications_for_user( $user_id );
    186     $grouped_notifications = array(); // Notification groups
    187     $renderable            = array(); // Renderable notifications
    188 
    189     // Group notifications by component and component_action and provide totals
     181    $grouped_notifications = array(); // Notification groups.
     182    $renderable            = array(); // Renderable notifications.
     183
     184    // Group notifications by component and component_action and provide totals.
    190185    for ( $i = 0, $count = count( $notifications ); $i < $count; ++$i ) {
    191186        $notification = $notifications[$i];
     
    193188    }
    194189
    195     // Bail if no notification groups
     190    // Bail if no notification groups.
    196191    if ( empty( $grouped_notifications ) ) {
    197192        return false;
    198193    }
    199194
    200     // Calculate a renderable output for each notification type
     195    // Calculate a renderable output for each notification type.
    201196    foreach ( $grouped_notifications as $component_name => $action_arrays ) {
    202197
     
    209204        }
    210205
    211         // Skip if group is empty
     206        // Skip if group is empty.
    212207        if ( empty( $action_arrays ) ) {
    213208            continue;
    214209        }
    215210
    216         // Loop through each actionable item and try to map it to a component
     211        // Loop through each actionable item and try to map it to a component.
    217212        foreach ( (array) $action_arrays as $component_action_name => $component_action_items ) {
    218213
    219             // Get the number of actionable items
     214            // Get the number of actionable items.
    220215            $action_item_count = count( $component_action_items );
    221216
    222             // Skip if the count is less than 1
     217            // Skip if the count is less than 1.
    223218            if ( $action_item_count < 1 ) {
    224219                continue;
    225220            }
    226221
    227             // Callback function exists
     222            // Callback function exists.
    228223            if ( isset( $bp->{$component_name}->notification_callback ) && is_callable( $bp->{$component_name}->notification_callback ) ) {
    229224
    230                 // Function should return an object
     225                // Function should return an object.
    231226                if ( 'object' === $format ) {
    232227
    233                     // Retrieve the content of the notification using the callback
     228                    // Retrieve the content of the notification using the callback.
    234229                    $content = call_user_func(
    235230                        $bp->{$component_name}->notification_callback,
     
    241236                    );
    242237
    243                     // Create the object to be returned
     238                    // Create the object to be returned.
    244239                    $notification_object = $component_action_items[0];
    245240
    246241                    // Minimal backpat with non-compatible notification
    247                     // callback functions
     242                    // callback functions.
    248243                    if ( is_string( $content ) ) {
    249244                        $notification_object->content = $content;
     
    256251                    $renderable[] = $notification_object;
    257252
    258                 // Return an array of content strings
     253                // Return an array of content strings.
    259254                } else {
    260255                    $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 );
     
    266261                $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 );
    267262
    268             // Allow non BuddyPress components to hook in
     263            // Allow non BuddyPress components to hook in.
    269264            } else {
    270265
    271                 // The array to reference with apply_filters_ref_array()
     266                // The array to reference with apply_filters_ref_array().
    272267                $ref_array = array(
    273268                    $component_action_name,
     
    278273                );
    279274
    280                 // Function should return an object
     275                // Function should return an object.
    281276                if ( 'object' === $format ) {
    282277
     
    290285                    $content = apply_filters_ref_array( 'bp_notifications_get_notifications_for_user', $ref_array );
    291286
    292                     // Create the object to be returned
     287                    // Create the object to be returned.
    293288                    $notification_object = $component_action_items[0];
    294289
    295290                    // Minimal backpat with non-compatible notification
    296                     // callback functions
     291                    // callback functions.
    297292                    if ( is_string( $content ) ) {
    298293                        $notification_object->content = $content;
     
    305300                    $renderable[] = $notification_object;
    306301
    307                 // Return an array of content strings
     302                // Return an array of content strings.
    308303                } else {
    309304
     
    315310    }
    316311
    317     // If renderable is empty array, set to false
     312    // If renderable is empty array, set to false.
    318313    if ( empty( $renderable ) ) {
    319314        $renderable = false;
     
    345340 * @param string $component_name   Name of the associated component.
    346341 * @param string $component_action Name of the associated action.
    347  *
    348342 * @return bool True on success, false on failure.
    349343 */
     
    369363 * @param string   $component_action  Name of the associated action.
    370364 * @param int|bool $secondary_item_id ID of the secondary associated item.
    371  *
    372365 * @return bool True on success, false on failure.
    373366 */
     
    393386 * @param string|bool $component_action  Optional. Name of the associated action.
    394387 * @param int|bool    $secondary_item_id Optional. ID of the secondary associated item.
    395  *
    396388 * @return bool True on success, false on failure.
    397389 */
     
    419411 * @param string $component_name   Name of the associated component.
    420412 * @param string $component_action Name of the associated action.
    421  *
    422413 * @return bool True on success, false on failure.
    423414 */
     
    444435 * @param string   $component_action Name of the associated action.
    445436 * @param int|bool $is_new           0 for read, 1 for unread.
    446  *
    447437 * @return bool True on success, false on failure.
    448438 */
     
    474464 * @param int|bool $secondary_item_id ID of the secondary associated item.
    475465 * @param int|bool $is_new            0 for read, 1 for unread.
    476  *
    477466 * @return bool True on success, false on failure.
    478467 */
     
    504493 * @param int|bool    $secondary_item_id Optional. ID of the secondary associated item.
    505494 * @param int|bool    $is_new            0 for read, 1 for unread.
    506  *
    507495 * @return bool True on success, false on failure.
    508496 */
     
    536524 * @param string   $component_action Name of the associated action.
    537525 * @param int|bool $is_new           0 for read, 1 for unread.
    538  *
    539526 * @return bool True on success, false on failure.
    540527 */
     
    563550 * @param int $user_id         ID of the user being checked.
    564551 * @param int $notification_id ID of the notification being checked.
    565  *
    566552 * @return bool True if the notification belongs to the user, otherwise false.
    567553 */
     
    577563 * @param int $user_id ID of the user whose unread notifications are being
    578564 *                     counted.
    579  *
    580565 * @return int Unread notification count.
    581566 */
     
    602587 * @see http://buddypress.trac.wordpress.org/ticket/5300
    603588 *
    604  * @return array
     589 * @return array $component_names Array of registered components.
    605590 */
    606591function bp_notifications_get_registered_components() {
    607592
    608     // Load BuddyPress
     593    // Load BuddyPress.
    609594    $bp = buddypress();
    610595
    611     // Setup return value
     596    // Setup return value.
    612597    $component_names = array();
    613598
    614     // Get the active components
     599    // Get the active components.
    615600    $active_components = array_keys( $bp->active_components );
    616601
    617     // Loop through components, look for callbacks, add to return value
     602    // Loop through components, look for callbacks, add to return value.
    618603    foreach ( $active_components as $component ) {
    619604        if ( !empty( $bp->$component->notification_callback ) ) {
     
    654639 *                                deleted if the meta_value matches this parameter.
    655640 * @param bool   $delete_all      Optional. If true, delete matching metadata entries
    656  *                                for all objects, ignoring the specified object_id. Otherwise,
    657  *                                only delete matching metadata entries for the specified
    658  *                                notification item. Default: false.
    659  *
     641 *                                for all objects, ignoring the specified object_id. Otherwise,
     642 *                                only delete matching metadata entries for the specified
     643 *                                notification item. Default: false.
    660644 * @return bool                   True on success, false on failure.
    661645 */
    662646function bp_notifications_delete_meta( $notification_id, $meta_key = '', $meta_value = '', $delete_all = false ) {
    663647
    664     // Legacy - if no meta_key is passed, delete all for the item
     648    // Legacy - if no meta_key is passed, delete all for the item.
    665649    if ( empty( $meta_key ) ) {
    666650        $all_meta = bp_notifications_get_meta( $notification_id );
     
    669653            : array();
    670654
    671         // With no meta_key, ignore $delete_all
     655        // With no meta_key, ignore $delete_all.
    672656        $delete_all = false;
    673657    } else {
     
    698682 *                                notification item will be fetched.
    699683 * @param bool   $single          Optional. If true, return only the first value of the
    700  *                                specified meta_key. This parameter has no effect if meta_key is not
    701  *                                specified. Default: true.
    702  *
     684 *                                specified meta_key. This parameter has no effect if meta_key is not
     685 *                                specified. Default: true.
    703686 * @return mixed                  The meta value(s) being requested.
    704687 */
     
    733716 *                                 metadata entries with the specified value.
    734717 *                                 Otherwise, update all entries.
    735  *
    736718 * @return bool|int                Returns false on failure. On successful
    737719 *                                 update of existing metadata, returns true. On
     
    758740 *                                for the given key. If true, and the object already has a value for
    759741 *                                the key, no change will be made. Default: false.
    760  *
    761742 * @return int|bool               The meta ID on successful update, false on failure.
    762743 */
Note: See TracChangeset for help on using the changeset viewer.