wp/wp-admin/edit-form-blocks.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    16 /**
    16 /**
    17  * @global string       $post_type
    17  * @global string       $post_type
    18  * @global WP_Post_Type $post_type_object
    18  * @global WP_Post_Type $post_type_object
    19  * @global WP_Post      $post             Global post object.
    19  * @global WP_Post      $post             Global post object.
    20  * @global string       $title
    20  * @global string       $title
    21  * @global array        $editor_styles
       
    22  * @global array        $wp_meta_boxes
    21  * @global array        $wp_meta_boxes
    23  */
    22  */
    24 global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes;
    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 ) );
    25 
    26 
    26 // Flag that we're loading the block editor.
    27 // Flag that we're loading the block editor.
    27 $current_screen = get_current_screen();
    28 $current_screen = get_current_screen();
    28 $current_screen->is_block_editor( true );
    29 $current_screen->is_block_editor( true );
    29 
    30 
       
    31 // Default to is-fullscreen-mode to avoid jumps in the UI.
       
    32 add_filter(
       
    33 	'admin_body_class',
       
    34 	function( $classes ) {
       
    35 		return "$classes is-fullscreen-mode";
       
    36 	}
       
    37 );
       
    38 
    30 /*
    39 /*
    31  * Emoji replacement is disabled for now, until it plays nicely with React.
    40  * Emoji replacement is disabled for now, until it plays nicely with React.
    32  */
    41  */
    33 remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
    42 remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
    34 
    43 
       
    44 /*
       
    45  * Block editor implements its own Options menu for toggling Document Panels.
       
    46  */
       
    47 add_filter( 'screen_options_show_screen', '__return_false' );
       
    48 
    35 wp_enqueue_script( 'heartbeat' );
    49 wp_enqueue_script( 'heartbeat' );
    36 wp_enqueue_script( 'wp-edit-post' );
    50 wp_enqueue_script( 'wp-edit-post' );
    37 wp_enqueue_script( 'wp-format-library' );
       
    38 
    51 
    39 $rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
    52 $rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
    40 
    53 
    41 // Preload common data.
    54 // Preload common data.
    42 $preload_paths = array(
    55 $preload_paths = array(
    50 	array( '/wp/v2/media', 'OPTIONS' ),
    63 	array( '/wp/v2/media', 'OPTIONS' ),
    51 	array( '/wp/v2/blocks', 'OPTIONS' ),
    64 	array( '/wp/v2/blocks', 'OPTIONS' ),
    52 	sprintf( '/wp/v2/%s/%d/autosaves?context=edit', $rest_base, $post->ID ),
    65 	sprintf( '/wp/v2/%s/%d/autosaves?context=edit', $rest_base, $post->ID ),
    53 );
    66 );
    54 
    67 
    55 /**
    68 block_editor_rest_api_preload( $preload_paths, $block_editor_context );
    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 
    69 
    89 wp_add_inline_script(
    70 wp_add_inline_script(
    90 	'wp-blocks',
    71 	'wp-blocks',
    91 	sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ),
    72 	sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ),
    92 	'after'
    73 	'after'
   123 		'meta-box-loader'       => true,
   104 		'meta-box-loader'       => true,
   124 		'meta-box-loader-nonce' => wp_create_nonce( 'meta-box-loader' ),
   105 		'meta-box-loader-nonce' => wp_create_nonce( 'meta-box-loader' ),
   125 	),
   106 	),
   126 	$meta_box_url
   107 	$meta_box_url
   127 );
   108 );
   128 wp_localize_script( 'wp-editor', '_wpMetaBoxUrl', $meta_box_url );
   109 wp_add_inline_script(
   129 
   110 	'wp-editor',
   130 
   111 	sprintf( 'var _wpMetaBoxUrl = %s;', wp_json_encode( $meta_box_url ) ),
   131 /*
   112 	'before'
   132  * Initialize the editor.
   113 );
   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 $gradient_presets   = current( (array) get_theme_support( 'editor-gradient-presets' ) );
       
   139 $custom_line_height = get_theme_support( 'custom-line-height' );
       
   140 $custom_units       = get_theme_support( 'custom-units' );
       
   141 
       
   142 /**
       
   143  * Filters the allowed block types for the editor, defaulting to true (all
       
   144  * block types supported).
       
   145  *
       
   146  * @since 5.0.0
       
   147  *
       
   148  * @param bool|array $allowed_block_types Array of block type slugs, or
       
   149  *                                        boolean to enable/disable all.
       
   150  * @param WP_Post    $post                The post resource data.
       
   151  */
       
   152 $allowed_block_types = apply_filters( 'allowed_block_types', true, $post );
       
   153 
   114 
   154 /*
   115 /*
   155  * Get all available templates for the post/page attributes meta-box.
   116  * Get all available templates for the post/page attributes meta-box.
   156  * The "Default template" array element should only be added if the array is
   117  * The "Default template" array element should only be added if the array is
   157  * not empty so we do not trigger the template select element without any options
   118  * not empty so we do not trigger the template select element without any options
   158  * besides the default value.
   119  * besides the default value.
   159  */
   120  */
   160 $available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) );
   121 $available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) );
   161 $available_templates = ! empty( $available_templates ) ? array_merge(
   122 $available_templates = ! empty( $available_templates ) ? array_replace(
   162 	array(
   123 	array(
   163 		/** This filter is documented in wp-admin/includes/meta-boxes.php */
   124 		/** This filter is documented in wp-admin/includes/meta-boxes.php */
   164 		'' => apply_filters( 'default_page_template_title', __( 'Default template' ), 'rest-api' ),
   125 		'' => apply_filters( 'default_page_template_title', __( 'Default template' ), 'rest-api' ),
   165 	),
   126 	),
   166 	$available_templates
   127 	$available_templates
   167 ) : $available_templates;
   128 ) : $available_templates;
   168 
       
   169 // Media settings.
       
   170 $max_upload_size = wp_max_upload_size();
       
   171 if ( ! $max_upload_size ) {
       
   172 	$max_upload_size = 0;
       
   173 }
       
   174 
       
   175 // Editor Styles.
       
   176 $styles = array(
       
   177 	array(
       
   178 		'css' => file_get_contents(
       
   179 			ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
       
   180 		),
       
   181 	),
       
   182 );
       
   183 
       
   184 /* translators: Use this to specify the CSS font family for the default font. */
       
   185 $locale_font_family = esc_html_x( 'Noto Serif', 'CSS Font Family for Editor Font' );
       
   186 $styles[]           = array(
       
   187 	'css' => "body { font-family: '$locale_font_family' }",
       
   188 );
       
   189 
       
   190 if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) {
       
   191 	foreach ( $editor_styles as $style ) {
       
   192 		if ( preg_match( '~^(https?:)?//~', $style ) ) {
       
   193 			$response = wp_remote_get( $style );
       
   194 			if ( ! is_wp_error( $response ) ) {
       
   195 				$styles[] = array(
       
   196 					'css' => wp_remote_retrieve_body( $response ),
       
   197 				);
       
   198 			}
       
   199 		} else {
       
   200 			$file = get_theme_file_path( $style );
       
   201 			if ( is_file( $file ) ) {
       
   202 				$styles[] = array(
       
   203 					'css'     => file_get_contents( $file ),
       
   204 					'baseURL' => get_theme_file_uri( $style ),
       
   205 				);
       
   206 			}
       
   207 		}
       
   208 	}
       
   209 }
       
   210 
       
   211 // Image sizes.
       
   212 
       
   213 /** This filter is documented in wp-admin/includes/media.php */
       
   214 $image_size_names = apply_filters(
       
   215 	'image_size_names_choose',
       
   216 	array(
       
   217 		'thumbnail' => __( 'Thumbnail' ),
       
   218 		'medium'    => __( 'Medium' ),
       
   219 		'large'     => __( 'Large' ),
       
   220 		'full'      => __( 'Full Size' ),
       
   221 	)
       
   222 );
       
   223 
       
   224 $available_image_sizes = array();
       
   225 foreach ( $image_size_names as $image_size_slug => $image_size_name ) {
       
   226 	$available_image_sizes[] = array(
       
   227 		'slug' => $image_size_slug,
       
   228 		'name' => $image_size_name,
       
   229 	);
       
   230 }
       
   231 
       
   232 $image_dimensions = array();
       
   233 $all_sizes        = wp_get_registered_image_subsizes();
       
   234 foreach ( $available_image_sizes as $size ) {
       
   235 	$key = $size['slug'];
       
   236 	if ( isset( $all_sizes[ $key ] ) ) {
       
   237 		$image_dimensions[ $key ] = $all_sizes[ $key ];
       
   238 	}
       
   239 }
       
   240 
   129 
   241 // Lock settings.
   130 // Lock settings.
   242 $user_id = wp_check_post_lock( $post->ID );
   131 $user_id = wp_check_post_lock( $post->ID );
   243 if ( $user_id ) {
   132 if ( $user_id ) {
   244 	$locked = false;
   133 	$locked = false;
   276 
   165 
   277 /**
   166 /**
   278  * Filters the body placeholder text.
   167  * Filters the body placeholder text.
   279  *
   168  *
   280  * @since 5.0.0
   169  * @since 5.0.0
   281  *
   170  * @since 5.8.0 Changed the default placeholder text.
   282  * @param string  $text Placeholder text. Default 'Start writing or type / to choose a block'.
   171  *
       
   172  * @param string  $text Placeholder text. Default 'Type / to choose a block'.
   283  * @param WP_Post $post Post object.
   173  * @param WP_Post $post Post object.
   284  */
   174  */
   285 $body_placeholder = apply_filters( 'write_your_story', __( 'Start writing or type / to choose a block' ), $post );
   175 $body_placeholder = apply_filters( 'write_your_story', __( 'Type / to choose a block' ), $post );
   286 
   176 
   287 $editor_settings = array(
   177 $editor_settings = array(
   288 	'alignWide'                            => $align_wide,
       
   289 	'availableTemplates'                   => $available_templates,
   178 	'availableTemplates'                   => $available_templates,
   290 	'allowedBlockTypes'                    => $allowed_block_types,
       
   291 	'disableCustomColors'                  => get_theme_support( 'disable-custom-colors' ),
       
   292 	'disableCustomFontSizes'               => get_theme_support( 'disable-custom-font-sizes' ),
       
   293 	'disableCustomGradients'               => get_theme_support( 'disable-custom-gradients' ),
       
   294 	'disablePostFormats'                   => ! current_theme_supports( 'post-formats' ),
   179 	'disablePostFormats'                   => ! current_theme_supports( 'post-formats' ),
   295 	/** This filter is documented in wp-admin/edit-form-advanced.php */
   180 	/** This filter is documented in wp-admin/edit-form-advanced.php */
   296 	'titlePlaceholder'                     => apply_filters( 'enter_title_here', __( 'Add title' ), $post ),
   181 	'titlePlaceholder'                     => apply_filters( 'enter_title_here', __( 'Add title' ), $post ),
   297 	'bodyPlaceholder'                      => $body_placeholder,
   182 	'bodyPlaceholder'                      => $body_placeholder,
   298 	'isRTL'                                => is_rtl(),
       
   299 	'autosaveInterval'                     => AUTOSAVE_INTERVAL,
   183 	'autosaveInterval'                     => AUTOSAVE_INTERVAL,
   300 	'maxUploadFileSize'                    => $max_upload_size,
   184 	'styles'                               => get_block_editor_theme_styles(),
   301 	'allowedMimeTypes'                     => get_allowed_mime_types(),
       
   302 	'styles'                               => $styles,
       
   303 	'imageSizes'                           => $available_image_sizes,
       
   304 	'imageDimensions'                      => $image_dimensions,
       
   305 	'richEditingEnabled'                   => user_can_richedit(),
   185 	'richEditingEnabled'                   => user_can_richedit(),
   306 	'postLock'                             => $lock_details,
   186 	'postLock'                             => $lock_details,
   307 	'postLockUtils'                        => array(
   187 	'postLockUtils'                        => array(
   308 		'nonce'       => wp_create_nonce( 'lock-post_' . $post->ID ),
   188 		'nonce'       => wp_create_nonce( 'lock-post_' . $post->ID ),
   309 		'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ),
   189 		'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ),
   310 		'ajaxUrl'     => admin_url( 'admin-ajax.php' ),
   190 		'ajaxUrl'     => admin_url( 'admin-ajax.php' ),
   311 	),
   191 	),
       
   192 	'supportsLayout'                       => WP_Theme_JSON_Resolver::theme_has_support(),
   312 	'__experimentalBlockPatterns'          => WP_Block_Patterns_Registry::get_instance()->get_all_registered(),
   193 	'__experimentalBlockPatterns'          => WP_Block_Patterns_Registry::get_instance()->get_all_registered(),
   313 	'__experimentalBlockPatternCategories' => WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(),
   194 	'__experimentalBlockPatternCategories' => WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(),
       
   195 	'supportsTemplateMode'                 => current_theme_supports( 'block-templates' ),
   314 
   196 
   315 	// Whether or not to load the 'postcustom' meta box is stored as a user meta
   197 	// Whether or not to load the 'postcustom' meta box is stored as a user meta
   316 	// field so that we're not always loading its assets.
   198 	// field so that we're not always loading its assets.
   317 	'enableCustomFields'                   => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
   199 	'enableCustomFields'                   => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
   318 	'enableCustomLineHeight'               => $custom_line_height,
   200 );
   319 	'enableCustomUnits'                    => $custom_units,
   201 
   320 );
   202 $autosave = wp_get_post_autosave( $post->ID );
   321 
       
   322 $autosave = wp_get_post_autosave( $post_ID );
       
   323 if ( $autosave ) {
   203 if ( $autosave ) {
   324 	if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
   204 	if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
   325 		$editor_settings['autosave'] = array(
   205 		$editor_settings['autosave'] = array(
   326 			'editLink' => get_edit_post_link( $autosave->ID ),
   206 			'editLink' => get_edit_post_link( $autosave->ID ),
   327 		);
   207 		);
   328 	} else {
   208 	} else {
   329 		wp_delete_post_revision( $autosave->ID );
   209 		wp_delete_post_revision( $autosave->ID );
   330 	}
   210 	}
   331 }
   211 }
   332 
   212 
   333 if ( false !== $color_palette ) {
       
   334 	$editor_settings['colors'] = $color_palette;
       
   335 }
       
   336 
       
   337 if ( false !== $font_sizes ) {
       
   338 	$editor_settings['fontSizes'] = $font_sizes;
       
   339 }
       
   340 
       
   341 if ( false !== $gradient_presets ) {
       
   342 	$editor_settings['gradients'] = $gradient_presets;
       
   343 }
       
   344 
       
   345 if ( ! empty( $post_type_object->template ) ) {
   213 if ( ! empty( $post_type_object->template ) ) {
   346 	$editor_settings['template']     = $post_type_object->template;
   214 	$editor_settings['template']     = $post_type_object->template;
   347 	$editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false;
   215 	$editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false;
   348 }
   216 }
   349 
   217 
   364 	)
   232 	)
   365 );
   233 );
   366 wp_tinymce_inline_scripts();
   234 wp_tinymce_inline_scripts();
   367 wp_enqueue_editor();
   235 wp_enqueue_editor();
   368 
   236 
   369 
       
   370 /**
   237 /**
   371  * Styles
   238  * Styles
   372  */
   239  */
   373 wp_enqueue_style( 'wp-edit-post' );
   240 wp_enqueue_style( 'wp-edit-post' );
   374 wp_enqueue_style( 'wp-format-library' );
       
   375 
   241 
   376 /**
   242 /**
   377  * Fires after block assets have been enqueued for the editing interface.
   243  * Fires after block assets have been enqueued for the editing interface.
   378  *
   244  *
   379  * Call `add_action` on any hook before 'admin_enqueue_scripts'.
   245  * Call `add_action` on any hook before 'admin_enqueue_scripts'.
   393 $core_meta_boxes = $wp_meta_boxes[ $current_screen->id ]['normal']['core'];
   259 $core_meta_boxes = $wp_meta_boxes[ $current_screen->id ]['normal']['core'];
   394 if ( ! isset( $core_meta_boxes['postcustom'] ) || ! $core_meta_boxes['postcustom'] ) {
   260 if ( ! isset( $core_meta_boxes['postcustom'] ) || ! $core_meta_boxes['postcustom'] ) {
   395 	unset( $editor_settings['enableCustomFields'] );
   261 	unset( $editor_settings['enableCustomFields'] );
   396 }
   262 }
   397 
   263 
   398 /**
   264 $editor_settings = get_block_editor_settings( $editor_settings, $block_editor_context );
   399  * Filters the settings to pass to the block editor.
       
   400  *
       
   401  * @since 5.0.0
       
   402  *
       
   403  * @param array   $editor_settings Default editor settings.
       
   404  * @param WP_Post $post            Post being edited.
       
   405  */
       
   406 $editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );
       
   407 
   265 
   408 $init_script = <<<JS
   266 $init_script = <<<JS
   409 ( function() {
   267 ( function() {
   410 	window._wpLoadBlockEditor = new Promise( function( resolve ) {
   268 	window._wpLoadBlockEditor = new Promise( function( resolve ) {
   411 		wp.domReady( function() {
   269 		wp.domReady( function() {
   421 	$post->ID,
   279 	$post->ID,
   422 	wp_json_encode( $editor_settings ),
   280 	wp_json_encode( $editor_settings ),
   423 	wp_json_encode( $initial_edits )
   281 	wp_json_encode( $initial_edits )
   424 );
   282 );
   425 wp_add_inline_script( 'wp-edit-post', $script );
   283 wp_add_inline_script( 'wp-edit-post', $script );
       
   284 
       
   285 if ( (int) get_option( 'page_for_posts' ) === $post->ID ) {
       
   286 	add_action( 'admin_enqueue_scripts', '_wp_block_editor_posts_page_notice' );
       
   287 }
   426 
   288 
   427 require_once ABSPATH . 'wp-admin/admin-header.php';
   289 require_once ABSPATH . 'wp-admin/admin-header.php';
   428 ?>
   290 ?>
   429 
   291 
   430 <div class="block-editor">
   292 <div class="block-editor">