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

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