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

Allow for custom revision / autosave endpoints classes. #3533

Closed
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
800d074
Allow for custom revision / autosave endpoints classes.
spacedmonkey Oct 27, 2022
e6601cb
Merge branch 'trunk' into fix/custom-endpoints-revisions
spacedmonkey Sep 25, 2023
13789c1
Improvements.
spacedmonkey Sep 25, 2023
c8f0cc9
Fix endpoints.
spacedmonkey Sep 26, 2023
987d796
Fix lints
spacedmonkey Sep 26, 2023
97d9faf
Better logic.
spacedmonkey Sep 26, 2023
dff4f3c
Apply suggestions from code review
spacedmonkey Sep 26, 2023
b69f359
Add links.
spacedmonkey Sep 28, 2023
c31b9e2
Fix links.
spacedmonkey Sep 28, 2023
572924b
Tweaks.
spacedmonkey Sep 28, 2023
99166d0
Tweaks again.
spacedmonkey Sep 28, 2023
86fbb30
Tweaks again.
spacedmonkey Sep 28, 2023
fe3640f
Add comment.
spacedmonkey Sep 28, 2023
e31904c
Update schema
spacedmonkey Sep 28, 2023
a92f8ec
Cherry pick the test from https://github.com/WordPress/wordpress-deve…
anton-vlasenko Sep 29, 2023
f80a74f
Mark test for CREATE and UPDATE operations as skipped.
anton-vlasenko Sep 29, 2023
649145a
Fix autosave endpoint.
spacedmonkey Sep 29, 2023
46cabc9
Implement a unit test for deleting a single template revision.
anton-vlasenko Sep 29, 2023
7d85c9b
Repeated code.
spacedmonkey Sep 29, 2023
88d753c
Implement the Tests_REST_wpRestTemplateRevisionsController::test_prep…
anton-vlasenko Sep 29, 2023
d768cfb
Implement the Tests_REST_wpRestTemplateRevisionsController::test_get_…
anton-vlasenko Sep 29, 2023
554cb7c
Add dots to the failure messages.
anton-vlasenko Sep 29, 2023
05c9cb3
Fix CS error.
anton-vlasenko Sep 29, 2023
db92e91
Merge branch 'trunk' into fix/custom-endpoints-revisions
spacedmonkey Sep 29, 2023
9dc722d
Fix unit tests.
spacedmonkey Sep 29, 2023
844ab3d
Fix doc block.
anton-vlasenko Oct 1, 2023
10e93c7
Commit draft for the Tests_REST_wpRestTemplateAutosavesController test.
anton-vlasenko Oct 1, 2023
6ebfcd3
Remove old code.
anton-vlasenko Oct 1, 2023
0d668b9
Fix the Tests_REST_wpRestTemplateAutosavesController::test_register_r…
anton-vlasenko Oct 1, 2023
3bb7921
Fix the Tests_REST_wpRestTemplateAutosavesController::test_context_pa…
anton-vlasenko Oct 1, 2023
3808893
Fix @covers tags.
anton-vlasenko Oct 1, 2023
6e88efb
Override the parent `WP_REST_Template_Autosaves_Controller::get_item`…
anton-vlasenko Oct 1, 2023
e01a9ff
Fix test_get_items(), test_get_item(), and test_delete_item() test me…
anton-vlasenko Oct 1, 2023
725a4e5
Implement the Tests_REST_wpRestTemplateAutosavesController::test_prep…
anton-vlasenko Oct 1, 2023
148140c
Implement the Tests_REST_wpRestTemplateAutosavesController::test_crea…
anton-vlasenko Oct 1, 2023
a145a29
1. Fix typo in the class name.
anton-vlasenko Oct 1, 2023
8b778a7
1. Add a unit test for new post_type properties.
anton-vlasenko Oct 1, 2023
e81bdc0
1. Avoid using the get_param() method to retrieve request parameters.
anton-vlasenko Oct 2, 2023
be6f016
Merge branch 'trunk' into fix/custom-endpoints-revisions
spacedmonkey Oct 2, 2023
e77e26f
Address code review feedback: https://github.com/WordPress/wordpress-…
anton-vlasenko Oct 2, 2023
552afa4
Implement PHPUnit tests for WP_Post_Type::get_revisions_rest_controll…
anton-vlasenko Oct 2, 2023
4ca8687
Improve unit tests.
spacedmonkey Oct 3, 2023
4c3cff0
Implement the test_should_not_return_items_with_no_permission() test …
anton-vlasenko Oct 3, 2023
e92f988
Improve unit tests.
spacedmonkey Oct 3, 2023
f5b8532
Add in since.
spacedmonkey Oct 3, 2023
c02dc01
Skip the test method for now.
anton-vlasenko Oct 3, 2023
5c52aaf
Update the wp-api-generated.js schema.
anton-vlasenko Oct 3, 2023
1e79796
Fix CS.
anton-vlasenko Oct 3, 2023
0488d6b
Refactor the test_should_not_return_items_with_no_permission() method…
anton-vlasenko Oct 3, 2023
e202dd7
Remove unused property.
anton-vlasenko Oct 3, 2023
7d11acc
Fix class property type in the corresponding doc block.
anton-vlasenko Oct 3, 2023
e852cc6
Refactor the get_parent() test method.
anton-vlasenko Oct 3, 2023
d7ad527
Try something.
spacedmonkey Oct 3, 2023
e7cbf5a
Merge branch 'trunk' into fix/custom-endpoints-revisions
spacedmonkey Oct 3, 2023
49abecd
Debug.
spacedmonkey Oct 3, 2023
089d0f0
Use _wp_put_post_revision.
spacedmonkey Oct 3, 2023
fa8032e
Remove var dump.
spacedmonkey Oct 3, 2023
881feb7
Use _wp_put_post_revision.
spacedmonkey Oct 3, 2023
ee2d827
Revert _wp_put_post_revision.
spacedmonkey Oct 3, 2023
9816b0a
Try something else.
spacedmonkey Oct 3, 2023
e607e71
Workaround.
spacedmonkey Oct 4, 2023
68bef5d
Fix lint.
spacedmonkey Oct 4, 2023
6f7a342
Remove unused class property.
anton-vlasenko Oct 4, 2023
51c897f
Apply suggestions from code review
spacedmonkey Oct 4, 2023
bbd150b
Add assertions for template part routes.
anton-vlasenko Oct 4, 2023
5d0b10a
Add assertions for template parts autosave routes.
anton-vlasenko Oct 4, 2023
534bf91
Merge branch 'trunk' into fix/custom-endpoints-revisions
spacedmonkey Oct 5, 2023
5bc70e9
Improve tests and documentation.
spacedmonkey Oct 5, 2023
fd7821f
Fix tests.
spacedmonkey Oct 5, 2023
bae2535
Apply suggestions from code review
spacedmonkey Oct 5, 2023
35f5bdf
Update fixture file again.
spacedmonkey Oct 5, 2023
73a515a
Update fixture file again again.
spacedmonkey Oct 5, 2023
50fcdec
Add new parameter of post type. Update docs.
spacedmonkey Oct 5, 2023
85f0c28
Merge branch 'trunk' into fix/custom-endpoints-revisions
spacedmonkey Oct 8, 2023
1f4c263
Feedback.
spacedmonkey Oct 9, 2023
f0cfb31
Update some docs.
spacedmonkey Oct 9, 2023
dc75bed
Update some docs again.
spacedmonkey Oct 9, 2023
49c0dc6
Apply suggestions from code review
spacedmonkey Oct 9, 2023
24f50b2
Apply suggestions from code review
spacedmonkey Oct 9, 2023
1368354
Add missing message.
spacedmonkey Oct 9, 2023
2401506
Update src/wp-includes/class-wp-post-type.php
spacedmonkey Oct 9, 2023
8d89067
Update src/wp-includes/rest-api/endpoints/class-wp-rest-templates-con…
spacedmonkey Oct 9, 2023
0b67eaf
Update tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController…
spacedmonkey Oct 9, 2023
4e3151e
Update tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController…
spacedmonkey Oct 9, 2023
c01c0fc
Update tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController…
spacedmonkey Oct 9, 2023
7fa0627
Apply suggestions from code review
spacedmonkey Oct 10, 2023
300d102
Apply suggestions from code review
spacedmonkey Oct 10, 2023
4e6d9c3
Merge branch 'trunk' into fix/custom-endpoints-revisions
spacedmonkey Oct 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 150 additions & 31 deletions src/wp-includes/class-wp-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,47 @@ final class WP_Post_Type {
*/
public $rest_controller;

/**
* The controller for this post type's REST API endpoints.
*
* Custom controllers must extend WP_REST_Controller.
*
* @since 6.4.0
* @var string|bool $rest_controller_class
*/
public $revisions_rest_controller_class;

/**
* The controller instance for this post type's REST API endpoints.
*
* Lazily computed. Should be accessed using {@see WP_Post_Type::get_rest_controller()}.
*
* @since 6.4.0
* @var WP_REST_Controller $rest_controller
*/
public $revisions_rest_controller;


spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
/**
* The controller for this post type's REST API endpoints.
*
* Custom controllers must extend WP_REST_Controller.
*
* @since 6.4.0
* @var string|bool $rest_controller_class
*/
public $autosave_rest_controller_class;

/**
* The controller instance for this post type's REST API endpoints.
*
* Lazily computed. Should be accessed using {@see WP_Post_Type::get_rest_controller()}.
*
* @since 6.4.0
* @var WP_REST_Controller $rest_controller
*/
public $autosave_rest_controller;

/**
* Constructor.
*
Expand Down Expand Up @@ -466,37 +507,39 @@ public function set_props( $args ) {

// Args prefixed with an underscore are reserved for internal use.
$defaults = array(
'labels' => array(),
'description' => '',
'public' => false,
'hierarchical' => false,
'exclude_from_search' => null,
'publicly_queryable' => null,
'show_ui' => null,
'show_in_menu' => null,
'show_in_nav_menus' => null,
'show_in_admin_bar' => null,
'menu_position' => null,
'menu_icon' => null,
'capability_type' => 'post',
'capabilities' => array(),
'map_meta_cap' => null,
'supports' => array(),
'register_meta_box_cb' => null,
'taxonomies' => array(),
'has_archive' => false,
'rewrite' => true,
'query_var' => true,
'can_export' => true,
'delete_with_user' => null,
'show_in_rest' => false,
'rest_base' => false,
'rest_namespace' => false,
'rest_controller_class' => false,
'template' => array(),
'template_lock' => false,
'_builtin' => false,
'_edit_link' => 'post.php?post=%d',
'labels' => array(),
'description' => '',
'public' => false,
'hierarchical' => false,
'exclude_from_search' => null,
'publicly_queryable' => null,
'show_ui' => null,
'show_in_menu' => null,
'show_in_nav_menus' => null,
'show_in_admin_bar' => null,
'menu_position' => null,
'menu_icon' => null,
'capability_type' => 'post',
'capabilities' => array(),
'map_meta_cap' => null,
'supports' => array(),
'register_meta_box_cb' => null,
'taxonomies' => array(),
'has_archive' => false,
'rewrite' => true,
'query_var' => true,
'can_export' => true,
'delete_with_user' => null,
'show_in_rest' => false,
'rest_base' => false,
'rest_namespace' => false,
'rest_controller_class' => false,
'autosave_rest_controller_class' => false,
'revisions_rest_controller_class' => false,
'template' => array(),
'template_lock' => false,
'_builtin' => false,
'_edit_link' => 'post.php?post=%d',
);

$args = array_merge( $defaults, $args );
Expand Down Expand Up @@ -816,6 +859,82 @@ public function get_rest_controller() {
return $this->rest_controller;
}

/**
* Gets the REST API revisions controller for this post type.
*
* Will only instantiate the controller class once per request.
*
* @since 6.2.0
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
*
* @return WP_REST_Controller|null The controller instance, or null if the post type
* is set not to show in rest.
*/
public function get_revisions_rest_controller() {

spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
if ( ! $this->show_in_rest ) {
return null;
}

if ( ! post_type_supports( $this->name, 'revisions' ) ) {
return null;
}

$class = $this->revisions_rest_controller_class ? $this->revisions_rest_controller_class : WP_REST_Revisions_Controller::class;
if ( ! class_exists( $class ) ) {
return null;
}

if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
return null;
}

if ( ! $this->revisions_rest_controller ) {
$this->revisions_rest_controller = new $class( $this->name );
}

if ( ! ( $this->revisions_rest_controller instanceof $class ) ) {
return null;
}

return $this->revisions_rest_controller;
}

