9
|
1 |
<?php |
|
2 |
/** |
16
|
3 |
* The block editor page. |
9
|
4 |
* |
|
5 |
* @since 5.0.0 |
|
6 |
* |
|
7 |
* @package WordPress |
|
8 |
* @subpackage Administration |
|
9 |
*/ |
|
10 |
|
16
|
11 |
// Don't load directly. |
9
|
12 |
if ( ! defined( 'ABSPATH' ) ) { |
|
13 |
die( '-1' ); |
|
14 |
} |
|
15 |
|
|
16 |
/** |
|
17 |
* @global string $post_type |
|
18 |
* @global WP_Post_Type $post_type_object |
16
|
19 |
* @global WP_Post $post Global post object. |
9
|
20 |
* @global string $title |
|
21 |
* @global array $wp_meta_boxes |
|
22 |
*/ |
18
|
23 |
global $post_type, $post_type_object, $post, $title, $wp_meta_boxes; |
|
24 |
|
|
25 |
$block_editor_context = new WP_Block_Editor_Context( array( 'post' => $post ) ); |
9
|
26 |
|
|
27 |
// Flag that we're loading the block editor. |
|
28 |
$current_screen = get_current_screen(); |
|
29 |
$current_screen->is_block_editor( true ); |
|
30 |
|
18
|
31 |
// Default to is-fullscreen-mode to avoid jumps in the UI. |
|
32 |
add_filter( |
|
33 |
'admin_body_class', |
19
|
34 |
static function( $classes ) { |
18
|
35 |
return "$classes is-fullscreen-mode"; |
|
36 |
} |
|
37 |
); |
|
38 |
|
9
|
39 |
/* |
|
40 |
* Emoji replacement is disabled for now, until it plays nicely with React. |
|
41 |
*/ |
|
42 |
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
|
43 |
|
18
|
44 |
/* |
|
45 |
* Block editor implements its own Options menu for toggling Document Panels. |
|
46 |
*/ |
|
47 |
add_filter( 'screen_options_show_screen', '__return_false' ); |
|
48 |
|
9
|
49 |
wp_enqueue_script( 'heartbeat' ); |
|
50 |
wp_enqueue_script( 'wp-edit-post' ); |
|
51 |
|
19
|
52 |
$rest_path = rest_get_route_for_post( $post ); |
9
|
53 |
|
|
54 |
// Preload common data. |
|
55 |
$preload_paths = array( |
19
|
56 |
'/wp/v2/types?context=view', |
|
57 |
'/wp/v2/taxonomies?context=view', |
|
58 |
add_query_arg( |
|
59 |
array( |
|
60 |
'context' => 'edit', |
|
61 |
'per_page' => -1, |
|
62 |
), |
|
63 |
rest_get_route_for_post_type_items( 'wp_block' ) |
|
64 |
), |
|
65 |
add_query_arg( 'context', 'edit', $rest_path ), |
9
|
66 |
sprintf( '/wp/v2/types/%s?context=edit', $post_type ), |
19
|
67 |
'/wp/v2/users/me', |
|
68 |
array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ), |
|
69 |
array( rest_get_route_for_post_type_items( 'page' ), 'OPTIONS' ), |
|
70 |
array( rest_get_route_for_post_type_items( 'wp_block' ), 'OPTIONS' ), |
|
71 |
sprintf( '%s/autosaves?context=edit', $rest_path ), |
|
72 |
'/wp/v2/settings', |
9
|
73 |
); |
|
74 |
|
18
|
75 |
block_editor_rest_api_preload( $preload_paths, $block_editor_context ); |
9
|
76 |
|
|
77 |
wp_add_inline_script( |
|
78 |
'wp-blocks', |
|
79 |
sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ), |
|
80 |
'after' |
|
81 |
); |
|
82 |
|
|
83 |
/* |
|
84 |
* Assign initial edits, if applicable. These are not initially assigned to the persisted post, |
|
85 |
* but should be included in its save payload. |
|
86 |
*/ |
19
|
87 |
$initial_edits = array(); |
9
|
88 |
$is_new_post = false; |
|
89 |
if ( 'auto-draft' === $post->post_status ) { |
|
90 |
$is_new_post = true; |
|
91 |
// Override "(Auto Draft)" new post default title with empty string, or filtered value. |
19
|
92 |
if ( post_type_supports( $post->post_type, 'title' ) ) { |
|
93 |
$initial_edits['title'] = $post->post_title; |
|
94 |
} |
|
95 |
|
|
96 |
if ( post_type_supports( $post->post_type, 'editor' ) ) { |
|
97 |
$initial_edits['content'] = $post->post_content; |
|
98 |
} |
|
99 |
|
|
100 |
if ( post_type_supports( $post->post_type, 'excerpt' ) ) { |
|
101 |
$initial_edits['excerpt'] = $post->post_excerpt; |
|
102 |
} |
9
|
103 |
} |
|
104 |
|
|
105 |
// Preload server-registered block schemas. |
|
106 |
wp_add_inline_script( |
|
107 |
'wp-blocks', |
|
108 |
'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' |
|
109 |
); |
|
110 |
|
|
111 |
// Get admin url for handling meta boxes. |
|
112 |
$meta_box_url = admin_url( 'post.php' ); |
|
113 |
$meta_box_url = add_query_arg( |
|
114 |
array( |
|
115 |
'post' => $post->ID, |
|
116 |
'action' => 'edit', |
|
117 |
'meta-box-loader' => true, |
|
118 |
'meta-box-loader-nonce' => wp_create_nonce( 'meta-box-loader' ), |
|
119 |
), |
|
120 |
$meta_box_url |
|
121 |
); |
18
|
122 |
wp_add_inline_script( |
|
123 |
'wp-editor', |
|
124 |
sprintf( 'var _wpMetaBoxUrl = %s;', wp_json_encode( $meta_box_url ) ), |
|
125 |
'before' |
|
126 |
); |
9
|
127 |
|
16
|
128 |
/* |
|
129 |
* Get all available templates for the post/page attributes meta-box. |
|
130 |
* The "Default template" array element should only be added if the array is |
|
131 |
* not empty so we do not trigger the template select element without any options |
|
132 |
* besides the default value. |
|
133 |
*/ |
9
|
134 |
$available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) ); |
18
|
135 |
$available_templates = ! empty( $available_templates ) ? array_replace( |
9
|
136 |
array( |
|
137 |
/** This filter is documented in wp-admin/includes/meta-boxes.php */ |
|
138 |
'' => apply_filters( 'default_page_template_title', __( 'Default template' ), 'rest-api' ), |
|
139 |
), |
|
140 |
$available_templates |
|
141 |
) : $available_templates; |
|
142 |
|
|
143 |
// Lock settings. |
|
144 |
$user_id = wp_check_post_lock( $post->ID ); |
|
145 |
if ( $user_id ) { |
|
146 |
$locked = false; |
|
147 |
|
|
148 |
/** This filter is documented in wp-admin/includes/post.php */ |
|
149 |
if ( apply_filters( 'show_post_locked_dialog', true, $post, $user_id ) ) { |
|
150 |
$locked = true; |
|
151 |
} |
|
152 |
|
|
153 |
$user_details = null; |
|
154 |
if ( $locked ) { |
|
155 |
$user = get_userdata( $user_id ); |
|
156 |
$user_details = array( |
19
|
157 |
'avatar' => get_avatar_url( $user_id, array( 'size' => 128 ) ), |
|
158 |
'name' => $user->display_name, |
9
|
159 |
); |
|
160 |
} |
|
161 |
|
|
162 |
$lock_details = array( |
|
163 |
'isLocked' => $locked, |
|
164 |
'user' => $user_details, |
|
165 |
); |
|
166 |
} else { |
|
167 |
// Lock the post. |
|
168 |
$active_post_lock = wp_set_post_lock( $post->ID ); |
16
|
169 |
if ( $active_post_lock ) { |
|
170 |
$active_post_lock = esc_attr( implode( ':', $active_post_lock ) ); |
|
171 |
} |
|
172 |
|
|
173 |
$lock_details = array( |
9
|
174 |
'isLocked' => false, |
16
|
175 |
'activePostLock' => $active_post_lock, |
9
|
176 |
); |
|
177 |
} |
|
178 |
|
|
179 |
/** |
|
180 |
* Filters the body placeholder text. |
|
181 |
* |
|
182 |
* @since 5.0.0 |
18
|
183 |
* @since 5.8.0 Changed the default placeholder text. |
9
|
184 |
* |
18
|
185 |
* @param string $text Placeholder text. Default 'Type / to choose a block'. |
9
|
186 |
* @param WP_Post $post Post object. |
|
187 |
*/ |
18
|
188 |
$body_placeholder = apply_filters( 'write_your_story', __( 'Type / to choose a block' ), $post ); |
9
|
189 |
|
|
190 |
$editor_settings = array( |
19
|
191 |
'availableTemplates' => $available_templates, |
|
192 |
'disablePostFormats' => ! current_theme_supports( 'post-formats' ), |
9
|
193 |
/** This filter is documented in wp-admin/edit-form-advanced.php */ |
19
|
194 |
'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title' ), $post ), |
|
195 |
'bodyPlaceholder' => $body_placeholder, |
|
196 |
'autosaveInterval' => AUTOSAVE_INTERVAL, |
|
197 |
'richEditingEnabled' => user_can_richedit(), |
|
198 |
'postLock' => $lock_details, |
|
199 |
'postLockUtils' => array( |
9
|
200 |
'nonce' => wp_create_nonce( 'lock-post_' . $post->ID ), |
|
201 |
'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ), |
|
202 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
|
203 |
), |
19
|
204 |
'supportsLayout' => WP_Theme_JSON_Resolver::theme_has_support(), |
|
205 |
'supportsTemplateMode' => current_theme_supports( 'block-templates' ), |
9
|
206 |
|
|
207 |
// Whether or not to load the 'postcustom' meta box is stored as a user meta |
|
208 |
// field so that we're not always loading its assets. |
19
|
209 |
'enableCustomFields' => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ), |
9
|
210 |
); |
|
211 |
|
19
|
212 |
// Add additional back-compat patterns registered by `current_screen` et al. |
|
213 |
$editor_settings['__experimentalAdditionalBlockPatterns'] = WP_Block_Patterns_Registry::get_instance()->get_all_registered( true ); |
|
214 |
$editor_settings['__experimentalAdditionalBlockPatternCategories'] = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered( true ); |
|
215 |
|
18
|
216 |
$autosave = wp_get_post_autosave( $post->ID ); |
9
|
217 |
if ( $autosave ) { |
|
218 |
if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) { |
|
219 |
$editor_settings['autosave'] = array( |
|
220 |
'editLink' => get_edit_post_link( $autosave->ID ), |
|
221 |
); |
|
222 |
} else { |
|
223 |
wp_delete_post_revision( $autosave->ID ); |
|
224 |
} |
|
225 |
} |
|
226 |
|
|
227 |
if ( ! empty( $post_type_object->template ) ) { |
|
228 |
$editor_settings['template'] = $post_type_object->template; |
|
229 |
$editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false; |
|
230 |
} |
|
231 |
|
|
232 |
// If there's no template set on a new post, use the post format, instead. |
|
233 |
if ( $is_new_post && ! isset( $editor_settings['template'] ) && 'post' === $post->post_type ) { |
|
234 |
$post_format = get_post_format( $post ); |
|
235 |
if ( in_array( $post_format, array( 'audio', 'gallery', 'image', 'quote', 'video' ), true ) ) { |
|
236 |
$editor_settings['template'] = array( array( "core/$post_format" ) ); |
|
237 |
} |
|
238 |
} |
|
239 |
|
19
|
240 |
if ( wp_is_block_theme() && $editor_settings['supportsTemplateMode'] ) { |
|
241 |
$editor_settings['defaultTemplatePartAreas'] = get_allowed_block_template_part_areas(); |
|
242 |
} |
|
243 |
|
9
|
244 |
/** |
|
245 |
* Scripts |
|
246 |
*/ |
|
247 |
wp_enqueue_media( |
|
248 |
array( |
|
249 |
'post' => $post->ID, |
|
250 |
) |
|
251 |
); |
|
252 |
wp_tinymce_inline_scripts(); |
|
253 |
wp_enqueue_editor(); |
|
254 |
|
|
255 |
/** |
|
256 |
* Styles |
|
257 |
*/ |
|
258 |
wp_enqueue_style( 'wp-edit-post' ); |
|
259 |
|
|
260 |
/** |
|
261 |
* Fires after block assets have been enqueued for the editing interface. |
|
262 |
* |
|
263 |
* Call `add_action` on any hook before 'admin_enqueue_scripts'. |
|
264 |
* |
|
265 |
* In the function call you supply, simply use `wp_enqueue_script` and |
|
266 |
* `wp_enqueue_style` to add your functionality to the block editor. |
|
267 |
* |
|
268 |
* @since 5.0.0 |
|
269 |
*/ |
|
270 |
do_action( 'enqueue_block_editor_assets' ); |
|
271 |
|
|
272 |
// In order to duplicate classic meta box behaviour, we need to run the classic meta box actions. |
16
|
273 |
require_once ABSPATH . 'wp-admin/includes/meta-boxes.php'; |
9
|
274 |
register_and_do_post_meta_boxes( $post ); |
|
275 |
|
|
276 |
// Check if the Custom Fields meta box has been removed at some point. |
|
277 |
$core_meta_boxes = $wp_meta_boxes[ $current_screen->id ]['normal']['core']; |
|
278 |
if ( ! isset( $core_meta_boxes['postcustom'] ) || ! $core_meta_boxes['postcustom'] ) { |
|
279 |
unset( $editor_settings['enableCustomFields'] ); |
|
280 |
} |
|
281 |
|
18
|
282 |
$editor_settings = get_block_editor_settings( $editor_settings, $block_editor_context ); |
9
|
283 |
|
|
284 |
$init_script = <<<JS |
|
285 |
( function() { |
|
286 |
window._wpLoadBlockEditor = new Promise( function( resolve ) { |
|
287 |
wp.domReady( function() { |
|
288 |
resolve( wp.editPost.initializeEditor( 'editor', "%s", %d, %s, %s ) ); |
|
289 |
} ); |
|
290 |
} ); |
|
291 |
} )(); |
|
292 |
JS; |
|
293 |
|
|
294 |
$script = sprintf( |
|
295 |
$init_script, |
|
296 |
$post->post_type, |
|
297 |
$post->ID, |
|
298 |
wp_json_encode( $editor_settings ), |
|
299 |
wp_json_encode( $initial_edits ) |
|
300 |
); |
|
301 |
wp_add_inline_script( 'wp-edit-post', $script ); |
|
302 |
|
18
|
303 |
if ( (int) get_option( 'page_for_posts' ) === $post->ID ) { |
|
304 |
add_action( 'admin_enqueue_scripts', '_wp_block_editor_posts_page_notice' ); |
|
305 |
} |
|
306 |
|
16
|
307 |
require_once ABSPATH . 'wp-admin/admin-header.php'; |
9
|
308 |
?> |
|
309 |
|
|
310 |
<div class="block-editor"> |
|
311 |
<h1 class="screen-reader-text hide-if-no-js"><?php echo esc_html( $title ); ?></h1> |
|
312 |
<div id="editor" class="block-editor__container hide-if-no-js"></div> |
|
313 |
<div id="metaboxes" class="hidden"> |
|
314 |
<?php the_block_editor_meta_boxes(); ?> |
|
315 |
</div> |
|
316 |
|
|
317 |
<?php // JavaScript is disabled. ?> |
|
318 |
<div class="wrap hide-if-js block-editor-no-js"> |
|
319 |
<h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1> |
|
320 |
<div class="notice notice-error notice-alt"> |
|
321 |
<p> |
|
322 |
<?php |
|
323 |
$message = sprintf( |
16
|
324 |
/* translators: %s: A link to install the Classic Editor plugin. */ |
9
|
325 |
__( 'The block editor requires JavaScript. Please enable JavaScript in your browser settings, or try the <a href="%s">Classic Editor plugin</a>.' ), |
16
|
326 |
esc_url( wp_nonce_url( self_admin_url( 'plugin-install.php?tab=favorites&user=wordpressdotorg&save=0' ), 'save_wporg_username_' . get_current_user_id() ) ) |
9
|
327 |
); |
|
328 |
|
|
329 |
/** |
|
330 |
* Filters the message displayed in the block editor interface when JavaScript is |
|
331 |
* not enabled in the browser. |
|
332 |
* |
|
333 |
* @since 5.0.3 |
|
334 |
* |
|
335 |
* @param string $message The message being displayed. |
|
336 |
* @param WP_Post $post The post being edited. |
|
337 |
*/ |
|
338 |
echo apply_filters( 'block_editor_no_javascript_message', $message, $post ); |
|
339 |
?> |
|
340 |
</p> |
|
341 |
</div> |
|
342 |
</div> |
|
343 |
</div> |