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


Generated: Mon Jan 20 01:00:03 2020 Cross-referenced by PHPXref 0.7.1