PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-wp-taxonomy.php

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