Skip to content

Commit

Permalink
Image Edit: REST API Code Cleanup (#23037)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlende authored Jun 11, 2020
1 parent 683e41d commit 1fd4ed3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 72 deletions.
72 changes: 35 additions & 37 deletions lib/class-wp-rest-image-editor-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mediaID>[\d]+)';
$this->rest_base = '/richimage/(?P<media_id>[\d]+)';
$this->editor = new Image_Editor();
}

Expand All @@ -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,
),
),
),
Expand All @@ -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,
),
),
),
Expand All @@ -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,
),
),
),
Expand All @@ -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() ) );
}

Expand All @@ -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 );
}

/**
Expand All @@ -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 );
}

/**
Expand All @@ -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 );
}
}
36 changes: 18 additions & 18 deletions lib/image-editor/class-image-editor-crop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 );
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions lib/image-editor/class-image-editor-flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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,
);
}
}
6 changes: 3 additions & 3 deletions lib/image-editor/class-image-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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' ),
);
}

Expand Down
12 changes: 6 additions & 6 deletions packages/block-library/src/rich-image/rich-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
} );
}
Expand All @@ -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,
} );
}

Expand Down

0 comments on commit 1fd4ed3

Please sign in to comment.