wp/wp-includes/block-editor.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    81 	 *
    81 	 *
    82 	 * @param array[]                 $block_categories     Array of categories for block types.
    82 	 * @param array[]                 $block_categories     Array of categories for block types.
    83 	 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
    83 	 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
    84 	 */
    84 	 */
    85 	$block_categories = apply_filters( 'block_categories_all', $block_categories, $block_editor_context );
    85 	$block_categories = apply_filters( 'block_categories_all', $block_categories, $block_editor_context );
       
    86 
    86 	if ( ! empty( $block_editor_context->post ) ) {
    87 	if ( ! empty( $block_editor_context->post ) ) {
    87 		$post = $block_editor_context->post;
    88 		$post = $block_editor_context->post;
    88 
    89 
    89 		/**
    90 		/**
    90 		 * Filters the default array of categories for block types.
    91 		 * Filters the default array of categories for block types.
   106  *
   107  *
   107  * @since 5.8.0
   108  * @since 5.8.0
   108  *
   109  *
   109  * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
   110  * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
   110  *
   111  *
   111  * @return bool|array Array of block type slugs, or boolean to enable/disable all.
   112  * @return bool|string[] Array of block type slugs, or boolean to enable/disable all.
   112  */
   113  */
   113 function get_allowed_block_types( $block_editor_context ) {
   114 function get_allowed_block_types( $block_editor_context ) {
   114 	$allowed_block_types = true;
   115 	$allowed_block_types = true;
   115 
   116 
   116 	/**
   117 	/**
   117 	 * Filters the allowed block types for all editor types.
   118 	 * Filters the allowed block types for all editor types.
   118 	 *
   119 	 *
   119 	 * @since 5.8.0
   120 	 * @since 5.8.0
   120 	 *
   121 	 *
   121 	 * @param bool|array              $allowed_block_types  Array of block type slugs, or boolean to enable/disable all.
   122 	 * @param bool|string[]           $allowed_block_types  Array of block type slugs, or boolean to enable/disable all.
   122 	 *                                                      Default true (all registered block types supported).
   123 	 *                                                      Default true (all registered block types supported).
   123 	 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
   124 	 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
   124 	 */
   125 	 */
   125 	$allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $block_editor_context );
   126 	$allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $block_editor_context );
       
   127 
   126 	if ( ! empty( $block_editor_context->post ) ) {
   128 	if ( ! empty( $block_editor_context->post ) ) {
   127 		$post = $block_editor_context->post;
   129 		$post = $block_editor_context->post;
   128 
   130 
   129 		/**
   131 		/**
   130 		 * Filters the allowed block types for the editor.
   132 		 * Filters the allowed block types for the editor.
   131 		 *
   133 		 *
   132 		 * @since 5.0.0
   134 		 * @since 5.0.0
   133 		 * @deprecated 5.8.0 Use the {@see 'allowed_block_types_all'} filter instead.
   135 		 * @deprecated 5.8.0 Use the {@see 'allowed_block_types_all'} filter instead.
   134 		 *
   136 		 *
   135 		 * @param bool|array $allowed_block_types Array of block type slugs, or boolean to enable/disable all.
   137 		 * @param bool|string[] $allowed_block_types Array of block type slugs, or boolean to enable/disable all.
   136 		 *                                        Default true (all registered block types supported)
   138 		 *                                           Default true (all registered block types supported)
   137 		 * @param WP_Post    $post                The post resource data.
   139 		 * @param WP_Post       $post                The post resource data.
   138 		 */
   140 		 */
   139 		$allowed_block_types = apply_filters_deprecated( 'allowed_block_types', array( $allowed_block_types, $post ), '5.8.0', 'allowed_block_types_all' );
   141 		$allowed_block_types = apply_filters_deprecated( 'allowed_block_types', array( $allowed_block_types, $post ), '5.8.0', 'allowed_block_types_all' );
   140 	}
   142 	}
   141 
   143 
   142 	return $allowed_block_types;
   144 	return $allowed_block_types;
   185 		if ( isset( $all_sizes[ $key ] ) ) {
   187 		if ( isset( $all_sizes[ $key ] ) ) {
   186 			$image_dimensions[ $key ] = $all_sizes[ $key ];
   188 			$image_dimensions[ $key ] = $all_sizes[ $key ];
   187 		}
   189 		}
   188 	}
   190 	}
   189 
   191 
       
   192 	// These styles are used if the "no theme styles" options is triggered or on
       
   193 	// themes without their own editor styles.
       
   194 	$default_editor_styles_file = ABSPATH . WPINC . '/css/dist/block-editor/default-editor-styles.css';
       
   195 	if ( file_exists( $default_editor_styles_file ) ) {
       
   196 		$default_editor_styles = array(
       
   197 			array( 'css' => file_get_contents( $default_editor_styles_file ) ),
       
   198 		);
       
   199 	} else {
       
   200 		$default_editor_styles = array();
       
   201 	}
       
   202 
   190 	$editor_settings = array(
   203 	$editor_settings = array(
   191 		'alignWide'              => get_theme_support( 'align-wide' ),
   204 		'alignWide'                        => get_theme_support( 'align-wide' ),
   192 		'allowedBlockTypes'      => true,
   205 		'allowedBlockTypes'                => true,
   193 		'allowedMimeTypes'       => get_allowed_mime_types(),
   206 		'allowedMimeTypes'                 => get_allowed_mime_types(),
   194 		'blockCategories'        => get_default_block_categories(),
   207 		'defaultEditorStyles'              => $default_editor_styles,
   195 		'disableCustomColors'    => get_theme_support( 'disable-custom-colors' ),
   208 		'blockCategories'                  => get_default_block_categories(),
   196 		'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
   209 		'disableCustomColors'              => get_theme_support( 'disable-custom-colors' ),
   197 		'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ),
   210 		'disableCustomFontSizes'           => get_theme_support( 'disable-custom-font-sizes' ),
   198 		'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ),
   211 		'disableCustomGradients'           => get_theme_support( 'disable-custom-gradients' ),
   199 		'enableCustomSpacing'    => get_theme_support( 'custom-spacing' ),
   212 		'enableCustomLineHeight'           => get_theme_support( 'custom-line-height' ),
   200 		'enableCustomUnits'      => get_theme_support( 'custom-units' ),
   213 		'enableCustomSpacing'              => get_theme_support( 'custom-spacing' ),
   201 		'isRTL'                  => is_rtl(),
   214 		'enableCustomUnits'                => get_theme_support( 'custom-units' ),
   202 		'imageDefaultSize'       => $image_default_size,
   215 		'isRTL'                            => is_rtl(),
   203 		'imageDimensions'        => $image_dimensions,
   216 		'imageDefaultSize'                 => $image_default_size,
   204 		'imageEditing'           => true,
   217 		'imageDimensions'                  => $image_dimensions,
   205 		'imageSizes'             => $available_image_sizes,
   218 		'imageEditing'                     => true,
   206 		'maxUploadFileSize'      => $max_upload_size,
   219 		'imageSizes'                       => $available_image_sizes,
       
   220 		'maxUploadFileSize'                => $max_upload_size,
       
   221 		// The following flag is required to enable the new Gallery block format on the mobile apps in 5.9.
       
   222 		'__unstableGalleryWithImageBlocks' => true,
   207 	);
   223 	);
   208 
   224 
   209 	// Theme settings.
   225 	// Theme settings.
   210 	$color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
   226 	$color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
   211 	if ( false !== $color_palette ) {
   227 	if ( false !== $color_palette ) {
   242 	 *
   258 	 *
   243 	 * Returning an empty array will make all widgets available.
   259 	 * Returning an empty array will make all widgets available.
   244 	 *
   260 	 *
   245 	 * @since 5.8.0
   261 	 * @since 5.8.0
   246 	 *
   262 	 *
   247 	 * @param array $widgets An array of excluded widget-type IDs.
   263 	 * @param string[] $widgets An array of excluded widget-type IDs.
   248 	 */
   264 	 */
   249 	$editor_settings['widgetTypesToHideFromLegacyWidgetBlock'] = apply_filters(
   265 	$editor_settings['widgetTypesToHideFromLegacyWidgetBlock'] = apply_filters(
   250 		'widget_types_to_hide_from_legacy_widget_block',
   266 		'widget_types_to_hide_from_legacy_widget_block',
   251 		array(
   267 		array(
   252 			'pages',
   268 			'pages',
   270 
   286 
   271 	return $editor_settings;
   287 	return $editor_settings;
   272 }
   288 }
   273 
   289 
   274 /**
   290 /**
       
   291  * Collect the block editor assets that need to be loaded into the editor's iframe.
       
   292  *
       
   293  * @since 6.0.0
       
   294  * @access private
       
   295  *
       
   296  * @global string $pagenow The filename of the current screen.
       
   297  *
       
   298  * @return array {
       
   299  *     The block editor assets.
       
   300  *
       
   301  *     @type string|false $styles  String containing the HTML for styles.
       
   302  *     @type string|false $scripts String containing the HTML for scripts.
       
   303  * }
       
   304  */
       
   305 function _wp_get_iframed_editor_assets() {
       
   306 	global $pagenow;
       
   307 
       
   308 	$script_handles = array();
       
   309 	$style_handles  = array(
       
   310 		'wp-block-editor',
       
   311 		'wp-block-library',
       
   312 		'wp-edit-blocks',
       
   313 	);
       
   314 
       
   315 	if ( current_theme_supports( 'wp-block-styles' ) ) {
       
   316 		$style_handles[] = 'wp-block-library-theme';
       
   317 	}
       
   318 
       
   319 	if ( 'widgets.php' === $pagenow || 'customize.php' === $pagenow ) {
       
   320 		$style_handles[] = 'wp-widgets';
       
   321 		$style_handles[] = 'wp-edit-widgets';
       
   322 	}
       
   323 
       
   324 	$block_registry = WP_Block_Type_Registry::get_instance();
       
   325 
       
   326 	foreach ( $block_registry->get_all_registered() as $block_type ) {
       
   327 		if ( ! empty( $block_type->style ) ) {
       
   328 			$style_handles[] = $block_type->style;
       
   329 		}
       
   330 
       
   331 		if ( ! empty( $block_type->editor_style ) ) {
       
   332 			$style_handles[] = $block_type->editor_style;
       
   333 		}
       
   334 
       
   335 		if ( ! empty( $block_type->script ) ) {
       
   336 			$script_handles[] = $block_type->script;
       
   337 		}
       
   338 	}
       
   339 
       
   340 	$style_handles = array_unique( $style_handles );
       
   341 	$done          = wp_styles()->done;
       
   342 
       
   343 	ob_start();
       
   344 
       
   345 	// We do not need reset styles for the iframed editor.
       
   346 	wp_styles()->done = array( 'wp-reset-editor-styles' );
       
   347 	wp_styles()->do_items( $style_handles );
       
   348 	wp_styles()->done = $done;
       
   349 
       
   350 	$styles = ob_get_clean();
       
   351 
       
   352 	$script_handles = array_unique( $script_handles );
       
   353 	$done           = wp_scripts()->done;
       
   354 
       
   355 	ob_start();
       
   356 
       
   357 	wp_scripts()->done = array();
       
   358 	wp_scripts()->do_items( $script_handles );
       
   359 	wp_scripts()->done = $done;
       
   360 
       
   361 	$scripts = ob_get_clean();
       
   362 
       
   363 	return array(
       
   364 		'styles'  => $styles,
       
   365 		'scripts' => $scripts,
       
   366 	);
       
   367 }
       
   368 
       
   369 /**
   275  * Returns the contextualized block editor settings for a selected editor context.
   370  * Returns the contextualized block editor settings for a selected editor context.
   276  *
   371  *
   277  * @since 5.8.0
   372  * @since 5.8.0
   278  *
   373  *
   279  * @param array                   $custom_settings      Custom settings to use with the given editor type.
   374  * @param array                   $custom_settings      Custom settings to use with the given editor type.
   289 			'blockCategories'   => get_block_categories( $block_editor_context ),
   384 			'blockCategories'   => get_block_categories( $block_editor_context ),
   290 		),
   385 		),
   291 		$custom_settings
   386 		$custom_settings
   292 	);
   387 	);
   293 
   388 
   294 	$theme_json = WP_Theme_JSON_Resolver::get_merged_data( $editor_settings );
   389 	$global_styles = array();
       
   390 	$presets       = array(
       
   391 		array(
       
   392 			'css'            => 'variables',
       
   393 			'__unstableType' => 'presets',
       
   394 			'isGlobalStyles' => true,
       
   395 		),
       
   396 		array(
       
   397 			'css'            => 'presets',
       
   398 			'__unstableType' => 'presets',
       
   399 			'isGlobalStyles' => true,
       
   400 		),
       
   401 	);
       
   402 	foreach ( $presets as $preset_style ) {
       
   403 		$actual_css = wp_get_global_stylesheet( array( $preset_style['css'] ) );
       
   404 		if ( '' !== $actual_css ) {
       
   405 			$preset_style['css'] = $actual_css;
       
   406 			$global_styles[]     = $preset_style;
       
   407 		}
       
   408 	}
   295 
   409 
   296 	if ( WP_Theme_JSON_Resolver::theme_has_support() ) {
   410 	if ( WP_Theme_JSON_Resolver::theme_has_support() ) {
   297 		$editor_settings['styles'][] = array(
   411 		$block_classes = array(
   298 			'css'            => $theme_json->get_stylesheet( 'block_styles' ),
   412 			'css'            => 'styles',
   299 			'__unstableType' => 'globalStyles',
   413 			'__unstableType' => 'theme',
       
   414 			'isGlobalStyles' => true,
   300 		);
   415 		);
   301 		$editor_settings['styles'][] = array(
   416 		$actual_css    = wp_get_global_stylesheet( array( $block_classes['css'] ) );
   302 			'css'                     => $theme_json->get_stylesheet( 'css_variables' ),
   417 		if ( '' !== $actual_css ) {
   303 			'__experimentalNoWrapper' => true,
   418 			$block_classes['css'] = $actual_css;
   304 			'__unstableType'          => 'globalStyles',
   419 			$global_styles[]      = $block_classes;
   305 		);
   420 		}
   306 	}
   421 	}
   307 
   422 
   308 	$editor_settings['__experimentalFeatures'] = $theme_json->get_settings();
   423 	$editor_settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() );
       
   424 
       
   425 	$editor_settings['__experimentalFeatures'] = wp_get_global_settings();
   309 	// These settings may need to be updated based on data coming from theme.json sources.
   426 	// These settings may need to be updated based on data coming from theme.json sources.
   310 	if ( isset( $editor_settings['__experimentalFeatures']['color']['palette'] ) ) {
   427 	if ( isset( $editor_settings['__experimentalFeatures']['color']['palette'] ) ) {
   311 		$colors_by_origin          = $editor_settings['__experimentalFeatures']['color']['palette'];
   428 		$colors_by_origin          = $editor_settings['__experimentalFeatures']['color']['palette'];
   312 		$editor_settings['colors'] = isset( $colors_by_origin['user'] ) ?
   429 		$editor_settings['colors'] = isset( $colors_by_origin['custom'] ) ?
   313 			$colors_by_origin['user'] : (
   430 			$colors_by_origin['custom'] : (
   314 				isset( $colors_by_origin['theme'] ) ?
   431 				isset( $colors_by_origin['theme'] ) ?
   315 					$colors_by_origin['theme'] :
   432 					$colors_by_origin['theme'] :
   316 					$colors_by_origin['core']
   433 					$colors_by_origin['default']
   317 			);
   434 			);
   318 	}
   435 	}
   319 	if ( isset( $editor_settings['__experimentalFeatures']['color']['gradients'] ) ) {
   436 	if ( isset( $editor_settings['__experimentalFeatures']['color']['gradients'] ) ) {
   320 		$gradients_by_origin          = $editor_settings['__experimentalFeatures']['color']['gradients'];
   437 		$gradients_by_origin          = $editor_settings['__experimentalFeatures']['color']['gradients'];
   321 		$editor_settings['gradients'] = isset( $gradients_by_origin['user'] ) ?
   438 		$editor_settings['gradients'] = isset( $gradients_by_origin['custom'] ) ?
   322 			$gradients_by_origin['user'] : (
   439 			$gradients_by_origin['custom'] : (
   323 				isset( $gradients_by_origin['theme'] ) ?
   440 				isset( $gradients_by_origin['theme'] ) ?
   324 					$gradients_by_origin['theme'] :
   441 					$gradients_by_origin['theme'] :
   325 					$gradients_by_origin['core']
   442 					$gradients_by_origin['default']
   326 			);
   443 			);
   327 	}
   444 	}
   328 	if ( isset( $editor_settings['__experimentalFeatures']['typography']['fontSizes'] ) ) {
   445 	if ( isset( $editor_settings['__experimentalFeatures']['typography']['fontSizes'] ) ) {
   329 		$font_sizes_by_origin         = $editor_settings['__experimentalFeatures']['typography']['fontSizes'];
   446 		$font_sizes_by_origin         = $editor_settings['__experimentalFeatures']['typography']['fontSizes'];
   330 		$editor_settings['fontSizes'] = isset( $font_sizes_by_origin['user'] ) ?
   447 		$editor_settings['fontSizes'] = isset( $font_sizes_by_origin['custom'] ) ?
   331 			$font_sizes_by_origin['user'] : (
   448 			$font_sizes_by_origin['custom'] : (
   332 				isset( $font_sizes_by_origin['theme'] ) ?
   449 				isset( $font_sizes_by_origin['theme'] ) ?
   333 					$font_sizes_by_origin['theme'] :
   450 					$font_sizes_by_origin['theme'] :
   334 					$font_sizes_by_origin['core']
   451 					$font_sizes_by_origin['default']
   335 			);
   452 			);
   336 	}
   453 	}
   337 	if ( isset( $editor_settings['__experimentalFeatures']['color']['custom'] ) ) {
   454 	if ( isset( $editor_settings['__experimentalFeatures']['color']['custom'] ) ) {
   338 		$editor_settings['disableCustomColors'] = ! $editor_settings['__experimentalFeatures']['color']['custom'];
   455 		$editor_settings['disableCustomColors'] = ! $editor_settings['__experimentalFeatures']['color']['custom'];
   339 		unset( $editor_settings['__experimentalFeatures']['color']['custom'] );
   456 		unset( $editor_settings['__experimentalFeatures']['color']['custom'] );
   344 	}
   461 	}
   345 	if ( isset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] ) ) {
   462 	if ( isset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] ) ) {
   346 		$editor_settings['disableCustomFontSizes'] = ! $editor_settings['__experimentalFeatures']['typography']['customFontSize'];
   463 		$editor_settings['disableCustomFontSizes'] = ! $editor_settings['__experimentalFeatures']['typography']['customFontSize'];
   347 		unset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] );
   464 		unset( $editor_settings['__experimentalFeatures']['typography']['customFontSize'] );
   348 	}
   465 	}
   349 	if ( isset( $editor_settings['__experimentalFeatures']['typography']['customLineHeight'] ) ) {
   466 	if ( isset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] ) ) {
   350 		$editor_settings['enableCustomLineHeight'] = $editor_settings['__experimentalFeatures']['typography']['customLineHeight'];
   467 		$editor_settings['enableCustomLineHeight'] = $editor_settings['__experimentalFeatures']['typography']['lineHeight'];
   351 		unset( $editor_settings['__experimentalFeatures']['typography']['customLineHeight'] );
   468 		unset( $editor_settings['__experimentalFeatures']['typography']['lineHeight'] );
   352 	}
   469 	}
   353 	if ( isset( $editor_settings['__experimentalFeatures']['spacing']['units'] ) ) {
   470 	if ( isset( $editor_settings['__experimentalFeatures']['spacing']['units'] ) ) {
   354 		$editor_settings['enableCustomUnits'] = $editor_settings['__experimentalFeatures']['spacing']['units'];
   471 		$editor_settings['enableCustomUnits'] = $editor_settings['__experimentalFeatures']['spacing']['units'];
   355 		unset( $editor_settings['__experimentalFeatures']['spacing']['units'] );
   472 		unset( $editor_settings['__experimentalFeatures']['spacing']['units'] );
   356 	}
   473 	}
   357 	if ( isset( $editor_settings['__experimentalFeatures']['spacing']['customPadding'] ) ) {
   474 	if ( isset( $editor_settings['__experimentalFeatures']['spacing']['padding'] ) ) {
   358 		$editor_settings['enableCustomSpacing'] = $editor_settings['__experimentalFeatures']['spacing']['customPadding'];
   475 		$editor_settings['enableCustomSpacing'] = $editor_settings['__experimentalFeatures']['spacing']['padding'];
   359 		unset( $editor_settings['__experimentalFeatures']['spacing']['customPadding'] );
   476 		unset( $editor_settings['__experimentalFeatures']['spacing']['padding'] );
   360 	}
   477 	}
       
   478 
       
   479 	$editor_settings['__unstableResolvedAssets']         = _wp_get_iframed_editor_assets();
       
   480 	$editor_settings['localAutosaveInterval']            = 15;
       
   481 	$editor_settings['__experimentalDiscussionSettings'] = array(
       
   482 		'commentOrder'         => get_option( 'comment_order' ),
       
   483 		'commentsPerPage'      => get_option( 'comments_per_page' ),
       
   484 		'defaultCommentsPage'  => get_option( 'default_comments_page' ),
       
   485 		'pageComments'         => get_option( 'page_comments' ),
       
   486 		'threadComments'       => get_option( 'thread_comments' ),
       
   487 		'threadCommentsDepth'  => get_option( 'thread_comments_depth' ),
       
   488 		'defaultCommentStatus' => get_option( 'default_comment_status' ),
       
   489 		'avatarURL'            => get_avatar_url(
       
   490 			'',
       
   491 			array(
       
   492 				'size'          => 96,
       
   493 				'force_default' => true,
       
   494 				'default'       => get_option( 'avatar_default' ),
       
   495 			)
       
   496 		),
       
   497 	);
   361 
   498 
   362 	/**
   499 	/**
   363 	 * Filters the settings to pass to the block editor for all editor type.
   500 	 * Filters the settings to pass to the block editor for all editor type.
   364 	 *
   501 	 *
   365 	 * @since 5.8.0
   502 	 * @since 5.8.0
   366 	 *
   503 	 *
   367 	 * @param array                   $editor_settings      Default editor settings.
   504 	 * @param array                   $editor_settings      Default editor settings.
   368 	 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
   505 	 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
   369 	 */
   506 	 */
   370 	$editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context );
   507 	$editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context );
       
   508 
   371 	if ( ! empty( $block_editor_context->post ) ) {
   509 	if ( ! empty( $block_editor_context->post ) ) {
   372 		$post = $block_editor_context->post;
   510 		$post = $block_editor_context->post;
   373 
   511 
   374 		/**
   512 		/**
   375 		 * Filters the settings to pass to the block editor.
   513 		 * Filters the settings to pass to the block editor.
   390  * Preloads common data used with the block editor by specifying an array of
   528  * Preloads common data used with the block editor by specifying an array of
   391  * REST API paths that will be preloaded for a given block editor context.
   529  * REST API paths that will be preloaded for a given block editor context.
   392  *
   530  *
   393  * @since 5.8.0
   531  * @since 5.8.0
   394  *
   532  *
   395  * @global WP_Post $post Global post object.
   533  * @global WP_Post    $post       Global post object.
   396  *
   534  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
   397  * @param array                   $preload_paths        List of paths to preload.
   535  * @global WP_Styles  $wp_styles  The WP_Styles object for printing styles.
       
   536  *
       
   537  * @param string[]                $preload_paths        List of paths to preload.
   398  * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
   538  * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
   399  *
       
   400  * @return void
       
   401  */
   539  */
   402 function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) {
   540 function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) {
   403 	global $post;
   541 	global $post, $wp_scripts, $wp_styles;
   404 
   542 
   405 	/**
   543 	/**
   406 	 * Filters the array of REST API paths that will be used to preloaded common data for the block editor.
   544 	 * Filters the array of REST API paths that will be used to preloaded common data for the block editor.
   407 	 *
   545 	 *
   408 	 * @since 5.8.0
   546 	 * @since 5.8.0
   409 	 *
   547 	 *
   410 	 * @param string[] $preload_paths Array of paths to preload.
   548 	 * @param string[]                $preload_paths        Array of paths to preload.
       
   549 	 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
   411 	 */
   550 	 */
   412 	$preload_paths = apply_filters( 'block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context );
   551 	$preload_paths = apply_filters( 'block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context );
       
   552 
   413 	if ( ! empty( $block_editor_context->post ) ) {
   553 	if ( ! empty( $block_editor_context->post ) ) {
   414 		$selected_post = $block_editor_context->post;
   554 		$selected_post = $block_editor_context->post;
   415 
   555 
   416 		/**
   556 		/**
   417 		 * Filters the array of paths that will be preloaded.
   557 		 * Filters the array of paths that will be preloaded.
   430 	if ( empty( $preload_paths ) ) {
   570 	if ( empty( $preload_paths ) ) {
   431 		return;
   571 		return;
   432 	}
   572 	}
   433 
   573 
   434 	/*
   574 	/*
   435 	 * Ensure the global $post remains the same after API data is preloaded.
   575 	 * Ensure the global $post, $wp_scripts, and $wp_styles remain the same after
       
   576 	 * API data is preloaded.
   436 	 * Because API preloading can call the_content and other filters, plugins
   577 	 * Because API preloading can call the_content and other filters, plugins
   437 	 * can unexpectedly modify $post.
   578 	 * can unexpectedly modify the global $post or enqueue assets which are not
       
   579 	 * intended for the block editor.
   438 	 */
   580 	 */
   439 	$backup_global_post = ! empty( $post ) ? clone $post : $post;
   581 	$backup_global_post = ! empty( $post ) ? clone $post : $post;
       
   582 	$backup_wp_scripts  = ! empty( $wp_scripts ) ? clone $wp_scripts : $wp_scripts;
       
   583 	$backup_wp_styles   = ! empty( $wp_styles ) ? clone $wp_styles : $wp_styles;
       
   584 
       
   585 	foreach ( $preload_paths as &$path ) {
       
   586 		if ( is_string( $path ) && ! str_starts_with( $path, '/' ) ) {
       
   587 			$path = '/' . $path;
       
   588 			continue;
       
   589 		}
       
   590 
       
   591 		if ( is_array( $path ) && is_string( $path[0] ) && ! str_starts_with( $path[0], '/' ) ) {
       
   592 			$path[0] = '/' . $path[0];
       
   593 		}
       
   594 	}
       
   595 
       
   596 	unset( $path );
   440 
   597 
   441 	$preload_data = array_reduce(
   598 	$preload_data = array_reduce(
   442 		$preload_paths,
   599 		$preload_paths,
   443 		'rest_preload_api_request',
   600 		'rest_preload_api_request',
   444 		array()
   601 		array()
   445 	);
   602 	);
   446 
   603 
   447 	// Restore the global $post as it was before API preloading.
   604 	// Restore the global $post, $wp_scripts, and $wp_styles as they were before API preloading.
   448 	$post = $backup_global_post;
   605 	$post       = $backup_global_post;
       
   606 	$wp_scripts = $backup_wp_scripts;
       
   607 	$wp_styles  = $backup_wp_styles;
   449 
   608 
   450 	wp_add_inline_script(
   609 	wp_add_inline_script(
   451 		'wp-api-fetch',
   610 		'wp-api-fetch',
   452 		sprintf(
   611 		sprintf(
   453 			'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
   612 			'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
   462  *
   621  *
   463  * @since 5.8.0
   622  * @since 5.8.0
   464  *
   623  *
   465  * @global array $editor_styles
   624  * @global array $editor_styles
   466  *
   625  *
   467  * @return array An array of theme styles for the block editor. Includes default font family
   626  * @return array An array of theme styles for the block editor.
   468  *               style and theme stylesheets.
       
   469  */
   627  */
   470 function get_block_editor_theme_styles() {
   628 function get_block_editor_theme_styles() {
   471 	global $editor_styles;
   629 	global $editor_styles;
   472 
   630 
   473 	if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) {
   631 	$styles = array();
   474 		$styles = array(
       
   475 			array(
       
   476 				'css'            => 'body { font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif }',
       
   477 				'__unstableType' => 'core',
       
   478 			),
       
   479 		);
       
   480 	} else {
       
   481 		$styles = array();
       
   482 	}
       
   483 
   632 
   484 	if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) {
   633 	if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) {
   485 		foreach ( $editor_styles as $style ) {
   634 		foreach ( $editor_styles as $style ) {
   486 			if ( preg_match( '~^(https?:)?//~', $style ) ) {
   635 			if ( preg_match( '~^(https?:)?//~', $style ) ) {
   487 				$response = wp_remote_get( $style );
   636 				$response = wp_remote_get( $style );
   488 				if ( ! is_wp_error( $response ) ) {
   637 				if ( ! is_wp_error( $response ) ) {
   489 					$styles[] = array(
   638 					$styles[] = array(
   490 						'css'            => wp_remote_retrieve_body( $response ),
   639 						'css'            => wp_remote_retrieve_body( $response ),
   491 						'__unstableType' => 'theme',
   640 						'__unstableType' => 'theme',
       
   641 						'isGlobalStyles' => false,
   492 					);
   642 					);
   493 				}
   643 				}
   494 			} else {
   644 			} else {
   495 				$file = get_theme_file_path( $style );
   645 				$file = get_theme_file_path( $style );
   496 				if ( is_file( $file ) ) {
   646 				if ( is_file( $file ) ) {
   497 					$styles[] = array(
   647 					$styles[] = array(
   498 						'css'            => file_get_contents( $file ),
   648 						'css'            => file_get_contents( $file ),
   499 						'baseURL'        => get_theme_file_uri( $style ),
   649 						'baseURL'        => get_theme_file_uri( $style ),
   500 						'__unstableType' => 'theme',
   650 						'__unstableType' => 'theme',
       
   651 						'isGlobalStyles' => false,
   501 					);
   652 					);
   502 				}
   653 				}
   503 			}
   654 			}
   504 		}
   655 		}
   505 	}
   656 	}