[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Taxonomy API: WP_Taxonomy class 4 * 5 * @package WordPress 6 * @subpackage Taxonomy 7 * @since 4.7.0 8 */ 9 10 /** 11 * Core class used for interacting with taxonomies. 12 * 13 * @since 4.7.0 14 */ 15 final class WP_Taxonomy { 16 /** 17 * Taxonomy key. 18 * 19 * @since 4.7.0 20 * @var string 21 */ 22 public $name; 23 24 /** 25 * Name of the taxonomy shown in the menu. Usually plural. 26 * 27 * @since 4.7.0 28 * @var string 29 */ 30 public $label; 31 32 /** 33 * Labels object for this taxonomy. 34 * 35 * If not set, tag labels are inherited for non-hierarchical types 36 * and category labels for hierarchical ones. 37 * 38 * @see get_taxonomy_labels() 39 * 40 * @since 4.7.0 41 * @var stdClass 42 */ 43 public $labels; 44 45 /** 46 * A short descriptive summary of what the taxonomy is for. 47 * 48 * @since 4.7.0 49 * @var string 50 */ 51 public $description = ''; 52 53 /** 54 * Whether a taxonomy is intended for use publicly either via the admin interface or by front-end users. 55 * 56 * @since 4.7.0 57 * @var bool 58 */ 59 public $public = true; 60 61 /** 62 * Whether the taxonomy is publicly queryable. 63 * 64 * @since 4.7.0 65 * @var bool 66 */ 67 public $publicly_queryable = true; 68 69 /** 70 * Whether the taxonomy is hierarchical. 71 * 72 * @since 4.7.0 73 * @var bool 74 */ 75 public $hierarchical = false; 76 77 /** 78 * Whether to generate and allow a UI for managing terms in this taxonomy in the admin. 79 * 80 * @since 4.7.0 81 * @var bool 82 */ 83 public $show_ui = true; 84 85 /** 86 * Whether to show the taxonomy in the admin menu. 87 * 88 * If true, the taxonomy is shown as a submenu of the object type menu. If false, no menu is shown. 89 * 90 * @since 4.7.0 91 * @var bool 92 */ 93 public $show_in_menu = true; 94 95 /** 96 * Whether the taxonomy is available for selection in navigation menus. 97 * 98 * @since 4.7.0 99 * @var bool 100 */ 101 public $show_in_nav_menus = true; 102 103 /** 104 * Whether to list the taxonomy in the tag cloud widget controls. 105 * 106 * @since 4.7.0 107 * @var bool 108 */ 109 public $show_tagcloud = true; 110 111 /** 112 * Whether to show the taxonomy in the quick/bulk edit panel. 113 * 114 * @since 4.7.0 115 * @var bool 116 */ 117 public $show_in_quick_edit = true; 118 119 /** 120 * Whether to display a column for the taxonomy on its post type listing screens. 121 * 122 * @since 4.7.0 123 * @var bool 124 */ 125 public $show_admin_column = false; 126 127 /** 128 * The callback function for the meta box display. 129 * 130 * @since 4.7.0 131 * @var bool|callable 132 */ 133 public $meta_box_cb = null; 134 135 /** 136 * The callback function for sanitizing taxonomy data saved from a meta box. 137 * 138 * @since 5.1.0 139 * @var callable 140 */ 141 public $meta_box_sanitize_cb = null; 142 143 /** 144 * An array of object types this taxonomy is registered for. 145 * 146 * @since 4.7.0 147 * @var array 148 */ 149 public $object_type = null; 150 151 /** 152 * Capabilities for this taxonomy. 153 * 154 * @since 4.7.0 155 * @var stdClass 156 */ 157 public $cap; 158 159 /** 160 * Rewrites information for this taxonomy. 161 * 162 * @since 4.7.0 163 * @var array|false 164 */ 165 public $rewrite; 166 167 /** 168 * Query var string for this taxonomy. 169 * 170 * @since 4.7.0 171 * @var string|false 172 */ 173 public $query_var; 174 175 /** 176 * Function that will be called when the count is updated. 177 * 178 * @since 4.7.0 179 * @var callable 180 */ 181 public $update_count_callback; 182 183 /** 184 * Whether this taxonomy should appear in the REST API. 185 * 186 * Default false. If true, standard endpoints will be registered with 187 * respect to $rest_base and $rest_controller_class. 188 * 189 * @since 4.7.4 190 * @var bool $show_in_rest 191 */ 192 public $show_in_rest; 193 194 /** 195 * The base path for this taxonomy's REST API endpoints. 196 * 197 * @since 4.7.4 198 * @var string|bool $rest_base 199 */ 200 public $rest_base; 201 202 /** 203 * The controller for this taxonomy's REST API endpoints. 204 * 205 * Custom controllers must extend WP_REST_Controller. 206 * 207 * @since 4.7.4 208 * @var string|bool $rest_controller_class 209 */ 210 public $rest_controller_class; 211 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 /** 232 * Whether it is a built-in taxonomy. 233 * 234 * @since 4.7.0 235 * @var bool 236 */ 237 public $_builtin; 238 239 /** 240 * Constructor. 241 * 242 * See the register_taxonomy() function for accepted arguments for `$args`. 243 * 244 * @since 4.7.0 245 * 246 * @global WP $wp Current WordPress environment instance. 247 * 248 * @param string $taxonomy Taxonomy key, must not exceed 32 characters. 249 * @param array|string $object_type Name of the object type for the taxonomy object. 250 * @param array|string $args Optional. Array or query string of arguments for registering a taxonomy. 251 * Default empty array. 252 */ 253 public function __construct( $taxonomy, $object_type, $args = array() ) { 254 $this->name = $taxonomy; 255 256 $this->set_props( $object_type, $args ); 257 } 258 259 /** 260 * Sets taxonomy properties. 261 * 262 * See the register_taxonomy() function for accepted arguments for `$args`. 263 * 264 * @since 4.7.0 265 * 266 * @param array|string $object_type Name of the object type for the taxonomy object. 267 * @param array|string $args Array or query string of arguments for registering a taxonomy. 268 */ 269 public function set_props( $object_type, $args ) { 270 $args = wp_parse_args( $args ); 271 272 /** 273 * Filters the arguments for registering a taxonomy. 274 * 275 * @since 4.4.0 276 * 277 * @param array $args Array of arguments for registering a taxonomy. 278 * See the register_taxonomy() function for accepted arguments. 279 * @param string $taxonomy Taxonomy key. 280 * @param string[] $object_type Array of names of object types for the taxonomy. 281 */ 282 $args = apply_filters( 'register_taxonomy_args', $args, $this->name, (array) $object_type ); 283 284 $defaults = array( 285 'labels' => array(), 286 'description' => '', 287 'public' => true, 288 'publicly_queryable' => null, 289 'hierarchical' => false, 290 'show_ui' => null, 291 'show_in_menu' => null, 292 'show_in_nav_menus' => null, 293 'show_tagcloud' => null, 294 'show_in_quick_edit' => null, 295 'show_admin_column' => false, 296 'meta_box_cb' => null, 297 'meta_box_sanitize_cb' => null, 298 'capabilities' => array(), 299 'rewrite' => true, 300 'query_var' => $this->name, 301 'update_count_callback' => '', 302 'show_in_rest' => false, 303 'rest_base' => false, 304 'rest_controller_class' => false, 305 'default_term' => null, 306 '_builtin' => false, 307 ); 308 309 $args = array_merge( $defaults, $args ); 310 311 // If not set, default to the setting for 'public'. 312 if ( null === $args['publicly_queryable'] ) { 313 $args['publicly_queryable'] = $args['public']; 314 } 315 316 if ( false !== $args['query_var'] && ( is_admin() || false !== $args['publicly_queryable'] ) ) { 317 if ( true === $args['query_var'] ) { 318 $args['query_var'] = $this->name; 319 } else { 320 $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] ); 321 } 322 } else { 323 // Force 'query_var' to false for non-public taxonomies. 324 $args['query_var'] = false; 325 } 326 327 if ( false !== $args['rewrite'] && ( is_admin() || get_option( 'permalink_structure' ) ) ) { 328 $args['rewrite'] = wp_parse_args( 329 $args['rewrite'], 330 array( 331 'with_front' => true, 332 'hierarchical' => false, 333 'ep_mask' => EP_NONE, 334 ) 335 ); 336 337 if ( empty( $args['rewrite']['slug'] ) ) { 338 $args['rewrite']['slug'] = sanitize_title_with_dashes( $this->name ); 339 } 340 } 341 342 // If not set, default to the setting for 'public'. 343 if ( null === $args['show_ui'] ) { 344 $args['show_ui'] = $args['public']; 345 } 346 347 // If not set, default to the setting for 'show_ui'. 348 if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) { 349 $args['show_in_menu'] = $args['show_ui']; 350 } 351 352 // If not set, default to the setting for 'public'. 353 if ( null === $args['show_in_nav_menus'] ) { 354 $args['show_in_nav_menus'] = $args['public']; 355 } 356 357 // If not set, default to the setting for 'show_ui'. 358 if ( null === $args['show_tagcloud'] ) { 359 $args['show_tagcloud'] = $args['show_ui']; 360 } 361 362 // If not set, default to the setting for 'show_ui'. 363 if ( null === $args['show_in_quick_edit'] ) { 364 $args['show_in_quick_edit'] = $args['show_ui']; 365 } 366 367 $default_caps = array( 368 'manage_terms' => 'manage_categories', 369 'edit_terms' => 'manage_categories', 370 'delete_terms' => 'manage_categories', 371 'assign_terms' => 'edit_posts', 372 ); 373 374 $args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] ); 375 unset( $args['capabilities'] ); 376 377 $args['object_type'] = array_unique( (array) $object_type ); 378 379 // If not set, use the default meta box. 380 if ( null === $args['meta_box_cb'] ) { 381 if ( $args['hierarchical'] ) { 382 $args['meta_box_cb'] = 'post_categories_meta_box'; 383 } else { 384 $args['meta_box_cb'] = 'post_tags_meta_box'; 385 } 386 } 387 388 $args['name'] = $this->name; 389 390 // Default meta box sanitization callback depends on the value of 'meta_box_cb'. 391 if ( null === $args['meta_box_sanitize_cb'] ) { 392 switch ( $args['meta_box_cb'] ) { 393 case 'post_categories_meta_box': 394 $args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_checkboxes'; 395 break; 396 397 case 'post_tags_meta_box': 398 default: 399 $args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_input'; 400 break; 401 } 402 } 403 404 // Default taxonomy term. 405 if ( ! empty( $args['default_term'] ) ) { 406 if ( ! is_array( $args['default_term'] ) ) { 407 $args['default_term'] = array( 'name' => $args['default_term'] ); 408 } 409 $args['default_term'] = wp_parse_args( 410 $args['default_term'], 411 array( 412 'name' => '', 413 'slug' => '', 414 'description' => '', 415 ) 416 ); 417 } 418 419 foreach ( $args as $property_name => $property_value ) { 420 $this->$property_name = $property_value; 421 } 422 423 $this->labels = get_taxonomy_labels( $this ); 424 $this->label = $this->labels->name; 425 } 426 427 /** 428 * Adds the necessary rewrite rules for the taxonomy. 429 * 430 * @since 4.7.0 431 * 432 * @global WP $wp Current WordPress environment instance. 433 */ 434 public function add_rewrite_rules() { 435 /* @var WP $wp */ 436 global $wp; 437 438 // Non-publicly queryable taxonomies should not register query vars, except in the admin. 439 if ( false !== $this->query_var && $wp ) { 440 $wp->add_query_var( $this->query_var ); 441 } 442 443 if ( false !== $this->rewrite && ( is_admin() || get_option( 'permalink_structure' ) ) ) { 444 if ( $this->hierarchical && $this->rewrite['hierarchical'] ) { 445 $tag = '(.+?)'; 446 } else { 447 $tag = '([^/]+)'; 448 } 449 450 add_rewrite_tag( "%$this->name%", $tag, $this->query_var ? "{$this->query_var}=" : "taxonomy=$this->name&term=" ); 451 add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $this->rewrite ); 452 } 453 } 454 455 /** 456 * Removes any rewrite rules, permastructs, and rules for the taxonomy. 457 * 458 * @since 4.7.0 459 * 460 * @global WP $wp Current WordPress environment instance. 461 */ 462 public function remove_rewrite_rules() { 463 /* @var WP $wp */ 464 global $wp; 465 466 // Remove query var. 467 if ( false !== $this->query_var ) { 468 $wp->remove_query_var( $this->query_var ); 469 } 470 471 // Remove rewrite tags and permastructs. 472 if ( false !== $this->rewrite ) { 473 remove_rewrite_tag( "%$this->name%" ); 474 remove_permastruct( $this->name ); 475 } 476 } 477 478 /** 479 * Registers the ajax callback for the meta box. 480 * 481 * @since 4.7.0 482 */ 483 public function add_hooks() { 484 add_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' ); 485 } 486 487 /** 488 * Removes the ajax callback for the meta box. 489 * 490 * @since 4.7.0 491 */ 492 public function remove_hooks() { 493 remove_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' ); 494 } 495 496 /** 497 * Gets the REST API controller for this taxonomy. 498 * 499 * Will only instantiate the controller class once per request. 500 * 501 * @since 5.5.0 502 * 503 * @return WP_REST_Controller|null The controller instance, or null if the taxonomy 504 * is set not to show in rest. 505 */ 506 public function get_rest_controller() { 507 if ( ! $this->show_in_rest ) { 508 return null; 509 } 510 511 $class = $this->rest_controller_class ? $this->rest_controller_class : WP_REST_Terms_Controller::class; 512 513 if ( ! class_exists( $class ) ) { 514 return null; 515 } 516 517 if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) { 518 return null; 519 } 520 521 if ( ! $this->rest_controller ) { 522 $this->rest_controller = new $class( $this->name ); 523 } 524 525 if ( ! ( $this->rest_controller instanceof $class ) ) { 526 return null; 527 } 528 529 return $this->rest_controller; 530 } 531 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 28 01:00:03 2021 | Cross-referenced by PHPXref 0.7.1 |