WP_REST_URL_Details_Controller::parse_url_details()publicWP 5.9.0

Retrieves the contents of the title tag from the HTML response.

Метод класса: WP_REST_URL_Details_Controller{}

Хуки из метода

Возвращает

WP_REST_Response|WP_Error. The parsed details as a response object. WP_Error if there are errors.

Использование

$WP_REST_URL_Details_Controller = new WP_REST_URL_Details_Controller();
$WP_REST_URL_Details_Controller->parse_url_details( $request );
$request(WP_REST_REQUEST) (обязательный)
Full details about the request.

Список изменений

С версии 5.9.0 Введена.

Код WP_REST_URL_Details_Controller::parse_url_details() WP 6.4.3

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 );
}