Skip to content

Commit 2904c80

Browse files
committed
Enhance image flipping functionality in Gutenberg REST Attachments Controller
- Updated the `Gutenberg_REST_Attachments_Controller` to handle both vertical and horizontal flipping of images by modifying the request structure. - Refactored the test case to reflect the new functionality, adding a separate test for vertical flipping.
1 parent 23a58ea commit 2904c80

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

lib/compat/wordpress-6.9/class-gutenberg-rest-attachments-controller.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,16 @@ public function edit_media_item( $request ) {
6262
} else {
6363
$modifiers = array();
6464

65-
if ( ! empty( $request['flip'] ) ) {
65+
if ( isset( $request['flip']['horizontal'] ) || isset( $request['flip']['vertical'] ) ) {
66+
$flip_args = array(
67+
'vertical' => $request['flip']['vertical'] ?? 0,
68+
'horizontal' => $request['flip']['horizontal'] ?? 0,
69+
);
70+
6671
$modifiers[] = array(
6772
'type' => 'flip',
6873
'args' => array(
69-
'flip' => $request['flip'],
74+
'flip' => $flip_args,
7075
),
7176
);
7277
}

phpunit/class-gutenberg-rest-attachments-controller-test.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ public function test_edit_image_updates_attachment_fields() {
131131
}
132132

133133
/**
134-
* Tests that the image is flipped correctly.
134+
* Tests that the image is flipped correctly vertically and horizontally.
135135
*
136136
* @ticket ???
137137
* @requires function imagejpeg
138138
*/
139-
public function test_edit_image_flip() {
139+
public function test_edit_image_vertical_and_horizontal_flip() {
140140
wp_set_current_user( self::$admin_id );
141141
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
142142

@@ -161,6 +161,36 @@ public function test_edit_image_flip() {
161161
$this->assertSame( array( true, true ), WP_Image_Editor_Mock::$spy['flip'][0] );
162162
}
163163

164+
/**
165+
* Tests that the image is flipped correctly vertically.
166+
*
167+
* @ticket ???
168+
* @requires function imagejpeg
169+
*/
170+
public function test_edit_image_vertical_flip() {
171+
wp_set_current_user( self::$admin_id );
172+
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
173+
174+
$this->setup_mock_editor();
175+
WP_Image_Editor_Mock::$edit_return['flip'] = new WP_Error();
176+
177+
$params = array(
178+
'flip' => array(
179+
'vertical' => 1,
180+
),
181+
'src' => wp_get_attachment_image_url( $attachment, 'full' ),
182+
);
183+
184+
$request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" );
185+
$request->set_body_params( $params );
186+
$response = rest_do_request( $request );
187+
$this->assertErrorResponse( 'rest_image_flip_failed', $response, 500 );
188+
189+
$this->assertCount( 1, WP_Image_Editor_Mock::$spy['flip'] );
190+
// The controller converts the integer values to booleans: 0 !== (int) 1 = true
191+
$this->assertSame( array( true, false ), WP_Image_Editor_Mock::$spy['flip'][0] );
192+
}
193+
164194
public function test_register_routes() {
165195
$this->markTestSkipped( 'No need to implement' );
166196
}

0 commit comments

Comments
 (0)