Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/08/2021 05:23:19 PM (4 years ago)
Author:
dcavins
Message:

Introduce bp_messages_dismiss_sitewide_notice().

Introduce bp_messages_dismiss_sitewide_notice()
as core function to dismiss a notice for a user.
This function allows us to replace code to update the
user's closed_notices meta that is repeated in
several places in BuddyPress.

See #8505.

File:
1 edited

Legend:

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

    r12984 r12989  
    740740    );
    741741}
     742
     743/**
     744 * Dismiss a sitewide notice for a user.
     745 *
     746 * @since 9.0.0
     747 *
     748 * @param int $user_id   ID of the user to dismiss the notice for.
     749 *                       Defaults to the logged-in user.
     750 * @param int $notice_id ID of the notice to be dismissed.
     751 *                       Defaults to the currently active notice.
     752 * @return bool False on failure, true if notice is dismissed
     753 *              (or was already dismissed).
     754 */
     755function bp_messages_dismiss_sitewide_notice( $user_id = 0, $notice_id = 0 ) {
     756    $retval = false;
     757    if ( ! $user_id ) {
     758        $user_id = bp_loggedin_user_id();
     759    }
     760
     761    // Bail if no user is set.
     762    if ( ! $user_id ) {
     763        return $retval;
     764    }
     765
     766    if ( $notice_id ) {
     767        $notice = new BP_Messages_Notice( $notice_id );
     768    } else {
     769        $notice = BP_Messages_Notice::get_active();
     770    }
     771
     772    // Bail if no notice is set.
     773    if ( empty( $notice->id ) ) {
     774        return $retval;
     775    }
     776
     777    // Fetch the user's closed notices and add the new item.
     778    $closed_notices = (array) bp_get_user_meta( $user_id, 'closed_notices', true );
     779    $closed_notices = array_filter( $closed_notices );
     780
     781    if ( in_array( (int) $notice->id, $closed_notices, true ) ) {
     782        // The notice has already been dismissed, so there's nothing to do.
     783        $retval = true;
     784    } else {
     785        // Add the notice to the closed_notices meta.
     786        $closed_notices[] = (int) $notice->id;
     787        $closed_notices   = array_map( 'absint', array_unique( $closed_notices ) );
     788        $success          = bp_update_user_meta( $user_id, 'closed_notices', $closed_notices );
     789
     790        // The return value from update_user_meta() could be an integer or a boolean.
     791        $retval = (bool) $success;
     792    }
     793
     794    return $retval;
     795}
Note: See TracChangeset for help on using the changeset viewer.