Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/02/2018 02:24:57 AM (7 years ago)
Author:
r-a-y
Message:

Messages: Conditionally load action and screen functions.

This commit conditionally loads action and screen function code for the
Messages component, utilizing the 'bp_late_include' hook introduced in
r11884.

Previously, we loaded these functions at all times, which is unnecessary
when a user is not on a BuddyPress messages page. Now, we only load this
code when needed.

See #7218.

File:
1 edited

Legend:

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

    r11586 r11925  
    311311}
    312312
    313 /** SCREENS **************************************************************/
    314 
    315 /**
    316  * Screen handler to display a user's "Starred" private messages page.
    317  *
    318  * @since 2.3.0
    319  */
    320 function bp_messages_star_screen() {
    321     add_action( 'bp_template_content', 'bp_messages_star_content' );
    322 
    323     /**
    324      * Fires right before the loading of the "Starred" messages box.
    325      *
    326      * @since 2.3.0
    327      */
    328     do_action( 'bp_messages_screen_star' );
    329 
    330     bp_core_load_template( 'members/single/plugins' );
    331 }
    332 
    333 /**
    334  * Screen content callback to display a user's "Starred" messages page.
    335  *
    336  * @since 2.3.0
    337  */
    338 function bp_messages_star_content() {
    339     // Add our message thread filter.
    340     add_filter( 'bp_after_has_message_threads_parse_args', 'bp_messages_filter_starred_message_threads' );
    341 
    342     // Load the message loop template part.
    343     bp_get_template_part( 'members/single/messages/messages-loop' );
    344 
    345     // Remove our filter.
    346     remove_filter( 'bp_after_has_message_threads_parse_args', 'bp_messages_filter_starred_message_threads' );
    347 }
    348 
    349 /**
    350  * Filter message threads by those starred by the logged-in user.
    351  *
    352  * @since 2.3.0
    353  *
    354  * @param  array $r Current message thread arguments.
    355  * @return array $r Array of starred message threads.
    356  */
    357 function bp_messages_filter_starred_message_threads( $r = array() ) {
    358     $r['box'] = 'starred';
    359     $r['meta_query'] = array( array(
    360         'key'   => 'starred_by_user',
    361         'value' => $r['user_id']
    362     ) );
    363 
    364     return $r;
    365 }
    366 
    367 /** ACTIONS **************************************************************/
    368 
    369 /**
    370  * Action handler to set a message's star status for those not using JS.
    371  *
    372  * @since 2.3.0
    373  */
    374 function bp_messages_star_action_handler() {
    375     if ( ! bp_is_user_messages() ) {
    376         return;
    377     }
    378 
    379     if ( false === ( bp_is_current_action( 'unstar' ) || bp_is_current_action( 'star' ) ) ) {
    380         return;
    381     }
    382 
    383     if ( ! wp_verify_nonce( bp_action_variable( 1 ), 'bp-messages-star-' . bp_action_variable( 0 ) ) ) {
    384         wp_die( "Oops!  That's a no-no!" );
    385     }
    386 
    387     // Check capability.
    388     if ( ! is_user_logged_in() || ! bp_core_can_edit_settings() ) {
    389         return;
    390     }
    391 
    392     // Mark the star.
    393     bp_messages_star_set_action( array(
    394         'action'     => bp_current_action(),
    395         'message_id' => bp_action_variable(),
    396         'bulk'       => (bool) bp_action_variable( 2 )
    397     ) );
    398 
    399     // Redirect back to previous screen.
    400     $redirect = wp_get_referer() ? wp_get_referer() : bp_displayed_user_domain() . bp_get_messages_slug();
    401     bp_core_redirect( $redirect );
    402     die();
    403 }
    404 add_action( 'bp_actions', 'bp_messages_star_action_handler' );
    405 
    406 /**
    407  * Bulk manage handler to set the star status for multiple messages.
    408  *
    409  * @since 2.3.0
    410  */
    411 function bp_messages_star_bulk_manage_handler() {
    412     if ( empty( $_POST['messages_bulk_nonce' ] ) ) {
    413         return;
    414     }
    415 
    416     // Check the nonce.
    417     if ( ! wp_verify_nonce( $_POST['messages_bulk_nonce'], 'messages_bulk_nonce' ) ) {
    418         return;
    419     }
    420 
    421     // Check capability.
    422     if ( ! is_user_logged_in() || ! bp_core_can_edit_settings() ) {
    423         return;
    424     }
    425 
    426     $action  = ! empty( $_POST['messages_bulk_action'] ) ? $_POST['messages_bulk_action'] : '';
    427     $threads = ! empty( $_POST['message_ids'] ) ? $_POST['message_ids'] : '';
    428     $threads = wp_parse_id_list( $threads );
    429 
    430     // Bail if action doesn't match our star actions or no IDs.
    431     if ( false === in_array( $action, array( 'star', 'unstar' ), true ) || empty( $threads ) ) {
    432         return;
    433     }
    434 
    435     // It's star time!
    436     switch ( $action ) {
    437         case 'star' :
    438             $count = count( $threads );
    439 
    440             // If we're starring a thread, we only star the first message in the thread.
    441             foreach ( $threads as $thread ) {
    442                 $thread = new BP_Messages_thread( $thread );
    443                 $mids = wp_list_pluck( $thread->messages, 'id' );
    444 
    445                 bp_messages_star_set_action( array(
    446                     'action'     => 'star',
    447                     'message_id' => $mids[0],
    448                 ) );
    449             }
    450 
    451             bp_core_add_message( sprintf( _n( '%s message was successfully starred', '%s messages were successfully starred', $count, 'buddypress' ), $count ) );
    452             break;
    453 
    454         case 'unstar' :
    455             $count = count( $threads );
    456 
    457             foreach ( $threads as $thread ) {
    458                 bp_messages_star_set_action( array(
    459                     'action'    => 'unstar',
    460                     'thread_id' => $thread,
    461                     'bulk'      => true
    462                 ) );
    463             }
    464 
    465             bp_core_add_message( sprintf( _n( '%s message was successfully unstarred', '%s messages were successfully unstarred', $count, 'buddypress' ), $count ) );
    466             break;
    467     }
    468 
    469     // Redirect back to message box.
    470     bp_core_redirect( bp_displayed_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/' );
    471     die();
    472 }
    473 add_action( 'bp_actions', 'bp_messages_star_bulk_manage_handler', 5 );
    474 
    475313/** HOOKS ****************************************************************/
    476314
Note: See TracChangeset for help on using the changeset viewer.