Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
69ed6a2
WIP Playwright migration
swissspidy Sep 27, 2023
d76a2c3
Convert visual regression tests
swissspidy Sep 27, 2023
f1f7ce1
Fix mask config
swissspidy Sep 27, 2023
792c758
Initial conversion of edit posts tests
swissspidy Sep 27, 2023
8fb0560
Fix file ext
swissspidy Sep 27, 2023
c7b7ce6
Convert dashboard test
swissspidy Sep 27, 2023
a88e396
Convert another test
swissspidy Sep 27, 2023
c91d0ca
Improve test
swissspidy Sep 27, 2023
0778ffe
Remove types package again
swissspidy Sep 28, 2023
cb04187
Merge branch 'trunk' into add/pw-performance-tests
swissspidy Sep 28, 2023
e691f09
Remove debug cruft
swissspidy Sep 28, 2023
ca36ecb
Install browsers separately
swissspidy Sep 28, 2023
b044cff
Fix webServer command
swissspidy Sep 28, 2023
2e17562
Merge branch 'trunk' into add/pw-performance-tests
swissspidy Sep 28, 2023
85021bb
Some more bug fixes
swissspidy Sep 28, 2023
f6fd1ca
Update dashboard tests
Mamaduka Sep 28, 2023
fda76fc
Update edit post tests
Mamaduka Sep 28, 2023
5afa823
Use keyPress util
Mamaduka Sep 28, 2023
9ae926f
Update empty and restore trash tests
Mamaduka Sep 28, 2023
21e9f12
Try using same PW version as Gutenberg and wp-scripts
Mamaduka Sep 28, 2023
5ffb5d7
Install deps again after git checkout
swissspidy Sep 28, 2023
5ca807c
Remove commented out config
swissspidy Sep 28, 2023
1cd3f2f
Fix test title
swissspidy Sep 28, 2023
fb49007
Use env var for prefix instead of argument
swissspidy Sep 28, 2023
6f3a89f
Fix app passwords tests
swissspidy Sep 28, 2023
53691a1
Debug: use different reporter on CI
swissspidy Sep 28, 2023
f488294
Update app password tests
Mamaduka Sep 28, 2023
d401663
Remove leftovers
swissspidy Sep 28, 2023
2109765
Move order
swissspidy Sep 28, 2023
c346d2b
Make Gutenberg test more robust
swissspidy Sep 28, 2023
1079d5c
Make comparison script more robust
swissspidy Sep 28, 2023
14d7496
Further improve comparison script
swissspidy Sep 28, 2023
cb8e4d6
Undo reporter change
swissspidy Sep 28, 2023
af97340
Merge branch 'trunk' into add/pw-performance-tests
swissspidy Sep 29, 2023
356d967
Merge branch 'trunk' into add/pw-performance-tests
swissspidy Oct 2, 2023
a3048ac
Remove `PUPPETEER_SKIP_DOWNLOAD`
swissspidy Oct 2, 2023
4141e47
Update readmes
swissspidy Oct 2, 2023
737b58c
Make Gutenberg plugin tests more resilient
Mamaduka Oct 2, 2023
8f8a971
Merge branch 'trunk' into add/pw-performance-tests
swissspidy Oct 2, 2023
0e8bd0c
Merge branch 'trunk' into add/pw-performance-tests
swissspidy Oct 5, 2023
dd3000a
Merge branch 'trunk' into add/pw-performance-tests
swissspidy Oct 10, 2023
fb78280
Extend base PW config from wp-scripts
swissspidy Oct 10, 2023
bc35c5e
Fix config
swissspidy Oct 10, 2023
8d64ece
Merge branch 'trunk' into add/pw-performance-tests
swissspidy Oct 10, 2023
827d43d
Fix indentation
swissspidy Oct 10, 2023
9f07be2
use `require.resolve`
swissspidy Oct 10, 2023
53b5166
Fix double comment
swissspidy Oct 12, 2023
a027cae
Options, Meta APIs: Check setting group exists before search in unreg…
hellofromtonya Oct 10, 2023
593894a
Patterns: Inject `theme` attribute into Template Part blocks.
ockham Oct 10, 2023
050997c
REST API: Fix issue with Template and Template Part Revision/Autosave…
spacedmonkey Oct 10, 2023
fc0e71a
Update/Install: Deactivate Gutenberg plugin version older than 16.5.
hellofromtonya Oct 10, 2023
6e632c1
WordPress 6.4 Beta 3.
karmatosed Oct 10, 2023
8e3ae9a
Post WordPress 6.4 Beta 3 version bump.
karmatosed Oct 10, 2023
8544fe3
Build/Test Tools: Correct variable typo.
desrosj Oct 10, 2023
03bdce8
Plugins: Fix broken `sprintf()` call in plugins list table.
costdev Oct 11, 2023
f2d285e
Docs: Use US spelling and correct a typing mistake.
costdev Oct 11, 2023
994d2ab
Help/About: Ensure that focus outline on the Credits screen is not cu…
SergeyBiryukov Oct 11, 2023
889f1a7
Build/Test Tools: Don’t send Slack notification for workflow retries.
desrosj Oct 11, 2023
c2313f9
Editor: Add further test coverage for `wp_render_elements_support()`.
felixarntz Oct 11, 2023
207cdb9
Build/Test Tools: Increase the number of retries when restarting a wo…
desrosj Oct 11, 2023
46d4273
Build/Test Tools: Increase the timeout for the failed workflow.
desrosj Oct 11, 2023
27be854
Build/Test Tools: Remove hardcoded PHPUnit config files.
desrosj Oct 11, 2023
4172c12
Twenty Nineteen: Add margins to editor iframe content.
SergeyBiryukov Oct 12, 2023
15049d6
Mention UI mode in readme
swissspidy Oct 12, 2023
96f591d
Merge branch 'trunk' into add/pw-performance-tests
swissspidy Oct 12, 2023
eba1afa
Revert r56198
swissspidy Oct 12, 2023
aa9fb1c
Apply r56660
swissspidy Oct 12, 2023
ba8db30
Fix env vars
swissspidy Oct 12, 2023
e3da622
Merge branch 'trunk' into add/pw-performance-tests
swissspidy Oct 12, 2023
f837898
Merge branch 'WordPress:trunk' into add/pw-performance-tests
swissspidy Oct 12, 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
Prev Previous commit
Next Next commit
REST API: Fix issue with Template and Template Part Revision/Autosave…
… REST API controllers.

