Changeset 58282
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php
r56586 r58282 225 225 * @since 5.0.0 226 226 * @since 5.9.0 Renamed `$theme` to `$item` to match parent class for PHP 8 named parameter support. 227 * @since 6.6.0 Added `stylesheet_uri` and `template_uri` fields. 227 228 * 228 229 * @param WP_Theme $item Theme object. … … 330 331 if ( rest_is_field_included( 'is_block_theme', $fields ) ) { 331 332 $data['is_block_theme'] = $theme->is_block_theme(); 333 } 334 335 if ( rest_is_field_included( 'stylesheet_uri', $fields ) ) { 336 if ( $this->is_same_theme( $theme, $current_theme ) ) { 337 $data['stylesheet_uri'] = get_stylesheet_directory_uri(); 338 } else { 339 $data['stylesheet_uri'] = $theme->get_stylesheet_directory_uri(); 340 } 341 } 342 343 if ( rest_is_field_included( 'template_uri', $fields ) ) { 344 if ( $this->is_same_theme( $theme, $current_theme ) ) { 345 $data['template_uri'] = get_template_directory_uri(); 346 } else { 347 $data['template_uri'] = $theme->get_template_directory_uri(); 348 } 332 349 } 333 350 … … 448 465 'readonly' => true, 449 466 ), 467 'stylesheet_uri' => array( 468 'description' => __( 'The uri for the theme\'s stylesheet directory.' ), 469 'type' => 'string', 470 'format' => 'uri', 471 'readonly' => true, 472 ), 450 473 'template' => array( 451 474 '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.' ), 452 475 'type' => 'string', 476 'readonly' => true, 477 ), 478 'template_uri' => array( 479 '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.' ), 480 'type' => 'string', 481 'format' => 'uri', 453 482 'readonly' => true, 454 483 ), -
trunk/tests/phpunit/tests/rest-api/rest-themes-controller.php
r57987 r58282 163 163 * 164 164 * @ticket 45016 165 * @ticket 61021 165 166 */ 166 167 public function test_get_items() { … … 183 184 'status', 184 185 'stylesheet', 186 'stylesheet_uri', 185 187 'tags', 186 188 'template', 189 'template_uri', 187 190 'textdomain', 188 191 'theme_supports', … … 199 202 * 200 203 * @ticket 50152 204 * @ticket 61021 201 205 */ 202 206 public function test_get_items_inactive() { … … 222 226 'status', 223 227 'stylesheet', 228 'stylesheet_uri', 224 229 'tags', 225 230 'template', 231 'template_uri', 226 232 'textdomain', 227 233 'theme_uri', … … 348 354 * 349 355 * @ticket 45016 356 * @ticket 61021 350 357 */ 351 358 public function test_get_item_schema() { … … 353 360 $data = $response->get_data(); 354 361 $properties = $data['schema']['properties']; 355 $this->assertCount( 1 6, $properties );362 $this->assertCount( 18, $properties ); 356 363 357 364 $this->assertArrayHasKey( 'author', $properties ); … … 378 385 $this->assertArrayHasKey( 'status', $properties ); 379 386 $this->assertArrayHasKey( 'stylesheet', $properties ); 387 $this->assertArrayHasKey( 'stylesheet_uri', $properties ); 380 388 381 389 $this->assertArrayHasKey( 'tags', $properties ); … … 385 393 386 394 $this->assertArrayHasKey( 'template', $properties ); 395 $this->assertArrayHasKey( 'template_uri', $properties ); 387 396 $this->assertArrayHasKey( 'textdomain', $properties ); 388 397 $this->assertArrayHasKey( 'theme_supports', $properties ); … … 536 545 537 546 /** 547 * @ticket 61021 548 */ 549 public function test_theme_stylesheet_uri() { 550 wp_set_current_user( self::$admin_id ); 551 $request = new WP_REST_Request( 'GET', self::$themes_route ); 552 $request->set_param( 'status', array( 'active', 'inactive' ) ); 553 554 $response = rest_get_server()->dispatch( $request ); 555 $result = $response->get_data(); 556 $current_theme = wp_get_theme(); 557 558 foreach ( $result as $theme_result ) { 559 $this->assertArrayHasKey( 'stylesheet_uri', $theme_result ); 560 if ( 'active' === $theme_result['status'] ) { 561 $this->assertSame( 562 get_stylesheet_directory_uri(), 563 $theme_result['stylesheet_uri'], 564 'stylesheet_uri for an active theme should be the same as the global get_stylesheet_directory_uri()' 565 ); 566 } else { 567 $theme = wp_get_theme( $theme_result['stylesheet'] ); 568 $this->assertSame( 569 $theme->get_stylesheet_directory_uri(), 570 $theme_result['stylesheet_uri'], 571 "stylesheet_uri for an inactive theme should be the same as the theme's get_stylesheet_directory_uri() method" 572 ); 573 } 574 } 575 } 576 577 /** 538 578 * @ticket 49906 539 579 */ … … 554 594 $this->assertArrayHasKey( 'template', $result[0] ); 555 595 $this->assertSame( 'default', $result[0]['template'] ); 596 } 597 598 /** 599 * @ticket 61021 600 */ 601 public function test_theme_template_uri() { 602 wp_set_current_user( self::$admin_id ); 603 $request = new WP_REST_Request( 'GET', self::$themes_route ); 604 $request->set_param( 'status', array( 'active', 'inactive' ) ); 605 606 $response = rest_get_server()->dispatch( $request ); 607 $result = $response->get_data(); 608 $current_theme = wp_get_theme(); 609 610 foreach ( $result as $theme_result ) { 611 $this->assertArrayHasKey( 'template_uri', $theme_result ); 612 if ( 'active' === $theme_result['status'] ) { 613 $this->assertSame( 614 get_template_directory_uri(), 615 $theme_result['template_uri'], 616 'template_uri for an active theme should be the same as the global get_template_directory_uri()' 617 ); 618 } else { 619 $theme = wp_get_theme( $theme_result['stylesheet'] ); 620 $this->assertSame( 621 $theme->get_template_directory_uri(), 622 $theme_result['template_uri'], 623 "template_uri for an inactive theme should be the same as the theme's get_template_directory_uri() method" 624 ); 625 } 626 } 556 627 } 557 628 … … 1274 1345 'status', 1275 1346 'stylesheet', 1347 'stylesheet_uri', 1276 1348 'tags', 1277 1349 'template', 1350 'template_uri', 1278 1351 'textdomain', 1279 1352 'theme_uri',
Note: See TracChangeset
for help on using the changeset viewer.