PluginProbe
Post Grid Gutenberg Blocks – PostX / 3.2.2
Post Grid Gutenberg Blocks – PostX v3.2.2
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 3.2.2, at classes/REST_API.php

951 lines 38.6 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
10 defined('ABSPATH') || exit;
11
12 /**
13 * Styles class.
14 */
15 class REST_API{
16
17 /**
18 * Setup class.
19 *
20 * @since v.1.0.0
21 */
22 public function __construct() {
23 add_action( 'rest_api_init', array($this, 'ultp_register_route') );
24 }
25
26
27 /**
28 * REST API Action
29 *
30 * @since v.1.0.0
31 * @return NULL
32 */
33 public function ultp_register_route() {
34 register_rest_route( 'ultp', 'posts', array(
35 'args' => array(),
36 'callback' => array($this,'ultp_route_post_data'),
37 'permission_callback' => '__return_true'
38 )
39 );
40 register_rest_route( 'ultp', 'common', array(
41 'methods' => \WP_REST_Server::READABLE,
42 'args' => array('wpnonce' => []),
43 'callback' => array($this,'ultp_route_common_data'),
44 'permission_callback' => '__return_true'
45 )
46 );
47 register_rest_route( 'ultp', 'specific_taxonomy', array(
48 'methods' => \WP_REST_Server::READABLE,
49 'args' => array('taxType' => [], 'taxSlug' => [], 'taxValue' => [], 'queryNumber' => [], 'wpnonce' => [], 'archiveBuilder' => []),
50 'callback' => array($this,'ultp_route_taxonomy_info_data'),
51 'permission_callback' => '__return_true'
52 )
53 );
54 register_rest_route(
55 'ultp/v1',
56 '/search/',
57 array(
58 array(
59 'methods' => 'POST',
60 'callback' => array($this, 'search_settings_action'),
61 'permission_callback' => function () {
62 return current_user_can('edit_posts');
63 },
64 'args' => array()
65 )
66 )
67 );
68 register_rest_route(
69 'ultp/v2',
70 '/template_page_insert/',
71 array(
72 array(
73 'methods' => 'POST',
74 'callback' => array($this, 'template_page_insert'),
75 'permission_callback' => function () {
76 return current_user_can('publish_posts');
77 },
78 'args' => array()
79 )
80 )
81 );
82 register_rest_route(
83 'ultp/v2',
84 '/addon_block_action/',
85 array(
86 array(
87 'methods' => 'POST',
88 'callback' => array($this, 'addon_block_action'),
89 'permission_callback' => function () {
90 return current_user_can('manage_options');
91 },
92 'args' => array()
93 )
94 )
95 );
96 register_rest_route(
97 'ultp/v2',
98 '/premade_wishlist_save/',
99 array(
100 array(
101 'methods' => 'POST',
102 'callback' => array($this, 'premade_wishlist_save'),
103 'permission_callback' => function () {
104 return current_user_can('edit_posts');
105 },
106 'args' => array()
107 )
108 )
109 );
110 register_rest_route(
111 'ultp/v2',
112 '/save_plugin_settings/',
113 array(
114 array(
115 'methods' => 'POST',
116 'callback' => array($this, 'save_plugin_settings'),
117 'permission_callback' => function () {
118 return current_user_can('manage_options');
119 },
120 'args' => array()
121 )
122 )
123 );
124 register_rest_route(
125 'ultp',
126 '/ultp_search_data/',
127 array(
128 array(
129 'methods' => 'POST',
130 'callback' => array($this, 'ultp_search_result'),
131 'permission_callback' => '__return_true'
132 )
133 )
134 );
135 register_rest_route(
136 'ultp/v2',
137 '/get_all_settings/',
138 array(
139 array(
140 'methods' => 'POST',
141 'callback' => array($this, 'get_all_settings'),
142 'permission_callback' => function () {
143 return current_user_can('edit_posts');
144 },
145 'args' => array()
146 )
147 )
148 );
149 register_rest_route(
150 'ultp/v2',
151 '/dashborad/',
152 array(
153 array(
154 'methods' => 'POST',
155 'callback' => array( $this, 'get_dashboard_callback'),
156 'permission_callback' => function () {
157 return current_user_can( 'manage_options' );
158 },
159 'args' => array()
160 )
161 )
162 );
163 register_rest_route(
164 'ultp/v2',
165 '/wizard_site_status/',
166 array(
167 array(
168 'methods' => 'POST',
169 'callback' => array( $this, 'wizard_site_status_callback'),
170 'permission_callback' => function () {
171 return current_user_can( 'manage_options' );
172 },
173 'args' => array()
174 )
175 )
176 );
177 register_rest_route(
178 'ultp/v2',
179 '/send_initial_plugin_data/',
180 array(
181 array(
182 'methods' => 'POST',
183 'callback' => array( $this, 'send_initial_plugin_data_callback'),
184 'permission_callback' => function () {
185 return current_user_can( 'edit_posts' );
186 },
187 'args' => array()
188 )
189 )
190 );
191 register_rest_route(
192 'ultp/v2',
193 '/initial_setup_complete/',
194 array(
195 array(
196 'methods' => 'POST',
197 'callback' => array( $this, 'initial_setup_complete_callback'),
198 'permission_callback' => function () {
199 return current_user_can( 'edit_posts' );
200 },
201 'args' => array()
202 )
203 )
204 );
205 }
206
207 /**
208 * Initial Plugin Setup Complete
209 *
210 * * @since v.2.8.1
211 * @return STRING
212 */
213 public static function initial_setup_complete_callback($server) {
214 $post = $server->get_params();
215 if ( ! (isset($post['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($post['wpnonce'])), 'ultp-nonce' )) ) {
216 die();
217 }
218 ultimate_post()->set_setting('init_setup', 'yes');
219 return rest_ensure_response([
220 'success' => true,
221 'redirect' => admin_url('admin.php?page=ultp-settings'),
222 ]);
223 }
224
225 /**
226 * Send Plugin Data When Initial Setup
227 *
228 * * @since v.2.8.1
229 * @return STRING
230 */
231 public function send_initial_plugin_data_callback($server) {
232 $post = $server->get_params();
233 if ( ! (isset($post['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($post['wpnonce'])), 'ultp-nonce' )) ) {
234 die();
235 }
236
237
238 $site = isset($post['site']) ? sanitize_text_field( $post['site'] ) : '';
239
240 require_once ULTP_PATH.'classes/Deactive.php';
241 $obj = new \ULTP\Deactive();
242 $obj->send_plugin_data('postx_wizard', $site);
243 }
244
245 /**
246 * wizard_site_status_callback
247 *
248 * * @since v.3.0.0
249 * @return STRING
250 */
251 public static function wizard_site_status_callback() {
252 if ( ! (isset($_POST['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($_POST['wpnonce'])), 'ultp-nonce' )) ) {
253 die();
254 }
255 if ( isset( $_FILES['siteIcon'],$_FILES['siteIcon']['name'] ) ) {
256 $file_extension = strtolower( pathinfo( $_FILES['siteIcon']['name'], PATHINFO_EXTENSION ) ); //phpcs:ignore
257 $allowed_extenstion = array( 'jpg', 'jpeg', 'png', 'gif', 'webp', 'ico' );
258 if ( in_array( $file_extension, $allowed_extenstion ) ) {
259 require_once ABSPATH . 'wp-admin/includes/image.php';
260 require_once ABSPATH . 'wp-admin/includes/file.php';
261 require_once ABSPATH . 'wp-admin/includes/media.php';
262 $file_id = media_handle_upload( 'siteIcon', 0 );
263 if ( $file_id ) {
264 update_option( 'site_icon', $file_id );
265 }
266 }
267 }
268 if ( isset( $_POST['siteName'] ) ) {
269 $site_name = sanitize_text_field( $_POST['siteName'] );
270 update_option( 'blogname', $site_name );
271 }
272 if ( isset( $_POST['siteType'] ) ) {
273 $site_type = sanitize_text_field( $_POST['siteType'] );
274 update_option( '__ultp_site_type', $site_type );
275 }
276 return rest_ensure_response( ['success' => true ]);
277 }
278
279 /**
280 * Save Settings of Option Panel
281 *
282 * @since v.3.0.0
283 * @return NULL
284 */
285 public function get_all_settings($server) {
286 return rest_ensure_response(['success' => true, 'settings' => ultimate_post()->get_setting()]);
287 }
288 /**
289 * Save Settings of Option Panel
290 *
291 * @since v.3.0.0
292 * @return NULL
293 */
294 public function save_plugin_settings($server) {
295 $post = $server->get_params();
296 $data = ultimate_post()->recursive_sanitize_text_field($post['settings']);
297 if (count($data) > 0) {
298 foreach ($data as $key => $val) {
299 ultimate_post()->set_setting($key, $val);
300 }
301 do_action('ultimate_post_after_setting_save');
302 }
303 return rest_ensure_response([
304 'success' => true,
305 'message' => __('You have successfully saved the settings data.', 'ultimate-post') ,
306 'wishListArr' => json_encode($data)]);
307 }
308
309 /**
310 * Save and get premade_wishlist_save
311 *
312 * @since v.3.0.0
313 * @param STRING
314 * @return ARRAY | Inserted Post Url
315 */
316 public function premade_wishlist_save($server) {
317 $post = $server->get_params();
318 $id = sanitize_text_field($post['id']);
319 $action = sanitize_text_field($post['action']);
320 $wishListArr = get_option('ultp_premade_wishlist', []);
321
322 if ($id && sanitize_text_field($post['type']) != 'fetchData') {
323 if($action == 'remove') {
324 $index = array_search($id, $wishListArr);
325 if ($index !== false) {
326 unset($wishListArr[$index]);
327 }
328 } else {
329 if (!in_array($id, $wishListArr)) {
330 array_push($wishListArr, $id );
331 }
332 }
333 update_option('ultp_premade_wishlist', $wishListArr);
334 }
335 return rest_ensure_response([
336 'success' => true,
337 'message' => $action == 'remove' ? __('Item has been removed from wishlist.', 'ultimate-post') : __('Item added to wishlist.', 'ultimate-post'),
338 'wishListArr' => json_encode($wishListArr)]
339 );
340 }
341 /**
342 * Save addon / blocks on/off data
343 *
344 * @since v.3.0.0
345 * @param STRING
346 * @return ARRAY | Inserted Post Url
347 */
348 public function addon_block_action($server) {
349 $post = $server->get_params();
350 $addon_name = sanitize_text_field($post['key']);
351 $addon_value = sanitize_text_field($post['value']);
352 if ($addon_name) {
353 $addon_data = ultimate_post()->get_setting();
354 $addon_data[$addon_name] = $addon_value;
355 $GLOBALS['ultp_settings'][$addon_name] = $addon_value;
356 update_option('ultp_options', $addon_data);
357 }
358 return array(
359 'success' => true,
360 'message' => ($addon_value == 'true' || $addon_value == 'false') ? ($addon_value == 'true' ? __('The addon has been enabled.', 'ultimate-post') : __('The addon has been disabled.', 'ultimate-post') ) : ($addon_value == 'yes' ? __('The block has been enabled.', 'ultimate-post') : __('The block has been disabled.', 'ultimate-post'))
361 );
362 }
363 /**
364 * Saved Template & Custom Font Actions
365 *
366 * @since v.3.0.0
367 * @param STRING
368 * @return ARRAY | Inserted Post Url
369 */
370 public function get_dashboard_callback($server) {
371 $post = $server->get_params();
372
373 if ( isset($post['type']) ) {
374
375 switch ($post['type']) {
376
377 case 'saved_templates':
378 $post_per_page = 10;
379 $data = [];
380 $args = array(
381 'post_type' => $post['pType'],
382 'post_status' => array('publish', 'draft'),
383 'posts_per_page' => $post_per_page,
384 'paged' => $post['pages']
385 );
386
387 if (isset($post['search'])) {
388 $args['paged'] = 1;
389 $args['s'] = $post['search'];
390 }
391
392 $the_query = new \WP_Query( $args );
393 if ( $the_query->have_posts() ) {
394 while ( $the_query->have_posts() ) {
395 $the_query->the_post();
396 $final = [
397 'id' => get_the_ID(),
398 'title' => get_the_title(),
399 'date' => get_the_modified_date('Y/m/d h:i a'),
400 'status' => get_post_status(),
401 'edit' => get_edit_post_link()
402 ];
403
404 if ($post['pType'] == 'ultp_custom_font') {
405 $final = array_merge($final ,['woff' => false,'woff2' => false,'ttf' => false,'svg' => false,'eot' => false]);
406 $settings = get_post_meta( get_the_ID(), '__font_settings', true );
407 foreach ($settings as $key => $value) {
408 if ($value['ttf']) { $final['ttf'] = true; }
409 if ($value['svg']) { $final['svg'] = true; }
410 if ($value['eot']) { $final['eot'] = true; }
411 if ($value['woff']) { $final['woff'] = true; }
412 if ($value['woff2']) { $final['woff2'] = true; }
413 }
414 $final['font_settings'] = $settings;
415 }
416 $data[] = $final;
417 }
418 }
419 wp_reset_postdata();
420 return array(
421 'success' => true,
422 'data' => $data,
423 'new' => ($post['pType'] == 'ultp_custom_font' ? admin_url('post-new.php?post_type=ultp_custom_font') : admin_url('post-new.php?post_type=ultp_templates')),
424 'found' => $the_query->found_posts,
425 'pages' => $the_query->max_num_pages
426 );
427 break;
428
429 case 'action_draft':
430 case 'action_publish':
431 if (isset($post['ids']) && is_array($post['ids'])) {
432 foreach ($post['ids'] as $id) {
433 wp_update_post(array(
434 'ID' => $id,
435 'post_status' => str_replace('action_', '',$post['type'])
436 ));
437 }
438 return array(
439 'success' => true,
440 'message' => __('Status changed for selected items.', 'ultimate-post')
441 );
442 }
443 break;
444
445 case 'license_action':
446 $message = '';
447
448 if ( isset($post['edd_ultp_license_key']) && function_exists('ultimate_post_pro') ) {
449 $is_success = false;
450 $license = trim( sanitize_text_field( $post['edd_ultp_license_key'] ) );
451
452 if ($license && $license != '******************') {
453 update_option( 'edd_ultp_license_key', $license);
454 $api_params = array(
455 'edd_action' => 'activate_license',
456 'license' => $license,
457 'item_id' => 181,
458 'url' => home_url()
459 );
460
461 $response = wp_remote_post(
462 'https://www.wpxpo.com',
463 array(
464 'timeout' => 15,
465 'sslverify' => false,
466 'body' => $api_params
467 )
468 );
469
470 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
471 $message = ( is_wp_error( $response ) && ! empty( $response->get_error_message() ) ) ? $response->get_error_message() : __('An error occurred, please try again.', 'ultimate-post-pro');
472 } else {
473 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
474 if ( false === $license_data->success ) {
475 update_option( 'edd_ultp_license_key', '');
476 switch( $license_data->error ) {
477 case 'expired' :
478 $message = sprintf(
479 __('Your license key expired on %s.', 'ultimate-post'),
480 date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) )
481 );
482 break;
483 case 'revoked' :
484 $message = __('Your license key has been disabled.', 'ultimate-post');
485 break;
486 case 'missing' :
487 $message = __('Invalid license.', 'ultimate-post');
488 break;
489 case 'invalid' :
490 case 'site_inactive':
491 $message = __( 'Your license is not active for this URL.', 'ultimate-post' );
492 break;
493 case 'item_name_mismatch':
494 $message = __( 'This appears to be an invalid license key.', 'ultimate-post' );
495 break;
496 case 'no_activations_left':
497 $message = __( 'Your license key has reached its activation limit.', 'ultimate-post' );
498 break;
499 default :
500 $message = __( 'An error occurred, please try again.', 'ultimate-post' );
501 break;
502 }
503 } else {
504 $message = __('Your license key has been updated.', 'ultimate-post');
505 $is_success = true;
506 }
507 update_option( 'edd_ultp_license_status', $license_data->license );
508 update_option( 'edd_ultp_license_expire', $license_data->expires );
509 update_option( 'edd_ultp_activations_left', $license_data->activations_left );
510
511 }
512 } else {
513 $message = __( 'Invalid license.', 'ultimate-post' );
514 }
515 } else {
516 $message = __( 'Invalid license.', 'ultimate-post' );
517 if (!function_exists('ultimate_post_pro')) {
518 $message = __( 'Install & Acivate PostX Pro plugin.', 'ultimate-post' );
519 }
520 }
521 return array('success' => $is_success, 'message' => $message);
522 break;
523
524 case 'action_delete':
525 if (isset($post['ids']) && is_array($post['ids'])) {
526 foreach ($post['ids'] as $id) {
527 wp_delete_post( $id, true);
528 }
529 }
530 return array(
531 'success' => true,
532 'message' => __('The selected item is deleted.', 'ultimate-post')
533 );
534
535 case 'support_data':
536 $user_info = get_userdata( get_current_user_id() );
537 $name = $user_info->first_name . ($user_info->last_name ? ' ' . $user_info->last_name : '');
538 return array(
539 'success' => true,
540 'data' => array(
541 'name' => $name ? $name : $user_info->user_login,
542 'email' => $user_info->user_email
543 )
544 );
545
546 case 'support_action':
547 $api_params = array(
548 'user_name' => sanitize_text_field($post['name']),
549 'user_email' => sanitize_email($post['email']),
550 'subject' => sanitize_text_field($post['subject']),
551 'desc' => sanitize_textarea_field($post['desc']),
552 );
553 $response = wp_remote_get(
554 'https://wpxpo.com/wp-json/v2/support_mail',
555 array(
556 'method' => 'POST',
557 'timeout' => 120,
558 'body' => $api_params
559 )
560 );
561 $response_data = json_decode($response['body']);
562 $success = ( isset($response_data->success) && $response_data->success ) ? true : false;
563
564 return array(
565 'success' => $success,
566 'message' => $success ? __('New Support Ticket has been Created.', 'ultimate-post') : __('New Support Ticket is not Created Due to Some Issues.', 'ultimate-post')
567 );
568 break;
569
570 case 'helloBarAction':
571 set_transient( 'ultp_helloBar', 'hide', 1296000);
572 return array(
573 'success' => true,
574 'message' => __('Notice is removed.', 'ultimate-post')
575 );
576 break;
577
578 default:
579 # code...
580 break;
581 }
582 }
583 }
584
585 /**
586 * Insert Post For Imported Template
587 *
588 * @since v.2.6.7
589 * @param STRING
590 * @return ARRAY | Inserted Post Url
591 */
592 public function template_page_insert($server) {
593 $post = $server->get_params();
594 $new_page = array(
595 'post_title' => $post['title'],
596 'post_type' => 'page',
597 'post_content' => $post['blockCode'],
598 'post_status' => 'draft',
599 );
600 $post_id = wp_insert_post( $new_page );
601 return array( 'success' => true, 'link' => get_edit_post_link($post_id));
602 }
603
604
605 public function search_settings_action($server) {
606 global $wpdb;
607 $post = $server->get_params();
608
609 switch ($post['type']) {
610 case 'posts':
611 case 'allpost':
612 case 'postExclude':
613 $post_type = array('post');
614 if ($post['type'] == 'allpost') {
615 $post_type = array_keys(ultimate_post()->get_post_type());
616 } else if ($post['type'] == 'postExclude') {
617 $post_type = array($post['condition']);
618 }
619 $args = array(
620 'post_type' => $post_type,
621 'post_status' => 'publish',
622 'posts_per_page' => 10,
623 );
624 if (is_numeric($post['term'])) {
625 $args['p'] = $post['term'];
626 } else {
627 $args['s'] = $post['term'];
628 }
629
630 $post_results = new \WP_Query($args);
631 $data = [];
632 if (!empty($post_results)) {
633 while ( $post_results->have_posts() ) {
634 $post_results->the_post();
635 $id = get_the_ID();
636 $title = html_entity_decode(get_the_title());
637 $data[] = array('value'=>$id, 'title'=>($title?'[ID: '.$id.'] '.$title:('[ID: '.$id.']')));
638 }
639 wp_reset_postdata();
640 }
641 return ['success' => true, 'data' => $data];
642 break;
643
644 case 'author':
645 $term = '%'. $wpdb->esc_like( $post['term'] ) .'%';
646 $post_results = $wpdb->get_results(
647 $wpdb->prepare(
648 "SELECT ID, display_name
649 FROM $wpdb->users
650 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
651 )
652 );
653 $data = [];
654 if (!empty($post_results)) {
655 foreach ($post_results as $key => $val) {
656 $data[] = array('value'=>$val->ID, 'title'=>'[ID: '.$val->ID.'] '.$val->display_name);
657 }
658 }
659 return ['success' => true, 'data' => $data];
660 break;
661
662 case 'taxvalue':
663 $split = explode('###', $post['condition']);
664 $condition = $split[1] != 'multiTaxonomy' ? array($split[1]) : get_object_taxonomies($split[0]);
665 $args = array(
666 'taxonomy' => $condition,
667 'fields' => 'all',
668 'orderby' => 'id',
669 'order' => 'ASC',
670 'name__like'=> $post['term']
671 );
672 if (is_numeric($post['term'])) {
673 unset($args['name__like']);
674 $args['include'] = array($post['term']);
675 }
676
677 $post_results = get_terms( $args );
678 $data = [];
679 if (!empty($post_results)) {
680 foreach ($post_results as $key => $val) {
681 if ($split[1] == 'multiTaxonomy') {
682 $data[] = array('value'=>$val->taxonomy.'###'.$val->slug, 'title'=> '[ID: '.$val->term_id.'] '.$val->taxonomy.': '.$val->name);
683 } else {
684 $data[] = array('value'=>urldecode($val->slug), 'title'=>'[ID: '.$val->term_id.'] '.$val->name);
685 }
686 }
687 }
688 return ['success' => true, 'data' => $data];
689 break;
690
691 case 'taxExclude':
692 $condition = get_object_taxonomies($post['condition']);
693 $args = array(
694 'taxonomy' => $condition,
695 'fields' => 'all',
696 'orderby' => 'id',
697 'order' => 'ASC',
698 'name__like'=> $post['term']
699 );
700 if (is_numeric($post['term'])) {
701 unset($args['name__like']);
702 $args['include'] = array($post['term']);
703 }
704 $post_results = get_terms( $args );
705 $data = [];
706 if (!empty($post_results)) {
707 foreach ($post_results as $key => $val) {
708 $data[] = array('value'=>$val->taxonomy.'###'.$val->slug, 'title'=> '[ID: '.$val->term_id.'] '.$val->taxonomy.': '.$val->name);
709 }
710 }
711 return ['success' => true, 'data' => $data];
712 break;
713 // allPostType
714 case 'allPostType':
715 $all_types = array_values(get_post_types( array( 'public' => true ), 'names' ));
716 $postType = array();
717 foreach($all_types as $type){
718 $postType[] = array(
719 'title' => $type,
720 'value' => $type,
721 );
722 };
723
724 return ['success' => true, 'data' => $postType ];
725 default:
726 return ['success' => true, 'data' => [['value'=>'', 'title'=>'- Select -']]];
727 break;
728 }
729 }
730
731 /**
732 * Post Data Response of REST API
733 *
734 * @since v.1.0.0
735 * @param MIXED | Pram (ARRAY), Local (BOOLEAN)
736 * @return ARRAY | Response Image Size as Array
737 */
738 public function ultp_route_post_data($prams) {
739 if ( ! (isset($_REQUEST['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($_REQUEST['wpnonce'])), 'ultp-nonce' )) ) {
740 die();
741 }
742 $prams = $prams->get_params();
743 $data = [];
744 $loop = new \WP_Query( ultimate_post()->get_query($prams) );
745 $max_tax = isset($prams['maxTaxonomy']) && $prams['maxTaxonomy'] ? ( $prams['maxTaxonomy'] == '0' ? 0 : $prams['maxTaxonomy'] ) : 30 ;
746
747
748 if ($loop->have_posts()) {
749 while($loop->have_posts()) {
750 $loop->the_post();
751 $var = array();
752 $post_id = get_the_ID();
753 $user_id = get_the_author_meta('ID');
754 $content_data = get_the_content();
755 $var['ID'] = $post_id;
756 $var['title'] = get_the_title();
757 $var['permalink'] = get_permalink();
758 $var['seo_meta'] = ultimate_post()->get_excerpt($post_id, 1);
759 $var['excerpt'] = strip_tags($content_data);
760 $var['excerpt_full']= strip_tags(get_the_excerpt());
761 $var['time'] = (int)get_the_date('U')*1000;
762 $var['timeModified']= (int)get_the_modified_date('U')*1000;
763 $var['post_time'] = human_time_diff(get_the_time('U'),current_time('U'));
764 $var['view'] = get_post_meta(get_the_ID(),'__post_views_count', true);
765 $var['comments'] = get_comments_number();
766 $var['author_link'] = get_author_posts_url($user_id);
767 $var['avatar_url'] = get_avatar_url($user_id);
768 $var['display_name']= get_the_author_meta('display_name');
769 $var['reading_time']= ceil(strlen($content_data)/1200);
770 $post_video = get_post_meta($post_id, '__builder_feature_video', true);
771 // Video
772 if ($post_video) {
773 $var['has_video'] = ultimate_post()->get_youtube_id($post_video);
774 }
775 // image
776 $image_sizes = ultimate_post()->get_image_size();
777 $image_src = array();
778 if (has_post_thumbnail()) {
779 $thumb_id = get_post_thumbnail_id($post_id);
780 foreach ($image_sizes as $key => $value) {
781 $image_src[$key] = wp_get_attachment_image_src($thumb_id, $key, false)[0];
782 }
783 $var['image'] = $image_src;
784 } elseif(isset($prams['fallbackImg']['id'])) {
785 foreach ($image_sizes as $key => $value) {
786 $image_src[$key] = wp_get_attachment_image_src($prams['fallbackImg']['id'], $key, false)[0];
787 }
788 $var['image'] = $image_src;
789 $var['is_fallback'] = true;
790 }
791
792 // tag
793 $tag = get_the_terms($post_id, (isset($prams['tag'])?esc_attr($prams['tag']):'post_tag'));
794 if (!empty($tag)) {
795 $v = array();
796 foreach ($tag as $k => $val) {
797 if ($k >= $max_tax) { break; }
798 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id));
799 }
800 $var['tag'] = $v;
801 }
802
803 // Taxonomy
804 $cat = get_the_terms($post_id, (isset($prams['taxonomy'])?esc_attr($prams['taxonomy']):'category'));
805
806 if(!empty($cat)){
807 $v = array();
808 foreach ($cat as $k => $val) {
809 if ($k >= $max_tax) { break; }
810 $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));
811 }
812 $var['category'] = $v;
813 }
814 $data[] = $var;
815 }
816 wp_reset_postdata();
817 }
818 return rest_ensure_response( $data);
819 }
820
821
822 /**
823 * Taxonomy Data Response of REST API
824 *
825 * @since v.1.0.0
826 * @param ARRAY | Parameter (ARRAY)
827 * @return ARRAY | Response Taxonomy List as Array
828 */
829 public function ultp_route_common_data($prams) {
830 if ( ! (isset($_REQUEST['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($_REQUEST['wpnonce'])), 'ultp-nonce' )) ) {
831 return rest_ensure_response([]);
832 }
833
834 $all_post_type = ultimate_post()->get_post_type();
835 $data = array();
836 foreach ($all_post_type as $post_type_slug => $post_type ) {
837 $data_term = array();
838 $taxonomies = get_object_taxonomies($post_type_slug);
839 foreach ($taxonomies as $key => $taxonomy_slug) {
840 $taxonomy_value = get_terms(array(
841 'taxonomy' => $taxonomy_slug,
842 'hide_empty' => false
843 ));
844 if (!is_wp_error($taxonomy_value)) {
845 $data_tax = array();
846 foreach ($taxonomy_value as $k => $taxonomy) {
847 $data_tax[urldecode_deep($taxonomy->slug)] = $taxonomy->name;
848 }
849 if (count($data_tax) > 0) {
850 $data_term[$taxonomy_slug] = $data_tax;
851 }
852 }
853 }
854 $data[$post_type_slug] = $data_term;
855 }
856 // Global Customizer
857 $global = get_option('postx_global', []);
858 // Image Size
859 $image_sizes = ultimate_post()->get_image_size();
860
861 return rest_ensure_response(['taxonomy' => $data, 'global' => $global, 'image' => json_encode($image_sizes), 'posttype' => json_encode($all_post_type)]);
862 }
863
864 /**
865 * Specific Taxonomy Data Response of REST API
866 *
867 * @since v.1.0.0
868 * @param ARRAY | Parameter (ARRAY)
869 * @return ARRAY | Response Taxonomy List as Array
870 */
871 public function ultp_route_taxonomy_info_data($prams) {
872 if ( ! (isset($_REQUEST['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($_REQUEST['wpnonce'])), 'ultp-nonce' )) ) {
873 return rest_ensure_response([]);
874 }
875
876 return rest_ensure_response( ultimate_post()->get_category_data(json_decode($prams['taxValue']), $prams['queryNumber'], $prams['taxType'], $prams['taxSlug'], $prams['archiveBuilder']) );
877 }
878
879 /**
880 * Search Block Data Showing
881 *
882 * @since v.2.9.9
883 * @param STRING
884 * @return ARRAY | Inserted Post Url
885 */
886 public function ultp_search_result($server) {
887 $post = $server->get_params();
888
889 $query_args = array(
890 's' => sanitize_text_field($post['searchText']),
891 'paged' => sanitize_text_field($post['paged']),
892 'compare' => 'LIKE',
893 'orderby' => 'relevance',
894 'posts_per_page' => sanitize_text_field($post['postPerPage']),
895 );
896 if(isset($post['exclude']) && is_array($post['exclude']) && count($post['exclude']) > 0) {
897 $post_exclude = array();
898 foreach( $post['exclude'] as $data ){
899 $post_exclude[$data['title']] = $data['title'];
900 }
901 $all_types = get_post_types( array( 'public' => true ), 'names' );
902 $post_type = array_diff_key($all_types, $post_exclude);
903 $query_args['post_type'] = $post_type;
904 }
905 $output = '';
906 $query_result = new \WP_Query($query_args);
907
908 if ($query_result->have_posts()) {
909 while ($query_result->have_posts()) {
910 $query_result->the_post();
911 $post_id = get_the_ID();
912 $title = get_the_title();
913
914 $output .= '<div class="ultp-search-result__item">';
915 if ($post['image'] == 1 && has_post_thumbnail()) {
916 $thumb_id = get_post_thumbnail_id($post_id);
917 $output .= '<img class="ultp-searchresult-image" src='.wp_get_attachment_image_src($thumb_id, 'thumbnail', false)[0].' alt="'.$title.'"/>';
918 }
919 $output .= '<div class="ultp-searchresult-content">';
920 $output .= '<div class="ultp-rescontent-meta">';
921 // Category
922 $post_cat = get_the_terms($post_id, 'category');
923 if ($post['category'] == 1 && $post_cat && count($post_cat)) {
924 $output .= '<div class="ultp-searchresult-category">';
925 foreach($post_cat as $cat){
926 $output .= '<a href="'.get_term_link($cat->term_id).'">'.$cat->name.'</a>';
927 }
928 $output .= '</div>';
929 }
930 // Author
931 if ($post['author'] == 1) {
932 $user_id = get_the_author_meta('ID');
933 $output .= '<a href="'.get_author_posts_url($user_id).'" class="ultp-searchresult-author">'.get_the_author_meta('display_name').'</a>';
934 }
935 // Date
936 if ($post['date'] == 1) {
937 $output .= '<div class="ultp-searchresult-publishdate">'.get_the_date('F j, Y').'</div>';
938 }
939 $output .= '</div>';
940 $output .= '<a href="'.get_permalink().'" class="ultp-searchresult-title">'.$title.'</a>';
941 if ($post['excerpt'] == 1) {
942 $output .= '<div class="ultp-searchresult-excerpt">'.wp_trim_words(get_the_excerpt(), $post['excerptLimit']).'</div>';
943 }
944 $output .= '</div>';
945 $output .= '</div>';
946 }
947 }
948
949 return array('post_data' => $output, 'post_count' => $query_result->found_posts);
950 }
951 }