Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/24/2023 02:32:27 AM (3 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-catchuri.php

    r13436 r13461  
    386386        // Reset the keys by merging with an empty array.
    387387        $bp->action_variables = array_merge( array(), $bp->action_variables );
     388}
     389
     390/**
     391 * Sets BuddyPress globals for Ajax requests using the BP Rewrites API.
     392 *
     393 * @since 12.0.0
     394 */
     395function bp_core_set_ajax_uri_globals() {
     396        if ( ! wp_doing_ajax() || 'rewrites' !== bp_core_get_query_parser() ) {
     397                return;
     398        }
     399
     400        $action = '';
     401        if ( isset( $_REQUEST['action'] ) ) {
     402                $action = wp_unslash( sanitize_text_field( $_REQUEST['action'] ) );
     403        }
     404
     405        // Only set BuddyPress URI globals for registered Ajax actions.
     406        if ( ! bp_ajax_action_is_registered( $action ) ) {
     407                return;
     408        }
     409
     410        bp_reset_query( bp_get_referer_path(), $GLOBALS['wp_query'] );
    388411}
    389412
     
    808831         * @param bool $value Whether or not to do canonical redirects. Default true.
    809832         */
    810         if ( !bp_is_blog_page() && apply_filters( 'bp_do_redirect_canonical', true ) ) {
     833        if ( ! bp_is_blog_page() && apply_filters( 'bp_do_redirect_canonical', true ) ) {
    811834                // If this is a POST request, don't do a canonical redirect.
    812835                // This is for backward compatibility with plugins that submit form requests to
    813836                // non-canonical URLs. Plugin authors should do their best to use canonical URLs in
    814837                // their form actions.
    815                 if ( !empty( $_POST ) ) {
     838                if ( ! empty( $_POST ) ) {
    816839                        return;
    817840                }
     
    819842                // Build the URL in the address bar.
    820843                $requested_url  = bp_get_requested_url();
     844                $query_args     = '';
    821845
    822846                // Stash query args.
    823                 $url_stack      = explode( '?', $requested_url );
    824                 $req_url_clean  = $url_stack[0];
    825                 $query_args     = isset( $url_stack[1] ) ? $url_stack[1] : '';
    826 
    827                 $canonical_url  = bp_get_canonical_url();
     847                if ( bp_has_pretty_urls() ) {
     848                        $query_args    = wp_parse_url( $requested_url, PHP_URL_QUERY );
     849                        $req_url_clean = str_replace( '?' . $query_args, '', $requested_url );
     850                } else {
     851                        $req_url_clean = $requested_url;
     852                }
     853
     854                $canonical_url = bp_get_canonical_url();
    828855
    829856                // Only redirect if we've assembled a URL different from the request.
    830                 if ( $canonical_url !== $req_url_clean ) {
    831 
     857                if ( esc_url( $canonical_url ) !== esc_url( $req_url_clean ) ) {
    832858                        $bp = buddypress();
    833859
     
    841867                        }
    842868
    843                         if ( !empty( $query_args ) ) {
     869                        if ( ! empty( $query_args ) ) {
    844870                                $canonical_url .= '?' . $query_args;
    845871                        }
     
    891917                $defaults
    892918        );
    893 
    894         extract( $r );
    895919
    896920        // Special case: when a BuddyPress directory (eg example.com/members)
     
    929953                // Build the URL in the address bar.
    930954                $requested_url  = bp_get_requested_url();
    931 
    932                 // Stash query args.
    933                 $url_stack      = explode( '?', $requested_url );
     955                $base_url       = '';
     956                $path_chunks    = array();
     957                $component_id   = '';
     958
     959                // Get query args.
     960                $query_string = wp_parse_url( $requested_url, PHP_URL_QUERY );
     961                $query_args   = wp_parse_args( $query_string, array() );
    934962
    935963                // Build the canonical URL out of the redirect stack.
    936                 if ( isset( $bp->canonical_stack['base_url'] ) )
    937                         $url_stack[0] = $bp->canonical_stack['base_url'];
    938 
    939                 if ( isset( $bp->canonical_stack['component'] ) )
    940                         $url_stack[0] = trailingslashit( $url_stack[0] . $bp->canonical_stack['component'] );
    941 
    942                 if ( isset( $bp->canonical_stack['action'] ) )
    943                         $url_stack[0] = trailingslashit( $url_stack[0] . $bp->canonical_stack['action'] );
    944 
    945                 if ( !empty( $bp->canonical_stack['action_variables'] ) ) {
    946                         foreach( (array) $bp->canonical_stack['action_variables'] as $av ) {
    947                                 $url_stack[0] = trailingslashit( $url_stack[0] . $av );
    948                         }
    949                 }
    950 
    951                 // Add trailing slash.
    952                 $url_stack[0] = trailingslashit( $url_stack[0] );
    953 
    954                 // Stash in the $bp global.
    955                 $bp->canonical_stack['canonical_url'] = implode( '?', $url_stack );
     964                if ( isset( $bp->canonical_stack['base_url'] ) ) {
     965                        $base_url = $bp->canonical_stack['base_url'];
     966                } else {
     967                        $base_url = $requested_url;
     968
     969                        if ( bp_has_pretty_urls() ) {
     970                                $base_url = str_replace( '?' . $query_string, '', $requested_url );
     971                        }
     972                }
     973
     974                // This is a BP Members URL.
     975                if ( isset( $bp->canonical_stack['component'] ) ) {
     976                        $component_id  = 'members';
     977                        $path_chunks[] = $bp->canonical_stack['component'];
     978
     979                        if ( $query_args ) {
     980                                $query_args = array_diff_key(
     981                                        $query_args,
     982                                        array_fill_keys(
     983                                                array( 'bp_members', 'bp_member', 'bp_member_component' ),
     984                                                true
     985                                        )
     986                                );
     987                        }
     988                } else {
     989                        $component_id = 'groups';
     990                }
     991
     992                if ( isset( $bp->canonical_stack['action'] ) ) {
     993                        $path_chunks[]        = $bp->canonical_stack['action'];
     994                        $action_key           = 'bp_member_action';
     995                        $action_variables_key = 'bp_member_action_variables';
     996
     997                        if ( 'groups' === $component_id ) {
     998                                $action_key           = 'bp_group_action';
     999                                $action_variables_key = 'bp_group_action_variables';
     1000                        }
     1001
     1002                        if ( ! empty( $bp->canonical_stack['action_variables'] ) ) {
     1003                                $path_chunks = array_merge( $path_chunks, (array) $bp->canonical_stack['action_variables'] );
     1004                        } elseif ( isset( $query_args[ $action_variables_key ] ) ) {
     1005                                unset( $query_args[ $action_variables_key ] );
     1006                        }
     1007
     1008                        if ( $query_args ) {
     1009                                $query_args = array_diff_key(
     1010                                        $query_args,
     1011                                        array_fill_keys(
     1012                                                array( $action_key, $action_variables_key ),
     1013                                                true
     1014                                        )
     1015                                );
     1016                        }
     1017                } elseif ( isset( $query_args['bp_member_action'] ) && 'members' === $component_id ) {
     1018                        unset( $query_args['bp_member_action'] );
     1019                } elseif( isset( $query_args['bp_group_action'] ) && 'groups' === $component_id ) {
     1020                        unset( $query_args['bp_group_action'] );
     1021                }
     1022
     1023                if ( $path_chunks ) {
     1024                        if ( 'groups' === $component_id ) {
     1025                                $bp->canonical_stack['canonical_url'] = bp_get_group_url(
     1026                                        groups_get_current_group(),
     1027                                        bp_groups_get_path_chunks( $path_chunks )
     1028                                );
     1029                        } else {
     1030                                $bp->canonical_stack['canonical_url'] = bp_displayed_user_url( bp_members_get_path_chunks( $path_chunks ) );
     1031                        }
     1032                } else {
     1033                        $bp->canonical_stack['canonical_url'] = $base_url;
     1034                }
    9561035        }
    9571036
    9581037        $canonical_url = $bp->canonical_stack['canonical_url'];
    9591038
    960         if ( !$include_query_args ) {
    961                 $canonical_url = array_reverse( explode( '?', $canonical_url ) );
    962                 $canonical_url = array_pop( $canonical_url );
     1039        if ( $r['include_query_args'] && $query_args ) {
     1040                $canonical_url = add_query_arg( $query_args, $canonical_url );
    9631041        }
    9641042
     
    10141092                remove_action( 'template_redirect', 'redirect_canonical' );
    10151093}
    1016 add_action( 'bp_init', '_bp_maybe_remove_redirect_canonical' );
    10171094
    10181095/**
Note: See TracChangeset for help on using the changeset viewer.