/**
* Gets the REST API autosave controller for this post type.
*
* Will only instantiate the controller class once per request.
*
* @since 6.2.0
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
*
* @return WP_REST_Controller|null The controller instance, or null if the post type
* is set not to show in rest.
*/
public function get_autosave_rest_controller() {
if ( ! $this->show_in_rest ) {
return null;
}

$class = $this->autosave_rest_controller_class ? $this->autosave_rest_controller_class : WP_REST_Autosaves_Controller::class;

if ( ! class_exists( $class ) ) {
return null;
}

if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
return null;
}

if ( ! $this->autosave_rest_controller ) {
$this->autosave_rest_controller = new $class( $this->name );
}

if ( ! ( $this->autosave_rest_controller instanceof $class ) ) {
return null;
}

return $this->autosave_rest_controller;
}

/**
* Returns the default labels for post types.
*
Expand Down
66 changes: 35 additions & 31 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ function create_initial_post_types() {
register_post_type(
'wp_template',
array(
'labels' => array(
'labels' => array(
'name' => _x( 'Templates', 'post type general name' ),
'singular_name' => _x( 'Template', 'post type singular name' ),
'add_new' => __( 'Add New Template' ),
Expand All @@ -366,19 +366,21 @@ function create_initial_post_types() {
'items_list_navigation' => __( 'Templates list navigation' ),
'items_list' => __( 'Templates list' ),
),
'description' => __( 'Templates to include in your theme.' ),
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => $template_edit_link, /* internal use only. don't use this when registering your own post type. */
'has_archive' => false,
'show_ui' => false,
'show_in_menu' => false,
'show_in_rest' => true,
'rewrite' => false,
'rest_base' => 'templates',
'rest_controller_class' => 'WP_REST_Templates_Controller',
'capability_type' => array( 'template', 'templates' ),
'capabilities' => array(
'description' => __( 'Templates to include in your theme.' ),
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => $template_edit_link, /* internal use only. don't use this when registering your own post type. */
'has_archive' => false,
'show_ui' => false,
'show_in_menu' => false,
'show_in_rest' => true,
'rewrite' => false,
'rest_base' => 'templates',
'rest_controller_class' => 'WP_REST_Templates_Controller',
'autosave_rest_controller_class' => 'WP_REST_Template_Autosaves_Controller',
'revisions_rest_controller_class' => 'WP_REST_Template_Revisions_Controller',
'capability_type' => array( 'template', 'templates' ),
'capabilities' => array(
'create_posts' => 'edit_theme_options',
'delete_posts' => 'edit_theme_options',
'delete_others_posts' => 'edit_theme_options',
Expand All @@ -392,8 +394,8 @@ function create_initial_post_types() {
'read' => 'edit_theme_options',
'read_private_posts' => 'edit_theme_options',
),
'map_meta_cap' => true,
'supports' => array(
'map_meta_cap' => true,
'supports' => array(
'title',
'slug',
'excerpt',
Expand All @@ -407,7 +409,7 @@ function create_initial_post_types() {
register_post_type(
'wp_template_part',
array(
'labels' => array(
'labels' => array(
'name' => _x( 'Template Parts', 'post type general name' ),
'singular_name' => _x( 'Template Part', 'post type singular name' ),
'add_new' => __( 'Add New Template Part' ),
Expand All @@ -427,19 +429,21 @@ function create_initial_post_types() {
'items_list_navigation' => __( 'Template parts list navigation' ),
'items_list' => __( 'Template parts list' ),
),
'description' => __( 'Template parts to include in your templates.' ),
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => $template_edit_link, /* internal use only. don't use this when registering your own post type. */
'has_archive' => false,
'show_ui' => false,
'show_in_menu' => false,
'show_in_rest' => true,
'rewrite' => false,
'rest_base' => 'template-parts',
'rest_controller_class' => 'WP_REST_Templates_Controller',
'map_meta_cap' => true,
'capabilities' => array(
'description' => __( 'Template parts to include in your templates.' ),
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => $template_edit_link, /* internal use only. don't use this when registering your own post type. */
'has_archive' => false,
'show_ui' => false,
'show_in_menu' => false,
'show_in_rest' => true,
'rewrite' => false,
'rest_base' => 'template-parts',
'rest_controller_class' => 'WP_REST_Templates_Controller',
'autosave_rest_controller_class' => 'WP_REST_Template_Autosaves_Controller',
'revisions_rest_controller_class' => 'WP_REST_Template_Revisions_Controller',
'map_meta_cap' => true,
'capabilities' => array(
'create_posts' => 'edit_theme_options',
'delete_posts' => 'edit_theme_options',
'delete_others_posts' => 'edit_theme_options',
Expand All @@ -453,7 +457,7 @@ function create_initial_post_types() {
'read' => 'edit_theme_options',
'read_private_posts' => 'edit_theme_options',
),
'supports' => array(
'supports' => array(
'title',
'slug',
'excerpt',
Expand Down
18 changes: 13 additions & 5 deletions src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,25 @@ function create_initial_rest_routes() {
continue;
}

$controller->register_routes();
$late_route_registration = 'WP_REST_Template_Autosaves_Controller' === $post_type->autosave_rest_controller_class || 'WP_REST_Template_Revisions_Controller' === $post_type->revisions_rest_controller_class;

if ( ! $late_route_registration ) {
$controller->register_routes();
}

if ( post_type_supports( $post_type->name, 'revisions' ) ) {
$revisions_controller = new WP_REST_Revisions_Controller( $post_type->name );
$revisions_controller = $post_type->get_revisions_rest_controller();
if ( $revisions_controller ) {
$revisions_controller->register_routes();
}

if ( 'attachment' !== $post_type->name ) {
$autosaves_controller = new WP_REST_Autosaves_Controller( $post_type->name );
$autosaves_controller = $post_type->get_autosave_rest_controller();
if ( $autosaves_controller ) {
$autosaves_controller->register_routes();
}

if ( $late_route_registration ) {
$controller->register_routes();
}
}

// Post types.
Expand Down
Loading