-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy patheditor-settings.php
95 lines (85 loc) · 3.07 KB
/
editor-settings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* Utilities to manage editor settings.
*
* @package gutenberg
*/
/**
* 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() ) . ');'
);
}
/**
* Sets a global JS variable used to trigger the availability of each Gutenberg Experiment.
*/
function gutenberg_enable_experiments() {
$gutenberg_experiments = get_option( 'gutenberg-experiments' );
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-zoomed-out-view', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableZoomedOutView = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-color-randomizer', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableColorRandomizer = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-group-grid-variation', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableGroupGridVariation = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-pattern-enhancements', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnablePatternEnhancements = true', 'before' );
}
}
add_action( 'admin_init', 'gutenberg_enable_experiments' );