wp/wp-admin/edit-form-blocks.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * The Block Editor page.
     3  * The block editor page.
     4  *
     4  *
     5  * @since 5.0.0
     5  * @since 5.0.0
     6  *
     6  *
     7  * @package WordPress
     7  * @package WordPress
     8  * @subpackage Administration
     8  * @subpackage Administration
     9  */
     9  */
    10 
    10 
    11 // don't load directly
    11 // Don't load directly.
    12 if ( ! defined( 'ABSPATH' ) ) {
    12 if ( ! defined( 'ABSPATH' ) ) {
    13 	die( '-1' );
    13 	die( '-1' );
    14 }
    14 }
    15 
    15 
    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
    19  * @global WP_Post      $post             Global post object.
    20  * @global string       $title
    20  * @global string       $title
    21  * @global array        $editor_styles
    21  * @global array        $editor_styles
    22  * @global array        $wp_meta_boxes
    22  * @global array        $wp_meta_boxes
    23  */
    23  */
    24 global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes;
    24 global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes;
    47 	sprintf( '/wp/v2/%s/%s?context=edit', $rest_base, $post->ID ),
    47 	sprintf( '/wp/v2/%s/%s?context=edit', $rest_base, $post->ID ),
    48 	sprintf( '/wp/v2/types/%s?context=edit', $post_type ),
    48 	sprintf( '/wp/v2/types/%s?context=edit', $post_type ),
    49 	sprintf( '/wp/v2/users/me?post_type=%s&context=edit', $post_type ),
    49 	sprintf( '/wp/v2/users/me?post_type=%s&context=edit', $post_type ),
    50 	array( '/wp/v2/media', 'OPTIONS' ),
    50 	array( '/wp/v2/media', 'OPTIONS' ),
    51 	array( '/wp/v2/blocks', 'OPTIONS' ),
    51 	array( '/wp/v2/blocks', 'OPTIONS' ),
       
    52 	sprintf( '/wp/v2/%s/%d/autosaves?context=edit', $rest_base, $post->ID ),
    52 );
    53 );
    53 
    54 
    54 /**
    55 /**
    55  * Preload common data by specifying an array of REST API paths that will be preloaded.
    56  * Preload common data by specifying an array of REST API paths that will be preloaded.
    56  *
    57  *
    57  * Filters the array of paths that will be preloaded.
    58  * Filters the array of paths that will be preloaded.
    58  *
    59  *
    59  * @since 5.0.0
    60  * @since 5.0.0
    60  *
    61  *
    61  * @param array  $preload_paths Array of paths to preload.
    62  * @param string[] $preload_paths Array of paths to preload.
    62  * @param object $post          The post resource data.
    63  * @param WP_Post  $post          Post being edited.
    63  */
    64  */
    64 $preload_paths = apply_filters( 'block_editor_preload_paths', $preload_paths, $post );
    65 $preload_paths = apply_filters( 'block_editor_preload_paths', $preload_paths, $post );
    65 
    66 
    66 /*
    67 /*
    67  * Ensure the global $post remains the same after API data is preloaded.
    68  * Ensure the global $post remains the same after API data is preloaded.
   129 
   130 
   130 /*
   131 /*
   131  * Initialize the editor.
   132  * Initialize the editor.
   132  */
   133  */
   133 
   134 
   134 $align_wide    = get_theme_support( 'align-wide' );
   135 $align_wide         = get_theme_support( 'align-wide' );
   135 $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
   136 $color_palette      = current( (array) get_theme_support( 'editor-color-palette' ) );
   136 $font_sizes    = current( (array) get_theme_support( 'editor-font-sizes' ) );
   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' );
   137 
   141 
   138 /**
   142 /**
   139  * Filters the allowed block types for the editor, defaulting to true (all
   143  * Filters the allowed block types for the editor, defaulting to true (all
   140  * block types supported).
   144  * block types supported).
   141  *
   145  *
   142  * @since 5.0.0
   146  * @since 5.0.0
   143  *
   147  *
   144  * @param bool|array $allowed_block_types Array of block type slugs, or
   148  * @param bool|array $allowed_block_types Array of block type slugs, or
   145  *                                        boolean to enable/disable all.
   149  *                                        boolean to enable/disable all.
   146  * @param object $post                    The post resource data.
   150  * @param WP_Post    $post                The post resource data.
   147  */
   151  */
   148 $allowed_block_types = apply_filters( 'allowed_block_types', true, $post );
   152 $allowed_block_types = apply_filters( 'allowed_block_types', true, $post );
   149 
   153 
   150 // Get all available templates for the post/page attributes meta-box.
   154 /*
   151 // The "Default template" array element should only be added if the array is
   155  * Get all available templates for the post/page attributes meta-box.
   152 // not empty so we do not trigger the template select element without any options
   156  * The "Default template" array element should only be added if the array is
   153 // besides the default value.
   157  * not empty so we do not trigger the template select element without any options
       
   158  * besides the default value.
       
   159  */
   154 $available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) );
   160 $available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) );
   155 $available_templates = ! empty( $available_templates ) ? array_merge(
   161 $available_templates = ! empty( $available_templates ) ? array_merge(
   156 	array(
   162 	array(
   157 		/** This filter is documented in wp-admin/includes/meta-boxes.php */
   163 		/** This filter is documented in wp-admin/includes/meta-boxes.php */
   158 		'' => apply_filters( 'default_page_template_title', __( 'Default template' ), 'rest-api' ),
   164 		'' => apply_filters( 'default_page_template_title', __( 'Default template' ), 'rest-api' ),
   173 			ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
   179 			ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
   174 		),
   180 		),
   175 	),
   181 	),
   176 );
   182 );
   177 
   183 
   178 /* Translators: Use this to specify the CSS font family for the default font */
   184 /* 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' );
   185 $locale_font_family = esc_html_x( 'Noto Serif', 'CSS Font Family for Editor Font' );
   180 $styles[]           = array(
   186 $styles[]           = array(
   181 	'css' => "body { font-family: '$locale_font_family' }",
   187 	'css' => "body { font-family: '$locale_font_family' }",
   182 );
   188 );
   183 
   189 
   221 		'slug' => $image_size_slug,
   227 		'slug' => $image_size_slug,
   222 		'name' => $image_size_name,
   228 		'name' => $image_size_name,
   223 	);
   229 	);
   224 }
   230 }
   225 
   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 
   226 // Lock settings.
   241 // Lock settings.
   227 $user_id = wp_check_post_lock( $post->ID );
   242 $user_id = wp_check_post_lock( $post->ID );
   228 if ( $user_id ) {
   243 if ( $user_id ) {
   229 	$locked = false;
   244 	$locked = false;
   230 
   245 
   247 		'user'     => $user_details,
   262 		'user'     => $user_details,
   248 	);
   263 	);
   249 } else {
   264 } else {
   250 	// Lock the post.
   265 	// Lock the post.
   251 	$active_post_lock = wp_set_post_lock( $post->ID );
   266 	$active_post_lock = wp_set_post_lock( $post->ID );
   252 	$lock_details     = array(
   267 	if ( $active_post_lock ) {
       
   268 		$active_post_lock = esc_attr( implode( ':', $active_post_lock ) );
       
   269 	}
       
   270 
       
   271 	$lock_details = array(
   253 		'isLocked'       => false,
   272 		'isLocked'       => false,
   254 		'activePostLock' => esc_attr( implode( ':', $active_post_lock ) ),
   273 		'activePostLock' => $active_post_lock,
   255 	);
   274 	);
   256 }
   275 }
   257 
   276 
   258 /**
   277 /**
   259  * Filters the body placeholder text.
   278  * Filters the body placeholder text.
   264  * @param WP_Post $post Post object.
   283  * @param WP_Post $post Post object.
   265  */
   284  */
   266 $body_placeholder = apply_filters( 'write_your_story', __( 'Start writing or type / to choose a block' ), $post );
   285 $body_placeholder = apply_filters( 'write_your_story', __( 'Start writing or type / to choose a block' ), $post );
   267 
   286 
   268 $editor_settings = array(
   287 $editor_settings = array(
   269 	'alignWide'              => $align_wide,
   288 	'alignWide'                            => $align_wide,
   270 	'availableTemplates'     => $available_templates,
   289 	'availableTemplates'                   => $available_templates,
   271 	'allowedBlockTypes'      => $allowed_block_types,
   290 	'allowedBlockTypes'                    => $allowed_block_types,
   272 	'disableCustomColors'    => get_theme_support( 'disable-custom-colors' ),
   291 	'disableCustomColors'                  => get_theme_support( 'disable-custom-colors' ),
   273 	'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
   292 	'disableCustomFontSizes'               => get_theme_support( 'disable-custom-font-sizes' ),
   274 	'disablePostFormats'     => ! current_theme_supports( 'post-formats' ),
   293 	'disableCustomGradients'               => get_theme_support( 'disable-custom-gradients' ),
       
   294 	'disablePostFormats'                   => ! current_theme_supports( 'post-formats' ),
   275 	/** This filter is documented in wp-admin/edit-form-advanced.php */
   295 	/** This filter is documented in wp-admin/edit-form-advanced.php */
   276 	'titlePlaceholder'       => apply_filters( 'enter_title_here', __( 'Add title' ), $post ),
   296 	'titlePlaceholder'                     => apply_filters( 'enter_title_here', __( 'Add title' ), $post ),
   277 	'bodyPlaceholder'        => $body_placeholder,
   297 	'bodyPlaceholder'                      => $body_placeholder,
   278 	'isRTL'                  => is_rtl(),
   298 	'isRTL'                                => is_rtl(),
   279 	'autosaveInterval'       => AUTOSAVE_INTERVAL,
   299 	'autosaveInterval'                     => AUTOSAVE_INTERVAL,
   280 	'maxUploadFileSize'      => $max_upload_size,
   300 	'maxUploadFileSize'                    => $max_upload_size,
   281 	'allowedMimeTypes'       => get_allowed_mime_types(),
   301 	'allowedMimeTypes'                     => get_allowed_mime_types(),
   282 	'styles'                 => $styles,
   302 	'styles'                               => $styles,
   283 	'imageSizes'             => $available_image_sizes,
   303 	'imageSizes'                           => $available_image_sizes,
   284 	'richEditingEnabled'     => user_can_richedit(),
   304 	'imageDimensions'                      => $image_dimensions,
   285 	'postLock'               => $lock_details,
   305 	'richEditingEnabled'                   => user_can_richedit(),
   286 	'postLockUtils'          => array(
   306 	'postLock'                             => $lock_details,
       
   307 	'postLockUtils'                        => array(
   287 		'nonce'       => wp_create_nonce( 'lock-post_' . $post->ID ),
   308 		'nonce'       => wp_create_nonce( 'lock-post_' . $post->ID ),
   288 		'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ),
   309 		'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ),
   289 		'ajaxUrl'     => admin_url( 'admin-ajax.php' ),
   310 		'ajaxUrl'     => admin_url( 'admin-ajax.php' ),
   290 	),
   311 	),
       
   312 	'__experimentalBlockPatterns'          => WP_Block_Patterns_Registry::get_instance()->get_all_registered(),
       
   313 	'__experimentalBlockPatternCategories' => WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(),
   291 
   314 
   292 	// Whether or not to load the 'postcustom' meta box is stored as a user meta
   315 	// 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.
   316 	// field so that we're not always loading its assets.
   294 	'enableCustomFields'     => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
   317 	'enableCustomFields'                   => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
       
   318 	'enableCustomLineHeight'               => $custom_line_height,
       
   319 	'enableCustomUnits'                    => $custom_units,
   295 );
   320 );
   296 
   321 
   297 $autosave = wp_get_post_autosave( $post_ID );
   322 $autosave = wp_get_post_autosave( $post_ID );
   298 if ( $autosave ) {
   323 if ( $autosave ) {
   299 	if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
   324 	if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
   311 
   336 
   312 if ( false !== $font_sizes ) {
   337 if ( false !== $font_sizes ) {
   313 	$editor_settings['fontSizes'] = $font_sizes;
   338 	$editor_settings['fontSizes'] = $font_sizes;
   314 }
   339 }
   315 
   340 
       
   341 if ( false !== $gradient_presets ) {
       
   342 	$editor_settings['gradients'] = $gradient_presets;
       
   343 }
       
   344 
   316 if ( ! empty( $post_type_object->template ) ) {
   345 if ( ! empty( $post_type_object->template ) ) {
   317 	$editor_settings['template']     = $post_type_object->template;
   346 	$editor_settings['template']     = $post_type_object->template;
   318 	$editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false;
   347 	$editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false;
   319 }
   348 }
   320 
   349 
   335 	)
   364 	)
   336 );
   365 );
   337 wp_tinymce_inline_scripts();
   366 wp_tinymce_inline_scripts();
   338 wp_enqueue_editor();
   367 wp_enqueue_editor();
   339 
   368 
       
   369 
   340 /**
   370 /**
   341  * Styles
   371  * Styles
   342  */
   372  */
   343 wp_enqueue_style( 'wp-edit-post' );
   373 wp_enqueue_style( 'wp-edit-post' );
   344 wp_enqueue_style( 'wp-format-library' );
   374 wp_enqueue_style( 'wp-format-library' );
   354  * @since 5.0.0
   384  * @since 5.0.0
   355  */
   385  */
   356 do_action( 'enqueue_block_editor_assets' );
   386 do_action( 'enqueue_block_editor_assets' );
   357 
   387 
   358 // In order to duplicate classic meta box behaviour, we need to run the classic meta box actions.
   388 // 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' );
   389 require_once ABSPATH . 'wp-admin/includes/meta-boxes.php';
   360 register_and_do_post_meta_boxes( $post );
   390 register_and_do_post_meta_boxes( $post );
   361 
   391 
   362 // Check if the Custom Fields meta box has been removed at some point.
   392 // 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'];
   393 $core_meta_boxes = $wp_meta_boxes[ $current_screen->id ]['normal']['core'];
   364 if ( ! isset( $core_meta_boxes['postcustom'] ) || ! $core_meta_boxes['postcustom'] ) {
   394 if ( ! isset( $core_meta_boxes['postcustom'] ) || ! $core_meta_boxes['postcustom'] ) {
   392 	wp_json_encode( $editor_settings ),
   422 	wp_json_encode( $editor_settings ),
   393 	wp_json_encode( $initial_edits )
   423 	wp_json_encode( $initial_edits )
   394 );
   424 );
   395 wp_add_inline_script( 'wp-edit-post', $script );
   425 wp_add_inline_script( 'wp-edit-post', $script );
   396 
   426 
   397 require_once( ABSPATH . 'wp-admin/admin-header.php' );
   427 require_once ABSPATH . 'wp-admin/admin-header.php';
   398 ?>
   428 ?>
   399 
   429 
   400 <div class="block-editor">
   430 <div class="block-editor">
   401 	<h1 class="screen-reader-text hide-if-no-js"><?php echo esc_html( $title ); ?></h1>
   431 	<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>
   432 	<div id="editor" class="block-editor__container hide-if-no-js"></div>
   409 		<h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
   439 		<h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
   410 		<div class="notice notice-error notice-alt">
   440 		<div class="notice notice-error notice-alt">
   411 			<p>
   441 			<p>
   412 				<?php
   442 				<?php
   413 					$message = sprintf(
   443 					$message = sprintf(
   414 						/* translators: %s: Classic Editor plugin URL */
   444 						/* translators: %s: A link to install the Classic Editor plugin. */
   415 						__( 'The block editor requires JavaScript. Please enable JavaScript in your browser settings, or try the <a href="%s">Classic Editor plugin</a>.' ),
   445 						__( '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/' )
   446 						esc_url( wp_nonce_url( self_admin_url( 'plugin-install.php?tab=favorites&user=wordpressdotorg&save=0' ), 'save_wporg_username_' . get_current_user_id() ) )
   417 					);
   447 					);
   418 
   448 
   419 					/**
   449 					/**
   420 					 * Filters the message displayed in the block editor interface when JavaScript is
   450 					 * Filters the message displayed in the block editor interface when JavaScript is
   421 					 * not enabled in the browser.
   451 					 * not enabled in the browser.