Changeset 13422
- Timestamp:
- 02/15/2023 10:23:05 AM (2 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/classes/class-bp-component.php
r13399 r13422 59 59 */ 60 60 public $has_directory = false; 61 62 /** 63 * Directory's permalink structure for the component. 64 * 65 * @since 12.0.0 66 * @var string 67 */ 68 public $directory_permastruct = ''; 69 70 /** 71 * List of available rewrite IDs for the component. 72 * 73 * @since 12.0.0 74 * @var array 75 */ 76 public $rewrite_ids = array(); 61 77 62 78 /** … … 258 274 'root_slug' => '', 259 275 'has_directory' => false, 276 'rewrite_ids' => array(), 260 277 'directory_title' => '', 261 278 'notification_callback' => '', … … 307 324 $this->has_directory = apply_filters( 'bp_' . $this->id . '_has_directory', $r['has_directory'] ); 308 325 326 $rewrite_ids = bp_parse_args( 327 /** 328 * Filters the component's rewrite IDs if available. 329 * 330 * @since 12.0.0 331 * 332 * @param array $value The list of rewrite IDs for the component. 333 */ 334 (array) apply_filters( 'bp_' . $this->id. '_rewrite_ids', $r['rewrite_ids'] ), 335 array_fill_keys( array_keys( bp_rewrites_get_default_url_chunks() ), '' ) 336 ); 337 338 if ( array_filter( $rewrite_ids ) ) { 339 foreach ( $rewrite_ids as $rewrite_id_key => $rewrite_id_value ) { 340 if ( ! $rewrite_id_value ) { 341 continue; 342 } 343 344 $this->rewrite_ids[ sanitize_key( $rewrite_id_key ) ] = 'bp_' . str_replace( 'bp_', '', sanitize_key( $rewrite_id_value ) ); 345 } 346 } 347 348 // Set the component's directory permastruct early so that it's available to build links. 349 if ( true === $this->has_directory && isset( $this->rewrite_ids['directory'] ) ) { 350 $this->directory_permastruct = $this->root_slug . '/%' . $this->rewrite_ids['directory'] . '%'; 351 } 352 309 353 /** 310 354 * Filters the component's directory title. … … 503 547 504 548 // Add the rewrite tags. 505 add_action( 'bp_add_rewrite_tags', array( $this, 'add_rewrite_tags' ), 10 );549 add_action( 'bp_add_rewrite_tags', array( $this, 'add_rewrite_tags' ), 10, 0 ); 506 550 507 551 // Add the rewrite rules. 508 add_action( 'bp_add_rewrite_rules', array( $this, 'add_rewrite_rules' ), 10 );552 add_action( 'bp_add_rewrite_rules', array( $this, 'add_rewrite_rules' ), 10, 0 ); 509 553 510 554 // Add the permalink structure. … … 833 877 834 878 /** 835 * Add any additional rewrite tags. 836 * 837 * @since 1.5.0 838 * 839 */ 840 public function add_rewrite_tags() { 879 * Add Component's additional rewrite tags. 880 * 881 * @since 1.5.0 882 * @since 12.0.0 Adds the `$rewrite_tags` parameter. 883 * 884 * @param array $rewrite_tags Array of arguments list used to add WordPress rewrite tags. 885 * Each argument key needs to match one of `$this->rewrite_ids` keys. 886 */ 887 public function add_rewrite_tags( $rewrite_tags = array() ) { 888 if ( array_filter( $this->rewrite_ids ) ) { 889 $chunks = bp_rewrites_get_default_url_chunks(); 890 891 foreach ( $this->rewrite_ids as $rewrite_id_key => $rewrite_id_value ) { 892 $rewrite_tag = '%' . $rewrite_id_value . '%'; 893 $rewrite_regex = ''; 894 895 if ( isset( $rewrite_tags[ $rewrite_id_key ] ) ) { 896 $rewrite_regex = $rewrite_tags[ $rewrite_id_key ]; 897 } elseif ( isset( $chunks[ $rewrite_id_key ]['regex'] ) ) { 898 $rewrite_regex = $chunks[ $rewrite_id_key ]['regex']; 899 } 900 901 if ( ! $rewrite_regex ) { 902 continue; 903 } 904 905 add_rewrite_tag( $rewrite_tag, $rewrite_regex ); 906 } 907 } 841 908 842 909 /** … … 851 918 852 919 /** 853 * Add anyadditional rewrite rules.920 * Add Component's additional rewrite rules. 854 921 * 855 922 * @since 1.9.0 856 * 857 */ 858 public function add_rewrite_rules() { 923 * @since 12.0.0 Adds the `$rewrite_rules` parameter. 924 * 925 * @param array $rewrite_rules { 926 * Array of associative arrays of arguments list used to add WordPress rewrite rules. 927 * Each associative array needs to include the following keys. 928 * 929 * @type string $regex Regular expression to match request against. Required. 930 * @type string $query The corresponding query vars for this rewrite rule. Required. 931 * @type int $order The insertion order for the rewrite rule. Required. 932 * @type string $priority The Priority of the new rule. Accepts 'top' or 'bottom'. Optional. 933 * Default 'top'. 934 * } 935 */ 936 public function add_rewrite_rules( $rewrite_rules = array() ) { 937 if ( array_filter( $this->rewrite_ids ) ) { 938 $priority = 'top'; 939 $chunks = array_merge( bp_rewrites_get_default_url_chunks(), $rewrite_rules ); 940 941 $rules = bp_sort_by_key( $chunks, 'order', 'num', true ); 942 $reversed_rules = array_reverse( $rules, true ); 943 944 $regex = ''; 945 $query = ''; 946 $match = 1; 947 948 // Build rewrite rules for the component. 949 foreach ( $reversed_rules as $rule_key => $rule_information ) { 950 if ( ! isset( $this->rewrite_ids[ $rule_key ] ) ) { 951 unset( $rules[ $rule_key ] ); 952 continue; 953 } 954 955 // The query is already set, use it. 956 if ( isset( $rule_information['query'] ) ) { 957 $rules[ $rule_key ]['regex'] = $rule_information['regex']; 958 $rules[ $rule_key ]['query'] = $rule_information['query']; 959 } elseif ( 'directory' === $rule_key ) { 960 $regex = $this->root_slug; 961 $query = 'index.php?' . $this->rewrite_ids['directory'] . '=1'; 962 963 $rules[ $rule_key ]['regex'] = $regex; 964 $rules[ $rule_key ]['query'] = $query; 965 } else { 966 $regex = trailingslashit( $regex ) . $rule_information['regex']; 967 $query .= '&' . $this->rewrite_ids[ $rule_key ] . '=$matches['. $match .']'; 968 $match += 1; 969 970 $rules[ $rule_key ]['regex'] = $regex . '/?$'; 971 $rules[ $rule_key ]['query'] = $query; 972 } 973 } 974 975 // Then register the rewrite rules. 976 if ( $rules ) { 977 foreach ( $rules as $rewrite_rule ) { 978 if ( ! isset( $rewrite_rule['regex'] ) || ! isset( $rewrite_rule['query'] ) ) { 979 continue; 980 } 981 982 if ( ! isset( $rewrite_rule['priority'] ) || ! $rewrite_rule['priority'] ) { 983 $rewrite_rule['priority'] = $priority; 984 } 985 986 add_rewrite_rule( $rewrite_rule['regex'], $rewrite_rule['query'], $rewrite_rule['priority'] ); 987 } 988 } 989 } 859 990 860 991 /** … … 869 1000 870 1001 /** 871 * Add anypermalink structures.1002 * Add Component's permalink structures. 872 1003 * 873 1004 * @since 1.9.0 874 * 875 */ 876 public function add_permastructs() { 1005 * @since 12.0.0 Adds the `$permastructs` parameter. 1006 * 1007 * @param array $permastructs { 1008 * Array of associative arrays of arguments list used to register WordPress additional permalink structures. 1009 * Each array enty is keyed with the permalink structure. 1010 * Each associative array needs to include the following keys. 1011 * 1012 * @type string $permastruct The permalink structure. Required. 1013 * @type array $args The permalink structure arguments. Optional. 1014 * } 1015 */ 1016 public function add_permastructs( $permastructs = array() ) { 1017 // Always include the directory permastruct when the component has a directory. 1018 if ( isset( $this->rewrite_ids['directory'] ) ) { 1019 $directory_permastruct = array( 1020 $this->rewrite_ids['directory'] => array( 1021 'permastruct' => $this->directory_permastruct, 1022 'args' => array(), 1023 ), 1024 ); 1025 1026 $permastructs = array_merge( $directory_permastruct, (array) $permastructs ); 1027 } 1028 1029 if ( $permastructs ) { 1030 foreach ( $permastructs as $name => $params ) { 1031 if ( ! $name || ! isset( $params['permastruct'] ) || ! $params['permastruct'] ) { 1032 continue; 1033 } 1034 1035 if ( ! $params['args'] ) { 1036 $params['args'] = array(); 1037 } 1038 1039 $args = wp_parse_args( 1040 $params['args'], 1041 array( 1042 'with_front' => false, 1043 'ep_mask' => EP_NONE, 1044 'paged' => true, 1045 'feed' => false, 1046 'forcomments' => false, 1047 'walk_dirs' => true, 1048 'endpoints' => false, 1049 ) 1050 ); 1051 1052 // Add the permastruct. 1053 add_permastruct( $name, $params['permastruct'], $args ); 1054 } 1055 } 877 1056 878 1057 /** … … 891 1070 * @since 1.9.0 892 1071 * 893 *894 1072 * @param object $query The main WP_Query. 895 1073 */ 896 1074 public function parse_query( $query ) { 1075 if ( is_buddypress() ) { 1076 add_filter( 'posts_pre_query', array( $this, 'pre_query' ), 10, 2 ); 1077 } 897 1078 898 1079 /** … … 906 1087 */ 907 1088 do_action_ref_array( 'bp_' . $this->id . '_parse_query', array( &$query ) ); 1089 } 1090 1091 /** 1092 * Make sure to avoid querying for regular posts when displaying a BuddyPress page. 1093 * 1094 * @since 12.0.0 1095 * 1096 * @param null $retval A null value to use the regular WP Query. 1097 * @param WP_Query $query The WP Query object. 1098 * @return null|array Null if not displaying a BuddyPress page. 1099 * An array containing the BuddyPress directory post otherwise. 1100 */ 1101 public function pre_query( $retval = null, $query = null ) { 1102 remove_filter( 'posts_pre_query', array( $this, 'pre_query' ), 10 ); 1103 1104 $queried_object = $query->get_queried_object(); 1105 1106 if ( $queried_object instanceof \WP_Post && 'buddypress' === get_post_type( $queried_object ) ) { 1107 // Only include the queried directory post into returned posts. 1108 $retval = array( $queried_object ); 1109 } 1110 1111 return $retval; 908 1112 } 909 1113 -
trunk/src/class-buddypress.php
r13413 r13422 623 623 require $this->plugin_dir . 'bp-core/bp-core-adminbar.php'; 624 624 require $this->plugin_dir . 'bp-core/bp-core-buddybar.php'; 625 require $this->plugin_dir . 'bp-core/bp-core-rewrites.php'; 625 626 require $this->plugin_dir . 'bp-core/bp-core-catchuri.php'; 626 627 require $this->plugin_dir . 'bp-core/bp-core-functions.php'; -
trunk/tests/phpunit/assets/class-bptest-component.php
r13108 r13422 35 35 parent::setup_globals( $this->globals ); 36 36 } 37 38 public function add_rewrite_tags( $rewrite_tags = array() ) { 39 parent::add_rewrite_tags( $rewrite_tags ); 40 } 41 42 public function add_rewrite_rules( $rewrite_rules = array() ) { 43 parent::add_rewrite_rules( $rewrite_rules ); 44 } 45 46 public function add_permastructs( $permastructs = array() ) { 47 parent::add_permastructs( $permastructs ); 48 } 37 49 } -
trunk/tests/phpunit/testcases/core/class-bp-component.php
r13314 r13422 93 93 $this->assertEquals( $expected, $example->block_globals['bp/example-block']->props ); 94 94 } 95 96 /** 97 * @group bp_rewrites 98 */ 99 public function test_component_rewrite_globals() { 100 $expected = array( 101 'directory' => 'bp_examples', 102 'single_item' => 'bp_example', 103 'single_item_action' => 'bp_example_action', 104 ); 105 106 $example = new BPTest_Component( 107 array( 108 'globals' => array( 109 'rewrite_ids' => array( 110 'directory' => 'examples ', 111 'single_item' => 'Exam?ple', 112 'single_item_action' => 'bp_example_action', 113 ) 114 ), 115 ) 116 ); 117 118 do_action( 'bp_setup_globals' ); 119 120 $this->assertEquals( $expected, $example->rewrite_ids ); 121 } 122 123 /** 124 * @group bp_rewrites 125 */ 126 public function test_component_add_rewrite_tags() { 127 $example = new BPTest_Component( 128 array( 129 'globals' => array( 130 'rewrite_ids' => array( 131 'directory' => 'examples', 132 'directory_type' => 'example_type', 133 ) 134 ), 135 ) 136 ); 137 138 do_action( 'bp_setup_globals' ); 139 140 $expected_directory_regex = '([1]{1,})'; 141 $rewrite_tags = array( 142 'directory_type' => '([^/]+)', 143 ); 144 145 $example->add_rewrite_tags( $rewrite_tags ); 146 147 global $wp_rewrite; 148 149 $position = array_search( '%' . $example->rewrite_ids['directory'] . '%', $wp_rewrite->rewritecode, true ); 150 $this->assertEquals( $wp_rewrite->rewritereplace[ $position ], $expected_directory_regex ); 151 152 $position = array_search( '%' . $example->rewrite_ids['directory_type'] . '%', $wp_rewrite->rewritecode, true ); 153 $this->assertEquals( $wp_rewrite->rewritereplace[ $position ], $rewrite_tags['directory_type'] ); 154 } 155 156 /** 157 * @group bp_rewrites 158 */ 159 public function test_component_add_rewrite_rules() { 160 $example = new BPTest_Component( 161 array( 162 'globals' => array( 163 'root_slug' => 'examples', 164 'rewrite_ids' => array( 165 'directory' => 'examples', 166 'directory_type' => 'example_type', 167 'single_item' => 'example', 168 'single_item_component' => 'example_component', 169 ) 170 ), 171 ) 172 ); 173 174 do_action( 'bp_setup_globals' ); 175 176 $rewrite_tags = array( 177 'directory_type' => '([^/]+)', 178 ); 179 180 $example->add_rewrite_tags( $rewrite_tags ); 181 182 $rewrite_rules = array( 183 'directory_type' => array( 184 'order' => 95, 185 'regex' => $example->root_slug . '/type/([^/]+)/?$', 186 'query' => 'index.php?' . $example->rewrite_ids['directory'] . '=1&' . $example->rewrite_ids['directory_type'] . '=$matches[1]', 187 ), 188 ); 189 190 $example->add_rewrite_rules( $rewrite_rules ); 191 192 global $wp_rewrite; 193 $this->assertEquals( $wp_rewrite->extra_rules_top[ $rewrite_rules['directory_type']['regex'] ], $rewrite_rules['directory_type']['query'] ); 194 } 195 196 /** 197 * @group bp_rewrites 198 */ 199 public function test_component_add_permastructs() { 200 $example = new BPTest_Component( 201 array( 202 'globals' => array( 203 'has_directory' => true, 204 'root_slug' => 'examples', 205 'rewrite_ids' => array( 206 'directory' => 'examples', 207 'example_signup' => 'signup', 208 ) 209 ), 210 ) 211 ); 212 213 do_action( 'bp_setup_globals' ); 214 215 $expected = 'example-signup/%' . $example->rewrite_ids['example_signup'] . '%'; 216 $permastructs = array( 217 $example->rewrite_ids['example_signup'] => array( 218 'permastruct' => $expected, 219 'args' => array(), 220 ), 221 ); 222 223 $example->add_permastructs( $permastructs ); 224 225 global $wp_rewrite; 226 227 // The directory permastruct should be created automatically. 228 $this->assertTrue( isset( $wp_rewrite->extra_permastructs['bp_examples'] ) ); 229 230 // The custom permastruct should be created as requested. 231 $this->assertEquals( $wp_rewrite->extra_permastructs[ $example->rewrite_ids['example_signup'] ]['struct'], $expected ); 232 } 95 233 }
Note: See TracChangeset
for help on using the changeset viewer.