PluginProbe
OttoKit: All-in-One Automation Platform / 1.0.12
OttoKit: All-in-One Automation Platform v1.0.12
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
6,695 lines 195.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\LearnDash\LearnDash;
33 use SureTriggers\Integrations\LifterLMS\LifterLMS;
34 use SureTriggers\Integrations\MemberPress\MemberPress;
35 use SureTriggers\Integrations\MemberPressCourse\MemberPressCourse;
36 use SureTriggers\Integrations\PeepSo\PeepSo;
37 use SureTriggers\Integrations\RestrictContent\RestrictContent;
38 use SureTriggers\Integrations\TheEventCalendar\TheEventCalendar;
39 use SureTriggers\Integrations\WishlistMember\WishlistMember;
40 use SureTriggers\Integrations\WooCommerce\WooCommerce;
41 use SureTriggers\Integrations\WordPress\WordPress;
42 use SureTriggers\Models\Utilities;
43 use SureTriggers\Traits\SingletonLoader;
44 use Tribe__Tickets__Tickets_Handler;
45 use WC_Subscription;
46 use WC_Subscriptions_Product;
47 use WP_Query;
48 use WP_Comment_Query;
49 use WP_REST_Request;
50 use WP_REST_Response;
51 use WPForms_Form_Handler;
52 use CP_V2_Popups;
53 use Project_Huddle;
54 use FrmForm;
55 use Forminator_API;
56 use SureTriggers\Integrations\LearnPress\LearnPress;
57
58 /**
59 * GlobalSearchController- Add ajax related functions here.
60 *
61 * @category GlobalSearchController
62 * @package SureTriggers
63 * @author BSF <username@example.com>
64 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
65 * @link https://www.brainstormforce.com/
66 * @since 1.0.0
67 *
68 * @psalm-suppress UndefinedTrait
69 */
70 class GlobalSearchController {
71
72 use SingletonLoader;
73
74 /**
75 * Search post by post type.
76 *
77 * @param array $data Search Params.
78 *
79 * @since 1.0.0
80 *
81 * @return array
82 */
83 public function search_post( $data ) {
84 $result = [];
85 $posts = Utilities::find_posts_by_title( $data );
86
87 foreach ( $posts['results'] as $post ) {
88 $result[] = [
89 'label' => $post['post_title'],
90 'value' => $post['ID'],
91 ];
92 }
93
94 return [
95 'options' => $result,
96 'hasMore' => $posts['has_more'],
97 ];
98 }
99
100 /**
101 * Search Course.
102 *
103 * @param array $data quesry params.
104 *
105 * @return array
106 * @since 1.0.0
107 */
108 public function search_ld_course( $data ) {
109 $courses = get_posts(
110 [
111
112 'post_type' => 'product',
113 'meta_key' => '_related_course',
114 'post_status' => 'publish',
115 ]
116 );
117 $options = [];
118 foreach ( $courses as $course ) {
119 $options[] = [
120 'label' => $course->post_title,
121 'value' => $course->ID,
122 ];
123 }
124
125 return [
126 'options' => $options,
127 'hasMore' => false,
128 ];
129 }
130
131 /**
132 * Search achievement by post type.
133 *
134 * @param array $data Search Params.
135 *
136 * @since 1.0.0
137 *
138 * @return array
139 */
140 public function search_achievements( $data ) {
141 $post = get_post( $data['dynamic'] );
142 $slug = $post->post_name;
143
144 $achievements = get_posts(
145 [
146 'post_type' => $slug,
147 'post_status' => 'publish',
148 ]
149 );
150 $options = [];
151 foreach ( $achievements as $achievement ) {
152 $options[] = [
153 'label' => $achievement->post_title,
154 'value' => (string) $achievement->ID,
155 ];
156 }
157
158 return [
159 'options' => $options,
160 'hasMore' => false,
161 ];
162 }
163
164 /**
165 * Search Course.
166 *
167 * @param array $data quesry params.
168 *
169 * @return array
170 * @since 1.0.0
171 */
172 public function search_tutor_course( $data ) {
173 $courses = get_posts(
174 [
175 'post_type' => tutor()->course_post_type,
176 'post_status' => 'publish',
177 'numberposts' => '-1',
178 ]
179 );
180 $options = [];
181 foreach ( $courses as $course ) {
182 $options[] = [
183 'label' => $course->post_title,
184 'value' => $course->ID,
185 ];
186 }
187
188 return [
189 'options' => $options,
190 'hasMore' => false,
191 ];
192 }
193
194 /**
195 * Search Products.
196 *
197 * @param array $data quesry params.
198 *
199 * @return array
200 * @since 1.0.0
201 */
202 public function search_product( $data ) {
203 $result = [];
204 $posts = Utilities::find_posts_by_title( $data );
205
206 foreach ( $posts['results'] as $post ) {
207 $result[] = [
208 'label' => $post['post_title'],
209 'value' => $post['post_title'],
210 ];
211 }
212
213 return [
214 'options' => $result,
215 'hasMore' => $posts['has_more'],
216 ];
217 }
218
219 /**
220 * Search Product Categories.
221 *
222 * @param array $data Search Params.
223 *
224 * @since 1.0.0
225 */
226 public function search_product_category( $data ) {
227 if ( ! empty( $data['dynamic'] ) ) {
228 $taxonomy = $data['dynamic'];
229 } else {
230 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
231 }
232
233 $term = $data['term'];
234 $result = [];
235 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
236 foreach ( $terms['result'] as $tax_term ) {
237 $result[] = [
238 'label' => $tax_term->name,
239 'value' => $tax_term->name,
240 ];
241 }
242
243 return [
244 'options' => $result,
245 'hasMore' => $terms['has_more'],
246 ];
247 }
248
249 /**
250 * Search Product Tags.
251 *
252 * @param array $data Search Params.
253 *
254 * @since 1.0.0
255 */
256 public function search_product_tags( $data ) {
257 if ( ! empty( $data['dynamic'] ) ) {
258 $taxonomy = $data['dynamic'];
259 } else {
260 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
261 }
262
263 $term = $data['term'];
264 $result = [];
265 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
266
267 foreach ( $terms['result'] as $tax_term ) {
268 $result[] = [
269 'label' => $tax_term->name,
270 'value' => $tax_term->name,
271 ];
272 }
273
274 return [
275 'options' => $result,
276 'hasMore' => $terms['has_more'],
277 ];
278 }
279
280 /**
281 * Global ajax search.
282 * Here you need to add the field action name to work.
283 *
284 * @param WP_REST_Request $request Request data.
285 *
286 * @return WP_REST_Response
287 * @since 1.0.0
288 */
289 public function global_search( $request ) {
290 $post_type = $request->get_param( 'post_type' );
291 $dynamic = $request->get_param( 'dynamic' );
292 $search_term = $request->get_param( 'term' );
293 $identifier = $request->get_param( 'field_identifier' );
294 $page = max( 1, $request->get_param( 'page' ) );
295 $taxonomy = $request->get_param( 'taxonomy' ) ? $request->get_param( 'taxonomy' ) : [];
296
297 $filter = $request->get_param( 'filter' ) ? json_decode( stripslashes( $request->get_param( 'filter' ) ), true ) : [];
298
299 $data = [
300 'dynamic' => $dynamic,
301 'search_term' => $search_term,
302 'page' => $page,
303 'taxonomy' => $taxonomy,
304 'filter' => $filter,
305 'post_type' => $post_type,
306 ];
307 $response = [
308 'hasMore' => false,
309 'options' => [],
310 ];
311
312 $method_name = 'search_' . $identifier;
313
314 if ( method_exists( $this, $method_name ) ) {
315 $response = $this->{$method_name}( $data );
316 } else {
317 return RestController::error_message( 'Invalid field Identifier param.' );
318 }
319
320 return RestController::success_message( $response );
321 }
322
323 /**
324 * Search Taxonomy Terms.
325 *
326 * @param array $data Search Params.
327 *
328 * @since 1.0.0
329 *
330 * @return array
331 */
332 public function search_term( $data ) {
333 if ( ! empty( $data['dynamic'] ) ) {
334 $taxonomy = $data['dynamic'];
335 } else {
336 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
337 }
338
339 $term = $data['term'];
340 $result = [];
341 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
342 foreach ( $terms['result'] as $tax_term ) {
343 $result[] = [
344 'label' => $tax_term->name,
345 'value' => $tax_term->term_id,
346 ];
347 }
348
349 return [
350 'options' => $result,
351 'hasMore' => $terms['has_more'],
352 ];
353 }
354
355 /**
356 * Search users.
357 *
358 * @param array $data Search Params.
359 *
360 * @since 1.0.0
361 *
362 * @return array
363 */
364 public function search_user( $data ) {
365 $result = [];
366 $page = $data['page'];
367 $users = Utilities::get_users( $data, $page );
368
369 if ( is_array( $users['results'] ) ) {
370 foreach ( $users['results'] as $user ) {
371 $result[] = [
372 'label' => $user->user_login,
373 'value' => $user->ID,
374 ];
375 }
376 }
377
378 return [
379 'options' => $result,
380 'hasMore' => $users['has_more'],
381 ];
382
383 }
384
385 /**
386 * Search WPForm fields.
387 *
388 * @param array $data Search Params.
389 *
390 * @since 1.0.0
391 *
392 * @return array
393 */
394 public function search_pluggable_wpform_fields( $data ) {
395 $result = [];
396 $page = $data['page'];
397 $form_id = absint( $data['dynamic'] );
398 $wpform_fields = Utilities::get_wpform_fields( $data['search_term'], $page, $form_id );
399
400 if ( is_array( $wpform_fields['results'] ) ) {
401 foreach ( $wpform_fields['results'] as $field ) {
402 $result[] = [
403 'label' => $field['label'],
404 'value' => '{' . $field['id'] . '}',
405 ];
406 }
407 }
408
409 return [
410 'options' => $result,
411 'hasMore' => $wpform_fields['has_more'],
412 ];
413 }
414
415 /**
416 * Prepare variable products.
417 *
418 * @param array $data data.
419 *
420 * @return array
421 */
422 public function search_variable_products( $data ) {
423 $products = Utilities::get_variable_products( $data['search_term'], $data['page'] );
424 $options = [];
425
426 foreach ( $products['result'] as $product ) {
427 $options[] = [
428 'label' => $product->get_title(),
429 'value' => (string) $product->get_id(),
430 ];
431 }
432
433 return [
434 'options' => $options,
435 'hasMore' => $products['has_more'],
436 ];
437 }
438
439 /**
440 * Prepare variable products.
441 *
442 * @param array $data data.
443 *
444 * @return array
445 */
446 public function search_product_variations( $data ) {
447 $variations = Utilities::get_product_variations( $data['dynamic'] );
448 $options = [];
449
450 foreach ( $variations['result'] as $product ) {
451 $options[] = [
452 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
453 'value' => (string) $product->ID,
454 ];
455 }
456
457 return [
458 'options' => $options,
459 'hasMore' => $variations['has_more'],
460 ];
461 }
462
463 /**
464 * Search WooCommerce Subscriptions.
465 *
466 * @param array $data Search Params.
467 *
468 * @since 1.0.0
469 *
470 * @return array
471 */
472 public function search_subscription_variation( $data ) {
473 $subscription_products = Utilities::get_subscription_variation( $data['search_term'], $data['page'] );
474 $result = [];
475
476 if ( ! function_exists( 'wc_get_products' ) ) {
477 return $result;
478 }
479
480 foreach ( $subscription_products['result'] as $post ) {
481 if ( $data['search_term'] ) {
482 if ( false !== stripos( $post->get_title(), $data['search_term'] ) ) {
483 $result[] = [
484 'label' => $post->get_title(),
485 'value' => (string) $post->get_id(),
486 ];
487 }
488 } else {
489 $result[] = [
490 'label' => $post->get_title(),
491 'value' => (string) $post->get_id(),
492 ];
493 }
494 }
495
496 return [
497 'options' => $result,
498 'hasMore' => $subscription_products['has_more'],
499 ];
500 }
501
502 /**
503 * Prepare WooCommerce Payment Methods.
504 *
505 * @param array $data Search Params.
506 * @return array[]
507 */
508 public function search_woo_payment_methods( $data ) {
509 $payment_methods = WC()->payment_gateways->get_available_payment_gateways();
510 $options = [];
511
512 if ( ! empty( $payment_methods ) ) {
513 foreach ( $payment_methods as $payment ) {
514 $options[] = [
515 'label' => $payment->title,
516 'value' => $payment->id,
517 ];
518 }
519 }
520
521 return [
522 'options' => $options,
523 'hasMore' => false,
524 ];
525 }
526
527 /**
528 * Prepare WooCommerce Order Status List.
529 *
530 * @param array $data Search Params.
531 * @return array[]
532 */
533 public function search_woo_order_status_list( $data ) {
534 $order_status = wc_get_order_statuses();
535 $options = [];
536
537 if ( ! empty( $order_status ) ) {
538 foreach ( $order_status as $key => $status ) {
539 $options[] = [
540 'label' => $status,
541 'value' => $key,
542 ];
543 }
544 }
545
546 return [
547 'options' => $options,
548 'hasMore' => false,
549 ];
550 }
551
552 /**
553 * Prepare WooCommerce Country List.
554 *
555 * @param array $data Search Params.
556 * @return array[]
557 */
558 public function search_woo_country_list( $data ) {
559 $countries = WC()->countries->get_countries();
560 $options = [];
561
562 if ( ! empty( $countries ) ) {
563 foreach ( $countries as $key => $country ) {
564 $options[] = [
565 'label' => $country,
566 'value' => $key,
567 ];
568 }
569 }
570
571 return [
572 'options' => $options,
573 'hasMore' => false,
574 ];
575 }
576
577 /**
578 * Prepare WooCommerce Country States List.
579 *
580 * @param array $data Search Params.
581 * @return array[]
582 */
583 public function search_woo_country_state_list( $data ) {
584 if ( ! empty( $data['dynamic']['shipping_country'] ) ) {
585 $cc = $data['dynamic']['shipping_country'];
586 } else {
587 $cc = $data['dynamic'];
588 }
589
590 $states = WC()->countries->get_states( $cc );
591
592 $options = [];
593 if ( ! empty( $states ) ) {
594 foreach ( $states as $key => $state ) {
595 $options[] = [
596 'label' => $state,
597 'value' => $key,
598 ];
599 }
600 }
601
602 return [
603 'options' => $options,
604 'hasMore' => false,
605 ];
606 }
607
608 /**
609 * Get Memberpress gatways (payment methods) for subscription.
610 *
611 * @param array $data QueryParams.
612 *
613 * @return array
614 * @since 1.0.0
615 */
616 public function search_memberpress_gayways( $data ) {
617 $mp_options = MeprOptions::fetch();
618
619 $pms = array_keys( $mp_options->integrations );
620 $gateways = [];
621
622 foreach ( $pms as $pm_id ) {
623 $obj = $mp_options->payment_method( $pm_id );
624 if ( $obj instanceof MeprBaseRealGateway ) {
625 $gateways[] = [
626 'label' => sprintf( '%1$s (%2$s)', $obj->label, $obj->name ),
627 'value' => $obj->id,
628 ];
629 }
630 }
631
632 return [
633 'options' => $gateways,
634 'hasMore' => false,
635 ];
636 }
637
638 /**
639 * Prepare roles.
640 *
641 * @param array $data data.
642 *
643 * @return array
644 */
645 public function search_roles( $data ) {
646 if ( function_exists( 'get_editable_roles' ) ) {
647 $roles = get_editable_roles();
648 } else {
649 $roles = wp_roles()->roles;
650 $roles = apply_filters( 'editable_roles', $roles );
651 }
652
653 $options = [];
654 foreach ( $roles as $role => $details ) {
655
656 $options[] = [
657 'label' => $details['name'],
658 'value' => $role,
659 ];
660
661 }
662
663 return [
664 'options' => $options,
665 'hasMore' => false,
666 ];
667 }
668
669 /**
670 * Fetch operators.
671 *
672 * @return array
673 */
674 public function search_condition_operators() {
675 return [
676 'options' => EventHelperController::get_instance()->prepare_operators(),
677 'hasMore' => false,
678 ];
679 }
680
681 /**
682 * Prepare post types.
683 *
684 * @param array $data data.
685 *
686 * @return array
687 */
688 public function search_post_types( $data ) {
689 $post_types = get_post_types( [ 'public' => true ], 'object' );
690 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
691 if ( isset( $post_types['attachment'] ) ) {
692 unset( $post_types['attachment'] );
693 }
694
695 $options = [];
696 foreach ( $post_types as $post_type => $details ) {
697 $options[] = [
698 'label' => $details->label,
699 'value' => $post_type,
700 ];
701 }
702
703 return [
704 'options' => $options,
705 'hasMore' => false,
706 ];
707 }
708
709 /**
710 * Get post statuses.
711 *
712 * @param array $data data.
713 *
714 * @return array
715 */
716 public function search_post_statuses( $data ) {
717 $post_statuses = get_post_stati( [], 'objects' );
718 $post_statuses = apply_filters( 'suretriggers_post_types', $post_statuses );
719 $options = [];
720
721 foreach ( $post_statuses as $post_status => $details ) {
722 if ( 'woocommerce' === $details->label_count['domain'] ) {
723 $options[] = [
724 'label' => 'WooCommerce - ' . $details->label,
725 'value' => $post_status,
726 ];
727 } else {
728 $options[] = [
729 'label' => $details->label,
730 'value' => $post_status,
731 ];
732 }
733 }
734
735 return [
736 'options' => $options,
737 'hasMore' => false,
738 ];
739 }
740
741 /**
742 * Get Taxonomies.
743 *
744 * @param array $data data.
745 *
746 * @return array
747 */
748 public function search_taxonomy_list( $data ) {
749 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
750 $options = [];
751 $options[0] = [
752 'label' => 'Any Taxonomy',
753 'value' => -1,
754 ];
755
756 foreach ( $taxonomies as $taxonomy => $taxonomy_obj ) {
757 $options[] = [
758 'label' => $taxonomy_obj->label,
759 'value' => $taxonomy_obj->name,
760 ];
761 }
762
763 return [
764 'options' => $options,
765 'hasMore' => false,
766 ];
767 }
768
769 /**
770 * Get WPForms.
771 *
772 * @param array $data data.
773 *
774 * @return array
775 */
776 public function search_wp_forms( $data ) {
777 if ( ! class_exists( 'WPForms_Form_Handler' ) ) {
778 return;
779 }
780
781 $wpforms = new WPForms_Form_Handler();
782 $forms = $wpforms->get( '', [ 'orderby' => 'title' ] );
783 $options = [];
784
785 if ( ! empty( $forms ) ) {
786 foreach ( $forms as $form ) {
787 $options[] = [
788 'label' => $form->post_title,
789 'value' => $form->ID,
790 ];
791 }
792 }
793
794 return [
795 'options' => $options,
796 'hasMore' => false,
797 ];
798 }
799
800 /**
801 * Get Gravity Forms.
802 *
803 * @param array $data data.
804 *
805 * @return array
806 */
807 public function search_gravity_forms( $data ) {
808 if ( ! class_exists( 'GFFormsModel' ) ) {
809 return;
810 }
811
812 $forms = GFFormsModel::get_forms();
813 $options = [];
814
815 if ( ! empty( $forms ) ) {
816 foreach ( $forms as $form ) {
817 $options[] = [
818 'label' => $form->title,
819 'value' => $form->id,
820 ];
821 }
822 }
823
824 return [
825 'options' => $options,
826 'hasMore' => false,
827 ];
828 }
829
830 /**
831 * Get tag & contact details.
832 *
833 * @param array $data data.
834 *
835 * @return array
836 */
837 public function search_pluggables_fluentcrm_contact_added_to_tags( $data ) {
838 $context = [];
839 $pluggable_data = [];
840 $tag_id = $data['filter'];
841
842 if ( $tag_id > 0 ) {
843 $tags = Tag::where( 'id', $tag_id )->first();
844 } else {
845 $tags = Tag::orderBy( 'id', 'DESC' )->first();
846 }
847 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
848 if ( $contact ) {
849 $pluggable_data['contact'] = $contact;
850 $context['tag_id'] = $tag_id;
851 $pluggable_data['tag'] = $tags;
852 $context['response_type'] = 'live';
853 } else {
854 $pluggable_data['conatct']['full_name'] = 'Test User';
855 $pluggable_data['conatct']['first_name'] = 'Test';
856 $pluggable_data['conatct']['last_name'] = 'User';
857 $pluggable_data['conatct']['company_id'] = 112;
858 $pluggable_data['conatct']['email'] = 'testuser@gmail.com';
859 $pluggable_data['conatct']['address_line_1'] = '33, Vincent Road';
860 $pluggable_data['conatct']['address_line_2'] = 'Chicago Street';
861 $pluggable_data['conatct']['postal_code'] = '212342';
862 $pluggable_data['conatct']['city'] = 'New York City';
863 $pluggable_data['conatct']['state'] = 'New York';
864 $pluggable_data['conatct']['country'] = 'USA';
865 $pluggable_data['conatct']['phone'] = '9992191911';
866 $pluggable_data['conatct']['status'] = 'subscribed';
867 $pluggable_data['conatct']['contact_type'] = 'lead';
868 $pluggable_data['conatct']['source'] = '';
869 $pluggable_data['conatct']['date_of_birth'] = '2022-11-09';
870 $context['tag_id'] = 1;
871 $pluggable_data['tag'] =
872 [
873 'id' => '1',
874 'title' => 'new',
875 'slug' => 'new',
876 'description' => null,
877 'created_at' => '2023-01-19 10:23:23',
878 'updated_at' => '2023-01-19 10:23:23',
879 'pivot' => [
880 'subscriber_id' => '1',
881 'object_id' => '1',
882 'object_type' => 'FluentCrm\\App\\Models\\Tag',
883 'created_at' => '2023-01-19 10:42:55',
884 'updated_at' => '2023-01-19 10:42:55',
885
886 ],
887 ];
888 $context['response_type'] = 'sample';
889 }
890
891 $context['pluggable_data'] = $pluggable_data;
892 return $context;
893 }
894
895 /**
896 * Get Divi Forms.
897 *
898 * @param array $data data.
899 *
900 * @return array
901 */
902 public function search_divi_forms( $data ) {
903 $form_posts = Utilities::get_divi_forms();
904 $options = [];
905
906 if ( empty( $form_posts ) ) {
907 return [
908 'options' => [],
909 'hasMore' => false,
910 ];
911 }
912
913 foreach ( $form_posts as $form_post ) {
914 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
915 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
916 if ( empty( $forms ) ) {
917 continue;
918 }
919
920 $count = 0;
921
922 foreach ( $forms as $form ) {
923 $pattern_form = get_shortcode_regex( [ 'et_pb_contact_form' ] );
924 preg_match_all( "/$pattern_form/", $form[0], $forms_extracted, PREG_SET_ORDER );
925
926 if ( empty( $forms_extracted ) ) {
927 continue;
928 }
929
930 foreach ( $forms_extracted as $form_extracted ) {
931 $form_attrs = shortcode_parse_atts( $form_extracted[3] );
932 $form_id = isset( $form_attrs['_unique_id'] ) ? $form_attrs['_unique_id'] : '';
933 if ( empty( $form_id ) ) {
934 continue;
935 }
936 $form_id = sprintf( '%d-%s', $form_post['ID'], $form_id );
937 $form_title = isset( $form_attrs['title'] ) ? $form_attrs['title'] : '';
938 $form_title = sprintf( '%s %s', $form_post['post_title'], $form_title );
939 $options[] = [
940 'label' => $form_title,
941 'value' => $form_id,
942 ];
943 }
944 $count++;
945 }
946 }
947
948 return [
949 'options' => $options,
950 'hasMore' => false,
951 ];
952 }
953
954 /**
955 * Get Comment Pluggable data.
956 *
957 * @param array $data data.
958 *
959 * @return array
960 */
961 public function search_pluggables_wp_insert_comment( $data ) {
962 $context = [];
963 $post_data = [];
964 $args = [
965 'number' => '1',
966 'status' => 'approve',
967 ];
968
969 if ( isset( $data['filter']['post']['value'] ) ) {
970 $post_id = $data['filter']['post']['value'];
971 if ( $post_id > 0 ) {
972 $args['post_id'] = $post_id;
973 }
974 }
975
976 $comments = get_comments( $args );
977 if ( empty( $comments ) ) {
978 unset( $args['post_id'] );
979 $comments = get_comments( $args );
980 }
981 $context['context_data'] = $data;
982 $context['context_args'] = $args;
983 if ( ! empty( $comments ) ) {
984 foreach ( $comments as $comment ) :
985 if ( is_object( $comment ) ) {
986 $comment = get_object_vars( $comment );
987 }
988 $post = get_post( absint( $comment['comment_post_ID'] ) );
989 $user_email = $comment['comment_author_email'];
990 $user = get_user_by( 'email', $user_email );
991 $context['pluggable_data'] = [
992 'post' => $post->ID,
993 'post_title' => $post->post_title,
994 'comment_id' => $comment['comment_ID'],
995 'comment' => $comment['comment_content'],
996 ];
997 if ( $user ) {
998 $context['pluggable_data']['wp_user_id'] = $user->ID;
999 $context['pluggable_data']['user_login'] = $user->user_login;
1000 $context['pluggable_data']['display_name'] = $user->display_name;
1001 $context['pluggable_data']['user_firstname'] = $user->user_firstname;
1002 $context['pluggable_data']['user_lastname'] = $user->user_lastname;
1003 $context['pluggable_data']['user_email'] = $user->user_email;
1004 $context['pluggable_data']['user_role'] = $user->roles;
1005 }
1006
1007 $context['response_type'] = 'live';
1008 endforeach;
1009 } else {
1010 $sample_comment = [
1011 'post' => 100,
1012 'post_title' => 'Sample Post',
1013 'comment_id' => 101,
1014 'comment' => 'Sample Comment',
1015 ];
1016 $sample_comment['wp_user_id'] = 7;
1017 $sample_comment['user_login'] = 'testuser@gmail.com';
1018 $sample_comment['display_name'] = 'Test User';
1019 $sample_comment['user_firstname'] = 'Test';
1020 $sample_comment['user_lastname'] = 'User';
1021 $sample_comment['user_email'] = 'testuser@gmail.com';
1022 $sample_comment['user_role'] = [ 'Subscriber' ];
1023
1024 $context['pluggable_data'] = $sample_comment;
1025 $context['response_type'] = 'sample';
1026 }
1027 return $context;
1028 }
1029
1030 /**
1031 * User reset password.
1032 *
1033 * @param array $data data.
1034 * @return array
1035 */
1036 public function search_pluggables_user_reset_password( $data ) {
1037 $user_context = $this->search_pluggables_add_user_role( $data );
1038 $user_context['pluggable_data']['new_password'] = '***password***';
1039 return $user_context;
1040 }
1041
1042 /**
1043 * User pluggable data.
1044 *
1045 * @param array $data data.
1046 * @return array
1047 */
1048 public function search_pluggables_add_user_role( $data ) {
1049 $context = [];
1050 $args = [
1051 'order' => 'DESC',
1052 'number' => 1,
1053 ];
1054
1055 if ( isset( $data['filter']['role']['value'] ) ) {
1056 $role = $data['filter']['role']['value'];
1057 $args['role'] = $role;
1058 }
1059 if ( isset( $data['filter']['new_role']['value'] ) ) {
1060 $role = $data['filter']['new_role']['value'];
1061 $args['role'] = $role;
1062 }
1063
1064 $users = get_users( $args );
1065
1066 if ( isset( $data['filter']['meta_key']['value'] ) ) {
1067 $meta_key = $data['filter']['meta_key']['value'];
1068 $args['st_meta_key'] = $meta_key;
1069 }
1070
1071 if ( isset( $data['filter']['profile_field']['value'] ) ) {
1072 $meta_key = $data['filter']['profile_field']['value'];
1073 $args['profile_field'] = $meta_key;
1074 }
1075
1076 if ( ! empty( $users ) ) {
1077 $user = $users[0];
1078 $pluggable_data = [];
1079 $pluggable_data['wp_user_id'] = $user->ID;
1080 $pluggable_data['user_login'] = $user->user_login;
1081 $pluggable_data['display_name'] = $user->display_name;
1082 $pluggable_data['user_firstname'] = $user->user_firstname;
1083 $pluggable_data['user_lastname'] = $user->user_lastname;
1084 $pluggable_data['user_email'] = $user->user_email;
1085 $pluggable_data['user_role'] = $user->roles;
1086
1087 if ( isset( $args['st_meta_key'] ) ) {
1088 $pluggable_data['meta_key'] = $args['st_meta_key'];
1089 $pluggable_data['meta_value'] = get_user_meta( $user->ID, $args['st_meta_key'], true );
1090 }
1091 if ( isset( $args['profile_field'] ) ) {
1092 $userdata = get_userdata( $user->ID );
1093 $userdata = json_decode( wp_json_encode( $userdata->data ), true );
1094
1095 $pluggable_data['profile_field'] = $args['profile_field'];
1096 $pluggable_data['profile_field_value'] = $userdata[ $args['profile_field'] ];
1097 }
1098 $context['pluggable_data'] = $pluggable_data;
1099 $context['response_type'] = 'live';
1100 } else {
1101 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
1102 $context['pluggable_data'] = [
1103 'wp_user_id' => 1,
1104 'user_login' => 'admin',
1105 'display_name' => 'Test User',
1106 'user_firstname' => 'Test',
1107 'user_lastname' => 'User',
1108 'user_email' => 'testuser@gmail.com',
1109 'user_role' => [ $role ],
1110 ];
1111 if ( isset( $args['st_meta_key'] ) ) {
1112 $context['pluggable_data']['meta_key'] = $args['st_meta_key'];
1113 $context['pluggable_data']['meta_value'] = 'test meta value';
1114 }
1115 if ( isset( $args['profile_field'] ) ) {
1116 $context['pluggable_data']['profile_field'] = $args['profile_field'];
1117 $context['pluggable_data']['profile_field_value'] = 'Profile Field Value';
1118 }
1119 $context['response_type'] = 'sample';
1120 }
1121 return $context;
1122 }
1123
1124 /**
1125 * User pluggable data.
1126 *
1127 * @param array $data data.
1128 * @return array
1129 */
1130 public function search_pluggables_last_user_login( $data ) {
1131 $context = [];
1132 $args = [
1133 'orderby' => 'meta_value',
1134 'meta_key' => 'st_last_login',
1135 'order' => 'DESC',
1136 'number' => 1,
1137 ];
1138 $users = get_users( $args );
1139
1140 if ( ! empty( $users ) ) {
1141 $user = $users[0];
1142 $pluggable_data = [];
1143 $pluggable_data['wp_user_id'] = $user->ID;
1144 $pluggable_data['user_login'] = $user->user_login;
1145 $pluggable_data['display_name'] = $user->display_name;
1146 $pluggable_data['user_firstname'] = $user->user_firstname;
1147 $pluggable_data['user_lastname'] = $user->user_lastname;
1148 $pluggable_data['user_email'] = $user->user_email;
1149 $pluggable_data['user_role'] = $user->roles;
1150
1151 $context['pluggable_data'] = $pluggable_data;
1152 $context['response_type'] = 'live';
1153 } else {
1154 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
1155 $context['pluggable_data'] = [
1156 'wp_user_id' => 1,
1157 'user_login' => 'admin',
1158 'display_name' => 'Test User',
1159 'user_firstname' => 'Test',
1160 'user_lastname' => 'User',
1161 'user_email' => 'testuser@gmail.com',
1162 'user_role' => [ $role ],
1163 ];
1164 $context['response_type'] = 'sample';
1165 }
1166 return $context;
1167 }
1168
1169 /**
1170 * Donation pluggable data.
1171 *
1172 * @param array $data data.
1173 * @return array
1174 */
1175 public function search_pluggables_wordpress_post( $data ) {
1176 $context = [];
1177 $args = [
1178 'post_type' => 'any',
1179 'posts_per_page' => 1,
1180 'orderby' => 'modified',
1181 'order' => 'DESC',
1182 ];
1183
1184 if ( isset( $data['filter']['post_type']['value'] ) ) {
1185 $post_type = $data['filter']['post_type']['value'];
1186 $args['post_type'] = $post_type;
1187 }
1188
1189 if ( isset( $data['filter']['status']['value'] ) ) {
1190 $post_status = $data['filter']['status']['value'];
1191 $args['post_status'] = $post_status;
1192 }
1193
1194 if ( isset( $data['filter']['post_status']['value'] ) ) {
1195 $post_status = $data['filter']['post_status']['value'];
1196 $args['post_status'] = $post_status;
1197 }
1198
1199 if ( isset( $data['filter']['post']['value'] ) ) {
1200 $post_id = $data['filter']['post']['value'];
1201 if ( $post_id > 0 ) {
1202 $args['p'] = $post_id;
1203 unset( $args['post_status'] );
1204 }
1205 }
1206
1207 $posts = get_posts( $args );
1208 if ( ! empty( $posts ) ) {
1209 $context['pluggable_data'] = $posts[0];
1210 $context['pluggable_data']->post = $posts[0]->ID;
1211 $context['response_type'] = 'live';
1212 } else {
1213 $context['pluggable_data'] = [
1214 'ID' => 557,
1215 'post' => 557,
1216 'post_author' => 1,
1217 'post_date' => '2022-11-18 12:18:14',
1218 'post_date_gmt' => '2022-11-18 12:18:14',
1219 'post_content' => 'Test Post Content',
1220 'post_title' => 'Test Post',
1221 'post_excerpt' => '',
1222 'post_status' => $args['post_status'],
1223 'comment_status' => 'open',
1224 'ping_status' => 'open',
1225 'post_password' => '',
1226 'post_name' => 'test-post',
1227 'to_ping' => '',
1228 'pinged' => '',
1229 'post_modified' => '2022-11-18 12:18:14',
1230 'post_modified_gmt' => '2022-11-18 12:18:14',
1231 'post_content_filtered' => '',
1232 'post_parent' => 0,
1233 'guid' => 'https://abc.com/test-post/',
1234 'menu_order' => 0,
1235 'post_type' => 'post',
1236 'post_mime_type' => '',
1237 'comment_count' => 0,
1238 'filter' => 'raw',
1239 ];
1240 $context['response_type'] = 'sample';
1241 }
1242
1243 return $context;
1244 }
1245
1246 /**
1247 * Donation pluggable data
1248 *
1249 * @param array $data data.
1250 * @return array
1251 */
1252 public function search_pluggables_givewp_donation_via_form( $data ) {
1253 global $wpdb;
1254 $context = [];
1255 $pluggable_data = [];
1256
1257 $payment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type=%s ORDER BY id DESC LIMIT 1", 'give_payment' ) );
1258
1259 if ( ! empty( $payment ) ) {
1260 $payment = new Give_Payment( $payment->ID );
1261 $address_data = $payment->address;
1262
1263 $pluggable_data['first_name'] = $payment->first_name;
1264 $pluggable_data['last_name'] = $payment->last_name;
1265 $pluggable_data['email'] = $payment->email;
1266 $pluggable_data['currency'] = $payment->currency;
1267 $pluggable_data['donated_amount'] = $payment->subtotal;
1268 $pluggable_data['donation_amount'] = $payment->subtotal;
1269 $pluggable_data['form_id'] = (int) $payment->form_id;
1270 $pluggable_data['form_title'] = $payment->form_title;
1271 $pluggable_data['name_title_prefix'] = $payment->title_prefix;
1272 $pluggable_data['date'] = $payment->date;
1273
1274 if ( is_array( $address_data ) ) {
1275 $pluggable_data['address_line_1'] = $address_data['line1'];
1276 $pluggable_data['address_line_2'] = $address_data['line2'];
1277 $pluggable_data['city'] = $address_data['city'];
1278 $pluggable_data['state'] = $address_data['state'];
1279 $pluggable_data['zip'] = $address_data['zip'];
1280 $pluggable_data['country'] = $address_data['country'];
1281 }
1282 $donor_comment = give_get_donor_donation_comment( $payment->ID, $payment->donor_id );
1283 $pluggable_data['comment'] = isset( $doner['comment_content'] ) ? $donor_comment : '';
1284 $context['response_type'] = 'live';
1285 } else {
1286 $pluggable_data['first_name'] = 'Test';
1287 $pluggable_data['last_name'] = 'User';
1288 $pluggable_data['email'] = 'testuser@gmail.com';
1289 $pluggable_data['currency'] = 'USD';
1290 $pluggable_data['donated_amount'] = 100;
1291 $pluggable_data['donation_amount'] = 100;
1292 $pluggable_data['form_id'] = 23;
1293 $pluggable_data['form_title'] = 'Test Donation';
1294 $pluggable_data['name_title_prefix'] = 'Mr';
1295 $pluggable_data['date'] = '2022-11-07 16:06:05';
1296 $pluggable_data['address_line_1'] = '33, Vincent Road';
1297 $pluggable_data['address_line_2'] = 'Chicago Street';
1298 $pluggable_data['city'] = 'New York City';
1299 $pluggable_data['state'] = 'New York';
1300 $pluggable_data['zip'] = '223131';
1301 $pluggable_data['country'] = 'USA';
1302 $pluggable_data['comment'] = 'Test Comment';
1303 $context['response_type'] = 'sample';
1304 }
1305
1306 $context['pluggable_data'] = $pluggable_data;
1307 return $context;
1308 }
1309
1310 /**
1311 * Search Divi Form fields.
1312 *
1313 * @param array $data Search Params.
1314 *
1315 * @since 1.0.0
1316 *
1317 * @return array
1318 */
1319 public function search_pluggable_divi_form_fields( $data ) {
1320 $result = [];
1321 $form_id = absint( $data['dynamic'] );
1322 $form_posts = Utilities::get_divi_forms();
1323
1324 if ( empty( $form_posts ) ) {
1325 return [
1326 'options' => [],
1327 'hasMore' => false,
1328 ];
1329 }
1330 $fields = [];
1331 foreach ( $form_posts as $form_post ) {
1332 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
1333 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
1334 if ( empty( $forms ) ) {
1335 continue;
1336 }
1337
1338 $count = 0;
1339
1340 foreach ( $forms as $form ) {
1341 $pattern = get_shortcode_regex( [ 'et_pb_contact_field' ] );
1342
1343 preg_match_all( "/$pattern/", $form[0], $contact_fields, PREG_SET_ORDER );
1344
1345 if ( empty( $contact_fields ) ) {
1346 return $fields;
1347 }
1348
1349 foreach ( $contact_fields as $contact_field ) {
1350 $contact_field_attrs = shortcode_parse_atts( $contact_field[3] );
1351 $field_id = strtolower( self::array_get( $contact_field_attrs, 'field_id' ) );
1352 $fields[] = [
1353 'field_title' => self::array_get( $contact_field_attrs, 'field_title', __( 'No title', 'suretriggers' ) ),
1354 'field_id' => $field_id,
1355 ];
1356 }
1357 }
1358 }
1359
1360 if ( ! empty( $fields ) ) {
1361 foreach ( $fields as $field ) {
1362 $result[] = [
1363 'label' => $field['field_title'],
1364 'value' => '{' . $field['field_id'] . '}',
1365 ];
1366 }
1367 }
1368
1369 return [
1370 'options' => $result,
1371 'hasMore' => false,
1372 ];
1373 }
1374
1375 /**
1376 * Pseudo function copied from Divi
1377 *
1378 * @param array $array An array which contains value located at `$address`.
1379 * @param string|array $address The location of the value within `$array` (dot notation).
1380 * @param mixed $default Value to return if not found. Default is an empty string.
1381 *
1382 * @return mixed The value, if found, otherwise $default.
1383 */
1384 public static function array_get( $array, $address, $default = '' ) {
1385 $keys = is_array( $address ) ? $address : explode( '.', $address );
1386 $value = $array;
1387
1388 foreach ( $keys as $key ) {
1389 if ( ! empty( $key ) && isset( $key[0] ) && '[' === $key[0] ) {
1390 $index = substr( $key, 1, -1 );
1391
1392 if ( is_numeric( $index ) ) {
1393 $key = (int) $index;
1394 }
1395 }
1396
1397 if ( ! isset( $value[ $key ] ) ) {
1398 return $default;
1399 }
1400
1401 $value = $value[ $key ];
1402 }
1403
1404 return $value;
1405 }
1406
1407 /**
1408 * Get UAG Forms.
1409 *
1410 * @param array $data data.
1411 *
1412 * @return array
1413 */
1414 public function search_spectra_forms( $data ) {
1415 $form_posts = Utilities::get_uag_forms();
1416
1417 $options = [];
1418 if ( empty( $form_posts ) ) {
1419 return [
1420 'options' => [],
1421 'hasMore' => false,
1422 ];
1423 }
1424
1425 foreach ( $form_posts as $form_post ) {
1426 $blocks = parse_blocks( $form_post['post_content'] );
1427 foreach ( $blocks as $key => $block ) {
1428 if ( 'uagb/forms' === $block['blockName'] ) {
1429 $options[] = [
1430 'label' => $form_post['post_title'] . ' (Form ' . ( $key + 1 ) . ')',
1431 'value' => $block['attrs']['block_id'],
1432 ];
1433 } elseif ( 'uagb/forms' !== $block['blockName'] && isset( $block['innerBlocks'] ) ) {
1434 $container_key = self::get_column_by_value( $block['innerBlocks'], 'uagb/container' );
1435 if ( $container_key ) {
1436 $new_block = self::get_column_by_value( $block['innerBlocks'][ $container_key ]['innerBlocks'], 'uagb/forms' );
1437 if ( $new_block ) {
1438 $options[] = [
1439 'label' => $form_post['post_title'] . ' (Form 1' . ( $key + 1 ) . ')',
1440 'value' => $block['innerBlocks'][ $container_key ]['innerBlocks'][ $new_block ]['attrs']['block_id'],
1441 ];
1442 }
1443 }
1444 }
1445 }
1446 }
1447
1448 return [
1449 'options' => $options,
1450 'hasMore' => false,
1451 ];
1452 }
1453
1454 /**
1455 * Check array recursive.
1456 *
1457 * @param array $array Array.
1458 * @param string $value search params.
1459 * @since 1.0.0
1460 *
1461 * @return array|void
1462 */
1463 public static function get_column_by_value( $array, $value ) {
1464
1465 foreach ( $array as $key => $sub_array ) {
1466 if ( is_array( $sub_array ) ) {
1467 $result = self::get_column_by_value( $sub_array, $value );
1468 if ( null !== $result ) {
1469 return $key;
1470 }
1471 } else {
1472 if ( $sub_array === $value ) {
1473 return $key;
1474 }
1475 }
1476 }
1477 return null;
1478
1479 }
1480
1481
1482 /**
1483 * Search UAG Form fields.
1484 *
1485 * @param array $data Search Params.
1486 *
1487 * @since 1.0.0
1488 *
1489 * @return array
1490 */
1491 public function search_spectraform_fields( $data ) {
1492 $result = [];
1493 $form_id = absint( $data['dynamic'] );
1494 $form_posts = Utilities::get_uag_forms();
1495
1496 if ( empty( $form_posts ) ) {
1497 return [
1498 'options' => [],
1499 'hasMore' => false,
1500 ];
1501 }
1502
1503 foreach ( $form_posts as $form_post ) {
1504 $blocks = parse_blocks( $form_post['post_content'] );
1505
1506 foreach ( $blocks as $block ) {
1507 if ( (int) $block['attrs']['block_id'] === $form_id ) {
1508 $doc = new DOMDocument();
1509 $rendered_block = render_block( $block );
1510 $doc->loadHTML( $rendered_block );
1511 $child_node_list = $doc->getElementsByTagName( 'div' );
1512 for ( $i = 0; $i < $child_node_list->length; $i++ ) {
1513 $temp = $child_node_list->item( $i );
1514 if ( $temp && stripos( $temp->getAttribute( 'class' ), 'uagb-forms-input-label' ) !== false ) {
1515 $nodes[] = $temp;
1516 }
1517 }
1518
1519 foreach ( $nodes as $node ) {
1520 $result[] = [
1521 'label' => $node->textContent, //phpcs:ignore
1522 'value' => $node->textContent, //phpcs:ignore
1523 ];
1524 }
1525 }
1526 }
1527 }
1528
1529 return [
1530 'options' => $result,
1531 'hasMore' => false,
1532 ];
1533 }
1534
1535 /**
1536 * Search forms of MetForms.
1537 *
1538 * @param array $data data.
1539 * @return array
1540 */
1541 public function search_met_forms( $data ) {
1542 $args = [
1543 'post_type' => 'metform-form',
1544 'post_status' => 'publish',
1545 'numberposts' => -1,
1546 ];
1547
1548 $forms = get_posts( $args );
1549
1550 $options = [];
1551
1552 if ( ! empty( $forms ) ) {
1553 foreach ( $forms as $form ) {
1554 $options[] = [
1555 'label' => $form->post_title,
1556 'value' => $form->ID,
1557 ];
1558 }
1559 }
1560
1561 return [
1562 'options' => $options,
1563 'hasMore' => false,
1564 ];
1565 }
1566
1567 /**
1568 * Search forms of Ninja Forms.
1569 *
1570 * @param array $data data.
1571 * @return array
1572 */
1573 public function search_ninja_forms( $data ) {
1574 $options = [];
1575
1576 if ( function_exists( 'Ninja_Forms' ) ) {
1577 foreach ( Ninja_Forms()->form()->get_forms() as $form ) {
1578 $options[] = [
1579 'label' => $form->get_setting( 'title' ),
1580 'value' => $form->get_id(),
1581 ];
1582 }
1583 }
1584
1585 return [
1586 'options' => $options,
1587 'hasMore' => false,
1588 ];
1589 }
1590
1591 /**
1592 * Search forms of Pie Forms.
1593 *
1594 * @param array $data data.
1595 * @return array
1596 */
1597 public function search_pie_forms( $data ) {
1598 global $wpdb;
1599 $options = [];
1600
1601 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pf_forms' ) ) ) {
1602
1603 $results = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'pf_forms WHERE post_status = "published"' );
1604
1605 if ( $results ) {
1606 foreach ( $results as $result ) {
1607 $options[] = [
1608 'label' => $result->form_title,
1609 'value' => $result->id,
1610 ];
1611 }
1612 }
1613 }
1614
1615 return [
1616 'options' => $options,
1617 'hasMore' => false,
1618 ];
1619 }
1620
1621 /**
1622 * Get Fluent Forms.
1623 *
1624 * @param array $data data.
1625 *
1626 * @return array
1627 */
1628 public function search_fluent_forms( $data ) {
1629
1630 if ( ! function_exists( 'wpFluent' ) ) {
1631 return [
1632 'options' => [],
1633 'hasMore' => false,
1634 ];
1635 }
1636
1637 $forms = wpFluent()->table( 'fluentform_forms' )
1638 ->select( [ 'id', 'title' ] )
1639 ->orderBy( 'id', 'DESC' )
1640 ->get();
1641
1642 $options = [];
1643 if ( ! empty( $forms ) ) {
1644 foreach ( $forms as $form ) {
1645 $options[] = [
1646 'label' => $form->title,
1647 'value' => $form->id,
1648 ];
1649 }
1650 }
1651
1652 return [
1653 'options' => $options,
1654 'hasMore' => false,
1655 ];
1656
1657 }
1658
1659 /**
1660 * Get Fluent Forms.
1661 *
1662 * @param array $data data.
1663 *
1664 * @return array
1665 */
1666 public function search_bricks_builder_forms( $data ) {
1667 $bricks_theme = wp_get_theme( 'bricks' );
1668 if ( ! $bricks_theme->exists() ) {
1669 return [
1670 'options' => [],
1671 'hasMore' => false,
1672 ];
1673 }
1674
1675 $args = [
1676 'post_type' => 'bricks_template',
1677 'post_status' => 'publish',
1678 'posts_per_page' => -1,
1679 ];
1680
1681 $templates = get_posts( $args );
1682
1683 $options = [];
1684 if ( ! empty( $templates ) ) {
1685 foreach ( $templates as $template ) {
1686 $fetch_content = get_post_meta( $template->ID, BRICKS_DB_PAGE_CONTENT, true );
1687 if ( is_array( $fetch_content ) ) {
1688 foreach ( $fetch_content as $content ) {
1689 if ( 'form' === $content['name'] ) {
1690 $options[] = [
1691 'label' => $template->post_title . ' - ' . ( isset( $content['label'] ) ? $content['label'] : 'Form' ),
1692 'value' => $content['id'],
1693 ];
1694 }
1695 }
1696 }
1697 }
1698 }
1699
1700 return [
1701 'options' => $options,
1702 'hasMore' => false,
1703 ];
1704
1705 }
1706
1707 /**
1708 * Bricks builder form fields.
1709 *
1710 * @param array $data data.
1711 * @return array
1712 */
1713 public function search_pluggable_bricks_builder_form_fields( $data ) {
1714 $result = [];
1715 $fields = [];
1716 $form_id_str = $data['dynamic'];
1717 $ids = explode( '_', $form_id_str );
1718 $post_id = $ids[0];
1719 $form_id = $ids[1];
1720 $fetch_content = get_post_meta( $post_id, BRICKS_DB_PAGE_CONTENT, true );
1721 if ( is_array( $fetch_content ) ) {
1722 foreach ( $fetch_content as $content ) {
1723 if ( 'form' === $content['name'] && $form_id === $content['id'] ) {
1724 $fields = $content['settings']['fields'];
1725 break;
1726 }
1727 }
1728 }
1729
1730 if ( ! empty( $fields ) ) {
1731 foreach ( $fields as $field ) {
1732 $result[] = [
1733 'label' => $field['label'],
1734 'value' => '{' . strtolower( $field['label'] ) . '}',
1735 ];
1736 }
1737 }
1738
1739 return [
1740 'options' => $result,
1741 'hasMore' => false,
1742 ];
1743 }
1744
1745 /**
1746 * Get fluent form fields
1747 *
1748 * @param array $data Data array.
1749 *
1750 * @return array
1751 */
1752 public function search_pluggable_fluent_form_fields( $data ) {
1753 $result = [];
1754 $form_id = absint( $data['dynamic'] );
1755
1756 $fluentform_fields = Utilities::get_fluentform_fields( $data['search_term'], -1, $form_id );
1757
1758 if ( is_array( $fluentform_fields['results'] ) ) {
1759 foreach ( $fluentform_fields['results'] as $field ) {
1760 $result[] = [
1761 'label' => $field['text'],
1762 'value' => "{{$field['value']}}",
1763 ];
1764 }
1765 }
1766
1767 $result[] = [
1768 'value' => '{form_id}',
1769 'label' => 'Form ID',
1770 ];
1771
1772 $result[] = [
1773 'value' => '{form_title}',
1774 'label' => 'Form Title',
1775 ];
1776 $result[] = [
1777 'value' => '{entry_id}',
1778 'label' => 'Entry ID',
1779 ];
1780
1781 $result[] = [
1782 'value' => '{entry_source_url}',
1783 'label' => 'Entry Source URL',
1784 ];
1785
1786 $result[] = [
1787 'value' => '{submission_date}',
1788 'label' => 'Submission Date',
1789 ];
1790
1791 $result[] = [
1792 'value' => '{user_ip}',
1793 'label' => 'User IP',
1794 ];
1795
1796 return [
1797 'options' => $result,
1798 'hasMore' => false,
1799 ];
1800 }
1801
1802 /**
1803 * Search Gravity Form fields.
1804 *
1805 * @param array $data Search Params.
1806 *
1807 * @since 1.0.0
1808 */
1809 public function search_gform_fields( $data ) {
1810 if ( ! class_exists( 'RGFormsModel' ) ) {
1811 return [
1812 'options' => [],
1813 'hasMore' => false,
1814 ];
1815 }
1816 $result = [];
1817 $page = $data['page'];
1818 $form_id = absint( $data['dynamic'] );
1819
1820 $form = RGFormsModel::get_form_meta( $form_id );
1821
1822 if ( is_array( $form['fields'] ) ) {
1823 foreach ( $form['fields'] as $field ) {
1824 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
1825 foreach ( $field['inputs'] as $input ) {
1826 if ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
1827 $result[] = [
1828 'value' => $input['id'],
1829 'label' => GFCommon::get_label( $field, $input['id'] ),
1830 ];
1831 }
1832 }
1833 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
1834 $result[] = [
1835 'value' => $field['id'],
1836 'label' => GFCommon::get_label( $field ),
1837 ];
1838 }
1839 }
1840 }
1841
1842 return [
1843 'options' => $result,
1844 'hasMore' => false,
1845 ];
1846
1847 }
1848
1849 /**
1850 * Search Gravity Form fields.
1851 *
1852 * @param array $data Search Params.
1853 *
1854 * @since 1.0.0
1855 */
1856 public function search_pluggable_gravity_form_fields( $data ) {
1857 if ( ! class_exists( 'RGFormsModel' ) ) {
1858 return [
1859 'options' => [],
1860 'hasMore' => false,
1861 ];
1862 }
1863 $result = [];
1864 $form_id = absint( $data['dynamic'] );
1865
1866 $form = RGFormsModel::get_form_meta( $form_id );
1867
1868 if ( is_array( $form['fields'] ) ) {
1869 foreach ( $form['fields'] as $field ) {
1870 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
1871 foreach ( $field['inputs'] as $input ) {
1872 if ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
1873 $result[] = [
1874 'value' => '{' . $input['id'] . '}',
1875 'label' => GFCommon::get_label( $field, $input['id'] ),
1876 ];
1877 }
1878 }
1879 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
1880 $result[] = [
1881 'value' => '{' . $field['id'] . '}',
1882 'label' => GFCommon::get_label( $field ),
1883 ];
1884 }
1885 }
1886 }
1887
1888 $result[] = [
1889 'value' => '{gravity_form}',
1890 'label' => 'Form ID',
1891 ];
1892 $result[] = [
1893 'value' => '{form_title}',
1894 'label' => 'Form Title',
1895 ];
1896 $result[] = [
1897 'value' => '{entry_id}',
1898 'label' => 'Entry ID',
1899 ];
1900 $result[] = [
1901 'value' => '{user_ip}',
1902 'label' => 'User IP',
1903 ];
1904 $result[] = [
1905 'value' => '{entry_source_url}',
1906 'label' => 'Entry Source URL',
1907 ];
1908 $result[] = [
1909 'value' => '{entry_submission_date}',
1910 'label' => 'Entry Submission Date',
1911 ];
1912
1913 return [
1914 'options' => $result,
1915 'hasMore' => false,
1916 ];
1917
1918 }
1919
1920 /**
1921 * Prepare fluentcrm lists.
1922 *
1923 * @param array $data Search Params.
1924 *
1925 * @return array[]
1926 */
1927 public function search_fluentcrm_lists( $data ) {
1928
1929 $list_api = FluentCrmApi( 'lists' );
1930 $all_lists = $list_api->all();
1931 $options = [];
1932
1933 if ( ! empty( $all_lists ) ) {
1934 foreach ( $all_lists as $list ) {
1935 $options[] = [
1936 'label' => $list->title,
1937 'value' => $list->id,
1938 ];
1939 }
1940 }
1941
1942 return [
1943 'options' => $options,
1944 'hasMore' => false,
1945 ];
1946 }
1947
1948 /**
1949 * Prepare fluentcrm contact status.
1950 *
1951 * @param array $data Search Params.
1952 *
1953 * @return array[]
1954 */
1955 public function search_fluentcrm_contact_status( $data ) {
1956
1957 $options = [
1958 [
1959 'label' => __( 'Subscribed', 'suretriggers' ),
1960 'value' => 'subscribed',
1961 ],
1962 [
1963 'label' => __( 'Pending', 'suretriggers' ),
1964 'value' => 'pending',
1965 ],
1966 [
1967 'label' => __( 'Unsubscribed', 'suretriggers' ),
1968 'value' => 'unsubscribed',
1969 ],
1970 [
1971 'label' => __( 'Bounced', 'suretriggers' ),
1972 'value' => 'bounced',
1973 ],
1974 [
1975 'label' => __( 'Complained', 'suretriggers' ),
1976 'value' => 'complained',
1977 ],
1978 ];
1979
1980 return [
1981 'options' => $options,
1982 'hasMore' => false,
1983 ];
1984 }
1985
1986 /**
1987 * Prepare fluentcrm contact status.
1988 *
1989 * @param array $data Search Params.
1990 *
1991 * @return array[]
1992 */
1993 public function search_fluentcrm_fetch_custom_fields( $data ) {
1994
1995 $options = [
1996 [
1997 'label' => __( 'Yes', 'suretriggers' ),
1998 'value' => 'true',
1999 ],
2000 [
2001 'label' => __( 'No', 'suretriggers' ),
2002 'value' => 'false',
2003 ],
2004 ];
2005
2006 return [
2007 'options' => $options,
2008 'hasMore' => false,
2009 ];
2010 }
2011
2012 /**
2013 * Prepare fluentcrm tags.
2014 *
2015 * @param array $data Search Params.
2016 *
2017 * @return array[]
2018 */
2019 public function search_fluentcrm_tags( $data ) {
2020
2021 $tag_api = FluentCrmApi( 'tags' );
2022 $all_tags = $tag_api->all();
2023 $options = [];
2024
2025 if ( ! empty( $all_tags ) ) {
2026 foreach ( $all_tags as $tag ) {
2027 $options[] = [
2028 'label' => $tag->title,
2029 'value' => $tag->id,
2030 ];
2031 }
2032 }
2033
2034 return [
2035 'options' => $options,
2036 'hasMore' => false,
2037 ];
2038 }
2039
2040 /**
2041 * Prepare Wishlist Memberlists level.
2042 *
2043 * @param array $data Search Params.
2044 *
2045 * @return array[]
2046 */
2047 public function search_wishlistmember_lists( $data ) {
2048
2049 $wlm_levels = wlmapi_get_levels();
2050 $options = [];
2051
2052 if ( ! empty( $wlm_levels ) ) {
2053 foreach ( $wlm_levels['levels']['level'] as $list ) {
2054 $options[] = [
2055 'label' => $list['name'],
2056 'value' => (string) $list['id'],
2057 ];
2058 }
2059 }
2060
2061 return [
2062 'options' => $options,
2063 'hasMore' => false,
2064 ];
2065 }
2066
2067 /**
2068 * Prepare elementor popups.
2069 *
2070 * @param array $data Search Params.
2071 *
2072 * @return array[]
2073 */
2074 public function search_elementor_popups( $data ) {
2075
2076 $posts = get_posts(
2077 [
2078 'post_type' => 'elementor_library',
2079 'orderby' => 'title',
2080 'order' => 'ASC',
2081 'post_status' => 'publish',
2082 'meta_query' => [
2083 [
2084 'key' => '_elementor_template_type',
2085 'value' => 'popup',
2086 'compare' => '=',
2087 ],
2088 ],
2089 ]
2090 );
2091
2092 $options = [];
2093 if ( ! empty( $posts ) ) {
2094 foreach ( $posts as $post ) {
2095 $options[] = [
2096 'label' => $post->post_title,
2097 'value' => $post->ID,
2098 ];
2099 }
2100 }
2101
2102 return [
2103 'options' => $options,
2104 'hasMore' => false,
2105 ];
2106 }
2107
2108 /**
2109 * Prepare givewp forms.
2110 *
2111 * @param array $data Search Params.
2112 *
2113 * @return array[]
2114 */
2115 public function search_givewp_forms( $data ) {
2116
2117 $posts = get_posts(
2118 [
2119 'post_type' => 'give_forms',
2120 'orderby' => 'title',
2121 'order' => 'ASC',
2122 'post_status' => 'publish',
2123 ]
2124 );
2125
2126 $options = [];
2127 if ( ! empty( $posts ) ) {
2128 foreach ( $posts as $post ) {
2129 $options[] = [
2130 'label' => $post->post_title,
2131 'value' => $post->ID,
2132 ];
2133 }
2134 }
2135
2136 return [
2137 'options' => $options,
2138 'hasMore' => false,
2139 ];
2140 }
2141
2142 /**
2143 * Prepare buddyboss group users.
2144 *
2145 * @param array $data Search Params.
2146 *
2147 * @return array[]
2148 */
2149 public function search_bb_group_users( $data ) {
2150 $options = [];
2151
2152 $group_id = $data['dynamic'];
2153 $admins = groups_get_group_admins( $group_id );
2154
2155 if ( ! empty( $admins ) ) {
2156 foreach ( $admins as $admin ) {
2157 $admin_user = get_user_by( 'id', $admin->user_id );
2158 $options[] = [
2159 'label' => $admin_user->display_name,
2160 'value' => $admin_user->ID,
2161 ];
2162 }
2163 }
2164
2165 $members = groups_get_group_members( [ 'group_id' => $group_id ] );
2166
2167 if ( isset( $members['members'] ) && ! empty( $members['members'] ) ) {
2168 foreach ( $members['members'] as $member ) {
2169 $options[] = [
2170 'label' => $member->display_name,
2171 'value' => $member->ID,
2172 ];
2173 }
2174 }
2175 return [
2176 'options' => $options,
2177 'hasMore' => false,
2178 ];
2179 }
2180
2181 /**
2182 * Prepare buddyboss groups.
2183 *
2184 * @param array $data Search Params.
2185 *
2186 * @return array[]
2187 */
2188 public function search_buddyboss_groups( $data ) {
2189 global $wpdb;
2190
2191 $options = [];
2192 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
2193 if ( ! empty( $groups ) ) {
2194 foreach ( $groups as $group ) {
2195 $options[] = [
2196 'label' => $group->name,
2197 'value' => $group->id,
2198 ];
2199 }
2200 }
2201 return [
2202 'options' => $options,
2203 'hasMore' => false,
2204 ];
2205 }
2206
2207 /**
2208 * Prepare buddyboss public groups.
2209 *
2210 * @param array $data Search Params.
2211 *
2212 * @return array[]
2213 */
2214 public function search_buddyboss_public_groups( $data ) {
2215 $options = [];
2216 $groups = groups_get_groups();
2217 if ( isset( $groups['groups'] ) && ! empty( $groups['groups'] ) ) {
2218 foreach ( $groups['groups'] as $group ) {
2219 if ( 'public' === $group->status ) {
2220 $options[] = [
2221 'label' => $group->name,
2222 'value' => $group->id,
2223 ];
2224 }
2225 }
2226 }
2227 return [
2228 'options' => $options,
2229 'hasMore' => false,
2230 ];
2231 }
2232
2233 /**
2234 * Prepare elementor forms.
2235 *
2236 * @param array $data Search Params.
2237 *
2238 * @return array[]
2239 */
2240 public function search_elementor_forms( $data ) {
2241
2242 $elementor_forms = Utilities::get_elementor_forms();
2243
2244 $options = [];
2245 if ( ! empty( $elementor_forms ) ) {
2246 foreach ( $elementor_forms as $key => $value ) {
2247 $options[] = [
2248 'label' => $value,
2249 'value' => $key,
2250 ];
2251 }
2252 }
2253
2254 return [
2255 'options' => $options,
2256 'hasMore' => false,
2257 ];
2258 }
2259
2260 /**
2261 * Prepare elementor form fields.
2262 *
2263 * @param array $data Search Params.
2264 *
2265 * @return array[]
2266 */
2267 public function search_pluggable_elementor_form_fields( $data ) {
2268 $result = [];
2269 $form_id = absint( $data['dynamic'] );
2270 $elementor_form_fields = ( new Utilities() )->get_elementor_form_fields( $data );
2271 $options = [];
2272 if ( ! empty( $elementor_form_fields ) ) {
2273 foreach ( $elementor_form_fields as $key => $value ) {
2274 $options[] = [
2275 'label' => $value,
2276 'value' => '{' . $key . '}',
2277 ];
2278 }
2279 }
2280
2281 return [
2282 'options' => $options,
2283 'hasMore' => false,
2284 ];
2285 }
2286
2287 /**
2288 * Get all events
2289 *
2290 * @param array $data Data array.
2291 *
2292 * @return array
2293 */
2294 public function search_event_calendar_event( $data ) {
2295 $page = $data['page'];
2296 $limit = Utilities::get_search_page_limit();
2297 $offset = $limit * ( $page - 1 );
2298
2299 $posts = get_posts(
2300 [
2301 'post_type' => 'tribe_events',
2302 'orderby' => 'title',
2303 'order' => 'ASC',
2304 'post_status' => 'publish',
2305 'posts_per_page' => $limit,
2306 'offset' => $offset,
2307 ]
2308 );
2309
2310 $options = [];
2311 if ( ! empty( $posts ) ) {
2312 foreach ( $posts as $post ) {
2313 $options[] = [
2314 'label' => $post->post_title,
2315 'value' => $post->ID,
2316 ];
2317 }
2318 }
2319
2320 $count = count( $options );
2321
2322 return [
2323 'options' => $options,
2324 'hasMore' => $count > $limit && $count > $offset,
2325 ];
2326 }
2327
2328 /**
2329 * Prepare rsvp event calendar events.
2330 *
2331 * @param array $data Search Params.
2332 *
2333 * @return array[]
2334 */
2335 public function search_event_calendar_rsvp_event( $data ) {
2336
2337 $posts = get_posts(
2338 [
2339 'post_type' => 'tribe_events',
2340 'orderby' => 'title',
2341 'order' => 'ASC',
2342 'post_status' => 'publish',
2343 ]
2344 );
2345
2346 $options = [];
2347 if ( ! empty( $posts ) ) {
2348 $ticket_handler = new Tribe__Tickets__Tickets_Handler();
2349 foreach ( $posts as $post ) {
2350
2351 $get_rsvp_ticket = $ticket_handler->get_event_rsvp_tickets( $post->ID );
2352
2353 if ( ! empty( $get_rsvp_ticket ) ) {
2354 $options[] = [
2355 'label' => $post->post_title,
2356 'value' => $post->ID,
2357 ];
2358 }
2359 }
2360 }
2361
2362 return [
2363 'options' => $options,
2364 'hasMore' => false,
2365 ];
2366 }
2367
2368 /**
2369 * Prepare Restrict Content Membership Level.
2370 *
2371 * @param array $data Search Params.
2372 *
2373 * @return array[]
2374 */
2375 public function search_restrictcontent_membership_level( $data ) {
2376
2377 $rcp_memberships = rcp_get_membership_levels();
2378 $options = [];
2379
2380 if ( ! empty( $rcp_memberships ) ) {
2381 foreach ( $rcp_memberships as $list ) {
2382 $options[] = [
2383 'label' => ucfirst( $list->name ),
2384 'value' => $list->id,
2385 ];
2386 }
2387 }
2388
2389 return [
2390 'options' => $options,
2391 'hasMore' => false,
2392 ];
2393 }
2394
2395 /**
2396 * Prepare Restrict Content Customer.
2397 *
2398 * @param array $data Search Params.
2399 *
2400 * @return array[]
2401 */
2402 public function search_restrictcontent_customer( $data ) {
2403
2404 $rcp_users = rcp_get_memberships();
2405 $options = [];
2406
2407 if ( ! empty( $rcp_users ) ) {
2408 foreach ( $rcp_users as $list ) {
2409 $user = get_user_by( 'ID', $list->get_user_id() );
2410 $user_label = $user->user_email;
2411
2412 if ( $user->display_name !== $user->user_email ) {
2413 $user_label .= ' (' . $user->display_name . ')';
2414 }
2415
2416 $options[] = [
2417 'label' => $user_label,
2418 'value' => $list->get_customer_id(),
2419 ];
2420 }
2421 }
2422
2423 return [
2424 'options' => $options,
2425 'hasMore' => false,
2426 ];
2427 }
2428
2429
2430 /**
2431 * Fetch the Presto Player video List.
2432 *
2433 * @param array $data Search Params.
2434 *
2435 * @return array[]
2436 */
2437 public function search_ap_presto_player_video_list( $data ) {
2438
2439 $videos = ( new Video() )->all();
2440 $options = [];
2441 if ( ! empty( $videos ) ) {
2442 foreach ( $videos as $video ) {
2443 $options[] = [
2444 'label' => $video->__get( 'title' ),
2445 'value' => (string) $video->__get( 'id' ),
2446 ];
2447 }
2448 }
2449
2450 return [
2451 'options' => $options,
2452 'hasMore' => false,
2453 ];
2454 }
2455
2456 /**
2457 * Presto Player Video percentage.
2458 *
2459 * @param array $data Search Params.
2460 *
2461 * @return array[]
2462 */
2463 public function search_prestoplayer_video_percent( $data ) {
2464
2465 $percents = [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ];
2466 $options = [];
2467
2468 foreach ( $percents as $percent ) {
2469 $options[] = [
2470 'label' => $percent . '%',
2471 'value' => (string) $percent,
2472 ];
2473 }
2474
2475 return [
2476 'options' => $options,
2477 'hasMore' => false,
2478 ];
2479 }
2480
2481 /**
2482 * Get user profile field options.
2483 *
2484 * @return array
2485 * @since 1.0.0
2486 */
2487 public function search_user_field_options() {
2488
2489 $options = apply_filters(
2490 'sure_trigger_get_user_field_options',
2491 [
2492 [
2493 'label' => __( 'User Name', 'suretriggers' ),
2494 'value' => 'user_login',
2495 ],
2496 [
2497 'label' => __( 'User Email', 'suretriggers' ),
2498 'value' => 'user_email',
2499 ],
2500 [
2501 'label' => __( 'Display Name', 'suretriggers' ),
2502 'value' => 'display_name',
2503 ],
2504 [
2505 'label' => __( 'User Password', 'suretriggers' ),
2506 'value' => 'user_pass',
2507 ],
2508 [
2509 'label' => __( 'Website', 'suretriggers' ),
2510 'value' => 'user_url',
2511 ],
2512 ]
2513 );
2514
2515 return [
2516 'options' => $options,
2517 'hasMore' => false,
2518 ];
2519 }
2520
2521 /**
2522 * Get user post field options.
2523 *
2524 * @return array
2525 * @since 1.0.0
2526 */
2527 public function search_post_field_options() {
2528
2529 return [
2530 'options' => [
2531 [
2532 'label' => __( 'Type', 'suretriggers' ),
2533 'value' => 'post_type',
2534 'dynamic_field' => [
2535 'type' => 'select-creatable',
2536 'ajaxIdentifier' => 'post_types',
2537 ],
2538 ],
2539 [
2540 'label' => __( 'Status', 'suretriggers' ),
2541 'value' => 'post_status',
2542 'dynamic_field' => [
2543 'type' => 'select-async',
2544 'ajaxIdentifier' => 'post_statuses',
2545 ],
2546 ],
2547 [
2548 'label' => __( 'Author', 'suretriggers' ),
2549 'value' => 'post_author',
2550 'dynamic_field' => [
2551 'type' => 'select-async',
2552 'ajaxIdentifier' => 'user',
2553 ],
2554 ],
2555 [
2556 'label' => __( 'Title', 'suretriggers' ),
2557 'value' => 'post_title',
2558 'dynamic_field' => [
2559 'type' => 'select-creatable',
2560 ],
2561 ],
2562 [
2563 'label' => __( 'Slug', 'suretriggers' ),
2564 'value' => 'post_slug',
2565 'dynamic_field' => [
2566 'type' => 'select-creatable',
2567 ],
2568 ],
2569 [
2570 'label' => __( 'Content', 'suretriggers' ),
2571 'value' => 'post_content',
2572 'dynamic_field' => [
2573 'type' => 'html-editor',
2574 ],
2575 ],
2576 ],
2577 'hasMore' => false,
2578 ];
2579 }
2580
2581 /**
2582 * Bricksbuilder grouped data.
2583 *
2584 * @param array $data data.
2585 * @return array
2586 */
2587 public function search_bb_groups( $data ) {
2588
2589 global $wpdb;
2590 $options = [];
2591
2592 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'bp_groups' ) ) ) {
2593
2594 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %s', $wpdb->prefix . 'bp_groups' ) );
2595
2596 if ( $results ) {
2597 foreach ( $results as $result ) {
2598 $options[] = [
2599 'label' => $result->name,
2600 'value' => $result->id,
2601 ];
2602 }
2603 }
2604 }
2605
2606 return [
2607 'options' => $options,
2608 'hasMore' => false,
2609 ];
2610 }
2611
2612 /**
2613 * Search forms.
2614 *
2615 * @return array
2616 */
2617 public function search_bb_forums() {
2618 $options = [];
2619 $allowed_atatus = [ 'publish', 'private' ];
2620 $forum_args = [
2621 'post_type' => bbp_get_forum_post_type(),
2622 'posts_per_page' => -1,
2623 'orderby' => 'title',
2624 'order' => 'ASC',
2625 'post_status' => 'any',
2626 ];
2627 $forums = get_posts( $forum_args );
2628
2629 if ( ! empty( $forums ) ) {
2630 foreach ( $forums as $forum ) {
2631 if ( in_array( $forum->post_status, $allowed_atatus, true ) ) {
2632 $options[] = [
2633 'label' => $forum->post_title,
2634 'value' => $forum->ID,
2635 ];
2636 }
2637 }
2638 }
2639 return [
2640 'options' => $options,
2641 'hasMore' => false,
2642 ];
2643 }
2644
2645 /**
2646 * Get last data for trigger.
2647 *
2648 * @param array $data data.
2649 * @return array
2650 */
2651 public function search_affiliate_wp_triggers_last_data( $data ) {
2652 global $wpdb;
2653
2654 $context = [];
2655 $context['response_type'] = 'sample';
2656
2657 $user_data = WordPress::get_sample_user_context();
2658
2659 $affiliate_data = [
2660 'affiliate_id' => 1,
2661 'rest_id' => '',
2662 'user_id' => 1,
2663 'rate' => '',
2664 'rate_type' => '',
2665 'flat_rate_basis' => '',
2666 'payment_email' => 'admin@bsf.io',
2667 'status' => 'active',
2668 'earnings' => 0,
2669 'unpaid_earnings' => 0,
2670 'referrals' => 0,
2671 'visits' => 0,
2672 'date_registered' => '2023-01-18 13:35:22',
2673 'dynamic_coupon' => 'KDJSKS',
2674 ];
2675
2676 $referral_data = [
2677 'referral_id' => 1,
2678 'affiliate_id' => 1,
2679 'visit_id' => 0,
2680 'rest_id' => '',
2681 'customer_id' => 0,
2682 'parent_id' => 0,
2683 'description' => 'Testing',
2684 'status' => 'unpaid',
2685 'amount' => '12.00',
2686 'currency' => '',
2687 'custom' => 'custom',
2688 'context' => '',
2689 'campaign' => '',
2690 'reference' => 'Reference',
2691 'products' => '',
2692 'date' => '2023-01-18 16:36:59',
2693 'type' => 'opt-in',
2694 'payout_id' => 0,
2695 ];
2696
2697 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
2698
2699 if ( in_array( $term, [ 'affiliate_approved', 'affiliate_awaiting_approval' ], true ) ) {
2700 $affiliate = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_affiliates WHERE affiliate_id = ( SELECT max(affiliate_id) FROM {$wpdb->prefix}affiliate_wp_affiliates )" );
2701
2702 if ( ! empty( $affiliate ) ) {
2703 $affiliate = affwp_get_affiliate( $affiliate->affiliate_id );
2704 $affiliate_data = get_object_vars( $affiliate );
2705 $user_data = WordPress::get_user_context( $affiliate->user_id );
2706 $context['response_type'] = 'live';
2707 }
2708 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data );
2709
2710 } else {
2711 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE referral_id = ( SELECT max(referral_id) FROM {$wpdb->prefix}affiliate_wp_referrals )" );
2712
2713 if ( ! empty( $referral ) ) {
2714 $referral = affwp_get_referral( $referral->referral_id );
2715 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
2716 $affiliate_data = get_object_vars( $affiliate );
2717 $user_data = WordPress::get_user_context( $affiliate->user_id );
2718 $referral_data = get_object_vars( $referral );
2719 $context['response_type'] = 'live';
2720 }
2721 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
2722 }
2723
2724 return $context;
2725 }
2726
2727 /**
2728 * Get last data for trigger.
2729 *
2730 * @param array $data data.
2731 * @return array
2732 */
2733 public function search_edd_triggers_last_data( $data ) {
2734 $context = [];
2735 $context['response_type'] = 'sample';
2736
2737 $order_data = [
2738 'order_id' => '1',
2739 'customer_email' => 'john_doe@bsf.io',
2740 'customer_first_name' => 'John',
2741 'customer_last_name' => 'Doe',
2742 'ordered_items' => 'Any Sample Book',
2743 'currency' => 'USD',
2744 'status' => 'complete',
2745 'discount_codes' => 'KDJSKS',
2746 'order_discounts' => '2.00',
2747 'order_subtotal' => '48.00',
2748 'order_tax' => '0.00',
2749 'order_total' => '48.00',
2750 'payment_method' => 'stripe',
2751 ];
2752
2753 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
2754
2755 if ( 'order_created' === $term ) {
2756 $order_data['purchase_key'] = '06d3b7d923ca922dc889354f9bc33fbb';
2757
2758 $args = [
2759 'number' => 1,
2760 'status' => [ 'complete', 'refunded', 'partially_refunded' ],
2761 ];
2762 $payments = edd_get_payments( $args );
2763
2764 if ( count( $payments ) > 0 ) {
2765 $order_data = EDD::get_product_purchase_context( $payments[0] );
2766 $context['response_type'] = 'live';
2767 }
2768 } else {
2769 $args = [
2770 'number' => 1,
2771 'status' => 'complete',
2772 'type' => 'refund',
2773 ];
2774 $payments = edd_get_payments( $args );
2775
2776 if ( count( $payments ) > 0 ) {
2777 $order_data = EDD::get_purchase_refund_context( $payments[0] );
2778 $context['response_type'] = 'live';
2779 }
2780 }
2781
2782 $context['pluggable_data'] = $order_data;
2783 return $context;
2784 }
2785
2786 /**
2787 * Get last data for trigger.
2788 *
2789 * @param array $data data.
2790 * @return array
2791 */
2792 public function search_presto_player_triggers_last_data( $data ) {
2793 $context = [];
2794 $context['response_type'] = 'sample';
2795
2796 $user_data = WordPress::get_sample_user_context();
2797
2798 $video_data = [
2799 'pp_video' => '1',
2800 'pp_video_percentage' => '100',
2801 'video_id' => '1',
2802 'video_title' => 'SureTriggers Is Here 🎉 The Easiest Automation Builder WordPress Websites & Apps',
2803 'video_type' => 'youtube',
2804 'video_external_id' => '-cYbNYgylLs',
2805 'video_attachment_id' => '0',
2806 'video_post_id' => '127',
2807 'video_src' => 'https://www.youtube.com/watch?v=-cYbNYgylLs',
2808 'video_created_by' => '1',
2809 'video_created_at' => '2022-11-10 00:28:25',
2810 'video_updated_at' => '2022-11-10 00:34:40',
2811 'video_deleted_at' => '',
2812 ];
2813
2814 $videos = ( new Video() )->all();
2815
2816 if ( count( $videos ) > 0 ) {
2817 $video_id = '-1' === $data['filter']['pp_video']['value'] ? $videos[0]->id : $data['filter']['pp_video']['value'];
2818 $video_data = ( new Video( $video_id ) )->toArray();
2819 $video_data['pp_video'] = $video_id;
2820 $video_data['pp_video_percentage'] = isset( $data['filter']['pp_video_percentage']['value'] ) ? $data['filter']['pp_video_percentage']['value'] : '100';
2821 $user_data = WordPress::get_user_context( $video_data['created_by'] );
2822
2823 $context['response_type'] = 'live';
2824 }
2825
2826 $context['pluggable_data'] = array_merge( $user_data, $video_data );
2827
2828 return $context;
2829 }
2830
2831 /**
2832 * Get last data for trigger.
2833 *
2834 * @param array $data data.
2835 * @return array
2836 */
2837 public function search_member_press_triggers_last_data( $data ) {
2838 global $wpdb;
2839
2840 $context = [];
2841 $context['response_type'] = 'sample';
2842
2843 $user_data = WordPress::get_sample_user_context();
2844
2845 $membership_data = [
2846 'membership_id' => '190',
2847 'membership_title' => 'Sample Membership',
2848 'amount' => '12.00',
2849 'total' => '12.00',
2850 'tax_amount' => '0.00',
2851 'tax_rate' => '0.00',
2852 'trans_num' => 't_63a03f3334f44',
2853 'status' => 'complete',
2854 'subscription_id' => '0',
2855 'membership_url' => site_url() . '/register/premium/',
2856 'membership_featured_image_id' => '521',
2857 'membership_featured_image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
2858 ];
2859
2860 $membership_id = (int) ( isset( $data['filter']['membership_id']['value'] ) ? $data['filter']['membership_id']['value'] : '-1' );
2861
2862 if ( $membership_id > 0 ) {
2863
2864 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE product_id= %s ORDER BY id DESC LIMIT 1", $membership_id ) );
2865 } else {
2866 $subscription = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mepr_transactions ORDER BY id DESC LIMIT 1" );
2867 }
2868
2869 if ( ! empty( $subscription ) ) {
2870 $membership_data = MemberPress::get_membership_context( $subscription );
2871 $user_data = WordPress::get_user_context( $subscription->user_id );
2872
2873 $context['response_type'] = 'live';
2874 }
2875
2876 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
2877
2878 return $context;
2879 }
2880
2881 /**
2882 * Get last data for trigger.
2883 *
2884 * @param array $data data.
2885 * @return array
2886 */
2887 public function search_wishlist_member_triggers_last_data( $data ) {
2888 global $wpdb;
2889
2890 $context = [];
2891 $context['response_type'] = 'sample';
2892
2893 $user_data = WordPress::get_sample_user_context();
2894
2895 $membership_data = [
2896 'membership_level_id' => '1',
2897 'membership_level_name' => 'Sample Membership Level',
2898 ];
2899
2900 $membership_level_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
2901
2902 if ( $membership_level_id > 0 ) {
2903 $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 ) );
2904 } else {
2905 $membership = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}wlm_userlevels ORDER BY id DESC LIMIT 1" );
2906 }
2907 if ( ! empty( $membership ) ) {
2908 $membership_data = WishlistMember::get_membership_detail_context( (int) $membership->level_id );
2909 $user_data = WordPress::get_user_context( $membership->user_id );
2910
2911 $context['response_type'] = 'live';
2912 }
2913
2914 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
2915
2916 return $context;
2917 }
2918
2919 /**
2920 * Get last data for trigger.
2921 *
2922 * @param array $data data.
2923 * @return array
2924 */
2925 public function search_peepso_triggers_last_data( $data ) {
2926 global $wpdb;
2927
2928 $context = [];
2929 $context['response_type'] = 'sample';
2930
2931 $user_data = WordPress::get_sample_user_context();
2932
2933 $post_data = [
2934 'post_id' => '1',
2935 'activity_id' => '2',
2936 'post_author' => '1',
2937 'post_content' => 'New sample post...!',
2938 'post_title' => 'Sample Post',
2939 'post_excerpt' => 'sample',
2940 'post_status' => 'publish',
2941 'post_type' => 'peepso-post',
2942 ];
2943
2944 $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" );
2945
2946 if ( ! empty( $post ) ) {
2947 $post_data = PeepSo::get_pp_activity_context( (int) $post->act_external_id, (int) $post->act_id );
2948 $user_data = WordPress::get_user_context( $post->act_owner_id );
2949
2950 $context['response_type'] = 'live';
2951 }
2952
2953 $context['pluggable_data'] = array_merge( $user_data, $post_data );
2954
2955 return $context;
2956 }
2957
2958 /**
2959 * Get last data for trigger
2960 *
2961 * @param array $data data.
2962 * @return array
2963 */
2964 public function search_restrict_content_pro_triggers_last_data( $data ) {
2965 $context = [];
2966 $context['response_type'] = 'sample';
2967
2968 $user_data = WordPress::get_sample_user_context();
2969
2970 $membership_data = [
2971 'membership_level_id' => '190',
2972 'membership_level' => 'Sample Membership',
2973 'membership_initial_payment' => '0.00',
2974 'membership_recurring_payment' => '0.00',
2975 'membership_expiry_date' => 'January 22, 2023',
2976 ];
2977
2978 $customer_id = (int) ( isset( $data['filter']['membership_customer_id']['value'] ) ? $data['filter']['membership_customer_id']['value'] : '-1' );
2979 $membership_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
2980
2981 $args = [
2982 'status' => 'expired',
2983 'number' => 1,
2984 'orderby' => 'id',
2985 ];
2986
2987 if ( -1 !== $customer_id ) {
2988 $args['customer_id'] = $customer_id;
2989 }
2990
2991 if ( -1 !== $membership_id ) {
2992 $args['object_id'] = $membership_id;
2993 }
2994
2995 $memberships = rcp_get_memberships( $args );
2996 if ( count( $memberships ) > 0 ) {
2997 $membership_data = RestrictContent::get_rcp_membership_detail_context( $memberships[0] );
2998 $user_data = WordPress::get_user_context( $memberships[0]->get_user_id() );
2999
3000 $context['response_type'] = 'live';
3001 }
3002
3003 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
3004
3005 return $context;
3006 }
3007
3008 /**
3009 * Get last data for trigger
3010 *
3011 * @param array $data data.
3012 * @return array
3013 */
3014 public function search_events_calendar_triggers_last_data( $data ) {
3015 $context = [];
3016 $context['response_type'] = 'sample';
3017
3018 $event_data = [
3019 'event' => [
3020 'ID' => 58,
3021 'post_author' => 1,
3022 'post_date' => '2023-01-19 09:27:58',
3023 'post_date_gmt' => '2023-01-19 09:27:58',
3024 'post_content' => '',
3025 'post_title' => 'New event',
3026 'post_excerpt' => '',
3027 'post_status' => 'publish',
3028 'comment_status' => 'open',
3029 'ping_status' => 'closed',
3030 'post_password' => '',
3031 'post_name' => 'new-event',
3032 'to_ping' => '',
3033 'pinged' => '',
3034 'post_modified' => '2023-01-19 09:44:25',
3035 'post_modified_gmt' => '2023-01-19 09:44:25',
3036 'post_content_filtered' => '',
3037 'post_parent' => 0,
3038 'guid' => 'http://connector.com/?post_type=tribe_events&#038;p=58',
3039 'menu_order' => -1,
3040 'post_type' => 'tribe_events',
3041 'post_mime_type' => '',
3042 'comment_count' => 0,
3043 'filter' => 'raw',
3044 ],
3045 'attendies' => [
3046 'order_id' => 68,
3047 'purchaser_name' => 'sapna Rana',
3048 'purchaser_email' => 'sapnar@bsf.io',
3049 'provider' => 'Tribe__Tickets__RSVP',
3050 'provider_slug' => 'rsvp',
3051 'purchase_time' => '2023-01-19 09:48:43',
3052 'optout' => 1,
3053 'ticket' => 'Prime',
3054 'attendee_id' => 68,
3055 'security' => '2cefc3b53e',
3056 'product_id' => 65,
3057 'check_in' => '',
3058 'order_status' => 'yes',
3059 'order_status_label' => 'Going',
3060 'user_id' => 1,
3061 'ticket_sent' => 1,
3062 'event_id' => 58,
3063 'ticket_name' => 'Prime',
3064 'holder_name' => 'sapna Rana',
3065 'holder_email' => 'sapnar@bsf.io',
3066 'ticket_id' => 68,
3067 'qr_ticket_id' => 68,
3068 'security_code' => '2cefc3b53e',
3069 'attendee_meta' => '',
3070 'is_subscribed' => '',
3071 'is_purchaser' => 1,
3072 'ticket_exists' => 1,
3073 ],
3074 ];
3075
3076 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
3077
3078 $args = [
3079 'post_type' => 'tribe_rsvp_attendees',
3080 'orderby' => 'ID',
3081 'order' => 'DESC',
3082 'post_status' => 'publish',
3083 'numberposts' => 1,
3084 ];
3085
3086 if ( -1 !== $event_id ) {
3087 $args['meta_query'] = [
3088 [
3089 'key' => '_tribe_rsvp_event',
3090 'value' => $event_id,
3091 ],
3092 ];
3093 }
3094
3095 $attendees = get_posts( $args );
3096
3097 if ( count( $attendees ) > 0 ) {
3098 $attendee = $attendees[0];
3099 $attendee_id = $attendee->ID;
3100
3101 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
3102 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
3103
3104 $event_data = TheEventCalendar::get_event_context( $product_id, $order_id );
3105 $context['response_type'] = 'live';
3106 }
3107
3108 $context['pluggable_data'] = $event_data;
3109
3110 return $context;
3111 }
3112
3113 /**
3114 * Get last data for trigger
3115 *
3116 * @param array $data data.
3117 * @return array
3118 */
3119 public function search_woo_commerce_triggers_last_data( $data ) {
3120 $context = [];
3121 $context['response_type'] = 'sample';
3122 $user_data = WordPress::get_sample_user_context();
3123
3124 $product_data['product'] = [
3125 'id' => '169',
3126 'name' => 'Sample Product',
3127 'description' => 'This is description of sample product.',
3128 'short_description' => 'This is short description of sample product.',
3129 'image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
3130 'slug' => 'sample-product',
3131 'status' => 'publish',
3132 'type' => 'simple',
3133 'price' => '89',
3134 'featured' => '0',
3135 'sku' => 'hoodie-blue-sm',
3136 'regular_price' => '90',
3137 'sale_price' => '89',
3138 'total_sales' => '21',
3139 'category' => 'Uncategorized',
3140 'tags' => 'sample, new, 2022',
3141 ];
3142
3143 $comment_data = [
3144 'comment_id' => '1',
3145 'comment' => 'This is a sample comment..!',
3146 ];
3147
3148 $order_data = [
3149 'order_id' => '500',
3150 'total_order_value' => '45',
3151 'currency' => 'USD',
3152 'shipping_total' => '5',
3153 'order_payment_method' => 'cod',
3154 'billing_firstname' => 'John',
3155 'billing_lastname' => 'Doe',
3156 'billing_company' => 'BSF',
3157 'billing_address_1' => '1004 Beaumont',
3158 'billing_address_2' => '',
3159 'billing_city' => 'Casper',
3160 'billing_state' => 'Wyoming',
3161 'billing_postcode' => '82601',
3162 'billing_country' => 'US',
3163 'billing_email' => 'john_doe@bsf.io',
3164 'billing_phone' => '(307) 7626541',
3165 'shipping_firstname' => 'John',
3166 'shipping_lastname' => 'Doe',
3167 'shipping_company' => 'BSF',
3168 'shipping_address_1' => '1004 Beaumont',
3169 'shipping_address_2' => '',
3170 'shipping_city' => 'Casper',
3171 'shipping_state' => 'Wyoming',
3172 'shipping_postcode' => '82601',
3173 'shipping_country' => 'US',
3174 'coupon_codes' => 'e3mstekq, f24sjakb',
3175 'total_items_in_order' => '1',
3176 'user_id' => '1',
3177 ];
3178
3179 $variation_data = [
3180 'product_variation_id' => '626',
3181 'product_variation' => 'Color: Silver',
3182 ];
3183
3184 $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
3185
3186 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
3187 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
3188
3189 if ( in_array( $term, [ 'product_added_to_cart', 'product_viewed' ], true ) ) {
3190 if ( -1 === $product_id ) {
3191 $args = [
3192 'post_type' => 'product',
3193 'orderby' => 'ID',
3194 'order' => 'DESC',
3195 'post_status' => 'publish',
3196 'numberposts' => 1,
3197 ];
3198 $products = get_posts( $args );
3199
3200 if ( count( $products ) > 0 ) {
3201 $product_id = $products[0]->ID;
3202 }
3203 }
3204
3205 if ( -1 !== $product_id ) {
3206 $post = get_post( $product_id );
3207 $user_data = WordPress::get_user_context( $post->post_author );
3208 $product_data['product_id'] = $product_id;
3209 $product_data['product'] = WooCommerce::get_product_context( $product_id );
3210 unset( $product_data['product']['id'] ); //phpcs:ignore
3211 $context['response_type'] = 'live';
3212 }
3213
3214 if ( 'product_added_to_cart' === $term ) {
3215 $product_data['product_quantity'] = 1;
3216 }
3217
3218 $context['pluggable_data'] = array_merge( $product_data, $user_data );
3219
3220 } elseif ( 'product_reviewed' === $term ) {
3221 $comment_args = [
3222 'number' => 1,
3223 'type' => 'review',
3224 'orderby' => 'comment_ID',
3225 'post_id' => -1 !== $product_id ? $product_id : 0,
3226 ];
3227
3228 $comments = get_comments( $comment_args );
3229
3230 if ( count( $comments ) > 0 ) {
3231 $comment = $comments[0];
3232 $comment_data = [
3233 'comment_id' => $comment->comment_ID,
3234 'comment' => $comment->comment_content,
3235 ];
3236 $product_data = WooCommerce::get_product_context( $comment->comment_post_ID );
3237 $user_data = WordPress::get_user_context( $comment->user_id );
3238 $context['response_type'] = 'live';
3239 }
3240
3241 $context['pluggable_data'] = array_merge( $product_data, $user_data, $comment_data );
3242
3243 } elseif ( 'product_purchased' === $term ) {
3244 $order_id = 0;
3245 $product_data['quantity'] = '1';
3246 if ( -1 !== $product_id ) {
3247 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
3248 if ( count( $order_ids ) > 0 ) {
3249 $order_id = $order_ids[0];
3250 }
3251 } else {
3252 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
3253 if ( count( $orders ) > 0 ) {
3254 $order_id = $orders[0]->get_id();
3255 }
3256 }
3257
3258 if ( 0 !== $order_id ) {
3259 $order = wc_get_order( $order_id );
3260
3261 if ( $order ) {
3262 $user_id = $order->get_customer_id();
3263 $items = $order->get_items();
3264
3265 $product_ids = [];
3266
3267 $iteration = 0;
3268 foreach ( $items as $item ) {
3269 $item_id = $item->get_product_id();
3270
3271 if ( -1 === $product_id && 0 === $iteration ) {
3272 $product_ids[] = $item_id;
3273 break;
3274 } elseif ( $item_id === $product_id ) {
3275 $product_ids[] = $item_id;
3276 break;
3277 }
3278
3279 $iteration++;
3280 }
3281 $order_data = WooCommerce::get_order_context( $order_id );
3282 $user_data = WordPress::get_user_context( $user_id );
3283 $order_data['total_items_in_order'] = count( $product_ids );
3284
3285 $context['response_type'] = 'live';
3286 }
3287 }
3288
3289 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
3290
3291 } elseif ( 'variable_product_purchased' === $term ) {
3292 $product_variation_id = (int) ( isset( $data['filter']['product_variation_id']['value'] ) ? $data['filter']['product_variation_id']['value'] : -1 );
3293 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
3294
3295 foreach ( $order_ids as $order_id ) {
3296 $order = wc_get_order( $order_id );
3297
3298 if ( $order ) {
3299 $user_id = $order->get_customer_id();
3300 $items = $order->get_items();
3301 $product_variations = [];
3302
3303 $iteration = 0;
3304 foreach ( $items as $item ) {
3305 $variation_id = $item->get_variation_id();
3306
3307 if ( -1 === $product_variation_id && 0 === $iteration ) {
3308 $product_variations[] = $variation_id;
3309 break;
3310 } elseif ( $variation_id === $product_variation_id ) {
3311 $product_variations[] = $variation_id;
3312 break;
3313 }
3314
3315 $iteration++;
3316 }
3317
3318 if ( count( $product_variations ) > 0 ) {
3319 $product_data = WooCommerce::get_product_context( $product_variation_id );
3320 $order_data = WooCommerce::get_order_context( $order_id );
3321 $user_data = WordPress::get_user_context( $user_id );
3322 $variation_data = [
3323 'product_variation_id' => $product_variations[0],
3324 'product_variation' => get_the_excerpt( $product_variations[0] ),
3325 ];
3326
3327 $context['response_type'] = 'live';
3328 break;
3329 }
3330 }
3331 }
3332
3333 $context['pluggable_data'] = array_merge( $order_data, $user_data, $variation_data );
3334
3335 } elseif ( 'variable_subscription_purchased' === $term ) {
3336 $product_data['quantity'] = '1';
3337 $product_data['product_name'] = 'Sample Product';
3338 $product_data['billing_period'] = '2021-2022';
3339
3340 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
3341
3342 $subscription_order_id = 0;
3343 $order_ids = [];
3344
3345 if ( -1 !== $product_id ) {
3346 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
3347
3348 } else {
3349 $orders = wc_get_orders( [] );
3350 if ( count( $orders ) > 0 ) {
3351 $order_ids[] = $orders[0]->get_id();
3352 }
3353 }
3354
3355 foreach ( $order_ids as $order_id ) {
3356 $query_args = [
3357 'post_type' => 'shop_subscription',
3358 'orderby' => 'ID',
3359 'order' => 'DESC',
3360 'post_status' => 'wc-active',
3361 'posts_per_page' => 1,
3362 'post_parent' => $order_id,
3363 ];
3364 $query_result = new WP_Query( $query_args );
3365 $subscription_orders = $query_result->get_posts();
3366
3367 if ( count( $subscription_orders ) > 0 ) {
3368 $subscription_order_id = $subscription_orders[0]->ID;
3369 break;
3370 }
3371 }
3372
3373 if ( 0 !== $subscription_order_id ) {
3374 $subscription = wcs_get_subscription( $subscription_order_id );
3375 if ( $subscription instanceof WC_Subscription ) {
3376 $last_order_id = $subscription->get_last_order();
3377 if ( ! empty( $last_order_id ) && $last_order_id === $subscription->get_parent_id() ) {
3378 $user_id = wc_get_order( $last_order_id )->get_customer_id();
3379 $items = $subscription->get_items();
3380
3381 foreach ( $items as $item ) {
3382 $product = $item->get_product();
3383 if ( class_exists( '\WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product ) ) {
3384 if ( $product->is_type( [ 'subscription', 'subscription_variation', 'variable-subscription' ] ) ) {
3385
3386 $product_data = WooCommerce::get_variable_subscription_product_context( $item, $last_order_id );
3387 $user_data = WordPress::get_user_context( $user_id );
3388
3389 $context['response_type'] = 'live';
3390 $context['pluggable_data'] = array_merge( $product_data, $user_data );
3391 }
3392 }
3393 }
3394 }
3395 }
3396 }
3397 } elseif ( 'order_created' === $term ) {
3398 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
3399 if ( count( $orders ) > 0 ) {
3400 $order_id = $orders[0]->get_id();
3401 }
3402 $order = wc_get_order( $order_id );
3403 $user_id = $order->get_customer_id();
3404 $order_sample_data = array_merge(
3405 WooCommerce::get_order_context( $order_id ),
3406 WordPress::get_user_context( $user_id )
3407 );
3408 $context['response_type'] = 'live';
3409 $context['pluggable_data'] = $order_sample_data;
3410
3411 }
3412
3413 return $context;
3414 }
3415
3416 /**
3417 * Search LMS data.
3418 *
3419 * @param array $data data.
3420 * @return array
3421 */
3422 public function search_lifter_lms_last_data( $data ) {
3423 global $wpdb;
3424 $post_type = $data['post_type'];
3425 $meta_key = '_is_complete';
3426 $trigger = $data['search_term'];
3427 $context = [];
3428 if ( 'lifterlms_purchase_course' === $trigger ) {
3429 $product_type = 'course';
3430 $post_id = $data['filter']['course_id']['value'];
3431 } elseif ( 'lifterlms_purchase_membership' === $trigger ) {
3432 $product_type = 'membership';
3433 $post_id = $data['filter']['membership_id']['value'];
3434 } elseif ( 'lifterlms_lesson_completed' === $trigger ) {
3435 $post_id = $data['filter']['lesson']['value'];
3436 } elseif ( 'lifterlms_course_completed' === $trigger ) {
3437 $post_id = $data['filter']['course']['value'];
3438 }
3439
3440 $where = 'postmeta.post_id = "' . $post_id . '" AND';
3441
3442 if ( 'llms_order' === $post_type ) {
3443 if ( -1 === $post_id ) {
3444 $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 ) );
3445 } else {
3446 $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 ) );
3447 }
3448 } else {
3449 if ( -1 === $post_id ) {
3450 $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 ) );
3451 } else {
3452 $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 ) );
3453 }
3454 }
3455
3456 $response = [];
3457 if ( ! empty( $result ) ) {
3458 $result_post_id = $result[0]->post_id;
3459 $result_user_id = $result[0]->user_id;
3460
3461 switch ( $trigger ) {
3462 case 'lifterlms_lesson_completed':
3463 $context = array_merge(
3464 WordPress::get_user_context( $result_user_id ),
3465 LifterLMS::get_lms_lesson_context( $result_post_id )
3466 );
3467
3468 $context['course'] = get_the_title( get_post_meta( $result_post_id, '_llms_parent_course', true ) );
3469 if ( '' !== ( get_post_meta( $result_post_id, '_llms_parent_section', true ) ) ) {
3470 $context['parent_section'] = get_the_title( get_post_meta( $result_post_id, '_llms_parent_section', true ) );
3471 }
3472 break;
3473 case 'lifterlms_course_completed':
3474 $context = array_merge(
3475 WordPress::get_user_context( $result_user_id ),
3476 LifterLMS::get_lms_course_context( $result_post_id )
3477 );
3478 break;
3479 case 'lifterlms_purchase_course':
3480 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
3481 $context['course_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
3482 $context['course_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
3483 $context['course_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
3484 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
3485 $context ['order'] = WordPress::get_post_context( $result_post_id );
3486 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
3487 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
3488 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
3489 $context = array_merge( $context, WordPress::get_user_context( $user_id ) );
3490 break;
3491 case 'lifterlms_purchase_membership':
3492 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
3493 $context['membership_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
3494 $context['membership_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
3495 $context['membership_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
3496 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
3497 $context ['order'] = WordPress::get_post_context( $result_post_id );
3498 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
3499 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
3500 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
3501 $context = array_merge( $context, WordPress::get_user_context( $user_id ) );
3502 break;
3503 default:
3504 return;
3505
3506 }
3507 $response['pluggable_data'] = $context;
3508 $response['response_type'] = 'live';
3509
3510 }
3511
3512 return $response;
3513
3514 }
3515
3516 /**
3517 * Search SM data.
3518 *
3519 * @param array $data data.
3520 * @return array
3521 */
3522 public function search_suremember_last_data( $data ) {
3523 global $wpdb;
3524 $post_type = $data['post_type'];
3525 $meta_key = '_is_complete';
3526 $trigger = $data['search_term'];
3527 $post_id = $data['filter']['group_id']['value'];
3528
3529 if ( 'suremember_updated_group' === $trigger ) {
3530 if ( -1 === $post_id ) {
3531 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts as posts WHERE posts.post_type=%s", $post_type ) );
3532 } else {
3533 $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 ) );
3534 }
3535 } else {
3536 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}usermeta as usermeta WHERE usermeta.meta_key = %s", 'suremembers_user_access_group_' . $post_id ) );
3537 }
3538
3539 $response = [];
3540
3541 if ( ! empty( $result ) ) {
3542 $context = [];
3543 switch ( $trigger ) {
3544 case 'suremember_updated_group':
3545 $group_id = $result[0]->ID;
3546 $suremembers_post['rules'] = get_post_meta( $group_id, 'suremembers_plan_include', true );
3547 $suremembers_post['exclude'] = get_post_meta( $group_id, 'suremembers_plan_exclude', true ); //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
3548 $suremembers_post['suremembers_user_roles'] = get_post_meta( $group_id, 'suremembers_user_roles', true );
3549 $suremembers_post['title'] = get_the_title( $group_id );
3550 $suremembers_post['restrict'] = get_post_meta( $group_id, 'suremembers_plan_rules', true )['restrict'];
3551 $context['group'] = array_merge( WordPress::get_post_context( $group_id ), $suremembers_post );
3552 $context['group_id'] = $group_id;
3553 unset( $context['group']['ID'] );
3554 $response['pluggable_data'] = $context;
3555 $response['response_type'] = 'live';
3556 break;
3557 case 'suremember_user_added_in_group':
3558 foreach ( $result as $res ) {
3559 $meta_value = unserialize( $res->meta_value );
3560 if ( 'active' === $meta_value['status'] ) {
3561 $context = WordPress::get_user_context( $res->user_id );
3562 $context['group'] = WordPress::get_post_context( $post_id );
3563 $response['pluggable_data'] = $context;
3564 $response['response_type'] = 'live';
3565 }
3566 }
3567 break;
3568 case 'suremember_user_removed_from_group':
3569 foreach ( $result as $res ) {
3570 $meta_value = unserialize( $res->meta_value );
3571 if ( 'revoked' === $meta_value['status'] ) {
3572 $context = WordPress::get_user_context( $res->user_id );
3573 $context['group'] = WordPress::get_post_context( $post_id );
3574 $response['pluggable_data'] = $context;
3575 $response['response_type'] = 'live';
3576 }
3577 }
3578 break;
3579 default:
3580 return;
3581
3582 }
3583 }
3584
3585 return $response;
3586
3587 }
3588
3589 /**
3590 * Search CartFlows data.
3591 *
3592 * @param array $data data.
3593 * @return array
3594 */
3595 public function search_cartflows_last_data( $data ) {
3596 global $wpdb;
3597 $trigger = $data['search_term'];
3598 $context = [];
3599 if ( 'cartflows_offer_accepted' === $trigger ) {
3600 $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'" );
3601 }
3602 $response = [];
3603 if ( ! empty( $result ) ) {
3604 $context = [];
3605 $order_upsell_id = $result[0]->post_id;
3606 $step_id = get_post_meta( $order_upsell_id, '_cartflows_offer_step_id', true );
3607 $order_id = get_post_meta( $order_upsell_id, '_cartflows_offer_parent_id', true );
3608 $order = wc_get_order( $order_id );
3609 $upsell_order = wc_get_order( $order_upsell_id );
3610 $variation_id = $upsell_order->get_items()[0]['product_id'];
3611 $input_qty = $upsell_order->get_items()[0]['quantity'];
3612 $offer_product = wcf_pro()->utils->get_offer_data( $step_id, $variation_id, $input_qty, $order_id );
3613 $user_id = get_post_meta( $order_upsell_id, '_customer_user', true );
3614 $context = WordPress::get_user_context( $user_id );
3615 $context['order'] = $order->get_data();
3616 $context['upsell'] = $offer_product;
3617 $response['pluggable_data'] = $context;
3618 $response['response_type'] = 'live';
3619 }
3620
3621 return $response;
3622
3623 }
3624
3625
3626 /**
3627 * Fetch user context.
3628 *
3629 * @param int $initiator_id initiator id.
3630 * @param int $friend_id friend id.
3631 * @return array
3632 */
3633 public function get_user_context( $initiator_id, $friend_id ) {
3634 $context = WordPress::get_user_context( $initiator_id );
3635
3636 $friend_context = WordPress::get_user_context( $friend_id );
3637
3638 $avatar = get_avatar_url( $initiator_id );
3639
3640 $context['avatar_url'] = ( $avatar ) ? $avatar : '';
3641
3642 $context['friend_id'] = $friend_id;
3643 $context['friend_first_name'] = $friend_context['user_firstname'];
3644 $context['friend_last_name'] = $friend_context['user_lastname'];
3645 $context['friend_email'] = $friend_context['user_email'];
3646
3647 $friend_avatar = get_avatar_url( $friend_id );
3648 $context['friend_avatar_url'] = $friend_avatar;
3649 return $context;
3650 }
3651
3652 /**
3653 * Search BP data.
3654 *
3655 * @param array $data data.
3656 * @return array
3657 */
3658 public function search_pluggables_bp_friendships( $data ) {
3659 global $wpdb, $bp;
3660 $context = [];
3661 $sample['pluggable_data'] = [
3662 'wp_user_id' => 4,
3663 'user_login' => 'katy1ßßßß',
3664 'display_name' => 'Katy Smith',
3665 'user_firstname' => 'Katy',
3666 'user_lastname' => 'Smith',
3667 'user_email' => 'katy1@gmail.com',
3668 'user_role' => [ 'subscriber' ],
3669 'avatar_url' => 'http://pqr.com/avatar',
3670 'friend_id' => 1,
3671 'friend_first_name' => 'John',
3672 'friend_last_name' => 'Wick',
3673 'friend_email' => 'john@gmail.com',
3674 'friend_avatar_url' => 'http://abc.com/avatar',
3675 ];
3676 $sample['response_type'] = 'sample';
3677
3678 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_friends'" );
3679 if ( $table_exists ) {
3680 $friendships = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_friends LIMIT 1" );
3681 if ( ! empty( $friendships ) ) {
3682 $friendship = $friendships[0];
3683 $initiator_id = $friendship['initiator_user_id'];
3684 $friend_user_id = $friendship['friend_user_id'];
3685 $context['pluggable_data'] = $this->get_user_context( $initiator_id, $friend_user_id );
3686 $context['response_type'] = 'live';
3687 } else {
3688 $context = $sample;
3689 }
3690 } else {
3691 $context = $sample;
3692 }
3693
3694
3695 return $context;
3696 }
3697
3698 /**
3699 * Search BP data.
3700 *
3701 * @param array $data data.
3702 * @return array
3703 */
3704 public function search_pluggables_bp_groups( $data ) {
3705 global $wpdb, $bp;
3706 $context = [];
3707 $group_data = [];
3708 $args = [
3709 'orderby' => 'user_nicename',
3710 'order' => 'ASC',
3711 'number' => 1,
3712 ];
3713
3714 $users = get_users( $args );
3715
3716 if ( isset( $data['filter']['group_id']['value'] ) ) {
3717 $group_id = $data['filter']['group_id']['value'];
3718 $args['group_id'] = $group_id;
3719 if ( $group_id > 0 ) {
3720 $group = groups_get_group( $group_id );
3721 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
3722 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
3723 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
3724 } else {
3725 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_groups'" );
3726 if ( $table_exists ) {
3727 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups LIMIT 1" );
3728 $context['groups'] = $groups;
3729 if ( ! empty( $groups ) ) {
3730 foreach ( $groups as $group ) {
3731 $group_data['group_id'] = $group->id;
3732 $group_data['group_name'] = $group->name;
3733 $group_data['group_description'] = $group->description;
3734 }
3735 }
3736 }
3737 }
3738 }
3739
3740 if ( ! empty( $users ) ) {
3741 $user = $users[0];
3742 $pluggable_data = $group_data;
3743
3744 $avatar = get_avatar_url( $user->ID );
3745 $pluggable_data['wp_user_id'] = $user->ID;
3746 $pluggable_data['avatar_url'] = ( $avatar ) ? $avatar : '';
3747 $pluggable_data['user_login'] = $user->user_login;
3748 $pluggable_data['display_name'] = $user->display_name;
3749 $pluggable_data['user_firstname'] = $user->user_firstname;
3750 $pluggable_data['user_lastname'] = $user->user_lastname;
3751 $pluggable_data['user_email'] = $user->user_email;
3752 $pluggable_data['user_role'] = $user->roles;
3753 $context['pluggable_data'] = $pluggable_data;
3754 $context['response_type'] = 'live';
3755 } else {
3756 $context['pluggable_data'] = [
3757 'wp_user_id' => 1,
3758 'user_login' => 'admin',
3759 'display_name' => 'Test User',
3760 'user_firstname' => 'Test',
3761 'user_lastname' => 'User',
3762 'user_email' => 'testuser@gmail.com',
3763 'user_role' => [ 'subscriber' ],
3764 'group_id' => 112,
3765 'group_name' => 'Test Group',
3766 'group_description' => 'Test Group Description',
3767 ];
3768 $context['response_type'] = 'sample';
3769 }
3770
3771 return $context;
3772 }
3773
3774 /**
3775 * Search complete courses.
3776 *
3777 * @param array $data data.
3778 * @return array
3779 */
3780 public function search_pluggables_complete_course( $data ) {
3781 global $wpdb;
3782 $context = [];
3783
3784 if ( isset( $data['filter']['sfwd_course_id']['value'] ) ) {
3785 $course_id = $data['filter']['sfwd_course_id']['value'];
3786 }
3787 if ( -1 === $course_id ) {
3788 $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 ) );
3789 } else {
3790 $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 ) );
3791 }
3792
3793 if ( ! empty( $courses ) ) {
3794 $course = $courses[0];
3795 $course_data['course_name'] = $course->post_title;
3796 $course_data['sfwd_course_id'] = $course->ID;
3797 $course_data['course_url'] = get_permalink( $course->ID );
3798 $course_data['course_featured_image_id'] = get_post_meta( $course->ID, '_thumbnail_id', true );
3799 $course_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course->ID );
3800 $context['response_type'] = 'live';
3801 } else {
3802 $course_data['course_name'] = 'Test Course';
3803 $course_data['sfwd_course_id'] = 112;
3804 $course_data['course_url'] = 'https://abc.com/test-course';
3805 $course_data['course_featured_image_id'] = 113;
3806 $course_data['course_featured_image_url'] = 'https://pqr.com/test-course-img';
3807 $context['response_type'] = 'sample';
3808 }
3809
3810 $users_data = $this->search_pluggables_add_user_role( [] );
3811 $user_data = $users_data['pluggable_data'];
3812
3813 $context['pluggable_data'] = array_merge( $course_data, $user_data );
3814 return $context;
3815 }
3816
3817 /**
3818 * Search lessons.
3819 *
3820 * @param array $data data.
3821 * @return array
3822 */
3823 public function search_pluggables_complete_lesson( $data ) {
3824 global $wpdb;
3825 $context = [];
3826
3827 if ( isset( $data['filter']['sfwd_lesson_id']['value'] ) ) {
3828 $lesson_id = $data['filter']['sfwd_lesson_id']['value'];
3829 $course_id = $data['filter']['sfwd_course_id']['value'];
3830 }
3831 $course = get_post( $course_id );
3832 $pluggable_data = LearnDash::get_course_context( $course );
3833
3834 if ( -1 === $lesson_id ) {
3835 $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 ) );
3836 } else {
3837 $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 ) );
3838 }
3839
3840 if ( ! empty( $lessons ) ) {
3841 $lesson = $lessons[0];
3842
3843 $pluggable_data['lesson_name'] = $lesson->post_title;
3844 $pluggable_data['sfwd_lesson_id'] = $lesson->ID;
3845 $pluggable_data['lesson_url'] = get_permalink( $lesson->ID );
3846 $pluggable_data['lesson_featured_image_id'] = get_post_meta( $lesson->ID, '_thumbnail_id', true );
3847 $pluggable_data['lesson_featured_image_url'] = get_the_post_thumbnail_url( $lesson->ID );
3848 $context['response_type'] = 'live';
3849 } else {
3850 $pluggable_data['lesson_name'] = 'Test Lesson';
3851 $pluggable_data['sfwd_lesson_id'] = 114;
3852 $pluggable_data['lesson_url'] = 'https://abc.com/test-lesson';
3853 $pluggable_data['lesson_featured_image_id'] = 116;
3854 $pluggable_data['lesson_featured_image_url'] = 'https://pqr.com/test-lesson-img';
3855 $context['response_type'] = 'sample';
3856 }
3857
3858 $context['pluggable_data'] = $pluggable_data;
3859 return $context;
3860 }
3861
3862 /**
3863 * Search topics.
3864 *
3865 * @param array $data data.
3866 * @return array
3867 */
3868 public function search_pluggables_complete_topic( $data ) {
3869 global $wpdb;
3870 $context = [];
3871
3872 if ( isset( $data['filter']['sfwd_topic_id']['value'] ) ) {
3873 $topic_id = $data['filter']['sfwd_topic_id']['value'];
3874 $course_id = $data['filter']['sfwd_course_id']['value'];
3875 }
3876 $course = get_post( $course_id );
3877 $pluggable_data = LearnDash::get_course_context( $course );
3878
3879 if ( -1 === $topic_id ) {
3880 $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 ) );
3881 } else {
3882 $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 ) );
3883 }
3884
3885 if ( ! empty( $topics ) ) {
3886 $topic = $topics[0];
3887
3888 $pluggable_data['topic_name'] = $topic->post_title;
3889 $pluggable_data['sfwd_topic_id'] = $topic->ID;
3890 $pluggable_data['topic_url'] = get_permalink( $topic->ID );
3891 $pluggable_data['topic_featured_image_id'] = get_post_meta( $topic->ID, '_thumbnail_id', true );
3892 $pluggable_data['topic_featured_image_url'] = get_the_post_thumbnail_url( $topic->ID );
3893 $context['response_type'] = 'live';
3894 } else {
3895 $pluggable_data['topic_name'] = 'Test Topic';
3896 $pluggable_data['sfwd_topic_id'] = 117;
3897 $pluggable_data['topic_url'] = 'https://abc.com/test-topic';
3898 $pluggable_data['topic_featured_image_id'] = 118;
3899 $pluggable_data['topic_featured_image_url'] = 'https://pqr.com/test-topic-img';
3900 $context['response_type'] = 'sample';
3901 }
3902
3903 $context['pluggable_data'] = $pluggable_data;
3904 return $context;
3905 }
3906
3907 /**
3908 * Search purchase courses.
3909 *
3910 * @param array $data data.
3911 * @return array
3912 */
3913 public function search_pluggables_purchase_course( $data ) {
3914 $context = [];
3915 $context['response_type'] = 'sample';
3916
3917 $purchase_data = [
3918 'course_product_id' => '1',
3919 'course_product_name' => 'Sample Course',
3920 'currency' => 'USD',
3921 'total_amount' => '100',
3922 'first_name' => 'John',
3923 'last_name' => 'Doe',
3924 'email' => 'john_doe@bsf.io',
3925 'phone' => '+923007626541',
3926 ];
3927
3928 $product_id = (int) ( isset( $data['filter']['course_product_id']['value'] ) ? $data['filter']['course_product_id']['value'] : '-1' );
3929 $order_id = 0;
3930
3931 if ( -1 !== $product_id ) {
3932 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
3933
3934 if ( count( $order_ids ) > 0 ) {
3935 $order_id = $order_ids[0];
3936 }
3937 } else {
3938 $orders = wc_get_orders( [] );
3939 if ( count( $orders ) > 0 ) {
3940 foreach ( $orders as $order ) {
3941 $items = $order->get_items();
3942
3943 if ( count( $items ) > 1 ) {
3944 continue;
3945 }
3946
3947 foreach ( $items as $item ) {
3948 if ( ! empty( get_post_meta( $item->get_product_id(), '_related_course', true ) ) ) {
3949 $order_id = $order->get_id();
3950 break;
3951 }
3952 }
3953 }
3954 }
3955 }
3956
3957 if ( 0 !== $order_id ) {
3958 $order = wc_get_order( $order_id );
3959
3960 if ( $order ) {
3961
3962 $purchase_data = LearnDash::get_purchase_course_context( $order );
3963
3964 $context['response_type'] = 'live';
3965 }
3966 }
3967
3968 $context['pluggable_data'] = $purchase_data;
3969
3970 return $context;
3971 }
3972
3973 /**
3974 * Fetch BB templates.
3975 *
3976 * @return array
3977 */
3978 public function get_beaver_builder_templates() {
3979 $allowed_types = [ 'subscribe-form', 'contact-form' ];
3980 $templates = [];
3981 $all_templates = get_posts(
3982 [
3983 'post_type' => 'fl-builder-template',
3984 'meta_key' => '_fl_builder_data',
3985 'posts_per_page' => -1,
3986 ]
3987 );
3988 $posts = get_posts(
3989 [
3990 'post_type' => 'any',
3991 'meta_key' => '_fl_builder_data',
3992 'posts_per_page' => -1,
3993 ]
3994 );
3995 $posts = array_merge( $all_templates, $posts );
3996
3997 if ( ! empty( $posts ) ) {
3998 foreach ( $posts as $post ) {
3999 $meta = get_post_meta( $post->ID, '_fl_builder_data', true );
4000 foreach ( (array) $meta as $node_id => $node ) {
4001 if ( isset( $node->type ) && 'module' === $node->type ) {
4002 $settings = $node->settings;
4003 if ( in_array( $settings->type, $allowed_types, true ) ) {
4004 $label = $post->post_title;
4005 if ( '' !== $settings->node_label ) {
4006 $label .= ' - ' . $settings->node_label;
4007 }
4008 $templates[] = [
4009 'label' => $label,
4010 'value' => $node_id,
4011 ];
4012 }
4013 }
4014 }
4015 }
4016 }
4017 return $templates;
4018 }
4019
4020 /**
4021 * Search beaver builder forms.
4022 *
4023 * @param array $data data.
4024 * @return array
4025 */
4026 public function search_beaver_builder_forms( $data ) {
4027 $templates = $this->get_beaver_builder_templates();
4028 return [
4029 'options' => $templates,
4030 'hasMore' => false,
4031 ];
4032 }
4033
4034 /**
4035 * Search fluentcrm fields.
4036 *
4037 * @param array $data data.
4038 * @return array
4039 */
4040 public function search_fluentcrm_custom_fields( $data ) {
4041 $context = [];
4042 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
4043 $context['fields'] = $custom_fields;
4044 return $context;
4045 }
4046
4047 /**
4048 * Fetch WP JOB Manager Last Data.
4049 *
4050 * @param array $data data.
4051 * @return array
4052 */
4053 public function search_wp_job_manger_last_data( $data ) {
4054 global $wpdb;
4055 $job_type = $data['filter']['job_type']['value'];
4056 $args = [
4057 'posts_per_page' => 1,
4058 'post_type' => 'job_listing',
4059 'orderby' => 'id',
4060 'order' => 'DESC',
4061 ];
4062
4063 if ( -1 !== $job_type ) {
4064 $args['tax_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
4065 [
4066 'taxonomy' => 'job_listing_type',
4067 'field' => 'term_id',
4068 'terms' => $job_type,
4069 ],
4070 ];
4071 }
4072 $posts = get_posts( $args );
4073 if ( empty( $posts ) ) {
4074 $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 );
4075 return $context;
4076 }
4077
4078 $post = $posts[0];
4079 $post_content = WordPress::get_post_context( $post->ID );
4080 $post_meta = WordPress::get_post_meta( $post->ID );
4081 $job_data = array_merge( $post_content, $post_meta, WordPress::get_user_context( $post->post_author ) );
4082 foreach ( $job_data as $key => $job ) {
4083 $newkey = str_replace( 'post', 'wpjob', $key );
4084 unset( $job_data[ $key ] );
4085 $job_data[ $newkey ] = $job;
4086 }
4087 $context['response_type'] = 'live';
4088 $context['pluggable_data'] = $job_data;
4089 return $context;
4090
4091 }
4092
4093 /**
4094 * Get Amelia Appointment Category.
4095 *
4096 * @param array $data data.
4097 *
4098 * @return array
4099 */
4100 public function search_amelia_category_list( $data ) {
4101
4102 global $wpdb;
4103
4104 $page = $data['page'];
4105 $limit = Utilities::get_search_page_limit();
4106 $offset = $limit * ( $page - 1 );
4107
4108 $categories = $wpdb->get_results(
4109 $wpdb->prepare(
4110 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_categories WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
4111 [ 'visible', $limit, $offset ]
4112 ),
4113 OBJECT
4114 );
4115
4116 $categories_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
4117
4118 $options = [];
4119 if ( ! empty( $categories ) ) {
4120 foreach ( $categories as $category ) {
4121 $options[] = [
4122 'label' => $category->name,
4123 'value' => $category->id,
4124 ];
4125 }
4126 }
4127
4128 return [
4129 'options' => $options,
4130 'hasMore' => $categories_count > $limit && $categories_count > $offset,
4131 ];
4132
4133 }
4134
4135 /**
4136 * Get Amelia Appointment Services.
4137 *
4138 * @param array $data data.
4139 *
4140 * @return array
4141 */
4142 public function search_amelia_service_list( $data ) {
4143
4144 global $wpdb;
4145
4146 $page = $data['page'];
4147 $limit = Utilities::get_search_page_limit();
4148 $offset = $limit * ( $page - 1 );
4149
4150 $services = $wpdb->get_results(
4151 $wpdb->prepare(
4152 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_services
4153 WHERE categoryId = %d AND status = %s
4154 ORDER BY name ASC LIMIT %d OFFSET %d",
4155 [ $data['dynamic'], 'visible', $limit, $offset ]
4156 ),
4157 OBJECT
4158 );
4159
4160 $services_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
4161
4162 $options = [];
4163 if ( ! empty( $services ) ) {
4164 foreach ( $services as $category ) {
4165 $options[] = [
4166 'label' => $category->name,
4167 'value' => $category->id,
4168 ];
4169 }
4170 }
4171
4172 return [
4173 'options' => $options,
4174 'hasMore' => $services_count > $limit && $services_count > $offset,
4175 ];
4176
4177 }
4178
4179 /**
4180 * Get Amelia Events.
4181 *
4182 * @param array $data data.
4183 *
4184 * @return array
4185 */
4186 public function search_amelia_events_list( $data ) {
4187
4188 global $wpdb;
4189
4190 $page = $data['page'];
4191 $limit = Utilities::get_search_page_limit();
4192 $offset = $limit * ( $page - 1 );
4193
4194 $events = $wpdb->get_results(
4195 $wpdb->prepare(
4196 "SELECT SQL_CALC_FOUND_ROWS id, name from {$wpdb->prefix}amelia_events WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
4197 [ 'approved', $limit, $offset ]
4198 ),
4199 OBJECT
4200 );
4201
4202 $list_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
4203
4204 $options = [];
4205 if ( ! empty( $events ) ) {
4206 foreach ( $events as $event ) {
4207 $options[] = [
4208 'label' => $event->name,
4209 'value' => $event->id,
4210 ];
4211 }
4212 }
4213
4214 return [
4215 'options' => $options,
4216 'hasMore' => $list_count > $limit && $list_count > $offset,
4217 ];
4218
4219 }
4220
4221 /**
4222 * Get last data for trigger.
4223 *
4224 * @param array $data data.
4225 * @return array
4226 */
4227 public function search_amelia_appointment_booked_triggers_last_data( $data ) {
4228 global $wpdb;
4229
4230 $context = [];
4231
4232 $appointment_category = $data['filter']['amelia_category_list']['value'];
4233 $appointment_service = $data['filter']['amelia_service_list']['value'];
4234
4235 if ( -1 === $appointment_service ) {
4236 // If service exists as per category selected.
4237 $service_exist = $wpdb->get_row(
4238 $wpdb->prepare(
4239 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
4240 [ $appointment_category ]
4241 ),
4242 ARRAY_A
4243 );
4244
4245 if ( empty( $service_exist ) ) {
4246 $result = [];
4247 } else {
4248 $result = $wpdb->get_row(
4249 '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 )',
4250 ARRAY_A
4251 );
4252 }
4253 } else {
4254 $result = $wpdb->get_row(
4255 $wpdb->prepare(
4256 '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',
4257 [ $appointment_service ]
4258 ),
4259 ARRAY_A
4260 );
4261 }
4262
4263 if ( ! empty( $result ) ) {
4264
4265 $payment_result = $wpdb->get_row(
4266 $wpdb->prepare(
4267 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
4268 [ $result['id'] ]
4269 ),
4270 ARRAY_A
4271 );
4272
4273 $customer_result = $wpdb->get_row(
4274 $wpdb->prepare(
4275 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
4276 [ $result['customerId'] ]
4277 ),
4278 ARRAY_A
4279 );
4280
4281 $service_result = $wpdb->get_row(
4282 $wpdb->prepare(
4283 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
4284 [ $result['serviceId'] ]
4285 ),
4286 ARRAY_A
4287 );
4288
4289 $category_result = $wpdb->get_row(
4290 $wpdb->prepare(
4291 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
4292 [ $service_result['categoryId'] ]
4293 ),
4294 ARRAY_A
4295 );
4296
4297 if ( $result['couponId'] ) {
4298 $coupon_result = $wpdb->get_row(
4299 $wpdb->prepare(
4300 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
4301 [ $result['couponId'] ]
4302 ),
4303 ARRAY_A
4304 );
4305 } else {
4306 $coupon_result = [];
4307 }
4308
4309 if ( ! empty( $result['customFields'] ) ) {
4310 $custom_fields = json_decode( $result['customFields'], true );
4311
4312 $fields_arr = [];
4313 foreach ( (array) $custom_fields as $fields ) {
4314 if ( is_array( $fields ) ) {
4315 $fields_arr[ $fields['label'] ] = $fields['value'];
4316 }
4317 }
4318 unset( $result['customFields'] );
4319 } else {
4320 $fields_arr = [];
4321 }
4322
4323 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
4324 $context['response_type'] = 'live';
4325 } else {
4326
4327 $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 );
4328 }
4329
4330 return $context;
4331 }
4332
4333 /**
4334 * Get last data for trigger.
4335 *
4336 * @param array $data data.
4337 * @return array
4338 */
4339 public function search_amelia_new_event_attendee_triggers_last_data( $data ) {
4340 global $wpdb;
4341
4342 $context = [];
4343
4344 $event_selected = $data['filter']['amelia_events_list']['value'];
4345
4346 if ( -1 === $event_selected ) {
4347 $result = $wpdb->get_row(
4348 'SELECT *
4349 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
4350 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
4351 ON customer.id = event_period.customerBookingId
4352 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods )',
4353 ARRAY_A
4354 );
4355 } else {
4356 $result = $wpdb->get_row(
4357 $wpdb->prepare(
4358 'SELECT *
4359 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
4360 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
4361 ON customer.id = event_period.customerBookingId
4362 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId = %d',
4363 [ $event_selected ]
4364 ),
4365 ARRAY_A
4366 );
4367 }
4368
4369 if ( ! empty( $result ) ) {
4370
4371 $event = $wpdb->get_row(
4372 $wpdb->prepare(
4373 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
4374 [ $result['eventPeriodId'] ]
4375 ),
4376 ARRAY_A
4377 );
4378
4379 $customer_result = $wpdb->get_row(
4380 $wpdb->prepare(
4381 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
4382 [ $result['customerId'] ]
4383 ),
4384 ARRAY_A
4385 );
4386
4387 if ( $result['couponId'] ) {
4388 $coupon_result = $wpdb->get_row(
4389 $wpdb->prepare(
4390 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
4391 [ $result['couponId'] ]
4392 ),
4393 ARRAY_A
4394 );
4395 } else {
4396 $coupon_result = [];
4397 }
4398
4399 if ( ! empty( $result['customFields'] ) ) {
4400 $custom_fields = json_decode( $result['customFields'], true );
4401
4402 $fields_arr = [];
4403 foreach ( (array) $custom_fields as $fields ) {
4404 if ( is_array( $fields ) ) {
4405 $fields_arr[ $fields['label'] ] = $fields['value'];
4406 }
4407 }
4408 unset( $result['customFields'] );
4409 } else {
4410 $fields_arr = [];
4411 }
4412
4413 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result );
4414 $context['response_type'] = 'live';
4415 } else {
4416
4417 $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 );
4418 }
4419
4420 return $context;
4421 }
4422
4423 /**
4424 * Get last data for trigger.
4425 *
4426 * @param array $data data.
4427 * @return array
4428 */
4429 public function search_amelia_appointment_rescheduled_triggers_last_data( $data ) {
4430 global $wpdb;
4431
4432 $context = [];
4433
4434 $appointment_category = $data['filter']['amelia_category_list']['value'];
4435 $appointment_service = $data['filter']['amelia_service_list']['value'];
4436
4437 if ( -1 === $appointment_service ) {
4438 // If service exists as per category selected.
4439 $service_exist = $wpdb->get_row(
4440 $wpdb->prepare(
4441 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
4442 [ $appointment_category ]
4443 ),
4444 ARRAY_A
4445 );
4446
4447 if ( empty( $service_exist ) ) {
4448 $result = [];
4449 } else {
4450 $result = $wpdb->get_row(
4451 '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 )',
4452 ARRAY_A
4453 );
4454 }
4455 } else {
4456 $result = $wpdb->get_row(
4457 $wpdb->prepare(
4458 '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',
4459 [ $appointment_service ]
4460 ),
4461 ARRAY_A
4462 );
4463 }
4464
4465 if ( ! empty( $result ) ) {
4466
4467 $payment_result = $wpdb->get_row(
4468 $wpdb->prepare(
4469 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
4470 [ $result['id'] ]
4471 ),
4472 ARRAY_A
4473 );
4474
4475 $customer_result = $wpdb->get_row(
4476 $wpdb->prepare(
4477 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
4478 [ $result['customerId'] ]
4479 ),
4480 ARRAY_A
4481 );
4482
4483 $service_result = $wpdb->get_row(
4484 $wpdb->prepare(
4485 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
4486 [ $result['serviceId'] ]
4487 ),
4488 ARRAY_A
4489 );
4490
4491 $category_result = $wpdb->get_row(
4492 $wpdb->prepare(
4493 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
4494 [ $service_result['categoryId'] ]
4495 ),
4496 ARRAY_A
4497 );
4498
4499 if ( $result['couponId'] ) {
4500 $coupon_result = $wpdb->get_row(
4501 $wpdb->prepare(
4502 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
4503 [ $result['couponId'] ]
4504 ),
4505 ARRAY_A
4506 );
4507 } else {
4508 $coupon_result = [];
4509 }
4510
4511 if ( ! empty( $result['customFields'] ) ) {
4512 $custom_fields = json_decode( $result['customFields'], true );
4513
4514 $fields_arr = [];
4515 foreach ( (array) $custom_fields as $fields ) {
4516 if ( is_array( $fields ) ) {
4517 $fields_arr[ $fields['label'] ] = $fields['value'];
4518 }
4519 }
4520 unset( $result['customFields'] );
4521 } else {
4522 $fields_arr = [];
4523 }
4524
4525 $appointment_data['isRescheduled'] = '1';
4526 $context['pluggable_data'] = array_merge( $result, $fields_arr, $appointment_data, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
4527 $context['response_type'] = 'live';
4528 } else {
4529
4530 $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 );
4531 }
4532
4533 return $context;
4534 }
4535
4536
4537 /**
4538 * Get last data for trigger.
4539 *
4540 * @param array $data data.
4541 * @return array
4542 */
4543 public function search_amelia_appointment_cancelled_triggers_last_data( $data ) {
4544 global $wpdb;
4545
4546 $context = [];
4547
4548 $appointment_category = $data['filter']['amelia_category_list']['value'];
4549 $appointment_service = $data['filter']['amelia_service_list']['value'];
4550
4551 if ( -1 === $appointment_service ) {
4552 // If service exists as per category selected.
4553 $service_exist = $wpdb->get_row(
4554 $wpdb->prepare(
4555 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
4556 [ $appointment_category ]
4557 ),
4558 ARRAY_A
4559 );
4560
4561 if ( empty( $service_exist ) ) {
4562 $result = [];
4563 } else {
4564 $result = $wpdb->get_row(
4565 $wpdb->prepare(
4566 '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',
4567 [ 'canceled' ]
4568 ),
4569 ARRAY_A
4570 );
4571 }
4572 } else {
4573 $result = $wpdb->get_row(
4574 $wpdb->prepare(
4575 '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',
4576 [ 'canceled', $appointment_service ]
4577 ),
4578 ARRAY_A
4579 );
4580 }
4581
4582 if ( ! empty( $result ) ) {
4583
4584 $payment_result = $wpdb->get_row(
4585 $wpdb->prepare(
4586 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
4587 [ $result['id'] ]
4588 ),
4589 ARRAY_A
4590 );
4591
4592 $customer_result = $wpdb->get_row(
4593 $wpdb->prepare(
4594 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
4595 [ $result['customerId'] ]
4596 ),
4597 ARRAY_A
4598 );
4599
4600 $service_result = $wpdb->get_row(
4601 $wpdb->prepare(
4602 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
4603 [ $result['serviceId'] ]
4604 ),
4605 ARRAY_A
4606 );
4607
4608 $category_result = $wpdb->get_row(
4609 $wpdb->prepare(
4610 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
4611 [ $service_result['categoryId'] ]
4612 ),
4613 ARRAY_A
4614 );
4615
4616 if ( $result['couponId'] ) {
4617 $coupon_result = $wpdb->get_row(
4618 $wpdb->prepare(
4619 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
4620 [ $result['couponId'] ]
4621 ),
4622 ARRAY_A
4623 );
4624 } else {
4625 $coupon_result = [];
4626 }
4627
4628 if ( ! empty( $result['customFields'] ) ) {
4629 $custom_fields = json_decode( $result['customFields'], true );
4630
4631 $fields_arr = [];
4632 foreach ( (array) $custom_fields as $fields ) {
4633 if ( is_array( $fields ) ) {
4634 $fields_arr[ $fields['label'] ] = $fields['value'];
4635 }
4636 }
4637 unset( $result['customFields'] );
4638 } else {
4639 $fields_arr = [];
4640 }
4641
4642 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
4643 $context['response_type'] = 'live';
4644 } else {
4645
4646 $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 );
4647 }
4648
4649 return $context;
4650 }
4651
4652 /**
4653 * Get MailPoet Forms.
4654 *
4655 * @param array $data data.
4656 *
4657 * @return array
4658 */
4659 public function search_mailpoet_forms( $data ) {
4660 if ( ! class_exists( '\MailPoet\API\API' ) ) {
4661 return;
4662 }
4663
4664 global $wpdb;
4665
4666 $page = $data['page'];
4667 $limit = Utilities::get_search_page_limit();
4668 $offset = $limit * ( $page - 1 );
4669
4670 $forms = $wpdb->get_results(
4671 $wpdb->prepare(
4672 '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',
4673 [ 'enabled', $limit, $offset ]
4674 )
4675 );
4676
4677 $form_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
4678
4679 $options = [];
4680
4681 if ( ! empty( $forms ) ) {
4682 if ( is_array( $forms ) ) {
4683 foreach ( $forms as $form ) {
4684 $options[] = [
4685 'label' => $form->name,
4686 'value' => $form->id,
4687 ];
4688 }
4689 }
4690 }
4691
4692 return [
4693 'options' => $options,
4694 'hasMore' => $form_count > $limit && $form_count > $offset,
4695 ];
4696
4697 }
4698
4699 /**
4700 * Get MailPoet List.
4701 *
4702 * @param array $data data.
4703 *
4704 * @return array
4705 */
4706 public function search_mailpoet_list( $data ) {
4707 if ( ! class_exists( '\MailPoet\API\API' ) ) {
4708 return;
4709 }
4710
4711 $mailpoet = \MailPoet\API\API::MP( 'v1' );
4712 $lists = $mailpoet->getLists();
4713
4714 $options = [];
4715
4716 if ( ! empty( $lists ) ) {
4717 if ( is_array( $lists ) ) {
4718 foreach ( $lists as $list ) {
4719 $options[] = [
4720 'label' => $list['name'],
4721 'value' => $list['id'],
4722 ];
4723 }
4724 }
4725 }
4726
4727 return [
4728 'options' => $options,
4729 'hasMore' => false,
4730 ];
4731 }
4732
4733 /**
4734 * Get MailPoet Subscriber Status.
4735 *
4736 * @param array $data data.
4737 *
4738 * @return array
4739 */
4740 public function search_mailpoet_subscriber_status( $data ) {
4741 if ( ! class_exists( '\MailPoet\API\API' ) ) {
4742 return;
4743 }
4744
4745 $subscriber_status = [
4746 'subscribed' => 'Subscribed',
4747 'unconfirmed' => 'Unconfirmed',
4748 'unsubscribed' => 'Unsubscribed',
4749 'inactive' => 'Inactive',
4750 'bounced' => 'Bounced',
4751 ];
4752
4753 $options = [];
4754 foreach ( $subscriber_status as $key => $status ) {
4755 $options[] = [
4756 'label' => $status,
4757 'value' => $key,
4758 ];
4759 }
4760
4761 return [
4762 'options' => $options,
4763 'hasMore' => false,
4764 ];
4765 }
4766
4767 /**
4768 * Get MailPoet Subscribers.
4769 *
4770 * @param array $data data.
4771 *
4772 * @return array
4773 */
4774 public function search_mailpoet_subscribers( $data ) {
4775 if ( ! class_exists( '\MailPoet\API\API' ) ) {
4776 return;
4777 }
4778
4779 global $wpdb;
4780
4781 $page = $data['page'];
4782 $limit = Utilities::get_search_page_limit();
4783 $offset = $limit * ( $page - 1 );
4784
4785 $subscribers = $wpdb->get_results(
4786 $wpdb->prepare(
4787 'SELECT SQL_CALC_FOUND_ROWS id,email FROM ' . $wpdb->prefix . 'mailpoet_subscribers ORDER BY id DESC LIMIT %d OFFSET %d',
4788 [ $limit, $offset ]
4789 )
4790 );
4791
4792 $subscribers_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
4793
4794 $options = [];
4795
4796 if ( ! empty( $subscribers ) ) {
4797 if ( is_array( $subscribers ) ) {
4798 foreach ( $subscribers as $subscriber ) {
4799 $options[] = [
4800 'label' => $subscriber->email,
4801 'value' => $subscriber->id,
4802 ];
4803 }
4804 }
4805 }
4806
4807 return [
4808 'options' => $options,
4809 'hasMore' => $subscribers_count > $limit && $subscribers_count > $offset,
4810 ];
4811 }
4812
4813 /**
4814 * Get ConvertPro Forms.
4815 *
4816 * @param array $data data.
4817 *
4818 * @return array
4819 */
4820 public function search_convertpro_form_list( $data ) {
4821 if ( ! class_exists( '\Cp_V2_Loader' ) ) {
4822 return;
4823 }
4824
4825 $cp_popups_inst = CP_V2_Popups::get_instance();
4826 $popups = $cp_popups_inst->get_all();
4827
4828 $form_count = count( $popups );
4829
4830 $page = $data['page'];
4831 $limit = Utilities::get_search_page_limit();
4832 $offset = $limit * ( $page - 1 );
4833
4834 $options = [];
4835
4836 if ( ! empty( $popups ) ) {
4837 if ( is_array( $popups ) ) {
4838 foreach ( $popups as $form ) {
4839 $options[] = [
4840 'label' => $form->post_title,
4841 'value' => $form->ID,
4842 ];
4843 }
4844 }
4845 }
4846
4847 return [
4848 'options' => $options,
4849 'hasMore' => $form_count > $limit && $form_count > $offset,
4850 ];
4851
4852 }
4853
4854 /**
4855 * Get ProjectHuddle Websites.
4856 *
4857 * @param array $data data.
4858 *
4859 * @return array
4860 */
4861 public function search_project_huddle_websites( $data ) {
4862
4863 $sites = new WP_Query(
4864 [
4865 'post_type' => 'ph-website',
4866 'posts_per_page' => - 1,
4867 'fields' => 'ids',
4868 ]
4869 );
4870
4871 $site_ids = (array) $sites->posts;
4872
4873 $options = [];
4874 if ( ! empty( $site_ids ) ) {
4875 if ( is_array( $site_ids ) ) {
4876 foreach ( $site_ids as $site_id ) {
4877 $options[] = [
4878 'label' => get_the_title( $site_id ),
4879 'value' => $site_id,
4880 ];
4881 }
4882 }
4883 }
4884 return [
4885 'options' => $options,
4886 'hasMore' => false,
4887 ];
4888
4889 }
4890
4891 /**
4892 * Get last data for trigger.
4893 *
4894 * @param array $data data.
4895 * @return mixed
4896 */
4897 public function search_project_huddle_comment_triggers_last_data( $data ) {
4898 global $wpdb;
4899
4900 $context = [];
4901
4902 if ( -1 !== $data['dynamic'] ) {
4903 $threads = get_posts(
4904 [
4905 'post_type' => 'phw_comment_loc',
4906 'posts_per_page' => 1,
4907 'meta_value' => $data['dynamic'],
4908 'meta_key' => 'project_id',
4909 'orderby' => 'asc',
4910 ]
4911 );
4912 } else {
4913 $threads = [];
4914 }
4915
4916 if ( ! empty( $threads ) ) {
4917 $comment_result = $wpdb->get_row(
4918 $wpdb->prepare(
4919 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
4920 FROM ' . $wpdb->prefix . 'comments
4921 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
4922 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
4923 LIMIT 0,1',
4924 $threads[0]->ID
4925 ),
4926 ARRAY_A
4927 );
4928
4929 if ( ! empty( $comment_result ) ) {
4930 $comment_id = get_comment( $comment_result['comment_ID'], ARRAY_A );
4931 $comments['comment_ID'] = isset( $comment_id['comment_ID'] ) ? $comment_id['comment_ID'] : '';
4932 $comments['comment_post_ID'] = isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : '';
4933
4934 $comments['comment_author'] = isset( $comment_id['comment_author'] ) ? $comment_id['comment_author'] : '';
4935
4936 $comments['comment_author_email'] = isset( $comment_id['comment_author_email'] ) ? $comment_id['comment_author_email'] : '';
4937
4938 $comments['comment_date'] = isset( $comment_id['comment_date'] ) ? $comment_id['comment_date'] : '';
4939
4940 $comments['comment_content'] = isset( $comment_id['comment_content'] ) ? $comment_id['comment_content'] : '';
4941
4942 $comments['comment_type'] = isset( $comment_id['comment_type'] ) ? $comment_id['comment_type'] : '';
4943
4944 $context['pluggable_data'] = $comments;
4945 $context['response_type'] = 'live';
4946 } else {
4947 $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 );
4948 }
4949 } else {
4950 $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 );
4951 }
4952
4953 return $context;
4954 }
4955
4956 /**
4957 * Get last data for trigger.
4958 *
4959 * @param array $data data.
4960 * @return mixed
4961 */
4962 public function search_project_huddle_resolved_comment_triggers_last_data( $data ) {
4963 global $wpdb;
4964
4965 $context = [];
4966
4967 $get_comments = $wpdb->get_row(
4968 'SELECT ' . $wpdb->prefix . 'comments.comment_ID, ' . $wpdb->prefix . 'comments.comment_content
4969 FROM ' . $wpdb->prefix . 'comments
4970 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_status") AND comment_content = "Resolved"
4971 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
4972 LIMIT 0,1'
4973 );
4974
4975 if ( ! empty( $get_comments ) ) {
4976 $comment_id = get_comment( $get_comments->comment_ID, ARRAY_A );
4977 $comment_result = $wpdb->get_row(
4978 $wpdb->prepare(
4979 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
4980 FROM ' . $wpdb->prefix . 'comments
4981 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
4982 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
4983 LIMIT 0,1',
4984 isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : ''
4985 ),
4986 ARRAY_A
4987 );
4988
4989 $actual_comment = get_comment( $comment_result['comment_ID'], ARRAY_A );
4990 $comments['comment_ID'] = isset( $actual_comment['comment_ID'] ) ? $actual_comment['comment_ID'] : '';
4991 $comments['comment_post_ID'] = isset( $actual_comment['comment_post_ID'] ) ? $actual_comment['comment_post_ID'] : '';
4992 $comments['comment_author'] = isset( $actual_comment['comment_author'] ) ? $actual_comment['comment_author'] : '';
4993 $comments['comment_author_email'] = isset( $actual_comment['comment_author_email'] ) ? $actual_comment['comment_author_email'] : '';
4994 $comments['comment_date'] = isset( $actual_comment['comment_date'] ) ? $actual_comment['comment_date'] : '';
4995 $comments['comment_content'] = isset( $actual_comment['comment_content'] ) ? $actual_comment['comment_content'] : '';
4996 $comments['comment_type'] = isset( $actual_comment['comment_type'] ) ? $actual_comment['comment_type'] : '';
4997 $comments['comment_status'] = $get_comments->comment_content;
4998 $context['pluggable_data'] = $comments;
4999 $context['response_type'] = 'live';
5000 } else {
5001 $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 );
5002 }
5003
5004 return $context;
5005 }
5006
5007 /**
5008 * Get MasterStudy LMS Courses.
5009 *
5010 * @param array $data data.
5011 *
5012 * @return array
5013 */
5014 public function search_ms_lms_courses( $data ) {
5015
5016 $page = $data['page'];
5017 $limit = Utilities::get_search_page_limit();
5018 $offset = $limit * ( $page - 1 );
5019
5020 $args = [
5021 'post_type' => 'stm-courses',
5022 'posts_per_page' => $limit,
5023 'offset' => $offset,
5024 'orderby' => 'title',
5025 'order' => 'ASC',
5026 'post_status' => 'publish',
5027 ];
5028
5029 $courses = get_posts( $args );
5030
5031 $course_count = count( $courses );
5032
5033 $options = [];
5034 if ( ! empty( $courses ) ) {
5035 if ( is_array( $courses ) ) {
5036 foreach ( $courses as $course ) {
5037 $options[] = [
5038 'label' => $course->post_title,
5039 'value' => $course->ID,
5040 ];
5041 }
5042 }
5043 }
5044 return [
5045 'options' => $options,
5046 'hasMore' => $course_count > $limit && $course_count > $offset,
5047 ];
5048
5049 }
5050
5051 /**
5052 * Get MasterStudy LMS Lessons.
5053 *
5054 * @param array $data data.
5055 *
5056 * @return array
5057 */
5058 public function search_ms_lms_lessons( $data ) {
5059
5060 $page = $data['page'];
5061 $limit = Utilities::get_search_page_limit();
5062 $offset = $limit * ( $page - 1 );
5063
5064 $args = [
5065 'post_type' => 'stm-lessons',
5066 'posts_per_page' => $limit,
5067 'offset' => $offset,
5068 'orderby' => 'title',
5069 'order' => 'ASC',
5070 'post_status' => 'publish',
5071 ];
5072
5073 $lessons = get_posts( $args );
5074
5075 $lessons_count = count( $lessons );
5076
5077 $options = [];
5078 if ( ! empty( $lessons ) ) {
5079 if ( is_array( $lessons ) ) {
5080 foreach ( $lessons as $lesson ) {
5081 $options[] = [
5082 'label' => $lesson->post_title,
5083 'value' => $lesson->ID,
5084 ];
5085 }
5086 }
5087 }
5088 return [
5089 'options' => $options,
5090 'hasMore' => $lessons_count > $limit && $lessons_count > $offset,
5091 ];
5092
5093 }
5094
5095 /**
5096 * Search MasterStudy LMS data.
5097 *
5098 * @param array $data data.
5099 * @return array|void
5100 */
5101 public function search_ms_lms_last_data( $data ) {
5102 global $wpdb;
5103 $post_type = $data['post_type'];
5104 $trigger = $data['search_term'];
5105 $context = [];
5106
5107 if ( 'stm_lms_course_completed' === $trigger ) {
5108 $post_id = $data['filter']['course']['value'];
5109 if ( -1 === $post_id ) {
5110 $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 ) );
5111 } else {
5112 $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 ) );
5113 }
5114 } elseif ( 'stm_lesson_passed' === $trigger ) {
5115 $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 ) );
5116 } elseif ( 'stm_quiz_passed' === $trigger ) {
5117 $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 ) );
5118 } elseif ( 'stm_quiz_failed' === $trigger ) {
5119 $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 ) );
5120 } elseif ( 'stm_lms_user_enroll_course' === $trigger ) {
5121 $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 ) );
5122 }
5123
5124 if ( ! empty( $result ) ) {
5125
5126 switch ( $trigger ) {
5127 case 'stm_lms_course_completed':
5128 $result_course_id = $result[0]->course_id;
5129 $result_user_id = $result[0]->user_id;
5130 $course = get_the_title( $result_course_id );
5131 $course_link = get_the_permalink( $result_course_id );
5132 $featured_image = get_the_post_thumbnail_url( $result_course_id );
5133
5134 $data = [
5135 'course_id' => $result_course_id,
5136 'course_title' => $course,
5137 'course_link' => $course_link,
5138 'course_featured_image' => $featured_image,
5139 'course_progress' => $result[0]->progress_percent,
5140 ];
5141 $context_data = array_merge(
5142 WordPress::get_user_context( $result_user_id ),
5143 $data
5144 );
5145 break;
5146 case 'stm_lesson_passed':
5147 $result_lesson_id = $result[0]->lesson_id;
5148 $result_user_id = $result[0]->user_id;
5149 $lesson = get_the_title( $result_lesson_id );
5150 $lesson_link = get_the_permalink( $result_lesson_id );
5151
5152 $data = [
5153 'lesson_id' => $result_lesson_id,
5154 'lesson_title' => $lesson,
5155 'lesson_link' => $lesson_link,
5156 ];
5157 $context_data = array_merge(
5158 WordPress::get_user_context( $result_user_id ),
5159 $data
5160 );
5161 break;
5162 case 'stm_quiz_passed':
5163 $result_quiz_id = $result[0]->quiz_id;
5164 $result_user_id = $result[0]->user_id;
5165 $quiz_title = get_the_title( $result_quiz_id );
5166 $quiz_link = get_the_permalink( $result_quiz_id );
5167
5168 $data = [
5169 'quiz_id' => $result_quiz_id,
5170 'quiz_title' => $quiz_title,
5171 'quiz_link' => $quiz_link,
5172 'quiz_score' => $result[0]->progress,
5173 'result' => 'passed',
5174 ];
5175 $context_data = array_merge(
5176 WordPress::get_user_context( $result_user_id ),
5177 $data
5178 );
5179 break;
5180 case 'stm_quiz_failed':
5181 $result_quiz_id = $result[0]->quiz_id;
5182 $result_user_id = $result[0]->user_id;
5183 $quiz_title = get_the_title( $result_quiz_id );
5184 $quiz_link = get_the_permalink( $result_quiz_id );
5185
5186 $data = [
5187 'quiz_id' => $result_quiz_id,
5188 'quiz_title' => $quiz_title,
5189 'quiz_link' => $quiz_link,
5190 'quiz_score' => $result[0]->progress,
5191 'result' => 'failed',
5192 ];
5193 $context_data = array_merge(
5194 WordPress::get_user_context( $result_user_id ),
5195 $data
5196 );
5197 break;
5198 case 'stm_lms_user_enroll_course':
5199 $result_course_id = $result[0]->course_id;
5200 $result_user_id = $result[0]->user_id;
5201
5202 $course = get_the_title( $result_course_id );
5203 $course_link = get_the_permalink( $result_course_id );
5204 $featured_image = get_the_post_thumbnail_url( $result_course_id );
5205
5206 $data = [
5207 'course_id' => $result_course_id,
5208 'course_title' => $course,
5209 'course_link' => $course_link,
5210 'course_featured_image' => $featured_image,
5211 ];
5212 $context_data = array_merge(
5213 WordPress::get_user_context( $result_user_id ),
5214 $data
5215 );
5216 break;
5217 default:
5218 return;
5219 }
5220 $context['pluggable_data'] = $context_data;
5221 $context['response_type'] = 'live';
5222 }
5223
5224 return $context;
5225
5226 }
5227
5228
5229 /**
5230 * Get last data for trigger.
5231 *
5232 * @param array $data data.
5233 * @return array
5234 */
5235 public function search_fluent_support_triggers_last_data( $data ) {
5236 $context = [];
5237 $context['response_type'] = 'sample';
5238
5239 $ticket_data = [
5240 'id' => '1',
5241 'customer_id' => '2',
5242 'agent_id' => '3',
5243 'product_id' => '5',
5244 'product_source' => 'local',
5245 'privacy' => 'private',
5246 'priority' => 'normal',
5247 'client_priority' => 'medium',
5248 'status' => 'active',
5249 'title' => 'Sample Ticket Title',
5250 'slug' => 'sample-ticket-title',
5251 'hash' => 'f8a8cfb946',
5252 'content_hash' => 'd65500d62621be8b493c22b1d888052c',
5253 'content' => '<p>Sample content.</p>',
5254 'last_customer_response' => '2023-04-27 07:30:46',
5255 'waiting_since' => '2023-04-27 07:30:46',
5256 'response_count' => '2',
5257 'total_close_time' => '7042',
5258 'resolved at' => '2023-04-27 09:28:08',
5259 'closed_by' => '1',
5260 'created_at' => '2023-04-27 07:30:46',
5261 'updated_at' => '2023-04-27 10:28:08',
5262 'mailbox_id' => '1',
5263 'mailbox_name' => 'SureTriggers',
5264 'mailbox_slug' => 'suretriggers',
5265 'mailbox_box_type' => 'web',
5266 'mailbox email' => 'john_doe@sample.com',
5267 'mailbox_settings_admin_email_address' => 'john_doe@sample.com',
5268 'mailbox_created_by' => '1',
5269 'mailbox_is_default' => 'yes',
5270 'mailbox_created_at' => '2023-04-26 06:29:01',
5271 'mailbox_updated_at' => '2023-04-26 06:29:01',
5272 ];
5273
5274 $customer_data = [
5275 'id' => '1',
5276 'first_name' => 'John',
5277 'last_name' => 'Doe',
5278 'email' => 'john_doe@sample.com',
5279 'person_type' => 'agent',
5280 'status' => 'active',
5281 'hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
5282 'user_id' => '1',
5283 'created_at' => '2023-04-27 07:30:46',
5284 'updated_at' => '2023-04-27 10:28:08',
5285 'full_name' => 'John Doe',
5286 'photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
5287 ];
5288
5289 $reply_data = [
5290 'ticket_id' => '1',
5291 'conversation_type' => 'response',
5292 'content' => '<p>Sample content.</p>',
5293 'source' => 'web',
5294 'content_hash' => '2cc0e35d8fb92a0675d67999b073b3a4',
5295 'created_at' => '2023-04-27 07:30:46',
5296 'updated_at' => '2023-04-27 10:28:08',
5297 'id' => '1',
5298 'person_id' => '2',
5299 'person_first_name' => 'John',
5300 'person_last_name' => 'Doe',
5301 'person_email' => 'john_doe@sample.com',
5302 'person_person_type' => 'agent',
5303 'person_status' => 'active',
5304 'person_hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
5305 'person_user_id' => '1',
5306 'person_created_at' => '2023-04-27 07:30:46',
5307 'person_updated_at' => '2023-04-27 10:28:08',
5308 'person_full_name' => 'John Doe',
5309 'person_photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
5310 ];
5311
5312 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
5313
5314 if ( in_array( $term, [ 'response_added_by_agent', 'response_added_by_customer' ], true ) ) {
5315 $context['pluggable_data'] = array_merge(
5316 [
5317 'reply' => $reply_data,
5318 'ticket' => $ticket_data,
5319 'customer' => $customer_data,
5320 ]
5321 );
5322 } else {
5323 $context['pluggable_data'] = array_merge(
5324 [
5325 'ticket' => $ticket_data,
5326 'customer' => $customer_data,
5327 ]
5328 );
5329 }
5330
5331 return $context;
5332 }
5333
5334 /**
5335 * Prepare Ultimate Member user_roles.
5336 *
5337 * @param array $data data.
5338 *
5339 * @return array
5340 */
5341 public function search_um_user_roles( $data ) {
5342 if ( function_exists( 'get_editable_roles' ) ) {
5343 $roles = get_editable_roles();
5344 } else {
5345 $roles = wp_roles()->roles;
5346 $roles = apply_filters( 'editable_roles', $roles );
5347 }
5348
5349 $options = [];
5350 foreach ( $roles as $role => $details ) {
5351
5352 $options[] = [
5353 'label' => $details['name'],
5354 'value' => $role,
5355 ];
5356
5357 }
5358
5359 return [
5360 'options' => $options,
5361 'hasMore' => false,
5362 ];
5363 }
5364
5365 /**
5366 * Prepare Ultimate Member forms_list.
5367 *
5368 * @param array $data data.
5369 *
5370 * @return array
5371 */
5372 public function search_um_forms_list( $data ) {
5373
5374 $page = $data['page'];
5375 $limit = Utilities::get_search_page_limit();
5376 $offset = $limit * ( $page - 1 );
5377
5378 $args = [
5379 'posts_per_page' => $limit,
5380 'offset' => $offset,
5381 'orderby' => 'title',
5382 'order' => 'ASC',
5383 'post_type' => 'um_form',
5384 'post_status' => 'publish',
5385 'fields' => 'ids',
5386 ];
5387
5388 $forms_list = get_posts( $args );
5389
5390 $forms_list_count = count( $forms_list );
5391
5392 $options = [];
5393 if ( ! empty( $forms_list ) ) {
5394 foreach ( $forms_list as $form ) {
5395 $options[] = [
5396 'label' => get_the_title( $form ),
5397 'value' => $form,
5398 ];
5399 }
5400 }
5401
5402 return [
5403 'options' => $options,
5404 'hasMore' => $forms_list_count > $limit && $forms_list_count > $offset,
5405 ];
5406 }
5407
5408 /**
5409 * Get last data for Ultimate Member Login trigger.
5410 *
5411 * @param array $data data.
5412 * @return mixed
5413 */
5414 public function search_ultimate_member_user_logsin( $data ) {
5415 $context = [];
5416 $args = [
5417 'orderby' => 'meta_value',
5418 'meta_key' => '_um_last_login',
5419 'order' => 'DESC',
5420 'number' => 1,
5421 ];
5422 $users = get_users( $args );
5423
5424 if ( ! empty( $users ) ) {
5425 $user = $users[0];
5426 $pluggable_data = WordPress::get_user_context( $user->ID );
5427 $context['pluggable_data'] = $pluggable_data;
5428 $context['response_type'] = 'live';
5429 } else {
5430 $role = 'subscriber';
5431 $context['pluggable_data'] = [
5432 'wp_user_id' => 1,
5433 'user_login' => 'test',
5434 'display_name' => 'Test User',
5435 'user_firstname' => 'Test',
5436 'user_lastname' => 'User',
5437 'user_email' => 'testuser@gmail.com',
5438 'user_role' => [ $role ],
5439 ];
5440 $context['response_type'] = 'sample';
5441 }
5442 return $context;
5443 }
5444
5445 /**
5446 * Get last data for Ultimate Member Register trigger.
5447 *
5448 * @param array $data data.
5449 * @return mixed
5450 */
5451 public function search_ultimate_member_user_registers( $data ) {
5452 $context = [];
5453 $args = [
5454 'orderby' => 'meta_value',
5455 'meta_key' => 'um_user_profile_url_slug_user_login',
5456 'order' => 'DESC',
5457 'number' => 1,
5458 ];
5459 $users = get_users( $args );
5460
5461 if ( ! empty( $users ) ) {
5462 $user = $users[0];
5463 $pluggable_data = WordPress::get_user_context( $user->ID );
5464 $context['pluggable_data'] = $pluggable_data;
5465 $context['response_type'] = 'live';
5466 } else {
5467 $role = 'subscriber';
5468 $context['pluggable_data'] = [
5469 'wp_user_id' => 1,
5470 'user_login' => 'test',
5471 'display_name' => 'Test User',
5472 'user_firstname' => 'Test',
5473 'user_lastname' => 'User',
5474 'user_email' => 'testuser@gmail.com',
5475 'user_role' => [ $role ],
5476 ];
5477 $context['response_type'] = 'sample';
5478 }
5479 return $context;
5480 }
5481
5482 /**
5483 * Get last data for Ultimate Member Register trigger.
5484 *
5485 * @param array $data data.
5486 * @return mixed
5487 */
5488 public function search_ultimate_member_user_inactive( $data ) {
5489 $context = [];
5490 $args = [
5491 'orderby' => 'user_id',
5492 'meta_key' => 'account_status',
5493 'meta_value' => 'inactive',
5494 'order' => 'ASC',
5495 'number' => 1,
5496 ];
5497 $users = get_users( $args );
5498
5499 if ( ! empty( $users ) ) {
5500 $user = $users[0];
5501 $pluggable_data = [];
5502 $pluggable_data[] = WordPress::get_user_context( $user->ID );
5503 $pluggable_data['user_account_status'] = 'inactive';
5504 $context['pluggable_data'] = $pluggable_data;
5505 $context['response_type'] = 'live';
5506 } else {
5507 $role = 'subscriber';
5508 $context['pluggable_data'] = [
5509 'wp_user_id' => 1,
5510 'user_login' => 'test',
5511 'display_name' => 'Test User',
5512 'user_firstname' => 'Test',
5513 'user_lastname' => 'User',
5514 'user_email' => 'testuser@gmail.com',
5515 'user_role' => [ $role ],
5516 'user_account_status' => 'inactive',
5517 ];
5518 $context['response_type'] = 'sample';
5519 }
5520 return $context;
5521 }
5522
5523 /**
5524 * Get last data for Ultimate Member Change Role trigger.
5525 *
5526 * @param array $data data.
5527 * @return mixed
5528 */
5529 public function search_ultimate_member_user_role_change( $data ) {
5530 $context = [];
5531
5532 $role = $data['filter']['role']['value'];
5533
5534 $args = [
5535 'number' => 1,
5536 'role' => $role,
5537 ];
5538 $users = get_users( $args );
5539 shuffle( $users );
5540 if ( ! empty( $users ) ) {
5541 $user = $users[0];
5542 $pluggable_data = WordPress::get_user_context( $user->ID );
5543 $context['pluggable_data'] = $pluggable_data;
5544 $context['response_type'] = 'live';
5545 } else {
5546 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
5547 $context['pluggable_data'] = [
5548 'wp_user_id' => 1,
5549 'user_login' => 'test',
5550 'display_name' => 'Test User',
5551 'user_firstname' => 'Test',
5552 'user_lastname' => 'User',
5553 'user_email' => 'testuser@gmail.com',
5554 'user_role' => [ $role ],
5555 'user_account_status' => 'inactive',
5556 ];
5557 $context['response_type'] = 'sample';
5558 }
5559 return $context;
5560 }
5561
5562 /**
5563 * Get JetEngine WP Posttypes.
5564 *
5565 * @param array $data data.
5566 *
5567 * @return array
5568 */
5569 public function search_je_posttype_list( $data ) {
5570
5571 $post_types = get_post_types( [ 'public' => true ], 'object' );
5572 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
5573 if ( isset( $post_types['attachment'] ) ) {
5574 unset( $post_types['attachment'] );
5575 }
5576
5577 $options = [];
5578 foreach ( $post_types as $post_type => $details ) {
5579 $options[] = [
5580 'label' => $details->label,
5581 'value' => $post_type,
5582 ];
5583 }
5584
5585 return [
5586 'options' => $options,
5587 'hasMore' => false,
5588 ];
5589 }
5590
5591 /**
5592 * Get JetEngine WP fields.
5593 *
5594 * @param array $data data.
5595 *
5596 * @return array
5597 */
5598 public function search_je_field_list( $data ) {
5599
5600 $post_type = $data['dynamic'];
5601
5602 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
5603
5604 $post_fields = array_filter(
5605 $metaboxes,
5606 function( $metabox ) {
5607 /**
5608 *
5609 * Ignore line
5610 *
5611 * @phpstan-ignore-next-line
5612 */
5613 return 'post' === $metabox['args']['object_type'];
5614 }
5615 );
5616
5617 $post_fields_count = count( $post_fields );
5618
5619 $options = [];
5620 if ( ! empty( $post_fields ) ) {
5621 if ( is_array( $post_fields ) ) {
5622 foreach ( $post_fields as $post_field ) {
5623 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
5624 foreach ( $post_field['meta_fields'] as $meta_field ) {
5625 $options[] = [
5626 'label' => $meta_field['title'],
5627 'value' => $meta_field['name'],
5628 ];
5629 }
5630 }
5631 }
5632 }
5633 }
5634 return [
5635 'options' => $options,
5636 'hasMore' => false,
5637 ];
5638 }
5639
5640 /**
5641 * Search Last Updated Field Data.
5642 *
5643 * @param array $data data.
5644 * @return array
5645 */
5646 public function search_jet_engine_field_data( $data ) {
5647 global $wpdb;
5648
5649 $context = [];
5650
5651 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
5652
5653 $post_type = $data['filter']['wp_post_type']['value'];
5654
5655 if ( -1 === $field ) {
5656 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
5657
5658 $post_fields = array_filter(
5659 $metaboxes,
5660 function( $metabox ) {
5661 /**
5662 *
5663 * Ignore line
5664 *
5665 * @phpstan-ignore-next-line
5666 */
5667 return 'post' === $metabox['args']['object_type'];
5668 }
5669 );
5670
5671 $options = [];
5672 if ( ! empty( $post_fields ) ) {
5673 if ( is_array( $post_fields ) ) {
5674 foreach ( $post_fields as $post_field ) {
5675 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
5676 foreach ( $post_field['meta_fields'] as $meta_field ) {
5677 $options[] = $meta_field['name'];
5678 }
5679 }
5680 }
5681 }
5682 }
5683 $random_key = array_rand( $options );
5684 $random_value = $options[ $random_key ];
5685 $string = '%' . $random_value . '%';
5686 } else {
5687 $string = '%' . $data['filter']['field_id']['value'] . '%';
5688 }
5689
5690 $result = $wpdb->get_results(
5691 $wpdb->prepare(
5692 'SELECT post_id FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key LIKE %s',
5693 [ $string ]
5694 ),
5695 ARRAY_A
5696 );
5697
5698 $ids = [];
5699
5700 if ( ! empty( $result ) ) {
5701 foreach ( $result as $val ) {
5702 $ids[] = $val['post_id'];
5703 }
5704 }
5705
5706 $lastupdated_args = [
5707 'post_type' => $post_type,
5708 'orderby' => 'modified',
5709 'post__in' => $ids,
5710 'posts_per_page' => 1,
5711 ];
5712 $lastupdated_loop = get_posts( $lastupdated_args );
5713
5714 $response = [];
5715 if ( ! empty( $result ) ) {
5716 $context['post'] = $lastupdated_loop[0];
5717
5718 $meta_value = get_post_meta( $lastupdated_loop[0]->ID, sprintf( '%s', $data['filter']['field_id']['value'] ), true );
5719 $meta_key = sprintf( '%s', $data['filter']['field_id']['value'] );
5720
5721 $context[ $meta_key ] = $meta_value;
5722
5723 $response['pluggable_data'] = $context;
5724 $response['response_type'] = 'live';
5725 } else {
5726 $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 );
5727 }
5728
5729 return $response;
5730
5731 }
5732
5733 /**
5734 * Get Formidable Forms.
5735 *
5736 * @param array $data data.
5737 *
5738 * @return array|void
5739 */
5740 public function search_formidable_form_list( $data ) {
5741 if ( ! class_exists( 'FrmForm' ) ) {
5742 return;
5743 }
5744
5745 $query = [
5746 [
5747 'or' => 1,
5748 'parent_form_id' => null,
5749 'parent_form_id <' => 1,
5750 ],
5751 ];
5752 $query['is_template'] = 0;
5753 $query['status !'] = 'trash';
5754 $forms_list = FrmForm::getAll( $query, '', ' 0, 10' );
5755
5756 $options = [];
5757
5758 if ( ! empty( $forms_list ) ) {
5759 if ( is_array( $forms_list ) ) {
5760 foreach ( $forms_list as $form ) {
5761 $options[] = [
5762 'label' => $form->name,
5763 'value' => $form->id,
5764 ];
5765 }
5766 }
5767 }
5768
5769 return [
5770 'options' => $options,
5771 'hasMore' => false,
5772 ];
5773 }
5774
5775 /**
5776 * Get JetFormBuilder Form List.
5777 *
5778 * @param array $data data.
5779 *
5780 * @return array|void
5781 */
5782 public function search_jetform_list( $data ) {
5783 if ( ! class_exists( '\Jet_Form_Builder\Classes\Tools' ) ) {
5784 return;
5785 }
5786
5787 $forms = \Jet_Form_Builder\Classes\Tools::get_forms_list_for_js();
5788
5789 $options = [];
5790 foreach ( $forms as $form ) {
5791
5792 if ( ! empty( $form['value'] ) ) {
5793 $options[] = [
5794 'label' => esc_html( $form['label'] ),
5795 'value' => esc_attr( $form['value'] ),
5796 ];
5797 }
5798 }
5799
5800 return [
5801 'options' => $options,
5802 'hasMore' => false,
5803 ];
5804 }
5805
5806 /**
5807 * Get Forminator Form List.
5808 *
5809 * @param array $data data.
5810 *
5811 * @return array|void
5812 */
5813 public function search_forminator_form_list( $data ) {
5814 if ( ! class_exists( 'Forminator_API' ) ) {
5815 return;
5816 }
5817
5818 $forms = Forminator_API::get_forms( null, 1, 10 );
5819
5820 $options = [];
5821 foreach ( $forms as $form ) {
5822 $options[] = [
5823 'label' => isset( $form->settings ) && isset( $form->settings['form_name'] ) ? $form->settings['form_name'] : $form->name,
5824 'value' => $form->id,
5825 ];
5826 }
5827
5828 return [
5829 'options' => $options,
5830 'hasMore' => false,
5831 ];
5832 }
5833
5834
5835 /**
5836 * Get BbPress topics list.
5837 *
5838 * @param array $data data.
5839 *
5840 * @return array|void
5841 */
5842 public function search_bbp_topic_list( $data ) {
5843 $page = $data['page'];
5844 $forum_id = $data['dynamic'];
5845 $limit = Utilities::get_search_page_limit();
5846 $offset = $limit * ( $page - 1 );
5847 $args = [
5848 'post_type' => 'topic',
5849 'offset' => $offset,
5850 'meta_query' => [
5851 [
5852 'key' => '_bbp_forum_id',
5853 'value' => $forum_id,
5854 'compare' => '==',
5855 ],
5856 ],
5857 ];
5858
5859 $topics = get_posts( $args );
5860 $topics_count = count( $topics );
5861
5862 $options = [];
5863 if ( ! empty( $topics ) ) {
5864 if ( is_array( $topics ) ) {
5865 foreach ( $topics as $topic ) {
5866 $options[] = [
5867 'label' => $topic->post_title,
5868 'value' => $topic->ID,
5869 ];
5870 }
5871 }
5872 }
5873 return [
5874 'options' => $options,
5875 'hasMore' => $topics_count > $limit && $topics_count > $offset,
5876 ];
5877 }
5878
5879
5880 /**
5881 * Search Last Updated Field Data.
5882 *
5883 * @param array $data data.
5884 * @return array
5885 */
5886 public function search_bbp_last_data( $data ) {
5887 global $wpdb;
5888
5889 $post_type = $data['post_type'];
5890 $trigger = $data['search_term'];
5891 $context = [];
5892
5893 if ( 'topic' === $post_type ) {
5894 $post_id = $data['filter']['forum']['value'];
5895 if ( -1 === $post_id ) {
5896 $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" );
5897
5898 } else {
5899 $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 ) );
5900
5901 }
5902 } elseif ( 'reply' === $post_type ) {
5903 $post_id = $data['filter']['topic']['value'];
5904
5905 if ( -1 === $post_id ) {
5906 $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" );
5907
5908 } else {
5909 $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 ) );
5910 }
5911 }
5912
5913
5914 $response = [];
5915 if ( ! empty( $result ) ) {
5916 if ( 'bbpress_topic_created' === $trigger ) {
5917 $topic_id = $result[0]->post_id;
5918 $forum_id = $result[0]->meta_value;
5919 $topic = get_the_title( $topic_id );
5920 $topic_link = get_the_permalink( $topic_id );
5921 $topic_description = get_the_content( $topic_id );
5922 $topic_status = get_post_status( $topic_id );
5923
5924 $forum = get_the_title( $forum_id );
5925 $forum_link = get_the_permalink( $forum_id );
5926 $forum_description = get_the_content( $forum_id );
5927 $forum_status = get_post_status( $forum_id );
5928
5929 $forum = [
5930 'forum' => $forum_id,
5931 'forum_title' => $forum,
5932 'forum_link' => $forum_link,
5933 'forum_description' => $forum_description,
5934 'forum_status' => $forum_status,
5935 ];
5936
5937 $topic = [
5938 'topic_title' => $topic,
5939 'topic_link' => $topic_link,
5940 'topic_description' => $topic_description,
5941 'topic_status' => $topic_status,
5942 ];
5943
5944 $user_id = $result[0]->post_author;
5945 $context = array_merge(
5946 WordPress::get_user_context( $user_id ),
5947 $forum,
5948 $topic
5949 );
5950
5951 $response['pluggable_data'] = $context;
5952 $response['response_type'] = 'live';
5953 } else {
5954 $reply_id = $result[0]->post_id;
5955 $topic_id = $result[0]->meta_value;
5956 $forum_id = get_post_meta( $topic_id, '_bbp_forum_id', true );
5957
5958 $reply = get_the_title( $reply_id );
5959 $reply_link = get_the_permalink( $reply_id );
5960 $reply_description = get_the_content( $reply_id );
5961 $reply_status = get_post_status( $reply_id );
5962
5963
5964 $topic = get_the_title( $topic_id );
5965 $topic_link = get_the_permalink( $topic_id );
5966 $topic_description = get_the_content( $topic_id );
5967 $topic_status = get_post_status( $topic_id );
5968
5969 $forum = get_the_title( intval( $forum_id ) );
5970 $forum_link = get_the_permalink( intval( $forum_id ) );
5971 $forum_description = get_the_content( null, false, intval( $forum_id ) );
5972 $forum_status = get_post_status( intval( $forum_id ) );
5973
5974 $forum = [
5975 'forum' => $forum_id,
5976 'forum_title' => $forum,
5977 'forum_link' => $forum_link,
5978 'forum_description' => $forum_description,
5979 'forum_status' => $forum_status,
5980 ];
5981
5982 $topic = [
5983 'topic_title' => $topic,
5984 'topic_link' => $topic_link,
5985 'topic_description' => $topic_description,
5986 'topic_status' => $topic_status,
5987 ];
5988
5989 $reply = [
5990 'reply_title' => $reply,
5991 'reply_link' => $reply_link,
5992 'reply_description' => $reply_description,
5993 'reply_status' => $reply_status,
5994 ];
5995 $user_id = $result[0]->post_author;
5996 $context = array_merge(
5997 WordPress::get_user_context( $user_id ),
5998 $forum,
5999 $topic,
6000 $reply
6001 );
6002
6003 $response['pluggable_data'] = $context;
6004 $response['response_type'] = 'live';
6005 }
6006 }
6007
6008 return $response;
6009 }
6010
6011 /**
6012 * Search Last Updated Field Data.
6013 *
6014 * @param array $data data.
6015 * @return array|void
6016 */
6017 public function search_happyform_list( $data ) {
6018 if ( ! function_exists( 'happyforms_get_form_controller' ) ) {
6019 return;
6020 }
6021
6022 $form_controller = happyforms_get_form_controller();
6023
6024 $forms = $form_controller->do_get();
6025 $options = [];
6026 if ( ! empty( $forms ) ) {
6027 foreach ( $forms as $form ) {
6028 $options[] = [
6029 'label' => $form['post_title'],
6030 'value' => $form['ID'],
6031 ];
6032 }
6033 }
6034
6035 return [
6036 'options' => $options,
6037 'hasMore' => false,
6038 ];
6039 }
6040
6041 /**
6042 * Get Memberpress Course List.
6043 *
6044 * @param array $data data.
6045 *
6046 * @return array|void
6047 */
6048 public function search_mpc_lessons_list( $data ) {
6049 if ( ! class_exists( '\memberpress\courses\models\Lesson' ) ) {
6050 return;
6051 }
6052 global $wpdb;
6053 $options = [];
6054 $course_id = $data['dynamic'];
6055 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mpcs_sections WHERE course_id =%s", $course_id ) );
6056 $sections = [];
6057 foreach ( $result as $rec ) {
6058 $sections[] = [
6059 'id' => $rec->id,
6060 'title' => $rec->title,
6061 ];
6062 }
6063 if ( is_array( $sections ) && count( $sections ) > 0 ) {
6064 foreach ( $sections as $section ) {
6065 $post_types_string = \memberpress\courses\models\Lesson::lesson_cpts();
6066 $post_types_string = implode( "','", $post_types_string );
6067
6068 $query = $wpdb->prepare(
6069 "SELECT * FROM {$wpdb->posts} AS p
6070 JOIN {$wpdb->postmeta} AS pm
6071 ON p.ID = pm.post_id
6072 AND pm.meta_key = %s
6073 AND pm.meta_value = %s
6074 JOIN {$wpdb->postmeta} AS pm_order
6075 ON p.ID = pm_order.post_id
6076 AND pm_order.meta_key = %s
6077 WHERE p.post_type in ( %s ) AND p.post_status <> 'trash'
6078 ORDER BY pm_order.meta_value * 1",
6079 models\Lesson::$section_id_str,
6080 $section['id'],
6081 models\Lesson::$lesson_order_str,
6082 stripcslashes( $post_types_string )
6083 );
6084
6085 $db_lessons = $wpdb->get_results( stripcslashes( $query ) ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
6086 foreach ( $db_lessons as $lesson ) {
6087 $options[] = [
6088 'label' => $section['title'] . '->' . $lesson->post_title,
6089 'value' => $lesson->ID,
6090 ];
6091 }
6092 }
6093 }
6094 return [
6095 'options' => $options,
6096 'hasMore' => false,
6097 ];
6098 }
6099
6100 /**
6101 * Get Memberpress Course List.
6102 *
6103 * @param array $data data.
6104 *
6105 * @return array|void
6106 */
6107 public function search_gp_rank_type_list( $data ) {
6108 global $wpdb;
6109
6110 $posts = $wpdb->get_results(
6111 "SELECT ID, post_name, post_title, post_type
6112 FROM $wpdb->posts
6113 WHERE post_type LIKE 'rank-type' AND post_status = 'publish' ORDER BY post_title ASC"
6114 );
6115
6116 $posts_count = count( $posts );
6117
6118 $options = [];
6119 if ( $posts ) {
6120 foreach ( $posts as $post ) {
6121 $options[] = [
6122 'label' => $post->post_title,
6123 'value' => $post->post_name,
6124 ];
6125 }
6126 }
6127
6128 return [
6129 'options' => $options,
6130 'hasMore' => false,
6131 ];
6132 }
6133
6134 /**
6135 * Get MPC last data.
6136 *
6137 * @param array $data data.
6138 *
6139 * @return array
6140 */
6141 public function search_mpc_last_data( $data ) {
6142 global $wpdb;
6143 $trigger = $data['search_term'];
6144 $course_data = [];
6145 $lesson_data = [];
6146 $context = [];
6147
6148 if ( 'mpc_course_completed' === $trigger ) {
6149 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course]']['value'] : '-1' );
6150 if ( $course_id > 0 ) {
6151
6152 $course = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $course_id ) );
6153 } else {
6154 $course = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-course' ORDER BY id DESC LIMIT 1" );
6155 }
6156
6157 if ( ! empty( $course ) ) {
6158 $course_data = [
6159 'course_id' => $course->ID,
6160 'course_title' => get_the_title( $course_id ),
6161 'course_url' => get_permalink( $course_id ),
6162 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
6163 'course_featured_image_url' => get_the_post_thumbnail_url( $course_id ),
6164 ];
6165 }
6166 $user_progress = $wpdb->get_row( $wpdb->prepare( "SELECT user_id FROM {$wpdb->prefix}mpcs_user_progress WHERE course_id=%s", $course_id ) );
6167 if ( ! empty( $user_progress ) ) {
6168 $context['response_type'] = 'live';
6169 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
6170 } else {
6171 $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}
6172 ,"response_type":"sample"} ';
6173 $context = json_decode( $sample_data, true );
6174 }
6175 } elseif ( 'mpc_lesson_completed' === $trigger ) {
6176 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
6177 $course_id = (int) $data['filter']['course']['value'];
6178 if ( $lesson_id > 0 ) {
6179
6180 $lesson = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $lesson_id ) );
6181 } else {
6182 $lesson = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-lesson' ORDER BY id DESC LIMIT 1" );
6183 }
6184
6185 if ( ! empty( $lesson ) ) {
6186 $lesson_data = [
6187 'lesson_id' => $lesson->ID,
6188 'lesson_title' => get_the_title( $lesson_id ),
6189 'lesson_url' => get_permalink( $lesson_id ),
6190 'lesson_featured_image_id' => get_post_meta( $lesson_id, '_thumbnail_id', true ),
6191 'lesson_featured_image_url' => get_the_post_thumbnail_url( $lesson_id ),
6192 ];
6193
6194 $lesson_section_id = get_post_meta( $lesson->ID, '_mpcs_lesson_section_id', true );
6195
6196 $section = $wpdb->get_row( $wpdb->prepare( "SELECT course_id FROM {$wpdb->prefix}mpcs_sections WHERE ID= %s", $lesson_section_id ) );
6197
6198 $course_data = [
6199 'course_id' => $course_id,
6200 'course_title' => get_the_title( $course_id ),
6201 'course_url' => get_permalink( $course_id ),
6202 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
6203 'course_featured_image_url' => get_the_post_thumbnail_url( $section->course_id ),
6204 ];
6205 }
6206
6207 $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 ) );
6208 if ( ! empty( $user_progress ) ) {
6209 $context['response_type'] = 'live';
6210 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
6211 } else {
6212 $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"}';
6213 $context = json_decode( $sample_data, true );
6214 }
6215 }
6216
6217
6218 return $context;
6219 }
6220
6221 /** Get GamiPress Rank List.
6222 *
6223 * @param array $data data.
6224 *
6225 * @return array|void
6226 */
6227 public function search_gp_rank_list( $data ) {
6228 global $wpdb;
6229
6230 $page = $data['page'];
6231 $limit = Utilities::get_search_page_limit();
6232 $offset = $limit * ( $page - 1 );
6233
6234 $args = [
6235 'post_type' => $data['dynamic']['rank_type'],
6236 'posts_per_page' => $limit,
6237 'offset' => $offset,
6238 'orderby' => 'title',
6239 'order' => 'ASC',
6240 'post_status' => 'publish',
6241 ];
6242
6243 $rank_type = get_posts( $args );
6244 $rank_type_count = count( $rank_type );
6245
6246 $options = [];
6247 if ( $rank_type ) {
6248 foreach ( $rank_type as $rank ) {
6249 $options[] = [
6250 'label' => $rank->post_title,
6251 'value' => $rank->ID,
6252 ];
6253 }
6254 }
6255
6256 return [
6257 'options' => $options,
6258 'hasMore' => $rank_type_count > $limit && $rank_type_count > $offset,
6259 ];
6260 }
6261
6262 /**
6263 * Get GamiPress PointType List.
6264 *
6265 * @param array $data data.
6266 *
6267 * @return array|void
6268 */
6269 public function search_gp_point_type_list( $data ) {
6270 global $wpdb;
6271
6272 $page = $data['page'];
6273 $limit = Utilities::get_search_page_limit();
6274 $offset = $limit * ( $page - 1 );
6275
6276 $args = [
6277 'post_type' => 'points-type',
6278 'posts_per_page' => $limit,
6279 'offset' => $offset,
6280 'orderby' => 'title',
6281 'order' => 'ASC',
6282 'post_status' => 'publish',
6283 ];
6284
6285 $point_type = get_posts( $args );
6286 $point_type_count = count( $point_type );
6287
6288 $options = [];
6289 if ( $point_type ) {
6290 foreach ( $point_type as $point ) {
6291 $options[] = [
6292 'label' => $point->post_title,
6293 'value' => $point->ID,
6294 ];
6295 }
6296 }
6297
6298 return [
6299 'options' => $options,
6300 'hasMore' => $point_type_count > $limit && $point_type_count > $offset,
6301 ];
6302 }
6303
6304 /**
6305 * Get GamiPress AchievementType List.
6306 *
6307 * @param array $data data.
6308 *
6309 * @return array|void
6310 */
6311 public function search_gp_achivement_type_list( $data ) {
6312 global $wpdb;
6313
6314 $posts = $wpdb->get_results(
6315 "SELECT ID, post_name, post_title, post_type
6316 FROM $wpdb->posts
6317 WHERE post_type LIKE 'achievement-type' AND post_status = 'publish' ORDER BY post_title ASC"
6318 );
6319
6320 $posts_count = count( $posts );
6321
6322 $options = [];
6323 if ( $posts ) {
6324 foreach ( $posts as $post ) {
6325 $options[] = [
6326 'label' => $post->post_title,
6327 'value' => $post->post_name,
6328 ];
6329 }
6330 }
6331
6332 $options[] = [
6333 'label' => 'Points awards',
6334 'value' => 'points-award',
6335 ];
6336 $options[] = [
6337 'label' => 'Step',
6338 'value' => 'step',
6339 ];
6340 $options[] = [
6341 'label' => 'Rank requirement',
6342 'value' => 'rank-requirement',
6343 ];
6344
6345 return [
6346 'options' => $options,
6347 'hasMore' => false,
6348 ];
6349 }
6350
6351 /**
6352 * Get GamiPress Award List.
6353 *
6354 * @param array $data data.
6355 *
6356 * @return array|void
6357 */
6358 public function search_gp_award_list( $data ) {
6359 global $wpdb;
6360
6361 $page = $data['page'];
6362 $limit = Utilities::get_search_page_limit();
6363 $offset = $limit * ( $page - 1 );
6364
6365 $args = [
6366 'post_type' => $data['dynamic']['achivement_type'],
6367 'posts_per_page' => $limit,
6368 'offset' => $offset,
6369 'orderby' => 'title',
6370 'order' => 'ASC',
6371 'post_status' => 'publish',
6372 ];
6373
6374 $award_type = get_posts( $args );
6375 $award_type_count = count( $award_type );
6376
6377 $options = [];
6378 if ( $award_type ) {
6379 foreach ( $award_type as $award ) {
6380 $options[] = [
6381 'label' => $award->post_title,
6382 'value' => $award->ID,
6383 ];
6384 }
6385 }
6386
6387 return [
6388 'options' => $options,
6389 'hasMore' => $award_type_count > $limit && $award_type_count > $offset,
6390 ];
6391 }
6392
6393 /**
6394 * Get Woocommerce Subscription Product List.
6395 *
6396 * @param array $data data.
6397 *
6398 * @return array|void
6399 */
6400 public function search_wc_subscription_product_list( $data ) {
6401 global $wpdb;
6402
6403 $subscriptions = $wpdb->get_results(
6404 $wpdb->prepare(
6405 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
6406 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
6407 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
6408 AND posts.post_type = %s
6409 AND posts.post_status = %s
6410 UNION ALL
6411 SELECT ID, post_title FROM $wpdb->posts
6412 WHERE post_type = %s
6413 AND post_status = %s
6414 ORDER BY post_title",
6415 'product',
6416 'publish',
6417 'shop_subscription',
6418 'publish'
6419 )
6420 );
6421
6422 $options = [];
6423 if ( $subscriptions ) {
6424 foreach ( $subscriptions as $post ) {
6425 $options[] = [
6426 'label' => $post->post_title,
6427 'value' => $post->ID,
6428 ];
6429 }
6430 }
6431
6432 return [
6433 'options' => $options,
6434 'hasMore' => false,
6435 ];
6436 }
6437
6438 /**
6439 * Get Woocommerce Subscriptions Variation list.
6440 *
6441 * @param array $data data.
6442 *
6443 * @return array|void
6444 */
6445 public function search_wc_variable_subscription_list( $data ) {
6446 global $wpdb;
6447
6448 $page = $data['page'];
6449 $limit = Utilities::get_search_page_limit();
6450 $offset = $limit * ( $page - 1 );
6451
6452 if ( ! function_exists( 'wc_get_products' ) ) {
6453 return;
6454 }
6455 $subscription_products = wc_get_products(
6456 [
6457 'type' => [ 'variable-subscription' ],
6458 'posts_per_page' => $limit,
6459 'offset' => $offset,
6460 'orderby' => 'date',
6461 'order' => 'DESC',
6462 ]
6463 );
6464
6465 $subscription_products_count = count( (array) $subscription_products );
6466
6467 $options = [];
6468 if ( $subscription_products ) {
6469 foreach ( (array) $subscription_products as $product ) {
6470 $options[] = [
6471 'label' => $product->get_title(),
6472 'value' => $product->get_id(),
6473 ];
6474 }
6475 }
6476
6477 return [
6478 'options' => $options,
6479 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
6480 ];
6481 }
6482
6483 /**
6484 * Get Woocommerce Variation list.
6485 *
6486 * @param array $data data.
6487 *
6488 * @return array|void
6489 */
6490 public function search_wc_variation_list( $data ) {
6491 global $wpdb;
6492
6493 $page = $data['page'];
6494 $limit = Utilities::get_search_page_limit();
6495 $offset = $limit * ( $page - 1 );
6496
6497 $args = [
6498 'post_type' => 'product_variation',
6499 'post_parent' => $data['dynamic']['variable_subscription'],
6500 'posts_per_page' => $limit,
6501 'offset' => $offset,
6502 'orderby' => 'ID',
6503 'order' => 'ASC',
6504 'post_status' => 'publish',
6505 ];
6506
6507 $variation = get_posts( $args );
6508 $variation_count = count( $variation );
6509
6510 $options = [];
6511 if ( $variation ) {
6512 foreach ( $variation as $product ) {
6513 $options[] = [
6514 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
6515 'value' => $product->ID,
6516 ];
6517 }
6518 }
6519
6520 return [
6521 'options' => $options,
6522 'hasMore' => $variation_count > $limit && $variation_count > $offset,
6523 ];
6524 }
6525
6526 /**
6527 * Get Membership List.
6528 *
6529 * @param array $data data.
6530 *
6531 * @return array
6532 */
6533 public function search_membership_list( $data ) {
6534 global $wpdb;
6535
6536 $levels = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}pmpro_membership_levels ORDER BY id ASC" );
6537 $options = [];
6538 if ( $levels ) {
6539 foreach ( $levels as $level ) {
6540 $options[] = [
6541 'label' => $level->name,
6542 'value' => $level->id,
6543 ];
6544 }
6545 }
6546
6547 return [
6548 'options' => $options,
6549 'hasMore' => false,
6550 ];
6551 }
6552
6553 /**
6554
6555 * Get EventsManager last data.
6556 *
6557 * @param array $data data.
6558 *
6559 * @return array
6560 */
6561 public function search_events_manager_data( $data ) {
6562 global $wpdb;
6563 $trigger = $data['search_term'];
6564 $context = [];
6565
6566 $post_id = (int) ( isset( $data['filter']['post_id']['value'] ) ? $data['filter']['post_id']['value'] : '-1' );
6567 if ( 'em_user_register_in_event' === $trigger ) {
6568 if ( $post_id > 0 ) {
6569 $event_id_id = get_post_meta( $post_id, '_event_id', true );
6570 $all_bookings = $wpdb->get_results( $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 e.event_id DESC LIMIT 1", $event_id_id ) );
6571 } else {
6572 $all_bookings = $wpdb->get_results( "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 e.event_id DESC LIMIT 1" );
6573
6574 }
6575
6576 if ( ! empty( $all_bookings ) ) {
6577 $user_id = $all_bookings->person_id;
6578 $context['pluggable_data'] = array_merge(
6579 WordPress::get_user_context( $user_id ),
6580 json_decode( wp_json_encode( $all_bookings ), 1 )
6581 );
6582 $context['response_type'] = 'live';
6583 }
6584 } elseif ( 'em_user_unregister_from_event' === $trigger ) {
6585
6586 if ( $post_id > 0 ) {
6587 $event_id_id = get_post_meta( $post_id, '_event_id', true );
6588 $all_bookings = $wpdb->get_results( $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 e.event_id DESC LIMIT 1", $event_id_id ) );
6589 } else {
6590 $all_bookings = $wpdb->get_results( "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 e.event_id DESC LIMIT 1" );
6591
6592 }
6593
6594 if ( ! empty( $all_bookings ) ) {
6595 $user_id = $all_bookings->person_id;
6596 $context['pluggable_data'] = array_merge(
6597 WordPress::get_user_context( $user_id ),
6598 json_decode( wp_json_encode( $all_bookings ), 1 )
6599 );
6600 $context['response_type'] = 'live';
6601 }
6602 } elseif ( 'em_user_booking_approved' === $trigger ) {
6603
6604 if ( $post_id > 0 ) {
6605 $event_id_id = get_post_meta( $post_id, '_event_id', true );
6606 $all_bookings = $wpdb->get_results( $wpdb->prepare( "SELECT e.*,b.person_id 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 e.event_id DESC LIMIT 1", $event_id_id ) );
6607 } else {
6608 $all_bookings = $wpdb->get_results( "SELECT e.*,b.person_id 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 e.event_id DESC LIMIT 1" );
6609
6610 }
6611
6612 if ( ! empty( $all_bookings ) ) {
6613 $user_id = $all_bookings->person_id;
6614 $context['pluggable_data'] = array_merge(
6615 WordPress::get_user_context( $user_id ),
6616 json_decode( wp_json_encode( $all_bookings ), 1 )
6617 );
6618 $context['response_type'] = 'live';
6619
6620 }
6621 }
6622 return $context;
6623 }
6624
6625 /**
6626
6627 * Get learnpress last data.
6628 *
6629 * @param array $data data.
6630 *
6631 * @return array
6632 */
6633 public function search_learnpress_lms_last_data( $data ) {
6634 global $wpdb;
6635 $trigger = $data['search_term'];
6636 $course_data = [];
6637 $lesson_data = [];
6638 $context = [];
6639
6640
6641 if ( 'learnpress_course_completed' === $trigger ) {
6642 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
6643 if ( $course_id > 0 ) {
6644
6645 $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 ) );
6646 } else {
6647
6648 $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" );
6649 }
6650
6651 if ( ! empty( $course ) ) {
6652 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
6653 $context['response_type'] = 'live';
6654 $context['pluggable_data'] = $course_data;
6655 }
6656 } elseif ( 'learnpress_lesson_completed' === $trigger ) {
6657 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
6658 if ( $lesson_id > 0 ) {
6659
6660 $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 ) );
6661 } else {
6662
6663 $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" );
6664 }
6665
6666 if ( ! empty( $lesson ) ) {
6667 $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 ) );
6668 $context['response_type'] = 'live';
6669 $context['pluggable_data'] = $lesson_data;
6670 }
6671 } elseif ( 'learnpress_user_enrolled_in_course' === $trigger ) {
6672 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
6673 if ( $course_id > 0 ) {
6674
6675 $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 ) );
6676 } else {
6677
6678 $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" );
6679 }
6680
6681 if ( ! empty( $course ) ) {
6682 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
6683 $context['response_type'] = 'live';
6684 $context['pluggable_data'] = $course_data;
6685
6686 }
6687 }
6688
6689 return $context;
6690 }
6691
6692 }
6693
6694 GlobalSearchController::get_instance();
6695