[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-wp-taxonomy.php (source)

   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 object
  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 object
 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       * @since 4.7.0
 243       *
 244       * @global WP $wp Current WordPress environment instance.
 245       *
 246       * @param string       $taxonomy    Taxonomy key, must not exceed 32 characters.
 247       * @param array|string $object_type Name of the object type for the taxonomy object.
 248       * @param array|string $args        Optional. Array or query string of arguments for registering a taxonomy.
 249       *                                  Default empty array.
 250       */
 251  	public function __construct( $taxonomy, $object_type, $args = array() ) {
 252          $this->name = $taxonomy;
 253  
 254          $this->set_props( $object_type, $args );
 255      }
 256  
 257      /**
 258       * Sets taxonomy properties.
 259       *
 260       * @since 4.7.0
 261       *
 262       * @param array|string $object_type Name of the object type for the taxonomy object.
 263       * @param array|string $args        Array or query string of arguments for registering a taxonomy.
 264       */
 265  	public function set_props( $object_type, $args ) {
 266          $args = wp_parse_args( $args );
 267  
 268          /**
 269           * Filters the arguments for registering a taxonomy.
 270           *
 271           * @since 4.4.0
 272           *
 273           * @param array    $args        Array of arguments for registering a taxonomy.
 274           * @param string   $taxonomy    Taxonomy key.
 275           * @param string[] $object_type Array of names of object types for the taxonomy.
 276           */
 277          $args = apply_filters( 'register_taxonomy_args', $args, $this->name, (array) $object_type );
 278  
 279          $defaults = array(
 280              'labels'                => array(),
 281              'description'           => '',
 282              'public'                => true,
 283              'publicly_queryable'    => null,
 284              'hierarchical'          => false,
 285              'show_ui'               => null,
 286              'show_in_menu'          => null,
 287              'show_in_nav_menus'     => null,
 288              'show_tagcloud'         => null,
 289              'show_in_quick_edit'    => null,
 290              'show_admin_column'     => false,
 291              'meta_box_cb'           => null,
 292              'meta_box_sanitize_cb'  => null,
 293              'capabilities'          => array(),
 294              'rewrite'               => true,
 295              'query_var'             => $this->name,
 296              'update_count_callback' => '',
 297              'show_in_rest'          => false,
 298              'rest_base'             => false,
 299              'rest_controller_class' => false,
 300              'default_term'          => null,
 301              '_builtin'              => false,
 302          );
 303  
 304          $args = array_merge( $defaults, $args );
 305  
 306          // If not set, default to the setting for 'public'.
 307          if ( null === $args['publicly_queryable'] ) {
 308              $args['publicly_queryable'] = $args['public'];
 309          }
 310  
 311          if ( false !== $args['query_var'] && ( is_admin() || false !== $args['publicly_queryable'] ) ) {
 312              if ( true === $args['query_var'] ) {
 313                  $args['query_var'] = $this->name;
 314              } else {
 315                  $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
 316              }
 317          } else {
 318              // Force 'query_var' to false for non-public taxonomies.
 319              $args['query_var'] = false;
 320          }
 321  
 322          if ( false !== $args['rewrite'] && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
 323              $args['rewrite'] = wp_parse_args(
 324                  $args['rewrite'],
 325                  array(
 326                      'with_front'   => true,
 327                      'hierarchical' => false,
 328                      'ep_mask'      => EP_NONE,
 329                  )
 330              );
 331  
 332              if ( empty( $args['rewrite']['slug'] ) ) {
 333                  $args['rewrite']['slug'] = sanitize_title_with_dashes( $this->name );
 334              }
 335          }
 336  
 337          // If not set, default to the setting for 'public'.
 338          if ( null === $args['show_ui'] ) {
 339              $args['show_ui'] = $args['public'];
 340          }
 341  
 342          // If not set, default to the setting for 'show_ui'.
 343          if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
 344              $args['show_in_menu'] = $args['show_ui'];
 345          }
 346  
 347          // If not set, default to the setting for 'public'.
 348          if ( null === $args['show_in_nav_menus'] ) {
 349              $args['show_in_nav_menus'] = $args['public'];
 350          }
 351  
 352          // If not set, default to the setting for 'show_ui'.
 353          if ( null === $args['show_tagcloud'] ) {
 354              $args['show_tagcloud'] = $args['show_ui'];
 355          }
 356  
 357          // If not set, default to the setting for 'show_ui'.
 358          if ( null === $args['show_in_quick_edit'] ) {
 359              $args['show_in_quick_edit'] = $args['show_ui'];
 360          }
 361  
 362          $default_caps = array(
 363              'manage_terms' => 'manage_categories',
 364              'edit_terms'   => 'manage_categories',
 365              'delete_terms' => 'manage_categories',
 366              'assign_terms' => 'edit_posts',
 367          );
 368  
 369          $args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] );
 370          unset( $args['capabilities'] );
 371  
 372          $args['object_type'] = array_unique( (array) $object_type );
 373  
 374          // If not set, use the default meta box.
 375          if ( null === $args['meta_box_cb'] ) {
 376              if ( $args['hierarchical'] ) {
 377                  $args['meta_box_cb'] = 'post_categories_meta_box';
 378              } else {
 379                  $args['meta_box_cb'] = 'post_tags_meta_box';
 380              }
 381          }
 382  
 383          $args['name'] = $this->name;
 384  
 385          // Default meta box sanitization callback depends on the value of 'meta_box_cb'.
 386          if ( null === $args['meta_box_sanitize_cb'] ) {
 387              switch ( $args['meta_box_cb'] ) {
 388                  case 'post_categories_meta_box':
 389                      $args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_checkboxes';
 390                      break;
 391  
 392                  case 'post_tags_meta_box':
 393                  default:
 394                      $args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_input';
 395                      break;
 396              }
 397          }
 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  
 414          foreach ( $args as $property_name => $property_value ) {
 415              $this->$property_name = $property_value;
 416          }
 417  
 418          $this->labels = get_taxonomy_labels( $this );
 419          $this->label  = $this->labels->name;
 420      }
 421  
 422      /**
 423       * Adds the necessary rewrite rules for the taxonomy.
 424       *
 425       * @since 4.7.0
 426       *
 427       * @global WP $wp Current WordPress environment instance.
 428       */
 429  	public function add_rewrite_rules() {
 430          /* @var WP $wp */
 431          global $wp;
 432  
 433          // Non-publicly queryable taxonomies should not register query vars, except in the admin.
 434          if ( false !== $this->query_var && $wp ) {
 435              $wp->add_query_var( $this->query_var );
 436          }
 437  
 438          if ( false !== $this->rewrite && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
 439              if ( $this->hierarchical && $this->rewrite['hierarchical'] ) {
 440                  $tag = '(.+?)';
 441              } else {
 442                  $tag = '([^/]+)';
 443              }
 444  
 445              add_rewrite_tag( "%$this->name%", $tag, $this->query_var ? "{$this->query_var}=" : "taxonomy=$this->name&term=" );
 446              add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $this->rewrite );
 447          }
 448      }
 449  
 450      /**
 451       * Removes any rewrite rules, permastructs, and rules for the taxonomy.
 452       *
 453       * @since 4.7.0
 454       *
 455       * @global WP $wp Current WordPress environment instance.
 456       */
 457  	public function remove_rewrite_rules() {
 458          /* @var WP $wp */
 459          global $wp;
 460  
 461          // Remove query var.
 462          if ( false !== $this->query_var ) {
 463              $wp->remove_query_var( $this->query_var );
 464          }
 465  
 466          // Remove rewrite tags and permastructs.
 467          if ( false !== $this->rewrite ) {
 468              remove_rewrite_tag( "%$this->name%" );
 469              remove_permastruct( $this->name );
 470          }
 471      }
 472  
 473      /**
 474       * Registers the ajax callback for the meta box.
 475       *
 476       * @since 4.7.0
 477       */
 478  	public function add_hooks() {
 479          add_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' );
 480      }
 481  
 482      /**
 483       * Removes the ajax callback for the meta box.
 484       *
 485       * @since 4.7.0
 486       */
 487  	public function remove_hooks() {
 488          remove_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' );
 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      }
 526  }


Generated: Sun Aug 9 01:00:03 2020 Cross-referenced by PHPXref 0.7.1