Changeset 57603
- Timestamp:
- 02/12/2024 10:15:45 PM (8 months ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
r57524 r57603 172 172 } 173 173 174 if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) { 175 $thumbnail_update = $this->handle_featured_media( $request['featured_media'], $attachment_id ); 176 177 if ( is_wp_error( $thumbnail_update ) ) { 178 return $thumbnail_update; 179 } 180 } 181 174 182 if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { 175 183 $meta_update = $this->meta->update_value( $request['meta'], $attachment_id ); … … 324 332 325 333 /** 334 * Determines the featured media based on a request param. 335 * 336 * @since 6.5.0 337 * 338 * @param int $featured_media Featured Media ID. 339 * @param int $post_id Post ID. 340 * @return bool|WP_Error Whether the post thumbnail was successfully deleted, otherwise WP_Error. 341 */ 342 protected function handle_featured_media( $featured_media, $post_id ) { 343 $post_type = get_post_type( $post_id ); 344 $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ); 345 346 // Similar check as in wp_insert_post(). 347 if ( ! $thumbnail_support && get_post_mime_type( $post_id ) ) { 348 if ( wp_attachment_is( 'audio', $post_id ) ) { 349 $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); 350 } elseif ( wp_attachment_is( 'video', $post_id ) ) { 351 $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); 352 } 353 } 354 355 if ( $thumbnail_support ) { 356 return parent::handle_featured_media( $featured_media, $post_id ); 357 } 358 359 return new WP_Error( 360 'rest_no_featured_media', 361 sprintf( 362 /* translators: %s: attachment mime type */ 363 __( 'This site does not support post thumbnails on attachments with MIME type %s.' ), 364 get_post_mime_type( $post_id ) 365 ), 366 array( 'status' => 400 ) 367 ); 368 } 369 370 /** 326 371 * Updates a single attachment. 327 372 * … … 355 400 356 401 $attachment = get_post( $request['id'] ); 402 403 if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) { 404 $thumbnail_update = $this->handle_featured_media( $request['featured_media'], $attachment->ID ); 405 406 if ( is_wp_error( $thumbnail_update ) ) { 407 return $thumbnail_update; 408 } 409 } 357 410 358 411 $fields_update = $this->update_additional_fields_for_object( $attachment, $request ); -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
r57230 r57603 2378 2378 'revisions', 2379 2379 'custom-fields', 2380 'thumbnail', 2380 2381 ), 2381 2382 ); -
trunk/tests/phpunit/includes/testcase-rest-post-type-controller.php
r56549 r57603 78 78 } 79 79 80 if ( post_type_supports( $post->post_type, 'thumbnail' ) ) { 80 if ( 81 post_type_supports( $post->post_type, 'thumbnail' ) || 82 ( 83 'attachment' === $post->post_type && 84 ( 85 post_type_supports( 'attachment:audio', 'thumbnail' ) || 86 post_type_supports( 'attachment:video', 'thumbnail' ) 87 ) 88 ) 89 ) { 81 90 $this->assertSame( (int) get_post_thumbnail_id( $post->ID ), $data['featured_media'] ); 82 91 } else { -
trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php
r57380 r57603 1066 1066 $term = wp_get_post_terms( $attachment['id'], 'category' ); 1067 1067 $this->assertSame( $category['term_id'], $term[0]->term_id ); 1068 } 1069 1070 /** 1071 * @ticket 41692 1072 */ 1073 public function test_create_update_post_with_featured_media() { 1074 // Add support for thumbnails on all attachment types to avoid incorrect-usage notice. 1075 add_post_type_support( 'attachment', 'thumbnail' ); 1076 1077 wp_set_current_user( self::$editor_id ); 1078 1079 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 1080 $request->set_file_params( 1081 array( 1082 'file' => array( 1083 'file' => file_get_contents( self::$test_file ), 1084 'name' => 'canola.jpg', 1085 'size' => filesize( self::$test_file ), 1086 'tmp_name' => self::$test_file, 1087 ), 1088 ) 1089 ); 1090 $request->set_header( 'Content-MD5', md5_file( self::$test_file ) ); 1091 1092 $file = DIR_TESTDATA . '/images/canola.jpg'; 1093 $attachment_id = self::factory()->attachment->create_object( 1094 $file, 1095 0, 1096 array( 1097 'post_mime_type' => 'image/jpeg', 1098 'menu_order' => rand( 1, 100 ), 1099 ) 1100 ); 1101 1102 $request->set_param( 'featured_media', $attachment_id ); 1103 1104 $response = rest_get_server()->dispatch( $request ); 1105 $data = $response->get_data(); 1106 1107 $this->assertEquals( 201, $response->get_status() ); 1108 1109 $new_attachment = get_post( $data['id'] ); 1110 1111 $this->assertEquals( $attachment_id, (int) get_post_thumbnail_id( $new_attachment->ID ) ); 1112 $this->assertEquals( $attachment_id, $data['featured_media'] ); 1113 1114 $request = new WP_REST_Request( 'PUT', '/wp/v2/media/' . $new_attachment->ID ); 1115 $params = $this->set_post_data( 1116 array( 1117 'featured_media' => 0, 1118 ) 1119 ); 1120 $request->set_body_params( $params ); 1121 $response = rest_get_server()->dispatch( $request ); 1122 $this->assertEquals( 200, $response->get_status() ); 1123 $data = $response->get_data(); 1124 $this->assertEquals( 0, $data['featured_media'] ); 1125 $this->assertEquals( 0, (int) get_post_thumbnail_id( $new_attachment->ID ) ); 1126 1127 $request = new WP_REST_Request( 'PUT', '/wp/v2/media/' . $new_attachment->ID ); 1128 $params = $this->set_post_data( 1129 array( 1130 'featured_media' => $attachment_id, 1131 ) 1132 ); 1133 $request->set_body_params( $params ); 1134 $response = rest_get_server()->dispatch( $request ); 1135 $this->assertEquals( 200, $response->get_status() ); 1136 $data = $response->get_data(); 1137 $this->assertEquals( $attachment_id, $data['featured_media'] ); 1138 $this->assertEquals( $attachment_id, (int) get_post_thumbnail_id( $new_attachment->ID ) ); 1068 1139 } 1069 1140 … … 1577 1648 $data = $response->get_data(); 1578 1649 $properties = $data['schema']['properties']; 1579 $this->assertCount( 2 7, $properties );1650 $this->assertCount( 28, $properties ); 1580 1651 $this->assertArrayHasKey( 'author', $properties ); 1581 1652 $this->assertArrayHasKey( 'alt_text', $properties ); … … 1611 1682 $this->assertArrayHasKey( 'type', $properties ); 1612 1683 $this->assertArrayHasKey( 'missing_image_sizes', $properties ); 1684 $this->assertArrayHasKey( 'featured_media', $properties ); 1613 1685 } 1614 1686 -
trunk/tests/qunit/fixtures/wp-api-generated.js
r57548 r57603 7316 7316 "required": false 7317 7317 }, 7318 "featured_media": { 7319 "description": "The ID of the featured media for the post.", 7320 "type": "integer", 7321 "required": false 7322 }, 7318 7323 "comment_status": { 7319 7324 "description": "Whether or not comments are open on the post.", … … 7515 7520 "author": { 7516 7521 "description": "The ID for the author of the post.", 7522 "type": "integer", 7523 "required": false 7524 }, 7525 "featured_media": { 7526 "description": "The ID of the featured media for the post.", 7517 7527 "type": "integer", 7518 7528 "required": false … … 12694 12704 }, 12695 12705 "author": 0, 12706 "featured_media": 0, 12696 12707 "comment_status": "open", 12697 12708 "ping_status": "closed", … … 12755 12766 }, 12756 12767 "author": 0, 12768 "featured_media": 0, 12757 12769 "comment_status": "open", 12758 12770 "ping_status": "closed",
Note: See TracChangeset
for help on using the changeset viewer.