No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

419 líneas
9.5 KiB

  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 it is a built-in taxonomy.
  171. *
  172. * @since 4.7.0
  173. * @access public
  174. * @var bool
  175. */
  176. public $_builtin;
  177. /**
  178. * Constructor.
  179. *
  180. * @since 4.7.0
  181. * @access public
  182. *
  183. * @global WP $wp WP instance.
  184. *
  185. * @param string $taxonomy Taxonomy key, must not exceed 32 characters.
  186. * @param array|string $object_type Name of the object type for the taxonomy object.
  187. * @param array|string $args Optional. Array or query string of arguments for registering a taxonomy.
  188. * Default empty array.
  189. */
  190. public function __construct( $taxonomy, $object_type, $args = array() ) {
  191. $this->name = $taxonomy;
  192. $this->set_props( $object_type, $args );
  193. }
  194. /**
  195. * Sets taxonomy properties.
  196. *
  197. * @since 4.7.0
  198. * @access public
  199. *
  200. * @param array|string $object_type Name of the object type for the taxonomy object.
  201. * @param array|string $args Array or query string of arguments for registering a taxonomy.
  202. */
  203. public function set_props( $object_type, $args ) {
  204. $args = wp_parse_args( $args );
  205. /**
  206. * Filters the arguments for registering a taxonomy.
  207. *
  208. * @since 4.4.0
  209. *
  210. * @param array $args Array of arguments for registering a taxonomy.
  211. * @param string $taxonomy Taxonomy key.
  212. * @param array $object_type Array of names of object types for the taxonomy.
  213. */
  214. $args = apply_filters( 'register_taxonomy_args', $args, $this->name, (array) $object_type );
  215. $defaults = array(
  216. 'labels' => array(),
  217. 'description' => '',
  218. 'public' => true,
  219. 'publicly_queryable' => null,
  220. 'hierarchical' => false,
  221. 'show_ui' => null,
  222. 'show_in_menu' => null,
  223. 'show_in_nav_menus' => null,
  224. 'show_tagcloud' => null,
  225. 'show_in_quick_edit' => null,
  226. 'show_admin_column' => false,
  227. 'meta_box_cb' => null,
  228. 'capabilities' => array(),
  229. 'rewrite' => true,
  230. 'query_var' => $this->name,
  231. 'update_count_callback' => '',
  232. '_builtin' => false,
  233. );
  234. $args = array_merge( $defaults, $args );
  235. // If not set, default to the setting for public.
  236. if ( null === $args['publicly_queryable'] ) {
  237. $args['publicly_queryable'] = $args['public'];
  238. }
  239. if ( false !== $args['query_var'] && ( is_admin() || false !== $args['publicly_queryable'] ) ) {
  240. if ( true === $args['query_var'] ) {
  241. $args['query_var'] = $this->name;
  242. } else {
  243. $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
  244. }
  245. } else {
  246. // Force query_var to false for non-public taxonomies.
  247. $args['query_var'] = false;
  248. }
  249. if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
  250. $args['rewrite'] = wp_parse_args( $args['rewrite'], array(
  251. 'with_front' => true,
  252. 'hierarchical' => false,
  253. 'ep_mask' => EP_NONE,
  254. ) );
  255. if ( empty( $args['rewrite']['slug'] ) ) {
  256. $args['rewrite']['slug'] = sanitize_title_with_dashes( $this->name );
  257. }
  258. }
  259. // If not set, default to the setting for public.
  260. if ( null === $args['show_ui'] ) {
  261. $args['show_ui'] = $args['public'];
  262. }
  263. // If not set, default to the setting for show_ui.
  264. if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
  265. $args['show_in_menu'] = $args['show_ui'];
  266. }
  267. // If not set, default to the setting for public.
  268. if ( null === $args['show_in_nav_menus'] ) {
  269. $args['show_in_nav_menus'] = $args['public'];
  270. }
  271. // If not set, default to the setting for show_ui.
  272. if ( null === $args['show_tagcloud'] ) {
  273. $args['show_tagcloud'] = $args['show_ui'];
  274. }
  275. // If not set, default to the setting for show_ui.
  276. if ( null === $args['show_in_quick_edit'] ) {
  277. $args['show_in_quick_edit'] = $args['show_ui'];
  278. }
  279. $default_caps = array(
  280. 'manage_terms' => 'manage_categories',
  281. 'edit_terms' => 'manage_categories',
  282. 'delete_terms' => 'manage_categories',
  283. 'assign_terms' => 'edit_posts',
  284. );
  285. $args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] );
  286. unset( $args['capabilities'] );
  287. $args['object_type'] = array_unique( (array) $object_type );
  288. // If not set, use the default meta box
  289. if ( null === $args['meta_box_cb'] ) {
  290. if ( $args['hierarchical'] ) {
  291. $args['meta_box_cb'] = 'post_categories_meta_box';
  292. } else {
  293. $args['meta_box_cb'] = 'post_tags_meta_box';
  294. }
  295. }
  296. $args['name'] = $this->name;
  297. foreach ( $args as $property_name => $property_value ) {
  298. $this->$property_name = $property_value;
  299. }
  300. $this->labels = get_taxonomy_labels( $this );
  301. $this->label = $this->labels->name;
  302. }
  303. /**
  304. * Adds the necessary rewrite rules for the taxonomy.
  305. *
  306. * @since 4.7.0
  307. * @access public
  308. *
  309. * @global WP $wp Current WordPress environment instance.
  310. */
  311. public function add_rewrite_rules() {
  312. /* @var WP $wp */
  313. global $wp;
  314. // Non-publicly queryable taxonomies should not register query vars, except in the admin.
  315. if ( false !== $this->query_var && $wp ) {
  316. $wp->add_query_var( $this->query_var );
  317. }
  318. if ( false !== $this->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
  319. if ( $this->hierarchical && $this->rewrite['hierarchical'] ) {
  320. $tag = '(.+?)';
  321. } else {
  322. $tag = '([^/]+)';
  323. }
  324. add_rewrite_tag( "%$this->name%", $tag, $this->query_var ? "{$this->query_var}=" : "taxonomy=$this->name&term=" );
  325. add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $this->rewrite );
  326. }
  327. }
  328. /**
  329. * Removes any rewrite rules, permastructs, and rules for the taxonomy.
  330. *
  331. * @since 4.7.0
  332. * @access public
  333. *
  334. * @global WP $wp Current WordPress environment instance.
  335. */
  336. public function remove_rewrite_rules() {
  337. /* @var WP $wp */
  338. global $wp;
  339. // Remove query var.
  340. if ( false !== $this->query_var ) {
  341. $wp->remove_query_var( $this->query_var );
  342. }
  343. // Remove rewrite tags and permastructs.
  344. if ( false !== $this->rewrite ) {
  345. remove_rewrite_tag( "%$this->name%" );
  346. remove_permastruct( $this->name );
  347. }
  348. }
  349. /**
  350. * Registers the ajax callback for the meta box.
  351. *
  352. * @since 4.7.0
  353. * @access public
  354. */
  355. public function add_hooks() {
  356. add_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' );
  357. }
  358. /**
  359. * Removes the ajax callback for the meta box.
  360. *
  361. * @since 4.7.0
  362. * @access public
  363. */
  364. public function remove_hooks() {
  365. remove_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' );
  366. }
  367. }