| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bootstraping 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 'toplevel_page_gutenberg-edit-site' === $page; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Load editor styles (this is copied from edit-form-blocks.php). |
| 36 |
* Ideally the code is extracted into a reusable function. |
| 37 |
* |
| 38 |
* @return array Editor Styles Setting. |
| 39 |
*/ |
| 40 |
function gutenberg_get_editor_styles() { |
| 41 |
global $editor_styles; |
| 42 |
|
| 43 |
// |
| 44 |
// Ideally the code is extracted into a reusable function. |
| 45 |
$styles = array( |
| 46 |
array( |
| 47 |
'css' => file_get_contents( |
| 48 |
ABSPATH . WPINC . '/css/dist/editor/editor-styles.css' |
| 49 |
), |
| 50 |
), |
| 51 |
); |
| 52 |
|
| 53 |
/* translators: Use this to specify the CSS font family for the default font. */ |
| 54 |
$locale_font_family = esc_html_x( 'Noto Serif', 'CSS Font Family for Editor Font', 'gutenberg' ); |
| 55 |
$styles[] = array( |
| 56 |
'css' => "body { font-family: '$locale_font_family' }", |
| 57 |
); |
| 58 |
|
| 59 |
if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) { |
| 60 |
foreach ( $editor_styles as $style ) { |
| 61 |
if ( preg_match( '~^(https?:)?//~', $style ) ) { |
| 62 |
$response = wp_remote_get( $style ); |
| 63 |
if ( ! is_wp_error( $response ) ) { |
| 64 |
$styles[] = array( |
| 65 |
'css' => wp_remote_retrieve_body( $response ), |
| 66 |
); |
| 67 |
} |
| 68 |
} else { |
| 69 |
$file = get_theme_file_path( $style ); |
| 70 |
if ( is_file( $file ) ) { |
| 71 |
$styles[] = array( |
| 72 |
'css' => file_get_contents( $file ), |
| 73 |
'baseURL' => get_theme_file_uri( $style ), |
| 74 |
); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return $styles; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Initialize the Gutenberg Edit Site Page. |
| 85 |
* |
| 86 |
* @since 7.2.0 |
| 87 |
* |
| 88 |
* @param string $hook Page. |
| 89 |
*/ |
| 90 |
function gutenberg_edit_site_init( $hook ) { |
| 91 |
global $current_screen; |
| 92 |
|
| 93 |
if ( ! gutenberg_is_edit_site_page( $hook ) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Make the WP Screen object aware that this is a block editor page. |
| 99 |
* Since custom blocks check whether the screen is_block_editor, |
| 100 |
* this is required for custom blocks to be loaded. |
| 101 |
* See wp_enqueue_registered_block_scripts_and_styles in wp-includes/script-loader.php |
| 102 |
*/ |
| 103 |
$current_screen->is_block_editor( true ); |
| 104 |
|
| 105 |
// Get editor settings. |
| 106 |
$max_upload_size = wp_max_upload_size(); |
| 107 |
if ( ! $max_upload_size ) { |
| 108 |
$max_upload_size = 0; |
| 109 |
} |
| 110 |
|
| 111 |
// This filter is documented in wp-admin/includes/media.php. |
| 112 |
$image_size_names = apply_filters( |
| 113 |
'image_size_names_choose', |
| 114 |
array( |
| 115 |
'thumbnail' => __( 'Thumbnail', 'gutenberg' ), |
| 116 |
'medium' => __( 'Medium', 'gutenberg' ), |
| 117 |
'large' => __( 'Large', 'gutenberg' ), |
| 118 |
'full' => __( 'Full Size', 'gutenberg' ), |
| 119 |
) |
| 120 |
); |
| 121 |
$available_image_sizes = array(); |
| 122 |
foreach ( $image_size_names as $image_size_slug => $image_size_name ) { |
| 123 |
$available_image_sizes[] = array( |
| 124 |
'slug' => $image_size_slug, |
| 125 |
'name' => $image_size_name, |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
$settings = array( |
| 130 |
'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), |
| 131 |
'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), |
| 132 |
'imageSizes' => $available_image_sizes, |
| 133 |
'isRTL' => is_rtl(), |
| 134 |
'maxUploadFileSize' => $max_upload_size, |
| 135 |
); |
| 136 |
|
| 137 |
list( $color_palette, ) = (array) get_theme_support( 'editor-color-palette' ); |
| 138 |
list( $font_sizes, ) = (array) get_theme_support( 'editor-font-sizes' ); |
| 139 |
if ( false !== $color_palette ) { |
| 140 |
$settings['colors'] = $color_palette; |
| 141 |
} |
| 142 |
if ( false !== $font_sizes ) { |
| 143 |
$settings['fontSizes'] = $font_sizes; |
| 144 |
} |
| 145 |
$settings['styles'] = gutenberg_get_editor_styles(); |
| 146 |
|
| 147 |
$template_ids = array(); |
| 148 |
$template_part_ids = array(); |
| 149 |
foreach ( get_template_types() as $template_type ) { |
| 150 |
// Skip 'embed' for now because it is not a regular template type. |
| 151 |
// Skip 'index' because it's a fallback that we handle differently. |
| 152 |
if ( in_array( $template_type, array( 'embed', 'index' ), true ) ) { |
| 153 |
continue; |
| 154 |
} |
| 155 |
|
| 156 |
$current_template = gutenberg_find_template_post_and_parts( $template_type ); |
| 157 |
if ( isset( $current_template ) ) { |
| 158 |
$template_ids[ $template_type ] = $current_template['template_post']->ID; |
| 159 |
$template_part_ids = $template_part_ids + $current_template['template_part_ids']; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
$current_template_id = $template_ids['front-page']; |
| 164 |
|
| 165 |
$settings['editSiteInitialState'] = array(); |
| 166 |
|
| 167 |
$settings['editSiteInitialState']['homeTemplateId'] = $current_template_id; |
| 168 |
$settings['editSiteInitialState']['templateId'] = $current_template_id; |
| 169 |
$settings['editSiteInitialState']['templateType'] = 'wp_template'; |
| 170 |
$settings['editSiteInitialState']['templateIds'] = array_values( $template_ids ); |
| 171 |
$settings['editSiteInitialState']['templatePartIds'] = array_values( $template_part_ids ); |
| 172 |
|
| 173 |
$settings['editSiteInitialState']['showOnFront'] = get_option( 'show_on_front' ); |
| 174 |
$settings['editSiteInitialState']['page'] = array( |
| 175 |
'path' => '/', |
| 176 |
'context' => 'page' === $settings['editSiteInitialState']['showOnFront'] ? array( |
| 177 |
'postType' => 'page', |
| 178 |
'postId' => get_option( 'page_on_front' ), |
| 179 |
) : array(), |
| 180 |
); |
| 181 |
|
| 182 |
// This is so other parts of the code can hook their own settings. |
| 183 |
// Example: Global Styles. |
| 184 |
global $post; |
| 185 |
$settings = apply_filters( 'block_editor_settings', $settings, $post ); |
| 186 |
|
| 187 |
// Preload block editor paths. |
| 188 |
// most of these are copied from edit-forms-blocks.php. |
| 189 |
$preload_paths = array( |
| 190 |
'/', |
| 191 |
'/wp/v2/types?context=edit', |
| 192 |
'/wp/v2/taxonomies?per_page=100&context=edit', |
| 193 |
'/wp/v2/pages?per_page=100&context=edit', |
| 194 |
'/wp/v2/themes?status=active', |
| 195 |
sprintf( '/wp/v2/templates/%s?context=edit', $current_template_id ), |
| 196 |
array( '/wp/v2/media', 'OPTIONS' ), |
| 197 |
); |
| 198 |
$preload_data = array_reduce( |
| 199 |
$preload_paths, |
| 200 |
'rest_preload_api_request', |
| 201 |
array() |
| 202 |
); |
| 203 |
wp_add_inline_script( |
| 204 |
'wp-api-fetch', |
| 205 |
sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode( $preload_data ) ), |
| 206 |
'after' |
| 207 |
); |
| 208 |
|
| 209 |
// Initialize editor. |
| 210 |
wp_add_inline_script( |
| 211 |
'wp-edit-site', |
| 212 |
sprintf( |
| 213 |
'wp.domReady( function() { |
| 214 |
wp.editSite.initialize( "edit-site-editor", %s ); |
| 215 |
} );', |
| 216 |
wp_json_encode( gutenberg_experiments_editor_settings( $settings ) ) |
| 217 |
) |
| 218 |
); |
| 219 |
|
| 220 |
wp_add_inline_script( |
| 221 |
'wp-blocks', |
| 222 |
sprintf( 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions( %s );', wp_json_encode( get_block_editor_server_block_settings() ) ), |
| 223 |
'after' |
| 224 |
); |
| 225 |
|
| 226 |
wp_add_inline_script( |
| 227 |
'wp-blocks', |
| 228 |
sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ), |
| 229 |
'after' |
| 230 |
); |
| 231 |
|
| 232 |
/** |
| 233 |
* Fires after block assets have been enqueued for the editing interface. |
| 234 |
* |
| 235 |
* Call `add_action` on any hook before 'admin_enqueue_scripts'. |
| 236 |
* |
| 237 |
* In the function call you supply, simply use `wp_enqueue_script` and |
| 238 |
* `wp_enqueue_style` to add your functionality to the block editor. |
| 239 |
* |
| 240 |
* @since 5.0.0 |
| 241 |
*/ |
| 242 |
|
| 243 |
do_action( 'enqueue_block_editor_assets' ); |
| 244 |
|
| 245 |
wp_enqueue_script( 'wp-edit-site' ); |
| 246 |
wp_enqueue_script( 'wp-format-library' ); |
| 247 |
wp_enqueue_style( 'wp-edit-site' ); |
| 248 |
wp_enqueue_style( 'wp-format-library' ); |
| 249 |
} |
| 250 |
add_action( 'admin_enqueue_scripts', 'gutenberg_edit_site_init' ); |
| 251 |
|