-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
edit-site-page.php
180 lines (161 loc) · 4.73 KB
/
edit-site-page.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* Bootstrapping the Gutenberg Edit Site Page.
*
* @package gutenberg
*/
/**
* The main entry point for the Gutenberg Edit Site Page.
*
* @since 7.2.0
*/
function gutenberg_edit_site_page() {
?>
<div
id="edit-site-editor"
class="edit-site"
>
</div>
<?php
}
/**
* Checks whether the provided page is one of allowed Site Editor pages.
*
* @param string $page Page to check.
*
* @return bool True for Site Editor pages, false otherwise.
*/
function gutenberg_is_edit_site_page( $page ) {
return 'toplevel_page_gutenberg-edit-site' === $page;
}
/**
* Load editor styles (this is copied from edit-form-blocks.php).
* Ideally the code is extracted into a reusable function.
*
* @return array Editor Styles Setting.
*/
function gutenberg_get_editor_styles() {
global $editor_styles;
// Ideally the code is extracted into a reusable function.
$styles = array();
if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) {
foreach ( $editor_styles as $style ) {
if ( preg_match( '~^(https?:)?//~', $style ) ) {
$response = wp_remote_get( $style );
if ( ! is_wp_error( $response ) ) {
$styles[] = array(
'css' => wp_remote_retrieve_body( $response ),
);
}
} else {
$file = get_theme_file_path( $style );
if ( is_file( $file ) ) {
$styles[] = array(
'css' => file_get_contents( $file ),
'baseURL' => get_theme_file_uri( $style ),
);
}
}
}
}
return $styles;
}
/**
* Initialize the Gutenberg Edit Site Page.
*
* @since 7.2.0
*
* @param string $hook Page.
*/
function gutenberg_edit_site_init( $hook ) {
global $current_screen, $post, $editor_styles;
if ( ! gutenberg_is_edit_site_page( $hook ) ) {
return;
}
/**
* Make the WP Screen object aware that this is a block editor page.
* Since custom blocks check whether the screen is_block_editor,
* this is required for custom blocks to be loaded.
* See wp_enqueue_registered_block_scripts_and_styles in wp-includes/script-loader.php
*/
$current_screen->is_block_editor( true );
$custom_settings = array(
'siteUrl' => site_url(),
'postsPerPage' => get_option( 'posts_per_page' ),
'styles' => gutenberg_get_editor_styles(),
'defaultTemplateTypes' => gutenberg_get_indexed_default_template_types(),
'defaultTemplatePartAreas' => gutenberg_get_allowed_template_part_areas(),
'__experimentalBlockPatterns' => WP_Block_Patterns_Registry::get_instance()->get_all_registered(),
'__experimentalBlockPatternCategories' => WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(),
);
$site_editor_context = new WP_Block_Editor_Context();
$settings = gutenberg_get_block_editor_settings( $custom_settings, $site_editor_context );
gutenberg_initialize_editor(
'edit_site_editor',
'edit-site',
array(
'preload_paths' => array(
array( '/wp/v2/media', 'OPTIONS' ),
'/',
'/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.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ),
'after'
);
wp_enqueue_script( 'wp-edit-site' );
wp_enqueue_script( 'wp-format-library' );
wp_enqueue_style( 'wp-edit-site' );
wp_enqueue_style( 'wp-format-library' );
wp_enqueue_media();
if (
current_theme_supports( 'wp-block-styles' ) ||
( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 )
) {
wp_enqueue_style( 'wp-block-library-theme' );
}
/**
* Fires after block assets have been enqueued for the editing interface.
*
* Call `add_action` on any hook before 'admin_enqueue_scripts'.
*
* In the function call you supply, simply use `wp_enqueue_script` and
* `wp_enqueue_style` to add your functionality to the block editor.
*
* @since 5.0.0
*/
do_action( 'enqueue_block_editor_assets' );
}
add_action( 'admin_enqueue_scripts', 'gutenberg_edit_site_init' );
/**
* Register a core site setting for front page information.
*/
function register_site_editor_homepage_settings() {
register_setting(
'reading',
'show_on_front',
array(
'show_in_rest' => true,
'type' => 'string',
'description' => __( 'What to show on the front page', 'gutenberg' ),
)
);
register_setting(
'reading',
'page_on_front',
array(
'show_in_rest' => true,
'type' => 'number',
'description' => __( 'The ID of the page that should be displayed on the front page', 'gutenberg' ),
)
);
}
add_action( 'init', 'register_site_editor_homepage_settings', 10 );