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