| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bootstrapping the Gutenberg Edit Site Page. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* The main entry point for the Gutenberg Edit Site Page. |
| 10 |
* |
| 11 |
* @since 7.2.0 |
| 12 |
*/ |
| 13 |
function gutenberg_edit_site_page() { |
| 14 |
?> |
| 15 |
<div |
| 16 |
id="edit-site-editor" |
| 17 |
class="edit-site" |
| 18 |
> |
| 19 |
</div> |
| 20 |
<?php |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Checks whether the provided page is one of allowed Site Editor pages. |
| 25 |
* |
| 26 |
* @param string $page Page to check. |
| 27 |
* |
| 28 |
* @return bool True for Site Editor pages, false otherwise. |
| 29 |
*/ |
| 30 |
function gutenberg_is_edit_site_page( $page ) { |
| 31 |
return 'appearance_page_gutenberg-edit-site' === $page; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Checks whether the provided page is the templates list page. |
| 36 |
* |
| 37 |
* @return bool True for Site Editor pages, false otherwise. |
| 38 |
*/ |
| 39 |
function gutenberg_is_edit_site_list_page() { |
| 40 |
return isset( $_GET['postType'] ) && ! isset( $_GET['postId'] ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Load editor styles (this is copied from edit-form-blocks.php). |
| 45 |
* Ideally the code is extracted into a reusable function. |
| 46 |
* |
| 47 |
* @return array Editor Styles Setting. |
| 48 |
*/ |
| 49 |
function gutenberg_get_editor_styles() { |
| 50 |
global $editor_styles; |
| 51 |
|
| 52 |
// Ideally the code is extracted into a reusable function. |
| 53 |
$styles = array(); |
| 54 |
|
| 55 |
if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) { |
| 56 |
foreach ( $editor_styles as $style ) { |
| 57 |
if ( preg_match( '~^(https?:)?//~', $style ) ) { |
| 58 |
$response = wp_remote_get( $style ); |
| 59 |
if ( ! is_wp_error( $response ) ) { |
| 60 |
$styles[] = array( |
| 61 |
'css' => wp_remote_retrieve_body( $response ), |
| 62 |
); |
| 63 |
} |
| 64 |
} else { |
| 65 |
$file = get_theme_file_path( $style ); |
| 66 |
if ( is_file( $file ) ) { |
| 67 |
$styles[] = array( |
| 68 |
'css' => file_get_contents( $file ), |
| 69 |
'baseURL' => get_theme_file_uri( $style ), |
| 70 |
); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return $styles; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Initialize the Gutenberg Site Editor. |
| 81 |
* |
| 82 |
* @since 7.2.0 |
| 83 |
* |
| 84 |
* @param string $hook Page. |
| 85 |
*/ |
| 86 |
function gutenberg_edit_site_init( $hook ) { |
| 87 |
global $current_screen, $post, $editor_styles; |
| 88 |
|
| 89 |
if ( ! gutenberg_is_edit_site_page( $hook ) ) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
if ( gutenberg_is_edit_site_list_page() ) { |
| 94 |
$post_type = get_post_type_object( $_GET['postType'] ); |
| 95 |
|
| 96 |
if ( ! $post_type ) { |
| 97 |
wp_die( __( 'Invalid post type.', 'gutenberg' ) ); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// Default to is-fullscreen-mode to avoid rendering wp-admin navigation menu while loading and |
| 102 |
// having jumps in the UI. |
| 103 |
add_filter( |
| 104 |
'admin_body_class', |
| 105 |
static function( $classes ) { |
| 106 |
return "$classes is-fullscreen-mode"; |
| 107 |
} |
| 108 |
); |
| 109 |
|
| 110 |
$indexed_template_types = array(); |
| 111 |
foreach ( get_default_block_template_types() as $slug => $template_type ) { |
| 112 |
$template_type['slug'] = (string) $slug; |
| 113 |
$indexed_template_types[] = $template_type; |
| 114 |
} |
| 115 |
|
| 116 |
$custom_settings = array( |
| 117 |
'siteUrl' => site_url(), |
| 118 |
'postsPerPage' => get_option( 'posts_per_page' ), |
| 119 |
'styles' => gutenberg_get_editor_styles(), |
| 120 |
'defaultTemplateTypes' => $indexed_template_types, |
| 121 |
'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(), |
| 122 |
'__experimentalBlockPatterns' => WP_Block_Patterns_Registry::get_instance()->get_all_registered(), |
| 123 |
'__experimentalBlockPatternCategories' => WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(), |
| 124 |
); |
| 125 |
|
| 126 |
/** |
| 127 |
* Make the WP Screen object aware that this is a block editor page. |
| 128 |
* Since custom blocks check whether the screen is_block_editor, |
| 129 |
* this is required for custom blocks to be loaded. |
| 130 |
* See wp_enqueue_registered_block_scripts_and_styles in wp-includes/script-loader.php |
| 131 |
*/ |
| 132 |
$current_screen->is_block_editor( true ); |
| 133 |
|
| 134 |
$site_editor_context = new WP_Block_Editor_Context(); |
| 135 |
$settings = get_block_editor_settings( $custom_settings, $site_editor_context ); |
| 136 |
$active_global_styles_id = WP_Theme_JSON_Resolver_Gutenberg::get_user_global_styles_post_id(); |
| 137 |
$active_theme = wp_get_theme()->get_stylesheet(); |
| 138 |
gutenberg_initialize_editor( |
| 139 |
'edit_site_editor', |
| 140 |
'edit-site', |
| 141 |
array( |
| 142 |
'preload_paths' => array_merge( |
| 143 |
array( |
| 144 |
array( '/wp/v2/media', 'OPTIONS' ), |
| 145 |
'/', |
| 146 |
'/wp/v2/types?context=edit', |
| 147 |
'/wp/v2/types/wp_template?context=edit', |
| 148 |
'/wp/v2/types/wp_template-part?context=edit', |
| 149 |
'/wp/v2/taxonomies?context=edit', |
| 150 |
'/wp/v2/pages?context=edit', |
| 151 |
'/wp/v2/categories?context=edit', |
| 152 |
'/wp/v2/posts?context=edit', |
| 153 |
'/wp/v2/tags?context=edit', |
| 154 |
'/wp/v2/templates?context=edit&per_page=-1', |
| 155 |
'/wp/v2/template-parts?context=edit&per_page=-1', |
| 156 |
'/wp/v2/settings', |
| 157 |
'/wp/v2/themes?context=edit&status=active', |
| 158 |
'/wp/v2/global-styles/' . $active_global_styles_id . '?context=edit', |
| 159 |
'/wp/v2/global-styles/' . $active_global_styles_id, |
| 160 |
'/wp/v2/global-styles/themes/' . $active_theme, |
| 161 |
) |
| 162 |
), |
| 163 |
'initializer_name' => 'initializeEditor', |
| 164 |
'editor_settings' => $settings, |
| 165 |
) |
| 166 |
); |
| 167 |
|
| 168 |
wp_add_inline_script( |
| 169 |
'wp-blocks', |
| 170 |
sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ), |
| 171 |
'after' |
| 172 |
); |
| 173 |
|
| 174 |
wp_enqueue_script( 'wp-edit-site' ); |
| 175 |
wp_enqueue_script( 'wp-format-library' ); |
| 176 |
wp_enqueue_style( 'wp-edit-site' ); |
| 177 |
wp_enqueue_style( 'wp-format-library' ); |
| 178 |
wp_enqueue_media(); |
| 179 |
|
| 180 |
if ( |
| 181 |
current_theme_supports( 'wp-block-styles' ) || |
| 182 |
( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 ) |
| 183 |
) { |
| 184 |
wp_enqueue_style( 'wp-block-library-theme' ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Fires after block assets have been enqueued for the editing interface. |
| 189 |
* |
| 190 |
* Call `add_action` on any hook before 'admin_enqueue_scripts'. |
| 191 |
* |
| 192 |
* In the function call you supply, simply use `wp_enqueue_script` and |
| 193 |
* `wp_enqueue_style` to add your functionality to the block editor. |
| 194 |
* |
| 195 |
* @since 5.0.0 |
| 196 |
*/ |
| 197 |
|
| 198 |
do_action( 'enqueue_block_editor_assets' ); |
| 199 |
|
| 200 |
} |
| 201 |
add_action( 'admin_enqueue_scripts', 'gutenberg_edit_site_init' ); |
| 202 |
|
| 203 |
/** |
| 204 |
* Register a core site setting for front page information. |
| 205 |
*/ |
| 206 |
function register_site_editor_homepage_settings() { |
| 207 |
register_setting( |
| 208 |
'reading', |
| 209 |
'show_on_front', |
| 210 |
array( |
| 211 |
'show_in_rest' => true, |
| 212 |
'type' => 'string', |
| 213 |
'description' => __( 'What to show on the front page', 'gutenberg' ), |
| 214 |
) |
| 215 |
); |
| 216 |
|
| 217 |
register_setting( |
| 218 |
'reading', |
| 219 |
'page_on_front', |
| 220 |
array( |
| 221 |
'show_in_rest' => true, |
| 222 |
'type' => 'number', |
| 223 |
'description' => __( 'The ID of the page that should be displayed on the front page', 'gutenberg' ), |
| 224 |
) |
| 225 |
); |
| 226 |
|
| 227 |
register_setting( |
| 228 |
'reading', |
| 229 |
'page_for_posts', |
| 230 |
array( |
| 231 |
'show_in_rest' => true, |
| 232 |
'type' => 'number', |
| 233 |
'description' => __( 'The ID of the page that should display the latest posts', 'gutenberg' ), |
| 234 |
) |
| 235 |
); |
| 236 |
} |
| 237 |
add_action( 'init', 'register_site_editor_homepage_settings', 10 ); |
| 238 |
|
| 239 |
/** |
| 240 |
* Tells the script loader to load the scripts and styles of custom block on site editor screen. |
| 241 |
* |
| 242 |
* @param bool $is_block_editor_screen Current decision about loading block assets. |
| 243 |
* @return bool Filtered decision about loading block assets. |
| 244 |
*/ |
| 245 |
function gutenberg_site_editor_load_block_editor_scripts_and_styles( $is_block_editor_screen ) { |
| 246 |
return ( is_callable( 'get_current_screen' ) && get_current_screen() && 'appearance_page_gutenberg-edit-site' === get_current_screen()->base ) |
| 247 |
? true |
| 248 |
: $is_block_editor_screen; |
| 249 |
} |
| 250 |
add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_site_editor_load_block_editor_scripts_and_styles' ); |
| 251 |
|