PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.0.63
OttoKit: All-in-One Automation Platform v1.0.63
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 1 year ago AutomationController.php 3 years ago EventController.php 3 years ago GlobalSearchController.php 1 year ago IntegrationsController.php 2 years ago OptionController.php 3 years ago RestController.php 1 year ago RoutesController.php 3 years ago SettingsController.php 3 years ago
GlobalSearchController.php
19209 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 FluentCrm\App\Models\SubscriberMeta;
21 use FluentCrm\App\Models\Lists;
22 use memberpress\courses\lib as lib;
23 use memberpress\courses\models as models;
24 use FluentCrm\Framework\Support\Arr;
25 use GFCommon;
26 use GFFormsModel;
27 use Give_Payment;
28 use Give_Subscription;
29 use MeprBaseRealGateway;
30 use MeprOptions;
31 use OsAgentHelper;
32 use OsBookingHelper;
33 use OsCustomerHelper;
34 use OsServiceHelper;
35 use OsWpUserHelper;
36 use PrestoPlayer\Models\Video;
37 use RGFormsModel;
38 use SureTriggers\Integrations\AffiliateWP\AffiliateWP;
39 use SureTriggers\Integrations\EDD\EDD;
40 use SureTriggers\Integrations\FunnelKitAutomations\FunnelKitAutomations;
41 use SureTriggers\Integrations\JetpackCRM\JetpackCRM;
42 use SureTriggers\Integrations\LearnDash\LearnDash;
43 use SureTriggers\Integrations\LifterLMS\LifterLMS;
44 use SureTriggers\Integrations\MemberPress\MemberPress;
45 use SureTriggers\Integrations\MemberPressCourse\MemberPressCourse;
46 use SureTriggers\Integrations\ModernEventsCalendar\ModernEventsCalendar;
47 use SureTriggers\Integrations\PeepSo\PeepSo;
48 use SureTriggers\Integrations\RafflePress\RafflePress;
49 use SureTriggers\Integrations\RestrictContent\RestrictContent;
50 use SureTriggers\Integrations\TheEventCalendar\TheEventCalendar;
51 use SureTriggers\Integrations\WishlistMember\WishlistMember;
52 use SureTriggers\Integrations\WooCommerce\WooCommerce;
53 use SureTriggers\Integrations\WordPress\WordPress;
54 use SureTriggers\Integrations\WpPolls\WpPolls;
55 use SureTriggers\Models\Utilities;
56 use SureTriggers\Traits\SingletonLoader;
57 use Tribe__Tickets__Tickets_Handler;
58 use WC_Subscription;
59 use WC_Subscriptions_Product;
60 use WP_Query;
61 use WP_Comment_Query;
62 use WP_REST_Request;
63 use WP_REST_Response;
64 use WPForms_Form_Handler;
65 use CP_V2_Popups;
66 use Project_Huddle;
67 use FrmForm;
68 use Forminator_API;
69 use SureTriggers\Integrations\LearnPress\LearnPress;
70 use WC_Customer;
71 use WC_Booking;
72 use WC_Bookings_Admin;
73 use MeprTransaction;
74 use WC_Order;
75 use LLMS_Section;
76 use BP_Signup;
77 use WP_Post;
78 use AsgarosForum;
79 use PeepSoUser;
80 use PeepSoField;
81 use Mint\MRM\DataBase\Models\ContactModel;
82 use Mint\MRM\DataBase\Models\ContactGroupModel;
83 use SureTriggers\Integrations\Voxel\Voxel;
84 use Surelywp_Support_Portal;
85
86 /**
87 * GlobalSearchController- Add ajax related functions here.
88 *
89 * @category GlobalSearchController
90 * @package SureTriggers
91 * @author BSF <username@example.com>
92 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
93 * @link https://www.brainstormforce.com/
94 * @since 1.0.0
95 *
96 * @psalm-suppress UndefinedTrait
97 */
98 class GlobalSearchController {
99
100 use SingletonLoader;
101
102 /**
103 * Search post by post type.
104 *
105 * @param array $data Search Params.
106 *
107 * @since 1.0.0
108 *
109 * @return array
110 */
111 public function search_post( $data ) {
112 $result = [];
113 $posts = Utilities::find_posts_by_title( $data );
114
115 foreach ( $posts['results'] as $post ) {
116 $result[] = [
117 'label' => $post['post_title'],
118 'value' => $post['ID'],
119 ];
120 }
121
122 return [
123 'options' => $result,
124 'hasMore' => $posts['has_more'],
125 ];
126 }
127
128 /**
129 * Search Course.
130 *
131 * @param array $data quesry params.
132 *
133 * @return array
134 * @since 1.0.0
135 */
136 public function search_ld_course( $data ) {
137
138 $page = $data['page'];
139 $limit = Utilities::get_search_page_limit();
140 $offset = $limit * ( $page - 1 );
141
142 $courses = get_posts(
143 [
144
145 'post_type' => 'product',
146 'meta_key' => '_related_course',
147 'post_status' => 'publish',
148 'posts_per_page' => $limit,
149 'offset' => $offset,
150 ]
151 );
152 $options = [];
153 foreach ( $courses as $course ) {
154 $options[] = [
155 'label' => $course->post_title,
156 'value' => $course->ID,
157 ];
158 }
159
160 $all_courses = get_posts(
161 [
162 'post_type' => 'product',
163 'meta_key' => '_related_course',
164 'post_status' => 'publish',
165 'fields' => 'ids',
166 'posts_per_page' => -1,
167 ]
168 );
169 $posts_count = count( $all_courses );
170
171 return [
172 'options' => $options,
173 'hasMore' => $posts_count > $limit && $posts_count > $offset,
174 ];
175 }
176
177 /**
178 * Search achievement by post type.
179 *
180 * @param array $data Search Params.
181 *
182 * @since 1.0.0
183 *
184 * @return array
185 */
186 public function search_achievements( $data ) {
187
188 $page = $data['page'];
189 $limit = Utilities::get_search_page_limit();
190 $offset = $limit * ( $page - 1 );
191
192 $post = get_post( $data['dynamic'] );
193 $slug = $post->post_name;
194
195 $achievements = get_posts(
196 [
197 'post_type' => $slug,
198 'post_status' => 'publish',
199 'posts_per_page' => $limit,
200 'offset' => $offset,
201 ]
202 );
203 $options = [];
204 foreach ( $achievements as $achievement ) {
205 $options[] = [
206 'label' => $achievement->post_title,
207 'value' => (string) $achievement->ID,
208 ];
209 }
210
211 $count = wp_count_posts( $slug )->publish;
212
213 return [
214 'options' => $options,
215 'hasMore' => $count > $limit && $count > $offset,
216 ];
217 }
218
219 /**
220 * Search Course.
221 *
222 * @param array $data quesry params.
223 *
224 * @return array
225 * @since 1.0.0
226 */
227 public function search_tutor_course( $data ) {
228 $courses = get_posts(
229 [
230 'post_type' => tutor()->course_post_type,
231 'post_status' => 'publish',
232 'numberposts' => '-1',
233 ]
234 );
235 $options = [];
236 foreach ( $courses as $course ) {
237 $options[] = [
238 'label' => $course->post_title,
239 'value' => $course->ID,
240 ];
241 }
242
243 return [
244 'options' => $options,
245 'hasMore' => false,
246 ];
247 }
248
249 /**
250 * Search Products.
251 *
252 * @param array $data quesry params.
253 *
254 * @return array
255 * @since 1.0.0
256 */
257 public function search_product( $data ) {
258 $result = [];
259 $posts = Utilities::find_posts_by_title( $data );
260
261 foreach ( $posts['results'] as $post ) {
262 $result[] = [
263 'label' => $post['post_title'],
264 'value' => $post['post_title'],
265 ];
266 }
267
268 return [
269 'options' => $result,
270 'hasMore' => $posts['has_more'],
271 ];
272 }
273
274 /**
275 * Search Product Categories.
276 *
277 * @param array $data Search Params.
278 *
279 * @since 1.0.0
280 */
281 public function search_product_category( $data ) {
282 if ( ! empty( $data['dynamic'] ) ) {
283 $taxonomy = $data['dynamic'];
284 } else {
285 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
286 }
287
288 $term = $data['term'];
289 $result = [];
290 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
291 foreach ( $terms['result'] as $tax_term ) {
292 $result[] = [
293 'label' => $tax_term->name,
294 'value' => $tax_term->name,
295 ];
296 }
297
298 return [
299 'options' => $result,
300 'hasMore' => $terms['has_more'],
301 ];
302 }
303
304 /**
305 * Search Product Tags.
306 *
307 * @param array $data Search Params.
308 *
309 * @since 1.0.0
310 */
311 public function search_product_tags( $data ) {
312 if ( ! empty( $data['dynamic'] ) ) {
313 $taxonomy = $data['dynamic'];
314 } else {
315 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
316 }
317
318 $term = $data['term'];
319 $result = [];
320 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
321
322 foreach ( $terms['result'] as $tax_term ) {
323 $result[] = [
324 'label' => $tax_term->name,
325 'value' => $tax_term->name,
326 ];
327 }
328
329 return [
330 'options' => $result,
331 'hasMore' => $terms['has_more'],
332 ];
333 }
334
335 /**
336 * Global ajax search.
337 * Here you need to add the field action name to work.
338 *
339 * @param WP_REST_Request $request Request data.
340 *
341 * @return WP_REST_Response
342 * @since 1.0.0
343 */
344 public function global_search( $request ) {
345 $post_type = $request->get_param( 'post_type' );
346 $dynamic = $request->get_param( 'dynamic' );
347 $search_term = $request->get_param( 'term' );
348 $identifier = $request->get_param( 'field_identifier' );
349 $page = max( 1, $request->get_param( 'page' ) );
350 $taxonomy = $request->get_param( 'taxonomy' ) ? $request->get_param( 'taxonomy' ) : [];
351
352 $filter = $request->get_param( 'filter' ) ? json_decode( stripslashes( $request->get_param( 'filter' ) ), true ) : [];
353
354 $data = [
355 'dynamic' => $dynamic,
356 'search_term' => $search_term,
357 'page' => $page,
358 'taxonomy' => $taxonomy,
359 'filter' => $filter,
360 'post_type' => $post_type,
361 ];
362 $response = [
363 'hasMore' => false,
364 'options' => [],
365 ];
366
367 $method_name = 'search_' . $identifier;
368
369 if ( method_exists( $this, $method_name ) ) {
370 $response = $this->{$method_name}( $data );
371 } else {
372 return RestController::error_message( 'Invalid field Identifier param.' );
373 }
374
375 return RestController::success_message( $response );
376 }
377
378 /**
379 * Search Taxonomy Terms.
380 *
381 * @param array $data Search Params.
382 *
383 * @since 1.0.0
384 *
385 * @return array
386 */
387 public function search_term( $data ) {
388 if ( ! empty( $data['dynamic'] ) ) {
389 $taxonomy = $data['dynamic'];
390 } else {
391 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
392 }
393
394 $term = $data['term'];
395 $result = [];
396 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
397 foreach ( $terms['result'] as $tax_term ) {
398 $result[] = [
399 'label' => $tax_term->name,
400 'value' => $tax_term->term_id,
401 ];
402 }
403
404 return [
405 'options' => $result,
406 'hasMore' => $terms['has_more'],
407 ];
408 }
409
410 /**
411 * Search sureemails data.
412 *
413 * @param array $data data.
414 * @return array
415 */
416 public function search_pluggables_sureemails_mail( $data ) {
417 $context['pluggable_data'] = [
418 'to' => [ 'johnDoe@xyz.com' ],
419 'subject' => 'Test Email',
420 'message' => 'This is a test email',
421 'headers' => [ 'X-Mailer' => 'PHP/8.1.29' ],
422 'attachments' => [ 'Sample Attachment' ],
423 ];
424 $context['response_type'] = 'sample';
425 return $context;
426 }
427
428 /**
429 * List Taxonomy Terms.
430 *
431 * @param array $data Search Params.
432 *
433 * @since 1.0.0
434 *
435 * @return array
436 */
437 public function search_term_list( $data ) {
438 $taxonomy = $data['dynamic'];
439 $result = [];
440 $terms = Utilities::get_terms( '', $data['page'], $taxonomy );
441 foreach ( $terms['result'] as $tax_term ) {
442 $result[] = [
443 'label' => $tax_term->name,
444 'value' => $tax_term->term_id,
445 ];
446 }
447
448 return [
449 'options' => $result,
450 'hasMore' => $terms['has_more'],
451 ];
452 }
453
454 /**
455 * List Taxonomy Tags.
456 *
457 * @param array $data Search Params.
458 *
459 * @since 1.0.0
460 *
461 * @return array
462 */
463 public function search_term_list_tags( $data ) {
464 $result = [];
465 $terms = Utilities::get_terms( '', $data['page'], [ 'post_tag' ] );
466 foreach ( $terms['result'] as $tax_term ) {
467 $result[] = [
468 'label' => $tax_term->name,
469 'value' => $tax_term->term_id,
470 ];
471 }
472
473 return [
474 'options' => $result,
475 'hasMore' => $terms['has_more'],
476 ];
477 }
478
479 /**
480 * List Role Capabilities.
481 *
482 * @param array $data Search Params.
483 *
484 * @since 1.0.0
485 *
486 * @return array
487 */
488 public function search_role_capabilities( $data ) {
489 $user_roles = wp_roles();
490 $result = [];
491
492 foreach ( $user_roles->roles as $name => $role ) {
493 foreach ( $role['capabilities'] as $key => $cap ) {
494 $result[] = [
495 'label' => $key,
496 'value' => esc_attr( $key ),
497 ];
498 }
499 }
500 $result = array_unique( $result, SORT_REGULAR );
501
502 return [
503 'options' => $result,
504 'hasMore' => false,
505 ];
506 }
507
508 /**
509 * Search users.
510 *
511 * @param array $data Search Params.
512 *
513 * @since 1.0.0
514 *
515 * @return array
516 */
517 public function search_user( $data ) {
518 $result = [];
519 $page = $data['page'];
520 $users = Utilities::get_users( $data, $page );
521
522 if ( is_array( $users['results'] ) ) {
523 foreach ( $users['results'] as $user ) {
524 $result[] = [
525 'label' => $user->user_login,
526 'value' => $user->ID,
527 ];
528 }
529 }
530
531 return [
532 'options' => $result,
533 'hasMore' => $users['has_more'],
534 ];
535
536 }
537
538 /**
539 * Search WPForm fields.
540 *
541 * @param array $data Search Params.
542 *
543 * @since 1.0.0
544 *
545 * @return array
546 */
547 public function search_pluggable_wpform_fields( $data ) {
548 $result = [];
549 $page = $data['page'];
550 $form_id = absint( $data['dynamic'] );
551 $wpform_fields = Utilities::get_wpform_fields( $data['search_term'], $page, $form_id );
552
553 if ( is_array( $wpform_fields['results'] ) ) {
554 foreach ( $wpform_fields['results'] as $field ) {
555 $result[] = [
556 'label' => $field['label'],
557 'value' => '{' . $field['id'] . '}',
558 ];
559 }
560 }
561
562 return [
563 'options' => $result,
564 'hasMore' => $wpform_fields['has_more'],
565 ];
566 }
567
568 /**
569 * Prepare variable products.
570 *
571 * @param array $data data.
572 *
573 * @return array
574 */
575 public function search_variable_products( $data ) {
576 $products = Utilities::get_variable_products( $data['search_term'], $data['page'] );
577 $options = [];
578
579 foreach ( $products['result'] as $product ) {
580 $options[] = [
581 'label' => $product->get_title(),
582 'value' => (string) $product->get_id(),
583 ];
584 }
585
586 return [
587 'options' => $options,
588 'hasMore' => $products['has_more'],
589 ];
590 }
591
592 /**
593 * Prepare variable products.
594 *
595 * @param array $data data.
596 *
597 * @return array
598 */
599 public function search_product_variations( $data ) {
600 $variations = Utilities::get_product_variations( $data['dynamic'] );
601 $options = [];
602
603 foreach ( $variations['result'] as $product ) {
604 $options[] = [
605 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
606 'value' => (string) $product->ID,
607 ];
608 }
609
610 return [
611 'options' => $options,
612 'hasMore' => $variations['has_more'],
613 ];
614 }
615
616 /**
617 * Search WooCommerce Subscriptions.
618 *
619 * @param array $data Search Params.
620 *
621 * @since 1.0.0
622 *
623 * @return array
624 */
625 public function search_subscription_variation( $data ) {
626 $subscription_products = Utilities::get_subscription_variation( $data['search_term'], $data['page'] );
627 $result = [];
628
629 if ( ! function_exists( 'wc_get_products' ) ) {
630 return $result;
631 }
632
633 foreach ( $subscription_products['result'] as $post ) {
634 if ( $data['search_term'] ) {
635 if ( false !== stripos( $post->get_title(), $data['search_term'] ) ) {
636 $result[] = [
637 'label' => $post->get_title(),
638 'value' => (string) $post->get_id(),
639 ];
640 }
641 } else {
642 $result[] = [
643 'label' => $post->get_title(),
644 'value' => (string) $post->get_id(),
645 ];
646 }
647 }
648
649 return [
650 'options' => $result,
651 'hasMore' => $subscription_products['has_more'],
652 ];
653 }
654
655 /**
656 * Prepare WooCommerce Payment Methods.
657 *
658 * @param array $data Search Params.
659 * @return array[]
660 */
661 public function search_woo_payment_methods( $data ) {
662 $payment_methods = WC()->payment_gateways->get_available_payment_gateways();
663 $options = [];
664
665 if ( ! empty( $payment_methods ) ) {
666 foreach ( $payment_methods as $payment ) {
667 $options[] = [
668 'label' => $payment->title,
669 'value' => $payment->id,
670 ];
671 }
672 }
673
674 return [
675 'options' => $options,
676 'hasMore' => false,
677 ];
678 }
679
680 /**
681 * Prepare WooCommerce Order Status List.
682 *
683 * @param array $data Search Params.
684 * @return array[]
685 */
686 public function search_woo_order_status_list( $data ) {
687 $order_status = wc_get_order_statuses();
688 $options = [];
689
690 if ( ! empty( $order_status ) ) {
691 foreach ( $order_status as $key => $status ) {
692 $options[] = [
693 'label' => $status,
694 'value' => $key,
695 ];
696 }
697 }
698
699 return [
700 'options' => $options,
701 'hasMore' => false,
702 ];
703 }
704
705 /**
706 * Prepare WooCommerce Country List.
707 *
708 * @param array $data Search Params.
709 * @return array[]
710 */
711 public function search_woo_country_list( $data ) {
712 $countries = WC()->countries->get_countries();
713 $options = [];
714
715 if ( ! empty( $countries ) ) {
716 foreach ( $countries as $key => $country ) {
717 $options[] = [
718 'label' => $country,
719 'value' => $key,
720 ];
721 }
722 }
723
724 return [
725 'options' => $options,
726 'hasMore' => false,
727 ];
728 }
729
730 /**
731 * Prepare WooCommerce Country States List.
732 *
733 * @param array $data Search Params.
734 * @return array[]
735 */
736 public function search_woo_country_state_list( $data ) {
737 if ( ! empty( $data['dynamic']['shipping_country'] ) ) {
738 $cc = $data['dynamic']['shipping_country'];
739 } else {
740 $cc = $data['dynamic'];
741 }
742
743 $states = WC()->countries->get_states( $cc );
744
745 $options = [];
746 if ( ! empty( $states ) ) {
747 foreach ( $states as $key => $state ) {
748 $options[] = [
749 'label' => $state,
750 'value' => $key,
751 ];
752 }
753 }
754
755 return [
756 'options' => $options,
757 'hasMore' => false,
758 ];
759 }
760
761 /**
762 * Get Memberpress gatways (payment methods) for subscription.
763 *
764 * @param array $data QueryParams.
765 *
766 * @return array
767 * @since 1.0.0
768 */
769 public function search_memberpress_gayways( $data ) {
770 $mp_options = MeprOptions::fetch();
771
772 $pms = array_keys( $mp_options->integrations );
773 $gateways = [];
774
775 foreach ( $pms as $pm_id ) {
776 $obj = $mp_options->payment_method( $pm_id );
777 if ( $obj instanceof MeprBaseRealGateway ) {
778 $gateways[] = [
779 'label' => sprintf( '%1$s (%2$s)', $obj->label, $obj->name ),
780 'value' => $obj->id,
781 ];
782 }
783 }
784
785 return [
786 'options' => $gateways,
787 'hasMore' => false,
788 ];
789 }
790
791 /**
792 * Prepare roles.
793 *
794 * @param array $data data.
795 *
796 * @return array
797 */
798 public function search_roles( $data ) {
799 $roles = wp_roles()->roles;
800 $options = [];
801 foreach ( $roles as $role => $details ) {
802
803 $options[] = [
804 'label' => $details['name'],
805 'value' => $role,
806 ];
807
808 }
809
810 return [
811 'options' => $options,
812 'hasMore' => false,
813 ];
814 }
815
816 /**
817 * Prepare post types.
818 *
819 * @param array $data data.
820 *
821 * @return array
822 */
823 public function search_post_types( $data ) {
824 $post_types = get_post_types( [ 'public' => true ], 'object' );
825 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
826 if ( isset( $post_types['attachment'] ) ) {
827 unset( $post_types['attachment'] );
828 }
829
830 $options = [];
831 foreach ( $post_types as $post_type => $details ) {
832 $options[] = [
833 'label' => $details->label,
834 'value' => $post_type,
835 ];
836 }
837
838 return [
839 'options' => $options,
840 'hasMore' => false,
841 ];
842 }
843
844 /**
845 * Get post statuses.
846 *
847 * @param array $data data.
848 *
849 * @return array
850 */
851 public function search_post_statuses( $data ) {
852 $post_statuses = get_post_stati( [], 'objects' );
853 $post_statuses = apply_filters( 'suretriggers_post_types', $post_statuses );
854 $options = [];
855
856 foreach ( $post_statuses as $post_status => $details ) {
857 if ( 'woocommerce' === $details->label_count['domain'] ) {
858 $options[] = [
859 'label' => 'WooCommerce - ' . $details->label,
860 'value' => $post_status,
861 ];
862 } else {
863 $options[] = [
864 'label' => $details->label,
865 'value' => $post_status,
866 ];
867 }
868 }
869
870 return [
871 'options' => $options,
872 'hasMore' => false,
873 ];
874 }
875
876 /**
877 * Get Taxonomies.
878 *
879 * @param array $data data.
880 *
881 * @return array
882 */
883 public function search_taxonomy_list( $data ) {
884 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
885 $options = [];
886 $options[0] = [
887 'label' => 'Any Taxonomy',
888 'value' => -1,
889 ];
890
891 foreach ( $taxonomies as $taxonomy => $taxonomy_obj ) {
892 $options[] = [
893 'label' => $taxonomy_obj->label,
894 'value' => $taxonomy_obj->name,
895 ];
896 }
897
898 return [
899 'options' => $options,
900 'hasMore' => false,
901 ];
902 }
903
904 /**
905 * Get Taxonomies without any option.
906 *
907 * @param array $data data.
908 *
909 * @return array
910 */
911 public function search_taxonomy_list_without_any( $data ) {
912 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
913 $options = [];
914 foreach ( $taxonomies as $taxonomy => $taxonomy_obj ) {
915 $options[] = [
916 'label' => $taxonomy_obj->label,
917 'value' => $taxonomy_obj->name,
918 ];
919 }
920
921 return [
922 'options' => $options,
923 'hasMore' => false,
924 ];
925 }
926
927 /**
928 * Get WPForms.
929 *
930 * @param array $data data.
931 *
932 * @return array
933 */
934 public function search_wp_forms( $data ) {
935 if ( ! class_exists( 'WPForms_Form_Handler' ) ) {
936 return;
937 }
938
939 $wpforms = new WPForms_Form_Handler();
940 $forms = $wpforms->get( '', [ 'orderby' => 'title' ] );
941 $options = [];
942
943 if ( ! empty( $forms ) ) {
944 foreach ( $forms as $form ) {
945 $options[] = [
946 'label' => $form->post_title,
947 'value' => $form->ID,
948 ];
949 }
950 }
951
952 return [
953 'options' => $options,
954 'hasMore' => false,
955 ];
956 }
957
958 /**
959 * Get Gravity Forms.
960 *
961 * @param array $data data.
962 *
963 * @return array
964 */
965 public function search_gravity_forms( $data ) {
966 if ( ! class_exists( 'GFFormsModel' ) ) {
967 return;
968 }
969
970 $forms = GFFormsModel::get_forms();
971 $options = [];
972
973 if ( ! empty( $forms ) ) {
974 foreach ( $forms as $form ) {
975 $options[] = [
976 'label' => $form->title,
977 'value' => $form->id,
978 ];
979 }
980 }
981
982 return [
983 'options' => $options,
984 'hasMore' => false,
985 ];
986 }
987
988 /**
989 * Get tag & contact details.
990 *
991 * @param array $data data.
992 *
993 * @return array
994 */
995 public function search_pluggables_fluentcrm_contact_added_to_tags( $data ) {
996 $context = [];
997 $pluggable_data = [];
998 $tag_id = $data['filter'];
999
1000 if ( ! class_exists( 'FluentCrm\App\Models\Subscriber' ) || ! class_exists( 'FluentCrm\App\Models\Tag' ) ) {
1001 return [];
1002 }
1003
1004 if ( $tag_id > 0 ) {
1005 $tags = Tag::where( 'id', $tag_id )->first();
1006 } else {
1007 $tags = Tag::orderBy( 'id', 'DESC' )->first();
1008 }
1009 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1010 if ( $contact ) {
1011 $pluggable_data['contact'] = $contact;
1012 $context['tag_id'] = $tag_id;
1013 $pluggable_data['tag'] = $tags;
1014 $context['response_type'] = 'live';
1015 } else {
1016 $pluggable_data['conatct']['full_name'] = 'Test User';
1017 $pluggable_data['conatct']['first_name'] = 'Test';
1018 $pluggable_data['conatct']['last_name'] = 'User';
1019 $pluggable_data['conatct']['company_id'] = 112;
1020 $pluggable_data['conatct']['email'] = 'testuser@gmail.com';
1021 $pluggable_data['conatct']['address_line_1'] = '33, Vincent Road';
1022 $pluggable_data['conatct']['address_line_2'] = 'Chicago Street';
1023 $pluggable_data['conatct']['postal_code'] = '212342';
1024 $pluggable_data['conatct']['city'] = 'New York City';
1025 $pluggable_data['conatct']['state'] = 'New York';
1026 $pluggable_data['conatct']['country'] = 'USA';
1027 $pluggable_data['conatct']['phone'] = '9992191911';
1028 $pluggable_data['conatct']['status'] = 'subscribed';
1029 $pluggable_data['conatct']['contact_type'] = 'lead';
1030 $pluggable_data['conatct']['source'] = '';
1031 $pluggable_data['conatct']['date_of_birth'] = '2022-11-09';
1032 $context['tag_id'] = 1;
1033 $pluggable_data['tag'] =
1034 [
1035 'id' => '1',
1036 'title' => 'new',
1037 'slug' => 'new',
1038 'description' => null,
1039 'created_at' => '2023-01-19 10:23:23',
1040 'updated_at' => '2023-01-19 10:23:23',
1041 'pivot' => [
1042 'subscriber_id' => '1',
1043 'object_id' => '1',
1044 'object_type' => 'FluentCrm\\App\\Models\\Tag',
1045 'created_at' => '2023-01-19 10:42:55',
1046 'updated_at' => '2023-01-19 10:42:55',
1047
1048 ],
1049 ];
1050 $context['response_type'] = 'sample';
1051 }
1052
1053 $context['pluggable_data'] = $pluggable_data;
1054 return $context;
1055 }
1056
1057 /**
1058 * Get FluentCRM contact details.
1059 *
1060 * @param array $data data.
1061 *
1062 * @return array
1063 */
1064 public function search_pluggables_fluentcrm_contact_added( $data ) {
1065 $context = [];
1066 $pluggable_data = [];
1067
1068 if ( ! class_exists( 'FluentCrm\App\Models\Subscriber' ) || ! class_exists( 'FluentCrm\App\Models\SubscriberMeta' ) ) {
1069 return [];
1070 }
1071 $contact = [];
1072 if ( 'status_set_to_specific_status' === $data['search_term'] ) {
1073 if ( '-1' === $data['filter']['status']['value'] ) {
1074 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1075 } else {
1076 $contact = Subscriber::where( 'status', $data['filter']['status']['value'] )->first();
1077 }
1078 } elseif ( 'new_contact_added' === $data['search_term'] ) {
1079 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1080 } elseif ( 'contact_updated' === $data['search_term'] ) {
1081 $contact = Subscriber::orderBy( 'updated_at', 'DESC' )->first();
1082 } elseif ( 'contact_field_updated' === $data['search_term'] ) {
1083 if ( '-1' === $data['filter']['field_id']['value'] ) {
1084 $contact = SubscriberMeta::where( 'object_type', 'custom_field' )->orderBy( 'updated_at', 'DESC' )->first();
1085 } else {
1086 $contact = SubscriberMeta::where( 'key', $data['filter']['field_id']['value'] )->orderBy( 'updated_at', 'DESC' )->first();
1087 }
1088 $contact = Subscriber::where( 'id', $contact->subscriber_id )->first();
1089 }
1090
1091 if ( $contact ) {
1092 $subscriber = Subscriber::with( [ 'tags', 'lists' ] )->find( $contact->id );
1093 $customer_fields = $subscriber->custom_fields();
1094 $pluggable_data['contact']['details'] = $subscriber;
1095 $pluggable_data['contact']['custom'] = $customer_fields;
1096 $pluggable_data['field_id'] = $data['filter']['field_id']['value'];
1097 $context['response_type'] = 'live';
1098 } else {
1099 $pluggable_data['contact']['details']['full_name'] = 'Test User';
1100 $pluggable_data['contact']['details']['first_name'] = 'Test';
1101 $pluggable_data['contact']['details']['last_name'] = 'User';
1102 $pluggable_data['contact']['details']['company_id'] = 112;
1103 $pluggable_data['contact']['details']['email'] = 'testuser@gmail.com';
1104 $pluggable_data['contact']['details']['address_line_1'] = '33, Vincent Road';
1105 $pluggable_data['contact']['details']['address_line_2'] = 'Chicago Street';
1106 $pluggable_data['contact']['details']['postal_code'] = '212342';
1107 $pluggable_data['contact']['details']['city'] = 'New York City';
1108 $pluggable_data['contact']['details']['state'] = 'New York';
1109 $pluggable_data['contact']['details']['country'] = 'USA';
1110 $pluggable_data['contact']['details']['phone'] = '9992191911';
1111 $pluggable_data['contact']['details']['status'] = 'subscribed';
1112 $pluggable_data['contact']['details']['contact_type'] = 'lead';
1113 $pluggable_data['contact']['details']['source'] = '';
1114 $pluggable_data['contact']['details']['date_of_birth'] = '2022-11-09';
1115 $context['response_type'] = 'sample';
1116 }
1117
1118 $context['pluggable_data'] = $pluggable_data;
1119 return $context;
1120 }
1121
1122 /**
1123 * Get contact added to list details.
1124 *
1125 * @param array $data data.
1126 *
1127 * @return array
1128 */
1129 public function search_pluggables_fluentcrm_contact_added_to_lists( $data ) {
1130 $context = [];
1131 $pluggable_data = [];
1132 $list_id = $data['filter'];
1133
1134 if ( ! class_exists( 'FluentCrm\App\Models\Lists' ) || ! class_exists( 'FluentCrm\App\Models\Subscriber' ) ) {
1135 return [];
1136 }
1137
1138 $contact_api = FluentCrmApi( 'contacts' );
1139 if ( -1 === $list_id ) {
1140 $lists = Lists::orderBy( 'id', 'DESC' )->first();
1141 $list_id = $lists->id;
1142 } else {
1143 $lists = Lists::where( 'id', $list_id )->first();
1144 }
1145 $list_ids = [ $list_id ];
1146 $list_contacts = $contact_api->getInstance()
1147 ->filterByLists( $list_ids )
1148 ->orderBy( 'id', 'DESC' )
1149 ->first();
1150
1151 $contact = Subscriber::where( 'id', $list_contacts->id )->get();
1152 if ( $contact ) {
1153 $pluggable_data['contact'] = $contact[0];
1154 $pluggable_data['list_id'] = $list_id;
1155 $pluggable_data['list'] = $lists;
1156 $context['response_type'] = 'live';
1157 } else {
1158 $pluggable_data['conatct']['id'] = 6;
1159 $pluggable_data['conatct']['prefix'] = 'Mr';
1160 $pluggable_data['conatct']['full_name'] = 'John Doe';
1161 $pluggable_data['conatct']['first_name'] = 'John';
1162 $pluggable_data['conatct']['last_name'] = 'Doe';
1163 $pluggable_data['conatct']['company_id'] = 112;
1164 $pluggable_data['conatct']['email'] = 'johnde@gmail.com';
1165 $pluggable_data['conatct']['address_line_1'] = '33, Vincent Road';
1166 $pluggable_data['conatct']['address_line_2'] = 'Chicago Street';
1167 $pluggable_data['conatct']['postal_code'] = '212342';
1168 $pluggable_data['conatct']['city'] = 'New York City';
1169 $pluggable_data['conatct']['state'] = 'New York';
1170 $pluggable_data['conatct']['country'] = 'USA';
1171 $pluggable_data['conatct']['phone'] = '9992191911';
1172 $pluggable_data['conatct']['status'] = 'subscribed';
1173 $pluggable_data['conatct']['contact_type'] = 'lead';
1174 $pluggable_data['conatct']['source'] = '';
1175 $pluggable_data['conatct']['date_of_birth'] = '2022-11-09';
1176 $context['list_id'] = 1;
1177 $pluggable_data['list'] =
1178 [
1179 'id' => '1',
1180 'title' => 'new',
1181 'slug' => 'new',
1182 'description' => null,
1183 'created_at' => '2023-01-19 10:23:23',
1184 'updated_at' => '2023-01-19 10:23:23',
1185 ];
1186 $context['response_type'] = 'sample';
1187 }
1188
1189 $context['pluggable_data'] = $pluggable_data;
1190 return $context;
1191 }
1192
1193 /**
1194 * Prepare fluentcrm campaigns.
1195 *
1196 * @param array $data Search Params.
1197 *
1198 * @return array
1199 */
1200 public function search_fluentcrm_campaigns( $data ) {
1201
1202 $options = [];
1203 global $wpdb;
1204
1205 $campaigns = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fc_campaigns ORDER BY id DESC", ARRAY_A );
1206
1207 if ( ! empty( $campaigns ) ) {
1208 foreach ( $campaigns as $campaign ) {
1209 $options[] = [
1210 'label' => $campaign['title'],
1211 'value' => $campaign['id'],
1212 ];
1213 }
1214 }
1215
1216 return [
1217 'options' => $options,
1218 'hasMore' => false,
1219 ];
1220 }
1221
1222 /**
1223 * Get Divi Forms.
1224 *
1225 * @param array $data data.
1226 *
1227 * @return array
1228 */
1229 public function search_divi_forms( $data ) {
1230 $form_posts = Utilities::get_divi_forms();
1231 $options = [];
1232
1233 if ( empty( $form_posts ) ) {
1234 return [
1235 'options' => [],
1236 'hasMore' => false,
1237 ];
1238 }
1239
1240 foreach ( $form_posts as $form_post ) {
1241 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
1242 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
1243 if ( empty( $forms ) ) {
1244 continue;
1245 }
1246
1247 $count = 0;
1248
1249 foreach ( $forms as $form ) {
1250 $pattern_form = get_shortcode_regex( [ 'et_pb_contact_form' ] );
1251 preg_match_all( "/$pattern_form/", $form[0], $forms_extracted, PREG_SET_ORDER );
1252
1253 if ( empty( $forms_extracted ) ) {
1254 continue;
1255 }
1256
1257 foreach ( $forms_extracted as $form_extracted ) {
1258 $form_attrs = shortcode_parse_atts( $form_extracted[3] );
1259 $form_id = isset( $form_attrs['_unique_id'] ) ? $form_attrs['_unique_id'] : '';
1260 if ( empty( $form_id ) ) {
1261 continue;
1262 }
1263 $form_id = sprintf( '%d-%s', $form_post['ID'], $form_id );
1264 $form_title = isset( $form_attrs['title'] ) ? $form_attrs['title'] : '';
1265 $form_title = sprintf( '%s %s', $form_post['post_title'], $form_title );
1266 $options[] = [
1267 'label' => $form_title,
1268 'value' => $form_id,
1269 ];
1270 }
1271 $count++;
1272 }
1273 }
1274
1275 return [
1276 'options' => $options,
1277 'hasMore' => false,
1278 ];
1279 }
1280
1281 /**
1282 * Get Comment Pluggable data.
1283 *
1284 * @param array $data data.
1285 *
1286 * @return array
1287 */
1288 public function search_pluggables_wp_insert_comment( $data ) {
1289 $context = [];
1290 $post_data = [];
1291 $args = [
1292 'number' => '1',
1293 'status' => 'approve',
1294 'post_type' => $data['filter']['post_type']['value'],
1295 ];
1296
1297 if ( isset( $data['filter']['post']['value'] ) ) {
1298 $post_id = $data['filter']['post']['value'];
1299 if ( $post_id > 0 ) {
1300 $args['post_id'] = $post_id;
1301 }
1302 }
1303
1304 $comments = get_comments( $args );
1305 if ( empty( $comments ) ) {
1306 unset( $args['post_id'] );
1307 $comments = get_comments( $args );
1308 }
1309 $context['context_data'] = $data;
1310 $context['context_args'] = $args;
1311 if ( ! empty( $comments ) ) {
1312 foreach ( $comments as $comment ) :
1313 if ( is_object( $comment ) ) {
1314 $comment = get_object_vars( $comment );
1315 }
1316 if ( is_array( $comment ) && isset( $comment['comment_post_ID'] ) ) {
1317 $post = get_post( absint( $comment['comment_post_ID'] ) );
1318 if ( is_object( $post ) ) {
1319 if ( property_exists( $post, 'ID' ) || property_exists( $post, 'post_author' ) || property_exists( $post, 'post_title' ) ) {
1320 $post_id = $post->ID;
1321 $postauthor = (int) $post->post_author;
1322 if ( is_array( $comment ) ) {
1323 $context['pluggable_data'] = [
1324 'post' => $post_id,
1325 'post_title' => $post->post_title,
1326 'post_author' => get_the_author_meta( 'display_name', $postauthor ),
1327 'post_link' => get_the_permalink( $post_id ),
1328 'comment_id' => $comment['comment_ID'],
1329 'comment' => $comment['comment_content'],
1330 'comment_author' => $comment['comment_author'],
1331 'comment_author_email' => $comment['comment_author_email'],
1332 'comment_date' => $comment['comment_date'],
1333 ];
1334 }
1335 }
1336 }
1337 }
1338 if ( is_array( $comment ) && isset( $comment['comment_author_email'] ) ) {
1339 $user_email = $comment['comment_author_email'];
1340 /**
1341 *
1342 * Ignore line
1343 *
1344 * @phpstan-ignore-next-line
1345 */
1346 $user = get_user_by( 'email', $user_email );
1347 if ( $user ) {
1348 $context['pluggable_data']['wp_user_id'] = $user->ID;
1349 $context['pluggable_data']['user_login'] = $user->user_login;
1350 $context['pluggable_data']['display_name'] = $user->display_name;
1351 $context['pluggable_data']['user_firstname'] = $user->user_firstname;
1352 $context['pluggable_data']['user_lastname'] = $user->user_lastname;
1353 $context['pluggable_data']['user_email'] = $user->user_email;
1354 $context['pluggable_data']['user_role'] = $user->roles;
1355 }
1356 }
1357
1358 $context['response_type'] = 'live';
1359 endforeach;
1360 } else {
1361 $sample_comment = [
1362 'post' => 100,
1363 'post_title' => 'Sample Post',
1364 'comment_id' => 101,
1365 'comment' => 'Sample Comment',
1366 ];
1367 $sample_comment['wp_user_id'] = 7;
1368 $sample_comment['user_login'] = 'testuser@gmail.com';
1369 $sample_comment['display_name'] = 'Test User';
1370 $sample_comment['user_firstname'] = 'Test';
1371 $sample_comment['user_lastname'] = 'User';
1372 $sample_comment['user_email'] = 'testuser@gmail.com';
1373 $sample_comment['user_role'] = [ 'Subscriber' ];
1374
1375 $context['pluggable_data'] = $sample_comment;
1376 $context['response_type'] = 'sample';
1377 }
1378 return $context;
1379 }
1380
1381 /**
1382 * User reset password.
1383 *
1384 * @param array $data data.
1385 * @return array
1386 */
1387 public function search_pluggables_user_reset_password( $data ) {
1388 $user_context = $this->search_pluggables_add_user_role( $data );
1389 $user_context['pluggable_data']['new_password'] = '***password***';
1390 return $user_context;
1391 }
1392
1393 /**
1394 * User pluggable data.
1395 *
1396 * @param array $data data.
1397 * @return array
1398 */
1399 public function search_pluggables_add_user_role( $data ) {
1400 $context = [];
1401 $args = [
1402 'order' => 'DESC',
1403 'number' => 1,
1404 'orderby' => 'ID',
1405 ];
1406
1407 if ( isset( $data['filter']['role']['value'] ) ) {
1408 $role = $data['filter']['role']['value'];
1409 $args['role'] = $role;
1410 }
1411 if ( isset( $data['filter']['new_role']['value'] ) ) {
1412 $role = $data['filter']['new_role']['value'];
1413 $args['role'] = $role;
1414 }
1415
1416 $users = get_users( $args );
1417
1418 if ( isset( $data['filter']['meta_key']['value'] ) ) {
1419 $meta_key = $data['filter']['meta_key']['value'];
1420 $args['st_meta_key'] = $meta_key;
1421 }
1422
1423 if ( isset( $data['filter']['profile_field']['value'] ) ) {
1424 $meta_key = $data['filter']['profile_field']['value'];
1425 $args['profile_field'] = $meta_key;
1426 }
1427
1428 if ( ! empty( $users ) ) {
1429 $user = $users[0];
1430 $pluggable_data = [];
1431 $pluggable_data['wp_user_id'] = $user->ID;
1432 $pluggable_data['user_login'] = $user->user_login;
1433 $pluggable_data['display_name'] = $user->display_name;
1434 $pluggable_data['user_firstname'] = $user->user_firstname;
1435 $pluggable_data['user_lastname'] = $user->user_lastname;
1436 $pluggable_data['user_email'] = $user->user_email;
1437 $pluggable_data['user_registered'] = $user->user_registered;
1438 $pluggable_data['user_role'] = $user->roles;
1439 if ( isset( $args['st_meta_key'] ) ) {
1440 $pluggable_data['meta_key'] = $args['st_meta_key'];
1441 $pluggable_data['meta_value'] = get_user_meta( $user->ID, $args['st_meta_key'], true );
1442 }
1443 if ( isset( $args['profile_field'] ) ) {
1444 $userdata = get_userdata( $user->ID );
1445 $userdata = json_decode( wp_json_encode( $userdata->data ), true );
1446
1447 $pluggable_data['profile_field'] = $args['profile_field'];
1448 $pluggable_data['profile_field_value'] = $userdata[ $args['profile_field'] ];
1449 }
1450 $context['pluggable_data'] = $pluggable_data;
1451 $context['response_type'] = 'live';
1452 } else {
1453 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
1454 $context['pluggable_data'] = [
1455 'wp_user_id' => 1,
1456 'user_login' => 'admin',
1457 'display_name' => 'Test User',
1458 'user_firstname' => 'Test',
1459 'user_lastname' => 'User',
1460 'user_email' => 'testuser@gmail.com',
1461 'user_registered' => '2024-06-18 09:47:58',
1462 'user_role' => [ $role ],
1463 ];
1464 if ( isset( $args['st_meta_key'] ) ) {
1465 $context['pluggable_data']['meta_key'] = $args['st_meta_key'];
1466 $context['pluggable_data']['meta_value'] = 'test meta value';
1467 }
1468 if ( isset( $args['profile_field'] ) ) {
1469 $context['pluggable_data']['profile_field'] = $args['profile_field'];
1470 $context['pluggable_data']['profile_field_value'] = 'Profile Field Value';
1471 }
1472 $context['response_type'] = 'sample';
1473 }
1474 return $context;
1475 }
1476
1477 /**
1478 * User pluggable data.
1479 *
1480 * @param array $data data.
1481 * @return array
1482 */
1483 public function search_pluggables_last_user_login( $data ) {
1484 $context = [];
1485 $args = [
1486 'orderby' => 'meta_value',
1487 'meta_key' => 'st_last_login',
1488 'order' => 'DESC',
1489 'number' => 1,
1490 ];
1491 $users = get_users( $args );
1492
1493 if ( ! empty( $users ) ) {
1494 $user = $users[0];
1495 $pluggable_data = [];
1496 $pluggable_data['wp_user_id'] = $user->ID;
1497 $pluggable_data['user_login'] = $user->user_login;
1498 $pluggable_data['display_name'] = $user->display_name;
1499 $pluggable_data['user_firstname'] = $user->user_firstname;
1500 $pluggable_data['user_lastname'] = $user->user_lastname;
1501 $pluggable_data['user_email'] = $user->user_email;
1502 $pluggable_data['user_role'] = $user->roles;
1503
1504 $context['pluggable_data'] = $pluggable_data;
1505 $context['response_type'] = 'live';
1506 } else {
1507 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
1508 $context['pluggable_data'] = [
1509 'wp_user_id' => 1,
1510 'user_login' => 'admin',
1511 'display_name' => 'Test User',
1512 'user_firstname' => 'Test',
1513 'user_lastname' => 'User',
1514 'user_email' => 'testuser@gmail.com',
1515 'user_role' => [ $role ],
1516 ];
1517 $context['response_type'] = 'sample';
1518 }
1519 return $context;
1520 }
1521
1522 /**
1523 * Donation pluggable data.
1524 *
1525 * @param array $data data.
1526 * @return array
1527 */
1528 public function search_pluggables_wordpress_post( $data ) {
1529 $context = [];
1530 $args = [
1531 'post_type' => 'any',
1532 'posts_per_page' => 1,
1533 'orderby' => 'modified',
1534 'order' => 'DESC',
1535 ];
1536
1537 if ( isset( $data['filter']['post_type']['value'] ) ) {
1538 $post_type = $data['filter']['post_type']['value'];
1539 $args['post_type'] = $post_type;
1540 }
1541
1542 if ( isset( $data['filter']['status']['value'] ) ) {
1543 $post_status = $data['filter']['status']['value'];
1544 $args['post_status'] = $post_status;
1545 }
1546
1547 if ( isset( $data['filter']['post_status']['value'] ) ) {
1548 $post_status = $data['filter']['post_status']['value'];
1549 $args['post_status'] = $post_status;
1550 }
1551
1552 if ( isset( $data['filter']['post']['value'] ) ) {
1553 $post_id = $data['filter']['post']['value'];
1554 if ( $post_id > 0 ) {
1555 $args['p'] = $post_id;
1556 unset( $args['post_status'] );
1557 }
1558 }
1559
1560 $posts = get_posts( $args );
1561 if ( ! empty( $posts ) ) {
1562 $context['pluggable_data'] = $posts[0];
1563 $custom_metas = get_post_meta( $posts[0]->ID );
1564 if ( property_exists( $context['pluggable_data'], 'post' ) ) {
1565 $context['pluggable_data']->post = $posts[0]->ID;
1566 }
1567 if ( is_object( $context['pluggable_data'] ) ) {
1568 $context['pluggable_data'] = get_object_vars( $context['pluggable_data'] );
1569 }
1570 if ( $posts[0] instanceof WP_Post ) {
1571 $taxonomies = get_object_taxonomies( get_post( $posts[0] ), 'objects' );
1572 if ( ! empty( $taxonomies ) ) {
1573 foreach ( $taxonomies as $taxonomy => $taxonomy_object ) {
1574 $terms = get_the_terms( $posts[0]->ID, $taxonomy );
1575 if ( ! empty( $terms ) && is_array( $terms ) ) {
1576 foreach ( $terms as $term ) {
1577 $context['pluggable_data'][ $taxonomy ] = $term->name;
1578 }
1579 }
1580 }
1581 }
1582 }
1583 $context['pluggable_data'] = array_merge( $context['pluggable_data'], WordPress::get_user_context( $posts[0]->post_author ) );
1584 $context['pluggable_data']['post'] = $posts[0]->ID;
1585 $context['pluggable_data']['custom_metas'] = $custom_metas;
1586 $context['response_type'] = 'live';
1587 } else {
1588 $context['pluggable_data'] = [
1589 'ID' => 557,
1590 'post' => 557,
1591 'post_author' => 1,
1592 'post_date' => '2022-11-18 12:18:14',
1593 'post_date_gmt' => '2022-11-18 12:18:14',
1594 'post_content' => 'Test Post Content',
1595 'post_title' => 'Test Post',
1596 'post_excerpt' => '',
1597 'post_status' => $args['post_status'],
1598 'comment_status' => 'open',
1599 'ping_status' => 'open',
1600 'post_password' => '',
1601 'post_name' => 'test-post',
1602 'to_ping' => '',
1603 'pinged' => '',
1604 'post_modified' => '2022-11-18 12:18:14',
1605 'post_modified_gmt' => '2022-11-18 12:18:14',
1606 'post_content_filtered' => '',
1607 'post_parent' => 0,
1608 'guid' => 'https://abc.com/test-post/',
1609 'menu_order' => 0,
1610 'post_type' => 'post',
1611 'post_mime_type' => '',
1612 'comment_count' => 0,
1613 'filter' => 'raw',
1614 ];
1615 $context['response_type'] = 'sample';
1616 }
1617
1618 return $context;
1619 }
1620
1621 /**
1622 * Donation pluggable data
1623 *
1624 * @param array $data data.
1625 * @return array
1626 */
1627 public function search_pluggables_givewp_donation_via_form( $data ) {
1628 global $wpdb;
1629 $context = [];
1630 $pluggable_data = [];
1631 $form_id = $data['filter']['form_id']['value'];
1632 $subscription_id = 1;
1633
1634 if ( ! class_exists( 'Give_Payment' ) || ! class_exists( 'Give_Subscription' ) ) {
1635 return [];
1636 }
1637
1638 if ( ! function_exists( 'give_get_donor_donation_comment' ) ) {
1639 return [];
1640 }
1641
1642 if ( 'donation_specific_field_value' == $data['search_term'] ) {
1643 $field_id = $data['filter']['field_id']['value'];
1644 $field_value = $data['filter']['field_value']['value'];
1645
1646 $donation_meta = $wpdb->get_row(
1647 $wpdb->prepare(
1648 "SELECT * FROM {$wpdb->prefix}give_donationmeta
1649 WHERE meta_key LIKE %s AND
1650 meta_value LIKE %s
1651 ORDER BY donation_id
1652 DESC LIMIT 1",
1653 $field_id,
1654 $field_value
1655 )
1656 );
1657 if ( $donation_meta ) {
1658 $donation_form_meta = $wpdb->get_row(
1659 $wpdb->prepare(
1660 "SELECT * FROM {$wpdb->prefix}give_donationmeta
1661 WHERE donation_id = %d AND
1662 meta_key LIKE %s AND
1663 meta_value LIKE %d
1664 ORDER BY donation_id
1665 DESC LIMIT 1",
1666 $donation_meta->donation_id,
1667 '_give_payment_form_id',
1668 $form_id
1669 )
1670 );
1671
1672 $payment = $wpdb->get_row(
1673 $wpdb->prepare(
1674 "SELECT * FROM {$wpdb->prefix}posts WHERE
1675 ID=%d ORDER BY ID DESC LIMIT 1",
1676 $donation_form_meta->donation_id
1677 )
1678 );
1679 }
1680 } elseif ( 'donation_specific_amount' == $data['search_term'] ) {
1681 $condition_compare = $data['filter']['condition_compare']['value'];
1682 $donation_amount = $data['filter']['donation_amount']['value'];
1683
1684 $donation_meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_donationmeta WHERE meta_key LIKE %s AND meta_value $condition_compare %d ORDER BY donation_id DESC LIMIT 1", '_give_payment_total',$donation_amount ) ); //phpcs:ignore
1685
1686 if ( $donation_meta ) {
1687 $donation_form_meta = $wpdb->get_row(
1688 $wpdb->prepare(
1689 "SELECT * FROM {$wpdb->prefix}give_donationmeta
1690 WHERE donation_id = %d AND
1691 meta_key LIKE %s AND
1692 meta_value LIKE %d
1693 ORDER BY donation_id
1694 DESC LIMIT 1",
1695 $donation_meta->donation_id,
1696 '_give_payment_form_id',
1697 $form_id
1698 )
1699 );
1700
1701 $payment = $wpdb->get_row(
1702 $wpdb->prepare(
1703 "SELECT * FROM {$wpdb->prefix}posts WHERE
1704 ID=%d ORDER BY ID DESC LIMIT 1",
1705 $donation_form_meta->donation_id
1706 )
1707 );
1708 }
1709 } elseif ( 'cancels_recurring_donation' == $data['search_term'] ) {
1710 $donation_meta = $wpdb->get_row(
1711 $wpdb->prepare(
1712 "SELECT * FROM {$wpdb->prefix}give_subscriptions
1713 WHERE product_id=%d AND
1714 status LIKE %s
1715 ORDER BY id
1716 DESC LIMIT 1",
1717 $form_id,
1718 'cancelled'
1719 )
1720 );
1721 if ( $donation_meta ) {
1722 $subscription_id = $donation_meta->id;
1723 $payment = $wpdb->get_row(
1724 $wpdb->prepare(
1725 "SELECT * FROM {$wpdb->prefix}posts WHERE
1726 ID=%d ORDER BY ID DESC LIMIT 1",
1727 $donation_meta->parent_payment_id
1728 )
1729 );
1730 }
1731 } elseif ( 'continues_recurring_donation' == $data['search_term'] ) {
1732 $donation_meta = $wpdb->get_row(
1733 $wpdb->prepare(
1734 "SELECT * FROM {$wpdb->prefix}give_subscriptions
1735 WHERE product_id=%d AND
1736 status LIKE %s
1737 ORDER BY id
1738 DESC LIMIT 1",
1739 $form_id,
1740 'active'
1741 )
1742 );
1743 if ( $donation_meta ) {
1744 $subscription_id = $donation_meta->id;
1745
1746 $payment = $wpdb->get_row(
1747 $wpdb->prepare(
1748 "SELECT * FROM {$wpdb->prefix}posts
1749 WHERE post_parent=%d AND post_status LIKE %s
1750 ORDER BY ID DESC LIMIT 1",
1751 $donation_meta->parent_payment_id,
1752 'give_subscription'
1753 )
1754 );
1755 }
1756 } else {
1757 $payment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type=%s ORDER BY id DESC LIMIT 1", 'give_payment' ) );
1758 }
1759
1760 if ( ! empty( $payment ) ) {
1761 if ( 'continues_recurring_donation' == $data['search_term'] || 'cancels_recurring_donation' == $data['search_term'] ) {
1762 $subscription = new Give_Subscription( $subscription_id );
1763
1764 $pluggable_data['form_id'] = $form_id;
1765 $pluggable_data['subscription'] = $subscription;
1766 } elseif ( 'donation_specific_field_value' == $data['search_term'] ) {
1767 $payment_data = new Give_Payment( $payment->ID );
1768 $input_array = $payment_data->payment_meta;
1769 unset( $input_array['user_info'] );
1770 $pluggable_data = $input_array;
1771 foreach ( $input_array as $key => $value ) {
1772 $pluggable_data['field_id'] = $key;
1773 $pluggable_data['field_value'] = $value;
1774 }
1775 } else {
1776 $payment = new Give_Payment( $payment->ID );
1777 $address_data = $payment->address;
1778
1779 $pluggable_data['first_name'] = $payment->first_name;
1780 $pluggable_data['last_name'] = $payment->last_name;
1781 $pluggable_data['email'] = $payment->email;
1782 $pluggable_data['currency'] = $payment->currency;
1783 $pluggable_data['donated_amount'] = $payment->subtotal;
1784 $pluggable_data['donation_amount'] = $payment->subtotal;
1785 $pluggable_data['form_id'] = (int) $payment->form_id;
1786 $pluggable_data['form_title'] = $payment->form_title;
1787 $pluggable_data['name_title_prefix'] = $payment->title_prefix;
1788 $pluggable_data['date'] = $payment->date;
1789
1790 if ( is_array( $address_data ) ) {
1791 $pluggable_data['address_line_1'] = $address_data['line1'];
1792 $pluggable_data['address_line_2'] = $address_data['line2'];
1793 $pluggable_data['city'] = $address_data['city'];
1794 $pluggable_data['state'] = $address_data['state'];
1795 $pluggable_data['zip'] = $address_data['zip'];
1796 $pluggable_data['country'] = $address_data['country'];
1797 }
1798 // Payment meta.
1799 $payment_meta = $payment->get_meta();
1800 if ( is_array( $payment_meta ) && isset( $payment_meta['user_info'] ) ) {
1801 unset( $payment_meta['user_info'] );
1802 }
1803 $pluggable_data['payment_meta'] = $payment_meta;
1804 $donor_comment = give_get_donor_donation_comment( $payment->ID, $payment->donor_id );
1805 $pluggable_data['comment'] = ( is_array( $donor_comment ) && isset( $donor_comment['comment_content'] ) ) ? $donor_comment : '';
1806 }
1807
1808 $context['response_type'] = 'live';
1809 } else {
1810 if ( 'continues_recurring_donation' == $data['search_term'] || 'cancels_recurring_donation' == $data['search_term'] ) {
1811 $pluggable_data['form_id'] = $form_id;
1812 $pluggable_data['subscription']['id'] = 3;
1813 $pluggable_data['subscription']['donor_id'] = 8;
1814 $pluggable_data['subscription']['period'] = 'month';
1815 $pluggable_data['subscription']['initial_amount'] = '25.0000000000';
1816 $pluggable_data['subscription']['recurring_amount'] = '25.0000000000';
1817 $pluggable_data['subscription']['recurring_fee_amount'] = '0.0000000000';
1818 $pluggable_data['subscription']['transaction_id'] = 'a228ec9c6357963d23079d7d6945dd61';
1819 $pluggable_data['subscription']['parent_payment_id'] = '7492';
1820 $pluggable_data['subscription']['created'] = '2024-01-23 11:12:11';
1821 $pluggable_data['subscription']['expiration'] = '2024-03-23 23:59:59';
1822 $pluggable_data['subscription']['status'] = 'cancelled';
1823 $pluggable_data['subscription']['donor']['id'] = 8;
1824 $pluggable_data['subscription']['donor']['purchase_count'] = '3';
1825 $pluggable_data['subscription']['donor']['purchase_value'] = '75.000000';
1826 $pluggable_data['subscription']['donor']['email'] = 'johndoee@yopmail.com';
1827 $pluggable_data['subscription']['donor']['name'] = 'John Doe';
1828 $pluggable_data['subscription']['donor']['payment_ids'] = '7487,7492,7499';
1829 $pluggable_data['subscription']['donor']['user_id'] = '131';
1830 $pluggable_data['subscription']['customer_id'] = '8';
1831 } elseif ( 'donation_specific_field_value' == $data['search_term'] ) {
1832 $pluggable_data['form_id'] = 23;
1833 $pluggable_data['form_title'] = 'Demo Donation';
1834 $pluggable_data['_give_donor_billing_first_name'] = 'John';
1835 $pluggable_data['_give_donor_billing_last_name'] = 'Doe';
1836 $pluggable_data['_give_payment_donor_email'] = 'johndoee@gmail.com';
1837 $pluggable_data['_give_payment_currency'] = 'USD';
1838 $pluggable_data['_give_payment_total'] = '100';
1839 $pluggable_data['name_title_prefix'] = 'Mr';
1840 $pluggable_data['date'] = '2022-11-07 16:06:05';
1841 $pluggable_data['_give_donor_billing_address1'] = '33, Vincent Road';
1842 $pluggable_data['_give_donor_billing_address2'] = 'Chicago Street';
1843 $pluggable_data['_give_donor_billing_city'] = 'New York City';
1844 $pluggable_data['_give_donor_billing_state'] = 'New York';
1845 $pluggable_data['_give_donor_billing_zip'] = '223131';
1846 $pluggable_data['_give_donor_billing_country'] = 'USA';
1847 $pluggable_data['_give_donation_comment'] = 'Demo Comment';
1848 $pluggable_data['field_id'] = 'last_name';
1849 $pluggable_data['field_value'] = 'Doe';
1850 } else {
1851 $pluggable_data['first_name'] = 'John';
1852 $pluggable_data['last_name'] = 'Doe';
1853 $pluggable_data['email'] = 'johndoee@gmail.com';
1854 $pluggable_data['currency'] = 'USD';
1855 $pluggable_data['donated_amount'] = 100;
1856 $pluggable_data['donation_amount'] = 100;
1857 $pluggable_data['form_id'] = 23;
1858 $pluggable_data['form_title'] = 'Demo Donation';
1859 $pluggable_data['name_title_prefix'] = 'Mr';
1860 $pluggable_data['date'] = '2022-11-07 16:06:05';
1861 $pluggable_data['address_line_1'] = '33, Vincent Road';
1862 $pluggable_data['address_line_2'] = 'Chicago Street';
1863 $pluggable_data['city'] = 'New York City';
1864 $pluggable_data['state'] = 'New York';
1865 $pluggable_data['zip'] = '223131';
1866 $pluggable_data['country'] = 'USA';
1867 $pluggable_data['comment'] = 'Demo Comment';
1868 }
1869 $context['response_type'] = 'sample';
1870 }
1871
1872 $context['pluggable_data'] = $pluggable_data;
1873 return $context;
1874 }
1875
1876 /**
1877 * Search Divi Form fields.
1878 *
1879 * @param array $data Search Params.
1880 *
1881 * @since 1.0.0
1882 *
1883 * @return array
1884 */
1885 public function search_pluggable_divi_form_fields( $data ) {
1886 $result = [];
1887 $form_id = absint( $data['dynamic'] );
1888 $form_posts = Utilities::get_divi_forms();
1889
1890 if ( empty( $form_posts ) ) {
1891 return [
1892 'options' => [],
1893 'hasMore' => false,
1894 ];
1895 }
1896 $fields = [];
1897 foreach ( $form_posts as $form_post ) {
1898 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
1899 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
1900 if ( empty( $forms ) ) {
1901 continue;
1902 }
1903
1904 $count = 0;
1905
1906 foreach ( $forms as $form ) {
1907 $pattern = get_shortcode_regex( [ 'et_pb_contact_field' ] );
1908
1909 preg_match_all( "/$pattern/", $form[0], $contact_fields, PREG_SET_ORDER );
1910
1911 if ( empty( $contact_fields ) ) {
1912 return $fields;
1913 }
1914
1915 foreach ( $contact_fields as $contact_field ) {
1916 $contact_field_attrs = shortcode_parse_atts( $contact_field[3] );
1917 $field_id = strtolower( self::array_get( $contact_field_attrs, 'field_id' ) );
1918 $fields[] = [
1919 'field_title' => self::array_get( $contact_field_attrs, 'field_title', __( 'No title', 'suretriggers' ) ),
1920 'field_id' => $field_id,
1921 ];
1922 }
1923 }
1924 }
1925
1926 if ( ! empty( $fields ) ) {
1927 foreach ( $fields as $field ) {
1928 $result[] = [
1929 'label' => $field['field_title'],
1930 'value' => '{' . $field['field_id'] . '}',
1931 ];
1932 }
1933 }
1934
1935 return [
1936 'options' => $result,
1937 'hasMore' => false,
1938 ];
1939 }
1940
1941 /**
1942 * Pseudo function copied from Divi
1943 *
1944 * @param array $array An array which contains value located at `$address`.
1945 * @param string|array $address The location of the value within `$array` (dot notation).
1946 * @param mixed $default Value to return if not found. Default is an empty string.
1947 *
1948 * @return mixed The value, if found, otherwise $default.
1949 */
1950 public static function array_get( $array, $address, $default = '' ) {
1951 $keys = is_array( $address ) ? $address : explode( '.', $address );
1952 $value = $array;
1953
1954 foreach ( $keys as $key ) {
1955 if ( ! empty( $key ) && isset( $key[0] ) && '[' === $key[0] ) {
1956 $index = substr( $key, 1, -1 );
1957
1958 if ( is_numeric( $index ) ) {
1959 $key = (int) $index;
1960 }
1961 }
1962
1963 if ( ! isset( $value[ $key ] ) ) {
1964 return $default;
1965 }
1966
1967 $value = $value[ $key ];
1968 }
1969
1970 return $value;
1971 }
1972
1973 /**
1974 * Get UAG Forms.
1975 *
1976 * @param array $data data.
1977 *
1978 * @return array
1979 */
1980 public function search_spectra_forms( $data ) {
1981 $form_posts = Utilities::get_uag_forms();
1982
1983 $options = [];
1984 if ( empty( $form_posts ) ) {
1985 return [
1986 'options' => [],
1987 'hasMore' => false,
1988 ];
1989 }
1990
1991 foreach ( $form_posts as $form_post ) {
1992 $blocks = parse_blocks( $form_post['post_content'] );
1993 $i = 1;
1994 // Get form blocks.
1995 $this->process_blocks( $blocks, $form_post, $options, $i );
1996 }
1997
1998 return [
1999 'options' => $options,
2000 'hasMore' => false,
2001 ];
2002 }
2003
2004 /**
2005 * Return forms in nested blocks in Spectra.
2006 *
2007 * @param array $blocks data.
2008 * @param array $form_post Form post.
2009 * @param array $options Options.
2010 * @param int $i Number.
2011 *
2012 * @return array
2013 */
2014 public function process_blocks( $blocks, $form_post, &$options, &$i ) {
2015 foreach ( $blocks as $block ) {
2016 if ( 'uagb/forms' === $block['blockName'] ) {
2017 $options[] = [
2018 'label' => $form_post['post_title'] . ' (Form ' . ( $i++ ) . ')',
2019 'value' => $block['attrs']['block_id'],
2020 ];
2021 } elseif ( isset( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) {
2022 $this->process_blocks( $block['innerBlocks'], $form_post, $options, $i );
2023 }
2024 }
2025 return $options;
2026 }
2027
2028 /**
2029 * Check array recursive.
2030 *
2031 * @param array $array Array.
2032 * @param string $value search params.
2033 * @since 1.0.0
2034 *
2035 * @return array|void
2036 */
2037 public static function get_column_by_value( $array, $value ) {
2038
2039 foreach ( $array as $key => $sub_array ) {
2040
2041 if ( is_array( $sub_array ) ) {
2042 $result = self::get_column_by_value( $sub_array, $value );
2043 if ( null !== $result ) {
2044 return $key;
2045 }
2046 } else {
2047 return $key;
2048 }
2049 }
2050 return null;
2051
2052 }
2053
2054
2055 /**
2056 * Search UAG Form fields.
2057 *
2058 * @param array $data Search Params.
2059 *
2060 * @since 1.0.0
2061 *
2062 * @return array
2063 */
2064 public function search_spectraform_fields( $data ) {
2065 $result = [];
2066 $form_id = absint( $data['dynamic'] );
2067 $form_posts = Utilities::get_uag_forms();
2068
2069 if ( empty( $form_posts ) ) {
2070 return [
2071 'options' => [],
2072 'hasMore' => false,
2073 ];
2074 }
2075
2076 foreach ( $form_posts as $form_post ) {
2077 $blocks = parse_blocks( $form_post['post_content'] );
2078
2079 foreach ( $blocks as $block ) {
2080 if ( (int) $block['attrs']['block_id'] === $form_id ) {
2081 $doc = new DOMDocument();
2082 $rendered_block = render_block( $block );
2083 $doc->loadHTML( $rendered_block );
2084 $child_node_list = $doc->getElementsByTagName( 'div' );
2085 for ( $i = 0; $i < $child_node_list->length; $i++ ) {
2086 $temp = $child_node_list->item( $i );
2087 if ( $temp && stripos( $temp->getAttribute( 'class' ), 'uagb-forms-input-label' ) !== false ) {
2088 $nodes[] = $temp;
2089 }
2090 }
2091
2092 foreach ( $nodes as $node ) {
2093 $result[] = [
2094 'label' => $node->textContent, //phpcs:ignore
2095 'value' => $node->textContent, //phpcs:ignore
2096 ];
2097 }
2098 }
2099 }
2100 }
2101
2102 return [
2103 'options' => $result,
2104 'hasMore' => false,
2105 ];
2106 }
2107
2108 /**
2109 * Search forms of MetForms.
2110 *
2111 * @param array $data data.
2112 * @return array
2113 */
2114 public function search_met_forms( $data ) {
2115 $args = [
2116 'post_type' => 'metform-form',
2117 'post_status' => 'publish',
2118 'numberposts' => -1,
2119 ];
2120
2121 $forms = get_posts( $args );
2122
2123 $options = [];
2124
2125 if ( ! empty( $forms ) ) {
2126 foreach ( $forms as $form ) {
2127 $options[] = [
2128 'label' => $form->post_title,
2129 'value' => $form->ID,
2130 ];
2131 }
2132 }
2133
2134 return [
2135 'options' => $options,
2136 'hasMore' => false,
2137 ];
2138 }
2139
2140 /**
2141 * Search forms of Ninja Forms.
2142 *
2143 * @param array $data data.
2144 * @return array
2145 */
2146 public function search_ninja_forms( $data ) {
2147 $options = [];
2148
2149 if ( function_exists( 'Ninja_Forms' ) ) {
2150 foreach ( Ninja_Forms()->form()->get_forms() as $form ) {
2151 $options[] = [
2152 'label' => $form->get_setting( 'title' ),
2153 'value' => $form->get_id(),
2154 ];
2155 }
2156 }
2157
2158 return [
2159 'options' => $options,
2160 'hasMore' => false,
2161 ];
2162 }
2163
2164 /**
2165 * Search forms of Pie Forms.
2166 *
2167 * @param array $data data.
2168 * @return array
2169 */
2170 public function search_pie_forms( $data ) {
2171 global $wpdb;
2172 $options = [];
2173
2174 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pf_forms' ) ) ) {
2175
2176 $results = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'pf_forms WHERE post_status = "published"' );
2177
2178 if ( $results ) {
2179 foreach ( $results as $result ) {
2180 $options[] = [
2181 'label' => $result->form_title,
2182 'value' => $result->id,
2183 ];
2184 }
2185 }
2186 }
2187
2188 return [
2189 'options' => $options,
2190 'hasMore' => false,
2191 ];
2192 }
2193
2194 /**
2195 * Get Fluent Forms.
2196 *
2197 * @param array $data data.
2198 *
2199 * @return array
2200 */
2201 public function search_fluent_forms( $data ) {
2202
2203 if ( ! function_exists( 'wpFluent' ) ) {
2204 return [
2205 'options' => [],
2206 'hasMore' => false,
2207 ];
2208 }
2209
2210 $forms = wpFluent()->table( 'fluentform_forms' )
2211 ->select( [ 'id', 'title' ] )
2212 ->orderBy( 'id', 'DESC' )
2213 ->get();
2214
2215 $options = [];
2216 if ( ! empty( $forms ) ) {
2217 foreach ( $forms as $form ) {
2218 $options[] = [
2219 'label' => $form->title,
2220 'value' => $form->id,
2221 ];
2222 }
2223 }
2224
2225 return [
2226 'options' => $options,
2227 'hasMore' => false,
2228 ];
2229
2230 }
2231
2232 /**
2233 * Get Fluent Forms Fields.
2234 *
2235 * @param array $data data.
2236 *
2237 * @return array
2238 */
2239 public function search_fluent_forms_form_fields( $data ) {
2240
2241 $options = [];
2242
2243 if ( ! function_exists( 'wpFluent' ) ) {
2244 return [];
2245 }
2246
2247 $form = wpFluent()->table( 'fluentform_forms' )->find( $data['dynamic'] );
2248 $field_data = json_decode( $form->form_fields, true );
2249 if ( is_array( $field_data ) && ! empty( $field_data['fields'] ) ) {
2250 foreach ( $field_data['fields'] as $field ) {
2251 if ( isset( $field['fields'] ) ) {
2252 foreach ( $field['fields'] as $field_key => $sub_field ) {
2253 if (
2254 isset( $sub_field['settings'] )
2255 && isset( $sub_field['settings']['label'] )
2256 && isset( $sub_field['settings']['visible'] )
2257 && true === $sub_field['settings']['visible']
2258 ) {
2259 $options[] = [
2260 'label' => $sub_field['settings']['label'],
2261 'value' => $field_key,
2262 ];
2263 }
2264 }
2265 } elseif ( isset( $field['element'] ) &&
2266 'container' === (string) $field['element'] &&
2267 isset( $field['columns'] ) && is_array( $field['columns'] ) ) {
2268 $container_fields = $field['columns'];
2269 foreach ( $container_fields as $c_fields ) {
2270 foreach ( $c_fields['fields'] as $field_key => $sub_field ) {
2271 if ( isset( $sub_field['settings'] ) ) {
2272 $options[] = [
2273 'label' => ( '' !== $sub_field['settings']['label'] ) ?
2274 $sub_field['settings']['label'] :
2275 $sub_field['attributes']['name'],
2276 'value' => isset( $sub_field['attributes']['name'] ) ?
2277 $sub_field['attributes']['name'] :
2278 strtolower( $sub_field['settings']['label'] ),
2279 ];
2280 }
2281 }
2282 }
2283 } elseif ( isset( $field['attributes'] ) && isset( $field['attributes']['name'] ) ) {
2284 if ( isset( $field['attributes']['placeholder'] ) && ! empty( $field['attributes']['placeholder'] ) ) {
2285 $options[] = [
2286 'label' => $field['attributes']['placeholder'],
2287 'value' => $field['attributes']['name'],
2288 ];
2289 } elseif ( isset( $field['settings'] ) &&
2290 isset( $field['settings']['label'] ) &&
2291 ! empty( $field['settings']['label'] ) ) {
2292 $options[] = [
2293 'label' => $field['settings']['label'],
2294 'value' => $field['attributes']['name'],
2295 ];
2296 }
2297 }
2298 }
2299 }
2300
2301 return [
2302 'options' => $options,
2303 'hasMore' => false,
2304 ];
2305
2306 }
2307
2308 /**
2309 * Get Fluent Forms.
2310 *
2311 * @param array $data data.
2312 *
2313 * @return array
2314 */
2315 public function search_bricks_builder_forms( $data ) {
2316 $bricks_theme = wp_get_theme( 'bricks' );
2317 if ( ! $bricks_theme->exists() ) {
2318 return [
2319 'options' => [],
2320 'hasMore' => false,
2321 ];
2322 }
2323
2324 $bricks_settings = (array) get_option( BRICKS_DB_GLOBAL_SETTINGS );
2325 if ( array_key_exists( 'postTypes', $bricks_settings ) ) {
2326 $bricks_posts = $bricks_settings['postTypes'];
2327 } else {
2328 $bricks_posts = [];
2329 }
2330 $bricks_posts[] = 'bricks_template';
2331
2332 $args = [
2333 'post_type' => $bricks_posts,
2334 'post_status' => 'publish',
2335 'posts_per_page' => -1,
2336 ];
2337
2338 $templates = get_posts( $args );
2339
2340 $options = [];
2341 if ( ! empty( $templates ) ) {
2342 foreach ( $templates as $template ) {
2343 $fetch_content = get_post_meta( $template->ID, BRICKS_DB_PAGE_CONTENT, true );
2344 if ( is_array( $fetch_content ) ) {
2345 foreach ( $fetch_content as $content ) {
2346 if ( 'form' === $content['name'] ) {
2347 $options[] = [
2348 'label' => $template->post_title . ' - ' . ( isset( $content['label'] ) ? $content['label'] : 'Form' ),
2349 'value' => $content['id'],
2350 ];
2351 }
2352 }
2353 }
2354 }
2355 }
2356
2357 return [
2358 'options' => $options,
2359 'hasMore' => false,
2360 ];
2361
2362 }
2363
2364 /**
2365 * Bricks builder form fields.
2366 *
2367 * @param array $data data.
2368 * @return array
2369 */
2370 public function search_pluggable_bricks_builder_form_fields( $data ) {
2371 $result = [];
2372 $fields = [];
2373 $form_id_str = $data['dynamic'];
2374 $ids = explode( '_', $form_id_str );
2375 $post_id = $ids[0];
2376 $form_id = $ids[1];
2377 $fetch_content = get_post_meta( $post_id, BRICKS_DB_PAGE_CONTENT, true );
2378 if ( is_array( $fetch_content ) ) {
2379 foreach ( $fetch_content as $content ) {
2380 if ( 'form' === $content['name'] && $form_id === $content['id'] ) {
2381 $fields = $content['settings']['fields'];
2382 break;
2383 }
2384 }
2385 }
2386
2387 if ( ! empty( $fields ) ) {
2388 foreach ( $fields as $field ) {
2389 $result[] = [
2390 'label' => $field['label'],
2391 'value' => '{' . strtolower( $field['label'] ) . '}',
2392 ];
2393 }
2394 }
2395
2396 return [
2397 'options' => $result,
2398 'hasMore' => false,
2399 ];
2400 }
2401
2402 /**
2403 * Get fluent form fields
2404 *
2405 * @param array $data Data array.
2406 *
2407 * @return array
2408 */
2409 public function search_pluggable_fluent_form_fields( $data ) {
2410 $result = [];
2411 $form_id = absint( $data['dynamic'] );
2412
2413 $fluentform_fields = Utilities::get_fluentform_fields( $data['search_term'], -1, $form_id );
2414
2415 if ( is_array( $fluentform_fields['results'] ) ) {
2416 foreach ( $fluentform_fields['results'] as $field ) {
2417 $result[] = [
2418 'label' => $field['text'],
2419 'value' => "{{$field['value']}}",
2420 ];
2421 }
2422 }
2423
2424 $result[] = [
2425 'value' => '{form_id}',
2426 'label' => 'Form ID',
2427 ];
2428
2429 $result[] = [
2430 'value' => '{form_title}',
2431 'label' => 'Form Title',
2432 ];
2433 $result[] = [
2434 'value' => '{entry_id}',
2435 'label' => 'Entry ID',
2436 ];
2437
2438 $result[] = [
2439 'value' => '{entry_source_url}',
2440 'label' => 'Entry Source URL',
2441 ];
2442
2443 $result[] = [
2444 'value' => '{submission_date}',
2445 'label' => 'Submission Date',
2446 ];
2447
2448 $result[] = [
2449 'value' => '{user_ip}',
2450 'label' => 'User IP',
2451 ];
2452
2453 return [
2454 'options' => $result,
2455 'hasMore' => false,
2456 ];
2457 }
2458
2459 /**
2460 * Search Gravity Form fields.
2461 *
2462 * @param array $data Search Params.
2463 *
2464 * @since 1.0.0
2465 */
2466 public function search_gform_fields( $data ) {
2467 if ( ! class_exists( 'RGFormsModel' ) ) {
2468 return [
2469 'options' => [],
2470 'hasMore' => false,
2471 ];
2472 }
2473 $result = [];
2474 $page = $data['page'];
2475 $form_id = absint( $data['dynamic'] );
2476
2477 $form = RGFormsModel::get_form_meta( $form_id );
2478
2479 if ( is_array( $form['fields'] ) ) {
2480 foreach ( $form['fields'] as $field ) {
2481 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
2482 foreach ( $field['inputs'] as $input ) {
2483 if ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
2484 $result[] = [
2485 'value' => $input['id'],
2486 'label' => GFCommon::get_label( $field, $input['id'] ),
2487 ];
2488 }
2489 }
2490 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
2491 $result[] = [
2492 'value' => (string) $field['id'],
2493 'label' => GFCommon::get_label( $field ),
2494 ];
2495 }
2496 }
2497 }
2498
2499 return [
2500 'options' => $result,
2501 'hasMore' => false,
2502 ];
2503
2504 }
2505
2506 /**
2507 * Search Gravity Form fields.
2508 *
2509 * @param array $data Search Params.
2510 *
2511 * @since 1.0.0
2512 */
2513 public function search_pluggable_gravity_form_fields( $data ) {
2514 if ( ! class_exists( 'RGFormsModel' ) ) {
2515 return [
2516 'options' => [],
2517 'hasMore' => false,
2518 ];
2519 }
2520 $result = [];
2521 $form_id = absint( $data['dynamic'] );
2522
2523 $form = RGFormsModel::get_form_meta( $form_id );
2524
2525 if ( is_array( $form['fields'] ) ) {
2526 foreach ( $form['fields'] as $field ) {
2527 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
2528 foreach ( $field['inputs'] as $input ) {
2529 if ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
2530 $result[] = [
2531 'value' => '{' . $input['id'] . '}',
2532 'label' => GFCommon::get_label( $field, $input['id'] ),
2533 ];
2534 }
2535 }
2536 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
2537 $result[] = [
2538 'value' => '{' . $field['id'] . '}',
2539 'label' => GFCommon::get_label( $field ),
2540 ];
2541 }
2542 }
2543 }
2544
2545 $result[] = [
2546 'value' => '{gravity_form}',
2547 'label' => 'Form ID',
2548 ];
2549 $result[] = [
2550 'value' => '{form_title}',
2551 'label' => 'Form Title',
2552 ];
2553 $result[] = [
2554 'value' => '{entry_id}',
2555 'label' => 'Entry ID',
2556 ];
2557 $result[] = [
2558 'value' => '{user_ip}',
2559 'label' => 'User IP',
2560 ];
2561 $result[] = [
2562 'value' => '{entry_source_url}',
2563 'label' => 'Entry Source URL',
2564 ];
2565 $result[] = [
2566 'value' => '{entry_submission_date}',
2567 'label' => 'Entry Submission Date',
2568 ];
2569
2570 return [
2571 'options' => $result,
2572 'hasMore' => false,
2573 ];
2574
2575 }
2576
2577 /**
2578 * Search Gravity Form fields for action.
2579 *
2580 * @param array $data data.
2581 * @return array
2582 */
2583 public function search_gravity_form_custom_fields( $data ) {
2584 $context = [];
2585 if ( ! class_exists( 'RGFormsModel' ) || ! class_exists( 'GFCommon' ) ) {
2586 return [];
2587 }
2588 $form_id = $data['filter'];
2589 $form = RGFormsModel::get_form_meta( $form_id );
2590 $custom_fields = [];
2591 if ( is_array( $form['fields'] ) ) {
2592 foreach ( $form['fields'] as $field ) {
2593 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
2594 foreach ( $field['inputs'] as $input ) {
2595 if ( ! $input['isHidden'] ) {
2596 $custom_fields[] = [
2597 'label' => GFCommon::get_label( $field, $input['id'] ),
2598 'value' => $input['id'],
2599 'type' => 'text',
2600 ];
2601 }
2602 }
2603 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
2604 $custom_fields[] = [
2605 'label' => GFCommon::get_label( $field ),
2606 'value' => $field['id'],
2607 'type' => 'text',
2608 ];
2609 }
2610 }
2611 }
2612 $context['fields'] = $custom_fields;
2613 return $context;
2614 }
2615
2616 /**
2617 * Get user register details via gravity forms.
2618 *
2619 * @param array $data data.
2620 *
2621 * @return array
2622 */
2623 public function search_pluggables_gravity_forms_user_register( $data ) {
2624 global $wpdb;
2625 $context = [];
2626 $pluggable_data = [];
2627
2628 $results = $wpdb->get_results(
2629 $wpdb->prepare(
2630 "SELECT id, form_id, date_created, ip, source_url, created_by FROM {$wpdb->prefix}gf_entry
2631 WHERE form_id = %d AND
2632 status = %s
2633 ORDER BY id
2634 DESC LIMIT 1",
2635 $data['filter']['gravity_form']['value'],
2636 'active'
2637 )
2638 );
2639
2640 if ( $results ) {
2641 $pluggable_data['gravity_form'] = (int) $results[0]->form_id;
2642 $pluggable_data['entry_id'] = $results[0]->id;
2643 $pluggable_data['user_ip'] = $results[0]->ip;
2644 $pluggable_data['entry_source_url'] = $results[0]->source_url;
2645 $pluggable_data['entry_submission_date'] = $results[0]->date_created;
2646 $pluggable_data['user'] = WordPress::get_user_context( $results[0]->created_by );
2647 $context['response_type'] = 'live';
2648 } else {
2649 $pluggable_data['gravity_form'] = '3';
2650 $pluggable_data['entry_id'] = '13';
2651 $pluggable_data['user_ip'] = '127.0.0.0';
2652 $pluggable_data['entry_source_url'] = 'https://example.com';
2653 $pluggable_data['entry_submission_date'] = '2024-02-05 09:41:59';
2654 $pluggable_data['user']['wp_user_id'] = '123';
2655 $pluggable_data['user']['user_login'] = 'johnd';
2656 $pluggable_data['user']['display_name'] = 'johnd';
2657 $pluggable_data['user']['user_firstname'] = 'John';
2658 $pluggable_data['user']['user_lastname'] = 'Doe';
2659 $pluggable_data['user']['user_email'] = 'johnd@yopmail.com';
2660 $pluggable_data['user']['user_role'] = [ 'subscriber' ];
2661 $context['response_type'] = 'sample';
2662 }
2663
2664 $context['pluggable_data'] = $pluggable_data;
2665 return $context;
2666 }
2667
2668 /**
2669 * Get payment form details for gravity forms.
2670 *
2671 * @param array $data data.
2672 *
2673 * @return array
2674 */
2675 public function search_pluggables_gravity_forms_payment( $data ) {
2676 global $wpdb;
2677 $context = [];
2678 $pluggable_data = [];
2679
2680 $results = $wpdb->get_results(
2681 $wpdb->prepare(
2682 "SELECT * FROM {$wpdb->prefix}gf_entry
2683 WHERE form_id = %d AND
2684 status = %s AND
2685 payment_status LIKE %s
2686 ORDER BY id
2687 DESC LIMIT 1",
2688 $data['filter']['gravity_form']['value'],
2689 'active',
2690 'Paid'
2691 )
2692 );
2693
2694 if ( $results ) {
2695 $pluggable_data['gravity_form'] = (int) $results[0]->form_id;
2696 $pluggable_data['entry_id'] = $results[0]->id;
2697 $pluggable_data['user_ip'] = $results[0]->ip;
2698 $pluggable_data['entry_source_url'] = $results[0]->source_url;
2699 $pluggable_data['entry_submission_date'] = $results[0]->date_created;
2700 $pluggable_data['payment_status'] = $results[0]->payment_status;
2701 $pluggable_data['payment_amount'] = $results[0]->payment_amount;
2702 $pluggable_data['currency'] = $results[0]->currency;
2703 $pluggable_data['payment_method'] = $results[0]->payment_method;
2704 $pluggable_data['transaction_id'] = $results[0]->transaction_id;
2705 $pluggable_data['user'] = WordPress::get_user_context( $results[0]->created_by );
2706 $context['response_type'] = 'live';
2707 } else {
2708 $pluggable_data['gravity_form'] = '3';
2709 $pluggable_data['entry_id'] = '13';
2710 $pluggable_data['user_ip'] = '127.0.0.0';
2711 $pluggable_data['entry_source_url'] = 'https://example.com';
2712 $pluggable_data['entry_submission_date'] = '2024-02-05 09:41:59';
2713 $pluggable_data['payment_status'] = 'Paid';
2714 $pluggable_data['payment_amount'] = '10.00';
2715 $pluggable_data['currency'] = 'USD';
2716 $pluggable_data['payment_method'] = 'visa';
2717 $pluggable_data['transaction_id'] = 'st_ooi98';
2718 $pluggable_data['user']['wp_user_id'] = '123';
2719 $pluggable_data['user']['user_login'] = 'johnd';
2720 $pluggable_data['user']['display_name'] = 'johnd';
2721 $pluggable_data['user']['user_firstname'] = 'John';
2722 $pluggable_data['user']['user_lastname'] = 'Doe';
2723 $pluggable_data['user']['user_email'] = 'johnd@yopmail.com';
2724 $pluggable_data['user']['user_role'] = [ 'subscriber' ];
2725 $context['response_type'] = 'sample';
2726 }
2727
2728 $context['pluggable_data'] = $pluggable_data;
2729 return $context;
2730 }
2731
2732 /**
2733 * Prepare fluentcrm lists.
2734 *
2735 * @param array $data Search Params.
2736 *
2737 * @return array[]
2738 */
2739 public function search_fluentcrm_lists( $data ) {
2740
2741 $list_api = FluentCrmApi( 'lists' );
2742 $all_lists = $list_api->all();
2743 $options = [];
2744
2745 if ( ! empty( $all_lists ) ) {
2746 foreach ( $all_lists as $list ) {
2747 $options[] = [
2748 'label' => $list->title,
2749 'value' => $list->id,
2750 ];
2751 }
2752 }
2753
2754 return [
2755 'options' => $options,
2756 'hasMore' => false,
2757 ];
2758 }
2759
2760 /**
2761 * Prepare fluentcrm contact status.
2762 *
2763 * @param array $data Search Params.
2764 *
2765 * @return array[]
2766 */
2767 public function search_fluentcrm_contact_status( $data ) {
2768
2769 $options = [
2770 [
2771 'label' => __( 'Subscribed', 'suretriggers' ),
2772 'value' => 'subscribed',
2773 ],
2774 [
2775 'label' => __( 'Pending', 'suretriggers' ),
2776 'value' => 'pending',
2777 ],
2778 [
2779 'label' => __( 'Unsubscribed', 'suretriggers' ),
2780 'value' => 'unsubscribed',
2781 ],
2782 [
2783 'label' => __( 'Bounced', 'suretriggers' ),
2784 'value' => 'bounced',
2785 ],
2786 [
2787 'label' => __( 'Complained', 'suretriggers' ),
2788 'value' => 'complained',
2789 ],
2790 ];
2791
2792 return [
2793 'options' => $options,
2794 'hasMore' => false,
2795 ];
2796 }
2797
2798 /**
2799 * Prepare fluentcrm contact status.
2800 *
2801 * @param array $data Search Params.
2802 *
2803 * @return array[]
2804 */
2805 public function search_fluentcrm_fetch_custom_fields( $data ) {
2806
2807 $options = [
2808 [
2809 'label' => __( 'Yes', 'suretriggers' ),
2810 'value' => 'true',
2811 ],
2812 [
2813 'label' => __( 'No', 'suretriggers' ),
2814 'value' => 'false',
2815 ],
2816 ];
2817
2818 return [
2819 'options' => $options,
2820 'hasMore' => false,
2821 ];
2822 }
2823
2824 /**
2825 * Prepare fluentcrm tags.
2826 *
2827 * @param array $data Search Params.
2828 *
2829 * @return array[]
2830 */
2831 public function search_fluentcrm_tags( $data ) {
2832
2833 if ( ! function_exists( 'FluentCrmApi' ) ) {
2834 return [];
2835 }
2836
2837 $tag_api = FluentCrmApi( 'tags' );
2838 $all_tags = $tag_api->all();
2839 $options = [];
2840
2841 if ( ! empty( $all_tags ) ) {
2842 foreach ( $all_tags as $tag ) {
2843 $options[] = [
2844 'label' => $tag->title,
2845 'value' => $tag->id,
2846 ];
2847 }
2848 }
2849
2850 return [
2851 'options' => $options,
2852 'hasMore' => false,
2853 ];
2854 }
2855
2856 /**
2857 * Prepare JetpackCRM Contact tags.
2858 *
2859 * @param array $data Search Params.
2860 *
2861 * @return array
2862 */
2863 public function search_jetpack_crm_contact_tags( $data ) {
2864
2865 if ( ! function_exists( 'zeroBSCRM_getCustomerTags' ) ) {
2866 return [];
2867 }
2868
2869 $all_tags = zeroBSCRM_getCustomerTags();
2870 $options = [];
2871
2872 if ( ! empty( $all_tags ) ) {
2873 foreach ( $all_tags as $tag ) {
2874 $options[] = [
2875 'label' => $tag['name'],
2876 'value' => $tag['id'],
2877 ];
2878 }
2879 }
2880
2881 return [
2882 'options' => $options,
2883 'hasMore' => false,
2884 ];
2885 }
2886
2887 /**
2888 * Prepare JetpackCRM Company tags.
2889 *
2890 * @param array $data Search Params.
2891 *
2892 * @return array
2893 */
2894 public function search_jetpack_crm_company_tags( $data ) {
2895
2896 if ( ! defined( 'ZBS_TYPE_COMPANY' ) ) {
2897 return [];
2898 }
2899
2900 global $wpdb;
2901 $all_tags = $wpdb->get_results( $wpdb->prepare( "SELECT `ID`,`zbstag_name` FROM `{$wpdb->prefix}zbs_tags` WHERE zbstag_objtype = %d", ZBS_TYPE_COMPANY ) );
2902
2903 $options = [];
2904 if ( ! empty( $all_tags ) ) {
2905 foreach ( $all_tags as $tag ) {
2906 $options[] = [
2907 'label' => $tag->zbstag_name,
2908 'value' => $tag->ID,
2909 ];
2910 }
2911 }
2912
2913 return [
2914 'options' => $options,
2915 'hasMore' => false,
2916 ];
2917 }
2918
2919 /**
2920 * Prepare JetpackCRM Companies list.
2921 *
2922 * @param array $data Search Params.
2923 *
2924 * @return array
2925 */
2926 public function search_jetpack_crm_companies_list( $data ) {
2927
2928 if ( ! function_exists( 'zeroBS_getCompanies' ) ) {
2929 return [];
2930 }
2931
2932 $all_companies = zeroBS_getCompanies();
2933 $options = [];
2934
2935 if ( ! empty( $all_companies ) ) {
2936 foreach ( $all_companies as $company ) {
2937 $options[] = [
2938 'label' => $company['name'],
2939 'value' => $company['id'],
2940 ];
2941 }
2942 }
2943
2944 return [
2945 'options' => $options,
2946 'hasMore' => false,
2947 ];
2948 }
2949
2950 /**
2951 * Prepare JetpackCRM contact status.
2952 *
2953 * @param array $data Search Params.
2954 *
2955 * @return array
2956 */
2957 public function search_jetpack_crm_contact_statuses( $data ) {
2958
2959 $options = [
2960 [
2961 'label' => __( 'Lead', 'suretriggers' ),
2962 'value' => 'Lead',
2963 ],
2964 [
2965 'label' => __( 'Customer', 'suretriggers' ),
2966 'value' => 'Customer',
2967 ],
2968 [
2969 'label' => __( 'Refused', 'suretriggers' ),
2970 'value' => 'Refused',
2971 ],
2972 [
2973 'label' => __( 'Blacklisted', 'suretriggers' ),
2974 'value' => 'Blacklisted',
2975 ],
2976 [
2977 'label' => __( 'Cancelled by Customer', 'suretriggers' ),
2978 'value' => 'Cancelled by Customer',
2979 ],
2980 [
2981 'label' => __( 'Cancelled by Us (Pre-Quote)', 'suretriggers' ),
2982 'value' => 'Cancelled by Us (Pre-Quote)',
2983 ],
2984 [
2985 'label' => __( 'Cancelled by Us (Post-Quote)', 'suretriggers' ),
2986 'value' => 'Cancelled by Us (Post-Quote)',
2987 ],
2988 ];
2989
2990 return [
2991 'options' => $options,
2992 'hasMore' => false,
2993 ];
2994 }
2995
2996 /**
2997 * Prepare FunnelKit Automations' lists.
2998 *
2999 * @param array $data Search Params.
3000 *
3001 * @return array
3002 */
3003 public function search_funnel_kit_automations_lists( $data ) {
3004
3005 if ( ! class_exists( 'BWFCRM_Lists' ) ) {
3006 return [];
3007 }
3008
3009 $bwfcrm_lists = \BWFCRM_Lists::get_lists();
3010
3011 $options = [];
3012
3013 foreach ( $bwfcrm_lists as $list ) {
3014 $options[] = [
3015 'label' => $list['name'],
3016 'value' => $list['ID'],
3017 ];
3018 }
3019
3020 return [
3021 'options' => $options,
3022 'hasMore' => false,
3023 ];
3024 }
3025
3026 /**
3027 * Prepare FunnelKit Automations' tags.
3028 *
3029 * @param array $data Search Params.
3030 *
3031 * @return array
3032 */
3033 public function search_funnel_kit_automations_tags( $data ) {
3034
3035 if ( ! class_exists( 'BWFCRM_Tag' ) ) {
3036 return [];
3037 }
3038
3039 $bwfcrm_tags = \BWFCRM_Tag::get_tags();
3040
3041 $options = [];
3042
3043 foreach ( $bwfcrm_tags as $tag ) {
3044 $options[] = [
3045 'label' => $tag['name'],
3046 'value' => $tag['ID'],
3047 ];
3048 }
3049
3050 return [
3051 'options' => $options,
3052 'hasMore' => false,
3053 ];
3054 }
3055
3056 /**
3057 * Prepare Wishlist Memberlists level.
3058 *
3059 * @param array $data Search Params.
3060 *
3061 * @return array[]
3062 */
3063 public function search_wishlistmember_lists( $data ) {
3064
3065 $wlm_levels = wlmapi_get_levels();
3066 $options = [];
3067
3068 if ( ! empty( $wlm_levels ) ) {
3069 foreach ( $wlm_levels['levels']['level'] as $list ) {
3070 if ( isset( $list['name'] ) && ! empty( $list['name'] ) ) {
3071 $options[] = [
3072 'label' => $list['name'],
3073 'value' => (string) $list['id'],
3074 ];
3075 }
3076 }
3077 }
3078
3079 return [
3080 'options' => $options,
3081 'hasMore' => false,
3082 ];
3083 }
3084
3085 /**
3086 * Prepare Wishlist Memberlists members.
3087 *
3088 * @param array $data Search Params.
3089 *
3090 * @return array<string, array<int<0, max>, array<string, mixed>>|false>
3091 */
3092 public function search_wishlistmember_members( $data ) {
3093
3094 if ( ! function_exists( 'wlmapi_get_members' ) ) {
3095 return [];
3096 }
3097
3098 $wlm_members = wlmapi_get_members();
3099 $options = [];
3100
3101 if ( ! empty( $wlm_members ) ) {
3102 foreach ( $wlm_members['members']['member'] as $list ) {
3103 if ( isset( $list['user_email'] ) && ! empty( $list['user_email'] ) ) {
3104 $options[] = [
3105 'label' => $list['user_email'],
3106 'value' => (string) $list['id'],
3107 ];
3108 }
3109 }
3110 }
3111
3112 return [
3113 'options' => $options,
3114 'hasMore' => false,
3115 ];
3116 }
3117
3118 /**
3119 * Prepare elementor popups.
3120 *
3121 * @param array $data Search Params.
3122 *
3123 * @return array[]
3124 */
3125 public function search_elementor_popups( $data ) {
3126
3127 $posts = get_posts(
3128 [
3129 'post_type' => 'elementor_library',
3130 'orderby' => 'title',
3131 'order' => 'ASC',
3132 'post_status' => 'publish',
3133 'meta_query' => [
3134 [
3135 'key' => '_elementor_template_type',
3136 'value' => 'popup',
3137 'compare' => '=',
3138 ],
3139 ],
3140 ]
3141 );
3142
3143 $options = [];
3144 if ( ! empty( $posts ) ) {
3145 foreach ( $posts as $post ) {
3146 $options[] = [
3147 'label' => $post->post_title,
3148 'value' => $post->ID,
3149 ];
3150 }
3151 }
3152
3153 return [
3154 'options' => $options,
3155 'hasMore' => false,
3156 ];
3157 }
3158
3159 /**
3160 * Prepare givewp forms.
3161 *
3162 * @param array $data Search Params.
3163 *
3164 * @return array
3165 */
3166 public function search_givewp_forms( $data ) {
3167
3168 $page = $data['page'];
3169 $limit = Utilities::get_search_page_limit();
3170 $offset = $limit * ( $page - 1 );
3171
3172 $posts = get_posts(
3173 [
3174 'post_type' => 'give_forms',
3175 'orderby' => 'title',
3176 'order' => 'ASC',
3177 'post_status' => 'publish',
3178 'posts_per_page' => $limit,
3179 'offset' => $offset,
3180 ]
3181 );
3182
3183 $options = [];
3184 if ( ! empty( $posts ) ) {
3185 foreach ( $posts as $post ) {
3186 $options[] = [
3187 'label' => $post->post_title,
3188 'value' => $post->ID,
3189 ];
3190 }
3191 }
3192
3193 $count = wp_count_posts( 'give_forms' )->publish;
3194
3195 return [
3196 'options' => $options,
3197 'hasMore' => $count > $limit && $count > $offset,
3198 ];
3199 }
3200
3201 /**
3202 * Prepare givewp recurring forms.
3203 *
3204 * @param array $data Search Params.
3205 *
3206 * @return array
3207 */
3208 public function search_givewp_recurring_forms( $data ) {
3209
3210 global $wpdb;
3211 $recurring = $wpdb->get_results( $wpdb->prepare( "SELECT form_id FROM {$wpdb->prefix}give_formmeta WHERE meta_key LIKE %s AND meta_value NOT LIKE %s", '_give_recurring', 'no' ), ARRAY_A );
3212
3213 $options = [];
3214 if ( ! empty( $recurring ) ) {
3215 foreach ( $recurring as $form_id ) {
3216 $post_status = get_post_status( $form_id['form_id'] );
3217 if ( 'trash' !== $post_status ) {
3218 $options[] = [
3219 'label' => get_the_title( $form_id['form_id'] ),
3220 'value' => $form_id['form_id'],
3221 ];
3222 }
3223 }
3224 }
3225
3226 return [
3227 'options' => $options,
3228 'hasMore' => false,
3229 ];
3230 }
3231
3232 /**
3233 * Prepare givewp forms.
3234 *
3235 * @param array $data Search Params.
3236 *
3237 * @return array
3238 */
3239 public function search_givewp_form_fields( $data ) {
3240 $options = [
3241 [
3242 'label' => 'First Name',
3243 'value' => '_give_donor_billing_first_name',
3244 ],
3245 [
3246 'label' => 'Last Name',
3247 'value' => '_give_donor_billing_last_name',
3248 ],
3249 [
3250 'label' => 'Email',
3251 'value' => '_give_payment_donor_email',
3252 ],
3253 [
3254 'label' => 'Donation Amount',
3255 'value' => '_give_payment_total',
3256 ],
3257 [
3258 'label' => 'Currency',
3259 'value' => '_give_payment_currency',
3260 ],
3261 [
3262 'label' => 'Comment',
3263 'value' => '_give_donation_comment',
3264 ],
3265 [
3266 'label' => 'Zip',
3267 'value' => '_give_donor_billing_zip',
3268 ],
3269 [
3270 'label' => 'Country',
3271 'value' => '_give_donor_billing_country',
3272 ],
3273 [
3274 'label' => 'Address 1',
3275 'value' => '_give_donor_billing_address1',
3276 ],
3277 [
3278 'label' => 'Address 2',
3279 'value' => '_give_donor_billing_address2',
3280 ],
3281 [
3282 'label' => 'City',
3283 'value' => '_give_donor_billing_city',
3284 ],
3285 [
3286 'label' => 'State',
3287 'value' => '_give_donor_billing_state',
3288 ],
3289 ];
3290
3291 if ( class_exists( '\Give_FFM_Render_Form' ) ) {
3292 $form_id = $data['dynamic'];
3293 $custom_form_fields = [];
3294 if ( class_exists( '\GiveFormFieldManager\Helpers\Form' ) ) {
3295 $custom_form_fields = \GiveFormFieldManager\Helpers\Form::get_input_fields( $form_id );
3296 }
3297
3298 if ( ! empty( $custom_form_fields ) ) {
3299 if ( ! empty( $custom_form_fields[2] ) && is_array( $custom_form_fields[2] ) ) {
3300 foreach ( $custom_form_fields[2] as $custom_form_field ) {
3301 $custom_form_field['required'] = ( 'no' === $custom_form_field['required'] ) ? false : true;
3302 $options[] = [
3303 'label' => $custom_form_field['label'],
3304 'value' => $custom_form_field['name'],
3305 ];
3306 }
3307 }
3308 }
3309 }
3310
3311 return [
3312 'options' => $options,
3313 'hasMore' => false,
3314 ];
3315 }
3316
3317 /**
3318 * Prepare buddyboss group users.
3319 *
3320 * @param array $data Search Params.
3321 *
3322 * @return array[]
3323 */
3324 public function search_bb_group_users( $data ) {
3325 $options = [];
3326
3327 $group_id = $data['dynamic'];
3328 $admins = groups_get_group_admins( $group_id );
3329
3330 if ( ! empty( $admins ) ) {
3331 foreach ( $admins as $admin ) {
3332 $admin_user = get_user_by( 'id', $admin->user_id );
3333 $options[] = [
3334 'label' => $admin_user->display_name,
3335 'value' => $admin_user->ID,
3336 ];
3337 }
3338 }
3339
3340 $members = groups_get_group_members( [ 'group_id' => $group_id ] );
3341
3342 if ( isset( $members['members'] ) && ! empty( $members['members'] ) ) {
3343 foreach ( $members['members'] as $member ) {
3344 $options[] = [
3345 'label' => $member->display_name,
3346 'value' => $member->ID,
3347 ];
3348 }
3349 }
3350 return [
3351 'options' => $options,
3352 'hasMore' => false,
3353 ];
3354 }
3355
3356 /**
3357 * Prepare buddyboss groups.
3358 *
3359 * @param array $data Search Params.
3360 *
3361 * @return array[]
3362 */
3363 public function search_buddyboss_groups( $data ) {
3364 global $wpdb;
3365
3366 $options = [];
3367 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
3368 if ( ! empty( $groups ) ) {
3369 foreach ( $groups as $group ) {
3370 $options[] = [
3371 'label' => $group->name,
3372 'value' => $group->id,
3373 ];
3374 }
3375 }
3376 return [
3377 'options' => $options,
3378 'hasMore' => false,
3379 ];
3380 }
3381
3382 /**
3383 * Prepare buddyboss public groups.
3384 *
3385 * @param array $data Search Params.
3386 *
3387 * @return array[]
3388 */
3389 public function search_buddyboss_public_groups( $data ) {
3390 $options = [];
3391 $groups = groups_get_groups();
3392 if ( isset( $groups['groups'] ) && ! empty( $groups['groups'] ) ) {
3393 foreach ( $groups['groups'] as $group ) {
3394 if ( 'public' === $group->status ) {
3395 $options[] = [
3396 'label' => $group->name,
3397 'value' => $group->id,
3398 ];
3399 }
3400 }
3401 }
3402 return [
3403 'options' => $options,
3404 'hasMore' => false,
3405 ];
3406 }
3407
3408 /**
3409 * Prepare buddyboss private groups.
3410 *
3411 * @param array $data Search Params.
3412 *
3413 * @return array<string, array<int, array<string, mixed>>|false>
3414 */
3415 public function search_buddyboss_private_groups( $data ) {
3416 global $wpdb;
3417
3418 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'private'" );
3419
3420 $options = [];
3421 if ( $groups ) {
3422 foreach ( $groups as $group ) {
3423 $options[] = [
3424 'label' => $group->name,
3425 'value' => $group->id,
3426 ];
3427 }
3428 }
3429
3430 return [
3431 'options' => $options,
3432 'hasMore' => false,
3433 ];
3434 }
3435
3436 /**
3437 * Prepare buddyboss profile types list.
3438 *
3439 * @param array $data Search Params.
3440 *
3441 * @return array[]
3442 */
3443 public function search_bb_profile_type_list( $data ) {
3444 $options = [];
3445
3446 if ( function_exists( 'bp_get_active_member_types' ) ) {
3447 $types = bp_get_active_member_types(
3448 [
3449 'fields' => '*',
3450 ]
3451 );
3452 if ( $types ) {
3453 foreach ( $types as $type ) {
3454 $options[] = [
3455 'label' => $type->post_title,
3456 'value' => $type->ID,
3457 ];
3458 }
3459 }
3460 }
3461
3462 /**
3463 *
3464 * Ignore line
3465 *
3466 * @phpstan-ignore-next-line
3467 */
3468 return [
3469 'options' => $options,
3470 'hasMore' => false,
3471 ];
3472 }
3473
3474 /**
3475 * Prepare buddyboss public groups.
3476 *
3477 * @param array $data Search Params.
3478 *
3479 * @return array
3480 */
3481 public function search_buddyboss_group_type( $data ) {
3482 $options = [];
3483
3484 if ( ! function_exists( 'bp_groups_get_group_types' ) ) {
3485 return [];
3486 }
3487
3488 $registered_types = bp_groups_get_group_types();
3489 if ( ! empty( $registered_types ) ) {
3490 foreach ( $registered_types as $key => $type ) {
3491 $options[] = [
3492 'label' => $type,
3493 'value' => $key,
3494 ];
3495 }
3496 }
3497 return [
3498 'options' => $options,
3499 'hasMore' => false,
3500 ];
3501 }
3502
3503 /**
3504 * Get BuddyBoss Forum Topic Status.
3505 *
3506 * @param array $data data.
3507 *
3508 * @return array|void
3509 */
3510 public function search_bb_forum_topic_status( $data ) {
3511 $options = [];
3512
3513 if ( ! function_exists( 'bbp_get_topic_post_type' ) ) {
3514 return [];
3515 }
3516
3517 $page = $data['page'];
3518 $limit = Utilities::get_search_page_limit();
3519 $offset = $limit * ( $page - 1 );
3520
3521 $forum_args = [
3522 'post_type' => bbp_get_topic_post_type(),
3523 'orderby' => 'title',
3524 'order' => 'ASC',
3525 'post_status' => [ 'publish', 'private' ],
3526 'posts_per_page' => $limit,
3527 'offset' => $offset,
3528 ];
3529
3530 $forums = get_posts( $forum_args );
3531 if ( ! empty( $forums ) ) {
3532 foreach ( $forums as $key => $forum ) {
3533 $options[] = [
3534 'label' => $forum,
3535 'value' => $key,
3536 ];
3537 }
3538 }
3539 $count = count( $options );
3540 return [
3541 'options' => $options,
3542 'hasMore' => $count > $limit && $count > $offset,
3543 ];
3544 }
3545
3546 /**
3547 * Get BuddyBoss Forums List.
3548 *
3549 * @param array $data data.
3550 *
3551 * @return array|void
3552 */
3553 public function search_bb_forums_list( $data ) {
3554 $options = [];
3555
3556 $page = $data['page'];
3557 $limit = Utilities::get_search_page_limit();
3558 $offset = $limit * ( $page - 1 );
3559
3560 if ( ! function_exists( 'bbp_get_forum_post_type' ) ) {
3561 return [];
3562 }
3563
3564 $forum_args = [
3565 'post_type' => bbp_get_forum_post_type(),
3566 'orderby' => 'title',
3567 'order' => 'ASC',
3568 'post_status' => [ 'publish', 'private' ],
3569 'posts_per_page' => $limit,
3570 'offset' => $offset,
3571 ];
3572
3573 $forums = get_posts( $forum_args );
3574 if ( ! empty( $forums ) ) {
3575 foreach ( $forums as $key => $forum ) {
3576 $options[] = [
3577 'label' => $forum->post_title,
3578 'value' => $forum->ID,
3579 ];
3580 }
3581 }
3582 $count = count( $options );
3583 return [
3584 'options' => $options,
3585 'hasMore' => $count > $limit && $count > $offset,
3586 ];
3587 }
3588
3589 /**
3590 * Get BuddyBoss Forums List.
3591 *
3592 * @param array $data data.
3593 *
3594 * @return array|void
3595 */
3596 public function search_bb_topics_list( $data ) {
3597 $options = [];
3598
3599 $page = $data['page'];
3600 $limit = Utilities::get_search_page_limit();
3601 $offset = $limit * ( $page - 1 );
3602
3603 if ( ! function_exists( 'bbp_get_topic_post_type' ) ) {
3604 return [];
3605 }
3606
3607 $topic_args = [
3608 'post_type' => bbp_get_topic_post_type(),
3609 'post_parent' => $data['dynamic'],
3610 'orderby' => 'title',
3611 'order' => 'ASC',
3612 'post_status' => [ 'publish', 'private' ],
3613 'posts_per_page' => $limit,
3614 'offset' => $offset,
3615 ];
3616
3617 $topics = get_posts( $topic_args );
3618 if ( ! empty( $topics ) ) {
3619 foreach ( $topics as $key => $topic ) {
3620 $options[] = [
3621 'label' => $topic->post_title,
3622 'value' => $topic->ID,
3623 ];
3624 }
3625 }
3626 $count = count( $options );
3627 return [
3628 'options' => $options,
3629 'hasMore' => $count > $limit && $count > $offset,
3630 ];
3631 }
3632
3633 /**
3634 * Prepare elementor forms.
3635 *
3636 * @param array $data Search Params.
3637 *
3638 * @return array[]
3639 */
3640 public function search_elementor_forms( $data ) {
3641
3642 $elementor_forms = Utilities::get_elementor_forms();
3643
3644 $options = [];
3645 if ( ! empty( $elementor_forms ) ) {
3646 foreach ( $elementor_forms as $key => $value ) {
3647 $options[] = [
3648 'label' => $value,
3649 'value' => $key,
3650 ];
3651 }
3652 }
3653
3654 return [
3655 'options' => $options,
3656 'hasMore' => false,
3657 ];
3658 }
3659
3660 /**
3661 * Prepare elementor forms.
3662 *
3663 * @param array $data Search Params.
3664 *
3665 * @return array[]
3666 */
3667 public function search_new_elementor_forms( $data ) {
3668
3669 global $wpdb;
3670 $elementor_forms = [];
3671 $post_metas = $wpdb->get_results(
3672 $wpdb->prepare(
3673 "SELECT pm.post_id, pm.meta_value
3674 FROM $wpdb->postmeta pm
3675 LEFT JOIN $wpdb->posts p
3676 ON p.ID = pm.post_id
3677 WHERE p.post_type IS NOT NULL
3678 AND p.post_status = %s
3679 AND pm.meta_key = %s
3680 AND pm.`meta_value` LIKE %s",
3681 'publish',
3682 '_elementor_data',
3683 '%%form_fields%%'
3684 )
3685 );
3686
3687 if ( ! empty( $post_metas ) ) {
3688 foreach ( $post_metas as $post_meta ) {
3689 /**
3690 *
3691 * Ignore line
3692 *
3693 * @phpstan-ignore-next-line
3694 */
3695 $inner_forms = Utilities::search_elementor_forms( json_decode( $post_meta->meta_value ) );
3696 if ( ! empty( $inner_forms ) ) {
3697 foreach ( $inner_forms as $form ) {
3698 /**
3699 *
3700 * Ignore line
3701 *
3702 * @phpstan-ignore-next-line
3703 */
3704 $elementor_forms[ $post_meta->post_id . '_' . $form->id ] = $form->settings->form_name . ' (' . $form->id . ')';
3705 }
3706 }
3707 }
3708 }
3709
3710 $options = [];
3711 if ( ! empty( $elementor_forms ) ) {
3712 foreach ( $elementor_forms as $key => $value ) {
3713 $options[] = [
3714 'label' => $value,
3715 'value' => $key,
3716 ];
3717 }
3718 }
3719
3720 /**
3721 *
3722 * Ignore line
3723 *
3724 * @phpstan-ignore-next-line
3725 */
3726 return [
3727 'options' => $options,
3728 'hasMore' => false,
3729 ];
3730 }
3731
3732 /**
3733 * Prepare elementor form fields.
3734 *
3735 * @param array $data Search Params.
3736 *
3737 * @return array[]
3738 */
3739 public function search_pluggable_elementor_form_fields( $data ) {
3740 $result = [];
3741 $form_id = absint( $data['dynamic'] );
3742 $elementor_form_fields = ( new Utilities() )->get_elementor_form_fields( $data );
3743 $options = [];
3744 if ( ! empty( $elementor_form_fields ) ) {
3745 foreach ( $elementor_form_fields as $key => $value ) {
3746 $options[] = [
3747 'label' => $value,
3748 'value' => '{' . $key . '}',
3749 ];
3750 }
3751 }
3752
3753 return [
3754 'options' => $options,
3755 'hasMore' => false,
3756 ];
3757 }
3758
3759 /**
3760 * Get all events
3761 *
3762 * @param array $data Data array.
3763 *
3764 * @return array
3765 */
3766 public function search_event_calendar_event( $data ) {
3767 $page = $data['page'];
3768 $limit = Utilities::get_search_page_limit();
3769 $offset = $limit * ( $page - 1 );
3770
3771 $posts = get_posts(
3772 [
3773 'post_type' => 'tribe_events',
3774 'orderby' => 'title',
3775 'order' => 'ASC',
3776 'post_status' => 'publish',
3777 'posts_per_page' => $limit,
3778 'offset' => $offset,
3779 ]
3780 );
3781
3782 $options = [];
3783 if ( ! empty( $posts ) ) {
3784 foreach ( $posts as $post ) {
3785 $options[] = [
3786 'label' => $post->post_title,
3787 'value' => $post->ID,
3788 ];
3789 }
3790 }
3791
3792 $count = wp_count_posts( 'tribe_events' )->publish;
3793
3794 return [
3795 'options' => $options,
3796 'hasMore' => $count > $limit && $count > $offset,
3797 ];
3798 }
3799
3800 /**
3801 * Get all events tickets
3802 *
3803 * @param array $data Data array.
3804 *
3805 * @return array
3806 */
3807 public function search_event_calendar_event_tickets( $data ) {
3808 $event_id = $data['dynamic'];
3809 $options = [];
3810
3811 if ( ! class_exists( 'Tribe__Tickets_Plus__Commerce__WooCommerce__Main' ) ) {
3812 return [];
3813 }
3814
3815 $tickets = \Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance()->get_all_event_tickets( $event_id );
3816 if ( ! empty( $tickets ) ) {
3817 foreach ( $tickets as $ticket_object ) {
3818 $options[] = [
3819 'label' => $ticket_object->name,
3820 'value' => $ticket_object->ID,
3821 ];
3822 }
3823 }
3824
3825 return [
3826 'options' => $options,
3827 'hasMore' => false,
3828 ];
3829 }
3830
3831 /**
3832 * Prepare rsvp event calendar events.
3833 *
3834 * @param array $data Search Params.
3835 *
3836 * @return array
3837 */
3838 public function search_event_calendar_rsvp_event( $data ) {
3839
3840 $page = $data['page'];
3841 $limit = Utilities::get_search_page_limit();
3842 $offset = $limit * ( $page - 1 );
3843
3844 $posts = get_posts(
3845 [
3846 'post_type' => 'tribe_events',
3847 'orderby' => 'title',
3848 'order' => 'ASC',
3849 'post_status' => 'publish',
3850 'posts_per_page' => $limit,
3851 'offset' => $offset,
3852 ]
3853 );
3854
3855 $options = [];
3856 if ( ! empty( $posts ) ) {
3857 $ticket_handler = new Tribe__Tickets__Tickets_Handler();
3858 foreach ( $posts as $post ) {
3859
3860 $get_rsvp_ticket = $ticket_handler->get_event_rsvp_tickets( $post->ID );
3861
3862 if ( ! empty( $get_rsvp_ticket ) ) {
3863 $options[] = [
3864 'label' => $post->post_title,
3865 'value' => $post->ID,
3866 ];
3867 }
3868 }
3869 }
3870 $count = wp_count_posts( 'tribe_events' )->publish;
3871
3872 return [
3873 'options' => $options,
3874 'hasMore' => $count > $limit && $count > $offset,
3875 ];
3876 }
3877
3878 /**
3879 * Prepare Restrict Content Membership Level.
3880 *
3881 * @param array $data Search Params.
3882 *
3883 * @return array[]
3884 */
3885 public function search_restrictcontent_membership_level( $data ) {
3886
3887 $rcp_memberships = rcp_get_membership_levels();
3888 $options = [];
3889
3890 if ( ! empty( $rcp_memberships ) ) {
3891 foreach ( $rcp_memberships as $list ) {
3892 $options[] = [
3893 'label' => ucfirst( $list->name ),
3894 'value' => $list->id,
3895 ];
3896 }
3897 }
3898
3899 return [
3900 'options' => $options,
3901 'hasMore' => false,
3902 ];
3903 }
3904
3905 /**
3906 * Prepare Restrict Content Customer.
3907 *
3908 * @param array $data Search Params.
3909 *
3910 * @return array[]
3911 */
3912 public function search_restrictcontent_customer( $data ) {
3913
3914 $rcp_users = rcp_get_memberships();
3915 $options = [];
3916
3917 if ( ! empty( $rcp_users ) ) {
3918 foreach ( $rcp_users as $list ) {
3919 $user = get_user_by( 'ID', $list->get_user_id() );
3920 $user_label = $user->user_email;
3921
3922 if ( $user->display_name !== $user->user_email ) {
3923 $user_label .= ' (' . $user->display_name . ')';
3924 }
3925
3926 $options[] = [
3927 'label' => $user_label,
3928 'value' => $list->get_customer_id(),
3929 ];
3930 }
3931 }
3932
3933 return [
3934 'options' => $options,
3935 'hasMore' => false,
3936 ];
3937 }
3938
3939
3940 /**
3941 * Fetch the Presto Player video List.
3942 *
3943 * @param array $data Search Params.
3944 *
3945 * @return array[]
3946 */
3947 public function search_ap_presto_player_video_list( $data ) {
3948
3949 $videos = ( new Video() )->all();
3950 $options = [];
3951 if ( ! empty( $videos ) ) {
3952 foreach ( $videos as $video ) {
3953 $options[] = [
3954 'label' => $video->__get( 'title' ),
3955 'value' => (string) $video->__get( 'id' ),
3956 ];
3957 }
3958 }
3959
3960 return [
3961 'options' => $options,
3962 'hasMore' => false,
3963 ];
3964 }
3965
3966 /**
3967 * Presto Player Video percentage.
3968 *
3969 * @param array $data Search Params.
3970 *
3971 * @return array[]
3972 */
3973 public function search_prestoplayer_video_percent( $data ) {
3974
3975 $percents = [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ];
3976 $options = [];
3977
3978 foreach ( $percents as $percent ) {
3979 $options[] = [
3980 'label' => $percent . '%',
3981 'value' => (string) $percent,
3982 ];
3983 }
3984
3985 return [
3986 'options' => $options,
3987 'hasMore' => false,
3988 ];
3989 }
3990
3991 /**
3992 * Get user profile field options.
3993 *
3994 * @return array
3995 * @since 1.0.0
3996 */
3997 public function search_user_field_options() {
3998
3999 $options = apply_filters(
4000 'sure_trigger_get_user_field_options',
4001 [
4002 [
4003 'label' => __( 'User Name', 'suretriggers' ),
4004 'value' => 'user_login',
4005 ],
4006 [
4007 'label' => __( 'User Email', 'suretriggers' ),
4008 'value' => 'user_email',
4009 ],
4010 [
4011 'label' => __( 'Display Name', 'suretriggers' ),
4012 'value' => 'display_name',
4013 ],
4014 [
4015 'label' => __( 'User Password', 'suretriggers' ),
4016 'value' => 'user_pass',
4017 ],
4018 [
4019 'label' => __( 'Website', 'suretriggers' ),
4020 'value' => 'user_url',
4021 ],
4022 ]
4023 );
4024
4025 return [
4026 'options' => $options,
4027 'hasMore' => false,
4028 ];
4029 }
4030
4031 /**
4032 * Get user post field options.
4033 *
4034 * @return array
4035 * @since 1.0.0
4036 */
4037 public function search_post_field_options() {
4038
4039 return [
4040 'options' => [
4041 [
4042 'label' => __( 'Type', 'suretriggers' ),
4043 'value' => 'post_type',
4044 'dynamic_field' => [
4045 'type' => 'select-creatable',
4046 'ajaxIdentifier' => 'post_types',
4047 ],
4048 ],
4049 [
4050 'label' => __( 'Status', 'suretriggers' ),
4051 'value' => 'post_status',
4052 'dynamic_field' => [
4053 'type' => 'select-async',
4054 'ajaxIdentifier' => 'post_statuses',
4055 ],
4056 ],
4057 [
4058 'label' => __( 'Author', 'suretriggers' ),
4059 'value' => 'post_author',
4060 'dynamic_field' => [
4061 'type' => 'select-async',
4062 'ajaxIdentifier' => 'user',
4063 ],
4064 ],
4065 [
4066 'label' => __( 'Title', 'suretriggers' ),
4067 'value' => 'post_title',
4068 'dynamic_field' => [
4069 'type' => 'select-creatable',
4070 ],
4071 ],
4072 [
4073 'label' => __( 'Slug', 'suretriggers' ),
4074 'value' => 'post_slug',
4075 'dynamic_field' => [
4076 'type' => 'select-creatable',
4077 ],
4078 ],
4079 [
4080 'label' => __( 'Content', 'suretriggers' ),
4081 'value' => 'post_content',
4082 'dynamic_field' => [
4083 'type' => 'html-editor',
4084 ],
4085 ],
4086 ],
4087 'hasMore' => false,
4088 ];
4089 }
4090
4091 /**
4092 * Bricksbuilder grouped data.
4093 *
4094 * @param array $data data.
4095 * @return array
4096 */
4097 public function search_bb_groups( $data ) {
4098
4099 global $wpdb;
4100 $options = [];
4101
4102 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'bp_groups' ) ) ) {
4103
4104 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %s', $wpdb->prefix . 'bp_groups' ) );
4105
4106 if ( $results ) {
4107 foreach ( $results as $result ) {
4108 $options[] = [
4109 'label' => $result->name,
4110 'value' => $result->id,
4111 ];
4112 }
4113 }
4114 }
4115
4116 return [
4117 'options' => $options,
4118 'hasMore' => false,
4119 ];
4120 }
4121
4122 /**
4123 * Search forms.
4124 *
4125 * @return array
4126 */
4127 public function search_bb_forums() {
4128 $options = [];
4129 $allowed_atatus = [ 'publish', 'private' ];
4130 $forum_args = [
4131 'post_type' => bbp_get_forum_post_type(),
4132 'posts_per_page' => -1,
4133 'orderby' => 'title',
4134 'order' => 'ASC',
4135 'post_status' => 'any',
4136 ];
4137 $forums = get_posts( $forum_args );
4138
4139 if ( ! empty( $forums ) ) {
4140 foreach ( $forums as $forum ) {
4141 if ( in_array( $forum->post_status, $allowed_atatus, true ) ) {
4142 $options[] = [
4143 'label' => $forum->post_title,
4144 'value' => $forum->ID,
4145 ];
4146 }
4147 }
4148 }
4149 return [
4150 'options' => $options,
4151 'hasMore' => false,
4152 ];
4153 }
4154
4155 /**
4156 * Search Affiliate WP Referral Type.
4157 *
4158 * @return array
4159 */
4160 public function search_affwp_referral_type() {
4161 $options = [];
4162
4163 if ( ! function_exists( 'affiliate_wp' ) ) {
4164 return [];
4165 }
4166
4167 $types = affiliate_wp()->referrals->types_registry->get_types();
4168 if ( ! empty( $types ) ) {
4169 foreach ( $types as $type_id => $type ) {
4170 $options[] = [
4171 'label' => $type['label'],
4172 'value' => $type_id,
4173 ];
4174 }
4175 }
4176 return [
4177 'options' => $options,
4178 'hasMore' => false,
4179 ];
4180 }
4181
4182 /**
4183 * Search Affiliate WP Referral Status.
4184 *
4185 * @return array
4186 */
4187 public function search_affwp_referral_status() {
4188 $options = [];
4189
4190 if ( ! function_exists( 'affwp_get_affiliate_statuses' ) ) {
4191 return [];
4192 }
4193
4194 $statuses = affwp_get_affiliate_statuses();
4195 if ( ! empty( $statuses ) ) {
4196 foreach ( $statuses as $key => $status ) {
4197 $options[] = [
4198 'label' => $status,
4199 'value' => $key,
4200 ];
4201 }
4202 }
4203 return [
4204 'options' => $options,
4205 'hasMore' => false,
4206 ];
4207 }
4208
4209 /**
4210 * Search Affiliate WP Affiliates list.
4211 *
4212 * @return array
4213 */
4214 public function search_affwp_affiliates_list() {
4215 $options = [];
4216
4217 global $wpdb;
4218 $affiliates = $wpdb->get_results( "SELECT affiliate_id FROM {$wpdb->prefix}affiliate_wp_affiliates" );
4219
4220 if ( ! function_exists( 'affwp_get_affiliate_name' ) ) {
4221 return [];
4222 }
4223
4224 if ( ! empty( $affiliates ) ) {
4225 foreach ( $affiliates as $affiliate ) {
4226 $options[] = [
4227 'label' => affwp_get_affiliate_name( $affiliate->affiliate_id ),
4228 'value' => $affiliate->affiliate_id,
4229 ];
4230 }
4231 }
4232 return [
4233 'options' => $options,
4234 'hasMore' => false,
4235 ];
4236 }
4237
4238 /**
4239 * Search Slice WP Affiliates list.
4240 *
4241 * @return array
4242 */
4243 public function search_slicewp_affiliates_list() {
4244 $options = [];
4245
4246 global $wpdb;
4247 $affiliates = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}slicewp_affiliates" );
4248
4249 if ( ! function_exists( 'slicewp_get_affiliate_name' ) ) {
4250 return [];
4251 }
4252
4253 if ( ! empty( $affiliates ) ) {
4254 foreach ( $affiliates as $affiliate ) {
4255 $options[] = [
4256 'label' => slicewp_get_affiliate_name( $affiliate->id ),
4257 'value' => $affiliate->id,
4258 ];
4259 }
4260 }
4261 return [
4262 'options' => $options,
4263 'hasMore' => false,
4264 ];
4265 }
4266
4267 /**
4268 * Search for commissions list.
4269 *
4270 * @param array $data data.
4271 *
4272 * @return array
4273 */
4274 public function search_slicewp_commissions_list( $data ) {
4275 $options = [];
4276 global $wpdb;
4277 $affiliate_id = $data['dynamic']['affiliate_id'];
4278 $commissions = $wpdb->get_results(
4279 $wpdb->prepare(
4280 "SELECT *
4281 FROM {$wpdb->prefix}slicewp_commissions WHERE affiliate_id=%d ORDER BY id DESC ",
4282 $affiliate_id
4283 )
4284 );
4285
4286
4287 if ( ! empty( $commissions ) ) {
4288 foreach ( $commissions as $commission ) {
4289 $options[] = [
4290 'label' => $commission->reference . '(' . $commission->type . ')',
4291 'value' => $commission->id,
4292 ];
4293 }
4294 }
4295 return [
4296 'options' => $options,
4297 'hasMore' => false,
4298 ];
4299 }
4300
4301
4302 /**
4303 * Search Affiliate WP Affiliates list.
4304 *
4305 * @return array
4306 */
4307 public function search_affwp_affiliates_rate_type() {
4308 $options = [];
4309
4310 if ( ! function_exists( 'affwp_get_affiliate_rate_types' ) ) {
4311 return [];
4312 }
4313
4314 $rate_types = affwp_get_affiliate_rate_types();
4315
4316 if ( ! empty( $rate_types ) ) {
4317 foreach ( $rate_types as $key => $rate_type ) {
4318 $options[] = [
4319 'label' => $rate_type,
4320 'value' => $key,
4321 ];
4322 }
4323 }
4324 return [
4325 'options' => $options,
4326 'hasMore' => false,
4327 ];
4328 }
4329
4330 /**
4331 * Get last data for trigger.
4332 *
4333 * @param array $data data.
4334 * @return array
4335 */
4336 public function search_affiliate_wp_triggers_last_data( $data ) {
4337 global $wpdb;
4338
4339 $context = [];
4340 $context['response_type'] = 'sample';
4341
4342 $user_data = WordPress::get_sample_user_context();
4343
4344 $affiliate_data = [
4345 'affiliate_id' => 1,
4346 'rest_id' => '',
4347 'user_id' => 1,
4348 'rate' => '',
4349 'rate_type' => '',
4350 'flat_rate_basis' => '',
4351 'payment_email' => 'admin@gmail.com',
4352 'status' => 'active',
4353 'earnings' => 0,
4354 'unpaid_earnings' => 0,
4355 'referrals' => 0,
4356 'visits' => 0,
4357 'date_registered' => '2023-01-18 13:35:22',
4358 'dynamic_coupon' => 'KDJSKS',
4359 ];
4360
4361 $referral_data = [
4362 'referral_id' => 1,
4363 'affiliate_id' => 1,
4364 'visit_id' => 0,
4365 'rest_id' => '',
4366 'customer_id' => 0,
4367 'parent_id' => 0,
4368 'description' => 'Testing',
4369 'status' => 'unpaid',
4370 'amount' => '12.00',
4371 'currency' => '',
4372 'custom' => 'custom',
4373 'context' => '',
4374 'campaign' => '',
4375 'reference' => 'Reference',
4376 'products' => '',
4377 'date' => '2023-01-18 16:36:59',
4378 'type' => 'opt-in',
4379 'payout_id' => 0,
4380 ];
4381
4382 if ( ! function_exists( 'affwp_get_dynamic_affiliate_coupons' ) || ! function_exists( 'affwp_get_affiliate' ) || ! function_exists( 'affwp_get_referral' ) ) {
4383 return [];
4384 }
4385
4386 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4387
4388 if ( in_array( $term, [ 'affiliate_approved', 'affiliate_awaiting_approval' ], true ) ) {
4389 $status = 'affiliate_approved' === $term ? 'active' : 'pending';
4390 $affiliate = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_affiliates WHERE affiliate_id = ( SELECT max(affiliate_id) FROM {$wpdb->prefix}affiliate_wp_affiliates )" );
4391
4392 if ( ! empty( $affiliate ) ) {
4393 $affiliate = affwp_get_affiliate( $affiliate->affiliate_id );
4394 $affiliate_data = get_object_vars( $affiliate );
4395 $user_data = WordPress::get_user_context( $affiliate->user_id );
4396 $context['response_type'] = 'live';
4397 }
4398 $affiliate_data['status'] = $status;
4399 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data );
4400
4401 } elseif ( 'affiliate_makes_referral' == $term ) {
4402 $type = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4403 $referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE referral_id = ( SELECT max(referral_id) FROM {$wpdb->prefix}affiliate_wp_referrals ) AND sale = %s", $type ) );
4404
4405 if ( ! empty( $referral ) ) {
4406 $referral = affwp_get_referral( $referral->referral_id );
4407 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4408 $affiliate_data = get_object_vars( $affiliate );
4409 $user_data = WordPress::get_user_context( $affiliate->user_id );
4410 $referral_data = get_object_vars( $referral );
4411 $context['response_type'] = 'live';
4412 }
4413 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4414 } elseif ( 'affiliate_wc_product_purchased' == $term ) {
4415 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4416 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'woocommerce' ORDER BY referral_id DESC LIMIT 1" );
4417
4418 if ( ! empty( $referral ) ) {
4419 $referral = affwp_get_referral( $referral->referral_id );
4420 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4421 $affiliate_data = get_object_vars( $affiliate );
4422 if ( ! empty( $referral ) && function_exists( 'wc_get_order' ) ) {
4423 $order_id = $referral->reference;
4424 $order = wc_get_order( $order_id );
4425 if ( $order instanceof \WC_Order ) {
4426 $user_data = WordPress::get_user_context( $order->get_customer_id() );
4427 }
4428 }
4429 $referral_data = get_object_vars( $referral );
4430 $context['response_type'] = 'live';
4431 }
4432 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
4433 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data, $dynamic_coupons );
4434 if ( ! empty( $referral ) && function_exists( 'wc_get_order' ) ) {
4435 $order_id = $referral->reference;
4436 $order = wc_get_order( $order_id );
4437 $items = $order->get_items();
4438 foreach ( $items as $item ) {
4439 $context['pluggable_data']['product'] = $item['product_id'];
4440 }
4441 } else {
4442 $context['pluggable_data']['product'] = 1;
4443 }
4444 } elseif ( 'affiliate_edd_product_purchased' == $term ) {
4445 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4446 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'edd' ORDER BY referral_id DESC LIMIT 1" );
4447
4448 if ( ! empty( $referral ) ) {
4449 $referral = affwp_get_referral( $referral->referral_id );
4450 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4451 $affiliate_data = get_object_vars( $affiliate );
4452 $user_data = WordPress::get_user_context( $affiliate->user_id );
4453 $referral_data = get_object_vars( $referral );
4454 $context['response_type'] = 'live';
4455 }
4456 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4457 if ( ! empty( $referral ) && function_exists( 'edd_get_payment' ) ) {
4458 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
4459 $edd_payment_id = $referral->reference;
4460 $payment = edd_get_payment( $edd_payment_id );
4461 $cart_details = $payment->cart_details;
4462 $payment = get_object_vars( $payment );
4463 $context['pluggable_data'] = array_merge( $context['pluggable_data'], $dynamic_coupons, $payment );
4464 foreach ( $cart_details as $detail ) {
4465 $context['pluggable_data']['product'] = $detail['id'];
4466 }
4467 } else {
4468 $context['pluggable_data']['product'] = 1;
4469 }
4470 } elseif ( 'affiliate_mb_product_purchased' == $term ) {
4471 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4472 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'memberpress' ORDER BY referral_id DESC LIMIT 1" );
4473
4474 if ( ! empty( $referral ) && class_exists( '\MeprTransaction' ) ) {
4475 $referral = affwp_get_referral( $referral->referral_id );
4476 $reference_id = $referral->reference;
4477 $transaction = new MeprTransaction( $reference_id );
4478 $user_id = $transaction->user_id;
4479 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4480 $affiliate_data = get_object_vars( $affiliate );
4481 $user_data = WordPress::get_user_context( $user_id );
4482 $referral_data = get_object_vars( $referral );
4483 $context['response_type'] = 'live';
4484 }
4485 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
4486 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data, $dynamic_coupons );
4487 if ( ! empty( $referral ) && class_exists( '\MeprTransaction' ) ) {
4488 $membership_id = $wpdb->get_var(
4489 $wpdb->prepare(
4490 "SELECT product_id FROM
4491 {$wpdb->prefix}mepr_transactions WHERE id = %d",
4492 $referral->reference
4493 )
4494 );
4495 $context['pluggable_data']['product'] = $membership_id;
4496 $context['pluggable_data']['product_name'] = get_the_title( $membership_id );
4497 } else {
4498 $context['pluggable_data']['product'] = 1;
4499 $context['pluggable_data']['product_name'] = 'membership1';
4500 }
4501 } elseif ( 'affiliate_referral_paid' == $term ) {
4502 $type = isset( $data['filter']['type']['value'] ) ? $data['filter']['type']['value'] : '';
4503 $referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE type = %s AND status = 'paid'", $type ) );
4504
4505 if ( ! empty( $referral ) ) {
4506 $referral = affwp_get_referral( $referral->referral_id );
4507 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4508 $affiliate_data = get_object_vars( $affiliate );
4509 $user_data = WordPress::get_user_context( $affiliate->user_id );
4510 $referral_data = get_object_vars( $referral );
4511 $context['response_type'] = 'live';
4512 }
4513 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4514 } elseif ( 'affiliate_referral_rejected' == $term ) {
4515 $type = isset( $data['filter']['type']['value'] ) ? $data['filter']['type']['value'] : '';
4516 $referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE type = %s AND status = 'rejected'", $type ) );
4517 if ( ! empty( $referral ) ) {
4518 $referral = affwp_get_referral( $referral->referral_id );
4519 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4520 $affiliate_data = get_object_vars( $affiliate );
4521 $user_data = WordPress::get_user_context( $affiliate->user_id );
4522 $referral_data = get_object_vars( $referral );
4523 $context['response_type'] = 'live';
4524 }
4525 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4526 } else {
4527 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE referral_id = ( SELECT max(referral_id) FROM {$wpdb->prefix}affiliate_wp_referrals )" );
4528
4529 if ( ! empty( $referral ) ) {
4530 $referral = affwp_get_referral( $referral->referral_id );
4531 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4532 $affiliate_data = get_object_vars( $affiliate );
4533 $user_data = WordPress::get_user_context( $affiliate->user_id );
4534 $referral_data = get_object_vars( $referral );
4535 $context['response_type'] = 'live';
4536 }
4537 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4538 }
4539
4540 return $context;
4541 }
4542
4543 /**
4544 * Get last data for trigger.
4545 *
4546 * @param array $data data.
4547 * @return array
4548 */
4549 public function search_jetpack_crm_triggers_last_data( $data ) {
4550
4551 if ( ! function_exists( 'zeroBS_getCompanyCount' ) || ! function_exists( 'zeroBS_getCustomerCount' ) || ! function_exists( 'zeroBS_getQuoteCount' ) ) {
4552 return [];
4553 }
4554
4555 global $wpdb;
4556
4557 $context = [];
4558 $context['response_type'] = 'sample';
4559
4560 $company_id = [ 'company_id' => 1 ];
4561 $contact_id = [ 'contact_id' => 1 ];
4562 $quote_id = [ 'quote_id' => 1 ];
4563 $event_id = [ 'event_id' => 1 ];
4564 $invoice_id = [ 'invoice_id' => 1 ];
4565 $transaction_id = [ 'transaction_id' => 1 ];
4566
4567 $company = [
4568 'company_id' => 1,
4569 'company_status' => 'Lead',
4570 'company_name' => 'Example Company',
4571 'company_email' => 'info@example.com',
4572 'main_address_line_1' => '123 Main Street',
4573 'main_address_line_2' => 'Suite 456',
4574 'main_address_city' => 'New York',
4575 'main_address_state' => 'NY',
4576 'main_address_postal_code' => '10001',
4577 'main_address_country' => 'United States',
4578 'second_address_line_1' => '789 Second Avenue',
4579 'second_address_line_2' => 'Floor 10',
4580 'second_address_city' => 'Los Angeles',
4581 'second_address_state' => 'CA',
4582 'second_address_postal_code' => '90001',
4583 'second_address_country' => 'United States',
4584 'main_telephone' => '+1 123-456-7890',
4585 'secondary_telephone' => '+1 987-654-3210',
4586 ];
4587
4588 $contact = [
4589 'contact_id' => 1,
4590 'status' => 'Lead',
4591 'prefix' => 'Mr.',
4592 'full_name' => 'John Doe',
4593 'first_name' => 'John',
4594 'last_name' => 'Doe',
4595 'email' => 'johndoe@example.com',
4596 'main_address_line_1' => '123 Main Street',
4597 'main_address_line_2' => 'Suite 456',
4598 'main_address_city' => 'New York',
4599 'main_address_state' => 'NY',
4600 'main_address_postal_code' => '10001',
4601 'main_address_country' => 'United States',
4602 'second_address_line_1' => '789 Second Avenue',
4603 'second_address_line_2' => 'Floor 10',
4604 'second_address_city' => 'Los Angeles',
4605 'second_address_state' => 'CA',
4606 'second_address_postal_code' => '90001',
4607 'second_address_country' => 'United States',
4608 'home_telephone' => '+1 123-456-7890',
4609 'work_telephone' => '+1 987-654-3210',
4610 'mobile_telephone' => '+1 555-555-5555',
4611 ];
4612
4613 $quote = [
4614 'quote_id' => 1,
4615 'contact_id' => 2,
4616 'contact_email' => 'john@example.com',
4617 'contact_name' => 'John Doe',
4618 'status' => '<strong>Created, not yet accepted</strong>',
4619 'title' => 'Sample Quote',
4620 'value' => 1000,
4621 'date' => '2023-05-23',
4622 'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
4623 'notes' => 'Additional notes about the quote.',
4624 ];
4625
4626 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4627
4628 switch ( $term ) {
4629 case 'company_created':
4630 if ( zeroBS_getCompanyCount() > 0 ) {
4631 $company_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_companies WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_companies )" );
4632 $company = JetpackCRM::get_company_context( $company_data->ID );
4633 $context['response_type'] = 'live';
4634 }
4635 $context['pluggable_data'] = $company;
4636 break;
4637 case 'company_deleted':
4638 $context['pluggable_data'] = $company_id;
4639 break;
4640 case 'contact_created':
4641 if ( zeroBS_getCustomerCount() > 0 ) {
4642 $contact_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_contacts WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_contacts )" );
4643 $contact = JetpackCRM::get_contact_context( $contact_data->ID );
4644 $context['response_type'] = 'live';
4645 }
4646 $context['pluggable_data'] = $contact;
4647 break;
4648 case 'contact_deleted':
4649 $context['pluggable_data'] = $contact_id;
4650 break;
4651 case 'event_deleted':
4652 $context['pluggable_data'] = $event_id;
4653 break;
4654 case 'invoice_deleted':
4655 $context['pluggable_data'] = $invoice_id;
4656 break;
4657 case 'quote_accepted':
4658 case 'quote_created':
4659 if ( zeroBS_getQuoteCount() > 0 ) {
4660 $quote_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_quotes WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_quotes )" );
4661 $quote = JetpackCRM::get_quote_context( $quote_data->ID );
4662 $context['response_type'] = 'live';
4663 }
4664 $context['pluggable_data'] = $quote;
4665 break;
4666 case 'quote_deleted':
4667 $context['pluggable_data'] = $quote_id;
4668 break;
4669 case 'transaction_deleted':
4670 $context['pluggable_data'] = $transaction_id;
4671 break;
4672 }
4673
4674 return $context;
4675 }
4676
4677 /**
4678 * Get last data for trigger.
4679 *
4680 * @param array $data data.
4681 * @return array
4682 */
4683 public function search_funnel_kit_automations_triggers_last_data( $data ) {
4684
4685 if ( ! class_exists( 'BWFCRM_Contact' ) || ! class_exists( 'BWFCRM_Lists' ) || ! class_exists( 'BWFCRM_Tag' ) ) {
4686 return [];
4687 }
4688
4689 $context = [];
4690 $context['response_type'] = 'sample';
4691
4692 $contact = [
4693 'contact_id' => '1',
4694 'wpid' => '0',
4695 'uid' => '9e74246335fd81b1c4a9123842c12549',
4696 'email' => 'johndoe@example.com',
4697 'first_name' => 'John',
4698 'last_name' => 'Doe',
4699 'contact_no' => '+1 555-555-5555',
4700 'state' => 'NY',
4701 'country' => 'United States',
4702 'timezone' => 'New York',
4703 'creation_date' => '2023-05-29 15:26:03',
4704 'last_modified' => '2023-05-29 17:08:30',
4705 'source' => '',
4706 'type' => 'Los Angeles',
4707 'status' => '0',
4708 'tags' => '["1"]',
4709 'lists' => '["2","1"]',
4710 ];
4711
4712 $list = [
4713 'list_id' => 1,
4714 'list_name' => 'Sample List',
4715 ];
4716
4717 $tag = [
4718 'tag_id' => 1,
4719 'tag_name' => 'Sample Tag',
4720 ];
4721
4722 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4723
4724 $recent_contacts = \BWFCRM_Contact::get_recent_contacts( 0, 1, [] );
4725 $contact_email = count( $recent_contacts['contacts'] ) > 0 && isset( $recent_contacts['contacts'][0]['email'] ) ? $recent_contacts['contacts'][0]['email'] : '';
4726
4727 $real_contact = false;
4728 if ( ! empty( $contact_email ) ) {
4729 $contact_obj = \BWFCRM_Contact::get_contacts( $contact_email, 0, 1, [], [], OBJECT );
4730
4731 if ( isset( $contact_obj['contacts'][0] ) ) {
4732 $contact = FunnelKitAutomations::get_contact_context( $contact_obj['contacts'][0]->contact );
4733 $real_contact = true;
4734 }
4735 }
4736
4737 if ( 'contact_added_to_list' === $term || 'contact_removed_from_list' === $term ) {
4738 $list_id = (int) ( isset( $data['filter']['list_id']['value'] ) ? $data['filter']['list_id']['value'] : '-1' );
4739
4740 if ( -1 === $list_id ) {
4741 $lists = \BWFCRM_Lists::get_lists( [], '', 0, 1 );
4742 $list_id = count( $lists ) > 0 ? $lists[0]['ID'] : -1;
4743 }
4744
4745
4746 if ( -1 !== $list_id ) {
4747 $list = FunnelKitAutomations::get_list_context( $list_id );
4748 $context['response_type'] = $real_contact ? 'live' : 'sample';
4749 }
4750
4751 $context['pluggable_data'] = array_merge( $list, $contact );
4752 } else {
4753 $tag_id = (int) ( isset( $data['filter']['tag_id']['value'] ) ? $data['filter']['tag_id']['value'] : '-1' );
4754
4755 if ( -1 === $tag_id ) {
4756 $tags = \BWFCRM_Tag::get_tags( [], '', 0, 1 );
4757 $tag_id = count( $tags ) > 0 ? $tags[0]['ID'] : -1;
4758 }
4759
4760
4761 if ( -1 !== $tag_id ) {
4762 $tag = FunnelKitAutomations::get_tag_context( $tag_id );
4763 $context['response_type'] = $real_contact ? 'live' : 'sample';
4764 }
4765
4766 $context['pluggable_data'] = array_merge( $tag, $contact );
4767 }
4768
4769 return $context;
4770 }
4771
4772 /**
4773 * Get last data for trigger.
4774 *
4775 * @param array $data data.
4776 * @return array
4777 */
4778 public function search_edd_triggers_last_data( $data ) {
4779 global $wpdb;
4780 $context = [];
4781 $context['response_type'] = 'sample';
4782 $context['pluggable_data'] = [];
4783 $order_data = [
4784 'order_id' => 187,
4785 'customer_email' => 'john_doe@gmail.com',
4786 'customer_id' => 2,
4787 'user_id' => 1,
4788 'customer_first_name' => 'Sure',
4789 'customer_last_name' => 'Dev',
4790 'ordered_items' => 'Price with license — Price one',
4791 'currency' => 'USD',
4792 'status' => 'complete',
4793 'discount_codes' => '',
4794 'order_discounts' => 0.00,
4795 'order_subtotal' => 12.00,
4796 'order_tax' => 0.00,
4797 'order_total' => 12.00,
4798 'payment_method' => 'manual',
4799 'purchase_key' => 'd797b9576a3895e7424bae2417ed87df',
4800 'ordered_items_ids' => 17250,
4801 'download_id' => 17250,
4802 'license_key' => 'f7736093411cfaed18b56ec60227117b',
4803 'license_key_expire_date' => '1697524076',
4804 'license_key_status' => 'inactive',
4805 ];
4806
4807 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4808 $download_id = isset( $data['filter']['download_id']['value'] ) ? $data['filter']['download_id']['value'] : 0;
4809 if ( 'order_created' === $term || 'order_one_product' === $term ) {
4810 $order_data['purchase_key'] = '06d3b7d923ca922dc889354f9bc33fbb';
4811
4812 $args = [
4813 'number' => 1,
4814 'status' => [ 'complete', 'refunded', 'partially_refunded', 'renewal' ],
4815 ];
4816 if ( $download_id > 0 ) {
4817 $args['download'] = $download_id;
4818 }
4819 $payments = edd_get_payments( $args );
4820 if ( count( $payments ) > 0 ) {
4821 $order_data = EDD::get_product_purchase_context( $payments[0], $term, $download_id );
4822
4823 $context['response_type'] = 'live';
4824 } else {
4825 if ( 'order_one_product' === $term ) {
4826 $order_data['price_id'] = 1;
4827 }
4828 }
4829 } elseif ( 'stripe_payment_refunded' === $term ) {
4830 $args = [
4831 'number' => 1,
4832 'status' => 'complete',
4833 'type' => 'refund',
4834 ];
4835 $payments = edd_get_payments( $args );
4836
4837 if ( count( $payments ) > 0 ) {
4838 $order_data = EDD::get_purchase_refund_context( $payments[0] );
4839 $context['response_type'] = 'live';
4840 }
4841 } else {
4842 $status = isset( $data['post_type'] ) ? $data['post_type'] : '';
4843 if ( ! empty( $status ) ) {
4844 if ( $download_id > 0 ) {
4845 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE status= %s AND download_id=%d ORDER BY id DESC", $status, $download_id ) );
4846 } else {
4847 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE status= %s ORDER BY id DESC", $status ) );
4848 }
4849 } else {
4850 if ( $download_id > 0 ) {
4851 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE download_id=%d ORDER BY id DESC", $download_id ) );
4852 } else {
4853 $licesnses = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}edd_licenses ORDER BY id DESC" );
4854 }
4855 }
4856 if ( ! empty( $licesnses ) ) {
4857 $order_data = EDD::edd_get_license_data( $licesnses->id, $licesnses->download_id, $licesnses->payment_id );
4858 $context['response_type'] = 'live';
4859 } else {
4860 $order_data = [
4861 'ID' => 1,
4862 'key' => '23232323232',
4863 'customer_email' => 'suretest@example.com',
4864 'customer_name' => 'Sure Test',
4865 'product_id' => 1,
4866 'download_id' => 1,
4867 'product_name' => 'Test',
4868 'activation_limit' => 2,
4869 'activation_count' => 1,
4870 'activated_urls' => 'https://example.com',
4871 'expiration' => '1686297914',
4872 'is_lifetime' => '0',
4873 'status' => $status,
4874 ];
4875
4876 }
4877 }
4878
4879 $context['pluggable_data'] = $order_data;
4880 return $context;
4881 }
4882
4883 /**
4884 * Get last data for trigger.
4885 *
4886 * @param array $data data.
4887 * @return array
4888 */
4889 public function search_presto_player_triggers_last_data( $data ) {
4890 $context = [];
4891 $context['response_type'] = 'sample';
4892
4893 $user_data = WordPress::get_sample_user_context();
4894
4895 $video_data = [
4896 'pp_video' => '1',
4897 'pp_video_percentage' => '100',
4898 ];
4899
4900 $video_data['video'] = [
4901 'id' => '1',
4902 'title' => 'SureTriggers Is Here 🎉 The Easiest Automation Builder WordPress Websites & Apps',
4903 'type' => 'youtube',
4904 'external_id' => '-cYbNYgylLs',
4905 'attachment_id' => '0',
4906 'post_id' => '127',
4907 'src' => 'https://www.youtube.com/watch?v=-cYbNYgylLs',
4908 'created_by' => '1',
4909 'created_at' => '2022-11-10 00:28:25',
4910 'updated_at' => '2022-11-10 00:34:40',
4911 'deleted_at' => '',
4912 ];
4913
4914 $video_data['media']['tag'] = [
4915 'Demo',
4916 'Demo1',
4917 ];
4918
4919 $mediahub_data = [
4920 'post_id' => '1',
4921 'activity_id' => '2',
4922 'post_author' => '1',
4923 'post_content' => 'New sample post...!',
4924 'post_title' => 'Sample Post',
4925 'post_excerpt' => 'sample',
4926 'post_status' => 'publish',
4927 'post_type' => 'pp_video_block',
4928 ];
4929
4930 $videos = ( new Video() )->all();
4931
4932 if ( count( $videos ) > 0 ) {
4933 if ( ! empty( $data['filter'] ) ) {
4934 $video_id = $data['filter']['pp_video']['value'];
4935 if ( -1 === $video_id ) {
4936 global $wpdb;
4937 $args = [
4938 'numberposts' => 1,
4939 'orderby' => 'rand',
4940 'order' => 'ASC',
4941 'post_type' => 'pp_video_block',
4942 ];
4943 $posts = get_posts( $args );
4944 $presto_post_video_id = $posts[0]->ID;
4945 $presto_video_id = $wpdb->get_var( $wpdb->prepare( 'SELECT * FROM wp_presto_player_videos WHERE post_id = %d', $presto_post_video_id ) );
4946 $video_id = $presto_video_id;
4947 }
4948 $pp_videos = ( new Video( $video_id ) )->toArray();
4949 if ( ! empty( $pp_videos ) ) {
4950 $video_data = [];
4951 $video_data['video'] = $pp_videos;
4952 if ( is_array( $video_data ) && array_key_exists( 'post_id', $video_data['video'] ) ) {
4953 $mediahub_data = WordPress::get_post_context( (int) $video_data['video']['post_id'] );
4954 $media_tags = get_the_terms( (int) $video_data['video']['post_id'], 'pp_video_tag' );
4955 if ( ! empty( $media_tags ) && is_array( $media_tags ) && isset( $media_tags[0] ) ) {
4956 $tag_name = [];
4957 foreach ( $media_tags as $tag ) {
4958 $tag_name[] = $tag->name;
4959 }
4960 $video_data['media']['tag'] = $tag_name;
4961 }
4962 }
4963 $video_data['pp_video'] = $video_id;
4964 $video_data['pp_video_percentage'] = isset( $data['filter']['pp_video_percentage']['value'] ) ? $data['filter']['pp_video_percentage']['value'] : '100';
4965 $user_data = WordPress::get_user_context( (int) $video_data['video']['created_by'] );
4966
4967 $context['response_type'] = 'live';
4968 }
4969 }
4970 }
4971
4972 $context['pluggable_data'] = array_merge( $user_data, $video_data, $mediahub_data );
4973
4974 return $context;
4975 }
4976
4977 /**
4978 * Get last data for trigger.
4979 *
4980 * @param array $data data.
4981 * @return array
4982 */
4983 public function search_member_press_triggers_last_data( $data ) {
4984 global $wpdb;
4985
4986 $term = $data['search_term'] ? $data['search_term'] : '';
4987
4988 $context = [];
4989 $context['response_type'] = 'sample';
4990
4991 $user_data = WordPress::get_sample_user_context();
4992
4993 $membership_data = [
4994 'membership_id' => '190',
4995 'membership_title' => 'Sample Membership',
4996 'amount' => '12.00',
4997 'total' => '12.00',
4998 'tax_amount' => '0.00',
4999 'tax_rate' => '0.00',
5000 'trans_num' => 't_63a03f3334f44',
5001 'status' => 'complete',
5002 'subscription_id' => '0',
5003 'membership_url' => site_url() . '/register/premium/',
5004 'membership_featured_image_id' => '521',
5005 'membership_featured_image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
5006 ];
5007
5008 $membership_id = (int) ( isset( $data['filter']['membership_id']['value'] ) ? $data['filter']['membership_id']['value'] : '-1' );
5009
5010 if ( in_array( $term, [ 'mepr-event-transaction-expired', 'mepr_subscription_transition_status', 'mepr-event-transaction-paused' ] ) ) {
5011 $status = 'cancelled';
5012 if ( 'mepr-event-transaction-paused' === $term ) {
5013 $status = 'suspended';
5014 }
5015 if ( $membership_id > 0 ) {
5016 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_subscriptions WHERE product_id= %s AND status=%s ORDER BY id DESC LIMIT 1", $membership_id, $status ) );
5017 } else {
5018 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_subscriptions WHERE status=%s ORDER BY id DESC LIMIT 1", $status ) );
5019
5020 }
5021 if ( ! empty( $subscription ) ) {
5022
5023 $membership_data = MemberPress::get_subscription_context( $subscription );
5024 $user_data = WordPress::get_user_context( $subscription->user_id );
5025
5026
5027 $context['response_type'] = 'live';
5028 }
5029 } elseif ( 'mepr-coupon-code-redeemed' === $term ) {
5030
5031 $coupon_id = (int) ( isset( $data['filter']['coupon_id']['value'] ) ? $data['filter']['coupon_id']['value'] : '-1' );
5032
5033 if ( $coupon_id > 0 ) {
5034 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE coupon_id= %s ORDER BY id DESC LIMIT 1", $coupon_id ) );
5035 } else {
5036 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE coupon_id!= %s ORDER BY id DESC LIMIT 1", '0' ) );
5037 }
5038
5039
5040 if ( ! empty( $subscription ) ) {
5041
5042 $membership_data = MemberPress::get_membership_context( $subscription );
5043 $user_data = WordPress::get_user_context( $subscription->user_id );
5044 $membership_data['coupon_id'] = $subscription->coupon_id;
5045 $membership_data['coupon'] = get_post( $subscription->coupon_id );
5046 $context['response_type'] = 'live';
5047 }
5048 } else {
5049
5050 if ( $membership_id > 0 ) {
5051
5052 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE product_id= %s ORDER BY id DESC LIMIT 1", $membership_id ) );
5053 } else {
5054 $subscription = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mepr_transactions ORDER BY id DESC LIMIT 1" );
5055 }
5056
5057
5058 if ( ! empty( $subscription ) ) {
5059
5060 $membership_data = MemberPress::get_membership_context( $subscription );
5061 $user_data = WordPress::get_user_context( $subscription->user_id );
5062
5063
5064 $context['response_type'] = 'live';
5065 }
5066 }
5067
5068
5069 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5070 return $context;
5071 }
5072
5073 /**
5074 * Get last data for trigger.
5075 *
5076 * @param array $data data.
5077 * @return array
5078 */
5079 public function search_wishlist_member_triggers_last_data( $data ) {
5080 global $wpdb;
5081
5082 $context = [];
5083 $context['response_type'] = 'sample';
5084
5085 $user_data = WordPress::get_sample_user_context();
5086
5087 $membership_data = [
5088 'membership_level_id' => '1',
5089 'membership_level_name' => 'Sample Membership Level',
5090 ];
5091
5092 $membership_level_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
5093
5094 if ( $membership_level_id > 0 ) {
5095 $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 ) );
5096 } else {
5097 $membership = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}wlm_userlevels ORDER BY id DESC LIMIT 1" );
5098 }
5099 if ( ! empty( $membership ) ) {
5100 $membership_data = WishlistMember::get_membership_detail_context( (int) $membership->level_id, (int) $membership->user_id );
5101 $user_data = WordPress::get_user_context( $membership->user_id );
5102
5103 $context['response_type'] = 'live';
5104 }
5105
5106 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5107
5108 return $context;
5109 }
5110
5111 /**
5112 * Get last data for trigger.
5113 *
5114 * @param array $data data.
5115 * @return array
5116 */
5117 public function search_peepso_triggers_last_data( $data ) {
5118 global $wpdb;
5119
5120 $context = [];
5121 $context['response_type'] = 'sample';
5122
5123 $user_data = WordPress::get_sample_user_context();
5124
5125 $post_data = [
5126 'post_id' => '1',
5127 'activity_id' => '2',
5128 'post_author' => '1',
5129 'post_content' => 'New sample post...!',
5130 'post_title' => 'Sample Post',
5131 'post_excerpt' => 'sample',
5132 'post_status' => 'publish',
5133 'post_type' => 'peepso-post',
5134 ];
5135
5136 $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" );
5137
5138 if ( ! empty( $post ) ) {
5139 $post_data = PeepSo::get_pp_activity_context( (int) $post->act_external_id, (int) $post->act_id );
5140 $user_data = WordPress::get_user_context( $post->act_owner_id );
5141
5142 $context['response_type'] = 'live';
5143 }
5144
5145 $context['pluggable_data'] = array_merge( $user_data, $post_data );
5146
5147 return $context;
5148 }
5149
5150 /**
5151 * Get last data for Peepso User triggers.
5152 *
5153 * @param array $data data.
5154 * @return array
5155 */
5156 public function search_peepso_user_triggers_last_data( $data ) {
5157 global $wpdb;
5158
5159 $context = [];
5160 $context['response_type'] = 'sample';
5161 $term = $data['search_term'];
5162
5163 if ( ! class_exists( 'PeepSoUser' ) ) {
5164 return [];
5165 }
5166
5167 if ( 'user_follows_member' === $term || 'user_gains_follower' === $term ) {
5168 $member_id = $data['filter']['follow_user_id']['value'];
5169 if ( -1 === $member_id ) {
5170 $followers = $wpdb->get_results(
5171 $wpdb->prepare(
5172 "SELECT * FROM
5173 {$wpdb->prefix}peepso_user_followers WHERE uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5174 1
5175 )
5176 );
5177 } else {
5178 $followers = $wpdb->get_results(
5179 $wpdb->prepare(
5180 "SELECT * FROM
5181 {$wpdb->prefix}peepso_user_followers WHERE uf_passive_user_id = %d AND
5182 uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5183 $member_id,
5184 1
5185 )
5186 );
5187 }
5188 } elseif ( 'user_unfollows_member' === $term || 'user_loses_follower' === $term ) {
5189 $member_id = $data['filter']['follow_user_id']['value'];
5190 if ( -1 === $member_id ) {
5191 $followers = $wpdb->get_results(
5192 $wpdb->prepare(
5193 "SELECT * FROM
5194 {$wpdb->prefix}peepso_user_followers WHERE uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5195 0
5196 )
5197 );
5198 } else {
5199 $followers = $wpdb->get_results(
5200 $wpdb->prepare(
5201 "SELECT * FROM
5202 {$wpdb->prefix}peepso_user_followers WHERE uf_passive_user_id = %d AND
5203 uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5204 $member_id,
5205 0
5206 )
5207 );
5208 }
5209 } elseif ( 'user_updates_avatar' === $term ) {
5210 $followers = $wpdb->get_results(
5211 $wpdb->prepare(
5212 "SELECT * FROM
5213 {$wpdb->prefix}peepso_users WHERE usr_avatar_custom = %d AND usr_role = %s
5214 ORDER BY usr_last_activity DESC LIMIT 1",
5215 1,
5216 'member'
5217 )
5218 );
5219 } elseif ( 'user_updates_specific_profile_field' === $term ) {
5220 $field_id = $data['filter']['user_profile_field_id']['value'];
5221 $followers = $wpdb->get_results(
5222 $wpdb->prepare(
5223 "SELECT * FROM
5224 {$wpdb->prefix}peepso_users WHERE usr_role = %s
5225 ORDER BY usr_last_activity DESC LIMIT 1",
5226 'member'
5227 )
5228 );
5229 }
5230 if ( 'user_updates_avatar' === $term ) {
5231 if ( ! empty( $followers ) ) {
5232 $context_data['user'] = WordPress::get_user_context( $followers[0]->usr_id );
5233 $context['response_type'] = 'live';
5234 } else {
5235 $context_data['user'] = WordPress::get_sample_user_context();
5236 }
5237 } elseif ( 'user_updates_specific_profile_field' === $term ) {
5238 if ( ! empty( $followers ) ) {
5239 $user = PeepSoUser::get_instance( $followers[0]->usr_id );
5240 $user->profile_fields->load_fields();
5241 $user_fields = $user->profile_fields->get_fields();
5242 foreach ( $user_fields as $key => $value ) {
5243 $val = get_user_meta( $followers[0]->usr_id, $value->key, true );
5244 if ( '' != $val ) {
5245 $context_data[ $value->title ] = $val;
5246 }
5247 }
5248 $curruser = get_userdata( $followers[0]->usr_id );
5249 $context_data['user_id'] = $followers[0]->usr_id;
5250 $context_data['user_email'] = $user->get_email();
5251 $context_data['avatar_url'] = $user->get_avatar();
5252 $context_data['profile_url'] = $user->get_profileurl();
5253 $context_data['about_me'] = get_user_meta( $followers[0]->usr_id, 'description', true );
5254 if ( $curruser instanceof \WP_User ) {
5255 $context_data['website'] = $curruser->user_url;
5256 }
5257 $context_data['role'] = $user->get_user_role();
5258 $context['response_type'] = 'live';
5259 } else {
5260 $context_data['user'] = WordPress::get_sample_user_context();
5261 }
5262 } else {
5263 if ( ! empty( $followers ) ) {
5264 $context_data['follower_user'] = WordPress::get_user_context( $followers[0]->uf_active_user_id );
5265 $context_data['following_user'] = WordPress::get_user_context( $followers[0]->uf_passive_user_id );
5266 $context['response_type'] = 'live';
5267 } else {
5268 $context_data['follower_user'] = WordPress::get_sample_user_context();
5269 $context_data['following_user'] = WordPress::get_sample_user_context();
5270 }
5271 }
5272
5273 $context['pluggable_data'] = $context_data;
5274
5275 return $context;
5276 }
5277
5278 /**
5279 * Search Peepso User Profile Fields list.
5280 *
5281 * @param array $data data.
5282 * @return array
5283 */
5284 public function search_peepso_profile_field_list( $data ) {
5285 $options = [];
5286 if ( ! class_exists( 'PeepSoUser' ) ) {
5287 return [];
5288 }
5289 $options = [
5290 [
5291 'label' => 'Allow others to "like" my profile',
5292 'value' => 'peepso_is_profile_likable',
5293 ],
5294 [
5295 'label' => 'Hide my birthday year',
5296 'value' => 'peepso_hide_birthday_year',
5297 ],
5298 [
5299 'label' => 'Who can see my profile',
5300 'value' => 'usr_profile_acc',
5301 ],
5302 [
5303 'label' => 'Who can post on my profile',
5304 'value' => 'peepso_profile_post_acc',
5305 ],
5306 [
5307 'label' => "Don't show my online status",
5308 'value' => 'peepso_hide_online_status',
5309 ],
5310 [
5311 'label' => 'My timezone',
5312 'value' => 'peepso_gmt_offset',
5313 ],
5314 ];
5315 $peepsouser = PeepSoUser::get_instance( 0 );
5316 $peepsouser->profile_fields->load_fields();
5317 $fields = $peepsouser->profile_fields->get_fields();
5318 foreach ( $fields as $field ) {
5319 if ( 1 == $field->published ) {
5320 $options[] = [
5321 'label' => $field->title,
5322 'value' => $field->id,
5323 ];
5324 }
5325 }
5326 return [
5327 'options' => $options,
5328 'hasMore' => false,
5329 ];
5330 }
5331
5332 /**
5333 * Search Peepso Roles list.
5334 *
5335 * @param array $data data.
5336 * @return array
5337 */
5338 public function search_peepso_role_list( $data ) {
5339 $options = [
5340 [
5341 'label' => 'Community Member',
5342 'value' => 'member',
5343 ],
5344 [
5345 'label' => 'Community Moderator',
5346 'value' => 'moderator',
5347 ],
5348 [
5349 'label' => 'Community Administrator',
5350 'value' => 'admin',
5351 ],
5352 [
5353 'label' => 'Banned',
5354 'value' => 'ban',
5355 ],
5356 [
5357 'label' => 'Pending user email verification',
5358 'value' => 'register',
5359 ],
5360 [
5361 'label' => 'Pending admin approval',
5362 'value' => 'verified',
5363 ],
5364 ];
5365 return [
5366 'options' => $options,
5367 'hasMore' => false,
5368 ];
5369 }
5370
5371 /**
5372 * Search Peepso Users list.
5373 *
5374 * @param array $data data.
5375 * @return array
5376 */
5377 public function search_peepso_follow_user_list( $data ) {
5378 $options = [];
5379 global $wpdb;
5380 $users = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}peepso_users", ARRAY_A );
5381 if ( count( $users ) > 0 ) {
5382 foreach ( $users as $user ) {
5383 $user_by_id = get_user_by( 'id', $user['usr_id'] );
5384 if ( $user_by_id instanceof \WP_User ) {
5385 $options[] = [
5386 'label' => sprintf( '%s %s [%s]', $user_by_id->last_name, $user_by_id->first_name, $user_by_id->user_email ),
5387 'value' => $user['usr_id'],
5388 ];
5389 } else {
5390 $options[] = [
5391 'label' => '#' . $user['usr_id'],
5392 'value' => $user['usr_id'],
5393 ];
5394 }
5395 }
5396 }
5397 return [
5398 'options' => $options,
5399 'hasMore' => false,
5400 ];
5401 }
5402
5403 /**
5404 * Get last data for trigger
5405 *
5406 * @param array $data data.
5407 * @return array
5408 */
5409 public function search_restrict_content_pro_triggers_last_data( $data ) {
5410 $context = [];
5411 $context['response_type'] = 'sample';
5412
5413 $user_data = WordPress::get_sample_user_context();
5414
5415 $membership_data = [
5416 'membership_level_id' => '190',
5417 'membership_level' => 'Sample Membership',
5418 'membership_initial_payment' => '0.00',
5419 'membership_recurring_payment' => '0.00',
5420 'membership_expiry_date' => 'January 22, 2023',
5421 ];
5422
5423 $customer_id = (int) ( isset( $data['filter']['membership_customer_id']['value'] ) ? $data['filter']['membership_customer_id']['value'] : '-1' );
5424 $membership_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
5425
5426 $args = [];
5427 if ( 'membership_purchased' === $data['search_term'] ) {
5428 $args = [
5429 'status' => 'active',
5430 'number' => 1,
5431 'orderby' => 'id',
5432 ];
5433 } elseif ( 'membership_cancelled' === $data['search_term'] ) {
5434 $args = [
5435 'status' => 'cancelled',
5436 'number' => 1,
5437 'orderby' => 'id',
5438 ];
5439 } elseif ( 'membership_expired' === $data['search_term'] ) {
5440 $args = [
5441 'status' => 'expired',
5442 'number' => 1,
5443 'orderby' => 'id',
5444 ];
5445 }
5446
5447 if ( 'membership_expired' === $data['search_term'] && -1 !== $customer_id ) {
5448 $args['customer_id'] = $customer_id;
5449 }
5450
5451 if ( -1 !== $membership_id ) {
5452 $args['object_id'] = $membership_id;
5453 }
5454
5455 $memberships = rcp_get_memberships( $args );
5456 if ( count( $memberships ) > 0 ) {
5457 $membership_data = RestrictContent::get_rcp_membership_detail_context( $memberships[0] );
5458 $user_data = WordPress::get_user_context( $memberships[0]->get_user_id() );
5459
5460 $context['response_type'] = 'live';
5461 }
5462
5463 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5464
5465 return $context;
5466 }
5467
5468 /**
5469 * Get last data for trigger
5470 *
5471 * @param array $data data.
5472 * @return array
5473 */
5474 public function search_events_calendar_triggers_last_data( $data ) {
5475 $context = [];
5476 $context['response_type'] = 'sample';
5477
5478 $event_data = [
5479 'event_id' => 1,
5480 'event' => [
5481 'ID' => 58,
5482 'post_author' => 1,
5483 'post_date' => '2023-01-19 09:27:58',
5484 'post_date_gmt' => '2023-01-19 09:27:58',
5485 'post_content' => '',
5486 'post_title' => 'New event',
5487 'post_excerpt' => '',
5488 'post_status' => 'publish',
5489 'comment_status' => 'open',
5490 'ping_status' => 'closed',
5491 'post_password' => '',
5492 'post_name' => 'new-event',
5493 'to_ping' => '',
5494 'pinged' => '',
5495 'post_modified' => '2023-01-19 09:44:25',
5496 'post_modified_gmt' => '2023-01-19 09:44:25',
5497 'post_content_filtered' => '',
5498 'post_parent' => 0,
5499 'guid' => 'http://connector.com/?post_type=tribe_events&#038;p=58',
5500 'menu_order' => -1,
5501 'post_type' => 'tribe_events',
5502 'post_mime_type' => '',
5503 'comment_count' => 0,
5504 'filter' => 'raw',
5505 ],
5506 'attendies' => [
5507 'order_id' => 68,
5508 'purchaser_name' => 'John Doe',
5509 'purchaser_email' => 'john@test.com',
5510 'provider' => 'Tribe__Tickets__RSVP',
5511 'provider_slug' => 'rsvp',
5512 'purchase_time' => '2023-01-19 09:48:43',
5513 'optout' => 1,
5514 'ticket' => 'Prime',
5515 'attendee_id' => 68,
5516 'security' => '2cefc3b53e',
5517 'product_id' => 65,
5518 'check_in' => '',
5519 'order_status' => 'yes',
5520 'order_status_label' => 'Going',
5521 'user_id' => 1,
5522 'ticket_sent' => 1,
5523 'event_id' => 58,
5524 'ticket_name' => 'Prime',
5525 'holder_name' => 'John Doe',
5526 'holder_email' => 'john@test.com',
5527 'ticket_id' => 68,
5528 'qr_ticket_id' => 68,
5529 'security_code' => '2cefc3b53e',
5530 'attendee_meta' => '',
5531 'is_subscribed' => '',
5532 'is_purchaser' => 1,
5533 'ticket_exists' => 1,
5534 ],
5535 ];
5536
5537 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
5538 $term = $data['search_term'];
5539
5540 if ( 'event_register' === $term || 'attendee_registered_event' === $term ) {
5541 if ( -1 === $event_id ) {
5542 $args = [
5543 'numberposts' => 1,
5544 'orderby' => 'rand',
5545 'post_type' => 'tribe_events',
5546 ];
5547 $posts = get_posts( $args );
5548 $event_id = $posts[0]->ID;
5549 }
5550 $args = [
5551 'post_type' => 'tribe_rsvp_attendees',
5552 'orderby' => 'ID',
5553 'order' => 'DESC',
5554 'post_status' => 'publish',
5555 'numberposts' => 1,
5556 ];
5557
5558 if ( -1 !== $event_id ) {
5559 $args['meta_query'] = [
5560 [
5561 'key' => '_tribe_rsvp_event',
5562 'value' => $event_id,
5563 ],
5564 ];
5565 }
5566
5567 $attendees = get_posts( $args );
5568
5569 if ( count( $attendees ) > 0 ) {
5570 $attendee = $attendees[0];
5571 $attendee_id = $attendee->ID;
5572
5573 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
5574 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
5575
5576 $event_context = TheEventCalendar::get_event_context( $product_id, $order_id );
5577
5578 if ( ! empty( $event_context ) ) {
5579 $event_data = $event_context;
5580 $context['response_type'] = 'live';
5581 }
5582 } else {
5583 $args = [
5584 'post_type' => 'tec_tc_attendee',
5585 'orderby' => 'ID',
5586 'order' => 'DESC',
5587 'post_status' => 'publish',
5588 'numberposts' => 1,
5589 ];
5590 if ( -1 !== $event_id ) {
5591 $args['meta_query'] = [
5592 [
5593 'key' => '_tec_tickets_commerce_event',
5594 'value' => $event_id,
5595 ],
5596 ];
5597 }
5598 $attendees = get_posts( $args );
5599 $attendee = $attendees[0];
5600 $attendee_id = $attendee->ID;
5601 $product_id = get_post_meta( $attendee_id, '_tec_tickets_commerce_ticket', true );
5602 $order_id = $attendee_id;
5603 $event_context = TheEventCalendar::get_event_context( $product_id, $order_id );
5604 if ( ! empty( $event_context ) ) {
5605 $event_data = $event_context;
5606 $context['response_type'] = 'live';
5607 }
5608 }
5609 } elseif ( 'event_attends' === $term ) {
5610 if ( -1 == $event_id ) {
5611 $args = [
5612 'numberposts' => 1,
5613 'orderby' => 'rand',
5614 'post_type' => 'tribe_events',
5615 ];
5616 $posts = get_posts( $args );
5617 $event_id = $posts[0]->ID;
5618 }
5619 $args = [
5620 'post_type' => 'tribe_rsvp_attendees',
5621 'orderby' => 'ID',
5622 'order' => 'DESC',
5623 'post_status' => 'publish',
5624 'numberposts' => 1,
5625 'meta_query' => [
5626 'relation' => 'AND',
5627 [
5628 'key' => '_tribe_rsvp_checkedin',
5629 'value' => 1,
5630 'compare' => '=',
5631 ],
5632 [
5633 'key' => '_tribe_rsvp_event',
5634 'value' => $event_id,
5635 'compare' => '=',
5636 ],
5637 ],
5638 ];
5639 $attendees = get_posts( $args );
5640 if ( ! function_exists( 'tribe_tickets_get_attendees' ) ) {
5641 return [];
5642 }
5643 if ( ! empty( $attendees ) ) {
5644 $attendee = $attendees[0];
5645 $attendee_id = $attendee->ID;
5646 /**
5647 *
5648 * Ignore line
5649 *
5650 * @phpstan-ignore-next-line
5651 */
5652 $attendee_details = tribe_tickets_get_attendees( $attendee_id, 'rsvp_order' );
5653 foreach ( $attendee_details as $detail ) {
5654 if ( (int) $detail['attendee_id'] !== (int) $attendee_id ) {
5655 continue;
5656 }
5657 $attendee = $detail;
5658 }
5659 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
5660 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
5661 $event_context = TheEventCalendar::get_event_context( $product_id, $order_id );
5662 if ( ! empty( $event_context ) ) {
5663 $event_data = array_merge( $attendee, $event_context );
5664 $context['response_type'] = 'live';
5665 }
5666 } else {
5667 $order = [
5668 'order_id' => 7962,
5669 'purchaser_name' => 'bella4 bella4',
5670 'purchaser_email' => 'bella4@yopmail.com',
5671 'provider' => 'Tribe__Tickets__RSVP',
5672 'provider_slug' => 'rsvp',
5673 'purchase_time' => '2024 - 03 - 04 07:26:41',
5674 'optout' => 1,
5675 'ticket' => 'test test',
5676 'attendee_id' => 7962,
5677 'security' => 'eb3a2d7bc4',
5678 'product_id' => 7959,
5679 'check_in' => 1,
5680 'order_status' => 'yes',
5681 'order_status_label' => 'Going',
5682 'user_id' => 35,
5683 'ticket_sent' => 1,
5684 'event_id' => 7956,
5685 'ticket_name' => 'test test',
5686 'holder_name' => 'bella4 bella4',
5687 'holder_email' => 'bella4@yopmail.com',
5688 'ticket_id' => 7962,
5689 'qr_ticket_id' => 7962,
5690 'security_code' => 'eb3a2d7bc4',
5691 'is_purchaser' => 1,
5692 'ticket_exists' => 1,
5693 ];
5694 $event_data = array_merge( $order, $event_data );
5695 }
5696 } elseif ( 'event_attendee_wc_register' === $term ) {
5697 if ( ! class_exists( 'Tribe__Tickets__Tickets' ) ) {
5698 return [];
5699 }
5700 if ( -1 === $event_id ) {
5701 $args = [
5702 'numberposts' => 1,
5703 'orderby' => 'rand',
5704 'post_type' => 'tribe_events',
5705 ];
5706 $posts = get_posts( $args );
5707 $event_id = $posts[0]->ID;
5708 }
5709 $event_ticket_id = (int) ( isset( $data['filter']['event_ticket_id']['value'] ) ? $data['filter']['event_ticket_id']['value'] : '-1' );
5710 if ( -1 === $event_ticket_id ) {
5711 $tickets = \Tribe__Tickets__Tickets::get_all_event_tickets( $event_id );
5712 $random_key = array_rand( $tickets );
5713 $random_value = $tickets[ $random_key ];
5714 $event_ticket_id = $random_value;
5715 }
5716 $args = [
5717 'post_type' => 'tribe_wooticket',
5718 'orderby' => 'ID',
5719 'order' => 'DESC',
5720 'post_status' => 'publish',
5721 'meta_query' => [
5722 'relation' => 'AND',
5723 [
5724 'key' => '_tribe_wooticket_event',
5725 'value' => $event_id,
5726 'compare' => '=',
5727 ],
5728 [
5729 'key' => '_tribe_wooticket_product',
5730 'value' => $event_ticket_id,
5731 'compare' => '=',
5732 ],
5733 ],
5734 ];
5735
5736 $woo_attendees = get_posts( $args );
5737 if ( ! empty( $woo_attendees ) ) {
5738 $attendee = $woo_attendees[0];
5739 $attendee_id = $attendee->ID;
5740 /**
5741 *
5742 * Ignore line
5743 *
5744 * @phpstan-ignore-next-line
5745 */
5746 $attendee_details = tribe_tickets_get_attendees( $attendee_id, 'rsvp_order' );
5747 foreach ( $attendee_details as $detail ) {
5748 if ( (int) $detail['attendee_id'] !== (int) $attendee_id ) {
5749 continue;
5750 }
5751 $attendee = $detail;
5752 }
5753 $order_id = get_post_meta( $attendee_id, '_tribe_wooticket_order', true );
5754 $product_id = get_post_meta( $attendee_id, '_tribe_wooticket_product', true );
5755 $event_context = TheEventCalendar::get_event_context( $product_id, $order_id );
5756 if ( ! empty( $event_context ) ) {
5757 $event_data = $event_context;
5758 $context['response_type'] = 'live';
5759 }
5760 $context['response_type'] = 'live';
5761 }
5762 }
5763
5764 $context['pluggable_data'] = $event_data;
5765
5766 return $context;
5767 }
5768
5769 /**
5770 * Get last data for trigger
5771 *
5772 * @param array $data data.
5773 * @return array
5774 */
5775 public function search_woo_commerce_triggers_last_data( $data ) {
5776 $context = [];
5777 $context['response_type'] = 'sample';
5778 $context['pluggable_data'] = [];
5779 $user_data = WordPress::get_sample_user_context();
5780
5781 $product_data['product'] = [
5782 'id' => '169',
5783 'name' => 'Sample Product',
5784 'description' => 'This is description of sample product.',
5785 'short_description' => 'This is short description of sample product.',
5786 'image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
5787 'slug' => 'sample-product',
5788 'status' => 'publish',
5789 'type' => 'simple',
5790 'price' => '89',
5791 'featured' => '0',
5792 'sku' => 'hoodie-blue-sm',
5793 'regular_price' => '90',
5794 'sale_price' => '89',
5795 'total_sales' => '21',
5796 'category' => 'Uncategorized',
5797 'tags' => 'sample, new, 2022',
5798 ];
5799
5800 $comment_data = [
5801 'comment_id' => '1',
5802 'comment' => 'This is a sample comment..!',
5803 'comment_author' => 'testsure',
5804 'comment_date' => '2023-06-23 10:10:40',
5805 'comment_author_email' => 'testsure@example.com',
5806 ];
5807
5808 $order_data = [
5809 'order_id' => '500',
5810 'total_order_value' => '45',
5811 'currency' => 'USD',
5812 'shipping_total' => '5',
5813 'order_payment_method' => 'cod',
5814 'billing_firstname' => 'John',
5815 'billing_lastname' => 'Doe',
5816 'billing_company' => 'BSF',
5817 'billing_address_1' => '1004 Beaumont',
5818 'billing_address_2' => '',
5819 'billing_city' => 'Casper',
5820 'billing_state' => 'Wyoming',
5821 'billing_state_name' => 'Wyoming',
5822 'billing_postcode' => '82601',
5823 'billing_country' => 'US',
5824 'billing_country_name' => 'US',
5825 'billing_email' => 'john_doe@gmail.com',
5826 'billing_phone' => '(307) 7626541',
5827 'shipping_firstname' => 'John',
5828 'shipping_lastname' => 'Doe',
5829 'shipping_company' => 'BSF',
5830 'shipping_address_1' => '1004 Beaumont',
5831 'shipping_address_2' => '',
5832 'shipping_city' => 'Casper',
5833 'shipping_state' => 'Wyoming',
5834 'shipping_state_name' => 'Wyoming',
5835 'shipping_postcode' => '82601',
5836 'shipping_country' => 'US',
5837 'shipping_country_name' => 'US',
5838 'coupon_codes' => 'e3mstekq, f24sjakb',
5839 'total_items_in_order' => '1',
5840 'user_id' => '1',
5841 ];
5842
5843 $variation_data = [
5844 'product_variation_id' => '626',
5845 'product_variation' => 'Color: Silver',
5846 ];
5847
5848 $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
5849
5850 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
5851 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
5852
5853 $order_status = ( isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1 );
5854 $from_order_status = ( isset( $data['filter']['from_status']['value'] ) ? $data['filter']['from_status']['value'] : -1 );
5855 $order_note_type = ( isset( $data['filter']['note_type']['value'] ) ? $data['filter']['note_type']['value'] : -1 );
5856
5857 if ( in_array( $term, [ 'product_added_to_cart', 'product_viewed' ], true ) ) {
5858 if ( -1 === $product_id ) {
5859 $args = [
5860 'post_type' => 'product',
5861 'orderby' => 'ID',
5862 'order' => 'DESC',
5863 'post_status' => 'publish',
5864 'numberposts' => 1,
5865 ];
5866 $products = get_posts( $args );
5867
5868 if ( count( $products ) > 0 ) {
5869 $product_id = $products[0]->ID;
5870 }
5871 }
5872
5873 if ( -1 !== $product_id ) {
5874 $post = get_post( $product_id );
5875 $user_data = WordPress::get_user_context( $post->post_author );
5876 $product_data['product_id'] = $product_id;
5877 $product_data['product'] = WooCommerce::get_product_context( $product_id );
5878 $terms = get_the_terms( $product_id, 'product_cat' );
5879 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
5880 $cat_name = [];
5881 foreach ( $terms as $cat ) {
5882 $cat_name[] = $cat->name;
5883 }
5884 $product_data['product']['category'] = implode( ', ', $cat_name );
5885 }
5886 $terms_tags = get_the_terms( $product_id, 'product_tag' );
5887 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
5888 $tag_name = [];
5889 foreach ( $terms_tags as $tag ) {
5890 $tag_name[] = $tag->name;
5891 }
5892 $product_data['product']['tag'] = implode( ', ', $tag_name );
5893 }
5894 unset( $product_data['product']['id'] ); //phpcs:ignore
5895 $context['response_type'] = 'live';
5896 }
5897
5898 if ( 'product_added_to_cart' === $term ) {
5899 $product_data['product_quantity'] = 1;
5900 }
5901
5902 $context['pluggable_data'] = array_merge( $product_data, $user_data );
5903
5904 } elseif ( 'product_reviewed' === $term ) {
5905 $comment_args = [
5906 'number' => 1,
5907 'type' => 'review',
5908 'orderby' => 'comment_ID',
5909 'post_id' => -1 !== $product_id ? $product_id : 0,
5910 ];
5911
5912 $comments = get_comments( $comment_args );
5913
5914 if ( count( $comments ) > 0 ) {
5915 $comment = $comments[0];
5916 $comment_data = [
5917 'comment_id' => $comment->comment_ID,
5918 'comment' => $comment->comment_content,
5919 'comment_author' => $comment->comment_author,
5920 'comment_date' => $comment->comment_date,
5921 'comment_author_email' => $comment->comment_author_email,
5922 ];
5923 $product_data = WooCommerce::get_product_context( $comment->comment_post_ID );
5924 if ( is_object( $comment ) ) {
5925 $terms = get_the_terms( (int) $comment->comment_post_ID, 'product_cat' );
5926 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
5927 $cat_name = [];
5928 foreach ( $terms as $cat ) {
5929 $cat_name[] = $cat->name;
5930 }
5931 $product_data['product']['category'] = implode( ', ', $cat_name );
5932 }
5933 $terms_tags = get_the_terms( (int) $comment->comment_post_ID, 'product_tag' );
5934 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
5935 $tag_name = [];
5936 foreach ( $terms_tags as $tag ) {
5937 $tag_name[] = $tag->name;
5938 }
5939 $product_data['product']['tag'] = implode( ', ', $tag_name );
5940 }
5941 }
5942 $user_data = WordPress::get_user_context( $comment->user_id );
5943 $context['response_type'] = 'live';
5944 }
5945
5946 $context['pluggable_data'] = array_merge( $product_data, $user_data, $comment_data );
5947
5948 } elseif ( 'product_purchased' === $term ) {
5949 $order_id = 0;
5950 $product_data['quantity'] = '1';
5951 if ( -1 !== $product_id ) {
5952 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
5953 if ( count( $order_ids ) > 0 ) {
5954 $order_id = $order_ids[0];
5955 }
5956 } else {
5957 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
5958 if ( count( $orders ) > 0 ) {
5959 $order_id = $orders[0]->get_id();
5960 }
5961 }
5962
5963 if ( 0 !== $order_id ) {
5964 $order = wc_get_order( $order_id );
5965
5966 if ( $order ) {
5967 $user_id = $order->get_customer_id();
5968 $items = $order->get_items();
5969
5970 $product_ids = [];
5971
5972 $iteration = 0;
5973 foreach ( $items as $item ) {
5974 if ( method_exists( $item, 'get_product_id' ) ) {
5975 $item_id = $item->get_product_id();
5976 if ( -1 === $product_id && 0 === $iteration ) {
5977 $product_ids[] = $item_id;
5978 break;
5979 } elseif ( $item_id === $product_id ) {
5980 $product_ids[] = $item_id;
5981 break;
5982 }
5983 }
5984
5985 $iteration++;
5986 }
5987 $order_data = WooCommerce::get_order_context( $order_id );
5988 $user_data = WordPress::get_user_context( $user_id );
5989 $order_data['total_items_in_order'] = count( $product_ids );
5990 $product_data = [];
5991 foreach ( $product_ids as $key => $product_id ) {
5992 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
5993 $terms = get_the_terms( $product_id, 'product_cat' );
5994 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
5995 $cat_name = [];
5996 foreach ( $terms as $cat ) {
5997 $cat_name[] = $cat->name;
5998 }
5999 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
6000 }
6001 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6002 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6003 $tag_name = [];
6004 foreach ( $terms_tags as $tag ) {
6005 $tag_name[] = $tag->name;
6006 }
6007 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
6008 }
6009 $product = wc_get_product( $product_id );
6010 /**
6011 *
6012 * Ignore line
6013 *
6014 * @phpstan-ignore-next-line
6015 */
6016 if ( $product->is_downloadable() ) {
6017 /**
6018 *
6019 * Ignore line
6020 *
6021 * @phpstan-ignore-next-line
6022 */
6023 foreach ( $product->get_downloads() as $key_download_id => $download ) {
6024 $download_name = $download->get_name();
6025 $download_link = $download->get_file();
6026 $download_id = $download->get_id();
6027 $download_type = $download->get_file_type();
6028 $download_ext = $download->get_file_extension();
6029 $product_data[ 'product' . $key ]['download'] = [
6030 'download_name' => $download_name,
6031 'download_link' => $download_link,
6032 'download_id' => $download_id,
6033 'download_type' => $download_type,
6034 'download_ext' => $download_ext,
6035 ];
6036 }
6037 }
6038 }
6039 $context['response_type'] = 'live';
6040 }
6041 }
6042
6043 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
6044
6045 } elseif ( 'variable_product_purchased' === $term ) {
6046 $product_variation_id = (int) ( isset( $data['filter']['product_variation_id']['value'] ) ? $data['filter']['product_variation_id']['value'] : -1 );
6047 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
6048
6049 foreach ( $order_ids as $order_id ) {
6050 $order = wc_get_order( $order_id );
6051
6052 if ( $order ) {
6053 $user_id = $order->get_customer_id();
6054 $items = $order->get_items();
6055 $product_variations = [];
6056
6057 $iteration = 0;
6058 foreach ( $items as $item ) {
6059 if ( method_exists( $item, 'get_variation_id' ) ) {
6060 $variation_id = $item->get_variation_id();
6061 if ( -1 === $product_variation_id && 0 === $iteration ) {
6062 $product_variations[] = $variation_id;
6063 break;
6064 } elseif ( $variation_id === $product_variation_id ) {
6065 $product_variations[] = $variation_id;
6066 break;
6067 }
6068 }
6069
6070 $iteration++;
6071 }
6072
6073 if ( count( $product_variations ) > 0 ) {
6074 $product_data = WooCommerce::get_product_context( $product_variation_id );
6075 $order_data = WooCommerce::get_order_context( $order_id );
6076 $user_data = WordPress::get_user_context( $user_id );
6077 $variation_data = [
6078 'product_variation_id' => $product_variations[0],
6079 'product_variation' => get_the_excerpt( $product_variations[0] ),
6080 ];
6081
6082 $context['response_type'] = 'live';
6083 break;
6084 }
6085 }
6086 }
6087
6088 $context['pluggable_data'] = array_merge( $order_data, $user_data, $variation_data );
6089
6090 } elseif ( 'variable_subscription_purchased' === $term ) {
6091 $product_data['quantity'] = '1';
6092 $product_data['product_name'] = 'Sample Product';
6093 $product_data['billing_period'] = '2021-2022';
6094
6095 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
6096
6097 $subscription_order_id = 0;
6098 $order_ids = [];
6099
6100 if ( -1 !== $product_id ) {
6101 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
6102
6103 } else {
6104 $orders = wc_get_orders( [] );
6105 if ( count( $orders ) > 0 ) {
6106 $order_ids[] = $orders[0]->get_id();
6107 }
6108 }
6109
6110 foreach ( $order_ids as $order_id ) {
6111 $query_args = [
6112 'post_type' => 'shop_subscription',
6113 'orderby' => 'ID',
6114 'order' => 'DESC',
6115 'post_status' => 'wc-active',
6116 'posts_per_page' => 1,
6117 'post_parent' => $order_id,
6118 ];
6119 $query_result = new WP_Query( $query_args );
6120 $subscription_orders = $query_result->get_posts();
6121
6122 if ( count( $subscription_orders ) > 0 ) {
6123 $subscription_order_id = $subscription_orders[0]->ID;
6124 break;
6125 }
6126 }
6127
6128 if ( 0 !== $subscription_order_id ) {
6129 $subscription = wcs_get_subscription( $subscription_order_id );
6130 if ( $subscription instanceof WC_Subscription ) {
6131 $last_order_id = $subscription->get_last_order();
6132 if ( ! empty( $last_order_id ) && $last_order_id === $subscription->get_parent_id() ) {
6133 $user_id = wc_get_order( $last_order_id )->get_customer_id();
6134 $items = $subscription->get_items();
6135
6136 foreach ( $items as $item ) {
6137 $product = $item->get_product();
6138 if ( class_exists( '\WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product ) ) {
6139 if ( $product->is_type( [ 'subscription', 'subscription_variation', 'variable-subscription' ] ) ) {
6140
6141 $product_data = WooCommerce::get_variable_subscription_product_context( $item, $last_order_id );
6142 $user_data = WordPress::get_user_context( $user_id );
6143
6144 $context['response_type'] = 'live';
6145 $context['pluggable_data'] = array_merge( $product_data, $user_data );
6146 }
6147 }
6148 }
6149 }
6150 }
6151 }
6152 } elseif ( 'order_created' === $term ) {
6153 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
6154 $order_id = '';
6155 if ( count( $orders ) > 0 ) {
6156 $order_id = $orders[0]->get_id();
6157 $order = wc_get_order( $order_id );
6158 $user_id = $order->get_customer_id();
6159 $order_sample_data = array_merge(
6160 WooCommerce::get_order_context( $order_id ),
6161 WordPress::get_user_context( $user_id )
6162 );
6163 $context['response_type'] = 'live';
6164 }
6165
6166 $context['pluggable_data'] = $order_sample_data;
6167
6168 } elseif ( 'order_status_changed' === $term ) {
6169 if ( -1 == $order_status ) {
6170 $args = [
6171 'numberposts' => 1,
6172 'orderby' => 'date',
6173 'order' => 'DESC',
6174 ];
6175 } else {
6176 $args = [
6177 'status' => [ $order_status ],
6178 'numberposts' => 1,
6179 'orderby' => 'date',
6180 'order' => 'DESC',
6181 ];
6182 }
6183 $orders = wc_get_orders( $args );
6184 $order_id = '';
6185 if ( count( $orders ) > 0 ) {
6186 $order_id = $orders[0]->get_id();
6187 $order = wc_get_order( $order_id );
6188 $user_id = $order->get_customer_id();
6189 $items = $order->get_items();
6190 $product_ids = [];
6191 foreach ( $items as $item ) {
6192 $product_ids[] = $item['product_id'];
6193 }
6194 $product_data = [];
6195 foreach ( $product_ids as $key => $product_id ) {
6196 /**
6197 *
6198 * Ignore line
6199 *
6200 * @phpstan-ignore-next-line
6201 */
6202 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
6203 /**
6204 *
6205 * Ignore line
6206 *
6207 * @phpstan-ignore-next-line
6208 */
6209 $terms = get_the_terms( $product_id, 'product_cat' );
6210 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6211 $cat_name = [];
6212 foreach ( $terms as $cat ) {
6213 $cat_name[] = $cat->name;
6214 }
6215 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
6216 }
6217 /**
6218 *
6219 * Ignore line
6220 *
6221 * @phpstan-ignore-next-line
6222 */
6223 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6224 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6225 $tag_name = [];
6226 foreach ( $terms_tags as $tag ) {
6227 $tag_name[] = $tag->name;
6228 }
6229 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
6230 }
6231 }
6232 $order_sample_data = array_merge(
6233 WooCommerce::get_order_context( $order_id ),
6234 $product_data
6235 );
6236 $order_sample_data['user'] = WordPress::get_user_context( $user_id );
6237 $order_sample_data['to_status'] = $order_status;
6238 $order_sample_data['from_status'] = $from_order_status;
6239 $context['response_type'] = 'live';
6240 }
6241
6242 $order_sample_data['to_status'] = $order_status;
6243 $order_sample_data['from_status'] = $from_order_status;
6244 $context['pluggable_data'] = $order_sample_data;
6245 } elseif ( 'order_note_added' === $term ) {
6246 global $wpdb;
6247 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type ='order_note' ORDER BY comment_ID DESC LIMIT 1" );
6248 $order_id = '';
6249 if ( ! empty( $result ) ) {
6250 $order_id = $result[0]->comment_post_ID;
6251 $order = wc_get_order( $order_id );
6252 if ( ! empty( $order ) ) {
6253 $user_id = $order->get_customer_id();
6254 $order_sample_data = array_merge(
6255 WooCommerce::get_order_context( $order_id ),
6256 WordPress::get_user_context( $user_id )
6257 );
6258 if ( -1 == $order_note_type ) {
6259 $args = [
6260 'order_id' => $order_id,
6261 'limit' => 1,
6262 ];
6263 } else {
6264 $args = [
6265 'order_id' => $order_id,
6266 'type' => $order_note_type,
6267 'limit' => 1,
6268 ];
6269 }
6270
6271 $notes = wc_get_order_notes( $args );
6272 if ( ! empty( $notes ) ) {
6273 $order_sample_data['note'] = [
6274 'id' => $notes[0]->id,
6275 'date' => $notes[0]->date_created,
6276 'author' => $notes[0]->added_by,
6277 'content' => $notes[0]->content,
6278 ];
6279 } else {
6280 $order_sample_data['note'] = [
6281 'id' => '1',
6282 'date' => [
6283 'date' => '2023-06-23 10:10:40',
6284 'timezone_type' => 1,
6285 'timezone' => '+00:00',
6286 ],
6287 'author' => 'admin',
6288 'content' => 'new note',
6289 ];
6290 }
6291 $order_sample_data['note_type'] = $order_note_type;
6292 $context['response_type'] = 'live';
6293 }
6294 } else {
6295 $order_sample_data['note'] = [
6296 'id' => '1',
6297 'date' => [
6298 'date' => '2023-06-23 10:10:40',
6299 'timezone_type' => 1,
6300 'timezone' => '+00:00',
6301 ],
6302 'author' => 'admin',
6303 'content' => 'new note',
6304 ];
6305 $order_sample_data['note_type'] = 'customer';
6306 }
6307 $context['pluggable_data'] = $order_sample_data;
6308 } elseif ( 'order_paid' === $term ) {
6309 $args = [
6310 'status' => [ 'completed' ],
6311 'numberposts' => 1,
6312 ];
6313 $orders = wc_get_orders( $args );
6314 $order_id = '';
6315 if ( count( $orders ) > 0 ) {
6316 $order_id = $orders[0]->get_id();
6317 $order = wc_get_order( $order_id );
6318 $user_id = $order->get_customer_id();
6319 $order_sample_data = array_merge(
6320 WooCommerce::get_order_context( $order_id ),
6321 WordPress::get_user_context( $user_id )
6322 );
6323 $context['response_type'] = 'live';
6324 }
6325 $context['pluggable_data'] = $order_sample_data;
6326 } elseif ( 'product_category_purchased' === $term ) {
6327 $product_category_id = (int) ( isset( $data['filter']['product_category_id']['value'] ) ? $data['filter']['product_category_id']['value'] : -1 );
6328 $args = [
6329 'post_status' => 'publish',
6330 'product_category_id' => [ $product_category_id ],
6331 'return' => 'ids',
6332 ];
6333 $products = wc_get_products( $args );
6334 if ( ! empty( $products ) ) {
6335 $order_id = 0;
6336 $product_data['quantity'] = '1';
6337 $orders = wc_get_orders(
6338 [
6339 'status' => 'any',
6340 ]
6341 );
6342 $filtered_orders = [];
6343 if ( ! empty( $orders ) ) {
6344 foreach ( $orders as $order ) {
6345 $order_items = $order->get_items();
6346 foreach ( $order_items as $item ) {
6347 if ( method_exists( $item, 'get_product_id' ) ) {
6348 $product_id = $item->get_product_id();
6349 }
6350 if ( is_array( $products ) && in_array( $product_id, $products ) ) {
6351 $filtered_orders[] = $order;
6352 break;
6353 }
6354 }
6355 }
6356 }
6357 if ( ! empty( $filtered_orders ) ) {
6358 if ( count( $filtered_orders ) > 0 ) {
6359 $order_id = $filtered_orders[0]->get_id();
6360 }
6361 if ( 0 !== $order_id ) {
6362 $order = wc_get_order( $order_id );
6363 if ( $order instanceof WC_Order ) {
6364 $user_id = $order->get_customer_id();
6365 $items = $order->get_items();
6366 $product_ids = [];
6367 $iteration = 0;
6368 foreach ( $items as $item ) {
6369 if ( method_exists( $item, 'get_product_id' ) ) {
6370 $item_id = $item->get_product_id();
6371 if ( -1 === $product_id && 0 === $iteration ) {
6372 $product_ids[] = $item_id;
6373 break;
6374 } elseif ( $item_id === $product_id ) {
6375 $product_ids[] = $item_id;
6376 break;
6377 }
6378 }
6379 $iteration++;
6380 }
6381 $order_data = WooCommerce::get_order_context( $order_id );
6382 $user_data = WordPress::get_user_context( $user_id );
6383 $order_data['total_items_in_order'] = count( $product_ids );
6384 $product_data = [];
6385 $category_ids = [];
6386 foreach ( $product_ids as $key => $product_id ) {
6387 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
6388 $terms = get_the_terms( $product_id, 'product_cat' );
6389 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6390 $cat_name = [];
6391 foreach ( $terms as $cat ) {
6392 $cat_name[] = $cat->name;
6393 $category_ids[] = $cat->term_id;
6394 }
6395 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
6396 }
6397 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6398 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6399 $tag_name = [];
6400 foreach ( $terms_tags as $tag ) {
6401 $tag_name[] = $tag->name;
6402 }
6403 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
6404 }
6405 $product = wc_get_product( $product_id );
6406 /**
6407 *
6408 * Ignore line
6409 *
6410 * @phpstan-ignore-next-line
6411 */
6412 if ( $product->is_downloadable() ) {
6413 /**
6414 *
6415 * Ignore line
6416 *
6417 * @phpstan-ignore-next-line
6418 */
6419 foreach ( $product->get_downloads() as $key_download_id => $download ) {
6420 $download_name = $download->get_name();
6421 $download_link = $download->get_file();
6422 $download_id = $download->get_id();
6423 $download_type = $download->get_file_type();
6424 $download_ext = $download->get_file_extension();
6425 $product_data[ 'product' . $key ]['download'] = [
6426 'download_name' => $download_name,
6427 'download_link' => $download_link,
6428 'download_id' => $download_id,
6429 'download_type' => $download_type,
6430 'download_ext' => $download_ext,
6431 ];
6432 }
6433 }
6434 }
6435 $context['response_type'] = 'live';
6436 }
6437 }
6438 }
6439 }
6440 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
6441 if ( ! empty( $category_ids ) ) {
6442 foreach ( $category_ids as $category_id ) {
6443 $context['pluggable_data']['product_category_id'] = $category_id;
6444 }
6445 } else {
6446 $context['pluggable_data']['product_category_id'] = 1;
6447 }
6448 }
6449
6450 return $context;
6451 }
6452
6453 /**
6454 * Search LMS data.
6455 *
6456 * @param array $data data.
6457 * @return array
6458 */
6459 public function search_lifter_lms_last_data( $data ) {
6460 global $wpdb;
6461 $post_type = $data['post_type'];
6462 $meta_key = '_is_complete';
6463 $trigger = $data['search_term'];
6464 $context = [];
6465 $post_id = -1;
6466
6467 if ( ! class_exists( 'LLMS_Section' ) ) {
6468 return [];
6469 }
6470
6471 if ( 'lifterlms_purchase_course' === $trigger ) {
6472 $product_type = 'course';
6473 $post_id = $data['filter']['course_id']['value'];
6474 } elseif ( 'lifterlms_purchase_membership' === $trigger ) {
6475 $product_type = 'membership';
6476 $post_id = $data['filter']['membership_id']['value'];
6477 } elseif ( 'lifterlms_cancel_membership' === $trigger ) {
6478 $product_type = 'membership';
6479 $post_id = $data['filter']['membership_id']['value'];
6480 } elseif ( 'lifterlms_lesson_completed' === $trigger ) {
6481 $post_id = $data['filter']['lesson']['value'];
6482 } elseif ( 'lifterlms_course_completed' === $trigger || 'lifterlms_course_enrolled' === $trigger || 'lifterlms_course_user_removed' === $trigger ) {
6483 $post_id = $data['filter']['course']['value'];
6484 } elseif ( 'lifterlms_section_completed' === $trigger ) {
6485 $post_id = $data['filter']['section']['value'];
6486 }
6487
6488 $where = 'postmeta.post_id = "' . $post_id . '" AND';
6489
6490 if ( 'llms_order' === $post_type ) {
6491 if ( -1 === $post_id ) {
6492 $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 ) );
6493 } else {
6494 $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 ) );
6495 }
6496 } elseif ( 'lifterlms_course_enrolled' === $trigger ) {
6497 if ( -1 === $post_id ) {
6498 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.post_id WHERE postmeta.meta_value='enrolled' AND postmeta.meta_key=%s AND posts.post_type=%s ORDER BY postmeta.meta_id DESC", '_status', $post_type ) );
6499 } else {
6500 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.post_id WHERE postmeta.post_id = %s AND postmeta.meta_value='enrolled' AND postmeta.meta_key=%s AND posts.post_type=%s ORDER BY postmeta.meta_id DESC", $post_id, '_status', $post_type ) );
6501 }
6502 } elseif ( 'lifterlms_course_user_removed' === $trigger ) {
6503 if ( -1 === $post_id ) {
6504 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.post_id WHERE postmeta.meta_value='cancelled' AND postmeta.meta_key=%s AND posts.post_type=%s ORDER BY postmeta.meta_id DESC", '_status', $post_type ) );
6505 } else {
6506 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.post_id WHERE postmeta.post_id = %s AND postmeta.meta_value='cancelled' AND postmeta.meta_key=%s AND posts.post_type=%s ORDER BY postmeta.meta_id DESC", $post_id, '_status', $post_type ) );
6507 }
6508 } elseif ( 'lifterlms_cancel_membership' === $trigger ) {
6509 if ( -1 === $post_id ) {
6510 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.post_id WHERE postmeta.meta_value='cancelled' AND postmeta.meta_key=%s AND posts.post_type=%s ORDER BY postmeta.meta_id DESC", '_status', $post_type ) );
6511 } else {
6512 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.post_id WHERE postmeta.post_id = %s AND postmeta.meta_value='cancelled' AND postmeta.meta_key=%s AND posts.post_type=%s ORDER BY postmeta.meta_id DESC", $post_id, '_status', $post_type ) );
6513 }
6514 } else {
6515 if ( -1 === $post_id ) {
6516 $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 ) );
6517 } else {
6518 $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 ) );
6519 }
6520 }
6521
6522 $response = [];
6523 if ( ! empty( $result ) ) {
6524 $result_post_id = $result[0]->post_id;
6525 $result_user_id = $result[0]->user_id;
6526
6527 switch ( $trigger ) {
6528 case 'lifterlms_lesson_completed':
6529 $context = array_merge(
6530 WordPress::get_user_context( $result_user_id ),
6531 LifterLMS::get_lms_lesson_context( $result_post_id )
6532 );
6533
6534 $context['course'] = get_the_title( get_post_meta( $result_post_id, '_llms_parent_course', true ) );
6535 if ( '' !== ( get_post_meta( $result_post_id, '_llms_parent_section', true ) ) ) {
6536 $context['parent_section'] = get_the_title( get_post_meta( $result_post_id, '_llms_parent_section', true ) );
6537 }
6538 break;
6539 case 'lifterlms_course_enrolled':
6540 case 'lifterlms_course_user_removed':
6541 case 'lifterlms_course_completed':
6542 $context = array_merge(
6543 WordPress::get_user_context( $result_user_id ),
6544 LifterLMS::get_lms_course_context( $result_post_id )
6545 );
6546 break;
6547 case 'lifterlms_section_completed':
6548 $data = new \LLMS_Section( $result_post_id );
6549 $lessons = $data->get_lessons();
6550 $context = array_merge(
6551 WordPress::get_user_context( $result_user_id ),
6552 WordPress::get_post_context( $result_post_id )
6553 );
6554 $context['parent_course'] = $data->get( 'parent_course' );
6555 $context['parent_course_title'] = get_the_title( $data->get( 'parent_course' ) );
6556 if ( ! empty( $lessons ) ) {
6557 foreach ( $lessons as $key => $lesson ) {
6558 $context['section_lesson'][ $key ] = $lesson->id;
6559 $context['section_lesson_title'][ $key ] = get_the_title( $lesson->id );
6560 }
6561 }
6562 $context['section_course'] = $data->get( 'parent_course' );
6563 $context['parent_course_title'] = get_the_title( $data->get( 'parent_course' ) );
6564 break;
6565 case 'lifterlms_purchase_course':
6566 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
6567 $context['course_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
6568 $context['course_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
6569 $context['course_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
6570 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
6571 $context ['order'] = WordPress::get_post_context( $result_post_id );
6572 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
6573 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
6574 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
6575 $context = array_merge( $context, WordPress::get_user_context( $user_id ) );
6576 break;
6577 case 'lifterlms_purchase_membership':
6578 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
6579 $context['membership_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
6580 $context['membership_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
6581 $context['membership_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
6582 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
6583 $context ['order'] = WordPress::get_post_context( $result_post_id );
6584 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
6585 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
6586 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
6587 $context = array_merge( $context, WordPress::get_user_context( $user_id ) );
6588 break;
6589 case 'lifterlms_cancel_membership':
6590 $context = array_merge( WordPress::get_post_context( $result_post_id ), WordPress::get_user_context( $result[0]->user_id ) );
6591 $context['membership_id'] = $result_post_id;
6592 $context['membership_name'] = get_the_title( $result_post_id );
6593 break;
6594 default:
6595 return;
6596
6597 }
6598 $response['pluggable_data'] = $context;
6599 $response['response_type'] = 'live';
6600
6601 }
6602
6603 return $response;
6604
6605 }
6606
6607 /**
6608 * Search SM data.
6609 *
6610 * @param array $data data.
6611 * @return array
6612 */
6613 public function search_suremember_last_data( $data ) {
6614 global $wpdb;
6615 $post_type = $data['post_type'];
6616 $meta_key = '_is_complete';
6617 $trigger = $data['search_term'];
6618 $post_id = $data['filter']['group_id']['value'];
6619
6620 if ( 'suremember_updated_group' === $trigger ) {
6621 if ( -1 === $post_id ) {
6622 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts as posts WHERE posts.post_type=%s", $post_type ) );
6623 } else {
6624 $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 ) );
6625 }
6626 } else {
6627 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}usermeta as usermeta WHERE usermeta.meta_key = %s", 'suremembers_user_access_group_' . $post_id ) );
6628 }
6629
6630 $response = [];
6631
6632 if ( ! empty( $result ) ) {
6633 $context = [];
6634 switch ( $trigger ) {
6635 case 'suremember_updated_group':
6636 $group_id = $result[0]->ID;
6637 $suremembers_post['rules'] = get_post_meta( $group_id, 'suremembers_plan_include', true );
6638 $suremembers_post['exclude'] = get_post_meta( $group_id, 'suremembers_plan_exclude', true ); //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
6639 $suremembers_post['suremembers_user_roles'] = get_post_meta( $group_id, 'suremembers_user_roles', true );
6640 $suremembers_post['title'] = get_the_title( $group_id );
6641 $suremembers_post['restrict'] = get_post_meta( $group_id, 'suremembers_plan_rules', true )['restrict'];
6642 $context['group'] = array_merge( WordPress::get_post_context( $group_id ), $suremembers_post );
6643 $context['group_id'] = $group_id;
6644 unset( $context['group']['ID'] );
6645 $response['pluggable_data'] = $context;
6646 $response['response_type'] = 'live';
6647 break;
6648 case 'suremember_user_added_in_group':
6649 foreach ( $result as $res ) {
6650 $meta_value = unserialize( $res->meta_value );
6651 if ( 'active' === $meta_value['status'] ) {
6652 $context = WordPress::get_user_context( $res->user_id );
6653 $context['group'] = WordPress::get_post_context( $post_id );
6654 $context['group_id'] = $post_id;
6655 unset( $context['group']['ID'] );
6656 $response['pluggable_data'] = $context;
6657 $response['response_type'] = 'live';
6658 }
6659 }
6660 break;
6661 case 'suremember_user_removed_from_group':
6662 foreach ( $result as $res ) {
6663 $meta_value = unserialize( $res->meta_value );
6664 if ( 'revoked' === $meta_value['status'] ) {
6665 $context = WordPress::get_user_context( $res->user_id );
6666 $context['group'] = WordPress::get_post_context( $post_id );
6667 $context['group_id'] = $post_id;
6668 unset( $context['group']['ID'] );
6669 $response['pluggable_data'] = $context;
6670 $response['response_type'] = 'live';
6671 }
6672 }
6673 break;
6674 default:
6675 return;
6676
6677 }
6678 }
6679
6680 return $response;
6681
6682 }
6683
6684 /**
6685 * Search CartFlows data.
6686 *
6687 * @param array $data data.
6688 * @return array
6689 */
6690 public function search_cartflows_last_data( $data ) {
6691 global $wpdb;
6692 $trigger = $data['search_term'];
6693
6694 if ( ! function_exists( 'wcf_pro' ) ) {
6695 return [];
6696 }
6697
6698 $context = [];
6699 $response = [];
6700 if ( 'cartflows_upsell_offer_accepted' === $trigger || 'cartflows_upsell_offer_rejected' === $trigger ) {
6701 $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'" );
6702 } elseif ( 'cartflows_downsell_offer_accepted' === $trigger || 'cartflows_downsell_offer_rejected' === $trigger ) {
6703 $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='downsell' AND postmeta.meta_key= '_cartflows_offer_type'" );
6704 } elseif ( 'wcf_order_bump_item_added' === $trigger || 'wcf_order_bump_item_removed' === $trigger || 'wcf_order_bump_item_replaced' === $trigger ) {
6705 $args = [
6706 'post_type' => 'product',
6707 'orderby' => 'ID',
6708 'order' => 'DESC',
6709 'post_status' => 'publish',
6710 'numberposts' => 1,
6711 ];
6712 $products = get_posts( $args );
6713
6714 if ( count( $products ) > 0 ) {
6715 $product_id = $products[0]->ID;
6716 $product_data['product_id'] = $product_id;
6717 $product_data['product'] = WooCommerce::get_product_context( $product_id );
6718 $terms = get_the_terms( $product_id, 'product_cat' );
6719 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6720 $cat_name = [];
6721 foreach ( $terms as $cat ) {
6722 $cat_name[] = $cat->name;
6723 }
6724 $product_data['product']['category'] = implode( ', ', $cat_name );
6725 }
6726 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6727 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6728 $tag_name = [];
6729 foreach ( $terms_tags as $tag ) {
6730 $tag_name[] = $tag->name;
6731 }
6732 $product_data['product']['tag'] = implode( ', ', $tag_name );
6733 }
6734 unset( $product_data['product']['id'] ); //phpcs:ignore
6735 $response['response_type'] = 'live';
6736 } else {
6737 $product_data['product'] = [
6738 'name' => 'Sample Product',
6739 'description' => 'This is description of sample product.',
6740 'short_description' => 'This is short description of sample product.',
6741 'image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
6742 'slug' => 'sample-product',
6743 'status' => 'publish',
6744 'type' => 'simple',
6745 'price' => '89',
6746 'featured' => '0',
6747 'sku' => 'hoodie-blue-sm',
6748 'regular_price' => '90',
6749 'sale_price' => '89',
6750 'total_sales' => '21',
6751 'category' => 'Uncategorized',
6752 'tags' => 'sample, new, 2022',
6753 'category_ids' => [ 40 ],
6754 'date_created' => [
6755 'date' => '2024-09-19 09:28:57.000000',
6756 'timezone_type' => '1',
6757 'timezone' => '+00:00',
6758 ],
6759 'date_modified' => [
6760 'date' => '2024-09-19 09:28:57.000000',
6761 'timezone_type' => '1',
6762 'timezone' => '+00:00',
6763 ],
6764 'product_id' => 9935,
6765 ];
6766 $response['response_type'] = 'sample';
6767 }
6768 $response['pluggable_data'] = $product_data;
6769 }
6770 if ( ! empty( $result ) && ( 'cartflows_upsell_offer_accepted' === $trigger || 'cartflows_upsell_offer_rejected' === $trigger ) ) {
6771 $context = [];
6772 $order_upsell_id = $result[0]->post_id;
6773 $step_id = get_post_meta( $order_upsell_id, '_cartflows_offer_step_id', true );
6774 $order_id = get_post_meta( $order_upsell_id, '_cartflows_offer_parent_id', true );
6775 $order = wc_get_order( $order_id );
6776 $upsell_order = wc_get_order( $order_upsell_id );
6777 $items = $upsell_order->get_items();
6778 if ( ! empty( $items ) && isset( $items[0] ) ) {
6779 $variation_id = $items[0]['product_id'];
6780 $input_qty = $items[0]['quantity'];
6781 } else {
6782 $variation_id = null;
6783 $input_qty = null;
6784 }
6785 $offer_product = wcf_pro()->utils->get_offer_data( $step_id, $variation_id, $input_qty, $order_id );
6786 $user_id = get_post_meta( $order_upsell_id, '_customer_user', true );
6787 $context = WordPress::get_user_context( $user_id );
6788 $context['order'] = $order->get_data();
6789 $context['upsell'] = $offer_product;
6790 $context['funnel_step_id'] = $step_id;
6791 if ( is_scalar( $step_id ) ) {
6792 $context['funnel_id'] = get_post_meta( intval( $step_id ), 'wcf-flow-id', true );
6793 } else {
6794 $context['funnel_id'] = null;
6795 }
6796 $response['pluggable_data'] = $context;
6797 $response['response_type'] = 'live';
6798 } elseif ( ! empty( $result ) && ( 'cartflows_downsell_offer_accepted' === $trigger || 'cartflows_downsell_offer_rejected' === $trigger ) ) {
6799 $context = [];
6800 $order_downsell_id = $result[0]->post_id;
6801 $step_id = get_post_meta( $order_downsell_id, '_cartflows_offer_step_id', true );
6802 $order_id = get_post_meta( $order_downsell_id, '_cartflows_offer_parent_id', true );
6803 $order = wc_get_order( $order_id );
6804 $downsell_order = wc_get_order( $order_downsell_id );
6805 $items = $downsell_order->get_items();
6806 if ( ! empty( $items ) && isset( $items[0] ) ) {
6807 $variation_id = $items[0]['product_id'];
6808 $input_qty = $items[0]['quantity'];
6809 } else {
6810 $variation_id = null;
6811 $input_qty = null;
6812 }
6813 $offer_product = wcf_pro()->utils->get_offer_data( $step_id, $variation_id, $input_qty, $order_id );
6814 $user_id = get_post_meta( $order_downsell_id, '_customer_user', true );
6815 $context = WordPress::get_user_context( $user_id );
6816 $context['order'] = $order->get_data();
6817 $context['downsell'] = $offer_product;
6818 $context['funnel_step_id'] = $step_id;
6819 if ( is_scalar( $step_id ) ) {
6820 $context['funnel_id'] = get_post_meta( intval( $step_id ), 'wcf-flow-id', true );
6821 } else {
6822 $context['funnel_id'] = null;
6823 }
6824 $response['pluggable_data'] = $context;
6825 $response['response_type'] = 'live';
6826 }
6827 return $response;
6828 }
6829
6830 /**
6831 * Prepare CartFlows Steps list.
6832 *
6833 * @param array $data data.
6834 *
6835 * @return array
6836 */
6837 public function search_cartflows_funnel_step_list( $data ) {
6838 $page = $data['page'];
6839 $limit = Utilities::get_search_page_limit();
6840 $offset = $limit * ( $page - 1 );
6841
6842 $args = [
6843 'posts_per_page' => $limit,
6844 'offset' => $offset,
6845 'orderby' => 'title',
6846 'order' => 'ASC',
6847 'post_type' => 'cartflows_step',
6848 'post_status' => 'publish',
6849 'fields' => 'ids',
6850 ];
6851 $args['meta_query'] = [
6852 [
6853 'key' => 'wcf-flow-id',
6854 'value' => $data['dynamic'],
6855 'compare' => '=',
6856 ],
6857 ];
6858
6859 $flow_step_list = get_posts( $args );
6860 $flow_step_list_count = count(
6861 get_posts(
6862 [
6863 'posts_per_page' => -1,
6864 'orderby' => 'title',
6865 'order' => 'ASC',
6866 'post_type' => 'cartflows_step',
6867 'post_status' => 'publish',
6868 'fields' => 'ids',
6869 'meta_query' => [
6870 [
6871 'key' => 'wcf-flow-id',
6872 'value' => $data['dynamic'],
6873 'compare' => '=',
6874 ],
6875 ],
6876 ]
6877 )
6878 );
6879
6880 $options = [];
6881 if ( ! empty( $flow_step_list ) ) {
6882 foreach ( $flow_step_list as $step ) {
6883 $options[] = [
6884 'label' => html_entity_decode( get_the_title( $step ), ENT_QUOTES, 'UTF-8' ),
6885 'value' => $step,
6886 ];
6887 }
6888 }
6889 return [
6890 'options' => $options,
6891 'hasMore' => $flow_step_list_count > $limit && $flow_step_list_count > $offset,
6892 ];
6893 }
6894
6895
6896 /**
6897 * Fetch user context.
6898 *
6899 * @param int $initiator_id initiator id.
6900 * @param int $friend_id friend id.
6901 * @return array
6902 */
6903 public function get_user_context( $initiator_id, $friend_id ) {
6904 $context = WordPress::get_user_context( $initiator_id );
6905
6906 $friend_context = WordPress::get_user_context( $friend_id );
6907
6908 $avatar = get_avatar_url( $initiator_id );
6909
6910 $context['avatar_url'] = ( $avatar ) ? $avatar : '';
6911
6912 $context['friend_id'] = $friend_id;
6913 $context['friend_first_name'] = $friend_context['user_firstname'];
6914 $context['friend_last_name'] = $friend_context['user_lastname'];
6915 $context['friend_email'] = $friend_context['user_email'];
6916
6917 $friend_avatar = get_avatar_url( $friend_id );
6918 $context['friend_avatar_url'] = $friend_avatar;
6919 return $context;
6920 }
6921
6922 /**
6923 * Search BP data.
6924 *
6925 * @param array $data data.
6926 * @return array
6927 */
6928 public function search_pluggables_bp_friendships( $data ) {
6929 global $wpdb, $bp;
6930 $context = [];
6931 $sample['pluggable_data'] = [
6932 'wp_user_id' => 4,
6933 'user_login' => 'katy1ßßßß',
6934 'display_name' => 'Katy Smith',
6935 'user_firstname' => 'Katy',
6936 'user_lastname' => 'Smith',
6937 'user_email' => 'katy1@gmail.com',
6938 'user_role' => [ 'subscriber' ],
6939 'avatar_url' => 'http://pqr.com/avatar',
6940 'friend_id' => 1,
6941 'friend_first_name' => 'John',
6942 'friend_last_name' => 'Wick',
6943 'friend_email' => 'john@gmail.com',
6944 'friend_avatar_url' => 'http://abc.com/avatar',
6945 ];
6946 $sample['response_type'] = 'sample';
6947
6948 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_friends'" );
6949 if ( $table_exists ) {
6950 $friendships = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_friends LIMIT 1" );
6951 if ( ! empty( $friendships ) ) {
6952 $friendship = $friendships[0];
6953 $initiator_id = $friendship->initiator_user_id;
6954 $friend_user_id = $friendship->friend_user_id;
6955 $context['pluggable_data'] = $this->get_user_context( $initiator_id, $friend_user_id );
6956 $context['response_type'] = 'live';
6957 } else {
6958 $context = $sample;
6959 }
6960 } else {
6961 $context = $sample;
6962 }
6963
6964
6965 return $context;
6966 }
6967
6968 /**
6969 * Search Buddyboss profile types data.
6970 *
6971 * @param array $data data.
6972 * @return array
6973 */
6974 public function search_pluggables_bp_profile_types( $data ) {
6975 global $wpdb, $bp;
6976 $context = [];
6977 $sample['pluggable_data'] = [
6978 'wp_user_id' => 4,
6979 'user_login' => 'katy1ßßßß',
6980 'display_name' => 'Katy Smith',
6981 'user_firstname' => 'Katy',
6982 'user_lastname' => 'Smith',
6983 'user_email' => 'katy1@gmail.com',
6984 'user_role' => [ 'subscriber' ],
6985 'bb_profile_type' => '10',
6986 'bb_profile_type_name' => 'student',
6987 ];
6988 $sample['response_type'] = 'sample';
6989
6990 $post_id = $data['filter']['bb_profile_type']['value'];
6991 $get_existing = get_post_meta( $post_id, '_bp_member_type_key', true );
6992
6993 $type_term = get_term_by(
6994 'name',
6995 /**
6996 *
6997 * Ignore line
6998 *
6999 * @phpstan-ignore-next-line
7000 */
7001 $get_existing,
7002 'bp_member_type'
7003 );
7004
7005 $results = $wpdb->get_results(
7006 $wpdb->prepare(
7007 "SELECT u.ID FROM {$wpdb->prefix}users u INNER JOIN {$wpdb->prefix}term_relationships r
7008 ON u.ID = r.object_id WHERE u.user_status = 0 AND
7009 r.term_taxonomy_id = %d ORDER BY RAND() LIMIT 1",
7010 /**
7011 *
7012 * Ignore line
7013 *
7014 * @phpstan-ignore-next-line
7015 */
7016 $type_term->term_id
7017 )
7018 );
7019
7020 if ( ! empty( $results ) ) {
7021 $user = $results[0];
7022 $context['pluggable_data'] = WordPress::get_user_context( $user->ID );
7023 $context['pluggable_data']['bb_profile_type'] = $post_id;
7024 $context['pluggable_data']['bb_profile_type_name'] = get_post_meta( $post_id, '_bp_member_type_label_singular_name', true );
7025 $context['response_type'] = 'live';
7026 } else {
7027 $context = $sample;
7028 }
7029
7030 return $context;
7031 }
7032
7033 /**
7034 * Search BP User data.
7035 *
7036 * @param array $data data.
7037 * @return array
7038 */
7039 public function search_pluggables_bb_users( $data ) {
7040 global $wpdb;
7041 $context = [];
7042
7043 if ( ! class_exists( 'BP_Signup' ) ) {
7044 return [];
7045 }
7046
7047 $term = $data['search_term'];
7048
7049 if ( 'account_activated' === $term ) {
7050 $signups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}signups WHERE active = 1 ORDER BY signup_id DESC LIMIT 1" );
7051 } elseif ( 'updates_profile' === $term ) {
7052 $custom_ids = $wpdb->get_var( "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data ORDER BY last_updated DESC LIMIT 1" );
7053 $args = [ 'include' => $custom_ids ];
7054 $users = get_users( $args );
7055 } elseif ( 'gains_follower' === $term ) {
7056 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_follow ORDER BY id DESC LIMIT 1" );
7057 }
7058
7059 if ( ! empty( $signups ) ) {
7060 $pluggable_data = $signups[0];
7061 $pluggable_data = get_object_vars( $pluggable_data );
7062 unset( $pluggable_data['activation_key'] );
7063 if ( is_string( $pluggable_data['meta'] ) ) {
7064 $pluggable_data['meta'] = unserialize( $pluggable_data['meta'] );
7065 }
7066 if ( is_array( $pluggable_data['meta'] ) ) {
7067 unset( $pluggable_data['meta']['password'] );
7068 }
7069 $context['pluggable_data'] = $pluggable_data;
7070 $context['response_type'] = 'live';
7071 } elseif ( 'updates_profile' === $term ) {
7072 if ( ! empty( $users ) ) {
7073 $user = $users[0];
7074 $fields = $wpdb->get_results( $wpdb->prepare( "SELECT field_id, value FROM {$wpdb->prefix}bp_xprofile_data WHERE user_id = %d", $user->ID ) );
7075 $user_data = WordPress::get_user_context( $user->ID );
7076 $pluggable_data['user_id'] = $user->ID;
7077 $pluggable_data['user_email'] = $user_data['user_email'];
7078 foreach ( $user_data['user_role'] as $key => $role ) {
7079 $pluggable_data['user_role'][ $key ] = $role;
7080 }
7081 foreach ( $fields as $field ) {
7082 if ( function_exists( 'xprofile_get_field' ) ) {
7083 $fieldj = xprofile_get_field( $field->field_id );
7084 $pluggable_data[ $fieldj->name ] = $field->value;
7085 }
7086 }
7087 $context['pluggable_data'] = $pluggable_data;
7088 $context['response_type'] = 'live';
7089 } else {
7090 $context['pluggable_data'] = [
7091 'user_login' => 'john',
7092 'display_name' => 'john',
7093 'user_firstname' => 'john',
7094 'user_lastname' => 'd',
7095 'user_email' => 'johnd@yopmail.com',
7096 'user_role' => [
7097 'subscriber',
7098 ],
7099 'Name' => 'john',
7100 'Nickname' => 'johnd',
7101 'wp_user_id' => 16,
7102 ];
7103 $context['response_type'] = 'sample';
7104 }
7105 } elseif ( 'gains_follower' === $term ) {
7106 if ( ! empty( $results ) ) {
7107 $pluggable_data['follower'] = WordPress::get_user_context( $results[0]->follower_id );
7108 $pluggable_data['leader'] = WordPress::get_user_context( $results[0]->leader_id );
7109 $context['pluggable_data'] = $pluggable_data;
7110 $context['response_type'] = 'live';
7111 } else {
7112 $context['pluggable_data'] = [
7113 'follower' => [
7114 'wp_user_id' => 126,
7115 'user_login' => 'belli',
7116 'display_name' => 'belli',
7117 'user_firstname' => 'test',
7118 'user_lastname' => 'test',
7119 'user_email' => 'belli@gmail.com',
7120 'user_role' => [
7121 'subscriber',
7122 'wpamelia-customer',
7123 ],
7124 ],
7125 'leader' => [
7126 'wp_user_id' => 34,
7127 'user_login' => 'bella3@gmail.com',
7128 'display_name' => 'bella3@gmail.com',
7129 'user_firstname' => 'bella3',
7130 'user_lastname' => 'bella3',
7131 'user_email' => 'bellaaa3@gmail.com',
7132 'user_role' => [
7133 'wpamelia-customer',
7134 ],
7135 ],
7136 ];
7137 $context['response_type'] = 'sample';
7138 }
7139 } else {
7140 $context['pluggable_data'] = [
7141 'signup_id' => '16',
7142 'domain' => '',
7143 'path' => '',
7144 'title' => '',
7145 'user_login' => 'johnd',
7146 'user_email' => 'johnd@yopmail.com',
7147 'registered' => '2024-01-29 04:52:13',
7148 'activated' => '0000-00-00 00:00:00',
7149 'active' => '0',
7150 'meta' => [
7151 'field_1' => 'john',
7152 'field_3' => 'd',
7153 'field_4' => '09878988766',
7154 'field_2' => '123 Main Street',
7155 'field_5' => 'johnd',
7156 'profile_field_ids' => '1,3,4,2,5',
7157 ],
7158 'id' => 16,
7159 'user_name' => 'johnd',
7160 'date_sent' => '2024-01-29 04:52:13',
7161 'recently_sent' => true,
7162 'count_sent' => 1,
7163 ];
7164 $context['response_type'] = 'sample';
7165 }
7166
7167 return $context;
7168 }
7169
7170 /**
7171 * Search BP data.
7172 *
7173 * @param array $data data.
7174 * @return array
7175 */
7176 public function search_pluggables_bp_groups( $data ) {
7177 global $wpdb, $bp;
7178 $context = [];
7179 $group_data = [];
7180 $args = [
7181 'orderby' => 'user_nicename',
7182 'order' => 'ASC',
7183 'number' => 1,
7184 ];
7185
7186 $term = $data['search_term'];
7187
7188 if ( ! function_exists( 'groups_get_group' ) || ! function_exists( 'bp_groups_get_group_types' ) || ! function_exists( 'bp_groups_get_group_type' ) || ! function_exists( 'bp_get_group_cover_url' ) || ! function_exists( 'bp_get_group_avatar_url' ) || ! function_exists( 'groups_get_group_members' ) || ! function_exists( 'groups_get_invites' ) ) {
7189 return [];
7190 }
7191
7192 if ( 'user_joins_specific_type_group' == $term ) {
7193 if ( -1 !== $data['filter']['group_type']['value'] ) {
7194 $group_type = $data['filter']['group_type']['value'];
7195 } else {
7196 $registered_types = bp_groups_get_group_types();
7197 $random_key = array_rand( $registered_types );
7198 $random_value = $registered_types[ $random_key ];
7199 $group_type = $random_value;
7200 }
7201 if ( function_exists( 'bp_get_group_ids_by_group_types' ) ) {
7202 $group_ids = bp_get_group_ids_by_group_types( $group_type );
7203 $random_key = array_rand( $group_ids );
7204 $random_value = $group_ids[ $random_key ];
7205 $group_id = $random_value;
7206 if ( function_exists( 'groups_get_group' ) ) {
7207 $group = groups_get_group( $group_id['id'] );
7208 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
7209 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
7210 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
7211 $group_data['group_type'] = $group_type;
7212 if ( function_exists( 'groups_get_group_members' ) ) {
7213 $members = groups_get_group_members(
7214 [
7215 'group_id' => $group_id,
7216 ]
7217 );
7218 $ids = [];
7219 foreach ( $members['members'] as $member ) {
7220 $ids[] = $member->ID;
7221 }
7222 $args = [
7223 'number' => 1,
7224 'include' => $ids,
7225 ];
7226 $users = get_users( $args );
7227 }
7228 }
7229 }
7230 } elseif ( 'requests_access_private_group' === $data['search_term'] ) {
7231 $group_id = $data['filter']['group_id']['value'];
7232 if ( $group_id > 0 ) {
7233 $results = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, item_id FROM {$wpdb->prefix}bp_invitations WHERE type LIKE %s AND item_id = %d ORDER BY id DESC LIMIT 1", 'request', $group_id ) );
7234 } else {
7235 $results = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, item_id FROM {$wpdb->prefix}bp_invitations WHERE type = %s ORDER BY id DESC LIMIT 1", 'request' ) );
7236 }
7237 $custom_ids = $results[0]->user_id;
7238 $args = [ 'include' => $custom_ids ];
7239 $users = get_users( $args );
7240 $group_id = $results[0]->item_id;
7241 $group = groups_get_group( $group_id );
7242 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
7243 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
7244 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
7245 } elseif ( 'bb_group_created' == $term ) {
7246 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups ORDER BY id DESC LIMIT 1" );
7247 if ( function_exists( 'groups_get_group' ) ) {
7248 $group = groups_get_group( $results[0]->id );
7249 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
7250 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
7251 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
7252 $current_types = (array) bp_groups_get_group_type( $results[0]->id, false );
7253 $group_data['group_type'] = $current_types;
7254 $group_data['group_status'] = $group->status;
7255 $group_data['group_date_created'] = $group->date_created;
7256 $group_data['group_enabled_forum'] = $group->enable_forum;
7257 $group_data['group_cover_url'] = bp_get_group_cover_url( $group );
7258 $group_data['group_avatar_url'] = bp_get_group_avatar_url( $group );
7259 $group_data['group_creator'] = WordPress::get_user_context( $group->creator_id );
7260 if ( function_exists( 'groups_get_group_members' ) ) {
7261 $members = groups_get_group_members(
7262 [
7263 'group_id' => $results[0]->id,
7264 ]
7265 );
7266 foreach ( $members['members'] as $key => $member ) {
7267 $group_data['group_member'][ $key ] = WordPress::get_user_context( $member->ID );
7268 }
7269 }
7270 $args = [
7271 'item_id' => $results[0]->id,
7272 ];
7273 $invitations = groups_get_invites( $args );
7274 if ( ! empty( $invitations ) ) {
7275 foreach ( $invitations as $key => $invite ) {
7276 $group_data['invitation'][ $key ] = $invite;
7277 }
7278 }
7279 }
7280 } else {
7281 $users = get_users( $args );
7282
7283 if ( isset( $data['filter']['group_id']['value'] ) ) {
7284 $group_id = $data['filter']['group_id']['value'];
7285 $args['group_id'] = $group_id;
7286 if ( $group_id > 0 ) {
7287 $group = groups_get_group( $group_id );
7288 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
7289 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
7290 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
7291 } else {
7292 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_groups'" );
7293 if ( $table_exists ) {
7294 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups LIMIT 1" );
7295 $context['groups'] = $groups;
7296 if ( ! empty( $groups ) ) {
7297 foreach ( $groups as $group ) {
7298 $group_data['group_id'] = $group->id;
7299 $group_data['group_name'] = $group->name;
7300 $group_data['group_description'] = $group->description;
7301 }
7302 }
7303 }
7304 }
7305 }
7306 }
7307
7308 if ( 'bb_group_created' == $term ) {
7309 if ( ! empty( $group_data ) ) {
7310 $pluggable_data = $group_data;
7311 $context['pluggable_data'] = $pluggable_data;
7312 $context['response_type'] = 'live';
7313 } else {
7314 $context['pluggable_data'] = [
7315 'group_id' => 112,
7316 'group_name' => 'New Group',
7317 'group_description' => 'New Group Description',
7318 'group_type' => [ 'business' ],
7319 'group_status' => 'public',
7320 'group_date_created' => '2024-02-16 05:37:22',
7321 'group_enabled_forum' => 1,
7322 'group_cover_url' => 'https:\/\/example.com\/wp-content\/uploads\/buddypress\/groups\/23\/cover-image\/65cef4c3ea9b6-bp-cover-image.jpeg',
7323 'group_avatar_url' => 'https:\/\/example.com\/wp-content\/uploads\/group-avatars\/23\/65cef4d7cee19-bpfull.png',
7324 'group_creator' => [
7325 'wp_user_id' => 183,
7326 'user_login' => 'johnd',
7327 'display_name' => 'john',
7328 'user_firstname' => 'john',
7329 'user_lastname' => 'd',
7330 'user_email' => 'johnd@yopmail.com',
7331 'user_role' => [
7332 'subscriber',
7333 'bbp_participant',
7334 ],
7335 ],
7336 ];
7337 $context['response_type'] = 'sample';
7338 }
7339 } elseif ( ! empty( $users ) ) {
7340 $user = $users[0];
7341 $pluggable_data = $group_data;
7342
7343 $avatar = get_avatar_url( $user->ID );
7344 $pluggable_data['wp_user_id'] = $user->ID;
7345 $pluggable_data['avatar_url'] = ( $avatar ) ? $avatar : '';
7346 $pluggable_data['user_login'] = $user->user_login;
7347 $pluggable_data['display_name'] = $user->display_name;
7348 $pluggable_data['user_firstname'] = $user->user_firstname;
7349 $pluggable_data['user_lastname'] = $user->user_lastname;
7350 $pluggable_data['user_email'] = $user->user_email;
7351 $pluggable_data['user_role'] = $user->roles;
7352 $context['pluggable_data'] = $pluggable_data;
7353 $context['response_type'] = 'live';
7354 } else {
7355 $context['pluggable_data'] = [
7356 'wp_user_id' => 1,
7357 'user_login' => 'admin',
7358 'display_name' => 'Johnd',
7359 'user_firstname' => 'John',
7360 'user_lastname' => 'D',
7361 'user_email' => 'johnd@gmail.com',
7362 'user_role' => [ 'subscriber' ],
7363 'group_id' => 112,
7364 'group_name' => 'New Group',
7365 'group_description' => 'New Group Description',
7366 ];
7367 $context['response_type'] = 'sample';
7368 }
7369
7370 return $context;
7371 }
7372
7373 /**
7374 * Search complete courses.
7375 *
7376 * @param array $data data.
7377 * @return array
7378 */
7379 public function search_pluggables_complete_course( $data ) {
7380 global $wpdb;
7381 $context = [];
7382
7383 if ( isset( $data['filter']['sfwd_course_id']['value'] ) ) {
7384 $course_id = $data['filter']['sfwd_course_id']['value'];
7385 }
7386 if ( -1 === $course_id ) {
7387 $courses = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='course' AND activity.activity_status= %d ORDER BY activity.activity_id DESC", 1 ) );
7388 } else {
7389 $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 ) );
7390 }
7391
7392 if ( ! empty( $courses ) ) {
7393 $course = $courses[0];
7394 $course_data['course_name'] = $course->post_title;
7395 $course_data['sfwd_course_id'] = $course->ID;
7396 $course_data['course_url'] = get_permalink( $course->ID );
7397 $course_data['course_featured_image_id'] = get_post_meta( $course->ID, '_thumbnail_id', true );
7398 $course_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course->ID );
7399 $timestamp = get_user_meta( $course->user_id, 'course_completed_' . $course->ID, true );
7400 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
7401 $date_format = get_option( 'date_format' );
7402 if ( is_string( $date_format ) ) {
7403 $course_data['course_completion_date'] = wp_date( $date_format, $timestamp );
7404 }
7405 if ( function_exists( 'learndash_get_course_certificate_link' ) ) {
7406 $course_data['course_certificate'] = learndash_get_course_certificate_link( $course->ID, $course->user_id );
7407 }
7408 $context['response_type'] = 'live';
7409 } else {
7410 $course_data['course_name'] = 'Test Course';
7411 $course_data['sfwd_course_id'] = 112;
7412 $course_data['course_url'] = 'https://abc.com/test-course';
7413 $course_data['course_featured_image_id'] = 113;
7414 $course_data['course_featured_image_url'] = 'https://pqr.com/test-course-img';
7415 $course_data['course_completion_date'] = '2023-10-20';
7416 $course_data['course_certificate'] = 'https://example.com/certificates/good-performance/?course_id=112&cert-nonce=f80d0f9cc1';
7417 $context['response_type'] = 'sample';
7418 }
7419
7420 $users_data = $this->search_pluggables_add_user_role( [] );
7421 $user_data = $users_data['pluggable_data'];
7422
7423 $context['pluggable_data'] = array_merge( $course_data, $user_data );
7424 return $context;
7425 }
7426
7427 /**
7428 * Search lessons.
7429 *
7430 * @param array $data data.
7431 * @return array
7432 */
7433 public function search_pluggables_complete_lesson( $data ) {
7434 global $wpdb;
7435 $context = [];
7436
7437 if ( isset( $data['filter']['sfwd_lesson_id']['value'] ) ) {
7438 $lesson_id = $data['filter']['sfwd_lesson_id']['value'];
7439 $course_id = $data['filter']['sfwd_course_id']['value'];
7440 }
7441 if ( -1 === $course_id ) {
7442 $courses = get_posts(
7443 [
7444 'posts_per_page' => - 1,
7445 'post_type' => 'sfwd-courses',
7446 'post_status' => 'publish',
7447 'fields' => 'ids',
7448 ]
7449 );
7450 $course_key = array_rand( $courses );
7451 $course_id = $courses[ $course_key ];
7452 }
7453 $course = get_post( $course_id );
7454 $pluggable_data = LearnDash::get_course_context( $course );
7455
7456 if ( -1 === $lesson_id ) {
7457 $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 ) );
7458 } else {
7459 $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 ) );
7460 }
7461
7462 if ( ! empty( $lessons ) ) {
7463 $lesson = $lessons[0];
7464
7465 $pluggable_data = WordPress::get_user_context( $lesson->user_id );
7466 $pluggable_data['lesson_name'] = $lesson->post_title;
7467 $pluggable_data['sfwd_lesson_id'] = $lesson->ID;
7468 $pluggable_data['lesson_url'] = get_permalink( $lesson->ID );
7469 $pluggable_data['lesson_featured_image_id'] = get_post_meta( $lesson->ID, '_thumbnail_id', true );
7470 $pluggable_data['lesson_featured_image_url'] = get_the_post_thumbnail_url( $lesson->ID );
7471 $context['response_type'] = 'live';
7472 } else {
7473 $pluggable_data = WordPress::get_sample_user_context();
7474 $pluggable_data['lesson_name'] = 'Test Lesson';
7475 $pluggable_data['sfwd_lesson_id'] = 114;
7476 $pluggable_data['lesson_url'] = 'https://abc.com/test-lesson';
7477 $pluggable_data['lesson_featured_image_id'] = 116;
7478 $pluggable_data['lesson_featured_image_url'] = 'https://pqr.com/test-lesson-img';
7479 $context['response_type'] = 'sample';
7480 }
7481
7482 $context['pluggable_data'] = $pluggable_data;
7483 return $context;
7484 }
7485
7486 /**
7487 * Search topics.
7488 *
7489 * @param array $data data.
7490 * @return array
7491 */
7492 public function search_pluggables_complete_topic( $data ) {
7493 global $wpdb;
7494 $context = [];
7495
7496 if ( isset( $data['filter']['sfwd_topic_id']['value'] ) ) {
7497 $topic_id = $data['filter']['sfwd_topic_id']['value'];
7498 $course_id = $data['filter']['sfwd_course_id']['value'];
7499 }
7500 if ( -1 === $course_id ) {
7501 $courses = get_posts(
7502 [
7503 'posts_per_page' => - 1,
7504 'post_type' => 'sfwd-courses',
7505 'post_status' => 'publish',
7506 'fields' => 'ids',
7507 ]
7508 );
7509 $course_key = array_rand( $courses );
7510 $course_id = $courses[ $course_key ];
7511 }
7512 $course = get_post( $course_id );
7513 $pluggable_data = LearnDash::get_course_context( $course );
7514
7515 if ( -1 === $topic_id ) {
7516 $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 ) );
7517 } else {
7518 $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 ) );
7519 }
7520
7521 if ( ! empty( $topics ) ) {
7522 $topic = $topics[0];
7523 $pluggable_data = WordPress::get_user_context( $topics[0]->user_id );
7524 $pluggable_data['topic_name'] = $topic->post_title;
7525 $pluggable_data['sfwd_topic_id'] = $topic->ID;
7526 $pluggable_data['topic_url'] = get_permalink( $topic->ID );
7527 $pluggable_data['topic_featured_image_id'] = get_post_meta( $topic->ID, '_thumbnail_id', true );
7528 $pluggable_data['topic_featured_image_url'] = get_the_post_thumbnail_url( $topic->ID );
7529 $context['response_type'] = 'live';
7530 } else {
7531 $pluggable_data = WordPress::get_sample_user_context();
7532 $pluggable_data['topic_name'] = 'Test Topic';
7533 $pluggable_data['sfwd_topic_id'] = 117;
7534 $pluggable_data['topic_url'] = 'https://abc.com/test-topic';
7535 $pluggable_data['topic_featured_image_id'] = 118;
7536 $pluggable_data['topic_featured_image_url'] = 'https://pqr.com/test-topic-img';
7537 $context['response_type'] = 'sample';
7538 }
7539
7540 $context['pluggable_data'] = $pluggable_data;
7541 return $context;
7542 }
7543
7544 /**
7545 * Search purchase courses.
7546 *
7547 * @param array $data data.
7548 * @return array
7549 */
7550 public function search_pluggables_purchase_course( $data ) {
7551 $context = [];
7552 $context['response_type'] = 'sample';
7553
7554 $purchase_data = [
7555 'course_product_id' => '1',
7556 'course_product_name' => 'Sample Course',
7557 'currency' => 'USD',
7558 'total_amount' => '100',
7559 'first_name' => 'John',
7560 'last_name' => 'Doe',
7561 'email' => 'john_doe@gmail.com',
7562 'phone' => '+923007626541',
7563 ];
7564
7565 $product_id = (int) ( isset( $data['filter']['course_product_id']['value'] ) ? $data['filter']['course_product_id']['value'] : '-1' );
7566 $order_id = 0;
7567
7568 if ( -1 !== $product_id ) {
7569 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
7570
7571 if ( count( $order_ids ) > 0 ) {
7572 $order_id = $order_ids[0];
7573 }
7574 } else {
7575 $orders = wc_get_orders( [] );
7576 if ( count( $orders ) > 0 ) {
7577 foreach ( $orders as $order ) {
7578 $items = $order->get_items();
7579
7580 if ( count( $items ) > 1 ) {
7581 continue;
7582 }
7583
7584 foreach ( $items as $item ) {
7585 if ( method_exists( $item, 'get_product_id' ) ) {
7586 $product_id = $item->get_product_id();
7587 if ( ! empty( get_post_meta( $item->get_product_id(), '_related_course', true ) ) ) {
7588 $order_id = $order->get_id();
7589 break;
7590 }
7591 }
7592 }
7593 }
7594 }
7595 }
7596
7597 if ( 0 !== $order_id ) {
7598 $order = wc_get_order( $order_id );
7599
7600 if ( $order ) {
7601
7602 $purchase_data = LearnDash::get_purchase_course_context( $order );
7603
7604 $context['response_type'] = 'live';
7605 }
7606 }
7607
7608 $context['pluggable_data'] = $purchase_data;
7609
7610 return $context;
7611 }
7612
7613 /**
7614 * Search quiz data in LearnDash.
7615 *
7616 * @param array $data data.
7617 * @return array
7618 */
7619 public function search_pluggables_ld_quiz( $data ) {
7620 $context = [];
7621 $context['response_type'] = 'sample';
7622 global $wpdb;
7623 $user_data = WordPress::get_sample_user_context();
7624 $quiz_data = [
7625 'quiz' => 7193,
7626 'score' => 1,
7627 'count' => 1,
7628 'question_show_count' => 1,
7629 'pass' => 1,
7630 'rank' => '-',
7631 'time' => 1703595328,
7632 'pro_quizid' => 1,
7633 'course' => 0,
7634 'lesson' => 0,
7635 'topic' => 0,
7636 'points' => 20,
7637 'total_points' => 20,
7638 'percentage' => 100,
7639 'timespent' => 2.7309999999999999,
7640 'has_graded' => false,
7641 'statistic_ref_id' => 9,
7642 'started' => 1703595325,
7643 'completed' => 1703595328,
7644 'ld_version' => '4.8.0.1',
7645 'quiz_key' => '1703595328_1_7193_0',
7646 ];
7647 $output_questions['question'] = [
7648 'ID' => 7195,
7649 'post_content' => '<p>This is a first question<\/p>',
7650 'type' => 'sfwd-question',
7651 'question_type' => 'single',
7652 'points' => 20,
7653 'answers' => [
7654 'sort_answer' => [
7655 [
7656 'answer' => '',
7657 'points' => 0,
7658 'correct' => false,
7659 'type' => 'answer',
7660 ],
7661 ],
7662 'classic_answer' => [
7663 [
7664 'answer' => 'Ans 1',
7665 'points' => 1,
7666 'correct' => false,
7667 'type' => 'answer',
7668 ],
7669 [
7670 'answer' => 'Ans 2',
7671 'points' => 0,
7672 'correct' => true,
7673 'type' => 'answer',
7674 ],
7675 ],
7676 'matrix_sort_answer' => [
7677 [
7678 'answer' => '',
7679 'points' => 0,
7680 'correct' => false,
7681 'type' => 'answer',
7682 ],
7683 ],
7684 'cloze_answer' => [
7685 [
7686 'answer' => '',
7687 'points' => 0,
7688 'correct' => false,
7689 'type' => 'answer',
7690 ],
7691 ],
7692 'free_answer' => [
7693 [
7694 'answer' => '',
7695 'points' => 0,
7696 'correct' => false,
7697 'type' => 'answer',
7698 ],
7699 ],
7700 'assessment_answer' => [
7701 [
7702 'answer' => '',
7703 'points' => 0,
7704 'correct' => false,
7705 'type' => 'answer',
7706 ],
7707 ],
7708 'essay' => [
7709 [
7710 'answer' => '',
7711 'points' => 0,
7712 'correct' => false,
7713 'type' => 'answer',
7714 ],
7715 ],
7716 ],
7717 ];
7718
7719 $quiz_id = (int) ( isset( $data['filter']['sfwd_quiz_id']['value'] ) ? $data['filter']['sfwd_quiz_id']['value'] : '-1' );
7720 $question_id = $data['filter']['sfwd_question_id']['value'];
7721 $term = $data['search_term'];
7722 $mark = 1;
7723 if ( 'passes_quiz' == $term ) {
7724 $mark = 1;
7725 } elseif ( 'fails_quiz' == $term ) {
7726 $mark = 0;
7727 }
7728 if ( 'passes_quiz' == $term || 'fails_quiz' == $term ) {
7729 if ( -1 == $quiz_id ) {
7730 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}learndash_pro_quiz_statistic_ref as statistic ON activity.activity_completed = statistic.create_time WHERE activity.activity_type='quiz' AND activity.activity_status=%d order by activity_id DESC LIMIT 1", $mark ) );
7731 } else {
7732 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity LEFT JOIN {$wpdb->prefix}learndash_pro_quiz_statistic_ref as statistic ON activity.activity_completed = statistic.create_time WHERE activity.activity_type='quiz' AND activity.activity_status=%d AND activity.post_id = %d order by activity.activity_id DESC LIMIT 1", $mark, $quiz_id ) );
7733 }
7734 } elseif ( 'quiz_essay_submitted' == $term ) {
7735 $question_id = $data['filter']['sfwd_question_id']['value'];
7736 $args = [
7737 'post_type' => 'sfwd-essays',
7738 'posts_per_page' => 1,
7739 'order' => 'DESC',
7740 'post_status' => [ 'graded', 'not_graded' ],
7741 'meta_query' => [
7742 'relation' => 'AND',
7743 [
7744 'key' => 'quiz_post_id',
7745 'value' => $quiz_id,
7746 'compare' => '=',
7747 ],
7748 [
7749 'key' => 'question_post_id',
7750 'value' => $question_id,
7751 'compare' => '=',
7752 ],
7753 ],
7754 ];
7755 $essay = get_posts( $args );
7756 } elseif ( 'quiz_essay_graded' == $term ) {
7757 $args = [
7758 'post_type' => 'sfwd-essays',
7759 'posts_per_page' => 1,
7760 'order' => 'DESC',
7761 'post_status' => 'graded',
7762 'meta_query' => [
7763 'relation' => 'AND',
7764 [
7765 'key' => 'quiz_post_id',
7766 'value' => $quiz_id,
7767 'compare' => '=',
7768 ],
7769 [
7770 'key' => 'question_post_id',
7771 'value' => $question_id,
7772 'compare' => '=',
7773 ],
7774 ],
7775 ];
7776 $essay = get_posts( $args );
7777 }
7778
7779 if ( 'quiz_essay_submitted' == $term || 'quiz_essay_graded' == $term ) {
7780 if ( ! empty( $essay ) ) {
7781 $context = WordPress::get_user_context( $essay[0]->post_author );
7782 $course_id = get_post_meta( $essay[0]->ID, 'course_id', true );
7783 $lesson_id = get_post_meta( $essay[0]->ID, 'lesson_id', true );
7784 $context['quiz_name'] = get_the_title( $quiz_id );
7785 $context['sfwd_quiz_id'] = $quiz_id;
7786 $context['sfwd_question_id'] = $question_id;
7787 $context['question_name'] = is_int( $question_id ) ? (int) get_the_title( $question_id ) : null;
7788 $context['course_name'] = is_int( $course_id ) ? (int) get_the_title( $course_id ) : null;
7789 $context['course_id'] = $course_id;
7790 $context['lesson_name'] = is_int( $lesson_id ) ? (int) get_the_title( $lesson_id ) : null;
7791 $context['lesson_id'] = $lesson_id;
7792 $context['essay_id'] = $essay[0]->ID;
7793 $context['essay'] = WordPress::get_post_context( $essay[0]->ID );
7794 if ( 'quiz_essay_graded' == $term ) {
7795 $users_quiz_data = get_user_meta( (int) $essay[0]->post_author, '_sfwd-quizzes', true );
7796 if ( is_array( $users_quiz_data ) && is_array( $users_quiz_data[0] ) ) {
7797 $essay_post_id = $essay[0]->ID;
7798 $result = array_filter(
7799 $users_quiz_data[0]['graded'],
7800 function( $item ) use ( $essay_post_id ) {
7801 return $item['post_id'] === $essay_post_id;
7802 }
7803 );
7804 $context['essay_points_earned'] = $result[1]['points_awarded'];
7805 }
7806 }
7807 $context['response_type'] = 'live';
7808 } else {
7809 $context = WordPress::get_sample_user_context();
7810 $context['quiz_name'] = 'Test Quiz';
7811 $context['sfwd_quiz_id'] = 11;
7812 $context['sfwd_question_id'] = 12;
7813 $context['course_name'] = 'Test Course';
7814 $context['course_id'] = 13;
7815 $context['lesson_name'] = 'Test Lesson';
7816 $context['lesson_id'] = 14;
7817 $context['essay'] = [
7818 'ID' => 12,
7819 'post_content' => 'demo',
7820 ];
7821 $context['response_type'] = 'sample';
7822 }
7823
7824 $context['pluggable_data'] = $context;
7825 } else {
7826 if ( ! empty( $result ) ) {
7827 $user_data = WordPress::get_user_context( $result[0]->user_id );
7828 $quiz_result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}usermeta WHERE user_id = %d AND meta_key = '_sfwd-quizzes'", $result[0]->user_id ) );
7829
7830 $data = unserialize( $quiz_result[0]->meta_value );
7831 $found_element = null;
7832 foreach ( (array) $data as $element ) {
7833 if ( is_array( $element ) ) {
7834 if ( isset( $element['statistic_ref_id'] ) && $element['statistic_ref_id'] == $result[0]->statistic_ref_id ) {
7835 $found_element = $element;
7836 break;
7837 }
7838 }
7839 }
7840 $quiz_data = (array) $found_element;
7841 $output_questions = LearnDash::get_quiz_questions_answers( $quiz_id );
7842 if ( is_array( $quiz_data ) ) {
7843 $quiz_data['quiz_name'] = get_the_title( $quiz_id );
7844 $quiz_data['sfwd_quiz_id'] = $quiz_id;
7845 }
7846 $context['response_type'] = 'live';
7847 }
7848 $context['pluggable_data'] = array_merge( $quiz_data, $user_data, $output_questions );
7849 }
7850
7851 return $context;
7852 }
7853
7854 /**
7855 * Fetch BB templates.
7856 *
7857 * @return array
7858 */
7859 public function get_beaver_builder_templates() {
7860 $allowed_types = [ 'subscribe-form', 'contact-form' ];
7861 $templates = [];
7862 $all_templates = get_posts(
7863 [
7864 'post_type' => 'fl-builder-template',
7865 'meta_key' => '_fl_builder_data',
7866 'posts_per_page' => -1,
7867 ]
7868 );
7869 $posts = get_posts(
7870 [
7871 'post_type' => 'any',
7872 'meta_key' => '_fl_builder_data',
7873 'posts_per_page' => -1,
7874 ]
7875 );
7876 $posts = array_merge( $all_templates, $posts );
7877
7878 if ( ! empty( $posts ) ) {
7879 foreach ( $posts as $post ) {
7880 $meta = get_post_meta( $post->ID, '_fl_builder_data', true );
7881 foreach ( (array) $meta as $node_id => $node ) {
7882 if ( isset( $node->type ) && 'module' === $node->type ) {
7883 $settings = $node->settings;
7884 if ( in_array( $settings->type, $allowed_types, true ) ) {
7885 $label = $post->post_title;
7886 if ( '' !== $settings->node_label ) {
7887 $label .= ' - ' . $settings->node_label;
7888 }
7889 $templates[] = [
7890 'label' => $label,
7891 'value' => $node_id,
7892 ];
7893 }
7894 }
7895 }
7896 }
7897 }
7898 return $templates;
7899 }
7900
7901 /**
7902 * Search beaver builder forms.
7903 *
7904 * @param array $data data.
7905 * @return array
7906 */
7907 public function search_beaver_builder_forms( $data ) {
7908 $templates = $this->get_beaver_builder_templates();
7909 return [
7910 'options' => $templates,
7911 'hasMore' => false,
7912 ];
7913 }
7914
7915 /**
7916 * Search FluentCRM fields.
7917 *
7918 * @param array $data data.
7919 * @return array
7920 */
7921 public function search_fluentcrm_custom_fields( $data ) {
7922 $context = [];
7923 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
7924 $context['fields'] = $custom_fields;
7925 return $context;
7926 }
7927
7928 /**
7929 * Search FluentCRM Company fields.
7930 *
7931 * @param array $data data.
7932 * @return array
7933 */
7934 public function search_fluentcrm_company_custom_fields( $data ) {
7935 $context = [];
7936 if ( function_exists( 'fluentcrm_get_custom_company_fields' ) ) {
7937 $custom_fields = fluentcrm_get_custom_company_fields();
7938 $context['fields'] = $custom_fields;
7939 }
7940 return $context;
7941 }
7942
7943 /**
7944 * Search FluentCRM fields and display it in dropdown for trigger.
7945 *
7946 * @param array $data data.
7947 * @return array
7948 */
7949 public function search_fluentcrm_custom_fields_data( $data ) {
7950
7951 if ( ! class_exists( 'FluentCrm\App\Models\CustomContactField' ) ) {
7952 return [];
7953 }
7954
7955 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
7956 $options = [];
7957
7958 if ( ! empty( $custom_fields ) ) {
7959 foreach ( $custom_fields as $custom_field ) {
7960 $options[] = [
7961 'label' => $custom_field['label'],
7962 'value' => $custom_field['slug'],
7963 ];
7964 }
7965 }
7966
7967 return [
7968 'options' => $options,
7969 'hasMore' => false,
7970 ];
7971 }
7972
7973 /**
7974 * Fetch WP JOB Manager Last Data.
7975 *
7976 * @param array $data data.
7977 * @return array
7978 */
7979 public function search_wp_job_manger_last_data( $data ) {
7980 global $wpdb;
7981 $job_type = $data['filter']['job_type']['value'];
7982 $args = [
7983 'posts_per_page' => 1,
7984 'post_type' => 'job_listing',
7985 'orderby' => 'id',
7986 'order' => 'DESC',
7987 ];
7988
7989 if ( -1 !== $job_type ) {
7990 $args['tax_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
7991 [
7992 'taxonomy' => 'job_listing_type',
7993 'field' => 'term_id',
7994 'terms' => $job_type,
7995 ],
7996 ];
7997 }
7998 $posts = get_posts( $args );
7999 if ( empty( $posts ) ) {
8000 $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 );
8001 return $context;
8002 }
8003
8004 $post = $posts[0];
8005 $post_content = WordPress::get_post_context( $post->ID );
8006 $post_meta = WordPress::get_post_meta( $post->ID );
8007 $job_data = array_merge( $post_content, $post_meta, WordPress::get_user_context( $post->post_author ) );
8008 foreach ( $job_data as $key => $job ) {
8009 $newkey = str_replace( 'post', 'wpjob', $key );
8010 unset( $job_data[ $key ] );
8011 $job_data[ $newkey ] = $job;
8012 }
8013 $context['response_type'] = 'live';
8014 $context['pluggable_data'] = $job_data;
8015 return $context;
8016
8017 }
8018
8019 /**
8020 * Get Amelia Appointment Category.
8021 *
8022 * @param array $data data.
8023 *
8024 * @return array
8025 */
8026 public function search_amelia_category_list( $data ) {
8027
8028 global $wpdb;
8029
8030 $page = $data['page'];
8031 $limit = Utilities::get_search_page_limit();
8032 $offset = $limit * ( $page - 1 );
8033
8034 $categories = $wpdb->get_results(
8035 $wpdb->prepare(
8036 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_categories WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
8037 [ 'visible', $limit, $offset ]
8038 ),
8039 OBJECT
8040 );
8041
8042 $categories_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
8043
8044 $options = [];
8045 if ( ! empty( $categories ) ) {
8046 foreach ( $categories as $category ) {
8047 $options[] = [
8048 'label' => $category->name,
8049 'value' => $category->id,
8050 ];
8051 }
8052 }
8053
8054 return [
8055 'options' => $options,
8056 'hasMore' => $categories_count > $limit && $categories_count > $offset,
8057 ];
8058
8059 }
8060
8061 /**
8062 * Get Amelia Appointment Services.
8063 *
8064 * @param array $data data.
8065 *
8066 * @return array
8067 */
8068 public function search_amelia_service_list( $data ) {
8069
8070 global $wpdb;
8071
8072 $page = $data['page'];
8073 $limit = Utilities::get_search_page_limit();
8074 $offset = $limit * ( $page - 1 );
8075
8076 $services = $wpdb->get_results(
8077 $wpdb->prepare(
8078 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_services
8079 WHERE categoryId = %d AND status = %s
8080 ORDER BY name ASC LIMIT %d OFFSET %d",
8081 [ $data['dynamic'], 'visible', $limit, $offset ]
8082 ),
8083 OBJECT
8084 );
8085
8086 $services_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
8087
8088 $options = [];
8089 if ( ! empty( $services ) ) {
8090 foreach ( $services as $category ) {
8091 $options[] = [
8092 'label' => $category->name,
8093 'value' => $category->id,
8094 ];
8095 }
8096 }
8097
8098 return [
8099 'options' => $options,
8100 'hasMore' => $services_count > $limit && $services_count > $offset,
8101 ];
8102
8103 }
8104
8105 /**
8106 * Get Amelia Events.
8107 *
8108 * @param array $data data.
8109 *
8110 * @return array
8111 */
8112 public function search_amelia_events_list( $data ) {
8113
8114 global $wpdb;
8115
8116 $page = $data['page'];
8117 $limit = Utilities::get_search_page_limit();
8118 $offset = $limit * ( $page - 1 );
8119
8120 $events = $wpdb->get_results(
8121 $wpdb->prepare(
8122 "SELECT SQL_CALC_FOUND_ROWS id, name from {$wpdb->prefix}amelia_events WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
8123 [ 'approved', $limit, $offset ]
8124 ),
8125 OBJECT
8126 );
8127
8128 $list_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
8129
8130 $options = [];
8131 if ( ! empty( $events ) ) {
8132 foreach ( $events as $event ) {
8133 $options[] = [
8134 'label' => $event->name,
8135 'value' => $event->id,
8136 ];
8137 }
8138 }
8139
8140 return [
8141 'options' => $options,
8142 'hasMore' => $list_count > $limit && $list_count > $offset,
8143 ];
8144
8145 }
8146
8147 /**
8148 * Get Amelia Events.
8149 *
8150 * @param array $data data.
8151 *
8152 * @return array
8153 */
8154 public function search_amelia_booking_status_list( $data ) {
8155
8156 $options = [];
8157 $options[] = [
8158 'label' => 'Approved',
8159 'value' => 'approved',
8160 ];
8161 $options[] = [
8162 'label' => 'Pending',
8163 'value' => 'pending',
8164 ];
8165 $options[] = [
8166 'label' => 'Rejected',
8167 'value' => 'rejected',
8168 ];
8169 $options[] = [
8170 'label' => 'Canceled',
8171 'value' => 'canceled',
8172 ];
8173 $options[] = [
8174 'label' => 'No-show',
8175 'value' => 'no-show',
8176 ];
8177 return [
8178 'options' => $options,
8179 'hasMore' => false,
8180 ];
8181 }
8182
8183 /**
8184 * Get last data for trigger.
8185 *
8186 * @param array $data data.
8187 * @return array
8188 */
8189 public function search_amelia_appointment_booked_triggers_last_data( $data ) {
8190 global $wpdb;
8191
8192 $context = [];
8193
8194 $appointment_category = $data['filter']['amelia_category_list']['value'];
8195 $appointment_service = $data['filter']['amelia_service_list']['value'];
8196
8197 if ( -1 === $appointment_service ) {
8198 // If service exists as per category selected.
8199 $service_exist = $wpdb->get_row(
8200 $wpdb->prepare(
8201 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
8202 [ $appointment_category ]
8203 ),
8204 ARRAY_A
8205 );
8206
8207 if ( empty( $service_exist ) ) {
8208 $result = [];
8209 } else {
8210 $result = $wpdb->get_row(
8211 '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 )',
8212 ARRAY_A
8213 );
8214 }
8215 } else {
8216 $result = $wpdb->get_row(
8217 $wpdb->prepare(
8218 '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',
8219 [ $appointment_service ]
8220 ),
8221 ARRAY_A
8222 );
8223 }
8224
8225 if ( ! empty( $result ) ) {
8226
8227 $payment_result = $wpdb->get_row(
8228 $wpdb->prepare(
8229 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
8230 [ $result['id'] ]
8231 ),
8232 ARRAY_A
8233 );
8234
8235 $customer_result = $wpdb->get_row(
8236 $wpdb->prepare(
8237 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8238 [ $result['customerId'] ]
8239 ),
8240 ARRAY_A
8241 );
8242
8243 $service_result = $wpdb->get_row(
8244 $wpdb->prepare(
8245 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
8246 [ $result['serviceId'] ]
8247 ),
8248 ARRAY_A
8249 );
8250
8251 $category_result = $wpdb->get_row(
8252 $wpdb->prepare(
8253 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
8254 [ $service_result['categoryId'] ]
8255 ),
8256 ARRAY_A
8257 );
8258
8259 if ( $result['couponId'] ) {
8260 $coupon_result = $wpdb->get_row(
8261 $wpdb->prepare(
8262 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8263 [ $result['couponId'] ]
8264 ),
8265 ARRAY_A
8266 );
8267 } else {
8268 $coupon_result = [];
8269 }
8270
8271 if ( ! empty( $result['customFields'] ) ) {
8272 $custom_fields = json_decode( $result['customFields'], true );
8273
8274 $fields_arr = [];
8275 foreach ( (array) $custom_fields as $fields ) {
8276 if ( is_array( $fields ) ) {
8277 $fields_arr[ $fields['label'] ] = $fields['value'];
8278 }
8279 }
8280 unset( $result['customFields'] );
8281 } else {
8282 $fields_arr = [];
8283 }
8284
8285 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
8286 $context['response_type'] = 'live';
8287 } else {
8288
8289 $context = json_decode( '{"response_type":"sample","pluggable_data":{"id":"1","status":"visible","bookingStart":"2023-02-28 13:00:00","bookingEnd":"2023-02-28 14:00:00","notifyParticipants":"1","serviceId":"4","packageId":null,"providerId":"2","locationId":null,"internalNotes":"","googleCalendarEventId":null,"googleMeetUrl":null,"outlookCalendarEventId":null,"zoomMeeting":null,"lessonSpace":null,"parentId":null,"appointmentId":"1","customerId":"1","price":"15","persons":"1","couponId":null,"token":"02cf0988c6","info":"{\"firstName\":\"John\",\"lastName\":\"Doe\",\"phone\":\"1 (234) 789\",\"locale\":\"en_US\",\"timeZone\":\"Asia\\\/Kolkata\",\"urlParams\":null}","utcOffset":null,"aggregatedPrice":"1","packageCustomerServiceId":null,"duration":"3600","created":"2023-02-08 11:16:03","actionsCompleted":"1","Do You Know Automation?":"Yes","When Are You Coming?":"2023-04-20","Upload Something":"","Tell Us About You!":"Hey there!","customerBookingId":"103","amount":"0","dateTime":"2023-02-28 13:00:00","gateway":"onSite","gatewayTitle":"","data":"","packageCustomerId":null,"entity":"appointment","wcOrderId":null,"type":"customer","externalId":"89","firstName":"John","lastName":"Doe","email":"johnd@gmail.com","birthday":null,"phone":"1 (234) 789","gender":null,"note":null,"description":null,"pictureFullPath":null,"pictureThumbPath":null,"password":null,"usedTokens":null,"zoomUserId":null,"countryPhoneIso":"us","translations":"{\"defaultLanguage\":\"en_US\"}","timeZone":null,"serviceName":"demo service","serviceDescription":"","categoryId":"2","categoryName":"New Category1"}}', true );
8290 }
8291
8292 return $context;
8293 }
8294
8295 /**
8296 * Get last data for trigger.
8297 *
8298 * @param array $data data.
8299 * @return array
8300 */
8301 public function search_amelia_appointment_booking_status_changed_triggers_last_data( $data ) {
8302 global $wpdb;
8303
8304 $context = [];
8305
8306 $appointment_category = $data['filter']['amelia_category_list']['value'];
8307 $appointment_service = $data['filter']['amelia_service_list']['value'];
8308 $appointment_status = $data['filter']['appointment_status']['value'];
8309
8310 if ( -1 === $appointment_service ) {
8311 // If service exists as per category selected.
8312 $service_exist = $wpdb->get_row(
8313 $wpdb->prepare(
8314 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
8315 [ $appointment_category ]
8316 ),
8317 ARRAY_A
8318 );
8319
8320 if ( empty( $service_exist ) ) {
8321 $result = [];
8322 } else {
8323 $result = $wpdb->get_row(
8324 '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 )',
8325 ARRAY_A
8326 );
8327 }
8328 } else {
8329 if ( -1 === $appointment_status ) {
8330 $result = $wpdb->get_row(
8331 $wpdb->prepare(
8332 'SELECT appointments.*, customer.* FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer INNER JOIN ' . $wpdb->prefix . 'amelia_appointments as appointments ON customer.appointmentId=appointments.id WHERE appointments.serviceId = %d',
8333 [ $appointment_service ]
8334 ),
8335 ARRAY_A
8336 );
8337 } else {
8338 $result = $wpdb->get_row(
8339 $wpdb->prepare(
8340 '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',
8341 [ $appointment_status, $appointment_service ]
8342 ),
8343 ARRAY_A
8344 );
8345 }
8346 }
8347
8348 if ( ! empty( $result ) ) {
8349
8350 $payment_result = $wpdb->get_row(
8351 $wpdb->prepare(
8352 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
8353 [ $result['id'] ]
8354 ),
8355 ARRAY_A
8356 );
8357
8358 $customer_result = $wpdb->get_row(
8359 $wpdb->prepare(
8360 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8361 [ $result['customerId'] ]
8362 ),
8363 ARRAY_A
8364 );
8365
8366 $service_result = $wpdb->get_row(
8367 $wpdb->prepare(
8368 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
8369 [ $result['serviceId'] ]
8370 ),
8371 ARRAY_A
8372 );
8373
8374 $category_result = $wpdb->get_row(
8375 $wpdb->prepare(
8376 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
8377 [ $service_result['categoryId'] ]
8378 ),
8379 ARRAY_A
8380 );
8381
8382 if ( $result['couponId'] ) {
8383 $coupon_result = $wpdb->get_row(
8384 $wpdb->prepare(
8385 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8386 [ $result['couponId'] ]
8387 ),
8388 ARRAY_A
8389 );
8390 } else {
8391 $coupon_result = [];
8392 }
8393
8394 if ( ! empty( $result['customFields'] ) ) {
8395 $custom_fields = json_decode( $result['customFields'], true );
8396
8397 $fields_arr = [];
8398 foreach ( (array) $custom_fields as $fields ) {
8399 if ( is_array( $fields ) ) {
8400 $fields_arr[ $fields['label'] ] = $fields['value'];
8401 }
8402 }
8403 unset( $result['customFields'] );
8404 } else {
8405 $fields_arr = [];
8406 }
8407
8408 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
8409 $context['pluggable_data']['amelia_category_list'] = $appointment_category;
8410 if ( -1 === $appointment_status ) {
8411 $context['pluggable_data']['appointment_status'] = 'approved';
8412 } else {
8413 $context['pluggable_data']['appointment_status'] = $appointment_status;
8414 }
8415 if ( -1 === $appointment_service ) {
8416 $context['pluggable_data']['amelia_service_list'] = $service_result['id'];
8417 } else {
8418 $context['pluggable_data']['amelia_service_list'] = $appointment_service;
8419 }
8420 $context['response_type'] = 'live';
8421 } else {
8422
8423 $context = json_decode( '{"response_type":"sample","pluggable_data":{"id":"1","status":"visible","bookingStart":"2023-02-28 13:00:00","bookingEnd":"2023-02-28 14:00:00","notifyParticipants":"1","serviceId":"4","packageId":null,"providerId":"2","locationId":null,"internalNotes":"","googleCalendarEventId":null,"googleMeetUrl":null,"outlookCalendarEventId":null,"zoomMeeting":null,"lessonSpace":null,"parentId":null,"appointmentId":"1","customerId":"1","price":"15","persons":"1","couponId":null,"token":"02cf0988c6","info":"{\"firstName\":\"John\",\"lastName\":\"Doe\",\"phone\":\"1 (234) 789\",\"locale\":\"en_US\",\"timeZone\":\"Asia\\\/Kolkata\",\"urlParams\":null}","utcOffset":null,"aggregatedPrice":"1","packageCustomerServiceId":null,"duration":"3600","created":"2023-02-08 11:16:03","actionsCompleted":"1","Do You Know Automation?":"Yes","When Are You Coming?":"2023-04-20","Upload Something":"","Tell Us About You!":"Hey there!","customerBookingId":"103","amount":"0","dateTime":"2023-02-28 13:00:00","gateway":"onSite","gatewayTitle":"","data":"","packageCustomerId":null,"entity":"appointment","wcOrderId":null,"type":"customer","externalId":"89","firstName":"John","lastName":"Doe","email":"johnd@gmail.com","birthday":null,"phone":"1 (234) 789","gender":null,"note":null,"description":null,"pictureFullPath":null,"pictureThumbPath":null,"password":null,"usedTokens":null,"zoomUserId":null,"countryPhoneIso":"us","translations":"{\"defaultLanguage\":\"en_US\"}","timeZone":null,"serviceName":"demo service","serviceDescription":"","categoryId":"2","categoryName":"New Category1","amelia_category_list": "2","appointment_status": "approved","amelia_service_list": "4"}}', true );
8424 }
8425
8426 return $context;
8427 }
8428
8429 /**
8430 * Get last data for trigger.
8431 *
8432 * @param array $data data.
8433 * @return array
8434 */
8435 public function search_amelia_event_status_changed_triggers_last_data( $data ) {
8436 global $wpdb;
8437
8438 $context = [];
8439
8440 $event_selected = $data['filter']['amelia_events_list']['value'];
8441 $event_status = $data['filter']['event_booking_status']['value'];
8442
8443 $query = 'SELECT *
8444 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8445 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8446 ON customer.id = event_period.customerBookingId';
8447
8448 if ( -1 === $event_selected ) {
8449 if ( -1 !== $event_status ) {
8450 $query .= $wpdb->prepare(
8451 ' WHERE event_period.customerBookingId = (
8452 SELECT MAX(customerBookingId)
8453 FROM ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods
8454 ) AND customer.status = %s',
8455 $event_status
8456 );
8457 } else {
8458 $query .= ' WHERE event_period.customerBookingId = (
8459 SELECT MAX(customerBookingId)
8460 FROM ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods
8461 )';
8462 }
8463 } else {
8464 if ( -1 !== $event_status ) {
8465 $query .= $wpdb->prepare(
8466 ' WHERE event_period.eventPeriodId = %d AND customer.status = %s',
8467 $event_selected,
8468 $event_status
8469 );
8470 } else {
8471 $query .= $wpdb->prepare(
8472 ' WHERE event_period.eventPeriodId = %d',
8473 $event_selected
8474 );
8475 }
8476 }
8477
8478 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
8479
8480 if ( ! empty( $result ) ) {
8481 $event = $wpdb->get_row(
8482 $wpdb->prepare(
8483 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
8484 [ $result['eventPeriodId'] ]
8485 ),
8486 ARRAY_A
8487 );
8488 $event_tags = $wpdb->get_results(
8489 $wpdb->prepare(
8490 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
8491 [ $result['eventPeriodId'] ]
8492 ),
8493 ARRAY_A
8494 );
8495 $tags = [];
8496 if ( ! empty( $event_tags ) ) {
8497 foreach ( $event_tags as $key => $tag ) {
8498 $tags['event_tag'][ $key ] = $tag['name'];
8499 }
8500 } else {
8501 $tags = [];
8502 }
8503 $customer_result = $wpdb->get_row(
8504 $wpdb->prepare(
8505 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8506 [ $result['customerId'] ]
8507 ),
8508 ARRAY_A
8509 );
8510 if ( $result['couponId'] ) {
8511 $coupon_result = $wpdb->get_row(
8512 $wpdb->prepare(
8513 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8514 [ $result['couponId'] ]
8515 ),
8516 ARRAY_A
8517 );
8518 } else {
8519 $coupon_result = [];
8520 }
8521 if ( ! empty( $result['customFields'] ) ) {
8522 $custom_fields = json_decode( $result['customFields'], true );
8523
8524 $fields_arr = [];
8525 foreach ( (array) $custom_fields as $fields ) {
8526 if ( is_array( $fields ) ) {
8527 $fields_arr[ $fields['label'] ] = $fields['value'];
8528 }
8529 }
8530 unset( $result['customFields'] );
8531 } else {
8532 $fields_arr = [];
8533 }
8534 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
8535 if ( -1 === $event_status ) {
8536 $context['pluggable_data']['event_booking_status'] = 'approved';
8537 } else {
8538 $context['pluggable_data']['event_booking_status'] = $event_status;
8539 }
8540 $context['response_type'] = 'live';
8541 } else {
8542
8543 $context = json_decode( '{"response_type": "sample","pluggable_data": {"id": "1","appointmentId": null,"customerId": "1","status": "visible","price": "10","persons": "1","couponId": null,"token": "6485b07ce9","info": "{\"firstName\":\"John\",\"lastName\":\"Doe\",\"phone\":\"+213551223123\",\"locale\":\"en_US\",\"timeZone\":\"Asia\\/Kolkata\",\"urlParams\":null}","utcOffset": null,"aggregatedPrice": "1","packageCustomerServiceId": null,"duration": null,"created": "2023-02-02 06:35:18","actionsCompleted": "1","Do You Know Automation?": "Yes","When Are You Coming?": "2023-04-20","Upload Something": "","Tell Us About You!": "Hey there!","customerBookingId": "105","eventPeriodId": "5","parentId": null,"name": "Music Event","bookingOpens": null,"bookingCloses": "2023-02-09 08:00:00","bookingOpensRec": "same","bookingClosesRec": "same","ticketRangeRec": "calculate","recurringCycle": null,"recurringOrder": null,"recurringInterval": null,"recurringMonthly": null,"monthlyDate": null,"monthlyOnRepeat": null,"monthlyOnDay": null,"recurringUntil": null,"maxCapacity": "12","maxCustomCapacity": null,"maxExtraPeople": null,"locationId": null,"customLocation": "Kolkata","description": null,"color": "#1788FB","show": "1","notifyParticipants": "1","settings": "{\"payments\":{\"onSite\":true,\"payPal\":{\"enabled\":false},\"stripe\":{\"enabled\":false},\"mollie\":{\"enabled\":false},\"razorpay\":{\"enabled\":false}},\"general\":{\"minimumTimeRequirementPriorToCanceling\":null,\"redirectUrlAfterAppointment\":null},\"zoom\":{\"enabled\":false},\"lessonSpace\":{\"enabled\":false}}","zoomUserId": null,"bringingAnyone": "1","bookMultipleTimes": "1","translations": "{\"defaultLanguage\":\"en_US\"}","depositPayment": "disabled","depositPerPerson": "1","fullPayment": "0","deposit": "0","customPricing": "0","organizerId": "2","closeAfterMin": null,"closeAfterMinBookings": "0","type": "customer","externalId": "91","firstName": "Jogn","lastName": "Doe","email": "johnd@gmail.com","birthday": null,"phone": "+213551223123","gender": null,"note": null,"pictureFullPath": null,"pictureThumbPath": null,"password": null,"usedTokens": null,"countryPhoneIso": "dz","timeZone": null,"event_booking_status":"approved"}}', true );
8544 }
8545
8546 return $context;
8547 }
8548
8549 /**
8550 * Get last data for trigger.
8551 *
8552 * @param array $data data.
8553 * @return array
8554 */
8555 public function search_amelia_new_event_attendee_triggers_last_data( $data ) {
8556 global $wpdb;
8557
8558 $context = [];
8559
8560 $event_selected = $data['filter']['amelia_events_list']['value'];
8561
8562 if ( -1 === $event_selected ) {
8563 $result = $wpdb->get_row(
8564 'SELECT *
8565 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8566 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8567 ON customer.id = event_period.customerBookingId
8568 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods )',
8569 ARRAY_A
8570 );
8571 } else {
8572 $result = $wpdb->get_row(
8573 $wpdb->prepare(
8574 'SELECT *
8575 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8576 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8577 ON customer.id = event_period.customerBookingId
8578 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId = %d',
8579 [ $event_selected ]
8580 ),
8581 ARRAY_A
8582 );
8583 }
8584
8585 if ( ! empty( $result ) ) {
8586
8587 $event = $wpdb->get_row(
8588 $wpdb->prepare(
8589 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
8590 [ $result['eventPeriodId'] ]
8591 ),
8592 ARRAY_A
8593 );
8594 $event_tags = $wpdb->get_results(
8595 $wpdb->prepare(
8596 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
8597 [ $result['eventPeriodId'] ]
8598 ),
8599 ARRAY_A
8600 );
8601 $tags = [];
8602 if ( ! empty( $event_tags ) ) {
8603 foreach ( $event_tags as $key => $tag ) {
8604 $tags['event_tag'][ $key ] = $tag['name'];
8605 }
8606 } else {
8607 $tags = [];
8608 }
8609
8610 $customer_result = $wpdb->get_row(
8611 $wpdb->prepare(
8612 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8613 [ $result['customerId'] ]
8614 ),
8615 ARRAY_A
8616 );
8617
8618 if ( $result['couponId'] ) {
8619 $coupon_result = $wpdb->get_row(
8620 $wpdb->prepare(
8621 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8622 [ $result['couponId'] ]
8623 ),
8624 ARRAY_A
8625 );
8626 } else {
8627 $coupon_result = [];
8628 }
8629
8630 if ( ! empty( $result['customFields'] ) ) {
8631 $custom_fields = json_decode( $result['customFields'], true );
8632
8633 $fields_arr = [];
8634 foreach ( (array) $custom_fields as $fields ) {
8635 if ( is_array( $fields ) ) {
8636 $fields_arr[ $fields['label'] ] = $fields['value'];
8637 }
8638 }
8639 unset( $result['customFields'] );
8640 } else {
8641 $fields_arr = [];
8642 }
8643
8644 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
8645 $context['response_type'] = 'live';
8646 } else {
8647
8648 $context = json_decode( '{"response_type": "sample","pluggable_data": {"id": "1","appointmentId": null,"customerId": "1","status": "visible","price": "10","persons": "1","couponId": null,"token": "6485b07ce9","info": "{\"firstName\":\"John\",\"lastName\":\"Doe\",\"phone\":\"+213551223123\",\"locale\":\"en_US\",\"timeZone\":\"Asia\\/Kolkata\",\"urlParams\":null}","utcOffset": null,"aggregatedPrice": "1","packageCustomerServiceId": null,"duration": null,"created": "2023-02-02 06:35:18","actionsCompleted": "1","Do You Know Automation?": "Yes","When Are You Coming?": "2023-04-20","Upload Something": "","Tell Us About You!": "Hey there!","customerBookingId": "105","eventPeriodId": "5","parentId": null,"name": "Music Event","bookingOpens": null,"bookingCloses": "2023-02-09 08:00:00","bookingOpensRec": "same","bookingClosesRec": "same","ticketRangeRec": "calculate","recurringCycle": null,"recurringOrder": null,"recurringInterval": null,"recurringMonthly": null,"monthlyDate": null,"monthlyOnRepeat": null,"monthlyOnDay": null,"recurringUntil": null,"maxCapacity": "12","maxCustomCapacity": null,"maxExtraPeople": null,"locationId": null,"customLocation": "Kolkata","description": null,"color": "#1788FB","show": "1","notifyParticipants": "1","settings": "{\"payments\":{\"onSite\":true,\"payPal\":{\"enabled\":false},\"stripe\":{\"enabled\":false},\"mollie\":{\"enabled\":false},\"razorpay\":{\"enabled\":false}},\"general\":{\"minimumTimeRequirementPriorToCanceling\":null,\"redirectUrlAfterAppointment\":null},\"zoom\":{\"enabled\":false},\"lessonSpace\":{\"enabled\":false}}","zoomUserId": null,"bringingAnyone": "1","bookMultipleTimes": "1","translations": "{\"defaultLanguage\":\"en_US\"}","depositPayment": "disabled","depositPerPerson": "1","fullPayment": "0","deposit": "0","customPricing": "0","organizerId": "2","closeAfterMin": null,"closeAfterMinBookings": "0","type": "customer","externalId": "91","firstName": "John","lastName": "Doe","email": "johnd@gmail.com","birthday": null,"phone": "+213551223123","gender": null,"note": null,"pictureFullPath": null,"pictureThumbPath": null,"password": null,"usedTokens": null,"countryPhoneIso": "dz","timeZone": null}}', true );
8649 }
8650
8651 return $context;
8652 }
8653
8654 /**
8655 * Get last data for trigger.
8656 *
8657 * @param array $data data.
8658 * @return array
8659 */
8660 public function search_amelia_event_booking_cancelled_triggers_last_data( $data ) {
8661 global $wpdb;
8662
8663 $context = [];
8664
8665 $event_selected = $data['filter']['amelia_events_list']['value'];
8666
8667 if ( -1 === $event_selected ) {
8668 $result = $wpdb->get_row(
8669 'SELECT *
8670 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8671 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8672 ON customer.id = event_period.customerBookingId
8673 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND customer.status = "canceled"',
8674 ARRAY_A
8675 );
8676 } else {
8677 $result = $wpdb->get_row(
8678 $wpdb->prepare(
8679 'SELECT *
8680 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8681 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8682 ON customer.id = event_period.customerBookingId
8683 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId = %d AND customer.status = "canceled"',
8684 [ $event_selected ]
8685 ),
8686 ARRAY_A
8687 );
8688 }
8689
8690 if ( ! empty( $result ) ) {
8691 $event = $wpdb->get_row(
8692 $wpdb->prepare(
8693 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
8694 [ $result['eventPeriodId'] ]
8695 ),
8696 ARRAY_A
8697 );
8698 $event_tags = $wpdb->get_results(
8699 $wpdb->prepare(
8700 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
8701 [ $result['eventPeriodId'] ]
8702 ),
8703 ARRAY_A
8704 );
8705 $tags = [];
8706 if ( ! empty( $event_tags ) ) {
8707 foreach ( $event_tags as $key => $tag ) {
8708 $tags['event_tag'][ $key ] = $tag['name'];
8709 }
8710 } else {
8711 $tags = [];
8712 }
8713 $customer_result = $wpdb->get_row(
8714 $wpdb->prepare(
8715 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8716 [ $result['customerId'] ]
8717 ),
8718 ARRAY_A
8719 );
8720 if ( $result['couponId'] ) {
8721 $coupon_result = $wpdb->get_row(
8722 $wpdb->prepare(
8723 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8724 [ $result['couponId'] ]
8725 ),
8726 ARRAY_A
8727 );
8728 } else {
8729 $coupon_result = [];
8730 }
8731 if ( ! empty( $result['customFields'] ) ) {
8732 $custom_fields = json_decode( $result['customFields'], true );
8733
8734 $fields_arr = [];
8735 foreach ( (array) $custom_fields as $fields ) {
8736 if ( is_array( $fields ) ) {
8737 $fields_arr[ $fields['label'] ] = $fields['value'];
8738 }
8739 }
8740 unset( $result['customFields'] );
8741 } else {
8742 $fields_arr = [];
8743 }
8744 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
8745 $context['response_type'] = 'live';
8746 } else {
8747
8748 $context = json_decode( '{"response_type": "sample","pluggable_data": {"id": "1","appointmentId": null,"customerId": "1","status": "visible","price": "10","persons": "1","couponId": null,"token": "6485b07ce9","info": "{\"firstName\":\"John\",\"lastName\":\"Doe\",\"phone\":\"+213551223123\",\"locale\":\"en_US\",\"timeZone\":\"Asia\\/Kolkata\",\"urlParams\":null}","utcOffset": null,"aggregatedPrice": "1","packageCustomerServiceId": null,"duration": null,"created": "2023-02-02 06:35:18","actionsCompleted": "1","Do You Know Automation?": "Yes","When Are You Coming?": "2023-04-20","Upload Something": "","Tell Us About You!": "Hey there!","customerBookingId": "105","eventPeriodId": "5","parentId": null,"name": "Music Event","bookingOpens": null,"bookingCloses": "2023-02-09 08:00:00","bookingOpensRec": "same","bookingClosesRec": "same","ticketRangeRec": "calculate","recurringCycle": null,"recurringOrder": null,"recurringInterval": null,"recurringMonthly": null,"monthlyDate": null,"monthlyOnRepeat": null,"monthlyOnDay": null,"recurringUntil": null,"maxCapacity": "12","maxCustomCapacity": null,"maxExtraPeople": null,"locationId": null,"customLocation": "Kolkata","description": null,"color": "#1788FB","show": "1","notifyParticipants": "1","settings": "{\"payments\":{\"onSite\":true,\"payPal\":{\"enabled\":false},\"stripe\":{\"enabled\":false},\"mollie\":{\"enabled\":false},\"razorpay\":{\"enabled\":false}},\"general\":{\"minimumTimeRequirementPriorToCanceling\":null,\"redirectUrlAfterAppointment\":null},\"zoom\":{\"enabled\":false},\"lessonSpace\":{\"enabled\":false}}","zoomUserId": null,"bringingAnyone": "1","bookMultipleTimes": "1","translations": "{\"defaultLanguage\":\"en_US\"}","depositPayment": "disabled","depositPerPerson": "1","fullPayment": "0","deposit": "0","customPricing": "0","organizerId": "2","closeAfterMin": null,"closeAfterMinBookings": "0","type": "customer","externalId": "91","firstName": "Jogn","lastName": "Doe","email": "johnd@gmail.com","birthday": null,"phone": "+213551223123","gender": null,"note": null,"pictureFullPath": null,"pictureThumbPath": null,"password": null,"usedTokens": null,"countryPhoneIso": "dz","timeZone": null}}', true );
8749 }
8750
8751 return $context;
8752 }
8753
8754 /**
8755 * Get last data for trigger.
8756 *
8757 * @param array $data data.
8758 * @return array
8759 */
8760 public function search_amelia_appointment_rescheduled_triggers_last_data( $data ) {
8761 global $wpdb;
8762
8763 $context = [];
8764
8765 $appointment_category = $data['filter']['amelia_category_list']['value'];
8766 $appointment_service = $data['filter']['amelia_service_list']['value'];
8767
8768 if ( -1 === $appointment_service ) {
8769 // If service exists as per category selected.
8770 $service_exist = $wpdb->get_row(
8771 $wpdb->prepare(
8772 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
8773 [ $appointment_category ]
8774 ),
8775 ARRAY_A
8776 );
8777
8778 if ( empty( $service_exist ) ) {
8779 $result = [];
8780 } else {
8781 $result = $wpdb->get_row(
8782 '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 )',
8783 ARRAY_A
8784 );
8785 }
8786 } else {
8787 $result = $wpdb->get_row(
8788 $wpdb->prepare(
8789 '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',
8790 [ $appointment_service ]
8791 ),
8792 ARRAY_A
8793 );
8794 }
8795
8796 if ( ! empty( $result ) ) {
8797
8798 $payment_result = $wpdb->get_row(
8799 $wpdb->prepare(
8800 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
8801 [ $result['id'] ]
8802 ),
8803 ARRAY_A
8804 );
8805
8806 $customer_result = $wpdb->get_row(
8807 $wpdb->prepare(
8808 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8809 [ $result['customerId'] ]
8810 ),
8811 ARRAY_A
8812 );
8813
8814 $service_result = $wpdb->get_row(
8815 $wpdb->prepare(
8816 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
8817 [ $result['serviceId'] ]
8818 ),
8819 ARRAY_A
8820 );
8821
8822 $category_result = $wpdb->get_row(
8823 $wpdb->prepare(
8824 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
8825 [ $service_result['categoryId'] ]
8826 ),
8827 ARRAY_A
8828 );
8829
8830 if ( $result['couponId'] ) {
8831 $coupon_result = $wpdb->get_row(
8832 $wpdb->prepare(
8833 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8834 [ $result['couponId'] ]
8835 ),
8836 ARRAY_A
8837 );
8838 } else {
8839 $coupon_result = [];
8840 }
8841
8842 if ( ! empty( $result['customFields'] ) ) {
8843 $custom_fields = json_decode( $result['customFields'], true );
8844
8845 $fields_arr = [];
8846 foreach ( (array) $custom_fields as $fields ) {
8847 if ( is_array( $fields ) ) {
8848 $fields_arr[ $fields['label'] ] = $fields['value'];
8849 }
8850 }
8851 unset( $result['customFields'] );
8852 } else {
8853 $fields_arr = [];
8854 }
8855
8856 $appointment_data['isRescheduled'] = '1';
8857 $context['pluggable_data'] = array_merge( $result, $fields_arr, $appointment_data, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
8858 $context['response_type'] = 'live';
8859 } else {
8860
8861 $context = json_decode( '{"response_type":"sample","pluggable_data":{"id":"1","status":"visible","bookingStart":"2023-02-28 15:30:00","bookingEnd":"2023-02-28 16:30:00","notifyParticipants":"1","serviceId":"4","packageId":null,"providerId":"2","locationId":null,"internalNotes":"","googleCalendarEventId":null,"googleMeetUrl":null,"outlookCalendarEventId":null,"zoomMeeting":null,"lessonSpace":null,"parentId":null,"appointmentId":"54","customerId":"1","price":"15","persons":"1","couponId":null,"token":"02cf0988c6","info":"{\"firstName\":\"John\",\"lastName\":\"Doe\",\"phone\":\"1 (234) 789\",\"locale\":\"en_US\",\"timeZone\":\"Asia\\\/Kolkata\",\"urlParams\":null}","utcOffset":null,"aggregatedPrice":"1","packageCustomerServiceId":null,"duration":"3600","created":"2023-02-08 11:16:03","actionsCompleted":"1","Do You Know Automation?":"Yes","When Are You Coming?":"2023-04-20","Upload Something":"","Tell Us About You!":"Hey there!","isRescheduled":"1","customerBookingId":"103","amount":"0","dateTime":"2023-02-28 15:30:00","gateway":"onSite","gatewayTitle":"","data":"","packageCustomerId":null,"entity":"appointment","wcOrderId":null,"type":"customer","externalId":"89","firstName":"John","lastName":"Doe","email":"johnd@gmail.com","birthday":null,"phone":"1 (234) 789","gender":null,"note":null,"description":null,"pictureFullPath":null,"pictureThumbPath":null,"password":null,"usedTokens":null,"zoomUserId":null,"countryPhoneIso":"us","translations":"{\"defaultLanguage\":\"en_US\"}","timeZone":null,"serviceName":"demo service","serviceDescription":"","categoryId":"2","categoryName":"New Category1"}}', true );
8862 }
8863
8864 return $context;
8865 }
8866
8867
8868 /**
8869 * Get last data for trigger.
8870 *
8871 * @param array $data data.
8872 * @return array
8873 */
8874 public function search_amelia_appointment_cancelled_triggers_last_data( $data ) {
8875 global $wpdb;
8876
8877 $context = [];
8878
8879 $appointment_category = $data['filter']['amelia_category_list']['value'];
8880 $appointment_service = $data['filter']['amelia_service_list']['value'];
8881
8882 if ( -1 === $appointment_service ) {
8883 // If service exists as per category selected.
8884 $service_exist = $wpdb->get_row(
8885 $wpdb->prepare(
8886 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
8887 [ $appointment_category ]
8888 ),
8889 ARRAY_A
8890 );
8891
8892 if ( empty( $service_exist ) ) {
8893 $result = [];
8894 } else {
8895 $result = $wpdb->get_row(
8896 $wpdb->prepare(
8897 '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',
8898 [ 'canceled' ]
8899 ),
8900 ARRAY_A
8901 );
8902 }
8903 } else {
8904 $result = $wpdb->get_row(
8905 $wpdb->prepare(
8906 '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',
8907 [ 'canceled', $appointment_service ]
8908 ),
8909 ARRAY_A
8910 );
8911 }
8912
8913 if ( ! empty( $result ) ) {
8914
8915 $payment_result = $wpdb->get_row(
8916 $wpdb->prepare(
8917 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
8918 [ $result['id'] ]
8919 ),
8920 ARRAY_A
8921 );
8922
8923 $customer_result = $wpdb->get_row(
8924 $wpdb->prepare(
8925 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8926 [ $result['customerId'] ]
8927 ),
8928 ARRAY_A
8929 );
8930
8931 $service_result = $wpdb->get_row(
8932 $wpdb->prepare(
8933 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
8934 [ $result['serviceId'] ]
8935 ),
8936 ARRAY_A
8937 );
8938
8939 $category_result = $wpdb->get_row(
8940 $wpdb->prepare(
8941 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
8942 [ $service_result['categoryId'] ]
8943 ),
8944 ARRAY_A
8945 );
8946
8947 if ( $result['couponId'] ) {
8948 $coupon_result = $wpdb->get_row(
8949 $wpdb->prepare(
8950 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8951 [ $result['couponId'] ]
8952 ),
8953 ARRAY_A
8954 );
8955 } else {
8956 $coupon_result = [];
8957 }
8958
8959 if ( ! empty( $result['customFields'] ) ) {
8960 $custom_fields = json_decode( $result['customFields'], true );
8961
8962 $fields_arr = [];
8963 foreach ( (array) $custom_fields as $fields ) {
8964 if ( is_array( $fields ) ) {
8965 $fields_arr[ $fields['label'] ] = $fields['value'];
8966 }
8967 }
8968 unset( $result['customFields'] );
8969 } else {
8970 $fields_arr = [];
8971 }
8972
8973 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
8974 $context['response_type'] = 'live';
8975 } else {
8976
8977 $context = json_decode( '{"response_type":"sample","pluggable_data":{"id":"1","status":"visible","bookingStart":"2023-02-28 15:30:00","bookingEnd":"2023-02-28 16:30:00","notifyParticipants":"1","serviceId":"4","packageId":null,"providerId":"2","locationId":null,"internalNotes":"","googleCalendarEventId":null,"googleMeetUrl":null,"outlookCalendarEventId":null,"zoomMeeting":null,"lessonSpace":null,"parentId":null,"appointmentId":"54","customerId":"1","price":"15","persons":"1","couponId":null,"token":"02cf0988c6","info":"{\"firstName\":\"John\",\"lastName\":\"Doe\",\"phone\":\"1 (234) 789\",\"locale\":\"en_US\",\"timeZone\":\"Asia\\\/Kolkata\",\"urlParams\":null}","utcOffset":null,"aggregatedPrice":"1","packageCustomerServiceId":null,"duration":"3600","created":"2023-02-08 11:16:03","actionsCompleted":"1","Do You Know Automation?":"Yes","When Are You Coming?":"2023-04-20","Upload Something":"","Tell Us About You!":"Hey there!","customerBookingId":"103","amount":"0","dateTime":"2023-02-28 15:30:00","gateway":"onSite","gatewayTitle":"","data":"","packageCustomerId":null,"entity":"appointment","wcOrderId":null,"type":"customer","externalId":"89","firstName":"John","lastName":"Doe","email":"johnd@gmail.com","birthday":null,"phone":"1 (234) 789","gender":null,"note":null,"description":null,"pictureFullPath":null,"pictureThumbPath":null,"password":null,"usedTokens":null,"zoomUserId":null,"countryPhoneIso":"us","translations":"{\"defaultLanguage\":\"en_US\"}","timeZone":null,"serviceName":"demo service","serviceDescription":"","categoryId":"2","categoryName":"New Category1"}}', true );
8978 }
8979
8980 return $context;
8981 }
8982
8983 /**
8984 * Get MailPoet Forms.
8985 *
8986 * @param array $data data.
8987 *
8988 * @return array
8989 */
8990 public function search_mailpoet_forms( $data ) {
8991 if ( ! class_exists( '\MailPoet\API\API' ) ) {
8992 return;
8993 }
8994
8995 global $wpdb;
8996
8997 $page = $data['page'];
8998 $limit = Utilities::get_search_page_limit();
8999 $offset = $limit * ( $page - 1 );
9000
9001 $forms = $wpdb->get_results(
9002 $wpdb->prepare(
9003 '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',
9004 [ 'enabled', $limit, $offset ]
9005 )
9006 );
9007
9008 $form_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9009
9010 $options = [];
9011
9012 if ( ! empty( $forms ) ) {
9013 if ( is_array( $forms ) ) {
9014 foreach ( $forms as $form ) {
9015 $options[] = [
9016 'label' => $form->name,
9017 'value' => $form->id,
9018 ];
9019 }
9020 }
9021 }
9022
9023 return [
9024 'options' => $options,
9025 'hasMore' => $form_count > $limit && $form_count > $offset,
9026 ];
9027
9028 }
9029
9030 /**
9031 * Get MailPoet List.
9032 *
9033 * @param array $data data.
9034 *
9035 * @return array
9036 */
9037 public function search_mailpoet_list( $data ) {
9038 if ( ! class_exists( '\MailPoet\API\API' ) ) {
9039 return;
9040 }
9041
9042 $mailpoet = \MailPoet\API\API::MP( 'v1' );
9043 $lists = $mailpoet->getLists();
9044
9045 $options = [];
9046
9047 if ( ! empty( $lists ) ) {
9048 if ( is_array( $lists ) ) {
9049 foreach ( $lists as $list ) {
9050 $options[] = [
9051 'label' => $list['name'],
9052 'value' => $list['id'],
9053 ];
9054 }
9055 }
9056 }
9057
9058 return [
9059 'options' => $options,
9060 'hasMore' => false,
9061 ];
9062 }
9063
9064 /**
9065 * Get MailPoet Subscriber Status.
9066 *
9067 * @param array $data data.
9068 *
9069 * @return array
9070 */
9071 public function search_mailpoet_subscriber_status( $data ) {
9072 if ( ! class_exists( '\MailPoet\API\API' ) ) {
9073 return;
9074 }
9075
9076 $subscriber_status = [
9077 'subscribed' => 'Subscribed',
9078 'unconfirmed' => 'Unconfirmed',
9079 'unsubscribed' => 'Unsubscribed',
9080 'inactive' => 'Inactive',
9081 'bounced' => 'Bounced',
9082 ];
9083
9084 $options = [];
9085 foreach ( $subscriber_status as $key => $status ) {
9086 $options[] = [
9087 'label' => $status,
9088 'value' => $key,
9089 ];
9090 }
9091
9092 return [
9093 'options' => $options,
9094 'hasMore' => false,
9095 ];
9096 }
9097
9098 /**
9099 * Get MailPoet Subscribers.
9100 *
9101 * @param array $data data.
9102 *
9103 * @return array
9104 */
9105 public function search_mailpoet_subscribers( $data ) {
9106 if ( ! class_exists( '\MailPoet\API\API' ) ) {
9107 return;
9108 }
9109
9110 global $wpdb;
9111
9112 $page = $data['page'];
9113 $limit = Utilities::get_search_page_limit();
9114 $offset = $limit * ( $page - 1 );
9115
9116 $subscribers = $wpdb->get_results(
9117 $wpdb->prepare(
9118 'SELECT SQL_CALC_FOUND_ROWS id,email FROM ' . $wpdb->prefix . 'mailpoet_subscribers ORDER BY id DESC LIMIT %d OFFSET %d',
9119 [ $limit, $offset ]
9120 )
9121 );
9122
9123 $subscribers_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9124
9125 $options = [];
9126
9127 if ( ! empty( $subscribers ) ) {
9128 if ( is_array( $subscribers ) ) {
9129 foreach ( $subscribers as $subscriber ) {
9130 $options[] = [
9131 'label' => $subscriber->email,
9132 'value' => $subscriber->id,
9133 ];
9134 }
9135 }
9136 }
9137
9138 return [
9139 'options' => $options,
9140 'hasMore' => $subscribers_count > $limit && $subscribers_count > $offset,
9141 ];
9142 }
9143
9144 /**
9145 * Get ConvertPro Forms.
9146 *
9147 * @param array $data data.
9148 *
9149 * @return array
9150 */
9151 public function search_convertpro_form_list( $data ) {
9152 if ( ! class_exists( '\Cp_V2_Loader' ) ) {
9153 return;
9154 }
9155
9156 $cp_popups_inst = CP_V2_Popups::get_instance();
9157 $popups = $cp_popups_inst->get_all();
9158
9159 $form_count = count( $popups );
9160
9161 $page = $data['page'];
9162 $limit = Utilities::get_search_page_limit();
9163 $offset = $limit * ( $page - 1 );
9164
9165 $options = [];
9166
9167 if ( ! empty( $popups ) ) {
9168 if ( is_array( $popups ) ) {
9169 foreach ( $popups as $form ) {
9170 $options[] = [
9171 'label' => $form->post_title,
9172 'value' => $form->ID,
9173 ];
9174 }
9175 }
9176 }
9177
9178 return [
9179 'options' => $options,
9180 'hasMore' => $form_count > $limit && $form_count > $offset,
9181 ];
9182
9183 }
9184
9185 /**
9186 * Get ProjectHuddle Websites.
9187 *
9188 * @param array $data data.
9189 *
9190 * @return array
9191 */
9192 public function search_project_huddle_websites( $data ) {
9193
9194 $sites = new WP_Query(
9195 [
9196 'post_type' => 'ph-website',
9197 'posts_per_page' => - 1,
9198 'fields' => 'ids',
9199 ]
9200 );
9201
9202 $site_ids = (array) $sites->posts;
9203
9204 $options = [];
9205 if ( ! empty( $site_ids ) ) {
9206 if ( is_array( $site_ids ) ) {
9207 foreach ( $site_ids as $site_id ) {
9208 $options[] = [
9209 'label' => get_the_title( $site_id ),
9210 'value' => $site_id,
9211 ];
9212 }
9213 }
9214 }
9215 return [
9216 'options' => $options,
9217 'hasMore' => false,
9218 ];
9219
9220 }
9221
9222 /**
9223 * Get last data for trigger.
9224 *
9225 * @param array $data data.
9226 * @return mixed
9227 */
9228 public function search_project_huddle_comment_triggers_last_data( $data ) {
9229 global $wpdb;
9230
9231 $context = [];
9232
9233 if ( -1 !== $data['dynamic'] ) {
9234 $threads = get_posts(
9235 [
9236 'post_type' => 'phw_comment_loc',
9237 'posts_per_page' => 1,
9238 'meta_value' => $data['dynamic'],
9239 'meta_key' => 'project_id',
9240 'orderby' => 'asc',
9241 ]
9242 );
9243 } else {
9244 $threads = [];
9245 }
9246
9247 if ( ! empty( $threads ) ) {
9248 $comment_result = $wpdb->get_row(
9249 $wpdb->prepare(
9250 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
9251 FROM ' . $wpdb->prefix . 'comments
9252 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
9253 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
9254 LIMIT 0,1',
9255 $threads[0]->ID
9256 ),
9257 ARRAY_A
9258 );
9259
9260 if ( ! empty( $comment_result ) ) {
9261 $comment_id = get_comment( $comment_result['comment_ID'], ARRAY_A );
9262 $comments['comment_ID'] = isset( $comment_id['comment_ID'] ) ? $comment_id['comment_ID'] : '';
9263 $comments['comment_post_ID'] = isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : '';
9264
9265 $comments['comment_author'] = isset( $comment_id['comment_author'] ) ? $comment_id['comment_author'] : '';
9266
9267 $comments['comment_author_email'] = isset( $comment_id['comment_author_email'] ) ? $comment_id['comment_author_email'] : '';
9268
9269 $comments['comment_date'] = isset( $comment_id['comment_date'] ) ? $comment_id['comment_date'] : '';
9270
9271 $comments['comment_content'] = isset( $comment_id['comment_content'] ) ? $comment_id['comment_content'] : '';
9272
9273 $comments['comment_type'] = isset( $comment_id['comment_type'] ) ? $comment_id['comment_type'] : '';
9274
9275 $comment_item_id = get_comment_meta( $comment_result['comment_ID'], 'item_id' );
9276 if ( ! empty( $comment_item_id ) && is_array( $comment_item_id ) ) {
9277 $comments['comment_item_id'] = $comment_item_id[0];
9278 $comments['comment_item_page_title'] = get_the_title( (int) $comment_item_id[0] );
9279 $comments['comment_item_page_url'] = get_post_meta( (int) $comment_item_id[0], 'page_url', true );
9280 }
9281
9282 $context['pluggable_data'] = $comments;
9283 $context['response_type'] = 'live';
9284 } else {
9285 $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 );
9286 }
9287 } else {
9288 $context = json_decode( '{"response_type":"sample","pluggable_data":{"comment_ID":"1","comment_post_ID":"1","comment_author":"test","comment_author_email":"test@test.com","comment_date":"2023-03-27 13:44:26","comment_content":"<p>Leave comment<\/p>","comment_type":"ph_comment","comment_item_id": 9579, "comment_item_page_title": "About Us","comment_item_page_url": "https://example.com/abotu-us"}}', true );
9289 }
9290
9291 return $context;
9292 }
9293
9294 /**
9295 * Get last data for trigger.
9296 *
9297 * @param array $data data.
9298 * @return mixed
9299 */
9300 public function search_project_huddle_resolved_comment_triggers_last_data( $data ) {
9301 global $wpdb;
9302
9303 $context = [];
9304
9305 $get_comments = $wpdb->get_row(
9306 'SELECT ' . $wpdb->prefix . 'comments.comment_ID, ' . $wpdb->prefix . 'comments.comment_content
9307 FROM ' . $wpdb->prefix . 'comments
9308 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_status") AND comment_content = "Resolved"
9309 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
9310 LIMIT 0,1'
9311 );
9312
9313 if ( ! empty( $get_comments ) ) {
9314 $comment_id = get_comment( $get_comments->comment_ID, ARRAY_A );
9315 $comment_result = $wpdb->get_row(
9316 $wpdb->prepare(
9317 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
9318 FROM ' . $wpdb->prefix . 'comments
9319 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
9320 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
9321 LIMIT 0,1',
9322 isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : ''
9323 ),
9324 ARRAY_A
9325 );
9326
9327 $actual_comment = get_comment( $comment_result['comment_ID'], ARRAY_A );
9328 $comments['comment_ID'] = isset( $actual_comment['comment_ID'] ) ? $actual_comment['comment_ID'] : '';
9329 $comments['comment_post_ID'] = isset( $actual_comment['comment_post_ID'] ) ? $actual_comment['comment_post_ID'] : '';
9330 $comments['comment_author'] = isset( $actual_comment['comment_author'] ) ? $actual_comment['comment_author'] : '';
9331 $comments['comment_author_email'] = isset( $actual_comment['comment_author_email'] ) ? $actual_comment['comment_author_email'] : '';
9332 $comments['comment_date'] = isset( $actual_comment['comment_date'] ) ? $actual_comment['comment_date'] : '';
9333 $comments['comment_content'] = isset( $actual_comment['comment_content'] ) ? $actual_comment['comment_content'] : '';
9334 $comments['comment_type'] = isset( $actual_comment['comment_type'] ) ? $actual_comment['comment_type'] : '';
9335 $comments['comment_status'] = $get_comments->comment_content;
9336 $comment_item_id = get_comment_meta( $comment_result['comment_ID'], 'item_id' );
9337 if ( ! empty( $comment_item_id ) && is_array( $comment_item_id ) ) {
9338 $comments['comment_item_id'] = $comment_item_id[0];
9339 $comments['comment_item_page_title'] = get_the_title( (int) $comment_item_id[0] );
9340 $comments['comment_item_page_url'] = get_post_meta( (int) $comment_item_id[0], 'page_url', true );
9341 }
9342 $context['pluggable_data'] = $comments;
9343 $context['response_type'] = 'live';
9344 } else {
9345 $context = json_decode( '{"response_type":"sample","pluggable_data":{"comment_ID":"1","comment_post_ID":"1","comment_author":"test","comment_author_email":"test@test.com","comment_date":"2023-03-27 13:44:26","comment_content":"<p>Leave comment<\/p>","comment_type":"ph_comment","comment_status":"Resolved","comment_item_id": 9579, "comment_item_page_title": "About Us","comment_item_page_url": "https://example.com/abotu-us" }}', true );
9346 }
9347
9348 return $context;
9349 }
9350
9351 /**
9352 * Get MasterStudy LMS Courses.
9353 *
9354 * @param array $data data.
9355 *
9356 * @return array
9357 */
9358 public function search_ms_lms_courses( $data ) {
9359
9360 $page = $data['page'];
9361 $limit = Utilities::get_search_page_limit();
9362 $offset = $limit * ( $page - 1 );
9363
9364 $args = [
9365 'post_type' => 'stm-courses',
9366 'posts_per_page' => $limit,
9367 'offset' => $offset,
9368 'orderby' => 'title',
9369 'order' => 'ASC',
9370 'post_status' => 'publish',
9371 ];
9372
9373 $courses = get_posts( $args );
9374
9375 $course_count = wp_count_posts( 'stm-courses' )->publish;
9376
9377 $options = [];
9378 if ( ! empty( $courses ) ) {
9379 if ( is_array( $courses ) ) {
9380 foreach ( $courses as $course ) {
9381 $options[] = [
9382 'label' => $course->post_title,
9383 'value' => $course->ID,
9384 ];
9385 }
9386 }
9387 }
9388 return [
9389 'options' => $options,
9390 'hasMore' => $course_count > $limit && $course_count > $offset,
9391 ];
9392
9393 }
9394
9395 /**
9396 * Get MasterStudy LMS Lessons.
9397 *
9398 * @param array $data data.
9399 *
9400 * @return array
9401 */
9402 public function search_ms_lms_lessons( $data ) {
9403
9404 global $wpdb;
9405 $options = [];
9406 $course_id = $data['dynamic'];
9407
9408 // Use curriculum repository class.
9409 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
9410 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
9411 } else {
9412 $curriculum_repo = false;
9413 }
9414
9415 if ( '-1' === $course_id ) {
9416 $lessons = $wpdb->get_results(
9417 $wpdb->prepare(
9418 "SELECT SQL_CALC_FOUND_ROWS ID, post_title FROM $wpdb->posts WHERE post_type = %s ORDER BY post_title ASC",
9419 'stm-lessons'
9420 )
9421 );
9422
9423 if ( ! empty( $lessons ) ) {
9424 if ( is_array( $lessons ) ) {
9425 foreach ( $lessons as $lesson ) {
9426 $options[] = [
9427 'label' => $lesson->post_title,
9428 'value' => $lesson->ID,
9429 ];
9430 }
9431 }
9432 }
9433 } else {
9434 $lessons = [];
9435 if ( $curriculum_repo ) {
9436 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
9437 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
9438 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
9439 foreach ( $curriculum['materials'] as $material ) {
9440 if ( 'stm-lessons' === $material['post_type'] ) {
9441 $lessons[] = [
9442 'value' => $material['post_id'],
9443 'text' => $material['title'],
9444 ];
9445 }
9446 }
9447 }
9448 }
9449 if ( ! empty( $lessons ) ) {
9450 if ( is_array( $lessons ) ) {
9451 foreach ( $lessons as $lesson ) {
9452 $options[] = [
9453 'label' => $lesson['text'],
9454 'value' => $lesson['value'],
9455 ];
9456 }
9457 }
9458 }
9459 } else {
9460 $lessons = $wpdb->get_results(
9461 $wpdb->prepare(
9462 "SELECT SQL_CALC_FOUND_ROWS ID, post_title
9463 FROM $wpdb->posts
9464 WHERE FIND_IN_SET(
9465 ID,
9466 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
9467 )
9468 AND post_type = 'stm-lessons'
9469 ORDER BY post_title ASC",
9470 absint( $course_id )
9471 )
9472 );
9473 if ( ! empty( $lessons ) ) {
9474 if ( is_array( $lessons ) ) {
9475 foreach ( $lessons as $lesson ) {
9476 $options[] = [
9477 'label' => $lesson->post_title,
9478 'value' => $lesson->ID,
9479 ];
9480 }
9481 }
9482 }
9483 }
9484 }
9485 return [
9486 'options' => $options,
9487 'hasMore' => false,
9488 ];
9489
9490 }
9491
9492 /**
9493 * Get MasterStudy LMS Quiz.
9494 *
9495 * @param array $data data.
9496 *
9497 * @return array
9498 */
9499 public function search_ms_lms_quiz( $data ) {
9500
9501 global $wpdb;
9502
9503 $options = [];
9504 $course_id = $data['dynamic'];
9505
9506 // Use curriculum repository class.
9507 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
9508 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
9509 } else {
9510 $curriculum_repo = false;
9511 }
9512
9513 if ( '-1' === $course_id ) {
9514 $quizzes = $wpdb->get_results(
9515 $wpdb->prepare(
9516 "SELECT ID, post_title
9517 FROM $wpdb->posts
9518 WHERE post_type = %s
9519 ORDER BY post_title ASC",
9520 'stm-quizzes'
9521 )
9522 );
9523 if ( ! empty( $quizzes ) ) {
9524 if ( is_array( $quizzes ) ) {
9525 foreach ( $quizzes as $quiz ) {
9526 $options[] = [
9527 'label' => $quiz->post_title,
9528 'value' => $quiz->ID,
9529 ];
9530 }
9531 }
9532 }
9533 } else {
9534 $quizzes = [];
9535 if ( $curriculum_repo ) {
9536 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
9537 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
9538 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
9539 foreach ( $curriculum['materials'] as $material ) {
9540 if ( 'stm-quizzes' === $material['post_type'] ) {
9541 $quizzes[] = [
9542 'value' => $material['post_id'],
9543 'text' => $material['title'],
9544 ];
9545 }
9546 }
9547 }
9548 }
9549 if ( ! empty( $quizzes ) ) {
9550 if ( is_array( $quizzes ) ) {
9551 foreach ( $quizzes as $quiz ) {
9552 $options[] = [
9553 'label' => $quiz['text'],
9554 'value' => $quiz['value'],
9555 ];
9556 }
9557 }
9558 }
9559 } else {
9560 $quizzes = $wpdb->get_results(
9561 $wpdb->prepare(
9562 "SELECT ID, post_title
9563 FROM $wpdb->posts
9564 WHERE FIND_IN_SET(
9565 ID,
9566 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
9567 )
9568 AND post_type = 'stm-quizzes'
9569 ORDER BY post_title ASC
9570 ",
9571 absint( $course_id )
9572 )
9573 );
9574 if ( ! empty( $quizzes ) ) {
9575 if ( is_array( $quizzes ) ) {
9576 foreach ( $quizzes as $quiz ) {
9577 $options[] = [
9578 'label' => $quiz->post_title,
9579 'value' => $quiz->ID,
9580 ];
9581 }
9582 }
9583 }
9584 }
9585 }
9586 return [
9587 'options' => $options,
9588 'hasMore' => false,
9589 ];
9590
9591 }
9592
9593 /**
9594 * Search MasterStudy LMS data.
9595 *
9596 * @param array $data data.
9597 * @return array|void
9598 */
9599 public function search_ms_lms_last_data( $data ) {
9600 global $wpdb;
9601 $post_type = $data['post_type'];
9602 $trigger = $data['search_term'];
9603 $context = [];
9604
9605 if ( 'stm_lms_course_completed' === $trigger ) {
9606 $post_id = $data['filter']['course']['value'];
9607 if ( -1 === $post_id ) {
9608 $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 ) );
9609 } else {
9610 $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 ) );
9611 }
9612 } elseif ( 'stm_lesson_passed' === $trigger ) {
9613 $post_id = $data['filter']['lesson']['value'];
9614 if ( -1 === $post_id ) {
9615 $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 ) );
9616 } else {
9617 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}stm_lms_user_lessons as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.lesson_id WHERE postmeta.lesson_id=%s AND posts.post_type=%s order by postmeta.user_lesson_id DESC LIMIT 1", $post_id, $post_type ) );
9618 }
9619 } elseif ( 'stm_quiz_passed' === $trigger ) {
9620 $post_id = $data['filter']['quiz']['value'];
9621 if ( -1 === $post_id ) {
9622 $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 ) );
9623 } else {
9624 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}stm_lms_user_quizzes as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.quiz_id WHERE postmeta.quiz_id=%s AND postmeta.status='passed' AND posts.post_type=%s order by postmeta.user_quiz_id DESC LIMIT 1", $post_id, $post_type ) );
9625 }
9626 } elseif ( 'stm_quiz_failed' === $trigger ) {
9627 $post_id = $data['filter']['quiz']['value'];
9628 if ( -1 === $post_id ) {
9629 $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 ) );
9630 } else {
9631 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}stm_lms_user_quizzes as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.quiz_id WHERE postmeta.quiz_id=%s AND postmeta.status='failed' AND posts.post_type=%s order by postmeta.user_quiz_id DESC LIMIT 1", $post_id, $post_type ) );
9632 }
9633 } elseif ( 'stm_lms_user_enroll_course' === $trigger ) {
9634 $post_id = $data['filter']['course']['value'];
9635 if ( -1 === $post_id ) {
9636 $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 ) );
9637 } else {
9638 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}stm_lms_user_courses as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.course_id WHERE postmeta.course_id=%s AND postmeta.status='enrolled' AND posts.post_type=%s order by postmeta.user_course_id DESC LIMIT 1", $post_id, $post_type ) );
9639 }
9640 }
9641
9642 if ( ! empty( $result ) ) {
9643
9644 switch ( $trigger ) {
9645 case 'stm_lms_course_completed':
9646 $result_course_id = $result[0]->course_id;
9647 $result_user_id = $result[0]->user_id;
9648 $course = get_the_title( $result_course_id );
9649 $course_link = get_the_permalink( $result_course_id );
9650 $featured_image = get_the_post_thumbnail_url( $result_course_id );
9651
9652 $data = [
9653 'course_id' => $result_course_id,
9654 'course_title' => $course,
9655 'course_link' => $course_link,
9656 'course_featured_image' => $featured_image,
9657 'course_progress' => $result[0]->progress_percent,
9658 ];
9659 $context_data = array_merge(
9660 WordPress::get_user_context( $result_user_id ),
9661 $data
9662 );
9663 break;
9664 case 'stm_lesson_passed':
9665 $result_lesson_id = $result[0]->lesson_id;
9666 $result_user_id = $result[0]->user_id;
9667 $lesson = get_the_title( $result_lesson_id );
9668 $lesson_link = get_the_permalink( $result_lesson_id );
9669
9670 $data = [
9671 'lesson_id' => $result_lesson_id,
9672 'lesson_title' => $lesson,
9673 'lesson_link' => $lesson_link,
9674 ];
9675 $context_data = array_merge(
9676 WordPress::get_user_context( $result_user_id ),
9677 $data
9678 );
9679 break;
9680 case 'stm_quiz_passed':
9681 $result_quiz_id = $result[0]->quiz_id;
9682 $result_user_id = $result[0]->user_id;
9683 $quiz_title = get_the_title( $result_quiz_id );
9684 $quiz_link = get_the_permalink( $result_quiz_id );
9685
9686 $data = [
9687 'quiz_id' => $result_quiz_id,
9688 'quiz_title' => $quiz_title,
9689 'quiz_link' => $quiz_link,
9690 'quiz_score' => $result[0]->progress,
9691 'result' => 'passed',
9692 ];
9693 $context_data = array_merge(
9694 WordPress::get_user_context( $result_user_id ),
9695 $data
9696 );
9697 break;
9698 case 'stm_quiz_failed':
9699 $result_quiz_id = $result[0]->quiz_id;
9700 $result_user_id = $result[0]->user_id;
9701 $quiz_title = get_the_title( $result_quiz_id );
9702 $quiz_link = get_the_permalink( $result_quiz_id );
9703
9704 $data = [
9705 'quiz_id' => $result_quiz_id,
9706 'quiz_title' => $quiz_title,
9707 'quiz_link' => $quiz_link,
9708 'quiz_score' => $result[0]->progress,
9709 'result' => 'failed',
9710 ];
9711 $context_data = array_merge(
9712 WordPress::get_user_context( $result_user_id ),
9713 $data
9714 );
9715 break;
9716 case 'stm_lms_user_enroll_course':
9717 $result_course_id = $result[0]->course_id;
9718 $result_user_id = $result[0]->user_id;
9719
9720 $course = get_the_title( $result_course_id );
9721 $course_link = get_the_permalink( $result_course_id );
9722 $featured_image = get_the_post_thumbnail_url( $result_course_id );
9723
9724 $data = [
9725 'course_id' => $result_course_id,
9726 'course_title' => $course,
9727 'course_link' => $course_link,
9728 'course_featured_image' => $featured_image,
9729 ];
9730 $context_data = array_merge(
9731 WordPress::get_user_context( $result_user_id ),
9732 $data
9733 );
9734 break;
9735 default:
9736 return;
9737 }
9738 $context['pluggable_data'] = $context_data;
9739 $context['response_type'] = 'live';
9740 }
9741
9742 return $context;
9743
9744 }
9745
9746 /**
9747 * Prepare Fluent Support Mailbox list.
9748 *
9749 * @param array $data data.
9750 *
9751 * @return array
9752 */
9753 public function search_fs_mailbox_list( $data ) {
9754 $options = [];
9755 global $wpdb;
9756 $mailboxes = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_mail_boxes ORDER BY id DESC", ARRAY_A );
9757 if ( ! empty( $mailboxes ) ) {
9758 foreach ( $mailboxes as $key => $value ) {
9759
9760 $options[] = [
9761 'label' => $value['name'],
9762 'value' => $value['id'],
9763 ];
9764
9765 }
9766 }
9767
9768 return [
9769 'options' => $options,
9770 'hasMore' => false,
9771 ];
9772 }
9773
9774 /**
9775 * Prepare Fluent Support Product list.
9776 *
9777 * @param array $data data.
9778 *
9779 * @return array
9780 */
9781 public function search_fs_product_list( $data ) {
9782 $options = [];
9783
9784 if ( ! class_exists( '\FluentSupport\App\Models\Product' ) ) {
9785 return [];
9786 }
9787
9788 $products = \FluentSupport\App\Models\Product::select( [ 'id', 'title' ] )->get();
9789 if ( ! empty( $products ) ) {
9790 foreach ( $products as $key => $product ) {
9791 $options[] = [
9792 'label' => $product['title'],
9793 'value' => $product['id'],
9794 ];
9795 }
9796 }
9797
9798 return [
9799 'options' => $options,
9800 'hasMore' => false,
9801 ];
9802 }
9803
9804 /**
9805 * Prepare Fluent Support Product list.
9806 *
9807 * @param array $data data.
9808 *
9809 * @return array
9810 */
9811 public function search_fs_person_type_list( $data ) {
9812 $options = [
9813 [
9814 'label' => 'Customer',
9815 'value' => 'customer',
9816 ],
9817 [
9818 'label' => 'Agent',
9819 'value' => 'agent',
9820 ],
9821 ];
9822
9823 return [
9824 'options' => $options,
9825 'hasMore' => false,
9826 ];
9827 }
9828
9829 /**
9830 * Get last data for trigger.
9831 *
9832 * @param array $data data.
9833 * @return array
9834 */
9835 public function search_fluent_support_triggers_last_data( $data ) {
9836 global $wpdb;
9837 $context = [];
9838 $context['response_type'] = 'sample';
9839
9840 if ( ! class_exists( '\FluentSupport\App\Models\Ticket' ) ) {
9841 return [];
9842 }
9843
9844 $ticket_data = [
9845 'id' => '1',
9846 'customer_id' => '2',
9847 'agent_id' => '3',
9848 'product_id' => '5',
9849 'product_source' => 'local',
9850 'privacy' => 'private',
9851 'priority' => 'normal',
9852 'client_priority' => 'medium',
9853 'status' => 'active',
9854 'title' => 'Sample Ticket Title',
9855 'slug' => 'sample-ticket-title',
9856 'hash' => 'f8a8cfb946',
9857 'content_hash' => 'd65500d62621be8b493c22b1d888052c',
9858 'content' => '<p>Sample content.</p>',
9859 'last_customer_response' => '2023-04-27 07:30:46',
9860 'waiting_since' => '2023-04-27 07:30:46',
9861 'response_count' => '2',
9862 'total_close_time' => '7042',
9863 'resolved at' => '2023-04-27 09:28:08',
9864 'closed_by' => '1',
9865 'created_at' => '2023-04-27 07:30:46',
9866 'updated_at' => '2023-04-27 10:28:08',
9867 'mailbox_id' => '1',
9868 'mailbox_name' => 'SureTriggers',
9869 'mailbox_slug' => 'suretriggers',
9870 'mailbox_box_type' => 'web',
9871 'mailbox email' => 'john_doe@sample.com',
9872 'mailbox_settings_admin_email_address' => 'john_doe@sample.com',
9873 'mailbox_created_by' => '1',
9874 'mailbox_is_default' => 'yes',
9875 'mailbox_created_at' => '2023-04-26 06:29:01',
9876 'mailbox_updated_at' => '2023-04-26 06:29:01',
9877 ];
9878
9879 $customer_data = [
9880 'id' => '1',
9881 'first_name' => 'John',
9882 'last_name' => 'Doe',
9883 'email' => 'john_doe@sample.com',
9884 'person_type' => 'agent',
9885 'status' => 'active',
9886 'hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
9887 'user_id' => '1',
9888 'created_at' => '2023-04-27 07:30:46',
9889 'updated_at' => '2023-04-27 10:28:08',
9890 'full_name' => 'John Doe',
9891 'photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
9892 ];
9893
9894 $reply_data = [
9895 'ticket_id' => '1',
9896 'conversation_type' => 'response',
9897 'content' => '<p>Sample content.</p>',
9898 'source' => 'web',
9899 'content_hash' => '2cc0e35d8fb92a0675d67999b073b3a4',
9900 'created_at' => '2023-04-27 07:30:46',
9901 'updated_at' => '2023-04-27 10:28:08',
9902 'id' => '1',
9903 'person_id' => '2',
9904 'person_first_name' => 'John',
9905 'person_last_name' => 'Doe',
9906 'person_email' => 'john_doe@sample.com',
9907 'person_person_type' => 'agent',
9908 'person_status' => 'active',
9909 'person_hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
9910 'person_user_id' => '1',
9911 'person_created_at' => '2023-04-27 07:30:46',
9912 'person_updated_at' => '2023-04-27 10:28:08',
9913 'person_full_name' => 'John Doe',
9914 'person_photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
9915 ];
9916
9917 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
9918 $mailbox = isset( $data['mailbox_id'] ) ? $data['mailbox_id'] : '';
9919 $product_id = isset( $data['filter']['ticket_product_id']['value'] ) ? $data['filter']['ticket_product_id']['value'] : '';
9920 $person_id = isset( $data['filter']['person_id']['value'] ) ? (string) $data['filter']['person_id']['value'] : '';
9921
9922 if ( in_array( $term, [ 'response_added_by_agent', 'response_added_by_customer', 'ticket_replied_product_agent', 'ticket_replied_product_customer' ], true ) ) {
9923 if ( 'response_added_by_agent' == $term ) {
9924 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
9925 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
9926 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE id = %d ORDER BY id DESC LIMIT 1", $replies[0]->ticket_id ) );
9927 if ( ! empty( $tickets ) ) {
9928 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
9929 $ticket_data = $tickets[0];
9930 $customer_data = $customers[0];
9931 $reply_data = $replies[0];
9932 $context['response_type'] = 'live';
9933 }
9934 } elseif ( 'response_added_by_customer' == $term ) {
9935 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
9936 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
9937 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE id = %d ORDER BY id DESC LIMIT 1", $replies[0]->ticket_id ) );
9938 if ( ! empty( $tickets ) ) {
9939 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
9940 $ticket_data = $tickets[0];
9941 $customer_data = $customers[0];
9942 $reply_data = $replies[0];
9943 $context['response_type'] = 'live';
9944 }
9945 } elseif ( 'ticket_replied_product_agent' == $term ) {
9946 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
9947 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
9948 if ( -1 == $product_id ) {
9949 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
9950 $products = array_map(
9951 function( $product ) {
9952 return $product['id'];
9953 },
9954 $products
9955 );
9956 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id IN (%s) ORDER BY last_customer_response DESC LIMIT 1", implode( ',', $products ) ) );
9957 } else {
9958 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id = %d ORDER BY last_agent_response DESC LIMIT 1", $product_id ) );
9959 }
9960 if ( ! empty( $tickets ) ) {
9961 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
9962 $ticket_data = $tickets[0];
9963 $customer_data = $customers[0];
9964 $reply_data = $replies[0];
9965 $context['response_type'] = 'live';
9966 }
9967 } elseif ( 'ticket_replied_product_customer' == $term ) {
9968 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
9969 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
9970 if ( -1 == $product_id ) {
9971 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
9972 $products = array_map(
9973 function( $product ) {
9974 return $product['id'];
9975 },
9976 $products
9977 );
9978 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id IN (%s) ORDER BY last_customer_response DESC LIMIT 1", implode( ',', $products ) ) );
9979 } else {
9980 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id = %d ORDER BY last_customer_response DESC LIMIT 1", $product_id ) );
9981 }
9982 if ( ! empty( $tickets ) ) {
9983 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
9984 $ticket_data = $tickets[0];
9985 $customer_data = $customers[0];
9986 $reply_data = $replies[0];
9987 $context['response_type'] = 'live';
9988 }
9989 }
9990 $context['pluggable_data'] = array_merge(
9991 [
9992 'reply' => $reply_data,
9993 'ticket' => $ticket_data,
9994 'customer' => $customer_data,
9995 ]
9996 );
9997 if ( 'ticket_replied_product_customer' == $term || 'ticket_replied_product_agent' == $term ) {
9998 $context['pluggable_data']['ticket_product_id'] = $product_id;
9999 }
10000 if ( is_object( $ticket_data ) ) {
10001 $ticket_data = get_object_vars( $ticket_data );
10002 }
10003 $ticket = \FluentSupport\App\Models\Ticket::find( $ticket_data['id'] );
10004 if ( is_object( $ticket ) && method_exists( $ticket, 'customData' ) ) {
10005 $context['pluggable_data']['custom_fields'] = $ticket->customData();
10006 }
10007 $context['pluggable_data']['ticket_link'] = admin_url( "admin.php?page=fluent-support#/tickets/{$ticket_data['id']}/view" );
10008 } else {
10009 if ( 'ticket_created' == $term ) {
10010 if ( -1 == $mailbox ) {
10011 $tickets = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE status = 'new' ORDER BY id DESC LIMIT 1" );
10012 } else {
10013 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE status = 'new' AND mailbox_id = %d ORDER BY id DESC LIMIT 1", $mailbox ) );
10014 }
10015 if ( ! empty( $tickets ) ) {
10016 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d", $tickets[0]->customer_id ) );
10017 $ticket_data = $tickets[0];
10018 $customer_data = $customers[0];
10019 }
10020 } elseif ( 'ticket_closed_by_customer' == $term ) {
10021 $customers_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
10022 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE status = 'closed' AND closed_by = %s ORDER BY id DESC LIMIT 1", $customers_type[0] ) );
10023 if ( ! empty( $tickets ) ) {
10024 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
10025 $ticket_data = $tickets[0];
10026 $customer_data = $customers[0];
10027 }
10028 } elseif ( 'ticket_closed_by_agent' == $term ) {
10029 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
10030 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE status = 'closed' AND closed_by = %s ORDER BY id DESC LIMIT 1", $agents_type[0] ) );
10031 if ( ! empty( $tickets ) ) {
10032 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
10033 $ticket_data = $tickets[0];
10034 $customer_data = $customers[0];
10035 }
10036 } elseif ( 'ticket_created_product' == $term ) {
10037 $agents_type = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_persons" );
10038 if ( -1 == $product_id ) {
10039 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
10040 $products = array_map(
10041 function( $product ) {
10042 return $product['id'];
10043 },
10044 $products
10045 );
10046 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id IN (%s) ORDER BY id DESC LIMIT 1", implode( ',', $products ) ) );
10047 } else {
10048 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id = %d ORDER BY id DESC LIMIT 1", $product_id ) );
10049 }
10050 if ( ! empty( $tickets ) ) {
10051 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
10052 $ticket_data = $tickets[0];
10053 $customer_data = $customers[0];
10054 }
10055 } elseif ( 'ticket_closed_product' == $term ) {
10056 $agents_type = $wpdb->get_results( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = %s", $person_id ), ARRAY_A );
10057 $agents = array_map(
10058 function( $agent ) {
10059 return $agent['id'];
10060 },
10061 $agents_type
10062 );
10063 if ( -1 == $product_id ) {
10064 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
10065 $products = array_map(
10066 function( $product ) {
10067 return $product['id'];
10068 },
10069 $products
10070 );
10071 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id IN (%s) AND status = 'closed' AND closed_by IN (%d) ORDER BY id DESC LIMIT 1", implode( ',', $products ), implode( ',', $agents ) ) );
10072 } else {
10073 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id = %d AND status = 'closed' AND closed_by IN (%d) ORDER BY id DESC LIMIT 1", $product_id, implode( ',', $agents ) ) );
10074 }
10075 if ( ! empty( $tickets ) ) {
10076 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d ORDER BY id DESC LIMIT 1", $tickets[0]->customer_id ) );
10077 $ticket_data = $tickets[0];
10078 $customer_data = $customers[0];
10079 }
10080 }
10081 $context['pluggable_data'] = array_merge(
10082 [
10083 'ticket' => $ticket_data,
10084 'customer' => $customer_data,
10085 ]
10086 );
10087 if ( 'ticket_created' == $term ) {
10088 $context['pluggable_data']['mailbox_id'] = $mailbox;
10089 } elseif ( 'ticket_created_product' == $term || 'ticket_replied_product_customer' == $term || 'ticket_replied_product_agent' == $term ) {
10090 $context['pluggable_data']['ticket_product_id'] = $product_id;
10091 } elseif ( 'ticket_closed_product' == $term ) {
10092 $context['pluggable_data']['ticket_product_id'] = $product_id;
10093 $context['pluggable_data']['person_id'] = $person_id;
10094 }
10095 if ( is_object( $ticket_data ) ) {
10096 $ticket_data = get_object_vars( $ticket_data );
10097 }
10098 $ticket = \FluentSupport\App\Models\Ticket::find( $ticket_data['id'] );
10099 if ( is_object( $ticket ) && method_exists( $ticket, 'customData' ) ) {
10100 $context['pluggable_data']['custom_fields'] = $ticket->customData();
10101 }
10102 $context['pluggable_data']['ticket_link'] = admin_url( "admin.php?page=fluent-support#/tickets/{$ticket_data['id']}/view" );
10103 $context['response_type'] = 'live';
10104 }
10105
10106 return $context;
10107 }
10108
10109 /**
10110 * Prepare Ultimate Member user_roles.
10111 *
10112 * @param array $data data.
10113 *
10114 * @return array
10115 */
10116 public function search_um_user_roles( $data ) {
10117 if ( function_exists( 'get_editable_roles' ) ) {
10118 $roles = get_editable_roles();
10119 } else {
10120 $roles = wp_roles()->roles;
10121 $roles = apply_filters( 'editable_roles', $roles );
10122 }
10123
10124 $options = [];
10125 foreach ( $roles as $role => $details ) {
10126
10127 $options[] = [
10128 'label' => $details['name'],
10129 'value' => $role,
10130 ];
10131
10132 }
10133
10134 return [
10135 'options' => $options,
10136 'hasMore' => false,
10137 ];
10138 }
10139
10140 /**
10141 * Prepare Ultimate Member forms_list.
10142 *
10143 * @param array $data data.
10144 *
10145 * @return array
10146 */
10147 public function search_um_forms_list( $data ) {
10148
10149 $page = $data['page'];
10150 $limit = Utilities::get_search_page_limit();
10151 $offset = $limit * ( $page - 1 );
10152
10153 $args = [
10154 'posts_per_page' => $limit,
10155 'offset' => $offset,
10156 'orderby' => 'title',
10157 'order' => 'ASC',
10158 'post_type' => 'um_form',
10159 'post_status' => 'publish',
10160 'fields' => 'ids',
10161 ];
10162 if ( 'register' == $data['filter']['type'] ) {
10163 $args['meta_query'] = [
10164 [
10165 'key' => '_um_mode',
10166 'value' => 'register',
10167 'compare' => 'LIKE',
10168 ],
10169 ];
10170 } elseif ( 'login' == $data['filter']['type'] ) {
10171 $args['meta_query'] = [
10172 [
10173 'key' => '_um_mode',
10174 'value' => 'login',
10175 'compare' => 'LIKE',
10176 ],
10177 ];
10178 }
10179
10180 $forms_list = get_posts( $args );
10181
10182 $forms_list_count = wp_count_posts( 'um_form' )->publish;
10183
10184 $options = [];
10185 if ( ! empty( $forms_list ) ) {
10186 foreach ( $forms_list as $form ) {
10187 $title = html_entity_decode( get_the_title( $form ), ENT_QUOTES, 'UTF-8' );
10188 $options[] = [
10189 'label' => $title,
10190 'value' => $form,
10191 ];
10192 }
10193 }
10194
10195 return [
10196 'options' => $options,
10197 'hasMore' => $forms_list_count > $limit && $forms_list_count > $offset,
10198 ];
10199 }
10200
10201 /**
10202 * Get last data for Ultimate Member Login trigger.
10203 *
10204 * @param array $data data.
10205 * @return mixed
10206 */
10207 public function search_ultimate_member_user_logsin( $data ) {
10208 $context = [];
10209 $args = [
10210 'orderby' => 'meta_value',
10211 'meta_key' => '_um_last_login',
10212 'order' => 'DESC',
10213 'number' => 1,
10214 ];
10215 $users = get_users( $args );
10216
10217 if ( ! empty( $users ) ) {
10218 $user = $users[0];
10219 $pluggable_data = WordPress::get_user_context( $user->ID );
10220 $context['pluggable_data'] = $pluggable_data;
10221 $context['response_type'] = 'live';
10222 } else {
10223 $role = 'subscriber';
10224 $context['pluggable_data'] = [
10225 'wp_user_id' => 1,
10226 'user_login' => 'test',
10227 'display_name' => 'Test User',
10228 'user_firstname' => 'Test',
10229 'user_lastname' => 'User',
10230 'user_email' => 'testuser@gmail.com',
10231 'user_role' => [ $role ],
10232 ];
10233 $context['response_type'] = 'sample';
10234 }
10235 return $context;
10236 }
10237
10238 /**
10239 * Get last data for Ultimate Member Register trigger.
10240 *
10241 * @param array $data data.
10242 * @return mixed
10243 */
10244 public function search_ultimate_member_user_registers( $data ) {
10245 $context = [];
10246 $args = [
10247 'orderby' => 'meta_value',
10248 'meta_key' => 'um_user_profile_url_slug_user_login',
10249 'order' => 'DESC',
10250 'number' => 1,
10251 ];
10252 $users = get_users( $args );
10253
10254 if ( ! empty( $users ) ) {
10255 $user = $users[0];
10256 $pluggable_data = WordPress::get_user_context( $user->ID );
10257 $context['pluggable_data'] = $pluggable_data;
10258 $context['response_type'] = 'live';
10259 } else {
10260 $role = 'subscriber';
10261 $context['pluggable_data'] = [
10262 'wp_user_id' => 1,
10263 'user_login' => 'test',
10264 'display_name' => 'Test User',
10265 'user_firstname' => 'Test',
10266 'user_lastname' => 'User',
10267 'user_email' => 'testuser@gmail.com',
10268 'user_role' => [ $role ],
10269 ];
10270 $context['response_type'] = 'sample';
10271 }
10272 return $context;
10273 }
10274
10275 /**
10276 * Get last data for Ultimate Member Register trigger.
10277 *
10278 * @param array $data data.
10279 * @return mixed
10280 */
10281 public function search_ultimate_member_user_inactive( $data ) {
10282 $context = [];
10283 $args = [
10284 'orderby' => 'user_id',
10285 'meta_key' => 'account_status',
10286 'meta_value' => 'inactive',
10287 'order' => 'ASC',
10288 'number' => 1,
10289 ];
10290 $users = get_users( $args );
10291
10292 if ( ! empty( $users ) ) {
10293 $user = $users[0];
10294 $pluggable_data = [];
10295 $pluggable_data[] = WordPress::get_user_context( $user->ID );
10296 $pluggable_data['user_account_status'] = 'inactive';
10297 $context['pluggable_data'] = $pluggable_data;
10298 $context['response_type'] = 'live';
10299 } else {
10300 $role = 'subscriber';
10301 $context['pluggable_data'] = [
10302 'wp_user_id' => 1,
10303 'user_login' => 'test',
10304 'display_name' => 'Test User',
10305 'user_firstname' => 'Test',
10306 'user_lastname' => 'User',
10307 'user_email' => 'testuser@gmail.com',
10308 'user_role' => [ $role ],
10309 'user_account_status' => 'inactive',
10310 ];
10311 $context['response_type'] = 'sample';
10312 }
10313 return $context;
10314 }
10315
10316 /**
10317 * Get last data for Ultimate Member Change Role trigger.
10318 *
10319 * @param array $data data.
10320 * @return mixed
10321 */
10322 public function search_ultimate_member_user_role_change( $data ) {
10323 $context = [];
10324
10325 $role = $data['filter']['role']['value'];
10326
10327 $args = [
10328 'number' => 1,
10329 'role' => $role,
10330 ];
10331 $users = get_users( $args );
10332 shuffle( $users );
10333 if ( ! empty( $users ) ) {
10334 $user = $users[0];
10335 $pluggable_data = WordPress::get_user_context( $user->ID );
10336 $context['pluggable_data'] = $pluggable_data;
10337 $context['response_type'] = 'live';
10338 } else {
10339 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
10340 $context['pluggable_data'] = [
10341 'wp_user_id' => 1,
10342 'user_login' => 'test',
10343 'display_name' => 'Test User',
10344 'user_firstname' => 'Test',
10345 'user_lastname' => 'User',
10346 'user_email' => 'testuser@gmail.com',
10347 'user_role' => [ $role ],
10348 'user_account_status' => 'inactive',
10349 ];
10350 $context['response_type'] = 'sample';
10351 }
10352 return $context;
10353 }
10354
10355 /**
10356 * Get JetEngine WP Posttypes.
10357 *
10358 * @param array $data data.
10359 *
10360 * @return array
10361 */
10362 public function search_je_posttype_list( $data ) {
10363
10364 $post_types = get_post_types( [ 'public' => true ], 'object' );
10365 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
10366 if ( isset( $post_types['attachment'] ) ) {
10367 unset( $post_types['attachment'] );
10368 }
10369
10370 $options = [];
10371 foreach ( $post_types as $post_type => $details ) {
10372 $options[] = [
10373 'label' => $details->label,
10374 'value' => $post_type,
10375 ];
10376 }
10377
10378 return [
10379 'options' => $options,
10380 'hasMore' => false,
10381 ];
10382 }
10383
10384 /**
10385 * Get JetEngine WP fields.
10386 *
10387 * @param array $data data.
10388 *
10389 * @return array
10390 */
10391 public function search_je_field_list( $data ) {
10392
10393 $post_type = $data['dynamic'];
10394
10395 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
10396
10397 $post_fields = array_filter(
10398 $metaboxes,
10399 function( $metabox ) {
10400 /**
10401 *
10402 * Ignore line
10403 *
10404 * @phpstan-ignore-next-line
10405 */
10406 return 'post' === $metabox['args']['object_type'];
10407 }
10408 );
10409
10410 $post_fields_count = count( $post_fields );
10411
10412 $options = [];
10413 if ( ! empty( $post_fields ) ) {
10414 if ( is_array( $post_fields ) ) {
10415 foreach ( $post_fields as $post_field ) {
10416 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
10417 foreach ( $post_field['meta_fields'] as $meta_field ) {
10418 $options[] = [
10419 'label' => $meta_field['title'],
10420 'value' => $meta_field['name'],
10421 ];
10422 }
10423 }
10424 }
10425 }
10426 }
10427 return [
10428 'options' => $options,
10429 'hasMore' => false,
10430 ];
10431 }
10432
10433 /**
10434 * Search Last Updated Field Data.
10435 *
10436 * @param array $data data.
10437 * @return array
10438 */
10439 public function search_jet_engine_field_data( $data ) {
10440 global $wpdb;
10441
10442 $context = [];
10443
10444 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
10445
10446 $post_type = $data['filter']['wp_post_type']['value'];
10447
10448 if ( -1 === $field ) {
10449 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
10450
10451 $post_fields = array_filter(
10452 $metaboxes,
10453 function( $metabox ) {
10454 /**
10455 *
10456 * Ignore line
10457 *
10458 * @phpstan-ignore-next-line
10459 */
10460 return 'post' === $metabox['args']['object_type'];
10461 }
10462 );
10463
10464 $options = [];
10465 if ( ! empty( $post_fields ) ) {
10466 if ( is_array( $post_fields ) ) {
10467 foreach ( $post_fields as $post_field ) {
10468 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
10469 foreach ( $post_field['meta_fields'] as $meta_field ) {
10470 $options[] = $meta_field['name'];
10471 }
10472 }
10473 }
10474 }
10475 }
10476 $random_key = array_rand( $options );
10477 $random_value = $options[ $random_key ];
10478 $string = '%' . $random_value . '%';
10479 } else {
10480 $string = '%' . $data['filter']['field_id']['value'] . '%';
10481 }
10482
10483 $result = $wpdb->get_results(
10484 $wpdb->prepare(
10485 'SELECT post_id FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key LIKE %s',
10486 [ $string ]
10487 ),
10488 ARRAY_A
10489 );
10490
10491 $ids = [];
10492
10493 if ( ! empty( $result ) ) {
10494 foreach ( $result as $val ) {
10495 $ids[] = $val['post_id'];
10496 }
10497 }
10498
10499 $lastupdated_args = [
10500 'post_type' => $post_type,
10501 'orderby' => 'modified',
10502 'post__in' => $ids,
10503 'posts_per_page' => 1,
10504 ];
10505 $lastupdated_loop = get_posts( $lastupdated_args );
10506
10507 $response = [];
10508 if ( ! empty( $result ) ) {
10509 $context['post'] = $lastupdated_loop[0];
10510
10511 $meta_value = get_post_meta( $lastupdated_loop[0]->ID, sprintf( '%s', $data['filter']['field_id']['value'] ), true );
10512 $meta_key = sprintf( '%s', $data['filter']['field_id']['value'] );
10513
10514 $context[ $meta_key ] = $meta_value;
10515
10516 $response['pluggable_data'] = $context;
10517 $response['response_type'] = 'live';
10518 } else {
10519 $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 );
10520 }
10521
10522 return $response;
10523
10524 }
10525
10526 /**
10527 * Get Formidable Forms.
10528 *
10529 * @param array $data data.
10530 *
10531 * @return array|void
10532 */
10533 public function search_formidable_form_list( $data ) {
10534 if ( ! class_exists( 'FrmForm' ) ) {
10535 return;
10536 }
10537
10538 $page = $data['page'];
10539 $term = $data['search_term'];
10540 $limit = Utilities::get_search_page_limit();
10541 $offset = $limit * ( $page - 1 );
10542 $per_page = 10;
10543
10544 $query = [
10545 [
10546 'or' => 1,
10547 'name LIKE' => $term,
10548 'parent_form_id' => null,
10549 ],
10550 ];
10551 $query['is_template'] = 0;
10552 $query['status !'] = 'trash';
10553 $forms_list = FrmForm::getAll( $query, '', $offset . ',' . $per_page );
10554 $form_count = FrmForm::getAll( $query );
10555 $form_count = count( $form_count );
10556 $options = [];
10557
10558 if ( ! empty( $forms_list ) ) {
10559 if ( is_array( $forms_list ) ) {
10560 foreach ( $forms_list as $form ) {
10561 $options[] = [
10562 'label' => $form->name,
10563 'value' => $form->id,
10564 ];
10565 }
10566 }
10567 }
10568
10569 return [
10570 'options' => $options,
10571 'hasMore' => $form_count > $limit && $form_count > $offset,
10572 ];
10573 }
10574
10575 /**
10576 * Get JetFormBuilder Form List.
10577 *
10578 * @param array $data data.
10579 *
10580 * @return array|void
10581 */
10582 public function search_jetform_list( $data ) {
10583 if ( ! class_exists( '\Jet_Form_Builder\Classes\Tools' ) ) {
10584 return;
10585 }
10586
10587 $forms = \Jet_Form_Builder\Classes\Tools::get_forms_list_for_js();
10588
10589 $options = [];
10590 foreach ( $forms as $form ) {
10591
10592 if ( ! empty( $form['value'] ) ) {
10593 $options[] = [
10594 'label' => esc_html( $form['label'] ),
10595 'value' => esc_attr( $form['value'] ),
10596 ];
10597 }
10598 }
10599
10600 return [
10601 'options' => $options,
10602 'hasMore' => false,
10603 ];
10604 }
10605
10606 /**
10607 * Get Forminator Form List.
10608 *
10609 * @param array $data data.
10610 *
10611 * @return array|void
10612 */
10613 public function search_forminator_form_list( $data ) {
10614 if ( ! class_exists( 'Forminator_API' ) ) {
10615 return;
10616 }
10617
10618 $forms = Forminator_API::get_forms( null, 1, 10 );
10619
10620 $options = [];
10621 foreach ( $forms as $form ) {
10622 $options[] = [
10623 'label' => isset( $form->settings ) && isset( $form->settings['form_name'] ) ? $form->settings['form_name'] : $form->name,
10624 'value' => $form->id,
10625 ];
10626 }
10627
10628 return [
10629 'options' => $options,
10630 'hasMore' => false,
10631 ];
10632 }
10633
10634
10635 /**
10636 * Get BbPress topics list.
10637 *
10638 * @param array $data data.
10639 *
10640 * @return array|void
10641 */
10642 public function search_bbp_topic_list( $data ) {
10643 $page = $data['page'];
10644 $forum_id = $data['dynamic'];
10645 $limit = Utilities::get_search_page_limit();
10646 $offset = $limit * ( $page - 1 );
10647 $args = [
10648 'post_type' => 'topic',
10649 'offset' => $offset,
10650 'meta_query' => [
10651 [
10652 'key' => '_bbp_forum_id',
10653 'value' => $forum_id,
10654 'compare' => '==',
10655 ],
10656 ],
10657 ];
10658
10659 $topics = get_posts( $args );
10660 $topics_count = wp_count_posts( 'topic' )->publish;
10661
10662 $options = [];
10663 if ( ! empty( $topics ) ) {
10664 if ( is_array( $topics ) ) {
10665 foreach ( $topics as $topic ) {
10666 $options[] = [
10667 'label' => $topic->post_title,
10668 'value' => $topic->ID,
10669 ];
10670 }
10671 }
10672 }
10673 return [
10674 'options' => $options,
10675 'hasMore' => $topics_count > $limit && $topics_count > $offset,
10676 ];
10677 }
10678
10679
10680 /**
10681 * Search Last Updated Field Data.
10682 *
10683 * @param array $data data.
10684 * @return array
10685 */
10686 public function search_bbp_last_data( $data ) {
10687 global $wpdb;
10688
10689 $post_type = $data['post_type'];
10690 $trigger = $data['search_term'];
10691 $context = [];
10692
10693 if ( 'topic' === $post_type ) {
10694 $post_id = $data['filter']['forum']['value'];
10695 if ( -1 === $post_id ) {
10696 $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" );
10697
10698 } else {
10699 $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 ) );
10700
10701 }
10702 } elseif ( 'reply' === $post_type ) {
10703 $post_id = $data['filter']['topic']['value'];
10704
10705 if ( -1 === $post_id ) {
10706 $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" );
10707
10708 } else {
10709 $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 ) );
10710 }
10711 }
10712
10713
10714 $response = [];
10715 if ( ! empty( $result ) ) {
10716 if ( 'bbpress_topic_created' === $trigger ) {
10717 $topic_id = $result[0]->post_id;
10718 $forum_id = $result[0]->meta_value;
10719 $topic = get_the_title( $topic_id );
10720 $topic_link = get_the_permalink( $topic_id );
10721 $topic_description = get_the_content( $topic_id );
10722 $topic_status = get_post_status( $topic_id );
10723
10724 $forum = get_the_title( $forum_id );
10725 $forum_link = get_the_permalink( $forum_id );
10726 $forum_description = get_the_content( $forum_id );
10727 $forum_status = get_post_status( $forum_id );
10728
10729 $forum = [
10730 'forum' => $forum_id,
10731 'forum_title' => $forum,
10732 'forum_link' => $forum_link,
10733 'forum_description' => $forum_description,
10734 'forum_status' => $forum_status,
10735 ];
10736
10737 $topic = [
10738 'topic_title' => $topic,
10739 'topic_link' => $topic_link,
10740 'topic_description' => $topic_description,
10741 'topic_status' => $topic_status,
10742 ];
10743
10744 $user_id = $result[0]->post_author;
10745 $context = array_merge(
10746 WordPress::get_user_context( $user_id ),
10747 $forum,
10748 $topic
10749 );
10750
10751 $response['pluggable_data'] = $context;
10752 $response['response_type'] = 'live';
10753 } else {
10754 $reply_id = $result[0]->post_id;
10755 $topic_id = $result[0]->meta_value;
10756 $forum_id = get_post_meta( $topic_id, '_bbp_forum_id', true );
10757 $forum_id = intval( '"' . $forum_id . '"' );
10758 $reply = get_the_title( $reply_id );
10759 $reply_link = get_the_permalink( $reply_id );
10760 $reply_description = get_the_content( $reply_id );
10761 $reply_status = get_post_status( $reply_id );
10762
10763
10764 $topic = get_the_title( $topic_id );
10765 $topic_link = get_the_permalink( $topic_id );
10766 $topic_description = get_the_content( $topic_id );
10767 $topic_status = get_post_status( $topic_id );
10768
10769 $forum = get_the_title( $forum_id );
10770 $forum_link = get_the_permalink( $forum_id );
10771 $forum_description = get_the_content( null, false, $forum_id );
10772 $forum_status = get_post_status( $forum_id );
10773
10774 $forum = [
10775 'forum' => $forum_id,
10776 'forum_title' => $forum,
10777 'forum_link' => $forum_link,
10778 'forum_description' => $forum_description,
10779 'forum_status' => $forum_status,
10780 ];
10781
10782 $topic = [
10783 'topic_title' => $topic,
10784 'topic_link' => $topic_link,
10785 'topic_description' => $topic_description,
10786 'topic_status' => $topic_status,
10787 ];
10788
10789 $reply = [
10790 'reply_title' => $reply,
10791 'reply_link' => $reply_link,
10792 'reply_description' => $reply_description,
10793 'reply_status' => $reply_status,
10794 ];
10795 $user_id = $result[0]->post_author;
10796 $context = array_merge(
10797 WordPress::get_user_context( $user_id ),
10798 $forum,
10799 $topic,
10800 $reply
10801 );
10802
10803 $response['pluggable_data'] = $context;
10804 $response['response_type'] = 'live';
10805 }
10806 }
10807
10808 return $response;
10809 }
10810
10811 /**
10812 * Search Last Updated Field Data.
10813 *
10814 * @param array $data data.
10815 * @return array|void
10816 */
10817 public function search_happyform_list( $data ) {
10818 if ( ! function_exists( 'happyforms_get_form_controller' ) ) {
10819 return;
10820 }
10821
10822 $form_controller = happyforms_get_form_controller();
10823
10824 $forms = $form_controller->do_get();
10825 $options = [];
10826 if ( ! empty( $forms ) ) {
10827 foreach ( $forms as $form ) {
10828 $options[] = [
10829 'label' => $form['post_title'],
10830 'value' => $form['ID'],
10831 ];
10832 }
10833 }
10834
10835 return [
10836 'options' => $options,
10837 'hasMore' => false,
10838 ];
10839 }
10840
10841 /**
10842 * Get Memberpress Course List.
10843 *
10844 * @param array $data data.
10845 *
10846 * @return array|void
10847 */
10848 public function search_mpc_lessons_list( $data ) {
10849 if ( ! class_exists( '\memberpress\courses\models\Lesson' ) ) {
10850 return;
10851 }
10852 global $wpdb;
10853 $options = [];
10854 $course_id = $data['dynamic'];
10855 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mpcs_sections WHERE course_id =%s", $course_id ) );
10856 $sections = [];
10857 foreach ( $result as $rec ) {
10858 $sections[] = [
10859 'id' => $rec->id,
10860 'title' => $rec->title,
10861 ];
10862 }
10863 if ( is_array( $sections ) && count( $sections ) > 0 ) {
10864 foreach ( $sections as $section ) {
10865 $post_types_string = \memberpress\courses\models\Lesson::lesson_cpts();
10866 $post_types_string = implode( "','", $post_types_string );
10867
10868 $query = $wpdb->prepare(
10869 "SELECT * FROM {$wpdb->posts} AS p
10870 JOIN {$wpdb->postmeta} AS pm
10871 ON p.ID = pm.post_id
10872 AND pm.meta_key = %s
10873 AND pm.meta_value = %s
10874 JOIN {$wpdb->postmeta} AS pm_order
10875 ON p.ID = pm_order.post_id
10876 AND pm_order.meta_key = %s
10877 WHERE p.post_type in ( %s ) AND p.post_status <> 'trash'
10878 ORDER BY pm_order.meta_value * 1",
10879 models\Lesson::$section_id_str,
10880 $section['id'],
10881 models\Lesson::$lesson_order_str,
10882 stripcslashes( $post_types_string )
10883 );
10884
10885 $db_lessons = $wpdb->get_results( stripcslashes( $query ) ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
10886 foreach ( $db_lessons as $lesson ) {
10887 $options[] = [
10888 'label' => $section['title'] . '->' . $lesson->post_title,
10889 'value' => $lesson->ID,
10890 ];
10891 }
10892 }
10893 }
10894 return [
10895 'options' => $options,
10896 'hasMore' => false,
10897 ];
10898 }
10899
10900 /**
10901 * Get Memberpress Course List.
10902 *
10903 * @param array $data data.
10904 *
10905 * @return array|void
10906 */
10907 public function search_gp_rank_type_list( $data ) {
10908 global $wpdb;
10909
10910 $posts = $wpdb->get_results(
10911 "SELECT ID, post_name, post_title, post_type
10912 FROM $wpdb->posts
10913 WHERE post_type LIKE 'rank-type' AND post_status = 'publish' ORDER BY post_title ASC"
10914 );
10915
10916 $posts_count = count( $posts );
10917
10918 $options = [];
10919 if ( $posts ) {
10920 foreach ( $posts as $post ) {
10921 $options[] = [
10922 'label' => $post->post_title,
10923 'value' => $post->post_name,
10924 ];
10925 }
10926 }
10927
10928 return [
10929 'options' => $options,
10930 'hasMore' => false,
10931 ];
10932 }
10933
10934 /**
10935 * Get MPC last data.
10936 *
10937 * @param array $data data.
10938 *
10939 * @return array
10940 */
10941 public function search_mpc_last_data( $data ) {
10942 global $wpdb;
10943 $trigger = $data['search_term'];
10944 $course_data = [];
10945 $lesson_data = [];
10946 $context = [];
10947
10948 if ( 'mpc_course_completed' === $trigger ) {
10949 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course]']['value'] : '-1' );
10950 if ( $course_id > 0 ) {
10951
10952 $course = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $course_id ) );
10953 } else {
10954 $course = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-course' ORDER BY id DESC LIMIT 1" );
10955 }
10956
10957 if ( ! empty( $course ) ) {
10958 $course_data = [
10959 'course_id' => $course->ID,
10960 'course_title' => get_the_title( $course_id ),
10961 'course_url' => get_permalink( $course_id ),
10962 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
10963 'course_featured_image_url' => get_the_post_thumbnail_url( $course_id ),
10964 ];
10965 }
10966 $user_progress = $wpdb->get_row( $wpdb->prepare( "SELECT user_id FROM {$wpdb->prefix}mpcs_user_progress WHERE course_id=%s", $course_id ) );
10967 if ( ! empty( $user_progress ) ) {
10968 $context['response_type'] = 'live';
10969 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
10970 } else {
10971 $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}
10972 ,"response_type":"sample"} ';
10973 $context = json_decode( $sample_data, true );
10974 }
10975 } elseif ( 'mpc_lesson_completed' === $trigger ) {
10976 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
10977 $course_id = (int) $data['filter']['course']['value'];
10978 if ( $lesson_id > 0 ) {
10979
10980 $lesson = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $lesson_id ) );
10981 } else {
10982 $lesson = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-lesson' ORDER BY id DESC LIMIT 1" );
10983 }
10984
10985 if ( ! empty( $lesson ) ) {
10986 $lesson_data = [
10987 'lesson_id' => $lesson->ID,
10988 'lesson_title' => get_the_title( $lesson_id ),
10989 'lesson_url' => get_permalink( $lesson_id ),
10990 'lesson_featured_image_id' => get_post_meta( $lesson_id, '_thumbnail_id', true ),
10991 'lesson_featured_image_url' => get_the_post_thumbnail_url( $lesson_id ),
10992 ];
10993
10994 $lesson_section_id = get_post_meta( $lesson->ID, '_mpcs_lesson_section_id', true );
10995
10996 $section = $wpdb->get_row( $wpdb->prepare( "SELECT course_id FROM {$wpdb->prefix}mpcs_sections WHERE ID= %s", $lesson_section_id ) );
10997
10998 $course_data = [
10999 'course_id' => $course_id,
11000 'course_title' => get_the_title( $course_id ),
11001 'course_url' => get_permalink( $course_id ),
11002 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
11003 'course_featured_image_url' => get_the_post_thumbnail_url( $section->course_id ),
11004 ];
11005 }
11006
11007 $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 ) );
11008 if ( ! empty( $user_progress ) ) {
11009 $context['response_type'] = 'live';
11010 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
11011 } else {
11012 $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"}';
11013 $context = json_decode( $sample_data, true );
11014 }
11015 }
11016
11017
11018 return $context;
11019 }
11020
11021 /** Get GamiPress Rank List.
11022 *
11023 * @param array $data data.
11024 *
11025 * @return array|void
11026 */
11027 public function search_gp_rank_list( $data ) {
11028 global $wpdb;
11029
11030 $page = $data['page'];
11031 $limit = Utilities::get_search_page_limit();
11032 $offset = $limit * ( $page - 1 );
11033
11034 $args = [
11035 'post_type' => $data['dynamic']['rank_type'],
11036 'posts_per_page' => $limit,
11037 'offset' => $offset,
11038 'orderby' => 'title',
11039 'order' => 'ASC',
11040 'post_status' => 'publish',
11041 's' => $data['search_term'],
11042 ];
11043
11044 $rank_type = get_posts( $args );
11045
11046 $count_args = [
11047 'post_type' => $data['dynamic']['rank_type'],
11048 'posts_per_page' => -1,
11049 'post_status' => 'publish',
11050 's' => $data['search_term'],
11051 ];
11052
11053 $rank_posts = get_posts( $count_args );
11054 $rank_type_count = count( $rank_posts );
11055
11056 $options = [];
11057 if ( $rank_type ) {
11058 foreach ( $rank_type as $rank ) {
11059 $options[] = [
11060 'label' => $rank->post_title,
11061 'value' => $rank->ID,
11062 ];
11063 }
11064 }
11065
11066 return [
11067 'options' => $options,
11068 'hasMore' => $rank_type_count > $limit && $rank_type_count > $offset,
11069 ];
11070 }
11071
11072 /**
11073 * Get GamiPress PointType List.
11074 *
11075 * @param array $data data.
11076 *
11077 * @return array|void
11078 */
11079 public function search_gp_point_type_list( $data ) {
11080 global $wpdb;
11081
11082 $page = $data['page'];
11083 $limit = Utilities::get_search_page_limit();
11084 $offset = $limit * ( $page - 1 );
11085
11086 $args = [
11087 'post_type' => 'points-type',
11088 'posts_per_page' => $limit,
11089 'offset' => $offset,
11090 'orderby' => 'title',
11091 'order' => 'ASC',
11092 'post_status' => 'publish',
11093 's' => $data['search_term'],
11094 ];
11095
11096 $point_type = get_posts( $args );
11097
11098 $count_args = [
11099 'post_type' => 'points-type',
11100 'posts_per_page' => -1,
11101 'post_status' => 'publish',
11102 's' => $data['search_term'],
11103 ];
11104
11105 $count_point_type = get_posts( $count_args );
11106 $point_type_count = count( $count_point_type );
11107
11108 $options = [];
11109 if ( $point_type ) {
11110 foreach ( $point_type as $point ) {
11111 $options[] = [
11112 'label' => $point->post_title,
11113 'value' => $point->ID,
11114 ];
11115 }
11116 }
11117
11118 return [
11119 'options' => $options,
11120 'hasMore' => $point_type_count > $limit && $point_type_count > $offset,
11121 ];
11122 }
11123
11124 /**
11125 * Get GamiPress AchievementType List.
11126 *
11127 * @param array $data data.
11128 *
11129 * @return array|void
11130 */
11131 public function search_gp_achivement_type_list( $data ) {
11132 global $wpdb;
11133
11134 $posts = $wpdb->get_results(
11135 "SELECT ID, post_name, post_title, post_type
11136 FROM $wpdb->posts
11137 WHERE post_type LIKE 'achievement-type' AND post_status = 'publish' ORDER BY post_title ASC"
11138 );
11139
11140 $posts_count = count( $posts );
11141
11142 $options = [];
11143 if ( $posts ) {
11144 foreach ( $posts as $post ) {
11145 $options[] = [
11146 'label' => $post->post_title,
11147 'value' => $post->post_name,
11148 ];
11149 }
11150 }
11151
11152 $options[] = [
11153 'label' => 'Points awards',
11154 'value' => 'points-award',
11155 ];
11156 $options[] = [
11157 'label' => 'Step',
11158 'value' => 'step',
11159 ];
11160 $options[] = [
11161 'label' => 'Rank requirement',
11162 'value' => 'rank-requirement',
11163 ];
11164
11165 return [
11166 'options' => $options,
11167 'hasMore' => false,
11168 ];
11169 }
11170
11171 /**
11172 * Get GamiPress Award List.
11173 *
11174 * @param array $data data.
11175 *
11176 * @return array|void
11177 */
11178 public function search_gp_award_list( $data ) {
11179 global $wpdb;
11180
11181 $page = $data['page'];
11182 $limit = Utilities::get_search_page_limit();
11183 $offset = $limit * ( $page - 1 );
11184
11185 $args = [
11186 'post_type' => $data['dynamic']['achivement_type'],
11187 'posts_per_page' => $limit,
11188 'offset' => $offset,
11189 'orderby' => 'title',
11190 'order' => 'ASC',
11191 'post_status' => 'publish',
11192 's' => $data['search_term'],
11193 ];
11194
11195 $award_type = get_posts( $args );
11196 $count_args = [
11197 'post_type' => $data['dynamic']['achivement_type'],
11198 'posts_per_page' => -1,
11199 'post_status' => 'publish',
11200 's' => $data['search_term'],
11201 ];
11202
11203 $count_award_type = get_posts( $count_args );
11204 $award_type_count = count( $count_award_type );
11205 $options = [];
11206 if ( $award_type ) {
11207 foreach ( $award_type as $award ) {
11208 $options[] = [
11209 'label' => $award->post_title,
11210 'value' => $award->ID,
11211 ];
11212 }
11213 }
11214
11215 return [
11216 'options' => $options,
11217 'hasMore' => $award_type_count > $limit && $award_type_count > $offset,
11218 ];
11219 }
11220
11221 /**
11222 * Get Woocommerce Subscription Product List.
11223 *
11224 * @param array $data data.
11225 *
11226 * @return array|void
11227 */
11228 public function search_wc_subscription_product_list( $data ) {
11229 global $wpdb;
11230
11231 $subscriptions = $wpdb->get_results(
11232 $wpdb->prepare(
11233 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
11234 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
11235 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
11236 AND posts.post_type = %s
11237 AND posts.post_status = %s
11238 UNION ALL
11239 SELECT ID, post_title FROM $wpdb->posts
11240 WHERE post_type = %s
11241 AND post_status = %s
11242 ORDER BY post_title",
11243 'product',
11244 'publish',
11245 'shop_subscription',
11246 'publish'
11247 )
11248 );
11249
11250 $options = [];
11251 if ( $subscriptions ) {
11252 foreach ( $subscriptions as $post ) {
11253 $options[] = [
11254 'label' => $post->post_title,
11255 'value' => $post->ID,
11256 ];
11257 }
11258 }
11259
11260 return [
11261 'options' => $options,
11262 'hasMore' => false,
11263 ];
11264 }
11265
11266 /**
11267 * Get Woocommerce Subscriptions Variation list.
11268 *
11269 * @param array $data data.
11270 *
11271 * @return array|void
11272 */
11273 public function search_wc_variable_subscription_list( $data ) {
11274 global $wpdb;
11275
11276 $page = $data['page'];
11277 $limit = Utilities::get_search_page_limit();
11278 $offset = $limit * ( $page - 1 );
11279
11280 if ( ! function_exists( 'wc_get_products' ) ) {
11281 return;
11282 }
11283 $subscription_products = wc_get_products(
11284 [
11285 'type' => [ 'variable-subscription' ],
11286 'posts_per_page' => $limit,
11287 'offset' => $offset,
11288 'orderby' => 'date',
11289 'order' => 'DESC',
11290 ]
11291 );
11292
11293 $subscription_products_count = count( (array) $subscription_products );
11294
11295 $options = [];
11296 if ( $subscription_products ) {
11297 foreach ( (array) $subscription_products as $product ) {
11298 $options[] = [
11299 'label' => $product->get_title(),
11300 'value' => $product->get_id(),
11301 ];
11302 }
11303 }
11304
11305 return [
11306 'options' => $options,
11307 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
11308 ];
11309 }
11310
11311 /**
11312 * Get Woocommerce Variation list.
11313 *
11314 * @param array $data data.
11315 *
11316 * @return array|void
11317 */
11318 public function search_wc_variation_list( $data ) {
11319 global $wpdb;
11320
11321 $page = $data['page'];
11322 $limit = Utilities::get_search_page_limit();
11323 $offset = $limit * ( $page - 1 );
11324
11325 $args = [
11326 'post_type' => 'product_variation',
11327 'post_parent' => $data['dynamic']['variable_subscription'],
11328 'posts_per_page' => $limit,
11329 'offset' => $offset,
11330 'orderby' => 'ID',
11331 'order' => 'ASC',
11332 'post_status' => 'publish',
11333 ];
11334
11335 $variation = get_posts( $args );
11336 $variation_count = count( $variation );
11337
11338 $options = [];
11339 if ( $variation ) {
11340 foreach ( $variation as $product ) {
11341 $options[] = [
11342 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
11343 'value' => $product->ID,
11344 ];
11345 }
11346 }
11347
11348 return [
11349 'options' => $options,
11350 'hasMore' => $variation_count > $limit && $variation_count > $offset,
11351 ];
11352 }
11353
11354 /**
11355 * Get Membership List.
11356 *
11357 * @param array $data data.
11358 *
11359 * @return array
11360 */
11361 public function search_membership_list( $data ) {
11362 global $wpdb;
11363
11364 $levels = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}pmpro_membership_levels ORDER BY id ASC" );
11365 $options = [];
11366 if ( $levels ) {
11367 foreach ( $levels as $level ) {
11368 $options[] = [
11369 'label' => $level->name,
11370 'value' => $level->id,
11371 ];
11372 }
11373 }
11374
11375 return [
11376 'options' => $options,
11377 'hasMore' => false,
11378 ];
11379 }
11380
11381 /**
11382
11383 * Get EventsManager last data.
11384 *
11385 * @param array $data data.
11386 *
11387 * @return array
11388 */
11389 public function search_events_manager_data( $data ) {
11390 global $wpdb;
11391 $trigger = $data['search_term'];
11392 $context = [];
11393
11394 $post_id = (int) ( isset( $data['filter']['post_id']['value'] ) ? $data['filter']['post_id']['value'] : '-1' );
11395 if ( 'em_user_register_in_event' === $trigger ) {
11396 if ( $post_id > 0 ) {
11397 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11398 $all_bookings = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_bookings as b INNER JOIN {$wpdb->prefix}em_events as e ON b.event_id = e.event_id WHERE e.event_status = 1 AND b.booking_status NOT IN (2,3) AND b.event_id = %s AND e.event_end_date >= CURRENT_DATE ORDER BY b.booking_id DESC LIMIT 1", $event_id_id ) );
11399 } else {
11400 $all_bookings = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}em_bookings as b INNER JOIN {$wpdb->prefix}em_events as e ON b.event_id = e.event_id WHERE e.event_status = 1 AND b.booking_status NOT IN (2,3) AND e.event_end_date >= CURRENT_DATE ORDER BY b.booking_id DESC LIMIT 1" );
11401
11402 }
11403
11404 if ( ! empty( $all_bookings ) ) {
11405 $user_id = $all_bookings->person_id;
11406 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
11407 $context['pluggable_data'] = array_merge(
11408 WordPress::get_user_context( $user_id ),
11409 json_decode( wp_json_encode( $all_bookings ), true )
11410 );
11411 if ( ! empty( $location ) ) {
11412 $context['pluggable_data'] = array_merge( $context['pluggable_data'], json_decode( wp_json_encode( $location ), true ) );
11413 }
11414
11415 $context['response_type'] = 'live';
11416 }
11417 } elseif ( 'em_user_unregister_from_event' === $trigger ) {
11418
11419 if ( $post_id > 0 ) {
11420 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11421 $all_bookings = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_bookings as b INNER JOIN {$wpdb->prefix}em_events as e ON b.event_id = e.event_id WHERE e.event_status = 1 AND b.booking_status IN (2,3) AND b.event_id = %s AND e.event_end_date >= CURRENT_DATE ORDER BY b.booking_id DESC LIMIT 1", $event_id_id ) );
11422 } else {
11423 $all_bookings = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}em_bookings as b INNER JOIN {$wpdb->prefix}em_events as e ON b.event_id = e.event_id WHERE e.event_status = 1 AND b.booking_status IN (2,3) AND e.event_end_date >= CURRENT_DATE ORDER BY b.booking_id DESC LIMIT 1" );
11424
11425 }
11426
11427 if ( ! empty( $all_bookings ) ) {
11428 $user_id = $all_bookings->person_id;
11429 $context['pluggable_data'] = array_merge(
11430 WordPress::get_user_context( $user_id ),
11431 json_decode( wp_json_encode( $all_bookings ), true )
11432 );
11433 $context['response_type'] = 'live';
11434 }
11435 } elseif ( 'em_user_booking_approved' === $trigger ) {
11436
11437 if ( $post_id > 0 ) {
11438 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11439 $all_bookings = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_bookings as b INNER JOIN {$wpdb->prefix}em_events as e ON b.event_id = e.event_id WHERE e.event_status = 1 AND b.booking_status=1 AND b.event_id = %s AND e.event_end_date >= CURRENT_DATE ORDER BY b.booking_id DESC LIMIT 1", $event_id_id ) );
11440 } else {
11441 $all_bookings = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}em_bookings as b INNER JOIN {$wpdb->prefix}em_events as e ON b.event_id = e.event_id WHERE e.event_status = 1 AND b.booking_status=1 AND e.event_end_date >= CURRENT_DATE ORDER BY b.booking_id DESC LIMIT 1" );
11442
11443 }
11444
11445 if ( ! empty( $all_bookings ) ) {
11446 $user_id = $all_bookings->person_id;
11447 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
11448 $context['pluggable_data'] = array_merge(
11449 WordPress::get_user_context( $user_id ),
11450 json_decode( wp_json_encode( $all_bookings ), true )
11451 );
11452 if ( ! empty( $location ) ) {
11453 $context['pluggable_data'] = array_merge( $context['pluggable_data'], json_decode( wp_json_encode( $location ), true ) );
11454 }
11455
11456 $context['response_type'] = 'live';
11457
11458 }
11459 } elseif ( 'em_user_registers_event_with_specific_ticket' === $trigger ) {
11460 if ( $post_id > 0 ) {
11461 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11462 $all_bookings = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_bookings as b INNER JOIN {$wpdb->prefix}em_events as e ON b.event_id = e.event_id WHERE e.event_status = 1 AND b.booking_status NOT IN (2,3) AND b.event_id = %s AND e.event_end_date >= CURRENT_DATE ORDER BY b.booking_id DESC LIMIT 1", $event_id_id ) );
11463 }
11464 if ( ! empty( $all_bookings ) ) {
11465 $ticket_id = (int) $data['filter']['ticket_id']['value'];
11466 $all_ticket_bookings = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_tickets_bookings as b INNER JOIN {$wpdb->prefix}em_tickets as e ON b.ticket_id = e.ticket_id WHERE b.booking_id = %d AND e.ticket_id = %d ORDER BY b.ticket_booking_id DESC LIMIT 1", $all_bookings->booking_id, $ticket_id ) );
11467 if ( ! empty( $all_ticket_bookings ) ) {
11468 $user_id = $all_bookings->person_id;
11469 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
11470 $bookings_str = wp_json_encode( $all_bookings );
11471 $ticket_bookings_str = wp_json_encode( $all_ticket_bookings );
11472
11473 $context['pluggable_data'] = array_merge(
11474 WordPress::get_user_context( $user_id )
11475 );
11476 if ( is_string( $bookings_str ) && is_string( $ticket_bookings_str ) ) {
11477 $bookings_arr = json_decode( $bookings_str, true );
11478 $ticket_bookings_arr = json_decode( $ticket_bookings_str, true );
11479 if ( is_array( $bookings_arr ) && is_array( $ticket_bookings_arr ) ) {
11480 $context['pluggable_data'] = array_merge(
11481 $context['pluggable_data'],
11482 $bookings_arr,
11483 $ticket_bookings_arr
11484 );
11485 }
11486 }
11487 if ( ! empty( $location ) ) {
11488 $location_str = wp_json_encode( $location );
11489 if ( is_string( $location_str ) ) {
11490 $location_arr = json_decode( $location_str, true );
11491 if ( is_array( $location_arr ) ) {
11492 $context['pluggable_data'] = array_merge( $context['pluggable_data'], $location_arr );
11493 }
11494 }
11495 }
11496 $context['response_type'] = 'live';
11497 }
11498 }
11499 }
11500 return $context;
11501 }
11502
11503 /**
11504 * Get Events Manager Events Ticket list.
11505 *
11506 * @param array $data data.
11507 *
11508 * @return array|void
11509 */
11510 public function search_em_event_tickets( $data ) {
11511 global $wpdb;
11512
11513 $options = [];
11514
11515 $event = $data['dynamic']['post_id'];
11516
11517 $event_id = $wpdb->get_var( $wpdb->prepare( "SELECT event_id FROM {$wpdb->prefix}em_events WHERE post_id = %d", $event ) );
11518 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT ticket_id,ticket_name FROM {$wpdb->prefix}em_tickets WHERE event_id = %d ORDER BY ticket_id", $event_id[0] ) );
11519
11520 if ( $tickets ) {
11521 foreach ( $tickets as $ticket ) {
11522 $options[] = [
11523 'label' => $ticket->ticket_name,
11524 'value' => $ticket->ticket_id,
11525 ];
11526 }
11527 }
11528
11529 return [
11530 'options' => $options,
11531 'hasMore' => false,
11532 ];
11533 }
11534
11535 /**
11536
11537 * Get learnpress last data.
11538 *
11539 * @param array $data data.
11540 *
11541 * @return array
11542 */
11543 public function search_learnpress_lms_last_data( $data ) {
11544 global $wpdb;
11545 $trigger = $data['search_term'];
11546 $course_data = [];
11547 $lesson_data = [];
11548 $context = [];
11549
11550
11551 if ( 'learnpress_course_completed' === $trigger ) {
11552 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
11553 if ( $course_id > 0 ) {
11554
11555 $course = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learnpress_user_items WHERE item_id= %s && user_id>0 && status= 'finished' ORDER BY item_id DESC LIMIT 1", $course_id ) );
11556 } else {
11557
11558 $course = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}learnpress_user_items WHERE item_type= 'lp_course' && user_id>0 && status= 'finished' ORDER BY item_id DESC LIMIT 1" );
11559 }
11560
11561 if ( ! empty( $course ) ) {
11562 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
11563 $context['response_type'] = 'live';
11564 $context['pluggable_data'] = $course_data;
11565 }
11566 } elseif ( 'learnpress_lesson_completed' === $trigger ) {
11567 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
11568 if ( $lesson_id > 0 ) {
11569
11570 $lesson = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learnpress_user_items WHERE item_id= %s && user_id>0 && status= 'completed' ORDER BY item_id DESC LIMIT 1", $lesson_id ) );
11571 } else {
11572
11573 $lesson = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}learnpress_user_items WHERE item_type= 'lp_lesson' && user_id>0 && status= 'completed' ORDER BY item_id DESC LIMIT 1" );
11574 }
11575
11576 if ( ! empty( $lesson ) ) {
11577 $lesson_data = array_merge( WordPress::get_user_context( $lesson->user_id ), LearnPress::get_lpc_lesson_context( $lesson->item_id ), LearnPress::get_lpc_course_context( $lesson->ref_id ) );
11578 $context['response_type'] = 'live';
11579 $context['pluggable_data'] = $lesson_data;
11580 }
11581 } elseif ( 'learnpress_user_enrolled_in_course' === $trigger ) {
11582 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
11583 if ( $course_id > 0 ) {
11584
11585 $course = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learnpress_user_items WHERE item_id= %s && status= 'enrolled' ORDER BY item_id DESC LIMIT 1", $course_id ) );
11586 } else {
11587
11588 $course = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}learnpress_user_items WHERE item_type= 'lp_course' && user_id>0 && status= 'enrolled' ORDER BY item_id DESC LIMIT 1" );
11589 }
11590
11591 if ( ! empty( $course ) ) {
11592 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
11593 $context['response_type'] = 'live';
11594 $context['pluggable_data'] = $course_data;
11595
11596 }
11597 }
11598
11599 return $context;
11600 }
11601
11602 /**
11603 * Get Woocommerce Memberships Plan List.
11604 *
11605 * @param array $data data.
11606 *
11607 * @return array|void
11608 */
11609 public function search_wc_membership_plan_list( $data ) {
11610 global $wpdb;
11611
11612 $page = $data['page'];
11613 $limit = Utilities::get_search_page_limit();
11614 $offset = $limit * ( $page - 1 );
11615
11616 $args = [
11617 'post_type' => 'wc_membership_plan',
11618 'posts_per_page' => $limit,
11619 'offset' => $offset,
11620 'orderby' => 'title',
11621 'order' => 'ASC',
11622 'post_status' => 'publish',
11623 'fields' => 'ids',
11624 ];
11625 $loop = new WP_Query( $args );
11626
11627 $plans = (array) $loop->posts;
11628 $plans_count = wp_count_posts( 'wc_membership_plan' )->publish;
11629
11630 $options = [];
11631 if ( ! empty( $plans ) ) {
11632 if ( is_array( $plans ) ) {
11633 foreach ( $plans as $plan_id ) {
11634 $options[] = [
11635 'label' => get_the_title( $plan_id ),
11636 'value' => $plan_id,
11637 ];
11638 }
11639 }
11640 }
11641
11642 return [
11643 'options' => $options,
11644 'hasMore' => $plans_count > $limit && $plans_count > $offset,
11645 ];
11646 }
11647
11648 /**
11649 * Get BuddyPress Private group.
11650 *
11651 * @param array $data data.
11652 *
11653 * @return array|void
11654 */
11655 public function search_bp_private_group_list( $data ) {
11656 global $wpdb;
11657
11658 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'private'" );
11659
11660 $options = [];
11661 if ( $groups ) {
11662 foreach ( $groups as $group ) {
11663 $options[] = [
11664 'label' => $group->name,
11665 'value' => $group->id,
11666 ];
11667 }
11668 }
11669
11670 return [
11671 'options' => $options,
11672 'hasMore' => false,
11673 ];
11674 }
11675
11676 /**
11677 * Get BuddyPress Public group.
11678 *
11679 * @param array $data data.
11680 *
11681 * @return array|void
11682 */
11683 public function search_bp_public_group_list( $data ) {
11684 global $wpdb;
11685
11686 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'public'" );
11687
11688 $options = [];
11689 if ( $groups ) {
11690 foreach ( $groups as $group ) {
11691 $options[] = [
11692 'label' => $group->name,
11693 'value' => $group->id,
11694 ];
11695 }
11696 }
11697
11698 return [
11699 'options' => $options,
11700 'hasMore' => false,
11701 ];
11702 }
11703
11704 /**
11705 * Get BuddyPress group.
11706 *
11707 * @param array $data data.
11708 *
11709 * @return array|void
11710 */
11711 public function search_bp_group_list( $data ) {
11712 global $wpdb;
11713
11714 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
11715
11716 $options = [];
11717 if ( $groups ) {
11718 foreach ( $groups as $group ) {
11719 $options[] = [
11720 'label' => $group->name,
11721 'value' => $group->id,
11722 ];
11723 }
11724 }
11725
11726 return [
11727 'options' => $options,
11728 'hasMore' => false,
11729 ];
11730 }
11731
11732 /**
11733 * Get BuddyPress field.
11734 *
11735 * @param array $data data.
11736 *
11737 * @return array|void
11738 */
11739 public function search_bp_field_list( $data ) {
11740 global $wpdb;
11741
11742 $base_group_id = 1;
11743 if ( function_exists( 'bp_xprofile_base_group_id' ) ) {
11744 $base_group_id = bp_xprofile_base_group_id();
11745 }
11746
11747 $xprofile_fields = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}bp_xprofile_fields WHERE parent_id = 0 AND group_id = %d ORDER BY field_order ASC", $base_group_id ) );
11748
11749 $options = [];
11750 if ( ! empty( $xprofile_fields ) ) {
11751 foreach ( $xprofile_fields as $xprofile_field ) {
11752 $options[] = [
11753 'label' => $xprofile_field->name,
11754 'value' => $xprofile_field->id,
11755 ];
11756 }
11757 }
11758
11759 return [
11760 'options' => $options,
11761 'hasMore' => false,
11762 ];
11763 }
11764
11765 /**
11766 * Get BuddyPress member type.
11767 *
11768 * @param array $data data.
11769 *
11770 * @return array|void
11771 */
11772 public function search_bp_member_type_list( $data ) {
11773 $options = [];
11774 if ( function_exists( 'bp_get_member_types' ) ) {
11775 $types = bp_get_member_types( [] );
11776 if ( $types ) {
11777 foreach ( $types as $key => $type ) {
11778 $options[] = [
11779 'label' => $type,
11780 'value' => $key,
11781 ];
11782 }
11783 }
11784 }
11785
11786 return [
11787 'options' => $options,
11788 'hasMore' => false,
11789 ];
11790 }
11791
11792 /**
11793 * Get last data for WP All Import.
11794 *
11795 * @param array $data data.
11796 * @return mixed
11797 */
11798 public function search_wp_all_import_last_data( $data ) {
11799 global $wpdb;
11800 $post_type = $data['filter']['post_type']['value'];
11801 $trigger = $data['search_term'];
11802
11803 if ( 'wp_all_import_post_type_imported' === $trigger ) {
11804 if ( -1 == $post_type ) {
11805 $imports = $wpdb->get_row( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts ORDER BY id DESC LIMIT 1", ARRAY_A );
11806 $posts[0] = $imports['post_id'];
11807 } else {
11808 $imports = $wpdb->get_results( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts", ARRAY_A );
11809 $imports = array_column( $imports, 'post_id' );
11810 $args = [
11811 'posts_per_page' => 1,
11812 'post_type' => $post_type,
11813 'post__in' => $imports,
11814 ];
11815 $posts = get_posts( $args );
11816 }
11817 } elseif ( 'wp_all_import_completed' === $trigger ) {
11818 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 0 ORDER BY id DESC LIMIT 1", ARRAY_A );
11819 } elseif ( 'wp_all_import_failed' === $trigger ) {
11820 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 1 ORDER BY id DESC LIMIT 1", ARRAY_A );
11821 }
11822
11823 if ( 'wp_all_import_post_type_imported' === $trigger && empty( $imports ) ) {
11824 $context = json_decode( '{"response_type":"sample","pluggable_data":{"ID": 1,"post_author": "1","post_date": "2023-07-12 06:31:35","post_date_gmt": "2023-07-12 06:31:35","post_content": "","post_title": "Test","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "open","post_password": "","post_name": "test","to_ping": "","pinged": "","post_modified": "2023-07-12 06:31:35","post_modified_gmt": "2023-07-12 06:31:35","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/example.com\/test\/","menu_order": 0,"post_type": "post","post_mime_type": "","comment_count": "0","filter": "raw"}}', true );
11825 return $context;
11826 } elseif ( empty( $imports ) ) {
11827 $context = json_decode( '{"response_type":"sample","pluggable_data":{"id": "1","parent_import_id": "0","name": "demowpinstawpxyz.WordPress.2023_07_12.xml","friendly_name": "","type": "upload","feed_type": "","path": "\/wpallimport\/uploads\/ee8816eebf7a373454cdd1189c831241\/demowpinstawpxyz.WordPress.2023_07_12.xml","xpath": "\/rss","registered_on": "2023-07-12 05:10:29","root_element": "rss","processing": "0","executing": "0","triggered": "0","queue_chunk_number": "0","first_import": "2023-07-12 05:09:41","count": "1","imported": "0","created": "0","updated": "0","skipped": "1","deleted": "0","changed_missing": "0","canceled": "0","canceled_on": "0000-00-00 00:00:00","failed": "0","failed_on": "0000-00-00 00:00:00","settings_update_on": "0000-00-00 00:00:00","last_activity": "2023-07-12 05:10:24","iteration": "1"}}', true );
11828 return $context;
11829 }
11830
11831 $context['response_type'] = 'live';
11832 if ( ! empty( $posts ) ) {
11833 $context['pluggable_data'] = WordPress::get_post_context( $posts[0] );
11834 } else {
11835 $context['pluggable_data'] = $imports;
11836 }
11837
11838 return $context;
11839 }
11840
11841 /**
11842 * Get Wp Simple Pay Forms.
11843 *
11844 * @param array $data data.
11845 *
11846 * @return array
11847 */
11848 public function search_wp_simple_pay_forms( $data ) {
11849
11850 $page = $data['page'];
11851 $limit = Utilities::get_search_page_limit();
11852 $offset = $limit * ( $page - 1 );
11853
11854 $forms = get_posts(
11855 [
11856 'post_type' => 'simple-pay',
11857 'posts_per_page' => $limit,
11858 'offset' => $offset,
11859 'fields' => 'ids',
11860 ]
11861 );
11862
11863 $forms_count = wp_count_posts( 'simple-pay' )->publish;
11864
11865 $options = [];
11866
11867 if ( ! empty( $forms ) ) {
11868 foreach ( $forms as $form_id ) {
11869 if ( function_exists( 'simpay_get_form' ) ) {
11870 $form = simpay_get_form( $form_id );
11871 $options[] = [
11872 'label' => null !== get_the_title( $form_id ) ? $form->company_name : get_the_title( $form_id ),
11873 'value' => $form_id,
11874 ];
11875 }
11876 }
11877 }
11878
11879 return [
11880 'options' => $options,
11881 'hasMore' => $forms_count > $limit && $forms_count > $offset,
11882 ];
11883 }
11884
11885 /**
11886 * Get Post list as per post type for metabox.
11887 *
11888 * @param array $data data.
11889 *
11890 * @return array
11891 */
11892 public function search_mb_posts_list( $data ) {
11893
11894 $page = $data['page'];
11895 $limit = Utilities::get_search_page_limit();
11896 $offset = $limit * ( $page - 1 );
11897
11898 $posts = get_posts(
11899 [
11900 'post_type' => $data['dynamic'],
11901 'posts_per_page' => $limit,
11902 'offset' => $offset,
11903 'fields' => 'ids',
11904 ]
11905 );
11906
11907 $all_posts = get_posts(
11908 [
11909 'post_type' => $data['dynamic'],
11910 'posts_per_page' => -1,
11911 'fields' => 'ids',
11912 ]
11913 );
11914
11915 $posts_count = count( $all_posts );
11916
11917 $options = [];
11918
11919 if ( ! empty( $posts ) ) {
11920 foreach ( $posts as $post ) {
11921 $title = html_entity_decode( get_the_title( $post ), ENT_QUOTES, 'UTF-8' );
11922 $options[] = [
11923 'label' => $title,
11924 'value' => $post,
11925 ];
11926 }
11927 }
11928
11929 return [
11930 'options' => $options,
11931 'hasMore' => $posts_count > $limit && $posts_count > $offset,
11932 ];
11933 }
11934
11935 /**
11936 * Get Metabox Custom box in Post list.
11937 *
11938 * @param array $data data.
11939 *
11940 * @return array
11941 */
11942 public function search_mb_field_list( $data ) {
11943
11944 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
11945 return [];
11946 }
11947
11948 $options = [];
11949
11950 $metabox_fields = (array) rwmb_get_object_fields( $data['dynamic'] );
11951
11952 foreach ( $metabox_fields as $metabox_field ) {
11953
11954 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
11955
11956 $options[] = [
11957 'label' => $metabox_field['name'],
11958 'value' => $metabox_field['id'],
11959 ];
11960
11961 }
11962 }
11963
11964 return [
11965 'options' => $options,
11966 'hasMore' => false,
11967 ];
11968 }
11969
11970 /**
11971 * Get Metabox Custom box user list.
11972 *
11973 * @param array $data data.
11974 *
11975 * @return array
11976 */
11977 public function search_mb_user_field_list( $data ) {
11978
11979 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
11980 return [];
11981 }
11982
11983 $options = [];
11984
11985 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
11986
11987 foreach ( $metabox_fields as $metabox_field ) {
11988
11989 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
11990
11991 $options[] = [
11992 'label' => $metabox_field['name'],
11993 'value' => $metabox_field['id'],
11994 ];
11995
11996 }
11997 }
11998
11999 return [
12000 'options' => $options,
12001 'hasMore' => false,
12002 ];
12003 }
12004
12005 /**
12006 * Search Last Updated Field Data for MetaBox.
12007 *
12008 * @param array $data data.
12009 * @return array
12010 */
12011 public function search_meta_box_field_data( $data ) {
12012 global $wpdb;
12013
12014 $context = [];
12015
12016 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12017
12018 $post_type = $data['filter']['wp_post_type']['value'];
12019 $post = $data['filter']['wp_post']['value'];
12020
12021 if ( -1 === $field ) {
12022 if ( function_exists( 'rwmb_get_object_fields' ) ) {
12023 $metaboxes = rwmb_get_object_fields( $post_type );
12024
12025 if ( ! empty( $metaboxes ) ) {
12026 $random_key = array_rand( $metaboxes );
12027 $field = $random_key;
12028 } else {
12029 $result = '';
12030 }
12031 }
12032 } else {
12033 $field = $data['filter']['field_id']['value'];
12034 }
12035
12036 if ( function_exists( 'rwmb_meta' ) ) {
12037 $result = rwmb_meta( $field, '', $post );
12038 }
12039
12040 $response = [];
12041 if ( ! empty( $result ) ) {
12042 $response['pluggable_data'] = array_merge( [ $field => $result ], WordPress::get_post_context( $post ) );
12043 $response['response_type'] = 'live';
12044 } else {
12045 $response = json_decode( '{"response_type":"sample","pluggable_data":{"custom_description": "custom message", "ID": 1, "post_author": "1", "post_date": "2023-05-31 13:26:24", "post_date_gmt": "2023-05-31 13:26:24", "post_content": "", "post_title": "Test", "post_excerpt": "", "post_status": "publish", "comment_status": "open", "ping_status": "open", "post_password": "", "post_name": "test", "to_ping": "", "pinged": "", "post_modified": "2023-08-17 09:15:56", "post_modified_gmt": "2023-08-17 09:15:56", "post_content_filtered": "", "post_parent": 0, "guid": "https:\/\/example.com\/?p=1", "menu_order": 0, "post_type": "post", "post_mime_type": "", "comment_count": "2", "filter": "raw"}}', true );
12046 }
12047
12048 return $response;
12049 }
12050
12051 /**
12052 * Search Last Updated User Field Data MetaBox.
12053 *
12054 * @param array $data data.
12055 * @return array
12056 */
12057 public function search_user_meta_box_field_data( $data ) {
12058 global $wpdb;
12059
12060 $context = [];
12061
12062 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12063
12064 if ( -1 === $field ) {
12065 if ( function_exists( 'rwmb_get_object_fields' ) ) {
12066 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
12067
12068 if ( ! empty( $metabox_fields ) ) {
12069 $random_key = array_rand( $metabox_fields );
12070 $field = $random_key;
12071 } else {
12072 $result = '';
12073 }
12074 }
12075 } else {
12076 $field = $data['filter']['field_id']['value'];
12077 }
12078
12079 $users = get_users(
12080 [
12081 'fields' => 'ID',
12082 'meta_key' => $field,
12083 ]
12084 );
12085
12086 if ( ! empty( $users ) ) {
12087 $user_random_key = array_rand( $users );
12088 $user_id = $user_random_key;
12089 if ( function_exists( 'rwmb_get_value' ) ) {
12090 $result = rwmb_get_value( $field, [ 'object_type' => 'user' ], $users[ $user_id ] );
12091 }
12092
12093 $response = [];
12094 if ( ! empty( $result ) ) {
12095 $context = [
12096 'field_id' => $field,
12097 $field => $result,
12098 'user' => WordPress::get_user_context( $users[ $user_id ] ),
12099 ];
12100 $response['pluggable_data'] = $context;
12101 $response['response_type'] = 'live';
12102 } else {
12103 $response = json_decode(
12104 '{
12105 "response_type": "sample",
12106 "pluggable_data": {
12107 "field_id": "gender",
12108 "user": {
12109 "wp_user_id": 114,
12110 "user_login": "test",
12111 "display_name": "test",
12112 "user_firstname": "test",
12113 "user_lastname": "test",
12114 "user_email": "test@test.com",
12115 "user_role": [ "subscriber" ]
12116 }
12117 }
12118 }',
12119 true
12120 );
12121 }
12122 } else {
12123 $response = json_decode(
12124 '{
12125 "response_type": "sample",
12126 "pluggable_data": {
12127 "field_id": "gender",
12128 "user": {
12129 "wp_user_id": 114,
12130 "user_login": "test",
12131 "display_name": "test",
12132 "user_firstname": "test",
12133 "user_lastname": "test",
12134 "user_email": "test@test.com",
12135 "user_role": [ "subscriber" ]
12136 }
12137 }
12138 }',
12139 true
12140 );
12141 }
12142
12143 return $response;
12144 }
12145
12146 /**
12147 * Search forms of Pie Forms.
12148 *
12149 * @param array $data data.
12150 * @return array
12151 */
12152 public function search_wp_polls_list( $data ) {
12153 global $wpdb;
12154 $options = [];
12155
12156 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsq' ) ) ) {
12157
12158 $results = $wpdb->get_results( 'SELECT pollq_id, pollq_question FROM ' . $wpdb->prefix . 'pollsq WHERE pollq_active = 1' );
12159
12160 if ( $results ) {
12161 foreach ( $results as $result ) {
12162 $options[] = [
12163 'label' => $result->pollq_question,
12164 'value' => $result->pollq_id,
12165 ];
12166 }
12167 }
12168 }
12169
12170 return [
12171 'options' => $options,
12172 'hasMore' => false,
12173 ];
12174 }
12175
12176 /**
12177 * Search answers of WP-Polls questions.
12178 *
12179 * @param array $data data.
12180 * @return array
12181 */
12182 public function search_wp_polls_answers( $data ) {
12183 global $wpdb;
12184
12185 $options = [];
12186 $poll_id = $data['dynamic'];
12187
12188 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsa' ) ) ) {
12189
12190 if ( '-1' !== $poll_id ) {
12191 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa WHERE polla_qid = %d', $poll_id ) );
12192 } else {
12193 $results = $wpdb->get_results( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa' );
12194 }
12195
12196 if ( $results ) {
12197 foreach ( $results as $result ) {
12198 $options[] = [
12199 'label' => $result->polla_answers,
12200 'value' => $result->polla_aid,
12201 ];
12202 }
12203 }
12204 }
12205
12206 return [
12207 'options' => $options,
12208 'hasMore' => false,
12209 ];
12210 }
12211
12212 /**
12213 * Get last data for trigger.
12214 *
12215 * @param array $data data.
12216 * @return array
12217 */
12218 public function search_wp_polls_triggers_last_data( $data ) {
12219 global $wpdb;
12220
12221 $context = [];
12222 $context['response_type'] = 'sample';
12223
12224 $poll = [
12225 'poll_id' => 1,
12226 'question' => 'Which skills are you interested to learn?',
12227 'answers' => 'Web Development, Graphic Designing, Content Writing, Digital Marketing',
12228 'start_date' => '2023-08-29 17:19:13',
12229 'end_date' => 'Not set',
12230 'selected_answers' => 'Content Writing, Web Development',
12231 'selected_answer_id' => 2,
12232 ];
12233
12234 $poll_data = $wpdb->get_row( "SELECT pollip_qid AS poll_id, pollip_aid AS answer_id FROM {$wpdb->prefix}pollsip ORDER BY pollip_id DESC LIMIT 1" );
12235
12236 if ( ! empty( $poll_data ) ) {
12237 $poll = WpPolls::get_poll_context( (string) $poll_data->answer_id, (int) $poll_data->poll_id );
12238 $poll['selected_answer_id'] = (int) $poll_data->answer_id;
12239
12240 $context['response_type'] = 'live';
12241 }
12242
12243 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
12244
12245 if ( 'poll_submitted' === $term ) {
12246 unset( $poll['selected_answer_id'] );
12247 }
12248
12249 $context['pluggable_data'] = $poll;
12250
12251 return $context;
12252 }
12253
12254 /**
12255 * Get ACF Custom fields list.
12256 *
12257 * @param array $data data.
12258 *
12259 * @return array
12260 */
12261 public function search_acf_post_field_list( $data ) {
12262
12263 $post_id = $data['dynamic']['wp_post'];
12264 $selected_post_type = $data['dynamic']['wp_post_type'];
12265
12266 if ( -1 === $post_id ) {
12267 $args = [
12268 'numberposts' => 1,
12269 'fields' => 'ids',
12270 'orderby' => 'rand',
12271 'post_type' => $selected_post_type,
12272 ];
12273 $posts = get_posts( $args );
12274 $post_id = $posts[0];
12275 }
12276
12277 $args = [
12278 'post_id' => $post_id,
12279 ];
12280 if ( ! is_numeric( $post_id ) ) {
12281 $args = [
12282 'post_type' => $post_id,
12283 ];
12284 }
12285 $options = [];
12286 if ( function_exists( 'acf_get_field_groups' ) ) {
12287 $field_groups_collection = acf_get_field_groups( $args );
12288 foreach ( $field_groups_collection as $field_group ) {
12289 if ( function_exists( 'acf_get_fields' ) ) {
12290 $field_groups[] = acf_get_fields( $field_group['key'] );
12291 }
12292 }
12293
12294 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
12295 foreach ( $field_groups as $field_groups ) {
12296 foreach ( $field_groups as $field_group ) {
12297 $options[] = [
12298 'value' => $field_group['name'],
12299 'label' => ! empty( $field_group['label'] ) ? $field_group['label'] : $field_group['name'],
12300 ];
12301 }
12302 }
12303 }
12304 }
12305
12306 return [
12307 'options' => $options,
12308 'hasMore' => false,
12309 ];
12310 }
12311
12312 /**
12313 * Get ACF Custom fields list.
12314 *
12315 * @param array $data data.
12316 *
12317 * @return array
12318 */
12319 public function search_acf_user_field_list( $data ) {
12320
12321 if ( ! function_exists( 'acf_get_fields' ) ) {
12322 return [];
12323 }
12324 if ( ! function_exists( 'acf_get_field_groups' ) ) {
12325 return [];
12326 }
12327 $groups_user_form = [];
12328 $options = [];
12329 if ( function_exists( 'acf_get_field_groups' ) ) {
12330 $field_groups = acf_get_field_groups();
12331 foreach ( $field_groups as $group ) {
12332 if ( ! empty( $group['location'] ) ) {
12333 foreach ( $group['location'] as $locations ) {
12334 foreach ( $locations as $location ) {
12335 if ( 'user_form' === $location['param'] || 'user_role' === $location['param'] || 'current_user' === $location['param'] || 'current_user_role' === $location['param'] ) {
12336 $groups_user_form[] = $group;
12337 }
12338 }
12339 }
12340 }
12341 }
12342
12343 if ( empty( $groups_user_form ) ) {
12344 return [];
12345 }
12346
12347 $key_values = array_map(
12348 function ( $item ) {
12349 return $item['key'];
12350 },
12351 $groups_user_form
12352 );
12353 $unique_keys = array_unique( $key_values );
12354 $unique_array = array_intersect_key( $groups_user_form, $unique_keys );
12355
12356 foreach ( $unique_array as $group ) {
12357 if ( function_exists( 'acf_get_fields' ) ) {
12358 $group_fields = acf_get_fields( $group['key'] );
12359 }
12360 if ( ! empty( $group_fields ) ) {
12361 foreach ( $group_fields as $field ) {
12362 $options[] = [
12363 'value' => $field['name'],
12364 'label' => $field['label'],
12365 ];
12366
12367 }
12368 }
12369 }
12370 }
12371
12372 return [
12373 'options' => $options,
12374 'hasMore' => false,
12375 ];
12376 }
12377
12378 /**
12379 * Get ACF Custom fields list.
12380 *
12381 * @param array $data data.
12382 *
12383 * @return array
12384 */
12385 public function search_acf_options_field_list( $data ) {
12386
12387 if ( ! function_exists( 'acf_get_fields' ) ) {
12388 return [];
12389 }
12390 if ( ! function_exists( 'acf_get_field_groups' ) ) {
12391 return [];
12392 }
12393 $groups_options_form = [];
12394 $options = [];
12395 if ( function_exists( 'acf_get_field_groups' ) ) {
12396 $field_groups = acf_get_field_groups();
12397 foreach ( $field_groups as $group ) {
12398 if ( ! empty( $group['location'] ) ) {
12399 foreach ( $group['location'] as $locations ) {
12400 foreach ( $locations as $location ) {
12401 if ( 'options_page' === $location['param'] ) {
12402 $groups_options_form[] = $group;
12403 }
12404 }
12405 }
12406 }
12407 }
12408 if ( empty( $groups_options_form ) ) {
12409 return [];
12410 }
12411 $key_values = array_map(
12412 function ( $item ) {
12413 return $item['key'];
12414 },
12415 $groups_options_form
12416 );
12417 $unique_keys = array_unique( $key_values );
12418 $unique_array = array_intersect_key( $groups_options_form, $unique_keys );
12419 foreach ( $unique_array as $group ) {
12420 if ( function_exists( 'acf_get_fields' ) ) {
12421 $group_fields = acf_get_fields( $group['key'] );
12422 }
12423 if ( ! empty( $group_fields ) ) {
12424 foreach ( $group_fields as $field ) {
12425 $options[] = [
12426 'value' => $field['name'],
12427 'label' => $field['label'],
12428 ];
12429
12430 }
12431 }
12432 }
12433 }
12434
12435 return [
12436 'options' => $options,
12437 'hasMore' => false,
12438 ];
12439 }
12440
12441 /**
12442 * Search Last Updated Field Data for ACF.
12443 *
12444 * @param array $data data.
12445 * @return array
12446 */
12447 public function search_acf_post_field_data( $data ) {
12448 $context = [];
12449
12450 $field = ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12451
12452 $post_type = $data['filter']['wp_post_type']['value'];
12453 $post = $data['filter']['wp_post']['value'];
12454
12455 if ( -1 === $post ) {
12456 $args = [
12457 'numberposts' => 1,
12458 'fields' => 'ids',
12459 'orderby' => 'rand',
12460 'post_type' => $post_type,
12461 ];
12462 $posts = get_posts( $args );
12463 $post = $posts[0];
12464 }
12465 if ( -1 === $field ) {
12466 $args = [
12467 'post_id' => $post,
12468 ];
12469 if ( function_exists( 'acf_get_field_groups' ) ) {
12470 $field_groups_collection = acf_get_field_groups( $args );
12471 }
12472 if ( ! empty( $field_groups_collection ) ) {
12473 foreach ( $field_groups_collection as $field_group ) {
12474 if ( function_exists( 'acf_get_fields' ) ) {
12475 $field_groups[] = acf_get_fields( $field_group['key'] );
12476 }
12477 }
12478 }
12479 $fields = [];
12480 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
12481 foreach ( $field_groups as $field_groups ) {
12482 $fields[] = $field_groups;
12483 }
12484 }
12485 if ( ! empty( $fields ) ) {
12486 $random_key = array_rand( $fields[0] );
12487 $field_key = $fields[0][ $random_key ];
12488 $field = $field_key['name'];
12489 } else {
12490 $result = '';
12491 }
12492 } else {
12493 $field = $data['filter']['field_id']['value'];
12494 }
12495 if ( function_exists( ( 'get_field' ) ) ) {
12496 $result = get_field( $field, $post );
12497 }
12498
12499 $response = [];
12500 if ( ! empty( $result ) ) {
12501 $post_fields = [];
12502 if ( function_exists( 'get_fields' ) ) {
12503 $post_fields = get_fields( $post );
12504 }
12505 $response['pluggable_data'] = array_merge( [ $field => $result ], [ 'field_id' => $field ], [ 'post_fields' => $post_fields ], [ 'post' => WordPress::get_post_context( $post ) ], [ 'wp_post' => $post ], [ 'wp_post_type' => get_post_type( $post ) ] );
12506 $response['response_type'] = 'live';
12507 } else {
12508 $response = json_decode( '{"response_type":"sample","pluggable_data":{"custom_description": "custom message", "ID": 1, "post_author": "1", "post_date": "2023-05-31 13:26:24", "post_date_gmt": "2023-05-31 13:26:24", "post_content": "", "post_title": "Test", "post_excerpt": "", "post_status": "publish", "comment_status": "open", "ping_status": "open", "post_password": "", "post_name": "test", "to_ping": "", "pinged": "", "post_modified": "2023-08-17 09:15:56", "post_modified_gmt": "2023-08-17 09:15:56", "post_content_filtered": "", "post_parent": 0, "guid": "https:\/\/example.com\/?p=1", "menu_order": 0, "post_type": "post", "post_mime_type": "", "comment_count": "2", "filter": "raw"}}', true );
12509 }
12510
12511 return $response;
12512 }
12513
12514 /**
12515 * Search Last Updated User Field Data ACF.
12516 *
12517 * @param array $data data.
12518 * @return array
12519 */
12520 public function search_acf_user_field_data( $data ) {
12521 global $wpdb;
12522
12523 $context = [];
12524
12525 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12526
12527 if ( -1 === $field ) {
12528 $groups_user_form = [];
12529 if ( function_exists( 'acf_get_field_groups' ) ) {
12530 $field_groups = acf_get_field_groups();
12531 }
12532 if ( ! empty( $field_groups ) ) {
12533 foreach ( $field_groups as $group ) {
12534 if ( ! empty( $group['location'] ) ) {
12535 foreach ( $group['location'] as $locations ) {
12536 foreach ( $locations as $location ) {
12537 if ( 'user_form' === $location['param'] || 'user_role' === $location['param'] || 'current_user' === $location['param'] || 'current_user_role' === $location['param'] ) {
12538 $groups_user_form[] = $group;
12539 }
12540 }
12541 }
12542 }
12543 }
12544 $field_groups = $groups_user_form;
12545 }
12546 if ( empty( $field_groups ) ) {
12547 $result = '';
12548 }
12549 $fields = [];
12550 if ( ! empty( $field_groups ) ) {
12551 foreach ( $field_groups as $group ) {
12552 if ( function_exists( 'acf_get_fields' ) ) {
12553 $group_fields = acf_get_fields( $group['key'] );
12554 }
12555 if ( ! empty( $group_fields ) ) {
12556 foreach ( $group_fields as $field ) {
12557 $fields[] = $group_fields;
12558 }
12559 }
12560 }
12561 }
12562 if ( ! empty( $fields ) ) {
12563 $random_key = array_rand( $fields );
12564 $field = $random_key;
12565 } else {
12566 $result = '';
12567 }
12568 } else {
12569 $field = $data['filter']['field_id']['value'];
12570 }
12571 $users = get_users(
12572 [
12573 'fields' => 'ID',
12574 'meta_key' => $field,
12575 ]
12576 );
12577
12578 if ( ! empty( $users ) ) {
12579 $user_random_key = array_rand( $users );
12580 $user_id = $user_random_key;
12581 if ( function_exists( 'get_field' ) ) {
12582 $result = get_field( $field, 'user_' . $users[ $user_id ] );
12583 }
12584 $response = [];
12585 if ( ! empty( $result ) ) {
12586 $context = [
12587 'field_id' => $field,
12588 $field => $result,
12589 'user' => WordPress::get_user_context( $users[ $user_id ] ),
12590 ];
12591 $response['pluggable_data'] = $context;
12592 $response['response_type'] = 'live';
12593 } else {
12594 $response = json_decode(
12595 '{
12596 "response_type": "sample",
12597 "pluggable_data": {
12598 "field_id": "gender",
12599 "user": {
12600 "wp_user_id": 114,
12601 "user_login": "test",
12602 "display_name": "test",
12603 "user_firstname": "test",
12604 "user_lastname": "test",
12605 "user_email": "test@test.com",
12606 "user_role": [ "subscriber" ]
12607 }
12608 }
12609 }',
12610 true
12611 );
12612 }
12613 } else {
12614 $response = json_decode(
12615 '{
12616 "response_type": "sample",
12617 "pluggable_data": {
12618 "field_id": "gender",
12619 "user": {
12620 "wp_user_id": 114,
12621 "user_login": "test",
12622 "display_name": "test",
12623 "user_firstname": "test",
12624 "user_lastname": "test",
12625 "user_email": "test@test.com",
12626 "user_role": [ "subscriber" ]
12627 }
12628 }
12629 }',
12630 true
12631 );
12632 }
12633
12634 return $response;
12635 }
12636
12637 /**
12638 * Search Last Updated Options Field Data ACF.
12639 *
12640 * @param array $data data.
12641 * @return array
12642 */
12643 public function search_acf_options_field_data( $data ) {
12644 global $wpdb;
12645 $context = [];
12646 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12647
12648 if ( -1 === $field ) {
12649 $groups_options_form = [];
12650 if ( function_exists( 'acf_get_field_groups' ) ) {
12651 $field_groups = acf_get_field_groups();
12652 }
12653 if ( ! empty( $field_groups ) ) {
12654 foreach ( $field_groups as $group ) {
12655 if ( ! empty( $group['location'] ) ) {
12656 foreach ( $group['location'] as $locations ) {
12657 foreach ( $locations as $location ) {
12658 if ( 'options_page' === $location['param'] ) {
12659 $groups_options_form[] = $group;
12660 }
12661 }
12662 }
12663 }
12664 }
12665 }
12666 if ( empty( $groups_options_form ) ) {
12667 $result = '';
12668 }
12669 $key_values = array_map(
12670 function ( $item ) {
12671 return $item['key'];
12672 },
12673 $groups_options_form
12674 );
12675 $unique_keys = array_unique( $key_values );
12676 $unique_array = array_intersect_key( $groups_options_form, $unique_keys );
12677 $fields = [];
12678 if ( ! empty( $unique_array ) ) {
12679 foreach ( $unique_array as $group ) {
12680 if ( function_exists( 'acf_get_fields' ) ) {
12681 $group_fields = acf_get_fields( $group['key'] );
12682 }
12683 if ( ! empty( $group_fields ) ) {
12684 foreach ( $group_fields as $field ) {
12685 $fields[] = $group_fields;
12686 }
12687 }
12688 }
12689 }
12690 if ( ! empty( $fields ) ) {
12691 $random_key = array_rand( $fields );
12692 $field = $random_key;
12693 } else {
12694 $result = '';
12695 }
12696 } else {
12697 $field = $data['filter']['field_id']['value'];
12698 }
12699 if ( function_exists( 'get_field' ) ) {
12700 $option_value = get_field( $field, 'option' );
12701 }
12702 if ( ! empty( $option_value ) ) {
12703 if ( function_exists( 'acf_get_field' ) ) {
12704 $options_fields = acf_get_field( $field );
12705 if ( function_exists( 'acf_maybe_get' ) ) {
12706 $options_page = acf_maybe_get( $options_fields, 'parent' );
12707 }
12708 }
12709 $context = [
12710 'field_id' => $field,
12711 $field => $option_value,
12712 ];
12713 $response['pluggable_data'] = $context;
12714 $response['response_type'] = 'live';
12715 } else {
12716 $response = json_decode(
12717 '{
12718 "response_type": "sample",
12719 "pluggable_data": {
12720 "field_id": "optionpage",
12721 "optionpage": "newoption"
12722 }
12723 }',
12724 true
12725 );
12726 }
12727 return $response;
12728 }
12729
12730 /**
12731 * Get WP Fusion Tags list.
12732 *
12733 * @param array $data data.
12734 *
12735 * @return array
12736 */
12737 public function search_wp_fusion_tag_list( $data ) {
12738
12739 if ( ! function_exists( 'wp_fusion' ) ) {
12740 return [];
12741 }
12742
12743 $options = [];
12744 $tags = wp_fusion()->settings->get( 'available_tags' );
12745
12746 if ( $tags ) {
12747 foreach ( $tags as $t_id => $tag ) {
12748 if ( is_array( $tag ) && isset( $tag['label'] ) ) {
12749 $options[] = [
12750 'value' => $t_id,
12751 'label' => $tag['label'],
12752 ];
12753 } else {
12754 $options[] = [
12755 'value' => $t_id,
12756 'label' => $tag,
12757 ];
12758 }
12759 }
12760 }
12761
12762 return [
12763 'options' => $options,
12764 'hasMore' => false,
12765 ];
12766 }
12767
12768 /**
12769 * Get list of events for Modern Events Calendar.
12770 *
12771 * @param array $data data.
12772 * @return array
12773 */
12774 public function search_mec_events_list( $data ) {
12775 $page = $data['page'];
12776 $limit = Utilities::get_search_page_limit();
12777 $offset = $limit * ( $page - 1 );
12778
12779 $args = [
12780 'post_type' => 'mec-events',
12781 'post_status' => [ 'publish', 'private' ],
12782 'posts_per_page' => -1,
12783 ];
12784 $loop = new WP_Query( $args );
12785 $events_count = count( $loop->posts );
12786
12787 $args = [
12788 'post_type' => 'mec-events',
12789 'posts_per_page' => $limit,
12790 'offset' => $offset,
12791 'orderby' => 'title',
12792 'order' => 'ASC',
12793 'post_status' => [ 'publish', 'private' ],
12794 ];
12795
12796 $loop = new WP_Query( $args );
12797 $events = $loop->posts;
12798
12799 $options = [];
12800 if ( ! empty( $events ) ) {
12801 foreach ( $events as $event ) {
12802 if ( isset( $event->ID ) ) {
12803 $options[] = [
12804 'label' => get_the_title( $event ),
12805 'value' => $event->ID,
12806 ];
12807 }
12808 }
12809 }
12810
12811 return [
12812 'options' => $options,
12813 'hasMore' => $events_count > $limit && $events_count > $offset,
12814 ];
12815 }
12816
12817 /**
12818 * Search tickets of MEC events.
12819 *
12820 * @param array $data data.
12821 * @return array
12822 */
12823 public function search_mec_event_tickets( $data ) {
12824 $options = [];
12825 $event_id = $data['dynamic'];
12826
12827 $event_tickets = get_post_meta( $event_id, 'mec_tickets', true );
12828
12829 if ( ! empty( $event_tickets ) && is_array( $event_tickets ) ) {
12830 foreach ( $event_tickets as $ticket_id => $event_ticket ) {
12831 if ( isset( $event_ticket['name'] ) ) {
12832 $options[] = [
12833 'label' => $event_ticket['name'],
12834 'value' => $ticket_id,
12835 ];
12836 }
12837 }
12838 }
12839
12840 return [
12841 'options' => $options,
12842 'hasMore' => false,
12843 ];
12844 }
12845
12846 /**
12847 * Get last data for trigger.
12848 *
12849 * @param array $data data.
12850 * @return array
12851 */
12852 public function search_mec_triggers_last_data( $data ) {
12853 global $wpdb;
12854
12855 $context = [];
12856 $context['response_type'] = 'sample';
12857
12858 $event = [
12859 'event_id' => 1,
12860 'title' => 'Sample Event',
12861 'description' => 'Description of the sample event.',
12862 'categories' => 'New, Sample',
12863 'start_date' => 'September 13, 2023',
12864 'start_time' => '8:00 AM',
12865 'end_date' => 'September 13, 2023',
12866 'end_time' => '11:00 AM',
12867 'location' => 'City Hall',
12868 'organizer' => 'John Doe',
12869 'cost' => '5000',
12870 'featured_image_id' => 1,
12871 'featured_image_url' => 'https://suretriggers.com/wp-content/uploads/2022/12/Screenshot_20221127_021332.png',
12872 'tickets' => [
12873 [
12874 'id' => 1,
12875 'name' => 'Silver',
12876 'description' => 'Standard seat with reasonable price.',
12877 'price' => '300',
12878 'price_label' => 'USD',
12879 'limit' => '20',
12880 ],
12881 [
12882 'id' => 2,
12883 'name' => 'Premium',
12884 'description' => 'VIP seat with high price.',
12885 'price' => '500',
12886 'price_label' => 'USD',
12887 'limit' => '10',
12888 ],
12889 ],
12890 'attendees' => [
12891 [
12892 'id' => 1,
12893 'email' => 'johndoe@test.com',
12894 'name' => 'John Doe',
12895 ],
12896 [
12897 'id' => 2,
12898 'email' => 'adamsmith@test.com',
12899 'name' => 'Adam Smith',
12900 ],
12901 ],
12902 'booking' => [
12903 'title' => 'johndoe@test.com - John Doe',
12904 'transaction_id' => 'RSH59404',
12905 'amount_payable' => '800',
12906 'price' => '800',
12907 'time' => '2023-09-07 06:40:32',
12908 'payment_gateway' => 'Manual Pay',
12909 'confirmation_status' => 'Pending',
12910 'verification_status' => 'Verified',
12911 'attendees_count' => 2,
12912 ],
12913 ];
12914
12915 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
12916
12917 $where = '';
12918
12919 if ( 'cancelled' === $term ) {
12920 $where = 'WHERE verified = -1';
12921 } elseif ( 'confirmed' === $term ) {
12922 $where = 'WHERE confirmed = 1';
12923 } elseif ( 'pending' === $term ) {
12924 $where = 'WHERE confirmed = 0';
12925 }
12926
12927 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
12928
12929 if ( -1 !== $event_id ) {
12930 if ( ! empty( $where ) ) {
12931 $where .= ' AND event_id = ' . $event_id;
12932 } else {
12933 $where = 'WHERE event_id = ' . $event_id;
12934 }
12935 }
12936
12937 $event_data = $wpdb->get_row( "SELECT booking_id FROM {$wpdb->prefix}mec_bookings $where ORDER BY id DESC LIMIT 1" ); // @phpcs:ignore
12938
12939 if ( ! empty( $event_data ) ) {
12940 $event = ModernEventsCalendar::get_event_context( (int) $event_data->booking_id );
12941 $context['response_type'] = 'live';
12942 }
12943
12944 $context['pluggable_data'] = $event;
12945
12946 return $context;
12947 }
12948
12949 /**
12950 * Get form list Contact Form 7.
12951 *
12952 * @param array $data data.
12953 *
12954 * @return array
12955 */
12956 public function search_contact_form7_list( $data ) {
12957
12958 $page = $data['page'];
12959 $limit = Utilities::get_search_page_limit();
12960 $offset = $limit * ( $page - 1 );
12961
12962 $posts = get_posts(
12963 [
12964 'post_type' => 'wpcf7_contact_form',
12965 'posts_per_page' => $limit,
12966 'offset' => $offset,
12967 ]
12968 );
12969
12970 $all_posts = get_posts(
12971 [
12972 'post_type' => 'wpcf7_contact_form',
12973 'posts_per_page' => -1,
12974 ]
12975 );
12976
12977 $posts_count = count( $all_posts );
12978
12979 $options = [];
12980
12981 if ( ! empty( $posts ) ) {
12982 foreach ( $posts as $post ) {
12983 $options[] = [
12984 'label' => get_the_title( $post->ID ),
12985 'value' => $post->ID,
12986 ];
12987 }
12988 }
12989
12990 return [
12991 'options' => $options,
12992 'hasMore' => $posts_count > $limit && $posts_count > $offset,
12993 ];
12994 }
12995
12996 /**
12997 * Get Thrive Leads form list
12998 *
12999 * @param array $data data.
13000 *
13001 * @return array
13002 */
13003 public function search_thrive_leads_forms_list( $data ) {
13004 $options = [];
13005
13006 $lg_ids = get_posts(
13007 [
13008 'post_type' => '_tcb_form_settings',
13009 'fields' => 'id=>parent',
13010 'posts_per_page' => -1,
13011 'post_status' => 'any',
13012 ]
13013 );
13014
13015 if ( function_exists( 'tve_leads_get_form_variations' ) ) {
13016 foreach ( $lg_ids as $lg_id => $lg_parent ) {
13017 $variations = tve_leads_get_form_variations( $lg_parent );
13018 foreach ( $variations as $variation ) {
13019 $options[] = [
13020 'label' => $variation['post_title'],
13021 'value' => $lg_parent,
13022 ];
13023 }
13024 }
13025 }
13026
13027 return [
13028 'options' => array_unique( $options ),
13029 'hasMore' => false,
13030 ];
13031 }
13032
13033 /**
13034 * Get list for Woocommerce Subscriptions
13035 *
13036 * @param array $data data.
13037 *
13038 * @return array
13039 */
13040 public function search_wc_subscription_variation_products( $data ) {
13041 $options = [];
13042
13043 global $wpdb;
13044
13045 if ( ! function_exists( 'wc_get_product' ) ) {
13046 return [];
13047 }
13048 $subscriptions = $wpdb->get_results(
13049 $wpdb->prepare(
13050 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
13051 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
13052 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
13053 AND posts.post_type = %s
13054 AND posts.post_status = %s
13055 UNION ALL
13056 SELECT ID, post_title FROM $wpdb->posts
13057 WHERE post_type = %s
13058 AND post_status = %s
13059 ORDER BY post_title",
13060 'product',
13061 'publish',
13062 'shop_subscription',
13063 'publish'
13064 )
13065 );
13066
13067 if ( $subscriptions ) {
13068 foreach ( $subscriptions as $product ) {
13069 $options[] = [
13070 'label' => $product->post_title . ' (#' . $product->ID . ')',
13071 'value' => (int) $product->ID,
13072 ];
13073 $product_s = wc_get_product( $product->ID );
13074 /**
13075 *
13076 * Ignore line
13077 *
13078 * @phpstan-ignore-next-line
13079 */
13080 if ( 'variable-subscription' == $product_s->product_type ) {
13081 $args = [
13082 'post_type' => 'product_variation',
13083 'post_parent' => $product->ID,
13084 'posts_per_page' => -1,
13085 'orderby' => 'ID',
13086 'order' => 'ASC',
13087 'post_status' => 'publish',
13088 ];
13089
13090 $variations = get_posts( $args );
13091
13092 foreach ( $variations as $var ) {
13093 $options[] = [
13094 'label' => $var->post_title . ' (#' . $var->ID . ')',
13095 'value' => $var->ID,
13096 ];
13097 }
13098 }
13099 }
13100 }
13101
13102 return [
13103 'options' => $options,
13104 'hasMore' => false,
13105 ];
13106 }
13107
13108 /**
13109 * Get WS Forms form list
13110 *
13111 * @param array $data data.
13112 *
13113 * @return array
13114 */
13115 public function search_ws_forms_list( $data ) {
13116 $options = [];
13117 global $wpdb;
13118
13119 $forms = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wsf_form", 'ARRAY_A' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
13120
13121 foreach ( $forms as $form ) {
13122 $options[] = [
13123 'label' => $form['label'],
13124 'value' => $form['id'],
13125 ];
13126 }
13127
13128 return [
13129 'options' => $options,
13130 'hasMore' => false,
13131 ];
13132 }
13133
13134 /**
13135 * Get Learndash Achievement list
13136 *
13137 * @param array $data data.
13138 *
13139 * @return array
13140 */
13141 public function search_ld_achievements_list( $data ) {
13142 $page = $data['page'];
13143 $limit = Utilities::get_search_page_limit();
13144 $offset = $limit * ( $page - 1 );
13145
13146 $posts = get_posts(
13147 [
13148 'post_type' => 'ld-achievement',
13149 'posts_per_page' => $limit,
13150 'offset' => $offset,
13151 'post_status' => [ 'publish' ],
13152 ]
13153 );
13154
13155 $posts_count = wp_count_posts( 'ld-achievement' )->publish;
13156
13157 $options = [];
13158
13159 if ( ! empty( $posts ) ) {
13160 foreach ( $posts as $post ) {
13161 $options[] = [
13162 'label' => get_the_title( $post->ID ),
13163 'value' => $post->ID,
13164 ];
13165 }
13166 }
13167
13168 return [
13169 'options' => $options,
13170 'hasMore' => $posts_count > $limit && $posts_count > $offset,
13171 ];
13172 }
13173
13174 /**
13175 * Get Advanced Ads list
13176 *
13177 * @param array $data data.
13178 *
13179 * @return array
13180 */
13181 public function search_ads_list( $data ) {
13182 $page = $data['page'];
13183 $limit = Utilities::get_search_page_limit();
13184 $offset = $limit * ( $page - 1 );
13185
13186 $posts = get_posts(
13187 [
13188 'post_type' => 'advanced_ads',
13189 'posts_per_page' => $limit,
13190 'offset' => $offset,
13191 'post_status' => [ 'publish', 'draft' ],
13192 ]
13193 );
13194
13195 $posts_count = wp_count_posts( 'advanced_ads' )->publish;
13196
13197 $options = [];
13198
13199 if ( ! empty( $posts ) ) {
13200 foreach ( $posts as $post ) {
13201 $options[] = [
13202 'label' => get_the_title( $post->ID ),
13203 'value' => $post->ID,
13204 ];
13205 }
13206 }
13207
13208 return [
13209 'options' => $options,
13210 'hasMore' => $posts_count > $limit && $posts_count > $offset,
13211 ];
13212 }
13213
13214 /**
13215 * Advanced Ads pluggable data.
13216 *
13217 * @param array $data data.
13218 * @return array
13219 */
13220 public function search_ad_last_data( $data ) {
13221 $context = [];
13222 $args = [
13223 'post_type' => 'advanced_ads',
13224 'posts_per_page' => 1,
13225 'orderby' => 'modified',
13226 'order' => 'DESC',
13227 ];
13228
13229 if ( isset( $data['filter']['ad_status']['value'] ) ) {
13230 $post_status = $data['filter']['ad_status']['value'];
13231 $args['post_status'] = $post_status;
13232 }
13233
13234 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
13235 $post_status = $data['filter']['ad_new_status']['value'];
13236 $args['post_status'] = $post_status;
13237 }
13238
13239 if ( isset( $data['filter']['ad_id']['value'] ) ) {
13240 $post_id = $data['filter']['ad_id']['value'];
13241 if ( -1 != $post_id ) {
13242 if ( $post_id > 0 ) {
13243 $args['p'] = $post_id;
13244 }
13245 }
13246 }
13247
13248 $posts = get_posts( $args );
13249 if ( ! empty( $posts ) ) {
13250 $context['pluggable_data'] = $posts[0];
13251 $context['pluggable_data'] = WordPress::get_post_context( $posts[0]->ID );
13252 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
13253 $context['pluggable_data']['ad_new_status'] = $posts[0]->post_status;
13254 }
13255 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
13256 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
13257 }
13258 $context['pluggable_data']['ad_id'] = $posts[0]->ID;
13259 if ( isset( $data['filter']['ad_status']['value'] ) ) {
13260 $context['pluggable_data']['ad_status'] = $posts[0]->post_status;
13261 }
13262 $context['response_type'] = 'live';
13263 } else {
13264 $context['pluggable_data'] = [
13265 'ID' => 1,
13266 'post' => 1,
13267 'post_author' => 1,
13268 'post_date' => '2022-11-18 12:18:14',
13269 'post_date_gmt' => '2022-11-18 12:18:14',
13270 'post_content' => 'Ad Post Content',
13271 'post_title' => 'Ad Post',
13272 'post_excerpt' => '',
13273 'post_status' => 'draft',
13274 'comment_status' => 'open',
13275 'ping_status' => 'open',
13276 'post_password' => '',
13277 'post_name' => 'ad-post',
13278 'to_ping' => '',
13279 'pinged' => '',
13280 'post_modified' => '2022-11-18 12:18:14',
13281 'post_modified_gmt' => '2022-11-18 12:18:14',
13282 'post_content_filtered' => '',
13283 'post_parent' => 0,
13284 'guid' => 'https://example.com/ad-post/',
13285 'menu_order' => 0,
13286 'post_type' => 'advanced_ads',
13287 'post_mime_type' => '',
13288 'comment_count' => 0,
13289 'filter' => 'raw',
13290 ];
13291 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
13292 $context['pluggable_data']['ad_new_status'] = $data['filter']['ad_new_status']['value'];
13293 }
13294 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
13295 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
13296 }
13297 $context['pluggable_data']['ad_id'] = 1;
13298 if ( isset( $data['filter']['ad_status']['value'] ) ) {
13299 $context['pluggable_data']['ad_status'] = $data['filter']['ad_status']['value'];
13300 }
13301 $context['response_type'] = 'sample';
13302 }
13303
13304 return $context;
13305 }
13306
13307 /**
13308 * Get Newsletter lists
13309 *
13310 * @param array $data data.
13311 *
13312 * @return array
13313 */
13314 public function search_newsletter_lists( $data ) {
13315
13316 $options = [];
13317 if ( class_exists( '\Newsletter' ) ) {
13318
13319 $lists = \Newsletter::instance()->get_lists();
13320
13321 if ( ! empty( $lists ) ) {
13322 foreach ( $lists as $list ) {
13323 $options[] = [
13324 'label' => $list->name,
13325 'value' => 'list_' . $list->id,
13326 ];
13327 }
13328 }
13329 }
13330
13331 return [
13332 'options' => $options,
13333 'hasMore' => false,
13334 ];
13335 }
13336
13337 /**
13338 * Newsletter pluggable data.
13339 *
13340 * @param array $data data.
13341 * @return array
13342 */
13343 public function search_newsletter_last_data( $data ) {
13344 $context = [];
13345 global $wpdb;
13346
13347 $list = $data['filter']['list_id']['value'];
13348
13349 if ( -1 == $list ) {
13350 $log = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . "newsletter where status='C' ORDER BY id DESC LIMIT 1" );
13351 } else {
13352 $num = $list;
13353 $location = 1;
13354 $sql = 'SELECT * FROM ' . $wpdb->prefix . "newsletter WHERE $num = %d AND status = 'C' ORDER BY id DESC LIMIT 1";
13355 $log = $wpdb->get_results( $wpdb->prepare( $sql, $location ), ARRAY_A );// @phpcs:ignore
13356 }
13357
13358 if ( ! empty( $log ) ) {
13359 $lists_arr = get_option( 'newsletter_lists' );
13360 if ( -1 == $list ) {
13361 foreach ( $log[0] as $key => $val ) {
13362 if ( defined( 'NEWSLETTER_LIST_MAX' ) ) {
13363 for ( $i = 1; $i <= NEWSLETTER_LIST_MAX; $i++ ) {
13364 $list_key = "list_$i";
13365
13366 if ( $key == $list_key ) {
13367 if ( 1 == $val ) {
13368 $context['pluggable_data']['list_id'] = $key;
13369 if ( is_array( $lists_arr ) ) {
13370 if ( isset( $lists_arr[ $key ] ) ) {
13371 $list_name = $lists_arr[ $key ];
13372 $context['pluggable_data']['list_name'] = $list_name;
13373 }
13374 }
13375 continue;
13376 }
13377 }
13378 }
13379 }
13380 if ( 'email' == $key ) {
13381 $context['pluggable_data']['email'] = $val;
13382 }
13383 }
13384 } else {
13385 $context['pluggable_data']['list_id'] = $list;
13386 $context['pluggable_data']['email'] = $log[0]['email'];
13387 if ( is_array( $lists_arr ) ) {
13388 if ( isset( $lists_arr[ $list ] ) ) {
13389 $list_name = $lists_arr[ $list ];
13390 $context['pluggable_data']['list_name'] = $list_name;
13391 }
13392 }
13393 }
13394 $context['response_type'] = 'live';
13395 } else {
13396 $context = json_decode( '{"response_type":"sample","pluggable_data":{"list_id": "list_1","email": "johnd@mailinator.com","list_name": "Contact List"}}', true );
13397 }
13398
13399 return $context;
13400 }
13401
13402 /**
13403 * Get wpForo Forum list
13404 *
13405 * @param array $data data.
13406 *
13407 * @return array
13408 */
13409 public function search_wp_forum_list( $data ) {
13410
13411 if ( ! function_exists( 'WPF' ) ) {
13412 return [];
13413 }
13414
13415 $forums = WPF()->forum->get_forums( [ 'type' => 'forum' ] );
13416
13417 $options = [];
13418
13419 if ( ! empty( $forums ) ) {
13420 foreach ( $forums as $forum ) {
13421 $options[] = [
13422 'label' => $forum['title'],
13423 'value' => $forum['forumid'],
13424 ];
13425 }
13426 }
13427
13428 return [
13429 'options' => $options,
13430 'hasMore' => false,
13431 ];
13432 }
13433
13434 /**
13435 * Get wpForo Topic list
13436 *
13437 * @param array $data data.
13438 *
13439 * @return array
13440 */
13441 public function search_wp_topic_list( $data ) {
13442
13443 if ( ! function_exists( 'WPF' ) ) {
13444 return [];
13445 }
13446
13447 $forum_id = $data['dynamic'];
13448
13449 $topics = WPF()->topic->get_topics( [ 'forumid' => $forum_id ] );
13450
13451 $options = [];
13452
13453 if ( ! empty( $topics ) ) {
13454 foreach ( $topics as $topic ) {
13455 $options[] = [
13456 'label' => $topic['title'],
13457 'value' => $topic['topicid'],
13458 ];
13459 }
13460 }
13461
13462 return [
13463 'options' => $options,
13464 'hasMore' => false,
13465 ];
13466 }
13467
13468 /**
13469 * Get wpForo Groups list
13470 *
13471 * @param array $data data.
13472 *
13473 * @return array
13474 */
13475 public function search_wp_user_groups_list( $data ) {
13476
13477 if ( ! function_exists( 'WPF' ) ) {
13478 return [];
13479 }
13480
13481 $usergroups = WPF()->usergroup->get_usergroups();
13482
13483 $options = [];
13484
13485 if ( ! empty( $usergroups ) ) {
13486 foreach ( $usergroups as $group ) {
13487 $options[] = [
13488 'label' => $group['name'],
13489 'value' => intval( $group['groupid'] ),
13490 ];
13491 }
13492 }
13493
13494 return [
13495 'options' => $options,
13496 'hasMore' => false,
13497 ];
13498 }
13499
13500 /**
13501 * Get wpForo Reputation list
13502 *
13503 * @param array $data data.
13504 *
13505 * @return array
13506 */
13507 public function search_wp_foro_reputation_list( $data ) {
13508
13509 if ( ! function_exists( 'WPF' ) ) {
13510 return [];
13511 }
13512
13513 $levels = WPF()->member->levels();
13514 $options = [];
13515
13516 if ( ! empty( $levels ) ) {
13517 foreach ( $levels as $level ) {
13518 $options[] = [
13519 'label' => esc_attr__( 'Level', 'suretriggers' ) . ' ' . $level . ' - ' . WPF()->member->rating( $level, 'title' ),
13520 'value' => strval( $level ),
13521 ];
13522 }
13523 }
13524
13525 return [
13526 'options' => $options,
13527 'hasMore' => false,
13528 ];
13529 }
13530
13531 /**
13532 * WPForo new topic pluggable data.
13533 *
13534 * @param array $data data.
13535 * @return array
13536 */
13537 public function search_pluggables_wpforo_topic_last_data( $data ) {
13538 $context = [];
13539 global $wpdb;
13540
13541 $forum_id = $data['filter']['forum_id']['value'];
13542
13543 if ( -1 == $forum_id ) {
13544 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'wpforo_topics WHERE closed = 0 ORDER BY topicid DESC LIMIT 1', ARRAY_A );
13545 } else {
13546 $forum = $forum_id;
13547 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_topics WHERE forumid = %d AND closed = 0 ORDER BY topicid DESC LIMIT 1';
13548 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum ), ARRAY_A );// @phpcs:ignore
13549 }
13550
13551 if ( ! empty( $results ) ) {
13552 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
13553 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
13554
13555 if ( function_exists( 'WPF' ) ) {
13556 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
13557 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
13558 }
13559 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
13560 $context['response_type'] = 'live';
13561 } else {
13562 $context = json_decode( '{"response_type":"sample","pluggable_data":{"forum_id": "2","topic_id": "1","forum": {"forumid": "2","title": "Main Forum","slug": "main-forum","description": "This is a simple parent forum","parentid": "1","icon": "fas fa-comments","cover": 0,"cover_height": "150","last_topicid": "1","last_postid": "2","last_userid": "4","last_post_date": "2023-09-19 11:40:19","topics": "1","posts": "2","permissions": "a:5:{i:1;s:4:\"full\";i:2;s:9:\"moderator\";i:3;s:8:\"standard\";i:4;s:9:\"read_only\";i:5;s:8:\"standard\";}","meta_key": "","meta_desc": "","status": "1","is_cat": "0","layout": "4","order": "0","color": "#888888","url": "https:\/\/example.com\/community\/main-forum\/","cover_url": ""},"topic": {"topicid": "1","forumid": "2","first_postid": "1","userid": "4","title": "New Forum topic title","slug": "new-forum-topic-title","created": "2023-09-19 11:39:01","modified": "2023-09-19 11:40:19","last_post": "2","posts": "2","votes": "0","answers": "0","views": "1","meta_key": "","meta_desc": "","type": "0","solved": "0","closed": "0","has_attach": "0","private": "0","status": "0","name": "","email": "","prefix": "","tags": "","url": "https:\/\/example.com\/community\/main-forum\/new-forum-topic-title\/","full_url": "https:\/\/example.com\/community\/main-forum\/new-forum-topic-title\/","short_url": "https:\/\/example.com\/community\/topicid\/1\/"},"user": {"wp_user_id": 4,"user_login": "john@d.com","display_name": "john@d.com","user_firstname": "john","user_lastname": "d","user_email": "john@d.com","user_role": ["customer"]}}}', true );// @phpcs:ignore
13563 }
13564
13565 return $context;
13566 }
13567
13568 /**
13569 * WpForo Topic Reply pluggable data.
13570 *
13571 * @param array $data data.
13572 * @return array
13573 */
13574 public function search_pluggables_wpforo_topic_reply_last_data( $data ) {
13575 $context = [];
13576 global $wpdb;
13577
13578 $forum_id = $data['filter']['forum_id']['value'];
13579 $topic_id = $data['filter']['topic_id']['value'];
13580
13581 if ( -1 == $forum_id && -1 != $topic_id ) {
13582 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE topicid = %d ORDER BY postid DESC LIMIT 1';
13583 $results = $wpdb->get_results( $wpdb->prepare( $sql, $topic_id ), ARRAY_A );// @phpcs:ignore
13584 } elseif ( -1 != $forum_id && -1 == $topic_id ) {
13585 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d ORDER BY postid DESC LIMIT 1';
13586 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id ), ARRAY_A );// @phpcs:ignore
13587 } elseif ( -1 == $forum_id && -1 == $topic_id ) {
13588 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts ORDER BY postid DESC LIMIT 1';
13589 $results = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );// @phpcs:ignore
13590 } else {
13591 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d AND topicid = %d ORDER BY postid DESC LIMIT 1';
13592 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id, $topic_id ), ARRAY_A );// @phpcs:ignore
13593 }
13594
13595 if ( ! empty( $results ) ) {
13596 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
13597 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
13598 if ( function_exists( 'WPF' ) ) {
13599 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
13600 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
13601 $context['pluggable_data']['reply'] = WPF()->post->get_post( $results[0]['postid'] );
13602 }
13603 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
13604 $context['response_type'] = 'live';
13605 } else {
13606 $context = json_decode( '{"response_type":"sample","pluggable_data":{"forum_id": "2","topic_id": "1","forum": {"forumid": "2","title": "Main Forum","slug": "main-forum","description": "This is a simple parent forum","parentid": "1","icon": "fas fa-comments","cover": 0,"cover_height": "150","last_topicid": "1","last_postid": "2","last_userid": "4","last_post_date": "2023-09-19 11:40:19","topics": "1","posts": "2","permissions": "a:5:{i:1;s:4:\"full\";i:2;s:9:\"moderator\";i:3;s:8:\"standard\";i:4;s:9:\"read_only\";i:5;s:8:\"standard\";}","meta_key": "","meta_desc": "","status": "1","is_cat": "0","layout": "4","order": "0","color": "#888888","url": "https:\/\/example.com\/community\/main-forum\/","cover_url": ""},"topic": {"topicid": "1","forumid": "2","first_postid": "1","userid": "4","title": "New Forum topic title","slug": "new-forum-topic-title","created": "2023-09-19 11:39:01","modified": "2023-09-19 11:40:19","last_post": "2","posts": "2","votes": "0","answers": "0","views": "1","meta_key": "","meta_desc": "","type": "0","solved": "0","closed": "0","has_attach": "0","private": "0","status": "0","name": "","email": "","prefix": "","tags": "","url": "https:\/\/example.com\/community\/main-forum\/new-forum-topic-title\/","full_url": "https:\/\/example.com\/community\/main-forum\/new-forum-topic-title\/","short_url": "https:\/\/example.com\/community\/topicid\/1\/"},"reply_url": "https:\/\/example.com\/community\/main-forum\/new-forum-topic-title\/#post-2","reply": {"postid": "2","parentid": "0","forumid": "2","topicid": "1","userid": 4,"title": "RE: New Forum topic title","body": "<p>new reply<\/p>","created": "2023-09-19 11:40:19","modified": "2023-09-19 11:40:19","likes": "0","votes": "0","is_answer": "0","is_first_post": "0","status": "0","name": "","email": "","private": "0","root": "-1","url": "https:\/\/example.com\/community\/main-forum\/new-forum-topic-title\/#post-2","full_url": "https:\/\/example.com\/community\/main-forum\/new-forum-topic-title\/#post-2","short_url": "https:\/\/example.com\/community\/postid\/2\/"},"user": {"wp_user_id": 4,"user_login": "john@d.com","display_name": "john@d.com","user_firstname": "john","user_lastname": "d","user_email": "john@d.com","user_role": ["customer"]}}}', true );// @phpcs:ignore
13607 }
13608
13609 return $context;
13610 }
13611
13612 /**
13613 * Get RafflePress Giveaways list
13614 *
13615 * @param array $data data.
13616 *
13617 * @return array
13618 */
13619 public function search_rp_giveaways_list( $data ) {
13620
13621 global $wpdb;
13622 $options = [];
13623
13624 $giveaways = $wpdb->get_results( "SELECT id,name FROM {$wpdb->prefix}rafflepress_giveaways WHERE deleted_at is null ORDER BY name ASC", ARRAY_A );
13625 foreach ( $giveaways as $giveaway ) {
13626 $options[] = [
13627 'label' => $giveaway['name'],
13628 'value' => $giveaway['id'],
13629 ];
13630 }
13631
13632 return [
13633 'options' => $options,
13634 'hasMore' => false,
13635 ];
13636 }
13637
13638 /**
13639 * Get RafflePress Giveaway Actions list
13640 *
13641 * @param array $data data.
13642 *
13643 * @return array
13644 */
13645 public function search_rp_giveaway_actions_list( $data ) {
13646
13647 global $wpdb;
13648 $options = [];
13649
13650 $giveaway = $wpdb->get_row( $wpdb->prepare( "SELECT settings FROM {$wpdb->prefix}rafflepress_giveaways WHERE id=%d", $data['dynamic'] ), ARRAY_A );
13651
13652 if ( is_array( $giveaway ) && isset( $giveaway['settings'] ) ) {
13653 $settings = json_decode( $giveaway['settings'], true );
13654
13655 if ( is_array( $settings ) && isset( $settings['entry_options'] ) ) {
13656 foreach ( $settings['entry_options'] as $action ) {
13657 $options[] = [
13658 'label' => $action['name'],
13659 'value' => $action['id'],
13660 ];
13661 }
13662 }
13663 }
13664
13665 return [
13666 'options' => $options,
13667 'hasMore' => false,
13668 ];
13669 }
13670
13671 /**
13672 * Get last data for trigger.
13673 *
13674 * @param array $data data.
13675 * @return array
13676 */
13677 public function search_raffle_press_triggers_last_data( $data ) {
13678
13679 global $wpdb;
13680
13681 $context = [];
13682 $context['response_type'] = 'sample';
13683
13684 $pluggable_data = [
13685 'giveaway_id' => 1,
13686 'giveaway_title' => 'Sample Giveaway (ID#1)',
13687 'giveaway_start_date' => 'September 20, 2023',
13688 'giveaway_end_date' => 'October 6, 2023',
13689 'giveaway_entries' => 9,
13690 'giveaway_user_count' => 3,
13691 'giveaway_status' => 'Active',
13692 'contestant_name' => 'John Doe',
13693 'contestant_email' => 'john_doe@gmail.com',
13694 'contestant_email_verified' => 'Yes',
13695 'action_id' => '0jnex',
13696 'action_name' => 'Visit us on Instagram',
13697 ];
13698
13699 $giveaway_id = isset( $data['filter']['giveaway_id'] ) ? $data['filter']['giveaway_id']['value'] : null;
13700 $action_id = isset( $data['filter']['action_id'] ) ? $data['filter']['action_id']['value'] : null;
13701
13702 $query = "SELECT contestant_id, giveaway_id, action_id, meta FROM {$wpdb->prefix}rafflepress_entries";
13703
13704 if ( $giveaway_id && -1 != $giveaway_id ) {
13705 $query .= ' WHERE giveaway_id = ' . $giveaway_id;
13706
13707 if ( $action_id ) {
13708 $query .= " AND action_id = '" . $action_id . "'";
13709 }
13710 }
13711
13712 $query .= ' ORDER BY created_at DESC LIMIT 1';
13713
13714 $giveaway_data = $wpdb->get_row( $query, ARRAY_A ); // @phpcs:ignore
13715
13716 if ( ! empty( $giveaway_data ) ) {
13717 $pluggable_data = array_merge(
13718 RafflePress::get_giveaway_context( $giveaway_data['giveaway_id'] ),
13719 RafflePress::get_contestant_context( $giveaway_data['contestant_id'] )
13720 );
13721
13722 $pluggable_data['performed_action_id'] = isset( $giveaway_data['action_id'] ) ? $giveaway_data['action_id'] : 0;
13723
13724 $giveaway_meta = isset( $giveaway_data['meta'] ) ? json_decode( $giveaway_data['meta'], true ) : [];
13725 $pluggable_data['performed_action_name'] = is_array( $giveaway_meta ) && isset( $giveaway_meta['action'] ) ? $giveaway_meta['action'] : '';
13726
13727 $context['response_type'] = 'live';
13728 }
13729
13730 $context['pluggable_data'] = $pluggable_data;
13731
13732 return $context;
13733 }
13734
13735 /**
13736 * Get last data for trigger
13737 *
13738 * @param array $data data.
13739 * @return array
13740 */
13741 public function search_woo_commerce_shipstation_triggers_last_data( $data ) {
13742 $context = [];
13743 $context['response_type'] = 'sample';
13744 $context['pluggable_data'] = [];
13745
13746 $order_sample_data = json_decode( '{"product_id": 47,"id": 49,"parent_id": 0,"status": "completed","currency": "USD","version": "8.1.1","prices_include_tax": false,"date_created": {"date": "2023-09-23 10:28:00.000000","timezone_type": 1,"timezone": "+00:00"},"date_modified": {"date": "2023-09-23 10:29:55.000000","timezone_type": 1,"timezone": "+00:00"},"discount_total": "0","discount_tax": "0", "shipping_total": "0","shipping_tax": "0","cart_tax": "0","total": "1.00","total_tax": "0","customer_id": 1,"order_key": "wc_order_64IaGkeQKRXdm","billing": {"first_name": "john","last_name": "d","company": "","address_1": "123 Main Street","address_2": "","city": "London","state": "Greater London","postcode": "SW1A 1AA","country": "GB","email": "johnd@d.com","phone": "9878988766"},"shipping": {"first_name": "","last_name": "","company": "","address_1": "","address_2": "","city": "","state": "","postcode": "","country": "","phone": ""},"payment_method": "cod","payment_method_title": "Cash on delivery","transaction_id": "","customer_ip_address": "182.184.87.226","customer_user_agent": "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/116.0.0.0 Safari\/537.36","created_via": "checkout","customer_note": "", "date_completed": { "date": "2023-09-23 10:29:55.000000", "timezone_type": 1, "timezone": "+00:00" }, "date_paid": { "date": "2023-09-23 10:29:55.000000", "timezone_type": 1, "timezone": "+00:00" }, "cart_hash": "ac073abcdc9a52025211d0fad52cfa46", "order_stock_reduced": true, "download_permissions_granted": true, "new_order_email_sent": true, "recorded_sales": true, "recorded_coupon_usage_counts": true, "number": "49", "meta_data": [{ "id": 1206, "key":"is_vat_exempt", "value": "no"},{"id": 1207,"key": "weglot_language","value": "en"},{"id": 1208,"key": "_shipstation_exported","value": "yes"},{"id": 1209,"key": "_shipstation_shipped_item_count","value": "1"}],"line_items": {"id": "25","order_id": "49","name": "New Product","product_id": "47","variation_id": "0","quantity": "1","tax_class": "","subtotal": "1","subtotal_tax": "0","total": "1","total_tax": "0","taxes": "","meta_data": []},"tax_lines": [],"shipping_lines": [],"fee_lines": [],"coupon_lines": [],"coupons": [],"products": [{"id": 25,"order_id": 49,"name": "New Product","product_id": 47,"variation_id": 0,"quantity": 1,"tax_class": "","subtotal": "1","subtotal_tax": "0","total": "1","total_tax": "0","taxes": {"total": [],"subtotal": []},"meta_data": []}],"quantity": "1","wp_user_id": 1,"user_login": "johnd","display_name": "johnd","user_firstname": "john","user_lastname": "d","user_email": "johnd@d.com","user_role": ["administrator"],"shipping_tracking_number": "","shipping_carrier": "","ship_date": "",}', true ); //phpcs:ignore
13747
13748 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
13749 $condition = $data['filter']['condition_compare']['value'];
13750 $price = $data['filter']['price']['value'];
13751 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
13752
13753 if ( 'order_shipped' === $term ) {
13754 $orders = wc_get_orders(
13755 [
13756 'numberposts' => 1,
13757 'orderby' => 'date',
13758 'order' => 'DESC',
13759 'status' => 'completed',
13760 'meta_query' => [
13761 [
13762 'key' => '_shipstation_shipped_item_count',
13763 'compare' => 'EXISTS',
13764 ],
13765 ],
13766 ]
13767 );
13768 if ( count( $orders ) > 0 ) {
13769 $order_id = $orders[0]->get_id();
13770 $order = wc_get_order( $order_id );
13771 $user_id = $order->get_customer_id();
13772 $product_ids = [];
13773 if ( $order ) {
13774 $items = $order->get_items();
13775 foreach ( $items as $item ) {
13776 if ( method_exists( $item, 'get_product_id' ) ) {
13777 $product_ids[] = $item->get_product_id();
13778 }
13779 }
13780
13781 foreach ( $product_ids as $product_id ) {
13782 $context['product_id'] = $product_id;
13783 }
13784 }
13785 $context = array_merge(
13786 WooCommerce::get_order_context( $order_id ),
13787 WordPress::get_user_context( $user_id )
13788 );
13789 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
13790 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
13791 /**
13792 *
13793 * Ignore line
13794 *
13795 * @phpstan-ignore-next-line
13796 */
13797 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
13798 /**
13799 *
13800 * Ignore line
13801 *
13802 * @phpstan-ignore-next-line
13803 */
13804 $date = date_i18n( get_option( 'date_format' ), $timestamp );
13805 $context['ship_date'] = $date;
13806 $context['response_type'] = 'live';
13807 }
13808
13809 $context['pluggable_data'] = $context;
13810 } elseif ( 'specific_product_order_shipped' === $term ) {
13811 if ( -1 != $product_id ) {
13812 $product_ids = [ $product_id ];
13813 $orders = wc_get_orders(
13814 [
13815 'numberposts' => 1,
13816 'orderby' => 'date',
13817 'order' => 'DESC',
13818 'status' => 'completed',
13819 'meta_query' => [
13820 [
13821 'key' => '_shipstation_shipped_item_count',
13822 'compare' => 'EXISTS',
13823 ],
13824 [
13825 'key' => '_product_id',
13826 'value' => $product_ids,
13827 'compare' => 'IN',
13828 ],
13829 ],
13830 ]
13831 );
13832 } else {
13833 $orders = wc_get_orders(
13834 [
13835 'numberposts' => 1,
13836 'orderby' => 'date',
13837 'order' => 'DESC',
13838 'status' => 'completed',
13839 'meta_query' => [
13840 [
13841 'key' => '_shipstation_shipped_item_count',
13842 'compare' => 'EXISTS',
13843 ],
13844 ],
13845 ]
13846 );
13847 }
13848 if ( count( $orders ) > 0 ) {
13849 $order_id = $orders[0]->get_id();
13850 $order = wc_get_order( $order_id );
13851 $user_id = $order->get_customer_id();
13852 $productids = [];
13853 $items = $order->get_items();
13854 foreach ( $items as $item ) {
13855 if ( method_exists( $item, 'get_product_id' ) ) {
13856 $productids[] = $item->get_product_id();
13857 }
13858 }
13859 $context = array_merge(
13860 WooCommerce::get_order_context( $order_id ),
13861 WordPress::get_user_context( $user_id )
13862 );
13863
13864 foreach ( $productids as $product_id ) {
13865 $context['product_id'] = $product_id;
13866 }
13867
13868 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
13869 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
13870 /**
13871 *
13872 * Ignore line
13873 *
13874 * @phpstan-ignore-next-line
13875 */
13876 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
13877 /**
13878 *
13879 * Ignore line
13880 *
13881 * @phpstan-ignore-next-line
13882 */
13883 $date = date_i18n( get_option( 'date_format' ), $timestamp );
13884 $context['ship_date'] = $date;
13885 $context['response_type'] = 'live';
13886 }
13887
13888 $context['pluggable_data'] = $context;
13889 } elseif ( 'specific_amount_order_shipped' === $term ) {
13890 $orders = wc_get_orders(
13891 [
13892 'numberposts' => 1,
13893 'orderby' => 'date',
13894 'order' => 'DESC',
13895 'status' => 'completed',
13896 'meta_query' => [
13897 [
13898 'key' => '_shipstation_shipped_item_count',
13899 'compare' => 'EXISTS',
13900 ],
13901 [
13902 'key' => '_order_total',
13903 'value' => $price,
13904 'compare' => $condition,
13905 ],
13906 ],
13907 ]
13908 );
13909 if ( count( $orders ) > 0 ) {
13910 $order_id = $orders[0]->get_id();
13911 $order = wc_get_order( $order_id );
13912 $user_id = $order->get_customer_id();
13913 $product_ids = [];
13914 $items = $order->get_items();
13915 foreach ( $items as $item ) {
13916 if ( method_exists( $item, 'get_product_id' ) ) {
13917 $product_ids[] = $item->get_product_id();
13918 }
13919 }
13920 $context = array_merge(
13921 WooCommerce::get_order_context( $order_id ),
13922 WordPress::get_user_context( $user_id )
13923 );
13924
13925 foreach ( $product_ids as $product_id ) {
13926 $context['product_id'] = $product_id;
13927 }
13928
13929 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
13930 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
13931 /**
13932 *
13933 * Ignore line
13934 *
13935 * @phpstan-ignore-next-line
13936 */
13937 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
13938 /**
13939 *
13940 * Ignore line
13941 *
13942 * @phpstan-ignore-next-line
13943 */
13944 $date = date_i18n( get_option( 'date_format' ), $timestamp );
13945 $context['ship_date'] = $date;
13946 $context['response_type'] = 'live';
13947 }
13948
13949 $context['pluggable_data'] = $context;
13950 }
13951
13952 return $context;
13953 }
13954
13955 /**
13956 * Get GroundHogg Tag list
13957 *
13958 * @param array $data data.
13959 *
13960 * @return array
13961 */
13962 public function search_groundhogg_tag_list( $data ) {
13963
13964 if ( ! function_exists( 'Groundhogg\get_db' ) ) {
13965 return [];
13966 }
13967
13968 $tags = \Groundhogg\get_db( 'tags' )->query( [] );
13969 $options = [];
13970
13971 if ( ! empty( $tags ) ) {
13972 foreach ( $tags as $tag ) {
13973 $options[] = [
13974 'label' => $tag->tag_name,
13975 'value' => $tag->tag_id,
13976 ];
13977 }
13978 }
13979
13980 return [
13981 'options' => $options,
13982 'hasMore' => false,
13983 ];
13984 }
13985
13986 /**
13987 * Get WP courseware courses list
13988 *
13989 * @param array $data data.
13990 *
13991 * @return array
13992 */
13993 public function search_wpcw_courses( $data ) {
13994 $options = [];
13995
13996 $page = $data['page'];
13997 $limit = Utilities::get_search_page_limit();
13998 $offset = $limit * ( $page - 1 );
13999
14000 $args = [
14001 'post_type' => 'wpcw_course',
14002 'posts_per_page' => $limit,
14003 'offset' => $offset,
14004 'orderby' => 'title',
14005 'order' => 'ASC',
14006 'post_status' => 'publish',
14007 ];
14008
14009 $courses = get_posts( $args );
14010
14011 $course_count = wp_count_posts( 'wpcw_course' )->publish;
14012
14013 if ( ! empty( $courses ) ) {
14014 if ( is_array( $courses ) ) {
14015 foreach ( $courses as $course ) {
14016 $options[] = [
14017 'label' => $course->post_title,
14018 'value' => $course->ID,
14019 ];
14020 }
14021 }
14022 }
14023 return [
14024 'options' => $options,
14025 'hasMore' => $course_count > $limit && $course_count > $offset,
14026 ];
14027 }
14028
14029 /**
14030 * Get WP courseware courses list
14031 *
14032 * @param array $data data.
14033 *
14034 * @return array
14035 */
14036 public function search_wpcw_modules( $data ) {
14037 $options = [];
14038
14039 if ( function_exists( 'wpcw_get_modules' ) ) {
14040 $modules = wpcw_get_modules();
14041 }
14042
14043 if ( ! empty( $modules ) ) {
14044 if ( is_array( $modules ) ) {
14045 foreach ( $modules as $module ) {
14046 $options[] = [
14047 'label' => $module->module_title,
14048 'value' => $module->module_id,
14049 ];
14050 }
14051 }
14052 }
14053 return [
14054 'options' => $options,
14055 'hasMore' => false,
14056 ];
14057 }
14058
14059 /**
14060 * Get WP courseware unit list
14061 *
14062 * @param array $data data.
14063 *
14064 * @return array
14065 */
14066 public function search_wpcw_units( $data ) {
14067 $options = [];
14068
14069 $page = $data['page'];
14070 $limit = Utilities::get_search_page_limit();
14071 $offset = $limit * ( $page - 1 );
14072
14073 $args = [
14074 'post_type' => 'course_unit',
14075 'posts_per_page' => $limit,
14076 'offset' => $offset,
14077 'orderby' => 'title',
14078 'order' => 'ASC',
14079 'post_status' => 'publish',
14080 ];
14081
14082 $units = get_posts( $args );
14083
14084 $unit_count = wp_count_posts( 'course_unit' )->publish;
14085
14086 if ( ! empty( $units ) ) {
14087 if ( is_array( $units ) ) {
14088 foreach ( $units as $unit ) {
14089 $options[] = [
14090 'label' => $unit->post_title,
14091 'value' => $unit->ID,
14092 ];
14093 }
14094 }
14095 }
14096 return [
14097 'options' => $options,
14098 'hasMore' => $unit_count > $limit && $unit_count > $offset,
14099 ];
14100 }
14101
14102 /**
14103 * Search WP Courseware data.
14104 *
14105 * @param array $data data.
14106 * @return array|void
14107 */
14108 public function search_wpcw_last_data( $data ) {
14109 global $wpdb;
14110 $post_type = $data['post_type'];
14111 $trigger = $data['search_term'];
14112 $context = [];
14113
14114 if ( 'wpcw_course_completed' === $trigger ) {
14115 $post_id = $data['filter']['course_post_id']['value'];
14116 if ( -1 === $post_id ) {
14117 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpcw_user_courses as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.course_id WHERE postmeta.course_progress=100 order by postmeta.user_id DESC LIMIT 1" );
14118 } else {
14119 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wpcw_user_courses as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.course_id WHERE postmeta.course_id = %s AND postmeta.course_progress=100 order by postmeta.user_id DESC LIMIT 1", $post_id ) );
14120 }
14121 } elseif ( 'wpcw_module_completed' === $trigger ) {
14122 $post_id = $data['filter']['module_id']['value'];
14123 if ( -1 === $post_id ) {
14124 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpcw_user_courses as postmeta JOIN {$wpdb->prefix}wpcw_modules as posts ON posts.parent_course_id=postmeta.course_id WHERE postmeta.course_progress>=0 order by postmeta.course_enrolment_date DESC LIMIT 1" );
14125 } else {
14126 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wpcw_user_courses as postmeta JOIN {$wpdb->prefix}wpcw_modules as posts ON posts.parent_course_id=postmeta.course_id WHERE postmeta.course_progress>=0 AND posts.module_id=%s order by postmeta.course_enrolment_date DESC LIMIT 1", $post_id ) );
14127 }
14128 } elseif ( 'wpcw_unit_completed' === $trigger ) {
14129 $post_id = $data['filter']['unit_id']['value'];
14130 if ( -1 === $post_id ) {
14131 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpcw_user_progress WHERE unit_completed_status='complete' order by unit_id DESC LIMIT 1" );
14132 } else {
14133 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wpcw_user_progress WHERE unit_id=%d AND unit_completed_status='complete' order by unit_id DESC LIMIT 1", $post_id ) );
14134 }
14135 } elseif ( 'wpcw_enroll_course' === $trigger ) {
14136 $post_id = $data['filter']['course_post_id']['value'];
14137 if ( -1 === $post_id ) {
14138 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpcw_user_courses as postmeta JOIN {$wpdb->prefix}wpcw_courses as posts ON posts.course_id=postmeta.course_id order by course_enrolment_date DESC LIMIT 1" );
14139 } else {
14140 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wpcw_user_courses as postmeta JOIN {$wpdb->prefix}wpcw_courses as posts ON posts.course_id=postmeta.course_id WHERE posts.course_post_id=%d order by course_enrolment_date DESC LIMIT 1", $post_id ) );
14141 }
14142 }
14143
14144 if ( ! empty( $result ) ) {
14145
14146 switch ( $trigger ) {
14147 case 'wpcw_course_completed':
14148 $result_course_id = $result[0]->course_id;
14149 $result_user_id = $result[0]->user_id;
14150 if ( function_exists( 'wpcw_get_course' ) ) {
14151 $course = wpcw_get_course( $result_course_id );
14152 if ( is_object( $course ) ) {
14153 $course = get_object_vars( $course );
14154 }
14155 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course );
14156 }
14157 break;
14158 case 'wpcw_module_completed':
14159 $result_module_id = $result[0]->module_id;
14160 $result_user_id = $result[0]->user_id;
14161 if ( function_exists( 'wpcw_get_module' ) ) {
14162 $module = wpcw_get_module( $result_module_id );
14163 if ( is_object( $module ) ) {
14164 $module = get_object_vars( $module );
14165 }
14166 $context = array_merge( WordPress::get_user_context( $result_user_id ), $module );
14167 }
14168 break;
14169 case 'wpcw_unit_completed':
14170 $result_unit_id = $result[0]->unit_id;
14171 $result_user_id = $result[0]->user_id;
14172 if ( function_exists( 'wpcw_get_unit' ) ) {
14173 $unit = wpcw_get_unit( $result_unit_id );
14174 if ( is_object( $unit ) ) {
14175 $unit = get_object_vars( $unit );
14176 $unit['name'] = get_the_title( $result_unit_id );
14177 }
14178 $context = array_merge( WordPress::get_user_context( $result_user_id ), $unit );
14179 }
14180 break;
14181 case 'wpcw_enroll_course':
14182 $result_course_id = $result[0]->course_id;
14183 $result_user_id = $result[0]->user_id;
14184 if ( function_exists( 'WPCW_courses_getCourseDetails' ) ) {
14185 $course_detail = WPCW_courses_getCourseDetails( $result_course_id );
14186 if ( is_object( $course_detail ) ) {
14187 $course_detail = get_object_vars( $course_detail );
14188 }
14189 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course_detail );
14190 }
14191 break;
14192 default:
14193 return;
14194 }
14195 $context['pluggable_data'] = $context;
14196 $context['response_type'] = 'live';
14197 }
14198
14199 return $context;
14200
14201 }
14202
14203 /**
14204 * Get WooCommerce Order Note list.
14205 *
14206 * @param array $data data.
14207 *
14208 * @return array
14209 */
14210 public function search_note_type_list( $data ) {
14211
14212 $options = [];
14213
14214 $options[] = [
14215 'label' => 'Customer',
14216 'value' => 'customer',
14217 ];
14218 $options[] = [
14219 'label' => 'Private',
14220 'value' => 'internal',
14221 ];
14222
14223 return [
14224 'options' => $options,
14225 'hasMore' => false,
14226 ];
14227 }
14228
14229 /**
14230 * Get last data for trigger
14231 *
14232 * @param array $data data.
14233 * @return array
14234 */
14235 public function search_woo_commerce_customers_triggers_last_data( $data ) {
14236 $context = [];
14237 $context['response_type'] = 'sample';
14238 $context['pluggable_data'] = [];
14239 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
14240 global $wpdb;
14241 if ( 'customer_created' === $term ) {
14242 $customer_query = get_users(
14243 [
14244 'fields' => 'ID',
14245 'role' => 'customer',
14246 'orderby' => 'ID',
14247 'order' => 'DESC',
14248 'number' => 1,
14249 ]
14250 );
14251 $results = $customer_query;
14252 if ( ! empty( $results ) ) {
14253 $customer = new WC_Customer( $results[0] );
14254 $last_order = $customer->get_last_order();
14255 $customer_data = [
14256 'id' => $customer->get_id(),
14257 'email' => $customer->get_email(),
14258 'first_name' => $customer->get_first_name(),
14259 'last_name' => $customer->get_last_name(),
14260 'username' => $customer->get_username(),
14261 'last_order_id' => is_object( $last_order ) ? $last_order->get_id() : null,
14262 'orders_count' => $customer->get_order_count(),
14263 'total_spent' => wc_format_decimal( $customer->get_total_spent(), 2 ),
14264 'avatar_url' => $customer->get_avatar_url(),
14265 'billing_address' => [
14266 'first_name' => $customer->get_billing_first_name(),
14267 'last_name' => $customer->get_billing_last_name(),
14268 'company' => $customer->get_billing_company(),
14269 'address_1' => $customer->get_billing_address_1(),
14270 'address_2' => $customer->get_billing_address_2(),
14271 'city' => $customer->get_billing_city(),
14272 'state' => $customer->get_billing_state(),
14273 'postcode' => $customer->get_billing_postcode(),
14274 'country' => $customer->get_billing_country(),
14275 'email' => $customer->get_billing_email(),
14276 'phone' => $customer->get_billing_phone(),
14277 ],
14278 'shipping_address' => [
14279 'first_name' => $customer->get_shipping_first_name(),
14280 'last_name' => $customer->get_shipping_last_name(),
14281 'company' => $customer->get_shipping_company(),
14282 'address_1' => $customer->get_shipping_address_1(),
14283 'address_2' => $customer->get_shipping_address_2(),
14284 'city' => $customer->get_shipping_city(),
14285 'state' => $customer->get_shipping_state(),
14286 'postcode' => $customer->get_shipping_postcode(),
14287 'country' => $customer->get_shipping_country(),
14288 ],
14289 ];
14290 if ( is_object( $last_order ) && method_exists( $last_order, 'get_date_created' ) ) {
14291 $created_date = $last_order->get_date_created();
14292 if ( is_object( $created_date ) && method_exists( $created_date, 'getTimestamp' ) ) {
14293 $last_order_date = $created_date->getTimestamp();
14294 $customer_data['created_at'] = $last_order_date;
14295 $customer_data['last_order_date'] = $last_order_date;
14296 }
14297 }
14298 $order_sample_data = $customer_data;
14299 $context['response_type'] = 'live';
14300 $context['pluggable_data'] = $order_sample_data;
14301 } else {
14302 $order_sample_data = json_decode( '{"id": 158,"email": "johnd@d.com","first_name": "john","last_name": "d","username": "johnd","last_order_id": 6604,"orders_count": 3,"total_spent": "45.00","avatar_url": "https:\/\/secure.gravatar.com\/avatar\/0f9c8dc78cff3ea4d447b2297c8f6803?s=96&d=mm&r=g","billing_address": {"first_name": "john","last_name": "d","company": "","address_1": "","address_2": "","city": "London","state": "TN","postcode": "PV1 QW2","country": "UK","email": "johnd@d.com","phone": "9878988766"},"shipping_address": {"first_name": "","last_name": "","company": "","address_1": "","address_2": "","city": "","state": "","postcode": "","country": ""},"created_at": 1697631243,"last_order_date": 1697631243}', true );
14303 $context['pluggable_data'] = $order_sample_data;
14304 }
14305 } elseif ( 'total_spend_reach' === $term ) {
14306 $total_spend_selected = isset( $data['filter']['total_spend']['value'] ) ? $data['filter']['total_spend']['value'] : '';
14307 $customers = $wpdb->get_col(
14308 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
14309 WHERE meta_key = '_customer_user' AND meta_value > 0"
14310 );
14311 $target_customer = null;
14312 foreach ( $customers as $customer ) {
14313 $total_spend = (int) wc_get_customer_total_spent( $customer );
14314 if ( $total_spend == $total_spend_selected ) {
14315 $target_customer = $customer;
14316 break; // Exit the loop once a matching customer is found.
14317 }
14318 }
14319 if ( $target_customer ) {
14320 $new_customer = new WC_Customer( $target_customer );
14321 $new_customer_data = [
14322 'id' => $new_customer->get_id(),
14323 'email' => $new_customer->get_email(),
14324 'first_name' => $new_customer->get_first_name(),
14325 'last_name' => $new_customer->get_last_name(),
14326 'username' => $new_customer->get_username(),
14327 'order_count' => $new_customer->get_order_count(),
14328 'total_spend' => wc_format_decimal( $new_customer->get_total_spent(), 2 ),
14329 ];
14330 $context['response_type'] = 'live';
14331 $context['pluggable_data'] = $new_customer_data;
14332 } else {
14333 $sample_customer_data = [
14334 'id' => '101',
14335 'created_at' => '1680675247',
14336 'email' => 'john@d.com',
14337 'first_name' => 'John',
14338 'last_name' => 'D',
14339 'username' => 'johnd',
14340 'order_count' => '3',
14341 'total_spend' => '22.00',
14342 ];
14343 $context['pluggable_data'] = $sample_customer_data;
14344 }
14345 } elseif ( 'order_count_reach' === $term ) {
14346 $total_order_selected = isset( $data['filter']['order_count']['value'] ) ? $data['filter']['order_count']['value'] : '';
14347 $customers = $wpdb->get_col(
14348 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
14349 WHERE meta_key = '_customer_user' AND meta_value > 0"
14350 );
14351 $target_customer = null;
14352 foreach ( $customers as $customer ) {
14353 $args = [
14354 'customer_id' => $customer,
14355 'limit' => -1,
14356 'status' => [ 'wc-completed' ],
14357 ];
14358 $orders = wc_get_orders( $args );
14359 if ( ! empty( $orders ) ) {
14360 $total_order = (int) wc_get_customer_order_count( $customer );
14361 if ( $total_order == $total_order_selected ) {
14362 $target_customer = $customer;
14363 break; // Exit the loop once a matching customer is found.
14364 }
14365 }
14366 }
14367 if ( $target_customer ) {
14368 $new_customer = new WC_Customer( $target_customer );
14369 $new_customer_data = [
14370 'id' => $new_customer->get_id(),
14371 'email' => $new_customer->get_email(),
14372 'first_name' => $new_customer->get_first_name(),
14373 'last_name' => $new_customer->get_last_name(),
14374 'username' => $new_customer->get_username(),
14375 'order_count' => $new_customer->get_order_count(),
14376 ];
14377 $context['response_type'] = 'live';
14378 $context['pluggable_data'] = $new_customer_data;
14379 } else {
14380 $sample_customer_data = [
14381 'id' => '101',
14382 'created_at' => '1680675247',
14383 'email' => 'john@d.com',
14384 'first_name' => 'John',
14385 'last_name' => 'D',
14386 'username' => 'johnd',
14387 'orders_count' => '3',
14388 ];
14389 $context['pluggable_data'] = $sample_customer_data;
14390 }
14391 }
14392
14393 return $context;
14394 }
14395
14396 /**
14397 * Get Affiliate list
14398 *
14399 * @param array $data data.
14400 *
14401 * @return array
14402 */
14403 public function search_affiliate_list( $data ) {
14404 $options = [];
14405
14406 $args = [
14407 'meta_query' => [
14408 [
14409 'key' => 'wafp_is_affiliate',
14410 'value' => '1',
14411 'compare' => '=',
14412 ],
14413 ],
14414 ];
14415
14416 $affiliates = get_users( $args );
14417
14418 foreach ( $affiliates as $user ) {
14419 $options[] = [
14420 'label' => $user->display_name,
14421 'value' => $user->ID,
14422 ];
14423 }
14424 return [
14425 'options' => $options,
14426 'hasMore' => false,
14427 ];
14428 }
14429
14430 /**
14431 * Get Affiliate Source list
14432 *
14433 * @param array $data data.
14434 *
14435 * @return array
14436 */
14437 public function search_affiliate_transaction_source_list( $data ) {
14438 $options = [];
14439
14440 $sources = [
14441 'General',
14442 'MemberPress',
14443 'WooCommerce',
14444 'Easy Digital Downloads',
14445 'WPForms',
14446 'Formidable',
14447 'PayPal',
14448 ];
14449 foreach ( $sources as $source ) {
14450 $options[] = [
14451 'label' => $source,
14452 'value' => str_replace( ' ', '_', strtolower( $source ) ),
14453 ];
14454 }
14455
14456 return [
14457 'options' => $options,
14458 'hasMore' => false,
14459 ];
14460 }
14461
14462 /**
14463 * Search Easy Affiliate data.
14464 *
14465 * @param array $data data.
14466 * @return array|void
14467 */
14468 public function search_easy_affiliate_last_data( $data ) {
14469
14470 global $wpdb;
14471 $trigger = $data['search_term'];
14472 $context = [];
14473
14474 if ( 'sale_recorded' === $trigger ) {
14475 $affiliate_id = $data['filter']['affiliate_id']['value'];
14476 if ( -1 === $affiliate_id ) {
14477 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wafp_transactions as postmeta JOIN {$wpdb->prefix}wafp_events as posts ON posts.evt_id=postmeta.id WHERE postmeta.status='complete' order by postmeta.id DESC LIMIT 1" );
14478 } else {
14479 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wafp_transactions as postmeta JOIN {$wpdb->prefix}wafp_events as posts ON posts.evt_id=postmeta.id WHERE postmeta.status='complete' AND affiliate_id=%d order by postmeta.id DESC LIMIT 1", $affiliate_id ) );
14480 }
14481 } elseif ( 'sale_added' === $trigger ) {
14482 $orderby = 'signup_date';
14483 $order = 'DESC';
14484 $paged = 1;
14485 $search = '';
14486 $perpage = 1;
14487 /**
14488 *
14489 * Ignore line
14490 *
14491 * @phpstan-ignore-next-line
14492 */
14493 $result = \EasyAffiliate\Models\User::affiliate_list_table( $orderby, $order, $paged, $search, $perpage );
14494 if ( empty( $result['results'] ) ) {
14495 $result = [];
14496 }
14497 } elseif ( 'payout_made' === $trigger ) {
14498 $affiliate_id = $data['filter']['affiliate_id']['value'];
14499 if ( -1 === $affiliate_id ) {
14500 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wafp_payments as postmeta JOIN {$wpdb->prefix}wafp_events as posts ON posts.evt_id=postmeta.id WHERE postmeta.amount>0 order by postmeta.id DESC LIMIT 1" );
14501 } else {
14502 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wafp_payments as postmeta JOIN {$wpdb->prefix}wafp_events as posts ON posts.evt_id=postmeta.id WHERE postmeta.amount>0 AND affiliate_id=%d order by postmeta.id DESC LIMIT 1", $affiliate_id ) );
14503 }
14504 }
14505
14506 if ( ! empty( $result ) ) {
14507 switch ( $trigger ) {
14508 case 'sale_recorded':
14509 $result_affiliate_id = $result[0]->affiliate_id;
14510 /**
14511 *
14512 * Ignore line
14513 *
14514 * @phpstan-ignore-next-line
14515 */
14516 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
14517 $affiliate = get_object_vars( $affiliate->rec );
14518 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
14519 break;
14520 case 'sale_added':
14521 /**
14522 *
14523 * Ignore line
14524 *
14525 * @phpstan-ignore-next-line
14526 */
14527 $data = new \EasyAffiliate\Models\User( $result['results'][0]->ID );
14528 /**
14529 *
14530 * Ignore line
14531 *
14532 * @phpstan-ignore-next-line
14533 */
14534 $context = get_object_vars( $data->rec );
14535 break;
14536 case 'payout_made':
14537 $result_affiliate_id = $result[0]->affiliate_id;
14538 /**
14539 *
14540 * Ignore line
14541 *
14542 * @phpstan-ignore-next-line
14543 */
14544 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
14545 $affiliate = get_object_vars( $affiliate->rec );
14546 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
14547 break;
14548 default:
14549 return;
14550 }
14551 $context['pluggable_data'] = $context;
14552 $context['response_type'] = 'live';
14553 } elseif ( empty( $result ) ) {
14554 switch ( $trigger ) {
14555 case 'sale_added':
14556 $context = json_decode(
14557 '{"ID": 1,"first_name": "John","last_name": "D","user_login": "johnd","user_nicename": "johnd","user_email": "johnd@d.com","user_url": "","user_registered": "2023-10-13 13:39:16","user_activation_key": "","user_status": "0","display_name": "johnd","paypal_email": "johnd@d.com","address_one": "1street","address_two": "",
14558 "city": "london","state": "","zip": "","country": "UK","tax_id_us": "","tax_id_int": "","is_affiliate": "1","is_blocked": "","blocked_message": "","referrer": "0","notes": "","unsubscribed": ""}',
14559 true
14560 );
14561 break;
14562 case 'sale_recorded':
14563 $context = json_decode( '{"wp_user_id": 73,"user_login": "johnd@yopmail.com","display_name": "john d","user_firstname": "john","user_lastname": "d","user_email": "johnd@yopmail.com","user_role": ["subscriber"],"id": "5","affiliate_id": "73","click_id": "0","item_id": "","item_name": "test","coupon": "","sale_amount": "10.00","refund_amount": "20.00","subscr_id": "","subscr_paynum": "0","ip_addr": "","cust_email": "","cust_name": "","trans_num": "test5","type": "commission","source": "general","order_id": "0","status": "complete","rebill": "0","created_at": "2023-10-11 09:18:41"}', true );
14564 break;
14565 case 'payout_made':
14566 $context = json_decode( '{"wp_user_id": 73,"user_login": "johnd@yopmail.com","display_name": "john d","user_firstname": "john","user_lastname": "d","user_email": "johnd@yopmail.com","user_role": ["subscriber"],"id": "5","affiliate_id": "73","click_id": "0","item_id": "","item_name": "test","coupon": "","sale_amount": "10.00","refund_amount": "20.00","subscr_id": "","subscr_paynum": "0","ip_addr": "","cust_email": "","cust_name": "","trans_num": "test5","type": "commission","source": "general","order_id": "0","status": "complete","rebill": "0","created_at": "2023-10-11 09:18:41"}', true );
14567 break;
14568 default:
14569 return;
14570 }
14571 $context['pluggable_data'] = $context;
14572 $context['response_type'] = 'sample';
14573 }
14574
14575 return $context;
14576
14577 }
14578
14579 /**
14580 * Get WooCommerce Subscriptions Coupon list
14581 *
14582 * @param array $data data.
14583 *
14584 * @return array
14585 */
14586 public function search_woo_subscription_coupon_list( $data ) {
14587 $options = [];
14588
14589 $coupon_codes = [];
14590
14591 $query = new \WP_Query(
14592 [
14593 'post_type' => 'shop_coupon',
14594 'posts_per_page' => -1,
14595 'no_found_rows' => true,
14596 'meta_query' => [
14597 [
14598 'key' => '_is_aw_coupon',
14599 'compare' => 'NOT EXISTS',
14600 ],
14601 ],
14602 ]
14603 );
14604
14605 foreach ( $query->posts as $coupon ) {
14606 /**
14607 *
14608 * Ignore line
14609 *
14610 * @phpstan-ignore-next-line
14611 */
14612 $code = wc_format_coupon_code( $coupon->post_title );
14613 $coupon_codes[ $code ] = $code;
14614 }
14615
14616 foreach ( $coupon_codes as $code ) {
14617
14618 $coupon = new \WC_Coupon( $code );
14619
14620 if ( ! in_array( $coupon->get_discount_type(), [ 'recurring_fee', 'recurring_percent' ], true ) ) {
14621 unset( $coupon_codes[ $code ] );
14622 }
14623 }
14624
14625 if ( ! empty( $coupon_codes ) ) {
14626 if ( is_array( $coupon_codes ) ) {
14627 foreach ( $coupon_codes as $coupon_code ) {
14628 $options[] = [
14629 'label' => $coupon_code,
14630 'value' => $coupon_code,
14631 ];
14632 }
14633 }
14634 }
14635 return [
14636 'options' => $options,
14637 'hasMore' => false,
14638 ];
14639 }
14640
14641 /**
14642 * Get BuddyBoss field.
14643 *
14644 * @param array $data data.
14645 *
14646 * @return array|void
14647 */
14648 public function search_bb_field_list( $data ) {
14649 global $wpdb;
14650 $options = [];
14651
14652 $xprofile_fields = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_xprofile_fields WHERE parent_id = 0 ORDER BY field_order ASC" );
14653 if ( ! empty( $xprofile_fields ) ) {
14654 foreach ( $xprofile_fields as $xprofile_field ) {
14655 $options[] = [
14656 'label' => $xprofile_field->name,
14657 'value' => $xprofile_field->id,
14658 ];
14659 }
14660 }
14661
14662 return [
14663 'options' => $options,
14664 'hasMore' => false,
14665 ];
14666 }
14667
14668 /**
14669 * Get Woocommerce Bookings Product List.
14670 *
14671 * @param array $data data.
14672 *
14673 * @return array|void
14674 */
14675 public function search_wb_bookable_product_list( $data ) {
14676 $options = [];
14677
14678 if ( ! class_exists( 'WC_Bookings_Admin' ) ) {
14679 return;
14680 }
14681
14682 $products = WC_Bookings_Admin::get_booking_products();
14683 if ( ! empty( $products ) ) {
14684 if ( is_array( $products ) ) {
14685 foreach ( $products as $product ) {
14686 $options[] = [
14687 'label' => $product->get_name(),
14688 'value' => $product->get_id(),
14689 ];
14690 $resources = $product->get_resources();
14691 if ( ! empty( $resources ) ) {
14692 foreach ( $resources as $resource ) {
14693 $options[] = [
14694 'label' => $resource->get_name(),
14695 'value' => $resource->get_id(),
14696 ];
14697 }
14698 }
14699 }
14700 }
14701 }
14702
14703 return [
14704 'options' => $options,
14705 'hasMore' => false,
14706 ];
14707 }
14708
14709 /**
14710 * Get last data for WooCommerce Booking trigger
14711 *
14712 * @param array $data data.
14713 * @return array
14714 */
14715 public function search_woo_commerce_booking_last_data( $data ) {
14716 $context = [];
14717 $context['response_type'] = 'sample';
14718 $context['pluggable_data'] = [];
14719 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
14720 global $wpdb;
14721 if ( 'booking_created' === $term ) {
14722 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' AND post_status='confirmed' order by ID DESC LIMIT 1" );
14723
14724 if ( ! empty( $results ) ) {
14725 if ( class_exists( 'WC_Booking' ) ) {
14726 $booking = new WC_Booking( $results[0]->ID );
14727 $person_counts = $booking->get_person_counts();
14728 $bookable_product_id = $booking->get_product_id();
14729 if ( method_exists( $booking, 'get_data' ) ) {
14730 $booking = $booking->get_data();
14731 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
14732 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
14733 if ( ! empty( $person_counts ) ) {
14734 $total_count = 0;
14735 foreach ( $person_counts as $key => $value ) {
14736 $total_count += $value;
14737 }
14738 $booking['total_person_counts'] = $total_count;
14739 }
14740 $booking['bookable_product'] = $bookable_product_id;
14741 $booking['bookable_product'] = $booking->get_product_id();
14742 $context['response_type'] = 'live';
14743 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
14744 }
14745 }
14746 } else {
14747 $booking_sample_data = json_decode( '{"id": 6546,"all_day": false,"cost": "45.02","customer_id": 1,"date_created": 1696915500,"date_modified": 1696915863,"end": "2023-10-14 12:00:00","google_calendar_event_id": "0","order_id": 0,"order_item_id": 0,"parent_id": 0,"person_counts": {"6525": 1,"6526": 1},"product_id": 6527,"resource_id": 0,"start": "2023-10-14 10:00:00","status": "confirmed","local_timezone": "","meta_data": [{"id": 12163,"key": "_booking_all_day","value": "0"},{"id": 12164,"key": "_booking_cost","value": "45.02"},{"id": 12165,"key": "_booking_customer_id","value": "1"},{"id": 12166,"key": "_booking_order_item_id","value": "0"},{"id": 12167,"key": "_booking_parent_id","value": "0"},{"id": 12168,"key": "_booking_persons","value": {"6525": 1,"6526": 1}},{"id": 12169,"key": "_booking_product_id","value": "6527"},{"id": 12170,"key": "_booking_resource_id","value": "0"},{"id": 12171,"key": "_booking_start","value": "20231014100000"},{"id": 12172,"key": "_booking_end","value": "20231014120000"},{"id": 12173,"key": "_wc_bookings_gcalendar_event_id","value": "0"},{"id": 12175,"key": "_edit_lock","value": "1696915725:1"},{"id": 12176,"key": "_edit_last","value": "1"}],"total_person_counts": 2,"bookable_product": 6527,"wp_user_id": 1,"user_login": "johnd@yopmail.com","display_name": "john d","user_firstname": "john","user_lastname": "d","user_email": "johnd@yopmail.com","user_role": ["customer"]}', true );
14748 $context['pluggable_data'] = $booking_sample_data;
14749 }
14750 } elseif ( 'booking_status_changed' === $term ) {
14751 $to_status = isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1;
14752
14753 if ( -1 == $to_status ) {
14754 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' order by ID DESC LIMIT 1" );
14755 } else {
14756 $results = $wpdb->get_results(
14757 $wpdb->prepare(
14758 "
14759 SELECT *
14760 FROM {$wpdb->prefix}posts
14761 WHERE `post_status` = %s
14762 AND `post_type` LIKE %s
14763 ",
14764 $to_status,
14765 'wc_booking'
14766 )
14767 );
14768 }
14769
14770 if ( ! empty( $results ) ) {
14771 if ( class_exists( 'WC_Booking' ) ) {
14772 $booking = new WC_Booking( $results[0]->ID );
14773 $person_counts = $booking->get_person_counts();
14774 $bookable_product_id = $booking->get_product_id();
14775 if ( method_exists( $booking, 'get_data' ) ) {
14776 $booking = $booking->get_data();
14777 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
14778 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
14779 if ( ! empty( $person_counts ) ) {
14780 $total_count = 0;
14781 foreach ( $person_counts as $key => $value ) {
14782 $total_count += $value;
14783 }
14784 $booking['total_person_counts'] = $total_count;
14785 }
14786 $booking['bookable_product'] = $bookable_product_id;
14787 $booking['from_status'] = $data['filter']['from_status']['value'];
14788 $booking['to_status'] = $booking['status'];
14789 $context['response_type'] = 'live';
14790 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
14791 }
14792 }
14793 } else {
14794 $booking_sample_data = json_decode( '{"id": 6546,"all_day": false,"cost": "45.02","customer_id": 1,"date_created": 1696915500,"date_modified": 1696915863,"end": "2023-10-14 12:00:00","google_calendar_event_id": "0","order_id": 0,"order_item_id": 0,"parent_id": 0,"person_counts": {"6525": 1,"6526": 1},"product_id": 6527,"resource_id": 0,"start": "2023-10-14 10:00:00","status": "confirmed","to_status": "confirmed","from_status": "cancelled","local_timezone": "","meta_data": [{"id": 12163,"key": "_booking_all_day","value": "0"},{"id": 12164,"key": "_booking_cost","value": "45.02"},{"id": 12165,"key": "_booking_customer_id","value": "1"},{"id": 12166,"key": "_booking_order_item_id","value": "0"},{"id": 12167,"key": "_booking_parent_id","value": "0"},{"id": 12168,"key": "_booking_persons","value": {"6525": 1,"6526": 1}},{"id": 12169,"key": "_booking_product_id","value": "6527"},{"id": 12170,"key": "_booking_resource_id","value": "0"},{"id": 12171,"key": "_booking_start","value": "20231014100000"},{"id": 12172,"key": "_booking_end","value": "20231014120000"},{"id": 12173,"key": "_wc_bookings_gcalendar_event_id","value": "0"},{"id": 12175,"key": "_edit_lock","value": "1696915725:1"},{"id": 12176,"key": "_edit_last","value": "1"}],"total_person_counts": 2,"bookable_product": 6527,"wp_user_id": 1,"user_login": "johnd@yopmail.com","display_name": "john d","user_firstname": "john","user_lastname": "d","user_email": "johnd@yopmail.com","user_role": ["customer"]}', true );
14795 $context['pluggable_data'] = $booking_sample_data;
14796 }
14797 }
14798 return $context;
14799 }
14800
14801 /**
14802 * Get WooCommerce Booking status list.
14803 *
14804 * @param array $data data.
14805 *
14806 * @return array
14807 */
14808 public function search_woo_booking_status_list( $data ) {
14809
14810 $options = [
14811 [
14812 'label' => 'Pending Confirmation',
14813 'value' => 'pending-confirmation',
14814 ],
14815 [
14816 'label' => 'Unpaid',
14817 'value' => 'unpaid',
14818 ],
14819 [
14820 'label' => 'Confirmed',
14821 'value' => 'confirmed',
14822 ],
14823 [
14824 'label' => 'Paid',
14825 'value' => 'paid',
14826 ],
14827 [
14828 'label' => 'Complete',
14829 'value' => 'complete',
14830 ],
14831 [
14832 'label' => 'In Cart',
14833 'value' => 'in-cart',
14834 ],
14835 [
14836 'label' => 'Cancelled',
14837 'value' => 'cancelled',
14838 ],
14839 ];
14840
14841 return [
14842 'options' => $options,
14843 'hasMore' => false,
14844 ];
14845 }
14846
14847 /**
14848 * Get Woocommerce Memberships Status List.
14849 *
14850 * @param array $data data.
14851 *
14852 * @return array|void
14853 */
14854 public function search_wc_membership_status_list( $data ) {
14855 $options = [];
14856 if ( ! function_exists( 'wc_memberships_get_user_membership_statuses' ) ) {
14857 return;
14858 }
14859
14860 $statuses = wc_memberships_get_user_membership_statuses();
14861 if ( ! empty( $statuses ) ) {
14862 if ( is_array( $statuses ) ) {
14863 foreach ( $statuses as $status => $value ) {
14864 $status = 0 === strpos( $status, 'wcm-' ) ? substr( $status, 4 ) : $status;
14865 $options[] = [
14866 'label' => $value['label'],
14867 'value' => $status,
14868 ];
14869 }
14870 }
14871 }
14872
14873 return [
14874 'options' => $options,
14875 'hasMore' => false,
14876 ];
14877 }
14878
14879 /**
14880 * Search Woocommerce Subscription data.
14881 *
14882 * @param array $data data.
14883 * @return array|void
14884 */
14885 public function search_user_subscription_last_data( $data ) {
14886
14887 $context = [];
14888
14889 if ( ! function_exists( 'wcs_get_subscription' ) ) {
14890 return;
14891 }
14892 if ( ! function_exists( 'wcs_get_subscriptions_for_product' ) ) {
14893 return;
14894 }
14895 if ( ! function_exists( 'wcs_order_contains_renewal' ) ) {
14896 return;
14897 }
14898 if ( ! function_exists( 'wcs_get_subscriptions_for_order' ) ) {
14899 return;
14900 }
14901
14902 if ( 'trial_end' == $data['search_term'] ) {
14903 $subscription_id = $data['filter']['subscription']['value'];
14904 $query_args = [
14905 'post_type' => 'shop_subscription',
14906 'orderby' => 'ID',
14907 'order' => 'DESC',
14908 'post_status' => 'wc-active',
14909 'posts_per_page' => 1,
14910 'meta_query' => [
14911 [
14912 'key' => '_schedule_trial_end',
14913 'value' => gmdate( 'Y-m-d H:i:s' ),
14914 'compare' => '<=',
14915 ],
14916 ],
14917 'post__in' => [ $subscription_id ],
14918 ];
14919 $query_result = new WP_Query( $query_args );
14920 $subscription_orders = $query_result->get_posts();
14921
14922 if ( ! empty( $subscription_orders ) ) {
14923 /**
14924 *
14925 * Ignore line
14926 *
14927 * @phpstan-ignore-next-line
14928 */
14929 $subscription = wcs_get_subscription( $subscription_orders[0]->ID );
14930 $user_id = $subscription->get_user_id();
14931
14932 $items = $subscription->get_items();
14933 $product_ids = [];
14934 foreach ( $items as $item ) {
14935 $product_ids[] = $item->get_product_id();
14936 }
14937
14938 $subscription_status = $subscription->get_status();
14939 $subscription_start_date = $subscription->get_date_created();
14940
14941 $context['subscription_data'] = [
14942 'status' => $subscription_status,
14943 'start_date' => $subscription_start_date,
14944 'trial_end_date' => $subscription->get_date( 'schedule_trial_end' ),
14945 'next_payment_date' => $subscription->get_date( 'next_payment' ),
14946 'end_date' => $subscription->get_date( 'schedule_end' ),
14947 ];
14948 $context['user'] = WordPress::get_user_context( $user_id );
14949
14950 foreach ( $product_ids as $val ) {
14951 $context['subscription'] = $val;
14952 $context['subscription_name'] = get_the_title( $val );
14953 }
14954 $context['pluggable_data'] = $context;
14955 $context['response_type'] = 'live';
14956 } else {
14957 $context = json_decode( '{"subscription_data": {"status": "active","start_date": {"date": "2023-05-18 12:36:53.000000","timezone_type": 1,"timezone": "+00:00"},"trial_end_date": "2023-10-20 03:31:04","next_payment_date": 0,"end_date": "2024-05-28 12:36:53"},"user": {"wp_user_id": 1,"user_login": "john@d.com","display_name": "john d","user_firstname": "john","user_lastname": "d","user_email": "john@d.com","user_role": ["customer","subscriber"]},"subscription": 5861,"subscription_name": "Demo Subscription"}', true );
14958 $context['pluggable_data'] = $context;
14959 $context['response_type'] = 'sample';
14960 }
14961 } elseif ( 'renewal_payment_failed' == $data['search_term'] ) {
14962 $subscription_item = $data['filter']['subscription_item']['value'];
14963 $subscription_ids = wcs_get_subscriptions_for_product( $subscription_item );
14964 $ids = [];
14965 $related = [];
14966 foreach ( $subscription_ids as $subscription ) {
14967 $ids[] = $subscription;
14968 /**
14969 *
14970 * Ignore line
14971 *
14972 * @phpstan-ignore-next-line
14973 */
14974 $subscriptions = new WC_Subscription( $subscription );
14975 /**
14976 *
14977 * Ignore line
14978 *
14979 * @phpstan-ignore-next-line
14980 */
14981 $related[] = $subscriptions->get_related_orders();
14982 }
14983
14984 $failed_payment_orders = [];
14985 foreach ( $related as $value ) {
14986 foreach ( $value as $val ) {
14987 if ( wcs_order_contains_renewal( $val ) ) {
14988 $order = wc_get_order( $val );
14989 if ( $order ) {
14990 /**
14991 *
14992 * Ignore line
14993 *
14994 * @phpstan-ignore-next-line
14995 */
14996 $payment_status = $order->get_status();
14997 if ( 'failed' === $payment_status || 'wc-on-hold' === $payment_status ) {
14998 $failed_payment_orders[] = $val;
14999 }
15000 }
15001 }
15002 }
15003 }
15004
15005 if ( ! empty( $failed_payment_orders ) ) {
15006 $failed = array_rand( $failed_payment_orders );
15007 $related_subscriptions = wcs_get_subscriptions_for_order( $failed_payment_orders[ $failed ] );
15008 if ( ! empty( $related_subscriptions ) ) {
15009 $sub_id = get_post_meta( $failed_payment_orders[ $failed ], '_subscription_renewal', true );
15010 $subscription = wcs_get_subscription( $sub_id );
15011
15012 $items = $subscription->get_items();
15013 $product_ids = [];
15014 foreach ( $items as $item ) {
15015 $product_ids[] = $item->get_product_id();
15016 }
15017
15018 $context['user'] = WordPress::get_user_context( $subscription->get_user_id() );
15019 foreach ( [ 'renewal' ] as $order_type ) {
15020 foreach ( $subscription->get_related_orders( 'ids', $order_type ) as $order_id ) {
15021 $context['subscription_related_order'][] = $order_id;
15022 }
15023 }
15024 foreach ( $product_ids as $val ) {
15025 $context['subscription_item'] = $val;
15026 $context['subscription_name'] = get_the_title( $val );
15027 }
15028 $context['data'] = $subscription->get_data();
15029 $context['pluggable_data'] = $context;
15030 $context['response_type'] = 'live';
15031 } else {
15032 $context['pluggable_data'] = json_decode( '{"user": {"wp_user_id": 36,"user_login": "john@d.com","display_name": "test","user_firstname": "Test","user_lastname": "test","user_email": "john@d.com","user_role": ["wpamelia-customer","customer"]},"subscription_related_order": [6643],"subscription_item": 5962,"subscription_name": "Demo2 Subscription","data": {"id": 6642,"parent_id": 6641,"status": "on-hold","currency": "USD","version": "7.6.1","prices_include_tax": false,"date_created": {"date": "2023-10-20 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"date_modified": {"date": "2023-10-20 08:45:02.000000","timezone_type": 1,"timezone": "+00:00"},"discount_total": "0","discount_tax": "0","shipping_total": "0","shipping_tax": "0","cart_tax": "0","total": "1.00","total_tax": "0","customer_id": 36,"order_key": "wc_order_6Pdirh5WDDiVA","billing": {"first_name": "bella","last_name": "test","company": "","address_1": "test","address_2": "","city": "test","state": "TN","postcode": "600144","country": "IN","email": "john@d.com","phone": "98675757"},"shipping": {"first_name": "bella","last_name": "test","company": "","address_1": "test","address_2": "","city": "Chennai","state": "TN","postcode": "600100","country": "IN","phone": ""},"payment_method": "","payment_method_title": "","transaction_id": "","customer_ip_address": "127.0.0.1","customer_user_agent": "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/118.0.0.0 Safari\/537.36","created_via": "checkout","customer_note": "","date_completed": null,"date_paid": null,"cart_hash": "","order_stock_reduced": false,"download_permissions_granted": true,"new_order_email_sent": false,"recorded_sales": false,"recorded_coupon_usage_counts": false,"billing_period": "day","billing_interval": "1","suspension_count": 1,"requires_manual_renewal": true,"cancelled_email_sent": "","trial_period": "day","schedule_trial_end": {"date": "2023-10-21 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"schedule_next_payment": {"date": "2023-10-21 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"schedule_end": {"date": "2023-10-22 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"schedule_start": {"date": "2023-10-22 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"switch_data": [],"number": "6642","meta_data": [{"id": 13719,"key": "is_vat_exempt","value": "no"}],"line_items": {"142": []},"tax_lines": [],"shipping_lines": [],"fee_lines": [],"coupon_lines": []}}', true );
15033 $context['response_type'] = 'sample';
15034 }
15035 } else {
15036 $context['pluggable_data'] = json_decode( '{"user": {"wp_user_id": 36,"user_login": "john@d.com","display_name": "test","user_firstname": "Test","user_lastname": "test","user_email": "john@d.com","user_role": ["wpamelia-customer","customer"]},"subscription_related_order": [6643],"subscription_item": 5962,"subscription_name": "Demo2 Subscription","data": {"id": 6642,"parent_id": 6641,"status": "on-hold","currency": "USD","version": "7.6.1","prices_include_tax": false,"date_created": {"date": "2023-10-20 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"date_modified": {"date": "2023-10-20 08:45:02.000000","timezone_type": 1,"timezone": "+00:00"},"discount_total": "0","discount_tax": "0","shipping_total": "0","shipping_tax": "0","cart_tax": "0","total": "1.00","total_tax": "0","customer_id": 36,"order_key": "wc_order_6Pdirh5WDDiVA","billing": {"first_name": "bella","last_name": "test","company": "","address_1": "test","address_2": "","city": "test","state": "TN","postcode": "600144","country": "IN","email": "john@d.com","phone": "98675757"},"shipping": {"first_name": "bella","last_name": "test","company": "","address_1": "test","address_2": "","city": "Chennai","state": "TN","postcode": "600100","country": "IN","phone": ""},"payment_method": "","payment_method_title": "","transaction_id": "","customer_ip_address": "127.0.0.1","customer_user_agent": "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/118.0.0.0 Safari\/537.36","created_via": "checkout","customer_note": "","date_completed": null,"date_paid": null,"cart_hash": "","order_stock_reduced": false,"download_permissions_granted": true,"new_order_email_sent": false,"recorded_sales": false,"recorded_coupon_usage_counts": false,"billing_period": "day","billing_interval": "1","suspension_count": 1,"requires_manual_renewal": true,"cancelled_email_sent": "","trial_period": "day","schedule_trial_end": {"date": "2023-10-21 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"schedule_next_payment": {"date": "2023-10-21 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"schedule_end": {"date": "2023-10-22 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"schedule_start": {"date": "2023-10-22 08:44:25.000000","timezone_type": 1,"timezone": "+00:00"},"switch_data": [],"number": "6642","meta_data": [{"id": 13719,"key": "is_vat_exempt","value": "no"}],"line_items": {"142": []},"tax_lines": [],"shipping_lines": [],"fee_lines": [],"coupon_lines": []}}', true );
15037 $context['response_type'] = 'sample';
15038 }
15039 }
15040 return $context;
15041 }
15042
15043 /**
15044 * Get Masteriyo LMS Courses.
15045 *
15046 * @param array $data data.
15047 *
15048 * @return array
15049 */
15050 public function search_masteriyo_lms_courses( $data ) {
15051
15052 $page = $data['page'];
15053 $limit = Utilities::get_search_page_limit();
15054 $offset = $limit * ( $page - 1 );
15055
15056 $args = [
15057 'post_type' => 'mto-course',
15058 'posts_per_page' => $limit,
15059 'offset' => $offset,
15060 'orderby' => 'title',
15061 'order' => 'ASC',
15062 'post_status' => 'publish',
15063 ];
15064
15065 $courses = get_posts( $args );
15066
15067 $course_count = wp_count_posts( 'mto-course' )->publish;
15068
15069 $options = [];
15070 if ( ! empty( $courses ) ) {
15071 if ( is_array( $courses ) ) {
15072 foreach ( $courses as $course ) {
15073 $options[] = [
15074 'label' => $course->post_title,
15075 'value' => $course->ID,
15076 ];
15077 }
15078 }
15079 }
15080 return [
15081 'options' => $options,
15082 'hasMore' => $course_count > $limit && $course_count > $offset,
15083 ];
15084 }
15085
15086 /**
15087 * Get Masteriyo LMS Lessons.
15088 *
15089 * @param array $data data.
15090 *
15091 * @return array
15092 */
15093 public function search_masteriyo_lms_lessons( $data ) {
15094
15095 $course_id = $data['dynamic'];
15096 $page = $data['page'];
15097 $limit = Utilities::get_search_page_limit();
15098 $offset = $limit * ( $page - 1 );
15099 $options = [];
15100 $args = [
15101 'post_type' => 'mto-lesson',
15102 'posts_per_page' => $limit,
15103 'offset' => $offset,
15104 'orderby' => 'title',
15105 'order' => 'ASC',
15106 'post_status' => 'publish',
15107 'meta_query' => [
15108 [
15109 'key' => '_course_id',
15110 'value' => $course_id,
15111 'compare' => '=',
15112 ],
15113 ],
15114 ];
15115
15116 $lessons = get_posts( $args );
15117 $lesson_count = wp_count_posts( 'mto-lesson' )->publish;
15118 if ( ! empty( $lessons ) ) {
15119 if ( is_array( $lessons ) ) {
15120 foreach ( $lessons as $lesson ) {
15121 $options[] = [
15122 'label' => $lesson->post_title,
15123 'value' => $lesson->ID,
15124 ];
15125 }
15126 }
15127 }
15128 return [
15129 'options' => $options,
15130 'hasMore' => $lesson_count > $limit && $lesson_count > $offset,
15131 ];
15132 }
15133
15134 /**
15135 * Get Masteriyo LMS Quiz.
15136 *
15137 * @param array $data data.
15138 *
15139 * @return array
15140 */
15141 public function search_masteriyo_lms_quizs( $data ) {
15142
15143 $course_id = $data['dynamic'];
15144 $page = $data['page'];
15145 $limit = Utilities::get_search_page_limit();
15146 $offset = $limit * ( $page - 1 );
15147 $options = [];
15148 $args = [
15149 'post_type' => 'mto-quiz',
15150 'posts_per_page' => $limit,
15151 'offset' => $offset,
15152 'orderby' => 'title',
15153 'order' => 'ASC',
15154 'post_status' => 'publish',
15155 'meta_query' => [
15156 [
15157 'key' => '_course_id',
15158 'value' => $course_id,
15159 'compare' => '=',
15160 ],
15161 ],
15162 ];
15163
15164 $quizs = get_posts( $args );
15165 $quiz_count = wp_count_posts( 'mto-quiz' )->publish;
15166 if ( ! empty( $quizs ) ) {
15167 if ( is_array( $quizs ) ) {
15168 foreach ( $quizs as $quiz ) {
15169 $options[] = [
15170 'label' => $quiz->post_title,
15171 'value' => $quiz->ID,
15172 ];
15173 }
15174 }
15175 }
15176 return [
15177 'options' => $options,
15178 'hasMore' => $quiz_count > $limit && $quiz_count > $offset,
15179 ];
15180 }
15181
15182 /**
15183 * Search Masteriyo LMS data.
15184 *
15185 * @param array $data data.
15186 * @return array|void|mixed
15187 */
15188 public function search_masteriyo_lms_last_data( $data ) {
15189 global $wpdb;
15190 $post_type = $data['post_type'];
15191 $trigger = $data['search_term'];
15192 $context = [];
15193
15194 $post_id = -1;
15195 if ( 'course_completed' === $trigger ) {
15196 $post_id = $data['filter']['course_id']['value'];
15197 } elseif ( 'lesson_completed' === $trigger ) {
15198 $post_id = $data['filter']['lesson_id']['value'];
15199 } elseif ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
15200 $post_id = $data['filter']['quiz_id']['value'];
15201 }
15202
15203 if ( 'course_completed' === $trigger || 'lesson_completed' === $trigger ) {
15204 if ( -1 === $post_id ) {
15205 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}masteriyo_user_activities as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.item_id WHERE postmeta.activity_status='completed' AND posts.post_type=%s order by postmeta.id DESC LIMIT 1", $post_type ) );
15206 } else {
15207 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}masteriyo_user_activities as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.item_id WHERE postmeta.item_id = %s AND postmeta.activity_status='completed' AND posts.post_type=%s order by postmeta.id DESC LIMIT 1", $post_id, $post_type ) );
15208 }
15209 }
15210
15211 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
15212 if ( -1 === $post_id ) {
15213 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}masteriyo_user_activities as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.item_id WHERE postmeta.activity_status='completed' AND postmeta.activity_type='quiz' AND posts.post_type=%s order by postmeta.user_item_id DESC LIMIT 1", $post_type ) );
15214 } else {
15215 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}masteriyo_user_activities as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.item_id WHERE postmeta.item_id=%s AND postmeta.activity_type='quiz' OR postmeta.activity_status='completed' AND posts.post_type=%s order by postmeta.id DESC LIMIT 1", $post_id, $post_type ) );
15216 }
15217 if ( ! empty( $result ) ) {
15218 $resultt = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}masteriyo_quiz_attempts WHERE quiz_id=%d AND user_id=%d order by id DESC LIMIT 1", $result[0]->item_id, $result[0]->user_id ) );
15219
15220 if ( function_exists( 'masteriyo_get_quiz' ) ) {
15221 $quiz = masteriyo_get_quiz( $resultt[0]->quiz_id );
15222 if ( is_object( $quiz ) && method_exists( $quiz, 'get_pass_mark' ) ) {
15223 if ( 'quiz_completed' === $trigger && $resultt[0]->earned_marks < $quiz->get_pass_mark() ) {
15224 $result = [];
15225 } elseif ( 'quiz_failed' === $trigger && $resultt[0]->earned_marks > $quiz->get_pass_mark() ) {
15226 $result = [];
15227 }
15228 }
15229 }
15230 }
15231 }
15232
15233 if ( ! empty( $result ) ) {
15234 $result_item_id = $result[0]->item_id;
15235 $result_user_id = $result[0]->user_id;
15236 $quiz_attempt = '';
15237 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
15238 $resultt = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}masteriyo_quiz_attempts WHERE quiz_id=%d AND user_id=%d order by id DESC LIMIT 1", $result[0]->item_id, $result[0]->user_id ) );
15239 $quiz_attempt = $resultt[0]->id;
15240 }
15241 switch ( $trigger ) {
15242 case 'course_completed':
15243 if ( function_exists( 'masteriyo_get_course' ) ) {
15244 $course = masteriyo_get_course( $result_item_id );
15245 $context_data = array_merge(
15246 WordPress::get_user_context( $result_user_id ),
15247 $course->get_data()
15248 );
15249 $context_data['course_id'] = $result_item_id;
15250 }
15251 break;
15252 case 'lesson_completed':
15253 if ( function_exists( 'masteriyo_get_lesson' ) ) {
15254 $lesson = masteriyo_get_lesson( $result_item_id );
15255 $context_data = array_merge(
15256 WordPress::get_user_context( $result_user_id ),
15257 $lesson->get_data()
15258 );
15259 $context_data['lesson_id'] = $result_item_id;
15260 }
15261 break;
15262 case 'quiz_completed':
15263 case 'quiz_failed':
15264 if ( function_exists( 'masteriyo_get_quiz' ) ) {
15265 $quiz = masteriyo_get_quiz( $result_item_id );
15266 $context_data = WordPress::get_user_context( $result_user_id );
15267 if ( function_exists( 'masteriyo_get_quiz_attempt' ) ) {
15268 $attempt = masteriyo_get_quiz_attempt( $quiz_attempt );
15269 $context_data['quiz_id'] = $result_item_id;
15270 $context_data['course_id'] = $quiz->get_course_id();
15271 $context_data['quiz'] = $quiz->get_data();
15272 $context_data['attempt'] = $attempt->get_data();
15273 }
15274 }
15275 break;
15276 default:
15277 return;
15278 }
15279 if ( ! empty( $context_data ) ) {
15280 $context['pluggable_data'] = $context_data;
15281 $context['response_type'] = 'live';
15282 }
15283 } elseif ( empty( $result ) ) {
15284 switch ( $trigger ) {
15285 case 'course_completed':
15286 $sample_data = '{"pluggable_data":{"wp_user_id": 1,"user_login": "admin","display_name": "admin","user_firstname": "test","user_lastname": "test","user_email": "john@d.com","user_role": ["customer"],"id": 6636,"name": "Modes Master Class","slug": "modes-master-class-2","date_created": {"date": "2023-10-20 06:09:15.000000","timezone_type": 1,"timezone": "+00:00"},"date_modified": {"date": "2023-10-21 15:22:29.000000","timezone_type": 1,"timezone": "+00:00"},"status": "publish","menu_order": 0,"featured": false,"catalog_visibility": "visible","description": "Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words.","short_description": "","post_password": "","author_id": 1,"parent_id": 0,"reviews_allowed": true,"date_on_sale_from": null,"date_on_sale_to": null,"price": "0","regular_price": "0","sale_price": "","price_type": "free","category_ids": [106,107],"tag_ids": [],"difficulty_id": 0,"featured_image": 6616,"rating_counts": [],"average_rating": "0","review_count": 0,"enrollment_limit": 0,"duration": 360,"access_mode": "open","billing_cycle": "","show_curriculum": true,"purchase_note": "","highlights": "<li>Suscipit tortor eget felis.<\/li><li>Curabitur arcu erat idimper.<\/li><li>Lorem ipsum dolor sit amet.<\/li>","is_ai_created": false,"is_creating": false,"meta_data": []},"response_type":"sample"} ';
15287 break;
15288 case 'lesson_completed':
15289 $sample_data = '{"pluggable_data":{"wp_user_id": 1,"user_login": "admin","display_name": "admin","user_firstname": "test","user_lastname": "test","user_email": "john@d.com","user_role": ["customer"],"id": 6636,"name": "Lesson 1","slug": "lesson-1","date_created": {"date": "2023-10-20 06:09:15.000000","timezone_type": 1,"timezone": "+00:00"},"date_modified": {"date": "2023-10-21 15:22:29.000000","timezone_type": 1,"timezone": "+00:00"},"status": "publish","menu_order": 0,"featured": false,"catalog_visibility": "visible","description": "new lesson","short_description": "","post_password": "","author_id": 1,"parent_id": 0,"course_id": 6594,"reviews_allowed": true,"featured_image": 6616,"rating_counts": [],"average_rating": "0","review_count": 0,"meta_data": [],"lesson_id": "6596"},"response_type":"sample"} ';
15290 break;
15291 case 'quiz_completed':
15292 case 'quiz_failed':
15293 $sample_data = '{"pluggable_data":{"wp_user_id": 18,"user_login": "john@yopmail.com","display_name": "johns john","user_firstname": "johns","user_lastname": "john","user_email": "john@d.com","user_role": ["customer"],"quiz_id": "6654","course_id": 6634,"quiz": {"id": 6654,"name": "New Quiz","slug": "new-quiz","date_created": {"date": "2023-10-23 16:36:14.000000","timezone_type": 1,"timezone": "+00:00"},"date_modified": {"date": "2023-10-23 17:10:42.000000","timezone_type": 1,"timezone": "+00:00"},"parent_id": 6630,"course_id": 6634,"author_id": 0,"menu_order": 4,"status": "publish","description": "","short_description": "","pass_mark": 2,"full_mark": 100,"duration": 0,"attempts_allowed": 0,"questions_display_per_page": 0,"meta_data": []},"attempt": {"id": 17,"course_id": 6634,"quiz_id": 6654,"user_id": "18","total_questions": 2,"total_answered_questions": 2,"total_marks": "100.00","total_attempts": 1,"total_correct_answers": 1,"total_incorrect_answers": 1,"earned_marks": "1.00","answers": {"6655": {"answered": "False","correct": false},"6656": {"answered": "False","correct": true}},"attempt_status": "attempt_ended","attempt_started_at": {"date": "2023-10-24 09:34:16.000000","timezone_type": 1,"timezone": "+00:00"},"attempt_ended_at": {"date": "2023-10-24 09:34:29.000000","timezone_type": 1,"timezone": "+00:00"},"meta_data": []}},"response_type":"sample"}';
15294 break;
15295 default:
15296 return;
15297 }
15298 $context = json_decode( $sample_data, true );
15299 }
15300
15301 return $context;
15302 }
15303
15304 /**
15305 * Search learndash user added in group.
15306 *
15307 * @param array $data data.
15308 * @return array|void
15309 */
15310 public function search_pluggables_user_added_in_group( $data ) {
15311 $context = [];
15312
15313 if ( ! function_exists( 'learndash_get_groups_user_ids' ) ) {
15314 return;
15315 }
15316 $term = $data['search_term'];
15317 $group_id = $data['filter']['sfwd_group_id']['value'];
15318
15319 if ( 'user_added_group' == $term ) {
15320 if ( -1 === $group_id ) {
15321 $args = [
15322 'numberposts' => 1,
15323 'orderby' => 'rand',
15324 'post_type' => 'groups',
15325 ];
15326 $posts = get_posts( $args );
15327 $random_value = $posts[0]->ID;
15328 $group_users = learndash_get_groups_user_ids( $random_value );
15329 $group_id = $random_value;
15330 } else {
15331 $group_users = learndash_get_groups_user_ids( $group_id );
15332 }
15333 $users = get_users(
15334 [
15335 'meta_key' => 'learndash_group_' . $group_id . '_enrolled_at',
15336 'orderby' => 'meta_value',
15337 'order' => 'DESC',
15338 'number' => 1,
15339 'include' => $group_users,
15340 ]
15341 );
15342 } elseif ( 'user_removed_group' == $term ) {
15343 if ( -1 === $group_id ) {
15344 $args = [
15345 'numberposts' => 1,
15346 'orderby' => 'rand',
15347 'post_type' => 'groups',
15348 ];
15349 $posts = get_posts( $args );
15350 $random_value = $posts[0]->ID;
15351 $group_id = $random_value;
15352 }
15353 $args = [
15354 'meta_query' => [
15355 'relation' => 'AND',
15356 [
15357 'key' => 'group_' . $group_id . '_access_from',
15358 'compare' => 'NOT EXISTS',
15359 ],
15360 [
15361 'key' => 'learndash_group_' . $group_id . '_enrolled_at',
15362 'compare' => 'EXISTS',
15363 ],
15364 ],
15365 'orderby' => 'meta_value',
15366 'order' => 'DESC',
15367 'number' => 1,
15368 ];
15369 $users = get_users( $args );
15370 } elseif ( 'leader_added_group' == $term || 'leader_removed_group' == $term ) {
15371 if ( -1 === $group_id ) {
15372 $args = [
15373 'numberposts' => 1,
15374 'fields' => 'ids',
15375 'orderby' => 'rand',
15376 'post_type' => 'groups',
15377 ];
15378 $posts = get_posts( $args );
15379 $random_key = array_rand( $posts );
15380 $random_value = $posts[ $random_key ];
15381 $group_id = $random_value;
15382 }
15383 $user_query_args = [
15384 'orderby' => 'learndash_group_leaders_' . intval( $group_id ),
15385 'order' => 'DESC',
15386 'meta_query' => [
15387 [
15388 'key' => 'learndash_group_leaders_' . intval( $group_id ),
15389 'value' => intval( $group_id ),
15390 'compare' => '=',
15391 'type' => 'NUMERIC',
15392 ],
15393 ],
15394 ];
15395 $users = get_users( $user_query_args );
15396 }
15397
15398 if ( ! empty( $users ) ) {
15399 $context = WordPress::get_user_context( $users[0]->ID );
15400 $context['sfwd_group_id'] = $group_id;
15401 $context['group_title'] = get_the_title( $group_id );
15402 $context['group_url'] = get_permalink( $group_id );
15403 $context['group_featured_image_id'] = get_post_meta( $group_id, '_thumbnail_id', true );
15404 $context['group_featured_image_url'] = get_the_post_thumbnail_url( $group_id );
15405 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
15406 $group_courses_id = learndash_group_enrolled_courses( $group_id );
15407 if ( ! empty( $group_courses_id ) ) {
15408 foreach ( $group_courses_id as $key => $course_id ) {
15409 $context['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
15410 }
15411 }
15412 }
15413 $context['response_type'] = 'live';
15414 } else {
15415 $context = WordPress::get_sample_user_context();
15416 $context['group_title'] = 'Test Group';
15417 $context['sfwd_group_id'] = 112;
15418 $context['group_url'] = 'https://example.com/test-group';
15419 $context['group_featured_image_id'] = 113;
15420 $context['group_featured_image_url'] = 'https://example.com/test-group-img';
15421 $context['group_courses'] = [
15422 [
15423 'ID' => 7915,
15424 'title' => 'Example Course',
15425 'URL' => 'https://example.com/courses/example-course/',
15426 'status' => 'publish',
15427 'featured_image_id' => '',
15428 'featured_image_url' => false,
15429 ],
15430 ];
15431 $context['response_type'] = 'sample';
15432 }
15433
15434 $context['pluggable_data'] = $context;
15435 return $context;
15436 }
15437
15438 /**
15439 * Search learndash user enrolled in course.
15440 *
15441 * @param array $data data.
15442 * @return array|void
15443 */
15444 public function search_pluggables_user_enrolled_course( $data ) {
15445 global $wpdb;
15446 $context = [];
15447
15448 if ( ! function_exists( 'ld_course_access_expires_on' ) ||
15449 ! function_exists( 'learndash_group_enrolled_courses' ) ||
15450 ! function_exists( 'learndash_get_lesson_list' ) ) {
15451 return;
15452 }
15453 $term = $data['search_term'];
15454 $course_id = isset( $data['filter']['sfwd_course_id'] ) ? $data['filter']['sfwd_course_id']['value'] : '';
15455 $group_id = isset( $data['filter']['sfwd_group_id'] ) ? $data['filter']['sfwd_group_id']['value'] : '';
15456
15457 if ( 'course_enrolled' == $term ) {
15458 if ( -1 === $course_id ) {
15459 $courses = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='access' AND activity.activity_status= %d order by activity_id DESC LIMIT 1", 0 ) );
15460 } else {
15461 $courses = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='access' AND activity.activity_status= %d AND activity.post_id= %d AND activity.course_id= %d order by activity_id DESC LIMIT 1", 0, $course_id, $course_id ) );
15462 }
15463 } elseif ( 'course_unenrolled' == $term ) {
15464 if ( -1 === $course_id ) {
15465 $courses = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='access' AND activity.activity_status= %d order by activity_id DESC LIMIT 1", 0 ) );
15466 $course_id = $courses[0]->course_id;
15467 }
15468 $args = [
15469 'meta_query' => [
15470 'relation' => 'AND',
15471 [
15472 'key' => 'course_' . $course_id . '_access_from',
15473 'compare' => 'NOT EXISTS',
15474 ],
15475 [
15476 'key' => 'learndash_course_' . $course_id . '_enrolled_at',
15477 'compare' => 'EXISTS',
15478 ],
15479 ],
15480 ];
15481 $users = get_users( $args );
15482 } elseif ( 'course_access_expired' == $term ) {
15483 if ( -1 === $course_id ) {
15484 $course_args = [
15485 'post_type' => 'sfwd-courses',
15486 'posts_per_page' => 1,
15487 'orderby' => 'rand',
15488 'order' => 'ASC',
15489 'post_status' => 'publish',
15490 ];
15491 $courses_posts = get_posts( $course_args );
15492 $course_id = $courses_posts[0]->ID;
15493 }
15494 $args = [
15495 'meta_query' => [
15496 [
15497 'key' => 'learndash_course_expired_' . $course_id,
15498 'compare' => 'EXISTS',
15499 ],
15500 ],
15501 ];
15502 $users = get_users( $args );
15503 } elseif ( 'group_course_completed' == $term ) {
15504 if ( -1 === $group_id ) {
15505 $courses = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='group_progress' AND activity.activity_status= %d order by activity_id DESC LIMIT 1", 1 ) );
15506 } else {
15507 $courses = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity as activity JOIN {$wpdb->prefix}posts as post ON activity.post_id=post.ID WHERE activity.activity_type ='group_progress' AND activity.activity_status= %d AND activity.post_id= %d order by activity_id DESC LIMIT 1", 1, $group_id ) );
15508 }
15509 $activity_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity_meta WHERE activity_id = %d", $courses[0]->activity_id ) );
15510 } elseif ( 'course_group_added' == $term ) {
15511 if ( -1 === $group_id ) {
15512 $args = [
15513 'numberposts' => 1,
15514 'fields' => 'ids',
15515 'orderby' => 'rand',
15516 'post_type' => 'groups',
15517 ];
15518 $posts = get_posts( $args );
15519 $random_key = array_rand( $posts );
15520 $random_value = $posts[ $random_key ];
15521 $group_id = $random_value;
15522 }
15523 $courses = learndash_group_enrolled_courses( $group_id );
15524 } elseif ( 'assignment_submitted' == $term ) {
15525 $course_id = isset( $data['filter']['sfwd-courses'] ) ? $data['filter']['sfwd-courses']['value'] : '';
15526 $lesson_id = $data['filter']['sfwd_lesson_topic_id']['value'];
15527 if ( -1 === $course_id && -1 === $lesson_id ) {
15528 $args = [
15529 'post_type' => 'sfwd-assignment',
15530 'posts_per_page' => 1,
15531 'order' => 'DESC',
15532 'post_status' => 'publish',
15533 ];
15534 } else {
15535 if ( -1 === $course_id ) {
15536 $courses = get_posts(
15537 [
15538 'posts_per_page' => - 1,
15539 'post_type' => 'sfwd-courses',
15540 'post_status' => 'publish',
15541 'fields' => 'ids',
15542 ]
15543 );
15544 $course_key = array_rand( $courses );
15545 $course_id = $courses[ $course_key ];
15546 }
15547 if ( -1 === $lesson_id ) {
15548 $args = [
15549 'post_type' => 'sfwd-assignment',
15550 'posts_per_page' => 1,
15551 'order' => 'DESC',
15552 'post_status' => 'publish',
15553 'meta_query' => [
15554 [
15555 'key' => 'course_id',
15556 'value' => $course_id,
15557 'compare' => '=',
15558 ],
15559 ],
15560 ];
15561 } else {
15562 $args = [
15563 'post_type' => 'sfwd-assignment',
15564 'posts_per_page' => 1,
15565 'order' => 'DESC',
15566 'post_status' => 'publish',
15567 'meta_query' => [
15568 'relation' => 'AND',
15569 [
15570 'key' => 'lesson_id',
15571 'value' => $lesson_id,
15572 'compare' => '=',
15573 ],
15574 [
15575 'key' => 'course_id',
15576 'value' => $course_id,
15577 'compare' => '=',
15578 ],
15579 ],
15580 ];
15581 }
15582 }
15583 $assignments = get_posts( $args );
15584 } elseif ( 'assignment_graded' == $term ) {
15585 $course_id = isset( $data['filter']['sfwd-courses'] ) ? $data['filter']['sfwd-courses']['value'] : '';
15586 $lesson_id = $data['filter']['sfwd_lesson_topic_id']['value'];
15587 if ( -1 === $course_id && -1 === $lesson_id ) {
15588 $args = [
15589 'post_type' => 'sfwd-assignment',
15590 'posts_per_page' => 1,
15591 'order' => 'DESC',
15592 'post_status' => 'publish',
15593 'meta_query' => [
15594 [
15595 'key' => 'approval_status',
15596 'compare' => 'EXISTS',
15597 ],
15598 ],
15599 ];
15600 } else {
15601 if ( -1 === $course_id ) {
15602 $courses = get_posts(
15603 [
15604 'posts_per_page' => - 1,
15605 'post_type' => 'sfwd-courses',
15606 'post_status' => 'publish',
15607 'fields' => 'ids',
15608 ]
15609 );
15610 $course_key = array_rand( $courses );
15611 $course_id = $courses[ $course_key ];
15612 }
15613 if ( -1 === $lesson_id ) {
15614 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
15615 if ( ! empty( $lessons ) ) {
15616 $random_key = array_rand( $lessons );
15617 $random_value = $lessons[ $random_key ];
15618 $lesson_id = $random_value->ID;
15619 }
15620 }
15621 $args = [
15622 'post_type' => 'sfwd-assignment',
15623 'posts_per_page' => 1,
15624 'order' => 'DESC',
15625 'post_status' => 'publish',
15626 'meta_query' => [
15627 'relation' => 'AND',
15628 [
15629 'key' => 'lesson_id',
15630 'value' => $lesson_id,
15631 'compare' => '=',
15632 ],
15633 [
15634 'key' => 'course_id',
15635 'value' => $course_id,
15636 'compare' => '=',
15637 ],
15638 [
15639 'key' => 'approval_status',
15640 'compare' => 'EXISTS',
15641 ],
15642 ],
15643 ];
15644 }
15645 $assignments = get_posts( $args );
15646 }
15647
15648 if ( 'assignment_submitted' == $term || 'assignment_graded' == $term ) {
15649 if ( ! empty( $assignments ) ) {
15650 $context_data = WordPress::get_user_context( (int) $assignments[0]->post_author );
15651 $context_data['assignment_id'] = $assignments[0]->ID;
15652 $context_data['assignment_title'] = $assignments[0]->post_title;
15653 $context_data['assignment_url'] = get_post_meta( $assignments[0]->ID, 'file_link', true );
15654 $context_data['lesson_id'] = get_post_meta( $assignments[0]->ID, 'lesson_id', true );
15655 $context_data['course_id'] = get_post_meta( $assignments[0]->ID, 'course_id', true );
15656 $context_data['points'] = get_post_meta( $assignments[0]->ID, 'points', true );
15657 $context['response_type'] = 'live';
15658 } else {
15659 $context_data = WordPress::get_sample_user_context();
15660 $context_data['assignment_title'] = 'Test Assignment';
15661 $context_data['assignment_id'] = 112;
15662 $context_data['assignment_url'] = 'https://example.com/test_assignment.pdf';
15663 $context_data['lesson_id'] = 2;
15664 $context_data['course_id'] = 1;
15665 $context_data['points'] = '12';
15666 $context['response_type'] = 'sample';
15667 }
15668 } elseif ( 'course_unenrolled' == $term || 'course_access_expired' == $term ) {
15669 if ( ! empty( $users ) ) {
15670 $context_data = WordPress::get_user_context( $users[0]->ID );
15671 $context_data['sfwd_course_id'] = $course_id;
15672 $context_data['course_title'] = get_the_title( $course_id );
15673 $context_data['course_url'] = get_permalink( $course_id );
15674 $context_data['course_featured_image_id'] = get_post_meta( $course_id, '_thumbnail_id', true );
15675 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course_id );
15676 $timestamp = ld_course_access_expires_on( $course_id, $users[0]->ID );
15677 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
15678 $date_format = get_option( 'date_format' );
15679 if ( is_string( $date_format ) ) {
15680 $context_data['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
15681 }
15682 $context['response_type'] = 'live';
15683 } else {
15684 $context_data = WordPress::get_sample_user_context();
15685 $context_data['course_name'] = 'Test Course';
15686 $context_data['sfwd_course_id'] = 112;
15687 $context_data['course_url'] = 'https://example.com/test-course';
15688 $context_data['course_featured_image_id'] = 113;
15689 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
15690 $context_data['course_access_expiry_date'] = '2023-10-20';
15691 $context['response_type'] = 'sample';
15692 }
15693 } elseif ( 'course_group_added' == $term ) {
15694 if ( ! empty( $courses ) ) {
15695 $context_data['course_id'] = $courses[0];
15696 $context_data['course_title'] = get_the_title( $courses[0] );
15697 $context_data['course_url'] = get_permalink( $courses[0] );
15698 $context_data['course_featured_image_id'] = get_post_meta( $courses[0], '_thumbnail_id', true );
15699 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $courses[0] );
15700 $context_data['sfwd_group_id'] = $group_id;
15701 $context_data['group_name'] = get_the_title( $group_id );
15702 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
15703 $group_courses_id = learndash_group_enrolled_courses( $group_id );
15704 if ( ! empty( $group_courses_id ) ) {
15705 foreach ( $group_courses_id as $key => $course_id ) {
15706 $context_data['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
15707 }
15708 }
15709 }
15710 $context['response_type'] = 'live';
15711 } else {
15712 $context_data = WordPress::get_sample_user_context();
15713 $context_data['course_name'] = 'Test Course';
15714 $context_data['sfwd_course_id'] = 112;
15715 $context_data['course_url'] = 'https://example.com/test-course';
15716 $context_data['course_featured_image_id'] = 113;
15717 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
15718 $context_data['course_access_expiry_date'] = '2023-10-20';
15719 $context_data['sfwd_group_id'] = 12;
15720 $context_data['group_name'] = 'Test Group';
15721 $context_data['group_courses'] = [
15722 [
15723 'ID' => 7915,
15724 'title' => 'Example Course',
15725 'URL' => 'https://example.com/courses/example-course/',
15726 'status' => 'publish',
15727 'featured_image_id' => '',
15728 'featured_image_url' => false,
15729 ],
15730 ];
15731 $context['response_type'] = 'sample';
15732 }
15733 } elseif ( 'group_course_completed' == $term ) {
15734 if ( ! empty( $courses ) && ! empty( $activity_meta ) ) {
15735 $context_data = WordPress::get_user_context( $courses[0]->user_id );
15736 $context_data['sfwd_group_id'] = $courses[0]->post_id;
15737 $context_data['group_title'] = get_the_title( $courses[0]->post_id );
15738 $context_data['group_url'] = get_permalink( $courses[0]->post_id );
15739 $context_data['group_featured_image_id'] = get_post_meta( $courses[0]->post_id, '_thumbnail_id', true );
15740 $context_data['group_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->post_id );
15741 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
15742 $group_courses_id = learndash_group_enrolled_courses( $courses[0]->post_id );
15743 if ( ! empty( $group_courses_id ) ) {
15744 foreach ( $group_courses_id as $key => $course_id ) {
15745 $context_data['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
15746 }
15747 }
15748 }
15749 $course_ids = null;
15750 foreach ( $activity_meta as $item ) {
15751 if ( 'course_ids' === $item->activity_meta_key ) {
15752 $course_ids = unserialize( $item->activity_meta_value );
15753 break;
15754 }
15755 }
15756 if ( ! empty( $course_ids ) && is_array( $course_ids ) ) {
15757 foreach ( $course_ids as $key => $course_id ) {
15758 if ( is_int( $course_id ) ) {
15759 $args = [
15760 'include' => [ $courses[0]->user_id ],
15761 'meta_query' => [
15762 [
15763 'key' => 'course_completed_' . $course_id,
15764 'compare' => 'EXISTS',
15765 ],
15766 ],
15767 ];
15768 $users = get_users( $args );
15769 if ( ! empty( $users ) ) {
15770 $context_data[ 'completed ' . $key ]['course_id'] = $course_id;
15771 $context_data[ 'completed ' . $key ]['course_title'] = get_the_title( $course_id );
15772 $context_data[ 'completed ' . $key ]['course_url'] = get_permalink( $course_id );
15773 $context_data[ 'completed ' . $key ]['course_featured_image_id'] = get_post_meta( $course_id, '_thumbnail_id', true );
15774 $context_data[ 'completed ' . $key ]['course_featured_image_url'] = get_the_post_thumbnail_url( $course_id );
15775 $timestamp = ld_course_access_expires_on( $course_id, $courses[0]->user_id );
15776 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
15777 $date_format = get_option( 'date_format' );
15778 if ( is_string( $date_format ) ) {
15779 $context_data[ 'completed ' . $key ]['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
15780 }
15781 }
15782 }
15783 }
15784 }
15785 $context['response_type'] = 'live';
15786 } else {
15787 $context_data = WordPress::get_sample_user_context();
15788 $context_data['sfwd_group_id'] = 112;
15789 $context_data['group_title'] = 'Test Group';
15790 $context_data['group_url'] = 113;
15791 $context_data['group_featured_image_id'] = 11;
15792 $context_data['group_featured_image_url'] = 'https://example.com/test-group-img';
15793 $context_data['completed 0'] = [
15794 'course_id' => 10,
15795 'course_title' => 'Test Course',
15796 'course_url' => 'https://example.com/test-course',
15797 'course_featured_image_id' => 14,
15798 'course_featured_image_url' => 'https://example.com/test-course-img',
15799 ];
15800 $context_data['group_courses'] = [
15801 [
15802 'ID' => 7915,
15803 'title' => 'Example Course',
15804 'URL' => 'https://example.com/courses/example-course/',
15805 'status' => 'publish',
15806 'featured_image_id' => '',
15807 'featured_image_url' => false,
15808 ],
15809 ];
15810 $context['response_type'] = 'sample';
15811 }
15812 } else {
15813 if ( ! empty( $courses ) ) {
15814 $context_data = WordPress::get_user_context( $courses[0]->user_id );
15815 $context_data['course_id'] = $courses[0]->course_id;
15816 $context_data['course_title'] = get_the_title( $courses[0]->course_id );
15817 $context_data['course_url'] = get_permalink( $courses[0]->course_id );
15818 $context_data['course_featured_image_id'] = get_post_meta( $courses[0]->course_id, '_thumbnail_id', true );
15819 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->course_id );
15820 $timestamp = ld_course_access_expires_on( $courses[0]->course_id, $courses[0]->user_id );
15821 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
15822 $date_format = get_option( 'date_format' );
15823 if ( is_string( $date_format ) ) {
15824 $context_data['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
15825 }
15826 if ( $courses[0]->post_id ) {
15827 $context_data['sfwd_group_id'] = $courses[0]->post_id;
15828 $context_data['group_title'] = get_the_title( $courses[0]->post_id );
15829 $context_data['group_url'] = get_permalink( $courses[0]->post_id );
15830 $context_data['group_featured_image_id'] = get_post_meta( $courses[0]->post_id, '_thumbnail_id', true );
15831 $context_data['group_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->post_id );
15832 }
15833 $context['response_type'] = 'live';
15834 } else {
15835 $context_data = WordPress::get_sample_user_context();
15836 $context_data['course_name'] = 'Test Course';
15837 $context_data['sfwd_course_id'] = 112;
15838 $context_data['course_url'] = 'https://example.com/test-course';
15839 $context_data['course_featured_image_id'] = 113;
15840 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
15841 $context_data['course_access_expiry_date'] = '2023-10-20';
15842 $context['response_type'] = 'sample';
15843 }
15844 }
15845
15846 $context['pluggable_data'] = $context_data;
15847 return $context;
15848 }
15849
15850 /**
15851 * Search LearnDash Lesson Topic List.
15852 *
15853 * @param array $data Search Params.
15854 *
15855 * @since 1.0.0
15856 *
15857 * @return array
15858 */
15859 public function search_ld_lessons_topics_list( $data ) {
15860 $options = [];
15861 $course_id = $data['dynamic']['sfwd-courses'];
15862
15863 if ( ! function_exists( 'learndash_get_lesson_list' ) || ! function_exists( 'learndash_get_topic_list' ) ) {
15864 return [];
15865 }
15866
15867 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
15868 foreach ( $lessons as $lesson ) {
15869 $options[] = [
15870 'label' => $lesson->post_title,
15871 'value' => $lesson->ID,
15872 ];
15873 $topics = learndash_get_topic_list( $lesson->ID, $course_id );
15874 foreach ( $topics as $topic ) {
15875 $options[] = [
15876 'label' => $topic->post_title,
15877 'value' => $topic->ID,
15878 ];
15879 }
15880 }
15881
15882 return [
15883 'options' => $options,
15884 'hasMore' => false,
15885 ];
15886 }
15887
15888 /**
15889 * Search LearnDash Quiz List.
15890 *
15891 * @param array $data Search Params.
15892 *
15893 * @since 1.0.0
15894 *
15895 * @return array
15896 */
15897 public function search_ld_quiz_list( $data ) {
15898 $options = [];
15899 $course_id = $data['dynamic']['sfwd-courses'];
15900 $lesson_id = $data['dynamic']['sfwd_lessons_topics'];
15901
15902 if ( ! function_exists( 'learndash_get_course_quiz_list' ) || ! function_exists( 'learndash_get_lesson_quiz_list' ) ) {
15903 return [];
15904 }
15905
15906 $quizzes = learndash_get_course_quiz_list( $course_id );
15907 $quizzes = array_merge( $quizzes, learndash_get_lesson_quiz_list( $lesson_id, null, $course_id ) );
15908 if ( ! empty( $quizzes ) ) {
15909 foreach ( $quizzes as $quiz ) {
15910 $options[] = [
15911 'label' => $quiz['post']->post_title,
15912 'value' => $quiz['post']->ID,
15913
15914 ];
15915 }
15916 }
15917
15918 return [
15919 'options' => $options,
15920 'hasMore' => false,
15921 ];
15922 }
15923
15924 /**
15925 * Search LearnDash Quiz Essay Question List.
15926 *
15927 * @param array $data Search Params.
15928 *
15929 * @since 1.0.0
15930 *
15931 * @return array
15932 */
15933 public function search_ld_quiz_essay_question_list( $data ) {
15934 $options = [];
15935 $quiz_id = $data['dynamic'];
15936
15937 if ( ! function_exists( 'learndash_get_quiz_questions' ) ) {
15938 return [];
15939 }
15940
15941 if ( 0 < $quiz_id ) {
15942 $quiz_question_ids = learndash_get_quiz_questions( $quiz_id );
15943 if ( ! empty( $quiz_question_ids ) ) {
15944 foreach ( $quiz_question_ids as $question_post_id => $question_pro_id ) {
15945 $question_type = get_post_meta( $question_post_id, 'question_type', true );
15946 if ( is_string( $question_type ) && 'essay' === $question_type ) {
15947 $title = html_entity_decode( get_the_title( $question_post_id ), ENT_QUOTES, 'UTF-8' );
15948 $options[] = [
15949 'label' => $title,
15950 'value' => $question_post_id,
15951 ];
15952 }
15953 }
15954 }
15955 }
15956
15957 return [
15958 'options' => $options,
15959 'hasMore' => false,
15960 ];
15961 }
15962
15963 /**
15964 * Search LearnDash Lessons List.
15965 *
15966 * @param array $data Search Params.
15967 *
15968 * @since 1.0.0
15969 *
15970 * @return array
15971 */
15972 public function search_ld_lessons_list( $data ) {
15973 $options = [];
15974 $course_id = $data['dynamic']['sfwd-courses'];
15975
15976 if ( ! function_exists( 'learndash_get_lesson_list' ) ) {
15977 return [];
15978 }
15979
15980 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
15981 foreach ( $lessons as $lesson ) {
15982 $options[] = [
15983 'label' => $lesson->post_title,
15984 'value' => $lesson->ID,
15985 ];
15986 }
15987 return [
15988 'options' => $options,
15989 'hasMore' => false,
15990 ];
15991 }
15992
15993 /**
15994 * Search LearnDash Topics List.
15995 *
15996 * @param array $data Search Params.
15997 *
15998 * @since 1.0.0
15999 *
16000 * @return array
16001 */
16002 public function search_ld_topics_list( $data ) {
16003 $options = [];
16004 $course_id = $data['dynamic']['sfwd-courses'];
16005 $lesson_id = $data['dynamic']['sfwd-lessons'];
16006
16007 if ( ! function_exists( 'learndash_get_topic_list' ) ) {
16008 return [];
16009 }
16010
16011 $topics = learndash_get_topic_list( $lesson_id, $course_id );
16012 foreach ( $topics as $topic ) {
16013 $options[] = [
16014 'label' => $topic->post_title,
16015 'value' => $topic->ID,
16016 ];
16017 }
16018 return [
16019 'options' => $options,
16020 'hasMore' => false,
16021 ];
16022 }
16023
16024 /**
16025 * Search LearnDash Assignments List.
16026 *
16027 * @param array $data Search Params.
16028 *
16029 * @since 1.0.0
16030 *
16031 * @return array
16032 */
16033 public function search_ld_assignments_list( $data ) {
16034 $options = [];
16035 $course_id = $data['dynamic']['sfwd-courses'];
16036 $lesson_id = $data['dynamic']['sfwd_lesson_topic_id'];
16037
16038 $args = [
16039 'post_type' => 'sfwd-assignment',
16040 'numberposts' => - 1,
16041 ];
16042 $meta_query = [];
16043 if ( ! empty( $course_id ) ) {
16044 $meta_query[] = [
16045 'key' => 'course_id',
16046 'value' => (int) $course_id,
16047 'compare' => '=',
16048 ];
16049 }
16050 if ( ! empty( $lesson_id ) ) {
16051 $meta_query[] = [
16052 'key' => 'lesson_id',
16053 'value' => (int) $lesson_id,
16054 'compare' => '=',
16055 ];
16056 }
16057 if ( ! empty( $meta_query ) ) {
16058 $args['meta_query'] = $meta_query;
16059 if ( count( $meta_query ) > 1 ) {
16060 $args['meta_query']['relation'] = 'AND';
16061 }
16062 }
16063 $assignments = get_posts( $args );
16064 foreach ( $assignments as $assignment ) {
16065 $options[] = [
16066 'label' => $assignment->post_title,
16067 'value' => $assignment->ID,
16068 ];
16069 }
16070 return [
16071 'options' => $options,
16072 'hasMore' => false,
16073 ];
16074 }
16075
16076 /**
16077 * Search post by post type.
16078 *
16079 * @param array $data Search Params.
16080 *
16081 * @since 1.0.0
16082 *
16083 * @return array
16084 */
16085 public function search_edd_prices( $data ) {
16086 $options = [];
16087 $downlaod_id = $data['dynamic']['download_id'];
16088
16089 $variable_prices = get_post_meta( $downlaod_id, 'edd_variable_prices', true );
16090 if ( ! empty( $variable_prices ) && is_array( $variable_prices ) ) {
16091 foreach ( $variable_prices as $price_id => $price ) {
16092 if ( isset( $price['name'] ) ) {
16093 $options[] = [
16094 'label' => $price['name'] . '(' . $price['amount'] . ')',
16095 'value' => $price['index'],
16096 ];
16097 }
16098 }
16099 }
16100
16101 return [
16102 'options' => $options,
16103 'hasMore' => false,
16104 ];
16105
16106 }
16107
16108 /**
16109 * GetPowerful Docs list.
16110 *
16111 * @param array $data data.
16112 *
16113 * @return array
16114 */
16115 public function search_pfd_docs_list( $data ) {
16116
16117 $course_id = $data['dynamic'];
16118 $page = $data['page'];
16119 $limit = Utilities::get_search_page_limit();
16120 $offset = $limit * ( $page - 1 );
16121 $options = [];
16122 $args = [
16123 'post_type' => 'docs',
16124 'posts_per_page' => $limit,
16125 'offset' => $offset,
16126 'orderby' => 'title',
16127 'order' => 'ASC',
16128 'post_status' => 'publish',
16129 ];
16130
16131 $docs = get_posts( $args );
16132 $docs_count = wp_count_posts( 'docs' )->publish;
16133 if ( ! empty( $docs ) ) {
16134 if ( is_array( $docs ) ) {
16135 foreach ( $docs as $doc ) {
16136 $options[] = [
16137 'label' => $doc->post_title,
16138 'value' => $doc->ID,
16139 ];
16140 }
16141 }
16142 }
16143 return [
16144 'options' => $options,
16145 'hasMore' => $docs_count > $limit && $docs_count > $offset,
16146 ];
16147 }
16148
16149 /**
16150 * Search Powerful Docs last data.
16151 *
16152 * @param array $data data.
16153 * @return array|void|mixed
16154 */
16155 public function search_pfd_feedback_last_data( $data ) {
16156 $sample_data = '{"pluggable_data":{"feedback": "no","questions": "- I need help with something else...","comment": "help me out!!","doc_id": "2409","time": "2024-09-11 11:56:48","doc_name": "Sample doc","doc_link": "https://example.com","doc_author_email": "john@example.com"},"response_type":"sample"}';
16157 $context = json_decode( $sample_data, true );
16158
16159 return $context;
16160 }
16161
16162 /**
16163 * WooCommerce Coupon Discount type list.
16164 *
16165 * @param array $data data.
16166 *
16167 * @return array
16168 */
16169 public function search_woo_coupon_discount_type_list( $data ) {
16170 $options = [];
16171
16172 $discount_types = wc_get_coupon_types();
16173
16174 if ( ! empty( $discount_types ) ) {
16175 if ( is_array( $discount_types ) ) {
16176 foreach ( $discount_types as $key => $value ) {
16177 $options[] = [
16178 'label' => $value,
16179 'value' => $key,
16180 ];
16181 }
16182 }
16183 }
16184
16185 return [
16186 'options' => $options,
16187 'hasMore' => false,
16188 ];
16189 }
16190
16191 /**
16192 * WooCommerce Product list along with variation list.
16193 *
16194 * @param array $data data.
16195 *
16196 * @return array|void
16197 */
16198 public function search_woo_product_variation_list( $data ) {
16199 $options = [];
16200
16201 $page = $data['page'];
16202 $limit = Utilities::get_search_page_limit();
16203 $offset = $limit * ( $page - 1 );
16204
16205 if ( ! function_exists( 'wc_get_products' ) ) {
16206 return;
16207 }
16208 $products = wc_get_products(
16209 [
16210 'posts_per_page' => $limit,
16211 'offset' => $offset,
16212 'orderby' => 'date',
16213 'order' => 'DESC',
16214 ]
16215 );
16216
16217
16218 $prod_count = wp_count_posts( 'product' )->publish;
16219
16220 if ( ! empty( $products ) ) {
16221 if ( is_array( $products ) ) {
16222 foreach ( $products as $product ) {
16223 $options[] = [
16224 'label' => $product->get_name(),
16225 'value' => $product->get_id(),
16226 ];
16227 if ( $product->is_type( 'variable' ) ) {
16228 $variations = Utilities::get_product_variations( $product->get_id() );
16229 foreach ( $variations['result'] as $variation ) {
16230 $options[] = [
16231 'label' => $variation->post_title,
16232 'value' => $variation->ID,
16233 ];
16234 }
16235 }
16236 }
16237 }
16238 }
16239 return [
16240 'options' => $options,
16241 'hasMore' => $prod_count > $limit && $prod_count > $offset,
16242 ];
16243 }
16244
16245 /**
16246 * WooCommerce Product list along with variation list.
16247 *
16248 * @param array $data data.
16249 *
16250 * @return array
16251 */
16252 public function search_woo_coupon_list( $data ) {
16253 $options = [];
16254
16255 $page = $data['page'];
16256 $limit = Utilities::get_search_page_limit();
16257 $offset = $limit * ( $page - 1 );
16258
16259 $coupons = get_posts(
16260 [
16261 'posts_per_page' => - 1,
16262 'orderby' => 'name',
16263 'order' => 'asc',
16264 'post_type' => 'shop_coupon',
16265 'post_status' => 'publish',
16266 ]
16267 );
16268 $coupon_count = wp_count_posts( 'shop_coupon' )->publish;
16269
16270 if ( ! empty( $coupons ) ) {
16271 if ( is_array( $coupons ) ) {
16272 foreach ( $coupons as $coupon ) {
16273 $code = wc_format_coupon_code( $coupon->post_title );
16274 $options[] = [
16275 'label' => $code,
16276 'value' => $code,
16277 ];
16278 }
16279 }
16280 }
16281
16282 return [
16283 'options' => $options,
16284 'hasMore' => $coupon_count > $limit && $coupon_count > $offset,
16285 ];
16286 }
16287
16288 /**
16289 * Prepare LatePoint Bookings List.
16290 *
16291 * @param array $data Search Params.
16292 * @return array
16293 */
16294 public function search_lp_bookings_list( $data ) {
16295
16296 if ( ! class_exists( 'OsBookingHelper' ) ) {
16297 return [];
16298 }
16299
16300 $bookings = OsBookingHelper::get_bookings_for_select();
16301 $options = [];
16302
16303 if ( ! empty( $bookings ) ) {
16304 foreach ( $bookings as $key => $booking ) {
16305 $options[] = [
16306 'label' => $booking['label'],
16307 'value' => $booking['value'],
16308 ];
16309 }
16310 }
16311
16312 return [
16313 'options' => $options,
16314 'hasMore' => false,
16315 ];
16316 }
16317
16318 /**
16319 * Prepare LatePoint Services List.
16320 *
16321 * @param array $data Search Params.
16322 * @return array
16323 */
16324 public function search_lp_services_list( $data ) {
16325
16326 if ( ! class_exists( 'OsServiceHelper' ) ) {
16327 return [];
16328 }
16329
16330 $services = OsServiceHelper::get_services_list();
16331 $options = [];
16332
16333 if ( ! empty( $services ) ) {
16334 foreach ( $services as $key => $service ) {
16335 $options[] = [
16336 'label' => $service['label'],
16337 'value' => $service['value'],
16338 ];
16339 }
16340 }
16341
16342 return [
16343 'options' => $options,
16344 'hasMore' => false,
16345 ];
16346 }
16347
16348 /**
16349 * Prepare LatePoint Agents List.
16350 *
16351 * @param array $data Search Params.
16352 * @return array
16353 */
16354 public function search_lp_agents_list( $data ) {
16355
16356 if ( ! class_exists( 'OsAgentHelper' ) ) {
16357 return [];
16358 }
16359
16360 $agent_ids_for_service = OsAgentHelper::get_agent_ids_for_service_and_location( $data['dynamic'] );
16361 $agents = OsAgentHelper::get_agents_list();
16362 $options = [];
16363
16364 if ( ! empty( $agents ) ) {
16365 foreach ( $agents as $key => $agent ) {
16366 if ( in_array( $agent['value'], $agent_ids_for_service ) ) {
16367 $options[] = [
16368 'label' => $agent['label'],
16369 'value' => $agent['value'],
16370 ];
16371 }
16372 }
16373 }
16374
16375 return [
16376 'options' => $options,
16377 'hasMore' => false,
16378 ];
16379 }
16380
16381 /**
16382 * Prepare LatePoint Agents WP User List.
16383 *
16384 * @param array $data Search Params.
16385 * @return array
16386 */
16387 public function search_lp_user_agents_list( $data ) {
16388
16389 if ( ! class_exists( 'OsWpUserHelper' ) ) {
16390 return [];
16391 }
16392 $agents = OsWpUserHelper::get_wp_users_for_select( [ 'role' => 'latepoint_agent' ] );
16393 $options = [];
16394 if ( ! empty( $agents ) ) {
16395 $options = $agents;
16396 }
16397 return [
16398 'options' => $options,
16399 'hasMore' => false,
16400 ];
16401 }
16402
16403 /**
16404 * Prepare LatePoint Statuses List.
16405 *
16406 * @param array $data Search Params.
16407 * @return array
16408 */
16409 public function search_lp_statuses_list( $data ) {
16410
16411 if ( ! class_exists( 'OsBookingHelper' ) ) {
16412 return [];
16413 }
16414
16415 $statuses = OsBookingHelper::get_statuses_list();
16416 $options = [];
16417
16418 if ( ! empty( $statuses ) ) {
16419 foreach ( $statuses as $key => $label ) {
16420 $options[] = [
16421 'label' => $label,
16422 'value' => $key,
16423 ];
16424 }
16425 }
16426
16427 return [
16428 'options' => $options,
16429 'hasMore' => false,
16430 ];
16431 }
16432
16433 /**
16434 * Prepare LatePoint Customers List.
16435 *
16436 * @param array $data Search Params.
16437 * @return array
16438 */
16439 public function search_lp_customers_list( $data ) {
16440
16441 if ( ! class_exists( 'OsCustomerHelper' ) ) {
16442 return [];
16443 }
16444
16445 $customers = OsCustomerHelper::get_customers_for_select();
16446 $options = [];
16447
16448 if ( ! empty( $customers ) ) {
16449 foreach ( $customers as $key => $customer ) {
16450 $options[] = [
16451 'label' => $customer['label'],
16452 'value' => $customer['value'],
16453 ];
16454 }
16455 }
16456
16457 return [
16458 'options' => $options,
16459 'hasMore' => false,
16460 ];
16461 }
16462
16463 /**
16464 * Prepare SureForms Forms List.
16465 *
16466 * @param array $data Search Params.
16467 * @return array
16468 */
16469 public function search_sureforms_form_list( $data ) {
16470
16471 $options = [];
16472
16473 $page = $data['page'];
16474 $limit = Utilities::get_search_page_limit();
16475 $offset = $limit * ( $page - 1 );
16476
16477 $forms = get_posts(
16478 [
16479 'posts_per_page' => - 1,
16480 'orderby' => 'name',
16481 'order' => 'asc',
16482 'post_type' => 'sureforms_form',
16483 'post_status' => 'publish',
16484 ]
16485 );
16486 $form_count = wp_count_posts( 'sureforms_form' )->publish;
16487
16488 if ( ! empty( $forms ) ) {
16489 if ( is_array( $forms ) ) {
16490 foreach ( $forms as $form ) {
16491 $title = html_entity_decode( get_the_title( $form->ID ), ENT_QUOTES, 'UTF-8' );
16492 $options[] = [
16493 'label' => $title,
16494 'value' => $form->ID,
16495 ];
16496 }
16497 }
16498 }
16499
16500 return [
16501 'options' => $options,
16502 'hasMore' => $form_count > $limit && $form_count > $offset,
16503 ];
16504 }
16505
16506 /**
16507 * Prepare Academy Course List.
16508 *
16509 * @param array $data Search Params.
16510 * @return array
16511 */
16512 public function search_ac_lms_courses( $data ) {
16513
16514 $options = [];
16515
16516 $page = $data['page'];
16517 $limit = Utilities::get_search_page_limit();
16518 $offset = $limit * ( $page - 1 );
16519
16520 $courses = get_posts(
16521 [
16522 'posts_per_page' => - 1,
16523 'orderby' => 'name',
16524 'order' => 'asc',
16525 'post_type' => 'academy_courses',
16526 'post_status' => 'publish',
16527 ]
16528 );
16529 $course_count = wp_count_posts( 'academy_courses' )->publish;
16530
16531 if ( ! empty( $courses ) ) {
16532 if ( is_array( $courses ) ) {
16533 foreach ( $courses as $course ) {
16534 $options[] = [
16535 'label' => get_the_title( $course->ID ),
16536 'value' => $course->ID,
16537 ];
16538 }
16539 }
16540 }
16541
16542 return [
16543 'options' => $options,
16544 'hasMore' => $course_count > $limit && $course_count > $offset,
16545 ];
16546 }
16547
16548 /**
16549 * Prepare Academy Lesson List.
16550 *
16551 * @param array $data Search Params.
16552 * @return array
16553 */
16554 public function search_ac_lms_lessons( $data ) {
16555
16556 $options = [];
16557
16558 if ( ! class_exists( '\Academy\Helper' ) ) {
16559 return [];
16560 }
16561
16562 $curriculums = \Academy\Helper::get_course_curriculum( $data['dynamic'] );
16563
16564 if ( ! empty( $curriculums ) ) {
16565 foreach ( $curriculums as $topic ) {
16566 if ( isset( $topic['topics'] ) && is_array( $topic['topics'] ) ) {
16567 foreach ( $topic['topics'] as $lesson ) {
16568 if ( isset( $lesson['type'] ) && 'lesson' === $lesson['type'] ) {
16569 $options[] = [
16570 'label' => $lesson['name'],
16571 'value' => $lesson['id'],
16572 ];
16573 }
16574 }
16575 }
16576 }
16577 }
16578
16579 return [
16580 'options' => $options,
16581 'hasMore' => false,
16582 ];
16583 }
16584
16585 /**
16586 * Prepare Academy Quiz List.
16587 *
16588 * @param array $data Search Params.
16589 * @return array
16590 */
16591 public function search_ac_lms_quiz( $data ) {
16592
16593 $options = [];
16594
16595 $page = $data['page'];
16596 $limit = Utilities::get_search_page_limit();
16597 $offset = $limit * ( $page - 1 );
16598
16599 $quizs = get_posts(
16600 [
16601 'posts_per_page' => - 1,
16602 'orderby' => 'name',
16603 'order' => 'asc',
16604 'post_type' => 'academy_quiz',
16605 'post_status' => 'publish',
16606 ]
16607 );
16608 $quiz_count = wp_count_posts( 'academy_quiz' )->publish;
16609
16610 if ( ! empty( $quizs ) ) {
16611 if ( is_array( $quizs ) ) {
16612 foreach ( $quizs as $quiz ) {
16613 $options[] = [
16614 'label' => get_the_title( $quiz->ID ),
16615 'value' => $quiz->ID,
16616 ];
16617 }
16618 }
16619 }
16620
16621 return [
16622 'options' => $options,
16623 'hasMore' => $quiz_count > $limit && $quiz_count > $offset,
16624 ];
16625 }
16626
16627 /**
16628 * Search Academy LMS data.
16629 *
16630 * @param array $data data.
16631 * @return array|void
16632 */
16633 public function search_ac_lms_last_data( $data ) {
16634 global $wpdb;
16635 $post_type = $data['post_type'];
16636 $trigger = $data['search_term'];
16637 $context = [];
16638
16639 if ( ! class_exists( '\Academy\Helper' ) ) {
16640 return [];
16641 }
16642
16643 $course_id = -1;
16644 $lesson_id = -1;
16645 $quiz_id = -1;
16646 if ( 'ac_lms_course_completed' === $trigger ) {
16647 $course_id = $data['filter']['course']['value'];
16648 } elseif ( 'ac_lms_lesson_completed' === $trigger ) {
16649 $course_id = $data['filter']['course']['value'];
16650 $lesson_id = $data['filter']['lesson']['value'];
16651 } elseif ( 'ac_lms_quiz_completed' === $trigger || 'ac_lms_quiz_failed' === $trigger ) {
16652 $quiz_id = $data['filter']['quiz']['value'];
16653 } elseif ( 'ac_lms_enrolled_course' === $trigger ) {
16654 $course_id = $data['filter']['course']['value'];
16655 }
16656
16657 $users = get_users(
16658 [
16659 'fields' => 'ID',
16660 'meta_key' => 'is_academy_student',
16661 ]
16662 );
16663 if ( ! empty( $users ) ) {
16664 $user_random_key = array_rand( $users );
16665 $user_id = $users[ $user_random_key ];
16666 }
16667
16668 if ( 'ac_lms_course_completed' === $trigger ) {
16669 if ( -1 === $course_id ) {
16670 $result = $wpdb->get_results(
16671 $wpdb->prepare(
16672 "SELECT * FROM {$wpdb->prefix}comments
16673 as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.comment_post_ID WHERE postmeta.comment_type='course_completed' AND posts.post_type=%s order by postmeta.comment_ID DESC LIMIT 1",
16674 $post_type
16675 )
16676 );
16677 } else {
16678 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}comments as postmeta JOIN {$wpdb->prefix}posts as posts ON posts.ID=postmeta.comment_post_ID WHERE postmeta.comment_post_ID = %s AND postmeta.comment_type='course_completed' AND posts.post_type=%s order by postmeta.comment_ID DESC LIMIT 1", $course_id, $post_type ) );
16679 }
16680 } elseif ( 'ac_lms_lesson_completed' === $trigger ) {
16681 if ( -1 === $course_id ) {
16682 $courses = get_posts(
16683 [
16684 'posts_per_page' => - 1,
16685 'post_type' => 'academy_courses',
16686 'post_status' => 'publish',
16687 'fields' => 'ids',
16688 ]
16689 );
16690 $course_id = array_rand( $courses );
16691 $option_name = 'academy_course_' . $course_id . '_completed_topics';
16692 } else {
16693 $option_name = 'academy_course_' . $course_id . '_completed_topics';
16694 }
16695 if ( ! empty( $users ) ) {
16696 $meta = get_user_meta( $user_id, $option_name, true );
16697 if ( is_string( $meta ) ) {
16698 $saved_topics_lists = (array) json_decode( $meta, true );
16699 if ( -1 === $lesson_id ) {
16700 $result = $saved_topics_lists['lesson'];
16701 } else {
16702 if ( is_array( $saved_topics_lists['lesson'] ) ) {
16703 if ( array_key_exists( $lesson_id, $saved_topics_lists['lesson'] ) ) {
16704 $result = $saved_topics_lists['lesson'];
16705 }
16706 }
16707 }
16708 }
16709 }
16710 } elseif ( 'ac_lms_quiz_completed' === $trigger ) {
16711 if ( -1 === $quiz_id ) {
16712 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE attempt_status='passed' order by attempt_id DESC LIMIT 1" );
16713 } else {
16714 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE quiz_id=%s AND attempt_status='passed' order by attempt_id DESC LIMIT 1", $quiz_id ) );
16715 }
16716 } elseif ( 'ac_lms_quiz_failed' === $trigger ) {
16717 if ( -1 === $quiz_id ) {
16718 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE attempt_status='failed' order by attempt_id DESC LIMIT 1" );
16719 } else {
16720 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE quiz_id=%s AND attempt_status='failed' order by attempt_id DESC LIMIT 1", $quiz_id ) );
16721 }
16722 } elseif ( 'ac_lms_enrolled_course' === $trigger ) {
16723 if ( -1 === $course_id ) {
16724 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type = %s order by ID DESC LIMIT 1", 'academy_enrolled' ) );
16725 } else {
16726 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type = %s AND post_parent = %d order by ID DESC LIMIT 1", 'academy_enrolled', $course_id ) );
16727 }
16728 }
16729
16730 if ( ! empty( $result ) ) {
16731 switch ( $trigger ) {
16732 case 'ac_lms_course_completed':
16733 $data = WordPress::get_post_context( $result[0]->comment_post_ID );
16734 $context_data = WordPress::get_user_context( $result[0]->user_id );
16735 $context_data['course_data'] = $data;
16736 $context_data['course'] = $result[0]->comment_post_ID;
16737 break;
16738 case 'ac_lms_enrolled_course':
16739 $data = WordPress::get_post_context( $result[0]->post_parent );
16740 $context_data = WordPress::get_user_context( $result[0]->post_author );
16741 $context_data['course_data'] = $data;
16742 $context_data['enrollment_data'] = $result[0];
16743 $context_data['course'] = $result[0]->post_parent;
16744 break;
16745 case 'ac_lms_lesson_completed':
16746 if ( -1 === $lesson_id ) {
16747 $key = array_rand( $result );
16748 $lesson_data = \Academy\Helper::get_lesson( $key );
16749 } else {
16750 $lesson_data = \Academy\Helper::get_lesson( $lesson_id );
16751 }
16752 if ( is_object( $lesson_data ) ) {
16753 $lesson_data = get_object_vars( $lesson_data );
16754 }
16755 if ( ! empty( $users ) ) {
16756 $context_data = array_merge( $lesson_data, WordPress::get_user_context( $user_id ) );
16757 }
16758 $context_data['course_data'] = WordPress::get_post_context( $course_id );
16759 $context_data['lesson'] = $lesson_id;
16760 $context_data['course'] = $course_id;
16761 break;
16762 case 'ac_lms_quiz_completed':
16763 case 'ac_lms_quiz_failed':
16764 $context_data = WordPress::get_user_context( $result[0]->user_id );
16765 $context_data['quiz_data'] = WordPress::get_post_context( $result[0]->quiz_id );
16766 $context_data['quiz_attempt_details'] = $result;
16767 $context_data['quiz'] = $result[0]->quiz_id;
16768 break;
16769 default:
16770 return;
16771 }
16772 $context['pluggable_data'] = $context_data;
16773 $context['response_type'] = 'live';
16774 } elseif ( empty( $result ) ) {
16775 switch ( $trigger ) {
16776 case 'ac_lms_course_completed':
16777 $sample_data = '{"pluggable_data":{"wp_user_id": 73, "user_login": "abc@yopmail.com","display_name": "data data1", "user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"course_data": {"ID": 6949,"post_author": "1","post_date": "2023-11-29 05:37:50","post_date_gmt": "2023-11-29 05:37:50","post_content": "<!-- wp:paragraph -->\n<p>this is a business course.<\/p>\n<!-- \/wp:paragraph -->","post_title": "Business Course","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "open","post_password": "","post_name": "business-course","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:27","post_modified_gmt": "2023-11-29 09:50:27","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/course\/business-course\/","menu_order": 0,"post_type": "academy_courses","post_mime_type": "","comment_count": "0","filter": "raw"},"course": 1},"response_type":"sample"}';
16778 break;
16779 case 'ac_lms_lesson_completed':
16780 $sample_data = '{"pluggable_data":{"ID": "1","lesson_author": "1","lesson_date": "2023-11-29 07:01:03","lesson_date_gmt": "2023-11-29 07:01:03","lesson_title": "Lesson 1","lesson_name": "","lesson_content": "","lesson_excerpt": "","lesson_status": "publish","comment_status": "close","comment_count": "0","lesson_password": "","lesson_modified": "2023-11-29 07:01:03","lesson_modified_gmt": "2023-11-29 07:01:03","wp_user_id": 73,"user_login": "abc@yopmail.com","display_name": "data data1","user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"course_data": {"ID": 6949,"post_author": "1","post_date": "2023-11-29 05:37:50","post_date_gmt": "2023-11-29 05:37:50","post_content": "<!-- wp:paragraph -->\n<p>this is a business course.<\/p>\n<!-- \/wp:paragraph -->","post_title": "Business Course","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "open","post_password": "","post_name": "business-course","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:27","post_modified_gmt": "2023-11-29 09:50:27","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/course\/business-course\/","menu_order": 0,"post_type": "academy_courses","post_mime_type": "","comment_count": "0","filter": "raw"},"lesson": 1,"course":1},"response_type":"sample"}';
16781 break;
16782 case 'ac_lms_quiz_completed':
16783 $sample_data = '{"pluggable_data":{"wp_user_id": 73,"user_login": "abc@yopmail.com","display_name": "data data1","user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"quiz_data": {"ID": 6960,"post_author": "1","post_date": "2023-11-29 09:49:42","post_date_gmt": "2023-11-29 09:49:42","post_content": "","post_title": "First Quiz","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "closed","post_password": "","post_name": "first-quiz","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:11","post_modified_gmt": "2023-11-29 09:50:11","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/?post_type=academy_quiz&#038;p=6960","menu_order": 0,"post_type": "academy_quiz","post_mime_type": "","comment_count": "0","filter": "raw"}, "quiz_attempt_details": [{"attempt_id": "2","course_id": "6949","quiz_id": "6960","user_id": "126","total_questions": "1","total_answered_questions": "1","total_marks": "20.00","earned_marks": "20.00","attempt_info": "{\"total_correct_answers\":1}","attempt_status": "passed","attempt_ip": "127.0.0.1","attempt_started_at": "2023-11-30 06:20:10","attempt_ended_at": "2023-11-30 06:20:10","is_manually_reviewed": null,"manually_reviewed_at": null}],"quiz":1},"response_type":"sample"}';
16784 break;
16785 case 'ac_lms_quiz_failed':
16786 $sample_data = '{"pluggable_data":{"wp_user_id": 73,"user_login": "abc@yopmail.com","display_name": "data data1","user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"quiz_data": {"ID": 6960,"post_author": "1","post_date": "2023-11-29 09:49:42","post_date_gmt": "2023-11-29 09:49:42","post_content": "","post_title": "First Quiz","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "closed","post_password": "","post_name": "first-quiz","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:11","post_modified_gmt": "2023-11-29 09:50:11","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/?post_type=academy_quiz&#038;p=6960","menu_order": 0,"post_type": "academy_quiz","post_mime_type": "","comment_count": "0","filter": "raw"}, "quiz_attempt_details": [{"attempt_id": "2","course_id": "6949","quiz_id": "6960","user_id": "126","total_questions": "1","total_answered_questions": "1","total_marks": "20.00","earned_marks": "0.00","attempt_info": "{\"total_correct_answers\":0}","attempt_status": "failed","attempt_ip": "127.0.0.1","attempt_started_at": "2023-11-30 06:20:10","attempt_ended_at": "2023-11-30 06:20:10","is_manually_reviewed": null,"manually_reviewed_at": null}],"quiz":1},"response_type":"sample"}';
16787 break;
16788 case 'ac_lms_enrolled_course':
16789 $sample_data = '{"pluggable_data":{"course_data": {"ID": 6949,"post_author": "1","post_date": "2023-11-29 05:37:50","post_date_gmt": "2023-11-29 05:37:50","post_content": "<!-- wp:paragraph -->\n<p>this is a business course.<\/p>\n<!-- \/wp:paragraph -->","post_title": "Business Course","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "open","post_password": "","post_name": "business-course","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:27","post_modified_gmt": "2023-11-29 09:50:27","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/course\/business-course\/","menu_order": 0,"post_type": "academy_courses","post_mime_type": "","comment_count": "0","filter": "raw"},"enrollment_data": {"ID": "6971","post_author": "126","post_date": "2023-11-30 05:52:54","post_date_gmt": "2023-11-30 05:52:54","post_content": "","post_title": "Course Enrolled November 30, 2023 @ 5:52 am","post_excerpt": "","post_status": "completed","comment_status": "closed","ping_status": "closed","post_password": "","post_name": "course-enrolled-november-30-2023-552-am","to_ping": "","pinged": "","post_modified": "2023-11-30 05:52:54","post_modified_gmt": "2023-11-30 05:52:54","post_content_filtered": "","post_parent": "6949","guid": "https:\/\/suretriggers-wpnew.local\/?p=6971","menu_order": "0","post_type": "academy_enrolled","post_mime_type": "","comment_count": "0"},"course":1, "wp_user_id": 2,"user_login": "test","display_name": "test test","user_firstname": "test","user_lastname": "test", "user_email": "test@yopmail.com","user_role": ["academy_student"]},"response_type":"sample"}';
16790 break;
16791 default:
16792 return;
16793 }
16794 $context = (array) json_decode( $sample_data, true );
16795 }
16796 return $context;
16797 }
16798
16799 /**
16800 * Search myCred Point Type List.
16801 *
16802 * @param array $data Search Params.
16803 * @return array
16804 */
16805 public function search_mycred_point_type_list( $data ) {
16806
16807 $options = [];
16808
16809 if ( ! function_exists( 'mycred_get_types' ) ) {
16810 return [];
16811 }
16812
16813 $posts = mycred_get_types();
16814
16815 if ( ! empty( $posts ) ) {
16816 if ( is_array( $posts ) ) {
16817 foreach ( $posts as $key => $post ) {
16818 $options[] = [
16819 'label' => $post,
16820 'value' => $key,
16821 ];
16822 }
16823 }
16824 }
16825
16826 return [
16827 'options' => $options,
16828 'hasMore' => false,
16829 ];
16830 }
16831
16832 /**
16833 * Prepare elementor forms.
16834 *
16835 * @param array $data Search Params.
16836 *
16837 * @return array
16838 */
16839 public function search_new_elementor_form_fields( $data ) {
16840
16841 $fields = [];
16842 $select_form_id = $data['dynamic'];
16843 global $wpdb;
16844 $post_metas = $wpdb->get_results(
16845 $wpdb->prepare(
16846 "SELECT pm.post_id, pm.meta_value
16847 FROM $wpdb->postmeta pm
16848 LEFT JOIN $wpdb->posts p
16849 ON p.ID = pm.post_id
16850 WHERE p.post_type IS NOT NULL
16851 AND p.post_status = %s
16852 AND pm.meta_key = %s
16853 AND pm.`meta_value` LIKE %s",
16854 'publish',
16855 '_elementor_data',
16856 '%%form_fields%%'
16857 )
16858 );
16859
16860 if ( ! empty( $post_metas ) ) {
16861 foreach ( $post_metas as $post_meta ) {
16862 /**
16863 *
16864 * Ignore line
16865 *
16866 * @phpstan-ignore-next-line
16867 */
16868 $inner_forms = Utilities::search_elementor_forms( json_decode( $post_meta->meta_value ) );
16869 if ( ! empty( $inner_forms ) ) {
16870 foreach ( $inner_forms as $form ) {
16871 $form_id = explode( '_', $select_form_id );
16872 if ( is_object( $form ) ) {
16873 if ( $form->id == $form_id[1] ) {
16874 if ( ! empty( $form->settings->form_fields ) ) {
16875 foreach ( $form->settings->form_fields as $field ) {
16876 $fields[] = [
16877 'value' => $field->custom_id,
16878 'text' => ! empty( $field->field_label ) ? $field->field_label : 'unknown',
16879 ];
16880 }
16881 }
16882 }
16883 }
16884 }
16885 }
16886 }
16887 }
16888 $options = [];
16889 if ( ! empty( $fields ) ) {
16890 foreach ( $fields as $key => $value ) {
16891 $options[] = [
16892 'label' => $value['text'],
16893 'value' => $value['value'],
16894 ];
16895 }
16896 }
16897 return [
16898 'options' => $options,
16899 'hasMore' => false,
16900 ];
16901 }
16902 /**
16903 * Get Fluent Booking Appointment Events.
16904 *
16905 * @param array $data data.
16906 *
16907 * @return array
16908 */
16909 public function search_fluent_booking_calendars_list( $data ) {
16910
16911 global $wpdb;
16912
16913 $page = $data['page'];
16914 $limit = Utilities::get_search_page_limit();
16915 $offset = $limit * ( $page - 1 );
16916
16917 $calendars = $wpdb->get_results(
16918 $wpdb->prepare(
16919 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendars WHERE status = %s ORDER BY id DESC LIMIT %d OFFSET %d",
16920 [ 'active', $limit, $offset ]
16921 ),
16922 OBJECT
16923 );
16924
16925 $calendars_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
16926
16927 $options = [];
16928 if ( ! empty( $calendars ) ) {
16929 foreach ( $calendars as $calendar ) {
16930 $options[] = [
16931 'label' => $calendar->title,
16932 'value' => $calendar->id,
16933 ];
16934 }
16935 }
16936 return [
16937 'options' => $options,
16938 'hasMore' => $calendars_count > $limit && $calendars_count > $offset,
16939 ];
16940
16941 }
16942
16943 /**
16944 * Get Fluent Booking Appointment Events.
16945 *
16946 * @param array $data data.
16947 *
16948 * @return array
16949 */
16950 public function search_fluent_booking_events_list( $data ) {
16951
16952 global $wpdb;
16953 $page = $data['page'];
16954 $limit = Utilities::get_search_page_limit();
16955 $offset = $limit * ( $page - 1 );
16956 $calendar_id = sanitize_text_field( $data['dynamic']['calender_id'] );
16957 if ( '-1' === $calendar_id ) {
16958 $events = $wpdb->get_results(
16959 $wpdb->prepare(
16960 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendar_events WHERE status = %s ORDER BY id DESC LIMIT %d OFFSET %d",
16961 [ 'active', $limit, $offset ]
16962 ),
16963 OBJECT
16964 );
16965 } else {
16966 $events = $wpdb->get_results(
16967 $wpdb->prepare(
16968 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendar_events WHERE status = %s AND calendar_id = %d ORDER BY id DESC LIMIT %d OFFSET %d",
16969 [ 'active', $calendar_id, $limit, $offset ]
16970 ),
16971 OBJECT
16972 );
16973 }
16974
16975 $events_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
16976
16977 $options = [];
16978 if ( ! empty( $events ) ) {
16979 foreach ( $events as $event ) {
16980 $options[] = [
16981 'label' => $event->title,
16982 'value' => $event->id,
16983 ];
16984 }
16985 }
16986 return [
16987 'options' => $options,
16988 'hasMore' => $events_count > $limit && $events_count > $offset,
16989 ];
16990
16991 }
16992
16993 /**
16994 * Get Fluent Booking last data.
16995 *
16996 * @param array $data data.
16997 *
16998 * @return array
16999 */
17000 public function search_fluent_booking_last_data( $data ) {
17001 global $wpdb;
17002 $trigger = $data['search_term'];
17003 $course_data = [];
17004 $lesson_data = [];
17005
17006 $sample_data = '{"pluggable_data":{"booking":{"id":"7","hash":"dc641fd972e27be079945e3f998def39","calendar_id":"1","event_id":"1","group_id":"7","fcrm_id":null,"parent_id":null,"host_user_id":"1","person_user_id":"1","person_contact_id":null,"person_time_zone":"Asia/Calcutta","start_time":"2024-01-29 11:00:00","end_time":"2024-01-29 11:15:00","slot_minutes":"15","first_name":"Sure","last_name":"Test","email":"dev-email@wpengine.local","message":"","internal_note":null,"phone":"","country":null,"ip_address":"","browser":null,"device":null,"other_info":null,"location_details":{"type":"in_person_guest","description":"Location description."},"cancelled_by":"1","status":"completed","source":"web","booking_type":"scheduling","event_type":"single","payment_status":null,"payment_method":null,"source_url":"https://connector.com/fluent-booking/","source_id":null,"utm_source":"","utm_medium":"","utm_campaign":"","utm_term":"","created_at":"2024-01-29 06:46:22","updated_at":"2024-01-29 06:57:42","custom_fields":null},"event":{"id":"1","hash":"38c65708b797f5d6956070896f792b34","user_id":"1","calendar_id":"1","duration":"15","title":"One to one","slug":"15min","media_id":null,"description":"","settings":{"schedule_type":"weekly_schedules","weekly_schedules":{"sun":{"enabled":false,"slots":[]},"mon":{"enabled":true,"slots":[{"start":"03:30","end":"11:30"}]},"tue":{"enabled":true,"slots":[{"start":"03:30","end":"11:30"}]},"wed":{"enabled":true,"slots":[{"start":"03:30","end":"11:30"}]},"thu":{"enabled":true,"slots":[{"start":"03:30","end":"11:30"}]},"fri":{"enabled":true,"slots":[{"start":"03:30","end":"11:30"}]},"sat":{"enabled":false,"slots":[]}},"date_overrides":[],"range_type":"range_days","range_days":60,"range_date_between":["",""],"schedule_conditions":{"value":4,"unit":"hours"},"location_fields":{"conferencing":{"label":"Conferencing","options":{"google_meet":{"title":"Google Meet (Connect Google Meet First)","disabled":true,"location_type":"conferencing"},"ms_teams":{"title":"MS Teams (Connect Outlook First)","disabled":true,"location_type":"conferencing"}}},"in_person":{"label":"In Person","options":{"in_person_guest":{"title":"In Person (Attendee Address)"},"in_person_organizer":{"title":"In Person (Organizer Address)"}}},"phone":{"label":"Phone","options":{"phone_guest":{"title":"Attendee Phone Number"},"phone_organizer":{"title":"Organizer Phone Number"}}},"online":{"label":"Online","options":{"online_meeting":{"title":"Online Meeting"}}},"other":{"label":"Other","options":{"custom":{"title":"Custom"}}}},"team_members":[],"custom_redirect":{"enabled":false,"redirect_url":"","is_query_string":"no","query_string":""},"multi_duration":{"enabled":true,"default_duration":"15","available_durations":["15","30","45"]}},"availability_type":"existing_schedule","availability_id":"1","status":"active","type":"free","color_schema":"#0099ff","location_type":"","location_heading":"","location_settings":[{"type":"in_person_guest","title":"In Person (Attendee Address)","display_on_booking":""}],"event_type":"single","is_display_spots":"0","max_book_per_slot":"1","created_at":"2024-01-28 08:35:29","updated_at":"2024-01-28 08:36:06"}},"response_type":"sample"}'; //phpcs:ignore
17007 $context = [
17008 'pluggable_data' => json_decode( $sample_data, true ),
17009 'response_type' => 'sample',
17010 ];
17011 if ( 'fluent_booking_appointment_cancelled' === $trigger ) {
17012 $status = 'cancelled';
17013 } elseif ( 'fluent_booking_appointment_completed' === $trigger ) {
17014 $status = 'completed';
17015 }
17016
17017 $event_id = (int) $data['filter']['event_id']['value'];
17018 if ( 'fluent_booking_appointment_cancelled' === $trigger || 'fluent_booking_appointment_completed' === $trigger ) {
17019 if ( $event_id > 0 ) {
17020 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE event_id= %d AND status= %s ORDER BY id DESC LIMIT 1", $event_id, $status ) );
17021 } else {
17022 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE status= %s ORDER BY id DESC LIMIT 1", $status ) );
17023 }
17024 } elseif ( 'fluent_booking_new_appointment_booked' === $trigger ) {
17025 if ( $event_id > 0 ) {
17026 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE event_id= %d AND status != %s ORDER BY id DESC LIMIT 1", $event_id, 'cancelled' ) );
17027 } else {
17028 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE status != %s ORDER BY id DESC LIMIT 1", 'cancelled' ) );
17029 }
17030 }
17031
17032 if ( ! empty( $booking ) ) {
17033 $booking_meta = $wpdb->get_row( $wpdb->prepare( "SELECT value FROM {$wpdb->prefix}fcal_booking_meta WHERE booking_id= %d AND meta_key= 'custom_fields_data'", $booking->id ) );
17034
17035 $booking_event = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_calendar_events WHERE id= %d", $booking->event_id ) );
17036 $booking_data = $booking;
17037 $booking_data->custom_fields = $booking_meta->value;
17038 $booking_array = [
17039 'booking' => $booking_data,
17040 'event' => $booking_event,
17041 ];
17042
17043
17044 $booking_array_response = self::recursive_unserialize( $booking_array );
17045 $context['pluggable_data'] = $booking_array_response;
17046 $context['response_type'] = 'live';
17047 }
17048 return $context;
17049 }
17050
17051 /**
17052 * Recursive unserilize.
17053 *
17054 * @param array $data data.
17055 *
17056 * @return array|mixed
17057 */
17058 public static function recursive_unserialize( $data ) {
17059 if ( is_array( $data ) ) {
17060 foreach ( $data as $key => $value ) {
17061 $data[ $key ] = self::recursive_unserialize( $value );
17062 }
17063 return $data;
17064 } elseif ( is_object( $data ) && 'stdClass' === get_class( $data ) ) {
17065 foreach ( $data as $property => $value ) {
17066 $data->$property = self::recursive_unserialize( $value );
17067 }
17068 return $data;
17069 } elseif ( is_string( $data ) && self::is_serialized( strval( $data ) ) ) {
17070 return unserialize( $data );
17071 } else {
17072 return $data;
17073 }
17074
17075 }
17076
17077 /**
17078 * Check if string serialized.
17079 *
17080 * @param string $data data.
17081 *
17082 * @return bool
17083 */
17084 public static function is_serialized( $data ) {
17085 $unserialized = unserialize( $data );
17086 if ( 'b:0;' === $data || false !== $unserialized ) {
17087 return true;
17088 } else {
17089 return false;
17090 }
17091 }
17092
17093 /**
17094 * Search Tutor LMS data.
17095 *
17096 * @param array $data data.
17097 * @return array|void|mixed
17098 */
17099 public function search_tutor_lms_last_data( $data ) {
17100 global $wpdb;
17101 $post_type = $data['post_type'];
17102 $trigger = $data['search_term'];
17103 $context = [];
17104
17105 if ( ! function_exists( 'tutor_utils' ) ) {
17106 return [];
17107 }
17108
17109 $post_id = -1;
17110 if ( 'course_enrolled' === $trigger || 'tutor_courses_question' === $trigger ) {
17111 $post_id = $data['filter']['tutor_course']['value'];
17112 } elseif ( 'quiz_attempt_percentage' === $trigger || 'quiz_failed' === $trigger || 'quiz_passed' === $trigger ) {
17113 $post_id = $data['filter']['quiz_id']['value'];
17114 }
17115
17116 if ( 'course_enrolled' === $trigger ) {
17117 if ( -1 === $post_id ) {
17118 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_status='completed' AND post_type=%s order by ID DESC LIMIT 1", $post_type ) );
17119 } else {
17120 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_parent = %s AND post_status='completed' AND post_type=%s order by ID DESC LIMIT 1", $post_id, $post_type ) );
17121 }
17122 } elseif ( 'tutor_courses_question' === $trigger ) {
17123 if ( -1 === $post_id ) {
17124 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}comments as comment JOIN {$wpdb->prefix}posts as posts ON posts.ID=comment.comment_post_ID WHERE comment.comment_approved='approved' AND comment.comment_type='tutor_q_and_a' AND posts.post_type=%s order by comment.comment_ID DESC LIMIT 1", $post_type ) );
17125 } else {
17126 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}comments as comment JOIN {$wpdb->prefix}posts as posts ON posts.ID=comment.comment_post_ID WHERE comment.comment_post_ID = %s AND comment.comment_approved='approved' AND comment.comment_type='tutor_q_and_a' AND posts.post_type=%s order by comment.comment_ID DESC LIMIT 1", $post_id, $post_type ) );
17127 }
17128 } elseif ( 'quiz_passed' === $trigger ) {
17129 if ( -1 === $post_id ) {
17130 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts as quiz JOIN {$wpdb->prefix}posts as posts ON posts.ID=quiz.quiz_id WHERE quiz.attempt_status='attempt_ended' AND quiz.earned_marks >= quiz.total_marks AND posts.post_type=%s order by quiz.attempt_id DESC LIMIT 1", $post_type ) );
17131 } else {
17132 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts as quiz JOIN {$wpdb->prefix}posts as posts ON posts.ID=quiz.quiz_id WHERE quiz.quiz_id = %s AND quiz.attempt_status='attempt_ended' AND quiz.earned_marks >= quiz.total_marks AND posts.post_type=%s order by quiz.attempt_id DESC LIMIT 1", $post_id, $post_type ) );
17133 }
17134 } elseif ( 'quiz_failed' === $trigger ) {
17135 if ( -1 === $post_id ) {
17136 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts as quiz JOIN {$wpdb->prefix}posts as posts ON posts.ID=quiz.quiz_id WHERE quiz.attempt_status='attempt_ended' AND quiz.earned_marks < quiz.total_marks AND posts.post_type=%s order by quiz.attempt_id DESC LIMIT 1", $post_type ) );
17137 } else {
17138 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts as quiz JOIN {$wpdb->prefix}posts as posts ON posts.ID=quiz.quiz_id WHERE quiz.quiz_id = %s AND quiz.attempt_status='attempt_ended' AND quiz.earned_marks < quiz.total_marks AND posts.post_type=%s order by quiz.attempt_id DESC LIMIT 1", $post_id, $post_type ) );
17139 }
17140 } elseif ( 'quiz_attempt_percentage' == $trigger ) {
17141 $condition_compare = $data['filter']['condition_compare']['value'];
17142 $percentage = $data['filter']['percentage']['value'];
17143 if ( -1 === $post_id ) {
17144 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts as quiz JOIN {$wpdb->prefix}posts as posts ON posts.ID=quiz.quiz_id WHERE earned_marks $condition_compare %d AND quiz.attempt_status='attempt_ended' AND posts.post_type=%s order by quiz.attempt_id DESC LIMIT 1", $percentage, $post_type ) ); //phpcs:ignore
17145 } else {
17146 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}tutor_quiz_attempts as quiz JOIN {$wpdb->prefix}posts as posts ON posts.ID=quiz.quiz_id WHERE earned_marks $condition_compare %d AND quiz.quiz_id = %s AND quiz.attempt_status='attempt_ended' AND posts.post_type=%s order by quiz.attempt_id DESC LIMIT 1", $percentage, $post_id, $post_type ) ); //phpcs:ignore
17147 }
17148 }
17149
17150 if ( ! empty( $result ) ) {
17151 switch ( $trigger ) {
17152 case 'course_enrolled':
17153 $result_item_id = $result[0]->post_parent;
17154 $result_user_id = $result[0]->post_author;
17155 $context_data = array_merge(
17156 WordPress::get_user_context( $result_user_id ),
17157 WordPress::get_post_context( $result_item_id )
17158 );
17159 $context_data['tutor_course'] = $result_item_id;
17160 break;
17161 case 'tutor_courses_question':
17162 $date = $result[0]->comment_date;
17163 $data = [
17164 'comment_post_ID' => $result[0]->comment_post_ID,
17165 'comment_author' => $result[0]->comment_author,
17166 'comment_date' => $date,
17167 'comment_date_gmt' => get_gmt_from_date( $date ),
17168 'comment_content' => $result[0]->comment_content,
17169 'comment_approved' => 'approved',
17170 'comment_agent' => 'TutorLMSPlugin',
17171 'comment_type' => 'tutor_q_and_a',
17172 'comment_parent' => $result[0]->comment_parent,
17173 'user_id' => $result[0]->user_id,
17174 ];
17175 $context_data['tutor_course'] = $result[0]->comment_post_ID;
17176 $context_data['data'] = $data;
17177 break;
17178 case 'quiz_attempt_percentage':
17179 case 'quiz_failed':
17180 case 'quiz_passed':
17181 $attempt = tutor_utils()->get_attempt( $result[0]->attempt_id );
17182 $context_data = WordPress::get_user_context( $result[0]->user_id );
17183 $context_data['quiz_id'] = $attempt->quiz_id;
17184 $context_data['attempt_id'] = $result[0]->attempt_id;
17185 $context_data['attempt'] = $attempt;
17186 break;
17187 default:
17188 return;
17189 }
17190 if ( ! empty( $context_data ) ) {
17191 $context['pluggable_data'] = $context_data;
17192 $context['response_type'] = 'live';
17193 }
17194 } elseif ( empty( $result ) ) {
17195 switch ( $trigger ) {
17196 case 'course_enrolled':
17197 $sample_data = '{"pluggable_data":{"wp_user_id": 1,"user_login": "admin","display_name": "admin","user_firstname": "test","user_lastname": "test","user_email": "john@d.com","user_role": ["customer"],"id": 6636,"name": "Modes Master Class","slug": "modes-master-class-2","date_created": {"date": "2023-10-20 06:09:15.000000","timezone_type": 1,"timezone": "+00:00"},"date_modified": {"date": "2023-10-21 15:22:29.000000","timezone_type": 1,"timezone": "+00:00"},"status": "publish","menu_order": 0,"featured": false,"catalog_visibility": "visible","description": "Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words.","short_description": "","post_password": "","author_id": 1,"parent_id": 0,"reviews_allowed": true,"date_on_sale_from": null,"date_on_sale_to": null,"price": "0","regular_price": "0","sale_price": "","price_type": "free","category_ids": [106,107],"tag_ids": [],"difficulty_id": 0,"featured_image": 6616,"rating_counts": [],"average_rating": "0","review_count": 0,"enrollment_limit": 0,"duration": 360,"access_mode": "open","billing_cycle": "","show_curriculum": true,"purchase_note": "","highlights": "<li>Suscipit tortor eget felis.<\/li><li>Curabitur arcu erat idimper.<\/li><li>Lorem ipsum dolor sit amet.<\/li>","is_ai_created": false,"is_creating": false,"meta_data": []},"response_type":"sample"} ';
17198 break;
17199 case 'tutor_courses_question':
17200 $sample_data = '{"pluggable_data":{"tutor_course": "74","data": {"comment_post_ID": "74","comment_author": "admin","comment_date": "2024-02-12 08:52:45","comment_date_gmt": "2024-02-12 08:52:45","comment_content": "asdsd","comment_approved": "approved","comment_agent": "TutorLMSPlugin","comment_type": "tutor_q_and_a","comment_parent": "0","user_id": "1"}},"response_type":"sample"}';
17201 break;
17202 case 'quiz_attempt_percentage':
17203 case 'quiz_failed':
17204 case 'quiz_passed':
17205 $sample_data = '{"pluggable_data":{"wp_user_id": 1,"user_login": "john","display_name": "john","user_firstname": "john","user_lastname": "d","user_email": "johnd@gmail.com","user_role": ["administrator","tutor_instructor"],"quiz_id": "77","attempt_id": "1","attempt": {"attempt_id": "1","course_id": "74","quiz_id": "77","user_id": "1","total_questions": "1","total_answered_questions": "1","total_marks": "10.00","earned_marks": "10.00","attempt_info": "a:9:{s:10:\"time_limit\";a:3:{s:10:\"time_value\";s:1:\"0\";s:9:\"time_type\";s:7:\"minutes\";s:18:\"time_limit_seconds\";i:0;}s:13:\"feedback_mode\";s:5:\"retry\";s:16:\"attempts_allowed\";s:2:\"10\";s:13:\"passing_grade\";s:2:\"10\";s:24:\"max_questions_for_answer\";s:2:\"10\";s:20:\"question_layout_view\";s:0:\"\";s:15:\"questions_order\";s:4:\"rand\";s:29:\"short_answer_characters_limit\";s:3:\"200\";s:34:\"open_ended_answer_characters_limit\";s:3:\"500\";}","attempt_status":"attempt_ended","attempt_ip": "::1","attempt_started_at": "2024-02-12 09:05:15","attempt_ended_at": "2024-02-12 09:05:18","is_manually_reviewed": null,"manually_reviewed_at": null}},"response_type":"sample"}';
17206 break;
17207 default:
17208 return;
17209 }
17210 $context = json_decode( $sample_data, true );
17211 }
17212
17213 return $context;
17214 }
17215
17216 /**
17217 * Get Asgorus Forum list
17218 *
17219 * @param array $data data.
17220 *
17221 * @return array
17222 */
17223 public function search_asgorus_forums_list( $data ) {
17224 if ( ! class_exists( 'AsgarosForum' ) ) {
17225 return [];
17226 }
17227 $category = $data['dynamic'];
17228 if ( is_array( $category ) ) {
17229 $category_id = $category['forum_category'];
17230 } else {
17231 $category_id = $category;
17232 }
17233 $asgaros_forum = new AsgarosForum();
17234 $forums = $asgaros_forum->get_forums( $category_id );
17235 $options = [];
17236 if ( ! empty( $forums ) ) {
17237 foreach ( $forums as $forum ) {
17238 $options[] = [
17239 'label' => $forum->name,
17240 'value' => $forum->id,
17241 ];
17242 }
17243 }
17244 return [
17245 'options' => $options,
17246 'hasMore' => false,
17247 ];
17248 }
17249
17250 /**
17251 * Get Asgorus Categories list
17252 *
17253 * @param array $data data.
17254 *
17255 * @return array
17256 */
17257 public function search_asgorus_categories_list( $data ) {
17258 if ( ! class_exists( 'AsgarosForum' ) ) {
17259 return [];
17260 }
17261 $asgaros_forum = new AsgarosForum();
17262 $categories = (array) $asgaros_forum->content->get_categories();
17263 $options = [];
17264
17265 if ( ! empty( $categories ) ) {
17266 foreach ( $categories as $category ) {
17267 $options[] = [
17268 'label' => $category->name,
17269 'value' => $category->term_id,
17270 ];
17271 }
17272 }
17273 return [
17274 'options' => $options,
17275 'hasMore' => false,
17276 ];
17277 }
17278
17279 /**
17280 * Get Asgorus Topic lists
17281 *
17282 * @param array $data data.
17283 *
17284 * @return array
17285 */
17286 public function search_asgorus_topic_list( $data ) {
17287 if ( ! class_exists( 'AsgarosForum' ) ) {
17288 return [];
17289 }
17290 global $wpdb;
17291 $forum_id = $data['dynamic'];
17292 $asgaros_forum = new AsgarosForum();
17293 $sql = 'SELECT name,id FROM ' . $wpdb->prefix . 'forum_topics WHERE parent_id = %d AND closed = 0 ORDER BY id';
17294 $topics = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id ), ARRAY_A );// @phpcs:ignore
17295 $options = [];
17296
17297 if ( ! empty( $topics ) ) {
17298 foreach ( $topics as $topic ) {
17299 $options[] = [
17300 'label' => $topic['name'],
17301 'value' => $topic['id'],
17302 ];
17303 }
17304 }
17305 return [
17306 'options' => $options,
17307 'hasMore' => false,
17308 ];
17309 }
17310
17311 /**
17312 * Get Asgorus Topic Last Data
17313 *
17314 * @param array $data data.
17315 *
17316 * @return array
17317 */
17318 public function search_pluggables_asgaros_topic_last_data( $data ) {
17319 if ( ! class_exists( 'AsgarosForum' ) ) {
17320 return [];
17321 }
17322 $asgaros_forum = new AsgarosForum();
17323 $context = [];
17324 global $wpdb;
17325 $forum_id = $data['filter']['forum_id']['value'];
17326
17327 if ( -1 == $forum_id ) {
17328 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'forum_topics WHERE closed = 0 ORDER BY id DESC LIMIT 1', ARRAY_A );
17329 } else {
17330 $forum = $forum_id;
17331 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'forum_topics WHERE parent_id = %d AND closed = 0 ORDER BY id DESC LIMIT 1';
17332 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum ), ARRAY_A );// @phpcs:ignore
17333 }
17334
17335 if ( ! empty( $results ) ) {
17336 $sql_post = 'SELECT * FROM ' . $wpdb->prefix . 'forum_posts WHERE parent_id = %d ORDER BY id DESC LIMIT 1';
17337 $results_post = $wpdb->get_results( $wpdb->prepare( $sql_post, $results[0]['id'] ), ARRAY_A ); // @phpcs:ignore
17338 $context['pluggable_data']['forum_id'] = $results[0]['parent_id'];
17339 $context['pluggable_data']['topic_id'] = $results[0]['id'];
17340 $context['pluggable_data']['post_id'] = $results_post[0]['id'];
17341 $context['pluggable_data']['forum'] = $asgaros_forum->content->get_forum( $results[0]['parent_id'] );
17342 $context['pluggable_data']['topic'] = $asgaros_forum->content->get_topic( $results[0]['id'] );
17343 $context['pluggable_data']['post'] = $asgaros_forum->content->get_post( $results_post[0]['id'] );
17344
17345 $context['pluggable_data']['author'] = WordPress::get_user_context( $results[0]['author_id'] );
17346 $context['response_type'] = 'live';
17347 } else {
17348 $context = json_decode( '{"pluggable_data":{"forum_id":"1","topic_id":"2","forum":{"id":"1","name":"First Forum","parent_id":"64","parent_forum":"0","description":"My first forum.","icon":"fas fa-comments","sort":"1","forum_status":"normal","slug":"first-forum"},"topic":{"id":"2","parent_id":"1","author_id":"1","views":"2","name":"other topic","sticky":"0","closed":"0","approved":"1","slug":"other-topic"},"user":{"wp_user_id":1,"user_login":"suredev","display_name":"SureDev","user_firstname":"Sure","user_lastname":"Dev","user_email":"dev-suretest@suretriggers.com","user_role":["administrator"]}},"response_type":"sample"}', true );// @phpcs:ignore
17349 }
17350 return $context;
17351 }
17352
17353 /**
17354 * Get Asgorus Topic Reply Last Data
17355 *
17356 * @param array $data data.
17357 *
17358 * @return array
17359 */
17360 public function search_pluggables_asgaros_reply_last_data( $data ) {
17361 if ( ! class_exists( 'AsgarosForum' ) ) {
17362 return [];
17363 }
17364 $asgaros_forum = new AsgarosForum();
17365 $context = [];
17366 global $wpdb;
17367 $forum_id = $data['filter']['forum_id']['value'];
17368 $topic_id = $data['filter']['forum_id']['value'];
17369 if ( -1 == $topic_id ) {
17370 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'forum_posts ORDER BY id DESC LIMIT 1', ARRAY_A );
17371 } else {
17372 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'forum_posts WHERE parent_id = %d ORDER BY id DESC LIMIT 1';
17373 $results = $wpdb->get_results( $wpdb->prepare( $sql, $topic_id ), ARRAY_A );// @phpcs:ignore
17374 }
17375
17376 if ( ! empty( $results ) ) {
17377 $topic_id = $results[0]['parent_id'];
17378 $post_id = $results[0]['id'];
17379 $context['pluggable_data']['forum_id'] = $forum_id;
17380 $context['pluggable_data']['topic_id'] = $topic_id;
17381 $context['pluggable_data']['post_id'] = $post_id;
17382 $context['pluggable_data']['forum'] = $asgaros_forum->content->get_forum( $forum_id );
17383 $context['pluggable_data']['topic'] = $asgaros_forum->content->get_topic( $topic_id );
17384 $context['pluggable_data']['post'] = $asgaros_forum->content->get_post( $post_id );
17385
17386 $context['pluggable_data']['author'] = WordPress::get_user_context( $results[0]['author_id'] );
17387 $context['response_type'] = 'live';
17388 } else {
17389 $context = json_decode( '{"pluggable_data":{"forum_id":"1","topic_id":"2","forum":{"id":"1","name":"First Forum","parent_id":"64","parent_forum":"0","description":"My first forum.","icon":"fas fa-comments","sort":"1","forum_status":"normal","slug":"first-forum"},"topic":{"id":"2","parent_id":"1","author_id":"1","views":"2","name":"other topic","sticky":"0","closed":"0","approved":"1","slug":"other-topic"},"user":{"wp_user_id":1,"user_login":"suredev","display_name":"SureDev","user_firstname":"Sure","user_lastname":"Dev","user_email":"dev-suretest@suretriggers.com","user_role":["administrator"]}},"response_type":"sample"}', true );// @phpcs:ignore
17390 }
17391 return $context;
17392 }
17393
17394 /**
17395 * Get WPLoyalty Points Awarded Last Data
17396 *
17397 * @param array $data data.
17398 *
17399 * @return array|mixed|string
17400 */
17401 public function search_wp_loyalty_points_awarded_customer( $data ) {
17402
17403 $context = [];
17404 global $wpdb;
17405 if ( ! class_exists( 'Wlr\App\Helpers\Base' ) ) {
17406 return [];
17407 }
17408 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wlr_earn_campaign_transaction WHERE transaction_type = %s ORDER BY id DESC LIMIT 1';
17409 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'credit' ), ARRAY_A );// @phpcs:ignore
17410
17411 if ( ! empty( $results ) ) {
17412 $context['pluggable_data']['user_email'] = $results[0]['user_email'];
17413 $context['pluggable_data']['points_earned'] = $results[0]['points'];
17414 $context['pluggable_data']['action_type'] = $results[0]['action_type'];
17415 $base_helper = new \Wlr\App\Helpers\Base();
17416 $user = $base_helper->getPointUserByEmail( $results[0]['user_email'] );
17417 $points_sql = 'SELECT * FROM ' . $wpdb->prefix . 'wlr_expire_points
17418 WHERE user_email = %s ORDER BY id DESC LIMIT 1';
17419 $points_results = $wpdb->get_results( $wpdb->prepare( $points_sql, $results[0]['user_email'] ), ARRAY_A );// @phpcs:ignore
17420 $context['pluggable_data']['user'] = $user;
17421 if ( ! empty( $points_results ) ) {
17422 $expire_date = $points_results[0]['expire_date'];
17423 $timestamp = is_numeric( $expire_date ) ? (int) $expire_date : null;
17424 $date_format = get_option( 'date_format' );
17425 if ( is_string( $date_format ) ) {
17426 $context['pluggable_data']['point_expiry_date'] = wp_date( $date_format, $timestamp );
17427 }
17428 }
17429 $context['response_type'] = 'live';
17430 } else {
17431 $context = json_decode( '{"pluggable_data":{"user_email": "johnd@yopmail.com","points_earned": "4","action_type": "point_for_purchase","user": {"id": "11","user_email": "johnd@yopmail.com","refer_code": "REF-Q5Z-ZFW","points": "17","used_total_points": "0","earn_total_point": "19","birth_date": "0","level_id": "0","is_banned_user": "0","is_allow_send_email": "1","birthday_date": "0000-00-00","last_login": "0","created_date": "1710304765"},"point_expiry_date": "April 27, 2024"},"response_type":"sample"}', true );// @phpcs:ignore
17432 }
17433 return $context;
17434 }
17435
17436 /**
17437 * Get WPLoyalty Campaign Type List
17438 *
17439 * @param array $data data.
17440 *
17441 * @return array
17442 */
17443 public function search_wp_loyalty_action_type_list( $data ) {
17444 $options = [];
17445 if ( ! class_exists( 'Wlr\App\Helpers\Woocommerce' ) ) {
17446 return [];
17447 }
17448
17449 $woocommerce_helper = new \Wlr\App\Helpers\Woocommerce();
17450 $action_types = $woocommerce_helper->getActionTypes();
17451
17452 if ( ! empty( $action_types ) ) {
17453 foreach ( $action_types as $key => $type ) {
17454 $options[] = [
17455 'label' => $type,
17456 'value' => $key,
17457 ];
17458 }
17459 }
17460 return [
17461 'options' => $options,
17462 'hasMore' => false,
17463 ];
17464 }
17465
17466 /**
17467 * Get last data for trigger.
17468 *
17469 * @param array $data data.
17470 * @return array
17471 */
17472 public function search_slicewp_last_data( $data ) {
17473 global $wpdb;
17474
17475 $context = [];
17476 $context['response_type'] = 'sample';
17477
17478 $user_data = WordPress::get_sample_user_context();
17479
17480
17481 if ( ! function_exists( 'slicewp_get_affiliate' ) ) {
17482 return [];
17483 }
17484
17485 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
17486
17487 if ( in_array( $term, [ 'slicewp_new_affiliate', 'slicewp_update_affiliate' ], true ) ) {
17488 $affiliate = [
17489 'id' => 14,
17490 'user_id' => 25,
17491 'date_created' => '2024-03-14 12:35:50',
17492 'date_modified' => '2024-03-14 12:36:20',
17493 'payment_email' => 'testcustomer12@gmail.com',
17494 'website' => '',
17495 'status' => 'active',
17496 'parent_id' => 0,
17497 ];
17498 $context['pluggable_data'] = array_merge( $affiliate, [ 'user' => $user_data ] );
17499
17500 if ( ( ! empty( $data['filter'] ) && '-1' === $data['filter']['affiliate_id']['value'] ) || empty( $data['filter'] ) ) {
17501 $query = $wpdb->prepare(
17502 "
17503 SELECT *
17504 FROM {$wpdb->prefix}slicewp_affiliates
17505 WHERE status = %s
17506 ORDER BY id DESC
17507 LIMIT 1",
17508 'active'
17509 );
17510 $affiliate_results = $wpdb->get_row( $query ); //phpcs:ignore
17511
17512 } else {
17513 $affiliate_id = $data['filter']['affiliate_id']['value'];
17514 $query = $wpdb->prepare(
17515 "
17516 SELECT *
17517 FROM {$wpdb->prefix}slicewp_affiliates
17518 WHERE status = %s AND id = %d
17519 ORDER BY id DESC
17520 LIMIT 1",
17521 'active',
17522 $affiliate_id
17523 );
17524 $affiliate_results = $wpdb->get_row( $query ); //phpcs:ignore
17525
17526 }
17527
17528
17529
17530
17531 if ( ! empty( $affiliate_results ) ) {
17532 $context['pluggable_data'] = (array) $affiliate_results;
17533
17534 $user_data = WordPress::get_user_context( $affiliate_results->user_id );
17535 $context['pluggable_data']['user'] = $user_data;
17536 $context['response_type'] = 'live';
17537 }
17538 } elseif ( in_array( $term, [ 'slicewp_new_commission', 'slicewp_update_commission' ], true ) ) {
17539 $commission = [
17540 'id' => 14,
17541 'user_id' => 25,
17542 'date_created' => '2024-03-14 12:35:50',
17543 'date_modified' => '2024-03-14 12:36:20',
17544 'payment_email' => 'testcustomer12@gmail.com',
17545 'website' => '',
17546 'status' => 'active',
17547 'parent_id' => 0,
17548 ];
17549 $context['pluggable_data'] = array_merge( $commission, [ 'user' => $user_data ] );
17550 $affiliate_id = $data['filter']['affiliate_id']['value'];
17551 if ( -1 === $data['filter']['commission_id']['value'] ) {
17552 $query = $wpdb->prepare(
17553 "
17554 SELECT *
17555 FROM {$wpdb->prefix}slicewp_commissions WHERE affiliate_id=%d ORDER BY id DESC limit 1",
17556 $affiliate_id
17557 );
17558 $commission_results = $wpdb->get_row( $query ); //phpcs:ignore
17559 } else {
17560 $commission_id = $data['filter']['commission_id']['value'];
17561 $query = $wpdb->prepare(
17562 "
17563 SELECT *
17564 FROM {$wpdb->prefix}slicewp_commissions WHERE id= %d AND affiliate_id=%d ORDER BY id DESC limit 1",
17565 $commission_id,
17566 $affiliate_id
17567 );
17568 $commission_results = $wpdb->get_row( $query ); //phpcs:ignore
17569 }
17570
17571
17572
17573
17574 if ( ! empty( $commission_results ) ) {
17575 $context['pluggable_data'] = (array) $commission_results;
17576 $affiliate = slicewp_get_affiliate( $commission_results->affiliate_id );
17577 $user_id = $affiliate->get( 'user_id' );
17578 $user_data = WordPress::get_user_context( $user_id );
17579 $context['pluggable_data']['user'] = $user_data;
17580 $context['response_type'] = 'live';
17581 }
17582 }
17583
17584 return $context;
17585 }
17586
17587
17588 /**
17589 * Get Ninja Tables Last Data
17590 *
17591 * @param array $data data.
17592 *
17593 * @return array
17594 */
17595 public function search_ninja_tables_last_data( $data ) {
17596 $context = [];
17597 global $wpdb;
17598 $table_id = isset( $data['filter']['table_id']['value'] ) ? $data['filter']['table_id']['value'] : -1;
17599
17600 if ( -1 == $table_id ) {
17601 $results = $wpdb->get_row( 'SELECT * FROM ' . $wpdb->prefix . 'ninja_table_items ORDER BY id DESC LIMIT 1', ARRAY_A );
17602 } else {
17603 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'ninja_table_items WHERE table_id = %d ORDER BY id DESC LIMIT 1';
17604 $results = $wpdb->get_row( $wpdb->prepare( $sql, $table_id ), ARRAY_A );// @phpcs:ignore
17605 }
17606
17607 if ( ! empty( $results ) ) {
17608 $results['value'] = json_decode( $results['value'], true );
17609 $context['pluggable_data'] = $results;
17610 $context['pluggable_data']['owner'] = WordPress::get_user_context( $results['owner_id'] );
17611 $context['response_type'] = 'live';
17612 } else {
17613 $context = json_decode( '{"pluggable_data":{"id":"24","position":null,"table_id":"17484","owner_id":"29","attribute":"value","settings":null,"value":{"id":"3","name":"Suretriggers","class":"Dev","gender":"Female"},"created_at":"2024-03-21 13:11:25","updated_at":"2024-03-21 13:11:25","owner":{"wp_user_id":29,"user_login":"testingdsd","display_name":"suretest","user_firstname":"Suretrigger","user_lastname":"Dev","user_email":"johndoe@email.com","user_role":["editor"]}},"response_type":"sample"}', true );
17614 }
17615 return $context;
17616 }
17617
17618 /**
17619 * Get Ninja Tables Fields
17620 *
17621 * @param array $data data.
17622 *
17623 * @return array
17624 */
17625 public function search_ninja_table_fields( $data ) {
17626
17627 $context = [];
17628 global $wpdb;
17629 $table_id = isset( $data['dynamic'] ) ? $data['dynamic'] : -1;
17630 $fields = [];
17631 if ( $table_id > 0 ) {
17632 $fields = get_post_meta( $table_id, '_ninja_table_columns', true );
17633 }
17634
17635 return (array) $fields;
17636 }
17637
17638 /**
17639 * Get Late Point Booking Fields
17640 *
17641 * @param array $data data.
17642 *
17643 * @return array|void
17644 */
17645 public function search_late_point_booking_fields( $data ) {
17646 if ( ! class_exists( 'OsCustomFieldsController' ) ) {
17647
17648 return;
17649 }
17650 global $wpdb;
17651 $booking_fields = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}latepoint_settings WHERE name = %s", 'custom_fields_for_booking' ) );
17652 $fields = [];
17653 if ( ! empty( $booking_fields ) ) {
17654 $fields = json_decode( $booking_fields->value, true );
17655 }
17656 return (array) $fields;
17657 }
17658
17659 /** Search Voxel fields for action.
17660 *
17661 * @param array $data data.
17662 * @return array
17663 */
17664 public function search_voxel_custom_fields( $data ) {
17665 $post_type_fields = [];
17666 $post_type = $data['post_type'];
17667 if ( ! class_exists( '\Voxel\Post_Type' ) ) {
17668 return [];
17669 }
17670 $fields = \Voxel\Post_Type::get( $post_type )->get_fields();
17671 foreach ( $fields as $key => $field ) {
17672 if ( 'step-general' == $key ) {
17673 continue;
17674 }
17675 $field_props = $field->get_props();
17676 $post_type_fields[ $key ] = $field_props;
17677 }
17678 $context['fields'] = $post_type_fields;
17679 return $context;
17680 }
17681
17682 /**
17683 * Get Voxel Post Types lists
17684 *
17685 * @param array $data data.
17686 *
17687 * @return array
17688 */
17689 public function search_voxel_posttype_list( $data ) {
17690 $options = [];
17691 $voxel_post_types = [];
17692 if ( class_exists( '\Voxel\Post_Type' ) ) {
17693 $voxel_post_types = \Voxel\Post_Type::get_voxel_types();
17694 }
17695
17696 if ( ! empty( $voxel_post_types ) ) {
17697 foreach ( $voxel_post_types as $key => $voxel_post_type ) {
17698 $post_type = get_post_type_object( $key );
17699 if ( $post_type ) {
17700 if ( in_array( $post_type->name, [ 'collection' ], true ) ) {
17701 continue;
17702 }
17703 $options[] = [
17704 'label' => esc_attr( $post_type->labels->singular_name ),
17705 'value' => $post_type->name,
17706 ];
17707 }
17708 }
17709 }
17710 return [
17711 'options' => $options,
17712 'hasMore' => false,
17713 ];
17714 }
17715
17716 /**
17717 * Get Voxel Users Last Data
17718 *
17719 * @param array $data data.
17720 *
17721 * @return array|mixed|string
17722 */
17723 public function search_voxel_users_triggers_last_data( $data ) {
17724
17725 global $wpdb;
17726 $context = [];
17727 $term = $data['search_term'];
17728 if ( 'direct_message_received' === $term ) {
17729 $sql = "SELECT * FROM {$wpdb->prefix}voxel_messages WHERE receiver_type LIKE %s AND sender_type LIKE %s ORDER BY id DESC LIMIT 1";
17730 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user', 'user' ), ARRAY_A );// @phpcs:ignore
17731 }
17732 if ( 'direct_message_received' === $term ) {
17733 if ( ! empty( $results ) ) {
17734 $context['pluggable_data']['sender'] = WordPress::get_user_context( $results[0]['sender_id'] );
17735 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $results[0]['receiver_id'] );
17736 $context['pluggable_data']['content'] = $results[0]['content'];
17737 $context['response_type'] = 'live';
17738 } else {
17739 $context = json_decode( '{"pluggable_data":{"sender": {"wp_user_id": 1,"user_login": "admin","display_name": "Arian","user_firstname": "john","user_lastname": "d","user_email": "johnd@gmail.com","user_role": ["subscriber"]}},"receiver": {"wp_user_id": 101,"user_login": "benni","display_name": "Benni Ben","user_firstname": "Benni","user_lastname": "Ben","user_email": "benni@mailinator.com","user_role": ["subscriber"]},"content": "new message"},"response_type":"sample"}', true );// @phpcs:ignore
17740 }
17741 }
17742 return $context;
17743 }
17744
17745
17746 /**
17747 * Get Voxel Membership Last Data
17748 *
17749 * @param array $data data.
17750 *
17751 * @return array|mixed|string
17752 */
17753 public function search_voxel_membership_triggers_last_data( $data ) {
17754
17755 global $wpdb;
17756 $context = [];
17757 $term = $data['search_term'];
17758 if ( ! class_exists( 'Voxel\Stripe' ) ) {
17759 return [];
17760 }
17761 if ( 'plan_activated' === $term || 'plan_canceled' === $term ) {
17762 $meta_key = \Voxel\Stripe::is_test_mode() ? 'voxel:test_plan' : 'voxel:plan';
17763 $sql = "SELECT
17764 m.user_id AS id,
17765 u.user_login AS title,
17766 u.user_email AS email,
17767 m.meta_value AS details,
17768 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) AS plan,
17769 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.amount' ) ) AS SIGNED ) AS amount,
17770 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.status' ) ) AS status,
17771 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.created' ) ) AS DATETIME ) AS created
17772 FROM {$wpdb->prefix}usermeta as m
17773 LEFT JOIN {$wpdb->prefix}users AS u ON m.user_id = u.ID
17774 WHERE m.meta_key = %s AND JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) != 'default'
17775 ORDER BY m.user_id DESC
17776 LIMIT 25 OFFSET 0";
17777 $results = $wpdb->get_results( $wpdb->prepare( $sql, $meta_key ), ARRAY_A );// @phpcs:ignore
17778 } elseif ( 'user_registered' === $term ) {
17779 $sql = "SELECT DISTINCT u.ID
17780 FROM {$wpdb->prefix}users u
17781 JOIN {$wpdb->prefix}usermeta um ON u.ID = um.user_id
17782 WHERE um.meta_key = 'voxel:profile_id' ORDER BY ID DESC LIMIT 1";
17783 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
17784 }
17785
17786 if ( 'plan_canceled' === $term ) {
17787 if ( ! empty( $results ) ) {
17788 if ( 'cancelled' == $results[0]['status'] ) {
17789 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
17790 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
17791 $context['response_type'] = 'live';
17792 } else {
17793 $context = json_decode( '{"pluggable_data":{"wp_user_id": 101,"user_login": "benni","display_name": "John D","user_firstname": "johnd","user_lastname": "D","user_email": "johnd@gmail.com","user_role": ["subscriber"],"details": {"plan": "learningmembership","type": "subscription","subscription_id": "sub_1OwOMySHDFghoeM1sInxPrG7","price_id": "price_1OwOLJSHDFghoeM177Vf8kgt","status": "cancelled","trial_end": null,"current_period_end": 1711542948,"cancel_at_period_end": true,"amount": 800,"currency": "usd","interval": "week","interval_count": 1,"created": "2024-03-20 12:35:48","metadata": {"voxel:payment_for": "membership","voxel:plan": "learningmembership"}}},"response_type":"sample"}', true );// @phpcs:ignore
17794 }
17795 } else {
17796 $context = json_decode( '{"pluggable_data":{"wp_user_id": 101,"user_login": "benni","display_name": "John D","user_firstname": "johnd","user_lastname": "D","user_email": "johnd@gmail.com","user_role": ["subscriber"],"details": {"plan": "learningmembership","type": "subscription","subscription_id": "sub_1OwOMySHDFghoeM1sInxPrG7","price_id": "price_1OwOLJSHDFghoeM177Vf8kgt","status": "cancelled","trial_end": null,"current_period_end": 1711542948,"cancel_at_period_end": true,"amount": 800,"currency": "usd","interval": "week","interval_count": 1,"created": "2024-03-20 12:35:48","metadata": {"voxel:payment_for": "membership","voxel:plan": "learningmembership"}}},"response_type":"sample"}', true );// @phpcs:ignore
17797 }
17798 } elseif ( 'plan_activated' === $term ) {
17799 if ( ! empty( $results ) ) {
17800 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
17801 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
17802 $context['response_type'] = 'live';
17803 } else {
17804 $context = json_decode( '{"pluggable_data":{"wp_user_id": 101,"user_login": "benni","display_name": "John D","user_firstname": "johnd","user_lastname": "D","user_email": "johnd@gmail.com","user_role": ["subscriber"],"details": {"plan": "learningmembership","type": "subscription","subscription_id": "sub_1OwOMySHDFghoeM1sInxPrG7","price_id": "price_1OwOLJSHDFghoeM177Vf8kgt","status": "active","trial_end": null,"current_period_end": 1711542948,"cancel_at_period_end": true,"amount": 800,"currency": "usd","interval": "week","interval_count": 1,"created": "2024-03-20 12:35:48","metadata": {"voxel:payment_for": "membership","voxel:plan": "learningmembership"}}},"response_type":"sample"}', true );// @phpcs:ignore
17805 }
17806 } elseif ( 'user_registered' === $term ) {
17807 if ( ! empty( $results ) ) {
17808 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['ID'] );
17809 $context['response_type'] = 'live';
17810 } else {
17811 $context['pluggable_data'] = WordPress::get_sample_user_context();
17812 $context['response_type'] = 'sample';
17813 }
17814 }
17815 return $context;
17816 }
17817
17818 /**
17819 * Get Voxel Orders Last Data
17820 *
17821 * @param array $data data.
17822 *
17823 * @return array|mixed|string
17824 */
17825 public function search_voxel_order_triggers_last_data( $data ) {
17826
17827 global $wpdb;
17828 $context = [];
17829 $term = $data['search_term'];
17830
17831 if ( ! class_exists( 'Voxel\Product_Types\Orders\Order' ) ) {
17832 return [];
17833 }
17834
17835 if ( 'order_placed' === $term || 'order_canceled' === $term ) {
17836 if ( 'order_placed' === $term ) {
17837 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders ORDER BY id DESC LIMIT 1";
17838 } elseif ( 'order_canceled' === $term ) {
17839 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' ORDER BY id DESC LIMIT 1";
17840 }
17841
17842 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
17843 if ( ! empty( $results ) ) {
17844 // Get Order.
17845 $context['pluggable_data']['id'] = $results[0]['id'];
17846 $context['pluggable_data']['vendor_id'] = $results[0]['vendor_id'];
17847 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
17848 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
17849 $context['pluggable_data']['status'] = $results[0]['status'];
17850 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
17851 $context['pluggable_data']['subtotal'] = $results[0]['subtotal'];
17852 $context['pluggable_data']['total'] = $results[0]['total'];
17853 // Get order items.
17854 $order = \Voxel\Product_Types\Orders\Order::find(
17855 [
17856 'id' => $results[0]['id'],
17857 ]
17858 );
17859 $order_items = $order->get_items();
17860 $context['pluggable_data']['tax_amount'] = $order->get_tax_amount();
17861 $context['pluggable_data']['discount_amount'] = $order->get_discount_amount();
17862 $context['pluggable_data']['shipping_amount'] = $order->get_shipping_amount();
17863 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
17864 foreach ( $order_items as $item ) {
17865 $addon_data = [];
17866 if ( is_object( $item ) && method_exists( $item, 'get_addons' ) ) {
17867 // Get addon details.
17868 $addons = $item->get_addons();
17869 $addon_data = [];
17870
17871 // Add the addons to the simple entry.
17872 if ( $addons && isset( $addons['summary'] ) ) {
17873 $addon_data = $addons['summary'];
17874 }
17875 }
17876 $context['pluggable_data']['order_items'][] = [
17877 'id' => $item->get_id(),
17878 'type' => $item->get_type(),
17879 'currency' => $item->get_currency(),
17880 'quantity' => $item->get_quantity(),
17881 'subtotal' => $item->get_subtotal(),
17882 'product_id' => $item->get_post()->get_id(),
17883 'product_label' => $item->get_product_label(),
17884 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
17885 'product_link' => $item->get_product_link(),
17886 'description' => $item->get_product_description(),
17887 'addon_data' => $addon_data,
17888 ];
17889 // If booking item, get booking details.
17890 if ( 'booking' === $item->get_type() ) {
17891 $details = $item->get_order_page_details();
17892 $context['pluggable_data']['order_items']['booking_type'] = $details['booking']['type'];
17893 if ( isset( $details['booking']['count_mode'] ) ) {
17894 $context['pluggable_data']['order_items']['booking_count_mode'] = $details['booking']['count_mode'];
17895 }
17896 if ( 'date_range' === $details['booking']['type'] ) {
17897 $context['pluggable_data']['order_items']['booking_start_date'] = $details['booking']['start_date'];
17898 $context['pluggable_data']['order_items']['booking_end_date'] = $details['booking']['end_date'];
17899 } elseif ( 'single_day' === $details['booking']['type'] ) {
17900 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
17901 } elseif ( 'timeslots' === $details['booking']['type'] ) {
17902 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
17903 $context['pluggable_data']['order_items']['booking_slot_from'] = $details['booking']['slot']['from'];
17904 $context['pluggable_data']['order_items']['booking_slot_to'] = $details['booking']['slot']['to'];
17905 }
17906 }
17907 }
17908 // Get Customer.
17909 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
17910 $context['response_type'] = 'live';
17911 } else {
17912 $context = json_decode( '{"pluggable_data":{"id": "15","vendor_id": null,"details": {"cart": {"type": "direct_cart","items": {"6b39iruj": {"product": {"post_id": 9211,"field_key": "product"},"stock": {"quantity": 1}}}},"pricing": {"currency": "USD","subtotal": 10,"total": 10},"status": {"last_updated": "2024-05-30 06:52:05"}},"payment_method": "offline_payment","status": "pending_approval","created_at": "2024-05-30 06:50:19","subtotal": 10,"total": 10,"tax_amount": null,"discount_amount": null,"shipping_amount": null,"order_item_count": 1,"order_items": [{"id": 11,"type": "regular","currency": "USD","quantity": 1,"subtotal": 10,"product_id": 9211,"product_label": "Pro 1","product_thumbnail_url": null,"product_link": "https:\/\/example.com\/products\/pro-1\/","description": "","addon_data": []}],"customer": {"wp_user_id": 98,"user_login": "johnd","display_name": "johndoe","user_firstname": "john","user_lastname": "d","user_email": "johnd@example.com","user_role": ["customer"]}},"response_type":"sample"}', true );// @phpcs:ignore
17913 }
17914 } elseif ( 'order_approved' === $term || 'order_declined' === $term ) {
17915 if ( 'order_approved' === $term ) {
17916 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'completed' ORDER BY id DESC LIMIT 1";
17917 } elseif ( 'order_declined' === $term ) {
17918 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' ORDER BY id DESC LIMIT 1";
17919 }
17920 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
17921 if ( ! empty( $results ) ) {
17922 // Get Order.
17923 $context['pluggable_data']['id'] = $results[0]['id'];
17924 $context['pluggable_data']['vendor_id'] = $results[0]['vendor_id'];
17925 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
17926 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
17927 $context['pluggable_data']['status'] = $results[0]['status'];
17928 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
17929 $context['pluggable_data']['subtotal'] = $results[0]['subtotal'];
17930 $context['pluggable_data']['total'] = $results[0]['total'];
17931 // Get order items.
17932 $order = \Voxel\Product_Types\Orders\Order::find(
17933 [
17934 'id' => $results[0]['id'],
17935 ]
17936 );
17937 $order_items = $order->get_items();
17938 $context['pluggable_data']['tax_amount'] = $order->get_tax_amount();
17939 $context['pluggable_data']['discount_amount'] = $order->get_discount_amount();
17940 $context['pluggable_data']['shipping_amount'] = $order->get_shipping_amount();
17941 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
17942 foreach ( $order_items as $item ) {
17943 $addon_data = [];
17944 if ( is_object( $item ) && method_exists( $item, 'get_addons' ) ) {
17945 // Get addon details.
17946 $addons = $item->get_addons();
17947 $addon_data = [];
17948
17949 // Add the addons to the simple entry.
17950 if ( $addons && isset( $addons['summary'] ) ) {
17951 $addon_data = $addons['summary'];
17952 }
17953 }
17954 $context['pluggable_data']['order_items'][] = [
17955 'id' => $item->get_id(),
17956 'type' => $item->get_type(),
17957 'currency' => $item->get_currency(),
17958 'quantity' => $item->get_quantity(),
17959 'subtotal' => $item->get_subtotal(),
17960 'product_id' => $item->get_post()->get_id(),
17961 'product_label' => $item->get_product_label(),
17962 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
17963 'product_link' => $item->get_product_link(),
17964 'description' => $item->get_product_description(),
17965 'addon_data' => $addon_data,
17966 ];
17967 // If booking item, get booking details.
17968 if ( 'booking' === $item->get_type() ) {
17969 $details = $item->get_order_page_details();
17970 $context['pluggable_data']['order_items']['booking_type'] = $details['booking']['type'];
17971 if ( isset( $details['booking']['count_mode'] ) ) {
17972 $context['pluggable_data']['order_items']['booking_count_mode'] = $details['booking']['count_mode'];
17973 }
17974 if ( 'date_range' === $details['booking']['type'] ) {
17975 $context['pluggable_data']['order_items']['booking_start_date'] = $details['booking']['start_date'];
17976 $context['pluggable_data']['order_items']['booking_end_date'] = $details['booking']['end_date'];
17977 } elseif ( 'single_day' === $details['booking']['type'] ) {
17978 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
17979 } elseif ( 'timeslots' === $details['booking']['type'] ) {
17980 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
17981 $context['pluggable_data']['order_items']['booking_slot_from'] = $details['booking']['slot']['from'];
17982 $context['pluggable_data']['order_items']['booking_slot_to'] = $details['booking']['slot']['to'];
17983 }
17984 }
17985 }
17986 // Get Customer.
17987 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
17988 // Get Vendor.
17989 $context['pluggable_data']['vendor'] = WordPress::get_user_context( $results[0]['vendor_id'] );
17990 $context['response_type'] = 'live';
17991 } else {
17992 $context = json_decode( '{"pluggable_data":{"id": "15","vendor_id": null,"details": {"cart": {"type": "direct_cart","items": {"6b39iruj": {"product": {"post_id": 9211,"field_key": "product"},"stock": {"quantity": 1}}}},"pricing": {"currency": "USD","subtotal": 10,"total": 10},"status": {"last_updated": "2024-05-30 06:52:05"}},"payment_method": "offline_payment","status": "pending_approval","created_at": "2024-05-30 06:50:19","subtotal": null,"total": null,"tax_amount": null,"discount_amount": null,"shipping_amount": null,"order_item_count": 1,"order_items": [{"id": 11,"type": "regular","currency": "USD","quantity": 1,"subtotal": 10,"product_id": 9211,"product_label": "Pro 1","product_thumbnail_url": null,"product_link": "https:\/\/example.com\/products\/pro-1\/","description": "","addon_data": []}],"vendor": {"wp_user_id": 1,"user_login": "admin","display_name": "Arian","user_firstname": "arian","user_lastname": "d","user_email": "johnd@gmail.com","user_role": {"0": "administrator","7": "academy_instructor","8": "tutor_instructor"}},"customer": {"wp_user_id": 98,"user_login": "johnd","display_name": "johndoe","user_firstname": "john","user_lastname": "d","user_email": "johnd@example.com","user_role": ["customer"]}},"response_type":"sample"}', true );// @phpcs:ignore
17993 }
17994 } elseif ( 'order_promotion_activated' === $term || 'order_promotion_canceled' === $term || 'order_claim_listing' === $term ) {
17995 if ( 'order_promotion_activated' === $term ) {
17996 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'completed' AND details LIKE '%voxel:promotion%' ORDER BY id DESC LIMIT 1";
17997 } elseif ( 'order_promotion_canceled' === $term ) {
17998 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' AND details LIKE '%voxel:promotion%' ORDER BY id DESC LIMIT 1";
17999 } elseif ( 'order_claim_listing' === $term ) {
18000 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE details LIKE '%voxel:claim%' ORDER BY id DESC LIMIT 1";
18001 }
18002 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18003 if ( ! empty( $results ) ) {
18004 // Get Order.
18005 $context['pluggable_data']['id'] = $results[0]['id'];
18006 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
18007 $context['pluggable_data']['status'] = $results[0]['status'];
18008 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
18009 // Get order items.
18010 $order = \Voxel\Product_Types\Orders\Order::find(
18011 [
18012 'id' => $results[0]['id'],
18013 ]
18014 );
18015 $order_items = $order->get_items();
18016 $context['pluggable_data']['details'] = $order->get_details();
18017 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
18018 foreach ( $order_items as $item ) {
18019 $context['pluggable_data']['order_items'][] = [
18020 'id' => $item->get_id(),
18021 'type' => $item->get_type(),
18022 'currency' => $item->get_currency(),
18023 'quantity' => $item->get_quantity(),
18024 'subtotal' => $item->get_subtotal(),
18025 'product_id' => $item->get_post()->get_id(),
18026 'product_label' => $item->get_product_label(),
18027 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
18028 'product_link' => $item->get_product_link(),
18029 'description' => $item->get_product_description(),
18030 ];
18031 }
18032 // Get Customer.
18033 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
18034 $context['response_type'] = 'live';
18035 } else {
18036 $context = json_decode( '{"pluggable_data":{"id":"22","payment_method":"offline_payment","status":"pending_approval","created_at":"2024-08-27 10:20:16","details":{"cart":{"type":"direct_cart","items":{"lzl47hyq":{"product":{"post_id":8912,"field_key":"voxel:claim"}}}},"pricing":{"currency":"USD","subtotal":5,"total":5},"order_notes":"ABDDD","status":{"last_updated":"2024-08-27 10:20:16"}},"order_item_count":1,"order_items":[{"id":22,"type":"regular","currency":"USD","quantity":null,"subtotal":5,"product_id":8912,"product_label":"Fokachio","product_thumbnail_url":"https://example.com/wp-content/uploads/2024/05/8a13537-150x150.jpg","product_link":"https://example.com/places/papas-pita-2/","description":"Claim request"}],"customer":{"wp_user_id":1,"user_login":"johnd","display_name":"johnd","user_firstname":"john","user_lastname":"d","user_email":"johnd@example.com","user_registered":"2023-01-16 09:23:31","user_role":{"8":"tutor_instructor"}}},"response_type":"sample"}', true );// @phpcs:ignore
18037 }
18038 }
18039 return $context;
18040 }
18041
18042 /**
18043 * Get Voxel Timeline Comments Last Data
18044 *
18045 * @param array $data data.
18046 *
18047 * @return array|mixed|string
18048 */
18049 public function search_voxel_timeline_triggers_last_data( $data ) {
18050
18051 global $wpdb;
18052 $context = [];
18053 $term = $data['search_term'];
18054 $sql = '';
18055 if ( 'new_comment_timeline' === $term ) {
18056 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE parent_id IS NULL ORDER BY id DESC LIMIT 1";
18057 } elseif ( 'comment_reply_timeline' === $term ) {
18058 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE parent_id IS NOT NULL ORDER BY id DESC LIMIT 1";
18059 }
18060 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18061 if ( 'new_comment_timeline' === $term ) {
18062 if ( ! empty( $results ) ) {
18063 $context['pluggable_data']['comment_by'] = WordPress::get_user_context( $results[0]['user_id'] );
18064 $context['pluggable_data']['id'] = $results[0]['id'];
18065 $context['pluggable_data']['status_id'] = $results[0]['status_id'];
18066 $context['pluggable_data']['content'] = $results[0]['content'];
18067 $context['response_type'] = 'live';
18068 } else {
18069 $context = json_decode( '{"pluggable_data":{"comment_by": {"wp_user_id": 1,"user_login": "admin","display_name": "Johnd","user_firstname": "john","user_lastname": "d","user_email": "johnd@yopmail.com","user_role": {"0": "administrator","7": "academy_instructor","8": "tutor_instructor"}},"id": "16","status_id": "5","content": "Nice"},"response_type":"sample"}', true );// @phpcs:ignore
18070 }
18071 } elseif ( 'comment_reply_timeline' === $term ) {
18072 if ( ! empty( $results ) ) {
18073 $comment_sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE id = %d";
18074 $comment_result = $wpdb->get_results( $wpdb->prepare( $comment_sql, $results[0]['parent_id']), ARRAY_A );// @phpcs:ignore
18075 $context['pluggable_data']['replied_by'] = WordPress::get_user_context( $results[0]['user_id'] );
18076 $context['pluggable_data']['comment_by'] = WordPress::get_user_context( $comment_result[0]['user_id'] );
18077 $context['pluggable_data']['comment'] = $comment_result[0]['content'];
18078 $context['pluggable_data']['comment_id'] = $results[0]['parent_id'];
18079 $context['pluggable_data']['reply_id'] = $results[0]['id'];
18080 $context['pluggable_data']['reply'] = $results[0]['content'];
18081 $context['response_type'] = 'live';
18082 } else {
18083 $context = json_decode( '{"pluggable_data":{"replied_by": {"wp_user_id": 101,"user_login": "johnd","display_name": "JohnD","user_firstname": "John","user_lastname": "D","user_email": "johnd@gmail.com","user_role": ["subscriber"]},"comment_by": {"wp_user_id": 1,"user_login": "admin","display_name": "Arian","user_firstname": "Arian","user_lastname": "D","user_email": "arian2@gmail.com","user_role": ["subscriber"]},"comment": "Nice","comment_id": "16","reply_id": "17","reply": "Nice too"},"response_type":"sample"}', true );// @phpcs:ignore
18084 }
18085 }
18086 return $context;
18087 }
18088
18089 /**
18090 * Get Voxel Timeline Comments Last Data
18091 *
18092 * @param array $data data.
18093 *
18094 * @return array|mixed|string
18095 */
18096 public function search_voxel_posts_triggers_last_data( $data ) {
18097 $context = [];
18098 global $wpdb;
18099 $post_type = 'post';
18100 $term = $data['search_term'];
18101 if ( isset( $data['filter']['post_type']['value'] ) ) {
18102 $post_type = $data['filter']['post_type']['value'];
18103 }
18104 $results = [];
18105 if ( 'post_approved' === $term ) {
18106 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
18107 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type, 'publish'), ARRAY_A );// @phpcs:ignore
18108 } elseif ( 'post_rejected' === $term ) {
18109 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
18110 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type, 'rejected'), ARRAY_A );// @phpcs:ignore
18111 } elseif ( 'post_reviewed' === $term ) {
18112 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline vt JOIN {$wpdb->prefix}posts p ON vt.post_id = p.ID JOIN {$wpdb->prefix}postmeta pm ON vt.post_id = pm.post_id
18113 WHERE pm.meta_key LIKE '%voxel:review_stats%' AND p.post_type LIKE %s AND vt.details IS NOT NULL AND vt.details LIKE %s";
18114 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type, '%rating%' ), ARRAY_A );// @phpcs:ignore
18115 } elseif ( 'post_submitted' === $term ) {
18116 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
18117 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type), ARRAY_A );// @phpcs:ignore
18118 } elseif ( 'post_updated' === $term ) {
18119 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
18120 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type ), ARRAY_A );// @phpcs:ignore
18121 } elseif ( 'collection_post_submitted' === $term ) {
18122 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
18123 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'collection' ), ARRAY_A );// @phpcs:ignore
18124 } elseif ( 'collection_post_updated' === $term ) {
18125 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
18126 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'collection' ), ARRAY_A );// @phpcs:ignore
18127 } elseif ( 'new_wall_post_by_user' === $term ) {
18128 $sql = "SELECT vt.* FROM {$wpdb->prefix}voxel_timeline vt JOIN {$wpdb->prefix}posts p ON vt.post_id = p.ID JOIN {$wpdb->prefix}postmeta pm ON pm.post_id = p.ID WHERE p.post_type LIKE %s AND vt.details IS NOT NULL AND vt.details NOT LIKE %s ORDER BY vt.id DESC LIMIT 1";
18129 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type, '%rating%' ), ARRAY_A );// @phpcs:ignore
18130 }
18131 if ( 'post_approved' === $term || 'post_rejected' === $term || 'post_submitted' === $term || 'post_updated' === $term ) {
18132 if ( ! empty( $results ) ) {
18133 $context['pluggable_data'] = WordPress::get_post_context( $results[0]['ID'] );
18134 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['ID'] );
18135 $context['response_type'] = 'live';
18136 } else {
18137 $context['pluggable_data'] = [
18138 'ID' => 557,
18139 'post' => 557,
18140 'post_author' => 1,
18141 'post_date' => '2022-11-18 12:18:14',
18142 'post_date_gmt' => '2022-11-18 12:18:14',
18143 'post_content' => 'Test Post Content',
18144 'post_title' => 'Test Post',
18145 'post_excerpt' => '',
18146 'post_status' => 'published',
18147 'comment_status' => 'open',
18148 'ping_status' => 'open',
18149 'post_password' => '',
18150 'post_name' => 'test-post',
18151 'to_ping' => '',
18152 'pinged' => '',
18153 'post_modified' => '2022-11-18 12:18:14',
18154 'post_modified_gmt' => '2022-11-18 12:18:14',
18155 'post_content_filtered' => '',
18156 'post_parent' => 0,
18157 'guid' => 'https://example.com/test-post/',
18158 'menu_order' => 0,
18159 'post_type' => 'post',
18160 'post_mime_type' => '',
18161 'comment_count' => 0,
18162 'filter' => 'raw',
18163 ];
18164 $context['response_type'] = 'sample';
18165 }
18166 } elseif ( 'post_reviewed' === $term ) {
18167 if ( ! empty( $results ) ) {
18168 $context['pluggable_data'] = WordPress::get_post_context( $results[0]['post_id'] );
18169 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['post_id'] );
18170 $context['pluggable_data']['review_content'] = $results[0]['content'];
18171 $context['pluggable_data']['review_created_at'] = $results[0]['created_at'];
18172 $context['pluggable_data']['review_details'] = json_decode( $results[0]['details'], true );
18173 $context['pluggable_data']['review_by'] = WordPress::get_user_context( $results[0]['user_id'] );
18174 $context['response_type'] = 'live';
18175 } else {
18176 $context = json_decode( '{"pluggable_data":{"ID": 8291,"post_author": "1","post_date": "2024-03-18 09:01:54","post_date_gmt": "2024-03-18 09:01:54","post_content": "<p>PizzaCrust - Since 2009! Whether it\u2019s our iconic Sandwiches, wooden baked pizzas, signature sauce, original fresh dough or toppings, we invest in bringing the freshest and best ingredients to bring you the tastiest meal.<\/p>","post_title": "Pizza Crust","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "closed","post_password": "","post_name": "sach-pizza","to_ping": "","pinged": "","post_modified": "2024-03-18 09:01:54","post_modified_gmt": "2024-03-18 09:01:54","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/places\/sach-pizza\/","menu_order": 0,"post_type": "places","post_mime_type": "","comment_count": "0","filter": "raw","review_content": "Nice one","review_created_at": "2024-04-04 12:59:22","review_details": {"rating": {"score": -1,"custom-660": -1,"custom-978": -1,"custom-271": -1}},"review_by": {"wp_user_id": 188,"user_login": "dev4","display_name": "dev4","user_firstname": "","user_lastname": "","user_email": "dev4@yopmail.com","user_role": ["subscriber"]}},"response_type":"sample"}', true );// @phpcs:ignore
18177 }
18178 } elseif ( 'collection_post_submitted' === $term || 'collection_post_updated' === $term ) {
18179 if ( ! empty( $results ) ) {
18180 $context['pluggable_data'] = Voxel::get_post_fields( $results[0]['ID'] );
18181 $context['pluggable_data']['collection'] = WordPress::get_post_context( $results[0]['ID'] );
18182 $context['response_type'] = 'live';
18183 } else {
18184 $context = json_decode(
18185 '{"pluggable_data":{"field_step-general": null,"field_title": "First Collection","field_items": [8909,8293],"collection": {"ID": 9142,"post_author": "35",
18186 "post_date": "2024-05-27 05:55:21","post_date_gmt": "2024-05-27 05:55:21","post_content": "This is a first collection","post_title": "First Collection","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "closed","post_password": "","post_name": "first-collection","to_ping": "","pinged": "","post_modified": "2024-05-27 05:55:21","post_modified_gmt": "2024-05-27 05:55:21","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/example.com\/collection\/first-collection\/","menu_order": 0,"post_type": "collection","post_mime_type": "","comment_count": "0","filter": "raw"}},"response_type":"sample"}', true );// @phpcs:ignore
18187 }
18188 } elseif ( 'new_wall_post_by_user' === $term ) {
18189 if ( ! empty( $results ) ) {
18190 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['post_id'] );
18191 $user = get_userdata( $results[0]['user_id'] );
18192 if ( $user ) {
18193 $user_data = (array) $user->data;
18194 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
18195 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
18196 $context['pluggable_data']['user_email'] = $user_data['user_email'];
18197 $context['pluggable_data']['user_id'] = $results[0]['user_id'];
18198 }
18199 if ( function_exists( 'Voxel\Timeline\prepare_status_json' ) && class_exists( 'Voxel\Timeline\Status' ) ) {
18200 // Get the status details.
18201 $args = [
18202 'post_id' => $results[0]['post_id'],
18203 'order_by' => 'created_at',
18204 'order' => 'desc',
18205 ];
18206 $statuses = \Voxel\Timeline\Status::query( $args );
18207 $status_details = \Voxel\Timeline\prepare_status_json( $statuses[0] );
18208 foreach ( (array) $status_details as $key => $value ) {
18209 $context['pluggable_data']['wall_post'][ $key ] = $value;
18210 }
18211 }
18212 $context['response_type'] = 'live';
18213 } else {
18214 $context = json_decode( '{"pluggable_data":{"post": {"step-general": null,"voxel:name": "johnd","voxel:avatar": [],"description": "","field_step-general": null,"field_voxel:name": "johnd","field_voxel:avatar": [],"field_description": ""},"user_display_name": "johnd","user_name": "johnd","user_email": "johnd@yopmail.com","user_id": "1","wall_post": {"content": "new wallpost","created_at": "2024-06-13 08:44:05","files": [{"source": "existing","id": 9331,"name": "download-1-1.png","alt": "","url": "https:\/\/example.com\/wp-content\/uploads\/2024\/06\/download-1-1.png","preview": "https:\/\/example.com\/wp-content\/uploads\/2024\/06\/download-1-1.png","type": "image\/png"}]}},"response_type":"sample"}', true );// @phpcs:ignore
18215 }
18216 }
18217 return $context;
18218 }
18219
18220 /**
18221 * Get Voxel Profiles Last Data
18222 *
18223 * @param array $data data.
18224 *
18225 * @return array|mixed|string
18226 */
18227 public function search_voxel_profiles_triggers_last_data( $data ) {
18228
18229 global $wpdb;
18230 $context = [];
18231 $term = $data['search_term'];
18232
18233 if ( ! class_exists( 'Voxel\Timeline\Status' ) || ! function_exists( 'Voxel\Timeline\prepare_status_json' ) || ! class_exists( 'Voxel\Post_Type' ) ) {
18234 return;
18235 }
18236
18237 if ( 'profile_created' === $term ) {
18238 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
18239 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
18240 } elseif ( 'profile_approved' === $term ) {
18241 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
18242 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile', 'publish' ), ARRAY_A );// @phpcs:ignore
18243 } elseif ( 'profile_rejected' === $term ) {
18244 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
18245 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile', 'rejected' ), ARRAY_A );// @phpcs:ignore
18246 } elseif ( 'profile_reviewed' === $term ) {
18247 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline vt LEFT JOIN {$wpdb->prefix}posts pm
18248 ON vt.post_id = pm.ID
18249 WHERE pm.post_type LIKE %s AND vt.details IS NOT NULL ORDER BY vt.id DESC LIMIT 1";
18250 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
18251 } elseif ( 'profile_updated' === $term ) {
18252 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
18253 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
18254 } elseif ( 'profile_new_wall_post' === $term ) {
18255 $sql = "SELECT vt.* FROM {$wpdb->prefix}voxel_timeline vt JOIN {$wpdb->prefix}posts p ON vt.post_id = p.ID JOIN {$wpdb->prefix}postmeta pm ON pm.post_id = p.ID WHERE p.post_type LIKE 'profile' ORDER BY vt.id DESC LIMIT 1";
18256 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
18257 }
18258 if ( 'profile_created' === $term || 'profile_approved' === $term || 'profile_rejected' === $term || 'profile_updated' === $term ) {
18259 if ( ! empty( $results ) ) {
18260 $context['pluggable_data'] = Voxel::get_post_fields( $results[0]['ID'] );
18261 $user = get_userdata( $results[0]['post_author'] );
18262 if ( $user ) {
18263 $user_data = (array) $user->data;
18264 $context['pluggable_data']['profile_display_name'] = $user_data['display_name'];
18265 $context['pluggable_data']['profile_email'] = $user_data['user_email'];
18266 $context['pluggable_data']['profile_user_id'] = $results[0]['post_author'];
18267 $context['pluggable_data']['profile_id'] = $results[0]['ID'];
18268 }
18269 $context['response_type'] = 'live';
18270 } else {
18271 $context = json_decode( '{"pluggable_data":{"field_step-general": null,"field_voxel:name":"johnd","field_voxel:avatar": [],"field_description": "","profile_display_name": "johnd","profile_name": "johnd","profile_email": "johnd@yopmail.com","profile_user_id": "1"},"response_type":"sample"}', true );// @phpcs:ignore
18272 }
18273 } elseif ( 'profile_new_wall_post' === $term ) {
18274 if ( ! empty( $results ) ) {
18275 $context['pluggable_data']['profile'] = Voxel::get_post_fields( $results[0]['post_id'] );
18276 $user = get_userdata( $results[0]['user_id'] );
18277 if ( $user ) {
18278 $user_data = (array) $user->data;
18279 $context['pluggable_data']['profile_display_name'] = $user_data['display_name'];
18280 $context['pluggable_data']['profile_name'] = $user_data['user_nicename'];
18281 $context['pluggable_data']['profile_email'] = $user_data['user_email'];
18282 $context['pluggable_data']['profile_user_id'] = $results[0]['user_id'];
18283 }
18284 // Get the status details.
18285 $args = [
18286 'post_id' => $results[0]['post_id'],
18287 'order_by' => 'created_at',
18288 'order' => 'desc',
18289 ];
18290 $statuses = \Voxel\Timeline\Status::query( $args );
18291 $status_details = \Voxel\Timeline\prepare_status_json( $statuses[0] );
18292 foreach ( (array) $status_details as $key => $value ) {
18293 $context['pluggable_data']['wall_post'][ $key ] = $value;
18294 }
18295 $context['response_type'] = 'live';
18296 } else {
18297 $context = json_decode( '{"pluggable_data":{"profile": {"step-general": null,"voxel:name": "johnd","voxel:avatar": [],"description": "","field_step-general": null,"field_voxel:name": "johnd","field_voxel:avatar": [],"field_description": ""},"profile_display_name": "johnd","profile_name": "johnd","profile_email": "johnd@yopmail.com","profile_user_id": "1""wall_post": {"content": "new wallpost","created_at": "2024-06-13 08:44:05","files": [{"source": "existing","id": 9331,"name": "download-1-1.png","alt": "","url": "https:\/\/example.com\/wp-content\/uploads\/2024\/06\/download-1-1.png","preview": "https:\/\/example.com\/wp-content\/uploads\/2024\/06\/download-1-1.png","type": "image\/png"}]}},"response_type":"sample"}', true );// @phpcs:ignore
18298 }
18299 } elseif ( 'profile_reviewed' === $term ) {
18300 if ( ! empty( $results ) ) {
18301 // Get the review details.
18302 $args = [
18303 'post_id' => $results[0]['post_id'],
18304 'order_by' => 'created_at',
18305 'order' => 'desc',
18306 ];
18307 $statuses = \Voxel\Timeline\Status::query( $args );
18308 $review_details = \Voxel\Timeline\prepare_status_json( $statuses[0] );
18309 foreach ( (array) $review_details as $key => $value ) {
18310 if ( 'user_can_edit' == $key || 'publisher' == $key || 'user_can_edit' == $key || 'user_can_moderate' == $key ) {
18311 continue;
18312 }
18313 if ( 'files' === $key ) {
18314 $value = wp_json_encode( $value );
18315 } elseif ( 'reviews' === $key ) {
18316 $review_ratings = isset( $value['ratings'] ) && is_array( $value['ratings'] ) ? $value['ratings'] : [];
18317 $value['ratings'] = [];
18318 $type = \Voxel\Post_Type::get( 'profile' );
18319
18320 if ( ! empty( $review_ratings ) ) {
18321 $rating_levels = $type->reviews->get_rating_levels();
18322 $categories = $type->reviews->get_categories();
18323
18324 foreach ( $categories as $category ) {
18325 $category_key = $category['key'];
18326 $category_label = strtolower( $category['label'] );
18327
18328 if ( isset( $review_ratings[ $category_key ] ) && $category_label ) {
18329 foreach ( $rating_levels as $rating_level ) {
18330 if ( $review_ratings[ $category_key ] === $rating_level['score'] ) {
18331 $value['ratings'][ $category_label ] = $rating_level['label'];
18332 break;
18333 }
18334 }
18335 }
18336 }
18337 }
18338 } else {
18339 $key = 'review_' . $key;
18340 }
18341 $context['pluggable_data'][ $key ] = $value;
18342 }
18343 if ( isset( $context['pluggable_data']['review_user'] ) ) {
18344 unset( $context['pluggable_data']['review_user']['avatar'] );
18345 }
18346 $context['response_type'] = 'live';
18347 } else {
18348 $context = json_decode( '{"pluggable_data":{"review_id":32,"review_key":"152b0720","review_link":"https://example.com/newsfeed/?status_id=32","review_time":"3 hours ago","review_edit_time":null,"review_content":"new review","review_raw_content":"new review","files":null,"review_is_review":true,"review_user":{"exists":true,"name":"johnd","link":"https://example.com/members/johnd/","id":217},"review_post":{"exists":true,"title":"admin","link":"https://example.com/members/admin/","is_profile":false,"post_type":"profile"},"review_liked_by_user":false,"review_like_count":null,"review_reply_count":null,"review_replies":{"requested":false,"visible":false,"page":1,"loading":false,"hasMore":false,"list":[]},"reviews":{"score":0,"score_formatted":"3.0","mode":"numeric","ratings":{"rating":"Good"}}},"response_type":"sample"}', true );// @phpcs:ignore
18349 }
18350 }
18351 return $context;
18352 }
18353
18354
18355 /**
18356 * Get Late Point Customer Fields
18357 *
18358 * @param array $data data.
18359 *
18360 * @return array|void
18361 */
18362 public function search_late_point_customer_fields( $data ) {
18363 if ( ! class_exists( 'OsCustomFieldsController' ) ) {
18364
18365 return;
18366 }
18367 global $wpdb;
18368 $booking_fields = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}latepoint_settings WHERE name = %s", 'custom_fields_for_customer' ) );
18369 $fields = [];
18370 if ( ! empty( $booking_fields ) ) {
18371 $fields = json_decode( $booking_fields->value, true );
18372 }
18373 return (array) $fields;
18374 }
18375
18376 /**
18377 * Is multi person booking
18378 *
18379 * @param array $data data.
18380 *
18381 * @return array|void
18382 */
18383 public function search_late_point_multi_person_booking( $data ) {
18384 if ( ! class_exists( 'LatePointAddonGroupBookings' ) ) {
18385
18386 return;
18387 }
18388 $service_id = isset( $data['dynamic'] ) ? $data['dynamic'] : '-1';
18389
18390 if ( -1 == $service_id ) {
18391 return;
18392 }
18393 global $wpdb;
18394 $booking_details = $wpdb->get_row( $wpdb->prepare( "SELECT capacity_max,capacity_min FROM {$wpdb->prefix}latepoint_services WHERE id= %s", intval( $service_id ) ) );
18395 if ( ! empty( $booking_details ) ) {
18396 return (array) $booking_details;
18397 }
18398
18399 }
18400
18401 /**
18402 * Get Mail Mint lists
18403 *
18404 * @param array $data data.
18405 *
18406 * @return array
18407 */
18408 public function search_mail_mint_contact_lists( $data ) {
18409 global $wpdb;
18410
18411 $lists = $wpdb->get_results( $wpdb->prepare( 'SELECT title, id FROM ' . $wpdb->prefix . 'mint_contact_groups WHERE type = %s', 'lists' ) );
18412 $options = [];
18413
18414 if ( ! empty( $lists ) ) {
18415 foreach ( $lists as $list ) {
18416 $options[] = [
18417 'label' => $list->title,
18418 'value' => $list->id,
18419 ];
18420 }
18421 }
18422 return [
18423 'options' => $options,
18424 'hasMore' => false,
18425 ];
18426 }
18427
18428 /**
18429 * Get Mail Mint tags
18430 *
18431 * @param array $data data.
18432 *
18433 * @return array
18434 */
18435 public function search_mail_mint_contact_tags( $data ) {
18436 global $wpdb;
18437
18438 $lists = $wpdb->get_results( $wpdb->prepare( 'SELECT title, id FROM ' . $wpdb->prefix . 'mint_contact_groups WHERE type = %s ', 'tags' ) );
18439 $options = [];
18440
18441 if ( ! empty( $lists ) ) {
18442 foreach ( $lists as $list ) {
18443 $options[] = [
18444 'label' => $list->title,
18445 'value' => $list->id,
18446 ];
18447 }
18448 }
18449 return [
18450 'options' => $options,
18451 'hasMore' => false,
18452 ];
18453 }
18454
18455 /** Get Better Messages trigger Last Data
18456 *
18457 * @param array $data data.
18458 *
18459 * @return array|mixed|string
18460 */
18461 public function search_better_messages_triggers_data( $data ) {
18462 $context = [];
18463 global $wpdb;
18464 if ( ! function_exists( 'Better_Messages' ) ) {
18465 return [];
18466 }
18467 $sql = "SELECT * FROM {$wpdb->prefix}bm_message_messages ORDER BY id DESC LIMIT 1";
18468 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18469
18470 if ( ! empty( $results ) ) {
18471 $message = Better_Messages()->functions->get_message( $results[0]['id'] );
18472 if ( is_object( $message ) ) {
18473 $message = get_object_vars( $message );
18474 }
18475 $context['pluggable_data'] = $message;
18476 $context['pluggable_data']['sender'] = WordPress::get_user_context( $results[0]['sender_id'] );
18477 $context['response_type'] = 'live';
18478 } else {
18479 $context = json_decode( '{"pluggable_data":{"id": "21","thread_id": "11","sender_id": "79","message": "New message","date_sent": "2024-04-09 07:08:35","sender": {"wp_user_id": 79,"user_login": "johnd@gmail.com","display_name": "johnd@gmail.com","user_firstname": "John","user_lastname": "D","user_email": "johnd@gmail.com","user_role": ["customer"]}},"response_type":"sample"}', true );// @phpcs:ignore
18480 }
18481 return $context;
18482 }
18483
18484 /**
18485 * Get Appointment Hour Booking trigger Last Data
18486 *
18487 * @param array $data data.
18488 *
18489 * @return array|mixed|string
18490 */
18491 public function search_ahb_appointment_booked_triggers_last_data( $data ) {
18492 $context = [];
18493 global $wpdb;
18494 $sql = "SELECT * FROM {$wpdb->prefix}cpappbk_messages ORDER BY id DESC LIMIT 1";
18495 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18496
18497 if ( ! empty( $results ) ) {
18498 $context['pluggable_data'] = unserialize( $results[0]['posted_data'] );
18499 $context['response_type'] = 'live';
18500 } else {
18501 $context = json_decode( '{"pluggable_data":{"final_price": "1.00","final_price_short": "1","request_timestamp": "04\/10\/2024 06:56:33","apps": [{"id": 1,"cancelled": "Pending","serviceindex": 0,"service": "Service 1","duration": 60,"price": 1,"date": "2024-04-13","slot": "10:00\/11:00","military": 0,"field": "fieldname1","quant": 1,"sid": ""}],"app_service_1": "Service 1","app_status_1": "Pending","app_duration_1": 60,"app_price_1": 1,"app_date_1": "04\/13\/2024","app_slot_1": "10:00\/11:00","app_starttime_1": "10:00 AM","app_endtime_1": "11:00 AM","app_quantity_1": 1,"formid": 1,"formname": "Form 1","referrer": "https:\/\/example.com\/wp-admin\/admin.php?page=cp_apphourbooking&amp;addbk=1&amp;cal=1&amp;r=0.7819147291131667","fieldname1": " - 04\/13\/2024 10:00 AM - 11:00 AM (Service 1)\n","email": "johnd@yopmail.com","username": "admin","itemnumber": 1},"response_type":"sample"}', true );// @phpcs:ignore
18502 }
18503 return $context;
18504 }
18505
18506 /**
18507 * Prepare mailmint contact status.
18508 *
18509 * @param array $data Search Params.
18510 *
18511 * @return array
18512 */
18513 public function search_mail_mint_fetch_custom_fields( $data ) {
18514
18515 $options = [
18516 [
18517 'label' => 'Yes',
18518 'value' => 'true',
18519 ],
18520 [
18521 'label' => 'No',
18522 'value' => 'false',
18523 ],
18524 ];
18525
18526 return [
18527 'options' => $options,
18528 'hasMore' => false,
18529 ];
18530 }
18531
18532 /**
18533 * Get Mail Mint custom fields
18534 *
18535 * @param array $data data.
18536 *
18537 * @return array
18538 */
18539 public function search_mail_mint_custom_fields( $data ) {
18540 global $wpdb;
18541 $fields = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'mint_custom_fields' );
18542
18543 return (array) $fields;
18544
18545 }
18546
18547 /**
18548 * Get Mail Mint lists
18549 *
18550 * @param array $data data.
18551 *
18552 * @return array|void
18553 */
18554 public function search_mail_mint_contacts( $data ) {
18555 if ( ! class_exists( 'Mint\MRM\DataBase\Models\ContactModel' ) ) {
18556 return [];
18557 }
18558 $contacts = ContactModel::get_all();
18559 $options = [];
18560 if ( ! empty( $contacts ) ) {
18561 foreach ( $contacts['data'] as $contact ) {
18562 $options[] = [
18563 'label' => $contact['email'],
18564 'value' => $contact['id'],
18565 ];
18566 }
18567 }
18568 return [
18569 'options' => $options,
18570 'hasMore' => false,
18571 ];
18572 }
18573
18574 /**
18575 * Get mail mint Last Data
18576 *
18577 * @param array $data data.
18578 *
18579 * @return array
18580 */
18581 public function search_mail_mint_last_data( $data ) {
18582 if ( ! class_exists( 'Mint\MRM\DataBase\Models\ContactModel' ) || ! class_exists( 'Mint\MRM\DataBase\Models\ContactGroupModel' ) ) {
18583 return [];
18584 }
18585 $context = [];
18586 global $wpdb;
18587 $contact_id = isset( $data['filter']['contact_id']['value'] ) ? $data['filter']['contact_id']['value'] : -1;
18588 $term = $data['search_term'] ? $data['search_term'] : '';
18589 $type = 'lists';
18590 if ( 'mail_mint_tags_added_to_contact' === $term ) {
18591 $type = 'tags';
18592 }
18593 $data = [];
18594 if ( -1 == $contact_id ) {
18595 $result = $wpdb->get_row( $wpdb->prepare( "SELECT relation.id as rid, relation.contact_id, cgroups.* FROM {$wpdb->prefix}mint_contact_group_relationship AS relation JOIN {$wpdb->prefix}mint_contact_groups AS cgroups ON cgroups.id = relation.group_id WHERE cgroups.type = %s ORDER BY rid DESC Limit 1", $type ) );
18596 } else {
18597 $result = $wpdb->get_row( $wpdb->prepare( "SELECT relation.id as rid, relation.contact_id, cgroups.* FROM {$wpdb->prefix}mint_contact_group_relationship AS relation JOIN {$wpdb->prefix}mint_contact_groups AS cgroups ON cgroups.id = relation.group_id WHERE cgroups.type = %s AND relation.contact_id =%d ORDER BY rid DESC Limit 1", $type, $contact_id ) );
18598 }
18599
18600 if ( ! empty( $result ) ) {
18601 $contact_id = $result->contact_id;
18602 $data['contact'] = ContactModel::get( $contact_id );
18603 $data[ $type ] = ContactGroupModel::get( $result->id );
18604 }
18605 if ( ! empty( $data ) ) {
18606 $context['pluggable_data'] = $data;
18607 $context['response_type'] = 'live';
18608 } else {
18609 $context = json_decode( '{"pluggable_data":{"contact":{"id":"10","wp_user_id":"0","hash":"96eb6293345a2b54246a3214d2419948","email":"john2@test.com","first_name":"John","last_name":"Doe","scores":"0","source":"WebHook","status":"subscribed","stage":"","last_activity":null,"created_by":"0","created_at":"2024-04-09 10:28:56","updated_at":null,"meta_fields":{"dropdown":"New","radio":"One","checkbox":["One"],"status_changed":"subscribed"}},"' . $type . '":{"id":"3","title":"new","type":"tags","data":null,"created_at":"2024-04-09 11:23:11","updated_at":null}},"response_type":"sample"}', true );
18610 }
18611 return (array) $context;
18612 }
18613
18614
18615 /**
18616 * Get Simple Schedule Appointment Types
18617 *
18618 * @param array $data data.
18619 *
18620 * @return array
18621 */
18622 public function search_simple_schedule_appointment_types( $data ) {
18623 global $wpdb;
18624
18625 $appointment_types = $wpdb->get_results( 'SELECT title, id FROM ' . $wpdb->prefix . 'ssa_appointment_types' );
18626 $options = [];
18627
18628 if ( ! empty( $appointment_types ) ) {
18629 foreach ( $appointment_types as $appointment_type ) {
18630 $options[] = [
18631 'label' => $appointment_type->title,
18632 'value' => $appointment_type->id,
18633 ];
18634 }
18635 }
18636 return [
18637 'options' => $options,
18638 'hasMore' => false,
18639 ];
18640 }
18641
18642 /**
18643 * Get mail mint Last Data
18644 *
18645 * @param array $data data.
18646 *
18647 * @return array
18648 */
18649 public function search_ssa_last_data( $data ) {
18650 $context = [];
18651 global $wpdb;
18652 $appointment_type_id = isset( $data['filter']['appointment_type_id']['value'] ) ? $data['filter']['appointment_type_id']['value'] : -1;
18653 $term = $data['search_term'] ? $data['search_term'] : '';
18654 $status = 'booked';
18655 if ( 'ssa_appointment_cancelled' === $term ) {
18656 $status = 'canceled';
18657 }
18658 $data = [];
18659 if ( -1 == $appointment_type_id ) {
18660 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointments WHERE status = %s ORDER BY id DESC Limit 1", $status ), ARRAY_A );
18661 } else {
18662 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointments where appointment_type_id=%d AND status = %s ORDER BY id DESC Limit 1", $appointment_type_id, $status ), ARRAY_A );
18663 }
18664
18665
18666 if ( ! empty( $result ) ) {
18667 $result_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointment_meta where appointment_id=%d", $result['id'] ) );
18668 if ( ! empty( $result_meta ) ) {
18669 foreach ( $result_meta as $meta ) {
18670 $result[ $meta->meta_key ] = $meta->meta_value;
18671 }
18672 }
18673 }
18674 if ( ! empty( $result ) ) {
18675
18676 $result['customer_information'] = json_decode( $result['customer_information'], true );
18677 $context['pluggable_data'] = $result;
18678 $context['response_type'] = 'live';
18679 } else {
18680 $context = json_decode( '{"pluggable_data":{"id":"4","appointment_type_id":"1","rescheduled_from_appointment_id":"0","rescheduled_to_appointment_id":"0","group_id":"0","author_id":"1","customer_id":"0","customer_information":{"Name":"John Doe","Email":"johndoe@email.com"},"customer_timezone":"Asia\/Calcutta","customer_locale":"en_US","start_date":"2024-04-22 03:30:00","end_date":"2024-04-22 04:00:00","title":"","description":"","payment_method":"","payment_received":"0.00","mailchimp_list_id":"","google_calendar_id":"","google_calendar_event_id":"","web_meeting_password":"","web_meeting_id":"","web_meeting_url":"","allow_sms":"","status":"booked","date_created":"2024-04-18 09:19:14","date_modified":"2024-04-18 09:19:14","expiration_date":"0000-00-00 00:00:00"},"response_type":"sample"}', true );
18681 }
18682 return (array) $context;
18683 }
18684
18685 /**
18686 * Get Simple Schedule Appointment Types
18687 *
18688 * @param array $data data.
18689 *
18690 * @return array
18691 */
18692 public function search_simple_schedule_appointments( $data ) {
18693 global $wpdb;
18694
18695 $appointment_types = $wpdb->get_results( 'SELECT title, id FROM ' . $wpdb->prefix . 'ssa_appointment_types' );
18696 $options = [];
18697
18698 if ( ! empty( $appointment_types ) ) {
18699 foreach ( $appointment_types as $appointment_type ) {
18700 $options[] = [
18701 'label' => $appointment_type->title,
18702 'value' => $appointment_type->id,
18703 ];
18704 }
18705 }
18706 return [
18707 'options' => $options,
18708 'hasMore' => false,
18709 ];
18710 }
18711
18712 /**
18713 * Get Sensei LMS Lessons
18714 *
18715 * @param array $data data.
18716 *
18717 * @return array
18718 */
18719 public function search_sensei_lms_lessons( $data ) {
18720 if ( ! function_exists( 'Sensei' ) ) {
18721 return [];
18722 }
18723 $course_id = $data['dynamic'];
18724 $lessons = \Sensei()->course->course_lessons( $course_id, 'publish', 'ids' );
18725 $options = [];
18726
18727 if ( ! empty( $lessons ) ) {
18728 foreach ( $lessons as $lesson ) {
18729 $options[] = [
18730 'label' => get_the_title( $lesson ),
18731 'value' => $lesson,
18732 ];
18733 }
18734 }
18735 return [
18736 'options' => $options,
18737 'hasMore' => false,
18738 ];
18739 }
18740
18741 /**
18742 * Get Sensei LMS Quiz List
18743 *
18744 * @param array $data data.
18745 *
18746 * @return array
18747 */
18748 public function search_sensei_lms_quiz( $data ) {
18749 $page = $data['page'];
18750 $limit = Utilities::get_search_page_limit();
18751 $offset = $limit * ( $page - 1 );
18752
18753 $quizes = get_posts(
18754 [
18755 'post_type' => 'lesson',
18756 'orderby' => 'title',
18757 'order' => 'ASC',
18758 'post_status' => 'publish',
18759 'posts_per_page' => $limit,
18760 'offset' => $offset,
18761 'fields' => 'ids',
18762 'meta_query' => [
18763 'relation' => 'AND',
18764 [
18765 'key' => '_lesson_quiz',
18766 'compare' => 'EXISTS',
18767 ],
18768 [
18769 'key' => '_quiz_has_questions',
18770 'value' => 1,
18771 'compare' => '=',
18772 ],
18773 ],
18774 ]
18775 );
18776 $options = [];
18777 $total_quizes = get_posts(
18778 [
18779 'post_type' => 'lesson',
18780 'orderby' => 'title',
18781 'order' => 'ASC',
18782 'post_status' => 'publish',
18783 'posts_per_page' => -1,
18784 'fields' => 'ids',
18785 'meta_query' => [
18786 'relation' => 'AND',
18787 [
18788 'key' => '_lesson_quiz',
18789 'compare' => 'EXISTS',
18790 ],
18791 [
18792 'key' => '_quiz_has_questions',
18793 'value' => 1,
18794 'compare' => '=',
18795 ],
18796 ],
18797 ]
18798 );
18799 $count = count( $total_quizes );
18800 if ( ! empty( $quizes ) ) {
18801 foreach ( $quizes as $quiz ) {
18802 $options[] = [
18803 'label' => get_the_title( $quiz ),
18804 'value' => $quiz,
18805 ];
18806 }
18807 }
18808 return [
18809 'options' => $options,
18810 'hasMore' => $count > $limit && $count > $offset,
18811 ];
18812 }
18813
18814 /**
18815 * Get Sensei LMS trigger Last Data
18816 *
18817 * @param array $data data.
18818 *
18819 * @return array|mixed|string
18820 */
18821 public function search_sensei_lms_triggers_last_data( $data ) {
18822 $context = [];
18823 global $wpdb;
18824 $term = $data['search_term'];
18825
18826 if ( ! function_exists( 'Sensei' ) ) {
18827 return;
18828 }
18829
18830 if ( 'course_completed' == $term ) {
18831 $comment_type = 'sensei_course_status';
18832 $course_id = $data['filter']['sensei_course']['value'];
18833 if ( -1 === $course_id ) {
18834 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s ORDER BY comment_ID DESC LIMIT 1";
18835 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type ), ARRAY_A );// @phpcs:ignore
18836 } else {
18837 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s AND comment_post_ID = %d ORDER BY comment_ID DESC LIMIT 1";
18838 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, $course_id ), ARRAY_A );// @phpcs:ignore
18839 }
18840 } elseif ( 'course_enrolled' == $term ) {
18841 $comment_type = 'sensei_course_status';
18842 $course_id = $data['filter']['sensei_course']['value'];
18843 if ( -1 === $course_id ) {
18844 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s ORDER BY comment_ID DESC LIMIT 1";
18845 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'in-progress', $comment_type ), ARRAY_A );// @phpcs:ignore
18846 } else {
18847 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s AND comment_post_ID = %d ORDER BY comment_ID DESC LIMIT 1";
18848 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'in-progress', $comment_type, $course_id ), ARRAY_A );// @phpcs:ignore
18849 }
18850 } elseif ( 'lesson_completed' == $term ) {
18851 $lesson_id = $data['filter']['sensei_lesson']['value'];
18852 $course_id = $data['filter']['sensei_course']['value'];
18853 $comment_type = 'sensei_lesson_status';
18854 if ( -1 === $lesson_id ) {
18855 $sql = "SELECT comments.*, meta.* FROM {$wpdb->prefix}comments as comments
18856 LEFT JOIN {$wpdb->prefix}postmeta as meta
18857 ON comments.comment_post_ID = meta.post_id
18858 WHERE comments.comment_approved = %s
18859 AND comments.comment_type = %s
18860 AND meta.meta_key = %s AND
18861 meta.meta_value = %d
18862 ORDER BY comments.comment_ID DESC LIMIT 1";
18863 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, '_lesson_course', $course_id ), ARRAY_A );// @phpcs:ignore
18864 } else {
18865 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s AND comment_post_ID = %d ORDER BY comment_ID DESC LIMIT 1";
18866 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, $lesson_id ), ARRAY_A );// @phpcs:ignore
18867 }
18868 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term ) {
18869 $quiz_id = $data['filter']['sensei_quiz']['value'];
18870 $comment_type = 'sensei_lesson_status';
18871 if ( 'quiz_passes' == $term ) {
18872 $status = "( comment_approved = 'passed' )";
18873 } elseif ( 'quiz_fails' == $term ) {
18874 $status = "( comment_approved = 'failed' )";
18875 } else {
18876 $status = "( comment_approved = 'failed' OR comment_approved = 'passed' )";
18877 }
18878 if ( -1 === $quiz_id ) {
18879 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type = %s AND $status ORDER BY comment_ID DESC LIMIT 1";
18880 $results = $wpdb->get_results( $wpdb->prepare( $sql, $comment_type ), ARRAY_A );// @phpcs:ignore
18881 } else {
18882 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type = %s AND $status AND comment_post_ID = %d ORDER BY comment_ID DESC LIMIT 1";
18883 $results = $wpdb->get_results( $wpdb->prepare( $sql, $comment_type, $quiz_id ), ARRAY_A );// @phpcs:ignore
18884 }
18885 } elseif ( 'quiz_completion' == $term ) {
18886 $quiz_id = $data['filter']['sensei_quiz']['value'];
18887 $comment_type = 'sensei_lesson_status';
18888 $condition_compare = $data['filter']['condition_compare']['value'];
18889 $percentage = $data['filter']['percentage']['value'];
18890 if ( -1 === $quiz_id ) {
18891 $results = $wpdb->get_results( $wpdb->prepare( "SELECT comments.*, meta.meta_key, meta.meta_value FROM wp_comments AS comments JOIN wp_commentmeta AS meta ON comments.comment_ID = meta.comment_id WHERE meta.meta_key LIKE %s AND meta.meta_value $condition_compare %d ORDER BY comments.comment_ID DESC LIMIT 1", 'grade', $percentage ), ARRAY_A ); //phpcs:ignore
18892 } else {
18893 $results = $wpdb->get_results( $wpdb->prepare( "SELECT comments.*, meta.meta_key, meta.meta_value FROM wp_comments AS comments JOIN wp_commentmeta AS meta ON comments.comment_ID = meta.comment_id WHERE comments.comment_post_ID = %d AND meta.meta_key LIKE %s AND meta.meta_value $condition_compare %d ORDER BY comments.comment_ID DESC LIMIT 1", $quiz_id, 'grade', $percentage ), ARRAY_A ); //phpcs:ignore
18894 }
18895 }
18896
18897 if ( ! empty( $results ) ) {
18898 $post = get_post( $results[0]['comment_post_ID'] );
18899 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['user_id'] );
18900 if ( 'course_completed' == $term || 'course_enrolled' == $term ) {
18901 $context['pluggable_data']['sensei_course'] = $results[0]['comment_post_ID'];
18902 if ( $post instanceof \WP_Post ) {
18903 $context['pluggable_data']['course_title'] = $post->post_title;
18904 }
18905 } elseif ( 'lesson_completed' == $term ) {
18906 $context['pluggable_data']['sensei_lesson'] = $results[0]['comment_post_ID'];
18907 if ( $post instanceof \WP_Post ) {
18908 $context['pluggable_data']['lesson_title'] = $post->post_title;
18909 }
18910 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term || 'quiz_completion' == $term ) {
18911 $lesson_quiz = get_post_meta( $results[0]['comment_post_ID'], '_lesson_quiz', true );
18912 $submission = \Sensei()->quiz_submission_repository->get( $lesson_quiz, $results[0]['user_id'] );
18913 if ( $post instanceof \WP_Post ) {
18914 $context['pluggable_data']['quiz_title'] = $post->post_title;
18915 }
18916 $context['pluggable_data']['quiz_status'] = $results[0]['comment_approved'];
18917 $context['pluggable_data']['quiz_data']['id'] = $submission->get_id();
18918 $context['sensei_quiz'] = $submission->get_id();
18919 $context['pluggable_data']['quiz_data']['final_grade'] = $submission->get_final_grade();
18920 $context['pluggable_data']['quiz_data']['created_at'] = $submission->get_created_at();
18921 }
18922 $context['response_type'] = 'live';
18923 } else {
18924 if ( 'course_completed' == $term || 'course_enrolled' == $term ) {
18925 $context = json_decode( '{"pluggable_data":{"wp_user_id": 48,"user_login": "john@yopmail.com","display_name": "john@yopmail.com","user_firstname": "john","user_lastname": "d","user_email": "johnd@yopmail.com","user_role": ["subscriber"],"sensei_course": "7500","course_title": "Course 1"},"response_type":"sample"}', true );// @phpcs:ignore
18926 } elseif ( 'lesson_completed' == $term ) {
18927 $context = json_decode( '{"pluggable_data":{"wp_user_id": 48,"user_login": "john@yopmail.com","display_name": "john@yopmail.com","user_firstname": "john","user_lastname": "d","user_email": "johnd@yopmail.com","user_role": ["subscriber"],"sensei_lesson": "7502","lesson_title": "Lesson 1"},"response_type":"sample"}', true );// @phpcs:ignore
18928 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term || 'quiz_completion' == $term ) {
18929 $context = json_decode( '{"pluggable_data":{"wp_user_id":2,"user_login":"johnd","display_name":"john d","user_firstname":"john","user_lastname":"d","user_email":"johnd@email.com","user_role":[],"quiz_title":"Lesson 2","quiz_status":"passed","quiz_data":{"id":27,"final_grade":100,"created_at":{"date":"2024-05-07 06:54:31.000000","timezone_type":1,"timezone":"+00:00"}}},"response_type":"sample"}', true );// @phpcs:ignore
18930 }
18931 }
18932 return $context;
18933 }
18934
18935 /**
18936 * Get SurelyWP Services - SureCart Addons Last Data
18937 *
18938 * @param array $data data.
18939 *
18940 * @return array
18941 */
18942 public function search_services_sc_triggers_last_data( $data ) {
18943 $context = [];
18944 global $wpdb;
18945 $term = $data['search_term'] ? $data['search_term'] : '';
18946 $data = [];
18947
18948 if ( 'new_service_created' === $term ) {
18949 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_status LIKE 'service_created' ORDER BY service_id DESC Limit 1", ARRAY_A );
18950 } elseif ( 'requirement_submitted' === $term ) {
18951 $result = $wpdb->get_row( "SELECT created_at FROM {$wpdb->prefix}surelywp_sv_requirements ORDER BY created_at DESC Limit 1", ARRAY_A );
18952 } elseif ( 'message_sent' === $term ) {
18953 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_messages ORDER BY message_id DESC Limit 1", ARRAY_A );
18954 } elseif ( 'message_final_delivery_sent' === $term ) {
18955 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_messages WHERE is_final_delivery = 1 ORDER BY message_id DESC Limit 1", ARRAY_A );
18956 } elseif ( 'customer_request_revision' === $term ) {
18957 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_status LIKE 'service_start' ORDER BY service_id DESC Limit 1", ARRAY_A );
18958 } elseif ( 'customer_approves_final_delivery' === $term ) {
18959 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_status LIKE 'service_complete' ORDER BY service_id DESC Limit 1", ARRAY_A );
18960 } elseif ( 'delivery_date_changed' === $term ) {
18961 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE delivery_date IS NOT NULL ORDER BY service_id DESC Limit 1", ARRAY_A );
18962 } elseif ( 'service_cancel' === $term || 'service_marked_canceled' === $term ) {
18963 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_status LIKE 'service_canceled' ORDER BY service_id DESC Limit 1", ARRAY_A );
18964 } elseif ( 'service_completed' === $term || 'service_mark_completed' === $term ) {
18965 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_status LIKE 'service_complete' ORDER BY service_id DESC Limit 1", ARRAY_A );
18966 } elseif ( 'contract_signed' === $term ) {
18967 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_contracts ORDER BY contract_id DESC Limit 1", ARRAY_A );
18968 }
18969 if ( ! empty( $result ) ) {
18970 if ( 'new_service_created' === $term ) {
18971 $service_data = [
18972 'service_setting_id' => $result['service_id'],
18973 'order_id' => $result['order_id'],
18974 'product_id' => $result['product_id'],
18975 'service_status' => $result['service_status'],
18976 'delivery_date' => $result['delivery_date'],
18977 ];
18978 $context['pluggable_data'] = array_merge( $service_data, WordPress::get_user_context( $result['user_id'] ) );
18979 $context['response_type'] = 'live';
18980 } elseif ( 'requirement_submitted' === $term ) {
18981 $requirements_data = $wpdb->get_results(
18982 $wpdb->prepare(
18983 "SELECT * FROM {$wpdb->prefix}surelywp_sv_requirements WHERE created_at = %s",
18984 $result['created_at']
18985 ),
18986 ARRAY_A
18987 );
18988 $service_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_id = %d", $requirements_data[0]['service_id'] ), ARRAY_A );
18989 $user_data = WordPress::get_user_context( $service_result['user_id'] );
18990 unset( $service_result['user_id'] );
18991 $context['pluggable_data'] = array_merge( $requirements_data, $service_result, $user_data );
18992 foreach ( $requirements_data as $value ) {
18993 if ( 'file' == $value['requirement_type'] ) {
18994 $upload_dir = wp_upload_dir();
18995 $attachment_file_names = json_decode( $value['requirement'], true );
18996 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
18997 $context['pluggable_data']['requirement_attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $value['service_id'] . '/requirement/' . $attachment_file_name;
18998 }
18999 }
19000 }
19001 $context['response_type'] = 'live';
19002 } elseif ( 'message_sent' === $term || 'message_final_delivery_sent' === $term ) {
19003 $message_data = [
19004 'sender' => WordPress::get_user_context( $result['sender_id'] ),
19005 'receiver' => WordPress::get_user_context( $result['receiver_id'] ),
19006 'service_id' => $result['service_id'],
19007 'message_text' => $result['message_text'],
19008 'attachment_file_name' => $result['attachment_file_name'],
19009 'is_final_delivery' => $result['is_final_delivery'],
19010 ];
19011 $context['pluggable_data'] = $message_data;
19012 $upload_dir = wp_upload_dir();
19013 $attachment_file_names = json_decode( $result['attachment_file_name'], true );
19014 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
19015 $context['pluggable_data']['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $result['service_id'] . '/messages/' . $attachment_file_name;
19016 }
19017 $context['response_type'] = 'live';
19018 } elseif ( 'customer_request_revision' === $term ) {
19019 $message_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sv_messages WHERE service_id = %d AND is_final_delivery = %d ORDER BY message_id DESC LIMIT 1", $result['service_id'], 1 ), ARRAY_A );
19020 if ( ! empty( $message_result ) ) {
19021 global $surelywp_sv_model;
19022 $revision_message = $surelywp_sv_model->surelywp_sv_get_customer_revision_msg( $message_result['service_id'], $message_result['message_id'] );
19023 $context['pluggable_data'] = array_merge( $result, $revision_message );
19024 $context['pluggable_data']['sender'] = WordPress::get_user_context( $revision_message['sender_id'] );
19025 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $revision_message['receiver_id'] );
19026 $upload_dir = wp_upload_dir();
19027 $attachment_file_names = json_decode( $revision_message['attachment_file_name'], true );
19028 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
19029 $context['pluggable_data']['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $revision_message['service_id'] . '/messages/' . $attachment_file_name;
19030 }
19031 $context['response_type'] = 'live';
19032 } else {
19033 $context = json_decode( '{"pluggable_data":{"service_id":"6","service_setting_id":"kXg4Exmj","user_id":"51","order_id":"16574f7f-66d8-466e-8716-8da9671e6668","product_id":"f59f62cc-fd70-4007-8bcf-56d07f1ac871","service_status":"service_start","delivery_date":"2024-08-29","created_at":"2024-08-25 22:42:57","updated_at":null,"message_id":"13","sender_id":"41","receiver_id":"0","message_text":"This is the final revision","attachment_file_name":"[\"test_copy-1724605977.pdf\",\"test-1724605977.pdf\"]","is_final_delivery":"0","is_approved_delivery":null,"sender":{"wp_user_id":41,"user_login":"john@example.com","display_name":"john@example.com","user_firstname":"john","user_lastname":"d","user_email":"john@example.com","user_registered":"2023-01-30 09:34:54","user_role":["customer"]},"receiver":[],"attachment_file":["https://example.com/wp-content/uploads/surelywp-services-data/6/messages/test_copy-1724605977.pdf","https://example.com/wp-content/uploads/surelywp-services-data/6/messages/test-1724605977.pdf"]},"response_type":"sample"}', true );
19034 }
19035 } elseif ( 'customer_approves_final_delivery' === $term || 'delivery_date_changed' === $term || 'service_cancel' === $term || 'service_marked_canceled' === $term || 'service_completed' === $term || 'service_mark_completed' === $term ) {
19036 $user_data = WordPress::get_user_context( $result['user_id'] );
19037 unset( $result['user_id'] );
19038 $context['pluggable_data'] = array_merge( $result, $user_data );
19039 $context['response_type'] = 'live';
19040 } elseif ( 'contract_signed' === $term ) {
19041 $contract_data = [
19042 'service_id' => $result['service_id'],
19043 'signature' => $result['signature'],
19044 'contract_details' => $result['contract_details'],
19045 ];
19046 $service_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_id = %d", $result['service_id'] ), ARRAY_A );
19047 $user_data = WordPress::get_user_context( $service_result['user_id'] );
19048 unset( $service_result['user_id'] );
19049 $context['pluggable_data'] = array_merge( $contract_data, $user_data, $service_result );
19050 $context['response_type'] = 'live';
19051 }
19052 } else {
19053 if ( 'new_service_created' === $term || 'customer_request_revision' === $term || 'customer_approves_final_delivery' === $term || 'delivery_date_changed' === $term ) {
19054 $context = json_decode( '{"pluggable_data":{"service_setting_id":"3","order_id":"81ac6e4f-1f8a-4f8b-a3d1-37fba6c8f893","product_id":"f59f62cc-fd70-4007-8bcf-56d07f1ac871","service_status":"service_created","delivery_date":null,"wp_user_id":84,"user_login":"johnd@example.com","display_name":"johnd@example.com","user_firstname":"John","user_lastname":"D","user_email":"johnd@example.com","user_registered":"2023-02-02 13:08:44","user_role":["customer"]},"response_type":"sample"}', true );
19055 } elseif ( 'requirement_submitted' === $term ) {
19056 $context = json_decode( '{"pluggable_data":{"0":{"requirement_id":"6","service_id":"7","requirement_type":"textarea","requirement_title":"TestReq","requirement_desc":"This is a testing requirement","requirement":"This is my requirements.","created_at":"2024-08-26 10:27:33","updated_at":null},"1":{"requirement_id":"7","service_id":"7","requirement_type":"file","requirement_title":"Upload Photos","requirement_desc":"Please upload reference photos","requirement":"[\"test_copy-1724648253.pdf\",\"test-1724648253.pdf\"]","created_at":"2024-08-26 10:27:33","updated_at":null},"service_id":"7","service_setting_id":"kXg4Exmj","order_id":"16574f7f-66d8-466e-8716-8da9671e6668","product_id":"f59f62cc-fd70-4007-8bcf-56d07f1ac871","service_status":"service_start","delivery_date":"2024-08-29","created_at":"2024-08-26 10:22:31","updated_at":"2024-08-26 04:57:36","wp_user_id":51,"user_login":"johnd@example.com","display_name":"johnd@example.com","user_firstname":"john","user_lastname":"d","user_email":"johnd@example.com","user_registered":"2023-02-02 07:12:46","user_role":["customer"],"requirement_attachment_file":["https://example.com/wp-content/uploads/surelywp-services-data/7/requirement/test_copy-1724648253.pdf","https://example.com/wp-content/uploads/surelywp-services-data/7/requirement/test-1724648253.pdf"]},"response_type":"sample"}', true );
19057 } elseif ( 'message_sent' === $term || 'message_final_delivery_sent' === $term ) {
19058 $context = json_decode( '{"pluggable_data":{"sender":{"wp_user_id":84,"user_login":"johnd@example.com","display_name":"johnd@example.com","user_firstname":"john","user_lastname":"d","user_email":"johnd@example.com","user_registered":"2023-02-02 13:08:44","user_role":["customer"]},"receiver_id": {"wp_user_id":8,"user_login":"johnde@example.com","display_name":"johnde@example.com","user_firstname":"johnd","user_lastname":"ed","user_email":"johnde@example.com","user_registered":"2023-02-02 13:08:44","user_role":["admin"]},"service_id": "1","message_text": "Message Text","attachment_file_name": "Attachment File Name","is_final_delivery": "1", "attachment_file":["https://example.com/wp-content/uploads/surelywp-services-data/6/messages/test_copy-1724605977.pdf","https://example.com/wp-content/uploads/surelywp-services-data/6/messages/test-1724605977.pdf"]},"response_type":"sample"}', true );
19059 } elseif ( 'service_cancel' === $term ) {
19060 $context = json_decode( '{"pluggable_data":{"delivery_date": null,"order_id": "a3830048-9a43-4088-a78e-285537f16ecc","product_id": "f59f62cc-fd70-4007-8bcf-56d07f1ac871","service_setting_id": "2","service_status": "service_canceled","wp_user_id":84,"user_login":"johnd@example.com","display_name":"johnd@example.com","user_firstname":"John","user_lastname":"D","user_email":"johnd@example.com","user_registered":"2023-02-02 13:08:44","user_role":["customer"]},"response_type":"sample"}', true );
19061 } elseif ( 'service_marked_canceled' === $term || 'service_completed' === $term || 'service_mark_completed' === $term ) {
19062 $context = json_decode( '{"pluggable_data":{"delivery_date": null,"order_id": "a3830048-9a43-4088-a78e-285537f16ecc","product_id": "f59f62cc-fd70-4007-8bcf-56d07f1ac871","service_setting_id": "2","service_status": "service_completed","wp_user_id":84,"user_login":"johnd@example.com","display_name":"johnd@example.com","user_firstname":"John","user_lastname":"D","user_email":"johnd@example.com","user_registered":"2023-02-02 13:08:44","user_role":["customer"]},"response_type":"sample"}', true );
19063 } elseif ( 'contract_signed' === $term ) {
19064 $context = json_decode( '{"pluggable_data":{"service_id":"4","signature":"signature","contract_details":"Contract Details","wp_user_id":84,"user_login":"johnd@example.com","display_name":"johnd@example.com","user_firstname":"John","user_lastname":"D","user_email":"johnd@example.com","user_registered":"2023-02-02 13:08:44","user_role":["customer"],"service_setting_id":"u2pDYtDF","user_id":"84","order_id":"8e8ca710-13cd-4c94-8de5-98a19a3b9de6","product_id":"a39c7d4f-50bd-49ba-b56c-4f17aac61306","service_status":"service_start","delivery_date":"2024-08-25","created_at":"2024-08-22 15:15:27","updated_at":"2024-08-22 09:46:02"},"response_type":"sample"}', true );
19065 }
19066 }
19067 return (array) $context;
19068 }
19069
19070 /**
19071 * Get SurelyWP Support Portal - SureCart Addons Last Data
19072 *
19073 * @param array $data data.
19074 *
19075 * @return array
19076 */
19077 public function search_sc_support_portal_triggers_last_data( $data ) {
19078 $context = [];
19079 if ( ! class_exists( 'Surelywp_Support_Portal' ) ) {
19080 return [];
19081 }
19082 global $wpdb;
19083 $term = $data['search_term'] ? $data['search_term'] : '';
19084 $data = [];
19085
19086 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
19087 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sp_support WHERE support_status = 1 ORDER BY support_id DESC Limit 1", ARRAY_A );
19088 } elseif ( 'new_message_sent' === $term ) {
19089 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sp_messages ORDER BY message_id DESC Limit 1", ARRAY_A );
19090 } elseif ( 'tickets_closed' === $term ) {
19091 $result = $wpdb->get_results( "SELECT support_id FROM {$wpdb->prefix}surelywp_sp_support WHERE support_status = 0 AND updated_at IN ( SELECT updated_at FROM {$wpdb->prefix}surelywp_sp_support WHERE support_status = 0 GROUP BY updated_at HAVING COUNT(*) > 1);", ARRAY_A );
19092 } elseif ( 'tickets_opens' === $term ) {
19093 $result = $wpdb->get_results( "SELECT support_id FROM {$wpdb->prefix}surelywp_sp_support WHERE support_status = 3 AND updated_at IN ( SELECT updated_at FROM {$wpdb->prefix}surelywp_sp_support WHERE support_status = 3 GROUP BY updated_at HAVING COUNT(*) > 1);", ARRAY_A );
19094 } elseif ( 'new_email_message_fetched' === $term ) {
19095 $context = json_decode( '{"pluggable_data":{"mail_id": 21,"mail_message_id": 20,"mail_references": "test","mail_subject": "test","sender_type": "admin","sender_id": 1,"receiver_id": 1,"support_id": 1,"message_text": "test","attachment_tmp_paths": "var/www","attachment_file_name": "test.png","created_at": "2024-10-10 00:00:00"},"response_type":"sample"}', true );
19096 }
19097 if ( ! empty( $result ) ) {
19098 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
19099 $support_res = $wpdb->get_results( $wpdb->prepare( "SELECT field_label, field_value FROM {$wpdb->prefix}surelywp_sp_support_form_fields WHERE support_id = %d", $result['support_id'] ), ARRAY_A );
19100 $support_data = [
19101 'support_id' => $result['support_id'],
19102 'order_id' => $result['order_id'],
19103 'product_id' => $result['product_id'],
19104 'support_title' => $result['support_title'],
19105 'support_status' => Surelywp_Support_Portal::surelywp_sp_get_support_status( $result['support_status'] ),
19106 'support_data' => $support_res,
19107 ];
19108 $context['pluggable_data'] = array_merge( $support_data, WordPress::get_user_context( $result['user_id'] ) );
19109 $context['response_type'] = 'live';
19110 } elseif ( 'tickets_closed' === $term || 'tickets_opens' === $term ) {
19111 $support_data = [];
19112 $support_ids = $result;
19113 foreach ( $support_ids as $key => $id ) {
19114 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sp_support WHERE support_id = %d", $id ), ARRAY_A );
19115 $support_res = $wpdb->get_results( $wpdb->prepare( "SELECT field_label, field_value FROM {$wpdb->prefix}surelywp_sp_support_form_fields WHERE support_id = %d", $id ), ARRAY_A );
19116 $support_data[ $key ] = WordPress::get_user_context( $result['user_id'] );
19117 $support_data[ $key ] = [
19118 'support_id' => $result['support_id'],
19119 'order_id' => $result['order_id'],
19120 'product_id' => $result['product_id'],
19121 'support_title' => $result['support_title'],
19122 'support_status' => Surelywp_Support_Portal::surelywp_sp_get_support_status( $result['support_status'] ),
19123 'support_data' => $support_res,
19124 ];
19125 }
19126 $context['pluggable_data'] = $support_data;
19127 $context['response_type'] = 'live';
19128 } elseif ( 'new_message_sent' === $term ) {
19129 $upload_dir = wp_upload_dir();
19130 $support_data = [
19131 'sender_id' => $result['sender_id'],
19132 'receiver_id' => $result['receiver_id'],
19133 'support_id' => $result['support_id'],
19134 'message_text' => $result['message_text'],
19135 'sender' => WordPress::get_user_context( $result['sender_id'] ),
19136 'receiver' => WordPress::get_user_context( $result['receiver_id'] ),
19137 ];
19138 if ( ! empty( $result['attachment_file_name'] ) ) {
19139 $attachment_file_names = json_decode( $result['attachment_file_name'], true );
19140 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
19141 $support_data['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-support-portal-data/' . $result['support_id'] . '/messages/' . $attachment_file_name;
19142 }
19143 }
19144 $context['pluggable_data'] = $support_data;
19145 $context['response_type'] = 'live';
19146 }
19147 } else {
19148 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
19149 $context = json_decode( '{"pluggable_data":{"support_id":"1","order_id":"0d0c3a6a-9846-42d0-9bc2-84485985358c","product_id":"a39c7d4f-50bd-49ba-b56c-4f17aac61306","support_title":"Not Delivered","support_status":"Closed","wp_user_id":84,"user_login":"johnd@gmail.com","display_name":"john","user_firstname":"john","user_lastname":"john","user_email":"johnd@gmail.com","user_registered":"2023-02-02 13:08:44","user_role":["customer"]},"response_type":"sample"}', true );
19150 } elseif ( 'tickets_closed' === $term || 'tickets_opens' === $term ) {
19151 $context = json_decode( '{"pluggable_data":[{"support_id":"1","order_id":"0d0c3a6a-9846-42d0-9bc2-84485985358c","product_id":"a39c7d4f-50bd-49ba-b56c-4f17aac61306","support_title":"Not Delivered","support_status":"Closed"}],"response_type":"sample"}', true );
19152 } elseif ( 'new_message_sent' === $term ) {
19153 $context = json_decode( '{"pluggable_data":{"sender_id":"1","receiver_id":"84","support_id":"2","message_text":"<p>asdasd</p>","sender":{"wp_user_id":1,"user_login":"johnd","display_name":"johnd","user_firstname":"john","user_lastname":"d","user_email":"johnd@example.com","user_registered":"2023-01-16 09:23:31","user_role":{"0":"customer"}},"receiver":{"wp_user_id":84,"user_login":"johnd@gmail.com","display_name":"johnd","user_firstname":"johnny","user_lastname":"d","user_email":"johndd@gmail.com","user_registered":"2023-02-02 13:08:44","user_role":["customer"]}},"response_type":"sample"}', true );
19154 }
19155 }
19156 return (array) $context;
19157 }
19158
19159 /**
19160 * Get Fluent Boards Last Data
19161 *
19162 * @param array $data data.
19163 *
19164 * @return array
19165 */
19166 public function search_fbs_triggers_last_data( $data ) {
19167 $context = [];
19168 global $wpdb;
19169 $term = $data['search_term'] ? $data['search_term'] : '';
19170 if ( ! class_exists( 'FluentBoards\App\Models\Board' ) || ! class_exists( 'FluentBoards\App\Models\User' ) ) {
19171 return [];
19172 }
19173 $data = [];
19174
19175 if ( 'board_created' === $term ) {
19176 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_boards ORDER BY id DESC Limit 1", ARRAY_A );
19177 } elseif ( 'board_member_added' === $term ) {
19178 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_relations WHERE object_type = 'board_user' ORDER BY id DESC Limit 1", ARRAY_A );
19179 } elseif ( 'task_created' === $term ) {
19180 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_tasks ORDER BY id DESC Limit 1", ARRAY_A );
19181 }
19182 if ( ! empty( $result ) ) {
19183 if ( 'board_created' === $term ) {
19184 $board_id = $result['id'];
19185 $context['pluggable_data'] = \FluentBoards\App\Models\Board::findOrFail( $board_id );
19186 $context['response_type'] = 'live';
19187 } elseif ( 'board_member_added' === $term ) {
19188 $context['pluggable_data']['board_id'] = $result['object_id'];
19189 $context['pluggable_data']['board_member'] = \FluentBoards\App\Models\User::find( $result['foreign_id'] );
19190 $context['response_type'] = 'live';
19191 } elseif ( 'task_created' === $term ) {
19192 $context['pluggable_data'] = $result;
19193 $context['response_type'] = 'live';
19194 }
19195 } else {
19196 if ( 'board_created' === $term ) {
19197 $context = json_decode( '{"pluggable_data":{"id":2,"parent_id":null,"title":"testing","description":"testing","type":"to-do","currency":"USD","background":{"id":"solid_1","is_image":false,"image_url":null,"color":"#d1d8e0"},"settings":null,"created_by":"1","archived_at":null,"meta":[]},"response_type":"sample"}', true );
19198 } elseif ( 'board_member_added' === $term ) {
19199 $context = json_decode( '{"pluggable_data":{"board_id":"2","board_member":{"ID":1,"user_login":"johnd","user_nicename":"johnd","user_email":"johnd@example.com","user_url":"https://example.com","user_registered":"2023-01-16 09:23:31","user_status":"0","display_name":"johnd","photo":"https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2Fodeploll/128"}},"response_type":"sample"}', true );
19200 } elseif ( 'task_created' === $term ) {
19201 $context = json_decode( '{"pluggable_data":{"id":"1","parent_id":null,"board_id":"1","crm_contact_id":null,"title":"Task1","slug":"task1","type":"task","status":"open","stage_id":"6","source":"web","source_id":null,"priority":"low","description":null,"lead_value":"0.00","created_by":"1","position":"1.00","comments_count":"0","issue_number":null,"reminder_type":"none","settings":"a:4:{s:5:\"cover\";a:1:{s:15:\"backgroundColor\";s:0:\"\";}s:13:\"subtask_count\";i:0;s:16:\"attachment_count\";i:0;s:23:\"subtask_completed_count\";i:0;}","remind_at":null,"started_at":null,"due_at":null,"last_completed_at":null,"archived_at":null,"created_at":"2024-10-14 17:11:20","updated_at":"2024-10-14 17:11:20"},"response_type":"sample"}', true );
19202 }
19203 }
19204 return (array) $context;
19205 }
19206 }
19207
19208 GlobalSearchController::get_instance();
19209