wp/wp-admin/site-editor.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     4  *
     4  *
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Administration
     6  * @subpackage Administration
     7  */
     7  */
     8 
     8 
     9 global $post, $editor_styles;
     9 global $editor_styles;
    10 
    10 
    11 /** WordPress Administration Bootstrap */
    11 /** WordPress Administration Bootstrap */
    12 require_once __DIR__ . '/admin.php';
    12 require_once __DIR__ . '/admin.php';
    13 
    13 
    14 if ( ! current_user_can( 'edit_theme_options' ) ) {
    14 if ( ! current_user_can( 'edit_theme_options' ) ) {
    17 		'<p>' . __( 'Sorry, you are not allowed to edit theme options on this site.' ) . '</p>',
    17 		'<p>' . __( 'Sorry, you are not allowed to edit theme options on this site.' ) . '</p>',
    18 		403
    18 		403
    19 	);
    19 	);
    20 }
    20 }
    21 
    21 
       
    22 $is_template_part        = isset( $_GET['postType'] ) && 'wp_template_part' === sanitize_key( $_GET['postType'] );
       
    23 $is_template_part_path   = isset( $_GET['path'] ) && 'wp_template_partall' === sanitize_key( $_GET['path'] );
       
    24 $is_template_part_editor = $is_template_part || $is_template_part_path;
       
    25 $is_patterns             = isset( $_GET['postType'] ) && 'wp_block' === sanitize_key( $_GET['postType'] );
       
    26 $is_patterns_path        = isset( $_GET['path'] ) && 'patterns' === sanitize_key( $_GET['path'] );
       
    27 $is_patterns_editor      = $is_patterns || $is_patterns_path;
       
    28 
    22 if ( ! wp_is_block_theme() ) {
    29 if ( ! wp_is_block_theme() ) {
    23 	wp_die( __( 'The theme you are currently using is not compatible with Full Site Editing.' ) );
    30 	if ( ! current_theme_supports( 'block-template-parts' ) && $is_template_part_editor ) {
    24 }
    31 		wp_die( __( 'The theme you are currently using is not compatible with the Site Editor.' ) );
    25 
    32 	} elseif ( ! $is_patterns_editor && ! $is_template_part_editor ) {
    26 /**
    33 		wp_die( __( 'The theme you are currently using is not compatible with the Site Editor.' ) );
    27  * Do a server-side redirection if missing `postType` and `postId`
       
    28  * query args when visiting Site Editor.
       
    29  */
       
    30 $home_template = _resolve_home_block_template();
       
    31 if ( $home_template && empty( $_GET['postType'] ) && empty( $_GET['postId'] ) ) {
       
    32 	if ( ! empty( $_GET['styles'] ) ) {
       
    33 		$home_template['styles'] = sanitize_key( $_GET['styles'] );
       
    34 	}
    34 	}
    35 	$redirect_url = add_query_arg(
       
    36 		$home_template,
       
    37 		admin_url( 'site-editor.php' )
       
    38 	);
       
    39 	wp_safe_redirect( $redirect_url );
       
    40 	exit;
       
    41 }
    35 }
    42 
    36 
    43 // Used in the HTML title tag.
    37 // Used in the HTML title tag.
    44 $title       = __( 'Editor (beta)' );
    38 $title       = _x( 'Editor', 'site editor title tag' );
    45 $parent_file = 'themes.php';
    39 $parent_file = 'themes.php';
    46 
    40 
    47 // Flag that we're loading the block editor.
    41 // Flag that we're loading the block editor.
    48 $current_screen = get_current_screen();
    42 $current_screen = get_current_screen();
    49 $current_screen->is_block_editor( true );
    43 $current_screen->is_block_editor( true );
    50 
    44 
    51 // Default to is-fullscreen-mode to avoid jumps in the UI.
    45 // Default to is-fullscreen-mode to avoid jumps in the UI.
    52 add_filter(
    46 add_filter(
    53 	'admin_body_class',
    47 	'admin_body_class',
    54 	static function( $classes ) {
    48 	static function ( $classes ) {
    55 		return "$classes is-fullscreen-mode";
    49 		return "$classes is-fullscreen-mode";
    56 	}
    50 	}
    57 );
    51 );
    58 
    52 
    59 $indexed_template_types = array();
    53 $indexed_template_types = array();
    62 	$indexed_template_types[] = $template_type;
    56 	$indexed_template_types[] = $template_type;
    63 }
    57 }
    64 
    58 
    65 $block_editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );
    59 $block_editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );
    66 $custom_settings      = array(
    60 $custom_settings      = array(
    67 	'siteUrl'                  => site_url(),
    61 	'siteUrl'                   => site_url(),
    68 	'postsPerPage'             => get_option( 'posts_per_page' ),
    62 	'postsPerPage'              => get_option( 'posts_per_page' ),
    69 	'styles'                   => get_block_editor_theme_styles(),
    63 	'styles'                    => get_block_editor_theme_styles(),
    70 	'defaultTemplateTypes'     => $indexed_template_types,
    64 	'defaultTemplateTypes'      => $indexed_template_types,
    71 	'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(),
    65 	'defaultTemplatePartAreas'  => get_allowed_block_template_part_areas(),
    72 	'__unstableHomeTemplate'   => $home_template,
    66 	'supportsLayout'            => wp_theme_has_theme_json(),
       
    67 	'supportsTemplatePartsMode' => ! wp_is_block_theme() && current_theme_supports( 'block-template-parts' ),
    73 );
    68 );
    74 
    69 
    75 // Add additional back-compat patterns registered by `current_screen` et al.
    70 // Add additional back-compat patterns registered by `current_screen` et al.
    76 $custom_settings['__experimentalAdditionalBlockPatterns']          = WP_Block_Patterns_Registry::get_instance()->get_all_registered( true );
    71 $custom_settings['__experimentalAdditionalBlockPatterns']          = WP_Block_Patterns_Registry::get_instance()->get_all_registered( true );
    77 $custom_settings['__experimentalAdditionalBlockPatternCategories'] = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered( true );
    72 $custom_settings['__experimentalAdditionalBlockPatternCategories'] = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered( true );
    78 
    73 
    79 $editor_settings      = get_block_editor_settings( $custom_settings, $block_editor_context );
    74 $editor_settings = get_block_editor_settings( $custom_settings, $block_editor_context );
    80 
    75 
    81 if ( isset( $_GET['postType'] ) && ! isset( $_GET['postId'] ) ) {
    76 if ( isset( $_GET['postType'] ) && ! isset( $_GET['postId'] ) ) {
    82 	$post_type = get_post_type_object( $_GET['postType'] );
    77 	$post_type = get_post_type_object( $_GET['postType'] );
    83 	if ( ! $post_type ) {
    78 	if ( ! $post_type ) {
    84 		wp_die( __( 'Invalid post type.' ) );
    79 		wp_die( __( 'Invalid post type.' ) );
    85 	}
    80 	}
    86 }
    81 }
    87 
    82 
    88 $active_global_styles_id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
    83 $active_global_styles_id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
    89 $active_theme            = wp_get_theme()->get_stylesheet();
    84 $active_theme            = get_stylesheet();
    90 $preload_paths           = array(
    85 
       
    86 $navigation_rest_route = rest_get_route_for_post_type_items(
       
    87 	'wp_navigation'
       
    88 );
       
    89 
       
    90 $preload_paths = array(
    91 	array( '/wp/v2/media', 'OPTIONS' ),
    91 	array( '/wp/v2/media', 'OPTIONS' ),
    92 	'/wp/v2/types?context=view',
    92 	'/wp/v2/types?context=view',
    93 	'/wp/v2/types/wp_template?context=edit',
    93 	'/wp/v2/types/wp_template?context=edit',
    94 	'/wp/v2/types/wp_template-part?context=edit',
    94 	'/wp/v2/types/wp_template-part?context=edit',
    95 	'/wp/v2/templates?context=edit&per_page=-1',
    95 	'/wp/v2/templates?context=edit&per_page=-1',
    96 	'/wp/v2/template-parts?context=edit&per_page=-1',
    96 	'/wp/v2/template-parts?context=edit&per_page=-1',
    97 	'/wp/v2/themes?context=edit&status=active',
    97 	'/wp/v2/themes?context=edit&status=active',
    98 	'/wp/v2/global-styles/' . $active_global_styles_id . '?context=edit',
    98 	'/wp/v2/global-styles/' . $active_global_styles_id . '?context=edit',
    99 	'/wp/v2/global-styles/' . $active_global_styles_id,
    99 	'/wp/v2/global-styles/' . $active_global_styles_id,
   100 	'/wp/v2/global-styles/themes/' . $active_theme,
   100 	'/wp/v2/global-styles/themes/' . $active_theme,
       
   101 	array( $navigation_rest_route, 'OPTIONS' ),
       
   102 	array(
       
   103 		add_query_arg(
       
   104 			array(
       
   105 				'context'   => 'edit',
       
   106 				'per_page'  => 100,
       
   107 				'order'     => 'desc',
       
   108 				'orderby'   => 'date',
       
   109 				// array indices are required to avoid query being encoded and not matching in cache.
       
   110 				'status[0]' => 'publish',
       
   111 				'status[1]' => 'draft',
       
   112 			),
       
   113 			$navigation_rest_route
       
   114 		),
       
   115 		'GET',
       
   116 	),
   101 );
   117 );
   102 
   118 
   103 block_editor_rest_api_preload( $preload_paths, $block_editor_context );
   119 block_editor_rest_api_preload( $preload_paths, $block_editor_context );
   104 
   120 
   105 wp_add_inline_script(
   121 wp_add_inline_script(
   118 	'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
   134 	'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
   119 );
   135 );
   120 
   136 
   121 wp_add_inline_script(
   137 wp_add_inline_script(
   122 	'wp-blocks',
   138 	'wp-blocks',
   123 	sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ),
   139 	sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( isset( $editor_settings['blockCategories'] ) ? $editor_settings['blockCategories'] : array() ) ),
   124 	'after'
   140 	'after'
   125 );
   141 );
   126 
   142 
   127 wp_enqueue_script( 'wp-edit-site' );
   143 wp_enqueue_script( 'wp-edit-site' );
   128 wp_enqueue_script( 'wp-format-library' );
   144 wp_enqueue_script( 'wp-format-library' );
   129 wp_enqueue_style( 'wp-edit-site' );
   145 wp_enqueue_style( 'wp-edit-site' );
   130 wp_enqueue_style( 'wp-format-library' );
   146 wp_enqueue_style( 'wp-format-library' );
   131 wp_enqueue_media();
   147 wp_enqueue_media();
   132 
   148 
   133 if (
   149 if (
   134 	current_theme_supports( 'wp-block-styles' ) ||
   150 	current_theme_supports( 'wp-block-styles' ) &&
   135 	( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 )
   151 	( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 )
   136 ) {
   152 ) {
   137 	wp_enqueue_style( 'wp-block-library-theme' );
   153 	wp_enqueue_style( 'wp-block-library-theme' );
   138 }
   154 }
   139 
   155 
   141 do_action( 'enqueue_block_editor_assets' );
   157 do_action( 'enqueue_block_editor_assets' );
   142 
   158 
   143 require_once ABSPATH . 'wp-admin/admin-header.php';
   159 require_once ABSPATH . 'wp-admin/admin-header.php';
   144 ?>
   160 ?>
   145 
   161 
   146 <div id="site-editor" class="edit-site"></div>
   162 <div class="edit-site" id="site-editor">
       
   163 	<?php // JavaScript is disabled. ?>
       
   164 	<div class="wrap hide-if-js site-editor-no-js">
       
   165 		<h1 class="wp-heading-inline"><?php _e( 'Edit site' ); ?></h1>
       
   166 		<?php
       
   167 		/**
       
   168 		 * Filters the message displayed in the site editor interface when JavaScript is
       
   169 		 * not enabled in the browser.
       
   170 		 *
       
   171 		 * @since 6.3.0
       
   172 		 *
       
   173 		 * @param string  $message The message being displayed.
       
   174 		 * @param WP_Post $post    The post being edited.
       
   175 		 */
       
   176 		$message = apply_filters( 'site_editor_no_javascript_message', __( 'The site editor requires JavaScript. Please enable JavaScript in your browser settings.' ), $post );
       
   177 		wp_admin_notice(
       
   178 			$message,
       
   179 			array(
       
   180 				'type'               => 'error',
       
   181 				'additional_classes' => array( 'hide-if-js' ),
       
   182 			)
       
   183 		);
       
   184 		?>
       
   185 	</div>
       
   186 </div>
   147 
   187 
   148 <?php
   188 <?php
   149 
   189 
   150 require_once ABSPATH . 'wp-admin/admin-footer.php';
   190 require_once ABSPATH . 'wp-admin/admin-footer.php';