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