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

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