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

Use core version of template and template part post types and REST endpoints for WP 5.9, with back compat for 5.8 #36898

Merged
merged 9 commits into from
Dec 2, 2021

Conversation

talldan
Copy link
Contributor

@talldan talldan commented Nov 26, 2021

Description

When the Gutenberg plugin is active, it's currently using it's own version of the template and template part post types and REST endpoints.

Usually the practice is to rely on the core version of PHP code when it exists.

register_post_type overwrites any previous registration and we don't want it to overwrite the core version of the post types. The fix here is to check whether the post types have already been registered before attempting to register them again.

This also solves the REST API issue, as that's also defined as part of the post type configuration.

How has this been tested?

  1. Make sure you're running WordPress 5.9
  2. Change something related to one of the post types in the Gutenberg codebase (i.e. one of the labels)
  3. Load the relevant part of the UI and notice the change is not displayed because this code isn't active.

Types of changes

Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

@talldan talldan added [Type] Bug An existing feature does not function as intended Core REST API Task Task for Core REST API efforts [Block] Template Part Affects the Template Parts Block labels Nov 26, 2021
@talldan talldan self-assigned this Nov 26, 2021
@youknowriad
Copy link
Contributor

Is it possible to move this code into compat/5.9 to know that it's only used on older WP versions?

@talldan
Copy link
Contributor Author

talldan commented Nov 26, 2021

@Mamaduka pointed out that this won't work for users of WordPress 5.8, because it'll cause Gutenberg to use the outdated REST APIs in 5.8.

Is it possible to move this code into compat/5.9 to know that it's only used on older WP versions?

@youknowriad Will that solve the problem mentioned above? I'll have a look at it. At the end of my day now, but I don't think there's a rush to do this, so will try to tackle it on Monday.

@youknowriad
Copy link
Contributor

Will that solve the problem mentioned above?

No it won't but we can still achieve it by testing something specific to WP 5.9 before registering the CPT and endpoints.

@talldan
Copy link
Contributor Author

talldan commented Nov 26, 2021

No it won't but we can still achieve it by testing something specific to WP 5.9 before registering the CPT and endpoints.

Great, thanks for the advice. I'll make those changes on Monday 👍

@talldan talldan force-pushed the fix/use-core-version-of-templates-rest-api branch from b03d527 to b48f643 Compare December 1, 2021 05:38

return $new_content;
if ( ! $has_updated_content ) {
return $template_content;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were some minor changes to this function in WordPress core, so I've replicated them for Gutenberg.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this branch there is no foreach ( $template_blocks as $block ) which is something I see in the Core version. Is that right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, oops. I obviously wasn't thorough enough. Fixed in #37137.

@talldan talldan marked this pull request as draft December 1, 2021 07:34
@@ -9,7 +9,7 @@
/**
* Class representing a block template.
*/
class WP_Block_Template {
class Gutenberg_Block_Template {
Copy link
Contributor Author

@talldan talldan Dec 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've renamed this and the situation should be:

  • If Gutenberg is running on WordPress 5.8, Gutenberg_Block_Template is used instead of WP_Block_Template`, since the latter is outdated in 5.8.
  • If Gutenberg is running on WordPress 5.9, WP_Block_Template is used.

This should resolve any issues where author or origin might not be defined on 5.8.

@talldan talldan changed the title Use core version of template and template part post types and REST endpoints Use core version of template and template part post types and REST endpoints for WP 5.9, with back compat for 5.8 Dec 1, 2021
@@ -33,7 +33,6 @@ public function set_up() {
* @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
*/
public static function wpSetupBeforeClass( $factory ) {
gutenberg_register_wp_theme_taxonomy();
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 function is now not defined for Gutenberg running against WP 5.9, as the taxonomy is registered in core. The call here was making the test fail though. I deleted it and the tests still pass, so I'll call that a win 🤷

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@youknowriad youknowriad added the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Dec 1, 2021
@youknowriad
Copy link
Contributor

Technically there's no backport to be done into Core but we should sync the "wp/trunk" branch, so I added the label.

@talldan
Copy link
Contributor Author

talldan commented Dec 1, 2021

Thanks for the thumbs up. I'll do some testing against 5.8 tomorrow before merging just to make sure I didn't break anything.

I'll also follow up to fix some more little inconsistencies between core and Gutenberg.

@talldan talldan marked this pull request as ready for review December 2, 2021 08:50
@talldan talldan merged commit 8eb1f34 into trunk Dec 2, 2021
@talldan talldan deleted the fix/use-core-version-of-templates-rest-api branch December 2, 2021 08:51
@talldan
Copy link
Contributor Author

talldan commented Dec 2, 2021

Tested against 5.8.2 and can't see any issues, so moving ahead with merging this.

@github-actions github-actions bot added this to the Gutenberg 12.2 milestone Dec 2, 2021
@noisysocks noisysocks removed the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Dec 6, 2021
noisysocks pushed a commit that referenced this pull request Dec 6, 2021
…dpoints for WP 5.9, with back compat for 5.8 (#36898)

* Use core version of template and template part post types and REST endpoints

* Move files and add explanation

* Only register gutenberg version of templates and template parts when WP5.8.x is active

* Rename WP_Block_Template to Gutenberg_Block_Template so that 5.8 receives improvements to it

* Load moved files

* Update references and sync some code

* Fix filename

* Remove templates tests since Gutenberg now defaults to using the WP core implementation

* Linting and test fixes

return $new_content;
if ( ! $has_updated_content ) {
return $template_content;
}

return $template_content;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if is now unnecessary. In both cases we return $template_content;. Is that right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Template Part Affects the Template Parts Block Core REST API Task Task for Core REST API efforts [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants