Skip to content

Commit 5564c70

Browse files
committed
REST API: Add additional default template data fields for the active theme.
The active theme(s) now return two additional properties, `default_template_types` and `default_template_part_areas`, in the REST response. Props mamaduka, joemcgill, timothyblynjacobs, audrasjb, gigitux, peterwilsoncc, youknowriad, jorbin. Fixes #62574. git-svn-id: https://develop.svn.wordpress.org/trunk@59965 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 07e3b25 commit 5564c70

2 files changed

Lines changed: 109 additions & 19 deletions

File tree

src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,19 @@ public function prepare_item_for_response( $item, $request ) {
348348
}
349349
}
350350

351+
if ( rest_is_field_included( 'default_template_types', $fields ) && $this->is_same_theme( $theme, $current_theme ) ) {
352+
$default_template_types = array();
353+
foreach ( get_default_block_template_types() as $slug => $template_type ) {
354+
$template_type['slug'] = (string) $slug;
355+
$default_template_types[] = $template_type;
356+
}
357+
$data['default_template_types'] = $default_template_types;
358+
}
359+
360+
if ( rest_is_field_included( 'default_template_part_areas', $fields ) && $this->is_same_theme( $theme, $current_theme ) ) {
361+
$data['default_template_part_areas'] = get_allowed_block_template_part_areas();
362+
}
363+
351364
$data = $this->add_additional_fields_to_object( $data, $request );
352365

