Make WordPress Core

Changeset 44225


Ignore:
Timestamp:
12/16/2018 02:16:16 AM (6 years ago)
Author:
jeremyfelt
Message:

REST API: Prevent duplicate firing of rest(_after)?_insert_attachment actions.

Merges [43862] from the 5.0 branch to trunk.

Props danielbachhuber, peterwilsoncc.
Fixes #45269.
See #42864.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r44216 r44225  
    237237
    238238        $attachment = get_post( $request['id'] );
    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 );
    242239
    243240        $fields_update = $this->update_additional_fields_for_object( $attachment, $request );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r44127 r44225  
    747747
    748748        $request->set_param( 'context', 'edit' );
     749
     750        // Filter is fired in WP_REST_Attachments_Controller subclass.
     751        if ( 'attachment' === $this->post_type ) {
     752            $response = $this->prepare_item_for_response( $post, $request );
     753            return rest_ensure_response( $response );
     754        }
    749755
    750756        /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r43981 r44225  
    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 ) {
     
    15141516        }
    15151517
     1518        remove_action( 'rest_insert_attachment', array( $this, 'filter_rest_insert_attachment' ) );
     1519        remove_action( 'rest_after_insert_attachment', array( $this, 'filter_rest_after_insert_attachment' ) );
     1520
    15161521        $this->remove_added_uploads();
    15171522    }
     
    16451650    }
    16461651
     1652    /**
     1653     * Ensure the `rest_after_insert_attachment` and `rest_insert_attachment` hooks only fire
     1654     * once when attachments are created.
     1655     *
     1656     * @ticket 45269
     1657     */
     1658    public function test_rest_insert_attachment_hooks_fire_once_on_create() {
     1659        self::$rest_insert_attachment_count       = 0;
     1660        self::$rest_after_insert_attachment_count = 0;
     1661        add_action( 'rest_insert_attachment', array( $this, 'filter_rest_insert_attachment' ) );
     1662        add_action( 'rest_after_insert_attachment', array( $this, 'filter_rest_after_insert_attachment' ) );
     1663
     1664        wp_set_current_user( self::$editor_id );
     1665        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     1666        $request->set_header( 'Content-Type', 'image/jpeg' );
     1667        $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
     1668        $request->set_param( 'title', 'My title is very cool' );
     1669        $request->set_param( 'caption', 'This is a better caption.' );
     1670        $request->set_param( 'description', 'Without a description, my attachment is descriptionless.' );
     1671        $request->set_param( 'alt_text', 'Alt text is stored outside post schema.' );
     1672
     1673        $request->set_body( file_get_contents( $this->test_file ) );
     1674        $response = $this->server->dispatch( $request );
     1675        $data     = $response->get_data();
     1676        $this->assertEquals( 201, $response->get_status() );
     1677
     1678        $this->assertSame( 1, self::$rest_insert_attachment_count );
     1679        $this->assertSame( 1, self::$rest_after_insert_attachment_count );
     1680    }
     1681
     1682    /**
     1683     * Ensure the `rest_after_insert_attachment` and `rest_insert_attachment` hooks only fire
     1684     * once when attachments are updated.
     1685     *
     1686     * @ticket 45269
     1687     */
     1688    public function test_rest_insert_attachment_hooks_fire_once_on_update() {
     1689        self::$rest_insert_attachment_count       = 0;
     1690        self::$rest_after_insert_attachment_count = 0;
     1691        add_action( 'rest_insert_attachment', array( $this, 'filter_rest_insert_attachment' ) );
     1692        add_action( 'rest_after_insert_attachment', array( $this, 'filter_rest_after_insert_attachment' ) );
     1693
     1694        wp_set_current_user( self::$editor_id );
     1695        $attachment_id = $this->factory->attachment->create_object(
     1696            $this->test_file,
     1697            0,
     1698            array(
     1699                'post_mime_type' => 'image/jpeg',
     1700                'post_excerpt'   => 'A sample caption',
     1701                'post_author'    => self::$editor_id,
     1702            )
     1703        );
     1704        $request       = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id );
     1705        $request->set_param( 'title', 'My title is very cool' );
     1706        $response = $this->server->dispatch( $request );
     1707
     1708        $this->assertSame( 1, self::$rest_insert_attachment_count );
     1709        $this->assertSame( 1, self::$rest_after_insert_attachment_count );
     1710    }
     1711
     1712    public function filter_rest_insert_attachment( $attachment ) {
     1713        self::$rest_insert_attachment_count++;
     1714    }
     1715
     1716    public function filter_rest_after_insert_attachment( $attachment ) {
     1717        self::$rest_after_insert_attachment_count++;
     1718    }
    16471719}
Note: See TracChangeset for help on using the changeset viewer.