| @@ -5,67 +5,94 @@ | ||
| 5 | 5 | * @package gutenberg |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | - * Sets a global JS variable used to trigger the availability of each Gutenberg Experiment. | |
| 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 | |
| 10 | 21 | */ |
| 11 | -function gutenberg_enable_experiments() { | |
| 12 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-color-randomizer' ) ) { | |
| 13 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableColorRandomizer = true', 'before' ); | |
| 14 | - } | |
| 15 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-grid-interactivity' ) ) { | |
| 16 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableGridInteractivity = true', 'before' ); | |
| 17 | - } | |
| 18 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-dataviews-media-modal' ) ) { | |
| 19 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalDataViewsMediaModal = true', 'before' ); | |
| 20 | - } | |
| 21 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-content-only-inspector-fields' ) ) { | |
| 22 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalContentOnlyInspectorFields = true', 'before' ); | |
| 23 | - } | |
| 24 | - if ( gutenberg_is_experiment_enabled( 'active_templates' ) ) { | |
| 25 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalTemplateActivate = true', 'before' ); | |
| 26 | - } | |
| 27 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-extensible-site-editor' ) ) { | |
| 28 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalExtensibleSiteEditor = true', 'before' ); | |
| 29 | - } | |
| 30 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-dataform-inspector' ) ) { | |
| 31 | - wp_add_inline_script( 'wp-editor', 'window.__experimentalDataFormInspector = true', 'before' ); | |
| 32 | - } | |
| 33 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-media-editor' ) ) { | |
| 34 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalMediaEditor = true', 'before' ); | |
| 35 | - } | |
| 36 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-dashboard-widgets' ) ) { | |
| 37 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalDashboardWidgets = true', 'before' ); | |
| 38 | - } | |
| 39 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-classic-block-deprecation' ) ) { | |
| 40 | - wp_add_inline_script( 'wp-block-library', 'window.__experimentalClassicBlockDeprecation = true', 'before' ); | |
| 41 | - } | |
| 42 | -} | |
| 22 | +function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $settings ) { | |
| 43 | 23 | |
| 44 | -add_action( 'admin_init', 'gutenberg_enable_experiments' ); | |
| 45 | -add_action( 'site-editor-v2_init', 'gutenberg_enable_experiments' ); | |
| 24 | + $defaults = array( | |
| 25 | + 'preload_paths' => array(), | |
| 26 | + 'initializer_name' => 'initialize', | |
| 27 | + 'editor_settings' => array(), | |
| 28 | + ); | |
| 46 | 29 | |
| 47 | -/** | |
| 48 | - * Sets a global JS variable used to trigger the availability of form & input blocks. | |
| 49 | - * | |
| 50 | - * @deprecated 19.0.0 Use gutenberg_enable_block_experiments(). | |
| 51 | - */ | |
| 52 | -function gutenberg_enable_form_input_blocks() { | |
| 53 | - _deprecated_function( __FUNCTION__, 'Gutenberg 19.0.0', 'gutenberg_enable_block_experiments' ); | |
| 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 | + ); | |
| 54 | 73 | } |
| 55 | 74 | |
| 56 | 75 | /** |
| 57 | - * Sets global JS variables used to enable various block experiments. | |
| 76 | + * Sets a global JS variable used to trigger the availability of each Gutenberg Experiment. | |
| 58 | 77 | */ |
| 59 | -function gutenberg_enable_block_experiments() { | |
| 60 | - // Experimental form blocks. | |
| 61 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-form-blocks' ) ) { | |
| 62 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableFormBlocks = true', 'before' ); | |
| 78 | +function gutenberg_enable_experiments() { | |
| 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' ); | |
| 63 | 82 | } |
| 83 | + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-color-randomizer', $gutenberg_experiments ) ) { | |
| 84 | + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableColorRandomizer = true', 'before' ); | |
| 85 | + } | |
| 86 | + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-group-grid-variation', $gutenberg_experiments ) ) { | |
| 87 | + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableGroupGridVariation = true', 'before' ); | |
| 88 | + } | |
| 89 | + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-interactivity-api-core-blocks', $gutenberg_experiments ) ) { | |
| 90 | + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalInteractivityAPI = true', 'before' ); | |
| 91 | + } | |
| 64 | 92 | |
| 65 | - // General experimental blocks that are not in the default block library. | |
| 66 | - if ( gutenberg_is_experiment_enabled( 'gutenberg-block-experiments' ) ) { | |
| 67 | - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableBlockExperiments = true', 'before' ); | |
| 93 | + if ( gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) ) { | |
| 94 | + wp_add_inline_script( 'wp-block-library', 'window.__experimentalDisableTinymce = true', 'before' ); | |
| 68 | 95 | } |
| 69 | 96 | } |
| 70 | 97 | |
| 71 | -add_action( 'admin_init', 'gutenberg_enable_block_experiments' ); | |
| 98 | +add_action( 'admin_init', 'gutenberg_enable_experiments' ); | |