PluginProbe
Meow Gallery / 5.5.3
Meow Gallery v5.5.3
5.5.5 5.5.4 5.5.3 5.5.2 5.5.1 5.5.0 5.4.9 5.4.8 5.4.7 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 All 157 releases
meow-gallery / classes / rest.php

rest.php in Meow Gallery 5.5.3, at classes/rest.php

689 lines 22.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Meow_MGL_Rest
4 {
5 private $core;
6 private $namespace = 'meow-gallery/v1';
7
8 public function __construct( $core ) {
9 $this->core = $core;
10
11 // FOR DEBUG
12 // For experiencing the UI behavior on a slower install.
13 // sleep( 1 );
14 // For experiencing the UI behavior on a buggy install.
15 // trigger_error( "Error", E_USER_ERROR );
16 // trigger_error( "Warning", E_USER_WARNING );
17 // trigger_error( "Notice", E_USER_NOTICE );
18 // trigger_error( "Deprecated", E_USER_DEPRECATED );
19
20 add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
21 }
22
23
24 function rest_api_init( ) {
25
26 // Settings
27 register_rest_route( $this->namespace, '/update_option/', array(
28 'methods' => 'POST',
29 'permission_callback' => array( $this->core, 'can_access_settings' ),
30 'callback' => array( $this, 'rest_update_option' )
31 ) );
32 register_rest_route( $this->namespace, '/all_settings/', array(
33 'methods' => 'GET',
34 'permission_callback' => array( $this->core, 'can_access_settings' ),
35 'callback' => array( $this, 'rest_all_settings' )
36 ) );
37 register_rest_route( $this->namespace, '/reset_options', array(
38 'methods' => 'POST',
39 'permission_callback' => array( $this->core, 'can_access_settings' ),
40 'callback' => array( $this, 'rest_reset_options' )
41 ) );
42
43
44 // Gallery Manager
45 register_rest_route( $this->namespace, '/latest_photos', array(
46 'methods' => 'GET',
47 'permission_callback' => array( $this->core, 'can_access_settings' ),
48 'callback' => array( $this, 'rest_latest_photos' ),
49 'args' => array(
50 'search' => array( 'required' => false ),
51 'offset' => array( 'required' => false, 'default' => 0 ),
52 'except' => array( 'required' => false ),
53 )
54 ) );
55 register_rest_route( $this->namespace, '/save_shortcode', array(
56 'methods' => 'POST',
57 'permission_callback' => array( $this->core, 'can_access_settings' ),
58 'callback' => array( $this, 'rest_save_shortcode' ),
59 ) );
60 register_rest_route( $this->namespace, '/remove_shortcode', array(
61 'methods' => 'POST',
62 'permission_callback' => array( $this->core, 'can_access_settings' ),
63 'callback' => array( $this, 'rest_remove_shortcode' ),
64 ) );
65 register_rest_route( $this->namespace, '/update_gallery_rank', array(
66 'methods' => 'POST',
67 'permission_callback' => array( $this->core, 'can_access_settings' ),
68 'callback' => array( $this, 'rest_update_gallery_rank' ),
69 ) );
70 register_rest_route( $this->namespace, '/rml_folders', array(
71 'methods' => 'GET',
72 'permission_callback' => array( $this->core, 'can_access_settings' ),
73 'callback' => array( $this, 'rest_rml_folders' ),
74 ) );
75
76
77 register_rest_route( $this->namespace, '/fetch_shortcodes', array(
78 'methods' => 'POST',
79 'permission_callback' => array( $this->core, 'can_access_features' ),
80 'callback' => array( $this, 'rest_fetch_shortcodes' ),
81 ) );
82 register_rest_route( $this->namespace, '/fetch_gallery_items', array(
83 'methods' => 'POST',
84 'permission_callback' => array( $this->core, 'can_access_features' ),
85 'callback' => array( $this, 'rest_fetch_gallery_items' ),
86 ) );
87
88
89
90 //Collection Manager
91 register_rest_route( $this->namespace, '/save_collection', array(
92 'methods' => 'POST',
93 'permission_callback' => array( $this->core, 'can_access_settings' ),
94 'callback' => array( $this, 'rest_save_collection' ),
95 ) );
96 register_rest_route( $this->namespace, '/remove_collection', array(
97 'methods' => 'POST',
98 'permission_callback' => array( $this->core, 'can_access_settings' ),
99 'callback' => array( $this, 'rest_remove_collection' ),
100 ) );
101
102 register_rest_route( $this->namespace, '/fetch_collections', array(
103 'methods' => 'POST',
104 'permission_callback' => array( $this->core, 'can_access_features' ),
105 'callback' => array( $this, 'rest_fetch_collections' ),
106 ) );
107
108 register_rest_route( $this->namespace, '/load_gallery_collection', array(
109 'methods' => 'POST',
110 'permission_callback' => '__return_true',
111 'callback' => array( $this, 'rest_load_gallery_collection' ),
112 ) );
113
114 // Gutenberg Block
115 register_rest_route( $this->namespace, '/preview', array(
116 'methods' => 'POST',
117 'permission_callback' => array( $this->core, 'can_access_features' ),
118 'callback' => array( $this, 'preview' ),
119 ) );
120
121 // Gallery
122 register_rest_route( $this->namespace, '/images/', array(
123 'methods' => 'POST',
124 'permission_callback' => '__return_true',
125 'callback' => array( $this, 'rest_images' )
126 ) );
127
128 register_rest_route( $this->namespace, '/fetch_posts', array(
129 'methods' => 'POST',
130 'permission_callback' => array( $this->core, 'can_access_features' ),
131 'callback' => array( $this, 'rest_fetch_posts' ),
132 'args' => array(
133 'search' => array( 'required' => false ),
134 'offset' => array( 'required' => false, 'default' => 0 ),
135 'limit' => array( 'required' => false, 'default' => 10 ),
136 )
137 ) );
138 }
139
140 function preview( WP_REST_Request $request ) {
141 $params = $request->get_body( );
142 $params = json_decode( $params );
143 $params->ids = implode( ',', $params->ids );
144 $atts = ( array ) $params;
145
146 $is_collection = isset( $atts['collection'] ) && !empty( $atts['collection'] );
147 if ( $is_collection ) {
148 $html = do_shortcode( '[meow-collection id="' . $atts['collection'] . '"]' );
149 $counts = [ 'total' => 0, 'shown' => 0 ];
150 } else {
151 $this->core->last_preview_counts = [ 'total' => 0, 'shown' => 0 ];
152 $html = $this->core->gallery( $atts, [ 'isPreview' => true ] );
153 $counts = $this->core->last_preview_counts;
154 }
155
156
157 return new WP_REST_Response( [
158 'success' => true,
159 'data' => $html,
160 'total' => intval( $counts['total'] ),
161 'shown' => intval( $counts['shown'] ),
162 ], 200 );
163 }
164
165 function rest_load_gallery_collection( $request ) {
166 try {
167 $params = $request->get_json_params( );
168 $gallery_id = $params['id'];
169 $search_slug = $params['search_slug'];
170 $gallery_atts = $params['gallery_atts'];
171
172 $key = [
173 'gallery_id' => 'id',
174 'wplr_collection_id' => 'wplr-collection',
175 'rml' => 'rml',
176 ];
177
178 $shortcode_atts = array();
179 $shortcode_atts[ $key[$search_slug] ] = $gallery_id;
180 $shortcode_atts = [...$shortcode_atts, ...$gallery_atts];
181
182 $html = $this->core->gallery( $shortcode_atts, [ 'isPreview' => false, 'isRest' => true ] );
183 $mwlData = json_encode( $this->core->get_rewritten_mwl_data( ) );
184 return new WP_REST_Response( [ 'success' => true, 'data' => $html, 'mwl_data' => $mwlData ], 200 );
185 }
186 catch ( Exception $e ) {
187 return new WP_REST_Response( [ 'success' => false, 'message' => $e->getMessage( ) ], 500 );
188 }
189 }
190
191 function rest_all_settings( ) {
192 return new WP_REST_Response( [ 'success' => true, 'data' => $this->core->get_all_options( ) ], 200 );
193 }
194
195 function rest_rml_folders( ) {
196 if ( ! Meow_MGL_RML::is_available() ) {
197 return new WP_REST_Response( [ 'success' => true, 'available' => false, 'data' => [] ], 200 );
198 }
199 return new WP_REST_Response( [ 'success' => true, 'available' => true, 'data' => Meow_MGL_RML::get_all_folders() ], 200 );
200 }
201
202 function rest_reset_options( ) {
203 $this->core->reset_options( );
204 return new WP_REST_Response( [ 'success' => true, 'options' => $this->core->get_all_options( ) ], 200 );
205 }
206
207 function rest_save_shortcode( $request ) {
208 try {
209 global $wpdb;
210 $params = $request->get_json_params( );
211
212 $id = $params['id'];
213 $medias = $params['medias'];
214 $name = $params['name'];
215 $layout = $params['layout'];
216 $description = $params['description'];
217 $posts = $params['posts'];
218 $latest_posts = $params['latest_posts'];
219 $tags = $params['tags'];
220 $dynamic_source = $params['dynamic_source'];
221 $lead_image_id = $params['lead_image_id'];
222 $order_by = $params['order_by'];
223 $is_post_mode = $params['is_post_mode'];
224 $is_hero_mode = $params['is_hero_mode'];
225 $rml = $params['rml'] ?? null;
226
227 if ( !$name ) {
228 throw new Exception( __( 'Please enter a name for your shortcode.', MGL_DOMAIN ));
229 }
230
231 if ( !$is_post_mode && ( !$medias || !count( $medias['thumbnail_ids'] )) ) {
232 throw new Exception( __( 'Please select at least one image.', MGL_DOMAIN ));
233 }
234
235 if ( $is_post_mode && $dynamic_source === 'posts' && ( !$posts && !$latest_posts )) {
236 throw new Exception( __( 'Please select at least one post.', MGL_DOMAIN ));
237 }
238
239 if ( $is_post_mode && $dynamic_source === 'tags' && !$tags ) {
240 throw new Exception( __( 'Please enter at least one tag.', MGL_DOMAIN ));
241 }
242
243 if ( $is_post_mode && $dynamic_source === 'rml' && empty( $rml ) ) {
244 throw new Exception( __( 'Please select a Real Media Library folder.', MGL_DOMAIN ));
245 }
246
247 if ( $is_hero_mode && !$is_post_mode ) {
248 throw new Exception( __( 'Hero mode is only available for post mode.', MGL_DOMAIN ));
249 }
250
251 if ( !$id || $id == '' ) {
252 $id = $this->core->generate_uniqid( 10 );
253 }
254
255 $shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
256 Meow_MGL_Migrations::check_db();
257
258 // Check if the record exists
259 $exists = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $shortcodes_table WHERE id = %s", $id ));
260
261 $data = [
262 'name' => $name,
263 'description' => $description,
264 'layout' => $layout,
265 'medias' => serialize( $medias ),
266 'lead_image_id' => $lead_image_id,
267 'order_by' => $order_by,
268 'is_post_mode' => $is_post_mode ? 1 : 0,
269 'is_hero_mode' => $is_hero_mode ? 1 : 0,
270 'posts' => $posts ? serialize( $posts ) : null,
271 'latest_posts' => $latest_posts,
272 'tags' => serialize( $tags ),
273 'dynamic_source' => $dynamic_source,
274 'rml' => $rml
275 ];
276
277 if ( $exists ) {
278 // Update existing record
279 $wpdb->update(
280 $shortcodes_table,
281 $data,
282 ['id' => $id]
283 );
284 } else {
285 // Insert new record
286 $data['id'] = $id;
287 $wpdb->insert( $shortcodes_table, $data );
288 }
289
290
291 return new WP_REST_Response( ['success' => true, 'message' => 'Shortcode created.'], 200 );
292 } catch ( Exception $e ) {
293 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage( )], 500 );
294 }
295 }
296
297 function rest_remove_collection( $request ) {
298 try {
299 global $wpdb;
300 $params = $request->get_json_params( );
301 $id = $params['id'];
302
303 $collections_table = $wpdb->prefix . 'mgl_collections';
304 Meow_MGL_Migrations::check_db();
305
306 $wpdb->delete( $collections_table, ['id' => $id] );
307
308
309 return new WP_REST_Response( ['success' => true, 'message' => 'Collection removed.'], 200 );
310 } catch ( Exception $e ) {
311 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage( )], 500 );
312 }
313 }
314
315 function rest_save_collection( $request ) {
316 try {
317 global $wpdb;
318 $params = $request->get_json_params( );
319
320 $id = $params['id'];
321 $name = $params['name'];
322 $layout = $params['layout'];
323 $galleries_ids = $params['galleries_ids'];
324 $description = $params['description'];
325
326 if ( !$name ) {
327 throw new Exception( __( 'Please enter a name for your collection.', MGL_DOMAIN ));
328 }
329
330 if ( !$galleries_ids || !count( $galleries_ids )) {
331 throw new Exception( __( 'Please select at least one gallery.', MGL_DOMAIN ));
332 }
333
334 if ( !$id || $id == '' ) {
335 $id = $this->core->generate_uniqid( 10 );
336 }
337
338 $collections_table = $wpdb->prefix . 'mgl_collections';
339 Meow_MGL_Migrations::check_db();
340
341 // Check if the record exists
342 $exists = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $collections_table WHERE id = %s", $id ));
343
344 $data = [
345 'name' => $name,
346 'description' => $description,
347 'layout' => $layout,
348 'galleries_ids' => serialize( $galleries_ids )
349 ];
350
351 if ( $exists ) {
352 // Update existing record
353 $wpdb->update(
354 $collections_table,
355 $data,
356 ['id' => $id]
357 );
358 } else {
359 // Insert new record
360 $data['id'] = $id;
361 $wpdb->insert( $collections_table, $data );
362 }
363
364
365 return new WP_REST_Response( ['success' => true, 'message' => 'Collection created.'], 200 );
366 } catch ( Exception $e ) {
367 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage( )], 500 );
368 }
369 }
370
371
372 function rest_fetch_collections( $request ) {
373 try {
374 $params = $request->get_json_params( );
375
376 $offset = isset( $params['offset'] ) ? $params['offset'] : 0;
377 $limit = isset( $params['limit'] ) ? $params['limit'] : 10;
378 $sort_updated = $params['sort']['by']; // desc, asc
379 $page = isset( $params['page'] ) ? $params['page'] : 1;
380 $order = $sort_updated === 'desc' ? 'DESC' : 'ASC';
381 $search = isset( $params['search'] ) ? $params['search'] : '';
382
383 $res = $this->core->get_collections( $offset, $limit, $order, $page, $search );
384 $collections = $res['collections'];
385 $total = $res['total'];
386
387 return new WP_REST_Response( ['success' => true, 'data' => $collections, 'total' => $total], 200 );
388 }
389 catch ( Exception $e ) {
390 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage( )], 500 );
391 }
392 }
393
394 function rest_fetch_gallery_items( $request ) {
395 try {
396 global $wpdb;
397 $params = $request->get_json_params( );
398 $galleryIds = $params['galleryIds'];
399
400 $shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
401 Meow_MGL_Migrations::check_db();
402
403 $galleries = [];
404 if ( !empty( $galleryIds )) {
405 $ids_str = "'" . implode( "','", array_map( 'esc_sql', $galleryIds )) . "'";
406 $query = "SELECT * FROM $shortcodes_table WHERE id IN ( $ids_str )";
407 $results = $wpdb->get_results( $query, ARRAY_A );
408
409 foreach ( $results as $gallery ) {
410 // Transform database format to match expected format
411 $galleries[$gallery['id']] = [
412 'name' => $gallery['name'],
413 'description' => $gallery['description'],
414 'layout' => $gallery['layout'],
415 'medias' => unserialize( $gallery['medias'] ),
416 'is_post_mode' => ( bool )$gallery['is_post_mode'],
417 'hero' => ( bool )$gallery['is_hero_mode'],
418 'posts' => $gallery['posts'] ? unserialize( $gallery['posts'] ) : null,
419 'latest_posts' => $gallery['latest_posts'],
420 'tags' => unserialize( $gallery['tags'] ),
421 'dynamic_source' => $gallery['dynamic_source'],
422 'updated' => strtotime( $gallery['updated_at'] )
423 ];
424 }
425 }
426
427 return new WP_REST_Response( ['success' => true, 'data' => $galleries], 200 );
428 } catch ( Exception $e ) {
429 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage( )], 500 );
430 }
431 }
432
433 function rest_fetch_shortcodes( $request ) {
434 try {
435 $params = $request->get_json_params( );
436
437 $offset = isset( $params['offset'] ) ? $params['offset'] : 0;
438 $limit = isset( $params['limit'] ) ? $params['limit'] : 10;
439 $page = isset( $params['page'] ) ? $params['page'] : 1;
440
441 $search = isset( $params['search'] ) ? $params['search'] : '';
442
443 $sort_by = $params['sort']['accessor'] ?? null;
444 $order_by = strtoupper( $params['sort']['by'] ); // desc, asc
445
446 $res = $this->core->get_galleries( $offset, $limit, $order_by, $sort_by, $page, $search );
447 $shortcodes = $res['galleries'];
448 $total = $res['total'];
449
450 return new WP_REST_Response( ['success' => true, 'data' => $shortcodes, 'total' => $total], 200 );
451 }
452 catch ( Exception $e ) {
453 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage( )], 500 );
454 }
455 }
456
457 function rest_remove_shortcode( $request ) {
458 try {
459 global $wpdb;
460 $params = $request->get_json_params( );
461 $id = $params['id'];
462
463 $shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
464 Meow_MGL_Migrations::check_db();
465
466 $wpdb->delete( $shortcodes_table, ['id' => $id] );
467
468
469 return new WP_REST_Response( ['success' => true, 'message' => 'Shortcode removed.'], 200 );
470 } catch ( Exception $e ) {
471 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage( )], 500 );
472 }
473 }
474
475 function rest_update_gallery_rank( $request ) {
476 try {
477 global $wpdb;
478 $params = $request->get_json_params();
479 $id = $params['id'];
480 $direction = $params['direction']; // 'up' or 'down'
481
482 $shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
483 Meow_MGL_Migrations::check_db();
484
485 // Get current rank
486 $current_rank = $wpdb->get_var( $wpdb->prepare( "SELECT pref_rank FROM $shortcodes_table WHERE id = %s", $id ) );
487 $current_rank = intval( $current_rank );
488
489 // Calculate new rank (up = higher priority = higher number, down = lower priority = lower number)
490 $new_rank = $direction === 'up' ? $current_rank + 1 : $current_rank - 1;
491
492 // Update the rank
493 $wpdb->update(
494 $shortcodes_table,
495 ['pref_rank' => $new_rank],
496 ['id' => $id]
497 );
498
499 return new WP_REST_Response( ['success' => true, 'message' => 'Gallery rank updated.', 'new_rank' => $new_rank], 200 );
500 } catch ( Exception $e ) {
501 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage()], 500 );
502 }
503 }
504
505 function rest_latest_photos( $request ) {
506
507 $search = trim( $request->get_param( 'search' ) );
508 $offset = trim( $request->get_param( 'offset' ) );
509 $limit = trim( $request->get_param( 'limit' ) );
510
511 $except = json_decode( trim( $request->get_param( 'except' ) ), true );
512 $unusedImages = trim( $request->get_param( 'unusedImages' ) );
513
514 global $wpdb;
515 $searchPlaceholder = $search ? '%' . $search . '%' : '';
516 $where_search_clause = $search ? $wpdb->prepare(
517 "AND ( p.post_title LIKE %s OR p.post_content LIKE %s OR p.post_name LIKE %s ) ",
518 $searchPlaceholder,
519 $searchPlaceholder,
520 $searchPlaceholder
521 ) : '';
522 $where_search_clause .= $except && count( $except ) ? $wpdb->prepare(
523 "AND p.ID NOT IN ( " . implode( ', ', array_fill( 0, count( $except ), '%s' )) . " )", $except
524 ) : '';
525 $join_clause = '';
526 if ( $unusedImages ) {
527 // Retrieve the serialized option from the database
528 $meow_gallery_shortcodes = get_option( 'mgl_shortcodes' );
529
530 // Deserialize the option to get the array
531 $shortcodes_array = maybe_unserialize( $meow_gallery_shortcodes );
532
533 // Extract all thumbnail IDs from the array
534 $used_thumbnail_ids = [];
535 foreach ( $shortcodes_array as $shortcode ) {
536 if ( isset( $shortcode['medias']['thumbnail_ids'] ) && is_array( $shortcode['medias']['thumbnail_ids'] ) ) {
537 $used_thumbnail_ids = array_merge( $used_thumbnail_ids, $shortcode['medias']['thumbnail_ids'] );
538 }
539 }
540
541 // Make sure the IDs are integers
542 $used_thumbnail_ids = array_map( 'intval', $used_thumbnail_ids );
543
544 // Include the NOT IN clause to exclude used thumbnail IDs
545 if ( !empty( $used_thumbnail_ids ) ) {
546 $placeholders = implode( ',', array_fill( 0, count( $used_thumbnail_ids ), '%d' ) );
547 $where_search_clause .= $wpdb->prepare( " AND p.ID NOT IN ( $placeholders ) ", $used_thumbnail_ids );
548 }
549 }
550 $posts = $wpdb->get_results(
551 $wpdb->prepare(
552 "SELECT p.ID, p.post_title, p.post_mime_type
553 FROM $wpdb->posts p
554 $join_clause
555 WHERE p.post_type='attachment'
556 AND p.post_status='inherit'
557 $where_search_clause
558 ORDER BY p.post_modified DESC
559 LIMIT %d, $limit", $offset
560 ), OBJECT
561 );
562 $posts_count = ( int )$wpdb->get_var(
563 "SELECT COUNT( * )
564 FROM $wpdb->posts p
565 $join_clause
566 WHERE p.post_type='attachment'
567 AND p.post_status='inherit'
568 $where_search_clause"
569 );
570
571 $data = [];
572 foreach ( $posts as $post ) {
573 $file_url = get_attached_file( $post->ID );
574
575 $mime = $post->post_mime_type;
576 $is_video = ( strpos( $mime, 'video' ) !== false );
577
578 $thumbnail_url = $is_video ? wp_get_attachment_url( $post->ID ) : wp_get_attachment_image_url( $post->ID, 'thumbnail' );
579
580 if ( file_exists( $file_url ) ) {
581 $data[] = [
582 'id' => $post->ID,
583 'thumbnail_url' => $thumbnail_url,
584 'zoom_url' => wp_get_attachment_image_url( $post->ID, 'large' ),
585 'title' => $post->post_title,
586 'filename' => basename( $file_url ),
587 'size' => size_format( filesize( $file_url ) ),
588 'mime' => $mime,
589 ];
590 }
591 }
592 return new WP_REST_Response( [
593 'success' => true,
594 'data' => $data,
595 'total' => $posts_count
596 ], 200 );
597 }
598
599 function rest_update_option( $request ) {
600 try {
601 $params = $request->get_json_params( );
602 $value = $params['options'];
603 $options = $this->core->update_options( $value );
604 $success = !!$options;
605 $message = __( $success ? 'OK' : "Could not update options.", MGL_DOMAIN );
606 return new WP_REST_Response( [ 'success' => $success, 'message' => $message, 'options' => $success ? $options : null ], 200 );
607 }
608 catch ( Exception $e ) {
609 return new WP_REST_Response( [ 'success' => false, 'message' => $e->getMessage( ) ], 500 );
610 }
611 }
612
613 function rest_images( $request ) {
614 $params = $request->get_json_params( );
615
616 $image_ids = $params['imageIds'];
617 $atts = $params['atts'];
618 $layout = trim( $params['layout'] );
619 $size = trim( $params['size'] );
620
621 return new WP_REST_Response( [
622 'success' => true,
623 'data' => $this->core->get_gallery_images( $image_ids, $atts, $layout, $size )
624 ], 200 );
625 }
626
627 function rest_fetch_posts( $request ) {
628 try {
629 $params = $request->get_json_params();
630 $search = isset($params['search']) ? $params['search'] : '';
631 $offset = isset($params['offset']) ? intval($params['offset']) : 0;
632 $limit = isset($params['limit']) ? intval($params['limit']) : 10;
633
634 global $wpdb;
635 $searchPlaceholder = $search ? '%' . $search . '%' : '';
636 $where_search_clause = $search ? $wpdb->prepare(
637 "AND ( p.post_title LIKE %s OR p.post_content LIKE %s OR p.post_name LIKE %s ) ",
638 $searchPlaceholder,
639 $searchPlaceholder,
640 $searchPlaceholder
641 ) : '';
642
643 $posts = $wpdb->get_results(
644 $wpdb->prepare(
645 "SELECT p.ID, p.post_title, p.post_date, p.post_status, u.display_name as author
646 FROM $wpdb->posts p
647 LEFT JOIN $wpdb->users u ON p.post_author = u.ID
648 WHERE p.post_type = 'post'
649 AND p.post_status IN ('publish', 'draft', 'private')
650 $where_search_clause
651 ORDER BY p.post_date DESC
652 LIMIT %d, %d",
653 $offset,
654 $limit
655 ),
656 OBJECT
657 );
658
659 $posts_count = (int)$wpdb->get_var(
660 "SELECT COUNT(*)
661 FROM $wpdb->posts p
662 WHERE p.post_type = 'post'
663 AND p.post_status IN ('publish', 'draft', 'private')
664 $where_search_clause"
665 );
666
667 $data = array_map(function($post) {
668 return [
669 'id' => $post->ID,
670 'title' => $post->post_title,
671 'date' => $post->post_date,
672 'author' => $post->author,
673 'status' => $post->post_status
674 ];
675 }, $posts);
676
677 return new WP_REST_Response([
678 'success' => true,
679 'data' => $data,
680 'total' => $posts_count
681 ], 200);
682 } catch (Exception $e) {
683 return new WP_REST_Response(['success' => false, 'message' => $e->getMessage()], 500);
684 }
685 }
686
687 }
688
689 ?>