Skip to content

Commit f48fc65

Browse files
committed
Refactor flip parameters in WP REST Attachments Controller to use boolean values instead of integers. Update related tests to reflect changes in parameter types and description
1 parent da30b77 commit f48fc65

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,8 @@ public function edit_media_item( $request ) {
587587

588588
if ( isset( $request['flip']['horizontal'] ) || isset( $request['flip']['vertical'] ) ) {
589589
$flip_args = array(
590-
'vertical' => $request['flip']['vertical'] ?? 0,
591-
'horizontal' => $request['flip']['horizontal'] ?? 0,
590+
'vertical' => isset( $request['flip']['vertical'] ) ? (bool) $request['flip']['vertical'] : false,
591+
'horizontal' => isset( $request['flip']['horizontal'] ) ? (bool) $request['flip']['horizontal'] : false,
592592
);
593593

594594
$modifiers[] = array(
@@ -658,7 +658,7 @@ public function edit_media_item( $request ) {
658658
* The vertical flip is the first argument (flip along horizontal axis), the horizontal flip is the second argument (flip along vertical axis).
659659
* See: WP_Image_Editor::flip()
660660
*/
661-
$result = $image_editor->flip( 0 !== (int) $args['flip']['vertical'], 0 !== (int) $args['flip']['horizontal'] );
661+
$result = $image_editor->flip( $args['flip']['vertical'], $args['flip']['horizontal'] );
662662
if ( is_wp_error( $result ) ) {
663663
return new WP_Error(
664664
'rest_image_flip_failed',
@@ -1531,20 +1531,20 @@ protected function get_edit_media_item_args() {
15311531
),
15321532
'properties' => array(
15331533
'flip' => array(
1534-
'description' => __( 'Flip direction. [ horizontal, vertical ] 0 for no flip, 1 for flip.' ),
1534+
'description' => __( 'Flip direction. [ horizontal, vertical ]' ),
15351535
'type' => 'object',
15361536
'required' => array(
15371537
'horizontal',
15381538
'vertical',
15391539
),
15401540
'properties' => array(
15411541
'horizontal' => array(
1542-
'description' => __( 'Horizontal flip direction. 0 for no flip, 1 for flip.' ),
1543-
'type' => 'number',
1542+
'description' => __( 'Whether to flip in the horizontal direction.' ),
1543+
'type' => 'boolean',
15441544
),
15451545
'vertical' => array(
1546-
'description' => __( 'Vertical flip direction. 0 for no flip, 1 for flip.' ),
1547-
'type' => 'number',
1546+
'description' => __( 'Whether to flip in the vertical direction.' ),
1547+
'type' => 'boolean',
15481548
),
15491549
),
15501550
),

tests/phpunit/tests/rest-api/rest-attachments-controller.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,8 +2709,8 @@ public function test_edit_image_vertical_and_horizontal_flip() {
27092709

27102710
$params = array(
27112711
'flip' => array(
2712-
'vertical' => 1,
2713-
'horizontal' => 1,
2712+
'vertical' => true,
2713+
'horizontal' => true,
27142714
),
27152715
'src' => wp_get_attachment_image_url( $attachment, 'full' ),
27162716
);
@@ -2726,12 +2726,12 @@ public function test_edit_image_vertical_and_horizontal_flip() {
27262726
}
27272727

27282728
/**
2729-
* Tests that the image is flipped correctly vertically.
2729+
* Tests that the image is flipped correctly vertically only.
27302730
*
27312731
* @ticket 64035
27322732
* @requires function imagejpeg
27332733
*/
2734-
public function test_edit_image_vertical_flip() {
2734+
public function test_edit_image_vertical_flip_with_horizontal_false() {
27352735
wp_set_current_user( self::$superadmin_id );
27362736
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
27372737

@@ -2740,7 +2740,38 @@ public function test_edit_image_vertical_flip() {
27402740

27412741
$params = array(
27422742
'flip' => array(
2743-
'vertical' => 1,
2743+
'vertical' => true,
2744+
'horizontal' => false,
2745+
),
2746+
'src' => wp_get_attachment_image_url( $attachment, 'full' ),
2747+
);
2748+
2749+
$request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" );
2750+
$request->set_body_params( $params );
2751+
$response = rest_do_request( $request );
2752+
$this->assertErrorResponse( 'rest_image_flip_failed', $response, 500 );
2753+
2754+
$this->assertCount( 1, WP_Image_Editor_Mock::$spy['flip'] );
2755+
// The controller converts the integer values to booleans: 0 !== (int) 1 = true.
2756+
$this->assertSame( array( true, false ), WP_Image_Editor_Mock::$spy['flip'][0], 'Vertical flip of the image is not identical.' );
2757+
}
2758+
2759+
/**
2760+
* Tests that the image is flipped correctly with only vertical flip in arguments.
2761+
*
2762+
* @ticket 64035
2763+
* @requires function imagejpeg
2764+
*/
2765+
public function test_edit_image_vertical_flip_only() {
2766+
wp_set_current_user( self::$superadmin_id );
2767+
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
2768+
2769+
$this->setup_mock_editor();
2770+
WP_Image_Editor_Mock::$edit_return['flip'] = new WP_Error();
2771+
2772+
$params = array(
2773+
'flip' => array(
2774+
'vertical' => true,
27442775
),
27452776
'src' => wp_get_attachment_image_url( $attachment, 'full' ),
27462777
);

tests/qunit/fixtures/wp-api-generated.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3445,20 +3445,20 @@ mockedApiResponse.Schema = {
34453445
],
34463446
"properties": {
34473447
"flip": {
3448-
"description": "Flip direction. [ horizontal, vertical ] 0 for no flip, 1 for flip.",
3448+
"description": "Flip direction. [ horizontal, vertical ]",
34493449
"type": "object",
34503450
"required": [
34513451
"horizontal",
34523452
"vertical"
34533453
],
34543454
"properties": {
34553455
"horizontal": {
3456-
"description": "Horizontal flip direction. 0 for no flip, 1 for flip.",
3457-
"type": "number"
3456+
"description": "Whether to flip in the horizontal direction.",
3457+
"type": "boolean"
34583458
},
34593459
"vertical": {
3460-
"description": "Vertical flip direction. 0 for no flip, 1 for flip.",
3461-
"type": "number"
3460+
"description": "Whether to flip in the vertical direction.",
3461+
"type": "boolean"
34623462
}
34633463
}
34643464
}

0 commit comments

Comments
 (0)