WP_REST_Pattern_Directory_Controller::get_items( WP_REST_Request $request ): WP_REST_Response|WP_Error
- Since
- 5.8.0, 6.0.0, 6.2.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php:89
Compatibility
- WordPress
- since 6.2.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$requestWP_REST_Request- Full details about the request.
Return value
WP_REST_Response|WP_Error- Response object on success, or WP_Error object on failure.
Performance profile
How much work a call to WP_REST_Pattern_Directory_Controller::get_items() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Expensive
- Scaling
- Scales with input
- Instructions
- 47–115
- Plugin surface
- None
- Called by
- 0
Reaches an outbound HTTP request via wp_remote_get().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 138.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- transienttransient
get_site_transient()called directly - httpoutbound HTTP request
wp_remote_get()called directly - serializeserialisation
json_decode()called directly - querycontent query
get_user_by()one call below WP_REST_Pattern_Directory_Controller::get_items() - hookthird-party callbacks
apply_filters()one call below WP_REST_Pattern_Directory_Controller::get_items() - cacheobject cache
wp_cache_get()one call below WP_REST_Pattern_Directory_Controller::get_items() - optionnetwork option
get_site_option()one call below WP_REST_Pattern_Directory_Controller::get_items()
What one call costs · 10 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Pattern_Directory_Controller::get_items() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_wp_error() | 47–49 | ->get_params(), array_intersect_key(), get_user_locale(), wp_get_wp_version(), array_filter(), ->get_transient_key(), get_site_transient(), is_wp_error(), ->add_data() |
!is_wp_error() | 51–58 | ->get_params(), array_intersect_key(), get_user_locale(), wp_get_wp_version(), array_filter(), ->get_transient_key(), get_site_transient(), is_wp_error(), ->is_method() |
!wp_http_supports() && is_wp_error() | 78–81 | ->get_params(), array_intersect_key(), get_user_locale(), wp_get_wp_version(), array_filter(), ->get_transient_key(), get_site_transient(), build_query(), wp_http_supports(), wp_remote_get(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), set_site_transient(), is_wp_error(), ->add_data() |
!wp_http_supports() && !is_wp_error() | 82–90 | ->get_params(), array_intersect_key(), get_user_locale(), wp_get_wp_version(), array_filter(), ->get_transient_key(), get_site_transient(), build_query(), wp_http_supports(), wp_remote_get(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), set_site_transient(), is_wp_error(), ->is_method() |
wp_http_supports() && is_wp_error() | 83–86 | ->get_params(), array_intersect_key(), get_user_locale(), wp_get_wp_version(), array_filter(), ->get_transient_key(), get_site_transient(), build_query(), wp_http_supports(), set_url_scheme(), wp_remote_get(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), set_site_transient(), is_wp_error(), ->add_data() |
wp_http_supports() && !is_wp_error() | 87–95 | ->get_params(), array_intersect_key(), get_user_locale(), wp_get_wp_version(), array_filter(), ->get_transient_key(), get_site_transient(), build_query(), wp_http_supports(), set_url_scheme(), wp_remote_get(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), set_site_transient(), is_wp_error(), ->is_method() |
!wp_http_supports() && !is_array($raw_patterns) | 99–101 | ->get_params(), array_intersect_key(), get_user_locale(), wp_get_wp_version(), array_filter(), ->get_transient_key(), get_site_transient(), build_query(), wp_http_supports(), wp_remote_get(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), __(), __(), sprintf(), wp_remote_retrieve_body(), response(), set_site_transient(), is_wp_error(), ->add_data() |
!wp_http_supports() && !is_wp_error() && !is_array($raw_patterns) | 103–110 | ->get_params(), array_intersect_key(), get_user_locale(), wp_get_wp_version(), array_filter(), ->get_transient_key(), get_site_transient(), build_query(), wp_http_supports(), wp_remote_get(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), __(), __(), sprintf(), wp_remote_retrieve_body(), response(), set_site_transient(), is_wp_error(), ->is_method() |
wp_http_supports() && !is_array($raw_patterns) | 104–106 | ->get_params(), array_intersect_key(), get_user_locale(), wp_get_wp_version(), array_filter(), ->get_transient_key(), get_site_transient(), build_query(), wp_http_supports(), set_url_scheme(), wp_remote_get(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), __(), __(), sprintf(), wp_remote_retrieve_body(), response(), set_site_transient(), is_wp_error(), ->add_data() |
wp_http_supports() && !is_wp_error() && !is_array($raw_patterns) | 108–115 | ->get_params(), array_intersect_key(), get_user_locale(), wp_get_wp_version(), array_filter(), ->get_transient_key(), get_site_transient(), build_query(), wp_http_supports(), set_url_scheme(), wp_remote_get(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), __(), __(), sprintf(), wp_remote_retrieve_body(), response(), set_site_transient(), is_wp_error(), ->is_method() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 138 | 47–115 | 11 | |
| 8.5 | 138 | 47–115 | 11 | |
| 8.4 | 138 | 47–115 | 11 | |
| 8.3 | 138 | 47–115 | 11 | |
| 8.2 | 138 | 47–115 | 11 | |
| 8.1 | 138 | 47–115 | 11 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 140 | 47–117 | 11 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 16
- get_user_locale()Retrieves the locale of a user.
- wp_get_wp_version()Returns the current WordPress version.
- get_site_transient()Retrieves the value of a site transient.
- build_query()Builds URL query based on an associative and, or indexed array.
- wp_http_supports()Determines if there is an HTTP Transport that can process this request.
- set_url_scheme()Sets the scheme for a URL.
- wp_remote_get()Performs an HTTP request using the GET method and returns its response.
- wp_remote_retrieve_body()Retrieves only the body from the raw response.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- __()Retrieves the translation of $text.
- set_site_transient()Sets/updates the value of a site transient.
- WP_REST_Pattern_Directory_Controller::get_transient_key()Include a hash of the query args, so that different requests are stored in separate caches.
Show all 16
- WP_Error::__construct()Initializes the error.
- WP_REST_Response::__construct()
- WP_REST_Pattern_Directory_Controller::prepare_response_for_collection()
- WP_REST_Pattern_Directory_Controller::prepare_item_for_response()Prepare a raw block pattern before it gets output in a REST API response.
Source code
public function get_items( $request ) { $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_get_wp_version(); $query_args['pattern-categories'] = isset( $request['category'] ) ? $request['category'] : false; $query_args['pattern-keywords'] = isset( $request['keyword'] ) ? $request['keyword'] : false; $query_args = array_filter( $query_args ); $transient_key = $this->get_transient_key( $query_args ); /* * Use network-wide transient to improve performance. The locale is the only site * configuration that affects the response, and it's included in the transient key. */ $raw_patterns = get_site_transient( $transient_key ); if ( ! $raw_patterns ) { $api_url = 'http://api.wordpress.org/patterns/1.0/?' . build_query( $query_args ); if ( wp_http_supports( array( 'ssl' ) ) ) { $api_url = set_url_scheme( $api_url, 'https' ); } /* * Default to a short TTL, to mitigate cache stampedes on high-traffic sites. * This assumes that most errors will be short-lived, e.g., packet loss that causes the * first request to fail, but a follow-up one will succeed. The value should be high * enough to avoid stampedes, but low enough to not interfere with users manually * re-trying a failed request. */ $cache_ttl = 5; $wporg_response = wp_remote_get( $api_url ); $raw_patterns = json_decode( wp_remote_retrieve_body( $wporg_response ) ); if ( is_wp_error( $wporg_response ) ) { $raw_patterns = $wporg_response; } elseif ( ! is_array( $raw_patterns ) ) { // HTTP request succeeded, but response data is invalid. $raw_patterns = new WP_Error( 'pattern_api_failed', sprintf( /* translators: %s: Support forums URL. */ __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ), __( 'https://wordpress.org/support/forums/' ) ), array( 'response' => wp_remote_retrieve_body( $wporg_response ), ) ); } else { // Response has valid data. $cache_ttl = HOUR_IN_SECONDS; } set_site_transient( $transient_key, $raw_patterns, $cache_ttl ); } if ( is_wp_error( $raw_patterns ) ) { $raw_patterns->add_data( array( 'status' => 500 ) ); return $raw_patterns; } if ( $request->is_method( 'HEAD' ) ) { // Return early as this handler doesn't add any response headers. return new WP_REST_Response( array() ); }Changelog
Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.