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

Server directive processing: Improve how block references are saved #56107

Merged
merged 8 commits into from Nov 15, 2023

Conversation

cbravobernal
Copy link
Contributor

What?

A small refactor to process only root blocks, deleting the root block reference after processing instead of using arrays comparison.

Why?

Although previous version works, this one is easier to read and have better performance.

@cbravobernal cbravobernal changed the title [Server Directives Processing] - Improve how block references are saved Server directive processing: Improve how block references are saved Nov 14, 2023
Copy link

This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress.

If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged.

If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack.

Thank you! ❤️

View changed files
❔ lib/experimental/interactivity-api/class-wp-directive-processor.php
❔ lib/experimental/interactivity-api/directive-processing.php
❔ phpunit/experimental/interactivity-api/directive-processing-test.php

@cbravobernal cbravobernal force-pushed the refactor/process-only-root-blocks branch from 4e57006 to f165fb7 Compare November 14, 2023 16:00
@cbravobernal cbravobernal marked this pull request as ready for review November 14, 2023 18:09
@cbravobernal cbravobernal added [Type] Code Quality Issues or PRs that relate to code quality [Packages] Interactivity /packages/interactivity labels Nov 14, 2023
Copy link
Member

@luisherranz luisherranz left a comment

Choose a reason for hiding this comment

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

Hey, Carlos, can you please add these tests:

  • Check how many times process_rendered_html has been called for different blocks, including one that has a Pattern block.
  • Check that even if a block with the same md5 is found, it's not processed if it's not a root block.

One question: Why did you decide not to create a different class than WP_Directive_Processor? Didn't it make sense?

@@ -19,120 +19,43 @@
* @return array The parsed block.
*/
function gutenberg_interactivity_process_directives( $parsed_block, $source_block, $parent_block ) {
Copy link
Member

Choose a reason for hiding this comment

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

We can rename this filter again because it's not processing the directives anymore, only marking the root blocks.

Also, the DocBlock description needs to be updated.

),
);
// Test that root block is not added if there is a parent block.
gutenberg_interactivity_process_directives( $parsed_block, $parsed_block, $parsed_block );
Copy link
Member

Choose a reason for hiding this comment

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

Passing $parsed_block three times is confusing.

Suggested change
gutenberg_interactivity_process_directives( $parsed_block, $parsed_block, $parsed_block );
$fake_parent_block = array();
$source_block = $parsed_block;
gutenberg_interactivity_process_directives( $parsed_block, $source_block, $fake_parent_block );

Comment on lines 107 to 108
// Test that a root block is not added if there is already a root block defined.
gutenberg_interactivity_process_directives( $parsed_block_second, $parsed_block_second, $parsed_block_second );
Copy link
Member

Choose a reason for hiding this comment

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

You are not testing root blocks if you are passing a parent.

Suggested change
// Test that a root block is not added if there is already a root block defined.
gutenberg_interactivity_process_directives( $parsed_block_second, $parsed_block_second, $parsed_block_second );
// Test that a root block is not added if there is already a root block defined.
gutenberg_interactivity_process_directives( $parsed_block_second, $parsed_block_second, null );

@cbravobernal
Copy link
Contributor Author

One question: Why did you decide not to create a different class than WP_Directive_Processor? Didn't it make sense?

Not yet, I think we should keep working on that class and then split it if we consider necessary.

@cbravobernal
Copy link
Contributor Author

Check how many times process_rendered_html has been called for different blocks, including one that has a Pattern block.

I'm afraid we cannot do this as a Unit test (is more an e2e test). In order to count how many times a function is called, you need a similar code to this one (provided by Copilot)

public function testProcessRenderedHtmlCalledXTimes()
    {
        // Create a mock for the class containing the process_rendered_html method
        $tagsMock = $this->getMockBuilder('TheClassName') // replace 'TheClassName' with the actual class name
                         ->setMethods(['process_rendered_html'])
                         ->getMock();

        // Specify the number of times the method should be called
        $tagsMock->expects($this->exactly(3)) // replace 3 with the number of times the method should be called
                 ->method('process_rendered_html')
                 ->willReturn('...'); // replace '...' with the value you want the method to return

        // Replace the real object with the mock in your function
        $tags = $tagsMock;
        $directives = array(
            'data-wp-bind'    => 'gutenberg_interactivity_process_wp_bind',
            'data-wp-context' => 'gutenberg_interactivity_process_wp_context',
            'data-wp-class'   => 'gutenberg_interactivity_process_wp_class',
            'data-wp-style'   => 'gutenberg_interactivity_process_wp_style',
            'data-wp-text'    => 'gutenberg_interactivity_process_wp_text',
        );

        // Call the method the specified number of times
        $tags->process_rendered_html($tags, 'data-wp-', $directives);
        $tags->process_rendered_html($tags, 'data-wp-', $directives);
        $tags->process_rendered_html($tags, 'data-wp-', $directives);
    }

You are "duplicating" the class, so using it in different functions won't work.

Also, that way, we are testing different things rather than the "root blocks code" of the PR, we would be testing:

  • That a filter is executed.
  • That render_block_data filter provides a $parent_block variable.

We can instead provide an mocked $parent_block just to check that function gutenberg_interactivity_process_directives does what it is required.

Hope it helps!

@cbravobernal
Copy link
Contributor Author

@luisherranz I've addressed most of the changes requested, thanks for reviewing!

Copy link
Member

@luisherranz luisherranz left a comment

Choose a reason for hiding this comment

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

Perfect. Thanks @c4rl0sbr4v0!!

@cbravobernal cbravobernal enabled auto-merge (squash) November 15, 2023 14:59
@cbravobernal cbravobernal merged commit cbe7680 into trunk Nov 15, 2023
49 checks passed
@cbravobernal cbravobernal deleted the refactor/process-only-root-blocks branch November 15, 2023 15:34
@youknowriad
Copy link
Contributor

Has this been backported to Core already?

@luisherranz luisherranz added the Backported to WP Core Pull request that has been successfully merged into WP Core label Feb 8, 2024
@luisherranz
Copy link
Member

Yesss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported to WP Core Pull request that has been successfully merged into WP Core [Packages] Interactivity /packages/interactivity [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants