diff --git a/lib/editor-settings.php b/lib/editor-settings.php index e50141f1f3aca..9b0c7f7d65b27 100644 --- a/lib/editor-settings.php +++ b/lib/editor-settings.php @@ -81,3 +81,70 @@ function gutenberg_extend_post_editor_settings( $settings ) { return $settings; } add_filter( 'block_editor_settings', 'gutenberg_extend_post_editor_settings' ); + +/** + * Initialize a block-based editor. + * + * @param string $editor_name Editor name. + * @param string $editor_script_handle Editor script handle. + * @param array $settings { + * Elements to initialize a block-based editor. + * + * @type array $preload_paths Array of paths to preload. + * @type string $initializer_name Editor initialization function name. + * @type array $editor_settings Editor settings. + * } + * @return void + */ +function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $settings ) { + + $defaults = array( + 'preload_paths' => array(), + 'initializer_name' => 'initialize', + 'editor_settings' => array(), + ); + + $settings = wp_parse_args( $settings, $defaults ); + + /** + * Preload common data by specifying an array of REST API paths that will be preloaded. + * + * Filters the array of paths that will be preloaded. + * + * @param string[] $preload_paths Array of paths to preload. + */ + $preload_paths = apply_filters( "{$editor_name}_preload_paths", $settings['preload_paths'] ); + + $preload_data = array_reduce( + $preload_paths, + 'rest_preload_api_request', + array() + ); + wp_add_inline_script( + 'wp-api-fetch', + sprintf( + 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', + wp_json_encode( $preload_data ) + ), + 'after' + ); + wp_add_inline_script( + "wp-{$editor_script_handle}", + sprintf( + 'wp.domReady( function() { + wp.%s.%s( "%s", %s ); + } );', + lcfirst( str_replace( '-', '', ucwords( $editor_script_handle, '-' ) ) ), + $settings['initializer_name'], + str_replace( '_', '-', $editor_name ), + wp_json_encode( $settings['editor_settings'] ) + ) + ); + + // Preload server-registered block schemas. + wp_add_inline_script( + 'wp-blocks', + 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' + ); + +} diff --git a/lib/full-site-editing/edit-site-page.php b/lib/full-site-editing/edit-site-page.php index d876f3878316e..137fc45aeaf9b 100644 --- a/lib/full-site-editing/edit-site-page.php +++ b/lib/full-site-editing/edit-site-page.php @@ -113,44 +113,23 @@ function gutenberg_edit_site_init( $hook ) { ); $settings = gutenberg_experimental_global_styles_settings( $settings ); - // Preload block editor paths. - // most of these are copied from edit-forms-blocks.php. - $preload_paths = array( - '/?context=edit', - '/wp/v2/types?context=edit', - '/wp/v2/taxonomies?context=edit', - '/wp/v2/pages?context=edit', - '/wp/v2/themes?status=active', - array( '/wp/v2/media', 'OPTIONS' ), - ); - $preload_data = array_reduce( - $preload_paths, - 'rest_preload_api_request', - array() - ); - wp_add_inline_script( - 'wp-api-fetch', - sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode( $preload_data ) ), - 'after' - ); - - // Initialize editor. - wp_add_inline_script( - 'wp-edit-site', - sprintf( - 'wp.domReady( function() { - wp.editSite.initialize( "edit-site-editor", %s ); - } );', - wp_json_encode( $settings ) + gutenberg_initialize_editor( + 'edit_site_editor', + 'edit-site', + array( + 'preload_paths' => array( + array( '/wp/v2/media', 'OPTIONS' ), + '/?context=edit', + '/wp/v2/types?context=edit', + '/wp/v2/taxonomies?context=edit', + '/wp/v2/pages?context=edit', + '/wp/v2/themes?status=active', + ), + 'initializer_name' => 'initialize', + 'editor_settings' => $settings, ) ); - wp_add_inline_script( - 'wp-blocks', - sprintf( 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions( %s );', wp_json_encode( get_block_editor_server_block_settings() ) ), - 'after' - ); - wp_add_inline_script( 'wp-blocks', sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ), diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 2ea6ff94f1ec2..d67e9bd13442b 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -40,22 +40,15 @@ function gutenberg_navigation_init( $hook ) { ); $settings = gutenberg_experimental_global_styles_settings( $settings ); - wp_add_inline_script( - 'wp-edit-navigation', - sprintf( - 'wp.domReady( function() { - wp.editNavigation.initialize( "navigation-editor", %s ); - } );', - wp_json_encode( $settings ) + gutenberg_initialize_editor( + 'navigation_editor', + 'edit-navigation', + array( + 'initializer_name' => 'initialize', + 'editor_settings' => $settings, ) ); - // Preload server-registered block schemas. - wp_add_inline_script( - 'wp-blocks', - 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' - ); - wp_enqueue_script( 'wp-edit-navigation' ); wp_enqueue_style( 'wp-edit-navigation' ); wp_enqueue_style( 'wp-block-library' ); diff --git a/lib/widgets-page.php b/lib/widgets-page.php index 6815bf5b75b7a..721f75a109b30 100644 --- a/lib/widgets-page.php +++ b/lib/widgets-page.php @@ -41,8 +41,6 @@ function gutenberg_widgets_init( $hook ) { return; } - $initializer_name = 'initialize'; - $settings = array_merge( gutenberg_get_common_block_editor_settings(), gutenberg_get_legacy_widget_settings() @@ -54,42 +52,19 @@ function gutenberg_widgets_init( $hook ) { $settings = gutenberg_experimental_global_styles_settings( $settings ); $settings = gutenberg_extend_block_editor_styles( $settings ); - $preload_paths = array( - array( '/wp/v2/media', 'OPTIONS' ), - '/wp/v2/sidebars?context=edit&per_page=-1', - '/wp/v2/widgets?context=edit&per_page=-1', - ); - $preload_data = array_reduce( - $preload_paths, - 'rest_preload_api_request', - array() - ); - wp_add_inline_script( - 'wp-api-fetch', - sprintf( - 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', - wp_json_encode( $preload_data ) - ), - 'after' - ); - - wp_add_inline_script( - 'wp-edit-widgets', - sprintf( - 'wp.domReady( function() { - wp.editWidgets.%s( "widgets-editor", %s ); - } );', - $initializer_name, - wp_json_encode( $settings ) + gutenberg_initialize_editor( + 'widgets_editor', + 'edit-widgets', + array( + 'preload_paths' => array( + array( '/wp/v2/media', 'OPTIONS' ), + '/wp/v2/sidebars?context=edit&per_page=-1', + '/wp/v2/widgets?context=edit&per_page=-1', + ), + 'editor_settings' => $settings, ) ); - // Preload server-registered block schemas. - wp_add_inline_script( - 'wp-blocks', - 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' - ); - wp_enqueue_script( 'wp-edit-widgets' ); wp_enqueue_script( 'admin-widgets' ); wp_enqueue_script( 'wp-format-library' );