Skip to:
Content

BuddyPress.org

Changeset 12989


Ignore:
Timestamp:
07/08/2021 05:23:19 PM (5 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.

Location:
trunk/src
Files:
3 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}
  • trunk/src/bp-templates/bp-legacy/buddypress-functions.php

    r12930 r12989  
    16241624
    16251625    } else {
    1626         $user_id    = get_current_user_id();
    1627         $notice_ids = bp_get_user_meta( $user_id, 'closed_notices', true );
    1628         if ( ! is_array( $notice_ids ) ) {
    1629             $notice_ids = array();
    1630         }
    1631 
    1632         $notice_ids[] = (int) $_POST['notice_id'];
    1633 
    1634         bp_update_user_meta( $user_id, 'closed_notices', $notice_ids );
     1626        bp_messages_dismiss_sitewide_notice( bp_loggedin_user_id(), (int) $_POST['notice_id'] );
    16351627    }
    16361628
  • trunk/src/bp-templates/bp-nouveau/includes/messages/ajax.php

    r12937 r12989  
    767767
    768768    // Mark the active notice as closed.
    769     $notice = BP_Messages_Notice::get_active();
    770 
    771     if ( ! empty( $notice->id ) ) {
    772         $user_id = bp_loggedin_user_id();
    773 
    774         $closed_notices = bp_get_user_meta( $user_id, 'closed_notices', true );
    775 
    776         if ( empty( $closed_notices ) ) {
    777             $closed_notices = array();
    778         }
    779 
    780         // Add the notice to the array of the user's closed notices.
    781         $closed_notices[] = (int) $notice->id;
    782         bp_update_user_meta( $user_id, 'closed_notices', array_map( 'absint', array_unique( $closed_notices ) ) );
    783 
     769    $success = bp_messages_dismiss_sitewide_notice();
     770
     771    if ( $success ) {
    784772        wp_send_json_success( array(
    785773            'feedback' => '<div class="bp-feedback info"><span class="bp-icon" aria-hidden="true"></span><p>' . __( 'Sitewide notice dismissed', 'buddypress' ) . '</p></div>',
    786774            'type'     => 'success',
    787775        ) );
    788     }
    789 }
     776    } else {
     777        wp_send_json_error( array(
     778            'feedback' => '<div class="bp-feedback info"><span class="bp-icon" aria-hidden="true"></span><p>' . __( 'There was a problem dismissing that sitewide notice', 'buddypress' ) . '</p></div>',
     779            'type'     => 'error',
     780        ) );
     781    }
     782}
Note: See TracChangeset for help on using the changeset viewer.