Changeset 57366
- Timestamp:
- 01/27/2024 12:05:24 AM (15 months ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php
r56819 r57366 727 727 } 728 728 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 729 737 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 730 738 $data = $this->add_additional_fields_to_object( $data, $request ); … … 747 755 748 756 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 } 749 836 } 750 837 … … 862 949 'type' => 'object', 863 950 'properties' => array( 864 'id' => array(951 'id' => array( 865 952 'description' => __( 'ID of template.' ), 866 953 'type' => 'string', … … 868 955 'readonly' => true, 869 956 ), 870 'slug' => array(957 'slug' => array( 871 958 'description' => __( 'Unique slug identifying the template.' ), 872 959 'type' => 'string', … … 876 963 'pattern' => '[a-zA-Z0-9_\%-]+', 877 964 ), 878 'theme' => array(965 'theme' => array( 879 966 'description' => __( 'Theme identifier for the template.' ), 880 967 'type' => 'string', 881 968 'context' => array( 'embed', 'view', 'edit' ), 882 969 ), 883 'type' => array(970 'type' => array( 884 971 'description' => __( 'Type of template.' ), 885 972 'type' => 'string', 886 973 'context' => array( 'embed', 'view', 'edit' ), 887 974 ), 888 'source' => array(975 'source' => array( 889 976 'description' => __( 'Source of template' ), 890 977 'type' => 'string', … … 892 979 'readonly' => true, 893 980 ), 894 'origin' => array(981 'origin' => array( 895 982 'description' => __( 'Source of a customized template' ), 896 983 'type' => 'string', … … 898 985 'readonly' => true, 899 986 ), 900 'content' => array(987 'content' => array( 901 988 'description' => __( 'Content of template.' ), 902 989 'type' => array( 'object', 'string' ), … … 917 1004 ), 918 1005 ), 919 'title' => array(1006 'title' => array( 920 1007 'description' => __( 'Title of template.' ), 921 1008 'type' => array( 'object', 'string' ), … … 936 1023 ), 937 1024 ), 938 'description' => array(1025 'description' => array( 939 1026 'description' => __( 'Description of template.' ), 940 1027 'type' => 'string', … … 942 1029 'context' => array( 'embed', 'view', 'edit' ), 943 1030 ), 944 'status' => array(1031 'status' => array( 945 1032 'description' => __( 'Status of template.' ), 946 1033 'type' => 'string', … … 949 1036 'context' => array( 'embed', 'view', 'edit' ), 950 1037 ), 951 'wp_id' => array(1038 'wp_id' => array( 952 1039 'description' => __( 'Post ID.' ), 953 1040 'type' => 'integer', … … 955 1042 'readonly' => true, 956 1043 ), 957 'has_theme_file' => array(1044 'has_theme_file' => array( 958 1045 'description' => __( 'Theme file exists.' ), 959 1046 'type' => 'bool', … … 961 1048 'readonly' => true, 962 1049 ), 963 'author' => array(1050 'author' => array( 964 1051 'description' => __( 'The ID for the author of the template.' ), 965 1052 'type' => 'integer', 966 1053 'context' => array( 'view', 'edit', 'embed' ), 967 1054 ), 968 'modified' => array(1055 'modified' => array( 969 1056 'description' => __( "The date the template was last modified, in the site's timezone." ), 970 1057 'type' => 'string', … … 972 1059 'context' => array( 'view', 'edit' ), 973 1060 '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 ), 974 1079 ), 975 1080 ), -
trunk/tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController.php
r56819 r57366 311 311 $properties = $data['schema']['properties']; 312 312 313 $this->assertCount( 1 6, $properties );313 $this->assertCount( 18, $properties ); 314 314 $this->assertArrayHasKey( 'id', $properties, 'ID key should exist in properties.' ); 315 315 $this->assertArrayHasKey( 'slug', $properties, 'Slug key should exist in properties.' ); … … 327 327 $this->assertArrayHasKey( 'is_custom', $properties, 'is_custom key should exist in properties.' ); 328 328 $this->assertArrayHasKey( 'parent', $properties, 'Parent key should exist in properties.' ); 329 $this->assertArrayHasKey( 'author_text', $properties, 'Parent key should exist in properties.' ); 330 $this->assertArrayHasKey( 'original_source', $properties, 'Parent key should exist in properties.' ); 329 331 } 330 332 -
trunk/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php
r57268 r57366 450 450 $properties = $data['schema']['properties']; 451 451 452 $this->assertCount( 1 6, $properties );452 $this->assertCount( 18, $properties ); 453 453 $this->assertArrayHasKey( 'id', $properties, 'ID key should exist in properties.' ); 454 454 $this->assertArrayHasKey( 'slug', $properties, 'Slug key should exist in properties.' ); … … 466 466 $this->assertArrayHasKey( 'is_custom', $properties, 'is_custom key should exist in properties.' ); 467 467 $this->assertArrayHasKey( 'parent', $properties, 'Parent key should exist in properties.' ); 468 $this->assertArrayHasKey( 'author_text', $properties, 'Parent key should exist in properties.' ); 469 $this->assertArrayHasKey( 'original_source', $properties, 'Parent key should exist in properties.' ); 468 470 } 469 471 -
trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php
r56421 r57366 103 103 $this->assertSame( 104 104 array( 105 'id' => 'default//my_template',106 'theme' => 'default',107 'slug' => 'my_template',108 'source' => 'custom',109 'origin' => null,110 'type' => 'wp_template',111 'description' => 'Description of my template.',112 'title' => array(105 'id' => 'default//my_template', 106 'theme' => 'default', 107 'slug' => 'my_template', 108 'source' => 'custom', 109 'origin' => null, 110 'type' => 'wp_template', 111 'description' => 'Description of my template.', 112 'title' => array( 113 113 'raw' => 'My Template', 114 114 'rendered' => 'My Template', 115 115 ), 116 'status' => 'publish', 117 'wp_id' => self::$post->ID, 118 'has_theme_file' => false, 119 'is_custom' => true, 120 'author' => 0, 121 'modified' => mysql_to_rfc3339( self::$post->post_modified ), 116 'status' => 'publish', 117 'wp_id' => self::$post->ID, 118 'has_theme_file' => false, 119 'is_custom' => true, 120 'author' => 0, 121 'modified' => mysql_to_rfc3339( self::$post->post_modified ), 122 'author_text' => 'Test Blog', 123 'original_source' => 'site', 122 124 ), 123 125 $this->find_and_normalize_template_by_id( $data, 'default//my_template' ) … … 148 150 $this->assertSame( 149 151 array( 150 'id' => 'default//my_template',151 'theme' => 'default',152 'slug' => 'my_template',153 'source' => 'custom',154 'origin' => null,155 'type' => 'wp_template',156 'description' => 'Description of my template.',157 'title' => array(152 'id' => 'default//my_template', 153 'theme' => 'default', 154 'slug' => 'my_template', 155 'source' => 'custom', 156 'origin' => null, 157 'type' => 'wp_template', 158 'description' => 'Description of my template.', 159 'title' => array( 158 160 'raw' => 'My Template', 159 161 'rendered' => 'My Template', 160 162 ), 161 'status' => 'publish', 162 'wp_id' => self::$post->ID, 163 'has_theme_file' => false, 164 'is_custom' => true, 165 'author' => 0, 166 'modified' => mysql_to_rfc3339( self::$post->post_modified ), 163 'status' => 'publish', 164 'wp_id' => self::$post->ID, 165 'has_theme_file' => false, 166 'is_custom' => true, 167 'author' => 0, 168 'modified' => mysql_to_rfc3339( self::$post->post_modified ), 169 'author_text' => 'Test Blog', 170 'original_source' => 'site', 167 171 ), 168 172 $data … … 185 189 $this->assertSame( 186 190 array( 187 'id' => 'default//my_template',188 'theme' => 'default',189 'slug' => 'my_template',190 'source' => 'custom',191 'origin' => null,192 'type' => 'wp_template',193 'description' => 'Description of my template.',194 'title' => array(191 'id' => 'default//my_template', 192 'theme' => 'default', 193 'slug' => 'my_template', 194 'source' => 'custom', 195 'origin' => null, 196 'type' => 'wp_template', 197 'description' => 'Description of my template.', 198 'title' => array( 195 199 'raw' => 'My Template', 196 200 'rendered' => 'My Template', 197 201 ), 198 'status' => 'publish', 199 'wp_id' => self::$post->ID, 200 'has_theme_file' => false, 201 'is_custom' => true, 202 'author' => 0, 203 'modified' => mysql_to_rfc3339( self::$post->post_modified ), 202 'status' => 'publish', 203 'wp_id' => self::$post->ID, 204 'has_theme_file' => false, 205 'is_custom' => true, 206 'author' => 0, 207 'modified' => mysql_to_rfc3339( self::$post->post_modified ), 208 'author_text' => 'Test Blog', 209 'original_source' => 'site', 204 210 ), 205 211 $data … … 242 248 unset( $data['content'] ); 243 249 unset( $data['_links'] ); 250 $author_name = get_user_by( 'id', self::$admin_id )->get( 'display_name' ); 244 251 245 252 $this->assertSameSetsWithIndex( 246 253 array( 247 'id' => "{$theme_dir}//{$template}",248 'theme' => $theme_dir,249 'slug' => $template,250 'source' => 'custom',251 'origin' => null,252 'type' => 'wp_template',253 'description' => $args['post_excerpt'],254 'title' => array(254 'id' => "{$theme_dir}//{$template}", 255 'theme' => $theme_dir, 256 'slug' => $template, 257 'source' => 'custom', 258 'origin' => null, 259 'type' => 'wp_template', 260 'description' => $args['post_excerpt'], 261 'title' => array( 255 262 'raw' => $args['post_title'], 256 263 'rendered' => $args['post_title'], 257 264 ), 258 'status' => 'publish', 259 'wp_id' => $post->ID, 260 'has_theme_file' => false, 261 'is_custom' => true, 262 'author' => self::$admin_id, 263 'modified' => mysql_to_rfc3339( $post->post_modified ), 265 'status' => 'publish', 266 'wp_id' => $post->ID, 267 'has_theme_file' => false, 268 'is_custom' => true, 269 'author' => self::$admin_id, 270 'modified' => mysql_to_rfc3339( $post->post_modified ), 271 'author_text' => $author_name, 272 'original_source' => 'user', 264 273 ), 265 274 $data … … 422 431 unset( $data['wp_id'] ); 423 432 433 $author_name = get_user_by( 'id', self::$admin_id )->get( 'display_name' ); 434 424 435 $this->assertSame( 425 436 array( 426 'id' => 'default//my_custom_template',427 'theme' => 'default',428 'content' => array(437 'id' => 'default//my_custom_template', 438 'theme' => 'default', 439 'content' => array( 429 440 'raw' => 'Content', 430 441 ), 431 'slug' => 'my_custom_template',432 'source' => 'custom',433 'origin' => null,434 'type' => 'wp_template',435 'description' => 'Just a description',436 'title' => array(442 'slug' => 'my_custom_template', 443 'source' => 'custom', 444 'origin' => null, 445 'type' => 'wp_template', 446 'description' => 'Just a description', 447 'title' => array( 437 448 'raw' => 'My Template', 438 449 'rendered' => 'My Template', 439 450 ), 440 'status' => 'publish', 441 'has_theme_file' => false, 442 'is_custom' => true, 443 'author' => self::$admin_id, 444 'modified' => mysql_to_rfc3339( $modified ), 451 'status' => 'publish', 452 'has_theme_file' => false, 453 'is_custom' => true, 454 'author' => self::$admin_id, 455 'modified' => mysql_to_rfc3339( $modified ), 456 'author_text' => $author_name, 457 'original_source' => 'user', 445 458 ), 446 459 $data … … 470 483 unset( $data['wp_id'] ); 471 484 485 $author_name = get_user_by( 'id', self::$admin_id )->get( 'display_name' ); 486 472 487 $this->assertSame( 473 488 array( 474 'id' => 'default//404',475 'theme' => 'default',476 'content' => array(489 'id' => 'default//404', 490 'theme' => 'default', 491 'content' => array( 477 492 'raw' => '', 478 493 ), 479 'slug' => '404',480 'source' => 'custom',481 'origin' => null,482 'type' => 'wp_template',483 'description' => 'Template shown when no content is found.',484 'title' => array(494 'slug' => '404', 495 'source' => 'custom', 496 'origin' => null, 497 'type' => 'wp_template', 498 'description' => 'Template shown when no content is found.', 499 'title' => array( 485 500 'raw' => '404', 486 501 'rendered' => '404', 487 502 ), 488 'status' => 'publish', 489 'has_theme_file' => false, 490 'is_custom' => false, 491 'author' => self::$admin_id, 492 'modified' => mysql_to_rfc3339( $modified ), 503 'status' => 'publish', 504 'has_theme_file' => false, 505 'is_custom' => false, 506 'author' => self::$admin_id, 507 'modified' => mysql_to_rfc3339( $modified ), 508 'author_text' => $author_name, 509 'original_source' => 'user', 493 510 ), 494 511 $data … … 522 539 unset( $data['wp_id'] ); 523 540 541 $author_name = get_user_by( 'id', self::$admin_id )->get( 'display_name' ); 542 524 543 $this->assertSame( 525 544 array( 526 'id' => 'default//my_custom_template_raw',527 'theme' => 'default',528 'content' => array(545 'id' => 'default//my_custom_template_raw', 546 'theme' => 'default', 547 'content' => array( 529 548 'raw' => 'Content', 530 549 ), 531 'slug' => 'my_custom_template_raw',532 'source' => 'custom',533 'origin' => null,534 'type' => 'wp_template',535 'description' => 'Just a description',536 'title' => array(550 'slug' => 'my_custom_template_raw', 551 'source' => 'custom', 552 'origin' => null, 553 'type' => 'wp_template', 554 'description' => 'Just a description', 555 'title' => array( 537 556 'raw' => 'My Template', 538 557 'rendered' => 'My Template', 539 558 ), 540 'status' => 'publish', 541 'has_theme_file' => false, 542 'is_custom' => true, 543 'author' => self::$admin_id, 544 'modified' => mysql_to_rfc3339( $modified ), 559 'status' => 'publish', 560 'has_theme_file' => false, 561 'is_custom' => true, 562 'author' => self::$admin_id, 563 'modified' => mysql_to_rfc3339( $modified ), 564 'author_text' => $author_name, 565 'original_source' => 'user', 545 566 ), 546 567 $data … … 701 722 $data = $response->get_data(); 702 723 $properties = $data['schema']['properties']; 703 $this->assertCount( 1 5, $properties );724 $this->assertCount( 17, $properties ); 704 725 $this->assertArrayHasKey( 'id', $properties ); 705 726 $this->assertArrayHasKey( 'description', $properties ); … … 718 739 $this->assertArrayHasKey( 'author', $properties ); 719 740 $this->assertArrayHasKey( 'modified', $properties ); 741 $this->assertArrayHasKey( 'author_text', $properties ); 742 $this->assertArrayHasKey( 'original_source', $properties ); 720 743 } 721 744 … … 748 771 $request = new WP_REST_Request( 'POST', '/wp/v2/templates' ); 749 772 $request->set_body_params( $body_params ); 750 $response = rest_get_server()->dispatch( $request ); 751 $data = $response->get_data(); 752 $modified = get_post( $data['wp_id'] )->post_modified; 753 $expected['modified'] = mysql_to_rfc3339( $modified ); 773 $response = rest_get_server()->dispatch( $request ); 774 $data = $response->get_data(); 775 $modified = get_post( $data['wp_id'] )->post_modified; 776 $expected['modified'] = mysql_to_rfc3339( $modified ); 777 $expected['author_text'] = get_user_by( 'id', self::$admin_id )->get( 'display_name' ); 778 $expected['original_source'] = 'user'; 779 754 780 unset( $data['_links'] ); 755 781 unset( $data['wp_id'] );
Note: See TracChangeset
for help on using the changeset viewer.