Skip to content

Commit 7a91a74

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 0c1a756 commit 7a91a74

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',
@@ -1558,20 +1558,20 @@ protected function get_edit_media_item_args() {
15581558
),
15591559
'properties' => array(
15601560
'flip' => array(
1561-
'description' => __( 'Flip direction. [ horizontal, vertical ] 0 for no flip, 1 for flip.' ),
1561+
'description' => __( 'Flip direction. [ horizontal, vertical ]' ),
15621562
'type' => 'object',
15631563
'required' => array(
15641564
'horizontal',
15651565
'vertical',
15661566
),
15671567
'properties' => array(
15681568
'horizontal' => array(
1569-
'description' => __( 'Horizontal flip direction. 0 for no flip, 1 for flip.' ),
1570-
'type' => 'number',
1569+
'description' => __( 'Whether to flip in the horizontal direction.' ),
1570+
'type' => 'boolean',
15711571
),
15721572
'vertical' => array(
1573-
'description' => __( 'Vertical flip direction. 0 for no flip, 1 for flip.' ),
1574-
'type' => 'number',
1573+
'description' => __( 'Whether to flip in the vertical direction.' ),
1574+
'type' => 'boolean',
15751575
),
15761576
),
15771577
),

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

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

27522752
$params = array(
27532753
'flip' => array(
2754-
'vertical' => 1,
2755-
'horizontal' => 1,
2754+
'vertical' => true,
2755+
'horizontal' => true,
27562756
),
27572757
'src' => wp_get_attachment_image_url( $attachment, 'full' ),
27582758
);
@@ -2768,12 +2768,12 @@ public function test_edit_image_vertical_and_horizontal_flip() {
27682768
}
27692769

27702770
/**
2771-
* Tests that the image is flipped correctly vertically.
2771+
* Tests that the image is flipped correctly vertically only.
27722772
*
27732773
* @ticket 64035
27742774
* @requires function imagejpeg
27752775
*/
2776-
public function test_edit_image_vertical_flip() {
2776+
public function test_edit_image_vertical_flip_with_horizontal_false() {
27772777
wp_set_current_user( self::$superadmin_id );
27782778
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
27792779

@@ -2782,7 +2782,38 @@ public function test_edit_image_vertical_flip() {
27822782

27832783
$params = array(
27842784
'flip' => array(
2785-
'vertical' => 1,
2785+
'vertical' => true,
2786+
'horizontal' => false,
2787+
),
2788+
'src' => wp_get_attachment_image_url( $attachment, 'full' ),
2789+
);
2790+
2791+
$request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" );
2792+
$request->set_body_params( $params );
2793+
$response = rest_do_request( $request );
2794+
$this->assertErrorResponse( 'rest_image_flip_failed', $response, 500 );
2795+
2796+
$this->assertCount( 1, WP_Image_Editor_Mock::$spy['flip'] );
2797+
// The controller converts the integer values to booleans: 0 !== (int) 1 = true.
2798+
$this->assertSame( array( true, false ), WP_Image_Editor_Mock::$spy['flip'][0], 'Vertical flip of the image is not identical.' );
2799+
}
2800+
2801+
/**
2802+
* Tests that the image is flipped correctly with only vertical flip in arguments.
2803+
*
2804+
* @ticket 64035
2805+
* @requires function imagejpeg
2806+
*/
2807+
public function test_edit_image_vertical_flip_only() {
2808+
wp_set_current_user( self::$superadmin_id );
2809+
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
2810+
2811+
$this->setup_mock_editor();
2812+
WP_Image_Editor_Mock::$edit_return['flip'] = new WP_Error();
2813+
2814+
$params = array(
2815+
'flip' => array(
2816+
'vertical' => true,
27862817
),
27872818
'src' => wp_get_attachment_image_url( $attachment, 'full' ),
27882819
);

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)