PluginProbe
Gutenberg / 19.6.1
Gutenberg v19.6.1
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.7 / rest-api.php

rest-api.php in Gutenberg 19.6.1, at lib/compat/wordpress-6.7/rest-api.php

159 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PHP and WordPress configuration compatibility functions for the Gutenberg
4 * editor plugin changes related to REST API.
5 *
6 * @package gutenberg
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 die( 'Silence is golden.' );
11 }
12
13 /**
14 * Update the preload paths registered in Core (`site-editor.php` or `edit-form-blocks.php`).
15 *
16 * @param array $paths REST API paths to preload.
17 * @param WP_Block_Editor_Context $context Current block editor context.
18 * @return array Filtered preload paths.
19 */
20 function gutenberg_block_editor_preload_paths_6_7( $paths, $context ) {
21 if ( 'core/edit-site' === $context->name ) {
22 // Fixes post type name. It should be `type/wp_template_part`.
23 $parts_key = array_search( '/wp/v2/types/wp_template-part?context=edit', $paths, true );
24 if ( false !== $parts_key ) {
25 $paths[ $parts_key ] = '/wp/v2/types/wp_template_part?context=edit';
26 }
27
28 $page_options_path = array( rest_get_route_for_post_type_items( 'page' ), 'OPTIONS' );
29 $page_options_key = array_search( $page_options_path, $paths, true );
30 if ( false === $page_options_key ) {
31 $paths[] = $page_options_path;
32 }
33 }
34
35 if ( 'core/edit-post' === $context->name ) {
36 $reusable_blocks_key = array_search(
37 add_query_arg(
38 array(
39 'context' => 'edit',
40 'per_page' => -1,
41 ),
42 rest_get_route_for_post_type_items( 'wp_block' )
43 ),
44 $paths,
45 true
46 );
47
48 if ( false !== $reusable_blocks_key ) {
49 unset( $paths[ $reusable_blocks_key ] );
50 }
51 }
52
53 // Preload theme and global styles paths.
54 $excluded_paths = array();
55 if ( 'core/edit-site' === $context->name || 'core/edit-post' === $context->name ) {
56 $active_theme = get_stylesheet();
57 $global_styles_id = WP_Theme_JSON_Resolver_Gutenberg::get_user_global_styles_post_id();
58 $paths[] = '/wp/v2/global-styles/themes/' . $active_theme . '?context=view';
59 $paths[] = '/wp/v2/global-styles/themes/' . $active_theme . '/variations?context=view';
60 $paths[] = array( '/wp/v2/global-styles/' . $global_styles_id, 'OPTIONS' );
61
62 // Remove duplicate or unnecessary global styles paths.
63 $excluded_paths[] = '/wp/v2/global-styles/themes/' . $active_theme;
64 $excluded_paths[] = '/wp/v2/global-styles/' . $global_styles_id;
65 foreach ( $paths as $key => $path ) {
66 if ( in_array( $path, $excluded_paths, true ) ) {
67 unset( $paths[ $key ] );
68 }
69 }
70 }
71
72 return $paths;
73 }
74 add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_6_7', 10, 2 );
75
76 if ( ! function_exists( 'wp_api_template_registry' ) ) {
77 /**
78 * Hook in to the template and template part post types and modify the rest
79 * endpoint to include modifications to read templates from the
80 * BlockTemplatesRegistry.
81 *
82 * @param array $args Current registered post type args.
83 * @param string $post_type Name of post type.
84 *
85 * @return array
86 */
87 function wp_api_template_registry( $args, $post_type ) {
88 if ( 'wp_template' === $post_type || 'wp_template_part' === $post_type ) {
89 $args['rest_controller_class'] = 'Gutenberg_REST_Templates_Controller_6_7';
90 }
91 return $args;
92 }
93 }
94 add_filter( 'register_post_type_args', 'wp_api_template_registry', 10, 2 );
95
96 /**
97 * Adds `plugin` fields to WP_REST_Templates_Controller class.
98 */
99 function gutenberg_register_wp_rest_templates_controller_plugin_field() {
100
101 register_rest_field(
102 'wp_template',
103 'plugin',
104 array(
105 'get_callback' => function ( $template_object ) {
106 if ( $template_object ) {
107 $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template_object['slug'] );
108 if ( $registered_template ) {
109 return $registered_template->plugin;
110 }
111 }
112
113 return;
114 },
115 'update_callback' => null,
116 'schema' => array(
117 'type' => 'string',
118 'description' => __( 'Plugin that registered the template.', 'gutenberg' ),
119 'readonly' => true,
120 'context' => array( 'view', 'edit', 'embed' ),
121 ),
122 )
123 );
124 }
125 add_action( 'rest_api_init', 'gutenberg_register_wp_rest_templates_controller_plugin_field' );
126
127 /**
128 * Overrides the default 'WP_REST_Server' class.
129 *
130 * @return string The name of the custom server class.
131 */
132 function gutenberg_override_default_rest_server() {
133 return 'Gutenberg_REST_Server';
134 }
135 add_filter( 'wp_rest_server_class', 'gutenberg_override_default_rest_server', 1 );
136
137
138 /**
139 * Filters the arguments for registering a wp_global_styles post type.
140 * Note when syncing to Core: the capabilities should be updates for `wp_global_styles` in the wp-includes/post.php.
141 *
142 * @since 6.7.0
143 *
144 * @param array $args Array of arguments for registering a post type.
145 * See the register_post_type() function for accepted arguments.
146 * @param string $post_type Post type key.
147 *
148 * @return array Array of arguments for registering a post type.
149 */
150 function gutenberg_register_post_type_args_for_wp_global_styles( $args, $post_type ) {
151 if ( 'wp_global_styles' === $post_type ) {
152 $args['capabilities']['read'] = 'edit_posts';
153 }
154
155 return $args;
156 }
157
158 add_filter( 'register_post_type_args', 'gutenberg_register_post_type_args_for_wp_global_styles', 10, 2 );
159