WP_REST_Themes_Controller::prepare_item_for_response()publicWP 5.0.0

Prepares a single theme output for response.

Method of the class: WP_REST_Themes_Controller{}

Hooks from the method

Return

WP_REST_Response. Response object.

Usage

$WP_REST_Themes_Controller = new WP_REST_Themes_Controller();
$WP_REST_Themes_Controller->prepare_item_for_response( $item, $request );
$item(WP_Theme) (required)
Theme object.
$request(WP_REST_Request) (required)
Request object.

Changelog

Since 5.0.0 Introduced.
Since 5.9.0 Renamed $theme to $item to match parent class for PHP 8 named parameter support.
Since 6.6.0 Added stylesheet_uri template_uri fields.

WP_REST_Themes_Controller::prepare_item_for_response() code WP 6.6.2

public function prepare_item_for_response( $item, $request ) {
	// Restores the more descriptive, specific name for use within this method.
	$theme = $item;

	$fields = $this->get_fields_for_response( $request );
	$data   = array();

	if ( rest_is_field_included( 'stylesheet', $fields ) ) {
		$data['stylesheet'] = $theme->get_stylesheet();
	}

	if ( rest_is_field_included( 'template', $fields ) ) {
		/**
		 * Use the get_template() method, not the 'Template' header, for finding the template.
		 * The 'Template' header is only good for what was written in the style.css, while
		 * get_template() takes into account where WordPress actually located the theme and
		 * whether it is actually valid.
		 */
		$data['template'] = $theme->get_template();
	}

	$plain_field_mappings = array(
		'requires_php' => 'RequiresPHP',
		'requires_wp'  => 'RequiresWP',
		'textdomain'   => 'TextDomain',
		'version'      => 'Version',
	);

	foreach ( $plain_field_mappings as $field => $header ) {
		if ( rest_is_field_included( $field, $fields ) ) {
			$data[ $field ] = $theme->get( $header );
		}
	}

	if ( rest_is_field_included( 'screenshot', $fields ) ) {
		// Using $theme->get_screenshot() with no args to get absolute URL.
		$data['screenshot'] = $theme->get_screenshot() ? $theme->get_screenshot() : '';
	}

	$rich_field_mappings = array(
		'author'      => 'Author',
		'author_uri'  => 'AuthorURI',
		'description' => 'Description',
		'name'        => 'Name',
		'tags'        => 'Tags',
		'theme_uri'   => 'ThemeURI',
	);

	foreach ( $rich_field_mappings as $field => $header ) {
		if ( rest_is_field_included( "{$field}.raw", $fields ) ) {
			$data[ $field ]['raw'] = $theme->display( $header, false, true );
		}

		if ( rest_is_field_included( "{$field}.rendered", $fields ) ) {
			$data[ $field ]['rendered'] = $theme->display( $header );
		}
	}

	$current_theme = wp_get_theme();
	if ( rest_is_field_included( 'status', $fields ) ) {
		$data['status'] = ( $this->is_same_theme( $theme, $current_theme ) ) ? 'active' : 'inactive';
	}

	if ( rest_is_field_included( 'theme_supports', $fields ) && $this->is_same_theme( $theme, $current_theme ) ) {
		foreach ( get_registered_theme_features() as $feature => $config ) {
			if ( ! is_array( $config['show_in_rest'] ) ) {
				continue;
			}

			$name = $config['show_in_rest']['name'];

			if ( ! rest_is_field_included( "theme_supports.{$name}", $fields ) ) {
				continue;
			}

			if ( ! current_theme_supports( $feature ) ) {
				$data['theme_supports'][ $name ] = $config['show_in_rest']['schema']['default'];
				continue;
			}

			$support = get_theme_support( $feature );

			if ( isset( $config['show_in_rest']['prepare_callback'] ) ) {
				$prepare = $config['show_in_rest']['prepare_callback'];
			} else {
				$prepare = array( $this, 'prepare_theme_support' );
			}

			$prepared = $prepare( $support, $config, $feature, $request );

			if ( is_wp_error( $prepared ) ) {
				continue;
			}

			$data['theme_supports'][ $name ] = $prepared;
		}
	}

	if ( rest_is_field_included( 'is_block_theme', $fields ) ) {
		$data['is_block_theme'] = $theme->is_block_theme();
	}

	if ( rest_is_field_included( 'stylesheet_uri', $fields ) ) {
		if ( $this->is_same_theme( $theme, $current_theme ) ) {
			$data['stylesheet_uri'] = get_stylesheet_directory_uri();
		} else {
			$data['stylesheet_uri'] = $theme->get_stylesheet_directory_uri();
		}
	}

	if ( rest_is_field_included( 'template_uri', $fields ) ) {
		if ( $this->is_same_theme( $theme, $current_theme ) ) {
			$data['template_uri'] = get_template_directory_uri();
		} else {
			$data['template_uri'] = $theme->get_template_directory_uri();
		}
	}

	$data = $this->add_additional_fields_to_object( $data, $request );

	// Wrap the data in a response object.
	$response = rest_ensure_response( $data );

	if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
		$response->add_links( $this->prepare_links( $theme ) );
	}

	/**
	 * Filters theme data returned from the REST API.
	 *
	 * @since 5.0.0
	 *
	 * @param WP_REST_Response $response The response object.
	 * @param WP_Theme         $theme    Theme object used to create response.
	 * @param WP_REST_Request  $request  Request object.
	 */
	return apply_filters( 'rest_prepare_theme', $response, $theme, $request );
}