Make WordPress Core


Ignore:
Timestamp:
02/01/2021 06:35:38 PM (4 years ago)
Author:
antpb
Message:

REST API, Media: Add batch image editing endpoints.

Introduces new endpoints to allow for batch image editing using the REST API.

The new endpoints can take an array of modifiers that will be applied in the order they appear.

Props ajlende, TimothyBlynJacobs, hellofromTonya, Mista-Flo.
Fixes #52192.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r50024 r50124  
    20802080
    20812081    /**
     2082     * @ticket 52192
     2083     * @requires function imagejpeg
     2084     */
     2085    public function test_batch_edit_image() {
     2086        wp_set_current_user( self::$superadmin_id );
     2087        $attachment = self::factory()->attachment->create_upload_object( $this->test_file );
     2088
     2089        $params = array(
     2090            'modifiers' => array(
     2091                array(
     2092                    'type' => 'rotate',
     2093                    'args' => array(
     2094                        'angle' => 60,
     2095                    ),
     2096                ),
     2097                array(
     2098                    'type' => 'crop',
     2099                    'args' => array(
     2100                        'left'   => 50,
     2101                        'top'    => 10,
     2102                        'width'  => 10,
     2103                        'height' => 5,
     2104                    ),
     2105                ),
     2106            ),
     2107            'src'       => wp_get_attachment_image_url( $attachment, 'full' ),
     2108        );
     2109
     2110        $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" );
     2111        $request->set_body_params( $params );
     2112        $response = rest_do_request( $request );
     2113        $item     = $response->get_data();
     2114
     2115        $this->assertSame( 201, $response->get_status() );
     2116        $this->assertSame( rest_url( '/wp/v2/media/' . $item['id'] ), $response->get_headers()['Location'] );
     2117
     2118        $this->assertStringEndsWith( '-edited.jpg', $item['media_details']['file'] );
     2119        $this->assertArrayHasKey( 'parent_image', $item['media_details'] );
     2120        $this->assertEquals( $attachment, $item['media_details']['parent_image']['attachment_id'] );
     2121        $this->assertContains( 'canola', $item['media_details']['parent_image']['file'] );
     2122    }
     2123
     2124    /**
    20822125     * @ticket 50565
    20832126     */
Note: See TracChangeset for help on using the changeset viewer.