-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[Try] menuSlug instead of navigationMenuId #36522
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
16cebf9
Remove remnants of nav area
adamziel 90ea05c
Assign a slug to the navigation block
adamziel ac9ada4
Sets a custom slug when creating auto-draft navigation
adamziel a1a2792
Rudimentary slug saving mechanics
adamziel 39f5ae0
Create nav menu when it's missing
adamziel 6e8aecd
Somewhat functional entity creation
adamziel ac8283e
Rename menuSlug to slug
adamziel f0c6c68
Minimal failing test
adamziel 779dec1
REST API endpoint that passes tests
adamziel 2233b87
Minimal functioning endpoint
adamziel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
274 changes: 274 additions & 0 deletions
274
lib/compat/wordpress-5.9/class-gutenberg-rest-navigation-controller.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,274 @@ | ||
<?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 { | ||
/** | ||
* Post type. | ||
* | ||
* @var string | ||
*/ | ||
protected $post_type; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $post_type Post type. | ||
*/ | ||
public function __construct( $post_type ) { | ||
$this->post_type = $post_type; | ||
$this->namespace = 'wp/v2'; | ||
$obj = get_post_type_object( $post_type ); | ||
$this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name; | ||
} | ||
|
||
/** | ||
* Registers the controllers routes. | ||
* | ||
* @return void | ||
*/ | ||
public function register_routes() { | ||
// Lists all templates. | ||
register_rest_route( | ||
$this->namespace, | ||
'/' . $this->rest_base, | ||
array( | ||
array( | ||
'methods' => WP_REST_Server::READABLE, | ||
'callback' => array( $this, 'get_items' ), | ||
'permission_callback' => array( $this, 'get_items_permissions_check' ), | ||
'args' => $this->get_collection_params(), | ||
), | ||
array( | ||
'methods' => WP_REST_Server::CREATABLE, | ||
'callback' => array( $this, 'create_item' ), | ||
'permission_callback' => array( $this, 'create_item_permissions_check' ), | ||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), | ||
), | ||
'schema' => array( $this, 'get_public_item_schema' ), | ||
) | ||
); | ||
|
||
// Lists/updates 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 id of a navigation', 'gutenberg' ), | ||
'type' => 'string', | ||
), | ||
), | ||
), | ||
array( | ||
'methods' => WP_REST_Server::EDITABLE, | ||
'callback' => array( $this, 'update_item' ), | ||
'permission_callback' => array( $this, 'update_item_permissions_check' ), | ||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), | ||
), | ||
array( | ||
'methods' => WP_REST_Server::DELETABLE, | ||
'callback' => array( $this, 'delete_item' ), | ||
'permission_callback' => array( $this, 'delete_item_permissions_check' ), | ||
'args' => array( | ||
'force' => array( | ||
'type' => 'boolean', | ||
'default' => false, | ||
'description' => __( 'Whether to bypass Trash and force deletion.', 'gutenberg' ), | ||
), | ||
), | ||
), | ||
'schema' => array( $this, 'get_public_item_schema' ), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Checks if the user has permissions to make the request. | ||
* | ||
* @return true|WP_Error True if the request has read access, WP_Error object otherwise. | ||
*/ | ||
protected function permissions_check() { | ||
// Verify if the current user has edit_theme_options capability. | ||
// This capability is required to edit/view/delete templates. | ||
// if ( ! current_user_can( 'edit_theme_options' ) ) { | ||
// return new WP_Error( | ||
// 'rest_cannot_manage_templates', | ||
// __( 'Sorry, you are not allowed to access the templates on this site.', 'gutenberg' ), | ||
// array( | ||
// 'status' => rest_authorization_required_code(), | ||
// ) | ||
// ); | ||
// } | ||
|
||
return true; | ||
} | ||
|
||
public function get_item_permissions_check( $request ) { | ||
return true; | ||
} | ||
|
||
public function create_item_permissions_check( $request ) { | ||
return true; | ||
} | ||
|
||
public function update_item_permissions_check( $request ) { | ||
return true; | ||
} | ||
|
||
public function delete_item_permissions_check( $request ) { | ||
return true; | ||
} | ||
|
||
protected function check_update_permission( $post ) { | ||
return true; | ||
} | ||
|
||
public function get_items_permissions_check( $request ) { | ||
return true; | ||
} | ||
|
||
public function check_read_permission( $post ) { | ||
return true; | ||
} | ||
|
||
protected function check_create_permission( $post ) { | ||
return true; | ||
} | ||
|
||
protected function check_delete_permission( $post ) { | ||
return true; | ||
} | ||
|
||
protected function handle_status_param( $post_status, $post_type ) { | ||
return $post_status; | ||
} | ||
|
||
public function prepare_item_for_response( $item, $request ) { | ||
unset( $request['id'] ); | ||
$item = parent::prepare_item_for_response( $item, $request ); // TODO: Change the autogenerated stub | ||
|
||
return $item; | ||
} | ||
// | ||
// public function update_additional_fields_for_object( $post, $request ) { | ||
// echo "update_additional_fields_for_object"; | ||
// print_r(get_post($post->ID)); | ||
// die(print_r($post)); | ||
// } | ||
|
||
public function filter_response_by_context( $data, $context ) { | ||
$data['id'] = $data['slug']; | ||
|
||
return $data; | ||
} | ||
|
||
public function get_item_schema() { | ||
$schema = parent::get_item_schema(); // TODO: Change the autogenerated stub | ||
$schema['properties']['id']['type'] = 'string'; | ||
|
||
return $schema; | ||
} | ||
|
||
|
||
protected function get_post( $id ) { | ||
$wp_query_args = array( | ||
'name' => $id, | ||
'post_type' => 'wp_navigation', | ||
'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ), | ||
'posts_per_page' => 1, | ||
'no_found_rows' => true, | ||
); | ||
$template_query = new WP_Query( $wp_query_args ); | ||
$post = $template_query->posts[0]; | ||
// print_r(get_post($post->ID)); | ||
// die(print_r($template_query->posts)); | ||
if ( ! $post ) { | ||
return new WP_Error( | ||
'rest_post_invalid_id', | ||
__( 'Invalid post ID.' ), | ||
array( 'status' => 404 ) | ||
); | ||
} | ||
|
||
return $post; | ||
} | ||
|
||
public function create_item( $request ) { | ||
return $this->update_item( $request ); | ||
} | ||
|
||
public function update_item( $request ) { | ||
$navigation = $this->get_post( $request['id'] ); | ||
if ( ! $navigation ) { | ||
return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) ); | ||
} | ||
|
||
$changes = $this->prepare_item_for_database( $request ); | ||
|
||
if ( ! is_wp_error( $navigation ) ) { | ||
$result = wp_update_post( wp_slash( (array) $changes ), true ); | ||
} else { | ||
$result = wp_insert_post( wp_slash( (array) $changes ), true ); | ||
} | ||
if ( is_wp_error( $result ) ) { | ||
return $result; | ||
} | ||
|
||
$navigation = $this->get_post( $request['id'] ); | ||
// $fields_update = $this->update_additional_fields_for_object( $navigation, $request ); | ||
// if ( is_wp_error( $fields_update ) ) { | ||
// return $fields_update; | ||
// } | ||
|
||
return $this->prepare_item_for_response( | ||
$navigation, | ||
$request | ||
); | ||
} | ||
|
||
protected function prepare_item_for_database( $request ) { | ||
$navigation = $this->get_post( $request['id'] ); | ||
$navigation = is_wp_error( $navigation ) ? null : $navigation; | ||
$changes = new stdClass(); | ||
if ( null === $navigation ) { | ||
$changes->post_type = $this->post_type; | ||
$changes->post_status = 'publish'; | ||
$changes->post_name = $request['id']; | ||
} else { | ||
$changes->post_name = $navigation->post_name; | ||
$changes->ID = $navigation->ID; | ||
$changes->post_status = 'publish'; | ||
} | ||
if ( isset( $request['content'] ) ) { | ||
$changes->post_content = $request['content']; | ||
} elseif ( null !== $navigation && 'custom' !== $navigation->source ) { | ||
$changes->post_content = $navigation->content; | ||
} | ||
if ( isset( $request['title'] ) ) { | ||
$changes->post_title = $request['title']; | ||
} elseif ( null !== $navigation && 'custom' !== $navigation->source ) { | ||
$changes->post_title = $navigation->title; | ||
} | ||
if ( isset( $request['description'] ) ) { | ||
$changes->post_excerpt = $request['description']; | ||
} elseif ( null !== $navigation && 'custom' !== $navigation->source ) { | ||
$changes->post_excerpt = $navigation->description; | ||
} | ||
|
||
return $changes; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,10 @@ | |
"textdomain": "default", | ||
"attributes": { | ||
"navigationMenuId": { | ||
"type": "number" | ||
"type": "number" | ||
}, | ||
Comment on lines
11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be removed of course |
||
"slug": { | ||
"type": "string" | ||
}, | ||
"textColor": { | ||
"type": "string" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is quick&dirty attempt to reproduce template areas mechanics of requesting items by their slug, e.g.
/wp/v2/navigation/header-menu
. A lot of cleaning up needed here.