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

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