Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Use post_name for query instead of ID #42778

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* REST API: Gutenberg_REST_Templates_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.
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\/\w-]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'id' => array(
'description' => __( 'The slug identifier for a avigation', 'gutenberg' ),
'type' => 'string',
),
),
),
)
);
}


/**
* Overide WP_REST_Posts_Controller function to query for
* `wp_navigation` posts by post_name / slug instead of ID.
*
* @param string $id the slug of the Navigation post.
* @return WP_Post|null
*/
protected function get_post( $id ) {

$error = new WP_Error(
'rest_post_invalid_id',
__( 'Invalid post ID.' ),
array( 'status' => 404 )
);

if ( ! is_string( $id ) ) {
return $error;
}

$args = array(
'name' => $id, // query by slug
'post_type' => array( $this->post_type ),
'nopaging' => true,
'posts_per_page' => '-1',
);

// The Query
$post = new WP_Query( $args );

if ( empty( $post ) || empty( $post->post->ID ) || $this->post_type !== $post->post->post_type ) {
return $error;
}

return $post->post;
}



}
14 changes: 14 additions & 0 deletions lib/compat/wordpress-6.1/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,17 @@ function gutenberg_register_gutenberg_rest_block_patterns() {
$block_patterns->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_gutenberg_rest_block_patterns', 100 );



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 @@ -47,6 +47,7 @@ function gutenberg_is_experiment_enabled( $name ) {
// WordPress 6.1 compat.
require_once __DIR__ . '/compat/wordpress-6.1/class-gutenberg-rest-block-patterns-controller.php';
require_once __DIR__ . '/compat/wordpress-6.1/class-gutenberg-rest-templates-controller.php';
require_once __DIR__ . '/compat/wordpress-6.1/class-gutenberg-rest-navigation-controller.php';
require_once __DIR__ . '/compat/wordpress-6.1/rest-api.php';

// Experimental.
Expand Down