wppaste
WordPress

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
Search and retrieve block patterns metadata

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

Reaches an outbound HTTP request via wp_remote_get().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
47–115

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 138.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • transienttransientget_site_transient()called directly
  • httpoutbound HTTP requestwp_remote_get()called directly
  • serializeserialisationjson_decode()called directly
  • querycontent queryget_user_by()one call below WP_REST_Pattern_Directory_Controller::get_items()
  • hookthird-party callbacksapply_filters()one call below WP_REST_Pattern_Directory_Controller::get_items()
  • cacheobject cachewp_cache_get()one call below WP_REST_Pattern_Directory_Controller::get_items()
  • optionnetwork optionget_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.

WhenInstructionsCalls 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

PHPCompiledExecutedBranchesNotes
8.6-dev13847–11511
8.513847–11511
8.413847–11511
8.313847–11511
8.213847–11511
8.113847–115112 fewer instructions than PHP 7.4
7.414047–11711

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

Show all 16

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&#8217;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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.2.0
Added 'per_page', 'page', 'offset', 'order', and 'orderby' to request.from the docblock
6.0.0
Added 'slug' to request.from the docblock
5.8.0
Introduced.from the docblock

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.