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

[Try] menuSlug instead of navigationMenuId #36522

Closed
wants to merge 10 commits into from
@@ -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 {
Copy link
Contributor Author

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.

/**
* 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;
}

}
1 change: 1 addition & 0 deletions lib/load.php
Expand Up @@ -57,6 +57,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require_once __DIR__ . '/class-wp-rest-customizer-nonces.php';
}
require_once __DIR__ . '/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php';
require_once __DIR__ . '/compat/wordpress-5.9/class-gutenberg-rest-navigation-controller.php';
if ( ! class_exists( 'WP_REST_Block_Editor_Settings_Controller' ) ) {
require_once dirname( __FILE__ ) . '/class-wp-rest-block-editor-settings-controller.php';
}
Expand Down
34 changes: 33 additions & 1 deletion lib/navigation.php
Expand Up @@ -44,7 +44,7 @@ function gutenberg_register_navigation_post_type() {
'show_in_rest' => true,
'map_meta_cap' => true,
'rest_base' => 'navigation',
'rest_controller_class' => WP_REST_Posts_Controller::class,
'rest_controller_class' => Gutenberg_REST_Navigation_Controller::class,
'supports' => array(
'title',
'editor',
Expand Down Expand Up @@ -291,3 +291,35 @@ function gutenberg_hide_visibility_and_status_for_navigation_posts( $hook ) {
}

add_action( 'admin_enqueue_scripts', 'gutenberg_hide_visibility_and_status_for_navigation_posts' );


/**
* Sets a custom slug when creating auto-draft navigation.
* This is only needed for auto-drafts created by the regular WP editor.
* If this page is to be removed, this won't be necessary.
*
* @param int $post_id Post ID.
*/
function gutenberg_set_unique_slug_on_create_navigation_post( $post_id ) {
// This is the core function with the same functionality.
if ( function_exists( 'set_unique_slug_on_create_navigation_post' ) ) {
return;
}

$post = get_post( $post_id );
if ( 'auto-draft' !== $post->post_status ) {
return;
}

if ( ! $post->post_name ) {
wp_update_post(
array(
'ID' => $post_id,
'post_name' => 'general-menu//' . uniqid(),
)
);
}
}

//add_action( 'save_post_wp_navigation', 'gutenberg_set_unique_slug_on_create_navigation_post' );

3 changes: 2 additions & 1 deletion packages/block-library/package.json
Expand Up @@ -68,7 +68,8 @@
"lodash": "^4.17.21",
"memize": "^1.1.0",
"micromodal": "^0.4.6",
"moment": "^2.22.1"
"moment": "^2.22.1",
"uuid": "^8.3.0"
},
"publishConfig": {
"access": "public"
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/navigation/block.json
Expand Up @@ -9,7 +9,10 @@
"textdomain": "default",
"attributes": {
"navigationMenuId": {
"type": "number"
"type": "number"
},
Comment on lines 11 to +13
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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"
Expand Down