PluginProbe
Gutenberg / 8.3.0
Gutenberg v8.3.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / class-wp-rest-image-editor-controller.php

class-wp-rest-image-editor-controller.php in Gutenberg 8.3.0, at lib/class-wp-rest-image-editor-controller.php

185 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Start: Include for phase 2
4 * REST API: WP_REST_Menus_Controller class
5 *
6 * @package WordPress
7 * @subpackage REST_API
8 */
9
10 /**
11 * Image editor
12 */
13 include_once __DIR__ . '/image-editor/class-image-editor.php';
14
15 /**
16 * Controller which provides REST API endpoints for image editing.
17 *
18 * @since 7.x ?
19 *
20 * @see WP_REST_Controller
21 */
22 class WP_REST_Image_Editor_Controller extends WP_REST_Controller {
23
24 /**
25 * Constructs the controller.
26 *
27 * @since 7.x ?
28 * @access public
29 */
30 public function __construct() {
31 $this->namespace = '__experimental';
32 $this->rest_base = '/richimage/(?P<mediaID>[\d]+)';
33 $this->editor = new Image_Editor();
34 }
35
36 /**
37 * Registers the necessary REST API routes.
38 *
39 * @since 7.x ?
40 * @access public
41 */
42 public function register_routes() {
43 register_rest_route(
44 $this->namespace,
45 $this->rest_base . '/rotate',
46 array(
47 array(
48 'methods' => WP_REST_Server::EDITABLE,
49 'callback' => array( $this, 'rotate_image' ),
50 'permission_callback' => array( $this, 'permission_callback' ),
51 'args' => array(
52 'angle' => array(
53 'type' => 'integer',
54 'required' => true,
55 ),
56 ),
57 ),
58 )
59 );
60
61 register_rest_route(
62 $this->namespace,
63 $this->rest_base . '/flip',
64 array(
65 array(
66 'methods' => WP_REST_Server::EDITABLE,
67 'callback' => array( $this, 'flip_image' ),
68 'permission_callback' => array( $this, 'permission_callback' ),
69 'args' => array(
70 'direction' => array(
71 'type' => 'enum',
72 'enum' => array( 'vertical', 'horizontal' ),
73 'required' => true,
74 ),
75 ),
76 ),
77 )
78 );
79
80 register_rest_route(
81 $this->namespace,
82 $this->rest_base . '/crop',
83 array(
84 array(
85 'methods' => WP_REST_Server::EDITABLE,
86 'callback' => array( $this, 'crop_image' ),
87 'permission_callback' => array( $this, 'permission_callback' ),
88 'args' => array(
89 'cropX' => array(
90 'type' => 'float',
91 'minimum' => 0,
92 'required' => true,
93 ),
94 'cropY' => array(
95 'type' => 'float',
96 'minimum' => 0,
97 'required' => true,
98 ),
99 'cropWidth' => array(
100 'type' => 'float',
101 'minimum' => 1,
102 'required' => true,
103 ),
104 'cropHeight' => array(
105 'type' => 'float',
106 'minimum' => 1,
107 'required' => true,
108 ),
109 ),
110 ),
111 )
112 );
113 }
114
115 /**
116 * Checks if the user has permissions to make the request.
117 *
118 * @since 7.x ?
119 * @access public
120 *
121 * @param WP_REST_Request $request Full details about the request.
122 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
123 */
124 public function permission_callback( $request ) {
125 $params = $request->get_params();
126
127 if ( ! current_user_can( 'edit_post', $params['mediaID'] ) ) {
128 return new WP_Error( 'rest_cannot_edit_image', __( 'Sorry, you are not allowed to edit images.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) );
129 }
130
131 return true;
132 }
133
134 /**
135 * Rotates an image.
136 *
137 * @since 7.x ?
138 * @access public
139 *
140 * @param WP_REST_Request $request Full details about the request.
141 * @return array|WP_Error If successful image JSON for the modified image, otherwise a WP_Error.
142 */
143 public function rotate_image( $request ) {
144 $params = $request->get_params();
145
146 $modifier = new Image_Editor_Rotate( $params['angle'] );
147
148 return $this->editor->modify_image( $params['mediaID'], $modifier );
149 }
150
151 /**
152 * Flips/mirrors an image.
153 *
154 * @since 7.x ?
155 * @access public
156 *
157 * @param WP_REST_Request $request Full details about the request.
158 * @return array|WP_Error If successful image JSON for the modified image, otherwise a WP_Error.
159 */
160 public function flip_image( $request ) {
161 $params = $request->get_params();
162
163 $modifier = new Image_Editor_Flip( $params['direction'] );
164
165 return $this->editor->modify_image( $params['mediaID'], $modifier );
166 }
167
168 /**
169 * Crops an image.
170 *
171 * @since 7.x ?
172 * @access public
173 *
174 * @param WP_REST_Request $request Full details about the request.
175 * @return array|WP_Error If successful image JSON for the modified image, otherwise a WP_Error.
176 */
177 public function crop_image( $request ) {
178 $params = $request->get_params();
179
180 $modifier = new Image_Editor_Crop( $params['cropX'], $params['cropY'], $params['cropWidth'], $params['cropHeight'] );
181
182 return $this->editor->modify_image( $params['mediaID'], $modifier );
183 }
184 }
185