Make WordPress Core


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:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • 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.