203 * @var string|bool $rest_controller_class |
208 * @var string|bool $rest_controller_class |
204 */ |
209 */ |
205 public $rest_controller_class; |
210 public $rest_controller_class; |
206 |
211 |
207 /** |
212 /** |
|
213 * The default term name for this taxonomy. If you pass an array you have |
|
214 * to set 'name' and optionally 'slug' and 'description'. |
|
215 * |
|
216 * @since 5.5.0 |
|
217 * @var array|string |
|
218 */ |
|
219 public $default_term; |
|
220 |
|
221 /** |
|
222 * The controller instance for this taxonomy's REST API endpoints. |
|
223 * |
|
224 * Lazily computed. Should be accessed using {@see WP_Taxonomy::get_rest_controller()}. |
|
225 * |
|
226 * @since 5.5.0 |
|
227 * @var WP_REST_Controller $rest_controller |
|
228 */ |
|
229 public $rest_controller; |
|
230 |
|
231 /** |
208 * Whether it is a built-in taxonomy. |
232 * Whether it is a built-in taxonomy. |
209 * |
233 * |
210 * @since 4.7.0 |
234 * @since 4.7.0 |
211 * @var bool |
235 * @var bool |
212 */ |
236 */ |
215 /** |
239 /** |
216 * Constructor. |
240 * Constructor. |
217 * |
241 * |
218 * @since 4.7.0 |
242 * @since 4.7.0 |
219 * |
243 * |
220 * @global WP $wp WP instance. |
244 * @global WP $wp Current WordPress environment instance. |
221 * |
245 * |
222 * @param string $taxonomy Taxonomy key, must not exceed 32 characters. |
246 * @param string $taxonomy Taxonomy key, must not exceed 32 characters. |
223 * @param array|string $object_type Name of the object type for the taxonomy object. |
247 * @param array|string $object_type Name of the object type for the taxonomy object. |
224 * @param array|string $args Optional. Array or query string of arguments for registering a taxonomy. |
248 * @param array|string $args Optional. Array or query string of arguments for registering a taxonomy. |
225 * Default empty array. |
249 * Default empty array. |
271 'query_var' => $this->name, |
295 'query_var' => $this->name, |
272 'update_count_callback' => '', |
296 'update_count_callback' => '', |
273 'show_in_rest' => false, |
297 'show_in_rest' => false, |
274 'rest_base' => false, |
298 'rest_base' => false, |
275 'rest_controller_class' => false, |
299 'rest_controller_class' => false, |
|
300 'default_term' => null, |
276 '_builtin' => false, |
301 '_builtin' => false, |
277 ); |
302 ); |
278 |
303 |
279 $args = array_merge( $defaults, $args ); |
304 $args = array_merge( $defaults, $args ); |
280 |
305 |
281 // If not set, default to the setting for public. |
306 // If not set, default to the setting for 'public'. |
282 if ( null === $args['publicly_queryable'] ) { |
307 if ( null === $args['publicly_queryable'] ) { |
283 $args['publicly_queryable'] = $args['public']; |
308 $args['publicly_queryable'] = $args['public']; |
284 } |
309 } |
285 |
310 |
286 if ( false !== $args['query_var'] && ( is_admin() || false !== $args['publicly_queryable'] ) ) { |
311 if ( false !== $args['query_var'] && ( is_admin() || false !== $args['publicly_queryable'] ) ) { |
288 $args['query_var'] = $this->name; |
313 $args['query_var'] = $this->name; |
289 } else { |
314 } else { |
290 $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] ); |
315 $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] ); |
291 } |
316 } |
292 } else { |
317 } else { |
293 // Force query_var to false for non-public taxonomies. |
318 // Force 'query_var' to false for non-public taxonomies. |
294 $args['query_var'] = false; |
319 $args['query_var'] = false; |
295 } |
320 } |
296 |
321 |
297 if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { |
322 if ( false !== $args['rewrite'] && ( is_admin() || get_option( 'permalink_structure' ) ) ) { |
298 $args['rewrite'] = wp_parse_args( |
323 $args['rewrite'] = wp_parse_args( |
299 $args['rewrite'], |
324 $args['rewrite'], |
300 array( |
325 array( |
301 'with_front' => true, |
326 'with_front' => true, |
302 'hierarchical' => false, |
327 'hierarchical' => false, |
307 if ( empty( $args['rewrite']['slug'] ) ) { |
332 if ( empty( $args['rewrite']['slug'] ) ) { |
308 $args['rewrite']['slug'] = sanitize_title_with_dashes( $this->name ); |
333 $args['rewrite']['slug'] = sanitize_title_with_dashes( $this->name ); |
309 } |
334 } |
310 } |
335 } |
311 |
336 |
312 // If not set, default to the setting for public. |
337 // If not set, default to the setting for 'public'. |
313 if ( null === $args['show_ui'] ) { |
338 if ( null === $args['show_ui'] ) { |
314 $args['show_ui'] = $args['public']; |
339 $args['show_ui'] = $args['public']; |
315 } |
340 } |
316 |
341 |
317 // If not set, default to the setting for show_ui. |
342 // If not set, default to the setting for 'show_ui'. |
318 if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) { |
343 if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) { |
319 $args['show_in_menu'] = $args['show_ui']; |
344 $args['show_in_menu'] = $args['show_ui']; |
320 } |
345 } |
321 |
346 |
322 // If not set, default to the setting for public. |
347 // If not set, default to the setting for 'public'. |
323 if ( null === $args['show_in_nav_menus'] ) { |
348 if ( null === $args['show_in_nav_menus'] ) { |
324 $args['show_in_nav_menus'] = $args['public']; |
349 $args['show_in_nav_menus'] = $args['public']; |
325 } |
350 } |
326 |
351 |
327 // If not set, default to the setting for show_ui. |
352 // If not set, default to the setting for 'show_ui'. |
328 if ( null === $args['show_tagcloud'] ) { |
353 if ( null === $args['show_tagcloud'] ) { |
329 $args['show_tagcloud'] = $args['show_ui']; |
354 $args['show_tagcloud'] = $args['show_ui']; |
330 } |
355 } |
331 |
356 |
332 // If not set, default to the setting for show_ui. |
357 // If not set, default to the setting for 'show_ui'. |
333 if ( null === $args['show_in_quick_edit'] ) { |
358 if ( null === $args['show_in_quick_edit'] ) { |
334 $args['show_in_quick_edit'] = $args['show_ui']; |
359 $args['show_in_quick_edit'] = $args['show_ui']; |
335 } |
360 } |
336 |
361 |
337 $default_caps = array( |
362 $default_caps = array( |
344 $args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] ); |
369 $args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] ); |
345 unset( $args['capabilities'] ); |
370 unset( $args['capabilities'] ); |
346 |
371 |
347 $args['object_type'] = array_unique( (array) $object_type ); |
372 $args['object_type'] = array_unique( (array) $object_type ); |
348 |
373 |
349 // If not set, use the default meta box |
374 // If not set, use the default meta box. |
350 if ( null === $args['meta_box_cb'] ) { |
375 if ( null === $args['meta_box_cb'] ) { |
351 if ( $args['hierarchical'] ) { |
376 if ( $args['hierarchical'] ) { |
352 $args['meta_box_cb'] = 'post_categories_meta_box'; |
377 $args['meta_box_cb'] = 'post_categories_meta_box'; |
353 } else { |
378 } else { |
354 $args['meta_box_cb'] = 'post_tags_meta_box'; |
379 $args['meta_box_cb'] = 'post_tags_meta_box'; |
369 $args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_input'; |
394 $args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_input'; |
370 break; |
395 break; |
371 } |
396 } |
372 } |
397 } |
373 |
398 |
|
399 // Default taxonomy term. |
|
400 if ( ! empty( $args['default_term'] ) ) { |
|
401 if ( ! is_array( $args['default_term'] ) ) { |
|
402 $args['default_term'] = array( 'name' => $args['default_term'] ); |
|
403 } |
|
404 $args['default_term'] = wp_parse_args( |
|
405 $args['default_term'], |
|
406 array( |
|
407 'name' => '', |
|
408 'slug' => '', |
|
409 'description' => '', |
|
410 ) |
|
411 ); |
|
412 } |
|
413 |
374 foreach ( $args as $property_name => $property_value ) { |
414 foreach ( $args as $property_name => $property_value ) { |
375 $this->$property_name = $property_value; |
415 $this->$property_name = $property_value; |
376 } |
416 } |
377 |
417 |
378 $this->labels = get_taxonomy_labels( $this ); |
418 $this->labels = get_taxonomy_labels( $this ); |
393 // Non-publicly queryable taxonomies should not register query vars, except in the admin. |
433 // Non-publicly queryable taxonomies should not register query vars, except in the admin. |
394 if ( false !== $this->query_var && $wp ) { |
434 if ( false !== $this->query_var && $wp ) { |
395 $wp->add_query_var( $this->query_var ); |
435 $wp->add_query_var( $this->query_var ); |
396 } |
436 } |
397 |
437 |
398 if ( false !== $this->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { |
438 if ( false !== $this->rewrite && ( is_admin() || get_option( 'permalink_structure' ) ) ) { |
399 if ( $this->hierarchical && $this->rewrite['hierarchical'] ) { |
439 if ( $this->hierarchical && $this->rewrite['hierarchical'] ) { |
400 $tag = '(.+?)'; |
440 $tag = '(.+?)'; |
401 } else { |
441 } else { |
402 $tag = '([^/]+)'; |
442 $tag = '([^/]+)'; |
403 } |
443 } |
445 * @since 4.7.0 |
485 * @since 4.7.0 |
446 */ |
486 */ |
447 public function remove_hooks() { |
487 public function remove_hooks() { |
448 remove_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' ); |
488 remove_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' ); |
449 } |
489 } |
|
490 |
|
491 /** |
|
492 * Gets the REST API controller for this taxonomy. |
|
493 * |
|
494 * Will only instantiate the controller class once per request. |
|
495 * |
|
496 * @since 5.5.0 |
|
497 * |
|
498 * @return WP_REST_Controller|null The controller instance, or null if the taxonomy |
|
499 * is set not to show in rest. |
|
500 */ |
|
501 public function get_rest_controller() { |
|
502 if ( ! $this->show_in_rest ) { |
|
503 return null; |
|
504 } |
|
505 |
|
506 $class = $this->rest_controller_class ? $this->rest_controller_class : WP_REST_Terms_Controller::class; |
|
507 |
|
508 if ( ! class_exists( $class ) ) { |
|
509 return null; |
|
510 } |
|
511 |
|
512 if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) { |
|
513 return null; |
|
514 } |
|
515 |
|
516 if ( ! $this->rest_controller ) { |
|
517 $this->rest_controller = new $class( $this->name ); |
|
518 } |
|
519 |
|
520 if ( ! ( $this->rest_controller instanceof $class ) ) { |
|
521 return null; |
|
522 } |
|
523 |
|
524 return $this->rest_controller; |
|
525 } |
450 } |
526 } |