PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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