Make WordPress Core


Ignore:
Timestamp:
06/07/2020 06:44:08 AM (6 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Add additional fields to the themes controller.

When the themes controller was introduced it only returned a theme's supported features. This adds the majority of a theme's header information to the response.

Props ockham, spacedmonkey.
Fixes #49906.

File:
1 edited

Legend:

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

    r47432 r47921  
    116116        $fields = $this->get_fields_for_response( $request );
    117117
    118         if ( in_array( 'theme_supports', $fields, true ) ) {
     118        if ( rest_is_field_included( 'stylesheet', $fields ) ) {
     119            $data['stylesheet'] = $theme->get_stylesheet();
     120        }
     121
     122        if ( rest_is_field_included( 'template', $fields ) ) {
     123            /**
     124             * Use the get_template() method, not the 'Template' header, for finding the template.
     125             * The 'Template' header is only good for what was written in the style.css, while
     126             * get_template() takes into account where WordPress actually located the theme and
     127             * whether it is actually valid.
     128             */
     129            $data['template'] = $theme->get_template();
     130        }
     131
     132        $plain_field_mappings = array(
     133            'requires_php' => 'RequiresPHP',
     134            'requires_wp'  => 'RequiresWP',
     135            'textdomain'   => 'TextDomain',
     136            'version'      => 'Version',
     137        );
     138
     139        foreach ( $plain_field_mappings as $field => $header ) {
     140            if ( rest_is_field_included( $field, $fields ) ) {
     141                $data[ $field ] = $theme->get( $header );
     142            }
     143        }
     144
     145        if ( rest_is_field_included( 'screenshot', $fields ) ) {
     146            // Using $theme->get_screenshot() with no args to get absolute URL.
     147            $data['screenshot'] = $theme->get_screenshot() ?: '';
     148        }
     149
     150        $rich_field_mappings = array(
     151            'author'      => 'Author',
     152            'author_uri'  => 'AuthorURI',
     153            'description' => 'Description',
     154            'name'        => 'Name',
     155            'tags'        => 'Tags',
     156            'theme_uri'   => 'ThemeURI',
     157        );
     158
     159        foreach ( $rich_field_mappings as $field => $header ) {
     160            if ( rest_is_field_included( "{$field}.raw", $fields ) ) {
     161                $data[ $field ]['raw'] = $theme->display( $header, false, true );
     162            }
     163
     164            if ( rest_is_field_included( "{$field}.rendered", $fields ) ) {
     165                $data[ $field ]['rendered'] = $theme->display( $header );
     166            }
     167        }
     168
     169        if ( rest_is_field_included( 'theme_supports', $fields ) ) {
    119170            $item_schemas   = $this->get_item_schema();
    120171            $theme_supports = $item_schemas['properties']['theme_supports']['properties'];
    121172            foreach ( $theme_supports as $name => $schema ) {
     173                if ( ! rest_is_field_included( "theme_supports.{$name}", $fields ) ) {
     174                    continue;
     175                }
     176
    122177                if ( 'formats' === $name ) {
    123178                    continue;
     
    193248            'type'       => 'object',
    194249            'properties' => array(
     250                'stylesheet'     => array(
     251                    'description' => __( 'The theme\'s stylesheet. This uniquely identifies the theme.' ),
     252                    'type'        => 'string',
     253                    'readonly'    => true,
     254                ),
     255                'template'       => array(
     256                    'description' => __( 'The theme\'s template. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme\'s stylesheet.' ),
     257                    'type'        => 'string',
     258                    'readonly'    => true,
     259                ),
     260                'author'         => array(
     261                    'description' => __( 'The theme author.' ),
     262                    'type'        => 'object',
     263                    'readonly'    => true,
     264                    'properties'  => array(
     265                        'raw'      => array(
     266                            'description' => __( 'The theme author\'s name, as found in the theme header.' ),
     267                            'type'        => 'string',
     268                        ),
     269                        'rendered' => array(
     270                            'description' => __( 'HTML for the theme author, transformed for display.' ),
     271                            'type'        => 'string',
     272                        ),
     273                    ),
     274                ),
     275                'author_uri'     => array(
     276                    'description' => __( 'The website of the theme author.' ),
     277                    'type'        => 'object',
     278                    'readonly'    => true,
     279                    'properties'  => array(
     280                        'raw'      => array(
     281                            'description' => __( 'The website of the theme author, as found in the theme header.' ),
     282                            'type'        => 'string',
     283                            'format'      => 'uri',
     284                        ),
     285                        'rendered' => array(
     286                            'description' => __( 'The website of the theme author, transformed for display.' ),
     287                            'type'        => 'string',
     288                            'format'      => 'uri',
     289                        ),
     290                    ),
     291                ),
     292                'description'    => array(
     293                    'description' => __( 'A description of the theme.' ),
     294                    'type'        => 'object',
     295                    'readonly'    => true,
     296                    'properties'  => array(
     297                        'raw'      => array(
     298                            'description' => __( 'The theme description, as found in the theme header.' ),
     299                            'type'        => 'string',
     300                        ),
     301                        'rendered' => array(
     302                            'description' => __( 'The theme description, transformed for display.' ),
     303                            'type'        => 'string',
     304                        ),
     305                    ),
     306                ),
     307                'name'           => array(
     308                    'description' => __( 'The name of the theme.' ),
     309                    'type'        => 'object',
     310                    'readonly'    => true,
     311                    'properties'  => array(
     312                        'raw'      => array(
     313                            'description' => __( 'The theme name, as found in the theme header.' ),
     314                            'type'        => 'string',
     315                        ),
     316                        'rendered' => array(
     317                            'description' => __( 'The theme name, transformed for display.' ),
     318                            'type'        => 'string',
     319                        ),
     320                    ),
     321                ),
     322                'requires_php'   => array(
     323                    'description' => __( 'The minimum PHP version required for the theme to work.' ),
     324                    'type'        => 'string',
     325                    'readonly'    => true,
     326                ),
     327                'requires_wp'    => array(
     328                    'description' => __( 'The minimum WordPress version required for the theme to work.' ),
     329                    'type'        => 'string',
     330                    'readonly'    => true,
     331                ),
     332                'screenshot'     => array(
     333                    'description' => __( 'The theme\'s screenshot URL.' ),
     334                    'type'        => 'string',
     335                    'format'      => 'uri',
     336                    'readonly'    => true,
     337                ),
     338                'tags'           => array(
     339                    'description' => __( 'Tags indicating styles and features of the theme.' ),
     340                    'type'        => 'object',
     341                    'readonly'    => true,
     342                    'properties'  => array(
     343                        'raw'      => array(
     344                            'description' => __( 'The theme tags, as found in the theme header.' ),
     345                            'type'        => 'array',
     346                            'items'       => array(
     347                                'type' => 'string',
     348                            ),
     349                        ),
     350                        'rendered' => array(
     351                            'description' => __( 'The theme tags, transformed for display.' ),
     352                            'type'        => 'string',
     353                        ),
     354                    ),
     355                ),
     356                'textdomain'     => array(
     357                    'description' => __( 'The theme\'s textdomain.' ),
     358                    'type'        => 'string',
     359                    'readonly'    => true,
     360                ),
    195361                'theme_supports' => array(
    196362                    'description' => __( 'Features supported by this theme.' ),
     
    459625                    ),
    460626                ),
     627                'theme_uri'      => array(
     628                    'description' => __( 'The URI of the theme\'s webpage.' ),
     629                    'type'        => 'object',
     630                    'readonly'    => true,
     631                    'properties'  => array(
     632                        'raw'      => array(
     633                            'description' => __( 'The URI of the theme\'s webpage, as found in the theme header.' ),
     634                            'type'        => 'string',
     635                            'format'      => 'uri',
     636                        ),
     637                        'rendered' => array(
     638                            'description' => __( 'The URI of the theme\'s webpage, transformed for display.' ),
     639                            'type'        => 'string',
     640                            'format'      => 'uri',
     641                        ),
     642                    ),
     643                ),
     644                'version'        => array(
     645                    'description' => __( 'The theme\'s current version.' ),
     646                    'type'        => 'string',
     647                    'readonly'    => true,
     648                ),
    461649            ),
    462650        );
Note: See TracChangeset for help on using the changeset viewer.