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

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