| @@ -20,8 +20,67 @@ | ||
| 20 | 20 | <?php |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 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 | +/** | |
| 24 | 83 | * Initialize the Gutenberg Edit Site Page. |
| 25 | 84 | * |
| 26 | 85 | * @since 7.2.0 |
| 27 | 86 | * |
| @@ -27,60 +86,57 @@ | ||
| 27 | 86 | * |
| 28 | 87 | * @param string $hook Page. |
| 29 | 88 | */ |
| 30 | 89 | function gutenberg_edit_site_init( $hook ) { |
| 31 | - global $_wp_current_template_id; | |
| 32 | - if ( 'gutenberg_page_gutenberg-edit-site' !== $hook ) { | |
| 90 | + global $current_screen, $post; | |
| 91 | + | |
| 92 | + if ( ! gutenberg_is_edit_site_page( $hook ) ) { | |
| 33 | 93 | return; |
| 34 | 94 | } |
| 35 | 95 | |
| 36 | - // Get editor settings. | |
| 37 | - $max_upload_size = wp_max_upload_size(); | |
| 38 | - if ( ! $max_upload_size ) { | |
| 39 | - $max_upload_size = 0; | |
| 40 | - } | |
| 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 ); | |
| 41 | 103 | |
| 42 | - // This filter is documented in wp-admin/includes/media.php. | |
| 43 | - $image_size_names = apply_filters( | |
| 44 | - 'image_size_names_choose', | |
| 104 | + $settings = array_merge( | |
| 105 | + gutenberg_get_common_block_editor_settings(), | |
| 45 | 106 | array( |
| 46 | - 'thumbnail' => __( 'Thumbnail', 'gutenberg' ), | |
| 47 | - 'medium' => __( 'Medium', 'gutenberg' ), | |
| 48 | - 'large' => __( 'Large', 'gutenberg' ), | |
| 49 | - 'full' => __( 'Full Size', 'gutenberg' ), | |
| 107 | + 'alignWide' => get_theme_support( 'align-wide' ), | |
| 108 | + 'siteUrl' => site_url(), | |
| 109 | + 'postsPerPage' => get_option( 'posts_per_page' ), | |
| 110 | + 'styles' => gutenberg_get_editor_styles(), | |
| 111 | + 'defaultTemplateTypes' => gutenberg_get_indexed_default_template_types(), | |
| 112 | + '__experimentalBlockPatterns' => WP_Block_Patterns_Registry::get_instance()->get_all_registered(), | |
| 113 | + '__experimentalBlockPatternCategories' => WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(), | |
| 50 | 114 | ) |
| 51 | 115 | ); |
| 52 | - $available_image_sizes = array(); | |
| 53 | - foreach ( $image_size_names as $image_size_slug => $image_size_name ) { | |
| 54 | - $available_image_sizes[] = array( | |
| 55 | - 'slug' => $image_size_slug, | |
| 56 | - 'name' => $image_size_name, | |
| 57 | - ); | |
| 58 | - } | |
| 116 | + $settings = gutenberg_experimental_global_styles_settings( $settings ); | |
| 59 | 117 | |
| 60 | - $settings = array( | |
| 61 | - 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), | |
| 62 | - 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), | |
| 63 | - 'imageSizes' => $available_image_sizes, | |
| 64 | - 'isRTL' => is_rtl(), | |
| 65 | - 'maxUploadFileSize' => $max_upload_size, | |
| 118 | + // Preload block editor paths. | |
| 119 | + // most of these are copied from edit-forms-blocks.php. | |
| 120 | + $preload_paths = array( | |
| 121 | + '/?context=edit', | |
| 122 | + '/wp/v2/types?context=edit', | |
| 123 | + '/wp/v2/taxonomies?context=edit', | |
| 124 | + '/wp/v2/pages?context=edit', | |
| 125 | + '/wp/v2/themes?status=active', | |
| 126 | + array( '/wp/v2/media', 'OPTIONS' ), | |
| 66 | 127 | ); |
| 128 | + $preload_data = array_reduce( | |
| 129 | + $preload_paths, | |
| 130 | + 'rest_preload_api_request', | |
| 131 | + array() | |
| 132 | + ); | |
| 133 | + wp_add_inline_script( | |
| 134 | + 'wp-api-fetch', | |
| 135 | + sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode( $preload_data ) ), | |
| 136 | + 'after' | |
| 137 | + ); | |
| 67 | 138 | |
| 68 | - list( $color_palette, ) = (array) get_theme_support( 'editor-color-palette' ); | |
| 69 | - list( $font_sizes, ) = (array) get_theme_support( 'editor-font-sizes' ); | |
| 70 | - if ( false !== $color_palette ) { | |
| 71 | - $settings['colors'] = $color_palette; | |
| 72 | - } | |
| 73 | - if ( false !== $font_sizes ) { | |
| 74 | - $settings['fontSizes'] = $font_sizes; | |
| 75 | - } | |
| 76 | - | |
| 77 | - // Get root template by trigerring `./template-loader.php`'s logic. | |
| 78 | - get_front_page_template(); | |
| 79 | - get_index_template(); | |
| 80 | - apply_filters( 'template_include', null ); | |
| 81 | - $settings['templateId'] = $_wp_current_template_id; | |
| 82 | - | |
| 83 | 139 | // Initialize editor. |
| 84 | 140 | wp_add_inline_script( |
| 85 | 141 | 'wp-edit-site', |
| 86 | 142 | sprintf( |
| @@ -86,18 +142,37 @@ | ||
| 86 | 142 | sprintf( |
| 87 | 143 | 'wp.domReady( function() { |
| 88 | 144 | wp.editSite.initialize( "edit-site-editor", %s ); |
| 89 | 145 | } );', |
| 90 | - wp_json_encode( gutenberg_experiments_editor_settings( $settings ) ) | |
| 146 | + wp_json_encode( $settings ) | |
| 91 | 147 | ) |
| 92 | 148 | ); |
| 93 | 149 | |
| 94 | - // Preload server-registered block schemas. | |
| 95 | 150 | wp_add_inline_script( |
| 96 | 151 | 'wp-blocks', |
| 97 | - 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' | |
| 152 | + sprintf( 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions( %s );', wp_json_encode( get_block_editor_server_block_settings() ) ), | |
| 153 | + 'after' | |
| 98 | 154 | ); |
| 99 | 155 | |
| 156 | + wp_add_inline_script( | |
| 157 | + 'wp-blocks', | |
| 158 | + sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ), | |
| 159 | + 'after' | |
| 160 | + ); | |
| 161 | + | |
| 162 | + /** | |
| 163 | + * Fires after block assets have been enqueued for the editing interface. | |
| 164 | + * | |
| 165 | + * Call `add_action` on any hook before 'admin_enqueue_scripts'. | |
| 166 | + * | |
| 167 | + * In the function call you supply, simply use `wp_enqueue_script` and | |
| 168 | + * `wp_enqueue_style` to add your functionality to the block editor. | |
| 169 | + * | |
| 170 | + * @since 5.0.0 | |
| 171 | + */ | |
| 172 | + | |
| 173 | + do_action( 'enqueue_block_editor_assets' ); | |
| 174 | + | |
| 100 | 175 | wp_enqueue_script( 'wp-edit-site' ); |
| 101 | 176 | wp_enqueue_script( 'wp-format-library' ); |
| 102 | 177 | wp_enqueue_style( 'wp-edit-site' ); |
| 103 | 178 | wp_enqueue_style( 'wp-format-library' ); |
| @@ -102,4 +177,30 @@ | ||
| 102 | 177 | wp_enqueue_style( 'wp-edit-site' ); |
| 103 | 178 | wp_enqueue_style( 'wp-format-library' ); |
| 104 | 179 | } |
| 105 | 180 | add_action( 'admin_enqueue_scripts', 'gutenberg_edit_site_init' ); |
| 181 | + | |
| 182 | +/** | |
| 183 | + * Register a core site setting for front page information. | |
| 184 | + */ | |
| 185 | +function register_site_editor_homepage_settings() { | |
| 186 | + register_setting( | |
| 187 | + 'reading', | |
| 188 | + 'show_on_front', | |
| 189 | + array( | |
| 190 | + 'show_in_rest' => true, | |
| 191 | + 'type' => 'string', | |
| 192 | + 'description' => __( 'What to show on the front page', 'gutenberg' ), | |
| 193 | + ) | |
| 194 | + ); | |
| 195 | + | |
| 196 | + register_setting( | |
| 197 | + 'reading', | |
| 198 | + 'page_on_front', | |
| 199 | + array( | |
| 200 | + 'show_in_rest' => true, | |
| 201 | + 'type' => 'number', | |
| 202 | + 'description' => __( 'The ID of the page that should be displayed on the front page', 'gutenberg' ), | |
| 203 | + ) | |
| 204 | + ); | |
| 205 | +} | |
| 206 | +add_action( 'init', 'register_site_editor_homepage_settings', 10 ); | |