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

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