Aucune description

class-wp-rest-sidebars-controller.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. /**
  3. * REST API: WP_REST_Sidebars_Controller class
  4. *
  5. * Original code from {@link https://github.com/martin-pettersson/wp-rest-api-sidebars Martin Pettersson (martin_pettersson@outlook.com)}.
  6. *
  7. * @package WordPress
  8. * @subpackage REST_API
  9. * @since 5.8.0
  10. */
  11. /**
  12. * Core class used to manage a site's sidebars.
  13. *
  14. * @since 5.8.0
  15. *
  16. * @see WP_REST_Controller
  17. */
  18. class WP_REST_Sidebars_Controller extends WP_REST_Controller {
  19. /**
  20. * Sidebars controller constructor.
  21. *
  22. * @since 5.8.0
  23. */
  24. public function __construct() {
  25. $this->namespace = 'wp/v2';
  26. $this->rest_base = 'sidebars';
  27. }
  28. /**
  29. * Registers the controllers routes.
  30. *
  31. * @since 5.8.0
  32. */
  33. public function register_routes() {
  34. register_rest_route(
  35. $this->namespace,
  36. '/' . $this->rest_base,
  37. array(
  38. array(
  39. 'methods' => WP_REST_Server::READABLE,
  40. 'callback' => array( $this, 'get_items' ),
  41. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  42. 'args' => array(
  43. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  44. ),
  45. ),
  46. 'schema' => array( $this, 'get_public_item_schema' ),
  47. )
  48. );
  49. register_rest_route(
  50. $this->namespace,
  51. '/' . $this->rest_base . '/(?P<id>[\w-]+)',
  52. array(
  53. array(
  54. 'methods' => WP_REST_Server::READABLE,
  55. 'callback' => array( $this, 'get_item' ),
  56. 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  57. 'args' => array(
  58. 'id' => array(
  59. 'description' => __( 'The id of a registered sidebar' ),
  60. 'type' => 'string',
  61. ),
  62. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  63. ),
  64. ),
  65. array(
  66. 'methods' => WP_REST_Server::EDITABLE,
  67. 'callback' => array( $this, 'update_item' ),
  68. 'permission_callback' => array( $this, 'update_item_permissions_check' ),
  69. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
  70. ),
  71. 'schema' => array( $this, 'get_public_item_schema' ),
  72. )
  73. );
  74. }
  75. /**
  76. * Checks if a given request has access to get sidebars.
  77. *
  78. * @since 5.8.0
  79. *
  80. * @param WP_REST_Request $request Full details about the request.
  81. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  82. */
  83. public function get_items_permissions_check( $request ) {
  84. return $this->do_permissions_check();
  85. }
  86. /**
  87. * Retrieves the list of sidebars (active or inactive).
  88. *
  89. * @since 5.8.0
  90. *
  91. * @param WP_REST_Request $request Full details about the request.
  92. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  93. */
  94. public function get_items( $request ) {
  95. retrieve_widgets();
  96. $data = array();
  97. foreach ( wp_get_sidebars_widgets() as $id => $widgets ) {
  98. $sidebar = $this->get_sidebar( $id );
  99. if ( ! $sidebar ) {
  100. continue;
  101. }
  102. $data[] = $this->prepare_response_for_collection(
  103. $this->prepare_item_for_response( $sidebar, $request )
  104. );
  105. }
  106. return rest_ensure_response( $data );
  107. }
  108. /**
  109. * Checks if a given request has access to get a single sidebar.
  110. *
  111. * @since 5.8.0
  112. *
  113. * @param WP_REST_Request $request Full details about the request.
  114. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  115. */
  116. public function get_item_permissions_check( $request ) {
  117. return $this->do_permissions_check();
  118. }
  119. /**
  120. * Retrieves one sidebar from the collection.
  121. *
  122. * @since 5.8.0
  123. *
  124. * @param WP_REST_Request $request Full details about the request.
  125. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  126. */
  127. public function get_item( $request ) {
  128. retrieve_widgets();
  129. $sidebar = $this->get_sidebar( $request['id'] );
  130. if ( ! $sidebar ) {
  131. return new WP_Error( 'rest_sidebar_not_found', __( 'No sidebar exists with that id.' ), array( 'status' => 404 ) );
  132. }
  133. return $this->prepare_item_for_response( $sidebar, $request );
  134. }
  135. /**
  136. * Checks if a given request has access to update sidebars.
  137. *
  138. * @since 5.8.0
  139. *
  140. * @param WP_REST_Request $request Full details about the request.
  141. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  142. */
  143. public function update_item_permissions_check( $request ) {
  144. return $this->do_permissions_check();
  145. }
  146. /**
  147. * Updates a sidebar.
  148. *
  149. * @since 5.8.0
  150. *
  151. * @param WP_REST_Request $request Full details about the request.
  152. * @return WP_REST_Response Response object on success, or WP_Error object on failure.
  153. */
  154. public function update_item( $request ) {
  155. if ( isset( $request['widgets'] ) ) {
  156. $sidebars = wp_get_sidebars_widgets();
  157. foreach ( $sidebars as $sidebar_id => $widgets ) {
  158. foreach ( $widgets as $i => $widget_id ) {
  159. // This automatically removes the passed widget ids from any other sidebars in use.
  160. if ( $sidebar_id !== $request['id'] && in_array( $widget_id, $request['widgets'], true ) ) {
  161. unset( $sidebars[ $sidebar_id ][ $i ] );
  162. }
  163. // This automatically removes omitted widget ids to the inactive sidebar.
  164. if ( $sidebar_id === $request['id'] && ! in_array( $widget_id, $request['widgets'], true ) ) {
  165. $sidebars['wp_inactive_widgets'][] = $widget_id;
  166. }
  167. }
  168. }
  169. $sidebars[ $request['id'] ] = $request['widgets'];
  170. wp_set_sidebars_widgets( $sidebars );
  171. }
  172. $request['context'] = 'edit';
  173. $sidebar = $this->get_sidebar( $request['id'] );
  174. /**
  175. * Fires after a sidebar is updated via the REST API.
  176. *
  177. * @since 5.8.0
  178. *
  179. * @param array $sidebar The updated sidebar.
  180. * @param WP_REST_Request $request Request object.
  181. */
  182. do_action( 'rest_save_sidebar', $sidebar, $request );
  183. return $this->prepare_item_for_response( $sidebar, $request );
  184. }
  185. /**
  186. * Checks if the user has permissions to make the request.
  187. *
  188. * @since 5.8.0
  189. *
  190. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  191. */
  192. protected function do_permissions_check() {
  193. // Verify if the current user has edit_theme_options capability.
  194. // This capability is required to access the widgets screen.
  195. if ( ! current_user_can( 'edit_theme_options' ) ) {
  196. return new WP_Error(
  197. 'rest_cannot_manage_widgets',
  198. __( 'Sorry, you are not allowed to manage widgets on this site.' ),
  199. array( 'status' => rest_authorization_required_code() )
  200. );
  201. }
  202. return true;
  203. }
  204. /**
  205. * Retrieves the registered sidebar with the given id.
  206. *
  207. * @since 5.8.0
  208. *
  209. * @global array $wp_registered_sidebars The registered sidebars.
  210. *
  211. * @param string|int $id ID of the sidebar.
  212. * @return array|null The discovered sidebar, or null if it is not registered.
  213. */
  214. protected function get_sidebar( $id ) {
  215. global $wp_registered_sidebars;
  216. foreach ( (array) $wp_registered_sidebars as $sidebar ) {
  217. if ( $sidebar['id'] === $id ) {
  218. return $sidebar;
  219. }
  220. }
  221. if ( 'wp_inactive_widgets' === $id ) {
  222. return array(
  223. 'id' => 'wp_inactive_widgets',
  224. 'name' => __( 'Inactive widgets' ),
  225. );
  226. }
  227. return null;
  228. }
  229. /**
  230. * Prepares a single sidebar output for response.
  231. *
  232. * @since 5.8.0
  233. *
  234. * @global array $wp_registered_sidebars The registered sidebars.
  235. * @global array $wp_registered_widgets The registered widgets.
  236. *
  237. * @param array $raw_sidebar Sidebar instance.
  238. * @param WP_REST_Request $request Full details about the request.
  239. * @return WP_REST_Response Prepared response object.
  240. */
  241. public function prepare_item_for_response( $raw_sidebar, $request ) {
  242. global $wp_registered_sidebars, $wp_registered_widgets;
  243. $id = $raw_sidebar['id'];
  244. $sidebar = array( 'id' => $id );
  245. if ( isset( $wp_registered_sidebars[ $id ] ) ) {
  246. $registered_sidebar = $wp_registered_sidebars[ $id ];
  247. $sidebar['status'] = 'active';
  248. $sidebar['name'] = isset( $registered_sidebar['name'] ) ? $registered_sidebar['name'] : '';
  249. $sidebar['description'] = isset( $registered_sidebar['description'] ) ? wp_sidebar_description( $id ) : '';
  250. $sidebar['class'] = isset( $registered_sidebar['class'] ) ? $registered_sidebar['class'] : '';
  251. $sidebar['before_widget'] = isset( $registered_sidebar['before_widget'] ) ? $registered_sidebar['before_widget'] : '';
  252. $sidebar['after_widget'] = isset( $registered_sidebar['after_widget'] ) ? $registered_sidebar['after_widget'] : '';
  253. $sidebar['before_title'] = isset( $registered_sidebar['before_title'] ) ? $registered_sidebar['before_title'] : '';
  254. $sidebar['after_title'] = isset( $registered_sidebar['after_title'] ) ? $registered_sidebar['after_title'] : '';
  255. } else {
  256. $sidebar['status'] = 'inactive';
  257. $sidebar['name'] = $raw_sidebar['name'];
  258. $sidebar['description'] = '';
  259. $sidebar['class'] = '';
  260. }
  261. $fields = $this->get_fields_for_response( $request );
  262. if ( rest_is_field_included( 'widgets', $fields ) ) {
  263. $sidebars = wp_get_sidebars_widgets();
  264. $widgets = array_filter(
  265. isset( $sidebars[ $sidebar['id'] ] ) ? $sidebars[ $sidebar['id'] ] : array(),
  266. static function ( $widget_id ) use ( $wp_registered_widgets ) {
  267. return isset( $wp_registered_widgets[ $widget_id ] );
  268. }
  269. );
  270. $sidebar['widgets'] = array_values( $widgets );
  271. }
  272. $schema = $this->get_item_schema();
  273. $data = array();
  274. foreach ( $schema['properties'] as $property_id => $property ) {
  275. if ( isset( $sidebar[ $property_id ] ) && true === rest_validate_value_from_schema( $sidebar[ $property_id ], $property ) ) {
  276. $data[ $property_id ] = $sidebar[ $property_id ];
  277. } elseif ( isset( $property['default'] ) ) {
  278. $data[ $property_id ] = $property['default'];
  279. }
  280. }
  281. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  282. $data = $this->add_additional_fields_to_object( $data, $request );
  283. $data = $this->filter_response_by_context( $data, $context );
  284. $response = rest_ensure_response( $data );
  285. $response->add_links( $this->prepare_links( $sidebar ) );
  286. /**
  287. * Filters the REST API response for a sidebar.
  288. *
  289. * @since 5.8.0
  290. *
  291. * @param WP_REST_Response $response The response object.
  292. * @param array $raw_sidebar The raw sidebar data.
  293. * @param WP_REST_Request $request The request object.
  294. */
  295. return apply_filters( 'rest_prepare_sidebar', $response, $raw_sidebar, $request );
  296. }
  297. /**
  298. * Prepares links for the sidebar.
  299. *
  300. * @since 5.8.0
  301. *
  302. * @param array $sidebar Sidebar.
  303. * @return array Links for the given widget.
  304. */
  305. protected function prepare_links( $sidebar ) {
  306. return array(
  307. 'collection' => array(
  308. 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
  309. ),
  310. 'self' => array(
  311. 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $sidebar['id'] ) ),
  312. ),
  313. 'https://api.w.org/widget' => array(
  314. 'href' => add_query_arg( 'sidebar', $sidebar['id'], rest_url( '/wp/v2/widgets' ) ),
  315. 'embeddable' => true,
  316. ),
  317. );
  318. }
  319. /**
  320. * Retrieves the block type' schema, conforming to JSON Schema.
  321. *
  322. * @since 5.8.0
  323. *
  324. * @return array Item schema data.
  325. */
  326. public function get_item_schema() {
  327. if ( $this->schema ) {
  328. return $this->add_additional_fields_schema( $this->schema );
  329. }
  330. $schema = array(
  331. '$schema' => 'http://json-schema.org/draft-04/schema#',
  332. 'title' => 'sidebar',
  333. 'type' => 'object',
  334. 'properties' => array(
  335. 'id' => array(
  336. 'description' => __( 'ID of sidebar.' ),
  337. 'type' => 'string',
  338. 'context' => array( 'embed', 'view', 'edit' ),
  339. 'readonly' => true,
  340. ),
  341. 'name' => array(
  342. 'description' => __( 'Unique name identifying the sidebar.' ),
  343. 'type' => 'string',
  344. 'context' => array( 'embed', 'view', 'edit' ),
  345. 'readonly' => true,
  346. ),
  347. 'description' => array(
  348. 'description' => __( 'Description of sidebar.' ),
  349. 'type' => 'string',
  350. 'context' => array( 'embed', 'view', 'edit' ),
  351. 'readonly' => true,
  352. ),
  353. 'class' => array(
  354. 'description' => __( 'Extra CSS class to assign to the sidebar in the Widgets interface.' ),
  355. 'type' => 'string',
  356. 'context' => array( 'embed', 'view', 'edit' ),
  357. 'readonly' => true,
  358. ),
  359. 'before_widget' => array(
  360. 'description' => __( 'HTML content to prepend to each widget\'s HTML output when assigned to this sidebar. Default is an opening list item element.' ),
  361. 'type' => 'string',
  362. 'default' => '',
  363. 'context' => array( 'embed', 'view', 'edit' ),
  364. 'readonly' => true,
  365. ),
  366. 'after_widget' => array(
  367. 'description' => __( 'HTML content to append to each widget\'s HTML output when assigned to this sidebar. Default is a closing list item element.' ),
  368. 'type' => 'string',
  369. 'default' => '',
  370. 'context' => array( 'embed', 'view', 'edit' ),
  371. 'readonly' => true,
  372. ),
  373. 'before_title' => array(
  374. 'description' => __( 'HTML content to prepend to the sidebar title when displayed. Default is an opening h2 element.' ),
  375. 'type' => 'string',
  376. 'default' => '',
  377. 'context' => array( 'embed', 'view', 'edit' ),
  378. 'readonly' => true,
  379. ),
  380. 'after_title' => array(
  381. 'description' => __( 'HTML content to append to the sidebar title when displayed. Default is a closing h2 element.' ),
  382. 'type' => 'string',
  383. 'default' => '',
  384. 'context' => array( 'embed', 'view', 'edit' ),
  385. 'readonly' => true,
  386. ),
  387. 'status' => array(
  388. 'description' => __( 'Status of sidebar.' ),
  389. 'type' => 'string',
  390. 'enum' => array( 'active', 'inactive' ),
  391. 'context' => array( 'embed', 'view', 'edit' ),
  392. 'readonly' => true,
  393. ),
  394. 'widgets' => array(
  395. 'description' => __( 'Nested widgets.' ),
  396. 'type' => 'array',
  397. 'items' => array(
  398. 'type' => array( 'object', 'string' ),
  399. ),
  400. 'default' => array(),
  401. 'context' => array( 'embed', 'view', 'edit' ),
  402. ),
  403. ),
  404. );
  405. $this->schema = $schema;
  406. return $this->add_additional_fields_schema( $this->schema );
  407. }
  408. }