Make WordPress Core

Changeset 47921

Timestamp:
06/07/2020 06:44:08 AM (4 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.

Location:
trunk
Files:
2 added
3 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
     174
     175
     176
    122177                if ( 'formats' === $name ) {
    123178                    continue;
     
    193248            'type'       => 'object',
    194249            'properties' => array(
     250
     251
     252
     253
     254
     255
     256
     257
     258
     259
     260
     261
     262
     263
     264
     265
     266
     267
     268
     269
     270
     271
     272
     273
     274
     275
     276
     277
     278
     279
     280
     281
     282
     283
     284
     285
     286
     287
     288
     289
     290
     291
     292
     293
     294
     295
     296
     297
     298
     299
     300
     301
     302
     303
     304
     305
     306
     307
     308
     309
     310
     311
     312
     313
     314
     315
     316
     317
     318
     319
     320
     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
     332
     333
     334
     335
     336
     337
     338
     339
     340
     341
     342
     343
     344
     345
     346
     347
     348
     349
     350
     351
     352
     353
     354
     355
     356
     357
     358
     359
     360
    195361                'theme_supports' => array(
    196362                    'description' => __( 'Features supported by this theme.' ),
     
    459625                    ),
    460626                ),
     627
     628
     629
     630
     631
     632
     633
     634
     635
     636
     637
     638
     639
     640
     641
     642
     643
     644
     645
     646
     647
     648
    461649            ),
    462650        );
  • trunk/tests/phpunit/tests/rest-api/rest-themes-controller.php

    r47361 r47921  
    126126
    127127        wp_set_current_user( self::$contributor_id );
     128
    128129    }
    129130
     
    151152        $this->check_get_theme_response( $response );
    152153        $fields = array(
     154
     155
     156
     157
     158
     159
     160
     161
     162
     163
     164
    153165            'theme_supports',
     166
     167
    154168        );
    155169        $this->assertEqualSets( $fields, array_keys( $data[0] ) );
     
    208222        $data       = $response->get_data();
    209223        $properties = $data['schema']['properties'];
    210         $this->assertEquals( 1, count( $properties ) );
     224        $this->assertEquals( 14, count( $properties ) );
     225
     226        $this->assertArrayHasKey( 'author', $properties );
     227        $this->assertArrayHasKey( 'raw', $properties['author']['properties'] );
     228        $this->assertArrayHasKey( 'rendered', $properties['author']['properties'] );
     229
     230        $this->assertArrayHasKey( 'author_uri', $properties );
     231        $this->assertArrayHasKey( 'raw', $properties['author_uri']['properties'] );
     232        $this->assertArrayHasKey( 'rendered', $properties['author_uri']['properties'] );
     233
     234        $this->assertArrayHasKey( 'description', $properties );
     235        $this->assertArrayHasKey( 'raw', $properties['description']['properties'] );
     236        $this->assertArrayHasKey( 'rendered', $properties['description']['properties'] );
     237
     238        $this->assertArrayHasKey( 'name', $properties );
     239        $this->assertArrayHasKey( 'raw', $properties['name']['properties'] );
     240        $this->assertArrayHasKey( 'rendered', $properties['name']['properties'] );
     241
     242        $this->assertArrayHasKey( 'requires_php', $properties );
     243        $this->assertArrayHasKey( 'requires_wp', $properties );
     244        $this->assertArrayHasKey( 'screenshot', $properties );
     245        $this->assertArrayHasKey( 'stylesheet', $properties );
     246
     247        $this->assertArrayHasKey( 'tags', $properties );
     248        $this->assertArrayHasKey( 'raw', $properties['tags']['properties'] );
     249        $this->assertArrayHasKey( 'items', $properties['tags']['properties']['raw'] );
     250        $this->assertArrayHasKey( 'rendered', $properties['tags']['properties'] );
     251
     252        $this->assertArrayHasKey( 'template', $properties );
     253        $this->assertArrayHasKey( 'textdomain', $properties );
    211254        $this->assertArrayHasKey( 'theme_supports', $properties );
     255
     256
     257
     258
     259
     260
     261
    212262        $theme_supports = $properties['theme_supports']['properties'];
    213263        $this->assertEquals( 20, count( $theme_supports ) );
     
    235285
    236286    /**
     287
     288
     289
     290
     291
     292
     293
     294
     295
     296
     297
     298
     299
     300
     301
     302
     303
     304
     305
     306
     307
     308
     309
     310
     311
     312
     313
     314
     315
     316
     317
     318
     319
     320
     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
     332
     333
     334
     335
     336
     337
     338
     339
     340
     341
     342
     343
     344
     345
     346
     347
     348
     349
     350
     351
     352
     353
     354
     355
     356
     357
     358
     359
     360
     361
     362
     363
     364
     365
     366
     367
     368
     369
     370
     371
     372
     373
     374
     375
     376
     377
     378
     379
     380
     381
     382
     383
     384
     385
     386
     387
     388
     389
     390
     391
     392
     393
     394
     395
     396
     397
     398
     399
     400
     401
     402
     403
     404
     405
     406
     407
     408
     409
     410
     411
     412
     413
     414
     415
     416
     417
     418
     419
     420
     421
     422
     423
     424
     425
     426
     427
     428
    237429     * @ticket 49037
    238430     */
  • trunk/tests/phpunit/tests/theme/themeDir.php

    r47122 r47921  
    164164            'Internationalized Theme',
    165165            'camelCase',
     166
    166167        );
    167168
Note: See TracChangeset for help on using the changeset viewer.