class-wp-post-type.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. * The controller instance for this post type's REST API endpoints.
  309. *
  310. * Lazily computed. Should be accessed using {@see WP_Post_Type::get_rest_controller()}.
  311. *
  312. * @since 5.3.0
  313. * @var WP_REST_Controller $rest_controller
  314. */
  315. public $rest_controller;
  316. /**
  317. * Constructor.
  318. *
  319. * Will populate object properties from the provided arguments and assign other
  320. * default properties based on that information.
  321. *
  322. * @since 4.6.0
  323. *
  324. * @see register_post_type()
  325. *
  326. * @param string $post_type Post type key.
  327. * @param array|string $args Optional. Array or string of arguments for registering a post type.
  328. * Default empty array.
  329. */
  330. public function __construct( $post_type, $args = array() ) {
  331. $this->name = $post_type;
  332. $this->set_props( $args );
  333. }
  334. /**
  335. * Sets post type properties.
  336. *
  337. * @since 4.6.0
  338. *
  339. * @param array|string $args Array or string of arguments for registering a post type.
  340. */
  341. public function set_props( $args ) {
  342. $args = wp_parse_args( $args );
  343. /**
  344. * Filters the arguments for registering a post type.
  345. *
  346. * @since 4.4.0
  347. *
  348. * @param array $args Array of arguments for registering a post type.
  349. * @param string $post_type Post type key.
  350. */
  351. $args = apply_filters( 'register_post_type_args', $args, $this->name );
  352. $has_edit_link = ! empty( $args['_edit_link'] );
  353. // Args prefixed with an underscore are reserved for internal use.
  354. $defaults = array(
  355. 'labels' => array(),
  356. 'description' => '',
  357. 'public' => false,
  358. 'hierarchical' => false,
  359. 'exclude_from_search' => null,
  360. 'publicly_queryable' => null,
  361. 'show_ui' => null,
  362. 'show_in_menu' => null,
  363. 'show_in_nav_menus' => null,
  364. 'show_in_admin_bar' => null,
  365. 'menu_position' => null,
  366. 'menu_icon' => null,
  367. 'capability_type' => 'post',
  368. 'capabilities' => array(),
  369. 'map_meta_cap' => null,
  370. 'supports' => array(),
  371. 'register_meta_box_cb' => null,
  372. 'taxonomies' => array(),
  373. 'has_archive' => false,
  374. 'rewrite' => true,
  375. 'query_var' => true,
  376. 'can_export' => true,
  377. 'delete_with_user' => null,
  378. 'show_in_rest' => false,
  379. 'rest_base' => false,
  380. 'rest_controller_class' => false,
  381. '_builtin' => false,
  382. '_edit_link' => 'post.php?post=%d',
  383. );
  384. $args = array_merge( $defaults, $args );
  385. $args['name'] = $this->name;
  386. // If not set, default to the setting for public.
  387. if ( null === $args['publicly_queryable'] ) {
  388. $args['publicly_queryable'] = $args['public'];
  389. }
  390. // If not set, default to the setting for public.
  391. if ( null === $args['show_ui'] ) {
  392. $args['show_ui'] = $args['public'];
  393. }
  394. // If not set, default to the setting for show_ui.
  395. if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
  396. $args['show_in_menu'] = $args['show_ui'];
  397. }
  398. // If not set, default to the whether the full UI is shown.
  399. if ( null === $args['show_in_admin_bar'] ) {
  400. $args['show_in_admin_bar'] = (bool) $args['show_in_menu'];
  401. }
  402. // If not set, default to the setting for public.
  403. if ( null === $args['show_in_nav_menus'] ) {
  404. $args['show_in_nav_menus'] = $args['public'];
  405. }
  406. // If not set, default to true if not public, false if public.
  407. if ( null === $args['exclude_from_search'] ) {
  408. $args['exclude_from_search'] = ! $args['public'];
  409. }
  410. // Back compat with quirky handling in version 3.0. #14122.
  411. if ( empty( $args['capabilities'] ) && null === $args['map_meta_cap'] && in_array( $args['capability_type'], array( 'post', 'page' ) ) ) {
  412. $args['map_meta_cap'] = true;
  413. }
  414. // If not set, default to false.
  415. if ( null === $args['map_meta_cap'] ) {
  416. $args['map_meta_cap'] = false;
  417. }
  418. // If there's no specified edit link and no UI, remove the edit link.
  419. if ( ! $args['show_ui'] && ! $has_edit_link ) {
  420. $args['_edit_link'] = '';
  421. }
  422. $this->cap = get_post_type_capabilities( (object) $args );
  423. unset( $args['capabilities'] );
  424. if ( is_array( $args['capability_type'] ) ) {
  425. $args['capability_type'] = $args['capability_type'][0];
  426. }
  427. if ( false !== $args['query_var'] ) {
  428. if ( true === $args['query_var'] ) {
  429. $args['query_var'] = $this->name;
  430. } else {
  431. $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
  432. }
  433. }
  434. if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
  435. if ( ! is_array( $args['rewrite'] ) ) {
  436. $args['rewrite'] = array();
  437. }
  438. if ( empty( $args['rewrite']['slug'] ) ) {
  439. $args['rewrite']['slug'] = $this->name;
  440. }
  441. if ( ! isset( $args['rewrite']['with_front'] ) ) {
  442. $args['rewrite']['with_front'] = true;
  443. }
  444. if ( ! isset( $args['rewrite']['pages'] ) ) {
  445. $args['rewrite']['pages'] = true;
  446. }
  447. if ( ! isset( $args['rewrite']['feeds'] ) || ! $args['has_archive'] ) {
  448. $args['rewrite']['feeds'] = (bool) $args['has_archive'];
  449. }
  450. if ( ! isset( $args['rewrite']['ep_mask'] ) ) {
  451. if ( isset( $args['permalink_epmask'] ) ) {
  452. $args['rewrite']['ep_mask'] = $args['permalink_epmask'];
  453. } else {
  454. $args['rewrite']['ep_mask'] = EP_PERMALINK;
  455. }
  456. }
  457. }
  458. foreach ( $args as $property_name => $property_value ) {
  459. $this->$property_name = $property_value;
  460. }
  461. $this->labels = get_post_type_labels( $this );
  462. $this->label = $this->labels->name;
  463. }
  464. /**
  465. * Sets the features support for the post type.
  466. *
  467. * @since 4.6.0
  468. */
  469. public function add_supports() {
  470. if ( ! empty( $this->supports ) ) {
  471. foreach ( $this->supports as $feature => $args ) {
  472. if ( is_array( $args ) ) {
  473. add_post_type_support( $this->name, $feature, $args );
  474. } else {
  475. add_post_type_support( $this->name, $args );
  476. }
  477. }
  478. unset( $this->supports );
  479. } elseif ( false !== $this->supports ) {
  480. // Add default features.
  481. add_post_type_support( $this->name, array( 'title', 'editor' ) );
  482. }
  483. }
  484. /**
  485. * Adds the necessary rewrite rules for the post type.
  486. *
  487. * @since 4.6.0
  488. *
  489. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  490. * @global WP $wp Current WordPress environment instance.
  491. */
  492. public function add_rewrite_rules() {
  493. global $wp_rewrite, $wp;
  494. if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) {
  495. $wp->add_query_var( $this->query_var );
  496. }
  497. if ( false !== $this->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
  498. if ( $this->hierarchical ) {
  499. add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" );
  500. } else {
  501. add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" );
  502. }
  503. if ( $this->has_archive ) {
  504. $archive_slug = true === $this->has_archive ? $this->rewrite['slug'] : $this->has_archive;
  505. if ( $this->rewrite['with_front'] ) {
  506. $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
  507. } else {
  508. $archive_slug = $wp_rewrite->root . $archive_slug;
  509. }
  510. add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' );
  511. if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) {
  512. $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
  513. add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
  514. add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
  515. }
  516. if ( $this->rewrite['pages'] ) {
  517. add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' );
  518. }
  519. }
  520. $permastruct_args = $this->rewrite;
  521. $permastruct_args['feed'] = $permastruct_args['feeds'];
  522. add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $permastruct_args );
  523. }
  524. }
  525. /**
  526. * Registers the post type meta box if a custom callback was specified.
  527. *
  528. * @since 4.6.0
  529. */
  530. public function register_meta_boxes() {
  531. if ( $this->register_meta_box_cb ) {
  532. add_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10, 1 );
  533. }
  534. }
  535. /**
  536. * Adds the future post hook action for the post type.
  537. *
  538. * @since 4.6.0
  539. */
  540. public function add_hooks() {
  541. add_action( 'future_' . $this->name, '_future_post_hook', 5, 2 );
  542. }
  543. /**
  544. * Registers the taxonomies for the post type.
  545. *
  546. * @since 4.6.0
  547. */
  548. public function register_taxonomies() {
  549. foreach ( $this->taxonomies as $taxonomy ) {
  550. register_taxonomy_for_object_type( $taxonomy, $this->name );
  551. }
  552. }
  553. /**
  554. * Removes the features support for the post type.
  555. *
  556. * @since 4.6.0
  557. *
  558. * @global array $_wp_post_type_features Post type features.
  559. */
  560. public function remove_supports() {
  561. global $_wp_post_type_features;
  562. unset( $_wp_post_type_features[ $this->name ] );
  563. }
  564. /**
  565. * Removes any rewrite rules, permastructs, and rules for the post type.
  566. *
  567. * @since 4.6.0
  568. *
  569. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  570. * @global WP $wp Current WordPress environment instance.
  571. * @global array $post_type_meta_caps Used to remove meta capabilities.
  572. */
  573. public function remove_rewrite_rules() {
  574. global $wp, $wp_rewrite, $post_type_meta_caps;
  575. // Remove query var.
  576. if ( false !== $this->query_var ) {
  577. $wp->remove_query_var( $this->query_var );
  578. }
  579. // Remove any rewrite rules, permastructs, and rules.
  580. if ( false !== $this->rewrite ) {
  581. remove_rewrite_tag( "%$this->name%" );
  582. remove_permastruct( $this->name );
  583. foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) {
  584. if ( false !== strpos( $query, "index.php?post_type=$this->name" ) ) {
  585. unset( $wp_rewrite->extra_rules_top[ $regex ] );
  586. }
  587. }
  588. }
  589. // Remove registered custom meta capabilities.
  590. foreach ( $this->cap as $cap ) {
  591. unset( $post_type_meta_caps[ $cap ] );
  592. }
  593. }
  594. /**
  595. * Unregisters the post type meta box if a custom callback was specified.
  596. *
  597. * @since 4.6.0
  598. */
  599. public function unregister_meta_boxes() {
  600. if ( $this->register_meta_box_cb ) {
  601. remove_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10 );
  602. }
  603. }
  604. /**
  605. * Removes the post type from all taxonomies.
  606. *
  607. * @since 4.6.0
  608. */
  609. public function unregister_taxonomies() {
  610. foreach ( get_object_taxonomies( $this->name ) as $taxonomy ) {
  611. unregister_taxonomy_for_object_type( $taxonomy, $this->name );
  612. }
  613. }
  614. /**
  615. * Removes the future post hook action for the post type.
  616. *
  617. * @since 4.6.0
  618. */
  619. public function remove_hooks() {
  620. remove_action( 'future_' . $this->name, '_future_post_hook', 5 );
  621. }
  622. /**
  623. * Gets the REST API controller for this post type.
  624. *
  625. * Will only instantiate the controller class once per request.
  626. *
  627. * @since 5.3.0
  628. *
  629. * @return WP_REST_Controller|null The controller instance, or null if the post type
  630. * is set not to show in rest.
  631. */
  632. public function get_rest_controller() {
  633. if ( ! $this->show_in_rest ) {
  634. return null;
  635. }
  636. $class = $this->rest_controller_class ? $this->rest_controller_class : WP_REST_Posts_Controller::class;
  637. if ( ! class_exists( $class ) ) {
  638. return null;
  639. }
  640. if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
  641. return null;
  642. }
  643. if ( ! $this->rest_controller ) {
  644. $this->rest_controller = new $class( $this->name );
  645. }
  646. if ( ! ( $this->rest_controller instanceof $class ) ) {
  647. return null;
  648. }
  649. return $this->rest_controller;
  650. }
  651. }