WordPress.org

Make WordPress Core

Ticket #45269: 45269.diff

File 45269.diff, 3.9 KB (added by peterwilsoncc, 2 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 { 
    237237
    238238                $attachment = get_post( $request['id'] );
    239239
    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 
    243240                $fields_update = $this->update_additional_fields_for_object( $attachment, $request );
    244241
    245242                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 { 
    739739
    740740                $request->set_param( 'context', 'edit' );
    741741
     742                $response = $this->prepare_item_for_response( $post, $request );
     743
     744                if ( 'attachment' === $this->post_type ) {
     745                        return rest_ensure_response( $response );
     746                }
     747
    742748                /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
    743749                do_action( "rest_after_insert_{$this->post_type}", $post, $request, false );
    744750
    745                 $response = $this->prepare_item_for_response( $post, $request );
    746 
    747751                return rest_ensure_response( $response );
    748752        }
    749753
  • 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 
    1616        protected static $author_id;
    1717        protected static $contributor_id;
    1818        protected static $uploader_id;
     19        protected static $rest_after_insert_attachment_count;
     20        protected static $rest_insert_attachment_count;
    1921
    2022        public static function wpSetUpBeforeClass( $factory ) {
    2123                self::$superadmin_id = $factory->user->create( array(
    class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 
    14371439                $this->assertErrorResponse( 'rest_upload_limited_space', $response, 400 );
    14381440        }
    14391441
     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        }
    14401475}