Skip to content

Instantly share code, notes, and snippets.

@MaximeCulea
Created August 6, 2019 09:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaximeCulea/e1a6266bf7f824b15567bc8502ba1144 to your computer and use it in GitHub Desktop.
Save MaximeCulea/e1a6266bf7f824b15567bc8502ba1144 to your computer and use it in GitHub Desktop.
BuddyPress - Read all notifications buton
<?php
/**
* Class MC_BP_Notifications
* Handle the read all notifications without post / get method, based on unread notifications
*
* To be used in HTML : <a href="<?php echo Wide_Notifications::mark_notifications_as_read_url(); ?>">Read all notifications</a>
*
* @author Maxime CULEA
*/
class MC_BP_Notifications {
function init() {
add_action( 'bp_actions', [ $this, 'mark_notifications_as_read_action' ] );
}
/**
* Generate the url for marking all user's notifications to read
* @see bp_get_the_message_thread_mark_read_url()
* @see bp_notifications_action_bulk_manage()
*
* @author Maxime CULEA
*
* @return string
*/
public static function mark_notifications_as_read_url() {
$notifications = self::get_new_notifications();
if ( empty( $notifications ) ) {
return '';
}
// Add the args to the URL.
$url = add_query_arg( [ 'notification_bulk_action' => 'mc_bp_read_all' ], bp_get_notifications_read_permalink( get_current_user_id() ) );
// Add the nonce.
return wp_nonce_url( $url, 'mc_bp_read_all', 'notification_bulk_nonce' );
}
/**
* Manage to mark all notification as read
* @see bp_notifications_action_bulk_manage()
*
* @author Maxime CULEA
*
* @return bool
*/
public function mark_notifications_as_read_action() {
if ( ! bp_is_notifications_component() || ( ! bp_is_current_action( 'read' ) || bp_is_current_action( 'unread' ) ) ) {
return false;
}
$action = $_GET['notification_bulk_action'] ?: '';
$nonce = $_GET['notification_bulk_nonce'] ?: '';
if ( 'mc_bp_read_all' !== $action || empty( $nonce ) ) {
return false;
}
if ( ! wp_verify_nonce( $nonce, 'mc_bp_read_all' ) ) {
bp_core_add_message( __( 'There was a problem managing your notifications.', 'buddypress' ), 'error' );
return false;
}
$notification_ids = self::get_new_notifications();
foreach ( $notification_ids as $notification_id ) {
bp_notifications_mark_notification( $notification_id, false );
}
bp_core_add_message( __( 'Notifications marked as read', 'buddypress' ) );
bp_core_redirect( bp_displayed_user_domain() . bp_get_notifications_slug() . '/' . bp_current_action() . '/' );
}
/**
* Get current user new notifications
*
* @author Maxime CULEA
*
* @return array
*/
private static function get_new_notifications() {
if ( ! class_exists( 'BP_Notifications_Notification' ) ) {
return [];
}
$notifications = BP_Notifications_Notification::get( [
'user_id' => get_current_user_id(),
'is_new' => true,
] );
return empty( $notifications ) ? [] : wp_list_pluck( $notifications, 'id' );
}
}
$mc_bp_n = new MC_BP_Notifications();
$mc_bp_n->init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment