Skip to content

Commit

Permalink
Add Navigation REST class with fallback endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Mar 22, 2023
1 parent b93fd88 commit 56e702c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
76 changes: 76 additions & 0 deletions lib/experimental/class-wp-rest-navigation-controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* REST API: Gutenberg_REST_Navigation_Controller class
*
* @package Gutenberg
* @subpackage REST_API
*/

/**
* Base Templates REST API Controller.
*/
class Gutenberg_REST_Navigation_Controller extends WP_REST_Posts_Controller {

/**
* Registers the controllers routes.
*
* @return void
*/
public function register_routes() {

parent::register_routes();

// Lists a single nav item based on the given id or slug.
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/fallbacks',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_fallbacks' ),
'permission_callback' => array( $this, 'get_fallbacks_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::READABLE ),
),
'allow_batch' => $this->allow_batch,
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}

public function get_fallbacks_permissions_check( $request ) {
return true;
}

public function get_fallbacks() {
return $this->block_core_navigation_create_fallback();
}

private function block_core_navigation_create_fallback() {
$should_skip = apply_filters( 'block_core_navigation_skip_fallback', false );
if ( $should_skip ) {
return;
}

// Get the most recently published Navigation post.
$navigation_post = block_core_navigation_get_most_recently_published_navigation();

// If there are no navigation posts then try to find a classic menu
// and convert it into a block based navigation menu.
if ( ! $navigation_post ) {
$navigation_post = block_core_navigation_maybe_use_classic_menu_fallback();
}

// If there are no navigation posts then default to a list of Pages.
if ( ! $navigation_post ) {
$navigation_post = block_core_navigation_get_default_fallback();
}

return $navigation_post;
}






}
10 changes: 10 additions & 0 deletions lib/experimental/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,13 @@ function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $na
return $permalink;
}
add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 );

function gutenberg_update_navigation_rest_controller( $args, $post_type ) {
if ( in_array( $post_type, array( 'wp_navigation' ), true ) ) {
// Original set in
// https://github.com/WordPress/wordpress-develop/blob/6cbed78c94b9d8c6a9b4c8b472b88ee0cd56528c/src/wp-includes/post.php#L528.
$args['rest_controller_class'] = 'Gutenberg_REST_Navigation_Controller';
}
return $args;
}
add_filter( 'register_post_type_args', 'gutenberg_update_navigation_rest_controller', 10, 2 );
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require_once __DIR__ . '/experimental/class-wp-rest-customizer-nonces.php';
}

require_once __DIR__ . '/experimental/class-wp-rest-navigation-controller.php';
require_once __DIR__ . '/experimental/rest-api.php';
}

Expand Down

0 comments on commit 56e702c

Please sign in to comment.