[ 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       * An array of labels for this taxonomy.
  34       *
  35       * @since 4.7.0
  36       * @var object
  37       */
  38      public $labels = array();
  39  
  40      /**
  41       * A short descriptive summary of what the taxonomy is for.
  42       *
  43       * @since 4.7.0
  44       * @var string
  45       */
  46      public $description = '';
  47  
  48      /**
  49       * Whether a taxonomy is intended for use publicly either via the admin interface or by front-end users.
  50       *
  51       * @since 4.7.0
  52       * @var bool
  53       */
  54      public $public = true;
  55  
  56      /**
  57       * Whether the taxonomy is publicly queryable.
  58       *
  59       * @since 4.7.0
  60       * @var bool
  61       */
  62      public $publicly_queryable = true;
  63  
  64      /**
  65       * Whether the taxonomy is hierarchical.
  66       *
  67       * @since 4.7.0
  68       * @var bool
  69       */
  70      public $hierarchical = false;
  71  
  72      /**
  73       * Whether to generate and allow a UI for managing terms in this taxonomy in the admin.
  74       *
  75       * @since 4.7.0
  76       * @var bool
  77       */
  78      public $show_ui = true;
  79  
  80      /**
  81       * Whether to show the taxonomy in the admin menu.
  82       *
  83       * If true, the taxonomy is shown as a submenu of the object type menu. If false, no menu is shown.
  84       *
  85       * @since 4.7.0
  86       * @var bool
  87       */
  88      public $show_in_menu = true;
  89  
  90      /**
  91       * Whether the taxonomy is available for selection in navigation menus.
  92       *
  93       * @since 4.7.0
  94       * @var bool
  95       */
  96      public $show_in_nav_menus = true;
  97  
  98      /**
  99       * Whether to list the taxonomy in the tag cloud widget controls.
 100       *
 101       * @since 4.7.0
 102       * @var bool
 103       */
 104      public $show_tagcloud = true;
 105  
 106      /**
 107       * Whether to show the taxonomy in the quick/bulk edit panel.
 108       *
 109       * @since 4.7.0
 110       * @var bool
 111       */
 112      public $show_in_quick_edit = true;
 113  
 114      /**
 115       * Whether to display a column for the taxonomy on its post type listing screens.
 116       *
 117       * @since 4.7.0
 118       * @var bool
 119       */
 120      public $show_admin_column = false;
 121  
 122      /**
 123       * The callback function for the meta box display.
 124       *
 125       * @since 4.7.0
 126       * @var bool|callable
 127       */
 128      public $meta_box_cb = null;
 129  
 130      /**
 131       * The callback function for sanitizing taxonomy data saved from a meta box.
 132       *
 133       * @since 5.1.0
 134       * @var callable
 135       */
 136      public $meta_box_sanitize_cb = null;
 137  
 138      /**
 139       * An array of object types this taxonomy is registered for.
 140       *
 141       * @since 4.7.0
 142       * @var array
 143       */
 144      public $object_type = null;
 145  
 146      /**
 147       * Capabilities for this taxonomy.
 148       *
 149       * @since 4.7.0
 150       * @var object
 151       */
 152      public $cap;
 153  
 154      /**
 155       * Rewrites information for this taxonomy.
 156       *
 157       * @since 4.7.0
 158       * @var array|false
 159       */
 160      public $rewrite;
 161  
 162      /**
 163       * Query var string for this taxonomy.
 164       *
 165       * @since 4.7.0
 166       * @var string|false
 167       */
 168      public $query_var;
 169  
 170      /**
 171       * Function that will be called when the count is updated.
 172       *
 173       * @since 4.7.0
 174       * @var callable
 175       */
 176      public $update_count_callback;
 177  
 178      /**
 179       * Whether this taxonomy should appear in the REST API.
 180       *
 181       * Default false. If true, standard endpoints will be registered with
 182       * respect to $rest_base and $rest_controller_class.
 183       *
 184       * @since 4.7.4
 185       * @var bool $show_in_rest
 186       */
 187      public $show_in_rest;
 188  
 189      /**
 190       * The base path for this taxonomy's REST API endpoints.
 191       *
 192       * @since 4.7.4
 193       * @var string|bool $rest_base
 194       */
 195      public $rest_base;
 196  
 197      /**
 198       * The controller for this taxonomy's REST API endpoints.
 199       *
 200       * Custom controllers must extend WP_REST_Controller.
 201       *
 202       * @since 4.7.4
 203       * @var string|bool $rest_controller_class
 204       */
 205      public $rest_controller_class;
 206  
 207      /**
 208       * Whether it is a built-in taxonomy.
 209       *
 210       * @since 4.7.0
 211       * @var bool
 212       */
 213      public $_builtin;
 214  
 215      /**
 216       * Constructor.
 217       *
 218       * @since 4.7.0
 219       *
 220       * @global WP $wp WP instance.
 221       *
 222       * @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.
 224       * @param array|string $args        Optional. Array or query string of arguments for registering a taxonomy.
 225       *                                  Default empty array.
 226       */
 227  	public function __construct( $taxonomy, $object_type, $args = array() ) {
 228          $this->name = $taxonomy;
 229  
 230          $this->set_props( $object_type, $args );
 231      }
 232  
 233      /**
 234       * Sets taxonomy properties.
 235       *
 236       * @since 4.7.0
 237       *
 238       * @param array|string $object_type Name of the object type for the taxonomy object.
 239       * @param array|string $args        Array or query string of arguments for registering a taxonomy.
 240       */
 241  	public function set_props( $object_type, $args ) {
 242          $args = wp_parse_args( $args );
 243  
 244          /**
 245           * Filters the arguments for registering a taxonomy.
 246           *
 247           * @since 4.4.0
 248           *
 249           * @param array    $args        Array of arguments for registering a taxonomy.
 250           * @param string   $taxonomy    Taxonomy key.
 251           * @param string[] $object_type Array of names of object types for the taxonomy.
 252           */
 253          $args = apply_filters( 'register_taxonomy_args', $args, $this->name, (array) $object_type );
 254  
 255          $defaults = array(
 256              'labels'                => array(),
 257              'description'           => '',
 258              'public'                => true,
 259              'publicly_queryable'    => null,
 260              'hierarchical'          => false,
 261              'show_ui'               => null,
 262              'show_in_menu'          => null,
 263              'show_in_nav_menus'     => null,
 264              'show_tagcloud'         => null,
 265              'show_in_quick_edit'    => null,
 266              'show_admin_column'     => false,
 267              'meta_box_cb'           => null,
 268              'meta_box_sanitize_cb'  => null,
 269              'capabilities'          => array(),
 270              'rewrite'               => true,
 271              'query_var'             => $this->name,
 272              'update_count_callback' => '',
 273              'show_in_rest'          => false,
 274              'rest_base'             => false,
 275              'rest_controller_class' => false,
 276              '_builtin'              => false,
 277          );
 278  
 279          $args = array_merge( $defaults, $args );
 280  
 281          // If not set, default to the setting for public.
 282          if ( null === $args['publicly_queryable'] ) {
 283              $args['publicly_queryable'] = $args['public'];
 284          }
 285  
 286          if ( false !== $args['query_var'] && ( is_admin() || false !== $args['publicly_queryable'] ) ) {
 287              if ( true === $args['query_var'] ) {
 288                  $args['query_var'] = $this->name;
 289              } else {
 290                  $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
 291              }
 292          } else {
 293              // Force query_var to false for non-public taxonomies.
 294              $args['query_var'] = false;
 295          }
 296  
 297          if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
 298              $args['rewrite'] = wp_parse_args(
 299                  $args['rewrite'],
 300                  array(
 301                      'with_front'   => true,
 302                      'hierarchical' => false,
 303                      'ep_mask'      => EP_NONE,
 304                  )
 305              );
 306  
 307              if ( empty( $args['rewrite']['slug'] ) ) {
 308                  $args['rewrite']['slug'] = sanitize_title_with_dashes( $this->name );
 309              }
 310          }
 311  
 312          // If not set, default to the setting for public.
 313          if ( null === $args['show_ui'] ) {
 314              $args['show_ui'] = $args['public'];
 315          }
 316  
 317          // If not set, default to the setting for show_ui.
 318          if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
 319              $args['show_in_menu'] = $args['show_ui'];
 320          }
 321  
 322          // If not set, default to the setting for public.
 323          if ( null === $args['show_in_nav_menus'] ) {
 324              $args['show_in_nav_menus'] = $args['public'];
 325          }
 326  
 327          // If not set, default to the setting for show_ui.
 328          if ( null === $args['show_tagcloud'] ) {
 329              $args['show_tagcloud'] = $args['show_ui'];
 330          }
 331  
 332          // If not set, default to the setting for show_ui.
 333          if ( null === $args['show_in_quick_edit'] ) {
 334              $args['show_in_quick_edit'] = $args['show_ui'];
 335          }
 336  
 337          $default_caps = array(
 338              'manage_terms' => 'manage_categories',
 339              'edit_terms'   => 'manage_categories',
 340              'delete_terms' => 'manage_categories',
 341              'assign_terms' => 'edit_posts',
 342          );
 343  
 344          $args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] );
 345          unset( $args['capabilities'] );
 346  
 347          $args['object_type'] = array_unique( (array) $object_type );
 348  
 349          // If not set, use the default meta box
 350          if ( null === $args['meta_box_cb'] ) {
 351              if ( $args['hierarchical'] ) {
 352                  $args['meta_box_cb'] = 'post_categories_meta_box';
 353              } else {
 354                  $args['meta_box_cb'] = 'post_tags_meta_box';
 355              }
 356          }
 357  
 358          $args['name'] = $this->name;
 359  
 360          // Default meta box sanitization callback depends on the value of 'meta_box_cb'.
 361          if ( null === $args['meta_box_sanitize_cb'] ) {
 362              switch ( $args['meta_box_cb'] ) {
 363                  case 'post_categories_meta_box':
 364                      $args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_checkboxes';
 365                      break;
 366  
 367                  case 'post_tags_meta_box':
 368                  default:
 369                      $args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_input';
 370                      break;
 371              }
 372          }
 373  
 374          foreach ( $args as $property_name => $property_value ) {
 375              $this->$property_name = $property_value;
 376          }
 377  
 378          $this->labels = get_taxonomy_labels( $this );
 379          $this->label  = $this->labels->name;
 380      }
 381  
 382      /**
 383       * Adds the necessary rewrite rules for the taxonomy.
 384       *
 385       * @since 4.7.0
 386       *
 387       * @global WP $wp Current WordPress environment instance.
 388       */
 389  	public function add_rewrite_rules() {
 390          /* @var WP $wp */
 391          global $wp;
 392  
 393          // Non-publicly queryable taxonomies should not register query vars, except in the admin.
 394          if ( false !== $this->query_var && $wp ) {
 395              $wp->add_query_var( $this->query_var );
 396          }
 397  
 398          if ( false !== $this->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
 399              if ( $this->hierarchical && $this->rewrite['hierarchical'] ) {
 400                  $tag = '(.+?)';
 401              } else {
 402                  $tag = '([^/]+)';
 403              }
 404  
 405              add_rewrite_tag( "%$this->name%", $tag, $this->query_var ? "{$this->query_var}=" : "taxonomy=$this->name&term=" );
 406              add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $this->rewrite );
 407          }
 408      }
 409  
 410      /**
 411       * Removes any rewrite rules, permastructs, and rules for the taxonomy.
 412       *
 413       * @since 4.7.0
 414       *
 415       * @global WP $wp Current WordPress environment instance.
 416       */
 417  	public function remove_rewrite_rules() {
 418          /* @var WP $wp */
 419          global $wp;
 420  
 421          // Remove query var.
 422          if ( false !== $this->query_var ) {
 423              $wp->remove_query_var( $this->query_var );
 424          }
 425  
 426          // Remove rewrite tags and permastructs.
 427          if ( false !== $this->rewrite ) {
 428              remove_rewrite_tag( "%$this->name%" );
 429              remove_permastruct( $this->name );
 430          }
 431      }
 432  
 433      /**
 434       * Registers the ajax callback for the meta box.
 435       *
 436       * @since 4.7.0
 437       */
 438  	public function add_hooks() {
 439          add_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' );
 440      }
 441  
 442      /**
 443       * Removes the ajax callback for the meta box.
 444       *
 445       * @since 4.7.0
 446       */
 447  	public function remove_hooks() {
 448          remove_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' );
 449      }
 450  }


Generated: Sat Jul 20 01:00:03 2019 Cross-referenced by PHPXref 0.7.1