Keine Beschreibung

class-wp-rest-menu-items-controller.php 32KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. <?php
  2. /**
  3. * REST API: WP_REST_Menu_Items_Controller class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 5.9.0
  8. */
  9. /**
  10. * Core class to access nav items via the REST API.
  11. *
  12. * @since 5.9.0
  13. *
  14. * @see WP_REST_Posts_Controller
  15. */
  16. class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
  17. /**
  18. * Get the nav menu item, if the ID is valid.
  19. *
  20. * @since 5.9.0
  21. *
  22. * @param int $id Supplied ID.
  23. * @return object|WP_Error Post object if ID is valid, WP_Error otherwise.
  24. */
  25. protected function get_nav_menu_item( $id ) {
  26. $post = $this->get_post( $id );
  27. if ( is_wp_error( $post ) ) {
  28. return $post;
  29. }
  30. return wp_setup_nav_menu_item( $post );
  31. }
  32. /**
  33. * Checks if a given request has access to read menu items.
  34. *
  35. * @since 5.9.0
  36. *
  37. * @param WP_REST_Request $request Full details about the request.
  38. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  39. */
  40. public function get_items_permissions_check( $request ) {
  41. $has_permission = parent::get_items_permissions_check( $request );
  42. if ( true !== $has_permission ) {
  43. return $has_permission;
  44. }
  45. return $this->check_has_read_only_access( $request );
  46. }
  47. /**
  48. * Checks if a given request has access to read a menu item if they have access to edit them.
  49. *
  50. * @since 5.9.0
  51. *
  52. * @param WP_REST_Request $request Full details about the request.
  53. * @return bool|WP_Error True if the request has read access for the item, WP_Error object otherwise.
  54. */
  55. public function get_item_permissions_check( $request ) {
  56. $permission_check = parent::get_item_permissions_check( $request );
  57. if ( true !== $permission_check ) {
  58. return $permission_check;
  59. }
  60. return $this->check_has_read_only_access( $request );
  61. }
  62. /**
  63. * Checks whether the current user has read permission for the endpoint.
  64. *
  65. * This allows for any user that can `edit_theme_options` or edit any REST API available post type.
  66. *
  67. * @since 5.9.0
  68. *
  69. * @param WP_REST_Request $request Full details about the request.
  70. * @return bool|WP_Error Whether the current user has permission.
  71. */
  72. protected function check_has_read_only_access( $request ) {
  73. if ( current_user_can( 'edit_theme_options' ) ) {
  74. return true;
  75. }
  76. if ( current_user_can( 'edit_posts' ) ) {
  77. return true;
  78. }
  79. foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
  80. if ( current_user_can( $post_type->cap->edit_posts ) ) {
  81. return true;
  82. }
  83. }
  84. return new WP_Error(
  85. 'rest_cannot_view',
  86. __( 'Sorry, you are not allowed to view menu items.' ),
  87. array( 'status' => rest_authorization_required_code() )
  88. );
  89. }
  90. /**
  91. * Creates a single post.
  92. *
  93. * @since 5.9.0
  94. *
  95. * @param WP_REST_Request $request Full details about the request.
  96. *
  97. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  98. */
  99. public function create_item( $request ) {
  100. if ( ! empty( $request['id'] ) ) {
  101. return new WP_Error( 'rest_post_exists', __( 'Cannot create existing post.' ), array( 'status' => 400 ) );
  102. }
  103. $prepared_nav_item = $this->prepare_item_for_database( $request );
  104. if ( is_wp_error( $prepared_nav_item ) ) {
  105. return $prepared_nav_item;
  106. }
  107. $prepared_nav_item = (array) $prepared_nav_item;
  108. $nav_menu_item_id = wp_update_nav_menu_item( $prepared_nav_item['menu-id'], $prepared_nav_item['menu-item-db-id'], wp_slash( $prepared_nav_item ), false );
  109. if ( is_wp_error( $nav_menu_item_id ) ) {
  110. if ( 'db_insert_error' === $nav_menu_item_id->get_error_code() ) {
  111. $nav_menu_item_id->add_data( array( 'status' => 500 ) );
  112. } else {
  113. $nav_menu_item_id->add_data( array( 'status' => 400 ) );
  114. }
  115. return $nav_menu_item_id;
  116. }
  117. $nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id );
  118. if ( is_wp_error( $nav_menu_item ) ) {
  119. $nav_menu_item->add_data( array( 'status' => 404 ) );
  120. return $nav_menu_item;
  121. }
  122. /**
  123. * Fires after a single menu item is created or updated via the REST API.
  124. *
  125. * @since 5.9.0
  126. *
  127. * @param object $nav_menu_item Inserted or updated menu item object.
  128. * @param WP_REST_Request $request Request object.
  129. * @param bool $creating True when creating a menu item, false when updating.
  130. */
  131. do_action( 'rest_insert_nav_menu_item', $nav_menu_item, $request, true );
  132. $schema = $this->get_item_schema();
  133. if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
  134. $meta_update = $this->meta->update_value( $request['meta'], $nav_menu_item_id );
  135. if ( is_wp_error( $meta_update ) ) {
  136. return $meta_update;
  137. }
  138. }
  139. $nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id );
  140. $fields_update = $this->update_additional_fields_for_object( $nav_menu_item, $request );
  141. if ( is_wp_error( $fields_update ) ) {
  142. return $fields_update;
  143. }
  144. $request->set_param( 'context', 'edit' );
  145. /**
  146. * Fires after a single menu item is completely created or updated via the REST API.
  147. *
  148. * @since 5.9.0
  149. *
  150. * @param object $nav_menu_item Inserted or updated menu item object.
  151. * @param WP_REST_Request $request Request object.
  152. * @param bool $creating True when creating a menu item, false when updating.
  153. */
  154. do_action( 'rest_after_insert_nav_menu_item', $nav_menu_item, $request, true );
  155. $post = get_post( $nav_menu_item_id );
  156. wp_after_insert_post( $post, false, null );
  157. $response = $this->prepare_item_for_response( $post, $request );
  158. $response = rest_ensure_response( $response );
  159. $response->set_status( 201 );
  160. $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $nav_menu_item_id ) ) );
  161. return $response;
  162. }
  163. /**
  164. * Updates a single nav menu item.
  165. *
  166. * @since 5.9.0
  167. *
  168. * @param WP_REST_Request $request Full details about the request.
  169. *
  170. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  171. */
  172. public function update_item( $request ) {
  173. $valid_check = $this->get_nav_menu_item( $request['id'] );
  174. if ( is_wp_error( $valid_check ) ) {
  175. return $valid_check;
  176. }
  177. $post_before = get_post( $request['id'] );
  178. $prepared_nav_item = $this->prepare_item_for_database( $request );
  179. if ( is_wp_error( $prepared_nav_item ) ) {
  180. return $prepared_nav_item;
  181. }
  182. $prepared_nav_item = (array) $prepared_nav_item;
  183. $nav_menu_item_id = wp_update_nav_menu_item( $prepared_nav_item['menu-id'], $prepared_nav_item['menu-item-db-id'], wp_slash( $prepared_nav_item ), false );
  184. if ( is_wp_error( $nav_menu_item_id ) ) {
  185. if ( 'db_update_error' === $nav_menu_item_id->get_error_code() ) {
  186. $nav_menu_item_id->add_data( array( 'status' => 500 ) );
  187. } else {
  188. $nav_menu_item_id->add_data( array( 'status' => 400 ) );
  189. }
  190. return $nav_menu_item_id;
  191. }
  192. $nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id );
  193. if ( is_wp_error( $nav_menu_item ) ) {
  194. $nav_menu_item->add_data( array( 'status' => 404 ) );
  195. return $nav_menu_item;
  196. }
  197. /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php */
  198. do_action( 'rest_insert_nav_menu_item', $nav_menu_item, $request, false );
  199. $schema = $this->get_item_schema();
  200. if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
  201. $meta_update = $this->meta->update_value( $request['meta'], $nav_menu_item->ID );
  202. if ( is_wp_error( $meta_update ) ) {
  203. return $meta_update;
  204. }
  205. }
  206. $post = get_post( $nav_menu_item_id );
  207. $nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id );
  208. $fields_update = $this->update_additional_fields_for_object( $nav_menu_item, $request );
  209. if ( is_wp_error( $fields_update ) ) {
  210. return $fields_update;
  211. }
  212. $request->set_param( 'context', 'edit' );
  213. /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php */
  214. do_action( 'rest_after_insert_nav_menu_item', $nav_menu_item, $request, false );
  215. wp_after_insert_post( $post, true, $post_before );
  216. $response = $this->prepare_item_for_response( get_post( $nav_menu_item_id ), $request );
  217. return rest_ensure_response( $response );
  218. }
  219. /**
  220. * Deletes a single menu item.
  221. *
  222. * @since 5.9.0
  223. *
  224. * @param WP_REST_Request $request Full details about the request.
  225. * @return WP_REST_Response|WP_Error True on success, or WP_Error object on failure.
  226. */
  227. public function delete_item( $request ) {
  228. $menu_item = $this->get_nav_menu_item( $request['id'] );
  229. if ( is_wp_error( $menu_item ) ) {
  230. return $menu_item;
  231. }
  232. // We don't support trashing for menu items.
  233. if ( ! $request['force'] ) {
  234. /* translators: %s: force=true */
  235. return new WP_Error( 'rest_trash_not_supported', sprintf( __( "Menu items do not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) );
  236. }
  237. $previous = $this->prepare_item_for_response( get_post( $request['id'] ), $request );
  238. $result = wp_delete_post( $request['id'], true );
  239. if ( ! $result ) {
  240. return new WP_Error( 'rest_cannot_delete', __( 'The post cannot be deleted.' ), array( 'status' => 500 ) );
  241. }
  242. $response = new WP_REST_Response();
  243. $response->set_data(
  244. array(
  245. 'deleted' => true,
  246. 'previous' => $previous->get_data(),
  247. )
  248. );
  249. /**
  250. * Fires immediately after a single menu item is deleted via the REST API.
  251. *
  252. * @since 5.9.0
  253. *
  254. * @param object $nav_menu_item Inserted or updated menu item object.
  255. * @param WP_REST_Response $response The response data.
  256. * @param WP_REST_Request $request Request object.
  257. */
  258. do_action( 'rest_delete_nav_menu_item', $menu_item, $response, $request );
  259. return $response;
  260. }
  261. /**
  262. * Prepares a single post for create or update.
  263. *
  264. * @since 5.9.0
  265. *
  266. * @param WP_REST_Request $request Request object.
  267. *
  268. * @return object|WP_Error
  269. */
  270. protected function prepare_item_for_database( $request ) {
  271. $menu_item_db_id = $request['id'];
  272. $menu_item_obj = $this->get_nav_menu_item( $menu_item_db_id );
  273. // Need to persist the menu item data. See https://core.trac.wordpress.org/ticket/28138
  274. if ( ! is_wp_error( $menu_item_obj ) ) {
  275. // Correct the menu position if this was the first item. See https://core.trac.wordpress.org/ticket/28140
  276. $position = ( 0 === $menu_item_obj->menu_order ) ? 1 : $menu_item_obj->menu_order;
  277. $prepared_nav_item = array(
  278. 'menu-item-db-id' => $menu_item_db_id,
  279. 'menu-item-object-id' => $menu_item_obj->object_id,
  280. 'menu-item-object' => $menu_item_obj->object,
  281. 'menu-item-parent-id' => $menu_item_obj->menu_item_parent,
  282. 'menu-item-position' => $position,
  283. 'menu-item-type' => $menu_item_obj->type,
  284. 'menu-item-title' => $menu_item_obj->title,
  285. 'menu-item-url' => $menu_item_obj->url,
  286. 'menu-item-description' => $menu_item_obj->description,
  287. 'menu-item-attr-title' => $menu_item_obj->attr_title,
  288. 'menu-item-target' => $menu_item_obj->target,
  289. 'menu-item-classes' => $menu_item_obj->classes,
  290. // Stored in the database as a string.
  291. 'menu-item-xfn' => explode( ' ', $menu_item_obj->xfn ),
  292. 'menu-item-status' => $menu_item_obj->post_status,
  293. 'menu-id' => $this->get_menu_id( $menu_item_db_id ),
  294. );
  295. } else {
  296. $prepared_nav_item = array(
  297. 'menu-id' => 0,
  298. 'menu-item-db-id' => 0,
  299. 'menu-item-object-id' => 0,
  300. 'menu-item-object' => '',
  301. 'menu-item-parent-id' => 0,
  302. 'menu-item-position' => 1,
  303. 'menu-item-type' => 'custom',
  304. 'menu-item-title' => '',
  305. 'menu-item-url' => '',
  306. 'menu-item-description' => '',
  307. 'menu-item-attr-title' => '',
  308. 'menu-item-target' => '',
  309. 'menu-item-classes' => array(),
  310. 'menu-item-xfn' => array(),
  311. 'menu-item-status' => 'publish',
  312. );
  313. }
  314. $mapping = array(
  315. 'menu-item-db-id' => 'id',
  316. 'menu-item-object-id' => 'object_id',
  317. 'menu-item-object' => 'object',
  318. 'menu-item-parent-id' => 'parent',
  319. 'menu-item-position' => 'menu_order',
  320. 'menu-item-type' => 'type',
  321. 'menu-item-url' => 'url',
  322. 'menu-item-description' => 'description',
  323. 'menu-item-attr-title' => 'attr_title',
  324. 'menu-item-target' => 'target',
  325. 'menu-item-classes' => 'classes',
  326. 'menu-item-xfn' => 'xfn',
  327. 'menu-item-status' => 'status',
  328. );
  329. $schema = $this->get_item_schema();
  330. foreach ( $mapping as $original => $api_request ) {
  331. if ( isset( $request[ $api_request ] ) ) {
  332. $prepared_nav_item[ $original ] = $request[ $api_request ];
  333. }
  334. }
  335. $taxonomy = get_taxonomy( 'nav_menu' );
  336. $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
  337. // If menus submitted, cast to int.
  338. if ( ! empty( $request[ $base ] ) ) {
  339. $prepared_nav_item['menu-id'] = absint( $request[ $base ] );
  340. }
  341. // Nav menu title.
  342. if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) {
  343. if ( is_string( $request['title'] ) ) {
  344. $prepared_nav_item['menu-item-title'] = $request['title'];
  345. } elseif ( ! empty( $request['title']['raw'] ) ) {
  346. $prepared_nav_item['menu-item-title'] = $request['title']['raw'];
  347. }
  348. }
  349. $error = new WP_Error();
  350. // Check if object id exists before saving.
  351. if ( ! $prepared_nav_item['menu-item-object'] ) {
  352. // If taxonomy, check if term exists.
  353. if ( 'taxonomy' === $prepared_nav_item['menu-item-type'] ) {
  354. $original = get_term( absint( $prepared_nav_item['menu-item-object-id'] ) );
  355. if ( empty( $original ) || is_wp_error( $original ) ) {
  356. $error->add( 'rest_term_invalid_id', __( 'Invalid term ID.' ), array( 'status' => 400 ) );
  357. } else {
  358. $prepared_nav_item['menu-item-object'] = get_term_field( 'taxonomy', $original );
  359. }
  360. // If post, check if post object exists.
  361. } elseif ( 'post_type' === $prepared_nav_item['menu-item-type'] ) {
  362. $original = get_post( absint( $prepared_nav_item['menu-item-object-id'] ) );
  363. if ( empty( $original ) ) {
  364. $error->add( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 400 ) );
  365. } else {
  366. $prepared_nav_item['menu-item-object'] = get_post_type( $original );
  367. }
  368. }
  369. }
  370. // If post type archive, check if post type exists.
  371. if ( 'post_type_archive' === $prepared_nav_item['menu-item-type'] ) {
  372. $post_type = $prepared_nav_item['menu-item-object'] ? $prepared_nav_item['menu-item-object'] : false;
  373. $original = get_post_type_object( $post_type );
  374. if ( ! $original ) {
  375. $error->add( 'rest_post_invalid_type', __( 'Invalid post type.' ), array( 'status' => 400 ) );
  376. }
  377. }
  378. // Check if menu item is type custom, then title and url are required.
  379. if ( 'custom' === $prepared_nav_item['menu-item-type'] ) {
  380. if ( '' === $prepared_nav_item['menu-item-title'] ) {
  381. $error->add( 'rest_title_required', __( 'The title is required when using a custom menu item type.' ), array( 'status' => 400 ) );
  382. }
  383. if ( empty( $prepared_nav_item['menu-item-url'] ) ) {
  384. $error->add( 'rest_url_required', __( 'The url is required when using a custom menu item type.' ), array( 'status' => 400 ) );
  385. }
  386. }
  387. if ( $error->has_errors() ) {
  388. return $error;
  389. }
  390. // The xfn and classes properties are arrays, but passed to wp_update_nav_menu_item as a string.
  391. foreach ( array( 'menu-item-xfn', 'menu-item-classes' ) as $key ) {
  392. $prepared_nav_item[ $key ] = implode( ' ', $prepared_nav_item[ $key ] );
  393. }
  394. // Only draft / publish are valid post status for menu items.
  395. if ( 'publish' !== $prepared_nav_item['menu-item-status'] ) {
  396. $prepared_nav_item['menu-item-status'] = 'draft';
  397. }
  398. $prepared_nav_item = (object) $prepared_nav_item;
  399. /**
  400. * Filters a menu item before it is inserted via the REST API.
  401. *
  402. * @since 5.9.0
  403. *
  404. * @param object $prepared_nav_item An object representing a single menu item prepared
  405. * for inserting or updating the database.
  406. * @param WP_REST_Request $request Request object.
  407. */
  408. return apply_filters( 'rest_pre_insert_nav_menu_item', $prepared_nav_item, $request );
  409. }
  410. /**
  411. * Prepares a single post output for response.
  412. *
  413. * @since 5.9.0
  414. *
  415. * @param WP_Post $item Post object.
  416. * @param WP_REST_Request $request Request object.
  417. * @return WP_REST_Response Response object.
  418. */
  419. public function prepare_item_for_response( $item, $request ) {
  420. // Base fields for every post.
  421. $fields = $this->get_fields_for_response( $request );
  422. $menu_item = $this->get_nav_menu_item( $item->ID );
  423. $data = array();
  424. if ( rest_is_field_included( 'id', $fields ) ) {
  425. $data['id'] = $menu_item->ID;
  426. }
  427. if ( rest_is_field_included( 'title', $fields ) ) {
  428. $data['title'] = array();
  429. }
  430. if ( rest_is_field_included( 'title.raw', $fields ) ) {
  431. $data['title']['raw'] = $menu_item->title;
  432. }
  433. if ( rest_is_field_included( 'title.rendered', $fields ) ) {
  434. add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
  435. /** This filter is documented in wp-includes/post-template.php */
  436. $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
  437. $data['title']['rendered'] = $title;
  438. remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
  439. }
  440. if ( rest_is_field_included( 'status', $fields ) ) {
  441. $data['status'] = $menu_item->post_status;
  442. }
  443. if ( rest_is_field_included( 'url', $fields ) ) {
  444. $data['url'] = $menu_item->url;
  445. }
  446. if ( rest_is_field_included( 'attr_title', $fields ) ) {
  447. // Same as post_excerpt.
  448. $data['attr_title'] = $menu_item->attr_title;
  449. }
  450. if ( rest_is_field_included( 'description', $fields ) ) {
  451. // Same as post_content.
  452. $data['description'] = $menu_item->description;
  453. }
  454. if ( rest_is_field_included( 'type', $fields ) ) {
  455. $data['type'] = $menu_item->type;
  456. }
  457. if ( rest_is_field_included( 'type_label', $fields ) ) {
  458. $data['type_label'] = $menu_item->type_label;
  459. }
  460. if ( rest_is_field_included( 'object', $fields ) ) {
  461. $data['object'] = $menu_item->object;
  462. }
  463. if ( rest_is_field_included( 'object_id', $fields ) ) {
  464. // It is stored as a string, but should be exposed as an integer.
  465. $data['object_id'] = absint( $menu_item->object_id );
  466. }
  467. if ( rest_is_field_included( 'parent', $fields ) ) {
  468. // Same as post_parent, exposed as an integer.
  469. $data['parent'] = (int) $menu_item->menu_item_parent;
  470. }
  471. if ( rest_is_field_included( 'menu_order', $fields ) ) {
  472. // Same as post_parent, exposed as an integer.
  473. $data['menu_order'] = (int) $menu_item->menu_order;
  474. }
  475. if ( rest_is_field_included( 'target', $fields ) ) {
  476. $data['target'] = $menu_item->target;
  477. }
  478. if ( rest_is_field_included( 'classes', $fields ) ) {
  479. $data['classes'] = (array) $menu_item->classes;
  480. }
  481. if ( rest_is_field_included( 'xfn', $fields ) ) {
  482. $data['xfn'] = array_map( 'sanitize_html_class', explode( ' ', $menu_item->xfn ) );
  483. }
  484. if ( rest_is_field_included( 'invalid', $fields ) ) {
  485. $data['invalid'] = (bool) $menu_item->_invalid;
  486. }
  487. if ( rest_is_field_included( 'meta', $fields ) ) {
  488. $data['meta'] = $this->meta->get_value( $menu_item->ID, $request );
  489. }
  490. $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
  491. foreach ( $taxonomies as $taxonomy ) {
  492. $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
  493. if ( rest_is_field_included( $base, $fields ) ) {
  494. $terms = get_the_terms( $item, $taxonomy->name );
  495. if ( ! is_array( $terms ) ) {
  496. continue;
  497. }
  498. $term_ids = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array();
  499. if ( 'nav_menu' === $taxonomy->name ) {
  500. $data[ $base ] = $term_ids ? array_shift( $term_ids ) : 0;
  501. } else {
  502. $data[ $base ] = $term_ids;
  503. }
  504. }
  505. }
  506. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  507. $data = $this->add_additional_fields_to_object( $data, $request );
  508. $data = $this->filter_response_by_context( $data, $context );
  509. // Wrap the data in a response object.
  510. $response = rest_ensure_response( $data );
  511. $links = $this->prepare_links( $item );
  512. $response->add_links( $links );
  513. if ( ! empty( $links['self']['href'] ) ) {
  514. $actions = $this->get_available_actions( $item, $request );
  515. $self = $links['self']['href'];
  516. foreach ( $actions as $rel ) {
  517. $response->add_link( $rel, $self );
  518. }
  519. }
  520. /**
  521. * Filters the menu item data for a REST API response.
  522. *
  523. * @since 5.9.0
  524. *
  525. * @param WP_REST_Response $response The response object.
  526. * @param object $menu_item Menu item setup by {@see wp_setup_nav_menu_item()}.
  527. * @param WP_REST_Request $request Request object.
  528. */
  529. return apply_filters( 'rest_prepare_nav_menu_item', $response, $menu_item, $request );
  530. }
  531. /**
  532. * Prepares links for the request.
  533. *
  534. * @since 5.9.0
  535. *
  536. * @param WP_Post $post Post object.
  537. * @return array Links for the given post.
  538. */
  539. protected function prepare_links( $post ) {
  540. $links = parent::prepare_links( $post );
  541. $menu_item = $this->get_nav_menu_item( $post->ID );
  542. if ( empty( $menu_item->object_id ) ) {
  543. return $links;
  544. }
  545. $path = '';
  546. $type = '';
  547. $key = $menu_item->type;
  548. if ( 'post_type' === $menu_item->type ) {
  549. $path = rest_get_route_for_post( $menu_item->object_id );
  550. $type = get_post_type( $menu_item->object_id );
  551. } elseif ( 'taxonomy' === $menu_item->type ) {
  552. $path = rest_get_route_for_term( $menu_item->object_id );
  553. $type = get_term_field( 'taxonomy', $menu_item->object_id );
  554. }
  555. if ( $path && $type ) {
  556. $links['https://api.w.org/menu-item-object'][] = array(
  557. 'href' => rest_url( $path ),
  558. $key => $type,
  559. 'embeddable' => true,
  560. );
  561. }
  562. return $links;
  563. }
  564. /**
  565. * Retrieve Link Description Objects that should be added to the Schema for the posts collection.
  566. *
  567. * @since 5.9.0
  568. *
  569. * @return array
  570. */
  571. protected function get_schema_links() {
  572. $links = parent::get_schema_links();
  573. $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" );
  574. $links[] = array(
  575. 'rel' => 'https://api.w.org/menu-item-object',
  576. 'title' => __( 'Get linked object.' ),
  577. 'href' => $href,
  578. 'targetSchema' => array(
  579. 'type' => 'object',
  580. 'properties' => array(
  581. 'object' => array(
  582. 'type' => 'integer',
  583. ),
  584. ),
  585. ),
  586. );
  587. return $links;
  588. }
  589. /**
  590. * Retrieves the term's schema, conforming to JSON Schema.
  591. *
  592. * @since 5.9.0
  593. *
  594. * @return array Item schema data.
  595. */
  596. public function get_item_schema() {
  597. $schema = array(
  598. '$schema' => 'http://json-schema.org/draft-04/schema#',
  599. 'title' => $this->post_type,
  600. 'type' => 'object',
  601. );
  602. $schema['properties']['title'] = array(
  603. 'description' => __( 'The title for the object.' ),
  604. 'type' => array( 'string', 'object' ),
  605. 'context' => array( 'view', 'edit', 'embed' ),
  606. 'properties' => array(
  607. 'raw' => array(
  608. 'description' => __( 'Title for the object, as it exists in the database.' ),
  609. 'type' => 'string',
  610. 'context' => array( 'edit' ),
  611. ),
  612. 'rendered' => array(
  613. 'description' => __( 'HTML title for the object, transformed for display.' ),
  614. 'type' => 'string',
  615. 'context' => array( 'view', 'edit', 'embed' ),
  616. 'readonly' => true,
  617. ),
  618. ),
  619. );
  620. $schema['properties']['id'] = array(
  621. 'description' => __( 'Unique identifier for the object.' ),
  622. 'type' => 'integer',
  623. 'default' => 0,
  624. 'minimum' => 0,
  625. 'context' => array( 'view', 'edit', 'embed' ),
  626. 'readonly' => true,
  627. );
  628. $schema['properties']['type_label'] = array(
  629. 'description' => __( 'Name of type.' ),
  630. 'type' => 'string',
  631. 'context' => array( 'view', 'edit', 'embed' ),
  632. 'readonly' => true,
  633. );
  634. $schema['properties']['type'] = array(
  635. 'description' => __( 'The family of objects originally represented, such as "post_type" or "taxonomy".' ),
  636. 'type' => 'string',
  637. 'enum' => array( 'taxonomy', 'post_type', 'post_type_archive', 'custom' ),
  638. 'context' => array( 'view', 'edit', 'embed' ),
  639. 'default' => 'custom',
  640. );
  641. $schema['properties']['status'] = array(
  642. 'description' => __( 'A named status for the object.' ),
  643. 'type' => 'string',
  644. 'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ),
  645. 'default' => 'publish',
  646. 'context' => array( 'view', 'edit', 'embed' ),
  647. );
  648. $schema['properties']['parent'] = array(
  649. 'description' => __( 'The ID for the parent of the object.' ),
  650. 'type' => 'integer',
  651. 'minimum' => 0,
  652. 'default' => 0,
  653. 'context' => array( 'view', 'edit', 'embed' ),
  654. );
  655. $schema['properties']['attr_title'] = array(
  656. 'description' => __( 'Text for the title attribute of the link element for this menu item.' ),
  657. 'type' => 'string',
  658. 'context' => array( 'view', 'edit', 'embed' ),
  659. 'arg_options' => array(
  660. 'sanitize_callback' => 'sanitize_text_field',
  661. ),
  662. );
  663. $schema['properties']['classes'] = array(
  664. 'description' => __( 'Class names for the link element of this menu item.' ),
  665. 'type' => 'array',
  666. 'items' => array(
  667. 'type' => 'string',
  668. ),
  669. 'context' => array( 'view', 'edit', 'embed' ),
  670. 'arg_options' => array(
  671. 'sanitize_callback' => function ( $value ) {
  672. return array_map( 'sanitize_html_class', wp_parse_list( $value ) );
  673. },
  674. ),
  675. );
  676. $schema['properties']['description'] = array(
  677. 'description' => __( 'The description of this menu item.' ),
  678. 'type' => 'string',
  679. 'context' => array( 'view', 'edit', 'embed' ),
  680. 'arg_options' => array(
  681. 'sanitize_callback' => 'sanitize_text_field',
  682. ),
  683. );
  684. $schema['properties']['menu_order'] = array(
  685. 'description' => __( 'The DB ID of the nav_menu_item that is this item\'s menu parent, if any, otherwise 0.' ),
  686. 'context' => array( 'view', 'edit', 'embed' ),
  687. 'type' => 'integer',
  688. 'minimum' => 1,
  689. 'default' => 1,
  690. );
  691. $schema['properties']['object'] = array(
  692. 'description' => __( 'The type of object originally represented, such as "category", "post", or "attachment".' ),
  693. 'context' => array( 'view', 'edit', 'embed' ),
  694. 'type' => 'string',
  695. 'arg_options' => array(
  696. 'sanitize_callback' => 'sanitize_key',
  697. ),
  698. );
  699. $schema['properties']['object_id'] = array(
  700. 'description' => __( 'The database ID of the original object this menu item represents, for example the ID for posts or the term_id for categories.' ),
  701. 'context' => array( 'view', 'edit', 'embed' ),
  702. 'type' => 'integer',
  703. 'minimum' => 0,
  704. 'default' => 0,
  705. );
  706. $schema['properties']['target'] = array(
  707. 'description' => __( 'The target attribute of the link element for this menu item.' ),
  708. 'type' => 'string',
  709. 'context' => array( 'view', 'edit', 'embed' ),
  710. 'enum' => array(
  711. '_blank',
  712. '',
  713. ),
  714. );
  715. $schema['properties']['type_label'] = array(
  716. 'description' => __( 'The singular label used to describe this type of menu item.' ),
  717. 'context' => array( 'view', 'edit', 'embed' ),
  718. 'type' => 'string',
  719. 'readonly' => true,
  720. );
  721. $schema['properties']['url'] = array(
  722. 'description' => __( 'The URL to which this menu item points.' ),
  723. 'type' => 'string',
  724. 'format' => 'uri',
  725. 'context' => array( 'view', 'edit', 'embed' ),
  726. 'arg_options' => array(
  727. 'validate_callback' => static function ( $url ) {
  728. if ( '' === $url ) {
  729. return true;
  730. }
  731. if ( esc_url_raw( $url ) ) {
  732. return true;
  733. }
  734. return new WP_Error(
  735. 'rest_invalid_url',
  736. __( 'Invalid URL.' )
  737. );
  738. },
  739. ),
  740. );
  741. $schema['properties']['xfn'] = array(
  742. 'description' => __( 'The XFN relationship expressed in the link of this menu item.' ),
  743. 'type' => 'array',
  744. 'items' => array(
  745. 'type' => 'string',
  746. ),
  747. 'context' => array( 'view', 'edit', 'embed' ),
  748. 'arg_options' => array(
  749. 'sanitize_callback' => function ( $value ) {
  750. return array_map( 'sanitize_html_class', wp_parse_list( $value ) );
  751. },
  752. ),
  753. );
  754. $schema['properties']['invalid'] = array(
  755. 'description' => __( 'Whether the menu item represents an object that no longer exists.' ),
  756. 'context' => array( 'view', 'edit', 'embed' ),
  757. 'type' => 'boolean',
  758. 'readonly' => true,
  759. );
  760. $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
  761. foreach ( $taxonomies as $taxonomy ) {
  762. $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
  763. $schema['properties'][ $base ] = array(
  764. /* translators: %s: taxonomy name */
  765. 'description' => sprintf( __( 'The terms assigned to the object in the %s taxonomy.' ), $taxonomy->name ),
  766. 'type' => 'array',
  767. 'items' => array(
  768. 'type' => 'integer',
  769. ),
  770. 'context' => array( 'view', 'edit' ),
  771. );
  772. if ( 'nav_menu' === $taxonomy->name ) {
  773. $schema['properties'][ $base ]['type'] = 'integer';
  774. unset( $schema['properties'][ $base ]['items'] );
  775. }
  776. }
  777. $schema['properties']['meta'] = $this->meta->get_field_schema();
  778. $schema_links = $this->get_schema_links();
  779. if ( $schema_links ) {
  780. $schema['links'] = $schema_links;
  781. }
  782. return $this->add_additional_fields_schema( $schema );
  783. }
  784. /**
  785. * Retrieves the query params for the posts collection.
  786. *
  787. * @since 5.9.0
  788. *
  789. * @return array Collection parameters.
  790. */
  791. public function get_collection_params() {
  792. $query_params = parent::get_collection_params();
  793. $query_params['menu_order'] = array(
  794. 'description' => __( 'Limit result set to posts with a specific menu_order value.' ),
  795. 'type' => 'integer',
  796. );
  797. $query_params['order'] = array(
  798. 'description' => __( 'Order sort attribute ascending or descending.' ),
  799. 'type' => 'string',
  800. 'default' => 'asc',
  801. 'enum' => array( 'asc', 'desc' ),
  802. );
  803. $query_params['orderby'] = array(
  804. 'description' => __( 'Sort collection by object attribute.' ),
  805. 'type' => 'string',
  806. 'default' => 'menu_order',
  807. 'enum' => array(
  808. 'author',
  809. 'date',
  810. 'id',
  811. 'include',
  812. 'modified',
  813. 'parent',
  814. 'relevance',
  815. 'slug',
  816. 'include_slugs',
  817. 'title',
  818. 'menu_order',
  819. ),
  820. );
  821. // Change default to 100 items.
  822. $query_params['per_page']['default'] = 100;
  823. return $query_params;
  824. }
  825. /**
  826. * Determines the allowed query_vars for a get_items() response and prepares
  827. * them for WP_Query.
  828. *
  829. * @since 5.9.0
  830. *
  831. * @param array $prepared_args Optional. Prepared WP_Query arguments. Default empty array.
  832. * @param WP_REST_Request $request Optional. Full details about the request.
  833. * @return array Items query arguments.
  834. */
  835. protected function prepare_items_query( $prepared_args = array(), $request = null ) {
  836. $query_args = parent::prepare_items_query( $prepared_args, $request );
  837. // Map to proper WP_Query orderby param.
  838. if ( isset( $query_args['orderby'], $request['orderby'] ) ) {
  839. $orderby_mappings = array(
  840. 'id' => 'ID',
  841. 'include' => 'post__in',
  842. 'slug' => 'post_name',
  843. 'include_slugs' => 'post_name__in',
  844. 'menu_order' => 'menu_order',
  845. );
  846. if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
  847. $query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
  848. }
  849. }
  850. return $query_args;
  851. }
  852. /**
  853. * Gets the id of the menu that the given menu item belongs to.
  854. *
  855. * @since 5.9.0
  856. *
  857. * @param int $menu_item_id Menu item id.
  858. * @return int
  859. */
  860. protected function get_menu_id( $menu_item_id ) {
  861. $menu_ids = wp_get_post_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
  862. $menu_id = 0;
  863. if ( $menu_ids && ! is_wp_error( $menu_ids ) ) {
  864. $menu_id = array_shift( $menu_ids );
  865. }
  866. return $menu_id;
  867. }
  868. }