|
1 <?php |
|
2 /** |
|
3 * The Block Editor page. |
|
4 * |
|
5 * @since 5.0.0 |
|
6 * |
|
7 * @package WordPress |
|
8 * @subpackage Administration |
|
9 */ |
|
10 |
|
11 // don't load directly |
|
12 if ( ! defined( 'ABSPATH' ) ) { |
|
13 die( '-1' ); |
|
14 } |
|
15 |
|
16 /** |
|
17 * @global string $post_type |
|
18 * @global WP_Post_Type $post_type_object |
|
19 * @global WP_Post $post |
|
20 * @global string $title |
|
21 * @global array $editor_styles |
|
22 * @global array $wp_meta_boxes |
|
23 */ |
|
24 global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes; |
|
25 |
|
26 // Flag that we're loading the block editor. |
|
27 $current_screen = get_current_screen(); |
|
28 $current_screen->is_block_editor( true ); |
|
29 |
|
30 /* |
|
31 * Emoji replacement is disabled for now, until it plays nicely with React. |
|
32 */ |
|
33 remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
|
34 |
|
35 wp_enqueue_script( 'heartbeat' ); |
|
36 wp_enqueue_script( 'wp-edit-post' ); |
|
37 wp_enqueue_script( 'wp-format-library' ); |
|
38 |
|
39 $rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name; |
|
40 |
|
41 // Preload common data. |
|
42 $preload_paths = array( |
|
43 '/', |
|
44 '/wp/v2/types?context=edit', |
|
45 '/wp/v2/taxonomies?per_page=-1&context=edit', |
|
46 '/wp/v2/themes?status=active', |
|
47 sprintf( '/wp/v2/%s/%s?context=edit', $rest_base, $post->ID ), |
|
48 sprintf( '/wp/v2/types/%s?context=edit', $post_type ), |
|
49 sprintf( '/wp/v2/users/me?post_type=%s&context=edit', $post_type ), |
|
50 array( '/wp/v2/media', 'OPTIONS' ), |
|
51 array( '/wp/v2/blocks', 'OPTIONS' ), |
|
52 ); |
|
53 |
|
54 /** |
|
55 * Preload common data by specifying an array of REST API paths that will be preloaded. |
|
56 * |
|
57 * Filters the array of paths that will be preloaded. |
|
58 * |
|
59 * @since 5.0.0 |
|
60 * |
|
61 * @param array $preload_paths Array of paths to preload. |
|
62 * @param object $post The post resource data. |
|
63 */ |
|
64 $preload_paths = apply_filters( 'block_editor_preload_paths', $preload_paths, $post ); |
|
65 |
|
66 /* |
|
67 * Ensure the global $post remains the same after API data is preloaded. |
|
68 * Because API preloading can call the_content and other filters, plugins |
|
69 * can unexpectedly modify $post. |
|
70 */ |
|
71 $backup_global_post = $post; |
|
72 |
|
73 $preload_data = array_reduce( |
|
74 $preload_paths, |
|
75 'rest_preload_api_request', |
|
76 array() |
|
77 ); |
|
78 |
|
79 // Restore the global $post as it was before API preloading. |
|
80 $post = $backup_global_post; |
|
81 |
|
82 wp_add_inline_script( |
|
83 'wp-api-fetch', |
|
84 sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode( $preload_data ) ), |
|
85 'after' |
|
86 ); |
|
87 |
|
88 wp_add_inline_script( |
|
89 'wp-blocks', |
|
90 sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ), |
|
91 'after' |
|
92 ); |
|
93 |
|
94 /* |
|
95 * Assign initial edits, if applicable. These are not initially assigned to the persisted post, |
|
96 * but should be included in its save payload. |
|
97 */ |
|
98 $initial_edits = null; |
|
99 $is_new_post = false; |
|
100 if ( 'auto-draft' === $post->post_status ) { |
|
101 $is_new_post = true; |
|
102 // Override "(Auto Draft)" new post default title with empty string, or filtered value. |
|
103 $initial_edits = array( |
|
104 'title' => $post->post_title, |
|
105 'content' => $post->post_content, |
|
106 'excerpt' => $post->post_excerpt, |
|
107 ); |
|
108 } |
|
109 |
|
110 // Preload server-registered block schemas. |
|
111 wp_add_inline_script( |
|
112 'wp-blocks', |
|
113 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' |
|
114 ); |
|
115 |
|
116 // Get admin url for handling meta boxes. |
|
117 $meta_box_url = admin_url( 'post.php' ); |
|
118 $meta_box_url = add_query_arg( |
|
119 array( |
|
120 'post' => $post->ID, |
|
121 'action' => 'edit', |
|
122 'meta-box-loader' => true, |
|
123 'meta-box-loader-nonce' => wp_create_nonce( 'meta-box-loader' ), |
|
124 ), |
|
125 $meta_box_url |
|
126 ); |
|
127 wp_localize_script( 'wp-editor', '_wpMetaBoxUrl', $meta_box_url ); |
|
128 |
|
129 |
|
130 /* |
|
131 * Initialize the editor. |
|
132 */ |
|
133 |
|
134 $align_wide = get_theme_support( 'align-wide' ); |
|
135 $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); |
|
136 $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); |
|
137 |
|
138 /** |
|
139 * Filters the allowed block types for the editor, defaulting to true (all |
|
140 * block types supported). |
|
141 * |
|
142 * @since 5.0.0 |
|
143 * |
|
144 * @param bool|array $allowed_block_types Array of block type slugs, or |
|
145 * boolean to enable/disable all. |
|
146 * @param object $post The post resource data. |
|
147 */ |
|
148 $allowed_block_types = apply_filters( 'allowed_block_types', true, $post ); |
|
149 |
|
150 // Get all available templates for the post/page attributes meta-box. |
|
151 // The "Default template" array element should only be added if the array is |
|
152 // not empty so we do not trigger the template select element without any options |
|
153 // besides the default value. |
|
154 $available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) ); |
|
155 $available_templates = ! empty( $available_templates ) ? array_merge( |
|
156 array( |
|
157 /** This filter is documented in wp-admin/includes/meta-boxes.php */ |
|
158 '' => apply_filters( 'default_page_template_title', __( 'Default template' ), 'rest-api' ), |
|
159 ), |
|
160 $available_templates |
|
161 ) : $available_templates; |
|
162 |
|
163 // Media settings. |
|
164 $max_upload_size = wp_max_upload_size(); |
|
165 if ( ! $max_upload_size ) { |
|
166 $max_upload_size = 0; |
|
167 } |
|
168 |
|
169 // Editor Styles. |
|
170 $styles = array( |
|
171 array( |
|
172 'css' => file_get_contents( |
|
173 ABSPATH . WPINC . '/css/dist/editor/editor-styles.css' |
|
174 ), |
|
175 ), |
|
176 ); |
|
177 |
|
178 /* Translators: Use this to specify the CSS font family for the default font */ |
|
179 $locale_font_family = esc_html_x( 'Noto Serif', 'CSS Font Family for Editor Font' ); |
|
180 $styles[] = array( |
|
181 'css' => "body { font-family: '$locale_font_family' }", |
|
182 ); |
|
183 |
|
184 if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) { |
|
185 foreach ( $editor_styles as $style ) { |
|
186 if ( preg_match( '~^(https?:)?//~', $style ) ) { |
|
187 $response = wp_remote_get( $style ); |
|
188 if ( ! is_wp_error( $response ) ) { |
|
189 $styles[] = array( |
|
190 'css' => wp_remote_retrieve_body( $response ), |
|
191 ); |
|
192 } |
|
193 } else { |
|
194 $file = get_theme_file_path( $style ); |
|
195 if ( is_file( $file ) ) { |
|
196 $styles[] = array( |
|
197 'css' => file_get_contents( $file ), |
|
198 'baseURL' => get_theme_file_uri( $style ), |
|
199 ); |
|
200 } |
|
201 } |
|
202 } |
|
203 } |
|
204 |
|
205 // Image sizes. |
|
206 |
|
207 /** This filter is documented in wp-admin/includes/media.php */ |
|
208 $image_size_names = apply_filters( |
|
209 'image_size_names_choose', |
|
210 array( |
|
211 'thumbnail' => __( 'Thumbnail' ), |
|
212 'medium' => __( 'Medium' ), |
|
213 'large' => __( 'Large' ), |
|
214 'full' => __( 'Full Size' ), |
|
215 ) |
|
216 ); |
|
217 |
|
218 $available_image_sizes = array(); |
|
219 foreach ( $image_size_names as $image_size_slug => $image_size_name ) { |
|
220 $available_image_sizes[] = array( |
|
221 'slug' => $image_size_slug, |
|
222 'name' => $image_size_name, |
|
223 ); |
|
224 } |
|
225 |
|
226 // Lock settings. |
|
227 $user_id = wp_check_post_lock( $post->ID ); |
|
228 if ( $user_id ) { |
|
229 $locked = false; |
|
230 |
|
231 /** This filter is documented in wp-admin/includes/post.php */ |
|
232 if ( apply_filters( 'show_post_locked_dialog', true, $post, $user_id ) ) { |
|
233 $locked = true; |
|
234 } |
|
235 |
|
236 $user_details = null; |
|
237 if ( $locked ) { |
|
238 $user = get_userdata( $user_id ); |
|
239 $user_details = array( |
|
240 'name' => $user->display_name, |
|
241 ); |
|
242 $avatar = get_avatar_url( $user_id, array( 'size' => 64 ) ); |
|
243 } |
|
244 |
|
245 $lock_details = array( |
|
246 'isLocked' => $locked, |
|
247 'user' => $user_details, |
|
248 ); |
|
249 } else { |
|
250 // Lock the post. |
|
251 $active_post_lock = wp_set_post_lock( $post->ID ); |
|
252 $lock_details = array( |
|
253 'isLocked' => false, |
|
254 'activePostLock' => esc_attr( implode( ':', $active_post_lock ) ), |
|
255 ); |
|
256 } |
|
257 |
|
258 /** |
|
259 * Filters the body placeholder text. |
|
260 * |
|
261 * @since 5.0.0 |
|
262 * |
|
263 * @param string $text Placeholder text. Default 'Start writing or type / to choose a block'. |
|
264 * @param WP_Post $post Post object. |
|
265 */ |
|
266 $body_placeholder = apply_filters( 'write_your_story', __( 'Start writing or type / to choose a block' ), $post ); |
|
267 |
|
268 $editor_settings = array( |
|
269 'alignWide' => $align_wide, |
|
270 'availableTemplates' => $available_templates, |
|
271 'allowedBlockTypes' => $allowed_block_types, |
|
272 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), |
|
273 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), |
|
274 'disablePostFormats' => ! current_theme_supports( 'post-formats' ), |
|
275 /** This filter is documented in wp-admin/edit-form-advanced.php */ |
|
276 'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title' ), $post ), |
|
277 'bodyPlaceholder' => $body_placeholder, |
|
278 'isRTL' => is_rtl(), |
|
279 'autosaveInterval' => AUTOSAVE_INTERVAL, |
|
280 'maxUploadFileSize' => $max_upload_size, |
|
281 'allowedMimeTypes' => get_allowed_mime_types(), |
|
282 'styles' => $styles, |
|
283 'imageSizes' => $available_image_sizes, |
|
284 'richEditingEnabled' => user_can_richedit(), |
|
285 'postLock' => $lock_details, |
|
286 'postLockUtils' => array( |
|
287 'nonce' => wp_create_nonce( 'lock-post_' . $post->ID ), |
|
288 'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ), |
|
289 'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
|
290 ), |
|
291 |
|
292 // Whether or not to load the 'postcustom' meta box is stored as a user meta |
|
293 // field so that we're not always loading its assets. |
|
294 'enableCustomFields' => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ), |
|
295 ); |
|
296 |
|
297 $autosave = wp_get_post_autosave( $post_ID ); |
|
298 if ( $autosave ) { |
|
299 if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) { |
|
300 $editor_settings['autosave'] = array( |
|
301 'editLink' => get_edit_post_link( $autosave->ID ), |
|
302 ); |
|
303 } else { |
|
304 wp_delete_post_revision( $autosave->ID ); |
|
305 } |
|
306 } |
|
307 |
|
308 if ( false !== $color_palette ) { |
|
309 $editor_settings['colors'] = $color_palette; |
|
310 } |
|
311 |
|
312 if ( false !== $font_sizes ) { |
|
313 $editor_settings['fontSizes'] = $font_sizes; |
|
314 } |
|
315 |
|
316 if ( ! empty( $post_type_object->template ) ) { |
|
317 $editor_settings['template'] = $post_type_object->template; |
|
318 $editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false; |
|
319 } |
|
320 |
|
321 // If there's no template set on a new post, use the post format, instead. |
|
322 if ( $is_new_post && ! isset( $editor_settings['template'] ) && 'post' === $post->post_type ) { |
|
323 $post_format = get_post_format( $post ); |
|
324 if ( in_array( $post_format, array( 'audio', 'gallery', 'image', 'quote', 'video' ), true ) ) { |
|
325 $editor_settings['template'] = array( array( "core/$post_format" ) ); |
|
326 } |
|
327 } |
|
328 |
|
329 /** |
|
330 * Scripts |
|
331 */ |
|
332 wp_enqueue_media( |
|
333 array( |
|
334 'post' => $post->ID, |
|
335 ) |
|
336 ); |
|
337 wp_tinymce_inline_scripts(); |
|
338 wp_enqueue_editor(); |
|
339 |
|
340 /** |
|
341 * Styles |
|
342 */ |
|
343 wp_enqueue_style( 'wp-edit-post' ); |
|
344 wp_enqueue_style( 'wp-format-library' ); |
|
345 |
|
346 /** |
|
347 * Fires after block assets have been enqueued for the editing interface. |
|
348 * |
|
349 * Call `add_action` on any hook before 'admin_enqueue_scripts'. |
|
350 * |
|
351 * In the function call you supply, simply use `wp_enqueue_script` and |
|
352 * `wp_enqueue_style` to add your functionality to the block editor. |
|
353 * |
|
354 * @since 5.0.0 |
|
355 */ |
|
356 do_action( 'enqueue_block_editor_assets' ); |
|
357 |
|
358 // In order to duplicate classic meta box behaviour, we need to run the classic meta box actions. |
|
359 require_once( ABSPATH . 'wp-admin/includes/meta-boxes.php' ); |
|
360 register_and_do_post_meta_boxes( $post ); |
|
361 |
|
362 // Check if the Custom Fields meta box has been removed at some point. |
|
363 $core_meta_boxes = $wp_meta_boxes[ $current_screen->id ]['normal']['core']; |
|
364 if ( ! isset( $core_meta_boxes['postcustom'] ) || ! $core_meta_boxes['postcustom'] ) { |
|
365 unset( $editor_settings['enableCustomFields'] ); |
|
366 } |
|
367 |
|
368 /** |
|
369 * Filters the settings to pass to the block editor. |
|
370 * |
|
371 * @since 5.0.0 |
|
372 * |
|
373 * @param array $editor_settings Default editor settings. |
|
374 * @param WP_Post $post Post being edited. |
|
375 */ |
|
376 $editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post ); |
|
377 |
|
378 $init_script = <<<JS |
|
379 ( function() { |
|
380 window._wpLoadBlockEditor = new Promise( function( resolve ) { |
|
381 wp.domReady( function() { |
|
382 resolve( wp.editPost.initializeEditor( 'editor', "%s", %d, %s, %s ) ); |
|
383 } ); |
|
384 } ); |
|
385 } )(); |
|
386 JS; |
|
387 |
|
388 $script = sprintf( |
|
389 $init_script, |
|
390 $post->post_type, |
|
391 $post->ID, |
|
392 wp_json_encode( $editor_settings ), |
|
393 wp_json_encode( $initial_edits ) |
|
394 ); |
|
395 wp_add_inline_script( 'wp-edit-post', $script ); |
|
396 |
|
397 require_once( ABSPATH . 'wp-admin/admin-header.php' ); |
|
398 ?> |
|
399 |
|
400 <div class="block-editor"> |
|
401 <h1 class="screen-reader-text hide-if-no-js"><?php echo esc_html( $title ); ?></h1> |
|
402 <div id="editor" class="block-editor__container hide-if-no-js"></div> |
|
403 <div id="metaboxes" class="hidden"> |
|
404 <?php the_block_editor_meta_boxes(); ?> |
|
405 </div> |
|
406 |
|
407 <?php // JavaScript is disabled. ?> |
|
408 <div class="wrap hide-if-js block-editor-no-js"> |
|
409 <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1> |
|
410 <div class="notice notice-error notice-alt"> |
|
411 <p> |
|
412 <?php |
|
413 $message = sprintf( |
|
414 /* translators: %s: Classic Editor plugin URL */ |
|
415 __( 'The block editor requires JavaScript. Please enable JavaScript in your browser settings, or try the <a href="%s">Classic Editor plugin</a>.' ), |
|
416 __( 'https://wordpress.org/plugins/classic-editor/' ) |
|
417 ); |
|
418 |
|
419 /** |
|
420 * Filters the message displayed in the block editor interface when JavaScript is |
|
421 * not enabled in the browser. |
|
422 * |
|
423 * @since 5.0.3 |
|
424 * |
|
425 * @param string $message The message being displayed. |
|
426 * @param WP_Post $post The post being edited. |
|
427 */ |
|
428 echo apply_filters( 'block_editor_no_javascript_message', $message, $post ); |
|
429 ?> |
|
430 </p> |
|
431 </div> |
|
432 </div> |
|
433 </div> |