class-wp-post-type.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. <?php
  2. /**
  3. * Post API: WP_Post_Type class
  4. *
  5. * @package WordPress
  6. * @subpackage Post
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Core class used for interacting with post types.
  11. *
  12. * @since 4.6.0
  13. *
  14. * @see register_post_type()
  15. */
  16. final class WP_Post_Type {
  17. /**
  18. * Post type key.
  19. *
  20. * @since 4.6.0
  21. * @var string $name
  22. */
  23. public $name;
  24. /**
  25. * Name of the post type shown in the menu. Usually plural.
  26. *
  27. * @since 4.6.0
  28. * @var string $label
  29. */
  30. public $label;
  31. /**
  32. * Labels object for this post type.
  33. *
  34. * If not set, post labels are inherited for non-hierarchical types
  35. * and page labels for hierarchical ones.
  36. *
  37. * @see get_post_type_labels()
  38. *
  39. * @since 4.6.0
  40. * @var object $labels
  41. */
  42. public $labels;
  43. /**
  44. * A short descriptive summary of what the post type is.
  45. *
  46. * Default empty.
  47. *
  48. * @since 4.6.0
  49. * @var string $description
  50. */
  51. public $description = '';
  52. /**
  53. * Whether a post type is intended for use publicly either via the admin interface or by front-end users.
  54. *
  55. * While the default settings of $exclude_from_search, $publicly_queryable, $show_ui, and $show_in_nav_menus
  56. * are inherited from public, each does not rely on this relationship and controls a very specific intention.
  57. *
  58. * Default false.
  59. *
  60. * @since 4.6.0
  61. * @var bool $public
  62. */
  63. public $public = false;
  64. /**
  65. * Whether the post type is hierarchical (e.g. page).
  66. *
  67. * Default false.
  68. *
  69. * @since 4.6.0
  70. * @var bool $hierarchical
  71. */
  72. public $hierarchical = false;
  73. /**
  74. * Whether to exclude posts with this post type from front end search
  75. * results.
  76. *
  77. * Default is the opposite value of $public.
  78. *
  79. * @since 4.6.0
  80. * @var bool $exclude_from_search
  81. */
  82. public $exclude_from_search = null;
  83. /**
  84. * Whether queries can be performed on the front end for the post type as part of `parse_request()`.
  85. *
  86. * Endpoints would include:
  87. * - `?post_type={post_type_key}`
  88. * - `?{post_type_key}={single_post_slug}`
  89. * - `?{post_type_query_var}={single_post_slug}`
  90. *
  91. * Default is the value of $public.
  92. *
  93. * @since 4.6.0
  94. * @var bool $publicly_queryable
  95. */
  96. public $publicly_queryable = null;
  97. /**
  98. * Whether to generate and allow a UI for managing this post type in the admin.
  99. *
  100. * Default is the value of $public.
  101. *
  102. * @since 4.6.0
  103. * @var bool $show_ui
  104. */
  105. public $show_ui = null;
  106. /**
  107. * Where to show the post type in the admin menu.
  108. *
  109. * To work, $show_ui must be true. If true, the post type is shown in its own top level menu. If false, no menu is
  110. * shown. If a string of an existing top level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post type
  111. * will be placed as a sub-menu of that.
  112. *
  113. * Default is the value of $show_ui.
  114. *
  115. * @since 4.6.0
  116. * @var bool $show_in_menu
  117. */
  118. public $show_in_menu = null;
  119. /**
  120. * Makes this post type available for selection in navigation menus.
  121. *
  122. * Default is the value $public.
  123. *
  124. * @since 4.6.0
  125. * @var bool $show_in_nav_menus
  126. */
  127. public $show_in_nav_menus = null;
  128. /**
  129. * Makes this post type available via the admin bar.
  130. *
  131. * Default is the value of $show_in_menu.
  132. *
  133. * @since 4.6.0
  134. * @var bool $show_in_admin_bar
  135. */
  136. public $show_in_admin_bar = null;
  137. /**
  138. * The position in the menu order the post type should appear.
  139. *
  140. * To work, $show_in_menu must be true. Default null (at the bottom).
  141. *
  142. * @since 4.6.0
  143. * @var int $menu_position
  144. */
  145. public $menu_position = null;
  146. /**
  147. * The URL to the icon to be used for this menu.
  148. *
  149. * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme.
  150. * This should begin with 'data:image/svg+xml;base64,'. Pass the name of a Dashicons helper class
  151. * to use a font icon, e.g. 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty
  152. * so an icon can be added via CSS.
  153. *
  154. * Defaults to use the posts icon.
  155. *
  156. * @since 4.6.0
  157. * @var string $menu_icon
  158. */
  159. public $menu_icon = null;
  160. /**
  161. * The string to use to build the read, edit, and delete capabilities.
  162. *
  163. * May be passed as an array to allow for alternative plurals when using
  164. * this argument as a base to construct the capabilities, e.g.
  165. * array( 'story', 'stories' ). Default 'post'.
  166. *
  167. * @since 4.6.0
  168. * @var string $capability_type
  169. */
  170. public $capability_type = 'post';
  171. /**
  172. * Whether to use the internal default meta capability handling.
  173. *
  174. * Default false.
  175. *
  176. * @since 4.6.0
  177. * @var bool $map_meta_cap
  178. */
  179. public $map_meta_cap = false;
  180. /**
  181. * Provide a callback function that sets up the meta boxes for the edit form.
  182. *
  183. * Do `remove_meta_box()` and `add_meta_box()` calls in the callback. Default null.
  184. *
  185. * @since 4.6.0
  186. * @var string $register_meta_box_cb
  187. */
  188. public $register_meta_box_cb = null;
  189. /**
  190. * An array of taxonomy identifiers that will be registered for the post type.
  191. *
  192. * Taxonomies can be registered later with `register_taxonomy()` or `register_taxonomy_for_object_type()`.
  193. *
  194. * Default empty array.
  195. *
  196. * @since 4.6.0
  197. * @var array $taxonomies
  198. */
  199. public $taxonomies = array();
  200. /**
  201. * Whether there should be post type archives, or if a string, the archive slug to use.
  202. *
  203. * Will generate the proper rewrite rules if $rewrite is enabled. Default false.
  204. *
  205. * @since 4.6.0
  206. * @var bool|string $has_archive
  207. */
  208. public $has_archive = false;
  209. /**
  210. * Sets the query_var key for this post type.
  211. *
  212. * Defaults to $post_type key. If false, a post type cannot be loaded at `?{query_var}={post_slug}`.
  213. * If specified as a string, the query `?{query_var_string}={post_slug}` will be valid.
  214. *
  215. * @since 4.6.0
  216. * @var string|bool $query_var
  217. */
  218. public $query_var;
  219. /**
  220. * Whether to allow this post type to be exported.
  221. *
  222. * Default true.
  223. *
  224. * @since 4.6.0
  225. * @var bool $can_export
  226. */
  227. public $can_export = true;
  228. /**
  229. * Whether to delete posts of this type when deleting a user.
  230. *
  231. * If true, posts of this type belonging to the user will be moved to trash when then user is deleted.
  232. * If false, posts of this type belonging to the user will *not* be trashed or deleted.
  233. * If not set (the default), posts are trashed if post_type_supports( 'author' ).
  234. * Otherwise posts are not trashed or deleted. Default null.
  235. *
  236. * @since 4.6.0
  237. * @var bool $delete_with_user
  238. */
  239. public $delete_with_user = null;
  240. /**
  241. * Whether this post type is a native or "built-in" post_type.
  242. *
  243. * Default false.
  244. *
  245. * @since 4.6.0
  246. * @var bool $_builtin
  247. */
  248. public $_builtin = false;
  249. /**
  250. * URL segment to use for edit link of this post type.
  251. *
  252. * Default 'post.php?post=%d'.
  253. *
  254. * @since 4.6.0
  255. * @var string $_edit_link
  256. */
  257. public $_edit_link = 'post.php?post=%d';
  258. /**
  259. * Post type capabilities.
  260. *
  261. * @since 4.6.0
  262. * @var object $cap
  263. */
  264. public $cap;
  265. /**
  266. * Triggers the handling of rewrites for this post type.
  267. *
  268. * Defaults to true, using $post_type as slug.
  269. *
  270. * @since 4.6.0
  271. * @var array|false $rewrite
  272. */
  273. public $rewrite;
  274. /**
  275. * The features supported by the post type.
  276. *
  277. * @since 4.6.0
  278. * @var array|bool $supports
  279. */
  280. public $supports;
  281. /**
  282. * Whether this post type should appear in the REST API.
  283. *
  284. * Default false. If true, standard endpoints will be registered with
  285. * respect to $rest_base and $rest_controller_class.
  286. *
  287. * @since 4.7.4
  288. * @var bool $show_in_rest
  289. */
  290. public $show_in_rest;
  291. /**
  292. * The base path for this post type's REST API endpoints.
  293. *
  294. * @since 4.7.4
  295. * @var string|bool $rest_base
  296. */
  297. public $rest_base;
  298. /**
  299. * The controller for this post type's REST API endpoints.
  300. *
  301. * Custom controllers must extend WP_REST_Controller.
  302. *
  303. * @since 4.7.4
  304. * @var string|bool $rest_controller_class
  305. */
  306. public $rest_controller_class;
  307. /**
  308. * Constructor.
  309. *
  310. * Will populate object properties from the provided arguments and assign other
  311. * default properties based on that information.
  312. *
  313. * @since 4.6.0
  314. *
  315. * @see register_post_type()
  316. *
  317. * @param string $post_type Post type key.
  318. * @param array|string $args Optional. Array or string of arguments for registering a post type.
  319. * Default empty array.
  320. */
  321. public function __construct( $post_type, $args = array() ) {
  322. $this->name = $post_type;
  323. $this->set_props( $args );
  324. }
  325. /**
  326. * Sets post type properties.
  327. *
  328. * @since 4.6.0
  329. *
  330. * @param array|string $args Array or string of arguments for registering a post type.
  331. */
  332. public function set_props( $args ) {
  333. $args = wp_parse_args( $args );
  334. /**
  335. * Filters the arguments for registering a post type.
  336. *
  337. * @since 4.4.0
  338. *
  339. * @param array $args Array of arguments for registering a post type.
  340. * @param string $post_type Post type key.
  341. */
  342. $args = apply_filters( 'register_post_type_args', $args, $this->name );
  343. $has_edit_link = ! empty( $args['_edit_link'] );
  344. // Args prefixed with an underscore are reserved for internal use.
  345. $defaults = array(
  346. 'labels' => array(),
  347. 'description' => '',
  348. 'public' => false,
  349. 'hierarchical' => false,
  350. 'exclude_from_search' => null,
  351. 'publicly_queryable' => null,
  352. 'show_ui' => null,
  353. 'show_in_menu' => null,
  354. 'show_in_nav_menus' => null,
  355. 'show_in_admin_bar' => null,
  356. 'menu_position' => null,
  357. 'menu_icon' => null,
  358. 'capability_type' => 'post',
  359. 'capabilities' => array(),
  360. 'map_meta_cap' => null,
  361. 'supports' => array(),
  362. 'register_meta_box_cb' => null,
  363. 'taxonomies' => array(),
  364. 'has_archive' => false,
  365. 'rewrite' => true,
  366. 'query_var' => true,
  367. 'can_export' => true,
  368. 'delete_with_user' => null,
  369. 'show_in_rest' => false,
  370. 'rest_base' => false,
  371. 'rest_controller_class' => false,
  372. '_builtin' => false,
  373. '_edit_link' => 'post.php?post=%d',
  374. );
  375. $args = array_merge( $defaults, $args );
  376. $args['name'] = $this->name;
  377. // If not set, default to the setting for public.
  378. if ( null === $args['publicly_queryable'] ) {
  379. $args['publicly_queryable'] = $args['public'];
  380. }
  381. // If not set, default to the setting for public.
  382. if ( null === $args['show_ui'] ) {
  383. $args['show_ui'] = $args['public'];
  384. }
  385. // If not set, default to the setting for show_ui.
  386. if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
  387. $args['show_in_menu'] = $args['show_ui'];
  388. }
  389. // If not set, default to the whether the full UI is shown.
  390. if ( null === $args['show_in_admin_bar'] ) {
  391. $args['show_in_admin_bar'] = (bool) $args['show_in_menu'];
  392. }
  393. // If not set, default to the setting for public.
  394. if ( null === $args['show_in_nav_menus'] ) {
  395. $args['show_in_nav_menus'] = $args['public'];
  396. }
  397. // If not set, default to true if not public, false if public.
  398. if ( null === $args['exclude_from_search'] ) {
  399. $args['exclude_from_search'] = ! $args['public'];
  400. }
  401. // Back compat with quirky handling in version 3.0. #14122.
  402. if ( empty( $args['capabilities'] ) && null === $args['map_meta_cap'] && in_array( $args['capability_type'], array( 'post', 'page' ) ) ) {
  403. $args['map_meta_cap'] = true;
  404. }
  405. // If not set, default to false.
  406. if ( null === $args['map_meta_cap'] ) {
  407. $args['map_meta_cap'] = false;
  408. }
  409. // If there's no specified edit link and no UI, remove the edit link.
  410. if ( ! $args['show_ui'] && ! $has_edit_link ) {
  411. $args['_edit_link'] = '';
  412. }
  413. $this->cap = get_post_type_capabilities( (object) $args );
  414. unset( $args['capabilities'] );
  415. if ( is_array( $args['capability_type'] ) ) {
  416. $args['capability_type'] = $args['capability_type'][0];
  417. }
  418. if ( false !== $args['query_var'] ) {
  419. if ( true === $args['query_var'] ) {
  420. $args['query_var'] = $this->name;
  421. } else {
  422. $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
  423. }
  424. }
  425. if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
  426. if ( ! is_array( $args['rewrite'] ) ) {
  427. $args['rewrite'] = array();
  428. }
  429. if ( empty( $args['rewrite']['slug'] ) ) {
  430. $args['rewrite']['slug'] = $this->name;
  431. }
  432. if ( ! isset( $args['rewrite']['with_front'] ) ) {
  433. $args['rewrite']['with_front'] = true;
  434. }
  435. if ( ! isset( $args['rewrite']['pages'] ) ) {
  436. $args['rewrite']['pages'] = true;
  437. }
  438. if ( ! isset( $args['rewrite']['feeds'] ) || ! $args['has_archive'] ) {
  439. $args['rewrite']['feeds'] = (bool) $args['has_archive'];
  440. }
  441. if ( ! isset( $args['rewrite']['ep_mask'] ) ) {
  442. if ( isset( $args['permalink_epmask'] ) ) {
  443. $args['rewrite']['ep_mask'] = $args['permalink_epmask'];
  444. } else {
  445. $args['rewrite']['ep_mask'] = EP_PERMALINK;
  446. }
  447. }
  448. }
  449. foreach ( $args as $property_name => $property_value ) {
  450. $this->$property_name = $property_value;
  451. }
  452. $this->labels = get_post_type_labels( $this );
  453. $this->label = $this->labels->name;
  454. }
  455. /**
  456. * Sets the features support for the post type.
  457. *
  458. * @since 4.6.0
  459. */
  460. public function add_supports() {
  461. if ( ! empty( $this->supports ) ) {
  462. add_post_type_support( $this->name, $this->supports );
  463. unset( $this->supports );
  464. } elseif ( false !== $this->supports ) {
  465. // Add default features.
  466. add_post_type_support( $this->name, array( 'title', 'editor' ) );
  467. }
  468. }
  469. /**
  470. * Adds the necessary rewrite rules for the post type.
  471. *
  472. * @since 4.6.0
  473. *
  474. * @global WP_Rewrite $wp_rewrite WordPress Rewrite Component.
  475. * @global WP $wp Current WordPress environment instance.
  476. */
  477. public function add_rewrite_rules() {
  478. global $wp_rewrite, $wp;
  479. if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) {
  480. $wp->add_query_var( $this->query_var );
  481. }
  482. if ( false !== $this->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
  483. if ( $this->hierarchical ) {
  484. add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" );
  485. } else {
  486. add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" );
  487. }
  488. if ( $this->has_archive ) {
  489. $archive_slug = true === $this->has_archive ? $this->rewrite['slug'] : $this->has_archive;
  490. if ( $this->rewrite['with_front'] ) {
  491. $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
  492. } else {
  493. $archive_slug = $wp_rewrite->root . $archive_slug;
  494. }
  495. add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' );
  496. if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) {
  497. $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
  498. add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
  499. add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
  500. }
  501. if ( $this->rewrite['pages'] ) {
  502. add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' );
  503. }
  504. }
  505. $permastruct_args = $this->rewrite;
  506. $permastruct_args['feed'] = $permastruct_args['feeds'];
  507. add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $permastruct_args );
  508. }
  509. }
  510. /**
  511. * Registers the post type meta box if a custom callback was specified.
  512. *
  513. * @since 4.6.0
  514. */
  515. public function register_meta_boxes() {
  516. if ( $this->register_meta_box_cb ) {
  517. add_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10, 1 );
  518. }
  519. }
  520. /**
  521. * Adds the future post hook action for the post type.
  522. *
  523. * @since 4.6.0
  524. */
  525. public function add_hooks() {
  526. add_action( 'future_' . $this->name, '_future_post_hook', 5, 2 );
  527. }
  528. /**
  529. * Registers the taxonomies for the post type.
  530. *
  531. * @since 4.6.0
  532. */
  533. public function register_taxonomies() {
  534. foreach ( $this->taxonomies as $taxonomy ) {
  535. register_taxonomy_for_object_type( $taxonomy, $this->name );
  536. }
  537. }
  538. /**
  539. * Removes the features support for the post type.
  540. *
  541. * @since 4.6.0
  542. *
  543. * @global array $_wp_post_type_features Post type features.
  544. */
  545. public function remove_supports() {
  546. global $_wp_post_type_features;
  547. unset( $_wp_post_type_features[ $this->name ] );
  548. }
  549. /**
  550. * Removes any rewrite rules, permastructs, and rules for the post type.
  551. *
  552. * @since 4.6.0
  553. *
  554. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  555. * @global WP $wp Current WordPress environment instance.
  556. * @global array $post_type_meta_caps Used to remove meta capabilities.
  557. */
  558. public function remove_rewrite_rules() {
  559. global $wp, $wp_rewrite, $post_type_meta_caps;
  560. // Remove query var.
  561. if ( false !== $this->query_var ) {
  562. $wp->remove_query_var( $this->query_var );
  563. }
  564. // Remove any rewrite rules, permastructs, and rules.
  565. if ( false !== $this->rewrite ) {
  566. remove_rewrite_tag( "%$this->name%" );
  567. remove_permastruct( $this->name );
  568. foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) {
  569. if ( false !== strpos( $query, "index.php?post_type=$this->name" ) ) {
  570. unset( $wp_rewrite->extra_rules_top[ $regex ] );
  571. }
  572. }
  573. }
  574. // Remove registered custom meta capabilities.
  575. foreach ( $this->cap as $cap ) {
  576. unset( $post_type_meta_caps[ $cap ] );
  577. }
  578. }
  579. /**
  580. * Unregisters the post type meta box if a custom callback was specified.
  581. *
  582. * @since 4.6.0
  583. */
  584. public function unregister_meta_boxes() {
  585. if ( $this->register_meta_box_cb ) {
  586. remove_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10 );
  587. }
  588. }
  589. /**
  590. * Removes the post type from all taxonomies.
  591. *
  592. * @since 4.6.0
  593. */
  594. public function unregister_taxonomies() {
  595. foreach ( get_object_taxonomies( $this->name ) as $taxonomy ) {
  596. unregister_taxonomy_for_object_type( $taxonomy, $this->name );
  597. }
  598. }
  599. /**
  600. * Removes the future post hook action for the post type.
  601. *
  602. * @since 4.6.0
  603. */
  604. public function remove_hooks() {
  605. remove_action( 'future_' . $this->name, '_future_post_hook', 5 );
  606. }
  607. }