WP_REST_URL_Details_Controller::parse_url_details( WP_REST_Request $request ): WP_REST_Response|WP_Error
- Since
- 5.9.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php:134
Compatibility
- WordPress
- since 5.9.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- The parsed details as a response object. WP_Error if there are errors.
Performance profile
How much work a call to WP_REST_URL_Details_Controller::parse_url_details() 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
- Trivial
- Scaling
- Constant
- Instructions
- 18–76
- Plugin surface
- 1 hook
- Called by
- 0
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 88.
Third-party callbacks on 'rest_prepare_url_details' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_URL_Details_Controller::parse_url_details() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($url) | 18 | untrailingslashit(), __() |
!empty($url) && empty($cached_response) | 28–30 | untrailingslashit(), ->build_cache_key_for_url(), ->get_cache(), ->get_remote_url(), is_wp_error() |
!empty($url) && !empty($cached_response) | 64 | untrailingslashit(), ->build_cache_key_for_url(), ->get_cache(), ->get_document_head(), ->get_meta_with_content_elements(), ->get_title(), ->get_icon(), ->get_description(), ->get_image(), title(), rest_ensure_response(), apply_filters() |
!empty($url) && empty($cached_response) && !is_wp_error() && !empty($remote_url_response) | 76 | untrailingslashit(), ->build_cache_key_for_url(), ->get_cache(), ->get_remote_url(), is_wp_error(), ->set_cache(), ->get_document_head(), ->get_meta_with_content_elements(), ->get_title(), ->get_icon(), ->get_description(), ->get_image(), title(), rest_ensure_response(), apply_filters() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 88 instructions, 18–76 executed per call, 4 branches. The work does not change between versions.
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.
Hooks and filters fired · 1
One hook fires while WP_REST_URL_Details_Controller::parse_url_details() runs, in this order:
- apply_filters( rest_prepare_url_details )filterline 187 (+53 into the body)
Filters the URL data for the response.
Uses · 17
- untrailingslashit()Removes trailing forward slashes and backslashes if they exist.
- __()Retrieves the translation of $text.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- rest_ensure_response()Ensures a REST response is a response object (for consistency).
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_Error::__construct()Initializes the error.
- WP_REST_URL_Details_Controller::build_cache_key_for_url()Utility function to build cache key for a given URL.
- WP_REST_URL_Details_Controller::get_cache()Utility function to retrieve a value from the cache at a given key.
- WP_REST_URL_Details_Controller::get_remote_url()Retrieves the document title from a remote URL.
- WP_REST_URL_Details_Controller::set_cache()Utility function to cache a given data set at a given cache key.
- WP_REST_URL_Details_Controller::get_document_head()Retrieves the head element section.
- WP_REST_URL_Details_Controller::get_meta_with_content_elements()Gets all the meta tag elements that have a 'content' attribute.
Show all 17
- WP_REST_URL_Details_Controller::add_additional_fields_to_object()
- WP_REST_URL_Details_Controller::get_title()Parses the title tag contents from the provided HTML.
- WP_REST_URL_Details_Controller::get_icon()Parses the site icon from the provided HTML.
- WP_REST_URL_Details_Controller::get_description()Parses the meta description from the provided HTML.
- WP_REST_URL_Details_Controller::get_image()Parses the Open Graph (OG) Image from the provided HTML.
Source code
public function parse_url_details( $request ) { $url = untrailingslashit( $request['url'] ); if ( empty( $url ) ) { return new WP_Error( 'rest_invalid_url', __( 'Invalid URL' ), array( 'status' => 404 ) ); } // Transient per URL. $cache_key = $this->build_cache_key_for_url( $url ); // Attempt to retrieve cached response. $cached_response = $this->get_cache( $cache_key ); if ( ! empty( $cached_response ) ) { $remote_url_response = $cached_response; } else { $remote_url_response = $this->get_remote_url( $url ); // Exit if we don't have a valid body or it's empty. if ( is_wp_error( $remote_url_response ) || empty( $remote_url_response ) ) { return $remote_url_response; } // Cache the valid response. $this->set_cache( $cache_key, $remote_url_response ); } $html_head = $this->get_document_head( $remote_url_response ); $meta_elements = $this->get_meta_with_content_elements( $html_head ); $data = $this->add_additional_fields_to_object( array( 'title' => $this->get_title( $html_head ), 'icon' => $this->get_icon( $html_head, $url ), 'description' => $this->get_description( $meta_elements ), 'image' => $this->get_image( $meta_elements, $url ), ), $request ); // Wrap the data in a response object. $response = rest_ensure_response( $data ); /** * Filters the URL data for the response. * * @since 5.9.0 * * @param WP_REST_Response $response The response object. * @param string $url The requested URL. * @param WP_REST_Request $request Request object. * @param string $remote_url_response HTTP response body from the remote URL. */ return apply_filters( 'rest_prepare_url_details', $response, $url, $request, $remote_url_response ); }Changelog
Introduced in 5.9.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.7.7 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-url-details-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.