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

Add Post Content attributes as a block editor setting #4614

Conversation

tellthemachines
Copy link
Contributor

@tellthemachines tellthemachines commented Jun 14, 2023

Trac ticket: https://core.trac.wordpress.org/ticket/58534

This PR adds the changes from WordPress/gutenberg#45299.

To test, checkout the PR locally and update the "@wordpress/editor" package to its latest version - "13.12.0". Then run the build, and use the Redux dev tools to inspect settings in the state of the core/block-editor store.

Edit: I somehow left out that this PR also adds the changes from WordPress/gutenberg#49049 🤦


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@audrasjb
Copy link
Contributor

Thanks for the PR!
I added one commit to fix a few coding standards issues :)

@spacedmonkey
Copy link
Member

Can you please add some unit tests to this PR.

* @param array $blocks Array of blocks.
* @return array Found block, or empty array if none found.
*/
function _wp_find_first_block( $block_name, $blocks ) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this function could be useful for the general developer community as well. I know I've reinvented this wheel a few times already for extracting images/excerpts from post content on various projects. Can the pseudo-private _ prefix be removed?

Also, wp_find_ is used as a prefix only 3 times in core right now. As a developer, I generally expect WordPress functions that return data to use wp_get_.

src/wp-includes/block-editor.php Show resolved Hide resolved
@tellthemachines
Copy link
Contributor Author

Thanks for fixing the coding standards stuff @audrasjb! I'm having issues running the composer scripts locally 😩

I've added a test for the now-renamed wp_get_first_block, now working on tests for _wp_get_post_content_block_attributes.

@tellthemachines
Copy link
Contributor Author

I just added a base test case for _wp_get_post_content_block_attributes to check it returns an empty array by default (if there's no post ID and no block template). Not sure how to test this further, as it requires both a post and its block template to exist. I tried setting $post_ID =1 and switch_theme( 'block-theme' ); but that doesn't seem to work. I'm probably missing something obvious 😅 any help appreciated!

@tellthemachines
Copy link
Contributor Author

Update: I got it working! Had to add a single template to the test block theme.

Copy link
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

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

Thanks @tellthemachines, Left some nitpick feedback.

function _wp_get_post_content_block_attributes() {
$is_block_theme = wp_is_block_theme();

global $post_ID;
Copy link
Member

Choose a reason for hiding this comment

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

Missing docblock for global.

* @global int $post_ID

Copy link
Member

Choose a reason for hiding this comment

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

Nit-pick: It would be good to follow the pattern of putting the global declaration to the very top of the function code.

Comment on lines 615 to 616
if ( ! empty( _wp_get_post_content_block_attributes() ) ) {
$editor_settings['postContentAttributes'] = _wp_get_post_content_block_attributes();
Copy link
Member

Choose a reason for hiding this comment

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

_wp_get_post_content_block_attributes() call multiple time if condition true instead define a variable and use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense!

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@tellthemachines Looks solid to me, +1 to having wp_get_first_block() as a generally available function.

I left a few small comments below, but none strictly a blocker (except probably the two documentation bits).

src/wp-includes/block-editor.php Outdated Show resolved Hide resolved
function _wp_get_post_content_block_attributes() {
$is_block_theme = wp_is_block_theme();

global $post_ID;
Copy link
Member

Choose a reason for hiding this comment

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

Nit-pick: It would be good to follow the pattern of putting the global declaration to the very top of the function code.

src/wp-includes/block-editor.php Show resolved Hide resolved
src/wp-includes/block-editor.php Outdated Show resolved Hide resolved
src/wp-includes/block-editor.php Outdated Show resolved Hide resolved
@tellthemachines
Copy link
Contributor Author

Thanks for the feedback folks!

Question on the tests: I added an extra assertion to test_get_block_editor_settings_theme_json_settings to check for the new setting, and in doing so realised that the test is now relying on the global set above in test_wp_get_post_content_block_attributes to pass. This isn't great as it could also alter results of any tests running after. Do we have any best practices for handling globals in tests? Should they be unset after each test?

@tellthemachines
Copy link
Contributor Author

Do we have any best practices for handling globals in tests? Should they be unset after each test?

Update: @hellofromtonya has pointed me in the right direction and I've updated the tests accordingly.

@@ -27,12 +27,18 @@ public function set_up() {
global $wp_rest_server;
$wp_rest_server = new Spy_REST_Server();
do_action( 'rest_api_init', $wp_rest_server );

global $post_ID;
$this->orig_post_id = $post_ID;
Copy link
Contributor

@andrewserong andrewserong Jun 20, 2023

Choose a reason for hiding this comment

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

Looks like this is throwing errors in the PHP 8.2 actions:

image

Would the fix be to define the orig_post_id property at the top of this class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure, so I copied what's being done for the $wp_rest_server global and set it to null on teardown. It shouldn't matter because $post_ID shouldn't be set to any value at this point (if it was, test_wp_get_post_content_block_attributes wouldn't fail without it being set explicitly).

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good to me!

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

Thanks for the updates @tellthemachines, LGTM! Just the one nit-pick remaining, but not a big deal. Feel free to update before commit.

src/wp-includes/block-editor.php Outdated Show resolved Hide resolved
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Copy link
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

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

Thanks @tellthemachines the update. Look good to me.

@mukeshpanchal27
Copy link
Member

@tellthemachines Re-run the failed job it show Navigation timeout of 30000 ms exceeded .

@tellthemachines
Copy link
Contributor Author

Thanks again for the reviews, everyone!

Committed in r55955 / b35b1fc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

7 participants