Changeset 48291
- Timestamp:
- 07/04/2020 04:13:17 AM (5 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
r47850 r48291 46 46 ) 47 47 ); 48 register_rest_route( 49 $this->namespace, 50 '/' . $this->rest_base . '/(?P<id>[\d]+)/edit', 51 array( 52 'methods' => WP_REST_Server::CREATABLE, 53 'callback' => array( $this, 'edit_media_item' ), 54 'permission_callback' => array( $this, 'edit_media_item_permissions_check' ), 55 'args' => $this->get_edit_media_item_args(), 56 ) 57 ); 48 58 } 49 59 … … 366 376 * Checks if a given request can perform post processing on an attachment. 367 377 * 368 * @si cne 5.3.0378 * @since 5.3.0 369 379 * 370 380 * @param WP_REST_Request $request Full details about the request. … … 373 383 public function post_process_item_permissions_check( $request ) { 374 384 return $this->update_item_permissions_check( $request ); 385 } 386 387 /** 388 * Checks if a given request has access to editing media. 389 * 390 * @since 5.5.0 391 * 392 * @param WP_REST_Request $request Full details about the request. 393 * @return true|WP_Error True if the request has read access, WP_Error object otherwise. 394 */ 395 public function edit_media_item_permissions_check( $request ) { 396 if ( ! current_user_can( 'upload_files' ) ) { 397 return new WP_Error( 398 'rest_cannot_edit_image', 399 __( 'Sorry, you are not allowed to upload media on this site.' ), 400 array( 'status' => rest_authorization_required_code() ) 401 ); 402 } 403 404 return $this->update_item_permissions_check( $request ); 405 } 406 407 /** 408 * Applies edits to a media item and creates a new attachment record. 409 * 410 * @since 5.5.0 411 * 412 * @param WP_REST_Request $request Full details about the request. 413 * @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure. 414 */ 415 public function edit_media_item( $request ) { 416 require_once ABSPATH . 'wp-admin/includes/image.php'; 417 418 $attachment_id = $request['id']; 419 420 // This also confirms the attachment is an image. 421 $image_file = wp_get_original_image_path( $attachment_id ); 422 $image_meta = wp_get_attachment_metadata( $attachment_id ); 423 424 if ( ! $image_meta || ! $image_file ) { 425 return new WP_Error( 426 'rest_unknown_attachment', 427 __( 'Unable to get meta information for file.' ), 428 array( 'status' => 404 ) 429 ); 430 } 431 432 $supported_types = array( 'image/jpeg', 'image/png', 'image/gif' ); 433 $mime_type = get_post_mime_type( $attachment_id ); 434 if ( ! in_array( $mime_type, $supported_types, true ) ) { 435 return new WP_Error( 436 'rest_cannot_edit_file_type', 437 __( 'This type of file cannot be edited.' ), 438 array( 'status' => 400 ) 439 ); 440 } 441 442 // Check if we need to do anything. 443 $rotate = 0; 444 $crop = false; 445 446 if ( ! empty( $request['rotation'] ) ) { 447 // Rotation direction: clockwise vs. counter clockwise. 448 $rotate = 0 - (int) $request['rotation']; 449 } 450 451 if ( isset( $request['x'], $request['y'], $request['width'], $request['height'] ) ) { 452 $crop = true; 453 } 454 455 if ( ! $rotate && ! $crop ) { 456 return new WP_Error( 457 'rest_image_not_edited', 458 __( 'The image was not edited. Edit the image before applying the changes.' ), 459 array( 'status' => 400 ) 460 ); 461 } 462 463 $image_editor = wp_get_image_editor( $image_file ); 464 465 if ( is_wp_error( $image_editor ) ) { 466 return new WP_Error( 467 'rest_unknown_image_file_type', 468 __( 'Unable to edit this image.' ), 469 array( 'status' => 500 ) 470 ); 471 } 472 473 if ( 0 !== $rotate ) { 474 $result = $image_editor->rotate( $rotate ); 475 476 if ( is_wp_error( $result ) ) { 477 return new WP_Error( 478 'rest_image_rotation_failed', 479 __( 'Unable to rotate this image.' ), 480 array( 'status' => 500 ) 481 ); 482 } 483 } 484 485 if ( $crop ) { 486 $size = $image_editor->get_size(); 487 488 $crop_x = round( ( $size['width'] * floatval( $request['x'] ) ) / 100.0 ); 489 $crop_y = round( ( $size['height'] * floatval( $request['y'] ) ) / 100.0 ); 490 $width = round( ( $size['width'] * floatval( $request['width'] ) ) / 100.0 ); 491 $height = round( ( $size['height'] * floatval( $request['height'] ) ) / 100.0 ); 492 493 $result = $image_editor->crop( $crop_x, $crop_y, $width, $height ); 494 495 if ( is_wp_error( $result ) ) { 496 return new WP_Error( 497 'rest_image_crop_failed', 498 __( 'Unable to crop this image.' ), 499 array( 'status' => 500 ) 500 ); 501 } 502 } 503 504 // Calculate the file name. 505 $image_ext = pathinfo( $image_file, PATHINFO_EXTENSION ); 506 $image_name = wp_basename( $image_file, ".{$image_ext}" ); 507 508 // Do not append multiple `-edited` to the file name. 509 // The user may be editing a previously edited image. 510 if ( preg_match( '/-edited(-\d+)?$/', $image_name ) ) { 511 // Remove any `-1`, `-2`, etc. `wp_unique_filename()` will add the proper number. 512 $image_name = preg_replace( '/-edited(-\d+)?$/', '-edited', $image_name ); 513 } else { 514 // Append `-edited` before the extension. 515 $image_name .= '-edited'; 516 } 517 518 $filename = "{$image_name}.{$image_ext}"; 519 520 // Create the uploads sub-directory if needed. 521 $uploads = wp_upload_dir(); 522 523 // Make the file name unique in the (new) upload directory. 524 $filename = wp_unique_filename( $uploads['path'], $filename ); 525 526 // Save to disk. 527 $saved = $image_editor->save( $uploads['path'] . "/$filename" ); 528 529 if ( is_wp_error( $saved ) ) { 530 return $saved; 531 } 532 533 // Create new attachment post. 534 $attachment_post = array( 535 'post_mime_type' => $saved['mime-type'], 536 'guid' => $uploads['url'] . "/$filename", 537 'post_title' => $filename, 538 'post_content' => '', 539 ); 540 541 $new_attachment_id = wp_insert_attachment( wp_slash( $attachment_post ), $saved['path'], 0, true ); 542 543 if ( is_wp_error( $new_attachment_id ) ) { 544 if ( 'db_update_error' === $new_attachment_id->get_error_code() ) { 545 $new_attachment_id->add_data( array( 'status' => 500 ) ); 546 } else { 547 $new_attachment_id->add_data( array( 'status' => 400 ) ); 548 } 549 550 return $new_attachment_id; 551 } 552 553 // Generate image sub-sizes and meta. 554 $new_image_meta = wp_generate_attachment_metadata( $new_attachment_id, $saved['path'] ); 555 556 // Copy the EXIF metadata from the original attachment if not generated for the edited image. 557 if ( ! empty( $image_meta['image_meta'] ) ) { 558 $empty_image_meta = true; 559 560 if ( isset( $new_image_meta['image_meta'] ) && is_array( $new_image_meta['image_meta'] ) ) { 561 $empty_image_meta = empty( array_filter( array_values( $new_image_meta['image_meta'] ) ) ); 562 } 563 564 if ( $empty_image_meta ) { 565 $new_image_meta['image_meta'] = $image_meta['image_meta']; 566 } 567 } 568 569 // Reset orientation. At this point the image is edited and orientation is correct. 570 if ( ! empty( $new_image_meta['image_meta']['orientation'] ) ) { 571 $new_image_meta['image_meta']['orientation'] = 1; 572 } 573 574 // The attachment_id may change if the site is exported and imported. 575 $new_image_meta['parent_image'] = array( 576 'attachment_id' => $attachment_id, 577 // Path to the originally uploaded image file relative to the uploads directory. 578 'file' => _wp_relative_upload_path( $image_file ), 579 ); 580 581 /** 582 * Filters the updated attachment meta data. 583 * 584 * @since 5.5.0 585 * 586 * @param array $data Array of updated attachment meta data. 587 * @param int $new_attachment_id Attachment post ID. 588 * @param int $attachment_id Original Attachment post ID. 589 */ 590 $new_image_meta = apply_filters( 'wp_edited_attachment_metadata', $new_image_meta, $new_attachment_id, $attachment_id ); 591 592 wp_update_attachment_metadata( $new_attachment_id, $new_image_meta ); 593 594 $response = $this->prepare_item_for_response( get_post( $new_attachment_id ), $request ); 595 $response->set_status( 201 ); 596 $response->header( 'Location', rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $new_attachment_id ) ) ); 597 598 return $response; 375 599 } 376 600 … … 1015 1239 } 1016 1240 1241 /** 1242 * Gets the request args for the edit item route. 1243 * 1244 * @since 5.5.0 1245 * 1246 * @return array 1247 */ 1248 protected function get_edit_media_item_args() { 1249 return array( 1250 'rotation' => array( 1251 'description' => __( 'The amount to rotate the image clockwise in degrees.' ), 1252 'type' => 'integer', 1253 'minimum' => 0, 1254 'exclusiveMinimum' => true, 1255 'maximum' => 360, 1256 'exclusiveMaximum' => true, 1257 ), 1258 'x' => array( 1259 'description' => __( 'As a percentage of the image, the x position to start the crop from.' ), 1260 'type' => 'number', 1261 'minimum' => 0, 1262 'maximum' => 100, 1263 ), 1264 'y' => array( 1265 'description' => __( 'As a percentage of the image, the y position to start the crop from.' ), 1266 'type' => 'number', 1267 'minimum' => 0, 1268 'maximum' => 100, 1269 ), 1270 'width' => array( 1271 'description' => __( 'As a percentage of the image, the width to crop the image to.' ), 1272 'type' => 'number', 1273 'minimum' => 0, 1274 'maximum' => 100, 1275 ), 1276 'height' => array( 1277 'description' => __( 'As a percentage of the image, the height to crop the image to.' ), 1278 'type' => 'number', 1279 'minimum' => 0, 1280 'maximum' => 100, 1281 ), 1282 ); 1283 } 1284 1017 1285 } -
trunk/tests/phpunit/includes/mock-image-editor.php
r46586 r48291 8 8 public static $test_return = true; 9 9 public static $save_return = array(); 10 public static $spy = array(); 11 public static $edit_return = array(); 12 public static $size_return = null; 10 13 11 14 // Allow testing of jpeg_quality filter. … … 24 27 } 25 28 public function resize( $max_w, $max_h, $crop = false ) { 26 29 self::$spy[ __FUNCTION__ ][] = func_get_args(); 30 if ( isset( self::$edit_return[ __FUNCTION__ ] ) ) { 31 return self::$edit_return[ __FUNCTION__ ]; 32 } 27 33 } 28 34 public function multi_resize( $sizes ) { 29 35 self::$spy[ __FUNCTION__ ][] = func_get_args(); 36 if ( isset( self::$edit_return[ __FUNCTION__ ] ) ) { 37 return self::$edit_return[ __FUNCTION__ ]; 38 } 30 39 } 31 40 public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { 32 41 self::$spy[ __FUNCTION__ ][] = func_get_args(); 42 if ( isset( self::$edit_return[ __FUNCTION__ ] ) ) { 43 return self::$edit_return[ __FUNCTION__ ]; 44 } 33 45 } 34 46 public function rotate( $angle ) { 35 47 self::$spy[ __FUNCTION__ ][] = func_get_args(); 48 if ( isset( self::$edit_return[ __FUNCTION__ ] ) ) { 49 return self::$edit_return[ __FUNCTION__ ]; 50 } 36 51 } 37 52 public function flip( $horz, $vert ) { 38 53 self::$spy[ __FUNCTION__ ][] = func_get_args(); 54 if ( isset( self::$edit_return[ __FUNCTION__ ] ) ) { 55 return self::$edit_return[ __FUNCTION__ ]; 56 } 39 57 } 40 58 public function save( $destfilename = null, $mime_type = null ) { … … 44 62 45 63 } 64 65 public function get_size() { 66 if ( self::$size_return ) { 67 return self::$size_return; 68 } 69 70 return parent::get_size(); 71 } 46 72 } 47 73 -
trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php
r47261 r48291 20 20 protected static $rest_insert_attachment_count; 21 21 22 /** 23 * @var string The path to a test file. 24 */ 25 private $test_file; 26 27 /** 28 * @var string The path to a second test file. 29 */ 30 private $test_file2; 31 22 32 public static function wpSetUpBeforeClass( $factory ) { 23 33 self::$superadmin_id = $factory->user->create( … … 76 86 $this->test_file2 = '/tmp/codeispoetry.png'; 77 87 copy( $orig_file2, $this->test_file2 ); 88 } 89 90 public function tearDown() { 91 parent::tearDown(); 92 93 if ( file_exists( $this->test_file ) ) { 94 unlink( $this->test_file ); 95 } 96 if ( file_exists( $this->test_file2 ) ) { 97 unlink( $this->test_file2 ); 98 } 99 100 remove_action( 'rest_insert_attachment', array( $this, 'filter_rest_insert_attachment' ) ); 101 remove_action( 'rest_after_insert_attachment', array( $this, 'filter_rest_after_insert_attachment' ) ); 102 103 $this->remove_added_uploads(); 104 105 if ( class_exists( WP_Image_Editor_Mock::class ) ) { 106 WP_Image_Editor_Mock::$spy = array(); 107 WP_Image_Editor_Mock::$edit_return = array(); 108 WP_Image_Editor_Mock::$size_return = null; 109 } 78 110 } 79 111 … … 1538 1570 } 1539 1571 1540 public function tearDown() {1541 parent::tearDown();1542 if ( file_exists( $this->test_file ) ) {1543 unlink( $this->test_file );1544 }1545 if ( file_exists( $this->test_file2 ) ) {1546 unlink( $this->test_file2 );1547 }1548 1549 remove_action( 'rest_insert_attachment', array( $this, 'filter_rest_insert_attachment' ) );1550 remove_action( 'rest_after_insert_attachment', array( $this, 'filter_rest_after_insert_attachment' ) );1551 1552 $this->remove_added_uploads();1553 }1554 1555 1572 protected function check_post_data( $attachment, $data, $context = 'view', $links ) { 1556 1573 parent::check_post_data( $attachment, $data, $context, $links ); … … 1777 1794 self::$rest_after_insert_attachment_count++; 1778 1795 } 1796 1797 /** 1798 * @ticket 44405 1799 */ 1800 public function test_edit_image_returns_error_if_logged_out() { 1801 $attachment = self::factory()->attachment->create(); 1802 1803 $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" ); 1804 $response = rest_do_request( $request ); 1805 $this->assertErrorResponse( 'rest_cannot_edit_image', $response, 401 ); 1806 } 1807 1808 /** 1809 * @ticket 44405 1810 */ 1811 public function test_edit_image_returns_error_if_cannot_upload() { 1812 $user = self::factory()->user->create_and_get( array( 'role' => 'editor' ) ); 1813 $user->add_cap( 'upload_files', false ); 1814 1815 wp_set_current_user( $user->ID ); 1816 $attachment = self::factory()->attachment->create( array( 'post_author' => $user->ID ) ); 1817 1818 $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" ); 1819 $response = rest_do_request( $request ); 1820 $this->assertErrorResponse( 'rest_cannot_edit_image', $response, 403 ); 1821 } 1822 1823 /** 1824 * @ticket 44405 1825 */ 1826 public function test_edit_image_returns_error_if_cannot_edit() { 1827 wp_set_current_user( self::$uploader_id ); 1828 $attachment = self::factory()->attachment->create(); 1829 1830 $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" ); 1831 $response = rest_do_request( $request ); 1832 $this->assertErrorResponse( 'rest_cannot_edit', $response, 403 ); 1833 } 1834 1835 /** 1836 * @ticket 44405 1837 */ 1838 public function test_edit_image_returns_error_if_no_attachment() { 1839 wp_set_current_user( self::$superadmin_id ); 1840 $attachment = self::factory()->attachment->create(); 1841 1842 $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" ); 1843 $response = rest_do_request( $request ); 1844 $this->assertErrorResponse( 'rest_unknown_attachment', $response, 404 ); 1845 } 1846 1847 /** 1848 * @ticket 44405 1849 */ 1850 public function test_edit_image_returns_error_if_unsupported_mime_type() { 1851 wp_set_current_user( self::$superadmin_id ); 1852 $attachment = self::factory()->attachment->create_upload_object( $this->test_file ); 1853 wp_update_post( 1854 array( 1855 'ID' => $attachment, 1856 'post_mime_type' => 'image/invalid', 1857 ) 1858 ); 1859 1860 $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" ); 1861 $response = rest_do_request( $request ); 1862 $this->assertErrorResponse( 'rest_cannot_edit_file_type', $response, 400 ); 1863 } 1864 1865 /** 1866 * @ticket 44405 1867 */ 1868 public function test_edit_image_returns_error_if_no_edits() { 1869 wp_set_current_user( self::$superadmin_id ); 1870 $attachment = self::factory()->attachment->create_upload_object( $this->test_file ); 1871 1872 $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" ); 1873 $response = rest_do_request( $request ); 1874 $this->assertErrorResponse( 'rest_image_not_edited', $response, 400 ); 1875 } 1876 1877 /** 1878 * @ticket 44405 1879 */ 1880 public function test_edit_image_rotate() { 1881 wp_set_current_user( self::$superadmin_id ); 1882 $attachment = self::factory()->attachment->create_upload_object( $this->test_file ); 1883 1884 $this->setup_mock_editor(); 1885 WP_Image_Editor_Mock::$edit_return['rotate'] = new WP_Error(); 1886 1887 $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" ); 1888 $request->set_body_params( array( 'rotation' => 60 ) ); 1889 $response = rest_do_request( $request ); 1890 $this->assertErrorResponse( 'rest_image_rotation_failed', $response, 500 ); 1891 1892 $this->assertCount( 1, WP_Image_Editor_Mock::$spy['rotate'] ); 1893 $this->assertEquals( array( -60 ), WP_Image_Editor_Mock::$spy['rotate'][0] ); 1894 } 1895 1896 /** 1897 * @ticket 44405 1898 */ 1899 public function test_edit_image_crop() { 1900 wp_set_current_user( self::$superadmin_id ); 1901 $attachment = self::factory()->attachment->create_upload_object( $this->test_file ); 1902 1903 $this->setup_mock_editor(); 1904 WP_Image_Editor_Mock::$size_return = array( 1905 'width' => 640, 1906 'height' => 480, 1907 ); 1908 1909 WP_Image_Editor_Mock::$edit_return['crop'] = new WP_Error(); 1910 1911 $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" ); 1912 $request->set_body_params( 1913 array( 1914 'x' => 50, 1915 'y' => 10, 1916 'width' => 10, 1917 'height' => 5, 1918 ) 1919 ); 1920 $response = rest_do_request( $request ); 1921 $this->assertErrorResponse( 'rest_image_crop_failed', $response, 500 ); 1922 1923 $this->assertCount( 1, WP_Image_Editor_Mock::$spy['crop'] ); 1924 $this->assertEquals( 1925 array( 320.0, 48.0, 64.0, 24.0 ), 1926 WP_Image_Editor_Mock::$spy['crop'][0] 1927 ); 1928 } 1929 1930 /** 1931 * @ticket 44405 1932 */ 1933 public function test_edit_image() { 1934 wp_set_current_user( self::$superadmin_id ); 1935 $attachment = self::factory()->attachment->create_upload_object( $this->test_file ); 1936 1937 $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" ); 1938 $request->set_body_params( array( 'rotation' => 60 ) ); 1939 $response = rest_do_request( $request ); 1940 $item = $response->get_data(); 1941 1942 $this->assertEquals( 201, $response->get_status() ); 1943 $this->assertEquals( rest_url( '/wp/v2/media/' . $item['id'] ), $response->get_headers()['Location'] ); 1944 1945 $this->assertStringEndsWith( '-edited.jpg', $item['media_details']['file'] ); 1946 $this->assertArrayHasKey( 'parent_image', $item['media_details'] ); 1947 $this->assertEquals( $attachment, $item['media_details']['parent_image']['attachment_id'] ); 1948 $this->assertContains( 'canola', $item['media_details']['parent_image']['file'] ); 1949 } 1950 1951 /** 1952 * Sets up the mock image editor. 1953 * 1954 * @since 5.5.0 1955 */ 1956 protected function setup_mock_editor() { 1957 require_once ABSPATH . WPINC . '/class-wp-image-editor.php'; 1958 require_once DIR_TESTDATA . '/../includes/mock-image-editor.php'; 1959 1960 add_filter( 1961 'wp_image_editors', 1962 static function () { 1963 return array( 'WP_Image_Editor_Mock' ); 1964 } 1965 ); 1966 } 1779 1967 } -
trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php
r48242 r48291 101 101 '/wp/v2/media/(?P<id>[\\d]+)', 102 102 '/wp/v2/media/(?P<id>[\\d]+)/post-process', 103 '/wp/v2/media/(?P<id>[\\d]+)/edit', 103 104 '/wp/v2/blocks', 104 105 '/wp/v2/blocks/(?P<id>[\d]+)', -
trunk/tests/qunit/fixtures/wp-api-generated.js
r48242 r48291 2338 2338 ], 2339 2339 "type": "string" 2340 } 2341 } 2342 } 2343 ] 2344 }, 2345 "/wp/v2/media/(?P<id>[\\d]+)/edit": { 2346 "namespace": "wp/v2", 2347 "methods": [ 2348 "POST" 2349 ], 2350 "endpoints": [ 2351 { 2352 "methods": [ 2353 "POST" 2354 ], 2355 "args": { 2356 "rotation": { 2357 "required": false, 2358 "description": "The amount to rotate the image clockwise in degrees.", 2359 "type": "integer" 2360 }, 2361 "x": { 2362 "required": false, 2363 "description": "As a percentage of the image, the x position to start the crop from.", 2364 "type": "number" 2365 }, 2366 "y": { 2367 "required": false, 2368 "description": "As a percentage of the image, the y position to start the crop from.", 2369 "type": "number" 2370 }, 2371 "width": { 2372 "required": false, 2373 "description": "As a percentage of the image, the width to crop the image to.", 2374 "type": "number" 2375 }, 2376 "height": { 2377 "required": false, 2378 "description": "As a percentage of the image, the height to crop the image to.", 2379 "type": "number" 2340 2380 } 2341 2381 }
Note: See TracChangeset
for help on using the changeset viewer.