PluginProbe
Gutenberg / 12.6.0
Gutenberg v12.6.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 / navigation-page.php

navigation-page.php in Gutenberg 12.6.0, at lib/navigation-page.php

176 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Bootstrapping the Gutenberg Navigation page.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * The main entry point for the Gutenberg Navigation page.
10 *
11 * @since 7.8.0
12 */
13 function gutenberg_navigation_page() {
14 ?>
15 <div
16 id="navigation-editor"
17 class="edit-navigation"
18 >
19 </div>
20 <?php
21 }
22
23 /**
24 * This function returns an url for the /wp/v2/menus endpoint
25 *
26 * @since 11.8.0
27 *
28 * @param int $results_per_page Results per page.
29 * @return string
30 */
31 function gutenberg_navigation_get_menus_endpoint( $results_per_page = 100 ) {
32 return '/wp/v2/menus?' . build_query(
33 array(
34 'per_page' => $results_per_page,
35 'context' => 'edit',
36 '_locale' => 'user',
37 )
38 );
39 }
40
41 /**
42 * This function returns an url for the /wp/v2/menu-items endpoint
43 *
44 * @since 11.8.0
45 *
46 * @param int $menu_id Menu ID.
47 * @param int $results_per_page Results per page.
48 * @return string
49 */
50 function gutenberg_navigation_get_menu_items_endpoint( $menu_id, $results_per_page = 100 ) {
51 return '/wp/v2/menu-items?' . build_query(
52 array(
53 'context' => 'edit',
54 'menus' => $menu_id,
55 'per_page' => $results_per_page,
56 '_locale' => 'user',
57 )
58 );
59 }
60
61 /**
62 * This function returns an url for the /wp/v2/types endpoint
63 *
64 * @since 11.8.0
65 *
66 * @return string
67 */
68 function gutenberg_navigation_get_types_endpoint() {
69 return '/wp/v2/types?' . build_query(
70 array(
71 'context' => 'edit',
72 )
73 );
74 }
75
76 /**
77 * Initialize the Gutenberg Navigation page.
78 *
79 * @param string $hook Page.
80 * @since 7.8.0
81 */
82 function gutenberg_navigation_init( $hook ) {
83 if ( 'gutenberg_page_gutenberg-navigation' !== $hook ) {
84 return;
85 }
86
87 $menus = wp_get_nav_menus();
88 $first_menu_id = ! empty( $menus ) ? $menus[0]->term_id : null;
89
90 $preload_paths = array(
91 '/wp/v2/menu-locations',
92 array( '/wp/v2/pages', 'OPTIONS' ),
93 array( '/wp/v2/posts', 'OPTIONS' ),
94 gutenberg_navigation_get_types_endpoint(),
95 );
96
97 if ( $first_menu_id ) {
98 $preload_paths[] = gutenberg_navigation_get_menu_items_endpoint( $first_menu_id );
99 }
100
101 $settings = array_merge(
102 get_default_block_editor_settings(),
103 array(
104 'blockNavMenus' => false,
105 // We should uncomment the line below when the block-nav-menus feature becomes stable.
106 // @see https://github.com/WordPress/gutenberg/issues/34265.
107 /*'blockNavMenus' => get_theme_support( 'block-nav-menus' ),*/
108 )
109 );
110 $settings = gutenberg_experimental_global_styles_settings( $settings );
111
112 gutenberg_initialize_editor(
113 'navigation_editor',
114 'edit-navigation',
115 array(
116 'initializer_name' => 'initialize',
117 'editor_settings' => $settings,
118 'preload_paths' => $preload_paths,
119 )
120 );
121
122 gutenberg_navigation_editor_preload_menus();
123
124 wp_enqueue_script( 'wp-edit-navigation' );
125 wp_enqueue_style( 'wp-edit-navigation' );
126 wp_enqueue_script( 'wp-format-library' );
127 wp_enqueue_style( 'wp-format-library' );
128 do_action( 'enqueue_block_editor_assets' );
129 }
130 add_action( 'admin_enqueue_scripts', 'gutenberg_navigation_init' );
131
132 /**
133 * Tells the script loader to load the scripts and styles of custom block on navigation editor screen.
134 *
135 * @param bool $is_block_editor_screen Current decision about loading block assets.
136 * @return bool Filtered decision about loading block assets.
137 */
138 function gutenberg_navigation_editor_load_block_editor_scripts_and_styles( $is_block_editor_screen ) {
139 if ( is_callable( 'get_current_screen' ) && get_current_screen() && 'gutenberg_page_gutenberg-navigation' === get_current_screen()->base ) {
140 return true;
141 }
142
143 return $is_block_editor_screen;
144 }
145
146 add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_navigation_editor_load_block_editor_scripts_and_styles' );
147
148 /**
149 * This function calls createMenuPreloadingMiddleware middleware because
150 * we need to use custom preloading logic for menus.
151 *
152 * @return void
153 */
154 function gutenberg_navigation_editor_preload_menus() {
155 $menus_data = array_reduce(
156 array(
157 gutenberg_navigation_get_menus_endpoint(),
158 ),
159 'rest_preload_api_request',
160 array()
161 );
162
163 if ( ! $menus_data ) {
164 return;
165 }
166
167 wp_add_inline_script(
168 'wp-edit-navigation',
169 sprintf(
170 'wp.apiFetch.use( wp.editNavigation.__unstableCreateMenuPreloadingMiddleware( %s ) );',
171 wp_json_encode( $menus_data )
172 ),
173 'after'
174 );
175 }
176