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