PluginProbe
Post Grid Gutenberg Blocks – PostX / 4.1.9
Post Grid Gutenberg Blocks – PostX v4.1.9
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 4.1.9, at classes/REST_API.php

1,214 lines 48.7 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 use WP_REST_Request;
11
12 defined('ABSPATH') || exit;
13
14 /**
15 * REST_API class.
16 */
17 class REST_API {
18
19 /**
20 * Setup class.
21 *
22 * @since v.1.0.0
23 */
24 public function __construct() {
25 add_action( 'rest_api_init', array($this, 'ultp_register_route') );
26 }
27
28 /**
29 * REST API Action
30 *
31 * @since v.1.0.0
32 * @return NULL
33 */
34 public function ultp_register_route() {
35 register_rest_route( 'ultp', 'common', array(
36 'methods' => \WP_REST_Server::READABLE,
37 'args' => array('wpnonce' => []),
38 'callback' => array($this,'ultp_route_common_data'),
39 'permission_callback' => function () {
40 return current_user_can( 'edit_posts' );
41 },
42 )
43 );
44 register_rest_route(
45 'ultp',
46 '/fetch_posts/',
47 array(
48 array(
49 'methods' => 'POST',
50 'args' => array(),
51 'callback' => array($this, 'ultp_route_post_data'),
52 'permission_callback' => function () {
53 return current_user_can( 'edit_posts' );
54 },
55 )
56 )
57 );
58 register_rest_route(
59 'ultp',
60 '/specific_taxonomy/',
61 array(
62 array(
63 'methods' => 'POST',
64 'args' => array(),
65 'callback' => array($this, 'ultp_route_taxonomy_info_data'),
66 'permission_callback' => function () {
67 return current_user_can( 'edit_others_posts' );
68 },
69 )
70 )
71 );
72 register_rest_route(
73 'ultp/v1',
74 '/search/',
75 array(
76 array(
77 'methods' => 'POST',
78 'callback' => array($this, 'search_settings_action'),
79 'permission_callback' => function () {
80 return current_user_can('edit_others_posts');
81 },
82 'args' => array()
83 )
84 )
85 );
86 register_rest_route(
87 'ultp/v2',
88 '/template_page_insert/',
89 array(
90 array(
91 'methods' => 'POST',
92 'callback' => array($this, 'template_page_insert'),
93 'permission_callback' => function () {
94 return current_user_can('manage_options');
95 },
96 'args' => array()
97 )
98 )
99 );
100 register_rest_route(
101 'ultp/v2',
102 '/addon_block_action/',
103 array(
104 array(
105 'methods' => 'POST',
106 'callback' => array($this, 'addon_block_action'),
107 'permission_callback' => function () {
108 return current_user_can('manage_options');
109 },
110 'args' => array()
111 )
112 )
113 );
114 register_rest_route(
115 'ultp/v2',
116 '/premade_wishlist_save/',
117 array(
118 array(
119 'methods' => 'POST',
120 'callback' => array($this, 'premade_wishlist_save'),
121 'permission_callback' => function () {
122 return current_user_can('edit_others_posts');
123 },
124 'args' => array()
125 )
126 )
127 );
128 register_rest_route(
129 'ultp/v2',
130 '/save_plugin_settings/',
131 array(
132 array(
133 'methods' => 'POST',
134 'callback' => array($this, 'save_plugin_settings'),
135 'permission_callback' => function () {
136 return current_user_can('manage_options');
137 },
138 'args' => array()
139 )
140 )
141 );
142 register_rest_route(
143 'ultp',
144 '/ultp_search_data/',
145 array(
146 array(
147 'methods' => 'POST',
148 'callback' => array($this, 'ultp_search_result'),
149 'permission_callback' => '__return_true'
150 )
151 )
152 );
153 register_rest_route(
154 'ultp/v2',
155 '/get_all_settings/',
156 array(
157 array(
158 'methods' => 'POST',
159 'callback' => array($this, 'get_all_settings'),
160 'permission_callback' => function () {
161 return current_user_can('manage_options');
162 },
163 'args' => array()
164 )
165 )
166 );
167 register_rest_route(
168 'ultp/v2',
169 '/dashborad/',
170 array(
171 array(
172 'methods' => 'POST',
173 'callback' => array( $this, 'get_dashboard_callback'),
174 'permission_callback' => function () {
175 return current_user_can( 'manage_options' );
176 },
177 'args' => array()
178 )
179 )
180 );
181 register_rest_route(
182 'ultp/v2',
183 '/wizard_site_status/',
184 array(
185 array(
186 'methods' => 'POST',
187 'callback' => array( $this, 'wizard_site_status_callback'),
188 'permission_callback' => function () {
189 return current_user_can( 'manage_options' );
190 },
191 'args' => array()
192 )
193 )
194 );
195 register_rest_route(
196 'ultp/v2',
197 '/send_initial_plugin_data/',
198 array(
199 array(
200 'methods' => 'POST',
201 'callback' => array( $this, 'send_initial_plugin_data_callback'),
202 'permission_callback' => function () {
203 return current_user_can( 'manage_options' );
204 },
205 'args' => array()
206 )
207 )
208 );
209 register_rest_route(
210 'ultp/v2',
211 '/initial_setup_complete/',
212 array(
213 array(
214 'methods' => 'POST',
215 'callback' => array( $this, 'initial_setup_complete_callback'),
216 'permission_callback' => function () {
217 return current_user_can( 'manage_options' );
218 },
219 'args' => array()
220 )
221 )
222 );
223 register_rest_route(
224 'ultp/v2',
225 '/custom_tax/',
226 array(
227 array(
228 'methods' => 'POST',
229 'callback' => array( $this, 'custom_tax_callback'),
230 'permission_callback' => function () {
231 return current_user_can( 'manage_options' );
232 },
233 'args' => array()
234 )
235 )
236 );
237 register_rest_route(
238 'ultp/v2',
239 '/init_site_dark_logo/',
240 array(
241 array(
242 'methods' => 'POST',
243 'callback' => array( $this, 'init_site_dark_logo_callback'),
244 'permission_callback' => function () {
245 return current_user_can( 'manage_options' );
246 },
247 'args' => array()
248 )
249 )
250 );
251 register_rest_route(
252 'ultp/v2',
253 '/init_site_dark_logo/',
254 array(
255 array(
256 'methods' => 'POST',
257 'callback' => array( $this, 'init_site_dark_logo_callback'),
258 'permission_callback' => function () {
259 return current_user_can( 'edit_others_posts' );
260 },
261 'args' => array()
262 )
263 )
264 );
265 register_rest_route(
266 'ultp/v2',
267 '/get_ultp_image_size/',
268 array(
269 array(
270 'methods' => 'POST',
271 'callback' => array( $this, 'get_custom_image_size'),
272 'permission_callback' => function () {
273 return current_user_can( 'edit_posts' );
274 },
275 'args' => array()
276 )
277 )
278 );
279 register_rest_route(
280 'ultp/v2',
281 '/template_action/',
282 array(
283 array(
284 'methods' => 'POST',
285 'callback' => array($this, 'template_page_action'),
286 'permission_callback' => function () {
287 return current_user_can('manage_options');
288 },
289 'args' => array()
290 )
291 )
292 );
293 }
294
295 /**
296 * Delete Template Page and Handle builder, saved templates, custom font actions
297 *
298 * @since v.2.6.6 // Shifted from RequestAPI since v4.0.3
299 * @param STRING
300 * @return ARRAY | Success Message
301 */
302 public function template_page_action($server) {
303 $post = $server->get_params();
304 $message = '';
305 $s_Type = isset($post['type']) ? ultimate_post()->ultp_rest_sanitize_params($post['type']) : '';
306 $s_id = isset($post['id']) ? ultimate_post()->ultp_rest_sanitize_params($post['id']) : '';
307 $s_status = isset($post['status']) ? ultimate_post()->ultp_rest_sanitize_params($post['status']) : '';
308
309 if ( $s_Type && $s_id ) {
310 if ( $s_Type == 'delete' ) {
311 if ( isset($post['section']) && $post['section'] == 'builder' ) { // phpcs:ignore WordPress.Security
312 $conditions = get_option('ultp_builder_conditions', []);
313 $builder_type = get_post_meta( $s_id, '__ultp_builder_type', true );
314 if ( isset($conditions[$builder_type][$s_id]) ) {
315 unset($conditions[$builder_type][$s_id]);
316 update_option('ultp_builder_conditions', $conditions);
317 }
318 }
319 wp_delete_post( $s_id, true);
320 $message = __('Template has been deleted.', 'ultimate-post');
321 } else if ( $s_Type == 'duplicate' ) {
322 $fromBuilder = ( isset($post['section']) && $post['section'] == 'builder' ) ? true : false; // phpcs:ignore WordPress.Security
323 $post_id = $s_id;
324 $r_post = get_post( $post_id );
325 $current_user = wp_get_current_user();
326 $new_post_author = $current_user->ID;
327 if ( isset( $r_post ) && $r_post != null ) {
328 $args = array(
329 'post_author' => $new_post_author,
330 'post_content' => str_replace('u0022', '\u0022', $r_post->post_content),
331 'post_excerpt' => $r_post->post_excerpt,
332 'post_name' => $r_post->post_name,
333 'post_status' => $fromBuilder ? 'draft' : $r_post->post_status,
334 'post_title' => $r_post->post_title,
335 'post_type' => $r_post->post_type,
336 );
337 }
338 $new_post_id = wp_insert_post( $args );
339
340 $css = get_post_meta( $post_id, '_ultp_css', true );
341 update_post_meta( $new_post_id, '_ultp_css', $css );
342 update_post_meta( $new_post_id, '_ultp_active', 'yes' );
343
344 if ( $fromBuilder ) {
345 $type = get_post_meta( $post_id, '__ultp_builder_type', true );
346 update_post_meta( $new_post_id, '__ultp_builder_type', $type );
347
348 $width = get_post_meta( $post_id, '__container_width', true );
349 update_post_meta( $new_post_id, '__container_width', $width );
350
351 $sidebar = get_post_meta( $post_id, '__builder_sidebar', true );
352 update_post_meta( $new_post_id, '__builder_sidebar', $sidebar );
353
354 $widget_area = get_post_meta( $post_id, '__builder_widget_area', true );
355 update_post_meta( $new_post_id, '__builder_widget_area', $widget_area );
356
357 $conditions = get_option('ultp_builder_conditions', array());
358 if ($conditions && $type) {
359 if (isset($conditions[$type][$post_id])) {
360 $conditions[$type][$new_post_id] = $conditions[$type][$post_id];
361 update_option('ultp_builder_conditions', $conditions);
362 }
363 }
364 }
365 $message = __('Template has been duplicated.', 'ultimate-post');
366 } else if ( $s_Type == 'status') {
367 if ( $s_status ) {
368 wp_update_post(array(
369 'ID' => $s_id,
370 'post_status' => $s_status
371 ));
372 }
373 $message = __('Status has been changed.', 'ultimate-post');
374 }
375 }
376
377 return array(
378 'success' => true,
379 'message' => $message
380 );
381 }
382
383 /**
384 * Initial Plugin Setup Complete
385 *
386 * * @since v.2.8.1
387 * @return STRING
388 */
389 public static function initial_setup_complete_callback($server) {
390 $post = $server->get_params();
391 if ( ! (isset($post['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($post['wpnonce'])), 'ultp-nonce' )) ) {
392 die();
393 }
394 ultimate_post()->set_setting('init_setup', 'yes');
395 return rest_ensure_response([
396 'success' => true,
397 'redirect' => admin_url('admin.php?page=ultp-settings#home'),
398 ]);
399 }
400
401 /**
402 * Send Plugin Data When Initial Setup
403 *
404 * * @since v.2.8.1
405 * @return STRING
406 */
407 public function send_initial_plugin_data_callback($server) {
408 $post = $server->get_params();
409 if ( ! (isset($post['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($post['wpnonce'])), 'ultp-nonce' )) ) {
410 die();
411 }
412
413
414 $site = isset($post['site']) ? ultimate_post()->ultp_rest_sanitize_params( $post['site'] ) : '';
415
416 require_once ULTP_PATH.'classes/Deactive.php';
417 $obj = new \ULTP\Deactive();
418 $obj->send_plugin_data('postx_wizard', $site);
419 }
420
421 /**
422 * wizard_site_status_callback
423 *
424 * * @since v.3.0.0
425 * @return STRING
426 */
427 public static function wizard_site_status_callback() {
428 if ( ! (isset($_POST['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($_POST['wpnonce'])), 'ultp-nonce' )) ) {
429 die();
430 }
431 if ( isset( $_POST['siteType'] ) ) {
432 $site_type = ultimate_post()->ultp_rest_sanitize_params( $_POST['siteType'] );
433 update_option( '__ultp_site_type', $site_type );
434 }
435 require_once ULTP_PATH.'classes/Deactive.php';
436 $obj = new \ULTP\Deactive();
437 $obj->send_plugin_data('postx_wizard', $site_type ? $site_type : 'other');
438
439 return rest_ensure_response( ['success' => true ]);
440 }
441
442 /**
443 * Save Settings of Option Panel
444 *
445 * @since v.3.0.0
446 * @return NULL
447 */
448 public function get_all_settings($server) {
449 return rest_ensure_response(['success' => true, 'settings' => ultimate_post()->get_setting()]);
450 }
451 /**
452 * Save Settings of Option Panel
453 *
454 * @since v.3.0.0
455 * @return NULL
456 */
457 public function save_plugin_settings($server) {
458 $post = $server->get_params();
459 $data = ultimate_post()->ultp_rest_sanitize_params($post['settings']);
460 if (count($data) > 0) {
461 foreach ($data as $key => $val) {
462 ultimate_post()->set_setting($key, $val);
463 }
464 do_action('ultimate_post_after_setting_save');
465 }
466 return rest_ensure_response([
467 'success' => true,
468 'message' => __('You have successfully saved the settings data.', 'ultimate-post') ,
469 'wishListArr' => wp_json_encode($data)]);
470 }
471
472 /**
473 * Save and get premade_wishlist_save
474 *
475 * @since v.3.0.0
476 * @param STRING
477 * @return ARRAY | Inserted Post Url
478 */
479 public function premade_wishlist_save($server) {
480 $post = $server->get_params();
481 $id = isset($post['id'])? ultimate_post()->ultp_rest_sanitize_params($post['id']):'';
482 $action = isset($post['action'])? ultimate_post()->ultp_rest_sanitize_params($post['action']):'';
483 $wishListArr = get_option('ultp_premade_wishlist', []);
484 $request_type = isset($post['type'])?ultimate_post()->ultp_rest_sanitize_params($post['type']):'';
485
486 if ($id && $request_type != 'fetchData') {
487 if($action == 'remove') {
488 $index = array_search($id, $wishListArr);
489 if ($index !== false) {
490 unset($wishListArr[$index]);
491 }
492 } else {
493 if (!in_array($id, $wishListArr)) {
494 array_push($wishListArr, $id );
495 }
496 }
497 update_option('ultp_premade_wishlist', $wishListArr);
498 }
499 return rest_ensure_response([
500 'success' => true,
501 'message' => $action == 'remove' ? __('Item has been removed from wishlist.', 'ultimate-post') : __('Item added to wishlist.', 'ultimate-post'),
502 'wishListArr' => wp_json_encode($wishListArr)]
503 );
504 }
505 /**
506 * Save addon / blocks on/off data
507 *
508 * @since v.3.0.0
509 * @param STRING
510 * @return ARRAY | Inserted Post Url
511 */
512 public function addon_block_action($server) {
513 $post = $server->get_params();
514 $addon_name = isset($post['key'])? ultimate_post()->ultp_rest_sanitize_params($post['key']):'';
515 $addon_value = isset($post['value'])? ultimate_post()->ultp_rest_sanitize_params($post['value']):'';
516 if ($addon_name) {
517 $addon_data = ultimate_post()->get_setting();
518 $addon_data[$addon_name] = $addon_value;
519 $GLOBALS['ultp_settings'][$addon_name] = $addon_value;
520 update_option('ultp_options', $addon_data);
521 }
522 return array(
523 'success' => true,
524 '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'))
525 );
526 }
527 /**
528 * Saved Template & Custom Font Actions
529 *
530 * @since v.3.0.0
531 * @param STRING
532 * @return ARRAY | Inserted Post Url
533 */
534 public function get_dashboard_callback($server) {
535 $post = $server->get_params();
536 $request_type = isset($post['type'])?ultimate_post()->ultp_rest_sanitize_params($post['type']):'';
537 $pType = isset($post['pType'])?ultimate_post()->ultp_rest_sanitize_params($post['pType']):'';
538 switch ($request_type) {
539
540 case 'saved_templates':
541 $post_per_page = 10;
542 $data = [];
543 $args = array(
544 'post_type' => $pType,
545 'post_status' => array('publish', 'draft'),
546 'posts_per_page' => $post_per_page,
547 'paged' => isset($post['pages'])?ultimate_post()->ultp_rest_sanitize_params($post['pages']):1
548 );
549
550 if (isset($post['search'])) {
551 $args['paged'] = 1;
552 $args['s'] = ultimate_post()->ultp_rest_sanitize_params($post['search']);
553 }
554
555 $the_query = new \WP_Query( $args );
556 if ( $the_query->have_posts() ) {
557 while ( $the_query->have_posts() ) {
558 $the_query->the_post();
559 $final = [
560 'id' => get_the_ID(),
561 'title' => get_the_title(),
562 'date' => get_the_modified_date('Y/m/d h:i a'),
563 'status' => get_post_status(),
564 'edit' => get_edit_post_link()
565 ];
566
567 if ($pType == 'ultp_custom_font') {
568 $final = array_merge($final ,['woff' => false,'woff2' => false,'ttf' => false,'svg' => false,'eot' => false]);
569 $settings = get_post_meta( get_the_ID(), '__font_settings', true );
570 foreach ($settings as $key => $value) {
571 if ($value['ttf']) { $final['ttf'] = true; }
572 if ($value['svg']) { $final['svg'] = true; }
573 if ($value['eot']) { $final['eot'] = true; }
574 if ($value['woff']) { $final['woff'] = true; }
575 if ($value['woff2']) { $final['woff2'] = true; }
576 }
577 $final['font_settings'] = $settings;
578 }
579 $data[] = $final;
580 }
581 }
582 wp_reset_postdata();
583 return array(
584 'success' => true,
585 'data' => $data,
586 'new' => ($pType == 'ultp_custom_font' ? admin_url('post-new.php?post_type=ultp_custom_font') : admin_url('post-new.php?post_type=ultp_templates')),
587 'found' => $the_query->found_posts,
588 'pages' => $the_query->max_num_pages
589 );
590 break;
591
592 case 'action_draft':
593 case 'action_publish':
594 if (isset($post['ids']) && is_array($post['ids'])) {
595 $post_ids = ultimate_post()->ultp_rest_sanitize_params($post['ids']);
596 foreach ($post_ids as $id) {
597 wp_update_post(array(
598 'ID' => $id,
599 'post_status' => str_replace('action_', '',$request_type)
600 ));
601 }
602 return array(
603 'success' => true,
604 'message' => __('Status changed for selected items.', 'ultimate-post')
605 );
606 }
607 break;
608
609 case 'license_action':
610 $message = '';
611
612 if ( isset($post['edd_ultp_license_key']) && function_exists('ultimate_post_pro') ) {
613 $is_success = false;
614 $license = trim( ultimate_post()->ultp_rest_sanitize_params( $post['edd_ultp_license_key'] ) );
615
616 if ($license && $license != '******************') {
617 update_option( 'edd_ultp_license_key', $license);
618 $api_params = array(
619 'edd_action' => 'activate_license',
620 'license' => $license,
621 'item_id' => 181,
622 'url' => home_url()
623 );
624
625 $response = wp_remote_post(
626 'https://account.wpxpo.com',
627 array(
628 'timeout' => 15,
629 'sslverify' => false,
630 'body' => $api_params
631 )
632 );
633
634 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
635 $message = ( is_wp_error( $response ) && ! empty( $response->get_error_message() ) ) ? $response->get_error_message() : __('An error occurred, please try again.', 'ultimate-post-pro');
636 } else {
637 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
638 if ( false === $license_data->success ) {
639 update_option( 'edd_ultp_license_key', '');
640 switch( $license_data->error ) {
641 case 'expired' :
642 $message = sprintf(
643 __('Your license key expired on %s.', 'ultimate-post'),
644 date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) )
645 );
646 break;
647 case 'revoked' :
648 $message = __('Your license key has been disabled.', 'ultimate-post');
649 break;
650 case 'missing' :
651 $message = __('Invalid license.', 'ultimate-post');
652 break;
653 case 'invalid' :
654 case 'site_inactive':
655 $message = __( 'Your license is not active for this URL.', 'ultimate-post' );
656 break;
657 case 'item_name_mismatch':
658 $message = __( 'This appears to be an invalid license key.', 'ultimate-post' );
659 break;
660 case 'no_activations_left':
661 $message = __( 'Your license key has reached its activation limit.', 'ultimate-post' );
662 break;
663 default :
664 $message = __( 'An error occurred, please try again.', 'ultimate-post' );
665 break;
666 }
667 } else {
668 $message = __('Your license key has been updated.', 'ultimate-post');
669 $is_success = true;
670 }
671 update_option( 'edd_ultp_license_status', $license_data->license );
672 update_option( 'edd_ultp_license_expire', $license_data->expires );
673 update_option( 'edd_ultp_activations_left', $license_data->activations_left );
674
675 }
676 } else {
677 $message = __( 'Invalid license.', 'ultimate-post' );
678 }
679 } else {
680 $message = __( 'Invalid license.', 'ultimate-post' );
681 if (!function_exists('ultimate_post_pro')) {
682 $message = __( 'Install & Acivate PostX Pro plugin.', 'ultimate-post' );
683 }
684 }
685 return array('success' => $is_success, 'message' => $message);
686 break;
687
688 case 'action_delete':
689 if (isset($post['ids']) && is_array($post['ids'])) {
690 $post_ids = ultimate_post()->ultp_rest_sanitize_params($post['ids']);
691 foreach ($post_ids as $id) {
692 wp_delete_post( $id, true);
693 }
694 }
695 return array(
696 'success' => true,
697 'message' => __('The selected item is deleted.', 'ultimate-post')
698 );
699
700 case 'support_data':
701 $user_info = get_userdata( get_current_user_id() );
702 $name = $user_info->first_name . ($user_info->last_name ? ' ' . $user_info->last_name : '');
703 return array(
704 'success' => true,
705 'data' => array(
706 'name' => $name ? $name : $user_info->user_login,
707 'email' => $user_info->user_email
708 )
709 );
710
711 case 'support_action':
712 $api_params = array(
713 'user_name' => isset($post['name'])? ultimate_post()->ultp_rest_sanitize_params($post['name']):'',
714 'user_email' =>isset($post['email'])? sanitize_email($post['email']):'',
715 'subject' => isset($post['subject'])? ultimate_post()->ultp_rest_sanitize_params($post['subject']):'',
716 'desc' => isset($post['desc'])? sanitize_textarea_field($post['desc']):'',
717 );
718 $response = wp_remote_get(
719 'https://wpxpo.com/wp-json/v2/support_mail',
720 array(
721 'method' => 'POST',
722 'timeout' => 120,
723 'body' => $api_params
724 )
725 );
726 $response_data = json_decode($response['body']);
727 $success = ( isset($response_data->success) && $response_data->success ) ? true : false;
728
729 return array(
730 'success' => $success,
731 'message' => $success ? __('New Support Ticket has been Created.', 'ultimate-post') : __('New Support Ticket is not Created Due to Some Issues.', 'ultimate-post')
732 );
733 break;
734
735 case 'helloBarAction':
736 set_transient( 'ultp_helloBar'.ULTP_HELLOBAR, 'hide', 1296000);
737 return array(
738 'success' => true,
739 'message' => __('Notice is removed.', 'ultimate-post')
740 );
741 break;
742 case 'generalDiscountAction':
743 set_transient( 'ultp_generalDiscount', 'hide', 60 * DAY_IN_SECONDS);
744 return array(
745 'success' => true,
746 'message' => __('Notice is removed.', 'ultimate-post')
747 );
748 break;
749
750 default:
751 # code...
752 break;
753 }
754
755 }
756
757 /**
758 * Insert Post For Imported Template
759 *
760 * @since v.2.6.7
761 * @param STRING
762 * @return ARRAY | Inserted Post Url
763 */
764 public function template_page_insert($server) {
765 $post = $server->get_params();
766 $new_page = array(
767 'post_title' => isset($post['title'])?ultimate_post()->ultp_rest_sanitize_params($post['title']):'',
768 'post_type' => 'page',
769 'post_content' => $post['blockCode'],
770 'post_status' => 'draft',
771 );
772 $post_id = wp_insert_post( $new_page );
773 return array( 'success' => true, 'link' => get_edit_post_link($post_id));
774 }
775
776
777 public function search_settings_action($server) {
778 global $wpdb;
779 $post = $server->get_params();
780 $request_type = isset($post['type'])?ultimate_post()->ultp_rest_sanitize_params($post['type']):'';
781 $condition_type = isset($post['condition'])?ultimate_post()->ultp_rest_sanitize_params($post['condition']):'';
782 $term_type = isset($post['term'])?ultimate_post()->ultp_rest_sanitize_params( $post['term'] ):'';
783 switch ($request_type) {
784 case 'posts':
785 case 'allpost':
786 case 'postExclude':
787 $post_type = array('post');
788 if ($request_type == 'allpost') {
789 $post_type = array_keys(ultimate_post()->get_post_type());
790 } else if ($request_type == 'postExclude') {
791 $post_type = array($condition_type);
792 }
793 $args = array(
794 'post_type' => $post_type,
795 'post_status' => 'publish',
796 'posts_per_page' => 10,
797 );
798 if (is_numeric($term_type)) {
799 $args['p'] = $term_type;
800 } else {
801 $args['s'] = $term_type;
802 }
803
804 $post_results = new \WP_Query($args);
805 $data = [];
806 if (!empty($post_results)) {
807 while ( $post_results->have_posts() ) {
808 $post_results->the_post();
809 $id = get_the_ID();
810 $title = html_entity_decode(get_the_title());
811 $data[] = array('value'=>$id, 'title'=>($title?'[ID: '.$id.'] '.$title:('[ID: '.$id.']')));
812 }
813 wp_reset_postdata();
814 }
815 return ['success' => true, 'data' => $data];
816 break;
817
818 case 'author':
819 $term = '%'. $wpdb->esc_like( $term_type ) .'%';
820 $post_results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
821 $wpdb->prepare(
822 "SELECT ID, display_name
823 FROM $wpdb->users
824 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
825 )
826 );
827 $data = [];
828 if (!empty($post_results)) {
829 foreach ($post_results as $key => $val) {
830 $data[] = array('value'=>$val->ID, 'title'=>'[ID: '.$val->ID.'] '.$val->display_name);
831 }
832 }
833 return ['success' => true, 'data' => $data];
834 break;
835
836 case 'taxvalue':
837 $split = explode('###', $condition_type);
838 $condition = $split[1] != 'multiTaxonomy' ? array($split[1]) : get_object_taxonomies($split[0]);
839 $args = array(
840 'taxonomy' => $condition,
841 'fields' => 'all',
842 'orderby' => 'id',
843 'order' => 'ASC',
844 'name__like'=> $term_type
845 );
846 if (is_numeric($term_type)) {
847 unset($args['name__like']);
848 $args['include'] = array($term_type);
849 }
850
851 $post_results = get_terms( $args );
852 $data = [];
853 if (!empty($post_results)) {
854 foreach ($post_results as $key => $val) {
855 if ($split[1] == 'multiTaxonomy') {
856 $data[] = array('value'=>$val->taxonomy.'###'.$val->slug, 'title'=> '[ID: '.$val->term_id.'] '.$val->taxonomy.': '.$val->name);
857 } else {
858 $data[] = array('value'=>urldecode($val->slug), 'title'=>'[ID: '.$val->term_id.'] '.$val->name, 'live_title'=> $val->name);
859 }
860 }
861 }
862 return ['success' => true, 'data' => $data];
863 break;
864
865 case 'taxExclude':
866 $condition = get_object_taxonomies($condition_type);
867 $args = array(
868 'taxonomy' => $condition,
869 'fields' => 'all',
870 'orderby' => 'id',
871 'order' => 'ASC',
872 'name__like'=> $term_type
873 );
874 if (is_numeric($term_type)) {
875 unset($args['name__like']);
876 $args['include'] = array($term_type);
877 }
878 $post_results = get_terms( $args );
879 $data = [];
880 if (!empty($post_results)) {
881 foreach ($post_results as $key => $val) {
882 $data[] = array('value'=>$val->taxonomy.'###'.$val->slug, 'title'=> '[ID: '.$val->term_id.'] '.$val->taxonomy.': '.$val->name);
883 }
884 }
885 return ['success' => true, 'data' => $data];
886 break;
887 // allPostType
888 case 'allPostType':
889 $all_types = array_values(get_post_types( array( 'public' => true ), 'names' ));
890 $postType = array();
891 foreach($all_types as $type){
892 $postType[] = array(
893 'title' => $type,
894 'value' => $type,
895 );
896 };
897
898 return ['success' => true, 'data' => $postType ];
899 default:
900 return ['success' => true, 'data' => [['value'=>'', 'title'=>'- Select -']]];
901 break;
902 }
903 }
904
905 /**
906 * Post Data Response of REST API
907 *
908 * @since v.1.0.0
909 * @param MIXED | Pram (ARRAY), Local (BOOLEAN)
910 * @return ARRAY | Response Image Size as Array
911 */
912 public function ultp_route_post_data($prams) {
913 $prams = $prams->get_params();
914 if ( !(isset($prams['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($prams['wpnonce'])), 'ultp-nonce' )) ) {
915 die();
916 }
917 $data = [];
918 $loop = new \WP_Query( ultimate_post()->get_query(ultimate_post()->ultp_rest_sanitize_params($prams)) );
919 $max_tax = isset($prams['maxTaxonomy']) && $prams['maxTaxonomy'] ? ( ultimate_post()->ultp_rest_sanitize_params($prams['maxTaxonomy']) == '0' ? 0 : ultimate_post()->ultp_rest_sanitize_params($prams['maxTaxonomy']) ) : 30 ;
920
921 if ($loop->have_posts()) {
922 while($loop->have_posts()) {
923 $loop->the_post();
924 $var = array();
925 $post_id = get_the_ID();
926 $user_id = get_the_author_meta('ID');
927 $content_data = get_the_content();
928 $var['ID'] = $post_id;
929 $var['title'] = get_the_title();
930 $var['permalink'] = get_permalink();
931 $var['seo_meta'] = ultimate_post()->get_excerpt($post_id, 1);
932 $var['excerpt'] = wp_strip_all_tags($content_data);
933 $var['excerpt_full']= wp_strip_all_tags(get_the_excerpt());
934 $var['time'] = (int)get_the_date('U')*1000;
935 $var['timeModified']= (int)get_the_modified_date('U')*1000;
936 $var['post_time'] = human_time_diff(get_the_time('U'),current_time('U'));
937 $var['view'] = get_post_meta(get_the_ID(),'__post_views_count', true);
938 $var['comments'] = get_comments_number();
939 $var['author_link'] = get_author_posts_url($user_id);
940 $var['avatar_url'] = get_avatar_url($user_id);
941 $var['display_name']= get_the_author_meta('display_name');
942 $var['reading_time']= ceil(strlen($content_data)/1200);
943 $var['acf'] = null;
944
945 if (function_exists('get_field_objects')) {
946 $var['acf'] = get_field_objects();
947 }
948
949 $post_video = get_post_meta($post_id, '__builder_feature_video', true);
950 // Video
951 if ($post_video) {
952 $var['has_video'] = ultimate_post()->get_youtube_id($post_video);
953 }
954 // image
955 $image_sizes = ultimate_post()->get_image_size();
956 $image_src = array();
957 if (has_post_thumbnail()) {
958 $thumb_id = get_post_thumbnail_id($post_id);
959 foreach ($image_sizes as $key => $value) {
960 $image_src[$key] = wp_get_attachment_image_src($thumb_id, $key, false)[0];
961 }
962 $var['image'] = $image_src;
963 } elseif(isset($prams['fallbackImg']['id'])) {
964 foreach ($image_sizes as $key => $value) {
965 $image_src[$key] = wp_get_attachment_image_src(esc_attr($prams['fallbackImg']['id']), $key, false)[0];
966 }
967 $var['image'] = $image_src;
968 $var['is_fallback'] = true;
969 }
970
971 // tag
972 $tag = get_the_terms($post_id, (isset($prams['tag'])?esc_attr($prams['tag']):'post_tag'));
973 if (!empty($tag)) {
974 $v = array();
975 foreach ($tag as $k => $val) {
976 if ($k >= $max_tax) { break; }
977 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id));
978 }
979 $var['tag'] = $v;
980 }
981
982 // Taxonomy
983 $cat = get_the_terms($post_id, (isset($prams['taxonomy'])?esc_attr($prams['taxonomy']):'category'));
984
985 if(!empty($cat)){
986 $v = array();
987 foreach ($cat as $k => $val) {
988 if ($k >= $max_tax) { break; }
989 $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));
990 }
991 $var['category'] = $v;
992 }
993 $data[] = $var;
994 }
995 wp_reset_postdata();
996 }
997 return rest_ensure_response( $data);
998 }
999
1000
1001 /**
1002 * Taxonomy Data Response of REST API
1003 *
1004 * @since v.1.0.0
1005 * @param ARRAY | Parameter (ARRAY)
1006 * @return ARRAY | Response Taxonomy List as Array
1007 */
1008 public function ultp_route_common_data($prams) {
1009 if ( ! (isset($_REQUEST['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($_REQUEST['wpnonce'])), 'ultp-nonce' )) ) {
1010 return rest_ensure_response([]);
1011 }
1012
1013 $all_post_type = ultimate_post()->get_post_type();
1014 $data = array();
1015 foreach ($all_post_type as $post_type_slug => $post_type ) {
1016 $data_term = array();
1017 $taxonomies = get_object_taxonomies($post_type_slug);
1018 foreach ($taxonomies as $key => $taxonomy_slug) {
1019 $taxonomy_value = get_terms(array(
1020 'taxonomy' => $taxonomy_slug,
1021 'hide_empty' => false
1022 ));
1023 if (!is_wp_error($taxonomy_value)) {
1024 $data_tax = array();
1025 foreach ($taxonomy_value as $k => $taxonomy) {
1026 $data_tax[urldecode_deep($taxonomy->slug)] = $taxonomy->name;
1027 }
1028 if (count($data_tax) > 0) {
1029 $data_term[$taxonomy_slug] = $data_tax;
1030 }
1031 }
1032 }
1033 $data[$post_type_slug] = $data_term;
1034 }
1035 // Global Customizer
1036 $global = get_option('postx_global', []);
1037 // Image Size
1038 $image_sizes = ultimate_post()->get_image_size();
1039
1040 return rest_ensure_response(['taxonomy' => $data, 'global' => $global, 'image' => wp_json_encode($image_sizes), 'posttype' => wp_json_encode($all_post_type)]);
1041 }
1042
1043 /**
1044 * Specific Taxonomy Data Response of REST API
1045 *
1046 * @since v.1.0.0
1047 * @param ARRAY | Parameter (ARRAY)
1048 * @return ARRAY | Response Taxonomy List as Array
1049 */
1050 public function ultp_route_taxonomy_info_data($prams) {
1051 $prams = $prams->get_params();
1052 if ( ! (isset($prams['wpnonce']) && wp_verify_nonce( sanitize_key(wp_unslash($prams['wpnonce'])), 'ultp-nonce' )) ) {
1053 return rest_ensure_response([]);
1054 }
1055 $taxValue = isset($prams['taxValue'])?ultimate_post()->ultp_rest_sanitize_params($prams['taxValue']):'';
1056 $queryNumber = isset($prams['queryNumber'])?ultimate_post()->ultp_rest_sanitize_params($prams['queryNumber']):'';
1057 $taxType = isset($prams['taxType'])?ultimate_post()->ultp_rest_sanitize_params($prams['taxType']):'';
1058 $taxSlug = isset($prams['taxSlug'])?ultimate_post()->ultp_rest_sanitize_params($prams['taxSlug']):'';
1059 $archiveBuilder = isset($prams['archiveBuilder'])?ultimate_post()->ultp_rest_sanitize_params($prams['archiveBuilder']):'';
1060
1061 return rest_ensure_response( ultimate_post()->get_category_data(json_decode($taxValue), $queryNumber, $taxType, $taxSlug, $archiveBuilder) );
1062 }
1063
1064 /**
1065 * Get Taxonomies for Custom Post Type
1066 *
1067 * @since v.3.2.8
1068 * @param array $params
1069 * @return array
1070 */
1071 public function custom_tax_callback($prams) {
1072
1073 $post_types = isset($prams['postTypes']) ? ultimate_post()->ultp_rest_sanitize_params($prams['postTypes']) : array();
1074
1075 $data = array(
1076 array(
1077 'id' => '_all',
1078 'name' => __('All', 'ultimate-post')
1079 )
1080 );
1081
1082 foreach ($post_types as $post_type) {
1083 $taxonomies = get_object_taxonomies($post_type);
1084 foreach ($taxonomies as $taxonomy) {
1085 $terms = get_terms(array(
1086 'taxonomy' => $taxonomy,
1087 'hide_empty' => false
1088 ));
1089 foreach ($terms as $term) {
1090 $data[] = array(
1091 'id' => $term->slug,
1092 'name' => $term->name
1093 );
1094 }
1095 }
1096 }
1097
1098 return rest_ensure_response($data);
1099 }
1100
1101 /**
1102 * Search Block Data Showing
1103 *
1104 * @since v.2.9.9
1105 * @param STRING
1106 * @return ARRAY | Inserted Post Url
1107 */
1108 public function ultp_search_result($server) {
1109 $post = $server->get_params();
1110 $searchText = isset($post['searchText'])?ultimate_post()->ultp_rest_sanitize_params($post['searchText']):'';
1111 $paged = isset($post['paged'])?ultimate_post()->ultp_rest_sanitize_params($post['paged']):'';
1112 $postPerPage = isset($post['postPerPage'])?ultimate_post()->ultp_rest_sanitize_params($post['postPerPage']):'';
1113 $query_args = array(
1114 's' => $searchText,
1115 'paged' => $paged,
1116 'compare' => 'LIKE',
1117 'orderby' => 'relevance',
1118 'posts_per_page' => $postPerPage,
1119 );
1120 if(isset($post['exclude']) && is_array($post['exclude']) && count($post['exclude']) > 0) {
1121 $post['exclude'] = ultimate_post()->ultp_rest_sanitize_params($post['exclude']);
1122 $post_exclude = array();
1123 foreach( $post['exclude'] as $data ){
1124 $post_exclude[$data['title']] = $data['title'];
1125 }
1126 $all_types = get_post_types( array( 'public' => true ), 'names' );
1127 $post_type = array_diff_key($all_types, $post_exclude);
1128 $query_args['post_type'] = $post_type;
1129 }
1130 $output = '';
1131 $query_result = new \WP_Query($query_args);
1132
1133 if ($query_result->have_posts()) {
1134 while ($query_result->have_posts()) {
1135 $query_result->the_post();
1136 $post_id = get_the_ID();
1137 $title = get_the_title();
1138
1139 $output .= '<div class="ultp-search-result__item">';
1140 if ($post['image'] == 1 && has_post_thumbnail()) {
1141 $thumb_id = get_post_thumbnail_id($post_id);
1142 $output .= '<img class="ultp-searchresult-image" src='.wp_get_attachment_image_src($thumb_id, 'thumbnail', false)[0].' alt="'.$title.'"/>';
1143 }
1144 $output .= '<div class="ultp-searchresult-content">';
1145 $output .= '<div class="ultp-rescontent-meta">';
1146 // Category
1147 $post_cat = get_the_terms($post_id, 'category');
1148 if ($post['category'] == 1 && $post_cat && count($post_cat)) {
1149 $output .= '<div class="ultp-searchresult-category">';
1150 foreach($post_cat as $cat){
1151 $output .= '<a href="'.get_term_link($cat->term_id).'">'.$cat->name.'</a>';
1152 }
1153 $output .= '</div>';
1154 }
1155 // Author
1156 if ($post['author'] == 1) {
1157 $user_id = get_the_author_meta('ID');
1158 $output .= '<a href="'.get_author_posts_url($user_id).'" class="ultp-searchresult-author">'.get_the_author_meta('display_name').'</a>';
1159 }
1160 // Date
1161 if ($post['date'] == 1) {
1162 $output .= '<div class="ultp-searchresult-publishdate">'.get_the_date('F j, Y').'</div>';
1163 }
1164 $output .= '</div>';
1165 $output .= '<a href="'.get_permalink().'" class="ultp-searchresult-title">'.$title.'</a>';
1166 if ($post['excerpt'] == 1) {
1167 $output .= '<div class="ultp-searchresult-excerpt">'.wp_trim_words(get_the_excerpt(), isset($post['excerptLimit'])?ultimate_post()->ultp_rest_sanitize_params($post['excerptLimit']):55).'</div>';
1168 }
1169 $output .= '</div>';
1170 $output .= '</div>';
1171 }
1172 }
1173
1174 return array('post_data' => $output, 'post_count' => $query_result->found_posts);
1175 }
1176
1177 /**
1178 * PostX Site Dark Logo Init
1179 *
1180 * @since v.3.1.9
1181 * @param ARRAY
1182 * @return BOOOLEAN | Inserted Post Url
1183 */
1184 public function init_site_dark_logo_callback ($server) {
1185 $logo_data = $server->get_params();
1186 $success = true;
1187 if( isset($logo_data['logo']['url'] ) ){
1188 update_option( 'ultp_site_dark_logo', $logo_data['logo']['url'] );
1189 } else {
1190 $success = false;
1191 }
1192 return rest_ensure_response([
1193 'success' => $success,
1194 ]);
1195 }
1196
1197
1198 /**
1199 * Getting Image Size
1200 *
1201 * @since v.4.0.1
1202 * @param ARRAY | Parameter (number)
1203 * @return ARRAY | Image Size List as Array
1204 */
1205 public function get_custom_image_size($server) {
1206 $img = $server->get_params();
1207 $image_src = array();
1208 $image_sizes = ultimate_post()->get_image_size();
1209 foreach ($image_sizes as $key => $value) {
1210 $image_src[$key] = wp_get_attachment_image_src($img['id'], $key, false)[0];
1211 }
1212 return rest_ensure_response( ['success' => true, 'size' => $image_src, 'id' => $img ]);
1213 }
1214 }