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

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