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