diff --git a/lib/class-wp-rest-image-editor-controller.php b/lib/class-wp-rest-image-editor-controller.php index fbe61aae8f96d3..8be0dd425834e0 100644 --- a/lib/class-wp-rest-image-editor-controller.php +++ b/lib/class-wp-rest-image-editor-controller.php @@ -29,7 +29,7 @@ class WP_REST_Image_Editor_Controller extends WP_REST_Controller { */ public function __construct() { $this->namespace = '__experimental'; - $this->rest_base = '/richimage/(?P[\d]+)'; + $this->rest_base = '/richimage/(?P[\d]+)'; $this->editor = new Image_Editor(); } @@ -50,8 +50,9 @@ public function register_routes() { 'permission_callback' => array( $this, 'permission_callback' ), 'args' => array( 'angle' => array( - 'type' => 'integer', - 'required' => true, + 'description' => __( 'Rotation angle', 'gutenberg' ), + 'type' => 'integer', + 'required' => true, ), ), ), @@ -68,9 +69,10 @@ public function register_routes() { 'permission_callback' => array( $this, 'permission_callback' ), 'args' => array( 'direction' => array( - 'type' => 'enum', - 'enum' => array( 'vertical', 'horizontal' ), - 'required' => true, + 'description' => __( 'Flip direction', 'gutenberg' ), + 'type' => 'string', + 'enum' => array( 'vertical', 'horizontal' ), + 'required' => true, ), ), ), @@ -86,25 +88,29 @@ public function register_routes() { 'callback' => array( $this, 'crop_image' ), 'permission_callback' => array( $this, 'permission_callback' ), 'args' => array( - 'cropX' => array( - 'type' => 'float', - 'minimum' => 0, - 'required' => true, + 'crop_x' => array( + 'description' => __( 'Crop offset percentage from left', 'gutenberg' ), + 'type' => 'number', + 'minimum' => 0, + 'required' => true, ), - 'cropY' => array( - 'type' => 'float', - 'minimum' => 0, - 'required' => true, + 'crop_y' => array( + 'description' => __( 'Crop offset percentage from top', 'gutenberg' ), + 'type' => 'number', + 'minimum' => 0, + 'required' => true, ), - 'cropWidth' => array( - 'type' => 'float', - 'minimum' => 1, - 'required' => true, + 'crop_width' => array( + 'description' => __( 'Crop width percentage', 'gutenberg' ), + 'type' => 'number', + 'minimum' => 1, + 'required' => true, ), - 'cropHeight' => array( - 'type' => 'float', - 'minimum' => 1, - 'required' => true, + 'crop_height' => array( + 'description' => __( 'Crop height percentage', 'gutenberg' ), + 'type' => 'number', + 'minimum' => 1, + 'required' => true, ), ), ), @@ -122,9 +128,7 @@ public function register_routes() { * @return true|WP_Error True if the request has read access, WP_Error object otherwise. */ public function permission_callback( $request ) { - $params = $request->get_params(); - - if ( ! current_user_can( 'edit_post', $params['mediaID'] ) ) { + if ( ! current_user_can( 'edit_post', $request['media_id'] ) ) { return new WP_Error( 'rest_cannot_edit_image', __( 'Sorry, you are not allowed to edit images.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); } @@ -141,11 +145,9 @@ public function permission_callback( $request ) { * @return array|WP_Error If successful image JSON for the modified image, otherwise a WP_Error. */ public function rotate_image( $request ) { - $params = $request->get_params(); - - $modifier = new Image_Editor_Rotate( $params['angle'] ); + $modifier = new Image_Editor_Rotate( $request['angle'] ); - return $this->editor->modify_image( $params['mediaID'], $modifier ); + return $this->editor->modify_image( $request['media_id'], $modifier ); } /** @@ -158,11 +160,9 @@ public function rotate_image( $request ) { * @return array|WP_Error If successful image JSON for the modified image, otherwise a WP_Error. */ public function flip_image( $request ) { - $params = $request->get_params(); + $modifier = new Image_Editor_Flip( $request['direction'] ); - $modifier = new Image_Editor_Flip( $params['direction'] ); - - return $this->editor->modify_image( $params['mediaID'], $modifier ); + return $this->editor->modify_image( $request['media_id'], $modifier ); } /** @@ -175,10 +175,8 @@ public function flip_image( $request ) { * @return array|WP_Error If successful image JSON for the modified image, otherwise a WP_Error. */ public function crop_image( $request ) { - $params = $request->get_params(); - - $modifier = new Image_Editor_Crop( $params['cropX'], $params['cropY'], $params['cropWidth'], $params['cropHeight'] ); + $modifier = new Image_Editor_Crop( $request['crop_x'], $request['crop_y'], $request['crop_width'], $request['crop_height'] ); - return $this->editor->modify_image( $params['mediaID'], $modifier ); + return $this->editor->modify_image( $request['media_id'], $modifier ); } } diff --git a/lib/image-editor/class-image-editor-crop.php b/lib/image-editor/class-image-editor-crop.php index 14179e911a2332..422d0994bfaade 100644 --- a/lib/image-editor/class-image-editor-crop.php +++ b/lib/image-editor/class-image-editor-crop.php @@ -12,30 +12,30 @@ */ class Image_Editor_Crop extends Image_Editor_Modifier { /** - * Pixels from the left for the crop. + * Distance from the left for the crop. * - * @var integer + * @var float */ private $crop_x = 0; /** - * Pixels from the top for the crop. + * Distance from the top for the crop. * - * @var integer + * @var float */ private $crop_y = 0; /** - * Width in pixels for the crop. + * Width of the crop. * - * @var integer + * @var float */ private $width = 0; /** - * Height in pixels for the crop. + * Height of the crop. * - * @var integer + * @var float */ private $height = 0; @@ -44,10 +44,10 @@ class Image_Editor_Crop extends Image_Editor_Modifier { * * Will populate object properties from the provided arguments. * - * @param integer $crop_x Pixels from the left for the crop. - * @param integer $crop_y Pixels from the top for the crop. - * @param integer $width Width in pixels for the crop. - * @param integer $height Height in pixels for the crop. + * @param float $crop_x Percentage from the left for the crop. + * @param float $crop_y Percentage from the top for the crop. + * @param float $width Percentage width for the crop. + * @param float $height Percentage height for the crop. */ public function __construct( $crop_x, $crop_y, $width, $height ) { $this->crop_x = floatval( $crop_x ); @@ -65,10 +65,10 @@ public function __construct( $crop_x, $crop_y, $width, $height ) { * @return array Updated metadata. */ public function apply_to_meta( $meta ) { - $meta['cropX'] = $this->crop_x; - $meta['cropY'] = $this->crop_y; - $meta['cropWidth'] = $this->width; - $meta['cropHeight'] = $this->height; + $meta['crop_x'] = $this->crop_x; + $meta['crop_y'] = $this->crop_y; + $meta['crop_width'] = $this->width; + $meta['crop_height'] = $this->height; return $meta; } @@ -101,8 +101,8 @@ public function apply_to_image( $image ) { * @return string Filename for the edited image. */ public static function get_filename( $meta ) { - if ( isset( $meta['cropWidth'] ) && $meta['cropWidth'] > 0 ) { - $target_file = sprintf( 'crop-%d-%d-%d-%d', round( $meta['cropX'], 2 ), round( $meta['cropY'], 2 ), round( $meta['cropWidth'], 2 ), round( $meta['cropHeight'], 2 ) ); + if ( isset( $meta['crop_width'] ) && $meta['crop_width'] > 0 ) { + $target_file = sprintf( 'crop-%d-%d-%d-%d', round( $meta['crop_x'], 2 ), round( $meta['crop_y'], 2 ), round( $meta['crop_width'], 2 ), round( $meta['crop_height'], 2 ) ); // We need to change the original name to include the crop. This way if it's cropped again we won't clash. $meta['original_name'] = $target_file; diff --git a/lib/image-editor/class-image-editor-flip.php b/lib/image-editor/class-image-editor-flip.php index f795913f39c45a..f9b4797c99e94a 100644 --- a/lib/image-editor/class-image-editor-flip.php +++ b/lib/image-editor/class-image-editor-flip.php @@ -43,9 +43,9 @@ public function __construct( $direction ) { */ public function apply_to_meta( $meta ) { if ( $this->is_vertical() ) { - $meta['flipv'] = ! $meta['flipv']; + $meta['flip_vertical'] = ! $meta['flip_vertical']; } elseif ( $this->is_horizontal() ) { - $meta['flipH'] = ! $meta['flipH']; + $meta['flip_horizontal'] = ! $meta['flip_horizontal']; } return $meta; @@ -96,12 +96,12 @@ private function is_horizontal() { public static function get_filename( $meta ) { $parts = array(); - if ( $meta['flipH'] ) { - $parts[] = 'fliph'; + if ( $meta['flip_horizontal'] ) { + $parts[] = 'flip_horizontal'; } - if ( $meta['flipv'] ) { - $parts[] = 'flipv'; + if ( $meta['flip_vertical'] ) { + $parts[] = 'flip_vertical'; } if ( count( $parts ) > 0 ) { @@ -120,8 +120,8 @@ public static function get_filename( $meta ) { */ public static function get_default_meta() { return array( - 'flipH' => false, - 'flipv' => false, + 'flip_horizontal' => false, + 'flip_vertical' => false, ); } } diff --git a/lib/image-editor/class-image-editor.php b/lib/image-editor/class-image-editor.php index 36f41dfb04a5d0..a201f65453a375 100644 --- a/lib/image-editor/class-image-editor.php +++ b/lib/image-editor/class-image-editor.php @@ -46,7 +46,7 @@ public function __construct() { * * @param integer $media_id Media id. * @param Image_Editor_Modifier $modifier Modifier to apply to the image. - * @return array|WP_Error If successful image JSON containing the mediaId and url of modified image, otherwise WP_Error. + * @return array|WP_Error If successful image JSON containing the media_id and url of modified image, otherwise WP_Error. */ public function modify_image( $media_id, $modifier ) { // Get image information. @@ -118,8 +118,8 @@ private function load_image( $media_id ) { */ private function get_image_as_json( $id ) { return array( - 'mediaID' => $id, - 'url' => wp_get_attachment_image_url( $id, 'original' ), + 'media_id' => $id, + 'url' => wp_get_attachment_image_url( $id, 'original' ), ); } diff --git a/packages/block-library/src/rich-image/rich-image/index.js b/packages/block-library/src/rich-image/rich-image/index.js index b393dc8443435d..f3d42b072e92f8 100644 --- a/packages/block-library/src/rich-image/rich-image/index.js +++ b/packages/block-library/src/rich-image/rich-image/index.js @@ -189,9 +189,9 @@ function RichImage( props ) { setIsProgress( null ); setIsCropping( false ); - if ( response.mediaID && response.mediaID !== id ) { + if ( response.media_id && response.media_id !== id ) { setAttributes( { - id: response.mediaID, + id: response.media_id, url: response.url, } ); } @@ -213,10 +213,10 @@ function RichImage( props ) { function cropImage() { adjustImage( 'crop', { - cropX: crop.x, - cropY: crop.y, - cropWidth: crop.width, - cropHeight: crop.height, + crop_x: crop.x, + crop_y: crop.y, + crop_width: crop.width, + crop_height: crop.height, } ); }