PluginProbe
Meow Gallery / trunk
Meow Gallery vtrunk
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 4.3.1 All 156 releases
meow-gallery / classes / rest.php

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

692 lines 22.3 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 $full = !empty( $atts['full'] );
147 unset( $atts['full'] );
148
149 $is_collection = isset( $atts['collection'] ) && !empty( $atts['collection'] );
150 if ( $is_collection ) {
151 $html = do_shortcode( '[meow-collection id="' . $atts['collection'] . '"]' );
152 $counts = [ 'total' => 0, 'shown' => 0 ];
153 } else {
154 $this->core->last_preview_counts = [ 'total' => 0, 'shown' => 0 ];
155 if ( $full ) {
156 $this->core->preview_cutoff = PHP_INT_MAX;
157 }
158 $html = $this->core->gallery( $atts, [ 'isPreview' => true ] );
159 $counts = $this->core->last_preview_counts;
160 }
161
162
163 return new WP_REST_Response( [
164 'success' => true,
165 'data' => $html,
166 'total' => intval( $counts['total'] ),
167 'shown' => intval( $counts['shown'] ),
168 ], 200 );
169 }
170
171 function rest_load_gallery_collection( $request ) {
172 try {
173 $params = $request->get_json_params( );
174 $gallery_id = $params['id'];
175 $search_slug = $params['search_slug'];
176 $gallery_atts = $params['gallery_atts'];
177
178 $key = [
179 'gallery_id' => 'id',
180 'wplr_collection_id' => 'wplr-collection',
181 'rml' => 'rml',
182 ];
183
184 $shortcode_atts = array();
185 $shortcode_atts[ $key[$search_slug] ] = $gallery_id;
186 $shortcode_atts = [...$shortcode_atts, ...$gallery_atts];
187
188 $html = $this->core->gallery( $shortcode_atts, [ 'isPreview' => false, 'isRest' => true ] );
189 $mwlData = json_encode( $this->core->get_rewritten_mwl_data( ) );
190 return new WP_REST_Response( [ 'success' => true, 'data' => $html, 'mwl_data' => $mwlData ], 200 );
191 }
192 catch ( Exception $e ) {
193 return new WP_REST_Response( [ 'success' => false, 'message' => $e->getMessage( ) ], 500 );
194 }
195 }
196
197 function rest_all_settings( ) {
198 return new WP_REST_Response( [ 'success' => true, 'data' => $this->core->get_all_options( ) ], 200 );
199 }
200
201 function rest_rml_folders( ) {
202 if ( ! Meow_MGL_RML::is_available() ) {
203 return new WP_REST_Response( [ 'success' => true, 'available' => false, 'data' => [] ], 200 );
204 }
205 return new WP_REST_Response( [ 'success' => true, 'available' => true, 'data' => Meow_MGL_RML::get_all_folders() ], 200 );
206 }
207
208 function rest_reset_options( ) {
209 $this->core->reset_options( );
210 return new WP_REST_Response( [ 'success' => true, 'options' => $this->core->get_all_options( ) ], 200 );
211 }
212
213 function rest_save_shortcode( $request ) {
214 try {
215 global $wpdb;
216 $params = $request->get_json_params( );
217
218 $id = $params['id'];
219 $medias = Meow_MGL_Core::normalize_medias( $params['medias'] ?? null );
220 $name = $params['name'];
221 $layout = $params['layout'];
222 $description = $params['description'];
223 $posts = $params['posts'];
224 $latest_posts = $params['latest_posts'];
225 $tags = $params['tags'];
226 $dynamic_source = $params['dynamic_source'];
227 $lead_image_id = $params['lead_image_id'];
228 $order_by = $params['order_by'];
229 $is_post_mode = $params['is_post_mode'];
230 $is_hero_mode = $params['is_hero_mode'];
231 $rml = $params['rml'] ?? null;
232
233 if ( !$name ) {
234 throw new Exception( __( 'Please enter a name for your shortcode.', MGL_DOMAIN ));
235 }
236
237 if ( !$is_post_mode && empty( $medias['thumbnail_ids'] ) ) {
238 throw new Exception( __( 'Please select at least one image.', MGL_DOMAIN ));
239 }
240
241 if ( $is_post_mode && $dynamic_source === 'posts' && ( !$posts && !$latest_posts )) {
242 throw new Exception( __( 'Please select at least one post.', MGL_DOMAIN ));
243 }
244
245 if ( $is_post_mode && $dynamic_source === 'tags' && !$tags ) {
246 throw new Exception( __( 'Please enter at least one tag.', MGL_DOMAIN ));
247 }
248
249 if ( $is_post_mode && $dynamic_source === 'rml' && empty( $rml ) ) {
250 throw new Exception( __( 'Please select a Real Media Library folder.', MGL_DOMAIN ));
251 }
252
253 if ( $is_hero_mode && !$is_post_mode ) {
254 throw new Exception( __( 'Hero mode is only available for post mode.', MGL_DOMAIN ));
255 }
256
257 if ( !$id || $id == '' ) {
258 $id = $this->core->generate_uniqid( 10 );
259 }
260
261 $shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
262 Meow_MGL_Migrations::check_db();
263
264 // Check if the record exists
265 $exists = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $shortcodes_table WHERE id = %s", $id ));
266
267 $data = [
268 'name' => $name,
269 'description' => $description,
270 'layout' => $layout,
271 'medias' => serialize( $medias ),
272 'lead_image_id' => $lead_image_id,
273 'order_by' => $order_by,
274 'is_post_mode' => $is_post_mode ? 1 : 0,
275 'is_hero_mode' => $is_hero_mode ? 1 : 0,
276 'posts' => $posts ? serialize( $posts ) : null,
277 'latest_posts' => $latest_posts,
278 'tags' => serialize( $tags ),
279 'dynamic_source' => $dynamic_source,
280 'rml' => $rml
281 ];
282
283 if ( $exists ) {
284 // Update existing record
285 $wpdb->update(
286 $shortcodes_table,
287 $data,
288 ['id' => $id]
289 );
290 } else {
291 // Insert new record
292 $data['id'] = $id;
293 $wpdb->insert( $shortcodes_table, $data );
294 }
295
296
297 return new WP_REST_Response( ['success' => true, 'message' => 'Shortcode created.'], 200 );
298 } catch ( Exception $e ) {
299 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage( )], 500 );
300 }
301 }
302
303 function rest_remove_collection( $request ) {
304 try {
305 global $wpdb;
306 $params = $request->get_json_params( );
307 $id = $params['id'];
308
309 $collections_table = $wpdb->prefix . 'mgl_collections';
310 Meow_MGL_Migrations::check_db();
311
312 $wpdb->delete( $collections_table, ['id' => $id] );
313
314
315 return new WP_REST_Response( ['success' => true, 'message' => 'Collection removed.'], 200 );
316 } catch ( Exception $e ) {
317 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage( )], 500 );
318 }
319 }
320
321 function rest_save_collection( $request ) {
322 try {
323 global $wpdb;
324 $params = $request->get_json_params( );
325
326 $id = $params['id'];
327 $name = $params['name'];
328 $layout = $params['layout'];
329 $galleries_ids = $params['galleries_ids'];
330 $description = $params['description'];
331
332 if ( !$name ) {
333 throw new Exception( __( 'Please enter a name for your collection.', MGL_DOMAIN ));
334 }
335
336 if ( !$galleries_ids || !count( $galleries_ids )) {
337 throw new Exception( __( 'Please select at least one gallery.', MGL_DOMAIN ));
338 }
339
340 if ( !$id || $id == '' ) {
341 $id = $this->core->generate_uniqid( 10 );
342 }
343
344 $collections_table = $wpdb->prefix . 'mgl_collections';
345 Meow_MGL_Migrations::check_db();
346
347 // Check if the record exists
348 $exists = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $collections_table WHERE id = %s", $id ));
349
350 $data = [
351 'name' => $name,
352 'description' => $description,
353 'layout' => $layout,
354 'galleries_ids' => serialize( $galleries_ids )
355 ];
356
357 if ( $exists ) {
358 // Update existing record
359 $wpdb->update(
360 $collections_table,
361 $data,
362 ['id' => $id]
363 );
364 } else {
365 // Insert new record
366 $data['id'] = $id;
367 $wpdb->insert( $collections_table, $data );
368 }
369
370
371 return new WP_REST_Response( ['success' => true, 'message' => 'Collection created.'], 200 );
372 } catch ( Exception $e ) {
373 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage( )], 500 );
374 }
375 }
376
377
378 function rest_fetch_collections( $request ) {
379 try {
380 $params = $request->get_json_params( );
381
382 $offset = isset( $params['offset'] ) ? $params['offset'] : 0;
383 $limit = isset( $params['limit'] ) ? $params['limit'] : 10;
384 $sort_updated = $params['sort']['by']; // desc, asc
385 $page = isset( $params['page'] ) ? $params['page'] : 1;
386 $order = $sort_updated === 'desc' ? 'DESC' : 'ASC';
387 $search = isset( $params['search'] ) ? $params['search'] : '';
388
389 $res = $this->core->get_collections( $offset, $limit, $order, $page, $search );
390 $collections = $res['collections'];
391 $total = $res['total'];
392
393 return new WP_REST_Response( ['success' => true, 'data' => $collections, 'total' => $total], 200 );
394 }
395 catch ( Exception $e ) {
396 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage( )], 500 );
397 }
398 }
399
400 function rest_fetch_gallery_items( $request ) {
401 try {
402 global $wpdb;
403 $params = $request->get_json_params( );
404 $galleryIds = $params['galleryIds'];
405
406 $shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
407 Meow_MGL_Migrations::check_db();
408
409 $galleries = [];
410 if ( !empty( $galleryIds )) {
411 $ids_str = "'" . implode( "','", array_map( 'esc_sql', $galleryIds )) . "'";
412 $query = "SELECT * FROM $shortcodes_table WHERE id IN ( $ids_str )";
413 $results = $wpdb->get_results( $query, ARRAY_A );
414
415 foreach ( $results as $gallery ) {
416 // Transform database format to match expected format
417 $galleries[$gallery['id']] = [
418 'name' => $gallery['name'],
419 'description' => $gallery['description'],
420 'layout' => $gallery['layout'],
421 'medias' => Meow_MGL_Core::hydrate_medias( maybe_unserialize( $gallery['medias'] ) ),
422 'is_post_mode' => ( bool )$gallery['is_post_mode'],
423 'hero' => ( bool )$gallery['is_hero_mode'],
424 'posts' => $gallery['posts'] ? unserialize( $gallery['posts'] ) : null,
425 'latest_posts' => $gallery['latest_posts'],
426 'tags' => unserialize( $gallery['tags'] ),
427 'dynamic_source' => $gallery['dynamic_source'],
428 'updated' => strtotime( $gallery['updated_at'] )
429 ];
430 }
431 }
432
433 return new WP_REST_Response( ['success' => true, 'data' => $galleries], 200 );
434 } catch ( Exception $e ) {
435 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage( )], 500 );
436 }
437 }
438
439 function rest_fetch_shortcodes( $request ) {
440 try {
441 $params = $request->get_json_params( );
442
443 $offset = isset( $params['offset'] ) ? $params['offset'] : 0;
444 $limit = isset( $params['limit'] ) ? $params['limit'] : 10;
445 $page = isset( $params['page'] ) ? $params['page'] : 1;
446
447 $search = isset( $params['search'] ) ? $params['search'] : '';
448
449 $sort_by = $params['sort']['accessor'] ?? null;
450 $order_by = strtoupper( $params['sort']['by'] ); // desc, asc
451
452 $res = $this->core->get_galleries( $offset, $limit, $order_by, $sort_by, $page, $search );
453 $shortcodes = $res['galleries'];
454 $total = $res['total'];
455
456 return new WP_REST_Response( ['success' => true, 'data' => $shortcodes, 'total' => $total], 200 );
457 }
458 catch ( Exception $e ) {
459 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage( )], 500 );
460 }
461 }
462
463 function rest_remove_shortcode( $request ) {
464 try {
465 global $wpdb;
466 $params = $request->get_json_params( );
467 $id = $params['id'];
468
469 $shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
470 Meow_MGL_Migrations::check_db();
471
472 $wpdb->delete( $shortcodes_table, ['id' => $id] );
473
474
475 return new WP_REST_Response( ['success' => true, 'message' => 'Shortcode removed.'], 200 );
476 } catch ( Exception $e ) {
477 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage( )], 500 );
478 }
479 }
480
481 function rest_update_gallery_rank( $request ) {
482 try {
483 global $wpdb;
484 $params = $request->get_json_params();
485 $id = $params['id'];
486 $direction = $params['direction']; // 'up' or 'down'
487
488 $shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
489 Meow_MGL_Migrations::check_db();
490
491 // Get current rank
492 $current_rank = $wpdb->get_var( $wpdb->prepare( "SELECT pref_rank FROM $shortcodes_table WHERE id = %s", $id ) );
493 $current_rank = intval( $current_rank );
494
495 // Calculate new rank (up = higher priority = higher number, down = lower priority = lower number)
496 $new_rank = $direction === 'up' ? $current_rank + 1 : $current_rank - 1;
497
498 // Update the rank
499 $wpdb->update(
500 $shortcodes_table,
501 ['pref_rank' => $new_rank],
502 ['id' => $id]
503 );
504
505 return new WP_REST_Response( ['success' => true, 'message' => 'Gallery rank updated.', 'new_rank' => $new_rank], 200 );
506 } catch ( Exception $e ) {
507 return new WP_REST_Response( ['success' => false, 'message' => $e->getMessage()], 500 );
508 }
509 }
510
511 function rest_latest_photos( $request ) {
512
513 $search = trim( $request->get_param( 'search' ) );
514 $offset = trim( $request->get_param( 'offset' ) );
515 $limit = trim( $request->get_param( 'limit' ) );
516
517 $except = json_decode( trim( $request->get_param( 'except' ) ), true );
518 $unusedImages = trim( $request->get_param( 'unusedImages' ) );
519
520 global $wpdb;
521 $searchPlaceholder = $search ? '%' . $search . '%' : '';
522 $where_search_clause = $search ? $wpdb->prepare(
523 "AND ( p.post_title LIKE %s OR p.post_content LIKE %s OR p.post_name LIKE %s ) ",
524 $searchPlaceholder,
525 $searchPlaceholder,
526 $searchPlaceholder
527 ) : '';
528 $where_search_clause .= $except && count( $except ) ? $wpdb->prepare(
529 "AND p.ID NOT IN ( " . implode( ', ', array_fill( 0, count( $except ), '%s' )) . " )", $except
530 ) : '';
531 $join_clause = '';
532 if ( $unusedImages ) {
533 // Every image used by a gallery, read from the galleries table (this used to read the
534 // old 'mgl_shortcodes' option, which isn't written anymore since the migration).
535 $shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
536 Meow_MGL_Migrations::check_db();
537
538 $used_thumbnail_ids = [];
539 foreach ( $wpdb->get_col( "SELECT medias FROM $shortcodes_table" ) as $medias ) {
540 $medias = Meow_MGL_Core::normalize_medias( maybe_unserialize( $medias ) );
541 $used_thumbnail_ids = array_merge( $used_thumbnail_ids, $medias['thumbnail_ids'] );
542 }
543
544 // Make sure the IDs are integers
545 $used_thumbnail_ids = array_unique( array_map( 'intval', $used_thumbnail_ids ) );
546
547 // Include the NOT IN clause to exclude used thumbnail IDs
548 if ( !empty( $used_thumbnail_ids ) ) {
549 $placeholders = implode( ',', array_fill( 0, count( $used_thumbnail_ids ), '%d' ) );
550 $where_search_clause .= $wpdb->prepare( " AND p.ID NOT IN ( $placeholders ) ", $used_thumbnail_ids );
551 }
552 }
553 $posts = $wpdb->get_results(
554 $wpdb->prepare(
555 "SELECT p.ID, p.post_title, p.post_mime_type
556 FROM $wpdb->posts p
557 $join_clause
558 WHERE p.post_type='attachment'
559 AND p.post_status='inherit'
560 $where_search_clause
561 ORDER BY p.post_modified DESC
562 LIMIT %d, $limit", $offset
563 ), OBJECT
564 );
565 $posts_count = ( int )$wpdb->get_var(
566 "SELECT COUNT( * )
567 FROM $wpdb->posts p
568 $join_clause
569 WHERE p.post_type='attachment'
570 AND p.post_status='inherit'
571 $where_search_clause"
572 );
573
574 $data = [];
575 foreach ( $posts as $post ) {
576 $file_url = get_attached_file( $post->ID );
577
578 $mime = $post->post_mime_type;
579 $is_video = ( strpos( $mime, 'video' ) !== false );
580
581 $thumbnail_url = $is_video ? wp_get_attachment_url( $post->ID ) : wp_get_attachment_image_url( $post->ID, 'thumbnail' );
582
583 if ( file_exists( $file_url ) ) {
584 $data[] = [
585 'id' => $post->ID,
586 'thumbnail_url' => $thumbnail_url,
587 'zoom_url' => wp_get_attachment_image_url( $post->ID, 'large' ),
588 'title' => $post->post_title,
589 'filename' => basename( $file_url ),
590 'size' => size_format( filesize( $file_url ) ),
591 'mime' => $mime,
592 ];
593 }
594 }
595 return new WP_REST_Response( [
596 'success' => true,
597 'data' => $data,
598 'total' => $posts_count
599 ], 200 );
600 }
601
602 function rest_update_option( $request ) {
603 try {
604 $params = $request->get_json_params( );
605 $value = $params['options'];
606 $options = $this->core->update_options( $value );
607 $success = !!$options;
608 $message = __( $success ? 'OK' : "Could not update options.", MGL_DOMAIN );
609 return new WP_REST_Response( [ 'success' => $success, 'message' => $message, 'options' => $success ? $options : null ], 200 );
610 }
611 catch ( Exception $e ) {
612 return new WP_REST_Response( [ 'success' => false, 'message' => $e->getMessage( ) ], 500 );
613 }
614 }
615
616 function rest_images( $request ) {
617 $params = $request->get_json_params( );
618
619 $image_ids = $params['imageIds'];
620 $atts = $params['atts'];
621 $layout = trim( $params['layout'] );
622 $size = trim( $params['size'] );
623
624 return new WP_REST_Response( [
625 'success' => true,
626 'data' => $this->core->get_gallery_images( $image_ids, $atts, $layout, $size )
627 ], 200 );
628 }
629
630 function rest_fetch_posts( $request ) {
631 try {
632 $params = $request->get_json_params();
633 $search = isset($params['search']) ? $params['search'] : '';
634 $offset = isset($params['offset']) ? intval($params['offset']) : 0;
635 $limit = isset($params['limit']) ? intval($params['limit']) : 10;
636
637 global $wpdb;
638 $searchPlaceholder = $search ? '%' . $search . '%' : '';
639 $where_search_clause = $search ? $wpdb->prepare(
640 "AND ( p.post_title LIKE %s OR p.post_content LIKE %s OR p.post_name LIKE %s ) ",
641 $searchPlaceholder,
642 $searchPlaceholder,
643 $searchPlaceholder
644 ) : '';
645
646 $posts = $wpdb->get_results(
647 $wpdb->prepare(
648 "SELECT p.ID, p.post_title, p.post_date, p.post_status, u.display_name as author
649 FROM $wpdb->posts p
650 LEFT JOIN $wpdb->users u ON p.post_author = u.ID
651 WHERE p.post_type = 'post'
652 AND p.post_status IN ('publish', 'draft', 'private')
653 $where_search_clause
654 ORDER BY p.post_date DESC
655 LIMIT %d, %d",
656 $offset,
657 $limit
658 ),
659 OBJECT
660 );
661
662 $posts_count = (int)$wpdb->get_var(
663 "SELECT COUNT(*)
664 FROM $wpdb->posts p
665 WHERE p.post_type = 'post'
666 AND p.post_status IN ('publish', 'draft', 'private')
667 $where_search_clause"
668 );
669
670 $data = array_map(function($post) {
671 return [
672 'id' => $post->ID,
673 'title' => $post->post_title,
674 'date' => $post->post_date,
675 'author' => $post->author,
676 'status' => $post->post_status
677 ];
678 }, $posts);
679
680 return new WP_REST_Response([
681 'success' => true,
682 'data' => $data,
683 'total' => $posts_count
684 ], 200);
685 } catch (Exception $e) {
686 return new WP_REST_Response(['success' => false, 'message' => $e->getMessage()], 500);
687 }
688 }
689
690 }
691
692 ?>