PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.1.2
GenerateBlocks v2.1.2
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / includes / class-rest.php
generateblocks / includes Last commit date
blocks 1 year ago dynamic-tags 10 months ago pattern-library 1 year ago utils 2 years ago class-do-css.php 2 years ago class-dynamic-content.php 1 year ago class-enqueue-css.php 1 year ago class-legacy-attributes.php 4 years ago class-map-deprecated-attributes.php 2 years ago class-meta-handler.php 8 months ago class-plugin-update.php 1 year ago class-query-loop.php 2 years ago class-query-utils.php 10 months ago class-render-blocks.php 1 year ago class-rest.php 1 year ago class-settings.php 1 year ago dashboard.php 1 year ago defaults.php 1 year ago deprecated.php 1 year ago functions.php 8 months ago general.php 8 months ago
class-rest.php
370 lines
1 <?php
2 /**
3 * Rest API functions
4 *
5 * @package GenerateBlocks
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Class GenerateBlocks_Rest
14 */
15 class GenerateBlocks_Rest extends WP_REST_Controller {
16 /**
17 * Instance.
18 *
19 * @access private
20 * @var object Instance
21 */
22 private static $instance;
23
24 /**
25 * Namespace.
26 *
27 * @var string
28 */
29 protected $namespace = 'generateblocks/v';
30
31 /**
32 * Version.
33 *
34 * @var string
35 */
36 protected $version = '1';
37
38 /**
39 * Onboarding meta key.
40 *
41 * @var string
42 */
43 const ONBOARDING_META_KEY = 'generateblocks_onboarding';
44
45 /**
46 * Initiator.
47 *
48 * @return object initialized object of class.
49 */
50 public static function get_instance() {
51 if ( ! isset( self::$instance ) ) {
52 self::$instance = new self();
53 }
54
55 return self::$instance;
56 }
57
58 /**
59 * GenerateBlocks_Rest constructor.
60 */
61 public function __construct() {
62 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
63 }
64
65 /**
66 * Register rest routes.
67 */
68 public function register_routes() {
69 $namespace = $this->namespace . $this->version;
70
71 // Update Settings.
72 register_rest_route(
73 $namespace,
74 '/settings/',
75 array(
76 'methods' => WP_REST_Server::EDITABLE,
77 'callback' => array( $this, 'update_settings' ),
78 'permission_callback' => array( $this, 'update_settings_permission' ),
79 )
80 );
81
82 // Update single setting.
83 register_rest_route(
84 $namespace,
85 '/setting/',
86 array(
87 'methods' => WP_REST_Server::EDITABLE,
88 'callback' => array( $this, 'update_setting' ),
89 'permission_callback' => array( $this, 'update_settings_permission' ),
90 )
91 );
92
93 // Regenerate CSS Files.
94 register_rest_route(
95 $namespace,
96 '/regenerate_css_files/',
97 array(
98 'methods' => WP_REST_Server::EDITABLE,
99 'callback' => array( $this, 'regenerate_css_files' ),
100 'permission_callback' => array( $this, 'update_settings_permission' ),
101 )
102 );
103
104 register_rest_route(
105 $namespace,
106 '/onboarding/',
107 array(
108 'methods' => WP_REST_Server::EDITABLE,
109 'callback' => array( $this, 'onboarding' ),
110 'permission_callback' => array( $this, 'onboarding_permission' ),
111 )
112 );
113
114 register_rest_route(
115 $namespace,
116 '/get-attachment-by-url/',
117 array(
118 'methods' => WP_REST_Server::READABLE,
119 'callback' => array( $this, 'get_attachment_by_url' ),
120 'permission_callback' => array( $this, 'can_edit_posts' ),
121 )
122 );
123 }
124
125 /**
126 * Get edit options permissions.
127 *
128 * @return bool
129 */
130 public function update_settings_permission() {
131 return current_user_can( 'manage_options' );
132 }
133
134 /**
135 * Sanitize our options.
136 *
137 * @since 1.2.0
138 * @param string $name The setting name.
139 * @param mixed $value The value to save.
140 */
141 public function sanitize_value( $name, $value ) {
142 $callbacks = apply_filters(
143 'generateblocks_option_sanitize_callbacks',
144 array(
145 'container_width' => 'absint',
146 'css_print_method' => 'sanitize_text_field',
147 'sync_responsive_previews' => 'rest_sanitize_boolean',
148 'disable_google_fonts' => 'rest_sanitize_boolean',
149 'gb_use_v1_blocks' => 'rest_sanitize_boolean',
150 )
151 );
152
153 $callback = $callbacks[ $name ];
154
155 if ( ! is_callable( $callback ) ) {
156 return sanitize_text_field( $value );
157 }
158
159 return $callback( $value );
160 }
161
162 /**
163 * Update a single setting.
164 *
165 * @param WP_REST_Request $request Full data about the request.
166 */
167 public function update_setting( WP_REST_Request $request ) {
168 $name = $request->get_param( 'name' );
169 $value = $request->get_param( 'value' );
170
171 $allowed_settings = [
172 'gb_use_v1_blocks',
173 ];
174
175 if ( ! in_array( $name, $allowed_settings, true ) ) {
176 return $this->error( 'rest_forbidden', 'Sorry, you are not allowed to update this setting.' );
177 }
178
179 $value = $this->sanitize_value( $name, $value );
180
181 update_option( $name, $value );
182
183 return $this->success( __( 'Settings saved.', 'generateblocks' ) );
184 }
185
186 /**
187 * Update Settings.
188 *
189 * @param WP_REST_Request $request request object.
190 *
191 * @return mixed
192 */
193 public function update_settings( WP_REST_Request $request ) {
194 $current_settings = get_option( 'generateblocks', array() );
195 $new_settings = $request->get_param( 'settings' );
196
197 foreach ( $new_settings as $name => $value ) {
198 // Skip if the option hasn't changed.
199 if ( isset( $current_settings[ $name ] ) && $current_settings[ $name ] === $new_settings[ $name ] ) {
200 unset( $new_settings[ $name ] );
201 continue;
202 }
203
204 // Only save options that we know about.
205 if ( ! array_key_exists( $name, generateblocks_get_option_defaults() ) ) {
206 unset( $new_settings[ $name ] );
207 continue;
208 }
209
210 // Sanitize our value.
211 $new_settings[ $name ] = $this->sanitize_value( $name, $value );
212 }
213
214 if ( empty( $new_settings ) ) {
215 return $this->success( __( 'No changes found.', 'generateblocks' ) );
216 }
217
218 if ( is_array( $new_settings ) ) {
219 update_option( 'generateblocks', array_merge( $current_settings, $new_settings ) );
220 }
221
222 return $this->success( __( 'Settings saved.', 'generateblocks' ) );
223 }
224
225 /**
226 * Regenerate CSS Files.
227 *
228 * @param WP_REST_Request $request request object.
229 *
230 * @return mixed
231 */
232 public function regenerate_css_files( WP_REST_Request $request ) {
233 update_option( 'generateblocks_dynamic_css_posts', array() );
234
235 return $this->success( __( 'CSS files regenerated.', 'generateblocks' ) );
236 }
237
238 /**
239 * Mark an onboard as "viewed" by the user.
240 *
241 * @param WP_REST_Request $request request object.
242 *
243 * @return WP_REST_Response The response.
244 */
245 public function onboarding( WP_REST_Request $request ) {
246 $user_id = get_current_user_id();
247 $onboard = get_user_meta( $user_id, self::ONBOARDING_META_KEY, true );
248 $key = $request->get_param( 'key' );
249
250 if ( ! $onboard ) {
251 $onboard = array();
252 }
253
254 $onboard[ $key ] = true;
255
256 update_user_meta( get_current_user_id(), self::ONBOARDING_META_KEY, $onboard );
257
258 return new WP_REST_Response( array( 'success' => true ), 200 );
259 }
260
261 /**
262 * Get the attachment object by its URL.
263 *
264 * @param WP_REST_Request $request request object.
265 *
266 * @return WP_REST_Response The response.
267 */
268 public function get_attachment_by_url( WP_REST_Request $request ) {
269 $url = $request->get_param( 'url' );
270
271 if ( ! $url ) {
272 return $this->error( 'no_url', __( 'No URL provided.', 'generateblocks' ) );
273 }
274
275 // Regular expression to remove '-300x300' like size patterns from the URL.
276
277 $pattern = '/-\d+x\d+(?=\.\w{3,4}$)/';
278 $url = preg_replace( $pattern, '', $url );
279 $id = attachment_url_to_postid( $url );
280
281 if ( ! $id ) {
282 return $this->error( 'no_attachment', __( 'No attachment found.', 'generateblocks' ) );
283 }
284
285 // Get image metadata, such as width, height, and sizes.
286 $image_metadata = wp_get_attachment_metadata( $id );
287
288 // Get the full URL of the image.
289 $full_url = wp_get_attachment_url( $id );
290
291 // Get URLs for all available sizes.
292 $sizes = [];
293 $image_sizes = get_intermediate_image_sizes();
294 foreach ( $image_sizes as $size ) {
295 $image_src = wp_get_attachment_image_src( $id, $size );
296 if ( $image_src && $image_src[0] !== $full_url ) {
297 $sizes[ $size ] = [
298 'url' => $image_src[0],
299 'width' => $image_src[1],
300 'height' => $image_src[2],
301 ];
302 }
303 }
304
305 $response = [
306 'id' => $id,
307 'full_url' => $full_url,
308 'width' => isset( $image_metadata['width'] ) ? $image_metadata['width'] : '',
309 'height' => isset( $image_metadata['height'] ) ? $image_metadata['height'] : '',
310 'sizes' => $sizes,
311 ];
312
313 return $this->success( $response );
314 }
315
316
317 /**
318 * Get onboarding edit permission.
319 *
320 * @return bool
321 */
322 public function onboarding_permission() {
323 return current_user_can( 'edit_posts' );
324 }
325
326 /**
327 * Check if the user can edit posts.
328 */
329 public function can_edit_posts() {
330 return current_user_can( 'edit_posts' );
331 }
332
333 /**
334 * Success rest.
335 *
336 * @param mixed $response response data.
337 * @return mixed
338 */
339 public function success( $response ) {
340 return new WP_REST_Response(
341 array(
342 'success' => true,
343 'response' => $response,
344 ),
345 200
346 );
347 }
348
349 /**
350 * Error rest.
351 *
352 * @param mixed $code error code.
353 * @param mixed $response response data.
354 * @return mixed
355 */
356 public function error( $code, $response ) {
357 return new WP_REST_Response(
358 array(
359 'error' => true,
360 'success' => false,
361 'error_code' => $code,
362 'response' => $response,
363 ),
364 401
365 );
366 }
367 }
368
369 GenerateBlocks_Rest::get_instance();
370