PluginProbe
OttoKit: All-in-One Automation Platform / 1.0.60
OttoKit: All-in-One Automation Platform v1.0.60
1.1.38 1.1.37 1.1.36 1.1.35 1.1.34 1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 All 124 releases
suretriggers / src / Controllers / GlobalSearchController.php
GlobalSearchController.php
18,739 lines 637.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * GlobalSearchController.
4 * php version 5.6
5 *
6 * @category GlobalSearchController
7 * @package SureTriggers
8 * @author BSF <username@example.com>
9 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
10 * @link https://www.brainstormforce.com/
11 * @since 1.0.0
12 */
13
14 namespace SureTriggers\Controllers;
15
16 use DOMDocument;
17 use FluentCrm\App\Models\CustomContactField;
18 use FluentCrm\App\Models\Subscriber;
19 use FluentCrm\App\Models\Tag;
20 use FluentCrm\App\Models\SubscriberMeta;
21 use FluentCrm\App\Models\Lists;
22 use memberpress\courses\lib as lib;
23 use memberpress\courses\models as models;
24 use FluentCrm\Framework\Support\Arr;
25 use GFCommon;
26 use GFFormsModel;
27 use Give_Payment;
28 use Give_Subscription;
29 use MeprBaseRealGateway;
30 use MeprOptions;
31 use OsAgentHelper;
32 use OsBookingHelper;
33 use OsCustomerHelper;
34 use OsServiceHelper;
35 use OsWpUserHelper;
36 use PrestoPlayer\Models\Video;
37 use RGFormsModel;
38 use SureTriggers\Integrations\AffiliateWP\AffiliateWP;
39 use SureTriggers\Integrations\EDD\EDD;
40 use SureTriggers\Integrations\FunnelKitAutomations\FunnelKitAutomations;
41 use SureTriggers\Integrations\JetpackCRM\JetpackCRM;
42 use SureTriggers\Integrations\LearnDash\LearnDash;
43 use SureTriggers\Integrations\LifterLMS\LifterLMS;
44 use SureTriggers\Integrations\MemberPress\MemberPress;
45 use SureTriggers\Integrations\MemberPressCourse\MemberPressCourse;
46 use SureTriggers\Integrations\ModernEventsCalendar\ModernEventsCalendar;
47 use SureTriggers\Integrations\PeepSo\PeepSo;
48 use SureTriggers\Integrations\RafflePress\RafflePress;
49 use SureTriggers\Integrations\RestrictContent\RestrictContent;
50 use SureTriggers\Integrations\TheEventCalendar\TheEventCalendar;
51 use SureTriggers\Integrations\WishlistMember\WishlistMember;
52 use SureTriggers\Integrations\WooCommerce\WooCommerce;
53 use SureTriggers\Integrations\WordPress\WordPress;
54 use SureTriggers\Integrations\WpPolls\WpPolls;
55 use SureTriggers\Models\Utilities;
56 use SureTriggers\Traits\SingletonLoader;
57 use Tribe__Tickets__Tickets_Handler;
58 use WC_Subscription;
59 use WC_Subscriptions_Product;
60 use WP_Query;
61 use WP_Comment_Query;
62 use WP_REST_Request;
63 use WP_REST_Response;
64 use WPForms_Form_Handler;
65 use CP_V2_Popups;
66 use Project_Huddle;
67 use FrmForm;
68 use Forminator_API;
69 use SureTriggers\Integrations\LearnPress\LearnPress;
70 use WC_Customer;
71 use WC_Booking;
72 use WC_Bookings_Admin;
73 use MeprTransaction;
74 use WC_Order;
75 use LLMS_Section;
76 use BP_Signup;
77 use WP_Post;
78 use AsgarosForum;
79 use PeepSoUser;
80 use PeepSoField;
81 use Mint\MRM\DataBase\Models\ContactModel;
82 use Mint\MRM\DataBase\Models\ContactGroupModel;
83 use SureTriggers\Integrations\Voxel\Voxel;
84
85 /**
86 * GlobalSearchController- Add ajax related functions here.
87 *
88 * @category GlobalSearchController
89 * @package SureTriggers
90 * @author BSF <username@example.com>
91 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
92 * @link https://www.brainstormforce.com/
93 * @since 1.0.0
94 *
95 * @psalm-suppress UndefinedTrait
96 */
97 class GlobalSearchController {
98
99 use SingletonLoader;
100
101 /**
102 * Search post by post type.
103 *
104 * @param array $data Search Params.
105 *
106 * @since 1.0.0
107 *
108 * @return array
109 */
110 public function search_post( $data ) {
111 $result = [];
112 $posts = Utilities::find_posts_by_title( $data );
113
114 foreach ( $posts['results'] as $post ) {
115 $result[] = [
116 'label' => $post['post_title'],
117 'value' => $post['ID'],
118 ];
119 }
120
121 return [
122 'options' => $result,
123 'hasMore' => $posts['has_more'],
124 ];
125 }
126
127 /**
128 * Search Course.
129 *
130 * @param array $data quesry params.
131 *
132 * @return array
133 * @since 1.0.0
134 */
135 public function search_ld_course( $data ) {
136
137 $page = $data['page'];
138 $limit = Utilities::get_search_page_limit();
139 $offset = $limit * ( $page - 1 );
140
141 $courses = get_posts(
142 [
143
144 'post_type' => 'product',
145 'meta_key' => '_related_course',
146 'post_status' => 'publish',
147 'posts_per_page' => $limit,
148 'offset' => $offset,
149 ]
150 );
151 $options = [];
152 foreach ( $courses as $course ) {
153 $options[] = [
154 'label' => $course->post_title,
155 'value' => $course->ID,
156 ];
157 }
158
159 $all_courses = get_posts(
160 [
161 'post_type' => 'product',
162 'meta_key' => '_related_course',
163 'post_status' => 'publish',
164 'fields' => 'ids',
165 'posts_per_page' => -1,
166 ]
167 );
168 $posts_count = count( $all_courses );
169
170 return [
171 'options' => $options,
172 'hasMore' => $posts_count > $limit && $posts_count > $offset,
173 ];
174 }
175
176 /**
177 * Search achievement by post type.
178 *
179 * @param array $data Search Params.
180 *
181 * @since 1.0.0
182 *
183 * @return array
184 */
185 public function search_achievements( $data ) {
186
187 $page = $data['page'];
188 $limit = Utilities::get_search_page_limit();
189 $offset = $limit * ( $page - 1 );
190
191 $post = get_post( $data['dynamic'] );
192 $slug = $post->post_name;
193
194 $achievements = get_posts(
195 [
196 'post_type' => $slug,
197 'post_status' => 'publish',
198 'posts_per_page' => $limit,
199 'offset' => $offset,
200 ]
201 );
202 $options = [];
203 foreach ( $achievements as $achievement ) {
204 $options[] = [
205 'label' => $achievement->post_title,
206 'value' => (string) $achievement->ID,
207 ];
208 }
209
210 $count = wp_count_posts( $slug )->publish;
211
212 return [
213 'options' => $options,
214 'hasMore' => $count > $limit && $count > $offset,
215 ];
216 }
217
218 /**
219 * Search Course.
220 *
221 * @param array $data quesry params.
222 *
223 * @return array
224 * @since 1.0.0
225 */
226 public function search_tutor_course( $data ) {
227 $courses = get_posts(
228 [
229 'post_type' => tutor()->course_post_type,
230 'post_status' => 'publish',
231 'numberposts' => '-1',
232 ]
233 );
234 $options = [];
235 foreach ( $courses as $course ) {
236 $options[] = [
237 'label' => $course->post_title,
238 'value' => $course->ID,
239 ];
240 }
241
242 return [
243 'options' => $options,
244 'hasMore' => false,
245 ];
246 }
247
248 /**
249 * Search Products.
250 *
251 * @param array $data quesry params.
252 *
253 * @return array
254 * @since 1.0.0
255 */
256 public function search_product( $data ) {
257 $result = [];
258 $posts = Utilities::find_posts_by_title( $data );
259
260 foreach ( $posts['results'] as $post ) {
261 $result[] = [
262 'label' => $post['post_title'],
263 'value' => $post['post_title'],
264 ];
265 }
266
267 return [
268 'options' => $result,
269 'hasMore' => $posts['has_more'],
270 ];
271 }
272
273 /**
274 * Search Product Categories.
275 *
276 * @param array $data Search Params.
277 *
278 * @since 1.0.0
279 */
280 public function search_product_category( $data ) {
281 if ( ! empty( $data['dynamic'] ) ) {
282 $taxonomy = $data['dynamic'];
283 } else {
284 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
285 }
286
287 $term = $data['term'];
288 $result = [];
289 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
290 foreach ( $terms['result'] as $tax_term ) {
291 $result[] = [
292 'label' => $tax_term->name,
293 'value' => $tax_term->name,
294 ];
295 }
296
297 return [
298 'options' => $result,
299 'hasMore' => $terms['has_more'],
300 ];
301 }
302
303 /**
304 * Search Product Tags.
305 *
306 * @param array $data Search Params.
307 *
308 * @since 1.0.0
309 */
310 public function search_product_tags( $data ) {
311 if ( ! empty( $data['dynamic'] ) ) {
312 $taxonomy = $data['dynamic'];
313 } else {
314 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
315 }
316
317 $term = $data['term'];
318 $result = [];
319 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
320
321 foreach ( $terms['result'] as $tax_term ) {
322 $result[] = [
323 'label' => $tax_term->name,
324 'value' => $tax_term->name,
325 ];
326 }
327
328 return [
329 'options' => $result,
330 'hasMore' => $terms['has_more'],
331 ];
332 }
333
334 /**
335 * Global ajax search.
336 * Here you need to add the field action name to work.
337 *
338 * @param WP_REST_Request $request Request data.
339 *
340 * @return WP_REST_Response
341 * @since 1.0.0
342 */
343 public function global_search( $request ) {
344 $post_type = $request->get_param( 'post_type' );
345 $dynamic = $request->get_param( 'dynamic' );
346 $search_term = $request->get_param( 'term' );
347 $identifier = $request->get_param( 'field_identifier' );
348 $page = max( 1, $request->get_param( 'page' ) );
349 $taxonomy = $request->get_param( 'taxonomy' ) ? $request->get_param( 'taxonomy' ) : [];
350
351 $filter = $request->get_param( 'filter' ) ? json_decode( stripslashes( $request->get_param( 'filter' ) ), true ) : [];
352
353 $data = [
354 'dynamic' => $dynamic,
355 'search_term' => $search_term,
356 'page' => $page,
357 'taxonomy' => $taxonomy,
358 'filter' => $filter,
359 'post_type' => $post_type,
360 ];
361 $response = [
362 'hasMore' => false,
363 'options' => [],
364 ];
365
366 $method_name = 'search_' . $identifier;
367
368 if ( method_exists( $this, $method_name ) ) {
369 $response = $this->{$method_name}( $data );
370 } else {
371 return RestController::error_message( 'Invalid field Identifier param.' );
372 }
373
374 return RestController::success_message( $response );
375 }
376
377 /**
378 * Search Taxonomy Terms.
379 *
380 * @param array $data Search Params.
381 *
382 * @since 1.0.0
383 *
384 * @return array
385 */
386 public function search_term( $data ) {
387 if ( ! empty( $data['dynamic'] ) ) {
388 $taxonomy = $data['dynamic'];
389 } else {
390 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
391 }
392
393 $term = $data['term'];
394 $result = [];
395 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
396 foreach ( $terms['result'] as $tax_term ) {
397 $result[] = [
398 'label' => $tax_term->name,
399 'value' => $tax_term->term_id,
400 ];
401 }
402
403 return [
404 'options' => $result,
405 'hasMore' => $terms['has_more'],
406 ];
407 }
408
409 /**
410 * List Taxonomy Terms.
411 *
412 * @param array $data Search Params.
413 *
414 * @since 1.0.0
415 *
416 * @return array
417 */
418 public function search_term_list( $data ) {
419 $taxonomy = $data['dynamic'];
420 $result = [];
421 $terms = Utilities::get_terms( '', $data['page'], $taxonomy );
422 foreach ( $terms['result'] as $tax_term ) {
423 $result[] = [
424 'label' => $tax_term->name,
425 'value' => $tax_term->term_id,
426 ];
427 }
428
429 return [
430 'options' => $result,
431 'hasMore' => $terms['has_more'],
432 ];
433 }
434
435 /**
436 * Search users.
437 *
438 * @param array $data Search Params.
439 *
440 * @since 1.0.0
441 *
442 * @return array
443 */
444 public function search_user( $data ) {
445 $result = [];
446 $page = $data['page'];
447 $users = Utilities::get_users( $data, $page );
448
449 if ( is_array( $users['results'] ) ) {
450 foreach ( $users['results'] as $user ) {
451 $result[] = [
452 'label' => $user->user_login,
453 'value' => $user->ID,
454 ];
455 }
456 }
457
458 return [
459 'options' => $result,
460 'hasMore' => $users['has_more'],
461 ];
462
463 }
464
465 /**
466 * Search WPForm fields.
467 *
468 * @param array $data Search Params.
469 *
470 * @since 1.0.0
471 *
472 * @return array
473 */
474 public function search_pluggable_wpform_fields( $data ) {
475 $result = [];
476 $page = $data['page'];
477 $form_id = absint( $data['dynamic'] );
478 $wpform_fields = Utilities::get_wpform_fields( $data['search_term'], $page, $form_id );
479
480 if ( is_array( $wpform_fields['results'] ) ) {
481 foreach ( $wpform_fields['results'] as $field ) {
482 $result[] = [
483 'label' => $field['label'],
484 'value' => '{' . $field['id'] . '}',
485 ];
486 }
487 }
488
489 return [
490 'options' => $result,
491 'hasMore' => $wpform_fields['has_more'],
492 ];
493 }
494
495 /**
496 * Prepare variable products.
497 *
498 * @param array $data data.
499 *
500 * @return array
501 */
502 public function search_variable_products( $data ) {
503 $products = Utilities::get_variable_products( $data['search_term'], $data['page'] );
504 $options = [];
505
506 foreach ( $products['result'] as $product ) {
507 $options[] = [
508 'label' => $product->get_title(),
509 'value' => (string) $product->get_id(),
510 ];
511 }
512
513 return [
514 'options' => $options,
515 'hasMore' => $products['has_more'],
516 ];
517 }
518
519 /**
520 * Prepare variable products.
521 *
522 * @param array $data data.
523 *
524 * @return array
525 */
526 public function search_product_variations( $data ) {
527 $variations = Utilities::get_product_variations( $data['dynamic'] );
528 $options = [];
529
530 foreach ( $variations['result'] as $product ) {
531 $options[] = [
532 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
533 'value' => (string) $product->ID,
534 ];
535 }
536
537 return [
538 'options' => $options,
539 'hasMore' => $variations['has_more'],
540 ];
541 }
542
543 /**
544 * Search WooCommerce Subscriptions.
545 *
546 * @param array $data Search Params.
547 *
548 * @since 1.0.0
549 *
550 * @return array
551 */
552 public function search_subscription_variation( $data ) {
553 $subscription_products = Utilities::get_subscription_variation( $data['search_term'], $data['page'] );
554 $result = [];
555
556 if ( ! function_exists( 'wc_get_products' ) ) {
557 return $result;
558 }
559
560 foreach ( $subscription_products['result'] as $post ) {
561 if ( $data['search_term'] ) {
562 if ( false !== stripos( $post->get_title(), $data['search_term'] ) ) {
563 $result[] = [
564 'label' => $post->get_title(),
565 'value' => (string) $post->get_id(),
566 ];
567 }
568 } else {
569 $result[] = [
570 'label' => $post->get_title(),
571 'value' => (string) $post->get_id(),
572 ];
573 }
574 }
575
576 return [
577 'options' => $result,
578 'hasMore' => $subscription_products['has_more'],
579 ];
580 }
581
582 /**
583 * Prepare WooCommerce Payment Methods.
584 *
585 * @param array $data Search Params.
586 * @return array[]
587 */
588 public function search_woo_payment_methods( $data ) {
589 $payment_methods = WC()->payment_gateways->get_available_payment_gateways();
590 $options = [];
591
592 if ( ! empty( $payment_methods ) ) {
593 foreach ( $payment_methods as $payment ) {
594 $options[] = [
595 'label' => $payment->title,
596 'value' => $payment->id,
597 ];
598 }
599 }
600
601 return [
602 'options' => $options,
603 'hasMore' => false,
604 ];
605 }
606
607 /**
608 * Prepare WooCommerce Order Status List.
609 *
610 * @param array $data Search Params.
611 * @return array[]
612 */
613 public function search_woo_order_status_list( $data ) {
614 $order_status = wc_get_order_statuses();
615 $options = [];
616
617 if ( ! empty( $order_status ) ) {
618 foreach ( $order_status as $key => $status ) {
619 $options[] = [
620 'label' => $status,
621 'value' => $key,
622 ];
623 }
624 }
625
626 return [
627 'options' => $options,
628 'hasMore' => false,
629 ];
630 }
631
632 /**
633 * Prepare WooCommerce Country List.
634 *
635 * @param array $data Search Params.
636 * @return array[]
637 */
638 public function search_woo_country_list( $data ) {
639 $countries = WC()->countries->get_countries();
640 $options = [];
641
642 if ( ! empty( $countries ) ) {
643 foreach ( $countries as $key => $country ) {
644 $options[] = [
645 'label' => $country,
646 'value' => $key,
647 ];
648 }
649 }
650
651 return [
652 'options' => $options,
653 'hasMore' => false,
654 ];
655 }
656
657 /**
658 * Prepare WooCommerce Country States List.
659 *
660 * @param array $data Search Params.
661 * @return array[]
662 */
663 public function search_woo_country_state_list( $data ) {
664 if ( ! empty( $data['dynamic']['shipping_country'] ) ) {
665 $cc = $data['dynamic']['shipping_country'];
666 } else {
667 $cc = $data['dynamic'];
668 }
669
670 $states = WC()->countries->get_states( $cc );
671
672 $options = [];
673 if ( ! empty( $states ) ) {
674 foreach ( $states as $key => $state ) {
675 $options[] = [
676 'label' => $state,
677 'value' => $key,
678 ];
679 }
680 }
681
682 return [
683 'options' => $options,
684 'hasMore' => false,
685 ];
686 }
687
688 /**
689 * Get Memberpress gatways (payment methods) for subscription.
690 *
691 * @param array $data QueryParams.
692 *
693 * @return array
694 * @since 1.0.0
695 */
696 public function search_memberpress_gayways( $data ) {
697 $mp_options = MeprOptions::fetch();
698
699 $pms = array_keys( $mp_options->integrations );
700 $gateways = [];
701
702 foreach ( $pms as $pm_id ) {
703 $obj = $mp_options->payment_method( $pm_id );
704 if ( $obj instanceof MeprBaseRealGateway ) {
705 $gateways[] = [
706 'label' => sprintf( '%1$s (%2$s)', $obj->label, $obj->name ),
707 'value' => $obj->id,
708 ];
709 }
710 }
711
712 return [
713 'options' => $gateways,
714 'hasMore' => false,
715 ];
716 }
717
718 /**
719 * Prepare roles.
720 *
721 * @param array $data data.
722 *
723 * @return array
724 */
725 public function search_roles( $data ) {
726 $roles = wp_roles()->roles;
727 $options = [];
728 foreach ( $roles as $role => $details ) {
729
730 $options[] = [
731 'label' => $details['name'],
732 'value' => $role,
733 ];
734
735 }
736
737 return [
738 'options' => $options,
739 'hasMore' => false,
740 ];
741 }
742
743 /**
744 * Prepare post types.
745 *
746 * @param array $data data.
747 *
748 * @return array
749 */
750 public function search_post_types( $data ) {
751 $post_types = get_post_types( [ 'public' => true ], 'object' );
752 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
753 if ( isset( $post_types['attachment'] ) ) {
754 unset( $post_types['attachment'] );
755 }
756
757 $options = [];
758 foreach ( $post_types as $post_type => $details ) {
759 $options[] = [
760 'label' => $details->label,
761 'value' => $post_type,
762 ];
763 }
764
765 return [
766 'options' => $options,
767 'hasMore' => false,
768 ];
769 }
770
771 /**
772 * Get post statuses.
773 *
774 * @param array $data data.
775 *
776 * @return array
777 */
778 public function search_post_statuses( $data ) {
779 $post_statuses = get_post_stati( [], 'objects' );
780 $post_statuses = apply_filters( 'suretriggers_post_types', $post_statuses );
781 $options = [];
782
783 foreach ( $post_statuses as $post_status => $details ) {
784 if ( 'woocommerce' === $details->label_count['domain'] ) {
785 $options[] = [
786 'label' => 'WooCommerce - ' . $details->label,
787 'value' => $post_status,
788 ];
789 } else {
790 $options[] = [
791 'label' => $details->label,
792 'value' => $post_status,
793 ];
794 }
795 }
796
797 return [
798 'options' => $options,
799 'hasMore' => false,
800 ];
801 }
802
803 /**
804 * Get Taxonomies.
805 *
806 * @param array $data data.
807 *
808 * @return array
809 */
810 public function search_taxonomy_list( $data ) {
811 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
812 $options = [];
813 $options[0] = [
814 'label' => 'Any Taxonomy',
815 'value' => -1,
816 ];
817
818 foreach ( $taxonomies as $taxonomy => $taxonomy_obj ) {
819 $options[] = [
820 'label' => $taxonomy_obj->label,
821 'value' => $taxonomy_obj->name,
822 ];
823 }
824
825 return [
826 'options' => $options,
827 'hasMore' => false,
828 ];
829 }
830
831 /**
832 * Get Taxonomies without any option.
833 *
834 * @param array $data data.
835 *
836 * @return array
837 */
838 public function search_taxonomy_list_without_any( $data ) {
839 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
840 $options = [];
841 foreach ( $taxonomies as $taxonomy => $taxonomy_obj ) {
842 $options[] = [
843 'label' => $taxonomy_obj->label,
844 'value' => $taxonomy_obj->name,
845 ];
846 }
847
848 return [
849 'options' => $options,
850 'hasMore' => false,
851 ];
852 }
853
854 /**
855 * Get WPForms.
856 *
857 * @param array $data data.
858 *
859 * @return array
860 */
861 public function search_wp_forms( $data ) {
862 if ( ! class_exists( 'WPForms_Form_Handler' ) ) {
863 return;
864 }
865
866 $wpforms = new WPForms_Form_Handler();
867 $forms = $wpforms->get( '', [ 'orderby' => 'title' ] );
868 $options = [];
869
870 if ( ! empty( $forms ) ) {
871 foreach ( $forms as $form ) {
872 $options[] = [
873 'label' => $form->post_title,
874 'value' => $form->ID,
875 ];
876 }
877 }
878
879 return [
880 'options' => $options,
881 'hasMore' => false,
882 ];
883 }
884
885 /**
886 * Get Gravity Forms.
887 *
888 * @param array $data data.
889 *
890 * @return array
891 */
892 public function search_gravity_forms( $data ) {
893 if ( ! class_exists( 'GFFormsModel' ) ) {
894 return;
895 }
896
897 $forms = GFFormsModel::get_forms();
898 $options = [];
899
900 if ( ! empty( $forms ) ) {
901 foreach ( $forms as $form ) {
902 $options[] = [
903 'label' => $form->title,
904 'value' => $form->id,
905 ];
906 }
907 }
908
909 return [
910 'options' => $options,
911 'hasMore' => false,
912 ];
913 }
914
915 /**
916 * Get tag & contact details.
917 *
918 * @param array $data data.
919 *
920 * @return array
921 */
922 public function search_pluggables_fluentcrm_contact_added_to_tags( $data ) {
923 $context = [];
924 $pluggable_data = [];
925 $tag_id = $data['filter'];
926
927 if ( ! class_exists( 'FluentCrm\App\Models\Subscriber' ) || ! class_exists( 'FluentCrm\App\Models\Tag' ) ) {
928 return [];
929 }
930
931 if ( $tag_id > 0 ) {
932 $tags = Tag::where( 'id', $tag_id )->first();
933 } else {
934 $tags = Tag::orderBy( 'id', 'DESC' )->first();
935 }
936 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
937 if ( $contact ) {
938 $pluggable_data['contact'] = $contact;
939 $context['tag_id'] = $tag_id;
940 $pluggable_data['tag'] = $tags;
941 $context['response_type'] = 'live';
942 } else {
943 $pluggable_data['conatct']['full_name'] = 'Test User';
944 $pluggable_data['conatct']['first_name'] = 'Test';
945 $pluggable_data['conatct']['last_name'] = 'User';
946 $pluggable_data['conatct']['company_id'] = 112;
947 $pluggable_data['conatct']['email'] = 'testuser@gmail.com';
948 $pluggable_data['conatct']['address_line_1'] = '33, Vincent Road';
949 $pluggable_data['conatct']['address_line_2'] = 'Chicago Street';
950 $pluggable_data['conatct']['postal_code'] = '212342';
951 $pluggable_data['conatct']['city'] = 'New York City';
952 $pluggable_data['conatct']['state'] = 'New York';
953 $pluggable_data['conatct']['country'] = 'USA';
954 $pluggable_data['conatct']['phone'] = '9992191911';
955 $pluggable_data['conatct']['status'] = 'subscribed';
956 $pluggable_data['conatct']['contact_type'] = 'lead';
957 $pluggable_data['conatct']['source'] = '';
958 $pluggable_data['conatct']['date_of_birth'] = '2022-11-09';
959 $context['tag_id'] = 1;
960 $pluggable_data['tag'] =
961 [
962 'id' => '1',
963 'title' => 'new',
964 'slug' => 'new',
965 'description' => null,
966 'created_at' => '2023-01-19 10:23:23',
967 'updated_at' => '2023-01-19 10:23:23',
968 'pivot' => [
969 'subscriber_id' => '1',
970 'object_id' => '1',
971 'object_type' => 'FluentCrm\\App\\Models\\Tag',
972 'created_at' => '2023-01-19 10:42:55',
973 'updated_at' => '2023-01-19 10:42:55',
974
975 ],
976 ];
977 $context['response_type'] = 'sample';
978 }
979
980 $context['pluggable_data'] = $pluggable_data;
981 return $context;
982 }
983
984 /**
985 * Get FluentCRM contact details.
986 *
987 * @param array $data data.
988 *
989 * @return array
990 */
991 public function search_pluggables_fluentcrm_contact_added( $data ) {
992 $context = [];
993 $pluggable_data = [];
994
995 if ( ! class_exists( 'FluentCrm\App\Models\Subscriber' ) || ! class_exists( 'FluentCrm\App\Models\SubscriberMeta' ) ) {
996 return [];
997 }
998 $contact = [];
999 if ( 'status_set_to_specific_status' === $data['search_term'] ) {
1000 if ( '-1' === $data['filter']['status']['value'] ) {
1001 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1002 } else {
1003 $contact = Subscriber::where( 'status', $data['filter']['status']['value'] )->first();
1004 }
1005 } elseif ( 'new_contact_added' === $data['search_term'] ) {
1006 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1007 } elseif ( 'contact_updated' === $data['search_term'] ) {
1008 $contact = Subscriber::orderBy( 'updated_at', 'DESC' )->first();
1009 } elseif ( 'contact_field_updated' === $data['search_term'] ) {
1010 if ( '-1' === $data['filter']['field_id']['value'] ) {
1011 $contact = SubscriberMeta::where( 'object_type', 'custom_field' )->orderBy( 'updated_at', 'DESC' )->first();
1012 } else {
1013 $contact = SubscriberMeta::where( 'key', $data['filter']['field_id']['value'] )->orderBy( 'updated_at', 'DESC' )->first();
1014 }
1015 $contact = Subscriber::where( 'id', $contact->subscriber_id )->first();
1016 }
1017
1018 if ( $contact ) {
1019 $subscriber = Subscriber::with( [ 'tags', 'lists' ] )->find( $contact->id );
1020 $customer_fields = $subscriber->custom_fields();
1021 $pluggable_data['contact']['details'] = $subscriber;
1022 $pluggable_data['contact']['custom'] = $customer_fields;
1023 $pluggable_data['field_id'] = $data['filter']['field_id']['value'];
1024 $context['response_type'] = 'live';
1025 } else {
1026 $pluggable_data['contact']['details']['full_name'] = 'Test User';
1027 $pluggable_data['contact']['details']['first_name'] = 'Test';
1028 $pluggable_data['contact']['details']['last_name'] = 'User';
1029 $pluggable_data['contact']['details']['company_id'] = 112;
1030 $pluggable_data['contact']['details']['email'] = 'testuser@gmail.com';
1031 $pluggable_data['contact']['details']['address_line_1'] = '33, Vincent Road';
1032 $pluggable_data['contact']['details']['address_line_2'] = 'Chicago Street';
1033 $pluggable_data['contact']['details']['postal_code'] = '212342';
1034 $pluggable_data['contact']['details']['city'] = 'New York City';
1035 $pluggable_data['contact']['details']['state'] = 'New York';
1036 $pluggable_data['contact']['details']['country'] = 'USA';
1037 $pluggable_data['contact']['details']['phone'] = '9992191911';
1038 $pluggable_data['contact']['details']['status'] = 'subscribed';
1039 $pluggable_data['contact']['details']['contact_type'] = 'lead';
1040 $pluggable_data['contact']['details']['source'] = '';
1041 $pluggable_data['contact']['details']['date_of_birth'] = '2022-11-09';
1042 $context['response_type'] = 'sample';
1043 }
1044
1045 $context['pluggable_data'] = $pluggable_data;
1046 return $context;
1047 }
1048
1049 /**
1050 * Get contact added to list details.
1051 *
1052 * @param array $data data.
1053 *
1054 * @return array
1055 */
1056 public function search_pluggables_fluentcrm_contact_added_to_lists( $data ) {
1057 $context = [];
1058 $pluggable_data = [];
1059 $list_id = $data['filter'];
1060
1061 if ( ! class_exists( 'FluentCrm\App\Models\Lists' ) || ! class_exists( 'FluentCrm\App\Models\Subscriber' ) ) {
1062 return [];
1063 }
1064
1065 $contact_api = FluentCrmApi( 'contacts' );
1066 if ( -1 === $list_id ) {
1067 $lists = Lists::orderBy( 'id', 'DESC' )->first();
1068 $list_id = $lists->id;
1069 } else {
1070 $lists = Lists::where( 'id', $list_id )->first();
1071 }
1072 $list_ids = [ $list_id ];
1073 $list_contacts = $contact_api->getInstance()
1074 ->filterByLists( $list_ids )
1075 ->orderBy( 'id', 'DESC' )
1076 ->first();
1077
1078 $contact = Subscriber::where( 'id', $list_contacts->id )->get();
1079 if ( $contact ) {
1080 $pluggable_data['contact'] = $contact[0];
1081 $pluggable_data['list_id'] = $list_id;
1082 $pluggable_data['list'] = $lists;
1083 $context['response_type'] = 'live';
1084 } else {
1085 $pluggable_data['conatct']['id'] = 6;
1086 $pluggable_data['conatct']['prefix'] = 'Mr';
1087 $pluggable_data['conatct']['full_name'] = 'John Doe';
1088 $pluggable_data['conatct']['first_name'] = 'John';
1089 $pluggable_data['conatct']['last_name'] = 'Doe';
1090 $pluggable_data['conatct']['company_id'] = 112;
1091 $pluggable_data['conatct']['email'] = 'johnde@gmail.com';
1092 $pluggable_data['conatct']['address_line_1'] = '33, Vincent Road';
1093 $pluggable_data['conatct']['address_line_2'] = 'Chicago Street';
1094 $pluggable_data['conatct']['postal_code'] = '212342';
1095 $pluggable_data['conatct']['city'] = 'New York City';
1096 $pluggable_data['conatct']['state'] = 'New York';
1097 $pluggable_data['conatct']['country'] = 'USA';
1098 $pluggable_data['conatct']['phone'] = '9992191911';
1099 $pluggable_data['conatct']['status'] = 'subscribed';
1100 $pluggable_data['conatct']['contact_type'] = 'lead';
1101 $pluggable_data['conatct']['source'] = '';
1102 $pluggable_data['conatct']['date_of_birth'] = '2022-11-09';
1103 $context['list_id'] = 1;
1104 $pluggable_data['list'] =
1105 [
1106 'id' => '1',
1107 'title' => 'new',
1108 'slug' => 'new',
1109 'description' => null,
1110 'created_at' => '2023-01-19 10:23:23',
1111 'updated_at' => '2023-01-19 10:23:23',
1112 ];
1113 $context['response_type'] = 'sample';
1114 }
1115
1116 $context['pluggable_data'] = $pluggable_data;
1117 return $context;
1118 }
1119
1120 /**
1121 * Prepare fluentcrm campaigns.
1122 *
1123 * @param array $data Search Params.
1124 *
1125 * @return array
1126 */
1127 public function search_fluentcrm_campaigns( $data ) {
1128
1129 $options = [];
1130 global $wpdb;
1131
1132 $campaigns = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fc_campaigns ORDER BY id DESC", ARRAY_A );
1133
1134 if ( ! empty( $campaigns ) ) {
1135 foreach ( $campaigns as $campaign ) {
1136 $options[] = [
1137 'label' => $campaign['title'],
1138 'value' => $campaign['id'],
1139 ];
1140 }
1141 }
1142
1143 return [
1144 'options' => $options,
1145 'hasMore' => false,
1146 ];
1147 }
1148
1149 /**
1150 * Get Divi Forms.
1151 *
1152 * @param array $data data.
1153 *
1154 * @return array
1155 */
1156 public function search_divi_forms( $data ) {
1157 $form_posts = Utilities::get_divi_forms();
1158 $options = [];
1159
1160 if ( empty( $form_posts ) ) {
1161 return [
1162 'options' => [],
1163 'hasMore' => false,
1164 ];
1165 }
1166
1167 foreach ( $form_posts as $form_post ) {
1168 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
1169 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
1170 if ( empty( $forms ) ) {
1171 continue;
1172 }
1173
1174 $count = 0;
1175
1176 foreach ( $forms as $form ) {
1177 $pattern_form = get_shortcode_regex( [ 'et_pb_contact_form' ] );
1178 preg_match_all( "/$pattern_form/", $form[0], $forms_extracted, PREG_SET_ORDER );
1179
1180 if ( empty( $forms_extracted ) ) {
1181 continue;
1182 }
1183
1184 foreach ( $forms_extracted as $form_extracted ) {
1185 $form_attrs = shortcode_parse_atts( $form_extracted[3] );
1186 $form_id = isset( $form_attrs['_unique_id'] ) ? $form_attrs['_unique_id'] : '';
1187 if ( empty( $form_id ) ) {
1188 continue;
1189 }
1190 $form_id = sprintf( '%d-%s', $form_post['ID'], $form_id );
1191 $form_title = isset( $form_attrs['title'] ) ? $form_attrs['title'] : '';
1192 $form_title = sprintf( '%s %s', $form_post['post_title'], $form_title );
1193 $options[] = [
1194 'label' => $form_title,
1195 'value' => $form_id,
1196 ];
1197 }
1198 $count++;
1199 }
1200 }
1201
1202 return [
1203 'options' => $options,
1204 'hasMore' => false,
1205 ];
1206 }
1207
1208 /**
1209 * Get Comment Pluggable data.
1210 *
1211 * @param array $data data.
1212 *
1213 * @return array
1214 */
1215 public function search_pluggables_wp_insert_comment( $data ) {
1216 $context = [];
1217 $post_data = [];
1218 $args = [
1219 'number' => '1',
1220 'status' => 'approve',
1221 'post_type' => $data['filter']['post_type']['value'],
1222 ];
1223
1224 if ( isset( $data['filter']['post']['value'] ) ) {
1225 $post_id = $data['filter']['post']['value'];
1226 if ( $post_id > 0 ) {
1227 $args['post_id'] = $post_id;
1228 }
1229 }
1230
1231 $comments = get_comments( $args );
1232 if ( empty( $comments ) ) {
1233 unset( $args['post_id'] );
1234 $comments = get_comments( $args );
1235 }
1236 $context['context_data'] = $data;
1237 $context['context_args'] = $args;
1238 if ( ! empty( $comments ) ) {
1239 foreach ( $comments as $comment ) :
1240 if ( is_object( $comment ) ) {
1241 $comment = get_object_vars( $comment );
1242 }
1243 if ( is_array( $comment ) && isset( $comment['comment_post_ID'] ) ) {
1244 $post = get_post( absint( $comment['comment_post_ID'] ) );
1245 if ( is_object( $post ) ) {
1246 if ( property_exists( $post, 'ID' ) || property_exists( $post, 'post_author' ) || property_exists( $post, 'post_title' ) ) {
1247 $post_id = $post->ID;
1248 $postauthor = (int) $post->post_author;
1249 if ( is_array( $comment ) ) {
1250 $context['pluggable_data'] = [
1251 'post' => $post_id,
1252 'post_title' => $post->post_title,
1253 'post_author' => get_the_author_meta( 'display_name', $postauthor ),
1254 'post_link' => get_the_permalink( $post_id ),
1255 'comment_id' => $comment['comment_ID'],
1256 'comment' => $comment['comment_content'],
1257 'comment_author' => $comment['comment_author'],
1258 'comment_author_email' => $comment['comment_author_email'],
1259 'comment_date' => $comment['comment_date'],
1260 ];
1261 }
1262 }
1263 }
1264 }
1265 if ( is_array( $comment ) && isset( $comment['comment_author_email'] ) ) {
1266 $user_email = $comment['comment_author_email'];
1267 /**
1268 *
1269 * Ignore line
1270 *
1271 * @phpstan-ignore-next-line
1272 */
1273 $user = get_user_by( 'email', $user_email );
1274 if ( $user ) {
1275 $context['pluggable_data']['wp_user_id'] = $user->ID;
1276 $context['pluggable_data']['user_login'] = $user->user_login;
1277 $context['pluggable_data']['display_name'] = $user->display_name;
1278 $context['pluggable_data']['user_firstname'] = $user->user_firstname;
1279 $context['pluggable_data']['user_lastname'] = $user->user_lastname;
1280 $context['pluggable_data']['user_email'] = $user->user_email;
1281 $context['pluggable_data']['user_role'] = $user->roles;
1282 }
1283 }
1284
1285 $context['response_type'] = 'live';
1286 endforeach;
1287 } else {
1288 $sample_comment = [
1289 'post' => 100,
1290 'post_title' => 'Sample Post',
1291 'comment_id' => 101,
1292 'comment' => 'Sample Comment',
1293 ];
1294 $sample_comment['wp_user_id'] = 7;
1295 $sample_comment['user_login'] = 'testuser@gmail.com';
1296 $sample_comment['display_name'] = 'Test User';
1297 $sample_comment['user_firstname'] = 'Test';
1298 $sample_comment['user_lastname'] = 'User';
1299 $sample_comment['user_email'] = 'testuser@gmail.com';
1300 $sample_comment['user_role'] = [ 'Subscriber' ];
1301
1302 $context['pluggable_data'] = $sample_comment;
1303 $context['response_type'] = 'sample';
1304 }
1305 return $context;
1306 }
1307
1308 /**
1309 * User reset password.
1310 *
1311 * @param array $data data.
1312 * @return array
1313 */
1314 public function search_pluggables_user_reset_password( $data ) {
1315 $user_context = $this->search_pluggables_add_user_role( $data );
1316 $user_context['pluggable_data']['new_password'] = '***password***';
1317 return $user_context;
1318 }
1319
1320 /**
1321 * User pluggable data.
1322 *
1323 * @param array $data data.
1324 * @return array
1325 */
1326 public function search_pluggables_add_user_role( $data ) {
1327 $context = [];
1328 $args = [
1329 'order' => 'DESC',
1330 'number' => 1,
1331 'orderby' => 'ID',
1332 ];
1333
1334 if ( isset( $data['filter']['role']['value'] ) ) {
1335 $role = $data['filter']['role']['value'];
1336 $args['role'] = $role;
1337 }
1338 if ( isset( $data['filter']['new_role']['value'] ) ) {
1339 $role = $data['filter']['new_role']['value'];
1340 $args['role'] = $role;
1341 }
1342
1343 $users = get_users( $args );
1344
1345 if ( isset( $data['filter']['meta_key']['value'] ) ) {
1346 $meta_key = $data['filter']['meta_key']['value'];
1347 $args['st_meta_key'] = $meta_key;
1348 }
1349
1350 if ( isset( $data['filter']['profile_field']['value'] ) ) {
1351 $meta_key = $data['filter']['profile_field']['value'];
1352 $args['profile_field'] = $meta_key;
1353 }
1354
1355 if ( ! empty( $users ) ) {
1356 $user = $users[0];
1357 $pluggable_data = [];
1358 $pluggable_data['wp_user_id'] = $user->ID;
1359 $pluggable_data['user_login'] = $user->user_login;
1360 $pluggable_data['display_name'] = $user->display_name;
1361 $pluggable_data['user_firstname'] = $user->user_firstname;
1362 $pluggable_data['user_lastname'] = $user->user_lastname;
1363 $pluggable_data['user_email'] = $user->user_email;
1364 $pluggable_data['user_registered'] = $user->user_registered;
1365 $pluggable_data['user_role'] = $user->roles;
1366 if ( isset( $args['st_meta_key'] ) ) {
1367 $pluggable_data['meta_key'] = $args['st_meta_key'];
1368 $pluggable_data['meta_value'] = get_user_meta( $user->ID, $args['st_meta_key'], true );
1369 }
1370 if ( isset( $args['profile_field'] ) ) {
1371 $userdata = get_userdata( $user->ID );
1372 $userdata = json_decode( wp_json_encode( $userdata->data ), true );
1373
1374 $pluggable_data['profile_field'] = $args['profile_field'];
1375 $pluggable_data['profile_field_value'] = $userdata[ $args['profile_field'] ];
1376 }
1377 $context['pluggable_data'] = $pluggable_data;
1378 $context['response_type'] = 'live';
1379 } else {
1380 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
1381 $context['pluggable_data'] = [
1382 'wp_user_id' => 1,
1383 'user_login' => 'admin',
1384 'display_name' => 'Test User',
1385 'user_firstname' => 'Test',
1386 'user_lastname' => 'User',
1387 'user_email' => 'testuser@gmail.com',
1388 'user_registered' => '2024-06-18 09:47:58',
1389 'user_role' => [ $role ],
1390 ];
1391 if ( isset( $args['st_meta_key'] ) ) {
1392 $context['pluggable_data']['meta_key'] = $args['st_meta_key'];
1393 $context['pluggable_data']['meta_value'] = 'test meta value';
1394 }
1395 if ( isset( $args['profile_field'] ) ) {
1396 $context['pluggable_data']['profile_field'] = $args['profile_field'];
1397 $context['pluggable_data']['profile_field_value'] = 'Profile Field Value';
1398 }
1399 $context['response_type'] = 'sample';
1400 }
1401 return $context;
1402 }
1403
1404 /**
1405 * User pluggable data.
1406 *
1407 * @param array $data data.
1408 * @return array
1409 */
1410 public function search_pluggables_last_user_login( $data ) {
1411 $context = [];
1412 $args = [
1413 'orderby' => 'meta_value',
1414 'meta_key' => 'st_last_login',
1415 'order' => 'DESC',
1416 'number' => 1,
1417 ];
1418 $users = get_users( $args );
1419
1420 if ( ! empty( $users ) ) {
1421 $user = $users[0];
1422 $pluggable_data = [];
1423 $pluggable_data['wp_user_id'] = $user->ID;
1424 $pluggable_data['user_login'] = $user->user_login;
1425 $pluggable_data['display_name'] = $user->display_name;
1426 $pluggable_data['user_firstname'] = $user->user_firstname;
1427 $pluggable_data['user_lastname'] = $user->user_lastname;
1428 $pluggable_data['user_email'] = $user->user_email;
1429 $pluggable_data['user_role'] = $user->roles;
1430
1431 $context['pluggable_data'] = $pluggable_data;
1432 $context['response_type'] = 'live';
1433 } else {
1434 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
1435 $context['pluggable_data'] = [
1436 'wp_user_id' => 1,
1437 'user_login' => 'admin',
1438 'display_name' => 'Test User',
1439 'user_firstname' => 'Test',
1440 'user_lastname' => 'User',
1441 'user_email' => 'testuser@gmail.com',
1442 'user_role' => [ $role ],
1443 ];
1444 $context['response_type'] = 'sample';
1445 }
1446 return $context;
1447 }
1448
1449 /**
1450 * Donation pluggable data.
1451 *
1452 * @param array $data data.
1453 * @return array
1454 */
1455 public function search_pluggables_wordpress_post( $data ) {
1456 $context = [];
1457 $args = [
1458 'post_type' => 'any',
1459 'posts_per_page' => 1,
1460 'orderby' => 'modified',
1461 'order' => 'DESC',
1462 ];
1463
1464 if ( isset( $data['filter']['post_type']['value'] ) ) {
1465 $post_type = $data['filter']['post_type']['value'];
1466 $args['post_type'] = $post_type;
1467 }
1468
1469 if ( isset( $data['filter']['status']['value'] ) ) {
1470 $post_status = $data['filter']['status']['value'];
1471 $args['post_status'] = $post_status;
1472 }
1473
1474 if ( isset( $data['filter']['post_status']['value'] ) ) {
1475 $post_status = $data['filter']['post_status']['value'];
1476 $args['post_status'] = $post_status;
1477 }
1478
1479 if ( isset( $data['filter']['post']['value'] ) ) {
1480 $post_id = $data['filter']['post']['value'];
1481 if ( $post_id > 0 ) {
1482 $args['p'] = $post_id;
1483 unset( $args['post_status'] );
1484 }
1485 }
1486
1487 $posts = get_posts( $args );
1488 if ( ! empty( $posts ) ) {
1489 $context['pluggable_data'] = $posts[0];
1490 $custom_metas = get_post_meta( $posts[0]->ID );
1491 if ( property_exists( $context['pluggable_data'], 'post' ) ) {
1492 $context['pluggable_data']->post = $posts[0]->ID;
1493 }
1494 if ( is_object( $context['pluggable_data'] ) ) {
1495 $context['pluggable_data'] = get_object_vars( $context['pluggable_data'] );
1496 }
1497 if ( $posts[0] instanceof WP_Post ) {
1498 $taxonomies = get_object_taxonomies( get_post( $posts[0] ), 'objects' );
1499 if ( ! empty( $taxonomies ) ) {
1500 foreach ( $taxonomies as $taxonomy => $taxonomy_object ) {
1501 $terms = get_the_terms( $posts[0]->ID, $taxonomy );
1502 if ( ! empty( $terms ) && is_array( $terms ) ) {
1503 foreach ( $terms as $term ) {
1504 $context['pluggable_data'][ $taxonomy ] = $term->name;
1505 }
1506 }
1507 }
1508 }
1509 }
1510 $context['pluggable_data'] = array_merge( $context['pluggable_data'], WordPress::get_user_context( $posts[0]->post_author ) );
1511 $context['pluggable_data']['post'] = $posts[0]->ID;
1512 $context['pluggable_data']['custom_metas'] = $custom_metas;
1513 $context['response_type'] = 'live';
1514 } else {
1515 $context['pluggable_data'] = [
1516 'ID' => 557,
1517 'post' => 557,
1518 'post_author' => 1,
1519 'post_date' => '2022-11-18 12:18:14',
1520 'post_date_gmt' => '2022-11-18 12:18:14',
1521 'post_content' => 'Test Post Content',
1522 'post_title' => 'Test Post',
1523 'post_excerpt' => '',
1524 'post_status' => $args['post_status'],
1525 'comment_status' => 'open',
1526 'ping_status' => 'open',
1527 'post_password' => '',
1528 'post_name' => 'test-post',
1529 'to_ping' => '',
1530 'pinged' => '',
1531 'post_modified' => '2022-11-18 12:18:14',
1532 'post_modified_gmt' => '2022-11-18 12:18:14',
1533 'post_content_filtered' => '',
1534 'post_parent' => 0,
1535 'guid' => 'https://abc.com/test-post/',
1536 'menu_order' => 0,
1537 'post_type' => 'post',
1538 'post_mime_type' => '',
1539 'comment_count' => 0,
1540 'filter' => 'raw',
1541 ];
1542 $context['response_type'] = 'sample';
1543 }
1544
1545 return $context;
1546 }
1547
1548 /**
1549 * Donation pluggable data
1550 *
1551 * @param array $data data.
1552 * @return array
1553 */
1554 public function search_pluggables_givewp_donation_via_form( $data ) {
1555 global $wpdb;
1556 $context = [];
1557 $pluggable_data = [];
1558 $form_id = $data['filter']['form_id']['value'];
1559 $subscription_id = 1;
1560
1561 if ( ! class_exists( 'Give_Payment' ) || ! class_exists( 'Give_Subscription' ) ) {
1562 return [];
1563 }
1564
1565 if ( ! function_exists( 'give_get_donor_donation_comment' ) ) {
1566 return [];
1567 }
1568
1569 if ( 'donation_specific_field_value' == $data['search_term'] ) {
1570 $field_id = $data['filter']['field_id']['value'];
1571 $field_value = $data['filter']['field_value']['value'];
1572
1573 $donation_meta = $wpdb->get_row(
1574 $wpdb->prepare(
1575 "SELECT * FROM {$wpdb->prefix}give_donationmeta
1576 WHERE meta_key LIKE %s AND
1577 meta_value LIKE %s
1578 ORDER BY donation_id
1579 DESC LIMIT 1",
1580 $field_id,
1581 $field_value
1582 )
1583 );
1584 if ( $donation_meta ) {
1585 $donation_form_meta = $wpdb->get_row(
1586 $wpdb->prepare(
1587 "SELECT * FROM {$wpdb->prefix}give_donationmeta
1588 WHERE donation_id = %d AND
1589 meta_key LIKE %s AND
1590 meta_value LIKE %d
1591 ORDER BY donation_id
1592 DESC LIMIT 1",
1593 $donation_meta->donation_id,
1594 '_give_payment_form_id',
1595 $form_id
1596 )
1597 );
1598
1599 $payment = $wpdb->get_row(
1600 $wpdb->prepare(
1601 "SELECT * FROM {$wpdb->prefix}posts WHERE
1602 ID=%d ORDER BY ID DESC LIMIT 1",
1603 $donation_form_meta->donation_id
1604 )
1605 );
1606 }
1607 } elseif ( 'donation_specific_amount' == $data['search_term'] ) {
1608 $condition_compare = $data['filter']['condition_compare']['value'];
1609 $donation_amount = $data['filter']['donation_amount']['value'];
1610
1611 $donation_meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_donationmeta WHERE meta_key LIKE %s AND meta_value $condition_compare %d ORDER BY donation_id DESC LIMIT 1", '_give_payment_total',$donation_amount ) ); //phpcs:ignore
1612
1613 if ( $donation_meta ) {
1614 $donation_form_meta = $wpdb->get_row(
1615 $wpdb->prepare(
1616 "SELECT * FROM {$wpdb->prefix}give_donationmeta
1617 WHERE donation_id = %d AND
1618 meta_key LIKE %s AND
1619 meta_value LIKE %d
1620 ORDER BY donation_id
1621 DESC LIMIT 1",
1622 $donation_meta->donation_id,
1623 '_give_payment_form_id',
1624 $form_id
1625 )
1626 );
1627
1628 $payment = $wpdb->get_row(
1629 $wpdb->prepare(
1630 "SELECT * FROM {$wpdb->prefix}posts WHERE
1631 ID=%d ORDER BY ID DESC LIMIT 1",
1632 $donation_form_meta->donation_id
1633 )
1634 );
1635 }
1636 } elseif ( 'cancels_recurring_donation' == $data['search_term'] ) {
1637 $donation_meta = $wpdb->get_row(
1638 $wpdb->prepare(
1639 "SELECT * FROM {$wpdb->prefix}give_subscriptions
1640 WHERE product_id=%d AND
1641 status LIKE %s
1642 ORDER BY id
1643 DESC LIMIT 1",
1644 $form_id,
1645 'cancelled'
1646 )
1647 );
1648 if ( $donation_meta ) {
1649 $subscription_id = $donation_meta->id;
1650 $payment = $wpdb->get_row(
1651 $wpdb->prepare(
1652 "SELECT * FROM {$wpdb->prefix}posts WHERE
1653 ID=%d ORDER BY ID DESC LIMIT 1",
1654 $donation_meta->parent_payment_id
1655 )
1656 );
1657 }
1658 } elseif ( 'continues_recurring_donation' == $data['search_term'] ) {
1659 $donation_meta = $wpdb->get_row(
1660 $wpdb->prepare(
1661 "SELECT * FROM {$wpdb->prefix}give_subscriptions
1662 WHERE product_id=%d AND
1663 status LIKE %s
1664 ORDER BY id
1665 DESC LIMIT 1",
1666 $form_id,
1667 'active'
1668 )
1669 );
1670 if ( $donation_meta ) {
1671 $subscription_id = $donation_meta->id;
1672
1673 $payment = $wpdb->get_row(
1674 $wpdb->prepare(
1675 "SELECT * FROM {$wpdb->prefix}posts
1676 WHERE post_parent=%d AND post_status LIKE %s
1677 ORDER BY ID DESC LIMIT 1",
1678 $donation_meta->parent_payment_id,
1679 'give_subscription'
1680 )
1681 );
1682 }
1683 } else {
1684 $payment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type=%s ORDER BY id DESC LIMIT 1", 'give_payment' ) );
1685 }
1686
1687 if ( ! empty( $payment ) ) {
1688 if ( 'continues_recurring_donation' == $data['search_term'] || 'cancels_recurring_donation' == $data['search_term'] ) {
1689 $subscription = new Give_Subscription( $subscription_id );
1690
1691 $pluggable_data['form_id'] = $form_id;
1692 $pluggable_data['subscription'] = $subscription;
1693 } elseif ( 'donation_specific_field_value' == $data['search_term'] ) {
1694 $payment_data = new Give_Payment( $payment->ID );
1695 $input_array = $payment_data->payment_meta;
1696 unset( $input_array['user_info'] );
1697 $pluggable_data = $input_array;
1698 foreach ( $input_array as $key => $value ) {
1699 $pluggable_data['field_id'] = $key;
1700 $pluggable_data['field_value'] = $value;
1701 }
1702 } else {
1703 $payment = new Give_Payment( $payment->ID );
1704 $address_data = $payment->address;
1705
1706 $pluggable_data['first_name'] = $payment->first_name;
1707 $pluggable_data['last_name'] = $payment->last_name;
1708 $pluggable_data['email'] = $payment->email;
1709 $pluggable_data['currency'] = $payment->currency;
1710 $pluggable_data['donated_amount'] = $payment->subtotal;
1711 $pluggable_data['donation_amount'] = $payment->subtotal;
1712 $pluggable_data['form_id'] = (int) $payment->form_id;
1713 $pluggable_data['form_title'] = $payment->form_title;
1714 $pluggable_data['name_title_prefix'] = $payment->title_prefix;
1715 $pluggable_data['date'] = $payment->date;
1716
1717 if ( is_array( $address_data ) ) {
1718 $pluggable_data['address_line_1'] = $address_data['line1'];
1719 $pluggable_data['address_line_2'] = $address_data['line2'];
1720 $pluggable_data['city'] = $address_data['city'];
1721 $pluggable_data['state'] = $address_data['state'];
1722 $pluggable_data['zip'] = $address_data['zip'];
1723 $pluggable_data['country'] = $address_data['country'];
1724 }
1725 // Payment meta.
1726 $payment_meta = $payment->get_meta();
1727 if ( is_array( $payment_meta ) && isset( $payment_meta['user_info'] ) ) {
1728 unset( $payment_meta['user_info'] );
1729 }
1730 $pluggable_data['payment_meta'] = $payment_meta;
1731 $donor_comment = give_get_donor_donation_comment( $payment->ID, $payment->donor_id );
1732 $pluggable_data['comment'] = ( is_array( $donor_comment ) && isset( $donor_comment['comment_content'] ) ) ? $donor_comment : '';
1733 }
1734
1735 $context['response_type'] = 'live';
1736 } else {
1737 if ( 'continues_recurring_donation' == $data['search_term'] || 'cancels_recurring_donation' == $data['search_term'] ) {
1738 $pluggable_data['form_id'] = $form_id;
1739 $pluggable_data['subscription']['id'] = 3;
1740 $pluggable_data['subscription']['donor_id'] = 8;
1741 $pluggable_data['subscription']['period'] = 'month';
1742 $pluggable_data['subscription']['initial_amount'] = '25.0000000000';
1743 $pluggable_data['subscription']['recurring_amount'] = '25.0000000000';
1744 $pluggable_data['subscription']['recurring_fee_amount'] = '0.0000000000';
1745 $pluggable_data['subscription']['transaction_id'] = 'a228ec9c6357963d23079d7d6945dd61';
1746 $pluggable_data['subscription']['parent_payment_id'] = '7492';
1747 $pluggable_data['subscription']['created'] = '2024-01-23 11:12:11';
1748 $pluggable_data['subscription']['expiration'] = '2024-03-23 23:59:59';
1749 $pluggable_data['subscription']['status'] = 'cancelled';
1750 $pluggable_data['subscription']['donor']['id'] = 8;
1751 $pluggable_data['subscription']['donor']['purchase_count'] = '3';
1752 $pluggable_data['subscription']['donor']['purchase_value'] = '75.000000';
1753 $pluggable_data['subscription']['donor']['email'] = 'johndoee@yopmail.com';
1754 $pluggable_data['subscription']['donor']['name'] = 'John Doe';
1755 $pluggable_data['subscription']['donor']['payment_ids'] = '7487,7492,7499';
1756 $pluggable_data['subscription']['donor']['user_id'] = '131';
1757 $pluggable_data['subscription']['customer_id'] = '8';
1758 } elseif ( 'donation_specific_field_value' == $data['search_term'] ) {
1759 $pluggable_data['form_id'] = 23;
1760 $pluggable_data['form_title'] = 'Demo Donation';
1761 $pluggable_data['_give_donor_billing_first_name'] = 'John';
1762 $pluggable_data['_give_donor_billing_last_name'] = 'Doe';
1763 $pluggable_data['_give_payment_donor_email'] = 'johndoee@gmail.com';
1764 $pluggable_data['_give_payment_currency'] = 'USD';
1765 $pluggable_data['_give_payment_total'] = '100';
1766 $pluggable_data['name_title_prefix'] = 'Mr';
1767 $pluggable_data['date'] = '2022-11-07 16:06:05';
1768 $pluggable_data['_give_donor_billing_address1'] = '33, Vincent Road';
1769 $pluggable_data['_give_donor_billing_address2'] = 'Chicago Street';
1770 $pluggable_data['_give_donor_billing_city'] = 'New York City';
1771 $pluggable_data['_give_donor_billing_state'] = 'New York';
1772 $pluggable_data['_give_donor_billing_zip'] = '223131';
1773 $pluggable_data['_give_donor_billing_country'] = 'USA';
1774 $pluggable_data['_give_donation_comment'] = 'Demo Comment';
1775 $pluggable_data['field_id'] = 'last_name';
1776 $pluggable_data['field_value'] = 'Doe';
1777 } else {
1778 $pluggable_data['first_name'] = 'John';
1779 $pluggable_data['last_name'] = 'Doe';
1780 $pluggable_data['email'] = 'johndoee@gmail.com';
1781 $pluggable_data['currency'] = 'USD';
1782 $pluggable_data['donated_amount'] = 100;
1783 $pluggable_data['donation_amount'] = 100;
1784 $pluggable_data['form_id'] = 23;
1785 $pluggable_data['form_title'] = 'Demo Donation';
1786 $pluggable_data['name_title_prefix'] = 'Mr';
1787 $pluggable_data['date'] = '2022-11-07 16:06:05';
1788 $pluggable_data['address_line_1'] = '33, Vincent Road';
1789 $pluggable_data['address_line_2'] = 'Chicago Street';
1790 $pluggable_data['city'] = 'New York City';
1791 $pluggable_data['state'] = 'New York';
1792 $pluggable_data['zip'] = '223131';
1793 $pluggable_data['country'] = 'USA';
1794 $pluggable_data['comment'] = 'Demo Comment';
1795 }
1796 $context['response_type'] = 'sample';
1797 }
1798
1799 $context['pluggable_data'] = $pluggable_data;
1800 return $context;
1801 }
1802
1803 /**
1804 * Search Divi Form fields.
1805 *
1806 * @param array $data Search Params.
1807 *
1808 * @since 1.0.0
1809 *
1810 * @return array
1811 */
1812 public function search_pluggable_divi_form_fields( $data ) {
1813 $result = [];
1814 $form_id = absint( $data['dynamic'] );
1815 $form_posts = Utilities::get_divi_forms();
1816
1817 if ( empty( $form_posts ) ) {
1818 return [
1819 'options' => [],
1820 'hasMore' => false,
1821 ];
1822 }
1823 $fields = [];
1824 foreach ( $form_posts as $form_post ) {
1825 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
1826 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
1827 if ( empty( $forms ) ) {
1828 continue;
1829 }
1830
1831 $count = 0;
1832
1833 foreach ( $forms as $form ) {
1834 $pattern = get_shortcode_regex( [ 'et_pb_contact_field' ] );
1835
1836 preg_match_all( "/$pattern/", $form[0], $contact_fields, PREG_SET_ORDER );
1837
1838 if ( empty( $contact_fields ) ) {
1839 return $fields;
1840 }
1841
1842 foreach ( $contact_fields as $contact_field ) {
1843 $contact_field_attrs = shortcode_parse_atts( $contact_field[3] );
1844 $field_id = strtolower( self::array_get( $contact_field_attrs, 'field_id' ) );
1845 $fields[] = [
1846 'field_title' => self::array_get( $contact_field_attrs, 'field_title', __( 'No title', 'suretriggers' ) ),
1847 'field_id' => $field_id,
1848 ];
1849 }
1850 }
1851 }
1852
1853 if ( ! empty( $fields ) ) {
1854 foreach ( $fields as $field ) {
1855 $result[] = [
1856 'label' => $field['field_title'],
1857 'value' => '{' . $field['field_id'] . '}',
1858 ];
1859 }
1860 }
1861
1862 return [
1863 'options' => $result,
1864 'hasMore' => false,
1865 ];
1866 }
1867
1868 /**
1869 * Pseudo function copied from Divi
1870 *
1871 * @param array $array An array which contains value located at `$address`.
1872 * @param string|array $address The location of the value within `$array` (dot notation).
1873 * @param mixed $default Value to return if not found. Default is an empty string.
1874 *
1875 * @return mixed The value, if found, otherwise $default.
1876 */
1877 public static function array_get( $array, $address, $default = '' ) {
1878 $keys = is_array( $address ) ? $address : explode( '.', $address );
1879 $value = $array;
1880
1881 foreach ( $keys as $key ) {
1882 if ( ! empty( $key ) && isset( $key[0] ) && '[' === $key[0] ) {
1883 $index = substr( $key, 1, -1 );
1884
1885 if ( is_numeric( $index ) ) {
1886 $key = (int) $index;
1887 }
1888 }
1889
1890 if ( ! isset( $value[ $key ] ) ) {
1891 return $default;
1892 }
1893
1894 $value = $value[ $key ];
1895 }
1896
1897 return $value;
1898 }
1899
1900 /**
1901 * Get UAG Forms.
1902 *
1903 * @param array $data data.
1904 *
1905 * @return array
1906 */
1907 public function search_spectra_forms( $data ) {
1908 $form_posts = Utilities::get_uag_forms();
1909
1910 $options = [];
1911 if ( empty( $form_posts ) ) {
1912 return [
1913 'options' => [],
1914 'hasMore' => false,
1915 ];
1916 }
1917
1918 foreach ( $form_posts as $form_post ) {
1919 $blocks = parse_blocks( $form_post['post_content'] );
1920 $i = 1;
1921 // Get form blocks.
1922 $this->process_blocks( $blocks, $form_post, $options, $i );
1923 }
1924
1925 return [
1926 'options' => $options,
1927 'hasMore' => false,
1928 ];
1929 }
1930
1931 /**
1932 * Return forms in nested blocks in Spectra.
1933 *
1934 * @param array $blocks data.
1935 * @param array $form_post Form post.
1936 * @param array $options Options.
1937 * @param int $i Number.
1938 *
1939 * @return array
1940 */
1941 public function process_blocks( $blocks, $form_post, &$options, &$i ) {
1942 foreach ( $blocks as $block ) {
1943 if ( 'uagb/forms' === $block['blockName'] ) {
1944 $options[] = [
1945 'label' => $form_post['post_title'] . ' (Form ' . ( $i++ ) . ')',
1946 'value' => $block['attrs']['block_id'],
1947 ];
1948 } elseif ( isset( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) {
1949 $this->process_blocks( $block['innerBlocks'], $form_post, $options, $i );
1950 }
1951 }
1952 return $options;
1953 }
1954
1955 /**
1956 * Check array recursive.
1957 *
1958 * @param array $array Array.
1959 * @param string $value search params.
1960 * @since 1.0.0
1961 *
1962 * @return array|void
1963 */
1964 public static function get_column_by_value( $array, $value ) {
1965
1966 foreach ( $array as $key => $sub_array ) {
1967
1968 if ( is_array( $sub_array ) ) {
1969 $result = self::get_column_by_value( $sub_array, $value );
1970 if ( null !== $result ) {
1971 return $key;
1972 }
1973 } else {
1974 return $key;
1975 }
1976 }
1977 return null;
1978
1979 }
1980
1981
1982 /**
1983 * Search UAG Form fields.
1984 *
1985 * @param array $data Search Params.
1986 *
1987 * @since 1.0.0
1988 *
1989 * @return array
1990 */
1991 public function search_spectraform_fields( $data ) {
1992 $result = [];
1993 $form_id = absint( $data['dynamic'] );
1994 $form_posts = Utilities::get_uag_forms();
1995
1996 if ( empty( $form_posts ) ) {
1997 return [
1998 'options' => [],
1999 'hasMore' => false,
2000 ];
2001 }
2002
2003 foreach ( $form_posts as $form_post ) {
2004 $blocks = parse_blocks( $form_post['post_content'] );
2005
2006 foreach ( $blocks as $block ) {
2007 if ( (int) $block['attrs']['block_id'] === $form_id ) {
2008 $doc = new DOMDocument();
2009 $rendered_block = render_block( $block );
2010 $doc->loadHTML( $rendered_block );
2011 $child_node_list = $doc->getElementsByTagName( 'div' );
2012 for ( $i = 0; $i < $child_node_list->length; $i++ ) {
2013 $temp = $child_node_list->item( $i );
2014 if ( $temp && stripos( $temp->getAttribute( 'class' ), 'uagb-forms-input-label' ) !== false ) {
2015 $nodes[] = $temp;
2016 }
2017 }
2018
2019 foreach ( $nodes as $node ) {
2020 $result[] = [
2021 'label' => $node->textContent, //phpcs:ignore
2022 'value' => $node->textContent, //phpcs:ignore
2023 ];
2024 }
2025 }
2026 }
2027 }
2028
2029 return [
2030 'options' => $result,
2031 'hasMore' => false,
2032 ];
2033 }
2034
2035 /**
2036 * Search forms of MetForms.
2037 *
2038 * @param array $data data.
2039 * @return array
2040 */
2041 public function search_met_forms( $data ) {
2042 $args = [
2043 'post_type' => 'metform-form',
2044 'post_status' => 'publish',
2045 'numberposts' => -1,
2046 ];
2047
2048 $forms = get_posts( $args );
2049
2050 $options = [];
2051
2052 if ( ! empty( $forms ) ) {
2053 foreach ( $forms as $form ) {
2054 $options[] = [
2055 'label' => $form->post_title,
2056 'value' => $form->ID,
2057 ];
2058 }
2059 }
2060
2061 return [
2062 'options' => $options,
2063 'hasMore' => false,
2064 ];
2065 }
2066
2067 /**
2068 * Search forms of Ninja Forms.
2069 *
2070 * @param array $data data.
2071 * @return array
2072 */
2073 public function search_ninja_forms( $data ) {
2074 $options = [];
2075
2076 if ( function_exists( 'Ninja_Forms' ) ) {
2077 foreach ( Ninja_Forms()->form()->get_forms() as $form ) {
2078 $options[] = [
2079 'label' => $form->get_setting( 'title' ),
2080 'value' => $form->get_id(),
2081 ];
2082 }
2083 }
2084
2085 return [
2086 'options' => $options,
2087 'hasMore' => false,
2088 ];
2089 }
2090
2091 /**
2092 * Search forms of Pie Forms.
2093 *
2094 * @param array $data data.
2095 * @return array
2096 */
2097 public function search_pie_forms( $data ) {
2098 global $wpdb;
2099 $options = [];
2100
2101 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pf_forms' ) ) ) {
2102
2103 $results = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'pf_forms WHERE post_status = "published"' );
2104
2105 if ( $results ) {
2106 foreach ( $results as $result ) {
2107 $options[] = [
2108 'label' => $result->form_title,
2109 'value' => $result->id,
2110 ];
2111 }
2112 }
2113 }
2114
2115 return [
2116 'options' => $options,
2117 'hasMore' => false,
2118 ];
2119 }
2120
2121 /**
2122 * Get Fluent Forms.
2123 *
2124 * @param array $data data.
2125 *
2126 * @return array
2127 */
2128 public function search_fluent_forms( $data ) {
2129
2130 if ( ! function_exists( 'wpFluent' ) ) {
2131 return [
2132 'options' => [],
2133 'hasMore' => false,
2134 ];
2135 }
2136
2137 $forms = wpFluent()->table( 'fluentform_forms' )
2138 ->select( [ 'id', 'title' ] )
2139 ->orderBy( 'id', 'DESC' )
2140 ->get();
2141
2142 $options = [];
2143 if ( ! empty( $forms ) ) {
2144 foreach ( $forms as $form ) {
2145 $options[] = [
2146 'label' => $form->title,
2147 'value' => $form->id,
2148 ];
2149 }
2150 }
2151
2152 return [
2153 'options' => $options,
2154 'hasMore' => false,
2155 ];
2156
2157 }
2158
2159 /**
2160 * Get Fluent Forms Fields.
2161 *
2162 * @param array $data data.
2163 *
2164 * @return array
2165 */
2166 public function search_fluent_forms_form_fields( $data ) {
2167
2168 $options = [];
2169
2170 if ( ! function_exists( 'wpFluent' ) ) {
2171 return [];
2172 }
2173
2174 $form = wpFluent()->table( 'fluentform_forms' )->find( $data['dynamic'] );
2175 $field_data = json_decode( $form->form_fields, true );
2176 if ( is_array( $field_data ) && ! empty( $field_data['fields'] ) ) {
2177 foreach ( $field_data['fields'] as $field ) {
2178 if ( isset( $field['fields'] ) ) {
2179 foreach ( $field['fields'] as $field_key => $sub_field ) {
2180 if (
2181 isset( $sub_field['settings'] )
2182 && isset( $sub_field['settings']['label'] )
2183 && isset( $sub_field['settings']['visible'] )
2184 && true === $sub_field['settings']['visible']
2185 ) {
2186 $options[] = [
2187 'label' => $sub_field['settings']['label'],
2188 'value' => $field_key,
2189 ];
2190 }
2191 }
2192 } elseif ( isset( $field['element'] ) &&
2193 'container' === (string) $field['element'] &&
2194 isset( $field['columns'] ) && is_array( $field['columns'] ) ) {
2195 $container_fields = $field['columns'];
2196 foreach ( $container_fields as $c_fields ) {
2197 foreach ( $c_fields['fields'] as $field_key => $sub_field ) {
2198 if ( isset( $sub_field['settings'] ) ) {
2199 $options[] = [
2200 'label' => ( '' !== $sub_field['settings']['label'] ) ?
2201 $sub_field['settings']['label'] :
2202 $sub_field['attributes']['name'],
2203 'value' => isset( $sub_field['attributes']['name'] ) ?
2204 $sub_field['attributes']['name'] :
2205 strtolower( $sub_field['settings']['label'] ),
2206 ];
2207 }
2208 }
2209 }
2210 } elseif ( isset( $field['attributes'] ) && isset( $field['attributes']['name'] ) ) {
2211 if ( isset( $field['attributes']['placeholder'] ) && ! empty( $field['attributes']['placeholder'] ) ) {
2212 $options[] = [
2213 'label' => $field['attributes']['placeholder'],
2214 'value' => $field['attributes']['name'],
2215 ];
2216 } elseif ( isset( $field['settings'] ) &&
2217 isset( $field['settings']['label'] ) &&
2218 ! empty( $field['settings']['label'] ) ) {
2219 $options[] = [
2220 'label' => $field['settings']['label'],
2221 'value' => $field['attributes']['name'],
2222 ];
2223 }
2224 }
2225 }
2226 }
2227
2228 return [
2229 'options' => $options,
2230 'hasMore' => false,
2231 ];
2232
2233 }
2234
2235 /**
2236 * Get Fluent Forms.
2237 *
2238 * @param array $data data.
2239 *
2240 * @return array
2241 */
2242 public function search_bricks_builder_forms( $data ) {
2243 $bricks_theme = wp_get_theme( 'bricks' );
2244 if ( ! $bricks_theme->exists() ) {
2245 return [
2246 'options' => [],
2247 'hasMore' => false,
2248 ];
2249 }
2250
2251 $bricks_settings = (array) get_option( BRICKS_DB_GLOBAL_SETTINGS );
2252 if ( array_key_exists( 'postTypes', $bricks_settings ) ) {
2253 $bricks_posts = $bricks_settings['postTypes'];
2254 } else {
2255 $bricks_posts = [];
2256 }
2257 $bricks_posts[] = 'bricks_template';
2258
2259 $args = [
2260 'post_type' => $bricks_posts,
2261 'post_status' => 'publish',
2262 'posts_per_page' => -1,
2263 ];
2264
2265 $templates = get_posts( $args );
2266
2267 $options = [];
2268 if ( ! empty( $templates ) ) {
2269 foreach ( $templates as $template ) {
2270 $fetch_content = get_post_meta( $template->ID, BRICKS_DB_PAGE_CONTENT, true );
2271 if ( is_array( $fetch_content ) ) {
2272 foreach ( $fetch_content as $content ) {
2273 if ( 'form' === $content['name'] ) {
2274 $options[] = [
2275 'label' => $template->post_title . ' - ' . ( isset( $content['label'] ) ? $content['label'] : 'Form' ),
2276 'value' => $content['id'],
2277 ];
2278 }
2279 }
2280 }
2281 }
2282 }
2283
2284 return [
2285 'options' => $options,
2286 'hasMore' => false,
2287 ];
2288
2289 }
2290
2291 /**
2292 * Bricks builder form fields.
2293 *
2294 * @param array $data data.
2295 * @return array
2296 */
2297 public function search_pluggable_bricks_builder_form_fields( $data ) {
2298 $result = [];
2299 $fields = [];
2300 $form_id_str = $data['dynamic'];
2301 $ids = explode( '_', $form_id_str );
2302 $post_id = $ids[0];
2303 $form_id = $ids[1];
2304 $fetch_content = get_post_meta( $post_id, BRICKS_DB_PAGE_CONTENT, true );
2305 if ( is_array( $fetch_content ) ) {
2306 foreach ( $fetch_content as $content ) {
2307 if ( 'form' === $content['name'] && $form_id === $content['id'] ) {
2308 $fields = $content['settings']['fields'];
2309 break;
2310 }
2311 }
2312 }
2313
2314 if ( ! empty( $fields ) ) {
2315 foreach ( $fields as $field ) {
2316 $result[] = [
2317 'label' => $field['label'],
2318 'value' => '{' . strtolower( $field['label'] ) . '}',
2319 ];
2320 }
2321 }
2322
2323 return [
2324 'options' => $result,
2325 'hasMore' => false,
2326 ];
2327 }
2328
2329 /**
2330 * Get fluent form fields
2331 *
2332 * @param array $data Data array.
2333 *
2334 * @return array
2335 */
2336 public function search_pluggable_fluent_form_fields( $data ) {
2337 $result = [];
2338 $form_id = absint( $data['dynamic'] );
2339
2340 $fluentform_fields = Utilities::get_fluentform_fields( $data['search_term'], -1, $form_id );
2341
2342 if ( is_array( $fluentform_fields['results'] ) ) {
2343 foreach ( $fluentform_fields['results'] as $field ) {
2344 $result[] = [
2345 'label' => $field['text'],
2346 'value' => "{{$field['value']}}",
2347 ];
2348 }
2349 }
2350
2351 $result[] = [
2352 'value' => '{form_id}',
2353 'label' => 'Form ID',
2354 ];
2355
2356 $result[] = [
2357 'value' => '{form_title}',
2358 'label' => 'Form Title',
2359 ];
2360 $result[] = [
2361 'value' => '{entry_id}',
2362 'label' => 'Entry ID',
2363 ];
2364
2365 $result[] = [
2366 'value' => '{entry_source_url}',
2367 'label' => 'Entry Source URL',
2368 ];
2369
2370 $result[] = [
2371 'value' => '{submission_date}',
2372 'label' => 'Submission Date',
2373 ];
2374
2375 $result[] = [
2376 'value' => '{user_ip}',
2377 'label' => 'User IP',
2378 ];
2379
2380 return [
2381 'options' => $result,
2382 'hasMore' => false,
2383 ];
2384 }
2385
2386 /**
2387 * Search Gravity Form fields.
2388 *
2389 * @param array $data Search Params.
2390 *
2391 * @since 1.0.0
2392 */
2393 public function search_gform_fields( $data ) {
2394 if ( ! class_exists( 'RGFormsModel' ) ) {
2395 return [
2396 'options' => [],
2397 'hasMore' => false,
2398 ];
2399 }
2400 $result = [];
2401 $page = $data['page'];
2402 $form_id = absint( $data['dynamic'] );
2403
2404 $form = RGFormsModel::get_form_meta( $form_id );
2405
2406 if ( is_array( $form['fields'] ) ) {
2407 foreach ( $form['fields'] as $field ) {
2408 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
2409 foreach ( $field['inputs'] as $input ) {
2410 if ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
2411 $result[] = [
2412 'value' => $input['id'],
2413 'label' => GFCommon::get_label( $field, $input['id'] ),
2414 ];
2415 }
2416 }
2417 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
2418 $result[] = [
2419 'value' => (string) $field['id'],
2420 'label' => GFCommon::get_label( $field ),
2421 ];
2422 }
2423 }
2424 }
2425
2426 return [
2427 'options' => $result,
2428 'hasMore' => false,
2429 ];
2430
2431 }
2432
2433 /**
2434 * Search Gravity Form fields.
2435 *
2436 * @param array $data Search Params.
2437 *
2438 * @since 1.0.0
2439 */
2440 public function search_pluggable_gravity_form_fields( $data ) {
2441 if ( ! class_exists( 'RGFormsModel' ) ) {
2442 return [
2443 'options' => [],
2444 'hasMore' => false,
2445 ];
2446 }
2447 $result = [];
2448 $form_id = absint( $data['dynamic'] );
2449
2450 $form = RGFormsModel::get_form_meta( $form_id );
2451
2452 if ( is_array( $form['fields'] ) ) {
2453 foreach ( $form['fields'] as $field ) {
2454 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
2455 foreach ( $field['inputs'] as $input ) {
2456 if ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
2457 $result[] = [
2458 'value' => '{' . $input['id'] . '}',
2459 'label' => GFCommon::get_label( $field, $input['id'] ),
2460 ];
2461 }
2462 }
2463 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
2464 $result[] = [
2465 'value' => '{' . $field['id'] . '}',
2466 'label' => GFCommon::get_label( $field ),
2467 ];
2468 }
2469 }
2470 }
2471
2472 $result[] = [
2473 'value' => '{gravity_form}',
2474 'label' => 'Form ID',
2475 ];
2476 $result[] = [
2477 'value' => '{form_title}',
2478 'label' => 'Form Title',
2479 ];
2480 $result[] = [
2481 'value' => '{entry_id}',
2482 'label' => 'Entry ID',
2483 ];
2484 $result[] = [
2485 'value' => '{user_ip}',
2486 'label' => 'User IP',
2487 ];
2488 $result[] = [
2489 'value' => '{entry_source_url}',
2490 'label' => 'Entry Source URL',
2491 ];
2492 $result[] = [
2493 'value' => '{entry_submission_date}',
2494 'label' => 'Entry Submission Date',
2495 ];
2496
2497 return [
2498 'options' => $result,
2499 'hasMore' => false,
2500 ];
2501
2502 }
2503
2504 /**
2505 * Search Gravity Form fields for action.
2506 *
2507 * @param array $data data.
2508 * @return array
2509 */
2510 public function search_gravity_form_custom_fields( $data ) {
2511 $context = [];
2512 if ( ! class_exists( 'RGFormsModel' ) || ! class_exists( 'GFCommon' ) ) {
2513 return [];
2514 }
2515 $form_id = $data['filter'];
2516 $form = RGFormsModel::get_form_meta( $form_id );
2517 $custom_fields = [];
2518 if ( is_array( $form['fields'] ) ) {
2519 foreach ( $form['fields'] as $field ) {
2520 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
2521 foreach ( $field['inputs'] as $input ) {
2522 if ( ! $input['isHidden'] ) {
2523 $custom_fields[] = [
2524 'label' => GFCommon::get_label( $field, $input['id'] ),
2525 'value' => $input['id'],
2526 'type' => 'text',
2527 ];
2528 }
2529 }
2530 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
2531 $custom_fields[] = [
2532 'label' => GFCommon::get_label( $field ),
2533 'value' => $field['id'],
2534 'type' => 'text',
2535 ];
2536 }
2537 }
2538 }
2539 $context['fields'] = $custom_fields;
2540 return $context;
2541 }
2542
2543 /**
2544 * Get user register details via gravity forms.
2545 *
2546 * @param array $data data.
2547 *
2548 * @return array
2549 */
2550 public function search_pluggables_gravity_forms_user_register( $data ) {
2551 global $wpdb;
2552 $context = [];
2553 $pluggable_data = [];
2554
2555 $results = $wpdb->get_results(
2556 $wpdb->prepare(
2557 "SELECT id, form_id, date_created, ip, source_url, created_by FROM {$wpdb->prefix}gf_entry
2558 WHERE form_id = %d AND
2559 status = %s
2560 ORDER BY id
2561 DESC LIMIT 1",
2562 $data['filter']['gravity_form']['value'],
2563 'active'
2564 )
2565 );
2566
2567 if ( $results ) {
2568 $pluggable_data['gravity_form'] = (int) $results[0]->form_id;
2569 $pluggable_data['entry_id'] = $results[0]->id;
2570 $pluggable_data['user_ip'] = $results[0]->ip;
2571 $pluggable_data['entry_source_url'] = $results[0]->source_url;
2572 $pluggable_data['entry_submission_date'] = $results[0]->date_created;
2573 $pluggable_data['user'] = WordPress::get_user_context( $results[0]->created_by );
2574 $context['response_type'] = 'live';
2575 } else {
2576 $pluggable_data['gravity_form'] = '3';
2577 $pluggable_data['entry_id'] = '13';
2578 $pluggable_data['user_ip'] = '127.0.0.0';
2579 $pluggable_data['entry_source_url'] = 'https://example.com';
2580 $pluggable_data['entry_submission_date'] = '2024-02-05 09:41:59';
2581 $pluggable_data['user']['wp_user_id'] = '123';
2582 $pluggable_data['user']['user_login'] = 'johnd';
2583 $pluggable_data['user']['display_name'] = 'johnd';
2584 $pluggable_data['user']['user_firstname'] = 'John';
2585 $pluggable_data['user']['user_lastname'] = 'Doe';
2586 $pluggable_data['user']['user_email'] = 'johnd@yopmail.com';
2587 $pluggable_data['user']['user_role'] = [ 'subscriber' ];
2588 $context['response_type'] = 'sample';
2589 }
2590
2591 $context['pluggable_data'] = $pluggable_data;
2592 return $context;
2593 }
2594
2595 /**
2596 * Get payment form details for gravity forms.
2597 *
2598 * @param array $data data.
2599 *
2600 * @return array
2601 */
2602 public function search_pluggables_gravity_forms_payment( $data ) {
2603 global $wpdb;
2604 $context = [];
2605 $pluggable_data = [];
2606
2607 $results = $wpdb->get_results(
2608 $wpdb->prepare(
2609 "SELECT * FROM {$wpdb->prefix}gf_entry
2610 WHERE form_id = %d AND
2611 status = %s AND
2612 payment_status LIKE %s
2613 ORDER BY id
2614 DESC LIMIT 1",
2615 $data['filter']['gravity_form']['value'],
2616 'active',
2617 'Paid'
2618 )
2619 );
2620
2621 if ( $results ) {
2622 $pluggable_data['gravity_form'] = (int) $results[0]->form_id;
2623 $pluggable_data['entry_id'] = $results[0]->id;
2624 $pluggable_data['user_ip'] = $results[0]->ip;
2625 $pluggable_data['entry_source_url'] = $results[0]->source_url;
2626 $pluggable_data['entry_submission_date'] = $results[0]->date_created;
2627 $pluggable_data['payment_status'] = $results[0]->payment_status;
2628 $pluggable_data['payment_amount'] = $results[0]->payment_amount;
2629 $pluggable_data['currency'] = $results[0]->currency;
2630 $pluggable_data['payment_method'] = $results[0]->payment_method;
2631 $pluggable_data['transaction_id'] = $results[0]->transaction_id;
2632 $pluggable_data['user'] = WordPress::get_user_context( $results[0]->created_by );
2633 $context['response_type'] = 'live';
2634 } else {
2635 $pluggable_data['gravity_form'] = '3';
2636 $pluggable_data['entry_id'] = '13';
2637 $pluggable_data['user_ip'] = '127.0.0.0';
2638 $pluggable_data['entry_source_url'] = 'https://example.com';
2639 $pluggable_data['entry_submission_date'] = '2024-02-05 09:41:59';
2640 $pluggable_data['payment_status'] = 'Paid';
2641 $pluggable_data['payment_amount'] = '10.00';
2642 $pluggable_data['currency'] = 'USD';
2643 $pluggable_data['payment_method'] = 'visa';
2644 $pluggable_data['transaction_id'] = 'st_ooi98';
2645 $pluggable_data['user']['wp_user_id'] = '123';
2646 $pluggable_data['user']['user_login'] = 'johnd';
2647 $pluggable_data['user']['display_name'] = 'johnd';
2648 $pluggable_data['user']['user_firstname'] = 'John';
2649 $pluggable_data['user']['user_lastname'] = 'Doe';
2650 $pluggable_data['user']['user_email'] = 'johnd@yopmail.com';
2651 $pluggable_data['user']['user_role'] = [ 'subscriber' ];
2652 $context['response_type'] = 'sample';
2653 }
2654
2655 $context['pluggable_data'] = $pluggable_data;
2656 return $context;
2657 }
2658
2659 /**
2660 * Prepare fluentcrm lists.
2661 *
2662 * @param array $data Search Params.
2663 *
2664 * @return array[]
2665 */
2666 public function search_fluentcrm_lists( $data ) {
2667
2668 $list_api = FluentCrmApi( 'lists' );
2669 $all_lists = $list_api->all();
2670 $options = [];
2671
2672 if ( ! empty( $all_lists ) ) {
2673 foreach ( $all_lists as $list ) {
2674 $options[] = [
2675 'label' => $list->title,
2676 'value' => $list->id,
2677 ];
2678 }
2679 }
2680
2681 return [
2682 'options' => $options,
2683 'hasMore' => false,
2684 ];
2685 }
2686
2687 /**
2688 * Prepare fluentcrm contact status.
2689 *
2690 * @param array $data Search Params.
2691 *
2692 * @return array[]
2693 */
2694 public function search_fluentcrm_contact_status( $data ) {
2695
2696 $options = [
2697 [
2698 'label' => __( 'Subscribed', 'suretriggers' ),
2699 'value' => 'subscribed',
2700 ],
2701 [
2702 'label' => __( 'Pending', 'suretriggers' ),
2703 'value' => 'pending',
2704 ],
2705 [
2706 'label' => __( 'Unsubscribed', 'suretriggers' ),
2707 'value' => 'unsubscribed',
2708 ],
2709 [
2710 'label' => __( 'Bounced', 'suretriggers' ),
2711 'value' => 'bounced',
2712 ],
2713 [
2714 'label' => __( 'Complained', 'suretriggers' ),
2715 'value' => 'complained',
2716 ],
2717 ];
2718
2719 return [
2720 'options' => $options,
2721 'hasMore' => false,
2722 ];
2723 }
2724
2725 /**
2726 * Prepare fluentcrm contact status.
2727 *
2728 * @param array $data Search Params.
2729 *
2730 * @return array[]
2731 */
2732 public function search_fluentcrm_fetch_custom_fields( $data ) {
2733
2734 $options = [
2735 [
2736 'label' => __( 'Yes', 'suretriggers' ),
2737 'value' => 'true',
2738 ],
2739 [
2740 'label' => __( 'No', 'suretriggers' ),
2741 'value' => 'false',
2742 ],
2743 ];
2744
2745 return [
2746 'options' => $options,
2747 'hasMore' => false,
2748 ];
2749 }
2750
2751 /**
2752 * Prepare fluentcrm tags.
2753 *
2754 * @param array $data Search Params.
2755 *
2756 * @return array[]
2757 */
2758 public function search_fluentcrm_tags( $data ) {
2759
2760 if ( ! function_exists( 'FluentCrmApi' ) ) {
2761 return [];
2762 }
2763
2764 $tag_api = FluentCrmApi( 'tags' );
2765 $all_tags = $tag_api->all();
2766 $options = [];
2767
2768 if ( ! empty( $all_tags ) ) {
2769 foreach ( $all_tags as $tag ) {
2770 $options[] = [
2771 'label' => $tag->title,
2772 'value' => $tag->id,
2773 ];
2774 }
2775 }
2776
2777 return [
2778 'options' => $options,
2779 'hasMore' => false,
2780 ];
2781 }
2782
2783 /**
2784 * Prepare JetpackCRM Contact tags.
2785 *
2786 * @param array $data Search Params.
2787 *
2788 * @return array
2789 */
2790 public function search_jetpack_crm_contact_tags( $data ) {
2791
2792 if ( ! function_exists( 'zeroBSCRM_getCustomerTags' ) ) {
2793 return [];
2794 }
2795
2796 $all_tags = zeroBSCRM_getCustomerTags();
2797 $options = [];
2798
2799 if ( ! empty( $all_tags ) ) {
2800 foreach ( $all_tags as $tag ) {
2801 $options[] = [
2802 'label' => $tag['name'],
2803 'value' => $tag['id'],
2804 ];
2805 }
2806 }
2807
2808 return [
2809 'options' => $options,
2810 'hasMore' => false,
2811 ];
2812 }
2813
2814 /**
2815 * Prepare JetpackCRM Company tags.
2816 *
2817 * @param array $data Search Params.
2818 *
2819 * @return array
2820 */
2821 public function search_jetpack_crm_company_tags( $data ) {
2822
2823 if ( ! defined( 'ZBS_TYPE_COMPANY' ) ) {
2824 return [];
2825 }
2826
2827 global $wpdb;
2828 $all_tags = $wpdb->get_results( $wpdb->prepare( "SELECT `ID`,`zbstag_name` FROM `{$wpdb->prefix}zbs_tags` WHERE zbstag_objtype = %d", ZBS_TYPE_COMPANY ) );
2829
2830 $options = [];
2831 if ( ! empty( $all_tags ) ) {
2832 foreach ( $all_tags as $tag ) {
2833 $options[] = [
2834 'label' => $tag->zbstag_name,
2835 'value' => $tag->ID,
2836 ];
2837 }
2838 }
2839
2840 return [
2841 'options' => $options,
2842 'hasMore' => false,
2843 ];
2844 }
2845
2846 /**
2847 * Prepare JetpackCRM Companies list.
2848 *
2849 * @param array $data Search Params.
2850 *
2851 * @return array
2852 */
2853 public function search_jetpack_crm_companies_list( $data ) {
2854
2855 if ( ! function_exists( 'zeroBS_getCompanies' ) ) {
2856 return [];
2857 }
2858
2859 $all_companies = zeroBS_getCompanies();
2860 $options = [];
2861
2862 if ( ! empty( $all_companies ) ) {
2863 foreach ( $all_companies as $company ) {
2864 $options[] = [
2865 'label' => $company['name'],
2866 'value' => $company['id'],
2867 ];
2868 }
2869 }
2870
2871 return [
2872 'options' => $options,
2873 'hasMore' => false,
2874 ];
2875 }
2876
2877 /**
2878 * Prepare JetpackCRM contact status.
2879 *
2880 * @param array $data Search Params.
2881 *
2882 * @return array
2883 */
2884 public function search_jetpack_crm_contact_statuses( $data ) {
2885
2886 $options = [
2887 [
2888 'label' => __( 'Lead', 'suretriggers' ),
2889 'value' => 'Lead',
2890 ],
2891 [
2892 'label' => __( 'Customer', 'suretriggers' ),
2893 'value' => 'Customer',
2894 ],
2895 [
2896 'label' => __( 'Refused', 'suretriggers' ),
2897 'value' => 'Refused',
2898 ],
2899 [
2900 'label' => __( 'Blacklisted', 'suretriggers' ),
2901 'value' => 'Blacklisted',
2902 ],
2903 [
2904 'label' => __( 'Cancelled by Customer', 'suretriggers' ),
2905 'value' => 'Cancelled by Customer',
2906 ],
2907 [
2908 'label' => __( 'Cancelled by Us (Pre-Quote)', 'suretriggers' ),
2909 'value' => 'Cancelled by Us (Pre-Quote)',
2910 ],
2911 [
2912 'label' => __( 'Cancelled by Us (Post-Quote)', 'suretriggers' ),
2913 'value' => 'Cancelled by Us (Post-Quote)',
2914 ],
2915 ];
2916
2917 return [
2918 'options' => $options,
2919 'hasMore' => false,
2920 ];
2921 }
2922
2923 /**
2924 * Prepare FunnelKit Automations' lists.
2925 *
2926 * @param array $data Search Params.
2927 *
2928 * @return array
2929 */
2930 public function search_funnel_kit_automations_lists( $data ) {
2931
2932 if ( ! class_exists( 'BWFCRM_Lists' ) ) {
2933 return [];
2934 }
2935
2936 $bwfcrm_lists = \BWFCRM_Lists::get_lists();
2937
2938 $options = [];
2939
2940 foreach ( $bwfcrm_lists as $list ) {
2941 $options[] = [
2942 'label' => $list['name'],
2943 'value' => $list['ID'],
2944 ];
2945 }
2946
2947 return [
2948 'options' => $options,
2949 'hasMore' => false,
2950 ];
2951 }
2952
2953 /**
2954 * Prepare FunnelKit Automations' tags.
2955 *
2956 * @param array $data Search Params.
2957 *
2958 * @return array
2959 */
2960 public function search_funnel_kit_automations_tags( $data ) {
2961
2962 if ( ! class_exists( 'BWFCRM_Tag' ) ) {
2963 return [];
2964 }
2965
2966 $bwfcrm_tags = \BWFCRM_Tag::get_tags();
2967
2968 $options = [];
2969
2970 foreach ( $bwfcrm_tags as $tag ) {
2971 $options[] = [
2972 'label' => $tag['name'],
2973 'value' => $tag['ID'],
2974 ];
2975 }
2976
2977 return [
2978 'options' => $options,
2979 'hasMore' => false,
2980 ];
2981 }
2982
2983 /**
2984 * Prepare Wishlist Memberlists level.
2985 *
2986 * @param array $data Search Params.
2987 *
2988 * @return array[]
2989 */
2990 public function search_wishlistmember_lists( $data ) {
2991
2992 $wlm_levels = wlmapi_get_levels();
2993 $options = [];
2994
2995 if ( ! empty( $wlm_levels ) ) {
2996 foreach ( $wlm_levels['levels']['level'] as $list ) {
2997 if ( isset( $list['name'] ) && ! empty( $list['name'] ) ) {
2998 $options[] = [
2999 'label' => $list['name'],
3000 'value' => (string) $list['id'],
3001 ];
3002 }
3003 }
3004 }
3005
3006 return [
3007 'options' => $options,
3008 'hasMore' => false,
3009 ];
3010 }
3011
3012 /**
3013 * Prepare Wishlist Memberlists members.
3014 *
3015 * @param array $data Search Params.
3016 *
3017 * @return array<string, array<int<0, max>, array<string, mixed>>|false>
3018 */
3019 public function search_wishlistmember_members( $data ) {
3020
3021 if ( ! function_exists( 'wlmapi_get_members' ) ) {
3022 return [];
3023 }
3024
3025 $wlm_members = wlmapi_get_members();
3026 $options = [];
3027
3028 if ( ! empty( $wlm_members ) ) {
3029 foreach ( $wlm_members['members']['member'] as $list ) {
3030 if ( isset( $list['user_email'] ) && ! empty( $list['user_email'] ) ) {
3031 $options[] = [
3032 'label' => $list['user_email'],
3033 'value' => (string) $list['id'],
3034 ];
3035 }
3036 }
3037 }
3038
3039 return [
3040 'options' => $options,
3041 'hasMore' => false,
3042 ];
3043 }
3044
3045 /**
3046 * Prepare elementor popups.
3047 *
3048 * @param array $data Search Params.
3049 *
3050 * @return array[]
3051 */
3052 public function search_elementor_popups( $data ) {
3053
3054 $posts = get_posts(
3055 [
3056 'post_type' => 'elementor_library',
3057 'orderby' => 'title',
3058 'order' => 'ASC',
3059 'post_status' => 'publish',
3060 'meta_query' => [
3061 [
3062 'key' => '_elementor_template_type',
3063 'value' => 'popup',
3064 'compare' => '=',
3065 ],
3066 ],
3067 ]
3068 );
3069
3070 $options = [];
3071 if ( ! empty( $posts ) ) {
3072 foreach ( $posts as $post ) {
3073 $options[] = [
3074 'label' => $post->post_title,
3075 'value' => $post->ID,
3076 ];
3077 }
3078 }
3079
3080 return [
3081 'options' => $options,
3082 'hasMore' => false,
3083 ];
3084 }
3085
3086 /**
3087 * Prepare givewp forms.
3088 *
3089 * @param array $data Search Params.
3090 *
3091 * @return array
3092 */
3093 public function search_givewp_forms( $data ) {
3094
3095 $page = $data['page'];
3096 $limit = Utilities::get_search_page_limit();
3097 $offset = $limit * ( $page - 1 );
3098
3099 $posts = get_posts(
3100 [
3101 'post_type' => 'give_forms',
3102 'orderby' => 'title',
3103 'order' => 'ASC',
3104 'post_status' => 'publish',
3105 'posts_per_page' => $limit,
3106 'offset' => $offset,
3107 ]
3108 );
3109
3110 $options = [];
3111 if ( ! empty( $posts ) ) {
3112 foreach ( $posts as $post ) {
3113 $options[] = [
3114 'label' => $post->post_title,
3115 'value' => $post->ID,
3116 ];
3117 }
3118 }
3119
3120 $count = wp_count_posts( 'give_forms' )->publish;
3121
3122 return [
3123 'options' => $options,
3124 'hasMore' => $count > $limit && $count > $offset,
3125 ];
3126 }
3127
3128 /**
3129 * Prepare givewp recurring forms.
3130 *
3131 * @param array $data Search Params.
3132 *
3133 * @return array
3134 */
3135 public function search_givewp_recurring_forms( $data ) {
3136
3137 global $wpdb;
3138 $recurring = $wpdb->get_results( $wpdb->prepare( "SELECT form_id FROM {$wpdb->prefix}give_formmeta WHERE meta_key LIKE %s AND meta_value NOT LIKE %s", '_give_recurring', 'no' ), ARRAY_A );
3139
3140 $options = [];
3141 if ( ! empty( $recurring ) ) {
3142 foreach ( $recurring as $form_id ) {
3143 $post_status = get_post_status( $form_id['form_id'] );
3144 if ( 'trash' !== $post_status ) {
3145 $options[] = [
3146 'label' => get_the_title( $form_id['form_id'] ),
3147 'value' => $form_id['form_id'],
3148 ];
3149 }
3150 }
3151 }
3152
3153 return [
3154 'options' => $options,
3155 'hasMore' => false,
3156 ];
3157 }
3158
3159 /**
3160 * Prepare givewp forms.
3161 *
3162 * @param array $data Search Params.
3163 *
3164 * @return array
3165 */
3166 public function search_givewp_form_fields( $data ) {
3167 $options = [
3168 [
3169 'label' => 'First Name',
3170 'value' => '_give_donor_billing_first_name',
3171 ],
3172 [
3173 'label' => 'Last Name',
3174 'value' => '_give_donor_billing_last_name',
3175 ],
3176 [
3177 'label' => 'Email',
3178 'value' => '_give_payment_donor_email',
3179 ],
3180 [
3181 'label' => 'Donation Amount',
3182 'value' => '_give_payment_total',
3183 ],
3184 [
3185 'label' => 'Currency',
3186 'value' => '_give_payment_currency',
3187 ],
3188 [
3189 'label' => 'Comment',
3190 'value' => '_give_donation_comment',
3191 ],
3192 [
3193 'label' => 'Zip',
3194 'value' => '_give_donor_billing_zip',
3195 ],
3196 [
3197 'label' => 'Country',
3198 'value' => '_give_donor_billing_country',
3199 ],
3200 [
3201 'label' => 'Address 1',
3202 'value' => '_give_donor_billing_address1',
3203 ],
3204 [
3205 'label' => 'Address 2',
3206 'value' => '_give_donor_billing_address2',
3207 ],
3208 [
3209 'label' => 'City',
3210 'value' => '_give_donor_billing_city',
3211 ],
3212 [
3213 'label' => 'State',
3214 'value' => '_give_donor_billing_state',
3215 ],
3216 ];
3217
3218 if ( class_exists( '\Give_FFM_Render_Form' ) ) {
3219 $form_id = $data['dynamic'];
3220 $custom_form_fields = [];
3221 if ( class_exists( '\GiveFormFieldManager\Helpers\Form' ) ) {
3222 $custom_form_fields = \GiveFormFieldManager\Helpers\Form::get_input_fields( $form_id );
3223 }
3224
3225 if ( ! empty( $custom_form_fields ) ) {
3226 if ( ! empty( $custom_form_fields[2] ) && is_array( $custom_form_fields[2] ) ) {
3227 foreach ( $custom_form_fields[2] as $custom_form_field ) {
3228 $custom_form_field['required'] = ( 'no' === $custom_form_field['required'] ) ? false : true;
3229 $options[] = [
3230 'label' => $custom_form_field['label'],
3231 'value' => $custom_form_field['name'],
3232 ];
3233 }
3234 }
3235 }
3236 }
3237
3238 return [
3239 'options' => $options,
3240 'hasMore' => false,
3241 ];
3242 }
3243
3244 /**
3245 * Prepare buddyboss group users.
3246 *
3247 * @param array $data Search Params.
3248 *
3249 * @return array[]
3250 */
3251 public function search_bb_group_users( $data ) {
3252 $options = [];
3253
3254 $group_id = $data['dynamic'];
3255 $admins = groups_get_group_admins( $group_id );
3256
3257 if ( ! empty( $admins ) ) {
3258 foreach ( $admins as $admin ) {
3259 $admin_user = get_user_by( 'id', $admin->user_id );
3260 $options[] = [
3261 'label' => $admin_user->display_name,
3262 'value' => $admin_user->ID,
3263 ];
3264 }
3265 }
3266
3267 $members = groups_get_group_members( [ 'group_id' => $group_id ] );
3268
3269 if ( isset( $members['members'] ) && ! empty( $members['members'] ) ) {
3270 foreach ( $members['members'] as $member ) {
3271 $options[] = [
3272 'label' => $member->display_name,
3273 'value' => $member->ID,
3274 ];
3275 }
3276 }
3277 return [
3278 'options' => $options,
3279 'hasMore' => false,
3280 ];
3281 }
3282
3283 /**
3284 * Prepare buddyboss groups.
3285 *
3286 * @param array $data Search Params.
3287 *
3288 * @return array[]
3289 */
3290 public function search_buddyboss_groups( $data ) {
3291 global $wpdb;
3292
3293 $options = [];
3294 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
3295 if ( ! empty( $groups ) ) {
3296 foreach ( $groups as $group ) {
3297 $options[] = [
3298 'label' => $group->name,
3299 'value' => $group->id,
3300 ];
3301 }
3302 }
3303 return [
3304 'options' => $options,
3305 'hasMore' => false,
3306 ];
3307 }
3308
3309 /**
3310 * Prepare buddyboss public groups.
3311 *
3312 * @param array $data Search Params.
3313 *
3314 * @return array[]
3315 */
3316 public function search_buddyboss_public_groups( $data ) {
3317 $options = [];
3318 $groups = groups_get_groups();
3319 if ( isset( $groups['groups'] ) && ! empty( $groups['groups'] ) ) {
3320 foreach ( $groups['groups'] as $group ) {
3321 if ( 'public' === $group->status ) {
3322 $options[] = [
3323 'label' => $group->name,
3324 'value' => $group->id,
3325 ];
3326 }
3327 }
3328 }
3329 return [
3330 'options' => $options,
3331 'hasMore' => false,
3332 ];
3333 }
3334
3335 /**
3336 * Prepare buddyboss private groups.
3337 *
3338 * @param array $data Search Params.
3339 *
3340 * @return array<string, array<int, array<string, mixed>>|false>
3341 */
3342 public function search_buddyboss_private_groups( $data ) {
3343 global $wpdb;
3344
3345 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'private'" );
3346
3347 $options = [];
3348 if ( $groups ) {
3349 foreach ( $groups as $group ) {
3350 $options[] = [
3351 'label' => $group->name,
3352 'value' => $group->id,
3353 ];
3354 }
3355 }
3356
3357 return [
3358 'options' => $options,
3359 'hasMore' => false,
3360 ];
3361 }
3362
3363 /**
3364 * Prepare buddyboss profile types list.
3365 *
3366 * @param array $data Search Params.
3367 *
3368 * @return array[]
3369 */
3370 public function search_bb_profile_type_list( $data ) {
3371 $options = [];
3372
3373 if ( function_exists( 'bp_get_active_member_types' ) ) {
3374 $types = bp_get_active_member_types(
3375 [
3376 'fields' => '*',
3377 ]
3378 );
3379 if ( $types ) {
3380 foreach ( $types as $type ) {
3381 $options[] = [
3382 'label' => $type->post_title,
3383 'value' => $type->ID,
3384 ];
3385 }
3386 }
3387 }
3388
3389 /**
3390 *
3391 * Ignore line
3392 *
3393 * @phpstan-ignore-next-line
3394 */
3395 return [
3396 'options' => $options,
3397 'hasMore' => false,
3398 ];
3399 }
3400
3401 /**
3402 * Prepare buddyboss public groups.
3403 *
3404 * @param array $data Search Params.
3405 *
3406 * @return array
3407 */
3408 public function search_buddyboss_group_type( $data ) {
3409 $options = [];
3410
3411 if ( ! function_exists( 'bp_groups_get_group_types' ) ) {
3412 return [];
3413 }
3414
3415 $registered_types = bp_groups_get_group_types();
3416 if ( ! empty( $registered_types ) ) {
3417 foreach ( $registered_types as $key => $type ) {
3418 $options[] = [
3419 'label' => $type,
3420 'value' => $key,
3421 ];
3422 }
3423 }
3424 return [
3425 'options' => $options,
3426 'hasMore' => false,
3427 ];
3428 }
3429
3430 /**
3431 * Get BuddyBoss Forum Topic Status.
3432 *
3433 * @param array $data data.
3434 *
3435 * @return array|void
3436 */
3437 public function search_bb_forum_topic_status( $data ) {
3438 $options = [];
3439
3440 if ( ! function_exists( 'bbp_get_topic_post_type' ) ) {
3441 return [];
3442 }
3443
3444 $page = $data['page'];
3445 $limit = Utilities::get_search_page_limit();
3446 $offset = $limit * ( $page - 1 );
3447
3448 $forum_args = [
3449 'post_type' => bbp_get_topic_post_type(),
3450 'orderby' => 'title',
3451 'order' => 'ASC',
3452 'post_status' => [ 'publish', 'private' ],
3453 'posts_per_page' => $limit,
3454 'offset' => $offset,
3455 ];
3456
3457 $forums = get_posts( $forum_args );
3458 if ( ! empty( $forums ) ) {
3459 foreach ( $forums as $key => $forum ) {
3460 $options[] = [
3461 'label' => $forum,
3462 'value' => $key,
3463 ];
3464 }
3465 }
3466 $count = count( $options );
3467 return [
3468 'options' => $options,
3469 'hasMore' => $count > $limit && $count > $offset,
3470 ];
3471 }
3472
3473 /**
3474 * Get BuddyBoss Forums List.
3475 *
3476 * @param array $data data.
3477 *
3478 * @return array|void
3479 */
3480 public function search_bb_forums_list( $data ) {
3481 $options = [];
3482
3483 $page = $data['page'];
3484 $limit = Utilities::get_search_page_limit();
3485 $offset = $limit * ( $page - 1 );
3486
3487 if ( ! function_exists( 'bbp_get_forum_post_type' ) ) {
3488 return [];
3489 }
3490
3491 $forum_args = [
3492 'post_type' => bbp_get_forum_post_type(),
3493 'orderby' => 'title',
3494 'order' => 'ASC',
3495 'post_status' => [ 'publish', 'private' ],
3496 'posts_per_page' => $limit,
3497 'offset' => $offset,
3498 ];
3499
3500 $forums = get_posts( $forum_args );
3501 if ( ! empty( $forums ) ) {
3502 foreach ( $forums as $key => $forum ) {
3503 $options[] = [
3504 'label' => $forum->post_title,
3505 'value' => $forum->ID,
3506 ];
3507 }
3508 }
3509 $count = count( $options );
3510 return [
3511 'options' => $options,
3512 'hasMore' => $count > $limit && $count > $offset,
3513 ];
3514 }
3515
3516 /**
3517 * Get BuddyBoss Forums List.
3518 *
3519 * @param array $data data.
3520 *
3521 * @return array|void
3522 */
3523 public function search_bb_topics_list( $data ) {
3524 $options = [];
3525
3526 $page = $data['page'];
3527 $limit = Utilities::get_search_page_limit();
3528 $offset = $limit * ( $page - 1 );
3529
3530 if ( ! function_exists( 'bbp_get_topic_post_type' ) ) {
3531 return [];
3532 }
3533
3534 $topic_args = [
3535 'post_type' => bbp_get_topic_post_type(),
3536 'post_parent' => $data['dynamic'],
3537 'orderby' => 'title',
3538 'order' => 'ASC',
3539 'post_status' => [ 'publish', 'private' ],
3540 'posts_per_page' => $limit,
3541 'offset' => $offset,
3542 ];
3543
3544 $topics = get_posts( $topic_args );
3545 if ( ! empty( $topics ) ) {
3546 foreach ( $topics as $key => $topic ) {
3547 $options[] = [
3548 'label' => $topic->post_title,
3549 'value' => $topic->ID,
3550 ];
3551 }
3552 }
3553 $count = count( $options );
3554 return [
3555 'options' => $options,
3556 'hasMore' => $count > $limit && $count > $offset,
3557 ];
3558 }
3559
3560 /**
3561 * Prepare elementor forms.
3562 *
3563 * @param array $data Search Params.
3564 *
3565 * @return array[]
3566 */
3567 public function search_elementor_forms( $data ) {
3568
3569 $elementor_forms = Utilities::get_elementor_forms();
3570
3571 $options = [];
3572 if ( ! empty( $elementor_forms ) ) {
3573 foreach ( $elementor_forms as $key => $value ) {
3574 $options[] = [
3575 'label' => $value,
3576 'value' => $key,
3577 ];
3578 }
3579 }
3580
3581 return [
3582 'options' => $options,
3583 'hasMore' => false,
3584 ];
3585 }
3586
3587 /**
3588 * Prepare elementor forms.
3589 *
3590 * @param array $data Search Params.
3591 *
3592 * @return array[]
3593 */
3594 public function search_new_elementor_forms( $data ) {
3595
3596 global $wpdb;
3597 $elementor_forms = [];
3598 $post_metas = $wpdb->get_results(
3599 $wpdb->prepare(
3600 "SELECT pm.post_id, pm.meta_value
3601 FROM $wpdb->postmeta pm
3602 LEFT JOIN $wpdb->posts p
3603 ON p.ID = pm.post_id
3604 WHERE p.post_type IS NOT NULL
3605 AND p.post_status = %s
3606 AND pm.meta_key = %s
3607 AND pm.`meta_value` LIKE %s",
3608 'publish',
3609 '_elementor_data',
3610 '%%form_fields%%'
3611 )
3612 );
3613
3614 if ( ! empty( $post_metas ) ) {
3615 foreach ( $post_metas as $post_meta ) {
3616 /**
3617 *
3618 * Ignore line
3619 *
3620 * @phpstan-ignore-next-line
3621 */
3622 $inner_forms = Utilities::search_elementor_forms( json_decode( $post_meta->meta_value ) );
3623 if ( ! empty( $inner_forms ) ) {
3624 foreach ( $inner_forms as $form ) {
3625 /**
3626 *
3627 * Ignore line
3628 *
3629 * @phpstan-ignore-next-line
3630 */
3631 $elementor_forms[ $post_meta->post_id . '_' . $form->id ] = $form->settings->form_name . ' (' . $form->id . ')';
3632 }
3633 }
3634 }
3635 }
3636
3637 $options = [];
3638 if ( ! empty( $elementor_forms ) ) {
3639 foreach ( $elementor_forms as $key => $value ) {
3640 $options[] = [
3641 'label' => $value,
3642 'value' => $key,
3643 ];
3644 }
3645 }
3646
3647 /**
3648 *
3649 * Ignore line
3650 *
3651 * @phpstan-ignore-next-line
3652 */
3653 return [
3654 'options' => $options,
3655 'hasMore' => false,
3656 ];
3657 }
3658
3659 /**
3660 * Prepare elementor form fields.
3661 *
3662 * @param array $data Search Params.
3663 *
3664 * @return array[]
3665 */
3666 public function search_pluggable_elementor_form_fields( $data ) {
3667 $result = [];
3668 $form_id = absint( $data['dynamic'] );
3669 $elementor_form_fields = ( new Utilities() )->get_elementor_form_fields( $data );
3670 $options = [];
3671 if ( ! empty( $elementor_form_fields ) ) {
3672 foreach ( $elementor_form_fields as $key => $value ) {
3673 $options[] = [
3674 'label' => $value,
3675 'value' => '{' . $key . '}',
3676 ];
3677 }
3678 }
3679
3680 return [
3681 'options' => $options,
3682 'hasMore' => false,
3683 ];
3684 }
3685
3686 /**
3687 * Get all events
3688 *
3689 * @param array $data Data array.
3690 *
3691 * @return array
3692 */
3693 public function search_event_calendar_event( $data ) {
3694 $page = $data['page'];
3695 $limit = Utilities::get_search_page_limit();
3696 $offset = $limit * ( $page - 1 );
3697
3698 $posts = get_posts(
3699 [
3700 'post_type' => 'tribe_events',
3701 'orderby' => 'title',
3702 'order' => 'ASC',
3703 'post_status' => 'publish',
3704 'posts_per_page' => $limit,
3705 'offset' => $offset,
3706 ]
3707 );
3708
3709 $options = [];
3710 if ( ! empty( $posts ) ) {
3711 foreach ( $posts as $post ) {
3712 $options[] = [
3713 'label' => $post->post_title,
3714 'value' => $post->ID,
3715 ];
3716 }
3717 }
3718
3719 $count = wp_count_posts( 'tribe_events' )->publish;
3720
3721 return [
3722 'options' => $options,
3723 'hasMore' => $count > $limit && $count > $offset,
3724 ];
3725 }
3726
3727 /**
3728 * Get all events tickets
3729 *
3730 * @param array $data Data array.
3731 *
3732 * @return array
3733 */
3734 public function search_event_calendar_event_tickets( $data ) {
3735 $event_id = $data['dynamic'];
3736 $options = [];
3737
3738 if ( ! class_exists( 'Tribe__Tickets_Plus__Commerce__WooCommerce__Main' ) ) {
3739 return [];
3740 }
3741
3742 $tickets = \Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance()->get_all_event_tickets( $event_id );
3743 if ( ! empty( $tickets ) ) {
3744 foreach ( $tickets as $ticket_object ) {
3745 $options[] = [
3746 'label' => $ticket_object->name,
3747 'value' => $ticket_object->ID,
3748 ];
3749 }
3750 }
3751
3752 return [
3753 'options' => $options,
3754 'hasMore' => false,
3755 ];
3756 }
3757
3758 /**
3759 * Prepare rsvp event calendar events.
3760 *
3761 * @param array $data Search Params.
3762 *
3763 * @return array
3764 */
3765 public function search_event_calendar_rsvp_event( $data ) {
3766
3767 $page = $data['page'];
3768 $limit = Utilities::get_search_page_limit();
3769 $offset = $limit * ( $page - 1 );
3770
3771 $posts = get_posts(
3772 [
3773 'post_type' => 'tribe_events',
3774 'orderby' => 'title',
3775 'order' => 'ASC',
3776 'post_status' => 'publish',
3777 'posts_per_page' => $limit,
3778 'offset' => $offset,
3779 ]
3780 );
3781
3782 $options = [];
3783 if ( ! empty( $posts ) ) {
3784 $ticket_handler = new Tribe__Tickets__Tickets_Handler();
3785 foreach ( $posts as $post ) {
3786
3787 $get_rsvp_ticket = $ticket_handler->get_event_rsvp_tickets( $post->ID );
3788
3789 if ( ! empty( $get_rsvp_ticket ) ) {
3790 $options[] = [
3791 'label' => $post->post_title,
3792 'value' => $post->ID,
3793 ];
3794 }
3795 }
3796 }
3797 $count = wp_count_posts( 'tribe_events' )->publish;
3798
3799 return [
3800 'options' => $options,
3801 'hasMore' => $count > $limit && $count > $offset,
3802 ];
3803 }
3804
3805 /**
3806 * Prepare Restrict Content Membership Level.
3807 *
3808 * @param array $data Search Params.
3809 *
3810 * @return array[]
3811 */
3812 public function search_restrictcontent_membership_level( $data ) {
3813
3814 $rcp_memberships = rcp_get_membership_levels();
3815 $options = [];
3816
3817 if ( ! empty( $rcp_memberships ) ) {
3818 foreach ( $rcp_memberships as $list ) {
3819 $options[] = [
3820 'label' => ucfirst( $list->name ),
3821 'value' => $list->id,
3822 ];
3823 }
3824 }
3825
3826 return [
3827 'options' => $options,
3828 'hasMore' => false,
3829 ];
3830 }
3831
3832 /**
3833 * Prepare Restrict Content Customer.
3834 *
3835 * @param array $data Search Params.
3836 *
3837 * @return array[]
3838 */
3839 public function search_restrictcontent_customer( $data ) {
3840
3841 $rcp_users = rcp_get_memberships();
3842 $options = [];
3843
3844 if ( ! empty( $rcp_users ) ) {
3845 foreach ( $rcp_users as $list ) {
3846 $user = get_user_by( 'ID', $list->get_user_id() );
3847 $user_label = $user->user_email;
3848
3849 if ( $user->display_name !== $user->user_email ) {
3850 $user_label .= ' (' . $user->display_name . ')';
3851 }
3852
3853 $options[] = [
3854 'label' => $user_label,
3855 'value' => $list->get_customer_id(),
3856 ];
3857 }
3858 }
3859
3860 return [
3861 'options' => $options,
3862 'hasMore' => false,
3863 ];
3864 }
3865
3866
3867 /**
3868 * Fetch the Presto Player video List.
3869 *
3870 * @param array $data Search Params.
3871 *
3872 * @return array[]
3873 */
3874 public function search_ap_presto_player_video_list( $data ) {
3875
3876 $videos = ( new Video() )->all();
3877 $options = [];
3878 if ( ! empty( $videos ) ) {
3879 foreach ( $videos as $video ) {
3880 $options[] = [
3881 'label' => $video->__get( 'title' ),
3882 'value' => (string) $video->__get( 'id' ),
3883 ];
3884 }
3885 }
3886
3887 return [
3888 'options' => $options,
3889 'hasMore' => false,
3890 ];
3891 }
3892
3893 /**
3894 * Presto Player Video percentage.
3895 *
3896 * @param array $data Search Params.
3897 *
3898 * @return array[]
3899 */
3900 public function search_prestoplayer_video_percent( $data ) {
3901
3902 $percents = [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ];
3903 $options = [];
3904
3905 foreach ( $percents as $percent ) {
3906 $options[] = [
3907 'label' => $percent . '%',
3908 'value' => (string) $percent,
3909 ];
3910 }
3911
3912 return [
3913 'options' => $options,
3914 'hasMore' => false,
3915 ];
3916 }
3917
3918 /**
3919 * Get user profile field options.
3920 *
3921 * @return array
3922 * @since 1.0.0
3923 */
3924 public function search_user_field_options() {
3925
3926 $options = apply_filters(
3927 'sure_trigger_get_user_field_options',
3928 [
3929 [
3930 'label' => __( 'User Name', 'suretriggers' ),
3931 'value' => 'user_login',
3932 ],
3933 [
3934 'label' => __( 'User Email', 'suretriggers' ),
3935 'value' => 'user_email',
3936 ],
3937 [
3938 'label' => __( 'Display Name', 'suretriggers' ),
3939 'value' => 'display_name',
3940 ],
3941 [
3942 'label' => __( 'User Password', 'suretriggers' ),
3943 'value' => 'user_pass',
3944 ],
3945 [
3946 'label' => __( 'Website', 'suretriggers' ),
3947 'value' => 'user_url',
3948 ],
3949 ]
3950 );
3951
3952 return [
3953 'options' => $options,
3954 'hasMore' => false,
3955 ];
3956 }
3957
3958 /**
3959 * Get user post field options.
3960 *
3961 * @return array
3962 * @since 1.0.0
3963 */
3964 public function search_post_field_options() {
3965
3966 return [
3967 'options' => [
3968 [
3969 'label' => __( 'Type', 'suretriggers' ),
3970 'value' => 'post_type',
3971 'dynamic_field' => [
3972 'type' => 'select-creatable',
3973 'ajaxIdentifier' => 'post_types',
3974 ],
3975 ],
3976 [
3977 'label' => __( 'Status', 'suretriggers' ),
3978 'value' => 'post_status',
3979 'dynamic_field' => [
3980 'type' => 'select-async',
3981 'ajaxIdentifier' => 'post_statuses',
3982 ],
3983 ],
3984 [
3985 'label' => __( 'Author', 'suretriggers' ),
3986 'value' => 'post_author',
3987 'dynamic_field' => [
3988 'type' => 'select-async',
3989 'ajaxIdentifier' => 'user',
3990 ],
3991 ],
3992 [
3993 'label' => __( 'Title', 'suretriggers' ),
3994 'value' => 'post_title',
3995 'dynamic_field' => [
3996 'type' => 'select-creatable',
3997 ],
3998 ],
3999 [
4000 'label' => __( 'Slug', 'suretriggers' ),
4001 'value' => 'post_slug',
4002 'dynamic_field' => [
4003 'type' => 'select-creatable',
4004 ],
4005 ],
4006 [
4007 'label' => __( 'Content', 'suretriggers' ),
4008 'value' => 'post_content',
4009 'dynamic_field' => [
4010 'type' => 'html-editor',
4011 ],
4012 ],
4013 ],
4014 'hasMore' => false,
4015 ];
4016 }
4017
4018 /**
4019 * Bricksbuilder grouped data.
4020 *
4021 * @param array $data data.
4022 * @return array
4023 */
4024 public function search_bb_groups( $data ) {
4025
4026 global $wpdb;
4027 $options = [];
4028
4029 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'bp_groups' ) ) ) {
4030
4031 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %s', $wpdb->prefix . 'bp_groups' ) );
4032
4033 if ( $results ) {
4034 foreach ( $results as $result ) {
4035 $options[] = [
4036 'label' => $result->name,
4037 'value' => $result->id,
4038 ];
4039 }
4040 }
4041 }
4042
4043 return [
4044 'options' => $options,
4045 'hasMore' => false,
4046 ];
4047 }
4048
4049 /**
4050 * Search forms.
4051 *
4052 * @return array
4053 */
4054 public function search_bb_forums() {
4055 $options = [];
4056 $allowed_atatus = [ 'publish', 'private' ];
4057 $forum_args = [
4058 'post_type' => bbp_get_forum_post_type(),
4059 'posts_per_page' => -1,
4060 'orderby' => 'title',
4061 'order' => 'ASC',
4062 'post_status' => 'any',
4063 ];
4064 $forums = get_posts( $forum_args );
4065
4066 if ( ! empty( $forums ) ) {
4067 foreach ( $forums as $forum ) {
4068 if ( in_array( $forum->post_status, $allowed_atatus, true ) ) {
4069 $options[] = [
4070 'label' => $forum->post_title,
4071 'value' => $forum->ID,
4072 ];
4073 }
4074 }
4075 }
4076 return [
4077 'options' => $options,
4078 'hasMore' => false,
4079 ];
4080 }
4081
4082 /**
4083 * Search Affiliate WP Referral Type.
4084 *
4085 * @return array
4086 */
4087 public function search_affwp_referral_type() {
4088 $options = [];
4089
4090 if ( ! function_exists( 'affiliate_wp' ) ) {
4091 return [];
4092 }
4093
4094 $types = affiliate_wp()->referrals->types_registry->get_types();
4095 if ( ! empty( $types ) ) {
4096 foreach ( $types as $type_id => $type ) {
4097 $options[] = [
4098 'label' => $type['label'],
4099 'value' => $type_id,
4100 ];
4101 }
4102 }
4103 return [
4104 'options' => $options,
4105 'hasMore' => false,
4106 ];
4107 }
4108
4109 /**
4110 * Search Affiliate WP Referral Status.
4111 *
4112 * @return array
4113 */
4114 public function search_affwp_referral_status() {
4115 $options = [];
4116
4117 if ( ! function_exists( 'affwp_get_affiliate_statuses' ) ) {
4118 return [];
4119 }
4120
4121 $statuses = affwp_get_affiliate_statuses();
4122 if ( ! empty( $statuses ) ) {
4123 foreach ( $statuses as $key => $status ) {
4124 $options[] = [
4125 'label' => $status,
4126 'value' => $key,
4127 ];
4128 }
4129 }
4130 return [
4131 'options' => $options,
4132 'hasMore' => false,
4133 ];
4134 }
4135
4136 /**
4137 * Search Affiliate WP Affiliates list.
4138 *
4139 * @return array
4140 */
4141 public function search_affwp_affiliates_list() {
4142 $options = [];
4143
4144 global $wpdb;
4145 $affiliates = $wpdb->get_results( "SELECT affiliate_id FROM {$wpdb->prefix}affiliate_wp_affiliates" );
4146
4147 if ( ! function_exists( 'affwp_get_affiliate_name' ) ) {
4148 return [];
4149 }
4150
4151 if ( ! empty( $affiliates ) ) {
4152 foreach ( $affiliates as $affiliate ) {
4153 $options[] = [
4154 'label' => affwp_get_affiliate_name( $affiliate->affiliate_id ),
4155 'value' => $affiliate->affiliate_id,
4156 ];
4157 }
4158 }
4159 return [
4160 'options' => $options,
4161 'hasMore' => false,
4162 ];
4163 }
4164
4165 /**
4166 * Search Slice WP Affiliates list.
4167 *
4168 * @return array
4169 */
4170 public function search_slicewp_affiliates_list() {
4171 $options = [];
4172
4173 global $wpdb;
4174 $affiliates = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}slicewp_affiliates" );
4175
4176 if ( ! function_exists( 'slicewp_get_affiliate_name' ) ) {
4177 return [];
4178 }
4179
4180 if ( ! empty( $affiliates ) ) {
4181 foreach ( $affiliates as $affiliate ) {
4182 $options[] = [
4183 'label' => slicewp_get_affiliate_name( $affiliate->id ),
4184 'value' => $affiliate->id,
4185 ];
4186 }
4187 }
4188 return [
4189 'options' => $options,
4190 'hasMore' => false,
4191 ];
4192 }
4193
4194 /**
4195 * Search for commissions list.
4196 *
4197 * @param array $data data.
4198 *
4199 * @return array
4200 */
4201 public function search_slicewp_commissions_list( $data ) {
4202 $options = [];
4203 global $wpdb;
4204 $affiliate_id = $data['dynamic']['affiliate_id'];
4205 $commissions = $wpdb->get_results(
4206 $wpdb->prepare(
4207 "SELECT *
4208 FROM {$wpdb->prefix}slicewp_commissions WHERE affiliate_id=%d ORDER BY id DESC ",
4209 $affiliate_id
4210 )
4211 );
4212
4213
4214 if ( ! empty( $commissions ) ) {
4215 foreach ( $commissions as $commission ) {
4216 $options[] = [
4217 'label' => $commission->reference . '(' . $commission->type . ')',
4218 'value' => $commission->id,
4219 ];
4220 }
4221 }
4222 return [
4223 'options' => $options,
4224 'hasMore' => false,
4225 ];
4226 }
4227
4228
4229 /**
4230 * Search Affiliate WP Affiliates list.
4231 *
4232 * @return array
4233 */
4234 public function search_affwp_affiliates_rate_type() {
4235 $options = [];
4236
4237 if ( ! function_exists( 'affwp_get_affiliate_rate_types' ) ) {
4238 return [];
4239 }
4240
4241 $rate_types = affwp_get_affiliate_rate_types();
4242
4243 if ( ! empty( $rate_types ) ) {
4244 foreach ( $rate_types as $key => $rate_type ) {
4245 $options[] = [
4246 'label' => $rate_type,
4247 'value' => $key,
4248 ];
4249 }
4250 }
4251 return [
4252 'options' => $options,
4253 'hasMore' => false,
4254 ];
4255 }
4256
4257 /**
4258 * Get last data for trigger.
4259 *
4260 * @param array $data data.
4261 * @return array
4262 */
4263 public function search_affiliate_wp_triggers_last_data( $data ) {
4264 global $wpdb;
4265
4266 $context = [];
4267 $context['response_type'] = 'sample';
4268
4269 $user_data = WordPress::get_sample_user_context();
4270
4271 $affiliate_data = [
4272 'affiliate_id' => 1,
4273 'rest_id' => '',
4274 'user_id' => 1,
4275 'rate' => '',
4276 'rate_type' => '',
4277 'flat_rate_basis' => '',
4278 'payment_email' => 'admin@gmail.com',
4279 'status' => 'active',
4280 'earnings' => 0,
4281 'unpaid_earnings' => 0,
4282 'referrals' => 0,
4283 'visits' => 0,
4284 'date_registered' => '2023-01-18 13:35:22',
4285 'dynamic_coupon' => 'KDJSKS',
4286 ];
4287
4288 $referral_data = [
4289 'referral_id' => 1,
4290 'affiliate_id' => 1,
4291 'visit_id' => 0,
4292 'rest_id' => '',
4293 'customer_id' => 0,
4294 'parent_id' => 0,
4295 'description' => 'Testing',
4296 'status' => 'unpaid',
4297 'amount' => '12.00',
4298 'currency' => '',
4299 'custom' => 'custom',
4300 'context' => '',
4301 'campaign' => '',
4302 'reference' => 'Reference',
4303 'products' => '',
4304 'date' => '2023-01-18 16:36:59',
4305 'type' => 'opt-in',
4306 'payout_id' => 0,
4307 ];
4308
4309 if ( ! function_exists( 'affwp_get_dynamic_affiliate_coupons' ) || ! function_exists( 'affwp_get_affiliate' ) || ! function_exists( 'affwp_get_referral' ) ) {
4310 return [];
4311 }
4312
4313 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4314
4315 if ( in_array( $term, [ 'affiliate_approved', 'affiliate_awaiting_approval' ], true ) ) {
4316 $status = 'affiliate_approved' === $term ? 'active' : 'pending';
4317 $affiliate = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_affiliates WHERE affiliate_id = ( SELECT max(affiliate_id) FROM {$wpdb->prefix}affiliate_wp_affiliates )" );
4318
4319 if ( ! empty( $affiliate ) ) {
4320 $affiliate = affwp_get_affiliate( $affiliate->affiliate_id );
4321 $affiliate_data = get_object_vars( $affiliate );
4322 $user_data = WordPress::get_user_context( $affiliate->user_id );
4323 $context['response_type'] = 'live';
4324 }
4325 $affiliate_data['status'] = $status;
4326 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data );
4327
4328 } elseif ( 'affiliate_makes_referral' == $term ) {
4329 $type = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4330 $referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE referral_id = ( SELECT max(referral_id) FROM {$wpdb->prefix}affiliate_wp_referrals ) AND sale = %s", $type ) );
4331
4332 if ( ! empty( $referral ) ) {
4333 $referral = affwp_get_referral( $referral->referral_id );
4334 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4335 $affiliate_data = get_object_vars( $affiliate );
4336 $user_data = WordPress::get_user_context( $affiliate->user_id );
4337 $referral_data = get_object_vars( $referral );
4338 $context['response_type'] = 'live';
4339 }
4340 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4341 } elseif ( 'affiliate_wc_product_purchased' == $term ) {
4342 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4343 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'woocommerce' ORDER BY referral_id DESC LIMIT 1" );
4344
4345 if ( ! empty( $referral ) ) {
4346 $referral = affwp_get_referral( $referral->referral_id );
4347 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4348 $affiliate_data = get_object_vars( $affiliate );
4349 if ( ! empty( $referral ) && function_exists( 'wc_get_order' ) ) {
4350 $order_id = $referral->reference;
4351 $order = wc_get_order( $order_id );
4352 if ( $order instanceof \WC_Order ) {
4353 $user_data = WordPress::get_user_context( $order->get_customer_id() );
4354 }
4355 }
4356 $referral_data = get_object_vars( $referral );
4357 $context['response_type'] = 'live';
4358 }
4359 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
4360 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data, $dynamic_coupons );
4361 if ( ! empty( $referral ) && function_exists( 'wc_get_order' ) ) {
4362 $order_id = $referral->reference;
4363 $order = wc_get_order( $order_id );
4364 $items = $order->get_items();
4365 foreach ( $items as $item ) {
4366 $context['pluggable_data']['product'] = $item['product_id'];
4367 }
4368 } else {
4369 $context['pluggable_data']['product'] = 1;
4370 }
4371 } elseif ( 'affiliate_edd_product_purchased' == $term ) {
4372 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4373 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'edd' ORDER BY referral_id DESC LIMIT 1" );
4374
4375 if ( ! empty( $referral ) ) {
4376 $referral = affwp_get_referral( $referral->referral_id );
4377 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4378 $affiliate_data = get_object_vars( $affiliate );
4379 $user_data = WordPress::get_user_context( $affiliate->user_id );
4380 $referral_data = get_object_vars( $referral );
4381 $context['response_type'] = 'live';
4382 }
4383 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4384 if ( ! empty( $referral ) && function_exists( 'edd_get_payment' ) ) {
4385 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
4386 $edd_payment_id = $referral->reference;
4387 $payment = edd_get_payment( $edd_payment_id );
4388 $cart_details = $payment->cart_details;
4389 $payment = get_object_vars( $payment );
4390 $context['pluggable_data'] = array_merge( $context['pluggable_data'], $dynamic_coupons, $payment );
4391 foreach ( $cart_details as $detail ) {
4392 $context['pluggable_data']['product'] = $detail['id'];
4393 }
4394 } else {
4395 $context['pluggable_data']['product'] = 1;
4396 }
4397 } elseif ( 'affiliate_mb_product_purchased' == $term ) {
4398 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4399 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'memberpress' ORDER BY referral_id DESC LIMIT 1" );
4400
4401 if ( ! empty( $referral ) && class_exists( '\MeprTransaction' ) ) {
4402 $referral = affwp_get_referral( $referral->referral_id );
4403 $reference_id = $referral->reference;
4404 $transaction = new MeprTransaction( $reference_id );
4405 $user_id = $transaction->user_id;
4406 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4407 $affiliate_data = get_object_vars( $affiliate );
4408 $user_data = WordPress::get_user_context( $user_id );
4409 $referral_data = get_object_vars( $referral );
4410 $context['response_type'] = 'live';
4411 }
4412 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
4413 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data, $dynamic_coupons );
4414 if ( ! empty( $referral ) && class_exists( '\MeprTransaction' ) ) {
4415 $membership_id = $wpdb->get_var(
4416 $wpdb->prepare(
4417 "SELECT product_id FROM
4418 {$wpdb->prefix}mepr_transactions WHERE id = %d",
4419 $referral->reference
4420 )
4421 );
4422 $context['pluggable_data']['product'] = $membership_id;
4423 $context['pluggable_data']['product_name'] = get_the_title( $membership_id );
4424 } else {
4425 $context['pluggable_data']['product'] = 1;
4426 $context['pluggable_data']['product_name'] = 'membership1';
4427 }
4428 } elseif ( 'affiliate_referral_paid' == $term ) {
4429 $type = isset( $data['filter']['type']['value'] ) ? $data['filter']['type']['value'] : '';
4430 $referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE type = %s AND status = 'paid'", $type ) );
4431
4432 if ( ! empty( $referral ) ) {
4433 $referral = affwp_get_referral( $referral->referral_id );
4434 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4435 $affiliate_data = get_object_vars( $affiliate );
4436 $user_data = WordPress::get_user_context( $affiliate->user_id );
4437 $referral_data = get_object_vars( $referral );
4438 $context['response_type'] = 'live';
4439 }
4440 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4441 } elseif ( 'affiliate_referral_rejected' == $term ) {
4442 $type = isset( $data['filter']['type']['value'] ) ? $data['filter']['type']['value'] : '';
4443 $referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE type = %s AND status = 'rejected'", $type ) );
4444 if ( ! empty( $referral ) ) {
4445 $referral = affwp_get_referral( $referral->referral_id );
4446 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4447 $affiliate_data = get_object_vars( $affiliate );
4448 $user_data = WordPress::get_user_context( $affiliate->user_id );
4449 $referral_data = get_object_vars( $referral );
4450 $context['response_type'] = 'live';
4451 }
4452 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4453 } else {
4454 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE referral_id = ( SELECT max(referral_id) FROM {$wpdb->prefix}affiliate_wp_referrals )" );
4455
4456 if ( ! empty( $referral ) ) {
4457 $referral = affwp_get_referral( $referral->referral_id );
4458 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4459 $affiliate_data = get_object_vars( $affiliate );
4460 $user_data = WordPress::get_user_context( $affiliate->user_id );
4461 $referral_data = get_object_vars( $referral );
4462 $context['response_type'] = 'live';
4463 }
4464 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4465 }
4466
4467 return $context;
4468 }
4469
4470 /**
4471 * Get last data for trigger.
4472 *
4473 * @param array $data data.
4474 * @return array
4475 */
4476 public function search_jetpack_crm_triggers_last_data( $data ) {
4477
4478 if ( ! function_exists( 'zeroBS_getCompanyCount' ) || ! function_exists( 'zeroBS_getCustomerCount' ) || ! function_exists( 'zeroBS_getQuoteCount' ) ) {
4479 return [];
4480 }
4481
4482 global $wpdb;
4483
4484 $context = [];
4485 $context['response_type'] = 'sample';
4486
4487 $company_id = [ 'company_id' => 1 ];
4488 $contact_id = [ 'contact_id' => 1 ];
4489 $quote_id = [ 'quote_id' => 1 ];
4490 $event_id = [ 'event_id' => 1 ];
4491 $invoice_id = [ 'invoice_id' => 1 ];
4492 $transaction_id = [ 'transaction_id' => 1 ];
4493
4494 $company = [
4495 'company_id' => 1,
4496 'company_status' => 'Lead',
4497 'company_name' => 'Example Company',
4498 'company_email' => 'info@example.com',
4499 'main_address_line_1' => '123 Main Street',
4500 'main_address_line_2' => 'Suite 456',
4501 'main_address_city' => 'New York',
4502 'main_address_state' => 'NY',
4503 'main_address_postal_code' => '10001',
4504 'main_address_country' => 'United States',
4505 'second_address_line_1' => '789 Second Avenue',
4506 'second_address_line_2' => 'Floor 10',
4507 'second_address_city' => 'Los Angeles',
4508 'second_address_state' => 'CA',
4509 'second_address_postal_code' => '90001',
4510 'second_address_country' => 'United States',
4511 'main_telephone' => '+1 123-456-7890',
4512 'secondary_telephone' => '+1 987-654-3210',
4513 ];
4514
4515 $contact = [
4516 'contact_id' => 1,
4517 'status' => 'Lead',
4518 'prefix' => 'Mr.',
4519 'full_name' => 'John Doe',
4520 'first_name' => 'John',
4521 'last_name' => 'Doe',
4522 'email' => 'johndoe@example.com',
4523 'main_address_line_1' => '123 Main Street',
4524 'main_address_line_2' => 'Suite 456',
4525 'main_address_city' => 'New York',
4526 'main_address_state' => 'NY',
4527 'main_address_postal_code' => '10001',
4528 'main_address_country' => 'United States',
4529 'second_address_line_1' => '789 Second Avenue',
4530 'second_address_line_2' => 'Floor 10',
4531 'second_address_city' => 'Los Angeles',
4532 'second_address_state' => 'CA',
4533 'second_address_postal_code' => '90001',
4534 'second_address_country' => 'United States',
4535 'home_telephone' => '+1 123-456-7890',
4536 'work_telephone' => '+1 987-654-3210',
4537 'mobile_telephone' => '+1 555-555-5555',
4538 ];
4539
4540 $quote = [
4541 'quote_id' => 1,
4542 'contact_id' => 2,
4543 'contact_email' => 'john@example.com',
4544 'contact_name' => 'John Doe',
4545 'status' => '<strong>Created, not yet accepted</strong>',
4546 'title' => 'Sample Quote',
4547 'value' => 1000,
4548 'date' => '2023-05-23',
4549 'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
4550 'notes' => 'Additional notes about the quote.',
4551 ];
4552
4553 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4554
4555 switch ( $term ) {
4556 case 'company_created':
4557 if ( zeroBS_getCompanyCount() > 0 ) {
4558 $company_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_companies WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_companies )" );
4559 $company = JetpackCRM::get_company_context( $company_data->ID );
4560 $context['response_type'] = 'live';
4561 }
4562 $context['pluggable_data'] = $company;
4563 break;
4564 case 'company_deleted':
4565 $context['pluggable_data'] = $company_id;
4566 break;
4567 case 'contact_created':
4568 if ( zeroBS_getCustomerCount() > 0 ) {
4569 $contact_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_contacts WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_contacts )" );
4570 $contact = JetpackCRM::get_contact_context( $contact_data->ID );
4571 $context['response_type'] = 'live';
4572 }
4573 $context['pluggable_data'] = $contact;
4574 break;
4575 case 'contact_deleted':
4576 $context['pluggable_data'] = $contact_id;
4577 break;
4578 case 'event_deleted':
4579 $context['pluggable_data'] = $event_id;
4580 break;
4581 case 'invoice_deleted':
4582 $context['pluggable_data'] = $invoice_id;
4583 break;
4584 case 'quote_accepted':
4585 case 'quote_created':
4586 if ( zeroBS_getQuoteCount() > 0 ) {
4587 $quote_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_quotes WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_quotes )" );
4588 $quote = JetpackCRM::get_quote_context( $quote_data->ID );
4589 $context['response_type'] = 'live';
4590 }
4591 $context['pluggable_data'] = $quote;
4592 break;
4593 case 'quote_deleted':
4594 $context['pluggable_data'] = $quote_id;
4595 break;
4596 case 'transaction_deleted':
4597 $context['pluggable_data'] = $transaction_id;
4598 break;
4599 }
4600
4601 return $context;
4602 }
4603
4604 /**
4605 * Get last data for trigger.
4606 *
4607 * @param array $data data.
4608 * @return array
4609 */
4610 public function search_funnel_kit_automations_triggers_last_data( $data ) {
4611
4612 if ( ! class_exists( 'BWFCRM_Contact' ) || ! class_exists( 'BWFCRM_Lists' ) || ! class_exists( 'BWFCRM_Tag' ) ) {
4613 return [];
4614 }
4615
4616 $context = [];
4617 $context['response_type'] = 'sample';
4618
4619 $contact = [
4620 'contact_id' => '1',
4621 'wpid' => '0',
4622 'uid' => '9e74246335fd81b1c4a9123842c12549',
4623 'email' => 'johndoe@example.com',
4624 'first_name' => 'John',
4625 'last_name' => 'Doe',
4626 'contact_no' => '+1 555-555-5555',
4627 'state' => 'NY',
4628 'country' => 'United States',
4629 'timezone' => 'New York',
4630 'creation_date' => '2023-05-29 15:26:03',
4631 'last_modified' => '2023-05-29 17:08:30',
4632 'source' => '',
4633 'type' => 'Los Angeles',
4634 'status' => '0',
4635 'tags' => '["1"]',
4636 'lists' => '["2","1"]',
4637 ];
4638
4639 $list = [
4640 'list_id' => 1,
4641 'list_name' => 'Sample List',
4642 ];
4643
4644 $tag = [
4645 'tag_id' => 1,
4646 'tag_name' => 'Sample Tag',
4647 ];
4648
4649 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4650
4651 $recent_contacts = \BWFCRM_Contact::get_recent_contacts( 0, 1, [] );
4652 $contact_email = count( $recent_contacts['contacts'] ) > 0 && isset( $recent_contacts['contacts'][0]['email'] ) ? $recent_contacts['contacts'][0]['email'] : '';
4653
4654 $real_contact = false;
4655 if ( ! empty( $contact_email ) ) {
4656 $contact_obj = \BWFCRM_Contact::get_contacts( $contact_email, 0, 1, [], [], OBJECT );
4657
4658 if ( isset( $contact_obj['contacts'][0] ) ) {
4659 $contact = FunnelKitAutomations::get_contact_context( $contact_obj['contacts'][0]->contact );
4660 $real_contact = true;
4661 }
4662 }
4663
4664 if ( 'contact_added_to_list' === $term || 'contact_removed_from_list' === $term ) {
4665 $list_id = (int) ( isset( $data['filter']['list_id']['value'] ) ? $data['filter']['list_id']['value'] : '-1' );
4666
4667 if ( -1 === $list_id ) {
4668 $lists = \BWFCRM_Lists::get_lists( [], '', 0, 1 );
4669 $list_id = count( $lists ) > 0 ? $lists[0]['ID'] : -1;
4670 }
4671
4672
4673 if ( -1 !== $list_id ) {
4674 $list = FunnelKitAutomations::get_list_context( $list_id );
4675 $context['response_type'] = $real_contact ? 'live' : 'sample';
4676 }
4677
4678 $context['pluggable_data'] = array_merge( $list, $contact );
4679 } else {
4680 $tag_id = (int) ( isset( $data['filter']['tag_id']['value'] ) ? $data['filter']['tag_id']['value'] : '-1' );
4681
4682 if ( -1 === $tag_id ) {
4683 $tags = \BWFCRM_Tag::get_tags( [], '', 0, 1 );
4684 $tag_id = count( $tags ) > 0 ? $tags[0]['ID'] : -1;
4685 }
4686
4687
4688 if ( -1 !== $tag_id ) {
4689 $tag = FunnelKitAutomations::get_tag_context( $tag_id );
4690 $context['response_type'] = $real_contact ? 'live' : 'sample';
4691 }
4692
4693 $context['pluggable_data'] = array_merge( $tag, $contact );
4694 }
4695
4696 return $context;
4697 }
4698
4699 /**
4700 * Get last data for trigger.
4701 *
4702 * @param array $data data.
4703 * @return array
4704 */
4705 public function search_edd_triggers_last_data( $data ) {
4706 global $wpdb;
4707 $context = [];
4708 $context['response_type'] = 'sample';
4709 $context['pluggable_data'] = [];
4710 $order_data = [
4711 'order_id' => 187,
4712 'customer_email' => 'john_doe@gmail.com',
4713 'customer_id' => 2,
4714 'user_id' => 1,
4715 'customer_first_name' => 'Sure',
4716 'customer_last_name' => 'Dev',
4717 'ordered_items' => 'Price with license — Price one',
4718 'currency' => 'USD',
4719 'status' => 'complete',
4720 'discount_codes' => '',
4721 'order_discounts' => 0.00,
4722 'order_subtotal' => 12.00,
4723 'order_tax' => 0.00,
4724 'order_total' => 12.00,
4725 'payment_method' => 'manual',
4726 'purchase_key' => 'd797b9576a3895e7424bae2417ed87df',
4727 'ordered_items_ids' => 17250,
4728 'download_id' => 17250,
4729 'license_key' => 'f7736093411cfaed18b56ec60227117b',
4730 'license_key_expire_date' => '1697524076',
4731 'license_key_status' => 'inactive',
4732 ];
4733
4734 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4735 $download_id = isset( $data['filter']['download_id']['value'] ) ? $data['filter']['download_id']['value'] : 0;
4736 if ( 'order_created' === $term || 'order_one_product' === $term ) {
4737 $order_data['purchase_key'] = '06d3b7d923ca922dc889354f9bc33fbb';
4738
4739 $args = [
4740 'number' => 1,
4741 'status' => [ 'complete', 'refunded', 'partially_refunded', 'renewal' ],
4742 ];
4743 if ( $download_id > 0 ) {
4744 $args['download'] = $download_id;
4745 }
4746 $payments = edd_get_payments( $args );
4747 if ( count( $payments ) > 0 ) {
4748 $order_data = EDD::get_product_purchase_context( $payments[0], $term, $download_id );
4749
4750 $context['response_type'] = 'live';
4751 } else {
4752 if ( 'order_one_product' === $term ) {
4753 $order_data['price_id'] = 1;
4754 }
4755 }
4756 } elseif ( 'stripe_payment_refunded' === $term ) {
4757 $args = [
4758 'number' => 1,
4759 'status' => 'complete',
4760 'type' => 'refund',
4761 ];
4762 $payments = edd_get_payments( $args );
4763
4764 if ( count( $payments ) > 0 ) {
4765 $order_data = EDD::get_purchase_refund_context( $payments[0] );
4766 $context['response_type'] = 'live';
4767 }
4768 } else {
4769 $status = isset( $data['post_type'] ) ? $data['post_type'] : '';
4770 if ( ! empty( $status ) ) {
4771 if ( $download_id > 0 ) {
4772 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE status= %s AND download_id=%d ORDER BY id DESC", $status, $download_id ) );
4773 } else {
4774 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE status= %s ORDER BY id DESC", $status ) );
4775 }
4776 } else {
4777 if ( $download_id > 0 ) {
4778 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE download_id=%d ORDER BY id DESC", $download_id ) );
4779 } else {
4780 $licesnses = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}edd_licenses ORDER BY id DESC" );
4781 }
4782 }
4783 if ( ! empty( $licesnses ) ) {
4784 $order_data = EDD::edd_get_license_data( $licesnses->id, $licesnses->download_id, $licesnses->payment_id );
4785 $context['response_type'] = 'live';
4786 } else {
4787 $order_data = [
4788 'ID' => 1,
4789 'key' => '23232323232',
4790 'customer_email' => 'suretest@example.com',
4791 'customer_name' => 'Sure Test',
4792 'product_id' => 1,
4793 'download_id' => 1,
4794 'product_name' => 'Test',
4795 'activation_limit' => 2,
4796 'activation_count' => 1,
4797 'activated_urls' => 'https://example.com',
4798 'expiration' => '1686297914',
4799 'is_lifetime' => '0',
4800 'status' => $status,
4801 ];
4802
4803 }
4804 }
4805
4806 $context['pluggable_data'] = $order_data;
4807 return $context;
4808 }
4809
4810 /**
4811 * Get last data for trigger.
4812 *
4813 * @param array $data data.
4814 * @return array
4815 */
4816 public function search_presto_player_triggers_last_data( $data ) {
4817 $context = [];
4818 $context['response_type'] = 'sample';
4819
4820 $user_data = WordPress::get_sample_user_context();
4821
4822 $video_data = [
4823 'pp_video' => '1',
4824 'pp_video_percentage' => '100',
4825 ];
4826
4827 $video_data['video'] = [
4828 'id' => '1',
4829 'title' => 'SureTriggers Is Here 🎉 The Easiest Automation Builder WordPress Websites & Apps',
4830 'type' => 'youtube',
4831 'external_id' => '-cYbNYgylLs',
4832 'attachment_id' => '0',
4833 'post_id' => '127',
4834 'src' => 'https://www.youtube.com/watch?v=-cYbNYgylLs',
4835 'created_by' => '1',
4836 'created_at' => '2022-11-10 00:28:25',
4837 'updated_at' => '2022-11-10 00:34:40',
4838 'deleted_at' => '',
4839 ];
4840
4841 $video_data['media']['tag'] = [
4842 'Demo',
4843 'Demo1',
4844 ];
4845
4846 $mediahub_data = [
4847 'post_id' => '1',
4848 'activity_id' => '2',
4849 'post_author' => '1',
4850 'post_content' => 'New sample post...!',
4851 'post_title' => 'Sample Post',
4852 'post_excerpt' => 'sample',
4853 'post_status' => 'publish',
4854 'post_type' => 'pp_video_block',
4855 ];
4856
4857 $videos = ( new Video() )->all();
4858
4859 if ( count( $videos ) > 0 ) {
4860 if ( ! empty( $data['filter'] ) ) {
4861 $video_id = $data['filter']['pp_video']['value'];
4862 if ( -1 === $video_id ) {
4863 global $wpdb;
4864 $args = [
4865 'numberposts' => 1,
4866 'orderby' => 'rand',
4867 'order' => 'ASC',
4868 'post_type' => 'pp_video_block',
4869 ];
4870 $posts = get_posts( $args );
4871 $presto_post_video_id = $posts[0]->ID;
4872 $presto_video_id = $wpdb->get_var( $wpdb->prepare( 'SELECT * FROM wp_presto_player_videos WHERE post_id = %d', $presto_post_video_id ) );
4873 $video_id = $presto_video_id;
4874 }
4875 $pp_videos = ( new Video( $video_id ) )->toArray();
4876 if ( ! empty( $pp_videos ) ) {
4877 $video_data = [];
4878 $video_data['video'] = $pp_videos;
4879 if ( is_array( $video_data ) && array_key_exists( 'post_id', $video_data['video'] ) ) {
4880 $mediahub_data = WordPress::get_post_context( (int) $video_data['video']['post_id'] );
4881 $media_tags = get_the_terms( (int) $video_data['video']['post_id'], 'pp_video_tag' );
4882 if ( ! empty( $media_tags ) && is_array( $media_tags ) && isset( $media_tags[0] ) ) {
4883 $tag_name = [];
4884 foreach ( $media_tags as $tag ) {
4885 $tag_name[] = $tag->name;
4886 }
4887 $video_data['media']['tag'] = $tag_name;
4888 }
4889 }
4890 $video_data['pp_video'] = $video_id;
4891 $video_data['pp_video_percentage'] = isset( $data['filter']['pp_video_percentage']['value'] ) ? $data['filter']['pp_video_percentage']['value'] : '100';
4892 $user_data = WordPress::get_user_context( (int) $video_data['video']['created_by'] );
4893
4894 $context['response_type'] = 'live';
4895 }
4896 }
4897 }
4898
4899 $context['pluggable_data'] = array_merge( $user_data, $video_data, $mediahub_data );
4900
4901 return $context;
4902 }
4903
4904 /**
4905 * Get last data for trigger.
4906 *
4907 * @param array $data data.
4908 * @return array
4909 */
4910 public function search_member_press_triggers_last_data( $data ) {
4911 global $wpdb;
4912
4913 $term = $data['search_term'] ? $data['search_term'] : '';
4914
4915 $context = [];
4916 $context['response_type'] = 'sample';
4917
4918 $user_data = WordPress::get_sample_user_context();
4919
4920 $membership_data = [
4921 'membership_id' => '190',
4922 'membership_title' => 'Sample Membership',
4923 'amount' => '12.00',
4924 'total' => '12.00',
4925 'tax_amount' => '0.00',
4926 'tax_rate' => '0.00',
4927 'trans_num' => 't_63a03f3334f44',
4928 'status' => 'complete',
4929 'subscription_id' => '0',
4930 'membership_url' => site_url() . '/register/premium/',
4931 'membership_featured_image_id' => '521',
4932 'membership_featured_image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
4933 ];
4934
4935 $membership_id = (int) ( isset( $data['filter']['membership_id']['value'] ) ? $data['filter']['membership_id']['value'] : '-1' );
4936
4937 if ( in_array( $term, [ 'mepr-event-transaction-expired', 'mepr_subscription_transition_status', 'mepr-event-transaction-paused' ] ) ) {
4938 $status = 'cancelled';
4939 if ( 'mepr-event-transaction-paused' === $term ) {
4940 $status = 'suspended';
4941 }
4942 if ( $membership_id > 0 ) {
4943 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_subscriptions WHERE product_id= %s AND status=%s ORDER BY id DESC LIMIT 1", $membership_id, $status ) );
4944 } else {
4945 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_subscriptions WHERE status=%s ORDER BY id DESC LIMIT 1", $status ) );
4946
4947 }
4948 if ( ! empty( $subscription ) ) {
4949
4950 $membership_data = MemberPress::get_subscription_context( $subscription );
4951 $user_data = WordPress::get_user_context( $subscription->user_id );
4952
4953
4954 $context['response_type'] = 'live';
4955 }
4956 } elseif ( 'mepr-coupon-code-redeemed' === $term ) {
4957
4958 $coupon_id = (int) ( isset( $data['filter']['coupon_id']['value'] ) ? $data['filter']['coupon_id']['value'] : '-1' );
4959
4960 if ( $coupon_id > 0 ) {
4961 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE coupon_id= %s ORDER BY id DESC LIMIT 1", $coupon_id ) );
4962 } else {
4963 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE coupon_id!= %s ORDER BY id DESC LIMIT 1", '0' ) );
4964 }
4965
4966
4967 if ( ! empty( $subscription ) ) {
4968
4969 $membership_data = MemberPress::get_membership_context( $subscription );
4970 $user_data = WordPress::get_user_context( $subscription->user_id );
4971 $membership_data['coupon_id'] = $subscription->coupon_id;
4972 $membership_data['coupon'] = get_post( $subscription->coupon_id );
4973 $context['response_type'] = 'live';
4974 }
4975 } else {
4976
4977 if ( $membership_id > 0 ) {
4978
4979 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE product_id= %s ORDER BY id DESC LIMIT 1", $membership_id ) );
4980 } else {
4981 $subscription = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mepr_transactions ORDER BY id DESC LIMIT 1" );
4982 }
4983
4984
4985 if ( ! empty( $subscription ) ) {
4986
4987 $membership_data = MemberPress::get_membership_context( $subscription );
4988 $user_data = WordPress::get_user_context( $subscription->user_id );
4989
4990
4991 $context['response_type'] = 'live';
4992 }
4993 }
4994
4995
4996 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
4997 return $context;
4998 }
4999
5000 /**
5001 * Get last data for trigger.
5002 *
5003 * @param array $data data.
5004 * @return array
5005 */
5006 public function search_wishlist_member_triggers_last_data( $data ) {
5007 global $wpdb;
5008
5009 $context = [];
5010 $context['response_type'] = 'sample';
5011
5012 $user_data = WordPress::get_sample_user_context();
5013
5014 $membership_data = [
5015 'membership_level_id' => '1',
5016 'membership_level_name' => 'Sample Membership Level',
5017 ];
5018
5019 $membership_level_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
5020
5021 if ( $membership_level_id > 0 ) {
5022 $membership = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wlm_userlevels WHERE level_id= %s ORDER BY id DESC LIMIT 1", $membership_level_id ) );
5023 } else {
5024 $membership = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}wlm_userlevels ORDER BY id DESC LIMIT 1" );
5025 }
5026 if ( ! empty( $membership ) ) {
5027 $membership_data = WishlistMember::get_membership_detail_context( (int) $membership->level_id, (int) $membership->user_id );
5028 $user_data = WordPress::get_user_context( $membership->user_id );
5029
5030 $context['response_type'] = 'live';
5031 }
5032
5033 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5034
5035 return $context;
5036 }
5037
5038 /**
5039 * Get last data for trigger.
5040 *
5041 * @param array $data data.
5042 * @return array
5043 */
5044 public function search_peepso_triggers_last_data( $data ) {
5045 global $wpdb;
5046
5047 $context = [];
5048 $context['response_type'] = 'sample';
5049
5050 $user_data = WordPress::get_sample_user_context();
5051
5052 $post_data = [
5053 'post_id' => '1',
5054 'activity_id' => '2',
5055 'post_author' => '1',
5056 'post_content' => 'New sample post...!',
5057 'post_title' => 'Sample Post',
5058 'post_excerpt' => 'sample',
5059 'post_status' => 'publish',
5060 'post_type' => 'peepso-post',
5061 ];
5062
5063 $post = $wpdb->get_row( "SELECT act_id, act_owner_id, act_external_id FROM {$wpdb->prefix}peepso_activities ORDER BY act_id DESC LIMIT 1" );
5064
5065 if ( ! empty( $post ) ) {
5066 $post_data = PeepSo::get_pp_activity_context( (int) $post->act_external_id, (int) $post->act_id );
5067 $user_data = WordPress::get_user_context( $post->act_owner_id );
5068
5069 $context['response_type'] = 'live';
5070 }
5071
5072 $context['pluggable_data'] = array_merge( $user_data, $post_data );
5073
5074 return $context;
5075 }
5076
5077 /**
5078 * Get last data for Peepso User triggers.
5079 *
5080 * @param array $data data.
5081 * @return array
5082 */
5083 public function search_peepso_user_triggers_last_data( $data ) {
5084 global $wpdb;
5085
5086 $context = [];
5087 $context['response_type'] = 'sample';
5088 $term = $data['search_term'];
5089
5090 if ( ! class_exists( 'PeepSoUser' ) ) {
5091 return [];
5092 }
5093
5094 if ( 'user_follows_member' === $term || 'user_gains_follower' === $term ) {
5095 $member_id = $data['filter']['follow_user_id']['value'];
5096 if ( -1 === $member_id ) {
5097 $followers = $wpdb->get_results(
5098 $wpdb->prepare(
5099 "SELECT * FROM
5100 {$wpdb->prefix}peepso_user_followers WHERE uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5101 1
5102 )
5103 );
5104 } else {
5105 $followers = $wpdb->get_results(
5106 $wpdb->prepare(
5107 "SELECT * FROM
5108 {$wpdb->prefix}peepso_user_followers WHERE uf_passive_user_id = %d AND
5109 uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5110 $member_id,
5111 1
5112 )
5113 );
5114 }
5115 } elseif ( 'user_unfollows_member' === $term || 'user_loses_follower' === $term ) {
5116 $member_id = $data['filter']['follow_user_id']['value'];
5117 if ( -1 === $member_id ) {
5118 $followers = $wpdb->get_results(
5119 $wpdb->prepare(
5120 "SELECT * FROM
5121 {$wpdb->prefix}peepso_user_followers WHERE uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5122 0
5123 )
5124 );
5125 } else {
5126 $followers = $wpdb->get_results(
5127 $wpdb->prepare(
5128 "SELECT * FROM
5129 {$wpdb->prefix}peepso_user_followers WHERE uf_passive_user_id = %d AND
5130 uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5131 $member_id,
5132 0
5133 )
5134 );
5135 }
5136 } elseif ( 'user_updates_avatar' === $term ) {
5137 $followers = $wpdb->get_results(
5138 $wpdb->prepare(
5139 "SELECT * FROM
5140 {$wpdb->prefix}peepso_users WHERE usr_avatar_custom = %d AND usr_role = %s
5141 ORDER BY usr_last_activity DESC LIMIT 1",
5142 1,
5143 'member'
5144 )
5145 );
5146 } elseif ( 'user_updates_specific_profile_field' === $term ) {
5147 $field_id = $data['filter']['user_profile_field_id']['value'];
5148 $followers = $wpdb->get_results(
5149 $wpdb->prepare(
5150 "SELECT * FROM
5151 {$wpdb->prefix}peepso_users WHERE usr_role = %s
5152 ORDER BY usr_last_activity DESC LIMIT 1",
5153 'member'
5154 )
5155 );
5156 }
5157 if ( 'user_updates_avatar' === $term ) {
5158 if ( ! empty( $followers ) ) {
5159 $context_data['user'] = WordPress::get_user_context( $followers[0]->usr_id );
5160 $context['response_type'] = 'live';
5161 } else {
5162 $context_data['user'] = WordPress::get_sample_user_context();
5163 }
5164 } elseif ( 'user_updates_specific_profile_field' === $term ) {
5165 if ( ! empty( $followers ) ) {
5166 $user = PeepSoUser::get_instance( $followers[0]->usr_id );
5167 $user->profile_fields->load_fields();
5168 $user_fields = $user->profile_fields->get_fields();
5169 foreach ( $user_fields as $key => $value ) {
5170 $val = get_user_meta( $followers[0]->usr_id, $value->key, true );
5171 if ( '' != $val ) {
5172 $context_data[ $value->title ] = $val;
5173 }
5174 }
5175 $curruser = get_userdata( $followers[0]->usr_id );
5176 $context_data['user_id'] = $followers[0]->usr_id;
5177 $context_data['user_email'] = $user->get_email();
5178 $context_data['avatar_url'] = $user->get_avatar();
5179 $context_data['profile_url'] = $user->get_profileurl();
5180 $context_data['about_me'] = get_user_meta( $followers[0]->usr_id, 'description', true );
5181 if ( $curruser instanceof \WP_User ) {
5182 $context_data['website'] = $curruser->user_url;
5183 }
5184 $context_data['role'] = $user->get_user_role();
5185 $context['response_type'] = 'live';
5186 } else {
5187 $context_data['user'] = WordPress::get_sample_user_context();
5188 }
5189 } else {
5190 if ( ! empty( $followers ) ) {
5191 $context_data['follower_user'] = WordPress::get_user_context( $followers[0]->uf_active_user_id );
5192 $context_data['following_user'] = WordPress::get_user_context( $followers[0]->uf_passive_user_id );
5193 $context['response_type'] = 'live';
5194 } else {
5195 $context_data['follower_user'] = WordPress::get_sample_user_context();
5196 $context_data['following_user'] = WordPress::get_sample_user_context();
5197 }
5198 }
5199
5200 $context['pluggable_data'] = $context_data;
5201
5202 return $context;
5203 }
5204
5205 /**
5206 * Search Peepso User Profile Fields list.
5207 *
5208 * @param array $data data.
5209 * @return array
5210 */
5211 public function search_peepso_profile_field_list( $data ) {
5212 $options = [];
5213 if ( ! class_exists( 'PeepSoUser' ) ) {
5214 return [];
5215 }
5216 $options = [
5217 [
5218 'label' => 'Allow others to "like" my profile',
5219 'value' => 'peepso_is_profile_likable',
5220 ],
5221 [
5222 'label' => 'Hide my birthday year',
5223 'value' => 'peepso_hide_birthday_year',
5224 ],
5225 [
5226 'label' => 'Who can see my profile',
5227 'value' => 'usr_profile_acc',
5228 ],
5229 [
5230 'label' => 'Who can post on my profile',
5231 'value' => 'peepso_profile_post_acc',
5232 ],
5233 [
5234 'label' => "Don't show my online status",
5235 'value' => 'peepso_hide_online_status',
5236 ],
5237 [
5238 'label' => 'My timezone',
5239 'value' => 'peepso_gmt_offset',
5240 ],
5241 ];
5242 $peepsouser = PeepSoUser::get_instance( 0 );
5243 $peepsouser->profile_fields->load_fields();
5244 $fields = $peepsouser->profile_fields->get_fields();
5245 foreach ( $fields as $field ) {
5246 if ( 1 == $field->published ) {
5247 $options[] = [
5248 'label' => $field->title,
5249 'value' => $field->id,
5250 ];
5251 }
5252 }
5253 return [
5254 'options' => $options,
5255 'hasMore' => false,
5256 ];
5257 }
5258
5259 /**
5260 * Search Peepso Roles list.
5261 *
5262 * @param array $data data.
5263 * @return array
5264 */
5265 public function search_peepso_role_list( $data ) {
5266 $options = [
5267 [
5268 'label' => 'Community Member',
5269 'value' => 'member',
5270 ],
5271 [
5272 'label' => 'Community Moderator',
5273 'value' => 'moderator',
5274 ],
5275 [
5276 'label' => 'Community Administrator',
5277 'value' => 'admin',
5278 ],
5279 [
5280 'label' => 'Banned',
5281 'value' => 'ban',
5282 ],
5283 [
5284 'label' => 'Pending user email verification',
5285 'value' => 'register',
5286 ],
5287 [
5288 'label' => 'Pending admin approval',
5289 'value' => 'verified',
5290 ],
5291 ];
5292 return [
5293 'options' => $options,
5294 'hasMore' => false,
5295 ];
5296 }
5297
5298 /**
5299 * Search Peepso Users list.
5300 *
5301 * @param array $data data.
5302 * @return array
5303 */
5304 public function search_peepso_follow_user_list( $data ) {
5305 $options = [];
5306 global $wpdb;
5307 $users = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}peepso_users", ARRAY_A );
5308 if ( count( $users ) > 0 ) {
5309 foreach ( $users as $user ) {
5310 $user_by_id = get_user_by( 'id', $user['usr_id'] );
5311 if ( $user_by_id instanceof \WP_User ) {
5312 $options[] = [
5313 'label' => sprintf( '%s %s [%s]', $user_by_id->last_name, $user_by_id->first_name, $user_by_id->user_email ),
5314 'value' => $user['usr_id'],
5315 ];
5316 } else {
5317 $options[] = [
5318 'label' => '#' . $user['usr_id'],
5319 'value' => $user['usr_id'],
5320 ];
5321 }
5322 }
5323 }
5324 return [
5325 'options' => $options,
5326 'hasMore' => false,
5327 ];
5328 }
5329
5330 /**
5331 * Get last data for trigger
5332 *
5333 * @param array $data data.
5334 * @return array
5335 */
5336 public function search_restrict_content_pro_triggers_last_data( $data ) {
5337 $context = [];
5338 $context['response_type'] = 'sample';
5339
5340 $user_data = WordPress::get_sample_user_context();
5341
5342 $membership_data = [
5343 'membership_level_id' => '190',
5344 'membership_level' => 'Sample Membership',
5345 'membership_initial_payment' => '0.00',
5346 'membership_recurring_payment' => '0.00',
5347 'membership_expiry_date' => 'January 22, 2023',
5348 ];
5349
5350 $customer_id = (int) ( isset( $data['filter']['membership_customer_id']['value'] ) ? $data['filter']['membership_customer_id']['value'] : '-1' );
5351 $membership_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
5352
5353 $args = [];
5354 if ( 'membership_purchased' === $data['search_term'] ) {
5355 $args = [
5356 'status' => 'active',
5357 'number' => 1,
5358 'orderby' => 'id',
5359 ];
5360 } elseif ( 'membership_cancelled' === $data['search_term'] ) {
5361 $args = [
5362 'status' => 'cancelled',
5363 'number' => 1,
5364 'orderby' => 'id',
5365 ];
5366 } elseif ( 'membership_expired' === $data['search_term'] ) {
5367 $args = [
5368 'status' => 'expired',
5369 'number' => 1,
5370 'orderby' => 'id',
5371 ];
5372 }
5373
5374 if ( 'membership_expired' === $data['search_term'] && -1 !== $customer_id ) {
5375 $args['customer_id'] = $customer_id;
5376 }
5377
5378 if ( -1 !== $membership_id ) {
5379 $args['object_id'] = $membership_id;
5380 }
5381
5382 $memberships = rcp_get_memberships( $args );
5383 if ( count( $memberships ) > 0 ) {
5384 $membership_data = RestrictContent::get_rcp_membership_detail_context( $memberships[0] );
5385 $user_data = WordPress::get_user_context( $memberships[0]->get_user_id() );
5386
5387 $context['response_type'] = 'live';
5388 }
5389
5390 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5391
5392 return $context;
5393 }
5394
5395 /**
5396 * Get last data for trigger
5397 *
5398 * @param array $data data.
5399 * @return array
5400 */
5401 public function search_events_calendar_triggers_last_data( $data ) {
5402 $context = [];
5403 $context['response_type'] = 'sample';
5404
5405 $event_data = [
5406 'event_id' => 1,
5407 'event' => [
5408 'ID' => 58,
5409 'post_author' => 1,
5410 'post_date' => '2023-01-19 09:27:58',
5411 'post_date_gmt' => '2023-01-19 09:27:58',
5412 'post_content' => '',
5413 'post_title' => 'New event',
5414 'post_excerpt' => '',
5415 'post_status' => 'publish',
5416 'comment_status' => 'open',
5417 'ping_status' => 'closed',
5418 'post_password' => '',
5419 'post_name' => 'new-event',
5420 'to_ping' => '',
5421 'pinged' => '',
5422 'post_modified' => '2023-01-19 09:44:25',
5423 'post_modified_gmt' => '2023-01-19 09:44:25',
5424 'post_content_filtered' => '',
5425 'post_parent' => 0,
5426 'guid' => 'http://connector.com/?post_type=tribe_events&#038;p=58',
5427 'menu_order' => -1,
5428 'post_type' => 'tribe_events',
5429 'post_mime_type' => '',
5430 'comment_count' => 0,
5431 'filter' => 'raw',
5432 ],
5433 'attendies' => [
5434 'order_id' => 68,
5435 'purchaser_name' => 'John Doe',
5436 'purchaser_email' => 'john@test.com',
5437 'provider' => 'Tribe__Tickets__RSVP',
5438 'provider_slug' => 'rsvp',
5439 'purchase_time' => '2023-01-19 09:48:43',
5440 'optout' => 1,
5441 'ticket' => 'Prime',
5442 'attendee_id' => 68,
5443 'security' => '2cefc3b53e',
5444 'product_id' => 65,
5445 'check_in' => '',
5446 'order_status' => 'yes',
5447 'order_status_label' => 'Going',
5448 'user_id' => 1,
5449 'ticket_sent' => 1,
5450 'event_id' => 58,
5451 'ticket_name' => 'Prime',
5452 'holder_name' => 'John Doe',
5453 'holder_email' => 'john@test.com',
5454 'ticket_id' => 68,
5455 'qr_ticket_id' => 68,
5456 'security_code' => '2cefc3b53e',
5457 'attendee_meta' => '',
5458 'is_subscribed' => '',
5459 'is_purchaser' => 1,
5460 'ticket_exists' => 1,
5461 ],
5462 ];
5463
5464 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
5465 $term = $data['search_term'];
5466
5467 if ( 'event_register' === $term || 'attendee_registered_event' === $term ) {
5468 $args = [
5469 'post_type' => 'tribe_rsvp_attendees',
5470 'orderby' => 'ID',
5471 'order' => 'DESC',
5472 'post_status' => 'publish',
5473 'numberposts' => 1,
5474 ];
5475
5476 if ( -1 !== $event_id ) {
5477 $args['meta_query'] = [
5478 [
5479 'key' => '_tribe_rsvp_event',
5480 'value' => $event_id,
5481 ],
5482 ];
5483 }
5484
5485 $attendees = get_posts( $args );
5486
5487 if ( count( $attendees ) > 0 ) {
5488 $attendee = $attendees[0];
5489 $attendee_id = $attendee->ID;
5490
5491 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
5492 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
5493
5494 $event_context = TheEventCalendar::get_event_context( $product_id, $order_id );
5495
5496 if ( ! empty( $event_context ) ) {
5497 $event_data = $event_context;
5498 $context['response_type'] = 'live';
5499 }
5500 }
5501 } elseif ( 'event_attends' === $term ) {
5502 if ( -1 == $event_id ) {
5503 $args = [
5504 'numberposts' => 1,
5505 'orderby' => 'rand',
5506 'post_type' => 'tribe_events',
5507 ];
5508 $posts = get_posts( $args );
5509 $event_id = $posts[0]->ID;
5510 }
5511 $args = [
5512 'post_type' => 'tribe_rsvp_attendees',
5513 'orderby' => 'ID',
5514 'order' => 'DESC',
5515 'post_status' => 'publish',
5516 'numberposts' => 1,
5517 'meta_query' => [
5518 'relation' => 'AND',
5519 [
5520 'key' => '_tribe_rsvp_checkedin',
5521 'value' => 1,
5522 'compare' => '=',
5523 ],
5524 [
5525 'key' => '_tribe_rsvp_event',
5526 'value' => $event_id,
5527 'compare' => '=',
5528 ],
5529 ],
5530 ];
5531 $attendees = get_posts( $args );
5532 if ( ! function_exists( 'tribe_tickets_get_attendees' ) ) {
5533 return [];
5534 }
5535 if ( ! empty( $attendees ) ) {
5536 $attendee = $attendees[0];
5537 $attendee_id = $attendee->ID;
5538 /**
5539 *
5540 * Ignore line
5541 *
5542 * @phpstan-ignore-next-line
5543 */
5544 $attendee_details = tribe_tickets_get_attendees( $attendee_id, 'rsvp_order' );
5545 foreach ( $attendee_details as $detail ) {
5546 if ( (int) $detail['attendee_id'] !== (int) $attendee_id ) {
5547 continue;
5548 }
5549 $attendee = $detail;
5550 }
5551 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
5552 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
5553 $event_context = TheEventCalendar::get_event_context( $product_id, $order_id );
5554 if ( ! empty( $event_context ) ) {
5555 $event_data = array_merge( $attendee, $event_context );
5556 $context['response_type'] = 'live';
5557 }
5558 } else {
5559 $order = [
5560 'order_id' => 7962,
5561 'purchaser_name' => 'bella4 bella4',
5562 'purchaser_email' => 'bella4@yopmail.com',
5563 'provider' => 'Tribe__Tickets__RSVP',
5564 'provider_slug' => 'rsvp',
5565 'purchase_time' => '2024 - 03 - 04 07:26:41',
5566 'optout' => 1,
5567 'ticket' => 'test test',
5568 'attendee_id' => 7962,
5569 'security' => 'eb3a2d7bc4',
5570 'product_id' => 7959,
5571 'check_in' => 1,
5572 'order_status' => 'yes',
5573 'order_status_label' => 'Going',
5574 'user_id' => 35,
5575 'ticket_sent' => 1,
5576 'event_id' => 7956,
5577 'ticket_name' => 'test test',
5578 'holder_name' => 'bella4 bella4',
5579 'holder_email' => 'bella4@yopmail.com',
5580 'ticket_id' => 7962,
5581 'qr_ticket_id' => 7962,
5582 'security_code' => 'eb3a2d7bc4',
5583 'is_purchaser' => 1,
5584 'ticket_exists' => 1,
5585 ];
5586 $event_data = array_merge( $order, $event_data );
5587 }
5588 } elseif ( 'event_attendee_wc_register' === $term ) {
5589 if ( ! class_exists( 'Tribe__Tickets__Tickets' ) ) {
5590 return [];
5591 }
5592 if ( -1 === $event_id ) {
5593 $args = [
5594 'numberposts' => 1,
5595 'orderby' => 'rand',
5596 'post_type' => 'tribe_events',
5597 ];
5598 $posts = get_posts( $args );
5599 $event_id = $posts[0]->ID;
5600 }
5601 $event_ticket_id = (int) ( isset( $data['filter']['event_ticket_id']['value'] ) ? $data['filter']['event_ticket_id']['value'] : '-1' );
5602 if ( -1 === $event_ticket_id ) {
5603 $tickets = \Tribe__Tickets__Tickets::get_all_event_tickets( $event_id );
5604 $random_key = array_rand( $tickets );
5605 $random_value = $tickets[ $random_key ];
5606 $event_ticket_id = $random_value;
5607 }
5608 $args = [
5609 'post_type' => 'tribe_wooticket',
5610 'orderby' => 'ID',
5611 'order' => 'DESC',
5612 'post_status' => 'publish',
5613 'meta_query' => [
5614 'relation' => 'AND',
5615 [
5616 'key' => '_tribe_wooticket_event',
5617 'value' => $event_id,
5618 'compare' => '=',
5619 ],
5620 [
5621 'key' => '_tribe_wooticket_product',
5622 'value' => $event_ticket_id,
5623 'compare' => '=',
5624 ],
5625 ],
5626 ];
5627
5628 $woo_attendees = get_posts( $args );
5629 if ( ! empty( $woo_attendees ) ) {
5630 $attendee = $woo_attendees[0];
5631 $attendee_id = $attendee->ID;
5632 /**
5633 *
5634 * Ignore line
5635 *
5636 * @phpstan-ignore-next-line
5637 */
5638 $attendee_details = tribe_tickets_get_attendees( $attendee_id, 'rsvp_order' );
5639 foreach ( $attendee_details as $detail ) {
5640 if ( (int) $detail['attendee_id'] !== (int) $attendee_id ) {
5641 continue;
5642 }
5643 $attendee = $detail;
5644 }
5645 $order_id = get_post_meta( $attendee_id, '_tribe_wooticket_order', true );
5646 $product_id = get_post_meta( $attendee_id, '_tribe_wooticket_product', true );
5647 $event_context = TheEventCalendar::get_event_context( $product_id, $order_id );
5648 if ( ! empty( $event_context ) ) {
5649 $event_data = $event_context;
5650 $context['response_type'] = 'live';
5651 }
5652 $context['response_type'] = 'live';
5653 }
5654 }
5655
5656 $context['pluggable_data'] = $event_data;
5657
5658 return $context;
5659 }
5660
5661 /**
5662 * Get last data for trigger
5663 *
5664 * @param array $data data.
5665 * @return array
5666 */
5667 public function search_woo_commerce_triggers_last_data( $data ) {
5668 $context = [];
5669 $context['response_type'] = 'sample';
5670 $context['pluggable_data'] = [];
5671 $user_data = WordPress::get_sample_user_context();
5672
5673 $product_data['product'] = [
5674 'id' => '169',
5675 'name' => 'Sample Product',
5676 'description' => 'This is description of sample product.',
5677 'short_description' => 'This is short description of sample product.',
5678 'image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
5679 'slug' => 'sample-product',
5680 'status' => 'publish',
5681 'type' => 'simple',
5682 'price' => '89',
5683 'featured' => '0',
5684 'sku' => 'hoodie-blue-sm',
5685 'regular_price' => '90',
5686 'sale_price' => '89',
5687 'total_sales' => '21',
5688 'category' => 'Uncategorized',
5689 'tags' => 'sample, new, 2022',
5690 ];
5691
5692 $comment_data = [
5693 'comment_id' => '1',
5694 'comment' => 'This is a sample comment..!',
5695 'comment_author' => 'testsure',
5696 'comment_date' => '2023-06-23 10:10:40',
5697 'comment_author_email' => 'testsure@example.com',
5698 ];
5699
5700 $order_data = [
5701 'order_id' => '500',
5702 'total_order_value' => '45',
5703 'currency' => 'USD',
5704 'shipping_total' => '5',
5705 'order_payment_method' => 'cod',
5706 'billing_firstname' => 'John',
5707 'billing_lastname' => 'Doe',
5708 'billing_company' => 'BSF',
5709 'billing_address_1' => '1004 Beaumont',
5710 'billing_address_2' => '',
5711 'billing_city' => 'Casper',
5712 'billing_state' => 'Wyoming',
5713 'billing_postcode' => '82601',
5714 'billing_country' => 'US',
5715 'billing_email' => 'john_doe@gmail.com',
5716 'billing_phone' => '(307) 7626541',
5717 'shipping_firstname' => 'John',
5718 'shipping_lastname' => 'Doe',
5719 'shipping_company' => 'BSF',
5720 'shipping_address_1' => '1004 Beaumont',
5721 'shipping_address_2' => '',
5722 'shipping_city' => 'Casper',
5723 'shipping_state' => 'Wyoming',
5724 'shipping_postcode' => '82601',
5725 'shipping_country' => 'US',
5726 'coupon_codes' => 'e3mstekq, f24sjakb',
5727 'total_items_in_order' => '1',
5728 'user_id' => '1',
5729 ];
5730
5731 $variation_data = [
5732 'product_variation_id' => '626',
5733 'product_variation' => 'Color: Silver',
5734 ];
5735
5736 $order_sample_data = json_decode( '{"id":37,"parent_id":0,"status":"processing","currency":"USD","version":"7.3.0","prices_include_tax":false,"date_created":{"date":"2023-01-18 08:00:49.000000","timezone_type":1,"timezone":"+00:00"},"date_modified":{"date":"2023-01-18 08:00:50.000000","timezone_type":1,"timezone":"+00:00"},"discount_total":"0","discount_tax":"0","shipping_total":"0","shipping_tax":"0","cart_tax":"0","total":"22.00","total_tax":"0","customer_id":1,"order_key":"wc_order_VdLfjJ9vP7pDs","billing":{"first_name":"John","last_name":"Rana","company":"","address_1":"test","address_2":"","city":"Mohali","state":"AL","postcode":"12344","country":"US","email":"test@example.com","phone":"13232323"},"shipping":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":"","phone":""},"payment_method":"cod","payment_method_title":"Cash on delivery","transaction_id":"","customer_ip_address":"::1","customer_user_agent":"Mozilla\/5.0 (X11; Linux x86_64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/108.0.0.0 Safari\/537.36","created_via":"checkout","customer_note":"","date_completed":null,"date_paid":null,"cart_hash":"10b8e2799df0f88e1506edc0f3ed99c9","order_stock_reduced":true,"download_permissions_granted":true,"new_order_email_sent":true,"recorded_sales":true,"recorded_coupon_usage_counts":true,"number":"37","meta_data":[{"id":204,"key":"is_vat_exempt","value":"no"}],"line_items":{"id":"2, 3","order_id":"37, 37","name":"Variable product - Red, Test product","product_id":"34, 31","variation_id":"35, 0","quantity":"1, 1","tax_class":", ","subtotal":"12, 10","subtotal_tax":"0, 0","total":"12, 10","total_tax":"0, 0","taxes":", ","meta_data":", "},"tax_lines":[],"shipping_lines":[],"fee_lines":[],"coupon_lines":[],"products":[{"id":2,"order_id":37,"name":"Variable product - Red","product_id":34,"variation_id":35,"quantity":1,"tax_class":"","subtotal":"12","subtotal_tax":"0","total":"12","total_tax":"0","taxes":{"total":[],"subtotal":[]},"meta_data":{"19":{"key":"color","value":"Red","display_key":"Color","display_value":"<p>Red<\/p>\n"}}},{"id":3,"order_id":37,"name":"Test product","product_id":31,"variation_id":0,"quantity":1,"tax_class":"","subtotal":"10","subtotal_tax":"0","total":"10","total_tax":"0","taxes":{"total":[],"subtotal":[]},"meta_data":[]}],"quantity":"1, 1","wp_user_id":1,"user_login":"john","display_name":"john smith","user_firstname":"John","user_lastname":"Smith","user_email":"test@example.com","user_role":["subscriber"]}', true ); //phpcs:ignore
5737
5738 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
5739 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
5740
5741 $order_status = ( isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1 );
5742 $from_order_status = ( isset( $data['filter']['from_status']['value'] ) ? $data['filter']['from_status']['value'] : -1 );
5743 $order_note_type = ( isset( $data['filter']['note_type']['value'] ) ? $data['filter']['note_type']['value'] : -1 );
5744
5745 if ( in_array( $term, [ 'product_added_to_cart', 'product_viewed' ], true ) ) {
5746 if ( -1 === $product_id ) {
5747 $args = [
5748 'post_type' => 'product',
5749 'orderby' => 'ID',
5750 'order' => 'DESC',
5751 'post_status' => 'publish',
5752 'numberposts' => 1,
5753 ];
5754 $products = get_posts( $args );
5755
5756 if ( count( $products ) > 0 ) {
5757 $product_id = $products[0]->ID;
5758 }
5759 }
5760
5761 if ( -1 !== $product_id ) {
5762 $post = get_post( $product_id );
5763 $user_data = WordPress::get_user_context( $post->post_author );
5764 $product_data['product_id'] = $product_id;
5765 $product_data['product'] = WooCommerce::get_product_context( $product_id );
5766 $terms = get_the_terms( $product_id, 'product_cat' );
5767 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
5768 $cat_name = [];
5769 foreach ( $terms as $cat ) {
5770 $cat_name[] = $cat->name;
5771 }
5772 $product_data['product']['category'] = implode( ', ', $cat_name );
5773 }
5774 $terms_tags = get_the_terms( $product_id, 'product_tag' );
5775 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
5776 $tag_name = [];
5777 foreach ( $terms_tags as $tag ) {
5778 $tag_name[] = $tag->name;
5779 }
5780 $product_data['product']['tag'] = implode( ', ', $tag_name );
5781 }
5782 unset( $product_data['product']['id'] ); //phpcs:ignore
5783 $context['response_type'] = 'live';
5784 }
5785
5786 if ( 'product_added_to_cart' === $term ) {
5787 $product_data['product_quantity'] = 1;
5788 }
5789
5790 $context['pluggable_data'] = array_merge( $product_data, $user_data );
5791
5792 } elseif ( 'product_reviewed' === $term ) {
5793 $comment_args = [
5794 'number' => 1,
5795 'type' => 'review',
5796 'orderby' => 'comment_ID',
5797 'post_id' => -1 !== $product_id ? $product_id : 0,
5798 ];
5799
5800 $comments = get_comments( $comment_args );
5801
5802 if ( count( $comments ) > 0 ) {
5803 $comment = $comments[0];
5804 $comment_data = [
5805 'comment_id' => $comment->comment_ID,
5806 'comment' => $comment->comment_content,
5807 'comment_author' => $comment->comment_author,
5808 'comment_date' => $comment->comment_date,
5809 'comment_author_email' => $comment->comment_author_email,
5810 ];
5811 $product_data = WooCommerce::get_product_context( $comment->comment_post_ID );
5812 if ( is_object( $comment ) ) {
5813 $terms = get_the_terms( (int) $comment->comment_post_ID, 'product_cat' );
5814 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
5815 $cat_name = [];
5816 foreach ( $terms as $cat ) {
5817 $cat_name[] = $cat->name;
5818 }
5819 $product_data['product']['category'] = implode( ', ', $cat_name );
5820 }
5821 $terms_tags = get_the_terms( (int) $comment->comment_post_ID, 'product_tag' );
5822 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
5823 $tag_name = [];
5824 foreach ( $terms_tags as $tag ) {
5825 $tag_name[] = $tag->name;
5826 }
5827 $product_data['product']['tag'] = implode( ', ', $tag_name );
5828 }
5829 }
5830 $user_data = WordPress::get_user_context( $comment->user_id );
5831 $context['response_type'] = 'live';
5832 }
5833
5834 $context['pluggable_data'] = array_merge( $product_data, $user_data, $comment_data );
5835
5836 } elseif ( 'product_purchased' === $term ) {
5837 $order_id = 0;
5838 $product_data['quantity'] = '1';
5839 if ( -1 !== $product_id ) {
5840 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
5841 if ( count( $order_ids ) > 0 ) {
5842 $order_id = $order_ids[0];
5843 }
5844 } else {
5845 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
5846 if ( count( $orders ) > 0 ) {
5847 $order_id = $orders[0]->get_id();
5848 }
5849 }
5850
5851 if ( 0 !== $order_id ) {
5852 $order = wc_get_order( $order_id );
5853
5854 if ( $order ) {
5855 $user_id = $order->get_customer_id();
5856 $items = $order->get_items();
5857
5858 $product_ids = [];
5859
5860 $iteration = 0;
5861 foreach ( $items as $item ) {
5862 if ( method_exists( $item, 'get_product_id' ) ) {
5863 $item_id = $item->get_product_id();
5864 if ( -1 === $product_id && 0 === $iteration ) {
5865 $product_ids[] = $item_id;
5866 break;
5867 } elseif ( $item_id === $product_id ) {
5868 $product_ids[] = $item_id;
5869 break;
5870 }
5871 }
5872
5873 $iteration++;
5874 }
5875 $order_data = WooCommerce::get_order_context( $order_id );
5876 $user_data = WordPress::get_user_context( $user_id );
5877 $order_data['total_items_in_order'] = count( $product_ids );
5878 $product_data = [];
5879 foreach ( $product_ids as $key => $product_id ) {
5880 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
5881 $terms = get_the_terms( $product_id, 'product_cat' );
5882 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
5883 $cat_name = [];
5884 foreach ( $terms as $cat ) {
5885 $cat_name[] = $cat->name;
5886 }
5887 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
5888 }
5889 $terms_tags = get_the_terms( $product_id, 'product_tag' );
5890 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
5891 $tag_name = [];
5892 foreach ( $terms_tags as $tag ) {
5893 $tag_name[] = $tag->name;
5894 }
5895 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
5896 }
5897 $product = wc_get_product( $product_id );
5898 /**
5899 *
5900 * Ignore line
5901 *
5902 * @phpstan-ignore-next-line
5903 */
5904 if ( $product->is_downloadable() ) {
5905 /**
5906 *
5907 * Ignore line
5908 *
5909 * @phpstan-ignore-next-line
5910 */
5911 foreach ( $product->get_downloads() as $key_download_id => $download ) {
5912 $download_name = $download->get_name();
5913 $download_link = $download->get_file();
5914 $download_id = $download->get_id();
5915 $download_type = $download->get_file_type();
5916 $download_ext = $download->get_file_extension();
5917 $product_data[ 'product' . $key ]['download'] = [
5918 'download_name' => $download_name,
5919 'download_link' => $download_link,
5920 'download_id' => $download_id,
5921 'download_type' => $download_type,
5922 'download_ext' => $download_ext,
5923 ];
5924 }
5925 }
5926 }
5927 $context['response_type'] = 'live';
5928 }
5929 }
5930
5931 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
5932
5933 } elseif ( 'variable_product_purchased' === $term ) {
5934 $product_variation_id = (int) ( isset( $data['filter']['product_variation_id']['value'] ) ? $data['filter']['product_variation_id']['value'] : -1 );
5935 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
5936
5937 foreach ( $order_ids as $order_id ) {
5938 $order = wc_get_order( $order_id );
5939
5940 if ( $order ) {
5941 $user_id = $order->get_customer_id();
5942 $items = $order->get_items();
5943 $product_variations = [];
5944
5945 $iteration = 0;
5946 foreach ( $items as $item ) {
5947 if ( method_exists( $item, 'get_variation_id' ) ) {
5948 $variation_id = $item->get_variation_id();
5949 if ( -1 === $product_variation_id && 0 === $iteration ) {
5950 $product_variations[] = $variation_id;
5951 break;
5952 } elseif ( $variation_id === $product_variation_id ) {
5953 $product_variations[] = $variation_id;
5954 break;
5955 }
5956 }
5957
5958 $iteration++;
5959 }
5960
5961 if ( count( $product_variations ) > 0 ) {
5962 $product_data = WooCommerce::get_product_context( $product_variation_id );
5963 $order_data = WooCommerce::get_order_context( $order_id );
5964 $user_data = WordPress::get_user_context( $user_id );
5965 $variation_data = [
5966 'product_variation_id' => $product_variations[0],
5967 'product_variation' => get_the_excerpt( $product_variations[0] ),
5968 ];
5969
5970 $context['response_type'] = 'live';
5971 break;
5972 }
5973 }
5974 }
5975
5976 $context['pluggable_data'] = array_merge( $order_data, $user_data, $variation_data );
5977
5978 } elseif ( 'variable_subscription_purchased' === $term ) {
5979 $product_data['quantity'] = '1';
5980 $product_data['product_name'] = 'Sample Product';
5981 $product_data['billing_period'] = '2021-2022';
5982
5983 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
5984
5985 $subscription_order_id = 0;
5986 $order_ids = [];
5987
5988 if ( -1 !== $product_id ) {
5989 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
5990
5991 } else {
5992 $orders = wc_get_orders( [] );
5993 if ( count( $orders ) > 0 ) {
5994 $order_ids[] = $orders[0]->get_id();
5995 }
5996 }
5997
5998 foreach ( $order_ids as $order_id ) {
5999 $query_args = [
6000 'post_type' => 'shop_subscription',
6001 'orderby' => 'ID',
6002 'order' => 'DESC',
6003 'post_status' => 'wc-active',
6004 'posts_per_page' => 1,
6005 'post_parent' => $order_id,
6006 ];
6007 $query_result = new WP_Query( $query_args );
6008 $subscription_orders = $query_result->get_posts();
6009
6010 if ( count( $subscription_orders ) > 0 ) {
6011 $subscription_order_id = $subscription_orders[0]->ID;
6012 break;
6013 }
6014 }
6015
6016 if ( 0 !== $subscription_order_id ) {
6017 $subscription = wcs_get_subscription( $subscription_order_id );
6018 if ( $subscription instanceof WC_Subscription ) {
6019 $last_order_id = $subscription->get_last_order();
6020 if ( ! empty( $last_order_id ) && $last_order_id === $subscription->get_parent_id() ) {
6021 $user_id = wc_get_order( $last_order_id )->get_customer_id();
6022 $items = $subscription->get_items();
6023
6024 foreach ( $items as $item ) {
6025 $product = $item->get_product();
6026 if ( class_exists( '\WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product ) ) {
6027 if ( $product->is_type( [ 'subscription', 'subscription_variation', 'variable-subscription' ] ) ) {
6028
6029 $product_data = WooCommerce::get_variable_subscription_product_context( $item, $last_order_id );
6030 $user_data = WordPress::get_user_context( $user_id );
6031
6032 $context['response_type'] = 'live';
6033 $context['pluggable_data'] = array_merge( $product_data, $user_data );
6034 }
6035 }
6036 }
6037 }
6038 }
6039 }
6040 } elseif ( 'order_created' === $term ) {
6041 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
6042 $order_id = '';
6043 if ( count( $orders ) > 0 ) {
6044 $order_id = $orders[0]->get_id();
6045 $order = wc_get_order( $order_id );
6046 $user_id = $order->get_customer_id();
6047 $order_sample_data = array_merge(
6048 WooCommerce::get_order_context( $order_id ),
6049 WordPress::get_user_context( $user_id )
6050 );
6051 $context['response_type'] = 'live';
6052 }
6053
6054 $context['pluggable_data'] = $order_sample_data;
6055
6056 } elseif ( 'order_status_changed' === $term ) {
6057 if ( -1 == $order_status ) {
6058 $args = [
6059 'numberposts' => 1,
6060 'orderby' => 'date',
6061 'order' => 'DESC',
6062 ];
6063 } else {
6064 $args = [
6065 'status' => [ $order_status ],
6066 'numberposts' => 1,
6067 'orderby' => 'date',
6068 'order' => 'DESC',
6069 ];
6070 }
6071 $orders = wc_get_orders( $args );
6072 $order_id = '';
6073 if ( count( $orders ) > 0 ) {
6074 $order_id = $orders[0]->get_id();
6075 $order = wc_get_order( $order_id );
6076 $user_id = $order->get_customer_id();
6077 $items = $order->get_items();
6078 $product_ids = [];
6079 foreach ( $items as $item ) {
6080 $product_ids[] = $item['product_id'];
6081 }
6082 $product_data = [];
6083 foreach ( $product_ids as $key => $product_id ) {
6084 /**
6085 *
6086 * Ignore line
6087 *
6088 * @phpstan-ignore-next-line
6089 */
6090 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
6091 /**
6092 *
6093 * Ignore line
6094 *
6095 * @phpstan-ignore-next-line
6096 */
6097 $terms = get_the_terms( $product_id, 'product_cat' );
6098 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6099 $cat_name = [];
6100 foreach ( $terms as $cat ) {
6101 $cat_name[] = $cat->name;
6102 }
6103 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
6104 }
6105 /**
6106 *
6107 * Ignore line
6108 *
6109 * @phpstan-ignore-next-line
6110 */
6111 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6112 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6113 $tag_name = [];
6114 foreach ( $terms_tags as $tag ) {
6115 $tag_name[] = $tag->name;
6116 }
6117 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
6118 }
6119 }
6120 $order_sample_data = array_merge(
6121 WooCommerce::get_order_context( $order_id ),
6122 $product_data
6123 );
6124 $order_sample_data['user'] = WordPress::get_user_context( $user_id );
6125 $order_sample_data['to_status'] = $order_status;
6126 $order_sample_data['from_status'] = $from_order_status;
6127 $context['response_type'] = 'live';
6128 }
6129
6130 $order_sample_data['to_status'] = $order_status;
6131 $order_sample_data['from_status'] = $from_order_status;
6132 $context['pluggable_data'] = $order_sample_data;
6133 } elseif ( 'order_note_added' === $term ) {
6134 global $wpdb;
6135 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type ='order_note' ORDER BY comment_ID DESC LIMIT 1" );
6136 $order_id = '';
6137 if ( ! empty( $result ) ) {
6138 $order_id = $result[0]->comment_post_ID;
6139 $order = wc_get_order( $order_id );
6140 if ( ! empty( $order ) ) {
6141 $user_id = $order->get_customer_id();
6142 $order_sample_data = array_merge(
6143 WooCommerce::get_order_context( $order_id ),
6144 WordPress::get_user_context( $user_id )
6145 );
6146 if ( -1 == $order_note_type ) {
6147 $args = [
6148 'order_id' => $order_id,
6149 'limit' => 1,
6150 ];
6151 } else {
6152 $args = [
6153 'order_id' => $order_id,
6154 'type' => $order_note_type,
6155 'limit' => 1,
6156 ];
6157 }
6158
6159 $notes = wc_get_order_notes( $args );
6160 if ( ! empty( $notes ) ) {
6161 $order_sample_data['note'] = [
6162 'id' => $notes[0]->id,
6163 'date' => $notes[0]->date_created,
6164 'author' => $notes[0]->added_by,
6165 'content' => $notes[0]->content,
6166 ];
6167 } else {
6168 $order_sample_data['note'] = [
6169 'id' => '1',
6170 'date' => [
6171 'date' => '2023-06-23 10:10:40',
6172 'timezone_type' => 1,
6173 'timezone' => '+00:00',
6174 ],
6175 'author' => 'admin',
6176 'content' => 'new note',
6177 ];
6178 }
6179 $order_sample_data['note_type'] = $order_note_type;
6180 $context['response_type'] = 'live';
6181 }
6182 } else {
6183 $order_sample_data['note'] = [
6184 'id' => '1',
6185 'date' => [
6186 'date' => '2023-06-23 10:10:40',
6187 'timezone_type' => 1,
6188 'timezone' => '+00:00',
6189 ],
6190 'author' => 'admin',
6191 'content' => 'new note',
6192 ];
6193 $order_sample_data['note_type'] = 'customer';
6194 }
6195 $context['pluggable_data'] = $order_sample_data;
6196 } elseif ( 'order_paid' === $term ) {
6197 $args = [
6198 'status' => [ 'completed' ],
6199 'numberposts' => 1,
6200 ];
6201 $orders = wc_get_orders( $args );
6202 $order_id = '';
6203 if ( count( $orders ) > 0 ) {
6204 $order_id = $orders[0]->get_id();
6205 $order = wc_get_order( $order_id );
6206 $user_id = $order->get_customer_id();
6207 $order_sample_data = array_merge(
6208 WooCommerce::get_order_context( $order_id ),
6209 WordPress::get_user_context( $user_id )
6210 );
6211 $context['response_type'] = 'live';
6212 }
6213 $context['pluggable_data'] = $order_sample_data;
6214 } elseif ( 'product_category_purchased' === $term ) {
6215 $product_category_id = (int) ( isset( $data['filter']['product_category_id']['value'] ) ? $data['filter']['product_category_id']['value'] : -1 );
6216 $args = [
6217 'post_status' => 'publish',
6218 'product_category_id' => [ $product_category_id ],
6219 'return' => 'ids',
6220 ];
6221 $products = wc_get_products( $args );
6222 if ( ! empty( $products ) ) {
6223 $order_id = 0;
6224 $product_data['quantity'] = '1';
6225 $orders = wc_get_orders(
6226 [
6227 'status' => 'any',
6228 ]
6229 );
6230 $filtered_orders = [];
6231 if ( ! empty( $orders ) ) {
6232 foreach ( $orders as $order ) {
6233 $order_items = $order->get_items();
6234 foreach ( $order_items as $item ) {
6235 if ( method_exists( $item, 'get_product_id' ) ) {
6236 $product_id = $item->get_product_id();
6237 }
6238 if ( is_array( $products ) && in_array( $product_id, $products ) ) {
6239 $filtered_orders[] = $order;
6240 break;
6241 }
6242 }
6243 }
6244 }
6245 if ( ! empty( $filtered_orders ) ) {
6246 if ( count( $filtered_orders ) > 0 ) {
6247 $order_id = $filtered_orders[0]->get_id();
6248 }
6249 if ( 0 !== $order_id ) {
6250 $order = wc_get_order( $order_id );
6251 if ( $order instanceof WC_Order ) {
6252 $user_id = $order->get_customer_id();
6253 $items = $order->get_items();
6254 $product_ids = [];
6255 $iteration = 0;
6256 foreach ( $items as $item ) {
6257 if ( method_exists( $item, 'get_product_id' ) ) {
6258 $item_id = $item->get_product_id();
6259 if ( -1 === $product_id && 0 === $iteration ) {
6260 $product_ids[] = $item_id;
6261 break;
6262 } elseif ( $item_id === $product_id ) {
6263 $product_ids[] = $item_id;
6264 break;
6265 }
6266 }
6267 $iteration++;
6268 }
6269 $order_data = WooCommerce::get_order_context( $order_id );
6270 $user_data = WordPress::get_user_context( $user_id );
6271 $order_data['total_items_in_order'] = count( $product_ids );
6272 $product_data = [];
6273 $category_ids = [];
6274 foreach ( $product_ids as $key => $product_id ) {
6275 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
6276 $terms = get_the_terms( $product_id, 'product_cat' );
6277 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6278 $cat_name = [];
6279 foreach ( $terms as $cat ) {
6280 $cat_name[] = $cat->name;
6281 $category_ids[] = $cat->term_id;
6282 }
6283 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
6284 }
6285 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6286 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6287 $tag_name = [];
6288 foreach ( $terms_tags as $tag ) {
6289 $tag_name[] = $tag->name;
6290 }
6291 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
6292 }
6293 $product = wc_get_product( $product_id );
6294 /**
6295 *
6296 * Ignore line
6297 *
6298 * @phpstan-ignore-next-line
6299 */
6300 if ( $product->is_downloadable() ) {
6301 /**
6302 *
6303 * Ignore line
6304 *
6305 * @phpstan-ignore-next-line
6306 */
6307 foreach ( $product->get_downloads() as $key_download_id => $download ) {
6308 $download_name = $download->get_name();
6309 $download_link = $download->get_file();
6310 $download_id = $download->get_id();
6311 $download_type = $download->get_file_type();
6312 $download_ext = $download->get_file_extension();
6313 $product_data[ 'product' . $key ]['download'] = [
6314 'download_name' => $download_name,
6315 'download_link' => $download_link,
6316 'download_id' => $download_id,
6317 'download_type' => $download_type,
6318 'download_ext' => $download_ext,
6319 ];
6320 }
6321 }
6322 }
6323 $context['response_type'] = 'live';
6324 }
6325 }
6326 }
6327 }
6328 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
6329 if ( ! empty( $category_ids ) ) {
6330 foreach ( $category_ids as $category_id ) {
6331 $context['pluggable_data']['product_category_id'] = $category_id;
6332 }
6333 } else {
6334 $context['pluggable_data']['product_category_id'] = 1;
6335 }
6336 }
6337
6338 return $context;
6339 }
6340
6341 /**
6342 * Search LMS data.
6343 *
6344 * @param array $data data.
6345 * @return array
6346 */
6347 public function search_lifter_lms_last_data( $data ) {
6348 global $wpdb;
6349 $post_type = $data['post_type'];
6350 $meta_key = '_is_complete';
6351 $trigger = $data['search_term'];
6352 $context = [];
6353 $post_id = -1;
6354
6355 if ( ! class_exists( 'LLMS_Section' ) ) {
6356 return [];
6357 }
6358
6359 if ( 'lifterlms_purchase_course' === $trigger ) {
6360 $product_type = 'course';
6361 $post_id = $data['filter']['course_id']['value'];
6362 } elseif ( 'lifterlms_purchase_membership' === $trigger ) {
6363 $product_type = 'membership';
6364 $post_id = $data['filter']['membership_id']['value'];
6365 } elseif ( 'lifterlms_cancel_membership' === $trigger ) {
6366 $product_type = 'membership';
6367 $post_id = $data['filter']['membership_id']['value'];
6368 } elseif ( 'lifterlms_lesson_completed' === $trigger ) {
6369 $post_id = $data['filter']['lesson']['value'];
6370 } elseif ( 'lifterlms_course_completed' === $trigger || 'lifterlms_course_enrolled' === $trigger || 'lifterlms_course_user_removed' === $trigger ) {
6371 $post_id = $data['filter']['course']['value'];
6372 } elseif ( 'lifterlms_section_completed' === $trigger ) {
6373 $post_id = $data['filter']['section']['value'];
6374 }
6375
6376 $where = 'postmeta.post_id = "' . $post_id . '" AND';
6377
6378 if ( 'llms_order' === $post_type ) {
6379 if ( -1 === $post_id ) {
6380 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts as posts JOIN {$wpdb->prefix}postmeta as postmeta ON posts.ID=postmeta.post_id WHERE posts.post_type ='llms_order' AND posts.post_status= 'llms-completed' AND postmeta.meta_value=%s AND postmeta.meta_key= '_llms_product_type'", $product_type ) );
6381 } else {
6382 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts as posts JOIN {$wpdb->prefix}postmeta as postmeta ON posts.ID=postmeta.post_id WHERE posts.post_type ='llms_order' AND posts.post_status= 'llms-completed' AND postmeta.meta_value=%s AND postmeta.meta_key= '_llms_product_id'", $post_id ) );
6383 }
6384 } elseif ( 'lifterlms_course_enrolled' === $trigger ) {
6385 if ( -1 === $post_id ) {
6386 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.post_id WHERE postmeta.meta_value='enrolled' AND postmeta.meta_key=%s AND posts.post_type=%s ORDER BY postmeta.meta_id DESC", '_status', $post_type ) );
6387 } else {
6388 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.post_id WHERE postmeta.post_id = %s AND postmeta.meta_value='enrolled' AND postmeta.meta_key=%s AND posts.post_type=%s ORDER BY postmeta.meta_id DESC", $post_id, '_status', $post_type ) );
6389 }
6390 } elseif ( 'lifterlms_course_user_removed' === $trigger ) {
6391 if ( -1 === $post_id ) {
6392 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.post_id WHERE postmeta.meta_value='cancelled' AND postmeta.meta_key=%s AND posts.post_type=%s ORDER BY postmeta.meta_id DESC", '_status', $post_type ) );
6393 } else {
6394 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.post_id WHERE postmeta.post_id = %s AND postmeta.meta_value='cancelled' AND postmeta.meta_key=%s AND posts.post_type=%s ORDER BY postmeta.meta_id DESC", $post_id, '_status', $post_type ) );
6395 }
6396 } elseif ( 'lifterlms_cancel_membership' === $trigger ) {
6397 if ( -1 === $post_id ) {
6398 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.post_id WHERE postmeta.meta_value='cancelled' AND postmeta.meta_key=%s AND posts.post_type=%s ORDER BY postmeta.meta_id DESC", '_status', $post_type ) );
6399 } else {
6400 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.post_id WHERE postmeta.post_id = %s AND postmeta.meta_value='cancelled' AND postmeta.meta_key=%s AND posts.post_type=%s ORDER BY postmeta.meta_id DESC", $post_id, '_status', $post_type ) );
6401 }
6402 } else {
6403 if ( -1 === $post_id ) {
6404 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.post_id WHERE postmeta.meta_value='yes' AND postmeta.meta_key=%s AND posts.post_type=%s", $meta_key, $post_type ) );
6405 } else {
6406 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.post_id WHERE postmeta.post_id = %s AND postmeta.meta_value='yes' AND postmeta.meta_key=%s AND posts.post_type=%s", $post_id, $meta_key, $post_type ) );
6407 }
6408 }
6409
6410 $response = [];
6411 if ( ! empty( $result ) ) {
6412 $result_post_id = $result[0]->post_id;
6413 $result_user_id = $result[0]->user_id;
6414
6415 switch ( $trigger ) {
6416 case 'lifterlms_lesson_completed':
6417 $context = array_merge(
6418 WordPress::get_user_context( $result_user_id ),
6419 LifterLMS::get_lms_lesson_context( $result_post_id )
6420 );
6421
6422 $context['course'] = get_the_title( get_post_meta( $result_post_id, '_llms_parent_course', true ) );
6423 if ( '' !== ( get_post_meta( $result_post_id, '_llms_parent_section', true ) ) ) {
6424 $context['parent_section'] = get_the_title( get_post_meta( $result_post_id, '_llms_parent_section', true ) );
6425 }
6426 break;
6427 case 'lifterlms_course_enrolled':
6428 case 'lifterlms_course_user_removed':
6429 case 'lifterlms_course_completed':
6430 $context = array_merge(
6431 WordPress::get_user_context( $result_user_id ),
6432 LifterLMS::get_lms_course_context( $result_post_id )
6433 );
6434 break;
6435 case 'lifterlms_section_completed':
6436 $data = new \LLMS_Section( $result_post_id );
6437 $lessons = $data->get_lessons();
6438 $context = array_merge(
6439 WordPress::get_user_context( $result_user_id ),
6440 WordPress::get_post_context( $result_post_id )
6441 );
6442 $context['parent_course'] = $data->get( 'parent_course' );
6443 $context['parent_course_title'] = get_the_title( $data->get( 'parent_course' ) );
6444 if ( ! empty( $lessons ) ) {
6445 foreach ( $lessons as $key => $lesson ) {
6446 $context['section_lesson'][ $key ] = $lesson->id;
6447 $context['section_lesson_title'][ $key ] = get_the_title( $lesson->id );
6448 }
6449 }
6450 $context['section_course'] = $data->get( 'parent_course' );
6451 $context['parent_course_title'] = get_the_title( $data->get( 'parent_course' ) );
6452 break;
6453 case 'lifterlms_purchase_course':
6454 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
6455 $context['course_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
6456 $context['course_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
6457 $context['course_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
6458 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
6459 $context ['order'] = WordPress::get_post_context( $result_post_id );
6460 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
6461 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
6462 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
6463 $context = array_merge( $context, WordPress::get_user_context( $user_id ) );
6464 break;
6465 case 'lifterlms_purchase_membership':
6466 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
6467 $context['membership_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
6468 $context['membership_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
6469 $context['membership_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
6470 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
6471 $context ['order'] = WordPress::get_post_context( $result_post_id );
6472 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
6473 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
6474 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
6475 $context = array_merge( $context, WordPress::get_user_context( $user_id ) );
6476 break;
6477 case 'lifterlms_cancel_membership':
6478 $context = array_merge( WordPress::get_post_context( $result_post_id ), WordPress::get_user_context( $result[0]->user_id ) );
6479 $context['membership_id'] = $result_post_id;
6480 $context['membership_name'] = get_the_title( $result_post_id );
6481 break;
6482 default:
6483 return;
6484
6485 }
6486 $response['pluggable_data'] = $context;
6487 $response['response_type'] = 'live';
6488
6489 }
6490
6491 return $response;
6492
6493 }
6494
6495 /**
6496 * Search SM data.
6497 *
6498 * @param array $data data.
6499 * @return array
6500 */
6501 public function search_suremember_last_data( $data ) {
6502 global $wpdb;
6503 $post_type = $data['post_type'];
6504 $meta_key = '_is_complete';
6505 $trigger = $data['search_term'];
6506 $post_id = $data['filter']['group_id']['value'];
6507
6508 if ( 'suremember_updated_group' === $trigger ) {
6509 if ( -1 === $post_id ) {
6510 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts as posts WHERE posts.post_type=%s", $post_type ) );
6511 } else {
6512 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts as posts WHERE posts.ID=%s AND posts.post_type=%s", $post_id, $post_type ) );
6513 }
6514 } else {
6515 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}usermeta as usermeta WHERE usermeta.meta_key = %s", 'suremembers_user_access_group_' . $post_id ) );
6516 }
6517
6518 $response = [];
6519
6520 if ( ! empty( $result ) ) {
6521 $context = [];
6522 switch ( $trigger ) {
6523 case 'suremember_updated_group':
6524 $group_id = $result[0]->ID;
6525 $suremembers_post['rules'] = get_post_meta( $group_id, 'suremembers_plan_include', true );
6526 $suremembers_post['exclude'] = get_post_meta( $group_id, 'suremembers_plan_exclude', true ); //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
6527 $suremembers_post['suremembers_user_roles'] = get_post_meta( $group_id, 'suremembers_user_roles', true );
6528 $suremembers_post['title'] = get_the_title( $group_id );
6529 $suremembers_post['restrict'] = get_post_meta( $group_id, 'suremembers_plan_rules', true )['restrict'];
6530 $context['group'] = array_merge( WordPress::get_post_context( $group_id ), $suremembers_post );
6531 $context['group_id'] = $group_id;
6532 unset( $context['group']['ID'] );
6533 $response['pluggable_data'] = $context;
6534 $response['response_type'] = 'live';
6535 break;
6536 case 'suremember_user_added_in_group':
6537 foreach ( $result as $res ) {
6538 $meta_value = unserialize( $res->meta_value );
6539 if ( 'active' === $meta_value['status'] ) {
6540 $context = WordPress::get_user_context( $res->user_id );
6541 $context['group'] = WordPress::get_post_context( $post_id );
6542 $context['group_id'] = $post_id;
6543 unset( $context['group']['ID'] );
6544 $response['pluggable_data'] = $context;
6545 $response['response_type'] = 'live';
6546 }
6547 }
6548 break;
6549 case 'suremember_user_removed_from_group':
6550 foreach ( $result as $res ) {
6551 $meta_value = unserialize( $res->meta_value );
6552 if ( 'revoked' === $meta_value['status'] ) {
6553 $context = WordPress::get_user_context( $res->user_id );
6554 $context['group'] = WordPress::get_post_context( $post_id );
6555 $context['group_id'] = $post_id;
6556 unset( $context['group']['ID'] );
6557 $response['pluggable_data'] = $context;
6558 $response['response_type'] = 'live';
6559 }
6560 }
6561 break;
6562 default:
6563 return;
6564
6565 }
6566 }
6567
6568 return $response;
6569
6570 }
6571
6572 /**
6573 * Search CartFlows data.
6574 *
6575 * @param array $data data.
6576 * @return array
6577 */
6578 public function search_cartflows_last_data( $data ) {
6579 global $wpdb;
6580 $trigger = $data['search_term'];
6581 $context = [];
6582 if ( 'cartflows_offer_accepted' === $trigger ) {
6583 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts as posts JOIN {$wpdb->prefix}postmeta as postmeta ON posts.ID=postmeta.post_id WHERE posts.post_type ='shop_order' AND postmeta.meta_value='upsell' AND postmeta.meta_key= '_cartflows_offer_type'" );
6584 }
6585 $response = [];
6586 if ( ! empty( $result ) ) {
6587 $context = [];
6588 $order_upsell_id = $result[0]->post_id;
6589 $step_id = get_post_meta( $order_upsell_id, '_cartflows_offer_step_id', true );
6590 $order_id = get_post_meta( $order_upsell_id, '_cartflows_offer_parent_id', true );
6591 $order = wc_get_order( $order_id );
6592 $upsell_order = wc_get_order( $order_upsell_id );
6593 $variation_id = $upsell_order->get_items()[0]['product_id'];
6594 $input_qty = $upsell_order->get_items()[0]['quantity'];
6595 $offer_product = wcf_pro()->utils->get_offer_data( $step_id, $variation_id, $input_qty, $order_id );
6596 $user_id = get_post_meta( $order_upsell_id, '_customer_user', true );
6597 $context = WordPress::get_user_context( $user_id );
6598 $context['order'] = $order->get_data();
6599 $context['upsell'] = $offer_product;
6600 $response['pluggable_data'] = $context;
6601 $response['response_type'] = 'live';
6602 }
6603
6604 return $response;
6605
6606 }
6607
6608
6609 /**
6610 * Fetch user context.
6611 *
6612 * @param int $initiator_id initiator id.
6613 * @param int $friend_id friend id.
6614 * @return array
6615 */
6616 public function get_user_context( $initiator_id, $friend_id ) {
6617 $context = WordPress::get_user_context( $initiator_id );
6618
6619 $friend_context = WordPress::get_user_context( $friend_id );
6620
6621 $avatar = get_avatar_url( $initiator_id );
6622
6623 $context['avatar_url'] = ( $avatar ) ? $avatar : '';
6624
6625 $context['friend_id'] = $friend_id;
6626 $context['friend_first_name'] = $friend_context['user_firstname'];
6627 $context['friend_last_name'] = $friend_context['user_lastname'];
6628 $context['friend_email'] = $friend_context['user_email'];
6629
6630 $friend_avatar = get_avatar_url( $friend_id );
6631 $context['friend_avatar_url'] = $friend_avatar;
6632 return $context;
6633 }
6634
6635 /**
6636 * Search BP data.
6637 *
6638 * @param array $data data.
6639 * @return array
6640 */
6641 public function search_pluggables_bp_friendships( $data ) {
6642 global $wpdb, $bp;
6643 $context = [];
6644 $sample['pluggable_data'] = [
6645 'wp_user_id' => 4,
6646 'user_login' => 'katy1ßßßß',
6647 'display_name' => 'Katy Smith',
6648 'user_firstname' => 'Katy',
6649 'user_lastname' => 'Smith',
6650 'user_email' => 'katy1@gmail.com',
6651 'user_role' => [ 'subscriber' ],
6652 'avatar_url' => 'http://pqr.com/avatar',
6653 'friend_id' => 1,
6654 'friend_first_name' => 'John',
6655 'friend_last_name' => 'Wick',
6656 'friend_email' => 'john@gmail.com',
6657 'friend_avatar_url' => 'http://abc.com/avatar',
6658 ];
6659 $sample['response_type'] = 'sample';
6660
6661 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_friends'" );
6662 if ( $table_exists ) {
6663 $friendships = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_friends LIMIT 1" );
6664 if ( ! empty( $friendships ) ) {
6665 $friendship = $friendships[0];
6666 $initiator_id = $friendship->initiator_user_id;
6667 $friend_user_id = $friendship->friend_user_id;
6668 $context['pluggable_data'] = $this->get_user_context( $initiator_id, $friend_user_id );
6669 $context['response_type'] = 'live';
6670 } else {
6671 $context = $sample;
6672 }
6673 } else {
6674 $context = $sample;
6675 }
6676
6677
6678 return $context;
6679 }
6680
6681 /**
6682 * Search Buddyboss profile types data.
6683 *
6684 * @param array $data data.
6685 * @return array
6686 */
6687 public function search_pluggables_bp_profile_types( $data ) {
6688 global $wpdb, $bp;
6689 $context = [];
6690 $sample['pluggable_data'] = [
6691 'wp_user_id' => 4,
6692 'user_login' => 'katy1ßßßß',
6693 'display_name' => 'Katy Smith',
6694 'user_firstname' => 'Katy',
6695 'user_lastname' => 'Smith',
6696 'user_email' => 'katy1@gmail.com',
6697 'user_role' => [ 'subscriber' ],
6698 'bb_profile_type' => '10',
6699 'bb_profile_type_name' => 'student',
6700 ];
6701 $sample['response_type'] = 'sample';
6702
6703 $post_id = $data['filter']['bb_profile_type']['value'];
6704 $get_existing = get_post_meta( $post_id, '_bp_member_type_key', true );
6705
6706 $type_term = get_term_by(
6707 'name',
6708 /**
6709 *
6710 * Ignore line
6711 *
6712 * @phpstan-ignore-next-line
6713 */
6714 $get_existing,
6715 'bp_member_type'
6716 );
6717
6718 $results = $wpdb->get_results(
6719 $wpdb->prepare(
6720 "SELECT u.ID FROM {$wpdb->prefix}users u INNER JOIN {$wpdb->prefix}term_relationships r
6721 ON u.ID = r.object_id WHERE u.user_status = 0 AND
6722 r.term_taxonomy_id = %d ORDER BY RAND() LIMIT 1",
6723 /**
6724 *
6725 * Ignore line
6726 *
6727 * @phpstan-ignore-next-line
6728 */
6729 $type_term->term_id
6730 )
6731 );
6732
6733 if ( ! empty( $results ) ) {
6734 $user = $results[0];
6735 $context['pluggable_data'] = WordPress::get_user_context( $user->ID );
6736 $context['pluggable_data']['bb_profile_type'] = $post_id;
6737 $context['pluggable_data']['bb_profile_type_name'] = get_post_meta( $post_id, '_bp_member_type_label_singular_name', true );
6738 $context['response_type'] = 'live';
6739 } else {
6740 $context = $sample;
6741 }
6742
6743 return $context;
6744 }
6745
6746 /**
6747 * Search BP User data.
6748 *
6749 * @param array $data data.
6750 * @return array
6751 */
6752 public function search_pluggables_bb_users( $data ) {
6753 global $wpdb;
6754 $context = [];
6755
6756 if ( ! class_exists( 'BP_Signup' ) ) {
6757 return [];
6758 }
6759
6760 $term = $data['search_term'];
6761
6762 if ( 'account_activated' === $term ) {
6763 $signups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}signups WHERE active = 1 ORDER BY signup_id DESC LIMIT 1" );
6764 } elseif ( 'updates_profile' === $term ) {
6765 $custom_ids = $wpdb->get_var( "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data ORDER BY last_updated DESC LIMIT 1" );
6766 $args = [ 'include' => $custom_ids ];
6767 $users = get_users( $args );
6768 } elseif ( 'gains_follower' === $term ) {
6769 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_follow ORDER BY id DESC LIMIT 1" );
6770 }
6771
6772 if ( ! empty( $signups ) ) {
6773 $pluggable_data = $signups[0];
6774 $pluggable_data = get_object_vars( $pluggable_data );
6775 unset( $pluggable_data['activation_key'] );
6776 if ( is_string( $pluggable_data['meta'] ) ) {
6777 $pluggable_data['meta'] = unserialize( $pluggable_data['meta'] );
6778 }
6779 if ( is_array( $pluggable_data['meta'] ) ) {
6780 unset( $pluggable_data['meta']['password'] );
6781 }
6782 $context['pluggable_data'] = $pluggable_data;
6783 $context['response_type'] = 'live';
6784 } elseif ( 'updates_profile' === $term ) {
6785 if ( ! empty( $users ) ) {
6786 $user = $users[0];
6787 $fields = $wpdb->get_results( $wpdb->prepare( "SELECT field_id, value FROM {$wpdb->prefix}bp_xprofile_data WHERE user_id = %d", $user->ID ) );
6788 $user_data = WordPress::get_user_context( $user->ID );
6789 $pluggable_data['user_id'] = $user->ID;
6790 $pluggable_data['user_email'] = $user_data['user_email'];
6791 foreach ( $user_data['user_role'] as $key => $role ) {
6792 $pluggable_data['user_role'][ $key ] = $role;
6793 }
6794 foreach ( $fields as $field ) {
6795 if ( function_exists( 'xprofile_get_field' ) ) {
6796 $fieldj = xprofile_get_field( $field->field_id );
6797 $pluggable_data[ $fieldj->name ] = $field->value;
6798 }
6799 }
6800 $context['pluggable_data'] = $pluggable_data;
6801 $context['response_type'] = 'live';
6802 } else {
6803 $context['pluggable_data'] = [
6804 'user_login' => 'john',
6805 'display_name' => 'john',
6806 'user_firstname' => 'john',
6807 'user_lastname' => 'd',
6808 'user_email' => 'johnd@yopmail.com',
6809 'user_role' => [
6810 'subscriber',
6811 ],
6812 'Name' => 'john',
6813 'Nickname' => 'johnd',
6814 'wp_user_id' => 16,
6815 ];
6816 $context['response_type'] = 'sample';
6817 }
6818 } elseif ( 'gains_follower' === $term ) {
6819 if ( ! empty( $results ) ) {
6820 $pluggable_data['follower'] = WordPress::get_user_context( $results[0]->follower_id );
6821 $pluggable_data['leader'] = WordPress::get_user_context( $results[0]->leader_id );
6822 $context['pluggable_data'] = $pluggable_data;
6823 $context['response_type'] = 'live';
6824 } else {
6825 $context['pluggable_data'] = [
6826 'follower' => [
6827 'wp_user_id' => 126,
6828 'user_login' => 'belli',
6829 'display_name' => 'belli',
6830 'user_firstname' => 'test',
6831 'user_lastname' => 'test',
6832 'user_email' => 'belli@gmail.com',
6833 'user_role' => [
6834 'subscriber',
6835 'wpamelia-customer',
6836 ],
6837 ],
6838 'leader' => [
6839 'wp_user_id' => 34,
6840 'user_login' => 'bella3@gmail.com',
6841 'display_name' => 'bella3@gmail.com',
6842 'user_firstname' => 'bella3',
6843 'user_lastname' => 'bella3',
6844 'user_email' => 'bellaaa3@gmail.com',
6845 'user_role' => [
6846 'wpamelia-customer',
6847 ],
6848 ],
6849 ];
6850 $context['response_type'] = 'sample';
6851 }
6852 } else {
6853 $context['pluggable_data'] = [
6854 'signup_id' => '16',
6855 'domain' => '',
6856 'path' => '',
6857 'title' => '',
6858 'user_login' => 'johnd',
6859 'user_email' => 'johnd@yopmail.com',
6860 'registered' => '2024-01-29 04:52:13',
6861 'activated' => '0000-00-00 00:00:00',
6862 'active' => '0',
6863 'meta' => [
6864 'field_1' => 'john',
6865 'field_3' => 'd',
6866 'field_4' => '09878988766',
6867 'field_2' => '123 Main Street',
6868 'field_5' => 'johnd',
6869 'profile_field_ids' => '1,3,4,2,5',
6870 ],
6871 'id' => 16,
6872 'user_name' => 'johnd',
6873 'date_sent' => '2024-01-29 04:52:13',
6874 'recently_sent' => true,
6875 'count_sent' => 1,
6876 ];
6877 $context['response_type'] = 'sample';
6878 }
6879
6880 return $context;
6881 }
6882
6883 /**
6884 * Search BP data.
6885 *
6886 * @param array $data data.
6887 * @return array
6888 */
6889 public function search_pluggables_bp_groups( $data ) {
6890 global $wpdb, $bp;
6891 $context = [];
6892 $group_data = [];
6893 $args = [
6894 'orderby' => 'user_nicename',
6895 'order' => 'ASC',
6896 'number' => 1,
6897 ];
6898
6899 $term = $data['search_term'];
6900
6901 if ( ! function_exists( 'groups_get_group' ) || ! function_exists( 'bp_groups_get_group_types' ) || ! function_exists( 'bp_groups_get_group_type' ) || ! function_exists( 'bp_get_group_cover_url' ) || ! function_exists( 'bp_get_group_avatar_url' ) || ! function_exists( 'groups_get_group_members' ) || ! function_exists( 'groups_get_invites' ) ) {
6902 return [];
6903 }
6904
6905 if ( 'user_joins_specific_type_group' == $term ) {
6906 if ( -1 !== $data['filter']['group_type']['value'] ) {
6907 $group_type = $data['filter']['group_type']['value'];
6908 } else {
6909 $registered_types = bp_groups_get_group_types();
6910 $random_key = array_rand( $registered_types );
6911 $random_value = $registered_types[ $random_key ];
6912 $group_type = $random_value;
6913 }
6914 if ( function_exists( 'bp_get_group_ids_by_group_types' ) ) {
6915 $group_ids = bp_get_group_ids_by_group_types( $group_type );
6916 $random_key = array_rand( $group_ids );
6917 $random_value = $group_ids[ $random_key ];
6918 $group_id = $random_value;
6919 if ( function_exists( 'groups_get_group' ) ) {
6920 $group = groups_get_group( $group_id['id'] );
6921 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
6922 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
6923 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
6924 $group_data['group_type'] = $group_type;
6925 if ( function_exists( 'groups_get_group_members' ) ) {
6926 $members = groups_get_group_members(
6927 [
6928 'group_id' => $group_id,
6929 ]
6930 );
6931 $ids = [];
6932 foreach ( $members['members'] as $member ) {
6933 $ids[] = $member->ID;
6934 }
6935 $args = [
6936 'number' => 1,
6937 'include' => $ids,
6938 ];
6939 $users = get_users( $args );
6940 }
6941 }
6942 }
6943 } elseif ( 'requests_access_private_group' === $data['search_term'] ) {
6944 $group_id = $data['filter']['group_id']['value'];
6945 if ( $group_id > 0 ) {
6946 $results = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, item_id FROM {$wpdb->prefix}bp_invitations WHERE type LIKE %s AND item_id = %d ORDER BY id DESC LIMIT 1", 'request', $group_id ) );
6947 } else {
6948 $results = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, item_id FROM {$wpdb->prefix}bp_invitations WHERE type = %s ORDER BY id DESC LIMIT 1", 'request' ) );
6949 }
6950 $custom_ids = $results[0]->user_id;
6951 $args = [ 'include' => $custom_ids ];
6952 $users = get_users( $args );
6953 $group_id = $results[0]->item_id;
6954 $group = groups_get_group( $group_id );
6955 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
6956 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
6957 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
6958 } elseif ( 'bb_group_created' == $term ) {
6959 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups ORDER BY id DESC LIMIT 1" );
6960 if ( function_exists( 'groups_get_group' ) ) {
6961 $group = groups_get_group( $results[0]->id );
6962 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
6963 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
6964 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
6965 $current_types = (array) bp_groups_get_group_type( $results[0]->id, false );
6966 $group_data['group_type'] = $current_types;
6967 $group_data['group_status'] = $group->status;
6968 $group_data['group_date_created'] = $group->date_created;
6969 $group_data['group_enabled_forum'] = $group->enable_forum;
6970 $group_data['group_cover_url'] = bp_get_group_cover_url( $group );
6971 $group_data['group_avatar_url'] = bp_get_group_avatar_url( $group );
6972 $group_data['group_creator'] = WordPress::get_user_context( $group->creator_id );
6973 if ( function_exists( 'groups_get_group_members' ) ) {
6974 $members = groups_get_group_members(
6975 [
6976 'group_id' => $results[0]->id,
6977 ]
6978 );
6979 foreach ( $members['members'] as $key => $member ) {
6980 $group_data['group_member'][ $key ] = WordPress::get_user_context( $member->ID );
6981 }
6982 }
6983 $args = [
6984 'item_id' => $results[0]->id,
6985 ];
6986 $invitations = groups_get_invites( $args );
6987 if ( ! empty( $invitations ) ) {
6988 foreach ( $invitations as $key => $invite ) {
6989 $group_data['invitation'][ $key ] = $invite;
6990 }
6991 }
6992 }
6993 } else {
6994 $users = get_users( $args );
6995
6996 if ( isset( $data['filter']['group_id']['value'] ) ) {
6997 $group_id = $data['filter']['group_id']['value'];
6998 $args['group_id'] = $group_id;
6999 if ( $group_id > 0 ) {
7000 $group = groups_get_group( $group_id );
7001 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
7002 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
7003 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
7004 } else {
7005 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_groups'" );
7006 if ( $table_exists ) {
7007 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups LIMIT 1" );
7008 $context['groups'] = $groups;
7009 if ( ! empty( $groups ) ) {
7010 foreach ( $groups as $group ) {
7011 $group_data['group_id'] = $group->id;
7012 $group_data['group_name'] = $group->name;
7013 $group_data['group_description'] = $group->description;
7014 }
7015 }
7016 }
7017 }
7018 }
7019 }
7020
7021 if ( 'bb_group_created' == $term ) {
7022 if ( ! empty( $group_data ) ) {
7023 $pluggable_data = $group_data;
7024 $context['pluggable_data'] = $pluggable_data;
7025 $context['response_type'] = 'live';
7026 } else {
7027 $context['pluggable_data'] = [
7028 'group_id' => 112,
7029 'group_name' => 'New Group',
7030 'group_description' => 'New Group Description',
7031 'group_type' => [ 'business' ],
7032 'group_status' => 'public',
7033 'group_date_created' => '2024-02-16 05:37:22',
7034 'group_enabled_forum' => 1,
7035 'group_cover_url' => 'https:\/\/example.com\/wp-content\/uploads\/buddypress\/groups\/23\/cover-image\/65cef4c3ea9b6-bp-cover-image.jpeg',
7036 'group_avatar_url' => 'https:\/\/example.com\/wp-content\/uploads\/group-avatars\/23\/65cef4d7cee19-bpfull.png',
7037 'group_creator' => [
7038 'wp_user_id' => 183,
7039 'user_login' => 'johnd',
7040 'display_name' => 'john',
7041 'user_firstname' => 'john',
7042 'user_lastname' => 'd',
7043 'user_email' => 'johnd@yopmail.com',
7044 'user_role' => [
7045 'subscriber',
7046 'bbp_participant',
7047 ],
7048 ],
7049 ];
7050 $context['response_type'] = 'sample';
7051 }
7052 } elseif ( ! empty( $users ) ) {
7053 $user = $users[0];
7054 $pluggable_data = $group_data;
7055
7056 $avatar = get_avatar_url( $user->ID );
7057 $pluggable_data['wp_user_id'] = $user->ID;
7058 $pluggable_data['avatar_url'] = ( $avatar ) ? $avatar : '';
7059 $pluggable_data['user_login'] = $user->user_login;
7060 $pluggable_data['display_name'] = $user->display_name;
7061 $pluggable_data['user_firstname'] = $user->user_firstname;
7062 $pluggable_data['user_lastname'] = $user->user_lastname;
7063 $pluggable_data['user_email'] = $user->user_email;
7064 $pluggable_data['user_role'] = $user->roles;
7065 $context['pluggable_data'] = $pluggable_data;
7066 $context['response_type'] = 'live';
7067 } else {
7068 $context['pluggable_data'] = [
7069 'wp_user_id' => 1,
7070 'user_login' => 'admin',
7071 'display_name' => 'Johnd',
7072 'user_firstname' => 'John',
7073 'user_lastname' => 'D',
7074 'user_email' => 'johnd@gmail.com',
7075 'user_role' => [ 'subscriber' ],
7076 'group_id' => 112,
7077 'group_name' => 'New Group',
7078 'group_description' => 'New Group Description',
7079 ];
7080 $context['response_type'] = 'sample';
7081 }
7082
7083 return $context;
7084 }
7085
7086 /**
7087 * Search complete courses.
7088 *
7089 * @param array $data data.
7090 * @return array
7091 */
7092 public function search_pluggables_complete_course( $data ) {
7093 global $wpdb;
7094 $context = [];
7095
7096 if ( isset( $data['filter']['sfwd_course_id']['value'] ) ) {
7097 $course_id = $data['filter']['sfwd_course_id']['value'];
7098 }
7099 if ( -1 === $course_id ) {
7100 $courses = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='course' AND activity.activity_status= %d ORDER BY activity.activity_id DESC", 1 ) );
7101 } else {
7102 $courses = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='course' AND activity.activity_status= %d AND activity.post_id= %d AND activity.course_id= %d", 1, $course_id, $course_id ) );
7103 }
7104
7105 if ( ! empty( $courses ) ) {
7106 $course = $courses[0];
7107 $course_data['course_name'] = $course->post_title;
7108 $course_data['sfwd_course_id'] = $course->ID;
7109 $course_data['course_url'] = get_permalink( $course->ID );
7110 $course_data['course_featured_image_id'] = get_post_meta( $course->ID, '_thumbnail_id', true );
7111 $course_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course->ID );
7112 $timestamp = get_user_meta( $course->user_id, 'course_completed_' . $course->ID, true );
7113 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
7114 $date_format = get_option( 'date_format' );
7115 if ( is_string( $date_format ) ) {
7116 $course_data['course_completion_date'] = wp_date( $date_format, $timestamp );
7117 }
7118 if ( function_exists( 'learndash_get_course_certificate_link' ) ) {
7119 $course_data['course_certificate'] = learndash_get_course_certificate_link( $course->ID, $course->user_id );
7120 }
7121 $context['response_type'] = 'live';
7122 } else {
7123 $course_data['course_name'] = 'Test Course';
7124 $course_data['sfwd_course_id'] = 112;
7125 $course_data['course_url'] = 'https://abc.com/test-course';
7126 $course_data['course_featured_image_id'] = 113;
7127 $course_data['course_featured_image_url'] = 'https://pqr.com/test-course-img';
7128 $course_data['course_completion_date'] = '2023-10-20';
7129 $course_data['course_certificate'] = 'https://example.com/certificates/good-performance/?course_id=112&cert-nonce=f80d0f9cc1';
7130 $context['response_type'] = 'sample';
7131 }
7132
7133 $users_data = $this->search_pluggables_add_user_role( [] );
7134 $user_data = $users_data['pluggable_data'];
7135
7136 $context['pluggable_data'] = array_merge( $course_data, $user_data );
7137 return $context;
7138 }
7139
7140 /**
7141 * Search lessons.
7142 *
7143 * @param array $data data.
7144 * @return array
7145 */
7146 public function search_pluggables_complete_lesson( $data ) {
7147 global $wpdb;
7148 $context = [];
7149
7150 if ( isset( $data['filter']['sfwd_lesson_id']['value'] ) ) {
7151 $lesson_id = $data['filter']['sfwd_lesson_id']['value'];
7152 $course_id = $data['filter']['sfwd_course_id']['value'];
7153 }
7154 if ( -1 === $course_id ) {
7155 $courses = get_posts(
7156 [
7157 'posts_per_page' => - 1,
7158 'post_type' => 'sfwd-courses',
7159 'post_status' => 'publish',
7160 'fields' => 'ids',
7161 ]
7162 );
7163 $course_key = array_rand( $courses );
7164 $course_id = $courses[ $course_key ];
7165 }
7166 $course = get_post( $course_id );
7167 $pluggable_data = LearnDash::get_course_context( $course );
7168
7169 if ( -1 === $lesson_id ) {
7170 $lessons = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='lesson' AND activity.activity_status= %d AND activity.course_id= %d", 1, $course_id ) );
7171 } else {
7172 $lessons = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='lesson' AND activity.activity_status= %d AND activity.post_id= %d AND activity.course_id= %d", 1, $lesson_id, $course_id ) );
7173 }
7174
7175 if ( ! empty( $lessons ) ) {
7176 $lesson = $lessons[0];
7177
7178 $pluggable_data = WordPress::get_user_context( $lesson->user_id );
7179 $pluggable_data['lesson_name'] = $lesson->post_title;
7180 $pluggable_data['sfwd_lesson_id'] = $lesson->ID;
7181 $pluggable_data['lesson_url'] = get_permalink( $lesson->ID );
7182 $pluggable_data['lesson_featured_image_id'] = get_post_meta( $lesson->ID, '_thumbnail_id', true );
7183 $pluggable_data['lesson_featured_image_url'] = get_the_post_thumbnail_url( $lesson->ID );
7184 $context['response_type'] = 'live';
7185 } else {
7186 $pluggable_data = WordPress::get_sample_user_context();
7187 $pluggable_data['lesson_name'] = 'Test Lesson';
7188 $pluggable_data['sfwd_lesson_id'] = 114;
7189 $pluggable_data['lesson_url'] = 'https://abc.com/test-lesson';
7190 $pluggable_data['lesson_featured_image_id'] = 116;
7191 $pluggable_data['lesson_featured_image_url'] = 'https://pqr.com/test-lesson-img';
7192 $context['response_type'] = 'sample';
7193 }
7194
7195 $context['pluggable_data'] = $pluggable_data;
7196 return $context;
7197 }
7198
7199 /**
7200 * Search topics.
7201 *
7202 * @param array $data data.
7203 * @return array
7204 */
7205 public function search_pluggables_complete_topic( $data ) {
7206 global $wpdb;
7207 $context = [];
7208
7209 if ( isset( $data['filter']['sfwd_topic_id']['value'] ) ) {
7210 $topic_id = $data['filter']['sfwd_topic_id']['value'];
7211 $course_id = $data['filter']['sfwd_course_id']['value'];
7212 }
7213 if ( -1 === $course_id ) {
7214 $courses = get_posts(
7215 [
7216 'posts_per_page' => - 1,
7217 'post_type' => 'sfwd-courses',
7218 'post_status' => 'publish',
7219 'fields' => 'ids',
7220 ]
7221 );
7222 $course_key = array_rand( $courses );
7223 $course_id = $courses[ $course_key ];
7224 }
7225 $course = get_post( $course_id );
7226 $pluggable_data = LearnDash::get_course_context( $course );
7227
7228 if ( -1 === $topic_id ) {
7229 $topics = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='topic' AND activity.activity_status= %d AND activity.course_id= %d", 1, $course_id ) );
7230 } else {
7231 $topics = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='topic' AND activity.activity_status= %d AND activity.post_id= %d AND activity.course_id= %d", 1, $topic_id, $course_id ) );
7232 }
7233
7234 if ( ! empty( $topics ) ) {
7235 $topic = $topics[0];
7236 $pluggable_data = WordPress::get_user_context( $topics[0]->user_id );
7237 $pluggable_data['topic_name'] = $topic->post_title;
7238 $pluggable_data['sfwd_topic_id'] = $topic->ID;
7239 $pluggable_data['topic_url'] = get_permalink( $topic->ID );
7240 $pluggable_data['topic_featured_image_id'] = get_post_meta( $topic->ID, '_thumbnail_id', true );
7241 $pluggable_data['topic_featured_image_url'] = get_the_post_thumbnail_url( $topic->ID );
7242 $context['response_type'] = 'live';
7243 } else {
7244 $pluggable_data = WordPress::get_sample_user_context();
7245 $pluggable_data['topic_name'] = 'Test Topic';
7246 $pluggable_data['sfwd_topic_id'] = 117;
7247 $pluggable_data['topic_url'] = 'https://abc.com/test-topic';
7248 $pluggable_data['topic_featured_image_id'] = 118;
7249 $pluggable_data['topic_featured_image_url'] = 'https://pqr.com/test-topic-img';
7250 $context['response_type'] = 'sample';
7251 }
7252
7253 $context['pluggable_data'] = $pluggable_data;
7254 return $context;
7255 }
7256
7257 /**
7258 * Search purchase courses.
7259 *
7260 * @param array $data data.
7261 * @return array
7262 */
7263 public function search_pluggables_purchase_course( $data ) {
7264 $context = [];
7265 $context['response_type'] = 'sample';
7266
7267 $purchase_data = [
7268 'course_product_id' => '1',
7269 'course_product_name' => 'Sample Course',
7270 'currency' => 'USD',
7271 'total_amount' => '100',
7272 'first_name' => 'John',
7273 'last_name' => 'Doe',
7274 'email' => 'john_doe@gmail.com',
7275 'phone' => '+923007626541',
7276 ];
7277
7278 $product_id = (int) ( isset( $data['filter']['course_product_id']['value'] ) ? $data['filter']['course_product_id']['value'] : '-1' );
7279 $order_id = 0;
7280
7281 if ( -1 !== $product_id ) {
7282 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
7283
7284 if ( count( $order_ids ) > 0 ) {
7285 $order_id = $order_ids[0];
7286 }
7287 } else {
7288 $orders = wc_get_orders( [] );
7289 if ( count( $orders ) > 0 ) {
7290 foreach ( $orders as $order ) {
7291 $items = $order->get_items();
7292
7293 if ( count( $items ) > 1 ) {
7294 continue;
7295 }
7296
7297 foreach ( $items as $item ) {
7298 if ( method_exists( $item, 'get_product_id' ) ) {
7299 $product_id = $item->get_product_id();
7300 if ( ! empty( get_post_meta( $item->get_product_id(), '_related_course', true ) ) ) {
7301 $order_id = $order->get_id();
7302 break;
7303 }
7304 }
7305 }
7306 }
7307 }
7308 }
7309
7310 if ( 0 !== $order_id ) {
7311 $order = wc_get_order( $order_id );
7312
7313 if ( $order ) {
7314
7315 $purchase_data = LearnDash::get_purchase_course_context( $order );
7316
7317 $context['response_type'] = 'live';
7318 }
7319 }
7320
7321 $context['pluggable_data'] = $purchase_data;
7322
7323 return $context;
7324 }
7325
7326 /**
7327 * Search quiz data in LearnDash.
7328 *
7329 * @param array $data data.
7330 * @return array
7331 */
7332 public function search_pluggables_ld_quiz( $data ) {
7333 $context = [];
7334 $context['response_type'] = 'sample';
7335 global $wpdb;
7336 $user_data = WordPress::get_sample_user_context();
7337 $quiz_data = [
7338 'quiz' => 7193,
7339 'score' => 1,
7340 'count' => 1,
7341 'question_show_count' => 1,
7342 'pass' => 1,
7343 'rank' => '-',
7344 'time' => 1703595328,
7345 'pro_quizid' => 1,
7346 'course' => 0,
7347 'lesson' => 0,
7348 'topic' => 0,
7349 'points' => 20,
7350 'total_points' => 20,
7351 'percentage' => 100,
7352 'timespent' => 2.7309999999999999,
7353 'has_graded' => false,
7354 'statistic_ref_id' => 9,
7355 'started' => 1703595325,
7356 'completed' => 1703595328,
7357 'ld_version' => '4.8.0.1',
7358 'quiz_key' => '1703595328_1_7193_0',
7359 ];
7360 $output_questions['question'] = [
7361 'ID' => 7195,
7362 'post_content' => '<p>This is a first question<\/p>',
7363 'type' => 'sfwd-question',
7364 'question_type' => 'single',
7365 'points' => 20,
7366 'answers' => [
7367 'sort_answer' => [
7368 [
7369 'answer' => '',
7370 'points' => 0,
7371 'correct' => false,
7372 'type' => 'answer',
7373 ],
7374 ],
7375 'classic_answer' => [
7376 [
7377 'answer' => 'Ans 1',
7378 'points' => 1,
7379 'correct' => false,
7380 'type' => 'answer',
7381 ],
7382 [
7383 'answer' => 'Ans 2',
7384 'points' => 0,
7385 'correct' => true,
7386 'type' => 'answer',
7387 ],
7388 ],
7389 'matrix_sort_answer' => [
7390 [
7391 'answer' => '',
7392 'points' => 0,
7393 'correct' => false,
7394 'type' => 'answer',
7395 ],
7396 ],
7397 'cloze_answer' => [
7398 [
7399 'answer' => '',
7400 'points' => 0,
7401 'correct' => false,
7402 'type' => 'answer',
7403 ],
7404 ],
7405 'free_answer' => [
7406 [
7407 'answer' => '',
7408 'points' => 0,
7409 'correct' => false,
7410 'type' => 'answer',
7411 ],
7412 ],
7413 'assessment_answer' => [
7414 [
7415 'answer' => '',
7416 'points' => 0,
7417 'correct' => false,
7418 'type' => 'answer',
7419 ],
7420 ],
7421 'essay' => [
7422 [
7423 'answer' => '',
7424 'points' => 0,
7425 'correct' => false,
7426 'type' => 'answer',
7427 ],
7428 ],
7429 ],
7430 ];
7431
7432 $quiz_id = (int) ( isset( $data['filter']['sfwd_quiz_id']['value'] ) ? $data['filter']['sfwd_quiz_id']['value'] : '-1' );
7433 $question_id = $data['filter']['sfwd_question_id']['value'];
7434 $term = $data['search_term'];
7435 $mark = 1;
7436 if ( 'passes_quiz' == $term ) {
7437 $mark = 1;
7438 } elseif ( 'fails_quiz' == $term ) {
7439 $mark = 0;
7440 }
7441 if ( 'passes_quiz' == $term || 'fails_quiz' == $term ) {
7442 if ( -1 == $quiz_id ) {
7443 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}learndash_pro_quiz_statistic_ref as statistic ON activity.activity_completed = statistic.create_time WHERE activity.activity_type='quiz' AND activity.activity_status=%d order by activity_id DESC LIMIT 1", $mark ) );
7444 } else {
7445 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity LEFT JOIN {$wpdb->prefix}learndash_pro_quiz_statistic_ref as statistic ON activity.activity_completed = statistic.create_time WHERE activity.activity_type='quiz' AND activity.activity_status=%d AND activity.post_id = %d order by activity.activity_id DESC LIMIT 1", $mark, $quiz_id ) );
7446 }
7447 } elseif ( 'quiz_essay_submitted' == $term ) {
7448 $question_id = $data['filter']['sfwd_question_id']['value'];
7449 $args = [
7450 'post_type' => 'sfwd-essays',
7451 'posts_per_page' => 1,
7452 'order' => 'DESC',
7453 'post_status' => [ 'graded', 'not_graded' ],
7454 'meta_query' => [
7455 'relation' => 'AND',
7456 [
7457 'key' => 'quiz_post_id',
7458 'value' => $quiz_id,
7459 'compare' => '=',
7460 ],
7461 [
7462 'key' => 'question_post_id',
7463 'value' => $question_id,
7464 'compare' => '=',
7465 ],
7466 ],
7467 ];
7468 $essay = get_posts( $args );
7469 } elseif ( 'quiz_essay_graded' == $term ) {
7470 $args = [
7471 'post_type' => 'sfwd-essays',
7472 'posts_per_page' => 1,
7473 'order' => 'DESC',
7474 'post_status' => 'graded',
7475 'meta_query' => [
7476 'relation' => 'AND',
7477 [
7478 'key' => 'quiz_post_id',
7479 'value' => $quiz_id,
7480 'compare' => '=',
7481 ],
7482 [
7483 'key' => 'question_post_id',
7484 'value' => $question_id,
7485 'compare' => '=',
7486 ],
7487 ],
7488 ];
7489 $essay = get_posts( $args );
7490 }
7491
7492 if ( 'quiz_essay_submitted' == $term || 'quiz_essay_graded' == $term ) {
7493 if ( ! empty( $essay ) ) {
7494 $context = WordPress::get_user_context( $essay[0]->post_author );
7495 $course_id = get_post_meta( $essay[0]->ID, 'course_id', true );
7496 $lesson_id = get_post_meta( $essay[0]->ID, 'lesson_id', true );
7497 $context['quiz_name'] = get_the_title( $quiz_id );
7498 $context['sfwd_quiz_id'] = $quiz_id;
7499 $context['sfwd_question_id'] = $question_id;
7500 $context['question_name'] = is_int( $question_id ) ? (int) get_the_title( $question_id ) : null;
7501 $context['course_name'] = is_int( $course_id ) ? (int) get_the_title( $course_id ) : null;
7502 $context['course_id'] = $course_id;
7503 $context['lesson_name'] = is_int( $lesson_id ) ? (int) get_the_title( $lesson_id ) : null;
7504 $context['lesson_id'] = $lesson_id;
7505 $context['essay_id'] = $essay[0]->ID;
7506 $context['essay'] = WordPress::get_post_context( $essay[0]->ID );
7507 if ( 'quiz_essay_graded' == $term ) {
7508 $users_quiz_data = get_user_meta( (int) $essay[0]->post_author, '_sfwd-quizzes', true );
7509 if ( is_array( $users_quiz_data ) && is_array( $users_quiz_data[0] ) ) {
7510 $essay_post_id = $essay[0]->ID;
7511 $result = array_filter(
7512 $users_quiz_data[0]['graded'],
7513 function( $item ) use ( $essay_post_id ) {
7514 return $item['post_id'] === $essay_post_id;
7515 }
7516 );
7517 $context['essay_points_earned'] = $result[1]['points_awarded'];
7518 }
7519 }
7520 $context['response_type'] = 'live';
7521 } else {
7522 $context = WordPress::get_sample_user_context();
7523 $context['quiz_name'] = 'Test Quiz';
7524 $context['sfwd_quiz_id'] = 11;
7525 $context['sfwd_question_id'] = 12;
7526 $context['course_name'] = 'Test Course';
7527 $context['course_id'] = 13;
7528 $context['lesson_name'] = 'Test Lesson';
7529 $context['lesson_id'] = 14;
7530 $context['essay'] = [
7531 'ID' => 12,
7532 'post_content' => 'demo',
7533 ];
7534 $context['response_type'] = 'sample';
7535 }
7536
7537 $context['pluggable_data'] = $context;
7538 } else {
7539 if ( ! empty( $result ) ) {
7540 $user_data = WordPress::get_user_context( $result[0]->user_id );
7541 $quiz_result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}usermeta WHERE user_id = %d AND meta_key = '_sfwd-quizzes'", $result[0]->user_id ) );
7542
7543 $data = unserialize( $quiz_result[0]->meta_value );
7544 $found_element = null;
7545 foreach ( (array) $data as $element ) {
7546 if ( is_array( $element ) ) {
7547 if ( isset( $element['statistic_ref_id'] ) && $element['statistic_ref_id'] == $result[0]->statistic_ref_id ) {
7548 $found_element = $element;
7549 break;
7550 }
7551 }
7552 }
7553 $quiz_data = (array) $found_element;
7554 $output_questions = LearnDash::get_quiz_questions_answers( $quiz_id );
7555 if ( is_array( $quiz_data ) ) {
7556 $quiz_data['quiz_name'] = get_the_title( $quiz_id );
7557 $quiz_data['sfwd_quiz_id'] = $quiz_id;
7558 }
7559 $context['response_type'] = 'live';
7560 }
7561 $context['pluggable_data'] = array_merge( $quiz_data, $user_data, $output_questions );
7562 }
7563
7564 return $context;
7565 }
7566
7567 /**
7568 * Fetch BB templates.
7569 *
7570 * @return array
7571 */
7572 public function get_beaver_builder_templates() {
7573 $allowed_types = [ 'subscribe-form', 'contact-form' ];
7574 $templates = [];
7575 $all_templates = get_posts(
7576 [
7577 'post_type' => 'fl-builder-template',
7578 'meta_key' => '_fl_builder_data',
7579 'posts_per_page' => -1,
7580 ]
7581 );
7582 $posts = get_posts(
7583 [
7584 'post_type' => 'any',
7585 'meta_key' => '_fl_builder_data',
7586 'posts_per_page' => -1,
7587 ]
7588 );
7589 $posts = array_merge( $all_templates, $posts );
7590
7591 if ( ! empty( $posts ) ) {
7592 foreach ( $posts as $post ) {
7593 $meta = get_post_meta( $post->ID, '_fl_builder_data', true );
7594 foreach ( (array) $meta as $node_id => $node ) {
7595 if ( isset( $node->type ) && 'module' === $node->type ) {
7596 $settings = $node->settings;
7597 if ( in_array( $settings->type, $allowed_types, true ) ) {
7598 $label = $post->post_title;
7599 if ( '' !== $settings->node_label ) {
7600 $label .= ' - ' . $settings->node_label;
7601 }
7602 $templates[] = [
7603 'label' => $label,
7604 'value' => $node_id,
7605 ];
7606 }
7607 }
7608 }
7609 }
7610 }
7611 return $templates;
7612 }
7613
7614 /**
7615 * Search beaver builder forms.
7616 *
7617 * @param array $data data.
7618 * @return array
7619 */
7620 public function search_beaver_builder_forms( $data ) {
7621 $templates = $this->get_beaver_builder_templates();
7622 return [
7623 'options' => $templates,
7624 'hasMore' => false,
7625 ];
7626 }
7627
7628 /**
7629 * Search FluentCRM fields.
7630 *
7631 * @param array $data data.
7632 * @return array
7633 */
7634 public function search_fluentcrm_custom_fields( $data ) {
7635 $context = [];
7636 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
7637 $context['fields'] = $custom_fields;
7638 return $context;
7639 }
7640
7641 /**
7642 * Search FluentCRM Company fields.
7643 *
7644 * @param array $data data.
7645 * @return array
7646 */
7647 public function search_fluentcrm_company_custom_fields( $data ) {
7648 $context = [];
7649 if ( function_exists( 'fluentcrm_get_custom_company_fields' ) ) {
7650 $custom_fields = fluentcrm_get_custom_company_fields();
7651 $context['fields'] = $custom_fields;
7652 }
7653 return $context;
7654 }
7655
7656 /**
7657 * Search FluentCRM fields and display it in dropdown for trigger.
7658 *
7659 * @param array $data data.
7660 * @return array
7661 */
7662 public function search_fluentcrm_custom_fields_data( $data ) {
7663
7664 if ( ! class_exists( 'FluentCrm\App\Models\CustomContactField' ) ) {
7665 return [];
7666 }
7667
7668 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
7669 $options = [];
7670
7671 if ( ! empty( $custom_fields ) ) {
7672 foreach ( $custom_fields as $custom_field ) {
7673 $options[] = [
7674 'label' => $custom_field['label'],
7675 'value' => $custom_field['slug'],
7676 ];
7677 }
7678 }
7679
7680 return [
7681 'options' => $options,
7682 'hasMore' => false,
7683 ];
7684 }
7685
7686 /**
7687 * Fetch WP JOB Manager Last Data.
7688 *
7689 * @param array $data data.
7690 * @return array
7691 */
7692 public function search_wp_job_manger_last_data( $data ) {
7693 global $wpdb;
7694 $job_type = $data['filter']['job_type']['value'];
7695 $args = [
7696 'posts_per_page' => 1,
7697 'post_type' => 'job_listing',
7698 'orderby' => 'id',
7699 'order' => 'DESC',
7700 ];
7701
7702 if ( -1 !== $job_type ) {
7703 $args['tax_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
7704 [
7705 'taxonomy' => 'job_listing_type',
7706 'field' => 'term_id',
7707 'terms' => $job_type,
7708 ],
7709 ];
7710 }
7711 $posts = get_posts( $args );
7712 if ( empty( $posts ) ) {
7713 $context = json_decode( '{"response_type":"sample","pluggable_data":{"ID":145,"wpjob_author":"1","wpjob_date":"2023-01-22 17:38:03","wpjob_date_gmt":"2023-01-22 17:38:03","wpjob_content":"","wpjob_title":"PHP Developer","wpjob_excerpt":"","wpjob_status":"publish","comment_status":"closed","ping_status":"closed","wpjob_password":"","wpjob_name":"project-manager","to_ping":"","pinged":"","wpjob_modified":"2023-01-23 03:23:35","wpjob_modified_gmt":"2023-01-23 03:23:35","wpjob_content_filtered":"","wpjob_parent":0,"guid":"http:\/\/connector.com\/?post_type=job_listing&#038;p=145","menu_order":-1,"wpjob_type":"job_listing","wpjob_mime_type":"","comment_count":"0","filter":"raw","_filled":["0"],"_featured":["1"],"_tribe_ticket_capacity":["0"],"_tribe_ticket_version":["5.5.6"],"_edit_lock":["1674444219:1"],"_job_expires":["2023-02-21"],"_tracked_submitted":["1674409083"],"_edit_last":["1"],"_job_location":[""],"_application":["johnsmith@bexample.com"],"_company_name":["test"],"_company_website":[""],"_company_tagline":[""],"_company_twitter":[""],"_company_video":[""],"_remote_position":["1"],"_llms_reviews_enabled":[""],"_llms_display_reviews":[""],"_llms_num_reviews":["0"],"_llms_multiple_reviews_disabled":[""],"wp_user_id":1,"user_login":"john","display_name":"john","user_firstname":"john","user_lastname":"smith","user_email":"johnsmith@bexample.com","user_role":["administrator","subscriber","tutor_instructor"]}}', true );
7714 return $context;
7715 }
7716
7717 $post = $posts[0];
7718 $post_content = WordPress::get_post_context( $post->ID );
7719 $post_meta = WordPress::get_post_meta( $post->ID );
7720 $job_data = array_merge( $post_content, $post_meta, WordPress::get_user_context( $post->post_author ) );
7721 foreach ( $job_data as $key => $job ) {
7722 $newkey = str_replace( 'post', 'wpjob', $key );
7723 unset( $job_data[ $key ] );
7724 $job_data[ $newkey ] = $job;
7725 }
7726 $context['response_type'] = 'live';
7727 $context['pluggable_data'] = $job_data;
7728 return $context;
7729
7730 }
7731
7732 /**
7733 * Get Amelia Appointment Category.
7734 *
7735 * @param array $data data.
7736 *
7737 * @return array
7738 */
7739 public function search_amelia_category_list( $data ) {
7740
7741 global $wpdb;
7742
7743 $page = $data['page'];
7744 $limit = Utilities::get_search_page_limit();
7745 $offset = $limit * ( $page - 1 );
7746
7747 $categories = $wpdb->get_results(
7748 $wpdb->prepare(
7749 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_categories WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
7750 [ 'visible', $limit, $offset ]
7751 ),
7752 OBJECT
7753 );
7754
7755 $categories_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
7756
7757 $options = [];
7758 if ( ! empty( $categories ) ) {
7759 foreach ( $categories as $category ) {
7760 $options[] = [
7761 'label' => $category->name,
7762 'value' => $category->id,
7763 ];
7764 }
7765 }
7766
7767 return [
7768 'options' => $options,
7769 'hasMore' => $categories_count > $limit && $categories_count > $offset,
7770 ];
7771
7772 }
7773
7774 /**
7775 * Get Amelia Appointment Services.
7776 *
7777 * @param array $data data.
7778 *
7779 * @return array
7780 */
7781 public function search_amelia_service_list( $data ) {
7782
7783 global $wpdb;
7784
7785 $page = $data['page'];
7786 $limit = Utilities::get_search_page_limit();
7787 $offset = $limit * ( $page - 1 );
7788
7789 $services = $wpdb->get_results(
7790 $wpdb->prepare(
7791 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_services
7792 WHERE categoryId = %d AND status = %s
7793 ORDER BY name ASC LIMIT %d OFFSET %d",
7794 [ $data['dynamic'], 'visible', $limit, $offset ]
7795 ),
7796 OBJECT
7797 );
7798
7799 $services_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
7800
7801 $options = [];
7802 if ( ! empty( $services ) ) {
7803 foreach ( $services as $category ) {
7804 $options[] = [
7805 'label' => $category->name,
7806 'value' => $category->id,
7807 ];
7808 }
7809 }
7810
7811 return [
7812 'options' => $options,
7813 'hasMore' => $services_count > $limit && $services_count > $offset,
7814 ];
7815
7816 }
7817
7818 /**
7819 * Get Amelia Events.
7820 *
7821 * @param array $data data.
7822 *
7823 * @return array
7824 */
7825 public function search_amelia_events_list( $data ) {
7826
7827 global $wpdb;
7828
7829 $page = $data['page'];
7830 $limit = Utilities::get_search_page_limit();
7831 $offset = $limit * ( $page - 1 );
7832
7833 $events = $wpdb->get_results(
7834 $wpdb->prepare(
7835 "SELECT SQL_CALC_FOUND_ROWS id, name from {$wpdb->prefix}amelia_events WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
7836 [ 'approved', $limit, $offset ]
7837 ),
7838 OBJECT
7839 );
7840
7841 $list_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
7842
7843 $options = [];
7844 if ( ! empty( $events ) ) {
7845 foreach ( $events as $event ) {
7846 $options[] = [
7847 'label' => $event->name,
7848 'value' => $event->id,
7849 ];
7850 }
7851 }
7852
7853 return [
7854 'options' => $options,
7855 'hasMore' => $list_count > $limit && $list_count > $offset,
7856 ];
7857
7858 }
7859
7860 /**
7861 * Get Amelia Events.
7862 *
7863 * @param array $data data.
7864 *
7865 * @return array
7866 */
7867 public function search_amelia_booking_status_list( $data ) {
7868
7869 $options = [];
7870 $options[] = [
7871 'label' => 'Approved',
7872 'value' => 'approved',
7873 ];
7874 $options[] = [
7875 'label' => 'Pending',
7876 'value' => 'pending',
7877 ];
7878 $options[] = [
7879 'label' => 'Rejected',
7880 'value' => 'rejected',
7881 ];
7882 $options[] = [
7883 'label' => 'Canceled',
7884 'value' => 'canceled',
7885 ];
7886 $options[] = [
7887 'label' => 'No-show',
7888 'value' => 'no-show',
7889 ];
7890 return [
7891 'options' => $options,
7892 'hasMore' => false,
7893 ];
7894 }
7895
7896 /**
7897 * Get last data for trigger.
7898 *
7899 * @param array $data data.
7900 * @return array
7901 */
7902 public function search_amelia_appointment_booked_triggers_last_data( $data ) {
7903 global $wpdb;
7904
7905 $context = [];
7906
7907 $appointment_category = $data['filter']['amelia_category_list']['value'];
7908 $appointment_service = $data['filter']['amelia_service_list']['value'];
7909
7910 if ( -1 === $appointment_service ) {
7911 // If service exists as per category selected.
7912 $service_exist = $wpdb->get_row(
7913 $wpdb->prepare(
7914 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
7915 [ $appointment_category ]
7916 ),
7917 ARRAY_A
7918 );
7919
7920 if ( empty( $service_exist ) ) {
7921 $result = [];
7922 } else {
7923 $result = $wpdb->get_row(
7924 'SELECT appointments.*, customer.* FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer INNER JOIN ' . $wpdb->prefix . 'amelia_appointments as appointments ON customer.appointmentId=appointments.id WHERE customer.appointmentId = ( SELECT max(id) FROM ' . $wpdb->prefix . 'amelia_appointments )',
7925 ARRAY_A
7926 );
7927 }
7928 } else {
7929 $result = $wpdb->get_row(
7930 $wpdb->prepare(
7931 'SELECT appointments.*, customer.* FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer INNER JOIN ' . $wpdb->prefix . 'amelia_appointments as appointments ON customer.appointmentId=appointments.id WHERE customer.appointmentId = ( SELECT max(id) FROM ' . $wpdb->prefix . 'amelia_appointments ) AND appointments.serviceId = %d',
7932 [ $appointment_service ]
7933 ),
7934 ARRAY_A
7935 );
7936 }
7937
7938 if ( ! empty( $result ) ) {
7939
7940 $payment_result = $wpdb->get_row(
7941 $wpdb->prepare(
7942 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
7943 [ $result['id'] ]
7944 ),
7945 ARRAY_A
7946 );
7947
7948 $customer_result = $wpdb->get_row(
7949 $wpdb->prepare(
7950 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
7951 [ $result['customerId'] ]
7952 ),
7953 ARRAY_A
7954 );
7955
7956 $service_result = $wpdb->get_row(
7957 $wpdb->prepare(
7958 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
7959 [ $result['serviceId'] ]
7960 ),
7961 ARRAY_A
7962 );
7963
7964 $category_result = $wpdb->get_row(
7965 $wpdb->prepare(
7966 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
7967 [ $service_result['categoryId'] ]
7968 ),
7969 ARRAY_A
7970 );
7971
7972 if ( $result['couponId'] ) {
7973 $coupon_result = $wpdb->get_row(
7974 $wpdb->prepare(
7975 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
7976 [ $result['couponId'] ]
7977 ),
7978 ARRAY_A
7979 );
7980 } else {
7981 $coupon_result = [];
7982 }
7983
7984 if ( ! empty( $result['customFields'] ) ) {
7985 $custom_fields = json_decode( $result['customFields'], true );
7986
7987 $fields_arr = [];
7988 foreach ( (array) $custom_fields as $fields ) {
7989 if ( is_array( $fields ) ) {
7990 $fields_arr[ $fields['label'] ] = $fields['value'];
7991 }
7992 }
7993 unset( $result['customFields'] );
7994 } else {
7995 $fields_arr = [];
7996 }
7997
7998 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
7999 $context['response_type'] = 'live';
8000 } else {
8001
8002 $context = json_decode( '{"response_type":"sample","pluggable_data":{"id":"1","status":"visible","bookingStart":"2023-02-28 13:00:00","bookingEnd":"2023-02-28 14:00:00","notifyParticipants":"1","serviceId":"4","packageId":null,"providerId":"2","locationId":null,"internalNotes":"","googleCalendarEventId":null,"googleMeetUrl":null,"outlookCalendarEventId":null,"zoomMeeting":null,"lessonSpace":null,"parentId":null,"appointmentId":"1","customerId":"1","price":"15","persons":"1","couponId":null,"token":"02cf0988c6","info":"{\"firstName\":\"John\",\"lastName\":\"Doe\",\"phone\":\"1 (234) 789\",\"locale\":\"en_US\",\"timeZone\":\"Asia\\\/Kolkata\",\"urlParams\":null}","utcOffset":null,"aggregatedPrice":"1","packageCustomerServiceId":null,"duration":"3600","created":"2023-02-08 11:16:03","actionsCompleted":"1","Do You Know Automation?":"Yes","When Are You Coming?":"2023-04-20","Upload Something":"","Tell Us About You!":"Hey there!","customerBookingId":"103","amount":"0","dateTime":"2023-02-28 13:00:00","gateway":"onSite","gatewayTitle":"","data":"","packageCustomerId":null,"entity":"appointment","wcOrderId":null,"type":"customer","externalId":"89","firstName":"John","lastName":"Doe","email":"johnd@gmail.com","birthday":null,"phone":"1 (234) 789","gender":null,"note":null,"description":null,"pictureFullPath":null,"pictureThumbPath":null,"password":null,"usedTokens":null,"zoomUserId":null,"countryPhoneIso":"us","translations":"{\"defaultLanguage\":\"en_US\"}","timeZone":null,"serviceName":"demo service","serviceDescription":"","categoryId":"2","categoryName":"New Category1"}}', true );
8003 }
8004
8005 return $context;
8006 }
8007
8008 /**
8009 * Get last data for trigger.
8010 *
8011 * @param array $data data.
8012 * @return array
8013 */
8014 public function search_amelia_appointment_booking_status_changed_triggers_last_data( $data ) {
8015 global $wpdb;
8016
8017 $context = [];
8018
8019 $appointment_category = $data['filter']['amelia_category_list']['value'];
8020 $appointment_service = $data['filter']['amelia_service_list']['value'];
8021 $appointment_status = $data['filter']['appointment_status']['value'];
8022
8023 if ( -1 === $appointment_service ) {
8024 // If service exists as per category selected.
8025 $service_exist = $wpdb->get_row(
8026 $wpdb->prepare(
8027 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
8028 [ $appointment_category ]
8029 ),
8030 ARRAY_A
8031 );
8032
8033 if ( empty( $service_exist ) ) {
8034 $result = [];
8035 } else {
8036 $result = $wpdb->get_row(
8037 'SELECT appointments.*, customer.* FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer INNER JOIN ' . $wpdb->prefix . 'amelia_appointments as appointments ON customer.appointmentId=appointments.id WHERE customer.appointmentId = ( SELECT max(id) FROM ' . $wpdb->prefix . 'amelia_appointments )',
8038 ARRAY_A
8039 );
8040 }
8041 } else {
8042 if ( -1 === $appointment_status ) {
8043 $result = $wpdb->get_row(
8044 $wpdb->prepare(
8045 'SELECT appointments.*, customer.* FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer INNER JOIN ' . $wpdb->prefix . 'amelia_appointments as appointments ON customer.appointmentId=appointments.id WHERE appointments.serviceId = %d',
8046 [ $appointment_service ]
8047 ),
8048 ARRAY_A
8049 );
8050 } else {
8051 $result = $wpdb->get_row(
8052 $wpdb->prepare(
8053 'SELECT appointments.*, customer.* FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer INNER JOIN ' . $wpdb->prefix . 'amelia_appointments as appointments ON customer.appointmentId=appointments.id WHERE appointments.status = %s AND appointments.serviceId = %d',
8054 [ $appointment_status, $appointment_service ]
8055 ),
8056 ARRAY_A
8057 );
8058 }
8059 }
8060
8061 if ( ! empty( $result ) ) {
8062
8063 $payment_result = $wpdb->get_row(
8064 $wpdb->prepare(
8065 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
8066 [ $result['id'] ]
8067 ),
8068 ARRAY_A
8069 );
8070
8071 $customer_result = $wpdb->get_row(
8072 $wpdb->prepare(
8073 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8074 [ $result['customerId'] ]
8075 ),
8076 ARRAY_A
8077 );
8078
8079 $service_result = $wpdb->get_row(
8080 $wpdb->prepare(
8081 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
8082 [ $result['serviceId'] ]
8083 ),
8084 ARRAY_A
8085 );
8086
8087 $category_result = $wpdb->get_row(
8088 $wpdb->prepare(
8089 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
8090 [ $service_result['categoryId'] ]
8091 ),
8092 ARRAY_A
8093 );
8094
8095 if ( $result['couponId'] ) {
8096 $coupon_result = $wpdb->get_row(
8097 $wpdb->prepare(
8098 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8099 [ $result['couponId'] ]
8100 ),
8101 ARRAY_A
8102 );
8103 } else {
8104 $coupon_result = [];
8105 }
8106
8107 if ( ! empty( $result['customFields'] ) ) {
8108 $custom_fields = json_decode( $result['customFields'], true );
8109
8110 $fields_arr = [];
8111 foreach ( (array) $custom_fields as $fields ) {
8112 if ( is_array( $fields ) ) {
8113 $fields_arr[ $fields['label'] ] = $fields['value'];
8114 }
8115 }
8116 unset( $result['customFields'] );
8117 } else {
8118 $fields_arr = [];
8119 }
8120
8121 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
8122 $context['pluggable_data']['amelia_category_list'] = $appointment_category;
8123 if ( -1 === $appointment_status ) {
8124 $context['pluggable_data']['appointment_status'] = 'approved';
8125 } else {
8126 $context['pluggable_data']['appointment_status'] = $appointment_status;
8127 }
8128 if ( -1 === $appointment_service ) {
8129 $context['pluggable_data']['amelia_service_list'] = $service_result['id'];
8130 } else {
8131 $context['pluggable_data']['amelia_service_list'] = $appointment_service;
8132 }
8133 $context['response_type'] = 'live';
8134 } else {
8135
8136 $context = json_decode( '{"response_type":"sample","pluggable_data":{"id":"1","status":"visible","bookingStart":"2023-02-28 13:00:00","bookingEnd":"2023-02-28 14:00:00","notifyParticipants":"1","serviceId":"4","packageId":null,"providerId":"2","locationId":null,"internalNotes":"","googleCalendarEventId":null,"googleMeetUrl":null,"outlookCalendarEventId":null,"zoomMeeting":null,"lessonSpace":null,"parentId":null,"appointmentId":"1","customerId":"1","price":"15","persons":"1","couponId":null,"token":"02cf0988c6","info":"{\"firstName\":\"John\",\"lastName\":\"Doe\",\"phone\":\"1 (234) 789\",\"locale\":\"en_US\",\"timeZone\":\"Asia\\\/Kolkata\",\"urlParams\":null}","utcOffset":null,"aggregatedPrice":"1","packageCustomerServiceId":null,"duration":"3600","created":"2023-02-08 11:16:03","actionsCompleted":"1","Do You Know Automation?":"Yes","When Are You Coming?":"2023-04-20","Upload Something":"","Tell Us About You!":"Hey there!","customerBookingId":"103","amount":"0","dateTime":"2023-02-28 13:00:00","gateway":"onSite","gatewayTitle":"","data":"","packageCustomerId":null,"entity":"appointment","wcOrderId":null,"type":"customer","externalId":"89","firstName":"John","lastName":"Doe","email":"johnd@gmail.com","birthday":null,"phone":"1 (234) 789","gender":null,"note":null,"description":null,"pictureFullPath":null,"pictureThumbPath":null,"password":null,"usedTokens":null,"zoomUserId":null,"countryPhoneIso":"us","translations":"{\"defaultLanguage\":\"en_US\"}","timeZone":null,"serviceName":"demo service","serviceDescription":"","categoryId":"2","categoryName":"New Category1","amelia_category_list": "2","appointment_status": "approved","amelia_service_list": "4"}}', true );
8137 }
8138
8139 return $context;
8140 }
8141
8142 /**
8143 * Get last data for trigger.
8144 *
8145 * @param array $data data.
8146 * @return array
8147 */
8148 public function search_amelia_event_status_changed_triggers_last_data( $data ) {
8149 global $wpdb;
8150
8151 $context = [];
8152
8153 $event_selected = $data['filter']['amelia_events_list']['value'];
8154 $event_status = $data['filter']['event_booking_status']['value'];
8155
8156 $query = 'SELECT *
8157 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8158 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8159 ON customer.id = event_period.customerBookingId';
8160
8161 if ( -1 === $event_selected ) {
8162 if ( -1 !== $event_status ) {
8163 $query .= $wpdb->prepare(
8164 ' WHERE event_period.customerBookingId = (
8165 SELECT MAX(customerBookingId)
8166 FROM ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods
8167 ) AND customer.status = %s',
8168 $event_status
8169 );
8170 } else {
8171 $query .= ' WHERE event_period.customerBookingId = (
8172 SELECT MAX(customerBookingId)
8173 FROM ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods
8174 )';
8175 }
8176 } else {
8177 if ( -1 !== $event_status ) {
8178 $query .= $wpdb->prepare(
8179 ' WHERE event_period.eventPeriodId = %d AND customer.status = %s',
8180 $event_selected,
8181 $event_status
8182 );
8183 } else {
8184 $query .= $wpdb->prepare(
8185 ' WHERE event_period.eventPeriodId = %d',
8186 $event_selected
8187 );
8188 }
8189 }
8190
8191 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
8192
8193 if ( ! empty( $result ) ) {
8194 $event = $wpdb->get_row(
8195 $wpdb->prepare(
8196 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
8197 [ $result['eventPeriodId'] ]
8198 ),
8199 ARRAY_A
8200 );
8201 $event_tags = $wpdb->get_results(
8202 $wpdb->prepare(
8203 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
8204 [ $result['eventPeriodId'] ]
8205 ),
8206 ARRAY_A
8207 );
8208 $tags = [];
8209 if ( ! empty( $event_tags ) ) {
8210 foreach ( $event_tags as $key => $tag ) {
8211 $tags['event_tag'][ $key ] = $tag['name'];
8212 }
8213 } else {
8214 $tags = [];
8215 }
8216 $customer_result = $wpdb->get_row(
8217 $wpdb->prepare(
8218 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8219 [ $result['customerId'] ]
8220 ),
8221 ARRAY_A
8222 );
8223 if ( $result['couponId'] ) {
8224 $coupon_result = $wpdb->get_row(
8225 $wpdb->prepare(
8226 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8227 [ $result['couponId'] ]
8228 ),
8229 ARRAY_A
8230 );
8231 } else {
8232 $coupon_result = [];
8233 }
8234 if ( ! empty( $result['customFields'] ) ) {
8235 $custom_fields = json_decode( $result['customFields'], true );
8236
8237 $fields_arr = [];
8238 foreach ( (array) $custom_fields as $fields ) {
8239 if ( is_array( $fields ) ) {
8240 $fields_arr[ $fields['label'] ] = $fields['value'];
8241 }
8242 }
8243 unset( $result['customFields'] );
8244 } else {
8245 $fields_arr = [];
8246 }
8247 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
8248 if ( -1 === $event_status ) {
8249 $context['pluggable_data']['event_booking_status'] = 'approved';
8250 } else {
8251 $context['pluggable_data']['event_booking_status'] = $event_status;
8252 }
8253 $context['response_type'] = 'live';
8254 } else {
8255
8256 $context = json_decode( '{"response_type": "sample","pluggable_data": {"id": "1","appointmentId": null,"customerId": "1","status": "visible","price": "10","persons": "1","couponId": null,"token": "6485b07ce9","info": "{\"firstName\":\"John\",\"lastName\":\"Doe\",\"phone\":\"+213551223123\",\"locale\":\"en_US\",\"timeZone\":\"Asia\\/Kolkata\",\"urlParams\":null}","utcOffset": null,"aggregatedPrice": "1","packageCustomerServiceId": null,"duration": null,"created": "2023-02-02 06:35:18","actionsCompleted": "1","Do You Know Automation?": "Yes","When Are You Coming?": "2023-04-20","Upload Something": "","Tell Us About You!": "Hey there!","customerBookingId": "105","eventPeriodId": "5","parentId": null,"name": "Music Event","bookingOpens": null,"bookingCloses": "2023-02-09 08:00:00","bookingOpensRec": "same","bookingClosesRec": "same","ticketRangeRec": "calculate","recurringCycle": null,"recurringOrder": null,"recurringInterval": null,"recurringMonthly": null,"monthlyDate": null,"monthlyOnRepeat": null,"monthlyOnDay": null,"recurringUntil": null,"maxCapacity": "12","maxCustomCapacity": null,"maxExtraPeople": null,"locationId": null,"customLocation": "Kolkata","description": null,"color": "#1788FB","show": "1","notifyParticipants": "1","settings": "{\"payments\":{\"onSite\":true,\"payPal\":{\"enabled\":false},\"stripe\":{\"enabled\":false},\"mollie\":{\"enabled\":false},\"razorpay\":{\"enabled\":false}},\"general\":{\"minimumTimeRequirementPriorToCanceling\":null,\"redirectUrlAfterAppointment\":null},\"zoom\":{\"enabled\":false},\"lessonSpace\":{\"enabled\":false}}","zoomUserId": null,"bringingAnyone": "1","bookMultipleTimes": "1","translations": "{\"defaultLanguage\":\"en_US\"}","depositPayment": "disabled","depositPerPerson": "1","fullPayment": "0","deposit": "0","customPricing": "0","organizerId": "2","closeAfterMin": null,"closeAfterMinBookings": "0","type": "customer","externalId": "91","firstName": "Jogn","lastName": "Doe","email": "johnd@gmail.com","birthday": null,"phone": "+213551223123","gender": null,"note": null,"pictureFullPath": null,"pictureThumbPath": null,"password": null,"usedTokens": null,"countryPhoneIso": "dz","timeZone": null,"event_booking_status":"approved"}}', true );
8257 }
8258
8259 return $context;
8260 }
8261
8262 /**
8263 * Get last data for trigger.
8264 *
8265 * @param array $data data.
8266 * @return array
8267 */
8268 public function search_amelia_new_event_attendee_triggers_last_data( $data ) {
8269 global $wpdb;
8270
8271 $context = [];
8272
8273 $event_selected = $data['filter']['amelia_events_list']['value'];
8274
8275 if ( -1 === $event_selected ) {
8276 $result = $wpdb->get_row(
8277 'SELECT *
8278 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8279 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8280 ON customer.id = event_period.customerBookingId
8281 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods )',
8282 ARRAY_A
8283 );
8284 } else {
8285 $result = $wpdb->get_row(
8286 $wpdb->prepare(
8287 'SELECT *
8288 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8289 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8290 ON customer.id = event_period.customerBookingId
8291 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId = %d',
8292 [ $event_selected ]
8293 ),
8294 ARRAY_A
8295 );
8296 }
8297
8298 if ( ! empty( $result ) ) {
8299
8300 $event = $wpdb->get_row(
8301 $wpdb->prepare(
8302 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
8303 [ $result['eventPeriodId'] ]
8304 ),
8305 ARRAY_A
8306 );
8307 $event_tags = $wpdb->get_results(
8308 $wpdb->prepare(
8309 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
8310 [ $result['eventPeriodId'] ]
8311 ),
8312 ARRAY_A
8313 );
8314 $tags = [];
8315 if ( ! empty( $event_tags ) ) {
8316 foreach ( $event_tags as $key => $tag ) {
8317 $tags['event_tag'][ $key ] = $tag['name'];
8318 }
8319 } else {
8320 $tags = [];
8321 }
8322
8323 $customer_result = $wpdb->get_row(
8324 $wpdb->prepare(
8325 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8326 [ $result['customerId'] ]
8327 ),
8328 ARRAY_A
8329 );
8330
8331 if ( $result['couponId'] ) {
8332 $coupon_result = $wpdb->get_row(
8333 $wpdb->prepare(
8334 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8335 [ $result['couponId'] ]
8336 ),
8337 ARRAY_A
8338 );
8339 } else {
8340 $coupon_result = [];
8341 }
8342
8343 if ( ! empty( $result['customFields'] ) ) {
8344 $custom_fields = json_decode( $result['customFields'], true );
8345
8346 $fields_arr = [];
8347 foreach ( (array) $custom_fields as $fields ) {
8348 if ( is_array( $fields ) ) {
8349 $fields_arr[ $fields['label'] ] = $fields['value'];
8350 }
8351 }
8352 unset( $result['customFields'] );
8353 } else {
8354 $fields_arr = [];
8355 }
8356
8357 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
8358 $context['response_type'] = 'live';
8359 } else {
8360
8361 $context = json_decode( '{"response_type": "sample","pluggable_data": {"id": "1","appointmentId": null,"customerId": "1","status": "visible","price": "10","persons": "1","couponId": null,"token": "6485b07ce9","info": "{\"firstName\":\"John\",\"lastName\":\"Doe\",\"phone\":\"+213551223123\",\"locale\":\"en_US\",\"timeZone\":\"Asia\\/Kolkata\",\"urlParams\":null}","utcOffset": null,"aggregatedPrice": "1","packageCustomerServiceId": null,"duration": null,"created": "2023-02-02 06:35:18","actionsCompleted": "1","Do You Know Automation?": "Yes","When Are You Coming?": "2023-04-20","Upload Something": "","Tell Us About You!": "Hey there!","customerBookingId": "105","eventPeriodId": "5","parentId": null,"name": "Music Event","bookingOpens": null,"bookingCloses": "2023-02-09 08:00:00","bookingOpensRec": "same","bookingClosesRec": "same","ticketRangeRec": "calculate","recurringCycle": null,"recurringOrder": null,"recurringInterval": null,"recurringMonthly": null,"monthlyDate": null,"monthlyOnRepeat": null,"monthlyOnDay": null,"recurringUntil": null,"maxCapacity": "12","maxCustomCapacity": null,"maxExtraPeople": null,"locationId": null,"customLocation": "Kolkata","description": null,"color": "#1788FB","show": "1","notifyParticipants": "1","settings": "{\"payments\":{\"onSite\":true,\"payPal\":{\"enabled\":false},\"stripe\":{\"enabled\":false},\"mollie\":{\"enabled\":false},\"razorpay\":{\"enabled\":false}},\"general\":{\"minimumTimeRequirementPriorToCanceling\":null,\"redirectUrlAfterAppointment\":null},\"zoom\":{\"enabled\":false},\"lessonSpace\":{\"enabled\":false}}","zoomUserId": null,"bringingAnyone": "1","bookMultipleTimes": "1","translations": "{\"defaultLanguage\":\"en_US\"}","depositPayment": "disabled","depositPerPerson": "1","fullPayment": "0","deposit": "0","customPricing": "0","organizerId": "2","closeAfterMin": null,"closeAfterMinBookings": "0","type": "customer","externalId": "91","firstName": "John","lastName": "Doe","email": "johnd@gmail.com","birthday": null,"phone": "+213551223123","gender": null,"note": null,"pictureFullPath": null,"pictureThumbPath": null,"password": null,"usedTokens": null,"countryPhoneIso": "dz","timeZone": null}}', true );
8362 }
8363
8364 return $context;
8365 }
8366
8367 /**
8368 * Get last data for trigger.
8369 *
8370 * @param array $data data.
8371 * @return array
8372 */
8373 public function search_amelia_event_booking_cancelled_triggers_last_data( $data ) {
8374 global $wpdb;
8375
8376 $context = [];
8377
8378 $event_selected = $data['filter']['amelia_events_list']['value'];
8379
8380 if ( -1 === $event_selected ) {
8381 $result = $wpdb->get_row(
8382 'SELECT *
8383 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8384 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8385 ON customer.id = event_period.customerBookingId
8386 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND customer.status = "canceled"',
8387 ARRAY_A
8388 );
8389 } else {
8390 $result = $wpdb->get_row(
8391 $wpdb->prepare(
8392 'SELECT *
8393 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8394 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8395 ON customer.id = event_period.customerBookingId
8396 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId = %d AND customer.status = "canceled"',
8397 [ $event_selected ]
8398 ),
8399 ARRAY_A
8400 );
8401 }
8402
8403 if ( ! empty( $result ) ) {
8404 $event = $wpdb->get_row(
8405 $wpdb->prepare(
8406 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
8407 [ $result['eventPeriodId'] ]
8408 ),
8409 ARRAY_A
8410 );
8411 $event_tags = $wpdb->get_results(
8412 $wpdb->prepare(
8413 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
8414 [ $result['eventPeriodId'] ]
8415 ),
8416 ARRAY_A
8417 );
8418 $tags = [];
8419 if ( ! empty( $event_tags ) ) {
8420 foreach ( $event_tags as $key => $tag ) {
8421 $tags['event_tag'][ $key ] = $tag['name'];
8422 }
8423 } else {
8424 $tags = [];
8425 }
8426 $customer_result = $wpdb->get_row(
8427 $wpdb->prepare(
8428 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8429 [ $result['customerId'] ]
8430 ),
8431 ARRAY_A
8432 );
8433 if ( $result['couponId'] ) {
8434 $coupon_result = $wpdb->get_row(
8435 $wpdb->prepare(
8436 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8437 [ $result['couponId'] ]
8438 ),
8439 ARRAY_A
8440 );
8441 } else {
8442 $coupon_result = [];
8443 }
8444 if ( ! empty( $result['customFields'] ) ) {
8445 $custom_fields = json_decode( $result['customFields'], true );
8446
8447 $fields_arr = [];
8448 foreach ( (array) $custom_fields as $fields ) {
8449 if ( is_array( $fields ) ) {
8450 $fields_arr[ $fields['label'] ] = $fields['value'];
8451 }
8452 }
8453 unset( $result['customFields'] );
8454 } else {
8455 $fields_arr = [];
8456 }
8457 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
8458 $context['response_type'] = 'live';
8459 } else {
8460
8461 $context = json_decode( '{"response_type": "sample","pluggable_data": {"id": "1","appointmentId": null,"customerId": "1","status": "visible","price": "10","persons": "1","couponId": null,"token": "6485b07ce9","info": "{\"firstName\":\"John\",\"lastName\":\"Doe\",\"phone\":\"+213551223123\",\"locale\":\"en_US\",\"timeZone\":\"Asia\\/Kolkata\",\"urlParams\":null}","utcOffset": null,"aggregatedPrice": "1","packageCustomerServiceId": null,"duration": null,"created": "2023-02-02 06:35:18","actionsCompleted": "1","Do You Know Automation?": "Yes","When Are You Coming?": "2023-04-20","Upload Something": "","Tell Us About You!": "Hey there!","customerBookingId": "105","eventPeriodId": "5","parentId": null,"name": "Music Event","bookingOpens": null,"bookingCloses": "2023-02-09 08:00:00","bookingOpensRec": "same","bookingClosesRec": "same","ticketRangeRec": "calculate","recurringCycle": null,"recurringOrder": null,"recurringInterval": null,"recurringMonthly": null,"monthlyDate": null,"monthlyOnRepeat": null,"monthlyOnDay": null,"recurringUntil": null,"maxCapacity": "12","maxCustomCapacity": null,"maxExtraPeople": null,"locationId": null,"customLocation": "Kolkata","description": null,"color": "#1788FB","show": "1","notifyParticipants": "1","settings": "{\"payments\":{\"onSite\":true,\"payPal\":{\"enabled\":false},\"stripe\":{\"enabled\":false},\"mollie\":{\"enabled\":false},\"razorpay\":{\"enabled\":false}},\"general\":{\"minimumTimeRequirementPriorToCanceling\":null,\"redirectUrlAfterAppointment\":null},\"zoom\":{\"enabled\":false},\"lessonSpace\":{\"enabled\":false}}","zoomUserId": null,"bringingAnyone": "1","bookMultipleTimes": "1","translations": "{\"defaultLanguage\":\"en_US\"}","depositPayment": "disabled","depositPerPerson": "1","fullPayment": "0","deposit": "0","customPricing": "0","organizerId": "2","closeAfterMin": null,"closeAfterMinBookings": "0","type": "customer","externalId": "91","firstName": "Jogn","lastName": "Doe","email": "johnd@gmail.com","birthday": null,"phone": "+213551223123","gender": null,"note": null,"pictureFullPath": null,"pictureThumbPath": null,"password": null,"usedTokens": null,"countryPhoneIso": "dz","timeZone": null}}', true );
8462 }
8463
8464 return $context;
8465 }
8466
8467 /**
8468 * Get last data for trigger.
8469 *
8470 * @param array $data data.
8471 * @return array
8472 */
8473 public function search_amelia_appointment_rescheduled_triggers_last_data( $data ) {
8474 global $wpdb;
8475
8476 $context = [];
8477
8478 $appointment_category = $data['filter']['amelia_category_list']['value'];
8479 $appointment_service = $data['filter']['amelia_service_list']['value'];
8480
8481 if ( -1 === $appointment_service ) {
8482 // If service exists as per category selected.
8483 $service_exist = $wpdb->get_row(
8484 $wpdb->prepare(
8485 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
8486 [ $appointment_category ]
8487 ),
8488 ARRAY_A
8489 );
8490
8491 if ( empty( $service_exist ) ) {
8492 $result = [];
8493 } else {
8494 $result = $wpdb->get_row(
8495 'SELECT appointments.*, customer.* FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer INNER JOIN ' . $wpdb->prefix . 'amelia_appointments as appointments ON customer.appointmentId=appointments.id WHERE customer.appointmentId = ( SELECT max(id) FROM ' . $wpdb->prefix . 'amelia_appointments )',
8496 ARRAY_A
8497 );
8498 }
8499 } else {
8500 $result = $wpdb->get_row(
8501 $wpdb->prepare(
8502 'SELECT appointments.*, customer.* FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer INNER JOIN ' . $wpdb->prefix . 'amelia_appointments as appointments ON customer.appointmentId=appointments.id WHERE customer.appointmentId = ( SELECT max(id) FROM ' . $wpdb->prefix . 'amelia_appointments ) AND appointments.serviceId = %d',
8503 [ $appointment_service ]
8504 ),
8505 ARRAY_A
8506 );
8507 }
8508
8509 if ( ! empty( $result ) ) {
8510
8511 $payment_result = $wpdb->get_row(
8512 $wpdb->prepare(
8513 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
8514 [ $result['id'] ]
8515 ),
8516 ARRAY_A
8517 );
8518
8519 $customer_result = $wpdb->get_row(
8520 $wpdb->prepare(
8521 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8522 [ $result['customerId'] ]
8523 ),
8524 ARRAY_A
8525 );
8526
8527 $service_result = $wpdb->get_row(
8528 $wpdb->prepare(
8529 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
8530 [ $result['serviceId'] ]
8531 ),
8532 ARRAY_A
8533 );
8534
8535 $category_result = $wpdb->get_row(
8536 $wpdb->prepare(
8537 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
8538 [ $service_result['categoryId'] ]
8539 ),
8540 ARRAY_A
8541 );
8542
8543 if ( $result['couponId'] ) {
8544 $coupon_result = $wpdb->get_row(
8545 $wpdb->prepare(
8546 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8547 [ $result['couponId'] ]
8548 ),
8549 ARRAY_A
8550 );
8551 } else {
8552 $coupon_result = [];
8553 }
8554
8555 if ( ! empty( $result['customFields'] ) ) {
8556 $custom_fields = json_decode( $result['customFields'], true );
8557
8558 $fields_arr = [];
8559 foreach ( (array) $custom_fields as $fields ) {
8560 if ( is_array( $fields ) ) {
8561 $fields_arr[ $fields['label'] ] = $fields['value'];
8562 }
8563 }
8564 unset( $result['customFields'] );
8565 } else {
8566 $fields_arr = [];
8567 }
8568
8569 $appointment_data['isRescheduled'] = '1';
8570 $context['pluggable_data'] = array_merge( $result, $fields_arr, $appointment_data, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
8571 $context['response_type'] = 'live';
8572 } else {
8573
8574 $context = json_decode( '{"response_type":"sample","pluggable_data":{"id":"1","status":"visible","bookingStart":"2023-02-28 15:30:00","bookingEnd":"2023-02-28 16:30:00","notifyParticipants":"1","serviceId":"4","packageId":null,"providerId":"2","locationId":null,"internalNotes":"","googleCalendarEventId":null,"googleMeetUrl":null,"outlookCalendarEventId":null,"zoomMeeting":null,"lessonSpace":null,"parentId":null,"appointmentId":"54","customerId":"1","price":"15","persons":"1","couponId":null,"token":"02cf0988c6","info":"{\"firstName\":\"John\",\"lastName\":\"Doe\",\"phone\":\"1 (234) 789\",\"locale\":\"en_US\",\"timeZone\":\"Asia\\\/Kolkata\",\"urlParams\":null}","utcOffset":null,"aggregatedPrice":"1","packageCustomerServiceId":null,"duration":"3600","created":"2023-02-08 11:16:03","actionsCompleted":"1","Do You Know Automation?":"Yes","When Are You Coming?":"2023-04-20","Upload Something":"","Tell Us About You!":"Hey there!","isRescheduled":"1","customerBookingId":"103","amount":"0","dateTime":"2023-02-28 15:30:00","gateway":"onSite","gatewayTitle":"","data":"","packageCustomerId":null,"entity":"appointment","wcOrderId":null,"type":"customer","externalId":"89","firstName":"John","lastName":"Doe","email":"johnd@gmail.com","birthday":null,"phone":"1 (234) 789","gender":null,"note":null,"description":null,"pictureFullPath":null,"pictureThumbPath":null,"password":null,"usedTokens":null,"zoomUserId":null,"countryPhoneIso":"us","translations":"{\"defaultLanguage\":\"en_US\"}","timeZone":null,"serviceName":"demo service","serviceDescription":"","categoryId":"2","categoryName":"New Category1"}}', true );
8575 }
8576
8577 return $context;
8578 }
8579
8580
8581 /**
8582 * Get last data for trigger.
8583 *
8584 * @param array $data data.
8585 * @return array
8586 */
8587 public function search_amelia_appointment_cancelled_triggers_last_data( $data ) {
8588 global $wpdb;
8589
8590 $context = [];
8591
8592 $appointment_category = $data['filter']['amelia_category_list']['value'];
8593 $appointment_service = $data['filter']['amelia_service_list']['value'];
8594
8595 if ( -1 === $appointment_service ) {
8596 // If service exists as per category selected.
8597 $service_exist = $wpdb->get_row(
8598 $wpdb->prepare(
8599 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
8600 [ $appointment_category ]
8601 ),
8602 ARRAY_A
8603 );
8604
8605 if ( empty( $service_exist ) ) {
8606 $result = [];
8607 } else {
8608 $result = $wpdb->get_row(
8609 $wpdb->prepare(
8610 'SELECT appointments.*, customer.* FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer INNER JOIN ' . $wpdb->prefix . 'amelia_appointments as appointments ON customer.appointmentId=appointments.id WHERE appointments.status = %s order by RAND() DESC LIMIT 1',
8611 [ 'canceled' ]
8612 ),
8613 ARRAY_A
8614 );
8615 }
8616 } else {
8617 $result = $wpdb->get_row(
8618 $wpdb->prepare(
8619 'SELECT appointments.*, customer.* FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer INNER JOIN ' . $wpdb->prefix . 'amelia_appointments as appointments ON customer.appointmentId=appointments.id WHERE appointments.status = %s AND appointments.serviceId = %d order by RAND() DESC LIMIT 1',
8620 [ 'canceled', $appointment_service ]
8621 ),
8622 ARRAY_A
8623 );
8624 }
8625
8626 if ( ! empty( $result ) ) {
8627
8628 $payment_result = $wpdb->get_row(
8629 $wpdb->prepare(
8630 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
8631 [ $result['id'] ]
8632 ),
8633 ARRAY_A
8634 );
8635
8636 $customer_result = $wpdb->get_row(
8637 $wpdb->prepare(
8638 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8639 [ $result['customerId'] ]
8640 ),
8641 ARRAY_A
8642 );
8643
8644 $service_result = $wpdb->get_row(
8645 $wpdb->prepare(
8646 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
8647 [ $result['serviceId'] ]
8648 ),
8649 ARRAY_A
8650 );
8651
8652 $category_result = $wpdb->get_row(
8653 $wpdb->prepare(
8654 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
8655 [ $service_result['categoryId'] ]
8656 ),
8657 ARRAY_A
8658 );
8659
8660 if ( $result['couponId'] ) {
8661 $coupon_result = $wpdb->get_row(
8662 $wpdb->prepare(
8663 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8664 [ $result['couponId'] ]
8665 ),
8666 ARRAY_A
8667 );
8668 } else {
8669 $coupon_result = [];
8670 }
8671
8672 if ( ! empty( $result['customFields'] ) ) {
8673 $custom_fields = json_decode( $result['customFields'], true );
8674
8675 $fields_arr = [];
8676 foreach ( (array) $custom_fields as $fields ) {
8677 if ( is_array( $fields ) ) {
8678 $fields_arr[ $fields['label'] ] = $fields['value'];
8679 }
8680 }
8681 unset( $result['customFields'] );
8682 } else {
8683 $fields_arr = [];
8684 }
8685
8686 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
8687 $context['response_type'] = 'live';
8688 } else {
8689
8690 $context = json_decode( '{"response_type":"sample","pluggable_data":{"id":"1","status":"visible","bookingStart":"2023-02-28 15:30:00","bookingEnd":"2023-02-28 16:30:00","notifyParticipants":"1","serviceId":"4","packageId":null,"providerId":"2","locationId":null,"internalNotes":"","googleCalendarEventId":null,"googleMeetUrl":null,"outlookCalendarEventId":null,"zoomMeeting":null,"lessonSpace":null,"parentId":null,"appointmentId":"54","customerId":"1","price":"15","persons":"1","couponId":null,"token":"02cf0988c6","info":"{\"firstName\":\"John\",\"lastName\":\"Doe\",\"phone\":\"1 (234) 789\",\"locale\":\"en_US\",\"timeZone\":\"Asia\\\/Kolkata\",\"urlParams\":null}","utcOffset":null,"aggregatedPrice":"1","packageCustomerServiceId":null,"duration":"3600","created":"2023-02-08 11:16:03","actionsCompleted":"1","Do You Know Automation?":"Yes","When Are You Coming?":"2023-04-20","Upload Something":"","Tell Us About You!":"Hey there!","customerBookingId":"103","amount":"0","dateTime":"2023-02-28 15:30:00","gateway":"onSite","gatewayTitle":"","data":"","packageCustomerId":null,"entity":"appointment","wcOrderId":null,"type":"customer","externalId":"89","firstName":"John","lastName":"Doe","email":"johnd@gmail.com","birthday":null,"phone":"1 (234) 789","gender":null,"note":null,"description":null,"pictureFullPath":null,"pictureThumbPath":null,"password":null,"usedTokens":null,"zoomUserId":null,"countryPhoneIso":"us","translations":"{\"defaultLanguage\":\"en_US\"}","timeZone":null,"serviceName":"demo service","serviceDescription":"","categoryId":"2","categoryName":"New Category1"}}', true );
8691 }
8692
8693 return $context;
8694 }
8695
8696 /**
8697 * Get MailPoet Forms.
8698 *
8699 * @param array $data data.
8700 *
8701 * @return array
8702 */
8703 public function search_mailpoet_forms( $data ) {
8704 if ( ! class_exists( '\MailPoet\API\API' ) ) {
8705 return;
8706 }
8707
8708 global $wpdb;
8709
8710 $page = $data['page'];
8711 $limit = Utilities::get_search_page_limit();
8712 $offset = $limit * ( $page - 1 );
8713
8714 $forms = $wpdb->get_results(
8715 $wpdb->prepare(
8716 'SELECT SQL_CALC_FOUND_ROWS * FROM ' . $wpdb->prefix . 'mailpoet_forms WHERE `deleted_at` IS NULL AND `status` = %s ORDER BY id LIMIT %d OFFSET %d',
8717 [ 'enabled', $limit, $offset ]
8718 )
8719 );
8720
8721 $form_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
8722
8723 $options = [];
8724
8725 if ( ! empty( $forms ) ) {
8726 if ( is_array( $forms ) ) {
8727 foreach ( $forms as $form ) {
8728 $options[] = [
8729 'label' => $form->name,
8730 'value' => $form->id,
8731 ];
8732 }
8733 }
8734 }
8735
8736 return [
8737 'options' => $options,
8738 'hasMore' => $form_count > $limit && $form_count > $offset,
8739 ];
8740
8741 }
8742
8743 /**
8744 * Get MailPoet List.
8745 *
8746 * @param array $data data.
8747 *
8748 * @return array
8749 */
8750 public function search_mailpoet_list( $data ) {
8751 if ( ! class_exists( '\MailPoet\API\API' ) ) {
8752 return;
8753 }
8754
8755 $mailpoet = \MailPoet\API\API::MP( 'v1' );
8756 $lists = $mailpoet->getLists();
8757
8758 $options = [];
8759
8760 if ( ! empty( $lists ) ) {
8761 if ( is_array( $lists ) ) {
8762 foreach ( $lists as $list ) {
8763 $options[] = [
8764 'label' => $list['name'],
8765 'value' => $list['id'],
8766 ];
8767 }
8768 }
8769 }
8770
8771 return [
8772 'options' => $options,
8773 'hasMore' => false,
8774 ];
8775 }
8776
8777 /**
8778 * Get MailPoet Subscriber Status.
8779 *
8780 * @param array $data data.
8781 *
8782 * @return array
8783 */
8784 public function search_mailpoet_subscriber_status( $data ) {
8785 if ( ! class_exists( '\MailPoet\API\API' ) ) {
8786 return;
8787 }
8788
8789 $subscriber_status = [
8790 'subscribed' => 'Subscribed',
8791 'unconfirmed' => 'Unconfirmed',
8792 'unsubscribed' => 'Unsubscribed',
8793 'inactive' => 'Inactive',
8794 'bounced' => 'Bounced',
8795 ];
8796
8797 $options = [];
8798 foreach ( $subscriber_status as $key => $status ) {
8799 $options[] = [
8800 'label' => $status,
8801 'value' => $key,
8802 ];
8803 }
8804
8805 return [
8806 'options' => $options,
8807 'hasMore' => false,
8808 ];
8809 }
8810
8811 /**
8812 * Get MailPoet Subscribers.
8813 *
8814 * @param array $data data.
8815 *
8816 * @return array
8817 */
8818 public function search_mailpoet_subscribers( $data ) {
8819 if ( ! class_exists( '\MailPoet\API\API' ) ) {
8820 return;
8821 }
8822
8823 global $wpdb;
8824
8825 $page = $data['page'];
8826 $limit = Utilities::get_search_page_limit();
8827 $offset = $limit * ( $page - 1 );
8828
8829 $subscribers = $wpdb->get_results(
8830 $wpdb->prepare(
8831 'SELECT SQL_CALC_FOUND_ROWS id,email FROM ' . $wpdb->prefix . 'mailpoet_subscribers ORDER BY id DESC LIMIT %d OFFSET %d',
8832 [ $limit, $offset ]
8833 )
8834 );
8835
8836 $subscribers_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
8837
8838 $options = [];
8839
8840 if ( ! empty( $subscribers ) ) {
8841 if ( is_array( $subscribers ) ) {
8842 foreach ( $subscribers as $subscriber ) {
8843 $options[] = [
8844 'label' => $subscriber->email,
8845 'value' => $subscriber->id,
8846 ];
8847 }
8848 }
8849 }
8850
8851 return [
8852 'options' => $options,
8853 'hasMore' => $subscribers_count > $limit && $subscribers_count > $offset,
8854 ];
8855 }
8856
8857 /**
8858 * Get ConvertPro Forms.
8859 *
8860 * @param array $data data.
8861 *
8862 * @return array
8863 */
8864 public function search_convertpro_form_list( $data ) {
8865 if ( ! class_exists( '\Cp_V2_Loader' ) ) {
8866 return;
8867 }
8868
8869 $cp_popups_inst = CP_V2_Popups::get_instance();
8870 $popups = $cp_popups_inst->get_all();
8871
8872 $form_count = count( $popups );
8873
8874 $page = $data['page'];
8875 $limit = Utilities::get_search_page_limit();
8876 $offset = $limit * ( $page - 1 );
8877
8878 $options = [];
8879
8880 if ( ! empty( $popups ) ) {
8881 if ( is_array( $popups ) ) {
8882 foreach ( $popups as $form ) {
8883 $options[] = [
8884 'label' => $form->post_title,
8885 'value' => $form->ID,
8886 ];
8887 }
8888 }
8889 }
8890
8891 return [
8892 'options' => $options,
8893 'hasMore' => $form_count > $limit && $form_count > $offset,
8894 ];
8895
8896 }
8897
8898 /**
8899 * Get ProjectHuddle Websites.
8900 *
8901 * @param array $data data.
8902 *
8903 * @return array
8904 */
8905 public function search_project_huddle_websites( $data ) {
8906
8907 $sites = new WP_Query(
8908 [
8909 'post_type' => 'ph-website',
8910 'posts_per_page' => - 1,
8911 'fields' => 'ids',
8912 ]
8913 );
8914
8915 $site_ids = (array) $sites->posts;
8916
8917 $options = [];
8918 if ( ! empty( $site_ids ) ) {
8919 if ( is_array( $site_ids ) ) {
8920 foreach ( $site_ids as $site_id ) {
8921 $options[] = [
8922 'label' => get_the_title( $site_id ),
8923 'value' => $site_id,
8924 ];
8925 }
8926 }
8927 }
8928 return [
8929 'options' => $options,
8930 'hasMore' => false,
8931 ];
8932
8933 }
8934
8935 /**
8936 * Get last data for trigger.
8937 *
8938 * @param array $data data.
8939 * @return mixed
8940 */
8941 public function search_project_huddle_comment_triggers_last_data( $data ) {
8942 global $wpdb;
8943
8944 $context = [];
8945
8946 if ( -1 !== $data['dynamic'] ) {
8947 $threads = get_posts(
8948 [
8949 'post_type' => 'phw_comment_loc',
8950 'posts_per_page' => 1,
8951 'meta_value' => $data['dynamic'],
8952 'meta_key' => 'project_id',
8953 'orderby' => 'asc',
8954 ]
8955 );
8956 } else {
8957 $threads = [];
8958 }
8959
8960 if ( ! empty( $threads ) ) {
8961 $comment_result = $wpdb->get_row(
8962 $wpdb->prepare(
8963 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
8964 FROM ' . $wpdb->prefix . 'comments
8965 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
8966 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
8967 LIMIT 0,1',
8968 $threads[0]->ID
8969 ),
8970 ARRAY_A
8971 );
8972
8973 if ( ! empty( $comment_result ) ) {
8974 $comment_id = get_comment( $comment_result['comment_ID'], ARRAY_A );
8975 $comments['comment_ID'] = isset( $comment_id['comment_ID'] ) ? $comment_id['comment_ID'] : '';
8976 $comments['comment_post_ID'] = isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : '';
8977
8978 $comments['comment_author'] = isset( $comment_id['comment_author'] ) ? $comment_id['comment_author'] : '';
8979
8980 $comments['comment_author_email'] = isset( $comment_id['comment_author_email'] ) ? $comment_id['comment_author_email'] : '';
8981
8982 $comments['comment_date'] = isset( $comment_id['comment_date'] ) ? $comment_id['comment_date'] : '';
8983
8984 $comments['comment_content'] = isset( $comment_id['comment_content'] ) ? $comment_id['comment_content'] : '';
8985
8986 $comments['comment_type'] = isset( $comment_id['comment_type'] ) ? $comment_id['comment_type'] : '';
8987
8988 $comment_item_id = get_comment_meta( $comment_result['comment_ID'], 'item_id' );
8989 if ( ! empty( $comment_item_id ) && is_array( $comment_item_id ) ) {
8990 $comments['comment_item_id'] = $comment_item_id[0];
8991 $comments['comment_item_page_title'] = get_the_title( (int) $comment_item_id[0] );
8992 $comments['comment_item_page_url'] = get_post_meta( (int) $comment_item_id[0], 'page_url', true );
8993 }
8994
8995 $context['pluggable_data'] = $comments;
8996 $context['response_type'] = 'live';
8997 } else {
8998 $context = json_decode( '{"response_type":"sample","pluggable_data":{"comment_ID":"1","comment_post_ID":"1","comment_author":"test","comment_author_email":"test@test.com","comment_date":"2023-03-27 13:44:26","comment_content":"<p>Leave comment<\/p>","comment_type":"ph_comment"}}', true );
8999 }
9000 } else {
9001 $context = json_decode( '{"response_type":"sample","pluggable_data":{"comment_ID":"1","comment_post_ID":"1","comment_author":"test","comment_author_email":"test@test.com","comment_date":"2023-03-27 13:44:26","comment_content":"<p>Leave comment<\/p>","comment_type":"ph_comment","comment_item_id": 9579, "comment_item_page_title": "About Us","comment_item_page_url": "https://example.com/abotu-us"}}', true );
9002 }
9003
9004 return $context;
9005 }
9006
9007 /**
9008 * Get last data for trigger.
9009 *
9010 * @param array $data data.
9011 * @return mixed
9012 */
9013 public function search_project_huddle_resolved_comment_triggers_last_data( $data ) {
9014 global $wpdb;
9015
9016 $context = [];
9017
9018 $get_comments = $wpdb->get_row(
9019 'SELECT ' . $wpdb->prefix . 'comments.comment_ID, ' . $wpdb->prefix . 'comments.comment_content
9020 FROM ' . $wpdb->prefix . 'comments
9021 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_status") AND comment_content = "Resolved"
9022 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
9023 LIMIT 0,1'
9024 );
9025
9026 if ( ! empty( $get_comments ) ) {
9027 $comment_id = get_comment( $get_comments->comment_ID, ARRAY_A );
9028 $comment_result = $wpdb->get_row(
9029 $wpdb->prepare(
9030 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
9031 FROM ' . $wpdb->prefix . 'comments
9032 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
9033 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
9034 LIMIT 0,1',
9035 isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : ''
9036 ),
9037 ARRAY_A
9038 );
9039
9040 $actual_comment = get_comment( $comment_result['comment_ID'], ARRAY_A );
9041 $comments['comment_ID'] = isset( $actual_comment['comment_ID'] ) ? $actual_comment['comment_ID'] : '';
9042 $comments['comment_post_ID'] = isset( $actual_comment['comment_post_ID'] ) ? $actual_comment['comment_post_ID'] : '';
9043 $comments['comment_author'] = isset( $actual_comment['comment_author'] ) ? $actual_comment['comment_author'] : '';
9044 $comments['comment_author_email'] = isset( $actual_comment['comment_author_email'] ) ? $actual_comment['comment_author_email'] : '';
9045 $comments['comment_date'] = isset( $actual_comment['comment_date'] ) ? $actual_comment['comment_date'] : '';
9046 $comments['comment_content'] = isset( $actual_comment['comment_content'] ) ? $actual_comment['comment_content'] : '';
9047 $comments['comment_type'] = isset( $actual_comment['comment_type'] ) ? $actual_comment['comment_type'] : '';
9048 $comments['comment_status'] = $get_comments->comment_content;
9049 $comment_item_id = get_comment_meta( $comment_result['comment_ID'], 'item_id' );
9050 if ( ! empty( $comment_item_id ) && is_array( $comment_item_id ) ) {
9051 $comments['comment_item_id'] = $comment_item_id[0];
9052 $comments['comment_item_page_title'] = get_the_title( (int) $comment_item_id[0] );
9053 $comments['comment_item_page_url'] = get_post_meta( (int) $comment_item_id[0], 'page_url', true );
9054 }
9055 $context['pluggable_data'] = $comments;
9056 $context['response_type'] = 'live';
9057 } else {
9058 $context = json_decode( '{"response_type":"sample","pluggable_data":{"comment_ID":"1","comment_post_ID":"1","comment_author":"test","comment_author_email":"test@test.com","comment_date":"2023-03-27 13:44:26","comment_content":"<p>Leave comment<\/p>","comment_type":"ph_comment","comment_status":"Resolved","comment_item_id": 9579, "comment_item_page_title": "About Us","comment_item_page_url": "https://example.com/abotu-us" }}', true );
9059 }
9060
9061 return $context;
9062 }
9063
9064 /**
9065 * Get MasterStudy LMS Courses.
9066 *
9067 * @param array $data data.
9068 *
9069 * @return array
9070 */
9071 public function search_ms_lms_courses( $data ) {
9072
9073 $page = $data['page'];
9074 $limit = Utilities::get_search_page_limit();
9075 $offset = $limit * ( $page - 1 );
9076
9077 $args = [
9078 'post_type' => 'stm-courses',
9079 'posts_per_page' => $limit,
9080 'offset' => $offset,
9081 'orderby' => 'title',
9082 'order' => 'ASC',
9083 'post_status' => 'publish',
9084 ];
9085
9086 $courses = get_posts( $args );
9087
9088 $course_count = wp_count_posts( 'stm-courses' )->publish;
9089
9090 $options = [];
9091 if ( ! empty( $courses ) ) {
9092 if ( is_array( $courses ) ) {
9093 foreach ( $courses as $course ) {
9094 $options[] = [
9095 'label' => $course->post_title,
9096 'value' => $course->ID,
9097 ];
9098 }
9099 }
9100 }
9101 return [
9102 'options' => $options,
9103 'hasMore' => $course_count > $limit && $course_count > $offset,
9104 ];
9105
9106 }
9107
9108 /**
9109 * Get MasterStudy LMS Lessons.
9110 *
9111 * @param array $data data.
9112 *
9113 * @return array
9114 */
9115 public function search_ms_lms_lessons( $data ) {
9116
9117 global $wpdb;
9118 $options = [];
9119 $course_id = $data['dynamic'];
9120
9121 // Use curriculum repository class.
9122 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
9123 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
9124 } else {
9125 $curriculum_repo = false;
9126 }
9127
9128 if ( '-1' === $course_id ) {
9129 $lessons = $wpdb->get_results(
9130 $wpdb->prepare(
9131 "SELECT SQL_CALC_FOUND_ROWS ID, post_title FROM $wpdb->posts WHERE post_type = %s ORDER BY post_title ASC",
9132 'stm-lessons'
9133 )
9134 );
9135
9136 if ( ! empty( $lessons ) ) {
9137 if ( is_array( $lessons ) ) {
9138 foreach ( $lessons as $lesson ) {
9139 $options[] = [
9140 'label' => $lesson->post_title,
9141 'value' => $lesson->ID,
9142 ];
9143 }
9144 }
9145 }
9146 } else {
9147 $lessons = [];
9148 if ( $curriculum_repo ) {
9149 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
9150 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
9151 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
9152 foreach ( $curriculum['materials'] as $material ) {
9153 if ( 'stm-lessons' === $material['post_type'] ) {
9154 $lessons[] = [
9155 'value' => $material['post_id'],
9156 'text' => $material['title'],
9157 ];
9158 }
9159 }
9160 }
9161 }
9162 if ( ! empty( $lessons ) ) {
9163 if ( is_array( $lessons ) ) {
9164 foreach ( $lessons as $lesson ) {
9165 $options[] = [
9166 'label' => $lesson['text'],
9167 'value' => $lesson['value'],
9168 ];
9169 }
9170 }
9171 }
9172 } else {
9173 $lessons = $wpdb->get_results(
9174 $wpdb->prepare(
9175 "SELECT SQL_CALC_FOUND_ROWS ID, post_title
9176 FROM $wpdb->posts
9177 WHERE FIND_IN_SET(
9178 ID,
9179 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
9180 )
9181 AND post_type = 'stm-lessons'
9182 ORDER BY post_title ASC",
9183 absint( $course_id )
9184 )
9185 );
9186 if ( ! empty( $lessons ) ) {
9187 if ( is_array( $lessons ) ) {
9188 foreach ( $lessons as $lesson ) {
9189 $options[] = [
9190 'label' => $lesson->post_title,
9191 'value' => $lesson->ID,
9192 ];
9193 }
9194 }
9195 }
9196 }
9197 }
9198 return [
9199 'options' => $options,
9200 'hasMore' => false,
9201 ];
9202
9203 }
9204
9205 /**
9206 * Get MasterStudy LMS Quiz.
9207 *
9208 * @param array $data data.
9209 *
9210 * @return array
9211 */
9212 public function search_ms_lms_quiz( $data ) {
9213
9214 global $wpdb;
9215
9216 $options = [];
9217 $course_id = $data['dynamic'];
9218
9219 // Use curriculum repository class.
9220 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
9221 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
9222 } else {
9223 $curriculum_repo = false;
9224 }
9225
9226 if ( '-1' === $course_id ) {
9227 $quizzes = $wpdb->get_results(
9228 $wpdb->prepare(
9229 "SELECT ID, post_title
9230 FROM $wpdb->posts
9231 WHERE post_type = %s
9232 ORDER BY post_title ASC",
9233 'stm-quizzes'
9234 )
9235 );
9236 if ( ! empty( $quizzes ) ) {
9237 if ( is_array( $quizzes ) ) {
9238 foreach ( $quizzes as $quiz ) {
9239 $options[] = [
9240 'label' => $quiz->post_title,
9241 'value' => $quiz->ID,
9242 ];
9243 }
9244 }
9245 }
9246 } else {
9247 $quizzes = [];
9248 if ( $curriculum_repo ) {
9249 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
9250 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
9251 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
9252 foreach ( $curriculum['materials'] as $material ) {
9253 if ( 'stm-quizzes' === $material['post_type'] ) {
9254 $quizzes[] = [
9255 'value' => $material['post_id'],
9256 'text' => $material['title'],
9257 ];
9258 }
9259 }
9260 }
9261 }
9262 if ( ! empty( $quizzes ) ) {
9263 if ( is_array( $quizzes ) ) {
9264 foreach ( $quizzes as $quiz ) {
9265 $options[] = [
9266 'label' => $quiz['text'],
9267 'value' => $quiz['value'],
9268 ];
9269 }
9270 }
9271 }
9272 } else {
9273 $quizzes = $wpdb->get_results(
9274 $wpdb->prepare(
9275 "SELECT ID, post_title
9276 FROM $wpdb->posts
9277 WHERE FIND_IN_SET(
9278 ID,
9279 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
9280 )
9281 AND post_type = 'stm-quizzes'
9282 ORDER BY post_title ASC
9283 ",
9284 absint( $course_id )
9285 )
9286 );
9287 if ( ! empty( $quizzes ) ) {
9288 if ( is_array( $quizzes ) ) {
9289 foreach ( $quizzes as $quiz ) {
9290 $options[] = [
9291 'label' => $quiz->post_title,
9292 'value' => $quiz->ID,
9293 ];
9294 }
9295 }
9296 }
9297 }
9298 }
9299 return [
9300 'options' => $options,
9301 'hasMore' => false,
9302 ];
9303
9304 }
9305
9306 /**
9307 * Search MasterStudy LMS data.
9308 *
9309 * @param array $data data.
9310 * @return array|void
9311 */
9312 public function search_ms_lms_last_data( $data ) {
9313 global $wpdb;
9314 $post_type = $data['post_type'];
9315 $trigger = $data['search_term'];
9316 $context = [];
9317
9318 if ( 'stm_lms_course_completed' === $trigger ) {
9319 $post_id = $data['filter']['course']['value'];
9320 if ( -1 === $post_id ) {
9321 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}stm_lms_user_courses as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.course_id WHERE postmeta.progress_percent=100 AND posts.post_type=%s order by postmeta.user_course_id DESC LIMIT 1", $post_type ) );
9322 } else {
9323 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}stm_lms_user_courses as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.course_id WHERE postmeta.course_id = %s AND postmeta.progress_percent=100 AND posts.post_type=%s order by postmeta.user_course_id DESC LIMIT 1", $post_id, $post_type ) );
9324 }
9325 } elseif ( 'stm_lesson_passed' === $trigger ) {
9326 $post_id = $data['filter']['lesson']['value'];
9327 if ( -1 === $post_id ) {
9328 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}stm_lms_user_lessons as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.lesson_id WHERE posts.post_type=%s order by postmeta.user_lesson_id DESC LIMIT 1", $post_type ) );
9329 } else {
9330 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}stm_lms_user_lessons as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.lesson_id WHERE postmeta.lesson_id=%s AND posts.post_type=%s order by postmeta.user_lesson_id DESC LIMIT 1", $post_id, $post_type ) );
9331 }
9332 } elseif ( 'stm_quiz_passed' === $trigger ) {
9333 $post_id = $data['filter']['quiz']['value'];
9334 if ( -1 === $post_id ) {
9335 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}stm_lms_user_quizzes as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.quiz_id WHERE postmeta.status='passed' AND posts.post_type=%s order by postmeta.user_quiz_id DESC LIMIT 1", $post_type ) );
9336 } else {
9337 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}stm_lms_user_quizzes as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.quiz_id WHERE postmeta.quiz_id=%s AND postmeta.status='passed' AND posts.post_type=%s order by postmeta.user_quiz_id DESC LIMIT 1", $post_id, $post_type ) );
9338 }
9339 } elseif ( 'stm_quiz_failed' === $trigger ) {
9340 $post_id = $data['filter']['quiz']['value'];
9341 if ( -1 === $post_id ) {
9342 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}stm_lms_user_quizzes as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.quiz_id WHERE postmeta.status='failed' AND posts.post_type=%s order by postmeta.user_quiz_id DESC LIMIT 1", $post_type ) );
9343 } else {
9344 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}stm_lms_user_quizzes as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.quiz_id WHERE postmeta.quiz_id=%s AND postmeta.status='failed' AND posts.post_type=%s order by postmeta.user_quiz_id DESC LIMIT 1", $post_id, $post_type ) );
9345 }
9346 } elseif ( 'stm_lms_user_enroll_course' === $trigger ) {
9347 $post_id = $data['filter']['course']['value'];
9348 if ( -1 === $post_id ) {
9349 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}stm_lms_user_courses as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.course_id WHERE postmeta.status='enrolled' AND posts.post_type=%s order by postmeta.user_course_id DESC LIMIT 1", $post_type ) );
9350 } else {
9351 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}stm_lms_user_courses as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.course_id WHERE postmeta.course_id=%s AND postmeta.status='enrolled' AND posts.post_type=%s order by postmeta.user_course_id DESC LIMIT 1", $post_id, $post_type ) );
9352 }
9353 }
9354
9355 if ( ! empty( $result ) ) {
9356
9357 switch ( $trigger ) {
9358 case 'stm_lms_course_completed':
9359 $result_course_id = $result[0]->course_id;
9360 $result_user_id = $result[0]->user_id;
9361 $course = get_the_title( $result_course_id );
9362 $course_link = get_the_permalink( $result_course_id );
9363 $featured_image = get_the_post_thumbnail_url( $result_course_id );
9364
9365 $data = [
9366 'course_id' => $result_course_id,
9367 'course_title' => $course,
9368 'course_link' => $course_link,
9369 'course_featured_image' => $featured_image,
9370 'course_progress' => $result[0]->progress_percent,
9371 ];
9372 $context_data = array_merge(
9373 WordPress::get_user_context( $result_user_id ),
9374 $data
9375 );
9376 break;
9377 case 'stm_lesson_passed':
9378 $result_lesson_id = $result[0]->lesson_id;
9379 $result_user_id = $result[0]->user_id;
9380 $lesson = get_the_title( $result_lesson_id );
9381 $lesson_link = get_the_permalink( $result_lesson_id );
9382
9383 $data = [
9384 'lesson_id' => $result_lesson_id,
9385 'lesson_title' => $lesson,
9386 'lesson_link' => $lesson_link,
9387 ];
9388 $context_data = array_merge(
9389 WordPress::get_user_context( $result_user_id ),
9390 $data
9391 );
9392 break;
9393 case 'stm_quiz_passed':
9394 $result_quiz_id = $result[0]->quiz_id;
9395 $result_user_id = $result[0]->user_id;
9396 $quiz_title = get_the_title( $result_quiz_id );
9397 $quiz_link = get_the_permalink( $result_quiz_id );
9398
9399 $data = [
9400 'quiz_id' => $result_quiz_id,
9401 'quiz_title' => $quiz_title,
9402 'quiz_link' => $quiz_link,
9403 'quiz_score' => $result[0]->progress,
9404 'result' => 'passed',
9405 ];
9406 $context_data = array_merge(
9407 WordPress::get_user_context( $result_user_id ),
9408 $data
9409 );
9410 break;
9411 case 'stm_quiz_failed':
9412 $result_quiz_id = $result[0]->quiz_id;
9413 $result_user_id = $result[0]->user_id;
9414 $quiz_title = get_the_title( $result_quiz_id );
9415 $quiz_link = get_the_permalink( $result_quiz_id );
9416
9417 $data = [
9418 'quiz_id' => $result_quiz_id,
9419 'quiz_title' => $quiz_title,
9420 'quiz_link' => $quiz_link,
9421 'quiz_score' => $result[0]->progress,
9422 'result' => 'failed',
9423 ];
9424 $context_data = array_merge(
9425 WordPress::get_user_context( $result_user_id ),
9426 $data
9427 );
9428 break;
9429 case 'stm_lms_user_enroll_course':
9430 $result_course_id = $result[0]->course_id;
9431 $result_user_id = $result[0]->user_id;
9432
9433 $course = get_the_title( $result_course_id );
9434 $course_link = get_the_permalink( $result_course_id );
9435 $featured_image = get_the_post_thumbnail_url( $result_course_id );
9436
9437 $data = [
9438 'course_id' => $result_course_id,
9439 'course_title' => $course,
9440 'course_link' => $course_link,
9441 'course_featured_image' => $featured_image,
9442 ];
9443 $context_data = array_merge(
9444 WordPress::get_user_context( $result_user_id ),
9445 $data
9446 );
9447 break;
9448 default:
9449 return;
9450 }
9451 $context['pluggable_data'] = $context_data;
9452 $context['response_type'] = 'live';
9453 }
9454
9455 return $context;
9456
9457 }
9458
9459 /**
9460 * Prepare Fluent Support Mailbox list.
9461 *
9462 * @param array $data data.
9463 *
9464 * @return array
9465 */
9466 public function search_fs_mailbox_list( $data ) {
9467 $options = [];
9468 global $wpdb;
9469 $mailboxes = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_mail_boxes ORDER BY id DESC", ARRAY_A );
9470 if ( ! empty( $mailboxes ) ) {
9471 foreach ( $mailboxes as $key => $value ) {
9472
9473 $options[] = [
9474 'label' => $value['name'],
9475 'value' => $value['id'],
9476 ];
9477
9478 }
9479 }
9480
9481 return [
9482 'options' => $options,
9483 'hasMore' => false,
9484 ];
9485 }
9486
9487 /**
9488 * Prepare Fluent Support Product list.
9489 *
9490 * @param array $data data.
9491 *
9492 * @return array
9493 */
9494 public function search_fs_product_list( $data ) {
9495 $options = [];
9496
9497 if ( ! class_exists( '\FluentSupport\App\Models\Product' ) ) {
9498 return [];
9499 }
9500
9501 $products = \FluentSupport\App\Models\Product::select( [ 'id', 'title' ] )->get();
9502 if ( ! empty( $products ) ) {
9503 foreach ( $products as $key => $product ) {
9504 $options[] = [
9505 'label' => $product['title'],
9506 'value' => $product['id'],
9507 ];
9508 }
9509 }
9510
9511 return [
9512 'options' => $options,
9513 'hasMore' => false,
9514 ];
9515 }
9516
9517 /**
9518 * Prepare Fluent Support Product list.
9519 *
9520 * @param array $data data.
9521 *
9522 * @return array
9523 */
9524 public function search_fs_person_type_list( $data ) {
9525 $options = [
9526 [
9527 'label' => 'Customer',
9528 'value' => 'customer',
9529 ],
9530 [
9531 'label' => 'Agent',
9532 'value' => 'agent',
9533 ],
9534 ];
9535
9536 return [
9537 'options' => $options,
9538 'hasMore' => false,
9539 ];
9540 }
9541
9542 /**
9543 * Get last data for trigger.
9544 *
9545 * @param array $data data.
9546 * @return array
9547 */
9548 public function search_fluent_support_triggers_last_data( $data ) {
9549 global $wpdb;
9550 $context = [];
9551 $context['response_type'] = 'sample';
9552
9553 if ( ! class_exists( '\FluentSupport\App\Models\Ticket' ) ) {
9554 return [];
9555 }
9556
9557 $ticket_data = [
9558 'id' => '1',
9559 'customer_id' => '2',
9560 'agent_id' => '3',
9561 'product_id' => '5',
9562 'product_source' => 'local',
9563 'privacy' => 'private',
9564 'priority' => 'normal',
9565 'client_priority' => 'medium',
9566 'status' => 'active',
9567 'title' => 'Sample Ticket Title',
9568 'slug' => 'sample-ticket-title',
9569 'hash' => 'f8a8cfb946',
9570 'content_hash' => 'd65500d62621be8b493c22b1d888052c',
9571 'content' => '<p>Sample content.</p>',
9572 'last_customer_response' => '2023-04-27 07:30:46',
9573 'waiting_since' => '2023-04-27 07:30:46',
9574 'response_count' => '2',
9575 'total_close_time' => '7042',
9576 'resolved at' => '2023-04-27 09:28:08',
9577 'closed_by' => '1',
9578 'created_at' => '2023-04-27 07:30:46',
9579 'updated_at' => '2023-04-27 10:28:08',
9580 'mailbox_id' => '1',
9581 'mailbox_name' => 'SureTriggers',
9582 'mailbox_slug' => 'suretriggers',
9583 'mailbox_box_type' => 'web',
9584 'mailbox email' => 'john_doe@sample.com',
9585 'mailbox_settings_admin_email_address' => 'john_doe@sample.com',
9586 'mailbox_created_by' => '1',
9587 'mailbox_is_default' => 'yes',
9588 'mailbox_created_at' => '2023-04-26 06:29:01',
9589 'mailbox_updated_at' => '2023-04-26 06:29:01',
9590 ];
9591
9592 $customer_data = [
9593 'id' => '1',
9594 'first_name' => 'John',
9595 'last_name' => 'Doe',
9596 'email' => 'john_doe@sample.com',
9597 'person_type' => 'agent',
9598 'status' => 'active',
9599 'hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
9600 'user_id' => '1',
9601 'created_at' => '2023-04-27 07:30:46',
9602 'updated_at' => '2023-04-27 10:28:08',
9603 'full_name' => 'John Doe',
9604 'photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
9605 ];
9606
9607 $reply_data = [
9608 'ticket_id' => '1',
9609 'conversation_type' => 'response',
9610 'content' => '<p>Sample content.</p>',
9611 'source' => 'web',
9612 'content_hash' => '2cc0e35d8fb92a0675d67999b073b3a4',
9613 'created_at' => '2023-04-27 07:30:46',
9614 'updated_at' => '2023-04-27 10:28:08',
9615 'id' => '1',
9616 'person_id' => '2',
9617 'person_first_name' => 'John',
9618 'person_last_name' => 'Doe',
9619 'person_email' => 'john_doe@sample.com',
9620 'person_person_type' => 'agent',
9621 'person_status' => 'active',
9622 'person_hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
9623 'person_user_id' => '1',
9624 'person_created_at' => '2023-04-27 07:30:46',
9625 'person_updated_at' => '2023-04-27 10:28:08',
9626 'person_full_name' => 'John Doe',
9627 'person_photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
9628 ];
9629
9630 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
9631 $mailbox = isset( $data['mailbox_id'] ) ? $data['mailbox_id'] : '';
9632 $product_id = isset( $data['filter']['ticket_product_id']['value'] ) ? $data['filter']['ticket_product_id']['value'] : '';
9633 $person_id = isset( $data['filter']['person_id']['value'] ) ? (string) $data['filter']['person_id']['value'] : '';
9634
9635 if ( in_array( $term, [ 'response_added_by_agent', 'response_added_by_customer', 'ticket_replied_product_agent', 'ticket_replied_product_customer' ], true ) ) {
9636 if ( 'response_added_by_agent' == $term ) {
9637 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
9638 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
9639 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE id = %d ORDER BY id DESC LIMIT 1", $replies[0]->ticket_id ) );
9640 if ( ! empty( $tickets ) ) {
9641 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
9642 $ticket_data = $tickets[0];
9643 $customer_data = $customers[0];
9644 $reply_data = $replies[0];
9645 $context['response_type'] = 'live';
9646 }
9647 } elseif ( 'response_added_by_customer' == $term ) {
9648 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
9649 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
9650 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE id = %d ORDER BY id DESC LIMIT 1", $replies[0]->ticket_id ) );
9651 if ( ! empty( $tickets ) ) {
9652 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
9653 $ticket_data = $tickets[0];
9654 $customer_data = $customers[0];
9655 $reply_data = $replies[0];
9656 $context['response_type'] = 'live';
9657 }
9658 } elseif ( 'ticket_replied_product_agent' == $term ) {
9659 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
9660 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
9661 if ( -1 == $product_id ) {
9662 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
9663 $products = array_map(
9664 function( $product ) {
9665 return $product['id'];
9666 },
9667 $products
9668 );
9669 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id IN (%s) ORDER BY last_customer_response DESC LIMIT 1", implode( ',', $products ) ) );
9670 } else {
9671 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id = %d ORDER BY last_agent_response DESC LIMIT 1", $product_id ) );
9672 }
9673 if ( ! empty( $tickets ) ) {
9674 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
9675 $ticket_data = $tickets[0];
9676 $customer_data = $customers[0];
9677 $reply_data = $replies[0];
9678 $context['response_type'] = 'live';
9679 }
9680 } elseif ( 'ticket_replied_product_customer' == $term ) {
9681 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
9682 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
9683 if ( -1 == $product_id ) {
9684 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
9685 $products = array_map(
9686 function( $product ) {
9687 return $product['id'];
9688 },
9689 $products
9690 );
9691 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id IN (%s) ORDER BY last_customer_response DESC LIMIT 1", implode( ',', $products ) ) );
9692 } else {
9693 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id = %d ORDER BY last_customer_response DESC LIMIT 1", $product_id ) );
9694 }
9695 if ( ! empty( $tickets ) ) {
9696 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
9697 $ticket_data = $tickets[0];
9698 $customer_data = $customers[0];
9699 $reply_data = $replies[0];
9700 $context['response_type'] = 'live';
9701 }
9702 }
9703 $context['pluggable_data'] = array_merge(
9704 [
9705 'reply' => $reply_data,
9706 'ticket' => $ticket_data,
9707 'customer' => $customer_data,
9708 ]
9709 );
9710 if ( 'ticket_replied_product_customer' == $term || 'ticket_replied_product_agent' == $term ) {
9711 $context['pluggable_data']['ticket_product_id'] = $product_id;
9712 }
9713 if ( is_object( $ticket_data ) ) {
9714 $ticket_data = get_object_vars( $ticket_data );
9715 }
9716 $ticket = \FluentSupport\App\Models\Ticket::find( $ticket_data['id'] );
9717 if ( is_object( $ticket ) && method_exists( $ticket, 'customData' ) ) {
9718 $context['pluggable_data']['custom_fields'] = $ticket->customData();
9719 }
9720 $context['pluggable_data']['ticket_link'] = admin_url( "admin.php?page=fluent-support#/tickets/{$ticket_data['id']}/view" );
9721 } else {
9722 if ( 'ticket_created' == $term ) {
9723 if ( -1 == $mailbox ) {
9724 $tickets = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE status = 'new' ORDER BY id DESC LIMIT 1" );
9725 } else {
9726 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE status = 'new' AND mailbox_id = %d ORDER BY id DESC LIMIT 1", $mailbox ) );
9727 }
9728 if ( ! empty( $tickets ) ) {
9729 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d", $tickets[0]->customer_id ) );
9730 $ticket_data = $tickets[0];
9731 $customer_data = $customers[0];
9732 }
9733 } elseif ( 'ticket_closed_by_customer' == $term ) {
9734 $customers_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
9735 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE status = 'closed' AND closed_by = %s ORDER BY id DESC LIMIT 1", $customers_type[0] ) );
9736 if ( ! empty( $tickets ) ) {
9737 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
9738 $ticket_data = $tickets[0];
9739 $customer_data = $customers[0];
9740 }
9741 } elseif ( 'ticket_closed_by_agent' == $term ) {
9742 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
9743 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE status = 'closed' AND closed_by = %s ORDER BY id DESC LIMIT 1", $agents_type[0] ) );
9744 if ( ! empty( $tickets ) ) {
9745 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
9746 $ticket_data = $tickets[0];
9747 $customer_data = $customers[0];
9748 }
9749 } elseif ( 'ticket_created_product' == $term ) {
9750 $agents_type = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_persons" );
9751 if ( -1 == $product_id ) {
9752 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
9753 $products = array_map(
9754 function( $product ) {
9755 return $product['id'];
9756 },
9757 $products
9758 );
9759 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id IN (%s) ORDER BY id DESC LIMIT 1", implode( ',', $products ) ) );
9760 } else {
9761 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id = %d ORDER BY id DESC LIMIT 1", $product_id ) );
9762 }
9763 if ( ! empty( $tickets ) ) {
9764 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
9765 $ticket_data = $tickets[0];
9766 $customer_data = $customers[0];
9767 }
9768 } elseif ( 'ticket_closed_product' == $term ) {
9769 $agents_type = $wpdb->get_results( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = %s", $person_id ), ARRAY_A );
9770 $agents = array_map(
9771 function( $agent ) {
9772 return $agent['id'];
9773 },
9774 $agents_type
9775 );
9776 if ( -1 == $product_id ) {
9777 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
9778 $products = array_map(
9779 function( $product ) {
9780 return $product['id'];
9781 },
9782 $products
9783 );
9784 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id IN (%s) AND status = 'closed' AND closed_by IN (%d) ORDER BY id DESC LIMIT 1", implode( ',', $products ), implode( ',', $agents ) ) );
9785 } else {
9786 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id = %d AND status = 'closed' AND closed_by IN (%d) ORDER BY id DESC LIMIT 1", $product_id, implode( ',', $agents ) ) );
9787 }
9788 if ( ! empty( $tickets ) ) {
9789 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
9790 $ticket_data = $tickets[0];
9791 $customer_data = $customers[0];
9792 }
9793 }
9794 $context['pluggable_data'] = array_merge(
9795 [
9796 'ticket' => $ticket_data,
9797 'customer' => $customer_data,
9798 ]
9799 );
9800 if ( 'ticket_created' == $term ) {
9801 $context['pluggable_data']['mailbox_id'] = $mailbox;
9802 } elseif ( 'ticket_created_product' == $term || 'ticket_replied_product_customer' == $term || 'ticket_replied_product_agent' == $term ) {
9803 $context['pluggable_data']['ticket_product_id'] = $product_id;
9804 } elseif ( 'ticket_closed_product' == $term ) {
9805 $context['pluggable_data']['ticket_product_id'] = $product_id;
9806 $context['pluggable_data']['person_id'] = $person_id;
9807 }
9808 if ( is_object( $ticket_data ) ) {
9809 $ticket_data = get_object_vars( $ticket_data );
9810 }
9811 $ticket = \FluentSupport\App\Models\Ticket::find( $ticket_data['id'] );
9812 if ( is_object( $ticket ) && method_exists( $ticket, 'customData' ) ) {
9813 $context['pluggable_data']['custom_fields'] = $ticket->customData();
9814 }
9815 $context['pluggable_data']['ticket_link'] = admin_url( "admin.php?page=fluent-support#/tickets/{$ticket_data['id']}/view" );
9816 $context['response_type'] = 'live';
9817 }
9818
9819 return $context;
9820 }
9821
9822 /**
9823 * Prepare Ultimate Member user_roles.
9824 *
9825 * @param array $data data.
9826 *
9827 * @return array
9828 */
9829 public function search_um_user_roles( $data ) {
9830 if ( function_exists( 'get_editable_roles' ) ) {
9831 $roles = get_editable_roles();
9832 } else {
9833 $roles = wp_roles()->roles;
9834 $roles = apply_filters( 'editable_roles', $roles );
9835 }
9836
9837 $options = [];
9838 foreach ( $roles as $role => $details ) {
9839
9840 $options[] = [
9841 'label' => $details['name'],
9842 'value' => $role,
9843 ];
9844
9845 }
9846
9847 return [
9848 'options' => $options,
9849 'hasMore' => false,
9850 ];
9851 }
9852
9853 /**
9854 * Prepare Ultimate Member forms_list.
9855 *
9856 * @param array $data data.
9857 *
9858 * @return array
9859 */
9860 public function search_um_forms_list( $data ) {
9861
9862 $page = $data['page'];
9863 $limit = Utilities::get_search_page_limit();
9864 $offset = $limit * ( $page - 1 );
9865
9866 $args = [
9867 'posts_per_page' => $limit,
9868 'offset' => $offset,
9869 'orderby' => 'title',
9870 'order' => 'ASC',
9871 'post_type' => 'um_form',
9872 'post_status' => 'publish',
9873 'fields' => 'ids',
9874 ];
9875 if ( 'register' == $data['filter']['type'] ) {
9876 $args['meta_query'] = [
9877 [
9878 'key' => '_um_mode',
9879 'value' => 'register',
9880 'compare' => 'LIKE',
9881 ],
9882 ];
9883 } elseif ( 'login' == $data['filter']['type'] ) {
9884 $args['meta_query'] = [
9885 [
9886 'key' => '_um_mode',
9887 'value' => 'login',
9888 'compare' => 'LIKE',
9889 ],
9890 ];
9891 }
9892
9893 $forms_list = get_posts( $args );
9894
9895 $forms_list_count = wp_count_posts( 'um_form' )->publish;
9896
9897 $options = [];
9898 if ( ! empty( $forms_list ) ) {
9899 foreach ( $forms_list as $form ) {
9900 $title = html_entity_decode( get_the_title( $form ), ENT_QUOTES, 'UTF-8' );
9901 $options[] = [
9902 'label' => $title,
9903 'value' => $form,
9904 ];
9905 }
9906 }
9907
9908 return [
9909 'options' => $options,
9910 'hasMore' => $forms_list_count > $limit && $forms_list_count > $offset,
9911 ];
9912 }
9913
9914 /**
9915 * Get last data for Ultimate Member Login trigger.
9916 *
9917 * @param array $data data.
9918 * @return mixed
9919 */
9920 public function search_ultimate_member_user_logsin( $data ) {
9921 $context = [];
9922 $args = [
9923 'orderby' => 'meta_value',
9924 'meta_key' => '_um_last_login',
9925 'order' => 'DESC',
9926 'number' => 1,
9927 ];
9928 $users = get_users( $args );
9929
9930 if ( ! empty( $users ) ) {
9931 $user = $users[0];
9932 $pluggable_data = WordPress::get_user_context( $user->ID );
9933 $context['pluggable_data'] = $pluggable_data;
9934 $context['response_type'] = 'live';
9935 } else {
9936 $role = 'subscriber';
9937 $context['pluggable_data'] = [
9938 'wp_user_id' => 1,
9939 'user_login' => 'test',
9940 'display_name' => 'Test User',
9941 'user_firstname' => 'Test',
9942 'user_lastname' => 'User',
9943 'user_email' => 'testuser@gmail.com',
9944 'user_role' => [ $role ],
9945 ];
9946 $context['response_type'] = 'sample';
9947 }
9948 return $context;
9949 }
9950
9951 /**
9952 * Get last data for Ultimate Member Register trigger.
9953 *
9954 * @param array $data data.
9955 * @return mixed
9956 */
9957 public function search_ultimate_member_user_registers( $data ) {
9958 $context = [];
9959 $args = [
9960 'orderby' => 'meta_value',
9961 'meta_key' => 'um_user_profile_url_slug_user_login',
9962 'order' => 'DESC',
9963 'number' => 1,
9964 ];
9965 $users = get_users( $args );
9966
9967 if ( ! empty( $users ) ) {
9968 $user = $users[0];
9969 $pluggable_data = WordPress::get_user_context( $user->ID );
9970 $context['pluggable_data'] = $pluggable_data;
9971 $context['response_type'] = 'live';
9972 } else {
9973 $role = 'subscriber';
9974 $context['pluggable_data'] = [
9975 'wp_user_id' => 1,
9976 'user_login' => 'test',
9977 'display_name' => 'Test User',
9978 'user_firstname' => 'Test',
9979 'user_lastname' => 'User',
9980 'user_email' => 'testuser@gmail.com',
9981 'user_role' => [ $role ],
9982 ];
9983 $context['response_type'] = 'sample';
9984 }
9985 return $context;
9986 }
9987
9988 /**
9989 * Get last data for Ultimate Member Register trigger.
9990 *
9991 * @param array $data data.
9992 * @return mixed
9993 */
9994 public function search_ultimate_member_user_inactive( $data ) {
9995 $context = [];
9996 $args = [
9997 'orderby' => 'user_id',
9998 'meta_key' => 'account_status',
9999 'meta_value' => 'inactive',
10000 'order' => 'ASC',
10001 'number' => 1,
10002 ];
10003 $users = get_users( $args );
10004
10005 if ( ! empty( $users ) ) {
10006 $user = $users[0];
10007 $pluggable_data = [];
10008 $pluggable_data[] = WordPress::get_user_context( $user->ID );
10009 $pluggable_data['user_account_status'] = 'inactive';
10010 $context['pluggable_data'] = $pluggable_data;
10011 $context['response_type'] = 'live';
10012 } else {
10013 $role = 'subscriber';
10014 $context['pluggable_data'] = [
10015 'wp_user_id' => 1,
10016 'user_login' => 'test',
10017 'display_name' => 'Test User',
10018 'user_firstname' => 'Test',
10019 'user_lastname' => 'User',
10020 'user_email' => 'testuser@gmail.com',
10021 'user_role' => [ $role ],
10022 'user_account_status' => 'inactive',
10023 ];
10024 $context['response_type'] = 'sample';
10025 }
10026 return $context;
10027 }
10028
10029 /**
10030 * Get last data for Ultimate Member Change Role trigger.
10031 *
10032 * @param array $data data.
10033 * @return mixed
10034 */
10035 public function search_ultimate_member_user_role_change( $data ) {
10036 $context = [];
10037
10038 $role = $data['filter']['role']['value'];
10039
10040 $args = [
10041 'number' => 1,
10042 'role' => $role,
10043 ];
10044 $users = get_users( $args );
10045 shuffle( $users );
10046 if ( ! empty( $users ) ) {
10047 $user = $users[0];
10048 $pluggable_data = WordPress::get_user_context( $user->ID );
10049 $context['pluggable_data'] = $pluggable_data;
10050 $context['response_type'] = 'live';
10051 } else {
10052 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
10053 $context['pluggable_data'] = [
10054 'wp_user_id' => 1,
10055 'user_login' => 'test',
10056 'display_name' => 'Test User',
10057 'user_firstname' => 'Test',
10058 'user_lastname' => 'User',
10059 'user_email' => 'testuser@gmail.com',
10060 'user_role' => [ $role ],
10061 'user_account_status' => 'inactive',
10062 ];
10063 $context['response_type'] = 'sample';
10064 }
10065 return $context;
10066 }
10067
10068 /**
10069 * Get JetEngine WP Posttypes.
10070 *
10071 * @param array $data data.
10072 *
10073 * @return array
10074 */
10075 public function search_je_posttype_list( $data ) {
10076
10077 $post_types = get_post_types( [ 'public' => true ], 'object' );
10078 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
10079 if ( isset( $post_types['attachment'] ) ) {
10080 unset( $post_types['attachment'] );
10081 }
10082
10083 $options = [];
10084 foreach ( $post_types as $post_type => $details ) {
10085 $options[] = [
10086 'label' => $details->label,
10087 'value' => $post_type,
10088 ];
10089 }
10090
10091 return [
10092 'options' => $options,
10093 'hasMore' => false,
10094 ];
10095 }
10096
10097 /**
10098 * Get JetEngine WP fields.
10099 *
10100 * @param array $data data.
10101 *
10102 * @return array
10103 */
10104 public function search_je_field_list( $data ) {
10105
10106 $post_type = $data['dynamic'];
10107
10108 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
10109
10110 $post_fields = array_filter(
10111 $metaboxes,
10112 function( $metabox ) {
10113 /**
10114 *
10115 * Ignore line
10116 *
10117 * @phpstan-ignore-next-line
10118 */
10119 return 'post' === $metabox['args']['object_type'];
10120 }
10121 );
10122
10123 $post_fields_count = count( $post_fields );
10124
10125 $options = [];
10126 if ( ! empty( $post_fields ) ) {
10127 if ( is_array( $post_fields ) ) {
10128 foreach ( $post_fields as $post_field ) {
10129 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
10130 foreach ( $post_field['meta_fields'] as $meta_field ) {
10131 $options[] = [
10132 'label' => $meta_field['title'],
10133 'value' => $meta_field['name'],
10134 ];
10135 }
10136 }
10137 }
10138 }
10139 }
10140 return [
10141 'options' => $options,
10142 'hasMore' => false,
10143 ];
10144 }
10145
10146 /**
10147 * Search Last Updated Field Data.
10148 *
10149 * @param array $data data.
10150 * @return array
10151 */
10152 public function search_jet_engine_field_data( $data ) {
10153 global $wpdb;
10154
10155 $context = [];
10156
10157 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
10158
10159 $post_type = $data['filter']['wp_post_type']['value'];
10160
10161 if ( -1 === $field ) {
10162 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
10163
10164 $post_fields = array_filter(
10165 $metaboxes,
10166 function( $metabox ) {
10167 /**
10168 *
10169 * Ignore line
10170 *
10171 * @phpstan-ignore-next-line
10172 */
10173 return 'post' === $metabox['args']['object_type'];
10174 }
10175 );
10176
10177 $options = [];
10178 if ( ! empty( $post_fields ) ) {
10179 if ( is_array( $post_fields ) ) {
10180 foreach ( $post_fields as $post_field ) {
10181 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
10182 foreach ( $post_field['meta_fields'] as $meta_field ) {
10183 $options[] = $meta_field['name'];
10184 }
10185 }
10186 }
10187 }
10188 }
10189 $random_key = array_rand( $options );
10190 $random_value = $options[ $random_key ];
10191 $string = '%' . $random_value . '%';
10192 } else {
10193 $string = '%' . $data['filter']['field_id']['value'] . '%';
10194 }
10195
10196 $result = $wpdb->get_results(
10197 $wpdb->prepare(
10198 'SELECT post_id FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key LIKE %s',
10199 [ $string ]
10200 ),
10201 ARRAY_A
10202 );
10203
10204 $ids = [];
10205
10206 if ( ! empty( $result ) ) {
10207 foreach ( $result as $val ) {
10208 $ids[] = $val['post_id'];
10209 }
10210 }
10211
10212 $lastupdated_args = [
10213 'post_type' => $post_type,
10214 'orderby' => 'modified',
10215 'post__in' => $ids,
10216 'posts_per_page' => 1,
10217 ];
10218 $lastupdated_loop = get_posts( $lastupdated_args );
10219
10220 $response = [];
10221 if ( ! empty( $result ) ) {
10222 $context['post'] = $lastupdated_loop[0];
10223
10224 $meta_value = get_post_meta( $lastupdated_loop[0]->ID, sprintf( '%s', $data['filter']['field_id']['value'] ), true );
10225 $meta_key = sprintf( '%s', $data['filter']['field_id']['value'] );
10226
10227 $context[ $meta_key ] = $meta_value;
10228
10229 $response['pluggable_data'] = $context;
10230 $response['response_type'] = 'live';
10231 } else {
10232 $response = json_decode( '{"response_type":"sample","pluggable_data":{"post":{"ID":198,"post_author":"1","post_date":"2023-02-08 13:31:13","post_date_gmt":"2023-02-08 13:31:13","post_content":"New Category1 - content","post_title":"jennjennn - Post - jenn","post_excerpt":"","post_status":"publish","comment_status":"open","ping_status":"open","post_password":"","post_name":"jennjennn-post-jenn","to_ping":"","pinged":"","post_modified":"2023-04-10 06:23:40","post_modified_gmt":"2023-04-10 06:23:40","post_content_filtered":"","post_parent":0,"guid":"https:\/\/suretriggerswp.local\/jennjennn-post-jenn\/","menu_order":0,"post_type":"post","post_mime_type":"","comment_count":"0","filter":"raw"},"enter-post-extra-content-title":"dummy"}}', true );
10233 }
10234
10235 return $response;
10236
10237 }
10238
10239 /**
10240 * Get Formidable Forms.
10241 *
10242 * @param array $data data.
10243 *
10244 * @return array|void
10245 */
10246 public function search_formidable_form_list( $data ) {
10247 if ( ! class_exists( 'FrmForm' ) ) {
10248 return;
10249 }
10250
10251 $page = $data['page'];
10252 $term = $data['search_term'];
10253 $limit = Utilities::get_search_page_limit();
10254 $offset = $limit * ( $page - 1 );
10255 $per_page = 10;
10256
10257 $query = [
10258 [
10259 'or' => 1,
10260 'name LIKE' => $term,
10261 'parent_form_id' => null,
10262 ],
10263 ];
10264 $query['is_template'] = 0;
10265 $query['status !'] = 'trash';
10266 $forms_list = FrmForm::getAll( $query, '', $offset . ',' . $per_page );
10267 $form_count = FrmForm::getAll( $query );
10268 $form_count = count( $form_count );
10269 $options = [];
10270
10271 if ( ! empty( $forms_list ) ) {
10272 if ( is_array( $forms_list ) ) {
10273 foreach ( $forms_list as $form ) {
10274 $options[] = [
10275 'label' => $form->name,
10276 'value' => $form->id,
10277 ];
10278 }
10279 }
10280 }
10281
10282 return [
10283 'options' => $options,
10284 'hasMore' => $form_count > $limit && $form_count > $offset,
10285 ];
10286 }
10287
10288 /**
10289 * Get JetFormBuilder Form List.
10290 *
10291 * @param array $data data.
10292 *
10293 * @return array|void
10294 */
10295 public function search_jetform_list( $data ) {
10296 if ( ! class_exists( '\Jet_Form_Builder\Classes\Tools' ) ) {
10297 return;
10298 }
10299
10300 $forms = \Jet_Form_Builder\Classes\Tools::get_forms_list_for_js();
10301
10302 $options = [];
10303 foreach ( $forms as $form ) {
10304
10305 if ( ! empty( $form['value'] ) ) {
10306 $options[] = [
10307 'label' => esc_html( $form['label'] ),
10308 'value' => esc_attr( $form['value'] ),
10309 ];
10310 }
10311 }
10312
10313 return [
10314 'options' => $options,
10315 'hasMore' => false,
10316 ];
10317 }
10318
10319 /**
10320 * Get Forminator Form List.
10321 *
10322 * @param array $data data.
10323 *
10324 * @return array|void
10325 */
10326 public function search_forminator_form_list( $data ) {
10327 if ( ! class_exists( 'Forminator_API' ) ) {
10328 return;
10329 }
10330
10331 $forms = Forminator_API::get_forms( null, 1, 10 );
10332
10333 $options = [];
10334 foreach ( $forms as $form ) {
10335 $options[] = [
10336 'label' => isset( $form->settings ) && isset( $form->settings['form_name'] ) ? $form->settings['form_name'] : $form->name,
10337 'value' => $form->id,
10338 ];
10339 }
10340
10341 return [
10342 'options' => $options,
10343 'hasMore' => false,
10344 ];
10345 }
10346
10347
10348 /**
10349 * Get BbPress topics list.
10350 *
10351 * @param array $data data.
10352 *
10353 * @return array|void
10354 */
10355 public function search_bbp_topic_list( $data ) {
10356 $page = $data['page'];
10357 $forum_id = $data['dynamic'];
10358 $limit = Utilities::get_search_page_limit();
10359 $offset = $limit * ( $page - 1 );
10360 $args = [
10361 'post_type' => 'topic',
10362 'offset' => $offset,
10363 'meta_query' => [
10364 [
10365 'key' => '_bbp_forum_id',
10366 'value' => $forum_id,
10367 'compare' => '==',
10368 ],
10369 ],
10370 ];
10371
10372 $topics = get_posts( $args );
10373 $topics_count = wp_count_posts( 'topic' )->publish;
10374
10375 $options = [];
10376 if ( ! empty( $topics ) ) {
10377 if ( is_array( $topics ) ) {
10378 foreach ( $topics as $topic ) {
10379 $options[] = [
10380 'label' => $topic->post_title,
10381 'value' => $topic->ID,
10382 ];
10383 }
10384 }
10385 }
10386 return [
10387 'options' => $options,
10388 'hasMore' => $topics_count > $limit && $topics_count > $offset,
10389 ];
10390 }
10391
10392
10393 /**
10394 * Search Last Updated Field Data.
10395 *
10396 * @param array $data data.
10397 * @return array
10398 */
10399 public function search_bbp_last_data( $data ) {
10400 global $wpdb;
10401
10402 $post_type = $data['post_type'];
10403 $trigger = $data['search_term'];
10404 $context = [];
10405
10406 if ( 'topic' === $post_type ) {
10407 $post_id = $data['filter']['forum']['value'];
10408 if ( -1 === $post_id ) {
10409 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts as posts JOIN {$wpdb->prefix}postmeta as postmeta ON posts.ID = postmeta.post_id WHERE posts.post_type = 'topic' AND postmeta.meta_key= '_bbp_forum_id' ORDER BY posts.ID DESC LIMIT 1" );
10410
10411 } else {
10412 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts as posts JOIN {$wpdb->prefix}postmeta as postmeta ON posts.ID=postmeta.post_id WHERE posts.post_type ='topic' AND postmeta.meta_key= '_bbp_forum_id' AND postmeta.meta_value=%s ORDER BY posts.ID DESC LIMIT 1", $post_id ) );
10413
10414 }
10415 } elseif ( 'reply' === $post_type ) {
10416 $post_id = $data['filter']['topic']['value'];
10417
10418 if ( -1 === $post_id ) {
10419 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts as posts JOIN {$wpdb->prefix}postmeta as postmeta ON posts.ID = postmeta.post_id WHERE posts.post_type = 'reply' AND postmeta.meta_key= '_bbp_topic_id' ORDER BY posts.ID DESC LIMIT 1" );
10420
10421 } else {
10422 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts as posts JOIN {$wpdb->prefix}postmeta as postmeta ON posts.ID=postmeta.post_id WHERE posts.post_type ='reply' AND postmeta.meta_key= '_bbp_topic_id' AND postmeta.meta_value=%s ORDER BY posts.ID DESC LIMIT 1", $post_id ) );
10423 }
10424 }
10425
10426
10427 $response = [];
10428 if ( ! empty( $result ) ) {
10429 if ( 'bbpress_topic_created' === $trigger ) {
10430 $topic_id = $result[0]->post_id;
10431 $forum_id = $result[0]->meta_value;
10432 $topic = get_the_title( $topic_id );
10433 $topic_link = get_the_permalink( $topic_id );
10434 $topic_description = get_the_content( $topic_id );
10435 $topic_status = get_post_status( $topic_id );
10436
10437 $forum = get_the_title( $forum_id );
10438 $forum_link = get_the_permalink( $forum_id );
10439 $forum_description = get_the_content( $forum_id );
10440 $forum_status = get_post_status( $forum_id );
10441
10442 $forum = [
10443 'forum' => $forum_id,
10444 'forum_title' => $forum,
10445 'forum_link' => $forum_link,
10446 'forum_description' => $forum_description,
10447 'forum_status' => $forum_status,
10448 ];
10449
10450 $topic = [
10451 'topic_title' => $topic,
10452 'topic_link' => $topic_link,
10453 'topic_description' => $topic_description,
10454 'topic_status' => $topic_status,
10455 ];
10456
10457 $user_id = $result[0]->post_author;
10458 $context = array_merge(
10459 WordPress::get_user_context( $user_id ),
10460 $forum,
10461 $topic
10462 );
10463
10464 $response['pluggable_data'] = $context;
10465 $response['response_type'] = 'live';
10466 } else {
10467 $reply_id = $result[0]->post_id;
10468 $topic_id = $result[0]->meta_value;
10469 $forum_id = get_post_meta( $topic_id, '_bbp_forum_id', true );
10470 $forum_id = intval( '"' . $forum_id . '"' );
10471 $reply = get_the_title( $reply_id );
10472 $reply_link = get_the_permalink( $reply_id );
10473 $reply_description = get_the_content( $reply_id );
10474 $reply_status = get_post_status( $reply_id );
10475
10476
10477 $topic = get_the_title( $topic_id );
10478 $topic_link = get_the_permalink( $topic_id );
10479 $topic_description = get_the_content( $topic_id );
10480 $topic_status = get_post_status( $topic_id );
10481
10482 $forum = get_the_title( $forum_id );
10483 $forum_link = get_the_permalink( $forum_id );
10484 $forum_description = get_the_content( null, false, $forum_id );
10485 $forum_status = get_post_status( $forum_id );
10486
10487 $forum = [
10488 'forum' => $forum_id,
10489 'forum_title' => $forum,
10490 'forum_link' => $forum_link,
10491 'forum_description' => $forum_description,
10492 'forum_status' => $forum_status,
10493 ];
10494
10495 $topic = [
10496 'topic_title' => $topic,
10497 'topic_link' => $topic_link,
10498 'topic_description' => $topic_description,
10499 'topic_status' => $topic_status,
10500 ];
10501
10502 $reply = [
10503 'reply_title' => $reply,
10504 'reply_link' => $reply_link,
10505 'reply_description' => $reply_description,
10506 'reply_status' => $reply_status,
10507 ];
10508 $user_id = $result[0]->post_author;
10509 $context = array_merge(
10510 WordPress::get_user_context( $user_id ),
10511 $forum,
10512 $topic,
10513 $reply
10514 );
10515
10516 $response['pluggable_data'] = $context;
10517 $response['response_type'] = 'live';
10518 }
10519 }
10520
10521 return $response;
10522 }
10523
10524 /**
10525 * Search Last Updated Field Data.
10526 *
10527 * @param array $data data.
10528 * @return array|void
10529 */
10530 public function search_happyform_list( $data ) {
10531 if ( ! function_exists( 'happyforms_get_form_controller' ) ) {
10532 return;
10533 }
10534
10535 $form_controller = happyforms_get_form_controller();
10536
10537 $forms = $form_controller->do_get();
10538 $options = [];
10539 if ( ! empty( $forms ) ) {
10540 foreach ( $forms as $form ) {
10541 $options[] = [
10542 'label' => $form['post_title'],
10543 'value' => $form['ID'],
10544 ];
10545 }
10546 }
10547
10548 return [
10549 'options' => $options,
10550 'hasMore' => false,
10551 ];
10552 }
10553
10554 /**
10555 * Get Memberpress Course List.
10556 *
10557 * @param array $data data.
10558 *
10559 * @return array|void
10560 */
10561 public function search_mpc_lessons_list( $data ) {
10562 if ( ! class_exists( '\memberpress\courses\models\Lesson' ) ) {
10563 return;
10564 }
10565 global $wpdb;
10566 $options = [];
10567 $course_id = $data['dynamic'];
10568 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mpcs_sections WHERE course_id =%s", $course_id ) );
10569 $sections = [];
10570 foreach ( $result as $rec ) {
10571 $sections[] = [
10572 'id' => $rec->id,
10573 'title' => $rec->title,
10574 ];
10575 }
10576 if ( is_array( $sections ) && count( $sections ) > 0 ) {
10577 foreach ( $sections as $section ) {
10578 $post_types_string = \memberpress\courses\models\Lesson::lesson_cpts();
10579 $post_types_string = implode( "','", $post_types_string );
10580
10581 $query = $wpdb->prepare(
10582 "SELECT * FROM {$wpdb->posts} AS p
10583 JOIN {$wpdb->postmeta} AS pm
10584 ON p.ID = pm.post_id
10585 AND pm.meta_key = %s
10586 AND pm.meta_value = %s
10587 JOIN {$wpdb->postmeta} AS pm_order
10588 ON p.ID = pm_order.post_id
10589 AND pm_order.meta_key = %s
10590 WHERE p.post_type in ( %s ) AND p.post_status <> 'trash'
10591 ORDER BY pm_order.meta_value * 1",
10592 models\Lesson::$section_id_str,
10593 $section['id'],
10594 models\Lesson::$lesson_order_str,
10595 stripcslashes( $post_types_string )
10596 );
10597
10598 $db_lessons = $wpdb->get_results( stripcslashes( $query ) ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
10599 foreach ( $db_lessons as $lesson ) {
10600 $options[] = [
10601 'label' => $section['title'] . '->' . $lesson->post_title,
10602 'value' => $lesson->ID,
10603 ];
10604 }
10605 }
10606 }
10607 return [
10608 'options' => $options,
10609 'hasMore' => false,
10610 ];
10611 }
10612
10613 /**
10614 * Get Memberpress Course List.
10615 *
10616 * @param array $data data.
10617 *
10618 * @return array|void
10619 */
10620 public function search_gp_rank_type_list( $data ) {
10621 global $wpdb;
10622
10623 $posts = $wpdb->get_results(
10624 "SELECT ID, post_name, post_title, post_type
10625 FROM $wpdb->posts
10626 WHERE post_type LIKE 'rank-type' AND post_status = 'publish' ORDER BY post_title ASC"
10627 );
10628
10629 $posts_count = count( $posts );
10630
10631 $options = [];
10632 if ( $posts ) {
10633 foreach ( $posts as $post ) {
10634 $options[] = [
10635 'label' => $post->post_title,
10636 'value' => $post->post_name,
10637 ];
10638 }
10639 }
10640
10641 return [
10642 'options' => $options,
10643 'hasMore' => false,
10644 ];
10645 }
10646
10647 /**
10648 * Get MPC last data.
10649 *
10650 * @param array $data data.
10651 *
10652 * @return array
10653 */
10654 public function search_mpc_last_data( $data ) {
10655 global $wpdb;
10656 $trigger = $data['search_term'];
10657 $course_data = [];
10658 $lesson_data = [];
10659 $context = [];
10660
10661 if ( 'mpc_course_completed' === $trigger ) {
10662 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course]']['value'] : '-1' );
10663 if ( $course_id > 0 ) {
10664
10665 $course = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $course_id ) );
10666 } else {
10667 $course = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-course' ORDER BY id DESC LIMIT 1" );
10668 }
10669
10670 if ( ! empty( $course ) ) {
10671 $course_data = [
10672 'course_id' => $course->ID,
10673 'course_title' => get_the_title( $course_id ),
10674 'course_url' => get_permalink( $course_id ),
10675 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
10676 'course_featured_image_url' => get_the_post_thumbnail_url( $course_id ),
10677 ];
10678 }
10679 $user_progress = $wpdb->get_row( $wpdb->prepare( "SELECT user_id FROM {$wpdb->prefix}mpcs_user_progress WHERE course_id=%s", $course_id ) );
10680 if ( ! empty( $user_progress ) ) {
10681 $context['response_type'] = 'live';
10682 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
10683 } else {
10684 $sample_data = '{"pluggable_data":{"wp_user_id":1,"user_login":"suretriggers","display_name":"suretriggers","user_firstname":"suretriggers","user_lastname":"suretriggers","user_email":"hello@suretriggers.io","user_role":["administrator","subscriber","tutor_instructor","bbp_keymaster"],"course_id":617,"course_title":"Course One","course_url":"https:\/\/connector.com\/courses\/course-one\/","course_featured_image_id":"","course_featured_image_url":false}
10685 ,"response_type":"sample"} ';
10686 $context = json_decode( $sample_data, true );
10687 }
10688 } elseif ( 'mpc_lesson_completed' === $trigger ) {
10689 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
10690 $course_id = (int) $data['filter']['course']['value'];
10691 if ( $lesson_id > 0 ) {
10692
10693 $lesson = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $lesson_id ) );
10694 } else {
10695 $lesson = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-lesson' ORDER BY id DESC LIMIT 1" );
10696 }
10697
10698 if ( ! empty( $lesson ) ) {
10699 $lesson_data = [
10700 'lesson_id' => $lesson->ID,
10701 'lesson_title' => get_the_title( $lesson_id ),
10702 'lesson_url' => get_permalink( $lesson_id ),
10703 'lesson_featured_image_id' => get_post_meta( $lesson_id, '_thumbnail_id', true ),
10704 'lesson_featured_image_url' => get_the_post_thumbnail_url( $lesson_id ),
10705 ];
10706
10707 $lesson_section_id = get_post_meta( $lesson->ID, '_mpcs_lesson_section_id', true );
10708
10709 $section = $wpdb->get_row( $wpdb->prepare( "SELECT course_id FROM {$wpdb->prefix}mpcs_sections WHERE ID= %s", $lesson_section_id ) );
10710
10711 $course_data = [
10712 'course_id' => $course_id,
10713 'course_title' => get_the_title( $course_id ),
10714 'course_url' => get_permalink( $course_id ),
10715 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
10716 'course_featured_image_url' => get_the_post_thumbnail_url( $section->course_id ),
10717 ];
10718 }
10719
10720 $user_progress = $wpdb->get_row( $wpdb->prepare( "SELECT user_id FROM {$wpdb->prefix}mpcs_user_progress WHERE lesson_id= %s AND course_id=%s", $lesson_id, $course_id ) );
10721 if ( ! empty( $user_progress ) ) {
10722 $context['response_type'] = 'live';
10723 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
10724 } else {
10725 $sample_data = '{"pluggable_data":{"wp_user_id":1,"user_login":"suretriggers","display_name":"suretriggers","user_firstname":"suretriggers","user_lastname":"dev","user_email":"hello@suretriggers.com","user_role":["administrator","subscriber","tutor_instructor","bbp_keymaster"],"lesson_id":620,"lesson_title":"second section","lesson_url":"https:\/\/connector.com\/courses\/course-one\/lessons\/second-section\/","lesson_featured_image_id":"","lesson_featured_image_url":false,"course_id":617,"course_title":"Course One","course_url":"https:\/\/connector.com\/courses\/course-one\/","course_featured_image_id":"","course_featured_image_url":false},"response_type":"sample"}';
10726 $context = json_decode( $sample_data, true );
10727 }
10728 }
10729
10730
10731 return $context;
10732 }
10733
10734 /** Get GamiPress Rank List.
10735 *
10736 * @param array $data data.
10737 *
10738 * @return array|void
10739 */
10740 public function search_gp_rank_list( $data ) {
10741 global $wpdb;
10742
10743 $page = $data['page'];
10744 $limit = Utilities::get_search_page_limit();
10745 $offset = $limit * ( $page - 1 );
10746
10747 $args = [
10748 'post_type' => $data['dynamic']['rank_type'],
10749 'posts_per_page' => $limit,
10750 'offset' => $offset,
10751 'orderby' => 'title',
10752 'order' => 'ASC',
10753 'post_status' => 'publish',
10754 's' => $data['search_term'],
10755 ];
10756
10757 $rank_type = get_posts( $args );
10758
10759 $count_args = [
10760 'post_type' => $data['dynamic']['rank_type'],
10761 'posts_per_page' => -1,
10762 'post_status' => 'publish',
10763 's' => $data['search_term'],
10764 ];
10765
10766 $rank_posts = get_posts( $count_args );
10767 $rank_type_count = count( $rank_posts );
10768
10769 $options = [];
10770 if ( $rank_type ) {
10771 foreach ( $rank_type as $rank ) {
10772 $options[] = [
10773 'label' => $rank->post_title,
10774 'value' => $rank->ID,
10775 ];
10776 }
10777 }
10778
10779 return [
10780 'options' => $options,
10781 'hasMore' => $rank_type_count > $limit && $rank_type_count > $offset,
10782 ];
10783 }
10784
10785 /**
10786 * Get GamiPress PointType List.
10787 *
10788 * @param array $data data.
10789 *
10790 * @return array|void
10791 */
10792 public function search_gp_point_type_list( $data ) {
10793 global $wpdb;
10794
10795 $page = $data['page'];
10796 $limit = Utilities::get_search_page_limit();
10797 $offset = $limit * ( $page - 1 );
10798
10799 $args = [
10800 'post_type' => 'points-type',
10801 'posts_per_page' => $limit,
10802 'offset' => $offset,
10803 'orderby' => 'title',
10804 'order' => 'ASC',
10805 'post_status' => 'publish',
10806 's' => $data['search_term'],
10807 ];
10808
10809 $point_type = get_posts( $args );
10810
10811 $count_args = [
10812 'post_type' => 'points-type',
10813 'posts_per_page' => -1,
10814 'post_status' => 'publish',
10815 's' => $data['search_term'],
10816 ];
10817
10818 $count_point_type = get_posts( $count_args );
10819 $point_type_count = count( $count_point_type );
10820
10821 $options = [];
10822 if ( $point_type ) {
10823 foreach ( $point_type as $point ) {
10824 $options[] = [
10825 'label' => $point->post_title,
10826 'value' => $point->ID,
10827 ];
10828 }
10829 }
10830
10831 return [
10832 'options' => $options,
10833 'hasMore' => $point_type_count > $limit && $point_type_count > $offset,
10834 ];
10835 }
10836
10837 /**
10838 * Get GamiPress AchievementType List.
10839 *
10840 * @param array $data data.
10841 *
10842 * @return array|void
10843 */
10844 public function search_gp_achivement_type_list( $data ) {
10845 global $wpdb;
10846
10847 $posts = $wpdb->get_results(
10848 "SELECT ID, post_name, post_title, post_type
10849 FROM $wpdb->posts
10850 WHERE post_type LIKE 'achievement-type' AND post_status = 'publish' ORDER BY post_title ASC"
10851 );
10852
10853 $posts_count = count( $posts );
10854
10855 $options = [];
10856 if ( $posts ) {
10857 foreach ( $posts as $post ) {
10858 $options[] = [
10859 'label' => $post->post_title,
10860 'value' => $post->post_name,
10861 ];
10862 }
10863 }
10864
10865 $options[] = [
10866 'label' => 'Points awards',
10867 'value' => 'points-award',
10868 ];
10869 $options[] = [
10870 'label' => 'Step',
10871 'value' => 'step',
10872 ];
10873 $options[] = [
10874 'label' => 'Rank requirement',
10875 'value' => 'rank-requirement',
10876 ];
10877
10878 return [
10879 'options' => $options,
10880 'hasMore' => false,
10881 ];
10882 }
10883
10884 /**
10885 * Get GamiPress Award List.
10886 *
10887 * @param array $data data.
10888 *
10889 * @return array|void
10890 */
10891 public function search_gp_award_list( $data ) {
10892 global $wpdb;
10893
10894 $page = $data['page'];
10895 $limit = Utilities::get_search_page_limit();
10896 $offset = $limit * ( $page - 1 );
10897
10898 $args = [
10899 'post_type' => $data['dynamic']['achivement_type'],
10900 'posts_per_page' => $limit,
10901 'offset' => $offset,
10902 'orderby' => 'title',
10903 'order' => 'ASC',
10904 'post_status' => 'publish',
10905 's' => $data['search_term'],
10906 ];
10907
10908 $award_type = get_posts( $args );
10909 $count_args = [
10910 'post_type' => $data['dynamic']['achivement_type'],
10911 'posts_per_page' => -1,
10912 'post_status' => 'publish',
10913 's' => $data['search_term'],
10914 ];
10915
10916 $count_award_type = get_posts( $count_args );
10917 $award_type_count = count( $count_award_type );
10918 $options = [];
10919 if ( $award_type ) {
10920 foreach ( $award_type as $award ) {
10921 $options[] = [
10922 'label' => $award->post_title,
10923 'value' => $award->ID,
10924 ];
10925 }
10926 }
10927
10928 return [
10929 'options' => $options,
10930 'hasMore' => $award_type_count > $limit && $award_type_count > $offset,
10931 ];
10932 }
10933
10934 /**
10935 * Get Woocommerce Subscription Product List.
10936 *
10937 * @param array $data data.
10938 *
10939 * @return array|void
10940 */
10941 public function search_wc_subscription_product_list( $data ) {
10942 global $wpdb;
10943
10944 $subscriptions = $wpdb->get_results(
10945 $wpdb->prepare(
10946 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
10947 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
10948 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
10949 AND posts.post_type = %s
10950 AND posts.post_status = %s
10951 UNION ALL
10952 SELECT ID, post_title FROM $wpdb->posts
10953 WHERE post_type = %s
10954 AND post_status = %s
10955 ORDER BY post_title",
10956 'product',
10957 'publish',
10958 'shop_subscription',
10959 'publish'
10960 )
10961 );
10962
10963 $options = [];
10964 if ( $subscriptions ) {
10965 foreach ( $subscriptions as $post ) {
10966 $options[] = [
10967 'label' => $post->post_title,
10968 'value' => $post->ID,
10969 ];
10970 }
10971 }
10972
10973 return [
10974 'options' => $options,
10975 'hasMore' => false,
10976 ];
10977 }
10978
10979 /**
10980 * Get Woocommerce Subscriptions Variation list.
10981 *
10982 * @param array $data data.
10983 *
10984 * @return array|void
10985 */
10986 public function search_wc_variable_subscription_list( $data ) {
10987 global $wpdb;
10988
10989 $page = $data['page'];
10990 $limit = Utilities::get_search_page_limit();
10991 $offset = $limit * ( $page - 1 );
10992
10993 if ( ! function_exists( 'wc_get_products' ) ) {
10994 return;
10995 }
10996 $subscription_products = wc_get_products(
10997 [
10998 'type' => [ 'variable-subscription' ],
10999 'posts_per_page' => $limit,
11000 'offset' => $offset,
11001 'orderby' => 'date',
11002 'order' => 'DESC',
11003 ]
11004 );
11005
11006 $subscription_products_count = count( (array) $subscription_products );
11007
11008 $options = [];
11009 if ( $subscription_products ) {
11010 foreach ( (array) $subscription_products as $product ) {
11011 $options[] = [
11012 'label' => $product->get_title(),
11013 'value' => $product->get_id(),
11014 ];
11015 }
11016 }
11017
11018 return [
11019 'options' => $options,
11020 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
11021 ];
11022 }
11023
11024 /**
11025 * Get Woocommerce Variation list.
11026 *
11027 * @param array $data data.
11028 *
11029 * @return array|void
11030 */
11031 public function search_wc_variation_list( $data ) {
11032 global $wpdb;
11033
11034 $page = $data['page'];
11035 $limit = Utilities::get_search_page_limit();
11036 $offset = $limit * ( $page - 1 );
11037
11038 $args = [
11039 'post_type' => 'product_variation',
11040 'post_parent' => $data['dynamic']['variable_subscription'],
11041 'posts_per_page' => $limit,
11042 'offset' => $offset,
11043 'orderby' => 'ID',
11044 'order' => 'ASC',
11045 'post_status' => 'publish',
11046 ];
11047
11048 $variation = get_posts( $args );
11049 $variation_count = count( $variation );
11050
11051 $options = [];
11052 if ( $variation ) {
11053 foreach ( $variation as $product ) {
11054 $options[] = [
11055 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
11056 'value' => $product->ID,
11057 ];
11058 }
11059 }
11060
11061 return [
11062 'options' => $options,
11063 'hasMore' => $variation_count > $limit && $variation_count > $offset,
11064 ];
11065 }
11066
11067 /**
11068 * Get Membership List.
11069 *
11070 * @param array $data data.
11071 *
11072 * @return array
11073 */
11074 public function search_membership_list( $data ) {
11075 global $wpdb;
11076
11077 $levels = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}pmpro_membership_levels ORDER BY id ASC" );
11078 $options = [];
11079 if ( $levels ) {
11080 foreach ( $levels as $level ) {
11081 $options[] = [
11082 'label' => $level->name,
11083 'value' => $level->id,
11084 ];
11085 }
11086 }
11087
11088 return [
11089 'options' => $options,
11090 'hasMore' => false,
11091 ];
11092 }
11093
11094 /**
11095
11096 * Get EventsManager last data.
11097 *
11098 * @param array $data data.
11099 *
11100 * @return array
11101 */
11102 public function search_events_manager_data( $data ) {
11103 global $wpdb;
11104 $trigger = $data['search_term'];
11105 $context = [];
11106
11107 $post_id = (int) ( isset( $data['filter']['post_id']['value'] ) ? $data['filter']['post_id']['value'] : '-1' );
11108 if ( 'em_user_register_in_event' === $trigger ) {
11109 if ( $post_id > 0 ) {
11110 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11111 $all_bookings = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_bookings as b INNER JOIN {$wpdb->prefix}em_events as e ON b.event_id = e.event_id WHERE e.event_status = 1 AND b.booking_status NOT IN (2,3) AND b.event_id = %s AND e.event_end_date >= CURRENT_DATE ORDER BY b.booking_id DESC LIMIT 1", $event_id_id ) );
11112 } else {
11113 $all_bookings = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}em_bookings as b INNER JOIN {$wpdb->prefix}em_events as e ON b.event_id = e.event_id WHERE e.event_status = 1 AND b.booking_status NOT IN (2,3) AND e.event_end_date >= CURRENT_DATE ORDER BY b.booking_id DESC LIMIT 1" );
11114
11115 }
11116
11117 if ( ! empty( $all_bookings ) ) {
11118 $user_id = $all_bookings->person_id;
11119 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
11120 $context['pluggable_data'] = array_merge(
11121 WordPress::get_user_context( $user_id ),
11122 json_decode( wp_json_encode( $all_bookings ), true )
11123 );
11124 if ( ! empty( $location ) ) {
11125 $context['pluggable_data'] = array_merge( $context['pluggable_data'], json_decode( wp_json_encode( $location ), true ) );
11126 }
11127
11128 $context['response_type'] = 'live';
11129 }
11130 } elseif ( 'em_user_unregister_from_event' === $trigger ) {
11131
11132 if ( $post_id > 0 ) {
11133 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11134 $all_bookings = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_bookings as b INNER JOIN {$wpdb->prefix}em_events as e ON b.event_id = e.event_id WHERE e.event_status = 1 AND b.booking_status IN (2,3) AND b.event_id = %s AND e.event_end_date >= CURRENT_DATE ORDER BY b.booking_id DESC LIMIT 1", $event_id_id ) );
11135 } else {
11136 $all_bookings = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}em_bookings as b INNER JOIN {$wpdb->prefix}em_events as e ON b.event_id = e.event_id WHERE e.event_status = 1 AND b.booking_status IN (2,3) AND e.event_end_date >= CURRENT_DATE ORDER BY b.booking_id DESC LIMIT 1" );
11137
11138 }
11139
11140 if ( ! empty( $all_bookings ) ) {
11141 $user_id = $all_bookings->person_id;
11142 $context['pluggable_data'] = array_merge(
11143 WordPress::get_user_context( $user_id ),
11144 json_decode( wp_json_encode( $all_bookings ), true )
11145 );
11146 $context['response_type'] = 'live';
11147 }
11148 } elseif ( 'em_user_booking_approved' === $trigger ) {
11149
11150 if ( $post_id > 0 ) {
11151 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11152 $all_bookings = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_bookings as b INNER JOIN {$wpdb->prefix}em_events as e ON b.event_id = e.event_id WHERE e.event_status = 1 AND b.booking_status=1 AND b.event_id = %s AND e.event_end_date >= CURRENT_DATE ORDER BY b.booking_id DESC LIMIT 1", $event_id_id ) );
11153 } else {
11154 $all_bookings = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}em_bookings as b INNER JOIN {$wpdb->prefix}em_events as e ON b.event_id = e.event_id WHERE e.event_status = 1 AND b.booking_status=1 AND e.event_end_date >= CURRENT_DATE ORDER BY b.booking_id DESC LIMIT 1" );
11155
11156 }
11157
11158 if ( ! empty( $all_bookings ) ) {
11159 $user_id = $all_bookings->person_id;
11160 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
11161 $context['pluggable_data'] = array_merge(
11162 WordPress::get_user_context( $user_id ),
11163 json_decode( wp_json_encode( $all_bookings ), true )
11164 );
11165 if ( ! empty( $location ) ) {
11166 $context['pluggable_data'] = array_merge( $context['pluggable_data'], json_decode( wp_json_encode( $location ), true ) );
11167 }
11168
11169 $context['response_type'] = 'live';
11170
11171 }
11172 } elseif ( 'em_user_registers_event_with_specific_ticket' === $trigger ) {
11173 if ( $post_id > 0 ) {
11174 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11175 $all_bookings = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_bookings as b INNER JOIN {$wpdb->prefix}em_events as e ON b.event_id = e.event_id WHERE e.event_status = 1 AND b.booking_status NOT IN (2,3) AND b.event_id = %s AND e.event_end_date >= CURRENT_DATE ORDER BY b.booking_id DESC LIMIT 1", $event_id_id ) );
11176 }
11177 if ( ! empty( $all_bookings ) ) {
11178 $ticket_id = (int) $data['filter']['ticket_id']['value'];
11179 $all_ticket_bookings = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_tickets_bookings as b INNER JOIN {$wpdb->prefix}em_tickets as e ON b.ticket_id = e.ticket_id WHERE b.booking_id = %d AND e.ticket_id = %d ORDER BY b.ticket_booking_id DESC LIMIT 1", $all_bookings->booking_id, $ticket_id ) );
11180 if ( ! empty( $all_ticket_bookings ) ) {
11181 $user_id = $all_bookings->person_id;
11182 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
11183 $bookings_str = wp_json_encode( $all_bookings );
11184 $ticket_bookings_str = wp_json_encode( $all_ticket_bookings );
11185
11186 $context['pluggable_data'] = array_merge(
11187 WordPress::get_user_context( $user_id )
11188 );
11189 if ( is_string( $bookings_str ) && is_string( $ticket_bookings_str ) ) {
11190 $bookings_arr = json_decode( $bookings_str, true );
11191 $ticket_bookings_arr = json_decode( $ticket_bookings_str, true );
11192 if ( is_array( $bookings_arr ) && is_array( $ticket_bookings_arr ) ) {
11193 $context['pluggable_data'] = array_merge(
11194 $context['pluggable_data'],
11195 $bookings_arr,
11196 $ticket_bookings_arr
11197 );
11198 }
11199 }
11200 if ( ! empty( $location ) ) {
11201 $location_str = wp_json_encode( $location );
11202 if ( is_string( $location_str ) ) {
11203 $location_arr = json_decode( $location_str, true );
11204 if ( is_array( $location_arr ) ) {
11205 $context['pluggable_data'] = array_merge( $context['pluggable_data'], $location_arr );
11206 }
11207 }
11208 }
11209 $context['response_type'] = 'live';
11210 }
11211 }
11212 }
11213 return $context;
11214 }
11215
11216 /**
11217 * Get Events Manager Events Ticket list.
11218 *
11219 * @param array $data data.
11220 *
11221 * @return array|void
11222 */
11223 public function search_em_event_tickets( $data ) {
11224 global $wpdb;
11225
11226 $options = [];
11227
11228 $event = $data['dynamic']['post_id'];
11229
11230 $event_id = $wpdb->get_var( $wpdb->prepare( "SELECT event_id FROM {$wpdb->prefix}em_events WHERE post_id = %d", $event ) );
11231 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT ticket_id,ticket_name FROM {$wpdb->prefix}em_tickets WHERE event_id = %d ORDER BY ticket_id", $event_id[0] ) );
11232
11233 if ( $tickets ) {
11234 foreach ( $tickets as $ticket ) {
11235 $options[] = [
11236 'label' => $ticket->ticket_name,
11237 'value' => $ticket->ticket_id,
11238 ];
11239 }
11240 }
11241
11242 return [
11243 'options' => $options,
11244 'hasMore' => false,
11245 ];
11246 }
11247
11248 /**
11249
11250 * Get learnpress last data.
11251 *
11252 * @param array $data data.
11253 *
11254 * @return array
11255 */
11256 public function search_learnpress_lms_last_data( $data ) {
11257 global $wpdb;
11258 $trigger = $data['search_term'];
11259 $course_data = [];
11260 $lesson_data = [];
11261 $context = [];
11262
11263
11264 if ( 'learnpress_course_completed' === $trigger ) {
11265 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
11266 if ( $course_id > 0 ) {
11267
11268 $course = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learnpress_user_items WHERE item_id= %s && user_id>0 && status= 'finished' ORDER BY item_id DESC LIMIT 1", $course_id ) );
11269 } else {
11270
11271 $course = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}learnpress_user_items WHERE item_type= 'lp_course' && user_id>0 && status= 'finished' ORDER BY item_id DESC LIMIT 1" );
11272 }
11273
11274 if ( ! empty( $course ) ) {
11275 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
11276 $context['response_type'] = 'live';
11277 $context['pluggable_data'] = $course_data;
11278 }
11279 } elseif ( 'learnpress_lesson_completed' === $trigger ) {
11280 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
11281 if ( $lesson_id > 0 ) {
11282
11283 $lesson = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learnpress_user_items WHERE item_id= %s && user_id>0 && status= 'completed' ORDER BY item_id DESC LIMIT 1", $lesson_id ) );
11284 } else {
11285
11286 $lesson = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}learnpress_user_items WHERE item_type= 'lp_lesson' && user_id>0 && status= 'completed' ORDER BY item_id DESC LIMIT 1" );
11287 }
11288
11289 if ( ! empty( $lesson ) ) {
11290 $lesson_data = array_merge( WordPress::get_user_context( $lesson->user_id ), LearnPress::get_lpc_lesson_context( $lesson->item_id ), LearnPress::get_lpc_course_context( $lesson->ref_id ) );
11291 $context['response_type'] = 'live';
11292 $context['pluggable_data'] = $lesson_data;
11293 }
11294 } elseif ( 'learnpress_user_enrolled_in_course' === $trigger ) {
11295 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
11296 if ( $course_id > 0 ) {
11297
11298 $course = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learnpress_user_items WHERE item_id= %s && status= 'enrolled' ORDER BY item_id DESC LIMIT 1", $course_id ) );
11299 } else {
11300
11301 $course = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}learnpress_user_items WHERE item_type= 'lp_course' && user_id>0 && status= 'enrolled' ORDER BY item_id DESC LIMIT 1" );
11302 }
11303
11304 if ( ! empty( $course ) ) {
11305 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
11306 $context['response_type'] = 'live';
11307 $context['pluggable_data'] = $course_data;
11308
11309 }
11310 }
11311
11312 return $context;
11313 }
11314
11315 /**
11316 * Get Woocommerce Memberships Plan List.
11317 *
11318 * @param array $data data.
11319 *
11320 * @return array|void
11321 */
11322 public function search_wc_membership_plan_list( $data ) {
11323 global $wpdb;
11324
11325 $page = $data['page'];
11326 $limit = Utilities::get_search_page_limit();
11327 $offset = $limit * ( $page - 1 );
11328
11329 $args = [
11330 'post_type' => 'wc_membership_plan',
11331 'posts_per_page' => $limit,
11332 'offset' => $offset,
11333 'orderby' => 'title',
11334 'order' => 'ASC',
11335 'post_status' => 'publish',
11336 'fields' => 'ids',
11337 ];
11338 $loop = new WP_Query( $args );
11339
11340 $plans = (array) $loop->posts;
11341 $plans_count = wp_count_posts( 'wc_membership_plan' )->publish;
11342
11343 $options = [];
11344 if ( ! empty( $plans ) ) {
11345 if ( is_array( $plans ) ) {
11346 foreach ( $plans as $plan_id ) {
11347 $options[] = [
11348 'label' => get_the_title( $plan_id ),
11349 'value' => $plan_id,
11350 ];
11351 }
11352 }
11353 }
11354
11355 return [
11356 'options' => $options,
11357 'hasMore' => $plans_count > $limit && $plans_count > $offset,
11358 ];
11359 }
11360
11361 /**
11362 * Get BuddyPress Private group.
11363 *
11364 * @param array $data data.
11365 *
11366 * @return array|void
11367 */
11368 public function search_bp_private_group_list( $data ) {
11369 global $wpdb;
11370
11371 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'private'" );
11372
11373 $options = [];
11374 if ( $groups ) {
11375 foreach ( $groups as $group ) {
11376 $options[] = [
11377 'label' => $group->name,
11378 'value' => $group->id,
11379 ];
11380 }
11381 }
11382
11383 return [
11384 'options' => $options,
11385 'hasMore' => false,
11386 ];
11387 }
11388
11389 /**
11390 * Get BuddyPress Public group.
11391 *
11392 * @param array $data data.
11393 *
11394 * @return array|void
11395 */
11396 public function search_bp_public_group_list( $data ) {
11397 global $wpdb;
11398
11399 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'public'" );
11400
11401 $options = [];
11402 if ( $groups ) {
11403 foreach ( $groups as $group ) {
11404 $options[] = [
11405 'label' => $group->name,
11406 'value' => $group->id,
11407 ];
11408 }
11409 }
11410
11411 return [
11412 'options' => $options,
11413 'hasMore' => false,
11414 ];
11415 }
11416
11417 /**
11418 * Get BuddyPress group.
11419 *
11420 * @param array $data data.
11421 *
11422 * @return array|void
11423 */
11424 public function search_bp_group_list( $data ) {
11425 global $wpdb;
11426
11427 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
11428
11429 $options = [];
11430 if ( $groups ) {
11431 foreach ( $groups as $group ) {
11432 $options[] = [
11433 'label' => $group->name,
11434 'value' => $group->id,
11435 ];
11436 }
11437 }
11438
11439 return [
11440 'options' => $options,
11441 'hasMore' => false,
11442 ];
11443 }
11444
11445 /**
11446 * Get BuddyPress field.
11447 *
11448 * @param array $data data.
11449 *
11450 * @return array|void
11451 */
11452 public function search_bp_field_list( $data ) {
11453 global $wpdb;
11454
11455 $base_group_id = 1;
11456 if ( function_exists( 'bp_xprofile_base_group_id' ) ) {
11457 $base_group_id = bp_xprofile_base_group_id();
11458 }
11459
11460 $xprofile_fields = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}bp_xprofile_fields WHERE parent_id = 0 AND group_id = %d ORDER BY field_order ASC", $base_group_id ) );
11461
11462 $options = [];
11463 if ( ! empty( $xprofile_fields ) ) {
11464 foreach ( $xprofile_fields as $xprofile_field ) {
11465 $options[] = [
11466 'label' => $xprofile_field->name,
11467 'value' => $xprofile_field->id,
11468 ];
11469 }
11470 }
11471
11472 return [
11473 'options' => $options,
11474 'hasMore' => false,
11475 ];
11476 }
11477
11478 /**
11479 * Get BuddyPress member type.
11480 *
11481 * @param array $data data.
11482 *
11483 * @return array|void
11484 */
11485 public function search_bp_member_type_list( $data ) {
11486 $options = [];
11487 if ( function_exists( 'bp_get_member_types' ) ) {
11488 $types = bp_get_member_types( [] );
11489 if ( $types ) {
11490 foreach ( $types as $key => $type ) {
11491 $options[] = [
11492 'label' => $type,
11493 'value' => $key,
11494 ];
11495 }
11496 }
11497 }
11498
11499 return [
11500 'options' => $options,
11501 'hasMore' => false,
11502 ];
11503 }
11504
11505 /**
11506 * Get last data for WP All Import.
11507 *
11508 * @param array $data data.
11509 * @return mixed
11510 */
11511 public function search_wp_all_import_last_data( $data ) {
11512 global $wpdb;
11513 $post_type = $data['filter']['post_type']['value'];
11514 $trigger = $data['search_term'];
11515
11516 if ( 'wp_all_import_post_type_imported' === $trigger ) {
11517 if ( -1 == $post_type ) {
11518 $imports = $wpdb->get_row( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts ORDER BY id DESC LIMIT 1", ARRAY_A );
11519 $posts[0] = $imports['post_id'];
11520 } else {
11521 $imports = $wpdb->get_results( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts", ARRAY_A );
11522 $imports = array_column( $imports, 'post_id' );
11523 $args = [
11524 'posts_per_page' => 1,
11525 'post_type' => $post_type,
11526 'post__in' => $imports,
11527 ];
11528 $posts = get_posts( $args );
11529 }
11530 } elseif ( 'wp_all_import_completed' === $trigger ) {
11531 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 0 ORDER BY id DESC LIMIT 1", ARRAY_A );
11532 } elseif ( 'wp_all_import_failed' === $trigger ) {
11533 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 1 ORDER BY id DESC LIMIT 1", ARRAY_A );
11534 }
11535
11536 if ( 'wp_all_import_post_type_imported' === $trigger && empty( $imports ) ) {
11537 $context = json_decode( '{"response_type":"sample","pluggable_data":{"ID": 1,"post_author": "1","post_date": "2023-07-12 06:31:35","post_date_gmt": "2023-07-12 06:31:35","post_content": "","post_title": "Test","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "open","post_password": "","post_name": "test","to_ping": "","pinged": "","post_modified": "2023-07-12 06:31:35","post_modified_gmt": "2023-07-12 06:31:35","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/example.com\/test\/","menu_order": 0,"post_type": "post","post_mime_type": "","comment_count": "0","filter": "raw"}}', true );
11538 return $context;
11539 } elseif ( empty( $imports ) ) {
11540 $context = json_decode( '{"response_type":"sample","pluggable_data":{"id": "1","parent_import_id": "0","name": "demowpinstawpxyz.WordPress.2023_07_12.xml","friendly_name": "","type": "upload","feed_type": "","path": "\/wpallimport\/uploads\/ee8816eebf7a373454cdd1189c831241\/demowpinstawpxyz.WordPress.2023_07_12.xml","xpath": "\/rss","registered_on": "2023-07-12 05:10:29","root_element": "rss","processing": "0","executing": "0","triggered": "0","queue_chunk_number": "0","first_import": "2023-07-12 05:09:41","count": "1","imported": "0","created": "0","updated": "0","skipped": "1","deleted": "0","changed_missing": "0","canceled": "0","canceled_on": "0000-00-00 00:00:00","failed": "0","failed_on": "0000-00-00 00:00:00","settings_update_on": "0000-00-00 00:00:00","last_activity": "2023-07-12 05:10:24","iteration": "1"}}', true );
11541 return $context;
11542 }
11543
11544 $context['response_type'] = 'live';
11545 if ( ! empty( $posts ) ) {
11546 $context['pluggable_data'] = WordPress::get_post_context( $posts[0] );
11547 } else {
11548 $context['pluggable_data'] = $imports;
11549 }
11550
11551 return $context;
11552 }
11553
11554 /**
11555 * Get Wp Simple Pay Forms.
11556 *
11557 * @param array $data data.
11558 *
11559 * @return array
11560 */
11561 public function search_wp_simple_pay_forms( $data ) {
11562
11563 $page = $data['page'];
11564 $limit = Utilities::get_search_page_limit();
11565 $offset = $limit * ( $page - 1 );
11566
11567 $forms = get_posts(
11568 [
11569 'post_type' => 'simple-pay',
11570 'posts_per_page' => $limit,
11571 'offset' => $offset,
11572 'fields' => 'ids',
11573 ]
11574 );
11575
11576 $forms_count = wp_count_posts( 'simple-pay' )->publish;
11577
11578 $options = [];
11579
11580 if ( ! empty( $forms ) ) {
11581 foreach ( $forms as $form_id ) {
11582 if ( function_exists( 'simpay_get_form' ) ) {
11583 $form = simpay_get_form( $form_id );
11584 $options[] = [
11585 'label' => null !== get_the_title( $form_id ) ? $form->company_name : get_the_title( $form_id ),
11586 'value' => $form_id,
11587 ];
11588 }
11589 }
11590 }
11591
11592 return [
11593 'options' => $options,
11594 'hasMore' => $forms_count > $limit && $forms_count > $offset,
11595 ];
11596 }
11597
11598 /**
11599 * Get Post list as per post type for metabox.
11600 *
11601 * @param array $data data.
11602 *
11603 * @return array
11604 */
11605 public function search_mb_posts_list( $data ) {
11606
11607 $page = $data['page'];
11608 $limit = Utilities::get_search_page_limit();
11609 $offset = $limit * ( $page - 1 );
11610
11611 $posts = get_posts(
11612 [
11613 'post_type' => $data['dynamic'],
11614 'posts_per_page' => $limit,
11615 'offset' => $offset,
11616 'fields' => 'ids',
11617 ]
11618 );
11619
11620 $all_posts = get_posts(
11621 [
11622 'post_type' => $data['dynamic'],
11623 'posts_per_page' => -1,
11624 'fields' => 'ids',
11625 ]
11626 );
11627
11628 $posts_count = count( $all_posts );
11629
11630 $options = [];
11631
11632 if ( ! empty( $posts ) ) {
11633 foreach ( $posts as $post ) {
11634 $title = html_entity_decode( get_the_title( $post ), ENT_QUOTES, 'UTF-8' );
11635 $options[] = [
11636 'label' => $title,
11637 'value' => $post,
11638 ];
11639 }
11640 }
11641
11642 return [
11643 'options' => $options,
11644 'hasMore' => $posts_count > $limit && $posts_count > $offset,
11645 ];
11646 }
11647
11648 /**
11649 * Get Metabox Custom box in Post list.
11650 *
11651 * @param array $data data.
11652 *
11653 * @return array
11654 */
11655 public function search_mb_field_list( $data ) {
11656
11657 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
11658 return [];
11659 }
11660
11661 $options = [];
11662
11663 $metabox_fields = (array) rwmb_get_object_fields( $data['dynamic'] );
11664
11665 foreach ( $metabox_fields as $metabox_field ) {
11666
11667 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
11668
11669 $options[] = [
11670 'label' => $metabox_field['name'],
11671 'value' => $metabox_field['id'],
11672 ];
11673
11674 }
11675 }
11676
11677 return [
11678 'options' => $options,
11679 'hasMore' => false,
11680 ];
11681 }
11682
11683 /**
11684 * Get Metabox Custom box user list.
11685 *
11686 * @param array $data data.
11687 *
11688 * @return array
11689 */
11690 public function search_mb_user_field_list( $data ) {
11691
11692 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
11693 return [];
11694 }
11695
11696 $options = [];
11697
11698 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
11699
11700 foreach ( $metabox_fields as $metabox_field ) {
11701
11702 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
11703
11704 $options[] = [
11705 'label' => $metabox_field['name'],
11706 'value' => $metabox_field['id'],
11707 ];
11708
11709 }
11710 }
11711
11712 return [
11713 'options' => $options,
11714 'hasMore' => false,
11715 ];
11716 }
11717
11718 /**
11719 * Search Last Updated Field Data for MetaBox.
11720 *
11721 * @param array $data data.
11722 * @return array
11723 */
11724 public function search_meta_box_field_data( $data ) {
11725 global $wpdb;
11726
11727 $context = [];
11728
11729 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
11730
11731 $post_type = $data['filter']['wp_post_type']['value'];
11732 $post = $data['filter']['wp_post']['value'];
11733
11734 if ( -1 === $field ) {
11735 if ( function_exists( 'rwmb_get_object_fields' ) ) {
11736 $metaboxes = rwmb_get_object_fields( $post_type );
11737
11738 if ( ! empty( $metaboxes ) ) {
11739 $random_key = array_rand( $metaboxes );
11740 $field = $random_key;
11741 } else {
11742 $result = '';
11743 }
11744 }
11745 } else {
11746 $field = $data['filter']['field_id']['value'];
11747 }
11748
11749 if ( function_exists( 'rwmb_meta' ) ) {
11750 $result = rwmb_meta( $field, '', $post );
11751 }
11752
11753 $response = [];
11754 if ( ! empty( $result ) ) {
11755 $response['pluggable_data'] = array_merge( [ $field => $result ], WordPress::get_post_context( $post ) );
11756 $response['response_type'] = 'live';
11757 } else {
11758 $response = json_decode( '{"response_type":"sample","pluggable_data":{"custom_description": "custom message", "ID": 1, "post_author": "1", "post_date": "2023-05-31 13:26:24", "post_date_gmt": "2023-05-31 13:26:24", "post_content": "", "post_title": "Test", "post_excerpt": "", "post_status": "publish", "comment_status": "open", "ping_status": "open", "post_password": "", "post_name": "test", "to_ping": "", "pinged": "", "post_modified": "2023-08-17 09:15:56", "post_modified_gmt": "2023-08-17 09:15:56", "post_content_filtered": "", "post_parent": 0, "guid": "https:\/\/example.com\/?p=1", "menu_order": 0, "post_type": "post", "post_mime_type": "", "comment_count": "2", "filter": "raw"}}', true );
11759 }
11760
11761 return $response;
11762 }
11763
11764 /**
11765 * Search Last Updated User Field Data MetaBox.
11766 *
11767 * @param array $data data.
11768 * @return array
11769 */
11770 public function search_user_meta_box_field_data( $data ) {
11771 global $wpdb;
11772
11773 $context = [];
11774
11775 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
11776
11777 if ( -1 === $field ) {
11778 if ( function_exists( 'rwmb_get_object_fields' ) ) {
11779 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
11780
11781 if ( ! empty( $metabox_fields ) ) {
11782 $random_key = array_rand( $metabox_fields );
11783 $field = $random_key;
11784 } else {
11785 $result = '';
11786 }
11787 }
11788 } else {
11789 $field = $data['filter']['field_id']['value'];
11790 }
11791
11792 $users = get_users(
11793 [
11794 'fields' => 'ID',
11795 'meta_key' => $field,
11796 ]
11797 );
11798
11799 if ( ! empty( $users ) ) {
11800 $user_random_key = array_rand( $users );
11801 $user_id = $user_random_key;
11802 if ( function_exists( 'rwmb_get_value' ) ) {
11803 $result = rwmb_get_value( $field, [ 'object_type' => 'user' ], $users[ $user_id ] );
11804 }
11805
11806 $response = [];
11807 if ( ! empty( $result ) ) {
11808 $context = [
11809 'field_id' => $field,
11810 $field => $result,
11811 'user' => WordPress::get_user_context( $users[ $user_id ] ),
11812 ];
11813 $response['pluggable_data'] = $context;
11814 $response['response_type'] = 'live';
11815 } else {
11816 $response = json_decode(
11817 '{
11818 "response_type": "sample",
11819 "pluggable_data": {
11820 "field_id": "gender",
11821 "user": {
11822 "wp_user_id": 114,
11823 "user_login": "test",
11824 "display_name": "test",
11825 "user_firstname": "test",
11826 "user_lastname": "test",
11827 "user_email": "test@test.com",
11828 "user_role": [ "subscriber" ]
11829 }
11830 }
11831 }',
11832 true
11833 );
11834 }
11835 } else {
11836 $response = json_decode(
11837 '{
11838 "response_type": "sample",
11839 "pluggable_data": {
11840 "field_id": "gender",
11841 "user": {
11842 "wp_user_id": 114,
11843 "user_login": "test",
11844 "display_name": "test",
11845 "user_firstname": "test",
11846 "user_lastname": "test",
11847 "user_email": "test@test.com",
11848 "user_role": [ "subscriber" ]
11849 }
11850 }
11851 }',
11852 true
11853 );
11854 }
11855
11856 return $response;
11857 }
11858
11859 /**
11860 * Search forms of Pie Forms.
11861 *
11862 * @param array $data data.
11863 * @return array
11864 */
11865 public function search_wp_polls_list( $data ) {
11866 global $wpdb;
11867 $options = [];
11868
11869 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsq' ) ) ) {
11870
11871 $results = $wpdb->get_results( 'SELECT pollq_id, pollq_question FROM ' . $wpdb->prefix . 'pollsq WHERE pollq_active = 1' );
11872
11873 if ( $results ) {
11874 foreach ( $results as $result ) {
11875 $options[] = [
11876 'label' => $result->pollq_question,
11877 'value' => $result->pollq_id,
11878 ];
11879 }
11880 }
11881 }
11882
11883 return [
11884 'options' => $options,
11885 'hasMore' => false,
11886 ];
11887 }
11888
11889 /**
11890 * Search answers of WP-Polls questions.
11891 *
11892 * @param array $data data.
11893 * @return array
11894 */
11895 public function search_wp_polls_answers( $data ) {
11896 global $wpdb;
11897
11898 $options = [];
11899 $poll_id = $data['dynamic'];
11900
11901 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsa' ) ) ) {
11902
11903 if ( '-1' !== $poll_id ) {
11904 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa WHERE polla_qid = %d', $poll_id ) );
11905 } else {
11906 $results = $wpdb->get_results( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa' );
11907 }
11908
11909 if ( $results ) {
11910 foreach ( $results as $result ) {
11911 $options[] = [
11912 'label' => $result->polla_answers,
11913 'value' => $result->polla_aid,
11914 ];
11915 }
11916 }
11917 }
11918
11919 return [
11920 'options' => $options,
11921 'hasMore' => false,
11922 ];
11923 }
11924
11925 /**
11926 * Get last data for trigger.
11927 *
11928 * @param array $data data.
11929 * @return array
11930 */
11931 public function search_wp_polls_triggers_last_data( $data ) {
11932 global $wpdb;
11933
11934 $context = [];
11935 $context['response_type'] = 'sample';
11936
11937 $poll = [
11938 'poll_id' => 1,
11939 'question' => 'Which skills are you interested to learn?',
11940 'answers' => 'Web Development, Graphic Designing, Content Writing, Digital Marketing',
11941 'start_date' => '2023-08-29 17:19:13',
11942 'end_date' => 'Not set',
11943 'selected_answers' => 'Content Writing, Web Development',
11944 'selected_answer_id' => 2,
11945 ];
11946
11947 $poll_data = $wpdb->get_row( "SELECT pollip_qid AS poll_id, pollip_aid AS answer_id FROM {$wpdb->prefix}pollsip ORDER BY pollip_id DESC LIMIT 1" );
11948
11949 if ( ! empty( $poll_data ) ) {
11950 $poll = WpPolls::get_poll_context( (string) $poll_data->answer_id, (int) $poll_data->poll_id );
11951 $poll['selected_answer_id'] = (int) $poll_data->answer_id;
11952
11953 $context['response_type'] = 'live';
11954 }
11955
11956 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
11957
11958 if ( 'poll_submitted' === $term ) {
11959 unset( $poll['selected_answer_id'] );
11960 }
11961
11962 $context['pluggable_data'] = $poll;
11963
11964 return $context;
11965 }
11966
11967 /**
11968 * Get ACF Custom fields list.
11969 *
11970 * @param array $data data.
11971 *
11972 * @return array
11973 */
11974 public function search_acf_post_field_list( $data ) {
11975
11976 $post_id = $data['dynamic']['wp_post'];
11977 $selected_post_type = $data['dynamic']['wp_post_type'];
11978
11979 if ( -1 === $post_id ) {
11980 $args = [
11981 'numberposts' => 1,
11982 'fields' => 'ids',
11983 'orderby' => 'rand',
11984 'post_type' => $selected_post_type,
11985 ];
11986 $posts = get_posts( $args );
11987 $post_id = $posts[0];
11988 }
11989
11990 $args = [
11991 'post_id' => $post_id,
11992 ];
11993 if ( ! is_numeric( $post_id ) ) {
11994 $args = [
11995 'post_type' => $post_id,
11996 ];
11997 }
11998 $options = [];
11999 if ( function_exists( 'acf_get_field_groups' ) ) {
12000 $field_groups_collection = acf_get_field_groups( $args );
12001 foreach ( $field_groups_collection as $field_group ) {
12002 if ( function_exists( 'acf_get_fields' ) ) {
12003 $field_groups[] = acf_get_fields( $field_group['key'] );
12004 }
12005 }
12006
12007 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
12008 foreach ( $field_groups as $field_groups ) {
12009 foreach ( $field_groups as $field_group ) {
12010 $options[] = [
12011 'value' => $field_group['name'],
12012 'label' => ! empty( $field_group['label'] ) ? $field_group['label'] : $field_group['name'],
12013 ];
12014 }
12015 }
12016 }
12017 }
12018
12019 return [
12020 'options' => $options,
12021 'hasMore' => false,
12022 ];
12023 }
12024
12025 /**
12026 * Get ACF Custom fields list.
12027 *
12028 * @param array $data data.
12029 *
12030 * @return array
12031 */
12032 public function search_acf_user_field_list( $data ) {
12033
12034 if ( ! function_exists( 'acf_get_fields' ) ) {
12035 return [];
12036 }
12037 if ( ! function_exists( 'acf_get_field_groups' ) ) {
12038 return [];
12039 }
12040 $groups_user_form = [];
12041 $options = [];
12042 if ( function_exists( 'acf_get_field_groups' ) ) {
12043 $field_groups = acf_get_field_groups();
12044 foreach ( $field_groups as $group ) {
12045 if ( ! empty( $group['location'] ) ) {
12046 foreach ( $group['location'] as $locations ) {
12047 foreach ( $locations as $location ) {
12048 if ( 'user_form' === $location['param'] || 'user_role' === $location['param'] || 'current_user' === $location['param'] || 'current_user_role' === $location['param'] ) {
12049 $groups_user_form[] = $group;
12050 }
12051 }
12052 }
12053 }
12054 }
12055
12056 if ( empty( $groups_user_form ) ) {
12057 return [];
12058 }
12059
12060 $key_values = array_map(
12061 function ( $item ) {
12062 return $item['key'];
12063 },
12064 $groups_user_form
12065 );
12066 $unique_keys = array_unique( $key_values );
12067 $unique_array = array_intersect_key( $groups_user_form, $unique_keys );
12068
12069 foreach ( $unique_array as $group ) {
12070 if ( function_exists( 'acf_get_fields' ) ) {
12071 $group_fields = acf_get_fields( $group['key'] );
12072 }
12073 if ( ! empty( $group_fields ) ) {
12074 foreach ( $group_fields as $field ) {
12075 $options[] = [
12076 'value' => $field['name'],
12077 'label' => $field['label'],
12078 ];
12079
12080 }
12081 }
12082 }
12083 }
12084
12085 return [
12086 'options' => $options,
12087 'hasMore' => false,
12088 ];
12089 }
12090
12091 /**
12092 * Get ACF Custom fields list.
12093 *
12094 * @param array $data data.
12095 *
12096 * @return array
12097 */
12098 public function search_acf_options_field_list( $data ) {
12099
12100 if ( ! function_exists( 'acf_get_fields' ) ) {
12101 return [];
12102 }
12103 if ( ! function_exists( 'acf_get_field_groups' ) ) {
12104 return [];
12105 }
12106 $groups_options_form = [];
12107 $options = [];
12108 if ( function_exists( 'acf_get_field_groups' ) ) {
12109 $field_groups = acf_get_field_groups();
12110 foreach ( $field_groups as $group ) {
12111 if ( ! empty( $group['location'] ) ) {
12112 foreach ( $group['location'] as $locations ) {
12113 foreach ( $locations as $location ) {
12114 if ( 'options_page' === $location['param'] ) {
12115 $groups_options_form[] = $group;
12116 }
12117 }
12118 }
12119 }
12120 }
12121 if ( empty( $groups_options_form ) ) {
12122 return [];
12123 }
12124 $key_values = array_map(
12125 function ( $item ) {
12126 return $item['key'];
12127 },
12128 $groups_options_form
12129 );
12130 $unique_keys = array_unique( $key_values );
12131 $unique_array = array_intersect_key( $groups_options_form, $unique_keys );
12132 foreach ( $unique_array as $group ) {
12133 if ( function_exists( 'acf_get_fields' ) ) {
12134 $group_fields = acf_get_fields( $group['key'] );
12135 }
12136 if ( ! empty( $group_fields ) ) {
12137 foreach ( $group_fields as $field ) {
12138 $options[] = [
12139 'value' => $field['name'],
12140 'label' => $field['label'],
12141 ];
12142
12143 }
12144 }
12145 }
12146 }
12147
12148 return [
12149 'options' => $options,
12150 'hasMore' => false,
12151 ];
12152 }
12153
12154 /**
12155 * Search Last Updated Field Data for ACF.
12156 *
12157 * @param array $data data.
12158 * @return array
12159 */
12160 public function search_acf_post_field_data( $data ) {
12161 $context = [];
12162
12163 $field = ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12164
12165 $post_type = $data['filter']['wp_post_type']['value'];
12166 $post = $data['filter']['wp_post']['value'];
12167
12168 if ( -1 === $post ) {
12169 $args = [
12170 'numberposts' => 1,
12171 'fields' => 'ids',
12172 'orderby' => 'rand',
12173 'post_type' => $post_type,
12174 ];
12175 $posts = get_posts( $args );
12176 $post = $posts[0];
12177 }
12178 if ( -1 === $field ) {
12179 $args = [
12180 'post_id' => $post,
12181 ];
12182 if ( function_exists( 'acf_get_field_groups' ) ) {
12183 $field_groups_collection = acf_get_field_groups( $args );
12184 }
12185 if ( ! empty( $field_groups_collection ) ) {
12186 foreach ( $field_groups_collection as $field_group ) {
12187 if ( function_exists( 'acf_get_fields' ) ) {
12188 $field_groups[] = acf_get_fields( $field_group['key'] );
12189 }
12190 }
12191 }
12192 $fields = [];
12193 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
12194 foreach ( $field_groups as $field_groups ) {
12195 $fields[] = $field_groups;
12196 }
12197 }
12198 if ( ! empty( $fields ) ) {
12199 $random_key = array_rand( $fields[0] );
12200 $field_key = $fields[0][ $random_key ];
12201 $field = $field_key['name'];
12202 } else {
12203 $result = '';
12204 }
12205 } else {
12206 $field = $data['filter']['field_id']['value'];
12207 }
12208 if ( function_exists( ( 'get_field' ) ) ) {
12209 $result = get_field( $field, $post );
12210 }
12211
12212 $response = [];
12213 if ( ! empty( $result ) ) {
12214 $post_fields = [];
12215 if ( function_exists( 'get_fields' ) ) {
12216 $post_fields = get_fields( $post );
12217 }
12218 $response['pluggable_data'] = array_merge( [ $field => $result ], [ 'field_id' => $field ], [ 'post_fields' => $post_fields ], [ 'post' => WordPress::get_post_context( $post ) ], [ 'wp_post' => $post ], [ 'wp_post_type' => get_post_type( $post ) ] );
12219 $response['response_type'] = 'live';
12220 } else {
12221 $response = json_decode( '{"response_type":"sample","pluggable_data":{"custom_description": "custom message", "ID": 1, "post_author": "1", "post_date": "2023-05-31 13:26:24", "post_date_gmt": "2023-05-31 13:26:24", "post_content": "", "post_title": "Test", "post_excerpt": "", "post_status": "publish", "comment_status": "open", "ping_status": "open", "post_password": "", "post_name": "test", "to_ping": "", "pinged": "", "post_modified": "2023-08-17 09:15:56", "post_modified_gmt": "2023-08-17 09:15:56", "post_content_filtered": "", "post_parent": 0, "guid": "https:\/\/example.com\/?p=1", "menu_order": 0, "post_type": "post", "post_mime_type": "", "comment_count": "2", "filter": "raw"}}', true );
12222 }
12223
12224 return $response;
12225 }
12226
12227 /**
12228 * Search Last Updated User Field Data ACF.
12229 *
12230 * @param array $data data.
12231 * @return array
12232 */
12233 public function search_acf_user_field_data( $data ) {
12234 global $wpdb;
12235
12236 $context = [];
12237
12238 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12239
12240 if ( -1 === $field ) {
12241 $groups_user_form = [];
12242 if ( function_exists( 'acf_get_field_groups' ) ) {
12243 $field_groups = acf_get_field_groups();
12244 }
12245 if ( ! empty( $field_groups ) ) {
12246 foreach ( $field_groups as $group ) {
12247 if ( ! empty( $group['location'] ) ) {
12248 foreach ( $group['location'] as $locations ) {
12249 foreach ( $locations as $location ) {
12250 if ( 'user_form' === $location['param'] || 'user_role' === $location['param'] || 'current_user' === $location['param'] || 'current_user_role' === $location['param'] ) {
12251 $groups_user_form[] = $group;
12252 }
12253 }
12254 }
12255 }
12256 }
12257 $field_groups = $groups_user_form;
12258 }
12259 if ( empty( $field_groups ) ) {
12260 $result = '';
12261 }
12262 $fields = [];
12263 if ( ! empty( $field_groups ) ) {
12264 foreach ( $field_groups as $group ) {
12265 if ( function_exists( 'acf_get_fields' ) ) {
12266 $group_fields = acf_get_fields( $group['key'] );
12267 }
12268 if ( ! empty( $group_fields ) ) {
12269 foreach ( $group_fields as $field ) {
12270 $fields[] = $group_fields;
12271 }
12272 }
12273 }
12274 }
12275 if ( ! empty( $fields ) ) {
12276 $random_key = array_rand( $fields );
12277 $field = $random_key;
12278 } else {
12279 $result = '';
12280 }
12281 } else {
12282 $field = $data['filter']['field_id']['value'];
12283 }
12284 $users = get_users(
12285 [
12286 'fields' => 'ID',
12287 'meta_key' => $field,
12288 ]
12289 );
12290
12291 if ( ! empty( $users ) ) {
12292 $user_random_key = array_rand( $users );
12293 $user_id = $user_random_key;
12294 if ( function_exists( 'get_field' ) ) {
12295 $result = get_field( $field, 'user_' . $users[ $user_id ] );
12296 }
12297 $response = [];
12298 if ( ! empty( $result ) ) {
12299 $context = [
12300 'field_id' => $field,
12301 $field => $result,
12302 'user' => WordPress::get_user_context( $users[ $user_id ] ),
12303 ];
12304 $response['pluggable_data'] = $context;
12305 $response['response_type'] = 'live';
12306 } else {
12307 $response = json_decode(
12308 '{
12309 "response_type": "sample",
12310 "pluggable_data": {
12311 "field_id": "gender",
12312 "user": {
12313 "wp_user_id": 114,
12314 "user_login": "test",
12315 "display_name": "test",
12316 "user_firstname": "test",
12317 "user_lastname": "test",
12318 "user_email": "test@test.com",
12319 "user_role": [ "subscriber" ]
12320 }
12321 }
12322 }',
12323 true
12324 );
12325 }
12326 } else {
12327 $response = json_decode(
12328 '{
12329 "response_type": "sample",
12330 "pluggable_data": {
12331 "field_id": "gender",
12332 "user": {
12333 "wp_user_id": 114,
12334 "user_login": "test",
12335 "display_name": "test",
12336 "user_firstname": "test",
12337 "user_lastname": "test",
12338 "user_email": "test@test.com",
12339 "user_role": [ "subscriber" ]
12340 }
12341 }
12342 }',
12343 true
12344 );
12345 }
12346
12347 return $response;
12348 }
12349
12350 /**
12351 * Search Last Updated Options Field Data ACF.
12352 *
12353 * @param array $data data.
12354 * @return array
12355 */
12356 public function search_acf_options_field_data( $data ) {
12357 global $wpdb;
12358 $context = [];
12359 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12360
12361 if ( -1 === $field ) {
12362 $groups_options_form = [];
12363 if ( function_exists( 'acf_get_field_groups' ) ) {
12364 $field_groups = acf_get_field_groups();
12365 }
12366 if ( ! empty( $field_groups ) ) {
12367 foreach ( $field_groups as $group ) {
12368 if ( ! empty( $group['location'] ) ) {
12369 foreach ( $group['location'] as $locations ) {
12370 foreach ( $locations as $location ) {
12371 if ( 'options_page' === $location['param'] ) {
12372 $groups_options_form[] = $group;
12373 }
12374 }
12375 }
12376 }
12377 }
12378 }
12379 if ( empty( $groups_options_form ) ) {
12380 $result = '';
12381 }
12382 $key_values = array_map(
12383 function ( $item ) {
12384 return $item['key'];
12385 },
12386 $groups_options_form
12387 );
12388 $unique_keys = array_unique( $key_values );
12389 $unique_array = array_intersect_key( $groups_options_form, $unique_keys );
12390 $fields = [];
12391 if ( ! empty( $unique_array ) ) {
12392 foreach ( $unique_array as $group ) {
12393 if ( function_exists( 'acf_get_fields' ) ) {
12394 $group_fields = acf_get_fields( $group['key'] );
12395 }
12396 if ( ! empty( $group_fields ) ) {
12397 foreach ( $group_fields as $field ) {
12398 $fields[] = $group_fields;
12399 }
12400 }
12401 }
12402 }
12403 if ( ! empty( $fields ) ) {
12404 $random_key = array_rand( $fields );
12405 $field = $random_key;
12406 } else {
12407 $result = '';
12408 }
12409 } else {
12410 $field = $data['filter']['field_id']['value'];
12411 }
12412 if ( function_exists( 'get_field' ) ) {
12413 $option_value = get_field( $field, 'option' );
12414 }
12415 if ( ! empty( $option_value ) ) {
12416 if ( function_exists( 'acf_get_field' ) ) {
12417 $options_fields = acf_get_field( $field );
12418 if ( function_exists( 'acf_maybe_get' ) ) {
12419 $options_page = acf_maybe_get( $options_fields, 'parent' );
12420 }
12421 }
12422 $context = [
12423 'field_id' => $field,
12424 $field => $option_value,
12425 ];
12426 $response['pluggable_data'] = $context;
12427 $response['response_type'] = 'live';
12428 } else {
12429 $response = json_decode(
12430 '{
12431 "response_type": "sample",
12432 "pluggable_data": {
12433 "field_id": "optionpage",
12434 "optionpage": "newoption"
12435 }
12436 }',
12437 true
12438 );
12439 }
12440 return $response;
12441 }
12442
12443 /**
12444 * Get WP Fusion Tags list.
12445 *
12446 * @param array $data data.
12447 *
12448 * @return array
12449 */
12450 public function search_wp_fusion_tag_list( $data ) {
12451
12452 if ( ! function_exists( 'wp_fusion' ) ) {
12453 return [];
12454 }
12455
12456 $options = [];
12457 $tags = wp_fusion()->settings->get( 'available_tags' );
12458
12459 if ( $tags ) {
12460 foreach ( $tags as $t_id => $tag ) {
12461 if ( is_array( $tag ) && isset( $tag['label'] ) ) {
12462 $options[] = [
12463 'value' => $t_id,
12464 'label' => $tag['label'],
12465 ];
12466 } else {
12467 $options[] = [
12468 'value' => $t_id,
12469 'label' => $tag,
12470 ];
12471 }
12472 }
12473 }
12474
12475 return [
12476 'options' => $options,
12477 'hasMore' => false,
12478 ];
12479 }
12480
12481 /**
12482 * Get list of events for Modern Events Calendar.
12483 *
12484 * @param array $data data.
12485 * @return array
12486 */
12487 public function search_mec_events_list( $data ) {
12488 $page = $data['page'];
12489 $limit = Utilities::get_search_page_limit();
12490 $offset = $limit * ( $page - 1 );
12491
12492 $args = [
12493 'post_type' => 'mec-events',
12494 'post_status' => [ 'publish', 'private' ],
12495 'posts_per_page' => -1,
12496 ];
12497 $loop = new WP_Query( $args );
12498 $events_count = count( $loop->posts );
12499
12500 $args = [
12501 'post_type' => 'mec-events',
12502 'posts_per_page' => $limit,
12503 'offset' => $offset,
12504 'orderby' => 'title',
12505 'order' => 'ASC',
12506 'post_status' => [ 'publish', 'private' ],
12507 ];
12508
12509 $loop = new WP_Query( $args );
12510 $events = $loop->posts;
12511
12512 $options = [];
12513 if ( ! empty( $events ) ) {
12514 foreach ( $events as $event ) {
12515 if ( isset( $event->ID ) ) {
12516 $options[] = [
12517 'label' => get_the_title( $event ),
12518 'value' => $event->ID,
12519 ];
12520 }
12521 }
12522 }
12523
12524 return [
12525 'options' => $options,
12526 'hasMore' => $events_count > $limit && $events_count > $offset,
12527 ];
12528 }
12529
12530 /**
12531 * Search tickets of MEC events.
12532 *
12533 * @param array $data data.
12534 * @return array
12535 */
12536 public function search_mec_event_tickets( $data ) {
12537 $options = [];
12538 $event_id = $data['dynamic'];
12539
12540 $event_tickets = get_post_meta( $event_id, 'mec_tickets', true );
12541
12542 if ( ! empty( $event_tickets ) && is_array( $event_tickets ) ) {
12543 foreach ( $event_tickets as $ticket_id => $event_ticket ) {
12544 if ( isset( $event_ticket['name'] ) ) {
12545 $options[] = [
12546 'label' => $event_ticket['name'],
12547 'value' => $ticket_id,
12548 ];
12549 }
12550 }
12551 }
12552
12553 return [
12554 'options' => $options,
12555 'hasMore' => false,
12556 ];
12557 }
12558
12559 /**
12560 * Get last data for trigger.
12561 *
12562 * @param array $data data.
12563 * @return array
12564 */
12565 public function search_mec_triggers_last_data( $data ) {
12566 global $wpdb;
12567
12568 $context = [];
12569 $context['response_type'] = 'sample';
12570
12571 $event = [
12572 'event_id' => 1,
12573 'title' => 'Sample Event',
12574 'description' => 'Description of the sample event.',
12575 'categories' => 'New, Sample',
12576 'start_date' => 'September 13, 2023',
12577 'start_time' => '8:00 AM',
12578 'end_date' => 'September 13, 2023',
12579 'end_time' => '11:00 AM',
12580 'location' => 'City Hall',
12581 'organizer' => 'John Doe',
12582 'cost' => '5000',
12583 'featured_image_id' => 1,
12584 'featured_image_url' => 'https://suretriggers.com/wp-content/uploads/2022/12/Screenshot_20221127_021332.png',
12585 'tickets' => [
12586 [
12587 'id' => 1,
12588 'name' => 'Silver',
12589 'description' => 'Standard seat with reasonable price.',
12590 'price' => '300',
12591 'price_label' => 'USD',
12592 'limit' => '20',
12593 ],
12594 [
12595 'id' => 2,
12596 'name' => 'Premium',
12597 'description' => 'VIP seat with high price.',
12598 'price' => '500',
12599 'price_label' => 'USD',
12600 'limit' => '10',
12601 ],
12602 ],
12603 'attendees' => [
12604 [
12605 'id' => 1,
12606 'email' => 'johndoe@test.com',
12607 'name' => 'John Doe',
12608 ],
12609 [
12610 'id' => 2,
12611 'email' => 'adamsmith@test.com',
12612 'name' => 'Adam Smith',
12613 ],
12614 ],
12615 'booking' => [
12616 'title' => 'johndoe@test.com - John Doe',
12617 'transaction_id' => 'RSH59404',
12618 'amount_payable' => '800',
12619 'price' => '800',
12620 'time' => '2023-09-07 06:40:32',
12621 'payment_gateway' => 'Manual Pay',
12622 'confirmation_status' => 'Pending',
12623 'verification_status' => 'Verified',
12624 'attendees_count' => 2,
12625 ],
12626 ];
12627
12628 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
12629
12630 $where = '';
12631
12632 if ( 'cancelled' === $term ) {
12633 $where = 'WHERE verified = -1';
12634 } elseif ( 'confirmed' === $term ) {
12635 $where = 'WHERE confirmed = 1';
12636 } elseif ( 'pending' === $term ) {
12637 $where = 'WHERE confirmed = 0';
12638 }
12639
12640 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
12641
12642 if ( -1 !== $event_id ) {
12643 if ( ! empty( $where ) ) {
12644 $where .= ' AND event_id = ' . $event_id;
12645 } else {
12646 $where = 'WHERE event_id = ' . $event_id;
12647 }
12648 }
12649
12650 $event_data = $wpdb->get_row( "SELECT booking_id FROM {$wpdb->prefix}mec_bookings $where ORDER BY id DESC LIMIT 1" ); // @phpcs:ignore
12651
12652 if ( ! empty( $event_data ) ) {
12653 $event = ModernEventsCalendar::get_event_context( (int) $event_data->booking_id );
12654 $context['response_type'] = 'live';
12655 }
12656
12657 $context['pluggable_data'] = $event;
12658
12659 return $context;
12660 }
12661
12662 /**
12663 * Get form list Contact Form 7.
12664 *
12665 * @param array $data data.
12666 *
12667 * @return array
12668 */
12669 public function search_contact_form7_list( $data ) {
12670
12671 $page = $data['page'];
12672 $limit = Utilities::get_search_page_limit();
12673 $offset = $limit * ( $page - 1 );
12674
12675 $posts = get_posts(
12676 [
12677 'post_type' => 'wpcf7_contact_form',
12678 'posts_per_page' => $limit,
12679 'offset' => $offset,
12680 ]
12681 );
12682
12683 $all_posts = get_posts(
12684 [
12685 'post_type' => 'wpcf7_contact_form',
12686 'posts_per_page' => -1,
12687 ]
12688 );
12689
12690 $posts_count = count( $all_posts );
12691
12692 $options = [];
12693
12694 if ( ! empty( $posts ) ) {
12695 foreach ( $posts as $post ) {
12696 $options[] = [
12697 'label' => get_the_title( $post->ID ),
12698 'value' => $post->ID,
12699 ];
12700 }
12701 }
12702
12703 return [
12704 'options' => $options,
12705 'hasMore' => $posts_count > $limit && $posts_count > $offset,
12706 ];
12707 }
12708
12709 /**
12710 * Get Thrive Leads form list
12711 *
12712 * @param array $data data.
12713 *
12714 * @return array
12715 */
12716 public function search_thrive_leads_forms_list( $data ) {
12717 $options = [];
12718
12719 $lg_ids = get_posts(
12720 [
12721 'post_type' => '_tcb_form_settings',
12722 'fields' => 'id=>parent',
12723 'posts_per_page' => -1,
12724 'post_status' => 'any',
12725 ]
12726 );
12727
12728 if ( function_exists( 'tve_leads_get_form_variations' ) ) {
12729 foreach ( $lg_ids as $lg_id => $lg_parent ) {
12730 $variations = tve_leads_get_form_variations( $lg_parent );
12731 foreach ( $variations as $variation ) {
12732 $options[] = [
12733 'label' => $variation['post_title'],
12734 'value' => $lg_parent,
12735 ];
12736 }
12737 }
12738 }
12739
12740 return [
12741 'options' => $options,
12742 'hasMore' => false,
12743 ];
12744 }
12745
12746 /**
12747 * Get list for Woocommerce Subscriptions
12748 *
12749 * @param array $data data.
12750 *
12751 * @return array
12752 */
12753 public function search_wc_subscription_variation_products( $data ) {
12754 $options = [];
12755
12756 global $wpdb;
12757
12758 if ( ! function_exists( 'wc_get_product' ) ) {
12759 return [];
12760 }
12761 $subscriptions = $wpdb->get_results(
12762 $wpdb->prepare(
12763 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
12764 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
12765 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
12766 AND posts.post_type = %s
12767 AND posts.post_status = %s
12768 UNION ALL
12769 SELECT ID, post_title FROM $wpdb->posts
12770 WHERE post_type = %s
12771 AND post_status = %s
12772 ORDER BY post_title",
12773 'product',
12774 'publish',
12775 'shop_subscription',
12776 'publish'
12777 )
12778 );
12779
12780 if ( $subscriptions ) {
12781 foreach ( $subscriptions as $product ) {
12782 $options[] = [
12783 'label' => $product->post_title . ' (#' . $product->ID . ')',
12784 'value' => (int) $product->ID,
12785 ];
12786 $product_s = wc_get_product( $product->ID );
12787 /**
12788 *
12789 * Ignore line
12790 *
12791 * @phpstan-ignore-next-line
12792 */
12793 if ( 'variable-subscription' == $product_s->product_type ) {
12794 $args = [
12795 'post_type' => 'product_variation',
12796 'post_parent' => $product->ID,
12797 'posts_per_page' => -1,
12798 'orderby' => 'ID',
12799 'order' => 'ASC',
12800 'post_status' => 'publish',
12801 ];
12802
12803 $variations = get_posts( $args );
12804
12805 foreach ( $variations as $var ) {
12806 $options[] = [
12807 'label' => $var->post_title . ' (#' . $var->ID . ')',
12808 'value' => $var->ID,
12809 ];
12810 }
12811 }
12812 }
12813 }
12814
12815 return [
12816 'options' => $options,
12817 'hasMore' => false,
12818 ];
12819 }
12820
12821 /**
12822 * Get WS Forms form list
12823 *
12824 * @param array $data data.
12825 *
12826 * @return array
12827 */
12828 public function search_ws_forms_list( $data ) {
12829 $options = [];
12830 global $wpdb;
12831
12832 $forms = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wsf_form", 'ARRAY_A' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
12833
12834 foreach ( $forms as $form ) {
12835 $options[] = [
12836 'label' => $form['label'],
12837 'value' => $form['id'],
12838 ];
12839 }
12840
12841 return [
12842 'options' => $options,
12843 'hasMore' => false,
12844 ];
12845 }
12846
12847 /**
12848 * Get Learndash Achievement list
12849 *
12850 * @param array $data data.
12851 *
12852 * @return array
12853 */
12854 public function search_ld_achievements_list( $data ) {
12855 $page = $data['page'];
12856 $limit = Utilities::get_search_page_limit();
12857 $offset = $limit * ( $page - 1 );
12858
12859 $posts = get_posts(
12860 [
12861 'post_type' => 'ld-achievement',
12862 'posts_per_page' => $limit,
12863 'offset' => $offset,
12864 'post_status' => [ 'publish' ],
12865 ]
12866 );
12867
12868 $posts_count = wp_count_posts( 'ld-achievement' )->publish;
12869
12870 $options = [];
12871
12872 if ( ! empty( $posts ) ) {
12873 foreach ( $posts as $post ) {
12874 $options[] = [
12875 'label' => get_the_title( $post->ID ),
12876 'value' => $post->ID,
12877 ];
12878 }
12879 }
12880
12881 return [
12882 'options' => $options,
12883 'hasMore' => $posts_count > $limit && $posts_count > $offset,
12884 ];
12885 }
12886
12887 /**
12888 * Get Advanced Ads list
12889 *
12890 * @param array $data data.
12891 *
12892 * @return array
12893 */
12894 public function search_ads_list( $data ) {
12895 $page = $data['page'];
12896 $limit = Utilities::get_search_page_limit();
12897 $offset = $limit * ( $page - 1 );
12898
12899 $posts = get_posts(
12900 [
12901 'post_type' => 'advanced_ads',
12902 'posts_per_page' => $limit,
12903 'offset' => $offset,
12904 'post_status' => [ 'publish', 'draft' ],
12905 ]
12906 );
12907
12908 $posts_count = wp_count_posts( 'advanced_ads' )->publish;
12909
12910 $options = [];
12911
12912 if ( ! empty( $posts ) ) {
12913 foreach ( $posts as $post ) {
12914 $options[] = [
12915 'label' => get_the_title( $post->ID ),
12916 'value' => $post->ID,
12917 ];
12918 }
12919 }
12920
12921 return [
12922 'options' => $options,
12923 'hasMore' => $posts_count > $limit && $posts_count > $offset,
12924 ];
12925 }
12926
12927 /**
12928 * Advanced Ads pluggable data.
12929 *
12930 * @param array $data data.
12931 * @return array
12932 */
12933 public function search_ad_last_data( $data ) {
12934 $context = [];
12935 $args = [
12936 'post_type' => 'advanced_ads',
12937 'posts_per_page' => 1,
12938 'orderby' => 'modified',
12939 'order' => 'DESC',
12940 ];
12941
12942 if ( isset( $data['filter']['ad_status']['value'] ) ) {
12943 $post_status = $data['filter']['ad_status']['value'];
12944 $args['post_status'] = $post_status;
12945 }
12946
12947 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
12948 $post_status = $data['filter']['ad_new_status']['value'];
12949 $args['post_status'] = $post_status;
12950 }
12951
12952 if ( isset( $data['filter']['ad_id']['value'] ) ) {
12953 $post_id = $data['filter']['ad_id']['value'];
12954 if ( -1 != $post_id ) {
12955 if ( $post_id > 0 ) {
12956 $args['p'] = $post_id;
12957 }
12958 }
12959 }
12960
12961 $posts = get_posts( $args );
12962 if ( ! empty( $posts ) ) {
12963 $context['pluggable_data'] = $posts[0];
12964 $context['pluggable_data'] = WordPress::get_post_context( $posts[0]->ID );
12965 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
12966 $context['pluggable_data']['ad_new_status'] = $posts[0]->post_status;
12967 }
12968 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
12969 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
12970 }
12971 $context['pluggable_data']['ad_id'] = $posts[0]->ID;
12972 if ( isset( $data['filter']['ad_status']['value'] ) ) {
12973 $context['pluggable_data']['ad_status'] = $posts[0]->post_status;
12974 }
12975 $context['response_type'] = 'live';
12976 } else {
12977 $context['pluggable_data'] = [
12978 'ID' => 1,
12979 'post' => 1,
12980 'post_author' => 1,
12981 'post_date' => '2022-11-18 12:18:14',
12982 'post_date_gmt' => '2022-11-18 12:18:14',
12983 'post_content' => 'Ad Post Content',
12984 'post_title' => 'Ad Post',
12985 'post_excerpt' => '',
12986 'post_status' => 'draft',
12987 'comment_status' => 'open',
12988 'ping_status' => 'open',
12989 'post_password' => '',
12990 'post_name' => 'ad-post',
12991 'to_ping' => '',
12992 'pinged' => '',
12993 'post_modified' => '2022-11-18 12:18:14',
12994 'post_modified_gmt' => '2022-11-18 12:18:14',
12995 'post_content_filtered' => '',
12996 'post_parent' => 0,
12997 'guid' => 'https://example.com/ad-post/',
12998 'menu_order' => 0,
12999 'post_type' => 'advanced_ads',
13000 'post_mime_type' => '',
13001 'comment_count' => 0,
13002 'filter' => 'raw',
13003 ];
13004 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
13005 $context['pluggable_data']['ad_new_status'] = $data['filter']['ad_new_status']['value'];
13006 }
13007 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
13008 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
13009 }
13010 $context['pluggable_data']['ad_id'] = 1;
13011 if ( isset( $data['filter']['ad_status']['value'] ) ) {
13012 $context['pluggable_data']['ad_status'] = $data['filter']['ad_status']['value'];
13013 }
13014 $context['response_type'] = 'sample';
13015 }
13016
13017 return $context;
13018 }
13019
13020 /**
13021 * Get Newsletter lists
13022 *
13023 * @param array $data data.
13024 *
13025 * @return array
13026 */
13027 public function search_newsletter_lists( $data ) {
13028
13029 $options = [];
13030 if ( class_exists( '\Newsletter' ) ) {
13031
13032 $lists = \Newsletter::instance()->get_lists();
13033
13034 if ( ! empty( $lists ) ) {
13035 foreach ( $lists as $list ) {
13036 $options[] = [
13037 'label' => $list->name,
13038 'value' => 'list_' . $list->id,
13039 ];
13040 }
13041 }
13042 }
13043
13044 return [
13045 'options' => $options,
13046 'hasMore' => false,
13047 ];
13048 }
13049
13050 /**
13051 * Newsletter pluggable data.
13052 *
13053 * @param array $data data.
13054 * @return array
13055 */
13056 public function search_newsletter_last_data( $data ) {
13057 $context = [];
13058 global $wpdb;
13059
13060 $list = $data['filter']['list_id']['value'];
13061
13062 if ( -1 == $list ) {
13063 $log = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . "newsletter where status='C' ORDER BY id DESC LIMIT 1" );
13064 } else {
13065 $num = $list;
13066 $location = 1;
13067 $sql = 'SELECT * FROM ' . $wpdb->prefix . "newsletter WHERE $num = %d AND status = 'C' ORDER BY id DESC LIMIT 1";
13068 $log = $wpdb->get_results( $wpdb->prepare( $sql, $location ), ARRAY_A );// @phpcs:ignore
13069 }
13070
13071 if ( ! empty( $log ) ) {
13072 $lists_arr = get_option( 'newsletter_lists' );
13073 if ( -1 == $list ) {
13074 foreach ( $log[0] as $key => $val ) {
13075 if ( defined( 'NEWSLETTER_LIST_MAX' ) ) {
13076 for ( $i = 1; $i <= NEWSLETTER_LIST_MAX; $i++ ) {
13077 $list_key = "list_$i";
13078
13079 if ( $key == $list_key ) {
13080 if ( 1 == $val ) {
13081 $context['pluggable_data']['list_id'] = $key;
13082 if ( is_array( $lists_arr ) ) {
13083 if ( isset( $lists_arr[ $key ] ) ) {
13084 $list_name = $lists_arr[ $key ];
13085 $context['pluggable_data']['list_name'] = $list_name;
13086 }
13087 }
13088 continue;
13089 }
13090 }
13091 }
13092 }
13093 if ( 'email' == $key ) {
13094 $context['pluggable_data']['email'] = $val;
13095 }
13096 }
13097 } else {
13098 $context['pluggable_data']['list_id'] = $list;
13099 $context['pluggable_data']['email'] = $log[0]['email'];
13100 if ( is_array( $lists_arr ) ) {
13101 if ( isset( $lists_arr[ $list ] ) ) {
13102 $list_name = $lists_arr[ $list ];
13103 $context['pluggable_data']['list_name'] = $list_name;
13104 }
13105 }
13106 }
13107 $context['response_type'] = 'live';
13108 } else {
13109 $context = json_decode( '{"response_type":"sample","pluggable_data":{"list_id": "list_1","email": "johnd@mailinator.com","list_name": "Contact List"}}', true );
13110 }
13111
13112 return $context;
13113 }
13114
13115 /**
13116 * Get wpForo Forum list
13117 *
13118 * @param array $data data.
13119 *
13120 * @return array
13121 */
13122 public function search_wp_forum_list( $data ) {
13123
13124 if ( ! function_exists( 'WPF' ) ) {
13125 return [];
13126 }
13127
13128 $forums = WPF()->forum->get_forums( [ 'type' => 'forum' ] );
13129
13130 $options = [];
13131
13132 if ( ! empty( $forums ) ) {
13133 foreach ( $forums as $forum ) {
13134 $options[] = [
13135 'label' => $forum['title'],
13136 'value' => $forum['forumid'],
13137 ];
13138 }
13139 }
13140
13141 return [
13142 'options' => $options,
13143 'hasMore' => false,
13144 ];
13145 }
13146
13147 /**
13148 * Get wpForo Topic list
13149 *
13150 * @param array $data data.
13151 *
13152 * @return array
13153 */
13154 public function search_wp_topic_list( $data ) {
13155
13156 if ( ! function_exists( 'WPF' ) ) {
13157 return [];
13158 }
13159
13160 $forum_id = $data['dynamic'];
13161
13162 $topics = WPF()->topic->get_topics( [ 'forumid' => $forum_id ] );
13163
13164 $options = [];
13165
13166 if ( ! empty( $topics ) ) {
13167 foreach ( $topics as $topic ) {
13168 $options[] = [
13169 'label' => $topic['title'],
13170 'value' => $topic['topicid'],
13171 ];
13172 }
13173 }
13174
13175 return [
13176 'options' => $options,
13177 'hasMore' => false,
13178 ];
13179 }
13180
13181 /**
13182 * Get wpForo Groups list
13183 *
13184 * @param array $data data.
13185 *
13186 * @return array
13187 */
13188 public function search_wp_user_groups_list( $data ) {
13189
13190 if ( ! function_exists( 'WPF' ) ) {
13191 return [];
13192 }
13193
13194 $usergroups = WPF()->usergroup->get_usergroups();
13195
13196 $options = [];
13197
13198 if ( ! empty( $usergroups ) ) {
13199 foreach ( $usergroups as $group ) {
13200 $options[] = [
13201 'label' => $group['name'],
13202 'value' => intval( $group['groupid'] ),
13203 ];
13204 }
13205 }
13206
13207 return [
13208 'options' => $options,
13209 'hasMore' => false,
13210 ];
13211 }
13212
13213 /**
13214 * Get wpForo Reputation list
13215 *
13216 * @param array $data data.
13217 *
13218 * @return array
13219 */
13220 public function search_wp_foro_reputation_list( $data ) {
13221
13222 if ( ! function_exists( 'WPF' ) ) {
13223 return [];
13224 }
13225
13226 $levels = WPF()->member->levels();
13227 $options = [];
13228
13229 if ( ! empty( $levels ) ) {
13230 foreach ( $levels as $level ) {
13231 $options[] = [
13232 'label' => esc_attr__( 'Level', 'suretriggers' ) . ' ' . $level . ' - ' . WPF()->member->rating( $level, 'title' ),
13233 'value' => strval( $level ),
13234 ];
13235 }
13236 }
13237
13238 return [
13239 'options' => $options,
13240 'hasMore' => false,
13241 ];
13242 }
13243
13244 /**
13245 * WPForo new topic pluggable data.
13246 *
13247 * @param array $data data.
13248 * @return array
13249 */
13250 public function search_pluggables_wpforo_topic_last_data( $data ) {
13251 $context = [];
13252 global $wpdb;
13253
13254 $forum_id = $data['filter']['forum_id']['value'];
13255
13256 if ( -1 == $forum_id ) {
13257 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'wpforo_topics WHERE closed = 0 ORDER BY topicid DESC LIMIT 1', ARRAY_A );
13258 } else {
13259 $forum = $forum_id;
13260 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_topics WHERE forumid = %d AND closed = 0 ORDER BY topicid DESC LIMIT 1';
13261 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum ), ARRAY_A );// @phpcs:ignore
13262 }
13263
13264 if ( ! empty( $results ) ) {
13265 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
13266 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
13267
13268 if ( function_exists( 'WPF' ) ) {
13269 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
13270 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
13271 }
13272 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
13273 $context['response_type'] = 'live';
13274 } else {
13275 $context = json_decode( '{"response_type":"sample","pluggable_data":{"forum_id": "2","topic_id": "1","forum": {"forumid": "2","title": "Main Forum","slug": "main-forum","description": "This is a simple parent forum","parentid": "1","icon": "fas fa-comments","cover": 0,"cover_height": "150","last_topicid": "1","last_postid": "2","last_userid": "4","last_post_date": "2023-09-19 11:40:19","topics": "1","posts": "2","permissions": "a:5:{i:1;s:4:\"full\";i:2;s:9:\"moderator\";i:3;s:8:\"standard\";i:4;s:9:\"read_only\";i:5;s:8:\"standard\";}","meta_key": "","meta_desc": "","status": "1","is_cat": "0","layout": "4","order": "0","color": "#888888","url": "https:\/\/example.com\/community\/main-forum\/","cover_url": ""},"topic": {"topicid": "1","forumid": "2","first_postid": "1","userid": "4","title": "New Forum topic title","slug": "new-forum-topic-title","created": "2023-09-19 11:39:01","modified": "2023-09-19 11:40:19","last_post": "2","posts": "2","votes": "0","answers": "0","views": "1","meta_key": "","meta_desc": "","type": "0","solved": "0","closed": "0","has_attach": "0","private": "0","status": "0","name": "","email": "","prefix": "","tags": "","url": "https:\/\/example.com\/community\/main-forum\/new-forum-topic-title\/","full_url": "https:\/\/example.com\/community\/main-forum\/new-forum-topic-title\/","short_url": "https:\/\/example.com\/community\/topicid\/1\/"},"user": {"wp_user_id": 4,"user_login": "john@d.com","display_name": "john@d.com","user_firstname": "john","user_lastname": "d","user_email": "john@d.com","user_role": ["customer"]}}}', true );// @phpcs:ignore
13276 }
13277
13278 return $context;
13279 }
13280
13281 /**
13282 * WpForo Topic Reply pluggable data.
13283 *
13284 * @param array $data data.
13285 * @return array
13286 */
13287 public function search_pluggables_wpforo_topic_reply_last_data( $data ) {
13288 $context = [];
13289 global $wpdb;
13290
13291 $forum_id = $data['filter']['forum_id']['value'];
13292 $topic_id = $data['filter']['topic_id']['value'];
13293
13294 if ( -1 == $forum_id && -1 != $topic_id ) {
13295 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE topicid = %d ORDER BY postid DESC LIMIT 1';
13296 $results = $wpdb->get_results( $wpdb->prepare( $sql, $topic_id ), ARRAY_A );// @phpcs:ignore
13297 } elseif ( -1 != $forum_id && -1 == $topic_id ) {
13298 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d ORDER BY postid DESC LIMIT 1';
13299 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id ), ARRAY_A );// @phpcs:ignore
13300 } elseif ( -1 == $forum_id && -1 == $topic_id ) {
13301 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts ORDER BY postid DESC LIMIT 1';
13302 $results = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );// @phpcs:ignore
13303 } else {
13304 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d AND topicid = %d ORDER BY postid DESC LIMIT 1';
13305 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id, $topic_id ), ARRAY_A );// @phpcs:ignore
13306 }
13307
13308 if ( ! empty( $results ) ) {
13309 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
13310 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
13311 if ( function_exists( 'WPF' ) ) {
13312 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
13313 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
13314 $context['pluggable_data']['reply'] = WPF()->post->get_post( $results[0]['postid'] );
13315 }
13316 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
13317 $context['response_type'] = 'live';
13318 } else {
13319 $context = json_decode( '{"response_type":"sample","pluggable_data":{"forum_id": "2","topic_id": "1","forum": {"forumid": "2","title": "Main Forum","slug": "main-forum","description": "This is a simple parent forum","parentid": "1","icon": "fas fa-comments","cover": 0,"cover_height": "150","last_topicid": "1","last_postid": "2","last_userid": "4","last_post_date": "2023-09-19 11:40:19","topics": "1","posts": "2","permissions": "a:5:{i:1;s:4:\"full\";i:2;s:9:\"moderator\";i:3;s:8:\"standard\";i:4;s:9:\"read_only\";i:5;s:8:\"standard\";}","meta_key": "","meta_desc": "","status": "1","is_cat": "0","layout": "4","order": "0","color": "#888888","url": "https:\/\/example.com\/community\/main-forum\/","cover_url": ""},"topic": {"topicid": "1","forumid": "2","first_postid": "1","userid": "4","title": "New Forum topic title","slug": "new-forum-topic-title","created": "2023-09-19 11:39:01","modified": "2023-09-19 11:40:19","last_post": "2","posts": "2","votes": "0","answers": "0","views": "1","meta_key": "","meta_desc": "","type": "0","solved": "0","closed": "0","has_attach": "0","private": "0","status": "0","name": "","email": "","prefix": "","tags": "","url": "https:\/\/example.com\/community\/main-forum\/new-forum-topic-title\/","full_url": "https:\/\/example.com\/community\/main-forum\/new-forum-topic-title\/","short_url": "https:\/\/example.com\/community\/topicid\/1\/"},"reply_url": "https:\/\/example.com\/community\/main-forum\/new-forum-topic-title\/#post-2","reply": {"postid": "2","parentid": "0","forumid": "2","topicid": "1","userid": 4,"title": "RE: New Forum topic title","body": "<p>new reply<\/p>","created": "2023-09-19 11:40:19","modified": "2023-09-19 11:40:19","likes": "0","votes": "0","is_answer": "0","is_first_post": "0","status": "0","name": "","email": "","private": "0","root": "-1","url": "https:\/\/example.com\/community\/main-forum\/new-forum-topic-title\/#post-2","full_url": "https:\/\/example.com\/community\/main-forum\/new-forum-topic-title\/#post-2","short_url": "https:\/\/example.com\/community\/postid\/2\/"},"user": {"wp_user_id": 4,"user_login": "john@d.com","display_name": "john@d.com","user_firstname": "john","user_lastname": "d","user_email": "john@d.com","user_role": ["customer"]}}}', true );// @phpcs:ignore
13320 }
13321
13322 return $context;
13323 }
13324
13325 /**
13326 * Get RafflePress Giveaways list
13327 *
13328 * @param array $data data.
13329 *
13330 * @return array
13331 */
13332 public function search_rp_giveaways_list( $data ) {
13333
13334 global $wpdb;
13335 $options = [];
13336
13337 $giveaways = $wpdb->get_results( "SELECT id,name FROM {$wpdb->prefix}rafflepress_giveaways WHERE deleted_at is null ORDER BY name ASC", ARRAY_A );
13338 foreach ( $giveaways as $giveaway ) {
13339 $options[] = [
13340 'label' => $giveaway['name'],
13341 'value' => $giveaway['id'],
13342 ];
13343 }
13344
13345 return [
13346 'options' => $options,
13347 'hasMore' => false,
13348 ];
13349 }
13350
13351 /**
13352 * Get RafflePress Giveaway Actions list
13353 *
13354 * @param array $data data.
13355 *
13356 * @return array
13357 */
13358 public function search_rp_giveaway_actions_list( $data ) {
13359
13360 global $wpdb;
13361 $options = [];
13362
13363 $giveaway = $wpdb->get_row( $wpdb->prepare( "SELECT settings FROM {$wpdb->prefix}rafflepress_giveaways WHERE id=%d", $data['dynamic'] ), ARRAY_A );
13364
13365 if ( is_array( $giveaway ) && isset( $giveaway['settings'] ) ) {
13366 $settings = json_decode( $giveaway['settings'], true );
13367
13368 if ( is_array( $settings ) && isset( $settings['entry_options'] ) ) {
13369 foreach ( $settings['entry_options'] as $action ) {
13370 $options[] = [
13371 'label' => $action['name'],
13372 'value' => $action['id'],
13373 ];
13374 }
13375 }
13376 }
13377
13378 return [
13379 'options' => $options,
13380 'hasMore' => false,
13381 ];
13382 }
13383
13384 /**
13385 * Get last data for trigger.
13386 *
13387 * @param array $data data.
13388 * @return array
13389 */
13390 public function search_raffle_press_triggers_last_data( $data ) {
13391
13392 global $wpdb;
13393
13394 $context = [];
13395 $context['response_type'] = 'sample';
13396
13397 $pluggable_data = [
13398 'giveaway_id' => 1,
13399 'giveaway_title' => 'Sample Giveaway (ID#1)',
13400 'giveaway_start_date' => 'September 20, 2023',
13401 'giveaway_end_date' => 'October 6, 2023',
13402 'giveaway_entries' => 9,
13403 'giveaway_user_count' => 3,
13404 'giveaway_status' => 'Active',
13405 'contestant_name' => 'John Doe',
13406 'contestant_email' => 'john_doe@gmail.com',
13407 'contestant_email_verified' => 'Yes',
13408 'action_id' => '0jnex',
13409 'action_name' => 'Visit us on Instagram',
13410 ];
13411
13412 $giveaway_id = isset( $data['filter']['giveaway_id'] ) ? $data['filter']['giveaway_id']['value'] : null;
13413 $action_id = isset( $data['filter']['action_id'] ) ? $data['filter']['action_id']['value'] : null;
13414
13415 $query = "SELECT contestant_id, giveaway_id, action_id, meta FROM {$wpdb->prefix}rafflepress_entries";
13416
13417 if ( $giveaway_id && -1 != $giveaway_id ) {
13418 $query .= ' WHERE giveaway_id = ' . $giveaway_id;
13419
13420 if ( $action_id ) {
13421 $query .= " AND action_id = '" . $action_id . "'";
13422 }
13423 }
13424
13425 $query .= ' ORDER BY created_at DESC LIMIT 1';
13426
13427 $giveaway_data = $wpdb->get_row( $query, ARRAY_A ); // @phpcs:ignore
13428
13429 if ( ! empty( $giveaway_data ) ) {
13430 $pluggable_data = array_merge(
13431 RafflePress::get_giveaway_context( $giveaway_data['giveaway_id'] ),
13432 RafflePress::get_contestant_context( $giveaway_data['contestant_id'] )
13433 );
13434
13435 $pluggable_data['performed_action_id'] = isset( $giveaway_data['action_id'] ) ? $giveaway_data['action_id'] : 0;
13436
13437 $giveaway_meta = isset( $giveaway_data['meta'] ) ? json_decode( $giveaway_data['meta'], true ) : [];
13438 $pluggable_data['performed_action_name'] = is_array( $giveaway_meta ) && isset( $giveaway_meta['action'] ) ? $giveaway_meta['action'] : '';
13439
13440 $context['response_type'] = 'live';
13441 }
13442
13443 $context['pluggable_data'] = $pluggable_data;
13444
13445 return $context;
13446 }
13447
13448 /**
13449 * Get last data for trigger
13450 *
13451 * @param array $data data.
13452 * @return array
13453 */
13454 public function search_woo_commerce_shipstation_triggers_last_data( $data ) {
13455 $context = [];
13456 $context['response_type'] = 'sample';
13457 $context['pluggable_data'] = [];
13458
13459 $order_sample_data = json_decode( '{"product_id": 47,"id": 49,"parent_id": 0,"status": "completed","currency": "USD","version": "8.1.1","prices_include_tax": false,"date_created": {"date": "2023-09-23 10:28:00.000000","timezone_type": 1,"timezone": "+00:00"},"date_modified": {"date": "2023-09-23 10:29:55.000000","timezone_type": 1,"timezone": "+00:00"},"discount_total": "0","discount_tax": "0", "shipping_total": "0","shipping_tax": "0","cart_tax": "0","total": "1.00","total_tax": "0","customer_id": 1,"order_key": "wc_order_64IaGkeQKRXdm","billing": {"first_name": "john","last_name": "d","company": "","address_1": "123 Main Street","address_2": "","city": "London","state": "Greater London","postcode": "SW1A 1AA","country": "GB","email": "johnd@d.com","phone": "9878988766"},"shipping": {"first_name": "","last_name": "","company": "","address_1": "","address_2": "","city": "","state": "","postcode": "","country": "","phone": ""},"payment_method": "cod","payment_method_title": "Cash on delivery","transaction_id": "","customer_ip_address": "182.184.87.226","customer_user_agent": "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/116.0.0.0 Safari\/537.36","created_via": "checkout","customer_note": "", "date_completed": { "date": "2023-09-23 10:29:55.000000", "timezone_type": 1, "timezone": "+00:00" }, "date_paid": { "date": "2023-09-23 10:29:55.000000", "timezone_type": 1, "timezone": "+00:00" }, "cart_hash": "ac073abcdc9a52025211d0fad52cfa46", "order_stock_reduced": true, "download_permissions_granted": true, "new_order_email_sent": true, "recorded_sales": true, "recorded_coupon_usage_counts": true, "number": "49", "meta_data": [{ "id": 1206, "key":"is_vat_exempt", "value": "no"},{"id": 1207,"key": "weglot_language","value": "en"},{"id": 1208,"key": "_shipstation_exported","value": "yes"},{"id": 1209,"key": "_shipstation_shipped_item_count","value": "1"}],"line_items": {"id": "25","order_id": "49","name": "New Product","product_id": "47","variation_id": "0","quantity": "1","tax_class": "","subtotal": "1","subtotal_tax": "0","total": "1","total_tax": "0","taxes": "","meta_data": []},"tax_lines": [],"shipping_lines": [],"fee_lines": [],"coupon_lines": [],"coupons": [],"products": [{"id": 25,"order_id": 49,"name": "New Product","product_id": 47,"variation_id": 0,"quantity": 1,"tax_class": "","subtotal": "1","subtotal_tax": "0","total": "1","total_tax": "0","taxes": {"total": [],"subtotal": []},"meta_data": []}],"quantity": "1","wp_user_id": 1,"user_login": "johnd","display_name": "johnd","user_firstname": "john","user_lastname": "d","user_email": "johnd@d.com","user_role": ["administrator"],"shipping_tracking_number": "","shipping_carrier": "","ship_date": "",}', true ); //phpcs:ignore
13460
13461 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
13462 $condition = $data['filter']['condition_compare']['value'];
13463 $price = $data['filter']['price']['value'];
13464 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
13465
13466 if ( 'order_shipped' === $term ) {
13467 $orders = wc_get_orders(
13468 [
13469 'numberposts' => 1,
13470 'orderby' => 'date',
13471 'order' => 'DESC',
13472 'status' => 'completed',
13473 'meta_query' => [
13474 [
13475 'key' => '_shipstation_shipped_item_count',
13476 'compare' => 'EXISTS',
13477 ],
13478 ],
13479 ]
13480 );
13481 if ( count( $orders ) > 0 ) {
13482 $order_id = $orders[0]->get_id();
13483 $order = wc_get_order( $order_id );
13484 $user_id = $order->get_customer_id();
13485 $product_ids = [];
13486 if ( $order ) {
13487 $items = $order->get_items();
13488 foreach ( $items as $item ) {
13489 if ( method_exists( $item, 'get_product_id' ) ) {
13490 $product_ids[] = $item->get_product_id();
13491 }
13492 }
13493
13494 foreach ( $product_ids as $product_id ) {
13495 $context['product_id'] = $product_id;
13496 }
13497 }
13498 $context = array_merge(
13499 WooCommerce::get_order_context( $order_id ),
13500 WordPress::get_user_context( $user_id )
13501 );
13502 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
13503 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
13504 /**
13505 *
13506 * Ignore line
13507 *
13508 * @phpstan-ignore-next-line
13509 */
13510 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
13511 /**
13512 *
13513 * Ignore line
13514 *
13515 * @phpstan-ignore-next-line
13516 */
13517 $date = date_i18n( get_option( 'date_format' ), $timestamp );
13518 $context['ship_date'] = $date;
13519 $context['response_type'] = 'live';
13520 }
13521
13522 $context['pluggable_data'] = $context;
13523 } elseif ( 'specific_product_order_shipped' === $term ) {
13524 if ( -1 != $product_id ) {
13525 $product_ids = [ $product_id ];
13526 $orders = wc_get_orders(
13527 [
13528 'numberposts' => 1,
13529 'orderby' => 'date',
13530 'order' => 'DESC',
13531 'status' => 'completed',
13532 'meta_query' => [
13533 [
13534 'key' => '_shipstation_shipped_item_count',
13535 'compare' => 'EXISTS',
13536 ],
13537 [
13538 'key' => '_product_id',
13539 'value' => $product_ids,
13540 'compare' => 'IN',
13541 ],
13542 ],
13543 ]
13544 );
13545 } else {
13546 $orders = wc_get_orders(
13547 [
13548 'numberposts' => 1,
13549 'orderby' => 'date',
13550 'order' => 'DESC',
13551 'status' => 'completed',
13552 'meta_query' => [
13553 [
13554 'key' => '_shipstation_shipped_item_count',
13555 'compare' => 'EXISTS',
13556 ],
13557 ],
13558 ]
13559 );
13560 }
13561 if ( count( $orders ) > 0 ) {
13562 $order_id = $orders[0]->get_id();
13563 $order = wc_get_order( $order_id );
13564 $user_id = $order->get_customer_id();
13565 $productids = [];
13566 $items = $order->get_items();
13567 foreach ( $items as $item ) {
13568 if ( method_exists( $item, 'get_product_id' ) ) {
13569 $productids[] = $item->get_product_id();
13570 }
13571 }
13572 $context = array_merge(
13573 WooCommerce::get_order_context( $order_id ),
13574 WordPress::get_user_context( $user_id )
13575 );
13576
13577 foreach ( $productids as $product_id ) {
13578 $context['product_id'] = $product_id;
13579 }
13580
13581 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
13582 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
13583 /**
13584 *
13585 * Ignore line
13586 *
13587 * @phpstan-ignore-next-line
13588 */
13589 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
13590 /**
13591 *
13592 * Ignore line
13593 *
13594 * @phpstan-ignore-next-line
13595 */
13596 $date = date_i18n( get_option( 'date_format' ), $timestamp );
13597 $context['ship_date'] = $date;
13598 $context['response_type'] = 'live';
13599 }
13600
13601 $context['pluggable_data'] = $context;
13602 } elseif ( 'specific_amount_order_shipped' === $term ) {
13603 $orders = wc_get_orders(
13604 [
13605 'numberposts' => 1,
13606 'orderby' => 'date',
13607 'order' => 'DESC',
13608 'status' => 'completed',
13609 'meta_query' => [
13610 [
13611 'key' => '_shipstation_shipped_item_count',
13612 'compare' => 'EXISTS',
13613 ],
13614 [
13615 'key' => '_order_total',
13616 'value' => $price,
13617 'compare' => $condition,
13618 ],
13619 ],
13620 ]
13621 );
13622 if ( count( $orders ) > 0 ) {
13623 $order_id = $orders[0]->get_id();
13624 $order = wc_get_order( $order_id );
13625 $user_id = $order->get_customer_id();
13626 $product_ids = [];
13627 $items = $order->get_items();
13628 foreach ( $items as $item ) {
13629 if ( method_exists( $item, 'get_product_id' ) ) {
13630 $product_ids[] = $item->get_product_id();
13631 }
13632 }
13633 $context = array_merge(
13634 WooCommerce::get_order_context( $order_id ),
13635 WordPress::get_user_context( $user_id )
13636 );
13637
13638 foreach ( $product_ids as $product_id ) {
13639 $context['product_id'] = $product_id;
13640 }
13641
13642 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
13643 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
13644 /**
13645 *
13646 * Ignore line
13647 *
13648 * @phpstan-ignore-next-line
13649 */
13650 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
13651 /**
13652 *
13653 * Ignore line
13654 *
13655 * @phpstan-ignore-next-line
13656 */
13657 $date = date_i18n( get_option( 'date_format' ), $timestamp );
13658 $context['ship_date'] = $date;
13659 $context['response_type'] = 'live';
13660 }
13661
13662 $context['pluggable_data'] = $context;
13663 }
13664
13665 return $context;
13666 }
13667
13668 /**
13669 * Get GroundHogg Tag list
13670 *
13671 * @param array $data data.
13672 *
13673 * @return array
13674 */
13675 public function search_groundhogg_tag_list( $data ) {
13676
13677 if ( ! function_exists( 'Groundhogg\get_db' ) ) {
13678 return [];
13679 }
13680
13681 $tags = \Groundhogg\get_db( 'tags' )->query( [] );
13682 $options = [];
13683
13684 if ( ! empty( $tags ) ) {
13685 foreach ( $tags as $tag ) {
13686 $options[] = [
13687 'label' => $tag->tag_name,
13688 'value' => $tag->tag_id,
13689 ];
13690 }
13691 }
13692
13693 return [
13694 'options' => $options,
13695 'hasMore' => false,
13696 ];
13697 }
13698
13699 /**
13700 * Get WP courseware courses list
13701 *
13702 * @param array $data data.
13703 *
13704 * @return array
13705 */
13706 public function search_wpcw_courses( $data ) {
13707 $options = [];
13708
13709 $page = $data['page'];
13710 $limit = Utilities::get_search_page_limit();
13711 $offset = $limit * ( $page - 1 );
13712
13713 $args = [
13714 'post_type' => 'wpcw_course',
13715 'posts_per_page' => $limit,
13716 'offset' => $offset,
13717 'orderby' => 'title',
13718 'order' => 'ASC',
13719 'post_status' => 'publish',
13720 ];
13721
13722 $courses = get_posts( $args );
13723
13724 $course_count = wp_count_posts( 'wpcw_course' )->publish;
13725
13726 if ( ! empty( $courses ) ) {
13727 if ( is_array( $courses ) ) {
13728 foreach ( $courses as $course ) {
13729 $options[] = [
13730 'label' => $course->post_title,
13731 'value' => $course->ID,
13732 ];
13733 }
13734 }
13735 }
13736 return [
13737 'options' => $options,
13738 'hasMore' => $course_count > $limit && $course_count > $offset,
13739 ];
13740 }
13741
13742 /**
13743 * Get WP courseware courses list
13744 *
13745 * @param array $data data.
13746 *
13747 * @return array
13748 */
13749 public function search_wpcw_modules( $data ) {
13750 $options = [];
13751
13752 if ( function_exists( 'wpcw_get_modules' ) ) {
13753 $modules = wpcw_get_modules();
13754 }
13755
13756 if ( ! empty( $modules ) ) {
13757 if ( is_array( $modules ) ) {
13758 foreach ( $modules as $module ) {
13759 $options[] = [
13760 'label' => $module->module_title,
13761 'value' => $module->module_id,
13762 ];
13763 }
13764 }
13765 }
13766 return [
13767 'options' => $options,
13768 'hasMore' => false,
13769 ];
13770 }
13771
13772 /**
13773 * Get WP courseware unit list
13774 *
13775 * @param array $data data.
13776 *
13777 * @return array
13778 */
13779 public function search_wpcw_units( $data ) {
13780 $options = [];
13781
13782 $page = $data['page'];
13783 $limit = Utilities::get_search_page_limit();
13784 $offset = $limit * ( $page - 1 );
13785
13786 $args = [
13787 'post_type' => 'course_unit',
13788 'posts_per_page' => $limit,
13789 'offset' => $offset,
13790 'orderby' => 'title',
13791 'order' => 'ASC',
13792 'post_status' => 'publish',
13793 ];
13794
13795 $units = get_posts( $args );
13796
13797 $unit_count = wp_count_posts( 'course_unit' )->publish;
13798
13799 if ( ! empty( $units ) ) {
13800 if ( is_array( $units ) ) {
13801 foreach ( $units as $unit ) {
13802 $options[] = [
13803 'label' => $unit->post_title,
13804 'value' => $unit->ID,
13805 ];
13806 }
13807 }
13808 }
13809 return [
13810 'options' => $options,
13811 'hasMore' => $unit_count > $limit && $unit_count > $offset,
13812 ];
13813 }
13814
13815 /**
13816 * Search WP Courseware data.
13817 *
13818 * @param array $data data.
13819 * @return array|void
13820 */
13821 public function search_wpcw_last_data( $data ) {
13822 global $wpdb;
13823 $post_type = $data['post_type'];
13824 $trigger = $data['search_term'];
13825 $context = [];
13826
13827 if ( 'wpcw_course_completed' === $trigger ) {
13828 $post_id = $data['filter']['course_post_id']['value'];
13829 if ( -1 === $post_id ) {
13830 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpcw_user_courses as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.course_id WHERE postmeta.course_progress=100 order by postmeta.user_id DESC LIMIT 1" );
13831 } else {
13832 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wpcw_user_courses as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.course_id WHERE postmeta.course_id = %s AND postmeta.course_progress=100 order by postmeta.user_id DESC LIMIT 1", $post_id ) );
13833 }
13834 } elseif ( 'wpcw_module_completed' === $trigger ) {
13835 $post_id = $data['filter']['module_id']['value'];
13836 if ( -1 === $post_id ) {
13837 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpcw_user_courses as postmeta JOIN {$wpdb->prefix}wpcw_modules as posts ON posts.parent_course_id=postmeta.course_id WHERE postmeta.course_progress>=0 order by postmeta.course_enrolment_date DESC LIMIT 1" );
13838 } else {
13839 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wpcw_user_courses as postmeta JOIN {$wpdb->prefix}wpcw_modules as posts ON posts.parent_course_id=postmeta.course_id WHERE postmeta.course_progress>=0 AND posts.module_id=%s order by postmeta.course_enrolment_date DESC LIMIT 1", $post_id ) );
13840 }
13841 } elseif ( 'wpcw_unit_completed' === $trigger ) {
13842 $post_id = $data['filter']['unit_id']['value'];
13843 if ( -1 === $post_id ) {
13844 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpcw_user_progress WHERE unit_completed_status='complete' order by unit_id DESC LIMIT 1" );
13845 } else {
13846 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wpcw_user_progress WHERE unit_id=%d AND unit_completed_status='complete' order by unit_id DESC LIMIT 1", $post_id ) );
13847 }
13848 } elseif ( 'wpcw_enroll_course' === $trigger ) {
13849 $post_id = $data['filter']['course_post_id']['value'];
13850 if ( -1 === $post_id ) {
13851 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpcw_user_courses as postmeta JOIN {$wpdb->prefix}wpcw_courses as posts ON posts.course_id=postmeta.course_id order by course_enrolment_date DESC LIMIT 1" );
13852 } else {
13853 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wpcw_user_courses as postmeta JOIN {$wpdb->prefix}wpcw_courses as posts ON posts.course_id=postmeta.course_id WHERE posts.course_post_id=%d order by course_enrolment_date DESC LIMIT 1", $post_id ) );
13854 }
13855 }
13856
13857 if ( ! empty( $result ) ) {
13858
13859 switch ( $trigger ) {
13860 case 'wpcw_course_completed':
13861 $result_course_id = $result[0]->course_id;
13862 $result_user_id = $result[0]->user_id;
13863 if ( function_exists( 'wpcw_get_course' ) ) {
13864 $course = wpcw_get_course( $result_course_id );
13865 if ( is_object( $course ) ) {
13866 $course = get_object_vars( $course );
13867 }
13868 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course );
13869 }
13870 break;
13871 case 'wpcw_module_completed':
13872 $result_module_id = $result[0]->module_id;
13873 $result_user_id = $result[0]->user_id;
13874 if ( function_exists( 'wpcw_get_module' ) ) {
13875 $module = wpcw_get_module( $result_module_id );
13876 if ( is_object( $module ) ) {
13877 $module = get_object_vars( $module );
13878 }
13879 $context = array_merge( WordPress::get_user_context( $result_user_id ), $module );
13880 }
13881 break;
13882 case 'wpcw_unit_completed':
13883 $result_unit_id = $result[0]->unit_id;
13884 $result_user_id = $result[0]->user_id;
13885 if ( function_exists( 'wpcw_get_unit' ) ) {
13886 $unit = wpcw_get_unit( $result_unit_id );
13887 if ( is_object( $unit ) ) {
13888 $unit = get_object_vars( $unit );
13889 $unit['name'] = get_the_title( $result_unit_id );
13890 }
13891 $context = array_merge( WordPress::get_user_context( $result_user_id ), $unit );
13892 }
13893 break;
13894 case 'wpcw_enroll_course':
13895 $result_course_id = $result[0]->course_id;
13896 $result_user_id = $result[0]->user_id;
13897 if ( function_exists( 'WPCW_courses_getCourseDetails' ) ) {
13898 $course_detail = WPCW_courses_getCourseDetails( $result_course_id );
13899 if ( is_object( $course_detail ) ) {
13900 $course_detail = get_object_vars( $course_detail );
13901 }
13902 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course_detail );
13903 }
13904 break;
13905 default:
13906 return;
13907 }
13908 $context['pluggable_data'] = $context;
13909 $context['response_type'] = 'live';
13910 }
13911
13912 return $context;
13913
13914 }
13915
13916 /**
13917 * Get WooCommerce Order Note list.
13918 *
13919 * @param array $data data.
13920 *
13921 * @return array
13922 */
13923 public function search_note_type_list( $data ) {
13924
13925 $options = [];
13926
13927 $options[] = [
13928 'label' => 'Customer',
13929 'value' => 'customer',
13930 ];
13931 $options[] = [
13932 'label' => 'Private',
13933 'value' => 'internal',
13934 ];
13935
13936 return [
13937 'options' => $options,
13938 'hasMore' => false,
13939 ];
13940 }
13941
13942 /**
13943 * Get last data for trigger
13944 *
13945 * @param array $data data.
13946 * @return array
13947 */
13948 public function search_woo_commerce_customers_triggers_last_data( $data ) {
13949 $context = [];
13950 $context['response_type'] = 'sample';
13951 $context['pluggable_data'] = [];
13952 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
13953 global $wpdb;
13954 if ( 'customer_created' === $term ) {
13955 $customer_query = get_users(
13956 [
13957 'fields' => 'ID',
13958 'role' => 'customer',
13959 'orderby' => 'ID',
13960 'order' => 'DESC',
13961 'number' => 1,
13962 ]
13963 );
13964 $results = $customer_query;
13965 if ( ! empty( $results ) ) {
13966 $customer = new WC_Customer( $results[0] );
13967 $last_order = $customer->get_last_order();
13968 $customer_data = [
13969 'id' => $customer->get_id(),
13970 'email' => $customer->get_email(),
13971 'first_name' => $customer->get_first_name(),
13972 'last_name' => $customer->get_last_name(),
13973 'username' => $customer->get_username(),
13974 'last_order_id' => is_object( $last_order ) ? $last_order->get_id() : null,
13975 'orders_count' => $customer->get_order_count(),
13976 'total_spent' => wc_format_decimal( $customer->get_total_spent(), 2 ),
13977 'avatar_url' => $customer->get_avatar_url(),
13978 'billing_address' => [
13979 'first_name' => $customer->get_billing_first_name(),
13980 'last_name' => $customer->get_billing_last_name(),
13981 'company' => $customer->get_billing_company(),
13982 'address_1' => $customer->get_billing_address_1(),
13983 'address_2' => $customer->get_billing_address_2(),
13984 'city' => $customer->get_billing_city(),
13985 'state' => $customer->get_billing_state(),
13986 'postcode' => $customer->get_billing_postcode(),
13987 'country' => $customer->get_billing_country(),
13988 'email' => $customer->get_billing_email(),
13989 'phone' => $customer->get_billing_phone(),
13990 ],
13991 'shipping_address' => [
13992 'first_name' => $customer->get_shipping_first_name(),
13993 'last_name' => $customer->get_shipping_last_name(),
13994 'company' => $customer->get_shipping_company(),
13995 'address_1' => $customer->get_shipping_address_1(),
13996 'address_2' => $customer->get_shipping_address_2(),
13997 'city' => $customer->get_shipping_city(),
13998 'state' => $customer->get_shipping_state(),
13999 'postcode' => $customer->get_shipping_postcode(),
14000 'country' => $customer->get_shipping_country(),
14001 ],
14002 ];
14003 if ( is_object( $last_order ) && method_exists( $last_order, 'get_date_created' ) ) {
14004 $created_date = $last_order->get_date_created();
14005 if ( is_object( $created_date ) && method_exists( $created_date, 'getTimestamp' ) ) {
14006 $last_order_date = $created_date->getTimestamp();
14007 $customer_data['created_at'] = $last_order_date;
14008 $customer_data['last_order_date'] = $last_order_date;
14009 }
14010 }
14011 $order_sample_data = $customer_data;
14012 $context['response_type'] = 'live';
14013 $context['pluggable_data'] = $order_sample_data;
14014 } else {
14015 $order_sample_data = json_decode( '{"id": 158,"email": "johnd@d.com","first_name": "john","last_name": "d","username": "johnd","last_order_id": 6604,"orders_count": 3,"total_spent": "45.00","avatar_url": "https:\/\/secure.gravatar.com\/avatar\/0f9c8dc78cff3ea4d447b2297c8f6803?s=96&d=mm&r=g","billing_address": {"first_name": "john","last_name": "d","company": "","address_1": "","address_2": "","city": "London","state": "TN","postcode": "PV1 QW2","country": "UK","email": "johnd@d.com","phone": "9878988766"},"shipping_address": {"first_name": "","last_name": "","company": "","address_1": "","address_2": "","city": "","state": "","postcode": "","country": ""},"created_at": 1697631243,"last_order_date": 1697631243}', true );
14016 $context['pluggable_data'] = $order_sample_data;
14017 }
14018 } elseif ( 'total_spend_reach' === $term ) {
14019 $total_spend_selected = isset( $data['filter']['total_spend']['value'] ) ? $data['filter']['total_spend']['value'] : '';
14020 $customers = $wpdb->get_col(
14021 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
14022 WHERE meta_key = '_customer_user' AND meta_value > 0"
14023 );
14024 $target_customer = null;
14025 foreach ( $customers as $customer ) {
14026 $total_spend = (int) wc_get_customer_total_spent( $customer );
14027 if ( $total_spend == $total_spend_selected ) {
14028 $target_customer = $customer;
14029 break; // Exit the loop once a matching customer is found.
14030 }
14031 }
14032 if ( $target_customer ) {
14033 $new_customer = new WC_Customer( $target_customer );
14034 $new_customer_data = [
14035 'id' => $new_customer->get_id(),
14036 'email' => $new_customer->get_email(),
14037 'first_name' => $new_customer->get_first_name(),
14038 'last_name' => $new_customer->get_last_name(),
14039 'username' => $new_customer->get_username(),
14040 'order_count' => $new_customer->get_order_count(),
14041 'total_spend' => wc_format_decimal( $new_customer->get_total_spent(), 2 ),
14042 ];
14043 $context['response_type'] = 'live';
14044 $context['pluggable_data'] = $new_customer_data;
14045 } else {
14046 $sample_customer_data = [
14047 'id' => '101',
14048 'created_at' => '1680675247',
14049 'email' => 'john@d.com',
14050 'first_name' => 'John',
14051 'last_name' => 'D',
14052 'username' => 'johnd',
14053 'order_count' => '3',
14054 'total_spend' => '22.00',
14055 ];
14056 $context['pluggable_data'] = $sample_customer_data;
14057 }
14058 } elseif ( 'order_count_reach' === $term ) {
14059 $total_order_selected = isset( $data['filter']['order_count']['value'] ) ? $data['filter']['order_count']['value'] : '';
14060 $customers = $wpdb->get_col(
14061 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
14062 WHERE meta_key = '_customer_user' AND meta_value > 0"
14063 );
14064 $target_customer = null;
14065 foreach ( $customers as $customer ) {
14066 $args = [
14067 'customer_id' => $customer,
14068 'limit' => -1,
14069 'status' => [ 'wc-completed' ],
14070 ];
14071 $orders = wc_get_orders( $args );
14072 if ( ! empty( $orders ) ) {
14073 $total_order = (int) wc_get_customer_order_count( $customer );
14074 if ( $total_order == $total_order_selected ) {
14075 $target_customer = $customer;
14076 break; // Exit the loop once a matching customer is found.
14077 }
14078 }
14079 }
14080 if ( $target_customer ) {
14081 $new_customer = new WC_Customer( $target_customer );
14082 $new_customer_data = [
14083 'id' => $new_customer->get_id(),
14084 'email' => $new_customer->get_email(),
14085 'first_name' => $new_customer->get_first_name(),
14086 'last_name' => $new_customer->get_last_name(),
14087 'username' => $new_customer->get_username(),
14088 'order_count' => $new_customer->get_order_count(),
14089 ];
14090 $context['response_type'] = 'live';
14091 $context['pluggable_data'] = $new_customer_data;
14092 } else {
14093 $sample_customer_data = [
14094 'id' => '101',
14095 'created_at' => '1680675247',
14096 'email' => 'john@d.com',
14097 'first_name' => 'John',
14098 'last_name' => 'D',
14099 'username' => 'johnd',
14100 'orders_count' => '3',
14101 ];
14102 $context['pluggable_data'] = $sample_customer_data;
14103 }
14104 }
14105
14106 return $context;
14107 }
14108
14109 /**
14110 * Get Affiliate list
14111 *
14112 * @param array $data data.
14113 *
14114 * @return array
14115 */
14116 public function search_affiliate_list( $data ) {
14117 $options = [];
14118
14119 $args = [
14120 'meta_query' => [
14121 [
14122 'key' => 'wafp_is_affiliate',
14123 'value' => '1',
14124 'compare' => '=',
14125 ],
14126 ],
14127 ];
14128
14129 $affiliates = get_users( $args );
14130
14131 foreach ( $affiliates as $user ) {
14132 $options[] = [
14133 'label' => $user->display_name,
14134 'value' => $user->ID,
14135 ];
14136 }
14137 return [
14138 'options' => $options,
14139 'hasMore' => false,
14140 ];
14141 }
14142
14143 /**
14144 * Get Affiliate Source list
14145 *
14146 * @param array $data data.
14147 *
14148 * @return array
14149 */
14150 public function search_affiliate_transaction_source_list( $data ) {
14151 $options = [];
14152
14153 $sources = [
14154 'General',
14155 'MemberPress',
14156 'WooCommerce',
14157 'Easy Digital Downloads',
14158 'WPForms',
14159 'Formidable',
14160 'PayPal',
14161 ];
14162 foreach ( $sources as $source ) {
14163 $options[] = [
14164 'label' => $source,
14165 'value' => str_replace( ' ', '_', strtolower( $source ) ),
14166 ];
14167 }
14168
14169 return [
14170 'options' => $options,
14171 'hasMore' => false,
14172 ];
14173 }
14174
14175 /**
14176 * Search Easy Affiliate data.
14177 *
14178 * @param array $data data.
14179 * @return array|void
14180 */
14181 public function search_easy_affiliate_last_data( $data ) {
14182
14183 global $wpdb;
14184 $trigger = $data['search_term'];
14185 $context = [];
14186
14187 if ( 'sale_recorded' === $trigger ) {
14188 $affiliate_id = $data['filter']['affiliate_id']['value'];
14189 if ( -1 === $affiliate_id ) {
14190 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wafp_transactions as postmeta JOIN {$wpdb->prefix}wafp_events as posts ON posts.evt_id=postmeta.id WHERE postmeta.status='complete' order by postmeta.id DESC LIMIT 1" );
14191 } else {
14192 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wafp_transactions as postmeta JOIN {$wpdb->prefix}wafp_events as posts ON posts.evt_id=postmeta.id WHERE postmeta.status='complete' AND affiliate_id=%d order by postmeta.id DESC LIMIT 1", $affiliate_id ) );
14193 }
14194 } elseif ( 'sale_added' === $trigger ) {
14195 $orderby = 'signup_date';
14196 $order = 'DESC';
14197 $paged = 1;
14198 $search = '';
14199 $perpage = 1;
14200 /**
14201 *
14202 * Ignore line
14203 *
14204 * @phpstan-ignore-next-line
14205 */
14206 $result = \EasyAffiliate\Models\User::affiliate_list_table( $orderby, $order, $paged, $search, $perpage );
14207 if ( empty( $result['results'] ) ) {
14208 $result = [];
14209 }
14210 } elseif ( 'payout_made' === $trigger ) {
14211 $affiliate_id = $data['filter']['affiliate_id']['value'];
14212 if ( -1 === $affiliate_id ) {
14213 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wafp_payments as postmeta JOIN {$wpdb->prefix}wafp_events as posts ON posts.evt_id=postmeta.id WHERE postmeta.amount>0 order by postmeta.id DESC LIMIT 1" );
14214 } else {
14215 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wafp_payments as postmeta JOIN {$wpdb->prefix}wafp_events as posts ON posts.evt_id=postmeta.id WHERE postmeta.amount>0 AND affiliate_id=%d order by postmeta.id DESC LIMIT 1", $affiliate_id ) );
14216 }
14217 }
14218
14219 if ( ! empty( $result ) ) {
14220 switch ( $trigger ) {
14221 case 'sale_recorded':
14222 $result_affiliate_id = $result[0]->affiliate_id;
14223 /**
14224 *
14225 * Ignore line
14226 *
14227 * @phpstan-ignore-next-line
14228 */
14229 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
14230 $affiliate = get_object_vars( $affiliate->rec );
14231 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
14232 break;
14233 case 'sale_added':
14234 /**
14235 *
14236 * Ignore line
14237 *
14238 * @phpstan-ignore-next-line
14239 */
14240 $data = new \EasyAffiliate\Models\User( $result['results'][0]->ID );
14241 /**
14242 *
14243 * Ignore line
14244 *
14245 * @phpstan-ignore-next-line
14246 */
14247 $context = get_object_vars( $data->rec );
14248 break;
14249 case 'payout_made':
14250 $result_affiliate_id = $result[0]->affiliate_id;
14251 /**
14252 *
14253 * Ignore line
14254 *
14255 * @phpstan-ignore-next-line
14256 */
14257 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
14258 $affiliate = get_object_vars( $affiliate->rec );
14259 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
14260 break;
14261 default:
14262 return;
14263 }
14264 $context['pluggable_data'] = $context;
14265 $context['response_type'] = 'live';
14266 } elseif ( empty( $result ) ) {
14267 switch ( $trigger ) {
14268 case 'sale_added':
14269 $context = json_decode(
14270 '{"ID": 1,"first_name": "John","last_name": "D","user_login": "johnd","user_nicename": "johnd","user_email": "johnd@d.com","user_url": "","user_registered": "2023-10-13 13:39:16","user_activation_key": "","user_status": "0","display_name": "johnd","paypal_email": "johnd@d.com","address_one": "1street","address_two": "",
14271 "city": "london","state": "","zip": "","country": "UK","tax_id_us": "","tax_id_int": "","is_affiliate": "1","is_blocked": "","blocked_message": "","referrer": "0","notes": "","unsubscribed": ""}',
14272 true
14273 );
14274 break;
14275 case 'sale_recorded':
14276 $context = json_decode( '{"wp_user_id": 73,"user_login": "johnd@yopmail.com","display_name": "john d","user_firstname": "john","user_lastname": "d","user_email": "johnd@yopmail.com","user_role": ["subscriber"],"id": "5","affiliate_id": "73","click_id": "0","item_id": "","item_name": "test","coupon": "","sale_amount": "10.00","refund_amount": "20.00","subscr_id": "","subscr_paynum": "0","ip_addr": "","cust_email": "","cust_name": "","trans_num": "test5","type": "commission","source": "general","order_id": "0","status": "complete","rebill": "0","created_at": "2023-10-11 09:18:41"}', true );
14277 break;
14278 case 'payout_made':
14279 $context = json_decode( '{"wp_user_id": 73,"user_login": "johnd@yopmail.com","display_name": "john d","user_firstname": "john","user_lastname": "d","user_email": "johnd@yopmail.com","user_role": ["subscriber"],"id": "5","affiliate_id": "73","click_id": "0","item_id": "","item_name": "test","coupon": "","sale_amount": "10.00","refund_amount": "20.00","subscr_id": "","subscr_paynum": "0","ip_addr": "","cust_email": "","cust_name": "","trans_num": "test5","type": "commission","source": "general","order_id": "0","status": "complete","rebill": "0","created_at": "2023-10-11 09:18:41"}', true );
14280 break;
14281 default:
14282 return;
14283 }
14284 $context['pluggable_data'] = $context;
14285 $context['response_type'] = 'sample';
14286 }
14287
14288 return $context;
14289
14290 }
14291
14292 /**
14293 * Get WooCommerce Subscriptions Coupon list
14294 *
14295 * @param array $data data.
14296 *
14297 * @return array
14298 */
14299 public function search_woo_subscription_coupon_list( $data ) {
14300 $options = [];
14301
14302 $coupon_codes = [];
14303
14304 $query = new \WP_Query(
14305 [
14306 'post_type' => 'shop_coupon',
14307 'posts_per_page' => -1,
14308 'no_found_rows' => true,
14309 'meta_query' => [
14310 [
14311 'key' => '_is_aw_coupon',
14312 'compare' => 'NOT EXISTS',
14313 ],
14314 ],
14315 ]
14316 );
14317
14318 foreach ( $query->posts as $coupon ) {
14319 /**
14320 *
14321 * Ignore line
14322 *
14323 * @phpstan-ignore-next-line
14324 */
14325 $code = wc_format_coupon_code( $coupon->post_title );
14326 $coupon_codes[ $code ] = $code;
14327 }
14328
14329 foreach ( $coupon_codes as $code ) {
14330
14331 $coupon = new \WC_Coupon( $code );
14332
14333 if ( ! in_array( $coupon->get_discount_type(), [ 'recurring_fee', 'recurring_percent' ], true ) ) {
14334 unset( $coupon_codes[ $code ] );
14335 }
14336 }
14337
14338 if ( ! empty( $coupon_codes ) ) {
14339 if ( is_array( $coupon_codes ) ) {
14340 foreach ( $coupon_codes as $coupon_code ) {
14341 $options[] = [
14342 'label' => $coupon_code,
14343 'value' => $coupon_code,
14344 ];
14345 }
14346 }
14347 }
14348 return [
14349 'options' => $options,
14350 'hasMore' => false,
14351 ];
14352 }
14353
14354 /**
14355 * Get BuddyBoss field.
14356 *
14357 * @param array $data data.
14358 *
14359 * @return array|void
14360 */
14361 public function search_bb_field_list( $data ) {
14362 global $wpdb;
14363 $options = [];
14364
14365 $xprofile_fields = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_xprofile_fields WHERE parent_id = 0 ORDER BY field_order ASC" );
14366 if ( ! empty( $xprofile_fields ) ) {
14367 foreach ( $xprofile_fields as $xprofile_field ) {
14368 $options[] = [
14369 'label' => $xprofile_field->name,
14370 'value' => $xprofile_field->id,
14371 ];
14372 }
14373 }
14374
14375 return [
14376 'options' => $options,
14377 'hasMore' => false,
14378 ];
14379 }
14380
14381 /**
14382 * Get Woocommerce Bookings Product List.
14383 *
14384 * @param array $data data.
14385 *
14386 * @return array|void
14387 */
14388 public function search_wb_bookable_product_list( $data ) {
14389 $options = [];
14390
14391 if ( ! class_exists( 'WC_Bookings_Admin' ) ) {
14392 return;
14393 }
14394
14395 $products = WC_Bookings_Admin::get_booking_products();
14396 if ( ! empty( $products ) ) {
14397 if ( is_array( $products ) ) {
14398 foreach ( $products as $product ) {
14399 $options[] = [
14400 'label' => $product->get_name(),
14401 'value' => $product->get_id(),
14402 ];
14403 $resources = $product->get_resources();
14404 if ( ! empty( $resources ) ) {
14405 foreach ( $resources as $resource ) {
14406 $options[] = [
14407 'label' => $resource->get_name(),
14408 'value' => $resource->get_id(),
14409 ];
14410 }
14411 }
14412 }
14413 }
14414 }
14415
14416 return [
14417 'options' => $options,
14418 'hasMore' => false,
14419 ];
14420 }
14421
14422 /**
14423 * Get last data for WooCommerce Booking trigger
14424 *
14425 * @param array $data data.
14426 * @return array
14427 */
14428 public function search_woo_commerce_booking_last_data( $data ) {
14429 $context = [];
14430 $context['response_type'] = 'sample';
14431 $context['pluggable_data'] = [];
14432 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
14433 global $wpdb;
14434 if ( 'booking_created' === $term ) {
14435 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' AND post_status='confirmed' order by ID DESC LIMIT 1" );
14436
14437 if ( ! empty( $results ) ) {
14438 if ( class_exists( 'WC_Booking' ) ) {
14439 $booking = new WC_Booking( $results[0]->ID );
14440 $person_counts = $booking->get_person_counts();
14441 $bookable_product_id = $booking->get_product_id();
14442 if ( method_exists( $booking, 'get_data' ) ) {
14443 $booking = $booking->get_data();
14444 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
14445 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
14446 if ( ! empty( $person_counts ) ) {
14447 $total_count = 0;
14448 foreach ( $person_counts as $key => $value ) {
14449 $total_count += $value;
14450 }
14451 $booking['total_person_counts'] = $total_count;
14452 }
14453 $booking['bookable_product'] = $bookable_product_id;
14454 $booking['bookable_product'] = $booking->get_product_id();
14455 $context['response_type'] = 'live';
14456 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
14457 }
14458 }
14459 } else {
14460 $booking_sample_data = json_decode( '{"id": 6546,"all_day": false,"cost": "45.02","customer_id": 1,"date_created": 1696915500,"date_modified": 1696915863,"end": "2023-10-14 12:00:00","google_calendar_event_id": "0","order_id": 0,"order_item_id": 0,"parent_id": 0,"person_counts": {"6525": 1,"6526": 1},"product_id": 6527,"resource_id": 0,"start": "2023-10-14 10:00:00","status": "confirmed","local_timezone": "","meta_data": [{"id": 12163,"key": "_booking_all_day","value": "0"},{"id": 12164,"key": "_booking_cost","value": "45.02"},{"id": 12165,"key": "_booking_customer_id","value": "1"},{"id": 12166,"key": "_booking_order_item_id","value": "0"},{"id": 12167,"key": "_booking_parent_id","value": "0"},{"id": 12168,"key": "_booking_persons","value": {"6525": 1,"6526": 1}},{"id": 12169,"key": "_booking_product_id","value": "6527"},{"id": 12170,"key": "_booking_resource_id","value": "0"},{"id": 12171,"key": "_booking_start","value": "20231014100000"},{"id": 12172,"key": "_booking_end","value": "20231014120000"},{"id": 12173,"key": "_wc_bookings_gcalendar_event_id","value": "0"},{"id": 12175,"key": "_edit_lock","value": "1696915725:1"},{"id": 12176,"key": "_edit_last","value": "1"}],"total_person_counts": 2,"bookable_product": 6527,"wp_user_id": 1,"user_login": "johnd@yopmail.com","display_name": "john d","user_firstname": "john","user_lastname": "d","user_email": "johnd@yopmail.com","user_role": ["customer"]}', true );
14461 $context['pluggable_data'] = $booking_sample_data;
14462 }
14463 } elseif ( 'booking_status_changed' === $term ) {
14464 $to_status = isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1;
14465
14466 if ( -1 == $to_status ) {
14467 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' order by ID DESC LIMIT 1" );
14468 } else {
14469 $results = $wpdb->get_results(
14470 $wpdb->prepare(
14471 "
14472 SELECT *
14473 FROM {$wpdb->prefix}posts
14474 WHERE `post_status` = %s
14475 AND `post_type` LIKE %s
14476 ",
14477 $to_status,
14478 'wc_booking'
14479 )
14480 );
14481 }
14482
14483 if ( ! empty( $results ) ) {
14484 if ( class_exists( 'WC_Booking' ) ) {
14485 $booking = new WC_Booking( $results[0]->ID );
14486 $person_counts = $booking->get_person_counts();
14487 $bookable_product_id = $booking->get_product_id();
14488 if ( method_exists( $booking, 'get_data' ) ) {
14489 $booking = $booking->get_data();
14490 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
14491 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
14492 if ( ! empty( $person_counts ) ) {
14493 $total_count = 0;
14494 foreach ( $person_counts as $key => $value ) {
14495 $total_count += $value;
14496 }
14497 $booking['total_person_counts'] = $total_count;
14498 }
14499 $booking['bookable_product'] = $bookable_product_id;
14500 $booking['from_status'] = $data['filter']['from_status']['value'];
14501 $booking['to_status'] = $booking['status'];
14502 $context['response_type'] = 'live';
14503 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
14504 }
14505 }
14506 } else {
14507 $booking_sample_data = json_decode( '{"id": 6546,"all_day": false,"cost": "45.02","customer_id": 1,"date_created": 1696915500,"date_modified": 1696915863,"end": "2023-10-14 12:00:00","google_calendar_event_id": "0","order_id": 0,"order_item_id": 0,"parent_id": 0,"person_counts": {"6525": 1,"6526": 1},"product_id": 6527,"resource_id": 0,"start": "2023-10-14 10:00:00","status": "confirmed","to_status": "confirmed","from_status": "cancelled","local_timezone": "","meta_data": [{"id": 12163,"key": "_booking_all_day","value": "0"},{"id": 12164,"key": "_booking_cost","value": "45.02"},{"id": 12165,"key": "_booking_customer_id","value": "1"},{"id": 12166,"key": "_booking_order_item_id","value": "0"},{"id": 12167,"key": "_booking_parent_id","value": "0"},{"id": 12168,"key": "_booking_persons","value": {"6525": 1,"6526": 1}},{"id": 12169,"key": "_booking_product_id","value": "6527"},{"id": 12170,"key": "_booking_resource_id","value": "0"},{"id": 12171,"key": "_booking_start","value": "20231014100000"},{"id": 12172,"key": "_booking_end","value": "20231014120000"},{"id": 12173,"key": "_wc_bookings_gcalendar_event_id","value": "0"},{"id": 12175,"key": "_edit_lock","value": "1696915725:1"},{"id": 12176,"key": "_edit_last","value": "1"}],"total_person_counts": 2,"bookable_product": 6527,"wp_user_id": 1,"user_login": "johnd@yopmail.com","display_name": "john d","user_firstname": "john","user_lastname": "d","user_email": "johnd@yopmail.com","user_role": ["customer"]}', true );
14508 $context['pluggable_data'] = $booking_sample_data;
14509 }
14510 }
14511 return $context;
14512 }
14513
14514 /**
14515 * Get WooCommerce Booking status list.
14516 *
14517 * @param array $data data.
14518 *
14519 * @return array
14520 */
14521 public function search_woo_booking_status_list( $data ) {
14522
14523 $options = [
14524 [
14525 'label' => 'Pending Confirmation',
14526 'value' => 'pending-confirmation',
14527 ],
14528 [
14529 'label' => 'Unpaid',
14530 'value' => 'unpaid',
14531 ],
14532 [
14533 'label' => 'Confirmed',
14534 'value' => 'confirmed',
14535 ],
14536 [
14537 'label' => 'Paid',
14538 'value' => 'paid',
14539 ],
14540 [
14541 'label' => 'Complete',
14542 'value' => 'complete',
14543 ],
14544 [
14545 'label' => 'In Cart',
14546 'value' => 'in-cart',
14547 ],
14548 [
14549 'label' => 'Cancelled',
14550 'value' => 'cancelled',
14551 ],
14552 ];
14553
14554 return [
14555 'options' => $options,
14556 'hasMore' => false,
14557 ];
14558 }
14559
14560 /**
14561 * Get Woocommerce Memberships Status List.
14562 *
14563 * @param array $data data.
14564 *
14565 * @return array|void
14566 */
14567 public function search_wc_membership_status_list( $data ) {
14568 $options = [];
14569 if ( ! function_exists( 'wc_memberships_get_user_membership_statuses' ) ) {
14570 return;
14571 }
14572
14573 $statuses = wc_memberships_get_user_membership_statuses();
14574 if ( ! empty( $statuses ) ) {
14575 if ( is_array( $statuses ) ) {
14576 foreach ( $statuses as $status => $value ) {
14577 $status = 0 === strpos( $status, 'wcm-' ) ? substr( $status, 4 ) : $status;
14578 $options[] = [
14579 'label' => $value['label'],
14580 'value' => $status,
14581 ];
14582 }
14583 }
14584 }
14585
14586 return [
14587 'options' => $options,
14588 'hasMore' => false,
14589 ];
14590 }
14591
14592 /**
14593 * Search Woocommerce Subscription data.
14594 *
14595 * @param array $data data.
14596 * @return array|void
14597 */
14598 public function search_user_subscription_last_data( $data ) {
14599
14600 $context = [];
14601
14602 if ( ! function_exists( 'wcs_get_subscription' ) ) {
14603 return;
14604 }
14605 if ( ! function_exists( 'wcs_get_subscriptions_for_product' ) ) {
14606 return;
14607 }
14608 if ( ! function_exists( 'wcs_order_contains_renewal' ) ) {
14609 return;
14610 }
14611 if ( ! function_exists( 'wcs_get_subscriptions_for_order' ) ) {
14612 return;
14613 }
14614
14615 if ( 'trial_end' == $data['search_term'] ) {
14616 $subscription_id = $data['filter']['subscription']['value'];
14617 $query_args = [
14618 'post_type' => 'shop_subscription',
14619 'orderby' => 'ID',
14620 'order' => 'DESC',
14621 'post_status' => 'wc-active',
14622 'posts_per_page' => 1,
14623 'meta_query' => [
14624 [
14625 'key' => '_schedule_trial_end',
14626 'value' => gmdate( 'Y-m-d H:i:s' ),
14627 'compare' => '<=',
14628 ],
14629 ],
14630 'post__in' => [ $subscription_id ],
14631 ];
14632 $query_result = new WP_Query( $query_args );
14633 $subscription_orders = $query_result->get_posts();
14634
14635 if ( ! empty( $subscription_orders ) ) {
14636 /**
14637 *
14638 * Ignore line
14639 *
14640 * @phpstan-ignore-next-line
14641 */
14642 $subscription = wcs_get_subscription( $subscription_orders[0]->ID );
14643 $user_id = $subscription->get_user_id();
14644
14645 $items = $subscription->get_items();
14646 $product_ids = [];
14647 foreach ( $items as $item ) {
14648 $product_ids[] = $item->get_product_id();
14649 }
14650
14651 $subscription_status = $subscription->get_status();
14652 $subscription_start_date = $subscription->get_date_created();
14653
14654 $context['subscription_data'] = [
14655 'status' => $subscription_status,
14656 'start_date' => $subscription_start_date,
14657 'trial_end_date' => $subscription->get_date( 'schedule_trial_end' ),
14658 'next_payment_date' => $subscription->get_date( 'next_payment' ),
14659 'end_date' => $subscription->get_date( 'schedule_end' ),
14660 ];
14661 $context['user'] = WordPress::get_user_context( $user_id );
14662
14663 foreach ( $product_ids as $val ) {
14664 $context['subscription'] = $val;
14665 $context['subscription_name'] = get_the_title( $val );
14666 }
14667 $context['pluggable_data'] = $context;
14668 $context['response_type'] = 'live';
14669 } else {
14670 $context = json_decode( '{"subscription_data": {"status": "active","start_date": {"date": "2023-05-18 12:36:53.000000","timezone_type": 1,"timezone": "+00:00"},"trial_end_date": "2023-10-20 03:31:04","next_payment_date": 0,"end_date": "2024-05-28 12:36:53"},"user": {"wp_user_id": 1,"user_login": "john@d.com","display_name": "john d","user_firstname": "john","user_lastname": "d","user_email": "john@d.com","user_role": ["customer","subscriber"]},"subscription": 5861,"subscription_name": "Demo Subscription"}', true );
14671 $context['pluggable_data'] = $context;
14672 $context['response_type'] = 'sample';
14673 }
14674 } elseif ( 'renewal_payment_failed' == $data['search_term'] ) {
14675 $subscription_item = $data['filter']['subscription_item']['value'];
14676 $subscription_ids = wcs_get_subscriptions_for_product( $subscription_item );
14677 $ids = [];
14678 $related = [];
14679 foreach ( $subscription_ids as $subscription ) {
14680 $ids[] = $subscription;
14681 /**
14682 *
14683 * Ignore line
14684 *
14685 * @phpstan-ignore-next-line
14686 */
14687 $subscriptions = new WC_Subscription( $subscription );
14688 /**
14689 *
14690 * Ignore line
14691 *
14692 * @phpstan-ignore-next-line
14693 */
14694 $related[] = $subscriptions->get_related_orders();
14695 }
14696
14697 $failed_payment_orders = [];
14698 foreach ( $related as $value ) {
14699 foreach ( $value as $val ) {
14700 if ( wcs_order_contains_renewal( $val ) ) {
14701 $order = wc_get_order( $val );
14702 if ( $order ) {
14703 /**
14704 *
14705 * Ignore line
14706 *
14707 * @phpstan-ignore-next-line
14708 */
14709 $payment_status = $order->get_status();
14710 if ( 'failed' === $payment_status || 'wc-on-hold' === $payment_status ) {
14711 $failed_payment_orders[] = $val;
14712 }
14713 }
14714 }
14715 }
14716 }
14717
14718 if ( ! empty( $failed_payment_orders ) ) {
14719 $failed = array_rand( $failed_payment_orders );
14720 $related_subscriptions = wcs_get_subscriptions_for_order( $failed_payment_orders[ $failed ] );
14721 if ( ! empty( $related_subscriptions ) ) {
14722 $sub_id = get_post_meta( $failed_payment_orders[ $failed ], '_subscription_renewal', true );
14723 $subscription = wcs_get_subscription( $sub_id );
14724
14725 $items = $subscription->get_items();
14726 $product_ids = [];
14727 foreach ( $items as $item ) {
14728 $product_ids[] = $item->get_product_id();
14729 }
14730
14731 $context['user'] = WordPress::get_user_context( $subscription->get_user_id() );
14732 foreach ( [ 'renewal' ] as $order_type ) {
14733 foreach ( $subscription->get_related_orders( 'ids', $order_type ) as $order_id ) {
14734 $context['subscription_related_order'][] = $order_id;
14735 }
14736 }
14737 foreach ( $product_ids as $val ) {
14738 $context['subscription_item'] = $val;
14739 $context['subscription_name'] = get_the_title( $val );
14740 }
14741 $context['data'] = $subscription->get_data();
14742 $context['pluggable_data'] = $context;
14743 $context['response_type'] = 'live';
14744 } else {
14745 $context['pluggable_data'] = json_decode( '{"user": {"wp_user_id": 36,"user_login": "john@d.com","display_name": "test","user_firstname": "Test","user_lastname": "test","user_email": "john@d.com","user_role": ["wpamelia-customer","customer"]},"subscription_related_order": [6643],"subscription_item": 5962,"subscription_name": "Demo2 Subscription","data": {"id": 6642,"parent_id": 6641,"status": "on-hold","currency": "USD","version": "7.6.1","prices_include_tax": false,"date_created": {"date": "2023-10-20 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"date_modified": {"date": "2023-10-20 08:45:02.000000","timezone_type": 1,"timezone": "+00:00"},"discount_total": "0","discount_tax": "0","shipping_total": "0","shipping_tax": "0","cart_tax": "0","total": "1.00","total_tax": "0","customer_id": 36,"order_key": "wc_order_6Pdirh5WDDiVA","billing": {"first_name": "bella","last_name": "test","company": "","address_1": "test","address_2": "","city": "test","state": "TN","postcode": "600144","country": "IN","email": "john@d.com","phone": "98675757"},"shipping": {"first_name": "bella","last_name": "test","company": "","address_1": "test","address_2": "","city": "Chennai","state": "TN","postcode": "600100","country": "IN","phone": ""},"payment_method": "","payment_method_title": "","transaction_id": "","customer_ip_address": "127.0.0.1","customer_user_agent": "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/118.0.0.0 Safari\/537.36","created_via": "checkout","customer_note": "","date_completed": null,"date_paid": null,"cart_hash": "","order_stock_reduced": false,"download_permissions_granted": true,"new_order_email_sent": false,"recorded_sales": false,"recorded_coupon_usage_counts": false,"billing_period": "day","billing_interval": "1","suspension_count": 1,"requires_manual_renewal": true,"cancelled_email_sent": "","trial_period": "day","schedule_trial_end": {"date": "2023-10-21 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"schedule_next_payment": {"date": "2023-10-21 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"schedule_end": {"date": "2023-10-22 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"schedule_start": {"date": "2023-10-22 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"switch_data": [],"number": "6642","meta_data": [{"id": 13719,"key": "is_vat_exempt","value": "no"}],"line_items": {"142": []},"tax_lines": [],"shipping_lines": [],"fee_lines": [],"coupon_lines": []}}', true );
14746 $context['response_type'] = 'sample';
14747 }
14748 } else {
14749 $context['pluggable_data'] = json_decode( '{"user": {"wp_user_id": 36,"user_login": "john@d.com","display_name": "test","user_firstname": "Test","user_lastname": "test","user_email": "john@d.com","user_role": ["wpamelia-customer","customer"]},"subscription_related_order": [6643],"subscription_item": 5962,"subscription_name": "Demo2 Subscription","data": {"id": 6642,"parent_id": 6641,"status": "on-hold","currency": "USD","version": "7.6.1","prices_include_tax": false,"date_created": {"date": "2023-10-20 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"date_modified": {"date": "2023-10-20 08:45:02.000000","timezone_type": 1,"timezone": "+00:00"},"discount_total": "0","discount_tax": "0","shipping_total": "0","shipping_tax": "0","cart_tax": "0","total": "1.00","total_tax": "0","customer_id": 36,"order_key": "wc_order_6Pdirh5WDDiVA","billing": {"first_name": "bella","last_name": "test","company": "","address_1": "test","address_2": "","city": "test","state": "TN","postcode": "600144","country": "IN","email": "john@d.com","phone": "98675757"},"shipping": {"first_name": "bella","last_name": "test","company": "","address_1": "test","address_2": "","city": "Chennai","state": "TN","postcode": "600100","country": "IN","phone": ""},"payment_method": "","payment_method_title": "","transaction_id": "","customer_ip_address": "127.0.0.1","customer_user_agent": "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/118.0.0.0 Safari\/537.36","created_via": "checkout","customer_note": "","date_completed": null,"date_paid": null,"cart_hash": "","order_stock_reduced": false,"download_permissions_granted": true,"new_order_email_sent": false,"recorded_sales": false,"recorded_coupon_usage_counts": false,"billing_period": "day","billing_interval": "1","suspension_count": 1,"requires_manual_renewal": true,"cancelled_email_sent": "","trial_period": "day","schedule_trial_end": {"date": "2023-10-21 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"schedule_next_payment": {"date": "2023-10-21 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"schedule_end": {"date": "2023-10-22 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"schedule_start": {"date": "2023-10-22 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"switch_data": [],"number": "6642","meta_data": [{"id": 13719,"key": "is_vat_exempt","value": "no"}],"line_items": {"142": []},"tax_lines": [],"shipping_lines": [],"fee_lines": [],"coupon_lines": []}}', true );
14750 $context['response_type'] = 'sample';
14751 }
14752 }
14753 return $context;
14754 }
14755
14756 /**
14757 * Get Masteriyo LMS Courses.
14758 *
14759 * @param array $data data.
14760 *
14761 * @return array
14762 */
14763 public function search_masteriyo_lms_courses( $data ) {
14764
14765 $page = $data['page'];
14766 $limit = Utilities::get_search_page_limit();
14767 $offset = $limit * ( $page - 1 );
14768
14769 $args = [
14770 'post_type' => 'mto-course',
14771 'posts_per_page' => $limit,
14772 'offset' => $offset,
14773 'orderby' => 'title',
14774 'order' => 'ASC',
14775 'post_status' => 'publish',
14776 ];
14777
14778 $courses = get_posts( $args );
14779
14780 $course_count = wp_count_posts( 'mto-course' )->publish;
14781
14782 $options = [];
14783 if ( ! empty( $courses ) ) {
14784 if ( is_array( $courses ) ) {
14785 foreach ( $courses as $course ) {
14786 $options[] = [
14787 'label' => $course->post_title,
14788 'value' => $course->ID,
14789 ];
14790 }
14791 }
14792 }
14793 return [
14794 'options' => $options,
14795 'hasMore' => $course_count > $limit && $course_count > $offset,
14796 ];
14797 }
14798
14799 /**
14800 * Get Masteriyo LMS Lessons.
14801 *
14802 * @param array $data data.
14803 *
14804 * @return array
14805 */
14806 public function search_masteriyo_lms_lessons( $data ) {
14807
14808 $course_id = $data['dynamic'];
14809 $page = $data['page'];
14810 $limit = Utilities::get_search_page_limit();
14811 $offset = $limit * ( $page - 1 );
14812 $options = [];
14813 $args = [
14814 'post_type' => 'mto-lesson',
14815 'posts_per_page' => $limit,
14816 'offset' => $offset,
14817 'orderby' => 'title',
14818 'order' => 'ASC',
14819 'post_status' => 'publish',
14820 'meta_query' => [
14821 [
14822 'key' => '_course_id',
14823 'value' => $course_id,
14824 'compare' => '=',
14825 ],
14826 ],
14827 ];
14828
14829 $lessons = get_posts( $args );
14830 $lesson_count = wp_count_posts( 'mto-lesson' )->publish;
14831 if ( ! empty( $lessons ) ) {
14832 if ( is_array( $lessons ) ) {
14833 foreach ( $lessons as $lesson ) {
14834 $options[] = [
14835 'label' => $lesson->post_title,
14836 'value' => $lesson->ID,
14837 ];
14838 }
14839 }
14840 }
14841 return [
14842 'options' => $options,
14843 'hasMore' => $lesson_count > $limit && $lesson_count > $offset,
14844 ];
14845 }
14846
14847 /**
14848 * Get Masteriyo LMS Quiz.
14849 *
14850 * @param array $data data.
14851 *
14852 * @return array
14853 */
14854 public function search_masteriyo_lms_quizs( $data ) {
14855
14856 $course_id = $data['dynamic'];
14857 $page = $data['page'];
14858 $limit = Utilities::get_search_page_limit();
14859 $offset = $limit * ( $page - 1 );
14860 $options = [];
14861 $args = [
14862 'post_type' => 'mto-quiz',
14863 'posts_per_page' => $limit,
14864 'offset' => $offset,
14865 'orderby' => 'title',
14866 'order' => 'ASC',
14867 'post_status' => 'publish',
14868 'meta_query' => [
14869 [
14870 'key' => '_course_id',
14871 'value' => $course_id,
14872 'compare' => '=',
14873 ],
14874 ],
14875 ];
14876
14877 $quizs = get_posts( $args );
14878 $quiz_count = wp_count_posts( 'mto-quiz' )->publish;
14879 if ( ! empty( $quizs ) ) {
14880 if ( is_array( $quizs ) ) {
14881 foreach ( $quizs as $quiz ) {
14882 $options[] = [
14883 'label' => $quiz->post_title,
14884 'value' => $quiz->ID,
14885 ];
14886 }
14887 }
14888 }
14889 return [
14890 'options' => $options,
14891 'hasMore' => $quiz_count > $limit && $quiz_count > $offset,
14892 ];
14893 }
14894
14895 /**
14896 * Search Masteriyo LMS data.
14897 *
14898 * @param array $data data.
14899 * @return array|void|mixed
14900 */
14901 public function search_masteriyo_lms_last_data( $data ) {
14902 global $wpdb;
14903 $post_type = $data['post_type'];
14904 $trigger = $data['search_term'];
14905 $context = [];
14906
14907 $post_id = -1;
14908 if ( 'course_completed' === $trigger ) {
14909 $post_id = $data['filter']['course_id']['value'];
14910 } elseif ( 'lesson_completed' === $trigger ) {
14911 $post_id = $data['filter']['lesson_id']['value'];
14912 } elseif ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
14913 $post_id = $data['filter']['quiz_id']['value'];
14914 }
14915
14916 if ( 'course_completed' === $trigger || 'lesson_completed' === $trigger ) {
14917 if ( -1 === $post_id ) {
14918 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}masteriyo_user_activities as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.item_id WHERE postmeta.activity_status='completed' AND posts.post_type=%s order by postmeta.id DESC LIMIT 1", $post_type ) );
14919 } else {
14920 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}masteriyo_user_activities as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.item_id WHERE postmeta.item_id = %s AND postmeta.activity_status='completed' AND posts.post_type=%s order by postmeta.id DESC LIMIT 1", $post_id, $post_type ) );
14921 }
14922 }
14923
14924 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
14925 if ( -1 === $post_id ) {
14926 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}masteriyo_user_activities as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.item_id WHERE postmeta.activity_status='completed' AND postmeta.activity_type='quiz' AND posts.post_type=%s order by postmeta.user_item_id DESC LIMIT 1", $post_type ) );
14927 } else {
14928 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}masteriyo_user_activities as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.item_id WHERE postmeta.item_id=%s AND postmeta.activity_type='quiz' OR postmeta.activity_status='completed' AND posts.post_type=%s order by postmeta.id DESC LIMIT 1", $post_id, $post_type ) );
14929 }
14930 if ( ! empty( $result ) ) {
14931 $resultt = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}masteriyo_quiz_attempts WHERE quiz_id=%d AND user_id=%d order by id DESC LIMIT 1", $result[0]->item_id, $result[0]->user_id ) );
14932
14933 if ( function_exists( 'masteriyo_get_quiz' ) ) {
14934 $quiz = masteriyo_get_quiz( $resultt[0]->quiz_id );
14935 if ( is_object( $quiz ) && method_exists( $quiz, 'get_pass_mark' ) ) {
14936 if ( 'quiz_completed' === $trigger && $resultt[0]->earned_marks < $quiz->get_pass_mark() ) {
14937 $result = [];
14938 } elseif ( 'quiz_failed' === $trigger && $resultt[0]->earned_marks > $quiz->get_pass_mark() ) {
14939 $result = [];
14940 }
14941 }
14942 }
14943 }
14944 }
14945
14946 if ( ! empty( $result ) ) {
14947 $result_item_id = $result[0]->item_id;
14948 $result_user_id = $result[0]->user_id;
14949 $quiz_attempt = '';
14950 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
14951 $resultt = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}masteriyo_quiz_attempts WHERE quiz_id=%d AND user_id=%d order by id DESC LIMIT 1", $result[0]->item_id, $result[0]->user_id ) );
14952 $quiz_attempt = $resultt[0]->id;
14953 }
14954 switch ( $trigger ) {
14955 case 'course_completed':
14956 if ( function_exists( 'masteriyo_get_course' ) ) {
14957 $course = masteriyo_get_course( $result_item_id );
14958 $context_data = array_merge(
14959 WordPress::get_user_context( $result_user_id ),
14960 $course->get_data()
14961 );
14962 $context_data['course_id'] = $result_item_id;
14963 }
14964 break;
14965 case 'lesson_completed':
14966 if ( function_exists( 'masteriyo_get_lesson' ) ) {
14967 $lesson = masteriyo_get_lesson( $result_item_id );
14968 $context_data = array_merge(
14969 WordPress::get_user_context( $result_user_id ),
14970 $lesson->get_data()
14971 );
14972 $context_data['lesson_id'] = $result_item_id;
14973 }
14974 break;
14975 case 'quiz_completed':
14976 case 'quiz_failed':
14977 if ( function_exists( 'masteriyo_get_quiz' ) ) {
14978 $quiz = masteriyo_get_quiz( $result_item_id );
14979 $context_data = WordPress::get_user_context( $result_user_id );
14980 if ( function_exists( 'masteriyo_get_quiz_attempt' ) ) {
14981 $attempt = masteriyo_get_quiz_attempt( $quiz_attempt );
14982 $context_data['quiz_id'] = $result_item_id;
14983 $context_data['course_id'] = $quiz->get_course_id();
14984 $context_data['quiz'] = $quiz->get_data();
14985 $context_data['attempt'] = $attempt->get_data();
14986 }
14987 }
14988 break;
14989 default:
14990 return;
14991 }
14992 if ( ! empty( $context_data ) ) {
14993 $context['pluggable_data'] = $context_data;
14994 $context['response_type'] = 'live';
14995 }
14996 } elseif ( empty( $result ) ) {
14997 switch ( $trigger ) {
14998 case 'course_completed':
14999 $sample_data = '{"pluggable_data":{"wp_user_id": 1,"user_login": "admin","display_name": "admin","user_firstname": "test","user_lastname": "test","user_email": "john@d.com","user_role": ["customer"],"id": 6636,"name": "Modes Master Class","slug": "modes-master-class-2","date_created": {"date": "2023-10-20 06:09:15.000000","timezone_type": 1,"timezone": "+00:00"},"date_modified": {"date": "2023-10-21 15:22:29.000000","timezone_type": 1,"timezone": "+00:00"},"status": "publish","menu_order": 0,"featured": false,"catalog_visibility": "visible","description": "Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words.","short_description": "","post_password": "","author_id": 1,"parent_id": 0,"reviews_allowed": true,"date_on_sale_from": null,"date_on_sale_to": null,"price": "0","regular_price": "0","sale_price": "","price_type": "free","category_ids": [106,107],"tag_ids": [],"difficulty_id": 0,"featured_image": 6616,"rating_counts": [],"average_rating": "0","review_count": 0,"enrollment_limit": 0,"duration": 360,"access_mode": "open","billing_cycle": "","show_curriculum": true,"purchase_note": "","highlights": "<li>Suscipit tortor eget felis.<\/li><li>Curabitur arcu erat idimper.<\/li><li>Lorem ipsum dolor sit amet.<\/li>","is_ai_created": false,"is_creating": false,"meta_data": []},"response_type":"sample"} ';
15000 break;
15001 case 'lesson_completed':
15002 $sample_data = '{"pluggable_data":{"wp_user_id": 1,"user_login": "admin","display_name": "admin","user_firstname": "test","user_lastname": "test","user_email": "john@d.com","user_role": ["customer"],"id": 6636,"name": "Lesson 1","slug": "lesson-1","date_created": {"date": "2023-10-20 06:09:15.000000","timezone_type": 1,"timezone": "+00:00"},"date_modified": {"date": "2023-10-21 15:22:29.000000","timezone_type": 1,"timezone": "+00:00"},"status": "publish","menu_order": 0,"featured": false,"catalog_visibility": "visible","description": "new lesson","short_description": "","post_password": "","author_id": 1,"parent_id": 0,"course_id": 6594,"reviews_allowed": true,"featured_image": 6616,"rating_counts": [],"average_rating": "0","review_count": 0,"meta_data": [],"lesson_id": "6596"},"response_type":"sample"} ';
15003 break;
15004 case 'quiz_completed':
15005 case 'quiz_failed':
15006 $sample_data = '{"pluggable_data":{"wp_user_id": 18,"user_login": "john@yopmail.com","display_name": "johns john","user_firstname": "johns","user_lastname": "john","user_email": "john@d.com","user_role": ["customer"],"quiz_id": "6654","course_id": 6634,"quiz": {"id": 6654,"name": "New Quiz","slug": "new-quiz","date_created": {"date": "2023-10-23 16:36:14.000000","timezone_type": 1,"timezone": "+00:00"},"date_modified": {"date": "2023-10-23 17:10:42.000000","timezone_type": 1,"timezone": "+00:00"},"parent_id": 6630,"course_id": 6634,"author_id": 0,"menu_order": 4,"status": "publish","description": "","short_description": "","pass_mark": 2,"full_mark": 100,"duration": 0,"attempts_allowed": 0,"questions_display_per_page": 0,"meta_data": []},"attempt": {"id": 17,"course_id": 6634,"quiz_id": 6654,"user_id": "18","total_questions": 2,"total_answered_questions": 2,"total_marks": "100.00","total_attempts": 1,"total_correct_answers": 1,"total_incorrect_answers": 1,"earned_marks": "1.00","answers": {"6655": {"answered": "False","correct": false},"6656": {"answered": "False","correct": true}},"attempt_status": "attempt_ended","attempt_started_at": {"date": "2023-10-24 09:34:16.000000","timezone_type": 1,"timezone": "+00:00"},"attempt_ended_at": {"date": "2023-10-24 09:34:29.000000","timezone_type": 1,"timezone": "+00:00"},"meta_data": []}},"response_type":"sample"}';
15007 break;
15008 default:
15009 return;
15010 }
15011 $context = json_decode( $sample_data, true );
15012 }
15013
15014 return $context;
15015 }
15016
15017 /**
15018 * Search learndash user added in group.
15019 *
15020 * @param array $data data.
15021 * @return array|void
15022 */
15023 public function search_pluggables_user_added_in_group( $data ) {
15024 $context = [];
15025
15026 if ( ! function_exists( 'learndash_get_groups_user_ids' ) ) {
15027 return;
15028 }
15029 $term = $data['search_term'];
15030 $group_id = $data['filter']['sfwd_group_id']['value'];
15031
15032 if ( 'user_added_group' == $term ) {
15033 if ( -1 === $group_id ) {
15034 $args = [
15035 'numberposts' => 1,
15036 'orderby' => 'rand',
15037 'post_type' => 'groups',
15038 ];
15039 $posts = get_posts( $args );
15040 $random_value = $posts[0]->ID;
15041 $group_users = learndash_get_groups_user_ids( $random_value );
15042 $group_id = $random_value;
15043 } else {
15044 $group_users = learndash_get_groups_user_ids( $group_id );
15045 }
15046 $users = get_users(
15047 [
15048 'meta_key' => 'learndash_group_' . $group_id . '_enrolled_at',
15049 'orderby' => 'meta_value',
15050 'order' => 'DESC',
15051 'number' => 1,
15052 'include' => $group_users,
15053 ]
15054 );
15055 } elseif ( 'user_removed_group' == $term ) {
15056 if ( -1 === $group_id ) {
15057 $args = [
15058 'numberposts' => 1,
15059 'orderby' => 'rand',
15060 'post_type' => 'groups',
15061 ];
15062 $posts = get_posts( $args );
15063 $random_value = $posts[0]->ID;
15064 $group_id = $random_value;
15065 }
15066 $args = [
15067 'meta_query' => [
15068 'relation' => 'AND',
15069 [
15070 'key' => 'group_' . $group_id . '_access_from',
15071 'compare' => 'NOT EXISTS',
15072 ],
15073 [
15074 'key' => 'learndash_group_' . $group_id . '_enrolled_at',
15075 'compare' => 'EXISTS',
15076 ],
15077 ],
15078 'orderby' => 'meta_value',
15079 'order' => 'DESC',
15080 'number' => 1,
15081 ];
15082 $users = get_users( $args );
15083 } elseif ( 'leader_added_group' == $term || 'leader_removed_group' == $term ) {
15084 if ( -1 === $group_id ) {
15085 $args = [
15086 'numberposts' => 1,
15087 'fields' => 'ids',
15088 'orderby' => 'rand',
15089 'post_type' => 'groups',
15090 ];
15091 $posts = get_posts( $args );
15092 $random_key = array_rand( $posts );
15093 $random_value = $posts[ $random_key ];
15094 $group_id = $random_value;
15095 }
15096 $user_query_args = [
15097 'orderby' => 'learndash_group_leaders_' . intval( $group_id ),
15098 'order' => 'DESC',
15099 'meta_query' => [
15100 [
15101 'key' => 'learndash_group_leaders_' . intval( $group_id ),
15102 'value' => intval( $group_id ),
15103 'compare' => '=',
15104 'type' => 'NUMERIC',
15105 ],
15106 ],
15107 ];
15108 $users = get_users( $user_query_args );
15109 }
15110
15111 if ( ! empty( $users ) ) {
15112 $context = WordPress::get_user_context( $users[0]->ID );
15113 $context['sfwd_group_id'] = $group_id;
15114 $context['group_title'] = get_the_title( $group_id );
15115 $context['group_url'] = get_permalink( $group_id );
15116 $context['group_featured_image_id'] = get_post_meta( $group_id, '_thumbnail_id', true );
15117 $context['group_featured_image_url'] = get_the_post_thumbnail_url( $group_id );
15118 if ( 'user_added_group' == $term && function_exists( 'learndash_group_enrolled_courses' ) ) {
15119 $group_courses_id = learndash_group_enrolled_courses( $group_id );
15120 if ( ! empty( $group_courses_id ) ) {
15121 foreach ( $group_courses_id as $key => $course_id ) {
15122 $context['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
15123 }
15124 }
15125 }
15126 $context['response_type'] = 'live';
15127 } else {
15128 $context = WordPress::get_sample_user_context();
15129 $context['group_title'] = 'Test Group';
15130 $context['sfwd_group_id'] = 112;
15131 $context['group_url'] = 'https://example.com/test-group';
15132 $context['group_featured_image_id'] = 113;
15133 $context['group_featured_image_url'] = 'https://example.com/test-group-img';
15134 $context['response_type'] = 'sample';
15135 }
15136
15137 $context['pluggable_data'] = $context;
15138 return $context;
15139 }
15140
15141 /**
15142 * Search learndash user enrolled in course.
15143 *
15144 * @param array $data data.
15145 * @return array|void
15146 */
15147 public function search_pluggables_user_enrolled_course( $data ) {
15148 global $wpdb;
15149 $context = [];
15150
15151 if ( ! function_exists( 'ld_course_access_expires_on' ) ||
15152 ! function_exists( 'learndash_group_enrolled_courses' ) ||
15153 ! function_exists( 'learndash_get_lesson_list' ) ) {
15154 return;
15155 }
15156 $term = $data['search_term'];
15157 $course_id = isset( $data['filter']['sfwd_course_id'] ) ? $data['filter']['sfwd_course_id']['value'] : '';
15158 $group_id = isset( $data['filter']['sfwd_group_id'] ) ? $data['filter']['sfwd_group_id']['value'] : '';
15159
15160 if ( 'course_enrolled' == $term ) {
15161 if ( -1 === $course_id ) {
15162 $courses = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='access' AND activity.activity_status= %d order by activity_id DESC LIMIT 1", 0 ) );
15163 } else {
15164 $courses = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='access' AND activity.activity_status= %d AND activity.post_id= %d AND activity.course_id= %d order by activity_id DESC LIMIT 1", 0, $course_id, $course_id ) );
15165 }
15166 } elseif ( 'course_unenrolled' == $term ) {
15167 if ( -1 === $course_id ) {
15168 $courses = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='access' AND activity.activity_status= %d order by activity_id DESC LIMIT 1", 0 ) );
15169 $course_id = $courses[0]->course_id;
15170 }
15171 $args = [
15172 'meta_query' => [
15173 'relation' => 'AND',
15174 [
15175 'key' => 'course_' . $course_id . '_access_from',
15176 'compare' => 'NOT EXISTS',
15177 ],
15178 [
15179 'key' => 'learndash_course_' . $course_id . '_enrolled_at',
15180 'compare' => 'EXISTS',
15181 ],
15182 ],
15183 ];
15184 $users = get_users( $args );
15185 } elseif ( 'course_access_expired' == $term ) {
15186 if ( -1 === $course_id ) {
15187 $course_args = [
15188 'post_type' => 'sfwd-courses',
15189 'posts_per_page' => 1,
15190 'orderby' => 'rand',
15191 'order' => 'ASC',
15192 'post_status' => 'publish',
15193 ];
15194 $courses_posts = get_posts( $course_args );
15195 $course_id = $courses_posts[0]->ID;
15196 }
15197 $args = [
15198 'meta_query' => [
15199 [
15200 'key' => 'learndash_course_expired_' . $course_id,
15201 'compare' => 'EXISTS',
15202 ],
15203 ],
15204 ];
15205 $users = get_users( $args );
15206 } elseif ( 'group_course_completed' == $term ) {
15207 if ( -1 === $group_id ) {
15208 $courses = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='group_progress' AND activity.activity_status= %d order by activity_id DESC LIMIT 1", 1 ) );
15209 } else {
15210 $courses = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='group_progress' AND activity.activity_status= %d AND activity.post_id= %d order by activity_id DESC LIMIT 1", 1, $group_id ) );
15211 }
15212 $activity_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity_meta WHERE activity_id = %d", $courses[0]->activity_id ) );
15213 } elseif ( 'course_group_added' == $term ) {
15214 if ( -1 === $group_id ) {
15215 $args = [
15216 'numberposts' => 1,
15217 'fields' => 'ids',
15218 'orderby' => 'rand',
15219 'post_type' => 'groups',
15220 ];
15221 $posts = get_posts( $args );
15222 $random_key = array_rand( $posts );
15223 $random_value = $posts[ $random_key ];
15224 $group_id = $random_value;
15225 }
15226 $courses = learndash_group_enrolled_courses( $group_id );
15227 } elseif ( 'assignment_submitted' == $term ) {
15228 $course_id = isset( $data['filter']['sfwd-courses'] ) ? $data['filter']['sfwd-courses']['value'] : '';
15229 $lesson_id = $data['filter']['sfwd_lesson_topic_id']['value'];
15230 if ( -1 === $course_id && -1 === $lesson_id ) {
15231 $args = [
15232 'post_type' => 'sfwd-assignment',
15233 'posts_per_page' => 1,
15234 'order' => 'DESC',
15235 'post_status' => 'publish',
15236 ];
15237 } else {
15238 if ( -1 === $course_id ) {
15239 $courses = get_posts(
15240 [
15241 'posts_per_page' => - 1,
15242 'post_type' => 'sfwd-courses',
15243 'post_status' => 'publish',
15244 'fields' => 'ids',
15245 ]
15246 );
15247 $course_key = array_rand( $courses );
15248 $course_id = $courses[ $course_key ];
15249 }
15250 if ( -1 === $lesson_id ) {
15251 $args = [
15252 'post_type' => 'sfwd-assignment',
15253 'posts_per_page' => 1,
15254 'order' => 'DESC',
15255 'post_status' => 'publish',
15256 'meta_query' => [
15257 [
15258 'key' => 'course_id',
15259 'value' => $course_id,
15260 'compare' => '=',
15261 ],
15262 ],
15263 ];
15264 } else {
15265 $args = [
15266 'post_type' => 'sfwd-assignment',
15267 'posts_per_page' => 1,
15268 'order' => 'DESC',
15269 'post_status' => 'publish',
15270 'meta_query' => [
15271 'relation' => 'AND',
15272 [
15273 'key' => 'lesson_id',
15274 'value' => $lesson_id,
15275 'compare' => '=',
15276 ],
15277 [
15278 'key' => 'course_id',
15279 'value' => $course_id,
15280 'compare' => '=',
15281 ],
15282 ],
15283 ];
15284 }
15285 }
15286 $assignments = get_posts( $args );
15287 } elseif ( 'assignment_graded' == $term ) {
15288 $course_id = isset( $data['filter']['sfwd-courses'] ) ? $data['filter']['sfwd-courses']['value'] : '';
15289 $lesson_id = $data['filter']['sfwd_lesson_topic_id']['value'];
15290 if ( -1 === $course_id && -1 === $lesson_id ) {
15291 $args = [
15292 'post_type' => 'sfwd-assignment',
15293 'posts_per_page' => 1,
15294 'order' => 'DESC',
15295 'post_status' => 'publish',
15296 'meta_query' => [
15297 [
15298 'key' => 'approval_status',
15299 'compare' => 'EXISTS',
15300 ],
15301 ],
15302 ];
15303 } else {
15304 if ( -1 === $course_id ) {
15305 $courses = get_posts(
15306 [
15307 'posts_per_page' => - 1,
15308 'post_type' => 'sfwd-courses',
15309 'post_status' => 'publish',
15310 'fields' => 'ids',
15311 ]
15312 );
15313 $course_key = array_rand( $courses );
15314 $course_id = $courses[ $course_key ];
15315 }
15316 if ( -1 === $lesson_id ) {
15317 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
15318 if ( ! empty( $lessons ) ) {
15319 $random_key = array_rand( $lessons );
15320 $random_value = $lessons[ $random_key ];
15321 $lesson_id = $random_value->ID;
15322 }
15323 }
15324 $args = [
15325 'post_type' => 'sfwd-assignment',
15326 'posts_per_page' => 1,
15327 'order' => 'DESC',
15328 'post_status' => 'publish',
15329 'meta_query' => [
15330 'relation' => 'AND',
15331 [
15332 'key' => 'lesson_id',
15333 'value' => $lesson_id,
15334 'compare' => '=',
15335 ],
15336 [
15337 'key' => 'course_id',
15338 'value' => $course_id,
15339 'compare' => '=',
15340 ],
15341 [
15342 'key' => 'approval_status',
15343 'compare' => 'EXISTS',
15344 ],
15345 ],
15346 ];
15347 }
15348 $assignments = get_posts( $args );
15349 }
15350
15351 if ( 'assignment_submitted' == $term || 'assignment_graded' == $term ) {
15352 if ( ! empty( $assignments ) ) {
15353 $context_data = WordPress::get_user_context( (int) $assignments[0]->post_author );
15354 $context_data['assignment_id'] = $assignments[0]->ID;
15355 $context_data['assignment_title'] = $assignments[0]->post_title;
15356 $context_data['assignment_url'] = get_post_meta( $assignments[0]->ID, 'file_link', true );
15357 $context_data['lesson_id'] = get_post_meta( $assignments[0]->ID, 'lesson_id', true );
15358 $context_data['course_id'] = get_post_meta( $assignments[0]->ID, 'course_id', true );
15359 $context_data['points'] = get_post_meta( $assignments[0]->ID, 'points', true );
15360 $context['response_type'] = 'live';
15361 } else {
15362 $context_data = WordPress::get_sample_user_context();
15363 $context_data['assignment_title'] = 'Test Assignment';
15364 $context_data['assignment_id'] = 112;
15365 $context_data['assignment_url'] = 'https://example.com/test_assignment.pdf';
15366 $context_data['lesson_id'] = 2;
15367 $context_data['course_id'] = 1;
15368 $context_data['points'] = '12';
15369 $context['response_type'] = 'sample';
15370 }
15371 } elseif ( 'course_unenrolled' == $term || 'course_access_expired' == $term ) {
15372 if ( ! empty( $users ) ) {
15373 $context_data = WordPress::get_user_context( $users[0]->ID );
15374 $context_data['sfwd_course_id'] = $course_id;
15375 $context_data['course_title'] = get_the_title( $course_id );
15376 $context_data['course_url'] = get_permalink( $course_id );
15377 $context_data['course_featured_image_id'] = get_post_meta( $course_id, '_thumbnail_id', true );
15378 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course_id );
15379 $timestamp = ld_course_access_expires_on( $course_id, $users[0]->ID );
15380 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
15381 $date_format = get_option( 'date_format' );
15382 if ( is_string( $date_format ) ) {
15383 $context_data['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
15384 }
15385 $context['response_type'] = 'live';
15386 } else {
15387 $context_data = WordPress::get_sample_user_context();
15388 $context_data['course_name'] = 'Test Course';
15389 $context_data['sfwd_course_id'] = 112;
15390 $context_data['course_url'] = 'https://example.com/test-course';
15391 $context_data['course_featured_image_id'] = 113;
15392 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
15393 $context_data['course_access_expiry_date'] = '2023-10-20';
15394 $context['response_type'] = 'sample';
15395 }
15396 } elseif ( 'course_group_added' == $term ) {
15397 if ( ! empty( $courses ) ) {
15398 $context_data['course_id'] = $courses[0];
15399 $context_data['course_title'] = get_the_title( $courses[0] );
15400 $context_data['course_url'] = get_permalink( $courses[0] );
15401 $context_data['course_featured_image_id'] = get_post_meta( $courses[0], '_thumbnail_id', true );
15402 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $courses[0] );
15403 $context_data['sfwd_group_id'] = $group_id;
15404 $context_data['group_name'] = get_the_title( $group_id );
15405 $context['response_type'] = 'live';
15406 } else {
15407 $context_data = WordPress::get_sample_user_context();
15408 $context_data['course_name'] = 'Test Course';
15409 $context_data['sfwd_course_id'] = 112;
15410 $context_data['course_url'] = 'https://example.com/test-course';
15411 $context_data['course_featured_image_id'] = 113;
15412 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
15413 $context_data['course_access_expiry_date'] = '2023-10-20';
15414 $context_data['sfwd_group_id'] = 12;
15415 $context_data['group_name'] = 'Test Group';
15416 $context['response_type'] = 'sample';
15417 }
15418 } elseif ( 'group_course_completed' == $term ) {
15419 if ( ! empty( $courses ) && ! empty( $activity_meta ) ) {
15420 $context_data = WordPress::get_user_context( $courses[0]->user_id );
15421 $context_data['sfwd_group_id'] = $courses[0]->post_id;
15422 $context_data['group_title'] = get_the_title( $courses[0]->post_id );
15423 $context_data['group_url'] = get_permalink( $courses[0]->post_id );
15424 $context_data['group_featured_image_id'] = get_post_meta( $courses[0]->post_id, '_thumbnail_id', true );
15425 $context_data['group_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->post_id );
15426 $course_ids = null;
15427 foreach ( $activity_meta as $item ) {
15428 if ( 'course_ids' === $item->activity_meta_key ) {
15429 $course_ids = unserialize( $item->activity_meta_value );
15430 break;
15431 }
15432 }
15433 if ( ! empty( $course_ids ) && is_array( $course_ids ) ) {
15434 foreach ( $course_ids as $key => $course_id ) {
15435 if ( is_int( $course_id ) ) {
15436 $args = [
15437 'include' => [ $courses[0]->user_id ],
15438 'meta_query' => [
15439 [
15440 'key' => 'course_completed_' . $course_id,
15441 'compare' => 'EXISTS',
15442 ],
15443 ],
15444 ];
15445 $users = get_users( $args );
15446 if ( ! empty( $users ) ) {
15447 $context_data[ 'completed ' . $key ]['course_id'] = $course_id;
15448 $context_data[ 'completed ' . $key ]['course_title'] = get_the_title( $course_id );
15449 $context_data[ 'completed ' . $key ]['course_url'] = get_permalink( $course_id );
15450 $context_data[ 'completed ' . $key ]['course_featured_image_id'] = get_post_meta( $course_id, '_thumbnail_id', true );
15451 $context_data[ 'completed ' . $key ]['course_featured_image_url'] = get_the_post_thumbnail_url( $course_id );
15452 $timestamp = ld_course_access_expires_on( $course_id, $courses[0]->user_id );
15453 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
15454 $date_format = get_option( 'date_format' );
15455 if ( is_string( $date_format ) ) {
15456 $context_data[ 'completed ' . $key ]['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
15457 }
15458 }
15459 }
15460 }
15461 }
15462 $context['response_type'] = 'live';
15463 } else {
15464 $context_data = WordPress::get_sample_user_context();
15465 $context_data['sfwd_group_id'] = 112;
15466 $context_data['group_title'] = 'Test Group';
15467 $context_data['group_url'] = 113;
15468 $context_data['group_featured_image_id'] = 11;
15469 $context_data['group_featured_image_url'] = 'https://example.com/test-group-img';
15470 $context_data['completed 0'] = [
15471 'course_id' => 10,
15472 'course_title' => 'Test Course',
15473 'course_url' => 'https://example.com/test-course',
15474 'course_featured_image_id' => 14,
15475 'course_featured_image_url' => 'https://example.com/test-course-img',
15476 ];
15477 $context['response_type'] = 'sample';
15478 }
15479 } else {
15480 if ( ! empty( $courses ) ) {
15481 $context_data = WordPress::get_user_context( $courses[0]->user_id );
15482 $context_data['course_id'] = $courses[0]->course_id;
15483 $context_data['course_title'] = get_the_title( $courses[0]->course_id );
15484 $context_data['course_url'] = get_permalink( $courses[0]->course_id );
15485 $context_data['course_featured_image_id'] = get_post_meta( $courses[0]->course_id, '_thumbnail_id', true );
15486 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->course_id );
15487 $timestamp = ld_course_access_expires_on( $courses[0]->course_id, $courses[0]->user_id );
15488 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
15489 $date_format = get_option( 'date_format' );
15490 if ( is_string( $date_format ) ) {
15491 $context_data['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
15492 }
15493 if ( $courses[0]->post_id ) {
15494 $context_data['sfwd_group_id'] = $courses[0]->post_id;
15495 $context_data['group_title'] = get_the_title( $courses[0]->post_id );
15496 $context_data['group_url'] = get_permalink( $courses[0]->post_id );
15497 $context_data['group_featured_image_id'] = get_post_meta( $courses[0]->post_id, '_thumbnail_id', true );
15498 $context_data['group_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->post_id );
15499 }
15500 $context['response_type'] = 'live';
15501 } else {
15502 $context_data = WordPress::get_sample_user_context();
15503 $context_data['course_name'] = 'Test Course';
15504 $context_data['sfwd_course_id'] = 112;
15505 $context_data['course_url'] = 'https://example.com/test-course';
15506 $context_data['course_featured_image_id'] = 113;
15507 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
15508 $context_data['course_access_expiry_date'] = '2023-10-20';
15509 $context['response_type'] = 'sample';
15510 }
15511 }
15512
15513 $context['pluggable_data'] = $context_data;
15514 return $context;
15515 }
15516
15517 /**
15518 * Search LearnDash Lesson Topic List.
15519 *
15520 * @param array $data Search Params.
15521 *
15522 * @since 1.0.0
15523 *
15524 * @return array
15525 */
15526 public function search_ld_lessons_topics_list( $data ) {
15527 $options = [];
15528 $course_id = $data['dynamic']['sfwd-courses'];
15529
15530 if ( ! function_exists( 'learndash_get_lesson_list' ) || ! function_exists( 'learndash_get_topic_list' ) ) {
15531 return [];
15532 }
15533
15534 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
15535 foreach ( $lessons as $lesson ) {
15536 $options[] = [
15537 'label' => $lesson->post_title,
15538 'value' => $lesson->ID,
15539 ];
15540 $topics = learndash_get_topic_list( $lesson->ID, $course_id );
15541 foreach ( $topics as $topic ) {
15542 $options[] = [
15543 'label' => $topic->post_title,
15544 'value' => $topic->ID,
15545 ];
15546 }
15547 }
15548
15549 return [
15550 'options' => $options,
15551 'hasMore' => false,
15552 ];
15553 }
15554
15555 /**
15556 * Search LearnDash Quiz List.
15557 *
15558 * @param array $data Search Params.
15559 *
15560 * @since 1.0.0
15561 *
15562 * @return array
15563 */
15564 public function search_ld_quiz_list( $data ) {
15565 $options = [];
15566 $course_id = $data['dynamic']['sfwd-courses'];
15567 $lesson_id = $data['dynamic']['sfwd_lessons_topics'];
15568
15569 if ( ! function_exists( 'learndash_get_course_quiz_list' ) || ! function_exists( 'learndash_get_lesson_quiz_list' ) ) {
15570 return [];
15571 }
15572
15573 $quizzes = learndash_get_course_quiz_list( $course_id );
15574 $quizzes = array_merge( $quizzes, learndash_get_lesson_quiz_list( $lesson_id, null, $course_id ) );
15575 if ( ! empty( $quizzes ) ) {
15576 foreach ( $quizzes as $quiz ) {
15577 $options[] = [
15578 'label' => $quiz['post']->post_title,
15579 'value' => $quiz['post']->ID,
15580
15581 ];
15582 }
15583 }
15584
15585 return [
15586 'options' => $options,
15587 'hasMore' => false,
15588 ];
15589 }
15590
15591 /**
15592 * Search LearnDash Quiz Essay Question List.
15593 *
15594 * @param array $data Search Params.
15595 *
15596 * @since 1.0.0
15597 *
15598 * @return array
15599 */
15600 public function search_ld_quiz_essay_question_list( $data ) {
15601 $options = [];
15602 $quiz_id = $data['dynamic'];
15603
15604 if ( ! function_exists( 'learndash_get_quiz_questions' ) ) {
15605 return [];
15606 }
15607
15608 if ( 0 < $quiz_id ) {
15609 $quiz_question_ids = learndash_get_quiz_questions( $quiz_id );
15610 if ( ! empty( $quiz_question_ids ) ) {
15611 foreach ( $quiz_question_ids as $question_post_id => $question_pro_id ) {
15612 $question_type = get_post_meta( $question_post_id, 'question_type', true );
15613 if ( is_string( $question_type ) && 'essay' === $question_type ) {
15614 $title = html_entity_decode( get_the_title( $question_post_id ), ENT_QUOTES, 'UTF-8' );
15615 $options[] = [
15616 'label' => $title,
15617 'value' => $question_post_id,
15618 ];
15619 }
15620 }
15621 }
15622 }
15623
15624 return [
15625 'options' => $options,
15626 'hasMore' => false,
15627 ];
15628 }
15629
15630 /**
15631 * Search LearnDash Lessons List.
15632 *
15633 * @param array $data Search Params.
15634 *
15635 * @since 1.0.0
15636 *
15637 * @return array
15638 */
15639 public function search_ld_lessons_list( $data ) {
15640 $options = [];
15641 $course_id = $data['dynamic']['sfwd-courses'];
15642
15643 if ( ! function_exists( 'learndash_get_lesson_list' ) ) {
15644 return [];
15645 }
15646
15647 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
15648 foreach ( $lessons as $lesson ) {
15649 $options[] = [
15650 'label' => $lesson->post_title,
15651 'value' => $lesson->ID,
15652 ];
15653 }
15654 return [
15655 'options' => $options,
15656 'hasMore' => false,
15657 ];
15658 }
15659
15660 /**
15661 * Search LearnDash Topics List.
15662 *
15663 * @param array $data Search Params.
15664 *
15665 * @since 1.0.0
15666 *
15667 * @return array
15668 */
15669 public function search_ld_topics_list( $data ) {
15670 $options = [];
15671 $course_id = $data['dynamic']['sfwd-courses'];
15672 $lesson_id = $data['dynamic']['sfwd-lessons'];
15673
15674 if ( ! function_exists( 'learndash_get_topic_list' ) ) {
15675 return [];
15676 }
15677
15678 $topics = learndash_get_topic_list( $lesson_id, $course_id );
15679 foreach ( $topics as $topic ) {
15680 $options[] = [
15681 'label' => $topic->post_title,
15682 'value' => $topic->ID,
15683 ];
15684 }
15685 return [
15686 'options' => $options,
15687 'hasMore' => false,
15688 ];
15689 }
15690
15691 /**
15692 * Search LearnDash Assignments List.
15693 *
15694 * @param array $data Search Params.
15695 *
15696 * @since 1.0.0
15697 *
15698 * @return array
15699 */
15700 public function search_ld_assignments_list( $data ) {
15701 $options = [];
15702 $course_id = $data['dynamic']['sfwd-courses'];
15703 $lesson_id = $data['dynamic']['sfwd_lesson_topic_id'];
15704
15705 $args = [
15706 'post_type' => 'sfwd-assignment',
15707 'numberposts' => - 1,
15708 ];
15709 $meta_query = [];
15710 if ( ! empty( $course_id ) ) {
15711 $meta_query[] = [
15712 'key' => 'course_id',
15713 'value' => (int) $course_id,
15714 'compare' => '=',
15715 ];
15716 }
15717 if ( ! empty( $lesson_id ) ) {
15718 $meta_query[] = [
15719 'key' => 'lesson_id',
15720 'value' => (int) $lesson_id,
15721 'compare' => '=',
15722 ];
15723 }
15724 if ( ! empty( $meta_query ) ) {
15725 $args['meta_query'] = $meta_query;
15726 if ( count( $meta_query ) > 1 ) {
15727 $args['meta_query']['relation'] = 'AND';
15728 }
15729 }
15730 $assignments = get_posts( $args );
15731 foreach ( $assignments as $assignment ) {
15732 $options[] = [
15733 'label' => $assignment->post_title,
15734 'value' => $assignment->ID,
15735 ];
15736 }
15737 return [
15738 'options' => $options,
15739 'hasMore' => false,
15740 ];
15741 }
15742
15743 /**
15744 * Search post by post type.
15745 *
15746 * @param array $data Search Params.
15747 *
15748 * @since 1.0.0
15749 *
15750 * @return array
15751 */
15752 public function search_edd_prices( $data ) {
15753 $options = [];
15754 $downlaod_id = $data['dynamic']['download_id'];
15755
15756 $variable_prices = get_post_meta( $downlaod_id, 'edd_variable_prices', true );
15757 if ( ! empty( $variable_prices ) && is_array( $variable_prices ) ) {
15758 foreach ( $variable_prices as $price_id => $price ) {
15759 if ( isset( $price['name'] ) ) {
15760 $options[] = [
15761 'label' => $price['name'] . '(' . $price['amount'] . ')',
15762 'value' => $price['index'],
15763 ];
15764 }
15765 }
15766 }
15767
15768 return [
15769 'options' => $options,
15770 'hasMore' => false,
15771 ];
15772
15773 }
15774
15775 /**
15776 * GetPowerful Docs list.
15777 *
15778 * @param array $data data.
15779 *
15780 * @return array
15781 */
15782 public function search_pfd_docs_list( $data ) {
15783
15784 $course_id = $data['dynamic'];
15785 $page = $data['page'];
15786 $limit = Utilities::get_search_page_limit();
15787 $offset = $limit * ( $page - 1 );
15788 $options = [];
15789 $args = [
15790 'post_type' => 'docs',
15791 'posts_per_page' => $limit,
15792 'offset' => $offset,
15793 'orderby' => 'title',
15794 'order' => 'ASC',
15795 'post_status' => 'publish',
15796 ];
15797
15798 $docs = get_posts( $args );
15799 $docs_count = wp_count_posts( 'docs' )->publish;
15800 if ( ! empty( $docs ) ) {
15801 if ( is_array( $docs ) ) {
15802 foreach ( $docs as $doc ) {
15803 $options[] = [
15804 'label' => $doc->post_title,
15805 'value' => $doc->ID,
15806 ];
15807 }
15808 }
15809 }
15810 return [
15811 'options' => $options,
15812 'hasMore' => $docs_count > $limit && $docs_count > $offset,
15813 ];
15814 }
15815
15816 /**
15817 * Search Powerful Docs last data.
15818 *
15819 * @param array $data data.
15820 * @return array|void|mixed
15821 */
15822 public function search_pfd_feedback_last_data( $data ) {
15823 $sample_data = '{"pluggable_data":{"feedback": "no","questions": "- I need help with something else...","comment": "help me out!!","doc_id": "2409","time": "2024-09-11 11:56:48","doc_name": "Sample doc","doc_link": "https://example.com","doc_author_email": "john@example.com"},"response_type":"sample"}';
15824 $context = json_decode( $sample_data, true );
15825
15826 return $context;
15827 }
15828
15829 /**
15830 * WooCommerce Coupon Discount type list.
15831 *
15832 * @param array $data data.
15833 *
15834 * @return array
15835 */
15836 public function search_woo_coupon_discount_type_list( $data ) {
15837 $options = [];
15838
15839 $discount_types = wc_get_coupon_types();
15840
15841 if ( ! empty( $discount_types ) ) {
15842 if ( is_array( $discount_types ) ) {
15843 foreach ( $discount_types as $key => $value ) {
15844 $options[] = [
15845 'label' => $value,
15846 'value' => $key,
15847 ];
15848 }
15849 }
15850 }
15851
15852 return [
15853 'options' => $options,
15854 'hasMore' => false,
15855 ];
15856 }
15857
15858 /**
15859 * WooCommerce Product list along with variation list.
15860 *
15861 * @param array $data data.
15862 *
15863 * @return array|void
15864 */
15865 public function search_woo_product_variation_list( $data ) {
15866 $options = [];
15867
15868 $page = $data['page'];
15869 $limit = Utilities::get_search_page_limit();
15870 $offset = $limit * ( $page - 1 );
15871
15872 if ( ! function_exists( 'wc_get_products' ) ) {
15873 return;
15874 }
15875 $products = wc_get_products(
15876 [
15877 'posts_per_page' => $limit,
15878 'offset' => $offset,
15879 'orderby' => 'date',
15880 'order' => 'DESC',
15881 ]
15882 );
15883
15884
15885 $prod_count = wp_count_posts( 'product' )->publish;
15886
15887 if ( ! empty( $products ) ) {
15888 if ( is_array( $products ) ) {
15889 foreach ( $products as $product ) {
15890 $options[] = [
15891 'label' => $product->get_name(),
15892 'value' => $product->get_id(),
15893 ];
15894 if ( $product->is_type( 'variable' ) ) {
15895 $variations = Utilities::get_product_variations( $product->get_id() );
15896 foreach ( $variations['result'] as $variation ) {
15897 $options[] = [
15898 'label' => $variation->post_title,
15899 'value' => $variation->ID,
15900 ];
15901 }
15902 }
15903 }
15904 }
15905 }
15906 return [
15907 'options' => $options,
15908 'hasMore' => $prod_count > $limit && $prod_count > $offset,
15909 ];
15910 }
15911
15912 /**
15913 * WooCommerce Product list along with variation list.
15914 *
15915 * @param array $data data.
15916 *
15917 * @return array
15918 */
15919 public function search_woo_coupon_list( $data ) {
15920 $options = [];
15921
15922 $page = $data['page'];
15923 $limit = Utilities::get_search_page_limit();
15924 $offset = $limit * ( $page - 1 );
15925
15926 $coupons = get_posts(
15927 [
15928 'posts_per_page' => - 1,
15929 'orderby' => 'name',
15930 'order' => 'asc',
15931 'post_type' => 'shop_coupon',
15932 'post_status' => 'publish',
15933 ]
15934 );
15935 $coupon_count = wp_count_posts( 'shop_coupon' )->publish;
15936
15937 if ( ! empty( $coupons ) ) {
15938 if ( is_array( $coupons ) ) {
15939 foreach ( $coupons as $coupon ) {
15940 $code = wc_format_coupon_code( $coupon->post_title );
15941 $options[] = [
15942 'label' => $code,
15943 'value' => $code,
15944 ];
15945 }
15946 }
15947 }
15948
15949 return [
15950 'options' => $options,
15951 'hasMore' => $coupon_count > $limit && $coupon_count > $offset,
15952 ];
15953 }
15954
15955 /**
15956 * Prepare LatePoint Bookings List.
15957 *
15958 * @param array $data Search Params.
15959 * @return array
15960 */
15961 public function search_lp_bookings_list( $data ) {
15962
15963 if ( ! class_exists( 'OsBookingHelper' ) ) {
15964 return [];
15965 }
15966
15967 $bookings = OsBookingHelper::get_bookings_for_select();
15968 $options = [];
15969
15970 if ( ! empty( $bookings ) ) {
15971 foreach ( $bookings as $key => $booking ) {
15972 $options[] = [
15973 'label' => $booking['label'],
15974 'value' => $booking['value'],
15975 ];
15976 }
15977 }
15978
15979 return [
15980 'options' => $options,
15981 'hasMore' => false,
15982 ];
15983 }
15984
15985 /**
15986 * Prepare LatePoint Services List.
15987 *
15988 * @param array $data Search Params.
15989 * @return array
15990 */
15991 public function search_lp_services_list( $data ) {
15992
15993 if ( ! class_exists( 'OsServiceHelper' ) ) {
15994 return [];
15995 }
15996
15997 $services = OsServiceHelper::get_services_list();
15998 $options = [];
15999
16000 if ( ! empty( $services ) ) {
16001 foreach ( $services as $key => $service ) {
16002 $options[] = [
16003 'label' => $service['label'],
16004 'value' => $service['value'],
16005 ];
16006 }
16007 }
16008
16009 return [
16010 'options' => $options,
16011 'hasMore' => false,
16012 ];
16013 }
16014
16015 /**
16016 * Prepare LatePoint Agents List.
16017 *
16018 * @param array $data Search Params.
16019 * @return array
16020 */
16021 public function search_lp_agents_list( $data ) {
16022
16023 if ( ! class_exists( 'OsAgentHelper' ) ) {
16024 return [];
16025 }
16026
16027 $agent_ids_for_service = OsAgentHelper::get_agent_ids_for_service_and_location( $data['dynamic'] );
16028 $agents = OsAgentHelper::get_agents_list();
16029 $options = [];
16030
16031 if ( ! empty( $agents ) ) {
16032 foreach ( $agents as $key => $agent ) {
16033 if ( in_array( $agent['value'], $agent_ids_for_service ) ) {
16034 $options[] = [
16035 'label' => $agent['label'],
16036 'value' => $agent['value'],
16037 ];
16038 }
16039 }
16040 }
16041
16042 return [
16043 'options' => $options,
16044 'hasMore' => false,
16045 ];
16046 }
16047
16048 /**
16049 * Prepare LatePoint Agents WP User List.
16050 *
16051 * @param array $data Search Params.
16052 * @return array
16053 */
16054 public function search_lp_user_agents_list( $data ) {
16055
16056 if ( ! class_exists( 'OsWpUserHelper' ) ) {
16057 return [];
16058 }
16059 $agents = OsWpUserHelper::get_wp_users_for_select( [ 'role' => 'latepoint_agent' ] );
16060 $options = [];
16061 if ( ! empty( $agents ) ) {
16062 $options = $agents;
16063 }
16064 return [
16065 'options' => $options,
16066 'hasMore' => false,
16067 ];
16068 }
16069
16070 /**
16071 * Prepare LatePoint Statuses List.
16072 *
16073 * @param array $data Search Params.
16074 * @return array
16075 */
16076 public function search_lp_statuses_list( $data ) {
16077
16078 if ( ! class_exists( 'OsBookingHelper' ) ) {
16079 return [];
16080 }
16081
16082 $statuses = OsBookingHelper::get_statuses_list();
16083 $options = [];
16084
16085 if ( ! empty( $statuses ) ) {
16086 foreach ( $statuses as $key => $label ) {
16087 $options[] = [
16088 'label' => $label,
16089 'value' => $key,
16090 ];
16091 }
16092 }
16093
16094 return [
16095 'options' => $options,
16096 'hasMore' => false,
16097 ];
16098 }
16099
16100 /**
16101 * Prepare LatePoint Customers List.
16102 *
16103 * @param array $data Search Params.
16104 * @return array
16105 */
16106 public function search_lp_customers_list( $data ) {
16107
16108 if ( ! class_exists( 'OsCustomerHelper' ) ) {
16109 return [];
16110 }
16111
16112 $customers = OsCustomerHelper::get_customers_for_select();
16113 $options = [];
16114
16115 if ( ! empty( $customers ) ) {
16116 foreach ( $customers as $key => $customer ) {
16117 $options[] = [
16118 'label' => $customer['label'],
16119 'value' => $customer['value'],
16120 ];
16121 }
16122 }
16123
16124 return [
16125 'options' => $options,
16126 'hasMore' => false,
16127 ];
16128 }
16129
16130 /**
16131 * Prepare SureForms Forms List.
16132 *
16133 * @param array $data Search Params.
16134 * @return array
16135 */
16136 public function search_sureforms_form_list( $data ) {
16137
16138 $options = [];
16139
16140 $page = $data['page'];
16141 $limit = Utilities::get_search_page_limit();
16142 $offset = $limit * ( $page - 1 );
16143
16144 $forms = get_posts(
16145 [
16146 'posts_per_page' => - 1,
16147 'orderby' => 'name',
16148 'order' => 'asc',
16149 'post_type' => 'sureforms_form',
16150 'post_status' => 'publish',
16151 ]
16152 );
16153 $form_count = wp_count_posts( 'sureforms_form' )->publish;
16154
16155 if ( ! empty( $forms ) ) {
16156 if ( is_array( $forms ) ) {
16157 foreach ( $forms as $form ) {
16158 $title = html_entity_decode( get_the_title( $form->ID ), ENT_QUOTES, 'UTF-8' );
16159 $options[] = [
16160 'label' => $title,
16161 'value' => $form->ID,
16162 ];
16163 }
16164 }
16165 }
16166
16167 return [
16168 'options' => $options,
16169 'hasMore' => $form_count > $limit && $form_count > $offset,
16170 ];
16171 }
16172
16173 /**
16174 * Prepare Academy Course List.
16175 *
16176 * @param array $data Search Params.
16177 * @return array
16178 */
16179 public function search_ac_lms_courses( $data ) {
16180
16181 $options = [];
16182
16183 $page = $data['page'];
16184 $limit = Utilities::get_search_page_limit();
16185 $offset = $limit * ( $page - 1 );
16186
16187 $courses = get_posts(
16188 [
16189 'posts_per_page' => - 1,
16190 'orderby' => 'name',
16191 'order' => 'asc',
16192 'post_type' => 'academy_courses',
16193 'post_status' => 'publish',
16194 ]
16195 );
16196 $course_count = wp_count_posts( 'academy_courses' )->publish;
16197
16198 if ( ! empty( $courses ) ) {
16199 if ( is_array( $courses ) ) {
16200 foreach ( $courses as $course ) {
16201 $options[] = [
16202 'label' => get_the_title( $course->ID ),
16203 'value' => $course->ID,
16204 ];
16205 }
16206 }
16207 }
16208
16209 return [
16210 'options' => $options,
16211 'hasMore' => $course_count > $limit && $course_count > $offset,
16212 ];
16213 }
16214
16215 /**
16216 * Prepare Academy Lesson List.
16217 *
16218 * @param array $data Search Params.
16219 * @return array
16220 */
16221 public function search_ac_lms_lessons( $data ) {
16222
16223 $options = [];
16224
16225 if ( ! class_exists( '\Academy\Helper' ) ) {
16226 return [];
16227 }
16228
16229 $curriculums = \Academy\Helper::get_course_curriculum( $data['dynamic'] );
16230
16231 if ( ! empty( $curriculums ) ) {
16232 foreach ( $curriculums as $topic ) {
16233 if ( isset( $topic['topics'] ) && is_array( $topic['topics'] ) ) {
16234 foreach ( $topic['topics'] as $lesson ) {
16235 if ( isset( $lesson['type'] ) && 'lesson' === $lesson['type'] ) {
16236 $options[] = [
16237 'label' => $lesson['name'],
16238 'value' => $lesson['id'],
16239 ];
16240 }
16241 }
16242 }
16243 }
16244 }
16245
16246 return [
16247 'options' => $options,
16248 'hasMore' => false,
16249 ];
16250 }
16251
16252 /**
16253 * Prepare Academy Quiz List.
16254 *
16255 * @param array $data Search Params.
16256 * @return array
16257 */
16258 public function search_ac_lms_quiz( $data ) {
16259
16260 $options = [];
16261
16262 $page = $data['page'];
16263 $limit = Utilities::get_search_page_limit();
16264 $offset = $limit * ( $page - 1 );
16265
16266 $quizs = get_posts(
16267 [
16268 'posts_per_page' => - 1,
16269 'orderby' => 'name',
16270 'order' => 'asc',
16271 'post_type' => 'academy_quiz',
16272 'post_status' => 'publish',
16273 ]
16274 );
16275 $quiz_count = wp_count_posts( 'academy_quiz' )->publish;
16276
16277 if ( ! empty( $quizs ) ) {
16278 if ( is_array( $quizs ) ) {
16279 foreach ( $quizs as $quiz ) {
16280 $options[] = [
16281 'label' => get_the_title( $quiz->ID ),
16282 'value' => $quiz->ID,
16283 ];
16284 }
16285 }
16286 }
16287
16288 return [
16289 'options' => $options,
16290 'hasMore' => $quiz_count > $limit && $quiz_count > $offset,
16291 ];
16292 }
16293
16294 /**
16295 * Search Academy LMS data.
16296 *
16297 * @param array $data data.
16298 * @return array|void
16299 */
16300 public function search_ac_lms_last_data( $data ) {
16301 global $wpdb;
16302 $post_type = $data['post_type'];
16303 $trigger = $data['search_term'];
16304 $context = [];
16305
16306 if ( ! class_exists( '\Academy\Helper' ) ) {
16307 return [];
16308 }
16309
16310 $course_id = -1;
16311 $lesson_id = -1;
16312 $quiz_id = -1;
16313 if ( 'ac_lms_course_completed' === $trigger ) {
16314 $course_id = $data['filter']['course']['value'];
16315 } elseif ( 'ac_lms_lesson_completed' === $trigger ) {
16316 $course_id = $data['filter']['course']['value'];
16317 $lesson_id = $data['filter']['lesson']['value'];
16318 } elseif ( 'ac_lms_quiz_completed' === $trigger || 'ac_lms_quiz_failed' === $trigger ) {
16319 $quiz_id = $data['filter']['quiz']['value'];
16320 } elseif ( 'ac_lms_enrolled_course' === $trigger ) {
16321 $course_id = $data['filter']['course']['value'];
16322 }
16323
16324 $users = get_users(
16325 [
16326 'fields' => 'ID',
16327 'meta_key' => 'is_academy_student',
16328 ]
16329 );
16330 if ( ! empty( $users ) ) {
16331 $user_random_key = array_rand( $users );
16332 $user_id = $users[ $user_random_key ];
16333 }
16334
16335 if ( 'ac_lms_course_completed' === $trigger ) {
16336 if ( -1 === $course_id ) {
16337 $result = $wpdb->get_results(
16338 $wpdb->prepare(
16339 "SELECT * FROM {$wpdb->prefix}comments
16340 as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.comment_post_ID WHERE postmeta.comment_type='course_completed' AND posts.post_type=%s order by postmeta.comment_ID DESC LIMIT 1",
16341 $post_type
16342 )
16343 );
16344 } else {
16345 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}comments as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.comment_post_ID WHERE postmeta.comment_post_ID = %s AND postmeta.comment_type='course_completed' AND posts.post_type=%s order by postmeta.comment_ID DESC LIMIT 1", $course_id, $post_type ) );
16346 }
16347 } elseif ( 'ac_lms_lesson_completed' === $trigger ) {
16348 if ( -1 === $course_id ) {
16349 $courses = get_posts(
16350 [
16351 'posts_per_page' => - 1,
16352 'post_type' => 'academy_courses',
16353 'post_status' => 'publish',
16354 'fields' => 'ids',
16355 ]
16356 );
16357 $course_id = array_rand( $courses );
16358 $option_name = 'academy_course_' . $course_id . '_completed_topics';
16359 } else {
16360 $option_name = 'academy_course_' . $course_id . '_completed_topics';
16361 }
16362 if ( ! empty( $users ) ) {
16363 $meta = get_user_meta( $user_id, $option_name, true );
16364 if ( is_string( $meta ) ) {
16365 $saved_topics_lists = (array) json_decode( $meta, true );
16366 if ( -1 === $lesson_id ) {
16367 $result = $saved_topics_lists['lesson'];
16368 } else {
16369 if ( is_array( $saved_topics_lists['lesson'] ) ) {
16370 if ( array_key_exists( $lesson_id, $saved_topics_lists['lesson'] ) ) {
16371 $result = $saved_topics_lists['lesson'];
16372 }
16373 }
16374 }
16375 }
16376 }
16377 } elseif ( 'ac_lms_quiz_completed' === $trigger ) {
16378 if ( -1 === $quiz_id ) {
16379 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE attempt_status='passed' order by attempt_id DESC LIMIT 1" );
16380 } else {
16381 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE quiz_id=%s AND attempt_status='passed' order by attempt_id DESC LIMIT 1", $quiz_id ) );
16382 }
16383 } elseif ( 'ac_lms_quiz_failed' === $trigger ) {
16384 if ( -1 === $quiz_id ) {
16385 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE attempt_status='failed' order by attempt_id DESC LIMIT 1" );
16386 } else {
16387 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE quiz_id=%s AND attempt_status='failed' order by attempt_id DESC LIMIT 1", $quiz_id ) );
16388 }
16389 } elseif ( 'ac_lms_enrolled_course' === $trigger ) {
16390 if ( -1 === $course_id ) {
16391 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type = %s order by ID DESC LIMIT 1", 'academy_enrolled' ) );
16392 } else {
16393 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type = %s AND post_parent = %d order by ID DESC LIMIT 1", 'academy_enrolled', $course_id ) );
16394 }
16395 }
16396
16397 if ( ! empty( $result ) ) {
16398 switch ( $trigger ) {
16399 case 'ac_lms_course_completed':
16400 $data = WordPress::get_post_context( $result[0]->comment_post_ID );
16401 $context_data = WordPress::get_user_context( $result[0]->user_id );
16402 $context_data['course_data'] = $data;
16403 $context_data['course'] = $result[0]->comment_post_ID;
16404 break;
16405 case 'ac_lms_enrolled_course':
16406 $data = WordPress::get_post_context( $result[0]->post_parent );
16407 $context_data = WordPress::get_user_context( $result[0]->post_author );
16408 $context_data['course_data'] = $data;
16409 $context_data['enrollment_data'] = $result[0];
16410 $context_data['course'] = $result[0]->post_parent;
16411 break;
16412 case 'ac_lms_lesson_completed':
16413 if ( -1 === $lesson_id ) {
16414 $key = array_rand( $result );
16415 $lesson_data = \Academy\Helper::get_lesson( $key );
16416 } else {
16417 $lesson_data = \Academy\Helper::get_lesson( $lesson_id );
16418 }
16419 if ( is_object( $lesson_data ) ) {
16420 $lesson_data = get_object_vars( $lesson_data );
16421 }
16422 if ( ! empty( $users ) ) {
16423 $context_data = array_merge( $lesson_data, WordPress::get_user_context( $user_id ) );
16424 }
16425 $context_data['course_data'] = WordPress::get_post_context( $course_id );
16426 $context_data['lesson'] = $lesson_id;
16427 $context_data['course'] = $course_id;
16428 break;
16429 case 'ac_lms_quiz_completed':
16430 case 'ac_lms_quiz_failed':
16431 $context_data = WordPress::get_user_context( $result[0]->user_id );
16432 $context_data['quiz_data'] = WordPress::get_post_context( $result[0]->quiz_id );
16433 $context_data['quiz_attempt_details'] = $result;
16434 $context_data['quiz'] = $result[0]->quiz_id;
16435 break;
16436 default:
16437 return;
16438 }
16439 $context['pluggable_data'] = $context_data;
16440 $context['response_type'] = 'live';
16441 } elseif ( empty( $result ) ) {
16442 switch ( $trigger ) {
16443 case 'ac_lms_course_completed':
16444 $sample_data = '{"pluggable_data":{"wp_user_id": 73, "user_login": "abc@yopmail.com","display_name": "data data1", "user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"course_data": {"ID": 6949,"post_author": "1","post_date": "2023-11-29 05:37:50","post_date_gmt": "2023-11-29 05:37:50","post_content": "<!-- wp:paragraph -->\n<p>this is a business course.<\/p>\n<!-- \/wp:paragraph -->","post_title": "Business Course","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "open","post_password": "","post_name": "business-course","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:27","post_modified_gmt": "2023-11-29 09:50:27","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/course\/business-course\/","menu_order": 0,"post_type": "academy_courses","post_mime_type": "","comment_count": "0","filter": "raw"},"course": 1},"response_type":"sample"}';
16445 break;
16446 case 'ac_lms_lesson_completed':
16447 $sample_data = '{"pluggable_data":{"ID": "1","lesson_author": "1","lesson_date": "2023-11-29 07:01:03","lesson_date_gmt": "2023-11-29 07:01:03","lesson_title": "Lesson 1","lesson_name": "","lesson_content": "","lesson_excerpt": "","lesson_status": "publish","comment_status": "close","comment_count": "0","lesson_password": "","lesson_modified": "2023-11-29 07:01:03","lesson_modified_gmt": "2023-11-29 07:01:03","wp_user_id": 73,"user_login": "abc@yopmail.com","display_name": "data data1","user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"course_data": {"ID": 6949,"post_author": "1","post_date": "2023-11-29 05:37:50","post_date_gmt": "2023-11-29 05:37:50","post_content": "<!-- wp:paragraph -->\n<p>this is a business course.<\/p>\n<!-- \/wp:paragraph -->","post_title": "Business Course","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "open","post_password": "","post_name": "business-course","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:27","post_modified_gmt": "2023-11-29 09:50:27","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/course\/business-course\/","menu_order": 0,"post_type": "academy_courses","post_mime_type": "","comment_count": "0","filter": "raw"},"lesson": 1,"course":1},"response_type":"sample"}';
16448 break;
16449 case 'ac_lms_quiz_completed':
16450 $sample_data = '{"pluggable_data":{"wp_user_id": 73,"user_login": "abc@yopmail.com","display_name": "data data1","user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"quiz_data": {"ID": 6960,"post_author": "1","post_date": "2023-11-29 09:49:42","post_date_gmt": "2023-11-29 09:49:42","post_content": "","post_title": "First Quiz","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "closed","post_password": "","post_name": "first-quiz","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:11","post_modified_gmt": "2023-11-29 09:50:11","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/?post_type=academy_quiz&#038;p=6960","menu_order": 0,"post_type": "academy_quiz","post_mime_type": "","comment_count": "0","filter": "raw"}, "quiz_attempt_details": [{"attempt_id": "2","course_id": "6949","quiz_id": "6960","user_id": "126","total_questions": "1","total_answered_questions": "1","total_marks": "20.00","earned_marks": "20.00","attempt_info": "{\"total_correct_answers\":1}","attempt_status": "passed","attempt_ip": "127.0.0.1","attempt_started_at": "2023-11-30 06:20:10","attempt_ended_at": "2023-11-30 06:20:10","is_manually_reviewed": null,"manually_reviewed_at": null}],"quiz":1},"response_type":"sample"}';
16451 break;
16452 case 'ac_lms_quiz_failed':
16453 $sample_data = '{"pluggable_data":{"wp_user_id": 73,"user_login": "abc@yopmail.com","display_name": "data data1","user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"quiz_data": {"ID": 6960,"post_author": "1","post_date": "2023-11-29 09:49:42","post_date_gmt": "2023-11-29 09:49:42","post_content": "","post_title": "First Quiz","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "closed","post_password": "","post_name": "first-quiz","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:11","post_modified_gmt": "2023-11-29 09:50:11","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/?post_type=academy_quiz&#038;p=6960","menu_order": 0,"post_type": "academy_quiz","post_mime_type": "","comment_count": "0","filter": "raw"}, "quiz_attempt_details": [{"attempt_id": "2","course_id": "6949","quiz_id": "6960","user_id": "126","total_questions": "1","total_answered_questions": "1","total_marks": "20.00","earned_marks": "0.00","attempt_info": "{\"total_correct_answers\":0}","attempt_status": "failed","attempt_ip": "127.0.0.1","attempt_started_at": "2023-11-30 06:20:10","attempt_ended_at": "2023-11-30 06:20:10","is_manually_reviewed": null,"manually_reviewed_at": null}],"quiz":1},"response_type":"sample"}';
16454 break;
16455 case 'ac_lms_enrolled_course':
16456 $sample_data = '{"pluggable_data":{"course_data": {"ID": 6949,"post_author": "1","post_date": "2023-11-29 05:37:50","post_date_gmt": "2023-11-29 05:37:50","post_content": "<!-- wp:paragraph -->\n<p>this is a business course.<\/p>\n<!-- \/wp:paragraph -->","post_title": "Business Course","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "open","post_password": "","post_name": "business-course","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:27","post_modified_gmt": "2023-11-29 09:50:27","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/course\/business-course\/","menu_order": 0,"post_type": "academy_courses","post_mime_type": "","comment_count": "0","filter": "raw"},"enrollment_data": {"ID": "6971","post_author": "126","post_date": "2023-11-30 05:52:54","post_date_gmt": "2023-11-30 05:52:54","post_content": "","post_title": "Course Enrolled November 30, 2023 @ 5:52 am","post_excerpt": "","post_status": "completed","comment_status": "closed","ping_status": "closed","post_password": "","post_name": "course-enrolled-november-30-2023-552-am","to_ping": "","pinged": "","post_modified": "2023-11-30 05:52:54","post_modified_gmt": "2023-11-30 05:52:54","post_content_filtered": "","post_parent": "6949","guid": "https:\/\/suretriggers-wpnew.local\/?p=6971","menu_order": "0","post_type": "academy_enrolled","post_mime_type": "","comment_count": "0"},"course":1, "wp_user_id": 2,"user_login": "test","display_name": "test test","user_firstname": "test","user_lastname": "test", "user_email": "test@yopmail.com","user_role": ["academy_student"]},"response_type":"sample"}';
16457 break;
16458 default:
16459 return;
16460 }
16461 $context = (array) json_decode( $sample_data, true );
16462 }
16463 return $context;
16464 }
16465
16466 /**
16467 * Search myCred Point Type List.
16468 *
16469 * @param array $data Search Params.
16470 * @return array
16471 */
16472 public function search_mycred_point_type_list( $data ) {
16473
16474 $options = [];
16475
16476 if ( ! function_exists( 'mycred_get_types' ) ) {
16477 return [];
16478 }
16479
16480 $posts = mycred_get_types();
16481
16482 if ( ! empty( $posts ) ) {
16483 if ( is_array( $posts ) ) {
16484 foreach ( $posts as $key => $post ) {
16485 $options[] = [
16486 'label' => $post,
16487 'value' => $key,
16488 ];
16489 }
16490 }
16491 }
16492
16493 return [
16494 'options' => $options,
16495 'hasMore' => false,
16496 ];
16497 }
16498
16499 /**
16500 * Prepare elementor forms.
16501 *
16502 * @param array $data Search Params.
16503 *
16504 * @return array
16505 */
16506 public function search_new_elementor_form_fields( $data ) {
16507
16508 $fields = [];
16509 $select_form_id = $data['dynamic'];
16510 global $wpdb;
16511 $post_metas = $wpdb->get_results(
16512 $wpdb->prepare(
16513 "SELECT pm.post_id, pm.meta_value
16514 FROM $wpdb->postmeta pm
16515 LEFT JOIN $wpdb->posts p
16516 ON p.ID = pm.post_id
16517 WHERE p.post_type IS NOT NULL
16518 AND p.post_status = %s
16519 AND pm.meta_key = %s
16520 AND pm.`meta_value` LIKE %s",
16521 'publish',
16522 '_elementor_data',
16523 '%%form_fields%%'
16524 )
16525 );
16526
16527 if ( ! empty( $post_metas ) ) {
16528 foreach ( $post_metas as $post_meta ) {
16529 /**
16530 *
16531 * Ignore line
16532 *
16533 * @phpstan-ignore-next-line
16534 */
16535 $inner_forms = Utilities::search_elementor_forms( json_decode( $post_meta->meta_value ) );
16536 if ( ! empty( $inner_forms ) ) {
16537 foreach ( $inner_forms as $form ) {
16538 $form_id = explode( '_', $select_form_id );
16539 if ( is_object( $form ) ) {
16540 if ( $form->id == $form_id[1] ) {
16541 if ( ! empty( $form->settings->form_fields ) ) {
16542 foreach ( $form->settings->form_fields as $field ) {
16543 $fields[] = [
16544 'value' => $field->custom_id,
16545 'text' => ! empty( $field->field_label ) ? $field->field_label : 'unknown',
16546 ];
16547 }
16548 }
16549 }
16550 }
16551 }
16552 }
16553 }
16554 }
16555 $options = [];
16556 if ( ! empty( $fields ) ) {
16557 foreach ( $fields as $key => $value ) {
16558 $options[] = [
16559 'label' => $value['text'],
16560 'value' => $value['value'],
16561 ];
16562 }
16563 }
16564 return [
16565 'options' => $options,
16566 'hasMore' => false,
16567 ];
16568 }
16569 /**
16570 * Get Fluent Booking Appointment Events.
16571 *
16572 * @param array $data data.
16573 *
16574 * @return array
16575 */
16576 public function search_fluent_booking_calendars_list( $data ) {
16577
16578 global $wpdb;
16579
16580 $page = $data['page'];
16581 $limit = Utilities::get_search_page_limit();
16582 $offset = $limit * ( $page - 1 );
16583
16584 $calendars = $wpdb->get_results(
16585 $wpdb->prepare(
16586 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendars WHERE status = %s ORDER BY id DESC LIMIT %d OFFSET %d",
16587 [ 'active', $limit, $offset ]
16588 ),
16589 OBJECT
16590 );
16591
16592 $calendars_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
16593
16594 $options = [];
16595 if ( ! empty( $calendars ) ) {
16596 foreach ( $calendars as $calendar ) {
16597 $options[] = [
16598 'label' => $calendar->title,
16599 'value' => $calendar->id,
16600 ];
16601 }
16602 }
16603 return [
16604 'options' => $options,
16605 'hasMore' => $calendars_count > $limit && $calendars_count > $offset,
16606 ];
16607
16608 }
16609
16610 /**
16611 * Get Fluent Booking Appointment Events.
16612 *
16613 * @param array $data data.
16614 *
16615 * @return array
16616 */
16617 public function search_fluent_booking_events_list( $data ) {
16618
16619 global $wpdb;
16620 $page = $data['page'];
16621 $limit = Utilities::get_search_page_limit();
16622 $offset = $limit * ( $page - 1 );
16623 $calendar_id = sanitize_text_field( $data['dynamic']['calender_id'] );
16624 if ( '-1' === $calendar_id ) {
16625 $events = $wpdb->get_results(
16626 $wpdb->prepare(
16627 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendar_events WHERE status = %s ORDER BY id DESC LIMIT %d OFFSET %d",
16628 [ 'active', $limit, $offset ]
16629 ),
16630 OBJECT
16631 );
16632 } else {
16633 $events = $wpdb->get_results(
16634 $wpdb->prepare(
16635 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendar_events WHERE status = %s AND calendar_id = %d ORDER BY id DESC LIMIT %d OFFSET %d",
16636 [ 'active', $calendar_id, $limit, $offset ]
16637 ),
16638 OBJECT
16639 );
16640 }
16641
16642 $events_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
16643
16644 $options = [];
16645 if ( ! empty( $events ) ) {
16646 foreach ( $events as $event ) {
16647 $options[] = [
16648 'label' => $event->title,
16649 'value' => $event->id,
16650 ];
16651 }
16652 }
16653 return [
16654 'options' => $options,
16655 'hasMore' => $events_count > $limit && $events_count > $offset,
16656 ];
16657
16658 }
16659
16660 /**
16661 * Get Fluent Booking last data.
16662 *
16663 * @param array $data data.
16664 *
16665 * @return array
16666 */
16667 public function search_fluent_booking_last_data( $data ) {
16668 global $wpdb;
16669 $trigger = $data['search_term'];
16670 $course_data = [];
16671 $lesson_data = [];
16672
16673 $sample_data = '{"pluggable_data":{"booking":{"id":"7","hash":"dc641fd972e27be079945e3f998def39","calendar_id":"1","event_id":"1","group_id":"7","fcrm_id":null,"parent_id":null,"host_user_id":"1","person_user_id":"1","person_contact_id":null,"person_time_zone":"Asia/Calcutta","start_time":"2024-01-29 11:00:00","end_time":"2024-01-29 11:15:00","slot_minutes":"15","first_name":"Sure","last_name":"Test","email":"dev-email@wpengine.local","message":"","internal_note":null,"phone":"","country":null,"ip_address":"","browser":null,"device":null,"other_info":null,"location_details":{"type":"in_person_guest","description":"Location description."},"cancelled_by":"1","status":"completed","source":"web","booking_type":"scheduling","event_type":"single","payment_status":null,"payment_method":null,"source_url":"https://connector.com/fluent-booking/","source_id":null,"utm_source":"","utm_medium":"","utm_campaign":"","utm_term":"","created_at":"2024-01-29 06:46:22","updated_at":"2024-01-29 06:57:42","custom_fields":null},"event":{"id":"1","hash":"38c65708b797f5d6956070896f792b34","user_id":"1","calendar_id":"1","duration":"15","title":"One to one","slug":"15min","media_id":null,"description":"","settings":{"schedule_type":"weekly_schedules","weekly_schedules":{"sun":{"enabled":false,"slots":[]},"mon":{"enabled":true,"slots":[{"start":"03:30","end":"11:30"}]},"tue":{"enabled":true,"slots":[{"start":"03:30","end":"11:30"}]},"wed":{"enabled":true,"slots":[{"start":"03:30","end":"11:30"}]},"thu":{"enabled":true,"slots":[{"start":"03:30","end":"11:30"}]},"fri":{"enabled":true,"slots":[{"start":"03:30","end":"11:30"}]},"sat":{"enabled":false,"slots":[]}},"date_overrides":[],"range_type":"range_days","range_days":60,"range_date_between":["",""],"schedule_conditions":{"value":4,"unit":"hours"},"location_fields":{"conferencing":{"label":"Conferencing","options":{"google_meet":{"title":"Google Meet (Connect Google Meet First)","disabled":true,"location_type":"conferencing"},"ms_teams":{"title":"MS Teams (Connect Outlook First)","disabled":true,"location_type":"conferencing"}}},"in_person":{"label":"In Person","options":{"in_person_guest":{"title":"In Person (Attendee Address)"},"in_person_organizer":{"title":"In Person (Organizer Address)"}}},"phone":{"label":"Phone","options":{"phone_guest":{"title":"Attendee Phone Number"},"phone_organizer":{"title":"Organizer Phone Number"}}},"online":{"label":"Online","options":{"online_meeting":{"title":"Online Meeting"}}},"other":{"label":"Other","options":{"custom":{"title":"Custom"}}}},"team_members":[],"custom_redirect":{"enabled":false,"redirect_url":"","is_query_string":"no","query_string":""},"multi_duration":{"enabled":true,"default_duration":"15","available_durations":["15","30","45"]}},"availability_type":"existing_schedule","availability_id":"1","status":"active","type":"free","color_schema":"#0099ff","location_type":"","location_heading":"","location_settings":[{"type":"in_person_guest","title":"In Person (Attendee Address)","display_on_booking":""}],"event_type":"single","is_display_spots":"0","max_book_per_slot":"1","created_at":"2024-01-28 08:35:29","updated_at":"2024-01-28 08:36:06"}},"response_type":"sample"}'; //phpcs:ignore
16674 $context = [
16675 'pluggable_data' => json_decode( $sample_data, true ),
16676 'response_type' => 'sample',
16677 ];
16678 if ( 'fluent_booking_appointment_cancelled' === $trigger ) {
16679 $status = 'cancelled';
16680 } elseif ( 'fluent_booking_appointment_completed' === $trigger ) {
16681 $status = 'completed';
16682 }
16683
16684 $event_id = (int) $data['filter']['event_id']['value'];
16685 if ( 'fluent_booking_appointment_cancelled' === $trigger || 'fluent_booking_appointment_completed' === $trigger ) {
16686 if ( $event_id > 0 ) {
16687 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE event_id= %d AND status= %s ORDER BY id DESC LIMIT 1", $event_id, $status ) );
16688 } else {
16689 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE status= %s ORDER BY id DESC LIMIT 1", $status ) );
16690 }
16691 } elseif ( 'fluent_booking_new_appointment_booked' === $trigger ) {
16692 if ( $event_id > 0 ) {
16693 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE event_id= %d AND status != %s ORDER BY id DESC LIMIT 1", $event_id, 'cancelled' ) );
16694 } else {
16695 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE status != %s ORDER BY id DESC LIMIT 1", 'cancelled' ) );
16696 }
16697 }
16698
16699 if ( ! empty( $booking ) ) {
16700 $booking_meta = $wpdb->get_row( $wpdb->prepare( "SELECT value FROM {$wpdb->prefix}fcal_booking_meta WHERE booking_id= %d AND meta_key= 'custom_fields_data'", $booking->id ) );
16701
16702 $booking_event = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_calendar_events WHERE id= %d", $booking->event_id ) );
16703 $booking_data = $booking;
16704 $booking_data->custom_fields = $booking_meta->value;
16705 $booking_array = [
16706 'booking' => $booking_data,
16707 'event' => $booking_event,
16708 ];
16709
16710
16711 $booking_array_response = self::recursive_unserialize( $booking_array );
16712 $context['pluggable_data'] = $booking_array_response;
16713 $context['response_type'] = 'live';
16714 }
16715 return $context;
16716 }
16717
16718 /**
16719 * Recursive unserilize.
16720 *
16721 * @param array $data data.
16722 *
16723 * @return array|mixed
16724 */
16725 public static function recursive_unserialize( $data ) {
16726 if ( is_array( $data ) ) {
16727 foreach ( $data as $key => $value ) {
16728 $data[ $key ] = self::recursive_unserialize( $value );
16729 }
16730 return $data;
16731 } elseif ( is_object( $data ) && 'stdClass' === get_class( $data ) ) {
16732 foreach ( $data as $property => $value ) {
16733 $data->$property = self::recursive_unserialize( $value );
16734 }
16735 return $data;
16736 } elseif ( is_string( $data ) && self::is_serialized( strval( $data ) ) ) {
16737 return unserialize( $data );
16738 } else {
16739 return $data;
16740 }
16741
16742 }
16743
16744 /**
16745 * Check if string serialized.
16746 *
16747 * @param string $data data.
16748 *
16749 * @return bool
16750 */
16751 public static function is_serialized( $data ) {
16752 $unserialized = unserialize( $data );
16753 if ( 'b:0;' === $data || false !== $unserialized ) {
16754 return true;
16755 } else {
16756 return false;
16757 }
16758 }
16759
16760 /**
16761 * Search Tutor LMS data.
16762 *
16763 * @param array $data data.
16764 * @return array|void|mixed
16765 */
16766 public function search_tutor_lms_last_data( $data ) {
16767 global $wpdb;
16768 $post_type = $data['post_type'];
16769 $trigger = $data['search_term'];
16770 $context = [];
16771
16772 if ( ! function_exists( 'tutor_utils' ) ) {
16773 return [];
16774 }
16775
16776 $post_id = -1;
16777 if ( 'course_enrolled' === $trigger || 'tutor_courses_question' === $trigger ) {
16778 $post_id = $data['filter']['tutor_course']['value'];
16779 } elseif ( 'quiz_attempt_percentage' === $trigger || 'quiz_failed' === $trigger || 'quiz_passed' === $trigger ) {
16780 $post_id = $data['filter']['quiz_id']['value'];
16781 }
16782
16783 if ( 'course_enrolled' === $trigger ) {
16784 if ( -1 === $post_id ) {
16785 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_status='completed' AND post_type=%s order by ID DESC LIMIT 1", $post_type ) );
16786 } else {
16787 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_parent = %s AND post_status='completed' AND post_type=%s order by ID DESC LIMIT 1", $post_id, $post_type ) );
16788 }
16789 } elseif ( 'tutor_courses_question' === $trigger ) {
16790 if ( -1 === $post_id ) {
16791 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}comments as comment JOIN {$wpdb->prefix}posts as posts ON posts.ID=comment.comment_post_ID WHERE comment.comment_approved='approved' AND comment.comment_type='tutor_q_and_a' AND posts.post_type=%s order by comment.comment_ID DESC LIMIT 1", $post_type ) );
16792 } else {
16793 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}comments as comment JOIN {$wpdb->prefix}posts as posts ON posts.ID=comment.comment_post_ID WHERE comment.comment_post_ID = %s AND comment.comment_approved='approved' AND comment.comment_type='tutor_q_and_a' AND posts.post_type=%s order by comment.comment_ID DESC LIMIT 1", $post_id, $post_type ) );
16794 }
16795 } elseif ( 'quiz_passed' === $trigger ) {
16796 if ( -1 === $post_id ) {
16797 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts as quiz JOIN {$wpdb->prefix}posts as posts ON posts.ID=quiz.quiz_id WHERE quiz.attempt_status='attempt_ended' AND quiz.earned_marks >= quiz.total_marks AND posts.post_type=%s order by quiz.attempt_id DESC LIMIT 1", $post_type ) );
16798 } else {
16799 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts as quiz JOIN {$wpdb->prefix}posts as posts ON posts.ID=quiz.quiz_id WHERE quiz.quiz_id = %s AND quiz.attempt_status='attempt_ended' AND quiz.earned_marks >= quiz.total_marks AND posts.post_type=%s order by quiz.attempt_id DESC LIMIT 1", $post_id, $post_type ) );
16800 }
16801 } elseif ( 'quiz_failed' === $trigger ) {
16802 if ( -1 === $post_id ) {
16803 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts as quiz JOIN {$wpdb->prefix}posts as posts ON posts.ID=quiz.quiz_id WHERE quiz.attempt_status='attempt_ended' AND quiz.earned_marks < quiz.total_marks AND posts.post_type=%s order by quiz.attempt_id DESC LIMIT 1", $post_type ) );
16804 } else {
16805 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts as quiz JOIN {$wpdb->prefix}posts as posts ON posts.ID=quiz.quiz_id WHERE quiz.quiz_id = %s AND quiz.attempt_status='attempt_ended' AND quiz.earned_marks < quiz.total_marks AND posts.post_type=%s order by quiz.attempt_id DESC LIMIT 1", $post_id, $post_type ) );
16806 }
16807 } elseif ( 'quiz_attempt_percentage' == $trigger ) {
16808 $condition_compare = $data['filter']['condition_compare']['value'];
16809 $percentage = $data['filter']['percentage']['value'];
16810 if ( -1 === $post_id ) {
16811 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts as quiz JOIN {$wpdb->prefix}posts as posts ON posts.ID=quiz.quiz_id WHERE earned_marks $condition_compare %d AND quiz.attempt_status='attempt_ended' AND posts.post_type=%s order by quiz.attempt_id DESC LIMIT 1", $percentage, $post_type ) ); //phpcs:ignore
16812 } else {
16813 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts as quiz JOIN {$wpdb->prefix}posts as posts ON posts.ID=quiz.quiz_id WHERE earned_marks $condition_compare %d AND quiz.quiz_id = %s AND quiz.attempt_status='attempt_ended' AND posts.post_type=%s order by quiz.attempt_id DESC LIMIT 1", $percentage, $post_id, $post_type ) ); //phpcs:ignore
16814 }
16815 }
16816
16817 if ( ! empty( $result ) ) {
16818 switch ( $trigger ) {
16819 case 'course_enrolled':
16820 $result_item_id = $result[0]->post_parent;
16821 $result_user_id = $result[0]->post_author;
16822 $context_data = array_merge(
16823 WordPress::get_user_context( $result_user_id ),
16824 WordPress::get_post_context( $result_item_id )
16825 );
16826 $context_data['tutor_course'] = $result_item_id;
16827 break;
16828 case 'tutor_courses_question':
16829 $date = $result[0]->comment_date;
16830 $data = [
16831 'comment_post_ID' => $result[0]->comment_post_ID,
16832 'comment_author' => $result[0]->comment_author,
16833 'comment_date' => $date,
16834 'comment_date_gmt' => get_gmt_from_date( $date ),
16835 'comment_content' => $result[0]->comment_content,
16836 'comment_approved' => 'approved',
16837 'comment_agent' => 'TutorLMSPlugin',
16838 'comment_type' => 'tutor_q_and_a',
16839 'comment_parent' => $result[0]->comment_parent,
16840 'user_id' => $result[0]->user_id,
16841 ];
16842 $context_data['tutor_course'] = $result[0]->comment_post_ID;
16843 $context_data['data'] = $data;
16844 break;
16845 case 'quiz_attempt_percentage':
16846 case 'quiz_failed':
16847 case 'quiz_passed':
16848 $attempt = tutor_utils()->get_attempt( $result[0]->attempt_id );
16849 $context_data = WordPress::get_user_context( $result[0]->user_id );
16850 $context_data['quiz_id'] = $attempt->quiz_id;
16851 $context_data['attempt_id'] = $result[0]->attempt_id;
16852 $context_data['attempt'] = $attempt;
16853 break;
16854 default:
16855 return;
16856 }
16857 if ( ! empty( $context_data ) ) {
16858 $context['pluggable_data'] = $context_data;
16859 $context['response_type'] = 'live';
16860 }
16861 } elseif ( empty( $result ) ) {
16862 switch ( $trigger ) {
16863 case 'course_enrolled':
16864 $sample_data = '{"pluggable_data":{"wp_user_id": 1,"user_login": "admin","display_name": "admin","user_firstname": "test","user_lastname": "test","user_email": "john@d.com","user_role": ["customer"],"id": 6636,"name": "Modes Master Class","slug": "modes-master-class-2","date_created": {"date": "2023-10-20 06:09:15.000000","timezone_type": 1,"timezone": "+00:00"},"date_modified": {"date": "2023-10-21 15:22:29.000000","timezone_type": 1,"timezone": "+00:00"},"status": "publish","menu_order": 0,"featured": false,"catalog_visibility": "visible","description": "Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words.","short_description": "","post_password": "","author_id": 1,"parent_id": 0,"reviews_allowed": true,"date_on_sale_from": null,"date_on_sale_to": null,"price": "0","regular_price": "0","sale_price": "","price_type": "free","category_ids": [106,107],"tag_ids": [],"difficulty_id": 0,"featured_image": 6616,"rating_counts": [],"average_rating": "0","review_count": 0,"enrollment_limit": 0,"duration": 360,"access_mode": "open","billing_cycle": "","show_curriculum": true,"purchase_note": "","highlights": "<li>Suscipit tortor eget felis.<\/li><li>Curabitur arcu erat idimper.<\/li><li>Lorem ipsum dolor sit amet.<\/li>","is_ai_created": false,"is_creating": false,"meta_data": []},"response_type":"sample"} ';
16865 break;
16866 case 'tutor_courses_question':
16867 $sample_data = '{"pluggable_data":{"tutor_course": "74","data": {"comment_post_ID": "74","comment_author": "admin","comment_date": "2024-02-12 08:52:45","comment_date_gmt": "2024-02-12 08:52:45","comment_content": "asdsd","comment_approved": "approved","comment_agent": "TutorLMSPlugin","comment_type": "tutor_q_and_a","comment_parent": "0","user_id": "1"}},"response_type":"sample"}';
16868 break;
16869 case 'quiz_attempt_percentage':
16870 case 'quiz_failed':
16871 case 'quiz_passed':
16872 $sample_data = '{"pluggable_data":{"wp_user_id": 1,"user_login": "john","display_name": "john","user_firstname": "john","user_lastname": "d","user_email": "johnd@gmail.com","user_role": ["administrator","tutor_instructor"],"quiz_id": "77","attempt_id": "1","attempt": {"attempt_id": "1","course_id": "74","quiz_id": "77","user_id": "1","total_questions": "1","total_answered_questions": "1","total_marks": "10.00","earned_marks": "10.00","attempt_info": "a:9:{s:10:\"time_limit\";a:3:{s:10:\"time_value\";s:1:\"0\";s:9:\"time_type\";s:7:\"minutes\";s:18:\"time_limit_seconds\";i:0;}s:13:\"feedback_mode\";s:5:\"retry\";s:16:\"attempts_allowed\";s:2:\"10\";s:13:\"passing_grade\";s:2:\"10\";s:24:\"max_questions_for_answer\";s:2:\"10\";s:20:\"question_layout_view\";s:0:\"\";s:15:\"questions_order\";s:4:\"rand\";s:29:\"short_answer_characters_limit\";s:3:\"200\";s:34:\"open_ended_answer_characters_limit\";s:3:\"500\";}","attempt_status":"attempt_ended","attempt_ip": "::1","attempt_started_at": "2024-02-12 09:05:15","attempt_ended_at": "2024-02-12 09:05:18","is_manually_reviewed": null,"manually_reviewed_at": null}},"response_type":"sample"}';
16873 break;
16874 default:
16875 return;
16876 }
16877 $context = json_decode( $sample_data, true );
16878 }
16879
16880 return $context;
16881 }
16882
16883 /**
16884 * Get Asgorus Forum list
16885 *
16886 * @param array $data data.
16887 *
16888 * @return array
16889 */
16890 public function search_asgorus_forums_list( $data ) {
16891 if ( ! class_exists( 'AsgarosForum' ) ) {
16892 return [];
16893 }
16894 $category = $data['dynamic'];
16895 if ( is_array( $category ) ) {
16896 $category_id = $category['forum_category'];
16897 } else {
16898 $category_id = $category;
16899 }
16900 $asgaros_forum = new AsgarosForum();
16901 $forums = $asgaros_forum->get_forums( $category_id );
16902 $options = [];
16903 if ( ! empty( $forums ) ) {
16904 foreach ( $forums as $forum ) {
16905 $options[] = [
16906 'label' => $forum->name,
16907 'value' => $forum->id,
16908 ];
16909 }
16910 }
16911 return [
16912 'options' => $options,
16913 'hasMore' => false,
16914 ];
16915 }
16916
16917 /**
16918 * Get Asgorus Categories list
16919 *
16920 * @param array $data data.
16921 *
16922 * @return array
16923 */
16924 public function search_asgorus_categories_list( $data ) {
16925 if ( ! class_exists( 'AsgarosForum' ) ) {
16926 return [];
16927 }
16928 $asgaros_forum = new AsgarosForum();
16929 $categories = (array) $asgaros_forum->content->get_categories();
16930 $options = [];
16931
16932 if ( ! empty( $categories ) ) {
16933 foreach ( $categories as $category ) {
16934 $options[] = [
16935 'label' => $category->name,
16936 'value' => $category->term_id,
16937 ];
16938 }
16939 }
16940 return [
16941 'options' => $options,
16942 'hasMore' => false,
16943 ];
16944 }
16945
16946 /**
16947 * Get Asgorus Topic lists
16948 *
16949 * @param array $data data.
16950 *
16951 * @return array
16952 */
16953 public function search_asgorus_topic_list( $data ) {
16954 if ( ! class_exists( 'AsgarosForum' ) ) {
16955 return [];
16956 }
16957 global $wpdb;
16958 $forum_id = $data['dynamic'];
16959 $asgaros_forum = new AsgarosForum();
16960 $sql = 'SELECT name,id FROM ' . $wpdb->prefix . 'forum_topics WHERE parent_id = %d AND closed = 0 ORDER BY id';
16961 $topics = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id ), ARRAY_A );// @phpcs:ignore
16962 $options = [];
16963
16964 if ( ! empty( $topics ) ) {
16965 foreach ( $topics as $topic ) {
16966 $options[] = [
16967 'label' => $topic['name'],
16968 'value' => $topic['id'],
16969 ];
16970 }
16971 }
16972 return [
16973 'options' => $options,
16974 'hasMore' => false,
16975 ];
16976 }
16977
16978 /**
16979 * Get Asgorus Topic Last Data
16980 *
16981 * @param array $data data.
16982 *
16983 * @return array
16984 */
16985 public function search_pluggables_asgaros_topic_last_data( $data ) {
16986 if ( ! class_exists( 'AsgarosForum' ) ) {
16987 return [];
16988 }
16989 $asgaros_forum = new AsgarosForum();
16990 $context = [];
16991 global $wpdb;
16992 $forum_id = $data['filter']['forum_id']['value'];
16993
16994 if ( -1 == $forum_id ) {
16995 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'forum_topics WHERE closed = 0 ORDER BY id DESC LIMIT 1', ARRAY_A );
16996 } else {
16997 $forum = $forum_id;
16998 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'forum_topics WHERE parent_id = %d AND closed = 0 ORDER BY id DESC LIMIT 1';
16999 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum ), ARRAY_A );// @phpcs:ignore
17000 }
17001
17002 if ( ! empty( $results ) ) {
17003 $sql_post = 'SELECT * FROM ' . $wpdb->prefix . 'forum_posts WHERE parent_id = %d ORDER BY id DESC LIMIT 1';
17004 $results_post = $wpdb->get_results( $wpdb->prepare( $sql_post, $results[0]['id'] ), ARRAY_A ); // @phpcs:ignore
17005 $context['pluggable_data']['forum_id'] = $results[0]['parent_id'];
17006 $context['pluggable_data']['topic_id'] = $results[0]['id'];
17007 $context['pluggable_data']['post_id'] = $results_post[0]['id'];
17008 $context['pluggable_data']['forum'] = $asgaros_forum->content->get_forum( $results[0]['parent_id'] );
17009 $context['pluggable_data']['topic'] = $asgaros_forum->content->get_topic( $results[0]['id'] );
17010 $context['pluggable_data']['post'] = $asgaros_forum->content->get_post( $results_post[0]['id'] );
17011
17012 $context['pluggable_data']['author'] = WordPress::get_user_context( $results[0]['author_id'] );
17013 $context['response_type'] = 'live';
17014 } else {
17015 $context = json_decode( '{"pluggable_data":{"forum_id":"1","topic_id":"2","forum":{"id":"1","name":"First Forum","parent_id":"64","parent_forum":"0","description":"My first forum.","icon":"fas fa-comments","sort":"1","forum_status":"normal","slug":"first-forum"},"topic":{"id":"2","parent_id":"1","author_id":"1","views":"2","name":"other topic","sticky":"0","closed":"0","approved":"1","slug":"other-topic"},"user":{"wp_user_id":1,"user_login":"suredev","display_name":"SureDev","user_firstname":"Sure","user_lastname":"Dev","user_email":"dev-suretest@suretriggers.com","user_role":["administrator"]}},"response_type":"sample"}', true );// @phpcs:ignore
17016 }
17017 return $context;
17018 }
17019
17020 /**
17021 * Get Asgorus Topic Reply Last Data
17022 *
17023 * @param array $data data.
17024 *
17025 * @return array
17026 */
17027 public function search_pluggables_asgaros_reply_last_data( $data ) {
17028 if ( ! class_exists( 'AsgarosForum' ) ) {
17029 return [];
17030 }
17031 $asgaros_forum = new AsgarosForum();
17032 $context = [];
17033 global $wpdb;
17034 $forum_id = $data['filter']['forum_id']['value'];
17035 $topic_id = $data['filter']['forum_id']['value'];
17036 if ( -1 == $topic_id ) {
17037 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'forum_posts ORDER BY id DESC LIMIT 1', ARRAY_A );
17038 } else {
17039 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'forum_posts WHERE parent_id = %d ORDER BY id DESC LIMIT 1';
17040 $results = $wpdb->get_results( $wpdb->prepare( $sql, $topic_id ), ARRAY_A );// @phpcs:ignore
17041 }
17042
17043 if ( ! empty( $results ) ) {
17044 $topic_id = $results[0]['parent_id'];
17045 $post_id = $results[0]['id'];
17046 $context['pluggable_data']['forum_id'] = $forum_id;
17047 $context['pluggable_data']['topic_id'] = $topic_id;
17048 $context['pluggable_data']['post_id'] = $post_id;
17049 $context['pluggable_data']['forum'] = $asgaros_forum->content->get_forum( $forum_id );
17050 $context['pluggable_data']['topic'] = $asgaros_forum->content->get_topic( $topic_id );
17051 $context['pluggable_data']['post'] = $asgaros_forum->content->get_post( $post_id );
17052
17053 $context['pluggable_data']['author'] = WordPress::get_user_context( $results[0]['author_id'] );
17054 $context['response_type'] = 'live';
17055 } else {
17056 $context = json_decode( '{"pluggable_data":{"forum_id":"1","topic_id":"2","forum":{"id":"1","name":"First Forum","parent_id":"64","parent_forum":"0","description":"My first forum.","icon":"fas fa-comments","sort":"1","forum_status":"normal","slug":"first-forum"},"topic":{"id":"2","parent_id":"1","author_id":"1","views":"2","name":"other topic","sticky":"0","closed":"0","approved":"1","slug":"other-topic"},"user":{"wp_user_id":1,"user_login":"suredev","display_name":"SureDev","user_firstname":"Sure","user_lastname":"Dev","user_email":"dev-suretest@suretriggers.com","user_role":["administrator"]}},"response_type":"sample"}', true );// @phpcs:ignore
17057 }
17058 return $context;
17059 }
17060
17061 /**
17062 * Get WPLoyalty Points Awarded Last Data
17063 *
17064 * @param array $data data.
17065 *
17066 * @return array|mixed|string
17067 */
17068 public function search_wp_loyalty_points_awarded_customer( $data ) {
17069
17070 $context = [];
17071 global $wpdb;
17072 if ( ! class_exists( 'Wlr\App\Helpers\Base' ) ) {
17073 return [];
17074 }
17075 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wlr_earn_campaign_transaction WHERE transaction_type = %s ORDER BY id DESC LIMIT 1';
17076 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'credit' ), ARRAY_A );// @phpcs:ignore
17077
17078 if ( ! empty( $results ) ) {
17079 $context['pluggable_data']['user_email'] = $results[0]['user_email'];
17080 $context['pluggable_data']['points_earned'] = $results[0]['points'];
17081 $context['pluggable_data']['action_type'] = $results[0]['action_type'];
17082 $base_helper = new \Wlr\App\Helpers\Base();
17083 $user = $base_helper->getPointUserByEmail( $results[0]['user_email'] );
17084 $points_sql = 'SELECT * FROM ' . $wpdb->prefix . 'wlr_expire_points
17085 WHERE user_email = %s ORDER BY id DESC LIMIT 1';
17086 $points_results = $wpdb->get_results( $wpdb->prepare( $points_sql, $results[0]['user_email'] ), ARRAY_A );// @phpcs:ignore
17087 $context['pluggable_data']['user'] = $user;
17088 if ( ! empty( $points_results ) ) {
17089 $expire_date = $points_results[0]['expire_date'];
17090 $timestamp = is_numeric( $expire_date ) ? (int) $expire_date : null;
17091 $date_format = get_option( 'date_format' );
17092 if ( is_string( $date_format ) ) {
17093 $context['pluggable_data']['point_expiry_date'] = wp_date( $date_format, $timestamp );
17094 }
17095 }
17096 $context['response_type'] = 'live';
17097 } else {
17098 $context = json_decode( '{"pluggable_data":{"user_email": "johnd@yopmail.com","points_earned": "4","action_type": "point_for_purchase","user": {"id": "11","user_email": "johnd@yopmail.com","refer_code": "REF-Q5Z-ZFW","points": "17","used_total_points": "0","earn_total_point": "19","birth_date": "0","level_id": "0","is_banned_user": "0","is_allow_send_email": "1","birthday_date": "0000-00-00","last_login": "0","created_date": "1710304765"},"point_expiry_date": "April 27, 2024"},"response_type":"sample"}', true );// @phpcs:ignore
17099 }
17100 return $context;
17101 }
17102
17103 /**
17104 * Get WPLoyalty Campaign Type List
17105 *
17106 * @param array $data data.
17107 *
17108 * @return array
17109 */
17110 public function search_wp_loyalty_action_type_list( $data ) {
17111 $options = [];
17112 if ( ! class_exists( 'Wlr\App\Helpers\Woocommerce' ) ) {
17113 return [];
17114 }
17115
17116 $woocommerce_helper = new \Wlr\App\Helpers\Woocommerce();
17117 $action_types = $woocommerce_helper->getActionTypes();
17118
17119 if ( ! empty( $action_types ) ) {
17120 foreach ( $action_types as $key => $type ) {
17121 $options[] = [
17122 'label' => $type,
17123 'value' => $key,
17124 ];
17125 }
17126 }
17127 return [
17128 'options' => $options,
17129 'hasMore' => false,
17130 ];
17131 }
17132
17133 /**
17134 * Get last data for trigger.
17135 *
17136 * @param array $data data.
17137 * @return array
17138 */
17139 public function search_slicewp_last_data( $data ) {
17140 global $wpdb;
17141
17142 $context = [];
17143 $context['response_type'] = 'sample';
17144
17145 $user_data = WordPress::get_sample_user_context();
17146
17147
17148 if ( ! function_exists( 'slicewp_get_affiliate' ) ) {
17149 return [];
17150 }
17151
17152 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
17153
17154 if ( in_array( $term, [ 'slicewp_new_affiliate', 'slicewp_update_affiliate' ], true ) ) {
17155 $affiliate = [
17156 'id' => 14,
17157 'user_id' => 25,
17158 'date_created' => '2024-03-14 12:35:50',
17159 'date_modified' => '2024-03-14 12:36:20',
17160 'payment_email' => 'testcustomer12@gmail.com',
17161 'website' => '',
17162 'status' => 'active',
17163 'parent_id' => 0,
17164 ];
17165 $context['pluggable_data'] = array_merge( $affiliate, [ 'user' => $user_data ] );
17166
17167 if ( ( ! empty( $data['filter'] ) && '-1' === $data['filter']['affiliate_id']['value'] ) || empty( $data['filter'] ) ) {
17168 $query = $wpdb->prepare(
17169 "
17170 SELECT *
17171 FROM {$wpdb->prefix}slicewp_affiliates
17172 WHERE status = %s
17173 ORDER BY id DESC
17174 LIMIT 1",
17175 'active'
17176 );
17177 $affiliate_results = $wpdb->get_row( $query ); //phpcs:ignore
17178
17179 } else {
17180 $affiliate_id = $data['filter']['affiliate_id']['value'];
17181 $query = $wpdb->prepare(
17182 "
17183 SELECT *
17184 FROM {$wpdb->prefix}slicewp_affiliates
17185 WHERE status = %s AND id = %d
17186 ORDER BY id DESC
17187 LIMIT 1",
17188 'active',
17189 $affiliate_id
17190 );
17191 $affiliate_results = $wpdb->get_row( $query ); //phpcs:ignore
17192
17193 }
17194
17195
17196
17197
17198 if ( ! empty( $affiliate_results ) ) {
17199 $context['pluggable_data'] = (array) $affiliate_results;
17200
17201 $user_data = WordPress::get_user_context( $affiliate_results->user_id );
17202 $context['pluggable_data']['user'] = $user_data;
17203 $context['response_type'] = 'live';
17204 }
17205 } elseif ( in_array( $term, [ 'slicewp_new_commission', 'slicewp_update_commission' ], true ) ) {
17206 $commission = [
17207 'id' => 14,
17208 'user_id' => 25,
17209 'date_created' => '2024-03-14 12:35:50',
17210 'date_modified' => '2024-03-14 12:36:20',
17211 'payment_email' => 'testcustomer12@gmail.com',
17212 'website' => '',
17213 'status' => 'active',
17214 'parent_id' => 0,
17215 ];
17216 $context['pluggable_data'] = array_merge( $commission, [ 'user' => $user_data ] );
17217 $affiliate_id = $data['filter']['affiliate_id']['value'];
17218 if ( -1 === $data['filter']['commission_id']['value'] ) {
17219 $query = $wpdb->prepare(
17220 "
17221 SELECT *
17222 FROM {$wpdb->prefix}slicewp_commissions WHERE affiliate_id=%d ORDER BY id DESC limit 1",
17223 $affiliate_id
17224 );
17225 $commission_results = $wpdb->get_row( $query ); //phpcs:ignore
17226 } else {
17227 $commission_id = $data['filter']['commission_id']['value'];
17228 $query = $wpdb->prepare(
17229 "
17230 SELECT *
17231 FROM {$wpdb->prefix}slicewp_commissions WHERE id= %d AND affiliate_id=%d ORDER BY id DESC limit 1",
17232 $commission_id,
17233 $affiliate_id
17234 );
17235 $commission_results = $wpdb->get_row( $query ); //phpcs:ignore
17236 }
17237
17238
17239
17240
17241 if ( ! empty( $commission_results ) ) {
17242 $context['pluggable_data'] = (array) $commission_results;
17243 $affiliate = slicewp_get_affiliate( $commission_results->affiliate_id );
17244 $user_id = $affiliate->get( 'user_id' );
17245 $user_data = WordPress::get_user_context( $user_id );
17246 $context['pluggable_data']['user'] = $user_data;
17247 $context['response_type'] = 'live';
17248 }
17249 }
17250
17251 return $context;
17252 }
17253
17254
17255 /**
17256 * Get Ninja Tables Last Data
17257 *
17258 * @param array $data data.
17259 *
17260 * @return array
17261 */
17262 public function search_ninja_tables_last_data( $data ) {
17263 $context = [];
17264 global $wpdb;
17265 $table_id = isset( $data['filter']['table_id']['value'] ) ? $data['filter']['table_id']['value'] : -1;
17266
17267 if ( -1 == $table_id ) {
17268 $results = $wpdb->get_row( 'SELECT * FROM ' . $wpdb->prefix . 'ninja_table_items ORDER BY id DESC LIMIT 1', ARRAY_A );
17269 } else {
17270 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'ninja_table_items WHERE table_id = %d ORDER BY id DESC LIMIT 1';
17271 $results = $wpdb->get_row( $wpdb->prepare( $sql, $table_id ), ARRAY_A );// @phpcs:ignore
17272 }
17273
17274 if ( ! empty( $results ) ) {
17275 $results['value'] = json_decode( $results['value'], true );
17276 $context['pluggable_data'] = $results;
17277 $context['pluggable_data']['owner'] = WordPress::get_user_context( $results['owner_id'] );
17278 $context['response_type'] = 'live';
17279 } else {
17280 $context = json_decode( '{"pluggable_data":{"id":"24","position":null,"table_id":"17484","owner_id":"29","attribute":"value","settings":null,"value":{"id":"3","name":"Suretriggers","class":"Dev","gender":"Female"},"created_at":"2024-03-21 13:11:25","updated_at":"2024-03-21 13:11:25","owner":{"wp_user_id":29,"user_login":"testingdsd","display_name":"suretest","user_firstname":"Suretrigger","user_lastname":"Dev","user_email":"johndoe@email.com","user_role":["editor"]}},"response_type":"sample"}', true );
17281 }
17282 return $context;
17283 }
17284
17285 /**
17286 * Get Ninja Tables Fields
17287 *
17288 * @param array $data data.
17289 *
17290 * @return array
17291 */
17292 public function search_ninja_table_fields( $data ) {
17293
17294 $context = [];
17295 global $wpdb;
17296 $table_id = isset( $data['dynamic'] ) ? $data['dynamic'] : -1;
17297 $fields = [];
17298 if ( $table_id > 0 ) {
17299 $fields = get_post_meta( $table_id, '_ninja_table_columns', true );
17300 }
17301
17302 return (array) $fields;
17303 }
17304
17305 /**
17306 * Get Late Point Booking Fields
17307 *
17308 * @param array $data data.
17309 *
17310 * @return array|void
17311 */
17312 public function search_late_point_booking_fields( $data ) {
17313 if ( ! class_exists( 'OsCustomFieldsController' ) ) {
17314
17315 return;
17316 }
17317 global $wpdb;
17318 $booking_fields = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}latepoint_settings WHERE name = %s", 'custom_fields_for_booking' ) );
17319 $fields = [];
17320 if ( ! empty( $booking_fields ) ) {
17321 $fields = json_decode( $booking_fields->value, true );
17322 }
17323 return (array) $fields;
17324 }
17325
17326 /** Search Voxel fields for action.
17327 *
17328 * @param array $data data.
17329 * @return array
17330 */
17331 public function search_voxel_custom_fields( $data ) {
17332 $post_type_fields = [];
17333 $post_type = $data['post_type'];
17334 if ( ! class_exists( '\Voxel\Post_Type' ) ) {
17335 return [];
17336 }
17337 $fields = \Voxel\Post_Type::get( $post_type )->get_fields();
17338 foreach ( $fields as $key => $field ) {
17339 if ( 'step-general' == $key ) {
17340 continue;
17341 }
17342 $field_props = $field->get_props();
17343 $post_type_fields[ $key ] = $field_props;
17344 }
17345 $context['fields'] = $post_type_fields;
17346 return $context;
17347 }
17348
17349 /**
17350 * Get Voxel Post Types lists
17351 *
17352 * @param array $data data.
17353 *
17354 * @return array
17355 */
17356 public function search_voxel_posttype_list( $data ) {
17357 $options = [];
17358 $voxel_post_types = [];
17359 if ( class_exists( '\Voxel\Post_Type' ) ) {
17360 $voxel_post_types = \Voxel\Post_Type::get_voxel_types();
17361 }
17362
17363 if ( ! empty( $voxel_post_types ) ) {
17364 foreach ( $voxel_post_types as $key => $voxel_post_type ) {
17365 $post_type = get_post_type_object( $key );
17366 if ( $post_type ) {
17367 if ( in_array( $post_type->name, [ 'collection' ], true ) ) {
17368 continue;
17369 }
17370 $options[] = [
17371 'label' => esc_attr( $post_type->labels->singular_name ),
17372 'value' => $post_type->name,
17373 ];
17374 }
17375 }
17376 }
17377 return [
17378 'options' => $options,
17379 'hasMore' => false,
17380 ];
17381 }
17382
17383 /**
17384 * Get Voxel Users Last Data
17385 *
17386 * @param array $data data.
17387 *
17388 * @return array|mixed|string
17389 */
17390 public function search_voxel_users_triggers_last_data( $data ) {
17391
17392 global $wpdb;
17393 $context = [];
17394 $term = $data['search_term'];
17395 if ( 'direct_message_received' === $term ) {
17396 $sql = "SELECT * FROM {$wpdb->prefix}voxel_messages WHERE receiver_type LIKE %s AND sender_type LIKE %s ORDER BY id DESC LIMIT 1";
17397 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user', 'user' ), ARRAY_A );// @phpcs:ignore
17398 }
17399 if ( 'direct_message_received' === $term ) {
17400 if ( ! empty( $results ) ) {
17401 $context['pluggable_data']['sender'] = WordPress::get_user_context( $results[0]['sender_id'] );
17402 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $results[0]['receiver_id'] );
17403 $context['pluggable_data']['content'] = $results[0]['content'];
17404 $context['response_type'] = 'live';
17405 } else {
17406 $context = json_decode( '{"pluggable_data":{"sender": {"wp_user_id": 1,"user_login": "admin","display_name": "Arian","user_firstname": "john","user_lastname": "d","user_email": "johnd@gmail.com","user_role": ["subscriber"]}},"receiver": {"wp_user_id": 101,"user_login": "benni","display_name": "Benni Ben","user_firstname": "Benni","user_lastname": "Ben","user_email": "benni@mailinator.com","user_role": ["subscriber"]},"content": "new message"},"response_type":"sample"}', true );// @phpcs:ignore
17407 }
17408 }
17409 return $context;
17410 }
17411
17412
17413 /**
17414 * Get Voxel Membership Last Data
17415 *
17416 * @param array $data data.
17417 *
17418 * @return array|mixed|string
17419 */
17420 public function search_voxel_membership_triggers_last_data( $data ) {
17421
17422 global $wpdb;
17423 $context = [];
17424 $term = $data['search_term'];
17425 if ( ! class_exists( 'Voxel\Stripe' ) ) {
17426 return [];
17427 }
17428 if ( 'plan_activated' === $term || 'plan_canceled' === $term ) {
17429 $meta_key = \Voxel\Stripe::is_test_mode() ? 'voxel:test_plan' : 'voxel:plan';
17430 $sql = "SELECT
17431 m.user_id AS id,
17432 u.user_login AS title,
17433 u.user_email AS email,
17434 m.meta_value AS details,
17435 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) AS plan,
17436 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.amount' ) ) AS SIGNED ) AS amount,
17437 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.status' ) ) AS status,
17438 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.created' ) ) AS DATETIME ) AS created
17439 FROM {$wpdb->prefix}usermeta as m
17440 LEFT JOIN {$wpdb->prefix}users AS u ON m.user_id = u.ID
17441 WHERE m.meta_key = %s AND JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) != 'default'
17442 ORDER BY m.user_id DESC
17443 LIMIT 25 OFFSET 0";
17444 $results = $wpdb->get_results( $wpdb->prepare( $sql, $meta_key ), ARRAY_A );// @phpcs:ignore
17445 } elseif ( 'user_registered' === $term ) {
17446 $sql = "SELECT DISTINCT u.ID
17447 FROM {$wpdb->prefix}users u
17448 JOIN {$wpdb->prefix}usermeta um ON u.ID = um.user_id
17449 WHERE um.meta_key = 'voxel:profile_id' ORDER BY ID DESC LIMIT 1";
17450 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
17451 }
17452
17453 if ( 'plan_canceled' === $term ) {
17454 if ( ! empty( $results ) ) {
17455 if ( 'cancelled' == $results[0]['status'] ) {
17456 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
17457 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
17458 $context['response_type'] = 'live';
17459 } else {
17460 $context = json_decode( '{"pluggable_data":{"wp_user_id": 101,"user_login": "benni","display_name": "John D","user_firstname": "johnd","user_lastname": "D","user_email": "johnd@gmail.com","user_role": ["subscriber"],"details": {"plan": "learningmembership","type": "subscription","subscription_id": "sub_1OwOMySHDFghoeM1sInxPrG7","price_id": "price_1OwOLJSHDFghoeM177Vf8kgt","status": "cancelled","trial_end": null,"current_period_end": 1711542948,"cancel_at_period_end": true,"amount": 800,"currency": "usd","interval": "week","interval_count": 1,"created": "2024-03-20 12:35:48","metadata": {"voxel:payment_for": "membership","voxel:plan": "learningmembership"}}},"response_type":"sample"}', true );// @phpcs:ignore
17461 }
17462 } else {
17463 $context = json_decode( '{"pluggable_data":{"wp_user_id": 101,"user_login": "benni","display_name": "John D","user_firstname": "johnd","user_lastname": "D","user_email": "johnd@gmail.com","user_role": ["subscriber"],"details": {"plan": "learningmembership","type": "subscription","subscription_id": "sub_1OwOMySHDFghoeM1sInxPrG7","price_id": "price_1OwOLJSHDFghoeM177Vf8kgt","status": "cancelled","trial_end": null,"current_period_end": 1711542948,"cancel_at_period_end": true,"amount": 800,"currency": "usd","interval": "week","interval_count": 1,"created": "2024-03-20 12:35:48","metadata": {"voxel:payment_for": "membership","voxel:plan": "learningmembership"}}},"response_type":"sample"}', true );// @phpcs:ignore
17464 }
17465 } elseif ( 'plan_activated' === $term ) {
17466 if ( ! empty( $results ) ) {
17467 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
17468 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
17469 $context['response_type'] = 'live';
17470 } else {
17471 $context = json_decode( '{"pluggable_data":{"wp_user_id": 101,"user_login": "benni","display_name": "John D","user_firstname": "johnd","user_lastname": "D","user_email": "johnd@gmail.com","user_role": ["subscriber"],"details": {"plan": "learningmembership","type": "subscription","subscription_id": "sub_1OwOMySHDFghoeM1sInxPrG7","price_id": "price_1OwOLJSHDFghoeM177Vf8kgt","status": "active","trial_end": null,"current_period_end": 1711542948,"cancel_at_period_end": true,"amount": 800,"currency": "usd","interval": "week","interval_count": 1,"created": "2024-03-20 12:35:48","metadata": {"voxel:payment_for": "membership","voxel:plan": "learningmembership"}}},"response_type":"sample"}', true );// @phpcs:ignore
17472 }
17473 } elseif ( 'user_registered' === $term ) {
17474 if ( ! empty( $results ) ) {
17475 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['ID'] );
17476 $context['response_type'] = 'live';
17477 } else {
17478 $context['pluggable_data'] = WordPress::get_sample_user_context();
17479 $context['response_type'] = 'sample';
17480 }
17481 }
17482 return $context;
17483 }
17484
17485 /**
17486 * Get Voxel Orders Last Data
17487 *
17488 * @param array $data data.
17489 *
17490 * @return array|mixed|string
17491 */
17492 public function search_voxel_order_triggers_last_data( $data ) {
17493
17494 global $wpdb;
17495 $context = [];
17496 $term = $data['search_term'];
17497
17498 if ( ! class_exists( 'Voxel\Product_Types\Orders\Order' ) ) {
17499 return [];
17500 }
17501
17502 if ( 'order_placed' === $term || 'order_canceled' === $term ) {
17503 if ( 'order_placed' === $term ) {
17504 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders ORDER BY id DESC LIMIT 1";
17505 } elseif ( 'order_canceled' === $term ) {
17506 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' ORDER BY id DESC LIMIT 1";
17507 }
17508
17509 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
17510 if ( ! empty( $results ) ) {
17511 // Get Order.
17512 $context['pluggable_data']['id'] = $results[0]['id'];
17513 $context['pluggable_data']['vendor_id'] = $results[0]['vendor_id'];
17514 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
17515 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
17516 $context['pluggable_data']['status'] = $results[0]['status'];
17517 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
17518 $context['pluggable_data']['subtotal'] = $results[0]['subtotal'];
17519 $context['pluggable_data']['total'] = $results[0]['total'];
17520 // Get order items.
17521 $order = \Voxel\Product_Types\Orders\Order::find(
17522 [
17523 'id' => $results[0]['id'],
17524 ]
17525 );
17526 $order_items = $order->get_items();
17527 $context['pluggable_data']['tax_amount'] = $order->get_tax_amount();
17528 $context['pluggable_data']['discount_amount'] = $order->get_discount_amount();
17529 $context['pluggable_data']['shipping_amount'] = $order->get_shipping_amount();
17530 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
17531 foreach ( $order_items as $item ) {
17532 $addon_data = [];
17533 if ( is_object( $item ) && method_exists( $item, 'get_addons' ) ) {
17534 // Get addon details.
17535 $addons = $item->get_addons();
17536 $addon_data = [];
17537
17538 // Add the addons to the simple entry.
17539 if ( $addons && isset( $addons['summary'] ) ) {
17540 $addon_data = $addons['summary'];
17541 }
17542 }
17543 $context['pluggable_data']['order_items'][] = [
17544 'id' => $item->get_id(),
17545 'type' => $item->get_type(),
17546 'currency' => $item->get_currency(),
17547 'quantity' => $item->get_quantity(),
17548 'subtotal' => $item->get_subtotal(),
17549 'product_id' => $item->get_post()->get_id(),
17550 'product_label' => $item->get_product_label(),
17551 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
17552 'product_link' => $item->get_product_link(),
17553 'description' => $item->get_product_description(),
17554 'addon_data' => $addon_data,
17555 ];
17556 // If booking item, get booking details.
17557 if ( 'booking' === $item->get_type() ) {
17558 $details = $item->get_order_page_details();
17559 $context['pluggable_data']['order_items']['booking_type'] = $details['booking']['type'];
17560 if ( isset( $details['booking']['count_mode'] ) ) {
17561 $context['pluggable_data']['order_items']['booking_count_mode'] = $details['booking']['count_mode'];
17562 }
17563 if ( 'date_range' === $details['booking']['type'] ) {
17564 $context['pluggable_data']['order_items']['booking_start_date'] = $details['booking']['start_date'];
17565 $context['pluggable_data']['order_items']['booking_end_date'] = $details['booking']['end_date'];
17566 } elseif ( 'single_day' === $details['booking']['type'] ) {
17567 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
17568 } elseif ( 'timeslots' === $details['booking']['type'] ) {
17569 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
17570 $context['pluggable_data']['order_items']['booking_slot_from'] = $details['booking']['slot']['from'];
17571 $context['pluggable_data']['order_items']['booking_slot_to'] = $details['booking']['slot']['to'];
17572 }
17573 }
17574 }
17575 // Get Customer.
17576 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
17577 $context['response_type'] = 'live';
17578 } else {
17579 $context = json_decode( '{"pluggable_data":{"id": "15","vendor_id": null,"details": {"cart": {"type": "direct_cart","items": {"6b39iruj": {"product": {"post_id": 9211,"field_key": "product"},"stock": {"quantity": 1}}}},"pricing": {"currency": "USD","subtotal": 10,"total": 10},"status": {"last_updated": "2024-05-30 06:52:05"}},"payment_method": "offline_payment","status": "pending_approval","created_at": "2024-05-30 06:50:19","subtotal": 10,"total": 10,"tax_amount": null,"discount_amount": null,"shipping_amount": null,"order_item_count": 1,"order_items": [{"id": 11,"type": "regular","currency": "USD","quantity": 1,"subtotal": 10,"product_id": 9211,"product_label": "Pro 1","product_thumbnail_url": null,"product_link": "https:\/\/example.com\/products\/pro-1\/","description": "","addon_data": []}],"customer": {"wp_user_id": 98,"user_login": "johnd","display_name": "johndoe","user_firstname": "john","user_lastname": "d","user_email": "johnd@example.com","user_role": ["customer"]}},"response_type":"sample"}', true );// @phpcs:ignore
17580 }
17581 } elseif ( 'order_approved' === $term || 'order_declined' === $term ) {
17582 if ( 'order_approved' === $term ) {
17583 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'completed' ORDER BY id DESC LIMIT 1";
17584 } elseif ( 'order_declined' === $term ) {
17585 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' ORDER BY id DESC LIMIT 1";
17586 }
17587 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
17588 if ( ! empty( $results ) ) {
17589 // Get Order.
17590 $context['pluggable_data']['id'] = $results[0]['id'];
17591 $context['pluggable_data']['vendor_id'] = $results[0]['vendor_id'];
17592 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
17593 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
17594 $context['pluggable_data']['status'] = $results[0]['status'];
17595 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
17596 $context['pluggable_data']['subtotal'] = $results[0]['subtotal'];
17597 $context['pluggable_data']['total'] = $results[0]['total'];
17598 // Get order items.
17599 $order = \Voxel\Product_Types\Orders\Order::find(
17600 [
17601 'id' => $results[0]['id'],
17602 ]
17603 );
17604 $order_items = $order->get_items();
17605 $context['pluggable_data']['tax_amount'] = $order->get_tax_amount();
17606 $context['pluggable_data']['discount_amount'] = $order->get_discount_amount();
17607 $context['pluggable_data']['shipping_amount'] = $order->get_shipping_amount();
17608 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
17609 foreach ( $order_items as $item ) {
17610 $addon_data = [];
17611 if ( is_object( $item ) && method_exists( $item, 'get_addons' ) ) {
17612 // Get addon details.
17613 $addons = $item->get_addons();
17614 $addon_data = [];
17615
17616 // Add the addons to the simple entry.
17617 if ( $addons && isset( $addons['summary'] ) ) {
17618 $addon_data = $addons['summary'];
17619 }
17620 }
17621 $context['pluggable_data']['order_items'][] = [
17622 'id' => $item->get_id(),
17623 'type' => $item->get_type(),
17624 'currency' => $item->get_currency(),
17625 'quantity' => $item->get_quantity(),
17626 'subtotal' => $item->get_subtotal(),
17627 'product_id' => $item->get_post()->get_id(),
17628 'product_label' => $item->get_product_label(),
17629 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
17630 'product_link' => $item->get_product_link(),
17631 'description' => $item->get_product_description(),
17632 'addon_data' => $addon_data,
17633 ];
17634 // If booking item, get booking details.
17635 if ( 'booking' === $item->get_type() ) {
17636 $details = $item->get_order_page_details();
17637 $context['pluggable_data']['order_items']['booking_type'] = $details['booking']['type'];
17638 if ( isset( $details['booking']['count_mode'] ) ) {
17639 $context['pluggable_data']['order_items']['booking_count_mode'] = $details['booking']['count_mode'];
17640 }
17641 if ( 'date_range' === $details['booking']['type'] ) {
17642 $context['pluggable_data']['order_items']['booking_start_date'] = $details['booking']['start_date'];
17643 $context['pluggable_data']['order_items']['booking_end_date'] = $details['booking']['end_date'];
17644 } elseif ( 'single_day' === $details['booking']['type'] ) {
17645 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
17646 } elseif ( 'timeslots' === $details['booking']['type'] ) {
17647 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
17648 $context['pluggable_data']['order_items']['booking_slot_from'] = $details['booking']['slot']['from'];
17649 $context['pluggable_data']['order_items']['booking_slot_to'] = $details['booking']['slot']['to'];
17650 }
17651 }
17652 }
17653 // Get Customer.
17654 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
17655 // Get Vendor.
17656 $context['pluggable_data']['vendor'] = WordPress::get_user_context( $results[0]['vendor_id'] );
17657 $context['response_type'] = 'live';
17658 } else {
17659 $context = json_decode( '{"pluggable_data":{"id": "15","vendor_id": null,"details": {"cart": {"type": "direct_cart","items": {"6b39iruj": {"product": {"post_id": 9211,"field_key": "product"},"stock": {"quantity": 1}}}},"pricing": {"currency": "USD","subtotal": 10,"total": 10},"status": {"last_updated": "2024-05-30 06:52:05"}},"payment_method": "offline_payment","status": "pending_approval","created_at": "2024-05-30 06:50:19","subtotal": null,"total": null,"tax_amount": null,"discount_amount": null,"shipping_amount": null,"order_item_count": 1,"order_items": [{"id": 11,"type": "regular","currency": "USD","quantity": 1,"subtotal": 10,"product_id": 9211,"product_label": "Pro 1","product_thumbnail_url": null,"product_link": "https:\/\/example.com\/products\/pro-1\/","description": "","addon_data": []}],"vendor": {"wp_user_id": 1,"user_login": "admin","display_name": "Arian","user_firstname": "arian","user_lastname": "d","user_email": "johnd@gmail.com","user_role": {"0": "administrator","7": "academy_instructor","8": "tutor_instructor"}},"customer": {"wp_user_id": 98,"user_login": "johnd","display_name": "johndoe","user_firstname": "john","user_lastname": "d","user_email": "johnd@example.com","user_role": ["customer"]}},"response_type":"sample"}', true );// @phpcs:ignore
17660 }
17661 } elseif ( 'order_promotion_activated' === $term || 'order_promotion_canceled' === $term || 'order_claim_listing' === $term ) {
17662 if ( 'order_promotion_activated' === $term ) {
17663 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'completed' AND details LIKE '%voxel:promotion%' ORDER BY id DESC LIMIT 1";
17664 } elseif ( 'order_promotion_canceled' === $term ) {
17665 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' AND details LIKE '%voxel:promotion%' ORDER BY id DESC LIMIT 1";
17666 } elseif ( 'order_claim_listing' === $term ) {
17667 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE details LIKE '%voxel:claim%' ORDER BY id DESC LIMIT 1";
17668 }
17669 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
17670 if ( ! empty( $results ) ) {
17671 // Get Order.
17672 $context['pluggable_data']['id'] = $results[0]['id'];
17673 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
17674 $context['pluggable_data']['status'] = $results[0]['status'];
17675 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
17676 // Get order items.
17677 $order = \Voxel\Product_Types\Orders\Order::find(
17678 [
17679 'id' => $results[0]['id'],
17680 ]
17681 );
17682 $order_items = $order->get_items();
17683 $context['pluggable_data']['details'] = $order->get_details();
17684 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
17685 foreach ( $order_items as $item ) {
17686 $context['pluggable_data']['order_items'][] = [
17687 'id' => $item->get_id(),
17688 'type' => $item->get_type(),
17689 'currency' => $item->get_currency(),
17690 'quantity' => $item->get_quantity(),
17691 'subtotal' => $item->get_subtotal(),
17692 'product_id' => $item->get_post()->get_id(),
17693 'product_label' => $item->get_product_label(),
17694 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
17695 'product_link' => $item->get_product_link(),
17696 'description' => $item->get_product_description(),
17697 ];
17698 }
17699 // Get Customer.
17700 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
17701 $context['response_type'] = 'live';
17702 } else {
17703 $context = json_decode( '{"pluggable_data":{"id":"22","payment_method":"offline_payment","status":"pending_approval","created_at":"2024-08-27 10:20:16","details":{"cart":{"type":"direct_cart","items":{"lzl47hyq":{"product":{"post_id":8912,"field_key":"voxel:claim"}}}},"pricing":{"currency":"USD","subtotal":5,"total":5},"order_notes":"ABDDD","status":{"last_updated":"2024-08-27 10:20:16"}},"order_item_count":1,"order_items":[{"id":22,"type":"regular","currency":"USD","quantity":null,"subtotal":5,"product_id":8912,"product_label":"Fokachio","product_thumbnail_url":"https://example.com/wp-content/uploads/2024/05/8a13537-150x150.jpg","product_link":"https://example.com/places/papas-pita-2/","description":"Claim request"}],"customer":{"wp_user_id":1,"user_login":"johnd","display_name":"johnd","user_firstname":"john","user_lastname":"d","user_email":"johnd@example.com","user_registered":"2023-01-16 09:23:31","user_role":{"8":"tutor_instructor"}}},"response_type":"sample"}', true );// @phpcs:ignore
17704 }
17705 }
17706 return $context;
17707 }
17708
17709 /**
17710 * Get Voxel Timeline Comments Last Data
17711 *
17712 * @param array $data data.
17713 *
17714 * @return array|mixed|string
17715 */
17716 public function search_voxel_timeline_triggers_last_data( $data ) {
17717
17718 global $wpdb;
17719 $context = [];
17720 $term = $data['search_term'];
17721 $sql = '';
17722 if ( 'new_comment_timeline' === $term ) {
17723 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE parent_id IS NULL ORDER BY id DESC LIMIT 1";
17724 } elseif ( 'comment_reply_timeline' === $term ) {
17725 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE parent_id IS NOT NULL ORDER BY id DESC LIMIT 1";
17726 }
17727 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
17728 if ( 'new_comment_timeline' === $term ) {
17729 if ( ! empty( $results ) ) {
17730 $context['pluggable_data']['comment_by'] = WordPress::get_user_context( $results[0]['user_id'] );
17731 $context['pluggable_data']['id'] = $results[0]['id'];
17732 $context['pluggable_data']['status_id'] = $results[0]['status_id'];
17733 $context['pluggable_data']['content'] = $results[0]['content'];
17734 $context['response_type'] = 'live';
17735 } else {
17736 $context = json_decode( '{"pluggable_data":{"comment_by": {"wp_user_id": 1,"user_login": "admin","display_name": "Johnd","user_firstname": "john","user_lastname": "d","user_email": "johnd@yopmail.com","user_role": {"0": "administrator","7": "academy_instructor","8": "tutor_instructor"}},"id": "16","status_id": "5","content": "Nice"},"response_type":"sample"}', true );// @phpcs:ignore
17737 }
17738 } elseif ( 'comment_reply_timeline' === $term ) {
17739 if ( ! empty( $results ) ) {
17740 $comment_sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE id = %d";
17741 $comment_result = $wpdb->get_results( $wpdb->prepare( $comment_sql, $results[0]['parent_id']), ARRAY_A );// @phpcs:ignore
17742 $context['pluggable_data']['replied_by'] = WordPress::get_user_context( $results[0]['user_id'] );
17743 $context['pluggable_data']['comment_by'] = WordPress::get_user_context( $comment_result[0]['user_id'] );
17744 $context['pluggable_data']['comment'] = $comment_result[0]['content'];
17745 $context['pluggable_data']['comment_id'] = $results[0]['parent_id'];
17746 $context['pluggable_data']['reply_id'] = $results[0]['id'];
17747 $context['pluggable_data']['reply'] = $results[0]['content'];
17748 $context['response_type'] = 'live';
17749 } else {
17750 $context = json_decode( '{"pluggable_data":{"replied_by": {"wp_user_id": 101,"user_login": "johnd","display_name": "JohnD","user_firstname": "John","user_lastname": "D","user_email": "johnd@gmail.com","user_role": ["subscriber"]},"comment_by": {"wp_user_id": 1,"user_login": "admin","display_name": "Arian","user_firstname": "Arian","user_lastname": "D","user_email": "arian2@gmail.com","user_role": ["subscriber"]},"comment": "Nice","comment_id": "16","reply_id": "17","reply": "Nice too"},"response_type":"sample"}', true );// @phpcs:ignore
17751 }
17752 }
17753 return $context;
17754 }
17755
17756 /**
17757 * Get Voxel Timeline Comments Last Data
17758 *
17759 * @param array $data data.
17760 *
17761 * @return array|mixed|string
17762 */
17763 public function search_voxel_posts_triggers_last_data( $data ) {
17764 $context = [];
17765 global $wpdb;
17766 $post_type = 'post';
17767 $term = $data['search_term'];
17768 if ( isset( $data['filter']['post_type']['value'] ) ) {
17769 $post_type = $data['filter']['post_type']['value'];
17770 }
17771 $results = [];
17772 if ( 'post_approved' === $term ) {
17773 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
17774 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type, 'publish'), ARRAY_A );// @phpcs:ignore
17775 } elseif ( 'post_rejected' === $term ) {
17776 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
17777 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type, 'rejected'), ARRAY_A );// @phpcs:ignore
17778 } elseif ( 'post_reviewed' === $term ) {
17779 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline vt JOIN {$wpdb->prefix}posts p ON vt.post_id = p.ID JOIN {$wpdb->prefix}postmeta pm ON vt.post_id = pm.post_id
17780 WHERE pm.meta_key LIKE '%voxel:review_stats%' AND p.post_type LIKE %s AND vt.details IS NOT NULL AND vt.details LIKE %s";
17781 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type, '%rating%' ), ARRAY_A );// @phpcs:ignore
17782 } elseif ( 'post_submitted' === $term ) {
17783 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
17784 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type), ARRAY_A );// @phpcs:ignore
17785 } elseif ( 'post_updated' === $term ) {
17786 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
17787 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type ), ARRAY_A );// @phpcs:ignore
17788 } elseif ( 'collection_post_submitted' === $term ) {
17789 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
17790 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'collection' ), ARRAY_A );// @phpcs:ignore
17791 } elseif ( 'collection_post_updated' === $term ) {
17792 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
17793 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'collection' ), ARRAY_A );// @phpcs:ignore
17794 } elseif ( 'new_wall_post_by_user' === $term ) {
17795 $sql = "SELECT vt.* FROM {$wpdb->prefix}voxel_timeline vt JOIN {$wpdb->prefix}posts p ON vt.post_id = p.ID JOIN {$wpdb->prefix}postmeta pm ON pm.post_id = p.ID WHERE p.post_type LIKE %s AND vt.details IS NOT NULL AND vt.details NOT LIKE %s ORDER BY vt.id DESC LIMIT 1";
17796 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type, '%rating%' ), ARRAY_A );// @phpcs:ignore
17797 }
17798 if ( 'post_approved' === $term || 'post_rejected' === $term || 'post_submitted' === $term || 'post_updated' === $term ) {
17799 if ( ! empty( $results ) ) {
17800 $context['pluggable_data'] = WordPress::get_post_context( $results[0]['ID'] );
17801 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['ID'] );
17802 $context['response_type'] = 'live';
17803 } else {
17804 $context['pluggable_data'] = [
17805 'ID' => 557,
17806 'post' => 557,
17807 'post_author' => 1,
17808 'post_date' => '2022-11-18 12:18:14',
17809 'post_date_gmt' => '2022-11-18 12:18:14',
17810 'post_content' => 'Test Post Content',
17811 'post_title' => 'Test Post',
17812 'post_excerpt' => '',
17813 'post_status' => 'published',
17814 'comment_status' => 'open',
17815 'ping_status' => 'open',
17816 'post_password' => '',
17817 'post_name' => 'test-post',
17818 'to_ping' => '',
17819 'pinged' => '',
17820 'post_modified' => '2022-11-18 12:18:14',
17821 'post_modified_gmt' => '2022-11-18 12:18:14',
17822 'post_content_filtered' => '',
17823 'post_parent' => 0,
17824 'guid' => 'https://example.com/test-post/',
17825 'menu_order' => 0,
17826 'post_type' => 'post',
17827 'post_mime_type' => '',
17828 'comment_count' => 0,
17829 'filter' => 'raw',
17830 ];
17831 $context['response_type'] = 'sample';
17832 }
17833 } elseif ( 'post_reviewed' === $term ) {
17834 if ( ! empty( $results ) ) {
17835 $context['pluggable_data'] = WordPress::get_post_context( $results[0]['post_id'] );
17836 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['post_id'] );
17837 $context['pluggable_data']['review_content'] = $results[0]['content'];
17838 $context['pluggable_data']['review_created_at'] = $results[0]['created_at'];
17839 $context['pluggable_data']['review_details'] = json_decode( $results[0]['details'], true );
17840 $context['pluggable_data']['review_by'] = WordPress::get_user_context( $results[0]['user_id'] );
17841 $context['response_type'] = 'live';
17842 } else {
17843 $context = json_decode( '{"pluggable_data":{"ID": 8291,"post_author": "1","post_date": "2024-03-18 09:01:54","post_date_gmt": "2024-03-18 09:01:54","post_content": "<p>PizzaCrust - Since 2009! Whether it\u2019s our iconic Sandwiches, wooden baked pizzas, signature sauce, original fresh dough or toppings, we invest in bringing the freshest and best ingredients to bring you the tastiest meal.<\/p>","post_title": "Pizza Crust","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "closed","post_password": "","post_name": "sach-pizza","to_ping": "","pinged": "","post_modified": "2024-03-18 09:01:54","post_modified_gmt": "2024-03-18 09:01:54","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/places\/sach-pizza\/","menu_order": 0,"post_type": "places","post_mime_type": "","comment_count": "0","filter": "raw","review_content": "Nice one","review_created_at": "2024-04-04 12:59:22","review_details": {"rating": {"score": -1,"custom-660": -1,"custom-978": -1,"custom-271": -1}},"review_by": {"wp_user_id": 188,"user_login": "dev4","display_name": "dev4","user_firstname": "","user_lastname": "","user_email": "dev4@yopmail.com","user_role": ["subscriber"]}},"response_type":"sample"}', true );// @phpcs:ignore
17844 }
17845 } elseif ( 'collection_post_submitted' === $term || 'collection_post_updated' === $term ) {
17846 if ( ! empty( $results ) ) {
17847 $context['pluggable_data'] = Voxel::get_post_fields( $results[0]['ID'] );
17848 $context['pluggable_data']['collection'] = WordPress::get_post_context( $results[0]['ID'] );
17849 $context['response_type'] = 'live';
17850 } else {
17851 $context = json_decode(
17852 '{"pluggable_data":{"field_step-general": null,"field_title": "First Collection","field_items": [8909,8293],"collection": {"ID": 9142,"post_author": "35",
17853 "post_date": "2024-05-27 05:55:21","post_date_gmt": "2024-05-27 05:55:21","post_content": "This is a first collection","post_title": "First Collection","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "closed","post_password": "","post_name": "first-collection","to_ping": "","pinged": "","post_modified": "2024-05-27 05:55:21","post_modified_gmt": "2024-05-27 05:55:21","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/example.com\/collection\/first-collection\/","menu_order": 0,"post_type": "collection","post_mime_type": "","comment_count": "0","filter": "raw"}},"response_type":"sample"}', true );// @phpcs:ignore
17854 }
17855 } elseif ( 'new_wall_post_by_user' === $term ) {
17856 if ( ! empty( $results ) ) {
17857 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['post_id'] );
17858 $user = get_userdata( $results[0]['user_id'] );
17859 if ( $user ) {
17860 $user_data = (array) $user->data;
17861 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
17862 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
17863 $context['pluggable_data']['user_email'] = $user_data['user_email'];
17864 $context['pluggable_data']['user_id'] = $results[0]['user_id'];
17865 }
17866 if ( function_exists( 'Voxel\Timeline\prepare_status_json' ) && class_exists( 'Voxel\Timeline\Status' ) ) {
17867 // Get the status details.
17868 $args = [
17869 'post_id' => $results[0]['post_id'],
17870 'order_by' => 'created_at',
17871 'order' => 'desc',
17872 ];
17873 $statuses = \Voxel\Timeline\Status::query( $args );
17874 $status_details = \Voxel\Timeline\prepare_status_json( $statuses[0] );
17875 foreach ( (array) $status_details as $key => $value ) {
17876 $context['pluggable_data']['wall_post'][ $key ] = $value;
17877 }
17878 }
17879 $context['response_type'] = 'live';
17880 } else {
17881 $context = json_decode( '{"pluggable_data":{"post": {"step-general": null,"voxel:name": "johnd","voxel:avatar": [],"description": "","field_step-general": null,"field_voxel:name": "johnd","field_voxel:avatar": [],"field_description": ""},"user_display_name": "johnd","user_name": "johnd","user_email": "johnd@yopmail.com","user_id": "1","wall_post": {"content": "new wallpost","created_at": "2024-06-13 08:44:05","files": [{"source": "existing","id": 9331,"name": "download-1-1.png","alt": "","url": "https:\/\/example.com\/wp-content\/uploads\/2024\/06\/download-1-1.png","preview": "https:\/\/example.com\/wp-content\/uploads\/2024\/06\/download-1-1.png","type": "image\/png"}]}},"response_type":"sample"}', true );// @phpcs:ignore
17882 }
17883 }
17884 return $context;
17885 }
17886
17887 /**
17888 * Get Voxel Profiles Last Data
17889 *
17890 * @param array $data data.
17891 *
17892 * @return array|mixed|string
17893 */
17894 public function search_voxel_profiles_triggers_last_data( $data ) {
17895
17896 global $wpdb;
17897 $context = [];
17898 $term = $data['search_term'];
17899
17900 if ( ! class_exists( 'Voxel\Timeline\Status' ) || ! function_exists( 'Voxel\Timeline\prepare_status_json' ) || ! class_exists( 'Voxel\Post_Type' ) ) {
17901 return;
17902 }
17903
17904 if ( 'profile_created' === $term ) {
17905 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
17906 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
17907 } elseif ( 'profile_approved' === $term ) {
17908 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
17909 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile', 'publish' ), ARRAY_A );// @phpcs:ignore
17910 } elseif ( 'profile_rejected' === $term ) {
17911 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
17912 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile', 'rejected' ), ARRAY_A );// @phpcs:ignore
17913 } elseif ( 'profile_reviewed' === $term ) {
17914 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline vt LEFT JOIN {$wpdb->prefix}posts pm
17915 ON vt.post_id = pm.ID
17916 WHERE pm.post_type LIKE %s AND vt.details IS NOT NULL ORDER BY vt.id DESC LIMIT 1";
17917 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
17918 } elseif ( 'profile_updated' === $term ) {
17919 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
17920 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
17921 } elseif ( 'profile_new_wall_post' === $term ) {
17922 $sql = "SELECT vt.* FROM {$wpdb->prefix}voxel_timeline vt JOIN {$wpdb->prefix}posts p ON vt.post_id = p.ID JOIN {$wpdb->prefix}postmeta pm ON pm.post_id = p.ID WHERE p.post_type LIKE 'profile' ORDER BY vt.id DESC LIMIT 1";
17923 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
17924 }
17925 if ( 'profile_created' === $term || 'profile_approved' === $term || 'profile_rejected' === $term || 'profile_updated' === $term ) {
17926 if ( ! empty( $results ) ) {
17927 $context['pluggable_data'] = Voxel::get_post_fields( $results[0]['ID'] );
17928 $user = get_userdata( $results[0]['post_author'] );
17929 if ( $user ) {
17930 $user_data = (array) $user->data;
17931 $context['pluggable_data']['profile_display_name'] = $user_data['display_name'];
17932 $context['pluggable_data']['profile_email'] = $user_data['user_email'];
17933 $context['pluggable_data']['profile_user_id'] = $results[0]['post_author'];
17934 $context['pluggable_data']['profile_id'] = $results[0]['ID'];
17935 }
17936 $context['response_type'] = 'live';
17937 } else {
17938 $context = json_decode( '{"pluggable_data":{"field_step-general": null,"field_voxel:name":"johnd","field_voxel:avatar": [],"field_description": "","profile_display_name": "johnd","profile_name": "johnd","profile_email": "johnd@yopmail.com","profile_user_id": "1"},"response_type":"sample"}', true );// @phpcs:ignore
17939 }
17940 } elseif ( 'profile_new_wall_post' === $term ) {
17941 if ( ! empty( $results ) ) {
17942 $context['pluggable_data']['profile'] = Voxel::get_post_fields( $results[0]['post_id'] );
17943 $user = get_userdata( $results[0]['user_id'] );
17944 if ( $user ) {
17945 $user_data = (array) $user->data;
17946 $context['pluggable_data']['profile_display_name'] = $user_data['display_name'];
17947 $context['pluggable_data']['profile_name'] = $user_data['user_nicename'];
17948 $context['pluggable_data']['profile_email'] = $user_data['user_email'];
17949 $context['pluggable_data']['profile_user_id'] = $results[0]['user_id'];
17950 }
17951 // Get the status details.
17952 $args = [
17953 'post_id' => $results[0]['post_id'],
17954 'order_by' => 'created_at',
17955 'order' => 'desc',
17956 ];
17957 $statuses = \Voxel\Timeline\Status::query( $args );
17958 $status_details = \Voxel\Timeline\prepare_status_json( $statuses[0] );
17959 foreach ( (array) $status_details as $key => $value ) {
17960 $context['pluggable_data']['wall_post'][ $key ] = $value;
17961 }
17962 $context['response_type'] = 'live';
17963 } else {
17964 $context = json_decode( '{"pluggable_data":{"profile": {"step-general": null,"voxel:name": "johnd","voxel:avatar": [],"description": "","field_step-general": null,"field_voxel:name": "johnd","field_voxel:avatar": [],"field_description": ""},"profile_display_name": "johnd","profile_name": "johnd","profile_email": "johnd@yopmail.com","profile_user_id": "1""wall_post": {"content": "new wallpost","created_at": "2024-06-13 08:44:05","files": [{"source": "existing","id": 9331,"name": "download-1-1.png","alt": "","url": "https:\/\/example.com\/wp-content\/uploads\/2024\/06\/download-1-1.png","preview": "https:\/\/example.com\/wp-content\/uploads\/2024\/06\/download-1-1.png","type": "image\/png"}]}},"response_type":"sample"}', true );// @phpcs:ignore
17965 }
17966 } elseif ( 'profile_reviewed' === $term ) {
17967 if ( ! empty( $results ) ) {
17968 // Get the review details.
17969 $args = [
17970 'post_id' => $results[0]['post_id'],
17971 'order_by' => 'created_at',
17972 'order' => 'desc',
17973 ];
17974 $statuses = \Voxel\Timeline\Status::query( $args );
17975 $review_details = \Voxel\Timeline\prepare_status_json( $statuses[0] );
17976 foreach ( (array) $review_details as $key => $value ) {
17977 if ( 'user_can_edit' == $key || 'publisher' == $key || 'user_can_edit' == $key || 'user_can_moderate' == $key ) {
17978 continue;
17979 }
17980 if ( 'files' === $key ) {
17981 $value = wp_json_encode( $value );
17982 } elseif ( 'reviews' === $key ) {
17983 $review_ratings = isset( $value['ratings'] ) && is_array( $value['ratings'] ) ? $value['ratings'] : [];
17984 $value['ratings'] = [];
17985 $type = \Voxel\Post_Type::get( 'profile' );
17986
17987 if ( ! empty( $review_ratings ) ) {
17988 $rating_levels = $type->reviews->get_rating_levels();
17989 $categories = $type->reviews->get_categories();
17990
17991 foreach ( $categories as $category ) {
17992 $category_key = $category['key'];
17993 $category_label = strtolower( $category['label'] );
17994
17995 if ( isset( $review_ratings[ $category_key ] ) && $category_label ) {
17996 foreach ( $rating_levels as $rating_level ) {
17997 if ( $review_ratings[ $category_key ] === $rating_level['score'] ) {
17998 $value['ratings'][ $category_label ] = $rating_level['label'];
17999 break;
18000 }
18001 }
18002 }
18003 }
18004 }
18005 } else {
18006 $key = 'review_' . $key;
18007 }
18008 $context['pluggable_data'][ $key ] = $value;
18009 }
18010 if ( isset( $context['pluggable_data']['review_user'] ) ) {
18011 unset( $context['pluggable_data']['review_user']['avatar'] );
18012 }
18013 $context['response_type'] = 'live';
18014 } else {
18015 $context = json_decode( '{"pluggable_data":{"review_id":32,"review_key":"152b0720","review_link":"https://example.com/newsfeed/?status_id=32","review_time":"3 hours ago","review_edit_time":null,"review_content":"new review","review_raw_content":"new review","files":null,"review_is_review":true,"review_user":{"exists":true,"name":"johnd","link":"https://example.com/members/johnd/","id":217},"review_post":{"exists":true,"title":"admin","link":"https://example.com/members/admin/","is_profile":false,"post_type":"profile"},"review_liked_by_user":false,"review_like_count":null,"review_reply_count":null,"review_replies":{"requested":false,"visible":false,"page":1,"loading":false,"hasMore":false,"list":[]},"reviews":{"score":0,"score_formatted":"3.0","mode":"numeric","ratings":{"rating":"Good"}}},"response_type":"sample"}', true );// @phpcs:ignore
18016 }
18017 }
18018 return $context;
18019 }
18020
18021
18022 /**
18023 * Get Late Point Customer Fields
18024 *
18025 * @param array $data data.
18026 *
18027 * @return array|void
18028 */
18029 public function search_late_point_customer_fields( $data ) {
18030 if ( ! class_exists( 'OsCustomFieldsController' ) ) {
18031
18032 return;
18033 }
18034 global $wpdb;
18035 $booking_fields = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}latepoint_settings WHERE name = %s", 'custom_fields_for_customer' ) );
18036 $fields = [];
18037 if ( ! empty( $booking_fields ) ) {
18038 $fields = json_decode( $booking_fields->value, true );
18039 }
18040 return (array) $fields;
18041 }
18042
18043 /**
18044 * Is multi person booking
18045 *
18046 * @param array $data data.
18047 *
18048 * @return array|void
18049 */
18050 public function search_late_point_multi_person_booking( $data ) {
18051 if ( ! class_exists( 'LatePointAddonGroupBookings' ) ) {
18052
18053 return;
18054 }
18055 $service_id = isset( $data['dynamic'] ) ? $data['dynamic'] : '-1';
18056
18057 if ( -1 == $service_id ) {
18058 return;
18059 }
18060 global $wpdb;
18061 $booking_details = $wpdb->get_row( $wpdb->prepare( "SELECT capacity_max,capacity_min FROM {$wpdb->prefix}latepoint_services WHERE id= %s", intval( $service_id ) ) );
18062 if ( ! empty( $booking_details ) ) {
18063 return (array) $booking_details;
18064 }
18065
18066 }
18067
18068 /**
18069 * Get Mail Mint lists
18070 *
18071 * @param array $data data.
18072 *
18073 * @return array
18074 */
18075 public function search_mail_mint_contact_lists( $data ) {
18076 global $wpdb;
18077
18078 $lists = $wpdb->get_results( $wpdb->prepare( 'SELECT title, id FROM ' . $wpdb->prefix . 'mint_contact_groups WHERE type = %s', 'lists' ) );
18079 $options = [];
18080
18081 if ( ! empty( $lists ) ) {
18082 foreach ( $lists as $list ) {
18083 $options[] = [
18084 'label' => $list->title,
18085 'value' => $list->id,
18086 ];
18087 }
18088 }
18089 return [
18090 'options' => $options,
18091 'hasMore' => false,
18092 ];
18093 }
18094
18095 /**
18096 * Get Mail Mint tags
18097 *
18098 * @param array $data data.
18099 *
18100 * @return array
18101 */
18102 public function search_mail_mint_contact_tags( $data ) {
18103 global $wpdb;
18104
18105 $lists = $wpdb->get_results( $wpdb->prepare( 'SELECT title, id FROM ' . $wpdb->prefix . 'mint_contact_groups WHERE type = %s ', 'tags' ) );
18106 $options = [];
18107
18108 if ( ! empty( $lists ) ) {
18109 foreach ( $lists as $list ) {
18110 $options[] = [
18111 'label' => $list->title,
18112 'value' => $list->id,
18113 ];
18114 }
18115 }
18116 return [
18117 'options' => $options,
18118 'hasMore' => false,
18119 ];
18120 }
18121
18122 /** Get Better Messages trigger Last Data
18123 *
18124 * @param array $data data.
18125 *
18126 * @return array|mixed|string
18127 */
18128 public function search_better_messages_triggers_data( $data ) {
18129 $context = [];
18130 global $wpdb;
18131 if ( ! function_exists( 'Better_Messages' ) ) {
18132 return [];
18133 }
18134 $sql = "SELECT * FROM {$wpdb->prefix}bm_message_messages ORDER BY id DESC LIMIT 1";
18135 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18136
18137 if ( ! empty( $results ) ) {
18138 $message = Better_Messages()->functions->get_message( $results[0]['id'] );
18139 if ( is_object( $message ) ) {
18140 $message = get_object_vars( $message );
18141 }
18142 $context['pluggable_data'] = $message;
18143 $context['pluggable_data']['sender'] = WordPress::get_user_context( $results[0]['sender_id'] );
18144 $context['response_type'] = 'live';
18145 } else {
18146 $context = json_decode( '{"pluggable_data":{"id": "21","thread_id": "11","sender_id": "79","message": "New message","date_sent": "2024-04-09 07:08:35","sender": {"wp_user_id": 79,"user_login": "johnd@gmail.com","display_name": "johnd@gmail.com","user_firstname": "John","user_lastname": "D","user_email": "johnd@gmail.com","user_role": ["customer"]}},"response_type":"sample"}', true );// @phpcs:ignore
18147 }
18148 return $context;
18149 }
18150
18151 /**
18152 * Get Appointment Hour Booking trigger Last Data
18153 *
18154 * @param array $data data.
18155 *
18156 * @return array|mixed|string
18157 */
18158 public function search_ahb_appointment_booked_triggers_last_data( $data ) {
18159 $context = [];
18160 global $wpdb;
18161 $sql = "SELECT * FROM {$wpdb->prefix}cpappbk_messages ORDER BY id DESC LIMIT 1";
18162 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18163
18164 if ( ! empty( $results ) ) {
18165 $context['pluggable_data'] = unserialize( $results[0]['posted_data'] );
18166 $context['response_type'] = 'live';
18167 } else {
18168 $context = json_decode( '{"pluggable_data":{"final_price": "1.00","final_price_short": "1","request_timestamp": "04\/10\/2024 06:56:33","apps": [{"id": 1,"cancelled": "Pending","serviceindex": 0,"service": "Service 1","duration": 60,"price": 1,"date": "2024-04-13","slot": "10:00\/11:00","military": 0,"field": "fieldname1","quant": 1,"sid": ""}],"app_service_1": "Service 1","app_status_1": "Pending","app_duration_1": 60,"app_price_1": 1,"app_date_1": "04\/13\/2024","app_slot_1": "10:00\/11:00","app_starttime_1": "10:00 AM","app_endtime_1": "11:00 AM","app_quantity_1": 1,"formid": 1,"formname": "Form 1","referrer": "https:\/\/example.com\/wp-admin\/admin.php?page=cp_apphourbooking&amp;addbk=1&amp;cal=1&amp;r=0.7819147291131667","fieldname1": " - 04\/13\/2024 10:00 AM - 11:00 AM (Service 1)\n","email": "johnd@yopmail.com","username": "admin","itemnumber": 1},"response_type":"sample"}', true );// @phpcs:ignore
18169 }
18170 return $context;
18171 }
18172
18173 /**
18174 * Prepare mailmint contact status.
18175 *
18176 * @param array $data Search Params.
18177 *
18178 * @return array
18179 */
18180 public function search_mail_mint_fetch_custom_fields( $data ) {
18181
18182 $options = [
18183 [
18184 'label' => 'Yes',
18185 'value' => 'true',
18186 ],
18187 [
18188 'label' => 'No',
18189 'value' => 'false',
18190 ],
18191 ];
18192
18193 return [
18194 'options' => $options,
18195 'hasMore' => false,
18196 ];
18197 }
18198
18199 /**
18200 * Get Mail Mint custom fields
18201 *
18202 * @param array $data data.
18203 *
18204 * @return array
18205 */
18206 public function search_mail_mint_custom_fields( $data ) {
18207 global $wpdb;
18208 $fields = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'mint_custom_fields' );
18209
18210 return (array) $fields;
18211
18212 }
18213
18214 /**
18215 * Get Mail Mint lists
18216 *
18217 * @param array $data data.
18218 *
18219 * @return array|void
18220 */
18221 public function search_mail_mint_contacts( $data ) {
18222 if ( ! class_exists( 'Mint\MRM\DataBase\Models\ContactModel' ) ) {
18223 return [];
18224 }
18225 $contacts = ContactModel::get_all();
18226 $options = [];
18227 if ( ! empty( $contacts ) ) {
18228 foreach ( $contacts['data'] as $contact ) {
18229 $options[] = [
18230 'label' => $contact['email'],
18231 'value' => $contact['id'],
18232 ];
18233 }
18234 }
18235 return [
18236 'options' => $options,
18237 'hasMore' => false,
18238 ];
18239 }
18240
18241 /**
18242 * Get mail mint Last Data
18243 *
18244 * @param array $data data.
18245 *
18246 * @return array
18247 */
18248 public function search_mail_mint_last_data( $data ) {
18249 if ( ! class_exists( 'Mint\MRM\DataBase\Models\ContactModel' ) || ! class_exists( 'Mint\MRM\DataBase\Models\ContactGroupModel' ) ) {
18250 return [];
18251 }
18252 $context = [];
18253 global $wpdb;
18254 $contact_id = isset( $data['filter']['contact_id']['value'] ) ? $data['filter']['contact_id']['value'] : -1;
18255 $term = $data['search_term'] ? $data['search_term'] : '';
18256 $type = 'lists';
18257 if ( 'mail_mint_tags_added_to_contact' === $term ) {
18258 $type = 'tags';
18259 }
18260 $data = [];
18261 if ( -1 == $contact_id ) {
18262 $result = $wpdb->get_row( $wpdb->prepare( "SELECT relation.id as rid, relation.contact_id, cgroups.* FROM {$wpdb->prefix}mint_contact_group_relationship AS relation JOIN {$wpdb->prefix}mint_contact_groups AS cgroups ON cgroups.id = relation.group_id WHERE cgroups.type = %s ORDER BY rid DESC Limit 1", $type ) );
18263 } else {
18264 $result = $wpdb->get_row( $wpdb->prepare( "SELECT relation.id as rid, relation.contact_id, cgroups.* FROM {$wpdb->prefix}mint_contact_group_relationship AS relation JOIN {$wpdb->prefix}mint_contact_groups AS cgroups ON cgroups.id = relation.group_id WHERE cgroups.type = %s AND relation.contact_id =%d ORDER BY rid DESC Limit 1", $type, $contact_id ) );
18265 }
18266
18267 if ( ! empty( $result ) ) {
18268 $contact_id = $result->contact_id;
18269 $data['contact'] = ContactModel::get( $contact_id );
18270 $data[ $type ] = ContactGroupModel::get( $result->id );
18271 }
18272 if ( ! empty( $data ) ) {
18273 $context['pluggable_data'] = $data;
18274 $context['response_type'] = 'live';
18275 } else {
18276 $context = json_decode( '{"pluggable_data":{"contact":{"id":"10","wp_user_id":"0","hash":"96eb6293345a2b54246a3214d2419948","email":"john2@test.com","first_name":"John","last_name":"Doe","scores":"0","source":"WebHook","status":"subscribed","stage":"","last_activity":null,"created_by":"0","created_at":"2024-04-09 10:28:56","updated_at":null,"meta_fields":{"dropdown":"New","radio":"One","checkbox":["One"],"status_changed":"subscribed"}},"' . $type . '":{"id":"3","title":"new","type":"tags","data":null,"created_at":"2024-04-09 11:23:11","updated_at":null}},"response_type":"sample"}', true );
18277 }
18278 return (array) $context;
18279 }
18280
18281
18282 /**
18283 * Get Simple Schedule Appointment Types
18284 *
18285 * @param array $data data.
18286 *
18287 * @return array
18288 */
18289 public function search_simple_schedule_appointment_types( $data ) {
18290 global $wpdb;
18291
18292 $appointment_types = $wpdb->get_results( 'SELECT title, id FROM ' . $wpdb->prefix . 'ssa_appointment_types' );
18293 $options = [];
18294
18295 if ( ! empty( $appointment_types ) ) {
18296 foreach ( $appointment_types as $appointment_type ) {
18297 $options[] = [
18298 'label' => $appointment_type->title,
18299 'value' => $appointment_type->id,
18300 ];
18301 }
18302 }
18303 return [
18304 'options' => $options,
18305 'hasMore' => false,
18306 ];
18307 }
18308
18309 /**
18310 * Get mail mint Last Data
18311 *
18312 * @param array $data data.
18313 *
18314 * @return array
18315 */
18316 public function search_ssa_last_data( $data ) {
18317 $context = [];
18318 global $wpdb;
18319 $appointment_type_id = isset( $data['filter']['appointment_type_id']['value'] ) ? $data['filter']['appointment_type_id']['value'] : -1;
18320 $term = $data['search_term'] ? $data['search_term'] : '';
18321 $status = 'booked';
18322 if ( 'ssa_appointment_cancelled' === $term ) {
18323 $status = 'canceled';
18324 }
18325 $data = [];
18326 if ( -1 == $appointment_type_id ) {
18327 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointments WHERE status = %s ORDER BY id DESC Limit 1", $status ), ARRAY_A );
18328 } else {
18329 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointments where appointment_type_id=%d AND status = %s ORDER BY id DESC Limit 1", $appointment_type_id, $status ), ARRAY_A );
18330 }
18331
18332
18333 if ( ! empty( $result ) ) {
18334 $result_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointment_meta where appointment_id=%d", $result['id'] ) );
18335 if ( ! empty( $result_meta ) ) {
18336 foreach ( $result_meta as $meta ) {
18337 $result[ $meta->meta_key ] = $meta->meta_value;
18338 }
18339 }
18340 }
18341 if ( ! empty( $result ) ) {
18342
18343 $result['customer_information'] = json_decode( $result['customer_information'], true );
18344 $context['pluggable_data'] = $result;
18345 $context['response_type'] = 'live';
18346 } else {
18347 $context = json_decode( '{"pluggable_data":{"id":"4","appointment_type_id":"1","rescheduled_from_appointment_id":"0","rescheduled_to_appointment_id":"0","group_id":"0","author_id":"1","customer_id":"0","customer_information":{"Name":"John Doe","Email":"johndoe@email.com"},"customer_timezone":"Asia\/Calcutta","customer_locale":"en_US","start_date":"2024-04-22 03:30:00","end_date":"2024-04-22 04:00:00","title":"","description":"","payment_method":"","payment_received":"0.00","mailchimp_list_id":"","google_calendar_id":"","google_calendar_event_id":"","web_meeting_password":"","web_meeting_id":"","web_meeting_url":"","allow_sms":"","status":"booked","date_created":"2024-04-18 09:19:14","date_modified":"2024-04-18 09:19:14","expiration_date":"0000-00-00 00:00:00"},"response_type":"sample"}', true );
18348 }
18349 return (array) $context;
18350 }
18351
18352 /**
18353 * Get Simple Schedule Appointment Types
18354 *
18355 * @param array $data data.
18356 *
18357 * @return array
18358 */
18359 public function search_simple_schedule_appointments( $data ) {
18360 global $wpdb;
18361
18362 $appointment_types = $wpdb->get_results( 'SELECT title, id FROM ' . $wpdb->prefix . 'ssa_appointment_types' );
18363 $options = [];
18364
18365 if ( ! empty( $appointment_types ) ) {
18366 foreach ( $appointment_types as $appointment_type ) {
18367 $options[] = [
18368 'label' => $appointment_type->title,
18369 'value' => $appointment_type->id,
18370 ];
18371 }
18372 }
18373 return [
18374 'options' => $options,
18375 'hasMore' => false,
18376 ];
18377 }
18378
18379 /**
18380 * Get Sensei LMS Lessons
18381 *
18382 * @param array $data data.
18383 *
18384 * @return array
18385 */
18386 public function search_sensei_lms_lessons( $data ) {
18387 if ( ! function_exists( 'Sensei' ) ) {
18388 return [];
18389 }
18390 $course_id = $data['dynamic'];
18391 $lessons = \Sensei()->course->course_lessons( $course_id, 'publish', 'ids' );
18392 $options = [];
18393
18394 if ( ! empty( $lessons ) ) {
18395 foreach ( $lessons as $lesson ) {
18396 $options[] = [
18397 'label' => get_the_title( $lesson ),
18398 'value' => $lesson,
18399 ];
18400 }
18401 }
18402 return [
18403 'options' => $options,
18404 'hasMore' => false,
18405 ];
18406 }
18407
18408 /**
18409 * Get Sensei LMS Quiz List
18410 *
18411 * @param array $data data.
18412 *
18413 * @return array
18414 */
18415 public function search_sensei_lms_quiz( $data ) {
18416 $page = $data['page'];
18417 $limit = Utilities::get_search_page_limit();
18418 $offset = $limit * ( $page - 1 );
18419
18420 $quizes = get_posts(
18421 [
18422 'post_type' => 'lesson',
18423 'orderby' => 'title',
18424 'order' => 'ASC',
18425 'post_status' => 'publish',
18426 'posts_per_page' => $limit,
18427 'offset' => $offset,
18428 'fields' => 'ids',
18429 'meta_query' => [
18430 'relation' => 'AND',
18431 [
18432 'key' => '_lesson_quiz',
18433 'compare' => 'EXISTS',
18434 ],
18435 [
18436 'key' => '_quiz_has_questions',
18437 'value' => 1,
18438 'compare' => '=',
18439 ],
18440 ],
18441 ]
18442 );
18443 $options = [];
18444 $total_quizes = get_posts(
18445 [
18446 'post_type' => 'lesson',
18447 'orderby' => 'title',
18448 'order' => 'ASC',
18449 'post_status' => 'publish',
18450 'posts_per_page' => -1,
18451 'fields' => 'ids',
18452 'meta_query' => [
18453 'relation' => 'AND',
18454 [
18455 'key' => '_lesson_quiz',
18456 'compare' => 'EXISTS',
18457 ],
18458 [
18459 'key' => '_quiz_has_questions',
18460 'value' => 1,
18461 'compare' => '=',
18462 ],
18463 ],
18464 ]
18465 );
18466 $count = count( $total_quizes );
18467 if ( ! empty( $quizes ) ) {
18468 foreach ( $quizes as $quiz ) {
18469 $options[] = [
18470 'label' => get_the_title( $quiz ),
18471 'value' => $quiz,
18472 ];
18473 }
18474 }
18475 return [
18476 'options' => $options,
18477 'hasMore' => $count > $limit && $count > $offset,
18478 ];
18479 }
18480
18481 /**
18482 * Get Sensei LMS trigger Last Data
18483 *
18484 * @param array $data data.
18485 *
18486 * @return array|mixed|string
18487 */
18488 public function search_sensei_lms_triggers_last_data( $data ) {
18489 $context = [];
18490 global $wpdb;
18491 $term = $data['search_term'];
18492
18493 if ( ! function_exists( 'Sensei' ) ) {
18494 return;
18495 }
18496
18497 if ( 'course_completed' == $term ) {
18498 $comment_type = 'sensei_course_status';
18499 $course_id = $data['filter']['sensei_course']['value'];
18500 if ( -1 === $course_id ) {
18501 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s ORDER BY comment_ID DESC LIMIT 1";
18502 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type ), ARRAY_A );// @phpcs:ignore
18503 } else {
18504 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s AND comment_post_ID = %d ORDER BY comment_ID DESC LIMIT 1";
18505 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, $course_id ), ARRAY_A );// @phpcs:ignore
18506 }
18507 } elseif ( 'course_enrolled' == $term ) {
18508 $comment_type = 'sensei_course_status';
18509 $course_id = $data['filter']['sensei_course']['value'];
18510 if ( -1 === $course_id ) {
18511 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s ORDER BY comment_ID DESC LIMIT 1";
18512 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'in-progress', $comment_type ), ARRAY_A );// @phpcs:ignore
18513 } else {
18514 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s AND comment_post_ID = %d ORDER BY comment_ID DESC LIMIT 1";
18515 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'in-progress', $comment_type, $course_id ), ARRAY_A );// @phpcs:ignore
18516 }
18517 } elseif ( 'lesson_completed' == $term ) {
18518 $lesson_id = $data['filter']['sensei_lesson']['value'];
18519 $course_id = $data['filter']['sensei_course']['value'];
18520 $comment_type = 'sensei_lesson_status';
18521 if ( -1 === $lesson_id ) {
18522 $sql = "SELECT comments.*, meta.* FROM {$wpdb->prefix}comments as comments
18523 LEFT JOIN {$wpdb->prefix}postmeta as meta
18524 ON comments.comment_post_ID = meta.post_id
18525 WHERE comments.comment_approved = %s
18526 AND comments.comment_type = %s
18527 AND meta.meta_key = %s AND
18528 meta.meta_value = %d
18529 ORDER BY comments.comment_ID DESC LIMIT 1";
18530 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, '_lesson_course', $course_id ), ARRAY_A );// @phpcs:ignore
18531 } else {
18532 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s AND comment_post_ID = %d ORDER BY comment_ID DESC LIMIT 1";
18533 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, $lesson_id ), ARRAY_A );// @phpcs:ignore
18534 }
18535 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term ) {
18536 $quiz_id = $data['filter']['sensei_quiz']['value'];
18537 $comment_type = 'sensei_lesson_status';
18538 if ( 'quiz_passes' == $term ) {
18539 $status = "( comment_approved = 'passed' )";
18540 } elseif ( 'quiz_fails' == $term ) {
18541 $status = "( comment_approved = 'failed' )";
18542 } else {
18543 $status = "( comment_approved = 'failed' OR comment_approved = 'passed' )";
18544 }
18545 if ( -1 === $quiz_id ) {
18546 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type = %s AND $status ORDER BY comment_ID DESC LIMIT 1";
18547 $results = $wpdb->get_results( $wpdb->prepare( $sql, $comment_type ), ARRAY_A );// @phpcs:ignore
18548 } else {
18549 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type = %s AND $status AND comment_post_ID = %d ORDER BY comment_ID DESC LIMIT 1";
18550 $results = $wpdb->get_results( $wpdb->prepare( $sql, $comment_type, $quiz_id ), ARRAY_A );// @phpcs:ignore
18551 }
18552 } elseif ( 'quiz_completion' == $term ) {
18553 $quiz_id = $data['filter']['sensei_quiz']['value'];
18554 $comment_type = 'sensei_lesson_status';
18555 $condition_compare = $data['filter']['condition_compare']['value'];
18556 $percentage = $data['filter']['percentage']['value'];
18557 if ( -1 === $quiz_id ) {
18558 $results = $wpdb->get_results( $wpdb->prepare( "SELECT comments.*, meta.meta_key, meta.meta_value FROM wp_comments AS comments JOIN wp_commentmeta AS meta ON comments.comment_ID = meta.comment_id WHERE meta.meta_key LIKE %s AND meta.meta_value $condition_compare %d ORDER BY comments.comment_ID DESC LIMIT 1", 'grade', $percentage ), ARRAY_A ); //phpcs:ignore
18559 } else {
18560 $results = $wpdb->get_results( $wpdb->prepare( "SELECT comments.*, meta.meta_key, meta.meta_value FROM wp_comments AS comments JOIN wp_commentmeta AS meta ON comments.comment_ID = meta.comment_id WHERE comments.comment_post_ID = %d AND meta.meta_key LIKE %s AND meta.meta_value $condition_compare %d ORDER BY comments.comment_ID DESC LIMIT 1", $quiz_id, 'grade', $percentage ), ARRAY_A ); //phpcs:ignore
18561 }
18562 }
18563
18564 if ( ! empty( $results ) ) {
18565 $post = get_post( $results[0]['comment_post_ID'] );
18566 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['user_id'] );
18567 if ( 'course_completed' == $term || 'course_enrolled' == $term ) {
18568 $context['pluggable_data']['sensei_course'] = $results[0]['comment_post_ID'];
18569 if ( $post instanceof \WP_Post ) {
18570 $context['pluggable_data']['course_title'] = $post->post_title;
18571 }
18572 } elseif ( 'lesson_completed' == $term ) {
18573 $context['pluggable_data']['sensei_lesson'] = $results[0]['comment_post_ID'];
18574 if ( $post instanceof \WP_Post ) {
18575 $context['pluggable_data']['lesson_title'] = $post->post_title;
18576 }
18577 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term || 'quiz_completion' == $term ) {
18578 $lesson_quiz = get_post_meta( $results[0]['comment_post_ID'], '_lesson_quiz', true );
18579 $submission = \Sensei()->quiz_submission_repository->get( $lesson_quiz, $results[0]['user_id'] );
18580 if ( $post instanceof \WP_Post ) {
18581 $context['pluggable_data']['quiz_title'] = $post->post_title;
18582 }
18583 $context['pluggable_data']['quiz_status'] = $results[0]['comment_approved'];
18584 $context['pluggable_data']['quiz_data']['id'] = $submission->get_id();
18585 $context['sensei_quiz'] = $submission->get_id();
18586 $context['pluggable_data']['quiz_data']['final_grade'] = $submission->get_final_grade();
18587 $context['pluggable_data']['quiz_data']['created_at'] = $submission->get_created_at();
18588 }
18589 $context['response_type'] = 'live';
18590 } else {
18591 if ( 'course_completed' == $term || 'course_enrolled' == $term ) {
18592 $context = json_decode( '{"pluggable_data":{"wp_user_id": 48,"user_login": "john@yopmail.com","display_name": "john@yopmail.com","user_firstname": "john","user_lastname": "d","user_email": "johnd@yopmail.com","user_role": ["subscriber"],"sensei_course": "7500","course_title": "Course 1"},"response_type":"sample"}', true );// @phpcs:ignore
18593 } elseif ( 'lesson_completed' == $term ) {
18594 $context = json_decode( '{"pluggable_data":{"wp_user_id": 48,"user_login": "john@yopmail.com","display_name": "john@yopmail.com","user_firstname": "john","user_lastname": "d","user_email": "johnd@yopmail.com","user_role": ["subscriber"],"sensei_lesson": "7502","lesson_title": "Lesson 1"},"response_type":"sample"}', true );// @phpcs:ignore
18595 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term || 'quiz_completion' == $term ) {
18596 $context = json_decode( '{"pluggable_data":{"wp_user_id":2,"user_login":"johnd","display_name":"john d","user_firstname":"john","user_lastname":"d","user_email":"johnd@email.com","user_role":[],"quiz_title":"Lesson 2","quiz_status":"passed","quiz_data":{"id":27,"final_grade":100,"created_at":{"date":"2024-05-07 06:54:31.000000","timezone_type":1,"timezone":"+00:00"}}},"response_type":"sample"}', true );// @phpcs:ignore
18597 }
18598 }
18599 return $context;
18600 }
18601
18602 /**
18603 * Get SurelyWP Services - SureCart Addons Last Data
18604 *
18605 * @param array $data data.
18606 *
18607 * @return array
18608 */
18609 public function search_services_sc_triggers_last_data( $data ) {
18610 $context = [];
18611 global $wpdb;
18612 $term = $data['search_term'] ? $data['search_term'] : '';
18613 $data = [];
18614
18615 if ( 'new_service_created' === $term ) {
18616 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_status LIKE 'service_created' ORDER BY service_id DESC Limit 1", ARRAY_A );
18617 } elseif ( 'requirement_submitted' === $term ) {
18618 $result = $wpdb->get_row( "SELECT created_at FROM {$wpdb->prefix}surelywp_sv_requirements ORDER BY created_at DESC Limit 1", ARRAY_A );
18619 } elseif ( 'message_sent' === $term ) {
18620 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_messages ORDER BY message_id DESC Limit 1", ARRAY_A );
18621 } elseif ( 'message_final_delivery_sent' === $term ) {
18622 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_messages WHERE is_final_delivery = 1 ORDER BY message_id DESC Limit 1", ARRAY_A );
18623 } elseif ( 'customer_request_revision' === $term ) {
18624 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_status LIKE 'service_start' ORDER BY service_id DESC Limit 1", ARRAY_A );
18625 } elseif ( 'customer_approves_final_delivery' === $term ) {
18626 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_status LIKE 'service_complete' ORDER BY service_id DESC Limit 1", ARRAY_A );
18627 } elseif ( 'delivery_date_changed' === $term ) {
18628 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE delivery_date IS NOT NULL ORDER BY service_id DESC Limit 1", ARRAY_A );
18629 } elseif ( 'service_cancel' === $term || 'service_marked_canceled' === $term ) {
18630 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_status LIKE 'service_canceled' ORDER BY service_id DESC Limit 1", ARRAY_A );
18631 } elseif ( 'service_completed' === $term || 'service_mark_completed' === $term ) {
18632 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_status LIKE 'service_complete' ORDER BY service_id DESC Limit 1", ARRAY_A );
18633 } elseif ( 'contract_signed' === $term ) {
18634 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_contracts ORDER BY contract_id DESC Limit 1", ARRAY_A );
18635 }
18636 if ( ! empty( $result ) ) {
18637 if ( 'new_service_created' === $term ) {
18638 $service_data = [
18639 'service_setting_id' => $result['service_id'],
18640 'order_id' => $result['order_id'],
18641 'product_id' => $result['product_id'],
18642 'service_status' => $result['service_status'],
18643 'delivery_date' => $result['delivery_date'],
18644 ];
18645 $context['pluggable_data'] = array_merge( $service_data, WordPress::get_user_context( $result['user_id'] ) );
18646 $context['response_type'] = 'live';
18647 } elseif ( 'requirement_submitted' === $term ) {
18648 $requirements_data = $wpdb->get_results(
18649 $wpdb->prepare(
18650 "SELECT * FROM {$wpdb->prefix}surelywp_sv_requirements WHERE created_at = %s",
18651 $result['created_at']
18652 ),
18653 ARRAY_A
18654 );
18655 $service_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_id = %d", $requirements_data[0]['service_id'] ), ARRAY_A );
18656 $user_data = WordPress::get_user_context( $service_result['user_id'] );
18657 unset( $service_result['user_id'] );
18658 $context['pluggable_data'] = array_merge( $requirements_data, $service_result, $user_data );
18659 foreach ( $requirements_data as $value ) {
18660 if ( 'file' == $value['requirement_type'] ) {
18661 $upload_dir = wp_upload_dir();
18662 $attachment_file_names = json_decode( $value['requirement'], true );
18663 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
18664 $context['pluggable_data']['requirement_attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $value['service_id'] . '/requirement/' . $attachment_file_name;
18665 }
18666 }
18667 }
18668 $context['response_type'] = 'live';
18669 } elseif ( 'message_sent' === $term || 'message_final_delivery_sent' === $term ) {
18670 $message_data = [
18671 'sender' => WordPress::get_user_context( $result['sender_id'] ),
18672 'receiver' => WordPress::get_user_context( $result['receiver_id'] ),
18673 'service_id' => $result['service_id'],
18674 'message_text' => $result['message_text'],
18675 'attachment_file_name' => $result['attachment_file_name'],
18676 'is_final_delivery' => $result['is_final_delivery'],
18677 ];
18678 $context['pluggable_data'] = $message_data;
18679 $upload_dir = wp_upload_dir();
18680 $attachment_file_names = json_decode( $result['attachment_file_name'], true );
18681 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
18682 $context['pluggable_data']['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $result['service_id'] . '/messages/' . $attachment_file_name;
18683 }
18684 $context['response_type'] = 'live';
18685 } elseif ( 'customer_request_revision' === $term ) {
18686 $message_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sv_messages WHERE service_id = %d AND is_final_delivery = %d ORDER BY message_id DESC LIMIT 1", $result['service_id'], 1 ), ARRAY_A );
18687 if ( ! empty( $message_result ) ) {
18688 global $surelywp_sv_model;
18689 $revision_message = $surelywp_sv_model->surelywp_sv_get_customer_revision_msg( $message_result['service_id'], $message_result['message_id'] );
18690 $context['pluggable_data'] = array_merge( $result, $revision_message );
18691 $context['pluggable_data']['sender'] = WordPress::get_user_context( $revision_message['sender_id'] );
18692 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $revision_message['receiver_id'] );
18693 $upload_dir = wp_upload_dir();
18694 $attachment_file_names = json_decode( $revision_message['attachment_file_name'], true );
18695 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
18696 $context['pluggable_data']['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $revision_message['service_id'] . '/messages/' . $attachment_file_name;
18697 }
18698 $context['response_type'] = 'live';
18699 } else {
18700 $context = json_decode( '{"pluggable_data":{"service_id":"6","service_setting_id":"kXg4Exmj","user_id":"51","order_id":"16574f7f-66d8-466e-8716-8da9671e6668","product_id":"f59f62cc-fd70-4007-8bcf-56d07f1ac871","service_status":"service_start","delivery_date":"2024-08-29","created_at":"2024-08-25 22:42:57","updated_at":null,"message_id":"13","sender_id":"41","receiver_id":"0","message_text":"This is the final revision","attachment_file_name":"[\"test_copy-1724605977.pdf\",\"test-1724605977.pdf\"]","is_final_delivery":"0","is_approved_delivery":null,"sender":{"wp_user_id":41,"user_login":"john@example.com","display_name":"john@example.com","user_firstname":"john","user_lastname":"d","user_email":"john@example.com","user_registered":"2023-01-30 09:34:54","user_role":["customer"]},"receiver":[],"attachment_file":["https://example.com/wp-content/uploads/surelywp-services-data/6/messages/test_copy-1724605977.pdf","https://example.com/wp-content/uploads/surelywp-services-data/6/messages/test-1724605977.pdf"]},"response_type":"sample"}', true );
18701 }
18702 } elseif ( 'customer_approves_final_delivery' === $term || 'delivery_date_changed' === $term || 'service_cancel' === $term || 'service_marked_canceled' === $term || 'service_completed' === $term || 'service_mark_completed' === $term ) {
18703 $user_data = WordPress::get_user_context( $result['user_id'] );
18704 unset( $result['user_id'] );
18705 $context['pluggable_data'] = array_merge( $result, $user_data );
18706 $context['response_type'] = 'live';
18707 } elseif ( 'contract_signed' === $term ) {
18708 $contract_data = [
18709 'service_id' => $result['service_id'],
18710 'signature' => $result['signature'],
18711 'contract_details' => $result['contract_details'],
18712 ];
18713 $service_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_id = %d", $result['service_id'] ), ARRAY_A );
18714 $user_data = WordPress::get_user_context( $service_result['user_id'] );
18715 unset( $service_result['user_id'] );
18716 $context['pluggable_data'] = array_merge( $contract_data, $user_data, $service_result );
18717 $context['response_type'] = 'live';
18718 }
18719 } else {
18720 if ( 'new_service_created' === $term || 'customer_request_revision' === $term || 'customer_approves_final_delivery' === $term || 'delivery_date_changed' === $term ) {
18721 $context = json_decode( '{"pluggable_data":{"service_setting_id":"3","order_id":"81ac6e4f-1f8a-4f8b-a3d1-37fba6c8f893","product_id":"f59f62cc-fd70-4007-8bcf-56d07f1ac871","service_status":"service_created","delivery_date":null,"wp_user_id":84,"user_login":"johnd@example.com","display_name":"johnd@example.com","user_firstname":"John","user_lastname":"D","user_email":"johnd@example.com","user_registered":"2023-02-02 13:08:44","user_role":["customer"]},"response_type":"sample"}', true );
18722 } elseif ( 'requirement_submitted' === $term ) {
18723 $context = json_decode( '{"pluggable_data":{"0":{"requirement_id":"6","service_id":"7","requirement_type":"textarea","requirement_title":"TestReq","requirement_desc":"This is a testing requirement","requirement":"This is my requirements.","created_at":"2024-08-26 10:27:33","updated_at":null},"1":{"requirement_id":"7","service_id":"7","requirement_type":"file","requirement_title":"Upload Photos","requirement_desc":"Please upload reference photos","requirement":"[\"test_copy-1724648253.pdf\",\"test-1724648253.pdf\"]","created_at":"2024-08-26 10:27:33","updated_at":null},"service_id":"7","service_setting_id":"kXg4Exmj","order_id":"16574f7f-66d8-466e-8716-8da9671e6668","product_id":"f59f62cc-fd70-4007-8bcf-56d07f1ac871","service_status":"service_start","delivery_date":"2024-08-29","created_at":"2024-08-26 10:22:31","updated_at":"2024-08-26 04:57:36","wp_user_id":51,"user_login":"johnd@example.com","display_name":"johnd@example.com","user_firstname":"john","user_lastname":"d","user_email":"johnd@example.com","user_registered":"2023-02-02 07:12:46","user_role":["customer"],"requirement_attachment_file":["https://example.com/wp-content/uploads/surelywp-services-data/7/requirement/test_copy-1724648253.pdf","https://example.com/wp-content/uploads/surelywp-services-data/7/requirement/test-1724648253.pdf"]},"response_type":"sample"}', true );
18724 } elseif ( 'message_sent' === $term || 'message_final_delivery_sent' === $term ) {
18725 $context = json_decode( '{"pluggable_data":{"sender":{"wp_user_id":84,"user_login":"johnd@example.com","display_name":"johnd@example.com","user_firstname":"john","user_lastname":"d","user_email":"johnd@example.com","user_registered":"2023-02-02 13:08:44","user_role":["customer"]},"receiver_id": {"wp_user_id":8,"user_login":"johnde@example.com","display_name":"johnde@example.com","user_firstname":"johnd","user_lastname":"ed","user_email":"johnde@example.com","user_registered":"2023-02-02 13:08:44","user_role":["admin"]},"service_id": "1","message_text": "Message Text","attachment_file_name": "Attachment File Name","is_final_delivery": "1", "attachment_file":["https://example.com/wp-content/uploads/surelywp-services-data/6/messages/test_copy-1724605977.pdf","https://example.com/wp-content/uploads/surelywp-services-data/6/messages/test-1724605977.pdf"]},"response_type":"sample"}', true );
18726 } elseif ( 'service_cancel' === $term ) {
18727 $context = json_decode( '{"pluggable_data":{"delivery_date": null,"order_id": "a3830048-9a43-4088-a78e-285537f16ecc","product_id": "f59f62cc-fd70-4007-8bcf-56d07f1ac871","service_setting_id": "2","service_status": "service_canceled","wp_user_id":84,"user_login":"johnd@example.com","display_name":"johnd@example.com","user_firstname":"John","user_lastname":"D","user_email":"johnd@example.com","user_registered":"2023-02-02 13:08:44","user_role":["customer"]},"response_type":"sample"}', true );
18728 } elseif ( 'service_marked_canceled' === $term || 'service_completed' === $term || 'service_mark_completed' === $term ) {
18729 $context = json_decode( '{"pluggable_data":{"delivery_date": null,"order_id": "a3830048-9a43-4088-a78e-285537f16ecc","product_id": "f59f62cc-fd70-4007-8bcf-56d07f1ac871","service_setting_id": "2","service_status": "service_completed","wp_user_id":84,"user_login":"johnd@example.com","display_name":"johnd@example.com","user_firstname":"John","user_lastname":"D","user_email":"johnd@example.com","user_registered":"2023-02-02 13:08:44","user_role":["customer"]},"response_type":"sample"}', true );
18730 } elseif ( 'contract_signed' === $term ) {
18731 $context = json_decode( '{"pluggable_data":{"service_id":"4","signature":"signature","contract_details":"Contract Details","wp_user_id":84,"user_login":"johnd@example.com","display_name":"johnd@example.com","user_firstname":"John","user_lastname":"D","user_email":"johnd@example.com","user_registered":"2023-02-02 13:08:44","user_role":["customer"],"service_setting_id":"u2pDYtDF","user_id":"84","order_id":"8e8ca710-13cd-4c94-8de5-98a19a3b9de6","product_id":"a39c7d4f-50bd-49ba-b56c-4f17aac61306","service_status":"service_start","delivery_date":"2024-08-25","created_at":"2024-08-22 15:15:27","updated_at":"2024-08-22 09:46:02"},"response_type":"sample"}', true );
18732 }
18733 }
18734 return (array) $context;
18735 }
18736 }
18737
18738 GlobalSearchController::get_instance();
18739