Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preload_paths filter for widgets screen and full site editing #28701

Merged
merged 8 commits into from
Mar 11, 2021
62 changes: 62 additions & 0 deletions lib/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,65 @@ 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 array $settings {
* Elements to initialize a block-based editor.
*
* @type array $preload_paths paths need to be preloaded.
* @type string $editor_name editor name.
* @type string $editor_script_handle editor script handle.
* @type string $initializer_name editor initialization function name.
* @type array $editor_settings editor settings.
* }
* @return void
*/
function gutenberg_initialize_editor( $settings ) {

$defaults = array(
'preload_paths' => array(),
'editor_name' => 'widgets_editor',
'editor_script_handle' => 'edit-widgets',
manooweb marked this conversation as resolved.
Show resolved Hide resolved
'initializer_name' => 'initialize',
'editor_settings' => array(),
);

$settings = wp_parse_args( $settings, $defaults );

$preload_paths = apply_filters( "{$settings['editor_name']}_preload_paths", $settings['preload_paths'] );
manooweb marked this conversation as resolved.
Show resolved Hide resolved

$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-{$settings['editor_script_handle']}",
sprintf(
'wp.domReady( function() {
wp.%s.%s( "%s", %s );
} );',
lcfirst( str_replace( '-', '', ucwords( $settings['editor_script_handle'], '-' ) ) ),
$settings['initializer_name'],
str_replace( '_', '-', $settings['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() ) . ');'
);

}
49 changes: 14 additions & 35 deletions lib/full-site-editing/edit-site-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
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',
),
'editor_name' => 'edit_site_editor',
'editor_script_handle' => 'edit-site',
'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 ) ) ),
Expand Down
19 changes: 6 additions & 13 deletions lib/navigation-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
array(
'editor_name' => 'navigation_editor',
'editor_script_handle' => 'edit-navigation',
'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' );
Expand Down
43 changes: 8 additions & 35 deletions lib/widgets-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -54,42 +52,17 @@ 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(
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' );
Expand Down