-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add new query args to Pattern Directory Controller #3861
Conversation
$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. | ||
) | ||
); |
There was a problem hiding this comment.
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:
$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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 1cede3c.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @hellofromtonya !
Reuses pattern from other "pre_http_request" mocks to convert the static function into a public callback.
There was a problem hiding this 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.
Once committed, I can open a PR in Gutenberg to synch the changes from here back to Gutenberg. |
There was a problem hiding this 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.
tests/phpunit/tests/rest-api/rest-pattern-directory-controller.php
Outdated
Show resolved
Hide resolved
} | ||
|
||
/** | ||
* Data provider. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Data provider. | |
* Data provider for test_get_items_query_args(). |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
@dataProvider
annotation in the docblock of the test method and is generally named after the test, replacing thetest
prefix in the method name withdata
.
/** | ||
* 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 ) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
Co-authored-by: Mukesh Panchal <[email protected]>
Committed via https://core.trac.wordpress.org/changeset/55132. |
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.