The Template and Template Part REST API controllers have unique characteristics compared to other post type REST API controllers. They do not rely on integer IDs to reference objects; instead, they use a combination of the theme name and slug of the template, like 'twentytwentyfour//home.' Consequently, when the post types template and template part were introduced in [52062], it led to the registration of REST API endpoints for autosaves and revisions with invalid URL structures.

In this commit, we introduce new functionality to enable custom autosave and revisions endpoints to be registered at the post type level. Similar to the 'rest_controller_class' parameter, developers can now define 'revisions_rest_controller' and 'autosave_rest_controller.' This empowers developers to create custom controllers for these functionalities. Additionally, we introduce a 'late_route_registration' parameter, which proves helpful when dealing with custom URL patterns and regex pattern matching issues.
This commit registers new classes for template and template part autosave and revisions controllers, differentiating them from standard controllers in the following ways:
* The response shape now matches that of the template controller.
* Permission checks align with the template controller.
* A custom URL pattern is introduced to support slug-based identification of templates.

Furthermore, we've updated the utility function '_build_block_template_result_from_post' to support passing revision post objects. This enhancement ensures compatibility with the custom revisions controller.

Props spacedmonkey, revgeorge, andraganescu, hellofromTonya, antonvlasenko, kadamwhite, ironprogrammer, costdev, mukesh27, timothyblynjacobs, adamsilverstein. 
Fixes 56922.

