Make WordPress Core

Changeset 57366


Ignore:
Timestamp:
01/27/2024 12:05:24 AM (3 months ago)
Author:
jorgefilipecosta
Message:

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.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

    r56819 r57366  
    727727        }
    728728
     729        if ( rest_is_field_included( 'author_text', $fields ) ) {
     730            $data['author_text'] = self::get_wp_templates_author_text_field( $template );
     731        }
     732
     733        if ( rest_is_field_included( 'original_source', $fields ) ) {
     734            $data['original_source'] = self::get_wp_templates_original_source_field( $template );
     735        }
     736
    729737        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
    730738        $data    = $this->add_additional_fields_to_object( $data, $request );
     
    747755
    748756        return $response;
     757    }
     758
     759    /**
     760     * Returns the source from where the template originally comes from.
     761     *
     762     * @access private
     763     * @internal
     764     *
     765     * @param WP_Block_Template $template_object Template instance.
     766     * @return string                            Original source of the template one of theme, plugin, site, or user.
     767     */
     768    private static function get_wp_templates_original_source_field( $template_object ) {
     769        if ( 'wp_template' === $template_object->type || 'wp_template_part' === $template_object->type ) {
     770            // Added by theme.
     771            // Template originally provided by a theme, but customized by a user.
     772            // Templates originally didn't have the 'origin' field so identify
     773            // older customized templates by checking for no origin and a 'theme'
     774            // or 'custom' source.
     775            if ( $template_object->has_theme_file &&
     776            ( 'theme' === $template_object->origin || (
     777                empty( $template_object->origin ) && in_array(
     778                    $template_object->source,
     779                    array(
     780                        'theme',
     781                        'custom',
     782                    ),
     783                    true
     784                ) )
     785            )
     786            ) {
     787                return 'theme';
     788            }
     789
     790            // Added by plugin.
     791            if ( $template_object->has_theme_file && 'plugin' === $template_object->origin ) {
     792                return 'plugin';
     793            }
     794
     795            // Added by site.
     796            // Template was created from scratch, but has no author. Author support
     797            // was only added to templates in WordPress 5.9. Fallback to showing the
     798            // site logo and title.
     799            if ( empty( $template_object->has_theme_file ) && 'custom' === $template_object->source && empty( $template_object->author ) ) {
     800                return 'site';
     801            }
     802        }
     803
     804        // Added by user.
     805        return 'user';
     806    }
     807
     808    /**
     809     * Returns a human readable text for the author of the template.
     810     *
     811     * @access private
     812     * @internal
     813     *
     814     * @param WP_Block_Template $template_object Template instance.
     815     * @return string                            Human readable text for the author.
     816     */
     817    private static function get_wp_templates_author_text_field( $template_object ) {
     818        $original_source = self::get_wp_templates_original_source_field( $template_object );
     819        switch ( $original_source ) {
     820            case 'theme':
     821                $theme_name = wp_get_theme( $template_object->theme )->get( 'Name' );
     822                return empty( $theme_name ) ? $template_object->theme : $theme_name;
     823            case 'plugin':
     824                $plugins = get_plugins();
     825                $plugin  = $plugins[ plugin_basename( sanitize_text_field( $template_object->theme . '.php' ) ) ];
     826                return empty( $plugin['Name'] ) ? $template_object->theme : $plugin['Name'];
     827            case 'site':
     828                return get_bloginfo( 'name' );
     829            case 'user':
     830                $author = get_user_by( 'id', $template_object->author );
     831                if ( ! $author ) {
     832                    return __( 'Unknown author' );
     833                }
     834                return $author->get( 'display_name' );
     835        }
    749836    }
    750837
     
    862949            'type'       => 'object',
    863950            'properties' => array(
    864                 'id'             => array(
     951                'id'              => array(
    865952                    'description' => __( 'ID of template.' ),
    866953                    'type'        => 'string',
     
    868955                    'readonly'    => true,
    869956                ),
    870                 'slug'           => array(
     957                'slug'            => array(
    871958                    'description' => __( 'Unique slug identifying the template.' ),
    872959                    'type'        => 'string',
     
    876963                    'pattern'     => '[a-zA-Z0-9_\%-]+',
    877964                ),
    878                 'theme'          => array(
     965                'theme'           => array(
    879966                    'description' => __( 'Theme identifier for the template.' ),
    880967                    'type'        => 'string',
    881968                    'context'     => array( 'embed', 'view', 'edit' ),
    882969                ),
    883                 'type'           => array(
     970                'type'            => array(
    884971                    'description' => __( 'Type of template.' ),
    885972                    'type'        => 'string',
    886973                    'context'     => array( 'embed', 'view', 'edit' ),
    887974                ),
    888                 'source'         => array(
     975                'source'          => array(
    889976                    'description' => __( 'Source of template' ),
    890977                    'type'        => 'string',
     
    892979                    'readonly'    => true,
    893980                ),
    894                 'origin'         => array(
     981                'origin'          => array(
    895982                    'description' => __( 'Source of a customized template' ),
    896983                    'type'        => 'string',
     
    898985                    'readonly'    => true,
    899986                ),
    900                 'content'        => array(
     987                'content'         => array(
    901988                    'description' => __( 'Content of template.' ),
    902989                    'type'        => array( 'object', 'string' ),
     
    9171004                    ),
    9181005                ),
    919                 'title'          => array(
     1006                'title'           => array(
    9201007                    'description' => __( 'Title of template.' ),
    9211008                    'type'        => array( 'object', 'string' ),
     
    9361023                    ),
    9371024                ),
    938                 'description'    => array(
     1025                'description'     => array(
    9391026                    'description' => __( 'Description of template.' ),
    9401027                    'type'        => 'string',
     
    9421029                    'context'     => array( 'embed', 'view', 'edit' ),
    9431030                ),
    944                 'status'         => array(
     1031                'status'          => array(
    9451032                    'description' => __( 'Status of template.' ),
    9461033                    'type'        => 'string',
     
    9491036                    'context'     => array( 'embed', 'view', 'edit' ),
    9501037                ),
    951                 'wp_id'          => array(
     1038                'wp_id'           => array(
    9521039                    'description' => __( 'Post ID.' ),
    9531040                    'type'        => 'integer',
     
    9551042                    'readonly'    => true,
    9561043                ),
    957                 'has_theme_file' => array(
     1044                'has_theme_file'  => array(
    9581045                    'description' => __( 'Theme file exists.' ),
    9591046                    'type'        => 'bool',
     
    9611048                    'readonly'    => true,
    9621049                ),
    963                 'author'         => array(
     1050                'author'          => array(
    9641051                    'description' => __( 'The ID for the author of the template.' ),
    9651052                    'type'        => 'integer',
    9661053                    'context'     => array( 'view', 'edit', 'embed' ),
    9671054                ),
    968                 'modified'       => array(
     1055                'modified'        => array(
    9691056                    'description' => __( "The date the template was last modified, in the site's timezone." ),
    9701057                    'type'        => 'string',
     
    9721059                    'context'     => array( 'view', 'edit' ),
    9731060                    'readonly'    => true,
     1061                ),
     1062                'author_text'     => array(
     1063                    'type'        => 'string',
     1064                    'description' => __( 'Human readable text for the author.' ),
     1065                    'readonly'    => true,
     1066                    'context'     => array( 'view', 'edit', 'embed' ),
     1067                ),
     1068                'original_source' => array(
     1069                    'description' => __( 'Where the template originally comes from e.g. \'theme\'' ),
     1070                    'type'        => 'string',
     1071                    'readonly'    => true,
     1072                    'context'     => array( 'view', 'edit', 'embed' ),
     1073                    'enum'        => array(
     1074                        'theme',
     1075                        'plugin',
     1076                        'site',
     1077                        'user',
     1078                    ),
    9741079                ),
    9751080            ),
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController.php

    r56819 r57366  
    311311        $properties = $data['schema']['properties'];
    312312
    313         $this->assertCount( 16, $properties );
     313        $this->assertCount( 18, $properties );
    314314        $this->assertArrayHasKey( 'id', $properties, 'ID key should exist in properties.' );
    315315        $this->assertArrayHasKey( 'slug', $properties, 'Slug key should exist in properties.' );
     
    327327        $this->assertArrayHasKey( 'is_custom', $properties, 'is_custom key should exist in properties.' );
    328328        $this->assertArrayHasKey( 'parent', $properties, 'Parent key should exist in properties.' );
     329        $this->assertArrayHasKey( 'author_text', $properties, 'Parent key should exist in properties.' );
     330        $this->assertArrayHasKey( 'original_source', $properties, 'Parent key should exist in properties.' );
    329331    }
    330332
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php

    r57268 r57366  
    450450        $properties = $data['schema']['properties'];
    451451
    452         $this->assertCount( 16, $properties );
     452        $this->assertCount( 18, $properties );
    453453        $this->assertArrayHasKey( 'id', $properties, 'ID key should exist in properties.' );
    454454        $this->assertArrayHasKey( 'slug', $properties, 'Slug key should exist in properties.' );
     
    466466        $this->assertArrayHasKey( 'is_custom', $properties, 'is_custom key should exist in properties.' );
    467467        $this->assertArrayHasKey( 'parent', $properties, 'Parent key should exist in properties.' );
     468        $this->assertArrayHasKey( 'author_text', $properties, 'Parent key should exist in properties.' );
     469        $this->assertArrayHasKey( 'original_source', $properties, 'Parent key should exist in properties.' );
    468470    }
    469471
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php

    r56421 r57366  
    103103        $this->assertSame(
    104104            array(
    105                 'id'             => 'default//my_template',
    106                 'theme'          => 'default',
    107                 'slug'           => 'my_template',
    108                 'source'         => 'custom',
    109                 'origin'         => null,
    110                 'type'           => 'wp_template',
    111                 'description'    => 'Description of my template.',
    112                 'title'          => array(
     105                'id'              => 'default//my_template',
     106                'theme'           => 'default',
     107                'slug'            => 'my_template',
     108                'source'          => 'custom',
     109                'origin'          => null,
     110                'type'            => 'wp_template',
     111                'description'     => 'Description of my template.',
     112                'title'           => array(
    113113                    'raw'      => 'My Template',
    114114                    'rendered' => 'My Template',
    115115                ),
    116                 'status'         => 'publish',
    117                 'wp_id'          => self::$post->ID,
    118                 'has_theme_file' => false,
    119                 'is_custom'      => true,
    120                 'author'         => 0,
    121                 'modified'       => mysql_to_rfc3339( self::$post->post_modified ),
     116                'status'          => 'publish',
     117                'wp_id'           => self::$post->ID,
     118                'has_theme_file'  => false,
     119                'is_custom'       => true,
     120                'author'          => 0,
     121                'modified'        => mysql_to_rfc3339( self::$post->post_modified ),
     122                'author_text'     => 'Test Blog',
     123                'original_source' => 'site',
    122124            ),
    123125            $this->find_and_normalize_template_by_id( $data, 'default//my_template' )
     
    148150        $this->assertSame(
    149151            array(
    150                 'id'             => 'default//my_template',
    151                 'theme'          => 'default',
    152                 'slug'           => 'my_template',
    153                 'source'         => 'custom',
    154                 'origin'         => null,
    155                 'type'           => 'wp_template',
    156                 'description'    => 'Description of my template.',
    157                 'title'          => array(
     152                'id'              => 'default//my_template',
     153                'theme'           => 'default',
     154                'slug'            => 'my_template',
     155                'source'          => 'custom',
     156                'origin'          => null,
     157                'type'            => 'wp_template',
     158                'description'     => 'Description of my template.',
     159                'title'           => array(
    158160                    'raw'      => 'My Template',
    159161                    'rendered' => 'My Template',
    160162                ),
    161                 'status'         => 'publish',
    162                 'wp_id'          => self::$post->ID,
    163                 'has_theme_file' => false,
    164                 'is_custom'      => true,
    165                 'author'         => 0,
    166                 'modified'       => mysql_to_rfc3339( self::$post->post_modified ),
     163                'status'          => 'publish',
     164                'wp_id'           => self::$post->ID,
     165                'has_theme_file'  => false,
     166                'is_custom'       => true,
     167                'author'          => 0,
     168                'modified'        => mysql_to_rfc3339( self::$post->post_modified ),
     169                'author_text'     => 'Test Blog',
     170                'original_source' => 'site',
    167171            ),
    168172            $data
     
    185189        $this->assertSame(
    186190            array(
    187                 'id'             => 'default//my_template',
    188                 'theme'          => 'default',
    189                 'slug'           => 'my_template',
    190                 'source'         => 'custom',
    191                 'origin'         => null,
    192                 'type'           => 'wp_template',
    193                 'description'    => 'Description of my template.',
    194                 'title'          => array(
     191                'id'              => 'default//my_template',
     192                'theme'           => 'default',
     193                'slug'            => 'my_template',
     194                'source'          => 'custom',
     195                'origin'          => null,
     196                'type'            => 'wp_template',
     197                'description'     => 'Description of my template.',
     198                'title'           => array(
    195199                    'raw'      => 'My Template',
    196200                    'rendered' => 'My Template',
    197201                ),
    198                 'status'         => 'publish',
    199                 'wp_id'          => self::$post->ID,
    200                 'has_theme_file' => false,
    201                 'is_custom'      => true,
    202                 'author'         => 0,
    203                 'modified'       => mysql_to_rfc3339( self::$post->post_modified ),
     202                'status'          => 'publish',
     203                'wp_id'           => self::$post->ID,
     204                'has_theme_file'  => false,
     205                'is_custom'       => true,
     206                'author'          => 0,
     207                'modified'        => mysql_to_rfc3339( self::$post->post_modified ),
     208                'author_text'     => 'Test Blog',
     209                'original_source' => 'site',
    204210            ),
    205211            $data
     
    242248        unset( $data['content'] );
    243249        unset( $data['_links'] );
     250        $author_name = get_user_by( 'id', self::$admin_id )->get( 'display_name' );
    244251
    245252        $this->assertSameSetsWithIndex(
    246253            array(
    247                 'id'             => "{$theme_dir}//{$template}",
    248                 'theme'          => $theme_dir,
    249                 'slug'           => $template,
    250                 'source'         => 'custom',
    251                 'origin'         => null,
    252                 'type'           => 'wp_template',
    253                 'description'    => $args['post_excerpt'],
    254                 'title'          => array(
     254                'id'              => "{$theme_dir}//{$template}",
     255                'theme'           => $theme_dir,
     256                'slug'            => $template,
     257                'source'          => 'custom',
     258                'origin'          => null,
     259                'type'            => 'wp_template',
     260                'description'     => $args['post_excerpt'],
     261                'title'           => array(
    255262                    'raw'      => $args['post_title'],
    256263                    'rendered' => $args['post_title'],
    257264                ),
    258                 'status'         => 'publish',
    259                 'wp_id'          => $post->ID,
    260                 'has_theme_file' => false,
    261                 'is_custom'      => true,
    262                 'author'         => self::$admin_id,
    263                 'modified'       => mysql_to_rfc3339( $post->post_modified ),
     265                'status'          => 'publish',
     266                'wp_id'           => $post->ID,
     267                'has_theme_file'  => false,
     268                'is_custom'       => true,
     269                'author'          => self::$admin_id,
     270                'modified'        => mysql_to_rfc3339( $post->post_modified ),
     271                'author_text'     => $author_name,
     272                'original_source' => 'user',
    264273            ),
    265274            $data
     
    422431        unset( $data['wp_id'] );
    423432
     433        $author_name = get_user_by( 'id', self::$admin_id )->get( 'display_name' );
     434
    424435        $this->assertSame(
    425436            array(
    426                 'id'             => 'default//my_custom_template',
    427                 'theme'          => 'default',
    428                 'content'        => array(
     437                'id'              => 'default//my_custom_template',
     438                'theme'           => 'default',
     439                'content'         => array(
    429440                    'raw' => 'Content',
    430441                ),
    431                 'slug'           => 'my_custom_template',
    432                 'source'         => 'custom',
    433                 'origin'         => null,
    434                 'type'           => 'wp_template',
    435                 'description'    => 'Just a description',
    436                 'title'          => array(
     442                'slug'            => 'my_custom_template',
     443                'source'          => 'custom',
     444                'origin'          => null,
     445                'type'            => 'wp_template',
     446                'description'     => 'Just a description',
     447                'title'           => array(
    437448                    'raw'      => 'My Template',
    438449                    'rendered' => 'My Template',
    439450                ),
    440                 'status'         => 'publish',
    441                 'has_theme_file' => false,
    442                 'is_custom'      => true,
    443                 'author'         => self::$admin_id,
    444                 'modified'       => mysql_to_rfc3339( $modified ),
     451                'status'          => 'publish',
     452                'has_theme_file'  => false,
     453                'is_custom'       => true,
     454                'author'          => self::$admin_id,
     455                'modified'        => mysql_to_rfc3339( $modified ),
     456                'author_text'     => $author_name,
     457                'original_source' => 'user',
    445458            ),
    446459            $data
     
    470483        unset( $data['wp_id'] );
    471484
     485        $author_name = get_user_by( 'id', self::$admin_id )->get( 'display_name' );
     486
    472487        $this->assertSame(
    473488            array(
    474                 'id'             => 'default//404',
    475                 'theme'          => 'default',
    476                 'content'        => array(
     489                'id'              => 'default//404',
     490                'theme'           => 'default',
     491                'content'         => array(
    477492                    'raw' => '',
    478493                ),
    479                 'slug'           => '404',
    480                 'source'         => 'custom',
    481                 'origin'         => null,
    482                 'type'           => 'wp_template',
    483                 'description'    => 'Template shown when no content is found.',
    484                 'title'          => array(
     494                'slug'            => '404',
     495                'source'          => 'custom',
     496                'origin'          => null,
     497                'type'            => 'wp_template',
     498                'description'     => 'Template shown when no content is found.',
     499                'title'           => array(
    485500                    'raw'      => '404',
    486501                    'rendered' => '404',
    487502                ),
    488                 'status'         => 'publish',
    489                 'has_theme_file' => false,
    490                 'is_custom'      => false,
    491                 'author'         => self::$admin_id,
    492                 'modified'       => mysql_to_rfc3339( $modified ),
     503                'status'          => 'publish',
     504                'has_theme_file'  => false,
     505                'is_custom'       => false,
     506                'author'          => self::$admin_id,
     507                'modified'        => mysql_to_rfc3339( $modified ),
     508                'author_text'     => $author_name,
     509                'original_source' => 'user',
    493510            ),
    494511            $data
     
    522539        unset( $data['wp_id'] );
    523540
     541        $author_name = get_user_by( 'id', self::$admin_id )->get( 'display_name' );
     542
    524543        $this->assertSame(
    525544            array(
    526                 'id'             => 'default//my_custom_template_raw',
    527                 'theme'          => 'default',
    528                 'content'        => array(
     545                'id'              => 'default//my_custom_template_raw',
     546                'theme'           => 'default',
     547                'content'         => array(
    529548                    'raw' => 'Content',
    530549                ),
    531                 'slug'           => 'my_custom_template_raw',
    532                 'source'         => 'custom',
    533                 'origin'         => null,
    534                 'type'           => 'wp_template',
    535                 'description'    => 'Just a description',
    536                 'title'          => array(
     550                'slug'            => 'my_custom_template_raw',
     551                'source'          => 'custom',
     552                'origin'          => null,
     553                'type'            => 'wp_template',
     554                'description'     => 'Just a description',
     555                'title'           => array(
    537556                    'raw'      => 'My Template',
    538557                    'rendered' => 'My Template',
    539558                ),
    540                 'status'         => 'publish',
    541                 'has_theme_file' => false,
    542                 'is_custom'      => true,
    543                 'author'         => self::$admin_id,
    544                 'modified'       => mysql_to_rfc3339( $modified ),
     559                'status'          => 'publish',
     560                'has_theme_file'  => false,
     561                'is_custom'       => true,
     562                'author'          => self::$admin_id,
     563                'modified'        => mysql_to_rfc3339( $modified ),
     564                'author_text'     => $author_name,
     565                'original_source' => 'user',
    545566            ),
    546567            $data
     
    701722        $data       = $response->get_data();
    702723        $properties = $data['schema']['properties'];
    703         $this->assertCount( 15, $properties );
     724        $this->assertCount( 17, $properties );
    704725        $this->assertArrayHasKey( 'id', $properties );
    705726        $this->assertArrayHasKey( 'description', $properties );
     
    718739        $this->assertArrayHasKey( 'author', $properties );
    719740        $this->assertArrayHasKey( 'modified', $properties );
     741        $this->assertArrayHasKey( 'author_text', $properties );
     742        $this->assertArrayHasKey( 'original_source', $properties );
    720743    }
    721744
     
    748771        $request = new WP_REST_Request( 'POST', '/wp/v2/templates' );
    749772        $request->set_body_params( $body_params );
    750         $response             = rest_get_server()->dispatch( $request );
    751         $data                 = $response->get_data();
    752         $modified             = get_post( $data['wp_id'] )->post_modified;
    753         $expected['modified'] = mysql_to_rfc3339( $modified );
     773        $response                    = rest_get_server()->dispatch( $request );
     774        $data                        = $response->get_data();
     775        $modified                    = get_post( $data['wp_id'] )->post_modified;
     776        $expected['modified']        = mysql_to_rfc3339( $modified );
     777        $expected['author_text']     = get_user_by( 'id', self::$admin_id )->get( 'display_name' );
     778        $expected['original_source'] = 'user';
     779
    754780        unset( $data['_links'] );
    755781        unset( $data['wp_id'] );
Note: See TracChangeset for help on using the changeset viewer.