| @@ -5,58 +5,90 @@ | ||
| 5 | 5 | * @package gutenberg |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | + * Initialize a block-based editor. | |
| 10 | + * | |
| 11 | + * @param string $editor_name Editor name. | |
| 12 | + * @param string $editor_script_handle Editor script handle. | |
| 13 | + * @param array $settings { | |
| 14 | + * Elements to initialize a block-based editor. | |
| 15 | + * | |
| 16 | + * @type array $preload_paths Array of paths to preload. | |
| 17 | + * @type string $initializer_name Editor initialization function name. | |
| 18 | + * @type array $editor_settings Editor settings. | |
| 19 | + * } | |
| 20 | + * @return void | |
| 21 | + */ | |
| 22 | +function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $settings ) { | |
| 23 | + | |
| 24 | + $defaults = array( | |
| 25 | + 'preload_paths' => array(), | |
| 26 | + 'initializer_name' => 'initialize', | |
| 27 | + 'editor_settings' => array(), | |
| 28 | + ); | |
| 29 | + | |
| 30 | + $settings = wp_parse_args( $settings, $defaults ); | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Preload common data by specifying an array of REST API paths that will be preloaded. | |
| 34 | + * | |
| 35 | + * Filters the array of paths that will be preloaded. | |
| 36 | + * | |
| 37 | + * @param string[] $preload_paths Array of paths to preload. | |
| 38 | + */ | |
| 39 | + $preload_paths = apply_filters( "{$editor_name}_preload_paths", $settings['preload_paths'] ); | |
| 40 | + | |
| 41 | + $preload_data = array_reduce( | |
| 42 | + $preload_paths, | |
| 43 | + 'rest_preload_api_request', | |
| 44 | + array() | |
| 45 | + ); | |
| 46 | + | |
| 47 | + wp_add_inline_script( | |
| 48 | + 'wp-api-fetch', | |
| 49 | + sprintf( | |
| 50 | + 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', | |
| 51 | + wp_json_encode( $preload_data ) | |
| 52 | + ), | |
| 53 | + 'after' | |
| 54 | + ); | |
| 55 | + wp_add_inline_script( | |
| 56 | + "wp-{$editor_script_handle}", | |
| 57 | + sprintf( | |
| 58 | + 'wp.domReady( function() { | |
| 59 | + wp.%s.%s( "%s", %s ); | |
| 60 | + } );', | |
| 61 | + lcfirst( str_replace( '-', '', ucwords( $editor_script_handle, '-' ) ) ), | |
| 62 | + $settings['initializer_name'], | |
| 63 | + str_replace( '_', '-', $editor_name ), | |
| 64 | + wp_json_encode( $settings['editor_settings'] ) | |
| 65 | + ) | |
| 66 | + ); | |
| 67 | + | |
| 68 | + // Preload server-registered block schemas. | |
| 69 | + wp_add_inline_script( | |
| 70 | + 'wp-blocks', | |
| 71 | + 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' | |
| 72 | + ); | |
| 73 | +} | |
| 74 | + | |
| 75 | +/** | |
| 9 | 76 | * Sets a global JS variable used to trigger the availability of each Gutenberg Experiment. |
| 10 | 77 | */ |
| 11 | 78 | function gutenberg_enable_experiments() { |
| 12 | - global $pagenow; | |
| 13 | - | |
| 14 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-grid-interactivity' ) ) { | |
| 15 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableGridInteractivity = true', 'before' ); | |
| 79 | + $gutenberg_experiments = get_option( 'gutenberg-experiments' ); | |
| 80 | + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-zoomed-out-view', $gutenberg_experiments ) ) { | |
| 81 | + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableZoomedOutView = true', 'before' ); | |
| 16 | 82 | } |
| 17 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-global-styles-inheritance-ui' ) ) { | |
| 18 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalGlobalStylesInheritanceUI = true', 'before' ); | |
| 83 | + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-color-randomizer', $gutenberg_experiments ) ) { | |
| 84 | + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableColorRandomizer = true', 'before' ); | |
| 19 | 85 | } |
| 20 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-dataviews-media-modal' ) ) { | |
| 21 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalDataViewsMediaModal = true', 'before' ); | |
| 86 | + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-off-canvas-navigation-editor', $gutenberg_experiments ) ) { | |
| 87 | + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableOffCanvasNavigationEditor = true', 'before' ); | |
| 22 | 88 | } |
| 23 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-extensible-site-editor' ) ) { | |
| 24 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalExtensibleSiteEditor = true', 'before' ); | |
| 89 | + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-block-inspector-tabs', $gutenberg_experiments ) ) { | |
| 90 | + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableBlockInspectorTabs = true', 'before' ); | |
| 25 | 91 | } |
| 26 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-dataform-inspector' ) ) { | |
| 27 | - wp_add_inline_script( 'wp-editor', 'window.__experimentalDataFormInspector = true', 'before' ); | |
| 28 | - } | |
| 29 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-media-editor' ) ) { | |
| 30 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalMediaEditor = true', 'before' ); | |
| 31 | - } | |
| 32 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-dashboard-widgets' ) ) { | |
| 33 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalDashboardWidgets = true', 'before' ); | |
| 34 | - } | |
| 35 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-real-time-collaboration' ) ) { | |
| 36 | - $collaboration_enabled = ! ( | |
| 37 | - 'site-editor.php' === $pagenow || | |
| 38 | - ( 'admin.php' === $pagenow && isset( $_GET['page'] ) && 'site-editor-v2' === $_GET['page'] ) | |
| 39 | - ); | |
| 40 | - | |
| 41 | - wp_add_inline_script( | |
| 42 | - 'wp-core-data', | |
| 43 | - 'window.__experimentalEnableRealTimeCollaboration = ' . wp_json_encode( $collaboration_enabled ) . ';', | |
| 44 | - 'before' | |
| 45 | - ); | |
| 46 | - } | |
| 47 | 92 | } |
| 48 | 93 | |
| 49 | 94 | add_action( 'admin_init', 'gutenberg_enable_experiments' ); |
| 50 | -add_action( 'site-editor-v2_init', 'gutenberg_enable_experiments' ); | |
| 51 | - | |
| 52 | -/** | |
| 53 | - * Sets global JS variables used to enable various block experiments. | |
| 54 | - */ | |
| 55 | -function gutenberg_enable_block_experiments() { | |
| 56 | - // General experimental blocks that are not in the default block library. | |
| 57 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-block-experiments' ) ) { | |
| 58 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableBlockExperiments = true', 'before' ); | |
| 59 | - } | |
| 60 | -} | |
| 61 | - | |
| 62 | -add_action( 'admin_init', 'gutenberg_enable_block_experiments' ); | |