class-wp-rest-menus-controller.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. <?php
  2. /**
  3. * REST API: WP_REST_Menus_Controller class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 5.9.0
  8. */
  9. /**
  10. * Core class used to managed menu terms associated via the REST API.
  11. *
  12. * @since 5.9.0
  13. *
  14. * @see WP_REST_Controller
  15. */
  16. class WP_REST_Menus_Controller extends WP_REST_Terms_Controller {
  17. /**
  18. * Checks if a request has access to read menus.
  19. *
  20. * @since 5.9.0
  21. *
  22. * @param WP_REST_Request $request Full details about the request.
  23. * @return bool|WP_Error True if the request has read access, otherwise false or WP_Error object.
  24. */
  25. public function get_items_permissions_check( $request ) {
  26. $has_permission = parent::get_items_permissions_check( $request );
  27. if ( true !== $has_permission ) {
  28. return $has_permission;
  29. }
  30. return $this->check_has_read_only_access( $request );
  31. }
  32. /**
  33. * Checks if a request has access to read or edit the specified menu.
  34. *
  35. * @since 5.9.0
  36. *
  37. * @param WP_REST_Request $request Full details about the request.
  38. * @return bool|WP_Error True if the request has read access for the item, otherwise false or WP_Error object.
  39. */
  40. public function get_item_permissions_check( $request ) {
  41. $has_permission = parent::get_item_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. * Gets the term, if the ID is valid.
  49. *
  50. * @since 5.9.0
  51. *
  52. * @param int $id Supplied ID.
  53. * @return WP_Term|WP_Error Term object if ID is valid, WP_Error otherwise.
  54. */
  55. protected function get_term( $id ) {
  56. $term = parent::get_term( $id );
  57. if ( is_wp_error( $term ) ) {
  58. return $term;
  59. }
  60. $nav_term = wp_get_nav_menu_object( $term );
  61. $nav_term->auto_add = $this->get_menu_auto_add( $nav_term->term_id );
  62. return $nav_term;
  63. }
  64. /**
  65. * Checks whether the current user has read permission for the endpoint.
  66. *
  67. * This allows for any user that can `edit_theme_options` or edit any REST API available post type.
  68. *
  69. * @since 5.9.0
  70. *
  71. * @param WP_REST_Request $request Full details about the request.
  72. * @return bool|WP_Error Whether the current user has permission.
  73. */
  74. protected function check_has_read_only_access( $request ) {
  75. if ( current_user_can( 'edit_theme_options' ) ) {
  76. return true;
  77. }
  78. if ( current_user_can( 'edit_posts' ) ) {
  79. return true;
  80. }
  81. foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
  82. if ( current_user_can( $post_type->cap->edit_posts ) ) {
  83. return true;
  84. }
  85. }
  86. return new WP_Error(
  87. 'rest_cannot_view',
  88. __( 'Sorry, you are not allowed to view menus.' ),
  89. array( 'status' => rest_authorization_required_code() )
  90. );
  91. }
  92. /**
  93. * Prepares a single term output for response.
  94. *
  95. * @since 5.9.0
  96. *
  97. * @param WP_Term $term Term object.
  98. * @param WP_REST_Request $request Request object.
  99. * @return WP_REST_Response Response object.
  100. */
  101. public function prepare_item_for_response( $term, $request ) {
  102. $nav_menu = wp_get_nav_menu_object( $term );
  103. $response = parent::prepare_item_for_response( $nav_menu, $request );
  104. $fields = $this->get_fields_for_response( $request );
  105. $data = $response->get_data();
  106. if ( rest_is_field_included( 'locations', $fields ) ) {
  107. $data['locations'] = $this->get_menu_locations( $nav_menu->term_id );
  108. }
  109. if ( rest_is_field_included( 'auto_add', $fields ) ) {
  110. $data['auto_add'] = $this->get_menu_auto_add( $nav_menu->term_id );
  111. }
  112. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  113. $data = $this->add_additional_fields_to_object( $data, $request );
  114. $data = $this->filter_response_by_context( $data, $context );
  115. $response = rest_ensure_response( $data );
  116. if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
  117. $response->add_links( $this->prepare_links( $term ) );
  118. }
  119. /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
  120. return apply_filters( "rest_prepare_{$this->taxonomy}", $response, $term, $request );
  121. }
  122. /**
  123. * Prepares links for the request.
  124. *
  125. * @since 5.9.0
  126. *
  127. * @param WP_Term $term Term object.
  128. * @return array Links for the given term.
  129. */
  130. protected function prepare_links( $term ) {
  131. $links = parent::prepare_links( $term );
  132. $locations = $this->get_menu_locations( $term->term_id );
  133. foreach ( $locations as $location ) {
  134. $url = rest_url( sprintf( 'wp/v2/menu-locations/%s', $location ) );
  135. $links['https://api.w.org/menu-location'][] = array(
  136. 'href' => $url,
  137. 'embeddable' => true,
  138. );
  139. }
  140. return $links;
  141. }
  142. /**
  143. * Prepares a single term for create or update.
  144. *
  145. * @since 5.9.0
  146. *
  147. * @param WP_REST_Request $request Request object.
  148. * @return object Prepared term data.
  149. */
  150. public function prepare_item_for_database( $request ) {
  151. $prepared_term = parent::prepare_item_for_database( $request );
  152. $schema = $this->get_item_schema();
  153. if ( isset( $request['name'] ) && ! empty( $schema['properties']['name'] ) ) {
  154. $prepared_term->{'menu-name'} = $request['name'];
  155. }
  156. return $prepared_term;
  157. }
  158. /**
  159. * Creates a single term in a taxonomy.
  160. *
  161. * @since 5.9.0
  162. *
  163. * @param WP_REST_Request $request Full details about the request.
  164. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  165. */
  166. public function create_item( $request ) {
  167. if ( isset( $request['parent'] ) ) {
  168. if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) {
  169. return new WP_Error( 'rest_taxonomy_not_hierarchical', __( 'Cannot set parent term, taxonomy is not hierarchical.' ), array( 'status' => 400 ) );
  170. }
  171. $parent = wp_get_nav_menu_object( (int) $request['parent'] );
  172. if ( ! $parent ) {
  173. return new WP_Error( 'rest_term_invalid', __( 'Parent term does not exist.' ), array( 'status' => 400 ) );
  174. }
  175. }
  176. $prepared_term = $this->prepare_item_for_database( $request );
  177. $term = wp_update_nav_menu_object( 0, wp_slash( (array) $prepared_term ) );
  178. if ( is_wp_error( $term ) ) {
  179. /*
  180. * If we're going to inform the client that the term already exists,
  181. * give them the identifier for future use.
  182. */
  183. if ( in_array( 'menu_exists', $term->get_error_codes(), true ) ) {
  184. $existing_term = get_term_by( 'name', $prepared_term->{'menu-name'}, $this->taxonomy );
  185. $term->add_data( $existing_term->term_id, 'menu_exists' );
  186. $term->add_data(
  187. array(
  188. 'status' => 400,
  189. 'term_id' => $existing_term->term_id,
  190. )
  191. );
  192. } else {
  193. $term->add_data( array( 'status' => 400 ) );
  194. }
  195. return $term;
  196. }
  197. $term = $this->get_term( $term );
  198. /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
  199. do_action( "rest_insert_{$this->taxonomy}", $term, $request, true );
  200. $schema = $this->get_item_schema();
  201. if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
  202. $meta_update = $this->meta->update_value( $request['meta'], $term->term_id );
  203. if ( is_wp_error( $meta_update ) ) {
  204. return $meta_update;
  205. }
  206. }
  207. $locations_update = $this->handle_locations( $term->term_id, $request );
  208. if ( is_wp_error( $locations_update ) ) {
  209. return $locations_update;
  210. }
  211. $this->handle_auto_add( $term->term_id, $request );
  212. $fields_update = $this->update_additional_fields_for_object( $term, $request );
  213. if ( is_wp_error( $fields_update ) ) {
  214. return $fields_update;
  215. }
  216. $request->set_param( 'context', 'view' );
  217. /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
  218. do_action( "rest_after_insert_{$this->taxonomy}", $term, $request, true );
  219. $response = $this->prepare_item_for_response( $term, $request );
  220. $response = rest_ensure_response( $response );
  221. $response->set_status( 201 );
  222. $response->header( 'Location', rest_url( $this->namespace . '/' . $this->rest_base . '/' . $term->term_id ) );
  223. return $response;
  224. }
  225. /**
  226. * Updates a single term from a taxonomy.
  227. *
  228. * @since 5.9.0
  229. *
  230. * @param WP_REST_Request $request Full details about the request.
  231. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  232. */
  233. public function update_item( $request ) {
  234. $term = $this->get_term( $request['id'] );
  235. if ( is_wp_error( $term ) ) {
  236. return $term;
  237. }
  238. if ( isset( $request['parent'] ) ) {
  239. if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) {
  240. return new WP_Error( 'rest_taxonomy_not_hierarchical', __( 'Cannot set parent term, taxonomy is not hierarchical.' ), array( 'status' => 400 ) );
  241. }
  242. $parent = get_term( (int) $request['parent'], $this->taxonomy );
  243. if ( ! $parent ) {
  244. return new WP_Error( 'rest_term_invalid', __( 'Parent term does not exist.' ), array( 'status' => 400 ) );
  245. }
  246. }
  247. $prepared_term = $this->prepare_item_for_database( $request );
  248. // Only update the term if we have something to update.
  249. if ( ! empty( $prepared_term ) ) {
  250. if ( ! isset( $prepared_term->{'menu-name'} ) ) {
  251. // wp_update_nav_menu_object() requires that the menu-name is always passed.
  252. $prepared_term->{'menu-name'} = $term->name;
  253. }
  254. $update = wp_update_nav_menu_object( $term->term_id, wp_slash( (array) $prepared_term ) );
  255. if ( is_wp_error( $update ) ) {
  256. return $update;
  257. }
  258. }
  259. $term = get_term( $term->term_id, $this->taxonomy );
  260. /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
  261. do_action( "rest_insert_{$this->taxonomy}", $term, $request, false );
  262. $schema = $this->get_item_schema();
  263. if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
  264. $meta_update = $this->meta->update_value( $request['meta'], $term->term_id );
  265. if ( is_wp_error( $meta_update ) ) {
  266. return $meta_update;
  267. }
  268. }
  269. $locations_update = $this->handle_locations( $term->term_id, $request );
  270. if ( is_wp_error( $locations_update ) ) {
  271. return $locations_update;
  272. }
  273. $this->handle_auto_add( $term->term_id, $request );
  274. $fields_update = $this->update_additional_fields_for_object( $term, $request );
  275. if ( is_wp_error( $fields_update ) ) {
  276. return $fields_update;
  277. }
  278. $request->set_param( 'context', 'view' );
  279. /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
  280. do_action( "rest_after_insert_{$this->taxonomy}", $term, $request, false );
  281. $response = $this->prepare_item_for_response( $term, $request );
  282. return rest_ensure_response( $response );
  283. }
  284. /**
  285. * Deletes a single term from a taxonomy.
  286. *
  287. * @since 5.9.0
  288. *
  289. * @param WP_REST_Request $request Full details about the request.
  290. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  291. */
  292. public function delete_item( $request ) {
  293. $term = $this->get_term( $request['id'] );
  294. if ( is_wp_error( $term ) ) {
  295. return $term;
  296. }
  297. // We don't support trashing for terms.
  298. if ( ! $request['force'] ) {
  299. /* translators: %s: force=true */
  300. return new WP_Error( 'rest_trash_not_supported', sprintf( __( "Menus do not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) );
  301. }
  302. $request->set_param( 'context', 'view' );
  303. $previous = $this->prepare_item_for_response( $term, $request );
  304. $result = wp_delete_nav_menu( $term );
  305. if ( ! $result || is_wp_error( $result ) ) {
  306. return new WP_Error( 'rest_cannot_delete', __( 'The menu cannot be deleted.' ), array( 'status' => 500 ) );
  307. }
  308. $response = new WP_REST_Response();
  309. $response->set_data(
  310. array(
  311. 'deleted' => true,
  312. 'previous' => $previous->get_data(),
  313. )
  314. );
  315. /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
  316. do_action( "rest_delete_{$this->taxonomy}", $term, $response, $request );
  317. return $response;
  318. }
  319. /**
  320. * Returns the value of a menu's auto_add setting.
  321. *
  322. * @since 5.9.0
  323. *
  324. * @param int $menu_id The menu id to query.
  325. * @return bool The value of auto_add.
  326. */
  327. protected function get_menu_auto_add( $menu_id ) {
  328. $nav_menu_option = (array) get_option( 'nav_menu_options', array( 'auto_add' => array() ) );
  329. return in_array( $menu_id, $nav_menu_option['auto_add'], true );
  330. }
  331. /**
  332. * Updates the menu's auto add from a REST request.
  333. *
  334. * @since 5.9.0
  335. *
  336. * @param int $menu_id The menu id to update.
  337. * @param WP_REST_Request $request Full details about the request.
  338. * @return bool True if the auto add setting was successfully updated.
  339. */
  340. protected function handle_auto_add( $menu_id, $request ) {
  341. if ( ! isset( $request['auto_add'] ) ) {
  342. return true;
  343. }
  344. $nav_menu_option = (array) get_option( 'nav_menu_options', array( 'auto_add' => array() ) );
  345. if ( ! isset( $nav_menu_option['auto_add'] ) ) {
  346. $nav_menu_option['auto_add'] = array();
  347. }
  348. $auto_add = $request['auto_add'];
  349. $i = array_search( $menu_id, $nav_menu_option['auto_add'], true );
  350. if ( $auto_add && false === $i ) {
  351. $nav_menu_option['auto_add'][] = $menu_id;
  352. } elseif ( ! $auto_add && false !== $i ) {
  353. array_splice( $nav_menu_option['auto_add'], $i, 1 );
  354. }
  355. $update = update_option( 'nav_menu_options', $nav_menu_option );
  356. /** This action is documented in wp-includes/nav-menu.php */
  357. do_action( 'wp_update_nav_menu', $menu_id );
  358. return $update;
  359. }
  360. /**
  361. * Returns the names of the locations assigned to the menu.
  362. *
  363. * @since 5.9.0
  364. *
  365. * @param int $menu_id The menu id.
  366. * @return string[] The locations assigned to the menu.
  367. */
  368. protected function get_menu_locations( $menu_id ) {
  369. $locations = get_nav_menu_locations();
  370. $menu_locations = array();
  371. foreach ( $locations as $location => $assigned_menu_id ) {
  372. if ( $menu_id === $assigned_menu_id ) {
  373. $menu_locations[] = $location;
  374. }
  375. }
  376. return $menu_locations;
  377. }
  378. /**
  379. * Updates the menu's locations from a REST request.
  380. *
  381. * @since 5.9.0
  382. *
  383. * @param int $menu_id The menu id to update.
  384. * @param WP_REST_Request $request Full details about the request.
  385. * @return true|WP_Error True on success, a WP_Error on an error updating any of the locations.
  386. */
  387. protected function handle_locations( $menu_id, $request ) {
  388. if ( ! isset( $request['locations'] ) ) {
  389. return true;
  390. }
  391. $menu_locations = get_registered_nav_menus();
  392. $menu_locations = array_keys( $menu_locations );
  393. $new_locations = array();
  394. foreach ( $request['locations'] as $location ) {
  395. if ( ! in_array( $location, $menu_locations, true ) ) {
  396. return new WP_Error(
  397. 'rest_invalid_menu_location',
  398. __( 'Invalid menu location.' ),
  399. array(
  400. 'status' => 400,
  401. 'location' => $location,
  402. )
  403. );
  404. }
  405. $new_locations[ $location ] = $menu_id;
  406. }
  407. $assigned_menu = get_nav_menu_locations();
  408. foreach ( $assigned_menu as $location => $term_id ) {
  409. if ( $term_id === $menu_id ) {
  410. unset( $assigned_menu[ $location ] );
  411. }
  412. }
  413. $new_assignments = array_merge( $assigned_menu, $new_locations );
  414. set_theme_mod( 'nav_menu_locations', $new_assignments );
  415. return true;
  416. }
  417. /**
  418. * Retrieves the term's schema, conforming to JSON Schema.
  419. *
  420. * @since 5.9.0
  421. *
  422. * @return array Item schema data.
  423. */
  424. public function get_item_schema() {
  425. $schema = parent::get_item_schema();
  426. unset( $schema['properties']['count'], $schema['properties']['link'], $schema['properties']['taxonomy'] );
  427. $schema['properties']['locations'] = array(
  428. 'description' => __( 'The locations assigned to the menu.' ),
  429. 'type' => 'array',
  430. 'items' => array(
  431. 'type' => 'string',
  432. ),
  433. 'context' => array( 'view', 'edit' ),
  434. 'arg_options' => array(
  435. 'validate_callback' => function ( $locations, $request, $param ) {
  436. $valid = rest_validate_request_arg( $locations, $request, $param );
  437. if ( true !== $valid ) {
  438. return $valid;
  439. }
  440. $locations = rest_sanitize_request_arg( $locations, $request, $param );
  441. foreach ( $locations as $location ) {
  442. if ( ! array_key_exists( $location, get_registered_nav_menus() ) ) {
  443. return new WP_Error(
  444. 'rest_invalid_menu_location',
  445. __( 'Invalid menu location.' ),
  446. array(
  447. 'location' => $location,
  448. )
  449. );
  450. }
  451. }
  452. return true;
  453. },
  454. ),
  455. );
  456. $schema['properties']['auto_add'] = array(
  457. 'description' => __( 'Whether to automatically add top level pages to this menu.' ),
  458. 'context' => array( 'view', 'edit' ),
  459. 'type' => 'boolean',
  460. );
  461. return $schema;
  462. }
  463. }