Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/25/2018 03:52:08 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

Fixes #7847 (Branch 3.0)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.0/src/bp-templates/bp-nouveau/includes/messages/ajax.php

    r12105 r12122  
    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 );
Note: See TracChangeset for help on using the changeset viewer.