PluginProbe
Gutenberg / 12.0.0
Gutenberg v12.0.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / init.php

init.php in Gutenberg 12.0.0, at lib/init.php

237 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Init hooks.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Gutenberg's Menu.
10 *
11 * Adds a new wp-admin menu page for the Gutenberg editor.
12 *
13 * @since 0.1.0
14 */
15 function gutenberg_menu() {
16 add_menu_page(
17 'Gutenberg',
18 'Gutenberg',
19 'edit_posts',
20 'gutenberg',
21 '',
22 'dashicons-edit'
23 );
24
25 add_submenu_page(
26 'gutenberg',
27 __( 'Demo', 'gutenberg' ),
28 __( 'Demo', 'gutenberg' ),
29 'edit_posts',
30 'gutenberg'
31 );
32
33 if (
34 gutenberg_use_widgets_block_editor() &&
35 ! function_exists( 'wp_use_widgets_block_editor' ) &&
36 current_theme_supports( 'widgets' )
37 ) {
38 add_theme_page(
39 __( 'Widgets', 'gutenberg' ),
40 __( 'Widgets', 'gutenberg' ),
41 'edit_theme_options',
42 'gutenberg-widgets',
43 'the_gutenberg_widgets',
44 2
45 );
46 remove_submenu_page( 'themes.php', 'widgets.php' );
47 }
48
49 if ( get_option( 'gutenberg-experiments' ) ) {
50 if ( array_key_exists( 'gutenberg-navigation', get_option( 'gutenberg-experiments' ) ) ) {
51 add_submenu_page(
52 'gutenberg',
53 __( 'Navigation (beta)', 'gutenberg' ),
54 __( 'Navigation (beta)', 'gutenberg' ),
55 'edit_theme_options',
56 'gutenberg-navigation',
57 'gutenberg_navigation_page'
58 );
59 }
60 }
61 if ( current_user_can( 'edit_posts' ) ) {
62 add_submenu_page(
63 'gutenberg',
64 __( 'Support', 'gutenberg' ),
65 __( 'Support', 'gutenberg' ),
66 'edit_posts',
67 __( 'https://wordpress.org/support/plugin/gutenberg/', 'gutenberg' )
68 );
69 add_submenu_page(
70 'gutenberg',
71 __( 'Documentation', 'gutenberg' ),
72 __( 'Documentation', 'gutenberg' ),
73 'edit_posts',
74 'https://developer.wordpress.org/block-editor/'
75 );
76 }
77
78 add_submenu_page(
79 'gutenberg',
80 __( 'Experiments Settings', 'gutenberg' ),
81 __( 'Experiments', 'gutenberg' ),
82 'edit_posts',
83 'gutenberg-experiments',
84 'the_gutenberg_experiments'
85 );
86 }
87 add_action( 'admin_menu', 'gutenberg_menu', 9 );
88
89 /**
90 * Site editor's Menu.
91 *
92 * Adds a new wp-admin menu item for the Site editor.
93 *
94 * @since 9.4.0
95 */
96 function gutenberg_site_editor_menu() {
97 if ( gutenberg_experimental_is_site_editor_available() ) {
98 add_theme_page(
99 __( 'Editor (beta)', 'gutenberg' ),
100 sprintf(
101 /* translators: %s: "beta" label. */
102 __( 'Editor %s', 'gutenberg' ),
103 '<span class="awaiting-mod">' . __( 'beta', 'gutenberg' ) . '</span>'
104 ),
105 'edit_theme_options',
106 'gutenberg-edit-site',
107 'gutenberg_edit_site_page'
108 );
109
110 add_theme_page(
111 __( 'Styles', 'gutenberg' ),
112 __( 'Styles', 'gutenberg' ),
113 'edit_theme_options',
114 'gutenberg-edit-site&styles=open',
115 'gutenberg_edit_site_page'
116 );
117 }
118 }
119 add_action( 'admin_menu', 'gutenberg_site_editor_menu', 9 );
120
121 /**
122 * Modify WP admin bar.
123 *
124 * @param WP_Admin_Bar $wp_admin_bar Core class used to implement the Toolbar API.
125 */
126 function modify_admin_bar( $wp_admin_bar ) {
127 if (
128 gutenberg_use_widgets_block_editor() &&
129 ! function_exists( 'wp_use_widgets_block_editor' ) &&
130 $wp_admin_bar->get_node( 'widgets' ) !== null
131 ) {
132 $wp_admin_bar->add_menu(
133 array(
134 'id' => 'widgets',
135 'href' => admin_url( 'themes.php?page=gutenberg-widgets' ),
136 )
137 );
138 }
139 }
140 add_action( 'admin_bar_menu', 'modify_admin_bar', 40 );
141
142
143 remove_action( 'welcome_panel', 'wp_welcome_panel' );
144 /**
145 * Modify Dashboard welcome panel.
146 *
147 * When widgets are merged in core this should go into `wp-admin/includes/dashboard.php`
148 * and replace the widgets link in the `wp_welcome_panel` checking for the same condition,
149 * because then `gutenberg_use_widgets_block_editor` will exist in core.
150 */
151 function modify_welcome_panel() {
152 ob_start();
153 wp_welcome_panel();
154 $welcome_panel = ob_get_clean();
155 if (
156 gutenberg_use_widgets_block_editor() &&
157 ! function_exists( 'wp_use_widgets_block_editor' )
158 ) {
159 echo str_replace(
160 admin_url( 'widgets.php' ),
161 admin_url( 'themes.php?page=gutenberg-widgets' ),
162 $welcome_panel
163 );
164 } else {
165 echo $welcome_panel;
166 }
167 }
168 add_action( 'welcome_panel', 'modify_welcome_panel', 40 );
169
170 /**
171 * Outputs a WP REST API nonce.
172 */
173 function gutenberg_rest_nonce() {
174 exit( wp_create_nonce( 'wp_rest' ) );
175 }
176 add_action( 'wp_ajax_gutenberg_rest_nonce', 'gutenberg_rest_nonce' );
177
178
179 /**
180 * Exposes the site icon url to the Gutenberg editor through the WordPress REST
181 * API. The site icon url should instead be fetched from the wp/v2/settings
182 * endpoint when https://github.com/WordPress/gutenberg/pull/19967 is complete.
183 *
184 * @since 8.2.1
185 *
186 * @param WP_REST_Response $response Response data served by the WordPress REST index endpoint.
187 * @return WP_REST_Response
188 */
189 function register_site_icon_url( $response ) {
190 $data = $response->data;
191 $data['site_icon_url'] = get_site_icon_url();
192 $response->set_data( $data );
193 return $response;
194 }
195
196 add_filter( 'rest_index', 'register_site_icon_url' );
197
198 /**
199 * Exposes the site logo to the Gutenberg editor through the WordPress REST
200 * API. This is used for fetching this information when user has no rights
201 * to update settings.
202 *
203 * @since 10.9
204 *
205 * @param WP_REST_Response $response Response data served by the WordPress REST index endpoint.
206 * @return WP_REST_Response
207 */
208 function register_site_logo_to_rest_index( $response ) {
209 $site_logo_id = get_theme_mod( 'custom_logo' );
210 $response->data['site_logo'] = $site_logo_id;
211 if ( $site_logo_id ) {
212 $response->add_link(
213 'https://api.w.org/featuredmedia',
214 rest_url( 'wp/v2/media/' . $site_logo_id ),
215 array(
216 'embeddable' => true,
217 )
218 );
219 }
220 return $response;
221 }
222
223 add_filter( 'rest_index', 'register_site_logo_to_rest_index' );
224
225 add_theme_support( 'widgets-block-editor' );
226
227 /**
228 * Enable block templates (editor mode) for themes with theme.json.
229 */
230 function gutenberg_enable_block_templates() {
231 if ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
232 add_theme_support( 'block-templates' );
233 }
234 }
235
236 add_action( 'setup_theme', 'gutenberg_enable_block_templates' );
237