PluginProbe
Post Grid Gutenberg Blocks – PostX / 4.1.30
Post Grid Gutenberg Blocks – PostX v4.1.30
5.0.39 5.0.38 5.0.37 5.0.36 5.0.35 5.0.34 5.0.33 5.0.32 5.0.31 5.0.30 5.0.29 5.0.28 5.0.27 5.0.26 5.0.25 5.0.23 5.0.24 5.0.22 5.0.21 5.0.20 5.0.19 5.0.18 5.0.17 2.1.1 2.1.2 All 237 releases
ultimate-post / classes / REST_API.php

REST_API.php in Post Grid Gutenberg Blocks – PostX 4.1.30, at classes/REST_API.php

626 lines 24.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API Action.
4 *
5 * @package ULTP\REST_API
6 * @since v.1.0.0
7 */
8 namespace ULTP;
9 defined('ABSPATH') || exit;
10
11 /**
12 * REST_API class.
13 */
14 class REST_API {
15
16 /**
17 * Setup class.
18 *
19 * @since v.1.0.0
20 */
21 public function __construct() {
22 add_action( 'rest_api_init', array($this, 'ultp_register_route') );
23 }
24
25 /**
26 * REST API Action
27 *
28 * @since v.1.0.0
29 * @return NULL
30 */
31 public function ultp_register_route() {
32 register_rest_route( 'ultp', 'common_data', array(
33 'methods' => \WP_REST_Server::READABLE,
34 'args' => array('wpnonce' => []),
35 'callback' => array($this,'ultp_route_common_data'),
36 'permission_callback' => function () {
37 return current_user_can( 'edit_posts' );
38 },
39 )
40 );
41 register_rest_route(
42 'ultp',
43 '/fetch_posts/',
44 array(
45 array(
46 'methods' => 'POST',
47 'args' => array(),
48 'callback' => array($this, 'ultp_route_post_data'),
49 'permission_callback' => function () {
50 return current_user_can( 'edit_posts' );
51 },
52 )
53 )
54 );
55 register_rest_route(
56 'ultp',
57 '/specific_taxonomy/',
58 array(
59 array(
60 'methods' => 'POST',
61 'args' => array(),
62 'callback' => array($this, 'ultp_route_taxonomy_info_data'),
63 'permission_callback' => function () {
64 return current_user_can( 'edit_others_posts' );
65 },
66 )
67 )
68 );
69 register_rest_route(
70 'ultp/v1',
71 '/search/',
72 array(
73 array(
74 'methods' => 'POST',
75 'callback' => array($this, 'search_settings_action'),
76 'permission_callback' => function () {
77 return current_user_can('edit_others_posts');
78 },
79 'args' => array()
80 )
81 )
82 );
83 register_rest_route(
84 'ultp/v2',
85 '/premade_wishlist_save/',
86 array(
87 array(
88 'methods' => 'POST',
89 'callback' => array($this, 'premade_wishlist_save'),
90 'permission_callback' => function () {
91 return current_user_can('edit_others_posts');
92 },
93 'args' => array()
94 )
95 )
96 );
97 register_rest_route(
98 'ultp',
99 '/ultp_search_data/',
100 array(
101 array(
102 'methods' => 'POST',
103 'callback' => array($this, 'ultp_search_result'),
104 'permission_callback' => '__return_true'
105 )
106 )
107 );
108 register_rest_route(
109 'ultp/v2',
110 '/custom_tax/',
111 array(
112 array(
113 'methods' => 'POST',
114 'callback' => array( $this, 'custom_tax_callback'),
115 'permission_callback' => function () {
116 return current_user_can( 'manage_options' );
117 },
118 'args' => array()
119 )
120 )
121 );
122 register_rest_route(
123 'ultp/v2',
124 '/init_site_dark_logo/',
125 array(
126 array(
127 'methods' => 'POST',
128 'callback' => array( $this, 'init_site_dark_logo_callback'),
129 'permission_callback' => function () {
130 return current_user_can( 'edit_others_posts' );
131 },
132 'args' => array()
133 )
134 )
135 );
136 register_rest_route(
137 'ultp/v2',
138 '/get_ultp_image_size/',
139 array(
140 array(
141 'methods' => 'POST',
142 'callback' => array( $this, 'get_custom_image_size'),
143 'permission_callback' => function () {
144 return current_user_can( 'edit_posts' );
145 },
146 'args' => array()
147 )
148 )
149 );
150 }
151
152
153 /**
154 * Save and get premade_wishlist_save
155 *
156 * @since v.3.0.0
157 * @param STRING
158 * @return ARRAY | Inserted Post Url
159 */
160 public function premade_wishlist_save($server) {
161 $post = $server->get_params();
162 $id = isset($post['id'])? ultimate_post()->ultp_rest_sanitize_params($post['id']):'';
163 $action = isset($post['action'])? ultimate_post()->ultp_rest_sanitize_params($post['action']):'';
164 $wishListArr = get_option('ultp_premade_wishlist', []);
165 $request_type = isset($post['type'])?ultimate_post()->ultp_rest_sanitize_params($post['type']):'';
166
167 if ($id && $request_type != 'fetchData') {
168 if($action == 'remove') {
169 $index = array_search($id, $wishListArr);
170 if ($index !== false) {
171 unset($wishListArr[$index]);
172 }
173 } else {
174 if (!in_array($id, $wishListArr)) {
175 array_push($wishListArr, $id );
176 }
177 }
178 update_option('ultp_premade_wishlist', $wishListArr);
179 }
180 return rest_ensure_response([
181 'success' => true,
182 'message' => $action == 'remove' ? __('Item has been removed from wishlist.', 'ultimate-post') : __('Item added to wishlist.', 'ultimate-post'),
183 'wishListArr' => wp_json_encode($wishListArr)]
184 );
185 }
186
187
188
189 public function search_settings_action($server) {
190 global $wpdb;
191 $post = $server->get_params();
192 $request_type = isset($post['type'])?ultimate_post()->ultp_rest_sanitize_params($post['type']):'';
193 $condition_type = isset($post['condition'])?ultimate_post()->ultp_rest_sanitize_params($post['condition']):'';
194 $term_type = isset($post['term'])?ultimate_post()->ultp_rest_sanitize_params( $post['term'] ):'';
195 switch ($request_type) {
196 case 'posts':
197 case 'allpost':
198 case 'postExclude':
199 $post_type = array('post');
200 if ($request_type == 'allpost') {
201 $post_type = array_keys(ultimate_post()->get_post_type());
202 } else if ($request_type == 'postExclude') {
203 $post_type = array($condition_type);
204 }
205 $args = array(
206 'post_type' => $post_type,
207 'post_status' => 'publish',
208 'posts_per_page' => 10,
209 );
210 if (is_numeric($term_type)) {
211 $args['p'] = $term_type;
212 } else {
213 $args['s'] = $term_type;
214 }
215
216 $post_results = new \WP_Query($args);
217 $data = [];
218 if (!empty($post_results)) {
219 while ( $post_results->have_posts() ) {
220 $post_results->the_post();
221 $id = get_the_ID();
222 $title = html_entity_decode(get_the_title());
223 $data[] = array('value'=>$id, 'title'=>($title?'[ID: '.$id.'] '.$title:('[ID: '.$id.']')));
224 }
225 wp_reset_postdata();
226 }
227 return ['success' => true, 'data' => $data];
228 break;
229
230 case 'author':
231 $term = '%'. $wpdb->esc_like( $term_type ) .'%';
232 $post_results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
233 $wpdb->prepare(
234 "SELECT ID, display_name
235 FROM $wpdb->users
236 WHERE user_login LIKE %s OR ID LIKE %s OR user_nicename LIKE %s OR user_email LIKE %s OR display_name LIKE %s LIMIT 10", $term, $term, $term, $term, $term
237 )
238 );
239 $data = [];
240 if (!empty($post_results)) {
241 foreach ($post_results as $key => $val) {
242 $data[] = array('value'=>$val->ID, 'title'=>'[ID: '.$val->ID.'] '.$val->display_name);
243 }
244 }
245 return ['success' => true, 'data' => $data];
246 break;
247
248 case 'taxvalue':
249 $split = explode('###', $condition_type);
250 $condition = $split[1] != 'multiTaxonomy' ? array($split[1]) : get_object_taxonomies($split[0]);
251 $args = array(
252 'taxonomy' => $condition,
253 'fields' => 'all',
254 'orderby' => 'id',
255 'order' => 'ASC',
256 'name__like'=> $term_type
257 );
258 if (is_numeric($term_type)) {
259 unset($args['name__like']);
260 $args['include'] = array($term_type);
261 }
262
263 $post_results = get_terms( $args );
264 $data = [];
265 if (!empty($post_results)) {
266 foreach ($post_results as $key => $val) {
267 if ($split[1] == 'multiTaxonomy') {
268 $data[] = array('value'=>$val->taxonomy.'###'.$val->slug, 'title'=> '[ID: '.$val->term_id.'] '.$val->taxonomy.': '.$val->name);
269 } else {
270 $data[] = array('value'=>urldecode($val->slug), 'title'=>'[ID: '.$val->term_id.'] '.$val->name, 'live_title'=> $val->name);
271 }
272 }
273 }
274 return ['success' => true, 'data' => $data];
275 break;
276
277 case 'taxExclude':
278 $condition = get_object_taxonomies($condition_type);
279 $args = array(
280 'taxonomy' => $condition,
281 'fields' => 'all',
282 'orderby' => 'id',
283 'order' => 'ASC',
284 'name__like'=> $term_type
285 );
286 if (is_numeric($term_type)) {
287 unset($args['name__like']);
288 $args['include'] = array($term_type);
289 }
290 $post_results = get_terms( $args );
291 $data = [];
292 if (!empty($post_results)) {
293 foreach ($post_results as $key => $val) {
294 $data[] = array('value'=>$val->taxonomy.'###'.$val->slug, 'title'=> '[ID: '.$val->term_id.'] '.$val->taxonomy.': '.$val->name);
295 }
296 }
297 return ['success' => true, 'data' => $data];
298 break;
299 // allPostType
300 case 'allPostType':
301 $all_types = array_values(get_post_types( array( 'public' => true ), 'names' ));
302 $postType = array();
303 foreach($all_types as $type){
304 $postType[] = array(
305 'title' => $type,
306 'value' => $type,
307 );
308 };
309
310 return ['success' => true, 'data' => $postType ];
311 default:
312 return ['success' => true, 'data' => [['value'=>'', 'title'=>'- Select -']]];
313 break;
314 }
315 }
316
317 /**
318 * Post Data Response of REST API
319 *
320 * @since v.1.0.0
321 * @param MIXED | Pram (ARRAY), Local (BOOLEAN)
322 * @return ARRAY | Response Image Size as Array
323 */
324 public function ultp_route_post_data($prams) {
325 $prams = $prams->get_params();
326 if ( !(isset($prams['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($prams['wpnonce'])), 'ultp-nonce' )) ) {
327 die();
328 }
329 $data = [];
330 $loop = new \WP_Query( ultimate_post()->get_query(ultimate_post()->ultp_rest_sanitize_params($prams)) );
331 $max_tax = isset($prams['maxTaxonomy']) && $prams['maxTaxonomy'] ? ( ultimate_post()->ultp_rest_sanitize_params($prams['maxTaxonomy']) == '0' ? 0 : ultimate_post()->ultp_rest_sanitize_params($prams['maxTaxonomy']) ) : 30 ;
332
333 if ($loop->have_posts()) {
334 while($loop->have_posts()) {
335 $loop->the_post();
336 $var = array();
337 $post_id = get_the_ID();
338 $user_id = get_the_author_meta('ID');
339 $content_data = get_the_content();
340 $var['ID'] = $post_id;
341 $var['title'] = get_the_title();
342 $var['permalink'] = get_permalink();
343 $var['seo_meta'] = ultimate_post()->get_excerpt($post_id, 1);
344 $var['excerpt'] = wp_strip_all_tags(get_the_excerpt());
345 $var['excerpt_full']= wp_strip_all_tags(get_the_excerpt());
346 $var['time'] = (int)get_the_date('U')*1000;
347 $var['timeModified']= (int)get_the_modified_date('U')*1000;
348 $var['post_time'] = human_time_diff(get_the_time('U'),current_time('U'));
349 $var['view'] = get_post_meta(get_the_ID(),'__post_views_count', true);
350 $var['comments'] = get_comments_number();
351 $var['author_link'] = get_author_posts_url($user_id);
352 $var['avatar_url'] = get_avatar_url($user_id);
353 $var['display_name']= get_the_author_meta('display_name');
354 $var['reading_time']= ceil(strlen($content_data)/1200);
355 $var['acf'] = null;
356
357 if (function_exists('get_field_objects')) {
358 $var['acf'] = get_field_objects();
359 }
360
361 $post_video = get_post_meta($post_id, '__builder_feature_video', true);
362 // Video
363 if ($post_video) {
364 $var['has_video'] = ultimate_post()->get_youtube_id($post_video);
365 }
366 // image
367 $image_sizes = ultimate_post()->get_image_size();
368 $image_src = array();
369 if (has_post_thumbnail()) {
370 $thumb_id = get_post_thumbnail_id($post_id);
371 foreach ($image_sizes as $key => $value) {
372 $image_src[$key] = wp_get_attachment_image_src($thumb_id, $key, false)[0];
373 }
374 $var['image'] = $image_src;
375 } elseif(isset($prams['fallbackImg']['id'])) {
376 foreach ($image_sizes as $key => $value) {
377 $image_src[$key] = wp_get_attachment_image_src(esc_attr($prams['fallbackImg']['id']), $key, false)[0];
378 }
379 $var['image'] = $image_src;
380 $var['is_fallback'] = true;
381 }
382
383 // tag
384 $tag = get_the_terms($post_id, (isset($prams['tag'])?esc_attr($prams['tag']):'post_tag'));
385 if (!empty($tag)) {
386 $v = array();
387 foreach ($tag as $k => $val) {
388 if ($k >= $max_tax) { break; }
389 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id));
390 }
391 $var['tag'] = $v;
392 }
393
394 // Taxonomy
395 $cat = get_the_terms($post_id, (isset($prams['taxonomy'])?esc_attr($prams['taxonomy']):'category'));
396
397 if(!empty($cat)){
398 $v = array();
399 foreach ($cat as $k => $val) {
400 if ($k >= $max_tax) { break; }
401 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id), 'color' => get_term_meta($val->term_id, 'ultp_category_color', true));
402 }
403 $var['category'] = $v;
404 }
405 $data[] = $var;
406 }
407 wp_reset_postdata();
408 }
409 return rest_ensure_response( $data);
410 }
411
412
413 /**
414 * Taxonomy Data Response of REST API
415 *
416 * @since v.1.0.0
417 * @param ARRAY | Parameter (ARRAY)
418 * @return ARRAY | Response Taxonomy List as Array
419 */
420 public function ultp_route_common_data($prams) {
421 if ( ! (isset($_REQUEST['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($_REQUEST['wpnonce'])), 'ultp-nonce' )) ) {
422 return rest_ensure_response([]);
423 }
424
425 $all_post_type = ultimate_post()->get_post_type();
426 $data = array();
427 foreach ($all_post_type as $post_type_slug => $post_type ) {
428 $data_term = array();
429 $taxonomies = get_object_taxonomies($post_type_slug);
430 foreach ($taxonomies as $key => $taxonomy_slug) {
431 $taxonomy_value = get_terms(array(
432 'taxonomy' => $taxonomy_slug,
433 'hide_empty' => false
434 ));
435 if (!is_wp_error($taxonomy_value)) {
436 $data_tax = array();
437 foreach ($taxonomy_value as $k => $taxonomy) {
438 $data_tax[urldecode_deep($taxonomy->slug)] = $taxonomy->name;
439 }
440 if (count($data_tax) > 0) {
441 $data_term[$taxonomy_slug] = $data_tax;
442 }
443 }
444 }
445 $data[$post_type_slug] = $data_term;
446 }
447 // Global Customizer
448 $global = get_option('postx_global', []);
449 // Image Size
450 $image_sizes = ultimate_post()->get_image_size();
451
452 return rest_ensure_response(['taxonomy' => $data, 'global' => $global, 'image' => wp_json_encode($image_sizes), 'posttype' => wp_json_encode($all_post_type)]);
453 }
454
455 /**
456 * Specific Taxonomy Data Response of REST API
457 *
458 * @since v.1.0.0
459 * @param ARRAY | Parameter (ARRAY)
460 * @return ARRAY | Response Taxonomy List as Array
461 */
462 public function ultp_route_taxonomy_info_data($prams) {
463 $prams = $prams->get_params();
464 if ( ! (isset($prams['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($prams['wpnonce'])), 'ultp-nonce' )) ) {
465 return rest_ensure_response([]);
466 }
467 $taxValue = isset($prams['taxValue'])?ultimate_post()->ultp_rest_sanitize_params($prams['taxValue']):'';
468 $queryNumber = isset($prams['queryNumber'])?ultimate_post()->ultp_rest_sanitize_params($prams['queryNumber']):'';
469 $taxType = isset($prams['taxType'])?ultimate_post()->ultp_rest_sanitize_params($prams['taxType']):'';
470 $taxSlug = isset($prams['taxSlug'])?ultimate_post()->ultp_rest_sanitize_params($prams['taxSlug']):'';
471 $archiveBuilder = isset($prams['archiveBuilder'])?ultimate_post()->ultp_rest_sanitize_params($prams['archiveBuilder']):'';
472
473 return rest_ensure_response( ultimate_post()->get_category_data(json_decode($taxValue), $queryNumber, $taxType, $taxSlug, $archiveBuilder) );
474 }
475
476 /**
477 * Get Taxonomies for Custom Post Type
478 *
479 * @since v.3.2.8
480 * @param array $params
481 * @return array
482 */
483 public function custom_tax_callback($prams) {
484
485 $post_types = isset($prams['postTypes']) ? ultimate_post()->ultp_rest_sanitize_params($prams['postTypes']) : array();
486
487 $data = array(
488 array(
489 'id' => '_all',
490 'name' => __('All', 'ultimate-post')
491 )
492 );
493
494 foreach ($post_types as $post_type) {
495 $taxonomies = get_object_taxonomies($post_type);
496 foreach ($taxonomies as $taxonomy) {
497 $terms = get_terms(array(
498 'taxonomy' => $taxonomy,
499 'hide_empty' => false
500 ));
501 foreach ($terms as $term) {
502 $data[] = array(
503 'id' => $term->slug,
504 'name' => $term->name
505 );
506 }
507 }
508 }
509
510 return rest_ensure_response($data);
511 }
512
513 /**
514 * Search Block Data Showing
515 *
516 * @since v.2.9.9
517 * @param STRING
518 * @return ARRAY | Inserted Post Url
519 */
520 public function ultp_search_result($server) {
521 $post = $server->get_params();
522 $searchText = isset($post['searchText'])?ultimate_post()->ultp_rest_sanitize_params($post['searchText']):'';
523 $paged = isset($post['paged'])?ultimate_post()->ultp_rest_sanitize_params($post['paged']):'';
524 $postPerPage = isset($post['postPerPage'])?ultimate_post()->ultp_rest_sanitize_params($post['postPerPage']):'';
525 $query_args = array(
526 's' => $searchText,
527 'paged' => $paged,
528 'compare' => 'LIKE',
529 'orderby' => 'relevance',
530 'posts_per_page' => $postPerPage,
531 );
532 if(isset($post['exclude']) && is_array($post['exclude']) && count($post['exclude']) > 0) {
533 $post['exclude'] = ultimate_post()->ultp_rest_sanitize_params($post['exclude']);
534 $post_exclude = array();
535 foreach( $post['exclude'] as $data ){
536 $post_exclude[$data['title']] = $data['title'];
537 }
538 $all_types = get_post_types( array( 'public' => true ), 'names' );
539 $post_type = array_diff_key($all_types, $post_exclude);
540 $query_args['post_type'] = $post_type;
541 }
542 $output = '';
543 $query_result = new \WP_Query($query_args);
544
545 if ($query_result->have_posts()) {
546 while ($query_result->have_posts()) {
547 $query_result->the_post();
548 $post_id = get_the_ID();
549 $title = get_the_title();
550
551 $output .= '<div class="ultp-search-result__item">';
552 if ($post['image'] == 1 && has_post_thumbnail()) {
553 $thumb_id = get_post_thumbnail_id($post_id);
554 $output .= '<img class="ultp-searchresult-image" src='.wp_get_attachment_image_src($thumb_id, 'thumbnail', false)[0].' alt="'.$title.'"/>';
555 }
556 $output .= '<div class="ultp-searchresult-content">';
557 $output .= '<div class="ultp-rescontent-meta">';
558 // Category
559 $post_cat = get_the_terms($post_id, 'category');
560 if ($post['category'] == 1 && $post_cat && count($post_cat)) {
561 $output .= '<div class="ultp-searchresult-category">';
562 foreach($post_cat as $cat){
563 $output .= '<a href="'.get_term_link($cat->term_id).'">'.$cat->name.'</a>';
564 }
565 $output .= '</div>';
566 }
567 // Author
568 if ($post['author'] == 1) {
569 $user_id = get_the_author_meta('ID');
570 $output .= '<a href="'.get_author_posts_url($user_id).'" class="ultp-searchresult-author">'.get_the_author_meta('display_name').'</a>';
571 }
572 // Date
573 if ($post['date'] == 1) {
574 $output .= '<div class="ultp-searchresult-publishdate">'.get_the_date('F j, Y').'</div>';
575 }
576 $output .= '</div>';
577 $output .= '<a href="'.get_permalink().'" class="ultp-searchresult-title">'.$title.'</a>';
578 if ($post['excerpt'] == 1) {
579 $output .= '<div class="ultp-searchresult-excerpt">'.wp_trim_words(get_the_excerpt(), isset($post['excerptLimit'])?ultimate_post()->ultp_rest_sanitize_params($post['excerptLimit']):55).'</div>';
580 }
581 $output .= '</div>';
582 $output .= '</div>';
583 }
584 }
585
586 return array('post_data' => $output, 'post_count' => $query_result->found_posts);
587 }
588
589 /**
590 * PostX Site Dark Logo Init
591 *
592 * @since v.3.1.9
593 * @param ARRAY
594 * @return BOOOLEAN | Inserted Post Url
595 */
596 public function init_site_dark_logo_callback ($server) {
597 $logo_data = $server->get_params();
598 $success = true;
599 if( isset($logo_data['logo']['url'] ) ){
600 update_option( 'ultp_site_dark_logo', $logo_data['logo']['url'] );
601 } else {
602 $success = false;
603 }
604 return rest_ensure_response([
605 'success' => $success,
606 ]);
607 }
608
609
610 /**
611 * Getting Image Size
612 *
613 * @since v.4.0.1
614 * @param ARRAY | Parameter (number)
615 * @return ARRAY | Image Size List as Array
616 */
617 public function get_custom_image_size($server) {
618 $img = $server->get_params();
619 $image_src = array();
620 $image_sizes = ultimate_post()->get_image_size();
621 foreach ($image_sizes as $key => $value) {
622 $image_src[$key] = wp_get_attachment_image_src($img['id'], $key, false)[0];
623 }
624 return rest_ensure_response( ['success' => true, 'size' => $image_src, 'id' => $img ]);
625 }
626 }