Changeset 54269
- Timestamp:
- 09/20/2022 09:19:10 PM (2 years ago)
- Location:
- trunk
- Files:
-
- 3 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/phpcs.xml.dist
r53800 r54269 238 238 <!-- Test case parent classes outside of the "includes" folder. --> 239 239 <element value="Tests_Query_Conditionals"/> 240 <element value="WP_Block_Templates_UnitTestCase"/> 240 241 <element value="WP_Filesystem_UnitTestCase"/> 241 242 <element value="WP_HTTP_UnitTestCase"/> -
trunk/src/wp-includes/block-template-utils.php
r54210 r54269 148 148 'category' => array( 149 149 'title' => _x( 'Category', 'Template name' ), 150 'description' => __( 'Displays latest posts insingle post category.' ),150 'description' => __( 'Displays latest posts from a single post category.' ), 151 151 ), 152 152 'taxonomy' => array( … … 556 556 $has_theme_file = wp_get_theme()->get_stylesheet() === $theme && null !== $template_file; 557 557 558 $origin = get_post_meta( $post->ID, 'origin', true ); 558 $origin = get_post_meta( $post->ID, 'origin', true ); 559 $is_wp_suggestion = get_post_meta( $post->ID, 'is_wp_suggestion', true ); 559 560 560 561 $template = new WP_Block_Template(); … … 571 572 $template->status = $post->post_status; 572 573 $template->has_theme_file = $has_theme_file; 573 $template->is_custom = true;574 $template->is_custom = empty( $is_wp_suggestion ); 574 575 $template->author = $post->post_author; 575 576 … … 680 681 } 681 682 682 if ( $post_type && 683 if ( 684 $post_type && 683 685 isset( $template->post_types ) && 684 686 ! in_array( $post_type, $template->post_types, true ) … … 913 915 */ 914 916 function wp_is_theme_directory_ignored( $path ) { 915 $directories_to_ignore = array( '.svn', '.git', '.hg', '.bzr', 'node_modules', 'vendor' ); 917 $directories_to_ignore = array( '.DS_Store', '.svn', '.git', '.hg', '.bzr', 'node_modules', 'vendor' ); 918 916 919 foreach ( $directories_to_ignore as $directory ) { 917 if ( str pos( $path, $directory ) === 0) {920 if ( str_starts_with( $path, $directory ) ) { 918 921 return true; 919 922 } … … 1024 1027 return $filename; 1025 1028 } 1029 1030 /** 1031 * Gets the template hierarchy for the given template slug to be created. 1032 * 1033 * 1034 * Note: Always add `index` as the last fallback template. 1035 * 1036 * @since 6.1.0 1037 * 1038 * @param string $slug The template slug to be created. 1039 * @param boolean $is_custom Optional. Indicates if a template is custom or 1040 * part of the template hierarchy. Default false. 1041 * @param string $template_prefix Optional. The template prefix for the created template. 1042 * Used to extract the main template type, e.g. 1043 * in `taxonomy-books` the `taxonomy` is extracted. 1044 * Default empty string. 1045 * @return string[] The template hierarchy. 1046 */ 1047 function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '' ) { 1048 if ( 'index' === $slug ) { 1049 return array( 'index' ); 1050 } 1051 if ( $is_custom ) { 1052 return array( 'page', 'singular', 'index' ); 1053 } 1054 if ( 'front-page' === $slug ) { 1055 return array( 'front-page', 'home', 'index' ); 1056 } 1057 1058 $template_hierarchy = array( $slug ); 1059 1060 // Most default templates don't have `$template_prefix` assigned. 1061 if ( $template_prefix ) { 1062 list( $type ) = explode( '-', $template_prefix ); 1063 // These checks are needed because the `$slug` above is always added. 1064 if ( ! in_array( $template_prefix, array( $slug, $type ), true ) ) { 1065 $template_hierarchy[] = $template_prefix; 1066 } 1067 if ( $slug !== $type ) { 1068 $template_hierarchy[] = $type; 1069 } 1070 } 1071 1072 // Handle `archive` template. 1073 if ( 1074 str_starts_with( $slug, 'author' ) || 1075 str_starts_with( $slug, 'taxonomy' ) || 1076 str_starts_with( $slug, 'category' ) || 1077 str_starts_with( $slug, 'tag' ) || 1078 'date' === $slug 1079 ) { 1080 $template_hierarchy[] = 'archive'; 1081 } 1082 // Handle `single` template. 1083 if ( 'attachment' === $slug ) { 1084 $template_hierarchy[] = 'single'; 1085 } 1086 1087 // Handle `singular` template. 1088 if ( 1089 str_starts_with( $slug, 'single' ) || 1090 str_starts_with( $slug, 'page' ) || 1091 'attachment' === $slug 1092 ) { 1093 $template_hierarchy[] = 'singular'; 1094 } 1095 1096 $template_hierarchy[] = 'index'; 1097 1098 return $template_hierarchy; 1099 }; -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php
r53760 r54269 187 187 $data = array(); 188 188 189 if ( in_array( 'capabilities', $fields, true) ) {189 if ( rest_is_field_included( 'capabilities', $fields ) ) { 190 190 $data['capabilities'] = $post_type->cap; 191 191 } 192 192 193 if ( in_array( 'description', $fields, true) ) {193 if ( rest_is_field_included( 'description', $fields ) ) { 194 194 $data['description'] = $post_type->description; 195 195 } 196 196 197 if ( in_array( 'hierarchical', $fields, true) ) {197 if ( rest_is_field_included( 'hierarchical', $fields ) ) { 198 198 $data['hierarchical'] = $post_type->hierarchical; 199 199 } 200 200 201 if ( in_array( 'visibility', $fields, true) ) {201 if ( rest_is_field_included( 'visibility', $fields ) ) { 202 202 $data['visibility'] = array( 203 203 'show_in_nav_menus' => (bool) $post_type->show_in_nav_menus, … … 206 206 } 207 207 208 if ( in_array( 'viewable', $fields, true) ) {208 if ( rest_is_field_included( 'viewable', $fields ) ) { 209 209 $data['viewable'] = is_post_type_viewable( $post_type ); 210 210 } 211 211 212 if ( in_array( 'labels', $fields, true) ) {212 if ( rest_is_field_included( 'labels', $fields ) ) { 213 213 $data['labels'] = $post_type->labels; 214 214 } 215 215 216 if ( in_array( 'name', $fields, true) ) {216 if ( rest_is_field_included( 'name', $fields ) ) { 217 217 $data['name'] = $post_type->label; 218 218 } 219 219 220 if ( in_array( 'slug', $fields, true) ) {220 if ( rest_is_field_included( 'slug', $fields ) ) { 221 221 $data['slug'] = $post_type->name; 222 222 } 223 223 224 if ( in_array( 'supports', $fields, true ) ) { 224 if ( rest_is_field_included( 'icon', $fields ) ) { 225 $data['icon'] = $post_type->menu_icon; 226 } 227 228 if ( rest_is_field_included( 'supports', $fields ) ) { 225 229 $data['supports'] = $supports; 226 230 } 227 231 228 if ( in_array( 'taxonomies', $fields, true) ) {232 if ( rest_is_field_included( 'taxonomies', $fields ) ) { 229 233 $data['taxonomies'] = array_values( $taxonomies ); 230 234 } 231 235 232 if ( in_array( 'rest_base', $fields, true) ) {236 if ( rest_is_field_included( 'rest_base', $fields ) ) { 233 237 $data['rest_base'] = $base; 234 238 } 235 239 236 if ( in_array( 'rest_namespace', $fields, true) ) {240 if ( rest_is_field_included( 'rest_namespace', $fields ) ) { 237 241 $data['rest_namespace'] = $namespace; 238 242 } … … 288 292 * @since 4.8.0 The `supports` property was added. 289 293 * @since 5.9.0 The `visibility` and `rest_namespace` properties were added. 294 * @since 6.1.0 The `icon` property was added. 290 295 * 291 296 * @return array Item schema data. … … 386 391 ), 387 392 ), 393 'icon' => array( 394 'description' => __( 'The icon for the post type.' ), 395 'type' => array( 'string', 'null' ), 396 'context' => array( 'view', 'edit', 'embed' ), 397 'readonly' => true, 398 ), 388 399 ), 389 400 ); -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php
r54121 r54269 43 43 * 44 44 * @since 5.8.0 45 * @since 6.1.0 Endpoint for fallback template content. 45 46 */ 46 47 public function register_routes() { … … 63 64 ), 64 65 'schema' => array( $this, 'get_public_item_schema' ), 66 ) 67 ); 68 69 // Get fallback template content. 70 register_rest_route( 71 $this->namespace, 72 '/' . $this->rest_base . '/lookup', 73 array( 74 array( 75 'methods' => WP_REST_Server::READABLE, 76 'callback' => array( $this, 'get_template_fallback' ), 77 'permission_callback' => array( $this, 'get_item_permissions_check' ), 78 'args' => array( 79 'slug' => array( 80 'description' => __( 'The slug of the template to get the fallback for' ), 81 'type' => 'string', 82 'required' => true, 83 ), 84 'is_custom' => array( 85 'description' => __( ' Indicates if a template is custom or part of the template hierarchy' ), 86 'type' => 'boolean', 87 ), 88 'template_prefix' => array( 89 'description' => __( 'The template prefix for the created template. This is used to extract the main template type, e.g. in `taxonomy-books` extracts the `taxonomy`' ), 90 'type' => 'string', 91 ), 92 ), 93 ), 65 94 ) 66 95 ); … … 116 145 ) 117 146 ); 147 } 148 149 /** 150 * Returns the fallback template for the given slug. 151 * 152 * @since 6.1.0 153 * 154 * @param WP_REST_Request $request The request instance. 155 * @return WP_REST_Response|WP_Error 156 */ 157 public function get_template_fallback( $request ) { 158 $hierarchy = get_template_hierarchy( $request['slug'], $request['is_custom'], $request['template_prefix'] ); 159 $fallback_template = resolve_block_template( $request['slug'], $hierarchy, '' ); 160 $response = $this->prepare_item_for_response( $fallback_template, $request ); 161 return rest_ensure_response( $response ); 118 162 } 119 163 … … 526 570 } 527 571 572 if ( 'wp_template' === $this->post_type && isset( $request['is_wp_suggestion'] ) ) { 573 $changes->meta_input = wp_parse_args( 574 array( 575 'is_wp_suggestion' => $request['is_wp_suggestion'], 576 ), 577 $changes->meta_input = array() 578 ); 579 } 580 528 581 if ( 'wp_template_part' === $this->post_type ) { 529 582 if ( isset( $request['area'] ) ) { -
trunk/tests/phpunit/tests/rest-api/rest-post-types-controller.php
r54090 r54269 157 157 } 158 158 159 /** 160 * @ticket 56467 161 * 162 * @covers WP_REST_Post_Types_Controller::get_item_schema 163 */ 159 164 public function test_get_item_schema() { 160 165 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/types' ); … … 162 167 $data = $response->get_data(); 163 168 $properties = $data['schema']['properties']; 164 $this->assertCount( 12, $properties ); 165 $this->assertArrayHasKey( 'capabilities', $properties ); 166 $this->assertArrayHasKey( 'description', $properties ); 167 $this->assertArrayHasKey( 'hierarchical', $properties ); 168 $this->assertArrayHasKey( 'viewable', $properties ); 169 $this->assertArrayHasKey( 'labels', $properties ); 170 $this->assertArrayHasKey( 'name', $properties ); 171 $this->assertArrayHasKey( 'slug', $properties ); 172 $this->assertArrayHasKey( 'supports', $properties ); 173 $this->assertArrayHasKey( 'taxonomies', $properties ); 174 $this->assertArrayHasKey( 'rest_base', $properties ); 175 $this->assertArrayHasKey( 'rest_namespace', $properties ); 176 $this->assertArrayHasKey( 'visibility', $properties ); 169 170 $this->assertCount( 13, $properties, 'Schema should have 13 properties' ); 171 $this->assertArrayHasKey( 'capabilities', $properties, '`capabilities` should be included in the schema' ); 172 $this->assertArrayHasKey( 'description', $properties, '`description` should be included in the schema' ); 173 $this->assertArrayHasKey( 'hierarchical', $properties, '`hierarchical` should be included in the schema' ); 174 $this->assertArrayHasKey( 'viewable', $properties, '`viewable` should be included in the schema' ); 175 $this->assertArrayHasKey( 'labels', $properties, '`labels` should be included in the schema' ); 176 $this->assertArrayHasKey( 'name', $properties, '`name` should be included in the schema' ); 177 $this->assertArrayHasKey( 'slug', $properties, '`slug` should be included in the schema' ); 178 $this->assertArrayHasKey( 'supports', $properties, '`supports` should be included in the schema' ); 179 $this->assertArrayHasKey( 'taxonomies', $properties, '`taxonomies` should be included in the schema' ); 180 $this->assertArrayHasKey( 'rest_base', $properties, '`rest_base` should be included in the schema' ); 181 $this->assertArrayHasKey( 'rest_namespace', $properties, '`rest_namespace` should be included in the schema' ); 182 $this->assertArrayHasKey( 'visibility', $properties, '`visibility` should be included in the schema' ); 183 $this->assertArrayHasKey( 'icon', $properties, '`icon` should be included in the schema' ); 177 184 } 178 185 -
trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php
r54090 r54269 151 151 '/wp/v2/template-parts/(?P<parent>[\d]+)/revisions', 152 152 '/wp/v2/template-parts/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)', 153 '/wp/v2/template-parts/lookup', 153 154 '/wp/v2/templates', 154 155 '/wp/v2/templates/(?P<id>[\d]+)/autosaves', … … 157 158 '/wp/v2/templates/(?P<parent>[\d]+)/revisions', 158 159 '/wp/v2/templates/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)', 160 '/wp/v2/templates/lookup', 159 161 '/wp/v2/themes', 160 162 '/wp/v2/themes/(?P<stylesheet>[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)', -
trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php
r54058 r54269 57 57 * @covers WP_REST_Templates_Controller::register_routes 58 58 * @ticket 54596 59 * @ticket 56467 59 60 */ 60 61 public function test_register_routes() { … … 69 70 $routes, 70 71 'Single template based on the given ID route does not exist' 72 ); 73 $this->assertArrayHasKey( 74 '/wp/v2/templates/lookup', 75 $routes, 76 'Get template fallback content route does not exist' 71 77 ); 72 78 } … … 679 685 } 680 686 687 /** 688 * @dataProvider data_create_item_with_is_wp_suggestion 689 * @ticket 56467 690 * @covers WP_REST_Templates_Controller::create_item 691 * 692 * @param array $body_params Data set to test. 693 * @param array $expected Expected results. 694 */ 695 public function test_create_item_with_is_wp_suggestion( array $body_params, array $expected ) { 696 // Set up the user. 697 $body_params['author'] = self::$admin_id; 698 $expected['author'] = self::$admin_id; 699 wp_set_current_user( self::$admin_id ); 700 701 $request = new WP_REST_Request( 'POST', '/wp/v2/templates' ); 702 $request->set_body_params( $body_params ); 703 $response = rest_get_server()->dispatch( $request ); 704 $data = $response->get_data(); 705 unset( $data['_links'] ); 706 unset( $data['wp_id'] ); 707 708 $this->assertSame( $expected, $data ); 709 } 710 711 /** 712 * Data provider. 713 * 714 * @return array 715 */ 716 public function data_create_item_with_is_wp_suggestion() { 717 $expected = array( 718 'id' => 'default//page-rigas', 719 'theme' => 'default', 720 'content' => array( 721 'raw' => 'Content', 722 ), 723 'slug' => 'page-rigas', 724 'source' => 'custom', 725 'origin' => null, 726 'type' => 'wp_template', 727 'description' => 'Just a description', 728 'title' => array( 729 'raw' => 'My Template', 730 'rendered' => 'My Template', 731 ), 732 'status' => 'publish', 733 'has_theme_file' => false, 734 'is_custom' => false, 735 'author' => null, 736 ); 737 738 return array( 739 'is_wp_suggestion: true' => array( 740 'body_params' => array( 741 'slug' => 'page-rigas', 742 'description' => 'Just a description', 743 'title' => 'My Template', 744 'content' => 'Content', 745 'is_wp_suggestion' => true, 746 'author' => null, 747 ), 748 'expected' => $expected, 749 ), 750 'is_wp_suggestion: false' => array( 751 'body_params' => array( 752 'slug' => 'page-hi', 753 'description' => 'Just a description', 754 'title' => 'My Template', 755 'content' => 'Content', 756 'is_wp_suggestion' => false, 757 'author' => null, 758 ), 759 'expected' => array_merge( 760 $expected, 761 array( 762 'id' => 'default//page-hi', 763 'slug' => 'page-hi', 764 'is_custom' => true, 765 ) 766 ), 767 ), 768 ); 769 } 770 771 /** 772 * @ticket 56467 773 * @covers WP_REST_Templates_Controller::get_template_fallback 774 */ 775 public function test_get_template_fallback() { 776 wp_set_current_user( self::$admin_id ); 777 switch_theme( 'block-theme' ); 778 $request = new WP_REST_Request( 'GET', '/wp/v2/templates/lookup' ); 779 // Should fallback to `index.html`. 780 $request->set_param( 'slug', 'tag-status' ); 781 $request->set_param( 'is_custom', false ); 782 $request->set_param( 'template_prefix', 'tag' ); 783 $response = rest_get_server()->dispatch( $request ); 784 $this->assertSame( 'index', $response->get_data()['slug'], 'Should fallback to `index.html`.' ); 785 // Should fallback to `page.html`. 786 $request->set_param( 'slug', 'page-hello' ); 787 $request->set_param( 'is_custom', false ); 788 $request->set_param( 'template_prefix', 'page' ); 789 $response = rest_get_server()->dispatch( $request ); 790 $this->assertSame( 'page', $response->get_data()['slug'], 'Should fallback to `page.html`.' ); 791 } 681 792 } -
trunk/tests/qunit/fixtures/wp-api-generated.js
r54123 r54269 5141 5141 } 5142 5142 }, 5143 "/wp/v2/templates/lookup": { 5144 "namespace": "wp/v2", 5145 "methods": [ 5146 "GET" 5147 ], 5148 "endpoints": [ 5149 { 5150 "methods": [ 5151 "GET" 5152 ], 5153 "args": { 5154 "slug": { 5155 "description": "The slug of the template to get the fallback for", 5156 "type": "string", 5157 "required": true 5158 }, 5159 "is_custom": { 5160 "description": " Indicates if a template is custom or part of the template hierarchy", 5161 "type": "boolean", 5162 "required": false 5163 }, 5164 "template_prefix": { 5165 "description": "The template prefix for the created template. This is used to extract the main template type, e.g. in `taxonomy-books` extracts the `taxonomy`", 5166 "type": "string", 5167 "required": false 5168 } 5169 } 5170 } 5171 ], 5172 "_links": { 5173 "self": [ 5174 { 5175 "href": "http://example.org/index.php?rest_route=/wp/v2/templates/lookup" 5176 } 5177 ] 5178 } 5179 }, 5143 5180 "/wp/v2/templates/(?P<id>([^\\/:<>\\*\\?\"\\|]+(?:\\/[^\\/:<>\\*\\?\"\\|]+)?)[\\/\\w-]+)": { 5144 5181 "namespace": "wp/v2", … … 5789 5826 { 5790 5827 "href": "http://example.org/index.php?rest_route=/wp/v2/template-parts" 5828 } 5829 ] 5830 } 5831 }, 5832 "/wp/v2/template-parts/lookup": { 5833 "namespace": "wp/v2", 5834 "methods": [ 5835 "GET" 5836 ], 5837 "endpoints": [ 5838 { 5839 "methods": [ 5840 "GET" 5841 ], 5842 "args": { 5843 "slug": { 5844 "description": "The slug of the template to get the fallback for", 5845 "type": "string", 5846 "required": true 5847 }, 5848 "is_custom": { 5849 "description": " Indicates if a template is custom or part of the template hierarchy", 5850 "type": "boolean", 5851 "required": false 5852 }, 5853 "template_prefix": { 5854 "description": "The template prefix for the created template. This is used to extract the main template type, e.g. in `taxonomy-books` extracts the `taxonomy`", 5855 "type": "string", 5856 "required": false 5857 } 5858 } 5859 } 5860 ], 5861 "_links": { 5862 "self": [ 5863 { 5864 "href": "http://example.org/index.php?rest_route=/wp/v2/template-parts/lookup" 5791 5865 } 5792 5866 ] … … 11592 11666 "name": "Posts", 11593 11667 "slug": "post", 11668 "icon": "dashicons-admin-post", 11594 11669 "taxonomies": [ 11595 11670 "category", … … 11623 11698 "name": "Pages", 11624 11699 "slug": "page", 11700 "icon": "dashicons-admin-page", 11625 11701 "taxonomies": [], 11626 11702 "rest_base": "pages", … … 11651 11727 "name": "Media", 11652 11728 "slug": "attachment", 11729 "icon": "dashicons-admin-media", 11653 11730 "taxonomies": [], 11654 11731 "rest_base": "media", … … 11679 11756 "name": "Navigation Menu Items", 11680 11757 "slug": "nav_menu_item", 11758 "icon": null, 11681 11759 "taxonomies": [ 11682 11760 "nav_menu" … … 11709 11787 "name": "Reusable blocks", 11710 11788 "slug": "wp_block", 11789 "icon": null, 11711 11790 "taxonomies": [], 11712 11791 "rest_base": "blocks", … … 11737 11816 "name": "Templates", 11738 11817 "slug": "wp_template", 11818 "icon": null, 11739 11819 "taxonomies": [], 11740 11820 "rest_base": "templates", … … 11765 11845 "name": "Template Parts", 11766 11846 "slug": "wp_template_part", 11847 "icon": null, 11767 11848 "taxonomies": [], 11768 11849 "rest_base": "template-parts", … … 11793 11874 "name": "Navigation Menus", 11794 11875 "slug": "wp_navigation", 11876 "icon": null, 11795 11877 "taxonomies": [], 11796 11878 "rest_base": "navigation", … … 11823 11905 "name": "Posts", 11824 11906 "slug": "post", 11907 "icon": "dashicons-admin-post", 11825 11908 "taxonomies": [ 11826 11909 "category",
Note: See TracChangeset
for help on using the changeset viewer.