Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/24/2023 02:32:27 AM (2 years ago)
Author:
imath
Message:

Make canonical redirection & Ajax requesting BP Rewrites ready

  • Introduces bp_core_setup_query_parser() to decide when to postpone some key hooks firing to bp_parse_query.
  • Introduces bp_core_get_query_parser() to get the query parser in use. It contains a filter BP Classic will be able to use to force the Legacy URL parser. So far it uses legacy for pretty links and rewrites for plain links.
  • Edit bp_redirect_canonical() & bp_get_canonical_url() so that they use the BP Rewrites API.
  • Introduces bp_core_set_ajax_uri_globals() to set the BuddyPress URI globales using the BP Rewrites API inside the Ajax context thanks to the updated bp_reset_query() function.
  • To avoid using WP() at each Ajax call, introduces a simple Ajax actions registration process thanks to the new bp_ajax_register_action().
  • Update Legacy & Nouveau template packs so that they use this logic. As BP Default will require the legacy URL parser and will be moved inside BP Classic, the theme can stay the way it is.
  • Improve the bp_rewrites_pre_get_slug filter logic making it depends on the bp_core_get_query_parser() function.
  • Make sure to reset the Members navigation to a new BP_Core_Nav based on the displayed user once the user is available in the component's parse_query() method.

Props r-a-y, johnjamesjacoby, boonebgorges

Closes https://github.com/buddypress/buddypress/pull/91
See #4954

File:
1 edited

Legend:

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

    r13455 r13461  
    579579
    580580/**
     581 * Parse the query for the Ajax context.
     582 *
     583 * @since 12.0.0
     584 *
     585 * @param WP_Query $referer_query WP_Query object.
     586 */
     587function bp_parse_ajax_referer_query( $referer_query ) {
     588    if ( ! wp_doing_ajax() || 'rewrites' !== bp_core_get_query_parser() ) {
     589        return;
     590    }
     591
     592    /**
     593     * Fires at the end of the bp_parse_ajax_referer_query function.
     594     *
     595     * Allow BuddyPress components to parse the ajax referer query.
     596     *
     597     * @since 12.0.0
     598     *
     599     * @param WP_Query $posts_query WP_Query instance. Passed by reference.
     600     */
     601    do_action_ref_array( 'bp_parse_query', array( &$referer_query ) );
     602}
     603
     604/**
    581605 * Resets the query to fit our permalink structure if needed.
    582606 *
     
    601625    }
    602626
    603     // Temporarly override the request uri.
    604     if ( isset( $wp->request ) ) {
     627    // Use the BP Rewrites API to parse the ajax referer request.
     628    if ( wp_doing_ajax() ) {
     629        if ( ! bp_has_pretty_urls() ) {
     630            $matched_query = wp_parse_url( $bp_request, PHP_URL_QUERY );
     631        } else {
     632            // Temporarly override the request uri.
     633            $_SERVER['REQUEST_URI'] = $bp_request;
     634
     635            $wp_ajax = new WP();
     636            $wp_ajax->parse_request();
     637
     638            // Extra step to check for root profiles.
     639            $member = bp_rewrites_get_member_data( $wp_ajax->request );
     640            if ( isset( $member['object'] ) && $member['object'] ) {
     641                $_SERVER['REQUEST_URI'] = trailingslashit( $bp->members->root_slug ) . $wp_ajax->request;
     642
     643                // Reparse the request.
     644                $wp_ajax->parse_request();
     645            }
     646
     647            $matched_query = $wp_ajax->matched_query;
     648        }
     649
     650        // Use a specific function to fire the `bp_parse_query` hook.
     651        add_action( 'parse_query', 'bp_parse_ajax_referer_query', 2 );
     652
     653        // Parse the matched query.
     654        $query->parse_query( $matched_query );
     655
     656        // Use to requery in case of root profiles.
     657    } elseif ( isset( $wp->request ) ) {
     658        // Temporarly override the request uri.
    605659        $_SERVER['REQUEST_URI'] = str_replace( $wp->request, $bp_request, $reset_server_request_uri );
    606660
Note: See TracChangeset for help on using the changeset viewer.