Skip to:
Content

BuddyPress.org

Changeset 11925


Ignore:
Timestamp:
04/02/2018 02:24:57 AM (19 months 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.

Location:
trunk
Files:
19 added
2 deleted
3 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
  • trunk/src/bp-messages/classes/class-bp-messages-component.php

    r11910 r11925  
    5959            'cssjs',
    6060            'cache',
    61             'actions',
    62             'screens',
    6361            'filters',
    6462            'template',
     
    7977
    8078        parent::includes( $includes );
     79    }
     80
     81    /**
     82     * Late includes method.
     83     *
     84     * Only load up certain code when on specific pages.
     85     *
     86     * @since 3.0.0
     87     */
     88    public function late_includes() {
     89        // Bail if PHPUnit is running.
     90        if ( defined( 'BP_TESTS_DIR' ) ) {
     91            return;
     92        }
     93
     94        if ( bp_is_messages_component() ) {
     95            // Authenticated actions.
     96            if ( is_user_logged_in() &&
     97                in_array( bp_current_action(), array( 'compose', 'notices', 'view' ), true )
     98            ) {
     99                require $this->path . 'bp-messages/actions/' . bp_current_action() . '.php';
     100            }
     101
     102            // Authenticated action variables.
     103            if ( is_user_logged_in() && bp_action_variable( 0 ) &&
     104                in_array( bp_action_variable( 0 ), array( 'delete', 'read', 'unread', 'bulk-manage', 'bulk-delete' ), true )
     105            ) {
     106                require $this->path . 'bp-messages/actions/' . bp_action_variable( 0 ) . '.php';
     107            }
     108
     109            // Authenticated actions - Star.
     110            if ( is_user_logged_in() && bp_is_active( $this->id, 'star' ) ) {
     111                // Single action.
     112                if ( in_array( bp_current_action(), array( 'star', 'unstar' ), true ) ) {
     113                    require $this->path . 'bp-messages/actions/star.php';
     114                }
     115
     116                // Bulk-manage.
     117                if ( bp_is_action_variable( 'bulk-manage' ) ) {
     118                    require $this->path . 'bp-messages/actions/bulk-manage-star.php';
     119                }
     120            }
     121
     122            // Screens - User profile integration.
     123            if ( bp_is_user() ) {
     124                require $this->path . 'bp-messages/screens/inbox.php';
     125
     126                /*
     127                 * Nav items.
     128                 *
     129                 * 'view' is not a registered nav item, but we add a screen handler manually.
     130                 */
     131                if ( bp_is_user_messages() && in_array( bp_current_action(), array( 'sentbox', 'compose', 'notices', 'view' ), true ) ) {
     132                    require $this->path . 'bp-messages/screens/' . bp_current_action() . '.php';
     133                }
     134
     135                // Nav item - Starred.
     136                if ( bp_is_active( $this->id, 'star' ) && bp_is_current_action( bp_get_messages_starred_slug() ) ) {
     137                    require $this->path . 'bp-messages/screens/starred.php';
     138                }
     139            }
     140        }
     141
     142        // Groups notifications HTML table.
     143        if ( bp_is_user_settings_notifications() ) {
     144            require $this->path . 'bp-messages/screens/settings-email.php';
     145        }
    81146    }
    82147
  • trunk/tests/phpunit/includes/loader.php

    r11923 r11925  
    2424 * loaded at the same time to prevent weird load order issues.
    2525 */
    26 $components = array( 'activity', 'groups' );
     26$components = array( 'activity', 'groups', 'messages' );
    2727foreach ( $components as $component ) {
    2828    add_action( "bp_{$component}_includes", function() use ( $component ) {
Note: See TracChangeset for help on using the changeset viewer.