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

1,024 lines 42.0 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 if (isset($post['type'])) {
373
374 switch ($post['type']) {
375
376 case 'saved_templates':
377 $post_per_page = 10;
378 $data = [];
379 $args = array(
380 'post_type' => $post['pType'],
381 'post_status' => array('publish', 'draft'),
382 'posts_per_page' => $post_per_page,
383 'paged' => $post['pages']
384 );
385
386 if (isset($post['search'])) {
387 $args['paged'] = 1;
388 $args['s'] = $post['search'];
389 }
390
391 $the_query = new \WP_Query( $args );
392 if ( $the_query->have_posts() ) {
393 while ( $the_query->have_posts() ) {
394 $the_query->the_post();
395 $final = [
396 'id' => get_the_ID(),
397 'title' => get_the_title(),
398 'date' => get_the_modified_date('Y/m/d h:i a'),
399 'status' => get_post_status(),
400 'edit' => get_edit_post_link()
401 ];
402
403 if ($post['pType'] == 'ultp_custom_font') {
404 $final = array_merge($final ,['woff' => false,'woff2' => false,'ttf' => false,'svg' => false,'eot' => false]);
405 $settings = get_post_meta( get_the_ID(), '__font_settings', true );
406 foreach ($settings as $key => $value) {
407 if ($value['ttf']) { $final['ttf'] = true; }
408 if ($value['svg']) { $final['svg'] = true; }
409 if ($value['eot']) { $final['eot'] = true; }
410 if ($value['woff']) { $final['woff'] = true; }
411 if ($value['woff2']) { $final['woff2'] = true; }
412 }
413 $final['font_settings'] = $settings;
414 }
415 $data[] = $final;
416 }
417 }
418 wp_reset_postdata();
419 return array(
420 'success' => true,
421 'data' => $data,
422 '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')),
423 'found' => $the_query->found_posts,
424 'pages' => $the_query->max_num_pages
425 );
426 break;
427
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 break;
535
536 // case 'header':
537 // $menu_list = array(
538 // 'menu' => array(
539 // array(
540 // 'link' => '#home',
541 // 'label' => __('Getting Started', 'ultimate-post'),
542 // 'showin' => 'both',
543 // ),
544 // array(
545 // 'link' => '#builder',
546 // 'label' => __('Site Builder', 'ultimate-post'),
547 // 'showin' => ultimate_post()->get_setting('ultp_builder') != 'false' ? 'both' : 'none',
548 // 'showhide' => true
549 // ),
550 // array(
551 // 'link' => '#templatekit',
552 // 'label' => __('Template kits', 'ultimate-post'),
553 // 'showin' => 'both',
554 // ),
555 // array(
556 // 'link' => '#saved-templates',
557 // 'label' => __('Saved Templates', 'ultimate-post'),
558 // 'showin' => ultimate_post()->get_setting('ultp_templates') != 'false' ? 'both' : 'none',
559 // 'showhide' => true
560 // ),
561 // array(
562 // 'link' => '#custom-font',
563 // 'label' => __('Custom Font', 'ultimate-post'),
564 // 'showin' => ultimate_post()->get_setting('ultp_custom_font') != 'false' ? 'both' : 'none',
565 // 'showhide' => true
566 // ),
567 // array(
568 // 'link' => '#addons',
569 // 'label' => __('Addons', 'ultimate-post'),
570 // 'showin' => 'both',
571 // ),
572 // array(
573 // 'link' => '#blocks',
574 // 'label' => __('Blocks', 'ultimate-post'),
575 // 'showin' => 'both',
576 // ),
577 // array(
578 // 'link' => '#settings',
579 // 'label' => __('Settings', 'ultimate-post'),
580 // 'showin' => 'both',
581 // ),
582 // array(
583 // 'link' => '#tutorials',
584 // 'label' => __('Tutorials', 'ultimate-post'),
585 // 'showin' => 'sidebar',
586 // ),
587 // array(
588 // 'link' => '#license',
589 // 'label' => __('License', 'ultimate-post'),
590 // 'showin' => 'sidebar',
591 // )
592 // ),
593 // 'common' => array(
594 // 'version' => ULTP_VER
595 // ),
596 // 'help_menu' => array(
597 // array(
598 // 'label' => __('Get Support', 'ultimate-post'),
599 // 'icon' => 'dashicons-phone',
600 // 'link' => ultimate_post()->get_premium_link('https://www.wpxpo.com/contact/', 'plugin_dir_pro')
601 // ),
602 // array(
603 // 'label' => __('Welcome Guide', 'ultimate-post'),
604 // 'icon' => 'dashicons-megaphone',
605 // 'link' => admin_url('admin.php?page=ultp-initial-setup-wizard')
606 // ),
607 // array(
608 // 'label' => __('Join Community', 'ultimate-post'),
609 // 'icon' => 'dashicons-facebook-alt',
610 // 'link' => 'https://www.facebook.com/groups/gutenbergpostx'
611 // ),
612 // array(
613 // 'label' => __('Feature Request', 'ultimate-post'),
614 // 'icon' => 'dashicons-email-alt',
615 // 'link' => ultimate_post()->get_premium_link('https://www.wpxpo.com/postx/roadmap/', 'plugin_dir_pro')
616 // ),
617 // array(
618 // 'label' => __('Youtube Tutorials', 'ultimate-post'),
619 // 'icon' => 'dashicons-youtube',
620 // 'link' => 'https://www.youtube.com/@wpxpo'
621 // ),
622 // array(
623 // 'label' => __('Documentation', 'ultimate-post'),
624 // 'icon' => 'dashicons-book',
625 // 'link' => 'https://docs.wpxpo.com/docs/postx/'
626 // ),
627 // array(
628 // 'label' => __('What’s New', 'ultimate-post'),
629 // 'icon' => 'dashicons-edit',
630 // 'link' => ultimate_post()->get_premium_link('https://www.wpxpo.com/category/postx/', 'plugin_dir_pro')
631 // ),
632 // )
633 // );
634
635 // return array(
636 // 'success' => true,
637 // 'data' => apply_filters( 'postx_menu_system', $menu_list ),
638 // 'helloBar' => get_transient('ultp_helloBar'),
639 // 'status' => get_option( 'edd_ultp_license_status' ),
640 // 'expire' => get_option( 'edd_ultp_license_expire' )
641 // );
642 // break;
643 case 'helloBarAction':
644 set_transient( 'ultp_helloBar', 'hide', 1296000);
645 return array(
646 'success' => true,
647 'message' => __('Notice is removed.', 'ultimate-post')
648 );
649 break;
650
651 default:
652 # code...
653 break;
654 }
655 }
656 }
657
658 /**
659 * Insert Post For Imported Template
660 *
661 * @since v.2.6.7
662 * @param STRING
663 * @return ARRAY | Inserted Post Url
664 */
665 public function template_page_insert($server) {
666 $post = $server->get_params();
667 $new_page = array(
668 'post_title' => $post['title'],
669 'post_type' => 'page',
670 'post_content' => $post['blockCode'],
671 'post_status' => 'draft',
672 );
673 $post_id = wp_insert_post( $new_page );
674 return array( 'success' => true, 'link' => get_edit_post_link($post_id));
675 }
676
677
678 public function search_settings_action($server) {
679 global $wpdb;
680 $post = $server->get_params();
681
682 switch ($post['type']) {
683 case 'posts':
684 case 'allpost':
685 case 'postExclude':
686 $post_type = array('post');
687 if ($post['type'] == 'allpost') {
688 $post_type = array_keys(ultimate_post()->get_post_type());
689 } else if ($post['type'] == 'postExclude') {
690 $post_type = array($post['condition']);
691 }
692 $args = array(
693 'post_type' => $post_type,
694 'post_status' => 'publish',
695 'posts_per_page' => 10,
696 );
697 if (is_numeric($post['term'])) {
698 $args['p'] = $post['term'];
699 } else {
700 $args['s'] = $post['term'];
701 }
702
703 $post_results = new \WP_Query($args);
704 $data = [];
705 if (!empty($post_results)) {
706 while ( $post_results->have_posts() ) {
707 $post_results->the_post();
708 $id = get_the_ID();
709 $title = html_entity_decode(get_the_title());
710 $data[] = array('value'=>$id, 'title'=>($title?'[ID: '.$id.'] '.$title:('[ID: '.$id.']')));
711 }
712 wp_reset_postdata();
713 }
714 return ['success' => true, 'data' => $data];
715 break;
716
717 case 'author':
718 $term = '%'. $wpdb->esc_like( $post['term'] ) .'%';
719 $post_results = $wpdb->get_results(
720 $wpdb->prepare(
721 "SELECT ID, display_name
722 FROM $wpdb->users
723 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
724 )
725 );
726 $data = [];
727 if (!empty($post_results)) {
728 foreach ($post_results as $key => $val) {
729 $data[] = array('value'=>$val->ID, 'title'=>'[ID: '.$val->ID.'] '.$val->display_name);
730 }
731 }
732 return ['success' => true, 'data' => $data];
733 break;
734
735 case 'taxvalue':
736 $split = explode('###', $post['condition']);
737 $condition = $split[1] != 'multiTaxonomy' ? array($split[1]) : get_object_taxonomies($split[0]);
738 $args = array(
739 'taxonomy' => $condition,
740 'fields' => 'all',
741 'orderby' => 'id',
742 'order' => 'ASC',
743 'name__like'=> $post['term']
744 );
745 if (is_numeric($post['term'])) {
746 unset($args['name__like']);
747 $args['include'] = array($post['term']);
748 }
749
750 $post_results = get_terms( $args );
751 $data = [];
752 if (!empty($post_results)) {
753 foreach ($post_results as $key => $val) {
754 if ($split[1] == 'multiTaxonomy') {
755 $data[] = array('value'=>$val->taxonomy.'###'.$val->slug, 'title'=> '[ID: '.$val->term_id.'] '.$val->taxonomy.': '.$val->name);
756 } else {
757 $data[] = array('value'=>urldecode($val->slug), 'title'=>'[ID: '.$val->term_id.'] '.$val->name);
758 }
759 }
760 }
761 return ['success' => true, 'data' => $data];
762 break;
763
764 case 'taxExclude':
765 $condition = get_object_taxonomies($post['condition']);
766 $args = array(
767 'taxonomy' => $condition,
768 'fields' => 'all',
769 'orderby' => 'id',
770 'order' => 'ASC',
771 'name__like'=> $post['term']
772 );
773 if (is_numeric($post['term'])) {
774 unset($args['name__like']);
775 $args['include'] = array($post['term']);
776 }
777 $post_results = get_terms( $args );
778 $data = [];
779 if (!empty($post_results)) {
780 foreach ($post_results as $key => $val) {
781 $data[] = array('value'=>$val->taxonomy.'###'.$val->slug, 'title'=> '[ID: '.$val->term_id.'] '.$val->taxonomy.': '.$val->name);
782 }
783 }
784 return ['success' => true, 'data' => $data];
785 break;
786 // allPostType
787 case 'allPostType':
788 $all_types = array_values(get_post_types( array( 'public' => true ), 'names' ));
789 $postType = array();
790 foreach($all_types as $type){
791 $postType[] = array(
792 'title' => $type,
793 'value' => $type,
794 );
795 };
796
797 return ['success' => true, 'data' => $postType ];
798 default:
799 return ['success' => true, 'data' => [['value'=>'', 'title'=>'- Select -']]];
800 break;
801 }
802 }
803
804 /**
805 * Post Data Response of REST API
806 *
807 * @since v.1.0.0
808 * @param MIXED | Pram (ARRAY), Local (BOOLEAN)
809 * @return ARRAY | Response Image Size as Array
810 */
811 public function ultp_route_post_data($prams) {
812 if ( ! (isset($_REQUEST['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($_REQUEST['wpnonce'])), 'ultp-nonce' )) ) {
813 die();
814 }
815 $prams = $prams->get_params();
816 $data = [];
817 $loop = new \WP_Query( ultimate_post()->get_query($prams) );
818 $max_tax = isset($prams['maxTaxonomy']) && $prams['maxTaxonomy'] ? ( $prams['maxTaxonomy'] == '0' ? 0 : $prams['maxTaxonomy'] ) : 30 ;
819
820
821 if ($loop->have_posts()) {
822 while($loop->have_posts()) {
823 $loop->the_post();
824 $var = array();
825 $post_id = get_the_ID();
826 $user_id = get_the_author_meta('ID');
827 $content_data = get_the_content();
828 $var['ID'] = $post_id;
829 $var['title'] = get_the_title();
830 $var['permalink'] = get_permalink();
831 $var['seo_meta'] = ultimate_post()->get_excerpt($post_id, 1);
832 $var['excerpt'] = strip_tags($content_data);
833 $var['excerpt_full']= strip_tags(get_the_excerpt());
834 $var['time'] = (int)get_the_date('U')*1000;
835 $var['timeModified']= (int)get_the_modified_date('U')*1000;
836 $var['post_time'] = human_time_diff(get_the_time('U'),current_time('U'));
837 $var['view'] = get_post_meta(get_the_ID(),'__post_views_count', true);
838 $var['comments'] = get_comments_number();
839 $var['author_link'] = get_author_posts_url($user_id);
840 $var['avatar_url'] = get_avatar_url($user_id);
841 $var['display_name']= get_the_author_meta('display_name');
842 $var['reading_time']= ceil(strlen($content_data)/1200);
843 $post_video = get_post_meta($post_id, '__builder_feature_video', true);
844 // Video
845 if ($post_video) {
846 $var['has_video'] = ultimate_post()->get_youtube_id($post_video);
847 }
848 // image
849 $image_sizes = ultimate_post()->get_image_size();
850 $image_src = array();
851 if (has_post_thumbnail()) {
852 $thumb_id = get_post_thumbnail_id($post_id);
853 foreach ($image_sizes as $key => $value) {
854 $image_src[$key] = wp_get_attachment_image_src($thumb_id, $key, false)[0];
855 }
856 $var['image'] = $image_src;
857 } elseif(isset($prams['fallbackImg']['id'])) {
858 foreach ($image_sizes as $key => $value) {
859 $image_src[$key] = wp_get_attachment_image_src($prams['fallbackImg']['id'], $key, false)[0];
860 }
861 $var['image'] = $image_src;
862 $var['is_fallback'] = true;
863 }
864
865 // tag
866 $tag = get_the_terms($post_id, (isset($prams['tag'])?esc_attr($prams['tag']):'post_tag'));
867 if (!empty($tag)) {
868 $v = array();
869 foreach ($tag as $k => $val) {
870 if ($k >= $max_tax) { break; }
871 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id));
872 }
873 $var['tag'] = $v;
874 }
875
876 // Taxonomy
877 $cat = get_the_terms($post_id, (isset($prams['taxonomy'])?esc_attr($prams['taxonomy']):'category'));
878
879 if(!empty($cat)){
880 $v = array();
881 foreach ($cat as $k => $val) {
882 if ($k >= $max_tax) { break; }
883 $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));
884 }
885 $var['category'] = $v;
886 }
887 $data[] = $var;
888 }
889 wp_reset_postdata();
890 }
891 return rest_ensure_response( $data);
892 }
893
894
895 /**
896 * Taxonomy Data Response of REST API
897 *
898 * @since v.1.0.0
899 * @param ARRAY | Parameter (ARRAY)
900 * @return ARRAY | Response Taxonomy List as Array
901 */
902 public function ultp_route_common_data($prams) {
903 if ( ! (isset($_REQUEST['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($_REQUEST['wpnonce'])), 'ultp-nonce' )) ) {
904 return rest_ensure_response([]);
905 }
906
907 $all_post_type = ultimate_post()->get_post_type();
908 $data = array();
909 foreach ($all_post_type as $post_type_slug => $post_type ) {
910 $data_term = array();
911 $taxonomies = get_object_taxonomies($post_type_slug);
912 foreach ($taxonomies as $key => $taxonomy_slug) {
913 $taxonomy_value = get_terms(array(
914 'taxonomy' => $taxonomy_slug,
915 'hide_empty' => false
916 ));
917 if (!is_wp_error($taxonomy_value)) {
918 $data_tax = array();
919 foreach ($taxonomy_value as $k => $taxonomy) {
920 $data_tax[urldecode_deep($taxonomy->slug)] = $taxonomy->name;
921 }
922 if (count($data_tax) > 0) {
923 $data_term[$taxonomy_slug] = $data_tax;
924 }
925 }
926 }
927 $data[$post_type_slug] = $data_term;
928 }
929 // Global Customizer
930 $global = get_option('postx_global', []);
931 // Image Size
932 $image_sizes = ultimate_post()->get_image_size();
933
934 return rest_ensure_response(['taxonomy' => $data, 'global' => $global, 'image' => json_encode($image_sizes), 'posttype' => json_encode($all_post_type)]);
935 }
936
937 /**
938 * Specific Taxonomy Data Response of REST API
939 *
940 * @since v.1.0.0
941 * @param ARRAY | Parameter (ARRAY)
942 * @return ARRAY | Response Taxonomy List as Array
943 */
944 public function ultp_route_taxonomy_info_data($prams) {
945 if ( ! (isset($_REQUEST['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($_REQUEST['wpnonce'])), 'ultp-nonce' )) ) {
946 return rest_ensure_response([]);
947 }
948
949 return rest_ensure_response( ultimate_post()->get_category_data(json_decode($prams['taxValue']), $prams['queryNumber'], $prams['taxType'], $prams['taxSlug'], $prams['archiveBuilder']) );
950 }
951
952 /**
953 * Search Block Data Showing
954 *
955 * @since v.2.9.9
956 * @param STRING
957 * @return ARRAY | Inserted Post Url
958 */
959 public function ultp_search_result($server) {
960 $post = $server->get_params();
961
962 $query_args = array(
963 's' => sanitize_text_field($post['searchText']),
964 'paged' => sanitize_text_field($post['paged']),
965 'compare' => 'LIKE',
966 'orderby' => 'relevance',
967 'posts_per_page' => sanitize_text_field($post['postPerPage']),
968 );
969 if(isset($post['exclude']) && is_array($post['exclude']) && count($post['exclude']) > 0) {
970 $post_exclude = array();
971 foreach( $post['exclude'] as $data ){
972 $post_exclude[$data['title']] = $data['title'];
973 }
974 $all_types = get_post_types( array( 'public' => true ), 'names' );
975 $post_type = array_diff_key($all_types, $post_exclude);
976 $query_args['post_type'] = $post_type;
977 }
978 $output = '';
979 $query_result = new \WP_Query($query_args);
980
981 if ($query_result->have_posts()) {
982 while ($query_result->have_posts()) {
983 $query_result->the_post();
984 $post_id = get_the_ID();
985 $title = get_the_title();
986
987 $output .= '<div class="ultp-search-result__item">';
988 if ($post['image'] == 1 && has_post_thumbnail()) {
989 $thumb_id = get_post_thumbnail_id($post_id);
990 $output .= '<img class="ultp-searchresult-image" src='.wp_get_attachment_image_src($thumb_id, 'thumbnail', false)[0].' alt="'.$title.'"/>';
991 }
992 $output .= '<div class="ultp-searchresult-content">';
993 $output .= '<div class="ultp-rescontent-meta">';
994 // Category
995 $post_cat = get_the_terms($post_id, 'category');
996 if ($post['category'] == 1 && $post_cat && count($post_cat)) {
997 $output .= '<div class="ultp-searchresult-category">';
998 foreach($post_cat as $cat){
999 $output .= '<a href="'.get_term_link($cat->term_id).'">'.$cat->name.'</a>';
1000 }
1001 $output .= '</div>';
1002 }
1003 // Author
1004 if ($post['author'] == 1) {
1005 $user_id = get_the_author_meta('ID');
1006 $output .= '<a href="'.get_author_posts_url($user_id).'" class="ultp-searchresult-author">'.get_the_author_meta('display_name').'</a>';
1007 }
1008 // Date
1009 if ($post['date'] == 1) {
1010 $output .= '<div class="ultp-searchresult-publishdate">'.get_the_date('F j, Y').'</div>';
1011 }
1012 $output .= '</div>';
1013 $output .= '<a href="'.get_permalink().'" class="ultp-searchresult-title">'.$title.'</a>';
1014 if ($post['excerpt'] == 1) {
1015 $output .= '<div class="ultp-searchresult-excerpt">'.wp_trim_words(get_the_excerpt(), $post['excerptLimit']).'</div>';
1016 }
1017 $output .= '</div>';
1018 $output .= '</div>';
1019 }
1020 }
1021
1022 return array('post_data' => $output, 'post_count' => $query_result->found_posts);
1023 }
1024 }