Ticket #45269: 45269.diff
| File 45269.diff, 3.9 KB (added by , 7 years ago) |
|---|
-
src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index a90308101a..31cb070b94 100644
class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 237 237 238 238 $attachment = get_post( $request['id'] ); 239 239 240 /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */241 do_action( 'rest_insert_attachment', $data, $request, false );242 243 240 $fields_update = $this->update_additional_fields_for_object( $attachment, $request ); 244 241 245 242 if ( is_wp_error( $fields_update ) ) { -
src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index c2b707dfa0..d008dbfa0b 100644
class WP_REST_Posts_Controller extends WP_REST_Controller { 739 739 740 740 $request->set_param( 'context', 'edit' ); 741 741 742 $response = $this->prepare_item_for_response( $post, $request ); 743 744 if ( 'attachment' === $this->post_type ) { 745 return rest_ensure_response( $response ); 746 } 747 742 748 /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */ 743 749 do_action( "rest_after_insert_{$this->post_type}", $post, $request, false ); 744 750 745 $response = $this->prepare_item_for_response( $post, $request );746 747 751 return rest_ensure_response( $response ); 748 752 } 749 753 -
tests/phpunit/tests/rest-api/rest-attachments-controller.php
diff --git tests/phpunit/tests/rest-api/rest-attachments-controller.php tests/phpunit/tests/rest-api/rest-attachments-controller.php index fa22a4a39d..ce9d15f0ee 100644
class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 16 16 protected static $author_id; 17 17 protected static $contributor_id; 18 18 protected static $uploader_id; 19 protected static $rest_after_insert_attachment_count; 20 protected static $rest_insert_attachment_count; 19 21 20 22 public static function wpSetUpBeforeClass( $factory ) { 21 23 self::$superadmin_id = $factory->user->create( array( … … class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 1437 1439 $this->assertErrorResponse( 'rest_upload_limited_space', $response, 400 ); 1438 1440 } 1439 1441 1442 /** 1443 * Ensure the `rest_after_insert_attachment` and `rest_insert_attachment` hooks only fire 1444 * once when attachments are updated. 1445 * 1446 * @ticket 45269 1447 */ 1448 public function test_rest_insert_attachment_hooks_fire_once_on_update() { 1449 self::$rest_insert_attachment_count = 0; 1450 self::$rest_after_insert_attachment_count = 0; 1451 add_action( 'rest_insert_attachment', array( $this, 'filter_rest_insert_attachment' ) ); 1452 add_action( 'rest_after_insert_attachment', array( $this, 'filter_rest_after_insert_attachment' ) ); 1453 1454 wp_set_current_user( self::$editor_id ); 1455 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 1456 'post_mime_type' => 'image/jpeg', 1457 'post_excerpt' => 'A sample caption', 1458 'post_author' => self::$editor_id, 1459 ) ); 1460 $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id ); 1461 $request->set_param( 'title', 'My title is very cool' ); 1462 $response = $this->server->dispatch( $request ); 1463 1464 $this->assertSame( 1, self::$rest_insert_attachment_count ); 1465 $this->assertSame( 1, self::$rest_after_insert_attachment_count ); 1466 } 1467 1468 public function filter_rest_insert_attachment( $attachment ) { 1469 self::$rest_insert_attachment_count++; 1470 } 1471 1472 public function filter_rest_after_insert_attachment( $attachment ) { 1473 self::$rest_after_insert_attachment_count++; 1474 } 1440 1475 }