PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.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 7.4.0 All 402 releases
gutenberg / lib / full-site-editing / edit-site-page.php

edit-site-page.php in Gutenberg 12.1.0, at lib/full-site-editing/edit-site-page.php

281 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Bootstrapping the Gutenberg Edit Site Page.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * The main entry point for the Gutenberg Edit Site Page.
10 *
11 * @since 7.2.0
12 */
13 function gutenberg_edit_site_page() {
14 ?>
15 <div
16 id="edit-site-editor"
17 class="edit-site"
18 >
19 </div>
20 <?php
21 }
22
23 /**
24 * Checks whether the provided page is one of allowed Site Editor pages.
25 *
26 * @param string $page Page to check.
27 *
28 * @return bool True for Site Editor pages, false otherwise.
29 */
30 function gutenberg_is_edit_site_page( $page ) {
31 return 'appearance_page_gutenberg-edit-site' === $page;
32 }
33
34 /**
35 * Checks whether the provided page is the templates list page.
36 *
37 * @return bool True for Site Editor pages, false otherwise.
38 */
39 function gutenberg_is_edit_site_list_page() {
40 return isset( $_GET['postType'] ) && ! isset( $_GET['postId'] );
41 }
42
43 /**
44 * Load editor styles (this is copied from edit-form-blocks.php).
45 * Ideally the code is extracted into a reusable function.
46 *
47 * @return array Editor Styles Setting.
48 */
49 function gutenberg_get_editor_styles() {
50 global $editor_styles;
51
52 // Ideally the code is extracted into a reusable function.
53 $styles = array();
54
55 if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) {
56 foreach ( $editor_styles as $style ) {
57 if ( preg_match( '~^(https?:)?//~', $style ) ) {
58 $response = wp_remote_get( $style );
59 if ( ! is_wp_error( $response ) ) {
60 $styles[] = array(
61 'css' => wp_remote_retrieve_body( $response ),
62 );
63 }
64 } else {
65 $file = get_theme_file_path( $style );
66 if ( is_file( $file ) ) {
67 $styles[] = array(
68 'css' => file_get_contents( $file ),
69 'baseURL' => get_theme_file_uri( $style ),
70 );
71 }
72 }
73 }
74 }
75
76 return $styles;
77 }
78
79 /**
80 * Initialize the Gutenberg Templates List Page.
81 *
82 * @param array $settings The editor settings.
83 */
84 function gutenberg_edit_site_list_init( $settings ) {
85 wp_enqueue_script( 'wp-edit-site' );
86 wp_enqueue_style( 'wp-edit-site' );
87 wp_enqueue_media();
88
89 $post_type = get_post_type_object( $_GET['postType'] );
90
91 if ( ! $post_type ) {
92 wp_die( __( 'Invalid post type.', 'gutenberg' ) );
93 }
94
95 $preload_data = array_reduce(
96 array(
97 '/',
98 "/wp/v2/types/$post_type->name?context=edit",
99 '/wp/v2/types?context=edit',
100 "/wp/v2/$post_type->rest_base?context=edit&per_page=-1",
101 ),
102 'rest_preload_api_request',
103 array()
104 );
105
106 wp_add_inline_script(
107 'wp-api-fetch',
108 sprintf(
109 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
110 wp_json_encode( $preload_data )
111 ),
112 'after'
113 );
114
115 wp_add_inline_script(
116 'wp-edit-site',
117 sprintf(
118 'wp.domReady( function() {
119 wp.editSite.initializeList( "%s", "%s", %s );
120 } );',
121 'edit-site-editor',
122 $post_type->name,
123 wp_json_encode( $settings )
124 )
125 );
126 }
127
128 /**
129 * Initialize the Gutenberg Site Editor.
130 *
131 * @since 7.2.0
132 *
133 * @param string $hook Page.
134 */
135 function gutenberg_edit_site_init( $hook ) {
136 global $current_screen, $post, $editor_styles;
137
138 if ( ! gutenberg_is_edit_site_page( $hook ) ) {
139 return;
140 }
141
142 // Default to is-fullscreen-mode to avoid rendering wp-admin navigation menu while loading and
143 // having jumps in the UI.
144 add_filter(
145 'admin_body_class',
146 static function( $classes ) {
147 return "$classes is-fullscreen-mode";
148 }
149 );
150
151 $custom_settings = array(
152 'siteUrl' => site_url(),
153 'postsPerPage' => get_option( 'posts_per_page' ),
154 'styles' => gutenberg_get_editor_styles(),
155 'defaultTemplateTypes' => gutenberg_get_indexed_default_template_types(),
156 'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(),
157 '__experimentalBlockPatterns' => WP_Block_Patterns_Registry::get_instance()->get_all_registered(),
158 '__experimentalBlockPatternCategories' => WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(),
159 );
160
161 if ( gutenberg_is_edit_site_list_page() ) {
162 return gutenberg_edit_site_list_init( $custom_settings );
163 }
164
165 /**
166 * Make the WP Screen object aware that this is a block editor page.
167 * Since custom blocks check whether the screen is_block_editor,
168 * this is required for custom blocks to be loaded.
169 * See wp_enqueue_registered_block_scripts_and_styles in wp-includes/script-loader.php
170 */
171 $current_screen->is_block_editor( true );
172
173 $site_editor_context = new WP_Block_Editor_Context();
174 $settings = gutenberg_get_block_editor_settings( $custom_settings, $site_editor_context );
175 $active_global_styles_id = WP_Theme_JSON_Resolver_Gutenberg::get_user_custom_post_type_id();
176 $active_theme = wp_get_theme()->get_stylesheet();
177 gutenberg_initialize_editor(
178 'edit_site_editor',
179 'edit-site',
180 array(
181 'preload_paths' => array_merge(
182 gutenberg_get_navigation_areas_paths_to_preload(),
183 array(
184 array( '/wp/v2/media', 'OPTIONS' ),
185 '/',
186 '/wp/v2/types?context=edit',
187 '/wp/v2/taxonomies?context=edit',
188 '/wp/v2/pages?context=edit',
189 '/wp/v2/categories?context=edit',
190 '/wp/v2/posts?context=edit',
191 '/wp/v2/tags?context=edit',
192 '/wp/v2/templates?context=edit',
193 '/wp/v2/template-parts?context=edit',
194 '/wp/v2/settings',
195 '/wp/v2/themes?context=edit&status=active',
196 '/wp/v2/global-styles/' . $active_global_styles_id . '?context=edit',
197 '/wp/v2/global-styles/' . $active_global_styles_id,
198 '/wp/v2/global-styles/themes/' . $active_theme,
199 )
200 ),
201 'initializer_name' => 'initializeEditor',
202 'editor_settings' => $settings,
203 )
204 );
205
206 wp_add_inline_script(
207 'wp-blocks',
208 sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ),
209 'after'
210 );
211
212 wp_enqueue_script( 'wp-edit-site' );
213 wp_enqueue_script( 'wp-format-library' );
214 wp_enqueue_style( 'wp-edit-site' );
215 wp_enqueue_style( 'wp-format-library' );
216 wp_enqueue_media();
217
218 if (
219 current_theme_supports( 'wp-block-styles' ) ||
220 ( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 )
221 ) {
222 wp_enqueue_style( 'wp-block-library-theme' );
223 }
224
225 /**
226 * Fires after block assets have been enqueued for the editing interface.
227 *
228 * Call `add_action` on any hook before 'admin_enqueue_scripts'.
229 *
230 * In the function call you supply, simply use `wp_enqueue_script` and
231 * `wp_enqueue_style` to add your functionality to the block editor.
232 *
233 * @since 5.0.0
234 */
235
236 do_action( 'enqueue_block_editor_assets' );
237
238 }
239 add_action( 'admin_enqueue_scripts', 'gutenberg_edit_site_init' );
240
241 /**
242 * Register a core site setting for front page information.
243 */
244 function register_site_editor_homepage_settings() {
245 register_setting(
246 'reading',
247 'show_on_front',
248 array(
249 'show_in_rest' => true,
250 'type' => 'string',
251 'description' => __( 'What to show on the front page', 'gutenberg' ),
252 )
253 );
254
255 register_setting(
256 'reading',
257 'page_on_front',
258 array(
259 'show_in_rest' => true,
260 'type' => 'number',
261 'description' => __( 'The ID of the page that should be displayed on the front page', 'gutenberg' ),
262 )
263 );
264 }
265 add_action( 'init', 'register_site_editor_homepage_settings', 10 );
266
267 /**
268 * Sets the HTML <title> in the Site Editor list page to be the title of the CPT
269 * being edited, e.g. 'Templates'.
270 */
271 function gutenberg_set_site_editor_list_page_title() {
272 global $title;
273 if ( gutenberg_is_edit_site_list_page() ) {
274 $post_type = get_post_type_object( $_GET['postType'] );
275 if ( $post_type ) {
276 $title = $post_type->labels->name;
277 }
278 }
279 }
280 add_action( 'load-appearance_page_gutenberg-edit-site', 'gutenberg_set_site_editor_list_page_title' );
281