Skip to:
Content

BuddyPress.org

Changeset 12121


Ignore:
Timestamp:
05/25/2018 03:48:20 AM (7 years ago)
Author:
imath
Message:

BP Nouveau: Improve Messages UI extensibility

In BP Nouveau, the Messages UI is a Backbone based one. This means current legacy hooks inserted into the JS templates are a bit more different to use for plugin developers as it requires them to use JavaScript to get the data models. If the current AJAX requests still need a way to be extended to fetch extra data such as specific Messages metas, this commit is a first step to help plugin developers to insert content into the Messages UI without changing their habits about using the PHP Messages template global variables to get data about the Messages loop. This is done by introducing the back compatibility function bp_nouveau_messages_catch_hook_content(). This function is used during the AJAX requests that are fetching messages for a specific thread. As a start, 2 legacy hooks have been moved from the JS Template level to this higher level :

  • bp_before_message_content
  • bp_after_message_content

If actions are attached to these hooks their outputs will be caught using the buffer and will be "transported" as new properties, respectively beforeContent and afterContent, of the JSON reply that is used by the Messages UI. If these 2 properties are set and populated, their content will be output inside the corresponding JS templates.

Props pareshradadiya

See #7847 (Trunk)

Location:
trunk/src
Files:
4 edited

Legend:

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

    r12067 r12121  
    38413841    ) );
    38423842}
     3843
     3844/**
     3845 * Remove script and style tags from a string.
     3846 *
     3847 * @since 3.0.1
     3848 *
     3849 * @param  string $string The string to strip tags from.
     3850 * @return string         The stripped tags string.
     3851 */
     3852function bp_strip_script_and_style_tags( $string ) {
     3853    return preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
     3854}
  • trunk/src/bp-templates/bp-nouveau/buddypress/common/js-templates/messages/index.php

    r12115 r12121  
    273273    </div>
    274274
    275     <?php bp_nouveau_messages_hook( 'before', 'content' ); ?>
    276 
    277     <div class="message-content"><# print( data.content ) #></div>
    278 
    279     <?php bp_nouveau_messages_hook( 'after', 'content' ); ?>
     275    <# if ( data.beforeContent ) { #>
     276        <div class="bp-messages-hook before-message-content">{{{data.beforeContent}}}</div>
     277    <# } #>
     278
     279    <div class="message-content">{{{data.content}}}</div>
     280
     281    <# if ( data.afterContent ) { #>
     282        <div class="bp-messages-hook after-message-content">{{{data.afterContent}}}</div>
     283    <# } #>
    280284
    281285</script>
  • trunk/src/bp-templates/bp-nouveau/includes/messages/ajax.php

    r12104 r12121  
    135135    }
    136136
    137     // Get the message bye pretending we're in the message loop.
     137    // Get the message by pretending we're in the message loop.
    138138    global $thread_template;
     139
     140    $bp           = buddypress();
     141    $reset_action = $bp->current_action;
     142
     143    // Override bp_current_action().
     144    $bp->current_action = 'view';
    139145
    140146    bp_thread_has_messages( array( 'thread_id' => (int) $_POST['thread_id'] ) );
     
    183189    }
    184190
     191    $extra_content = bp_nouveau_messages_catch_hook_content( array(
     192        'beforeContent' => 'bp_before_message_content',
     193        'afterContent'  => 'bp_after_message_content',
     194    ) );
     195
     196    if ( array_filter( $extra_content ) ) {
     197        $reply = array_merge( $reply, $extra_content );
     198    }
     199
    185200    // Clean up the loop.
    186201    bp_thread_messages();
     202
     203    // Remove the bp_current_action() override.
     204    $bp->current_action = $reset_action;
    187205
    188206    wp_send_json_success( array(
     
    357375    }
    358376
    359     $thread_id = (int) $_POST['id'];
     377    $thread_id    = (int) $_POST['id'];
     378    $bp           = buddypress();
     379    $reset_action = $bp->current_action;
     380
     381    // Override bp_current_action().
     382    $bp->current_action = 'view';
    360383
    361384    // Simulate the loop.
    362385    if ( ! bp_thread_has_messages( array( 'thread_id' => $thread_id ) ) ) {
     386        // Remove the bp_current_action() override.
     387        $bp->current_action = $reset_action;
     388
    363389        wp_send_json_error( $response );
    364390    }
     
    423449        }
    424450
     451        $extra_content = bp_nouveau_messages_catch_hook_content( array(
     452            'beforeContent' => 'bp_before_message_content',
     453            'afterContent'  => 'bp_after_message_content',
     454        ) );
     455
     456        if ( array_filter( $extra_content ) ) {
     457            $thread->messages[ $i ] = array_merge( $thread->messages[ $i ], $extra_content );
     458        }
     459
    425460        $i += 1;
    426461    endwhile;
    427462
    428463    $thread->messages = array_filter( $thread->messages );
     464
     465    // Remove the bp_current_action() override.
     466    $bp->current_action = $reset_action;
    429467
    430468    wp_send_json_success( $thread );
  • trunk/src/bp-templates/bp-nouveau/includes/messages/functions.php

    r12119 r12121  
    439439    );
    440440}
     441
     442/**
     443 * Fires Messages Legacy hooks to catch the content and add them
     444 * as extra keys to the JSON Messages UI reply.
     445 *
     446 * @since 3.0.1
     447 *
     448 * @param array $hooks The list of hooks to fire.
     449 * @return array       An associative containing the caught content.
     450 */
     451function bp_nouveau_messages_catch_hook_content( $hooks = array() ) {
     452    $content = array();
     453
     454    ob_start();
     455    foreach ( $hooks as $js_key => $hook ) {
     456        if ( ! has_action( $hook ) ) {
     457            continue;
     458        }
     459
     460        // Fire the hook.
     461        do_action( $hook );
     462
     463        // Catch the sanitized content.
     464        $content[ $js_key ] = bp_strip_script_and_style_tags( ob_get_contents() );
     465
     466        // Clean the buffer.
     467        ob_clean();
     468    }
     469    ob_end_clean();
     470
     471    return $content;
     472}
Note: See TracChangeset for help on using the changeset viewer.