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 new query args to Pattern Directory Controller #3861

Conversation

ntsekouras
Copy link

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

Adds the 'per_page', 'page', 'offset', 'order', and 'orderby' args to WP_REST_Pattern_Directory_Controller.

GB PR: WordPress/gutenberg#45293

--cc @hellofromtonya @ryelle


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.

Comment on lines 97 to 104
$valid_query_args = array( 'offset', 'order', 'orderby', 'page', 'per_page', 'search', 'slug' );
$query_args = array_merge(
array_intersect_key( $request->get_params(), array_flip( $valid_query_args ) ),
array(
'locale' => get_user_locale(),
'wp-version' => $wp_version, // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable -- it's defined in `version.php` above.
)
);
Copy link
Contributor

Choose a reason for hiding this comment

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

This code can be simplified and made more performant:

Suggested change
$valid_query_args = array( 'offset', 'order', 'orderby', 'page', 'per_page', 'search', 'slug' );
$query_args = array_merge(
array_intersect_key( $request->get_params(), array_flip( $valid_query_args ) ),
array(
'locale' => get_user_locale(),
'wp-version' => $wp_version, // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable -- it's defined in `version.php` above.
)
);
$valid_query_args = array(
'offset' => true,
'order' => true,
'orderby' => true,
'page' => true,
'per_page' => true,
'search' => true,
'slug' => true,
);
$query_args = array_intersect_key( $request->get_params(), $valid_query_args );
$query_args['locale'] = get_user_locale();
$query_args['wp-version'] = $wp_version;

The array_flip() creates an array with the values of $valid_query_args as the keys. Since this is used only for filtering the args, why not set up the $valid_query_args into a keyed structure? In doing so, the array_flip() is eliminated.

The array_merge() also creates another array. As 'locale' and 'wp-version' keys will not be in the array after doing the array_intersect_key(), a more performant way is to directly adding them to $query_args rather than array_merge().

The changes remove 2 unnecessary array iterations and creations.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed in 1cede3c.

Copy link
Contributor

Choose a reason for hiding this comment

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

See it in action https://3v4l.org/DGvja

Copy link
Author

Choose a reason for hiding this comment

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

Thanks @hellofromtonya !

Copy link
Contributor

@hellofromtonya hellofromtonya left a comment

Choose a reason for hiding this comment

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

The code itself LGTM 👍

Next step is to test it and add a Test Report. If all is good, then commit.

@hellofromtonya
Copy link
Contributor

Once committed, I can open a PR in Gutenberg to synch the changes from here back to Gutenberg.

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 @ntsekouras, Left some feedback on unit test.

}

/**
* Data provider.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Data provider.
* Data provider for test_get_items_query_args().

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is unnecessary. The naming convention of test_ and data_ align to know that this data provider is for the matching test.

Copy link
Contributor

@hellofromtonya hellofromtonya Jan 24, 2023

Choose a reason for hiding this comment

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

Whoops forgot to share where the naming convention is identified the handbook 🤦‍♀️

A data provider is a secondary function which is linked to a test using a@dataProviderannotation in the docblock of the test method and is generally named after the test, replacing the test prefix in the method name with data.

https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/#repetitive-tests.

Comment on lines +775 to +782
/**
* Mock the request to wp.org URL to capture the URLs.
*
* @since 6.2.0
*
* @return array faux/mocked response.
*/
public function mock_request_to_apiwporg_url( $response, $args, $url ) {
Copy link
Member

Choose a reason for hiding this comment

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

In the core, some mock functions have the document and some don't. I appreciated @hellofromtonya's and @SergeyBiryukov's input on this. 

Copy link
Contributor

Choose a reason for hiding this comment

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

I know. The handbook and similar test mocks needs some love to align for consistency. I agree!

@hellofromtonya
Copy link
Contributor

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

Successfully merging this pull request may close these issues.

3 participants