Changeset 13461 for trunk/src/bp-core/bp-core-catchuri.php
- Timestamp:
- 04/24/2023 02:32:27 AM (20 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-catchuri.php
r13436 r13461 386 386 // Reset the keys by merging with an empty array. 387 387 $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 */ 395 function 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'] ); 388 411 } 389 412 … … 808 831 * @param bool $value Whether or not to do canonical redirects. Default true. 809 832 */ 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 ) ) { 811 834 // If this is a POST request, don't do a canonical redirect. 812 835 // This is for backward compatibility with plugins that submit form requests to 813 836 // non-canonical URLs. Plugin authors should do their best to use canonical URLs in 814 837 // their form actions. 815 if ( ! empty( $_POST ) ) {838 if ( ! empty( $_POST ) ) { 816 839 return; 817 840 } … … 819 842 // Build the URL in the address bar. 820 843 $requested_url = bp_get_requested_url(); 844 $query_args = ''; 821 845 822 846 // 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(); 828 855 829 856 // 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 ) ) { 832 858 $bp = buddypress(); 833 859 … … 841 867 } 842 868 843 if ( ! empty( $query_args ) ) {869 if ( ! empty( $query_args ) ) { 844 870 $canonical_url .= '?' . $query_args; 845 871 } … … 891 917 $defaults 892 918 ); 893 894 extract( $r );895 919 896 920 // Special case: when a BuddyPress directory (eg example.com/members) … … 929 953 // Build the URL in the address bar. 930 954 $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() ); 934 962 935 963 // 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 } 956 1035 } 957 1036 958 1037 $canonical_url = $bp->canonical_stack['canonical_url']; 959 1038 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 ); 963 1041 } 964 1042 … … 1014 1092 remove_action( 'template_redirect', 'redirect_canonical' ); 1015 1093 } 1016 add_action( 'bp_init', '_bp_maybe_remove_redirect_canonical' );1017 1094 1018 1095 /**
Note: See TracChangeset
for help on using the changeset viewer.