Make WordPress Core


Ignore:
Timestamp:
01/27/2024 12:05:24 AM (3 years 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.

File:
1 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                        ),
Note: See TracChangeset for help on using the changeset viewer.