| 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 |
$custom_settings = array( |
| 102 |
'blockNavMenus' => false, |
| 103 |
// We should uncomment the line below when the block-nav-menus feature becomes stable. |
| 104 |
// @see https://github.com/WordPress/gutenberg/issues/34265. |
| 105 |
/*'blockNavMenus' => get_theme_support( 'block-nav-menus' ),*/ |
| 106 |
); |
| 107 |
|
| 108 |
$context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-navigation' ) ); |
| 109 |
$settings = get_block_editor_settings( $custom_settings, $context ); |
| 110 |
gutenberg_initialize_editor( |
| 111 |
'navigation_editor', |
| 112 |
'edit-navigation', |
| 113 |
array( |
| 114 |
'initializer_name' => 'initialize', |
| 115 |
'editor_settings' => $settings, |
| 116 |
'preload_paths' => $preload_paths, |
| 117 |
) |
| 118 |
); |
| 119 |
|
| 120 |
gutenberg_navigation_editor_preload_menus(); |
| 121 |
|
| 122 |
wp_enqueue_script( 'wp-edit-navigation' ); |
| 123 |
wp_enqueue_style( 'wp-edit-navigation' ); |
| 124 |
wp_enqueue_script( 'wp-format-library' ); |
| 125 |
wp_enqueue_style( 'wp-format-library' ); |
| 126 |
do_action( 'enqueue_block_editor_assets' ); |
| 127 |
} |
| 128 |
add_action( 'admin_enqueue_scripts', 'gutenberg_navigation_init' ); |
| 129 |
|
| 130 |
/** |
| 131 |
* Tells the script loader to load the scripts and styles of custom block on navigation editor screen. |
| 132 |
* |
| 133 |
* @param bool $is_block_editor_screen Current decision about loading block assets. |
| 134 |
* @return bool Filtered decision about loading block assets. |
| 135 |
*/ |
| 136 |
function gutenberg_navigation_editor_load_block_editor_scripts_and_styles( $is_block_editor_screen ) { |
| 137 |
if ( is_callable( 'get_current_screen' ) && get_current_screen() && 'gutenberg_page_gutenberg-navigation' === get_current_screen()->base ) { |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
return $is_block_editor_screen; |
| 142 |
} |
| 143 |
|
| 144 |
add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_navigation_editor_load_block_editor_scripts_and_styles' ); |
| 145 |
|
| 146 |
/** |
| 147 |
* This function calls createMenuPreloadingMiddleware middleware because |
| 148 |
* we need to use custom preloading logic for menus. |
| 149 |
* |
| 150 |
* @return void |
| 151 |
*/ |
| 152 |
function gutenberg_navigation_editor_preload_menus() { |
| 153 |
$menus_data = array_reduce( |
| 154 |
array( |
| 155 |
gutenberg_navigation_get_menus_endpoint(), |
| 156 |
), |
| 157 |
'rest_preload_api_request', |
| 158 |
array() |
| 159 |
); |
| 160 |
|
| 161 |
if ( ! $menus_data ) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
wp_add_inline_script( |
| 166 |
'wp-edit-navigation', |
| 167 |
sprintf( |
| 168 |
'wp.apiFetch.use( wp.editNavigation.__unstableCreateMenuPreloadingMiddleware( %s ) );', |
| 169 |
wp_json_encode( $menus_data ) |
| 170 |
), |
| 171 |
'after' |
| 172 |
); |
| 173 |
} |
| 174 |
|