| @@ -20,156 +20,46 @@ | ||
| 20 | 20 | <?php |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
| 24 | - * This function returns an url for the /wp/v2/menus endpoint | |
| 24 | + * Initialize the Gutenberg Navigation page. | |
| 25 | 25 | * |
| 26 | - * @since 11.8.0 | |
| 26 | + * @since 7.8.0 | |
| 27 | 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 | 28 | * @param string $hook Page. |
| 80 | - * @since 7.8.0 | |
| 81 | 29 | */ |
| 82 | 30 | function gutenberg_navigation_init( $hook ) { |
| 83 | 31 | if ( 'gutenberg_page_gutenberg-navigation' !== $hook ) { |
| 84 | - return; | |
| 32 | + return; | |
| 85 | 33 | } |
| 86 | 34 | |
| 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 | 35 | $settings = array_merge( |
| 102 | - get_default_block_editor_settings(), | |
| 36 | + gutenberg_get_common_block_editor_settings(), | |
| 103 | 37 | 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' ),*/ | |
| 38 | + 'blockNavMenus' => get_theme_support( 'block-nav-menus' ), | |
| 108 | 39 | ) |
| 109 | 40 | ); |
| 110 | 41 | $settings = gutenberg_experimental_global_styles_settings( $settings ); |
| 111 | 42 | |
| 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, | |
| 43 | + wp_add_inline_script( | |
| 44 | + 'wp-edit-navigation', | |
| 45 | + sprintf( | |
| 46 | + 'wp.domReady( function() { | |
| 47 | + wp.editNavigation.initialize( "navigation-editor", %s ); | |
| 48 | + } );', | |
| 49 | + wp_json_encode( $settings ) | |
| 119 | 50 | ) |
| 120 | 51 | ); |
| 121 | 52 | |
| 122 | - gutenberg_navigation_editor_preload_menus(); | |
| 53 | + // Preload server-registered block schemas. | |
| 54 | + wp_add_inline_script( | |
| 55 | + 'wp-blocks', | |
| 56 | + 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' | |
| 57 | + ); | |
| 123 | 58 | |
| 124 | 59 | wp_enqueue_script( 'wp-edit-navigation' ); |
| 125 | 60 | wp_enqueue_style( 'wp-edit-navigation' ); |
| 61 | + wp_enqueue_style( 'wp-block-library' ); | |
| 126 | 62 | wp_enqueue_script( 'wp-format-library' ); |
| 127 | 63 | wp_enqueue_style( 'wp-format-library' ); |
| 128 | - do_action( 'enqueue_block_editor_assets' ); | |
| 129 | 64 | } |
| 130 | 65 | 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 | -} | |