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

1,021 lines 41.8 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 }
302 return rest_ensure_response([
303 'success' => true,
304 'message' => __('You have successfully saved the settings data.', 'ultimate-post') ,
305 'wishListArr' => json_encode($data)]);
306 }
307
308 /**
309 * Save and get premade_wishlist_save
310 *
311 * @since v.3.0.0
312 * @param STRING
313 * @return ARRAY | Inserted Post Url
314 */
315 public function premade_wishlist_save($server) {
316 $post = $server->get_params();
317 $id = sanitize_text_field($post['id']);
318 $action = sanitize_text_field($post['action']);
319 $wishListArr = get_option('ultp_premade_wishlist', []);
320
321 if ($id && sanitize_text_field($post['type']) != 'fetchData') {
322 if($action == 'remove') {
323 $index = array_search($id, $wishListArr);
324 if ($index !== false) {
325 unset($wishListArr[$index]);
326 }
327 } else {
328 if (!in_array($id, $wishListArr)) {
329 array_push($wishListArr, $id );
330 }
331 }
332 update_option('ultp_premade_wishlist', $wishListArr);
333 }
334 return rest_ensure_response([
335 'success' => true,
336 'message' => $action == 'remove' ? __('Item has been removed from wishlist.', 'ultimate-post') : __('Item added to wishlist.', 'ultimate-post'),
337 'wishListArr' => json_encode($wishListArr)]
338 );
339 }
340 /**
341 * Save addon / blocks on/off data
342 *
343 * @since v.3.0.0
344 * @param STRING
345 * @return ARRAY | Inserted Post Url
346 */
347 public function addon_block_action($server) {
348 $post = $server->get_params();
349 $addon_name = sanitize_text_field($post['key']);
350 $addon_value = sanitize_text_field($post['value']);
351 if ($addon_name) {
352 $addon_data = ultimate_post()->get_setting();
353 $addon_data[$addon_name] = $addon_value;
354 $GLOBALS['ultp_settings'][$addon_name] = $addon_value;
355 update_option('ultp_options', $addon_data);
356 }
357 return array(
358 'success' => true,
359 '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'))
360 );
361 }
362 /**
363 * Saved Template & Custom Font Actions
364 *
365 * @since v.3.0.0
366 * @param STRING
367 * @return ARRAY | Inserted Post Url
368 */
369 public function get_dashboard_callback($server) {
370 $post = $server->get_params();
371 if (isset($post['type'])) {
372
373 switch ($post['type']) {
374
375 case 'saved_templates':
376 $post_per_page = 10;
377 $data = [];
378 $args = array(
379 'post_type' => $post['pType'],
380 'post_status' => array('publish', 'draft'),
381 'posts_per_page' => $post_per_page,
382 'paged' => $post['pages']
383 );
384
385 if (isset($post['search'])) {
386 $args['paged'] = 1;
387 $args['s'] = $post['search'];
388 }
389
390 $the_query = new \WP_Query( $args );
391 if ( $the_query->have_posts() ) {
392 while ( $the_query->have_posts() ) {
393 $the_query->the_post();
394 $final = [
395 'id' => get_the_ID(),
396 'title' => get_the_title(),
397 'date' => get_the_modified_date('Y/m/d h:i a'),
398 'status' => get_post_status(),
399 'edit' => get_edit_post_link()
400 ];
401
402 if ($post['pType'] == 'ultp_custom_font') {
403 $final = array_merge($final ,['woff' => false,'woff2' => false,'ttf' => false,'svg' => false,'eot' => false]);
404 $settings = get_post_meta( get_the_ID(), '__font_settings', true );
405 foreach ($settings as $key => $value) {
406 if ($value['ttf']) { $final['ttf'] = true; }
407 if ($value['svg']) { $final['svg'] = true; }
408 if ($value['eot']) { $final['eot'] = true; }
409 if ($value['woff']) { $final['woff'] = true; }
410 if ($value['woff2']) { $final['woff2'] = true; }
411 }
412 $final['font_settings'] = $settings;
413 }
414 $data[] = $final;
415 }
416 }
417 wp_reset_postdata();
418 return array(
419 'success' => true,
420 'data' => $data,
421 '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')),
422 'found' => $the_query->found_posts,
423 'pages' => $the_query->max_num_pages
424 );
425 break;
426
427
428 case 'action_draft':
429 case 'action_publish':
430 if (isset($post['ids']) && is_array($post['ids'])) {
431 foreach ($post['ids'] as $id) {
432 wp_update_post(array(
433 'ID' => $id,
434 'post_status' => str_replace('action_', '',$post['type'])
435 ));
436 }
437 return array(
438 'success' => true,
439 'message' => __('Status changed for selected items.', 'ultimate-post')
440 );
441 }
442 break;
443
444 case 'license_action':
445 $message = '';
446
447 if ( isset($post['edd_ultp_license_key']) && function_exists('ultimate_post_pro') ) {
448 $is_success = false;
449 $license = trim( sanitize_text_field( $post['edd_ultp_license_key'] ) );
450
451 if ($license && $license != '******************') {
452 update_option( 'edd_ultp_license_key', $license);
453 $api_params = array(
454 'edd_action' => 'activate_license',
455 'license' => $license,
456 'item_id' => 181,
457 'url' => home_url()
458 );
459
460 $response = wp_remote_post(
461 'https://www.wpxpo.com',
462 array(
463 'timeout' => 15,
464 'sslverify' => false,
465 'body' => $api_params
466 )
467 );
468
469 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
470 $message = ( is_wp_error( $response ) && ! empty( $response->get_error_message() ) ) ? $response->get_error_message() : __('An error occurred, please try again.', 'ultimate-post-pro');
471 } else {
472 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
473 if ( false === $license_data->success ) {
474 update_option( 'edd_ultp_license_key', '');
475 switch( $license_data->error ) {
476 case 'expired' :
477 $message = sprintf(
478 __('Your license key expired on %s.', 'ultimate-post'),
479 date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) )
480 );
481 break;
482 case 'revoked' :
483 $message = __('Your license key has been disabled.', 'ultimate-post');
484 break;
485 case 'missing' :
486 $message = __('Invalid license.', 'ultimate-post');
487 break;
488 case 'invalid' :
489 case 'site_inactive':
490 $message = __( 'Your license is not active for this URL.', 'ultimate-post' );
491 break;
492 case 'item_name_mismatch':
493 $message = __( 'This appears to be an invalid license key.', 'ultimate-post' );
494 break;
495 case 'no_activations_left':
496 $message = __( 'Your license key has reached its activation limit.', 'ultimate-post' );
497 break;
498 default :
499 $message = __( 'An error occurred, please try again.', 'ultimate-post' );
500 break;
501 }
502 } else {
503 $message = __('Your license key has been updated.', 'ultimate-post');
504 $is_success = true;
505 }
506 update_option( 'edd_ultp_license_status', $license_data->license );
507 update_option( 'edd_ultp_license_expire', $license_data->expires );
508 }
509 } else {
510 $message = __( 'Invalid license.', 'ultimate-post' );
511 }
512 } else {
513 $message = __( 'Invalid license.', 'ultimate-post' );
514 if (!function_exists('ultimate_post_pro')) {
515 $message = __( 'Install & Acivate PostX Pro plugin.', 'ultimate-post' );
516 }
517 }
518 return array('success' => $is_success, 'message' => $message);
519 break;
520
521 case 'action_delete':
522 if (isset($post['ids']) && is_array($post['ids'])) {
523 foreach ($post['ids'] as $id) {
524 wp_delete_post( $id, true);
525 }
526 }
527 return array(
528 'success' => true,
529 'message' => __('The selected item is deleted.', 'ultimate-post')
530 );
531 break;
532
533 // case 'header':
534 // $menu_list = array(
535 // 'menu' => array(
536 // array(
537 // 'link' => '#home',
538 // 'label' => __('Getting Started', 'ultimate-post'),
539 // 'showin' => 'both',
540 // ),
541 // array(
542 // 'link' => '#builder',
543 // 'label' => __('Site Builder', 'ultimate-post'),
544 // 'showin' => ultimate_post()->get_setting('ultp_builder') != 'false' ? 'both' : 'none',
545 // 'showhide' => true
546 // ),
547 // array(
548 // 'link' => '#templatekit',
549 // 'label' => __('Template kits', 'ultimate-post'),
550 // 'showin' => 'both',
551 // ),
552 // array(
553 // 'link' => '#saved-templates',
554 // 'label' => __('Saved Templates', 'ultimate-post'),
555 // 'showin' => ultimate_post()->get_setting('ultp_templates') != 'false' ? 'both' : 'none',
556 // 'showhide' => true
557 // ),
558 // array(
559 // 'link' => '#custom-font',
560 // 'label' => __('Custom Font', 'ultimate-post'),
561 // 'showin' => ultimate_post()->get_setting('ultp_custom_font') != 'false' ? 'both' : 'none',
562 // 'showhide' => true
563 // ),
564 // array(
565 // 'link' => '#addons',
566 // 'label' => __('Addons', 'ultimate-post'),
567 // 'showin' => 'both',
568 // ),
569 // array(
570 // 'link' => '#blocks',
571 // 'label' => __('Blocks', 'ultimate-post'),
572 // 'showin' => 'both',
573 // ),
574 // array(
575 // 'link' => '#settings',
576 // 'label' => __('Settings', 'ultimate-post'),
577 // 'showin' => 'both',
578 // ),
579 // array(
580 // 'link' => '#tutorials',
581 // 'label' => __('Tutorials', 'ultimate-post'),
582 // 'showin' => 'sidebar',
583 // ),
584 // array(
585 // 'link' => '#license',
586 // 'label' => __('License', 'ultimate-post'),
587 // 'showin' => 'sidebar',
588 // )
589 // ),
590 // 'common' => array(
591 // 'version' => ULTP_VER
592 // ),
593 // 'help_menu' => array(
594 // array(
595 // 'label' => __('Get Support', 'ultimate-post'),
596 // 'icon' => 'dashicons-phone',
597 // 'link' => ultimate_post()->get_premium_link('https://www.wpxpo.com/contact/', 'plugin_dir_pro')
598 // ),
599 // array(
600 // 'label' => __('Welcome Guide', 'ultimate-post'),
601 // 'icon' => 'dashicons-megaphone',
602 // 'link' => admin_url('admin.php?page=ultp-initial-setup-wizard')
603 // ),
604 // array(
605 // 'label' => __('Join Community', 'ultimate-post'),
606 // 'icon' => 'dashicons-facebook-alt',
607 // 'link' => 'https://www.facebook.com/groups/gutenbergpostx'
608 // ),
609 // array(
610 // 'label' => __('Feature Request', 'ultimate-post'),
611 // 'icon' => 'dashicons-email-alt',
612 // 'link' => ultimate_post()->get_premium_link('https://www.wpxpo.com/postx/roadmap/', 'plugin_dir_pro')
613 // ),
614 // array(
615 // 'label' => __('Youtube Tutorials', 'ultimate-post'),
616 // 'icon' => 'dashicons-youtube',
617 // 'link' => 'https://www.youtube.com/@wpxpo'
618 // ),
619 // array(
620 // 'label' => __('Documentation', 'ultimate-post'),
621 // 'icon' => 'dashicons-book',
622 // 'link' => 'https://docs.wpxpo.com/docs/postx/'
623 // ),
624 // array(
625 // 'label' => __('What’s New', 'ultimate-post'),
626 // 'icon' => 'dashicons-edit',
627 // 'link' => ultimate_post()->get_premium_link('https://www.wpxpo.com/category/postx/', 'plugin_dir_pro')
628 // ),
629 // )
630 // );
631
632 // return array(
633 // 'success' => true,
634 // 'data' => apply_filters( 'postx_menu_system', $menu_list ),
635 // 'helloBar' => get_transient('ultp_helloBar'),
636 // 'status' => get_option( 'edd_ultp_license_status' ),
637 // 'expire' => get_option( 'edd_ultp_license_expire' )
638 // );
639 // break;
640 case 'helloBarAction':
641 set_transient( 'ultp_helloBar', 'hide', 1296000);
642 return array(
643 'success' => true,
644 'message' => __('Notice is removed.', 'ultimate-post')
645 );
646 break;
647
648 default:
649 # code...
650 break;
651 }
652 }
653 }
654
655 /**
656 * Insert Post For Imported Template
657 *
658 * @since v.2.6.7
659 * @param STRING
660 * @return ARRAY | Inserted Post Url
661 */
662 public function template_page_insert($server) {
663 $post = $server->get_params();
664 $new_page = array(
665 'post_title' => $post['title'],
666 'post_type' => 'page',
667 'post_content' => $post['blockCode'],
668 'post_status' => 'draft',
669 );
670 $post_id = wp_insert_post( $new_page );
671 return array( 'success' => true, 'link' => get_edit_post_link($post_id));
672 }
673
674
675 public function search_settings_action($server) {
676 global $wpdb;
677 $post = $server->get_params();
678
679 switch ($post['type']) {
680 case 'posts':
681 case 'allpost':
682 case 'postExclude':
683 $post_type = array('post');
684 if ($post['type'] == 'allpost') {
685 $post_type = array_keys(ultimate_post()->get_post_type());
686 } else if ($post['type'] == 'postExclude') {
687 $post_type = array($post['condition']);
688 }
689 $args = array(
690 'post_type' => $post_type,
691 'post_status' => 'publish',
692 'posts_per_page' => 10,
693 );
694 if (is_numeric($post['term'])) {
695 $args['p'] = $post['term'];
696 } else {
697 $args['s'] = $post['term'];
698 }
699
700 $post_results = new \WP_Query($args);
701 $data = [];
702 if (!empty($post_results)) {
703 while ( $post_results->have_posts() ) {
704 $post_results->the_post();
705 $id = get_the_ID();
706 $title = html_entity_decode(get_the_title());
707 $data[] = array('value'=>$id, 'title'=>($title?'[ID: '.$id.'] '.$title:('[ID: '.$id.']')));
708 }
709 wp_reset_postdata();
710 }
711 return ['success' => true, 'data' => $data];
712 break;
713
714 case 'author':
715 $term = '%'. $wpdb->esc_like( $post['term'] ) .'%';
716 $post_results = $wpdb->get_results(
717 $wpdb->prepare(
718 "SELECT ID, display_name
719 FROM $wpdb->users
720 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
721 )
722 );
723 $data = [];
724 if (!empty($post_results)) {
725 foreach ($post_results as $key => $val) {
726 $data[] = array('value'=>$val->ID, 'title'=>'[ID: '.$val->ID.'] '.$val->display_name);
727 }
728 }
729 return ['success' => true, 'data' => $data];
730 break;
731
732 case 'taxvalue':
733 $split = explode('###', $post['condition']);
734 $condition = $split[1] != 'multiTaxonomy' ? array($split[1]) : get_object_taxonomies($split[0]);
735 $args = array(
736 'taxonomy' => $condition,
737 'fields' => 'all',
738 'orderby' => 'id',
739 'order' => 'ASC',
740 'name__like'=> $post['term']
741 );
742 if (is_numeric($post['term'])) {
743 unset($args['name__like']);
744 $args['include'] = array($post['term']);
745 }
746
747 $post_results = get_terms( $args );
748 $data = [];
749 if (!empty($post_results)) {
750 foreach ($post_results as $key => $val) {
751 if ($split[1] == 'multiTaxonomy') {
752 $data[] = array('value'=>$val->taxonomy.'###'.$val->slug, 'title'=> '[ID: '.$val->term_id.'] '.$val->taxonomy.': '.$val->name);
753 } else {
754 $data[] = array('value'=>urldecode($val->slug), 'title'=>'[ID: '.$val->term_id.'] '.$val->name);
755 }
756 }
757 }
758 return ['success' => true, 'data' => $data];
759 break;
760
761 case 'taxExclude':
762 $condition = get_object_taxonomies($post['condition']);
763 $args = array(
764 'taxonomy' => $condition,
765 'fields' => 'all',
766 'orderby' => 'id',
767 'order' => 'ASC',
768 'name__like'=> $post['term']
769 );
770 if (is_numeric($post['term'])) {
771 unset($args['name__like']);
772 $args['include'] = array($post['term']);
773 }
774 $post_results = get_terms( $args );
775 $data = [];
776 if (!empty($post_results)) {
777 foreach ($post_results as $key => $val) {
778 $data[] = array('value'=>$val->taxonomy.'###'.$val->slug, 'title'=> '[ID: '.$val->term_id.'] '.$val->taxonomy.': '.$val->name);
779 }
780 }
781 return ['success' => true, 'data' => $data];
782 break;
783 // allPostType
784 case 'allPostType':
785 $all_types = array_values(get_post_types( array( 'public' => true ), 'names' ));
786 $postType = array();
787 foreach($all_types as $type){
788 $postType[] = array(
789 'title' => $type,
790 'value' => $type,
791 );
792 };
793
794 return ['success' => true, 'data' => $postType ];
795 default:
796 return ['success' => true, 'data' => [['value'=>'', 'title'=>'- Select -']]];
797 break;
798 }
799 }
800
801 /**
802 * Post Data Response of REST API
803 *
804 * @since v.1.0.0
805 * @param MIXED | Pram (ARRAY), Local (BOOLEAN)
806 * @return ARRAY | Response Image Size as Array
807 */
808 public function ultp_route_post_data($prams) {
809 if ( ! (isset($_REQUEST['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($_REQUEST['wpnonce'])), 'ultp-nonce' )) ) {
810 die();
811 }
812 $prams = $prams->get_params();
813 $data = [];
814 $loop = new \WP_Query( ultimate_post()->get_query($prams) );
815 $max_tax = isset($prams['maxTaxonomy']) && $prams['maxTaxonomy'] ? ( $prams['maxTaxonomy'] == '0' ? 0 : $prams['maxTaxonomy'] ) : 30 ;
816
817
818 if ($loop->have_posts()) {
819 while($loop->have_posts()) {
820 $loop->the_post();
821 $var = array();
822 $post_id = get_the_ID();
823 $user_id = get_the_author_meta('ID');
824 $content_data = get_the_content();
825 $var['ID'] = $post_id;
826 $var['title'] = get_the_title();
827 $var['permalink'] = get_permalink();
828 $var['seo_meta'] = ultimate_post()->get_excerpt($post_id, 1);
829 $var['excerpt'] = strip_tags($content_data);
830 $var['excerpt_full']= strip_tags(get_the_excerpt());
831 $var['time'] = (int)get_the_date('U')*1000;
832 $var['timeModified']= (int)get_the_modified_date('U')*1000;
833 $var['post_time'] = human_time_diff(get_the_time('U'),current_time('U'));
834 $var['view'] = get_post_meta(get_the_ID(),'__post_views_count', true);
835 $var['comments'] = get_comments_number();
836 $var['author_link'] = get_author_posts_url($user_id);
837 $var['avatar_url'] = get_avatar_url($user_id);
838 $var['display_name']= get_the_author_meta('display_name');
839 $var['reading_time']= ceil(strlen($content_data)/1200);
840 $post_video = get_post_meta($post_id, '__builder_feature_video', true);
841 // Video
842 if($post_video){
843 $var['has_video'] = true;
844 }
845 // image
846 $image_sizes = ultimate_post()->get_image_size();
847 $image_src = array();
848 if (has_post_thumbnail()) {
849 $thumb_id = get_post_thumbnail_id($post_id);
850 foreach ($image_sizes as $key => $value) {
851 $image_src[$key] = wp_get_attachment_image_src($thumb_id, $key, false)[0];
852 }
853 $var['image'] = $image_src;
854 } elseif(isset($prams['fallbackImg']['id'])) {
855 foreach ($image_sizes as $key => $value) {
856 $image_src[$key] = wp_get_attachment_image_src($prams['fallbackImg']['id'], $key, false)[0];
857 }
858 $var['image'] = $image_src;
859 $var['is_fallback'] = true;
860 }
861
862 // tag
863 $tag = get_the_terms($post_id, (isset($prams['tag'])?esc_attr($prams['tag']):'post_tag'));
864 if (!empty($tag)) {
865 $v = array();
866 foreach ($tag as $k => $val) {
867 if ($k >= $max_tax) { break; }
868 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id));
869 }
870 $var['tag'] = $v;
871 }
872
873 // Taxonomy
874 $cat = get_the_terms($post_id, (isset($prams['taxonomy'])?esc_attr($prams['taxonomy']):'category'));
875
876 if(!empty($cat)){
877 $v = array();
878 foreach ($cat as $k => $val) {
879 if ($k >= $max_tax) { break; }
880 $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));
881 }
882 $var['category'] = $v;
883 }
884 $data[] = $var;
885 }
886 wp_reset_postdata();
887 }
888 return rest_ensure_response( $data);
889 }
890
891
892 /**
893 * Taxonomy Data Response of REST API
894 *
895 * @since v.1.0.0
896 * @param ARRAY | Parameter (ARRAY)
897 * @return ARRAY | Response Taxonomy List as Array
898 */
899 public function ultp_route_common_data($prams) {
900 if ( ! (isset($_REQUEST['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($_REQUEST['wpnonce'])), 'ultp-nonce' )) ) {
901 return rest_ensure_response([]);
902 }
903
904 $all_post_type = ultimate_post()->get_post_type();
905 $data = array();
906 foreach ($all_post_type as $post_type_slug => $post_type ) {
907 $data_term = array();
908 $taxonomies = get_object_taxonomies($post_type_slug);
909 foreach ($taxonomies as $key => $taxonomy_slug) {
910 $taxonomy_value = get_terms(array(
911 'taxonomy' => $taxonomy_slug,
912 'hide_empty' => false
913 ));
914 if (!is_wp_error($taxonomy_value)) {
915 $data_tax = array();
916 foreach ($taxonomy_value as $k => $taxonomy) {
917 $data_tax[urldecode_deep($taxonomy->slug)] = $taxonomy->name;
918 }
919 if (count($data_tax) > 0) {
920 $data_term[$taxonomy_slug] = $data_tax;
921 }
922 }
923 }
924 $data[$post_type_slug] = $data_term;
925 }
926 // Global Customizer
927 $global = get_option('postx_global', []);
928 // Image Size
929 $image_sizes = ultimate_post()->get_image_size();
930
931 return rest_ensure_response(['taxonomy' => $data, 'global' => $global, 'image' => json_encode($image_sizes), 'posttype' => json_encode($all_post_type)]);
932 }
933
934 /**
935 * Specific Taxonomy Data Response of REST API
936 *
937 * @since v.1.0.0
938 * @param ARRAY | Parameter (ARRAY)
939 * @return ARRAY | Response Taxonomy List as Array
940 */
941 public function ultp_route_taxonomy_info_data($prams) {
942 if ( ! (isset($_REQUEST['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($_REQUEST['wpnonce'])), 'ultp-nonce' )) ) {
943 return rest_ensure_response([]);
944 }
945
946 return rest_ensure_response( ultimate_post()->get_category_data(json_decode($prams['taxValue']), $prams['queryNumber'], $prams['taxType'], $prams['taxSlug'], $prams['archiveBuilder']) );
947 }
948
949 /**
950 * Search Block Data Showing
951 *
952 * @since v.2.9.9
953 * @param STRING
954 * @return ARRAY | Inserted Post Url
955 */
956 public function ultp_search_result($server) {
957 $post = $server->get_params();
958
959 $query_args = array(
960 's' => sanitize_text_field($post['searchText']),
961 'paged' => sanitize_text_field($post['paged']),
962 'compare' => 'LIKE',
963 'orderby' => 'relevance',
964 'posts_per_page' => sanitize_text_field($post['postPerPage']),
965 );
966 if(isset($post['exclude']) && is_array($post['exclude']) && count($post['exclude']) > 0) {
967 $post_exclude = array();
968 foreach( $post['exclude'] as $data ){
969 $post_exclude[$data['title']] = $data['title'];
970 }
971 $all_types = get_post_types( array( 'public' => true ), 'names' );
972 $post_type = array_diff_key($all_types, $post_exclude);
973 $query_args['post_type'] = $post_type;
974 }
975 $output = '';
976 $query_result = new \WP_Query($query_args);
977
978 if ($query_result->have_posts()) {
979 while ($query_result->have_posts()) {
980 $query_result->the_post();
981 $post_id = get_the_ID();
982 $title = get_the_title();
983
984 $output .= '<div class="ultp-search-result__item">';
985 if ($post['image'] == 1 && has_post_thumbnail()) {
986 $thumb_id = get_post_thumbnail_id($post_id);
987 $output .= '<img class="ultp-searchresult-image" src='.wp_get_attachment_image_src($thumb_id, 'thumbnail', false)[0].' alt="'.$title.'"/>';
988 }
989 $output .= '<div class="ultp-searchresult-content">';
990 $output .= '<div class="ultp-rescontent-meta">';
991 // Category
992 $post_cat = get_the_terms($post_id, 'category');
993 if ($post['category'] == 1 && $post_cat && count($post_cat)) {
994 $output .= '<div class="ultp-searchresult-category">';
995 foreach($post_cat as $cat){
996 $output .= '<a href="'.get_term_link($cat->term_id).'">'.$cat->name.'</a>';
997 }
998 $output .= '</div>';
999 }
1000 // Author
1001 if ($post['author'] == 1) {
1002 $user_id = get_the_author_meta('ID');
1003 $output .= '<a href="'.get_author_posts_url($user_id).'" class="ultp-searchresult-author">'.get_the_author_meta('display_name').'</a>';
1004 }
1005 // Date
1006 if ($post['date'] == 1) {
1007 $output .= '<div class="ultp-searchresult-publishdate">'.get_the_date('F j, Y').'</div>';
1008 }
1009 $output .= '</div>';
1010 $output .= '<a href="'.get_permalink().'" class="ultp-searchresult-title">'.$title.'</a>';
1011 if ($post['excerpt'] == 1) {
1012 $output .= '<div class="ultp-searchresult-excerpt">'.wp_trim_words(get_the_excerpt(), $post['excerptLimit']).'</div>';
1013 }
1014 $output .= '</div>';
1015 $output .= '</div>';
1016 }
1017 }
1018
1019 return array('post_data' => $output, 'post_count' => $query_result->found_posts);
1020 }
1021 }