Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Editor: Add original_source and author_text to the templates REST API.
For the new "All templates" UI to work properly we need the REST API to provide to additional fields original_source, and author_text.

Props ntsekouras, get_dave.
Fixes #60358.

git-svn-id: https://develop.svn.wordpress.org/trunk@57366 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
jorgefilipecosta committed Jan 27, 2024
commit 400a7ac31b79411ed69751810d35a4391cd6e132
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,14 @@ public function prepare_item_for_response( $item, $request ) {
$data['modified'] = mysql_to_rfc3339( $template->modified );
}

if ( rest_is_field_included( 'author_text', $fields ) ) {
$data['author_text'] = self::get_wp_templates_author_text_field( $template );
}

if ( rest_is_field_included( 'original_source', $fields ) ) {
$data['original_source'] = self::get_wp_templates_original_source_field( $template );
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
Expand All @@ -748,6 +756,85 @@ public function prepare_item_for_response( $item, $request ) {
return $response;
}

/**
* Returns the source from where the template originally comes from.
*
* @access private
* @internal
*
* @param WP_Block_Template $template_object Template instance.
* @return string Original source of the template one of theme, plugin, site, or user.
*/
private static function get_wp_templates_original_source_field( $template_object ) {
if ( 'wp_template' === $template_object->type || 'wp_template_part' === $template_object->type ) {
// Added by theme.
// Template originally provided by a theme, but customized by a user.
// Templates originally didn't have the 'origin' field so identify
// older customized templates by checking for no origin and a 'theme'
// or 'custom' source.
if ( $template_object->has_theme_file &&
( 'theme' === $template_object->origin || (
empty( $template_object->origin ) && in_array(
$template_object->source,
array(
'theme',
'custom',
),
true
) )
)
) {
return 'theme';
}

// Added by plugin.
if ( $template_object->has_theme_file && 'plugin' === $template_object->origin ) {
return 'plugin';
}

// Added by site.
// Template was created from scratch, but has no author. Author support
// was only added to templates in WordPress 5.9. Fallback to showing the
// site logo and title.
if ( empty( $template_object->has_theme_file ) && 'custom' === $template_object->source && empty( $template_object->author ) ) {
return 'site';
}
}

// Added by user.
return 'user';
}

/**
* Returns a human readable text for the author of the template.
*
* @access private
* @internal
*
* @param WP_Block_Template $template_object Template instance.
* @return string Human readable text for the author.
*/
private static function get_wp_templates_author_text_field( $template_object ) {
$original_source = self::get_wp_templates_original_source_field( $template_object );
switch ( $original_source ) {
case 'theme':
$theme_name = wp_get_theme( $template_object->theme )->get( 'Name' );
return empty( $theme_name ) ? $template_object->theme : $theme_name;
case 'plugin':
$plugins = get_plugins();
$plugin = $plugins[ plugin_basename( sanitize_text_field( $template_object->theme . '.php' ) ) ];
return empty( $plugin['Name'] ) ? $template_object->theme : $plugin['Name'];
case 'site':
return get_bloginfo( 'name' );
case 'user':
$author = get_user_by( 'id', $template_object->author );
if ( ! $author ) {
return __( 'Unknown author' );
}
return $author->get( 'display_name' );
}
}


/**
* Prepares links for the request.
Expand Down Expand Up @@ -861,43 +948,43 @@ public function get_item_schema() {
'title' => $this->post_type,
'type' => 'object',
'properties' => array(
'id' => array(
'id' => array(
'description' => __( 'ID of template.' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'slug' => array(
'slug' => array(
'description' => __( 'Unique slug identifying the template.' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
'required' => true,
'minLength' => 1,
'pattern' => '[a-zA-Z0-9_\%-]+',
),
'theme' => array(
'theme' => array(
'description' => __( 'Theme identifier for the template.' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'type' => array(
'type' => array(
'description' => __( 'Type of template.' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'source' => array(
'source' => array(
'description' => __( 'Source of template' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'origin' => array(
'origin' => array(
'description' => __( 'Source of a customized template' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'content' => array(
'content' => array(
'description' => __( 'Content of template.' ),
'type' => array( 'object', 'string' ),
'default' => '',
Expand All @@ -916,7 +1003,7 @@ public function get_item_schema() {
),
),
),
'title' => array(
'title' => array(
'description' => __( 'Title of template.' ),
'type' => array( 'object', 'string' ),
'default' => '',
Expand All @@ -935,43 +1022,61 @@ public function get_item_schema() {
),
),
),
'description' => array(
'description' => array(
'description' => __( 'Description of template.' ),
'type' => 'string',
'default' => '',
'context' => array( 'embed', 'view', 'edit' ),
),
'status' => array(
'status' => array(
'description' => __( 'Status of template.' ),
'type' => 'string',
'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ),
'default' => 'publish',
'context' => array( 'embed', 'view', 'edit' ),
),
'wp_id' => array(
'wp_id' => array(
'description' => __( 'Post ID.' ),
'type' => 'integer',
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'has_theme_file' => array(
'has_theme_file' => array(
'description' => __( 'Theme file exists.' ),
'type' => 'bool',
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'author' => array(
'author' => array(
'description' => __( 'The ID for the author of the template.' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
),
'modified' => array(
'modified' => array(
'description' => __( "The date the template was last modified, in the site's timezone." ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'author_text' => array(
'type' => 'string',
'description' => __( 'Human readable text for the author.' ),
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
'original_source' => array(
'description' => __( 'Where the template originally comes from e.g. \'theme\'' ),
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
'enum' => array(
'theme',
'plugin',
'site',
'user',
),
),
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public function test_get_item_schema() {

$properties = $data['schema']['properties'];

$this->assertCount( 16, $properties );
$this->assertCount( 18, $properties );
$this->assertArrayHasKey( 'id', $properties, 'ID key should exist in properties.' );
$this->assertArrayHasKey( 'slug', $properties, 'Slug key should exist in properties.' );
$this->assertArrayHasKey( 'theme', $properties, 'Theme key should exist in properties.' );
Expand All @@ -326,6 +326,8 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'modified', $properties, 'modified key should exist in properties.' );
$this->assertArrayHasKey( 'is_custom', $properties, 'is_custom key should exist in properties.' );
$this->assertArrayHasKey( 'parent', $properties, 'Parent key should exist in properties.' );
$this->assertArrayHasKey( 'author_text', $properties, 'Parent key should exist in properties.' );
$this->assertArrayHasKey( 'original_source', $properties, 'Parent key should exist in properties.' );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public function test_get_item_schema() {
$data = $response->get_data();
$properties = $data['schema']['properties'];

$this->assertCount( 16, $properties );
$this->assertCount( 18, $properties );
$this->assertArrayHasKey( 'id', $properties, 'ID key should exist in properties.' );
$this->assertArrayHasKey( 'slug', $properties, 'Slug key should exist in properties.' );
$this->assertArrayHasKey( 'theme', $properties, 'Theme key should exist in properties.' );
Expand All @@ -465,6 +465,8 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'modified', $properties, 'modified key should exist in properties.' );
$this->assertArrayHasKey( 'is_custom', $properties, 'is_custom key should exist in properties.' );
$this->assertArrayHasKey( 'parent', $properties, 'Parent key should exist in properties.' );
$this->assertArrayHasKey( 'author_text', $properties, 'Parent key should exist in properties.' );
$this->assertArrayHasKey( 'original_source', $properties, 'Parent key should exist in properties.' );
}

/**
Expand Down
Loading