git-svn-id: https://develop.svn.wordpress.org/trunk@56819 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
spacedmonkey authored and swissspidy committed Oct 12, 2023
commit 050997ce000864b3927175702097fbdec086d60c
26 changes: 17 additions & 9 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,14 +724,22 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy,
*
* @since 5.9.0
* @since 6.3.0 Added `modified` property to template objects.
* @since 6.4.0 Added support for a revision post to be passed to this function.
* @access private
*
* @param WP_Post $post Template post.
* @return WP_Block_Template|WP_Error Template or error object.
*/
function _build_block_template_result_from_post( $post ) {
$default_template_types = get_default_block_template_types();
$terms = get_the_terms( $post, 'wp_theme' );

$post_id = wp_is_post_revision( $post );
if ( ! $post_id ) {
$post_id = $post;
}
$parent_post = get_post( $post_id );

$terms = get_the_terms( $parent_post, 'wp_theme' );

if ( is_wp_error( $terms ) ) {
return $terms;
Expand All @@ -745,12 +753,12 @@ function _build_block_template_result_from_post( $post ) {
$template_file = _get_block_template_file( $post->post_type, $post->post_name );
$has_theme_file = get_stylesheet() === $theme && null !== $template_file;

$origin = get_post_meta( $post->ID, 'origin', true );
$is_wp_suggestion = get_post_meta( $post->ID, 'is_wp_suggestion', true );
$origin = get_post_meta( $parent_post->ID, 'origin', true );
$is_wp_suggestion = get_post_meta( $parent_post->ID, 'is_wp_suggestion', true );

$template = new WP_Block_Template();
$template->wp_id = $post->ID;
$template->id = $theme . '//' . $post->post_name;
$template->id = $theme . '//' . $parent_post->post_name;
$template->theme = $theme;
$template->content = $post->post_content;
$template->slug = $post->post_name;
Expand All @@ -765,23 +773,23 @@ function _build_block_template_result_from_post( $post ) {
$template->author = $post->post_author;
$template->modified = $post->post_modified;

if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {
if ( 'wp_template' === $parent_post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {
$template->post_types = $template_file['postTypes'];
}

if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) {
if ( 'wp_template' === $parent_post->post_type && isset( $default_template_types[ $template->slug ] ) ) {
$template->is_custom = false;
}

if ( 'wp_template_part' === $post->post_type ) {
$type_terms = get_the_terms( $post, 'wp_template_part_area' );
if ( 'wp_template_part' === $parent_post->post_type ) {
$type_terms = get_the_terms( $parent_post, 'wp_template_part_area' );
if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) {
$template->area = $type_terms[0]->name;
}
}

// Check for a block template without a description and title or with a title equal to the slug.
if ( 'wp_template' === $post->post_type && empty( $template->description ) && ( empty( $template->title ) || $template->title === $template->slug ) ) {
if ( 'wp_template' === $parent_post->post_type && empty( $template->description ) && ( empty( $template->title ) || $template->title === $template->slug ) ) {
$matches = array();

// Check for a block template for a single author, page, post, tag, category, custom post type, or custom taxonomy.
Expand Down
193 changes: 162 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,54 @@ final class WP_Post_Type {
*/
public $rest_controller;

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

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

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

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

/**
* A flag to register the post type REST API controller after its associated autosave / revisions controllers, instead of before. Registration order affects route matching priority.
*
* @since 6.4.0
* @var bool $late_route_registration
*/
public $late_route_registration;

/**
* Constructor.
*
Expand Down Expand Up @@ -455,6 +503,7 @@ public function set_props( $args ) {
* - `register_page_post_type_args`
*
* @since 6.0.0
* @since 6.4.0 Added `late_route_registration`, `autosave_rest_controller_class` and `revisions_rest_controller_class` arguments.
*
* @param array $args Array of arguments for registering a post type.
* See the register_post_type() function for accepted arguments.
Expand All @@ -466,37 +515,40 @@ 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,
'late_route_registration' => 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 +868,85 @@ 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.4.0
*
* @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() {
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.4.0
*
* @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;
}

if ( 'attachment' === $this->name ) {
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
Loading