17 '<p>' . __( 'Sorry, you are not allowed to edit theme options on this site.' ) . '</p>', |
17 '<p>' . __( 'Sorry, you are not allowed to edit theme options on this site.' ) . '</p>', |
18 403 |
18 403 |
19 ); |
19 ); |
20 } |
20 } |
21 |
21 |
22 $is_template_part = isset( $_GET['postType'] ) && 'wp_template_part' === sanitize_key( $_GET['postType'] ); |
22 /** |
23 $is_template_part_path = isset( $_GET['path'] ) && 'wp_template_partall' === sanitize_key( $_GET['path'] ); |
23 * Maps old site editor urls to the new updated ones. |
24 $is_template_part_editor = $is_template_part || $is_template_part_path; |
24 * |
25 $is_patterns = isset( $_GET['postType'] ) && 'wp_block' === sanitize_key( $_GET['postType'] ); |
25 * @since 6.8.0 |
26 $is_patterns_path = isset( $_GET['path'] ) && 'patterns' === sanitize_key( $_GET['path'] ); |
26 * @access private |
27 $is_patterns_editor = $is_patterns || $is_patterns_path; |
27 * |
28 |
28 * @global string $pagenow The filename of the current screen. |
29 if ( ! wp_is_block_theme() ) { |
29 * |
30 if ( ! current_theme_supports( 'block-template-parts' ) && $is_template_part_editor ) { |
30 * @return string|false The new URL to redirect to, or false if no redirection is needed. |
31 wp_die( __( 'The theme you are currently using is not compatible with the Site Editor.' ) ); |
31 */ |
32 } elseif ( ! $is_patterns_editor && ! $is_template_part_editor ) { |
32 function _wp_get_site_editor_redirection_url() { |
33 wp_die( __( 'The theme you are currently using is not compatible with the Site Editor.' ) ); |
33 global $pagenow; |
34 } |
34 if ( 'site-editor.php' !== $pagenow || isset( $_REQUEST['p'] ) || empty( $_SERVER['QUERY_STRING'] ) ) { |
|
35 return false; |
|
36 } |
|
37 |
|
38 // The following redirects are for the new permalinks in the site editor. |
|
39 if ( isset( $_REQUEST['postType'] ) && 'wp_navigation' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) { |
|
40 return add_query_arg( array( 'p' => '/wp_navigation/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) ); |
|
41 } |
|
42 |
|
43 if ( isset( $_REQUEST['postType'] ) && 'wp_navigation' === $_REQUEST['postType'] && empty( $_REQUEST['postId'] ) ) { |
|
44 return add_query_arg( array( 'p' => '/navigation' ), remove_query_arg( 'postType' ) ); |
|
45 } |
|
46 |
|
47 if ( isset( $_REQUEST['path'] ) && '/wp_global_styles' === $_REQUEST['path'] ) { |
|
48 return add_query_arg( array( 'p' => '/styles' ), remove_query_arg( 'path' ) ); |
|
49 } |
|
50 |
|
51 if ( isset( $_REQUEST['postType'] ) && 'page' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) { |
|
52 return add_query_arg( array( 'p' => '/page' ), remove_query_arg( 'postType' ) ); |
|
53 } |
|
54 |
|
55 if ( isset( $_REQUEST['postType'] ) && 'page' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) { |
|
56 return add_query_arg( array( 'p' => '/page/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) ); |
|
57 } |
|
58 |
|
59 if ( isset( $_REQUEST['postType'] ) && 'wp_template' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) { |
|
60 return add_query_arg( array( 'p' => '/template' ), remove_query_arg( 'postType' ) ); |
|
61 } |
|
62 |
|
63 if ( isset( $_REQUEST['postType'] ) && 'wp_template' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) { |
|
64 return add_query_arg( array( 'p' => '/wp_template/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) ); |
|
65 } |
|
66 |
|
67 if ( isset( $_REQUEST['postType'] ) && 'wp_block' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) { |
|
68 return add_query_arg( array( 'p' => '/pattern' ), remove_query_arg( 'postType' ) ); |
|
69 } |
|
70 |
|
71 if ( isset( $_REQUEST['postType'] ) && 'wp_block' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) { |
|
72 return add_query_arg( array( 'p' => '/wp_block/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) ); |
|
73 } |
|
74 |
|
75 if ( isset( $_REQUEST['postType'] ) && 'wp_template_part' === $_REQUEST['postType'] && ( empty( $_REQUEST['canvas'] ) || empty( $_REQUEST['postId'] ) ) ) { |
|
76 return add_query_arg( array( 'p' => '/pattern' ) ); |
|
77 } |
|
78 |
|
79 if ( isset( $_REQUEST['postType'] ) && 'wp_template_part' === $_REQUEST['postType'] && ! empty( $_REQUEST['postId'] ) ) { |
|
80 return add_query_arg( array( 'p' => '/wp_template_part/' . $_REQUEST['postId'] ), remove_query_arg( array( 'postType', 'postId' ) ) ); |
|
81 } |
|
82 |
|
83 // The following redirects are for backward compatibility with the old site editor URLs. |
|
84 if ( isset( $_REQUEST['path'] ) && '/wp_template_part/all' === $_REQUEST['path'] ) { |
|
85 return add_query_arg( |
|
86 array( |
|
87 'p' => '/pattern', |
|
88 'postType' => 'wp_template_part', |
|
89 ), |
|
90 remove_query_arg( 'path' ) |
|
91 ); |
|
92 } |
|
93 |
|
94 if ( isset( $_REQUEST['path'] ) && '/page' === $_REQUEST['path'] ) { |
|
95 return add_query_arg( array( 'p' => '/page' ), remove_query_arg( 'path' ) ); |
|
96 } |
|
97 |
|
98 if ( isset( $_REQUEST['path'] ) && '/wp_template' === $_REQUEST['path'] ) { |
|
99 return add_query_arg( array( 'p' => '/template' ), remove_query_arg( 'path' ) ); |
|
100 } |
|
101 |
|
102 if ( isset( $_REQUEST['path'] ) && '/patterns' === $_REQUEST['path'] ) { |
|
103 return add_query_arg( array( 'p' => '/pattern' ), remove_query_arg( 'path' ) ); |
|
104 } |
|
105 |
|
106 if ( isset( $_REQUEST['path'] ) && '/navigation' === $_REQUEST['path'] ) { |
|
107 return add_query_arg( array( 'p' => '/navigation' ), remove_query_arg( 'path' ) ); |
|
108 } |
|
109 |
|
110 return add_query_arg( array( 'p' => '/' ) ); |
|
111 } |
|
112 |
|
113 // Redirect to the site editor to the new URLs if needed. |
|
114 $redirection = _wp_get_site_editor_redirection_url(); |
|
115 if ( false !== $redirection ) { |
|
116 wp_safe_redirect( $redirection ); |
|
117 exit; |
35 } |
118 } |
36 |
119 |
37 // Used in the HTML title tag. |
120 // Used in the HTML title tag. |
38 $title = _x( 'Editor', 'site editor title tag' ); |
121 $title = _x( 'Editor', 'site editor title tag' ); |
39 $parent_file = 'themes.php'; |
122 $parent_file = 'themes.php'; |
54 foreach ( get_default_block_template_types() as $slug => $template_type ) { |
137 foreach ( get_default_block_template_types() as $slug => $template_type ) { |
55 $template_type['slug'] = (string) $slug; |
138 $template_type['slug'] = (string) $slug; |
56 $indexed_template_types[] = $template_type; |
139 $indexed_template_types[] = $template_type; |
57 } |
140 } |
58 |
141 |
59 $block_editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) ); |
142 $context_settings = array( 'name' => 'core/edit-site' ); |
|
143 |
|
144 if ( ! empty( $_GET['postId'] ) && is_numeric( $_GET['postId'] ) ) { |
|
145 $context_settings['post'] = get_post( (int) $_GET['postId'] ); |
|
146 } elseif ( isset( $_GET['p'] ) && preg_match( '/^\/page\/(\d+)$/', $_GET['p'], $matches ) ) { |
|
147 $context_settings['post'] = get_post( (int) $matches[1] ); |
|
148 } |
|
149 |
|
150 $block_editor_context = new WP_Block_Editor_Context( $context_settings ); |
60 $custom_settings = array( |
151 $custom_settings = array( |
61 'siteUrl' => site_url(), |
152 'siteUrl' => site_url(), |
62 'postsPerPage' => get_option( 'posts_per_page' ), |
153 'postsPerPage' => get_option( 'posts_per_page' ), |
63 'styles' => get_block_editor_theme_styles(), |
154 'styles' => get_block_editor_theme_styles(), |
64 'defaultTemplateTypes' => $indexed_template_types, |
155 'defaultTemplateTypes' => $indexed_template_types, |
86 $navigation_rest_route = rest_get_route_for_post_type_items( |
177 $navigation_rest_route = rest_get_route_for_post_type_items( |
87 'wp_navigation' |
178 'wp_navigation' |
88 ); |
179 ); |
89 |
180 |
90 $preload_paths = array( |
181 $preload_paths = array( |
91 array( '/wp/v2/media', 'OPTIONS' ), |
182 array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ), |
|
183 array( rest_get_route_for_post_type_items( 'page' ), 'OPTIONS' ), |
92 '/wp/v2/types?context=view', |
184 '/wp/v2/types?context=view', |
93 '/wp/v2/types/wp_template?context=edit', |
185 '/wp/v2/types/wp_template?context=edit', |
94 '/wp/v2/types/wp_template-part?context=edit', |
186 '/wp/v2/types/wp_template_part?context=edit', |
95 '/wp/v2/templates?context=edit&per_page=-1', |
187 '/wp/v2/templates?context=edit&per_page=-1', |
96 '/wp/v2/template-parts?context=edit&per_page=-1', |
188 '/wp/v2/template-parts?context=edit&per_page=-1', |
97 '/wp/v2/themes?context=edit&status=active', |
189 '/wp/v2/themes?context=edit&status=active', |
98 '/wp/v2/global-styles/' . $active_global_styles_id . '?context=edit', |
190 '/wp/v2/global-styles/' . $active_global_styles_id . '?context=edit', |
99 '/wp/v2/global-styles/' . $active_global_styles_id, |
191 array( '/wp/v2/global-styles/' . $active_global_styles_id, 'OPTIONS' ), |
100 '/wp/v2/global-styles/themes/' . $active_theme, |
192 '/wp/v2/global-styles/themes/' . $active_theme . '?context=view', |
|
193 '/wp/v2/global-styles/themes/' . $active_theme . '/variations?context=view', |
101 array( $navigation_rest_route, 'OPTIONS' ), |
194 array( $navigation_rest_route, 'OPTIONS' ), |
102 array( |
195 array( |
103 add_query_arg( |
196 add_query_arg( |
104 array( |
197 array( |
105 'context' => 'edit', |
198 'context' => 'edit', |
112 ), |
205 ), |
113 $navigation_rest_route |
206 $navigation_rest_route |
114 ), |
207 ), |
115 'GET', |
208 'GET', |
116 ), |
209 ), |
117 ); |
210 '/wp/v2/settings', |
|
211 array( '/wp/v2/settings', 'OPTIONS' ), |
|
212 // Used by getBlockPatternCategories in useBlockEditorSettings. |
|
213 '/wp/v2/block-patterns/categories', |
|
214 // @see packages/core-data/src/entities.js |
|
215 '/?_fields=' . implode( |
|
216 ',', |
|
217 array( |
|
218 'description', |
|
219 'gmt_offset', |
|
220 'home', |
|
221 'name', |
|
222 'site_icon', |
|
223 'site_icon_url', |
|
224 'site_logo', |
|
225 'timezone_string', |
|
226 'url', |
|
227 'page_for_posts', |
|
228 'page_on_front', |
|
229 'show_on_front', |
|
230 ) |
|
231 ), |
|
232 ); |
|
233 |
|
234 if ( $block_editor_context->post ) { |
|
235 $route_for_post = rest_get_route_for_post( $block_editor_context->post ); |
|
236 if ( $route_for_post ) { |
|
237 $preload_paths[] = add_query_arg( 'context', 'edit', $route_for_post ); |
|
238 if ( 'page' === $block_editor_context->post->post_type ) { |
|
239 $preload_paths[] = add_query_arg( |
|
240 'slug', |
|
241 // @see https://github.com/WordPress/gutenberg/blob/e093fefd041eb6cc4a4e7f67b92ab54fd75c8858/packages/core-data/src/private-selectors.ts#L244-L254 |
|
242 empty( $block_editor_context->post->post_name ) ? 'page' : 'page-' . $block_editor_context->post->post_name, |
|
243 '/wp/v2/templates/lookup' |
|
244 ); |
|
245 } |
|
246 } |
|
247 } else { |
|
248 $preload_paths[] = '/wp/v2/templates/lookup?slug=front-page'; |
|
249 $preload_paths[] = '/wp/v2/templates/lookup?slug=home'; |
|
250 } |
118 |
251 |
119 block_editor_rest_api_preload( $preload_paths, $block_editor_context ); |
252 block_editor_rest_api_preload( $preload_paths, $block_editor_context ); |
120 |
253 |
121 wp_add_inline_script( |
254 wp_add_inline_script( |
122 'wp-edit-site', |
255 'wp-edit-site', |
132 wp_add_inline_script( |
265 wp_add_inline_script( |
133 'wp-blocks', |
266 'wp-blocks', |
134 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' |
267 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' |
135 ); |
268 ); |
136 |
269 |
|
270 // Preload server-registered block bindings sources. |
|
271 $registered_sources = get_all_registered_block_bindings_sources(); |
|
272 if ( ! empty( $registered_sources ) ) { |
|
273 $filtered_sources = array(); |
|
274 foreach ( $registered_sources as $source ) { |
|
275 $filtered_sources[] = array( |
|
276 'name' => $source->name, |
|
277 'label' => $source->label, |
|
278 'usesContext' => $source->uses_context, |
|
279 ); |
|
280 } |
|
281 $script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) ); |
|
282 wp_add_inline_script( |
|
283 'wp-blocks', |
|
284 $script |
|
285 ); |
|
286 } |
|
287 |
137 wp_add_inline_script( |
288 wp_add_inline_script( |
138 'wp-blocks', |
289 'wp-blocks', |
139 sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( isset( $editor_settings['blockCategories'] ) ? $editor_settings['blockCategories'] : array() ) ), |
290 sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( isset( $editor_settings['blockCategories'] ) ? $editor_settings['blockCategories'] : array() ) ), |
140 'after' |
291 'after' |
141 ); |
292 ); |