PluginProbe
Meow Gallery / 5.4.9
Meow Gallery v5.4.9
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.4.9, at classes/rest.php

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