PluginProbe
OttoKit: All-in-One Automation Platform / 1.0.28
OttoKit: All-in-One Automation Platform v1.0.28
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 in OttoKit: All-in-One Automation Platform 1.0.28, at src/Controllers/GlobalSearchController.php

11,333 lines 344.0 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 memberpress\courses\lib as lib;
21 use memberpress\courses\models as models;
22 use FluentCrm\Framework\Support\Arr;
23 use GFCommon;
24 use GFFormsModel;
25 use Give_Payment;
26 use MeprBaseRealGateway;
27 use MeprOptions;
28 use PrestoPlayer\Models\Video;
29 use RGFormsModel;
30 use SureTriggers\Integrations\AffiliateWP\AffiliateWP;
31 use SureTriggers\Integrations\EDD\EDD;
32 use SureTriggers\Integrations\FunnelKitAutomations\FunnelKitAutomations;
33 use SureTriggers\Integrations\JetpackCRM\JetpackCRM;
34 use SureTriggers\Integrations\LearnDash\LearnDash;
35 use SureTriggers\Integrations\LifterLMS\LifterLMS;
36 use SureTriggers\Integrations\MemberPress\MemberPress;
37 use SureTriggers\Integrations\MemberPressCourse\MemberPressCourse;
38 use SureTriggers\Integrations\ModernEventsCalendar\ModernEventsCalendar;
39 use SureTriggers\Integrations\PeepSo\PeepSo;
40 use SureTriggers\Integrations\RafflePress\RafflePress;
41 use SureTriggers\Integrations\RestrictContent\RestrictContent;
42 use SureTriggers\Integrations\TheEventCalendar\TheEventCalendar;
43 use SureTriggers\Integrations\WishlistMember\WishlistMember;
44 use SureTriggers\Integrations\WooCommerce\WooCommerce;
45 use SureTriggers\Integrations\WordPress\WordPress;
46 use SureTriggers\Integrations\WpPolls\WpPolls;
47 use SureTriggers\Models\Utilities;
48 use SureTriggers\Traits\SingletonLoader;
49 use Tribe__Tickets__Tickets_Handler;
50 use WC_Subscription;
51 use WC_Subscriptions_Product;
52 use WP_Query;
53 use WP_Comment_Query;
54 use WP_REST_Request;
55 use WP_REST_Response;
56 use WPForms_Form_Handler;
57 use CP_V2_Popups;
58 use Project_Huddle;
59 use FrmForm;
60 use Forminator_API;
61 use SureTriggers\Integrations\LearnPress\LearnPress;
62 use WC_Customer;
63 use WC_Booking;
64
65 /**
66 * GlobalSearchController- Add ajax related functions here.
67 *
68 * @category GlobalSearchController
69 * @package SureTriggers
70 * @author BSF <username@example.com>
71 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
72 * @link https://www.brainstormforce.com/
73 * @since 1.0.0
74 *
75 * @psalm-suppress UndefinedTrait
76 */
77 class GlobalSearchController {
78
79 use SingletonLoader;
80
81 /**
82 * Search post by post type.
83 *
84 * @param array $data Search Params.
85 *
86 * @since 1.0.0
87 *
88 * @return array
89 */
90 public function search_post( $data ) {
91 $result = [];
92 $posts = Utilities::find_posts_by_title( $data );
93
94 foreach ( $posts['results'] as $post ) {
95 $result[] = [
96 'label' => $post['post_title'],
97 'value' => $post['ID'],
98 ];
99 }
100
101 return [
102 'options' => $result,
103 'hasMore' => $posts['has_more'],
104 ];
105 }
106
107 /**
108 * Search Course.
109 *
110 * @param array $data quesry params.
111 *
112 * @return array
113 * @since 1.0.0
114 */
115 public function search_ld_course( $data ) {
116 $courses = get_posts(
117 [
118
119 'post_type' => 'product',
120 'meta_key' => '_related_course',
121 'post_status' => 'publish',
122 ]
123 );
124 $options = [];
125 foreach ( $courses as $course ) {
126 $options[] = [
127 'label' => $course->post_title,
128 'value' => $course->ID,
129 ];
130 }
131
132 return [
133 'options' => $options,
134 'hasMore' => false,
135 ];
136 }
137
138 /**
139 * Search achievement by post type.
140 *
141 * @param array $data Search Params.
142 *
143 * @since 1.0.0
144 *
145 * @return array
146 */
147 public function search_achievements( $data ) {
148 $post = get_post( $data['dynamic'] );
149 $slug = $post->post_name;
150
151 $achievements = get_posts(
152 [
153 'post_type' => $slug,
154 'post_status' => 'publish',
155 ]
156 );
157 $options = [];
158 foreach ( $achievements as $achievement ) {
159 $options[] = [
160 'label' => $achievement->post_title,
161 'value' => (string) $achievement->ID,
162 ];
163 }
164
165 return [
166 'options' => $options,
167 'hasMore' => false,
168 ];
169 }
170
171 /**
172 * Search Course.
173 *
174 * @param array $data quesry params.
175 *
176 * @return array
177 * @since 1.0.0
178 */
179 public function search_tutor_course( $data ) {
180 $courses = get_posts(
181 [
182 'post_type' => tutor()->course_post_type,
183 'post_status' => 'publish',
184 'numberposts' => '-1',
185 ]
186 );
187 $options = [];
188 foreach ( $courses as $course ) {
189 $options[] = [
190 'label' => $course->post_title,
191 'value' => $course->ID,
192 ];
193 }
194
195 return [
196 'options' => $options,
197 'hasMore' => false,
198 ];
199 }
200
201 /**
202 * Search Products.
203 *
204 * @param array $data quesry params.
205 *
206 * @return array
207 * @since 1.0.0
208 */
209 public function search_product( $data ) {
210 $result = [];
211 $posts = Utilities::find_posts_by_title( $data );
212
213 foreach ( $posts['results'] as $post ) {
214 $result[] = [
215 'label' => $post['post_title'],
216 'value' => $post['post_title'],
217 ];
218 }
219
220 return [
221 'options' => $result,
222 'hasMore' => $posts['has_more'],
223 ];
224 }
225
226 /**
227 * Search Product Categories.
228 *
229 * @param array $data Search Params.
230 *
231 * @since 1.0.0
232 */
233 public function search_product_category( $data ) {
234 if ( ! empty( $data['dynamic'] ) ) {
235 $taxonomy = $data['dynamic'];
236 } else {
237 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
238 }
239
240 $term = $data['term'];
241 $result = [];
242 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
243 foreach ( $terms['result'] as $tax_term ) {
244 $result[] = [
245 'label' => $tax_term->name,
246 'value' => $tax_term->name,
247 ];
248 }
249
250 return [
251 'options' => $result,
252 'hasMore' => $terms['has_more'],
253 ];
254 }
255
256 /**
257 * Search Product Tags.
258 *
259 * @param array $data Search Params.
260 *
261 * @since 1.0.0
262 */
263 public function search_product_tags( $data ) {
264 if ( ! empty( $data['dynamic'] ) ) {
265 $taxonomy = $data['dynamic'];
266 } else {
267 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
268 }
269
270 $term = $data['term'];
271 $result = [];
272 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
273
274 foreach ( $terms['result'] as $tax_term ) {
275 $result[] = [
276 'label' => $tax_term->name,
277 'value' => $tax_term->name,
278 ];
279 }
280
281 return [
282 'options' => $result,
283 'hasMore' => $terms['has_more'],
284 ];
285 }
286
287 /**
288 * Global ajax search.
289 * Here you need to add the field action name to work.
290 *
291 * @param WP_REST_Request $request Request data.
292 *
293 * @return WP_REST_Response
294 * @since 1.0.0
295 */
296 public function global_search( $request ) {
297 $post_type = $request->get_param( 'post_type' );
298 $dynamic = $request->get_param( 'dynamic' );
299 $search_term = $request->get_param( 'term' );
300 $identifier = $request->get_param( 'field_identifier' );
301 $page = max( 1, $request->get_param( 'page' ) );
302 $taxonomy = $request->get_param( 'taxonomy' ) ? $request->get_param( 'taxonomy' ) : [];
303
304 $filter = $request->get_param( 'filter' ) ? json_decode( stripslashes( $request->get_param( 'filter' ) ), true ) : [];
305
306 $data = [
307 'dynamic' => $dynamic,
308 'search_term' => $search_term,
309 'page' => $page,
310 'taxonomy' => $taxonomy,
311 'filter' => $filter,
312 'post_type' => $post_type,
313 ];
314 $response = [
315 'hasMore' => false,
316 'options' => [],
317 ];
318
319 $method_name = 'search_' . $identifier;
320
321 if ( method_exists( $this, $method_name ) ) {
322 $response = $this->{$method_name}( $data );
323 } else {
324 return RestController::error_message( 'Invalid field Identifier param.' );
325 }
326
327 return RestController::success_message( $response );
328 }
329
330 /**
331 * Search Taxonomy Terms.
332 *
333 * @param array $data Search Params.
334 *
335 * @since 1.0.0
336 *
337 * @return array
338 */
339 public function search_term( $data ) {
340 if ( ! empty( $data['dynamic'] ) ) {
341 $taxonomy = $data['dynamic'];
342 } else {
343 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
344 }
345
346 $term = $data['term'];
347 $result = [];
348 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
349 foreach ( $terms['result'] as $tax_term ) {
350 $result[] = [
351 'label' => $tax_term->name,
352 'value' => $tax_term->term_id,
353 ];
354 }
355
356 return [
357 'options' => $result,
358 'hasMore' => $terms['has_more'],
359 ];
360 }
361
362 /**
363 * Search users.
364 *
365 * @param array $data Search Params.
366 *
367 * @since 1.0.0
368 *
369 * @return array
370 */
371 public function search_user( $data ) {
372 $result = [];
373 $page = $data['page'];
374 $users = Utilities::get_users( $data, $page );
375
376 if ( is_array( $users['results'] ) ) {
377 foreach ( $users['results'] as $user ) {
378 $result[] = [
379 'label' => $user->user_login,
380 'value' => $user->ID,
381 ];
382 }
383 }
384
385 return [
386 'options' => $result,
387 'hasMore' => $users['has_more'],
388 ];
389
390 }
391
392 /**
393 * Search WPForm fields.
394 *
395 * @param array $data Search Params.
396 *
397 * @since 1.0.0
398 *
399 * @return array
400 */
401 public function search_pluggable_wpform_fields( $data ) {
402 $result = [];
403 $page = $data['page'];
404 $form_id = absint( $data['dynamic'] );
405 $wpform_fields = Utilities::get_wpform_fields( $data['search_term'], $page, $form_id );
406
407 if ( is_array( $wpform_fields['results'] ) ) {
408 foreach ( $wpform_fields['results'] as $field ) {
409 $result[] = [
410 'label' => $field['label'],
411 'value' => '{' . $field['id'] . '}',
412 ];
413 }
414 }
415
416 return [
417 'options' => $result,
418 'hasMore' => $wpform_fields['has_more'],
419 ];
420 }
421
422 /**
423 * Prepare variable products.
424 *
425 * @param array $data data.
426 *
427 * @return array
428 */
429 public function search_variable_products( $data ) {
430 $products = Utilities::get_variable_products( $data['search_term'], $data['page'] );
431 $options = [];
432
433 foreach ( $products['result'] as $product ) {
434 $options[] = [
435 'label' => $product->get_title(),
436 'value' => (string) $product->get_id(),
437 ];
438 }
439
440 return [
441 'options' => $options,
442 'hasMore' => $products['has_more'],
443 ];
444 }
445
446 /**
447 * Prepare variable products.
448 *
449 * @param array $data data.
450 *
451 * @return array
452 */
453 public function search_product_variations( $data ) {
454 $variations = Utilities::get_product_variations( $data['dynamic'] );
455 $options = [];
456
457 foreach ( $variations['result'] as $product ) {
458 $options[] = [
459 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
460 'value' => (string) $product->ID,
461 ];
462 }
463
464 return [
465 'options' => $options,
466 'hasMore' => $variations['has_more'],
467 ];
468 }
469
470 /**
471 * Search WooCommerce Subscriptions.
472 *
473 * @param array $data Search Params.
474 *
475 * @since 1.0.0
476 *
477 * @return array
478 */
479 public function search_subscription_variation( $data ) {
480 $subscription_products = Utilities::get_subscription_variation( $data['search_term'], $data['page'] );
481 $result = [];
482
483 if ( ! function_exists( 'wc_get_products' ) ) {
484 return $result;
485 }
486
487 foreach ( $subscription_products['result'] as $post ) {
488 if ( $data['search_term'] ) {
489 if ( false !== stripos( $post->get_title(), $data['search_term'] ) ) {
490 $result[] = [
491 'label' => $post->get_title(),
492 'value' => (string) $post->get_id(),
493 ];
494 }
495 } else {
496 $result[] = [
497 'label' => $post->get_title(),
498 'value' => (string) $post->get_id(),
499 ];
500 }
501 }
502
503 return [
504 'options' => $result,
505 'hasMore' => $subscription_products['has_more'],
506 ];
507 }
508
509 /**
510 * Prepare WooCommerce Payment Methods.
511 *
512 * @param array $data Search Params.
513 * @return array[]
514 */
515 public function search_woo_payment_methods( $data ) {
516 $payment_methods = WC()->payment_gateways->get_available_payment_gateways();
517 $options = [];
518
519 if ( ! empty( $payment_methods ) ) {
520 foreach ( $payment_methods as $payment ) {
521 $options[] = [
522 'label' => $payment->title,
523 'value' => $payment->id,
524 ];
525 }
526 }
527
528 return [
529 'options' => $options,
530 'hasMore' => false,
531 ];
532 }
533
534 /**
535 * Prepare WooCommerce Order Status List.
536 *
537 * @param array $data Search Params.
538 * @return array[]
539 */
540 public function search_woo_order_status_list( $data ) {
541 $order_status = wc_get_order_statuses();
542 $options = [];
543
544 if ( ! empty( $order_status ) ) {
545 foreach ( $order_status as $key => $status ) {
546 $options[] = [
547 'label' => $status,
548 'value' => $key,
549 ];
550 }
551 }
552
553 return [
554 'options' => $options,
555 'hasMore' => false,
556 ];
557 }
558
559 /**
560 * Prepare WooCommerce Country List.
561 *
562 * @param array $data Search Params.
563 * @return array[]
564 */
565 public function search_woo_country_list( $data ) {
566 $countries = WC()->countries->get_countries();
567 $options = [];
568
569 if ( ! empty( $countries ) ) {
570 foreach ( $countries as $key => $country ) {
571 $options[] = [
572 'label' => $country,
573 'value' => $key,
574 ];
575 }
576 }
577
578 return [
579 'options' => $options,
580 'hasMore' => false,
581 ];
582 }
583
584 /**
585 * Prepare WooCommerce Country States List.
586 *
587 * @param array $data Search Params.
588 * @return array[]
589 */
590 public function search_woo_country_state_list( $data ) {
591 if ( ! empty( $data['dynamic']['shipping_country'] ) ) {
592 $cc = $data['dynamic']['shipping_country'];
593 } else {
594 $cc = $data['dynamic'];
595 }
596
597 $states = WC()->countries->get_states( $cc );
598
599 $options = [];
600 if ( ! empty( $states ) ) {
601 foreach ( $states as $key => $state ) {
602 $options[] = [
603 'label' => $state,
604 'value' => $key,
605 ];
606 }
607 }
608
609 return [
610 'options' => $options,
611 'hasMore' => false,
612 ];
613 }
614
615 /**
616 * Get Memberpress gatways (payment methods) for subscription.
617 *
618 * @param array $data QueryParams.
619 *
620 * @return array
621 * @since 1.0.0
622 */
623 public function search_memberpress_gayways( $data ) {
624 $mp_options = MeprOptions::fetch();
625
626 $pms = array_keys( $mp_options->integrations );
627 $gateways = [];
628
629 foreach ( $pms as $pm_id ) {
630 $obj = $mp_options->payment_method( $pm_id );
631 if ( $obj instanceof MeprBaseRealGateway ) {
632 $gateways[] = [
633 'label' => sprintf( '%1$s (%2$s)', $obj->label, $obj->name ),
634 'value' => $obj->id,
635 ];
636 }
637 }
638
639 return [
640 'options' => $gateways,
641 'hasMore' => false,
642 ];
643 }
644
645 /**
646 * Prepare roles.
647 *
648 * @param array $data data.
649 *
650 * @return array
651 */
652 public function search_roles( $data ) {
653 $roles = wp_roles()->roles;
654 $options = [];
655 foreach ( $roles as $role => $details ) {
656
657 $options[] = [
658 'label' => $details['name'],
659 'value' => $role,
660 ];
661
662 }
663
664 return [
665 'options' => $options,
666 'hasMore' => false,
667 ];
668 }
669
670 /**
671 * Fetch operators.
672 *
673 * @return array
674 */
675 public function search_condition_operators() {
676 return [
677 'options' => EventHelperController::get_instance()->prepare_operators(),
678 'hasMore' => false,
679 ];
680 }
681
682 /**
683 * Prepare post types.
684 *
685 * @param array $data data.
686 *
687 * @return array
688 */
689 public function search_post_types( $data ) {
690 $post_types = get_post_types( [ 'public' => true ], 'object' );
691 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
692 if ( isset( $post_types['attachment'] ) ) {
693 unset( $post_types['attachment'] );
694 }
695
696 $options = [];
697 foreach ( $post_types as $post_type => $details ) {
698 $options[] = [
699 'label' => $details->label,
700 'value' => $post_type,
701 ];
702 }
703
704 return [
705 'options' => $options,
706 'hasMore' => false,
707 ];
708 }
709
710 /**
711 * Get post statuses.
712 *
713 * @param array $data data.
714 *
715 * @return array
716 */
717 public function search_post_statuses( $data ) {
718 $post_statuses = get_post_stati( [], 'objects' );
719 $post_statuses = apply_filters( 'suretriggers_post_types', $post_statuses );
720 $options = [];
721
722 foreach ( $post_statuses as $post_status => $details ) {
723 if ( 'woocommerce' === $details->label_count['domain'] ) {
724 $options[] = [
725 'label' => 'WooCommerce - ' . $details->label,
726 'value' => $post_status,
727 ];
728 } else {
729 $options[] = [
730 'label' => $details->label,
731 'value' => $post_status,
732 ];
733 }
734 }
735
736 return [
737 'options' => $options,
738 'hasMore' => false,
739 ];
740 }
741
742 /**
743 * Get Taxonomies.
744 *
745 * @param array $data data.
746 *
747 * @return array
748 */
749 public function search_taxonomy_list( $data ) {
750 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
751 $options = [];
752 $options[0] = [
753 'label' => 'Any Taxonomy',
754 'value' => -1,
755 ];
756
757 foreach ( $taxonomies as $taxonomy => $taxonomy_obj ) {
758 $options[] = [
759 'label' => $taxonomy_obj->label,
760 'value' => $taxonomy_obj->name,
761 ];
762 }
763
764 return [
765 'options' => $options,
766 'hasMore' => false,
767 ];
768 }
769
770 /**
771 * Get WPForms.
772 *
773 * @param array $data data.
774 *
775 * @return array
776 */
777 public function search_wp_forms( $data ) {
778 if ( ! class_exists( 'WPForms_Form_Handler' ) ) {
779 return;
780 }
781
782 $wpforms = new WPForms_Form_Handler();
783 $forms = $wpforms->get( '', [ 'orderby' => 'title' ] );
784 $options = [];
785
786 if ( ! empty( $forms ) ) {
787 foreach ( $forms as $form ) {
788 $options[] = [
789 'label' => $form->post_title,
790 'value' => $form->ID,
791 ];
792 }
793 }
794
795 return [
796 'options' => $options,
797 'hasMore' => false,
798 ];
799 }
800
801 /**
802 * Get Gravity Forms.
803 *
804 * @param array $data data.
805 *
806 * @return array
807 */
808 public function search_gravity_forms( $data ) {
809 if ( ! class_exists( 'GFFormsModel' ) ) {
810 return;
811 }
812
813 $forms = GFFormsModel::get_forms();
814 $options = [];
815
816 if ( ! empty( $forms ) ) {
817 foreach ( $forms as $form ) {
818 $options[] = [
819 'label' => $form->title,
820 'value' => $form->id,
821 ];
822 }
823 }
824
825 return [
826 'options' => $options,
827 'hasMore' => false,
828 ];
829 }
830
831 /**
832 * Get tag & contact details.
833 *
834 * @param array $data data.
835 *
836 * @return array
837 */
838 public function search_pluggables_fluentcrm_contact_added_to_tags( $data ) {
839 $context = [];
840 $pluggable_data = [];
841 $tag_id = $data['filter'];
842
843 if ( $tag_id > 0 ) {
844 $tags = Tag::where( 'id', $tag_id )->first();
845 } else {
846 $tags = Tag::orderBy( 'id', 'DESC' )->first();
847 }
848 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
849 if ( $contact ) {
850 $pluggable_data['contact'] = $contact;
851 $context['tag_id'] = $tag_id;
852 $pluggable_data['tag'] = $tags;
853 $context['response_type'] = 'live';
854 } else {
855 $pluggable_data['conatct']['full_name'] = 'Test User';
856 $pluggable_data['conatct']['first_name'] = 'Test';
857 $pluggable_data['conatct']['last_name'] = 'User';
858 $pluggable_data['conatct']['company_id'] = 112;
859 $pluggable_data['conatct']['email'] = 'testuser@gmail.com';
860 $pluggable_data['conatct']['address_line_1'] = '33, Vincent Road';
861 $pluggable_data['conatct']['address_line_2'] = 'Chicago Street';
862 $pluggable_data['conatct']['postal_code'] = '212342';
863 $pluggable_data['conatct']['city'] = 'New York City';
864 $pluggable_data['conatct']['state'] = 'New York';
865 $pluggable_data['conatct']['country'] = 'USA';
866 $pluggable_data['conatct']['phone'] = '9992191911';
867 $pluggable_data['conatct']['status'] = 'subscribed';
868 $pluggable_data['conatct']['contact_type'] = 'lead';
869 $pluggable_data['conatct']['source'] = '';
870 $pluggable_data['conatct']['date_of_birth'] = '2022-11-09';
871 $context['tag_id'] = 1;
872 $pluggable_data['tag'] =
873 [
874 'id' => '1',
875 'title' => 'new',
876 'slug' => 'new',
877 'description' => null,
878 'created_at' => '2023-01-19 10:23:23',
879 'updated_at' => '2023-01-19 10:23:23',
880 'pivot' => [
881 'subscriber_id' => '1',
882 'object_id' => '1',
883 'object_type' => 'FluentCrm\\App\\Models\\Tag',
884 'created_at' => '2023-01-19 10:42:55',
885 'updated_at' => '2023-01-19 10:42:55',
886
887 ],
888 ];
889 $context['response_type'] = 'sample';
890 }
891
892 $context['pluggable_data'] = $pluggable_data;
893 return $context;
894 }
895
896 /**
897 * Get FluentCRM contact details.
898 *
899 * @param array $data data.
900 *
901 * @return array
902 */
903 public function search_pluggables_fluentcrm_contact_added( $data ) {
904 $context = [];
905 $pluggable_data = [];
906
907 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
908 if ( $contact ) {
909 /**
910 *
911 * Ignore line
912 *
913 * @phpstan-ignore-next-line
914 */
915 $subscriber = Subscriber::with( [ 'tags', 'lists' ] )->find( $contact->id );
916 $customer_fields = $subscriber->custom_fields();
917 $pluggable_data['contact']['details'] = $subscriber;
918 $pluggable_data['contact']['custom'] = $customer_fields;
919 $context['response_type'] = 'live';
920 } else {
921 $pluggable_data['contact']['details']['full_name'] = 'Test User';
922 $pluggable_data['contact']['details']['first_name'] = 'Test';
923 $pluggable_data['contact']['details']['last_name'] = 'User';
924 $pluggable_data['contact']['details']['company_id'] = 112;
925 $pluggable_data['contact']['details']['email'] = 'testuser@gmail.com';
926 $pluggable_data['contact']['details']['address_line_1'] = '33, Vincent Road';
927 $pluggable_data['contact']['details']['address_line_2'] = 'Chicago Street';
928 $pluggable_data['contact']['details']['postal_code'] = '212342';
929 $pluggable_data['contact']['details']['city'] = 'New York City';
930 $pluggable_data['contact']['details']['state'] = 'New York';
931 $pluggable_data['contact']['details']['country'] = 'USA';
932 $pluggable_data['contact']['details']['phone'] = '9992191911';
933 $pluggable_data['contact']['details']['status'] = 'subscribed';
934 $pluggable_data['contact']['details']['contact_type'] = 'lead';
935 $pluggable_data['contact']['details']['source'] = '';
936 $pluggable_data['contact']['details']['date_of_birth'] = '2022-11-09';
937 $context['response_type'] = 'sample';
938 }
939
940 $context['pluggable_data'] = $pluggable_data;
941 return $context;
942 }
943
944 /**
945 * Get Divi Forms.
946 *
947 * @param array $data data.
948 *
949 * @return array
950 */
951 public function search_divi_forms( $data ) {
952 $form_posts = Utilities::get_divi_forms();
953 $options = [];
954
955 if ( empty( $form_posts ) ) {
956 return [
957 'options' => [],
958 'hasMore' => false,
959 ];
960 }
961
962 foreach ( $form_posts as $form_post ) {
963 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
964 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
965 if ( empty( $forms ) ) {
966 continue;
967 }
968
969 $count = 0;
970
971 foreach ( $forms as $form ) {
972 $pattern_form = get_shortcode_regex( [ 'et_pb_contact_form' ] );
973 preg_match_all( "/$pattern_form/", $form[0], $forms_extracted, PREG_SET_ORDER );
974
975 if ( empty( $forms_extracted ) ) {
976 continue;
977 }
978
979 foreach ( $forms_extracted as $form_extracted ) {
980 $form_attrs = shortcode_parse_atts( $form_extracted[3] );
981 $form_id = isset( $form_attrs['_unique_id'] ) ? $form_attrs['_unique_id'] : '';
982 if ( empty( $form_id ) ) {
983 continue;
984 }
985 $form_id = sprintf( '%d-%s', $form_post['ID'], $form_id );
986 $form_title = isset( $form_attrs['title'] ) ? $form_attrs['title'] : '';
987 $form_title = sprintf( '%s %s', $form_post['post_title'], $form_title );
988 $options[] = [
989 'label' => $form_title,
990 'value' => $form_id,
991 ];
992 }
993 $count++;
994 }
995 }
996
997 return [
998 'options' => $options,
999 'hasMore' => false,
1000 ];
1001 }
1002
1003 /**
1004 * Get Comment Pluggable data.
1005 *
1006 * @param array $data data.
1007 *
1008 * @return array
1009 */
1010 public function search_pluggables_wp_insert_comment( $data ) {
1011 $context = [];
1012 $post_data = [];
1013 $args = [
1014 'number' => '1',
1015 'status' => 'approve',
1016 'post_type' => $data['filter']['post_type']['value'],
1017 ];
1018
1019 if ( isset( $data['filter']['post']['value'] ) ) {
1020 $post_id = $data['filter']['post']['value'];
1021 if ( $post_id > 0 ) {
1022 $args['post_id'] = $post_id;
1023 }
1024 }
1025
1026 $comments = get_comments( $args );
1027 if ( empty( $comments ) ) {
1028 unset( $args['post_id'] );
1029 $comments = get_comments( $args );
1030 }
1031 $context['context_data'] = $data;
1032 $context['context_args'] = $args;
1033 if ( ! empty( $comments ) ) {
1034 foreach ( $comments as $comment ) :
1035 if ( is_object( $comment ) ) {
1036 $comment = get_object_vars( $comment );
1037 }
1038 if ( is_array( $comment ) && isset( $comment['comment_post_ID'] ) ) {
1039 $post = get_post( absint( $comment['comment_post_ID'] ) );
1040 if ( is_object( $post ) ) {
1041 if ( property_exists( $post, 'ID' ) || property_exists( $post, 'post_author' ) || property_exists( $post, 'post_title' ) ) {
1042 $post_id = $post->ID;
1043 $postauthor = (int) $post->post_author;
1044 if ( is_array( $comment ) ) {
1045 $context['pluggable_data'] = [
1046 'post' => $post_id,
1047 'post_title' => $post->post_title,
1048 'post_author' => get_the_author_meta( 'display_name', $postauthor ),
1049 'post_link' => get_the_permalink( $post_id ),
1050 'comment_id' => $comment['comment_ID'],
1051 'comment' => $comment['comment_content'],
1052 'comment_author' => $comment['comment_author'],
1053 'comment_author_email' => $comment['comment_author_email'],
1054 'comment_date' => $comment['comment_date'],
1055 ];
1056 }
1057 }
1058 }
1059 }
1060 if ( is_array( $comment ) && isset( $comment['comment_author_email'] ) ) {
1061 $user_email = $comment['comment_author_email'];
1062 /**
1063 *
1064 * Ignore line
1065 *
1066 * @phpstan-ignore-next-line
1067 */
1068 $user = get_user_by( 'email', $user_email );
1069 if ( $user ) {
1070 $context['pluggable_data']['wp_user_id'] = $user->ID;
1071 $context['pluggable_data']['user_login'] = $user->user_login;
1072 $context['pluggable_data']['display_name'] = $user->display_name;
1073 $context['pluggable_data']['user_firstname'] = $user->user_firstname;
1074 $context['pluggable_data']['user_lastname'] = $user->user_lastname;
1075 $context['pluggable_data']['user_email'] = $user->user_email;
1076 $context['pluggable_data']['user_role'] = $user->roles;
1077 }
1078 }
1079
1080 $context['response_type'] = 'live';
1081 endforeach;
1082 } else {
1083 $sample_comment = [
1084 'post' => 100,
1085 'post_title' => 'Sample Post',
1086 'comment_id' => 101,
1087 'comment' => 'Sample Comment',
1088 ];
1089 $sample_comment['wp_user_id'] = 7;
1090 $sample_comment['user_login'] = 'testuser@gmail.com';
1091 $sample_comment['display_name'] = 'Test User';
1092 $sample_comment['user_firstname'] = 'Test';
1093 $sample_comment['user_lastname'] = 'User';
1094 $sample_comment['user_email'] = 'testuser@gmail.com';
1095 $sample_comment['user_role'] = [ 'Subscriber' ];
1096
1097 $context['pluggable_data'] = $sample_comment;
1098 $context['response_type'] = 'sample';
1099 }
1100 return $context;
1101 }
1102
1103 /**
1104 * User reset password.
1105 *
1106 * @param array $data data.
1107 * @return array
1108 */
1109 public function search_pluggables_user_reset_password( $data ) {
1110 $user_context = $this->search_pluggables_add_user_role( $data );
1111 $user_context['pluggable_data']['new_password'] = '***password***';
1112 return $user_context;
1113 }
1114
1115 /**
1116 * User pluggable data.
1117 *
1118 * @param array $data data.
1119 * @return array
1120 */
1121 public function search_pluggables_add_user_role( $data ) {
1122 $context = [];
1123 $args = [
1124 'order' => 'DESC',
1125 'number' => 1,
1126 'orderby' => 'ID',
1127 ];
1128
1129 if ( isset( $data['filter']['role']['value'] ) ) {
1130 $role = $data['filter']['role']['value'];
1131 $args['role'] = $role;
1132 }
1133 if ( isset( $data['filter']['new_role']['value'] ) ) {
1134 $role = $data['filter']['new_role']['value'];
1135 $args['role'] = $role;
1136 }
1137
1138 $users = get_users( $args );
1139
1140 if ( isset( $data['filter']['meta_key']['value'] ) ) {
1141 $meta_key = $data['filter']['meta_key']['value'];
1142 $args['st_meta_key'] = $meta_key;
1143 }
1144
1145 if ( isset( $data['filter']['profile_field']['value'] ) ) {
1146 $meta_key = $data['filter']['profile_field']['value'];
1147 $args['profile_field'] = $meta_key;
1148 }
1149
1150 if ( ! empty( $users ) ) {
1151 $user = $users[0];
1152 $pluggable_data = [];
1153 $pluggable_data['wp_user_id'] = $user->ID;
1154 $pluggable_data['user_login'] = $user->user_login;
1155 $pluggable_data['display_name'] = $user->display_name;
1156 $pluggable_data['user_firstname'] = $user->user_firstname;
1157 $pluggable_data['user_lastname'] = $user->user_lastname;
1158 $pluggable_data['user_email'] = $user->user_email;
1159 $pluggable_data['user_role'] = $user->roles;
1160 if ( isset( $args['st_meta_key'] ) ) {
1161 $pluggable_data['meta_key'] = $args['st_meta_key'];
1162 $pluggable_data['meta_value'] = get_user_meta( $user->ID, $args['st_meta_key'], true );
1163 }
1164 if ( isset( $args['profile_field'] ) ) {
1165 $userdata = get_userdata( $user->ID );
1166 $userdata = json_decode( wp_json_encode( $userdata->data ), true );
1167
1168 $pluggable_data['profile_field'] = $args['profile_field'];
1169 $pluggable_data['profile_field_value'] = $userdata[ $args['profile_field'] ];
1170 }
1171 $context['pluggable_data'] = $pluggable_data;
1172 $context['response_type'] = 'live';
1173 } else {
1174 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
1175 $context['pluggable_data'] = [
1176 'wp_user_id' => 1,
1177 'user_login' => 'admin',
1178 'display_name' => 'Test User',
1179 'user_firstname' => 'Test',
1180 'user_lastname' => 'User',
1181 'user_email' => 'testuser@gmail.com',
1182 'user_role' => [ $role ],
1183 ];
1184 if ( isset( $args['st_meta_key'] ) ) {
1185 $context['pluggable_data']['meta_key'] = $args['st_meta_key'];
1186 $context['pluggable_data']['meta_value'] = 'test meta value';
1187 }
1188 if ( isset( $args['profile_field'] ) ) {
1189 $context['pluggable_data']['profile_field'] = $args['profile_field'];
1190 $context['pluggable_data']['profile_field_value'] = 'Profile Field Value';
1191 }
1192 $context['response_type'] = 'sample';
1193 }
1194 return $context;
1195 }
1196
1197 /**
1198 * User pluggable data.
1199 *
1200 * @param array $data data.
1201 * @return array
1202 */
1203 public function search_pluggables_last_user_login( $data ) {
1204 $context = [];
1205 $args = [
1206 'orderby' => 'meta_value',
1207 'meta_key' => 'st_last_login',
1208 'order' => 'DESC',
1209 'number' => 1,
1210 ];
1211 $users = get_users( $args );
1212
1213 if ( ! empty( $users ) ) {
1214 $user = $users[0];
1215 $pluggable_data = [];
1216 $pluggable_data['wp_user_id'] = $user->ID;
1217 $pluggable_data['user_login'] = $user->user_login;
1218 $pluggable_data['display_name'] = $user->display_name;
1219 $pluggable_data['user_firstname'] = $user->user_firstname;
1220 $pluggable_data['user_lastname'] = $user->user_lastname;
1221 $pluggable_data['user_email'] = $user->user_email;
1222 $pluggable_data['user_role'] = $user->roles;
1223
1224 $context['pluggable_data'] = $pluggable_data;
1225 $context['response_type'] = 'live';
1226 } else {
1227 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
1228 $context['pluggable_data'] = [
1229 'wp_user_id' => 1,
1230 'user_login' => 'admin',
1231 'display_name' => 'Test User',
1232 'user_firstname' => 'Test',
1233 'user_lastname' => 'User',
1234 'user_email' => 'testuser@gmail.com',
1235 'user_role' => [ $role ],
1236 ];
1237 $context['response_type'] = 'sample';
1238 }
1239 return $context;
1240 }
1241
1242 /**
1243 * Donation pluggable data.
1244 *
1245 * @param array $data data.
1246 * @return array
1247 */
1248 public function search_pluggables_wordpress_post( $data ) {
1249 $context = [];
1250 $args = [
1251 'post_type' => 'any',
1252 'posts_per_page' => 1,
1253 'orderby' => 'modified',
1254 'order' => 'DESC',
1255 ];
1256
1257 if ( isset( $data['filter']['post_type']['value'] ) ) {
1258 $post_type = $data['filter']['post_type']['value'];
1259 $args['post_type'] = $post_type;
1260 }
1261
1262 if ( isset( $data['filter']['status']['value'] ) ) {
1263 $post_status = $data['filter']['status']['value'];
1264 $args['post_status'] = $post_status;
1265 }
1266
1267 if ( isset( $data['filter']['post_status']['value'] ) ) {
1268 $post_status = $data['filter']['post_status']['value'];
1269 $args['post_status'] = $post_status;
1270 }
1271
1272 if ( isset( $data['filter']['post']['value'] ) ) {
1273 $post_id = $data['filter']['post']['value'];
1274 if ( $post_id > 0 ) {
1275 $args['p'] = $post_id;
1276 unset( $args['post_status'] );
1277 }
1278 }
1279
1280 $posts = get_posts( $args );
1281 if ( ! empty( $posts ) ) {
1282 $context['pluggable_data'] = $posts[0];
1283 $custom_metas = get_post_meta( $posts[0]->ID );
1284 if ( property_exists( $context['pluggable_data'], 'post' ) ) {
1285 $context['pluggable_data']->post = $posts[0]->ID;
1286 }
1287 if ( is_object( $context['pluggable_data'] ) ) {
1288 $context['pluggable_data'] = get_object_vars( $context['pluggable_data'] );
1289 }
1290 $context['pluggable_data']['custom_metas'] = $custom_metas;
1291 $context['response_type'] = 'live';
1292 } else {
1293 $context['pluggable_data'] = [
1294 'ID' => 557,
1295 'post' => 557,
1296 'post_author' => 1,
1297 'post_date' => '2022-11-18 12:18:14',
1298 'post_date_gmt' => '2022-11-18 12:18:14',
1299 'post_content' => 'Test Post Content',
1300 'post_title' => 'Test Post',
1301 'post_excerpt' => '',
1302 'post_status' => $args['post_status'],
1303 'comment_status' => 'open',
1304 'ping_status' => 'open',
1305 'post_password' => '',
1306 'post_name' => 'test-post',
1307 'to_ping' => '',
1308 'pinged' => '',
1309 'post_modified' => '2022-11-18 12:18:14',
1310 'post_modified_gmt' => '2022-11-18 12:18:14',
1311 'post_content_filtered' => '',
1312 'post_parent' => 0,
1313 'guid' => 'https://abc.com/test-post/',
1314 'menu_order' => 0,
1315 'post_type' => 'post',
1316 'post_mime_type' => '',
1317 'comment_count' => 0,
1318 'filter' => 'raw',
1319 ];
1320 $context['response_type'] = 'sample';
1321 }
1322
1323 return $context;
1324 }
1325
1326 /**
1327 * Donation pluggable data
1328 *
1329 * @param array $data data.
1330 * @return array
1331 */
1332 public function search_pluggables_givewp_donation_via_form( $data ) {
1333 global $wpdb;
1334 $context = [];
1335 $pluggable_data = [];
1336
1337 $payment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type=%s ORDER BY id DESC LIMIT 1", 'give_payment' ) );
1338
1339 if ( ! empty( $payment ) ) {
1340 $payment = new Give_Payment( $payment->ID );
1341 $address_data = $payment->address;
1342
1343 $pluggable_data['first_name'] = $payment->first_name;
1344 $pluggable_data['last_name'] = $payment->last_name;
1345 $pluggable_data['email'] = $payment->email;
1346 $pluggable_data['currency'] = $payment->currency;
1347 $pluggable_data['donated_amount'] = $payment->subtotal;
1348 $pluggable_data['donation_amount'] = $payment->subtotal;
1349 $pluggable_data['form_id'] = (int) $payment->form_id;
1350 $pluggable_data['form_title'] = $payment->form_title;
1351 $pluggable_data['name_title_prefix'] = $payment->title_prefix;
1352 $pluggable_data['date'] = $payment->date;
1353
1354 if ( is_array( $address_data ) ) {
1355 $pluggable_data['address_line_1'] = $address_data['line1'];
1356 $pluggable_data['address_line_2'] = $address_data['line2'];
1357 $pluggable_data['city'] = $address_data['city'];
1358 $pluggable_data['state'] = $address_data['state'];
1359 $pluggable_data['zip'] = $address_data['zip'];
1360 $pluggable_data['country'] = $address_data['country'];
1361 }
1362 $donor_comment = give_get_donor_donation_comment( $payment->ID, $payment->donor_id );
1363 $pluggable_data['comment'] = isset( $doner['comment_content'] ) ? $donor_comment : '';
1364 $context['response_type'] = 'live';
1365 } else {
1366 $pluggable_data['first_name'] = 'Test';
1367 $pluggable_data['last_name'] = 'User';
1368 $pluggable_data['email'] = 'testuser@gmail.com';
1369 $pluggable_data['currency'] = 'USD';
1370 $pluggable_data['donated_amount'] = 100;
1371 $pluggable_data['donation_amount'] = 100;
1372 $pluggable_data['form_id'] = 23;
1373 $pluggable_data['form_title'] = 'Test Donation';
1374 $pluggable_data['name_title_prefix'] = 'Mr';
1375 $pluggable_data['date'] = '2022-11-07 16:06:05';
1376 $pluggable_data['address_line_1'] = '33, Vincent Road';
1377 $pluggable_data['address_line_2'] = 'Chicago Street';
1378 $pluggable_data['city'] = 'New York City';
1379 $pluggable_data['state'] = 'New York';
1380 $pluggable_data['zip'] = '223131';
1381 $pluggable_data['country'] = 'USA';
1382 $pluggable_data['comment'] = 'Test Comment';
1383 $context['response_type'] = 'sample';
1384 }
1385
1386 $context['pluggable_data'] = $pluggable_data;
1387 return $context;
1388 }
1389
1390 /**
1391 * Search Divi Form fields.
1392 *
1393 * @param array $data Search Params.
1394 *
1395 * @since 1.0.0
1396 *
1397 * @return array
1398 */
1399 public function search_pluggable_divi_form_fields( $data ) {
1400 $result = [];
1401 $form_id = absint( $data['dynamic'] );
1402 $form_posts = Utilities::get_divi_forms();
1403
1404 if ( empty( $form_posts ) ) {
1405 return [
1406 'options' => [],
1407 'hasMore' => false,
1408 ];
1409 }
1410 $fields = [];
1411 foreach ( $form_posts as $form_post ) {
1412 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
1413 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
1414 if ( empty( $forms ) ) {
1415 continue;
1416 }
1417
1418 $count = 0;
1419
1420 foreach ( $forms as $form ) {
1421 $pattern = get_shortcode_regex( [ 'et_pb_contact_field' ] );
1422
1423 preg_match_all( "/$pattern/", $form[0], $contact_fields, PREG_SET_ORDER );
1424
1425 if ( empty( $contact_fields ) ) {
1426 return $fields;
1427 }
1428
1429 foreach ( $contact_fields as $contact_field ) {
1430 $contact_field_attrs = shortcode_parse_atts( $contact_field[3] );
1431 $field_id = strtolower( self::array_get( $contact_field_attrs, 'field_id' ) );
1432 $fields[] = [
1433 'field_title' => self::array_get( $contact_field_attrs, 'field_title', __( 'No title', 'suretriggers' ) ),
1434 'field_id' => $field_id,
1435 ];
1436 }
1437 }
1438 }
1439
1440 if ( ! empty( $fields ) ) {
1441 foreach ( $fields as $field ) {
1442 $result[] = [
1443 'label' => $field['field_title'],
1444 'value' => '{' . $field['field_id'] . '}',
1445 ];
1446 }
1447 }
1448
1449 return [
1450 'options' => $result,
1451 'hasMore' => false,
1452 ];
1453 }
1454
1455 /**
1456 * Pseudo function copied from Divi
1457 *
1458 * @param array $array An array which contains value located at `$address`.
1459 * @param string|array $address The location of the value within `$array` (dot notation).
1460 * @param mixed $default Value to return if not found. Default is an empty string.
1461 *
1462 * @return mixed The value, if found, otherwise $default.
1463 */
1464 public static function array_get( $array, $address, $default = '' ) {
1465 $keys = is_array( $address ) ? $address : explode( '.', $address );
1466 $value = $array;
1467
1468 foreach ( $keys as $key ) {
1469 if ( ! empty( $key ) && isset( $key[0] ) && '[' === $key[0] ) {
1470 $index = substr( $key, 1, -1 );
1471
1472 if ( is_numeric( $index ) ) {
1473 $key = (int) $index;
1474 }
1475 }
1476
1477 if ( ! isset( $value[ $key ] ) ) {
1478 return $default;
1479 }
1480
1481 $value = $value[ $key ];
1482 }
1483
1484 return $value;
1485 }
1486
1487 /**
1488 * Get UAG Forms.
1489 *
1490 * @param array $data data.
1491 *
1492 * @return array
1493 */
1494 public function search_spectra_forms( $data ) {
1495 $form_posts = Utilities::get_uag_forms();
1496
1497 $options = [];
1498 if ( empty( $form_posts ) ) {
1499 return [
1500 'options' => [],
1501 'hasMore' => false,
1502 ];
1503 }
1504
1505 foreach ( $form_posts as $form_post ) {
1506 $blocks = parse_blocks( $form_post['post_content'] );
1507 $i = 1;
1508 foreach ( $blocks as $key => $block ) {
1509 if ( 'uagb/forms' === $block['blockName'] ) {
1510 $options[] = [
1511 'label' => $form_post['post_title'] . ' (Form ' . ( $i ) . ')',
1512 'value' => $block['attrs']['block_id'],
1513 ];
1514 $i++;
1515 } elseif ( 'uagb/forms' !== $block['blockName'] && isset( $block['innerBlocks'] ) ) {
1516 $container_key = self::get_column_by_value( $block['innerBlocks'], 'uagb/container' );
1517 if ( isset( $container_key ) && '' !== $container_key ) {
1518 $options[] = [
1519 'label' => $form_post['post_title'] . ' (Form ' . ( $i ) . ')',
1520 'value' => $block['innerBlocks'][ $container_key ]['attrs']['block_id'],
1521 ];
1522 $i++;
1523 }
1524 }
1525 }
1526 }
1527
1528 return [
1529 'options' => $options,
1530 'hasMore' => false,
1531 ];
1532 }
1533
1534 /**
1535 * Check array recursive.
1536 *
1537 * @param array $array Array.
1538 * @param string $value search params.
1539 * @since 1.0.0
1540 *
1541 * @return array|void
1542 */
1543 public static function get_column_by_value( $array, $value ) {
1544
1545 foreach ( $array as $key => $sub_array ) {
1546
1547 if ( is_array( $sub_array ) ) {
1548 $result = self::get_column_by_value( $sub_array, $value );
1549 if ( null !== $result ) {
1550 return $key;
1551 }
1552 } else {
1553 return $key;
1554 }
1555 }
1556 return null;
1557
1558 }
1559
1560
1561 /**
1562 * Search UAG Form fields.
1563 *
1564 * @param array $data Search Params.
1565 *
1566 * @since 1.0.0
1567 *
1568 * @return array
1569 */
1570 public function search_spectraform_fields( $data ) {
1571 $result = [];
1572 $form_id = absint( $data['dynamic'] );
1573 $form_posts = Utilities::get_uag_forms();
1574
1575 if ( empty( $form_posts ) ) {
1576 return [
1577 'options' => [],
1578 'hasMore' => false,
1579 ];
1580 }
1581
1582 foreach ( $form_posts as $form_post ) {
1583 $blocks = parse_blocks( $form_post['post_content'] );
1584
1585 foreach ( $blocks as $block ) {
1586 if ( (int) $block['attrs']['block_id'] === $form_id ) {
1587 $doc = new DOMDocument();
1588 $rendered_block = render_block( $block );
1589 $doc->loadHTML( $rendered_block );
1590 $child_node_list = $doc->getElementsByTagName( 'div' );
1591 for ( $i = 0; $i < $child_node_list->length; $i++ ) {
1592 $temp = $child_node_list->item( $i );
1593 if ( $temp && stripos( $temp->getAttribute( 'class' ), 'uagb-forms-input-label' ) !== false ) {
1594 $nodes[] = $temp;
1595 }
1596 }
1597
1598 foreach ( $nodes as $node ) {
1599 $result[] = [
1600 'label' => $node->textContent, //phpcs:ignore
1601 'value' => $node->textContent, //phpcs:ignore
1602 ];
1603 }
1604 }
1605 }
1606 }
1607
1608 return [
1609 'options' => $result,
1610 'hasMore' => false,
1611 ];
1612 }
1613
1614 /**
1615 * Search forms of MetForms.
1616 *
1617 * @param array $data data.
1618 * @return array
1619 */
1620 public function search_met_forms( $data ) {
1621 $args = [
1622 'post_type' => 'metform-form',
1623 'post_status' => 'publish',
1624 'numberposts' => -1,
1625 ];
1626
1627 $forms = get_posts( $args );
1628
1629 $options = [];
1630
1631 if ( ! empty( $forms ) ) {
1632 foreach ( $forms as $form ) {
1633 $options[] = [
1634 'label' => $form->post_title,
1635 'value' => $form->ID,
1636 ];
1637 }
1638 }
1639
1640 return [
1641 'options' => $options,
1642 'hasMore' => false,
1643 ];
1644 }
1645
1646 /**
1647 * Search forms of Ninja Forms.
1648 *
1649 * @param array $data data.
1650 * @return array
1651 */
1652 public function search_ninja_forms( $data ) {
1653 $options = [];
1654
1655 if ( function_exists( 'Ninja_Forms' ) ) {
1656 foreach ( Ninja_Forms()->form()->get_forms() as $form ) {
1657 $options[] = [
1658 'label' => $form->get_setting( 'title' ),
1659 'value' => $form->get_id(),
1660 ];
1661 }
1662 }
1663
1664 return [
1665 'options' => $options,
1666 'hasMore' => false,
1667 ];
1668 }
1669
1670 /**
1671 * Search forms of Pie Forms.
1672 *
1673 * @param array $data data.
1674 * @return array
1675 */
1676 public function search_pie_forms( $data ) {
1677 global $wpdb;
1678 $options = [];
1679
1680 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pf_forms' ) ) ) {
1681
1682 $results = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'pf_forms WHERE post_status = "published"' );
1683
1684 if ( $results ) {
1685 foreach ( $results as $result ) {
1686 $options[] = [
1687 'label' => $result->form_title,
1688 'value' => $result->id,
1689 ];
1690 }
1691 }
1692 }
1693
1694 return [
1695 'options' => $options,
1696 'hasMore' => false,
1697 ];
1698 }
1699
1700 /**
1701 * Get Fluent Forms.
1702 *
1703 * @param array $data data.
1704 *
1705 * @return array
1706 */
1707 public function search_fluent_forms( $data ) {
1708
1709 if ( ! function_exists( 'wpFluent' ) ) {
1710 return [
1711 'options' => [],
1712 'hasMore' => false,
1713 ];
1714 }
1715
1716 $forms = wpFluent()->table( 'fluentform_forms' )
1717 ->select( [ 'id', 'title' ] )
1718 ->orderBy( 'id', 'DESC' )
1719 ->get();
1720
1721 $options = [];
1722 if ( ! empty( $forms ) ) {
1723 foreach ( $forms as $form ) {
1724 $options[] = [
1725 'label' => $form->title,
1726 'value' => $form->id,
1727 ];
1728 }
1729 }
1730
1731 return [
1732 'options' => $options,
1733 'hasMore' => false,
1734 ];
1735
1736 }
1737
1738 /**
1739 * Get Fluent Forms.
1740 *
1741 * @param array $data data.
1742 *
1743 * @return array
1744 */
1745 public function search_bricks_builder_forms( $data ) {
1746 $bricks_theme = wp_get_theme( 'bricks' );
1747 if ( ! $bricks_theme->exists() ) {
1748 return [
1749 'options' => [],
1750 'hasMore' => false,
1751 ];
1752 }
1753
1754 $args = [
1755 'post_type' => 'bricks_template',
1756 'post_status' => 'publish',
1757 'posts_per_page' => -1,
1758 ];
1759
1760 $templates = get_posts( $args );
1761
1762 $options = [];
1763 if ( ! empty( $templates ) ) {
1764 foreach ( $templates as $template ) {
1765 $fetch_content = get_post_meta( $template->ID, BRICKS_DB_PAGE_CONTENT, true );
1766 if ( is_array( $fetch_content ) ) {
1767 foreach ( $fetch_content as $content ) {
1768 if ( 'form' === $content['name'] ) {
1769 $options[] = [
1770 'label' => $template->post_title . ' - ' . ( isset( $content['label'] ) ? $content['label'] : 'Form' ),
1771 'value' => $content['id'],
1772 ];
1773 }
1774 }
1775 }
1776 }
1777 }
1778
1779 return [
1780 'options' => $options,
1781 'hasMore' => false,
1782 ];
1783
1784 }
1785
1786 /**
1787 * Bricks builder form fields.
1788 *
1789 * @param array $data data.
1790 * @return array
1791 */
1792 public function search_pluggable_bricks_builder_form_fields( $data ) {
1793 $result = [];
1794 $fields = [];
1795 $form_id_str = $data['dynamic'];
1796 $ids = explode( '_', $form_id_str );
1797 $post_id = $ids[0];
1798 $form_id = $ids[1];
1799 $fetch_content = get_post_meta( $post_id, BRICKS_DB_PAGE_CONTENT, true );
1800 if ( is_array( $fetch_content ) ) {
1801 foreach ( $fetch_content as $content ) {
1802 if ( 'form' === $content['name'] && $form_id === $content['id'] ) {
1803 $fields = $content['settings']['fields'];
1804 break;
1805 }
1806 }
1807 }
1808
1809 if ( ! empty( $fields ) ) {
1810 foreach ( $fields as $field ) {
1811 $result[] = [
1812 'label' => $field['label'],
1813 'value' => '{' . strtolower( $field['label'] ) . '}',
1814 ];
1815 }
1816 }
1817
1818 return [
1819 'options' => $result,
1820 'hasMore' => false,
1821 ];
1822 }
1823
1824 /**
1825 * Get fluent form fields
1826 *
1827 * @param array $data Data array.
1828 *
1829 * @return array
1830 */
1831 public function search_pluggable_fluent_form_fields( $data ) {
1832 $result = [];
1833 $form_id = absint( $data['dynamic'] );
1834
1835 $fluentform_fields = Utilities::get_fluentform_fields( $data['search_term'], -1, $form_id );
1836
1837 if ( is_array( $fluentform_fields['results'] ) ) {
1838 foreach ( $fluentform_fields['results'] as $field ) {
1839 $result[] = [
1840 'label' => $field['text'],
1841 'value' => "{{$field['value']}}",
1842 ];
1843 }
1844 }
1845
1846 $result[] = [
1847 'value' => '{form_id}',
1848 'label' => 'Form ID',
1849 ];
1850
1851 $result[] = [
1852 'value' => '{form_title}',
1853 'label' => 'Form Title',
1854 ];
1855 $result[] = [
1856 'value' => '{entry_id}',
1857 'label' => 'Entry ID',
1858 ];
1859
1860 $result[] = [
1861 'value' => '{entry_source_url}',
1862 'label' => 'Entry Source URL',
1863 ];
1864
1865 $result[] = [
1866 'value' => '{submission_date}',
1867 'label' => 'Submission Date',
1868 ];
1869
1870 $result[] = [
1871 'value' => '{user_ip}',
1872 'label' => 'User IP',
1873 ];
1874
1875 return [
1876 'options' => $result,
1877 'hasMore' => false,
1878 ];
1879 }
1880
1881 /**
1882 * Search Gravity Form fields.
1883 *
1884 * @param array $data Search Params.
1885 *
1886 * @since 1.0.0
1887 */
1888 public function search_gform_fields( $data ) {
1889 if ( ! class_exists( 'RGFormsModel' ) ) {
1890 return [
1891 'options' => [],
1892 'hasMore' => false,
1893 ];
1894 }
1895 $result = [];
1896 $page = $data['page'];
1897 $form_id = absint( $data['dynamic'] );
1898
1899 $form = RGFormsModel::get_form_meta( $form_id );
1900
1901 if ( is_array( $form['fields'] ) ) {
1902 foreach ( $form['fields'] as $field ) {
1903 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
1904 foreach ( $field['inputs'] as $input ) {
1905 if ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
1906 $result[] = [
1907 'value' => $input['id'],
1908 'label' => GFCommon::get_label( $field, $input['id'] ),
1909 ];
1910 }
1911 }
1912 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
1913 $result[] = [
1914 'value' => $field['id'],
1915 'label' => GFCommon::get_label( $field ),
1916 ];
1917 }
1918 }
1919 }
1920
1921 return [
1922 'options' => $result,
1923 'hasMore' => false,
1924 ];
1925
1926 }
1927
1928 /**
1929 * Search Gravity Form fields.
1930 *
1931 * @param array $data Search Params.
1932 *
1933 * @since 1.0.0
1934 */
1935 public function search_pluggable_gravity_form_fields( $data ) {
1936 if ( ! class_exists( 'RGFormsModel' ) ) {
1937 return [
1938 'options' => [],
1939 'hasMore' => false,
1940 ];
1941 }
1942 $result = [];
1943 $form_id = absint( $data['dynamic'] );
1944
1945 $form = RGFormsModel::get_form_meta( $form_id );
1946
1947 if ( is_array( $form['fields'] ) ) {
1948 foreach ( $form['fields'] as $field ) {
1949 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
1950 foreach ( $field['inputs'] as $input ) {
1951 if ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
1952 $result[] = [
1953 'value' => '{' . $input['id'] . '}',
1954 'label' => GFCommon::get_label( $field, $input['id'] ),
1955 ];
1956 }
1957 }
1958 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
1959 $result[] = [
1960 'value' => '{' . $field['id'] . '}',
1961 'label' => GFCommon::get_label( $field ),
1962 ];
1963 }
1964 }
1965 }
1966
1967 $result[] = [
1968 'value' => '{gravity_form}',
1969 'label' => 'Form ID',
1970 ];
1971 $result[] = [
1972 'value' => '{form_title}',
1973 'label' => 'Form Title',
1974 ];
1975 $result[] = [
1976 'value' => '{entry_id}',
1977 'label' => 'Entry ID',
1978 ];
1979 $result[] = [
1980 'value' => '{user_ip}',
1981 'label' => 'User IP',
1982 ];
1983 $result[] = [
1984 'value' => '{entry_source_url}',
1985 'label' => 'Entry Source URL',
1986 ];
1987 $result[] = [
1988 'value' => '{entry_submission_date}',
1989 'label' => 'Entry Submission Date',
1990 ];
1991
1992 return [
1993 'options' => $result,
1994 'hasMore' => false,
1995 ];
1996
1997 }
1998
1999 /**
2000 * Prepare fluentcrm lists.
2001 *
2002 * @param array $data Search Params.
2003 *
2004 * @return array[]
2005 */
2006 public function search_fluentcrm_lists( $data ) {
2007
2008 $list_api = FluentCrmApi( 'lists' );
2009 $all_lists = $list_api->all();
2010 $options = [];
2011
2012 if ( ! empty( $all_lists ) ) {
2013 foreach ( $all_lists as $list ) {
2014 $options[] = [
2015 'label' => $list->title,
2016 'value' => $list->id,
2017 ];
2018 }
2019 }
2020
2021 return [
2022 'options' => $options,
2023 'hasMore' => false,
2024 ];
2025 }
2026
2027 /**
2028 * Prepare fluentcrm contact status.
2029 *
2030 * @param array $data Search Params.
2031 *
2032 * @return array[]
2033 */
2034 public function search_fluentcrm_contact_status( $data ) {
2035
2036 $options = [
2037 [
2038 'label' => __( 'Subscribed', 'suretriggers' ),
2039 'value' => 'subscribed',
2040 ],
2041 [
2042 'label' => __( 'Pending', 'suretriggers' ),
2043 'value' => 'pending',
2044 ],
2045 [
2046 'label' => __( 'Unsubscribed', 'suretriggers' ),
2047 'value' => 'unsubscribed',
2048 ],
2049 [
2050 'label' => __( 'Bounced', 'suretriggers' ),
2051 'value' => 'bounced',
2052 ],
2053 [
2054 'label' => __( 'Complained', 'suretriggers' ),
2055 'value' => 'complained',
2056 ],
2057 ];
2058
2059 return [
2060 'options' => $options,
2061 'hasMore' => false,
2062 ];
2063 }
2064
2065 /**
2066 * Prepare fluentcrm contact status.
2067 *
2068 * @param array $data Search Params.
2069 *
2070 * @return array[]
2071 */
2072 public function search_fluentcrm_fetch_custom_fields( $data ) {
2073
2074 $options = [
2075 [
2076 'label' => __( 'Yes', 'suretriggers' ),
2077 'value' => 'true',
2078 ],
2079 [
2080 'label' => __( 'No', 'suretriggers' ),
2081 'value' => 'false',
2082 ],
2083 ];
2084
2085 return [
2086 'options' => $options,
2087 'hasMore' => false,
2088 ];
2089 }
2090
2091 /**
2092 * Prepare fluentcrm tags.
2093 *
2094 * @param array $data Search Params.
2095 *
2096 * @return array[]
2097 */
2098 public function search_fluentcrm_tags( $data ) {
2099
2100 $tag_api = FluentCrmApi( 'tags' );
2101 $all_tags = $tag_api->all();
2102 $options = [];
2103
2104 if ( ! empty( $all_tags ) ) {
2105 foreach ( $all_tags as $tag ) {
2106 $options[] = [
2107 'label' => $tag->title,
2108 'value' => $tag->id,
2109 ];
2110 }
2111 }
2112
2113 return [
2114 'options' => $options,
2115 'hasMore' => false,
2116 ];
2117 }
2118
2119 /**
2120 * Prepare JetpackCRM Contact tags.
2121 *
2122 * @param array $data Search Params.
2123 *
2124 * @return array
2125 */
2126 public function search_jetpack_crm_contact_tags( $data ) {
2127
2128 if ( ! function_exists( 'zeroBSCRM_getCustomerTags' ) ) {
2129 return [];
2130 }
2131
2132 $all_tags = zeroBSCRM_getCustomerTags();
2133 $options = [];
2134
2135 if ( ! empty( $all_tags ) ) {
2136 foreach ( $all_tags as $tag ) {
2137 $options[] = [
2138 'label' => $tag['name'],
2139 'value' => $tag['id'],
2140 ];
2141 }
2142 }
2143
2144 return [
2145 'options' => $options,
2146 'hasMore' => false,
2147 ];
2148 }
2149
2150 /**
2151 * Prepare JetpackCRM Company tags.
2152 *
2153 * @param array $data Search Params.
2154 *
2155 * @return array
2156 */
2157 public function search_jetpack_crm_company_tags( $data ) {
2158
2159 if ( ! defined( 'ZBS_TYPE_COMPANY' ) ) {
2160 return [];
2161 }
2162
2163 global $wpdb;
2164 $all_tags = $wpdb->get_results( $wpdb->prepare( "SELECT `ID`,`zbstag_name` FROM `{$wpdb->prefix}zbs_tags` WHERE zbstag_objtype = %d", ZBS_TYPE_COMPANY ) );
2165
2166 $options = [];
2167 if ( ! empty( $all_tags ) ) {
2168 foreach ( $all_tags as $tag ) {
2169 $options[] = [
2170 'label' => $tag->zbstag_name,
2171 'value' => $tag->ID,
2172 ];
2173 }
2174 }
2175
2176 return [
2177 'options' => $options,
2178 'hasMore' => false,
2179 ];
2180 }
2181
2182 /**
2183 * Prepare JetpackCRM Companies list.
2184 *
2185 * @param array $data Search Params.
2186 *
2187 * @return array
2188 */
2189 public function search_jetpack_crm_companies_list( $data ) {
2190
2191 if ( ! function_exists( 'zeroBS_getCompanies' ) ) {
2192 return [];
2193 }
2194
2195 $all_companies = zeroBS_getCompanies();
2196 $options = [];
2197
2198 if ( ! empty( $all_companies ) ) {
2199 foreach ( $all_companies as $company ) {
2200 $options[] = [
2201 'label' => $company['name'],
2202 'value' => $company['id'],
2203 ];
2204 }
2205 }
2206
2207 return [
2208 'options' => $options,
2209 'hasMore' => false,
2210 ];
2211 }
2212
2213 /**
2214 * Prepare JetpackCRM contact status.
2215 *
2216 * @param array $data Search Params.
2217 *
2218 * @return array
2219 */
2220 public function search_jetpack_crm_contact_statuses( $data ) {
2221
2222 $options = [
2223 [
2224 'label' => __( 'Lead', 'suretriggers' ),
2225 'value' => 'Lead',
2226 ],
2227 [
2228 'label' => __( 'Customer', 'suretriggers' ),
2229 'value' => 'Customer',
2230 ],
2231 [
2232 'label' => __( 'Refused', 'suretriggers' ),
2233 'value' => 'Refused',
2234 ],
2235 [
2236 'label' => __( 'Blacklisted', 'suretriggers' ),
2237 'value' => 'Blacklisted',
2238 ],
2239 [
2240 'label' => __( 'Cancelled by Customer', 'suretriggers' ),
2241 'value' => 'Cancelled by Customer',
2242 ],
2243 [
2244 'label' => __( 'Cancelled by Us (Pre-Quote)', 'suretriggers' ),
2245 'value' => 'Cancelled by Us (Pre-Quote)',
2246 ],
2247 [
2248 'label' => __( 'Cancelled by Us (Post-Quote)', 'suretriggers' ),
2249 'value' => 'Cancelled by Us (Post-Quote)',
2250 ],
2251 ];
2252
2253 return [
2254 'options' => $options,
2255 'hasMore' => false,
2256 ];
2257 }
2258
2259 /**
2260 * Prepare FunnelKit Automations' lists.
2261 *
2262 * @param array $data Search Params.
2263 *
2264 * @return array
2265 */
2266 public function search_funnel_kit_automations_lists( $data ) {
2267
2268 if ( ! class_exists( 'BWFCRM_Lists' ) ) {
2269 return [];
2270 }
2271
2272 $bwfcrm_lists = \BWFCRM_Lists::get_lists();
2273
2274 $options = [];
2275
2276 foreach ( $bwfcrm_lists as $list ) {
2277 $options[] = [
2278 'label' => $list['name'],
2279 'value' => $list['ID'],
2280 ];
2281 }
2282
2283 return [
2284 'options' => $options,
2285 'hasMore' => false,
2286 ];
2287 }
2288
2289 /**
2290 * Prepare FunnelKit Automations' tags.
2291 *
2292 * @param array $data Search Params.
2293 *
2294 * @return array
2295 */
2296 public function search_funnel_kit_automations_tags( $data ) {
2297
2298 if ( ! class_exists( 'BWFCRM_Tag' ) ) {
2299 return [];
2300 }
2301
2302 $bwfcrm_tags = \BWFCRM_Tag::get_tags();
2303
2304 $options = [];
2305
2306 foreach ( $bwfcrm_tags as $tag ) {
2307 $options[] = [
2308 'label' => $tag['name'],
2309 'value' => $tag['ID'],
2310 ];
2311 }
2312
2313 return [
2314 'options' => $options,
2315 'hasMore' => false,
2316 ];
2317 }
2318
2319 /**
2320 * Prepare Wishlist Memberlists level.
2321 *
2322 * @param array $data Search Params.
2323 *
2324 * @return array[]
2325 */
2326 public function search_wishlistmember_lists( $data ) {
2327
2328 $wlm_levels = wlmapi_get_levels();
2329 $options = [];
2330
2331 if ( ! empty( $wlm_levels ) ) {
2332 foreach ( $wlm_levels['levels']['level'] as $list ) {
2333 $options[] = [
2334 'label' => $list['name'],
2335 'value' => (string) $list['id'],
2336 ];
2337 }
2338 }
2339
2340 return [
2341 'options' => $options,
2342 'hasMore' => false,
2343 ];
2344 }
2345
2346 /**
2347 * Prepare elementor popups.
2348 *
2349 * @param array $data Search Params.
2350 *
2351 * @return array[]
2352 */
2353 public function search_elementor_popups( $data ) {
2354
2355 $posts = get_posts(
2356 [
2357 'post_type' => 'elementor_library',
2358 'orderby' => 'title',
2359 'order' => 'ASC',
2360 'post_status' => 'publish',
2361 'meta_query' => [
2362 [
2363 'key' => '_elementor_template_type',
2364 'value' => 'popup',
2365 'compare' => '=',
2366 ],
2367 ],
2368 ]
2369 );
2370
2371 $options = [];
2372 if ( ! empty( $posts ) ) {
2373 foreach ( $posts as $post ) {
2374 $options[] = [
2375 'label' => $post->post_title,
2376 'value' => $post->ID,
2377 ];
2378 }
2379 }
2380
2381 return [
2382 'options' => $options,
2383 'hasMore' => false,
2384 ];
2385 }
2386
2387 /**
2388 * Prepare givewp forms.
2389 *
2390 * @param array $data Search Params.
2391 *
2392 * @return array[]
2393 */
2394 public function search_givewp_forms( $data ) {
2395
2396 $posts = get_posts(
2397 [
2398 'post_type' => 'give_forms',
2399 'orderby' => 'title',
2400 'order' => 'ASC',
2401 'post_status' => 'publish',
2402 ]
2403 );
2404
2405 $options = [];
2406 if ( ! empty( $posts ) ) {
2407 foreach ( $posts as $post ) {
2408 $options[] = [
2409 'label' => $post->post_title,
2410 'value' => $post->ID,
2411 ];
2412 }
2413 }
2414
2415 return [
2416 'options' => $options,
2417 'hasMore' => false,
2418 ];
2419 }
2420
2421 /**
2422 * Prepare buddyboss group users.
2423 *
2424 * @param array $data Search Params.
2425 *
2426 * @return array[]
2427 */
2428 public function search_bb_group_users( $data ) {
2429 $options = [];
2430
2431 $group_id = $data['dynamic'];
2432 $admins = groups_get_group_admins( $group_id );
2433
2434 if ( ! empty( $admins ) ) {
2435 foreach ( $admins as $admin ) {
2436 $admin_user = get_user_by( 'id', $admin->user_id );
2437 $options[] = [
2438 'label' => $admin_user->display_name,
2439 'value' => $admin_user->ID,
2440 ];
2441 }
2442 }
2443
2444 $members = groups_get_group_members( [ 'group_id' => $group_id ] );
2445
2446 if ( isset( $members['members'] ) && ! empty( $members['members'] ) ) {
2447 foreach ( $members['members'] as $member ) {
2448 $options[] = [
2449 'label' => $member->display_name,
2450 'value' => $member->ID,
2451 ];
2452 }
2453 }
2454 return [
2455 'options' => $options,
2456 'hasMore' => false,
2457 ];
2458 }
2459
2460 /**
2461 * Prepare buddyboss groups.
2462 *
2463 * @param array $data Search Params.
2464 *
2465 * @return array[]
2466 */
2467 public function search_buddyboss_groups( $data ) {
2468 global $wpdb;
2469
2470 $options = [];
2471 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
2472 if ( ! empty( $groups ) ) {
2473 foreach ( $groups as $group ) {
2474 $options[] = [
2475 'label' => $group->name,
2476 'value' => $group->id,
2477 ];
2478 }
2479 }
2480 return [
2481 'options' => $options,
2482 'hasMore' => false,
2483 ];
2484 }
2485
2486 /**
2487 * Prepare buddyboss public groups.
2488 *
2489 * @param array $data Search Params.
2490 *
2491 * @return array[]
2492 */
2493 public function search_buddyboss_public_groups( $data ) {
2494 $options = [];
2495 $groups = groups_get_groups();
2496 if ( isset( $groups['groups'] ) && ! empty( $groups['groups'] ) ) {
2497 foreach ( $groups['groups'] as $group ) {
2498 if ( 'public' === $group->status ) {
2499 $options[] = [
2500 'label' => $group->name,
2501 'value' => $group->id,
2502 ];
2503 }
2504 }
2505 }
2506 return [
2507 'options' => $options,
2508 'hasMore' => false,
2509 ];
2510 }
2511
2512 /**
2513 * Prepare buddyboss profile types list.
2514 *
2515 * @param array $data Search Params.
2516 *
2517 * @return array[]
2518 */
2519 public function search_bb_profile_type_list( $data ) {
2520 $options = [];
2521
2522 if ( function_exists( 'bp_get_active_member_types' ) ) {
2523 $types = bp_get_active_member_types(
2524 [
2525 'fields' => '*',
2526 ]
2527 );
2528 if ( $types ) {
2529 foreach ( $types as $type ) {
2530 $options[] = [
2531 'label' => $type->post_title,
2532 'value' => $type->ID,
2533 ];
2534 }
2535 }
2536 }
2537
2538 /**
2539 *
2540 * Ignore line
2541 *
2542 * @phpstan-ignore-next-line
2543 */
2544 return [
2545 'options' => $options,
2546 'hasMore' => false,
2547 ];
2548 }
2549
2550 /**
2551 * Prepare elementor forms.
2552 *
2553 * @param array $data Search Params.
2554 *
2555 * @return array[]
2556 */
2557 public function search_elementor_forms( $data ) {
2558
2559 $elementor_forms = Utilities::get_elementor_forms();
2560
2561 $options = [];
2562 if ( ! empty( $elementor_forms ) ) {
2563 foreach ( $elementor_forms as $key => $value ) {
2564 $options[] = [
2565 'label' => $value,
2566 'value' => $key,
2567 ];
2568 }
2569 }
2570
2571 return [
2572 'options' => $options,
2573 'hasMore' => false,
2574 ];
2575 }
2576
2577 /**
2578 * Prepare elementor forms.
2579 *
2580 * @param array $data Search Params.
2581 *
2582 * @return array[]
2583 */
2584 public function search_new_elementor_forms( $data ) {
2585
2586 global $wpdb;
2587 $elementor_forms = [];
2588 $post_metas = $wpdb->get_results(
2589 $wpdb->prepare(
2590 "SELECT pm.post_id, pm.meta_value
2591 FROM $wpdb->postmeta pm
2592 LEFT JOIN $wpdb->posts p
2593 ON p.ID = pm.post_id
2594 WHERE p.post_type IS NOT NULL
2595 AND p.post_status = %s
2596 AND pm.meta_key = %s
2597 AND pm.`meta_value` LIKE %s",
2598 'publish',
2599 '_elementor_data',
2600 '%%form_fields%%'
2601 )
2602 );
2603
2604 if ( ! empty( $post_metas ) ) {
2605 foreach ( $post_metas as $post_meta ) {
2606 /**
2607 *
2608 * Ignore line
2609 *
2610 * @phpstan-ignore-next-line
2611 */
2612 $inner_forms = Utilities::search_elementor_forms( json_decode( $post_meta->meta_value ) );
2613 if ( ! empty( $inner_forms ) ) {
2614 foreach ( $inner_forms as $form ) {
2615 /**
2616 *
2617 * Ignore line
2618 *
2619 * @phpstan-ignore-next-line
2620 */
2621 $elementor_forms[ $post_meta->post_id . '_' . $form->id ] = $form->settings->form_name . ' (' . $form->id . ')';
2622 }
2623 }
2624 }
2625 }
2626
2627 $options = [];
2628 if ( ! empty( $elementor_forms ) ) {
2629 foreach ( $elementor_forms as $key => $value ) {
2630 $options[] = [
2631 'label' => $value,
2632 'value' => $key,
2633 ];
2634 }
2635 }
2636
2637 /**
2638 *
2639 * Ignore line
2640 *
2641 * @phpstan-ignore-next-line
2642 */
2643 return [
2644 'options' => $options,
2645 'hasMore' => false,
2646 ];
2647 }
2648
2649 /**
2650 * Prepare elementor form fields.
2651 *
2652 * @param array $data Search Params.
2653 *
2654 * @return array[]
2655 */
2656 public function search_pluggable_elementor_form_fields( $data ) {
2657 $result = [];
2658 $form_id = absint( $data['dynamic'] );
2659 $elementor_form_fields = ( new Utilities() )->get_elementor_form_fields( $data );
2660 $options = [];
2661 if ( ! empty( $elementor_form_fields ) ) {
2662 foreach ( $elementor_form_fields as $key => $value ) {
2663 $options[] = [
2664 'label' => $value,
2665 'value' => '{' . $key . '}',
2666 ];
2667 }
2668 }
2669
2670 return [
2671 'options' => $options,
2672 'hasMore' => false,
2673 ];
2674 }
2675
2676 /**
2677 * Get all events
2678 *
2679 * @param array $data Data array.
2680 *
2681 * @return array
2682 */
2683 public function search_event_calendar_event( $data ) {
2684 $page = $data['page'];
2685 $limit = Utilities::get_search_page_limit();
2686 $offset = $limit * ( $page - 1 );
2687
2688 $posts = get_posts(
2689 [
2690 'post_type' => 'tribe_events',
2691 'orderby' => 'title',
2692 'order' => 'ASC',
2693 'post_status' => 'publish',
2694 'posts_per_page' => $limit,
2695 'offset' => $offset,
2696 ]
2697 );
2698
2699 $options = [];
2700 if ( ! empty( $posts ) ) {
2701 foreach ( $posts as $post ) {
2702 $options[] = [
2703 'label' => $post->post_title,
2704 'value' => $post->ID,
2705 ];
2706 }
2707 }
2708
2709 $count = count( $options );
2710
2711 return [
2712 'options' => $options,
2713 'hasMore' => $count > $limit && $count > $offset,
2714 ];
2715 }
2716
2717 /**
2718 * Prepare rsvp event calendar events.
2719 *
2720 * @param array $data Search Params.
2721 *
2722 * @return array[]
2723 */
2724 public function search_event_calendar_rsvp_event( $data ) {
2725
2726 $posts = get_posts(
2727 [
2728 'post_type' => 'tribe_events',
2729 'orderby' => 'title',
2730 'order' => 'ASC',
2731 'post_status' => 'publish',
2732 ]
2733 );
2734
2735 $options = [];
2736 if ( ! empty( $posts ) ) {
2737 $ticket_handler = new Tribe__Tickets__Tickets_Handler();
2738 foreach ( $posts as $post ) {
2739
2740 $get_rsvp_ticket = $ticket_handler->get_event_rsvp_tickets( $post->ID );
2741
2742 if ( ! empty( $get_rsvp_ticket ) ) {
2743 $options[] = [
2744 'label' => $post->post_title,
2745 'value' => $post->ID,
2746 ];
2747 }
2748 }
2749 }
2750
2751 return [
2752 'options' => $options,
2753 'hasMore' => false,
2754 ];
2755 }
2756
2757 /**
2758 * Prepare Restrict Content Membership Level.
2759 *
2760 * @param array $data Search Params.
2761 *
2762 * @return array[]
2763 */
2764 public function search_restrictcontent_membership_level( $data ) {
2765
2766 $rcp_memberships = rcp_get_membership_levels();
2767 $options = [];
2768
2769 if ( ! empty( $rcp_memberships ) ) {
2770 foreach ( $rcp_memberships as $list ) {
2771 $options[] = [
2772 'label' => ucfirst( $list->name ),
2773 'value' => $list->id,
2774 ];
2775 }
2776 }
2777
2778 return [
2779 'options' => $options,
2780 'hasMore' => false,
2781 ];
2782 }
2783
2784 /**
2785 * Prepare Restrict Content Customer.
2786 *
2787 * @param array $data Search Params.
2788 *
2789 * @return array[]
2790 */
2791 public function search_restrictcontent_customer( $data ) {
2792
2793 $rcp_users = rcp_get_memberships();
2794 $options = [];
2795
2796 if ( ! empty( $rcp_users ) ) {
2797 foreach ( $rcp_users as $list ) {
2798 $user = get_user_by( 'ID', $list->get_user_id() );
2799 $user_label = $user->user_email;
2800
2801 if ( $user->display_name !== $user->user_email ) {
2802 $user_label .= ' (' . $user->display_name . ')';
2803 }
2804
2805 $options[] = [
2806 'label' => $user_label,
2807 'value' => $list->get_customer_id(),
2808 ];
2809 }
2810 }
2811
2812 return [
2813 'options' => $options,
2814 'hasMore' => false,
2815 ];
2816 }
2817
2818
2819 /**
2820 * Fetch the Presto Player video List.
2821 *
2822 * @param array $data Search Params.
2823 *
2824 * @return array[]
2825 */
2826 public function search_ap_presto_player_video_list( $data ) {
2827
2828 $videos = ( new Video() )->all();
2829 $options = [];
2830 if ( ! empty( $videos ) ) {
2831 foreach ( $videos as $video ) {
2832 $options[] = [
2833 'label' => $video->__get( 'title' ),
2834 'value' => (string) $video->__get( 'id' ),
2835 ];
2836 }
2837 }
2838
2839 return [
2840 'options' => $options,
2841 'hasMore' => false,
2842 ];
2843 }
2844
2845 /**
2846 * Presto Player Video percentage.
2847 *
2848 * @param array $data Search Params.
2849 *
2850 * @return array[]
2851 */
2852 public function search_prestoplayer_video_percent( $data ) {
2853
2854 $percents = [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ];
2855 $options = [];
2856
2857 foreach ( $percents as $percent ) {
2858 $options[] = [
2859 'label' => $percent . '%',
2860 'value' => (string) $percent,
2861 ];
2862 }
2863
2864 return [
2865 'options' => $options,
2866 'hasMore' => false,
2867 ];
2868 }
2869
2870 /**
2871 * Get user profile field options.
2872 *
2873 * @return array
2874 * @since 1.0.0
2875 */
2876 public function search_user_field_options() {
2877
2878 $options = apply_filters(
2879 'sure_trigger_get_user_field_options',
2880 [
2881 [
2882 'label' => __( 'User Name', 'suretriggers' ),
2883 'value' => 'user_login',
2884 ],
2885 [
2886 'label' => __( 'User Email', 'suretriggers' ),
2887 'value' => 'user_email',
2888 ],
2889 [
2890 'label' => __( 'Display Name', 'suretriggers' ),
2891 'value' => 'display_name',
2892 ],
2893 [
2894 'label' => __( 'User Password', 'suretriggers' ),
2895 'value' => 'user_pass',
2896 ],
2897 [
2898 'label' => __( 'Website', 'suretriggers' ),
2899 'value' => 'user_url',
2900 ],
2901 ]
2902 );
2903
2904 return [
2905 'options' => $options,
2906 'hasMore' => false,
2907 ];
2908 }
2909
2910 /**
2911 * Get user post field options.
2912 *
2913 * @return array
2914 * @since 1.0.0
2915 */
2916 public function search_post_field_options() {
2917
2918 return [
2919 'options' => [
2920 [
2921 'label' => __( 'Type', 'suretriggers' ),
2922 'value' => 'post_type',
2923 'dynamic_field' => [
2924 'type' => 'select-creatable',
2925 'ajaxIdentifier' => 'post_types',
2926 ],
2927 ],
2928 [
2929 'label' => __( 'Status', 'suretriggers' ),
2930 'value' => 'post_status',
2931 'dynamic_field' => [
2932 'type' => 'select-async',
2933 'ajaxIdentifier' => 'post_statuses',
2934 ],
2935 ],
2936 [
2937 'label' => __( 'Author', 'suretriggers' ),
2938 'value' => 'post_author',
2939 'dynamic_field' => [
2940 'type' => 'select-async',
2941 'ajaxIdentifier' => 'user',
2942 ],
2943 ],
2944 [
2945 'label' => __( 'Title', 'suretriggers' ),
2946 'value' => 'post_title',
2947 'dynamic_field' => [
2948 'type' => 'select-creatable',
2949 ],
2950 ],
2951 [
2952 'label' => __( 'Slug', 'suretriggers' ),
2953 'value' => 'post_slug',
2954 'dynamic_field' => [
2955 'type' => 'select-creatable',
2956 ],
2957 ],
2958 [
2959 'label' => __( 'Content', 'suretriggers' ),
2960 'value' => 'post_content',
2961 'dynamic_field' => [
2962 'type' => 'html-editor',
2963 ],
2964 ],
2965 ],
2966 'hasMore' => false,
2967 ];
2968 }
2969
2970 /**
2971 * Bricksbuilder grouped data.
2972 *
2973 * @param array $data data.
2974 * @return array
2975 */
2976 public function search_bb_groups( $data ) {
2977
2978 global $wpdb;
2979 $options = [];
2980
2981 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'bp_groups' ) ) ) {
2982
2983 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %s', $wpdb->prefix . 'bp_groups' ) );
2984
2985 if ( $results ) {
2986 foreach ( $results as $result ) {
2987 $options[] = [
2988 'label' => $result->name,
2989 'value' => $result->id,
2990 ];
2991 }
2992 }
2993 }
2994
2995 return [
2996 'options' => $options,
2997 'hasMore' => false,
2998 ];
2999 }
3000
3001 /**
3002 * Search forms.
3003 *
3004 * @return array
3005 */
3006 public function search_bb_forums() {
3007 $options = [];
3008 $allowed_atatus = [ 'publish', 'private' ];
3009 $forum_args = [
3010 'post_type' => bbp_get_forum_post_type(),
3011 'posts_per_page' => -1,
3012 'orderby' => 'title',
3013 'order' => 'ASC',
3014 'post_status' => 'any',
3015 ];
3016 $forums = get_posts( $forum_args );
3017
3018 if ( ! empty( $forums ) ) {
3019 foreach ( $forums as $forum ) {
3020 if ( in_array( $forum->post_status, $allowed_atatus, true ) ) {
3021 $options[] = [
3022 'label' => $forum->post_title,
3023 'value' => $forum->ID,
3024 ];
3025 }
3026 }
3027 }
3028 return [
3029 'options' => $options,
3030 'hasMore' => false,
3031 ];
3032 }
3033
3034 /**
3035 * Get last data for trigger.
3036 *
3037 * @param array $data data.
3038 * @return array
3039 */
3040 public function search_affiliate_wp_triggers_last_data( $data ) {
3041 global $wpdb;
3042
3043 $context = [];
3044 $context['response_type'] = 'sample';
3045
3046 $user_data = WordPress::get_sample_user_context();
3047
3048 $affiliate_data = [
3049 'affiliate_id' => 1,
3050 'rest_id' => '',
3051 'user_id' => 1,
3052 'rate' => '',
3053 'rate_type' => '',
3054 'flat_rate_basis' => '',
3055 'payment_email' => 'admin@bsf.io',
3056 'status' => 'active',
3057 'earnings' => 0,
3058 'unpaid_earnings' => 0,
3059 'referrals' => 0,
3060 'visits' => 0,
3061 'date_registered' => '2023-01-18 13:35:22',
3062 'dynamic_coupon' => 'KDJSKS',
3063 ];
3064
3065 $referral_data = [
3066 'referral_id' => 1,
3067 'affiliate_id' => 1,
3068 'visit_id' => 0,
3069 'rest_id' => '',
3070 'customer_id' => 0,
3071 'parent_id' => 0,
3072 'description' => 'Testing',
3073 'status' => 'unpaid',
3074 'amount' => '12.00',
3075 'currency' => '',
3076 'custom' => 'custom',
3077 'context' => '',
3078 'campaign' => '',
3079 'reference' => 'Reference',
3080 'products' => '',
3081 'date' => '2023-01-18 16:36:59',
3082 'type' => 'opt-in',
3083 'payout_id' => 0,
3084 ];
3085
3086 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
3087
3088 if ( in_array( $term, [ 'affiliate_approved', 'affiliate_awaiting_approval' ], true ) ) {
3089 $status = 'affiliate_approved' === $term ? 'active' : 'pending';
3090 $affiliate = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_affiliates WHERE affiliate_id = ( SELECT max(affiliate_id) FROM {$wpdb->prefix}affiliate_wp_affiliates )" );
3091
3092 if ( ! empty( $affiliate ) ) {
3093 $affiliate = affwp_get_affiliate( $affiliate->affiliate_id );
3094 $affiliate_data = get_object_vars( $affiliate );
3095 $user_data = WordPress::get_user_context( $affiliate->user_id );
3096 $context['response_type'] = 'live';
3097 }
3098 $affiliate_data['status'] = $status;
3099 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data );
3100
3101 } else {
3102 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE referral_id = ( SELECT max(referral_id) FROM {$wpdb->prefix}affiliate_wp_referrals )" );
3103
3104 if ( ! empty( $referral ) ) {
3105 $referral = affwp_get_referral( $referral->referral_id );
3106 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
3107 $affiliate_data = get_object_vars( $affiliate );
3108 $user_data = WordPress::get_user_context( $affiliate->user_id );
3109 $referral_data = get_object_vars( $referral );
3110 $context['response_type'] = 'live';
3111 }
3112 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
3113 }
3114
3115 return $context;
3116 }
3117
3118 /**
3119 * Get last data for trigger.
3120 *
3121 * @param array $data data.
3122 * @return array
3123 */
3124 public function search_jetpack_crm_triggers_last_data( $data ) {
3125
3126 if ( ! function_exists( 'zeroBS_getCompanyCount' ) || ! function_exists( 'zeroBS_getCustomerCount' ) || ! function_exists( 'zeroBS_getQuoteCount' ) ) {
3127 return [];
3128 }
3129
3130 global $wpdb;
3131
3132 $context = [];
3133 $context['response_type'] = 'sample';
3134
3135 $company_id = [ 'company_id' => 1 ];
3136 $contact_id = [ 'contact_id' => 1 ];
3137 $quote_id = [ 'quote_id' => 1 ];
3138 $event_id = [ 'event_id' => 1 ];
3139 $invoice_id = [ 'invoice_id' => 1 ];
3140 $transaction_id = [ 'transaction_id' => 1 ];
3141
3142 $company = [
3143 'company_id' => 1,
3144 'company_status' => 'Lead',
3145 'company_name' => 'Example Company',
3146 'company_email' => 'info@example.com',
3147 'main_address_line_1' => '123 Main Street',
3148 'main_address_line_2' => 'Suite 456',
3149 'main_address_city' => 'New York',
3150 'main_address_state' => 'NY',
3151 'main_address_postal_code' => '10001',
3152 'main_address_country' => 'United States',
3153 'second_address_line_1' => '789 Second Avenue',
3154 'second_address_line_2' => 'Floor 10',
3155 'second_address_city' => 'Los Angeles',
3156 'second_address_state' => 'CA',
3157 'second_address_postal_code' => '90001',
3158 'second_address_country' => 'United States',
3159 'main_telephone' => '+1 123-456-7890',
3160 'secondary_telephone' => '+1 987-654-3210',
3161 ];
3162
3163 $contact = [
3164 'contact_id' => 1,
3165 'status' => 'Lead',
3166 'prefix' => 'Mr.',
3167 'full_name' => 'John Doe',
3168 'first_name' => 'John',
3169 'last_name' => 'Doe',
3170 'email' => 'johndoe@example.com',
3171 'main_address_line_1' => '123 Main Street',
3172 'main_address_line_2' => 'Suite 456',
3173 'main_address_city' => 'New York',
3174 'main_address_state' => 'NY',
3175 'main_address_postal_code' => '10001',
3176 'main_address_country' => 'United States',
3177 'second_address_line_1' => '789 Second Avenue',
3178 'second_address_line_2' => 'Floor 10',
3179 'second_address_city' => 'Los Angeles',
3180 'second_address_state' => 'CA',
3181 'second_address_postal_code' => '90001',
3182 'second_address_country' => 'United States',
3183 'home_telephone' => '+1 123-456-7890',
3184 'work_telephone' => '+1 987-654-3210',
3185 'mobile_telephone' => '+1 555-555-5555',
3186 ];
3187
3188 $quote = [
3189 'quote_id' => 1,
3190 'contact_id' => 2,
3191 'contact_email' => 'john@example.com',
3192 'contact_name' => 'John Doe',
3193 'status' => '<strong>Created, not yet accepted</strong>',
3194 'title' => 'Sample Quote',
3195 'value' => 1000,
3196 'date' => '2023-05-23',
3197 'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
3198 'notes' => 'Additional notes about the quote.',
3199 ];
3200
3201 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
3202
3203 switch ( $term ) {
3204 case 'company_created':
3205 if ( zeroBS_getCompanyCount() > 0 ) {
3206 $company_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_companies WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_companies )" );
3207 $company = JetpackCRM::get_company_context( $company_data->ID );
3208 $context['response_type'] = 'live';
3209 }
3210 $context['pluggable_data'] = $company;
3211 break;
3212 case 'company_deleted':
3213 $context['pluggable_data'] = $company_id;
3214 break;
3215 case 'contact_created':
3216 if ( zeroBS_getCustomerCount() > 0 ) {
3217 $contact_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_contacts WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_contacts )" );
3218 $contact = JetpackCRM::get_contact_context( $contact_data->ID );
3219 $context['response_type'] = 'live';
3220 }
3221 $context['pluggable_data'] = $contact;
3222 break;
3223 case 'contact_deleted':
3224 $context['pluggable_data'] = $contact_id;
3225 break;
3226 case 'event_deleted':
3227 $context['pluggable_data'] = $event_id;
3228 break;
3229 case 'invoice_deleted':
3230 $context['pluggable_data'] = $invoice_id;
3231 break;
3232 case 'quote_accepted':
3233 case 'quote_created':
3234 if ( zeroBS_getQuoteCount() > 0 ) {
3235 $quote_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_quotes WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_quotes )" );
3236 $quote = JetpackCRM::get_quote_context( $quote_data->ID );
3237 $context['response_type'] = 'live';
3238 }
3239 $context['pluggable_data'] = $quote;
3240 break;
3241 case 'quote_deleted':
3242 $context['pluggable_data'] = $quote_id;
3243 break;
3244 case 'transaction_deleted':
3245 $context['pluggable_data'] = $transaction_id;
3246 break;
3247 }
3248
3249 return $context;
3250 }
3251
3252 /**
3253 * Get last data for trigger.
3254 *
3255 * @param array $data data.
3256 * @return array
3257 */
3258 public function search_funnel_kit_automations_triggers_last_data( $data ) {
3259
3260 if ( ! class_exists( 'BWFCRM_Contact' ) || ! class_exists( 'BWFCRM_Lists' ) || ! class_exists( 'BWFCRM_Tag' ) ) {
3261 return [];
3262 }
3263
3264 $context = [];
3265 $context['response_type'] = 'sample';
3266
3267 $contact = [
3268 'contact_id' => '1',
3269 'wpid' => '0',
3270 'uid' => '9e74246335fd81b1c4a9123842c12549',
3271 'email' => 'johndoe@example.com',
3272 'first_name' => 'John',
3273 'last_name' => 'Doe',
3274 'contact_no' => '+1 555-555-5555',
3275 'state' => 'NY',
3276 'country' => 'United States',
3277 'timezone' => 'New York',
3278 'creation_date' => '2023-05-29 15:26:03',
3279 'last_modified' => '2023-05-29 17:08:30',
3280 'source' => '',
3281 'type' => 'Los Angeles',
3282 'status' => '0',
3283 'tags' => '["1"]',
3284 'lists' => '["2","1"]',
3285 ];
3286
3287 $list = [
3288 'list_id' => 1,
3289 'list_name' => 'Sample List',
3290 ];
3291
3292 $tag = [
3293 'tag_id' => 1,
3294 'tag_name' => 'Sample Tag',
3295 ];
3296
3297 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
3298
3299 $recent_contacts = \BWFCRM_Contact::get_recent_contacts( 0, 1, [] );
3300 $contact_email = count( $recent_contacts['contacts'] ) > 0 && isset( $recent_contacts['contacts'][0]['email'] ) ? $recent_contacts['contacts'][0]['email'] : '';
3301
3302 $real_contact = false;
3303 if ( ! empty( $contact_email ) ) {
3304 $contact_obj = \BWFCRM_Contact::get_contacts( $contact_email, 0, 1, [], [], OBJECT );
3305
3306 if ( isset( $contact_obj['contacts'][0] ) ) {
3307 $contact = FunnelKitAutomations::get_contact_context( $contact_obj['contacts'][0]->contact );
3308 $real_contact = true;
3309 }
3310 }
3311
3312 if ( 'contact_added_to_list' === $term || 'contact_removed_from_list' === $term ) {
3313 $list_id = (int) ( isset( $data['filter']['list_id']['value'] ) ? $data['filter']['list_id']['value'] : '-1' );
3314
3315 if ( -1 === $list_id ) {
3316 $lists = \BWFCRM_Lists::get_lists( [], '', 0, 1 );
3317 $list_id = count( $lists ) > 0 ? $lists[0]['ID'] : -1;
3318 }
3319
3320
3321 if ( -1 !== $list_id ) {
3322 $list = FunnelKitAutomations::get_list_context( $list_id );
3323 $context['response_type'] = $real_contact ? 'live' : 'sample';
3324 }
3325
3326 $context['pluggable_data'] = array_merge( $list, $contact );
3327 } else {
3328 $tag_id = (int) ( isset( $data['filter']['tag_id']['value'] ) ? $data['filter']['tag_id']['value'] : '-1' );
3329
3330 if ( -1 === $tag_id ) {
3331 $tags = \BWFCRM_Tag::get_tags( [], '', 0, 1 );
3332 $tag_id = count( $tags ) > 0 ? $tags[0]['ID'] : -1;
3333 }
3334
3335
3336 if ( -1 !== $tag_id ) {
3337 $tag = FunnelKitAutomations::get_tag_context( $tag_id );
3338 $context['response_type'] = $real_contact ? 'live' : 'sample';
3339 }
3340
3341 $context['pluggable_data'] = array_merge( $tag, $contact );
3342 }
3343
3344 return $context;
3345 }
3346
3347 /**
3348 * Get last data for trigger.
3349 *
3350 * @param array $data data.
3351 * @return array
3352 */
3353 public function search_edd_triggers_last_data( $data ) {
3354 global $wpdb;
3355 $context = [];
3356 $context['response_type'] = 'sample';
3357 $context['pluggable_data'] = [];
3358 $order_data = [
3359 'order_id' => 187,
3360 'customer_email' => 'john_doe@bsf.io',
3361 'customer_id' => 2,
3362 'user_id' => 1,
3363 'customer_first_name' => 'Sure',
3364 'customer_last_name' => 'Dev',
3365 'ordered_items' => 'Price with license — Price one',
3366 'currency' => 'USD',
3367 'status' => 'complete',
3368 'discount_codes' => '',
3369 'order_discounts' => 0.00,
3370 'order_subtotal' => 12.00,
3371 'order_tax' => 0.00,
3372 'order_total' => 12.00,
3373 'payment_method' => 'manual',
3374 'purchase_key' => 'd797b9576a3895e7424bae2417ed87df',
3375 'ordered_items_ids' => 17250,
3376 'download_id' => 17250,
3377 'license_key' => 'f7736093411cfaed18b56ec60227117b',
3378 'license_key_expire_date' => '1697524076',
3379 'license_key_status' => 'inactive',
3380 ];
3381
3382 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
3383 $download_id = isset( $data['filter']['download_id']['value'] ) ? $data['filter']['download_id']['value'] : 0;
3384 if ( 'order_created' === $term || 'order_one_product' === $term ) {
3385 $order_data['purchase_key'] = '06d3b7d923ca922dc889354f9bc33fbb';
3386
3387 $args = [
3388 'number' => 1,
3389 'status' => [ 'complete', 'refunded', 'partially_refunded', 'renewal' ],
3390 ];
3391 if ( $download_id > 0 ) {
3392 $args['download'] = $download_id;
3393 }
3394 $payments = edd_get_payments( $args );
3395 if ( count( $payments ) > 0 ) {
3396 $order_data = EDD::get_product_purchase_context( $payments[0], $term, $download_id );
3397
3398 $context['response_type'] = 'live';
3399 } else {
3400 if ( 'order_one_product' === $term ) {
3401 $order_data['price_id'] = 1;
3402 }
3403 }
3404 } elseif ( 'stripe_payment_refunded' === $term ) {
3405 $args = [
3406 'number' => 1,
3407 'status' => 'complete',
3408 'type' => 'refund',
3409 ];
3410 $payments = edd_get_payments( $args );
3411
3412 if ( count( $payments ) > 0 ) {
3413 $order_data = EDD::get_purchase_refund_context( $payments[0] );
3414 $context['response_type'] = 'live';
3415 }
3416 } else {
3417 $status = isset( $data['post_type'] ) ? $data['post_type'] : '';
3418 if ( ! empty( $status ) ) {
3419 if ( $download_id > 0 ) {
3420 $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 ) );
3421 } else {
3422 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE status= %s ORDER BY id DESC", $status ) );
3423 }
3424 } else {
3425 if ( $download_id > 0 ) {
3426 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE download_id=%d ORDER BY id DESC", $download_id ) );
3427 } else {
3428 $licesnses = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}edd_licenses ORDER BY id DESC" );
3429 }
3430 }
3431 if ( ! empty( $licesnses ) ) {
3432 $order_data = EDD::edd_get_license_data( $licesnses->id, $licesnses->download_id, $licesnses->payment_id );
3433 $context['response_type'] = 'live';
3434 } else {
3435 $order_data = [
3436 'ID' => 1,
3437 'key' => '23232323232',
3438 'customer_email' => 'suretest@example.com',
3439 'customer_name' => 'Sure Test',
3440 'product_id' => 1,
3441 'download_id' => 1,
3442 'product_name' => 'Test',
3443 'activation_limit' => 2,
3444 'activation_count' => 1,
3445 'activated_urls' => 'https://example.com',
3446 'expiration' => '1686297914',
3447 'is_lifetime' => '0',
3448 'status' => $status,
3449 ];
3450
3451 }
3452 }
3453
3454 $context['pluggable_data'] = $order_data;
3455 return $context;
3456 }
3457
3458 /**
3459 * Get last data for trigger.
3460 *
3461 * @param array $data data.
3462 * @return array
3463 */
3464 public function search_presto_player_triggers_last_data( $data ) {
3465 $context = [];
3466 $context['response_type'] = 'sample';
3467
3468 $user_data = WordPress::get_sample_user_context();
3469
3470 $video_data = [
3471 'pp_video' => '1',
3472 'pp_video_percentage' => '100',
3473 'video_id' => '1',
3474 'video_title' => 'SureTriggers Is Here 🎉 The Easiest Automation Builder WordPress Websites & Apps',
3475 'video_type' => 'youtube',
3476 'video_external_id' => '-cYbNYgylLs',
3477 'video_attachment_id' => '0',
3478 'video_post_id' => '127',
3479 'video_src' => 'https://www.youtube.com/watch?v=-cYbNYgylLs',
3480 'video_created_by' => '1',
3481 'video_created_at' => '2022-11-10 00:28:25',
3482 'video_updated_at' => '2022-11-10 00:34:40',
3483 'video_deleted_at' => '',
3484 ];
3485
3486 $videos = ( new Video() )->all();
3487
3488 if ( count( $videos ) > 0 ) {
3489 $video_id = '-1' === $data['filter']['pp_video']['value'] ? $videos[0]->id : $data['filter']['pp_video']['value'];
3490 $video_data = ( new Video( $video_id ) )->toArray();
3491 $video_data['pp_video'] = $video_id;
3492 $video_data['pp_video_percentage'] = isset( $data['filter']['pp_video_percentage']['value'] ) ? $data['filter']['pp_video_percentage']['value'] : '100';
3493 $user_data = WordPress::get_user_context( $video_data['created_by'] );
3494
3495 $context['response_type'] = 'live';
3496 }
3497
3498 $context['pluggable_data'] = array_merge( $user_data, $video_data );
3499
3500 return $context;
3501 }
3502
3503 /**
3504 * Get last data for trigger.
3505 *
3506 * @param array $data data.
3507 * @return array
3508 */
3509 public function search_member_press_triggers_last_data( $data ) {
3510 global $wpdb;
3511
3512 $context = [];
3513 $context['response_type'] = 'sample';
3514
3515 $user_data = WordPress::get_sample_user_context();
3516
3517 $membership_data = [
3518 'membership_id' => '190',
3519 'membership_title' => 'Sample Membership',
3520 'amount' => '12.00',
3521 'total' => '12.00',
3522 'tax_amount' => '0.00',
3523 'tax_rate' => '0.00',
3524 'trans_num' => 't_63a03f3334f44',
3525 'status' => 'complete',
3526 'subscription_id' => '0',
3527 'membership_url' => site_url() . '/register/premium/',
3528 'membership_featured_image_id' => '521',
3529 'membership_featured_image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
3530 ];
3531
3532 $membership_id = (int) ( isset( $data['filter']['membership_id']['value'] ) ? $data['filter']['membership_id']['value'] : '-1' );
3533
3534 if ( $membership_id > 0 ) {
3535
3536 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE product_id= %s ORDER BY id DESC LIMIT 1", $membership_id ) );
3537 } else {
3538 $subscription = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mepr_transactions ORDER BY id DESC LIMIT 1" );
3539 }
3540
3541 if ( ! empty( $subscription ) ) {
3542 $membership_data = MemberPress::get_membership_context( $subscription );
3543 $user_data = WordPress::get_user_context( $subscription->user_id );
3544
3545 $context['response_type'] = 'live';
3546 }
3547
3548 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
3549
3550 return $context;
3551 }
3552
3553 /**
3554 * Get last data for trigger.
3555 *
3556 * @param array $data data.
3557 * @return array
3558 */
3559 public function search_wishlist_member_triggers_last_data( $data ) {
3560 global $wpdb;
3561
3562 $context = [];
3563 $context['response_type'] = 'sample';
3564
3565 $user_data = WordPress::get_sample_user_context();
3566
3567 $membership_data = [
3568 'membership_level_id' => '1',
3569 'membership_level_name' => 'Sample Membership Level',
3570 ];
3571
3572 $membership_level_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
3573
3574 if ( $membership_level_id > 0 ) {
3575 $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 ) );
3576 } else {
3577 $membership = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}wlm_userlevels ORDER BY id DESC LIMIT 1" );
3578 }
3579 if ( ! empty( $membership ) ) {
3580 $membership_data = WishlistMember::get_membership_detail_context( (int) $membership->level_id );
3581 $user_data = WordPress::get_user_context( $membership->user_id );
3582
3583 $context['response_type'] = 'live';
3584 }
3585
3586 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
3587
3588 return $context;
3589 }
3590
3591 /**
3592 * Get last data for trigger.
3593 *
3594 * @param array $data data.
3595 * @return array
3596 */
3597 public function search_peepso_triggers_last_data( $data ) {
3598 global $wpdb;
3599
3600 $context = [];
3601 $context['response_type'] = 'sample';
3602
3603 $user_data = WordPress::get_sample_user_context();
3604
3605 $post_data = [
3606 'post_id' => '1',
3607 'activity_id' => '2',
3608 'post_author' => '1',
3609 'post_content' => 'New sample post...!',
3610 'post_title' => 'Sample Post',
3611 'post_excerpt' => 'sample',
3612 'post_status' => 'publish',
3613 'post_type' => 'peepso-post',
3614 ];
3615
3616 $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" );
3617
3618 if ( ! empty( $post ) ) {
3619 $post_data = PeepSo::get_pp_activity_context( (int) $post->act_external_id, (int) $post->act_id );
3620 $user_data = WordPress::get_user_context( $post->act_owner_id );
3621
3622 $context['response_type'] = 'live';
3623 }
3624
3625 $context['pluggable_data'] = array_merge( $user_data, $post_data );
3626
3627 return $context;
3628 }
3629
3630 /**
3631 * Get last data for trigger
3632 *
3633 * @param array $data data.
3634 * @return array
3635 */
3636 public function search_restrict_content_pro_triggers_last_data( $data ) {
3637 $context = [];
3638 $context['response_type'] = 'sample';
3639
3640 $user_data = WordPress::get_sample_user_context();
3641
3642 $membership_data = [
3643 'membership_level_id' => '190',
3644 'membership_level' => 'Sample Membership',
3645 'membership_initial_payment' => '0.00',
3646 'membership_recurring_payment' => '0.00',
3647 'membership_expiry_date' => 'January 22, 2023',
3648 ];
3649
3650 $customer_id = (int) ( isset( $data['filter']['membership_customer_id']['value'] ) ? $data['filter']['membership_customer_id']['value'] : '-1' );
3651 $membership_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
3652
3653 $args = [
3654 'status' => 'expired',
3655 'number' => 1,
3656 'orderby' => 'id',
3657 ];
3658
3659 if ( -1 !== $customer_id ) {
3660 $args['customer_id'] = $customer_id;
3661 }
3662
3663 if ( -1 !== $membership_id ) {
3664 $args['object_id'] = $membership_id;
3665 }
3666
3667 $memberships = rcp_get_memberships( $args );
3668 if ( count( $memberships ) > 0 ) {
3669 $membership_data = RestrictContent::get_rcp_membership_detail_context( $memberships[0] );
3670 $user_data = WordPress::get_user_context( $memberships[0]->get_user_id() );
3671
3672 $context['response_type'] = 'live';
3673 }
3674
3675 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
3676
3677 return $context;
3678 }
3679
3680 /**
3681 * Get last data for trigger
3682 *
3683 * @param array $data data.
3684 * @return array
3685 */
3686 public function search_events_calendar_triggers_last_data( $data ) {
3687 $context = [];
3688 $context['response_type'] = 'sample';
3689
3690 $event_data = [
3691 'event' => [
3692 'ID' => 58,
3693 'post_author' => 1,
3694 'post_date' => '2023-01-19 09:27:58',
3695 'post_date_gmt' => '2023-01-19 09:27:58',
3696 'post_content' => '',
3697 'post_title' => 'New event',
3698 'post_excerpt' => '',
3699 'post_status' => 'publish',
3700 'comment_status' => 'open',
3701 'ping_status' => 'closed',
3702 'post_password' => '',
3703 'post_name' => 'new-event',
3704 'to_ping' => '',
3705 'pinged' => '',
3706 'post_modified' => '2023-01-19 09:44:25',
3707 'post_modified_gmt' => '2023-01-19 09:44:25',
3708 'post_content_filtered' => '',
3709 'post_parent' => 0,
3710 'guid' => 'http://connector.com/?post_type=tribe_events&#038;p=58',
3711 'menu_order' => -1,
3712 'post_type' => 'tribe_events',
3713 'post_mime_type' => '',
3714 'comment_count' => 0,
3715 'filter' => 'raw',
3716 ],
3717 'attendies' => [
3718 'order_id' => 68,
3719 'purchaser_name' => 'sapna Rana',
3720 'purchaser_email' => 'sapnar@bsf.io',
3721 'provider' => 'Tribe__Tickets__RSVP',
3722 'provider_slug' => 'rsvp',
3723 'purchase_time' => '2023-01-19 09:48:43',
3724 'optout' => 1,
3725 'ticket' => 'Prime',
3726 'attendee_id' => 68,
3727 'security' => '2cefc3b53e',
3728 'product_id' => 65,
3729 'check_in' => '',
3730 'order_status' => 'yes',
3731 'order_status_label' => 'Going',
3732 'user_id' => 1,
3733 'ticket_sent' => 1,
3734 'event_id' => 58,
3735 'ticket_name' => 'Prime',
3736 'holder_name' => 'sapna Rana',
3737 'holder_email' => 'sapnar@bsf.io',
3738 'ticket_id' => 68,
3739 'qr_ticket_id' => 68,
3740 'security_code' => '2cefc3b53e',
3741 'attendee_meta' => '',
3742 'is_subscribed' => '',
3743 'is_purchaser' => 1,
3744 'ticket_exists' => 1,
3745 ],
3746 ];
3747
3748 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
3749
3750 $args = [
3751 'post_type' => 'tribe_rsvp_attendees',
3752 'orderby' => 'ID',
3753 'order' => 'DESC',
3754 'post_status' => 'publish',
3755 'numberposts' => 1,
3756 ];
3757
3758 if ( -1 !== $event_id ) {
3759 $args['meta_query'] = [
3760 [
3761 'key' => '_tribe_rsvp_event',
3762 'value' => $event_id,
3763 ],
3764 ];
3765 }
3766
3767 $attendees = get_posts( $args );
3768
3769 if ( count( $attendees ) > 0 ) {
3770 $attendee = $attendees[0];
3771 $attendee_id = $attendee->ID;
3772
3773 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
3774 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
3775
3776 $event_data = TheEventCalendar::get_event_context( $product_id, $order_id );
3777 $context['response_type'] = 'live';
3778 }
3779
3780 $context['pluggable_data'] = $event_data;
3781
3782 return $context;
3783 }
3784
3785 /**
3786 * Get last data for trigger
3787 *
3788 * @param array $data data.
3789 * @return array
3790 */
3791 public function search_woo_commerce_triggers_last_data( $data ) {
3792 $context = [];
3793 $context['response_type'] = 'sample';
3794 $context['pluggable_data'] = [];
3795 $user_data = WordPress::get_sample_user_context();
3796
3797 $product_data['product'] = [
3798 'id' => '169',
3799 'name' => 'Sample Product',
3800 'description' => 'This is description of sample product.',
3801 'short_description' => 'This is short description of sample product.',
3802 'image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
3803 'slug' => 'sample-product',
3804 'status' => 'publish',
3805 'type' => 'simple',
3806 'price' => '89',
3807 'featured' => '0',
3808 'sku' => 'hoodie-blue-sm',
3809 'regular_price' => '90',
3810 'sale_price' => '89',
3811 'total_sales' => '21',
3812 'category' => 'Uncategorized',
3813 'tags' => 'sample, new, 2022',
3814 ];
3815
3816 $comment_data = [
3817 'comment_id' => '1',
3818 'comment' => 'This is a sample comment..!',
3819 'comment_author' => 'testsure',
3820 'comment_date' => '2023-06-23 10:10:40',
3821 'comment_author_email' => 'testsure@example.com',
3822 ];
3823
3824 $order_data = [
3825 'order_id' => '500',
3826 'total_order_value' => '45',
3827 'currency' => 'USD',
3828 'shipping_total' => '5',
3829 'order_payment_method' => 'cod',
3830 'billing_firstname' => 'John',
3831 'billing_lastname' => 'Doe',
3832 'billing_company' => 'BSF',
3833 'billing_address_1' => '1004 Beaumont',
3834 'billing_address_2' => '',
3835 'billing_city' => 'Casper',
3836 'billing_state' => 'Wyoming',
3837 'billing_postcode' => '82601',
3838 'billing_country' => 'US',
3839 'billing_email' => 'john_doe@bsf.io',
3840 'billing_phone' => '(307) 7626541',
3841 'shipping_firstname' => 'John',
3842 'shipping_lastname' => 'Doe',
3843 'shipping_company' => 'BSF',
3844 'shipping_address_1' => '1004 Beaumont',
3845 'shipping_address_2' => '',
3846 'shipping_city' => 'Casper',
3847 'shipping_state' => 'Wyoming',
3848 'shipping_postcode' => '82601',
3849 'shipping_country' => 'US',
3850 'coupon_codes' => 'e3mstekq, f24sjakb',
3851 'total_items_in_order' => '1',
3852 'user_id' => '1',
3853 ];
3854
3855 $variation_data = [
3856 'product_variation_id' => '626',
3857 'product_variation' => 'Color: Silver',
3858 ];
3859
3860 $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
3861
3862 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
3863 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
3864
3865 $order_status = ( isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1 );
3866 $order_note_type = ( isset( $data['filter']['note_type']['value'] ) ? $data['filter']['note_type']['value'] : -1 );
3867
3868 if ( in_array( $term, [ 'product_added_to_cart', 'product_viewed' ], true ) ) {
3869 if ( -1 === $product_id ) {
3870 $args = [
3871 'post_type' => 'product',
3872 'orderby' => 'ID',
3873 'order' => 'DESC',
3874 'post_status' => 'publish',
3875 'numberposts' => 1,
3876 ];
3877 $products = get_posts( $args );
3878
3879 if ( count( $products ) > 0 ) {
3880 $product_id = $products[0]->ID;
3881 }
3882 }
3883
3884 if ( -1 !== $product_id ) {
3885 $post = get_post( $product_id );
3886 $user_data = WordPress::get_user_context( $post->post_author );
3887 $product_data['product_id'] = $product_id;
3888 $product_data['product'] = WooCommerce::get_product_context( $product_id );
3889 $terms = get_the_terms( $product_id, 'product_cat' );
3890 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
3891 $cat_name = [];
3892 foreach ( $terms as $cat ) {
3893 $cat_name[] = $cat->name;
3894 }
3895 $product_data['product']['category'] = implode( ', ', $cat_name );
3896 }
3897 $terms_tags = get_the_terms( $product_id, 'product_tag' );
3898 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
3899 $tag_name = [];
3900 foreach ( $terms_tags as $tag ) {
3901 $tag_name[] = $tag->name;
3902 }
3903 $product_data['product']['tag'] = implode( ', ', $tag_name );
3904 }
3905 unset( $product_data['product']['id'] ); //phpcs:ignore
3906 $context['response_type'] = 'live';
3907 }
3908
3909 if ( 'product_added_to_cart' === $term ) {
3910 $product_data['product_quantity'] = 1;
3911 }
3912
3913 $context['pluggable_data'] = array_merge( $product_data, $user_data );
3914
3915 } elseif ( 'product_reviewed' === $term ) {
3916 $comment_args = [
3917 'number' => 1,
3918 'type' => 'review',
3919 'orderby' => 'comment_ID',
3920 'post_id' => -1 !== $product_id ? $product_id : 0,
3921 ];
3922
3923 $comments = get_comments( $comment_args );
3924
3925 if ( count( $comments ) > 0 ) {
3926 $comment = $comments[0];
3927 $comment_data = [
3928 'comment_id' => $comment->comment_ID,
3929 'comment' => $comment->comment_content,
3930 'comment_author' => $comment->comment_author,
3931 'comment_date' => $comment->comment_date,
3932 'comment_author_email' => $comment->comment_author_email,
3933 ];
3934 $product_data = WooCommerce::get_product_context( $comment->comment_post_ID );
3935 if ( is_object( $comment ) ) {
3936 $terms = get_the_terms( (int) $comment->comment_post_ID, 'product_cat' );
3937 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
3938 $cat_name = [];
3939 foreach ( $terms as $cat ) {
3940 $cat_name[] = $cat->name;
3941 }
3942 $product_data['product']['category'] = implode( ', ', $cat_name );
3943 }
3944 $terms_tags = get_the_terms( (int) $comment->comment_post_ID, 'product_tag' );
3945 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
3946 $tag_name = [];
3947 foreach ( $terms_tags as $tag ) {
3948 $tag_name[] = $tag->name;
3949 }
3950 $product_data['product']['tag'] = implode( ', ', $tag_name );
3951 }
3952 }
3953 $user_data = WordPress::get_user_context( $comment->user_id );
3954 $context['response_type'] = 'live';
3955 }
3956
3957 $context['pluggable_data'] = array_merge( $product_data, $user_data, $comment_data );
3958
3959 } elseif ( 'product_purchased' === $term ) {
3960 $order_id = 0;
3961 $product_data['quantity'] = '1';
3962 if ( -1 !== $product_id ) {
3963 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
3964 if ( count( $order_ids ) > 0 ) {
3965 $order_id = $order_ids[0];
3966 }
3967 } else {
3968 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
3969 if ( count( $orders ) > 0 ) {
3970 $order_id = $orders[0]->get_id();
3971 }
3972 }
3973
3974 if ( 0 !== $order_id ) {
3975 $order = wc_get_order( $order_id );
3976
3977 if ( $order ) {
3978 $user_id = $order->get_customer_id();
3979 $items = $order->get_items();
3980
3981 $product_ids = [];
3982
3983 $iteration = 0;
3984 foreach ( $items as $item ) {
3985 if ( method_exists( $item, 'get_product_id' ) ) {
3986 $item_id = $item->get_product_id();
3987 if ( -1 === $product_id && 0 === $iteration ) {
3988 $product_ids[] = $item_id;
3989 break;
3990 } elseif ( $item_id === $product_id ) {
3991 $product_ids[] = $item_id;
3992 break;
3993 }
3994 }
3995
3996 $iteration++;
3997 }
3998 $order_data = WooCommerce::get_order_context( $order_id );
3999 $user_data = WordPress::get_user_context( $user_id );
4000 $order_data['total_items_in_order'] = count( $product_ids );
4001 $product_data = [];
4002 foreach ( $product_ids as $key => $product_id ) {
4003 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
4004 $terms = get_the_terms( $product_id, 'product_cat' );
4005 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
4006 $cat_name = [];
4007 foreach ( $terms as $cat ) {
4008 $cat_name[] = $cat->name;
4009 }
4010 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
4011 }
4012 $terms_tags = get_the_terms( $product_id, 'product_tag' );
4013 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
4014 $tag_name = [];
4015 foreach ( $terms_tags as $tag ) {
4016 $tag_name[] = $tag->name;
4017 }
4018 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
4019 }
4020 $product = wc_get_product( $product_id );
4021 /**
4022 *
4023 * Ignore line
4024 *
4025 * @phpstan-ignore-next-line
4026 */
4027 if ( $product->is_downloadable() ) {
4028 /**
4029 *
4030 * Ignore line
4031 *
4032 * @phpstan-ignore-next-line
4033 */
4034 foreach ( $product->get_downloads() as $key_download_id => $download ) {
4035 $download_name = $download->get_name();
4036 $download_link = $download->get_file();
4037 $download_id = $download->get_id();
4038 $download_type = $download->get_file_type();
4039 $download_ext = $download->get_file_extension();
4040 $product_data[ 'product' . $key ]['download'] = [
4041 'download_name' => $download_name,
4042 'download_link' => $download_link,
4043 'download_id' => $download_id,
4044 'download_type' => $download_type,
4045 'download_ext' => $download_ext,
4046 ];
4047 }
4048 }
4049 }
4050 $context['response_type'] = 'live';
4051 }
4052 }
4053
4054 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
4055
4056 } elseif ( 'variable_product_purchased' === $term ) {
4057 $product_variation_id = (int) ( isset( $data['filter']['product_variation_id']['value'] ) ? $data['filter']['product_variation_id']['value'] : -1 );
4058 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
4059
4060 foreach ( $order_ids as $order_id ) {
4061 $order = wc_get_order( $order_id );
4062
4063 if ( $order ) {
4064 $user_id = $order->get_customer_id();
4065 $items = $order->get_items();
4066 $product_variations = [];
4067
4068 $iteration = 0;
4069 foreach ( $items as $item ) {
4070 if ( method_exists( $item, 'get_variation_id' ) ) {
4071 $variation_id = $item->get_variation_id();
4072 if ( -1 === $product_variation_id && 0 === $iteration ) {
4073 $product_variations[] = $variation_id;
4074 break;
4075 } elseif ( $variation_id === $product_variation_id ) {
4076 $product_variations[] = $variation_id;
4077 break;
4078 }
4079 }
4080
4081 $iteration++;
4082 }
4083
4084 if ( count( $product_variations ) > 0 ) {
4085 $product_data = WooCommerce::get_product_context( $product_variation_id );
4086 $order_data = WooCommerce::get_order_context( $order_id );
4087 $user_data = WordPress::get_user_context( $user_id );
4088 $variation_data = [
4089 'product_variation_id' => $product_variations[0],
4090 'product_variation' => get_the_excerpt( $product_variations[0] ),
4091 ];
4092
4093 $context['response_type'] = 'live';
4094 break;
4095 }
4096 }
4097 }
4098
4099 $context['pluggable_data'] = array_merge( $order_data, $user_data, $variation_data );
4100
4101 } elseif ( 'variable_subscription_purchased' === $term ) {
4102 $product_data['quantity'] = '1';
4103 $product_data['product_name'] = 'Sample Product';
4104 $product_data['billing_period'] = '2021-2022';
4105
4106 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
4107
4108 $subscription_order_id = 0;
4109 $order_ids = [];
4110
4111 if ( -1 !== $product_id ) {
4112 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
4113
4114 } else {
4115 $orders = wc_get_orders( [] );
4116 if ( count( $orders ) > 0 ) {
4117 $order_ids[] = $orders[0]->get_id();
4118 }
4119 }
4120
4121 foreach ( $order_ids as $order_id ) {
4122 $query_args = [
4123 'post_type' => 'shop_subscription',
4124 'orderby' => 'ID',
4125 'order' => 'DESC',
4126 'post_status' => 'wc-active',
4127 'posts_per_page' => 1,
4128 'post_parent' => $order_id,
4129 ];
4130 $query_result = new WP_Query( $query_args );
4131 $subscription_orders = $query_result->get_posts();
4132
4133 if ( count( $subscription_orders ) > 0 ) {
4134 $subscription_order_id = $subscription_orders[0]->ID;
4135 break;
4136 }
4137 }
4138
4139 if ( 0 !== $subscription_order_id ) {
4140 $subscription = wcs_get_subscription( $subscription_order_id );
4141 if ( $subscription instanceof WC_Subscription ) {
4142 $last_order_id = $subscription->get_last_order();
4143 if ( ! empty( $last_order_id ) && $last_order_id === $subscription->get_parent_id() ) {
4144 $user_id = wc_get_order( $last_order_id )->get_customer_id();
4145 $items = $subscription->get_items();
4146
4147 foreach ( $items as $item ) {
4148 $product = $item->get_product();
4149 if ( class_exists( '\WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product ) ) {
4150 if ( $product->is_type( [ 'subscription', 'subscription_variation', 'variable-subscription' ] ) ) {
4151
4152 $product_data = WooCommerce::get_variable_subscription_product_context( $item, $last_order_id );
4153 $user_data = WordPress::get_user_context( $user_id );
4154
4155 $context['response_type'] = 'live';
4156 $context['pluggable_data'] = array_merge( $product_data, $user_data );
4157 }
4158 }
4159 }
4160 }
4161 }
4162 }
4163 } elseif ( 'order_created' === $term ) {
4164 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
4165 $order_id = '';
4166 if ( count( $orders ) > 0 ) {
4167 $order_id = $orders[0]->get_id();
4168 $order = wc_get_order( $order_id );
4169 $user_id = $order->get_customer_id();
4170 $order_sample_data = array_merge(
4171 WooCommerce::get_order_context( $order_id ),
4172 WordPress::get_user_context( $user_id )
4173 );
4174 $context['response_type'] = 'live';
4175 }
4176
4177 $context['pluggable_data'] = $order_sample_data;
4178
4179 } elseif ( 'order_status_changed' === $term ) {
4180 if ( -1 == $order_status ) {
4181 $args = [
4182 'numberposts' => 1,
4183 'orderby' => 'date',
4184 'order' => 'DESC',
4185 ];
4186 } else {
4187 $args = [
4188 'status' => [ $order_status ],
4189 'numberposts' => 1,
4190 'orderby' => 'date',
4191 'order' => 'DESC',
4192 ];
4193 }
4194 $orders = wc_get_orders( $args );
4195 $order_id = '';
4196 if ( count( $orders ) > 0 ) {
4197 $order_id = $orders[0]->get_id();
4198 $order = wc_get_order( $order_id );
4199 $user_id = $order->get_customer_id();
4200 $order_sample_data = array_merge(
4201 WooCommerce::get_order_context( $order_id ),
4202 WordPress::get_user_context( $user_id )
4203 );
4204 $context['response_type'] = 'live';
4205 }
4206 $context['pluggable_data'] = $order_sample_data;
4207 } elseif ( 'order_note_added' === $term ) {
4208 global $wpdb;
4209 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type ='order_note' ORDER BY comment_ID DESC LIMIT 1" );
4210 $order_id = '';
4211 if ( ! empty( $result ) ) {
4212 $order_id = $result[0]->comment_post_ID;
4213 $order = wc_get_order( $order_id );
4214 if ( ! empty( $order ) ) {
4215 $user_id = $order->get_customer_id();
4216 $order_sample_data = array_merge(
4217 WooCommerce::get_order_context( $order_id ),
4218 WordPress::get_user_context( $user_id )
4219 );
4220 if ( -1 == $order_note_type ) {
4221 $args = [
4222 'order_id' => $order_id,
4223 'limit' => 1,
4224 ];
4225 } else {
4226 $args = [
4227 'order_id' => $order_id,
4228 'type' => $order_note_type,
4229 'limit' => 1,
4230 ];
4231 }
4232
4233 $notes = wc_get_order_notes( $args );
4234 if ( ! empty( $notes ) ) {
4235 $order_sample_data['note'] = [
4236 'id' => $notes[0]->id,
4237 'date' => $notes[0]->date_created,
4238 'author' => $notes[0]->added_by,
4239 'content' => $notes[0]->content,
4240 ];
4241 } else {
4242 $order_sample_data['note'] = [
4243 'id' => '1',
4244 'date' => [
4245 'date' => '2023-06-23 10:10:40',
4246 'timezone_type' => 1,
4247 'timezone' => '+00:00',
4248 ],
4249 'author' => 'admin',
4250 'content' => 'new note',
4251 ];
4252 }
4253 $order_sample_data['note_type'] = $order_note_type;
4254 $context['response_type'] = 'live';
4255 }
4256 } else {
4257 $order_sample_data['note'] = [
4258 'id' => '1',
4259 'date' => [
4260 'date' => '2023-06-23 10:10:40',
4261 'timezone_type' => 1,
4262 'timezone' => '+00:00',
4263 ],
4264 'author' => 'admin',
4265 'content' => 'new note',
4266 ];
4267 $order_sample_data['note_type'] = 'customer';
4268 }
4269 $context['pluggable_data'] = $order_sample_data;
4270 } elseif ( 'order_paid' === $term ) {
4271 $args = [
4272 'status' => [ 'completed' ],
4273 'numberposts' => 1,
4274 ];
4275 $orders = wc_get_orders( $args );
4276 $order_id = '';
4277 if ( count( $orders ) > 0 ) {
4278 $order_id = $orders[0]->get_id();
4279 $order = wc_get_order( $order_id );
4280 $user_id = $order->get_customer_id();
4281 $order_sample_data = array_merge(
4282 WooCommerce::get_order_context( $order_id ),
4283 WordPress::get_user_context( $user_id )
4284 );
4285 $context['response_type'] = 'live';
4286 }
4287 $context['pluggable_data'] = $order_sample_data;
4288 }
4289
4290 return $context;
4291 }
4292
4293 /**
4294 * Search LMS data.
4295 *
4296 * @param array $data data.
4297 * @return array
4298 */
4299 public function search_lifter_lms_last_data( $data ) {
4300 global $wpdb;
4301 $post_type = $data['post_type'];
4302 $meta_key = '_is_complete';
4303 $trigger = $data['search_term'];
4304 $context = [];
4305 if ( 'lifterlms_purchase_course' === $trigger ) {
4306 $product_type = 'course';
4307 $post_id = $data['filter']['course_id']['value'];
4308 } elseif ( 'lifterlms_purchase_membership' === $trigger ) {
4309 $product_type = 'membership';
4310 $post_id = $data['filter']['membership_id']['value'];
4311 } elseif ( 'lifterlms_lesson_completed' === $trigger ) {
4312 $post_id = $data['filter']['lesson']['value'];
4313 } elseif ( 'lifterlms_course_completed' === $trigger ) {
4314 $post_id = $data['filter']['course']['value'];
4315 }
4316
4317 $where = 'postmeta.post_id = "' . $post_id . '" AND';
4318
4319 if ( 'llms_order' === $post_type ) {
4320 if ( -1 === $post_id ) {
4321 $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 ) );
4322 } else {
4323 $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 ) );
4324 }
4325 } else {
4326 if ( -1 === $post_id ) {
4327 $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 ) );
4328 } else {
4329 $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 ) );
4330 }
4331 }
4332
4333 $response = [];
4334 if ( ! empty( $result ) ) {
4335 $result_post_id = $result[0]->post_id;
4336 $result_user_id = $result[0]->user_id;
4337
4338 switch ( $trigger ) {
4339 case 'lifterlms_lesson_completed':
4340 $context = array_merge(
4341 WordPress::get_user_context( $result_user_id ),
4342 LifterLMS::get_lms_lesson_context( $result_post_id )
4343 );
4344
4345 $context['course'] = get_the_title( get_post_meta( $result_post_id, '_llms_parent_course', true ) );
4346 if ( '' !== ( get_post_meta( $result_post_id, '_llms_parent_section', true ) ) ) {
4347 $context['parent_section'] = get_the_title( get_post_meta( $result_post_id, '_llms_parent_section', true ) );
4348 }
4349 break;
4350 case 'lifterlms_course_completed':
4351 $context = array_merge(
4352 WordPress::get_user_context( $result_user_id ),
4353 LifterLMS::get_lms_course_context( $result_post_id )
4354 );
4355 break;
4356 case 'lifterlms_purchase_course':
4357 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
4358 $context['course_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
4359 $context['course_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
4360 $context['course_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
4361 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
4362 $context ['order'] = WordPress::get_post_context( $result_post_id );
4363 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
4364 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
4365 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
4366 $context = array_merge( $context, WordPress::get_user_context( $user_id ) );
4367 break;
4368 case 'lifterlms_purchase_membership':
4369 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
4370 $context['membership_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
4371 $context['membership_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
4372 $context['membership_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
4373 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
4374 $context ['order'] = WordPress::get_post_context( $result_post_id );
4375 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
4376 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
4377 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
4378 $context = array_merge( $context, WordPress::get_user_context( $user_id ) );
4379 break;
4380 default:
4381 return;
4382
4383 }
4384 $response['pluggable_data'] = $context;
4385 $response['response_type'] = 'live';
4386
4387 }
4388
4389 return $response;
4390
4391 }
4392
4393 /**
4394 * Search SM data.
4395 *
4396 * @param array $data data.
4397 * @return array
4398 */
4399 public function search_suremember_last_data( $data ) {
4400 global $wpdb;
4401 $post_type = $data['post_type'];
4402 $meta_key = '_is_complete';
4403 $trigger = $data['search_term'];
4404 $post_id = $data['filter']['group_id']['value'];
4405
4406 if ( 'suremember_updated_group' === $trigger ) {
4407 if ( -1 === $post_id ) {
4408 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts as posts WHERE posts.post_type=%s", $post_type ) );
4409 } else {
4410 $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 ) );
4411 }
4412 } else {
4413 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}usermeta as usermeta WHERE usermeta.meta_key = %s", 'suremembers_user_access_group_' . $post_id ) );
4414 }
4415
4416 $response = [];
4417
4418 if ( ! empty( $result ) ) {
4419 $context = [];
4420 switch ( $trigger ) {
4421 case 'suremember_updated_group':
4422 $group_id = $result[0]->ID;
4423 $suremembers_post['rules'] = get_post_meta( $group_id, 'suremembers_plan_include', true );
4424 $suremembers_post['exclude'] = get_post_meta( $group_id, 'suremembers_plan_exclude', true ); //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
4425 $suremembers_post['suremembers_user_roles'] = get_post_meta( $group_id, 'suremembers_user_roles', true );
4426 $suremembers_post['title'] = get_the_title( $group_id );
4427 $suremembers_post['restrict'] = get_post_meta( $group_id, 'suremembers_plan_rules', true )['restrict'];
4428 $context['group'] = array_merge( WordPress::get_post_context( $group_id ), $suremembers_post );
4429 $context['group_id'] = $group_id;
4430 unset( $context['group']['ID'] );
4431 $response['pluggable_data'] = $context;
4432 $response['response_type'] = 'live';
4433 break;
4434 case 'suremember_user_added_in_group':
4435 foreach ( $result as $res ) {
4436 $meta_value = unserialize( $res->meta_value );
4437 if ( 'active' === $meta_value['status'] ) {
4438 $context = WordPress::get_user_context( $res->user_id );
4439 $context['group'] = WordPress::get_post_context( $post_id );
4440 $response['pluggable_data'] = $context;
4441 $response['response_type'] = 'live';
4442 }
4443 }
4444 break;
4445 case 'suremember_user_removed_from_group':
4446 foreach ( $result as $res ) {
4447 $meta_value = unserialize( $res->meta_value );
4448 if ( 'revoked' === $meta_value['status'] ) {
4449 $context = WordPress::get_user_context( $res->user_id );
4450 $context['group'] = WordPress::get_post_context( $post_id );
4451 $response['pluggable_data'] = $context;
4452 $response['response_type'] = 'live';
4453 }
4454 }
4455 break;
4456 default:
4457 return;
4458
4459 }
4460 }
4461
4462 return $response;
4463
4464 }
4465
4466 /**
4467 * Search CartFlows data.
4468 *
4469 * @param array $data data.
4470 * @return array
4471 */
4472 public function search_cartflows_last_data( $data ) {
4473 global $wpdb;
4474 $trigger = $data['search_term'];
4475 $context = [];
4476 if ( 'cartflows_offer_accepted' === $trigger ) {
4477 $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'" );
4478 }
4479 $response = [];
4480 if ( ! empty( $result ) ) {
4481 $context = [];
4482 $order_upsell_id = $result[0]->post_id;
4483 $step_id = get_post_meta( $order_upsell_id, '_cartflows_offer_step_id', true );
4484 $order_id = get_post_meta( $order_upsell_id, '_cartflows_offer_parent_id', true );
4485 $order = wc_get_order( $order_id );
4486 $upsell_order = wc_get_order( $order_upsell_id );
4487 $variation_id = $upsell_order->get_items()[0]['product_id'];
4488 $input_qty = $upsell_order->get_items()[0]['quantity'];
4489 $offer_product = wcf_pro()->utils->get_offer_data( $step_id, $variation_id, $input_qty, $order_id );
4490 $user_id = get_post_meta( $order_upsell_id, '_customer_user', true );
4491 $context = WordPress::get_user_context( $user_id );
4492 $context['order'] = $order->get_data();
4493 $context['upsell'] = $offer_product;
4494 $response['pluggable_data'] = $context;
4495 $response['response_type'] = 'live';
4496 }
4497
4498 return $response;
4499
4500 }
4501
4502
4503 /**
4504 * Fetch user context.
4505 *
4506 * @param int $initiator_id initiator id.
4507 * @param int $friend_id friend id.
4508 * @return array
4509 */
4510 public function get_user_context( $initiator_id, $friend_id ) {
4511 $context = WordPress::get_user_context( $initiator_id );
4512
4513 $friend_context = WordPress::get_user_context( $friend_id );
4514
4515 $avatar = get_avatar_url( $initiator_id );
4516
4517 $context['avatar_url'] = ( $avatar ) ? $avatar : '';
4518
4519 $context['friend_id'] = $friend_id;
4520 $context['friend_first_name'] = $friend_context['user_firstname'];
4521 $context['friend_last_name'] = $friend_context['user_lastname'];
4522 $context['friend_email'] = $friend_context['user_email'];
4523
4524 $friend_avatar = get_avatar_url( $friend_id );
4525 $context['friend_avatar_url'] = $friend_avatar;
4526 return $context;
4527 }
4528
4529 /**
4530 * Search BP data.
4531 *
4532 * @param array $data data.
4533 * @return array
4534 */
4535 public function search_pluggables_bp_friendships( $data ) {
4536 global $wpdb, $bp;
4537 $context = [];
4538 $sample['pluggable_data'] = [
4539 'wp_user_id' => 4,
4540 'user_login' => 'katy1ßßßß',
4541 'display_name' => 'Katy Smith',
4542 'user_firstname' => 'Katy',
4543 'user_lastname' => 'Smith',
4544 'user_email' => 'katy1@gmail.com',
4545 'user_role' => [ 'subscriber' ],
4546 'avatar_url' => 'http://pqr.com/avatar',
4547 'friend_id' => 1,
4548 'friend_first_name' => 'John',
4549 'friend_last_name' => 'Wick',
4550 'friend_email' => 'john@gmail.com',
4551 'friend_avatar_url' => 'http://abc.com/avatar',
4552 ];
4553 $sample['response_type'] = 'sample';
4554
4555 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_friends'" );
4556 if ( $table_exists ) {
4557 $friendships = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_friends LIMIT 1" );
4558 if ( ! empty( $friendships ) ) {
4559 $friendship = $friendships[0];
4560 $initiator_id = $friendship->initiator_user_id;
4561 $friend_user_id = $friendship->friend_user_id;
4562 $context['pluggable_data'] = $this->get_user_context( $initiator_id, $friend_user_id );
4563 $context['response_type'] = 'live';
4564 } else {
4565 $context = $sample;
4566 }
4567 } else {
4568 $context = $sample;
4569 }
4570
4571
4572 return $context;
4573 }
4574
4575 /**
4576 * Search Buddyboss profile types data.
4577 *
4578 * @param array $data data.
4579 * @return array
4580 */
4581 public function search_pluggables_bp_profile_types( $data ) {
4582 global $wpdb, $bp;
4583 $context = [];
4584 $sample['pluggable_data'] = [
4585 'wp_user_id' => 4,
4586 'user_login' => 'katy1ßßßß',
4587 'display_name' => 'Katy Smith',
4588 'user_firstname' => 'Katy',
4589 'user_lastname' => 'Smith',
4590 'user_email' => 'katy1@gmail.com',
4591 'user_role' => [ 'subscriber' ],
4592 'bb_profile_type' => '10',
4593 'bb_profile_type_name' => 'student',
4594 ];
4595 $sample['response_type'] = 'sample';
4596
4597 $post_id = $data['filter']['bb_profile_type']['value'];
4598 $get_existing = get_post_meta( $post_id, '_bp_member_type_key', true );
4599
4600 $type_term = get_term_by(
4601 'name',
4602 /**
4603 *
4604 * Ignore line
4605 *
4606 * @phpstan-ignore-next-line
4607 */
4608 $get_existing,
4609 'bp_member_type'
4610 );
4611
4612 $results = $wpdb->get_results(
4613 $wpdb->prepare(
4614 "SELECT u.ID FROM {$wpdb->prefix}users u INNER JOIN {$wpdb->prefix}term_relationships r
4615 ON u.ID = r.object_id WHERE u.user_status = 0 AND
4616 r.term_taxonomy_id = %d ORDER BY RAND() LIMIT 1",
4617 /**
4618 *
4619 * Ignore line
4620 *
4621 * @phpstan-ignore-next-line
4622 */
4623 $type_term->term_id
4624 )
4625 );
4626
4627 if ( ! empty( $results ) ) {
4628 $user = $results[0];
4629 $context['pluggable_data'] = WordPress::get_user_context( $user->ID );
4630 $context['pluggable_data']['bb_profile_type'] = $post_id;
4631 $context['pluggable_data']['bb_profile_type_name'] = get_post_meta( $post_id, '_bp_member_type_label_singular_name', true );
4632 $context['response_type'] = 'live';
4633 } else {
4634 $context = $sample;
4635 }
4636
4637 return $context;
4638 }
4639 /**
4640 * Search BP data.
4641 *
4642 * @param array $data data.
4643 * @return array
4644 */
4645 public function search_pluggables_bp_groups( $data ) {
4646 global $wpdb, $bp;
4647 $context = [];
4648 $group_data = [];
4649 $args = [
4650 'orderby' => 'user_nicename',
4651 'order' => 'ASC',
4652 'number' => 1,
4653 ];
4654
4655 $users = get_users( $args );
4656
4657 if ( isset( $data['filter']['group_id']['value'] ) ) {
4658 $group_id = $data['filter']['group_id']['value'];
4659 $args['group_id'] = $group_id;
4660 if ( $group_id > 0 ) {
4661 $group = groups_get_group( $group_id );
4662 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
4663 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
4664 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
4665 } else {
4666 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_groups'" );
4667 if ( $table_exists ) {
4668 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups LIMIT 1" );
4669 $context['groups'] = $groups;
4670 if ( ! empty( $groups ) ) {
4671 foreach ( $groups as $group ) {
4672 $group_data['group_id'] = $group->id;
4673 $group_data['group_name'] = $group->name;
4674 $group_data['group_description'] = $group->description;
4675 }
4676 }
4677 }
4678 }
4679 }
4680
4681 if ( ! empty( $users ) ) {
4682 $user = $users[0];
4683 $pluggable_data = $group_data;
4684
4685 $avatar = get_avatar_url( $user->ID );
4686 $pluggable_data['wp_user_id'] = $user->ID;
4687 $pluggable_data['avatar_url'] = ( $avatar ) ? $avatar : '';
4688 $pluggable_data['user_login'] = $user->user_login;
4689 $pluggable_data['display_name'] = $user->display_name;
4690 $pluggable_data['user_firstname'] = $user->user_firstname;
4691 $pluggable_data['user_lastname'] = $user->user_lastname;
4692 $pluggable_data['user_email'] = $user->user_email;
4693 $pluggable_data['user_role'] = $user->roles;
4694 $context['pluggable_data'] = $pluggable_data;
4695 $context['response_type'] = 'live';
4696 } else {
4697 $context['pluggable_data'] = [
4698 'wp_user_id' => 1,
4699 'user_login' => 'admin',
4700 'display_name' => 'Test User',
4701 'user_firstname' => 'Test',
4702 'user_lastname' => 'User',
4703 'user_email' => 'testuser@gmail.com',
4704 'user_role' => [ 'subscriber' ],
4705 'group_id' => 112,
4706 'group_name' => 'Test Group',
4707 'group_description' => 'Test Group Description',
4708 ];
4709 $context['response_type'] = 'sample';
4710 }
4711
4712 return $context;
4713 }
4714
4715 /**
4716 * Search complete courses.
4717 *
4718 * @param array $data data.
4719 * @return array
4720 */
4721 public function search_pluggables_complete_course( $data ) {
4722 global $wpdb;
4723 $context = [];
4724
4725 if ( isset( $data['filter']['sfwd_course_id']['value'] ) ) {
4726 $course_id = $data['filter']['sfwd_course_id']['value'];
4727 }
4728 if ( -1 === $course_id ) {
4729 $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.course_id= %d", 1, $course_id ) );
4730 } else {
4731 $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 ) );
4732 }
4733
4734 if ( ! empty( $courses ) ) {
4735 $course = $courses[0];
4736 $course_data['course_name'] = $course->post_title;
4737 $course_data['sfwd_course_id'] = $course->ID;
4738 $course_data['course_url'] = get_permalink( $course->ID );
4739 $course_data['course_featured_image_id'] = get_post_meta( $course->ID, '_thumbnail_id', true );
4740 $course_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course->ID );
4741 $context['response_type'] = 'live';
4742 } else {
4743 $course_data['course_name'] = 'Test Course';
4744 $course_data['sfwd_course_id'] = 112;
4745 $course_data['course_url'] = 'https://abc.com/test-course';
4746 $course_data['course_featured_image_id'] = 113;
4747 $course_data['course_featured_image_url'] = 'https://pqr.com/test-course-img';
4748 $context['response_type'] = 'sample';
4749 }
4750
4751 $users_data = $this->search_pluggables_add_user_role( [] );
4752 $user_data = $users_data['pluggable_data'];
4753
4754 $context['pluggable_data'] = array_merge( $course_data, $user_data );
4755 return $context;
4756 }
4757
4758 /**
4759 * Search lessons.
4760 *
4761 * @param array $data data.
4762 * @return array
4763 */
4764 public function search_pluggables_complete_lesson( $data ) {
4765 global $wpdb;
4766 $context = [];
4767
4768 if ( isset( $data['filter']['sfwd_lesson_id']['value'] ) ) {
4769 $lesson_id = $data['filter']['sfwd_lesson_id']['value'];
4770 $course_id = $data['filter']['sfwd_course_id']['value'];
4771 }
4772 $course = get_post( $course_id );
4773 $pluggable_data = LearnDash::get_course_context( $course );
4774
4775 if ( -1 === $lesson_id ) {
4776 $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 ) );
4777 } else {
4778 $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 ) );
4779 }
4780
4781 if ( ! empty( $lessons ) ) {
4782 $lesson = $lessons[0];
4783
4784 $pluggable_data = WordPress::get_user_context( $lesson->user_id );
4785 $pluggable_data['lesson_name'] = $lesson->post_title;
4786 $pluggable_data['sfwd_lesson_id'] = $lesson->ID;
4787 $pluggable_data['lesson_url'] = get_permalink( $lesson->ID );
4788 $pluggable_data['lesson_featured_image_id'] = get_post_meta( $lesson->ID, '_thumbnail_id', true );
4789 $pluggable_data['lesson_featured_image_url'] = get_the_post_thumbnail_url( $lesson->ID );
4790 $context['response_type'] = 'live';
4791 } else {
4792 $pluggable_data = WordPress::get_sample_user_context();
4793 $pluggable_data['lesson_name'] = 'Test Lesson';
4794 $pluggable_data['sfwd_lesson_id'] = 114;
4795 $pluggable_data['lesson_url'] = 'https://abc.com/test-lesson';
4796 $pluggable_data['lesson_featured_image_id'] = 116;
4797 $pluggable_data['lesson_featured_image_url'] = 'https://pqr.com/test-lesson-img';
4798 $context['response_type'] = 'sample';
4799 }
4800
4801 $context['pluggable_data'] = $pluggable_data;
4802 return $context;
4803 }
4804
4805 /**
4806 * Search topics.
4807 *
4808 * @param array $data data.
4809 * @return array
4810 */
4811 public function search_pluggables_complete_topic( $data ) {
4812 global $wpdb;
4813 $context = [];
4814
4815 if ( isset( $data['filter']['sfwd_topic_id']['value'] ) ) {
4816 $topic_id = $data['filter']['sfwd_topic_id']['value'];
4817 $course_id = $data['filter']['sfwd_course_id']['value'];
4818 }
4819 $course = get_post( $course_id );
4820 $pluggable_data = LearnDash::get_course_context( $course );
4821
4822 if ( -1 === $topic_id ) {
4823 $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 ) );
4824 } else {
4825 $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 ) );
4826 }
4827
4828 if ( ! empty( $topics ) ) {
4829 $topic = $topics[0];
4830 $pluggable_data = WordPress::get_user_context( $topics[0]->user_id );
4831 $pluggable_data['topic_name'] = $topic->post_title;
4832 $pluggable_data['sfwd_topic_id'] = $topic->ID;
4833 $pluggable_data['topic_url'] = get_permalink( $topic->ID );
4834 $pluggable_data['topic_featured_image_id'] = get_post_meta( $topic->ID, '_thumbnail_id', true );
4835 $pluggable_data['topic_featured_image_url'] = get_the_post_thumbnail_url( $topic->ID );
4836 $context['response_type'] = 'live';
4837 } else {
4838 $pluggable_data = WordPress::get_sample_user_context();
4839 $pluggable_data['topic_name'] = 'Test Topic';
4840 $pluggable_data['sfwd_topic_id'] = 117;
4841 $pluggable_data['topic_url'] = 'https://abc.com/test-topic';
4842 $pluggable_data['topic_featured_image_id'] = 118;
4843 $pluggable_data['topic_featured_image_url'] = 'https://pqr.com/test-topic-img';
4844 $context['response_type'] = 'sample';
4845 }
4846
4847 $context['pluggable_data'] = $pluggable_data;
4848 return $context;
4849 }
4850
4851 /**
4852 * Search purchase courses.
4853 *
4854 * @param array $data data.
4855 * @return array
4856 */
4857 public function search_pluggables_purchase_course( $data ) {
4858 $context = [];
4859 $context['response_type'] = 'sample';
4860
4861 $purchase_data = [
4862 'course_product_id' => '1',
4863 'course_product_name' => 'Sample Course',
4864 'currency' => 'USD',
4865 'total_amount' => '100',
4866 'first_name' => 'John',
4867 'last_name' => 'Doe',
4868 'email' => 'john_doe@bsf.io',
4869 'phone' => '+923007626541',
4870 ];
4871
4872 $product_id = (int) ( isset( $data['filter']['course_product_id']['value'] ) ? $data['filter']['course_product_id']['value'] : '-1' );
4873 $order_id = 0;
4874
4875 if ( -1 !== $product_id ) {
4876 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
4877
4878 if ( count( $order_ids ) > 0 ) {
4879 $order_id = $order_ids[0];
4880 }
4881 } else {
4882 $orders = wc_get_orders( [] );
4883 if ( count( $orders ) > 0 ) {
4884 foreach ( $orders as $order ) {
4885 $items = $order->get_items();
4886
4887 if ( count( $items ) > 1 ) {
4888 continue;
4889 }
4890
4891 foreach ( $items as $item ) {
4892 if ( method_exists( $item, 'get_product_id' ) ) {
4893 $product_id = $item->get_product_id();
4894 if ( ! empty( get_post_meta( $item->get_product_id(), '_related_course', true ) ) ) {
4895 $order_id = $order->get_id();
4896 break;
4897 }
4898 }
4899 }
4900 }
4901 }
4902 }
4903
4904 if ( 0 !== $order_id ) {
4905 $order = wc_get_order( $order_id );
4906
4907 if ( $order ) {
4908
4909 $purchase_data = LearnDash::get_purchase_course_context( $order );
4910
4911 $context['response_type'] = 'live';
4912 }
4913 }
4914
4915 $context['pluggable_data'] = $purchase_data;
4916
4917 return $context;
4918 }
4919
4920 /**
4921 * Fetch BB templates.
4922 *
4923 * @return array
4924 */
4925 public function get_beaver_builder_templates() {
4926 $allowed_types = [ 'subscribe-form', 'contact-form' ];
4927 $templates = [];
4928 $all_templates = get_posts(
4929 [
4930 'post_type' => 'fl-builder-template',
4931 'meta_key' => '_fl_builder_data',
4932 'posts_per_page' => -1,
4933 ]
4934 );
4935 $posts = get_posts(
4936 [
4937 'post_type' => 'any',
4938 'meta_key' => '_fl_builder_data',
4939 'posts_per_page' => -1,
4940 ]
4941 );
4942 $posts = array_merge( $all_templates, $posts );
4943
4944 if ( ! empty( $posts ) ) {
4945 foreach ( $posts as $post ) {
4946 $meta = get_post_meta( $post->ID, '_fl_builder_data', true );
4947 foreach ( (array) $meta as $node_id => $node ) {
4948 if ( isset( $node->type ) && 'module' === $node->type ) {
4949 $settings = $node->settings;
4950 if ( in_array( $settings->type, $allowed_types, true ) ) {
4951 $label = $post->post_title;
4952 if ( '' !== $settings->node_label ) {
4953 $label .= ' - ' . $settings->node_label;
4954 }
4955 $templates[] = [
4956 'label' => $label,
4957 'value' => $node_id,
4958 ];
4959 }
4960 }
4961 }
4962 }
4963 }
4964 return $templates;
4965 }
4966
4967 /**
4968 * Search beaver builder forms.
4969 *
4970 * @param array $data data.
4971 * @return array
4972 */
4973 public function search_beaver_builder_forms( $data ) {
4974 $templates = $this->get_beaver_builder_templates();
4975 return [
4976 'options' => $templates,
4977 'hasMore' => false,
4978 ];
4979 }
4980
4981 /**
4982 * Search fluentcrm fields.
4983 *
4984 * @param array $data data.
4985 * @return array
4986 */
4987 public function search_fluentcrm_custom_fields( $data ) {
4988 $context = [];
4989 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
4990 $context['fields'] = $custom_fields;
4991 return $context;
4992 }
4993
4994 /**
4995 * Fetch WP JOB Manager Last Data.
4996 *
4997 * @param array $data data.
4998 * @return array
4999 */
5000 public function search_wp_job_manger_last_data( $data ) {
5001 global $wpdb;
5002 $job_type = $data['filter']['job_type']['value'];
5003 $args = [
5004 'posts_per_page' => 1,
5005 'post_type' => 'job_listing',
5006 'orderby' => 'id',
5007 'order' => 'DESC',
5008 ];
5009
5010 if ( -1 !== $job_type ) {
5011 $args['tax_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
5012 [
5013 'taxonomy' => 'job_listing_type',
5014 'field' => 'term_id',
5015 'terms' => $job_type,
5016 ],
5017 ];
5018 }
5019 $posts = get_posts( $args );
5020 if ( empty( $posts ) ) {
5021 $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 );
5022 return $context;
5023 }
5024
5025 $post = $posts[0];
5026 $post_content = WordPress::get_post_context( $post->ID );
5027 $post_meta = WordPress::get_post_meta( $post->ID );
5028 $job_data = array_merge( $post_content, $post_meta, WordPress::get_user_context( $post->post_author ) );
5029 foreach ( $job_data as $key => $job ) {
5030 $newkey = str_replace( 'post', 'wpjob', $key );
5031 unset( $job_data[ $key ] );
5032 $job_data[ $newkey ] = $job;
5033 }
5034 $context['response_type'] = 'live';
5035 $context['pluggable_data'] = $job_data;
5036 return $context;
5037
5038 }
5039
5040 /**
5041 * Get Amelia Appointment Category.
5042 *
5043 * @param array $data data.
5044 *
5045 * @return array
5046 */
5047 public function search_amelia_category_list( $data ) {
5048
5049 global $wpdb;
5050
5051 $page = $data['page'];
5052 $limit = Utilities::get_search_page_limit();
5053 $offset = $limit * ( $page - 1 );
5054
5055 $categories = $wpdb->get_results(
5056 $wpdb->prepare(
5057 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_categories WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
5058 [ 'visible', $limit, $offset ]
5059 ),
5060 OBJECT
5061 );
5062
5063 $categories_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
5064
5065 $options = [];
5066 if ( ! empty( $categories ) ) {
5067 foreach ( $categories as $category ) {
5068 $options[] = [
5069 'label' => $category->name,
5070 'value' => $category->id,
5071 ];
5072 }
5073 }
5074
5075 return [
5076 'options' => $options,
5077 'hasMore' => $categories_count > $limit && $categories_count > $offset,
5078 ];
5079
5080 }
5081
5082 /**
5083 * Get Amelia Appointment Services.
5084 *
5085 * @param array $data data.
5086 *
5087 * @return array
5088 */
5089 public function search_amelia_service_list( $data ) {
5090
5091 global $wpdb;
5092
5093 $page = $data['page'];
5094 $limit = Utilities::get_search_page_limit();
5095 $offset = $limit * ( $page - 1 );
5096
5097 $services = $wpdb->get_results(
5098 $wpdb->prepare(
5099 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_services
5100 WHERE categoryId = %d AND status = %s
5101 ORDER BY name ASC LIMIT %d OFFSET %d",
5102 [ $data['dynamic'], 'visible', $limit, $offset ]
5103 ),
5104 OBJECT
5105 );
5106
5107 $services_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
5108
5109 $options = [];
5110 if ( ! empty( $services ) ) {
5111 foreach ( $services as $category ) {
5112 $options[] = [
5113 'label' => $category->name,
5114 'value' => $category->id,
5115 ];
5116 }
5117 }
5118
5119 return [
5120 'options' => $options,
5121 'hasMore' => $services_count > $limit && $services_count > $offset,
5122 ];
5123
5124 }
5125
5126 /**
5127 * Get Amelia Events.
5128 *
5129 * @param array $data data.
5130 *
5131 * @return array
5132 */
5133 public function search_amelia_events_list( $data ) {
5134
5135 global $wpdb;
5136
5137 $page = $data['page'];
5138 $limit = Utilities::get_search_page_limit();
5139 $offset = $limit * ( $page - 1 );
5140
5141 $events = $wpdb->get_results(
5142 $wpdb->prepare(
5143 "SELECT SQL_CALC_FOUND_ROWS id, name from {$wpdb->prefix}amelia_events WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
5144 [ 'approved', $limit, $offset ]
5145 ),
5146 OBJECT
5147 );
5148
5149 $list_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
5150
5151 $options = [];
5152 if ( ! empty( $events ) ) {
5153 foreach ( $events as $event ) {
5154 $options[] = [
5155 'label' => $event->name,
5156 'value' => $event->id,
5157 ];
5158 }
5159 }
5160
5161 return [
5162 'options' => $options,
5163 'hasMore' => $list_count > $limit && $list_count > $offset,
5164 ];
5165
5166 }
5167
5168 /**
5169 * Get last data for trigger.
5170 *
5171 * @param array $data data.
5172 * @return array
5173 */
5174 public function search_amelia_appointment_booked_triggers_last_data( $data ) {
5175 global $wpdb;
5176
5177 $context = [];
5178
5179 $appointment_category = $data['filter']['amelia_category_list']['value'];
5180 $appointment_service = $data['filter']['amelia_service_list']['value'];
5181
5182 if ( -1 === $appointment_service ) {
5183 // If service exists as per category selected.
5184 $service_exist = $wpdb->get_row(
5185 $wpdb->prepare(
5186 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
5187 [ $appointment_category ]
5188 ),
5189 ARRAY_A
5190 );
5191
5192 if ( empty( $service_exist ) ) {
5193 $result = [];
5194 } else {
5195 $result = $wpdb->get_row(
5196 '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 )',
5197 ARRAY_A
5198 );
5199 }
5200 } else {
5201 $result = $wpdb->get_row(
5202 $wpdb->prepare(
5203 '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',
5204 [ $appointment_service ]
5205 ),
5206 ARRAY_A
5207 );
5208 }
5209
5210 if ( ! empty( $result ) ) {
5211
5212 $payment_result = $wpdb->get_row(
5213 $wpdb->prepare(
5214 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
5215 [ $result['id'] ]
5216 ),
5217 ARRAY_A
5218 );
5219
5220 $customer_result = $wpdb->get_row(
5221 $wpdb->prepare(
5222 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
5223 [ $result['customerId'] ]
5224 ),
5225 ARRAY_A
5226 );
5227
5228 $service_result = $wpdb->get_row(
5229 $wpdb->prepare(
5230 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
5231 [ $result['serviceId'] ]
5232 ),
5233 ARRAY_A
5234 );
5235
5236 $category_result = $wpdb->get_row(
5237 $wpdb->prepare(
5238 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
5239 [ $service_result['categoryId'] ]
5240 ),
5241 ARRAY_A
5242 );
5243
5244 if ( $result['couponId'] ) {
5245 $coupon_result = $wpdb->get_row(
5246 $wpdb->prepare(
5247 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
5248 [ $result['couponId'] ]
5249 ),
5250 ARRAY_A
5251 );
5252 } else {
5253 $coupon_result = [];
5254 }
5255
5256 if ( ! empty( $result['customFields'] ) ) {
5257 $custom_fields = json_decode( $result['customFields'], true );
5258
5259 $fields_arr = [];
5260 foreach ( (array) $custom_fields as $fields ) {
5261 if ( is_array( $fields ) ) {
5262 $fields_arr[ $fields['label'] ] = $fields['value'];
5263 }
5264 }
5265 unset( $result['customFields'] );
5266 } else {
5267 $fields_arr = [];
5268 }
5269
5270 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
5271 $context['response_type'] = 'live';
5272 } else {
5273
5274 $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\":\"test\",\"lastName\":\"test\",\"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":"test","lastName":"test","email":"test@test.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 );
5275 }
5276
5277 return $context;
5278 }
5279
5280 /**
5281 * Get last data for trigger.
5282 *
5283 * @param array $data data.
5284 * @return array
5285 */
5286 public function search_amelia_new_event_attendee_triggers_last_data( $data ) {
5287 global $wpdb;
5288
5289 $context = [];
5290
5291 $event_selected = $data['filter']['amelia_events_list']['value'];
5292
5293 if ( -1 === $event_selected ) {
5294 $result = $wpdb->get_row(
5295 'SELECT *
5296 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
5297 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
5298 ON customer.id = event_period.customerBookingId
5299 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods )',
5300 ARRAY_A
5301 );
5302 } else {
5303 $result = $wpdb->get_row(
5304 $wpdb->prepare(
5305 'SELECT *
5306 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
5307 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
5308 ON customer.id = event_period.customerBookingId
5309 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId = %d',
5310 [ $event_selected ]
5311 ),
5312 ARRAY_A
5313 );
5314 }
5315
5316 if ( ! empty( $result ) ) {
5317
5318 $event = $wpdb->get_row(
5319 $wpdb->prepare(
5320 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
5321 [ $result['eventPeriodId'] ]
5322 ),
5323 ARRAY_A
5324 );
5325
5326 $customer_result = $wpdb->get_row(
5327 $wpdb->prepare(
5328 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
5329 [ $result['customerId'] ]
5330 ),
5331 ARRAY_A
5332 );
5333
5334 if ( $result['couponId'] ) {
5335 $coupon_result = $wpdb->get_row(
5336 $wpdb->prepare(
5337 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
5338 [ $result['couponId'] ]
5339 ),
5340 ARRAY_A
5341 );
5342 } else {
5343 $coupon_result = [];
5344 }
5345
5346 if ( ! empty( $result['customFields'] ) ) {
5347 $custom_fields = json_decode( $result['customFields'], true );
5348
5349 $fields_arr = [];
5350 foreach ( (array) $custom_fields as $fields ) {
5351 if ( is_array( $fields ) ) {
5352 $fields_arr[ $fields['label'] ] = $fields['value'];
5353 }
5354 }
5355 unset( $result['customFields'] );
5356 } else {
5357 $fields_arr = [];
5358 }
5359
5360 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result );
5361 $context['response_type'] = 'live';
5362 } else {
5363
5364 $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\":\"test\",\"lastName\":\"test\",\"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":"Testing Event 5","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":"test","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":"test","lastName":"test","email":"test@test.com","birthday":null,"phone":"+213551223123","gender":null,"note":null,"pictureFullPath":null,"pictureThumbPath":null,"password":null,"usedTokens":null,"countryPhoneIso":"dz","timeZone":null}}', true );
5365 }
5366
5367 return $context;
5368 }
5369
5370 /**
5371 * Get last data for trigger.
5372 *
5373 * @param array $data data.
5374 * @return array
5375 */
5376 public function search_amelia_appointment_rescheduled_triggers_last_data( $data ) {
5377 global $wpdb;
5378
5379 $context = [];
5380
5381 $appointment_category = $data['filter']['amelia_category_list']['value'];
5382 $appointment_service = $data['filter']['amelia_service_list']['value'];
5383
5384 if ( -1 === $appointment_service ) {
5385 // If service exists as per category selected.
5386 $service_exist = $wpdb->get_row(
5387 $wpdb->prepare(
5388 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
5389 [ $appointment_category ]
5390 ),
5391 ARRAY_A
5392 );
5393
5394 if ( empty( $service_exist ) ) {
5395 $result = [];
5396 } else {
5397 $result = $wpdb->get_row(
5398 '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 )',
5399 ARRAY_A
5400 );
5401 }
5402 } else {
5403 $result = $wpdb->get_row(
5404 $wpdb->prepare(
5405 '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',
5406 [ $appointment_service ]
5407 ),
5408 ARRAY_A
5409 );
5410 }
5411
5412 if ( ! empty( $result ) ) {
5413
5414 $payment_result = $wpdb->get_row(
5415 $wpdb->prepare(
5416 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
5417 [ $result['id'] ]
5418 ),
5419 ARRAY_A
5420 );
5421
5422 $customer_result = $wpdb->get_row(
5423 $wpdb->prepare(
5424 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
5425 [ $result['customerId'] ]
5426 ),
5427 ARRAY_A
5428 );
5429
5430 $service_result = $wpdb->get_row(
5431 $wpdb->prepare(
5432 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
5433 [ $result['serviceId'] ]
5434 ),
5435 ARRAY_A
5436 );
5437
5438 $category_result = $wpdb->get_row(
5439 $wpdb->prepare(
5440 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
5441 [ $service_result['categoryId'] ]
5442 ),
5443 ARRAY_A
5444 );
5445
5446 if ( $result['couponId'] ) {
5447 $coupon_result = $wpdb->get_row(
5448 $wpdb->prepare(
5449 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
5450 [ $result['couponId'] ]
5451 ),
5452 ARRAY_A
5453 );
5454 } else {
5455 $coupon_result = [];
5456 }
5457
5458 if ( ! empty( $result['customFields'] ) ) {
5459 $custom_fields = json_decode( $result['customFields'], true );
5460
5461 $fields_arr = [];
5462 foreach ( (array) $custom_fields as $fields ) {
5463 if ( is_array( $fields ) ) {
5464 $fields_arr[ $fields['label'] ] = $fields['value'];
5465 }
5466 }
5467 unset( $result['customFields'] );
5468 } else {
5469 $fields_arr = [];
5470 }
5471
5472 $appointment_data['isRescheduled'] = '1';
5473 $context['pluggable_data'] = array_merge( $result, $fields_arr, $appointment_data, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
5474 $context['response_type'] = 'live';
5475 } else {
5476
5477 $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\":\"test\",\"lastName\":\"test\",\"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":"test","lastName":"test","email":"test@test.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 );
5478 }
5479
5480 return $context;
5481 }
5482
5483
5484 /**
5485 * Get last data for trigger.
5486 *
5487 * @param array $data data.
5488 * @return array
5489 */
5490 public function search_amelia_appointment_cancelled_triggers_last_data( $data ) {
5491 global $wpdb;
5492
5493 $context = [];
5494
5495 $appointment_category = $data['filter']['amelia_category_list']['value'];
5496 $appointment_service = $data['filter']['amelia_service_list']['value'];
5497
5498 if ( -1 === $appointment_service ) {
5499 // If service exists as per category selected.
5500 $service_exist = $wpdb->get_row(
5501 $wpdb->prepare(
5502 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
5503 [ $appointment_category ]
5504 ),
5505 ARRAY_A
5506 );
5507
5508 if ( empty( $service_exist ) ) {
5509 $result = [];
5510 } else {
5511 $result = $wpdb->get_row(
5512 $wpdb->prepare(
5513 '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',
5514 [ 'canceled' ]
5515 ),
5516 ARRAY_A
5517 );
5518 }
5519 } else {
5520 $result = $wpdb->get_row(
5521 $wpdb->prepare(
5522 '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',
5523 [ 'canceled', $appointment_service ]
5524 ),
5525 ARRAY_A
5526 );
5527 }
5528
5529 if ( ! empty( $result ) ) {
5530
5531 $payment_result = $wpdb->get_row(
5532 $wpdb->prepare(
5533 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
5534 [ $result['id'] ]
5535 ),
5536 ARRAY_A
5537 );
5538
5539 $customer_result = $wpdb->get_row(
5540 $wpdb->prepare(
5541 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
5542 [ $result['customerId'] ]
5543 ),
5544 ARRAY_A
5545 );
5546
5547 $service_result = $wpdb->get_row(
5548 $wpdb->prepare(
5549 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
5550 [ $result['serviceId'] ]
5551 ),
5552 ARRAY_A
5553 );
5554
5555 $category_result = $wpdb->get_row(
5556 $wpdb->prepare(
5557 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
5558 [ $service_result['categoryId'] ]
5559 ),
5560 ARRAY_A
5561 );
5562
5563 if ( $result['couponId'] ) {
5564 $coupon_result = $wpdb->get_row(
5565 $wpdb->prepare(
5566 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
5567 [ $result['couponId'] ]
5568 ),
5569 ARRAY_A
5570 );
5571 } else {
5572 $coupon_result = [];
5573 }
5574
5575 if ( ! empty( $result['customFields'] ) ) {
5576 $custom_fields = json_decode( $result['customFields'], true );
5577
5578 $fields_arr = [];
5579 foreach ( (array) $custom_fields as $fields ) {
5580 if ( is_array( $fields ) ) {
5581 $fields_arr[ $fields['label'] ] = $fields['value'];
5582 }
5583 }
5584 unset( $result['customFields'] );
5585 } else {
5586 $fields_arr = [];
5587 }
5588
5589 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
5590 $context['response_type'] = 'live';
5591 } else {
5592
5593 $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\":\"test\",\"lastName\":\"test\",\"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":"test","lastName":"test","email":"test@test.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 );
5594 }
5595
5596 return $context;
5597 }
5598
5599 /**
5600 * Get MailPoet Forms.
5601 *
5602 * @param array $data data.
5603 *
5604 * @return array
5605 */
5606 public function search_mailpoet_forms( $data ) {
5607 if ( ! class_exists( '\MailPoet\API\API' ) ) {
5608 return;
5609 }
5610
5611 global $wpdb;
5612
5613 $page = $data['page'];
5614 $limit = Utilities::get_search_page_limit();
5615 $offset = $limit * ( $page - 1 );
5616
5617 $forms = $wpdb->get_results(
5618 $wpdb->prepare(
5619 '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',
5620 [ 'enabled', $limit, $offset ]
5621 )
5622 );
5623
5624 $form_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
5625
5626 $options = [];
5627
5628 if ( ! empty( $forms ) ) {
5629 if ( is_array( $forms ) ) {
5630 foreach ( $forms as $form ) {
5631 $options[] = [
5632 'label' => $form->name,
5633 'value' => $form->id,
5634 ];
5635 }
5636 }
5637 }
5638
5639 return [
5640 'options' => $options,
5641 'hasMore' => $form_count > $limit && $form_count > $offset,
5642 ];
5643
5644 }
5645
5646 /**
5647 * Get MailPoet List.
5648 *
5649 * @param array $data data.
5650 *
5651 * @return array
5652 */
5653 public function search_mailpoet_list( $data ) {
5654 if ( ! class_exists( '\MailPoet\API\API' ) ) {
5655 return;
5656 }
5657
5658 $mailpoet = \MailPoet\API\API::MP( 'v1' );
5659 $lists = $mailpoet->getLists();
5660
5661 $options = [];
5662
5663 if ( ! empty( $lists ) ) {
5664 if ( is_array( $lists ) ) {
5665 foreach ( $lists as $list ) {
5666 $options[] = [
5667 'label' => $list['name'],
5668 'value' => $list['id'],
5669 ];
5670 }
5671 }
5672 }
5673
5674 return [
5675 'options' => $options,
5676 'hasMore' => false,
5677 ];
5678 }
5679
5680 /**
5681 * Get MailPoet Subscriber Status.
5682 *
5683 * @param array $data data.
5684 *
5685 * @return array
5686 */
5687 public function search_mailpoet_subscriber_status( $data ) {
5688 if ( ! class_exists( '\MailPoet\API\API' ) ) {
5689 return;
5690 }
5691
5692 $subscriber_status = [
5693 'subscribed' => 'Subscribed',
5694 'unconfirmed' => 'Unconfirmed',
5695 'unsubscribed' => 'Unsubscribed',
5696 'inactive' => 'Inactive',
5697 'bounced' => 'Bounced',
5698 ];
5699
5700 $options = [];
5701 foreach ( $subscriber_status as $key => $status ) {
5702 $options[] = [
5703 'label' => $status,
5704 'value' => $key,
5705 ];
5706 }
5707
5708 return [
5709 'options' => $options,
5710 'hasMore' => false,
5711 ];
5712 }
5713
5714 /**
5715 * Get MailPoet Subscribers.
5716 *
5717 * @param array $data data.
5718 *
5719 * @return array
5720 */
5721 public function search_mailpoet_subscribers( $data ) {
5722 if ( ! class_exists( '\MailPoet\API\API' ) ) {
5723 return;
5724 }
5725
5726 global $wpdb;
5727
5728 $page = $data['page'];
5729 $limit = Utilities::get_search_page_limit();
5730 $offset = $limit * ( $page - 1 );
5731
5732 $subscribers = $wpdb->get_results(
5733 $wpdb->prepare(
5734 'SELECT SQL_CALC_FOUND_ROWS id,email FROM ' . $wpdb->prefix . 'mailpoet_subscribers ORDER BY id DESC LIMIT %d OFFSET %d',
5735 [ $limit, $offset ]
5736 )
5737 );
5738
5739 $subscribers_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
5740
5741 $options = [];
5742
5743 if ( ! empty( $subscribers ) ) {
5744 if ( is_array( $subscribers ) ) {
5745 foreach ( $subscribers as $subscriber ) {
5746 $options[] = [
5747 'label' => $subscriber->email,
5748 'value' => $subscriber->id,
5749 ];
5750 }
5751 }
5752 }
5753
5754 return [
5755 'options' => $options,
5756 'hasMore' => $subscribers_count > $limit && $subscribers_count > $offset,
5757 ];
5758 }
5759
5760 /**
5761 * Get ConvertPro Forms.
5762 *
5763 * @param array $data data.
5764 *
5765 * @return array
5766 */
5767 public function search_convertpro_form_list( $data ) {
5768 if ( ! class_exists( '\Cp_V2_Loader' ) ) {
5769 return;
5770 }
5771
5772 $cp_popups_inst = CP_V2_Popups::get_instance();
5773 $popups = $cp_popups_inst->get_all();
5774
5775 $form_count = count( $popups );
5776
5777 $page = $data['page'];
5778 $limit = Utilities::get_search_page_limit();
5779 $offset = $limit * ( $page - 1 );
5780
5781 $options = [];
5782
5783 if ( ! empty( $popups ) ) {
5784 if ( is_array( $popups ) ) {
5785 foreach ( $popups as $form ) {
5786 $options[] = [
5787 'label' => $form->post_title,
5788 'value' => $form->ID,
5789 ];
5790 }
5791 }
5792 }
5793
5794 return [
5795 'options' => $options,
5796 'hasMore' => $form_count > $limit && $form_count > $offset,
5797 ];
5798
5799 }
5800
5801 /**
5802 * Get ProjectHuddle Websites.
5803 *
5804 * @param array $data data.
5805 *
5806 * @return array
5807 */
5808 public function search_project_huddle_websites( $data ) {
5809
5810 $sites = new WP_Query(
5811 [
5812 'post_type' => 'ph-website',
5813 'posts_per_page' => - 1,
5814 'fields' => 'ids',
5815 ]
5816 );
5817
5818 $site_ids = (array) $sites->posts;
5819
5820 $options = [];
5821 if ( ! empty( $site_ids ) ) {
5822 if ( is_array( $site_ids ) ) {
5823 foreach ( $site_ids as $site_id ) {
5824 $options[] = [
5825 'label' => get_the_title( $site_id ),
5826 'value' => $site_id,
5827 ];
5828 }
5829 }
5830 }
5831 return [
5832 'options' => $options,
5833 'hasMore' => false,
5834 ];
5835
5836 }
5837
5838 /**
5839 * Get last data for trigger.
5840 *
5841 * @param array $data data.
5842 * @return mixed
5843 */
5844 public function search_project_huddle_comment_triggers_last_data( $data ) {
5845 global $wpdb;
5846
5847 $context = [];
5848
5849 if ( -1 !== $data['dynamic'] ) {
5850 $threads = get_posts(
5851 [
5852 'post_type' => 'phw_comment_loc',
5853 'posts_per_page' => 1,
5854 'meta_value' => $data['dynamic'],
5855 'meta_key' => 'project_id',
5856 'orderby' => 'asc',
5857 ]
5858 );
5859 } else {
5860 $threads = [];
5861 }
5862
5863 if ( ! empty( $threads ) ) {
5864 $comment_result = $wpdb->get_row(
5865 $wpdb->prepare(
5866 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
5867 FROM ' . $wpdb->prefix . 'comments
5868 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
5869 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
5870 LIMIT 0,1',
5871 $threads[0]->ID
5872 ),
5873 ARRAY_A
5874 );
5875
5876 if ( ! empty( $comment_result ) ) {
5877 $comment_id = get_comment( $comment_result['comment_ID'], ARRAY_A );
5878 $comments['comment_ID'] = isset( $comment_id['comment_ID'] ) ? $comment_id['comment_ID'] : '';
5879 $comments['comment_post_ID'] = isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : '';
5880
5881 $comments['comment_author'] = isset( $comment_id['comment_author'] ) ? $comment_id['comment_author'] : '';
5882
5883 $comments['comment_author_email'] = isset( $comment_id['comment_author_email'] ) ? $comment_id['comment_author_email'] : '';
5884
5885 $comments['comment_date'] = isset( $comment_id['comment_date'] ) ? $comment_id['comment_date'] : '';
5886
5887 $comments['comment_content'] = isset( $comment_id['comment_content'] ) ? $comment_id['comment_content'] : '';
5888
5889 $comments['comment_type'] = isset( $comment_id['comment_type'] ) ? $comment_id['comment_type'] : '';
5890
5891 $context['pluggable_data'] = $comments;
5892 $context['response_type'] = 'live';
5893 } else {
5894 $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 );
5895 }
5896 } else {
5897 $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 );
5898 }
5899
5900 return $context;
5901 }
5902
5903 /**
5904 * Get last data for trigger.
5905 *
5906 * @param array $data data.
5907 * @return mixed
5908 */
5909 public function search_project_huddle_resolved_comment_triggers_last_data( $data ) {
5910 global $wpdb;
5911
5912 $context = [];
5913
5914 $get_comments = $wpdb->get_row(
5915 'SELECT ' . $wpdb->prefix . 'comments.comment_ID, ' . $wpdb->prefix . 'comments.comment_content
5916 FROM ' . $wpdb->prefix . 'comments
5917 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_status") AND comment_content = "Resolved"
5918 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
5919 LIMIT 0,1'
5920 );
5921
5922 if ( ! empty( $get_comments ) ) {
5923 $comment_id = get_comment( $get_comments->comment_ID, ARRAY_A );
5924 $comment_result = $wpdb->get_row(
5925 $wpdb->prepare(
5926 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
5927 FROM ' . $wpdb->prefix . 'comments
5928 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
5929 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
5930 LIMIT 0,1',
5931 isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : ''
5932 ),
5933 ARRAY_A
5934 );
5935
5936 $actual_comment = get_comment( $comment_result['comment_ID'], ARRAY_A );
5937 $comments['comment_ID'] = isset( $actual_comment['comment_ID'] ) ? $actual_comment['comment_ID'] : '';
5938 $comments['comment_post_ID'] = isset( $actual_comment['comment_post_ID'] ) ? $actual_comment['comment_post_ID'] : '';
5939 $comments['comment_author'] = isset( $actual_comment['comment_author'] ) ? $actual_comment['comment_author'] : '';
5940 $comments['comment_author_email'] = isset( $actual_comment['comment_author_email'] ) ? $actual_comment['comment_author_email'] : '';
5941 $comments['comment_date'] = isset( $actual_comment['comment_date'] ) ? $actual_comment['comment_date'] : '';
5942 $comments['comment_content'] = isset( $actual_comment['comment_content'] ) ? $actual_comment['comment_content'] : '';
5943 $comments['comment_type'] = isset( $actual_comment['comment_type'] ) ? $actual_comment['comment_type'] : '';
5944 $comments['comment_status'] = $get_comments->comment_content;
5945 $context['pluggable_data'] = $comments;
5946 $context['response_type'] = 'live';
5947 } else {
5948 $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"}}', true );
5949 }
5950
5951 return $context;
5952 }
5953
5954 /**
5955 * Get MasterStudy LMS Courses.
5956 *
5957 * @param array $data data.
5958 *
5959 * @return array
5960 */
5961 public function search_ms_lms_courses( $data ) {
5962
5963 $page = $data['page'];
5964 $limit = Utilities::get_search_page_limit();
5965 $offset = $limit * ( $page - 1 );
5966
5967 $args = [
5968 'post_type' => 'stm-courses',
5969 'posts_per_page' => $limit,
5970 'offset' => $offset,
5971 'orderby' => 'title',
5972 'order' => 'ASC',
5973 'post_status' => 'publish',
5974 ];
5975
5976 $courses = get_posts( $args );
5977
5978 $course_count = count( $courses );
5979
5980 $options = [];
5981 if ( ! empty( $courses ) ) {
5982 if ( is_array( $courses ) ) {
5983 foreach ( $courses as $course ) {
5984 $options[] = [
5985 'label' => $course->post_title,
5986 'value' => $course->ID,
5987 ];
5988 }
5989 }
5990 }
5991 return [
5992 'options' => $options,
5993 'hasMore' => $course_count > $limit && $course_count > $offset,
5994 ];
5995
5996 }
5997
5998 /**
5999 * Get MasterStudy LMS Lessons.
6000 *
6001 * @param array $data data.
6002 *
6003 * @return array
6004 */
6005 public function search_ms_lms_lessons( $data ) {
6006
6007 global $wpdb;
6008 $page = $data['page'];
6009 $limit = Utilities::get_search_page_limit();
6010 $offset = $limit * ( $page - 1 );
6011
6012 $course_id = $data['dynamic'];
6013
6014 $lessons = $wpdb->get_results(
6015 $wpdb->prepare(
6016 "SELECT ID, post_title
6017 FROM $wpdb->posts
6018 WHERE FIND_IN_SET(
6019 ID,
6020 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
6021 )
6022 AND post_type = 'stm-lessons'
6023 ORDER BY post_title ASC",
6024 absint( $course_id )
6025 )
6026 );
6027
6028 if ( '-1' === $course_id ) {
6029 $lessons = $wpdb->get_results(
6030 $wpdb->prepare(
6031 "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = %s ORDER BY post_title ASC",
6032 'stm-lessons'
6033 )
6034 );
6035 }
6036
6037 $lessons_count = count( $lessons );
6038
6039 $options = [];
6040 if ( ! empty( $lessons ) ) {
6041 if ( is_array( $lessons ) ) {
6042 foreach ( $lessons as $lesson ) {
6043 $options[] = [
6044 'label' => $lesson->post_title,
6045 'value' => $lesson->ID,
6046 ];
6047 }
6048 }
6049 }
6050 return [
6051 'options' => $options,
6052 'hasMore' => $lessons_count > $limit && $lessons_count > $offset,
6053 ];
6054
6055 }
6056
6057 /**
6058 * Get MasterStudy LMS Quiz.
6059 *
6060 * @param array $data data.
6061 *
6062 * @return array
6063 */
6064 public function search_ms_lms_quiz( $data ) {
6065
6066 global $wpdb;
6067 $page = $data['page'];
6068 $limit = Utilities::get_search_page_limit();
6069 $offset = $limit * ( $page - 1 );
6070
6071 $course_id = $data['dynamic'];
6072
6073 $quizzes = $wpdb->get_results(
6074 $wpdb->prepare(
6075 "SELECT ID, post_title
6076 FROM $wpdb->posts
6077 WHERE FIND_IN_SET(
6078 ID,
6079 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
6080 )
6081 AND post_type = 'stm-quizzes'
6082 ORDER BY post_title ASC
6083 ",
6084 absint( $course_id )
6085 )
6086 );
6087
6088 if ( '-1' === $course_id ) {
6089 $quizzes = $wpdb->get_results(
6090 $wpdb->prepare(
6091 "SELECT ID, post_title
6092 FROM $wpdb->posts
6093 WHERE post_type = %s
6094 ORDER BY post_title ASC",
6095 'stm-quizzes'
6096 )
6097 );
6098 }
6099
6100 $quizzes_count = count( $quizzes );
6101
6102 $options = [];
6103 if ( ! empty( $quizzes ) ) {
6104 if ( is_array( $quizzes ) ) {
6105 foreach ( $quizzes as $quiz ) {
6106 $options[] = [
6107 'label' => $quiz->post_title,
6108 'value' => $quiz->ID,
6109 ];
6110 }
6111 }
6112 }
6113 return [
6114 'options' => $options,
6115 'hasMore' => $quizzes_count > $limit && $quizzes_count > $offset,
6116 ];
6117
6118 }
6119
6120 /**
6121 * Search MasterStudy LMS data.
6122 *
6123 * @param array $data data.
6124 * @return array|void
6125 */
6126 public function search_ms_lms_last_data( $data ) {
6127 global $wpdb;
6128 $post_type = $data['post_type'];
6129 $trigger = $data['search_term'];
6130 $context = [];
6131
6132 if ( 'stm_lms_course_completed' === $trigger ) {
6133 $post_id = $data['filter']['course']['value'];
6134 if ( -1 === $post_id ) {
6135 $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 ) );
6136 } else {
6137 $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 ) );
6138 }
6139 } elseif ( 'stm_lesson_passed' === $trigger ) {
6140 $post_id = $data['filter']['lesson']['value'];
6141 if ( -1 === $post_id ) {
6142 $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 ) );
6143 } else {
6144 $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 ) );
6145 }
6146 } elseif ( 'stm_quiz_passed' === $trigger ) {
6147 $post_id = $data['filter']['quiz']['value'];
6148 if ( -1 === $post_id ) {
6149 $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 ) );
6150 } else {
6151 $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 ) );
6152 }
6153 } elseif ( 'stm_quiz_failed' === $trigger ) {
6154 $post_id = $data['filter']['quiz']['value'];
6155 if ( -1 === $post_id ) {
6156 $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 ) );
6157 } else {
6158 $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 ) );
6159 }
6160 } elseif ( 'stm_lms_user_enroll_course' === $trigger ) {
6161 $post_id = $data['filter']['course']['value'];
6162 if ( -1 === $post_id ) {
6163 $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 ) );
6164 } else {
6165 $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 ) );
6166 }
6167 }
6168
6169 if ( ! empty( $result ) ) {
6170
6171 switch ( $trigger ) {
6172 case 'stm_lms_course_completed':
6173 $result_course_id = $result[0]->course_id;
6174 $result_user_id = $result[0]->user_id;
6175 $course = get_the_title( $result_course_id );
6176 $course_link = get_the_permalink( $result_course_id );
6177 $featured_image = get_the_post_thumbnail_url( $result_course_id );
6178
6179 $data = [
6180 'course_id' => $result_course_id,
6181 'course_title' => $course,
6182 'course_link' => $course_link,
6183 'course_featured_image' => $featured_image,
6184 'course_progress' => $result[0]->progress_percent,
6185 ];
6186 $context_data = array_merge(
6187 WordPress::get_user_context( $result_user_id ),
6188 $data
6189 );
6190 break;
6191 case 'stm_lesson_passed':
6192 $result_lesson_id = $result[0]->lesson_id;
6193 $result_user_id = $result[0]->user_id;
6194 $lesson = get_the_title( $result_lesson_id );
6195 $lesson_link = get_the_permalink( $result_lesson_id );
6196
6197 $data = [
6198 'lesson_id' => $result_lesson_id,
6199 'lesson_title' => $lesson,
6200 'lesson_link' => $lesson_link,
6201 ];
6202 $context_data = array_merge(
6203 WordPress::get_user_context( $result_user_id ),
6204 $data
6205 );
6206 break;
6207 case 'stm_quiz_passed':
6208 $result_quiz_id = $result[0]->quiz_id;
6209 $result_user_id = $result[0]->user_id;
6210 $quiz_title = get_the_title( $result_quiz_id );
6211 $quiz_link = get_the_permalink( $result_quiz_id );
6212
6213 $data = [
6214 'quiz_id' => $result_quiz_id,
6215 'quiz_title' => $quiz_title,
6216 'quiz_link' => $quiz_link,
6217 'quiz_score' => $result[0]->progress,
6218 'result' => 'passed',
6219 ];
6220 $context_data = array_merge(
6221 WordPress::get_user_context( $result_user_id ),
6222 $data
6223 );
6224 break;
6225 case 'stm_quiz_failed':
6226 $result_quiz_id = $result[0]->quiz_id;
6227 $result_user_id = $result[0]->user_id;
6228 $quiz_title = get_the_title( $result_quiz_id );
6229 $quiz_link = get_the_permalink( $result_quiz_id );
6230
6231 $data = [
6232 'quiz_id' => $result_quiz_id,
6233 'quiz_title' => $quiz_title,
6234 'quiz_link' => $quiz_link,
6235 'quiz_score' => $result[0]->progress,
6236 'result' => 'failed',
6237 ];
6238 $context_data = array_merge(
6239 WordPress::get_user_context( $result_user_id ),
6240 $data
6241 );
6242 break;
6243 case 'stm_lms_user_enroll_course':
6244 $result_course_id = $result[0]->course_id;
6245 $result_user_id = $result[0]->user_id;
6246
6247 $course = get_the_title( $result_course_id );
6248 $course_link = get_the_permalink( $result_course_id );
6249 $featured_image = get_the_post_thumbnail_url( $result_course_id );
6250
6251 $data = [
6252 'course_id' => $result_course_id,
6253 'course_title' => $course,
6254 'course_link' => $course_link,
6255 'course_featured_image' => $featured_image,
6256 ];
6257 $context_data = array_merge(
6258 WordPress::get_user_context( $result_user_id ),
6259 $data
6260 );
6261 break;
6262 default:
6263 return;
6264 }
6265 $context['pluggable_data'] = $context_data;
6266 $context['response_type'] = 'live';
6267 }
6268
6269 return $context;
6270
6271 }
6272
6273
6274 /**
6275 * Get last data for trigger.
6276 *
6277 * @param array $data data.
6278 * @return array
6279 */
6280 public function search_fluent_support_triggers_last_data( $data ) {
6281 $context = [];
6282 $context['response_type'] = 'sample';
6283
6284 $ticket_data = [
6285 'id' => '1',
6286 'customer_id' => '2',
6287 'agent_id' => '3',
6288 'product_id' => '5',
6289 'product_source' => 'local',
6290 'privacy' => 'private',
6291 'priority' => 'normal',
6292 'client_priority' => 'medium',
6293 'status' => 'active',
6294 'title' => 'Sample Ticket Title',
6295 'slug' => 'sample-ticket-title',
6296 'hash' => 'f8a8cfb946',
6297 'content_hash' => 'd65500d62621be8b493c22b1d888052c',
6298 'content' => '<p>Sample content.</p>',
6299 'last_customer_response' => '2023-04-27 07:30:46',
6300 'waiting_since' => '2023-04-27 07:30:46',
6301 'response_count' => '2',
6302 'total_close_time' => '7042',
6303 'resolved at' => '2023-04-27 09:28:08',
6304 'closed_by' => '1',
6305 'created_at' => '2023-04-27 07:30:46',
6306 'updated_at' => '2023-04-27 10:28:08',
6307 'mailbox_id' => '1',
6308 'mailbox_name' => 'SureTriggers',
6309 'mailbox_slug' => 'suretriggers',
6310 'mailbox_box_type' => 'web',
6311 'mailbox email' => 'john_doe@sample.com',
6312 'mailbox_settings_admin_email_address' => 'john_doe@sample.com',
6313 'mailbox_created_by' => '1',
6314 'mailbox_is_default' => 'yes',
6315 'mailbox_created_at' => '2023-04-26 06:29:01',
6316 'mailbox_updated_at' => '2023-04-26 06:29:01',
6317 ];
6318
6319 $customer_data = [
6320 'id' => '1',
6321 'first_name' => 'John',
6322 'last_name' => 'Doe',
6323 'email' => 'john_doe@sample.com',
6324 'person_type' => 'agent',
6325 'status' => 'active',
6326 'hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
6327 'user_id' => '1',
6328 'created_at' => '2023-04-27 07:30:46',
6329 'updated_at' => '2023-04-27 10:28:08',
6330 'full_name' => 'John Doe',
6331 'photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
6332 ];
6333
6334 $reply_data = [
6335 'ticket_id' => '1',
6336 'conversation_type' => 'response',
6337 'content' => '<p>Sample content.</p>',
6338 'source' => 'web',
6339 'content_hash' => '2cc0e35d8fb92a0675d67999b073b3a4',
6340 'created_at' => '2023-04-27 07:30:46',
6341 'updated_at' => '2023-04-27 10:28:08',
6342 'id' => '1',
6343 'person_id' => '2',
6344 'person_first_name' => 'John',
6345 'person_last_name' => 'Doe',
6346 'person_email' => 'john_doe@sample.com',
6347 'person_person_type' => 'agent',
6348 'person_status' => 'active',
6349 'person_hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
6350 'person_user_id' => '1',
6351 'person_created_at' => '2023-04-27 07:30:46',
6352 'person_updated_at' => '2023-04-27 10:28:08',
6353 'person_full_name' => 'John Doe',
6354 'person_photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
6355 ];
6356
6357 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
6358
6359 if ( in_array( $term, [ 'response_added_by_agent', 'response_added_by_customer' ], true ) ) {
6360 $context['pluggable_data'] = array_merge(
6361 [
6362 'reply' => $reply_data,
6363 'ticket' => $ticket_data,
6364 'customer' => $customer_data,
6365 ]
6366 );
6367 } else {
6368 $context['pluggable_data'] = array_merge(
6369 [
6370 'ticket' => $ticket_data,
6371 'customer' => $customer_data,
6372 ]
6373 );
6374 }
6375
6376 return $context;
6377 }
6378
6379 /**
6380 * Prepare Ultimate Member user_roles.
6381 *
6382 * @param array $data data.
6383 *
6384 * @return array
6385 */
6386 public function search_um_user_roles( $data ) {
6387 if ( function_exists( 'get_editable_roles' ) ) {
6388 $roles = get_editable_roles();
6389 } else {
6390 $roles = wp_roles()->roles;
6391 $roles = apply_filters( 'editable_roles', $roles );
6392 }
6393
6394 $options = [];
6395 foreach ( $roles as $role => $details ) {
6396
6397 $options[] = [
6398 'label' => $details['name'],
6399 'value' => $role,
6400 ];
6401
6402 }
6403
6404 return [
6405 'options' => $options,
6406 'hasMore' => false,
6407 ];
6408 }
6409
6410 /**
6411 * Prepare Ultimate Member forms_list.
6412 *
6413 * @param array $data data.
6414 *
6415 * @return array
6416 */
6417 public function search_um_forms_list( $data ) {
6418
6419 $page = $data['page'];
6420 $limit = Utilities::get_search_page_limit();
6421 $offset = $limit * ( $page - 1 );
6422
6423 $args = [
6424 'posts_per_page' => $limit,
6425 'offset' => $offset,
6426 'orderby' => 'title',
6427 'order' => 'ASC',
6428 'post_type' => 'um_form',
6429 'post_status' => 'publish',
6430 'fields' => 'ids',
6431 ];
6432
6433 $forms_list = get_posts( $args );
6434
6435 $forms_list_count = count( $forms_list );
6436
6437 $options = [];
6438 if ( ! empty( $forms_list ) ) {
6439 foreach ( $forms_list as $form ) {
6440 $options[] = [
6441 'label' => get_the_title( $form ),
6442 'value' => $form,
6443 ];
6444 }
6445 }
6446
6447 return [
6448 'options' => $options,
6449 'hasMore' => $forms_list_count > $limit && $forms_list_count > $offset,
6450 ];
6451 }
6452
6453 /**
6454 * Get last data for Ultimate Member Login trigger.
6455 *
6456 * @param array $data data.
6457 * @return mixed
6458 */
6459 public function search_ultimate_member_user_logsin( $data ) {
6460 $context = [];
6461 $args = [
6462 'orderby' => 'meta_value',
6463 'meta_key' => '_um_last_login',
6464 'order' => 'DESC',
6465 'number' => 1,
6466 ];
6467 $users = get_users( $args );
6468
6469 if ( ! empty( $users ) ) {
6470 $user = $users[0];
6471 $pluggable_data = WordPress::get_user_context( $user->ID );
6472 $context['pluggable_data'] = $pluggable_data;
6473 $context['response_type'] = 'live';
6474 } else {
6475 $role = 'subscriber';
6476 $context['pluggable_data'] = [
6477 'wp_user_id' => 1,
6478 'user_login' => 'test',
6479 'display_name' => 'Test User',
6480 'user_firstname' => 'Test',
6481 'user_lastname' => 'User',
6482 'user_email' => 'testuser@gmail.com',
6483 'user_role' => [ $role ],
6484 ];
6485 $context['response_type'] = 'sample';
6486 }
6487 return $context;
6488 }
6489
6490 /**
6491 * Get last data for Ultimate Member Register trigger.
6492 *
6493 * @param array $data data.
6494 * @return mixed
6495 */
6496 public function search_ultimate_member_user_registers( $data ) {
6497 $context = [];
6498 $args = [
6499 'orderby' => 'meta_value',
6500 'meta_key' => 'um_user_profile_url_slug_user_login',
6501 'order' => 'DESC',
6502 'number' => 1,
6503 ];
6504 $users = get_users( $args );
6505
6506 if ( ! empty( $users ) ) {
6507 $user = $users[0];
6508 $pluggable_data = WordPress::get_user_context( $user->ID );
6509 $context['pluggable_data'] = $pluggable_data;
6510 $context['response_type'] = 'live';
6511 } else {
6512 $role = 'subscriber';
6513 $context['pluggable_data'] = [
6514 'wp_user_id' => 1,
6515 'user_login' => 'test',
6516 'display_name' => 'Test User',
6517 'user_firstname' => 'Test',
6518 'user_lastname' => 'User',
6519 'user_email' => 'testuser@gmail.com',
6520 'user_role' => [ $role ],
6521 ];
6522 $context['response_type'] = 'sample';
6523 }
6524 return $context;
6525 }
6526
6527 /**
6528 * Get last data for Ultimate Member Register trigger.
6529 *
6530 * @param array $data data.
6531 * @return mixed
6532 */
6533 public function search_ultimate_member_user_inactive( $data ) {
6534 $context = [];
6535 $args = [
6536 'orderby' => 'user_id',
6537 'meta_key' => 'account_status',
6538 'meta_value' => 'inactive',
6539 'order' => 'ASC',
6540 'number' => 1,
6541 ];
6542 $users = get_users( $args );
6543
6544 if ( ! empty( $users ) ) {
6545 $user = $users[0];
6546 $pluggable_data = [];
6547 $pluggable_data[] = WordPress::get_user_context( $user->ID );
6548 $pluggable_data['user_account_status'] = 'inactive';
6549 $context['pluggable_data'] = $pluggable_data;
6550 $context['response_type'] = 'live';
6551 } else {
6552 $role = 'subscriber';
6553 $context['pluggable_data'] = [
6554 'wp_user_id' => 1,
6555 'user_login' => 'test',
6556 'display_name' => 'Test User',
6557 'user_firstname' => 'Test',
6558 'user_lastname' => 'User',
6559 'user_email' => 'testuser@gmail.com',
6560 'user_role' => [ $role ],
6561 'user_account_status' => 'inactive',
6562 ];
6563 $context['response_type'] = 'sample';
6564 }
6565 return $context;
6566 }
6567
6568 /**
6569 * Get last data for Ultimate Member Change Role trigger.
6570 *
6571 * @param array $data data.
6572 * @return mixed
6573 */
6574 public function search_ultimate_member_user_role_change( $data ) {
6575 $context = [];
6576
6577 $role = $data['filter']['role']['value'];
6578
6579 $args = [
6580 'number' => 1,
6581 'role' => $role,
6582 ];
6583 $users = get_users( $args );
6584 shuffle( $users );
6585 if ( ! empty( $users ) ) {
6586 $user = $users[0];
6587 $pluggable_data = WordPress::get_user_context( $user->ID );
6588 $context['pluggable_data'] = $pluggable_data;
6589 $context['response_type'] = 'live';
6590 } else {
6591 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
6592 $context['pluggable_data'] = [
6593 'wp_user_id' => 1,
6594 'user_login' => 'test',
6595 'display_name' => 'Test User',
6596 'user_firstname' => 'Test',
6597 'user_lastname' => 'User',
6598 'user_email' => 'testuser@gmail.com',
6599 'user_role' => [ $role ],
6600 'user_account_status' => 'inactive',
6601 ];
6602 $context['response_type'] = 'sample';
6603 }
6604 return $context;
6605 }
6606
6607 /**
6608 * Get JetEngine WP Posttypes.
6609 *
6610 * @param array $data data.
6611 *
6612 * @return array
6613 */
6614 public function search_je_posttype_list( $data ) {
6615
6616 $post_types = get_post_types( [ 'public' => true ], 'object' );
6617 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
6618 if ( isset( $post_types['attachment'] ) ) {
6619 unset( $post_types['attachment'] );
6620 }
6621
6622 $options = [];
6623 foreach ( $post_types as $post_type => $details ) {
6624 $options[] = [
6625 'label' => $details->label,
6626 'value' => $post_type,
6627 ];
6628 }
6629
6630 return [
6631 'options' => $options,
6632 'hasMore' => false,
6633 ];
6634 }
6635
6636 /**
6637 * Get JetEngine WP fields.
6638 *
6639 * @param array $data data.
6640 *
6641 * @return array
6642 */
6643 public function search_je_field_list( $data ) {
6644
6645 $post_type = $data['dynamic'];
6646
6647 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
6648
6649 $post_fields = array_filter(
6650 $metaboxes,
6651 function( $metabox ) {
6652 /**
6653 *
6654 * Ignore line
6655 *
6656 * @phpstan-ignore-next-line
6657 */
6658 return 'post' === $metabox['args']['object_type'];
6659 }
6660 );
6661
6662 $post_fields_count = count( $post_fields );
6663
6664 $options = [];
6665 if ( ! empty( $post_fields ) ) {
6666 if ( is_array( $post_fields ) ) {
6667 foreach ( $post_fields as $post_field ) {
6668 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
6669 foreach ( $post_field['meta_fields'] as $meta_field ) {
6670 $options[] = [
6671 'label' => $meta_field['title'],
6672 'value' => $meta_field['name'],
6673 ];
6674 }
6675 }
6676 }
6677 }
6678 }
6679 return [
6680 'options' => $options,
6681 'hasMore' => false,
6682 ];
6683 }
6684
6685 /**
6686 * Search Last Updated Field Data.
6687 *
6688 * @param array $data data.
6689 * @return array
6690 */
6691 public function search_jet_engine_field_data( $data ) {
6692 global $wpdb;
6693
6694 $context = [];
6695
6696 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
6697
6698 $post_type = $data['filter']['wp_post_type']['value'];
6699
6700 if ( -1 === $field ) {
6701 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
6702
6703 $post_fields = array_filter(
6704 $metaboxes,
6705 function( $metabox ) {
6706 /**
6707 *
6708 * Ignore line
6709 *
6710 * @phpstan-ignore-next-line
6711 */
6712 return 'post' === $metabox['args']['object_type'];
6713 }
6714 );
6715
6716 $options = [];
6717 if ( ! empty( $post_fields ) ) {
6718 if ( is_array( $post_fields ) ) {
6719 foreach ( $post_fields as $post_field ) {
6720 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
6721 foreach ( $post_field['meta_fields'] as $meta_field ) {
6722 $options[] = $meta_field['name'];
6723 }
6724 }
6725 }
6726 }
6727 }
6728 $random_key = array_rand( $options );
6729 $random_value = $options[ $random_key ];
6730 $string = '%' . $random_value . '%';
6731 } else {
6732 $string = '%' . $data['filter']['field_id']['value'] . '%';
6733 }
6734
6735 $result = $wpdb->get_results(
6736 $wpdb->prepare(
6737 'SELECT post_id FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key LIKE %s',
6738 [ $string ]
6739 ),
6740 ARRAY_A
6741 );
6742
6743 $ids = [];
6744
6745 if ( ! empty( $result ) ) {
6746 foreach ( $result as $val ) {
6747 $ids[] = $val['post_id'];
6748 }
6749 }
6750
6751 $lastupdated_args = [
6752 'post_type' => $post_type,
6753 'orderby' => 'modified',
6754 'post__in' => $ids,
6755 'posts_per_page' => 1,
6756 ];
6757 $lastupdated_loop = get_posts( $lastupdated_args );
6758
6759 $response = [];
6760 if ( ! empty( $result ) ) {
6761 $context['post'] = $lastupdated_loop[0];
6762
6763 $meta_value = get_post_meta( $lastupdated_loop[0]->ID, sprintf( '%s', $data['filter']['field_id']['value'] ), true );
6764 $meta_key = sprintf( '%s', $data['filter']['field_id']['value'] );
6765
6766 $context[ $meta_key ] = $meta_value;
6767
6768 $response['pluggable_data'] = $context;
6769 $response['response_type'] = 'live';
6770 } else {
6771 $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 );
6772 }
6773
6774 return $response;
6775
6776 }
6777
6778 /**
6779 * Get Formidable Forms.
6780 *
6781 * @param array $data data.
6782 *
6783 * @return array|void
6784 */
6785 public function search_formidable_form_list( $data ) {
6786 if ( ! class_exists( 'FrmForm' ) ) {
6787 return;
6788 }
6789
6790 $page = $data['page'];
6791 $term = $data['search_term'];
6792 $limit = Utilities::get_search_page_limit();
6793 $offset = $limit * ( $page - 1 );
6794 $per_page = 10;
6795
6796 $query = [
6797 [
6798 'or' => 1,
6799 'name LIKE' => $term,
6800 'parent_form_id' => null,
6801 ],
6802 ];
6803 $query['is_template'] = 0;
6804 $query['status !'] = 'trash';
6805 $forms_list = FrmForm::getAll( $query, '', $offset . ',' . $per_page );
6806 $form_count = FrmForm::getAll( $query );
6807 $form_count = count( $form_count );
6808 $options = [];
6809
6810 if ( ! empty( $forms_list ) ) {
6811 if ( is_array( $forms_list ) ) {
6812 foreach ( $forms_list as $form ) {
6813 $options[] = [
6814 'label' => $form->name,
6815 'value' => $form->id,
6816 ];
6817 }
6818 }
6819 }
6820
6821 return [
6822 'options' => $options,
6823 'hasMore' => $form_count > $limit && $form_count > $offset,
6824 ];
6825 }
6826
6827 /**
6828 * Get JetFormBuilder Form List.
6829 *
6830 * @param array $data data.
6831 *
6832 * @return array|void
6833 */
6834 public function search_jetform_list( $data ) {
6835 if ( ! class_exists( '\Jet_Form_Builder\Classes\Tools' ) ) {
6836 return;
6837 }
6838
6839 $forms = \Jet_Form_Builder\Classes\Tools::get_forms_list_for_js();
6840
6841 $options = [];
6842 foreach ( $forms as $form ) {
6843
6844 if ( ! empty( $form['value'] ) ) {
6845 $options[] = [
6846 'label' => esc_html( $form['label'] ),
6847 'value' => esc_attr( $form['value'] ),
6848 ];
6849 }
6850 }
6851
6852 return [
6853 'options' => $options,
6854 'hasMore' => false,
6855 ];
6856 }
6857
6858 /**
6859 * Get Forminator Form List.
6860 *
6861 * @param array $data data.
6862 *
6863 * @return array|void
6864 */
6865 public function search_forminator_form_list( $data ) {
6866 if ( ! class_exists( 'Forminator_API' ) ) {
6867 return;
6868 }
6869
6870 $forms = Forminator_API::get_forms( null, 1, 10 );
6871
6872 $options = [];
6873 foreach ( $forms as $form ) {
6874 $options[] = [
6875 'label' => isset( $form->settings ) && isset( $form->settings['form_name'] ) ? $form->settings['form_name'] : $form->name,
6876 'value' => $form->id,
6877 ];
6878 }
6879
6880 return [
6881 'options' => $options,
6882 'hasMore' => false,
6883 ];
6884 }
6885
6886
6887 /**
6888 * Get BbPress topics list.
6889 *
6890 * @param array $data data.
6891 *
6892 * @return array|void
6893 */
6894 public function search_bbp_topic_list( $data ) {
6895 $page = $data['page'];
6896 $forum_id = $data['dynamic'];
6897 $limit = Utilities::get_search_page_limit();
6898 $offset = $limit * ( $page - 1 );
6899 $args = [
6900 'post_type' => 'topic',
6901 'offset' => $offset,
6902 'meta_query' => [
6903 [
6904 'key' => '_bbp_forum_id',
6905 'value' => $forum_id,
6906 'compare' => '==',
6907 ],
6908 ],
6909 ];
6910
6911 $topics = get_posts( $args );
6912 $topics_count = count( $topics );
6913
6914 $options = [];
6915 if ( ! empty( $topics ) ) {
6916 if ( is_array( $topics ) ) {
6917 foreach ( $topics as $topic ) {
6918 $options[] = [
6919 'label' => $topic->post_title,
6920 'value' => $topic->ID,
6921 ];
6922 }
6923 }
6924 }
6925 return [
6926 'options' => $options,
6927 'hasMore' => $topics_count > $limit && $topics_count > $offset,
6928 ];
6929 }
6930
6931
6932 /**
6933 * Search Last Updated Field Data.
6934 *
6935 * @param array $data data.
6936 * @return array
6937 */
6938 public function search_bbp_last_data( $data ) {
6939 global $wpdb;
6940
6941 $post_type = $data['post_type'];
6942 $trigger = $data['search_term'];
6943 $context = [];
6944
6945 if ( 'topic' === $post_type ) {
6946 $post_id = $data['filter']['forum']['value'];
6947 if ( -1 === $post_id ) {
6948 $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" );
6949
6950 } else {
6951 $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 ) );
6952
6953 }
6954 } elseif ( 'reply' === $post_type ) {
6955 $post_id = $data['filter']['topic']['value'];
6956
6957 if ( -1 === $post_id ) {
6958 $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" );
6959
6960 } else {
6961 $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 ) );
6962 }
6963 }
6964
6965
6966 $response = [];
6967 if ( ! empty( $result ) ) {
6968 if ( 'bbpress_topic_created' === $trigger ) {
6969 $topic_id = $result[0]->post_id;
6970 $forum_id = $result[0]->meta_value;
6971 $topic = get_the_title( $topic_id );
6972 $topic_link = get_the_permalink( $topic_id );
6973 $topic_description = get_the_content( $topic_id );
6974 $topic_status = get_post_status( $topic_id );
6975
6976 $forum = get_the_title( $forum_id );
6977 $forum_link = get_the_permalink( $forum_id );
6978 $forum_description = get_the_content( $forum_id );
6979 $forum_status = get_post_status( $forum_id );
6980
6981 $forum = [
6982 'forum' => $forum_id,
6983 'forum_title' => $forum,
6984 'forum_link' => $forum_link,
6985 'forum_description' => $forum_description,
6986 'forum_status' => $forum_status,
6987 ];
6988
6989 $topic = [
6990 'topic_title' => $topic,
6991 'topic_link' => $topic_link,
6992 'topic_description' => $topic_description,
6993 'topic_status' => $topic_status,
6994 ];
6995
6996 $user_id = $result[0]->post_author;
6997 $context = array_merge(
6998 WordPress::get_user_context( $user_id ),
6999 $forum,
7000 $topic
7001 );
7002
7003 $response['pluggable_data'] = $context;
7004 $response['response_type'] = 'live';
7005 } else {
7006 $reply_id = $result[0]->post_id;
7007 $topic_id = $result[0]->meta_value;
7008 $forum_id = get_post_meta( $topic_id, '_bbp_forum_id', true );
7009 $forum_id = intval( '"' . $forum_id . '"' );
7010 $reply = get_the_title( $reply_id );
7011 $reply_link = get_the_permalink( $reply_id );
7012 $reply_description = get_the_content( $reply_id );
7013 $reply_status = get_post_status( $reply_id );
7014
7015
7016 $topic = get_the_title( $topic_id );
7017 $topic_link = get_the_permalink( $topic_id );
7018 $topic_description = get_the_content( $topic_id );
7019 $topic_status = get_post_status( $topic_id );
7020
7021 $forum = get_the_title( $forum_id );
7022 $forum_link = get_the_permalink( $forum_id );
7023 $forum_description = get_the_content( null, false, $forum_id );
7024 $forum_status = get_post_status( $forum_id );
7025
7026 $forum = [
7027 'forum' => $forum_id,
7028 'forum_title' => $forum,
7029 'forum_link' => $forum_link,
7030 'forum_description' => $forum_description,
7031 'forum_status' => $forum_status,
7032 ];
7033
7034 $topic = [
7035 'topic_title' => $topic,
7036 'topic_link' => $topic_link,
7037 'topic_description' => $topic_description,
7038 'topic_status' => $topic_status,
7039 ];
7040
7041 $reply = [
7042 'reply_title' => $reply,
7043 'reply_link' => $reply_link,
7044 'reply_description' => $reply_description,
7045 'reply_status' => $reply_status,
7046 ];
7047 $user_id = $result[0]->post_author;
7048 $context = array_merge(
7049 WordPress::get_user_context( $user_id ),
7050 $forum,
7051 $topic,
7052 $reply
7053 );
7054
7055 $response['pluggable_data'] = $context;
7056 $response['response_type'] = 'live';
7057 }
7058 }
7059
7060 return $response;
7061 }
7062
7063 /**
7064 * Search Last Updated Field Data.
7065 *
7066 * @param array $data data.
7067 * @return array|void
7068 */
7069 public function search_happyform_list( $data ) {
7070 if ( ! function_exists( 'happyforms_get_form_controller' ) ) {
7071 return;
7072 }
7073
7074 $form_controller = happyforms_get_form_controller();
7075
7076 $forms = $form_controller->do_get();
7077 $options = [];
7078 if ( ! empty( $forms ) ) {
7079 foreach ( $forms as $form ) {
7080 $options[] = [
7081 'label' => $form['post_title'],
7082 'value' => $form['ID'],
7083 ];
7084 }
7085 }
7086
7087 return [
7088 'options' => $options,
7089 'hasMore' => false,
7090 ];
7091 }
7092
7093 /**
7094 * Get Memberpress Course List.
7095 *
7096 * @param array $data data.
7097 *
7098 * @return array|void
7099 */
7100 public function search_mpc_lessons_list( $data ) {
7101 if ( ! class_exists( '\memberpress\courses\models\Lesson' ) ) {
7102 return;
7103 }
7104 global $wpdb;
7105 $options = [];
7106 $course_id = $data['dynamic'];
7107 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mpcs_sections WHERE course_id =%s", $course_id ) );
7108 $sections = [];
7109 foreach ( $result as $rec ) {
7110 $sections[] = [
7111 'id' => $rec->id,
7112 'title' => $rec->title,
7113 ];
7114 }
7115 if ( is_array( $sections ) && count( $sections ) > 0 ) {
7116 foreach ( $sections as $section ) {
7117 $post_types_string = \memberpress\courses\models\Lesson::lesson_cpts();
7118 $post_types_string = implode( "','", $post_types_string );
7119
7120 $query = $wpdb->prepare(
7121 "SELECT * FROM {$wpdb->posts} AS p
7122 JOIN {$wpdb->postmeta} AS pm
7123 ON p.ID = pm.post_id
7124 AND pm.meta_key = %s
7125 AND pm.meta_value = %s
7126 JOIN {$wpdb->postmeta} AS pm_order
7127 ON p.ID = pm_order.post_id
7128 AND pm_order.meta_key = %s
7129 WHERE p.post_type in ( %s ) AND p.post_status <> 'trash'
7130 ORDER BY pm_order.meta_value * 1",
7131 models\Lesson::$section_id_str,
7132 $section['id'],
7133 models\Lesson::$lesson_order_str,
7134 stripcslashes( $post_types_string )
7135 );
7136
7137 $db_lessons = $wpdb->get_results( stripcslashes( $query ) ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
7138 foreach ( $db_lessons as $lesson ) {
7139 $options[] = [
7140 'label' => $section['title'] . '->' . $lesson->post_title,
7141 'value' => $lesson->ID,
7142 ];
7143 }
7144 }
7145 }
7146 return [
7147 'options' => $options,
7148 'hasMore' => false,
7149 ];
7150 }
7151
7152 /**
7153 * Get Memberpress Course List.
7154 *
7155 * @param array $data data.
7156 *
7157 * @return array|void
7158 */
7159 public function search_gp_rank_type_list( $data ) {
7160 global $wpdb;
7161
7162 $posts = $wpdb->get_results(
7163 "SELECT ID, post_name, post_title, post_type
7164 FROM $wpdb->posts
7165 WHERE post_type LIKE 'rank-type' AND post_status = 'publish' ORDER BY post_title ASC"
7166 );
7167
7168 $posts_count = count( $posts );
7169
7170 $options = [];
7171 if ( $posts ) {
7172 foreach ( $posts as $post ) {
7173 $options[] = [
7174 'label' => $post->post_title,
7175 'value' => $post->post_name,
7176 ];
7177 }
7178 }
7179
7180 return [
7181 'options' => $options,
7182 'hasMore' => false,
7183 ];
7184 }
7185
7186 /**
7187 * Get MPC last data.
7188 *
7189 * @param array $data data.
7190 *
7191 * @return array
7192 */
7193 public function search_mpc_last_data( $data ) {
7194 global $wpdb;
7195 $trigger = $data['search_term'];
7196 $course_data = [];
7197 $lesson_data = [];
7198 $context = [];
7199
7200 if ( 'mpc_course_completed' === $trigger ) {
7201 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course]']['value'] : '-1' );
7202 if ( $course_id > 0 ) {
7203
7204 $course = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $course_id ) );
7205 } else {
7206 $course = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-course' ORDER BY id DESC LIMIT 1" );
7207 }
7208
7209 if ( ! empty( $course ) ) {
7210 $course_data = [
7211 'course_id' => $course->ID,
7212 'course_title' => get_the_title( $course_id ),
7213 'course_url' => get_permalink( $course_id ),
7214 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
7215 'course_featured_image_url' => get_the_post_thumbnail_url( $course_id ),
7216 ];
7217 }
7218 $user_progress = $wpdb->get_row( $wpdb->prepare( "SELECT user_id FROM {$wpdb->prefix}mpcs_user_progress WHERE course_id=%s", $course_id ) );
7219 if ( ! empty( $user_progress ) ) {
7220 $context['response_type'] = 'live';
7221 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
7222 } else {
7223 $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}
7224 ,"response_type":"sample"} ';
7225 $context = json_decode( $sample_data, true );
7226 }
7227 } elseif ( 'mpc_lesson_completed' === $trigger ) {
7228 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
7229 $course_id = (int) $data['filter']['course']['value'];
7230 if ( $lesson_id > 0 ) {
7231
7232 $lesson = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $lesson_id ) );
7233 } else {
7234 $lesson = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-lesson' ORDER BY id DESC LIMIT 1" );
7235 }
7236
7237 if ( ! empty( $lesson ) ) {
7238 $lesson_data = [
7239 'lesson_id' => $lesson->ID,
7240 'lesson_title' => get_the_title( $lesson_id ),
7241 'lesson_url' => get_permalink( $lesson_id ),
7242 'lesson_featured_image_id' => get_post_meta( $lesson_id, '_thumbnail_id', true ),
7243 'lesson_featured_image_url' => get_the_post_thumbnail_url( $lesson_id ),
7244 ];
7245
7246 $lesson_section_id = get_post_meta( $lesson->ID, '_mpcs_lesson_section_id', true );
7247
7248 $section = $wpdb->get_row( $wpdb->prepare( "SELECT course_id FROM {$wpdb->prefix}mpcs_sections WHERE ID= %s", $lesson_section_id ) );
7249
7250 $course_data = [
7251 'course_id' => $course_id,
7252 'course_title' => get_the_title( $course_id ),
7253 'course_url' => get_permalink( $course_id ),
7254 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
7255 'course_featured_image_url' => get_the_post_thumbnail_url( $section->course_id ),
7256 ];
7257 }
7258
7259 $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 ) );
7260 if ( ! empty( $user_progress ) ) {
7261 $context['response_type'] = 'live';
7262 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
7263 } else {
7264 $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"}';
7265 $context = json_decode( $sample_data, true );
7266 }
7267 }
7268
7269
7270 return $context;
7271 }
7272
7273 /** Get GamiPress Rank List.
7274 *
7275 * @param array $data data.
7276 *
7277 * @return array|void
7278 */
7279 public function search_gp_rank_list( $data ) {
7280 global $wpdb;
7281
7282 $page = $data['page'];
7283 $limit = Utilities::get_search_page_limit();
7284 $offset = $limit * ( $page - 1 );
7285
7286 $args = [
7287 'post_type' => $data['dynamic']['rank_type'],
7288 'posts_per_page' => $limit,
7289 'offset' => $offset,
7290 'orderby' => 'title',
7291 'order' => 'ASC',
7292 'post_status' => 'publish',
7293 's' => $data['search_term'],
7294 ];
7295
7296 $rank_type = get_posts( $args );
7297
7298 $count_args = [
7299 'post_type' => $data['dynamic']['rank_type'],
7300 'posts_per_page' => -1,
7301 'post_status' => 'publish',
7302 's' => $data['search_term'],
7303 ];
7304
7305 $rank_posts = get_posts( $count_args );
7306 $rank_type_count = count( $rank_posts );
7307
7308 $options = [];
7309 if ( $rank_type ) {
7310 foreach ( $rank_type as $rank ) {
7311 $options[] = [
7312 'label' => $rank->post_title,
7313 'value' => $rank->ID,
7314 ];
7315 }
7316 }
7317
7318 return [
7319 'options' => $options,
7320 'hasMore' => $rank_type_count > $limit && $rank_type_count > $offset,
7321 ];
7322 }
7323
7324 /**
7325 * Get GamiPress PointType List.
7326 *
7327 * @param array $data data.
7328 *
7329 * @return array|void
7330 */
7331 public function search_gp_point_type_list( $data ) {
7332 global $wpdb;
7333
7334 $page = $data['page'];
7335 $limit = Utilities::get_search_page_limit();
7336 $offset = $limit * ( $page - 1 );
7337
7338 $args = [
7339 'post_type' => 'points-type',
7340 'posts_per_page' => $limit,
7341 'offset' => $offset,
7342 'orderby' => 'title',
7343 'order' => 'ASC',
7344 'post_status' => 'publish',
7345 's' => $data['search_term'],
7346 ];
7347
7348 $point_type = get_posts( $args );
7349
7350 $count_args = [
7351 'post_type' => 'points-type',
7352 'posts_per_page' => -1,
7353 'post_status' => 'publish',
7354 's' => $data['search_term'],
7355 ];
7356
7357 $count_point_type = get_posts( $count_args );
7358 $point_type_count = count( $count_point_type );
7359
7360 $options = [];
7361 if ( $point_type ) {
7362 foreach ( $point_type as $point ) {
7363 $options[] = [
7364 'label' => $point->post_title,
7365 'value' => $point->ID,
7366 ];
7367 }
7368 }
7369
7370 return [
7371 'options' => $options,
7372 'hasMore' => $point_type_count > $limit && $point_type_count > $offset,
7373 ];
7374 }
7375
7376 /**
7377 * Get GamiPress AchievementType List.
7378 *
7379 * @param array $data data.
7380 *
7381 * @return array|void
7382 */
7383 public function search_gp_achivement_type_list( $data ) {
7384 global $wpdb;
7385
7386 $posts = $wpdb->get_results(
7387 "SELECT ID, post_name, post_title, post_type
7388 FROM $wpdb->posts
7389 WHERE post_type LIKE 'achievement-type' AND post_status = 'publish' ORDER BY post_title ASC"
7390 );
7391
7392 $posts_count = count( $posts );
7393
7394 $options = [];
7395 if ( $posts ) {
7396 foreach ( $posts as $post ) {
7397 $options[] = [
7398 'label' => $post->post_title,
7399 'value' => $post->post_name,
7400 ];
7401 }
7402 }
7403
7404 $options[] = [
7405 'label' => 'Points awards',
7406 'value' => 'points-award',
7407 ];
7408 $options[] = [
7409 'label' => 'Step',
7410 'value' => 'step',
7411 ];
7412 $options[] = [
7413 'label' => 'Rank requirement',
7414 'value' => 'rank-requirement',
7415 ];
7416
7417 return [
7418 'options' => $options,
7419 'hasMore' => false,
7420 ];
7421 }
7422
7423 /**
7424 * Get GamiPress Award List.
7425 *
7426 * @param array $data data.
7427 *
7428 * @return array|void
7429 */
7430 public function search_gp_award_list( $data ) {
7431 global $wpdb;
7432
7433 $page = $data['page'];
7434 $limit = Utilities::get_search_page_limit();
7435 $offset = $limit * ( $page - 1 );
7436
7437 $args = [
7438 'post_type' => $data['dynamic']['achivement_type'],
7439 'posts_per_page' => $limit,
7440 'offset' => $offset,
7441 'orderby' => 'title',
7442 'order' => 'ASC',
7443 'post_status' => 'publish',
7444 's' => $data['search_term'],
7445 ];
7446
7447 $award_type = get_posts( $args );
7448 $count_args = [
7449 'post_type' => $data['dynamic']['achivement_type'],
7450 'posts_per_page' => -1,
7451 'post_status' => 'publish',
7452 's' => $data['search_term'],
7453 ];
7454
7455 $count_award_type = get_posts( $count_args );
7456 $award_type_count = count( $count_award_type );
7457 $options = [];
7458 if ( $award_type ) {
7459 foreach ( $award_type as $award ) {
7460 $options[] = [
7461 'label' => $award->post_title,
7462 'value' => $award->ID,
7463 ];
7464 }
7465 }
7466
7467 return [
7468 'options' => $options,
7469 'hasMore' => $award_type_count > $limit && $award_type_count > $offset,
7470 ];
7471 }
7472
7473 /**
7474 * Get Woocommerce Subscription Product List.
7475 *
7476 * @param array $data data.
7477 *
7478 * @return array|void
7479 */
7480 public function search_wc_subscription_product_list( $data ) {
7481 global $wpdb;
7482
7483 $subscriptions = $wpdb->get_results(
7484 $wpdb->prepare(
7485 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
7486 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
7487 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
7488 AND posts.post_type = %s
7489 AND posts.post_status = %s
7490 UNION ALL
7491 SELECT ID, post_title FROM $wpdb->posts
7492 WHERE post_type = %s
7493 AND post_status = %s
7494 ORDER BY post_title",
7495 'product',
7496 'publish',
7497 'shop_subscription',
7498 'publish'
7499 )
7500 );
7501
7502 $options = [];
7503 if ( $subscriptions ) {
7504 foreach ( $subscriptions as $post ) {
7505 $options[] = [
7506 'label' => $post->post_title,
7507 'value' => $post->ID,
7508 ];
7509 }
7510 }
7511
7512 return [
7513 'options' => $options,
7514 'hasMore' => false,
7515 ];
7516 }
7517
7518 /**
7519 * Get Woocommerce Subscriptions Variation list.
7520 *
7521 * @param array $data data.
7522 *
7523 * @return array|void
7524 */
7525 public function search_wc_variable_subscription_list( $data ) {
7526 global $wpdb;
7527
7528 $page = $data['page'];
7529 $limit = Utilities::get_search_page_limit();
7530 $offset = $limit * ( $page - 1 );
7531
7532 if ( ! function_exists( 'wc_get_products' ) ) {
7533 return;
7534 }
7535 $subscription_products = wc_get_products(
7536 [
7537 'type' => [ 'variable-subscription' ],
7538 'posts_per_page' => $limit,
7539 'offset' => $offset,
7540 'orderby' => 'date',
7541 'order' => 'DESC',
7542 ]
7543 );
7544
7545 $subscription_products_count = count( (array) $subscription_products );
7546
7547 $options = [];
7548 if ( $subscription_products ) {
7549 foreach ( (array) $subscription_products as $product ) {
7550 $options[] = [
7551 'label' => $product->get_title(),
7552 'value' => $product->get_id(),
7553 ];
7554 }
7555 }
7556
7557 return [
7558 'options' => $options,
7559 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
7560 ];
7561 }
7562
7563 /**
7564 * Get Woocommerce Variation list.
7565 *
7566 * @param array $data data.
7567 *
7568 * @return array|void
7569 */
7570 public function search_wc_variation_list( $data ) {
7571 global $wpdb;
7572
7573 $page = $data['page'];
7574 $limit = Utilities::get_search_page_limit();
7575 $offset = $limit * ( $page - 1 );
7576
7577 $args = [
7578 'post_type' => 'product_variation',
7579 'post_parent' => $data['dynamic']['variable_subscription'],
7580 'posts_per_page' => $limit,
7581 'offset' => $offset,
7582 'orderby' => 'ID',
7583 'order' => 'ASC',
7584 'post_status' => 'publish',
7585 ];
7586
7587 $variation = get_posts( $args );
7588 $variation_count = count( $variation );
7589
7590 $options = [];
7591 if ( $variation ) {
7592 foreach ( $variation as $product ) {
7593 $options[] = [
7594 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
7595 'value' => $product->ID,
7596 ];
7597 }
7598 }
7599
7600 return [
7601 'options' => $options,
7602 'hasMore' => $variation_count > $limit && $variation_count > $offset,
7603 ];
7604 }
7605
7606 /**
7607 * Get Membership List.
7608 *
7609 * @param array $data data.
7610 *
7611 * @return array
7612 */
7613 public function search_membership_list( $data ) {
7614 global $wpdb;
7615
7616 $levels = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}pmpro_membership_levels ORDER BY id ASC" );
7617 $options = [];
7618 if ( $levels ) {
7619 foreach ( $levels as $level ) {
7620 $options[] = [
7621 'label' => $level->name,
7622 'value' => $level->id,
7623 ];
7624 }
7625 }
7626
7627 return [
7628 'options' => $options,
7629 'hasMore' => false,
7630 ];
7631 }
7632
7633 /**
7634
7635 * Get EventsManager last data.
7636 *
7637 * @param array $data data.
7638 *
7639 * @return array
7640 */
7641 public function search_events_manager_data( $data ) {
7642 global $wpdb;
7643 $trigger = $data['search_term'];
7644 $context = [];
7645
7646 $post_id = (int) ( isset( $data['filter']['post_id']['value'] ) ? $data['filter']['post_id']['value'] : '-1' );
7647 if ( 'em_user_register_in_event' === $trigger ) {
7648 if ( $post_id > 0 ) {
7649 $event_id_id = get_post_meta( $post_id, '_event_id', true );
7650 $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 ) );
7651 } else {
7652 $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" );
7653
7654 }
7655
7656 if ( ! empty( $all_bookings ) ) {
7657 $user_id = $all_bookings->person_id;
7658 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
7659 $context['pluggable_data'] = array_merge(
7660 WordPress::get_user_context( $user_id ),
7661 json_decode( wp_json_encode( $all_bookings ), true )
7662 );
7663 if ( ! empty( $location ) ) {
7664 $context['pluggable_data'] = array_merge( $context['pluggable_data'], json_decode( wp_json_encode( $location ), true ) );
7665 }
7666
7667 $context['response_type'] = 'live';
7668 }
7669 } elseif ( 'em_user_unregister_from_event' === $trigger ) {
7670
7671 if ( $post_id > 0 ) {
7672 $event_id_id = get_post_meta( $post_id, '_event_id', true );
7673 $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 ) );
7674 } else {
7675 $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" );
7676
7677 }
7678
7679 if ( ! empty( $all_bookings ) ) {
7680 $user_id = $all_bookings->person_id;
7681 $context['pluggable_data'] = array_merge(
7682 WordPress::get_user_context( $user_id ),
7683 json_decode( wp_json_encode( $all_bookings ), true )
7684 );
7685 $context['response_type'] = 'live';
7686 }
7687 } elseif ( 'em_user_booking_approved' === $trigger ) {
7688
7689 if ( $post_id > 0 ) {
7690 $event_id_id = get_post_meta( $post_id, '_event_id', true );
7691 $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 ) );
7692 } else {
7693 $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" );
7694
7695 }
7696
7697 if ( ! empty( $all_bookings ) ) {
7698 $user_id = $all_bookings->person_id;
7699 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
7700 $context['pluggable_data'] = array_merge(
7701 WordPress::get_user_context( $user_id ),
7702 json_decode( wp_json_encode( $all_bookings ), true )
7703 );
7704 if ( ! empty( $location ) ) {
7705 $context['pluggable_data'] = array_merge( $context['pluggable_data'], json_decode( wp_json_encode( $location ), true ) );
7706 }
7707
7708 $context['response_type'] = 'live';
7709
7710 }
7711 }
7712 return $context;
7713 }
7714
7715 /**
7716
7717 * Get learnpress last data.
7718 *
7719 * @param array $data data.
7720 *
7721 * @return array
7722 */
7723 public function search_learnpress_lms_last_data( $data ) {
7724 global $wpdb;
7725 $trigger = $data['search_term'];
7726 $course_data = [];
7727 $lesson_data = [];
7728 $context = [];
7729
7730
7731 if ( 'learnpress_course_completed' === $trigger ) {
7732 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
7733 if ( $course_id > 0 ) {
7734
7735 $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 ) );
7736 } else {
7737
7738 $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" );
7739 }
7740
7741 if ( ! empty( $course ) ) {
7742 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
7743 $context['response_type'] = 'live';
7744 $context['pluggable_data'] = $course_data;
7745 }
7746 } elseif ( 'learnpress_lesson_completed' === $trigger ) {
7747 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
7748 if ( $lesson_id > 0 ) {
7749
7750 $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 ) );
7751 } else {
7752
7753 $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" );
7754 }
7755
7756 if ( ! empty( $lesson ) ) {
7757 $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 ) );
7758 $context['response_type'] = 'live';
7759 $context['pluggable_data'] = $lesson_data;
7760 }
7761 } elseif ( 'learnpress_user_enrolled_in_course' === $trigger ) {
7762 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
7763 if ( $course_id > 0 ) {
7764
7765 $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 ) );
7766 } else {
7767
7768 $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" );
7769 }
7770
7771 if ( ! empty( $course ) ) {
7772 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
7773 $context['response_type'] = 'live';
7774 $context['pluggable_data'] = $course_data;
7775
7776 }
7777 }
7778
7779 return $context;
7780 }
7781
7782 /**
7783 * Get Woocommerce Memberships Plan List.
7784 *
7785 * @param array $data data.
7786 *
7787 * @return array|void
7788 */
7789 public function search_wc_membership_plan_list( $data ) {
7790 global $wpdb;
7791
7792 $page = $data['page'];
7793 $limit = Utilities::get_search_page_limit();
7794 $offset = $limit * ( $page - 1 );
7795
7796 $args = [
7797 'post_type' => 'wc_membership_plan',
7798 'posts_per_page' => $limit,
7799 'offset' => $offset,
7800 'orderby' => 'title',
7801 'order' => 'ASC',
7802 'post_status' => 'publish',
7803 'fields' => 'ids',
7804 ];
7805 $loop = new WP_Query( $args );
7806
7807 $plans = (array) $loop->posts;
7808 $plans_count = count( $plans );
7809
7810 $options = [];
7811 if ( ! empty( $plans ) ) {
7812 if ( is_array( $plans ) ) {
7813 foreach ( $plans as $plan_id ) {
7814 $options[] = [
7815 'label' => get_the_title( $plan_id ),
7816 'value' => $plan_id,
7817 ];
7818 }
7819 }
7820 }
7821
7822 return [
7823 'options' => $options,
7824 'hasMore' => $plans_count > $limit && $plans_count > $offset,
7825 ];
7826 }
7827
7828 /**
7829 * Get BuddyPress Private group.
7830 *
7831 * @param array $data data.
7832 *
7833 * @return array|void
7834 */
7835 public function search_bp_private_group_list( $data ) {
7836 global $wpdb;
7837
7838 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'private'" );
7839
7840 $options = [];
7841 if ( $groups ) {
7842 foreach ( $groups as $group ) {
7843 $options[] = [
7844 'label' => $group->name,
7845 'value' => $group->id,
7846 ];
7847 }
7848 }
7849
7850 return [
7851 'options' => $options,
7852 'hasMore' => false,
7853 ];
7854 }
7855
7856 /**
7857 * Get BuddyPress Public group.
7858 *
7859 * @param array $data data.
7860 *
7861 * @return array|void
7862 */
7863 public function search_bp_public_group_list( $data ) {
7864 global $wpdb;
7865
7866 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'public'" );
7867
7868 $options = [];
7869 if ( $groups ) {
7870 foreach ( $groups as $group ) {
7871 $options[] = [
7872 'label' => $group->name,
7873 'value' => $group->id,
7874 ];
7875 }
7876 }
7877
7878 return [
7879 'options' => $options,
7880 'hasMore' => false,
7881 ];
7882 }
7883
7884 /**
7885 * Get BuddyPress group.
7886 *
7887 * @param array $data data.
7888 *
7889 * @return array|void
7890 */
7891 public function search_bp_group_list( $data ) {
7892 global $wpdb;
7893
7894 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
7895
7896 $options = [];
7897 if ( $groups ) {
7898 foreach ( $groups as $group ) {
7899 $options[] = [
7900 'label' => $group->name,
7901 'value' => $group->id,
7902 ];
7903 }
7904 }
7905
7906 return [
7907 'options' => $options,
7908 'hasMore' => false,
7909 ];
7910 }
7911
7912 /**
7913 * Get BuddyPress field.
7914 *
7915 * @param array $data data.
7916 *
7917 * @return array|void
7918 */
7919 public function search_bp_field_list( $data ) {
7920 global $wpdb;
7921
7922 $base_group_id = 1;
7923 if ( function_exists( 'bp_xprofile_base_group_id' ) ) {
7924 $base_group_id = bp_xprofile_base_group_id();
7925 }
7926
7927 $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 ) );
7928
7929 $options = [];
7930 if ( ! empty( $xprofile_fields ) ) {
7931 foreach ( $xprofile_fields as $xprofile_field ) {
7932 $options[] = [
7933 'label' => $xprofile_field->name,
7934 'value' => $xprofile_field->id,
7935 ];
7936 }
7937 }
7938
7939 return [
7940 'options' => $options,
7941 'hasMore' => false,
7942 ];
7943 }
7944
7945 /**
7946 * Get BuddyPress member type.
7947 *
7948 * @param array $data data.
7949 *
7950 * @return array|void
7951 */
7952 public function search_bp_member_type_list( $data ) {
7953 $options = [];
7954 if ( function_exists( 'bp_get_member_types' ) ) {
7955 $types = bp_get_member_types( [] );
7956 if ( $types ) {
7957 foreach ( $types as $key => $type ) {
7958 $options[] = [
7959 'label' => $type,
7960 'value' => $key,
7961 ];
7962 }
7963 }
7964 }
7965
7966 return [
7967 'options' => $options,
7968 'hasMore' => false,
7969 ];
7970 }
7971
7972 /**
7973 * Get last data for WP All Import.
7974 *
7975 * @param array $data data.
7976 * @return mixed
7977 */
7978 public function search_wp_all_import_last_data( $data ) {
7979 global $wpdb;
7980 $post_type = $data['filter']['post_type']['value'];
7981 $trigger = $data['search_term'];
7982
7983 if ( 'wp_all_import_post_type_imported' === $trigger ) {
7984 if ( -1 == $post_type ) {
7985 $imports = $wpdb->get_row( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts ORDER BY id DESC LIMIT 1", ARRAY_A );
7986 $posts[0] = $imports['post_id'];
7987 } else {
7988 $imports = $wpdb->get_results( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts", ARRAY_A );
7989 $imports = array_column( $imports, 'post_id' );
7990 $args = [
7991 'posts_per_page' => 1,
7992 'post_type' => $post_type,
7993 'post__in' => $imports,
7994 ];
7995 $posts = get_posts( $args );
7996 }
7997 } elseif ( 'wp_all_import_completed' === $trigger ) {
7998 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 0 ORDER BY id DESC LIMIT 1", ARRAY_A );
7999 } elseif ( 'wp_all_import_failed' === $trigger ) {
8000 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 1 ORDER BY id DESC LIMIT 1", ARRAY_A );
8001 }
8002
8003 if ( 'wp_all_import_post_type_imported' === $trigger && empty( $imports ) ) {
8004 $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 );
8005 return $context;
8006 } elseif ( empty( $imports ) ) {
8007 $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 );
8008 return $context;
8009 }
8010
8011 $context['response_type'] = 'live';
8012 if ( ! empty( $posts ) ) {
8013 $context['pluggable_data'] = WordPress::get_post_context( $posts[0] );
8014 } else {
8015 $context['pluggable_data'] = $imports;
8016 }
8017
8018 return $context;
8019 }
8020
8021 /**
8022 * Get Wp Simple Pay Forms.
8023 *
8024 * @param array $data data.
8025 *
8026 * @return array
8027 */
8028 public function search_wp_simple_pay_forms( $data ) {
8029
8030 $page = $data['page'];
8031 $limit = Utilities::get_search_page_limit();
8032 $offset = $limit * ( $page - 1 );
8033
8034 $forms = get_posts(
8035 [
8036 'post_type' => 'simple-pay',
8037 'posts_per_page' => $limit,
8038 'offset' => $offset,
8039 'fields' => 'ids',
8040 ]
8041 );
8042
8043 $forms_count = count( $forms );
8044
8045 $options = [];
8046
8047 if ( ! empty( $forms ) ) {
8048 foreach ( $forms as $form_id ) {
8049 if ( function_exists( 'simpay_get_form' ) ) {
8050 $form = simpay_get_form( $form_id );
8051 $options[] = [
8052 'label' => null !== get_the_title( $form_id ) ? $form->company_name : get_the_title( $form_id ),
8053 'value' => $form_id,
8054 ];
8055 }
8056 }
8057 }
8058
8059 return [
8060 'options' => $options,
8061 'hasMore' => $forms_count > $limit && $forms_count > $offset,
8062 ];
8063 }
8064
8065 /**
8066 * Get Post list as per post type for metabox.
8067 *
8068 * @param array $data data.
8069 *
8070 * @return array
8071 */
8072 public function search_mb_posts_list( $data ) {
8073
8074 $page = $data['page'];
8075 $limit = Utilities::get_search_page_limit();
8076 $offset = $limit * ( $page - 1 );
8077
8078 $posts = get_posts(
8079 [
8080 'post_type' => $data['dynamic'],
8081 'posts_per_page' => $limit,
8082 'offset' => $offset,
8083 'fields' => 'ids',
8084 ]
8085 );
8086
8087 $all_posts = get_posts(
8088 [
8089 'post_type' => $data['dynamic'],
8090 'posts_per_page' => -1,
8091 'fields' => 'ids',
8092 ]
8093 );
8094
8095 $posts_count = count( $all_posts );
8096
8097 $options = [];
8098
8099 if ( ! empty( $posts ) ) {
8100 foreach ( $posts as $post ) {
8101 $options[] = [
8102 'label' => get_the_title( $post ),
8103 'value' => $post,
8104 ];
8105 }
8106 }
8107
8108 return [
8109 'options' => $options,
8110 'hasMore' => $posts_count > $limit && $posts_count > $offset,
8111 ];
8112 }
8113
8114 /**
8115 * Get Metabox Custom box in Post list.
8116 *
8117 * @param array $data data.
8118 *
8119 * @return array
8120 */
8121 public function search_mb_field_list( $data ) {
8122
8123 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
8124 return [];
8125 }
8126
8127 $options = [];
8128
8129 $metabox_fields = (array) rwmb_get_object_fields( $data['dynamic'] );
8130
8131 foreach ( $metabox_fields as $metabox_field ) {
8132
8133 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
8134
8135 $options[] = [
8136 'label' => $metabox_field['name'],
8137 'value' => $metabox_field['id'],
8138 ];
8139
8140 }
8141 }
8142
8143 return [
8144 'options' => $options,
8145 'hasMore' => false,
8146 ];
8147 }
8148
8149 /**
8150 * Get Metabox Custom box user list.
8151 *
8152 * @param array $data data.
8153 *
8154 * @return array
8155 */
8156 public function search_mb_user_field_list( $data ) {
8157
8158 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
8159 return [];
8160 }
8161
8162 $options = [];
8163
8164 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
8165
8166 foreach ( $metabox_fields as $metabox_field ) {
8167
8168 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
8169
8170 $options[] = [
8171 'label' => $metabox_field['name'],
8172 'value' => $metabox_field['id'],
8173 ];
8174
8175 }
8176 }
8177
8178 return [
8179 'options' => $options,
8180 'hasMore' => false,
8181 ];
8182 }
8183
8184 /**
8185 * Search Last Updated Field Data for MetaBox.
8186 *
8187 * @param array $data data.
8188 * @return array
8189 */
8190 public function search_meta_box_field_data( $data ) {
8191 global $wpdb;
8192
8193 $context = [];
8194
8195 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
8196
8197 $post_type = $data['filter']['wp_post_type']['value'];
8198 $post = $data['filter']['wp_post']['value'];
8199
8200 if ( -1 === $field ) {
8201 if ( function_exists( 'rwmb_get_object_fields' ) ) {
8202 $metaboxes = rwmb_get_object_fields( $post_type );
8203
8204 if ( ! empty( $metaboxes ) ) {
8205 $random_key = array_rand( $metaboxes );
8206 $field = $random_key;
8207 } else {
8208 $result = '';
8209 }
8210 }
8211 } else {
8212 $field = $data['filter']['field_id']['value'];
8213 }
8214
8215 if ( function_exists( 'rwmb_meta' ) ) {
8216 $result = rwmb_meta( $field, '', $post );
8217 }
8218
8219 $response = [];
8220 if ( ! empty( $result ) ) {
8221 $response['pluggable_data'] = array_merge( [ $field => $result ], WordPress::get_post_context( $post ) );
8222 $response['response_type'] = 'live';
8223 } else {
8224 $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 );
8225 }
8226
8227 return $response;
8228 }
8229
8230 /**
8231 * Search Last Updated User Field Data MetaBox.
8232 *
8233 * @param array $data data.
8234 * @return array
8235 */
8236 public function search_user_meta_box_field_data( $data ) {
8237 global $wpdb;
8238
8239 $context = [];
8240
8241 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
8242
8243 if ( -1 === $field ) {
8244 if ( function_exists( 'rwmb_get_object_fields' ) ) {
8245 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
8246
8247 if ( ! empty( $metabox_fields ) ) {
8248 $random_key = array_rand( $metabox_fields );
8249 $field = $random_key;
8250 } else {
8251 $result = '';
8252 }
8253 }
8254 } else {
8255 $field = $data['filter']['field_id']['value'];
8256 }
8257
8258 $users = get_users(
8259 [
8260 'fields' => 'ID',
8261 'meta_key' => $field,
8262 ]
8263 );
8264
8265 if ( ! empty( $users ) ) {
8266 $user_random_key = array_rand( $users );
8267 $user_id = $user_random_key;
8268 if ( function_exists( 'rwmb_get_value' ) ) {
8269 $result = rwmb_get_value( $field, [ 'object_type' => 'user' ], $users[ $user_id ] );
8270 }
8271
8272 $response = [];
8273 if ( ! empty( $result ) ) {
8274 $context = [
8275 'field_id' => $field,
8276 $field => $result,
8277 'user' => WordPress::get_user_context( $users[ $user_id ] ),
8278 ];
8279 $response['pluggable_data'] = $context;
8280 $response['response_type'] = 'live';
8281 } else {
8282 $response = json_decode(
8283 '{
8284 "response_type": "sample",
8285 "pluggable_data": {
8286 "field_id": "gender",
8287 "user": {
8288 "wp_user_id": 114,
8289 "user_login": "test",
8290 "display_name": "test",
8291 "user_firstname": "test",
8292 "user_lastname": "test",
8293 "user_email": "test@test.com",
8294 "user_role": [ "subscriber" ]
8295 }
8296 }
8297 }',
8298 true
8299 );
8300 }
8301 } else {
8302 $response = json_decode(
8303 '{
8304 "response_type": "sample",
8305 "pluggable_data": {
8306 "field_id": "gender",
8307 "user": {
8308 "wp_user_id": 114,
8309 "user_login": "test",
8310 "display_name": "test",
8311 "user_firstname": "test",
8312 "user_lastname": "test",
8313 "user_email": "test@test.com",
8314 "user_role": [ "subscriber" ]
8315 }
8316 }
8317 }',
8318 true
8319 );
8320 }
8321
8322 return $response;
8323 }
8324
8325 /**
8326 * Search forms of Pie Forms.
8327 *
8328 * @param array $data data.
8329 * @return array
8330 */
8331 public function search_wp_polls_list( $data ) {
8332 global $wpdb;
8333 $options = [];
8334
8335 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsq' ) ) ) {
8336
8337 $results = $wpdb->get_results( 'SELECT pollq_id, pollq_question FROM ' . $wpdb->prefix . 'pollsq WHERE pollq_active = 1' );
8338
8339 if ( $results ) {
8340 foreach ( $results as $result ) {
8341 $options[] = [
8342 'label' => $result->pollq_question,
8343 'value' => $result->pollq_id,
8344 ];
8345 }
8346 }
8347 }
8348
8349 return [
8350 'options' => $options,
8351 'hasMore' => false,
8352 ];
8353 }
8354
8355 /**
8356 * Search answers of WP-Polls questions.
8357 *
8358 * @param array $data data.
8359 * @return array
8360 */
8361 public function search_wp_polls_answers( $data ) {
8362 global $wpdb;
8363
8364 $options = [];
8365 $poll_id = $data['dynamic'];
8366
8367 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsa' ) ) ) {
8368
8369 if ( '-1' !== $poll_id ) {
8370 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa WHERE polla_qid = %d', $poll_id ) );
8371 } else {
8372 $results = $wpdb->get_results( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa' );
8373 }
8374
8375 if ( $results ) {
8376 foreach ( $results as $result ) {
8377 $options[] = [
8378 'label' => $result->polla_answers,
8379 'value' => $result->polla_aid,
8380 ];
8381 }
8382 }
8383 }
8384
8385 return [
8386 'options' => $options,
8387 'hasMore' => false,
8388 ];
8389 }
8390
8391 /**
8392 * Get last data for trigger.
8393 *
8394 * @param array $data data.
8395 * @return array
8396 */
8397 public function search_wp_polls_triggers_last_data( $data ) {
8398 global $wpdb;
8399
8400 $context = [];
8401 $context['response_type'] = 'sample';
8402
8403 $poll = [
8404 'poll_id' => 1,
8405 'question' => 'Which skills are you interested to learn?',
8406 'answers' => 'Web Development, Graphic Designing, Content Writing, Digital Marketing',
8407 'start_date' => '2023-08-29 17:19:13',
8408 'end_date' => 'Not set',
8409 'selected_answers' => 'Content Writing, Web Development',
8410 'selected_answer_id' => 2,
8411 ];
8412
8413 $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" );
8414
8415 if ( ! empty( $poll_data ) ) {
8416 $poll = WpPolls::get_poll_context( (string) $poll_data->answer_id, (int) $poll_data->poll_id );
8417 $poll['selected_answer_id'] = (int) $poll_data->answer_id;
8418
8419 $context['response_type'] = 'live';
8420 }
8421
8422 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
8423
8424 if ( 'poll_submitted' === $term ) {
8425 unset( $poll['selected_answer_id'] );
8426 }
8427
8428 $context['pluggable_data'] = $poll;
8429
8430 return $context;
8431 }
8432
8433 /**
8434 * Get ACF Custom fields list.
8435 *
8436 * @param array $data data.
8437 *
8438 * @return array
8439 */
8440 public function search_acf_post_field_list( $data ) {
8441
8442 $post_id = $data['dynamic'];
8443
8444 $args = [
8445 'post_id' => $post_id,
8446 ];
8447 if ( ! is_numeric( $post_id ) ) {
8448 $args = [
8449 'post_type' => $post_id,
8450 ];
8451 }
8452 $options = [];
8453 if ( function_exists( 'acf_get_field_groups' ) ) {
8454 $field_groups_collection = acf_get_field_groups( $args );
8455 foreach ( $field_groups_collection as $field_group ) {
8456 if ( function_exists( 'acf_get_fields' ) ) {
8457 $field_groups[] = acf_get_fields( $field_group['key'] );
8458 }
8459 }
8460
8461 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
8462 foreach ( $field_groups as $field_groups ) {
8463 foreach ( $field_groups as $field_group ) {
8464 $options[] = [
8465 'value' => $field_group['name'],
8466 'label' => $field_group['label'],
8467 ];
8468 }
8469 }
8470 }
8471 }
8472
8473 return [
8474 'options' => $options,
8475 'hasMore' => false,
8476 ];
8477 }
8478
8479 /**
8480 * Get ACF Custom fields list.
8481 *
8482 * @param array $data data.
8483 *
8484 * @return array
8485 */
8486 public function search_acf_user_field_list( $data ) {
8487
8488 if ( ! function_exists( 'acf_get_fields' ) ) {
8489 return [];
8490 }
8491 if ( ! function_exists( 'acf_get_field_groups' ) ) {
8492 return [];
8493 }
8494 $groups_user_form = [];
8495 $options = [];
8496 if ( function_exists( 'acf_get_field_groups' ) ) {
8497 $field_groups = acf_get_field_groups();
8498 foreach ( $field_groups as $group ) {
8499 if ( ! empty( $group['location'] ) ) {
8500 foreach ( $group['location'] as $locations ) {
8501 foreach ( $locations as $location ) {
8502 if ( 'user_form' === $location['param'] ) {
8503 $groups_user_form[] = $group;
8504 }
8505 }
8506 }
8507 }
8508 }
8509 $field_groups = $groups_user_form;
8510 if ( empty( $field_groups ) ) {
8511 return [];
8512 }
8513 foreach ( $field_groups as $group ) {
8514 if ( function_exists( 'acf_get_fields' ) ) {
8515 $group_fields = acf_get_fields( $group['key'] );
8516 }
8517 if ( ! empty( $group_fields ) ) {
8518 foreach ( $group_fields as $field ) {
8519 $options[] = [
8520 'value' => $field['name'],
8521 'label' => $field['label'],
8522 ];
8523
8524 }
8525 }
8526 }
8527 }
8528
8529 return [
8530 'options' => $options,
8531 'hasMore' => false,
8532 ];
8533 }
8534
8535 /**
8536 * Search Last Updated Field Data for ACF.
8537 *
8538 * @param array $data data.
8539 * @return array
8540 */
8541 public function search_acf_post_field_data( $data ) {
8542 $context = [];
8543
8544 $field = ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
8545
8546 $post_type = $data['filter']['wp_post_type']['value'];
8547 $post = $data['filter']['wp_post']['value'];
8548
8549 if ( -1 === $field ) {
8550 $args = [
8551 'post_id' => $post,
8552 ];
8553 if ( function_exists( 'acf_get_field_groups' ) ) {
8554 $field_groups_collection = acf_get_field_groups( $args );
8555 }
8556 if ( ! empty( $field_groups_collection ) ) {
8557 foreach ( $field_groups_collection as $field_group ) {
8558 if ( function_exists( 'acf_get_fields' ) ) {
8559 $field_groups[] = acf_get_fields( $field_group['key'] );
8560 }
8561 }
8562 }
8563 $fields = [];
8564 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
8565 foreach ( $field_groups as $field_groups ) {
8566 $fields[] = $field_groups;
8567 }
8568 }
8569 if ( ! empty( $fields ) ) {
8570 $random_key = array_rand( $fields );
8571 $field = $random_key;
8572 } else {
8573 $result = '';
8574 }
8575 } else {
8576 $field = $data['filter']['field_id']['value'];
8577 }
8578 if ( function_exists( ( 'get_field' ) ) ) {
8579 $result = get_field( $field, $post );
8580 }
8581
8582 $response = [];
8583 if ( ! empty( $result ) ) {
8584 $response['pluggable_data'] = array_merge( [ $field => $result ], [ 'field_id' => $field ], [ 'post' => WordPress::get_post_context( $post ) ] );
8585 $response['response_type'] = 'live';
8586 } else {
8587 $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 );
8588 }
8589
8590 return $response;
8591 }
8592
8593 /**
8594 * Search Last Updated User Field Data ACF.
8595 *
8596 * @param array $data data.
8597 * @return array
8598 */
8599 public function search_acf_user_field_data( $data ) {
8600 global $wpdb;
8601
8602 $context = [];
8603
8604 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
8605
8606 if ( -1 === $field ) {
8607 $groups_user_form = [];
8608 if ( function_exists( 'acf_get_field_groups' ) ) {
8609 $field_groups = acf_get_field_groups();
8610 }
8611 if ( ! empty( $field_groups ) ) {
8612 foreach ( $field_groups as $group ) {
8613 if ( ! empty( $group['location'] ) ) {
8614 foreach ( $group['location'] as $locations ) {
8615 foreach ( $locations as $location ) {
8616 if ( 'user_form' === $location['param'] ) {
8617 $groups_user_form[] = $group;
8618 }
8619 }
8620 }
8621 }
8622 }
8623 $field_groups = $groups_user_form;
8624 }
8625 if ( empty( $field_groups ) ) {
8626 $result = '';
8627 }
8628 $fields = [];
8629 if ( ! empty( $field_groups ) ) {
8630 foreach ( $field_groups as $group ) {
8631 if ( function_exists( 'acf_get_fields' ) ) {
8632 $group_fields = acf_get_fields( $group['key'] );
8633 }
8634 if ( ! empty( $group_fields ) ) {
8635 foreach ( $group_fields as $field ) {
8636 $fields[] = $group_fields;
8637 }
8638 }
8639 }
8640 }
8641 if ( ! empty( $fields ) ) {
8642 $random_key = array_rand( $fields );
8643 $field = $random_key;
8644 } else {
8645 $result = '';
8646 }
8647 } else {
8648 $field = $data['filter']['field_id']['value'];
8649 }
8650 $users = get_users(
8651 [
8652 'fields' => 'ID',
8653 'meta_key' => $field,
8654 ]
8655 );
8656
8657 if ( ! empty( $users ) ) {
8658 $user_random_key = array_rand( $users );
8659 $user_id = $user_random_key;
8660 if ( function_exists( 'get_field' ) ) {
8661 $result = get_field( $field, 'user_' . $users[ $user_id ] );
8662 }
8663 $response = [];
8664 if ( ! empty( $result ) ) {
8665 $context = [
8666 'field_id' => $field,
8667 $field => $result,
8668 'user' => WordPress::get_user_context( $users[ $user_id ] ),
8669 ];
8670 $response['pluggable_data'] = $context;
8671 $response['response_type'] = 'live';
8672 } else {
8673 $response = json_decode(
8674 '{
8675 "response_type": "sample",
8676 "pluggable_data": {
8677 "field_id": "gender",
8678 "user": {
8679 "wp_user_id": 114,
8680 "user_login": "test",
8681 "display_name": "test",
8682 "user_firstname": "test",
8683 "user_lastname": "test",
8684 "user_email": "test@test.com",
8685 "user_role": [ "subscriber" ]
8686 }
8687 }
8688 }',
8689 true
8690 );
8691 }
8692 } else {
8693 $response = json_decode(
8694 '{
8695 "response_type": "sample",
8696 "pluggable_data": {
8697 "field_id": "gender",
8698 "user": {
8699 "wp_user_id": 114,
8700 "user_login": "test",
8701 "display_name": "test",
8702 "user_firstname": "test",
8703 "user_lastname": "test",
8704 "user_email": "test@test.com",
8705 "user_role": [ "subscriber" ]
8706 }
8707 }
8708 }',
8709 true
8710 );
8711 }
8712
8713 return $response;
8714 }
8715
8716 /**
8717 * Get WP Fusion Tags list.
8718 *
8719 * @param array $data data.
8720 *
8721 * @return array
8722 */
8723 public function search_wp_fusion_tag_list( $data ) {
8724
8725 if ( ! function_exists( 'wp_fusion' ) ) {
8726 return [];
8727 }
8728
8729 $options = [];
8730 $tags = wp_fusion()->settings->get( 'available_tags' );
8731
8732 if ( $tags ) {
8733 foreach ( $tags as $t_id => $tag ) {
8734 if ( is_array( $tag ) && isset( $tag['label'] ) ) {
8735 $options[] = [
8736 'value' => $t_id,
8737 'label' => $tag['label'],
8738 ];
8739 } else {
8740 $options[] = [
8741 'value' => $t_id,
8742 'label' => $tag,
8743 ];
8744 }
8745 }
8746 }
8747
8748 return [
8749 'options' => $options,
8750 'hasMore' => false,
8751 ];
8752 }
8753
8754 /**
8755 * Get list of events for Modern Events Calendar.
8756 *
8757 * @param array $data data.
8758 * @return array
8759 */
8760 public function search_mec_events_list( $data ) {
8761 $page = $data['page'];
8762 $limit = Utilities::get_search_page_limit();
8763 $offset = $limit * ( $page - 1 );
8764
8765 $args = [
8766 'post_type' => 'mec-events',
8767 'post_status' => [ 'publish', 'private' ],
8768 ];
8769 $loop = new WP_Query( $args );
8770 $events_count = count( $loop->posts );
8771
8772 $args = [
8773 'post_type' => 'mec-events',
8774 'posts_per_page' => $limit,
8775 'offset' => $offset,
8776 'orderby' => 'title',
8777 'order' => 'ASC',
8778 'post_status' => [ 'publish', 'private' ],
8779 ];
8780
8781 $loop = new WP_Query( $args );
8782 $events = $loop->posts;
8783
8784 $options = [];
8785 if ( ! empty( $events ) ) {
8786 foreach ( $events as $event ) {
8787 if ( isset( $event->ID ) ) {
8788 $options[] = [
8789 'label' => get_the_title( $event ),
8790 'value' => $event->ID,
8791 ];
8792 }
8793 }
8794 }
8795
8796 return [
8797 'options' => $options,
8798 'hasMore' => $events_count > $limit && $events_count > $offset,
8799 ];
8800 }
8801
8802 /**
8803 * Search tickets of MEC events.
8804 *
8805 * @param array $data data.
8806 * @return array
8807 */
8808 public function search_mec_event_tickets( $data ) {
8809 $options = [];
8810 $event_id = $data['dynamic'];
8811
8812 $event_tickets = get_post_meta( $event_id, 'mec_tickets', true );
8813
8814 if ( ! empty( $event_tickets ) && is_array( $event_tickets ) ) {
8815 foreach ( $event_tickets as $ticket_id => $event_ticket ) {
8816 if ( isset( $event_ticket['name'] ) ) {
8817 $options[] = [
8818 'label' => $event_ticket['name'],
8819 'value' => $ticket_id,
8820 ];
8821 }
8822 }
8823 }
8824
8825 return [
8826 'options' => $options,
8827 'hasMore' => false,
8828 ];
8829 }
8830
8831 /**
8832 * Get last data for trigger.
8833 *
8834 * @param array $data data.
8835 * @return array
8836 */
8837 public function search_mec_triggers_last_data( $data ) {
8838 global $wpdb;
8839
8840 $context = [];
8841 $context['response_type'] = 'sample';
8842
8843 $event = [
8844 'event_id' => 1,
8845 'title' => 'Sample Event',
8846 'description' => 'Description of the sample event.',
8847 'categories' => 'New, Sample',
8848 'start_date' => 'September 13, 2023',
8849 'start_time' => '8:00 AM',
8850 'end_date' => 'September 13, 2023',
8851 'end_time' => '11:00 AM',
8852 'location' => 'City Hall',
8853 'organizer' => 'John Doe',
8854 'cost' => '5000',
8855 'featured_image_id' => 1,
8856 'featured_image_url' => 'https://suretriggers.com/wp-content/uploads/2022/12/Screenshot_20221127_021332.png',
8857 'tickets' => [
8858 [
8859 'id' => 1,
8860 'name' => 'Silver',
8861 'description' => 'Standard seat with reasonable price.',
8862 'price' => '300',
8863 'price_label' => 'USD',
8864 'limit' => '20',
8865 ],
8866 [
8867 'id' => 2,
8868 'name' => 'Premium',
8869 'description' => 'VIP seat with high price.',
8870 'price' => '500',
8871 'price_label' => 'USD',
8872 'limit' => '10',
8873 ],
8874 ],
8875 'attendees' => [
8876 [
8877 'id' => 1,
8878 'email' => 'johndoe@test.com',
8879 'name' => 'John Doe',
8880 ],
8881 [
8882 'id' => 2,
8883 'email' => 'adamsmith@test.com',
8884 'name' => 'Adam Smith',
8885 ],
8886 ],
8887 'booking' => [
8888 'title' => 'johndoe@test.com - John Doe',
8889 'transaction_id' => 'RSH59404',
8890 'amount_payable' => '800',
8891 'price' => '800',
8892 'time' => '2023-09-07 06:40:32',
8893 'payment_gateway' => 'Manual Pay',
8894 'confirmation_status' => 'Pending',
8895 'verification_status' => 'Verified',
8896 'attendees_count' => 2,
8897 ],
8898 ];
8899
8900 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
8901
8902 $where = '';
8903
8904 if ( 'cancelled' === $term ) {
8905 $where = 'WHERE verified = -1';
8906 } elseif ( 'confirmed' === $term ) {
8907 $where = 'WHERE confirmed = 1';
8908 } elseif ( 'pending' === $term ) {
8909 $where = 'WHERE confirmed = 0';
8910 }
8911
8912 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
8913
8914 if ( -1 !== $event_id ) {
8915 if ( ! empty( $where ) ) {
8916 $where .= ' AND event_id = ' . $event_id;
8917 } else {
8918 $where = 'WHERE event_id = ' . $event_id;
8919 }
8920 }
8921
8922 $event_data = $wpdb->get_row( "SELECT booking_id FROM {$wpdb->prefix}mec_bookings $where ORDER BY id DESC LIMIT 1" ); // @phpcs:ignore
8923
8924 if ( ! empty( $event_data ) ) {
8925 $event = ModernEventsCalendar::get_event_context( (int) $event_data->booking_id );
8926 $context['response_type'] = 'live';
8927 }
8928
8929 $context['pluggable_data'] = $event;
8930
8931 return $context;
8932 }
8933
8934 /**
8935 * Get form list Contact Form 7.
8936 *
8937 * @param array $data data.
8938 *
8939 * @return array
8940 */
8941 public function search_contact_form7_list( $data ) {
8942
8943 $page = $data['page'];
8944 $limit = Utilities::get_search_page_limit();
8945 $offset = $limit * ( $page - 1 );
8946
8947 $posts = get_posts(
8948 [
8949 'post_type' => 'wpcf7_contact_form',
8950 'posts_per_page' => $limit,
8951 'offset' => $offset,
8952 ]
8953 );
8954
8955 $all_posts = get_posts(
8956 [
8957 'post_type' => 'wpcf7_contact_form',
8958 'posts_per_page' => -1,
8959 ]
8960 );
8961
8962 $posts_count = count( $all_posts );
8963
8964 $options = [];
8965
8966 if ( ! empty( $posts ) ) {
8967 foreach ( $posts as $post ) {
8968 $options[] = [
8969 'label' => get_the_title( $post->ID ),
8970 'value' => $post->ID,
8971 ];
8972 }
8973 }
8974
8975 return [
8976 'options' => $options,
8977 'hasMore' => $posts_count > $limit && $posts_count > $offset,
8978 ];
8979 }
8980
8981 /**
8982 * Get Thrive Leads form list
8983 *
8984 * @param array $data data.
8985 *
8986 * @return array
8987 */
8988 public function search_thrive_leads_forms_list( $data ) {
8989 $options = [];
8990
8991 $lg_ids = get_posts(
8992 [
8993 'post_type' => '_tcb_form_settings',
8994 'fields' => 'id=>parent',
8995 'posts_per_page' => -1,
8996 'post_status' => 'any',
8997 ]
8998 );
8999
9000 if ( function_exists( 'tve_leads_get_form_variations' ) ) {
9001 foreach ( $lg_ids as $lg_id => $lg_parent ) {
9002 $variations = tve_leads_get_form_variations( $lg_parent );
9003 foreach ( $variations as $variation ) {
9004 $options[] = [
9005 'label' => $variation['post_title'],
9006 'value' => $lg_parent,
9007 ];
9008 }
9009 }
9010 }
9011
9012 return [
9013 'options' => $options,
9014 'hasMore' => false,
9015 ];
9016 }
9017
9018 /**
9019 * Get list for Woocommerce Subscriptions
9020 *
9021 * @param array $data data.
9022 *
9023 * @return array
9024 */
9025 public function search_wc_subscription_variation_products( $data ) {
9026 $options = [];
9027
9028 global $wpdb;
9029
9030 if ( ! function_exists( 'wc_get_product' ) ) {
9031 return [];
9032 }
9033 $subscriptions = $wpdb->get_results(
9034 $wpdb->prepare(
9035 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
9036 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
9037 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
9038 AND posts.post_type = %s
9039 AND posts.post_status = %s
9040 UNION ALL
9041 SELECT ID, post_title FROM $wpdb->posts
9042 WHERE post_type = %s
9043 AND post_status = %s
9044 ORDER BY post_title",
9045 'product',
9046 'publish',
9047 'shop_subscription',
9048 'publish'
9049 )
9050 );
9051
9052 if ( $subscriptions ) {
9053 foreach ( $subscriptions as $product ) {
9054 $options[] = [
9055 'label' => $product->post_title . ' (#' . $product->ID . ')',
9056 'value' => (int) $product->ID,
9057 ];
9058 $product_s = wc_get_product( $product->ID );
9059 /**
9060 *
9061 * Ignore line
9062 *
9063 * @phpstan-ignore-next-line
9064 */
9065 if ( 'variable-subscription' == $product_s->product_type ) {
9066 $args = [
9067 'post_type' => 'product_variation',
9068 'post_parent' => $product->ID,
9069 'posts_per_page' => -1,
9070 'orderby' => 'ID',
9071 'order' => 'ASC',
9072 'post_status' => 'publish',
9073 ];
9074
9075 $variations = get_posts( $args );
9076
9077 foreach ( $variations as $var ) {
9078 $options[] = [
9079 'label' => $var->post_title . ' (#' . $var->ID . ')',
9080 'value' => $var->ID,
9081 ];
9082 }
9083 }
9084 }
9085 }
9086
9087 return [
9088 'options' => $options,
9089 'hasMore' => false,
9090 ];
9091 }
9092
9093 /**
9094 * Get WS Forms form list
9095 *
9096 * @param array $data data.
9097 *
9098 * @return array
9099 */
9100 public function search_ws_forms_list( $data ) {
9101 $options = [];
9102 global $wpdb;
9103
9104 $forms = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wsf_form", 'ARRAY_A' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
9105
9106 foreach ( $forms as $form ) {
9107 $options[] = [
9108 'label' => $form['label'],
9109 'value' => $form['id'],
9110 ];
9111 }
9112
9113 return [
9114 'options' => $options,
9115 'hasMore' => false,
9116 ];
9117 }
9118
9119 /**
9120 * Get Learndash Achievement list
9121 *
9122 * @param array $data data.
9123 *
9124 * @return array
9125 */
9126 public function search_ld_achievements_list( $data ) {
9127 $page = $data['page'];
9128 $limit = Utilities::get_search_page_limit();
9129 $offset = $limit * ( $page - 1 );
9130
9131 $posts = get_posts(
9132 [
9133 'post_type' => 'ld-achievement',
9134 'posts_per_page' => $limit,
9135 'offset' => $offset,
9136 'post_status' => [ 'publish' ],
9137 ]
9138 );
9139
9140 $posts_count = wp_count_posts( 'ld-achievement' )->publish;
9141
9142 $options = [];
9143
9144 if ( ! empty( $posts ) ) {
9145 foreach ( $posts as $post ) {
9146 $options[] = [
9147 'label' => get_the_title( $post->ID ),
9148 'value' => $post->ID,
9149 ];
9150 }
9151 }
9152
9153 return [
9154 'options' => $options,
9155 'hasMore' => $posts_count > $limit && $posts_count > $offset,
9156 ];
9157 }
9158
9159 /**
9160 * Get Advanced Ads list
9161 *
9162 * @param array $data data.
9163 *
9164 * @return array
9165 */
9166 public function search_ads_list( $data ) {
9167 $page = $data['page'];
9168 $limit = Utilities::get_search_page_limit();
9169 $offset = $limit * ( $page - 1 );
9170
9171 $posts = get_posts(
9172 [
9173 'post_type' => 'advanced_ads',
9174 'posts_per_page' => $limit,
9175 'offset' => $offset,
9176 'post_status' => [ 'publish', 'draft' ],
9177 ]
9178 );
9179
9180 $posts_count = wp_count_posts( 'advanced_ads' )->publish;
9181
9182 $options = [];
9183
9184 if ( ! empty( $posts ) ) {
9185 foreach ( $posts as $post ) {
9186 $options[] = [
9187 'label' => get_the_title( $post->ID ),
9188 'value' => $post->ID,
9189 ];
9190 }
9191 }
9192
9193 return [
9194 'options' => $options,
9195 'hasMore' => $posts_count > $limit && $posts_count > $offset,
9196 ];
9197 }
9198
9199 /**
9200 * Advanced Ads pluggable data.
9201 *
9202 * @param array $data data.
9203 * @return array
9204 */
9205 public function search_ad_last_data( $data ) {
9206 $context = [];
9207 $args = [
9208 'post_type' => 'advanced_ads',
9209 'posts_per_page' => 1,
9210 'orderby' => 'modified',
9211 'order' => 'DESC',
9212 ];
9213
9214 if ( isset( $data['filter']['ad_status']['value'] ) ) {
9215 $post_status = $data['filter']['ad_status']['value'];
9216 $args['post_status'] = $post_status;
9217 }
9218
9219 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
9220 $post_status = $data['filter']['ad_new_status']['value'];
9221 $args['post_status'] = $post_status;
9222 }
9223
9224 if ( isset( $data['filter']['ad_id']['value'] ) ) {
9225 $post_id = $data['filter']['ad_id']['value'];
9226 if ( -1 != $post_id ) {
9227 if ( $post_id > 0 ) {
9228 $args['p'] = $post_id;
9229 }
9230 }
9231 }
9232
9233 $posts = get_posts( $args );
9234 if ( ! empty( $posts ) ) {
9235 $context['pluggable_data'] = $posts[0];
9236 $context['pluggable_data'] = WordPress::get_post_context( $posts[0]->ID );
9237 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
9238 $context['pluggable_data']['ad_new_status'] = $posts[0]->post_status;
9239 }
9240 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
9241 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
9242 }
9243 $context['pluggable_data']['ad_id'] = $posts[0]->ID;
9244 if ( isset( $data['filter']['ad_status']['value'] ) ) {
9245 $context['pluggable_data']['ad_status'] = $posts[0]->post_status;
9246 }
9247 $context['response_type'] = 'live';
9248 } else {
9249 $context['pluggable_data'] = [
9250 'ID' => 1,
9251 'post' => 1,
9252 'post_author' => 1,
9253 'post_date' => '2022-11-18 12:18:14',
9254 'post_date_gmt' => '2022-11-18 12:18:14',
9255 'post_content' => 'Ad Post Content',
9256 'post_title' => 'Ad Post',
9257 'post_excerpt' => '',
9258 'post_status' => 'draft',
9259 'comment_status' => 'open',
9260 'ping_status' => 'open',
9261 'post_password' => '',
9262 'post_name' => 'ad-post',
9263 'to_ping' => '',
9264 'pinged' => '',
9265 'post_modified' => '2022-11-18 12:18:14',
9266 'post_modified_gmt' => '2022-11-18 12:18:14',
9267 'post_content_filtered' => '',
9268 'post_parent' => 0,
9269 'guid' => 'https://example.com/ad-post/',
9270 'menu_order' => 0,
9271 'post_type' => 'advanced_ads',
9272 'post_mime_type' => '',
9273 'comment_count' => 0,
9274 'filter' => 'raw',
9275 ];
9276 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
9277 $context['pluggable_data']['ad_new_status'] = $data['filter']['ad_new_status']['value'];
9278 }
9279 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
9280 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
9281 }
9282 $context['pluggable_data']['ad_id'] = 1;
9283 if ( isset( $data['filter']['ad_status']['value'] ) ) {
9284 $context['pluggable_data']['ad_status'] = $data['filter']['ad_status']['value'];
9285 }
9286 $context['response_type'] = 'sample';
9287 }
9288
9289 return $context;
9290 }
9291
9292 /**
9293 * Get Newsletter lists
9294 *
9295 * @param array $data data.
9296 *
9297 * @return array
9298 */
9299 public function search_newsletter_lists( $data ) {
9300
9301 $options = [];
9302 if ( class_exists( '\Newsletter' ) ) {
9303
9304 $lists = \Newsletter::instance()->get_lists();
9305
9306 if ( ! empty( $lists ) ) {
9307 foreach ( $lists as $list ) {
9308 $options[] = [
9309 'label' => $list->name,
9310 'value' => 'list_' . $list->id,
9311 ];
9312 }
9313 }
9314 }
9315
9316 return [
9317 'options' => $options,
9318 'hasMore' => false,
9319 ];
9320 }
9321
9322 /**
9323 * Newsletter pluggable data.
9324 *
9325 * @param array $data data.
9326 * @return array
9327 */
9328 public function search_newsletter_last_data( $data ) {
9329 $context = [];
9330 global $wpdb;
9331
9332 $list = $data['filter']['list_id']['value'];
9333
9334 if ( -1 == $list ) {
9335 $log = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . "newsletter where status='C' ORDER BY id DESC LIMIT 1" );
9336 } else {
9337 $num = $list;
9338 $location = 1;
9339 $sql = 'SELECT * FROM ' . $wpdb->prefix . "newsletter WHERE $num = %d AND status = 'C' ORDER BY id DESC LIMIT 1";
9340 $log = $wpdb->get_results( $wpdb->prepare( $sql, $location ), ARRAY_A );// @phpcs:ignore
9341 }
9342
9343 if ( ! empty( $log ) ) {
9344 $lists_arr = get_option( 'newsletter_lists' );
9345 if ( -1 == $list ) {
9346 foreach ( $log[0] as $key => $val ) {
9347 if ( defined( 'NEWSLETTER_LIST_MAX' ) ) {
9348 for ( $i = 1; $i <= NEWSLETTER_LIST_MAX; $i++ ) {
9349 $list_key = "list_$i";
9350
9351 if ( $key == $list_key ) {
9352 if ( 1 == $val ) {
9353 $context['pluggable_data']['list_id'] = $key;
9354 if ( is_array( $lists_arr ) ) {
9355 if ( isset( $lists_arr[ $key ] ) ) {
9356 $list_name = $lists_arr[ $key ];
9357 $context['pluggable_data']['list_name'] = $list_name;
9358 }
9359 }
9360 continue;
9361 }
9362 }
9363 }
9364 }
9365 if ( 'email' == $key ) {
9366 $context['pluggable_data']['email'] = $val;
9367 }
9368 }
9369 } else {
9370 $context['pluggable_data']['list_id'] = $list;
9371 $context['pluggable_data']['email'] = $log[0]['email'];
9372 if ( is_array( $lists_arr ) ) {
9373 if ( isset( $lists_arr[ $list ] ) ) {
9374 $list_name = $lists_arr[ $list ];
9375 $context['pluggable_data']['list_name'] = $list_name;
9376 }
9377 }
9378 }
9379 $context['response_type'] = 'live';
9380 } else {
9381 $context = json_decode( '{"response_type":"sample","pluggable_data":{"list_id": "list_1","email": "johnd@mailinator.com","list_name": "Contact List"}}', true );
9382 }
9383
9384 return $context;
9385 }
9386
9387 /**
9388 * Get wpForo Forum list
9389 *
9390 * @param array $data data.
9391 *
9392 * @return array
9393 */
9394 public function search_wp_forum_list( $data ) {
9395
9396 if ( ! function_exists( 'WPF' ) ) {
9397 return [];
9398 }
9399
9400 $forums = WPF()->forum->get_forums( [ 'type' => 'forum' ] );
9401
9402 $options = [];
9403
9404 if ( ! empty( $forums ) ) {
9405 foreach ( $forums as $forum ) {
9406 $options[] = [
9407 'label' => $forum['title'],
9408 'value' => $forum['forumid'],
9409 ];
9410 }
9411 }
9412
9413 return [
9414 'options' => $options,
9415 'hasMore' => false,
9416 ];
9417 }
9418
9419 /**
9420 * Get wpForo Topic list
9421 *
9422 * @param array $data data.
9423 *
9424 * @return array
9425 */
9426 public function search_wp_topic_list( $data ) {
9427
9428 if ( ! function_exists( 'WPF' ) ) {
9429 return [];
9430 }
9431
9432 $forum_id = $data['dynamic'];
9433
9434 $topics = WPF()->topic->get_topics( [ 'forumid' => $forum_id ] );
9435
9436 $options = [];
9437
9438 if ( ! empty( $topics ) ) {
9439 foreach ( $topics as $topic ) {
9440 $options[] = [
9441 'label' => $topic['title'],
9442 'value' => $topic['topicid'],
9443 ];
9444 }
9445 }
9446
9447 return [
9448 'options' => $options,
9449 'hasMore' => false,
9450 ];
9451 }
9452
9453 /**
9454 * Get wpForo Groups list
9455 *
9456 * @param array $data data.
9457 *
9458 * @return array
9459 */
9460 public function search_wp_user_groups_list( $data ) {
9461
9462 if ( ! function_exists( 'WPF' ) ) {
9463 return [];
9464 }
9465
9466 $usergroups = WPF()->usergroup->get_usergroups();
9467
9468 $options = [];
9469
9470 if ( ! empty( $usergroups ) ) {
9471 foreach ( $usergroups as $group ) {
9472 $options[] = [
9473 'label' => $group['name'],
9474 'value' => intval( $group['groupid'] ),
9475 ];
9476 }
9477 }
9478
9479 return [
9480 'options' => $options,
9481 'hasMore' => false,
9482 ];
9483 }
9484
9485 /**
9486 * Get wpForo Reputation list
9487 *
9488 * @param array $data data.
9489 *
9490 * @return array
9491 */
9492 public function search_wp_foro_reputation_list( $data ) {
9493
9494 if ( ! function_exists( 'WPF' ) ) {
9495 return [];
9496 }
9497
9498 $levels = WPF()->member->levels();
9499 $options = [];
9500
9501 if ( ! empty( $levels ) ) {
9502 foreach ( $levels as $level ) {
9503 $options[] = [
9504 'label' => esc_attr__( 'Level', 'suretriggers' ) . ' ' . $level . ' - ' . WPF()->member->rating( $level, 'title' ),
9505 'value' => strval( $level ),
9506 ];
9507 }
9508 }
9509
9510 return [
9511 'options' => $options,
9512 'hasMore' => false,
9513 ];
9514 }
9515
9516 /**
9517 * WPForo new topic pluggable data.
9518 *
9519 * @param array $data data.
9520 * @return array
9521 */
9522 public function search_pluggables_wpforo_topic_last_data( $data ) {
9523 $context = [];
9524 global $wpdb;
9525
9526 $forum_id = $data['filter']['forum_id']['value'];
9527
9528 if ( -1 == $forum_id ) {
9529 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'wpforo_topics WHERE closed = 0 ORDER BY topicid DESC LIMIT 1', ARRAY_A );
9530 } else {
9531 $forum = $forum_id;
9532 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_topics WHERE forumid = %d AND closed = 0 ORDER BY topicid DESC LIMIT 1';
9533 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum ), ARRAY_A );// @phpcs:ignore
9534 }
9535
9536 if ( ! empty( $results ) ) {
9537 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
9538 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
9539
9540 if ( function_exists( 'WPF' ) ) {
9541 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
9542 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
9543 }
9544 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
9545 $context['response_type'] = 'live';
9546 } else {
9547 $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
9548 }
9549
9550 return $context;
9551 }
9552
9553 /**
9554 * WpForo Topic Reply pluggable data.
9555 *
9556 * @param array $data data.
9557 * @return array
9558 */
9559 public function search_pluggables_wpforo_topic_reply_last_data( $data ) {
9560 $context = [];
9561 global $wpdb;
9562
9563 $forum_id = $data['filter']['forum_id']['value'];
9564 $topic_id = $data['filter']['topic_id']['value'];
9565
9566 if ( -1 == $forum_id && -1 != $topic_id ) {
9567 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE topicid = %d ORDER BY postid DESC LIMIT 1';
9568 $results = $wpdb->get_results( $wpdb->prepare( $sql, $topic_id ), ARRAY_A );// @phpcs:ignore
9569 } elseif ( -1 != $forum_id && -1 == $topic_id ) {
9570 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d ORDER BY postid DESC LIMIT 1';
9571 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id ), ARRAY_A );// @phpcs:ignore
9572 } elseif ( -1 == $forum_id && -1 == $topic_id ) {
9573 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts ORDER BY postid DESC LIMIT 1';
9574 $results = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );// @phpcs:ignore
9575 } else {
9576 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d AND topicid = %d ORDER BY postid DESC LIMIT 1';
9577 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id, $topic_id ), ARRAY_A );// @phpcs:ignore
9578 }
9579
9580 if ( ! empty( $results ) ) {
9581 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
9582 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
9583 if ( function_exists( 'WPF' ) ) {
9584 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
9585 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
9586 $context['pluggable_data']['reply'] = WPF()->post->get_post( $results[0]['postid'] );
9587 }
9588 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
9589 $context['response_type'] = 'live';
9590 } else {
9591 $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
9592 }
9593
9594 return $context;
9595 }
9596
9597 /**
9598 * Get RafflePress Giveaways list
9599 *
9600 * @param array $data data.
9601 *
9602 * @return array
9603 */
9604 public function search_rp_giveaways_list( $data ) {
9605
9606 global $wpdb;
9607 $options = [];
9608
9609 $giveaways = $wpdb->get_results( "SELECT id,name FROM {$wpdb->prefix}rafflepress_giveaways WHERE deleted_at is null ORDER BY name ASC", ARRAY_A );
9610 foreach ( $giveaways as $giveaway ) {
9611 $options[] = [
9612 'label' => $giveaway['name'],
9613 'value' => $giveaway['id'],
9614 ];
9615 }
9616
9617 return [
9618 'options' => $options,
9619 'hasMore' => false,
9620 ];
9621 }
9622
9623 /**
9624 * Get RafflePress Giveaway Actions list
9625 *
9626 * @param array $data data.
9627 *
9628 * @return array
9629 */
9630 public function search_rp_giveaway_actions_list( $data ) {
9631
9632 global $wpdb;
9633 $options = [];
9634
9635 $giveaway = $wpdb->get_row( $wpdb->prepare( "SELECT settings FROM {$wpdb->prefix}rafflepress_giveaways WHERE id=%d", $data['dynamic'] ), ARRAY_A );
9636
9637 if ( is_array( $giveaway ) && isset( $giveaway['settings'] ) ) {
9638 $settings = json_decode( $giveaway['settings'], true );
9639
9640 if ( is_array( $settings ) && isset( $settings['entry_options'] ) ) {
9641 foreach ( $settings['entry_options'] as $action ) {
9642 $options[] = [
9643 'label' => $action['name'],
9644 'value' => $action['id'],
9645 ];
9646 }
9647 }
9648 }
9649
9650 return [
9651 'options' => $options,
9652 'hasMore' => false,
9653 ];
9654 }
9655
9656 /**
9657 * Get last data for trigger.
9658 *
9659 * @param array $data data.
9660 * @return array
9661 */
9662 public function search_raffle_press_triggers_last_data( $data ) {
9663
9664 global $wpdb;
9665
9666 $context = [];
9667 $context['response_type'] = 'sample';
9668
9669 $pluggable_data = [
9670 'giveaway_id' => 1,
9671 'giveaway_title' => 'Sample Giveaway (ID#1)',
9672 'giveaway_start_date' => 'September 20, 2023',
9673 'giveaway_end_date' => 'October 6, 2023',
9674 'giveaway_entries' => 9,
9675 'giveaway_user_count' => 3,
9676 'giveaway_status' => 'Active',
9677 'contestant_name' => 'John Doe',
9678 'contestant_email' => 'john_doe@gmail.com',
9679 'contestant_email_verified' => 'Yes',
9680 'action_id' => '0jnex',
9681 'action_name' => 'Visit us on Instagram',
9682 ];
9683
9684 $giveaway_id = isset( $data['filter']['giveaway_id'] ) ? $data['filter']['giveaway_id']['value'] : null;
9685 $action_id = isset( $data['filter']['action_id'] ) ? $data['filter']['action_id']['value'] : null;
9686
9687 $query = "SELECT contestant_id, giveaway_id, action_id, meta FROM {$wpdb->prefix}rafflepress_entries";
9688
9689 if ( $giveaway_id && -1 != $giveaway_id ) {
9690 $query .= ' WHERE giveaway_id = ' . $giveaway_id;
9691
9692 if ( $action_id ) {
9693 $query .= " AND action_id = '" . $action_id . "'";
9694 }
9695 }
9696
9697 $query .= ' ORDER BY created_at DESC LIMIT 1';
9698
9699 $giveaway_data = $wpdb->get_row( $query, ARRAY_A ); // @phpcs:ignore
9700
9701 if ( ! empty( $giveaway_data ) ) {
9702 $pluggable_data = array_merge(
9703 RafflePress::get_giveaway_context( $giveaway_data['giveaway_id'] ),
9704 RafflePress::get_contestant_context( $giveaway_data['contestant_id'] )
9705 );
9706
9707 $pluggable_data['performed_action_id'] = isset( $giveaway_data['action_id'] ) ? $giveaway_data['action_id'] : 0;
9708
9709 $giveaway_meta = isset( $giveaway_data['meta'] ) ? json_decode( $giveaway_data['meta'], true ) : [];
9710 $pluggable_data['performed_action_name'] = is_array( $giveaway_meta ) && isset( $giveaway_meta['action'] ) ? $giveaway_meta['action'] : '';
9711
9712 $context['response_type'] = 'live';
9713 }
9714
9715 $context['pluggable_data'] = $pluggable_data;
9716
9717 return $context;
9718 }
9719
9720 /**
9721 * Get last data for trigger
9722 *
9723 * @param array $data data.
9724 * @return array
9725 */
9726 public function search_woo_commerce_shipstation_triggers_last_data( $data ) {
9727 $context = [];
9728 $context['response_type'] = 'sample';
9729 $context['pluggable_data'] = [];
9730
9731 $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
9732
9733 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
9734 $condition = $data['filter']['condition_compare']['value'];
9735 $price = $data['filter']['price']['value'];
9736 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
9737
9738 if ( 'order_shipped' === $term ) {
9739 $orders = wc_get_orders(
9740 [
9741 'numberposts' => 1,
9742 'orderby' => 'date',
9743 'order' => 'DESC',
9744 'status' => 'completed',
9745 'meta_query' => [
9746 [
9747 'key' => '_shipstation_shipped_item_count',
9748 'compare' => 'EXISTS',
9749 ],
9750 ],
9751 ]
9752 );
9753 if ( count( $orders ) > 0 ) {
9754 $order_id = $orders[0]->get_id();
9755 $order = wc_get_order( $order_id );
9756 $user_id = $order->get_customer_id();
9757 $product_ids = [];
9758 if ( $order ) {
9759 $items = $order->get_items();
9760 foreach ( $items as $item ) {
9761 if ( method_exists( $item, 'get_product_id' ) ) {
9762 $product_ids[] = $item->get_product_id();
9763 }
9764 }
9765
9766 foreach ( $product_ids as $product_id ) {
9767 $context['product_id'] = $product_id;
9768 }
9769 }
9770 $context = array_merge(
9771 WooCommerce::get_order_context( $order_id ),
9772 WordPress::get_user_context( $user_id )
9773 );
9774 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
9775 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
9776 /**
9777 *
9778 * Ignore line
9779 *
9780 * @phpstan-ignore-next-line
9781 */
9782 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
9783 /**
9784 *
9785 * Ignore line
9786 *
9787 * @phpstan-ignore-next-line
9788 */
9789 $date = date_i18n( get_option( 'date_format' ), $timestamp );
9790 $context['ship_date'] = $date;
9791 $context['response_type'] = 'live';
9792 }
9793
9794 $context['pluggable_data'] = $context;
9795 } elseif ( 'specific_product_order_shipped' === $term ) {
9796 if ( -1 != $product_id ) {
9797 $product_ids = [ $product_id ];
9798 $orders = wc_get_orders(
9799 [
9800 'numberposts' => 1,
9801 'orderby' => 'date',
9802 'order' => 'DESC',
9803 'status' => 'completed',
9804 'meta_query' => [
9805 [
9806 'key' => '_shipstation_shipped_item_count',
9807 'compare' => 'EXISTS',
9808 ],
9809 [
9810 'key' => '_product_id',
9811 'value' => $product_ids,
9812 'compare' => 'IN',
9813 ],
9814 ],
9815 ]
9816 );
9817 } else {
9818 $orders = wc_get_orders(
9819 [
9820 'numberposts' => 1,
9821 'orderby' => 'date',
9822 'order' => 'DESC',
9823 'status' => 'completed',
9824 'meta_query' => [
9825 [
9826 'key' => '_shipstation_shipped_item_count',
9827 'compare' => 'EXISTS',
9828 ],
9829 ],
9830 ]
9831 );
9832 }
9833 if ( count( $orders ) > 0 ) {
9834 $order_id = $orders[0]->get_id();
9835 $order = wc_get_order( $order_id );
9836 $user_id = $order->get_customer_id();
9837 $productids = [];
9838 $items = $order->get_items();
9839 foreach ( $items as $item ) {
9840 if ( method_exists( $item, 'get_product_id' ) ) {
9841 $productids[] = $item->get_product_id();
9842 }
9843 }
9844 $context = array_merge(
9845 WooCommerce::get_order_context( $order_id ),
9846 WordPress::get_user_context( $user_id )
9847 );
9848
9849 foreach ( $productids as $product_id ) {
9850 $context['product_id'] = $product_id;
9851 }
9852
9853 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
9854 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
9855 /**
9856 *
9857 * Ignore line
9858 *
9859 * @phpstan-ignore-next-line
9860 */
9861 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
9862 /**
9863 *
9864 * Ignore line
9865 *
9866 * @phpstan-ignore-next-line
9867 */
9868 $date = date_i18n( get_option( 'date_format' ), $timestamp );
9869 $context['ship_date'] = $date;
9870 $context['response_type'] = 'live';
9871 }
9872
9873 $context['pluggable_data'] = $context;
9874 } elseif ( 'specific_amount_order_shipped' === $term ) {
9875 $orders = wc_get_orders(
9876 [
9877 'numberposts' => 1,
9878 'orderby' => 'date',
9879 'order' => 'DESC',
9880 'status' => 'completed',
9881 'meta_query' => [
9882 [
9883 'key' => '_shipstation_shipped_item_count',
9884 'compare' => 'EXISTS',
9885 ],
9886 [
9887 'key' => '_order_total',
9888 'value' => $price,
9889 'compare' => $condition,
9890 ],
9891 ],
9892 ]
9893 );
9894 if ( count( $orders ) > 0 ) {
9895 $order_id = $orders[0]->get_id();
9896 $order = wc_get_order( $order_id );
9897 $user_id = $order->get_customer_id();
9898 $product_ids = [];
9899 $items = $order->get_items();
9900 foreach ( $items as $item ) {
9901 if ( method_exists( $item, 'get_product_id' ) ) {
9902 $product_ids[] = $item->get_product_id();
9903 }
9904 }
9905 $context = array_merge(
9906 WooCommerce::get_order_context( $order_id ),
9907 WordPress::get_user_context( $user_id )
9908 );
9909
9910 foreach ( $product_ids as $product_id ) {
9911 $context['product_id'] = $product_id;
9912 }
9913
9914 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
9915 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
9916 /**
9917 *
9918 * Ignore line
9919 *
9920 * @phpstan-ignore-next-line
9921 */
9922 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
9923 /**
9924 *
9925 * Ignore line
9926 *
9927 * @phpstan-ignore-next-line
9928 */
9929 $date = date_i18n( get_option( 'date_format' ), $timestamp );
9930 $context['ship_date'] = $date;
9931 $context['response_type'] = 'live';
9932 }
9933
9934 $context['pluggable_data'] = $context;
9935 }
9936
9937 return $context;
9938 }
9939
9940 /**
9941 * Get GroundHogg Tag list
9942 *
9943 * @param array $data data.
9944 *
9945 * @return array
9946 */
9947 public function search_groundhogg_tag_list( $data ) {
9948 if ( ! class_exists( '\Groundhogg\DB\Tags' ) ) {
9949 return [];
9950 }
9951
9952 $tags = new \Groundhogg\DB\Tags();
9953 $options = [];
9954
9955 if ( ! empty( $tags->get_tags() ) ) {
9956 foreach ( $tags->get_tags() as $tag ) {
9957 $options[] = [
9958 'label' => $tag->tag_name,
9959 'value' => $tag->tag_id,
9960 ];
9961 }
9962 }
9963
9964 return [
9965 'options' => $options,
9966 'hasMore' => false,
9967 ];
9968 }
9969
9970 /**
9971 * Get WP courseware courses list
9972 *
9973 * @param array $data data.
9974 *
9975 * @return array
9976 */
9977 public function search_wpcw_courses( $data ) {
9978 $options = [];
9979
9980 $page = $data['page'];
9981 $limit = Utilities::get_search_page_limit();
9982 $offset = $limit * ( $page - 1 );
9983
9984 $args = [
9985 'post_type' => 'wpcw_course',
9986 'posts_per_page' => $limit,
9987 'offset' => $offset,
9988 'orderby' => 'title',
9989 'order' => 'ASC',
9990 'post_status' => 'publish',
9991 ];
9992
9993 $courses = get_posts( $args );
9994
9995 $course_count = wp_count_posts( 'wpcw_course' )->publish;
9996
9997 if ( ! empty( $courses ) ) {
9998 if ( is_array( $courses ) ) {
9999 foreach ( $courses as $course ) {
10000 $options[] = [
10001 'label' => $course->post_title,
10002 'value' => $course->ID,
10003 ];
10004 }
10005 }
10006 }
10007 return [
10008 'options' => $options,
10009 'hasMore' => $course_count > $limit && $course_count > $offset,
10010 ];
10011 }
10012
10013 /**
10014 * Get WP courseware courses list
10015 *
10016 * @param array $data data.
10017 *
10018 * @return array
10019 */
10020 public function search_wpcw_modules( $data ) {
10021 $options = [];
10022
10023 if ( function_exists( 'wpcw_get_modules' ) ) {
10024 $modules = wpcw_get_modules();
10025 }
10026
10027 if ( ! empty( $modules ) ) {
10028 if ( is_array( $modules ) ) {
10029 foreach ( $modules as $module ) {
10030 $options[] = [
10031 'label' => $module->module_title,
10032 'value' => $module->module_id,
10033 ];
10034 }
10035 }
10036 }
10037 return [
10038 'options' => $options,
10039 'hasMore' => false,
10040 ];
10041 }
10042
10043 /**
10044 * Get WP courseware unit list
10045 *
10046 * @param array $data data.
10047 *
10048 * @return array
10049 */
10050 public function search_wpcw_units( $data ) {
10051 $options = [];
10052
10053 $page = $data['page'];
10054 $limit = Utilities::get_search_page_limit();
10055 $offset = $limit * ( $page - 1 );
10056
10057 $args = [
10058 'post_type' => 'course_unit',
10059 'posts_per_page' => $limit,
10060 'offset' => $offset,
10061 'orderby' => 'title',
10062 'order' => 'ASC',
10063 'post_status' => 'publish',
10064 ];
10065
10066 $units = get_posts( $args );
10067
10068 $unit_count = wp_count_posts( 'course_unit' )->publish;
10069
10070 if ( ! empty( $units ) ) {
10071 if ( is_array( $units ) ) {
10072 foreach ( $units as $unit ) {
10073 $options[] = [
10074 'label' => $unit->post_title,
10075 'value' => $unit->ID,
10076 ];
10077 }
10078 }
10079 }
10080 return [
10081 'options' => $options,
10082 'hasMore' => $unit_count > $limit && $unit_count > $offset,
10083 ];
10084 }
10085
10086 /**
10087 * Search WP Courseware data.
10088 *
10089 * @param array $data data.
10090 * @return array|void
10091 */
10092 public function search_wpcw_last_data( $data ) {
10093 global $wpdb;
10094 $post_type = $data['post_type'];
10095 $trigger = $data['search_term'];
10096 $context = [];
10097
10098 if ( 'wpcw_course_completed' === $trigger ) {
10099 $post_id = $data['filter']['course_post_id']['value'];
10100 if ( -1 === $post_id ) {
10101 $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" );
10102 } else {
10103 $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 ) );
10104 }
10105 } elseif ( 'wpcw_module_completed' === $trigger ) {
10106 $post_id = $data['filter']['module_id']['value'];
10107 if ( -1 === $post_id ) {
10108 $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" );
10109 } else {
10110 $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 ) );
10111 }
10112 } elseif ( 'wpcw_unit_completed' === $trigger ) {
10113 $post_id = $data['filter']['unit_id']['value'];
10114 if ( -1 === $post_id ) {
10115 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpcw_user_progress WHERE unit_completed_status='complete' order by unit_id DESC LIMIT 1" );
10116 } else {
10117 $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 ) );
10118 }
10119 } elseif ( 'wpcw_enroll_course' === $trigger ) {
10120 $post_id = $data['filter']['course_post_id']['value'];
10121 if ( -1 === $post_id ) {
10122 $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" );
10123 } else {
10124 $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 ) );
10125 }
10126 }
10127
10128 if ( ! empty( $result ) ) {
10129
10130 switch ( $trigger ) {
10131 case 'wpcw_course_completed':
10132 $result_course_id = $result[0]->course_id;
10133 $result_user_id = $result[0]->user_id;
10134 if ( function_exists( 'wpcw_get_course' ) ) {
10135 $course = wpcw_get_course( $result_course_id );
10136 if ( is_object( $course ) ) {
10137 $course = get_object_vars( $course );
10138 }
10139 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course );
10140 }
10141 break;
10142 case 'wpcw_module_completed':
10143 $result_module_id = $result[0]->module_id;
10144 $result_user_id = $result[0]->user_id;
10145 if ( function_exists( 'wpcw_get_module' ) ) {
10146 $module = wpcw_get_module( $result_module_id );
10147 if ( is_object( $module ) ) {
10148 $module = get_object_vars( $module );
10149 }
10150 $context = array_merge( WordPress::get_user_context( $result_user_id ), $module );
10151 }
10152 break;
10153 case 'wpcw_unit_completed':
10154 $result_unit_id = $result[0]->unit_id;
10155 $result_user_id = $result[0]->user_id;
10156 if ( function_exists( 'wpcw_get_unit' ) ) {
10157 $unit = wpcw_get_unit( $result_unit_id );
10158 if ( is_object( $unit ) ) {
10159 $unit = get_object_vars( $unit );
10160 $unit['name'] = get_the_title( $result_unit_id );
10161 }
10162 $context = array_merge( WordPress::get_user_context( $result_user_id ), $unit );
10163 }
10164 break;
10165 case 'wpcw_enroll_course':
10166 $result_course_id = $result[0]->course_id;
10167 $result_user_id = $result[0]->user_id;
10168 if ( function_exists( 'WPCW_courses_getCourseDetails' ) ) {
10169 $course_detail = WPCW_courses_getCourseDetails( $result_course_id );
10170 if ( is_object( $course_detail ) ) {
10171 $course_detail = get_object_vars( $course_detail );
10172 }
10173 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course_detail );
10174 }
10175 break;
10176 default:
10177 return;
10178 }
10179 $context['pluggable_data'] = $context;
10180 $context['response_type'] = 'live';
10181 }
10182
10183 return $context;
10184
10185 }
10186
10187 /**
10188 * Get WooCommerce Order Note list.
10189 *
10190 * @param array $data data.
10191 *
10192 * @return array
10193 */
10194 public function search_note_type_list( $data ) {
10195
10196 $options = [];
10197
10198 $options[] = [
10199 'label' => 'Customer',
10200 'value' => 'customer',
10201 ];
10202 $options[] = [
10203 'label' => 'Private',
10204 'value' => 'internal',
10205 ];
10206
10207 return [
10208 'options' => $options,
10209 'hasMore' => false,
10210 ];
10211 }
10212
10213 /**
10214 * Get last data for trigger
10215 *
10216 * @param array $data data.
10217 * @return array
10218 */
10219 public function search_woo_commerce_customers_triggers_last_data( $data ) {
10220 $context = [];
10221 $context['response_type'] = 'sample';
10222 $context['pluggable_data'] = [];
10223 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
10224 global $wpdb;
10225 if ( 'customer_created' === $term ) {
10226 $customer_query = get_users(
10227 [
10228 'fields' => 'ID',
10229 'role' => 'customer',
10230 'orderby' => 'ID',
10231 'order' => 'DESC',
10232 'number' => 1,
10233 ]
10234 );
10235 $results = $customer_query;
10236 if ( ! empty( $results ) ) {
10237 $customer = new WC_Customer( $results[0] );
10238 $last_order = $customer->get_last_order();
10239 $customer_data = [
10240 'id' => $customer->get_id(),
10241 'email' => $customer->get_email(),
10242 'first_name' => $customer->get_first_name(),
10243 'last_name' => $customer->get_last_name(),
10244 'username' => $customer->get_username(),
10245 'last_order_id' => is_object( $last_order ) ? $last_order->get_id() : null,
10246 'orders_count' => $customer->get_order_count(),
10247 'total_spent' => wc_format_decimal( $customer->get_total_spent(), 2 ),
10248 'avatar_url' => $customer->get_avatar_url(),
10249 'billing_address' => [
10250 'first_name' => $customer->get_billing_first_name(),
10251 'last_name' => $customer->get_billing_last_name(),
10252 'company' => $customer->get_billing_company(),
10253 'address_1' => $customer->get_billing_address_1(),
10254 'address_2' => $customer->get_billing_address_2(),
10255 'city' => $customer->get_billing_city(),
10256 'state' => $customer->get_billing_state(),
10257 'postcode' => $customer->get_billing_postcode(),
10258 'country' => $customer->get_billing_country(),
10259 'email' => $customer->get_billing_email(),
10260 'phone' => $customer->get_billing_phone(),
10261 ],
10262 'shipping_address' => [
10263 'first_name' => $customer->get_shipping_first_name(),
10264 'last_name' => $customer->get_shipping_last_name(),
10265 'company' => $customer->get_shipping_company(),
10266 'address_1' => $customer->get_shipping_address_1(),
10267 'address_2' => $customer->get_shipping_address_2(),
10268 'city' => $customer->get_shipping_city(),
10269 'state' => $customer->get_shipping_state(),
10270 'postcode' => $customer->get_shipping_postcode(),
10271 'country' => $customer->get_shipping_country(),
10272 ],
10273 ];
10274 if ( is_object( $last_order ) && method_exists( $last_order, 'get_date_created' ) ) {
10275 $created_date = $last_order->get_date_created();
10276 if ( is_object( $created_date ) && method_exists( $created_date, 'getTimestamp' ) ) {
10277 $last_order_date = $created_date->getTimestamp();
10278 $customer_data['created_at'] = $last_order_date;
10279 $customer_data['last_order_date'] = $last_order_date;
10280 }
10281 }
10282 $order_sample_data = $customer_data;
10283 $context['response_type'] = 'live';
10284 $context['pluggable_data'] = $order_sample_data;
10285 } else {
10286 $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 );
10287 $context['pluggable_data'] = $order_sample_data;
10288 }
10289 } elseif ( 'total_spend_reach' === $term ) {
10290 $total_spend_selected = isset( $data['filter']['total_spend']['value'] ) ? $data['filter']['total_spend']['value'] : '';
10291 $customers = $wpdb->get_col(
10292 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
10293 WHERE meta_key = '_customer_user' AND meta_value > 0"
10294 );
10295 $target_customer = null;
10296 foreach ( $customers as $customer ) {
10297 $total_spend = (int) wc_get_customer_total_spent( $customer );
10298 if ( $total_spend == $total_spend_selected ) {
10299 $target_customer = $customer;
10300 break; // Exit the loop once a matching customer is found.
10301 }
10302 }
10303 if ( $target_customer ) {
10304 $new_customer = new WC_Customer( $target_customer );
10305 $new_customer_data = [
10306 'id' => $new_customer->get_id(),
10307 'email' => $new_customer->get_email(),
10308 'first_name' => $new_customer->get_first_name(),
10309 'last_name' => $new_customer->get_last_name(),
10310 'username' => $new_customer->get_username(),
10311 'order_count' => $new_customer->get_order_count(),
10312 'total_spend' => wc_format_decimal( $new_customer->get_total_spent(), 2 ),
10313 ];
10314 $context['response_type'] = 'live';
10315 $context['pluggable_data'] = $new_customer_data;
10316 } else {
10317 $sample_customer_data = [
10318 'id' => '101',
10319 'created_at' => '1680675247',
10320 'email' => 'john@d.com',
10321 'first_name' => 'John',
10322 'last_name' => 'D',
10323 'username' => 'johnd',
10324 'order_count' => '3',
10325 'total_spend' => '22.00',
10326 ];
10327 $context['pluggable_data'] = $sample_customer_data;
10328 }
10329 } elseif ( 'order_count_reach' === $term ) {
10330 $total_order_selected = isset( $data['filter']['order_count']['value'] ) ? $data['filter']['order_count']['value'] : '';
10331 $customers = $wpdb->get_col(
10332 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
10333 WHERE meta_key = '_customer_user' AND meta_value > 0"
10334 );
10335 $target_customer = null;
10336 foreach ( $customers as $customer ) {
10337 $args = [
10338 'customer_id' => $customer,
10339 'limit' => -1,
10340 'status' => [ 'wc-completed' ],
10341 ];
10342 $orders = wc_get_orders( $args );
10343 if ( ! empty( $orders ) ) {
10344 $total_order = (int) wc_get_customer_order_count( $customer );
10345 if ( $total_order == $total_order_selected ) {
10346 $target_customer = $customer;
10347 break; // Exit the loop once a matching customer is found.
10348 }
10349 }
10350 }
10351 if ( $target_customer ) {
10352 $new_customer = new WC_Customer( $target_customer );
10353 $new_customer_data = [
10354 'id' => $new_customer->get_id(),
10355 'email' => $new_customer->get_email(),
10356 'first_name' => $new_customer->get_first_name(),
10357 'last_name' => $new_customer->get_last_name(),
10358 'username' => $new_customer->get_username(),
10359 'order_count' => $new_customer->get_order_count(),
10360 ];
10361 $context['response_type'] = 'live';
10362 $context['pluggable_data'] = $new_customer_data;
10363 } else {
10364 $sample_customer_data = [
10365 'id' => '101',
10366 'created_at' => '1680675247',
10367 'email' => 'john@d.com',
10368 'first_name' => 'John',
10369 'last_name' => 'D',
10370 'username' => 'johnd',
10371 'orders_count' => '3',
10372 ];
10373 $context['pluggable_data'] = $sample_customer_data;
10374 }
10375 }
10376
10377 return $context;
10378 }
10379
10380 /**
10381 * Get Affiliate list
10382 *
10383 * @param array $data data.
10384 *
10385 * @return array
10386 */
10387 public function search_affiliate_list( $data ) {
10388 $options = [];
10389
10390 $args = [
10391 'meta_query' => [
10392 [
10393 'key' => 'wafp_is_affiliate',
10394 'value' => '1',
10395 'compare' => '=',
10396 ],
10397 ],
10398 ];
10399
10400 $affiliates = get_users( $args );
10401
10402 foreach ( $affiliates as $user ) {
10403 $options[] = [
10404 'label' => $user->display_name,
10405 'value' => $user->ID,
10406 ];
10407 }
10408 return [
10409 'options' => $options,
10410 'hasMore' => false,
10411 ];
10412 }
10413
10414 /**
10415 * Get Affiliate Source list
10416 *
10417 * @param array $data data.
10418 *
10419 * @return array
10420 */
10421 public function search_affiliate_transaction_source_list( $data ) {
10422 $options = [];
10423
10424 $sources = [
10425 'General',
10426 'MemberPress',
10427 'WooCommerce',
10428 'Easy Digital Downloads',
10429 'WPForms',
10430 'Formidable',
10431 'PayPal',
10432 ];
10433 foreach ( $sources as $source ) {
10434 $options[] = [
10435 'label' => $source,
10436 'value' => str_replace( ' ', '_', strtolower( $source ) ),
10437 ];
10438 }
10439
10440 return [
10441 'options' => $options,
10442 'hasMore' => false,
10443 ];
10444 }
10445
10446 /**
10447 * Search Easy Affiliate data.
10448 *
10449 * @param array $data data.
10450 * @return array|void
10451 */
10452 public function search_easy_affiliate_last_data( $data ) {
10453
10454 global $wpdb;
10455 $trigger = $data['search_term'];
10456 $context = [];
10457
10458 if ( 'sale_recorded' === $trigger ) {
10459 $affiliate_id = $data['filter']['affiliate_id']['value'];
10460 if ( -1 === $affiliate_id ) {
10461 $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" );
10462 } else {
10463 $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 ) );
10464 }
10465 } elseif ( 'sale_added' === $trigger ) {
10466 $orderby = 'signup_date';
10467 $order = 'DESC';
10468 $paged = 1;
10469 $search = '';
10470 $perpage = 1;
10471 /**
10472 *
10473 * Ignore line
10474 *
10475 * @phpstan-ignore-next-line
10476 */
10477 $result = \EasyAffiliate\Models\User::affiliate_list_table( $orderby, $order, $paged, $search, $perpage );
10478 if ( empty( $result['results'] ) ) {
10479 $result = [];
10480 }
10481 } elseif ( 'payout_made' === $trigger ) {
10482 $affiliate_id = $data['filter']['affiliate_id']['value'];
10483 if ( -1 === $affiliate_id ) {
10484 $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" );
10485 } else {
10486 $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 ) );
10487 }
10488 }
10489
10490 if ( ! empty( $result ) ) {
10491 switch ( $trigger ) {
10492 case 'sale_recorded':
10493 $result_affiliate_id = $result[0]->affiliate_id;
10494 /**
10495 *
10496 * Ignore line
10497 *
10498 * @phpstan-ignore-next-line
10499 */
10500 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
10501 $affiliate = get_object_vars( $affiliate->rec );
10502 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
10503 break;
10504 case 'sale_added':
10505 /**
10506 *
10507 * Ignore line
10508 *
10509 * @phpstan-ignore-next-line
10510 */
10511 $data = new \EasyAffiliate\Models\User( $result['results'][0]->ID );
10512 /**
10513 *
10514 * Ignore line
10515 *
10516 * @phpstan-ignore-next-line
10517 */
10518 $context = get_object_vars( $data->rec );
10519 break;
10520 case 'payout_made':
10521 $result_affiliate_id = $result[0]->affiliate_id;
10522 /**
10523 *
10524 * Ignore line
10525 *
10526 * @phpstan-ignore-next-line
10527 */
10528 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
10529 $affiliate = get_object_vars( $affiliate->rec );
10530 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
10531 break;
10532 default:
10533 return;
10534 }
10535 $context['pluggable_data'] = $context;
10536 $context['response_type'] = 'live';
10537 } elseif ( empty( $result ) ) {
10538 switch ( $trigger ) {
10539 case 'sale_added':
10540 $context = json_decode(
10541 '{"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": "",
10542 "city": "london","state": "","zip": "","country": "UK","tax_id_us": "","tax_id_int": "","is_affiliate": "1","is_blocked": "","blocked_message": "","referrer": "0","notes": "","unsubscribed": ""}',
10543 true
10544 );
10545 break;
10546 case 'sale_recorded':
10547 $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 );
10548 break;
10549 case 'payout_made':
10550 $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 );
10551 break;
10552 default:
10553 return;
10554 }
10555 $context['pluggable_data'] = $context;
10556 $context['response_type'] = 'sample';
10557 }
10558
10559 return $context;
10560
10561 }
10562
10563 /**
10564 * Get WooCommerce Subscriptions Coupon list
10565 *
10566 * @param array $data data.
10567 *
10568 * @return array
10569 */
10570 public function search_woo_subscription_coupon_list( $data ) {
10571 $options = [];
10572
10573 $coupon_codes = [];
10574
10575 $query = new \WP_Query(
10576 [
10577 'post_type' => 'shop_coupon',
10578 'posts_per_page' => -1,
10579 'no_found_rows' => true,
10580 'meta_query' => [
10581 [
10582 'key' => '_is_aw_coupon',
10583 'compare' => 'NOT EXISTS',
10584 ],
10585 ],
10586 ]
10587 );
10588
10589 foreach ( $query->posts as $coupon ) {
10590 /**
10591 *
10592 * Ignore line
10593 *
10594 * @phpstan-ignore-next-line
10595 */
10596 $code = wc_format_coupon_code( $coupon->post_title );
10597 $coupon_codes[ $code ] = $code;
10598 }
10599
10600 foreach ( $coupon_codes as $code ) {
10601
10602 $coupon = new \WC_Coupon( $code );
10603
10604 if ( ! in_array( $coupon->get_discount_type(), [ 'recurring_fee', 'recurring_percent' ], true ) ) {
10605 unset( $coupon_codes[ $code ] );
10606 }
10607 }
10608
10609 if ( ! empty( $coupon_codes ) ) {
10610 if ( is_array( $coupon_codes ) ) {
10611 foreach ( $coupon_codes as $coupon_code ) {
10612 $options[] = [
10613 'label' => $coupon_code,
10614 'value' => $coupon_code,
10615 ];
10616 }
10617 }
10618 }
10619 return [
10620 'options' => $options,
10621 'hasMore' => false,
10622 ];
10623 }
10624
10625 /**
10626 * Get BuddyBoss field.
10627 *
10628 * @param array $data data.
10629 *
10630 * @return array|void
10631 */
10632 public function search_bb_field_list( $data ) {
10633 global $wpdb;
10634 $options = [];
10635
10636 $xprofile_fields = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_xprofile_fields WHERE parent_id = 0 ORDER BY field_order ASC" );
10637 if ( ! empty( $xprofile_fields ) ) {
10638 foreach ( $xprofile_fields as $xprofile_field ) {
10639 $options[] = [
10640 'label' => $xprofile_field->name,
10641 'value' => $xprofile_field->id,
10642 ];
10643 }
10644 }
10645
10646 return [
10647 'options' => $options,
10648 'hasMore' => false,
10649 ];
10650 }
10651
10652 /**
10653 * Get last data for WooCommerce Booking trigger
10654 *
10655 * @param array $data data.
10656 * @return array
10657 */
10658 public function search_woo_commerce_booking_last_data( $data ) {
10659 $context = [];
10660 $context['response_type'] = 'sample';
10661 $context['pluggable_data'] = [];
10662 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
10663 global $wpdb;
10664 if ( 'booking_created' === $term ) {
10665 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' AND post_status='confirmed' order by ID DESC LIMIT 1" );
10666
10667 if ( ! empty( $results ) ) {
10668 if ( class_exists( 'WC_Booking' ) ) {
10669 $booking = new WC_Booking( $results[0]->ID );
10670 if ( method_exists( $booking, 'get_data' ) ) {
10671 $booking = $booking->get_data();
10672 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
10673 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
10674 $context['response_type'] = 'live';
10675 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
10676 }
10677 }
10678 } else {
10679 $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"}],"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 );
10680 $context['pluggable_data'] = $booking_sample_data;
10681 }
10682 } elseif ( 'booking_status_changed' === $term ) {
10683 $to_status = isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1;
10684
10685 if ( -1 == $to_status ) {
10686 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' order by ID DESC LIMIT 1" );
10687 } else {
10688 $results = $wpdb->get_results(
10689 $wpdb->prepare(
10690 "
10691 SELECT *
10692 FROM {$wpdb->prefix}posts
10693 WHERE `post_status` = %s
10694 AND `post_type` LIKE %s
10695 ",
10696 $to_status,
10697 'wc_booking'
10698 )
10699 );
10700 }
10701
10702 if ( ! empty( $results ) ) {
10703 if ( class_exists( 'WC_Booking' ) ) {
10704 $booking = new WC_Booking( $results[0]->ID );
10705 if ( method_exists( $booking, 'get_data' ) ) {
10706 $booking = $booking->get_data();
10707 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
10708 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
10709 $booking['from_status'] = $data['filter']['from_status']['value'];
10710 $booking['to_status'] = $booking['status'];
10711 $context['response_type'] = 'live';
10712 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
10713 }
10714 }
10715 } else {
10716 $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"}],"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 );
10717 $context['pluggable_data'] = $booking_sample_data;
10718 }
10719 }
10720 return $context;
10721 }
10722
10723 /**
10724 * Get WooCommerce Booking status list.
10725 *
10726 * @param array $data data.
10727 *
10728 * @return array
10729 */
10730 public function search_woo_booking_status_list( $data ) {
10731
10732 $options = [
10733 [
10734 'label' => 'Pending Confirmation',
10735 'value' => 'pending-confirmation',
10736 ],
10737 [
10738 'label' => 'Unpaid',
10739 'value' => 'unpaid',
10740 ],
10741 [
10742 'label' => 'Confirmed',
10743 'value' => 'confirmed',
10744 ],
10745 [
10746 'label' => 'Paid',
10747 'value' => 'paid',
10748 ],
10749 [
10750 'label' => 'Complete',
10751 'value' => 'complete',
10752 ],
10753 [
10754 'label' => 'In Cart',
10755 'value' => 'in-cart',
10756 ],
10757 [
10758 'label' => 'Cancelled',
10759 'value' => 'cancelled',
10760 ],
10761 ];
10762
10763 return [
10764 'options' => $options,
10765 'hasMore' => false,
10766 ];
10767 }
10768
10769 /**
10770 * Get Woocommerce Memberships Status List.
10771 *
10772 * @param array $data data.
10773 *
10774 * @return array|void
10775 */
10776 public function search_wc_membership_status_list( $data ) {
10777 $options = [];
10778 if ( ! function_exists( 'wc_memberships_get_user_membership_statuses' ) ) {
10779 return;
10780 }
10781
10782 $statuses = wc_memberships_get_user_membership_statuses();
10783 if ( ! empty( $statuses ) ) {
10784 if ( is_array( $statuses ) ) {
10785 foreach ( $statuses as $status => $value ) {
10786 $status = 0 === strpos( $status, 'wcm-' ) ? substr( $status, 4 ) : $status;
10787 $options[] = [
10788 'label' => $value['label'],
10789 'value' => $status,
10790 ];
10791 }
10792 }
10793 }
10794
10795 return [
10796 'options' => $options,
10797 'hasMore' => false,
10798 ];
10799 }
10800
10801 /**
10802 * Search Woocommerce Subscription data.
10803 *
10804 * @param array $data data.
10805 * @return array|void
10806 */
10807 public function search_user_subscription_last_data( $data ) {
10808
10809 $context = [];
10810
10811 if ( ! function_exists( 'wcs_get_subscription' ) ) {
10812 return;
10813 }
10814 if ( ! function_exists( 'wcs_get_subscriptions_for_product' ) ) {
10815 return;
10816 }
10817 if ( ! function_exists( 'wcs_order_contains_renewal' ) ) {
10818 return;
10819 }
10820 if ( ! function_exists( 'wcs_get_subscriptions_for_order' ) ) {
10821 return;
10822 }
10823
10824 $subscription_id = $data['filter']['subscription']['value'];
10825
10826 if ( 'trial_end' == $data['search_term'] ) {
10827 $query_args = [
10828 'post_type' => 'shop_subscription',
10829 'orderby' => 'ID',
10830 'order' => 'DESC',
10831 'post_status' => 'wc-active',
10832 'posts_per_page' => 1,
10833 'meta_query' => [
10834 [
10835 'key' => '_schedule_trial_end',
10836 'value' => gmdate( 'Y-m-d H:i:s' ),
10837 'compare' => '<=',
10838 ],
10839 ],
10840 'post__in' => [ $subscription_id ],
10841 ];
10842 $query_result = new WP_Query( $query_args );
10843 $subscription_orders = $query_result->get_posts();
10844
10845 if ( ! empty( $subscription_orders ) ) {
10846 /**
10847 *
10848 * Ignore line
10849 *
10850 * @phpstan-ignore-next-line
10851 */
10852 $subscription = wcs_get_subscription( $subscription_orders[0]->ID );
10853 $user_id = $subscription->get_user_id();
10854
10855 $items = $subscription->get_items();
10856 $product_ids = [];
10857 foreach ( $items as $item ) {
10858 $product_ids[] = $item->get_product_id();
10859 }
10860
10861 $subscription_status = $subscription->get_status();
10862 $subscription_start_date = $subscription->get_date_created();
10863
10864 $context['subscription_data'] = [
10865 'status' => $subscription_status,
10866 'start_date' => $subscription_start_date,
10867 'trial_end_date' => $subscription->get_date( 'schedule_trial_end' ),
10868 'next_payment_date' => $subscription->get_date( 'next_payment' ),
10869 'end_date' => $subscription->get_date( 'schedule_end' ),
10870 ];
10871 $context['user'] = WordPress::get_user_context( $user_id );
10872
10873 foreach ( $product_ids as $val ) {
10874 $context['subscription'] = $val;
10875 $context['subscription_name'] = get_the_title( $val );
10876 }
10877 $context['pluggable_data'] = $context;
10878 $context['response_type'] = 'live';
10879 } else {
10880 $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 );
10881 $context['pluggable_data'] = $context;
10882 $context['response_type'] = 'sample';
10883 }
10884 } elseif ( 'renewal_payment_failed' == $data['search_term'] ) {
10885 $subscription_ids = wcs_get_subscriptions_for_product( $subscription_id );
10886 $ids = [];
10887 $related = [];
10888 foreach ( $subscription_ids as $subscription ) {
10889 $ids[] = $subscription;
10890 /**
10891 *
10892 * Ignore line
10893 *
10894 * @phpstan-ignore-next-line
10895 */
10896 $subscriptions = new WC_Subscription( $subscription );
10897 /**
10898 *
10899 * Ignore line
10900 *
10901 * @phpstan-ignore-next-line
10902 */
10903 $related[] = $subscriptions->get_related_orders();
10904 }
10905
10906 $failed_payment_orders = [];
10907 foreach ( $related as $value ) {
10908 foreach ( $value as $val ) {
10909 if ( wcs_order_contains_renewal( $val ) ) {
10910 $order = wc_get_order( $val );
10911 if ( $order ) {
10912 /**
10913 *
10914 * Ignore line
10915 *
10916 * @phpstan-ignore-next-line
10917 */
10918 $payment_status = $order->get_status();
10919 if ( 'failed' === $payment_status || 'wc-on-hold' === $payment_status ) {
10920 $failed_payment_orders[] = $val;
10921 }
10922 }
10923 }
10924 }
10925 }
10926
10927 $failed = array_rand( $failed_payment_orders );
10928 $related_subscriptions = wcs_get_subscriptions_for_order( $failed_payment_orders[ $failed ] );
10929 $sub_id = get_post_meta( $failed_payment_orders[ $failed ], '_subscription_renewal', true );
10930 if ( ! empty( $failed_payment_orders ) ) {
10931 $subscription = wcs_get_subscription( $sub_id );
10932
10933 $items = $subscription->get_items();
10934 $product_ids = [];
10935 foreach ( $items as $item ) {
10936 $product_ids[] = $item->get_product_id();
10937 }
10938
10939 $context['user'] = WordPress::get_user_context( $subscription->get_user_id() );
10940 foreach ( [ 'renewal' ] as $order_type ) {
10941 foreach ( $subscription->get_related_orders( 'ids', $order_type ) as $order_id ) {
10942 $context['subscription_related_order'][] = $order_id;
10943 }
10944 }
10945 foreach ( $product_ids as $val ) {
10946 $context['subscription_item'] = $val;
10947 $context['subscription_name'] = get_the_title( $val );
10948 }
10949 $context['data'] = $subscription->get_data();
10950 $context['pluggable_data'] = $context;
10951 $context['response_type'] = 'live';
10952 } else {
10953 $context = 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 );
10954 $context['pluggable_data'] = $context;
10955 $context['response_type'] = 'sample';
10956 }
10957 }
10958 return $context;
10959 }
10960
10961 /**
10962 * Get Masteriyo LMS Courses.
10963 *
10964 * @param array $data data.
10965 *
10966 * @return array
10967 */
10968 public function search_masteriyo_lms_courses( $data ) {
10969
10970 $page = $data['page'];
10971 $limit = Utilities::get_search_page_limit();
10972 $offset = $limit * ( $page - 1 );
10973
10974 $args = [
10975 'post_type' => 'mto-course',
10976 'posts_per_page' => $limit,
10977 'offset' => $offset,
10978 'orderby' => 'title',
10979 'order' => 'ASC',
10980 'post_status' => 'publish',
10981 ];
10982
10983 $courses = get_posts( $args );
10984
10985 $course_count = wp_count_posts( 'mto-course' )->publish;
10986
10987 $options = [];
10988 if ( ! empty( $courses ) ) {
10989 if ( is_array( $courses ) ) {
10990 foreach ( $courses as $course ) {
10991 $options[] = [
10992 'label' => $course->post_title,
10993 'value' => $course->ID,
10994 ];
10995 }
10996 }
10997 }
10998 return [
10999 'options' => $options,
11000 'hasMore' => $course_count > $limit && $course_count > $offset,
11001 ];
11002 }
11003
11004 /**
11005 * Get Masteriyo LMS Lessons.
11006 *
11007 * @param array $data data.
11008 *
11009 * @return array
11010 */
11011 public function search_masteriyo_lms_lessons( $data ) {
11012
11013 $course_id = $data['dynamic'];
11014 $page = $data['page'];
11015 $limit = Utilities::get_search_page_limit();
11016 $offset = $limit * ( $page - 1 );
11017 $options = [];
11018 $args = [
11019 'post_type' => 'mto-lesson',
11020 'posts_per_page' => $limit,
11021 'offset' => $offset,
11022 'orderby' => 'title',
11023 'order' => 'ASC',
11024 'post_status' => 'publish',
11025 'meta_query' => [
11026 [
11027 'key' => '_course_id',
11028 'value' => $course_id,
11029 'compare' => '=',
11030 ],
11031 ],
11032 ];
11033
11034 $lessons = get_posts( $args );
11035 $lesson_count = wp_count_posts( 'mto-lesson' )->publish;
11036 if ( ! empty( $lessons ) ) {
11037 if ( is_array( $lessons ) ) {
11038 foreach ( $lessons as $lesson ) {
11039 $options[] = [
11040 'label' => $lesson->post_title,
11041 'value' => $lesson->ID,
11042 ];
11043 }
11044 }
11045 }
11046 return [
11047 'options' => $options,
11048 'hasMore' => $lesson_count > $limit && $lesson_count > $offset,
11049 ];
11050 }
11051
11052 /**
11053 * Get Masteriyo LMS Quiz.
11054 *
11055 * @param array $data data.
11056 *
11057 * @return array
11058 */
11059 public function search_masteriyo_lms_quizs( $data ) {
11060
11061 $course_id = $data['dynamic'];
11062 $page = $data['page'];
11063 $limit = Utilities::get_search_page_limit();
11064 $offset = $limit * ( $page - 1 );
11065 $options = [];
11066 $args = [
11067 'post_type' => 'mto-quiz',
11068 'posts_per_page' => $limit,
11069 'offset' => $offset,
11070 'orderby' => 'title',
11071 'order' => 'ASC',
11072 'post_status' => 'publish',
11073 'meta_query' => [
11074 [
11075 'key' => '_course_id',
11076 'value' => $course_id,
11077 'compare' => '=',
11078 ],
11079 ],
11080 ];
11081
11082 $quizs = get_posts( $args );
11083 $quiz_count = wp_count_posts( 'mto-quiz' )->publish;
11084 if ( ! empty( $quizs ) ) {
11085 if ( is_array( $quizs ) ) {
11086 foreach ( $quizs as $quiz ) {
11087 $options[] = [
11088 'label' => $quiz->post_title,
11089 'value' => $quiz->ID,
11090 ];
11091 }
11092 }
11093 }
11094 return [
11095 'options' => $options,
11096 'hasMore' => $quiz_count > $limit && $quiz_count > $offset,
11097 ];
11098 }
11099
11100 /**
11101 * Search Masteriyo LMS data.
11102 *
11103 * @param array $data data.
11104 * @return array|void|mixed
11105 */
11106 public function search_masteriyo_lms_last_data( $data ) {
11107 global $wpdb;
11108 $post_type = $data['post_type'];
11109 $trigger = $data['search_term'];
11110 $context = [];
11111
11112 $post_id = -1;
11113 if ( 'course_completed' === $trigger ) {
11114 $post_id = $data['filter']['course_id']['value'];
11115 } elseif ( 'lesson_completed' === $trigger ) {
11116 $post_id = $data['filter']['lesson_id']['value'];
11117 } elseif ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
11118 $post_id = $data['filter']['quiz_id']['value'];
11119 }
11120
11121 if ( 'course_completed' === $trigger || 'lesson_completed' === $trigger ) {
11122 if ( -1 === $post_id ) {
11123 $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 ) );
11124 } else {
11125 $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 ) );
11126 }
11127 }
11128
11129 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
11130 if ( -1 === $post_id ) {
11131 $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 ) );
11132 } else {
11133 $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 ) );
11134 }
11135 if ( ! empty( $result ) ) {
11136 $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 ) );
11137
11138 if ( function_exists( 'masteriyo_get_quiz' ) ) {
11139 $quiz = masteriyo_get_quiz( $resultt[0]->quiz_id );
11140 if ( is_object( $quiz ) && method_exists( $quiz, 'get_pass_mark' ) ) {
11141 if ( 'quiz_completed' === $trigger && $resultt[0]->earned_marks < $quiz->get_pass_mark() ) {
11142 $result = [];
11143 } elseif ( 'quiz_failed' === $trigger && $resultt[0]->earned_marks > $quiz->get_pass_mark() ) {
11144 $result = [];
11145 }
11146 }
11147 }
11148 }
11149 }
11150
11151 if ( ! empty( $result ) ) {
11152 $result_item_id = $result[0]->item_id;
11153 $result_user_id = $result[0]->user_id;
11154 $quiz_attempt = '';
11155 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
11156 $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 ) );
11157 $quiz_attempt = $resultt[0]->id;
11158 }
11159 switch ( $trigger ) {
11160 case 'course_completed':
11161 if ( function_exists( 'masteriyo_get_course' ) ) {
11162 $course = masteriyo_get_course( $result_item_id );
11163 $context_data = array_merge(
11164 WordPress::get_user_context( $result_user_id ),
11165 $course->get_data()
11166 );
11167 $context_data['course_id'] = $result_item_id;
11168 }
11169 break;
11170 case 'lesson_completed':
11171 if ( function_exists( 'masteriyo_get_lesson' ) ) {
11172 $lesson = masteriyo_get_lesson( $result_item_id );
11173 $context_data = array_merge(
11174 WordPress::get_user_context( $result_user_id ),
11175 $lesson->get_data()
11176 );
11177 $context_data['lesson_id'] = $result_item_id;
11178 }
11179 break;
11180 case 'quiz_completed':
11181 case 'quiz_failed':
11182 if ( function_exists( 'masteriyo_get_quiz' ) ) {
11183 $quiz = masteriyo_get_quiz( $result_item_id );
11184 $context_data = WordPress::get_user_context( $result_user_id );
11185 if ( function_exists( 'masteriyo_get_quiz_attempt' ) ) {
11186 $attempt = masteriyo_get_quiz_attempt( $quiz_attempt );
11187 $context_data['quiz_id'] = $result_item_id;
11188 $context_data['course_id'] = $quiz->get_course_id();
11189 $context_data['quiz'] = $quiz->get_data();
11190 $context_data['attempt'] = $attempt->get_data();
11191 }
11192 }
11193 break;
11194 default:
11195 return;
11196 }
11197 if ( ! empty( $context_data ) ) {
11198 $context['pluggable_data'] = $context_data;
11199 $context['response_type'] = 'live';
11200 }
11201 } elseif ( empty( $result ) ) {
11202 switch ( $trigger ) {
11203 case 'course_completed':
11204 $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"} ';
11205 break;
11206 case 'lesson_completed':
11207 $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"} ';
11208 break;
11209 case 'quiz_completed':
11210 case 'quiz_failed':
11211 $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"}';
11212 break;
11213 default:
11214 return;
11215 }
11216 $context = json_decode( $sample_data, true );
11217 }
11218
11219 return $context;
11220 }
11221
11222 /**
11223 * Search learndash user added in group.
11224 *
11225 * @param array $data data.
11226 * @return array|void
11227 */
11228 public function search_pluggables_user_added_in_group( $data ) {
11229 $context = [];
11230
11231 if ( ! function_exists( 'learndash_get_groups_user_ids' ) ) {
11232 return;
11233 }
11234
11235 $group_id = $data['filter']['sfwd_group_id']['value'];
11236
11237 if ( -1 === $group_id ) {
11238 $args = [
11239 'numberposts' => 1,
11240 'fields' => 'ids',
11241 'orderby' => 'rand',
11242 'post_type' => 'groups',
11243 ];
11244 $posts = get_posts( $args );
11245 $random_key = array_rand( $posts );
11246 $random_value = $posts[ $random_key ];
11247 $group_users = learndash_get_groups_user_ids( $random_value );
11248 $group_id = $random_value;
11249 } else {
11250 $group_users = learndash_get_groups_user_ids( $group_id );
11251 }
11252
11253 $users = get_users(
11254 [
11255 'meta_key' => 'learndash_group_' . $group_id . '_enrolled_at',
11256 'orderby' => 'meta_value',
11257 'order' => 'DESC',
11258 'number' => 1,
11259 'include' => $group_users,
11260 ]
11261 );
11262
11263 if ( ! empty( $users ) ) {
11264 $context = WordPress::get_user_context( $users[0]->ID );
11265 $context['sfwd_group_id'] = $group_id;
11266 $context['group_title'] = get_the_title( $group_id );
11267 $context['group_url'] = get_permalink( $group_id );
11268 $context['group_featured_image_id'] = get_post_meta( $group_id, '_thumbnail_id', true );
11269 $context['group_featured_image_url'] = get_the_post_thumbnail_url( $group_id );
11270 $context['response_type'] = 'live';
11271 } else {
11272 $context = WordPress::get_sample_user_context();
11273 $context['group_title'] = 'Test Group';
11274 $context['sfwd_group_id'] = 112;
11275 $context['group_url'] = 'https://abc.com/test-group';
11276 $context['group_featured_image_id'] = 113;
11277 $context['group_featured_image_url'] = 'https://pqr.com/test-group-img';
11278 $context['response_type'] = 'sample';
11279 }
11280
11281 $context['pluggable_data'] = $context;
11282 return $context;
11283 }
11284
11285 /**
11286 * Search learndash user enrolled in course.
11287 *
11288 * @param array $data data.
11289 * @return array|void
11290 */
11291 public function search_pluggables_user_enrolled_course( $data ) {
11292 global $wpdb;
11293 $context = [];
11294
11295 if ( ! function_exists( 'ld_course_access_expires_on' ) ) {
11296 return;
11297 }
11298
11299 $course_id = $data['filter']['sfwd_course_id']['value'];
11300
11301 if ( -1 === $course_id ) {
11302 $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 ) );
11303 } else {
11304 $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 ) );
11305 }
11306
11307 if ( ! empty( $courses ) ) {
11308 $context = WordPress::get_user_context( $courses[0]->user_id );
11309 $context['course_id'] = $courses[0]->course_id;
11310 $context['course_title'] = get_the_title( $courses[0]->course_id );
11311 $context['course_url'] = get_permalink( $courses[0]->course_id );
11312 $context['course_featured_image_id'] = get_post_meta( $courses[0]->course_id, '_thumbnail_id', true );
11313 $context['course_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->course_id );
11314 $context['course_access_expiry_date'] = wp_date( get_option( 'date_format' ), ld_course_access_expires_on( $courses[0]->course_id, $courses[0]->user_id ) );
11315 $context['response_type'] = 'live';
11316 } else {
11317 $context = WordPress::get_sample_user_context();
11318 $context['course_name'] = 'Test Course';
11319 $context['sfwd_course_id'] = 112;
11320 $context['course_url'] = 'https://abc.com/test-course';
11321 $context['course_featured_image_id'] = 113;
11322 $context['course_featured_image_url'] = 'https://pqr.com/test-course-img';
11323 $context['course_access_expiry_date'] = '2023-10-20';
11324 $context['response_type'] = 'sample';
11325 }
11326
11327 $context['pluggable_data'] = $context;
11328 return $context;
11329 }
11330 }
11331
11332 GlobalSearchController::get_instance();
11333