PluginProbe
Gutenberg / 14.7.0
Gutenberg v14.7.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / lib / compat / wordpress-6.1 / rest-api.php

rest-api.php in Gutenberg 14.7.0, at lib/compat/wordpress-6.1/rest-api.php

96 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Overrides Core's wp-includes/rest-api.php and registers the new endpoint for WP 6.0.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Update `wp_template` and `wp_template-part` post types to use
10 * Gutenberg's REST controller.
11 *
12 * @param array $args Array of arguments for registering a post type.
13 * @param string $post_type Post type key.
14 */
15 function gutenberg_update_templates_template_parts_rest_controller( $args, $post_type ) {
16 if ( in_array( $post_type, array( 'wp_template', 'wp_template-part' ), true ) ) {
17 $args['rest_controller_class'] = 'Gutenberg_REST_Templates_Controller';
18 }
19 return $args;
20 }
21 add_filter( 'register_post_type_args', 'gutenberg_update_templates_template_parts_rest_controller', 10, 2 );
22
23
24 /**
25 * Add the post type's `icon`(menu_icon) in the response.
26 * When we backport this change we will need to add the
27 * `icon` to WP_REST_Post_Types_Controller schema.
28 *
29 * @param WP_REST_Response $response The response object.
30 * @param WP_Post_Type $post_type The original post type object.
31 */
32 function gutenberg_update_post_types_rest_response( $response, $post_type ) {
33 $response->data['icon'] = $post_type->menu_icon;
34 return $response;
35 }
36 add_filter( 'rest_prepare_post_type', 'gutenberg_update_post_types_rest_response', 10, 2 );
37
38 /**
39 * Registers the block patterns REST API routes.
40 */
41 function gutenberg_register_gutenberg_rest_block_patterns() {
42 $block_patterns = new Gutenberg_REST_Block_Patterns_Controller();
43 $block_patterns->register_routes();
44 }
45 add_action( 'rest_api_init', 'gutenberg_register_gutenberg_rest_block_patterns', 100 );
46
47 /**
48 * Exposes the site logo URL through the WordPress REST API.
49 *
50 * This is used for fetching this information when user has no rights
51 * to update settings.
52 *
53 * Note: Backports into wp-includes/rest-api/class-wp-rest-server.php file.
54 *
55 * @param WP_REST_Response $response REST API response.
56 * @return WP_REST_Response $response REST API response.
57 */
58 function gutenberg_add_site_icon_url_to_index( WP_REST_Response $response ) {
59 $response->data['site_icon_url'] = get_site_icon_url();
60
61 return $response;
62 }
63 add_action( 'rest_index', 'gutenberg_add_site_icon_url_to_index' );
64
65 /**
66 * Returns the has_archive post type field.
67 *
68 * @param array $type The response data.
69 * @param string $field_name The field name. The function handles field has_archive.
70 */
71 function gutenberg_get_post_type_has_archive_field( $type, $field_name ) {
72 if ( ! empty( $type ) && ! empty( $type['slug'] ) && 'has_archive' === $field_name ) {
73 $post_type_object = get_post_type_object( $type['slug'] );
74 return $post_type_object->has_archive;
75 }
76 }
77
78 /**
79 * Registers the has_archive post type REST API field.
80 */
81 function gutenberg_register_has_archive_on_post_types_endpoint() {
82 register_rest_field(
83 'type',
84 'has_archive',
85 array(
86 'get_callback' => 'gutenberg_get_post_type_has_archive_field',
87 'schema' => array(
88 'description' => __( 'If the value is a string, the value will be used as the archive slug. If the value is false the post type has no archive.', 'gutenberg' ),
89 'type' => array( 'string', 'boolean' ),
90 'context' => array( 'view', 'edit' ),
91 ),
92 )
93 );
94 }
95 add_action( 'rest_api_init', 'gutenberg_register_has_archive_on_post_types_endpoint' );
96