Geen omschrijving

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. $response->add_links( $this->prepare_links( $term ) );
  117. /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
  118. return apply_filters( "rest_prepare_{$this->taxonomy}", $response, $term, $request );
  119. }
  120. /**
  121. * Prepares links for the request.
  122. *
  123. * @since 5.9.0
  124. *
  125. * @param WP_Term $term Term object.
  126. * @return array Links for the given term.
  127. */
  128. protected function prepare_links( $term ) {
  129. $links = parent::prepare_links( $term );
  130. $locations = $this->get_menu_locations( $term->term_id );
  131. foreach ( $locations as $location ) {
  132. $url = rest_url( sprintf( 'wp/v2/menu-locations/%s', $location ) );
  133. $links['https://api.w.org/menu-location'][] = array(
  134. 'href' => $url,
  135. 'embeddable' => true,
  136. );
  137. }
  138. return $links;
  139. }
  140. /**
  141. * Prepares a single term for create or update.
  142. *
  143. * @since 5.9.0
  144. *
  145. * @param WP_REST_Request $request Request object.
  146. * @return object Prepared term data.
  147. */
  148. public function prepare_item_for_database( $request ) {
  149. $prepared_term = parent::prepare_item_for_database( $request );
  150. $schema = $this->get_item_schema();
  151. if ( isset( $request['name'] ) && ! empty( $schema['properties']['name'] ) ) {
  152. $prepared_term->{'menu-name'} = $request['name'];
  153. }
  154. return $prepared_term;
  155. }
  156. /**
  157. * Creates a single term in a taxonomy.
  158. *
  159. * @since 5.9.0
  160. *
  161. * @param WP_REST_Request $request Full details about the request.
  162. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  163. */
  164. public function create_item( $request ) {
  165. if ( isset( $request['parent'] ) ) {
  166. if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) {
  167. return new WP_Error( 'rest_taxonomy_not_hierarchical', __( 'Cannot set parent term, taxonomy is not hierarchical.' ), array( 'status' => 400 ) );
  168. }
  169. $parent = wp_get_nav_menu_object( (int) $request['parent'] );
  170. if ( ! $parent ) {
  171. return new WP_Error( 'rest_term_invalid', __( 'Parent term does not exist.' ), array( 'status' => 400 ) );
  172. }
  173. }
  174. $prepared_term = $this->prepare_item_for_database( $request );
  175. $term = wp_update_nav_menu_object( 0, wp_slash( (array) $prepared_term ) );
  176. if ( is_wp_error( $term ) ) {
  177. /*
  178. * If we're going to inform the client that the term already exists,
  179. * give them the identifier for future use.
  180. */
  181. if ( in_array( 'menu_exists', $term->get_error_codes(), true ) ) {
  182. $existing_term = get_term_by( 'name', $prepared_term->{'menu-name'}, $this->taxonomy );
  183. $term->add_data( $existing_term->term_id, 'menu_exists' );
  184. $term->add_data(
  185. array(
  186. 'status' => 400,
  187. 'term_id' => $existing_term->term_id,
  188. )
  189. );
  190. } else {
  191. $term->add_data( array( 'status' => 400 ) );
  192. }
  193. return $term;
  194. }
  195. $term = $this->get_term( $term );
  196. /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
  197. do_action( "rest_insert_{$this->taxonomy}", $term, $request, true );
  198. $schema = $this->get_item_schema();
  199. if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
  200. $meta_update = $this->meta->update_value( $request['meta'], $term->term_id );
  201. if ( is_wp_error( $meta_update ) ) {
  202. return $meta_update;
  203. }
  204. }
  205. $locations_update = $this->handle_locations( $term->term_id, $request );
  206. if ( is_wp_error( $locations_update ) ) {
  207. return $locations_update;
  208. }
  209. $this->handle_auto_add( $term->term_id, $request );
  210. $fields_update = $this->update_additional_fields_for_object( $term, $request );
  211. if ( is_wp_error( $fields_update ) ) {
  212. return $fields_update;
  213. }
  214. $request->set_param( 'context', 'view' );
  215. /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
  216. do_action( "rest_after_insert_{$this->taxonomy}", $term, $request, true );
  217. $response = $this->prepare_item_for_response( $term, $request );
  218. $response = rest_ensure_response( $response );
  219. $response->set_status( 201 );
  220. $response->header( 'Location', rest_url( $this->namespace . '/' . $this->rest_base . '/' . $term->term_id ) );
  221. return $response;
  222. }
  223. /**
  224. * Updates a single term from a taxonomy.
  225. *
  226. * @since 5.9.0
  227. *
  228. * @param WP_REST_Request $request Full details about the request.
  229. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  230. */
  231. public function update_item( $request ) {
  232. $term = $this->get_term( $request['id'] );
  233. if ( is_wp_error( $term ) ) {
  234. return $term;
  235. }
  236. if ( isset( $request['parent'] ) ) {
  237. if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) {
  238. return new WP_Error( 'rest_taxonomy_not_hierarchical', __( 'Cannot set parent term, taxonomy is not hierarchical.' ), array( 'status' => 400 ) );
  239. }
  240. $parent = get_term( (int) $request['parent'], $this->taxonomy );
  241. if ( ! $parent ) {
  242. return new WP_Error( 'rest_term_invalid', __( 'Parent term does not exist.' ), array( 'status' => 400 ) );
  243. }
  244. }
  245. $prepared_term = $this->prepare_item_for_database( $request );
  246. // Only update the term if we have something to update.
  247. if ( ! empty( $prepared_term ) ) {
  248. if ( ! isset( $prepared_term->{'menu-name'} ) ) {
  249. // wp_update_nav_menu_object() requires that the menu-name is always passed.
  250. $prepared_term->{'menu-name'} = $term->name;
  251. }
  252. $update = wp_update_nav_menu_object( $term->term_id, wp_slash( (array) $prepared_term ) );
  253. if ( is_wp_error( $update ) ) {
  254. return $update;
  255. }
  256. }
  257. $term = get_term( $term->term_id, $this->taxonomy );
  258. /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
  259. do_action( "rest_insert_{$this->taxonomy}", $term, $request, false );
  260. $schema = $this->get_item_schema();
  261. if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
  262. $meta_update = $this->meta->update_value( $request['meta'], $term->term_id );
  263. if ( is_wp_error( $meta_update ) ) {
  264. return $meta_update;
  265. }
  266. }
  267. $locations_update = $this->handle_locations( $term->term_id, $request );
  268. if ( is_wp_error( $locations_update ) ) {
  269. return $locations_update;
  270. }
  271. $this->handle_auto_add( $term->term_id, $request );
  272. $fields_update = $this->update_additional_fields_for_object( $term, $request );
  273. if ( is_wp_error( $fields_update ) ) {
  274. return $fields_update;
  275. }
  276. $request->set_param( 'context', 'view' );
  277. /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
  278. do_action( "rest_after_insert_{$this->taxonomy}", $term, $request, false );
  279. $response = $this->prepare_item_for_response( $term, $request );
  280. return rest_ensure_response( $response );
  281. }
  282. /**
  283. * Deletes a single term from a taxonomy.
  284. *
  285. * @since 5.9.0
  286. *
  287. * @param WP_REST_Request $request Full details about the request.
  288. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  289. */
  290. public function delete_item( $request ) {
  291. $term = $this->get_term( $request['id'] );
  292. if ( is_wp_error( $term ) ) {
  293. return $term;
  294. }
  295. // We don't support trashing for terms.
  296. if ( ! $request['force'] ) {
  297. /* translators: %s: force=true */
  298. return new WP_Error( 'rest_trash_not_supported', sprintf( __( "Menus do not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) );
  299. }
  300. $request->set_param( 'context', 'view' );
  301. $previous = $this->prepare_item_for_response( $term, $request );
  302. $result = wp_delete_nav_menu( $term );
  303. if ( ! $result || is_wp_error( $result ) ) {
  304. return new WP_Error( 'rest_cannot_delete', __( 'The menu cannot be deleted.' ), array( 'status' => 500 ) );
  305. }
  306. $response = new WP_REST_Response();
  307. $response->set_data(
  308. array(
  309. 'deleted' => true,
  310. 'previous' => $previous->get_data(),
  311. )
  312. );
  313. /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
  314. do_action( "rest_delete_{$this->taxonomy}", $term, $response, $request );
  315. return $response;
  316. }
  317. /**
  318. * Returns the value of a menu's auto_add setting.
  319. *
  320. * @since 5.9.0
  321. *
  322. * @param int $menu_id The menu id to query.
  323. * @return bool The value of auto_add.
  324. */
  325. protected function get_menu_auto_add( $menu_id ) {
  326. $nav_menu_option = (array) get_option( 'nav_menu_options', array( 'auto_add' => array() ) );
  327. return in_array( $menu_id, $nav_menu_option['auto_add'], true );
  328. }
  329. /**
  330. * Updates the menu's auto add from a REST request.
  331. *
  332. * @since 5.9.0
  333. *
  334. * @param int $menu_id The menu id to update.
  335. * @param WP_REST_Request $request Full details about the request.
  336. * @return bool True if the auto add setting was successfully updated.
  337. */
  338. protected function handle_auto_add( $menu_id, $request ) {
  339. if ( ! isset( $request['auto_add'] ) ) {
  340. return true;
  341. }
  342. $nav_menu_option = (array) get_option( 'nav_menu_options', array( 'auto_add' => array() ) );
  343. if ( ! isset( $nav_menu_option['auto_add'] ) ) {
  344. $nav_menu_option['auto_add'] = array();
  345. }
  346. $auto_add = $request['auto_add'];
  347. $i = array_search( $menu_id, $nav_menu_option['auto_add'], true );
  348. if ( $auto_add && false === $i ) {
  349. $nav_menu_option['auto_add'][] = $menu_id;
  350. } elseif ( ! $auto_add && false !== $i ) {
  351. array_splice( $nav_menu_option['auto_add'], $i, 1 );
  352. }
  353. $update = update_option( 'nav_menu_options', $nav_menu_option );
  354. /** This action is documented in wp-includes/nav-menu.php */
  355. do_action( 'wp_update_nav_menu', $menu_id );
  356. return $update;
  357. }
  358. /**
  359. * Returns the names of the locations assigned to the menu.
  360. *
  361. * @since 5.9.0
  362. *
  363. * @param int $menu_id The menu id.
  364. * @return string[] The locations assigned to the menu.
  365. */
  366. protected function get_menu_locations( $menu_id ) {
  367. $locations = get_nav_menu_locations();
  368. $menu_locations = array();
  369. foreach ( $locations as $location => $assigned_menu_id ) {
  370. if ( $menu_id === $assigned_menu_id ) {
  371. $menu_locations[] = $location;
  372. }
  373. }
  374. return $menu_locations;
  375. }
  376. /**
  377. * Updates the menu's locations from a REST request.
  378. *
  379. * @since 5.9.0
  380. *
  381. * @param int $menu_id The menu id to update.
  382. * @param WP_REST_Request $request Full details about the request.
  383. * @return true|WP_Error True on success, a WP_Error on an error updating any of the locations.
  384. */
  385. protected function handle_locations( $menu_id, $request ) {
  386. if ( ! isset( $request['locations'] ) ) {
  387. return true;
  388. }
  389. $menu_locations = get_registered_nav_menus();
  390. $menu_locations = array_keys( $menu_locations );
  391. $new_locations = array();
  392. foreach ( $request['locations'] as $location ) {
  393. if ( ! in_array( $location, $menu_locations, true ) ) {
  394. return new WP_Error(
  395. 'rest_invalid_menu_location',
  396. __( 'Invalid menu location.' ),
  397. array(
  398. 'status' => 400,
  399. 'location' => $location,
  400. )
  401. );
  402. }
  403. $new_locations[ $location ] = $menu_id;
  404. }
  405. $assigned_menu = get_nav_menu_locations();
  406. foreach ( $assigned_menu as $location => $term_id ) {
  407. if ( $term_id === $menu_id ) {
  408. unset( $assigned_menu[ $location ] );
  409. }
  410. }
  411. $new_assignments = array_merge( $assigned_menu, $new_locations );
  412. set_theme_mod( 'nav_menu_locations', $new_assignments );
  413. return true;
  414. }
  415. /**
  416. * Retrieves the term's schema, conforming to JSON Schema.
  417. *
  418. * @since 5.9.0
  419. *
  420. * @return array Item schema data.
  421. */
  422. public function get_item_schema() {
  423. $schema = parent::get_item_schema();
  424. unset( $schema['properties']['count'], $schema['properties']['link'], $schema['properties']['taxonomy'] );
  425. $schema['properties']['locations'] = array(
  426. 'description' => __( 'The locations assigned to the menu.' ),
  427. 'type' => 'array',
  428. 'items' => array(
  429. 'type' => 'string',
  430. ),
  431. 'context' => array( 'view', 'edit' ),
  432. 'arg_options' => array(
  433. 'validate_callback' => function ( $locations, $request, $param ) {
  434. $valid = rest_validate_request_arg( $locations, $request, $param );
  435. if ( true !== $valid ) {
  436. return $valid;
  437. }
  438. $locations = rest_sanitize_request_arg( $locations, $request, $param );
  439. foreach ( $locations as $location ) {
  440. if ( ! array_key_exists( $location, get_registered_nav_menus() ) ) {
  441. return new WP_Error(
  442. 'rest_invalid_menu_location',
  443. __( 'Invalid menu location.' ),
  444. array(
  445. 'location' => $location,
  446. )
  447. );
  448. }
  449. }
  450. return true;
  451. },
  452. ),
  453. );
  454. $schema['properties']['auto_add'] = array(
  455. 'description' => __( 'Whether to automatically add top level pages to this menu.' ),
  456. 'context' => array( 'view', 'edit' ),
  457. 'type' => 'boolean',
  458. );
  459. return $schema;
  460. }
  461. }