Skip to content

Commit

Permalink
Make image-editor API take an array
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlende committed Jun 22, 2020
1 parent 0a1c4c8 commit ad18028
Showing 1 changed file with 79 additions and 52 deletions.
131 changes: 79 additions & 52 deletions lib/class-wp-rest-image-editor-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,50 +50,77 @@ public function register_routes() {
'permission_callback' => array( $this, 'permission_callback' ),
'args' => array(
array(
'crop' => array(
'type' => 'object',
'properties' => array(
'left' => array(
'type' => 'integer',
'minimum' => 0,
'required' => true,
),
'top' => array(
'type' => 'integer',
'minimum' => 0,
'required' => true,
),
'width' => array(
'type' => 'integer',
'minimum' => 1,
'required' => true,
),
'height' => array(
'type' => 'integer',
'minimum' => 1,
'required' => true,
),
),
),
'rotate' => array(
'type' => 'object',
'properties' => array(
'angle' => array(
'type' => 'integer',
'required' => true,
),
),
),
'flip' => array(
'type' => 'object',
'properties' => array(
'horizontal' => array(
'type' => 'boolean',
'required' => true,
),
'vertical' => array(
'type' => 'boolean',
'required' => true,
'modifiers' => array(
'type' => 'array',
'required' => true,
'items' => array(
'anyOf' => array(
array(
'type' => 'object',
'properties' => array(
'modifier' => array(
'type' => 'string',
'required' => true,
'const' => 'crop',
),
'left' => array(
'type' => 'number',
'required' => true,
'minimum' => 0,
'maximum' => 100,
),
'top' => array(
'type' => 'number',
'required' => true,
'minimum' => 0,
'maximum' => 100,
),
'width' => array(
'type' => 'number',
'required' => true,
'minimum' => 1,
'maximum' => 100,
),
'height' => array(
'type' => 'number',
'required' => true,
'minimum' => 1,
'maximum' => 100,
),
),
),
array(
'type' => 'object',
'properties' => array(
'modifier' => array(
'type' => 'string',
'required' => true,
'const' => 'rotate',
),
'angle' => array(
'type' => 'integer',
'required' => true,
),
),
),
array(
'type' => 'object',
'properties' => array(
'modifier' => array(
'type' => 'string',
'required' => true,
'const' => 'flip',
),
'horizontal' => array(
'type' => 'boolean',
'required' => true,
),
'vertical' => array(
'type' => 'boolean',
'required' => true,
),
),
),
),
),
),
Expand Down Expand Up @@ -132,14 +159,14 @@ public function permission_callback( $request ) {
*/
public function apply_edits( $request ) {
$modifiers = array();
if ( $request['rotate'] ) {
$modifiers[] = new Image_Editor_Rotate( $request['rotate']['angle'] );
}
if ( $request['flip'] ) {
$modifiers[] = new Image_Editor_Flip( $request['flip']['vertical'], $request['flip']['horizontal'] );
}
if ( $request['crop'] ) {
$modifiers[] = new Image_Editor_Crop( $request['crop']['left'], $request['crop']['top'], $request['crop']['width'], $request['crop']['height'] );
foreach ( $request['modifiers'] as $modifier ) {
if ( 'rotate' === $modifier['modifier'] ) {
$modifiers[] = new Image_Editor_Rotate( $modifier['angle'] );
} elseif ( 'flip' === $modifier['modifier'] ) {
$modifiers[] = new Image_Editor_Flip( $modifier['vertical'], $modifier['horizontal'] );
} elseif ( 'crop' === $modifier['modifier'] ) {
$modifiers[] = new Image_Editor_Crop( $modifier['left'], $modifier['top'], $modifier['width'], $modifier['height'] );
}
}
return $this->editor->modify_image( $request['media_id'], $modifiers );
}
Expand Down

0 comments on commit ad18028

Please sign in to comment.