Geen omschrijving

class-wp-post-type.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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 stdClass $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|string $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 or reference 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 callable $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 the 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 the 'author' feature.
  234. * Otherwise posts are not trashed or deleted.
  235. *
  236. * Default null.
  237. *
  238. * @since 4.6.0
  239. * @var bool $delete_with_user
  240. */
  241. public $delete_with_user = null;
  242. /**
  243. * Array of blocks to use as the default initial state for an editor session.
  244. *
  245. * Each item should be an array containing block name and optional attributes.
  246. *
  247. * Default empty array.
  248. *
  249. * @link https://developer.wordpress.org/block-editor/developers/block-api/block-templates/
  250. *
  251. * @since 5.0.0
  252. * @var array $template
  253. */
  254. public $template = array();
  255. /**
  256. * Whether the block template should be locked if $template is set.
  257. *
  258. * - If set to 'all', the user is unable to insert new blocks, move existing blocks
  259. * and delete blocks.
  260. * - If set to 'insert', the user is able to move existing blocks but is unable to insert
  261. * new blocks and delete blocks.
  262. *
  263. * Default false.
  264. *
  265. * @link https://developer.wordpress.org/block-editor/developers/block-api/block-templates/
  266. *
  267. * @since 5.0.0
  268. * @var string|false $template_lock
  269. */
  270. public $template_lock = false;
  271. /**
  272. * Whether this post type is a native or "built-in" post_type.
  273. *
  274. * Default false.
  275. *
  276. * @since 4.6.0
  277. * @var bool $_builtin
  278. */
  279. public $_builtin = false;
  280. /**
  281. * URL segment to use for edit link of this post type.
  282. *
  283. * Default 'post.php?post=%d'.
  284. *
  285. * @since 4.6.0
  286. * @var string $_edit_link
  287. */
  288. public $_edit_link = 'post.php?post=%d';
  289. /**
  290. * Post type capabilities.
  291. *
  292. * @since 4.6.0
  293. * @var stdClass $cap
  294. */
  295. public $cap;
  296. /**
  297. * Triggers the handling of rewrites for this post type.
  298. *
  299. * Defaults to true, using $post_type as slug.
  300. *
  301. * @since 4.6.0
  302. * @var array|false $rewrite
  303. */
  304. public $rewrite;
  305. /**
  306. * The features supported by the post type.
  307. *
  308. * @since 4.6.0
  309. * @var array|bool $supports
  310. */
  311. public $supports;
  312. /**
  313. * Whether this post type should appear in the REST API.
  314. *
  315. * Default false. If true, standard endpoints will be registered with
  316. * respect to $rest_base and $rest_controller_class.
  317. *
  318. * @since 4.7.4
  319. * @var bool $show_in_rest
  320. */
  321. public $show_in_rest;
  322. /**
  323. * The base path for this post type's REST API endpoints.
  324. *
  325. * @since 4.7.4
  326. * @var string|bool $rest_base
  327. */
  328. public $rest_base;
  329. /**
  330. * The controller for this post type's REST API endpoints.
  331. *
  332. * Custom controllers must extend WP_REST_Controller.
  333. *
  334. * @since 4.7.4
  335. * @var string|bool $rest_controller_class
  336. */
  337. public $rest_controller_class;
  338. /**
  339. * The controller instance for this post type's REST API endpoints.
  340. *
  341. * Lazily computed. Should be accessed using {@see WP_Post_Type::get_rest_controller()}.
  342. *
  343. * @since 5.3.0
  344. * @var WP_REST_Controller $rest_controller
  345. */
  346. public $rest_controller;
  347. /**
  348. * Constructor.
  349. *
  350. * See the register_post_type() function for accepted arguments for `$args`.
  351. *
  352. * Will populate object properties from the provided arguments and assign other
  353. * default properties based on that information.
  354. *
  355. * @since 4.6.0
  356. *
  357. * @see register_post_type()
  358. *
  359. * @param string $post_type Post type key.
  360. * @param array|string $args Optional. Array or string of arguments for registering a post type.
  361. * Default empty array.
  362. */
  363. public function __construct( $post_type, $args = array() ) {
  364. $this->name = $post_type;
  365. $this->set_props( $args );
  366. }
  367. /**
  368. * Sets post type properties.
  369. *
  370. * See the register_post_type() function for accepted arguments for `$args`.
  371. *
  372. * @since 4.6.0
  373. *
  374. * @param array|string $args Array or string of arguments for registering a post type.
  375. */
  376. public function set_props( $args ) {
  377. $args = wp_parse_args( $args );
  378. /**
  379. * Filters the arguments for registering a post type.
  380. *
  381. * @since 4.4.0
  382. *
  383. * @param array $args Array of arguments for registering a post type.
  384. * See the register_post_type() function for accepted arguments.
  385. * @param string $post_type Post type key.
  386. */
  387. $args = apply_filters( 'register_post_type_args', $args, $this->name );
  388. $has_edit_link = ! empty( $args['_edit_link'] );
  389. // Args prefixed with an underscore are reserved for internal use.
  390. $defaults = array(
  391. 'labels' => array(),
  392. 'description' => '',
  393. 'public' => false,
  394. 'hierarchical' => false,
  395. 'exclude_from_search' => null,
  396. 'publicly_queryable' => null,
  397. 'show_ui' => null,
  398. 'show_in_menu' => null,
  399. 'show_in_nav_menus' => null,
  400. 'show_in_admin_bar' => null,
  401. 'menu_position' => null,
  402. 'menu_icon' => null,
  403. 'capability_type' => 'post',
  404. 'capabilities' => array(),
  405. 'map_meta_cap' => null,
  406. 'supports' => array(),
  407. 'register_meta_box_cb' => null,
  408. 'taxonomies' => array(),
  409. 'has_archive' => false,
  410. 'rewrite' => true,
  411. 'query_var' => true,
  412. 'can_export' => true,
  413. 'delete_with_user' => null,
  414. 'show_in_rest' => false,
  415. 'rest_base' => false,
  416. 'rest_controller_class' => false,
  417. 'template' => array(),
  418. 'template_lock' => false,
  419. '_builtin' => false,
  420. '_edit_link' => 'post.php?post=%d',
  421. );
  422. $args = array_merge( $defaults, $args );
  423. $args['name'] = $this->name;
  424. // If not set, default to the setting for 'public'.
  425. if ( null === $args['publicly_queryable'] ) {
  426. $args['publicly_queryable'] = $args['public'];
  427. }
  428. // If not set, default to the setting for 'public'.
  429. if ( null === $args['show_ui'] ) {
  430. $args['show_ui'] = $args['public'];
  431. }
  432. // If not set, default to the setting for 'show_ui'.
  433. if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
  434. $args['show_in_menu'] = $args['show_ui'];
  435. }
  436. // If not set, default to the setting for 'show_in_menu'.
  437. if ( null === $args['show_in_admin_bar'] ) {
  438. $args['show_in_admin_bar'] = (bool) $args['show_in_menu'];
  439. }
  440. // If not set, default to the setting for 'public'.
  441. if ( null === $args['show_in_nav_menus'] ) {
  442. $args['show_in_nav_menus'] = $args['public'];
  443. }
  444. // If not set, default to true if not public, false if public.
  445. if ( null === $args['exclude_from_search'] ) {
  446. $args['exclude_from_search'] = ! $args['public'];
  447. }
  448. // Back compat with quirky handling in version 3.0. #14122.
  449. if ( empty( $args['capabilities'] )
  450. && null === $args['map_meta_cap'] && in_array( $args['capability_type'], array( 'post', 'page' ), true )
  451. ) {
  452. $args['map_meta_cap'] = true;
  453. }
  454. // If not set, default to false.
  455. if ( null === $args['map_meta_cap'] ) {
  456. $args['map_meta_cap'] = false;
  457. }
  458. // If there's no specified edit link and no UI, remove the edit link.
  459. if ( ! $args['show_ui'] && ! $has_edit_link ) {
  460. $args['_edit_link'] = '';
  461. }
  462. $this->cap = get_post_type_capabilities( (object) $args );
  463. unset( $args['capabilities'] );
  464. if ( is_array( $args['capability_type'] ) ) {
  465. $args['capability_type'] = $args['capability_type'][0];
  466. }
  467. if ( false !== $args['query_var'] ) {
  468. if ( true === $args['query_var'] ) {
  469. $args['query_var'] = $this->name;
  470. } else {
  471. $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
  472. }
  473. }
  474. if ( false !== $args['rewrite'] && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
  475. if ( ! is_array( $args['rewrite'] ) ) {
  476. $args['rewrite'] = array();
  477. }
  478. if ( empty( $args['rewrite']['slug'] ) ) {
  479. $args['rewrite']['slug'] = $this->name;
  480. }
  481. if ( ! isset( $args['rewrite']['with_front'] ) ) {
  482. $args['rewrite']['with_front'] = true;
  483. }
  484. if ( ! isset( $args['rewrite']['pages'] ) ) {
  485. $args['rewrite']['pages'] = true;
  486. }
  487. if ( ! isset( $args['rewrite']['feeds'] ) || ! $args['has_archive'] ) {
  488. $args['rewrite']['feeds'] = (bool) $args['has_archive'];
  489. }
  490. if ( ! isset( $args['rewrite']['ep_mask'] ) ) {
  491. if ( isset( $args['permalink_epmask'] ) ) {
  492. $args['rewrite']['ep_mask'] = $args['permalink_epmask'];
  493. } else {
  494. $args['rewrite']['ep_mask'] = EP_PERMALINK;
  495. }
  496. }
  497. }
  498. foreach ( $args as $property_name => $property_value ) {
  499. $this->$property_name = $property_value;
  500. }
  501. $this->labels = get_post_type_labels( $this );
  502. $this->label = $this->labels->name;
  503. }
  504. /**
  505. * Sets the features support for the post type.
  506. *
  507. * @since 4.6.0
  508. */
  509. public function add_supports() {
  510. if ( ! empty( $this->supports ) ) {
  511. foreach ( $this->supports as $feature => $args ) {
  512. if ( is_array( $args ) ) {
  513. add_post_type_support( $this->name, $feature, $args );
  514. } else {
  515. add_post_type_support( $this->name, $args );
  516. }
  517. }
  518. unset( $this->supports );
  519. } elseif ( false !== $this->supports ) {
  520. // Add default features.
  521. add_post_type_support( $this->name, array( 'title', 'editor' ) );
  522. }
  523. }
  524. /**
  525. * Adds the necessary rewrite rules for the post type.
  526. *
  527. * @since 4.6.0
  528. *
  529. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  530. * @global WP $wp Current WordPress environment instance.
  531. */
  532. public function add_rewrite_rules() {
  533. global $wp_rewrite, $wp;
  534. if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) {
  535. $wp->add_query_var( $this->query_var );
  536. }
  537. if ( false !== $this->rewrite && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
  538. if ( $this->hierarchical ) {
  539. add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" );
  540. } else {
  541. add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" );
  542. }
  543. if ( $this->has_archive ) {
  544. $archive_slug = true === $this->has_archive ? $this->rewrite['slug'] : $this->has_archive;
  545. if ( $this->rewrite['with_front'] ) {
  546. $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
  547. } else {
  548. $archive_slug = $wp_rewrite->root . $archive_slug;
  549. }
  550. add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' );
  551. if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) {
  552. $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
  553. add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
  554. add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
  555. }
  556. if ( $this->rewrite['pages'] ) {
  557. add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' );
  558. }
  559. }
  560. $permastruct_args = $this->rewrite;
  561. $permastruct_args['feed'] = $permastruct_args['feeds'];
  562. add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $permastruct_args );
  563. }
  564. }
  565. /**
  566. * Registers the post type meta box if a custom callback was specified.
  567. *
  568. * @since 4.6.0
  569. */
  570. public function register_meta_boxes() {
  571. if ( $this->register_meta_box_cb ) {
  572. add_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10, 1 );
  573. }
  574. }
  575. /**
  576. * Adds the future post hook action for the post type.
  577. *
  578. * @since 4.6.0
  579. */
  580. public function add_hooks() {
  581. add_action( 'future_' . $this->name, '_future_post_hook', 5, 2 );
  582. }
  583. /**
  584. * Registers the taxonomies for the post type.
  585. *
  586. * @since 4.6.0
  587. */
  588. public function register_taxonomies() {
  589. foreach ( $this->taxonomies as $taxonomy ) {
  590. register_taxonomy_for_object_type( $taxonomy, $this->name );
  591. }
  592. }
  593. /**
  594. * Removes the features support for the post type.
  595. *
  596. * @since 4.6.0
  597. *
  598. * @global array $_wp_post_type_features Post type features.
  599. */
  600. public function remove_supports() {
  601. global $_wp_post_type_features;
  602. unset( $_wp_post_type_features[ $this->name ] );
  603. }
  604. /**
  605. * Removes any rewrite rules, permastructs, and rules for the post type.
  606. *
  607. * @since 4.6.0
  608. *
  609. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  610. * @global WP $wp Current WordPress environment instance.
  611. * @global array $post_type_meta_caps Used to remove meta capabilities.
  612. */
  613. public function remove_rewrite_rules() {
  614. global $wp, $wp_rewrite, $post_type_meta_caps;
  615. // Remove query var.
  616. if ( false !== $this->query_var ) {
  617. $wp->remove_query_var( $this->query_var );
  618. }
  619. // Remove any rewrite rules, permastructs, and rules.
  620. if ( false !== $this->rewrite ) {
  621. remove_rewrite_tag( "%$this->name%" );
  622. remove_permastruct( $this->name );
  623. foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) {
  624. if ( false !== strpos( $query, "index.php?post_type=$this->name" ) ) {
  625. unset( $wp_rewrite->extra_rules_top[ $regex ] );
  626. }
  627. }
  628. }
  629. // Remove registered custom meta capabilities.
  630. foreach ( $this->cap as $cap ) {
  631. unset( $post_type_meta_caps[ $cap ] );
  632. }
  633. }
  634. /**
  635. * Unregisters the post type meta box if a custom callback was specified.
  636. *
  637. * @since 4.6.0
  638. */
  639. public function unregister_meta_boxes() {
  640. if ( $this->register_meta_box_cb ) {
  641. remove_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10 );
  642. }
  643. }
  644. /**
  645. * Removes the post type from all taxonomies.
  646. *
  647. * @since 4.6.0
  648. */
  649. public function unregister_taxonomies() {
  650. foreach ( get_object_taxonomies( $this->name ) as $taxonomy ) {
  651. unregister_taxonomy_for_object_type( $taxonomy, $this->name );
  652. }
  653. }
  654. /**
  655. * Removes the future post hook action for the post type.
  656. *
  657. * @since 4.6.0
  658. */
  659. public function remove_hooks() {
  660. remove_action( 'future_' . $this->name, '_future_post_hook', 5 );
  661. }
  662. /**
  663. * Gets the REST API controller for this post type.
  664. *
  665. * Will only instantiate the controller class once per request.
  666. *
  667. * @since 5.3.0
  668. *
  669. * @return WP_REST_Controller|null The controller instance, or null if the post type
  670. * is set not to show in rest.
  671. */
  672. public function get_rest_controller() {
  673. if ( ! $this->show_in_rest ) {
  674. return null;
  675. }
  676. $class = $this->rest_controller_class ? $this->rest_controller_class : WP_REST_Posts_Controller::class;
  677. if ( ! class_exists( $class ) ) {
  678. return null;
  679. }
  680. if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
  681. return null;
  682. }
  683. if ( ! $this->rest_controller ) {
  684. $this->rest_controller = new $class( $this->name );
  685. }
  686. if ( ! ( $this->rest_controller instanceof $class ) ) {
  687. return null;
  688. }
  689. return $this->rest_controller;
  690. }
  691. }