353366
// Wrap the data in a response object.
@@ -459,29 +472,29 @@ public function get_item_schema() {
459472
'title' => 'theme',
460473
'type' => 'object',
461474
'properties' => array(
462-
'stylesheet' => array(
475+
'stylesheet' => array(
463476
'description' => __( 'The theme\'s stylesheet. This uniquely identifies the theme.' ),
464477
'type' => 'string',
465478
'readonly' => true,
466479
),
467-
'stylesheet_uri' => array(
480+
'stylesheet_uri' => array(
468481
'description' => __( 'The uri for the theme\'s stylesheet directory.' ),
469482
'type' => 'string',
470483
'format' => 'uri',
471484
'readonly' => true,
472485
),
473-
'template' => array(
486+
'template' => array(
474487
'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.' ),
475488
'type' => 'string',
476489
'readonly' => true,
477490
),
478-
'template_uri' => array(
491+
'template_uri' => array(
479492
'description' => __( 'The uri for the theme\'s template directory. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme\'s stylesheet directory.' ),
480493
'type' => 'string',
481494
'format' => 'uri',
482495
'readonly' => true,
483496
),
484-
'author' => array(
497+
'author' => array(
485498
'description' => __( 'The theme author.' ),
486499
'type' => 'object',
487500
'readonly' => true,
@@ -496,7 +509,7 @@ public function get_item_schema() {
496509
),
497510
),
498511
),
499-
'author_uri' => array(
512+
'author_uri' => array(
500513
'description' => __( 'The website of the theme author.' ),
501514
'type' => 'object',
502515
'readonly' => true,
@@ -513,7 +526,7 @@ public function get_item_schema() {
513526
),
514527
),
515528
),
516-
'description' => array(
529+
'description' => array(
517530
'description' => __( 'A description of the theme.' ),
518531
'type' => 'object',
519532
'readonly' => true,
@@ -528,12 +541,12 @@ public function get_item_schema() {
528541
),
529542
),
530543
),
531-
'is_block_theme' => array(
544+
'is_block_theme' => array(
532545
'description' => __( 'Whether the theme is a block-based theme.' ),
533546
'type' => 'boolean',
534547
'readonly' => true,
535548
),
536-
'name' => array(
549+
'name' => array(
537550
'description' => __( 'The name of the theme.' ),
538551
'type' => 'object',
539552
'readonly' => true,
@@ -548,23 +561,23 @@ public function get_item_schema() {
548561
),
549562
),
550563
),
551-
'requires_php' => array(
564+
'requires_php' => array(
552565
'description' => __( 'The minimum PHP version required for the theme to work.' ),
553566
'type' => 'string',
554567
'readonly' => true,
555568
),
556-
'requires_wp' => array(
569+
'requires_wp' => array(
557570
'description' => __( 'The minimum WordPress version required for the theme to work.' ),
558571
'type' => 'string',
559572
'readonly' => true,
560573
),
561-
'screenshot' => array(
574+
'screenshot' => array(
562575
'description' => __( 'The theme\'s screenshot URL.' ),
563576
'type' => 'string',
564577
'format' => 'uri',
565578
'readonly' => true,
566579
),
567-
'tags' => array(
580+
'tags' => array(
568581
'description' => __( 'Tags indicating styles and features of the theme.' ),
569582
'type' => 'object',
570583
'readonly' => true,
@@ -582,18 +595,18 @@ public function get_item_schema() {
582595
),
583596
),
584597
),
585-
'textdomain' => array(
598+
'textdomain' => array(
586599
'description' => __( 'The theme\'s text domain.' ),
587600
'type' => 'string',
588601
'readonly' => true,
589602
),
590-
'theme_supports' => array(
603+
'theme_supports' => array(
591604
'description' => __( 'Features supported by this theme.' ),
592605
'type' => 'object',
593606
'readonly' => true,
594607
'properties' => array(),
595608
),
596-
'theme_uri' => array(
609+
'theme_uri' => array(
597610
'description' => __( 'The URI of the theme\'s webpage.' ),
598611
'type' => 'object',
599612
'readonly' => true,
@@ -610,16 +623,60 @@ public function get_item_schema() {
610623
),
611624
),
612625
),
613-
'version' => array(
626+
'version' => array(
614627
'description' => __( 'The theme\'s current version.' ),
615628
'type' => 'string',
616629
'readonly' => true,
617630
),
618-
'status' => array(
631+
'status' => array(
619632
'description' => __( 'A named status for the theme.' ),
620633
'type' => 'string',
621634
'enum' => array( 'inactive', 'active' ),
622635
),
636+
'default_template_types' => array(
637+
'description' => __( 'A list of default template types.' ),
638+
'type' => 'array',
639+
'readonly' => true,
640+
'items' => array(
641+
'type' => 'object',
642+
'properties' => array(
643+
'slug' => array(
644+
'type' => 'string',
645+
),
646+
'title' => array(
647+
'type' => 'string',
648+
),
649+
'description' => array(
650+
'type' => 'string',
651+
),
652+
),
653+
),
654+
),
655+
'default_template_part_areas' => array(
656+
'description' => __( 'A list of allowed area values for template parts.' ),
657+
'type' => 'array',
658+
'readonly' => true,
659+
'items' => array(
660+
'type' => 'object',
661+
'properties' => array(
662+
'area' => array(
663+
'type' => 'string',
664+
),
665+
'label' => array(
666+
'type' => 'string',
667+
),
668+
'description' => array(
669+
'type' => 'string',
670+
),
671+
'icon' => array(
672+
'type' => 'string',
673+
),
674+
'area_tag' => array(
675+
'type' => 'string',
676+
),
677+
),
678+
),
679+
),
623680
),
624681
);
625682

tests/phpunit/tests/rest-api/rest-themes-controller.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public function test_register_routes() {
163163
*
164164
* @ticket 45016
165165
* @ticket 61021
166+
* @ticket 62574.
166167
*/
167168
public function test_get_items() {
168169
$response = self::perform_active_theme_request();
@@ -175,6 +176,8 @@ public function test_get_items() {
175176
'_links',
176177
'author',
177178
'author_uri',
179+
'default_template_part_areas',
180+
'default_template_types',
178181
'description',
179182
'is_block_theme',
180183
'name',
@@ -354,12 +357,13 @@ public function test_prepare_item() {
354357
*
355358
* @ticket 45016
356359
* @ticket 61021
360+
* @ticket 62574
357361
*/
358362
public function test_get_item_schema() {
359363
$response = self::perform_active_theme_request( 'OPTIONS' );
360364
$data = $response->get_data();
361365
$properties = $data['schema']['properties'];
362-
$this->assertCount( 18, $properties );
366+
$this->assertCount( 20, $properties );
363367

364368
$this->assertArrayHasKey( 'author', $properties );
365369
$this->assertArrayHasKey( 'raw', $properties['author']['properties'] );
@@ -373,6 +377,9 @@ public function test_get_item_schema() {
373377
$this->assertArrayHasKey( 'raw', $properties['description']['properties'] );
374378
$this->assertArrayHasKey( 'rendered', $properties['description']['properties'] );
375379

380+
$this->assertArrayHasKey( 'default_template_part_areas', $properties );
381+
$this->assertArrayHasKey( 'default_template_types', $properties );
382+
376383
$this->assertArrayHasKey( 'is_block_theme', $properties );
377384

378385
$this->assertArrayHasKey( 'name', $properties );
@@ -472,6 +479,32 @@ public function test_theme_description() {
472479
);
473480
}
474481

482+
/**
483+
* @ticket 62574
484+
*/
485+
public function test_theme_default_template_part_areas() {
486+
$response = self::perform_active_theme_request();
487+
$result = $response->get_data();
488+
$this->assertArrayHasKey( 'default_template_part_areas', $result[0] );
489+
$this->assertSame( get_allowed_block_template_part_areas(), $result[0]['default_template_part_areas'] );
490+
}
491+
492+
/**
493+
* @ticket 62574
494+
*/
495+
public function test_theme_default_template_types() {
496+
$response = self::perform_active_theme_request();
497+
$result = $response->get_data();
498+
$expected = array();
499+
foreach ( get_default_block_template_types() as $slug => $template_type ) {
500+
$template_type['slug'] = (string) $slug;
501+
$expected[] = $template_type;
502+
}
503+
504+
$this->assertArrayHasKey( 'default_template_types', $result[0] );
505+
$this->assertSame( $expected, $result[0]['default_template_types'] );
506+
}
507+
475508
/**
476509
* @ticket 49906
477510
*/

0 commit comments

Comments
 (0)