PluginProbe
OttoKit: All-in-One Automation Platform / 1.1.20
OttoKit: All-in-One Automation Platform v1.1.20
1.1.38 1.1.37 1.1.36 1.1.35 1.1.34 1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 All 124 releases
suretriggers / src / Controllers / GlobalSearchController.php

GlobalSearchController.php in OttoKit: All-in-One Automation Platform 1.1.20, at src/Controllers/GlobalSearchController.php

25,622 lines 875.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * GlobalSearchController.
4 * php version 5.6
5 *
6 * @category GlobalSearchController
7 * @package SureTriggers
8 * @author BSF <username@example.com>
9 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
10 * @link https://www.brainstormforce.com/
11 * @since 1.0.0
12 */
13
14 namespace SureTriggers\Controllers;
15
16 use DOMDocument;
17 use FluentCrm\App\Models\CustomContactField;
18 use FluentCrm\App\Models\Subscriber;
19 use FluentCrm\App\Models\Tag;
20 use 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 OsServiceModel;
36 use OsWpUserHelper;
37 use PrestoPlayer\Models\Video;
38 use RGFormsModel;
39 use SureTriggers\Integrations\AffiliateWP\AffiliateWP;
40 use SureTriggers\Integrations\EDD\EDD;
41 use SureTriggers\Integrations\FunnelKitAutomations\FunnelKitAutomations;
42 use SureTriggers\Integrations\JetpackCRM\JetpackCRM;
43 use SureTriggers\Integrations\LearnDash\LearnDash;
44 use SureTriggers\Integrations\LifterLMS\LifterLMS;
45 use SureTriggers\Integrations\MemberPress\MemberPress;
46 use SureTriggers\Integrations\MemberPressCourse\MemberPressCourse;
47 use SureTriggers\Integrations\ModernEventsCalendar\ModernEventsCalendar;
48 use SureTriggers\Integrations\PeepSo\PeepSo;
49 use SureTriggers\Integrations\RafflePress\RafflePress;
50 use SureTriggers\Integrations\RestrictContent\RestrictContent;
51 use SureTriggers\Integrations\TheEventCalendar\TheEventCalendar;
52 use SureTriggers\Integrations\WishlistMember\WishlistMember;
53 use SureTriggers\Integrations\WooCommerce\WooCommerce;
54 use SureTriggers\Integrations\WordPress\WordPress;
55 use SureTriggers\Integrations\WpPolls\WpPolls;
56 use SureTriggers\Models\Utilities;
57 use SureTriggers\Traits\SingletonLoader;
58 use Tribe__Tickets__Tickets_Handler;
59 use WC_Subscription;
60 use WC_Subscriptions_Product;
61 use WP_Query;
62 use WP_Comment_Query;
63 use WP_REST_Request;
64 use WP_REST_Response;
65 use WPForms_Form_Handler;
66 use CP_V2_Popups;
67 use Project_Huddle;
68 use PH\Models\Post;
69 use FrmForm;
70 use Forminator_API;
71 use SureTriggers\Integrations\LearnPress\LearnPress;
72 use WC_Customer;
73 use WC_Booking;
74 use WC_Bookings_Admin;
75 use MeprTransaction;
76 use WC_Order;
77 use LLMS_Section;
78 use BP_Signup;
79 use WP_Post;
80 use AsgarosForum;
81 use PeepSoUser;
82 use PeepSoField;
83 use Mint\MRM\DataBase\Models\ContactModel;
84 use Mint\MRM\DataBase\Models\ContactGroupModel;
85 use SureTriggers\Integrations\Voxel\Voxel;
86 use FluentBoards\App\Models\Stage;
87 use FluentCommunity\App\Functions\Utility;
88 use ParagonIE\Sodium\Core\Util;
89 use Surelywp_Support_Portal;
90 use SureTriggers\Integrations\ProfileGrid\ProfileGrid;
91
92 /**
93 * GlobalSearchController- Add ajax related functions here.
94 *
95 * @category GlobalSearchController
96 * @package SureTriggers
97 * @author BSF <username@example.com>
98 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
99 * @link https://www.brainstormforce.com/
100 * @since 1.0.0
101 * @method static GlobalSearchController get_instance()
102 *
103 * @psalm-suppress UndefinedTrait
104 */
105 class GlobalSearchController {
106
107 use SingletonLoader;
108
109 /**
110 * Search post by post type.
111 *
112 * @param array $data Search Params.
113 *
114 * @since 1.0.0
115 *
116 * @return array
117 */
118 public function search_post( $data ) {
119 $result = [];
120 $posts = Utilities::find_posts_by_title( $data );
121
122 foreach ( $posts['results'] as $post ) {
123 $result[] = [
124 'label' => $post['post_title'],
125 'value' => $post['ID'],
126 ];
127 }
128
129 return [
130 'options' => $result,
131 'hasMore' => $posts['has_more'],
132 ];
133 }
134
135 /**
136 * Search download.
137 *
138 * @param array $data Search Params.
139 *
140 * @since 1.0.0
141 *
142 * @return array
143 */
144 public function search_download( $data ) {
145 $result = [];
146 $posts = Utilities::find_posts_by_title( $data );
147
148 foreach ( $posts['results'] as $post ) {
149 $result[] = [
150 'label' => $post['post_title'],
151 'value' => $post['ID'],
152 ];
153
154 if ( 'download' === $post['post_type'] && function_exists( 'edd_has_variable_prices' ) && edd_has_variable_prices( $post['ID'] ) ) {
155 if ( function_exists( 'edd_get_variable_prices' ) ) {
156 $prices = edd_get_variable_prices( $post['ID'] );
157
158 foreach ( $prices as $price_id => $price ) {
159 $result[] = [
160 'label' => $post['post_title'] . ': ' . $price['name'],
161 'value' => $post['ID'] . '_' . $price_id,
162 ];
163 }
164 }
165 }
166 }
167
168 return [
169 'options' => $result,
170 'hasMore' => $posts['has_more'],
171 ];
172 }
173
174
175 /**
176 * Search Course.
177 *
178 * @param array $data quesry params.
179 *
180 * @return array
181 * @since 1.0.0
182 */
183 public function search_ld_course( $data ) {
184
185 $page = $data['page'];
186 $limit = Utilities::get_search_page_limit();
187 $offset = $limit * ( $page - 1 );
188
189 $courses = get_posts(
190 [
191
192 'post_type' => 'product',
193 'meta_key' => '_related_course',
194 'post_status' => 'publish',
195 'posts_per_page' => $limit,
196 'offset' => $offset,
197 ]
198 );
199 $options = [];
200 foreach ( $courses as $course ) {
201 $options[] = [
202 'label' => $course->post_title,
203 'value' => $course->ID,
204 ];
205 }
206
207 $all_courses = get_posts(
208 [
209 'post_type' => 'product',
210 'meta_key' => '_related_course',
211 'post_status' => 'publish',
212 'fields' => 'ids',
213 'posts_per_page' => -1,
214 ]
215 );
216 $posts_count = count( $all_courses );
217
218 return [
219 'options' => $options,
220 'hasMore' => $posts_count > $limit && $posts_count > $offset,
221 ];
222 }
223
224 /**
225 * Search achievement by post type.
226 *
227 * @param array $data Search Params.
228 *
229 * @since 1.0.0
230 *
231 * @return array
232 */
233 public function search_achievements( $data ) {
234
235 $page = $data['page'];
236 $limit = Utilities::get_search_page_limit();
237 $offset = $limit * ( $page - 1 );
238
239 $post = get_post( $data['dynamic'] );
240 if ( ! $post instanceof WP_Post ) {
241 return [];
242 }
243 $slug = $post->post_name;
244
245 $achievements = get_posts(
246 [
247 'post_type' => $slug,
248 'post_status' => 'publish',
249 'posts_per_page' => $limit,
250 'offset' => $offset,
251 ]
252 );
253 $options = [];
254 foreach ( $achievements as $achievement ) {
255 $options[] = [
256 'label' => $achievement->post_title,
257 'value' => (string) $achievement->ID,
258 ];
259 }
260
261 $count = wp_count_posts( $slug )->publish;
262
263 return [
264 'options' => $options,
265 'hasMore' => $count > $limit && $count > $offset,
266 ];
267 }
268
269 /**
270 * Search sureemails data.
271 *
272 * @param array $data data.
273 * @return array
274 */
275 public function search_pluggables_course_completed( $data ) {
276 $context = [];
277 $args = [
278 'order' => 'DESC',
279 'number' => 1,
280 'orderby' => 'ID',
281 ];
282
283 if ( isset( $data['filter']['suredash_course']['value'] ) ) {
284 $course_id = $data['filter']['suredash_course']['value'];
285 }
286
287 $users = get_users( $args );
288
289 if ( ! empty( $users ) ) {
290 $user = $users[0];
291 $course_title = 'Sample Course';
292 if ( isset( $course_id ) ) {
293 $course = get_post( $course_id );
294 if ( $course ) {
295 $course_title = $course->post_title;
296 }
297 }
298
299 $first_name = get_user_meta( $user->ID, 'first_name', true );
300 $last_name = get_user_meta( $user->ID, 'last_name', true );
301
302 $pluggable_data = [
303 'wp_user_id' => $user->ID,
304 'user_login' => $user->user_login,
305 'display_name' => $user->display_name,
306 'user_firstname' => $first_name ? $first_name : '--',
307 'user_lastname' => $last_name ? $last_name : '--',
308 'user_email' => $user->user_email,
309 'user_registered' => $user->user_registered,
310 'user_role' => $user->roles,
311 'suredash_course' => isset( $course_id ) ? $course_id : 102,
312 'course_title' => $course_title,
313 ];
314 $context['pluggable_data'] = $pluggable_data;
315 $context['response_type'] = 'live';
316 } else {
317 $context['pluggable_data'] = [
318 'wp_user_id' => 1,
319 'user_login' => 'testuser',
320 'display_name' => 'Test User',
321 'user_firstname' => '--',
322 'user_lastname' => '--',
323 'user_email' => 'testuser@gmail.com',
324 'user_registered' => '2024-06-18 09:47:58',
325 'user_role' => [ 'subscriber' ],
326 'suredash_course' => isset( $course_id ) ? $course_id : 102,
327 'course_title' => 'Sample Course',
328 ];
329 $context['response_type'] = 'sample';
330 }
331 return $context;
332 }
333
334 /**
335 * Search Course.
336 *
337 * @param array $data quesry params.
338 *
339 * @return array
340 * @since 1.0.0
341 */
342 public function search_suredash_course( $data ) {
343 if ( ! defined( 'SUREDASHBOARD_POST_TYPE' ) ) {
344 return [
345 'options' => [],
346 'hasMore' => false,
347 ];
348 }
349
350 $courses = get_posts(
351 [
352 'post_type' => SUREDASHBOARD_POST_TYPE,
353 'post_status' => 'publish',
354 'meta_query' => [
355 [
356 'key' => 'integration',
357 'value' => 'course',
358 ],
359 ],
360 'numberposts' => -1,
361 ]
362 );
363
364 $options = [];
365 foreach ( $courses as $course ) {
366 $options[] = [
367 'label' => $course->post_title,
368 'value' => $course->ID,
369 ];
370 }
371
372 return [
373 'options' => $options,
374 'hasMore' => false,
375 ];
376 }
377
378 /**
379 * Search SureDash Posts.
380 *
381 * @param array $data quesry params.
382 *
383 * @return array
384 * @since 1.0.0
385 */
386 public function search_suredash_post( $data ) {
387 if ( ! defined( 'SUREDASHBOARD_FEED_POST_TYPE' ) ) {
388 return [
389 'options' => [],
390 'hasMore' => false,
391 ];
392 }
393
394 $posts = get_posts(
395 [
396 'post_type' => SUREDASHBOARD_FEED_POST_TYPE,
397 'post_status' => 'publish',
398 'numberposts' => -1,
399 ]
400 );
401 $options = [];
402 foreach ( $posts as $post ) {
403 $options[] = [
404 'label' => $post->post_title,
405 'value' => $post->ID,
406 ];
407 }
408
409 return [
410 'options' => $options,
411 'hasMore' => false,
412 ];
413 }
414
415 /**
416 * Search SureDash Discussion Spaces.
417 *
418 * @param array $data query params.
419 *
420 * @return array
421 * @since 1.0.0
422 */
423 public function search_suredash_discussion_spaces( $data ) {
424 if ( ! defined( 'SUREDASHBOARD_POST_TYPE' ) ) {
425 return [
426 'options' => [],
427 'hasMore' => false,
428 ];
429 }
430
431 $spaces = get_posts(
432 [
433 'post_type' => SUREDASHBOARD_POST_TYPE,
434 'post_status' => 'publish',
435 'meta_query' => [
436 [
437 'key' => 'integration',
438 'value' => 'posts_discussion',
439 ],
440 ],
441 'numberposts' => -1,
442 ]
443 );
444
445 $options = [];
446 foreach ( $spaces as $space ) {
447 $options[] = [
448 'label' => $space->post_title,
449 'value' => $space->ID,
450 ];
451 }
452
453 return [
454 'options' => $options,
455 'hasMore' => false,
456 ];
457 }
458
459 /**
460 * Search SureDash Badges.
461 *
462 * @param array $data query params.
463 *
464 * @return array
465 * @since 1.0.0
466 */
467 public function search_suredash_badges( $data ) {
468 if ( ! defined( 'SUREDASHBOARD_VER' ) ) {
469 return [
470 'options' => [],
471 'hasMore' => false,
472 ];
473 }
474
475 $portal_settings = get_option( defined( 'SUREDASHBOARD_SETTINGS' ) ? SUREDASHBOARD_SETTINGS : 'portal_admin_settings', [] );
476 $all_badges = is_array( $portal_settings ) && isset( $portal_settings['user_badges'] ) && is_array( $portal_settings['user_badges'] ) ? $portal_settings['user_badges'] : [];
477
478 $options = [];
479 foreach ( $all_badges as $badge ) {
480 if ( is_array( $badge ) && isset( $badge['id'], $badge['name'] ) ) {
481 $options[] = [
482 'label' => (string) $badge['name'],
483 'value' => (string) $badge['id'],
484 ];
485 }
486 }
487
488 return [
489 'options' => $options,
490 'hasMore' => false,
491 ];
492 }
493
494 /**
495 * Search Course.
496 *
497 * @param array $data quesry params.
498 *
499 * @return array
500 * @since 1.0.0
501 */
502 public function search_tutor_course( $data ) {
503 if ( ! function_exists( 'tutor' ) ) {
504 return [];
505 }
506 $courses = get_posts(
507 [
508 'post_type' => tutor()->course_post_type,
509 'post_status' => 'publish',
510 'numberposts' => -1,
511 ]
512 );
513 $options = [];
514 foreach ( $courses as $course ) {
515 $options[] = [
516 'label' => $course->post_title,
517 'value' => $course->ID,
518 ];
519 }
520
521 return [
522 'options' => $options,
523 'hasMore' => false,
524 ];
525 }
526
527 /**
528 * Search Products.
529 *
530 * @param array $data quesry params.
531 *
532 * @return array
533 * @since 1.0.0
534 */
535 public function search_product( $data ) {
536 $result = [];
537 $posts = Utilities::find_posts_by_title( $data );
538
539 foreach ( $posts['results'] as $post ) {
540 $result[] = [
541 'label' => $post['post_title'],
542 'value' => $post['post_title'],
543 ];
544 }
545
546 return [
547 'options' => $result,
548 'hasMore' => $posts['has_more'],
549 ];
550 }
551
552 /**
553 * Search Product Categories.
554 *
555 * @param array $data Search Params.
556 *
557 * @return array
558 * @since 1.0.0
559 */
560 public function search_product_category( $data ) {
561 if ( ! empty( $data['dynamic'] ) ) {
562 $taxonomy = $data['dynamic'];
563 } else {
564 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
565 }
566
567 $term = $data['term'];
568 $result = [];
569 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
570 foreach ( $terms['result'] as $tax_term ) {
571 $result[] = [
572 'label' => $tax_term->name,
573 'value' => $tax_term->name,
574 ];
575 }
576
577 return [
578 'options' => $result,
579 'hasMore' => $terms['has_more'],
580 ];
581 }
582
583 /**
584 * Search Product Tags.
585 *
586 * @param array $data Search Params.
587 *
588 * @return array
589 * @since 1.0.0
590 */
591 public function search_product_tags( $data ) {
592 if ( ! empty( $data['dynamic'] ) ) {
593 $taxonomy = $data['dynamic'];
594 } else {
595 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
596 }
597
598 $term = $data['term'];
599 $result = [];
600 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
601
602 foreach ( $terms['result'] as $tax_term ) {
603 $result[] = [
604 'label' => $tax_term->name,
605 'value' => $tax_term->name,
606 ];
607 }
608
609 return [
610 'options' => $result,
611 'hasMore' => $terms['has_more'],
612 ];
613 }
614
615 /**
616 * Global ajax search.
617 * Here you need to add the field action name to work.
618 *
619 * @param WP_REST_Request $request Request data.
620 *
621 * @return WP_REST_Response|object
622 * @since 1.0.0
623 */
624 public function global_search( $request ) {
625 $post_type = $request->get_param( 'post_type' );
626 $dynamic = $request->get_param( 'dynamic' );
627 $search_term = $request->get_param( 'term' );
628 $identifier = $request->get_param( 'field_identifier' );
629 $page = max( 1, $request->get_param( 'page' ) );
630 $taxonomy = $request->get_param( 'taxonomy' ) ? $request->get_param( 'taxonomy' ) : [];
631
632 $filter = $request->get_param( 'filter' ) ? json_decode( stripslashes( $request->get_param( 'filter' ) ), true ) : [];
633
634 $data = [
635 'dynamic' => $dynamic,
636 'search_term' => $search_term,
637 'page' => $page,
638 'taxonomy' => $taxonomy,
639 'filter' => $filter,
640 'post_type' => $post_type,
641 ];
642 $response = [
643 'hasMore' => false,
644 'options' => [],
645 ];
646
647 $method_name = 'search_' . $identifier;
648
649 if ( method_exists( $this, $method_name ) ) {
650 $response = $this->{$method_name}( $data );
651 } else {
652 return RestController::error_message( 'Invalid field Identifier param.' );
653 }
654
655 return RestController::success_message( $response );
656 }
657
658 /**
659 * Search pluggables post liked data.
660 *
661 * @param array $data data.
662 * @return array
663 */
664 public function search_pluggables_post_liked( $data ) {
665 $context = [];
666 $args = [
667 'order' => 'DESC',
668 'number' => 1,
669 'orderby' => 'ID',
670 ];
671
672 if ( isset( $data['filter']['suredash_post']['value'] ) ) {
673 $post_id = $data['filter']['suredash_post']['value'];
674 }
675
676 $users = get_users( $args );
677
678 if ( ! empty( $users ) ) {
679 $user = $users[0];
680 $post_title = 'Sample Post';
681 if ( isset( $post_id ) ) {
682 $post = get_post( $post_id );
683 if ( $post ) {
684 $post_title = $post->post_title;
685 }
686 }
687
688 $pluggable_data = [
689 'wp_user_id' => $user->ID,
690 'user_login' => $user->user_login,
691 'display_name' => $user->display_name,
692 'user_firstname' => get_user_meta( $user->ID, 'first_name', true ) ? get_user_meta( $user->ID, 'first_name', true ) : '--',
693 'user_lastname' => get_user_meta( $user->ID, 'last_name', true ) ? get_user_meta( $user->ID, 'last_name', true ) : '--',
694 'user_email' => $user->user_email,
695 'user_registered' => $user->user_registered,
696 'user_role' => $user->roles,
697 'post_id' => isset( $post_id ) ? $post_id : 123,
698 'post_title' => $post_title,
699 'post_author' => isset( $post_id ) ? get_post_field( 'post_author', $post_id ) : 1,
700 ];
701 $context['pluggable_data'] = $pluggable_data;
702 $context['response_type'] = 'live';
703 } else {
704 $context['pluggable_data'] = [
705 'wp_user_id' => 1,
706 'user_login' => 'testuser',
707 'display_name' => 'Test User',
708 'user_firstname' => '--',
709 'user_lastname' => '--',
710 'user_email' => 'testuser@gmail.com',
711 'user_registered' => '2024-06-18 09:47:58',
712 'user_role' => [ 'subscriber' ],
713 'post_id' => isset( $post_id ) ? $post_id : 123,
714 'post_title' => 'Sample Post',
715 'post_author' => 1,
716 ];
717 $context['response_type'] = 'sample';
718 }
719 return $context;
720 }
721
722
723 /**
724 * Search Taxonomy Terms.
725 *
726 * @param array $data Search Params.
727 *
728 * @since 1.0.0
729 *
730 * @return array
731 */
732 public function search_term( $data ) {
733 if ( ! empty( $data['dynamic'] ) ) {
734 $taxonomy = $data['dynamic'];
735 } else {
736 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
737 }
738
739 $term = $data['term'];
740 $result = [];
741 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
742 foreach ( $terms['result'] as $tax_term ) {
743 $result[] = [
744 'label' => $tax_term->name,
745 'value' => $tax_term->term_id,
746 ];
747 }
748
749 return [
750 'options' => $result,
751 'hasMore' => $terms['has_more'],
752 ];
753 }
754
755 /**
756 * Search sureemails data.
757 *
758 * @param array $data data.
759 * @return array
760 */
761 public function search_pluggables_sureemails_mail( $data ) {
762 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
763 $context = [];
764
765 if ( 'mail_sent' === $term ) {
766 $context['pluggable_data'] = [
767 'to' => [ 'johnDoe@xyz.com' ],
768 'subject' => 'Test Email',
769 'message' => 'This is a test email',
770 'headers' => 'From: johnDoe@xyz.com,X-Mailer: PHP/8.1.22,Content-Type: text/html; charset=utf-8,Reply-To: johnDoe@xyz.com,Cc:johnDoe@xyz.com,Bcc:johnDoe@xyz.com',
771 'attachments' => [ 'Sample Attachment' ],
772 ];
773 $context['response_type'] = 'sample';
774 } elseif ( 'mail_failed' === $term ) {
775 $context['pluggable_data'] = [
776 'errors' => [
777 'wp_mail_failed' => [
778 'We were unable to send the email. Please ensure the recipient email address and sender configuration are correct. If the issue persists, contact support.',
779 ],
780 ],
781 'error_data' => [
782 'wp_mail_failed' => [
783 'to' => 'johnDoe@xyz.com',
784 'subject' => 'Test Email',
785 'message' => 'This is a test email.',
786 'headers' => 'From: johnDoe@xyz.com,X-Mailer: PHP/8.1.22,Content-Type: text/html; charset=utf-8,Reply-To: johnDoe@xyz.com,Cc:johnDoe@xyz.com,Bcc:johnDoe@xyz.com',
787 'attachments' => [ 'Sample Attachment' ],
788 ],
789 ],
790 ];
791 $context['response_type'] = 'sample';
792 } elseif ( 'mail_blocked' === $term ) {
793 $context['pluggable_data'] = [
794 'to' => 'johnDoe@xyz.com',
795 'subject' => 'Test Email',
796 'message' => 'This is a test email.',
797 'headers' => 'From: johnDoe@xyz.com
798 X-Mailer: PHP/8.1.22
799 Content-Type: text/html; charset=utf-8
800 Reply-To: johnDoe@xyz.com
801 Cc:johnDoe@xyz.com Bcc:johnDoe@xyz.com',
802 'attachments' => [ 'Sample Attachment' ],
803 'categories' => [
804 'harassment' => 1,
805 'harassment/threatening' => 1,
806 'sexual' => 0,
807 'hate' => 0,
808 'hate/threatening' => 0,
809 'illicit' => 0,
810 'illicit/violent' => 0,
811 'self-harm/intent' => 0,
812 'self-harm/instructions' => 0,
813 'self-harm' => 0,
814 'sexual/minors' => 0,
815 'violence' => 1,
816 'violence/graphic' => 0,
817 ],
818 ];
819 $context['response_type'] = 'sample';
820 }
821
822 return $context;
823 }
824
825
826
827 /**
828 * List Taxonomy Terms.
829 *
830 * @param array $data Search Params.
831 *
832 * @since 1.0.0
833 *
834 * @return array
835 */
836 public function search_term_list( $data ) {
837 $taxonomy = $data['dynamic'];
838 $result = [];
839 $terms = Utilities::get_terms( '', $data['page'], $taxonomy );
840 foreach ( $terms['result'] as $tax_term ) {
841 $result[] = [
842 'label' => $tax_term->name,
843 'value' => $tax_term->term_id,
844 ];
845 }
846
847 return [
848 'options' => $result,
849 'hasMore' => $terms['has_more'],
850 ];
851 }
852
853 /**
854 * List All Taxonomy Terms.
855 *
856 * @param array $data Search Params.
857 *
858 * @since 1.0.0
859 *
860 * @return array
861 */
862 public function search_term_list_for_all_taxonomy( $data ) {
863 global $wpdb;
864
865 $result = [];
866
867 $all_taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
868 $taxonomy_names = array_keys( $all_taxonomies );
869
870 if ( empty( $taxonomy_names ) ) {
871 return [
872 'options' => [],
873 'hasMore' => false,
874 ];
875 }
876
877 $all_terms = get_terms(
878 [
879 'taxonomy' => $taxonomy_names,
880 'hide_empty' => false,
881 'number' => 0,
882 'orderby' => 'name',
883 'order' => 'ASC',
884 ]
885 );
886
887 $terms = [];
888 if ( ! is_wp_error( $all_terms ) && ! empty( $all_terms ) ) {
889 foreach ( $all_terms as $term ) {
890 $terms[] = (object) [
891 'term_id' => $term->term_id,
892 'name' => $term->name,
893 'taxonomy' => $term->taxonomy,
894 ];
895 }
896 }
897
898 if ( ! empty( $terms ) ) {
899 foreach ( $terms as $term ) {
900 $taxonomy_obj = isset( $all_taxonomies[ $term->taxonomy ] ) ? $all_taxonomies[ $term->taxonomy ] : null;
901 $taxonomy_label = $taxonomy_obj ? $taxonomy_obj->label : $term->taxonomy;
902
903 $result[] = [
904 'label' => $term->name . ' - ' . ucwords( $taxonomy_label ),
905 'value' => $term->term_id . '%-%' . $term->taxonomy,
906 ];
907 }
908 }
909
910 return [
911 'options' => $result,
912 'hasMore' => false,
913 ];
914 }
915
916
917 /**
918 * List Category Terms.
919 *
920 * @param array $data Search Params.
921 *
922 * @since 1.0.0
923 *
924 * @return array
925 */
926 public function search_category_term_list( $data ) {
927 $result = [];
928 $taxonomy = $data['dynamic'];
929 $terms = Utilities::get_terms( '', $data['page'], $taxonomy );
930 foreach ( $terms['result'] as $tax_term ) {
931 if ( 0 == $tax_term->parent ) {
932 $result[] = [
933 'label' => $tax_term->name,
934 'value' => $tax_term->term_id,
935 ];
936 foreach ( $terms['result'] as $child ) {
937 if ( $child->parent == $tax_term->term_id ) {
938 $result[] = [
939 'label' => ' - ' . $child->name,
940 'value' => $child->term_id,
941 ];
942 }
943 }
944 }
945 }
946
947 return [
948 'options' => $result,
949 'hasMore' => $terms['has_more'],
950 ];
951 }
952
953 /**
954 * List Taxonomy Tags.
955 *
956 * @param array $data Search Params.
957 *
958 * @since 1.0.0
959 *
960 * @return array
961 */
962 public function search_term_list_tags( $data ) {
963 $result = [];
964 $terms = Utilities::get_terms( '', $data['page'], [ 'post_tag' ] );
965 foreach ( $terms['result'] as $tax_term ) {
966 $result[] = [
967 'label' => $tax_term->name,
968 'value' => $tax_term->term_id,
969 ];
970 }
971
972 return [
973 'options' => $result,
974 'hasMore' => $terms['has_more'],
975 ];
976 }
977
978 /**
979 * List Taxonomy Product Tags.
980 *
981 * @param array $data Search Params.
982 *
983 * @since 1.0.0
984 *
985 * @return array
986 */
987 public function search_term_list_prod_tags( $data ) {
988 $result = [];
989 $terms = Utilities::get_terms( '', $data['page'], [ 'product_tag' ] );
990 foreach ( $terms['result'] as $tax_term ) {
991 $result[] = [
992 'label' => $tax_term->name,
993 'value' => $tax_term->term_id,
994 ];
995 }
996
997 return [
998 'options' => $result,
999 'hasMore' => $terms['has_more'],
1000 ];
1001 }
1002
1003 /**
1004 * List Role Capabilities.
1005 *
1006 * @param array $data Search Params.
1007 *
1008 * @since 1.0.0
1009 *
1010 * @return array
1011 */
1012 public function search_role_capabilities( $data ) {
1013 $user_roles = wp_roles();
1014 $result = [];
1015 $capabilities = [];
1016
1017 foreach ( $user_roles->roles as $name => $role ) {
1018 foreach ( $role['capabilities'] as $key => $cap ) {
1019 if ( ! in_array( $key, $capabilities ) ) {
1020 $capabilities[] = $key;
1021 $result[] = [
1022 'label' => $key,
1023 'value' => esc_attr( $key ),
1024 ];
1025 }
1026 }
1027 }
1028
1029 return [
1030 'options' => $result,
1031 'hasMore' => false,
1032 ];
1033 }
1034
1035 /**
1036 * Search users.
1037 *
1038 * @param array $data Search Params.
1039 *
1040 * @since 1.0.0
1041 *
1042 * @return array
1043 */
1044 public function search_user( $data ) {
1045 $result = [];
1046 $page = $data['page'];
1047 $users = Utilities::get_users( $data, $page );
1048
1049 if ( is_array( $users['results'] ) ) {
1050 foreach ( $users['results'] as $user ) {
1051 $result[] = [
1052 'label' => $user->user_login,
1053 'value' => $user->ID,
1054 ];
1055 }
1056 }
1057
1058 return [
1059 'options' => $result,
1060 'hasMore' => $users['has_more'],
1061 ];
1062
1063 }
1064
1065 /**
1066 * Search WPForm fields.
1067 *
1068 * @param array $data Search Params.
1069 *
1070 * @since 1.0.0
1071 *
1072 * @return array
1073 */
1074 public function search_pluggable_wpform_fields( $data ) {
1075 $result = [];
1076 $page = $data['page'];
1077 $form_id = absint( $data['dynamic'] );
1078 $wpform_fields = Utilities::get_wpform_fields( $data['search_term'], $page, $form_id );
1079
1080 if ( $wpform_fields && is_array( $wpform_fields['results'] ) ) {
1081 foreach ( $wpform_fields['results'] as $field ) {
1082 $result[] = [
1083 'label' => $field['label'],
1084 'value' => '{' . $field['id'] . '}',
1085 ];
1086 }
1087 }
1088
1089 return [
1090 'options' => $result,
1091 'hasMore' => ( $wpform_fields && $wpform_fields['has_more'] ) ? $wpform_fields['has_more'] : false,
1092 ];
1093 }
1094
1095 /**
1096 * Prepare variable products.
1097 *
1098 * @param array $data data.
1099 *
1100 * @return array
1101 */
1102 public function search_variable_products( $data ) {
1103 if ( ! function_exists( 'wc_get_products' ) ) {
1104 return [
1105 'options' => [],
1106 'hasMore' => false,
1107 ];
1108 }
1109
1110 $products = wc_get_products(
1111 [
1112 'type' => 'variable',
1113 'status' => 'publish',
1114 'limit' => -1,
1115 ]
1116 );
1117
1118 $options = [];
1119 if ( is_array( $products ) ) {
1120 foreach ( $products as $product ) {
1121 $options[] = [
1122 'label' => $product->get_name(),
1123 'value' => (string) $product->get_id(),
1124 ];
1125 }
1126 }
1127
1128 return [
1129 'options' => $options,
1130 'hasMore' => false,
1131 ];
1132 }
1133
1134 /**
1135 * Prepare variable products.
1136 *
1137 * @param array $data data.
1138 *
1139 * @return array
1140 */
1141 public function search_product_variations( $data ) {
1142 $variations = Utilities::get_product_variations( $data['dynamic'] );
1143 $options = [];
1144
1145 foreach ( $variations['result'] as $product ) {
1146 $options[] = [
1147 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
1148 'value' => (string) $product->ID,
1149 ];
1150 }
1151
1152 return [
1153 'options' => $options,
1154 'hasMore' => $variations['has_more'],
1155 ];
1156 }
1157
1158 /**
1159 * Search WooCommerce Subscriptions.
1160 *
1161 * @param array $data Search Params.
1162 *
1163 * @since 1.0.0
1164 *
1165 * @return array
1166 */
1167 public function search_subscription_variation( $data ) {
1168 $subscription_products = Utilities::get_subscription_variation( $data['search_term'], $data['page'] );
1169 $result = [];
1170
1171 if ( ! function_exists( 'wc_get_products' ) ) {
1172 return $result;
1173 }
1174
1175 foreach ( $subscription_products['result'] as $post ) {
1176 if ( $data['search_term'] ) {
1177 if ( false !== stripos( $post->get_title(), $data['search_term'] ) ) {
1178 $result[] = [
1179 'label' => $post->get_title(),
1180 'value' => (string) $post->get_id(),
1181 ];
1182 }
1183 } else {
1184 $result[] = [
1185 'label' => $post->get_title(),
1186 'value' => (string) $post->get_id(),
1187 ];
1188 }
1189 }
1190
1191 return [
1192 'options' => $result,
1193 'hasMore' => $subscription_products['has_more'],
1194 ];
1195 }
1196
1197 /**
1198 * Prepare WooCommerce Payment Methods.
1199 *
1200 * @param array $data Search Params.
1201 * @return array
1202 */
1203 public function search_woo_payment_methods( $data ) {
1204 $payment_methods = WC()->payment_gateways()->get_available_payment_gateways();
1205 $options = [];
1206
1207 if ( ! empty( $payment_methods ) ) {
1208 foreach ( $payment_methods as $payment ) {
1209 $options[] = [
1210 'label' => $payment->title,
1211 'value' => $payment->id,
1212 ];
1213 }
1214 }
1215
1216 return [
1217 'options' => $options,
1218 'hasMore' => false,
1219 ];
1220 }
1221
1222 /**
1223 * Prepare WooCommerce Order Status List.
1224 *
1225 * @param array $data Search Params.
1226 * @return array
1227 */
1228 public function search_woo_order_status_list( $data ) {
1229 $order_status = wc_get_order_statuses();
1230 $options = [];
1231
1232 if ( ! empty( $order_status ) ) {
1233 foreach ( $order_status as $key => $status ) {
1234 $options[] = [
1235 'label' => $status,
1236 'value' => $key,
1237 ];
1238 }
1239 }
1240
1241 return [
1242 'options' => $options,
1243 'hasMore' => false,
1244 ];
1245 }
1246
1247 /**
1248 * Prepare WooCommerce Country List.
1249 *
1250 * @param array $data Search Params.
1251 * @return array
1252 */
1253 public function search_woo_country_list( $data ) {
1254 $countries = WC()->countries->get_countries();
1255 $options = [];
1256
1257 if ( ! empty( $countries ) ) {
1258 foreach ( $countries as $key => $country ) {
1259 $options[] = [
1260 'label' => $country,
1261 'value' => $key,
1262 ];
1263 }
1264 }
1265
1266 return [
1267 'options' => $options,
1268 'hasMore' => false,
1269 ];
1270 }
1271
1272 /**
1273 * Prepare WooCommerce Country States List.
1274 *
1275 * @param array $data Search Params.
1276 * @return array
1277 */
1278 public function search_woo_country_state_list( $data ) {
1279 if ( ! empty( $data['dynamic']['shipping_country'] ) ) {
1280 $cc = $data['dynamic']['shipping_country'];
1281 } else {
1282 $cc = $data['dynamic'];
1283 }
1284
1285 $states = WC()->countries->get_states( $cc );
1286
1287 $options = [];
1288 if ( ! empty( $states ) ) {
1289 foreach ( $states as $key => $state ) {
1290 $options[] = [
1291 'label' => $state,
1292 'value' => $key,
1293 ];
1294 }
1295 }
1296
1297 return [
1298 'options' => $options,
1299 'hasMore' => false,
1300 ];
1301 }
1302
1303 /**
1304 * Get Memberpress gatways (payment methods) for subscription.
1305 *
1306 * @param array $data QueryParams.
1307 *
1308 * @return array
1309 * @since 1.0.0
1310 */
1311 public function search_memberpress_gayways( $data ) {
1312 if ( ! class_exists( 'MeprOptions' ) || ! class_exists( 'MeprBaseRealGateway' ) ) {
1313 return [];
1314 }
1315 $mp_options = MeprOptions::fetch();
1316
1317 $pms = array_keys( $mp_options->integrations );
1318 $gateways = [];
1319
1320 foreach ( $pms as $pm_id ) {
1321 $obj = $mp_options->payment_method( $pm_id );
1322 if ( $obj instanceof MeprBaseRealGateway ) {
1323 $gateways[] = [
1324 'label' => sprintf( '%1$s (%2$s)', $obj->label, $obj->name ),
1325 'value' => $obj->id,
1326 ];
1327 }
1328 }
1329
1330 return [
1331 'options' => $gateways,
1332 'hasMore' => false,
1333 ];
1334 }
1335
1336 /**
1337 * Prepare roles.
1338 *
1339 * @param array $data data.
1340 *
1341 * @return array
1342 */
1343 public function search_roles( $data ) {
1344 $roles = wp_roles()->roles;
1345 $options = [];
1346 foreach ( $roles as $role => $details ) {
1347
1348 $options[] = [
1349 'label' => $details['name'],
1350 'value' => $role,
1351 ];
1352
1353 }
1354
1355 return [
1356 'options' => $options,
1357 'hasMore' => false,
1358 ];
1359 }
1360
1361 /**
1362 * Prepare post types.
1363 *
1364 * @param array $data data.
1365 *
1366 * @return array
1367 */
1368 public function search_post_types( $data ) {
1369 $post_types = get_post_types( [ 'public' => true ], 'objects' );
1370 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
1371 if ( isset( $post_types['attachment'] ) ) {
1372 unset( $post_types['attachment'] );
1373 }
1374
1375 $options = [];
1376 foreach ( $post_types as $post_type => $details ) {
1377 $options[] = [
1378 'label' => $details->label,
1379 'value' => $post_type,
1380 ];
1381 }
1382
1383 return [
1384 'options' => $options,
1385 'hasMore' => false,
1386 ];
1387 }
1388
1389 /**
1390 * Get post statuses.
1391 *
1392 * @param array $data data.
1393 *
1394 * @return array
1395 */
1396 public function search_post_statuses( $data ) {
1397 $post_statuses = get_post_stati( [], 'objects' );
1398 $post_statuses = apply_filters( 'suretriggers_post_types', $post_statuses );
1399 $options = [];
1400
1401 foreach ( $post_statuses as $post_status => $details ) {
1402 if ( 'woocommerce' === $details->label_count['domain'] ) {
1403 $options[] = [
1404 'label' => 'WooCommerce - ' . $details->label,
1405 'value' => $post_status,
1406 ];
1407 } else {
1408 $options[] = [
1409 'label' => $details->label,
1410 'value' => $post_status,
1411 ];
1412 }
1413 }
1414 return [
1415 'options' => $options,
1416 'hasMore' => false,
1417 ];
1418 }
1419
1420 /**
1421 * Get Taxonomies.
1422 *
1423 * @param array $data data.
1424 *
1425 * @return array
1426 */
1427 public function search_taxonomy_list( $data ) {
1428 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
1429 $options = [];
1430 $options[0] = [
1431 'label' => 'Any Taxonomy',
1432 'value' => -1,
1433 ];
1434
1435 foreach ( $taxonomies as $taxonomy => $taxonomy_obj ) {
1436 $options[] = [
1437 'label' => $taxonomy_obj->label,
1438 'value' => $taxonomy_obj->name,
1439 ];
1440 }
1441
1442 return [
1443 'options' => $options,
1444 'hasMore' => false,
1445 ];
1446 }
1447
1448 /**
1449 * Get Taxonomies without any option.
1450 *
1451 * @param array $data data.
1452 *
1453 * @return array
1454 */
1455 public function search_taxonomy_list_without_any( $data ) {
1456 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
1457 $options = [];
1458 foreach ( $taxonomies as $taxonomy => $taxonomy_obj ) {
1459 $options[] = [
1460 'label' => $taxonomy_obj->label,
1461 'value' => $taxonomy_obj->name,
1462 ];
1463 }
1464
1465 return [
1466 'options' => $options,
1467 'hasMore' => false,
1468 ];
1469 }
1470
1471 /**
1472 * Get WPForms.
1473 *
1474 * @param array $data data.
1475 *
1476 * @return array
1477 */
1478 public function search_wp_forms( $data ) {
1479 if ( ! class_exists( 'WPForms_Form_Handler' ) ) {
1480 return [];
1481 }
1482
1483 $wpforms = new WPForms_Form_Handler();
1484 $forms = $wpforms->get( '', [ 'orderby' => 'title' ] );
1485 $options = [];
1486
1487 if ( ! empty( $forms ) ) {
1488 foreach ( $forms as $form ) {
1489 $options[] = [
1490 'label' => $form->post_title,
1491 'value' => $form->ID,
1492 ];
1493 }
1494 }
1495
1496 return [
1497 'options' => $options,
1498 'hasMore' => false,
1499 ];
1500 }
1501
1502 /**
1503 * Get Gravity Forms.
1504 *
1505 * @param array $data data.
1506 *
1507 * @return array
1508 */
1509 public function search_gravity_forms( $data ) {
1510 if ( ! class_exists( 'GFFormsModel' ) ) {
1511 return [];
1512 }
1513
1514 $forms = GFFormsModel::get_forms();
1515 $options = [];
1516
1517 if ( ! empty( $forms ) ) {
1518 foreach ( $forms as $form ) {
1519 $options[] = [
1520 'label' => $form->title,
1521 'value' => $form->id,
1522 ];
1523 }
1524 }
1525
1526 return [
1527 'options' => $options,
1528 'hasMore' => false,
1529 ];
1530 }
1531
1532 /**
1533 * Get tag & contact details.
1534 *
1535 * @param array $data data.
1536 *
1537 * @return array
1538 */
1539 public function search_pluggables_fluentcrm_contact_added_to_tags( $data ) {
1540 $context = [];
1541 $pluggable_data = [];
1542 $tag_id = $data['filter'];
1543
1544 if ( ! class_exists( 'FluentCrm\App\Models\Subscriber' ) || ! class_exists( 'FluentCrm\App\Models\Tag' ) ) {
1545 return [];
1546 }
1547
1548 if ( $tag_id > 0 ) {
1549 $tags = Tag::where( 'id', $tag_id )->first();
1550 } else {
1551 $tags = Tag::orderBy( 'id', 'DESC' )->first();
1552 }
1553 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1554 if ( $contact ) {
1555 $pluggable_data['contact'] = $contact;
1556 $context['tag_id'] = $tag_id;
1557 $pluggable_data['tag'] = $tags;
1558 $context['response_type'] = 'live';
1559 } else {
1560 $pluggable_data['conatct']['full_name'] = 'Test User';
1561 $pluggable_data['conatct']['first_name'] = 'Test';
1562 $pluggable_data['conatct']['last_name'] = 'User';
1563 $pluggable_data['conatct']['company_id'] = 112;
1564 $pluggable_data['conatct']['email'] = 'testuser@gmail.com';
1565 $pluggable_data['conatct']['address_line_1'] = '33, Vincent Road';
1566 $pluggable_data['conatct']['address_line_2'] = 'Chicago Street';
1567 $pluggable_data['conatct']['postal_code'] = '212342';
1568 $pluggable_data['conatct']['city'] = 'New York City';
1569 $pluggable_data['conatct']['state'] = 'New York';
1570 $pluggable_data['conatct']['country'] = 'USA';
1571 $pluggable_data['conatct']['phone'] = '9992191911';
1572 $pluggable_data['conatct']['status'] = 'subscribed';
1573 $pluggable_data['conatct']['contact_type'] = 'lead';
1574 $pluggable_data['conatct']['source'] = '';
1575 $pluggable_data['conatct']['date_of_birth'] = '2022-11-09';
1576 $context['tag_id'] = 1;
1577 $pluggable_data['tag'] =
1578 [
1579 'id' => '1',
1580 'title' => 'new',
1581 'slug' => 'new',
1582 'description' => null,
1583 'created_at' => '2023-01-19 10:23:23',
1584 'updated_at' => '2023-01-19 10:23:23',
1585 'pivot' => [
1586 'subscriber_id' => '1',
1587 'object_id' => '1',
1588 'object_type' => 'FluentCrm\\App\\Models\\Tag',
1589 'created_at' => '2023-01-19 10:42:55',
1590 'updated_at' => '2023-01-19 10:42:55',
1591
1592 ],
1593 ];
1594 $context['response_type'] = 'sample';
1595 }
1596
1597 $context['pluggable_data'] = $pluggable_data;
1598 return $context;
1599 }
1600
1601 /**
1602 * Get FluentCRM contact details.
1603 *
1604 * @param array $data data.
1605 *
1606 * @return array
1607 */
1608 public function search_pluggables_fluentcrm_contact_added( $data ) {
1609 $context = [];
1610 $pluggable_data = [];
1611
1612 if ( ! class_exists( 'FluentCrm\App\Models\Subscriber' ) || ! class_exists( 'FluentCrm\App\Models\SubscriberMeta' ) ) {
1613 return [];
1614 }
1615 $contact = [];
1616 if ( 'status_set_to_specific_status' === $data['search_term'] ) {
1617 if ( '-1' === $data['filter']['status']['value'] ) {
1618 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1619 } else {
1620 $contact = Subscriber::where( 'status', $data['filter']['status']['value'] )->first();
1621 }
1622 } elseif ( 'new_contact_added' === $data['search_term'] ) {
1623 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1624 } elseif ( 'contact_updated' === $data['search_term'] ) {
1625 $contact = Subscriber::orderBy( 'updated_at', 'DESC' )->first();
1626 } elseif ( 'contact_field_updated' === $data['search_term'] ) {
1627 if ( '-1' === $data['filter']['field_id']['value'] ) {
1628 $contact = SubscriberMeta::where( 'object_type', 'custom_field' )->orderBy( 'updated_at', 'DESC' )->first();
1629 } else {
1630 $contact = SubscriberMeta::where( 'key', $data['filter']['field_id']['value'] )->orderBy( 'updated_at', 'DESC' )->first();
1631 }
1632 $contact = Subscriber::where( 'id', $contact->subscriber_id )->first();
1633 }
1634
1635 if ( $contact ) {
1636 $subscriber = Subscriber::with( [ 'tags', 'lists' ] )->find( $contact->id );
1637 $customer_fields = $subscriber->custom_fields();
1638 $pluggable_data['contact']['details'] = $subscriber;
1639 $pluggable_data['contact']['custom'] = $customer_fields;
1640 $pluggable_data['field_id'] = $data['filter']['field_id']['value'];
1641 $context['response_type'] = 'live';
1642 } else {
1643 $pluggable_data['contact']['details']['full_name'] = 'Test User';
1644 $pluggable_data['contact']['details']['first_name'] = 'Test';
1645 $pluggable_data['contact']['details']['last_name'] = 'User';
1646 $pluggable_data['contact']['details']['company_id'] = 112;
1647 $pluggable_data['contact']['details']['email'] = 'testuser@gmail.com';
1648 $pluggable_data['contact']['details']['address_line_1'] = '33, Vincent Road';
1649 $pluggable_data['contact']['details']['address_line_2'] = 'Chicago Street';
1650 $pluggable_data['contact']['details']['postal_code'] = '212342';
1651 $pluggable_data['contact']['details']['city'] = 'New York City';
1652 $pluggable_data['contact']['details']['state'] = 'New York';
1653 $pluggable_data['contact']['details']['country'] = 'USA';
1654 $pluggable_data['contact']['details']['phone'] = '9992191911';
1655 $pluggable_data['contact']['details']['status'] = 'subscribed';
1656 $pluggable_data['contact']['details']['contact_type'] = 'lead';
1657 $pluggable_data['contact']['details']['source'] = '';
1658 $pluggable_data['contact']['details']['date_of_birth'] = '2022-11-09';
1659 $context['response_type'] = 'sample';
1660 }
1661
1662 $context['pluggable_data'] = $pluggable_data;
1663 return $context;
1664 }
1665
1666 /**
1667 * Get contact added to list details.
1668 *
1669 * @param array $data data.
1670 *
1671 * @return array
1672 */
1673 public function search_pluggables_fluentcrm_contact_added_to_lists( $data ) {
1674 $context = [];
1675 $pluggable_data = [];
1676 $list_id = $data['filter'];
1677
1678 if ( ! class_exists( 'FluentCrm\App\Models\Lists' ) || ! class_exists( 'FluentCrm\App\Models\Subscriber' ) || ! function_exists( 'FluentCrmApi' ) ) {
1679 return [];
1680 }
1681
1682 $contact_api = FluentCrmApi( 'contacts' );
1683 if ( -1 === $list_id ) {
1684 $lists = Lists::orderBy( 'id', 'DESC' )->first();
1685 $list_id = $lists->id;
1686 } else {
1687 $lists = Lists::where( 'id', $list_id )->first();
1688 }
1689 $list_ids = [ $list_id ];
1690 $list_contacts = $contact_api->getInstance()
1691 ->filterByLists( $list_ids )
1692 ->orderBy( 'id', 'DESC' )
1693 ->first();
1694
1695 $contact = Subscriber::where( 'id', $list_contacts->id )->get();
1696 if ( $contact ) {
1697 $pluggable_data['contact'] = $contact[0];
1698 $pluggable_data['list_id'] = $list_id;
1699 $pluggable_data['list'] = $lists;
1700 $context['response_type'] = 'live';
1701 } else {
1702 $pluggable_data['conatct']['id'] = 6;
1703 $pluggable_data['conatct']['prefix'] = 'Mr';
1704 $pluggable_data['conatct']['full_name'] = 'John Doe';
1705 $pluggable_data['conatct']['first_name'] = 'John';
1706 $pluggable_data['conatct']['last_name'] = 'Doe';
1707 $pluggable_data['conatct']['company_id'] = 112;
1708 $pluggable_data['conatct']['email'] = 'johnde@gmail.com';
1709 $pluggable_data['conatct']['address_line_1'] = '33, Vincent Road';
1710 $pluggable_data['conatct']['address_line_2'] = 'Chicago Street';
1711 $pluggable_data['conatct']['postal_code'] = '212342';
1712 $pluggable_data['conatct']['city'] = 'New York City';
1713 $pluggable_data['conatct']['state'] = 'New York';
1714 $pluggable_data['conatct']['country'] = 'USA';
1715 $pluggable_data['conatct']['phone'] = '9992191911';
1716 $pluggable_data['conatct']['status'] = 'subscribed';
1717 $pluggable_data['conatct']['contact_type'] = 'lead';
1718 $pluggable_data['conatct']['source'] = '';
1719 $pluggable_data['conatct']['date_of_birth'] = '2022-11-09';
1720 $context['list_id'] = 1;
1721 $pluggable_data['list'] =
1722 [
1723 'id' => '1',
1724 'title' => 'new',
1725 'slug' => 'new',
1726 'description' => null,
1727 'created_at' => '2023-01-19 10:23:23',
1728 'updated_at' => '2023-01-19 10:23:23',
1729 ];
1730 $context['response_type'] = 'sample';
1731 }
1732
1733 $context['pluggable_data'] = $pluggable_data;
1734 return $context;
1735 }
1736
1737 /**
1738 * Prepare fluentcrm campaigns.
1739 *
1740 * @param array $data Search Params.
1741 *
1742 * @return array
1743 */
1744 public function search_fluentcrm_campaigns( $data ) {
1745
1746 $options = [];
1747 global $wpdb;
1748
1749 $campaigns = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fc_campaigns ORDER BY id DESC", ARRAY_A );
1750
1751 if ( ! empty( $campaigns ) ) {
1752 foreach ( $campaigns as $campaign ) {
1753 $options[] = [
1754 'label' => $campaign['title'],
1755 'value' => $campaign['id'],
1756 ];
1757 }
1758 }
1759
1760 return [
1761 'options' => $options,
1762 'hasMore' => false,
1763 ];
1764 }
1765
1766 /**
1767 * Get Divi Forms.
1768 *
1769 * @param array $data data.
1770 *
1771 * @return array
1772 */
1773 public function search_divi_forms( $data ) {
1774 $form_posts = Utilities::get_divi_forms();
1775 $options = [];
1776
1777 if ( empty( $form_posts ) ) {
1778 return [
1779 'options' => [],
1780 'hasMore' => false,
1781 ];
1782 }
1783
1784 foreach ( (array) $form_posts as $form_post ) {
1785 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
1786 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
1787 if ( empty( $forms ) ) {
1788 continue;
1789 }
1790
1791 $count = 0;
1792
1793 foreach ( $forms as $form ) {
1794 $pattern_form = get_shortcode_regex( [ 'et_pb_contact_form' ] );
1795 preg_match_all( "/$pattern_form/", $form[0], $forms_extracted, PREG_SET_ORDER );
1796
1797 if ( empty( $forms_extracted ) ) {
1798 continue;
1799 }
1800
1801 foreach ( $forms_extracted as $form_extracted ) {
1802 $form_attrs = shortcode_parse_atts( $form_extracted[3] );
1803 $form_id = isset( $form_attrs['_unique_id'] ) ? $form_attrs['_unique_id'] : '';
1804 if ( empty( $form_id ) ) {
1805 continue;
1806 }
1807 $form_id = sprintf( '%d-%s', $form_post['ID'], $form_id );
1808 $form_title = isset( $form_attrs['title'] ) ? $form_attrs['title'] : '';
1809 $form_title = sprintf( '%s %s', $form_post['post_title'], $form_title );
1810 $options[] = [
1811 'label' => $form_title,
1812 'value' => $form_id,
1813 ];
1814 }
1815 $count++;
1816 }
1817 }
1818
1819 return [
1820 'options' => $options,
1821 'hasMore' => false,
1822 ];
1823 }
1824
1825 /**
1826 * Get Comment Pluggable data.
1827 *
1828 * @param array $data data.
1829 *
1830 * @return array
1831 */
1832 public function search_pluggables_wp_insert_comment( $data ) {
1833 $context = [];
1834 $args = [
1835 'number' => 1,
1836 'status' => 'approve',
1837 'post_type' => $data['filter']['post_type']['value'],
1838 ];
1839
1840 if ( isset( $data['filter']['post']['value'] ) ) {
1841 $post_id = $data['filter']['post']['value'];
1842 if ( $post_id > 0 ) {
1843 $args['post_id'] = $post_id;
1844 }
1845 }
1846
1847 $comments = get_comments( $args );
1848
1849 if ( empty( $comments ) ) {
1850 unset( $args['post_id'] );
1851 $comments = get_comments( $args );
1852 }
1853
1854 $context['context_data'] = $data;
1855 $context['context_args'] = $args;
1856
1857 if ( ! empty( $comments ) && is_array( $comments ) ) {
1858 foreach ( $comments as $comment ) :
1859 if ( is_object( $comment ) ) {
1860 $comment = get_object_vars( $comment );
1861 }
1862
1863 if ( is_array( $comment ) && isset( $comment['comment_post_ID'] ) && is_numeric( $comment['comment_post_ID'] ) ) {
1864 $post_id = absint( $comment['comment_post_ID'] );
1865 $post = get_post( $post_id );
1866
1867 if ( is_object( $post ) ) {
1868 $postauthor = (int) $post->post_author;
1869
1870 $context['pluggable_data'] = [
1871 'post' => $post_id,
1872 'post_title' => $post->post_title,
1873 'post_author' => get_the_author_meta( 'display_name', $postauthor ),
1874 'post_link' => get_the_permalink( $post_id ),
1875 'comment_id' => $comment['comment_ID'],
1876 'comment' => $comment['comment_content'],
1877 'comment_author' => $comment['comment_author'],
1878 'comment_author_email' => $comment['comment_author_email'],
1879 'comment_date' => $comment['comment_date'],
1880 ];
1881 }
1882 }
1883
1884 if ( is_array( $comment ) && isset( $comment['comment_author_email'] ) ) {
1885 $user_email = $comment['comment_author_email'];
1886 /**
1887 *
1888 * Ignore line
1889 *
1890 * @phpstan-ignore-next-line
1891 */
1892 $user = get_user_by( 'email', $user_email );
1893
1894 if ( $user ) {
1895
1896 $context['pluggable_data']['wp_user_id'] = $user->ID;
1897 $context['pluggable_data']['user_login'] = $user->user_login;
1898 $context['pluggable_data']['display_name'] = $user->display_name;
1899 $context['pluggable_data']['user_firstname'] = $user->user_firstname;
1900 $context['pluggable_data']['user_lastname'] = $user->user_lastname;
1901 $context['pluggable_data']['user_email'] = $user->user_email;
1902 $context['pluggable_data']['user_role'] = $user->roles;
1903 }
1904 }
1905
1906 $context['response_type'] = 'live';
1907 endforeach;
1908 } else {
1909
1910 $sample_comment = [
1911 'post' => 100,
1912 'post_title' => 'Sample Post',
1913 'comment_id' => 101,
1914 'comment' => 'Sample Comment',
1915 'post_author' => 'Sample Author',
1916 'post_link' => 'https://example.com/sample-post',
1917 'comment_author' => 'Test User',
1918 'comment_author_email' => 'testuser@example.com',
1919 'comment_date' => '2024-01-01 10:00:00',
1920 ];
1921 $sample_comment['wp_user_id'] = 7;
1922 $sample_comment['user_login'] = 'testuser@gmail.com';
1923 $sample_comment['display_name'] = 'Test User';
1924 $sample_comment['user_firstname'] = 'Test';
1925 $sample_comment['user_lastname'] = 'User';
1926 $sample_comment['user_email'] = 'testuser@gmail.com';
1927 $sample_comment['user_role'] = [ 'Subscriber' ];
1928
1929 $context['pluggable_data'] = $sample_comment;
1930 $context['response_type'] = 'sample';
1931 }
1932
1933 return $context;
1934 }
1935
1936
1937 /**
1938 * User reset password.
1939 *
1940 * @param array $data data.
1941 * @return array
1942 */
1943 public function search_pluggables_user_reset_password( $data ) {
1944 $user_context = $this->search_pluggables_add_user_role( $data );
1945 $user_context['pluggable_data']['new_password'] = '***password***';
1946 return $user_context;
1947 }
1948
1949 /**
1950 * User pluggable data.
1951 *
1952 * @param array $data data.
1953 * @return array
1954 */
1955 public function search_pluggables_add_user_role( $data ) {
1956 $context = [];
1957 $args = [
1958 'order' => 'DESC',
1959 'number' => 1,
1960 'orderby' => 'ID',
1961 ];
1962
1963 if ( isset( $data['filter']['role']['value'] ) ) {
1964 $role = $data['filter']['role']['value'];
1965 $args['role'] = $role;
1966 }
1967 if ( isset( $data['filter']['new_role']['value'] ) ) {
1968 $role = $data['filter']['new_role']['value'];
1969 $args['role'] = $role;
1970 }
1971
1972 $users = get_users( $args );
1973
1974 if ( isset( $data['filter']['meta_key']['value'] ) ) {
1975 $meta_key = $data['filter']['meta_key']['value'];
1976 $args['st_meta_key'] = $meta_key;
1977 }
1978
1979 if ( isset( $data['filter']['profile_field']['value'] ) ) {
1980 $meta_key = $data['filter']['profile_field']['value'];
1981 $args['profile_field'] = $meta_key;
1982 }
1983
1984 if ( ! empty( $users ) ) {
1985 $user = $users[0];
1986 $pluggable_data = [];
1987 $pluggable_data['wp_user_id'] = $user->ID;
1988 $pluggable_data['user_login'] = $user->user_login;
1989 $pluggable_data['display_name'] = $user->display_name;
1990 $pluggable_data['user_firstname'] = $user->user_firstname;
1991 $pluggable_data['user_lastname'] = $user->user_lastname;
1992 $pluggable_data['user_email'] = $user->user_email;
1993 $pluggable_data['user_registered'] = $user->user_registered;
1994 $pluggable_data['user_role'] = $user->roles;
1995 if ( isset( $args['st_meta_key'] ) ) {
1996 $pluggable_data['meta_key'] = $args['st_meta_key'];
1997 $pluggable_data['meta_value'] = get_user_meta( $user->ID, $args['st_meta_key'], true );
1998 }
1999 if ( isset( $args['profile_field'] ) ) {
2000 $userdata = get_userdata( $user->ID );
2001 if ( $userdata instanceof \WP_User ) {
2002 $userdata = json_decode( (string) wp_json_encode( $userdata->data ), true );
2003
2004 $pluggable_data['profile_field'] = $args['profile_field'];
2005 if ( is_array( $userdata ) && is_string( $args['profile_field'] ) ) {
2006 $pluggable_data['profile_field_value'] = $userdata[ $args['profile_field'] ];
2007 }
2008 }
2009 }
2010 $context['pluggable_data'] = $pluggable_data;
2011 $context['response_type'] = 'live';
2012 } else {
2013 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
2014 $context['pluggable_data'] = [
2015 'wp_user_id' => 1,
2016 'user_login' => 'admin',
2017 'display_name' => 'Test User',
2018 'user_firstname' => 'Test',
2019 'user_lastname' => 'User',
2020 'user_email' => 'testuser@gmail.com',
2021 'user_registered' => '2024-06-18 09:47:58',
2022 'user_role' => [ $role ],
2023 ];
2024 if ( isset( $args['st_meta_key'] ) ) {
2025 $context['pluggable_data']['meta_key'] = $args['st_meta_key'];
2026 $context['pluggable_data']['meta_value'] = 'test meta value';
2027 }
2028 if ( isset( $args['profile_field'] ) ) {
2029 $context['pluggable_data']['profile_field'] = $args['profile_field'];
2030 $context['pluggable_data']['profile_field_value'] = 'Profile Field Value';
2031 }
2032 $context['response_type'] = 'sample';
2033 }
2034 return $context;
2035 }
2036
2037 /**
2038 * User pluggable data.
2039 *
2040 * @param array $data data.
2041 * @return array
2042 */
2043 public function search_pluggables_last_user_login( $data ) {
2044 $context = [];
2045 $args = [
2046 'orderby' => 'meta_value',
2047 'meta_key' => 'st_last_login',
2048 'order' => 'DESC',
2049 'number' => 1,
2050 ];
2051 $users = get_users( $args );
2052
2053 if ( ! empty( $users ) ) {
2054 $user = $users[0];
2055 $pluggable_data = [];
2056 $pluggable_data['wp_user_id'] = $user->ID;
2057 $pluggable_data['user_login'] = $user->user_login;
2058 $pluggable_data['display_name'] = $user->display_name;
2059 $pluggable_data['user_firstname'] = $user->user_firstname;
2060 $pluggable_data['user_lastname'] = $user->user_lastname;
2061 $pluggable_data['user_email'] = $user->user_email;
2062 $pluggable_data['user_role'] = $user->roles;
2063
2064 $context['pluggable_data'] = $pluggable_data;
2065 $context['response_type'] = 'live';
2066 } else {
2067 $role = 'subscriber';
2068 $context['pluggable_data'] = [
2069 'wp_user_id' => 1,
2070 'user_login' => 'admin',
2071 'display_name' => 'Test User',
2072 'user_firstname' => 'Test',
2073 'user_lastname' => 'User',
2074 'user_email' => 'testuser@gmail.com',
2075 'user_role' => [ $role ],
2076 ];
2077 $context['response_type'] = 'sample';
2078 }
2079 return $context;
2080 }
2081
2082 /**
2083 * Donation pluggable data.
2084 *
2085 * @param array $data data.
2086 * @return array
2087 */
2088 public function search_pluggables_wordpress_post( $data ) {
2089 $context = [];
2090 $args = [
2091 'post_type' => 'any',
2092 'posts_per_page' => 1,
2093 'orderby' => 'modified',
2094 'order' => 'DESC',
2095 ];
2096
2097 if ( isset( $data['filter']['post_type']['value'] ) ) {
2098 $post_type = $data['filter']['post_type']['value'];
2099 $args['post_type'] = $post_type;
2100 }
2101
2102 if ( isset( $data['filter']['status']['value'] ) ) {
2103 $post_status = $data['filter']['status']['value'];
2104 $args['post_status'] = $post_status;
2105 }
2106
2107 if ( isset( $data['filter']['post_status']['value'] ) ) {
2108 $post_status = $data['filter']['post_status']['value'];
2109 $args['post_status'] = $post_status;
2110 }
2111
2112 if ( isset( $data['filter']['post']['value'] ) ) {
2113 $post_id = $data['filter']['post']['value'];
2114 if ( $post_id > 0 ) {
2115 $args['p'] = $post_id;
2116 unset( $args['post_status'] );
2117 }
2118 }
2119
2120 $posts = get_posts( $args );
2121 if ( ! empty( $posts ) ) {
2122 $context['pluggable_data'] = $posts[0];
2123 $custom_metas = get_post_meta( $posts[0]->ID );
2124 if ( property_exists( $context['pluggable_data'], 'post' ) ) {
2125 $context['pluggable_data']->post = $posts[0]->ID;
2126 }
2127 if ( is_object( $context['pluggable_data'] ) ) {
2128 $context['pluggable_data'] = get_object_vars( $context['pluggable_data'] );
2129 }
2130 $context['pluggable_data']['featured_image'] = wp_get_attachment_image_src( (int) get_post_thumbnail_id( $posts[0]->ID ), 'full' )[0]; // @phpstan-ignore-line
2131 $context['pluggable_data']['permalink'] = get_permalink( $posts[0]->ID );
2132 if ( $posts[0] instanceof WP_Post ) {
2133 $taxonomies = get_object_taxonomies( get_post( $posts[0] ), 'objects' );
2134 if ( ! empty( $taxonomies ) ) {
2135 foreach ( $taxonomies as $taxonomy => $taxonomy_object ) {
2136 $terms = get_the_terms( $posts[0]->ID, $taxonomy );
2137 if ( ! empty( $terms ) && is_array( $terms ) ) {
2138 foreach ( $terms as $term ) {
2139 $context['pluggable_data'][ $taxonomy ] = $term->name;
2140 }
2141 }
2142 }
2143 }
2144 }
2145 $context['pluggable_data'] = array_merge( $context['pluggable_data'], is_numeric( $posts[0]->post_author ) ? WordPress::get_user_context( (int) $posts[0]->post_author ) : [] );
2146 $context['pluggable_data']['post'] = $posts[0]->ID;
2147 $context['pluggable_data']['custom_metas'] = $custom_metas;
2148 $context['response_type'] = 'live';
2149 } else {
2150 $context['pluggable_data'] = [
2151 'ID' => 557,
2152 'post' => 557,
2153 'post_author' => 1,
2154 'post_date' => '2022-11-18 12:18:14',
2155 'post_date_gmt' => '2022-11-18 12:18:14',
2156 'post_content' => 'Test Post Content',
2157 'post_title' => 'Test Post',
2158 'post_excerpt' => '',
2159 'post_status' => 'published',
2160 'comment_status' => 'open',
2161 'ping_status' => 'open',
2162 'post_password' => '',
2163 'post_name' => 'test-post',
2164 'to_ping' => '',
2165 'pinged' => '',
2166 'post_modified' => '2022-11-18 12:18:14',
2167 'post_modified_gmt' => '2022-11-18 12:18:14',
2168 'post_content_filtered' => '',
2169 'post_parent' => 0,
2170 'guid' => 'https://abc.com/?p=1',
2171 'permalink' => 'https://abc.com/test-post/',
2172 'menu_order' => 0,
2173 'post_type' => 'post',
2174 'post_mime_type' => '',
2175 'comment_count' => 0,
2176 'filter' => 'raw',
2177 'featured_image' => 'https://abc.com/test-post/',
2178 ];
2179 $context['response_type'] = 'sample';
2180 }
2181
2182 return $context;
2183 }
2184
2185 /**
2186 * Donation pluggable data
2187 *
2188 * @param array $data data.
2189 * @return array
2190 */
2191 public function search_pluggables_givewp_donation_via_form( $data ) {
2192 global $wpdb;
2193 $context = [];
2194 $pluggable_data = [];
2195 $form_id = $data['filter']['form_id']['value'];
2196 $subscription_id = 1;
2197
2198 if ( ! class_exists( 'Give_Payment' ) || ! class_exists( 'Give_Subscription' ) ) {
2199 return [];
2200 }
2201
2202 if ( ! function_exists( 'give_get_donor_donation_comment' ) ) {
2203 return [];
2204 }
2205
2206 if ( 'donation_specific_field_value' == $data['search_term'] ) {
2207 $field_id = $data['filter']['field_id']['value'];
2208 $field_value = $data['filter']['field_value']['value'];
2209
2210 $donation_meta = $wpdb->get_row(
2211 $wpdb->prepare(
2212 "SELECT * FROM {$wpdb->prefix}give_donationmeta
2213 WHERE meta_key LIKE %s AND
2214 meta_value LIKE %s
2215 ORDER BY donation_id
2216 DESC LIMIT 1",
2217 $field_id,
2218 $field_value
2219 )
2220 );
2221 if ( $donation_meta ) {
2222 $donation_form_meta = $wpdb->get_row(
2223 $wpdb->prepare(
2224 "SELECT * FROM {$wpdb->prefix}give_donationmeta
2225 WHERE donation_id = %d AND
2226 meta_key LIKE %s AND
2227 meta_value LIKE %d
2228 ORDER BY donation_id
2229 DESC LIMIT 1",
2230 $donation_meta->donation_id,
2231 '_give_payment_form_id',
2232 $form_id
2233 )
2234 );
2235
2236 $payment = $wpdb->get_row(
2237 $wpdb->prepare(
2238 "SELECT * FROM {$wpdb->prefix}posts WHERE
2239 ID=%d ORDER BY ID DESC LIMIT 1",
2240 $donation_form_meta->donation_id
2241 )
2242 );
2243 }
2244 } elseif ( 'donation_specific_amount' == $data['search_term'] ) {
2245 $condition_compare = $data['filter']['condition_compare']['value'];
2246 $donation_amount = $data['filter']['donation_amount']['value'];
2247
2248 $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
2249
2250 if ( $donation_meta ) {
2251 $donation_form_meta = $wpdb->get_row(
2252 $wpdb->prepare(
2253 "SELECT * FROM {$wpdb->prefix}give_donationmeta
2254 WHERE donation_id = %d AND
2255 meta_key LIKE %s AND
2256 meta_value LIKE %d
2257 ORDER BY donation_id
2258 DESC LIMIT 1",
2259 $donation_meta->donation_id,
2260 '_give_payment_form_id',
2261 $form_id
2262 )
2263 );
2264
2265 $payment = $wpdb->get_row(
2266 $wpdb->prepare(
2267 "SELECT * FROM {$wpdb->prefix}posts WHERE
2268 ID=%d ORDER BY ID DESC LIMIT 1",
2269 $donation_form_meta->donation_id
2270 )
2271 );
2272 }
2273 } elseif ( 'cancels_recurring_donation' == $data['search_term'] ) {
2274 $donation_meta = $wpdb->get_row(
2275 $wpdb->prepare(
2276 "SELECT * FROM {$wpdb->prefix}give_subscriptions
2277 WHERE product_id=%d AND
2278 status LIKE %s
2279 ORDER BY id
2280 DESC LIMIT 1",
2281 $form_id,
2282 'cancelled'
2283 )
2284 );
2285 if ( $donation_meta ) {
2286 $subscription_id = $donation_meta->id;
2287 $payment = $wpdb->get_row(
2288 $wpdb->prepare(
2289 "SELECT * FROM {$wpdb->prefix}posts WHERE
2290 ID=%d ORDER BY ID DESC LIMIT 1",
2291 $donation_meta->parent_payment_id
2292 )
2293 );
2294 }
2295 } elseif ( 'continues_recurring_donation' == $data['search_term'] ) {
2296 $donation_meta = $wpdb->get_row(
2297 $wpdb->prepare(
2298 "SELECT * FROM {$wpdb->prefix}give_subscriptions
2299 WHERE product_id=%d AND
2300 status LIKE %s
2301 ORDER BY id
2302 DESC LIMIT 1",
2303 $form_id,
2304 'active'
2305 )
2306 );
2307 if ( $donation_meta ) {
2308 $subscription_id = $donation_meta->id;
2309
2310 $payment = $wpdb->get_row(
2311 $wpdb->prepare(
2312 "SELECT * FROM {$wpdb->prefix}posts
2313 WHERE post_parent=%d AND post_status LIKE %s
2314 ORDER BY ID DESC LIMIT 1",
2315 $donation_meta->parent_payment_id,
2316 'give_subscription'
2317 )
2318 );
2319 }
2320 } else {
2321 $payment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type=%s ORDER BY id DESC LIMIT 1", 'give_payment' ) );
2322 }
2323
2324 if ( ! empty( $payment ) ) {
2325 if ( 'continues_recurring_donation' == $data['search_term'] || 'cancels_recurring_donation' == $data['search_term'] ) {
2326 $subscription = new Give_Subscription( $subscription_id );
2327
2328 $pluggable_data['form_id'] = $form_id;
2329 $pluggable_data['subscription'] = $subscription;
2330 } elseif ( 'donation_specific_field_value' == $data['search_term'] ) {
2331 $payment_data = new Give_Payment( $payment->ID );
2332 $input_array = $payment_data->payment_meta;
2333 unset( $input_array['user_info'] );
2334 $pluggable_data = $input_array;
2335 foreach ( $input_array as $key => $value ) {
2336 $pluggable_data['field_id'] = $key;
2337 $pluggable_data['field_value'] = $value;
2338 }
2339 } else {
2340 $payment = new Give_Payment( $payment->ID );
2341 $address_data = $payment->address;
2342
2343 $pluggable_data['first_name'] = $payment->first_name;
2344 $pluggable_data['last_name'] = $payment->last_name;
2345 $pluggable_data['email'] = $payment->email;
2346 $pluggable_data['currency'] = $payment->currency;
2347 $pluggable_data['donated_amount'] = $payment->subtotal;
2348 $pluggable_data['donation_amount'] = $payment->subtotal;
2349 $pluggable_data['form_id'] = (int) $payment->form_id;
2350 $pluggable_data['form_title'] = $payment->form_title;
2351 $pluggable_data['name_title_prefix'] = $payment->title_prefix;
2352 $pluggable_data['date'] = $payment->date;
2353
2354 if ( is_array( $address_data ) ) {
2355 $pluggable_data['address_line_1'] = $address_data['line1'];
2356 $pluggable_data['address_line_2'] = $address_data['line2'];
2357 $pluggable_data['city'] = $address_data['city'];
2358 $pluggable_data['state'] = $address_data['state'];
2359 $pluggable_data['zip'] = $address_data['zip'];
2360 $pluggable_data['country'] = $address_data['country'];
2361 }
2362 // Payment meta.
2363 $payment_meta = $payment->get_meta();
2364 if ( is_array( $payment_meta ) && isset( $payment_meta['user_info'] ) ) {
2365 unset( $payment_meta['user_info'] );
2366 }
2367 $pluggable_data['payment_meta'] = $payment_meta;
2368 $donor_comment = give_get_donor_donation_comment( $payment->ID, $payment->donor_id );
2369 $pluggable_data['comment'] = ( is_array( $donor_comment ) && isset( $donor_comment['comment_content'] ) ) ? $donor_comment : '';
2370 }
2371
2372 $context['response_type'] = 'live';
2373 } else {
2374 if ( 'continues_recurring_donation' == $data['search_term'] || 'cancels_recurring_donation' == $data['search_term'] ) {
2375 $pluggable_data['form_id'] = $form_id;
2376 $pluggable_data['subscription']['id'] = 3;
2377 $pluggable_data['subscription']['donor_id'] = 8;
2378 $pluggable_data['subscription']['period'] = 'month';
2379 $pluggable_data['subscription']['initial_amount'] = '25.0000000000';
2380 $pluggable_data['subscription']['recurring_amount'] = '25.0000000000';
2381 $pluggable_data['subscription']['recurring_fee_amount'] = '0.0000000000';
2382 $pluggable_data['subscription']['transaction_id'] = 'a228ec9c6357963d23079d7d6945dd61';
2383 $pluggable_data['subscription']['parent_payment_id'] = '7492';
2384 $pluggable_data['subscription']['created'] = '2024-01-23 11:12:11';
2385 $pluggable_data['subscription']['expiration'] = '2024-03-23 23:59:59';
2386 $pluggable_data['subscription']['status'] = 'cancelled';
2387 $pluggable_data['subscription']['donor']['id'] = 8;
2388 $pluggable_data['subscription']['donor']['purchase_count'] = '3';
2389 $pluggable_data['subscription']['donor']['purchase_value'] = '75.000000';
2390 $pluggable_data['subscription']['donor']['email'] = 'johndoee@yopmail.com';
2391 $pluggable_data['subscription']['donor']['name'] = 'John Doe';
2392 $pluggable_data['subscription']['donor']['payment_ids'] = '7487,7492,7499';
2393 $pluggable_data['subscription']['donor']['user_id'] = '131';
2394 $pluggable_data['subscription']['customer_id'] = '8';
2395 } elseif ( 'donation_specific_field_value' == $data['search_term'] ) {
2396 $pluggable_data['form_id'] = 23;
2397 $pluggable_data['form_title'] = 'Demo Donation';
2398 $pluggable_data['_give_donor_billing_first_name'] = 'John';
2399 $pluggable_data['_give_donor_billing_last_name'] = 'Doe';
2400 $pluggable_data['_give_payment_donor_email'] = 'johndoee@gmail.com';
2401 $pluggable_data['_give_payment_currency'] = 'USD';
2402 $pluggable_data['_give_payment_total'] = '100';
2403 $pluggable_data['name_title_prefix'] = 'Mr';
2404 $pluggable_data['date'] = '2022-11-07 16:06:05';
2405 $pluggable_data['_give_donor_billing_address1'] = '33, Vincent Road';
2406 $pluggable_data['_give_donor_billing_address2'] = 'Chicago Street';
2407 $pluggable_data['_give_donor_billing_city'] = 'New York City';
2408 $pluggable_data['_give_donor_billing_state'] = 'New York';
2409 $pluggable_data['_give_donor_billing_zip'] = '223131';
2410 $pluggable_data['_give_donor_billing_country'] = 'USA';
2411 $pluggable_data['_give_donation_comment'] = 'Demo Comment';
2412 $pluggable_data['field_id'] = 'last_name';
2413 $pluggable_data['field_value'] = 'Doe';
2414 } else {
2415 $pluggable_data['first_name'] = 'John';
2416 $pluggable_data['last_name'] = 'Doe';
2417 $pluggable_data['email'] = 'johndoee@gmail.com';
2418 $pluggable_data['currency'] = 'USD';
2419 $pluggable_data['donated_amount'] = 100;
2420 $pluggable_data['donation_amount'] = 100;
2421 $pluggable_data['form_id'] = 23;
2422 $pluggable_data['form_title'] = 'Demo Donation';
2423 $pluggable_data['name_title_prefix'] = 'Mr';
2424 $pluggable_data['date'] = '2022-11-07 16:06:05';
2425 $pluggable_data['address_line_1'] = '33, Vincent Road';
2426 $pluggable_data['address_line_2'] = 'Chicago Street';
2427 $pluggable_data['city'] = 'New York City';
2428 $pluggable_data['state'] = 'New York';
2429 $pluggable_data['zip'] = '223131';
2430 $pluggable_data['country'] = 'USA';
2431 $pluggable_data['comment'] = 'Demo Comment';
2432 }
2433 $context['response_type'] = 'sample';
2434 }
2435
2436 $context['pluggable_data'] = $pluggable_data;
2437 return $context;
2438 }
2439
2440 /**
2441 * Search Divi Form fields.
2442 *
2443 * @param array $data Search Params.
2444 *
2445 * @since 1.0.0
2446 *
2447 * @return array
2448 */
2449 public function search_pluggable_divi_form_fields( $data ) {
2450 $result = [];
2451 $form_id = absint( $data['dynamic'] );
2452 $form_posts = Utilities::get_divi_forms();
2453
2454 if ( empty( $form_posts ) ) {
2455 return [
2456 'options' => [],
2457 'hasMore' => false,
2458 ];
2459 }
2460 $fields = [];
2461 foreach ( (array) $form_posts as $form_post ) {
2462 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
2463 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
2464 if ( empty( $forms ) ) {
2465 continue;
2466 }
2467
2468 $count = 0;
2469
2470 foreach ( $forms as $form ) {
2471 $pattern = get_shortcode_regex( [ 'et_pb_contact_field' ] );
2472
2473 preg_match_all( "/$pattern/", $form[0], $contact_fields, PREG_SET_ORDER );
2474
2475 if ( empty( $contact_fields ) ) {
2476 return $fields;
2477 }
2478
2479 foreach ( $contact_fields as $contact_field ) {
2480 $contact_field_attrs = shortcode_parse_atts( $contact_field[3] );
2481 $contact_field_attrs_str = self::array_get( $contact_field_attrs, 'field_id' );
2482 if ( is_string( $contact_field_attrs_str ) ) {
2483 $field_id = strtolower( $contact_field_attrs_str );
2484 $fields[] = [
2485 'field_title' => self::array_get( $contact_field_attrs, 'field_title', __( 'No title', 'suretriggers' ) ),
2486 'field_id' => $field_id,
2487 ];
2488 }
2489 }
2490 }
2491 }
2492
2493 if ( ! empty( $fields ) ) {
2494 foreach ( $fields as $field ) {
2495 $result[] = [
2496 'label' => $field['field_title'],
2497 'value' => '{' . $field['field_id'] . '}',
2498 ];
2499 }
2500 }
2501
2502 return [
2503 'options' => $result,
2504 'hasMore' => false,
2505 ];
2506 }
2507
2508 /**
2509 * Pseudo function copied from Divi
2510 *
2511 * @param array $array An array which contains value located at `$address`.
2512 * @param string|array $address The location of the value within `$array` (dot notation).
2513 * @param mixed $default Value to return if not found. Default is an empty string.
2514 *
2515 * @return mixed The value, if found, otherwise $default.
2516 */
2517 public static function array_get( $array, $address, $default = '' ) {
2518 $keys = is_array( $address ) ? $address : explode( '.', $address );
2519 $value = $array;
2520
2521 foreach ( $keys as $key ) {
2522 if ( ! empty( $key ) && isset( $key[0] ) && '[' === $key[0] ) {
2523 $index = substr( $key, 1, -1 );
2524
2525 if ( is_numeric( $index ) ) {
2526 $key = (int) $index;
2527 }
2528 }
2529
2530 if ( ! isset( $value[ $key ] ) ) {
2531 return $default;
2532 }
2533
2534 $value = $value[ $key ];
2535 }
2536
2537 return $value;
2538 }
2539
2540 /**
2541 * Get UAG Forms.
2542 *
2543 * @param array $data data.
2544 *
2545 * @return array
2546 */
2547 public function search_spectra_forms( $data ) {
2548 global $wpdb;
2549 $form_posts = Utilities::get_uag_forms();
2550
2551 $options = [];
2552 if ( empty( $form_posts ) ) {
2553 return [
2554 'options' => [],
2555 'hasMore' => false,
2556 ];
2557 }
2558
2559 foreach ( (array) $form_posts as $form_post ) {
2560 $blocks = parse_blocks( $form_post['post_content'] );
2561 $i = 1;
2562 // Get form blocks.
2563 $this->process_blocks( $blocks, $form_post, $options, $i );
2564 }
2565
2566 // Below is for Spectra forms from widgets.
2567 $search_string = '%wp-block-uagb-forms%';
2568 $option_results = $wpdb->get_results(
2569 $wpdb->prepare(
2570 "
2571 SELECT `option_name`, `option_value`
2572 FROM {$wpdb->options}
2573 WHERE (
2574 option_name LIKE %s OR
2575 option_name LIKE %s OR
2576 option_name LIKE %s
2577 )
2578 AND option_value LIKE %s
2579 ",
2580 'widget_%',
2581 'theme_mods_%',
2582 'customize_changeset_%',
2583 $search_string
2584 ),
2585 ARRAY_A
2586 );
2587 if ( ! empty( $option_results ) ) {
2588 foreach ( (array) $option_results as $form_post ) {
2589 $blocks = parse_blocks( $form_post['option_value'] );
2590 $i = 1;
2591 $this->process_blocks( $blocks, $form_post, $options, $i );
2592 }
2593 }
2594
2595 return [
2596 'options' => $options,
2597 'hasMore' => false,
2598 ];
2599 }
2600
2601 /**
2602 * Return forms in nested blocks in Spectra.
2603 *
2604 * @param array $blocks data.
2605 * @param array $form_post Form post.
2606 * @param array $options Options.
2607 * @param int $i Number.
2608 *
2609 * @return array
2610 */
2611 public function process_blocks( $blocks, $form_post, &$options, &$i ) {
2612 foreach ( $blocks as $block ) {
2613 if ( 'uagb/forms' === $block['blockName'] ) {
2614 $prefix = '';
2615 if ( isset( $form_post['post_title'] ) && ! empty( $form_post['post_title'] ) ) {
2616 $prefix = $form_post['post_title'] . ' - ';
2617 } elseif (
2618 isset( $form_post['option_value'] ) ) {
2619 $option_value = maybe_unserialize( $form_post['option_value'] );
2620 if ( is_array( $option_value ) ) {
2621 foreach ( $option_value as $widget_block ) {
2622 if (
2623 isset( $widget_block['content'] )
2624 && is_string( $widget_block['content'] )
2625 && strpos( $widget_block['content'], 'uagb/forms' ) !== false
2626 ) {
2627 if ( preg_match( '/"metadata"\s*:\s*{[^}]*"name"\s*:\s*"([^"]+)"/', $widget_block['content'], $matches ) ) {
2628 $prefix = sanitize_text_field( $matches[1] ) . ' - ';
2629 break;
2630 }
2631 }
2632 }
2633 }
2634 }
2635
2636 $options[] = [
2637 'label' => $prefix . $block['attrs']['block_id'] . ' (Form ' . ( $i++ ) . ')',
2638 'value' => $block['attrs']['block_id'],
2639 ];
2640 } elseif ( isset( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) {
2641 $this->process_blocks( $block['innerBlocks'], $form_post, $options, $i );
2642 }
2643 }
2644 return $options;
2645 }
2646
2647 /**
2648 * Check array recursive.
2649 *
2650 * @param array $array Array.
2651 * @param string $value search params.
2652 * @since 1.0.0
2653 *
2654 * @return array|void|int|string
2655 */
2656 public static function get_column_by_value( $array, $value ) {
2657
2658 foreach ( $array as $key => $sub_array ) {
2659
2660 if ( is_array( $sub_array ) ) {
2661 $result = self::get_column_by_value( $sub_array, $value );
2662 if ( null !== $result ) {
2663 return $key;
2664 }
2665 } else {
2666 return $key;
2667 }
2668 }
2669 return null;
2670
2671 }
2672
2673
2674 /**
2675 * Search UAG Form fields.
2676 *
2677 * @param array $data Search Params.
2678 *
2679 * @since 1.0.0
2680 *
2681 * @return array
2682 */
2683 public function search_spectraform_fields( $data ) {
2684 $result = [];
2685 $form_id = absint( $data['dynamic'] );
2686 $form_posts = Utilities::get_uag_forms();
2687
2688 if ( empty( $form_posts ) ) {
2689 return [
2690 'options' => [],
2691 'hasMore' => false,
2692 ];
2693 }
2694
2695 foreach ( (array) $form_posts as $form_post ) {
2696 $blocks = parse_blocks( $form_post['post_content'] );
2697
2698 foreach ( $blocks as $block ) {
2699 if ( (int) $block['attrs']['block_id'] === $form_id ) {
2700 $doc = new DOMDocument();
2701 $rendered_block = render_block( $block );
2702 $doc->loadHTML( $rendered_block );
2703 $child_node_list = $doc->getElementsByTagName( 'div' );
2704 $nodes = [];
2705 for ( $i = 0; $i < $child_node_list->length; $i++ ) {
2706 $temp = $child_node_list->item( $i );
2707 if ( $temp && stripos( $temp->getAttribute( 'class' ), 'uagb-forms-input-label' ) !== false ) {
2708 $nodes[] = $temp;
2709 }
2710 }
2711 if ( ! empty( $nodes ) ) {
2712 foreach ( $nodes as $node ) {
2713 $result[] = [
2714 'label' => $node->textContent, //phpcs:ignore
2715 'value' => $node->textContent, //phpcs:ignore
2716 ];
2717 }
2718 }
2719 }
2720 }
2721 }
2722
2723 return [
2724 'options' => $result,
2725 'hasMore' => false,
2726 ];
2727 }
2728
2729 /**
2730 * Search forms of MetForms.
2731 *
2732 * @param array $data data.
2733 * @return array
2734 */
2735 public function search_met_forms( $data ) {
2736 $args = [
2737 'post_type' => 'metform-form',
2738 'post_status' => 'publish',
2739 'numberposts' => -1,
2740 ];
2741
2742 $forms = get_posts( $args );
2743
2744 $options = [];
2745
2746 if ( ! empty( $forms ) ) {
2747 foreach ( $forms as $form ) {
2748 $options[] = [
2749 'label' => $form->post_title,
2750 'value' => $form->ID,
2751 ];
2752 }
2753 }
2754
2755 return [
2756 'options' => $options,
2757 'hasMore' => false,
2758 ];
2759 }
2760
2761 /**
2762 * Search forms of Ninja Forms.
2763 *
2764 * @param array $data data.
2765 * @return array
2766 */
2767 public function search_ninja_forms( $data ) {
2768 $options = [];
2769
2770 if ( function_exists( 'Ninja_Forms' ) ) {
2771 foreach ( Ninja_Forms()->form()->get_forms() as $form ) {
2772 $options[] = [
2773 'label' => $form->get_setting( 'title' ),
2774 'value' => $form->get_id(),
2775 ];
2776 }
2777 }
2778
2779 return [
2780 'options' => $options,
2781 'hasMore' => false,
2782 ];
2783 }
2784
2785 /**
2786 * Search forms of Pie Forms.
2787 *
2788 * @param array $data data.
2789 * @return array
2790 */
2791 public function search_pie_forms( $data ) {
2792 global $wpdb;
2793 $options = [];
2794
2795 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pf_forms' ) ) ) {
2796
2797 $results = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'pf_forms WHERE post_status = "published"' );
2798
2799 if ( $results ) {
2800 foreach ( $results as $result ) {
2801 $options[] = [
2802 'label' => $result->form_title,
2803 'value' => $result->id,
2804 ];
2805 }
2806 }
2807 }
2808
2809 return [
2810 'options' => $options,
2811 'hasMore' => false,
2812 ];
2813 }
2814
2815 /**
2816 * Get Fluent Forms.
2817 *
2818 * @param array $data data.
2819 *
2820 * @return array
2821 */
2822 public function search_fluent_forms( $data ) {
2823
2824 if ( ! function_exists( 'wpFluent' ) ) {
2825 return [
2826 'options' => [],
2827 'hasMore' => false,
2828 ];
2829 }
2830
2831 $forms = wpFluent()->table( 'fluentform_forms' )
2832 ->select( [ 'id', 'title' ] )
2833 ->orderBy( 'id', 'DESC' )
2834 ->get();
2835
2836 $options = [];
2837 if ( ! empty( $forms ) ) {
2838 foreach ( $forms as $form ) {
2839 $options[] = [
2840 'label' => $form->title,
2841 'value' => $form->id,
2842 ];
2843 }
2844 }
2845
2846 return [
2847 'options' => $options,
2848 'hasMore' => false,
2849 ];
2850
2851 }
2852
2853 /**
2854 * Get Fluent Forms Fields.
2855 *
2856 * @param array $data data.
2857 *
2858 * @return array
2859 */
2860 public function search_fluent_forms_form_fields( $data ) {
2861
2862 $options = [];
2863
2864 if ( ! function_exists( 'wpFluent' ) ) {
2865 return [];
2866 }
2867
2868 $form = wpFluent()->table( 'fluentform_forms' )->find( $data['dynamic'] );
2869 $field_data = json_decode( $form->form_fields, true );
2870 if ( is_array( $field_data ) && ! empty( $field_data['fields'] ) ) {
2871 foreach ( $field_data['fields'] as $field ) {
2872 if ( isset( $field['fields'] ) ) {
2873 foreach ( $field['fields'] as $field_key => $sub_field ) {
2874 if (
2875 isset( $sub_field['settings'] )
2876 && isset( $sub_field['settings']['label'] )
2877 && isset( $sub_field['settings']['visible'] )
2878 && true === $sub_field['settings']['visible']
2879 ) {
2880 $options[] = [
2881 'label' => $sub_field['settings']['label'],
2882 'value' => $field_key,
2883 ];
2884 }
2885 }
2886 } elseif ( isset( $field['element'] ) &&
2887 'container' === (string) $field['element'] &&
2888 isset( $field['columns'] ) && is_array( $field['columns'] ) ) {
2889 $container_fields = $field['columns'];
2890 foreach ( $container_fields as $c_fields ) {
2891 foreach ( $c_fields['fields'] as $field_key => $sub_field ) {
2892 if ( isset( $sub_field['settings'] ) ) {
2893 $options[] = [
2894 'label' => ( '' !== $sub_field['settings']['label'] ) ?
2895 $sub_field['settings']['label'] :
2896 $sub_field['attributes']['name'],
2897 'value' => isset( $sub_field['attributes']['name'] ) ?
2898 $sub_field['attributes']['name'] :
2899 strtolower( $sub_field['settings']['label'] ),
2900 ];
2901 }
2902 }
2903 }
2904 } elseif ( isset( $field['attributes'] ) && isset( $field['attributes']['name'] ) ) {
2905 if ( isset( $field['attributes']['placeholder'] ) && ! empty( $field['attributes']['placeholder'] ) ) {
2906 $options[] = [
2907 'label' => $field['attributes']['placeholder'],
2908 'value' => $field['attributes']['name'],
2909 ];
2910 } elseif ( isset( $field['settings'] ) &&
2911 isset( $field['settings']['label'] ) &&
2912 ! empty( $field['settings']['label'] ) ) {
2913 $options[] = [
2914 'label' => $field['settings']['label'],
2915 'value' => $field['attributes']['name'],
2916 ];
2917 }
2918 }
2919 }
2920 }
2921
2922 return [
2923 'options' => $options,
2924 'hasMore' => false,
2925 ];
2926
2927 }
2928
2929 /**
2930 * Get Fluent Forms.
2931 *
2932 * @param array $data data.
2933 *
2934 * @return array
2935 */
2936 public function search_bricks_builder_forms( $data ) {
2937 $bricks_theme = wp_get_theme( 'bricks' );
2938 if ( ! $bricks_theme->exists() ) {
2939 return [
2940 'options' => [],
2941 'hasMore' => false,
2942 ];
2943 }
2944
2945 $bricks_settings = (array) get_option( BRICKS_DB_GLOBAL_SETTINGS );
2946 if ( array_key_exists( 'postTypes', $bricks_settings ) ) {
2947 $bricks_posts = $bricks_settings['postTypes'];
2948 } else {
2949 $bricks_posts = [];
2950 }
2951 $bricks_posts[] = 'bricks_template';
2952
2953 $args = [
2954 'post_type' => $bricks_posts,
2955 'post_status' => 'publish',
2956 'posts_per_page' => -1,
2957 ];
2958
2959 $templates = get_posts( $args );
2960
2961 $options = [];
2962 if ( ! empty( $templates ) ) {
2963 foreach ( $templates as $template ) {
2964 $fetch_content = get_post_meta( $template->ID, BRICKS_DB_PAGE_CONTENT, true );
2965 if ( is_array( $fetch_content ) ) {
2966 foreach ( $fetch_content as $content ) {
2967 if ( 'form' === $content['name'] ) {
2968 $options[] = [
2969 'label' => $template->post_title . ' - ' . ( isset( $content['label'] ) ? $content['label'] : 'Form' ) . ' ( ID: ' . $content['id'] . ')',
2970 'value' => $content['id'],
2971 ];
2972 }
2973 }
2974 }
2975 }
2976 }
2977
2978 return [
2979 'options' => $options,
2980 'hasMore' => false,
2981 ];
2982
2983 }
2984
2985 /**
2986 * Bricks builder form fields.
2987 *
2988 * @param array $data data.
2989 * @return array
2990 */
2991 public function search_pluggable_bricks_builder_form_fields( $data ) {
2992 $result = [];
2993 $fields = [];
2994 $form_id_str = $data['dynamic'];
2995 $ids = explode( '_', $form_id_str );
2996 $post_id = $ids[0];
2997 $form_id = $ids[1];
2998 $fetch_content = get_post_meta( (int) $post_id, BRICKS_DB_PAGE_CONTENT, true );
2999 if ( is_array( $fetch_content ) ) {
3000 foreach ( $fetch_content as $content ) {
3001 if ( 'form' === $content['name'] && $form_id === $content['id'] ) {
3002 $fields = $content['settings']['fields'];
3003 break;
3004 }
3005 }
3006 }
3007
3008 if ( ! empty( $fields ) ) {
3009 foreach ( $fields as $field ) {
3010 $result[] = [
3011 'label' => $field['label'],
3012 'value' => '{' . strtolower( $field['label'] ) . '}',
3013 ];
3014 }
3015 }
3016
3017 return [
3018 'options' => $result,
3019 'hasMore' => false,
3020 ];
3021 }
3022
3023 /**
3024 * Get fluent form fields
3025 *
3026 * @param array $data Data array.
3027 *
3028 * @return array
3029 */
3030 public function search_pluggable_fluent_form_fields( $data ) {
3031 $result = [];
3032 $form_id = absint( $data['dynamic'] );
3033
3034 $fluentform_fields = Utilities::get_fluentform_fields( $data['search_term'], -1, $form_id );
3035
3036 if ( is_array( $fluentform_fields['results'] ) ) {
3037 foreach ( $fluentform_fields['results'] as $field ) {
3038 $result[] = [
3039 'label' => $field['text'],
3040 'value' => "{{$field['value']}}",
3041 ];
3042 }
3043 }
3044
3045 $result[] = [
3046 'value' => '{form_id}',
3047 'label' => 'Form ID',
3048 ];
3049
3050 $result[] = [
3051 'value' => '{form_title}',
3052 'label' => 'Form Title',
3053 ];
3054 $result[] = [
3055 'value' => '{entry_id}',
3056 'label' => 'Entry ID',
3057 ];
3058
3059 $result[] = [
3060 'value' => '{entry_source_url}',
3061 'label' => 'Entry Source URL',
3062 ];
3063
3064 $result[] = [
3065 'value' => '{submission_date}',
3066 'label' => 'Submission Date',
3067 ];
3068
3069 $result[] = [
3070 'value' => '{user_ip}',
3071 'label' => 'User IP',
3072 ];
3073
3074 return [
3075 'options' => $result,
3076 'hasMore' => false,
3077 ];
3078 }
3079
3080 /**
3081 * Search Gravity Form fields.
3082 *
3083 * @param array $data Search Params.
3084 *
3085 * @return array
3086 * @since 1.0.0
3087 */
3088 public function search_gform_fields( $data ) {
3089 if ( ! class_exists( 'RGFormsModel' ) || ! class_exists( 'GFCommon' ) ) {
3090 return [
3091 'options' => [],
3092 'hasMore' => false,
3093 ];
3094 }
3095 $result = [];
3096 $page = $data['page'];
3097 $form_id = absint( $data['dynamic'] );
3098
3099 $form = RGFormsModel::get_form_meta( $form_id );
3100
3101 if ( is_array( $form['fields'] ) ) {
3102 foreach ( $form['fields'] as $field ) {
3103 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
3104 foreach ( $field['inputs'] as $input ) {
3105 if ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
3106 $result[] = [
3107 'value' => $input['id'],
3108 'label' => GFCommon::get_label( $field, $input['id'] ),
3109 ];
3110 }
3111 }
3112 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
3113 $result[] = [
3114 'value' => (string) $field['id'],
3115 'label' => GFCommon::get_label( $field ),
3116 ];
3117 }
3118 }
3119 }
3120
3121 return [
3122 'options' => $result,
3123 'hasMore' => false,
3124 ];
3125
3126 }
3127
3128 /**
3129 * Search Gravity Form fields.
3130 *
3131 * @param array $data Search Params.
3132 *
3133 * @return array
3134 * @since 1.0.0
3135 */
3136 public function search_pluggable_gravity_form_fields( $data ) {
3137 if ( ! class_exists( 'RGFormsModel' ) || ! class_exists( 'GFCommon' ) ) {
3138 return [
3139 'options' => [],
3140 'hasMore' => false,
3141 ];
3142 }
3143 $result = [];
3144 $form_id = absint( $data['dynamic'] );
3145
3146 $form = RGFormsModel::get_form_meta( $form_id );
3147
3148 if ( is_array( $form['fields'] ) ) {
3149 foreach ( $form['fields'] as $field ) {
3150 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
3151 foreach ( $field['inputs'] as $input ) {
3152 if ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
3153 $result[] = [
3154 'value' => '{' . $input['id'] . '}',
3155 'label' => GFCommon::get_label( $field, $input['id'] ),
3156 ];
3157 }
3158 }
3159 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
3160 $result[] = [
3161 'value' => '{' . $field['id'] . '}',
3162 'label' => GFCommon::get_label( $field ),
3163 ];
3164 }
3165 }
3166 }
3167
3168 $result[] = [
3169 'value' => '{gravity_form}',
3170 'label' => 'Form ID',
3171 ];
3172 $result[] = [
3173 'value' => '{form_title}',
3174 'label' => 'Form Title',
3175 ];
3176 $result[] = [
3177 'value' => '{entry_id}',
3178 'label' => 'Entry ID',
3179 ];
3180 $result[] = [
3181 'value' => '{user_ip}',
3182 'label' => 'User IP',
3183 ];
3184 $result[] = [
3185 'value' => '{entry_source_url}',
3186 'label' => 'Entry Source URL',
3187 ];
3188 $result[] = [
3189 'value' => '{entry_submission_date}',
3190 'label' => 'Entry Submission Date',
3191 ];
3192
3193 return [
3194 'options' => $result,
3195 'hasMore' => false,
3196 ];
3197
3198 }
3199
3200 /**
3201 * Search Gravity Form fields for action.
3202 *
3203 * @param array $data data.
3204 * @return array
3205 */
3206 public function search_gravity_form_custom_fields( $data ) {
3207 $context = [];
3208 if ( ! class_exists( 'RGFormsModel' ) || ! class_exists( 'GFCommon' ) ) {
3209 return [];
3210 }
3211 $form_id = $data['filter'];
3212 $form = RGFormsModel::get_form_meta( $form_id );
3213 $custom_fields = [];
3214 if ( is_array( $form['fields'] ) ) {
3215 foreach ( $form['fields'] as $field ) {
3216 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
3217 foreach ( $field['inputs'] as $input ) {
3218 if ( ! $input['isHidden'] ) {
3219 $custom_fields[] = [
3220 'label' => GFCommon::get_label( $field, $input['id'] ),
3221 'value' => $input['id'],
3222 'type' => 'text',
3223 ];
3224 }
3225 }
3226 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
3227 $custom_fields[] = [
3228 'label' => GFCommon::get_label( $field ),
3229 'value' => $field['id'],
3230 'type' => 'text',
3231 ];
3232 }
3233 }
3234 }
3235 $context['fields'] = $custom_fields;
3236 return $context;
3237 }
3238
3239 /**
3240 * Get user register details via gravity forms.
3241 *
3242 * @param array $data data.
3243 *
3244 * @return array
3245 */
3246 public function search_pluggables_gravity_forms_user_register( $data ) {
3247 global $wpdb;
3248 $context = [];
3249 $pluggable_data = [];
3250
3251 $results = $wpdb->get_results(
3252 $wpdb->prepare(
3253 "SELECT id, form_id, date_created, ip, source_url, created_by FROM {$wpdb->prefix}gf_entry
3254 WHERE form_id = %d AND
3255 status = %s
3256 ORDER BY id
3257 DESC LIMIT 1",
3258 $data['filter']['gravity_form']['value'],
3259 'active'
3260 )
3261 );
3262
3263 if ( $results ) {
3264 $pluggable_data['gravity_form'] = (int) $results[0]->form_id;
3265 $pluggable_data['entry_id'] = $results[0]->id;
3266 $pluggable_data['user_ip'] = $results[0]->ip;
3267 $pluggable_data['entry_source_url'] = $results[0]->source_url;
3268 $pluggable_data['entry_submission_date'] = $results[0]->date_created;
3269 $pluggable_data['user'] = WordPress::get_user_context( $results[0]->created_by );
3270 $context['response_type'] = 'live';
3271 } else {
3272 $pluggable_data['gravity_form'] = '3';
3273 $pluggable_data['entry_id'] = '13';
3274 $pluggable_data['user_ip'] = '127.0.0.0';
3275 $pluggable_data['entry_source_url'] = 'https://example.com';
3276 $pluggable_data['entry_submission_date'] = '2024-02-05 09:41:59';
3277 $pluggable_data['user']['wp_user_id'] = '123';
3278 $pluggable_data['user']['user_login'] = 'johnd';
3279 $pluggable_data['user']['display_name'] = 'johnd';
3280 $pluggable_data['user']['user_firstname'] = 'John';
3281 $pluggable_data['user']['user_lastname'] = 'Doe';
3282 $pluggable_data['user']['user_email'] = 'johnd@yopmail.com';
3283 $pluggable_data['user']['user_role'] = [ 'subscriber' ];
3284 $context['response_type'] = 'sample';
3285 }
3286
3287 $context['pluggable_data'] = $pluggable_data;
3288 return $context;
3289 }
3290
3291 /**
3292 * Get payment form details for gravity forms.
3293 *
3294 * @param array $data data.
3295 *
3296 * @return array
3297 */
3298 public function search_pluggables_gravity_forms_payment( $data ) {
3299 global $wpdb;
3300 $context = [];
3301 $pluggable_data = [];
3302
3303 $results = $wpdb->get_results(
3304 $wpdb->prepare(
3305 "SELECT * FROM {$wpdb->prefix}gf_entry
3306 WHERE form_id = %d AND
3307 status = %s AND
3308 payment_status LIKE %s
3309 ORDER BY id
3310 DESC LIMIT 1",
3311 $data['filter']['gravity_form']['value'],
3312 'active',
3313 'Paid'
3314 )
3315 );
3316
3317 if ( $results ) {
3318 $pluggable_data['gravity_form'] = (int) $results[0]->form_id;
3319 $pluggable_data['entry_id'] = $results[0]->id;
3320 $pluggable_data['user_ip'] = $results[0]->ip;
3321 $pluggable_data['entry_source_url'] = $results[0]->source_url;
3322 $pluggable_data['entry_submission_date'] = $results[0]->date_created;
3323 $pluggable_data['payment_status'] = $results[0]->payment_status;
3324 $pluggable_data['payment_amount'] = $results[0]->payment_amount;
3325 $pluggable_data['currency'] = $results[0]->currency;
3326 $pluggable_data['payment_method'] = $results[0]->payment_method;
3327 $pluggable_data['transaction_id'] = $results[0]->transaction_id;
3328 $pluggable_data['user'] = WordPress::get_user_context( $results[0]->created_by );
3329 $context['response_type'] = 'live';
3330 } else {
3331 $pluggable_data['gravity_form'] = '3';
3332 $pluggable_data['entry_id'] = '13';
3333 $pluggable_data['user_ip'] = '127.0.0.0';
3334 $pluggable_data['entry_source_url'] = 'https://example.com';
3335 $pluggable_data['entry_submission_date'] = '2024-02-05 09:41:59';
3336 $pluggable_data['payment_status'] = 'Paid';
3337 $pluggable_data['payment_amount'] = '10.00';
3338 $pluggable_data['currency'] = 'USD';
3339 $pluggable_data['payment_method'] = 'visa';
3340 $pluggable_data['transaction_id'] = 'st_ooi98';
3341 $pluggable_data['user']['wp_user_id'] = '123';
3342 $pluggable_data['user']['user_login'] = 'johnd';
3343 $pluggable_data['user']['display_name'] = 'johnd';
3344 $pluggable_data['user']['user_firstname'] = 'John';
3345 $pluggable_data['user']['user_lastname'] = 'Doe';
3346 $pluggable_data['user']['user_email'] = 'johnd@yopmail.com';
3347 $pluggable_data['user']['user_role'] = [ 'subscriber' ];
3348 $context['response_type'] = 'sample';
3349 }
3350
3351 $context['pluggable_data'] = $pluggable_data;
3352 return $context;
3353 }
3354
3355 /**
3356 * Prepare fluentcrm lists.
3357 *
3358 * @param array $data Search Params.
3359 *
3360 * @return array
3361 */
3362 public function search_fluentcrm_lists( $data ) {
3363 if ( ! function_exists( 'FluentCrmApi' ) ) {
3364 return [];
3365 }
3366 $list_api = FluentCrmApi( 'lists' );
3367 $all_lists = $list_api->all();
3368 $options = [];
3369
3370 if ( ! empty( $all_lists ) ) {
3371 foreach ( $all_lists as $list ) {
3372 $options[] = [
3373 'label' => $list->title,
3374 'value' => $list->id,
3375 ];
3376 }
3377 }
3378
3379 return [
3380 'options' => $options,
3381 'hasMore' => false,
3382 ];
3383 }
3384
3385 /**
3386 * Prepare fluentcrm segments.
3387 *
3388 * @param array $data Search Params.
3389 *
3390 * @return array
3391 */
3392 public function search_fluentcrm_segments( $data ) {
3393 global $wpdb;
3394 $options = [];
3395
3396 $table = $wpdb->prefix . 'fc_meta';
3397
3398 $segments = $wpdb->get_results(
3399 $wpdb->prepare(
3400 'SELECT id, value FROM ' . $wpdb->prefix . 'fc_meta WHERE object_type = %s',
3401 'custom_segment'
3402 )
3403 );
3404
3405 if ( is_array( $segments ) && ! empty( $segments ) ) {
3406 foreach ( $segments as $segment ) {
3407 $raw_value = is_string( $segment->value ) ? $segment->value : '';
3408 $segment_data = maybe_unserialize( $raw_value );
3409
3410 if ( is_array( $segment_data ) && isset( $segment_data['title'] ) ) {
3411 $options[] = [
3412 'label' => sanitize_text_field( $segment_data['title'] ),
3413 'value' => intval( $segment->id ),
3414 ];
3415 }
3416 }
3417 }
3418
3419 return [
3420 'options' => $options,
3421 'hasMore' => false,
3422 ];
3423 }
3424
3425 /**
3426 * Prepare fluentcrm contact status.
3427 *
3428 * @param array $data Search Params.
3429 *
3430 * @return array
3431 */
3432 public function search_fluentcrm_contact_status( $data ) {
3433
3434 $options = [
3435 [
3436 'label' => __( 'Subscribed', 'suretriggers' ),
3437 'value' => 'subscribed',
3438 ],
3439 [
3440 'label' => __( 'Pending', 'suretriggers' ),
3441 'value' => 'pending',
3442 ],
3443 [
3444 'label' => __( 'Unsubscribed', 'suretriggers' ),
3445 'value' => 'unsubscribed',
3446 ],
3447 [
3448 'label' => __( 'Bounced', 'suretriggers' ),
3449 'value' => 'bounced',
3450 ],
3451 [
3452 'label' => __( 'Complained', 'suretriggers' ),
3453 'value' => 'complained',
3454 ],
3455 ];
3456
3457 return [
3458 'options' => $options,
3459 'hasMore' => false,
3460 ];
3461 }
3462
3463 /**
3464 * Prepare fluentcrm contact status.
3465 *
3466 * @param array $data Search Params.
3467 *
3468 * @return array
3469 */
3470 public function search_fluentcrm_fetch_custom_fields( $data ) {
3471
3472 $options = [
3473 [
3474 'label' => __( 'Yes', 'suretriggers' ),
3475 'value' => 'true',
3476 ],
3477 [
3478 'label' => __( 'No', 'suretriggers' ),
3479 'value' => 'false',
3480 ],
3481 ];
3482
3483 return [
3484 'options' => $options,
3485 'hasMore' => false,
3486 ];
3487 }
3488
3489 /**
3490 * Prepare fluentcrm tags.
3491 *
3492 * @param array $data Search Params.
3493 *
3494 * @return array
3495 */
3496 public function search_fluentcrm_tags( $data ) {
3497
3498 if ( ! function_exists( 'FluentCrmApi' ) ) {
3499 return [];
3500 }
3501
3502 $tag_api = FluentCrmApi( 'tags' );
3503 $all_tags = $tag_api->all();
3504 $options = [];
3505
3506 if ( ! empty( $all_tags ) ) {
3507 foreach ( $all_tags as $tag ) {
3508 $options[] = [
3509 'label' => $tag->title,
3510 'value' => $tag->id,
3511 ];
3512 }
3513 }
3514
3515 return [
3516 'options' => $options,
3517 'hasMore' => false,
3518 ];
3519 }
3520
3521 /**
3522 * Prepare fluentcrm templates.
3523 *
3524 * @param array $data Search Params.
3525 *
3526 * @return array
3527 */
3528 public function search_fluentcrm_templates( $data ) {
3529 if ( ! class_exists( '\FluentCrm\App\Models\Template' ) ) {
3530 return [
3531 'options' => [],
3532 'hasMore' => false,
3533 ];
3534 }
3535
3536 $options = [];
3537 $templates = \FluentCrm\App\Models\Template::emailTemplates( [ 'publish', 'draft' ] )->orderBy( 'ID', 'desc' )->get();
3538
3539 foreach ( $templates as $template ) {
3540 $options[] = [
3541 'label' => $template->post_title,
3542 'value' => $template->ID,
3543 'email_subject' => get_post_meta( $template->ID, '_email_subject', true ),
3544 'design_template' => get_post_meta( $template->ID, '_design_template', true ),
3545 ];
3546 }
3547
3548 return [
3549 'options' => $options,
3550 'hasMore' => false,
3551 ];
3552 }
3553
3554 /**
3555 * Prepare fluentcrm sequences.
3556 *
3557 * @param array $data Search Params.
3558 *
3559 * @return array
3560 */
3561 public function search_fluentcrm_sequences( $data ) {
3562 if ( ! class_exists( '\FluentCampaign\App\Models\Sequence' ) ) {
3563 return [
3564 'options' => [],
3565 'hasMore' => false,
3566 ];
3567 }
3568
3569 $options = [];
3570 $sequences = \FluentCampaign\App\Models\Sequence::orderBy( 'id', 'DESC' )->get();
3571
3572 foreach ( $sequences as $sequence ) {
3573 $options[] = [
3574 'label' => $sequence->title,
3575 'value' => $sequence->id,
3576 ];
3577 }
3578
3579 return [
3580 'options' => $options,
3581 'hasMore' => false,
3582 ];
3583 }
3584
3585 /**
3586 * Prepare JetpackCRM Contact tags.
3587 *
3588 * @param array $data Search Params.
3589 *
3590 * @return array
3591 */
3592 public function search_jetpack_crm_contact_tags( $data ) {
3593
3594 if ( ! function_exists( 'zeroBSCRM_getCustomerTags' ) ) {
3595 return [];
3596 }
3597
3598 $all_tags = zeroBSCRM_getCustomerTags();
3599 $options = [];
3600
3601 if ( ! empty( $all_tags ) ) {
3602 foreach ( $all_tags as $tag ) {
3603 $options[] = [
3604 'label' => $tag['name'],
3605 'value' => $tag['id'],
3606 ];
3607 }
3608 }
3609
3610 return [
3611 'options' => $options,
3612 'hasMore' => false,
3613 ];
3614 }
3615
3616 /**
3617 * Prepare JetpackCRM Company tags.
3618 *
3619 * @param array $data Search Params.
3620 *
3621 * @return array
3622 */
3623 public function search_jetpack_crm_company_tags( $data ) {
3624
3625 if ( ! defined( 'ZBS_TYPE_COMPANY' ) ) {
3626 return [];
3627 }
3628
3629 global $wpdb;
3630 $all_tags = $wpdb->get_results( $wpdb->prepare( "SELECT `ID`,`zbstag_name` FROM `{$wpdb->prefix}zbs_tags` WHERE zbstag_objtype = %d", ZBS_TYPE_COMPANY ) );
3631
3632 $options = [];
3633 if ( ! empty( $all_tags ) ) {
3634 foreach ( $all_tags as $tag ) {
3635 $options[] = [
3636 'label' => $tag->zbstag_name,
3637 'value' => $tag->ID,
3638 ];
3639 }
3640 }
3641
3642 return [
3643 'options' => $options,
3644 'hasMore' => false,
3645 ];
3646 }
3647
3648 /**
3649 * Prepare JetpackCRM Companies list.
3650 *
3651 * @param array $data Search Params.
3652 *
3653 * @return array
3654 */
3655 public function search_jetpack_crm_companies_list( $data ) {
3656
3657 if ( ! function_exists( 'zeroBS_getCompanies' ) ) {
3658 return [];
3659 }
3660
3661 $all_companies = zeroBS_getCompanies();
3662 $options = [];
3663
3664 if ( ! empty( $all_companies ) ) {
3665 foreach ( $all_companies as $company ) {
3666 $options[] = [
3667 'label' => $company['name'],
3668 'value' => $company['id'],
3669 ];
3670 }
3671 }
3672
3673 return [
3674 'options' => $options,
3675 'hasMore' => false,
3676 ];
3677 }
3678
3679 /**
3680 * Prepare JetpackCRM contact status.
3681 *
3682 * @param array $data Search Params.
3683 *
3684 * @return array
3685 */
3686 public function search_jetpack_crm_contact_statuses( $data ) {
3687
3688 $options = [
3689 [
3690 'label' => __( 'Lead', 'suretriggers' ),
3691 'value' => 'Lead',
3692 ],
3693 [
3694 'label' => __( 'Customer', 'suretriggers' ),
3695 'value' => 'Customer',
3696 ],
3697 [
3698 'label' => __( 'Refused', 'suretriggers' ),
3699 'value' => 'Refused',
3700 ],
3701 [
3702 'label' => __( 'Blacklisted', 'suretriggers' ),
3703 'value' => 'Blacklisted',
3704 ],
3705 [
3706 'label' => __( 'Cancelled by Customer', 'suretriggers' ),
3707 'value' => 'Cancelled by Customer',
3708 ],
3709 [
3710 'label' => __( 'Cancelled by Us (Pre-Quote)', 'suretriggers' ),
3711 'value' => 'Cancelled by Us (Pre-Quote)',
3712 ],
3713 [
3714 'label' => __( 'Cancelled by Us (Post-Quote)', 'suretriggers' ),
3715 'value' => 'Cancelled by Us (Post-Quote)',
3716 ],
3717 ];
3718
3719 return [
3720 'options' => $options,
3721 'hasMore' => false,
3722 ];
3723 }
3724
3725 /**
3726 * Prepare FunnelKit Automations' lists.
3727 *
3728 * @param array $data Search Params.
3729 *
3730 * @return array
3731 */
3732 public function search_funnel_kit_automations_lists( $data ) {
3733
3734 if ( ! class_exists( 'BWFCRM_Lists' ) ) {
3735 return [];
3736 }
3737
3738 $bwfcrm_lists = \BWFCRM_Lists::get_lists();
3739
3740 $options = [];
3741
3742 foreach ( $bwfcrm_lists as $list ) {
3743 $options[] = [
3744 'label' => $list['name'],
3745 'value' => $list['ID'],
3746 ];
3747 }
3748
3749 return [
3750 'options' => $options,
3751 'hasMore' => false,
3752 ];
3753 }
3754
3755 /**
3756 * Prepare FunnelKit Automations' tags.
3757 *
3758 * @param array $data Search Params.
3759 *
3760 * @return array
3761 */
3762 public function search_funnel_kit_automations_tags( $data ) {
3763
3764 if ( ! class_exists( 'BWFCRM_Tag' ) ) {
3765 return [];
3766 }
3767
3768 $bwfcrm_tags = \BWFCRM_Tag::get_tags();
3769
3770 $options = [];
3771
3772 foreach ( $bwfcrm_tags as $tag ) {
3773 $options[] = [
3774 'label' => $tag['name'],
3775 'value' => $tag['ID'],
3776 ];
3777 }
3778
3779 return [
3780 'options' => $options,
3781 'hasMore' => false,
3782 ];
3783 }
3784
3785 /**
3786 * Prepare Wishlist Memberlists level.
3787 *
3788 * @param array $data Search Params.
3789 *
3790 * @return array
3791 */
3792 public function search_wishlistmember_lists( $data ) {
3793 if ( ! function_exists( 'wlmapi_get_levels' ) ) {
3794 return [];
3795 }
3796 $wlm_levels = wlmapi_get_levels();
3797 $options = [];
3798
3799 if ( ! empty( $wlm_levels ) ) {
3800 foreach ( $wlm_levels['levels']['level'] as $list ) {
3801 if ( isset( $list['name'] ) && ! empty( $list['name'] ) ) {
3802 $options[] = [
3803 'label' => $list['name'],
3804 'value' => (string) $list['id'],
3805 ];
3806 }
3807 }
3808 }
3809
3810 return [
3811 'options' => $options,
3812 'hasMore' => false,
3813 ];
3814 }
3815
3816 /**
3817 * Prepare Wishlist Memberlists members.
3818 *
3819 * @param array $data Search Params.
3820 *
3821 * @return array<string, array<int<0, max>, array<string, mixed>>|false>
3822 */
3823 public function search_wishlistmember_members( $data ) {
3824
3825 if ( ! function_exists( 'wlmapi_get_members' ) ) {
3826 return [];
3827 }
3828
3829 $wlm_members = wlmapi_get_members();
3830 $options = [];
3831
3832 if ( ! empty( $wlm_members ) ) {
3833 foreach ( $wlm_members['members']['member'] as $list ) {
3834 if ( isset( $list['user_email'] ) && ! empty( $list['user_email'] ) ) {
3835 $options[] = [
3836 'label' => $list['user_email'],
3837 'value' => (string) $list['id'],
3838 ];
3839 }
3840 }
3841 }
3842
3843 return [
3844 'options' => $options,
3845 'hasMore' => false,
3846 ];
3847 }
3848
3849 /**
3850 * Prepare elementor popups.
3851 *
3852 * @param array $data Search Params.
3853 *
3854 * @return array
3855 */
3856 public function search_elementor_popups( $data ) {
3857
3858 $posts = get_posts(
3859 [
3860 'post_type' => 'elementor_library',
3861 'orderby' => 'title',
3862 'order' => 'ASC',
3863 'post_status' => 'publish',
3864 'meta_query' => [
3865 [
3866 'key' => '_elementor_template_type',
3867 'value' => 'popup',
3868 'compare' => '=',
3869 ],
3870 ],
3871 ]
3872 );
3873
3874 $options = [];
3875 if ( ! empty( $posts ) ) {
3876 foreach ( $posts as $post ) {
3877 $options[] = [
3878 'label' => $post->post_title,
3879 'value' => $post->ID,
3880 ];
3881 }
3882 }
3883
3884 return [
3885 'options' => $options,
3886 'hasMore' => false,
3887 ];
3888 }
3889
3890 /**
3891 * Prepare givewp forms.
3892 *
3893 * @param array $data Search Params.
3894 *
3895 * @return array
3896 */
3897 public function search_givewp_forms( $data ) {
3898
3899 $page = $data['page'];
3900 $limit = Utilities::get_search_page_limit();
3901 $offset = $limit * ( $page - 1 );
3902
3903 $posts = get_posts(
3904 [
3905 'post_type' => 'give_forms',
3906 'orderby' => 'title',
3907 'order' => 'ASC',
3908 'post_status' => 'publish',
3909 'posts_per_page' => $limit,
3910 'offset' => $offset,
3911 ]
3912 );
3913
3914 $options = [];
3915 if ( ! empty( $posts ) ) {
3916 foreach ( $posts as $post ) {
3917 $options[] = [
3918 'label' => $post->post_title,
3919 'value' => $post->ID,
3920 ];
3921 }
3922 }
3923
3924 $count = wp_count_posts( 'give_forms' )->publish;
3925
3926 return [
3927 'options' => $options,
3928 'hasMore' => $count > $limit && $count > $offset,
3929 ];
3930 }
3931
3932 /**
3933 * Prepare givewp recurring forms.
3934 *
3935 * @param array $data Search Params.
3936 *
3937 * @return array
3938 */
3939 public function search_givewp_recurring_forms( $data ) {
3940
3941 global $wpdb;
3942 $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 );
3943
3944 $options = [];
3945 if ( ! empty( $recurring ) ) {
3946 foreach ( $recurring as $form_id ) {
3947 $post_status = get_post_status( $form_id['form_id'] );
3948 if ( 'trash' !== $post_status ) {
3949 $options[] = [
3950 'label' => get_the_title( $form_id['form_id'] ),
3951 'value' => $form_id['form_id'],
3952 ];
3953 }
3954 }
3955 }
3956
3957 return [
3958 'options' => $options,
3959 'hasMore' => false,
3960 ];
3961 }
3962
3963 /**
3964 * Prepare givewp forms.
3965 *
3966 * @param array $data Search Params.
3967 *
3968 * @return array
3969 */
3970 public function search_givewp_form_fields( $data ) {
3971 $options = [
3972 [
3973 'label' => 'First Name',
3974 'value' => '_give_donor_billing_first_name',
3975 ],
3976 [
3977 'label' => 'Last Name',
3978 'value' => '_give_donor_billing_last_name',
3979 ],
3980 [
3981 'label' => 'Email',
3982 'value' => '_give_payment_donor_email',
3983 ],
3984 [
3985 'label' => 'Donation Amount',
3986 'value' => '_give_payment_total',
3987 ],
3988 [
3989 'label' => 'Currency',
3990 'value' => '_give_payment_currency',
3991 ],
3992 [
3993 'label' => 'Comment',
3994 'value' => '_give_donation_comment',
3995 ],
3996 [
3997 'label' => 'Zip',
3998 'value' => '_give_donor_billing_zip',
3999 ],
4000 [
4001 'label' => 'Country',
4002 'value' => '_give_donor_billing_country',
4003 ],
4004 [
4005 'label' => 'Address 1',
4006 'value' => '_give_donor_billing_address1',
4007 ],
4008 [
4009 'label' => 'Address 2',
4010 'value' => '_give_donor_billing_address2',
4011 ],
4012 [
4013 'label' => 'City',
4014 'value' => '_give_donor_billing_city',
4015 ],
4016 [
4017 'label' => 'State',
4018 'value' => '_give_donor_billing_state',
4019 ],
4020 ];
4021
4022 if ( class_exists( '\Give_FFM_Render_Form' ) ) {
4023 $form_id = $data['dynamic'];
4024 $custom_form_fields = [];
4025 if ( class_exists( '\GiveFormFieldManager\Helpers\Form' ) ) {
4026 $custom_form_fields = \GiveFormFieldManager\Helpers\Form::get_input_fields( $form_id );
4027 }
4028
4029 if ( ! empty( $custom_form_fields ) ) {
4030 if ( ! empty( $custom_form_fields[2] ) && is_array( $custom_form_fields[2] ) ) {
4031 foreach ( $custom_form_fields[2] as $custom_form_field ) {
4032 $custom_form_field['required'] = ( 'no' === $custom_form_field['required'] ) ? false : true;
4033 $options[] = [
4034 'label' => $custom_form_field['label'],
4035 'value' => $custom_form_field['name'],
4036 ];
4037 }
4038 }
4039 }
4040 }
4041
4042 return [
4043 'options' => $options,
4044 'hasMore' => false,
4045 ];
4046 }
4047
4048 /**
4049 * Prepare buddyboss group users.
4050 *
4051 * @param array $data Search Params.
4052 *
4053 * @return array
4054 */
4055 public function search_bb_group_users( $data ) {
4056 $options = [];
4057 if ( ! function_exists( 'groups_get_group_members' ) || ! function_exists( 'groups_get_group_admins' ) ) {
4058 return [];
4059 }
4060 $group_id = $data['dynamic'];
4061 $admins = groups_get_group_admins( $group_id );
4062
4063 if ( ! empty( $admins ) ) {
4064 foreach ( $admins as $admin ) {
4065 $admin_user = get_user_by( 'id', $admin->user_id );
4066 if ( $admin_user instanceof \WP_User ) {
4067 $options[] = [
4068 'label' => $admin_user->display_name,
4069 'value' => $admin_user->ID,
4070 ];
4071 }
4072 }
4073 }
4074
4075 $members = groups_get_group_members( [ 'group_id' => $group_id ] );
4076
4077 if ( isset( $members['members'] ) && ! empty( $members['members'] ) ) {
4078 foreach ( $members['members'] as $member ) {
4079 $options[] = [
4080 'label' => $member->display_name,
4081 'value' => $member->ID,
4082 ];
4083 }
4084 }
4085 return [
4086 'options' => $options,
4087 'hasMore' => false,
4088 ];
4089 }
4090
4091 /**
4092 * Prepare buddyboss groups.
4093 *
4094 * @param array $data Search Params.
4095 *
4096 * @return array
4097 */
4098 public function search_buddyboss_groups( $data ) {
4099 global $wpdb;
4100
4101 $options = [];
4102 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
4103 if ( ! empty( $groups ) ) {
4104 foreach ( $groups as $group ) {
4105 $options[] = [
4106 'label' => $group->name,
4107 'value' => $group->id,
4108 ];
4109 }
4110 }
4111 return [
4112 'options' => $options,
4113 'hasMore' => false,
4114 ];
4115 }
4116
4117 /**
4118 * Prepare buddyboss public groups.
4119 *
4120 * @param array $data Search Params.
4121 *
4122 * @return array
4123 */
4124 public function search_buddyboss_public_groups( $data ) {
4125 $options = [];
4126 if ( ! function_exists( 'groups_get_groups' ) ) {
4127 return [];
4128 }
4129 $groups = groups_get_groups();
4130 if ( isset( $groups['groups'] ) && ! empty( $groups['groups'] ) ) {
4131 foreach ( $groups['groups'] as $group ) {
4132 if ( 'public' === $group->status ) {
4133 $options[] = [
4134 'label' => $group->name,
4135 'value' => $group->id,
4136 ];
4137 }
4138 }
4139 }
4140 return [
4141 'options' => $options,
4142 'hasMore' => false,
4143 ];
4144 }
4145
4146 /**
4147 * Prepare buddyboss private groups.
4148 *
4149 * @param array $data Search Params.
4150 *
4151 * @return array<string, array<int, array<string, mixed>>|false>
4152 */
4153 public function search_buddyboss_private_groups( $data ) {
4154 global $wpdb;
4155
4156 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'private'" );
4157
4158 $options = [];
4159 if ( $groups ) {
4160 foreach ( $groups as $group ) {
4161 $options[] = [
4162 'label' => $group->name,
4163 'value' => $group->id,
4164 ];
4165 }
4166 }
4167
4168 return [
4169 'options' => $options,
4170 'hasMore' => false,
4171 ];
4172 }
4173
4174 /**
4175 * Prepare buddyboss profile types list.
4176 *
4177 * @param array $data Search Params.
4178 *
4179 * @return array[]
4180 */
4181 public function search_bb_profile_type_list( $data ) {
4182 $options = [];
4183
4184 if ( function_exists( 'bp_get_active_member_types' ) ) {
4185 $types = bp_get_active_member_types(
4186 [
4187 'fields' => '*',
4188 ]
4189 );
4190 if ( $types ) {
4191 foreach ( $types as $type ) {
4192 $options[] = [
4193 'label' => $type->post_title,
4194 'value' => $type->ID,
4195 ];
4196 }
4197 }
4198 }
4199
4200 /**
4201 *
4202 * Ignore line
4203 *
4204 * @phpstan-ignore-next-line
4205 */
4206 return [
4207 'options' => $options,
4208 'hasMore' => false,
4209 ];
4210 }
4211
4212 /**
4213 * Prepare buddyboss public groups.
4214 *
4215 * @param array $data Search Params.
4216 *
4217 * @return array
4218 */
4219 public function search_buddyboss_group_type( $data ) {
4220 $options = [];
4221
4222 if ( ! function_exists( 'bp_groups_get_group_types' ) ) {
4223 return [];
4224 }
4225
4226 $registered_types = bp_groups_get_group_types();
4227 if ( ! empty( $registered_types ) ) {
4228 foreach ( $registered_types as $key => $type ) {
4229 $options[] = [
4230 'label' => $type,
4231 'value' => $key,
4232 ];
4233 }
4234 }
4235 return [
4236 'options' => $options,
4237 'hasMore' => false,
4238 ];
4239 }
4240
4241 /**
4242 * Get BuddyBoss Forum Topic Status.
4243 *
4244 * @param array $data data.
4245 *
4246 * @return array|void
4247 */
4248 public function search_bb_forum_topic_status( $data ) {
4249 $options = [];
4250
4251 if ( ! function_exists( 'bbp_get_topic_post_type' ) ) {
4252 return [];
4253 }
4254
4255 $page = $data['page'];
4256 $limit = Utilities::get_search_page_limit();
4257 $offset = $limit * ( $page - 1 );
4258
4259 $forum_args = [
4260 'post_type' => bbp_get_topic_post_type(),
4261 'orderby' => 'title',
4262 'order' => 'ASC',
4263 'post_status' => [ 'publish', 'private' ],
4264 'posts_per_page' => $limit,
4265 'offset' => $offset,
4266 ];
4267
4268 $forums = get_posts( $forum_args );
4269 if ( ! empty( $forums ) ) {
4270 foreach ( $forums as $key => $forum ) {
4271 $options[] = [
4272 'label' => $forum,
4273 'value' => $key,
4274 ];
4275 }
4276 }
4277 $count = count( $options );
4278 return [
4279 'options' => $options,
4280 'hasMore' => $count > $limit && $count > $offset,
4281 ];
4282 }
4283
4284 /**
4285 * Get BuddyBoss Forums List.
4286 *
4287 * @param array $data data.
4288 *
4289 * @return array|void
4290 */
4291 public function search_bb_forums_list( $data ) {
4292 $options = [];
4293
4294 $page = $data['page'];
4295 $limit = Utilities::get_search_page_limit();
4296 $offset = $limit * ( $page - 1 );
4297
4298 if ( ! function_exists( 'bbp_get_forum_post_type' ) ) {
4299 return [];
4300 }
4301
4302 $forum_args = [
4303 'post_type' => bbp_get_forum_post_type(),
4304 'orderby' => 'title',
4305 'order' => 'ASC',
4306 'post_status' => [ 'publish', 'private' ],
4307 'posts_per_page' => $limit,
4308 'offset' => $offset,
4309 ];
4310
4311 $forums = get_posts( $forum_args );
4312 if ( ! empty( $forums ) ) {
4313 foreach ( $forums as $key => $forum ) {
4314 $options[] = [
4315 'label' => $forum->post_title,
4316 'value' => $forum->ID,
4317 ];
4318 }
4319 }
4320 $count = count( $options );
4321 return [
4322 'options' => $options,
4323 'hasMore' => $count > $limit && $count > $offset,
4324 ];
4325 }
4326
4327 /**
4328 * Get BuddyBoss Forums List.
4329 *
4330 * @param array $data data.
4331 *
4332 * @return array|void
4333 */
4334 public function search_bb_topics_list( $data ) {
4335 $options = [];
4336
4337 $page = $data['page'];
4338 $limit = Utilities::get_search_page_limit();
4339 $offset = $limit * ( $page - 1 );
4340
4341 if ( ! function_exists( 'bbp_get_topic_post_type' ) ) {
4342 return [];
4343 }
4344
4345 $topic_args = [
4346 'post_type' => bbp_get_topic_post_type(),
4347 'post_parent' => $data['dynamic'],
4348 'orderby' => 'title',
4349 'order' => 'ASC',
4350 'post_status' => [ 'publish', 'private' ],
4351 'posts_per_page' => $limit,
4352 'offset' => $offset,
4353 ];
4354
4355 $topics = get_posts( $topic_args );
4356 if ( ! empty( $topics ) ) {
4357 foreach ( $topics as $key => $topic ) {
4358 $options[] = [
4359 'label' => $topic->post_title,
4360 'value' => $topic->ID,
4361 ];
4362 }
4363 }
4364 $count = count( $options );
4365 return [
4366 'options' => $options,
4367 'hasMore' => $count > $limit && $count > $offset,
4368 ];
4369 }
4370
4371 /**
4372 * Prepare elementor forms.
4373 *
4374 * @param array $data Search Params.
4375 *
4376 * @return array
4377 */
4378 public function search_elementor_forms( $data ) {
4379
4380 $elementor_forms = Utilities::get_elementor_forms();
4381
4382 $options = [];
4383 if ( ! empty( $elementor_forms ) ) {
4384 foreach ( $elementor_forms as $key => $value ) {
4385 $options[] = [
4386 'label' => $value,
4387 'value' => $key,
4388 ];
4389 }
4390 }
4391
4392 return [
4393 'options' => $options,
4394 'hasMore' => false,
4395 ];
4396 }
4397
4398 /**
4399 * Prepare elementor forms.
4400 *
4401 * @param array $data Search Params.
4402 *
4403 * @return array[]
4404 */
4405 public function search_new_elementor_forms( $data ) {
4406
4407 global $wpdb;
4408 $elementor_forms = [];
4409 $post_metas = $wpdb->get_results(
4410 $wpdb->prepare(
4411 "SELECT pm.post_id, pm.meta_value
4412 FROM $wpdb->postmeta pm
4413 LEFT JOIN $wpdb->posts p
4414 ON p.ID = pm.post_id
4415 WHERE p.post_type IS NOT NULL
4416 AND p.post_status = %s
4417 AND pm.meta_key = %s
4418 AND pm.`meta_value` LIKE %s",
4419 'publish',
4420 '_elementor_data',
4421 '%%form_fields%%'
4422 )
4423 );
4424
4425 if ( ! empty( $post_metas ) ) {
4426 foreach ( $post_metas as $post_meta ) {
4427 /**
4428 *
4429 * Ignore line
4430 *
4431 * @phpstan-ignore-next-line
4432 */
4433 $inner_forms = Utilities::search_elementor_forms( json_decode( $post_meta->meta_value ) );
4434 if ( ! empty( $inner_forms ) ) {
4435 foreach ( $inner_forms as $form ) {
4436 /**
4437 *
4438 * Ignore line
4439 *
4440 * @phpstan-ignore-next-line
4441 */
4442 $elementor_forms[ $post_meta->post_id . '_' . $form->id ] = $form->settings->form_name . ' (' . $form->id . ')';
4443 }
4444 }
4445 }
4446 }
4447
4448 $options = [];
4449 if ( ! empty( $elementor_forms ) ) {
4450 foreach ( $elementor_forms as $key => $value ) {
4451 $options[] = [
4452 'label' => $value,
4453 'value' => $key,
4454 ];
4455 }
4456 }
4457
4458 /**
4459 *
4460 * Ignore line
4461 *
4462 * @phpstan-ignore-next-line
4463 */
4464 return [
4465 'options' => $options,
4466 'hasMore' => false,
4467 ];
4468 }
4469
4470 /**
4471 * Prepare elementor form fields.
4472 *
4473 * @param array $data Search Params.
4474 *
4475 * @return array
4476 */
4477 public function search_pluggable_elementor_form_fields( $data ) {
4478 $result = [];
4479 $form_id = absint( $data['dynamic'] );
4480 $elementor_form_fields = ( new Utilities() )->get_elementor_form_fields( $data );
4481 $options = [];
4482 if ( ! empty( $elementor_form_fields ) ) {
4483 foreach ( $elementor_form_fields as $key => $value ) {
4484 $options[] = [
4485 'label' => $value,
4486 'value' => '{' . $key . '}',
4487 ];
4488 }
4489 }
4490
4491 return [
4492 'options' => $options,
4493 'hasMore' => false,
4494 ];
4495 }
4496
4497 /**
4498 * Get all events
4499 *
4500 * @param array $data Data array.
4501 *
4502 * @return array
4503 */
4504 public function search_event_calendar_event( $data ) {
4505 $page = $data['page'];
4506 $limit = Utilities::get_search_page_limit();
4507 $offset = $limit * ( $page - 1 );
4508
4509 $posts = get_posts(
4510 [
4511 'post_type' => 'tribe_events',
4512 'orderby' => 'title',
4513 'order' => 'ASC',
4514 'post_status' => 'publish',
4515 'posts_per_page' => $limit,
4516 'offset' => $offset,
4517 ]
4518 );
4519
4520 $options = [];
4521 if ( ! empty( $posts ) ) {
4522 foreach ( $posts as $post ) {
4523 $options[] = [
4524 'label' => $post->post_title,
4525 'value' => $post->ID,
4526 ];
4527 }
4528 }
4529
4530 $count = wp_count_posts( 'tribe_events' )->publish;
4531
4532 return [
4533 'options' => $options,
4534 'hasMore' => $count > $limit && $count > $offset,
4535 ];
4536 }
4537
4538 /**
4539 * Get all events tickets
4540 *
4541 * @param array $data Data array.
4542 *
4543 * @return array
4544 */
4545 public function search_event_calendar_event_tickets( $data ) {
4546 $event_id = $data['dynamic'];
4547 $options = [];
4548
4549 if ( ! class_exists( 'Tribe__Tickets_Plus__Commerce__WooCommerce__Main' ) ) {
4550 return [];
4551 }
4552
4553 $tickets = \Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance()->get_all_event_tickets( $event_id );
4554 if ( ! empty( $tickets ) ) {
4555 foreach ( $tickets as $ticket_object ) {
4556 $options[] = [
4557 'label' => $ticket_object->name,
4558 'value' => $ticket_object->ID,
4559 ];
4560 }
4561 }
4562
4563 return [
4564 'options' => $options,
4565 'hasMore' => false,
4566 ];
4567 }
4568
4569 /**
4570 * Prepare rsvp event calendar events.
4571 *
4572 * @param array $data Search Params.
4573 *
4574 * @return array
4575 */
4576 public function search_event_calendar_rsvp_event( $data ) {
4577
4578 if ( ! class_exists( 'Tribe__Tickets__Tickets_Handler' ) ) {
4579 return [];
4580 }
4581 $page = $data['page'];
4582 $limit = Utilities::get_search_page_limit();
4583 $offset = $limit * ( $page - 1 );
4584
4585 $posts = get_posts(
4586 [
4587 'post_type' => 'tribe_events',
4588 'orderby' => 'title',
4589 'order' => 'ASC',
4590 'post_status' => 'publish',
4591 'posts_per_page' => $limit,
4592 'offset' => $offset,
4593 ]
4594 );
4595
4596 $options = [];
4597 if ( ! empty( $posts ) ) {
4598 $ticket_handler = new Tribe__Tickets__Tickets_Handler();
4599 foreach ( $posts as $post ) {
4600
4601 $get_rsvp_ticket = $ticket_handler->get_event_rsvp_tickets( $post->ID );
4602
4603 if ( ! empty( $get_rsvp_ticket ) ) {
4604 $options[] = [
4605 'label' => $post->post_title,
4606 'value' => $post->ID,
4607 ];
4608 }
4609 }
4610 }
4611 $count = wp_count_posts( 'tribe_events' )->publish;
4612
4613 return [
4614 'options' => $options,
4615 'hasMore' => $count > $limit && $count > $offset,
4616 ];
4617 }
4618
4619 /**
4620 * Prepare Restrict Content Membership Level.
4621 *
4622 * @param array $data Search Params.
4623 *
4624 * @return array
4625 */
4626 public function search_restrictcontent_membership_level( $data ) {
4627 if ( ! function_exists( 'rcp_get_membership_levels' ) ) {
4628 return [];
4629 }
4630 $rcp_memberships = rcp_get_membership_levels();
4631 $options = [];
4632
4633 if ( ! empty( $rcp_memberships ) ) {
4634 foreach ( $rcp_memberships as $list ) {
4635 $options[] = [
4636 'label' => ucfirst( $list->name ),
4637 'value' => $list->id,
4638 ];
4639 }
4640 }
4641
4642 return [
4643 'options' => $options,
4644 'hasMore' => false,
4645 ];
4646 }
4647
4648 /**
4649 * Prepare Restrict Content Customer.
4650 *
4651 * @param array $data Search Params.
4652 *
4653 * @return array
4654 */
4655 public function search_restrictcontent_customer( $data ) {
4656 if ( ! function_exists( 'rcp_get_memberships' ) ) {
4657 return [];
4658 }
4659 $rcp_users = rcp_get_memberships();
4660 $options = [];
4661
4662 if ( ! empty( $rcp_users ) ) {
4663 foreach ( $rcp_users as $list ) {
4664 $user = get_user_by( 'ID', $list->get_user_id() );
4665 if ( $user instanceof \WP_User ) {
4666 $user_label = $user->user_email;
4667
4668 if ( $user->display_name !== $user->user_email ) {
4669 $user_label .= ' (' . $user->display_name . ')';
4670 }
4671
4672 $options[] = [
4673 'label' => $user_label,
4674 'value' => $list->get_customer_id(),
4675 ];
4676 }
4677 }
4678 }
4679
4680 return [
4681 'options' => $options,
4682 'hasMore' => false,
4683 ];
4684 }
4685
4686
4687 /**
4688 * Fetch the Presto Player video List.
4689 *
4690 * @param array $data Search Params.
4691 *
4692 * @return array
4693 */
4694 public function search_ap_presto_player_video_list( $data ) {
4695
4696 if ( ! class_exists( 'PrestoPlayer\Models\Video' ) ) {
4697 return [];
4698 }
4699
4700 $videos = ( new Video() )->all();
4701 $options = [];
4702 if ( ! empty( $videos ) ) {
4703 foreach ( $videos as $video ) {
4704 $options[] = [
4705 'label' => $video->__get( 'title' ),
4706 'value' => (string) $video->__get( 'id' ),
4707 ];
4708 }
4709 }
4710
4711 return [
4712 'options' => $options,
4713 'hasMore' => false,
4714 ];
4715 }
4716
4717 /**
4718 * Presto Player Video percentage.
4719 *
4720 * @param array $data Search Params.
4721 *
4722 * @return array
4723 */
4724 public function search_prestoplayer_video_percent( $data ) {
4725
4726 $percents = [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ];
4727 $options = [];
4728
4729 foreach ( $percents as $percent ) {
4730 $options[] = [
4731 'label' => $percent . '%',
4732 'value' => (string) $percent,
4733 ];
4734 }
4735
4736 return [
4737 'options' => $options,
4738 'hasMore' => false,
4739 ];
4740 }
4741
4742 /**
4743 * Get user profile field options.
4744 *
4745 * @return array
4746 * @since 1.0.0
4747 */
4748 public function search_user_field_options() {
4749
4750 $options = apply_filters(
4751 'sure_trigger_get_user_field_options',
4752 [
4753 [
4754 'label' => __( 'User Name', 'suretriggers' ),
4755 'value' => 'user_login',
4756 ],
4757 [
4758 'label' => __( 'User Email', 'suretriggers' ),
4759 'value' => 'user_email',
4760 ],
4761 [
4762 'label' => __( 'Display Name', 'suretriggers' ),
4763 'value' => 'display_name',
4764 ],
4765 [
4766 'label' => __( 'User Password', 'suretriggers' ),
4767 'value' => 'user_pass',
4768 ],
4769 [
4770 'label' => __( 'Website', 'suretriggers' ),
4771 'value' => 'user_url',
4772 ],
4773 ]
4774 );
4775
4776 return [
4777 'options' => $options,
4778 'hasMore' => false,
4779 ];
4780 }
4781
4782 /**
4783 * Get user post field options.
4784 *
4785 * @return array
4786 * @since 1.0.0
4787 */
4788 public function search_post_field_options() {
4789
4790 return [
4791 'options' => [
4792 [
4793 'label' => __( 'Type', 'suretriggers' ),
4794 'value' => 'post_type',
4795 'dynamic_field' => [
4796 'type' => 'select-creatable',
4797 'ajaxIdentifier' => 'post_types',
4798 ],
4799 ],
4800 [
4801 'label' => __( 'Status', 'suretriggers' ),
4802 'value' => 'post_status',
4803 'dynamic_field' => [
4804 'type' => 'select-async',
4805 'ajaxIdentifier' => 'post_statuses',
4806 ],
4807 ],
4808 [
4809 'label' => __( 'Author', 'suretriggers' ),
4810 'value' => 'post_author',
4811 'dynamic_field' => [
4812 'type' => 'select-async',
4813 'ajaxIdentifier' => 'user',
4814 ],
4815 ],
4816 [
4817 'label' => __( 'Title', 'suretriggers' ),
4818 'value' => 'post_title',
4819 'dynamic_field' => [
4820 'type' => 'select-creatable',
4821 ],
4822 ],
4823 [
4824 'label' => __( 'Slug', 'suretriggers' ),
4825 'value' => 'post_slug',
4826 'dynamic_field' => [
4827 'type' => 'select-creatable',
4828 ],
4829 ],
4830 [
4831 'label' => __( 'Content', 'suretriggers' ),
4832 'value' => 'post_content',
4833 'dynamic_field' => [
4834 'type' => 'html-editor',
4835 ],
4836 ],
4837 ],
4838 'hasMore' => false,
4839 ];
4840 }
4841
4842 /**
4843 * Bricksbuilder grouped data.
4844 *
4845 * @param array $data data.
4846 * @return array
4847 */
4848 public function search_bb_groups( $data ) {
4849
4850 global $wpdb;
4851 $options = [];
4852
4853 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'bp_groups' ) ) ) {
4854
4855 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %s', $wpdb->prefix . 'bp_groups' ) );
4856
4857 if ( $results ) {
4858 foreach ( $results as $result ) {
4859 $options[] = [
4860 'label' => $result->name,
4861 'value' => $result->id,
4862 ];
4863 }
4864 }
4865 }
4866
4867 return [
4868 'options' => $options,
4869 'hasMore' => false,
4870 ];
4871 }
4872
4873 /**
4874 * Search forms.
4875 *
4876 * @return array
4877 */
4878 public function search_bb_forums() {
4879 $options = [];
4880 if ( ! function_exists( 'bbp_get_forum_post_type' ) ) {
4881 return [];
4882 }
4883 $allowed_atatus = [ 'publish', 'private' ];
4884 $forum_args = [
4885 'post_type' => bbp_get_forum_post_type(),
4886 'posts_per_page' => -1,
4887 'orderby' => 'title',
4888 'order' => 'ASC',
4889 'post_status' => 'any',
4890 ];
4891 $forums = get_posts( $forum_args );
4892
4893 if ( ! empty( $forums ) ) {
4894 foreach ( $forums as $forum ) {
4895 if ( in_array( $forum->post_status, $allowed_atatus, true ) ) {
4896 $options[] = [
4897 'label' => $forum->post_title,
4898 'value' => $forum->ID,
4899 ];
4900 }
4901 }
4902 }
4903 return [
4904 'options' => $options,
4905 'hasMore' => false,
4906 ];
4907 }
4908
4909 /**
4910 * Search Affiliate WP Referral Type.
4911 *
4912 * @return array
4913 */
4914 public function search_affwp_referral_type() {
4915 $options = [];
4916
4917 if ( ! function_exists( 'affiliate_wp' ) ) {
4918 return [];
4919 }
4920
4921 $types = affiliate_wp()->referrals->types_registry->get_types();
4922 if ( ! empty( $types ) ) {
4923 foreach ( $types as $type_id => $type ) {
4924 $options[] = [
4925 'label' => $type['label'],
4926 'value' => $type_id,
4927 ];
4928 }
4929 }
4930 return [
4931 'options' => $options,
4932 'hasMore' => false,
4933 ];
4934 }
4935
4936 /**
4937 * Search Affiliate WP Referral Status.
4938 *
4939 * @return array
4940 */
4941 public function search_affwp_referral_status() {
4942 $options = [];
4943
4944 if ( ! function_exists( 'affwp_get_affiliate_statuses' ) ) {
4945 return [];
4946 }
4947
4948 $statuses = affwp_get_affiliate_statuses();
4949 if ( ! empty( $statuses ) ) {
4950 foreach ( $statuses as $key => $status ) {
4951 $options[] = [
4952 'label' => $status,
4953 'value' => $key,
4954 ];
4955 }
4956 }
4957 return [
4958 'options' => $options,
4959 'hasMore' => false,
4960 ];
4961 }
4962
4963 /**
4964 * Search Affiliate WP Affiliates list.
4965 *
4966 * @return array
4967 */
4968 public function search_affwp_affiliates_list() {
4969 $options = [];
4970
4971 global $wpdb;
4972 $affiliates = $wpdb->get_results( "SELECT affiliate_id FROM {$wpdb->prefix}affiliate_wp_affiliates" );
4973
4974 if ( ! function_exists( 'affwp_get_affiliate_name' ) ) {
4975 return [];
4976 }
4977
4978 if ( ! empty( $affiliates ) ) {
4979 foreach ( $affiliates as $affiliate ) {
4980 $options[] = [
4981 'label' => affwp_get_affiliate_name( $affiliate->affiliate_id ),
4982 'value' => $affiliate->affiliate_id,
4983 ];
4984 }
4985 }
4986 return [
4987 'options' => $options,
4988 'hasMore' => false,
4989 ];
4990 }
4991
4992 /**
4993 * Search Slice WP Affiliates list.
4994 *
4995 * @return array
4996 */
4997 public function search_slicewp_affiliates_list() {
4998 $options = [];
4999
5000 global $wpdb;
5001 $affiliates = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}slicewp_affiliates" );
5002
5003 if ( ! function_exists( 'slicewp_get_affiliate_name' ) ) {
5004 return [];
5005 }
5006
5007 if ( ! empty( $affiliates ) ) {
5008 foreach ( $affiliates as $affiliate ) {
5009 $options[] = [
5010 'label' => slicewp_get_affiliate_name( $affiliate->id ),
5011 'value' => $affiliate->id,
5012 ];
5013 }
5014 }
5015 return [
5016 'options' => $options,
5017 'hasMore' => false,
5018 ];
5019 }
5020
5021 /**
5022 * Search for commissions list.
5023 *
5024 * @param array $data data.
5025 *
5026 * @return array
5027 */
5028 public function search_slicewp_commissions_list( $data ) {
5029 $options = [];
5030 global $wpdb;
5031 $affiliate_id = $data['dynamic']['affiliate_id'];
5032 $commissions = $wpdb->get_results(
5033 $wpdb->prepare(
5034 "SELECT *
5035 FROM {$wpdb->prefix}slicewp_commissions WHERE affiliate_id=%d ORDER BY id DESC ",
5036 $affiliate_id
5037 )
5038 );
5039
5040
5041 if ( ! empty( $commissions ) ) {
5042 foreach ( $commissions as $commission ) {
5043 $options[] = [
5044 'label' => $commission->reference . '(' . $commission->type . ')',
5045 'value' => $commission->id,
5046 ];
5047 }
5048 }
5049 return [
5050 'options' => $options,
5051 'hasMore' => false,
5052 ];
5053 }
5054
5055
5056 /**
5057 * Search Affiliate WP Affiliates list.
5058 *
5059 * @return array
5060 */
5061 public function search_affwp_affiliates_rate_type() {
5062 $options = [];
5063
5064 if ( ! function_exists( 'affwp_get_affiliate_rate_types' ) ) {
5065 return [];
5066 }
5067
5068 $rate_types = affwp_get_affiliate_rate_types();
5069
5070 if ( ! empty( $rate_types ) ) {
5071 foreach ( $rate_types as $key => $rate_type ) {
5072 $options[] = [
5073 'label' => $rate_type,
5074 'value' => $key,
5075 ];
5076 }
5077 }
5078 return [
5079 'options' => $options,
5080 'hasMore' => false,
5081 ];
5082 }
5083
5084 /**
5085 * Get last data for trigger.
5086 *
5087 * @param array $data data.
5088 * @return array
5089 */
5090 public function search_affiliate_wp_triggers_last_data( $data ) {
5091 global $wpdb;
5092
5093 $context = [];
5094 $context['response_type'] = 'sample';
5095
5096 $user_data = WordPress::get_sample_user_context();
5097
5098 $affiliate_data = [
5099 'affiliate_id' => 1,
5100 'rest_id' => '',
5101 'user_id' => 1,
5102 'rate' => '',
5103 'rate_type' => '',
5104 'flat_rate_basis' => '',
5105 'payment_email' => 'admin@gmail.com',
5106 'status' => 'active',
5107 'earnings' => 0,
5108 'unpaid_earnings' => 0,
5109 'referrals' => 0,
5110 'visits' => 0,
5111 'date_registered' => '2023-01-18 13:35:22',
5112 'dynamic_coupon' => 'KDJSKS',
5113 ];
5114
5115 $referral_data = [
5116 'referral_id' => 1,
5117 'affiliate_id' => 1,
5118 'visit_id' => 0,
5119 'rest_id' => '',
5120 'customer_id' => 0,
5121 'parent_id' => 0,
5122 'description' => 'Testing',
5123 'status' => 'unpaid',
5124 'amount' => '12.00',
5125 'currency' => '',
5126 'custom' => 'custom',
5127 'context' => '',
5128 'campaign' => '',
5129 'reference' => 'Reference',
5130 'products' => '',
5131 'date' => '2023-01-18 16:36:59',
5132 'type' => 'opt-in',
5133 'payout_id' => 0,
5134 ];
5135
5136 if ( ! function_exists( 'affwp_get_dynamic_affiliate_coupons' ) || ! function_exists( 'affwp_get_affiliate' ) || ! function_exists( 'affwp_get_referral' ) ) {
5137 return [];
5138 }
5139
5140 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
5141
5142 if ( in_array( $term, [ 'affiliate_approved', 'affiliate_awaiting_approval' ], true ) ) {
5143 $status = 'affiliate_approved' === $term ? 'active' : 'pending';
5144 $affiliate = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_affiliates WHERE affiliate_id = ( SELECT max(affiliate_id) FROM {$wpdb->prefix}affiliate_wp_affiliates )" );
5145
5146 if ( ! empty( $affiliate ) ) {
5147 $affiliate = affwp_get_affiliate( $affiliate->affiliate_id );
5148 $affiliate_data = get_object_vars( $affiliate );
5149 $user_data = WordPress::get_user_context( $affiliate->user_id );
5150 $context['response_type'] = 'live';
5151 }
5152 $affiliate_data['status'] = $status;
5153 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data );
5154
5155 } elseif ( 'affiliate_makes_referral' == $term ) {
5156 $type = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
5157 $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 ) );
5158
5159 if ( ! empty( $referral ) ) {
5160 $referral = affwp_get_referral( $referral->referral_id );
5161 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
5162 $affiliate_data = get_object_vars( $affiliate );
5163 $user_data = WordPress::get_user_context( $affiliate->user_id );
5164 $referral_data = get_object_vars( $referral );
5165 $context['response_type'] = 'live';
5166 }
5167 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
5168 } elseif ( 'affiliate_wc_product_purchased' == $term ) {
5169 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
5170 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'woocommerce' ORDER BY referral_id DESC LIMIT 1" );
5171
5172 if ( ! empty( $referral ) ) {
5173 $referral = affwp_get_referral( $referral->referral_id );
5174 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
5175 $affiliate_data = get_object_vars( $affiliate );
5176 if ( ! empty( $referral ) && function_exists( 'wc_get_order' ) ) {
5177 $order_id = $referral->reference;
5178 $order = wc_get_order( $order_id );
5179 if ( $order instanceof \WC_Order ) {
5180 $user_data = WordPress::get_user_context( $order->get_customer_id() );
5181 }
5182 }
5183 $referral_data = get_object_vars( $referral );
5184 $context['response_type'] = 'live';
5185 }
5186 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
5187 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data, $dynamic_coupons );
5188 if ( ! empty( $referral ) && function_exists( 'wc_get_order' ) ) {
5189 $order_id = $referral->reference;
5190 $order = wc_get_order( $order_id );
5191 if ( $order instanceof \WC_Order ) {
5192 $items = $order->get_items();
5193 foreach ( $items as $item ) {
5194 $context['pluggable_data']['product'] = $item['product_id'];
5195 }
5196 }
5197 } else {
5198 $context['pluggable_data']['product'] = 1;
5199 }
5200 } elseif ( 'affiliate_edd_product_purchased' == $term ) {
5201 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
5202 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'edd' ORDER BY referral_id DESC LIMIT 1" );
5203
5204 if ( ! empty( $referral ) ) {
5205 $referral = affwp_get_referral( $referral->referral_id );
5206 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
5207 $affiliate_data = get_object_vars( $affiliate );
5208 $user_data = WordPress::get_user_context( $affiliate->user_id );
5209 $referral_data = get_object_vars( $referral );
5210 $context['response_type'] = 'live';
5211 }
5212 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
5213 if ( ! empty( $referral ) && function_exists( 'edd_get_payment' ) ) {
5214 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
5215 $edd_payment_id = $referral->reference;
5216 $payment = edd_get_payment( $edd_payment_id );
5217 $cart_details = $payment->cart_details;
5218 $payment = get_object_vars( $payment );
5219 $context['pluggable_data'] = array_merge( $context['pluggable_data'], $dynamic_coupons, $payment );
5220 foreach ( $cart_details as $detail ) {
5221 $context['pluggable_data']['product'] = $detail['id'];
5222 }
5223 } else {
5224 $context['pluggable_data']['product'] = 1;
5225 }
5226 } elseif ( 'affiliate_mb_product_purchased' == $term ) {
5227 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
5228 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'memberpress' ORDER BY referral_id DESC LIMIT 1" );
5229
5230 if ( ! empty( $referral ) && class_exists( '\MeprTransaction' ) ) {
5231 $referral = affwp_get_referral( $referral->referral_id );
5232 $reference_id = $referral->reference;
5233 $transaction = new MeprTransaction( $reference_id );
5234 $user_id = $transaction->user_id;
5235 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
5236 $affiliate_data = get_object_vars( $affiliate );
5237 $user_data = WordPress::get_user_context( $user_id );
5238 $referral_data = get_object_vars( $referral );
5239 $context['response_type'] = 'live';
5240 }
5241 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
5242 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data, $dynamic_coupons );
5243 if ( ! empty( $referral ) && class_exists( '\MeprTransaction' ) ) {
5244 $membership_id = $wpdb->get_var(
5245 $wpdb->prepare(
5246 "SELECT product_id FROM
5247 {$wpdb->prefix}mepr_transactions WHERE id = %d",
5248 $referral->reference
5249 )
5250 );
5251 $context['pluggable_data']['product'] = $membership_id;
5252 $context['pluggable_data']['product_name'] = get_the_title( $membership_id );
5253 } else {
5254 $context['pluggable_data']['product'] = 1;
5255 $context['pluggable_data']['product_name'] = 'membership1';
5256 }
5257 } elseif ( 'affiliate_referral_paid' == $term ) {
5258 $type = isset( $data['filter']['type']['value'] ) ? $data['filter']['type']['value'] : '';
5259 $referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE type = %s AND status = 'paid'", $type ) );
5260
5261 if ( ! empty( $referral ) ) {
5262 $referral = affwp_get_referral( $referral->referral_id );
5263 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
5264 $affiliate_data = get_object_vars( $affiliate );
5265 $user_data = WordPress::get_user_context( $affiliate->user_id );
5266 $referral_data = get_object_vars( $referral );
5267 $context['response_type'] = 'live';
5268 }
5269 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
5270 } elseif ( 'affiliate_referral_rejected' == $term ) {
5271 $type = isset( $data['filter']['type']['value'] ) ? $data['filter']['type']['value'] : '';
5272 $referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE type = %s AND status = 'rejected'", $type ) );
5273 if ( ! empty( $referral ) ) {
5274 $referral = affwp_get_referral( $referral->referral_id );
5275 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
5276 $affiliate_data = get_object_vars( $affiliate );
5277 $user_data = WordPress::get_user_context( $affiliate->user_id );
5278 $referral_data = get_object_vars( $referral );
5279 $context['response_type'] = 'live';
5280 }
5281 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
5282 } else {
5283 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE referral_id = ( SELECT max(referral_id) FROM {$wpdb->prefix}affiliate_wp_referrals )" );
5284
5285 if ( ! empty( $referral ) ) {
5286 $referral = affwp_get_referral( $referral->referral_id );
5287 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
5288 $affiliate_data = get_object_vars( $affiliate );
5289 $user_data = WordPress::get_user_context( $affiliate->user_id );
5290 $referral_data = get_object_vars( $referral );
5291 $context['response_type'] = 'live';
5292 }
5293 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
5294 }
5295
5296 return $context;
5297 }
5298
5299 /**
5300 * Get last data for trigger.
5301 *
5302 * @param array $data data.
5303 * @return array
5304 */
5305 public function search_jetpack_crm_triggers_last_data( $data ) {
5306
5307 if ( ! function_exists( 'zeroBS_getCompanyCount' ) || ! function_exists( 'zeroBS_getCustomerCount' ) || ! function_exists( 'zeroBS_getQuoteCount' ) ) {
5308 return [];
5309 }
5310
5311 global $wpdb;
5312
5313 $context = [];
5314 $context['response_type'] = 'sample';
5315
5316 $company_id = [ 'company_id' => 1 ];
5317 $contact_id = [ 'contact_id' => 1 ];
5318 $quote_id = [ 'quote_id' => 1 ];
5319 $event_id = [ 'event_id' => 1 ];
5320 $invoice_id = [ 'invoice_id' => 1 ];
5321 $transaction_id = [ 'transaction_id' => 1 ];
5322
5323 $company = [
5324 'company_id' => 1,
5325 'company_status' => 'Lead',
5326 'company_name' => 'Example Company',
5327 'company_email' => 'info@example.com',
5328 'main_address_line_1' => '123 Main Street',
5329 'main_address_line_2' => 'Suite 456',
5330 'main_address_city' => 'New York',
5331 'main_address_state' => 'NY',
5332 'main_address_postal_code' => '10001',
5333 'main_address_country' => 'United States',
5334 'second_address_line_1' => '789 Second Avenue',
5335 'second_address_line_2' => 'Floor 10',
5336 'second_address_city' => 'Los Angeles',
5337 'second_address_state' => 'CA',
5338 'second_address_postal_code' => '90001',
5339 'second_address_country' => 'United States',
5340 'main_telephone' => '+1 123-456-7890',
5341 'secondary_telephone' => '+1 987-654-3210',
5342 ];
5343
5344 $contact = [
5345 'contact_id' => 1,
5346 'status' => 'Lead',
5347 'prefix' => 'Mr.',
5348 'full_name' => 'John Doe',
5349 'first_name' => 'John',
5350 'last_name' => 'Doe',
5351 'email' => 'johndoe@example.com',
5352 'main_address_line_1' => '123 Main Street',
5353 'main_address_line_2' => 'Suite 456',
5354 'main_address_city' => 'New York',
5355 'main_address_state' => 'NY',
5356 'main_address_postal_code' => '10001',
5357 'main_address_country' => 'United States',
5358 'second_address_line_1' => '789 Second Avenue',
5359 'second_address_line_2' => 'Floor 10',
5360 'second_address_city' => 'Los Angeles',
5361 'second_address_state' => 'CA',
5362 'second_address_postal_code' => '90001',
5363 'second_address_country' => 'United States',
5364 'home_telephone' => '+1 123-456-7890',
5365 'work_telephone' => '+1 987-654-3210',
5366 'mobile_telephone' => '+1 555-555-5555',
5367 ];
5368
5369 $quote = [
5370 'quote_id' => 1,
5371 'contact_id' => 2,
5372 'contact_email' => 'john@example.com',
5373 'contact_name' => 'John Doe',
5374 'status' => '<strong>Created, not yet accepted</strong>',
5375 'title' => 'Sample Quote',
5376 'value' => 1000,
5377 'date' => '2023-05-23',
5378 'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
5379 'notes' => 'Additional notes about the quote.',
5380 ];
5381
5382 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
5383
5384 switch ( $term ) {
5385 case 'company_created':
5386 if ( zeroBS_getCompanyCount() > 0 ) {
5387 $company_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_companies WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_companies )" );
5388 $company = JetpackCRM::get_company_context( $company_data->ID );
5389 $context['response_type'] = 'live';
5390 }
5391 $context['pluggable_data'] = $company;
5392 break;
5393 case 'company_deleted':
5394 $context['pluggable_data'] = $company_id;
5395 break;
5396 case 'contact_created':
5397 if ( zeroBS_getCustomerCount() > 0 ) {
5398 $contact_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_contacts WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_contacts )" );
5399 $contact = JetpackCRM::get_contact_context( $contact_data->ID );
5400 $context['response_type'] = 'live';
5401 }
5402 $context['pluggable_data'] = $contact;
5403 break;
5404 case 'contact_deleted':
5405 $context['pluggable_data'] = $contact_id;
5406 break;
5407 case 'event_deleted':
5408 $context['pluggable_data'] = $event_id;
5409 break;
5410 case 'invoice_deleted':
5411 $context['pluggable_data'] = $invoice_id;
5412 break;
5413 case 'quote_accepted':
5414 case 'quote_created':
5415 if ( zeroBS_getQuoteCount() > 0 ) {
5416 $quote_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_quotes WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_quotes )" );
5417 $quote = JetpackCRM::get_quote_context( $quote_data->ID );
5418 $context['response_type'] = 'live';
5419 }
5420 $context['pluggable_data'] = $quote;
5421 break;
5422 case 'quote_deleted':
5423 $context['pluggable_data'] = $quote_id;
5424 break;
5425 case 'transaction_deleted':
5426 $context['pluggable_data'] = $transaction_id;
5427 break;
5428 }
5429
5430 return $context;
5431 }
5432
5433 /**
5434 * Get last data for trigger.
5435 *
5436 * @param array $data data.
5437 * @return array
5438 */
5439 public function search_funnel_kit_automations_triggers_last_data( $data ) {
5440
5441 if ( ! class_exists( 'BWFCRM_Contact' ) || ! class_exists( 'BWFCRM_Lists' ) || ! class_exists( 'BWFCRM_Tag' ) ) {
5442 return [];
5443 }
5444
5445 $context = [];
5446 $context['response_type'] = 'sample';
5447
5448 $contact = [
5449 'contact_id' => '1',
5450 'wpid' => '0',
5451 'uid' => '9e74246335fd81b1c4a9123842c12549',
5452 'email' => 'johndoe@example.com',
5453 'first_name' => 'John',
5454 'last_name' => 'Doe',
5455 'contact_no' => '+1 555-555-5555',
5456 'state' => 'NY',
5457 'country' => 'United States',
5458 'timezone' => 'New York',
5459 'creation_date' => '2023-05-29 15:26:03',
5460 'last_modified' => '2023-05-29 17:08:30',
5461 'source' => '',
5462 'type' => 'Los Angeles',
5463 'status' => '0',
5464 'tags' => '["1"]',
5465 'lists' => '["2","1"]',
5466 ];
5467
5468 $list = [
5469 'list_id' => 1,
5470 'list_name' => 'Sample List',
5471 ];
5472
5473 $tag = [
5474 'tag_id' => 1,
5475 'tag_name' => 'Sample Tag',
5476 ];
5477
5478 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
5479
5480 $recent_contacts = \BWFCRM_Contact::get_recent_contacts( 0, 1, [] );
5481 $contact_email = count( $recent_contacts['contacts'] ) > 0 && isset( $recent_contacts['contacts'][0]['email'] ) ? $recent_contacts['contacts'][0]['email'] : '';
5482
5483 $real_contact = false;
5484 if ( ! empty( $contact_email ) ) {
5485 $contact_obj = \BWFCRM_Contact::get_contacts( $contact_email, 0, 1, [], [], OBJECT );
5486
5487 if ( isset( $contact_obj['contacts'][0] ) ) {
5488 $contact = FunnelKitAutomations::get_contact_context( $contact_obj['contacts'][0]->contact );
5489 $real_contact = true;
5490 }
5491 }
5492
5493 if ( 'contact_added_to_list' === $term || 'contact_removed_from_list' === $term ) {
5494 $list_id = (int) ( isset( $data['filter']['list_id']['value'] ) ? $data['filter']['list_id']['value'] : '-1' );
5495
5496 if ( -1 === $list_id ) {
5497 $lists = \BWFCRM_Lists::get_lists( [], '', 0, 1 );
5498 $list_id = count( $lists ) > 0 ? $lists[0]['ID'] : -1;
5499 }
5500
5501
5502 if ( -1 !== $list_id ) {
5503 $list = FunnelKitAutomations::get_list_context( $list_id );
5504 $context['response_type'] = $real_contact ? 'live' : 'sample';
5505 }
5506
5507 $context['pluggable_data'] = array_merge( $list, $contact );
5508 } else {
5509 $tag_id = (int) ( isset( $data['filter']['tag_id']['value'] ) ? $data['filter']['tag_id']['value'] : '-1' );
5510
5511 if ( -1 === $tag_id ) {
5512 $tags = \BWFCRM_Tag::get_tags( [], '', 0, 1 );
5513 $tag_id = count( $tags ) > 0 ? $tags[0]['ID'] : -1;
5514 }
5515
5516
5517 if ( -1 !== $tag_id ) {
5518 $tag = FunnelKitAutomations::get_tag_context( $tag_id );
5519 $context['response_type'] = $real_contact ? 'live' : 'sample';
5520 }
5521
5522 $context['pluggable_data'] = array_merge( $tag, $contact );
5523 }
5524
5525 return $context;
5526 }
5527 /**
5528 * Prepare EDD Order Status List.
5529 *
5530 * @param array $data Search Params.
5531 * @return array{options: array<int, array{label: string, value: string}>, hasMore: bool}
5532 */
5533 public function search_edd_order_status_list( $data ) {
5534 $options = [];
5535
5536 if ( function_exists( 'edd_get_payment_statuses' ) ) {
5537 $order_statuses = edd_get_payment_statuses();
5538
5539 if ( ! empty( $order_statuses ) ) {
5540 foreach ( $order_statuses as $key => $label ) {
5541 $options[] = [
5542 'label' => $label,
5543 'value' => $key,
5544 ];
5545 }
5546 }
5547 }
5548
5549 return [
5550 'options' => $options,
5551 'hasMore' => false,
5552 ];
5553 }
5554
5555 /**
5556 * Get last data for trigger.
5557 *
5558 * @param array $data data.
5559 * @return array
5560 */
5561 public function search_edd_triggers_last_data( $data ) {
5562 global $wpdb;
5563 if ( ! function_exists( 'edd_get_payments' ) ) {
5564 return [];
5565 }
5566 $context = [];
5567 $context['response_type'] = 'sample';
5568 $context['pluggable_data'] = [];
5569 $order_data = [
5570 'order_id' => 187,
5571 'customer_email' => 'john_doe@gmail.com',
5572 'customer_id' => 2,
5573 'user_id' => 1,
5574 'customer_first_name' => 'Sure',
5575 'customer_last_name' => 'Dev',
5576 'ordered_items' => 'Price with license — Price one',
5577 'currency' => 'USD',
5578 'status' => 'complete',
5579 'discount_codes' => '',
5580 'order_discounts' => 0.00,
5581 'order_subtotal' => 12.00,
5582 'order_tax' => 0.00,
5583 'order_total' => 12.00,
5584 'payment_method' => 'manual',
5585 'purchase_key' => 'd797b9576a3895e7424bae2417ed87df',
5586 'ordered_items_ids' => 17250,
5587 'download_id' => 17250,
5588 'license_key' => 'f7736093411cfaed18b56ec60227117b',
5589 'license_key_expire_date' => '1697524076',
5590 'license_key_status' => 'inactive',
5591 ];
5592
5593 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
5594 $download_id = isset( $data['filter']['download_id']['value'] ) ? $data['filter']['download_id']['value'] : 0;
5595 if ( 'order_created' === $term || 'order_one_product' === $term ) {
5596 $order_data['purchase_key'] = '06d3b7d923ca922dc889354f9bc33fbb';
5597
5598 $args = [
5599 'number' => 1,
5600 'status' => [ 'complete', 'refunded', 'partially_refunded', 'renewal' ],
5601 ];
5602 if ( $download_id > 0 ) {
5603 $args['download'] = $download_id;
5604 }
5605 $payments = edd_get_payments( $args );
5606 if ( count( $payments ) > 0 ) {
5607 $order_data = EDD::get_product_purchase_context( $payments[0], $term, $download_id );
5608
5609 $context['response_type'] = 'live';
5610 } else {
5611 if ( 'order_one_product' === $term ) {
5612 $order_data['price_id'] = 1;
5613 }
5614 }
5615 } elseif ( 'order_status_changes' === $term ) {
5616 $order_id = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}edd_orders ORDER BY id DESC LIMIT 1" );
5617 if ( ! function_exists( 'edd_get_order' ) ) {
5618 return $context;
5619 }
5620 $order = edd_get_order( $order_id );
5621
5622 if ( $order ) {
5623 $downloads = [];
5624
5625 foreach ( $order->get_items() as $item ) {
5626 $downloads[] = [
5627 'id' => $item->product_id,
5628 'name' => get_the_title( $item->product_id ),
5629 'quantity' => $item->quantity,
5630 'price' => $item->price,
5631 ];
5632 }
5633
5634 $context['pluggable_data'] = [
5635 'order_id' => $order_id,
5636 'customer_id' => $order->customer_id,
5637 'email' => $order->email,
5638 'total' => $order->total,
5639 'downloads' => $downloads,
5640 'date_created' => $order->date_created,
5641 'old_status' => 'completed',
5642 'new_status' => 'pending',
5643 ];
5644 $context['response_type'] = 'live';
5645 } else {
5646 $context['pluggable_data'] = [
5647 'order_id' => 123,
5648 'customer_id' => 5,
5649 'email' => 'sample@example.com',
5650 'total' => 25.00,
5651 'downloads' => [
5652 [
5653 'id' => 101,
5654 'name' => 'Sample Product',
5655 'quantity' => 1,
5656 'price' => 25.00,
5657 ],
5658 ],
5659 'date_created' => current_time( 'mysql' ),
5660 'old_status' => 'completed',
5661 'new_status' => 'pending',
5662 ];
5663 $context['response_type'] = 'sample';
5664 }
5665 } elseif ( 'stripe_payment_refunded' === $term ) {
5666 $args = [
5667 'number' => 1,
5668 'status' => 'complete',
5669 'type' => 'refund',
5670 ];
5671 $payments = edd_get_payments( $args );
5672
5673 if ( count( $payments ) > 0 ) {
5674 $order_data = EDD::get_purchase_refund_context( $payments[0] );
5675 $context['response_type'] = 'live';
5676 }
5677 } else {
5678 $status = isset( $data['post_type'] ) ? $data['post_type'] : '';
5679 if ( ! empty( $status ) ) {
5680 if ( $download_id > 0 ) {
5681 $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 ) );
5682 } else {
5683 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE status= %s ORDER BY id DESC", $status ) );
5684 }
5685 } else {
5686 if ( $download_id > 0 ) {
5687 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE download_id=%d ORDER BY id DESC", $download_id ) );
5688 } else {
5689 $licesnses = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}edd_licenses ORDER BY id DESC" );
5690 }
5691 }
5692 if ( ! empty( $licesnses ) ) {
5693 $order_data = EDD::edd_get_license_data( $licesnses->id, $licesnses->download_id, $licesnses->payment_id );
5694 $context['response_type'] = 'live';
5695 } else {
5696 $order_data = [
5697 'ID' => 1,
5698 'key' => '23232323232',
5699 'customer_email' => 'suretest@example.com',
5700 'customer_name' => 'Sure Test',
5701 'product_id' => 1,
5702 'download_id' => 1,
5703 'product_name' => 'Test',
5704 'activation_limit' => 2,
5705 'activation_count' => 1,
5706 'activated_urls' => 'https://example.com',
5707 'expiration' => '1686297914',
5708 'is_lifetime' => '0',
5709 'status' => $status,
5710 ];
5711
5712 }
5713 }
5714
5715 if ( empty( $context['pluggable_data'] ) ) {
5716 $context['pluggable_data'] = $order_data;
5717 }
5718 return $context;
5719 }
5720
5721 /**
5722 * Get last data for trigger.
5723 *
5724 * @param array $data data.
5725 * @return array
5726 */
5727 public function search_presto_player_triggers_last_data( $data ) {
5728 $context = [];
5729 if ( ! class_exists( 'PrestoPlayer\Models\Video' ) ) {
5730 return [];
5731 }
5732 $context['response_type'] = 'sample';
5733
5734 $user_data = WordPress::get_sample_user_context();
5735
5736 $video_data = [
5737 'pp_video' => '1',
5738 'pp_video_percentage' => '100',
5739 ];
5740
5741 $video_data['video'] = [
5742 'id' => '1',
5743 'title' => 'OttoKit Is Here 🎉 The Easiest Automation Builder WordPress Websites & Apps',
5744 'type' => 'youtube',
5745 'external_id' => '-cYbNYgylLs',
5746 'attachment_id' => '0',
5747 'post_id' => '127',
5748 'src' => 'https://www.youtube.com/watch?v=-cYbNYgylLs',
5749 'created_by' => '1',
5750 'created_at' => '2022-11-10 00:28:25',
5751 'updated_at' => '2022-11-10 00:34:40',
5752 'deleted_at' => '',
5753 ];
5754
5755 $video_data['media']['tag'] = [
5756 'Demo',
5757 'Demo1',
5758 ];
5759
5760 $mediahub_data = [
5761 'post_id' => '1',
5762 'activity_id' => '2',
5763 'post_author' => '1',
5764 'post_content' => 'New sample post...!',
5765 'post_title' => 'Sample Post',
5766 'post_excerpt' => 'sample',
5767 'post_status' => 'publish',
5768 'post_type' => 'pp_video_block',
5769 ];
5770
5771 $videos = ( new Video() )->all();
5772
5773 if ( count( $videos ) > 0 ) {
5774 if ( ! empty( $data['filter'] ) ) {
5775 $video_id = $data['filter']['pp_video']['value'];
5776 if ( -1 === $video_id ) {
5777 global $wpdb;
5778 $args = [
5779 'numberposts' => 1,
5780 'orderby' => 'rand',
5781 'order' => 'ASC',
5782 'post_type' => 'pp_video_block',
5783 ];
5784 $posts = get_posts( $args );
5785 $presto_post_video_id = $posts[0]->ID;
5786 $presto_video_id = $wpdb->get_var( $wpdb->prepare( 'SELECT * FROM wp_presto_player_videos WHERE post_id = %d', $presto_post_video_id ) );
5787 $video_id = $presto_video_id;
5788 }
5789 $pp_videos = ( new Video( $video_id ) )->toArray();
5790 if ( ! empty( $pp_videos ) ) {
5791 $video_data = [];
5792 $video_data['video'] = $pp_videos;
5793 if ( is_array( $video_data ) && array_key_exists( 'post_id', $video_data['video'] ) ) {
5794 $mediahub_data = WordPress::get_post_context( (int) $video_data['video']['post_id'] );
5795 $media_tags = get_the_terms( (int) $video_data['video']['post_id'], 'pp_video_tag' );
5796 if ( ! empty( $media_tags ) && is_array( $media_tags ) && isset( $media_tags[0] ) ) {
5797 $tag_name = [];
5798 foreach ( $media_tags as $tag ) {
5799 $tag_name[] = $tag->name;
5800 }
5801 $video_data['media']['tag'] = $tag_name;
5802 }
5803 }
5804 $video_data['pp_video'] = $video_id;
5805 $video_data['pp_video_percentage'] = isset( $data['filter']['pp_video_percentage']['value'] ) ? $data['filter']['pp_video_percentage']['value'] : '100';
5806 $user_data = WordPress::get_user_context( (int) $video_data['video']['created_by'] );
5807
5808 $context['response_type'] = 'live';
5809 }
5810 }
5811 }
5812
5813 $context['pluggable_data'] = array_merge( $user_data, $video_data, $mediahub_data );
5814
5815 return $context;
5816 }
5817
5818 /**
5819 * Get last data for trigger.
5820 *
5821 * @param array $data data.
5822 * @return array
5823 */
5824 public function search_member_press_triggers_last_data( $data ) {
5825 global $wpdb;
5826
5827 $term = $data['search_term'] ? $data['search_term'] : '';
5828
5829 $context = [];
5830 $context['response_type'] = 'sample';
5831
5832 $user_data = WordPress::get_sample_user_context();
5833
5834 $membership_data = [
5835 'membership_id' => '190',
5836 'membership_title' => 'Sample Membership',
5837 'amount' => '12.00',
5838 'total' => '12.00',
5839 'tax_amount' => '0.00',
5840 'tax_rate' => '0.00',
5841 'trans_num' => 't_63a03f3334f44',
5842 'status' => 'complete',
5843 'subscription_id' => '0',
5844 'membership_url' => site_url() . '/register/premium/',
5845 'membership_featured_image_id' => '521',
5846 'membership_featured_image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
5847 ];
5848
5849 $membership_id = (int) ( isset( $data['filter']['membership_id']['value'] ) ? $data['filter']['membership_id']['value'] : '-1' );
5850
5851 if ( in_array( $term, [ 'mepr-event-transaction-expired', 'mepr_subscription_transition_status', 'mepr-event-transaction-paused' ] ) ) {
5852 $status = 'cancelled';
5853 if ( 'mepr-event-transaction-paused' === $term ) {
5854 $status = 'suspended';
5855 }
5856 if ( $membership_id > 0 ) {
5857 $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 ) );
5858 } else {
5859 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_subscriptions WHERE status=%s ORDER BY id DESC LIMIT 1", $status ) );
5860
5861 }
5862 if ( ! empty( $subscription ) ) {
5863
5864 $membership_data = MemberPress::get_subscription_context( $subscription );
5865 $user_data = WordPress::get_user_context( $subscription->user_id );
5866
5867
5868 $context['response_type'] = 'live';
5869 }
5870 } elseif ( 'mepr-coupon-code-redeemed' === $term ) {
5871
5872 $coupon_id = (int) ( isset( $data['filter']['coupon_id']['value'] ) ? $data['filter']['coupon_id']['value'] : '-1' );
5873
5874 if ( $coupon_id > 0 ) {
5875 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE coupon_id= %s ORDER BY id DESC LIMIT 1", $coupon_id ) );
5876 } else {
5877 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE coupon_id!= %s ORDER BY id DESC LIMIT 1", '0' ) );
5878 }
5879
5880
5881 if ( ! empty( $subscription ) ) {
5882
5883 $membership_data = MemberPress::get_membership_context( $subscription );
5884 $user_data = WordPress::get_user_context( $subscription->user_id );
5885 $membership_data['coupon_id'] = $subscription->coupon_id;
5886 $membership_data['coupon'] = get_post( $subscription->coupon_id );
5887 $context['response_type'] = 'live';
5888 }
5889 } else {
5890
5891 if ( $membership_id > 0 ) {
5892
5893 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE product_id= %s ORDER BY id DESC LIMIT 1", $membership_id ) );
5894 } else {
5895 $subscription = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mepr_transactions ORDER BY id DESC LIMIT 1" );
5896 }
5897
5898
5899 if ( ! empty( $subscription ) ) {
5900
5901 $membership_data = MemberPress::get_membership_context( $subscription );
5902 $user_data = WordPress::get_user_context( $subscription->user_id );
5903
5904
5905 $context['response_type'] = 'live';
5906 }
5907 }
5908
5909
5910 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5911 return $context;
5912 }
5913
5914 /**
5915 * Get last data for MemberPress membership created trigger.
5916 *
5917 * @param array $data data.
5918 * @return array
5919 */
5920 public function search_mepr_event_member_signup_completed( $data ) {
5921 global $wpdb;
5922
5923 $term = $data['search_term'] ? $data['search_term'] : '';
5924
5925 $context = [];
5926 $context['response_type'] = 'sample';
5927
5928 $user_data = WordPress::get_sample_user_context();
5929
5930 $membership_data = [
5931 'membership_id' => '190',
5932 'membership_title' => 'Sample Membership',
5933 'membership_url' => site_url() . '/register/premium/',
5934 'membership_featured_image_id' => '521',
5935 'membership_featured_image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
5936 'amount' => '12.00',
5937 'total' => '12.00',
5938 'tax_amount' => '0.00',
5939 'tax_rate' => '0.00',
5940 'trans_num' => 't_63a03f3334f44',
5941 'status' => 'complete',
5942 'subscription_id' => '0',
5943 'transaction_id' => '123',
5944 'signup_date' => '2023-12-19 10:30:00',
5945 ];
5946
5947 // Get last completed transaction for live data.
5948 $transaction = $wpdb->get_row(
5949 "SELECT t.*, e.created_at as signup_date FROM {$wpdb->prefix}mepr_transactions t
5950 LEFT JOIN {$wpdb->prefix}mepr_events e ON e.evt_id = t.user_id AND e.event = 'member-signup-completed'
5951 WHERE t.status = 'complete'
5952 ORDER BY t.id DESC LIMIT 1"
5953 );
5954
5955 if ( ! empty( $transaction ) ) {
5956 // Get membership/product details.
5957 $membership = get_post( $transaction->product_id );
5958 if ( $membership ) {
5959 $membership_data = [
5960 'membership_id' => $transaction->product_id,
5961 'membership_title' => $membership->post_title,
5962 'membership_url' => get_permalink( $transaction->product_id ),
5963 'membership_featured_image_id' => get_post_meta( $transaction->product_id, '_thumbnail_id', true ),
5964 'membership_featured_image_url' => get_the_post_thumbnail_url( $transaction->product_id ),
5965 'amount' => $transaction->amount,
5966 'total' => $transaction->total,
5967 'tax_amount' => $transaction->tax_amount,
5968 'tax_rate' => $transaction->tax_rate,
5969 'trans_num' => $transaction->trans_num,
5970 'status' => $transaction->status,
5971 'subscription_id' => $transaction->subscription_id,
5972 'transaction_id' => $transaction->id,
5973 'signup_date' => $transaction->signup_date ? $transaction->signup_date : $transaction->created_at,
5974 ];
5975
5976 $user_data = WordPress::get_user_context( $transaction->user_id );
5977 $context['response_type'] = 'live';
5978 }
5979 }
5980
5981 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5982 return $context;
5983 }
5984
5985 /**
5986 * Get last data for trigger.
5987 *
5988 * @param array $data data.
5989 * @return array
5990 */
5991 public function search_wishlist_member_triggers_last_data( $data ) {
5992 global $wpdb;
5993
5994 $context = [];
5995 $context['response_type'] = 'sample';
5996
5997 $user_data = WordPress::get_sample_user_context();
5998
5999 $membership_data = [
6000 'membership_level_id' => '1',
6001 'membership_level_name' => 'Sample Membership Level',
6002 ];
6003
6004 $membership_level_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
6005
6006 if ( $membership_level_id > 0 ) {
6007 $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 ) );
6008 } else {
6009 $membership = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}wlm_userlevels ORDER BY id DESC LIMIT 1" );
6010 }
6011 if ( ! empty( $membership ) ) {
6012 $membership_data = WishlistMember::get_membership_detail_context( (int) $membership->level_id, (int) $membership->user_id );
6013 $user_data = WordPress::get_user_context( $membership->user_id );
6014
6015 $context['response_type'] = 'live';
6016 }
6017
6018 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
6019
6020 return $context;
6021 }
6022
6023 /**
6024 * Get last data for trigger.
6025 *
6026 * @param array $data data.
6027 * @return array
6028 */
6029 public function search_peepso_triggers_last_data( $data ) {
6030 global $wpdb;
6031
6032 $context = [];
6033 $context['response_type'] = 'sample';
6034
6035 $user_data = WordPress::get_sample_user_context();
6036
6037 $post_data = [
6038 'post_id' => '1',
6039 'activity_id' => '2',
6040 'post_author' => '1',
6041 'post_content' => 'New sample post...!',
6042 'post_title' => 'Sample Post',
6043 'post_excerpt' => 'sample',
6044 'post_status' => 'publish',
6045 'post_type' => 'peepso-post',
6046 ];
6047
6048 $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" );
6049
6050 if ( ! empty( $post ) ) {
6051 $post_data = PeepSo::get_pp_activity_context( (int) $post->act_external_id, (int) $post->act_id );
6052 $user_data = WordPress::get_user_context( $post->act_owner_id );
6053
6054 $context['response_type'] = 'live';
6055 }
6056
6057 $context['pluggable_data'] = array_merge( $user_data, $post_data );
6058
6059 return $context;
6060 }
6061
6062 /**
6063 * Get last data for Peepso User triggers.
6064 *
6065 * @param array $data data.
6066 * @return array
6067 */
6068 public function search_peepso_user_triggers_last_data( $data ) {
6069 global $wpdb;
6070
6071 $context = [];
6072 $context['response_type'] = 'sample';
6073 $term = $data['search_term'];
6074
6075 if ( ! class_exists( 'PeepSoUser' ) ) {
6076 return [];
6077 }
6078
6079 if ( 'user_follows_member' === $term || 'user_gains_follower' === $term ) {
6080 $member_id = $data['filter']['follow_user_id']['value'];
6081 if ( -1 === $member_id ) {
6082 $followers = $wpdb->get_results(
6083 $wpdb->prepare(
6084 "SELECT * FROM
6085 {$wpdb->prefix}peepso_user_followers WHERE uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
6086 1
6087 )
6088 );
6089 } else {
6090 $followers = $wpdb->get_results(
6091 $wpdb->prepare(
6092 "SELECT * FROM
6093 {$wpdb->prefix}peepso_user_followers WHERE uf_passive_user_id = %d AND
6094 uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
6095 $member_id,
6096 1
6097 )
6098 );
6099 }
6100 } elseif ( 'user_unfollows_member' === $term || 'user_loses_follower' === $term ) {
6101 $member_id = $data['filter']['follow_user_id']['value'];
6102 if ( -1 === $member_id ) {
6103 $followers = $wpdb->get_results(
6104 $wpdb->prepare(
6105 "SELECT * FROM
6106 {$wpdb->prefix}peepso_user_followers WHERE uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
6107 0
6108 )
6109 );
6110 } else {
6111 $followers = $wpdb->get_results(
6112 $wpdb->prepare(
6113 "SELECT * FROM
6114 {$wpdb->prefix}peepso_user_followers WHERE uf_passive_user_id = %d AND
6115 uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
6116 $member_id,
6117 0
6118 )
6119 );
6120 }
6121 } elseif ( 'user_updates_avatar' === $term ) {
6122 $followers = $wpdb->get_results(
6123 $wpdb->prepare(
6124 "SELECT * FROM
6125 {$wpdb->prefix}peepso_users WHERE usr_avatar_custom = %d AND usr_role = %s
6126 ORDER BY usr_last_activity DESC LIMIT 1",
6127 1,
6128 'member'
6129 )
6130 );
6131 } elseif ( 'user_updates_specific_profile_field' === $term ) {
6132 $field_id = $data['filter']['user_profile_field_id']['value'];
6133 $followers = $wpdb->get_results(
6134 $wpdb->prepare(
6135 "SELECT * FROM
6136 {$wpdb->prefix}peepso_users WHERE usr_role = %s
6137 ORDER BY usr_last_activity DESC LIMIT 1",
6138 'member'
6139 )
6140 );
6141 }
6142 if ( 'user_updates_avatar' === $term ) {
6143 if ( ! empty( $followers ) ) {
6144 $context_data['user'] = WordPress::get_user_context( $followers[0]->usr_id );
6145 $context['response_type'] = 'live';
6146 } else {
6147 $context_data['user'] = WordPress::get_sample_user_context();
6148 }
6149 } elseif ( 'user_updates_specific_profile_field' === $term ) {
6150 if ( ! empty( $followers ) ) {
6151 $user = PeepSoUser::get_instance( $followers[0]->usr_id );
6152 $user->profile_fields->load_fields();
6153 $user_fields = $user->profile_fields->get_fields();
6154 foreach ( $user_fields as $key => $value ) {
6155 $val = get_user_meta( $followers[0]->usr_id, $value->key, true );
6156 if ( '' != $val ) {
6157 $context_data[ $value->title ] = $val;
6158 }
6159 }
6160 $curruser = get_userdata( $followers[0]->usr_id );
6161 $context_data['user_id'] = $followers[0]->usr_id;
6162 $context_data['user_email'] = $user->get_email();
6163 $context_data['avatar_url'] = $user->get_avatar();
6164 $context_data['profile_url'] = $user->get_profileurl();
6165 $context_data['about_me'] = get_user_meta( $followers[0]->usr_id, 'description', true );
6166 if ( $curruser instanceof \WP_User ) {
6167 $context_data['website'] = $curruser->user_url;
6168 }
6169 $context_data['role'] = $user->get_user_role();
6170 $context['response_type'] = 'live';
6171 } else {
6172 $context_data['user'] = WordPress::get_sample_user_context();
6173 }
6174 } else {
6175 if ( ! empty( $followers ) ) {
6176 $context_data['follower_user'] = WordPress::get_user_context( $followers[0]->uf_active_user_id );
6177 $context_data['following_user'] = WordPress::get_user_context( $followers[0]->uf_passive_user_id );
6178 $context['response_type'] = 'live';
6179 } else {
6180 $context_data['follower_user'] = WordPress::get_sample_user_context();
6181 $context_data['following_user'] = WordPress::get_sample_user_context();
6182 }
6183 }
6184
6185 $context['pluggable_data'] = $context_data;
6186
6187 return $context;
6188 }
6189
6190 /**
6191 * Search Peepso User Profile Fields list.
6192 *
6193 * @param array $data data.
6194 * @return array
6195 */
6196 public function search_peepso_profile_field_list( $data ) {
6197 $options = [];
6198 if ( ! class_exists( 'PeepSoUser' ) ) {
6199 return [];
6200 }
6201 $options = [
6202 [
6203 'label' => 'Allow others to "like" my profile',
6204 'value' => 'peepso_is_profile_likable',
6205 ],
6206 [
6207 'label' => 'Hide my birthday year',
6208 'value' => 'peepso_hide_birthday_year',
6209 ],
6210 [
6211 'label' => 'Who can see my profile',
6212 'value' => 'usr_profile_acc',
6213 ],
6214 [
6215 'label' => 'Who can post on my profile',
6216 'value' => 'peepso_profile_post_acc',
6217 ],
6218 [
6219 'label' => "Don't show my online status",
6220 'value' => 'peepso_hide_online_status',
6221 ],
6222 [
6223 'label' => 'My timezone',
6224 'value' => 'peepso_gmt_offset',
6225 ],
6226 ];
6227 $peepsouser = PeepSoUser::get_instance( 0 );
6228 $peepsouser->profile_fields->load_fields();
6229 $fields = $peepsouser->profile_fields->get_fields();
6230 foreach ( $fields as $field ) {
6231 if ( 1 == $field->published ) {
6232 $options[] = [
6233 'label' => $field->title,
6234 'value' => $field->id,
6235 ];
6236 }
6237 }
6238 return [
6239 'options' => $options,
6240 'hasMore' => false,
6241 ];
6242 }
6243
6244 /**
6245 * Search Peepso Roles list.
6246 *
6247 * @param array $data data.
6248 * @return array
6249 */
6250 public function search_peepso_role_list( $data ) {
6251 $options = [
6252 [
6253 'label' => 'Community Member',
6254 'value' => 'member',
6255 ],
6256 [
6257 'label' => 'Community Moderator',
6258 'value' => 'moderator',
6259 ],
6260 [
6261 'label' => 'Community Administrator',
6262 'value' => 'admin',
6263 ],
6264 [
6265 'label' => 'Banned',
6266 'value' => 'ban',
6267 ],
6268 [
6269 'label' => 'Pending user email verification',
6270 'value' => 'register',
6271 ],
6272 [
6273 'label' => 'Pending admin approval',
6274 'value' => 'verified',
6275 ],
6276 ];
6277 return [
6278 'options' => $options,
6279 'hasMore' => false,
6280 ];
6281 }
6282
6283 /**
6284 * Search Peepso Users list.
6285 *
6286 * @param array $data data.
6287 * @return array
6288 */
6289 public function search_peepso_follow_user_list( $data ) {
6290 $options = [];
6291 global $wpdb;
6292 $users = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}peepso_users", ARRAY_A );
6293 if ( count( $users ) > 0 ) {
6294 foreach ( $users as $user ) {
6295 $user_by_id = get_user_by( 'id', $user['usr_id'] );
6296 if ( $user_by_id instanceof \WP_User ) {
6297 $options[] = [
6298 'label' => sprintf( '%s %s [%s]', $user_by_id->last_name, $user_by_id->first_name, $user_by_id->user_email ),
6299 'value' => $user['usr_id'],
6300 ];
6301 } else {
6302 $options[] = [
6303 'label' => '#' . $user['usr_id'],
6304 'value' => $user['usr_id'],
6305 ];
6306 }
6307 }
6308 }
6309 return [
6310 'options' => $options,
6311 'hasMore' => false,
6312 ];
6313 }
6314
6315 /**
6316 * Get last data for trigger
6317 *
6318 * @param array $data data.
6319 * @return array
6320 */
6321 public function search_restrict_content_pro_triggers_last_data( $data ) {
6322 $context = [];
6323 if ( ! function_exists( 'rcp_get_memberships' ) ) {
6324 return [];
6325 }
6326 $context['response_type'] = 'sample';
6327
6328 $user_data = WordPress::get_sample_user_context();
6329
6330 $membership_data = [
6331 'membership_level_id' => '190',
6332 'membership_level' => 'Sample Membership',
6333 'membership_initial_payment' => '0.00',
6334 'membership_recurring_payment' => '0.00',
6335 'membership_expiry_date' => 'January 22, 2023',
6336 ];
6337
6338 $customer_id = (int) ( isset( $data['filter']['membership_customer_id']['value'] ) ? $data['filter']['membership_customer_id']['value'] : '-1' );
6339 $membership_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
6340
6341 $args = [];
6342 if ( 'membership_purchased' === $data['search_term'] ) {
6343 $args = [
6344 'status' => 'active',
6345 'number' => 1,
6346 'orderby' => 'id',
6347 ];
6348 } elseif ( 'membership_cancelled' === $data['search_term'] ) {
6349 $args = [
6350 'status' => 'cancelled',
6351 'number' => 1,
6352 'orderby' => 'id',
6353 ];
6354 } elseif ( 'membership_expired' === $data['search_term'] ) {
6355 $args = [
6356 'status' => 'expired',
6357 'number' => 1,
6358 'orderby' => 'id',
6359 ];
6360 }
6361
6362 if ( 'membership_expired' === $data['search_term'] && -1 !== $customer_id ) {
6363 $args['customer_id'] = $customer_id;
6364 }
6365
6366 if ( -1 !== $membership_id ) {
6367 $args['object_id'] = $membership_id;
6368 }
6369
6370 $memberships = rcp_get_memberships( $args );
6371 if ( count( $memberships ) > 0 ) {
6372 $membership_data = RestrictContent::get_rcp_membership_detail_context( $memberships[0] );
6373 $user_data = WordPress::get_user_context( $memberships[0]->get_user_id() );
6374
6375 $context['response_type'] = 'live';
6376 }
6377
6378 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
6379
6380 return $context;
6381 }
6382
6383 /**
6384 * Get last data for trigger
6385 *
6386 * @param array $data data.
6387 * @return array
6388 */
6389 public function search_events_calendar_triggers_last_data( $data ) {
6390 $context = [];
6391 $context['response_type'] = 'sample';
6392
6393 $event_data = [
6394 'event_id' => 1,
6395 'event' => [
6396 'ID' => 58,
6397 'post_author' => 1,
6398 'post_date' => '2023-01-19 09:27:58',
6399 'post_date_gmt' => '2023-01-19 09:27:58',
6400 'post_content' => '',
6401 'post_title' => 'New event',
6402 'post_excerpt' => '',
6403 'post_status' => 'publish',
6404 'comment_status' => 'open',
6405 'ping_status' => 'closed',
6406 'post_password' => '',
6407 'post_name' => 'new-event',
6408 'to_ping' => '',
6409 'pinged' => '',
6410 'post_modified' => '2023-01-19 09:44:25',
6411 'post_modified_gmt' => '2023-01-19 09:44:25',
6412 'post_content_filtered' => '',
6413 'post_parent' => 0,
6414 'guid' => 'http://connector.com/?post_type=tribe_events&#038;p=58',
6415 'menu_order' => -1,
6416 'post_type' => 'tribe_events',
6417 'post_mime_type' => '',
6418 'comment_count' => 0,
6419 'filter' => 'raw',
6420 ],
6421 'attendies' => [
6422 'order_id' => 68,
6423 'purchaser_name' => 'John Doe',
6424 'purchaser_email' => 'john@test.com',
6425 'provider' => 'Tribe__Tickets__RSVP',
6426 'provider_slug' => 'rsvp',
6427 'purchase_time' => '2023-01-19 09:48:43',
6428 'optout' => 1,
6429 'ticket' => 'Prime',
6430 'attendee_id' => 68,
6431 'security' => '2cefc3b53e',
6432 'product_id' => 65,
6433 'check_in' => '',
6434 'order_status' => 'yes',
6435 'order_status_label' => 'Going',
6436 'user_id' => 1,
6437 'ticket_sent' => 1,
6438 'event_id' => 58,
6439 'ticket_name' => 'Prime',
6440 'holder_name' => 'John Doe',
6441 'holder_email' => 'john@test.com',
6442 'ticket_id' => 68,
6443 'qr_ticket_id' => 68,
6444 'security_code' => '2cefc3b53e',
6445 'attendee_meta' => '',
6446 'is_subscribed' => '',
6447 'is_purchaser' => 1,
6448 'ticket_exists' => 1,
6449 ],
6450 ];
6451
6452 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
6453 $term = $data['search_term'];
6454
6455 if ( 'event_register' === $term || 'attendee_registered_event' === $term ) {
6456 if ( -1 === $event_id ) {
6457 $args = [
6458 'numberposts' => 1,
6459 'orderby' => 'rand',
6460 'post_type' => 'tribe_events',
6461 ];
6462 $posts = get_posts( $args );
6463 $event_id = $posts[0]->ID;
6464 }
6465 $args = [
6466 'post_type' => 'tribe_rsvp_attendees',
6467 'orderby' => 'ID',
6468 'order' => 'DESC',
6469 'post_status' => 'publish',
6470 'numberposts' => 1,
6471 ];
6472
6473 if ( -1 !== $event_id ) {
6474 $args['meta_query'] = [
6475 [
6476 'key' => '_tribe_rsvp_event',
6477 'value' => $event_id,
6478 ],
6479 ];
6480 }
6481
6482 $attendees = get_posts( $args );
6483
6484 if ( count( $attendees ) > 0 ) {
6485 $attendee = $attendees[0];
6486 $attendee_id = $attendee->ID;
6487
6488 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
6489 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
6490 if ( is_numeric( $product_id ) && is_numeric( $order_id ) ) {
6491 $event_context = TheEventCalendar::get_event_context( (int) $product_id, (int) $order_id );
6492 }
6493
6494 if ( ! empty( $event_context ) ) {
6495 $event_data = $event_context;
6496 $context['response_type'] = 'live';
6497 }
6498 } else {
6499 $args = [
6500 'post_type' => 'tec_tc_attendee',
6501 'orderby' => 'ID',
6502 'order' => 'DESC',
6503 'post_status' => 'publish',
6504 'numberposts' => 1,
6505 ];
6506 if ( -1 !== $event_id ) {
6507 $args['meta_query'] = [
6508 [
6509 'key' => '_tec_tickets_commerce_event',
6510 'value' => $event_id,
6511 ],
6512 ];
6513 }
6514 $attendees = get_posts( $args );
6515 $attendee = $attendees[0];
6516 $attendee_id = $attendee->ID;
6517 $product_id = get_post_meta( $attendee_id, '_tec_tickets_commerce_ticket', true );
6518 $order_id = $attendee_id;
6519 if ( is_numeric( $product_id ) ) {
6520 $event_context = TheEventCalendar::get_event_context( (int) $product_id, $order_id );
6521 }
6522 if ( ! empty( $event_context ) ) {
6523 $event_data = $event_context;
6524 $context['response_type'] = 'live';
6525 }
6526 }
6527 } elseif ( 'event_attends' === $term ) {
6528 if ( -1 == $event_id ) {
6529 $args = [
6530 'numberposts' => 1,
6531 'orderby' => 'rand',
6532 'post_type' => 'tribe_events',
6533 ];
6534 $posts = get_posts( $args );
6535 $event_id = $posts[0]->ID;
6536 }
6537 $args = [
6538 'post_type' => 'tribe_rsvp_attendees',
6539 'orderby' => 'ID',
6540 'order' => 'DESC',
6541 'post_status' => 'publish',
6542 'numberposts' => 1,
6543 'meta_query' => [
6544 'relation' => 'AND',
6545 [
6546 'key' => '_tribe_rsvp_checkedin',
6547 'value' => 1,
6548 'compare' => '=',
6549 ],
6550 [
6551 'key' => '_tribe_rsvp_event',
6552 'value' => $event_id,
6553 'compare' => '=',
6554 ],
6555 ],
6556 ];
6557 $attendees = get_posts( $args );
6558 if ( ! function_exists( 'tribe_tickets_get_attendees' ) ) {
6559 return [];
6560 }
6561 if ( ! empty( $attendees ) ) {
6562 $attendee = $attendees[0];
6563 $attendee_id = $attendee->ID;
6564 /**
6565 *
6566 * Ignore line
6567 *
6568 * @phpstan-ignore-next-line
6569 */
6570 $attendee_details = tribe_tickets_get_attendees( $attendee_id, 'rsvp_order' );
6571 foreach ( $attendee_details as $detail ) {
6572 if ( (int) $detail['attendee_id'] !== (int) $attendee_id ) {
6573 continue;
6574 }
6575 $attendee = $detail;
6576 }
6577 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
6578 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
6579 if ( is_numeric( $product_id ) && is_numeric( $order_id ) ) {
6580 $event_context = TheEventCalendar::get_event_context( (int) $product_id, (int) $order_id );
6581 }
6582 if ( ! empty( $event_context ) ) {
6583 $event_data = array_merge( $attendee, $event_context );
6584 $context['response_type'] = 'live';
6585 }
6586 } else {
6587 $order = [
6588 'order_id' => 7962,
6589 'purchaser_name' => 'bella4 bella4',
6590 'purchaser_email' => 'bella4@yopmail.com',
6591 'provider' => 'Tribe__Tickets__RSVP',
6592 'provider_slug' => 'rsvp',
6593 'purchase_time' => '2024 - 03 - 04 07:26:41',
6594 'optout' => 1,
6595 'ticket' => 'test test',
6596 'attendee_id' => 7962,
6597 'security' => 'eb3a2d7bc4',
6598 'product_id' => 7959,
6599 'check_in' => 1,
6600 'order_status' => 'yes',
6601 'order_status_label' => 'Going',
6602 'user_id' => 35,
6603 'ticket_sent' => 1,
6604 'event_id' => 7956,
6605 'ticket_name' => 'test test',
6606 'holder_name' => 'bella4 bella4',
6607 'holder_email' => 'bella4@yopmail.com',
6608 'ticket_id' => 7962,
6609 'qr_ticket_id' => 7962,
6610 'security_code' => 'eb3a2d7bc4',
6611 'is_purchaser' => 1,
6612 'ticket_exists' => 1,
6613 ];
6614 $event_data = array_merge( $order, $event_data );
6615 }
6616 } elseif ( 'event_attendee_wc_register' === $term ) {
6617 if ( ! class_exists( 'Tribe__Tickets__Tickets' ) ) {
6618 return [];
6619 }
6620 if ( -1 === $event_id ) {
6621 $args = [
6622 'numberposts' => 1,
6623 'orderby' => 'rand',
6624 'post_type' => 'tribe_events',
6625 ];
6626 $posts = get_posts( $args );
6627 $event_id = $posts[0]->ID;
6628 }
6629 $event_ticket_id = (int) ( isset( $data['filter']['event_ticket_id']['value'] ) ? $data['filter']['event_ticket_id']['value'] : '-1' );
6630 if ( -1 === $event_ticket_id ) {
6631 $tickets = \Tribe__Tickets__Tickets::get_all_event_tickets( $event_id );
6632 $random_key = array_rand( $tickets );
6633 $random_value = $tickets[ $random_key ];
6634 $event_ticket_id = $random_value;
6635 }
6636 $args = [
6637 'post_type' => 'tribe_wooticket',
6638 'orderby' => 'ID',
6639 'order' => 'DESC',
6640 'post_status' => 'publish',
6641 'meta_query' => [
6642 'relation' => 'AND',
6643 [
6644 'key' => '_tribe_wooticket_event',
6645 'value' => $event_id,
6646 'compare' => '=',
6647 ],
6648 [
6649 'key' => '_tribe_wooticket_product',
6650 'value' => $event_ticket_id,
6651 'compare' => '=',
6652 ],
6653 ],
6654 ];
6655
6656 $woo_attendees = get_posts( $args );
6657 if ( ! empty( $woo_attendees ) ) {
6658 $attendee = $woo_attendees[0];
6659 $attendee_id = $attendee->ID;
6660 /**
6661 *
6662 * Ignore line
6663 *
6664 * @phpstan-ignore-next-line
6665 */
6666 $attendee_details = tribe_tickets_get_attendees( $attendee_id, 'rsvp_order' );
6667 foreach ( $attendee_details as $detail ) {
6668 if ( (int) $detail['attendee_id'] !== (int) $attendee_id ) {
6669 continue;
6670 }
6671 $attendee = $detail;
6672 }
6673 $order_id = get_post_meta( $attendee_id, '_tribe_wooticket_order', true );
6674 $product_id = get_post_meta( $attendee_id, '_tribe_wooticket_product', true );
6675 if ( is_numeric( $product_id ) && is_numeric( $order_id ) ) {
6676 $event_context = TheEventCalendar::get_event_context( (int) $product_id, (int) $order_id );
6677 }
6678 if ( ! empty( $event_context ) ) {
6679 $event_data = $event_context;
6680 $context['response_type'] = 'live';
6681 }
6682 $context['response_type'] = 'live';
6683 }
6684 }
6685
6686 $context['pluggable_data'] = $event_data;
6687
6688 return $context;
6689 }
6690
6691 /**
6692 * Get last data for trigger
6693 *
6694 * @param array $data data.
6695 * @return array
6696 */
6697 public function search_woo_commerce_triggers_last_data( $data ) {
6698 $context = [];
6699 $context['response_type'] = 'sample';
6700 $context['pluggable_data'] = [];
6701 $user_data = WordPress::get_sample_user_context();
6702
6703 $product_data['product'] = [
6704 'id' => '169',
6705 'name' => 'Sample Product',
6706 'description' => 'This is description of sample product.',
6707 'short_description' => 'This is short description of sample product.',
6708 'image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
6709 'slug' => 'sample-product',
6710 'status' => 'publish',
6711 'type' => 'simple',
6712 'price' => '89',
6713 'featured' => '0',
6714 'sku' => 'hoodie-blue-sm',
6715 'regular_price' => '90',
6716 'sale_price' => '89',
6717 'total_sales' => '21',
6718 'category' => 'Uncategorized',
6719 'tags' => 'sample, new, 2022',
6720 ];
6721
6722 $comment_data = [
6723 'comment_id' => '1',
6724 'comment' => 'This is a sample comment..!',
6725 'comment_author' => 'testsure',
6726 'comment_date' => '2023-06-23 10:10:40',
6727 'comment_author_email' => 'testsure@example.com',
6728 ];
6729
6730 $order_data = [
6731 'order_id' => '500',
6732 'total_order_value' => '45',
6733 'currency' => 'USD',
6734 'shipping_total' => '5',
6735 'order_payment_method' => 'cod',
6736 'billing_firstname' => 'John',
6737 'billing_lastname' => 'Doe',
6738 'billing_company' => 'BSF',
6739 'billing_address_1' => '1004 Beaumont',
6740 'billing_address_2' => '',
6741 'billing_city' => 'Casper',
6742 'billing_state' => 'Wyoming',
6743 'billing_state_name' => 'Wyoming',
6744 'billing_postcode' => '82601',
6745 'billing_country' => 'US',
6746 'billing_country_name' => 'US',
6747 'billing_email' => 'john_doe@gmail.com',
6748 'billing_phone' => '(307) 7626541',
6749 'shipping_firstname' => 'John',
6750 'shipping_lastname' => 'Doe',
6751 'shipping_company' => 'BSF',
6752 'shipping_address_1' => '1004 Beaumont',
6753 'shipping_address_2' => '',
6754 'shipping_city' => 'Casper',
6755 'shipping_state' => 'Wyoming',
6756 'shipping_state_name' => 'Wyoming',
6757 'shipping_postcode' => '82601',
6758 'shipping_country' => 'US',
6759 'shipping_country_name' => 'US',
6760 'coupon_codes' => 'e3mstekq, f24sjakb',
6761 'total_items_in_order' => '1',
6762 'user_id' => '1',
6763 ];
6764
6765 $variation_data = [
6766 'product_variation_id' => '626',
6767 'product_variation' => 'Color: Silver',
6768 ];
6769
6770 $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
6771
6772 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
6773 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
6774
6775 $order_status = ( isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1 );
6776 $from_order_status = ( isset( $data['filter']['from_status']['value'] ) ? $data['filter']['from_status']['value'] : -1 );
6777 $order_note_type = ( isset( $data['filter']['note_type']['value'] ) ? $data['filter']['note_type']['value'] : -1 );
6778
6779 if ( in_array( $term, [ 'product_added_to_cart', 'product_viewed' ], true ) ) {
6780 if ( -1 === $product_id ) {
6781 $args = [
6782 'post_type' => 'product',
6783 'orderby' => 'ID',
6784 'order' => 'DESC',
6785 'post_status' => 'publish',
6786 'numberposts' => 1,
6787 ];
6788 $products = get_posts( $args );
6789
6790 if ( count( $products ) > 0 ) {
6791 $product_id = $products[0]->ID;
6792 }
6793 }
6794
6795 if ( -1 !== $product_id ) {
6796 $post = get_post( $product_id );
6797 if ( $post instanceof \WP_Post ) {
6798 $user_data = WordPress::get_user_context( (int) $post->post_author );
6799 }
6800 $product_data['product_id'] = $product_id;
6801 $product_data['product'] = WooCommerce::get_product_context( $product_id );
6802 $terms = get_the_terms( $product_id, 'product_cat' );
6803 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6804 $cat_name = [];
6805 foreach ( $terms as $cat ) {
6806 $cat_name[] = $cat->name;
6807 }
6808 $product_data['product']['category'] = implode( ', ', $cat_name );
6809 }
6810 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6811 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6812 $tag_name = [];
6813 foreach ( $terms_tags as $tag ) {
6814 $tag_name[] = $tag->name;
6815 }
6816 $product_data['product']['tag'] = implode( ', ', $tag_name );
6817 }
6818 unset( $product_data['product']['id'] ); //phpcs:ignore
6819 $context['response_type'] = 'live';
6820 }
6821
6822 if ( 'product_added_to_cart' === $term ) {
6823 $product_data['product_quantity'] = 1;
6824 }
6825
6826 $context['pluggable_data'] = array_merge( $product_data, $user_data );
6827
6828 } elseif ( 'product_reviewed' === $term ) {
6829 $comment_args = [
6830 'number' => 1,
6831 'type' => 'review',
6832 'orderby' => 'comment_ID',
6833 'post_id' => -1 !== $product_id ? $product_id : 0,
6834 ];
6835
6836 $comments = get_comments( $comment_args );
6837
6838 if ( is_array( $comments ) && count( $comments ) > 0 ) {
6839 $comment = $comments[0];
6840 if ( is_object( $comment ) ) {
6841 $comment_data = [
6842 'comment_id' => $comment->comment_ID,
6843 'comment' => $comment->comment_content,
6844 'comment_author' => $comment->comment_author,
6845 'comment_date' => $comment->comment_date,
6846 'comment_author_email' => $comment->comment_author_email,
6847 ];
6848 $product_data = WooCommerce::get_product_context( (int) $comment->comment_post_ID );
6849 $terms = get_the_terms( (int) $comment->comment_post_ID, 'product_cat' );
6850 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6851 $cat_name = [];
6852 foreach ( $terms as $cat ) {
6853 $cat_name[] = $cat->name;
6854 }
6855 $product_data['product']['category'] = implode( ', ', $cat_name );
6856 }
6857 $terms_tags = get_the_terms( (int) $comment->comment_post_ID, 'product_tag' );
6858 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6859 $tag_name = [];
6860 foreach ( $terms_tags as $tag ) {
6861 $tag_name[] = $tag->name;
6862 }
6863 $product_data['product']['tag'] = implode( ', ', $tag_name );
6864 }
6865 }
6866 if ( $comment instanceof \WP_Comment ) {
6867 $user_data = WordPress::get_user_context( (int) $comment->user_id );
6868 }
6869 $context['response_type'] = 'live';
6870 }
6871
6872 $context['pluggable_data'] = array_merge( $product_data, $user_data, $comment_data );
6873
6874 } elseif ( 'product_purchased' === $term ) {
6875 $order_id = 0;
6876 $product_data['quantity'] = '1';
6877 if ( -1 !== $product_id ) {
6878 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
6879 if ( count( $order_ids ) > 0 ) {
6880 $order_id = $order_ids[0];
6881 }
6882 } else {
6883 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
6884 if ( is_array( $orders ) && count( $orders ) > 0 ) {
6885 $order_id = $orders[0]->get_id();
6886 }
6887 }
6888
6889 if ( 0 !== $order_id ) {
6890 $order = wc_get_order( $order_id );
6891
6892 if ( $order && $order instanceof \WC_Order ) {
6893 $user_id = $order->get_customer_id();
6894 $items = $order->get_items();
6895
6896 $product_ids = [];
6897
6898 $iteration = 0;
6899 foreach ( $items as $item ) {
6900 if ( method_exists( $item, 'get_product_id' ) ) {
6901 $item_id = $item->get_product_id();
6902 if ( -1 === $product_id && 0 === $iteration ) {
6903 $product_ids[] = $item_id;
6904 break;
6905 } elseif ( $item_id === $product_id ) {
6906 $product_ids[] = $item_id;
6907 break;
6908 }
6909 }
6910
6911 $iteration++;
6912 }
6913 $order_data = WooCommerce::get_order_context( $order_id );
6914 $user_data = WordPress::get_user_context( $user_id );
6915 $order_data['total_items_in_order'] = count( $product_ids );
6916 $product_data = [];
6917 foreach ( $product_ids as $key => $product_id ) {
6918 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
6919 $terms = get_the_terms( $product_id, 'product_cat' );
6920 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6921 $cat_name = [];
6922 foreach ( $terms as $cat ) {
6923 $cat_name[] = $cat->name;
6924 }
6925 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
6926 }
6927 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6928 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6929 $tag_name = [];
6930 foreach ( $terms_tags as $tag ) {
6931 $tag_name[] = $tag->name;
6932 }
6933 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
6934 }
6935 $product = wc_get_product( $product_id );
6936 /**
6937 *
6938 * Ignore line
6939 *
6940 * @phpstan-ignore-next-line
6941 */
6942 if ( $product->is_downloadable() ) {
6943 /**
6944 *
6945 * Ignore line
6946 *
6947 * @phpstan-ignore-next-line
6948 */
6949 foreach ( $product->get_downloads() as $key_download_id => $download ) {
6950 $download_name = $download->get_name();
6951 $download_link = $download->get_file();
6952 $download_id = $download->get_id();
6953 $download_type = $download->get_file_type();
6954 $download_ext = $download->get_file_extension();
6955 $product_data[ 'product' . $key ]['download'] = [
6956 'download_name' => $download_name,
6957 'download_link' => $download_link,
6958 'download_id' => $download_id,
6959 'download_type' => $download_type,
6960 'download_ext' => $download_ext,
6961 ];
6962 }
6963 }
6964 }
6965 $context['response_type'] = 'live';
6966 }
6967 }
6968
6969 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
6970
6971 } elseif ( 'variable_product_purchased' === $term ) {
6972 $product_variation_id = (int) ( isset( $data['filter']['product_variation_id']['value'] ) ? $data['filter']['product_variation_id']['value'] : -1 );
6973 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
6974
6975 foreach ( $order_ids as $order_id ) {
6976 $order = wc_get_order( $order_id );
6977
6978 if ( $order && $order instanceof \WC_Order ) {
6979 $user_id = $order->get_customer_id();
6980 $items = $order->get_items();
6981 $product_variations = [];
6982
6983 $iteration = 0;
6984 foreach ( $items as $item ) {
6985 if ( method_exists( $item, 'get_variation_id' ) ) {
6986 $variation_id = $item->get_variation_id();
6987 if ( -1 === $product_variation_id && 0 === $iteration ) {
6988 $product_variations[] = $variation_id;
6989 break;
6990 } elseif ( $variation_id === $product_variation_id ) {
6991 $product_variations[] = $variation_id;
6992 break;
6993 }
6994 }
6995
6996 $iteration++;
6997 }
6998
6999 if ( count( $product_variations ) > 0 ) {
7000 $product_data = WooCommerce::get_product_context( $product_variation_id );
7001 $order_data = WooCommerce::get_order_context( $order_id );
7002 $user_data = WordPress::get_user_context( $user_id );
7003 $variation_data = [
7004 'product_variation_id' => $product_variations[0],
7005 'product_variation' => get_the_excerpt( $product_variations[0] ),
7006 ];
7007
7008 $context['response_type'] = 'live';
7009 break;
7010 }
7011 }
7012 }
7013
7014 $context['pluggable_data'] = array_merge(
7015 isset( $order_data ) ? $order_data : [],
7016 $user_data,
7017 $variation_data
7018 );
7019
7020 } elseif ( 'variable_subscription_purchased' === $term ) {
7021 $product_data['quantity'] = '1';
7022 $product_data['product_name'] = 'Sample Product';
7023 $product_data['billing_period'] = '2021-2022';
7024
7025 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
7026
7027 $subscription_order_id = 0;
7028 $order_ids = [];
7029
7030 if ( -1 !== $product_id ) {
7031 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
7032
7033 } else {
7034 $orders = wc_get_orders( [] );
7035 if ( is_array( $orders ) && count( $orders ) > 0 ) {
7036 $order_ids[] = $orders[0]->get_id();
7037 }
7038 }
7039
7040 foreach ( $order_ids as $order_id ) {
7041 $query_args = [
7042 'post_type' => 'shop_subscription',
7043 'orderby' => 'ID',
7044 'order' => 'DESC',
7045 'post_status' => 'wc-active',
7046 'posts_per_page' => 1,
7047 'post_parent' => $order_id,
7048 ];
7049 $query_result = new WP_Query( $query_args );
7050 $subscription_orders = $query_result->get_posts();
7051
7052 if ( count( $subscription_orders ) > 0 ) {
7053 if ( $subscription_orders[0] instanceof \WP_Post ) {
7054 $subscription_order_id = $subscription_orders[0]->ID;
7055 }
7056 break;
7057 }
7058 }
7059
7060 if ( 0 !== $subscription_order_id ) {
7061 if ( function_exists( 'wcs_get_subscription' ) ) {
7062 $subscription = wcs_get_subscription( $subscription_order_id );
7063 if ( class_exists( 'WC_Subscription' ) && $subscription instanceof WC_Subscription ) {
7064 $last_order_id = $subscription->get_last_order();
7065 if ( ! empty( $last_order_id ) && $last_order_id === $subscription->get_parent_id() ) {
7066 $last_order_data = wc_get_order( $last_order_id );
7067 if ( $last_order_data instanceof \WC_Order ) {
7068 $user_id = $last_order_data->get_customer_id();
7069 }
7070 $items = $subscription->get_items();
7071
7072 foreach ( $items as $item ) {
7073 $product = $item->get_product();
7074 if ( class_exists( '\WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product ) ) {
7075 if ( $product->is_type( [ 'subscription', 'subscription_variation', 'variable-subscription' ] ) ) {
7076
7077 $product_data = WooCommerce::get_variable_subscription_product_context( $item, $last_order_id );
7078 if ( isset( $user_id ) ) {
7079 $user_data = WordPress::get_user_context( $user_id );
7080 }
7081
7082 $context['response_type'] = 'live';
7083 $context['pluggable_data'] = array_merge( $product_data, $user_data );
7084 }
7085 }
7086 }
7087 }
7088 }
7089 }
7090 }
7091 } elseif ( 'order_created' === $term ) {
7092 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
7093 $order_id = '';
7094 if ( is_array( $orders ) && count( $orders ) > 0 ) {
7095 $order_id = $orders[0]->get_id();
7096 $order = wc_get_order( $order_id );
7097 if ( $order instanceof \WC_Order ) {
7098 $user_id = $order->get_customer_id();
7099 $order_context = WooCommerce::get_order_context( $order_id );
7100 $order_sample_data = array_merge(
7101 isset( $order_context ) ? $order_context : [],
7102 WordPress::get_user_context( $user_id )
7103 );
7104 $context['response_type'] = 'live';
7105 }
7106 }
7107
7108 // Add dummy coupon details if no coupons exist in the order.
7109 if ( ! isset( $order_sample_data ) ) {
7110 $order_sample_data = [];
7111 }
7112 if ( ! isset( $order_sample_data['coupons'] ) || empty( $order_sample_data['coupons'] ) ) {
7113 $order_sample_data['coupons'] = [
7114 [
7115 'code_name' => 'SAMPLE10',
7116 'discount_type' => 'percent',
7117 'coupon_amount' => '10',
7118 'meta_data' => [],
7119 ],
7120 [
7121 'code_name' => 'SAMPLE20',
7122 'discount_type' => 'fixed_cart',
7123 'coupon_amount' => '20',
7124 'meta_data' => [],
7125 ],
7126 ];
7127 }
7128
7129 $context['pluggable_data'] = $order_sample_data;
7130
7131 } elseif ( 'order_status_changed' === $term ) {
7132 if ( -1 == $order_status ) {
7133 $args = [
7134 'numberposts' => 1,
7135 'orderby' => 'date',
7136 'order' => 'DESC',
7137 ];
7138 } else {
7139 $args = [
7140 'status' => [ $order_status ],
7141 'numberposts' => 1,
7142 'orderby' => 'date',
7143 'order' => 'DESC',
7144 ];
7145 }
7146 $orders = wc_get_orders( $args );
7147 $order_id = '';
7148 if ( is_array( $orders ) && count( $orders ) > 0 ) {
7149 $order_id = $orders[0]->get_id();
7150 $order = wc_get_order( $order_id );
7151 if ( ! empty( $order ) && $order instanceof \WC_Order ) {
7152 $user_id = $order->get_customer_id();
7153 $items = $order->get_items();
7154 $product_ids = [];
7155 foreach ( $items as $item ) {
7156 $product_ids[] = $item['product_id'];
7157 }
7158 $product_data = [];
7159 foreach ( $product_ids as $key => $product_id ) {
7160 /**
7161 *
7162 * Ignore line
7163 *
7164 * @phpstan-ignore-next-line
7165 */
7166 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
7167 /**
7168 *
7169 * Ignore line
7170 *
7171 * @phpstan-ignore-next-line
7172 */
7173 $terms = get_the_terms( $product_id, 'product_cat' );
7174 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
7175 $cat_name = [];
7176 foreach ( $terms as $cat ) {
7177 $cat_name[] = $cat->name;
7178 }
7179 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
7180 }
7181 /**
7182 *
7183 * Ignore line
7184 *
7185 * @phpstan-ignore-next-line
7186 */
7187 $terms_tags = get_the_terms( $product_id, 'product_tag' );
7188 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
7189 $tag_name = [];
7190 foreach ( $terms_tags as $tag ) {
7191 $tag_name[] = $tag->name;
7192 }
7193 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
7194 }
7195 }
7196 $order_context = WooCommerce::get_order_context( $order_id );
7197 $order_sample_data = array_merge(
7198 isset( $order_context ) ? $order_context : [],
7199 $product_data
7200 );
7201 $order_sample_data['user'] = WordPress::get_user_context( $user_id );
7202 $order_sample_data['to_status'] = $order_status;
7203 $order_sample_data['from_status'] = $from_order_status;
7204 $context['response_type'] = 'live';
7205 }
7206 }
7207
7208 $order_sample_data['to_status'] = $order_status;
7209 $order_sample_data['from_status'] = $from_order_status;
7210 $context['pluggable_data'] = $order_sample_data;
7211 } elseif ( 'order_note_added' === $term ) {
7212 global $wpdb;
7213 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type ='order_note' ORDER BY comment_ID DESC LIMIT 1" );
7214 $order_id = '';
7215 if ( ! empty( $result ) ) {
7216 $order_id = $result[0]->comment_post_ID;
7217 $order = wc_get_order( $order_id );
7218 if ( ! empty( $order ) && $order instanceof \WC_Order ) {
7219 $user_id = $order->get_customer_id();
7220 $order_context = WooCommerce::get_order_context( $order_id );
7221 $order_sample_data = array_merge(
7222 isset( $order_context ) ? $order_context : [],
7223 WordPress::get_user_context( $user_id )
7224 );
7225 if ( -1 == $order_note_type ) {
7226 $args = [
7227 'order_id' => $order_id,
7228 'limit' => 1,
7229 ];
7230 } else {
7231 $args = [
7232 'order_id' => $order_id,
7233 'type' => $order_note_type,
7234 'limit' => 1,
7235 ];
7236 }
7237
7238 $notes = wc_get_order_notes( $args );
7239 if ( ! empty( $notes ) ) {
7240 $order_sample_data['note'] = [
7241 'id' => $notes[0]->id,
7242 'date' => $notes[0]->date_created,
7243 'author' => $notes[0]->added_by,
7244 'content' => $notes[0]->content,
7245 ];
7246 } else {
7247 $order_sample_data['note'] = [
7248 'id' => '1',
7249 'date' => [
7250 'date' => '2023-06-23 10:10:40',
7251 'timezone_type' => 1,
7252 'timezone' => '+00:00',
7253 ],
7254 'author' => 'admin',
7255 'content' => 'new note',
7256 ];
7257 }
7258 $order_sample_data['note_type'] = $order_note_type;
7259 $context['response_type'] = 'live';
7260 }
7261 } else {
7262 $order_sample_data['note'] = [
7263 'id' => '1',
7264 'date' => [
7265 'date' => '2023-06-23 10:10:40',
7266 'timezone_type' => 1,
7267 'timezone' => '+00:00',
7268 ],
7269 'author' => 'admin',
7270 'content' => 'new note',
7271 ];
7272 $order_sample_data['note_type'] = 'customer';
7273 }
7274 $context['pluggable_data'] = $order_sample_data;
7275 } elseif ( 'order_paid' === $term ) {
7276 $args = [
7277 'status' => [ 'completed' ],
7278 'numberposts' => 1,
7279 ];
7280 $orders = wc_get_orders( $args );
7281 $order_id = '';
7282 if ( is_array( $orders ) && count( $orders ) > 0 ) {
7283 $order_id = $orders[0]->get_id();
7284 $order = wc_get_order( $order_id );
7285 if ( $order instanceof \WC_Order ) {
7286 $user_id = $order->get_customer_id();
7287 $order_context = WooCommerce::get_order_context( $order_id );
7288 $order_sample_data = array_merge(
7289 isset( $order_context ) ? $order_context : [],
7290 WordPress::get_user_context( $user_id )
7291 );
7292 $context['response_type'] = 'live';
7293 }
7294 }
7295 $context['pluggable_data'] = $order_sample_data;
7296 } elseif ( 'product_category_purchased' === $term ) {
7297 $product_category_id = (int) ( isset( $data['filter']['product_category_id']['value'] ) ? $data['filter']['product_category_id']['value'] : -1 );
7298 $args = [
7299 'post_status' => 'publish',
7300 'product_category_id' => [ $product_category_id ],
7301 'return' => 'ids',
7302 ];
7303 $products = wc_get_products( $args );
7304 if ( ! empty( $products ) ) {
7305 $order_id = 0;
7306 $product_data['quantity'] = '1';
7307 $orders = wc_get_orders(
7308 [
7309 'status' => 'any',
7310 ]
7311 );
7312 $filtered_orders = [];
7313 if ( ! empty( $orders ) && is_array( $orders ) ) {
7314 foreach ( $orders as $order ) {
7315 $order_items = $order->get_items();
7316 foreach ( $order_items as $item ) {
7317 if ( method_exists( $item, 'get_product_id' ) ) {
7318 $product_id = $item->get_product_id();
7319 }
7320 if ( is_array( $products ) && in_array( $product_id, $products ) ) {
7321 $filtered_orders[] = $order;
7322 break;
7323 }
7324 }
7325 }
7326 }
7327 if ( ! empty( $filtered_orders ) ) {
7328 if ( count( $filtered_orders ) > 0 ) {
7329 $order_id = $filtered_orders[0]->get_id();
7330 }
7331 if ( 0 !== $order_id ) {
7332 $order = wc_get_order( $order_id );
7333 if ( $order instanceof WC_Order ) {
7334 $user_id = $order->get_customer_id();
7335 $items = $order->get_items();
7336 $product_ids = [];
7337 $iteration = 0;
7338 foreach ( $items as $item ) {
7339 if ( method_exists( $item, 'get_product_id' ) ) {
7340 $item_id = $item->get_product_id();
7341 if ( -1 === $product_id && 0 === $iteration ) {
7342 $product_ids[] = $item_id;
7343 break;
7344 } elseif ( $item_id === $product_id ) {
7345 $product_ids[] = $item_id;
7346 break;
7347 }
7348 }
7349 $iteration++;
7350 }
7351 $order_data = WooCommerce::get_order_context( $order_id );
7352 $user_data = WordPress::get_user_context( $user_id );
7353 $order_data['total_items_in_order'] = count( $product_ids );
7354 $product_data = [];
7355 $category_ids = [];
7356 foreach ( $product_ids as $key => $product_id ) {
7357 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
7358 $terms = get_the_terms( $product_id, 'product_cat' );
7359 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
7360 $cat_name = [];
7361 foreach ( $terms as $cat ) {
7362 $cat_name[] = $cat->name;
7363 $category_ids[] = $cat->term_id;
7364 }
7365 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
7366 }
7367 $terms_tags = get_the_terms( $product_id, 'product_tag' );
7368 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
7369 $tag_name = [];
7370 foreach ( $terms_tags as $tag ) {
7371 $tag_name[] = $tag->name;
7372 }
7373 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
7374 }
7375 $product = wc_get_product( $product_id );
7376 /**
7377 *
7378 * Ignore line
7379 *
7380 * @phpstan-ignore-next-line
7381 */
7382 if ( $product->is_downloadable() ) {
7383 /**
7384 *
7385 * Ignore line
7386 *
7387 * @phpstan-ignore-next-line
7388 */
7389 foreach ( $product->get_downloads() as $key_download_id => $download ) {
7390 $download_name = $download->get_name();
7391 $download_link = $download->get_file();
7392 $download_id = $download->get_id();
7393 $download_type = $download->get_file_type();
7394 $download_ext = $download->get_file_extension();
7395 $product_data[ 'product' . $key ]['download'] = [
7396 'download_name' => $download_name,
7397 'download_link' => $download_link,
7398 'download_id' => $download_id,
7399 'download_type' => $download_type,
7400 'download_ext' => $download_ext,
7401 ];
7402 }
7403 }
7404 }
7405 $context['response_type'] = 'live';
7406 }
7407 }
7408 }
7409 }
7410 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
7411 if ( ! empty( $category_ids ) ) {
7412 foreach ( $category_ids as $category_id ) {
7413 $context['pluggable_data']['product_category_id'] = $category_id;
7414 }
7415 } else {
7416 $context['pluggable_data']['product_category_id'] = 1;
7417 }
7418 }
7419
7420 return $context;
7421 }
7422
7423 /**
7424 * Get last data for Jet Appointments Booking triggers.
7425 *
7426 * @param array $data data.
7427 * @return array
7428 */
7429 public function search_jet_appointment_triggers_last_data( $data ) {
7430 if ( ! class_exists( '\JET_APB\Plugin' ) ) {
7431 return [];
7432 }
7433
7434 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
7435
7436 // Sample appointment data.
7437 $appointment_data = [
7438 'ID' => 1,
7439 'group_ID' => '',
7440 'status' => 'pending',
7441 'service' => 1,
7442 'provider' => 1,
7443 'order_id' => '',
7444 'user_id' => 1,
7445 'user_name' => 'john',
7446 'user_email' => 'john@example.com',
7447 'date' => time(),
7448 'date_end' => 0,
7449 'slot' => time() - 3600,
7450 'slot_end' => time(),
7451 'type' => 'single',
7452 'appointment_date' => gmdate( 'Y-m-d H:i:s' ),
7453 'service_title' => 'Sample Service',
7454 'provider_title' => 'Sample Provider',
7455 'user_login' => 'john',
7456 'user_display_name' => 'John Doe',
7457 'date_formatted' => date_i18n( is_string( get_option( 'date_format' ) ) ? get_option( 'date_format' ) : 'Y-m-d', time() ),
7458 'slot_formatted' => date_i18n( is_string( get_option( 'time_format' ) ) ? get_option( 'time_format' ) : 'H:i', time() - 3600 ),
7459 'slot_end_formatted' => date_i18n( is_string( get_option( 'time_format' ) ) ? get_option( 'time_format' ) : 'H:i', time() ),
7460 ];
7461
7462 // Add term-specific fields based on search term.
7463 if ( 'appointment_cancelled' === $term || 'appointment_status_changed' === $term ) {
7464 $appointment_data['old_status'] = 'confirmed';
7465 $appointment_data['new_status'] = 'appointment_cancelled' === $term ? 'cancelled' : 'completed';
7466 }
7467
7468 $context = [
7469 'response_type' => 'sample',
7470 ];
7471
7472 // Try to get live data using the same method as triggers.
7473 if ( class_exists( '\JET_APB\Plugin' ) ) {
7474 $live_appointment_data = \JET_APB\Plugin::instance()->db->appointments->query( [], 1 );
7475 if ( ! empty( $live_appointment_data ) ) {
7476 $live_data = $live_appointment_data[0];
7477 $appointment_id = $live_data['ID'];
7478
7479 // Get appointment meta using the same method as triggers.
7480 $appointment_meta = $this->get_appointment_meta( $appointment_id );
7481
7482 // Merge appointment data with meta data.
7483 $appointment_data = array_merge( $live_data, $appointment_meta );
7484
7485 // Add service title.
7486 if ( ! empty( $appointment_data['service'] ) ) {
7487 $service_post = get_post( $appointment_data['service'] );
7488 if ( $service_post ) {
7489 $appointment_data['service_title'] = $service_post->post_title;
7490 }
7491 }
7492
7493 // Add provider title.
7494 if ( ! empty( $appointment_data['provider'] ) && $appointment_data['provider'] > 0 ) {
7495 $provider_post = get_post( $appointment_data['provider'] );
7496 if ( $provider_post ) {
7497 $appointment_data['provider_title'] = $provider_post->post_title;
7498 }
7499 }
7500
7501 // Add user data.
7502 if ( ! empty( $appointment_data['user_id'] ) ) {
7503 $user = get_user_by( 'ID', $appointment_data['user_id'] );
7504 if ( $user ) {
7505 $appointment_data['user_login'] = $user->user_login;
7506 $appointment_data['user_email'] = $user->user_email;
7507 $appointment_data['user_display_name'] = $user->display_name;
7508 }
7509 }
7510
7511 // Add formatted dates.
7512 $date_format = get_option( 'date_format' );
7513 $time_format = get_option( 'time_format' );
7514 $appointment_data['date_formatted'] = is_string( $date_format ) ? date_i18n( $date_format, $appointment_data['date'] ) : '';
7515 $appointment_data['slot_formatted'] = is_string( $time_format ) ? date_i18n( $time_format, $appointment_data['slot'] ) : '';
7516 $appointment_data['slot_end_formatted'] = is_string( $time_format ) ? date_i18n( $time_format, $appointment_data['slot_end'] ) : '';
7517
7518 // Add term-specific fields for live data.
7519 if ( 'appointment_cancelled' === $term || 'appointment_status_changed' === $term ) {
7520 $appointment_data['old_status'] = 'confirmed';
7521 $appointment_data['new_status'] = 'appointment_cancelled' === $term ? 'cancelled' : 'completed';
7522 }
7523
7524 $context['response_type'] = 'live';
7525 }
7526 }
7527
7528 $context['pluggable_data'] = $appointment_data;
7529 return $context;
7530 }
7531
7532 /**
7533 * Get appointment meta data.
7534 *
7535 * @param int $appointment_id Appointment ID.
7536 * @return array
7537 */
7538 private function get_appointment_meta( $appointment_id ) {
7539 if ( ! class_exists( '\JET_APB\Plugin' ) ) {
7540 return [];
7541 }
7542
7543 global $wpdb;
7544 $meta_table = \JET_APB\Plugin::instance()->db->appointments_meta->table();
7545
7546 if ( empty( $meta_table ) || is_array( $meta_table ) || ! is_string( $meta_table ) ) {
7547 return [];
7548 }
7549
7550 $results = $wpdb->get_results(
7551 $wpdb->prepare(
7552 'SELECT meta_key, meta_value FROM ' . esc_sql( $meta_table ) . ' WHERE appointment_id = %d',
7553 $appointment_id
7554 ),
7555 ARRAY_A
7556 );
7557
7558 $meta = [];
7559 if ( ! empty( $results ) ) {
7560 foreach ( $results as $row ) {
7561 $meta[ $row['meta_key'] ] = $row['meta_value'];
7562 }
7563 }
7564
7565 return $meta;
7566 }
7567
7568 /**
7569 * Search LMS data.
7570 *
7571 * @param array $data data.
7572 * @return array
7573 */
7574 public function search_lifter_lms_last_data( $data ) {
7575 global $wpdb;
7576 $post_type = $data['post_type'];
7577 $meta_key = '_is_complete';
7578 $trigger = $data['search_term'];
7579 $context = [];
7580 $post_id = -1;
7581
7582 if ( ! class_exists( 'LLMS_Section' ) ) {
7583 return [];
7584 }
7585 $product_type = '';
7586 if ( 'lifterlms_purchase_course' === $trigger ) {
7587 $product_type = 'course';
7588 $post_id = $data['filter']['course_id']['value'];
7589 } elseif ( 'lifterlms_purchase_membership' === $trigger ) {
7590 $product_type = 'membership';
7591 $post_id = $data['filter']['membership_id']['value'];
7592 } elseif ( 'lifterlms_cancel_membership' === $trigger ) {
7593 $product_type = 'membership';
7594 $post_id = $data['filter']['membership_id']['value'];
7595 } elseif ( 'lifterlms_lesson_completed' === $trigger ) {
7596 $post_id = $data['filter']['lesson']['value'];
7597 } elseif ( 'lifterlms_course_completed' === $trigger || 'lifterlms_course_enrolled' === $trigger || 'lifterlms_course_user_removed' === $trigger ) {
7598 $post_id = $data['filter']['course']['value'];
7599 } elseif ( 'lifterlms_section_completed' === $trigger ) {
7600 $post_id = $data['filter']['section']['value'];
7601 }
7602
7603 $where = 'postmeta.post_id = "' . $post_id . '" AND';
7604
7605 if ( 'llms_order' === $post_type ) {
7606 if ( -1 === $post_id ) {
7607 $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 ) );
7608 } else {
7609 $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 ) );
7610 }
7611 } elseif ( 'lifterlms_course_enrolled' === $trigger ) {
7612 if ( -1 === $post_id ) {
7613 $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 ) );
7614 } else {
7615 $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 ) );
7616 }
7617 } elseif ( 'lifterlms_course_user_removed' === $trigger ) {
7618 if ( -1 === $post_id ) {
7619 $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 ) );
7620 } else {
7621 $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 ) );
7622 }
7623 } elseif ( 'lifterlms_cancel_membership' === $trigger ) {
7624 if ( -1 === $post_id ) {
7625 $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 ) );
7626 } else {
7627 $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 ) );
7628 }
7629 } else {
7630 if ( -1 === $post_id ) {
7631 $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 ) );
7632 } else {
7633 $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 ) );
7634 }
7635 }
7636
7637 $response = [];
7638 if ( ! empty( $result ) ) {
7639 $result_post_id = $result[0]->post_id;
7640 $result_user_id = $result[0]->user_id;
7641
7642 switch ( $trigger ) {
7643 case 'lifterlms_lesson_completed':
7644 $context = array_merge(
7645 WordPress::get_user_context( $result_user_id ),
7646 LifterLMS::get_lms_lesson_context( $result_post_id )
7647 );
7648 $parent_course = get_post_meta( $result_post_id, '_llms_parent_course', true );
7649 if ( is_numeric( $parent_course ) ) {
7650 $context['course'] = get_the_title( (int) $parent_course );
7651 }
7652 $parent_section = get_post_meta( $result_post_id, '_llms_parent_section', true );
7653 if ( '' !== $parent_section && is_numeric( $parent_section ) ) {
7654 $context['parent_section'] = get_the_title( (int) $parent_section );
7655 }
7656 break;
7657 case 'lifterlms_course_enrolled':
7658 case 'lifterlms_course_user_removed':
7659 case 'lifterlms_course_completed':
7660 $context = array_merge(
7661 WordPress::get_user_context( $result_user_id ),
7662 LifterLMS::get_lms_course_context( $result_post_id )
7663 );
7664 break;
7665 case 'lifterlms_section_completed':
7666 $data = new \LLMS_Section( $result_post_id );
7667 $lessons = $data->get_lessons();
7668 $context = array_merge(
7669 WordPress::get_user_context( $result_user_id ),
7670 WordPress::get_post_context( $result_post_id )
7671 );
7672 $context['parent_course'] = $data->get( 'parent_course' );
7673 $context['parent_course_title'] = get_the_title( $data->get( 'parent_course' ) );
7674 if ( ! empty( $lessons ) ) {
7675 foreach ( $lessons as $key => $lesson ) {
7676 $context['section_lesson'][ $key ] = $lesson->id;
7677 $context['section_lesson_title'][ $key ] = get_the_title( $lesson->id );
7678 }
7679 }
7680 $context['section_course'] = $data->get( 'parent_course' );
7681 $context['parent_course_title'] = get_the_title( $data->get( 'parent_course' ) );
7682 break;
7683 case 'lifterlms_purchase_course':
7684 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
7685 $context['course_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
7686 $context['course_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
7687 $context['course_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
7688 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
7689 $context ['order'] = WordPress::get_post_context( $result_post_id );
7690 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
7691 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
7692 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
7693 $context = array_merge( $context, is_numeric( $user_id ) ? WordPress::get_user_context( (int) $user_id ) : [] );
7694 break;
7695 case 'lifterlms_purchase_membership':
7696 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
7697 $context['membership_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
7698 $context['membership_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
7699 $context['membership_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
7700 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
7701 $context ['order'] = WordPress::get_post_context( $result_post_id );
7702 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
7703 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
7704 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
7705 $context = array_merge( $context, is_numeric( $user_id ) ? WordPress::get_user_context( (int) $user_id ) : [] );
7706 break;
7707 case 'lifterlms_cancel_membership':
7708 $context = array_merge( WordPress::get_post_context( $result_post_id ), WordPress::get_user_context( $result[0]->user_id ) );
7709 $context['membership_id'] = $result_post_id;
7710 $context['membership_name'] = get_the_title( $result_post_id );
7711 break;
7712 default:
7713 return [];
7714
7715 }
7716 $response['pluggable_data'] = $context;
7717 $response['response_type'] = 'live';
7718
7719 }
7720
7721 return $response;
7722
7723 }
7724
7725 /**
7726 * Search SM data.
7727 *
7728 * @param array $data data.
7729 * @return array
7730 */
7731 public function search_suremember_last_data( $data ) {
7732 global $wpdb;
7733 $post_type = $data['post_type'];
7734 $meta_key = '_is_complete';
7735 $trigger = $data['search_term'];
7736 $post_id = $data['filter']['group_id']['value'];
7737
7738 if ( 'suremember_updated_group' === $trigger ) {
7739 if ( -1 === $post_id ) {
7740 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts as posts WHERE posts.post_type=%s", $post_type ) );
7741 } else {
7742 $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 ) );
7743 }
7744 } else {
7745 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}usermeta as usermeta WHERE usermeta.meta_key = %s", 'suremembers_user_access_group_' . $post_id ) );
7746 }
7747
7748 $response = [];
7749
7750 if ( ! empty( $result ) ) {
7751 $context = [];
7752 switch ( $trigger ) {
7753 case 'suremember_updated_group':
7754 $group_id = $result[0]->ID;
7755 $suremembers_post['rules'] = get_post_meta( $group_id, 'suremembers_plan_include', true );
7756 $suremembers_post['exclude'] = get_post_meta( $group_id, 'suremembers_plan_exclude', true ); //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
7757 $suremembers_post['suremembers_user_roles'] = get_post_meta( $group_id, 'suremembers_user_roles', true );
7758 $suremembers_post['title'] = get_the_title( $group_id );
7759 $plan_rules = get_post_meta( $group_id, 'suremembers_plan_rules', true );
7760 $suremembers_post['restrict'] = is_array( $plan_rules ) ? $plan_rules['restrict'] : '';
7761 $context['group'] = array_merge( WordPress::get_post_context( $group_id ), $suremembers_post );
7762 $context['group_id'] = $group_id;
7763 unset( $context['group']['ID'] );
7764 $response['pluggable_data'] = $context;
7765 $response['response_type'] = 'live';
7766 break;
7767 case 'suremember_user_added_in_group':
7768 foreach ( $result as $res ) {
7769 $meta_value = unserialize( $res->meta_value );
7770 if ( is_array( $meta_value ) && 'active' === $meta_value['status'] ) {
7771 $context = WordPress::get_user_context( $res->user_id );
7772 $context['group'] = WordPress::get_post_context( $post_id );
7773 $context['group_id'] = $post_id;
7774 unset( $context['group']['ID'] );
7775 $response['pluggable_data'] = $context;
7776 $response['response_type'] = 'live';
7777 }
7778 }
7779 break;
7780 case 'suremember_user_removed_from_group':
7781 foreach ( $result as $res ) {
7782 $meta_value = unserialize( $res->meta_value );
7783 if ( is_array( $meta_value ) && 'revoked' === $meta_value['status'] ) {
7784 $context = WordPress::get_user_context( $res->user_id );
7785 $context['group'] = WordPress::get_post_context( $post_id );
7786 $context['group_id'] = $post_id;
7787 unset( $context['group']['ID'] );
7788 $response['pluggable_data'] = $context;
7789 $response['response_type'] = 'live';
7790 }
7791 }
7792 break;
7793 default:
7794 return [];
7795
7796 }
7797 }
7798
7799 return $response;
7800
7801 }
7802
7803 /**
7804 * Search CartFlows data.
7805 *
7806 * @param array $data data.
7807 * @return array
7808 */
7809 public function search_cartflows_last_data( $data ) {
7810 global $wpdb;
7811 $trigger = $data['search_term'];
7812
7813 if ( ! function_exists( 'wcf_pro' ) ) {
7814 return [];
7815 }
7816
7817 $context = [];
7818 $response = [];
7819 if ( 'cartflows_upsell_offer_accepted' === $trigger || 'cartflows_upsell_offer_rejected' === $trigger ) {
7820 $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'" );
7821 } elseif ( 'cartflows_downsell_offer_accepted' === $trigger || 'cartflows_downsell_offer_rejected' === $trigger ) {
7822 $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'" );
7823 } elseif ( 'wcf_order_bump_item_added' === $trigger || 'wcf_order_bump_item_removed' === $trigger || 'wcf_order_bump_item_replaced' === $trigger ) {
7824 $args = [
7825 'post_type' => 'product',
7826 'orderby' => 'ID',
7827 'order' => 'DESC',
7828 'post_status' => 'publish',
7829 'numberposts' => 1,
7830 ];
7831 $products = get_posts( $args );
7832
7833 if ( count( $products ) > 0 ) {
7834 $product_id = $products[0]->ID;
7835 $product_data['product_id'] = $product_id;
7836 $product_data['product'] = WooCommerce::get_product_context( $product_id );
7837 $terms = get_the_terms( $product_id, 'product_cat' );
7838 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
7839 $cat_name = [];
7840 foreach ( $terms as $cat ) {
7841 $cat_name[] = $cat->name;
7842 }
7843 $product_data['product']['category'] = implode( ', ', $cat_name );
7844 }
7845 $terms_tags = get_the_terms( $product_id, 'product_tag' );
7846 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
7847 $tag_name = [];
7848 foreach ( $terms_tags as $tag ) {
7849 $tag_name[] = $tag->name;
7850 }
7851 $product_data['product']['tag'] = implode( ', ', $tag_name );
7852 }
7853 unset( $product_data['product']['id'] ); //phpcs:ignore
7854 $response['response_type'] = 'live';
7855 } else {
7856 $product_data['product'] = [
7857 'name' => 'Sample Product',
7858 'description' => 'This is description of sample product.',
7859 'short_description' => 'This is short description of sample product.',
7860 'image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
7861 'slug' => 'sample-product',
7862 'status' => 'publish',
7863 'type' => 'simple',
7864 'price' => '89',
7865 'featured' => '0',
7866 'sku' => 'hoodie-blue-sm',
7867 'regular_price' => '90',
7868 'sale_price' => '89',
7869 'total_sales' => '21',
7870 'category' => 'Uncategorized',
7871 'tags' => 'sample, new, 2022',
7872 'category_ids' => [ 40 ],
7873 'date_created' => [
7874 'date' => '2024-09-19 09:28:57.000000',
7875 'timezone_type' => '1',
7876 'timezone' => '+00:00',
7877 ],
7878 'date_modified' => [
7879 'date' => '2024-09-19 09:28:57.000000',
7880 'timezone_type' => '1',
7881 'timezone' => '+00:00',
7882 ],
7883 'product_id' => 9935,
7884 ];
7885 $response['response_type'] = 'sample';
7886 }
7887 $response['pluggable_data'] = $product_data;
7888 }
7889 if ( ! empty( $result ) && ( 'cartflows_upsell_offer_accepted' === $trigger || 'cartflows_upsell_offer_rejected' === $trigger ) ) {
7890 $context = [];
7891 $order_upsell_id = $result[0]->post_id;
7892 $step_id = get_post_meta( $order_upsell_id, '_cartflows_offer_step_id', true );
7893 $order_id = get_post_meta( $order_upsell_id, '_cartflows_offer_parent_id', true );
7894 $order = wc_get_order( $order_id );
7895 $upsell_order = wc_get_order( $order_upsell_id );
7896 if ( $upsell_order instanceof \WC_Order ) {
7897 $items = $upsell_order->get_items();
7898 if ( ! empty( $items ) && isset( $items[0] ) ) {
7899 $variation_id = $items[0]['product_id'];
7900 $input_qty = $items[0]['quantity'];
7901 } else {
7902 $variation_id = null;
7903 $input_qty = null;
7904 }
7905 } else {
7906 $variation_id = null;
7907 $input_qty = null;
7908 }
7909 $offer_product = wcf_pro()->utils->get_offer_data( $step_id, $variation_id, $input_qty, $order_id );
7910 $user_id = get_post_meta( $order_upsell_id, '_customer_user', true );
7911 $context = is_numeric( $user_id ) ? WordPress::get_user_context( (int) $user_id ) : [];
7912 if ( $order instanceof \WC_Order ) {
7913 $context['order'] = $order->get_data();
7914 }
7915 $context['upsell'] = $offer_product;
7916 $context['funnel_step_id'] = $step_id;
7917 if ( is_scalar( $step_id ) ) {
7918 $context['funnel_id'] = get_post_meta( intval( $step_id ), 'wcf-flow-id', true );
7919 } else {
7920 $context['funnel_id'] = null;
7921 }
7922 $response['pluggable_data'] = $context;
7923 $response['response_type'] = 'live';
7924 } elseif ( ! empty( $result ) && ( 'cartflows_downsell_offer_accepted' === $trigger || 'cartflows_downsell_offer_rejected' === $trigger ) ) {
7925 $context = [];
7926 $order_downsell_id = $result[0]->post_id;
7927 $step_id = get_post_meta( $order_downsell_id, '_cartflows_offer_step_id', true );
7928 $order_id = get_post_meta( $order_downsell_id, '_cartflows_offer_parent_id', true );
7929 $order = wc_get_order( $order_id );
7930 $downsell_order = wc_get_order( $order_downsell_id );
7931 if ( $downsell_order instanceof \WC_Order ) {
7932 $items = $downsell_order->get_items();
7933 if ( ! empty( $items ) && isset( $items[0] ) ) {
7934 $variation_id = $items[0]['product_id'];
7935 $input_qty = $items[0]['quantity'];
7936 } else {
7937 $variation_id = null;
7938 $input_qty = null;
7939 }
7940 } else {
7941 $variation_id = null;
7942 $input_qty = null;
7943 }
7944 $offer_product = wcf_pro()->utils->get_offer_data( $step_id, $variation_id, $input_qty, $order_id );
7945 $user_id = get_post_meta( $order_downsell_id, '_customer_user', true );
7946 $context = is_numeric( $user_id ) ? WordPress::get_user_context( (int) $user_id ) : [];
7947 if ( $order instanceof \WC_Order ) {
7948 $context['order'] = $order->get_data();
7949 }
7950 $context['downsell'] = $offer_product;
7951 $context['funnel_step_id'] = $step_id;
7952 if ( is_scalar( $step_id ) ) {
7953 $context['funnel_id'] = get_post_meta( intval( $step_id ), 'wcf-flow-id', true );
7954 } else {
7955 $context['funnel_id'] = null;
7956 }
7957 $response['pluggable_data'] = $context;
7958 $response['response_type'] = 'live';
7959 }
7960 return $response;
7961 }
7962
7963 /**
7964 * Prepare CartFlows Steps list.
7965 *
7966 * @param array $data data.
7967 *
7968 * @return array
7969 */
7970 public function search_cartflows_funnel_step_list( $data ) {
7971 $page = $data['page'];
7972 $limit = Utilities::get_search_page_limit();
7973 $offset = $limit * ( $page - 1 );
7974
7975 $args = [
7976 'posts_per_page' => $limit,
7977 'offset' => $offset,
7978 'orderby' => 'title',
7979 'order' => 'ASC',
7980 'post_type' => 'cartflows_step',
7981 'post_status' => 'publish',
7982 'fields' => 'ids',
7983 ];
7984 $args['meta_query'] = [
7985 [
7986 'key' => 'wcf-flow-id',
7987 'value' => $data['dynamic'],
7988 'compare' => '=',
7989 ],
7990 ];
7991
7992 $flow_step_list = get_posts( $args );
7993 $flow_step_list_count = count(
7994 get_posts(
7995 [
7996 'posts_per_page' => -1,
7997 'orderby' => 'title',
7998 'order' => 'ASC',
7999 'post_type' => 'cartflows_step',
8000 'post_status' => 'publish',
8001 'fields' => 'ids',
8002 'meta_query' => [
8003 [
8004 'key' => 'wcf-flow-id',
8005 'value' => $data['dynamic'],
8006 'compare' => '=',
8007 ],
8008 ],
8009 ]
8010 )
8011 );
8012
8013 $options = [];
8014 if ( ! empty( $flow_step_list ) ) {
8015 foreach ( $flow_step_list as $step ) {
8016 $options[] = [
8017 'label' => html_entity_decode( get_the_title( $step ), ENT_QUOTES, 'UTF-8' ),
8018 'value' => $step,
8019 ];
8020 }
8021 }
8022 return [
8023 'options' => $options,
8024 'hasMore' => $flow_step_list_count > $limit && $flow_step_list_count > $offset,
8025 ];
8026 }
8027
8028
8029 /**
8030 * Fetch user context.
8031 *
8032 * @param int $initiator_id initiator id.
8033 * @param int $friend_id friend id.
8034 * @return array
8035 */
8036 public function get_user_context( $initiator_id, $friend_id ) {
8037 $context = WordPress::get_user_context( $initiator_id );
8038
8039 $friend_context = WordPress::get_user_context( $friend_id );
8040
8041 $avatar = get_avatar_url( $initiator_id );
8042
8043 $context['avatar_url'] = ( $avatar ) ? $avatar : '';
8044
8045 $context['friend_id'] = $friend_id;
8046 $context['friend_first_name'] = $friend_context['user_firstname'];
8047 $context['friend_last_name'] = $friend_context['user_lastname'];
8048 $context['friend_email'] = $friend_context['user_email'];
8049
8050 $friend_avatar = get_avatar_url( $friend_id );
8051 $context['friend_avatar_url'] = $friend_avatar;
8052 return $context;
8053 }
8054
8055 /**
8056 * Search BP data.
8057 *
8058 * @param array $data data.
8059 * @return array
8060 */
8061 public function search_pluggables_bp_friendships( $data ) {
8062 global $wpdb, $bp;
8063 $context = [];
8064 $sample['pluggable_data'] = [
8065 'wp_user_id' => 4,
8066 'user_login' => 'katy1ßßßß',
8067 'display_name' => 'Katy Smith',
8068 'user_firstname' => 'Katy',
8069 'user_lastname' => 'Smith',
8070 'user_email' => 'katy1@gmail.com',
8071 'user_role' => [ 'subscriber' ],
8072 'avatar_url' => 'http://pqr.com/avatar',
8073 'friend_id' => 1,
8074 'friend_first_name' => 'John',
8075 'friend_last_name' => 'Wick',
8076 'friend_email' => 'john@gmail.com',
8077 'friend_avatar_url' => 'http://abc.com/avatar',
8078 ];
8079 $sample['response_type'] = 'sample';
8080
8081 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_friends'" );
8082 if ( $table_exists ) {
8083 $friendships = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_friends LIMIT 1" );
8084 if ( ! empty( $friendships ) ) {
8085 $friendship = $friendships[0];
8086 $initiator_id = $friendship->initiator_user_id;
8087 $friend_user_id = $friendship->friend_user_id;
8088 $context['pluggable_data'] = $this->get_user_context( $initiator_id, $friend_user_id );
8089 $context['response_type'] = 'live';
8090 } else {
8091 $context = $sample;
8092 }
8093 } else {
8094 $context = $sample;
8095 }
8096
8097
8098 return $context;
8099 }
8100
8101 /**
8102 * Search BuddyBoss private messages data.
8103 *
8104 * @param array $data data.
8105 * @return array
8106 */
8107 public function search_pluggables_bb_private_messages( $data ) {
8108 global $wpdb;
8109 $context = [];
8110 $sample['pluggable_data'] = [
8111 'wp_user_id' => 2,
8112 'user_login' => 'johndoe',
8113 'display_name' => 'John Doe',
8114 'user_firstname' => 'John',
8115 'user_lastname' => 'Doe',
8116 'user_email' => 'john@example.com',
8117 'user_role' => [ 'subscriber' ],
8118 'sender_id' => 1,
8119 'sender_first_name' => 'Jane',
8120 'sender_last_name' => 'Smith',
8121 'sender_email' => 'jane@example.com',
8122 'sender_display_name' => 'Jane Smith',
8123 'sender_avatar_url' => 'http://example.com/avatar.jpg',
8124 'message_id' => 1,
8125 'thread_id' => 1,
8126 'message_subject' => 'Hello!',
8127 'message_content' => 'This is a sample private message.',
8128 'message_content_html' => '<p>This is a sample private message.</p>',
8129 'message_date' => '2024-01-01 10:00:00',
8130 ];
8131 $sample['response_type'] = 'sample';
8132
8133 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_messages_messages'" );
8134 if ( $table_exists ) {
8135 $message = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}bp_messages_messages ORDER BY id DESC LIMIT 1" );
8136 if ( ! empty( $message ) ) {
8137 $recipients = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}bp_messages_recipients WHERE thread_id = %d", $message->thread_id ) );
8138 if ( ! empty( $recipients ) ) {
8139 foreach ( $recipients as $recipient ) {
8140 if ( $recipient->user_id != $message->sender_id ) {
8141 $recipient_context = WordPress::get_user_context( $recipient->user_id );
8142 $sender_context = WordPress::get_user_context( $message->sender_id );
8143
8144 $context['pluggable_data'] = array_merge(
8145 $recipient_context,
8146 [
8147 'sender_id' => $message->sender_id,
8148 'sender_first_name' => $sender_context['user_firstname'],
8149 'sender_last_name' => $sender_context['user_lastname'],
8150 'sender_email' => $sender_context['user_email'],
8151 'sender_display_name' => $sender_context['display_name'],
8152 'sender_avatar_url' => get_avatar_url( $message->sender_id ),
8153 'message_id' => $message->id,
8154 'thread_id' => $message->thread_id,
8155 'message_subject' => $message->subject,
8156 'message_content' => wp_strip_all_tags( $message->message ),
8157 'message_content_html' => $message->message,
8158 'message_date' => $message->date_sent,
8159 ]
8160 );
8161 $context['response_type'] = 'live';
8162 break;
8163 }
8164 }
8165 }
8166 }
8167 }
8168
8169 if ( empty( $context ) ) {
8170 $context = $sample;
8171 }
8172
8173 return $context;
8174 }
8175
8176 /**
8177 * Search Buddyboss profile types data.
8178 *
8179 * @param array $data data.
8180 * @return array
8181 */
8182 public function search_pluggables_bp_profile_types( $data ) {
8183 global $wpdb, $bp;
8184 $context = [];
8185 $sample['pluggable_data'] = [
8186 'wp_user_id' => 4,
8187 'user_login' => 'katy1ßßßß',
8188 'display_name' => 'Katy Smith',
8189 'user_firstname' => 'Katy',
8190 'user_lastname' => 'Smith',
8191 'user_email' => 'katy1@gmail.com',
8192 'user_role' => [ 'subscriber' ],
8193 'bb_profile_type' => '10',
8194 'bb_profile_type_name' => 'student',
8195 ];
8196 $sample['response_type'] = 'sample';
8197
8198 $post_id = $data['filter']['bb_profile_type']['value'];
8199 $get_existing = get_post_meta( $post_id, '_bp_member_type_key', true );
8200
8201 $type_term = get_term_by(
8202 'name',
8203 /**
8204 *
8205 * Ignore line
8206 *
8207 * @phpstan-ignore-next-line
8208 */
8209 $get_existing,
8210 'bp_member_type'
8211 );
8212
8213 $results = $wpdb->get_results(
8214 $wpdb->prepare(
8215 "SELECT u.ID FROM {$wpdb->prefix}users u INNER JOIN {$wpdb->prefix}term_relationships r
8216 ON u.ID = r.object_id WHERE u.user_status = 0 AND
8217 r.term_taxonomy_id = %d ORDER BY RAND() LIMIT 1",
8218 /**
8219 *
8220 * Ignore line
8221 *
8222 * @phpstan-ignore-next-line
8223 */
8224 $type_term->term_id
8225 )
8226 );
8227
8228 if ( ! empty( $results ) ) {
8229 $user = $results[0];
8230 $context['pluggable_data'] = WordPress::get_user_context( $user->ID );
8231 $context['pluggable_data']['bb_profile_type'] = $post_id;
8232 $context['pluggable_data']['bb_profile_type_name'] = get_post_meta( $post_id, '_bp_member_type_label_singular_name', true );
8233 $context['response_type'] = 'live';
8234 } else {
8235 $context = $sample;
8236 }
8237
8238 return $context;
8239 }
8240
8241 /**
8242 * Search BP User data.
8243 *
8244 * @param array $data data.
8245 * @return array
8246 */
8247 public function search_pluggables_bb_users( $data ) {
8248 global $wpdb;
8249 $context = [];
8250
8251 if ( ! class_exists( 'BP_Signup' ) ) {
8252 return [];
8253 }
8254
8255 $term = $data['search_term'];
8256
8257 if ( 'account_activated' === $term ) {
8258 $signups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}signups WHERE active = 1 ORDER BY signup_id DESC LIMIT 1" );
8259 } elseif ( 'updates_profile' === $term ) {
8260 $custom_ids = $wpdb->get_var( "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data ORDER BY last_updated DESC LIMIT 1" );
8261 $args = [ 'include' => $custom_ids ];
8262 $users = get_users( $args );
8263 } elseif ( 'gains_follower' === $term ) {
8264 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_follow ORDER BY id DESC LIMIT 1" );
8265 }
8266
8267 if ( ! empty( $signups ) ) {
8268 $pluggable_data = $signups[0];
8269 $pluggable_data = get_object_vars( $pluggable_data );
8270 unset( $pluggable_data['activation_key'] );
8271 if ( is_string( $pluggable_data['meta'] ) ) {
8272 $pluggable_data['meta'] = unserialize( $pluggable_data['meta'] );
8273 }
8274 if ( is_array( $pluggable_data['meta'] ) ) {
8275 unset( $pluggable_data['meta']['password'] );
8276 }
8277 $context['pluggable_data'] = $pluggable_data;
8278 $context['response_type'] = 'live';
8279 } elseif ( 'updates_profile' === $term ) {
8280 if ( ! empty( $users ) ) {
8281 $user = $users[0];
8282 $fields = $wpdb->get_results( $wpdb->prepare( "SELECT field_id, value FROM {$wpdb->prefix}bp_xprofile_data WHERE user_id = %d", $user->ID ) );
8283 $user_data = WordPress::get_user_context( $user->ID );
8284 $pluggable_data['user_id'] = $user->ID;
8285 $pluggable_data['user_email'] = $user_data['user_email'];
8286 foreach ( $user_data['user_role'] as $key => $role ) {
8287 $pluggable_data['user_role'][ $key ] = $role;
8288 }
8289 foreach ( $fields as $field ) {
8290 if ( function_exists( 'xprofile_get_field' ) ) {
8291 $fieldj = xprofile_get_field( $field->field_id );
8292 $pluggable_data[ $fieldj->name ] = $field->value;
8293 }
8294 }
8295 $context['pluggable_data'] = $pluggable_data;
8296 $context['response_type'] = 'live';
8297 } else {
8298 $context['pluggable_data'] = [
8299 'user_login' => 'john',
8300 'display_name' => 'john',
8301 'user_firstname' => 'john',
8302 'user_lastname' => 'd',
8303 'user_email' => 'johnd@yopmail.com',
8304 'user_role' => [
8305 'subscriber',
8306 ],
8307 'Name' => 'john',
8308 'Nickname' => 'johnd',
8309 'wp_user_id' => 16,
8310 ];
8311 $context['response_type'] = 'sample';
8312 }
8313 } elseif ( 'gains_follower' === $term ) {
8314 if ( ! empty( $results ) ) {
8315 $pluggable_data['follower'] = WordPress::get_user_context( $results[0]->follower_id );
8316 $pluggable_data['leader'] = WordPress::get_user_context( $results[0]->leader_id );
8317 $context['pluggable_data'] = $pluggable_data;
8318 $context['response_type'] = 'live';
8319 } else {
8320 $context['pluggable_data'] = [
8321 'follower' => [
8322 'wp_user_id' => 126,
8323 'user_login' => 'belli',
8324 'display_name' => 'belli',
8325 'user_firstname' => 'test',
8326 'user_lastname' => 'test',
8327 'user_email' => 'belli@gmail.com',
8328 'user_role' => [
8329 'subscriber',
8330 'wpamelia-customer',
8331 ],
8332 ],
8333 'leader' => [
8334 'wp_user_id' => 34,
8335 'user_login' => 'bella3@gmail.com',
8336 'display_name' => 'bella3@gmail.com',
8337 'user_firstname' => 'bella3',
8338 'user_lastname' => 'bella3',
8339 'user_email' => 'bellaaa3@gmail.com',
8340 'user_role' => [
8341 'wpamelia-customer',
8342 ],
8343 ],
8344 ];
8345 $context['response_type'] = 'sample';
8346 }
8347 } else {
8348 $context['pluggable_data'] = [
8349 'signup_id' => '16',
8350 'domain' => '',
8351 'path' => '',
8352 'title' => '',
8353 'user_login' => 'johnd',
8354 'user_email' => 'johnd@yopmail.com',
8355 'registered' => '2024-01-29 04:52:13',
8356 'activated' => '0000-00-00 00:00:00',
8357 'active' => '0',
8358 'meta' => [
8359 'field_1' => 'john',
8360 'field_3' => 'd',
8361 'field_4' => '09878988766',
8362 'field_2' => '123 Main Street',
8363 'field_5' => 'johnd',
8364 'profile_field_ids' => '1,3,4,2,5',
8365 ],
8366 'id' => 16,
8367 'user_name' => 'johnd',
8368 'date_sent' => '2024-01-29 04:52:13',
8369 'recently_sent' => true,
8370 'count_sent' => 1,
8371 ];
8372 $context['response_type'] = 'sample';
8373 }
8374
8375 return $context;
8376 }
8377
8378 /**
8379 * Search BP data.
8380 *
8381 * @param array $data data.
8382 * @return array
8383 */
8384 public function search_pluggables_bp_groups( $data ) {
8385 global $wpdb, $bp;
8386 $context = [];
8387 $group_data = [];
8388 $args = [
8389 'orderby' => 'user_nicename',
8390 'order' => 'ASC',
8391 'number' => 1,
8392 ];
8393
8394 $term = $data['search_term'];
8395
8396 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' ) ) {
8397 return [];
8398 }
8399
8400 if ( 'user_joins_specific_type_group' == $term ) {
8401 if ( -1 !== $data['filter']['group_type']['value'] ) {
8402 $group_type = $data['filter']['group_type']['value'];
8403 } else {
8404 $registered_types = bp_groups_get_group_types();
8405 $random_key = array_rand( $registered_types );
8406 $random_value = $registered_types[ $random_key ];
8407 $group_type = $random_value;
8408 }
8409 if ( function_exists( 'bp_get_group_ids_by_group_types' ) ) {
8410 $group_ids = bp_get_group_ids_by_group_types( $group_type );
8411 $random_key = array_rand( $group_ids );
8412 $random_value = $group_ids[ $random_key ];
8413 $group_id = $random_value;
8414 if ( function_exists( 'groups_get_group' ) ) {
8415 $group = groups_get_group( $group_id['id'] );
8416 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
8417 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
8418 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
8419 $group_data['group_type'] = $group_type;
8420 if ( function_exists( 'groups_get_group_members' ) ) {
8421 $members = groups_get_group_members(
8422 [
8423 'group_id' => $group_id,
8424 ]
8425 );
8426 $ids = [];
8427 foreach ( $members['members'] as $member ) {
8428 $ids[] = $member->ID;
8429 }
8430 $args = [
8431 'number' => 1,
8432 'include' => $ids,
8433 ];
8434 $users = get_users( $args );
8435 }
8436 }
8437 }
8438 } elseif ( 'requests_access_private_group' === $data['search_term'] ) {
8439 $group_id = $data['filter']['group_id']['value'];
8440 if ( $group_id > 0 ) {
8441 $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 ) );
8442 } else {
8443 $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' ) );
8444 }
8445 $custom_ids = $results[0]->user_id;
8446 $args = [ 'include' => $custom_ids ];
8447 $users = get_users( $args );
8448 $group_id = $results[0]->item_id;
8449 $group = groups_get_group( $group_id );
8450 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
8451 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
8452 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
8453 } elseif ( 'bb_group_created' == $term ) {
8454 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups ORDER BY id DESC LIMIT 1" );
8455 if ( function_exists( 'groups_get_group' ) ) {
8456 $group = groups_get_group( $results[0]->id );
8457 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
8458 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
8459 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
8460 $current_types = (array) bp_groups_get_group_type( $results[0]->id, false );
8461 $group_data['group_type'] = $current_types;
8462 $group_data['group_status'] = $group->status;
8463 $group_data['group_date_created'] = $group->date_created;
8464 $group_data['group_enabled_forum'] = $group->enable_forum;
8465 $group_data['group_cover_url'] = bp_get_group_cover_url( $group );
8466 $group_data['group_avatar_url'] = bp_get_group_avatar_url( $group );
8467 $group_data['group_creator'] = WordPress::get_user_context( $group->creator_id );
8468 if ( function_exists( 'groups_get_group_members' ) ) {
8469 $members = groups_get_group_members(
8470 [
8471 'group_id' => $results[0]->id,
8472 ]
8473 );
8474 foreach ( $members['members'] as $key => $member ) {
8475 $group_data['group_member'][ $key ] = WordPress::get_user_context( $member->ID );
8476 }
8477 }
8478 $args = [
8479 'item_id' => $results[0]->id,
8480 ];
8481 $invitations = groups_get_invites( $args );
8482 if ( ! empty( $invitations ) ) {
8483 foreach ( $invitations as $key => $invite ) {
8484 $group_data['invitation'][ $key ] = $invite;
8485 }
8486 }
8487 }
8488 } else {
8489 $users = get_users( $args );
8490
8491 if ( isset( $data['filter']['group_id']['value'] ) ) {
8492 $group_id = $data['filter']['group_id']['value'];
8493 $args['group_id'] = $group_id;
8494 if ( $group_id > 0 ) {
8495 $group = groups_get_group( $group_id );
8496 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
8497 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
8498 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
8499 } else {
8500 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_groups'" );
8501 if ( $table_exists ) {
8502 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups LIMIT 1" );
8503 $context['groups'] = $groups;
8504 if ( ! empty( $groups ) ) {
8505 foreach ( $groups as $group ) {
8506 $group_data['group_id'] = $group->id;
8507 $group_data['group_name'] = $group->name;
8508 $group_data['group_description'] = $group->description;
8509 }
8510 }
8511 }
8512 }
8513 }
8514 }
8515
8516 if ( 'bb_group_created' == $term ) {
8517 if ( ! empty( $group_data ) ) {
8518 $pluggable_data = $group_data;
8519 $context['pluggable_data'] = $pluggable_data;
8520 $context['response_type'] = 'live';
8521 } else {
8522 $context['pluggable_data'] = [
8523 'group_id' => 112,
8524 'group_name' => 'New Group',
8525 'group_description' => 'New Group Description',
8526 'group_type' => [ 'business' ],
8527 'group_status' => 'public',
8528 'group_date_created' => '2024-02-16 05:37:22',
8529 'group_enabled_forum' => 1,
8530 'group_cover_url' => 'https:\/\/example.com\/wp-content\/uploads\/buddypress\/groups\/23\/cover-image\/65cef4c3ea9b6-bp-cover-image.jpeg',
8531 'group_avatar_url' => 'https:\/\/example.com\/wp-content\/uploads\/group-avatars\/23\/65cef4d7cee19-bpfull.png',
8532 'group_creator' => [
8533 'wp_user_id' => 183,
8534 'user_login' => 'johnd',
8535 'display_name' => 'john',
8536 'user_firstname' => 'john',
8537 'user_lastname' => 'd',
8538 'user_email' => 'johnd@yopmail.com',
8539 'user_role' => [
8540 'subscriber',
8541 'bbp_participant',
8542 ],
8543 ],
8544 ];
8545 $context['response_type'] = 'sample';
8546 }
8547 } elseif ( ! empty( $users ) ) {
8548 $user = $users[0];
8549 $pluggable_data = $group_data;
8550
8551 $avatar = get_avatar_url( $user->ID );
8552 $pluggable_data['wp_user_id'] = $user->ID;
8553 $pluggable_data['avatar_url'] = ( $avatar ) ? $avatar : '';
8554 $pluggable_data['user_login'] = $user->user_login;
8555 $pluggable_data['display_name'] = $user->display_name;
8556 $pluggable_data['user_firstname'] = $user->user_firstname;
8557 $pluggable_data['user_lastname'] = $user->user_lastname;
8558 $pluggable_data['user_email'] = $user->user_email;
8559 $pluggable_data['user_role'] = $user->roles;
8560 $context['pluggable_data'] = $pluggable_data;
8561 $context['response_type'] = 'live';
8562 } else {
8563 $context['pluggable_data'] = [
8564 'wp_user_id' => 1,
8565 'user_login' => 'admin',
8566 'display_name' => 'Johnd',
8567 'user_firstname' => 'John',
8568 'user_lastname' => 'D',
8569 'user_email' => 'johnd@gmail.com',
8570 'user_role' => [ 'subscriber' ],
8571 'group_id' => 112,
8572 'group_name' => 'New Group',
8573 'group_description' => 'New Group Description',
8574 ];
8575 $context['response_type'] = 'sample';
8576 }
8577
8578 return $context;
8579 }
8580
8581 /**
8582 * Search complete courses.
8583 *
8584 * @param array $data data.
8585 * @return array
8586 */
8587 public function search_pluggables_complete_course( $data ) {
8588 global $wpdb;
8589 $context = [];
8590
8591 if ( isset( $data['filter']['sfwd_course_id']['value'] ) ) {
8592 $course_id = $data['filter']['sfwd_course_id']['value'];
8593 } else {
8594 $course_id = -1;
8595 }
8596 if ( -1 === $course_id ) {
8597 $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 ) );
8598 } else {
8599 $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 ) );
8600 }
8601
8602 if ( ! empty( $courses ) ) {
8603 $course = $courses[0];
8604 $course_data['course_name'] = $course->post_title;
8605 $course_data['sfwd_course_id'] = $course->ID;
8606 $course_data['course_url'] = get_permalink( $course->ID );
8607 $course_data['course_featured_image_id'] = get_post_meta( $course->ID, '_thumbnail_id', true );
8608 $course_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course->ID );
8609 $timestamp = get_user_meta( $course->user_id, 'course_completed_' . $course->ID, true );
8610 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
8611 $date_format = get_option( 'date_format' );
8612 if ( is_string( $date_format ) ) {
8613 $course_data['course_completion_date'] = wp_date( $date_format, $timestamp );
8614 }
8615 if ( function_exists( 'learndash_get_course_certificate_link' ) ) {
8616 $course_data['course_certificate'] = learndash_get_course_certificate_link( $course->ID, $course->user_id );
8617 }
8618 $context['response_type'] = 'live';
8619 } else {
8620 $course_data['course_name'] = 'Test Course';
8621 $course_data['sfwd_course_id'] = 112;
8622 $course_data['course_url'] = 'https://abc.com/test-course';
8623 $course_data['course_featured_image_id'] = 113;
8624 $course_data['course_featured_image_url'] = 'https://pqr.com/test-course-img';
8625 $course_data['course_completion_date'] = '2023-10-20';
8626 $course_data['course_certificate'] = 'https://example.com/certificates/good-performance/?course_id=112&cert-nonce=f80d0f9cc1';
8627 $context['response_type'] = 'sample';
8628 }
8629
8630 $users_data = $this->search_pluggables_add_user_role( [] );
8631 $user_data = $users_data['pluggable_data'];
8632
8633 $context['pluggable_data'] = array_merge( $course_data, $user_data );
8634 return $context;
8635 }
8636
8637 /**
8638 * Search lessons.
8639 *
8640 * @param array $data data.
8641 * @return array
8642 */
8643 public function search_pluggables_complete_lesson( $data ) {
8644 global $wpdb;
8645 $context = [];
8646
8647 if ( isset( $data['filter']['sfwd_lesson_id']['value'] ) ) {
8648 $lesson_id = $data['filter']['sfwd_lesson_id']['value'];
8649 $course_id = $data['filter']['sfwd_course_id']['value'];
8650 } else {
8651 $lesson_id = -1;
8652 $course_id = -1;
8653 }
8654 if ( -1 === $course_id ) {
8655 $courses = get_posts(
8656 [
8657 'posts_per_page' => - 1,
8658 'post_type' => 'sfwd-courses',
8659 'post_status' => 'publish',
8660 'fields' => 'ids',
8661 ]
8662 );
8663 $course_key = array_rand( $courses );
8664 $course_id = $courses[ $course_key ];
8665 }
8666 $course = get_post( $course_id );
8667 if ( $course instanceof WP_Post ) {
8668 $pluggable_data = LearnDash::get_course_context( $course );
8669 }
8670
8671 if ( -1 === $lesson_id ) {
8672 $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 ) );
8673 } else {
8674 $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 ) );
8675 }
8676
8677 if ( ! empty( $lessons ) ) {
8678 $lesson = $lessons[0];
8679
8680 $pluggable_data = WordPress::get_user_context( $lesson->user_id );
8681 $pluggable_data['lesson_name'] = $lesson->post_title;
8682 $pluggable_data['sfwd_lesson_id'] = $lesson->ID;
8683 $pluggable_data['lesson_url'] = get_permalink( $lesson->ID );
8684 $pluggable_data['lesson_featured_image_id'] = get_post_meta( $lesson->ID, '_thumbnail_id', true );
8685 $pluggable_data['lesson_featured_image_url'] = get_the_post_thumbnail_url( $lesson->ID );
8686 $context['response_type'] = 'live';
8687 } else {
8688 $pluggable_data = WordPress::get_sample_user_context();
8689 $pluggable_data['lesson_name'] = 'Test Lesson';
8690 $pluggable_data['sfwd_lesson_id'] = 114;
8691 $pluggable_data['lesson_url'] = 'https://abc.com/test-lesson';
8692 $pluggable_data['lesson_featured_image_id'] = 116;
8693 $pluggable_data['lesson_featured_image_url'] = 'https://pqr.com/test-lesson-img';
8694 $context['response_type'] = 'sample';
8695 }
8696
8697 $context['pluggable_data'] = $pluggable_data;
8698 return $context;
8699 }
8700
8701 /**
8702 * Search topics.
8703 *
8704 * @param array $data data.
8705 * @return array
8706 */
8707 public function search_pluggables_complete_topic( $data ) {
8708 global $wpdb;
8709 $context = [];
8710
8711 if ( isset( $data['filter']['sfwd_topic_id']['value'] ) ) {
8712 $topic_id = $data['filter']['sfwd_topic_id']['value'];
8713 $course_id = $data['filter']['sfwd_course_id']['value'];
8714 } else {
8715 $topic_id = -1;
8716 $course_id = -1;
8717 }
8718 if ( -1 === $course_id ) {
8719 $courses = get_posts(
8720 [
8721 'posts_per_page' => - 1,
8722 'post_type' => 'sfwd-courses',
8723 'post_status' => 'publish',
8724 'fields' => 'ids',
8725 ]
8726 );
8727 $course_key = array_rand( $courses );
8728 $course_id = $courses[ $course_key ];
8729 }
8730 $course = get_post( $course_id );
8731 if ( $course instanceof WP_Post ) {
8732 $pluggable_data = LearnDash::get_course_context( $course );
8733 }
8734
8735 if ( -1 === $topic_id ) {
8736 $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 ) );
8737 } else {
8738 $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 ) );
8739 }
8740
8741 if ( ! empty( $topics ) ) {
8742 $topic = $topics[0];
8743 $pluggable_data = WordPress::get_user_context( $topics[0]->user_id );
8744 $pluggable_data['topic_name'] = $topic->post_title;
8745 $pluggable_data['sfwd_topic_id'] = $topic->ID;
8746 $pluggable_data['topic_url'] = get_permalink( $topic->ID );
8747 $pluggable_data['topic_featured_image_id'] = get_post_meta( $topic->ID, '_thumbnail_id', true );
8748 $pluggable_data['topic_featured_image_url'] = get_the_post_thumbnail_url( $topic->ID );
8749 $context['response_type'] = 'live';
8750 } else {
8751 $pluggable_data = WordPress::get_sample_user_context();
8752 $pluggable_data['topic_name'] = 'Test Topic';
8753 $pluggable_data['sfwd_topic_id'] = 117;
8754 $pluggable_data['topic_url'] = 'https://abc.com/test-topic';
8755 $pluggable_data['topic_featured_image_id'] = 118;
8756 $pluggable_data['topic_featured_image_url'] = 'https://pqr.com/test-topic-img';
8757 $context['response_type'] = 'sample';
8758 }
8759
8760 $context['pluggable_data'] = $pluggable_data;
8761 return $context;
8762 }
8763
8764 /**
8765 * Search purchase courses.
8766 *
8767 * @param array $data data.
8768 * @return array
8769 */
8770 public function search_pluggables_purchase_course( $data ) {
8771 $context = [];
8772 $context['response_type'] = 'sample';
8773
8774 $purchase_data = [
8775 'course_product_id' => '1',
8776 'course_product_name' => 'Sample Course',
8777 'currency' => 'USD',
8778 'total_amount' => '100',
8779 'first_name' => 'John',
8780 'last_name' => 'Doe',
8781 'email' => 'john_doe@gmail.com',
8782 'phone' => '+923007626541',
8783 ];
8784
8785 $product_id = (int) ( isset( $data['filter']['course_product_id']['value'] ) ? $data['filter']['course_product_id']['value'] : '-1' );
8786 $order_id = 0;
8787
8788 if ( -1 !== $product_id ) {
8789 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
8790
8791 if ( is_array( $order_ids ) && count( $order_ids ) > 0 ) {
8792 $order_id = $order_ids[0];
8793 }
8794 } else {
8795 $orders = wc_get_orders( [] );
8796 if ( is_array( $orders ) && count( $orders ) > 0 ) {
8797 foreach ( $orders as $order ) {
8798 $items = $order->get_items();
8799
8800 if ( count( $items ) > 1 ) {
8801 continue;
8802 }
8803
8804 foreach ( $items as $item ) {
8805 if ( method_exists( $item, 'get_product_id' ) ) {
8806 $product_id = $item->get_product_id();
8807 if ( ! empty( get_post_meta( $item->get_product_id(), '_related_course', true ) ) ) {
8808 $order_id = $order->get_id();
8809 break;
8810 }
8811 }
8812 }
8813 }
8814 }
8815 }
8816
8817 if ( 0 !== $order_id ) {
8818 $order = wc_get_order( $order_id );
8819
8820 if ( $order && $order instanceof \WC_Order ) {
8821
8822 $purchase_data = LearnDash::get_purchase_course_context( $order );
8823
8824 $context['response_type'] = 'live';
8825 }
8826 }
8827
8828 $context['pluggable_data'] = $purchase_data;
8829
8830 return $context;
8831 }
8832
8833 /**
8834 * Search quiz data in LearnDash.
8835 *
8836 * @param array $data data.
8837 * @return array
8838 */
8839 public function search_pluggables_ld_quiz( $data ) {
8840 $context = [];
8841 $context['response_type'] = 'sample';
8842 global $wpdb;
8843 $user_data = WordPress::get_sample_user_context();
8844 $quiz_data = [
8845 'quiz' => 7193,
8846 'score' => 1,
8847 'count' => 1,
8848 'question_show_count' => 1,
8849 'pass' => 1,
8850 'rank' => '-',
8851 'time' => 1703595328,
8852 'pro_quizid' => 1,
8853 'course' => 0,
8854 'lesson' => 0,
8855 'topic' => 0,
8856 'points' => 20,
8857 'total_points' => 20,
8858 'percentage' => 100,
8859 'timespent' => 2.7309999999999999,
8860 'has_graded' => false,
8861 'statistic_ref_id' => 9,
8862 'started' => 1703595325,
8863 'completed' => 1703595328,
8864 'ld_version' => '4.8.0.1',
8865 'quiz_key' => '1703595328_1_7193_0',
8866 ];
8867 $output_questions['question'] = [
8868 'ID' => 7195,
8869 'post_content' => '<p>This is a first question<\/p>',
8870 'type' => 'sfwd-question',
8871 'question_type' => 'single',
8872 'points' => 20,
8873 'answers' => [
8874 'sort_answer' => [
8875 [
8876 'answer' => '',
8877 'points' => 0,
8878 'correct' => false,
8879 'type' => 'answer',
8880 ],
8881 ],
8882 'classic_answer' => [
8883 [
8884 'answer' => 'Ans 1',
8885 'points' => 1,
8886 'correct' => false,
8887 'type' => 'answer',
8888 ],
8889 [
8890 'answer' => 'Ans 2',
8891 'points' => 0,
8892 'correct' => true,
8893 'type' => 'answer',
8894 ],
8895 ],
8896 'matrix_sort_answer' => [
8897 [
8898 'answer' => '',
8899 'points' => 0,
8900 'correct' => false,
8901 'type' => 'answer',
8902 ],
8903 ],
8904 'cloze_answer' => [
8905 [
8906 'answer' => '',
8907 'points' => 0,
8908 'correct' => false,
8909 'type' => 'answer',
8910 ],
8911 ],
8912 'free_answer' => [
8913 [
8914 'answer' => '',
8915 'points' => 0,
8916 'correct' => false,
8917 'type' => 'answer',
8918 ],
8919 ],
8920 'assessment_answer' => [
8921 [
8922 'answer' => '',
8923 'points' => 0,
8924 'correct' => false,
8925 'type' => 'answer',
8926 ],
8927 ],
8928 'essay' => [
8929 [
8930 'answer' => '',
8931 'points' => 0,
8932 'correct' => false,
8933 'type' => 'answer',
8934 ],
8935 ],
8936 ],
8937 ];
8938
8939 $quiz_id = (int) ( isset( $data['filter']['sfwd_quiz_id']['value'] ) ? $data['filter']['sfwd_quiz_id']['value'] : '-1' );
8940 $question_id = $data['filter']['sfwd_question_id']['value'];
8941 $term = $data['search_term'];
8942 $mark = 1;
8943 if ( 'passes_quiz' == $term ) {
8944 $mark = 1;
8945 } elseif ( 'fails_quiz' == $term ) {
8946 $mark = 0;
8947 }
8948 if ( 'passes_quiz' == $term || 'fails_quiz' == $term ) {
8949 if ( -1 == $quiz_id ) {
8950 $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 ) );
8951 } else {
8952 $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 ) );
8953 }
8954 } elseif ( 'quiz_essay_submitted' == $term ) {
8955 $question_id = $data['filter']['sfwd_question_id']['value'];
8956 $args = [
8957 'post_type' => 'sfwd-essays',
8958 'posts_per_page' => 1,
8959 'order' => 'DESC',
8960 'post_status' => [ 'graded', 'not_graded' ],
8961 'meta_query' => [
8962 'relation' => 'AND',
8963 [
8964 'key' => 'quiz_post_id',
8965 'value' => $quiz_id,
8966 'compare' => '=',
8967 ],
8968 [
8969 'key' => 'question_post_id',
8970 'value' => $question_id,
8971 'compare' => '=',
8972 ],
8973 ],
8974 ];
8975 $essay = get_posts( $args );
8976 } elseif ( 'quiz_essay_graded' == $term ) {
8977 $args = [
8978 'post_type' => 'sfwd-essays',
8979 'posts_per_page' => 1,
8980 'order' => 'DESC',
8981 'post_status' => 'graded',
8982 'meta_query' => [
8983 'relation' => 'AND',
8984 [
8985 'key' => 'quiz_post_id',
8986 'value' => $quiz_id,
8987 'compare' => '=',
8988 ],
8989 [
8990 'key' => 'question_post_id',
8991 'value' => $question_id,
8992 'compare' => '=',
8993 ],
8994 ],
8995 ];
8996 $essay = get_posts( $args );
8997 }
8998
8999 if ( 'quiz_essay_submitted' == $term || 'quiz_essay_graded' == $term ) {
9000 if ( ! empty( $essay ) ) {
9001 $context = is_numeric( $essay[0]->post_author ) ? WordPress::get_user_context( (int) $essay[0]->post_author ) : [];
9002 $course_id = get_post_meta( $essay[0]->ID, 'course_id', true );
9003 $lesson_id = get_post_meta( $essay[0]->ID, 'lesson_id', true );
9004 $context['quiz_name'] = get_the_title( $quiz_id );
9005 $context['sfwd_quiz_id'] = $quiz_id;
9006 $context['sfwd_question_id'] = $question_id;
9007 $context['question_name'] = is_numeric( $question_id ) ? get_the_title( (int) $question_id ) : null;
9008 $context['course_name'] = is_numeric( $course_id ) ? get_the_title( (int) $course_id ) : null;
9009 $context['course_id'] = $course_id;
9010 $context['lesson_name'] = is_numeric( $lesson_id ) ? get_the_title( (int) $lesson_id ) : null;
9011 $context['lesson_id'] = $lesson_id;
9012 $context['essay_id'] = $essay[0]->ID;
9013 $context['essay'] = WordPress::get_post_context( $essay[0]->ID );
9014 if ( 'quiz_essay_graded' == $term ) {
9015 $users_quiz_data = get_user_meta( (int) $essay[0]->post_author, '_sfwd-quizzes', true );
9016 if ( is_array( $users_quiz_data ) && is_array( $users_quiz_data[0] ) ) {
9017 $essay_post_id = $essay[0]->ID;
9018 $result = array_filter(
9019 $users_quiz_data[0]['graded'],
9020 function( $item ) use ( $essay_post_id ) {
9021 return $item['post_id'] === $essay_post_id;
9022 }
9023 );
9024 $context['essay_points_earned'] = $result[1]['points_awarded'];
9025 }
9026 }
9027 $context['response_type'] = 'live';
9028 } else {
9029 $context = WordPress::get_sample_user_context();
9030 $context['quiz_name'] = 'Test Quiz';
9031 $context['sfwd_quiz_id'] = 11;
9032 $context['sfwd_question_id'] = 12;
9033 $context['course_name'] = 'Test Course';
9034 $context['course_id'] = 13;
9035 $context['lesson_name'] = 'Test Lesson';
9036 $context['lesson_id'] = 14;
9037 $context['essay'] = [
9038 'ID' => 12,
9039 'post_content' => 'demo',
9040 ];
9041 $context['response_type'] = 'sample';
9042 }
9043
9044 $context['pluggable_data'] = $context;
9045 } else {
9046 if ( ! empty( $result ) ) {
9047 $user_data = WordPress::get_user_context( $result[0]->user_id );
9048 $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 ) );
9049
9050 $data = unserialize( $quiz_result[0]->meta_value );
9051 $found_element = null;
9052 foreach ( (array) $data as $element ) {
9053 if ( is_array( $element ) ) {
9054 if ( isset( $element['statistic_ref_id'] ) && $element['statistic_ref_id'] == $result[0]->statistic_ref_id ) {
9055 $found_element = $element;
9056 break;
9057 }
9058 }
9059 }
9060 $quiz_data = (array) $found_element;
9061 $output_questions = LearnDash::get_quiz_questions_answers( $quiz_id );
9062 if ( is_array( $quiz_data ) ) {
9063 $quiz_data['quiz_name'] = get_the_title( $quiz_id );
9064 $quiz_data['sfwd_quiz_id'] = $quiz_id;
9065 }
9066 $context['response_type'] = 'live';
9067 }
9068 $context['pluggable_data'] = array_merge( $quiz_data, $user_data, $output_questions );
9069 }
9070
9071 return $context;
9072 }
9073
9074 /**
9075 * Fetch BB templates.
9076 *
9077 * @return array
9078 */
9079 public function get_beaver_builder_templates() {
9080 $allowed_types = [ 'subscribe-form', 'contact-form' ];
9081 $templates = [];
9082 $all_templates = get_posts(
9083 [
9084 'post_type' => 'fl-builder-template',
9085 'meta_key' => '_fl_builder_data',
9086 'posts_per_page' => -1,
9087 ]
9088 );
9089 $posts = get_posts(
9090 [
9091 'post_type' => 'any',
9092 'meta_key' => '_fl_builder_data',
9093 'posts_per_page' => -1,
9094 ]
9095 );
9096 $posts = array_merge( $all_templates, $posts );
9097
9098 if ( ! empty( $posts ) ) {
9099 foreach ( $posts as $post ) {
9100 $meta = get_post_meta( $post->ID, '_fl_builder_data', true );
9101 foreach ( (array) $meta as $node_id => $node ) {
9102 if ( is_object( $node ) && isset( $node->type ) && 'module' === $node->type && property_exists( $node, 'settings' ) ) {
9103 $settings = $node->settings;
9104 if ( in_array( $settings->type, $allowed_types, true ) ) {
9105 $label = $post->post_title;
9106 if ( '' !== $settings->node_label ) {
9107 $label .= ' - ' . $settings->node_label;
9108 }
9109 $templates[] = [
9110 'label' => $label,
9111 'value' => $node_id,
9112 ];
9113 }
9114 }
9115 }
9116 }
9117 }
9118 return $templates;
9119 }
9120
9121 /**
9122 * Search beaver builder forms.
9123 *
9124 * @param array $data data.
9125 * @return array
9126 */
9127 public function search_beaver_builder_forms( $data ) {
9128 $templates = $this->get_beaver_builder_templates();
9129 return [
9130 'options' => $templates,
9131 'hasMore' => false,
9132 ];
9133 }
9134
9135 /**
9136 * Search FluentCRM fields.
9137 *
9138 * @param array $data data.
9139 * @return array
9140 */
9141 public function search_fluentcrm_custom_fields( $data ) {
9142 $context = [];
9143 if ( ! class_exists( 'FluentCrm\App\Models\CustomContactField' ) ) {
9144 return [];
9145 }
9146 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
9147 $context['fields'] = $custom_fields;
9148 return $context;
9149 }
9150
9151 /**
9152 * Search FluentCRM Company fields.
9153 *
9154 * @param array $data data.
9155 * @return array
9156 */
9157 public function search_fluentcrm_company_custom_fields( $data ) {
9158 $context = [];
9159 if ( function_exists( 'fluentcrm_get_custom_company_fields' ) ) {
9160 $custom_fields = fluentcrm_get_custom_company_fields();
9161 $context['fields'] = $custom_fields;
9162 }
9163 return $context;
9164 }
9165
9166 /**
9167 * Search FluentCRM fields and display it in dropdown for trigger.
9168 *
9169 * @param array $data data.
9170 * @return array
9171 */
9172 public function search_fluentcrm_custom_fields_data( $data ) {
9173
9174 if ( ! class_exists( 'FluentCrm\App\Models\CustomContactField' ) ) {
9175 return [];
9176 }
9177
9178 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
9179 $options = [];
9180
9181 if ( ! empty( $custom_fields ) ) {
9182 foreach ( $custom_fields as $custom_field ) {
9183 $options[] = [
9184 'label' => $custom_field['label'],
9185 'value' => $custom_field['slug'],
9186 ];
9187 }
9188 }
9189
9190 return [
9191 'options' => $options,
9192 'hasMore' => false,
9193 ];
9194 }
9195
9196 /**
9197 * Fetch WP JOB Manager Last Data.
9198 *
9199 * @param array $data data.
9200 * @return array
9201 */
9202 public function search_wp_job_manger_last_data( $data ) {
9203 global $wpdb;
9204 $job_type = $data['filter']['job_type']['value'];
9205 $args = [
9206 'posts_per_page' => 1,
9207 'post_type' => 'job_listing',
9208 'orderby' => 'id',
9209 'order' => 'DESC',
9210 ];
9211
9212 if ( -1 !== $job_type ) {
9213 $args['tax_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
9214 [
9215 'taxonomy' => 'job_listing_type',
9216 'field' => 'term_id',
9217 'terms' => $job_type,
9218 ],
9219 ];
9220 }
9221 $posts = get_posts( $args );
9222 if ( empty( $posts ) ) {
9223 $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 );
9224 return $context;
9225 }
9226
9227 $post = $posts[0];
9228 $post_content = WordPress::get_post_context( $post->ID );
9229 $post_meta = WordPress::get_post_meta( $post->ID );
9230 $job_data = array_merge(
9231 $post_content,
9232 is_array( $post_meta ) ? $post_meta : [],
9233 is_numeric( $post->post_author ) ? WordPress::get_user_context( (int) $post->post_author ) : []
9234 );
9235 foreach ( $job_data as $key => $job ) {
9236 $newkey = str_replace( 'post', 'wpjob', $key );
9237 unset( $job_data[ $key ] );
9238 $job_data[ $newkey ] = $job;
9239 }
9240 $context['response_type'] = 'live';
9241 $context['pluggable_data'] = $job_data;
9242 return $context;
9243
9244 }
9245
9246 /**
9247 * Get Amelia Appointment Category.
9248 *
9249 * @param array $data data.
9250 *
9251 * @return array
9252 */
9253 public function search_amelia_category_list( $data ) {
9254
9255 global $wpdb;
9256
9257 $page = $data['page'];
9258 $limit = Utilities::get_search_page_limit();
9259 $offset = $limit * ( $page - 1 );
9260
9261 $categories = $wpdb->get_results(
9262 $wpdb->prepare(
9263 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_categories WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
9264 [ 'visible', $limit, $offset ]
9265 ),
9266 OBJECT
9267 );
9268
9269 $categories_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9270
9271 $options = [];
9272 if ( ! empty( $categories ) ) {
9273 foreach ( $categories as $category ) {
9274 $options[] = [
9275 'label' => $category->name,
9276 'value' => $category->id,
9277 ];
9278 }
9279 }
9280
9281 return [
9282 'options' => $options,
9283 'hasMore' => $categories_count > $limit && $categories_count > $offset,
9284 ];
9285
9286 }
9287
9288 /**
9289 * Get Amelia Appointment Services.
9290 *
9291 * @param array $data data.
9292 *
9293 * @return array
9294 */
9295 public function search_amelia_service_list( $data ) {
9296
9297 global $wpdb;
9298
9299 $page = $data['page'];
9300 $limit = Utilities::get_search_page_limit();
9301 $offset = $limit * ( $page - 1 );
9302
9303 $services = $wpdb->get_results(
9304 $wpdb->prepare(
9305 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_services
9306 WHERE categoryId = %d AND status = %s
9307 ORDER BY name ASC LIMIT %d OFFSET %d",
9308 [ $data['dynamic'], 'visible', $limit, $offset ]
9309 ),
9310 OBJECT
9311 );
9312
9313 $services_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9314
9315 $options = [];
9316 if ( ! empty( $services ) ) {
9317 foreach ( $services as $category ) {
9318 $options[] = [
9319 'label' => $category->name,
9320 'value' => $category->id,
9321 ];
9322 }
9323 }
9324
9325 return [
9326 'options' => $options,
9327 'hasMore' => $services_count > $limit && $services_count > $offset,
9328 ];
9329
9330 }
9331
9332 /**
9333 * Get Amelia Package List.
9334 *
9335 * @param array $data data.
9336 *
9337 * @return array
9338 */
9339 public function search_amelia_package_list( $data ) {
9340 global $wpdb;
9341
9342 $page = isset( $data['page'] ) ? $data['page'] : 1;
9343 $limit = Utilities::get_search_page_limit();
9344 $offset = $limit * ( $page - 1 );
9345
9346 $packages = $wpdb->get_results(
9347 $wpdb->prepare(
9348 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_packages
9349 WHERE status = %s
9350 ORDER BY name ASC LIMIT %d OFFSET %d",
9351 [ 'visible', $limit, $offset ]
9352 ),
9353 OBJECT
9354 );
9355
9356 $packages_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9357
9358 $options = [];
9359 if ( ! empty( $packages ) ) {
9360 foreach ( $packages as $package ) {
9361 $options[] = [
9362 'label' => $package->name,
9363 'value' => $package->id,
9364 ];
9365 }
9366 }
9367
9368 return [
9369 'options' => $options,
9370 'hasMore' => $packages_count > $limit && $packages_count > $offset,
9371 ];
9372
9373 }
9374
9375 /**
9376 * Get Amelia Events.
9377 *
9378 * @param array $data data.
9379 *
9380 * @return array
9381 */
9382 public function search_amelia_events_list( $data ) {
9383
9384 global $wpdb;
9385
9386 $page = $data['page'];
9387 $limit = Utilities::get_search_page_limit();
9388 $offset = $limit * ( $page - 1 );
9389
9390 $events = $wpdb->get_results(
9391 $wpdb->prepare(
9392 "SELECT SQL_CALC_FOUND_ROWS id, name from {$wpdb->prefix}amelia_events WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
9393 [ 'approved', $limit, $offset ]
9394 ),
9395 OBJECT
9396 );
9397
9398 $list_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9399
9400 $options = [];
9401 if ( ! empty( $events ) ) {
9402 foreach ( $events as $event ) {
9403 $options[] = [
9404 'label' => $event->name,
9405 'value' => $event->id,
9406 ];
9407 }
9408 }
9409
9410 return [
9411 'options' => $options,
9412 'hasMore' => $list_count > $limit && $list_count > $offset,
9413 ];
9414
9415 }
9416
9417 /**
9418 * Get Amelia Events.
9419 *
9420 * @param array $data data.
9421 *
9422 * @return array
9423 */
9424 public function search_amelia_booking_status_list( $data ) {
9425
9426 $options = [];
9427 $options[] = [
9428 'label' => 'Approved',
9429 'value' => 'approved',
9430 ];
9431 $options[] = [
9432 'label' => 'Pending',
9433 'value' => 'pending',
9434 ];
9435 $options[] = [
9436 'label' => 'Rejected',
9437 'value' => 'rejected',
9438 ];
9439 $options[] = [
9440 'label' => 'Canceled',
9441 'value' => 'canceled',
9442 ];
9443 $options[] = [
9444 'label' => 'No-show',
9445 'value' => 'no-show',
9446 ];
9447 return [
9448 'options' => $options,
9449 'hasMore' => false,
9450 ];
9451 }
9452
9453 /**
9454 * Get last data for trigger.
9455 *
9456 * @param array $data data.
9457 * @return array
9458 */
9459 public function search_amelia_appointment_booked_triggers_last_data( $data ) {
9460 global $wpdb;
9461
9462 $context = [];
9463
9464 $appointment_category = $data['filter']['amelia_category_list']['value'];
9465 $appointment_service = $data['filter']['amelia_service_list']['value'];
9466
9467 if ( -1 === $appointment_service ) {
9468 // If service exists as per category selected.
9469 $service_exist = $wpdb->get_row(
9470 $wpdb->prepare(
9471 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
9472 [ $appointment_category ]
9473 ),
9474 ARRAY_A
9475 );
9476
9477 if ( empty( $service_exist ) ) {
9478 $result = [];
9479 } else {
9480 $result = $wpdb->get_row(
9481 '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 )',
9482 ARRAY_A
9483 );
9484 }
9485 } else {
9486 $result = $wpdb->get_row(
9487 $wpdb->prepare(
9488 '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',
9489 [ $appointment_service ]
9490 ),
9491 ARRAY_A
9492 );
9493 }
9494
9495 if ( ! empty( $result ) ) {
9496
9497 $payment_result = $wpdb->get_row(
9498 $wpdb->prepare(
9499 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
9500 [ $result['id'] ]
9501 ),
9502 ARRAY_A
9503 );
9504
9505 $customer_result = $wpdb->get_row(
9506 $wpdb->prepare(
9507 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
9508 [ $result['customerId'] ]
9509 ),
9510 ARRAY_A
9511 );
9512
9513 $service_result = $wpdb->get_row(
9514 $wpdb->prepare(
9515 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
9516 [ $result['serviceId'] ]
9517 ),
9518 ARRAY_A
9519 );
9520
9521 $category_result = $wpdb->get_row(
9522 $wpdb->prepare(
9523 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
9524 [ $service_result['categoryId'] ]
9525 ),
9526 ARRAY_A
9527 );
9528
9529 if ( $result['couponId'] ) {
9530 $coupon_result = $wpdb->get_row(
9531 $wpdb->prepare(
9532 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9533 [ $result['couponId'] ]
9534 ),
9535 ARRAY_A
9536 );
9537 } else {
9538 $coupon_result = [];
9539 }
9540
9541 if ( ! empty( $result['customFields'] ) ) {
9542 $custom_fields = json_decode( $result['customFields'], true );
9543
9544 $fields_arr = [];
9545 foreach ( (array) $custom_fields as $fields ) {
9546 if ( is_array( $fields ) ) {
9547 $fields_arr[ $fields['label'] ] = $fields['value'];
9548 }
9549 }
9550 unset( $result['customFields'] );
9551 } else {
9552 $fields_arr = [];
9553 }
9554
9555 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
9556 $context['response_type'] = 'live';
9557 } else {
9558
9559 $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 );
9560 }
9561
9562 return $context;
9563 }
9564
9565 /**
9566 * Get last data for package purchase triggers (both purchase and status updated).
9567 *
9568 * @param array $data data.
9569 * @return array
9570 */
9571 public function search_amelia_package_purchase_triggers_last_data( $data ) {
9572 global $wpdb;
9573
9574 $context = [];
9575 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
9576 $package_filter = isset( $data['filter']['amelia_package_list']['value'] ) ? $data['filter']['amelia_package_list']['value'] : -1;
9577
9578
9579 if ( -1 === $package_filter ) {
9580 $result = $wpdb->get_row(
9581 'SELECT packages.*, customer.*, pc.* FROM ' . $wpdb->prefix . 'amelia_packages_to_customers as pc
9582 INNER JOIN ' . $wpdb->prefix . 'amelia_packages as packages ON pc.packageId=packages.id
9583 INNER JOIN ' . $wpdb->prefix . 'amelia_users as customer ON pc.customerId=customer.id
9584 WHERE pc.id = ( SELECT max(id) FROM ' . $wpdb->prefix . 'amelia_packages_to_customers )',
9585 ARRAY_A
9586 );
9587 } else {
9588 $result = $wpdb->get_row(
9589 $wpdb->prepare(
9590 'SELECT packages.*, customer.*, pc.* FROM ' . $wpdb->prefix . 'amelia_packages_to_customers as pc
9591 INNER JOIN ' . $wpdb->prefix . 'amelia_packages as packages ON pc.packageId=packages.id
9592 INNER JOIN ' . $wpdb->prefix . 'amelia_users as customer ON pc.customerId=customer.id
9593 WHERE packages.id = %d AND pc.id = ( SELECT max(id) FROM ' . $wpdb->prefix . 'amelia_packages_to_customers WHERE packageId = %d )',
9594 [ $package_filter, $package_filter ]
9595 ),
9596 ARRAY_A
9597 );
9598 }
9599
9600 if ( ! empty( $result ) ) {
9601 $payment_result = $wpdb->get_row(
9602 $wpdb->prepare(
9603 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE packageCustomerId = %d',
9604 [ $result['id'] ]
9605 ),
9606 ARRAY_A
9607 );
9608
9609 if ( empty( $payment_result ) ) {
9610 $payment_result = [];
9611 }
9612
9613 if ( ! empty( $result['couponId'] ) ) {
9614 $coupon_result = $wpdb->get_row(
9615 $wpdb->prepare(
9616 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9617 [ $result['couponId'] ]
9618 ),
9619 ARRAY_A
9620 );
9621 } else {
9622 $coupon_result = [];
9623 }
9624
9625 $fields_arr = [];
9626 if ( ! empty( $result['customFields'] ) ) {
9627 $custom_fields = json_decode( $result['customFields'], true );
9628 if ( is_array( $custom_fields ) ) {
9629 foreach ( $custom_fields as $fields ) {
9630 if ( is_array( $fields ) && isset( $fields['label'], $fields['value'] ) ) {
9631 $fields_arr[ $fields['label'] ] = $fields['value'];
9632 }
9633 }
9634 }
9635 unset( $result['customFields'] );
9636 }
9637
9638 $json_fields = [ 'limitPerCustomer', 'settings', 'translations' ];
9639 foreach ( $json_fields as $field ) {
9640 if ( isset( $result[ $field ] ) && is_string( $result[ $field ] ) && ! empty( $result[ $field ] ) ) {
9641 $decoded = json_decode( $result[ $field ], true );
9642 if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) {
9643 foreach ( $decoded as $key => $value ) {
9644 if ( is_array( $value ) ) {
9645 foreach ( $value as $subkey => $subvalue ) {
9646 $result[ $field . '_' . $key . '_' . $subkey ] = $subvalue;
9647 }
9648 } else {
9649 $result[ $field . '_' . $key ] = $value;
9650 }
9651 }
9652 $result[ $field . '_original' ] = $decoded;
9653 unset( $result[ $field ] );
9654 }
9655 }
9656 }
9657
9658 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $coupon_result );
9659 $context['pluggable_data']['amelia_package_list'] = $result['packageId'];
9660
9661 switch ( $term ) {
9662 case 'amelia_package_purchase_status_updated':
9663 $context['pluggable_data']['old_status'] = isset( $result['status'] ) ? $result['status'] : 'pending';
9664 $context['pluggable_data']['new_status'] = 'approved';
9665 break;
9666 }
9667
9668 $context['response_type'] = 'live';
9669 } else {
9670 $context = [
9671 'response_type' => 'sample',
9672 'pluggable_data' => [
9673 'id' => '1',
9674 'name' => 'Premium Wellness Package',
9675 'description' => 'Complete wellness package with multiple services',
9676 'color' => '#1788FB',
9677 'price' => '1800',
9678 'status' => 'visible',
9679 'pictureFullPath' => '',
9680 'pictureThumbPath' => '',
9681 'position' => '1',
9682 'calculatedPrice' => '1',
9683 'discount' => '0',
9684 'endDate' => '',
9685 'durationType' => '',
9686 'durationCount' => '',
9687 'depositPayment' => 'disabled',
9688 'deposit' => '0',
9689 'fullPayment' => '0',
9690 'sharedCapacity' => '0',
9691 'quantity' => '1',
9692 'type' => 'customer',
9693 'externalId' => '',
9694 'firstName' => 'John',
9695 'lastName' => 'Doe',
9696 'email' => 'john.doe@example.com',
9697 'birthday' => '1990-05-15',
9698 'phone' => '+1-234-567-8900',
9699 'gender' => 'male',
9700 'note' => 'Regular customer',
9701 'password' => '',
9702 'usedTokens' => '',
9703 'zoomUserId' => '',
9704 'stripeConnect' => '',
9705 'countryPhoneIso' => 'us',
9706 'timeZone' => 'America/New_York',
9707 'appleCalendarId' => '',
9708 'employeeAppleCalendar' => '',
9709 'badgeId' => '',
9710 'error' => '',
9711 'show' => '1',
9712 'customerId' => '4',
9713 'customer' => '',
9714 'couponId' => '',
9715 'coupon' => '',
9716 'tax' => '',
9717 'packageId' => '1',
9718 'payments' => [],
9719 'start' => '2025-12-15 08:39:52',
9720 'end' => '2025-12-29 08:39:52',
9721 'purchased' => '2025-12-15 08:39:52',
9722 'bookingsCount' => '3',
9723 'token' => 'abc123xyz',
9724 'limitPerCustomer_enabled' => '1',
9725 'limitPerCustomer_numberOfApp' => '15',
9726 'limitPerCustomer_timeFrame' => 'day',
9727 'limitPerCustomer_period' => '15',
9728 'limitPerCustomer_original' => [
9729 'enabled' => true,
9730 'numberOfApp' => 15,
9731 'timeFrame' => 'day',
9732 'period' => 15,
9733 ],
9734 'settings_payments_paymentLinks' => [
9735 'enabled' => '',
9736 'changeBookingStatus' => '',
9737 'redirectUrl' => '',
9738 ],
9739 'settings_original' => [
9740 'payments' => [
9741 'paymentLinks' => [
9742 'enabled' => '',
9743 'changeBookingStatus' => '',
9744 'redirectUrl' => '',
9745 ],
9746 ],
9747 ],
9748 'translations_defaultLanguage' => 'en_US',
9749 'translations_original' => [
9750 'defaultLanguage' => 'en_US',
9751 ],
9752 'amelia_package_list' => '1',
9753 ],
9754 ];
9755
9756 switch ( $term ) {
9757 case 'amelia_package_purchase_status_updated':
9758 $context['pluggable_data']['old_status'] = 'pending';
9759 $context['pluggable_data']['new_status'] = 'approved';
9760 break;
9761 }
9762 }
9763
9764 return $context;
9765 }
9766
9767 /**
9768 * Get last data for trigger.
9769 *
9770 * @param array $data data.
9771 * @return array
9772 */
9773 public function search_amelia_appointment_booking_status_changed_triggers_last_data( $data ) {
9774 global $wpdb;
9775
9776 $context = [];
9777
9778 $appointment_category = $data['filter']['amelia_category_list']['value'];
9779 $appointment_service = $data['filter']['amelia_service_list']['value'];
9780 $appointment_status = $data['filter']['appointment_status']['value'];
9781
9782 if ( -1 === $appointment_service ) {
9783 // If service exists as per category selected.
9784 $service_exist = $wpdb->get_row(
9785 $wpdb->prepare(
9786 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
9787 [ $appointment_category ]
9788 ),
9789 ARRAY_A
9790 );
9791
9792 if ( empty( $service_exist ) ) {
9793 $result = [];
9794 } else {
9795 $result = $wpdb->get_row(
9796 '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 )',
9797 ARRAY_A
9798 );
9799 }
9800 } else {
9801 if ( -1 === $appointment_status ) {
9802 $result = $wpdb->get_row(
9803 $wpdb->prepare(
9804 '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',
9805 [ $appointment_service ]
9806 ),
9807 ARRAY_A
9808 );
9809 } else {
9810 $result = $wpdb->get_row(
9811 $wpdb->prepare(
9812 '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',
9813 [ $appointment_status, $appointment_service ]
9814 ),
9815 ARRAY_A
9816 );
9817 }
9818 }
9819
9820 if ( ! empty( $result ) ) {
9821
9822 $payment_result = $wpdb->get_row(
9823 $wpdb->prepare(
9824 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
9825 [ $result['id'] ]
9826 ),
9827 ARRAY_A
9828 );
9829
9830 $customer_result = $wpdb->get_row(
9831 $wpdb->prepare(
9832 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
9833 [ $result['customerId'] ]
9834 ),
9835 ARRAY_A
9836 );
9837
9838 $service_result = $wpdb->get_row(
9839 $wpdb->prepare(
9840 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
9841 [ $result['serviceId'] ]
9842 ),
9843 ARRAY_A
9844 );
9845
9846 $category_result = $wpdb->get_row(
9847 $wpdb->prepare(
9848 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
9849 [ $service_result['categoryId'] ]
9850 ),
9851 ARRAY_A
9852 );
9853
9854 if ( $result['couponId'] ) {
9855 $coupon_result = $wpdb->get_row(
9856 $wpdb->prepare(
9857 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9858 [ $result['couponId'] ]
9859 ),
9860 ARRAY_A
9861 );
9862 } else {
9863 $coupon_result = [];
9864 }
9865
9866 if ( ! empty( $result['customFields'] ) ) {
9867 $custom_fields = json_decode( $result['customFields'], true );
9868
9869 $fields_arr = [];
9870 foreach ( (array) $custom_fields as $fields ) {
9871 if ( is_array( $fields ) ) {
9872 $fields_arr[ $fields['label'] ] = $fields['value'];
9873 }
9874 }
9875 unset( $result['customFields'] );
9876 } else {
9877 $fields_arr = [];
9878 }
9879
9880 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
9881 $context['pluggable_data']['amelia_category_list'] = $appointment_category;
9882 if ( -1 === $appointment_status ) {
9883 $context['pluggable_data']['appointment_status'] = 'approved';
9884 } else {
9885 $context['pluggable_data']['appointment_status'] = $appointment_status;
9886 }
9887 if ( -1 === $appointment_service ) {
9888 $context['pluggable_data']['amelia_service_list'] = $service_result['id'];
9889 } else {
9890 $context['pluggable_data']['amelia_service_list'] = $appointment_service;
9891 }
9892 $context['response_type'] = 'live';
9893 } else {
9894
9895 $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 );
9896 }
9897
9898 return $context;
9899 }
9900
9901 /**
9902 * Get last data for trigger.
9903 *
9904 * @param array $data data.
9905 * @return array
9906 */
9907 public function search_amelia_event_status_changed_triggers_last_data( $data ) {
9908 global $wpdb;
9909
9910 $context = [];
9911
9912 $event_selected = $data['filter']['amelia_events_list']['value'];
9913 $event_status = $data['filter']['event_booking_status']['value'];
9914
9915 $query = 'SELECT *
9916 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
9917 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
9918 ON customer.id = event_period.customerBookingId';
9919
9920 if ( -1 === $event_selected ) {
9921 if ( -1 !== $event_status ) {
9922 $query .= $wpdb->prepare(
9923 ' WHERE event_period.customerBookingId = (
9924 SELECT MAX(customerBookingId)
9925 FROM ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods
9926 ) AND customer.status = %s',
9927 $event_status
9928 );
9929 } else {
9930 $query .= ' WHERE event_period.customerBookingId = (
9931 SELECT MAX(customerBookingId)
9932 FROM ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods
9933 )';
9934 }
9935 } else {
9936 $event_periods_result = $wpdb->get_results(
9937 $wpdb->prepare(
9938 'SELECT id
9939 FROM ' . $wpdb->prefix . 'amelia_events_periods
9940 WHERE eventId = %d',
9941 [ $event_selected ]
9942 ),
9943 ARRAY_A
9944 );
9945 if ( ! empty( $event_periods_result ) ) {
9946 $ids = wp_list_pluck( $event_periods_result, 'id' );
9947 }
9948
9949 if ( ! empty( $ids ) ) {
9950 if ( -1 !== $event_status ) {
9951 $query .= $wpdb->prepare(
9952 ' WHERE event_period.eventPeriodId IN (%s) AND customer.status = %s',
9953 implode( ',', $ids ),
9954 $event_status
9955 );
9956 } else {
9957 $query .= $wpdb->prepare(
9958 ' WHERE event_period.eventPeriodId IN (%s)',
9959 implode( ',', $ids )
9960 );
9961 }
9962 } else {
9963 if ( -1 !== $event_status ) {
9964 $query .= $wpdb->prepare(
9965 ' WHERE event_period.eventPeriodId = %d AND customer.status = %s',
9966 $event_selected,
9967 $event_status
9968 );
9969 } else {
9970 $query .= $wpdb->prepare(
9971 ' WHERE event_period.eventPeriodId = %d',
9972 $event_selected
9973 );
9974 }
9975 }
9976 }
9977
9978 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
9979
9980 if ( ! empty( $result ) ) {
9981 $event = $wpdb->get_row(
9982 $wpdb->prepare(
9983 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
9984 [ $event_selected ]
9985 ),
9986 ARRAY_A
9987 );
9988 $event = isset( $event ) ? $event : [];
9989 $event_tags = $wpdb->get_results(
9990 $wpdb->prepare(
9991 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
9992 [ $event_selected ]
9993 ),
9994 ARRAY_A
9995 );
9996 $tags = [];
9997 if ( ! empty( $event_tags ) ) {
9998 foreach ( $event_tags as $key => $tag ) {
9999 $tags['event_tag'][ $key ] = $tag['name'];
10000 }
10001 } else {
10002 $tags = [];
10003 }
10004 $customer_result = $wpdb->get_row(
10005 $wpdb->prepare(
10006 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
10007 [ $result['customerId'] ]
10008 ),
10009 ARRAY_A
10010 );
10011 $customer_result = isset( $customer_result ) ? $customer_result : [];
10012 if ( $result['couponId'] ) {
10013 $coupon_result = $wpdb->get_row(
10014 $wpdb->prepare(
10015 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
10016 [ $result['couponId'] ]
10017 ),
10018 ARRAY_A
10019 );
10020 } else {
10021 $coupon_result = [];
10022 }
10023 if ( ! empty( $result['customFields'] ) ) {
10024 $custom_fields = json_decode( $result['customFields'], true );
10025
10026 $fields_arr = [];
10027 foreach ( (array) $custom_fields as $fields ) {
10028 if ( is_array( $fields ) ) {
10029 $fields_arr[ $fields['label'] ] = $fields['value'];
10030 }
10031 }
10032 unset( $result['customFields'] );
10033 } else {
10034 $fields_arr = [];
10035 }
10036 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
10037 if ( -1 === $event_status ) {
10038 $context['pluggable_data']['event_booking_status'] = 'approved';
10039 } else {
10040 $context['pluggable_data']['event_booking_status'] = $event_status;
10041 }
10042 $context['response_type'] = 'live';
10043 } else {
10044
10045 $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 );
10046 }
10047
10048 return $context;
10049 }
10050
10051 /**
10052 * Get last data for trigger.
10053 *
10054 * @param array $data data.
10055 * @return array
10056 */
10057 public function search_amelia_new_event_attendee_triggers_last_data( $data ) {
10058 global $wpdb;
10059
10060 $context = [];
10061
10062 $event_selected = $data['filter']['amelia_events_list']['value'];
10063
10064 $query = 'SELECT *
10065 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
10066 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
10067 ON customer.id = event_period.customerBookingId';
10068
10069 if ( -1 === $event_selected ) {
10070 $query .= '
10071 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods )';
10072 } else {
10073 $event_periods_result = $wpdb->get_results(
10074 $wpdb->prepare(
10075 'SELECT id
10076 FROM ' . $wpdb->prefix . 'amelia_events_periods
10077 WHERE eventId = %d',
10078 [ $event_selected ]
10079 ),
10080 ARRAY_A
10081 );
10082 if ( ! empty( $event_periods_result ) ) {
10083 $ids = wp_list_pluck( $event_periods_result, 'id' );
10084 }
10085 if ( ! empty( $ids ) ) {
10086 $query .= $wpdb->prepare(
10087 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND event_period.eventPeriodId IN (%s)',
10088 implode( ',', $ids )
10089 );
10090 } else {
10091 $query .= $wpdb->prepare(
10092 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND event_period.eventPeriodId = %d',
10093 [ $event_selected ]
10094 );
10095 }
10096 }
10097 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
10098
10099 if ( ! empty( $result ) ) {
10100
10101 $event = $wpdb->get_row(
10102 $wpdb->prepare(
10103 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
10104 [ $event_selected ]
10105 ),
10106 ARRAY_A
10107 );
10108 $event = isset( $event ) ? $event : [];
10109 $event_tags = $wpdb->get_results(
10110 $wpdb->prepare(
10111 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
10112 [ $event_selected ]
10113 ),
10114 ARRAY_A
10115 );
10116 $tags = [];
10117 if ( ! empty( $event_tags ) ) {
10118 foreach ( $event_tags as $key => $tag ) {
10119 $tags['event_tag'][ $key ] = $tag['name'];
10120 }
10121 } else {
10122 $tags = [];
10123 }
10124
10125 $customer_result = $wpdb->get_row(
10126 $wpdb->prepare(
10127 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
10128 [ $result['customerId'] ]
10129 ),
10130 ARRAY_A
10131 );
10132 $customer_result = isset( $customer_result ) ? $customer_result : [];
10133
10134 if ( $result['couponId'] ) {
10135 $coupon_result = $wpdb->get_row(
10136 $wpdb->prepare(
10137 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
10138 [ $result['couponId'] ]
10139 ),
10140 ARRAY_A
10141 );
10142 } else {
10143 $coupon_result = [];
10144 }
10145
10146 if ( ! empty( $result['customFields'] ) ) {
10147 $custom_fields = json_decode( $result['customFields'], true );
10148
10149 $fields_arr = [];
10150 foreach ( (array) $custom_fields as $fields ) {
10151 if ( is_array( $fields ) ) {
10152 $fields_arr[ $fields['label'] ] = $fields['value'];
10153 }
10154 }
10155 unset( $result['customFields'] );
10156 } else {
10157 $fields_arr = [];
10158 }
10159
10160 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
10161 $context['response_type'] = 'live';
10162 } else {
10163
10164 $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 );
10165 }
10166
10167 return $context;
10168 }
10169
10170 /**
10171 * Get last data for trigger.
10172 *
10173 * @param array $data data.
10174 * @return array
10175 */
10176 public function search_amelia_event_booking_cancelled_triggers_last_data( $data ) {
10177 global $wpdb;
10178
10179 $context = [];
10180
10181 $event_selected = $data['filter']['amelia_events_list']['value'];
10182
10183 if ( -1 === $event_selected ) {
10184 $result = $wpdb->get_row(
10185 'SELECT *
10186 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
10187 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
10188 ON customer.id = event_period.customerBookingId
10189 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND customer.status = "canceled"',
10190 ARRAY_A
10191 );
10192 } else {
10193 $event_periods_result = $wpdb->get_results(
10194 $wpdb->prepare(
10195 'SELECT id
10196 FROM ' . $wpdb->prefix . 'amelia_events_periods
10197 WHERE eventId = %d',
10198 [ $event_selected ]
10199 ),
10200 ARRAY_A
10201 );
10202 if ( ! empty( $event_periods_result ) ) {
10203 $ids = wp_list_pluck( $event_periods_result, 'id' );
10204 }
10205 $query = 'SELECT *
10206 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
10207 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
10208 ON customer.id = event_period.customerBookingId';
10209 if ( ! empty( $ids ) ) {
10210 $query .= $wpdb->prepare(
10211 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId IN (%s) AND customer.status = "canceled"',
10212 implode( ',', $ids )
10213 );
10214 } else {
10215 $query .= $wpdb->prepare(
10216 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId = %d
10217 AND customer.status = "canceled"',
10218 [ $event_selected ]
10219 );
10220 }
10221 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
10222 }
10223
10224 if ( ! empty( $result ) ) {
10225 $event = $wpdb->get_row(
10226 $wpdb->prepare(
10227 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
10228 [ $event_selected ]
10229 ),
10230 ARRAY_A
10231 );
10232 $event = isset( $event ) ? $event : [];
10233 $event_tags = $wpdb->get_results(
10234 $wpdb->prepare(
10235 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
10236 [ $event_selected ]
10237 ),
10238 ARRAY_A
10239 );
10240 $tags = [];
10241 if ( ! empty( $event_tags ) ) {
10242 foreach ( $event_tags as $key => $tag ) {
10243 $tags['event_tag'][ $key ] = $tag['name'];
10244 }
10245 } else {
10246 $tags = [];
10247 }
10248 $customer_result = $wpdb->get_row(
10249 $wpdb->prepare(
10250 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
10251 [ $result['customerId'] ]
10252 ),
10253 ARRAY_A
10254 );
10255 $customer_result = isset( $customer_result ) ? $customer_result : [];
10256 if ( $result['couponId'] ) {
10257 $coupon_result = $wpdb->get_row(
10258 $wpdb->prepare(
10259 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
10260 [ $result['couponId'] ]
10261 ),
10262 ARRAY_A
10263 );
10264 } else {
10265 $coupon_result = [];
10266 }
10267 if ( ! empty( $result['customFields'] ) ) {
10268 $custom_fields = json_decode( $result['customFields'], true );
10269
10270 $fields_arr = [];
10271 foreach ( (array) $custom_fields as $fields ) {
10272 if ( is_array( $fields ) ) {
10273 $fields_arr[ $fields['label'] ] = $fields['value'];
10274 }
10275 }
10276 unset( $result['customFields'] );
10277 } else {
10278 $fields_arr = [];
10279 }
10280 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
10281 $context['response_type'] = 'live';
10282 } else {
10283
10284 $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 );
10285 }
10286
10287 return $context;
10288 }
10289
10290 /**
10291 * Get last data for trigger.
10292 *
10293 * @param array $data data.
10294 * @return array
10295 */
10296 public function search_amelia_appointment_rescheduled_triggers_last_data( $data ) {
10297 global $wpdb;
10298
10299 $context = [];
10300
10301 $appointment_category = $data['filter']['amelia_category_list']['value'];
10302 $appointment_service = $data['filter']['amelia_service_list']['value'];
10303
10304 if ( -1 === $appointment_service ) {
10305 // If service exists as per category selected.
10306 $service_exist = $wpdb->get_row(
10307 $wpdb->prepare(
10308 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
10309 [ $appointment_category ]
10310 ),
10311 ARRAY_A
10312 );
10313
10314 if ( empty( $service_exist ) ) {
10315 $result = [];
10316 } else {
10317 $result = $wpdb->get_row(
10318 '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 )',
10319 ARRAY_A
10320 );
10321 }
10322 } else {
10323 $result = $wpdb->get_row(
10324 $wpdb->prepare(
10325 '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',
10326 [ $appointment_service ]
10327 ),
10328 ARRAY_A
10329 );
10330 }
10331
10332 if ( ! empty( $result ) ) {
10333
10334 $payment_result = $wpdb->get_row(
10335 $wpdb->prepare(
10336 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
10337 [ $result['id'] ]
10338 ),
10339 ARRAY_A
10340 );
10341
10342 $customer_result = $wpdb->get_row(
10343 $wpdb->prepare(
10344 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
10345 [ $result['customerId'] ]
10346 ),
10347 ARRAY_A
10348 );
10349
10350 $service_result = $wpdb->get_row(
10351 $wpdb->prepare(
10352 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
10353 [ $result['serviceId'] ]
10354 ),
10355 ARRAY_A
10356 );
10357
10358 $category_result = $wpdb->get_row(
10359 $wpdb->prepare(
10360 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
10361 [ $service_result['categoryId'] ]
10362 ),
10363 ARRAY_A
10364 );
10365
10366 if ( $result['couponId'] ) {
10367 $coupon_result = $wpdb->get_row(
10368 $wpdb->prepare(
10369 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
10370 [ $result['couponId'] ]
10371 ),
10372 ARRAY_A
10373 );
10374 } else {
10375 $coupon_result = [];
10376 }
10377
10378 if ( ! empty( $result['customFields'] ) ) {
10379 $custom_fields = json_decode( $result['customFields'], true );
10380
10381 $fields_arr = [];
10382 foreach ( (array) $custom_fields as $fields ) {
10383 if ( is_array( $fields ) ) {
10384 $fields_arr[ $fields['label'] ] = $fields['value'];
10385 }
10386 }
10387 unset( $result['customFields'] );
10388 } else {
10389 $fields_arr = [];
10390 }
10391
10392 $appointment_data['isRescheduled'] = '1';
10393 $context['pluggable_data'] = array_merge( $result, $fields_arr, $appointment_data, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
10394 $context['response_type'] = 'live';
10395 } else {
10396
10397 $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 );
10398 }
10399
10400 return $context;
10401 }
10402
10403
10404 /**
10405 * Get last data for trigger.
10406 *
10407 * @param array $data data.
10408 * @return array
10409 */
10410 public function search_amelia_appointment_cancelled_triggers_last_data( $data ) {
10411 global $wpdb;
10412
10413 $context = [];
10414
10415 $appointment_category = $data['filter']['amelia_category_list']['value'];
10416 $appointment_service = $data['filter']['amelia_service_list']['value'];
10417
10418 if ( -1 === $appointment_service ) {
10419 // If service exists as per category selected.
10420 $service_exist = $wpdb->get_row(
10421 $wpdb->prepare(
10422 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
10423 [ $appointment_category ]
10424 ),
10425 ARRAY_A
10426 );
10427
10428 if ( empty( $service_exist ) ) {
10429 $result = [];
10430 } else {
10431 $result = $wpdb->get_row(
10432 $wpdb->prepare(
10433 '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',
10434 [ 'canceled' ]
10435 ),
10436 ARRAY_A
10437 );
10438 }
10439 } else {
10440 $result = $wpdb->get_row(
10441 $wpdb->prepare(
10442 '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',
10443 [ 'canceled', $appointment_service ]
10444 ),
10445 ARRAY_A
10446 );
10447 }
10448
10449 if ( ! empty( $result ) ) {
10450
10451 $payment_result = $wpdb->get_row(
10452 $wpdb->prepare(
10453 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
10454 [ $result['id'] ]
10455 ),
10456 ARRAY_A
10457 );
10458
10459 $customer_result = $wpdb->get_row(
10460 $wpdb->prepare(
10461 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
10462 [ $result['customerId'] ]
10463 ),
10464 ARRAY_A
10465 );
10466
10467 $service_result = $wpdb->get_row(
10468 $wpdb->prepare(
10469 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
10470 [ $result['serviceId'] ]
10471 ),
10472 ARRAY_A
10473 );
10474
10475 $category_result = $wpdb->get_row(
10476 $wpdb->prepare(
10477 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
10478 [ $service_result['categoryId'] ]
10479 ),
10480 ARRAY_A
10481 );
10482
10483 if ( $result['couponId'] ) {
10484 $coupon_result = $wpdb->get_row(
10485 $wpdb->prepare(
10486 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
10487 [ $result['couponId'] ]
10488 ),
10489 ARRAY_A
10490 );
10491 } else {
10492 $coupon_result = [];
10493 }
10494
10495 if ( ! empty( $result['customFields'] ) ) {
10496 $custom_fields = json_decode( $result['customFields'], true );
10497
10498 $fields_arr = [];
10499 foreach ( (array) $custom_fields as $fields ) {
10500 if ( is_array( $fields ) ) {
10501 $fields_arr[ $fields['label'] ] = $fields['value'];
10502 }
10503 }
10504 unset( $result['customFields'] );
10505 } else {
10506 $fields_arr = [];
10507 }
10508
10509 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
10510 $context['response_type'] = 'live';
10511 } else {
10512
10513 $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 );
10514 }
10515
10516 return $context;
10517 }
10518
10519 /**
10520 * Get MailPoet Forms.
10521 *
10522 * @param array $data data.
10523 *
10524 * @return array
10525 */
10526 public function search_mailpoet_forms( $data ) {
10527 if ( ! class_exists( '\MailPoet\API\API' ) ) {
10528 return [];
10529 }
10530
10531 global $wpdb;
10532
10533 $page = $data['page'];
10534 $limit = Utilities::get_search_page_limit();
10535 $offset = $limit * ( $page - 1 );
10536
10537 $forms = $wpdb->get_results(
10538 $wpdb->prepare(
10539 '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',
10540 [ 'enabled', $limit, $offset ]
10541 )
10542 );
10543
10544 $form_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
10545
10546 $options = [];
10547
10548 if ( ! empty( $forms ) ) {
10549 if ( is_array( $forms ) ) {
10550 foreach ( $forms as $form ) {
10551 $options[] = [
10552 'label' => $form->name,
10553 'value' => $form->id,
10554 ];
10555 }
10556 }
10557 }
10558
10559 return [
10560 'options' => $options,
10561 'hasMore' => $form_count > $limit && $form_count > $offset,
10562 ];
10563
10564 }
10565
10566 /**
10567 * Get MailPoet List.
10568 *
10569 * @param array $data data.
10570 *
10571 * @return array
10572 */
10573 public function search_mailpoet_list( $data ) {
10574 if ( ! class_exists( '\MailPoet\API\API' ) ) {
10575 return [];
10576 }
10577
10578 $mailpoet = \MailPoet\API\API::MP( 'v1' );
10579 $lists = $mailpoet->getLists();
10580
10581 $options = [];
10582
10583 if ( ! empty( $lists ) ) {
10584 if ( is_array( $lists ) ) {
10585 foreach ( $lists as $list ) {
10586 $options[] = [
10587 'label' => $list['name'],
10588 'value' => $list['id'],
10589 ];
10590 }
10591 }
10592 }
10593
10594 return [
10595 'options' => $options,
10596 'hasMore' => false,
10597 ];
10598 }
10599
10600 /**
10601 * Get MailPoet Subscriber Status.
10602 *
10603 * @param array $data data.
10604 *
10605 * @return array
10606 */
10607 public function search_mailpoet_subscriber_status( $data ) {
10608 if ( ! class_exists( '\MailPoet\API\API' ) ) {
10609 return [];
10610 }
10611
10612 $subscriber_status = [
10613 'subscribed' => 'Subscribed',
10614 'unconfirmed' => 'Unconfirmed',
10615 'unsubscribed' => 'Unsubscribed',
10616 'inactive' => 'Inactive',
10617 'bounced' => 'Bounced',
10618 ];
10619
10620 $options = [];
10621 foreach ( $subscriber_status as $key => $status ) {
10622 $options[] = [
10623 'label' => $status,
10624 'value' => $key,
10625 ];
10626 }
10627
10628 return [
10629 'options' => $options,
10630 'hasMore' => false,
10631 ];
10632 }
10633
10634 /**
10635 * Get MailPoet Subscribers.
10636 *
10637 * @param array $data data.
10638 *
10639 * @return array
10640 */
10641 public function search_mailpoet_subscribers( $data ) {
10642 if ( ! class_exists( '\MailPoet\API\API' ) ) {
10643 return [];
10644 }
10645
10646 global $wpdb;
10647
10648 $page = $data['page'];
10649 $limit = Utilities::get_search_page_limit();
10650 $offset = $limit * ( $page - 1 );
10651
10652 $subscribers = $wpdb->get_results(
10653 $wpdb->prepare(
10654 'SELECT SQL_CALC_FOUND_ROWS id,email FROM ' . $wpdb->prefix . 'mailpoet_subscribers ORDER BY id DESC LIMIT %d OFFSET %d',
10655 [ $limit, $offset ]
10656 )
10657 );
10658
10659 $subscribers_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
10660
10661 $options = [];
10662
10663 if ( ! empty( $subscribers ) ) {
10664 if ( is_array( $subscribers ) ) {
10665 foreach ( $subscribers as $subscriber ) {
10666 $options[] = [
10667 'label' => $subscriber->email,
10668 'value' => $subscriber->id,
10669 ];
10670 }
10671 }
10672 }
10673
10674 return [
10675 'options' => $options,
10676 'hasMore' => $subscribers_count > $limit && $subscribers_count > $offset,
10677 ];
10678 }
10679
10680 /**
10681 * Get ConvertPro Forms.
10682 *
10683 * @param array $data data.
10684 *
10685 * @return array
10686 */
10687 public function search_convertpro_form_list( $data ) {
10688 if ( ! class_exists( '\Cp_V2_Loader' ) || ! class_exists( 'CP_V2_Popups' ) ) {
10689 return [];
10690 }
10691
10692 $cp_popups_inst = CP_V2_Popups::get_instance();
10693 $popups = $cp_popups_inst->get_all();
10694
10695 $form_count = count( $popups );
10696
10697 $page = $data['page'];
10698 $limit = Utilities::get_search_page_limit();
10699 $offset = $limit * ( $page - 1 );
10700
10701 $options = [];
10702
10703 if ( ! empty( $popups ) ) {
10704 if ( is_array( $popups ) ) {
10705 foreach ( $popups as $form ) {
10706 $options[] = [
10707 'label' => $form->post_title,
10708 'value' => $form->ID,
10709 ];
10710 }
10711 }
10712 }
10713
10714 return [
10715 'options' => $options,
10716 'hasMore' => $form_count > $limit && $form_count > $offset,
10717 ];
10718
10719 }
10720
10721 /**
10722 * Get ProjectHuddle Websites.
10723 *
10724 * @param array $data data.
10725 *
10726 * @return array
10727 */
10728 public function search_project_huddle_websites( $data ) {
10729
10730 $sites = new WP_Query(
10731 [
10732 'post_type' => 'ph-website',
10733 'posts_per_page' => - 1,
10734 'fields' => 'ids',
10735 ]
10736 );
10737
10738 $site_ids = (array) $sites->posts;
10739
10740 $options = [];
10741 if ( ! empty( $site_ids ) ) {
10742 if ( is_array( $site_ids ) ) {
10743 foreach ( $site_ids as $site_id ) {
10744 $options[] = [
10745 'label' => get_the_title( $site_id ),
10746 'value' => $site_id,
10747 ];
10748 }
10749 }
10750 }
10751 return [
10752 'options' => $options,
10753 'hasMore' => false,
10754 ];
10755
10756 }
10757
10758 /**
10759 * Get last data for trigger.
10760 *
10761 * @param array $data data.
10762 * @return mixed
10763 */
10764 public function search_project_huddle_comment_triggers_last_data( $data ) {
10765 global $wpdb;
10766
10767 $context = [];
10768 if ( ! class_exists( 'PH\Models\Post' ) || ! function_exists( 'ph_get_the_title' ) ) {
10769 return [];
10770 }
10771
10772 if ( -1 !== $data['dynamic'] ) {
10773 $threads = get_posts(
10774 [
10775 'post_type' => 'phw_comment_loc',
10776 'posts_per_page' => 1,
10777 'meta_value' => $data['dynamic'],
10778 'meta_key' => 'project_id',
10779 'orderby' => 'asc',
10780 ]
10781 );
10782 } else {
10783 $threads = [];
10784 }
10785
10786 if ( ! empty( $threads ) ) {
10787 $comment_result = $wpdb->get_row(
10788 $wpdb->prepare(
10789 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
10790 FROM ' . $wpdb->prefix . 'comments
10791 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
10792 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
10793 LIMIT 0,1',
10794 $threads[0]->ID
10795 ),
10796 ARRAY_A
10797 );
10798
10799 if ( ! empty( $comment_result ) ) {
10800 $comment_id = get_comment( $comment_result['comment_ID'], ARRAY_A );
10801 $comments['comment_ID'] = isset( $comment_id['comment_ID'] ) ? $comment_id['comment_ID'] : '';
10802 $comments['comment_post_ID'] = isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : '';
10803
10804 $comments['comment_author'] = isset( $comment_id['comment_author'] ) ? $comment_id['comment_author'] : '';
10805
10806 $comments['comment_author_email'] = isset( $comment_id['comment_author_email'] ) ? $comment_id['comment_author_email'] : '';
10807
10808 $comments['comment_date'] = isset( $comment_id['comment_date'] ) ? $comment_id['comment_date'] : '';
10809
10810 $comments['comment_content'] = isset( $comment_id['comment_content'] ) ? $comment_id['comment_content'] : '';
10811
10812 $comments['comment_type'] = isset( $comment_id['comment_type'] ) ? $comment_id['comment_type'] : '';
10813
10814 $comment_item_id = get_comment_meta( $comment_result['comment_ID'], 'item_id' );
10815 if ( ! empty( $comment_item_id ) && is_array( $comment_item_id ) ) {
10816 $comments['comment_item_id'] = $comment_item_id[0];
10817 $comments['comment_item_page_title'] = get_the_title( (int) $comment_item_id[0] );
10818 $comments['comment_item_page_url'] = get_post_meta( (int) $comment_item_id[0], 'page_url', true );
10819 }
10820 if ( isset( $comment_id['comment_post_ID'] ) || isset( $comment_id['comment_author'] ) ) {
10821 $comments['ph_project_name'] = ph_get_the_title( Post::get( $comment_id['comment_post_ID'] )->parentsIds()['project'] );
10822 $comments['ph_commenter_name'] = $comment_id['comment_author'];
10823 $comments['ph_project_type'] = ( get_post_type( $comment_id['comment_post_ID'] ) == 'ph-website' ) ? __( 'Website', 'suretriggers' ) : __( 'Mockup', 'suretriggers' );
10824 $comments['ph_action_status'] = get_post_meta( $comment_id['comment_post_ID'], 'resolved', true ) ? __( 'Resolved', 'suretriggers' ) : __( 'Unresolved', 'suretriggers' );
10825 $comments['ph_project_link'] = get_the_guid( $comment_id['comment_post_ID'] );
10826 }
10827
10828 $context['pluggable_data'] = $comments;
10829 $context['response_type'] = 'live';
10830 } else {
10831 $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/about-us","ph_project_name":"Test","ph_commenter_name":"Admin","ph_project_type":"Mockup","ph_action_status":"Unresolved","ph_project_link":"https://example.com"}}', true );
10832 }
10833 } else {
10834 $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/about-us","ph_project_name":"Test","ph_commenter_name":"Admin","ph_project_type":"Mockup","ph_action_status":"Unresolved","ph_project_link":"https://example.com"}}', true );
10835 }
10836
10837 return $context;
10838 }
10839
10840 /**
10841 * Get last data for trigger.
10842 *
10843 * @param array $data data.
10844 * @return mixed
10845 */
10846 public function search_project_huddle_resolved_comment_triggers_last_data( $data ) {
10847 global $wpdb;
10848
10849 $context = [];
10850 if ( ! class_exists( 'PH\Models\Post' ) || ! function_exists( 'ph_get_the_title' ) ) {
10851 return [];
10852 }
10853
10854 $get_comments = $wpdb->get_row(
10855 'SELECT ' . $wpdb->prefix . 'comments.comment_ID, ' . $wpdb->prefix . 'comments.comment_content
10856 FROM ' . $wpdb->prefix . 'comments
10857 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_status") AND comment_content = "Resolved"
10858 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
10859 LIMIT 0,1'
10860 );
10861
10862 if ( ! empty( $get_comments ) ) {
10863 $comment_id = get_comment( $get_comments->comment_ID, ARRAY_A );
10864 $comment_result = $wpdb->get_row(
10865 $wpdb->prepare(
10866 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
10867 FROM ' . $wpdb->prefix . 'comments
10868 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
10869 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
10870 LIMIT 0,1',
10871 isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : ''
10872 ),
10873 ARRAY_A
10874 );
10875
10876 $actual_comment = get_comment( $comment_result['comment_ID'], ARRAY_A );
10877 $comments['comment_ID'] = isset( $actual_comment['comment_ID'] ) ? $actual_comment['comment_ID'] : '';
10878 $comments['comment_post_ID'] = isset( $actual_comment['comment_post_ID'] ) ? $actual_comment['comment_post_ID'] : '';
10879 $comments['comment_author'] = isset( $actual_comment['comment_author'] ) ? $actual_comment['comment_author'] : '';
10880 $comments['comment_author_email'] = isset( $actual_comment['comment_author_email'] ) ? $actual_comment['comment_author_email'] : '';
10881 $comments['comment_date'] = isset( $actual_comment['comment_date'] ) ? $actual_comment['comment_date'] : '';
10882 $comments['comment_content'] = isset( $actual_comment['comment_content'] ) ? $actual_comment['comment_content'] : '';
10883 $comments['comment_type'] = isset( $actual_comment['comment_type'] ) ? $actual_comment['comment_type'] : '';
10884 $comments['comment_status'] = $get_comments->comment_content;
10885 $comment_item_id = get_comment_meta( $comment_result['comment_ID'], 'item_id' );
10886 if ( ! empty( $comment_item_id ) && is_array( $comment_item_id ) ) {
10887 $comments['comment_item_id'] = $comment_item_id[0];
10888 $comments['comment_item_page_title'] = get_the_title( (int) $comment_item_id[0] );
10889 $comments['comment_item_page_url'] = get_post_meta( (int) $comment_item_id[0], 'page_url', true );
10890 }
10891 if ( isset( $comment_id['comment_post_ID'] ) || isset( $comment_id['comment_author'] ) ) {
10892 $comments['ph_project_name'] = ph_get_the_title( Post::get( $comment_id['comment_post_ID'] )->parentsIds()['project'] );
10893 $comments['ph_commenter_name'] = $comment_id['comment_author'];
10894 $comments['ph_project_type'] = ( get_post_type( $comment_id['comment_post_ID'] ) == 'ph-website' ) ? __( 'Website', 'suretriggers' ) : __( 'Mockup', 'suretriggers' );
10895 $comments['ph_action_status'] = get_post_meta( $comment_id['comment_post_ID'], 'resolved', true ) ? __( 'Resolved', 'suretriggers' ) : __( 'Unresolved', 'suretriggers' );
10896 $comments['ph_project_link'] = get_the_guid( $comment_id['comment_post_ID'] );
10897 }
10898 $context['pluggable_data'] = $comments;
10899 $context['response_type'] = 'live';
10900 } else {
10901 $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","ph_project_name":"Test","ph_commenter_name":"Admin","ph_project_type":"Mockup","ph_action_status":"Unresolved","ph_project_link":"https://example.com" }}', true );
10902 }
10903
10904 return $context;
10905 }
10906
10907 /**
10908 * Get MasterStudy LMS Courses.
10909 *
10910 * @param array $data data.
10911 *
10912 * @return array
10913 */
10914 public function search_ms_lms_courses( $data ) {
10915
10916 $page = $data['page'];
10917 $limit = Utilities::get_search_page_limit();
10918 $offset = $limit * ( $page - 1 );
10919
10920 $args = [
10921 'post_type' => 'stm-courses',
10922 'posts_per_page' => $limit,
10923 'offset' => $offset,
10924 'orderby' => 'title',
10925 'order' => 'ASC',
10926 'post_status' => 'publish',
10927 ];
10928
10929 $courses = get_posts( $args );
10930
10931 $course_count = wp_count_posts( 'stm-courses' )->publish;
10932
10933 $options = [];
10934 if ( ! empty( $courses ) ) {
10935 if ( is_array( $courses ) ) {
10936 foreach ( $courses as $course ) {
10937 $options[] = [
10938 'label' => $course->post_title,
10939 'value' => $course->ID,
10940 ];
10941 }
10942 }
10943 }
10944 return [
10945 'options' => $options,
10946 'hasMore' => $course_count > $limit && $course_count > $offset,
10947 ];
10948
10949 }
10950
10951 /**
10952 * Get MasterStudy LMS Lessons.
10953 *
10954 * @param array $data data.
10955 *
10956 * @return array
10957 */
10958 public function search_ms_lms_lessons( $data ) {
10959
10960 global $wpdb;
10961 $options = [];
10962 $course_id = $data['dynamic'];
10963
10964 // Use curriculum repository class.
10965 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
10966 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
10967 } else {
10968 $curriculum_repo = false;
10969 }
10970
10971 if ( '-1' === $course_id ) {
10972 $lessons = $wpdb->get_results(
10973 $wpdb->prepare(
10974 "SELECT SQL_CALC_FOUND_ROWS ID, post_title FROM $wpdb->posts WHERE post_type = %s ORDER BY post_title ASC",
10975 'stm-lessons'
10976 )
10977 );
10978
10979 if ( ! empty( $lessons ) ) {
10980 if ( is_array( $lessons ) ) {
10981 foreach ( $lessons as $lesson ) {
10982 $options[] = [
10983 'label' => $lesson->post_title,
10984 'value' => $lesson->ID,
10985 ];
10986 }
10987 }
10988 }
10989 } else {
10990 $lessons = [];
10991 if ( $curriculum_repo ) {
10992 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
10993 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
10994 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
10995 foreach ( $curriculum['materials'] as $material ) {
10996 if ( 'stm-lessons' === $material['post_type'] ) {
10997 $lessons[] = [
10998 'value' => $material['post_id'],
10999 'text' => $material['title'],
11000 ];
11001 }
11002 }
11003 }
11004 }
11005 if ( ! empty( $lessons ) ) {
11006 if ( is_array( $lessons ) ) {
11007 foreach ( $lessons as $lesson ) {
11008 $options[] = [
11009 'label' => $lesson['text'],
11010 'value' => $lesson['value'],
11011 ];
11012 }
11013 }
11014 }
11015 } else {
11016 $lessons = $wpdb->get_results(
11017 $wpdb->prepare(
11018 "SELECT SQL_CALC_FOUND_ROWS ID, post_title
11019 FROM $wpdb->posts
11020 WHERE FIND_IN_SET(
11021 ID,
11022 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
11023 )
11024 AND post_type = 'stm-lessons'
11025 ORDER BY post_title ASC",
11026 absint( $course_id )
11027 )
11028 );
11029 if ( ! empty( $lessons ) ) {
11030 if ( is_array( $lessons ) ) {
11031 foreach ( $lessons as $lesson ) {
11032 $options[] = [
11033 'label' => $lesson->post_title,
11034 'value' => $lesson->ID,
11035 ];
11036 }
11037 }
11038 }
11039 }
11040 }
11041 return [
11042 'options' => $options,
11043 'hasMore' => false,
11044 ];
11045
11046 }
11047
11048 /**
11049 * Get MasterStudy LMS Quiz.
11050 *
11051 * @param array $data data.
11052 *
11053 * @return array
11054 */
11055 public function search_ms_lms_quiz( $data ) {
11056
11057 global $wpdb;
11058
11059 $options = [];
11060 $course_id = $data['dynamic'];
11061
11062 // Use curriculum repository class.
11063 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
11064 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
11065 } else {
11066 $curriculum_repo = false;
11067 }
11068
11069 if ( '-1' === $course_id ) {
11070 $quizzes = $wpdb->get_results(
11071 $wpdb->prepare(
11072 "SELECT ID, post_title
11073 FROM $wpdb->posts
11074 WHERE post_type = %s
11075 ORDER BY post_title ASC",
11076 'stm-quizzes'
11077 )
11078 );
11079 if ( ! empty( $quizzes ) ) {
11080 if ( is_array( $quizzes ) ) {
11081 foreach ( $quizzes as $quiz ) {
11082 $options[] = [
11083 'label' => $quiz->post_title,
11084 'value' => $quiz->ID,
11085 ];
11086 }
11087 }
11088 }
11089 } else {
11090 $quizzes = [];
11091 if ( $curriculum_repo ) {
11092 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
11093 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
11094 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
11095 foreach ( $curriculum['materials'] as $material ) {
11096 if ( 'stm-quizzes' === $material['post_type'] ) {
11097 $quizzes[] = [
11098 'value' => $material['post_id'],
11099 'text' => $material['title'],
11100 ];
11101 }
11102 }
11103 }
11104 }
11105 if ( ! empty( $quizzes ) ) {
11106 if ( is_array( $quizzes ) ) {
11107 foreach ( $quizzes as $quiz ) {
11108 $options[] = [
11109 'label' => $quiz['text'],
11110 'value' => $quiz['value'],
11111 ];
11112 }
11113 }
11114 }
11115 } else {
11116 $quizzes = $wpdb->get_results(
11117 $wpdb->prepare(
11118 "SELECT ID, post_title
11119 FROM $wpdb->posts
11120 WHERE FIND_IN_SET(
11121 ID,
11122 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
11123 )
11124 AND post_type = 'stm-quizzes'
11125 ORDER BY post_title ASC
11126 ",
11127 absint( $course_id )
11128 )
11129 );
11130 if ( ! empty( $quizzes ) ) {
11131 if ( is_array( $quizzes ) ) {
11132 foreach ( $quizzes as $quiz ) {
11133 $options[] = [
11134 'label' => $quiz->post_title,
11135 'value' => $quiz->ID,
11136 ];
11137 }
11138 }
11139 }
11140 }
11141 }
11142 return [
11143 'options' => $options,
11144 'hasMore' => false,
11145 ];
11146
11147 }
11148
11149 /**
11150 * Search MasterStudy LMS data.
11151 *
11152 * @param array $data data.
11153 * @return array|void
11154 */
11155 public function search_ms_lms_last_data( $data ) {
11156 global $wpdb;
11157 $post_type = $data['post_type'];
11158 $trigger = $data['search_term'];
11159 $context = [];
11160
11161 if ( 'stm_lms_course_completed' === $trigger ) {
11162 $post_id = $data['filter']['course']['value'];
11163 if ( -1 === $post_id ) {
11164 $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 ) );
11165 } else {
11166 $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 ) );
11167 }
11168 } elseif ( 'stm_lesson_passed' === $trigger ) {
11169 $post_id = $data['filter']['lesson']['value'];
11170 if ( -1 === $post_id ) {
11171 $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 ) );
11172 } else {
11173 $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 ) );
11174 }
11175 } elseif ( 'stm_quiz_passed' === $trigger ) {
11176 $post_id = $data['filter']['quiz']['value'];
11177 if ( -1 === $post_id ) {
11178 $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 ) );
11179 } else {
11180 $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 ) );
11181 }
11182 } elseif ( 'stm_quiz_failed' === $trigger ) {
11183 $post_id = $data['filter']['quiz']['value'];
11184 if ( -1 === $post_id ) {
11185 $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 ) );
11186 } else {
11187 $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 ) );
11188 }
11189 } elseif ( 'stm_lms_user_enroll_course' === $trigger ) {
11190 $post_id = $data['filter']['course']['value'];
11191 if ( -1 === $post_id ) {
11192 $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 ) );
11193 } else {
11194 $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 ) );
11195 }
11196 }
11197
11198 if ( ! empty( $result ) ) {
11199
11200 switch ( $trigger ) {
11201 case 'stm_lms_course_completed':
11202 $result_course_id = $result[0]->course_id;
11203 $result_user_id = $result[0]->user_id;
11204 $course = get_the_title( $result_course_id );
11205 $course_link = get_the_permalink( $result_course_id );
11206 $featured_image = get_the_post_thumbnail_url( $result_course_id );
11207
11208 $data = [
11209 'course_id' => $result_course_id,
11210 'course_title' => $course,
11211 'course_link' => $course_link,
11212 'course_featured_image' => $featured_image,
11213 'course_progress' => $result[0]->progress_percent,
11214 ];
11215 $context_data = array_merge(
11216 WordPress::get_user_context( $result_user_id ),
11217 $data
11218 );
11219 break;
11220 case 'stm_lesson_passed':
11221 $result_lesson_id = $result[0]->lesson_id;
11222 $result_user_id = $result[0]->user_id;
11223 $lesson = get_the_title( $result_lesson_id );
11224 $lesson_link = get_the_permalink( $result_lesson_id );
11225
11226 $data = [
11227 'lesson_id' => $result_lesson_id,
11228 'lesson_title' => $lesson,
11229 'lesson_link' => $lesson_link,
11230 ];
11231 $context_data = array_merge(
11232 WordPress::get_user_context( $result_user_id ),
11233 $data
11234 );
11235 break;
11236 case 'stm_quiz_passed':
11237 $result_quiz_id = $result[0]->quiz_id;
11238 $result_user_id = $result[0]->user_id;
11239 $quiz_title = get_the_title( $result_quiz_id );
11240 $quiz_link = get_the_permalink( $result_quiz_id );
11241
11242 $data = [
11243 'quiz_id' => $result_quiz_id,
11244 'quiz_title' => $quiz_title,
11245 'quiz_link' => $quiz_link,
11246 'quiz_score' => $result[0]->progress,
11247 'result' => 'passed',
11248 ];
11249 $context_data = array_merge(
11250 WordPress::get_user_context( $result_user_id ),
11251 $data
11252 );
11253 break;
11254 case 'stm_quiz_failed':
11255 $result_quiz_id = $result[0]->quiz_id;
11256 $result_user_id = $result[0]->user_id;
11257 $quiz_title = get_the_title( $result_quiz_id );
11258 $quiz_link = get_the_permalink( $result_quiz_id );
11259
11260 $data = [
11261 'quiz_id' => $result_quiz_id,
11262 'quiz_title' => $quiz_title,
11263 'quiz_link' => $quiz_link,
11264 'quiz_score' => $result[0]->progress,
11265 'result' => 'failed',
11266 ];
11267 $context_data = array_merge(
11268 WordPress::get_user_context( $result_user_id ),
11269 $data
11270 );
11271 break;
11272 case 'stm_lms_user_enroll_course':
11273 $result_course_id = $result[0]->course_id;
11274 $result_user_id = $result[0]->user_id;
11275
11276 $course = get_the_title( $result_course_id );
11277 $course_link = get_the_permalink( $result_course_id );
11278 $featured_image = get_the_post_thumbnail_url( $result_course_id );
11279
11280 $data = [
11281 'course_id' => $result_course_id,
11282 'course_title' => $course,
11283 'course_link' => $course_link,
11284 'course_featured_image' => $featured_image,
11285 ];
11286 $context_data = array_merge(
11287 WordPress::get_user_context( $result_user_id ),
11288 $data
11289 );
11290 break;
11291 default:
11292 return;
11293 }
11294 $context['pluggable_data'] = $context_data;
11295 $context['response_type'] = 'live';
11296 }
11297
11298 return $context;
11299
11300 }
11301
11302 /**
11303 * Prepare Fluent Support Mailbox list.
11304 *
11305 * @param array $data data.
11306 *
11307 * @return array
11308 */
11309 public function search_fs_mailbox_list( $data ) {
11310 $options = [];
11311 global $wpdb;
11312 $mailboxes = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_mail_boxes ORDER BY id DESC", ARRAY_A );
11313 if ( ! empty( $mailboxes ) ) {
11314 foreach ( $mailboxes as $key => $value ) {
11315
11316 $options[] = [
11317 'label' => $value['name'],
11318 'value' => $value['id'],
11319 ];
11320
11321 }
11322 }
11323
11324 return [
11325 'options' => $options,
11326 'hasMore' => false,
11327 ];
11328 }
11329
11330 /**
11331 * Prepare Fluent Support Product list.
11332 *
11333 * @param array $data data.
11334 *
11335 * @return array
11336 */
11337 public function search_fs_product_list( $data ) {
11338 $options = [];
11339
11340 if ( ! class_exists( '\FluentSupport\App\Models\Product' ) ) {
11341 return [];
11342 }
11343
11344 $products = \FluentSupport\App\Models\Product::select( [ 'id', 'title' ] )->get();
11345 if ( ! empty( $products ) ) {
11346 foreach ( $products as $key => $product ) {
11347 $options[] = [
11348 'label' => $product['title'],
11349 'value' => $product['id'],
11350 ];
11351 }
11352 }
11353
11354 return [
11355 'options' => $options,
11356 'hasMore' => false,
11357 ];
11358 }
11359
11360 /**
11361 * Prepare Fluent Support Product list.
11362 *
11363 * @param array $data data.
11364 *
11365 * @return array
11366 */
11367 public function search_fs_person_type_list( $data ) {
11368 $options = [
11369 [
11370 'label' => 'Customer',
11371 'value' => 'customer',
11372 ],
11373 [
11374 'label' => 'Agent',
11375 'value' => 'agent',
11376 ],
11377 ];
11378
11379 return [
11380 'options' => $options,
11381 'hasMore' => false,
11382 ];
11383 }
11384
11385 /**
11386 * Prepare FluentSupport Agents List.
11387 *
11388 * @param array $data Search Params.
11389 * @return array
11390 */
11391 public function search_fluent_support_agents_list( $data ) {
11392 global $wpdb;
11393
11394 $agents = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent' ", ARRAY_A );
11395
11396 $options = [];
11397
11398 if ( ! empty( $agents ) ) {
11399 foreach ( $agents as $agent ) {
11400 $options[] = [
11401 'label' => $agent['email'],
11402 'value' => $agent['id'],
11403 ];
11404 }
11405 }
11406
11407 return [
11408 'options' => $options,
11409 'hasMore' => false,
11410 ];
11411 }
11412
11413 /**
11414 * Get last data for trigger.
11415 *
11416 * @param array $data data.
11417 * @return array
11418 */
11419 public function search_fluent_support_triggers_last_data( $data ) {
11420 global $wpdb;
11421 $context = [];
11422 $context['response_type'] = 'sample';
11423
11424 if ( ! class_exists( '\FluentSupport\App\Models\Ticket' ) ) {
11425 return [];
11426 }
11427
11428 $ticket_data = [
11429 'id' => '1',
11430 'customer_id' => '2',
11431 'agent_id' => '3',
11432 'product_id' => '5',
11433 'product_source' => 'local',
11434 'privacy' => 'private',
11435 'priority' => 'normal',
11436 'client_priority' => 'medium',
11437 'status' => 'active',
11438 'title' => 'Sample Ticket Title',
11439 'slug' => 'sample-ticket-title',
11440 'hash' => 'f8a8cfb946',
11441 'content_hash' => 'd65500d62621be8b493c22b1d888052c',
11442 'content' => '<p>Sample content.</p>',
11443 'last_customer_response' => '2023-04-27 07:30:46',
11444 'waiting_since' => '2023-04-27 07:30:46',
11445 'response_count' => '2',
11446 'total_close_time' => '7042',
11447 'resolved at' => '2023-04-27 09:28:08',
11448 'closed_by' => '1',
11449 'created_at' => '2023-04-27 07:30:46',
11450 'updated_at' => '2023-04-27 10:28:08',
11451 'mailbox_id' => '1',
11452 'mailbox_name' => 'OttoKit',
11453 'mailbox_slug' => 'suretriggers',
11454 'mailbox_box_type' => 'web',
11455 'mailbox email' => 'john_doe@sample.com',
11456 'mailbox_settings_admin_email_address' => 'john_doe@sample.com',
11457 'mailbox_created_by' => '1',
11458 'mailbox_is_default' => 'yes',
11459 'mailbox_created_at' => '2023-04-26 06:29:01',
11460 'mailbox_updated_at' => '2023-04-26 06:29:01',
11461 ];
11462
11463 $customer_data = [
11464 'id' => '1',
11465 'first_name' => 'John',
11466 'last_name' => 'Doe',
11467 'email' => 'john_doe@sample.com',
11468 'person_type' => 'agent',
11469 'status' => 'active',
11470 'hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
11471 'user_id' => '1',
11472 'created_at' => '2023-04-27 07:30:46',
11473 'updated_at' => '2023-04-27 10:28:08',
11474 'full_name' => 'John Doe',
11475 'photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
11476 ];
11477
11478 $reply_data = [
11479 'ticket_id' => '1',
11480 'conversation_type' => 'response',
11481 'content' => '<p>Sample content.</p>',
11482 'source' => 'web',
11483 'content_hash' => '2cc0e35d8fb92a0675d67999b073b3a4',
11484 'created_at' => '2023-04-27 07:30:46',
11485 'updated_at' => '2023-04-27 10:28:08',
11486 'id' => '1',
11487 'person_id' => '2',
11488 'person_first_name' => 'John',
11489 'person_last_name' => 'Doe',
11490 'person_email' => 'john_doe@sample.com',
11491 'person_person_type' => 'agent',
11492 'person_status' => 'active',
11493 'person_hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
11494 'person_user_id' => '1',
11495 'person_created_at' => '2023-04-27 07:30:46',
11496 'person_updated_at' => '2023-04-27 10:28:08',
11497 'person_full_name' => 'John Doe',
11498 'person_photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
11499 ];
11500
11501 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
11502 $mailbox = isset( $data['mailbox_id'] ) ? $data['mailbox_id'] : '';
11503 $product_id = isset( $data['filter']['ticket_product_id']['value'] ) ? $data['filter']['ticket_product_id']['value'] : '';
11504 $person_id = isset( $data['filter']['person_id']['value'] ) ? (string) $data['filter']['person_id']['value'] : '';
11505
11506 if ( in_array( $term, [ 'response_added_by_agent', 'response_added_by_customer', 'ticket_replied_product_agent', 'ticket_replied_product_customer' ], true ) ) {
11507 if ( 'response_added_by_agent' == $term ) {
11508 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
11509 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
11510 $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 ) );
11511 if ( ! empty( $tickets ) ) {
11512 $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 ) );
11513 $ticket_data = $tickets[0];
11514 $customer_data = $customers[0];
11515 $reply_data = $replies[0];
11516 $context['response_type'] = 'live';
11517 }
11518 } elseif ( 'response_added_by_customer' == $term ) {
11519 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
11520 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
11521 $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 ) );
11522 if ( ! empty( $tickets ) ) {
11523 $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 ) );
11524 $ticket_data = $tickets[0];
11525 $customer_data = $customers[0];
11526 $reply_data = $replies[0];
11527 $context['response_type'] = 'live';
11528 }
11529 } elseif ( 'ticket_replied_product_agent' == $term ) {
11530 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
11531 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
11532 if ( -1 == $product_id ) {
11533 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
11534 $products = array_map(
11535 function( $product ) {
11536 return $product['id'];
11537 },
11538 $products
11539 );
11540 $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 ) ) );
11541 } else {
11542 $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 ) );
11543 }
11544 if ( ! empty( $tickets ) ) {
11545 $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 ) );
11546 $ticket_data = $tickets[0];
11547 $customer_data = $customers[0];
11548 $reply_data = $replies[0];
11549 $context['response_type'] = 'live';
11550 }
11551 } elseif ( 'ticket_replied_product_customer' == $term ) {
11552 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
11553 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
11554 if ( -1 == $product_id ) {
11555 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
11556 $products = array_map(
11557 function( $product ) {
11558 return $product['id'];
11559 },
11560 $products
11561 );
11562 $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 ) ) );
11563 } else {
11564 $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 ) );
11565 }
11566 if ( ! empty( $tickets ) ) {
11567 $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 ) );
11568 $ticket_data = $tickets[0];
11569 $customer_data = $customers[0];
11570 $reply_data = $replies[0];
11571 $context['response_type'] = 'live';
11572 }
11573 }
11574 $context['pluggable_data'] = array_merge(
11575 [
11576 'reply' => $reply_data,
11577 'ticket' => $ticket_data,
11578 'customer' => $customer_data,
11579 ]
11580 );
11581 if ( 'ticket_replied_product_customer' == $term || 'ticket_replied_product_agent' == $term ) {
11582 $context['pluggable_data']['ticket_product_id'] = $product_id;
11583 }
11584 if ( is_object( $ticket_data ) ) {
11585 $ticket_data = get_object_vars( $ticket_data );
11586 }
11587 $ticket = \FluentSupport\App\Models\Ticket::find( $ticket_data['id'] );
11588 if ( is_object( $ticket ) && method_exists( $ticket, 'customData' ) ) {
11589 $context['pluggable_data']['custom_fields'] = $ticket->customData();
11590 }
11591 $context['pluggable_data']['ticket_link'] = admin_url( "admin.php?page=fluent-support#/tickets/{$ticket_data['id']}/view" );
11592 } else {
11593 if ( 'ticket_created' == $term ) {
11594 if ( -1 == $mailbox ) {
11595 $tickets = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE status = 'new' ORDER BY id DESC LIMIT 1" );
11596 } else {
11597 $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 ) );
11598 }
11599 if ( ! empty( $tickets ) ) {
11600 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d", $tickets[0]->customer_id ) );
11601 $ticket_data = $tickets[0];
11602 $customer_data = $customers[0];
11603 }
11604 } elseif ( 'ticket_closed_by_customer' == $term ) {
11605 $customers_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
11606 $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] ) );
11607 if ( ! empty( $tickets ) ) {
11608 $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 ) );
11609 $ticket_data = $tickets[0];
11610 $customer_data = $customers[0];
11611 }
11612 } elseif ( 'ticket_closed_by_agent' == $term ) {
11613 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
11614 $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] ) );
11615 if ( ! empty( $tickets ) ) {
11616 $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 ) );
11617 $ticket_data = $tickets[0];
11618 $customer_data = $customers[0];
11619 }
11620 } elseif ( 'ticket_created_product' == $term ) {
11621 $agents_type = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_persons" );
11622 if ( -1 == $product_id ) {
11623 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
11624 $products = array_map(
11625 function( $product ) {
11626 return $product['id'];
11627 },
11628 $products
11629 );
11630 $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 ) ) );
11631 } else {
11632 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id = %d ORDER BY id DESC LIMIT 1", $product_id ) );
11633 }
11634 if ( ! empty( $tickets ) ) {
11635 $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 ) );
11636 $ticket_data = $tickets[0];
11637 $customer_data = $customers[0];
11638 }
11639 } elseif ( 'ticket_closed_product' == $term ) {
11640 $agents_type = $wpdb->get_results( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = %s", $person_id ), ARRAY_A );
11641 $agents = array_map(
11642 function( $agent ) {
11643 return $agent['id'];
11644 },
11645 $agents_type
11646 );
11647 if ( -1 == $product_id ) {
11648 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
11649 $products = array_map(
11650 function( $product ) {
11651 return $product['id'];
11652 },
11653 $products
11654 );
11655 $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 ) ) );
11656 } else {
11657 $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 ) ) );
11658 }
11659 if ( ! empty( $tickets ) ) {
11660 $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 ) );
11661 $ticket_data = $tickets[0];
11662 $customer_data = $customers[0];
11663 }
11664 }
11665 $context['pluggable_data'] = array_merge(
11666 [
11667 'ticket' => $ticket_data,
11668 'customer' => $customer_data,
11669 ]
11670 );
11671 if ( 'ticket_created' == $term ) {
11672 $context['pluggable_data']['mailbox_id'] = $mailbox;
11673 } elseif ( 'ticket_created_product' == $term || 'ticket_replied_product_customer' == $term || 'ticket_replied_product_agent' == $term ) {
11674 $context['pluggable_data']['ticket_product_id'] = $product_id;
11675 } elseif ( 'ticket_closed_product' == $term ) {
11676 $context['pluggable_data']['ticket_product_id'] = $product_id;
11677 $context['pluggable_data']['person_id'] = $person_id;
11678 }
11679 if ( is_object( $ticket_data ) ) {
11680 $ticket_data = get_object_vars( $ticket_data );
11681 }
11682 $ticket = \FluentSupport\App\Models\Ticket::find( $ticket_data['id'] );
11683 if ( is_object( $ticket ) && method_exists( $ticket, 'customData' ) ) {
11684 $context['pluggable_data']['custom_fields'] = $ticket->customData();
11685 }
11686 $context['pluggable_data']['ticket_link'] = admin_url( "admin.php?page=fluent-support#/tickets/{$ticket_data['id']}/view" );
11687 $context['response_type'] = 'live';
11688 }
11689
11690 return $context;
11691 }
11692
11693 /**
11694 * Prepare Ultimate Member user_roles.
11695 *
11696 * @param array $data data.
11697 *
11698 * @return array
11699 */
11700 public function search_um_user_roles( $data ) {
11701 if ( function_exists( 'get_editable_roles' ) ) {
11702 $roles = get_editable_roles();
11703 } else {
11704 $roles = wp_roles()->roles;
11705 $roles = apply_filters( 'editable_roles', $roles );
11706 }
11707
11708 $options = [];
11709 foreach ( $roles as $role => $details ) {
11710
11711 $options[] = [
11712 'label' => $details['name'],
11713 'value' => $role,
11714 ];
11715
11716 }
11717
11718 return [
11719 'options' => $options,
11720 'hasMore' => false,
11721 ];
11722 }
11723
11724 /**
11725 * Prepare Ultimate Member forms_list.
11726 *
11727 * @param array $data data.
11728 *
11729 * @return array
11730 */
11731 public function search_um_forms_list( $data ) {
11732
11733 $page = $data['page'];
11734 $limit = Utilities::get_search_page_limit();
11735 $offset = $limit * ( $page - 1 );
11736
11737 $args = [
11738 'posts_per_page' => $limit,
11739 'offset' => $offset,
11740 'orderby' => 'title',
11741 'order' => 'ASC',
11742 'post_type' => 'um_form',
11743 'post_status' => 'publish',
11744 'fields' => 'ids',
11745 ];
11746 if ( 'register' == $data['filter']['type'] ) {
11747 $args['meta_query'] = [
11748 [
11749 'key' => '_um_mode',
11750 'value' => 'register',
11751 'compare' => 'LIKE',
11752 ],
11753 ];
11754 } elseif ( 'login' == $data['filter']['type'] ) {
11755 $args['meta_query'] = [
11756 [
11757 'key' => '_um_mode',
11758 'value' => 'login',
11759 'compare' => 'LIKE',
11760 ],
11761 ];
11762 }
11763
11764 $forms_list = get_posts( $args );
11765
11766 $forms_list_count = wp_count_posts( 'um_form' )->publish;
11767
11768 $options = [];
11769 if ( ! empty( $forms_list ) ) {
11770 foreach ( $forms_list as $form ) {
11771 $title = html_entity_decode( get_the_title( $form ), ENT_QUOTES, 'UTF-8' );
11772 $options[] = [
11773 'label' => $title,
11774 'value' => $form,
11775 ];
11776 }
11777 }
11778
11779 return [
11780 'options' => $options,
11781 'hasMore' => $forms_list_count > $limit && $forms_list_count > $offset,
11782 ];
11783 }
11784
11785 /**
11786 * Get last data for Ultimate Member Login trigger.
11787 *
11788 * @param array $data data.
11789 * @return mixed
11790 */
11791 public function search_ultimate_member_user_logsin( $data ) {
11792 $context = [];
11793 $args = [
11794 'orderby' => 'meta_value',
11795 'meta_key' => '_um_last_login',
11796 'order' => 'DESC',
11797 'number' => 1,
11798 ];
11799 $users = get_users( $args );
11800
11801 if ( ! empty( $users ) ) {
11802 $user = $users[0];
11803 $submitted_data = get_user_meta( $user->ID, 'submitted', true );
11804 if ( is_array( $submitted_data ) ) {
11805 unset(
11806 $submitted_data['user_password'],
11807 $submitted_data['confirm_user_password']
11808 );
11809 }
11810 $pluggable_data = WordPress::get_user_context( $user->ID );
11811 $pluggable_data['data'] = $submitted_data;
11812 $context['pluggable_data'] = $pluggable_data;
11813 $context['response_type'] = 'live';
11814 } else {
11815 $role = 'subscriber';
11816 $context['pluggable_data'] = [
11817 'wp_user_id' => 1,
11818 'user_login' => 'test',
11819 'display_name' => 'Test User',
11820 'user_firstname' => 'Test',
11821 'user_lastname' => 'User',
11822 'user_email' => 'testuser@gmail.com',
11823 'user_role' => [ $role ],
11824 ];
11825 $context['response_type'] = 'sample';
11826 }
11827 return $context;
11828 }
11829
11830 /**
11831 * Get last data for Ultimate Member Register trigger.
11832 *
11833 * @param array $data data.
11834 * @return mixed
11835 */
11836 public function search_ultimate_member_user_registers( $data ) {
11837 $context = [];
11838 $args = [
11839 'orderby' => 'meta_value',
11840 'meta_key' => 'um_user_profile_url_slug_user_login',
11841 'order' => 'DESC',
11842 'number' => 1,
11843 ];
11844 $users = get_users( $args );
11845
11846 if ( ! empty( $users ) ) {
11847 $user = $users[0];
11848 $submitted_data = get_user_meta( $user->ID, 'submitted', true );
11849 if ( is_array( $submitted_data ) ) {
11850 unset(
11851 $submitted_data['user_password'],
11852 $submitted_data['confirm_user_password']
11853 );
11854 }
11855 $pluggable_data = WordPress::get_user_context( $user->ID );
11856 $pluggable_data['data'] = $submitted_data;
11857 $context['pluggable_data'] = $pluggable_data;
11858 $context['response_type'] = 'live';
11859 } else {
11860 $role = 'subscriber';
11861 $context['pluggable_data'] = [
11862 'wp_user_id' => 1,
11863 'user_login' => 'test',
11864 'display_name' => 'Test User',
11865 'user_firstname' => 'Test',
11866 'user_lastname' => 'User',
11867 'user_email' => 'testuser@gmail.com',
11868 'user_role' => [ $role ],
11869 ];
11870 $context['response_type'] = 'sample';
11871 }
11872 return $context;
11873 }
11874
11875 /**
11876 * Get last data for Ultimate Member Register trigger.
11877 *
11878 * @param array $data data.
11879 * @return mixed
11880 */
11881 public function search_ultimate_member_user_inactive( $data ) {
11882 $context = [];
11883 $args = [
11884 'orderby' => 'user_id',
11885 'meta_key' => 'account_status',
11886 'meta_value' => 'inactive',
11887 'order' => 'ASC',
11888 'number' => 1,
11889 ];
11890 $users = get_users( $args );
11891
11892 if ( ! empty( $users ) ) {
11893 $user = $users[0];
11894 $pluggable_data = [];
11895 $pluggable_data[] = WordPress::get_user_context( $user->ID );
11896 $pluggable_data['user_account_status'] = 'inactive';
11897 $context['pluggable_data'] = $pluggable_data;
11898 $context['response_type'] = 'live';
11899 } else {
11900 $role = 'subscriber';
11901 $context['pluggable_data'] = [
11902 'wp_user_id' => 1,
11903 'user_login' => 'test',
11904 'display_name' => 'Test User',
11905 'user_firstname' => 'Test',
11906 'user_lastname' => 'User',
11907 'user_email' => 'testuser@gmail.com',
11908 'user_role' => [ $role ],
11909 'user_account_status' => 'inactive',
11910 ];
11911 $context['response_type'] = 'sample';
11912 }
11913 return $context;
11914 }
11915
11916 /**
11917 * Get last data for Ultimate Member Change Role trigger.
11918 *
11919 * @param array $data data.
11920 * @return mixed
11921 */
11922 public function search_ultimate_member_user_role_change( $data ) {
11923 $context = [];
11924
11925 $role = $data['filter']['role']['value'];
11926
11927 $args = [
11928 'number' => 1,
11929 'role' => $role,
11930 ];
11931 $users = get_users( $args );
11932 shuffle( $users );
11933 if ( ! empty( $users ) ) {
11934 $user = $users[0];
11935 $pluggable_data = WordPress::get_user_context( $user->ID );
11936 $context['pluggable_data'] = $pluggable_data;
11937 $context['response_type'] = 'live';
11938 } else {
11939 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
11940 $context['pluggable_data'] = [
11941 'wp_user_id' => 1,
11942 'user_login' => 'test',
11943 'display_name' => 'Test User',
11944 'user_firstname' => 'Test',
11945 'user_lastname' => 'User',
11946 'user_email' => 'testuser@gmail.com',
11947 'user_role' => [ $role ],
11948 'user_account_status' => 'inactive',
11949 ];
11950 $context['response_type'] = 'sample';
11951 }
11952 return $context;
11953 }
11954
11955 /**
11956 * Get JetEngine WP Posttypes.
11957 *
11958 * @param array $data data.
11959 *
11960 * @return array
11961 */
11962 public function search_je_posttype_list( $data ) {
11963
11964 $post_types = get_post_types( [ 'public' => true ], 'objects' );
11965 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
11966 if ( isset( $post_types['attachment'] ) ) {
11967 unset( $post_types['attachment'] );
11968 }
11969
11970 $options = [];
11971 foreach ( $post_types as $post_type => $details ) {
11972 $options[] = [
11973 'label' => $details->label,
11974 'value' => $post_type,
11975 ];
11976 }
11977
11978 return [
11979 'options' => $options,
11980 'hasMore' => false,
11981 ];
11982 }
11983
11984 /**
11985 * Get JetEngine WP fields.
11986 *
11987 * @param array $data data.
11988 *
11989 * @return array
11990 */
11991 public function search_je_field_list( $data ) {
11992
11993 $post_type = $data['dynamic'];
11994
11995 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
11996
11997 $post_fields = array_filter(
11998 $metaboxes,
11999 function( $metabox ) {
12000 /**
12001 *
12002 * Ignore line
12003 *
12004 * @phpstan-ignore-next-line
12005 */
12006 return 'post' === $metabox['args']['object_type'];
12007 }
12008 );
12009
12010 $post_fields_count = count( $post_fields );
12011
12012 $options = [];
12013 if ( ! empty( $post_fields ) ) {
12014 if ( is_array( $post_fields ) ) {
12015 foreach ( $post_fields as $post_field ) {
12016 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
12017 foreach ( $post_field['meta_fields'] as $meta_field ) {
12018 $options[] = [
12019 'label' => $meta_field['title'],
12020 'value' => $meta_field['name'],
12021 ];
12022 }
12023 }
12024 }
12025 }
12026 }
12027 return [
12028 'options' => $options,
12029 'hasMore' => false,
12030 ];
12031 }
12032
12033 /**
12034 * Search Last Updated Field Data.
12035 *
12036 * @param array $data data.
12037 * @return array
12038 */
12039 public function search_jet_engine_field_data( $data ) {
12040 global $wpdb;
12041
12042 $context = [];
12043
12044 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12045
12046 $post_type = $data['filter']['wp_post_type']['value'];
12047
12048 if ( -1 === $field ) {
12049 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
12050
12051 $post_fields = array_filter(
12052 $metaboxes,
12053 function( $metabox ) {
12054 /**
12055 *
12056 * Ignore line
12057 *
12058 * @phpstan-ignore-next-line
12059 */
12060 return 'post' === $metabox['args']['object_type'];
12061 }
12062 );
12063
12064 $options = [];
12065 if ( ! empty( $post_fields ) ) {
12066 if ( is_array( $post_fields ) ) {
12067 foreach ( $post_fields as $post_field ) {
12068 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
12069 foreach ( $post_field['meta_fields'] as $meta_field ) {
12070 $options[] = $meta_field['name'];
12071 }
12072 }
12073 }
12074 }
12075 }
12076 $random_key = array_rand( $options );
12077 $random_value = $options[ $random_key ];
12078 $string = '%' . $random_value . '%';
12079 } else {
12080 $string = '%' . $data['filter']['field_id']['value'] . '%';
12081 }
12082
12083 $result = $wpdb->get_results(
12084 $wpdb->prepare(
12085 'SELECT post_id FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key LIKE %s',
12086 [ $string ]
12087 ),
12088 ARRAY_A
12089 );
12090
12091 $ids = [];
12092
12093 if ( ! empty( $result ) ) {
12094 foreach ( $result as $val ) {
12095 $ids[] = $val['post_id'];
12096 }
12097 }
12098
12099 $lastupdated_args = [
12100 'post_type' => $post_type,
12101 'orderby' => 'modified',
12102 'post__in' => $ids,
12103 'posts_per_page' => 1,
12104 ];
12105 $lastupdated_loop = get_posts( $lastupdated_args );
12106
12107 $response = [];
12108 if ( ! empty( $result ) ) {
12109 $context['post'] = $lastupdated_loop[0];
12110
12111 $meta_value = get_post_meta( $lastupdated_loop[0]->ID, sprintf( '%s', $data['filter']['field_id']['value'] ), true );
12112 $meta_key = sprintf( '%s', $data['filter']['field_id']['value'] );
12113
12114 $context[ $meta_key ] = $meta_value;
12115
12116 $response['pluggable_data'] = $context;
12117 $response['response_type'] = 'live';
12118 } else {
12119 $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:\/\/examplewp.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 );
12120 }
12121
12122 return $response;
12123
12124 }
12125
12126 /**
12127 * Get Formidable Forms.
12128 *
12129 * @param array $data data.
12130 *
12131 * @return array|void
12132 */
12133 public function search_formidable_form_list( $data ) {
12134 if ( ! class_exists( 'FrmForm' ) ) {
12135 return;
12136 }
12137
12138 $page = $data['page'];
12139 $term = $data['search_term'];
12140 $limit = Utilities::get_search_page_limit();
12141 $offset = $limit * ( $page - 1 );
12142 $per_page = 10;
12143
12144 $query = [
12145 [
12146 'or' => 1,
12147 'name LIKE' => $term,
12148 'parent_form_id' => null,
12149 ],
12150 ];
12151 $query['is_template'] = 0;
12152 $query['status !'] = 'trash';
12153 $forms_list = FrmForm::getAll( $query, '', $offset . ',' . $per_page );
12154 $form_count = FrmForm::getAll( $query );
12155 $form_count = count( $form_count );
12156 $options = [];
12157
12158 if ( ! empty( $forms_list ) ) {
12159 if ( is_array( $forms_list ) ) {
12160 foreach ( $forms_list as $form ) {
12161 $options[] = [
12162 'label' => $form->name,
12163 'value' => $form->id,
12164 ];
12165 }
12166 }
12167 }
12168
12169 return [
12170 'options' => $options,
12171 'hasMore' => $form_count > $limit && $form_count > $offset,
12172 ];
12173 }
12174
12175 /**
12176 * Get JetFormBuilder Form List.
12177 *
12178 * @param array $data data.
12179 *
12180 * @return array|void
12181 */
12182 public function search_jetform_list( $data ) {
12183 if ( ! class_exists( '\Jet_Form_Builder\Classes\Tools' ) ) {
12184 return;
12185 }
12186
12187 $forms = \Jet_Form_Builder\Classes\Tools::get_forms_list_for_js();
12188
12189 $options = [];
12190 foreach ( $forms as $form ) {
12191
12192 if ( ! empty( $form['value'] ) ) {
12193 $options[] = [
12194 'label' => esc_html( $form['label'] ),
12195 'value' => esc_attr( $form['value'] ),
12196 ];
12197 }
12198 }
12199
12200 return [
12201 'options' => $options,
12202 'hasMore' => false,
12203 ];
12204 }
12205
12206 /**
12207 * Get Forminator Form List.
12208 *
12209 * @param array $data data.
12210 *
12211 * @return array|void
12212 */
12213 public function search_forminator_form_list( $data ) {
12214 if ( ! class_exists( 'Forminator_API' ) ) {
12215 return;
12216 }
12217
12218 $forms = Forminator_API::get_forms( null, 1, 10 );
12219
12220 $options = [];
12221 foreach ( $forms as $form ) {
12222 $options[] = [
12223 'label' => isset( $form->settings ) && isset( $form->settings['form_name'] ) ? $form->settings['form_name'] : $form->name,
12224 'value' => $form->id,
12225 ];
12226 }
12227
12228 return [
12229 'options' => $options,
12230 'hasMore' => false,
12231 ];
12232 }
12233
12234
12235 /**
12236 * Get BbPress topics list.
12237 *
12238 * @param array $data data.
12239 *
12240 * @return array|void
12241 */
12242 public function search_bbp_topic_list( $data ) {
12243 $page = $data['page'];
12244 $forum_id = $data['dynamic'];
12245 $limit = Utilities::get_search_page_limit();
12246 $offset = $limit * ( $page - 1 );
12247 $args = [
12248 'post_type' => 'topic',
12249 'offset' => $offset,
12250 'meta_query' => [
12251 [
12252 'key' => '_bbp_forum_id',
12253 'value' => $forum_id,
12254 'compare' => '==',
12255 ],
12256 ],
12257 ];
12258
12259 $topics = get_posts( $args );
12260 $topics_count = wp_count_posts( 'topic' )->publish;
12261
12262 $options = [];
12263 if ( ! empty( $topics ) ) {
12264 if ( is_array( $topics ) ) {
12265 foreach ( $topics as $topic ) {
12266 $options[] = [
12267 'label' => $topic->post_title,
12268 'value' => $topic->ID,
12269 ];
12270 }
12271 }
12272 }
12273 return [
12274 'options' => $options,
12275 'hasMore' => $topics_count > $limit && $topics_count > $offset,
12276 ];
12277 }
12278
12279
12280 /**
12281 * Search Last Updated Field Data.
12282 *
12283 * @param array $data data.
12284 * @return array
12285 */
12286 public function search_bbp_last_data( $data ) {
12287 global $wpdb;
12288
12289 $post_type = $data['post_type'];
12290 $trigger = $data['search_term'];
12291 $context = [];
12292
12293 if ( 'topic' === $post_type ) {
12294 $post_id = $data['filter']['forum']['value'];
12295 if ( -1 === $post_id ) {
12296 $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" );
12297
12298 } else {
12299 $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 ) );
12300
12301 }
12302 } elseif ( 'reply' === $post_type ) {
12303 $post_id = $data['filter']['topic']['value'];
12304
12305 if ( -1 === $post_id ) {
12306 $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" );
12307
12308 } else {
12309 $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 ) );
12310 }
12311 }
12312
12313
12314 $response = [];
12315 if ( ! empty( $result ) ) {
12316 if ( 'bbpress_topic_created' === $trigger ) {
12317 $topic_id = $result[0]->post_id;
12318 $forum_id = $result[0]->meta_value;
12319 $topic = get_the_title( $topic_id );
12320 $topic_link = get_the_permalink( $topic_id );
12321 $topic_description = get_the_content( null, false, $topic_id );
12322 $topic_status = get_post_status( $topic_id );
12323
12324 $forum = get_the_title( $forum_id );
12325 $forum_link = get_the_permalink( $forum_id );
12326 $forum_description = get_the_content( null, false, $forum_id );
12327 $forum_status = get_post_status( $forum_id );
12328
12329 $forum = [
12330 'forum' => $forum_id,
12331 'forum_title' => $forum,
12332 'forum_link' => $forum_link,
12333 'forum_description' => $forum_description,
12334 'forum_status' => $forum_status,
12335 ];
12336
12337 $topic = [
12338 'topic' => $topic_id,
12339 'topic_title' => $topic,
12340 'topic_link' => $topic_link,
12341 'topic_description' => $topic_description,
12342 'topic_status' => $topic_status,
12343 ];
12344
12345 $user_id = $result[0]->post_author;
12346 if ( '0' != $user_id ) {
12347 $context = array_merge(
12348 WordPress::get_user_context( $user_id ),
12349 $forum,
12350 $topic
12351 );
12352 } else {
12353 $anonymous_data = [
12354 'bbp_anonymous_name' => get_post_meta( $result[0]->ID, '_bbp_anonymous_name', true ),
12355 'bbp_anonymous_email' => get_post_meta( $result[0]->ID, '_bbp_anonymous_email', true ),
12356 'bbp_anonymous_website' => get_post_meta( $result[0]->ID, '_bbp_anonymous_website', true ),
12357 ];
12358 $context = array_merge(
12359 $anonymous_data,
12360 $forum,
12361 $topic
12362 );
12363 }
12364
12365 $response['pluggable_data'] = $context;
12366 $response['response_type'] = 'live';
12367 } else {
12368 $reply_id = $result[0]->post_id;
12369 $topic_id = $result[0]->meta_value;
12370 $forum_id = get_post_meta( $topic_id, '_bbp_forum_id', true );
12371 $forum_id = intval( '"' . $forum_id . '"' );
12372 $reply = get_the_title( $reply_id );
12373 $reply_link = get_the_permalink( $reply_id );
12374 $reply_description = get_the_content( null, false, $reply_id );
12375 $reply_status = get_post_status( $reply_id );
12376
12377
12378 $topic = get_the_title( $topic_id );
12379 $topic_link = get_the_permalink( $topic_id );
12380 $topic_description = get_the_content( null, false, $topic_id );
12381 $topic_status = get_post_status( $topic_id );
12382
12383 $forum = get_the_title( $forum_id );
12384 $forum_link = get_the_permalink( $forum_id );
12385 $forum_description = get_the_content( null, false, $forum_id );
12386 $forum_status = get_post_status( $forum_id );
12387
12388 $forum = [
12389 'forum' => $forum_id,
12390 'forum_title' => $forum,
12391 'forum_link' => $forum_link,
12392 'forum_description' => $forum_description,
12393 'forum_status' => $forum_status,
12394 ];
12395
12396 $topic = [
12397 'topic' => $topic_id,
12398 'topic_title' => $topic,
12399 'topic_link' => $topic_link,
12400 'topic_description' => $topic_description,
12401 'topic_status' => $topic_status,
12402 ];
12403
12404 $reply = [
12405 'reply_title' => $reply,
12406 'reply_link' => $reply_link,
12407 'reply_description' => $reply_description,
12408 'reply_status' => $reply_status,
12409 ];
12410 $user_id = $result[0]->post_author;
12411 if ( '0' != $user_id ) {
12412 $context = array_merge(
12413 WordPress::get_user_context( $user_id ),
12414 $forum,
12415 $topic,
12416 $reply
12417 );
12418 } else {
12419 $anonymous_data = [
12420 'bbp_anonymous_name' => get_post_meta( $result[0]->ID, '_bbp_anonymous_name', true ),
12421 'bbp_anonymous_email' => get_post_meta( $result[0]->ID, '_bbp_anonymous_email', true ),
12422 'bbp_anonymous_website' => get_post_meta( $result[0]->ID, '_bbp_anonymous_website', true ),
12423 ];
12424 $context = array_merge(
12425 $anonymous_data,
12426 $forum,
12427 $topic,
12428 $reply
12429 );
12430 }
12431
12432 $response['pluggable_data'] = $context;
12433 $response['response_type'] = 'live';
12434 }
12435 }
12436
12437 return $response;
12438 }
12439
12440 /**
12441 * Search Last Updated Field Data.
12442 *
12443 * @param array $data data.
12444 * @return array|void
12445 */
12446 public function search_happyform_list( $data ) {
12447 if ( ! function_exists( 'happyforms_get_form_controller' ) ) {
12448 return;
12449 }
12450
12451 $form_controller = happyforms_get_form_controller();
12452
12453 $forms = $form_controller->do_get();
12454 $options = [];
12455 if ( ! empty( $forms ) ) {
12456 foreach ( $forms as $form ) {
12457 $options[] = [
12458 'label' => $form['post_title'],
12459 'value' => $form['ID'],
12460 ];
12461 }
12462 }
12463
12464 return [
12465 'options' => $options,
12466 'hasMore' => false,
12467 ];
12468 }
12469
12470 /**
12471 * Get Memberpress Course List.
12472 *
12473 * @param array $data data.
12474 *
12475 * @return array|void
12476 */
12477 public function search_mpc_lessons_list( $data ) {
12478 if ( ! class_exists( '\memberpress\courses\models\Lesson' ) ) {
12479 return;
12480 }
12481 global $wpdb;
12482 $options = [];
12483 $course_id = $data['dynamic'];
12484 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mpcs_sections WHERE course_id =%s", $course_id ) );
12485 $sections = [];
12486 foreach ( $result as $rec ) {
12487 $sections[] = [
12488 'id' => $rec->id,
12489 'title' => $rec->title,
12490 ];
12491 }
12492 if ( is_array( $sections ) && count( $sections ) > 0 ) {
12493 foreach ( $sections as $section ) {
12494 $post_types_string = \memberpress\courses\models\Lesson::lesson_cpts();
12495 $post_types_string = implode( "','", $post_types_string );
12496
12497 $query = $wpdb->prepare(
12498 "SELECT * FROM {$wpdb->posts} AS p
12499 JOIN {$wpdb->postmeta} AS pm
12500 ON p.ID = pm.post_id
12501 AND pm.meta_key = %s
12502 AND pm.meta_value = %s
12503 JOIN {$wpdb->postmeta} AS pm_order
12504 ON p.ID = pm_order.post_id
12505 AND pm_order.meta_key = %s
12506 WHERE p.post_type in ( %s ) AND p.post_status <> 'trash'
12507 ORDER BY pm_order.meta_value * 1",
12508 models\Lesson::$section_id_str,
12509 $section['id'],
12510 models\Lesson::$lesson_order_str,
12511 stripcslashes( $post_types_string )
12512 );
12513
12514 $db_lessons = $wpdb->get_results( stripcslashes( $query ) ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
12515 foreach ( $db_lessons as $lesson ) {
12516 $options[] = [
12517 'label' => $section['title'] . '->' . $lesson->post_title,
12518 'value' => $lesson->ID,
12519 ];
12520 }
12521 }
12522 }
12523 return [
12524 'options' => $options,
12525 'hasMore' => false,
12526 ];
12527 }
12528
12529 /**
12530 * Get Memberpress Course List.
12531 *
12532 * @param array $data data.
12533 *
12534 * @return array|void
12535 */
12536 public function search_gp_rank_type_list( $data ) {
12537 global $wpdb;
12538
12539 $posts = $wpdb->get_results(
12540 "SELECT ID, post_name, post_title, post_type
12541 FROM $wpdb->posts
12542 WHERE post_type LIKE 'rank-type' AND post_status = 'publish' ORDER BY post_title ASC"
12543 );
12544
12545 $posts_count = count( $posts );
12546
12547 $options = [];
12548 if ( $posts ) {
12549 foreach ( $posts as $post ) {
12550 $options[] = [
12551 'label' => $post->post_title,
12552 'value' => $post->post_name,
12553 ];
12554 }
12555 }
12556
12557 return [
12558 'options' => $options,
12559 'hasMore' => false,
12560 ];
12561 }
12562
12563 /**
12564 * Get MPC last data.
12565 *
12566 * @param array $data data.
12567 *
12568 * @return array
12569 */
12570 public function search_mpc_last_data( $data ) {
12571 global $wpdb;
12572 $trigger = $data['search_term'];
12573 $course_data = [];
12574 $lesson_data = [];
12575 $context = [];
12576
12577 if ( 'mpc_course_completed' === $trigger ) {
12578 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course]']['value'] : '-1' );
12579 if ( $course_id > 0 ) {
12580
12581 $course = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $course_id ) );
12582 } else {
12583 $course = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-course' ORDER BY id DESC LIMIT 1" );
12584 }
12585
12586 if ( ! empty( $course ) ) {
12587 $course_data = [
12588 'course_id' => $course->ID,
12589 'course_title' => get_the_title( $course_id ),
12590 'course_url' => get_permalink( $course_id ),
12591 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
12592 'course_featured_image_url' => get_the_post_thumbnail_url( $course_id ),
12593 ];
12594 }
12595 $user_progress = $wpdb->get_row( $wpdb->prepare( "SELECT user_id FROM {$wpdb->prefix}mpcs_user_progress WHERE course_id=%s", $course_id ) );
12596 if ( ! empty( $user_progress ) ) {
12597 $context['response_type'] = 'live';
12598 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
12599 } else {
12600 $sample_data = '{"pluggable_data":{"wp_user_id":1,"user_login":"johnd","display_name":"johnd","user_firstname":"johnd","user_lastname":"johnd","user_email":"hello@example.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}
12601 ,"response_type":"sample"} ';
12602 $context = json_decode( $sample_data, true );
12603 }
12604 } elseif ( 'mpc_lesson_completed' === $trigger ) {
12605 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
12606 $course_id = (int) $data['filter']['course']['value'];
12607 if ( $lesson_id > 0 ) {
12608
12609 $lesson = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $lesson_id ) );
12610 } else {
12611 $lesson = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-lesson' ORDER BY id DESC LIMIT 1" );
12612 }
12613
12614 if ( ! empty( $lesson ) ) {
12615 $lesson_data = [
12616 'lesson_id' => $lesson->ID,
12617 'lesson_title' => get_the_title( $lesson_id ),
12618 'lesson_url' => get_permalink( $lesson_id ),
12619 'lesson_featured_image_id' => get_post_meta( $lesson_id, '_thumbnail_id', true ),
12620 'lesson_featured_image_url' => get_the_post_thumbnail_url( $lesson_id ),
12621 ];
12622
12623 $lesson_section_id = get_post_meta( $lesson->ID, '_mpcs_lesson_section_id', true );
12624
12625 $section = $wpdb->get_row( $wpdb->prepare( "SELECT course_id FROM {$wpdb->prefix}mpcs_sections WHERE ID= %s", $lesson_section_id ) );
12626
12627 $course_data = [
12628 'course_id' => $course_id,
12629 'course_title' => get_the_title( $course_id ),
12630 'course_url' => get_permalink( $course_id ),
12631 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
12632 'course_featured_image_url' => get_the_post_thumbnail_url( $section->course_id ),
12633 ];
12634 }
12635
12636 $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 ) );
12637 if ( ! empty( $user_progress ) ) {
12638 $context['response_type'] = 'live';
12639 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
12640 } else {
12641 $sample_data = '{"pluggable_data":{"wp_user_id":1,"user_login":"johnd","display_name":"johnd","user_firstname":"john","user_lastname":"dev","user_email":"johnd@example.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"}';
12642 $context = json_decode( $sample_data, true );
12643 }
12644 }
12645
12646
12647 return $context;
12648 }
12649
12650 /** Get GamiPress Rank List.
12651 *
12652 * @param array $data data.
12653 *
12654 * @return array|void
12655 */
12656 public function search_gp_rank_list( $data ) {
12657 global $wpdb;
12658
12659 $page = $data['page'];
12660 $limit = Utilities::get_search_page_limit();
12661 $offset = $limit * ( $page - 1 );
12662
12663 $args = [
12664 'post_type' => $data['dynamic']['rank_type'],
12665 'posts_per_page' => $limit,
12666 'offset' => $offset,
12667 'orderby' => 'title',
12668 'order' => 'ASC',
12669 'post_status' => 'publish',
12670 's' => $data['search_term'],
12671 ];
12672
12673 $rank_type = get_posts( $args );
12674
12675 $count_args = [
12676 'post_type' => $data['dynamic']['rank_type'],
12677 'posts_per_page' => -1,
12678 'post_status' => 'publish',
12679 's' => $data['search_term'],
12680 ];
12681
12682 $rank_posts = get_posts( $count_args );
12683 $rank_type_count = count( $rank_posts );
12684
12685 $options = [];
12686 if ( $rank_type ) {
12687 foreach ( $rank_type as $rank ) {
12688 $options[] = [
12689 'label' => $rank->post_title,
12690 'value' => $rank->ID,
12691 ];
12692 }
12693 }
12694
12695 return [
12696 'options' => $options,
12697 'hasMore' => $rank_type_count > $limit && $rank_type_count > $offset,
12698 ];
12699 }
12700
12701 /**
12702 * Get GamiPress PointType List.
12703 *
12704 * @param array $data data.
12705 *
12706 * @return array|void
12707 */
12708 public function search_gp_point_type_list( $data ) {
12709 global $wpdb;
12710
12711 $page = $data['page'];
12712 $limit = Utilities::get_search_page_limit();
12713 $offset = $limit * ( $page - 1 );
12714
12715 $args = [
12716 'post_type' => 'points-type',
12717 'posts_per_page' => $limit,
12718 'offset' => $offset,
12719 'orderby' => 'title',
12720 'order' => 'ASC',
12721 'post_status' => 'publish',
12722 's' => $data['search_term'],
12723 ];
12724
12725 $point_type = get_posts( $args );
12726
12727 $count_args = [
12728 'post_type' => 'points-type',
12729 'posts_per_page' => -1,
12730 'post_status' => 'publish',
12731 's' => $data['search_term'],
12732 ];
12733
12734 $count_point_type = get_posts( $count_args );
12735 $point_type_count = count( $count_point_type );
12736
12737 $options = [];
12738 if ( $point_type ) {
12739 foreach ( $point_type as $point ) {
12740 $options[] = [
12741 'label' => $point->post_title,
12742 'value' => $point->ID,
12743 ];
12744 }
12745 }
12746
12747 return [
12748 'options' => $options,
12749 'hasMore' => $point_type_count > $limit && $point_type_count > $offset,
12750 ];
12751 }
12752
12753 /**
12754 * Get GamiPress AchievementType List.
12755 *
12756 * @param array $data data.
12757 *
12758 * @return array|void
12759 */
12760 public function search_gp_achivement_type_list( $data ) {
12761 global $wpdb;
12762
12763 $posts = $wpdb->get_results(
12764 "SELECT ID, post_name, post_title, post_type
12765 FROM $wpdb->posts
12766 WHERE post_type LIKE 'achievement-type' AND post_status = 'publish' ORDER BY post_title ASC"
12767 );
12768
12769 $posts_count = count( $posts );
12770
12771 $options = [];
12772 if ( $posts ) {
12773 foreach ( $posts as $post ) {
12774 $options[] = [
12775 'label' => $post->post_title,
12776 'value' => $post->post_name,
12777 ];
12778 }
12779 }
12780
12781 $options[] = [
12782 'label' => 'Points awards',
12783 'value' => 'points-award',
12784 ];
12785 $options[] = [
12786 'label' => 'Step',
12787 'value' => 'step',
12788 ];
12789 $options[] = [
12790 'label' => 'Rank requirement',
12791 'value' => 'rank-requirement',
12792 ];
12793
12794 return [
12795 'options' => $options,
12796 'hasMore' => false,
12797 ];
12798 }
12799
12800 /**
12801 * Get GamiPress Award List.
12802 *
12803 * @param array $data data.
12804 *
12805 * @return array|void
12806 */
12807 public function search_gp_award_list( $data ) {
12808 global $wpdb;
12809
12810 $page = $data['page'];
12811 $limit = Utilities::get_search_page_limit();
12812 $offset = $limit * ( $page - 1 );
12813
12814 $args = [
12815 'post_type' => $data['dynamic']['achivement_type'],
12816 'posts_per_page' => $limit,
12817 'offset' => $offset,
12818 'orderby' => 'title',
12819 'order' => 'ASC',
12820 'post_status' => 'publish',
12821 's' => $data['search_term'],
12822 ];
12823
12824 $award_type = get_posts( $args );
12825 $count_args = [
12826 'post_type' => $data['dynamic']['achivement_type'],
12827 'posts_per_page' => -1,
12828 'post_status' => 'publish',
12829 's' => $data['search_term'],
12830 ];
12831
12832 $count_award_type = get_posts( $count_args );
12833 $award_type_count = count( $count_award_type );
12834 $options = [];
12835 if ( $award_type ) {
12836 foreach ( $award_type as $award ) {
12837 $options[] = [
12838 'label' => $award->post_title,
12839 'value' => $award->ID,
12840 ];
12841 }
12842 }
12843
12844 return [
12845 'options' => $options,
12846 'hasMore' => $award_type_count > $limit && $award_type_count > $offset,
12847 ];
12848 }
12849
12850 /**
12851 * Get Woocommerce Subscription Product List.
12852 *
12853 * @param array $data data.
12854 *
12855 * @return array|void
12856 */
12857 public function search_wc_subscription_product_list( $data ) {
12858 global $wpdb;
12859
12860 $subscriptions = $wpdb->get_results(
12861 $wpdb->prepare(
12862 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
12863 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
12864 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
12865 AND posts.post_type = %s
12866 AND posts.post_status = %s
12867 UNION ALL
12868 SELECT ID, post_title FROM $wpdb->posts
12869 WHERE post_type = %s
12870 AND post_status = %s
12871 ORDER BY post_title",
12872 'product',
12873 'publish',
12874 'shop_subscription',
12875 'publish'
12876 )
12877 );
12878 $page = $data['page'];
12879 $limit = Utilities::get_search_page_limit();
12880 $offset = $limit * ( $page - 1 );
12881 $subscriptions_products = [];
12882 $subscription_products_count = 10;
12883 if ( function_exists( 'wc_get_products' ) ) {
12884 $product_query = wc_get_products(
12885 [
12886 'type' => [ 'subscription', 'variable-subscription' ],
12887 'posts_per_page' => $limit,
12888 'offset' => $offset,
12889 'paginate' => true,
12890 ]
12891 );
12892 if ( is_object( $product_query ) ) {
12893 $subscriptions_products = $product_query->products;
12894 $subscription_products_count = $product_query->total;
12895 }
12896 }
12897
12898 $options = [];
12899 if ( $subscriptions ) {
12900 foreach ( $subscriptions as $post ) {
12901 $options[] = [
12902 'label' => $post->post_title,
12903 'value' => $post->ID,
12904 ];
12905 }
12906 return [
12907 'options' => $options,
12908 'hasMore' => false,
12909 ];
12910 } elseif ( $subscriptions_products ) {
12911 foreach ( $subscriptions_products as $product ) {
12912 $title = $product->get_name();
12913 $options[] = [
12914 'label' => ! empty( $title ) ? $title : $product->get_id(),
12915 'value' => $product->get_id(),
12916 ];
12917 }
12918 return [
12919 'options' => $options,
12920 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
12921 ];
12922 } else {
12923 return [
12924 'options' => $options,
12925 'hasMore' => false,
12926 ];
12927 }
12928 }
12929
12930 /**
12931 * Get Woocommerce Subscriptions Variation list.
12932 *
12933 * @param array $data data.
12934 *
12935 * @return array|void
12936 */
12937 public function search_wc_variable_subscription_list( $data ) {
12938 global $wpdb;
12939
12940 $page = $data['page'];
12941 $limit = Utilities::get_search_page_limit();
12942 $offset = $limit * ( $page - 1 );
12943
12944 if ( ! function_exists( 'wc_get_products' ) ) {
12945 return;
12946 }
12947 $subscription_products = wc_get_products(
12948 [
12949 'type' => [ 'variable-subscription' ],
12950 'posts_per_page' => $limit,
12951 'offset' => $offset,
12952 'orderby' => 'date',
12953 'order' => 'DESC',
12954 ]
12955 );
12956
12957 $subscription_products_count = count( (array) $subscription_products );
12958
12959 $options = [];
12960 if ( $subscription_products ) {
12961 foreach ( (array) $subscription_products as $product ) {
12962 $options[] = [
12963 'label' => $product->get_title(),
12964 'value' => $product->get_id(),
12965 ];
12966 }
12967 }
12968
12969 return [
12970 'options' => $options,
12971 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
12972 ];
12973 }
12974
12975 /**
12976 * Get Woocommerce Variation list.
12977 *
12978 * @param array $data data.
12979 *
12980 * @return array|void
12981 */
12982 public function search_wc_variation_list( $data ) {
12983 global $wpdb;
12984
12985 $page = $data['page'];
12986 $limit = Utilities::get_search_page_limit();
12987 $offset = $limit * ( $page - 1 );
12988
12989 $args = [
12990 'post_type' => 'product_variation',
12991 'post_parent' => $data['dynamic']['variable_subscription'],
12992 'posts_per_page' => $limit,
12993 'offset' => $offset,
12994 'orderby' => 'ID',
12995 'order' => 'ASC',
12996 'post_status' => 'publish',
12997 ];
12998
12999 $variation = get_posts( $args );
13000 $variation_count = count( $variation );
13001
13002 $options = [];
13003 if ( $variation ) {
13004 foreach ( $variation as $product ) {
13005 $options[] = [
13006 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
13007 'value' => $product->ID,
13008 ];
13009 }
13010 }
13011
13012 return [
13013 'options' => $options,
13014 'hasMore' => $variation_count > $limit && $variation_count > $offset,
13015 ];
13016 }
13017
13018 /**
13019 * Get Membership List.
13020 *
13021 * @param array $data data.
13022 *
13023 * @return array
13024 */
13025 public function search_membership_list( $data ) {
13026 global $wpdb;
13027
13028 $levels = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}pmpro_membership_levels ORDER BY id ASC" );
13029 $options = [];
13030 if ( $levels ) {
13031 foreach ( $levels as $level ) {
13032 $options[] = [
13033 'label' => $level->name,
13034 'value' => $level->id,
13035 ];
13036 }
13037 }
13038
13039 return [
13040 'options' => $options,
13041 'hasMore' => false,
13042 ];
13043 }
13044
13045 /**
13046
13047 * Get EventsManager last data.
13048 *
13049 * @param array $data data.
13050 *
13051 * @return array
13052 */
13053 public function search_events_manager_data( $data ) {
13054 global $wpdb;
13055 $trigger = $data['search_term'];
13056 $context = [];
13057
13058 $post_id = (int) ( isset( $data['filter']['post_id']['value'] ) ? $data['filter']['post_id']['value'] : '-1' );
13059 if ( 'em_user_register_in_event' === $trigger ) {
13060 if ( $post_id > 0 ) {
13061 $event_id_id = get_post_meta( $post_id, '_event_id', true );
13062 $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 ) );
13063 } else {
13064 $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" );
13065
13066 }
13067
13068 if ( ! empty( $all_bookings ) ) {
13069 $user_id = $all_bookings->person_id;
13070 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
13071 $bookings = json_decode( (string) wp_json_encode( $all_bookings ), true );
13072 $context['pluggable_data'] = array_merge(
13073 WordPress::get_user_context( $user_id ),
13074 is_array( $bookings ) ? $bookings : []
13075 );
13076 if ( ! empty( $location ) ) {
13077 $locations = json_decode( (string) wp_json_encode( $location ), true );
13078 $context['pluggable_data'] = array_merge(
13079 $context['pluggable_data'],
13080 is_array( $locations ) ? $locations : []
13081 );
13082 }
13083
13084 $context['response_type'] = 'live';
13085 }
13086 } elseif ( 'em_user_unregister_from_event' === $trigger ) {
13087
13088 if ( $post_id > 0 ) {
13089 $event_id_id = get_post_meta( $post_id, '_event_id', true );
13090 $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 ) );
13091 } else {
13092 $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" );
13093
13094 }
13095
13096 if ( ! empty( $all_bookings ) ) {
13097 $user_id = $all_bookings->person_id;
13098 $bookings = json_decode( (string) wp_json_encode( $all_bookings ), true );
13099 $context['pluggable_data'] = array_merge(
13100 WordPress::get_user_context( $user_id ),
13101 is_array( $bookings ) ? $bookings : []
13102 );
13103 $context['response_type'] = 'live';
13104 }
13105 } elseif ( 'em_user_booking_approved' === $trigger ) {
13106
13107 if ( $post_id > 0 ) {
13108 $event_id_id = get_post_meta( $post_id, '_event_id', true );
13109 $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 ) );
13110 } else {
13111 $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" );
13112
13113 }
13114
13115 if ( ! empty( $all_bookings ) ) {
13116 $user_id = $all_bookings->person_id;
13117 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
13118 $bookings = json_decode( (string) wp_json_encode( $all_bookings ), true );
13119 $context['pluggable_data'] = array_merge(
13120 WordPress::get_user_context( $user_id ),
13121 is_array( $bookings ) ? $bookings : []
13122 );
13123 if ( ! empty( $location ) ) {
13124 $locations = json_decode( (string) wp_json_encode( $location ), true );
13125 $context['pluggable_data'] = array_merge(
13126 $context['pluggable_data'],
13127 is_array( $locations ) ? $locations : []
13128 );
13129 }
13130
13131 $context['response_type'] = 'live';
13132
13133 }
13134 } elseif ( 'em_user_registers_event_with_specific_ticket' === $trigger ) {
13135 if ( $post_id > 0 ) {
13136 $event_id_id = get_post_meta( $post_id, '_event_id', true );
13137 $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 ) );
13138 }
13139 if ( ! empty( $all_bookings ) ) {
13140 $ticket_id = (int) $data['filter']['ticket_id']['value'];
13141 $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 ) );
13142 if ( ! empty( $all_ticket_bookings ) ) {
13143 $user_id = $all_bookings->person_id;
13144 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
13145 $bookings_str = wp_json_encode( $all_bookings );
13146 $ticket_bookings_str = wp_json_encode( $all_ticket_bookings );
13147
13148 $context['pluggable_data'] = array_merge(
13149 WordPress::get_user_context( $user_id )
13150 );
13151 if ( is_string( $bookings_str ) && is_string( $ticket_bookings_str ) ) {
13152 $bookings_arr = json_decode( $bookings_str, true );
13153 $ticket_bookings_arr = json_decode( $ticket_bookings_str, true );
13154 if ( is_array( $bookings_arr ) && is_array( $ticket_bookings_arr ) ) {
13155 $context['pluggable_data'] = array_merge(
13156 $context['pluggable_data'],
13157 $bookings_arr,
13158 $ticket_bookings_arr
13159 );
13160 }
13161 }
13162 if ( ! empty( $location ) ) {
13163 $location_str = wp_json_encode( $location );
13164 if ( is_string( $location_str ) ) {
13165 $location_arr = json_decode( $location_str, true );
13166 if ( is_array( $location_arr ) ) {
13167 $context['pluggable_data'] = array_merge( $context['pluggable_data'], $location_arr );
13168 }
13169 }
13170 }
13171 $context['response_type'] = 'live';
13172 }
13173 }
13174 }
13175 return $context;
13176 }
13177
13178 /**
13179 * Get Events Manager Events Ticket list.
13180 *
13181 * @param array $data data.
13182 *
13183 * @return array|void
13184 */
13185 public function search_em_event_tickets( $data ) {
13186 global $wpdb;
13187
13188 $options = [];
13189
13190 $event = $data['dynamic']['post_id'];
13191
13192 $event_id = $wpdb->get_var( $wpdb->prepare( "SELECT event_id FROM {$wpdb->prefix}em_events WHERE post_id = %d", $event ) );
13193 $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] ) );
13194
13195 if ( $tickets ) {
13196 foreach ( $tickets as $ticket ) {
13197 $options[] = [
13198 'label' => $ticket->ticket_name,
13199 'value' => $ticket->ticket_id,
13200 ];
13201 }
13202 }
13203
13204 return [
13205 'options' => $options,
13206 'hasMore' => false,
13207 ];
13208 }
13209
13210 /**
13211
13212 * Get learnpress last data.
13213 *
13214 * @param array $data data.
13215 *
13216 * @return array
13217 */
13218 public function search_learnpress_lms_last_data( $data ) {
13219 global $wpdb;
13220 $trigger = $data['search_term'];
13221 $course_data = [];
13222 $lesson_data = [];
13223 $context = [];
13224
13225
13226 if ( 'learnpress_course_completed' === $trigger ) {
13227 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
13228 if ( $course_id > 0 ) {
13229
13230 $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 ) );
13231 } else {
13232
13233 $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" );
13234 }
13235
13236 if ( ! empty( $course ) ) {
13237 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
13238 $context['response_type'] = 'live';
13239 $context['pluggable_data'] = $course_data;
13240 }
13241 } elseif ( 'learnpress_lesson_completed' === $trigger ) {
13242 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
13243 if ( $lesson_id > 0 ) {
13244
13245 $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 ) );
13246 } else {
13247
13248 $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" );
13249 }
13250
13251 if ( ! empty( $lesson ) ) {
13252 $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 ) );
13253 $context['response_type'] = 'live';
13254 $context['pluggable_data'] = $lesson_data;
13255 }
13256 } elseif ( 'learnpress_user_enrolled_in_course' === $trigger ) {
13257 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
13258 if ( $course_id > 0 ) {
13259
13260 $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 ) );
13261 } else {
13262
13263 $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" );
13264 }
13265
13266 if ( ! empty( $course ) ) {
13267 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
13268 $context['response_type'] = 'live';
13269 $context['pluggable_data'] = $course_data;
13270
13271 }
13272 }
13273
13274 return $context;
13275 }
13276
13277 /**
13278 * Get Woocommerce Memberships Plan List.
13279 *
13280 * @param array $data data.
13281 *
13282 * @return array|void
13283 */
13284 public function search_wc_membership_plan_list( $data ) {
13285 global $wpdb;
13286
13287 $page = $data['page'];
13288 $limit = Utilities::get_search_page_limit();
13289 $offset = $limit * ( $page - 1 );
13290
13291 $args = [
13292 'post_type' => 'wc_membership_plan',
13293 'posts_per_page' => $limit,
13294 'offset' => $offset,
13295 'orderby' => 'title',
13296 'order' => 'ASC',
13297 'post_status' => 'publish',
13298 'fields' => 'ids',
13299 ];
13300 $loop = new WP_Query( $args );
13301
13302 $plans = (array) $loop->posts;
13303 $plans_count = wp_count_posts( 'wc_membership_plan' )->publish;
13304
13305 $options = [];
13306 if ( ! empty( $plans ) ) {
13307 if ( is_array( $plans ) ) {
13308 foreach ( $plans as $plan_id ) {
13309 $options[] = [
13310 'label' => get_the_title( $plan_id ),
13311 'value' => $plan_id,
13312 ];
13313 }
13314 }
13315 }
13316
13317 return [
13318 'options' => $options,
13319 'hasMore' => $plans_count > $limit && $plans_count > $offset,
13320 ];
13321 }
13322
13323 /**
13324 * Get BuddyPress Private group.
13325 *
13326 * @param array $data data.
13327 *
13328 * @return array|void
13329 */
13330 public function search_bp_private_group_list( $data ) {
13331 global $wpdb;
13332
13333 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'private'" );
13334
13335 $options = [];
13336 if ( $groups ) {
13337 foreach ( $groups as $group ) {
13338 $options[] = [
13339 'label' => $group->name,
13340 'value' => $group->id,
13341 ];
13342 }
13343 }
13344
13345 return [
13346 'options' => $options,
13347 'hasMore' => false,
13348 ];
13349 }
13350
13351 /**
13352 * Get BuddyPress Public group.
13353 *
13354 * @param array $data data.
13355 *
13356 * @return array|void
13357 */
13358 public function search_bp_public_group_list( $data ) {
13359 global $wpdb;
13360
13361 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'public'" );
13362
13363 $options = [];
13364 if ( $groups ) {
13365 foreach ( $groups as $group ) {
13366 $options[] = [
13367 'label' => $group->name,
13368 'value' => $group->id,
13369 ];
13370 }
13371 }
13372
13373 return [
13374 'options' => $options,
13375 'hasMore' => false,
13376 ];
13377 }
13378
13379 /**
13380 * Get BuddyPress group.
13381 *
13382 * @param array $data data.
13383 *
13384 * @return array|void
13385 */
13386 public function search_bp_group_list( $data ) {
13387 global $wpdb;
13388
13389 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
13390
13391 $options = [];
13392 if ( $groups ) {
13393 foreach ( $groups as $group ) {
13394 $options[] = [
13395 'label' => $group->name,
13396 'value' => $group->id,
13397 ];
13398 }
13399 }
13400
13401 return [
13402 'options' => $options,
13403 'hasMore' => false,
13404 ];
13405 }
13406
13407 /**
13408 * Get BuddyPress field.
13409 *
13410 * @param array $data data.
13411 *
13412 * @return array|void
13413 */
13414 public function search_bp_field_list( $data ) {
13415 global $wpdb;
13416
13417 $base_group_id = 1;
13418 if ( function_exists( 'bp_xprofile_base_group_id' ) ) {
13419 $base_group_id = bp_xprofile_base_group_id();
13420 }
13421
13422 $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 ) );
13423
13424 $options = [];
13425 if ( ! empty( $xprofile_fields ) ) {
13426 foreach ( $xprofile_fields as $xprofile_field ) {
13427 $options[] = [
13428 'label' => $xprofile_field->name,
13429 'value' => $xprofile_field->id,
13430 ];
13431 }
13432 }
13433
13434 return [
13435 'options' => $options,
13436 'hasMore' => false,
13437 ];
13438 }
13439
13440 /**
13441 * Get BuddyPress member type.
13442 *
13443 * @param array $data data.
13444 *
13445 * @return array|void
13446 */
13447 public function search_bp_member_type_list( $data ) {
13448 $options = [];
13449 if ( function_exists( 'bp_get_member_types' ) ) {
13450 $types = bp_get_member_types( [] );
13451 if ( $types ) {
13452 foreach ( $types as $key => $type ) {
13453 $options[] = [
13454 'label' => $type,
13455 'value' => $key,
13456 ];
13457 }
13458 }
13459 }
13460
13461 return [
13462 'options' => $options,
13463 'hasMore' => false,
13464 ];
13465 }
13466
13467 /**
13468 * Get last data for WP All Import.
13469 *
13470 * @param array $data data.
13471 * @return mixed
13472 */
13473 public function search_wp_all_import_last_data( $data ) {
13474 global $wpdb;
13475 $post_type = $data['filter']['post_type']['value'];
13476 $trigger = $data['search_term'];
13477
13478 if ( 'wp_all_import_post_type_imported' === $trigger ) {
13479 if ( -1 == $post_type ) {
13480 $imports = $wpdb->get_row( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts ORDER BY id DESC LIMIT 1", ARRAY_A );
13481 $posts[0] = $imports['post_id'];
13482 } else {
13483 $imports = $wpdb->get_results( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts", ARRAY_A );
13484 $imports = array_column( $imports, 'post_id' );
13485 $args = [
13486 'posts_per_page' => 1,
13487 'post_type' => $post_type,
13488 'post__in' => $imports,
13489 ];
13490 $posts = get_posts( $args );
13491 }
13492 } elseif ( 'wp_all_import_completed' === $trigger ) {
13493 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 0 ORDER BY id DESC LIMIT 1", ARRAY_A );
13494 } elseif ( 'wp_all_import_failed' === $trigger ) {
13495 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 1 ORDER BY id DESC LIMIT 1", ARRAY_A );
13496 }
13497
13498 if ( 'wp_all_import_post_type_imported' === $trigger && empty( $imports ) ) {
13499 $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 );
13500 return $context;
13501 } elseif ( empty( $imports ) ) {
13502 $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 );
13503 return $context;
13504 }
13505
13506 $context['response_type'] = 'live';
13507 if ( ! empty( $posts ) ) {
13508 $context['pluggable_data'] = WordPress::get_post_context( $posts[0] );
13509 } else {
13510 $context['pluggable_data'] = $imports;
13511 }
13512
13513 return $context;
13514 }
13515
13516 /**
13517 * Get Wp Simple Pay Forms.
13518 *
13519 * @param array $data data.
13520 *
13521 * @return array
13522 */
13523 public function search_wp_simple_pay_forms( $data ) {
13524
13525 $page = $data['page'];
13526 $limit = Utilities::get_search_page_limit();
13527 $offset = $limit * ( $page - 1 );
13528
13529 $forms = get_posts(
13530 [
13531 'post_type' => 'simple-pay',
13532 'posts_per_page' => $limit,
13533 'offset' => $offset,
13534 'fields' => 'ids',
13535 ]
13536 );
13537
13538 $forms_count = wp_count_posts( 'simple-pay' )->publish;
13539
13540 $options = [];
13541
13542 if ( ! empty( $forms ) ) {
13543 foreach ( $forms as $form_id ) {
13544 if ( function_exists( 'simpay_get_form' ) ) {
13545 $form = simpay_get_form( $form_id );
13546 $options[] = [
13547 'label' => null !== get_the_title( $form_id ) ? $form->company_name : get_the_title( $form_id ),
13548 'value' => $form_id,
13549 ];
13550 }
13551 }
13552 }
13553
13554 return [
13555 'options' => $options,
13556 'hasMore' => $forms_count > $limit && $forms_count > $offset,
13557 ];
13558 }
13559
13560 /**
13561 * Get Post list as per post type for metabox.
13562 *
13563 * @param array $data data.
13564 *
13565 * @return array
13566 */
13567 public function search_mb_posts_list( $data ) {
13568
13569 $page = $data['page'];
13570 $limit = Utilities::get_search_page_limit();
13571 $offset = $limit * ( $page - 1 );
13572
13573 $posts = get_posts(
13574 [
13575 'post_type' => $data['dynamic'],
13576 'posts_per_page' => $limit,
13577 'offset' => $offset,
13578 'fields' => 'ids',
13579 ]
13580 );
13581
13582 $all_posts = get_posts(
13583 [
13584 'post_type' => $data['dynamic'],
13585 'posts_per_page' => -1,
13586 'fields' => 'ids',
13587 ]
13588 );
13589
13590 $posts_count = count( $all_posts );
13591
13592 $options = [];
13593
13594 if ( ! empty( $posts ) ) {
13595 foreach ( $posts as $post ) {
13596 $title = html_entity_decode( get_the_title( $post ), ENT_QUOTES, 'UTF-8' );
13597 $options[] = [
13598 'label' => $title,
13599 'value' => $post,
13600 ];
13601 }
13602 }
13603
13604 return [
13605 'options' => $options,
13606 'hasMore' => $posts_count > $limit && $posts_count > $offset,
13607 ];
13608 }
13609
13610 /**
13611 * Get Metabox Custom box in Post list.
13612 *
13613 * @param array $data data.
13614 *
13615 * @return array
13616 */
13617 public function search_mb_field_list( $data ) {
13618
13619 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
13620 return [];
13621 }
13622
13623 $options = [];
13624
13625 $metabox_fields = (array) rwmb_get_object_fields( $data['dynamic'] );
13626
13627 foreach ( $metabox_fields as $metabox_field ) {
13628
13629 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
13630
13631 $options[] = [
13632 'label' => $metabox_field['name'],
13633 'value' => $metabox_field['id'],
13634 ];
13635
13636 }
13637 }
13638
13639 return [
13640 'options' => $options,
13641 'hasMore' => false,
13642 ];
13643 }
13644
13645 /**
13646 * Get Metabox Custom box user list.
13647 *
13648 * @param array $data data.
13649 *
13650 * @return array
13651 */
13652 public function search_mb_user_field_list( $data ) {
13653
13654 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
13655 return [];
13656 }
13657
13658 $options = [];
13659
13660 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
13661
13662 foreach ( $metabox_fields as $metabox_field ) {
13663
13664 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
13665
13666 $options[] = [
13667 'label' => $metabox_field['name'],
13668 'value' => $metabox_field['id'],
13669 ];
13670
13671 }
13672 }
13673
13674 return [
13675 'options' => $options,
13676 'hasMore' => false,
13677 ];
13678 }
13679
13680 /**
13681 * Search Last Updated Field Data for MetaBox.
13682 *
13683 * @param array $data data.
13684 * @return array
13685 */
13686 public function search_meta_box_field_data( $data ) {
13687 global $wpdb;
13688
13689 $context = [];
13690
13691 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
13692
13693 $post_type = $data['filter']['wp_post_type']['value'];
13694 $post = $data['filter']['wp_post']['value'];
13695
13696 if ( -1 === $field ) {
13697 if ( function_exists( 'rwmb_get_object_fields' ) ) {
13698 $metaboxes = rwmb_get_object_fields( $post_type );
13699
13700 if ( ! empty( $metaboxes ) ) {
13701 $random_key = array_rand( $metaboxes );
13702 $field = $random_key;
13703 } else {
13704 $result = '';
13705 }
13706 }
13707 } else {
13708 $field = $data['filter']['field_id']['value'];
13709 }
13710
13711 if ( function_exists( 'rwmb_meta' ) ) {
13712 $result = rwmb_meta( $field, '', $post );
13713 }
13714
13715 $response = [];
13716 if ( ! empty( $result ) ) {
13717 $response['pluggable_data'] = array_merge( [ $field => $result ], WordPress::get_post_context( $post ) );
13718 $response['response_type'] = 'live';
13719 } else {
13720 $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 );
13721 }
13722
13723 return $response;
13724 }
13725
13726 /**
13727 * Search Last Updated User Field Data MetaBox.
13728 *
13729 * @param array $data data.
13730 * @return array
13731 */
13732 public function search_user_meta_box_field_data( $data ) {
13733 global $wpdb;
13734
13735 $context = [];
13736
13737 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
13738
13739 if ( -1 === $field ) {
13740 if ( function_exists( 'rwmb_get_object_fields' ) ) {
13741 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
13742
13743 if ( ! empty( $metabox_fields ) ) {
13744 $random_key = array_rand( $metabox_fields );
13745 $field = $random_key;
13746 } else {
13747 $result = '';
13748 }
13749 }
13750 } else {
13751 $field = $data['filter']['field_id']['value'];
13752 }
13753
13754 $users = get_users(
13755 [
13756 'fields' => 'ID',
13757 'meta_key' => $field,
13758 ]
13759 );
13760
13761 if ( ! empty( $users ) ) {
13762 $user_random_key = array_rand( $users );
13763 $user_id = $user_random_key;
13764 if ( function_exists( 'rwmb_get_value' ) ) {
13765 $result = rwmb_get_value( $field, [ 'object_type' => 'user' ], $users[ $user_id ] );
13766 }
13767
13768 $response = [];
13769 if ( ! empty( $result ) ) {
13770 $context = [
13771 'field_id' => $field,
13772 $field => $result,
13773 'user' => WordPress::get_user_context( $users[ $user_id ] ),
13774 ];
13775 $response['pluggable_data'] = $context;
13776 $response['response_type'] = 'live';
13777 } else {
13778 $response = json_decode(
13779 '{
13780 "response_type": "sample",
13781 "pluggable_data": {
13782 "field_id": "gender",
13783 "user": {
13784 "wp_user_id": 114,
13785 "user_login": "test",
13786 "display_name": "test",
13787 "user_firstname": "test",
13788 "user_lastname": "test",
13789 "user_email": "test@test.com",
13790 "user_role": [ "subscriber" ]
13791 }
13792 }
13793 }',
13794 true
13795 );
13796 }
13797 } else {
13798 $response = json_decode(
13799 '{
13800 "response_type": "sample",
13801 "pluggable_data": {
13802 "field_id": "gender",
13803 "user": {
13804 "wp_user_id": 114,
13805 "user_login": "test",
13806 "display_name": "test",
13807 "user_firstname": "test",
13808 "user_lastname": "test",
13809 "user_email": "test@test.com",
13810 "user_role": [ "subscriber" ]
13811 }
13812 }
13813 }',
13814 true
13815 );
13816 }
13817
13818 return $response;
13819 }
13820
13821 /**
13822 * Search forms of Pie Forms.
13823 *
13824 * @param array $data data.
13825 * @return array
13826 */
13827 public function search_wp_polls_list( $data ) {
13828 global $wpdb;
13829 $options = [];
13830
13831 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsq' ) ) ) {
13832
13833 $results = $wpdb->get_results( 'SELECT pollq_id, pollq_question FROM ' . $wpdb->prefix . 'pollsq WHERE pollq_active = 1' );
13834
13835 if ( $results ) {
13836 foreach ( $results as $result ) {
13837 $options[] = [
13838 'label' => $result->pollq_question,
13839 'value' => $result->pollq_id,
13840 ];
13841 }
13842 }
13843 }
13844
13845 return [
13846 'options' => $options,
13847 'hasMore' => false,
13848 ];
13849 }
13850
13851 /**
13852 * Search answers of WP-Polls questions.
13853 *
13854 * @param array $data data.
13855 * @return array
13856 */
13857 public function search_wp_polls_answers( $data ) {
13858 global $wpdb;
13859
13860 $options = [];
13861 $poll_id = $data['dynamic'];
13862
13863 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsa' ) ) ) {
13864
13865 if ( '-1' !== $poll_id ) {
13866 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa WHERE polla_qid = %d', $poll_id ) );
13867 } else {
13868 $results = $wpdb->get_results( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa' );
13869 }
13870
13871 if ( $results ) {
13872 foreach ( $results as $result ) {
13873 $options[] = [
13874 'label' => $result->polla_answers,
13875 'value' => $result->polla_aid,
13876 ];
13877 }
13878 }
13879 }
13880
13881 return [
13882 'options' => $options,
13883 'hasMore' => false,
13884 ];
13885 }
13886
13887 /**
13888 * Get last data for trigger.
13889 *
13890 * @param array $data data.
13891 * @return array
13892 */
13893 public function search_wp_polls_triggers_last_data( $data ) {
13894 global $wpdb;
13895
13896 $context = [];
13897 $context['response_type'] = 'sample';
13898
13899 $poll = [
13900 'poll_id' => 1,
13901 'question' => 'Which skills are you interested to learn?',
13902 'answers' => 'Web Development, Graphic Designing, Content Writing, Digital Marketing',
13903 'start_date' => '2023-08-29 17:19:13',
13904 'end_date' => 'Not set',
13905 'selected_answers' => 'Content Writing, Web Development',
13906 'selected_answer_id' => 2,
13907 ];
13908
13909 $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" );
13910
13911 if ( ! empty( $poll_data ) ) {
13912 $poll = WpPolls::get_poll_context( (string) $poll_data->answer_id, (int) $poll_data->poll_id );
13913 $poll['selected_answer_id'] = (int) $poll_data->answer_id;
13914
13915 $context['response_type'] = 'live';
13916 }
13917
13918 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
13919
13920 if ( 'poll_submitted' === $term ) {
13921 unset( $poll['selected_answer_id'] );
13922 }
13923
13924 $context['pluggable_data'] = $poll;
13925
13926 return $context;
13927 }
13928
13929 /**
13930 * Get ACF Custom fields list.
13931 *
13932 * @param array $data data.
13933 *
13934 * @return array
13935 */
13936 public function search_acf_post_field_list( $data ) {
13937
13938 $post_id = $data['dynamic']['wp_post'];
13939 $selected_post_type = $data['dynamic']['wp_post_type'];
13940
13941 if ( -1 === $post_id ) {
13942 $args = [
13943 'numberposts' => 1,
13944 'fields' => 'ids',
13945 'orderby' => 'rand',
13946 'post_type' => $selected_post_type,
13947 ];
13948 $posts = get_posts( $args );
13949 $post_id = $posts[0];
13950 }
13951
13952 $args = [
13953 'post_id' => $post_id,
13954 ];
13955 if ( ! is_numeric( $post_id ) ) {
13956 $args = [
13957 'post_type' => $post_id,
13958 ];
13959 }
13960 $options = [];
13961 if ( function_exists( 'acf_get_field_groups' ) ) {
13962 $field_groups_collection = acf_get_field_groups( $args );
13963 foreach ( $field_groups_collection as $field_group ) {
13964 if ( function_exists( 'acf_get_fields' ) ) {
13965 $field_groups[] = acf_get_fields( $field_group['key'] );
13966 }
13967 }
13968
13969 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
13970 foreach ( $field_groups as $field_groups ) {
13971 foreach ( $field_groups as $field_group ) {
13972 $options[] = [
13973 'value' => $field_group['name'],
13974 'label' => ! empty( $field_group['label'] ) ? $field_group['label'] : $field_group['name'],
13975 ];
13976 }
13977 }
13978 }
13979 }
13980
13981 return [
13982 'options' => $options,
13983 'hasMore' => false,
13984 ];
13985 }
13986
13987 /**
13988 * Get ACF Custom fields list.
13989 *
13990 * @param array $data data.
13991 *
13992 * @return array
13993 */
13994 public function search_acf_user_field_list( $data ) {
13995
13996 if ( ! function_exists( 'acf_get_fields' ) ) {
13997 return [];
13998 }
13999 if ( ! function_exists( 'acf_get_field_groups' ) ) {
14000 return [];
14001 }
14002 $groups_user_form = [];
14003 $options = [];
14004 if ( function_exists( 'acf_get_field_groups' ) ) {
14005 $field_groups = acf_get_field_groups();
14006 foreach ( $field_groups as $group ) {
14007 if ( ! empty( $group['location'] ) ) {
14008 foreach ( $group['location'] as $locations ) {
14009 foreach ( $locations as $location ) {
14010 if ( 'user_form' === $location['param'] || 'user_role' === $location['param'] || 'current_user' === $location['param'] || 'current_user_role' === $location['param'] ) {
14011 $groups_user_form[] = $group;
14012 }
14013 }
14014 }
14015 }
14016 }
14017
14018 if ( empty( $groups_user_form ) ) {
14019 return [];
14020 }
14021
14022 $key_values = array_map(
14023 function ( $item ) {
14024 return $item['key'];
14025 },
14026 $groups_user_form
14027 );
14028 $unique_keys = array_unique( $key_values );
14029 $unique_array = array_intersect_key( $groups_user_form, $unique_keys );
14030
14031 foreach ( $unique_array as $group ) {
14032 if ( function_exists( 'acf_get_fields' ) ) {
14033 $group_fields = acf_get_fields( $group['key'] );
14034 }
14035 if ( ! empty( $group_fields ) ) {
14036 foreach ( $group_fields as $field ) {
14037 $options[] = [
14038 'value' => $field['name'],
14039 'label' => $field['label'],
14040 ];
14041
14042 }
14043 }
14044 }
14045 }
14046
14047 return [
14048 'options' => $options,
14049 'hasMore' => false,
14050 ];
14051 }
14052
14053 /**
14054 * Get ACF Custom fields list.
14055 *
14056 * @param array $data data.
14057 *
14058 * @return array
14059 */
14060 public function search_acf_options_field_list( $data ) {
14061
14062 if ( ! function_exists( 'acf_get_fields' ) ) {
14063 return [];
14064 }
14065 if ( ! function_exists( 'acf_get_field_groups' ) ) {
14066 return [];
14067 }
14068 $groups_options_form = [];
14069 $options = [];
14070 if ( function_exists( 'acf_get_field_groups' ) ) {
14071 $field_groups = acf_get_field_groups();
14072 foreach ( $field_groups as $group ) {
14073 if ( ! empty( $group['location'] ) ) {
14074 foreach ( $group['location'] as $locations ) {
14075 foreach ( $locations as $location ) {
14076 if ( 'options_page' === $location['param'] ) {
14077 $groups_options_form[] = $group;
14078 }
14079 }
14080 }
14081 }
14082 }
14083 if ( empty( $groups_options_form ) ) {
14084 return [];
14085 }
14086 $key_values = array_map(
14087 function ( $item ) {
14088 return $item['key'];
14089 },
14090 $groups_options_form
14091 );
14092 $unique_keys = array_unique( $key_values );
14093 $unique_array = array_intersect_key( $groups_options_form, $unique_keys );
14094 foreach ( $unique_array as $group ) {
14095 if ( function_exists( 'acf_get_fields' ) ) {
14096 $group_fields = acf_get_fields( $group['key'] );
14097 }
14098 if ( ! empty( $group_fields ) ) {
14099 foreach ( $group_fields as $field ) {
14100 $options[] = [
14101 'value' => $field['name'],
14102 'label' => $field['label'],
14103 ];
14104
14105 }
14106 }
14107 }
14108 }
14109
14110 return [
14111 'options' => $options,
14112 'hasMore' => false,
14113 ];
14114 }
14115
14116 /**
14117 * Search Last Updated Field Data for ACF.
14118 *
14119 * @param array $data data.
14120 * @return array
14121 */
14122 public function search_acf_post_field_data( $data ) {
14123 $context = [];
14124
14125 $field = ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
14126
14127 $post_type = $data['filter']['wp_post_type']['value'];
14128 $post = $data['filter']['wp_post']['value'];
14129
14130 if ( -1 === $post ) {
14131 $args = [
14132 'numberposts' => 1,
14133 'fields' => 'ids',
14134 'orderby' => 'rand',
14135 'post_type' => $post_type,
14136 ];
14137 $posts = get_posts( $args );
14138 $post = $posts[0];
14139 }
14140 if ( -1 === $field ) {
14141 $args = [
14142 'post_id' => $post,
14143 ];
14144 if ( function_exists( 'acf_get_field_groups' ) ) {
14145 $field_groups_collection = acf_get_field_groups( $args );
14146 }
14147 if ( ! empty( $field_groups_collection ) ) {
14148 foreach ( $field_groups_collection as $field_group ) {
14149 if ( function_exists( 'acf_get_fields' ) ) {
14150 $field_groups[] = acf_get_fields( $field_group['key'] );
14151 }
14152 }
14153 }
14154 $fields = [];
14155 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
14156 foreach ( $field_groups as $field_groups ) {
14157 $fields[] = $field_groups;
14158 }
14159 }
14160 if ( ! empty( $fields ) ) {
14161 $random_key = array_rand( $fields[0] );
14162 $field_key = $fields[0][ $random_key ];
14163 $field = $field_key['name'];
14164 } else {
14165 $result = '';
14166 }
14167 } else {
14168 $field = $data['filter']['field_id']['value'];
14169 }
14170 if ( function_exists( ( 'get_field' ) ) ) {
14171 $result = get_field( $field, $post );
14172 }
14173
14174 $response = [];
14175 if ( ! empty( $result ) ) {
14176 $post_fields = [];
14177 if ( function_exists( 'get_fields' ) ) {
14178 $post_fields = get_fields( $post );
14179 }
14180 $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 ) ] );
14181 $response['response_type'] = 'live';
14182 } else {
14183 $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 );
14184 }
14185
14186 return $response;
14187 }
14188
14189 /**
14190 * Search Last Updated User Field Data ACF.
14191 *
14192 * @param array $data data.
14193 * @return array
14194 */
14195 public function search_acf_user_field_data( $data ) {
14196 global $wpdb;
14197
14198 $context = [];
14199
14200 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
14201
14202 if ( -1 === $field ) {
14203 $groups_user_form = [];
14204 if ( function_exists( 'acf_get_field_groups' ) ) {
14205 $field_groups = acf_get_field_groups();
14206 }
14207 if ( ! empty( $field_groups ) ) {
14208 foreach ( $field_groups as $group ) {
14209 if ( ! empty( $group['location'] ) ) {
14210 foreach ( $group['location'] as $locations ) {
14211 foreach ( $locations as $location ) {
14212 if ( 'user_form' === $location['param'] || 'user_role' === $location['param'] || 'current_user' === $location['param'] || 'current_user_role' === $location['param'] ) {
14213 $groups_user_form[] = $group;
14214 }
14215 }
14216 }
14217 }
14218 }
14219 $field_groups = $groups_user_form;
14220 }
14221 if ( empty( $field_groups ) ) {
14222 $result = '';
14223 }
14224 $fields = [];
14225 if ( ! empty( $field_groups ) ) {
14226 foreach ( $field_groups as $group ) {
14227 if ( function_exists( 'acf_get_fields' ) ) {
14228 $group_fields = acf_get_fields( $group['key'] );
14229 }
14230 if ( ! empty( $group_fields ) ) {
14231 foreach ( $group_fields as $field ) {
14232 $fields[] = $group_fields;
14233 }
14234 }
14235 }
14236 }
14237 if ( ! empty( $fields ) ) {
14238 $random_key = array_rand( $fields );
14239 $field = $random_key;
14240 } else {
14241 $result = '';
14242 }
14243 } else {
14244 $field = $data['filter']['field_id']['value'];
14245 }
14246 $users = get_users(
14247 [
14248 'fields' => 'ID',
14249 'meta_key' => $field,
14250 ]
14251 );
14252
14253 if ( ! empty( $users ) ) {
14254 $user_random_key = array_rand( $users );
14255 $user_id = $user_random_key;
14256 if ( function_exists( 'get_field' ) ) {
14257 $result = get_field( $field, 'user_' . $users[ $user_id ] );
14258 }
14259 $response = [];
14260 if ( ! empty( $result ) ) {
14261 $context = [
14262 'field_id' => $field,
14263 $field => $result,
14264 'user' => WordPress::get_user_context( $users[ $user_id ] ),
14265 ];
14266 $response['pluggable_data'] = $context;
14267 $response['response_type'] = 'live';
14268 } else {
14269 $response = json_decode(
14270 '{
14271 "response_type": "sample",
14272 "pluggable_data": {
14273 "field_id": "gender",
14274 "user": {
14275 "wp_user_id": 114,
14276 "user_login": "test",
14277 "display_name": "test",
14278 "user_firstname": "test",
14279 "user_lastname": "test",
14280 "user_email": "test@test.com",
14281 "user_role": [ "subscriber" ]
14282 }
14283 }
14284 }',
14285 true
14286 );
14287 }
14288 } else {
14289 $response = json_decode(
14290 '{
14291 "response_type": "sample",
14292 "pluggable_data": {
14293 "field_id": "gender",
14294 "user": {
14295 "wp_user_id": 114,
14296 "user_login": "test",
14297 "display_name": "test",
14298 "user_firstname": "test",
14299 "user_lastname": "test",
14300 "user_email": "test@test.com",
14301 "user_role": [ "subscriber" ]
14302 }
14303 }
14304 }',
14305 true
14306 );
14307 }
14308
14309 return $response;
14310 }
14311
14312 /**
14313 * Search Last Updated Options Field Data ACF.
14314 *
14315 * @param array $data data.
14316 * @return array
14317 */
14318 public function search_acf_options_field_data( $data ) {
14319 global $wpdb;
14320 $context = [];
14321 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
14322
14323 if ( -1 === $field ) {
14324 $groups_options_form = [];
14325 if ( function_exists( 'acf_get_field_groups' ) ) {
14326 $field_groups = acf_get_field_groups();
14327 }
14328 if ( ! empty( $field_groups ) ) {
14329 foreach ( $field_groups as $group ) {
14330 if ( ! empty( $group['location'] ) ) {
14331 foreach ( $group['location'] as $locations ) {
14332 foreach ( $locations as $location ) {
14333 if ( 'options_page' === $location['param'] ) {
14334 $groups_options_form[] = $group;
14335 }
14336 }
14337 }
14338 }
14339 }
14340 }
14341 if ( empty( $groups_options_form ) ) {
14342 $result = '';
14343 }
14344 $key_values = array_map(
14345 function ( $item ) {
14346 return $item['key'];
14347 },
14348 $groups_options_form
14349 );
14350 $unique_keys = array_unique( $key_values );
14351 $unique_array = array_intersect_key( $groups_options_form, $unique_keys );
14352 $fields = [];
14353 if ( ! empty( $unique_array ) ) {
14354 foreach ( $unique_array as $group ) {
14355 if ( function_exists( 'acf_get_fields' ) ) {
14356 $group_fields = acf_get_fields( $group['key'] );
14357 }
14358 if ( ! empty( $group_fields ) ) {
14359 foreach ( $group_fields as $field ) {
14360 $fields[] = $group_fields;
14361 }
14362 }
14363 }
14364 }
14365 if ( ! empty( $fields ) ) {
14366 $random_key = array_rand( $fields );
14367 $field = $random_key;
14368 } else {
14369 $result = '';
14370 }
14371 } else {
14372 $field = $data['filter']['field_id']['value'];
14373 }
14374 if ( function_exists( 'get_field' ) ) {
14375 $option_value = get_field( $field, 'option' );
14376 }
14377 if ( ! empty( $option_value ) ) {
14378 if ( function_exists( 'acf_get_field' ) ) {
14379 $options_fields = acf_get_field( $field );
14380 if ( function_exists( 'acf_maybe_get' ) ) {
14381 $options_page = acf_maybe_get( $options_fields, 'parent' );
14382 }
14383 }
14384 $context = [
14385 'field_id' => $field,
14386 $field => $option_value,
14387 ];
14388 $response['pluggable_data'] = $context;
14389 $response['response_type'] = 'live';
14390 } else {
14391 $response = json_decode(
14392 '{
14393 "response_type": "sample",
14394 "pluggable_data": {
14395 "field_id": "optionpage",
14396 "optionpage": "newoption"
14397 }
14398 }',
14399 true
14400 );
14401 }
14402 return $response;
14403 }
14404
14405 /**
14406 * Get WP Fusion Tags list.
14407 *
14408 * @param array $data data.
14409 *
14410 * @return array
14411 */
14412 public function search_wp_fusion_tag_list( $data ) {
14413
14414 if ( ! function_exists( 'wp_fusion' ) ) {
14415 return [];
14416 }
14417
14418 $options = [];
14419 $tags = wp_fusion()->settings->get( 'available_tags' );
14420
14421 if ( $tags ) {
14422 foreach ( $tags as $t_id => $tag ) {
14423 if ( is_array( $tag ) && isset( $tag['label'] ) ) {
14424 $options[] = [
14425 'value' => $t_id,
14426 'label' => $tag['label'],
14427 ];
14428 } else {
14429 $options[] = [
14430 'value' => $t_id,
14431 'label' => $tag,
14432 ];
14433 }
14434 }
14435 }
14436
14437 return [
14438 'options' => $options,
14439 'hasMore' => false,
14440 ];
14441 }
14442
14443 /**
14444 * Get list of events for Modern Events Calendar.
14445 *
14446 * @param array $data data.
14447 * @return array
14448 */
14449 public function search_mec_events_list( $data ) {
14450 $page = $data['page'];
14451 $limit = Utilities::get_search_page_limit();
14452 $offset = $limit * ( $page - 1 );
14453
14454 $args = [
14455 'post_type' => 'mec-events',
14456 'post_status' => [ 'publish', 'private' ],
14457 'posts_per_page' => -1,
14458 ];
14459 $loop = new WP_Query( $args );
14460 $events_count = count( $loop->posts );
14461
14462 $args = [
14463 'post_type' => 'mec-events',
14464 'posts_per_page' => $limit,
14465 'offset' => $offset,
14466 'orderby' => 'title',
14467 'order' => 'ASC',
14468 'post_status' => [ 'publish', 'private' ],
14469 ];
14470
14471 $loop = new WP_Query( $args );
14472 $events = $loop->posts;
14473
14474 $options = [];
14475 if ( ! empty( $events ) ) {
14476 foreach ( $events as $event ) {
14477 if ( isset( $event->ID ) ) {
14478 $options[] = [
14479 'label' => get_the_title( $event ),
14480 'value' => $event->ID,
14481 ];
14482 }
14483 }
14484 }
14485
14486 return [
14487 'options' => $options,
14488 'hasMore' => $events_count > $limit && $events_count > $offset,
14489 ];
14490 }
14491
14492 /**
14493 * Search tickets of MEC events.
14494 *
14495 * @param array $data data.
14496 * @return array
14497 */
14498 public function search_mec_event_tickets( $data ) {
14499 $options = [];
14500 $event_id = $data['dynamic'];
14501
14502 $event_tickets = get_post_meta( $event_id, 'mec_tickets', true );
14503
14504 if ( ! empty( $event_tickets ) && is_array( $event_tickets ) ) {
14505 foreach ( $event_tickets as $ticket_id => $event_ticket ) {
14506 if ( isset( $event_ticket['name'] ) ) {
14507 $options[] = [
14508 'label' => $event_ticket['name'],
14509 'value' => $ticket_id,
14510 ];
14511 }
14512 }
14513 }
14514
14515 return [
14516 'options' => $options,
14517 'hasMore' => false,
14518 ];
14519 }
14520
14521 /**
14522 * Get last data for trigger.
14523 *
14524 * @param array $data data.
14525 * @return array
14526 */
14527 public function search_mec_triggers_last_data( $data ) {
14528 global $wpdb;
14529
14530 $context = [];
14531 $context['response_type'] = 'sample';
14532
14533 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
14534
14535 if ( 'new_event' === $term ) {
14536 $post_id = $wpdb->get_var( "SELECT post_id FROM {$wpdb->prefix}mec_events ORDER BY id DESC LIMIT 1" );
14537
14538 $live_data = [
14539 'post_id' => $post_id,
14540 'event' => get_post_meta( $post_id ),
14541 'post' => get_post( $post_id ),
14542 ];
14543
14544 if ( ! empty( $post_id ) ) {
14545 $context['pluggable_data'] = $live_data;
14546 $context['response_type'] = 'live';
14547 } else {
14548 $context['pluggable_data'] = [
14549 'post_id' => 272,
14550 'event' => [
14551 'mec_color' => [ 'e14d43' ],
14552 '_thumbnail_id' => [ 229 ],
14553 'mec_location_id' => [ 49 ],
14554 'mec_dont_show_map' => [ 0 ],
14555 'mec_organizer_id' => [ 53 ],
14556 'mec_start_date' => [ '2025-04-01' ],
14557 'mec_start_time_hour' => [ 8 ],
14558 'mec_start_time_minutes' => [ 0 ],
14559 'mec_start_time_ampm' => [ 'AM' ],
14560 'mec_start_day_seconds' => [ 32400 ],
14561 'mec_start_datetime' => [ '2025-04-01 08:00 AM' ],
14562 'mec_end_date' => [ '2025-04-01' ],
14563 'mec_date' => [ 'a:3:{s:5:"start";a:4:{s:4:"date";s:10:"2025-03-29";s:4:"hour";s:1:"8";s:7:"minutes";s:1:"0";s:4:"ampm";s:2:"AM";}s:3:"end";a:4:{s:4:"date";s:10:"2025-03-29";s:4:"hour";s:1:"6";s:7:"minutes";s:1:"0";s:4:"ampm";s:2:"PM";}s:6:"allday";s:1:"1";}' ],
14564 'mec_end_time_hour' => [ 6 ],
14565 'mec_end_time_minutes' => [ 0 ],
14566 'mec_end_time_ampm' => [ 'PM' ],
14567 'mec_end_day_seconds' => [ 68400 ],
14568 'mec_end_datetime' => [ '2025-04-01 06:00 PM' ],
14569 'mec_allday' => [ 1 ],
14570 'mec_fees' => [ 'a:0:{}' ],
14571 'mec_op' => [ 'a:0:{}' ],
14572 'mec_repeat' => [ 'a:0:{}' ],
14573 'mec_booking' => [ 'a:0:{}' ],
14574 'mec_comment' => [ '' ],
14575 'mec_in_days' => [ '' ],
14576 'mec_tickets' => [ 'a:0:{}' ],
14577 'mec_reg_fields' => [ 'a:0:{}' ],
14578 'mec_hourly_schedules' => [ 'a:0:{}' ],
14579 'mec_ticket_variations' => [ 'a:0:{}' ],
14580 'mec_hide_time' => [ 0 ],
14581 'mec_hide_end_time' => [ 0 ],
14582 'mec_repeat_status' => [ 0 ],
14583 'mec_fees_global_inheritance' => [ 1 ],
14584 'mec_ticket_variations_global_inheritance' => [ 1 ],
14585 'mec_reg_fields_global_inheritance' => [ 1 ],
14586 'mec_event_date_submit' => [ '20250331042811' ],
14587 'mec_new_event_notif_sent' => [ 1 ],
14588 ],
14589 'post' => [
14590 'ID' => 272,
14591 'post_author' => 1,
14592 'post_date' => '2025-03-31 04:28:09',
14593 'post_date_gmt' => '2025-03-31 04:28:09',
14594 'post_content' => '<p>Sample Event</p>',
14595 'post_title' => 'Sample Event',
14596 'post_status' => 'publish',
14597 'comment_status' => 'open',
14598 'ping_status' => 'closed',
14599 'post_name' => 'sample-event',
14600 'guid' => 'http://example.local/events/sample-event/',
14601 'post_type' => 'mec-events',
14602 'comment_count' => 0,
14603 ],
14604 ];
14605 $context['response_type'] = 'sample';
14606 }
14607
14608 return $context;
14609 }
14610
14611 $where = '';
14612 if ( 'cancelled' === $term ) {
14613 $where = 'WHERE verified = -1';
14614 } elseif ( 'confirmed' === $term ) {
14615 $where = 'WHERE confirmed = 1';
14616 } elseif ( 'pending' === $term ) {
14617 $where = 'WHERE confirmed = 0';
14618 }
14619
14620 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
14621
14622 if ( -1 !== $event_id ) {
14623 if ( ! empty( $where ) ) {
14624 $where .= ' AND event_id = ' . $event_id;
14625 } else {
14626 $where = 'WHERE event_id = ' . $event_id;
14627 }
14628 }
14629
14630 $event_data = $wpdb->get_row( "SELECT booking_id FROM {$wpdb->prefix}mec_bookings $where ORDER BY id DESC LIMIT 1" ); // @phpcs:ignore
14631
14632 if ( ! empty( $event_data ) ) {
14633 $event = ModernEventsCalendar::get_event_context( (int) $event_data->booking_id );
14634 $context['response_type'] = 'live';
14635 } else {
14636 $event = [
14637 'event_id' => 1,
14638 'title' => 'Sample Event',
14639 'description' => 'Description of the sample event.',
14640 'categories' => 'New, Sample',
14641 'start_date' => 'September 13, 2023',
14642 'start_time' => '8:00 AM',
14643 'end_date' => 'September 13, 2023',
14644 'end_time' => '11:00 AM',
14645 'location' => 'City Hall',
14646 'organizer' => 'John Doe',
14647 'cost' => '5000',
14648 'featured_image_id' => 1,
14649 'featured_image_url' => 'https://example.com/wp-content/uploads/2022/12/Screenshot_20221127_021332.png',
14650 'tickets' => [
14651 [
14652 'id' => 1,
14653 'name' => 'Silver',
14654 'description' => 'Standard seat with reasonable price.',
14655 'price' => '300',
14656 'price_label' => 'USD',
14657 'limit' => '20',
14658 ],
14659 [
14660 'id' => 2,
14661 'name' => 'Premium',
14662 'description' => 'VIP seat with high price.',
14663 'price' => '500',
14664 'price_label' => 'USD',
14665 'limit' => '10',
14666 ],
14667 ],
14668 'attendees' => [
14669 [
14670 'id' => 1,
14671 'email' => 'johndoe@test.com',
14672 'name' => 'John Doe',
14673 ],
14674 [
14675 'id' => 2,
14676 'email' => 'adamsmith@test.com',
14677 'name' => 'Adam Smith',
14678 ],
14679 ],
14680 'booking' => [
14681 'title' => 'johndoe@test.com - John Doe',
14682 'transaction_id' => 'RSH59404',
14683 'amount_payable' => '800',
14684 'price' => '800',
14685 'time' => '2023-09-07 06:40:32',
14686 'payment_gateway' => 'Manual Pay',
14687 'confirmation_status' => 'Pending',
14688 'verification_status' => 'Verified',
14689 'attendees_count' => 2,
14690 ],
14691 ];
14692 }
14693
14694 $context['pluggable_data'] = $event;
14695 return $context;
14696 }
14697
14698 /**
14699 * Get form list Contact Form 7.
14700 *
14701 * @param array $data data.
14702 *
14703 * @return array
14704 */
14705 public function search_contact_form7_list( $data ) {
14706
14707 $page = $data['page'];
14708 $limit = Utilities::get_search_page_limit();
14709 $offset = $limit * ( $page - 1 );
14710
14711 $posts = get_posts(
14712 [
14713 'post_type' => 'wpcf7_contact_form',
14714 'posts_per_page' => $limit,
14715 'offset' => $offset,
14716 ]
14717 );
14718
14719 $all_posts = get_posts(
14720 [
14721 'post_type' => 'wpcf7_contact_form',
14722 'posts_per_page' => -1,
14723 ]
14724 );
14725
14726 $posts_count = count( $all_posts );
14727
14728 $options = [];
14729
14730 if ( ! empty( $posts ) ) {
14731 foreach ( $posts as $post ) {
14732 $options[] = [
14733 'label' => get_the_title( $post->ID ),
14734 'value' => $post->ID,
14735 ];
14736 }
14737 }
14738
14739 return [
14740 'options' => $options,
14741 'hasMore' => $posts_count > $limit && $posts_count > $offset,
14742 ];
14743 }
14744
14745 /**
14746 * Get Thrive Leads form list
14747 *
14748 * @param array $data data.
14749 *
14750 * @return array
14751 */
14752 public function search_thrive_leads_forms_list( $data ) {
14753 $options = [];
14754
14755 $lg_ids = get_posts(
14756 [
14757 'post_type' => '_tcb_form_settings',
14758 'fields' => 'id=>parent',
14759 'posts_per_page' => -1,
14760 'post_status' => 'any',
14761 ]
14762 );
14763
14764 if ( function_exists( 'tve_leads_get_form_variations' ) ) {
14765 foreach ( $lg_ids as $lg_id => $lg_parent ) {
14766 $variations = tve_leads_get_form_variations( $lg_parent );
14767 foreach ( $variations as $variation ) {
14768 $options[] = [
14769 'label' => $variation['post_title'],
14770 'value' => $lg_parent,
14771 ];
14772 }
14773 }
14774 }
14775
14776 return [
14777 'options' => array_unique( $options ),
14778 'hasMore' => false,
14779 ];
14780 }
14781
14782 /**
14783 * Get Store Engine Products list
14784 *
14785 * @param array $data data.
14786 *
14787 * @return array
14788 */
14789 public function search_store_engine_products( $data ) {
14790 $options = [];
14791
14792 if ( ! class_exists( 'StoreEngine' ) ) {
14793 return $options;
14794 }
14795
14796 $products = get_posts(
14797 [
14798 'post_type' => 'storeengine_product',
14799 'post_status' => 'publish',
14800 'posts_per_page' => -1,
14801 'orderby' => 'title',
14802 'order' => 'ASC',
14803 ]
14804 );
14805
14806 foreach ( $products as $product ) {
14807 $current_price = get_post_meta( $product->ID, '_storeengine_product_price', true );
14808
14809 $price_display = '';
14810 if ( ! empty( $current_price ) && is_numeric( $current_price ) ) {
14811 $price_display = ' - $' . number_format( floatval( $current_price ), 2 );
14812 }
14813
14814 $id_display = ' (ID: ' . $product->ID . ')';
14815
14816 $options[] = [
14817 'label' => $product->post_title . $price_display . $id_display,
14818 'value' => $product->ID,
14819 ];
14820 }
14821
14822 return [
14823 'options' => $options,
14824 'hasMore' => false,
14825 ];
14826 }
14827
14828 /**
14829 * Get all countries list from StoreEngine
14830 *
14831 * @param array $data data.
14832 *
14833 * @return array
14834 */
14835 public function search_store_engine_countries( $data ) {
14836 $options = [];
14837
14838 if ( ! class_exists( 'StoreEngine\Classes\Countries' ) ) {
14839 return [
14840 'options' => $options,
14841 'hasMore' => false,
14842 ];
14843 }
14844
14845 $countries_obj = \StoreEngine\Classes\Countries::get_instance();
14846 $countries = $countries_obj->get_countries();
14847
14848 foreach ( $countries as $code => $name ) {
14849 $options[] = [
14850 'label' => $name . ' (' . $code . ')',
14851 'value' => $code,
14852 ];
14853 }
14854
14855 return [
14856 'options' => $options,
14857 'hasMore' => false,
14858 ];
14859 }
14860
14861 /**
14862 * Get Store Engine Last Data
14863 *
14864 * @param array $data data.
14865 *
14866 * @return array
14867 */
14868 public function search_se_triggers_last_data( $data ) {
14869 $context = [];
14870 global $wpdb;
14871
14872 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
14873
14874 if ( 'se_product_purchased' === $term ) {
14875 $product = $wpdb->get_row(
14876 $wpdb->prepare(
14877 "SELECT * FROM {$wpdb->prefix}posts
14878 WHERE post_type = %s
14879 AND post_status = %s
14880 ORDER BY ID DESC
14881 LIMIT 1",
14882 'storeengine_product',
14883 'publish'
14884 ),
14885 ARRAY_A
14886 );
14887
14888 if ( ! empty( $product ) ) {
14889 $product_meta = $wpdb->get_results(
14890 $wpdb->prepare(
14891 "SELECT meta_key, meta_value FROM {$wpdb->prefix}postmeta WHERE post_id = %d",
14892 $product['ID']
14893 ),
14894 ARRAY_A
14895 );
14896
14897 $custom_metas = [];
14898 foreach ( $product_meta as $meta ) {
14899 if ( strpos( $meta['meta_key'], '_storeengine_' ) === 0 ) {
14900 $clean_key = ltrim( $meta['meta_key'], '_' );
14901 $custom_metas[ $clean_key ] = maybe_unserialize( $meta['meta_value'] );
14902 }
14903 }
14904
14905 $table_exists = $wpdb->get_var(
14906 $wpdb->prepare(
14907 'SHOW TABLES LIKE %s',
14908 $wpdb->prefix . 'storeengine_orders'
14909 )
14910 );
14911
14912 $order = null;
14913 if ( $table_exists ) {
14914 $order = $wpdb->get_row(
14915 $wpdb->prepare(
14916 "SELECT * FROM {$wpdb->prefix}storeengine_orders
14917 ORDER BY id DESC
14918 LIMIT %d",
14919 1
14920 ),
14921 ARRAY_A
14922 );
14923 }
14924
14925 $customer_id = isset( $order['customer_id'] ) ? $order['customer_id'] : null;
14926 $user = null;
14927 if ( $customer_id ) {
14928 $user = get_userdata( $customer_id );
14929 } else {
14930 $customers = get_users(
14931 [
14932 'role__in' => [ 'storeengine_customer', 'customer' ],
14933 'number' => 1,
14934 'orderby' => 'ID',
14935 'order' => 'DESC',
14936 ]
14937 );
14938 if ( ! empty( $customers ) ) {
14939 $user = $customers[0];
14940 } else {
14941 $user = get_userdata( $product['post_author'] );
14942 }
14943 }
14944
14945 $product_data = [
14946 'id' => (int) $product['ID'],
14947 'guid' => $product['guid'],
14948 'post' => (int) $product['ID'],
14949 'filter' => 'raw',
14950 'pinged' => $product['pinged'],
14951 'to_ping' => $product['to_ping'],
14952 'order_id' => '',
14953 'permalink' => get_permalink( $product['ID'] ),
14954 'post_date' => $product['post_date'],
14955 'post_name' => $product['post_name'],
14956 'post_type' => $product['post_type'],
14957 'user_role' => $user ? $user->roles : [],
14958 'membership_details' => [
14959 'expiration_specific_date' => '',
14960 'expiration_fixed_date_duration' => 0,
14961 'is_enable_expiration' => 0,
14962 'content_protects_rules' => [],
14963 'membership_plan' => [
14964 'id' => null,
14965 'name' => '',
14966 'slug' => '',
14967 'status' => '',
14968 ],
14969 ],
14970 'menu_order' => (int) $product['menu_order'],
14971 'post_title' => $product['post_title'],
14972 'user_email' => $user ? $user->user_email : '',
14973 'user_login' => $user ? $user->user_login : '',
14974 'wp_user_id' => $user ? $user->ID : 0,
14975 'ping_status' => $product['ping_status'],
14976 'post_author' => (int) $product['post_author'],
14977 'post_parent' => (int) $product['post_parent'],
14978 'post_status' => $product['post_status'],
14979 'custom_metas' => $custom_metas,
14980 'display_name' => $user ? $user->display_name : '',
14981 'post_content' => $product['post_content'],
14982 'post_excerpt' => $product['post_excerpt'],
14983 'comment_count' => (int) $product['comment_count'],
14984 'post_date_gmt' => $product['post_date_gmt'],
14985 'post_modified' => $product['post_modified'],
14986 'post_password' => $product['post_password'],
14987 'user_lastname' => $user ? get_user_meta( $user->ID, 'last_name', true ) : '',
14988 'comment_status' => $product['comment_status'],
14989 'post_mime_type' => $product['post_mime_type'],
14990 'transaction_id' => '',
14991 'user_firstname' => $user ? get_user_meta( $user->ID, 'first_name', true ) : '',
14992 'user_registered' => $user ? $user->user_registered : '',
14993 'post_modified_gmt' => $product['post_modified_gmt'],
14994 'post_content_filtered' => $product['post_content_filtered'],
14995 ];
14996
14997 $integration = $wpdb->get_row(
14998 $wpdb->prepare(
14999 "SELECT integration_id, price_id FROM {$wpdb->prefix}storeengine_integrations
15000 WHERE product_id = %d AND provider = %s
15001 LIMIT 1",
15002 $product['ID'],
15003 'storeengine/membership-addon'
15004 )
15005 );
15006
15007 $membership_plan_id = isset( $integration->integration_id ) ? $integration->integration_id : null;
15008 if ( $membership_plan_id ) {
15009 $access_group = get_post( $membership_plan_id );
15010 if ( 'storeengine_groups' === $access_group && $access_group->post_type ) {
15011 $product_data['membership_details']['membership_plan'] = [
15012 'id' => (int) $membership_plan_id,
15013 'name' => $access_group->post_title,
15014 'slug' => $access_group->post_name,
15015 'status' => $access_group->post_status,
15016 ];
15017
15018 $expiration_data = get_post_meta( $membership_plan_id, '_storeengine_membership_expiration', true );
15019 if ( $expiration_data && is_array( $expiration_data ) ) {
15020 $product_data['membership_details']['expiration_specific_date'] = isset( $expiration_data['specific_date'] ) ? $expiration_data['specific_date'] : '';
15021 $product_data['membership_details']['expiration_fixed_date_duration'] = isset( $expiration_data['fixed_date_duration'] ) ? (int) $expiration_data['fixed_date_duration'] : 0;
15022 $product_data['membership_details']['is_enable_expiration'] = isset( $expiration_data['is_enable'] ) ? (int) $expiration_data['is_enable'] : 0;}
15023
15024 $content_protects = get_post_meta( $membership_plan_id, '_storeengine_membership_content_protect_types', true );
15025 if ( $content_protects && is_array( $content_protects ) ) {
15026 $product_data['membership_details']['content_protects_rules'] = $content_protects;
15027 }
15028 }
15029 }
15030
15031 if ( $order ) {
15032 $product_data['order_id'] = (int) $order['id'];
15033 $product_data['transaction_id'] = isset( $order['transaction_id'] ) ? $order['transaction_id'] : '';
15034 }
15035
15036 $context['pluggable_data'] = $product_data;
15037 $context['response_type'] = 'live';
15038
15039 } else {
15040 $sample_data = [
15041 'id' => 53,
15042 'guid' => 'http://suretriggers-wp.local/product/product-2/',
15043 'post' => 53,
15044 'filter' => 'raw',
15045 'pinged' => '',
15046 'to_ping' => '',
15047 'order_id' => 43,
15048 'permalink' => 'http://suretriggers-wp.local/product/product-2/',
15049 'post_date' => '2025-07-24 07:01:01',
15050 'post_name' => 'product-2',
15051 'post_type' => 'storeengine_product',
15052 'user_role' => [ 'storeengine_customer' ],
15053 'membership_details' => [
15054 'expiration_specific_date' => '',
15055 'expiration_fixed_date_duration' => 0,
15056 'is_enable_expiration' => 0,
15057 'content_protects_rules' => [],
15058 'membership_plan' => [
15059 'id' => null,
15060 'name' => '',
15061 'slug' => '',
15062 'status' => '',
15063 ],
15064 ],
15065 'menu_order' => 0,
15066 'post_title' => 'Product 2',
15067 'user_email' => 'mucool@gmail.com',
15068 'user_login' => 'mucool',
15069 'wp_user_id' => 12,
15070 'ping_status' => 'open',
15071 'post_author' => 1,
15072 'post_parent' => 0,
15073 'post_status' => 'publish',
15074 'custom_metas' => [
15075 'storeengine_product_hide' => '',
15076 'storeengine_product_type' => 'simple',
15077 'storeengine_product_shipping_type' => 'digital',
15078 'storeengine_product_physical_weight' => '',
15079 'storeengine_product_physical_weight_unit' => 'g',
15080 'storeengine_product_digital_auto_complete' => 1,
15081 'storeengine_product_integrated_plan_id' => 99,
15082 'storeengine_product_gallery_ids' => [],
15083 'storeengine_product_downloadable_files' => [
15084 [
15085 'id' => '9bd1194a-0406-46f3-aff3-dc539cdc791f',
15086 'name' => '',
15087 'file' => 'http://suretriggers-wp.local/wp-content/uploads/2025/07/bd0f98d1bede01622fe7f03a4f8fdc2155abafb4.jpg',
15088 'enabled' => 1,
15089 ],
15090 ],
15091 'storeengine_upsell_ids' => [],
15092 'storeengine_crosssell_ids' => [],
15093 ],
15094 'display_name' => 'mucool titirmare',
15095 'post_content' => '<p>sdfdsfsd</p>',
15096 'post_excerpt' => '',
15097 'comment_count' => 0,
15098 'post_date_gmt' => '2025-07-24 07:01:01',
15099 'post_modified' => '2025-07-24 08:14:01',
15100 'post_password' => '',
15101 'user_lastname' => 'titirmare',
15102 'comment_status' => 'open',
15103 'post_mime_type' => '',
15104 'transaction_id' => '',
15105 'user_firstname' => 'mucool',
15106 'user_registered' => '2025-07-24 08:16:48',
15107 'post_modified_gmt' => '2025-07-24 08:14:01',
15108 'post_content_filtered' => '',
15109 ];
15110
15111 $context['pluggable_data'] = $sample_data;
15112 $context['response_type'] = 'sample';
15113 }
15114 }
15115
15116 return $context;
15117 }
15118
15119
15120
15121 /**
15122 * Get list for Woocommerce Subscriptions
15123 *
15124 * @param array $data data.
15125 *
15126 * @return array
15127 */
15128 public function search_wc_subscription_variation_products( $data ) {
15129 $options = [];
15130
15131 global $wpdb;
15132
15133 if ( ! function_exists( 'wc_get_product' ) ) {
15134 return [];
15135 }
15136 $subscriptions = $wpdb->get_results(
15137 $wpdb->prepare(
15138 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
15139 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
15140 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
15141 AND posts.post_type = %s
15142 AND posts.post_status = %s
15143 UNION ALL
15144 SELECT ID, post_title FROM $wpdb->posts
15145 WHERE post_type = %s
15146 AND post_status = %s
15147 ORDER BY post_title",
15148 'product',
15149 'publish',
15150 'shop_subscription',
15151 'publish'
15152 )
15153 );
15154 $page = $data['page'];
15155 $limit = Utilities::get_search_page_limit();
15156 $offset = $limit * ( $page - 1 );
15157 $subscriptions_products = [];
15158 $subscription_products_count = 10;
15159 if ( function_exists( 'wc_get_products' ) ) {
15160 $product_query = wc_get_products(
15161 [
15162 'type' => [ 'subscription', 'variable-subscription' ],
15163 'posts_per_page' => $limit,
15164 'offset' => $offset,
15165 'paginate' => true,
15166 ]
15167 );
15168 if ( is_object( $product_query ) ) {
15169 $subscriptions_products = $product_query->products;
15170 $subscription_products_count = $product_query->total;
15171 }
15172 }
15173
15174 if ( $subscriptions ) {
15175 foreach ( $subscriptions as $product ) {
15176 $options[] = [
15177 'label' => $product->post_title . ' (#' . $product->ID . ')',
15178 'value' => (int) $product->ID,
15179 ];
15180 $product_s = wc_get_product( $product->ID );
15181 /**
15182 *
15183 * Ignore line
15184 *
15185 * @phpstan-ignore-next-line
15186 */
15187 if ( 'variable-subscription' == $product_s->product_type ) {
15188 $args = [
15189 'post_type' => 'product_variation',
15190 'post_parent' => $product->ID,
15191 'posts_per_page' => -1,
15192 'orderby' => 'ID',
15193 'order' => 'ASC',
15194 'post_status' => 'publish',
15195 ];
15196
15197 $variations = get_posts( $args );
15198
15199 foreach ( $variations as $var ) {
15200 $options[] = [
15201 'label' => $var->post_title . ' (#' . $var->ID . ')',
15202 'value' => $var->ID,
15203 ];
15204 }
15205 }
15206 }
15207 return [
15208 'options' => $options,
15209 'hasMore' => false,
15210 ];
15211 } elseif ( ! empty( $subscriptions_products ) ) {
15212 foreach ( $subscriptions_products as $product ) {
15213 $title = $product->get_name();
15214 $options[] = [
15215 'label' => $title . ' (#' . $product->get_id() . ')',
15216 'value' => $product->get_id(),
15217 ];
15218 $product_s = wc_get_product( $product->get_id() );
15219 /**
15220 *
15221 * Ignore line
15222 *
15223 * @phpstan-ignore-next-line
15224 */
15225 if ( 'variable-subscription' == $product_s->product_type ) {
15226 $args = [
15227 'post_type' => 'product_variation',
15228 'post_parent' => $product->get_id(),
15229 'posts_per_page' => -1,
15230 'orderby' => 'ID',
15231 'order' => 'ASC',
15232 'post_status' => 'publish',
15233 ];
15234
15235 $variations = get_posts( $args );
15236
15237 foreach ( $variations as $var ) {
15238 $options[] = [
15239 'label' => $var->post_title . ' (#' . $var->ID . ')',
15240 'value' => $var->ID,
15241 ];
15242 }
15243 }
15244 }
15245 return [
15246 'options' => $options,
15247 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
15248 ];
15249 } else {
15250 return [
15251 'options' => $options,
15252 'hasMore' => false,
15253 ];
15254 }
15255 }
15256
15257 /**
15258 * Get WS Forms form list
15259 *
15260 * @param array $data data.
15261 *
15262 * @return array
15263 */
15264 public function search_ws_forms_list( $data ) {
15265 $options = [];
15266 global $wpdb;
15267
15268 $forms = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wsf_form", 'ARRAY_A' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
15269
15270 foreach ( $forms as $form ) {
15271 $options[] = [
15272 'label' => $form['label'],
15273 'value' => $form['id'],
15274 ];
15275 }
15276
15277 return [
15278 'options' => $options,
15279 'hasMore' => false,
15280 ];
15281 }
15282
15283 /**
15284 * Get Learndash Achievement list
15285 *
15286 * @param array $data data.
15287 *
15288 * @return array
15289 */
15290 public function search_ld_achievements_list( $data ) {
15291 $page = $data['page'];
15292 $limit = Utilities::get_search_page_limit();
15293 $offset = $limit * ( $page - 1 );
15294
15295 $posts = get_posts(
15296 [
15297 'post_type' => 'ld-achievement',
15298 'posts_per_page' => $limit,
15299 'offset' => $offset,
15300 'post_status' => [ 'publish' ],
15301 ]
15302 );
15303
15304 $posts_count = wp_count_posts( 'ld-achievement' )->publish;
15305
15306 $options = [];
15307
15308 if ( ! empty( $posts ) ) {
15309 foreach ( $posts as $post ) {
15310 $options[] = [
15311 'label' => get_the_title( $post->ID ),
15312 'value' => $post->ID,
15313 ];
15314 }
15315 }
15316
15317 return [
15318 'options' => $options,
15319 'hasMore' => $posts_count > $limit && $posts_count > $offset,
15320 ];
15321 }
15322
15323 /**
15324 * Get Advanced Ads list
15325 *
15326 * @param array $data data.
15327 *
15328 * @return array
15329 */
15330 public function search_ads_list( $data ) {
15331 $page = $data['page'];
15332 $limit = Utilities::get_search_page_limit();
15333 $offset = $limit * ( $page - 1 );
15334
15335 $posts = get_posts(
15336 [
15337 'post_type' => 'advanced_ads',
15338 'posts_per_page' => $limit,
15339 'offset' => $offset,
15340 'post_status' => [ 'publish', 'draft' ],
15341 ]
15342 );
15343
15344 $posts_count = wp_count_posts( 'advanced_ads' )->publish;
15345
15346 $options = [];
15347
15348 if ( ! empty( $posts ) ) {
15349 foreach ( $posts as $post ) {
15350 $options[] = [
15351 'label' => get_the_title( $post->ID ),
15352 'value' => $post->ID,
15353 ];
15354 }
15355 }
15356
15357 return [
15358 'options' => $options,
15359 'hasMore' => $posts_count > $limit && $posts_count > $offset,
15360 ];
15361 }
15362
15363 /**
15364 * Advanced Ads pluggable data.
15365 *
15366 * @param array $data data.
15367 * @return array
15368 */
15369 public function search_ad_last_data( $data ) {
15370 $context = [];
15371 $args = [
15372 'post_type' => 'advanced_ads',
15373 'posts_per_page' => 1,
15374 'orderby' => 'modified',
15375 'order' => 'DESC',
15376 ];
15377
15378 if ( isset( $data['filter']['ad_status']['value'] ) ) {
15379 $post_status = $data['filter']['ad_status']['value'];
15380 $args['post_status'] = $post_status;
15381 }
15382
15383 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
15384 $post_status = $data['filter']['ad_new_status']['value'];
15385 $args['post_status'] = $post_status;
15386 }
15387
15388 if ( isset( $data['filter']['ad_id']['value'] ) ) {
15389 $post_id = $data['filter']['ad_id']['value'];
15390 if ( -1 != $post_id ) {
15391 if ( $post_id > 0 ) {
15392 $args['p'] = $post_id;
15393 }
15394 }
15395 }
15396
15397 $posts = get_posts( $args );
15398 if ( ! empty( $posts ) ) {
15399 $context['pluggable_data'] = $posts[0];
15400 $context['pluggable_data'] = WordPress::get_post_context( $posts[0]->ID );
15401 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
15402 $context['pluggable_data']['ad_new_status'] = $posts[0]->post_status;
15403 }
15404 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
15405 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
15406 }
15407 $context['pluggable_data']['ad_id'] = $posts[0]->ID;
15408 if ( isset( $data['filter']['ad_status']['value'] ) ) {
15409 $context['pluggable_data']['ad_status'] = $posts[0]->post_status;
15410 }
15411 $context['response_type'] = 'live';
15412 } else {
15413 $context['pluggable_data'] = [
15414 'ID' => 1,
15415 'post' => 1,
15416 'post_author' => 1,
15417 'post_date' => '2022-11-18 12:18:14',
15418 'post_date_gmt' => '2022-11-18 12:18:14',
15419 'post_content' => 'Ad Post Content',
15420 'post_title' => 'Ad Post',
15421 'post_excerpt' => '',
15422 'post_status' => 'draft',
15423 'comment_status' => 'open',
15424 'ping_status' => 'open',
15425 'post_password' => '',
15426 'post_name' => 'ad-post',
15427 'to_ping' => '',
15428 'pinged' => '',
15429 'post_modified' => '2022-11-18 12:18:14',
15430 'post_modified_gmt' => '2022-11-18 12:18:14',
15431 'post_content_filtered' => '',
15432 'post_parent' => 0,
15433 'guid' => 'https://example.com/ad-post/',
15434 'menu_order' => 0,
15435 'post_type' => 'advanced_ads',
15436 'post_mime_type' => '',
15437 'comment_count' => 0,
15438 'filter' => 'raw',
15439 ];
15440 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
15441 $context['pluggable_data']['ad_new_status'] = $data['filter']['ad_new_status']['value'];
15442 }
15443 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
15444 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
15445 }
15446 $context['pluggable_data']['ad_id'] = 1;
15447 if ( isset( $data['filter']['ad_status']['value'] ) ) {
15448 $context['pluggable_data']['ad_status'] = $data['filter']['ad_status']['value'];
15449 }
15450 $context['response_type'] = 'sample';
15451 }
15452
15453 return $context;
15454 }
15455
15456 /**
15457 * Get Newsletter lists
15458 *
15459 * @param array $data data.
15460 *
15461 * @return array
15462 */
15463 public function search_newsletter_lists( $data ) {
15464
15465 $options = [];
15466 if ( class_exists( '\Newsletter' ) ) {
15467
15468 $lists = \Newsletter::instance()->get_lists();
15469
15470 if ( ! empty( $lists ) ) {
15471 foreach ( $lists as $list ) {
15472 $options[] = [
15473 'label' => $list->name,
15474 'value' => 'list_' . $list->id,
15475 ];
15476 }
15477 }
15478 }
15479
15480 return [
15481 'options' => $options,
15482 'hasMore' => false,
15483 ];
15484 }
15485
15486 /**
15487 * Newsletter pluggable data.
15488 *
15489 * @param array $data data.
15490 * @return array
15491 */
15492 public function search_newsletter_last_data( $data ) {
15493 $context = [];
15494 global $wpdb;
15495
15496 $list = $data['filter']['list_id']['value'];
15497
15498 if ( -1 == $list ) {
15499 $log = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . "newsletter where status='C' ORDER BY id DESC LIMIT 1" );
15500 } else {
15501 $num = $list;
15502 $location = 1;
15503 $sql = 'SELECT * FROM ' . $wpdb->prefix . "newsletter WHERE $num = %d AND status = 'C' ORDER BY id DESC LIMIT 1";
15504 $log = $wpdb->get_results( $wpdb->prepare( $sql, $location ), ARRAY_A );// @phpcs:ignore
15505 }
15506
15507 if ( ! empty( $log ) ) {
15508 $lists_arr = get_option( 'newsletter_lists' );
15509 if ( -1 == $list ) {
15510 foreach ( $log[0] as $key => $val ) {
15511 if ( defined( 'NEWSLETTER_LIST_MAX' ) ) {
15512 for ( $i = 1; $i <= NEWSLETTER_LIST_MAX; $i++ ) {
15513 $list_key = "list_$i";
15514
15515 if ( $key == $list_key ) {
15516 if ( 1 == $val ) {
15517 $context['pluggable_data']['list_id'] = $key;
15518 if ( is_array( $lists_arr ) ) {
15519 if ( isset( $lists_arr[ $key ] ) ) {
15520 $list_name = $lists_arr[ $key ];
15521 $context['pluggable_data']['list_name'] = $list_name;
15522 }
15523 }
15524 continue;
15525 }
15526 }
15527 }
15528 }
15529 if ( 'email' == $key ) {
15530 $context['pluggable_data']['email'] = $val;
15531 }
15532 }
15533 } else {
15534 $context['pluggable_data']['list_id'] = $list;
15535 $context['pluggable_data']['email'] = $log[0]['email'];
15536 if ( is_array( $lists_arr ) ) {
15537 if ( isset( $lists_arr[ $list ] ) ) {
15538 $list_name = $lists_arr[ $list ];
15539 $context['pluggable_data']['list_name'] = $list_name;
15540 }
15541 }
15542 }
15543 $context['response_type'] = 'live';
15544 } else {
15545 $context = json_decode( '{"response_type":"sample","pluggable_data":{"list_id": "list_1","email": "johnd@mailinator.com","list_name": "Contact List"}}', true );
15546 }
15547
15548 return $context;
15549 }
15550
15551 /**
15552 * Get wpForo Forum list
15553 *
15554 * @param array $data data.
15555 *
15556 * @return array
15557 */
15558 public function search_wp_forum_list( $data ) {
15559
15560 if ( ! function_exists( 'WPF' ) ) {
15561 return [];
15562 }
15563
15564 $forums = WPF()->forum->get_forums( [ 'type' => 'forum' ] );
15565
15566 $options = [];
15567
15568 if ( ! empty( $forums ) ) {
15569 foreach ( $forums as $forum ) {
15570 $options[] = [
15571 'label' => $forum['title'],
15572 'value' => $forum['forumid'],
15573 ];
15574 }
15575 }
15576
15577 return [
15578 'options' => $options,
15579 'hasMore' => false,
15580 ];
15581 }
15582
15583 /**
15584 * Get wpForo Topic list
15585 *
15586 * @param array $data data.
15587 *
15588 * @return array
15589 */
15590 public function search_wp_topic_list( $data ) {
15591
15592 if ( ! function_exists( 'WPF' ) ) {
15593 return [];
15594 }
15595
15596 $forum_id = $data['dynamic'];
15597
15598 $topics = WPF()->topic->get_topics( [ 'forumid' => $forum_id ] );
15599
15600 $options = [];
15601
15602 if ( ! empty( $topics ) ) {
15603 foreach ( $topics as $topic ) {
15604 $options[] = [
15605 'label' => $topic['title'],
15606 'value' => $topic['topicid'],
15607 ];
15608 }
15609 }
15610
15611 return [
15612 'options' => $options,
15613 'hasMore' => false,
15614 ];
15615 }
15616
15617 /**
15618 * Get wpForo Groups list
15619 *
15620 * @param array $data data.
15621 *
15622 * @return array
15623 */
15624 public function search_wp_user_groups_list( $data ) {
15625
15626 if ( ! function_exists( 'WPF' ) ) {
15627 return [];
15628 }
15629
15630 $usergroups = WPF()->usergroup->get_usergroups();
15631
15632 $options = [];
15633
15634 if ( ! empty( $usergroups ) ) {
15635 foreach ( $usergroups as $group ) {
15636 $options[] = [
15637 'label' => $group['name'],
15638 'value' => intval( $group['groupid'] ),
15639 ];
15640 }
15641 }
15642
15643 return [
15644 'options' => $options,
15645 'hasMore' => false,
15646 ];
15647 }
15648
15649 /**
15650 * Get wpForo Reputation list
15651 *
15652 * @param array $data data.
15653 *
15654 * @return array
15655 */
15656 public function search_wp_foro_reputation_list( $data ) {
15657
15658 if ( ! function_exists( 'WPF' ) ) {
15659 return [];
15660 }
15661
15662 $levels = WPF()->member->levels();
15663 $options = [];
15664
15665 if ( ! empty( $levels ) ) {
15666 foreach ( $levels as $level ) {
15667 $options[] = [
15668 'label' => esc_attr__( 'Level', 'suretriggers' ) . ' ' . $level . ' - ' . WPF()->member->rating( $level, 'title' ),
15669 'value' => strval( $level ),
15670 ];
15671 }
15672 }
15673
15674 return [
15675 'options' => $options,
15676 'hasMore' => false,
15677 ];
15678 }
15679
15680 /**
15681 * WPForo new topic pluggable data.
15682 *
15683 * @param array $data data.
15684 * @return array
15685 */
15686 public function search_pluggables_wpforo_topic_last_data( $data ) {
15687 $context = [];
15688 global $wpdb;
15689
15690 $forum_id = $data['filter']['forum_id']['value'];
15691
15692 if ( -1 == $forum_id ) {
15693 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'wpforo_topics WHERE closed = 0 ORDER BY topicid DESC LIMIT 1', ARRAY_A );
15694 } else {
15695 $forum = $forum_id;
15696 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_topics WHERE forumid = %d AND closed = 0 ORDER BY topicid DESC LIMIT 1';
15697 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum ), ARRAY_A );// @phpcs:ignore
15698 }
15699
15700 if ( ! empty( $results ) ) {
15701 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
15702 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
15703
15704 if ( function_exists( 'WPF' ) ) {
15705 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
15706 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
15707 }
15708 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
15709 $context['response_type'] = 'live';
15710 } else {
15711 $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
15712 }
15713
15714 return $context;
15715 }
15716
15717 /**
15718 * WpForo Topic Reply pluggable data.
15719 *
15720 * @param array $data data.
15721 * @return array
15722 */
15723 public function search_pluggables_wpforo_topic_reply_last_data( $data ) {
15724 $context = [];
15725 global $wpdb;
15726
15727 $forum_id = $data['filter']['forum_id']['value'];
15728 $topic_id = $data['filter']['topic_id']['value'];
15729
15730 if ( -1 == $forum_id && -1 != $topic_id ) {
15731 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE topicid = %d ORDER BY postid DESC LIMIT 1';
15732 $results = $wpdb->get_results( $wpdb->prepare( $sql, $topic_id ), ARRAY_A );// @phpcs:ignore
15733 } elseif ( -1 != $forum_id && -1 == $topic_id ) {
15734 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d ORDER BY postid DESC LIMIT 1';
15735 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id ), ARRAY_A );// @phpcs:ignore
15736 } elseif ( -1 == $forum_id && -1 == $topic_id ) {
15737 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts ORDER BY postid DESC LIMIT 1';
15738 $results = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );// @phpcs:ignore
15739 } else {
15740 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d AND topicid = %d ORDER BY postid DESC LIMIT 1';
15741 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id, $topic_id ), ARRAY_A );// @phpcs:ignore
15742 }
15743
15744 if ( ! empty( $results ) ) {
15745 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
15746 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
15747 if ( function_exists( 'WPF' ) ) {
15748 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
15749 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
15750 $context['pluggable_data']['reply'] = WPF()->post->get_post( $results[0]['postid'] );
15751 }
15752 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
15753 $context['response_type'] = 'live';
15754 } else {
15755 $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
15756 }
15757
15758 return $context;
15759 }
15760
15761 /**
15762 * Get RafflePress Giveaways list
15763 *
15764 * @param array $data data.
15765 *
15766 * @return array
15767 */
15768 public function search_rp_giveaways_list( $data ) {
15769
15770 global $wpdb;
15771 $options = [];
15772
15773 $giveaways = $wpdb->get_results( "SELECT id,name FROM {$wpdb->prefix}rafflepress_giveaways WHERE deleted_at is null ORDER BY name ASC", ARRAY_A );
15774 foreach ( $giveaways as $giveaway ) {
15775 $options[] = [
15776 'label' => $giveaway['name'],
15777 'value' => $giveaway['id'],
15778 ];
15779 }
15780
15781 return [
15782 'options' => $options,
15783 'hasMore' => false,
15784 ];
15785 }
15786
15787 /**
15788 * Get RafflePress Giveaway Actions list
15789 *
15790 * @param array $data data.
15791 *
15792 * @return array
15793 */
15794 public function search_rp_giveaway_actions_list( $data ) {
15795
15796 global $wpdb;
15797 $options = [];
15798
15799 $giveaway = $wpdb->get_row( $wpdb->prepare( "SELECT settings FROM {$wpdb->prefix}rafflepress_giveaways WHERE id=%d", $data['dynamic'] ), ARRAY_A );
15800
15801 if ( is_array( $giveaway ) && isset( $giveaway['settings'] ) ) {
15802 $settings = json_decode( $giveaway['settings'], true );
15803
15804 if ( is_array( $settings ) && isset( $settings['entry_options'] ) ) {
15805 foreach ( $settings['entry_options'] as $action ) {
15806 $options[] = [
15807 'label' => $action['name'],
15808 'value' => $action['id'],
15809 ];
15810 }
15811 }
15812 }
15813
15814 return [
15815 'options' => $options,
15816 'hasMore' => false,
15817 ];
15818 }
15819
15820 /**
15821 * Get last data for trigger.
15822 *
15823 * @param array $data data.
15824 * @return array
15825 */
15826 public function search_raffle_press_triggers_last_data( $data ) {
15827
15828 global $wpdb;
15829
15830 $context = [];
15831 $context['response_type'] = 'sample';
15832
15833 $pluggable_data = [
15834 'giveaway_id' => 1,
15835 'giveaway_title' => 'Sample Giveaway (ID#1)',
15836 'giveaway_start_date' => 'September 20, 2023',
15837 'giveaway_end_date' => 'October 6, 2023',
15838 'giveaway_entries' => 9,
15839 'giveaway_user_count' => 3,
15840 'giveaway_status' => 'Active',
15841 'contestant_name' => 'John Doe',
15842 'contestant_email' => 'john_doe@gmail.com',
15843 'contestant_email_verified' => 'Yes',
15844 'action_id' => '0jnex',
15845 'action_name' => 'Visit us on Instagram',
15846 ];
15847
15848 $giveaway_id = isset( $data['filter']['giveaway_id'] ) ? $data['filter']['giveaway_id']['value'] : null;
15849 $action_id = isset( $data['filter']['action_id'] ) ? $data['filter']['action_id']['value'] : null;
15850
15851 $query = "SELECT contestant_id, giveaway_id, action_id, meta FROM {$wpdb->prefix}rafflepress_entries";
15852
15853 if ( $giveaway_id && -1 != $giveaway_id ) {
15854 $query .= ' WHERE giveaway_id = ' . $giveaway_id;
15855
15856 if ( $action_id ) {
15857 $query .= " AND action_id = '" . $action_id . "'";
15858 }
15859 }
15860
15861 $query .= ' ORDER BY created_at DESC LIMIT 1';
15862
15863 $giveaway_data = $wpdb->get_row( $query, ARRAY_A ); // @phpcs:ignore
15864
15865 if ( ! empty( $giveaway_data ) ) {
15866 $pluggable_data = array_merge(
15867 RafflePress::get_giveaway_context( $giveaway_data['giveaway_id'] ),
15868 RafflePress::get_contestant_context( $giveaway_data['contestant_id'] )
15869 );
15870
15871 $pluggable_data['performed_action_id'] = isset( $giveaway_data['action_id'] ) ? $giveaway_data['action_id'] : 0;
15872
15873 $giveaway_meta = isset( $giveaway_data['meta'] ) ? json_decode( $giveaway_data['meta'], true ) : [];
15874 $pluggable_data['performed_action_name'] = is_array( $giveaway_meta ) && isset( $giveaway_meta['action'] ) ? $giveaway_meta['action'] : '';
15875
15876 $context['response_type'] = 'live';
15877 }
15878
15879 $context['pluggable_data'] = $pluggable_data;
15880
15881 return $context;
15882 }
15883
15884 /**
15885 * Get last data for trigger
15886 *
15887 * @param array $data data.
15888 * @return array
15889 */
15890 public function search_woo_commerce_shipstation_triggers_last_data( $data ) {
15891 $context = [];
15892 $context['response_type'] = 'sample';
15893 $context['pluggable_data'] = [];
15894
15895 $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
15896
15897 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
15898 $condition = $data['filter']['condition_compare']['value'];
15899 $price = $data['filter']['price']['value'];
15900 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
15901
15902 if ( 'order_shipped' === $term ) {
15903 $orders = wc_get_orders(
15904 [
15905 'numberposts' => 1,
15906 'orderby' => 'date',
15907 'order' => 'DESC',
15908 'status' => 'completed',
15909 'meta_query' => [
15910 [
15911 'key' => '_shipstation_shipped_item_count',
15912 'compare' => 'EXISTS',
15913 ],
15914 ],
15915 ]
15916 );
15917 if ( is_array( $orders ) && count( $orders ) > 0 ) {
15918 $order_id = $orders[0]->get_id();
15919 $order = wc_get_order( $order_id );
15920 $product_ids = [];
15921 if ( $order && $order instanceof \WC_Order ) {
15922 $user_id = $order->get_customer_id();
15923 $items = $order->get_items();
15924 foreach ( $items as $item ) {
15925 if ( method_exists( $item, 'get_product_id' ) ) {
15926 $product_ids[] = $item->get_product_id();
15927 }
15928 }
15929
15930 foreach ( $product_ids as $product_id ) {
15931 $context['product_id'] = $product_id;
15932 }
15933 $order_context = WooCommerce::get_order_context( $order_id );
15934 $context = array_merge(
15935 isset( $order_context ) ? $order_context : [],
15936 isset( $user_id ) ? WordPress::get_user_context( $user_id ) : []
15937 );
15938 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
15939 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
15940 /**
15941 *
15942 * Ignore line
15943 *
15944 * @phpstan-ignore-next-line
15945 */
15946 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
15947 /**
15948 *
15949 * Ignore line
15950 *
15951 * @phpstan-ignore-next-line
15952 */
15953 $date = date_i18n( get_option( 'date_format' ), $timestamp );
15954 $context['ship_date'] = $date;
15955 $context['response_type'] = 'live';
15956 }
15957 }
15958
15959 $context['pluggable_data'] = $context;
15960 } elseif ( 'specific_product_order_shipped' === $term ) {
15961 if ( -1 != $product_id ) {
15962 $product_ids = [ $product_id ];
15963 $orders = wc_get_orders(
15964 [
15965 'numberposts' => 1,
15966 'orderby' => 'date',
15967 'order' => 'DESC',
15968 'status' => 'completed',
15969 'meta_query' => [
15970 [
15971 'key' => '_shipstation_shipped_item_count',
15972 'compare' => 'EXISTS',
15973 ],
15974 [
15975 'key' => '_product_id',
15976 'value' => $product_ids,
15977 'compare' => 'IN',
15978 ],
15979 ],
15980 ]
15981 );
15982 } else {
15983 $orders = wc_get_orders(
15984 [
15985 'numberposts' => 1,
15986 'orderby' => 'date',
15987 'order' => 'DESC',
15988 'status' => 'completed',
15989 'meta_query' => [
15990 [
15991 'key' => '_shipstation_shipped_item_count',
15992 'compare' => 'EXISTS',
15993 ],
15994 ],
15995 ]
15996 );
15997 }
15998 if ( is_array( $orders ) && count( $orders ) > 0 ) {
15999 $order_id = $orders[0]->get_id();
16000 $order = wc_get_order( $order_id );
16001 $productids = [];
16002 if ( $order instanceof \WC_Order ) {
16003 $user_id = $order->get_customer_id();
16004 $items = $order->get_items();
16005 foreach ( $items as $item ) {
16006 if ( method_exists( $item, 'get_product_id' ) ) {
16007 $productids[] = $item->get_product_id();
16008 }
16009 }
16010 $order_context = WooCommerce::get_order_context( $order_id );
16011 $context = array_merge(
16012 isset( $order_context ) ? $order_context : [],
16013 WordPress::get_user_context( $user_id )
16014 );
16015 foreach ( $productids as $product_id ) {
16016 $context['product_id'] = $product_id;
16017 }
16018
16019 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
16020 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
16021 /**
16022 *
16023 * Ignore line
16024 *
16025 * @phpstan-ignore-next-line
16026 */
16027 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
16028 /**
16029 *
16030 * Ignore line
16031 *
16032 * @phpstan-ignore-next-line
16033 */
16034 $date = date_i18n( get_option( 'date_format' ), $timestamp );
16035 $context['ship_date'] = $date;
16036 $context['response_type'] = 'live';
16037 }
16038 }
16039
16040 $context['pluggable_data'] = $context;
16041 } elseif ( 'specific_amount_order_shipped' === $term ) {
16042 $orders = wc_get_orders(
16043 [
16044 'numberposts' => 1,
16045 'orderby' => 'date',
16046 'order' => 'DESC',
16047 'status' => 'completed',
16048 'meta_query' => [
16049 [
16050 'key' => '_shipstation_shipped_item_count',
16051 'compare' => 'EXISTS',
16052 ],
16053 [
16054 'key' => '_order_total',
16055 'value' => $price,
16056 'compare' => $condition,
16057 ],
16058 ],
16059 ]
16060 );
16061 if ( is_array( $orders ) && count( $orders ) > 0 ) {
16062 $order_id = $orders[0]->get_id();
16063 $order = wc_get_order( $order_id );
16064 $product_ids = [];
16065 if ( $order instanceof \WC_Order ) {
16066 $user_id = $order->get_customer_id();
16067 $items = $order->get_items();
16068 foreach ( $items as $item ) {
16069 if ( method_exists( $item, 'get_product_id' ) ) {
16070 $product_ids[] = $item->get_product_id();
16071 }
16072 }
16073 $order_context = WooCommerce::get_order_context( $order_id );
16074 $context = array_merge(
16075 isset( $order_context ) ? $order_context : [],
16076 isset( $user_id ) ? WordPress::get_user_context( $user_id ) : []
16077 );
16078
16079 foreach ( $product_ids as $product_id ) {
16080 $context['product_id'] = $product_id;
16081 }
16082
16083 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
16084 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
16085 /**
16086 *
16087 * Ignore line
16088 *
16089 * @phpstan-ignore-next-line
16090 */
16091 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
16092 /**
16093 *
16094 * Ignore line
16095 *
16096 * @phpstan-ignore-next-line
16097 */
16098 $date = date_i18n( get_option( 'date_format' ), $timestamp );
16099 $context['ship_date'] = $date;
16100 $context['response_type'] = 'live';
16101 }
16102 }
16103
16104 $context['pluggable_data'] = $context;
16105 }
16106
16107 return $context;
16108 }
16109
16110 /**
16111 * Get GroundHogg Tag list
16112 *
16113 * @param array $data data.
16114 *
16115 * @return array
16116 */
16117 public function search_groundhogg_tag_list( $data ) {
16118
16119 if ( ! function_exists( 'Groundhogg\get_db' ) ) {
16120 return [];
16121 }
16122
16123 $tags = \Groundhogg\get_db( 'tags' )->query( [] );
16124 $options = [];
16125
16126 if ( ! empty( $tags ) ) {
16127 foreach ( $tags as $tag ) {
16128 $options[] = [
16129 'label' => $tag->tag_name,
16130 'value' => $tag->tag_id,
16131 ];
16132 }
16133 }
16134
16135 return [
16136 'options' => $options,
16137 'hasMore' => false,
16138 ];
16139 }
16140
16141 /**
16142 * Get WP courseware courses list
16143 *
16144 * @param array $data data.
16145 *
16146 * @return array
16147 */
16148 public function search_wpcw_courses( $data ) {
16149 $options = [];
16150
16151 $page = $data['page'];
16152 $limit = Utilities::get_search_page_limit();
16153 $offset = $limit * ( $page - 1 );
16154
16155 $args = [
16156 'post_type' => 'wpcw_course',
16157 'posts_per_page' => $limit,
16158 'offset' => $offset,
16159 'orderby' => 'title',
16160 'order' => 'ASC',
16161 'post_status' => 'publish',
16162 ];
16163
16164 $courses = get_posts( $args );
16165
16166 $course_count = wp_count_posts( 'wpcw_course' )->publish;
16167
16168 if ( ! empty( $courses ) ) {
16169 if ( is_array( $courses ) ) {
16170 foreach ( $courses as $course ) {
16171 $options[] = [
16172 'label' => $course->post_title,
16173 'value' => $course->ID,
16174 ];
16175 }
16176 }
16177 }
16178 return [
16179 'options' => $options,
16180 'hasMore' => $course_count > $limit && $course_count > $offset,
16181 ];
16182 }
16183
16184 /**
16185 * Get WP courseware courses list
16186 *
16187 * @param array $data data.
16188 *
16189 * @return array
16190 */
16191 public function search_wpcw_modules( $data ) {
16192 $options = [];
16193
16194 if ( function_exists( 'wpcw_get_modules' ) ) {
16195 $modules = wpcw_get_modules();
16196 }
16197
16198 if ( ! empty( $modules ) ) {
16199 if ( is_array( $modules ) ) {
16200 foreach ( $modules as $module ) {
16201 $options[] = [
16202 'label' => $module->module_title,
16203 'value' => $module->module_id,
16204 ];
16205 }
16206 }
16207 }
16208 return [
16209 'options' => $options,
16210 'hasMore' => false,
16211 ];
16212 }
16213
16214 /**
16215 * Get WP courseware unit list
16216 *
16217 * @param array $data data.
16218 *
16219 * @return array
16220 */
16221 public function search_wpcw_units( $data ) {
16222 $options = [];
16223
16224 $page = $data['page'];
16225 $limit = Utilities::get_search_page_limit();
16226 $offset = $limit * ( $page - 1 );
16227
16228 $args = [
16229 'post_type' => 'course_unit',
16230 'posts_per_page' => $limit,
16231 'offset' => $offset,
16232 'orderby' => 'title',
16233 'order' => 'ASC',
16234 'post_status' => 'publish',
16235 ];
16236
16237 $units = get_posts( $args );
16238
16239 $unit_count = wp_count_posts( 'course_unit' )->publish;
16240
16241 if ( ! empty( $units ) ) {
16242 if ( is_array( $units ) ) {
16243 foreach ( $units as $unit ) {
16244 $options[] = [
16245 'label' => $unit->post_title,
16246 'value' => $unit->ID,
16247 ];
16248 }
16249 }
16250 }
16251 return [
16252 'options' => $options,
16253 'hasMore' => $unit_count > $limit && $unit_count > $offset,
16254 ];
16255 }
16256
16257 /**
16258 * Search WP Courseware data.
16259 *
16260 * @param array $data data.
16261 * @return array|void
16262 */
16263 public function search_wpcw_last_data( $data ) {
16264 global $wpdb;
16265 $post_type = $data['post_type'];
16266 $trigger = $data['search_term'];
16267 $context = [];
16268
16269 if ( 'wpcw_course_completed' === $trigger ) {
16270 $post_id = $data['filter']['course_post_id']['value'];
16271 if ( -1 === $post_id ) {
16272 $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" );
16273 } else {
16274 $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 ) );
16275 }
16276 } elseif ( 'wpcw_module_completed' === $trigger ) {
16277 $post_id = $data['filter']['module_id']['value'];
16278 if ( -1 === $post_id ) {
16279 $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" );
16280 } else {
16281 $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 ) );
16282 }
16283 } elseif ( 'wpcw_unit_completed' === $trigger ) {
16284 $post_id = $data['filter']['unit_id']['value'];
16285 if ( -1 === $post_id ) {
16286 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpcw_user_progress WHERE unit_completed_status='complete' order by unit_id DESC LIMIT 1" );
16287 } else {
16288 $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 ) );
16289 }
16290 } elseif ( 'wpcw_enroll_course' === $trigger ) {
16291 $post_id = $data['filter']['course_post_id']['value'];
16292 if ( -1 === $post_id ) {
16293 $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" );
16294 } else {
16295 $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 ) );
16296 }
16297 }
16298
16299 if ( ! empty( $result ) ) {
16300
16301 switch ( $trigger ) {
16302 case 'wpcw_course_completed':
16303 $result_course_id = $result[0]->course_id;
16304 $result_user_id = $result[0]->user_id;
16305 if ( function_exists( 'wpcw_get_course' ) ) {
16306 $course = wpcw_get_course( $result_course_id );
16307 if ( is_object( $course ) ) {
16308 $course = get_object_vars( $course );
16309 }
16310 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course );
16311 }
16312 break;
16313 case 'wpcw_module_completed':
16314 $result_module_id = $result[0]->module_id;
16315 $result_user_id = $result[0]->user_id;
16316 if ( function_exists( 'wpcw_get_module' ) ) {
16317 $module = wpcw_get_module( $result_module_id );
16318 if ( is_object( $module ) ) {
16319 $module = get_object_vars( $module );
16320 }
16321 $context = array_merge( WordPress::get_user_context( $result_user_id ), $module );
16322 }
16323 break;
16324 case 'wpcw_unit_completed':
16325 $result_unit_id = $result[0]->unit_id;
16326 $result_user_id = $result[0]->user_id;
16327 if ( function_exists( 'wpcw_get_unit' ) ) {
16328 $unit = wpcw_get_unit( $result_unit_id );
16329 if ( is_object( $unit ) ) {
16330 $unit = get_object_vars( $unit );
16331 $unit['name'] = get_the_title( $result_unit_id );
16332 }
16333 $context = array_merge( WordPress::get_user_context( $result_user_id ), $unit );
16334 }
16335 break;
16336 case 'wpcw_enroll_course':
16337 $result_course_id = $result[0]->course_id;
16338 $result_user_id = $result[0]->user_id;
16339 if ( function_exists( 'WPCW_courses_getCourseDetails' ) ) {
16340 $course_detail = WPCW_courses_getCourseDetails( $result_course_id );
16341 if ( is_object( $course_detail ) ) {
16342 $course_detail = get_object_vars( $course_detail );
16343 }
16344 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course_detail );
16345 }
16346 break;
16347 default:
16348 return;
16349 }
16350 $context['pluggable_data'] = $context;
16351 $context['response_type'] = 'live';
16352 }
16353
16354 return $context;
16355
16356 }
16357
16358 /**
16359 * Get WooCommerce Order Note list.
16360 *
16361 * @param array $data data.
16362 *
16363 * @return array
16364 */
16365 public function search_note_type_list( $data ) {
16366
16367 $options = [];
16368
16369 $options[] = [
16370 'label' => 'Customer',
16371 'value' => 'customer',
16372 ];
16373 $options[] = [
16374 'label' => 'Private',
16375 'value' => 'internal',
16376 ];
16377
16378 return [
16379 'options' => $options,
16380 'hasMore' => false,
16381 ];
16382 }
16383
16384 /**
16385 * Get last data for trigger
16386 *
16387 * @param array $data data.
16388 * @return array
16389 */
16390 public function search_woo_commerce_customers_triggers_last_data( $data ) {
16391 $context = [];
16392 $context['response_type'] = 'sample';
16393 $context['pluggable_data'] = [];
16394 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
16395 global $wpdb;
16396 if ( 'customer_created' === $term ) {
16397 $customer_query = get_users(
16398 [
16399 'fields' => 'ID',
16400 'role' => 'customer',
16401 'orderby' => 'ID',
16402 'order' => 'DESC',
16403 'number' => 1,
16404 ]
16405 );
16406 $results = $customer_query;
16407 if ( ! empty( $results ) ) {
16408 $customer = new WC_Customer( $results[0] );
16409 $last_order = $customer->get_last_order();
16410 $customer_data = [
16411 'id' => $customer->get_id(),
16412 'email' => $customer->get_email(),
16413 'first_name' => $customer->get_first_name(),
16414 'last_name' => $customer->get_last_name(),
16415 'username' => $customer->get_username(),
16416 'last_order_id' => is_object( $last_order ) ? $last_order->get_id() : null,
16417 'orders_count' => $customer->get_order_count(),
16418 'total_spent' => wc_format_decimal( $customer->get_total_spent(), 2 ),
16419 'avatar_url' => $customer->get_avatar_url(),
16420 'billing_address' => [
16421 'first_name' => $customer->get_billing_first_name(),
16422 'last_name' => $customer->get_billing_last_name(),
16423 'company' => $customer->get_billing_company(),
16424 'address_1' => $customer->get_billing_address_1(),
16425 'address_2' => $customer->get_billing_address_2(),
16426 'city' => $customer->get_billing_city(),
16427 'state' => $customer->get_billing_state(),
16428 'postcode' => $customer->get_billing_postcode(),
16429 'country' => $customer->get_billing_country(),
16430 'email' => $customer->get_billing_email(),
16431 'phone' => $customer->get_billing_phone(),
16432 ],
16433 'shipping_address' => [
16434 'first_name' => $customer->get_shipping_first_name(),
16435 'last_name' => $customer->get_shipping_last_name(),
16436 'company' => $customer->get_shipping_company(),
16437 'address_1' => $customer->get_shipping_address_1(),
16438 'address_2' => $customer->get_shipping_address_2(),
16439 'city' => $customer->get_shipping_city(),
16440 'state' => $customer->get_shipping_state(),
16441 'postcode' => $customer->get_shipping_postcode(),
16442 'country' => $customer->get_shipping_country(),
16443 ],
16444 ];
16445 if ( is_object( $last_order ) && method_exists( $last_order, 'get_date_created' ) ) {
16446 $created_date = $last_order->get_date_created();
16447 if ( is_object( $created_date ) && method_exists( $created_date, 'getTimestamp' ) ) {
16448 $last_order_date = $created_date->getTimestamp();
16449 $customer_data['created_at'] = $last_order_date;
16450 $customer_data['last_order_date'] = $last_order_date;
16451 }
16452 }
16453 $order_sample_data = $customer_data;
16454 $context['response_type'] = 'live';
16455 $context['pluggable_data'] = $order_sample_data;
16456 } else {
16457 $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 );
16458 $context['pluggable_data'] = $order_sample_data;
16459 }
16460 } elseif ( 'total_spend_reach' === $term ) {
16461 $total_spend_selected = isset( $data['filter']['total_spend']['value'] ) ? $data['filter']['total_spend']['value'] : '';
16462 $customers = $wpdb->get_col(
16463 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
16464 WHERE meta_key = '_customer_user' AND meta_value > 0"
16465 );
16466 $target_customer = null;
16467 foreach ( $customers as $customer ) {
16468 $total_spend = (int) wc_get_customer_total_spent( $customer );
16469 if ( $total_spend == $total_spend_selected ) {
16470 $target_customer = $customer;
16471 break; // Exit the loop once a matching customer is found.
16472 }
16473 }
16474 if ( $target_customer ) {
16475 $new_customer = new WC_Customer( $target_customer );
16476 $new_customer_data = [
16477 'id' => $new_customer->get_id(),
16478 'email' => $new_customer->get_email(),
16479 'first_name' => $new_customer->get_first_name(),
16480 'last_name' => $new_customer->get_last_name(),
16481 'username' => $new_customer->get_username(),
16482 'order_count' => $new_customer->get_order_count(),
16483 'total_spend' => wc_format_decimal( $new_customer->get_total_spent(), 2 ),
16484 ];
16485 $context['response_type'] = 'live';
16486 $context['pluggable_data'] = $new_customer_data;
16487 } else {
16488 $sample_customer_data = [
16489 'id' => '101',
16490 'created_at' => '1680675247',
16491 'email' => 'john@d.com',
16492 'first_name' => 'John',
16493 'last_name' => 'D',
16494 'username' => 'johnd',
16495 'order_count' => '3',
16496 'total_spend' => '22.00',
16497 ];
16498 $context['pluggable_data'] = $sample_customer_data;
16499 }
16500 } elseif ( 'order_count_reach' === $term ) {
16501 $total_order_selected = isset( $data['filter']['order_count']['value'] ) ? $data['filter']['order_count']['value'] : '';
16502 $customers = $wpdb->get_col(
16503 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
16504 WHERE meta_key = '_customer_user' AND meta_value > 0"
16505 );
16506 $target_customer = null;
16507 foreach ( $customers as $customer ) {
16508 $args = [
16509 'customer_id' => $customer,
16510 'limit' => -1,
16511 'status' => [ 'wc-completed' ],
16512 ];
16513 $orders = wc_get_orders( $args );
16514 if ( ! empty( $orders ) ) {
16515 $total_order = (int) wc_get_customer_order_count( $customer );
16516 if ( $total_order == $total_order_selected ) {
16517 $target_customer = $customer;
16518 break; // Exit the loop once a matching customer is found.
16519 }
16520 }
16521 }
16522 if ( $target_customer ) {
16523 $new_customer = new WC_Customer( $target_customer );
16524 $new_customer_data = [
16525 'id' => $new_customer->get_id(),
16526 'email' => $new_customer->get_email(),
16527 'first_name' => $new_customer->get_first_name(),
16528 'last_name' => $new_customer->get_last_name(),
16529 'username' => $new_customer->get_username(),
16530 'order_count' => $new_customer->get_order_count(),
16531 ];
16532 $context['response_type'] = 'live';
16533 $context['pluggable_data'] = $new_customer_data;
16534 } else {
16535 $sample_customer_data = [
16536 'id' => '101',
16537 'created_at' => '1680675247',
16538 'email' => 'john@d.com',
16539 'first_name' => 'John',
16540 'last_name' => 'D',
16541 'username' => 'johnd',
16542 'orders_count' => '3',
16543 ];
16544 $context['pluggable_data'] = $sample_customer_data;
16545 }
16546 }
16547
16548 return $context;
16549 }
16550
16551 /**
16552 * Get Affiliate list
16553 *
16554 * @param array $data data.
16555 *
16556 * @return array
16557 */
16558 public function search_affiliate_list( $data ) {
16559 $options = [];
16560
16561 $args = [
16562 'meta_query' => [
16563 [
16564 'key' => 'wafp_is_affiliate',
16565 'value' => '1',
16566 'compare' => '=',
16567 ],
16568 ],
16569 ];
16570
16571 $affiliates = get_users( $args );
16572
16573 foreach ( $affiliates as $user ) {
16574 $options[] = [
16575 'label' => $user->display_name,
16576 'value' => $user->ID,
16577 ];
16578 }
16579 return [
16580 'options' => $options,
16581 'hasMore' => false,
16582 ];
16583 }
16584
16585 /**
16586 * Get Affiliate Source list
16587 *
16588 * @param array $data data.
16589 *
16590 * @return array
16591 */
16592 public function search_affiliate_transaction_source_list( $data ) {
16593 $options = [];
16594
16595 $sources = [
16596 'General',
16597 'MemberPress',
16598 'WooCommerce',
16599 'Easy Digital Downloads',
16600 'WPForms',
16601 'Formidable',
16602 'PayPal',
16603 ];
16604 foreach ( $sources as $source ) {
16605 $options[] = [
16606 'label' => $source,
16607 'value' => str_replace( ' ', '_', strtolower( $source ) ),
16608 ];
16609 }
16610
16611 return [
16612 'options' => $options,
16613 'hasMore' => false,
16614 ];
16615 }
16616
16617 /**
16618 * Search Easy Affiliate data.
16619 *
16620 * @param array $data data.
16621 * @return array|void
16622 */
16623 public function search_easy_affiliate_last_data( $data ) {
16624
16625 global $wpdb;
16626 $trigger = $data['search_term'];
16627 $context = [];
16628
16629 if ( 'sale_recorded' === $trigger ) {
16630 $affiliate_id = $data['filter']['affiliate_id']['value'];
16631 if ( -1 === $affiliate_id ) {
16632 $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" );
16633 } else {
16634 $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 ) );
16635 }
16636 } elseif ( 'sale_added' === $trigger ) {
16637 $orderby = 'signup_date';
16638 $order = 'DESC';
16639 $paged = 1;
16640 $search = '';
16641 $perpage = 1;
16642 /**
16643 *
16644 * Ignore line
16645 *
16646 * @phpstan-ignore-next-line
16647 */
16648 $result = \EasyAffiliate\Models\User::affiliate_list_table( $orderby, $order, $paged, $search, $perpage );
16649 if ( empty( $result['results'] ) ) {
16650 $result = [];
16651 }
16652 } elseif ( 'payout_made' === $trigger ) {
16653 $affiliate_id = $data['filter']['affiliate_id']['value'];
16654 if ( -1 === $affiliate_id ) {
16655 $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" );
16656 } else {
16657 $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 ) );
16658 }
16659 }
16660
16661 if ( ! empty( $result ) ) {
16662 switch ( $trigger ) {
16663 case 'sale_recorded':
16664 $result_affiliate_id = $result[0]->affiliate_id;
16665 /**
16666 *
16667 * Ignore line
16668 *
16669 * @phpstan-ignore-next-line
16670 */
16671 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
16672 $affiliate = get_object_vars( $affiliate->rec );
16673 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
16674 break;
16675 case 'sale_added':
16676 /**
16677 *
16678 * Ignore line
16679 *
16680 * @phpstan-ignore-next-line
16681 */
16682 $data = new \EasyAffiliate\Models\User( $result['results'][0]->ID );
16683 /**
16684 *
16685 * Ignore line
16686 *
16687 * @phpstan-ignore-next-line
16688 */
16689 $context = get_object_vars( $data->rec );
16690 break;
16691 case 'payout_made':
16692 $result_affiliate_id = $result[0]->affiliate_id;
16693 /**
16694 *
16695 * Ignore line
16696 *
16697 * @phpstan-ignore-next-line
16698 */
16699 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
16700 $affiliate = get_object_vars( $affiliate->rec );
16701 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
16702 break;
16703 default:
16704 return;
16705 }
16706 $context['pluggable_data'] = $context;
16707 $context['response_type'] = 'live';
16708 } elseif ( empty( $result ) ) {
16709 switch ( $trigger ) {
16710 case 'sale_added':
16711 $context = json_decode(
16712 '{"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": "",
16713 "city": "london","state": "","zip": "","country": "UK","tax_id_us": "","tax_id_int": "","is_affiliate": "1","is_blocked": "","blocked_message": "","referrer": "0","notes": "","unsubscribed": ""}',
16714 true
16715 );
16716 break;
16717 case 'sale_recorded':
16718 $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 );
16719 break;
16720 case 'payout_made':
16721 $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 );
16722 break;
16723 default:
16724 return;
16725 }
16726 $context['pluggable_data'] = $context;
16727 $context['response_type'] = 'sample';
16728 }
16729
16730 return $context;
16731
16732 }
16733
16734 /**
16735 * Get WooCommerce Subscriptions Coupon list
16736 *
16737 * @param array $data data.
16738 *
16739 * @return array
16740 */
16741 public function search_woo_subscription_coupon_list( $data ) {
16742 $options = [];
16743
16744 $coupon_codes = [];
16745
16746 $query = new \WP_Query(
16747 [
16748 'post_type' => 'shop_coupon',
16749 'posts_per_page' => -1,
16750 'no_found_rows' => true,
16751 'meta_query' => [
16752 [
16753 'key' => '_is_aw_coupon',
16754 'compare' => 'NOT EXISTS',
16755 ],
16756 ],
16757 ]
16758 );
16759
16760 foreach ( $query->posts as $coupon ) {
16761 /**
16762 *
16763 * Ignore line
16764 *
16765 * @phpstan-ignore-next-line
16766 */
16767 $code = wc_format_coupon_code( $coupon->post_title );
16768 $coupon_codes[ $code ] = $code;
16769 }
16770
16771 foreach ( $coupon_codes as $code ) {
16772
16773 $coupon = new \WC_Coupon( $code );
16774
16775 if ( ! in_array( $coupon->get_discount_type(), [ 'recurring_fee', 'recurring_percent' ], true ) ) {
16776 unset( $coupon_codes[ $code ] );
16777 }
16778 }
16779
16780 if ( ! empty( $coupon_codes ) ) {
16781 if ( is_array( $coupon_codes ) ) {
16782 foreach ( $coupon_codes as $coupon_code ) {
16783 $options[] = [
16784 'label' => $coupon_code,
16785 'value' => $coupon_code,
16786 ];
16787 }
16788 }
16789 }
16790 return [
16791 'options' => $options,
16792 'hasMore' => false,
16793 ];
16794 }
16795
16796 /**
16797 * Get BuddyBoss field.
16798 *
16799 * @param array $data data.
16800 *
16801 * @return array|void
16802 */
16803 public function search_bb_field_list( $data ) {
16804 global $wpdb;
16805 $options = [];
16806
16807 $xprofile_fields = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_xprofile_fields WHERE parent_id = 0 ORDER BY field_order ASC" );
16808 if ( ! empty( $xprofile_fields ) ) {
16809 foreach ( $xprofile_fields as $xprofile_field ) {
16810 $options[] = [
16811 'label' => $xprofile_field->name,
16812 'value' => $xprofile_field->id,
16813 ];
16814 }
16815 }
16816
16817 return [
16818 'options' => $options,
16819 'hasMore' => false,
16820 ];
16821 }
16822
16823 /**
16824 * Get Woocommerce Bookings Product List.
16825 *
16826 * @param array $data data.
16827 *
16828 * @return array|void
16829 */
16830 public function search_wb_bookable_product_list( $data ) {
16831 $options = [];
16832
16833 if ( ! class_exists( 'WC_Bookings_Admin' ) ) {
16834 return;
16835 }
16836
16837 $products = WC_Bookings_Admin::get_booking_products();
16838 if ( ! empty( $products ) ) {
16839 if ( is_array( $products ) ) {
16840 foreach ( $products as $product ) {
16841 $options[] = [
16842 'label' => $product->get_name(),
16843 'value' => $product->get_id(),
16844 ];
16845 $resources = $product->get_resources();
16846 if ( ! empty( $resources ) ) {
16847 foreach ( $resources as $resource ) {
16848 $options[] = [
16849 'label' => $resource->get_name(),
16850 'value' => $resource->get_id(),
16851 ];
16852 }
16853 }
16854 }
16855 }
16856 }
16857
16858 return [
16859 'options' => $options,
16860 'hasMore' => false,
16861 ];
16862 }
16863
16864 /**
16865 * Get last data for WooCommerce Booking trigger
16866 *
16867 * @param array $data data.
16868 * @return array
16869 */
16870 public function search_woo_commerce_booking_last_data( $data ) {
16871 $context = [];
16872 $context['response_type'] = 'sample';
16873 $context['pluggable_data'] = [];
16874 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
16875 global $wpdb;
16876 if ( 'booking_created' === $term ) {
16877 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' AND post_status='confirmed' order by ID DESC LIMIT 1" );
16878
16879 if ( ! empty( $results ) ) {
16880 if ( class_exists( 'WC_Booking' ) ) {
16881 $booking = new WC_Booking( $results[0]->ID );
16882 $person_counts = $booking->get_person_counts();
16883 $bookable_product_id = $booking->get_product_id();
16884 if ( method_exists( $booking, 'get_data' ) ) {
16885 $booking = $booking->get_data();
16886 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
16887 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
16888 if ( ! empty( $person_counts ) ) {
16889 $total_count = 0;
16890 foreach ( $person_counts as $key => $value ) {
16891 $total_count += $value;
16892 }
16893 $booking['total_person_counts'] = $total_count;
16894 }
16895 $booking['bookable_product'] = $bookable_product_id;
16896 $context['response_type'] = 'live';
16897 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
16898 }
16899 }
16900 } else {
16901 $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 );
16902 $context['pluggable_data'] = $booking_sample_data;
16903 }
16904 } elseif ( 'booking_status_changed' === $term ) {
16905 $to_status = isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1;
16906
16907 if ( -1 == $to_status ) {
16908 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' order by ID DESC LIMIT 1" );
16909 } else {
16910 $results = $wpdb->get_results(
16911 $wpdb->prepare(
16912 "
16913 SELECT *
16914 FROM {$wpdb->prefix}posts
16915 WHERE `post_status` = %s
16916 AND `post_type` LIKE %s
16917 ",
16918 $to_status,
16919 'wc_booking'
16920 )
16921 );
16922 }
16923
16924 if ( ! empty( $results ) ) {
16925 if ( class_exists( 'WC_Booking' ) ) {
16926 $booking = new WC_Booking( $results[0]->ID );
16927 $person_counts = $booking->get_person_counts();
16928 $bookable_product_id = $booking->get_product_id();
16929 if ( method_exists( $booking, 'get_data' ) ) {
16930 $booking = $booking->get_data();
16931 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
16932 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
16933 if ( ! empty( $person_counts ) ) {
16934 $total_count = 0;
16935 foreach ( $person_counts as $key => $value ) {
16936 $total_count += $value;
16937 }
16938 $booking['total_person_counts'] = $total_count;
16939 }
16940 $booking['bookable_product'] = $bookable_product_id;
16941 $booking['from_status'] = $data['filter']['from_status']['value'];
16942 $booking['to_status'] = $booking['status'];
16943 $context['response_type'] = 'live';
16944 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
16945 }
16946 }
16947 } else {
16948 $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 );
16949 $context['pluggable_data'] = $booking_sample_data;
16950 }
16951 }
16952 return $context;
16953 }
16954
16955 /**
16956 * Get WooCommerce Booking status list.
16957 *
16958 * @param array $data data.
16959 *
16960 * @return array
16961 */
16962 public function search_woo_booking_status_list( $data ) {
16963
16964 $options = [
16965 [
16966 'label' => 'Pending Confirmation',
16967 'value' => 'pending-confirmation',
16968 ],
16969 [
16970 'label' => 'Unpaid',
16971 'value' => 'unpaid',
16972 ],
16973 [
16974 'label' => 'Confirmed',
16975 'value' => 'confirmed',
16976 ],
16977 [
16978 'label' => 'Paid',
16979 'value' => 'paid',
16980 ],
16981 [
16982 'label' => 'Complete',
16983 'value' => 'complete',
16984 ],
16985 [
16986 'label' => 'In Cart',
16987 'value' => 'in-cart',
16988 ],
16989 [
16990 'label' => 'Cancelled',
16991 'value' => 'cancelled',
16992 ],
16993 ];
16994
16995 return [
16996 'options' => $options,
16997 'hasMore' => false,
16998 ];
16999 }
17000
17001 /**
17002 * Get Woocommerce Memberships Status List.
17003 *
17004 * @param array $data data.
17005 *
17006 * @return array|void
17007 */
17008 public function search_wc_membership_status_list( $data ) {
17009 $options = [];
17010 if ( ! function_exists( 'wc_memberships_get_user_membership_statuses' ) ) {
17011 return;
17012 }
17013
17014 $statuses = wc_memberships_get_user_membership_statuses();
17015 if ( ! empty( $statuses ) ) {
17016 if ( is_array( $statuses ) ) {
17017 foreach ( $statuses as $status => $value ) {
17018 $status = 0 === strpos( $status, 'wcm-' ) ? substr( $status, 4 ) : $status;
17019 $options[] = [
17020 'label' => $value['label'],
17021 'value' => $status,
17022 ];
17023 }
17024 }
17025 }
17026
17027 return [
17028 'options' => $options,
17029 'hasMore' => false,
17030 ];
17031 }
17032
17033 /**
17034 * Search Woocommerce Subscription data.
17035 *
17036 * @param array $data data.
17037 * @return array|void
17038 */
17039 public function search_user_subscription_last_data( $data ) {
17040
17041 $context = [];
17042 if (
17043 ! class_exists( 'WC_Subscription' ) ||
17044 ! function_exists( 'wcs_get_subscription' ) ||
17045 ! function_exists( 'wcs_get_subscriptions_for_product' ) ||
17046 ! function_exists( 'wcs_order_contains_renewal' ) ||
17047 ! function_exists( 'wcs_get_subscriptions_for_order' )
17048 ) {
17049 return;
17050 }
17051
17052 if ( 'trial_end' == $data['search_term'] ) {
17053 $subscription_id = $data['filter']['subscription']['value'];
17054 $query_args = [
17055 'post_type' => 'shop_subscription',
17056 'orderby' => 'ID',
17057 'order' => 'DESC',
17058 'post_status' => 'wc-active',
17059 'posts_per_page' => 1,
17060 'meta_query' => [
17061 [
17062 'key' => '_schedule_trial_end',
17063 'value' => gmdate( 'Y-m-d H:i:s' ),
17064 'compare' => '<=',
17065 ],
17066 ],
17067 'post__in' => [ $subscription_id ],
17068 ];
17069 $query_result = new WP_Query( $query_args );
17070 $subscription_orders = $query_result->get_posts();
17071
17072 if ( ! empty( $subscription_orders ) ) {
17073 /**
17074 *
17075 * Ignore line
17076 *
17077 * @phpstan-ignore-next-line
17078 */
17079 $subscription = wcs_get_subscription( $subscription_orders[0]->ID );
17080 $user_id = $subscription->get_user_id();
17081
17082 $items = $subscription->get_items();
17083 $product_ids = [];
17084 foreach ( $items as $item ) {
17085 $product_ids[] = $item->get_product_id();
17086 }
17087
17088 $subscription_status = $subscription->get_status();
17089 $subscription_start_date = $subscription->get_date_created();
17090
17091 $context['subscription_data'] = [
17092 'status' => $subscription_status,
17093 'start_date' => $subscription_start_date,
17094 'trial_end_date' => $subscription->get_date( 'schedule_trial_end' ),
17095 'next_payment_date' => $subscription->get_date( 'next_payment' ),
17096 'end_date' => $subscription->get_date( 'schedule_end' ),
17097 ];
17098 $context['user'] = WordPress::get_user_context( $user_id );
17099
17100 foreach ( $product_ids as $val ) {
17101 $context['subscription'] = $val;
17102 $context['subscription_name'] = get_the_title( $val );
17103 }
17104 $context['pluggable_data'] = $context;
17105 $context['response_type'] = 'live';
17106 } else {
17107 $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 );
17108 $context['pluggable_data'] = $context;
17109 $context['response_type'] = 'sample';
17110 }
17111 } elseif ( 'renewal_payment_failed' == $data['search_term'] ) {
17112 $subscription_item = $data['filter']['subscription_item']['value'];
17113 $subscription_ids = wcs_get_subscriptions_for_product( $subscription_item );
17114 $ids = [];
17115 $related = [];
17116 foreach ( $subscription_ids as $subscription ) {
17117 $ids[] = $subscription;
17118 $subscriptions = new WC_Subscription( $subscription );
17119 $related[] = $subscriptions->get_related_orders();
17120 }
17121
17122 $failed_payment_orders = [];
17123 foreach ( $related as $value ) {
17124 foreach ( $value as $val ) {
17125 if ( wcs_order_contains_renewal( $val ) ) {
17126 $order = wc_get_order( $val );
17127 if ( $order ) {
17128 /**
17129 *
17130 * Ignore line
17131 *
17132 * @phpstan-ignore-next-line
17133 */
17134 $payment_status = $order->get_status();
17135 if ( 'failed' === $payment_status || 'wc-on-hold' === $payment_status ) {
17136 $failed_payment_orders[] = $val;
17137 }
17138 }
17139 }
17140 }
17141 }
17142
17143 if ( ! empty( $failed_payment_orders ) ) {
17144 $failed = array_rand( $failed_payment_orders );
17145 $related_subscriptions = wcs_get_subscriptions_for_order( $failed_payment_orders[ $failed ] );
17146 if ( ! empty( $related_subscriptions ) ) {
17147 $sub_id = get_post_meta( $failed_payment_orders[ $failed ], '_subscription_renewal', true );
17148 $subscription = wcs_get_subscription( $sub_id );
17149
17150 $items = $subscription->get_items();
17151 $product_ids = [];
17152 foreach ( $items as $item ) {
17153 $product_ids[] = $item->get_product_id();
17154 }
17155
17156 $context['user'] = WordPress::get_user_context( $subscription->get_user_id() );
17157 foreach ( [ 'renewal' ] as $order_type ) {
17158 foreach ( $subscription->get_related_orders( 'ids', $order_type ) as $order_id ) {
17159 $context['subscription_related_order'][] = $order_id;
17160 }
17161 }
17162 foreach ( $product_ids as $val ) {
17163 $context['subscription_item'] = $val;
17164 $context['subscription_name'] = get_the_title( $val );
17165 }
17166 $context['data'] = $subscription->get_data();
17167 $context['pluggable_data'] = $context;
17168 $context['response_type'] = 'live';
17169 } else {
17170 $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 );
17171 $context['response_type'] = 'sample';
17172 }
17173 } else {
17174 $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 );
17175 $context['response_type'] = 'sample';
17176 }
17177 }
17178 return $context;
17179 }
17180
17181 /**
17182 * Get Masteriyo LMS Courses.
17183 *
17184 * @param array $data data.
17185 *
17186 * @return array
17187 */
17188 public function search_masteriyo_lms_courses( $data ) {
17189
17190 $page = $data['page'];
17191 $limit = Utilities::get_search_page_limit();
17192 $offset = $limit * ( $page - 1 );
17193
17194 $args = [
17195 'post_type' => 'mto-course',
17196 'posts_per_page' => $limit,
17197 'offset' => $offset,
17198 'orderby' => 'title',
17199 'order' => 'ASC',
17200 'post_status' => 'publish',
17201 ];
17202
17203 $courses = get_posts( $args );
17204
17205 $course_count = wp_count_posts( 'mto-course' )->publish;
17206
17207 $options = [];
17208 if ( ! empty( $courses ) ) {
17209 if ( is_array( $courses ) ) {
17210 foreach ( $courses as $course ) {
17211 $options[] = [
17212 'label' => $course->post_title,
17213 'value' => $course->ID,
17214 ];
17215 }
17216 }
17217 }
17218 return [
17219 'options' => $options,
17220 'hasMore' => $course_count > $limit && $course_count > $offset,
17221 ];
17222 }
17223
17224 /**
17225 * Get Masteriyo LMS Lessons.
17226 *
17227 * @param array $data data.
17228 *
17229 * @return array
17230 */
17231 public function search_masteriyo_lms_lessons( $data ) {
17232
17233 $course_id = $data['dynamic'];
17234 $page = $data['page'];
17235 $limit = Utilities::get_search_page_limit();
17236 $offset = $limit * ( $page - 1 );
17237 $options = [];
17238 $args = [
17239 'post_type' => 'mto-lesson',
17240 'posts_per_page' => $limit,
17241 'offset' => $offset,
17242 'orderby' => 'title',
17243 'order' => 'ASC',
17244 'post_status' => 'publish',
17245 'meta_query' => [
17246 [
17247 'key' => '_course_id',
17248 'value' => $course_id,
17249 'compare' => '=',
17250 ],
17251 ],
17252 ];
17253
17254 $lessons = get_posts( $args );
17255 $lesson_count = wp_count_posts( 'mto-lesson' )->publish;
17256 if ( ! empty( $lessons ) ) {
17257 if ( is_array( $lessons ) ) {
17258 foreach ( $lessons as $lesson ) {
17259 $options[] = [
17260 'label' => $lesson->post_title,
17261 'value' => $lesson->ID,
17262 ];
17263 }
17264 }
17265 }
17266 return [
17267 'options' => $options,
17268 'hasMore' => $lesson_count > $limit && $lesson_count > $offset,
17269 ];
17270 }
17271
17272 /**
17273 * Get Masteriyo LMS Quiz.
17274 *
17275 * @param array $data data.
17276 *
17277 * @return array
17278 */
17279 public function search_masteriyo_lms_quizs( $data ) {
17280
17281 $course_id = $data['dynamic'];
17282 $page = $data['page'];
17283 $limit = Utilities::get_search_page_limit();
17284 $offset = $limit * ( $page - 1 );
17285 $options = [];
17286 $args = [
17287 'post_type' => 'mto-quiz',
17288 'posts_per_page' => $limit,
17289 'offset' => $offset,
17290 'orderby' => 'title',
17291 'order' => 'ASC',
17292 'post_status' => 'publish',
17293 'meta_query' => [
17294 [
17295 'key' => '_course_id',
17296 'value' => $course_id,
17297 'compare' => '=',
17298 ],
17299 ],
17300 ];
17301
17302 $quizs = get_posts( $args );
17303 $quiz_count = wp_count_posts( 'mto-quiz' )->publish;
17304 if ( ! empty( $quizs ) ) {
17305 if ( is_array( $quizs ) ) {
17306 foreach ( $quizs as $quiz ) {
17307 $options[] = [
17308 'label' => $quiz->post_title,
17309 'value' => $quiz->ID,
17310 ];
17311 }
17312 }
17313 }
17314 return [
17315 'options' => $options,
17316 'hasMore' => $quiz_count > $limit && $quiz_count > $offset,
17317 ];
17318 }
17319
17320 /**
17321 * Search Masteriyo LMS data.
17322 *
17323 * @param array $data data.
17324 * @return array|void|mixed
17325 */
17326 public function search_masteriyo_lms_last_data( $data ) {
17327 global $wpdb;
17328 $post_type = $data['post_type'];
17329 $trigger = $data['search_term'];
17330 $context = [];
17331
17332 $post_id = -1;
17333 if ( 'course_completed' === $trigger ) {
17334 $post_id = $data['filter']['course_id']['value'];
17335 } elseif ( 'lesson_completed' === $trigger ) {
17336 $post_id = $data['filter']['lesson_id']['value'];
17337 } elseif ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
17338 $post_id = $data['filter']['quiz_id']['value'];
17339 }
17340
17341 if ( 'course_completed' === $trigger || 'lesson_completed' === $trigger ) {
17342 if ( -1 === $post_id ) {
17343 $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 ) );
17344 } else {
17345 $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 ) );
17346 }
17347 }
17348
17349 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
17350 if ( -1 === $post_id ) {
17351 $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 ) );
17352 } else {
17353 $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 ) );
17354 }
17355 if ( ! empty( $result ) ) {
17356 $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 ) );
17357
17358 if ( function_exists( 'masteriyo_get_quiz' ) ) {
17359 $quiz = masteriyo_get_quiz( $resultt[0]->quiz_id );
17360 if ( is_object( $quiz ) && method_exists( $quiz, 'get_pass_mark' ) ) {
17361 if ( 'quiz_completed' === $trigger && $resultt[0]->earned_marks < $quiz->get_pass_mark() ) {
17362 $result = [];
17363 } elseif ( 'quiz_failed' === $trigger && $resultt[0]->earned_marks > $quiz->get_pass_mark() ) {
17364 $result = [];
17365 }
17366 }
17367 }
17368 }
17369 }
17370
17371 if ( ! empty( $result ) ) {
17372 $result_item_id = $result[0]->item_id;
17373 $result_user_id = $result[0]->user_id;
17374 $quiz_attempt = '';
17375 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
17376 $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 ) );
17377 $quiz_attempt = $resultt[0]->id;
17378 }
17379 switch ( $trigger ) {
17380 case 'course_completed':
17381 if ( function_exists( 'masteriyo_get_course' ) ) {
17382 $course = masteriyo_get_course( $result_item_id );
17383 $context_data = array_merge(
17384 WordPress::get_user_context( $result_user_id ),
17385 $course->get_data()
17386 );
17387 $context_data['course_id'] = $result_item_id;
17388 }
17389 break;
17390 case 'lesson_completed':
17391 if ( function_exists( 'masteriyo_get_lesson' ) ) {
17392 $lesson = masteriyo_get_lesson( $result_item_id );
17393 $context_data = array_merge(
17394 WordPress::get_user_context( $result_user_id ),
17395 $lesson->get_data()
17396 );
17397 $context_data['lesson_id'] = $result_item_id;
17398 }
17399 break;
17400 case 'quiz_completed':
17401 case 'quiz_failed':
17402 if ( function_exists( 'masteriyo_get_quiz' ) ) {
17403 $quiz = masteriyo_get_quiz( $result_item_id );
17404 $context_data = WordPress::get_user_context( $result_user_id );
17405 if ( function_exists( 'masteriyo_get_quiz_attempt' ) ) {
17406 $attempt = masteriyo_get_quiz_attempt( $quiz_attempt );
17407 $context_data['quiz_id'] = $result_item_id;
17408 $context_data['course_id'] = $quiz->get_course_id();
17409 $context_data['quiz'] = $quiz->get_data();
17410 $context_data['attempt'] = $attempt->get_data();
17411 }
17412 }
17413 break;
17414 default:
17415 return;
17416 }
17417 if ( ! empty( $context_data ) ) {
17418 $context['pluggable_data'] = $context_data;
17419 $context['response_type'] = 'live';
17420 }
17421 } elseif ( empty( $result ) ) {
17422 switch ( $trigger ) {
17423 case 'course_completed':
17424 $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"} ';
17425 break;
17426 case 'lesson_completed':
17427 $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"} ';
17428 break;
17429 case 'quiz_completed':
17430 case 'quiz_failed':
17431 $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"}';
17432 break;
17433 default:
17434 return;
17435 }
17436 $context = json_decode( $sample_data, true );
17437 }
17438
17439 return $context;
17440 }
17441
17442 /**
17443 * Search learndash user added in group.
17444 *
17445 * @param array $data data.
17446 * @return array|void
17447 */
17448 public function search_pluggables_user_added_in_group( $data ) {
17449 $context = [];
17450
17451 if ( ! function_exists( 'learndash_get_groups_user_ids' ) ) {
17452 return;
17453 }
17454 $term = $data['search_term'];
17455 $group_id = $data['filter']['sfwd_group_id']['value'];
17456
17457 if ( 'user_added_group' == $term ) {
17458 if ( -1 === $group_id ) {
17459 $args = [
17460 'numberposts' => 1,
17461 'orderby' => 'rand',
17462 'post_type' => 'groups',
17463 ];
17464 $posts = get_posts( $args );
17465 $random_value = $posts[0]->ID;
17466 $group_users = learndash_get_groups_user_ids( $random_value );
17467 $group_id = $random_value;
17468 } else {
17469 $group_users = learndash_get_groups_user_ids( $group_id );
17470 }
17471 $users = get_users(
17472 [
17473 'meta_key' => 'learndash_group_' . $group_id . '_enrolled_at',
17474 'orderby' => 'meta_value',
17475 'order' => 'DESC',
17476 'number' => 1,
17477 'include' => $group_users,
17478 ]
17479 );
17480 } elseif ( 'user_removed_group' == $term ) {
17481 if ( -1 === $group_id ) {
17482 $args = [
17483 'numberposts' => 1,
17484 'orderby' => 'rand',
17485 'post_type' => 'groups',
17486 ];
17487 $posts = get_posts( $args );
17488 $random_value = $posts[0]->ID;
17489 $group_id = $random_value;
17490 }
17491 $args = [
17492 'meta_query' => [
17493 'relation' => 'AND',
17494 [
17495 'key' => 'group_' . $group_id . '_access_from',
17496 'compare' => 'NOT EXISTS',
17497 ],
17498 [
17499 'key' => 'learndash_group_' . $group_id . '_enrolled_at',
17500 'compare' => 'EXISTS',
17501 ],
17502 ],
17503 'orderby' => 'meta_value',
17504 'order' => 'DESC',
17505 'number' => 1,
17506 ];
17507 $users = get_users( $args );
17508 } elseif ( 'leader_added_group' == $term || 'leader_removed_group' == $term ) {
17509 if ( -1 === $group_id ) {
17510 $args = [
17511 'numberposts' => 1,
17512 'fields' => 'ids',
17513 'orderby' => 'rand',
17514 'post_type' => 'groups',
17515 ];
17516 $posts = get_posts( $args );
17517 $random_key = array_rand( $posts );
17518 $random_value = $posts[ $random_key ];
17519 $group_id = $random_value;
17520 }
17521 $user_query_args = [
17522 'orderby' => 'learndash_group_leaders_' . intval( $group_id ),
17523 'order' => 'DESC',
17524 'meta_query' => [
17525 [
17526 'key' => 'learndash_group_leaders_' . intval( $group_id ),
17527 'value' => intval( $group_id ),
17528 'compare' => '=',
17529 'type' => 'NUMERIC',
17530 ],
17531 ],
17532 ];
17533 $users = get_users( $user_query_args );
17534 }
17535
17536 if ( ! empty( $users ) ) {
17537 $context = WordPress::get_user_context( $users[0]->ID );
17538 $context['sfwd_group_id'] = $group_id;
17539 $context['group_title'] = get_the_title( $group_id );
17540 $context['group_url'] = get_permalink( $group_id );
17541 $context['group_featured_image_id'] = get_post_meta( $group_id, '_thumbnail_id', true );
17542 $context['group_featured_image_url'] = get_the_post_thumbnail_url( $group_id );
17543 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
17544 $group_courses_id = learndash_group_enrolled_courses( $group_id );
17545 if ( ! empty( $group_courses_id ) ) {
17546 foreach ( $group_courses_id as $key => $course_id ) {
17547 $context['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
17548 }
17549 }
17550 }
17551 $context['response_type'] = 'live';
17552 } else {
17553 $context = WordPress::get_sample_user_context();
17554 $context['group_title'] = 'Test Group';
17555 $context['sfwd_group_id'] = 112;
17556 $context['group_url'] = 'https://example.com/test-group';
17557 $context['group_featured_image_id'] = 113;
17558 $context['group_featured_image_url'] = 'https://example.com/test-group-img';
17559 $context['group_courses'] = [
17560 [
17561 'ID' => 7915,
17562 'title' => 'Example Course',
17563 'URL' => 'https://example.com/courses/example-course/',
17564 'status' => 'publish',
17565 'featured_image_id' => '',
17566 'featured_image_url' => false,
17567 ],
17568 ];
17569 $context['response_type'] = 'sample';
17570 }
17571
17572 $context['pluggable_data'] = $context;
17573 return $context;
17574 }
17575
17576 /**
17577 * Search learndash user enrolled in course.
17578 *
17579 * @param array $data data.
17580 * @return array|void
17581 */
17582 public function search_pluggables_user_enrolled_course( $data ) {
17583 global $wpdb;
17584 $context = [];
17585
17586 if ( ! function_exists( 'ld_course_access_expires_on' ) ||
17587 ! function_exists( 'learndash_group_enrolled_courses' ) ||
17588 ! function_exists( 'learndash_get_lesson_list' ) ) {
17589 return;
17590 }
17591 $term = $data['search_term'];
17592 $course_id = isset( $data['filter']['sfwd_course_id'] ) ? $data['filter']['sfwd_course_id']['value'] : '';
17593 $group_id = isset( $data['filter']['sfwd_group_id'] ) ? $data['filter']['sfwd_group_id']['value'] : '';
17594
17595 if ( 'course_enrolled' == $term ) {
17596 if ( -1 === $course_id ) {
17597 $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 ) );
17598 } else {
17599 $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 ) );
17600 }
17601 } elseif ( 'course_unenrolled' == $term ) {
17602 if ( -1 === $course_id ) {
17603 $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 ) );
17604 $course_id = $courses[0]->course_id;
17605 }
17606 $args = [
17607 'meta_query' => [
17608 'relation' => 'AND',
17609 [
17610 'key' => 'course_' . $course_id . '_access_from',
17611 'compare' => 'NOT EXISTS',
17612 ],
17613 [
17614 'key' => 'learndash_course_' . $course_id . '_enrolled_at',
17615 'compare' => 'EXISTS',
17616 ],
17617 ],
17618 ];
17619 $users = get_users( $args );
17620 } elseif ( 'course_access_expired' == $term ) {
17621 if ( -1 === $course_id ) {
17622 $course_args = [
17623 'post_type' => 'sfwd-courses',
17624 'posts_per_page' => 1,
17625 'orderby' => 'rand',
17626 'order' => 'ASC',
17627 'post_status' => 'publish',
17628 ];
17629 $courses_posts = get_posts( $course_args );
17630 $course_id = $courses_posts[0]->ID;
17631 }
17632 $args = [
17633 'meta_query' => [
17634 [
17635 'key' => 'learndash_course_expired_' . $course_id,
17636 'compare' => 'EXISTS',
17637 ],
17638 ],
17639 ];
17640 $users = get_users( $args );
17641 } elseif ( 'group_course_completed' == $term ) {
17642 if ( -1 === $group_id ) {
17643 $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 ) );
17644 } else {
17645 $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 ) );
17646 }
17647 $activity_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity_meta WHERE activity_id = %d", $courses[0]->activity_id ) );
17648 } elseif ( 'course_group_added' == $term ) {
17649 if ( -1 === $group_id ) {
17650 $args = [
17651 'numberposts' => 1,
17652 'fields' => 'ids',
17653 'orderby' => 'rand',
17654 'post_type' => 'groups',
17655 ];
17656 $posts = get_posts( $args );
17657 $random_key = array_rand( $posts );
17658 $random_value = $posts[ $random_key ];
17659 $group_id = $random_value;
17660 }
17661 $courses = learndash_group_enrolled_courses( $group_id );
17662 } elseif ( 'assignment_submitted' == $term ) {
17663 $course_id = isset( $data['filter']['sfwd-courses'] ) ? $data['filter']['sfwd-courses']['value'] : '';
17664 $lesson_id = $data['filter']['sfwd_lesson_topic_id']['value'];
17665 if ( -1 === $course_id && -1 === $lesson_id ) {
17666 $args = [
17667 'post_type' => 'sfwd-assignment',
17668 'posts_per_page' => 1,
17669 'order' => 'DESC',
17670 'post_status' => 'publish',
17671 ];
17672 } else {
17673 if ( -1 === $course_id ) {
17674 $courses = get_posts(
17675 [
17676 'posts_per_page' => - 1,
17677 'post_type' => 'sfwd-courses',
17678 'post_status' => 'publish',
17679 'fields' => 'ids',
17680 ]
17681 );
17682 $course_key = array_rand( $courses );
17683 $course_id = $courses[ $course_key ];
17684 }
17685 if ( -1 === $lesson_id ) {
17686 $args = [
17687 'post_type' => 'sfwd-assignment',
17688 'posts_per_page' => 1,
17689 'order' => 'DESC',
17690 'post_status' => 'publish',
17691 'meta_query' => [
17692 [
17693 'key' => 'course_id',
17694 'value' => $course_id,
17695 'compare' => '=',
17696 ],
17697 ],
17698 ];
17699 } else {
17700 $args = [
17701 'post_type' => 'sfwd-assignment',
17702 'posts_per_page' => 1,
17703 'order' => 'DESC',
17704 'post_status' => 'publish',
17705 'meta_query' => [
17706 'relation' => 'AND',
17707 [
17708 'key' => 'lesson_id',
17709 'value' => $lesson_id,
17710 'compare' => '=',
17711 ],
17712 [
17713 'key' => 'course_id',
17714 'value' => $course_id,
17715 'compare' => '=',
17716 ],
17717 ],
17718 ];
17719 }
17720 }
17721 $assignments = get_posts( $args );
17722 } elseif ( 'assignment_graded' == $term ) {
17723 $course_id = isset( $data['filter']['sfwd-courses'] ) ? $data['filter']['sfwd-courses']['value'] : '';
17724 $lesson_id = $data['filter']['sfwd_lesson_topic_id']['value'];
17725 if ( -1 === $course_id && -1 === $lesson_id ) {
17726 $args = [
17727 'post_type' => 'sfwd-assignment',
17728 'posts_per_page' => 1,
17729 'order' => 'DESC',
17730 'post_status' => 'publish',
17731 'meta_query' => [
17732 [
17733 'key' => 'approval_status',
17734 'compare' => 'EXISTS',
17735 ],
17736 ],
17737 ];
17738 } else {
17739 if ( -1 === $course_id ) {
17740 $courses = get_posts(
17741 [
17742 'posts_per_page' => - 1,
17743 'post_type' => 'sfwd-courses',
17744 'post_status' => 'publish',
17745 'fields' => 'ids',
17746 ]
17747 );
17748 $course_key = array_rand( $courses );
17749 $course_id = $courses[ $course_key ];
17750 }
17751 if ( -1 === $lesson_id ) {
17752 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
17753 if ( ! empty( $lessons ) ) {
17754 $random_key = array_rand( $lessons );
17755 $random_value = $lessons[ $random_key ];
17756 $lesson_id = $random_value->ID;
17757 }
17758 }
17759 $args = [
17760 'post_type' => 'sfwd-assignment',
17761 'posts_per_page' => 1,
17762 'order' => 'DESC',
17763 'post_status' => 'publish',
17764 'meta_query' => [
17765 'relation' => 'AND',
17766 [
17767 'key' => 'lesson_id',
17768 'value' => $lesson_id,
17769 'compare' => '=',
17770 ],
17771 [
17772 'key' => 'course_id',
17773 'value' => $course_id,
17774 'compare' => '=',
17775 ],
17776 [
17777 'key' => 'approval_status',
17778 'compare' => 'EXISTS',
17779 ],
17780 ],
17781 ];
17782 }
17783 $assignments = get_posts( $args );
17784 }
17785
17786 if ( 'assignment_submitted' == $term || 'assignment_graded' == $term ) {
17787 if ( ! empty( $assignments ) ) {
17788 $context_data = WordPress::get_user_context( (int) $assignments[0]->post_author );
17789 $context_data['assignment_id'] = $assignments[0]->ID;
17790 $context_data['assignment_title'] = $assignments[0]->post_title;
17791 $context_data['assignment_url'] = get_post_meta( $assignments[0]->ID, 'file_link', true );
17792 $context_data['lesson_id'] = get_post_meta( $assignments[0]->ID, 'lesson_id', true );
17793 $context_data['course_id'] = get_post_meta( $assignments[0]->ID, 'course_id', true );
17794 $context_data['points'] = get_post_meta( $assignments[0]->ID, 'points', true );
17795 $context['response_type'] = 'live';
17796 } else {
17797 $context_data = WordPress::get_sample_user_context();
17798 $context_data['assignment_title'] = 'Test Assignment';
17799 $context_data['assignment_id'] = 112;
17800 $context_data['assignment_url'] = 'https://example.com/test_assignment.pdf';
17801 $context_data['lesson_id'] = 2;
17802 $context_data['course_id'] = 1;
17803 $context_data['points'] = '12';
17804 $context['response_type'] = 'sample';
17805 }
17806 } elseif ( 'course_unenrolled' == $term || 'course_access_expired' == $term ) {
17807 if ( ! empty( $users ) ) {
17808 $context_data = WordPress::get_user_context( $users[0]->ID );
17809 $context_data['sfwd_course_id'] = $course_id;
17810 $context_data['course_title'] = get_the_title( $course_id );
17811 $context_data['course_url'] = get_permalink( $course_id );
17812 $context_data['course_featured_image_id'] = get_post_meta( $course_id, '_thumbnail_id', true );
17813 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course_id );
17814 $timestamp = ld_course_access_expires_on( $course_id, $users[0]->ID );
17815 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
17816 $date_format = get_option( 'date_format' );
17817 if ( is_string( $date_format ) ) {
17818 $context_data['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
17819 }
17820 $context['response_type'] = 'live';
17821 } else {
17822 $context_data = WordPress::get_sample_user_context();
17823 $context_data['course_name'] = 'Test Course';
17824 $context_data['sfwd_course_id'] = 112;
17825 $context_data['course_url'] = 'https://example.com/test-course';
17826 $context_data['course_featured_image_id'] = 113;
17827 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
17828 $context_data['course_access_expiry_date'] = '2023-10-20';
17829 $context['response_type'] = 'sample';
17830 }
17831 } elseif ( 'course_group_added' == $term ) {
17832 if ( ! empty( $courses ) ) {
17833 $context_data['course_id'] = $courses[0];
17834 $context_data['course_title'] = get_the_title( $courses[0] );
17835 $context_data['course_url'] = get_permalink( $courses[0] );
17836 $context_data['course_featured_image_id'] = get_post_meta( $courses[0], '_thumbnail_id', true );
17837 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $courses[0] );
17838 $context_data['sfwd_group_id'] = $group_id;
17839 $context_data['group_name'] = get_the_title( $group_id );
17840 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
17841 $group_courses_id = learndash_group_enrolled_courses( $group_id );
17842 if ( ! empty( $group_courses_id ) ) {
17843 foreach ( $group_courses_id as $key => $course_id ) {
17844 $context_data['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
17845 }
17846 }
17847 }
17848 $context['response_type'] = 'live';
17849 } else {
17850 $context_data = WordPress::get_sample_user_context();
17851 $context_data['course_name'] = 'Test Course';
17852 $context_data['sfwd_course_id'] = 112;
17853 $context_data['course_url'] = 'https://example.com/test-course';
17854 $context_data['course_featured_image_id'] = 113;
17855 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
17856 $context_data['course_access_expiry_date'] = '2023-10-20';
17857 $context_data['sfwd_group_id'] = 12;
17858 $context_data['group_name'] = 'Test Group';
17859 $context_data['group_courses'] = [
17860 [
17861 'ID' => 7915,
17862 'title' => 'Example Course',
17863 'URL' => 'https://example.com/courses/example-course/',
17864 'status' => 'publish',
17865 'featured_image_id' => '',
17866 'featured_image_url' => false,
17867 ],
17868 ];
17869 $context['response_type'] = 'sample';
17870 }
17871 } elseif ( 'group_course_completed' == $term ) {
17872 if ( ! empty( $courses ) && ! empty( $activity_meta ) ) {
17873 $context_data = WordPress::get_user_context( $courses[0]->user_id );
17874 $context_data['sfwd_group_id'] = $courses[0]->post_id;
17875 $context_data['group_title'] = get_the_title( $courses[0]->post_id );
17876 $context_data['group_url'] = get_permalink( $courses[0]->post_id );
17877 $context_data['group_featured_image_id'] = get_post_meta( $courses[0]->post_id, '_thumbnail_id', true );
17878 $context_data['group_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->post_id );
17879 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
17880 $group_courses_id = learndash_group_enrolled_courses( $courses[0]->post_id );
17881 if ( ! empty( $group_courses_id ) ) {
17882 foreach ( $group_courses_id as $key => $course_id ) {
17883 $context_data['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
17884 }
17885 }
17886 }
17887 $course_ids = null;
17888 foreach ( $activity_meta as $item ) {
17889 if ( 'course_ids' === $item->activity_meta_key ) {
17890 $course_ids = unserialize( $item->activity_meta_value );
17891 break;
17892 }
17893 }
17894 if ( ! empty( $course_ids ) && is_array( $course_ids ) ) {
17895 foreach ( $course_ids as $key => $course_id ) {
17896 if ( is_numeric( $course_id ) ) {
17897 $course_id = (int) $course_id;
17898 $args = [
17899 'include' => [ $courses[0]->user_id ],
17900 'meta_query' => [
17901 [
17902 'key' => 'course_completed_' . $course_id,
17903 'compare' => 'EXISTS',
17904 ],
17905 ],
17906 ];
17907 $users = get_users( $args );
17908 if ( ! empty( $users ) ) {
17909 $context_data[ 'completed ' . $key ]['course_id'] = $course_id;
17910 $context_data[ 'completed ' . $key ]['course_title'] = get_the_title( $course_id );
17911 $context_data[ 'completed ' . $key ]['course_url'] = get_permalink( $course_id );
17912 $context_data[ 'completed ' . $key ]['course_featured_image_id'] = get_post_meta( $course_id, '_thumbnail_id', true );
17913 $context_data[ 'completed ' . $key ]['course_featured_image_url'] = get_the_post_thumbnail_url( $course_id );
17914 $timestamp = ld_course_access_expires_on( $course_id, $courses[0]->user_id );
17915 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
17916 $date_format = get_option( 'date_format' );
17917 if ( is_string( $date_format ) ) {
17918 $context_data[ 'completed ' . $key ]['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
17919 }
17920 }
17921 }
17922 }
17923 }
17924 $context['response_type'] = 'live';
17925 } else {
17926 $context_data = WordPress::get_sample_user_context();
17927 $context_data['sfwd_group_id'] = 112;
17928 $context_data['group_title'] = 'Test Group';
17929 $context_data['group_url'] = 113;
17930 $context_data['group_featured_image_id'] = 11;
17931 $context_data['group_featured_image_url'] = 'https://example.com/test-group-img';
17932 $context_data['completed 0'] = [
17933 'course_id' => 10,
17934 'course_title' => 'Test Course',
17935 'course_url' => 'https://example.com/test-course',
17936 'course_featured_image_id' => 14,
17937 'course_featured_image_url' => 'https://example.com/test-course-img',
17938 ];
17939 $context_data['group_courses'] = [
17940 [
17941 'ID' => 7915,
17942 'title' => 'Example Course',
17943 'URL' => 'https://example.com/courses/example-course/',
17944 'status' => 'publish',
17945 'featured_image_id' => '',
17946 'featured_image_url' => false,
17947 ],
17948 ];
17949 $context['response_type'] = 'sample';
17950 }
17951 } else {
17952 if ( ! empty( $courses ) ) {
17953 $context_data = WordPress::get_user_context( $courses[0]->user_id );
17954 $context_data['course_id'] = $courses[0]->course_id;
17955 $context_data['course_title'] = get_the_title( $courses[0]->course_id );
17956 $context_data['course_url'] = get_permalink( $courses[0]->course_id );
17957 $context_data['course_featured_image_id'] = get_post_meta( $courses[0]->course_id, '_thumbnail_id', true );
17958 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->course_id );
17959 $timestamp = ld_course_access_expires_on( $courses[0]->course_id, $courses[0]->user_id );
17960 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
17961 $date_format = get_option( 'date_format' );
17962 if ( is_string( $date_format ) ) {
17963 $context_data['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
17964 }
17965 if ( $courses[0]->post_id ) {
17966 $context_data['sfwd_group_id'] = $courses[0]->post_id;
17967 $context_data['group_title'] = get_the_title( $courses[0]->post_id );
17968 $context_data['group_url'] = get_permalink( $courses[0]->post_id );
17969 $context_data['group_featured_image_id'] = get_post_meta( $courses[0]->post_id, '_thumbnail_id', true );
17970 $context_data['group_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->post_id );
17971 }
17972 $context['response_type'] = 'live';
17973 } else {
17974 $context_data = WordPress::get_sample_user_context();
17975 $context_data['course_name'] = 'Test Course';
17976 $context_data['sfwd_course_id'] = 112;
17977 $context_data['course_url'] = 'https://example.com/test-course';
17978 $context_data['course_featured_image_id'] = 113;
17979 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
17980 $context_data['course_access_expiry_date'] = '2023-10-20';
17981 $context['response_type'] = 'sample';
17982 }
17983 }
17984
17985 $context['pluggable_data'] = $context_data;
17986 return $context;
17987 }
17988
17989 /**
17990 * Search LearnDash Lesson Topic List.
17991 *
17992 * @param array $data Search Params.
17993 *
17994 * @since 1.0.0
17995 *
17996 * @return array
17997 */
17998 public function search_ld_lessons_topics_list( $data ) {
17999 $options = [];
18000 $course_id = $data['dynamic']['sfwd-courses'];
18001
18002 if ( ! function_exists( 'learndash_get_lesson_list' ) || ! function_exists( 'learndash_get_topic_list' ) ) {
18003 return [];
18004 }
18005
18006 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
18007 foreach ( $lessons as $lesson ) {
18008 $options[] = [
18009 'label' => $lesson->post_title,
18010 'value' => $lesson->ID,
18011 ];
18012 $topics = learndash_get_topic_list( $lesson->ID, $course_id );
18013 foreach ( $topics as $topic ) {
18014 $options[] = [
18015 'label' => $topic->post_title,
18016 'value' => $topic->ID,
18017 ];
18018 }
18019 }
18020
18021 return [
18022 'options' => $options,
18023 'hasMore' => false,
18024 ];
18025 }
18026
18027 /**
18028 * Search LearnDash Quiz List.
18029 *
18030 * @param array $data Search Params.
18031 *
18032 * @since 1.0.0
18033 *
18034 * @return array
18035 */
18036 public function search_ld_quiz_list( $data ) {
18037 $options = [];
18038 $course_id = $data['dynamic']['sfwd-courses'];
18039 $lesson_id = $data['dynamic']['sfwd_lessons_topics'];
18040
18041 if ( ! function_exists( 'learndash_get_course_quiz_list' ) || ! function_exists( 'learndash_get_lesson_quiz_list' ) ) {
18042 return [];
18043 }
18044
18045 $quizzes = learndash_get_course_quiz_list( $course_id );
18046 $quizzes = array_merge( $quizzes, learndash_get_lesson_quiz_list( $lesson_id, null, $course_id ) );
18047 if ( ! empty( $quizzes ) ) {
18048 foreach ( $quizzes as $quiz ) {
18049 $options[] = [
18050 'label' => $quiz['post']->post_title,
18051 'value' => $quiz['post']->ID,
18052
18053 ];
18054 }
18055 }
18056
18057 return [
18058 'options' => $options,
18059 'hasMore' => false,
18060 ];
18061 }
18062
18063 /**
18064 * Search LearnDash Quiz Essay Question List.
18065 *
18066 * @param array $data Search Params.
18067 *
18068 * @since 1.0.0
18069 *
18070 * @return array
18071 */
18072 public function search_ld_quiz_essay_question_list( $data ) {
18073 $options = [];
18074 $quiz_id = $data['dynamic'];
18075
18076 if ( ! function_exists( 'learndash_get_quiz_questions' ) ) {
18077 return [];
18078 }
18079
18080 if ( 0 < $quiz_id ) {
18081 $quiz_question_ids = learndash_get_quiz_questions( $quiz_id );
18082 if ( ! empty( $quiz_question_ids ) ) {
18083 foreach ( $quiz_question_ids as $question_post_id => $question_pro_id ) {
18084 $question_type = get_post_meta( $question_post_id, 'question_type', true );
18085 if ( is_string( $question_type ) && 'essay' === $question_type ) {
18086 $title = html_entity_decode( get_the_title( $question_post_id ), ENT_QUOTES, 'UTF-8' );
18087 $options[] = [
18088 'label' => $title,
18089 'value' => $question_post_id,
18090 ];
18091 }
18092 }
18093 }
18094 }
18095
18096 return [
18097 'options' => $options,
18098 'hasMore' => false,
18099 ];
18100 }
18101
18102 /**
18103 * Search LearnDash Lessons List.
18104 *
18105 * @param array $data Search Params.
18106 *
18107 * @since 1.0.0
18108 *
18109 * @return array
18110 */
18111 public function search_ld_lessons_list( $data ) {
18112 $options = [];
18113 $course_id = $data['dynamic']['sfwd-courses'];
18114
18115 if ( ! function_exists( 'learndash_get_lesson_list' ) ) {
18116 return [];
18117 }
18118
18119 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
18120 foreach ( $lessons as $lesson ) {
18121 $options[] = [
18122 'label' => $lesson->post_title,
18123 'value' => $lesson->ID,
18124 ];
18125 }
18126 return [
18127 'options' => $options,
18128 'hasMore' => false,
18129 ];
18130 }
18131
18132 /**
18133 * Search LearnDash Topics List.
18134 *
18135 * @param array $data Search Params.
18136 *
18137 * @since 1.0.0
18138 *
18139 * @return array
18140 */
18141 public function search_ld_topics_list( $data ) {
18142 $options = [];
18143 $course_id = $data['dynamic']['sfwd-courses'];
18144 $lesson_id = $data['dynamic']['sfwd-lessons'];
18145
18146 if ( ! function_exists( 'learndash_get_topic_list' ) ) {
18147 return [];
18148 }
18149
18150 $topics = learndash_get_topic_list( $lesson_id, $course_id );
18151 foreach ( $topics as $topic ) {
18152 $options[] = [
18153 'label' => $topic->post_title,
18154 'value' => $topic->ID,
18155 ];
18156 }
18157 return [
18158 'options' => $options,
18159 'hasMore' => false,
18160 ];
18161 }
18162
18163 /**
18164 * Search LearnDash Assignments List.
18165 *
18166 * @param array $data Search Params.
18167 *
18168 * @since 1.0.0
18169 *
18170 * @return array
18171 */
18172 public function search_ld_assignments_list( $data ) {
18173 $options = [];
18174 $course_id = $data['dynamic']['sfwd-courses'];
18175 $lesson_id = $data['dynamic']['sfwd_lesson_topic_id'];
18176
18177 $args = [
18178 'post_type' => 'sfwd-assignment',
18179 'numberposts' => - 1,
18180 ];
18181 $meta_query = [];
18182 if ( ! empty( $course_id ) ) {
18183 $meta_query[] = [
18184 'key' => 'course_id',
18185 'value' => (int) $course_id,
18186 'compare' => '=',
18187 ];
18188 }
18189 if ( ! empty( $lesson_id ) ) {
18190 $meta_query[] = [
18191 'key' => 'lesson_id',
18192 'value' => (int) $lesson_id,
18193 'compare' => '=',
18194 ];
18195 }
18196 if ( ! empty( $meta_query ) ) {
18197 $args['meta_query'] = $meta_query;
18198 if ( count( $meta_query ) > 1 ) {
18199 $args['meta_query']['relation'] = 'AND';
18200 }
18201 }
18202 $assignments = get_posts( $args );
18203 foreach ( $assignments as $assignment ) {
18204 $options[] = [
18205 'label' => $assignment->post_title,
18206 'value' => $assignment->ID,
18207 ];
18208 }
18209 return [
18210 'options' => $options,
18211 'hasMore' => false,
18212 ];
18213 }
18214
18215 /**
18216 * Search post by post type.
18217 *
18218 * @param array $data Search Params.
18219 *
18220 * @since 1.0.0
18221 *
18222 * @return array
18223 */
18224 public function search_edd_prices( $data ) {
18225 $options = [];
18226 $downlaod_id = $data['dynamic']['download_id'];
18227
18228 $variable_prices = get_post_meta( $downlaod_id, 'edd_variable_prices', true );
18229 if ( ! empty( $variable_prices ) && is_array( $variable_prices ) ) {
18230 foreach ( $variable_prices as $price_id => $price ) {
18231 if ( isset( $price['name'] ) ) {
18232 $options[] = [
18233 'label' => $price['name'] . ' (' . $price['amount'] . ')',
18234 'value' => $price['index'],
18235 ];
18236 }
18237 }
18238 }
18239
18240 return [
18241 'options' => $options,
18242 'hasMore' => false,
18243 ];
18244
18245 }
18246
18247 /**
18248 * GetPowerful Docs list.
18249 *
18250 * @param array $data data.
18251 *
18252 * @return array
18253 */
18254 public function search_pfd_docs_list( $data ) {
18255
18256 $course_id = $data['dynamic'];
18257 $page = $data['page'];
18258 $limit = Utilities::get_search_page_limit();
18259 $offset = $limit * ( $page - 1 );
18260 $options = [];
18261 $args = [
18262 'post_type' => 'docs',
18263 'posts_per_page' => $limit,
18264 'offset' => $offset,
18265 'orderby' => 'title',
18266 'order' => 'ASC',
18267 'post_status' => 'publish',
18268 ];
18269
18270 $docs = get_posts( $args );
18271 $docs_count = wp_count_posts( 'docs' )->publish;
18272 if ( ! empty( $docs ) ) {
18273 if ( is_array( $docs ) ) {
18274 foreach ( $docs as $doc ) {
18275 $options[] = [
18276 'label' => $doc->post_title,
18277 'value' => $doc->ID,
18278 ];
18279 }
18280 }
18281 }
18282 return [
18283 'options' => $options,
18284 'hasMore' => $docs_count > $limit && $docs_count > $offset,
18285 ];
18286 }
18287
18288 /**
18289 * Search Powerful Docs last data.
18290 *
18291 * @param array $data data.
18292 * @return array|void|mixed
18293 */
18294 public function search_pfd_feedback_last_data( $data ) {
18295 $sample_data = '{"pluggable_data":{"wp_user_id":4587,"user_login":"alice.wright","display_name":"Alice Wright","user_firstname":"Alice","user_lastname":"Wright","user_email":"alice.wright@example.com","user_registered":"2024-11-15 09:30:00","user_role":["subscriber","editor"],"doc_id":"3245","feedback":"yes","questions":"— Can you explain the next steps for this document?","comment":"Looking forward to your feedback.","name":"Alice Wright","email":"alice.wright@example.com","doc_name":"Project Plan Q4","doc_link":"https://example.com/project-plan-q4","time":"2025-09-29 10:00:00","doc_author_email":"author@company.com"},"response_type":"sample"}';
18296 $context = json_decode( $sample_data, true );
18297
18298 return $context;
18299 }
18300
18301 /**
18302 * WooCommerce Coupon Discount type list.
18303 *
18304 * @param array $data data.
18305 *
18306 * @return array
18307 */
18308 public function search_woo_coupon_discount_type_list( $data ) {
18309 $options = [];
18310
18311 $discount_types = wc_get_coupon_types();
18312
18313 if ( ! empty( $discount_types ) ) {
18314 if ( is_array( $discount_types ) ) {
18315 foreach ( $discount_types as $key => $value ) {
18316 $options[] = [
18317 'label' => $value,
18318 'value' => $key,
18319 ];
18320 }
18321 }
18322 }
18323
18324 return [
18325 'options' => $options,
18326 'hasMore' => false,
18327 ];
18328 }
18329
18330 /**
18331 * WooCommerce Product list along with variation list.
18332 *
18333 * @param array $data data.
18334 *
18335 * @return array|void
18336 */
18337 public function search_woo_product_variation_list( $data ) {
18338 $options = [];
18339
18340 $page = $data['page'];
18341 $limit = Utilities::get_search_page_limit();
18342 $offset = $limit * ( $page - 1 );
18343
18344 if ( ! function_exists( 'wc_get_products' ) ) {
18345 return;
18346 }
18347 $products = wc_get_products(
18348 [
18349 'posts_per_page' => $limit,
18350 'offset' => $offset,
18351 'orderby' => 'date',
18352 'order' => 'DESC',
18353 ]
18354 );
18355
18356
18357 $prod_count = wp_count_posts( 'product' )->publish;
18358
18359 if ( ! empty( $products ) ) {
18360 if ( is_array( $products ) ) {
18361 foreach ( $products as $product ) {
18362 $options[] = [
18363 'label' => $product->get_name(),
18364 'value' => $product->get_id(),
18365 ];
18366 if ( $product->is_type( 'variable' ) ) {
18367 $variations = Utilities::get_product_variations( $product->get_id() );
18368 foreach ( $variations['result'] as $variation ) {
18369 $options[] = [
18370 'label' => $variation->post_title,
18371 'value' => $variation->ID,
18372 ];
18373 }
18374 }
18375 }
18376 }
18377 }
18378 return [
18379 'options' => $options,
18380 'hasMore' => $prod_count > $limit && $prod_count > $offset,
18381 ];
18382 }
18383
18384 /**
18385 * WooCommerce Product list along with variation list.
18386 *
18387 * @param array $data data.
18388 *
18389 * @return array
18390 */
18391 public function search_woo_coupon_list( $data ) {
18392 $options = [];
18393
18394 $page = $data['page'];
18395 $limit = Utilities::get_search_page_limit();
18396 $offset = $limit * ( $page - 1 );
18397
18398 $coupons = get_posts(
18399 [
18400 'posts_per_page' => - 1,
18401 'orderby' => 'name',
18402 'order' => 'asc',
18403 'post_type' => 'shop_coupon',
18404 'post_status' => 'publish',
18405 ]
18406 );
18407 $coupon_count = wp_count_posts( 'shop_coupon' )->publish;
18408
18409 if ( ! empty( $coupons ) ) {
18410 if ( is_array( $coupons ) ) {
18411 foreach ( $coupons as $coupon ) {
18412 $code = wc_format_coupon_code( $coupon->post_title );
18413 $options[] = [
18414 'label' => $code,
18415 'value' => $code,
18416 ];
18417 }
18418 }
18419 }
18420
18421 return [
18422 'options' => $options,
18423 'hasMore' => $coupon_count > $limit && $coupon_count > $offset,
18424 ];
18425 }
18426
18427 /**
18428 * Prepare LatePoint Bookings List.
18429 *
18430 * @param array $data Search Params.
18431 * @return array
18432 */
18433 public function search_lp_bookings_list( $data ) {
18434
18435 if ( ! class_exists( 'OsBookingHelper' ) ) {
18436 return [];
18437 }
18438
18439 $bookings = OsBookingHelper::get_bookings_for_select();
18440 $options = [];
18441
18442 if ( ! empty( $bookings ) ) {
18443 foreach ( $bookings as $key => $booking ) {
18444 $options[] = [
18445 'label' => $booking['label'],
18446 'value' => $booking['value'],
18447 ];
18448 }
18449 }
18450
18451 return [
18452 'options' => $options,
18453 'hasMore' => false,
18454 ];
18455 }
18456
18457 /**
18458 * Prepare LatePoint Services List.
18459 *
18460 * @param array $data Search Params.
18461 * @return array
18462 */
18463 public function search_lp_services_list( $data ) {
18464
18465 if ( ! class_exists( 'OsServiceHelper' ) ) {
18466 return [];
18467 }
18468
18469 $services = OsServiceHelper::get_services_list();
18470 $options = [];
18471
18472 if ( ! empty( $services ) ) {
18473 foreach ( $services as $key => $service ) {
18474 $options[] = [
18475 'label' => $service['label'],
18476 'value' => $service['value'],
18477 ];
18478 }
18479 }
18480
18481 return [
18482 'options' => $options,
18483 'hasMore' => false,
18484 ];
18485 }
18486
18487 /**
18488 * Prepare LatePoint Bundles List.
18489 *
18490 * @param array $data Search Params.
18491 * @return array
18492 */
18493 public function search_lp_bundles_list( $data ) {
18494 global $wpdb;
18495
18496 $bundles = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}latepoint_bundles", ARRAY_A );
18497
18498 $options = [];
18499
18500 if ( ! empty( $bundles ) ) {
18501 foreach ( $bundles as $bundle ) {
18502 $options[] = [
18503 'label' => $bundle['name'],
18504 'value' => $bundle['id'],
18505 ];
18506 }
18507 }
18508
18509 return [
18510 'options' => $options,
18511 'hasMore' => false,
18512 ];
18513 }
18514
18515 /**
18516 * Prepare LatePoint Agents List.
18517 *
18518 * @param array $data Search Params.
18519 * @return array
18520 */
18521 public function search_lp_agents_list( $data ) {
18522
18523 if ( ! class_exists( 'OsAgentHelper' ) ) {
18524 return [];
18525 }
18526
18527 $agent_ids_for_service = OsAgentHelper::get_agent_ids_for_service_and_location( $data['dynamic'] );
18528 $agents = OsAgentHelper::get_agents_list();
18529 $options = [];
18530
18531 if ( ! empty( $agents ) ) {
18532 foreach ( $agents as $key => $agent ) {
18533 if ( in_array( $agent['value'], $agent_ids_for_service ) ) {
18534 $options[] = [
18535 'label' => $agent['label'],
18536 'value' => $agent['value'],
18537 ];
18538 }
18539 }
18540 }
18541
18542 return [
18543 'options' => $options,
18544 'hasMore' => false,
18545 ];
18546 }
18547
18548 /**
18549 * Prepare LatePoint Agents WP User List.
18550 *
18551 * @param array $data Search Params.
18552 * @return array
18553 */
18554 public function search_lp_user_agents_list( $data ) {
18555
18556 if ( ! class_exists( 'OsWpUserHelper' ) ) {
18557 return [];
18558 }
18559 $agents = OsWpUserHelper::get_wp_users_for_select( [ 'role' => 'latepoint_agent' ] );
18560 $options = [];
18561 if ( ! empty( $agents ) ) {
18562 $options = $agents;
18563 }
18564 return [
18565 'options' => $options,
18566 'hasMore' => false,
18567 ];
18568 }
18569
18570 /**
18571 * Prepare LatePoint Statuses List.
18572 *
18573 * @param array $data Search Params.
18574 * @return array
18575 */
18576 public function search_lp_statuses_list( $data ) {
18577
18578 if ( ! class_exists( 'OsBookingHelper' ) ) {
18579 return [];
18580 }
18581
18582 $statuses = OsBookingHelper::get_statuses_list();
18583 $options = [];
18584
18585 if ( ! empty( $statuses ) ) {
18586 foreach ( $statuses as $key => $label ) {
18587 $options[] = [
18588 'label' => $label,
18589 'value' => $key,
18590 ];
18591 }
18592 }
18593
18594 return [
18595 'options' => $options,
18596 'hasMore' => false,
18597 ];
18598 }
18599
18600 /**
18601 * Prepare LatePoint Customers List.
18602 *
18603 * @param array $data Search Params.
18604 * @return array
18605 */
18606 public function search_lp_customers_list( $data ) {
18607
18608 if ( ! class_exists( 'OsCustomerHelper' ) ) {
18609 return [];
18610 }
18611
18612 $customers = OsCustomerHelper::get_customers_for_select();
18613 $options = [];
18614
18615 if ( ! empty( $customers ) ) {
18616 foreach ( $customers as $key => $customer ) {
18617 $options[] = [
18618 'label' => $customer['label'],
18619 'value' => $customer['value'],
18620 ];
18621 }
18622 }
18623
18624 return [
18625 'options' => $options,
18626 'hasMore' => false,
18627 ];
18628 }
18629
18630 /**
18631 * Prepare SureForms Forms List.
18632 *
18633 * @param array $data Search Params.
18634 * @return array
18635 */
18636 public function search_sureforms_form_list( $data ) {
18637
18638 $options = [];
18639
18640 $page = $data['page'];
18641 $limit = Utilities::get_search_page_limit();
18642 $offset = $limit * ( $page - 1 );
18643
18644 $forms = get_posts(
18645 [
18646 'posts_per_page' => - 1,
18647 'orderby' => 'name',
18648 'order' => 'asc',
18649 'post_type' => 'sureforms_form',
18650 'post_status' => 'publish',
18651 ]
18652 );
18653 $form_count = wp_count_posts( 'sureforms_form' )->publish;
18654
18655 if ( ! empty( $forms ) ) {
18656 if ( is_array( $forms ) ) {
18657 foreach ( $forms as $form ) {
18658 $title = html_entity_decode( get_the_title( $form->ID ), ENT_QUOTES, 'UTF-8' );
18659 $options[] = [
18660 'label' => $title,
18661 'value' => $form->ID,
18662 ];
18663 }
18664 }
18665 }
18666
18667 return [
18668 'options' => $options,
18669 'hasMore' => $form_count > $limit && $form_count > $offset,
18670 ];
18671 }
18672
18673 /**
18674 * Prepare Academy Course List.
18675 *
18676 * @param array $data Search Params.
18677 * @return array
18678 */
18679 public function search_ac_lms_courses( $data ) {
18680
18681 $options = [];
18682
18683 $page = $data['page'];
18684 $limit = Utilities::get_search_page_limit();
18685 $offset = $limit * ( $page - 1 );
18686
18687 $courses = get_posts(
18688 [
18689 'posts_per_page' => - 1,
18690 'orderby' => 'name',
18691 'order' => 'asc',
18692 'post_type' => 'academy_courses',
18693 'post_status' => 'publish',
18694 ]
18695 );
18696 $course_count = wp_count_posts( 'academy_courses' )->publish;
18697
18698 if ( ! empty( $courses ) ) {
18699 if ( is_array( $courses ) ) {
18700 foreach ( $courses as $course ) {
18701 $options[] = [
18702 'label' => get_the_title( $course->ID ),
18703 'value' => $course->ID,
18704 ];
18705 }
18706 }
18707 }
18708
18709 return [
18710 'options' => $options,
18711 'hasMore' => $course_count > $limit && $course_count > $offset,
18712 ];
18713 }
18714
18715 /**
18716 * Prepare Academy Lesson List.
18717 *
18718 * @param array $data Search Params.
18719 * @return array
18720 */
18721 public function search_ac_lms_lessons( $data ) {
18722
18723 $options = [];
18724
18725 if ( ! class_exists( '\Academy\Helper' ) ) {
18726 return [];
18727 }
18728
18729 $curriculums = \Academy\Helper::get_course_curriculum( $data['dynamic'] );
18730
18731 if ( ! empty( $curriculums ) ) {
18732 foreach ( $curriculums as $topic ) {
18733 if ( isset( $topic['topics'] ) && is_array( $topic['topics'] ) ) {
18734 foreach ( $topic['topics'] as $lesson ) {
18735 if ( isset( $lesson['type'] ) && 'lesson' === $lesson['type'] ) {
18736 $options[] = [
18737 'label' => $lesson['name'],
18738 'value' => $lesson['id'],
18739 ];
18740 }
18741 }
18742 }
18743 }
18744 }
18745
18746 return [
18747 'options' => $options,
18748 'hasMore' => false,
18749 ];
18750 }
18751
18752 /**
18753 * Prepare Academy Quiz List.
18754 *
18755 * @param array $data Search Params.
18756 * @return array
18757 */
18758 public function search_ac_lms_quiz( $data ) {
18759
18760 $options = [];
18761
18762 $page = $data['page'];
18763 $limit = Utilities::get_search_page_limit();
18764 $offset = $limit * ( $page - 1 );
18765
18766 $quizs = get_posts(
18767 [
18768 'posts_per_page' => - 1,
18769 'orderby' => 'name',
18770 'order' => 'asc',
18771 'post_type' => 'academy_quiz',
18772 'post_status' => 'publish',
18773 ]
18774 );
18775 $quiz_count = wp_count_posts( 'academy_quiz' )->publish;
18776
18777 if ( ! empty( $quizs ) ) {
18778 if ( is_array( $quizs ) ) {
18779 foreach ( $quizs as $quiz ) {
18780 $options[] = [
18781 'label' => get_the_title( $quiz->ID ),
18782 'value' => $quiz->ID,
18783 ];
18784 }
18785 }
18786 }
18787
18788 return [
18789 'options' => $options,
18790 'hasMore' => $quiz_count > $limit && $quiz_count > $offset,
18791 ];
18792 }
18793
18794 /**
18795 * Search Academy LMS data.
18796 *
18797 * @param array $data data.
18798 * @return array|void
18799 */
18800 public function search_ac_lms_last_data( $data ) {
18801 global $wpdb;
18802 $post_type = $data['post_type'];
18803 $trigger = $data['search_term'];
18804 $context = [];
18805
18806 if ( ! class_exists( '\Academy\Helper' ) ) {
18807 return [];
18808 }
18809
18810 $course_id = -1;
18811 $lesson_id = -1;
18812 $quiz_id = -1;
18813 if ( 'ac_lms_course_completed' === $trigger ) {
18814 $course_id = $data['filter']['course']['value'];
18815 } elseif ( 'ac_lms_lesson_completed' === $trigger ) {
18816 $course_id = $data['filter']['course']['value'];
18817 $lesson_id = $data['filter']['lesson']['value'];
18818 } elseif ( 'ac_lms_quiz_completed' === $trigger || 'ac_lms_quiz_failed' === $trigger ) {
18819 $quiz_id = $data['filter']['quiz']['value'];
18820 } elseif ( 'ac_lms_enrolled_course' === $trigger ) {
18821 $course_id = $data['filter']['course']['value'];
18822 }
18823
18824 $users = get_users(
18825 [
18826 'fields' => 'ID',
18827 'meta_key' => 'is_academy_student',
18828 ]
18829 );
18830 if ( ! empty( $users ) ) {
18831 $user_random_key = array_rand( $users );
18832 $user_id = $users[ $user_random_key ];
18833 }
18834
18835 if ( 'ac_lms_course_completed' === $trigger ) {
18836 if ( -1 === $course_id ) {
18837 $result = $wpdb->get_results(
18838 $wpdb->prepare(
18839 "SELECT * FROM {$wpdb->prefix}comments
18840 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",
18841 $post_type
18842 )
18843 );
18844 } else {
18845 $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 ) );
18846 }
18847 } elseif ( 'ac_lms_lesson_completed' === $trigger ) {
18848 if ( -1 === $course_id ) {
18849 $courses = get_posts(
18850 [
18851 'posts_per_page' => - 1,
18852 'post_type' => 'academy_courses',
18853 'post_status' => 'publish',
18854 'fields' => 'ids',
18855 ]
18856 );
18857 $course_id = array_rand( $courses );
18858 $option_name = 'academy_course_' . $course_id . '_completed_topics';
18859 } else {
18860 $option_name = 'academy_course_' . $course_id . '_completed_topics';
18861 }
18862 if ( ! empty( $users ) ) {
18863 $meta = get_user_meta( $user_id, $option_name, true );
18864 if ( is_string( $meta ) ) {
18865 $saved_topics_lists = (array) json_decode( $meta, true );
18866 if ( -1 === $lesson_id ) {
18867 $result = $saved_topics_lists['lesson'];
18868 } else {
18869 if ( is_array( $saved_topics_lists['lesson'] ) ) {
18870 if ( array_key_exists( $lesson_id, $saved_topics_lists['lesson'] ) ) {
18871 $result = $saved_topics_lists['lesson'];
18872 }
18873 }
18874 }
18875 }
18876 }
18877 } elseif ( 'ac_lms_quiz_completed' === $trigger ) {
18878 if ( -1 === $quiz_id ) {
18879 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE attempt_status='passed' order by attempt_id DESC LIMIT 1" );
18880 } else {
18881 $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 ) );
18882 }
18883 } elseif ( 'ac_lms_quiz_failed' === $trigger ) {
18884 if ( -1 === $quiz_id ) {
18885 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE attempt_status='failed' order by attempt_id DESC LIMIT 1" );
18886 } else {
18887 $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 ) );
18888 }
18889 } elseif ( 'ac_lms_enrolled_course' === $trigger ) {
18890 if ( -1 === $course_id ) {
18891 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type = %s order by ID DESC LIMIT 1", 'academy_enrolled' ) );
18892 } else {
18893 $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 ) );
18894 }
18895 }
18896
18897 if ( ! empty( $result ) ) {
18898 switch ( $trigger ) {
18899 case 'ac_lms_course_completed':
18900 $data = WordPress::get_post_context( $result[0]->comment_post_ID );
18901 $context_data = WordPress::get_user_context( $result[0]->user_id );
18902 $context_data['course_data'] = $data;
18903 $context_data['course'] = $result[0]->comment_post_ID;
18904 break;
18905 case 'ac_lms_enrolled_course':
18906 $data = WordPress::get_post_context( $result[0]->post_parent );
18907 $context_data = WordPress::get_user_context( $result[0]->post_author );
18908 $context_data['course_data'] = $data;
18909 $context_data['enrollment_data'] = $result[0];
18910 $context_data['course'] = $result[0]->post_parent;
18911 break;
18912 case 'ac_lms_lesson_completed':
18913 if ( -1 === $lesson_id ) {
18914 $key = array_rand( $result );
18915 $lesson_data = \Academy\Helper::get_lesson( $key );
18916 } else {
18917 $lesson_data = \Academy\Helper::get_lesson( $lesson_id );
18918 }
18919 if ( is_object( $lesson_data ) ) {
18920 $lesson_data = get_object_vars( $lesson_data );
18921 }
18922 if ( ! empty( $users ) ) {
18923 $context_data = array_merge( $lesson_data, WordPress::get_user_context( $user_id ) );
18924 }
18925 $context_data['course_data'] = WordPress::get_post_context( $course_id );
18926 $context_data['lesson'] = $lesson_id;
18927 $context_data['course'] = $course_id;
18928 break;
18929 case 'ac_lms_quiz_completed':
18930 case 'ac_lms_quiz_failed':
18931 $context_data = WordPress::get_user_context( $result[0]->user_id );
18932 $context_data['quiz_data'] = WordPress::get_post_context( $result[0]->quiz_id );
18933 $context_data['quiz_attempt_details'] = $result;
18934 $context_data['quiz'] = $result[0]->quiz_id;
18935 break;
18936 default:
18937 return;
18938 }
18939 $context['pluggable_data'] = $context_data;
18940 $context['response_type'] = 'live';
18941 } elseif ( empty( $result ) ) {
18942 switch ( $trigger ) {
18943 case 'ac_lms_course_completed':
18944 $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:\/\/example-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"}';
18945 break;
18946 case 'ac_lms_lesson_completed':
18947 $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:\/\/example-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"}';
18948 break;
18949 case 'ac_lms_quiz_completed':
18950 $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:\/\/example-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"}';
18951 break;
18952 case 'ac_lms_quiz_failed':
18953 $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:\/\/example-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"}';
18954 break;
18955 case 'ac_lms_enrolled_course':
18956 $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:\/\/example-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:\/\/example-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"}';
18957 break;
18958 default:
18959 return;
18960 }
18961 $context = (array) json_decode( $sample_data, true );
18962 }
18963 return $context;
18964 }
18965
18966 /**
18967 * Search myCred Point Type List.
18968 *
18969 * @param array $data Search Params.
18970 * @return array
18971 */
18972 public function search_mycred_point_type_list( $data ) {
18973
18974 $options = [];
18975
18976 if ( ! function_exists( 'mycred_get_types' ) ) {
18977 return [];
18978 }
18979
18980 $posts = mycred_get_types();
18981
18982 if ( ! empty( $posts ) ) {
18983 if ( is_array( $posts ) ) {
18984 foreach ( $posts as $key => $post ) {
18985 $options[] = [
18986 'label' => $post,
18987 'value' => $key,
18988 ];
18989 }
18990 }
18991 }
18992
18993 return [
18994 'options' => $options,
18995 'hasMore' => false,
18996 ];
18997 }
18998
18999 /**
19000 * Prepare elementor forms.
19001 *
19002 * @param array $data Search Params.
19003 *
19004 * @return array
19005 */
19006 public function search_new_elementor_form_fields( $data ) {
19007
19008 $fields = [];
19009 $select_form_id = $data['dynamic'];
19010 global $wpdb;
19011 $post_metas = $wpdb->get_results(
19012 $wpdb->prepare(
19013 "SELECT pm.post_id, pm.meta_value
19014 FROM $wpdb->postmeta pm
19015 LEFT JOIN $wpdb->posts p
19016 ON p.ID = pm.post_id
19017 WHERE p.post_type IS NOT NULL
19018 AND p.post_status = %s
19019 AND pm.meta_key = %s
19020 AND pm.`meta_value` LIKE %s",
19021 'publish',
19022 '_elementor_data',
19023 '%%form_fields%%'
19024 )
19025 );
19026
19027 if ( ! empty( $post_metas ) ) {
19028 foreach ( $post_metas as $post_meta ) {
19029 /**
19030 *
19031 * Ignore line
19032 *
19033 * @phpstan-ignore-next-line
19034 */
19035 $inner_forms = Utilities::search_elementor_forms( json_decode( $post_meta->meta_value ) );
19036 if ( ! empty( $inner_forms ) ) {
19037 foreach ( $inner_forms as $form ) {
19038 $form_id = explode( '_', $select_form_id );
19039 if ( is_object( $form ) ) {
19040 if ( $form->id == $form_id[1] ) {
19041 if ( ! empty( $form->settings->form_fields ) ) {
19042 foreach ( $form->settings->form_fields as $field ) {
19043 $fields[] = [
19044 'value' => $field->custom_id,
19045 'text' => ! empty( $field->field_label ) ? $field->field_label : 'unknown',
19046 ];
19047 }
19048 }
19049 }
19050 }
19051 }
19052 }
19053 }
19054 }
19055 $options = [];
19056 if ( ! empty( $fields ) ) {
19057 foreach ( $fields as $key => $value ) {
19058 $options[] = [
19059 'label' => $value['text'],
19060 'value' => $value['value'],
19061 ];
19062 }
19063 }
19064 return [
19065 'options' => $options,
19066 'hasMore' => false,
19067 ];
19068 }
19069 /**
19070 * Get Fluent Booking Appointment Events.
19071 *
19072 * @param array $data data.
19073 *
19074 * @return array
19075 */
19076 public function search_fluent_booking_calendars_list( $data ) {
19077
19078 global $wpdb;
19079
19080 $page = $data['page'];
19081 $limit = Utilities::get_search_page_limit();
19082 $offset = $limit * ( $page - 1 );
19083
19084 $calendars = $wpdb->get_results(
19085 $wpdb->prepare(
19086 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendars WHERE status = %s ORDER BY id DESC LIMIT %d OFFSET %d",
19087 [ 'active', $limit, $offset ]
19088 ),
19089 OBJECT
19090 );
19091
19092 $calendars_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
19093
19094 $options = [];
19095 if ( ! empty( $calendars ) ) {
19096 foreach ( $calendars as $calendar ) {
19097 $options[] = [
19098 'label' => $calendar->title,
19099 'value' => $calendar->id,
19100 ];
19101 }
19102 }
19103 return [
19104 'options' => $options,
19105 'hasMore' => $calendars_count > $limit && $calendars_count > $offset,
19106 ];
19107
19108 }
19109
19110 /**
19111 * Get Fluent Booking Appointment Events.
19112 *
19113 * @param array $data data.
19114 *
19115 * @return array
19116 */
19117 public function search_fluent_booking_events_list( $data ) {
19118
19119 global $wpdb;
19120 $page = $data['page'];
19121 $limit = Utilities::get_search_page_limit();
19122 $offset = $limit * ( $page - 1 );
19123 $calendar_id = sanitize_text_field( $data['dynamic']['calender_id'] );
19124 if ( '-1' === $calendar_id ) {
19125 $events = $wpdb->get_results(
19126 $wpdb->prepare(
19127 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendar_events WHERE status = %s ORDER BY id DESC LIMIT %d OFFSET %d",
19128 [ 'active', $limit, $offset ]
19129 ),
19130 OBJECT
19131 );
19132 } else {
19133 $events = $wpdb->get_results(
19134 $wpdb->prepare(
19135 "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",
19136 [ 'active', $calendar_id, $limit, $offset ]
19137 ),
19138 OBJECT
19139 );
19140 }
19141
19142 $events_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
19143
19144 $options = [];
19145 if ( ! empty( $events ) ) {
19146 foreach ( $events as $event ) {
19147 $options[] = [
19148 'label' => $event->title,
19149 'value' => $event->id,
19150 ];
19151 }
19152 }
19153 return [
19154 'options' => $options,
19155 'hasMore' => $events_count > $limit && $events_count > $offset,
19156 ];
19157
19158 }
19159
19160 /**
19161 * Get Fluent Booking last data.
19162 *
19163 * @param array $data data.
19164 *
19165 * @return array
19166 */
19167 public function search_fluent_booking_last_data( $data ) {
19168 global $wpdb;
19169 $trigger = $data['search_term'];
19170 $course_data = [];
19171 $lesson_data = [];
19172
19173 $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
19174 $context = [
19175 'pluggable_data' => json_decode( $sample_data, true ),
19176 'response_type' => 'sample',
19177 ];
19178 if ( 'fluent_booking_appointment_cancelled' === $trigger ) {
19179 $status = 'cancelled';
19180 } elseif ( 'fluent_booking_appointment_completed' === $trigger ) {
19181 $status = 'completed';
19182 }
19183
19184 $event_id = (int) $data['filter']['event_id']['value'];
19185 if ( 'fluent_booking_appointment_cancelled' === $trigger || 'fluent_booking_appointment_completed' === $trigger ) {
19186 if ( $event_id > 0 ) {
19187 $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 ) );
19188 } else {
19189 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE status= %s ORDER BY id DESC LIMIT 1", $status ) );
19190 }
19191 } elseif ( 'fluent_booking_new_appointment_booked' === $trigger ) {
19192 if ( $event_id > 0 ) {
19193 $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' ) );
19194 } else {
19195 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE status != %s ORDER BY id DESC LIMIT 1", 'cancelled' ) );
19196 }
19197 }
19198
19199 if ( ! empty( $booking ) ) {
19200 $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 ) );
19201
19202 $booking_event = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_calendar_events WHERE id= %d", $booking->event_id ) );
19203 $booking_data = $booking;
19204 $booking_data->custom_fields = $booking_meta->value;
19205 $booking_array = [
19206 'booking' => $booking_data,
19207 'event' => $booking_event,
19208 ];
19209
19210
19211 $booking_array_response = self::recursive_unserialize( $booking_array );
19212 $context['pluggable_data'] = $booking_array_response;
19213 $context['response_type'] = 'live';
19214 }
19215 return $context;
19216 }
19217
19218 /**
19219 * Recursive unserilize.
19220 *
19221 * @param array $data data.
19222 *
19223 * @return array|mixed
19224 */
19225 public static function recursive_unserialize( $data ) {
19226 if ( is_array( $data ) ) {
19227 foreach ( $data as $key => $value ) {
19228 $data[ $key ] = self::recursive_unserialize( $value );
19229 }
19230 return $data;
19231 } elseif ( is_object( $data ) && 'stdClass' === get_class( $data ) ) {
19232 foreach ( $data as $property => $value ) {
19233 $data->$property = self::recursive_unserialize( $value );
19234 }
19235 return $data;
19236 } elseif ( is_string( $data ) && self::is_serialized( strval( $data ) ) ) {
19237 return unserialize( $data );
19238 } else {
19239 return $data;
19240 }
19241
19242 }
19243
19244 /**
19245 * Check if string serialized.
19246 *
19247 * @param string $data data.
19248 *
19249 * @return bool
19250 */
19251 public static function is_serialized( $data ) {
19252 $unserialized = unserialize( $data );
19253 if ( 'b:0;' === $data || false !== $unserialized ) {
19254 return true;
19255 } else {
19256 return false;
19257 }
19258 }
19259
19260 /**
19261 * Search Tutor LMS data.
19262 *
19263 * @param array $data data.
19264 * @return array|void|mixed
19265 */
19266 public function search_tutor_lms_last_data( $data ) {
19267 global $wpdb;
19268 $post_type = $data['post_type'];
19269 $trigger = $data['search_term'];
19270 $context = [];
19271
19272 if ( ! function_exists( 'tutor_utils' ) ) {
19273 return [];
19274 }
19275
19276 $post_id = -1;
19277 if ( 'course_enrolled' === $trigger || 'tutor_courses_question' === $trigger ) {
19278 $post_id = $data['filter']['tutor_course']['value'];
19279 } elseif ( 'quiz_attempt_percentage' === $trigger || 'quiz_failed' === $trigger || 'quiz_passed' === $trigger ) {
19280 $post_id = $data['filter']['quiz_id']['value'];
19281 }
19282
19283 if ( 'course_enrolled' === $trigger ) {
19284 if ( -1 === $post_id ) {
19285 $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 ) );
19286 } else {
19287 $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 ) );
19288 }
19289 } elseif ( 'tutor_courses_question' === $trigger ) {
19290 if ( -1 === $post_id ) {
19291 $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 ) );
19292 } else {
19293 $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 ) );
19294 }
19295 } elseif ( 'quiz_passed' === $trigger ) {
19296 if ( -1 === $post_id ) {
19297 $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 ) );
19298 } else {
19299 $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 ) );
19300 }
19301 } elseif ( 'quiz_failed' === $trigger ) {
19302 if ( -1 === $post_id ) {
19303 $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 ) );
19304 } else {
19305 $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 ) );
19306 }
19307 } elseif ( 'quiz_attempt_percentage' == $trigger ) {
19308 $condition_compare = $data['filter']['condition_compare']['value'];
19309 $percentage = $data['filter']['percentage']['value'];
19310 if ( -1 === $post_id ) {
19311 $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
19312 } else {
19313 $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
19314 }
19315 }
19316
19317 if ( ! empty( $result ) ) {
19318 switch ( $trigger ) {
19319 case 'course_enrolled':
19320 $result_item_id = $result[0]->post_parent;
19321 $result_user_id = $result[0]->post_author;
19322 $context_data = array_merge(
19323 WordPress::get_user_context( $result_user_id ),
19324 WordPress::get_post_context( $result_item_id )
19325 );
19326 $context_data['tutor_course'] = $result_item_id;
19327 break;
19328 case 'tutor_courses_question':
19329 $date = $result[0]->comment_date;
19330 $data = [
19331 'comment_post_ID' => $result[0]->comment_post_ID,
19332 'comment_author' => $result[0]->comment_author,
19333 'comment_date' => $date,
19334 'comment_date_gmt' => get_gmt_from_date( $date ),
19335 'comment_content' => $result[0]->comment_content,
19336 'comment_approved' => 'approved',
19337 'comment_agent' => 'TutorLMSPlugin',
19338 'comment_type' => 'tutor_q_and_a',
19339 'comment_parent' => $result[0]->comment_parent,
19340 'user_id' => $result[0]->user_id,
19341 ];
19342 $context_data['tutor_course'] = $result[0]->comment_post_ID;
19343 $context_data['data'] = $data;
19344 break;
19345 case 'quiz_attempt_percentage':
19346 case 'quiz_failed':
19347 case 'quiz_passed':
19348 $attempt = tutor_utils()->get_attempt( $result[0]->attempt_id );
19349 $context_data = WordPress::get_user_context( $result[0]->user_id );
19350 $context_data['quiz_id'] = $attempt->quiz_id;
19351 $context_data['attempt_id'] = $result[0]->attempt_id;
19352 $context_data['attempt'] = $attempt;
19353 break;
19354 default:
19355 return;
19356 }
19357 if ( ! empty( $context_data ) ) {
19358 $context['pluggable_data'] = $context_data;
19359 $context['response_type'] = 'live';
19360 }
19361 } elseif ( empty( $result ) ) {
19362 switch ( $trigger ) {
19363 case 'course_enrolled':
19364 $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"} ';
19365 break;
19366 case 'tutor_courses_question':
19367 $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"}';
19368 break;
19369 case 'quiz_attempt_percentage':
19370 case 'quiz_failed':
19371 case 'quiz_passed':
19372 $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"}';
19373 break;
19374 default:
19375 return;
19376 }
19377 $context = json_decode( $sample_data, true );
19378 }
19379
19380 return $context;
19381 }
19382
19383 /**
19384 * Get Asgorus Forum list
19385 *
19386 * @param array $data data.
19387 *
19388 * @return array
19389 */
19390 public function search_asgorus_forums_list( $data ) {
19391 if ( ! class_exists( 'AsgarosForum' ) ) {
19392 return [];
19393 }
19394 $category = $data['dynamic'];
19395 if ( is_array( $category ) ) {
19396 $category_id = $category['forum_category'];
19397 } else {
19398 $category_id = $category;
19399 }
19400 $asgaros_forum = new AsgarosForum();
19401 $forums = $asgaros_forum->get_forums( $category_id );
19402 $options = [];
19403 if ( ! empty( $forums ) ) {
19404 foreach ( $forums as $forum ) {
19405 $options[] = [
19406 'label' => $forum->name,
19407 'value' => $forum->id,
19408 ];
19409 }
19410 }
19411 return [
19412 'options' => $options,
19413 'hasMore' => false,
19414 ];
19415 }
19416
19417 /**
19418 * Get Asgorus Categories list
19419 *
19420 * @param array $data data.
19421 *
19422 * @return array
19423 */
19424 public function search_asgorus_categories_list( $data ) {
19425 if ( ! class_exists( 'AsgarosForum' ) ) {
19426 return [];
19427 }
19428 $asgaros_forum = new AsgarosForum();
19429 $categories = (array) $asgaros_forum->content->get_categories();
19430 $options = [];
19431
19432 if ( ! empty( $categories ) ) {
19433 foreach ( $categories as $category ) {
19434 $options[] = [
19435 'label' => $category->name,
19436 'value' => $category->term_id,
19437 ];
19438 }
19439 }
19440 return [
19441 'options' => $options,
19442 'hasMore' => false,
19443 ];
19444 }
19445
19446 /**
19447 * Get Asgorus Topic lists
19448 *
19449 * @param array $data data.
19450 *
19451 * @return array
19452 */
19453 public function search_asgorus_topic_list( $data ) {
19454 if ( ! class_exists( 'AsgarosForum' ) ) {
19455 return [];
19456 }
19457 global $wpdb;
19458 $forum_id = $data['dynamic'];
19459 $asgaros_forum = new AsgarosForum();
19460 $sql = 'SELECT name,id FROM ' . $wpdb->prefix . 'forum_topics WHERE parent_id = %d AND closed = 0 ORDER BY id';
19461 $topics = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id ), ARRAY_A );// @phpcs:ignore
19462 $options = [];
19463
19464 if ( ! empty( $topics ) ) {
19465 foreach ( $topics as $topic ) {
19466 $options[] = [
19467 'label' => $topic['name'],
19468 'value' => $topic['id'],
19469 ];
19470 }
19471 }
19472 return [
19473 'options' => $options,
19474 'hasMore' => false,
19475 ];
19476 }
19477
19478 /**
19479 * Get Asgorus Topic Last Data
19480 *
19481 * @param array $data data.
19482 *
19483 * @return array
19484 */
19485 public function search_pluggables_asgaros_topic_last_data( $data ) {
19486 if ( ! class_exists( 'AsgarosForum' ) ) {
19487 return [];
19488 }
19489 $asgaros_forum = new AsgarosForum();
19490 $context = [];
19491 global $wpdb;
19492 $forum_id = $data['filter']['forum_id']['value'];
19493
19494 if ( -1 == $forum_id ) {
19495 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'forum_topics WHERE closed = 0 ORDER BY id DESC LIMIT 1', ARRAY_A );
19496 } else {
19497 $forum = $forum_id;
19498 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'forum_topics WHERE parent_id = %d AND closed = 0 ORDER BY id DESC LIMIT 1';
19499 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum ), ARRAY_A );// @phpcs:ignore
19500 }
19501
19502 if ( ! empty( $results ) ) {
19503 $sql_post = 'SELECT * FROM ' . $wpdb->prefix . 'forum_posts WHERE parent_id = %d ORDER BY id DESC LIMIT 1';
19504 $results_post = $wpdb->get_results( $wpdb->prepare( $sql_post, $results[0]['id'] ), ARRAY_A ); // @phpcs:ignore
19505 $context['pluggable_data']['forum_id'] = $results[0]['parent_id'];
19506 $context['pluggable_data']['topic_id'] = $results[0]['id'];
19507 $context['pluggable_data']['post_id'] = $results_post[0]['id'];
19508 $context['pluggable_data']['forum'] = $asgaros_forum->content->get_forum( $results[0]['parent_id'] );
19509 $context['pluggable_data']['topic'] = $asgaros_forum->content->get_topic( $results[0]['id'] );
19510 $context['pluggable_data']['post'] = $asgaros_forum->content->get_post( $results_post[0]['id'] );
19511
19512 $context['pluggable_data']['author'] = WordPress::get_user_context( $results[0]['author_id'] );
19513 $context['response_type'] = 'live';
19514 } else {
19515 $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-test@example.com","user_role":["administrator"]}},"response_type":"sample"}', true );// @phpcs:ignore
19516 }
19517 return $context;
19518 }
19519
19520 /**
19521 * Get Asgorus Topic Reply Last Data
19522 *
19523 * @param array $data data.
19524 *
19525 * @return array
19526 */
19527 public function search_pluggables_asgaros_reply_last_data( $data ) {
19528 if ( ! class_exists( 'AsgarosForum' ) ) {
19529 return [];
19530 }
19531 $asgaros_forum = new AsgarosForum();
19532 $context = [];
19533 global $wpdb;
19534 $forum_id = $data['filter']['forum_id']['value'];
19535 $topic_id = $data['filter']['forum_id']['value'];
19536 if ( -1 == $topic_id ) {
19537 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'forum_posts ORDER BY id DESC LIMIT 1', ARRAY_A );
19538 } else {
19539 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'forum_posts WHERE parent_id = %d ORDER BY id DESC LIMIT 1';
19540 $results = $wpdb->get_results( $wpdb->prepare( $sql, $topic_id ), ARRAY_A );// @phpcs:ignore
19541 }
19542
19543 if ( ! empty( $results ) ) {
19544 $topic_id = $results[0]['parent_id'];
19545 $post_id = $results[0]['id'];
19546 $context['pluggable_data']['forum_id'] = $forum_id;
19547 $context['pluggable_data']['topic_id'] = $topic_id;
19548 $context['pluggable_data']['post_id'] = $post_id;
19549 $context['pluggable_data']['forum'] = $asgaros_forum->content->get_forum( $forum_id );
19550 $context['pluggable_data']['topic'] = $asgaros_forum->content->get_topic( $topic_id );
19551 $context['pluggable_data']['post'] = $asgaros_forum->content->get_post( $post_id );
19552
19553 $context['pluggable_data']['author'] = WordPress::get_user_context( $results[0]['author_id'] );
19554 $context['response_type'] = 'live';
19555 } else {
19556 $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-test@example.com","user_role":["administrator"]}},"response_type":"sample"}', true );// @phpcs:ignore
19557 }
19558 return $context;
19559 }
19560
19561 /**
19562 * Get WPLoyalty Points Awarded Last Data
19563 *
19564 * @param array $data data.
19565 *
19566 * @return array|mixed|string
19567 */
19568 public function search_wp_loyalty_points_awarded_customer( $data ) {
19569
19570 $context = [];
19571 global $wpdb;
19572 if ( ! class_exists( 'Wlr\App\Helpers\Base' ) ) {
19573 return [];
19574 }
19575 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wlr_earn_campaign_transaction WHERE transaction_type = %s ORDER BY id DESC LIMIT 1';
19576 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'credit' ), ARRAY_A );// @phpcs:ignore
19577
19578 if ( ! empty( $results ) ) {
19579 $context['pluggable_data']['user_email'] = $results[0]['user_email'];
19580 $context['pluggable_data']['points_earned'] = $results[0]['points'];
19581 $context['pluggable_data']['action_type'] = $results[0]['action_type'];
19582 $base_helper = new \Wlr\App\Helpers\Base();
19583 $user = $base_helper->getPointUserByEmail( $results[0]['user_email'] );
19584 $points_sql = 'SELECT * FROM ' . $wpdb->prefix . 'wlr_expire_points
19585 WHERE user_email = %s ORDER BY id DESC LIMIT 1';
19586 $points_results = $wpdb->get_results( $wpdb->prepare( $points_sql, $results[0]['user_email'] ), ARRAY_A );// @phpcs:ignore
19587 $context['pluggable_data']['user'] = $user;
19588 if ( ! empty( $points_results ) ) {
19589 $expire_date = $points_results[0]['expire_date'];
19590 $timestamp = is_numeric( $expire_date ) ? (int) $expire_date : null;
19591 $date_format = get_option( 'date_format' );
19592 if ( is_string( $date_format ) ) {
19593 $context['pluggable_data']['point_expiry_date'] = wp_date( $date_format, $timestamp );
19594 }
19595 }
19596 $context['response_type'] = 'live';
19597 } else {
19598 $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
19599 }
19600 return $context;
19601 }
19602
19603 /**
19604 * Get WPLoyalty Campaign Type List
19605 *
19606 * @param array $data data.
19607 *
19608 * @return array
19609 */
19610 public function search_wp_loyalty_action_type_list( $data ) {
19611 $options = [];
19612 if ( ! class_exists( 'Wlr\App\Helpers\Woocommerce' ) ) {
19613 return [];
19614 }
19615
19616 $woocommerce_helper = new \Wlr\App\Helpers\Woocommerce();
19617 $action_types = $woocommerce_helper->getActionTypes();
19618
19619 if ( ! empty( $action_types ) ) {
19620 foreach ( $action_types as $key => $type ) {
19621 $options[] = [
19622 'label' => $type,
19623 'value' => $key,
19624 ];
19625 }
19626 }
19627 return [
19628 'options' => $options,
19629 'hasMore' => false,
19630 ];
19631 }
19632
19633 /**
19634 * Get last data for trigger.
19635 *
19636 * @param array $data data.
19637 * @return array
19638 */
19639 public function search_slicewp_last_data( $data ) {
19640 global $wpdb;
19641
19642 $context = [];
19643 $context['response_type'] = 'sample';
19644
19645 $user_data = WordPress::get_sample_user_context();
19646
19647
19648 if ( ! function_exists( 'slicewp_get_affiliate' ) ) {
19649 return [];
19650 }
19651
19652 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
19653
19654 if ( in_array( $term, [ 'slicewp_new_affiliate', 'slicewp_update_affiliate' ], true ) ) {
19655 $affiliate = [
19656 'id' => 14,
19657 'user_id' => 25,
19658 'date_created' => '2024-03-14 12:35:50',
19659 'date_modified' => '2024-03-14 12:36:20',
19660 'payment_email' => 'testcustomer12@gmail.com',
19661 'website' => '',
19662 'status' => 'active',
19663 'parent_id' => 0,
19664 ];
19665 $context['pluggable_data'] = array_merge( $affiliate, [ 'user' => $user_data ] );
19666
19667 if ( ( ! empty( $data['filter'] ) && '-1' === $data['filter']['affiliate_id']['value'] ) || empty( $data['filter'] ) ) {
19668 $query = $wpdb->prepare(
19669 "
19670 SELECT *
19671 FROM {$wpdb->prefix}slicewp_affiliates
19672 WHERE status = %s
19673 ORDER BY id DESC
19674 LIMIT 1",
19675 'active'
19676 );
19677 $affiliate_results = $wpdb->get_row( $query ); //phpcs:ignore
19678
19679 } else {
19680 $affiliate_id = $data['filter']['affiliate_id']['value'];
19681 $query = $wpdb->prepare(
19682 "
19683 SELECT *
19684 FROM {$wpdb->prefix}slicewp_affiliates
19685 WHERE status = %s AND id = %d
19686 ORDER BY id DESC
19687 LIMIT 1",
19688 'active',
19689 $affiliate_id
19690 );
19691 $affiliate_results = $wpdb->get_row( $query ); //phpcs:ignore
19692
19693 }
19694
19695
19696
19697
19698 if ( ! empty( $affiliate_results ) ) {
19699 $context['pluggable_data'] = (array) $affiliate_results;
19700
19701 $user_data = WordPress::get_user_context( $affiliate_results->user_id );
19702 $context['pluggable_data']['user'] = $user_data;
19703 $context['response_type'] = 'live';
19704 }
19705 } elseif ( in_array( $term, [ 'slicewp_new_commission', 'slicewp_update_commission' ], true ) ) {
19706 $commission = [
19707 'id' => 14,
19708 'user_id' => 25,
19709 'date_created' => '2024-03-14 12:35:50',
19710 'date_modified' => '2024-03-14 12:36:20',
19711 'payment_email' => 'testcustomer12@gmail.com',
19712 'website' => '',
19713 'status' => 'active',
19714 'parent_id' => 0,
19715 ];
19716 $context['pluggable_data'] = array_merge( $commission, [ 'user' => $user_data ] );
19717 $affiliate_id = $data['filter']['affiliate_id']['value'];
19718 if ( -1 === $data['filter']['commission_id']['value'] ) {
19719 $query = $wpdb->prepare(
19720 "
19721 SELECT *
19722 FROM {$wpdb->prefix}slicewp_commissions WHERE affiliate_id=%d ORDER BY id DESC limit 1",
19723 $affiliate_id
19724 );
19725 $commission_results = $wpdb->get_row( $query ); //phpcs:ignore
19726 } else {
19727 $commission_id = $data['filter']['commission_id']['value'];
19728 $query = $wpdb->prepare(
19729 "
19730 SELECT *
19731 FROM {$wpdb->prefix}slicewp_commissions WHERE id= %d AND affiliate_id=%d ORDER BY id DESC limit 1",
19732 $commission_id,
19733 $affiliate_id
19734 );
19735 $commission_results = $wpdb->get_row( $query ); //phpcs:ignore
19736 }
19737
19738
19739
19740
19741 if ( ! empty( $commission_results ) ) {
19742 $context['pluggable_data'] = (array) $commission_results;
19743 $affiliate = slicewp_get_affiliate( $commission_results->affiliate_id );
19744 $user_id = $affiliate->get( 'user_id' );
19745 $user_data = WordPress::get_user_context( $user_id );
19746 $context['pluggable_data']['user'] = $user_data;
19747 $context['response_type'] = 'live';
19748 }
19749 }
19750
19751 return $context;
19752 }
19753
19754
19755 /**
19756 * Get Ninja Tables Last Data
19757 *
19758 * @param array $data data.
19759 *
19760 * @return array
19761 */
19762 public function search_ninja_tables_last_data( $data ) {
19763 $context = [];
19764 global $wpdb;
19765 $table_id = isset( $data['filter']['table_id']['value'] ) ? $data['filter']['table_id']['value'] : -1;
19766
19767 if ( -1 == $table_id ) {
19768 $results = $wpdb->get_row( 'SELECT * FROM ' . $wpdb->prefix . 'ninja_table_items ORDER BY id DESC LIMIT 1', ARRAY_A );
19769 } else {
19770 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'ninja_table_items WHERE table_id = %d ORDER BY id DESC LIMIT 1';
19771 $results = $wpdb->get_row( $wpdb->prepare( $sql, $table_id ), ARRAY_A );// @phpcs:ignore
19772 }
19773
19774 if ( ! empty( $results ) ) {
19775 $results['value'] = json_decode( $results['value'], true );
19776 $context['pluggable_data'] = $results;
19777 $context['pluggable_data']['owner'] = WordPress::get_user_context( $results['owner_id'] );
19778 $context['response_type'] = 'live';
19779 } else {
19780 $context = json_decode( '{"pluggable_data":{"id":"24","position":null,"table_id":"17484","owner_id":"29","attribute":"value","settings":null,"value":{"id":"3","name":"Table1","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":"John","user_lastname":"Dev","user_email":"johndoe@email.com","user_role":["editor"]}},"response_type":"sample"}', true );
19781 }
19782 return $context;
19783 }
19784
19785 /**
19786 * Get Ninja Tables Fields
19787 *
19788 * @param array $data data.
19789 *
19790 * @return array
19791 */
19792 public function search_ninja_table_fields( $data ) {
19793
19794 $context = [];
19795 global $wpdb;
19796 $table_id = isset( $data['dynamic'] ) ? $data['dynamic'] : -1;
19797 $fields = [];
19798 if ( $table_id > 0 ) {
19799 $fields = get_post_meta( $table_id, '_ninja_table_columns', true );
19800 }
19801
19802 return (array) $fields;
19803 }
19804
19805 /**
19806 * Get Late Point Booking Fields
19807 *
19808 * @param array $data data.
19809 *
19810 * @return array|void
19811 */
19812 public function search_late_point_booking_fields( $data ) {
19813 if ( ! class_exists( 'OsCustomFieldsController' ) ) {
19814
19815 return;
19816 }
19817 global $wpdb;
19818 $booking_fields = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}latepoint_settings WHERE name = %s", 'custom_fields_for_booking' ) );
19819 $fields = [];
19820 if ( ! empty( $booking_fields ) ) {
19821 $fields = json_decode( $booking_fields->value, true );
19822 }
19823 return (array) $fields;
19824 }
19825
19826 /** Search Voxel fields for action.
19827 *
19828 * @param array $data data.
19829 * @return array
19830 */
19831 public function search_voxel_custom_fields( $data ) {
19832 $post_type_fields = [];
19833 $post_type = $data['post_type'];
19834 if ( ! class_exists( '\Voxel\Post_Type' ) ) {
19835 return [];
19836 }
19837 $fields = \Voxel\Post_Type::get( $post_type )->get_fields();
19838 foreach ( $fields as $key => $field ) {
19839 if ( 'step-general' == $key ) {
19840 continue;
19841 }
19842 $field_props = $field->get_props();
19843 $post_type_fields[ $key ] = $field_props;
19844 }
19845 $context['fields'] = $post_type_fields;
19846 return $context;
19847 }
19848
19849 /**
19850 * Get Voxel Post Types lists
19851 *
19852 * @param array $data data.
19853 *
19854 * @return array
19855 */
19856 public function search_voxel_posttype_list( $data ) {
19857 $options = [];
19858 $voxel_post_types = [];
19859 if ( class_exists( '\Voxel\Post_Type' ) ) {
19860 $voxel_post_types = \Voxel\Post_Type::get_voxel_types();
19861 }
19862
19863 if ( ! empty( $voxel_post_types ) ) {
19864 foreach ( $voxel_post_types as $key => $voxel_post_type ) {
19865 $post_type = get_post_type_object( $key );
19866 if ( $post_type ) {
19867 if ( in_array( $post_type->name, [ 'collection' ], true ) ) {
19868 continue;
19869 }
19870 $options[] = [
19871 'label' => esc_attr( $post_type->labels->singular_name ),
19872 'value' => $post_type->name,
19873 ];
19874 }
19875 }
19876 }
19877 return [
19878 'options' => $options,
19879 'hasMore' => false,
19880 ];
19881 }
19882
19883 /**
19884 * Get Voxel Users Last Data
19885 *
19886 * @param array $data data.
19887 *
19888 * @return array|mixed|string
19889 */
19890 public function search_voxel_users_triggers_last_data( $data ) {
19891
19892 global $wpdb;
19893 $context = [];
19894 $term = $data['search_term'];
19895 if ( 'direct_message_received' === $term ) {
19896 $sql = "SELECT * FROM {$wpdb->prefix}voxel_messages WHERE receiver_type LIKE %s AND sender_type LIKE %s ORDER BY id DESC LIMIT 1";
19897 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user', 'user' ), ARRAY_A );// @phpcs:ignore
19898 } elseif ( 'user_timeline_post_created' === $term ) {
19899 $sql = "SELECT vt.* FROM {$wpdb->prefix}voxel_timeline vt WHERE vt.feed = %s AND vt.moderation = %d ORDER BY vt.id DESC LIMIT 1";
19900 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user_timeline', 0 ), ARRAY_A );// @phpcs:ignore
19901 } elseif ( 'user_timeline_post_approved' === $term ) {
19902 $sql = "SELECT vt.* FROM {$wpdb->prefix}voxel_timeline vt WHERE vt.feed = %s AND vt.moderation = %d ORDER BY vt.id DESC LIMIT 1";
19903 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user_timeline', 1 ), ARRAY_A );// @phpcs:ignore
19904 }
19905 if ( 'direct_message_received' === $term ) {
19906 if ( ! empty( $results ) ) {
19907 $context['pluggable_data']['sender'] = WordPress::get_user_context( $results[0]['sender_id'] );
19908 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $results[0]['receiver_id'] );
19909 $context['pluggable_data']['content'] = $results[0]['content'];
19910 $context['response_type'] = 'live';
19911 } else {
19912 $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
19913 }
19914 } elseif ( 'user_timeline_post_created' === $term || 'user_timeline_post_approved' === $term ) {
19915 if ( ! empty( $results ) ) {
19916 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['user_id'] );
19917 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
19918 $status_details = \Voxel\Timeline\Status::get( $results[0]['id'] );
19919 foreach ( (array) $status_details as $key => $value ) {
19920 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
19921 $context['pluggable_data'][ $clean_key ] = $value;
19922 }
19923 }
19924 $context['response_type'] = 'live';
19925 } else {
19926 $context = json_decode( '{"pluggable_data":{"wp_user_id":2,"user_login":"johnd","display_name":"johnd","user_firstname":"john","user_lastname":"doe","user_email":"johnd@gmail.com","user_registered":"2024-12-16 06:00:43","user_role":{"16":"administrator"},"id":51,"user_id":2,"post_id":null,"published_as":null,"content":"test","feed":"user_timeline","moderation":0,"repost_of":null,"quote_of":null,"created_at":"2025-01-22 11:10:06","edited_at":null,"review_score":null,"like_count":0,"reply_count":0,"details":{"files":"541"},"liked_by_user":false,"reposted_by_user":false,"last3_liked":[],"friends_reposted":[],"friends_liked":[]},"response_type":"sample"}', true );// @phpcs:ignore
19927 }
19928 }
19929 return $context;
19930 }
19931
19932
19933 /**
19934 * Get Voxel Membership Last Data
19935 *
19936 * @param array $data data.
19937 *
19938 * @return array|mixed|string
19939 */
19940 public function search_voxel_membership_triggers_last_data( $data ) {
19941
19942 global $wpdb;
19943 $context = [];
19944 $term = $data['search_term'];
19945
19946 if ( ! class_exists( 'Voxel\User' ) ) {
19947 // Return sample data when Voxel class is not available.
19948 if ( 'plan_canceled' === $term ) {
19949 $context['pluggable_data'] = WordPress::get_sample_user_context();
19950 $context['pluggable_data']['details'] = [
19951 'plan' => 'premium-plus',
19952 'type' => 'order',
19953 'order_id' => 14,
19954 'order_item_id' => 17,
19955 'billing' => [
19956 'price_key' => 'premium-plus_pz3xfk7z',
19957 'amount' => 20,
19958 'currency' => 'USD',
19959 'interval' => 'month',
19960 'frequency' => 1,
19961 'current_period' => [
19962 'start' => '2025-10-15 09:01:47',
19963 'end' => '2025-11-15 09:01:47',
19964 ],
19965 'is_active' => 0,
19966 'is_canceled' => 1,
19967 ],
19968 ];
19969 $context['response_type'] = 'sample';
19970 } elseif ( 'plan_activated' === $term ) {
19971 $context['pluggable_data'] = WordPress::get_sample_user_context();
19972 $context['pluggable_data']['details'] = [
19973 'plan' => 'premium-plus',
19974 'type' => 'order',
19975 'order_id' => 14,
19976 'order_item_id' => 17,
19977 'billing' => [
19978 'price_key' => 'premium-plus_pz3xfk7z',
19979 'amount' => 20,
19980 'currency' => 'USD',
19981 'interval' => 'month',
19982 'frequency' => 1,
19983 'current_period' => [
19984 'start' => '2025-10-15 09:01:47',
19985 'end' => '2025-11-15 09:01:47',
19986 ],
19987 'is_active' => 1,
19988 'is_canceled' => '',
19989 ],
19990 ];
19991 $context['response_type'] = 'sample';
19992 } elseif ( 'plan_updated' === $term ) {
19993 $context['pluggable_data'] = WordPress::get_sample_user_context();
19994 $context['pluggable_data']['details'] = [
19995 'plan' => 'premium-plus',
19996 'type' => 'subscription',
19997 'price_id' => 'premium-plus_pz3xfk7z',
19998 'status' => 'active',
19999 'metadata' => [
20000 'voxel:payment_for' => 'membership',
20001 'voxel:plan' => 'premium-plus',
20002 ],
20003 ];
20004 $context['response_type'] = 'sample';
20005 } elseif ( 'user_registered' === $term ) {
20006 $context['pluggable_data'] = WordPress::get_sample_user_context();
20007 $context['response_type'] = 'sample';
20008 }
20009 return $context;
20010 }
20011 if ( 'plan_activated' === $term ) {
20012 $meta_key = ( function_exists( '\Voxel\is_test_mode' ) && \Voxel\is_test_mode() ) ? 'voxel:test_plan' : 'voxel:plan';
20013 $sql = "SELECT
20014 m.user_id AS id,
20015 u.user_login AS title,
20016 u.user_email AS email,
20017 m.meta_value AS details,
20018 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) AS plan,
20019 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.amount' ) ) AS SIGNED ) AS amount,
20020 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.status' ) ) AS status,
20021 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.created' ) ) AS DATETIME ) AS created
20022 FROM {$wpdb->prefix}usermeta as m
20023 LEFT JOIN {$wpdb->prefix}users AS u ON m.user_id = u.ID
20024 WHERE m.meta_key = %s AND JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) != 'default'
20025 ORDER BY m.user_id DESC
20026 LIMIT 25 OFFSET 0";
20027 $results = $wpdb->get_results( $wpdb->prepare( $sql, $meta_key ), ARRAY_A );// @phpcs:ignore
20028 } elseif ( 'plan_canceled' === $term ) {
20029 // Simple approach: Get the most recent user with membership plan and modify data to show as cancelled.
20030 $meta_key = ( function_exists( '\Voxel\is_test_mode' ) && \Voxel\is_test_mode() ) ? 'voxel:test_plan' : 'voxel:plan';
20031 $sql = "SELECT
20032 m.user_id AS id,
20033 u.user_login AS title,
20034 u.user_email AS email,
20035 m.meta_value AS details,
20036 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) AS plan
20037 FROM {$wpdb->prefix}usermeta as m
20038 LEFT JOIN {$wpdb->prefix}users AS u ON m.user_id = u.ID
20039 WHERE m.meta_key = %s AND JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) != 'default'
20040 ORDER BY m.user_id DESC
20041 LIMIT 1";
20042 $results = $wpdb->get_results( $wpdb->prepare( $sql, $meta_key ), ARRAY_A );// @phpcs:ignore
20043 } elseif ( 'plan_updated' === $term ) {
20044 $meta_key = ( function_exists( '\Voxel\is_test_mode' ) && \Voxel\is_test_mode() ) ? 'voxel:test_plan' : 'voxel:plan';
20045 $sql = "SELECT
20046 m.user_id AS id,
20047 u.user_login AS title,
20048 u.user_email AS email,
20049 m.meta_value AS details,
20050 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) AS plan,
20051 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.amount' ) ) AS SIGNED ) AS amount,
20052 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.status' ) ) AS status,
20053 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.created' ) ) AS DATETIME ) AS created
20054 FROM {$wpdb->prefix}usermeta as m
20055 LEFT JOIN {$wpdb->prefix}users AS u ON m.user_id = u.ID
20056 WHERE m.meta_key = %s AND JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) != 'default'
20057 ORDER BY m.user_id DESC
20058 LIMIT 25 OFFSET 0";
20059 $results = $wpdb->get_results( $wpdb->prepare( $sql, $meta_key ), ARRAY_A );// @phpcs:ignore
20060 } elseif ( 'user_registered' === $term ) {
20061 $sql = "SELECT DISTINCT u.ID
20062 FROM {$wpdb->prefix}users u
20063 JOIN {$wpdb->prefix}usermeta um ON u.ID = um.user_id
20064 WHERE um.meta_key = 'voxel:profile_id' ORDER BY ID DESC LIMIT 1";
20065 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
20066 }
20067
20068 if ( 'plan_canceled' === $term ) {
20069 if ( ! empty( $results ) ) {
20070 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
20071 $membership_details = json_decode( $results[0]['details'], true );
20072 $membership_details = is_array( $membership_details ) ? $membership_details : [];
20073
20074 if ( ! isset( $membership_details['billing'] ) && isset( $membership_details['type'] ) ) {
20075 $membership_details = [
20076 'plan' => isset( $membership_details['plan'] ) ? $membership_details['plan'] : 'premium-plus',
20077 'type' => 'order',
20078 'order_id' => isset( $membership_details['order_id'] ) ? $membership_details['order_id'] : 14,
20079 'order_item_id' => isset( $membership_details['order_item_id'] ) ? $membership_details['order_item_id'] : 17,
20080 'billing' => [
20081 'price_key' => isset( $membership_details['price_id'] ) ? $membership_details['price_id'] : 'premium-plus_pz3xfk7z',
20082 'amount' => isset( $membership_details['amount'] ) ? $membership_details['amount'] : 20,
20083 'currency' => isset( $membership_details['currency'] ) ? $membership_details['currency'] : 'USD',
20084 'interval' => isset( $membership_details['interval'] ) ? $membership_details['interval'] : 'month',
20085 'frequency' => isset( $membership_details['interval_count'] ) ? $membership_details['interval_count'] : 1,
20086 'current_period' => [
20087 'start' => gmdate( 'Y-m-d H:i:s' ),
20088 'end' => gmdate( 'Y-m-d H:i:s', strtotime( '+1 month' ) ),
20089 ],
20090 'is_active' => 0,
20091 'is_canceled' => 1,
20092 ],
20093 ];
20094 } else {
20095 if ( isset( $membership_details['billing'] ) ) {
20096 $membership_details['billing']['is_active'] = 0;
20097 $membership_details['billing']['is_canceled'] = 1;
20098 }
20099 }
20100
20101 $context['pluggable_data']['details'] = $membership_details;
20102 $context['response_type'] = 'live';
20103 } else {
20104 $context['pluggable_data'] = WordPress::get_sample_user_context();
20105 $context['pluggable_data']['details'] = [
20106 'plan' => 'premium-plus',
20107 'type' => 'order',
20108 'order_id' => 14,
20109 'order_item_id' => 17,
20110 'billing' => [
20111 'price_key' => 'premium-plus_pz3xfk7z',
20112 'amount' => 20,
20113 'currency' => 'USD',
20114 'interval' => 'month',
20115 'frequency' => 1,
20116 'current_period' => [
20117 'start' => '2025-10-15 09:01:47',
20118 'end' => '2025-11-15 09:01:47',
20119 ],
20120 'is_active' => 0,
20121 'is_canceled' => 1,
20122 ],
20123 ];
20124 $context['response_type'] = 'sample';
20125 }
20126 } elseif ( 'plan_activated' === $term ) {
20127 if ( ! empty( $results ) ) {
20128 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
20129 $membership_details = json_decode( $results[0]['details'], true );
20130 $membership_details = is_array( $membership_details ) ? $membership_details : [];
20131
20132 if ( ! isset( $membership_details['billing'] ) && isset( $membership_details['type'] ) ) {
20133 $membership_details = [
20134 'plan' => isset( $membership_details['plan'] ) ? $membership_details['plan'] : 'premium-plus',
20135 'type' => 'order',
20136 'order_id' => isset( $membership_details['order_id'] ) ? $membership_details['order_id'] : 14,
20137 'order_item_id' => isset( $membership_details['order_item_id'] ) ? $membership_details['order_item_id'] : 17,
20138 'billing' => [
20139 'price_key' => isset( $membership_details['price_id'] ) ? $membership_details['price_id'] : 'premium-plus_pz3xfk7z',
20140 'amount' => isset( $membership_details['amount'] ) ? $membership_details['amount'] : 20,
20141 'currency' => isset( $membership_details['currency'] ) ? $membership_details['currency'] : 'USD',
20142 'interval' => isset( $membership_details['interval'] ) ? $membership_details['interval'] : 'month',
20143 'frequency' => isset( $membership_details['interval_count'] ) ? $membership_details['interval_count'] : 1,
20144 'current_period' => [
20145 'start' => gmdate( 'Y-m-d H:i:s' ),
20146 'end' => gmdate( 'Y-m-d H:i:s', strtotime( '+1 month' ) ),
20147 ],
20148 'is_active' => 1,
20149 'is_canceled' => '',
20150 ],
20151 ];
20152 }
20153
20154 $context['pluggable_data']['details'] = $membership_details;
20155 $context['response_type'] = 'live';
20156 } else {
20157 $context['pluggable_data'] = WordPress::get_sample_user_context();
20158 $context['pluggable_data']['details'] = [
20159 'plan' => 'premium-plus',
20160 'type' => 'order',
20161 'order_id' => 14,
20162 'order_item_id' => 17,
20163 'billing' => [
20164 'price_key' => 'premium-plus_pz3xfk7z',
20165 'amount' => 20,
20166 'currency' => 'USD',
20167 'interval' => 'month',
20168 'frequency' => 1,
20169 'current_period' => [
20170 'start' => '2025-10-15 09:01:47',
20171 'end' => '2025-11-15 09:01:47',
20172 ],
20173 'is_active' => 1,
20174 'is_canceled' => '',
20175 ],
20176 ];
20177 $context['response_type'] = 'sample';
20178 }
20179 } elseif ( 'plan_updated' === $term ) {
20180 if ( ! empty( $results ) ) {
20181 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
20182 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
20183 $context['response_type'] = 'live';
20184 } else {
20185 $context['pluggable_data'] = WordPress::get_sample_user_context();
20186 $context['pluggable_data']['details'] = [
20187 'plan' => 'premium-plus',
20188 'type' => 'subscription',
20189 'price_id' => 'premium-plus_pz3xfk7z',
20190 'status' => 'active',
20191 'metadata' => [
20192 'voxel:payment_for' => 'membership',
20193 'voxel:plan' => 'premium-plus',
20194 ],
20195 ];
20196 $context['response_type'] = 'sample';
20197 }
20198 } elseif ( 'user_registered' === $term ) {
20199 if ( ! empty( $results ) ) {
20200 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['ID'] );
20201 $context['response_type'] = 'live';
20202 } else {
20203 $context['pluggable_data'] = WordPress::get_sample_user_context();
20204 $context['response_type'] = 'sample';
20205 }
20206 }
20207 return $context;
20208 }
20209
20210 /**
20211 * Get Voxel Orders Last Data
20212 *
20213 * @param array $data data.
20214 *
20215 * @return array|mixed|string
20216 */
20217 public function search_voxel_order_triggers_last_data( $data ) {
20218
20219 global $wpdb;
20220 $context = [];
20221 $term = $data['search_term'];
20222
20223 if ( ! class_exists( 'Voxel\Product_Types\Orders\Order' ) ) {
20224 return [];
20225 }
20226
20227 if ( 'order_placed' === $term || 'order_canceled' === $term ) {
20228 if ( 'order_placed' === $term ) {
20229 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders ORDER BY id DESC LIMIT 1";
20230 } elseif ( 'order_canceled' === $term ) {
20231 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' ORDER BY id DESC LIMIT 1";
20232 }
20233
20234 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
20235 if ( ! empty( $results ) ) {
20236 // Get Order.
20237 $context['pluggable_data']['id'] = $results[0]['id'];
20238 $context['pluggable_data']['vendor_id'] = $results[0]['vendor_id'];
20239 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
20240 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
20241 $context['pluggable_data']['status'] = $results[0]['status'];
20242 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
20243 $context['pluggable_data']['subtotal'] = $results[0]['subtotal'];
20244 $context['pluggable_data']['total'] = $results[0]['total'];
20245 // Get order items.
20246 $order = \Voxel\Product_Types\Orders\Order::find(
20247 [
20248 'id' => $results[0]['id'],
20249 ]
20250 );
20251 $order_items = $order->get_items();
20252 $context['pluggable_data']['tax_amount'] = $order->get_tax_amount();
20253 $context['pluggable_data']['discount_amount'] = $order->get_discount_amount();
20254 $context['pluggable_data']['shipping_amount'] = $order->get_shipping_amount();
20255 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
20256 foreach ( $order_items as $item ) {
20257 $addon_data = [];
20258 if ( is_object( $item ) && method_exists( $item, 'get_addons' ) ) {
20259 // Get addon details.
20260 $addons = $item->get_addons();
20261 $addon_data = [];
20262
20263 // Add the addons to the simple entry.
20264 if ( $addons && isset( $addons['summary'] ) ) {
20265 $addon_data = $addons['summary'];
20266 }
20267 }
20268 $context['pluggable_data']['order_items'][] = [
20269 'id' => $item->get_id(),
20270 'type' => $item->get_type(),
20271 'currency' => $item->get_currency(),
20272 'quantity' => $item->get_quantity(),
20273 'subtotal' => $item->get_subtotal(),
20274 'product_id' => $item->get_post()->get_id(),
20275 'product_label' => $item->get_product_label(),
20276 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
20277 'product_link' => $item->get_product_link(),
20278 'description' => $item->get_product_description(),
20279 'addon_data' => $addon_data,
20280 ];
20281 // If booking item, get booking details.
20282 if ( 'booking' === $item->get_type() ) {
20283 $details = $item->get_order_page_details();
20284 $context['pluggable_data']['order_items']['booking_type'] = $details['booking']['type'];
20285 if ( isset( $details['booking']['count_mode'] ) ) {
20286 $context['pluggable_data']['order_items']['booking_count_mode'] = $details['booking']['count_mode'];
20287 }
20288 if ( 'date_range' === $details['booking']['type'] ) {
20289 $context['pluggable_data']['order_items']['booking_start_date'] = $details['booking']['start_date'];
20290 $context['pluggable_data']['order_items']['booking_end_date'] = $details['booking']['end_date'];
20291 } elseif ( 'single_day' === $details['booking']['type'] ) {
20292 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
20293 } elseif ( 'timeslots' === $details['booking']['type'] ) {
20294 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
20295 $context['pluggable_data']['order_items']['booking_slot_from'] = $details['booking']['slot']['from'];
20296 $context['pluggable_data']['order_items']['booking_slot_to'] = $details['booking']['slot']['to'];
20297 }
20298 }
20299 }
20300 // Get Customer.
20301 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
20302 $context['response_type'] = 'live';
20303 } else {
20304 $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
20305 }
20306 } elseif ( 'order_approved' === $term || 'order_declined' === $term ) {
20307 if ( 'order_approved' === $term ) {
20308 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'completed' ORDER BY id DESC LIMIT 1";
20309 } elseif ( 'order_declined' === $term ) {
20310 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' ORDER BY id DESC LIMIT 1";
20311 }
20312 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
20313 if ( ! empty( $results ) ) {
20314 // Get Order.
20315 $context['pluggable_data']['id'] = $results[0]['id'];
20316 $context['pluggable_data']['vendor_id'] = $results[0]['vendor_id'];
20317 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
20318 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
20319 $context['pluggable_data']['status'] = $results[0]['status'];
20320 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
20321 $context['pluggable_data']['subtotal'] = $results[0]['subtotal'];
20322 $context['pluggable_data']['total'] = $results[0]['total'];
20323 // Get order items.
20324 $order = \Voxel\Product_Types\Orders\Order::find(
20325 [
20326 'id' => $results[0]['id'],
20327 ]
20328 );
20329 $order_items = $order->get_items();
20330 $context['pluggable_data']['tax_amount'] = $order->get_tax_amount();
20331 $context['pluggable_data']['discount_amount'] = $order->get_discount_amount();
20332 $context['pluggable_data']['shipping_amount'] = $order->get_shipping_amount();
20333 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
20334 foreach ( $order_items as $item ) {
20335 $addon_data = [];
20336 if ( is_object( $item ) && method_exists( $item, 'get_addons' ) ) {
20337 // Get addon details.
20338 $addons = $item->get_addons();
20339 $addon_data = [];
20340
20341 // Add the addons to the simple entry.
20342 if ( $addons && isset( $addons['summary'] ) ) {
20343 $addon_data = $addons['summary'];
20344 }
20345 }
20346 $context['pluggable_data']['order_items'][] = [
20347 'id' => $item->get_id(),
20348 'type' => $item->get_type(),
20349 'currency' => $item->get_currency(),
20350 'quantity' => $item->get_quantity(),
20351 'subtotal' => $item->get_subtotal(),
20352 'product_id' => $item->get_post()->get_id(),
20353 'product_label' => $item->get_product_label(),
20354 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
20355 'product_link' => $item->get_product_link(),
20356 'description' => $item->get_product_description(),
20357 'addon_data' => $addon_data,
20358 ];
20359 // If booking item, get booking details.
20360 if ( 'booking' === $item->get_type() ) {
20361 $details = $item->get_order_page_details();
20362 $context['pluggable_data']['order_items']['booking_type'] = $details['booking']['type'];
20363 if ( isset( $details['booking']['count_mode'] ) ) {
20364 $context['pluggable_data']['order_items']['booking_count_mode'] = $details['booking']['count_mode'];
20365 }
20366 if ( 'date_range' === $details['booking']['type'] ) {
20367 $context['pluggable_data']['order_items']['booking_start_date'] = $details['booking']['start_date'];
20368 $context['pluggable_data']['order_items']['booking_end_date'] = $details['booking']['end_date'];
20369 } elseif ( 'single_day' === $details['booking']['type'] ) {
20370 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
20371 } elseif ( 'timeslots' === $details['booking']['type'] ) {
20372 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
20373 $context['pluggable_data']['order_items']['booking_slot_from'] = $details['booking']['slot']['from'];
20374 $context['pluggable_data']['order_items']['booking_slot_to'] = $details['booking']['slot']['to'];
20375 }
20376 }
20377 }
20378 // Get Customer.
20379 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
20380 // Get Vendor.
20381 $context['pluggable_data']['vendor'] = WordPress::get_user_context( $results[0]['vendor_id'] );
20382 $context['response_type'] = 'live';
20383 } else {
20384 $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
20385 }
20386 } elseif ( 'order_promotion_activated' === $term || 'order_promotion_canceled' === $term || 'order_claim_listing' === $term ) {
20387 if ( 'order_promotion_activated' === $term ) {
20388 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'completed' AND details LIKE '%voxel:promotion%' ORDER BY id DESC LIMIT 1";
20389 } elseif ( 'order_promotion_canceled' === $term ) {
20390 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' AND details LIKE '%voxel:promotion%' ORDER BY id DESC LIMIT 1";
20391 } elseif ( 'order_claim_listing' === $term ) {
20392 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE details LIKE '%voxel:claim%' ORDER BY id DESC LIMIT 1";
20393 }
20394 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
20395 if ( ! empty( $results ) ) {
20396 // Get Order.
20397 $context['pluggable_data']['id'] = $results[0]['id'];
20398 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
20399 $context['pluggable_data']['status'] = $results[0]['status'];
20400 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
20401 // Get order items.
20402 $order = \Voxel\Product_Types\Orders\Order::find(
20403 [
20404 'id' => $results[0]['id'],
20405 ]
20406 );
20407 $order_items = $order->get_items();
20408 $context['pluggable_data']['details'] = $order->get_details();
20409 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
20410 foreach ( $order_items as $item ) {
20411 $context['pluggable_data']['order_items'][] = [
20412 'id' => $item->get_id(),
20413 'type' => $item->get_type(),
20414 'currency' => $item->get_currency(),
20415 'quantity' => $item->get_quantity(),
20416 'subtotal' => $item->get_subtotal(),
20417 'product_id' => $item->get_post()->get_id(),
20418 'product_label' => $item->get_product_label(),
20419 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
20420 'product_link' => $item->get_product_link(),
20421 'description' => $item->get_product_description(),
20422 ];
20423 }
20424 // Get Customer.
20425 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
20426 $context['response_type'] = 'live';
20427 } else {
20428 $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
20429 }
20430 }
20431 return $context;
20432 }
20433
20434 /**
20435 * Get Voxel Timeline Comments Last Data
20436 *
20437 * @param array $data data.
20438 *
20439 * @return array|mixed|string
20440 */
20441 public function search_voxel_timeline_triggers_last_data( $data ) {
20442
20443 global $wpdb;
20444 $context = [];
20445 $term = $data['search_term'];
20446 $sql = '';
20447 if ( 'new_comment_timeline' === $term ) {
20448 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE parent_id IS NULL ORDER BY id DESC LIMIT 1";
20449 } elseif ( 'comment_reply_timeline' === $term ) {
20450 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE parent_id IS NOT NULL ORDER BY id DESC LIMIT 1";
20451 } elseif ( 'comment_liked' === $term ) {
20452 $sql = "SELECT vtrl.user_id as like_user_id, vtrl.reply_id as like_reply_id, vtr.* FROM {$wpdb->prefix}voxel_timeline_reply_likes_v2 vtrl JOIN {$wpdb->prefix}voxel_timeline_replies vtr ON vtrl.reply_id = vtr.ID WHERE vtr.like_count > 0";
20453 } elseif ( 'user_timeline_post_liked' === $term ) {
20454 $sql = "SELECT vtsl.user_id as like_user_id, vtsl.status_id as like_status_id, vtr.* FROM {$wpdb->prefix}voxel_timeline_status_likes vtsl JOIN {$wpdb->prefix}voxel_timeline vt ON vtsl.status_id = vt.id WHERE vt.like_count > 0";
20455 } elseif ( 'user_timeline_post_quoted' === $term ) {
20456 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline WHERE feed = %s AND quote_of IS NOT NULL ORDER BY id DESC LIMIT 1";
20457 } elseif ( 'user_timeline_post_reposted' === $term ) {
20458 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline WHERE feed = %s AND repost_of IS NOT NULL ORDER BY id DESC LIMIT 1";
20459 }
20460 if ( 'user_timeline_post_reposted' === $term || 'user_timeline_post_quoted' === $term ) {
20461 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user_timeline' ), ARRAY_A );// @phpcs:ignore
20462 } else {
20463 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
20464 }
20465 if ( 'new_comment_timeline' === $term ) {
20466 if ( ! empty( $results ) ) {
20467 $context['pluggable_data']['comment_by'] = WordPress::get_user_context( $results[0]['user_id'] );
20468 $context['pluggable_data']['id'] = $results[0]['id'];
20469 $context['pluggable_data']['status_id'] = $results[0]['status_id'];
20470 $context['pluggable_data']['content'] = $results[0]['content'];
20471 $context['response_type'] = 'live';
20472 } else {
20473 $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
20474 }
20475 } elseif ( 'comment_reply_timeline' === $term ) {
20476 if ( ! empty( $results ) ) {
20477 $comment_sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE id = %d";
20478 $comment_result = $wpdb->get_results( $wpdb->prepare( $comment_sql, $results[0]['parent_id']), ARRAY_A );// @phpcs:ignore
20479 $context['pluggable_data']['replied_by'] = WordPress::get_user_context( $results[0]['user_id'] );
20480 $context['pluggable_data']['comment_by'] = WordPress::get_user_context( $comment_result[0]['user_id'] );
20481 $context['pluggable_data']['comment'] = $comment_result[0]['content'];
20482 $context['pluggable_data']['comment_id'] = $results[0]['parent_id'];
20483 $context['pluggable_data']['reply_id'] = $results[0]['id'];
20484 $context['pluggable_data']['reply'] = $results[0]['content'];
20485 $context['response_type'] = 'live';
20486 } else {
20487 $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
20488 }
20489 } elseif ( 'comment_liked' === $term ) {
20490 if ( ! empty( $results ) ) {
20491 $user = get_userdata( $results[0]['user_id'] );
20492 if ( $user ) {
20493 $user_data = (array) $user->data;
20494 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
20495 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
20496 $context['pluggable_data']['user_email'] = $user_data['user_email'];
20497 }
20498 $recipient_user = get_userdata( $results[0]['like_user_id'] );
20499 if ( $recipient_user ) {
20500 $recipient_user_data = (array) $recipient_user->data;
20501 $context['pluggable_data']['recipient']['user_display_name'] = $recipient_user_data['display_name'];
20502 $context['pluggable_data']['recipient']['user_name'] = $recipient_user_data['user_nicename'];
20503 $context['pluggable_data']['recipient']['user_email'] = $recipient_user_data['user_email'];
20504 }
20505 if ( class_exists( 'Voxel\Timeline\Reply' ) ) {
20506 $reply_details = \Voxel\Timeline\Reply::get( $results[0]['id'] );
20507 foreach ( (array) $reply_details as $key => $value ) {
20508 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
20509 $context['pluggable_data'][ $clean_key ] = $value;
20510 }
20511 }
20512 $context['response_type'] = 'live';
20513 } else {
20514 $context = json_decode( '{"pluggable_data":{"user_display_name":"test test","user_name":"test","user_email":"test@yopmail.com","recipient":{"user_display_name":"Arian","user_name":"admin","user_email":"admin@example.com"},"id":1,"user_id":2,"published_as":null,"status_id":56,"parent_id":null,"content":"@admin test","details":[],"created_at":"2025-01-22 12:46:33","edited_at":null,"like_count":1,"reply_count":0,"liked_by_user":false,"last3_liked":[{"post_id":null,"user_id":1}]},"response_type":"sample"}', true );// @phpcs:ignore
20515 }
20516 } elseif ( 'user_timeline_post_liked' === $term ) {
20517 if ( ! empty( $results ) ) {
20518 $user = get_userdata( $results[0]['user_id'] );
20519 if ( $user ) {
20520 $user_data = (array) $user->data;
20521 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
20522 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
20523 $context['pluggable_data']['user_email'] = $user_data['user_email'];
20524 }
20525 $recipient_user = get_userdata( $results[0]['like_user_id'] );
20526 if ( $recipient_user ) {
20527 $recipient_user_data = (array) $recipient_user->data;
20528 $context['pluggable_data']['recipient']['user_display_name'] = $recipient_user_data['display_name'];
20529 $context['pluggable_data']['recipient']['user_name'] = $recipient_user_data['user_nicename'];
20530 $context['pluggable_data']['recipient']['user_email'] = $recipient_user_data['user_email'];
20531 }
20532 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
20533 $reply_details = \Voxel\Timeline\Status::get( $results[0]['id'] );
20534 foreach ( (array) $reply_details as $key => $value ) {
20535 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
20536 $context['pluggable_data'][ $clean_key ] = $value;
20537 }
20538 }
20539 $context['response_type'] = 'live';
20540 } else {
20541 $context = json_decode( '{"pluggable_data":{"user_display_name":"test test","user_name":"test","user_email":"test@yopmail.com","recipient":{"user_display_name":"Arian","user_name":"admin","user_email":"admin@example.com"},"id":51,"user_id":2,"post_id":null,"published_as":null,"content":"test","feed":"user_timeline","moderation":0,"repost_of":null,"quote_of":null,"created_at":"2025-01-22 11:10:06","edited_at":null,"review_score":null,"like_count":0,"reply_count":0,"details":{"files":"541"},"liked_by_user":false,"reposted_by_user":false,"last3_liked":[],"friends_reposted":[],"friends_liked":[]},"response_type":"sample"}', true );// @phpcs:ignore
20542 }
20543 } elseif ( 'user_timeline_post_reposted' === $term || 'user_timeline_post_quoted' === $term ) {
20544 if ( ! empty( $results ) ) {
20545 $author = get_userdata( $results[0]['user_id'] );
20546 if ( $author ) {
20547 $author_data = (array) $author->data;
20548 $context['pluggable_data']['author_display_name'] = $author_data['display_name'];
20549 $context['pluggable_data']['author_name'] = $author_data['user_nicename'];
20550 $context['pluggable_data']['author_email'] = $author_data['user_email'];
20551 }
20552 $column = 'user_timeline_post_reposted' === $term ? 'repost_of' : 'quote_of';
20553 $recipient_sql = "SELECT user_id FROM {$wpdb->prefix}voxel_timeline WHERE id = %d";
20554 $recipient_results = $wpdb->get_results( $wpdb->prepare( $recipient_sql, $results[0][$column] ), ARRAY_A ); // @phpcs:ignore
20555 if ( ! empty( $recipient_results ) ) {
20556 $recipient_user = get_userdata( $recipient_results[0]['user_id'] );
20557 if ( $recipient_user ) {
20558 $recipient_user_data = (array) $recipient_user->data;
20559 $context['pluggable_data']['recipient']['user_display_name'] = $recipient_user_data['display_name'];
20560 $context['pluggable_data']['recipient']['user_name'] = $recipient_user_data['user_nicename'];
20561 $context['pluggable_data']['recipient']['user_email'] = $recipient_user_data['user_email'];
20562 }
20563 }
20564 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
20565 $status_details = \Voxel\Timeline\Status::get( $results[0]['id'] );
20566 $details_column = 'user_timeline_post_reposted' === $term ? 'repost_of' : 'quote_of';
20567 $related_details = \Voxel\Timeline\Status::get( $results[0][ $details_column ] );
20568 foreach ( [
20569 'status' => $status_details,
20570 $details_column => $related_details,
20571 ] as $key => $details ) {
20572 foreach ( (array) $details as $sub_key => $value ) {
20573 $clean_key = preg_replace( '/^\0.*?\0/', '', $sub_key );
20574 $context['pluggable_data'][ $key ][ $clean_key ] = $value;
20575 }
20576 }
20577 }
20578 $context['response_type'] = 'live';
20579 } else {
20580 if ( 'user_timeline_post_reposted' === $term ) {
20581 $context = json_decode( '{"pluggable_data":{"author_display_name":"test test","author_name":"test","author_email":"test@yopmail.com","recipient":{"user_display_name":"Arian","user_name":"admin","user_email":"admin@example.com"},"status":{"id":59,"user_id":2,"post_id":null,"published_as":null,"content":"","feed":"user_timeline","moderation":1,"repost_of":12,"quote_of":null,"created_at":"2025-01-23 04:02:45","edited_at":null,"review_score":null,"like_count":0,"reply_count":0,"details":[],"liked_by_user":false,"reposted_by_user":false,"last3_liked":[],"friends_reposted":[],"friends_liked":[]},"repost_of":{"id":12,"user_id":1,"post_id":447,"published_as":null,"content":"hello\n\nhttps://example.com/mountain-bike/","feed":"post_wall","moderation":1,"repost_of":null,"quote_of":null,"created_at":"2025-01-17 05:26:23","edited_at":null,"review_score":null,"like_count":0,"reply_count":0,"details":{"link_preview":{"url":"https://example.com/mountain-bike/","title":"Mountain Bike - spacesdemo.wp1.sh","image":"https://example.comt/uploads/2025/01/test.jpg"}},"liked_by_user":false,"reposted_by_user":false,"last3_liked":[],"friends_reposted":[],"friends_liked":[]}},"response_type":"sample"}', true );// @phpcs:ignore
20582 } else {
20583 $context = json_decode( '{"pluggable_data":{"author_display_name":"test test","author_name":"test","author_email":"test@yopmail.com","recipient":{"user_display_name":"Arian","user_name":"admin","user_email":"admin@example.com"},"status":{"id":59,"user_id":2,"post_id":null,"published_as":null,"content":"","feed":"user_timeline","moderation":1,"quote_of":12,"quote_of":null,"created_at":"2025-01-23 04:02:45","edited_at":null,"review_score":null,"like_count":0,"reply_count":0,"details":[],"liked_by_user":false,"quoted_by_user":false,"last3_liked":[],"friends_quoted":[],"friends_liked":[]},"quote_of":{"id":12,"user_id":1,"post_id":447,"published_as":null,"content":"hello\n\nhttps://example.com/mountain-bike/","feed":"post_wall","moderation":1,"quote_of":null,"quote_of":null,"created_at":"2025-01-17 05:26:23","edited_at":null,"review_score":null,"like_count":0,"reply_count":0,"details":{"link_preview":{"url":"https://example.com/mountain-bike/","title":"Mountain Bike - spacesdemo.wp1.sh","image":"https://example.comt/uploads/2025/01/test.jpg"}},"liked_by_user":false,"quoted_by_user":false,"last3_liked":[],"friends_quoted":[],"friends_liked":[]}},"response_type":"sample"}', true );// @phpcs:ignore
20584 }
20585 }
20586 }
20587 return $context;
20588 }
20589
20590 /**
20591 * Get Voxel Timeline Comments Last Data
20592 *
20593 * @param array $data data.
20594 *
20595 * @return array|mixed|string
20596 */
20597 public function search_voxel_posts_triggers_last_data( $data ) {
20598 $context = [];
20599 global $wpdb;
20600 $post_type = 'post';
20601 $term = $data['search_term'];
20602 if ( isset( $data['filter']['post_type']['value'] ) ) {
20603 $post_type = $data['filter']['post_type']['value'];
20604 }
20605 $results = [];
20606 if ( 'post_approved' === $term ) {
20607 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
20608 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type, 'publish'), ARRAY_A );// @phpcs:ignore
20609 } elseif ( 'post_rejected' === $term ) {
20610 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
20611 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type, 'rejected'), ARRAY_A );// @phpcs:ignore
20612 } elseif ( 'post_reviewed' === $term ) {
20613 $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
20614 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";
20615 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type, '%rating%' ), ARRAY_A );// @phpcs:ignore
20616 } elseif ( 'post_review_approved' === $term ) {
20617 $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
20618 WHERE vt.moderation = %d AND pm.meta_key LIKE '%voxel:review_stats%' AND p.post_type LIKE %s AND vt.details IS NOT NULL AND vt.details LIKE %s AND vt.feed = %s";
20619 $results = $wpdb->get_results( $wpdb->prepare( $sql, 1, $post_type, '%rating%', 'post_reviews' ), ARRAY_A );// @phpcs:ignore
20620 } elseif ( 'post_submitted' === $term ) {
20621 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
20622 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type), ARRAY_A );// @phpcs:ignore
20623 } elseif ( 'post_updated' === $term ) {
20624 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
20625 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type ), ARRAY_A );// @phpcs:ignore
20626 } elseif ( 'collection_post_submitted' === $term ) {
20627 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
20628 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'collection' ), ARRAY_A );// @phpcs:ignore
20629 } elseif ( 'collection_post_updated' === $term ) {
20630 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
20631 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'collection' ), ARRAY_A );// @phpcs:ignore
20632 } elseif ( 'new_wall_post_by_user' === $term ) {
20633 $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";
20634 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type, '%rating%' ), ARRAY_A );// @phpcs:ignore
20635 } elseif ( 'wall_post_approved' === $term ) {
20636 $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 vt.feed = %s AND p.post_type LIKE %s AND vt.moderation = %d AND vt.details IS NOT NULL ORDER BY vt.id DESC LIMIT 1";
20637 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'post_wall', $post_type, 1 ), ARRAY_A );// @phpcs:ignore
20638 } elseif ( 'timeline_post_approved' === $term ) {
20639 $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 vt.feed = %s AND vt.moderation = %d AND p.post_type LIKE %s ORDER BY vt.id DESC LIMIT 1";
20640 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'post_timeline', 1, $post_type ), ARRAY_A );// @phpcs:ignore
20641 } elseif ( 'taxonomy_updated' === $term ) {
20642 $sql = "SELECT tr.object_id as post_id, tt.taxonomy, tr.term_taxonomy_id
20643 FROM {$wpdb->prefix}term_relationships tr
20644 JOIN {$wpdb->prefix}term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
20645 JOIN {$wpdb->prefix}posts p ON tr.object_id = p.ID
20646 WHERE p.post_type LIKE %s
20647 ORDER BY tr.object_id DESC LIMIT 1";
20648 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type ), ARRAY_A );// @phpcs:ignore
20649 } elseif ( 'post_followed' === $term ) {
20650 $sql = "SELECT * FROM {$wpdb->prefix}voxel_followers WHERE object_type = %s LIMIT 1";
20651 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'post' ), ARRAY_A );// @phpcs:ignore
20652 }
20653 if ( 'post_approved' === $term || 'post_rejected' === $term || 'post_submitted' === $term || 'post_updated' === $term || 'timeline_post_approved' === $term ) {
20654 if ( ! empty( $results ) ) {
20655 $context['pluggable_data'] = WordPress::get_post_context( $results[0]['ID'] );
20656 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['ID'] );
20657 $context['response_type'] = 'live';
20658 } else {
20659 $context['pluggable_data'] = [
20660 'ID' => 557,
20661 'post' => 557,
20662 'post_author' => 1,
20663 'post_date' => '2022-11-18 12:18:14',
20664 'post_date_gmt' => '2022-11-18 12:18:14',
20665 'post_content' => 'Test Post Content',
20666 'post_title' => 'Test Post',
20667 'post_excerpt' => '',
20668 'post_status' => 'published',
20669 'comment_status' => 'open',
20670 'ping_status' => 'open',
20671 'post_password' => '',
20672 'post_name' => 'test-post',
20673 'to_ping' => '',
20674 'pinged' => '',
20675 'post_modified' => '2022-11-18 12:18:14',
20676 'post_modified_gmt' => '2022-11-18 12:18:14',
20677 'post_content_filtered' => '',
20678 'post_parent' => 0,
20679 'guid' => 'https://example.com/test-post/',
20680 'menu_order' => 0,
20681 'post_type' => 'post',
20682 'post_mime_type' => '',
20683 'comment_count' => 0,
20684 'filter' => 'raw',
20685 ];
20686 $context['response_type'] = 'sample';
20687 }
20688 } elseif ( 'post_reviewed' === $term || 'post_review_approved' === $term ) {
20689 if ( ! empty( $results ) ) {
20690 $context['pluggable_data'] = WordPress::get_post_context( $results[0]['post_id'] );
20691 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['post_id'] );
20692 $context['pluggable_data']['review_content'] = $results[0]['content'];
20693 $context['pluggable_data']['review_created_at'] = $results[0]['created_at'];
20694 $context['pluggable_data']['review_details'] = json_decode( $results[0]['details'], true );
20695 $context['pluggable_data']['review_by'] = WordPress::get_user_context( $results[0]['user_id'] );
20696 $context['response_type'] = 'live';
20697 } else {
20698 $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:\/\/example-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
20699 }
20700 } elseif ( 'collection_post_submitted' === $term || 'collection_post_updated' === $term ) {
20701 if ( ! empty( $results ) ) {
20702 $context['pluggable_data'] = Voxel::get_post_fields( $results[0]['ID'] );
20703 $context['pluggable_data']['collection'] = WordPress::get_post_context( $results[0]['ID'] );
20704 $context['response_type'] = 'live';
20705 } else {
20706 $context = json_decode(
20707 '{"pluggable_data":{"field_step-general": null,"field_title": "First Collection","field_items": [8909,8293],"collection": {"ID": 9142,"post_author": "35",
20708 "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
20709 }
20710 } elseif ( 'new_wall_post_by_user' === $term || 'wall_post_approved' === $term ) {
20711 if ( ! empty( $results ) ) {
20712 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['post_id'] );
20713 $user = get_userdata( $results[0]['user_id'] );
20714 if ( $user ) {
20715 $user_data = (array) $user->data;
20716 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
20717 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
20718 $context['pluggable_data']['user_email'] = $user_data['user_email'];
20719 $context['pluggable_data']['user_id'] = $results[0]['user_id'];
20720 }
20721 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
20722 $args = [
20723 'post_id' => $results[0]['post_id'],
20724 'order_by' => 'created_at',
20725 'order' => 'desc',
20726 'feed' => $results[0]['feed'],
20727 'limit' => 1,
20728 'moderation' => $results[0]['moderation'],
20729 ];
20730 $statuses = \Voxel\Timeline\Status::query( $args );
20731 $status_details = $statuses['items'][0];
20732 foreach ( (array) $status_details as $key => $value ) {
20733 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
20734 if ( is_object( $value ) ) {
20735 $encoded_value = wp_json_encode( $value );
20736 if ( is_string( $encoded_value ) ) {
20737 $value = json_decode( $encoded_value, true );
20738 }
20739 }
20740 $context['pluggable_data']['wall_post'][ $clean_key ] = $value;
20741 }
20742 }
20743 $context['response_type'] = 'live';
20744 } else {
20745 $context = json_decode( '{"pluggable_data":{"post":{"field_step-general":null,"field_title":"How Data Fundamentally Changed Marketing","field__thumbnail_id":{"_thumbnail_id_0_url":"https://example.com/wp-content/uploads/2025/01/169cbc8.jpg"},"field_taxonomy":"General"},"user_display_name":"john d","user_name":"john","user_email":"johnd@example.com","user_id":"2","wall_post":{"id":31,"user_id":2,"post_id":447,"published_as":null,"content":"new hello","feed":"post_wall","moderation":0,"repost_of":null,"quote_of":null,"created_at":"2025-01-22 08:56:24","edited_at":null,"review_score":null,"like_count":0,"reply_count":0,"details":[],"liked_by_user":false,"reposted_by_user":false,"last3_liked":[],"friends_reposted":[],"friends_liked":[]}},"response_type":"sample"}', true );// @phpcs:ignore
20746 }
20747 } elseif ( 'taxonomy_updated' === $term ) {
20748 if ( ! empty( $results ) ) {
20749 $post_id = $results[0]['post_id'];
20750 $context['pluggable_data'] = WordPress::get_post_context( $post_id );
20751 $context['pluggable_data']['post'] = Voxel::get_post_fields( $post_id );
20752 $context['pluggable_data']['post_type'] = get_post_type( $post_id );
20753
20754 // Get taxonomy details.
20755 $term_obj = get_term( $results[0]['term_taxonomy_id'] );
20756 if ( $term_obj && ! is_wp_error( $term_obj ) ) {
20757 $context['pluggable_data']['taxonomy'] = $results[0]['taxonomy'];
20758 $context['pluggable_data']['update_type'] = 'updated';
20759 $context['pluggable_data']['terms_added'] = [ $results[0]['term_taxonomy_id'] ];
20760 $context['pluggable_data']['terms_removed'] = [];
20761 $context['pluggable_data']['current_terms'] = [ $results[0]['term_taxonomy_id'] ];
20762 $context['pluggable_data']['previous_terms'] = [];
20763 $context['pluggable_data']['added_terms_details'] = [
20764 [
20765 'term_id' => $term_obj->term_id,
20766 'name' => $term_obj->name,
20767 'slug' => $term_obj->slug,
20768 ],
20769 ];
20770 $context['pluggable_data']['removed_terms_details'] = [];
20771 $context['pluggable_data']['current_terms_details'] = [
20772 [
20773 'term_id' => $term_obj->term_id,
20774 'name' => $term_obj->name,
20775 'slug' => $term_obj->slug,
20776 ],
20777 ];
20778 $context['pluggable_data']['is_voxel_taxonomy_field'] = false;
20779 $context['pluggable_data']['voxel_field_data'] = null;
20780 }
20781 $context['response_type'] = 'live';
20782 } else {
20783 $context = json_decode( '{"pluggable_data":{"ID": 557,"post_author": "1","post_date": "2022-11-18 12:18:14","post_date_gmt": "2022-11-18 12:18:14","post_content": "Test Post Content","post_title": "Test Post","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "open","post_password": "","post_name": "test-post","to_ping": "","pinged": "","post_modified": "2022-11-18 12:18:14","post_modified_gmt": "2022-11-18 12:18:14","post_content_filtered": "","post_parent": 0,"guid": "https://example.com/test-post/","menu_order": 0,"post_type": "post","post_mime_type": "","comment_count": 0,"filter": "raw","post": {"field_step-general": null,"field_title": "Test Post","field_taxonomy": "General"},"post_type": "post","taxonomy": "category","update_type": "added","terms_added": [1],"terms_removed": [],"current_terms": [1],"previous_terms": [],"added_terms_details": [{"term_id": 1,"name": "Uncategorized","slug": "uncategorized"}],"removed_terms_details": [],"current_terms_details": [{"term_id": 1,"name": "Uncategorized","slug": "uncategorized"}],"is_voxel_taxonomy_field": false,"voxel_field_data": null},"response_type":"sample"}', true );// @phpcs:ignore
20784 }
20785 } elseif ( 'post_followed' === $term ) {
20786 if ( ! empty( $results ) ) {
20787 $follow_data = [
20788 'post_id' => $results[0]['object_id'],
20789 'user_id' => $results[0]['follower_id'],
20790 'status' => $results[0]['status'],
20791 ];
20792 $follow_data = array_merge( $follow_data, Voxel::get_post_fields( $results[0]['object_id'] ), WordPress::get_post_context( $results[0]['object_id'] ) );
20793 $followers_sql = "SELECT COUNT(*) FROM {$wpdb->prefix}voxel_followers WHERE object_id= %d";
20794 $followers = $wpdb->get_var( $wpdb->prepare( $followers_sql, $results[0]['object_id'] ));// @phpcs:ignore
20795 $follow_data['total_followers'] = $followers;
20796 $context['pluggable_data'] = $follow_data;
20797 $context['response_type'] = 'live';
20798 } else {
20799 $context = json_decode( '{"pluggable_data":{"field_step-general":null,"field_title":"How Data Fundamentally Changed Marketing","field__thumbnail_id":{"_thumbnail_id_0_url":"https://example.com/wp-content/uploads/2025/01/169cbc8.jpg"},"field_taxonomy":"General","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:\/\/example-wpnew.local\/places\/sach-pizza\/","menu_order": 0,"post_type": "places","post_mime_type": "","comment_count": "0","filter": "raw","total_followers":4,"post_id": 447,"user_id": 2,"status":1},"response_type":"sample"}', true );// @phpcs:ignore
20800 }
20801 }
20802 return $context;
20803 }
20804
20805 /**
20806 * Get Voxel Profiles Last Data
20807 *
20808 * @param array $data data.
20809 *
20810 * @return array|mixed|string
20811 */
20812 public function search_voxel_profiles_triggers_last_data( $data ) {
20813
20814 global $wpdb;
20815 $context = [];
20816 $term = $data['search_term'];
20817
20818 if ( ! class_exists( 'Voxel\Timeline\Status' ) || ! class_exists( 'Voxel\Post_Type' ) ) {
20819 return [];
20820 }
20821
20822 if ( 'profile_created' === $term ) {
20823 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
20824 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
20825 } elseif ( 'profile_approved' === $term ) {
20826 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
20827 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile', 'publish' ), ARRAY_A );// @phpcs:ignore
20828 } elseif ( 'profile_rejected' === $term ) {
20829 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
20830 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile', 'rejected' ), ARRAY_A );// @phpcs:ignore
20831 } elseif ( 'profile_reviewed' === $term ) {
20832 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline vt LEFT JOIN {$wpdb->prefix}posts pm
20833 ON vt.post_id = pm.ID
20834 WHERE pm.post_type LIKE %s AND vt.details IS NOT NULL ORDER BY vt.id DESC LIMIT 1";
20835 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
20836 } elseif ( 'profile_updated' === $term ) {
20837 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
20838 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
20839 } elseif ( 'profile_new_wall_post' === $term ) {
20840 $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";
20841 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
20842 }
20843 if ( 'profile_created' === $term || 'profile_approved' === $term || 'profile_rejected' === $term || 'profile_updated' === $term ) {
20844 if ( ! empty( $results ) ) {
20845 $context['pluggable_data'] = Voxel::get_post_fields( $results[0]['ID'] );
20846 $user = get_userdata( $results[0]['post_author'] );
20847 if ( $user ) {
20848 $user_data = (array) $user->data;
20849 $context['pluggable_data']['profile_display_name'] = $user_data['display_name'];
20850 $context['pluggable_data']['profile_email'] = $user_data['user_email'];
20851 $context['pluggable_data']['profile_user_id'] = $results[0]['post_author'];
20852 $context['pluggable_data']['profile_id'] = $results[0]['ID'];
20853 }
20854 $context['response_type'] = 'live';
20855 } else {
20856 $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
20857 }
20858 } elseif ( 'profile_new_wall_post' === $term ) {
20859 if ( ! empty( $results ) ) {
20860 $context['pluggable_data']['profile'] = Voxel::get_post_fields( $results[0]['post_id'] );
20861 $user = get_userdata( $results[0]['user_id'] );
20862 if ( $user ) {
20863 $user_data = (array) $user->data;
20864 $context['pluggable_data']['profile_display_name'] = $user_data['display_name'];
20865 $context['pluggable_data']['profile_name'] = $user_data['user_nicename'];
20866 $context['pluggable_data']['profile_email'] = $user_data['user_email'];
20867 $context['pluggable_data']['profile_user_id'] = $results[0]['user_id'];
20868 }
20869 // Get the status details.
20870 $args = [
20871 'post_id' => $results[0]['post_id'],
20872 'order_by' => 'created_at',
20873 'order' => 'desc',
20874 'feed' => $results[0]['feed'],
20875 'limit' => 1,
20876 'moderation' => $results[0]['moderation'],
20877 ];
20878 $statuses = \Voxel\Timeline\Status::query( $args );
20879 $status_details = $statuses['items'][0];
20880 foreach ( (array) $status_details as $key => $value ) {
20881 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
20882 if ( is_object( $value ) ) {
20883 $encoded_value = wp_json_encode( $value );
20884 if ( is_string( $encoded_value ) ) {
20885 $value = json_decode( $encoded_value, true );
20886 }
20887 }
20888 $context['pluggable_data']['wall_post'][ $clean_key ] = $value;
20889 }
20890 $context['response_type'] = 'live';
20891 } else {
20892 $context = json_decode( '{"pluggable_data":{"profile":{"field_step-general":null,"field_ui-image-2":null,"field_voxel:name":"John","field_voxel:avatar":null,"field_cover":null,"field_ui-step":null,"field_ui-image-3":null,"field_location":null,"field_ui-step-2":null,"field_ui-image":null,"field_description":""},"profile_display_name":"John","profile_name":"admin","profile_email":"johnd@gmail.com","profile_user_id":"1","wall_post":{"id":73,"user_id":1,"post_id":586,"published_as":null,"content":"Test content","feed":"post_reviews","moderation":1,"repost_of":null,"quote_of":null,"created_at":"2025-02-02 05:28:00","edited_at":null,"review_score":-2,"like_count":0,"reply_count":0,"details":{"rating":{"score":-2}},"liked_by_user":false,"reposted_by_user":false,"last3_liked":[],"friends_reposted":[],"friends_liked":[]}},"response_type":"sample"}', true );// @phpcs:ignore
20893 }
20894 } elseif ( 'profile_reviewed' === $term ) {
20895 if ( ! empty( $results ) ) {
20896 // Get the review details.
20897 $args = [
20898 'post_id' => $results[0]['post_id'],
20899 'order_by' => 'created_at',
20900 'order' => 'desc',
20901 'feed' => $results[0]['feed'],
20902 'limit' => 1,
20903 'moderation' => $results[0]['moderation'],
20904 ];
20905 $statuses = \Voxel\Timeline\Status::query( $args );
20906 $review_details = $statuses['items'][0];
20907 foreach ( (array) $review_details as $key => $value ) {
20908 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
20909 if ( 'user_can_edit' == $clean_key || 'publisher' == $clean_key || 'user_can_edit' == $clean_key || 'user_can_moderate' == $clean_key ) {
20910 continue;
20911 }
20912 if ( 'files' === $clean_key ) {
20913 $value = wp_json_encode( $value );
20914 } elseif ( 'details' === $clean_key ) {
20915 $review_ratings = isset( $value['rating'] ) && is_array( $value['rating'] ) ? $value['rating'] : [];
20916 $value['rating'] = [];
20917 $type = \Voxel\Post_Type::get( 'profile' );
20918
20919 if ( ! empty( $review_ratings ) ) {
20920 $rating_levels = $type->reviews->get_rating_levels();
20921 $categories = $type->reviews->get_categories();
20922
20923 foreach ( $categories as $category ) {
20924 $category_key = $category['key'];
20925 $category_label = strtolower( $category['label'] );
20926
20927 if ( isset( $review_ratings[ $category_key ] ) && $category_label ) {
20928 foreach ( $rating_levels as $rating_level ) {
20929 if ( $review_ratings[ $category_key ] === $rating_level['score'] ) {
20930 $value['rating'][ $category_label ] = $rating_level['label'];
20931 break;
20932 }
20933 }
20934 }
20935 }
20936 }
20937 } else {
20938 $clean_key = 'review_' . $clean_key;
20939 }
20940 $context['pluggable_data'][ $clean_key ] = $value;
20941 }
20942 $context['response_type'] = 'live';
20943 } else {
20944 $context = json_decode( '{"pluggable_data":{"review_id":73,"review_user_id":1,"review_post_id":586,"review_published_as":null,"review_content":"test content","review_feed":"post_reviews","review_moderation":1,"review_repost_of":null,"review_quote_of":null,"review_created_at":"2025-02-02 05:28:00","review_edited_at":null,"review_review_score":-2,"review_like_count":0,"review_reply_count":0,"details":{"rating":[],"rating":{"rating":"Poor"}},"review_liked_by_user":false,"review_reposted_by_user":false,"review_last3_liked":[],"review_friends_reposted":[],"review_friends_liked":[]},"response_type":"sample"}', true );// @phpcs:ignore
20945 }
20946 }
20947 return $context;
20948 }
20949
20950 /**
20951 * Get Voxel Messages Triggers Last Data
20952 *
20953 * @param array $data data.
20954 *
20955 * @return array|mixed|string
20956 */
20957 public function search_voxel_messages_triggers_last_data( $data ) {
20958 global $wpdb;
20959 $context = [];
20960 $term = $data['search_term'];
20961
20962 if ( 'voxel_new_message_post_created' === $term ) {
20963 $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}voxel_messages ORDER BY id DESC LIMIT %d", 1 ), ARRAY_A );
20964
20965 if ( ! empty( $results ) ) {
20966 $message = $results[0];
20967 $context['pluggable_data']['sender'] = WordPress::get_user_context( $message['sender_id'] );
20968 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $message['receiver_id'] );
20969 $context['pluggable_data']['content'] = $message['content'];
20970 $context['pluggable_data']['message_id'] = $message['id'];
20971 $context['pluggable_data']['created_at'] = $message['created_at'];
20972 $context['response_type'] = 'live';
20973 } else {
20974 $context = json_decode( '{"pluggable_data":{"sender":{"wp_user_id":9,"user_login":"johndoe","display_name":"John Doe","user_firstname":"John","user_lastname":"Doe","user_email":"john@example.com","user_registered":"2025-07-18 15:01:53","user_role":["author"]},"receiver":{"wp_user_id":11,"user_login":"janedoe","display_name":"Jane Smith","user_firstname":"Jane","user_lastname":"Smith","user_email":"jane@example.com","user_registered":"2025-07-21 03:16:45","user_role":["author"]},"content":"Hello, how are you?","message_id":11,"created_at":"2025-08-06 05:27:02"},"response_type":"sample"}', true );
20975 }
20976 }
20977
20978 return $context;
20979 }
20980
20981 /**
20982 * Get Late Point Customer Fields
20983 *
20984 * @param array $data data.
20985 *
20986 * @return array|void
20987 */
20988 public function search_late_point_customer_fields( $data ) {
20989 if ( ! class_exists( 'OsCustomFieldsController' ) ) {
20990
20991 return;
20992 }
20993 global $wpdb;
20994 $booking_fields = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}latepoint_settings WHERE name = %s", 'custom_fields_for_customer' ) );
20995 $fields = [];
20996 if ( ! empty( $booking_fields ) ) {
20997 $fields = json_decode( $booking_fields->value, true );
20998 }
20999 return (array) $fields;
21000 }
21001
21002 /**
21003 * Is multi person booking
21004 *
21005 * @param array $data data.
21006 *
21007 * @return array|void
21008 */
21009 public function search_late_point_multi_person_booking( $data ) {
21010 if ( ! class_exists( 'LatePointAddonGroupBookings' ) ) {
21011
21012 return;
21013 }
21014 $service_id = isset( $data['dynamic'] ) ? $data['dynamic'] : '-1';
21015
21016 if ( -1 == $service_id ) {
21017 return;
21018 }
21019 global $wpdb;
21020 $booking_details = $wpdb->get_row( $wpdb->prepare( "SELECT capacity_max,capacity_min FROM {$wpdb->prefix}latepoint_services WHERE id= %s", intval( $service_id ) ) );
21021 if ( ! empty( $booking_details ) ) {
21022 return (array) $booking_details;
21023 }
21024
21025 }
21026
21027 /**
21028 * Get Mail Mint lists
21029 *
21030 * @param array $data data.
21031 *
21032 * @return array
21033 */
21034 public function search_mail_mint_contact_lists( $data ) {
21035 global $wpdb;
21036
21037 $lists = $wpdb->get_results( $wpdb->prepare( 'SELECT title, id FROM ' . $wpdb->prefix . 'mint_contact_groups WHERE type = %s', 'lists' ) );
21038 $options = [];
21039
21040 if ( ! empty( $lists ) ) {
21041 foreach ( $lists as $list ) {
21042 $options[] = [
21043 'label' => $list->title,
21044 'value' => $list->id,
21045 ];
21046 }
21047 }
21048 return [
21049 'options' => $options,
21050 'hasMore' => false,
21051 ];
21052 }
21053
21054 /**
21055 * Get Mail Mint tags
21056 *
21057 * @param array $data data.
21058 *
21059 * @return array
21060 */
21061 public function search_mail_mint_contact_tags( $data ) {
21062 global $wpdb;
21063
21064 $lists = $wpdb->get_results( $wpdb->prepare( 'SELECT title, id FROM ' . $wpdb->prefix . 'mint_contact_groups WHERE type = %s ', 'tags' ) );
21065 $options = [];
21066
21067 if ( ! empty( $lists ) ) {
21068 foreach ( $lists as $list ) {
21069 $options[] = [
21070 'label' => $list->title,
21071 'value' => $list->id,
21072 ];
21073 }
21074 }
21075 return [
21076 'options' => $options,
21077 'hasMore' => false,
21078 ];
21079 }
21080
21081 /** Get Better Messages trigger Last Data
21082 *
21083 * @param array $data data.
21084 *
21085 * @return array|mixed|string
21086 */
21087 public function search_better_messages_triggers_data( $data ) {
21088 $context = [];
21089 global $wpdb;
21090 if ( ! function_exists( 'Better_Messages' ) ) {
21091 return [];
21092 }
21093 $sql = "SELECT * FROM {$wpdb->prefix}bm_message_messages ORDER BY id DESC LIMIT 1";
21094 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
21095
21096 if ( ! empty( $results ) ) {
21097 $message = Better_Messages()->functions->get_message( $results[0]['id'] );
21098 if ( is_object( $message ) ) {
21099 $message = get_object_vars( $message );
21100 }
21101 $context['pluggable_data'] = $message;
21102 $context['pluggable_data']['sender'] = WordPress::get_user_context( $results[0]['sender_id'] );
21103 $context['response_type'] = 'live';
21104 } else {
21105 $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
21106 }
21107 return $context;
21108 }
21109
21110 /**
21111 * Get Appointment Hour Booking trigger Last Data
21112 *
21113 * @param array $data data.
21114 *
21115 * @return array|mixed|string
21116 */
21117 public function search_ahb_appointment_booked_triggers_last_data( $data ) {
21118 $context = [];
21119 global $wpdb;
21120 $sql = "SELECT * FROM {$wpdb->prefix}cpappbk_messages ORDER BY id DESC LIMIT 1";
21121 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
21122
21123 if ( ! empty( $results ) ) {
21124 $context['pluggable_data'] = unserialize( $results[0]['posted_data'] );
21125 $context['response_type'] = 'live';
21126 } else {
21127 $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
21128 }
21129 return $context;
21130 }
21131
21132 /**
21133 * Prepare mailmint contact status.
21134 *
21135 * @param array $data Search Params.
21136 *
21137 * @return array
21138 */
21139 public function search_mail_mint_fetch_custom_fields( $data ) {
21140
21141 $options = [
21142 [
21143 'label' => 'Yes',
21144 'value' => 'true',
21145 ],
21146 [
21147 'label' => 'No',
21148 'value' => 'false',
21149 ],
21150 ];
21151
21152 return [
21153 'options' => $options,
21154 'hasMore' => false,
21155 ];
21156 }
21157
21158 /**
21159 * Get Mail Mint custom fields
21160 *
21161 * @param array $data data.
21162 *
21163 * @return array
21164 */
21165 public function search_mail_mint_custom_fields( $data ) {
21166 global $wpdb;
21167 $fields = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'mint_custom_fields' );
21168
21169 return (array) $fields;
21170
21171 }
21172
21173 /**
21174 * Get Mail Mint lists
21175 *
21176 * @param array $data data.
21177 *
21178 * @return array|void
21179 */
21180 public function search_mail_mint_contacts( $data ) {
21181 if ( ! class_exists( 'Mint\MRM\DataBase\Models\ContactModel' ) ) {
21182 return [];
21183 }
21184 $contacts = ContactModel::get_all();
21185 $options = [];
21186 if ( ! empty( $contacts ) ) {
21187 foreach ( $contacts['data'] as $contact ) {
21188 $options[] = [
21189 'label' => $contact['email'],
21190 'value' => $contact['id'],
21191 ];
21192 }
21193 }
21194 return [
21195 'options' => $options,
21196 'hasMore' => false,
21197 ];
21198 }
21199
21200 /**
21201 * Get mail mint Last Data
21202 *
21203 * @param array $data data.
21204 *
21205 * @return array
21206 */
21207 public function search_mail_mint_last_data( $data ) {
21208 if ( ! class_exists( 'Mint\MRM\DataBase\Models\ContactModel' ) || ! class_exists( 'Mint\MRM\DataBase\Models\ContactGroupModel' ) ) {
21209 return [];
21210 }
21211 $context = [];
21212 global $wpdb;
21213 $contact_id = isset( $data['filter']['contact_id']['value'] ) ? $data['filter']['contact_id']['value'] : -1;
21214 $term = $data['search_term'] ? $data['search_term'] : '';
21215 $type = 'lists';
21216 if ( 'mail_mint_tags_added_to_contact' === $term ) {
21217 $type = 'tags';
21218 }
21219 $data = [];
21220 if ( -1 == $contact_id ) {
21221 $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 ) );
21222 } else {
21223 $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 ) );
21224 }
21225
21226 if ( ! empty( $result ) ) {
21227 $contact_id = $result->contact_id;
21228 $data['contact'] = ContactModel::get( $contact_id );
21229 $data[ $type ] = ContactGroupModel::get( $result->id );
21230 }
21231 if ( ! empty( $data ) ) {
21232 $context['pluggable_data'] = $data;
21233 $context['response_type'] = 'live';
21234 } else {
21235 $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 );
21236 }
21237 return (array) $context;
21238 }
21239
21240
21241 /**
21242 * Get Simple Schedule Appointment Types
21243 *
21244 * @param array $data data.
21245 *
21246 * @return array
21247 */
21248 public function search_simple_schedule_appointment_types( $data ) {
21249 global $wpdb;
21250
21251 $appointment_types = $wpdb->get_results( 'SELECT title, id FROM ' . $wpdb->prefix . 'ssa_appointment_types' );
21252 $options = [];
21253
21254 if ( ! empty( $appointment_types ) ) {
21255 foreach ( $appointment_types as $appointment_type ) {
21256 $options[] = [
21257 'label' => $appointment_type->title,
21258 'value' => $appointment_type->id,
21259 ];
21260 }
21261 }
21262 return [
21263 'options' => $options,
21264 'hasMore' => false,
21265 ];
21266 }
21267
21268 /**
21269 * Get mail mint Last Data
21270 *
21271 * @param array $data data.
21272 *
21273 * @return array
21274 */
21275 public function search_ssa_last_data( $data ) {
21276 $context = [];
21277 global $wpdb;
21278 $appointment_type_id = isset( $data['filter']['appointment_type_id']['value'] ) ? $data['filter']['appointment_type_id']['value'] : -1;
21279 $term = $data['search_term'] ? $data['search_term'] : '';
21280 $status = 'booked';
21281 if ( 'ssa_appointment_cancelled' === $term ) {
21282 $status = 'canceled';
21283 }
21284 $data = [];
21285 if ( -1 == $appointment_type_id ) {
21286 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointments WHERE status = %s ORDER BY id DESC Limit 1", $status ), ARRAY_A );
21287 } else {
21288 $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 );
21289 }
21290
21291
21292 if ( ! empty( $result ) ) {
21293 $result_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointment_meta where appointment_id=%d", $result['id'] ) );
21294 if ( ! empty( $result_meta ) ) {
21295 foreach ( $result_meta as $meta ) {
21296 $result[ $meta->meta_key ] = $meta->meta_value;
21297 }
21298 }
21299 }
21300 if ( ! empty( $result ) ) {
21301
21302 $result['customer_information'] = json_decode( $result['customer_information'], true );
21303 $context['pluggable_data'] = $result;
21304 $context['response_type'] = 'live';
21305 } else {
21306 $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 );
21307 }
21308 return (array) $context;
21309 }
21310
21311 /**
21312 * Get Simple Schedule Appointment Types
21313 *
21314 * @param array $data data.
21315 *
21316 * @return array
21317 */
21318 public function search_simple_schedule_appointments( $data ) {
21319 global $wpdb;
21320
21321 $appointment_types = $wpdb->get_results( 'SELECT title, id FROM ' . $wpdb->prefix . 'ssa_appointment_types' );
21322 $options = [];
21323
21324 if ( ! empty( $appointment_types ) ) {
21325 foreach ( $appointment_types as $appointment_type ) {
21326 $options[] = [
21327 'label' => $appointment_type->title,
21328 'value' => $appointment_type->id,
21329 ];
21330 }
21331 }
21332 return [
21333 'options' => $options,
21334 'hasMore' => false,
21335 ];
21336 }
21337
21338 /**
21339 * Get Sensei LMS Lessons
21340 *
21341 * @param array $data data.
21342 *
21343 * @return array
21344 */
21345 public function search_sensei_lms_lessons( $data ) {
21346 if ( ! function_exists( 'Sensei' ) ) {
21347 return [];
21348 }
21349 $course_id = $data['dynamic'];
21350 $lessons = \Sensei()->course->course_lessons( $course_id, 'publish', 'ids' );
21351 $options = [];
21352
21353 if ( ! empty( $lessons ) ) {
21354 foreach ( $lessons as $lesson ) {
21355 $options[] = [
21356 'label' => get_the_title( $lesson ),
21357 'value' => $lesson,
21358 ];
21359 }
21360 }
21361 return [
21362 'options' => $options,
21363 'hasMore' => false,
21364 ];
21365 }
21366
21367 /**
21368 * Get Sensei LMS Quiz List
21369 *
21370 * @param array $data data.
21371 *
21372 * @return array
21373 */
21374 public function search_sensei_lms_quiz( $data ) {
21375 $page = $data['page'];
21376 $limit = Utilities::get_search_page_limit();
21377 $offset = $limit * ( $page - 1 );
21378
21379 $quizes = get_posts(
21380 [
21381 'post_type' => 'lesson',
21382 'orderby' => 'title',
21383 'order' => 'ASC',
21384 'post_status' => 'publish',
21385 'posts_per_page' => $limit,
21386 'offset' => $offset,
21387 'fields' => 'ids',
21388 'meta_query' => [
21389 'relation' => 'AND',
21390 [
21391 'key' => '_lesson_quiz',
21392 'compare' => 'EXISTS',
21393 ],
21394 [
21395 'key' => '_quiz_has_questions',
21396 'value' => 1,
21397 'compare' => '=',
21398 ],
21399 ],
21400 ]
21401 );
21402 $options = [];
21403 $total_quizes = get_posts(
21404 [
21405 'post_type' => 'lesson',
21406 'orderby' => 'title',
21407 'order' => 'ASC',
21408 'post_status' => 'publish',
21409 'posts_per_page' => -1,
21410 'fields' => 'ids',
21411 'meta_query' => [
21412 'relation' => 'AND',
21413 [
21414 'key' => '_lesson_quiz',
21415 'compare' => 'EXISTS',
21416 ],
21417 [
21418 'key' => '_quiz_has_questions',
21419 'value' => 1,
21420 'compare' => '=',
21421 ],
21422 ],
21423 ]
21424 );
21425 $count = count( $total_quizes );
21426 if ( ! empty( $quizes ) ) {
21427 foreach ( $quizes as $quiz ) {
21428 $options[] = [
21429 'label' => get_the_title( $quiz ),
21430 'value' => $quiz,
21431 ];
21432 }
21433 }
21434 return [
21435 'options' => $options,
21436 'hasMore' => $count > $limit && $count > $offset,
21437 ];
21438 }
21439
21440 /**
21441 * Get Sensei LMS trigger Last Data
21442 *
21443 * @param array $data data.
21444 *
21445 * @return array|mixed|string
21446 */
21447 public function search_sensei_lms_triggers_last_data( $data ) {
21448 $context = [];
21449 global $wpdb;
21450 $term = $data['search_term'];
21451
21452 if ( ! function_exists( 'Sensei' ) ) {
21453 return;
21454 }
21455
21456 if ( 'course_completed' == $term ) {
21457 $comment_type = 'sensei_course_status';
21458 $course_id = $data['filter']['sensei_course']['value'];
21459 if ( -1 === $course_id ) {
21460 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s ORDER BY comment_ID DESC LIMIT 1";
21461 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type ), ARRAY_A );// @phpcs:ignore
21462 } else {
21463 $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";
21464 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, $course_id ), ARRAY_A );// @phpcs:ignore
21465 }
21466 } elseif ( 'course_enrolled' == $term ) {
21467 $comment_type = 'sensei_course_status';
21468 $course_id = $data['filter']['sensei_course']['value'];
21469 if ( -1 === $course_id ) {
21470 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s ORDER BY comment_ID DESC LIMIT 1";
21471 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'in-progress', $comment_type ), ARRAY_A );// @phpcs:ignore
21472 } else {
21473 $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";
21474 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'in-progress', $comment_type, $course_id ), ARRAY_A );// @phpcs:ignore
21475 }
21476 } elseif ( 'lesson_completed' == $term ) {
21477 $lesson_id = $data['filter']['sensei_lesson']['value'];
21478 $course_id = $data['filter']['sensei_course']['value'];
21479 $comment_type = 'sensei_lesson_status';
21480 if ( -1 === $lesson_id ) {
21481 $sql = "SELECT comments.*, meta.* FROM {$wpdb->prefix}comments as comments
21482 LEFT JOIN {$wpdb->prefix}postmeta as meta
21483 ON comments.comment_post_ID = meta.post_id
21484 WHERE comments.comment_approved = %s
21485 AND comments.comment_type = %s
21486 AND meta.meta_key = %s AND
21487 meta.meta_value = %d
21488 ORDER BY comments.comment_ID DESC LIMIT 1";
21489 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, '_lesson_course', $course_id ), ARRAY_A );// @phpcs:ignore
21490 } else {
21491 $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";
21492 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, $lesson_id ), ARRAY_A );// @phpcs:ignore
21493 }
21494 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term ) {
21495 $quiz_id = $data['filter']['sensei_quiz']['value'];
21496 $comment_type = 'sensei_lesson_status';
21497 if ( 'quiz_passes' == $term ) {
21498 $status = "( comment_approved = 'passed' )";
21499 } elseif ( 'quiz_fails' == $term ) {
21500 $status = "( comment_approved = 'failed' )";
21501 } else {
21502 $status = "( comment_approved = 'failed' OR comment_approved = 'passed' )";
21503 }
21504 if ( -1 === $quiz_id ) {
21505 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type = %s AND $status ORDER BY comment_ID DESC LIMIT 1";
21506 $results = $wpdb->get_results( $wpdb->prepare( $sql, $comment_type ), ARRAY_A );// @phpcs:ignore
21507 } else {
21508 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type = %s AND $status AND comment_post_ID = %d ORDER BY comment_ID DESC LIMIT 1";
21509 $results = $wpdb->get_results( $wpdb->prepare( $sql, $comment_type, $quiz_id ), ARRAY_A );// @phpcs:ignore
21510 }
21511 } elseif ( 'quiz_completion' == $term ) {
21512 $quiz_id = $data['filter']['sensei_quiz']['value'];
21513 $comment_type = 'sensei_lesson_status';
21514 $condition_compare = $data['filter']['condition_compare']['value'];
21515 $percentage = $data['filter']['percentage']['value'];
21516 if ( -1 === $quiz_id ) {
21517 $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
21518 } else {
21519 $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
21520 }
21521 }
21522
21523 if ( ! empty( $results ) ) {
21524 $post = get_post( $results[0]['comment_post_ID'] );
21525 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['user_id'] );
21526 if ( 'course_completed' == $term || 'course_enrolled' == $term ) {
21527 $context['pluggable_data']['sensei_course'] = $results[0]['comment_post_ID'];
21528 if ( $post instanceof \WP_Post ) {
21529 $context['pluggable_data']['course_title'] = $post->post_title;
21530 }
21531 } elseif ( 'lesson_completed' == $term ) {
21532 $context['pluggable_data']['sensei_lesson'] = $results[0]['comment_post_ID'];
21533 if ( $post instanceof \WP_Post ) {
21534 $context['pluggable_data']['lesson_title'] = $post->post_title;
21535 }
21536 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term || 'quiz_completion' == $term ) {
21537 $lesson_quiz = get_post_meta( $results[0]['comment_post_ID'], '_lesson_quiz', true );
21538 $submission = \Sensei()->quiz_submission_repository->get( $lesson_quiz, $results[0]['user_id'] );
21539 if ( $post instanceof \WP_Post ) {
21540 $context['pluggable_data']['quiz_title'] = $post->post_title;
21541 }
21542 $context['pluggable_data']['quiz_status'] = $results[0]['comment_approved'];
21543 $context['pluggable_data']['quiz_data']['id'] = $submission->get_id();
21544 $context['sensei_quiz'] = $submission->get_id();
21545 $context['pluggable_data']['quiz_data']['final_grade'] = $submission->get_final_grade();
21546 $context['pluggable_data']['quiz_data']['created_at'] = $submission->get_created_at();
21547 }
21548 $context['response_type'] = 'live';
21549 } else {
21550 if ( 'course_completed' == $term || 'course_enrolled' == $term ) {
21551 $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
21552 } elseif ( 'lesson_completed' == $term ) {
21553 $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
21554 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term || 'quiz_completion' == $term ) {
21555 $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
21556 }
21557 }
21558 return $context;
21559 }
21560
21561 /**
21562 * Get SurelyWP Services - SureCart Addons Last Data
21563 *
21564 * @param array $data data.
21565 *
21566 * @return array
21567 */
21568 public function search_services_sc_triggers_last_data( $data ) {
21569 $context = [];
21570 global $wpdb;
21571 $term = $data['search_term'] ? $data['search_term'] : '';
21572 $data = [];
21573
21574 if ( 'new_service_created' === $term ) {
21575 $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 );
21576 } elseif ( 'requirement_submitted' === $term ) {
21577 $result = $wpdb->get_row( "SELECT created_at FROM {$wpdb->prefix}surelywp_sv_requirements ORDER BY created_at DESC Limit 1", ARRAY_A );
21578 } elseif ( 'message_sent' === $term ) {
21579 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_messages ORDER BY message_id DESC Limit 1", ARRAY_A );
21580 } elseif ( 'message_final_delivery_sent' === $term ) {
21581 $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 );
21582 } elseif ( 'customer_request_revision' === $term ) {
21583 $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 );
21584 } elseif ( 'customer_approves_final_delivery' === $term ) {
21585 $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 );
21586 } elseif ( 'delivery_date_changed' === $term ) {
21587 $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 );
21588 } elseif ( 'service_cancel' === $term || 'service_marked_canceled' === $term ) {
21589 $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 );
21590 } elseif ( 'service_completed' === $term || 'service_mark_completed' === $term ) {
21591 $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 );
21592 } elseif ( 'contract_signed' === $term ) {
21593 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_contracts ORDER BY contract_id DESC Limit 1", ARRAY_A );
21594 }
21595 if ( ! empty( $result ) ) {
21596 if ( 'new_service_created' === $term ) {
21597 $service_data = [
21598 'service_setting_id' => $result['service_id'],
21599 'order_id' => $result['order_id'],
21600 'product_id' => $result['product_id'],
21601 'service_status' => $result['service_status'],
21602 'delivery_date' => $result['delivery_date'],
21603 ];
21604 $context['pluggable_data'] = array_merge( $service_data, WordPress::get_user_context( $result['user_id'] ) );
21605 $context['response_type'] = 'live';
21606 } elseif ( 'requirement_submitted' === $term ) {
21607 $requirements_data = $wpdb->get_results(
21608 $wpdb->prepare(
21609 "SELECT * FROM {$wpdb->prefix}surelywp_sv_requirements WHERE created_at = %s",
21610 $result['created_at']
21611 ),
21612 ARRAY_A
21613 );
21614 $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 );
21615 $user_data = WordPress::get_user_context( $service_result['user_id'] );
21616 unset( $service_result['user_id'] );
21617 $context['pluggable_data'] = array_merge( $requirements_data, $service_result, $user_data );
21618 foreach ( $requirements_data as $value ) {
21619 if ( 'file' == $value['requirement_type'] ) {
21620 $upload_dir = wp_upload_dir();
21621 $attachment_file_names = json_decode( $value['requirement'], true );
21622 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
21623 $context['pluggable_data']['requirement_attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $value['service_id'] . '/requirement/' . $attachment_file_name;
21624 }
21625 }
21626 }
21627 $context['response_type'] = 'live';
21628 } elseif ( 'message_sent' === $term || 'message_final_delivery_sent' === $term ) {
21629 $message_data = [
21630 'sender' => WordPress::get_user_context( $result['sender_id'] ),
21631 'receiver' => WordPress::get_user_context( $result['receiver_id'] ),
21632 'service_id' => $result['service_id'],
21633 'message_text' => $result['message_text'],
21634 'attachment_file_name' => $result['attachment_file_name'],
21635 'is_final_delivery' => $result['is_final_delivery'],
21636 ];
21637 $context['pluggable_data'] = $message_data;
21638 $upload_dir = wp_upload_dir();
21639 $attachment_file_names = json_decode( $result['attachment_file_name'], true );
21640 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
21641 $context['pluggable_data']['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $result['service_id'] . '/messages/' . $attachment_file_name;
21642 }
21643 $context['response_type'] = 'live';
21644 } elseif ( 'customer_request_revision' === $term ) {
21645 $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 );
21646 if ( ! empty( $message_result ) ) {
21647 global $surelywp_sv_model;
21648 $revision_message = $surelywp_sv_model->surelywp_sv_get_customer_revision_msg( $message_result['service_id'], $message_result['message_id'] );
21649 $context['pluggable_data'] = array_merge( $result, $revision_message );
21650 $context['pluggable_data']['sender'] = WordPress::get_user_context( $revision_message['sender_id'] );
21651 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $revision_message['receiver_id'] );
21652 $upload_dir = wp_upload_dir();
21653 $attachment_file_names = json_decode( $revision_message['attachment_file_name'], true );
21654 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
21655 $context['pluggable_data']['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $revision_message['service_id'] . '/messages/' . $attachment_file_name;
21656 }
21657 $context['response_type'] = 'live';
21658 } else {
21659 $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 );
21660 }
21661 } elseif ( 'customer_approves_final_delivery' === $term || 'delivery_date_changed' === $term || 'service_cancel' === $term || 'service_marked_canceled' === $term || 'service_completed' === $term || 'service_mark_completed' === $term ) {
21662 $user_data = WordPress::get_user_context( $result['user_id'] );
21663 unset( $result['user_id'] );
21664 $context['pluggable_data'] = array_merge( $result, $user_data );
21665 $context['response_type'] = 'live';
21666 } elseif ( 'contract_signed' === $term ) {
21667 $contract_data = [
21668 'service_id' => $result['service_id'],
21669 'signature' => $result['signature'],
21670 'contract_details' => $result['contract_details'],
21671 ];
21672 $service_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_id = %d", $result['service_id'] ), ARRAY_A );
21673 $user_data = WordPress::get_user_context( $service_result['user_id'] );
21674 unset( $service_result['user_id'] );
21675 $context['pluggable_data'] = array_merge( $contract_data, $user_data, $service_result );
21676 $context['response_type'] = 'live';
21677 }
21678 } else {
21679 if ( 'new_service_created' === $term || 'customer_request_revision' === $term || 'customer_approves_final_delivery' === $term || 'delivery_date_changed' === $term ) {
21680 $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 );
21681 } elseif ( 'requirement_submitted' === $term ) {
21682 $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 );
21683 } elseif ( 'message_sent' === $term || 'message_final_delivery_sent' === $term ) {
21684 $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 );
21685 } elseif ( 'service_cancel' === $term ) {
21686 $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 );
21687 } elseif ( 'service_marked_canceled' === $term || 'service_completed' === $term || 'service_mark_completed' === $term ) {
21688 $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 );
21689 } elseif ( 'contract_signed' === $term ) {
21690 $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 );
21691 }
21692 }
21693 return (array) $context;
21694 }
21695
21696 /**
21697 * Get SurelyWP Support Portal - SureCart Addons Last Data
21698 *
21699 * @param array $data data.
21700 *
21701 * @return array
21702 */
21703 public function search_sc_support_portal_triggers_last_data( $data ) {
21704 $context = [];
21705 if ( ! class_exists( 'Surelywp_Support_Portal' ) ) {
21706 return [];
21707 }
21708 global $wpdb;
21709 $term = $data['search_term'] ? $data['search_term'] : '';
21710 $data = [];
21711
21712 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
21713 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sp_support WHERE support_status = 1 ORDER BY support_id DESC Limit 1", ARRAY_A );
21714 } elseif ( 'new_message_sent' === $term ) {
21715 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sp_messages ORDER BY message_id DESC Limit 1", ARRAY_A );
21716 } elseif ( 'tickets_closed' === $term ) {
21717 $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 );
21718 } elseif ( 'tickets_opens' === $term ) {
21719 $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 );
21720 } elseif ( 'new_email_message_fetched' === $term ) {
21721 $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 );
21722 }
21723 if ( ! empty( $result ) ) {
21724 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
21725 $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 );
21726 $support_data = [
21727 'support_id' => $result['support_id'],
21728 'order_id' => $result['order_id'],
21729 'product_id' => $result['product_id'],
21730 'support_title' => $result['support_title'],
21731 'support_status' => Surelywp_Support_Portal::surelywp_sp_get_support_status( $result['support_status'] ),
21732 'support_data' => $support_res,
21733 ];
21734 $context['pluggable_data'] = array_merge( $support_data, WordPress::get_user_context( $result['user_id'] ) );
21735 $context['response_type'] = 'live';
21736 } elseif ( 'tickets_closed' === $term || 'tickets_opens' === $term ) {
21737 $support_data = [];
21738 $support_ids = $result;
21739 foreach ( $support_ids as $key => $id ) {
21740 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sp_support WHERE support_id = %d", $id ), ARRAY_A );
21741 $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 );
21742 $support_data[ $key ] = WordPress::get_user_context( $result['user_id'] );
21743 $support_data[ $key ] = [
21744 'support_id' => $result['support_id'],
21745 'order_id' => $result['order_id'],
21746 'product_id' => $result['product_id'],
21747 'support_title' => $result['support_title'],
21748 'support_status' => Surelywp_Support_Portal::surelywp_sp_get_support_status( $result['support_status'] ),
21749 'support_data' => $support_res,
21750 ];
21751 }
21752 $context['pluggable_data'] = $support_data;
21753 $context['response_type'] = 'live';
21754 } elseif ( 'new_message_sent' === $term ) {
21755 $upload_dir = wp_upload_dir();
21756 $support_data = [
21757 'sender_id' => $result['sender_id'],
21758 'receiver_id' => $result['receiver_id'],
21759 'support_id' => $result['support_id'],
21760 'message_text' => $result['message_text'],
21761 'sender' => WordPress::get_user_context( $result['sender_id'] ),
21762 'receiver' => WordPress::get_user_context( $result['receiver_id'] ),
21763 ];
21764 if ( ! empty( $result['attachment_file_name'] ) ) {
21765 $attachment_file_names = json_decode( $result['attachment_file_name'], true );
21766 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
21767 $support_data['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-support-portal-data/' . $result['support_id'] . '/messages/' . $attachment_file_name;
21768 }
21769 }
21770 $context['pluggable_data'] = $support_data;
21771 $context['response_type'] = 'live';
21772 }
21773 } else {
21774 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
21775 $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 );
21776 } elseif ( 'tickets_closed' === $term || 'tickets_opens' === $term ) {
21777 $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 );
21778 } elseif ( 'new_message_sent' === $term ) {
21779 $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 );
21780 }
21781 }
21782 return (array) $context;
21783 }
21784
21785 /**
21786 * Get Fluent Boards Last Data
21787 *
21788 * @param array $data data.
21789 *
21790 * @return array
21791 */
21792 public function search_fbs_triggers_last_data( $data ) {
21793 if ( ! class_exists( '\FluentBoards\App\Models\Stage' ) ) {
21794 return [];
21795 }
21796 global $wpdb;
21797
21798 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
21799 if ( ! class_exists( 'FluentBoards\App\Models\Board' ) || ! class_exists( 'FluentBoards\App\Models\User' ) ) {
21800 return [];
21801 }
21802
21803 $context = [];
21804 $result = null;
21805
21806 switch ( $term ) {
21807 case 'board_created':
21808 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_boards ORDER BY id DESC LIMIT 1", ARRAY_A );
21809 break;
21810 case 'board_member_added':
21811 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_relations WHERE object_type = 'board_user' ORDER BY id DESC LIMIT 1", ARRAY_A );
21812 break;
21813 case 'task_created':
21814 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_tasks ORDER BY id DESC LIMIT 1", ARRAY_A );
21815 break;
21816 case 'stage_changed':
21817 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_tasks ORDER BY id DESC LIMIT 1", ARRAY_A );
21818 break;
21819
21820 }
21821
21822 if ( ! empty( $result ) ) {
21823 switch ( $term ) {
21824 case 'board_created':
21825 $context['pluggable_data'] = \FluentBoards\App\Models\Board::findOrFail( $result['id'] );
21826 $context['response_type'] = 'live';
21827 break;
21828 case 'board_member_added':
21829 $context['pluggable_data']['board_id'] = $result['object_id'];
21830 $context['pluggable_data']['board_member'] = \FluentBoards\App\Models\User::find( $result['foreign_id'] );
21831 $context['response_type'] = 'live';
21832 break;
21833 case 'task_created':
21834 $stage_data = Stage::where( 'board_id', $result['board_id'] )->whereNull( 'archived_at' )->first();
21835
21836 $context['pluggable_data'] = [
21837 'id' => $result['id'],
21838 'slug' => $result['slug'],
21839 'title' => $result['title'],
21840 'description' => $result['description'],
21841 'type' => $result['type'],
21842 'board_id' => $result['board_id'],
21843 'stage_id' => $result['stage_id'],
21844 'position' => $result['position'],
21845 'priority' => $result['priority'],
21846 'created_at' => $result['created_at'],
21847 'created_by' => $result['created_by'],
21848 'updated_at' => $result['updated_at'],
21849 'settings' => unserialize( $result['settings'] ),
21850 'stage' => $stage_data ? [
21851 'id' => $stage_data->id,
21852 'slug' => $stage_data->slug,
21853 'title' => $stage_data->title,
21854 'type' => $stage_data->type,
21855 'board_id' => $stage_data->board_id,
21856 'position' => $stage_data->position,
21857 'settings' => $stage_data->settings,
21858 'created_at' => '',
21859 'updated_at' => '',
21860 ] : null,
21861 ];
21862 $context['response_type'] = 'live';
21863 break;
21864 case 'stage_changed':
21865 $stage_data = Stage::where( 'board_id', $result['board_id'] )->whereNull( 'archived_at' )->first();
21866 $user_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}users ORDER BY id DESC LIMIT 1", ARRAY_A );
21867
21868 $context['pluggable_data'] = [
21869 'task' => [
21870 'id' => $result['id'],
21871 'slug' => $result['slug'],
21872 'title' => $result['title'],
21873 'description' => $result['description'],
21874 'type' => $result['type'],
21875 'board_id' => $result['board_id'],
21876 'stage_id' => $stage_data->id,
21877 'position' => $result['position'],
21878 'priority' => $result['priority'],
21879 'created_at' => $result['created_at'],
21880 'created_by' => $result['created_by'],
21881 'updated_at' => $result['updated_at'],
21882 'settings' => unserialize( $result['settings'] ),
21883 'stage' => [
21884 'id' => $stage_data->id,
21885 'slug' => $stage_data->slug,
21886 'title' => $stage_data->title,
21887 'type' => $stage_data->type,
21888 'board_id' => $stage_data->board_id,
21889 'position' => $stage_data->position,
21890 'settings' => $stage_data->settings,
21891 'created_at' => '',
21892 'updated_at' => '',
21893 ],
21894 'watchers' => [
21895 [
21896 'ID' => $user_result['ID'],
21897 'photo' => '',
21898 'user_url' => $user_result['user_url'],
21899 'user_email' => $user_result['user_email'],
21900 'user_login' => $user_result['user_login'],
21901 'user_status' => $user_result['user_status'],
21902 'display_name' => $user_result['display_name'],
21903 'user_nicename' => $user_result['user_nicename'],
21904 'user_registered' => $user_result['user_registered'],
21905 'pivot' => [
21906 'object_id' => $user_result['id'],
21907 'created_at' => $user_result['created_at'],
21908 'foreign_id' => $user_result['ID'],
21909 'user_id' => $user_result['ID'],
21910 ],
21911 ],
21912 ],
21913 ],
21914 'old_stage_id' => $stage_data->id,
21915 ];
21916 $context['response_type'] = 'live';
21917 break;
21918 }
21919 } else {
21920 switch ( $term ) {
21921 case 'board_created':
21922 $context = json_decode( '{"pluggable_data":{"id":2,"title":"testing","description":"This is a sample board.","type":"to-do","currency":"USD","background":{"color":"#d1d8e0","id":"solid_10"},"created_by":"1","isUserOnlyViewer":0},"response_type":"sample"}', true );
21923 break;
21924 case 'board_member_added':
21925 $context = json_decode( '{"pluggable_data":{"board_id":"2","board_member":{"ID":1,"user_login":"johnd","display_name":"johnd"}},"response_type":"sample"}', true );
21926 break;
21927 case 'task_created':
21928 $context = json_decode( '{"pluggable_data":{"id":"1001","slug":"sample-task","title":"Sample Task","description":"This is a sample task.","type":"task","board_id":"10","stage_id":"3","position":"1","priority":"medium","created_at":"2024-03-20 12:00:00","created_by":"1","updated_at":"2024-03-20 12:30:00","settings":[],"stage":{"id":"3","slug":"sample-stage","title":"Sample Stage","type":"default","board_id":"10","position":"1","settings":[],"created_at":"2024-03-19 10:00:00","updated_at":"2024-03-20 11:00:00"}},"response_type":"sample"}', true );
21929 break;
21930 case 'stage_changed':
21931 $context = json_decode( '{"pluggable_data":{"id":"1002","slug":"changed-task","title":"Changed Task","description":"This task changed stages.","type":"task","board_id":"10","stage_id":"4","position":"2","priority":"high","created_at":"2024-04-01 10:00:00","created_by":"1","updated_at":"2024-04-01 12:00:00","settings":[],"stage":{"id":"4","slug":"new-stage","title":"New Stage","type":"default","board_id":"10","position":"2","settings":[],"created_at":"","updated_at":""},"old_stage_id":"3","watchers":[{"ID":71,"photo":"https://www.gravatar.com/avatar/e361de3380a6b977abf350619468ce4f?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FJohn+Doe/128","user_url":"","user_email":"john@gmail.com","user_login":"john","user_status":0,"display_name":"John Doe","user_nicename":"john","user_registered":"2025-05-28 12:07:48","pivot":{"object_id":1002,"created_at":"2025-06-05T10:11:31+00:00","foreign_id":71,"user_id":71}}]},"response_type":"sample"}', true );
21932 break;
21933
21934 }
21935 }
21936
21937 return (array) $context;
21938 }
21939
21940
21941 /**
21942 * Prepare FluentBoards Boards List.
21943 *
21944 * @param array $data Search Params.
21945 * @return array
21946 */
21947 public function search_fbs_boards_list( $data ) {
21948 global $wpdb;
21949
21950 $boards = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fbs_boards", ARRAY_A );
21951
21952 $options = [];
21953
21954 if ( ! empty( $boards ) ) {
21955 foreach ( $boards as $board ) {
21956 $options[] = [
21957 'label' => $board['title'],
21958 'value' => $board['id'],
21959 ];
21960 }
21961 }
21962
21963 return [
21964 'options' => $options,
21965 'hasMore' => false,
21966 ];
21967 }
21968
21969 /**
21970 * Prepare FluentBoards Subtask Groups List.
21971 *
21972 * @param array $data Search Params.
21973 * @return array
21974 */
21975 public function search_fbs_subtask_groups_list( $data ) {
21976 global $wpdb;
21977
21978 $task_id = isset( $data['dynamic'] ) ? sanitize_text_field( $data['dynamic'] ) : '';
21979
21980 $groups = $wpdb->get_results(
21981 $wpdb->prepare(
21982 "SELECT id, value FROM {$wpdb->prefix}fbs_task_metas WHERE task_id = %d AND `key` = %s",
21983 $task_id,
21984 'group_name'
21985 )
21986 );
21987
21988 $options = [];
21989
21990 foreach ( $groups as $group ) {
21991 $options[] = [
21992 'label' => $group->value,
21993 'value' => $group->id,
21994 ];
21995 }
21996
21997 return [
21998 'options' => $options,
21999 'hasMore' => false,
22000 ];
22001 }
22002
22003 /**
22004 * Prepare FluentBoards Stages List.
22005 *
22006 * @param array $data Search Params.
22007 * @return array
22008 */
22009 public function search_fbs_stages_list( $data ) {
22010 if ( ! class_exists( '\FluentBoards\App\Models\Stage' ) ) {
22011 return [];
22012 }
22013 $query = Stage::where( 'board_id', $data['dynamic'] )->whereNull( 'archived_at' );
22014 $stages = $query->get();
22015 $options = [];
22016
22017 if ( ! empty( $stages ) ) {
22018 foreach ( $stages as $stage ) {
22019 $options[] = [
22020 'label' => $stage['title'],
22021 'value' => $stage['id'],
22022 ];
22023 }
22024 }
22025
22026 return [
22027 'options' => $options,
22028 'hasMore' => false,
22029 ];
22030 }
22031
22032 /**
22033 * Get Profile Grid Last Data
22034 *
22035 * @param array $data data.
22036 *
22037 * @return array
22038 */
22039 public function search_profile_grid_triggers_last_data( $data ) {
22040 $context = [];
22041 global $wpdb;
22042 $term = $data['search_term'] ? $data['search_term'] : '';
22043
22044 if ( 'user_added_group' === $term || 'user_removed_from_group' === $term || 'user_assigned_group_manager' === $term || 'user_unassigned_group_manager' === $term || 'membership_request_approved' === $term ) {
22045 $users = get_users(
22046 [
22047 'orderby' => 'meta_value',
22048 'meta_key' => 'pm_group',
22049 'order' => 'DESC',
22050 'number' => 1,
22051 'meta_query' => [
22052 [
22053 'key' => 'pm_group',
22054 'compare' => 'EXISTS',
22055 ],
22056 ],
22057 'fields' => 'ids',
22058 ]
22059 );
22060 } elseif ( 'payment_complete' === $term || 'payment_failed' === $term ) {
22061 $users = get_users(
22062 [
22063 'orderby' => 'meta_value',
22064 'meta_key' => 'pm_group_payment_status',
22065 'order' => 'DESC',
22066 'number' => 1,
22067 'meta_query' => [
22068 [
22069 'key' => 'pm_group_payment_status',
22070 'compare' => 'EXISTS',
22071 ],
22072 ],
22073 'fields' => 'ids',
22074 ]
22075 );
22076 } elseif ( 'membership_request' === $term ) {
22077 $results = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}promag_group_requests WHERE status = 1 ORDER BY id DESC Limit 1", ARRAY_A );
22078 }
22079 if ( ! empty( $users ) ) {
22080 if ( 'user_added_group' === $term || 'user_removed_from_group' === $term || 'user_assigned_group_manager' === $term || 'user_unassigned_group_manager' === $term || 'payment_complete' === $term || 'payment_failed' === $term || 'membership_request_approved' === $term ) {
22081 $context = WordPress::get_user_context( $users[0] );
22082 $user_groups = get_user_meta( $users[0], 'pm_group', true );
22083 if ( is_array( $user_groups ) ) {
22084 $last_element = end( $user_groups );
22085 $context['group_id'] = $last_element;
22086 $context = array_merge( $context, ProfileGrid::pg_group_details( $last_element ) );
22087 }
22088 $context['pluggable_data'] = $context;
22089 $context['response_type'] = 'live';
22090 }
22091 } elseif ( ! empty( $results ) && 'membership_request' === $term ) {
22092 $context = WordPress::get_user_context( $results[0]['uid'] );
22093 $context['group_id'] = $results[0]['gid'];
22094 $context = array_merge( $context, ProfileGrid::pg_group_details( $results[0]['gid'] ) );
22095 $context['pluggable_data'] = $context;
22096 $context['response_type'] = 'live';
22097 } elseif ( 'group_manager_resets_password' === $term ) {
22098 $context = json_decode( '{"pluggable_data":{"wp_user_id":2,"user_login":"johnd@gmail.com","display_name":"JohnD","user_firstname":"johnd","user_lastname":"johnd","user_email":"johnd@gmail.com","user_registered":"2023-01-19 09:14:50","user_role":["editor"]},"response_type":"sample"}', true );
22099 } elseif ( 'group_deleted' === $term ) {
22100 $context = json_decode( '{"pluggable_data":{"group_name":"Test Group","group_description":"Testing Group","group_id":"2"},"response_type":"sample"}', true );
22101 } else {
22102 $context = json_decode( '{"pluggable_data":{"wp_user_id":2,"user_login":"johnd@gmail.com","display_name":"JohnD","user_firstname":"johnd","user_lastname":"johnd","user_email":"johnd@gmail.com","user_registered":"2023-01-19 09:14:50","user_role":["editor"],"group_id":"2","group_name":"Test Group","group_description":"Testing Group"},"response_type":"sample"}', true );
22103 }
22104 return (array) $context;
22105 }
22106
22107 /**
22108 * Get Fluent SMTP Last Data
22109 *
22110 * @param array $data data.
22111 *
22112 * @return array
22113 */
22114 public function search_fluent_smtp_last_data( $data ) {
22115 $context = [];
22116 global $wpdb;
22117 $results = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fsmpt_email_logs WHERE status = 'failed' ORDER BY id DESC Limit 1", ARRAY_A );
22118 if ( ! empty( $results ) ) {
22119 $context['to'] = unserialize( $results['to'] );
22120 $context['from'] = $results['from'];
22121 $context['subject'] = $results['subject'];
22122 $context['body'] = $results['body'];
22123 $context['attachments'] = unserialize( $results['attachments'] );
22124 $context['status'] = $results['status'];
22125 $context['response'] = unserialize( $results['response'] );
22126 $context['headers'] = unserialize( $results['headers'] );
22127 $context['extra'] = unserialize( $results['extra'] );
22128 $context['pluggable_data'] = $context;
22129 $context['response_type'] = 'live';
22130 } else {
22131 $context = json_decode( '{"pluggable_data":{"to":[{"email":"johnd@example.com"}],"from":"johnd <johnd@example.com>","subject":"We received your message","body":"Your message has been successfully sent. We appreciate you contacting us and we will be in touch soon.\n<br><br>","attachments":[],"status":"failed","response":{"code":422,"message":"SMTP Error: data not accepted.","errors":["SMTP Error: data not accepted."]},"headers":{"reply-to":[{"email":"johnd@example.com"}],"cc":[],"bcc":[],"content-type":"text/html"},"extra":{"provider":"smtp"}},"response_type":"sample"}', true );
22132 }
22133 return (array) $context;
22134 }
22135
22136 /**
22137 * Get Fluent Community Last Data
22138 *
22139 * @param array $data data.
22140 *
22141 * @return array
22142 */
22143 public function search_fc_triggers_last_data( $data ) {
22144 $context = [];
22145 $space_result = [];
22146 $profile_result = [];
22147 $user_result = [];
22148 $feed_data = [];
22149 $course_result = [];
22150 global $wpdb;
22151 $term = $data['search_term'] ? $data['search_term'] : '';
22152
22153 $data = [];
22154
22155 if ( 'space_created' === $term || 'user_leaves_space' === $term || 'user_requests_join_space' === $term || 'feed_created' === $term || 'feed_updated' === $term || 'space_feed_created' === $term || 'user_joins_space' === $term ) {
22156 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_spaces ORDER BY id DESC LIMIT 1", ARRAY_A );
22157 $space_result = [
22158 'id' => $result['id'],
22159 'slug' => $result['slug'],
22160 'title' => $result['title'],
22161 'description' => $result['description'],
22162 'type' => $result['type'],
22163 'privacy' => $result['privacy'],
22164 'status' => $result['status'],
22165 'serial' => $result['serial'],
22166 'created_at' => $result['created_at'],
22167 'created_by' => $result['created_by'],
22168 'updated_at' => $result['updated_at'],
22169 'settings' => unserialize( $result['settings'] ),
22170 ];
22171 }
22172
22173 if ( 'feed_created' === $term || 'feed_updated' === $term || 'space_feed_created' === $term ) {
22174 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_posts ORDER BY id DESC LIMIT 1", ARRAY_A );
22175 $profile_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_xprofile ORDER BY id DESC LIMIT 1", ARRAY_A );
22176 $user_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}users ORDER BY id DESC LIMIT 1", ARRAY_A );
22177 $feed_data = [
22178 'feed' => [
22179 'id' => $result['id'],
22180 'user_id' => $result['user_id'],
22181 'parentid' => $result['parent_id'],
22182 'title' => $result['title'],
22183 'slug' => $result['slug'],
22184 'message' => $result['message'],
22185 'message_rendered' => $result['message_rendered'],
22186 'type' => $result['type'],
22187 'content_type' => $result['content_type'],
22188 'space_id' => $result['space_id'],
22189 'privacy' => $result['privacy'],
22190 'status' => $result['status'],
22191 'featured_image' => $result['featured_image'],
22192 'meta' => unserialize( $result['meta'] ),
22193 'issticky' => $result['issticky'],
22194 'comments_count' => $result['comments_count'],
22195 'reactions_count' => $result['reactions_count'],
22196 'priority' => $result['priority'],
22197 'expiredat' => $result['expired_at'],
22198 'scheduledat' => $result['scheduled_at'],
22199 'created_at' => $result['created_at'],
22200 'updated_at' => $result['updated_at'],
22201 'space' => $space_result,
22202 'xprofile' => [
22203 'id' => $profile_result['id'],
22204 'user_id' => $profile_result['user_id'],
22205 'total_points' => $profile_result['total_points'],
22206 'username' => $profile_result['username'],
22207 'status' => $profile_result['status'],
22208 'is_verified' => $profile_result['is_verified'],
22209 'display_name' => $profile_result['display_name'],
22210 'avatar' => $profile_result['avatar'],
22211 'short_description' => $profile_result['short_description'],
22212 'last_activity' => $profile_result['last_activity'],
22213 'meta' => unserialize( $profile_result['meta'] ),
22214 'created_at' => $profile_result['created_at'],
22215 'updated_at' => $profile_result['updated_at'],
22216 ],
22217 ],
22218 ];
22219 }
22220
22221 if ( 'user_joins_space' === $term ) {
22222 $user_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}users ORDER BY id DESC LIMIT 1", ARRAY_A );
22223 }
22224
22225 if ( 'course_created' === $term || 'course_published' === $term || 'course_updated' === $term || 'user_enrolls_course' === $term || 'user_unenrolls_course' === $term || 'user_completes_course' === $term ) {
22226 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_spaces WHERE type = 'course' ORDER BY id DESC LIMIT 1", ARRAY_A );
22227 $course_result = [
22228 'id' => $result['id'],
22229 'slug' => $result['slug'],
22230 'title' => $result['title'],
22231 'serial' => $result['serial'],
22232 'status' => $result['status'],
22233 'privacy' => $result['privacy'],
22234 'created_at' => $result['created_at'],
22235 'created_by' => $result['created_by'],
22236 'updated_at' => $result['updated_at'],
22237 'description' => $result['description'],
22238 'settings' => unserialize( $result['settings'] ),
22239 ];
22240 }
22241
22242 if ( 'course_deleted' === $term ) {
22243 $result = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}fcom_spaces WHERE type = 'course' ORDER BY id DESC LIMIT 1" );
22244 }
22245
22246 if ( 'new_comment_added' === $term || 'comment_updated' === $term ) {
22247 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_post_comments ORDER BY id DESC LIMIT 1", ARRAY_A );
22248 $profile_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_xprofile ORDER BY id DESC Limit 1", ARRAY_A );
22249 }
22250
22251 if ( 'comment_deleted' === $term ) {
22252 $result = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}fcom_post_comments ORDER BY id DESC LIMIT 1" );
22253 }
22254
22255 if ( ! empty( $result ) ) {
22256 if ( 'space_created' === $term ) {
22257 $context['pluggable_data'] = [ 'space' => $space_result ];
22258 $context['response_type'] = 'live';
22259 }
22260
22261 if ( 'feed_created' === $term || 'feed_updated' === $term ) {
22262 $context['pluggable_data'] = $feed_data;
22263 $context['response_type'] = 'live';
22264 }
22265
22266 if ( 'space_feed_created' === $term ) {
22267 $feed_data['feed']['user'] = [
22268 'user_url' => $user_result['user_url'],
22269 'user_email' => $user_result['user_email'],
22270 'user_login' => $user_result['user_login'],
22271 'user_status' => $user_result['user_status'],
22272 'display_name' => $user_result['display_name'],
22273 'user_nicename' => $user_result['user_nicename'],
22274 'user_registered' => $user_result['user_registered'],
22275 ];
22276 $context['pluggable_data'] = $feed_data;
22277 $context['response_type'] = 'live';
22278 }
22279
22280 if ( 'user_joins_space' === $term ) {
22281 $context['pluggable_data'] = [
22282 'space' => [
22283 'id' => $result['id'],
22284 'slug' => $result['slug'],
22285 'title' => $result['title'],
22286 'description' => $result['description'],
22287 'type' => $result['type'],
22288 'privacy' => $result['privacy'],
22289 'status' => $result['status'],
22290 'serial' => $result['serial'],
22291 'created_at' => $result['created_at'],
22292 'created_by' => $result['created_by'],
22293 'updated_at' => $result['updated_at'],
22294 'settings' => unserialize( $result['settings'] ),
22295 'membership' => [
22296 'ID' => $user_result['ID'],
22297 'photo' => '',
22298 'pivot_role' => 'admin',
22299 'pivot_status' => 'active',
22300 'pivot_created_at' => $result['created_at'],
22301 'pivot_user_id' => $user_result['ID'],
22302 'pivot_space_id' => $result['id'],
22303 'user_url' => $user_result['user_url'],
22304 'user_email' => $user_result['user_email'],
22305 'user_login' => $user_result['user_login'],
22306 'user_status' => $user_result['user_status'],
22307 'display_name' => $user_result['display_name'],
22308 'user_nicename' => $user_result['user_nicename'],
22309 'user_registered' => $user_result['user_registered'],
22310 ],
22311 ],
22312 'userId' => 1,
22313 'by' => 'self',
22314 ];
22315 $context['response_type'] = 'live';
22316 }
22317 if ( 'user_leaves_space' === $term ) {
22318 $context['pluggable_data'] = [
22319 'space' => $space_result,
22320 'user' => WordPress::get_sample_user_context(),
22321 'userId' => 1,
22322 'by' => 'self',
22323 ];
22324 $context['response_type'] = 'live';
22325 }
22326 if ( 'course_created' === $term || 'course_updated' === $term || 'course_published' === $term ) {
22327 $context['pluggable_data'] = [
22328 'course' => $course_result,
22329 ];
22330 $context['response_type'] = 'live';
22331 }
22332
22333 if ( 'course_deleted' === $term ) {
22334 $context['pluggable_data'] = [ 'course' => $result ];
22335 $context['response_type'] = 'live';
22336 }
22337
22338 if ( 'comment_deleted' === $term ) {
22339 $context['pluggable_data'] = [ 'comment_id' => $result ];
22340 $context['response_type'] = 'live';
22341 }
22342 if ( 'new_comment_added' === $term || 'comment_updated' === $term ) {
22343 $context['pluggable_data'] = [
22344 'comment' => [
22345 'id' => $result['id'],
22346 'user_id' => $result['user_id'],
22347 'post_id' => $result['post_id'],
22348 'parent_id' => $result['parent_id'],
22349 'reactions_count' => $result['reactions_count'],
22350 'message' => $result['message'],
22351 'message_rendered' => $result['message_rendered'],
22352 'meta' => unserialize( $result['meta'] ),
22353 'type' => $result['type'],
22354 'content_type' => $result['content_type'],
22355 'status' => $result['status'],
22356 'is_sticky' => $result['is_sticky'],
22357 'created_at' => $result['created_at'],
22358 'updated_at' => $result['updated_at'],
22359 'xprofile' => [
22360 'id' => $profile_result['id'],
22361 'user_id' => $profile_result['user_id'],
22362 'total_points' => $profile_result['total_points'],
22363 'username' => $profile_result['username'],
22364 'status' => $profile_result['status'],
22365 'is_verified' => $profile_result['is_verified'],
22366 'display_name' => $profile_result['display_name'],
22367 'avatar' => $profile_result['avatar'],
22368 'short_description' => $profile_result['short_description'],
22369 'last_activity' => $profile_result['last_activity'],
22370 'meta' => unserialize( $profile_result['meta'] ),
22371 'created_at' => $profile_result['created_at'],
22372 'updated_at' => $profile_result['updated_at'],
22373 ],
22374 ],
22375 ];
22376 $context['response_type'] = 'live';
22377 }
22378 if ( 'user_enrolls_course' === $term || 'user_unenrolls_course' === $term || 'user_completes_course' === $term ) {
22379 $context['pluggable_data'] = [
22380 'course' => $course_result,
22381 'user' => WordPress::get_sample_user_context(),
22382 'userId' => 1,
22383 ];
22384 $context['response_type'] = 'live';
22385 }
22386 if ( 'user_requests_join_space' === $term ) {
22387 $context['pluggable_data'] = [
22388 'space' => $space_result,
22389 'user' => WordPress::get_sample_user_context(),
22390 'userId' => 1,
22391 ];
22392 $context['response_type'] = 'live';
22393 }
22394 } else {
22395 $sample_space_data = [
22396 'id' => 101,
22397 'slug' => 'sample-space',
22398 'title' => 'Sample Space',
22399 'description' => 'This is a sample space description.',
22400 'type' => 'community',
22401 'privacy' => 'public',
22402 'status' => 'active',
22403 'serial' => 'SP101',
22404 'created_at' => '2025-01-01 10:00:00',
22405 'created_by' => 1,
22406 'updated_at' => '2025-01-05 12:00:00',
22407 'settings' => [
22408 'coursetype' => 'self_paced',
22409 'emoji' => '😍',
22410 'shapesvg' => null,
22411 'disablecomments' => 'no',
22412 'hidememberscount' => 'no',
22413 ],
22414 ];
22415 $sample_profile_data = [
22416 'id' => 1,
22417 'user_id' => 1,
22418 'total_points' => 100,
22419 'username' => 'sampleuser',
22420 'status' => 'active',
22421 'is_verified' => true,
22422 'display_name' => 'Sample User',
22423 'avatar' => 'https://example.com/avatar.jpg',
22424 'short_description' => 'This is a sample user.',
22425 'last_activity' => '2025-01-08 15:00:00',
22426 'meta' => [
22427 'website' => 'https://example.com',
22428 'coverphoto' => 'https://example.com/coverphoto.jpg',
22429 ],
22430 'created_at' => '2025-01-01 10:00:00',
22431 'updated_at' => '2025-01-05 12:00:00',
22432 ];
22433
22434 if ( 'feed_created' === $term || 'feed_updated' === $term || 'space_feed_created' === $term ) {
22435 $context['pluggable_data'] = [
22436 'feed' => [
22437 'id' => 1,
22438 'userid' => 1,
22439 'parentid' => null,
22440 'title' => 'Sample Feed',
22441 'slug' => 'sample-feed',
22442 'message' => 'This is a sample message for the feed.',
22443 'messagerendered' => '<p>This is a sample message for the feed.</p>',
22444 'type' => 'post',
22445 'contenttype' => 'text',
22446 'space_id' => 101,
22447 'privacy' => 'public',
22448 'status' => 'published',
22449 'featuredimage' => 'https://example.com/image.jpg',
22450 'meta' => [
22451 'previewdata' => 0,
22452 ],
22453 'issticky' => false,
22454 'commentscount' => 10,
22455 'reactionscount' => 25,
22456 'priority' => 'normal',
22457 'expiredat' => null,
22458 'scheduledat' => '2025-01-10 08:00:00',
22459 'createdat' => '2025-01-09 09:00:00',
22460 'updatedat' => '2025-01-09 10:00:00',
22461 'space' => $sample_space_data,
22462 'xprofile' => $sample_profile_data,
22463 ],
22464 ];
22465 $context['response_type'] = 'sample';
22466 }
22467 if ( 'space_feed_created' === $term ) {
22468 $conext['pluggable_data']['feed']['user'] = [
22469 'user_url' => 'https://example.com/user/profile',
22470 'user_email' => 'user@example.com',
22471 'user_login' => 'sampleuser',
22472 'user_status' => 'active',
22473 'display_name' => 'Sample User',
22474 'user_nicename' => 'sample-user',
22475 'user_registered' => '2024-01-01 00:00:00',
22476 ];
22477 $context['pluggable_data']['feed']['space'] = $sample_space_data;
22478 $context['pluggable_data']['feed']['xprofile'] = $sample_profile_data;
22479 $context['response_type'] = 'sample';
22480 }
22481 if ( 'user_joins_space' === $term ) {
22482 $context['pluggable_data'] = [
22483 'space' => [
22484 $sample_space_data,
22485 'membership' => [
22486 'ID' => 25,
22487 'photo' => 'https://example.com/user-photo.jpg',
22488 'pivot_role' => 'admin',
22489 'pivot_status' => 'active',
22490 'pivot_created_at' => '2025-01-24 09:00:00',
22491 'pivot_user_id' => 25,
22492 'pivot_space_id' => 100,
22493 'user_url' => 'https://example.com/user-profile',
22494 'user_email' => 'user@example.com',
22495 'user_login' => 'sampleuser',
22496 'user_status' => 'active',
22497 'display_name' => 'Sample User',
22498 'user_nicename' => 'sample-user',
22499 'user_registered' => '2024-01-01 00:00:00',
22500 ],
22501 ],
22502 'userId' => 1,
22503 'by' => 'self',
22504 ];
22505 $context['response_type'] = 'sample';
22506 }
22507
22508 if ( 'user_leaves_space' === $term ) {
22509 $context['pluggable_data'] = [
22510 'space' => $sample_space_data,
22511 'user' => WordPress::get_sample_user_context(),
22512 'userId' => 35,
22513 'by' => 'self',
22514 ];
22515 $context['response_type'] = 'sample';
22516 }
22517
22518 if ( 'new_comment_added' === $term || 'comment_updated' === $term ) {
22519 $context['pluggable_data'] = [
22520 'comment' => [
22521 'id' => 301,
22522 'user_id' => 45,
22523 'post_id' => 101,
22524 'parent_id' => 0,
22525 'reactions_count' => 5,
22526 'message' => 'This is a sample comment message.',
22527 'message_rendered' => '<p>This is a sample comment message.</p>',
22528 'meta' => [
22529 'likes' => 3,
22530 'shares' => 2,
22531 ],
22532 'type' => 'text',
22533 'content_type' => 'post',
22534 'status' => 'approved',
22535 'is_sticky' => false,
22536 'created_at' => '2025-01-23 10:30:00',
22537 'updated_at' => '2025-01-23 12:00:00',
22538 'xprofile' => $sample_profile_data,
22539 ],
22540 ];
22541 $context['response_type'] = 'sample';
22542 }
22543
22544 if ( 'course_deleted' === $term ) {
22545 $context['pluggable_data'] = [ 'course' => 2 ];
22546 $context['response_type'] = 'sample';
22547 }
22548 if ( 'comment_deleted' === $term ) {
22549 $context['pluggable_data'] = [ 'comment_id' => 1 ];
22550 $context['response_type'] = 'sample';
22551 }
22552 if ( 'user_enrolls_course' === $term || 'user_unenrolls_course' === $term || 'user_completes_course' === $term ) {
22553 $context['pluggable_data'] = [
22554 'course' => $sample_space_data,
22555 'user' => WordPress::get_sample_user_context(),
22556 'userId' => 1,
22557 ];
22558 $context['response_type'] = 'sample';
22559 }
22560 if ( 'course_created' === $term || 'course_updated' === $term || 'course_published' === $term ) {
22561 $context['pluggable_data'] = [
22562 'course' => $sample_space_data,
22563 ];
22564 $context['response_type'] = 'sample';
22565 }
22566 if ( 'user_requests_join_space' === $term ) {
22567 $context['pluggable_data'] = [
22568 'space' => $sample_space_data,
22569 'user' => WordPress::get_sample_user_context(),
22570 'userId' => 1,
22571 ];
22572 $context['response_type'] = 'sample';
22573 }
22574 }
22575 return (array) $context;
22576 }
22577
22578 /**
22579 * Prepare FluentCommunity Courses List.
22580 *
22581 * @param array $data Search Params.
22582 * @return array
22583 */
22584 public function search_fc_courses_list( $data ) {
22585 global $wpdb;
22586
22587 $courses = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fcom_spaces WHERE type = 'course' ", ARRAY_A );
22588
22589 $options = [];
22590
22591 if ( ! empty( $courses ) ) {
22592 foreach ( $courses as $course ) {
22593 $options[] = [
22594 'label' => $course['title'],
22595 'value' => $course['id'],
22596 ];
22597 }
22598 }
22599
22600 return [
22601 'options' => $options,
22602 'hasMore' => false,
22603 ];
22604 }
22605
22606 /**
22607 * Search Thrive Apprentice Courses List.
22608 *
22609 * @param array $data Search Params.
22610 * @return array
22611 */
22612 public function search_thrive_apprentice_courses_list( $data ) {
22613 $page = isset( $data['page'] ) ? $data['page'] : 1;
22614 $limit = Utilities::get_search_page_limit();
22615 $offset = $limit * ( $page - 1 );
22616
22617 if ( ! defined( 'TVA_IS_APPRENTICE' ) && ! class_exists( 'TVA_Const' ) ) {
22618 return [
22619 'options' => [],
22620 'hasMore' => false,
22621 ];
22622 }
22623
22624 $courses = get_terms(
22625 [
22626 'taxonomy' => 'tva_courses',
22627 'hide_empty' => false,
22628 'number' => $limit,
22629 'offset' => $offset,
22630 ]
22631 );
22632
22633 $options = [];
22634
22635 if ( ! empty( $courses ) && ! is_wp_error( $courses ) ) {
22636 foreach ( $courses as $course ) {
22637 $options[] = [
22638 'label' => $course->name,
22639 'value' => $course->term_id,
22640 ];
22641 }
22642 }
22643
22644 $all_courses_count = get_terms(
22645 [
22646 'taxonomy' => 'tva_courses',
22647 'hide_empty' => false,
22648 'fields' => 'count',
22649 ]
22650 );
22651
22652 $has_more = ( $offset + $limit ) < $all_courses_count;
22653
22654 return [
22655 'options' => $options,
22656 'hasMore' => $has_more,
22657 ];
22658 }
22659
22660 /**
22661 * Search Thrive Apprentice Assessments List.
22662 *
22663 * @param array $data Search Params.
22664 * @return array
22665 */
22666 public function search_thrive_apprentice_assessments_list( $data ) {
22667 $page = isset( $data['page'] ) ? $data['page'] : 1;
22668 $limit = Utilities::get_search_page_limit();
22669 $offset = $limit * ( $page - 1 );
22670 $course_id = isset( $data['dynamic']['course_id'] ) ? $data['dynamic']['course_id'] : '';
22671
22672 if ( ! defined( 'TVA_IS_APPRENTICE' ) && ! class_exists( 'TVA_Const' ) ) {
22673 return [
22674 'options' => [],
22675 'hasMore' => false,
22676 ];
22677 }
22678
22679 if ( ! empty( $course_id ) ) {
22680 $course_post_ids = get_objects_in_term( intval( $course_id ), 'tva_courses' );
22681 if ( empty( $course_post_ids ) || is_wp_error( $course_post_ids ) ) {
22682 return [
22683 'options' => [],
22684 'hasMore' => false,
22685 ];
22686 }
22687 $args = [
22688 'post_type' => 'tva_assessment',
22689 'post_status' => 'publish',
22690 'posts_per_page' => $limit,
22691 'offset' => $offset,
22692 'fields' => 'ids',
22693 'post__in' => array_map( 'intval', $course_post_ids ),
22694 ];
22695 } else {
22696 $args = [
22697 'post_type' => 'tva_assessment',
22698 'post_status' => 'publish',
22699 'posts_per_page' => $limit,
22700 'offset' => $offset,
22701 'fields' => 'ids',
22702 ];
22703 }
22704
22705 $assessments = get_posts( $args );
22706 $options = [];
22707
22708 if ( ! empty( $assessments ) ) {
22709 foreach ( $assessments as $assessment_id ) {
22710 $assessment = get_post( $assessment_id );
22711 if ( $assessment ) {
22712 $options[] = [
22713 'label' => $assessment->post_title,
22714 'value' => $assessment->ID,
22715 ];
22716 }
22717 }
22718 }
22719
22720 $count_args = $args;
22721 $count_args['posts_per_page'] = -1;
22722 $count_args['offset'] = 0;
22723 $all_assessments = get_posts( $count_args );
22724 $has_more = ( $offset + $limit ) < count( $all_assessments );
22725
22726 return [
22727 'options' => $options,
22728 'hasMore' => $has_more,
22729 ];
22730 }
22731
22732 /**
22733 * Search Thrive Apprentice Lessons List.
22734 *
22735 * @param array $data Search Params.
22736 * @return array
22737 */
22738 public function search_thrive_apprentice_lessons_list( $data ) {
22739 $page = isset( $data['page'] ) ? $data['page'] : 1;
22740 $limit = Utilities::get_search_page_limit();
22741 $offset = $limit * ( $page - 1 );
22742 $course_id = isset( $data['dynamic']['course_id'] ) ? $data['dynamic']['course_id'] : '';
22743
22744 if ( ! defined( 'TVA_IS_APPRENTICE' ) && ! class_exists( 'TVA_Const' ) ) {
22745 return [
22746 'options' => [],
22747 'hasMore' => false,
22748 ];
22749 }
22750
22751 if ( ! empty( $course_id ) ) {
22752 $course_post_ids = get_objects_in_term( intval( $course_id ), 'tva_courses' );
22753 if ( empty( $course_post_ids ) || is_wp_error( $course_post_ids ) ) {
22754 return [
22755 'options' => [],
22756 'hasMore' => false,
22757 ];
22758 }
22759 $args = [
22760 'post_type' => 'tva_lesson',
22761 'post_status' => 'publish',
22762 'posts_per_page' => $limit,
22763 'offset' => $offset,
22764 'fields' => 'ids',
22765 'post__in' => array_map( 'intval', $course_post_ids ),
22766 ];
22767 } else {
22768 $args = [
22769 'post_type' => 'tva_lesson',
22770 'post_status' => 'publish',
22771 'posts_per_page' => $limit,
22772 'offset' => $offset,
22773 'fields' => 'ids',
22774 ];
22775 }
22776
22777 $lessons = get_posts( $args );
22778 $options = [];
22779
22780 if ( ! empty( $lessons ) ) {
22781 foreach ( $lessons as $lesson_id ) {
22782 $lesson = get_post( $lesson_id );
22783 if ( $lesson ) {
22784 $options[] = [
22785 'label' => $lesson->post_title,
22786 'value' => $lesson->ID,
22787 ];
22788 }
22789 }
22790 }
22791
22792 $count_args = $args;
22793 $count_args['posts_per_page'] = -1;
22794 $count_args['offset'] = 0;
22795 $all_lessons = get_posts( $count_args );
22796 $has_more = ( $offset + $limit ) < count( $all_lessons );
22797
22798 return [
22799 'options' => $options,
22800 'hasMore' => $has_more,
22801 ];
22802 }
22803
22804 /**
22805 * Search Thrive Apprentice Premium Courses List.
22806 *
22807 * @param array $data Search Params.
22808 * @return array
22809 */
22810 public function search_thrive_apprentice_premium_courses_list( $data ) {
22811 $page = isset( $data['page'] ) ? $data['page'] : 1;
22812 $limit = Utilities::get_search_page_limit();
22813 $offset = $limit * ( $page - 1 );
22814
22815 if ( ! defined( 'TVA_IS_APPRENTICE' ) && ! class_exists( 'TVA_Const' ) ) {
22816 return [
22817 'options' => [],
22818 'hasMore' => false,
22819 ];
22820 }
22821
22822 $courses = get_terms(
22823 [
22824 'taxonomy' => 'tva_courses',
22825 'hide_empty' => false,
22826 'number' => $limit,
22827 'offset' => $offset,
22828 ]
22829 );
22830
22831 $options = [];
22832
22833 if ( ! empty( $courses ) && ! is_wp_error( $courses ) ) {
22834 foreach ( $courses as $course ) {
22835 if ( class_exists( 'TVA_Course_V2' ) ) {
22836 $course_obj = new \TVA_Course_V2( $course->term_id );
22837 $product = $course_obj->get_product();
22838 if ( ! empty( $product ) ) {
22839 $options[] = [
22840 'label' => $course->name,
22841 'value' => $course->term_id,
22842 ];
22843 }
22844 }
22845 }
22846 }
22847 $all_courses = get_terms(
22848 [
22849 'taxonomy' => 'tva_courses',
22850 'hide_empty' => false,
22851 'fields' => 'all',
22852 ]
22853 );
22854
22855 $premium_count = 0;
22856 if ( ! empty( $all_courses ) && ! is_wp_error( $all_courses ) ) {
22857 foreach ( $all_courses as $course ) {
22858 if ( class_exists( 'TVA_Course_V2' ) ) {
22859 $course_obj = new \TVA_Course_V2( $course->term_id );
22860 $product = $course_obj->get_product();
22861 if ( ! empty( $product ) ) {
22862 $premium_count++;
22863 }
22864 }
22865 }
22866 }
22867
22868 $has_more = ( $offset + $limit ) < $premium_count;
22869
22870 return [
22871 'options' => $options,
22872 'hasMore' => $has_more,
22873 ];
22874 }
22875
22876 /**
22877 * Search Thrive Apprentice Modules List.
22878 *
22879 * @param array $data Search Params.
22880 * @return array
22881 */
22882 public function search_thrive_apprentice_modules_list( $data ) {
22883 $page = isset( $data['page'] ) ? $data['page'] : 1;
22884 $limit = Utilities::get_search_page_limit();
22885 $offset = $limit * ( $page - 1 );
22886 $course_id = isset( $data['dynamic']['course_id'] ) ? $data['dynamic']['course_id'] : '';
22887
22888 if ( ! defined( 'TVA_IS_APPRENTICE' ) && ! class_exists( 'TVA_Const' ) ) {
22889 return [
22890 'options' => [],
22891 'hasMore' => false,
22892 ];
22893 }
22894
22895 if ( ! empty( $course_id ) ) {
22896 $course_post_ids = get_objects_in_term( intval( $course_id ), 'tva_courses' );
22897 if ( empty( $course_post_ids ) || is_wp_error( $course_post_ids ) ) {
22898 return [
22899 'options' => [],
22900 'hasMore' => false,
22901 ];
22902 }
22903 $args = [
22904 'post_type' => 'tva_module',
22905 'post_status' => 'publish',
22906 'posts_per_page' => $limit,
22907 'offset' => $offset,
22908 'fields' => 'ids',
22909 'post__in' => array_map( 'intval', $course_post_ids ),
22910 ];
22911 } else {
22912 $args = [
22913 'post_type' => 'tva_module',
22914 'post_status' => 'publish',
22915 'posts_per_page' => $limit,
22916 'offset' => $offset,
22917 'fields' => 'ids',
22918 ];
22919 }
22920
22921 $modules = get_posts( $args );
22922 $options = [];
22923
22924 if ( ! empty( $modules ) ) {
22925 foreach ( $modules as $module_id ) {
22926 $module = get_post( $module_id );
22927 if ( $module ) {
22928 $options[] = [
22929 'label' => $module->post_title,
22930 'value' => $module->ID,
22931 ];
22932 }
22933 }
22934 }
22935
22936 if ( ! empty( $course_id ) ) {
22937 $total_count = count( $course_post_ids );
22938 $has_more = ( $offset + $limit ) < $total_count;
22939 } else {
22940 $count_args = $args;
22941 $count_args['posts_per_page'] = -1;
22942 $count_args['offset'] = 0;
22943 $all_modules = get_posts( $count_args );
22944 $has_more = ( $offset + $limit ) < count( $all_modules );
22945 }
22946
22947 return [
22948 'options' => $options,
22949 'hasMore' => $has_more,
22950 ];
22951 }
22952
22953 /**
22954 * Search Thrive Apprentice Products List.
22955 *
22956 * @param array $data Search Params.
22957 * @return array
22958 */
22959 public function search_thrive_apprentice_products_list( $data ) {
22960 $page = isset( $data['page'] ) ? $data['page'] : 1;
22961 $limit = Utilities::get_search_page_limit();
22962 $offset = $limit * ( $page - 1 );
22963
22964 if ( ! defined( 'TVA_IS_APPRENTICE' ) && ! class_exists( 'TVA_Const' ) ) {
22965 return [
22966 'options' => [],
22967 'hasMore' => false,
22968 ];
22969 }
22970
22971 $products = get_terms(
22972 [
22973 'taxonomy' => 'tva_product',
22974 'hide_empty' => false,
22975 'number' => $limit,
22976 'offset' => $offset,
22977 ]
22978 );
22979
22980 $options = [];
22981
22982 if ( ! empty( $products ) && ! is_wp_error( $products ) ) {
22983 foreach ( $products as $product ) {
22984 $options[] = [
22985 'label' => $product->name,
22986 'value' => $product->term_id,
22987 ];
22988 }
22989 }
22990
22991 $all_products_count = get_terms(
22992 [
22993 'taxonomy' => 'tva_product',
22994 'hide_empty' => false,
22995 'fields' => 'count',
22996 ]
22997 );
22998
22999 $has_more = ( $offset + $limit ) < $all_products_count;
23000
23001 return [
23002 'options' => $options,
23003 'hasMore' => $has_more,
23004 ];
23005 }
23006
23007 /**
23008 * Prepare FluentCommunity Spaces List.
23009 *
23010 * @param array $data Search Params.
23011 * @return array
23012 */
23013 public function search_fc_spaces_list( $data ) {
23014 global $wpdb;
23015
23016 $spaces = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fcom_spaces WHERE type NOT IN ('course', 'space_group')", ARRAY_A );
23017
23018 $options = [];
23019
23020 if ( ! empty( $spaces ) ) {
23021 foreach ( $spaces as $space ) {
23022 $options[] = [
23023 'label' => $space['title'],
23024 'value' => $space['id'],
23025 ];
23026 }
23027 }
23028
23029 return [
23030 'options' => $options,
23031 'hasMore' => false,
23032 ];
23033 }
23034
23035 /**
23036 * Prepare FluentCommunity Topics List.
23037 *
23038 * @param array $data Search Params.
23039 * @return array
23040 */
23041 public function search_fc_topics_list( $data ) {
23042 if ( ! class_exists( '\FluentCommunity\App\Functions\Utility' ) ) {
23043 return [];
23044 }
23045
23046 $space_topics = Utility::getTopicsBySpaceId( $data['dynamic'] );
23047
23048 $options = [];
23049
23050 if ( ! empty( $space_topics ) ) {
23051 foreach ( $space_topics as $topic ) {
23052 $options[] = [
23053 'label' => $topic['title'],
23054 'value' => $topic['id'],
23055 ];
23056 }
23057 }
23058
23059 return [
23060 'options' => $options,
23061 'hasMore' => false,
23062 ];
23063 }
23064
23065 /**
23066 * Get WP Travel Engine Last Data
23067 *
23068 * @param array $data data.
23069 *
23070 * @return array
23071 */
23072 public function search_wpte_triggers_last_data( $data ) {
23073 global $wpdb;
23074
23075 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
23076
23077 $context = [];
23078 $result = null;
23079
23080 switch ( $term ) {
23081 case 'enquiry_submitted':
23082 $result = $wpdb->get_row(
23083 "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'enquiry' ORDER BY ID DESC LIMIT 1",
23084 ARRAY_A
23085 );
23086
23087 if ( $result && isset( $result['ID'] ) ) {
23088 $data = get_post( $result['ID'] );
23089 }
23090 break;
23091
23092 case 'booking_created':
23093 case 'booking_confirmed':
23094 case 'booking_cancelled':
23095 $result = $wpdb->get_row(
23096 "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'booking' ORDER BY ID DESC LIMIT 1",
23097 ARRAY_A
23098 );
23099
23100 if ( $result && isset( $result['ID'] ) ) {
23101 $booking_id = $result['ID'];
23102 $booking_data = get_post( $booking_id );
23103 $booking_meta = get_post_meta( $booking_id, 'wp_travel_engine_booking_setting', true );
23104 $booking_status = get_post_meta( $booking_id, 'wp_travel_engine_booking_status', true );
23105
23106 // Set correct booking status based on trigger type.
23107 switch ( $term ) {
23108 case 'booking_confirmed':
23109 $booking_status = 'booked';
23110 break;
23111 case 'booking_cancelled':
23112 $booking_status = 'cancelled';
23113 break;
23114 }
23115
23116 // For booking_created, include payment data as well.
23117 if ( 'booking_created' === $term ) {
23118 $payment_status = get_post_meta( $booking_id, 'wp_travel_engine_booking_payment_status', true );
23119 $payment_gateway = get_post_meta( $booking_id, 'wp_travel_engine_booking_payment_gateway', true );
23120 $payment_details = get_post_meta( $booking_id, 'wp_travel_engine_booking_payment_details', true );
23121
23122 $coupon_details = [];
23123 $cart_info = get_post_meta( $booking_id, 'cart_info', true );
23124
23125 if ( ! empty( $cart_info ) && is_array( $cart_info ) && isset( $cart_info['discounts'] ) ) {
23126 foreach ( $cart_info['discounts'] as $discount ) {
23127 if ( isset( $discount['name'] ) ) {
23128 $coupon_details[] = [
23129 'coupon_code' => $discount['name'],
23130 'discount_type' => isset( $discount['type'] ) ? $discount['type'] : '',
23131 'discount_value' => isset( $discount['value'] ) ? $discount['value'] : 0,
23132 ];
23133 }
23134 }
23135 }
23136
23137 if ( empty( $coupon_details ) ) {
23138 $coupon_details = [
23139 [
23140 'coupon_code' => 'SAVE20',
23141 'discount_type' => 'percentage',
23142 'discount_value' => 20,
23143 ],
23144 ];
23145 }
23146
23147 $data = [
23148 'booking_data' => $booking_data,
23149 'booking_meta' => $booking_meta,
23150 'payment_status' => $payment_status,
23151 'payment_gateway' => $payment_gateway,
23152 'payment_details' => $payment_details,
23153 'coupon_details' => $coupon_details,
23154 ];
23155 } else {
23156 $data = [
23157 'booking_data' => $booking_data,
23158 'booking_meta' => $booking_meta,
23159 'booking_status' => $booking_status,
23160 ];
23161 }
23162 }
23163 break;
23164 }
23165
23166 if ( ! empty( $result ) && isset( $result['ID'] ) ) {
23167 if ( in_array( $term, [ 'booking_created', 'booking_confirmed', 'booking_cancelled' ] ) ) {
23168 $context['pluggable_data'] = $data;
23169 } else {
23170 $context['pluggable_data'] = [
23171 'enquiry_post_id' => $result['ID'],
23172 'post_data' => $data,
23173 ];
23174 }
23175 $context['response_type'] = 'live';
23176 } else {
23177 // Helper function to generate booking sample data.
23178 $get_booking_sample_data = function( $customer, $trip, $booking_status = '', $payment_data = [] ) {
23179 $booking_data = [
23180 'ID' => 123,
23181 'post_title' => $customer['fname'] . ' ' . $customer['lname'] . ' #123',
23182 'post_name' => strtolower( $customer['fname'] . '-' . $customer['lname'] ) . '-123',
23183 'post_date' => '2025-01-15 10:00:00',
23184 'post_status' => 'publish',
23185 'post_type' => 'booking',
23186 'post_author' => 1,
23187 'post_content' => '',
23188 'post_excerpt' => '',
23189 'comment_status' => 'closed',
23190 'ping_status' => 'closed',
23191 'post_modified' => '2025-01-15 10:00:00',
23192 'guid' => 'http://example.com/booking/' . strtolower( $customer['fname'] . '-' . $customer['lname'] ) . '-123',
23193 ];
23194
23195 $booking_meta = [
23196 'place_order' => [
23197 'traveler' => 1,
23198 'cost' => 10,
23199 'due' => 10,
23200 'tid' => 1308,
23201 'tname' => 'New York',
23202 'datetime' => '2025-08-14',
23203 'datewithtime' => '',
23204 'booking' => [
23205 'fname' => 'John',
23206 'lname' => 'Doe',
23207 'email' => 'john.doe@example.com',
23208 'address' => '123 Main St',
23209 'city' => 'New York',
23210 'country' => 'US',
23211 ],
23212 'tax' => '',
23213 'tduration' => '1 days',
23214 'tenddate' => '2025-08-14T00:00',
23215 'trip_package' => 'Camp Trek',
23216 ],
23217 ];
23218
23219 $sample_data = [
23220 'booking_data' => $booking_data,
23221 'booking_meta' => $booking_meta,
23222 ];
23223
23224 if ( $booking_status ) {
23225 $sample_data['booking_status'] = $booking_status;
23226 }
23227
23228 if ( ! empty( $payment_data ) ) {
23229 $sample_data = array_merge( $sample_data, $payment_data );
23230 }
23231
23232 return $sample_data;
23233 };
23234
23235 // Sample data based on term.
23236 if ( in_array( $term, [ 'booking_created', 'booking_confirmed', 'booking_cancelled' ] ) ) {
23237 $sample_customer = [
23238 'fname' => 'John',
23239 'lname' => 'Doe',
23240 'email' => 'john.doe@example.com',
23241 'address' => '123 Main St',
23242 'city' => 'New York',
23243 'country' => 'US',
23244 ];
23245 $sample_trip = [
23246 'travelers' => '2',
23247 'cost' => '800',
23248 'id' => '101',
23249 'name' => 'Sample Trip to Nepal',
23250 'date' => '2025-03-15',
23251 ];
23252
23253 $booking_status = '';
23254
23255 // Set correct booking status based on trigger type.
23256 switch ( $term ) {
23257 case 'booking_confirmed':
23258 $booking_status = 'booked';
23259 break;
23260 case 'booking_cancelled':
23261 $booking_status = 'cancelled';
23262 break;
23263 }
23264
23265 // For booking_created, include payment data as well.
23266 if ( 'booking_created' === $term ) {
23267 $payment_data = [
23268 'payment_status' => 'check-waiting',
23269 'payment_gateway' => 'Check Payment',
23270 'payment_details' => '',
23271 'coupon_details' => [
23272 [
23273 'coupon_code' => 'SAVE20',
23274 'discount_type' => 'percentage',
23275 'discount_value' => 20,
23276 ],
23277 ],
23278 ];
23279 $context['pluggable_data'] = $get_booking_sample_data( $sample_customer, $sample_trip, '', $payment_data );
23280 } else {
23281 $context['pluggable_data'] = $get_booking_sample_data( $sample_customer, $sample_trip, $booking_status );
23282 }
23283 } else {
23284 switch ( $term ) {
23285
23286 default:
23287 $context['pluggable_data'] = [
23288 'enquiry_post_id' => 123,
23289 'post_data' => [
23290 'ID' => 123,
23291 'post_title' => 'Sample Enquiry',
23292 'post_name' => 'sample-enquiry',
23293 'post_date' => '2025-01-01 10:00:00',
23294 'post_status' => 'publish',
23295 'post_type' => 'enquiry',
23296 'post_author' => 1,
23297 'post_content' => '',
23298 'post_excerpt' => '',
23299 'comment_status' => 'closed',
23300 'ping_status' => 'closed',
23301 'post_modified' => '2025-01-01 10:00:00',
23302 'guid' => 'http://example.com/enquiry/sample-enquiry',
23303 ],
23304 ];
23305 break;
23306 }
23307 $context['response_type'] = 'sample';
23308 }
23309 }
23310 return (array) $context;
23311 }
23312
23313 /**
23314 * Prepare WP Travel Engine Trips List.
23315 *
23316 * @param array $data Search Params.
23317 * @return array
23318 */
23319 public function search_wte_trips_list( $data ) {
23320 $page = isset( $data['page'] ) ? max( 1, intval( $data['page'] ) ) : 1;
23321 $limit = Utilities::get_search_page_limit();
23322 $offset = $limit * ( $page - 1 );
23323
23324 $query_args = [
23325 'post_type' => 'trip',
23326 'post_status' => 'publish',
23327 'posts_per_page' => $limit,
23328 'offset' => $offset,
23329 ];
23330
23331 $query = new \WP_Query( $query_args );
23332 $result = [];
23333
23334 foreach ( $query->posts as $post ) {
23335 if ( $post instanceof \WP_Post ) {
23336 $result[] = [
23337 'label' => $post->post_title,
23338 'value' => $post->ID,
23339 ];
23340 }
23341 }
23342
23343 $total_query = new \WP_Query(
23344 [
23345 'post_type' => 'trip',
23346 'post_status' => 'publish',
23347 'posts_per_page' => -1,
23348 'fields' => 'ids',
23349 ]
23350 );
23351 $posts_count = count( $total_query->posts );
23352
23353 return [
23354 'options' => $result,
23355 'hasMore' => $posts_count > ( $offset + $limit ),
23356 ];
23357 }
23358 /**
23359 * Get GeoDirectory trigger last data.
23360 *
23361 * @param array $data data.
23362 * @return array|mixed
23363 */
23364 public function search_geodir_triggers_last_data( $data ) {
23365 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
23366 $context = [];
23367
23368 if ( function_exists( 'geodir_get_post_info' ) ) {
23369 $args = [
23370 'post_type' => 'gd_place',
23371 'posts_per_page' => 1,
23372 'orderby' => 'date',
23373 'order' => 'DESC',
23374 ];
23375
23376 $posts = get_posts( $args );
23377
23378 if ( ! empty( $posts ) ) {
23379 $post = $posts[0];
23380 $post_id = $post->ID;
23381 $user = WordPress::get_user_context( absint( $post->post_author ) );
23382 $gd_post = function_exists( 'geodir_get_post_info' ) ? geodir_get_post_info( $post_id ) : null;
23383
23384 $categories = [];
23385 $cat_taxonomy = $post->post_type . 'category';
23386 if ( taxonomy_exists( $cat_taxonomy ) ) {
23387 $terms = wp_get_post_terms( $post_id, $cat_taxonomy );
23388 if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
23389 foreach ( $terms as $term_obj ) {
23390 $categories[] = $term_obj->name;
23391 }
23392 }
23393 }
23394
23395 $tags = [];
23396 $tag_taxonomy = $post->post_type . '_tags';
23397 if ( taxonomy_exists( $tag_taxonomy ) ) {
23398 $terms = wp_get_post_terms( $post_id, $tag_taxonomy );
23399 if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
23400 foreach ( $terms as $term_obj ) {
23401 $tags[] = $term_obj->name;
23402 }
23403 }
23404 }
23405
23406 $attachments = [];
23407 if ( function_exists( 'geodir_get_images' ) ) {
23408 $images = geodir_get_images( $post_id );
23409 if ( ! empty( $images ) ) {
23410 foreach ( $images as $image ) {
23411 if ( ! empty( $image->file ) ) {
23412 $attachments[] = $image->file;
23413 }
23414 }
23415 }
23416 }
23417
23418 $address = '';
23419 $country = '';
23420 $region = '';
23421 $city = '';
23422 $postal_code = '';
23423 $latitude = '';
23424 $longitude = '';
23425
23426 if ( ! empty( $gd_post ) ) {
23427 if ( isset( $gd_post->street ) ) {
23428 $address = $gd_post->street;
23429 }
23430 if ( isset( $gd_post->country ) ) {
23431 $country = $gd_post->country;
23432 }
23433 if ( isset( $gd_post->region ) ) {
23434 $region = $gd_post->region;
23435 }
23436 if ( isset( $gd_post->city ) ) {
23437 $city = $gd_post->city;
23438 }
23439 if ( isset( $gd_post->zip ) ) {
23440 $postal_code = $gd_post->zip;
23441 }
23442 if ( isset( $gd_post->latitude ) ) {
23443 $latitude = $gd_post->latitude;
23444 }
23445 if ( isset( $gd_post->longitude ) ) {
23446 $longitude = $gd_post->longitude;
23447 }
23448 }
23449
23450 $base_data = [
23451 'listing_id' => $post->ID,
23452 'listing_title' => $post->post_title,
23453 'listing_type' => $post->post_type,
23454 'listing_url' => get_permalink( $post->ID ),
23455 'description' => $post->post_content,
23456 'categories' => $categories,
23457 'tags' => $tags,
23458 'attachments' => $attachments,
23459 'address' => $address,
23460 'country' => $country,
23461 'region' => $region,
23462 'city' => $city,
23463 'postal_code' => $postal_code,
23464 'latitude' => $latitude,
23465 'longitude' => $longitude,
23466 ];
23467
23468 switch ( $term ) {
23469 case 'geodir_claimed_listing_added':
23470 case 'geodir_claim_approved':
23471 case 'geodir_claim_rejected':
23472 $claim = [];
23473 $claim_status_text = 'unknown';
23474
23475 if ( 'geodir_claimed_listing_added' === $term ) {
23476 $claim_status_text = 'pending';
23477 } elseif ( 'geodir_claim_approved' === $term ) {
23478 $claim_status_text = 'approved';
23479 } elseif ( 'geodir_claim_rejected' === $term ) {
23480 $claim_status_text = 'rejected';
23481 }
23482
23483 global $wpdb;
23484 $claim_table = $wpdb->prefix . 'geodir_claim';
23485
23486 $claim_id = $wpdb->get_var(
23487 $wpdb->prepare(
23488 "SELECT id FROM {$wpdb->prefix}geodir_claim WHERE post_id = %d ORDER BY id DESC LIMIT 1",
23489 $post_id
23490 )
23491 );
23492
23493 if ( $claim_id && class_exists( '\GeoDir_Claim_Post' ) && is_callable( [ '\GeoDir_Claim_Post', 'get_item' ] ) ) {
23494 $claim = \GeoDir_Claim_Post::get_item( $claim_id );
23495 }
23496
23497 $context['pluggable_data'] = array_merge(
23498 $base_data,
23499 [
23500 'claim' => array_merge(
23501 (array) $claim,
23502 [
23503 'status' => $claim_status_text,
23504 ]
23505 ),
23506 ],
23507 $user
23508 );
23509 break;
23510 case 'geodir_listing_upgraded':
23511 $context['pluggable_data'] = array_merge(
23512 $base_data,
23513 [
23514 'package_id' => 789,
23515 'package_name' => 'Premium Package',
23516 'package_amount' => '99.99',
23517 'package_days' => '365',
23518 ],
23519 $user
23520 );
23521 break;
23522 default:
23523 $context['pluggable_data'] = array_merge( $base_data, $user );
23524 break;
23525 }
23526
23527 $context['response_type'] = 'live';
23528 return $context;
23529 }
23530 }
23531
23532 $sample_data = [
23533 'listing_id' => 123,
23534 'listing_title' => 'Sample Business Listing',
23535 'listing_type' => 'gd_place',
23536 'listing_url' => site_url( '/places/sample-business-listing/' ),
23537 'description' => 'This is a sample business listing description.',
23538 'categories' => [ 'Restaurant', 'Cafe' ],
23539 'tags' => [ 'Food', 'Coffee', 'Breakfast' ],
23540 'attachments' => [ site_url( '/wp-content/uploads/sample-image.jpg' ) ],
23541 'address' => '123 Main St',
23542 'country' => 'United States',
23543 'region' => 'California',
23544 'city' => 'San Francisco',
23545 'postal_code' => '94105',
23546 'latitude' => '37.7749',
23547 'longitude' => '-122.4194',
23548 'wp_user_id' => 10,
23549 'user_login' => 'sampleuser',
23550 'display_name' => 'Sample User',
23551 'user_firstname' => 'Sample',
23552 'user_lastname' => 'User',
23553 'user_email' => 'sample@example.com',
23554 'user_role' => [ 'subscriber' ],
23555 ];
23556
23557 switch ( $term ) {
23558 case 'geodir_claim_approved':
23559 case 'geodir_claim_rejected':
23560 case 'geodir_claimed_listing_added':
23561 $sample_data['claim'] = [
23562 'id' => 14,
23563 'post_id' => 123,
23564 'post_type' => 'gd_place',
23565 'author_id' => 10,
23566 'user_id' => 10,
23567 'user_fullname' => 'Sample User',
23568 'user_number' => '+1234567890',
23569 'user_position' => 'Business Owner',
23570 'user_ip' => '127.0.0.1',
23571 'user_comments' => 'Hi, I am the owner of this business and I would like to claim it.',
23572 'admin_comments' => '',
23573 'claim_date' => current_time( 'mysql' ),
23574 'status' => 'pending',
23575 'rand_string' => 'sampleclaimstring123',
23576 'payment_id' => 0,
23577 'old_package_id' => 0,
23578 'package_id' => 0,
23579 'meta' => [
23580 'ninja_form_id' => '2',
23581 'ninja_sub_id' => '',
23582 'ninja_post' => 123,
23583 'extra_fields' => [
23584 'user_id' => [
23585 'label' => 'User ID',
23586 'value' => '10',
23587 ],
23588 ],
23589 ],
23590 ];
23591 break;
23592 case 'geodir_listing_upgraded':
23593 $sample_data['package_id'] = 789;
23594 $sample_data['package_name'] = 'Premium Package';
23595 $sample_data['package_amount'] = '99.99';
23596 $sample_data['package_days'] = '365';
23597 break;
23598 }
23599
23600 $context['pluggable_data'] = $sample_data;
23601 $context['response_type'] = 'sample';
23602 return $context;
23603 }
23604
23605 /**
23606 * Get WP Subscription triggers last data.
23607 *
23608 * @param array $data data.
23609 *
23610 * @return array
23611 */
23612 public function search_wp_subscription_triggers_last_data( $data ) {
23613 global $wpdb;
23614
23615 $context = [];
23616 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
23617
23618 $needs_subscription = in_array(
23619 $term,
23620 [
23621 'subscription_activated',
23622 'subscription_cancelled',
23623 'subscription_expired',
23624 'subscription_status_changed',
23625 'subscription_pending',
23626 'subscription_pending_cancellation',
23627 ],
23628 true
23629 );
23630
23631 if ( ! $needs_subscription ) {
23632 return $context;
23633 }
23634
23635 $subscription = $wpdb->get_row(
23636 "SELECT * FROM {$wpdb->prefix}posts WHERE post_type = 'subscrpt_order' ORDER BY post_modified DESC LIMIT 1"
23637 );
23638
23639 $sample_subscription = new \stdClass();
23640 $sample_subscription->ID = 100;
23641 $sample_subscription->post_author = 2;
23642 $sample_subscription->post_date = '2025-07-07 09:51:52';
23643 $sample_subscription->post_date_gmt = '2025-07-07 09:51:52';
23644 $sample_subscription->post_content = '';
23645 $sample_subscription->post_title = 'Subscription #100';
23646 $sample_subscription->post_excerpt = '';
23647 $sample_subscription->post_status = 'active';
23648 $sample_subscription->comment_status = 'closed';
23649 $sample_subscription->ping_status = 'closed';
23650 $sample_subscription->post_password = '';
23651 $sample_subscription->post_name = 'subscription-100';
23652 $sample_subscription->to_ping = '';
23653 $sample_subscription->pinged = '';
23654 $sample_subscription->post_modified = '2025-07-07 10:15:16';
23655 $sample_subscription->post_modified_gmt = '2025-07-07 10:15:16';
23656 $sample_subscription->post_content_filtered = '';
23657 $sample_subscription->post_parent = 0;
23658 $sample_subscription->guid = 'http://example.com/subscrpt_order/';
23659 $sample_subscription->menu_order = 0;
23660 $sample_subscription->post_type = 'subscrpt_order';
23661 $sample_subscription->post_mime_type = '';
23662 $sample_subscription->comment_count = 4;
23663 $sample_subscription->filter = 'raw';
23664
23665 $sub = $subscription ? $subscription : $sample_subscription;
23666 $type = $subscription ? 'live' : 'sample';
23667
23668 $pluggable_data = [
23669 'subscription_id' => $sub->ID,
23670 'subscription' => $sub,
23671 'user_id' => $sub->post_author,
23672 ];
23673
23674 if ( 'subscription_status_changed' === $term ) {
23675 $pluggable_data['old_status'] = 'pending';
23676 $pluggable_data['new_status'] = $sub->post_status;
23677 }
23678
23679 $context['pluggable_data'] = $pluggable_data;
23680 $context['response_type'] = $type;
23681
23682 return $context;
23683 }
23684 /**
23685 * Prepare clickwhale categories.
23686 *
23687 * @param array $data Search Params.
23688 *
23689 * @return array
23690 */
23691 public function search_clickwhale_categories( $data ) {
23692
23693 $options = [];
23694 global $wpdb;
23695
23696 $categories = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}clickwhale_categories ORDER BY id DESC", ARRAY_A );
23697
23698 if ( ! empty( $categories ) ) {
23699 foreach ( $categories as $category ) {
23700 $options[] = [
23701 'label' => $category['title'],
23702 'value' => $category['id'],
23703 ];
23704 }
23705 }
23706
23707 return [
23708 'options' => $options,
23709 'hasMore' => false,
23710 ];
23711 }
23712
23713 /**
23714 * Get ClickWhale Last Data
23715 *
23716 * @param array $data data.
23717 *
23718 * @return array
23719 */
23720 public function search_clickwhale_triggers_last_data( $data ) {
23721 global $wpdb;
23722
23723 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
23724 $context = [];
23725
23726 $latest_link = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}clickwhale_links ORDER BY id DESC LIMIT 1", ARRAY_A );
23727
23728 $get_author_data = function( $author_id ) {
23729 if ( class_exists( '\SureTriggers\Integrations\WordPress\WordPress' ) ) {
23730 return WordPress::get_user_context( $author_id );
23731 }
23732 return [];
23733 };
23734
23735 $format_link = function( $link_data ) use ( $get_author_data ) {
23736 $author_id = isset( $link_data['author'] ) ? $link_data['author'] : 0;
23737
23738 return [
23739 'id' => isset( $link_data['id'] ) ? $link_data['id'] : 17,
23740 'title' => isset( $link_data['title'] ) ? $link_data['title'] : 'Sample Title',
23741 'slug' => isset( $link_data['slug'] ) ? $link_data['slug'] : 'sample-slug',
23742 'url' => isset( $link_data['url'] ) ? $link_data['url'] : 'https://example.com',
23743 'redirection' => isset( $link_data['redirection'] ) ? $link_data['redirection'] : '301',
23744 'link_target' => isset( $link_data['link_target'] ) ? $link_data['link_target'] : '',
23745 'nofollow' => isset( $link_data['nofollow'] ) ? $link_data['nofollow'] : '1',
23746 'sponsored' => isset( $link_data['sponsored'] ) ? $link_data['sponsored'] : '0',
23747 'description' => isset( $link_data['description'] ) ? $link_data['description'] : '',
23748 'categories' => isset( $link_data['categories'] ) ? $link_data['categories'] : '',
23749 'author_id' => $author_id,
23750 'author' => $get_author_data( $author_id ),
23751 'created_at' => isset( $link_data['created_at'] ) ? $link_data['created_at'] : current_time( 'mysql' ),
23752 'updated_at' => isset( $link_data['updated_at'] ) ? $link_data['updated_at'] : current_time( 'mysql' ),
23753 ];
23754 };
23755
23756 switch ( $term ) {
23757 case 'link_created':
23758 case 'link_updated':
23759 $link_data = ! empty( $latest_link ) ? $format_link( $latest_link ) : $format_link( [] );
23760 $context['pluggable_data'] = [ 'link' => $link_data ];
23761 $context['response_type'] = ! empty( $latest_link ) ? 'live' : 'sample';
23762 break;
23763
23764 case 'link_deleted':
23765 if ( ! empty( $latest_link ) ) {
23766 $context['pluggable_data'] = [
23767 'link_id' => $latest_link['id'],
23768 'deleted_at' => current_time( 'mysql' ),
23769 ];
23770 $context['response_type'] = 'live';
23771 } else {
23772 $context['pluggable_data'] = [
23773 'link_id' => 17,
23774 'deleted_at' => current_time( 'mysql' ),
23775 ];
23776 $context['response_type'] = 'sample';
23777 }
23778 break;
23779
23780 case 'link_clicked':
23781 $link_data = ! empty( $latest_link ) ? $format_link( $latest_link ) : $format_link( [] );
23782
23783 $context['pluggable_data'] = [
23784 'link' => $link_data,
23785 ];
23786 $context['response_type'] = ! empty( $latest_link ) ? 'live' : 'sample';
23787 break;
23788 }
23789
23790 return (array) $context;
23791 }
23792
23793 /**
23794 * Get Pretty Links Last Data
23795 *
23796 * @param array $data data.
23797 *
23798 * @return array
23799 */
23800 public function search_pretty_links_triggers_last_data( $data ) {
23801 global $wpdb;
23802
23803 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
23804 $context = [];
23805
23806 // Check if Pretty Links plugin is active.
23807 if ( ! defined( 'PRLI_VERSION' ) ) {
23808 return $context;
23809 }
23810
23811 // Get latest pretty link.
23812 $latest_link = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}prli_links ORDER BY id DESC LIMIT 1", ARRAY_A );
23813
23814 // Get latest click if available.
23815 $latest_click = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}prli_clicks ORDER BY id DESC LIMIT 1", ARRAY_A );
23816
23817 $get_user_data = function( $user_id ) {
23818 if ( class_exists( '\SureTriggers\Integrations\WordPress\WordPress' ) ) {
23819 return WordPress::get_user_context( $user_id );
23820 }
23821 return [
23822 'user_id' => 0,
23823 'user_login' => 'guest',
23824 'display_name' => 'Guest User',
23825 'user_firstname' => '',
23826 'user_lastname' => '',
23827 'user_email' => 'guest@example.com',
23828 'user_role' => [],
23829 ];
23830 };
23831 $format_link = function( $link_data ) {
23832 return [
23833 'id' => isset( $link_data['id'] ) ? $link_data['id'] : 17,
23834 'name' => isset( $link_data['name'] ) ? $link_data['name'] : 'Sample Pretty Link',
23835 'url' => isset( $link_data['url'] ) ? $link_data['url'] : 'https://example.com',
23836 'slug' => isset( $link_data['slug'] ) ? $link_data['slug'] : 'sample-link',
23837 'pretty_url' => home_url( '/' . ( isset( $link_data['slug'] ) ? $link_data['slug'] : 'sample-link' ) ),
23838 'description' => isset( $link_data['description'] ) ? $link_data['description'] : 'Sample description',
23839 'redirect_type' => isset( $link_data['redirect_type'] ) ? $link_data['redirect_type'] : '307',
23840 'clicks' => isset( $link_data['clicks'] ) ? $link_data['clicks'] : '5',
23841 'created_at' => isset( $link_data['created_at'] ) ? $link_data['created_at'] : current_time( 'mysql' ),
23842 ];
23843 };
23844
23845 $format_click = function( $click_data ) {
23846 return [
23847 'click_id' => isset( $click_data['id'] ) ? $click_data['id'] : 42,
23848 'url' => isset( $click_data['url'] ) ? $click_data['url'] : 'https://example.com',
23849 'timestamp' => isset( $click_data['created_at'] ) ? $click_data['created_at'] : current_time( 'mysql' ),
23850 'is_logged_in' => true,
23851 ];
23852 };
23853
23854 switch ( $term ) {
23855 case 'link_clicked':
23856 $link_data = ! empty( $latest_link ) ? $format_link( $latest_link ) : $format_link( [] );
23857 $click_data = ! empty( $latest_click ) ? $format_click( $latest_click ) : $format_click( [] );
23858
23859 // Get user context from latest click or sample data.
23860 $user_id = 0;
23861 if ( ! empty( $latest_click ) ) {
23862 if ( isset( $latest_click['user'] ) && $latest_click['user'] > 0 ) {
23863 $user_id = $latest_click['user'];
23864 } elseif ( isset( $latest_click['user_id'] ) && $latest_click['user_id'] > 0 ) {
23865 $user_id = $latest_click['user_id'];
23866 } elseif ( isset( $latest_click['visitor'] ) && is_numeric( $latest_click['visitor'] ) && $latest_click['visitor'] > 0 ) {
23867 $user_id = $latest_click['visitor'];
23868 }
23869 }
23870
23871 if ( $user_id > 0 ) {
23872 $user_data = $get_user_data( $user_id );
23873 } else {
23874 $user_data = [
23875 'wp_user_id' => 1,
23876 'user_login' => 'sampleuser',
23877 'display_name' => 'Sample User',
23878 'user_firstname' => 'John',
23879 'user_lastname' => 'Doe',
23880 'user_email' => 'sampleuser@example.com',
23881 'user_registered' => current_time( 'mysql' ),
23882 'user_role' => [ 'subscriber' ],
23883 ];
23884 }
23885
23886 $context['pluggable_data'] = array_merge(
23887 $user_data,
23888 [
23889 'link' => $link_data,
23890 'click' => $click_data,
23891 ]
23892 );
23893 $context['response_type'] = ! empty( $latest_link ) ? 'live' : 'sample';
23894 break;
23895 }
23896
23897 return (array) $context;
23898 }
23899
23900 /**
23901 * Get ProfilePress Payment Methods.
23902 *
23903 * @param array $data data.
23904 *
23905 * @return array|void
23906 */
23907 public function search_profilepress_payment_methods( $data ) {
23908
23909 $options = [];
23910 if ( class_exists( '\ProfilePress\Core\Membership\PaymentMethods\PaymentMethods' ) ) {
23911 $payment_methods = \ProfilePress\Core\Membership\PaymentMethods\PaymentMethods::get_instance()->get_all();
23912
23913 $methods = [ '' => 'Default' ];
23914 if ( ! empty( $payment_methods ) ) {
23915 foreach ( $payment_methods as $method ) {
23916 $methods[ $method->get_id() ] = $method->get_method_title();
23917 }
23918 }
23919
23920 $methods = apply_filters( 'ppress_admin_order_page_enabled_payment_methods', $methods );
23921
23922 if ( $methods ) {
23923 foreach ( $methods as $id => $name ) {
23924 $options[] = [
23925 'label' => $name,
23926 'value' => $id,
23927 ];
23928 }
23929 }
23930 }
23931
23932 return [
23933 'options' => $options,
23934 'hasMore' => false,
23935 ];
23936 }
23937
23938 /**
23939 * Get ProfilePress Order Status List.
23940 *
23941 * @param array $data data.
23942 *
23943 * @return array|void
23944 */
23945 public function search_profilepress_order_status_list( $data ) {
23946
23947 $options = [];
23948
23949 if ( class_exists( 'ProfilePress\Core\Membership\Models\Order\OrderStatus' ) ) {
23950 $statuses = \ProfilePress\Core\Membership\Models\Order\OrderStatus::get_all();
23951 if ( $statuses ) {
23952 foreach ( $statuses as $id => $name ) {
23953 $options[] = [
23954 'label' => $name,
23955 'value' => $id,
23956 ];
23957 }
23958 }
23959 }
23960
23961 return [
23962 'options' => $options,
23963 'hasMore' => false,
23964 ];
23965 }
23966
23967 /**
23968 * Get ProfilePress Last Data
23969 *
23970 * @param array $data data.
23971 *
23972 * @return array
23973 */
23974 public function search_profilepress_triggers_last_data( $data ) {
23975 global $wpdb;
23976
23977 $context = [];
23978
23979 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
23980
23981 switch ( $term ) {
23982
23983 /**
23984 * Customer Updated trigger.
23985 */
23986 case 'profilepress_customer_updated':
23987 if ( class_exists( '\ProfilePress\Core\Membership\Models\Customer\CustomerFactory' ) ) {
23988 $customer = $wpdb->get_row(
23989 "SELECT * FROM {$wpdb->prefix}ppress_customers ORDER BY id DESC LIMIT 1",
23990 ARRAY_A
23991 );
23992
23993 if ( $customer ) {
23994 $customer_id = isset( $customer['id'] ) ? (int) $customer['id'] : 0;
23995 $customer = \ProfilePress\Core\Membership\Models\Customer\CustomerFactory::fromId( absint( $customer_id ) );
23996
23997 $pluggable_data = [
23998 'customer' => Utilities::object_to_array( $customer ),
23999 ];
24000
24001 $context = [
24002 'pluggable_data' => $pluggable_data,
24003 'response_type' => 'live',
24004 ];
24005 } else {
24006
24007 $customer = \ProfilePress\Core\Membership\Models\Customer\CustomerFactory::fromId( absint( 0 ) );
24008 $sample_data = [
24009 'customer' => Utilities::object_to_array( $customer ),
24010 ];
24011
24012 $context = [
24013 'pluggable_data' => $sample_data,
24014 'response_type' => 'sample',
24015 ];
24016 }
24017 }
24018
24019 break;
24020
24021 /**
24022 * Subscription Activated trigger.
24023 */
24024 case 'profilepress_subscription_activated':
24025 $latest_subscription = $wpdb->get_row(
24026 'SELECT * FROM ' . $wpdb->prefix . 'ppress_subscriptions ORDER BY id DESC LIMIT 1',
24027 ARRAY_A
24028 );
24029
24030 if ( $latest_subscription ) {
24031 if ( class_exists( '\ProfilePress\Core\Membership\Models\Subscription\SubscriptionFactory' ) ) {
24032 $latest_subscription = \ProfilePress\Core\Membership\Models\Subscription\SubscriptionFactory::fromId( intval( $latest_subscription['id'] ) );
24033 $context['pluggable_data']['subscription'] = Utilities::object_to_array( $latest_subscription );
24034 $context['response_type'] = 'live';
24035 }
24036 } else {
24037 $sample_subscription = [
24038 'id' => 1,
24039 'parent_order_id' => 1,
24040 'plan_id' => 1,
24041 'customer_id' => 7,
24042 'billing_frequency' => 'monthly',
24043 'initial_amount' => '10.00000000',
24044 'initial_tax_rate' => '0',
24045 'initial_tax' => '0',
24046 'recurring_amount' => '10.00000000',
24047 'recurring_tax_rate' => '0',
24048 'recurring_tax' => '0',
24049 'total_payments' => '0',
24050 'trial_period' => 'disabled',
24051 'profile_id' => '',
24052 'status' => 'active',
24053 'notes' => null,
24054 'created_date' => '2025-08-19 11:45:41',
24055 'expiration_date' => '2025-09-19 11:45:41',
24056 ];
24057 $context['pluggable_data']['subscription'] = $sample_subscription;
24058 $context['response_type'] = 'sample';
24059 }
24060 break;
24061
24062 default:
24063 $context['pluggable_data'] = [];
24064 $context['response_type'] = 'error';
24065 break;
24066
24067 }
24068
24069 return $context;
24070 }
24071
24072 /**
24073 * Search FluentCRM Company Data.
24074 *
24075 * @param array $data data.
24076 * @return array
24077 */
24078 public function search_pluggables_fluentcrm_company_created( $data ) {
24079 global $wpdb;
24080
24081 $context = [];
24082 $pluggable_data = [];
24083 $term = ! empty( $data['search_term'] ) ? $data['search_term'] : '';
24084
24085 if ( ! class_exists( 'FluentCrm\App\Models\Company' ) ) {
24086 return [];
24087 }
24088
24089 $result = null;
24090
24091 // Fetch last record based on term.
24092 switch ( $term ) {
24093 case 'contact_deleted':
24094 $result = $wpdb->get_row( "SELECT id FROM {$wpdb->prefix}fc_subscribers ORDER BY id DESC LIMIT 1", ARRAY_A );
24095 break;
24096
24097 case 'campaign_sent':
24098 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fc_campaigns WHERE status = 'archived' ORDER BY updated_at DESC LIMIT 1", ARRAY_A );
24099 break;
24100
24101 case 'company_updated':
24102 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fc_companies ORDER BY updated_at DESC LIMIT 1", ARRAY_A );
24103 break;
24104
24105 case 'company_deleted':
24106 case 'company_assigned':
24107 case 'company_unassigned':
24108 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fc_companies ORDER BY id DESC LIMIT 1", ARRAY_A );
24109 break;
24110
24111 default:
24112 // Default to company_created.
24113 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fc_companies ORDER BY created_at DESC LIMIT 1", ARRAY_A );
24114 break;
24115 }
24116
24117 if ( $result ) {
24118 switch ( $term ) {
24119 case 'contact_deleted':
24120 $pluggable_data = [
24121 'contact_id' => $result['id'],
24122 'deleted_at' => current_time( 'mysql' ),
24123 ];
24124 break;
24125
24126 case 'campaign_sent':
24127 $campaign_data = $result;
24128
24129 if ( ! empty( $campaign_data['settings'] ) ) {
24130 $settings = maybe_unserialize( $campaign_data['settings'] );
24131 if ( is_array( $settings ) ) {
24132 $campaign_data['settings'] = $settings;
24133 }
24134 }
24135
24136 $pluggable_data = [
24137 'campaign' => $campaign_data,
24138 'status' => 'archived',
24139 'archived_at' => current_time( 'mysql' ),
24140 ];
24141
24142 if ( ! empty( $result['id'] ) ) {
24143 $total_emails = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}fc_campaign_emails WHERE campaign_id = %d", $result['id'] ) );
24144 $sent_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}fc_campaign_emails WHERE campaign_id = %d AND status = 'sent'", $result['id'] ) );
24145 $opened_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}fc_campaign_emails WHERE campaign_id = %d AND is_open > 0", $result['id'] ) );
24146 $clicked_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}fc_campaign_emails WHERE campaign_id = %d AND click_counter > 0", $result['id'] ) );
24147
24148 $pluggable_data['total_emails_sent'] = $total_emails;
24149 $pluggable_data['sent_count'] = $sent_count;
24150 $pluggable_data['opened_count'] = $opened_count;
24151 $pluggable_data['clicked_count'] = $clicked_count;
24152 $pluggable_data['open_rate'] = $sent_count > 0 ? round( ( $opened_count / $sent_count ) * 100, 2 ) : 0;
24153 $pluggable_data['click_rate'] = $sent_count > 0 ? round( ( $clicked_count / $sent_count ) * 100, 2 ) : 0;
24154 }
24155 break;
24156
24157 case 'company_deleted':
24158 $pluggable_data = [
24159 'company_id' => $result['id'],
24160 'deleted_at' => current_time( 'mysql' ),
24161 ];
24162 break;
24163
24164 case 'company_assigned':
24165 case 'company_unassigned':
24166 $contact_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fc_subscribers ORDER BY id DESC LIMIT 1", ARRAY_A );
24167 if ( $contact_result ) {
24168 $pluggable_data['contact'] = [
24169 'details' => $contact_result,
24170 'custom' => [],
24171 ];
24172 $pluggable_data['companies'] = [ $result ];
24173
24174 if ( 'company_assigned' === $term ) {
24175 $pluggable_data['assigned_company_ids'] = [ $result['id'] ];
24176 $pluggable_data['assigned_at'] = current_time( 'mysql' );
24177 } else {
24178 $pluggable_data['unassigned_company_ids'] = [ $result['id'] ];
24179 $pluggable_data['unassigned_at'] = current_time( 'mysql' );
24180 }
24181 }
24182 break;
24183
24184 default:
24185 $meta_data = ! empty( $result['meta'] ) ? maybe_unserialize( $result['meta'] ) : [];
24186 $company_result = array_intersect_key(
24187 $result,
24188 array_flip(
24189 [
24190 'id',
24191 'hash',
24192 'name',
24193 'email',
24194 'description',
24195 'address_line_1',
24196 'address_line_2',
24197 'city',
24198 'state',
24199 'postal_code',
24200 'country',
24201 'phone',
24202 'type',
24203 'owner_id',
24204 'employees_number',
24205 'industry',
24206 'website',
24207 'linkedin_url',
24208 'facebook_url',
24209 'twitter_url',
24210 'logo',
24211 'timezone',
24212 'date_of_start',
24213 'created_at',
24214 'updated_at',
24215 ]
24216 )
24217 );
24218
24219 if ( is_array( $meta_data ) && isset( $meta_data['custom_values'] ) ) {
24220 $company_result['custom_fields'] = $meta_data['custom_values'];
24221 }
24222
24223 $pluggable_data['company'] = $company_result;
24224 break;
24225 }
24226 $context['response_type'] = 'live';
24227 } else {
24228 // Sample data for different terms.
24229 switch ( $term ) {
24230 case 'contact_deleted':
24231 $pluggable_data = [
24232 'contact_id' => 1,
24233 'deleted_at' => current_time( 'mysql' ),
24234 ];
24235 break;
24236
24237 case 'campaign_sent':
24238 $pluggable_data = [
24239 'campaign' => [
24240 'id' => 1,
24241 'type' => 'campaign',
24242 'title' => 'Sample Email Campaign',
24243 'status' => 'archived',
24244 'template_id' => 1,
24245 'email_subject' => 'Welcome to Our Newsletter!',
24246 'email_body' => 'Sample campaign email content...',
24247 'utm_status' => 1,
24248 'utm_source' => 'fluentcrm',
24249 'utm_medium' => 'email',
24250 'utm_campaign' => 'sample-campaign',
24251 'created_by' => 1,
24252 'created_at' => current_time( 'mysql' ),
24253 'updated_at' => current_time( 'mysql' ),
24254 'scheduled_at' => current_time( 'mysql' ),
24255 ],
24256 'status' => 'archived',
24257 'archived_at' => current_time( 'mysql' ),
24258 'total_emails_sent' => 1500,
24259 'sent_count' => 1450,
24260 'opened_count' => 725,
24261 'clicked_count' => 145,
24262 'open_rate' => 50.0,
24263 'click_rate' => 10.0,
24264 ];
24265 break;
24266
24267 case 'company_deleted':
24268 $pluggable_data = [
24269 'company_id' => 1,
24270 'deleted_at' => current_time( 'mysql' ),
24271 ];
24272 break;
24273
24274 case 'company_assigned':
24275 case 'company_unassigned':
24276 $pluggable_data['contact']['details'] = [
24277 'id' => 1,
24278 'user_id' => 1,
24279 'hash' => 'sample_hash',
24280 'contact_owner' => 0,
24281 'company_id' => 0,
24282 'prefix' => 'Mr.',
24283 'first_name' => 'John',
24284 'last_name' => 'Doe',
24285 'email' => 'john@example.com',
24286 'timezone' => 'America/New_York',
24287 'address_line_1' => '123 Main Street',
24288 'address_line_2' => 'Apt 4B',
24289 'postal_code' => '10001',
24290 'city' => 'New York',
24291 'state' => 'NY',
24292 'country' => 'USA',
24293 'phone' => '+1-555-123-4567',
24294 'status' => 'subscribed',
24295 'contact_type' => 'customer',
24296 'source' => 'manual',
24297 'lifecycle_stage' => 'customer',
24298 'created_at' => current_time( 'mysql' ),
24299 'updated_at' => current_time( 'mysql' ),
24300 ];
24301 $pluggable_data['contact']['custom'] = [];
24302 $pluggable_data['companies'] = [
24303 [
24304 'id' => 1,
24305 'hash' => 'sample_hash',
24306 'name' => 'Sample Company Inc.',
24307 'email' => 'contact@samplecompany.com',
24308 'description' => 'This is a sample company',
24309 'address_line_1' => '123 Business Street',
24310 'address_line_2' => 'Suite 100',
24311 'city' => 'Business City',
24312 'state' => 'California',
24313 'postal_code' => '90210',
24314 'country' => 'USA',
24315 'phone' => '+1-555-123-4567',
24316 'type' => 'client',
24317 'created_at' => current_time( 'mysql' ),
24318 'updated_at' => current_time( 'mysql' ),
24319 ],
24320 ];
24321 if ( 'company_assigned' === $term ) {
24322 $pluggable_data['assigned_company_ids'] = [ 1 ];
24323 $pluggable_data['assigned_at'] = current_time( 'mysql' );
24324 } else {
24325 $pluggable_data['unassigned_company_ids'] = [ 1 ];
24326 $pluggable_data['unassigned_at'] = current_time( 'mysql' );
24327 }
24328 break;
24329
24330 case 'company_created':
24331 case 'company_updated':
24332 default:
24333 $pluggable_data['company'] = [
24334 'id' => 1,
24335 'hash' => 'sample_hash',
24336 'name' => 'Sample Company Inc.',
24337 'email' => 'contact@samplecompany.com',
24338 'description' => 'This is a sample company',
24339 'address_line_1' => '123 Business Street',
24340 'address_line_2' => 'Suite 100',
24341 'city' => 'Business City',
24342 'state' => 'California',
24343 'postal_code' => '90210',
24344 'country' => 'USA',
24345 'phone' => '+1-555-123-4567',
24346 'type' => 'client',
24347 'owner_id' => 1,
24348 'employees_number' => 50,
24349 'industry' => 'Technology',
24350 'website' => 'https://samplecompany.com',
24351 'linkedin_url' => 'https://linkedin.com/company/samplecompany',
24352 'facebook_url' => 'https://facebook.com/samplecompany',
24353 'twitter_url' => 'https://twitter.com/samplecompany',
24354 'logo' => '',
24355 'timezone' => 'America/Los_Angeles',
24356 'date_of_start' => '2024-01-01',
24357 'created_at' => '2024-01-01 10:00:00',
24358 'updated_at' => '2024-01-01 10:00:00',
24359 'custom_fields' => [
24360 'annual_revenue' => '$1,000,000',
24361 'primary_contact' => 'John Doe',
24362 ],
24363 ];
24364 break;
24365 }
24366
24367 $context['response_type'] = 'sample';
24368 }
24369
24370 $context['pluggable_data'] = $pluggable_data;
24371 return $context;
24372 }
24373
24374 /**
24375 * Get FluentCRM email trigger details.
24376 *
24377 * @param array $data data.
24378 *
24379 * @return array
24380 */
24381 public function search_pluggables_fluentcrm_email_triggers( $data ) {
24382 $context = [];
24383 $pluggable_data = [];
24384 global $wpdb;
24385 $term = $data['search_term'] ? $data['search_term'] : '';
24386
24387 // Helper function to build email data structure.
24388 $build_email_data = function( $email_result, $subscriber_result = null, $subscriber_status = null ) {
24389 $email_data = [
24390 'id' => $email_result['id'],
24391 'status' => $email_result['status'],
24392 'is_open' => $email_result['is_open'],
24393 'is_parsed' => $email_result['is_parsed'],
24394 'created_at' => $email_result['created_at'],
24395 'updated_at' => isset( $email_result['updated_at'] ) ? $email_result['updated_at'] : $email_result['created_at'],
24396 'campaign_id' => isset( $email_result['campaign_id'] ) ? $email_result['campaign_id'] : null,
24397 'scheduled_at' => isset( $email_result['scheduled_at'] ) ? $email_result['scheduled_at'] : $email_result['created_at'],
24398 'email_address' => isset( $email_result['email_address'] ) ? $email_result['email_address'] : ( isset( $subscriber_result['email'] ) ? $subscriber_result['email'] : '' ),
24399 'email_headers' => isset( $email_result['email_headers'] ) ? $email_result['email_headers'] : '',
24400 'email_subject' => isset( $email_result['email_subject'] ) ? $email_result['email_subject'] : '',
24401 'email_body' => $email_result['email_body'],
24402 'email_hash' => $email_result['email_hash'],
24403 'email_type' => $email_result['email_type'],
24404 'subscriber_id' => $email_result['subscriber_id'],
24405 ];
24406
24407 if ( $subscriber_result ) {
24408 $subscriber_fields = [
24409 'hash',
24410 'email',
24411 'phone',
24412 'photo',
24413 'prefix',
24414 'status',
24415 'full_name',
24416 'last_name',
24417 'created_at',
24418 'first_name',
24419 'updated_at',
24420 ];
24421 foreach ( $subscriber_fields as $field ) {
24422 $email_data[ 'subscriber_' . $field ] = $subscriber_result[ $field ];
24423 }
24424 if ( $subscriber_status ) {
24425 $email_data['subscriber_status'] = $subscriber_status;
24426 }
24427 }
24428
24429 return $email_data;
24430 };
24431
24432 // Helper function to get sample email data.
24433 $get_sample_email_data = function( $status = 'subscribed' ) {
24434 return [
24435 'id' => 1,
24436 'status' => 'sent',
24437 'is_open' => 0,
24438 'is_parsed' => 1,
24439 'created_at' => current_time( 'mysql' ),
24440 'updated_at' => current_time( 'mysql' ),
24441 'campaign_id' => 83,
24442 'scheduled_at' => current_time( 'mysql' ),
24443 'email_address' => 'john@example.com',
24444 'email_headers' => 'From: noreply@example.com',
24445 'email_subject' => 'Sample Email Subject',
24446 'email_body' => 'Sample email content here...',
24447 'email_hash' => 'sample-email-hash-123',
24448 'email_type' => 'campaign',
24449 'subscriber_id' => 1,
24450 'subscriber_hash' => 'sample_hash',
24451 'subscriber_email' => 'john@example.com',
24452 'subscriber_phone' => '9876654423',
24453 'subscriber_photo' => 'http://example.com/avatar.png',
24454 'subscriber_prefix' => 'Mr.',
24455 'subscriber_status' => $status,
24456 'subscriber_full_name' => 'John Doe',
24457 'subscriber_last_name' => 'Doe',
24458 'subscriber_created_at' => current_time( 'mysql' ),
24459 'subscriber_first_name' => 'John',
24460 'subscriber_updated_at' => current_time( 'mysql' ),
24461 ];
24462 };
24463
24464 // Get result based on term.
24465 $result = null;
24466 if ( 'email_opened' === $term ) {
24467 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fc_campaign_emails ORDER BY id DESC LIMIT %d", 1 ), ARRAY_A );
24468 } elseif ( 'email_clicked' === $term ) {
24469 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fc_campaign_emails WHERE click_counter > %d ORDER BY id DESC LIMIT %d", 0, 1 ), ARRAY_A );
24470 } elseif ( 'email_unsubscribed' === $term ) {
24471 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fc_subscribers WHERE status = 'unsubscribed' ORDER BY updated_at DESC LIMIT %d", 1 ), ARRAY_A );
24472 } elseif ( 'email_bounced' === $term ) {
24473 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fc_subscribers WHERE status = 'bounced' ORDER BY updated_at DESC LIMIT %d", 1 ), ARRAY_A );
24474 }
24475
24476 if ( $result ) {
24477 if ( 'email_opened' === $term || 'email_clicked' === $term ) {
24478 // Get subscriber details.
24479 $subscriber_result = null;
24480 if ( ! empty( $result['subscriber_id'] ) ) {
24481 $subscriber_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fc_subscribers WHERE id = %d", $result['subscriber_id'] ), ARRAY_A );
24482 }
24483
24484 $pluggable_data['email'] = $build_email_data( $result, $subscriber_result );
24485 $pluggable_data[ 'email_opened' === $term ? 'opened_at' : 'clicked_at' ] = current_time( 'mysql' );
24486
24487 } elseif ( 'email_unsubscribed' === $term ) {
24488 // Get email data for subscriber.
24489 $email_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fc_campaign_emails WHERE subscriber_id = %d ORDER BY id DESC LIMIT %d", $result['id'], 1 ), ARRAY_A );
24490
24491 if ( $email_result ) {
24492 $pluggable_data['email'] = $build_email_data( $email_result, $result );
24493 }
24494
24495 $pluggable_data['source'] = 'from_header';
24496 $pluggable_data['unsubscribed_at'] = current_time( 'mysql' );
24497 } elseif ( 'email_bounced' === $term ) {
24498 // For bounced emails, we only need subscriber object and bounce-specific fields.
24499 $subscriber_data = [];
24500 $subscriber_fields = [
24501 'id',
24502 'hash',
24503 'email',
24504 'phone',
24505 'photo',
24506 'prefix',
24507 'status',
24508 'full_name',
24509 'last_name',
24510 'created_at',
24511 'first_name',
24512 'updated_at',
24513 'contact_type',
24514 'total_points',
24515 'last_activity',
24516 'life_time_value',
24517 'ip',
24518 ];
24519
24520 foreach ( $subscriber_fields as $field ) {
24521 if ( isset( $result[ $field ] ) ) {
24522 $subscriber_data[ $field ] = $result[ $field ];
24523 }
24524 }
24525
24526 $pluggable_data['subscriber'] = $subscriber_data;
24527
24528 // Add bounce-specific data.
24529 $pluggable_data['old_status'] = 'subscribed';
24530 $pluggable_data['new_status'] = 'bounced';
24531 $pluggable_data['bounced_at'] = $result['updated_at'];
24532 }
24533 $context['response_type'] = 'live';
24534 } else {
24535 // Sample data for different email terms.
24536 if ( 'email_opened' === $term || 'email_clicked' === $term ) {
24537 $pluggable_data['email'] = $get_sample_email_data();
24538
24539 if ( 'email_opened' === $term ) {
24540 $pluggable_data['opened_at'] = current_time( 'mysql' );
24541 } else {
24542 $pluggable_data['url'] = [
24543 'id' => 1,
24544 'url' => 'https://example.com/sample-link',
24545 'short_url' => 'https://example.com/r/abc123',
24546 'title' => 'Sample Link',
24547 'created_at' => current_time( 'mysql' ),
24548 ];
24549 $pluggable_data['clicked_at'] = current_time( 'mysql' );
24550 }
24551 } elseif ( 'email_unsubscribed' === $term ) {
24552 $pluggable_data['email'] = $get_sample_email_data( 'unsubscribed' );
24553 $pluggable_data['source'] = 'from_header';
24554 $pluggable_data['unsubscribed_at'] = current_time( 'mysql' );
24555 } elseif ( 'email_bounced' === $term ) {
24556 // Sample subscriber object for bounced email (no email object).
24557 $pluggable_data['subscriber'] = [
24558 'id' => 8,
24559 'ip' => '::1',
24560 'hash' => '6b2c0a83138a7e42ef12e6f87e8c58aa',
24561 'email' => 'mrunalis@bsf.io',
24562 'phone' => '9876654423',
24563 'photo' => 'http://ottokit.local/wp-content/plugins/fluent-crm/assets/images/avatar.png',
24564 'prefix' => 'Ms',
24565 'status' => 'bounced',
24566 'full_name' => 'Mrunali Salunke',
24567 'last_name' => 'Salunke',
24568 'created_at' => '2025-09-18 04:51:26',
24569 'first_name' => 'Mrunali',
24570 'updated_at' => current_time( 'mysql' ),
24571 'contact_type' => 'lead',
24572 'total_points' => 0,
24573 'last_activity' => '2025-09-25 05:47:45',
24574 'life_time_value' => 0,
24575 ];
24576 $pluggable_data['old_status'] = 'subscribed';
24577 $pluggable_data['new_status'] = 'bounced';
24578 $pluggable_data['bounced_at'] = current_time( 'mysql' );
24579 }
24580 $context['response_type'] = 'sample';
24581 }
24582
24583 $context['pluggable_data'] = $pluggable_data;
24584 return $context;
24585 }
24586
24587 /**
24588 * Get WooCommerce Subscription triggers last data.
24589 *
24590 * @param array $data data.
24591 *
24592 * @return array
24593 */
24594 public function search_woocommerce_subscription_triggers_last_data( $data ) {
24595 $context = [];
24596 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
24597
24598 if ( ! class_exists( 'WC_Subscription' ) || ! function_exists( 'wcs_get_subscription' ) ) {
24599 return $context;
24600 }
24601
24602 $needs_subscription = in_array(
24603 $term,
24604 [
24605 'wc_subscription_updated',
24606 'wc_cancels_subscription_product',
24607 'wc_subscribes_product',
24608 'wc_purchases_variable_subscription',
24609 'wc_renews_subscription_product',
24610 'wc_subscription_product_expires',
24611 ],
24612 true
24613 );
24614
24615 if ( ! $needs_subscription ) {
24616 return $context;
24617 }
24618
24619 $subscription = null;
24620 $subscription_obj = null;
24621
24622 if ( function_exists( 'wcs_get_subscriptions' ) ) {
24623 $subscriptions = wcs_get_subscriptions(
24624 [
24625 'subscriptions_per_page' => 1,
24626 'orderby' => 'date',
24627 'order' => 'DESC',
24628 ]
24629 );
24630
24631 if ( ! empty( $subscriptions ) ) {
24632 $subscription_obj = reset( $subscriptions );
24633 $subscription = (object) [ 'ID' => $subscription_obj->get_id() ];
24634 }
24635 }
24636
24637 if ( $subscription && ! $subscription_obj ) {
24638 $subscription_obj = wcs_get_subscription( $subscription->ID );
24639 }
24640
24641 if ( $subscription_obj ) {
24642 $user_id = $subscription_obj->get_user_id();
24643 $items = $subscription_obj->get_items();
24644 $product_id = 0;
24645 $variation_id = 0;
24646 $product_name = '';
24647 $variation_name = '';
24648
24649 foreach ( $items as $item ) {
24650 $product_id = $item->get_product_id();
24651 $variation_id = $item->get_variation_id();
24652 $product_name = $item->get_name();
24653 $variation_name = $item->get_name();
24654 break;
24655 }
24656
24657 $user_billing_data = [
24658 'billing_first_name' => $subscription_obj->get_billing_first_name(),
24659 'billing_last_name' => $subscription_obj->get_billing_last_name(),
24660 'billing_country' => $subscription_obj->get_billing_country(),
24661 'billing_address_1' => $subscription_obj->get_billing_address_1(),
24662 'billing_address_2' => $subscription_obj->get_billing_address_2(),
24663 'billing_city' => $subscription_obj->get_billing_city(),
24664 'billing_state' => $subscription_obj->get_billing_state(),
24665 'billing_postcode' => $subscription_obj->get_billing_postcode(),
24666 'billing_phone' => $subscription_obj->get_billing_phone(),
24667 'billing_email' => $subscription_obj->get_billing_email(),
24668 ];
24669
24670 if ( 'wc_subscription_updated' === $term ) {
24671 $subscription_data = [
24672 'start_date' => $subscription_obj->get_date_created(),
24673 'next_payment_date' => $subscription_obj->get_date( 'next_payment' ) ? $subscription_obj->get_date( 'next_payment' ) : 0,
24674 ];
24675
24676 $context['variation_id'] = $variation_id;
24677 $context['product_id'] = $product_id;
24678 $context['subscription'] = $product_id;
24679 $context['variation_name'] = $variation_name;
24680 $context['product_name'] = $product_name;
24681 $context['subscription_data'] = $subscription_data;
24682 $context['subscription_id'] = $subscription_obj->get_id();
24683 $context['user'] = WordPress::get_user_context( $user_id );
24684 $context['user_billing_data'] = $user_billing_data;
24685 $context['new_status'] = $subscription_obj->get_status();
24686 $context['old_status'] = 'pending-cancel';
24687 $context['status'] = 'wc-' . $subscription_obj->get_status();
24688 } elseif ( 'wc_cancels_subscription_product' === $term ) {
24689 $subscription_data = [
24690 'status' => $subscription_obj->get_status(),
24691 'start_date' => $subscription_obj->get_date_created(),
24692 ];
24693
24694 $context['subscription_data'] = $subscription_data;
24695 $context['user'] = WordPress::get_user_context( $user_id );
24696 $context['user_billing_data'] = $user_billing_data;
24697 $context['subscription_id'] = $subscription_obj->get_id();
24698 } elseif ( 'wc_subscribes_product' === $term ) {
24699 $subscription_data = [
24700 'status' => $subscription_obj->get_status(),
24701 'start_date' => $subscription_obj->get_date_created(),
24702 'next_payment_date' => $subscription_obj->get_date( 'next_payment' ) ? $subscription_obj->get_date( 'next_payment' ) : 0,
24703 ];
24704
24705 $context['subscription_data'] = $subscription_data;
24706 $context['subscription_id'] = $subscription_obj->get_id();
24707 $context['user'] = WordPress::get_user_context( $user_id );
24708 $context['user_billing_data'] = $user_billing_data;
24709 $context['subscription'] = $subscription_obj->get_id();
24710 $context['subscription_name'] = get_the_title( $product_id );
24711 } elseif ( 'wc_purchases_variable_subscription' === $term ) {
24712 $subscription_data = [
24713 'status' => $subscription_obj->get_status(),
24714 'start_date' => $subscription_obj->get_date_created(),
24715 'next_payment_date' => $subscription_obj->get_date( 'next_payment' ) ? $subscription_obj->get_date( 'next_payment' ) : 0,
24716 ];
24717
24718 $context['subscription_data'] = $subscription_data;
24719 $context['user'] = WordPress::get_user_context( $user_id );
24720 $context['user_billing_data'] = $user_billing_data;
24721 $context['variable_subscription_option'] = $variation_id;
24722 $context['variable_subscription'] = $product_id;
24723 $context['subscription_name'] = $variation_name;
24724 $context['subscription_id'] = $subscription_obj->get_id();
24725 } elseif ( 'wc_renews_subscription_product' === $term ) {
24726 $subscription_data = [
24727 'status' => $subscription_obj->get_status(),
24728 'start_date' => $subscription_obj->get_date_created(),
24729 'next_payment_date' => $subscription_obj->get_date( 'next_payment' ) ? $subscription_obj->get_date( 'next_payment' ) : 0,
24730 ];
24731
24732 $context['subscription'] = $product_id;
24733 $context['subscription_name'] = get_the_title( $product_id );
24734 $context['last_order'] = $subscription_obj->get_last_order();
24735 $context['subscription_data'] = $subscription_data;
24736 $context['subscription_id'] = $subscription_obj->get_id();
24737 $context['user'] = WordPress::get_user_context( $user_id );
24738 $context['user_billing_data'] = $user_billing_data;
24739 } elseif ( 'wc_subscription_product_expires' === $term ) {
24740 $subscription_data = [
24741 'status' => $subscription_obj->get_status(),
24742 'start_date' => $subscription_obj->get_date_created(),
24743 'next_payment_date' => $subscription_obj->get_date( 'next_payment' ) ? $subscription_obj->get_date( 'next_payment' ) : 0,
24744 ];
24745
24746 $context['subscription_data'] = $subscription_data;
24747 $context['subscription_id'] = $subscription_obj->get_id();
24748 $context['user'] = WordPress::get_user_context( $user_id );
24749 $context['user_billing_data'] = $user_billing_data;
24750 $context['subscription'] = $product_id;
24751 $context['subscription_name'] = get_the_title( $product_id );
24752 }
24753
24754 $context['pluggable_data'] = $context;
24755 $context['response_type'] = 'live';
24756 } else {
24757 $sample_user = [
24758 'wp_user_id' => 1,
24759 'user_login' => 'johndoe',
24760 'display_name' => 'John Doe',
24761 'user_firstname' => 'John',
24762 'user_lastname' => 'Doe',
24763 'user_email' => 'john.doe@example.com',
24764 'user_registered' => '2024-01-15 10:30:25',
24765 'user_role' => [
24766 'customer',
24767 'subscriber',
24768 ],
24769 ];
24770
24771 $sample_billing_data = [
24772 'billing_first_name' => 'John',
24773 'billing_last_name' => 'Doe',
24774 'billing_country' => 'US',
24775 'billing_address_1' => '123 Main Street',
24776 'billing_address_2' => 'Apt 4B',
24777 'billing_city' => 'New York',
24778 'billing_state' => 'NY',
24779 'billing_postcode' => '10001',
24780 'billing_phone' => '+1-555-123-4567',
24781 'billing_email' => 'john.doe@example.com',
24782 ];
24783
24784 $sample_start_date = [
24785 'utc_offset' => 0,
24786 'date' => '2024-01-15 08:30:25.000000',
24787 'timezone_type' => 1,
24788 'timezone' => '+00:00',
24789 ];
24790
24791 if ( 'wc_subscription_updated' === $term ) {
24792 $context = [
24793 'variation_id' => 0,
24794 'product_id' => 73,
24795 'subscription' => 73,
24796 'variation_name' => 'Hi-Fi Headphones',
24797 'product_name' => 'Hi-Fi Headphones',
24798 'subscription_data' => [
24799 'start_date' => $sample_start_date,
24800 'next_payment_date' => '2024-12-15 08:30:25',
24801 ],
24802 'subscription_id' => 1234,
24803 'user' => $sample_user,
24804 'user_billing_data' => $sample_billing_data,
24805 'new_status' => 'active',
24806 'old_status' => 'pending-cancel',
24807 'status' => 'wc-active',
24808 ];
24809 } elseif ( 'wc_cancels_subscription_product' === $term ) {
24810 $context = [
24811 'subscription_data' => [
24812 'status' => 'cancelled',
24813 'start_date' => $sample_start_date,
24814 ],
24815 'user' => $sample_user,
24816 'user_billing_data' => $sample_billing_data,
24817 'subscription_id' => 1567,
24818 ];
24819 } elseif ( 'wc_subscribes_product' === $term ) {
24820 $context = [
24821 'subscription_data' => [
24822 'status' => 'active',
24823 'start_date' => $sample_start_date,
24824 'next_payment_date' => '2025-10-07 08:38:53',
24825 ],
24826 'subscription_id' => 1798,
24827 'user' => $sample_user,
24828 'user_billing_data' => $sample_billing_data,
24829 'subscription' => 1795,
24830 'subscription_name' => 'Subscription Product',
24831 ];
24832 } elseif ( 'wc_purchases_variable_subscription' === $term ) {
24833 $context = [
24834 'subscription_data' => [
24835 'status' => 'active',
24836 'start_date' => $sample_start_date,
24837 'next_payment_date' => '2025-11-06 05:25:01',
24838 ],
24839 'user' => $sample_user,
24840 'user_billing_data' => $sample_billing_data,
24841 'variable_subscription_option' => 1803,
24842 'variable_subscription' => 1802,
24843 'subscription_name' => 'Variation test product – 1200, 10',
24844 'subscription_id' => 1836,
24845 ];
24846 } elseif ( 'wc_renews_subscription_product' === $term ) {
24847 $context = [
24848 'subscription' => 1234,
24849 'subscription_name' => 'Monthly Subscription Product',
24850 'last_order' => 5678,
24851 'subscription_data' => [
24852 'status' => 'active',
24853 'start_date' => $sample_start_date,
24854 'next_payment_date' => '2025-11-06 08:30:25',
24855 ],
24856 'subscription_id' => 1567,
24857 'user' => $sample_user,
24858 'user_billing_data' => $sample_billing_data,
24859 ];
24860 } elseif ( 'wc_subscription_product_expires' === $term ) {
24861 $context = [
24862 'subscription_data' => [
24863 'status' => 'expired',
24864 'start_date' => $sample_start_date,
24865 'next_payment_date' => '2025-01-15 08:30:25',
24866 ],
24867 'subscription_id' => 1890,
24868 'user' => $sample_user,
24869 'user_billing_data' => $sample_billing_data,
24870 'subscription' => 1567,
24871 'subscription_name' => 'Premium Subscription Product',
24872 ];
24873 }
24874 $context['pluggable_data'] = $context;
24875 $context['response_type'] = 'sample';
24876 }
24877
24878 return $context;
24879 }
24880
24881 /**
24882 * Prepare FluentCart Product list.
24883 *
24884 * @param array $data data.
24885 *
24886 * @return array
24887 */
24888 public function search_fluentcart_product_list( $data ) {
24889 $options = [];
24890
24891 if ( ! class_exists( '\FluentCart\App\Models\Product' ) ) {
24892 return [
24893 'options' => $options,
24894 'hasMore' => false,
24895 ];
24896 }
24897
24898 $page = isset( $data['page'] ) ? max( 1, intval( $data['page'] ) ) : 1;
24899 $limit = Utilities::get_search_page_limit();
24900 $offset = $limit * ( $page - 1 );
24901
24902 $search_term = isset( $data['search_term'] ) ? $data['search_term'] : '';
24903
24904 // Start with a basic query.
24905 $query = \FluentCart\App\Models\Product::where( 'post_status', 'publish' );
24906
24907 if ( ! empty( $search_term ) ) {
24908 $query->where( 'post_title', 'LIKE', '%' . $search_term . '%' );
24909 }
24910
24911 $products = $query->select( [ 'ID', 'post_title' ] )
24912 ->offset( $offset )
24913 ->limit( $limit )
24914 ->get();
24915
24916 if ( ! empty( $products ) ) {
24917 foreach ( $products as $product ) {
24918 $options[] = [
24919 'label' => $product->post_title,
24920 'value' => $product->ID,
24921 ];
24922 }
24923 }
24924
24925 // Get total count for pagination.
24926 $total_query = \FluentCart\App\Models\Product::where( 'post_status', 'publish' );
24927
24928 if ( ! empty( $search_term ) ) {
24929 $total_query->where( 'post_title', 'LIKE', '%' . $search_term . '%' );
24930 }
24931
24932 $posts_count = $total_query->count();
24933
24934 return [
24935 'options' => $options,
24936 'hasMore' => $posts_count > ( $offset + $limit ),
24937 ];
24938 }
24939
24940 /**
24941 * Search SureDash triggers last data.
24942 *
24943 * @param array $data query params.
24944 *
24945 * @return array
24946 * @since 1.0.0
24947 */
24948 public function search_suredash_triggers_last_data( $data ) {
24949 $context = [];
24950 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
24951
24952 global $wpdb;
24953
24954 // Sample user data reused across all cases.
24955 $sample_user = [
24956 'user_id' => 789,
24957 'user_login' => 'john_doe',
24958 'user_email' => 'john@example.com',
24959 'user_nicename' => 'John Doe',
24960 'display_name' => 'John Doe',
24961 ];
24962
24963 switch ( $term ) {
24964 case 'suredash_comments_data':
24965 $recent_comments = $wpdb->get_results(
24966 $wpdb->prepare(
24967 "SELECT c.*, p.post_title, p.post_content, p.post_author, p.post_type, p.post_status
24968 FROM {$wpdb->comments} c
24969 LEFT JOIN {$wpdb->posts} p ON c.comment_post_ID = p.ID
24970 WHERE c.comment_approved = 1
24971 ORDER BY c.comment_date DESC
24972 LIMIT %d",
24973 1
24974 )
24975 );
24976
24977 if ( ! empty( $recent_comments ) ) {
24978 $comment = $recent_comments[0];
24979 $user_context = [];
24980 if ( $comment->user_id ) {
24981 $user_context = WordPress::get_user_context( $comment->user_id );
24982 }
24983
24984 $context = array_merge(
24985 [
24986 'comment_id' => $comment->comment_ID,
24987 'comment_content' => $comment->comment_content,
24988 'comment_author' => $comment->comment_author,
24989 'comment_author_email' => $comment->comment_author_email,
24990 'comment_date' => $comment->comment_date,
24991 'comment_approved' => $comment->comment_approved,
24992 'comment_type' => $comment->comment_type,
24993 'comment_parent' => $comment->comment_parent,
24994 'post_id' => $comment->comment_post_ID,
24995 'post_title' => $comment->post_title,
24996 'post_content' => $comment->post_content,
24997 'post_author' => $comment->post_author,
24998 'post_type' => $comment->post_type,
24999 'post_status' => $comment->post_status,
25000 'post_permalink' => get_permalink( $comment->comment_post_ID ),
25001 'suredash_post' => $comment->comment_post_ID,
25002 ],
25003 $user_context
25004 );
25005 $context['pluggable_data'] = $context;
25006 $context['response_type'] = 'live';
25007 } else {
25008 $context = [
25009 'comment_id' => 123,
25010 'comment_content' => 'This is a sample comment submission',
25011 'comment_author' => 'John Doe',
25012 'comment_author_email' => 'john@example.com',
25013 'comment_date' => '2024-01-15 10:30:00',
25014 'comment_approved' => 1,
25015 'comment_type' => 'comment',
25016 'comment_parent' => 0,
25017 'post_id' => 456,
25018 'post_title' => 'Sample Course Title',
25019 'post_content' => 'Course content here...',
25020 'post_author' => 1,
25021 'post_type' => 'suredash_course',
25022 'post_status' => 'publish',
25023 'post_permalink' => 'https://example.com/course/sample-course',
25024 'suredash_post' => 456,
25025 ];
25026 $context = array_merge( $context, $sample_user );
25027
25028 $context['pluggable_data'] = $context;
25029 $context['response_type'] = 'sample';
25030 }
25031
25032 return $context;
25033
25034 case 'suredash_comment_deleted_data':
25035 $recent_comments = $wpdb->get_results(
25036 $wpdb->prepare(
25037 "SELECT c.comment_ID, c.user_id
25038 FROM {$wpdb->comments} c
25039 ORDER BY c.comment_date DESC
25040 LIMIT %d",
25041 1
25042 )
25043 );
25044
25045 if ( ! empty( $recent_comments ) ) {
25046 $comment = $recent_comments[0];
25047 $user_context = WordPress::get_user_context( $comment->user_id );
25048
25049 $context = array_merge(
25050 [
25051 'comment_id' => $comment->comment_ID,
25052 ],
25053 $user_context
25054 );
25055
25056 $context['pluggable_data'] = $context;
25057 $context['response_type'] = 'live';
25058 } else {
25059 $context = array_merge( [ 'comment_id' => 123 ], $sample_user );
25060
25061 $context['pluggable_data'] = $context;
25062 $context['response_type'] = 'sample';
25063 }
25064
25065 return $context;
25066
25067 case 'suredash_post_deleted_data':
25068 $recent_posts = $wpdb->get_results(
25069 $wpdb->prepare(
25070 "SELECT ID, post_author
25071 FROM {$wpdb->posts}
25072 ORDER BY post_date DESC
25073 LIMIT %d",
25074 1
25075 )
25076 );
25077
25078 if ( ! empty( $recent_posts ) ) {
25079 $post = $recent_posts[0];
25080 $user_context = WordPress::get_user_context( $post->post_author );
25081
25082 $context = array_merge(
25083 [
25084 'post_id' => $post->ID,
25085 'deleter_user_id' => $post->post_author,
25086 ],
25087 $user_context
25088 );
25089
25090 $context['pluggable_data'] = $context;
25091 $context['response_type'] = 'live';
25092 } else {
25093 $context = array_merge(
25094 [
25095 'post_id' => 456,
25096 'deleter_user_id' => 789,
25097 ],
25098 $sample_user
25099 );
25100
25101 $context['pluggable_data'] = $context;
25102 $context['response_type'] = 'sample';
25103 }
25104
25105 return $context;
25106
25107 case 'suredash_post_data':
25108 $recent_posts = $wpdb->get_results(
25109 $wpdb->prepare(
25110 "SELECT * FROM {$wpdb->posts}
25111 WHERE post_status = 'publish'
25112 AND post_type NOT IN ('revision', 'nav_menu_item', 'attachment')
25113 ORDER BY post_date DESC
25114 LIMIT %d",
25115 1
25116 )
25117 );
25118
25119 if ( ! empty( $recent_posts ) ) {
25120 $post = $recent_posts[0];
25121 $user_context = WordPress::get_user_context( $post->post_author );
25122
25123 $context = array_merge(
25124 [
25125 'post_id' => $post->ID,
25126 'post_title' => $post->post_title,
25127 'post_content' => $post->post_content,
25128 'post_excerpt' => $post->post_excerpt,
25129 'post_status' => $post->post_status,
25130 'post_author' => $post->post_author,
25131 'post_date' => $post->post_date,
25132 'post_modified' => $post->post_modified,
25133 'post_type' => $post->post_type,
25134 'post_name' => $post->post_name,
25135 'post_permalink' => get_permalink( $post->ID ),
25136 'suredash_post' => $post->ID,
25137 'form_data' => [],
25138 ],
25139 $user_context
25140 );
25141
25142 $context['pluggable_data'] = $context;
25143 $context['response_type'] = 'live';
25144 } else {
25145 $context = [
25146 'post_id' => 456,
25147 'post_title' => 'Sample Post Title',
25148 'post_content' => 'This is sample post content for SureDash...',
25149 'post_excerpt' => 'Sample excerpt',
25150 'post_status' => 'publish',
25151 'post_author' => 1,
25152 'post_date' => '2024-01-15 10:30:00',
25153 'post_modified' => '2024-01-15 10:35:00',
25154 'post_type' => 'post',
25155 'post_name' => 'sample-post-title',
25156 'post_permalink' => 'https://example.com/sample-post-title',
25157 'suredash_post' => 456,
25158 'form_data' => [
25159 'post_title' => 'Sample Post Title',
25160 'post_content' => 'Sample content',
25161 ],
25162 ];
25163 $context = array_merge( $context, $sample_user );
25164
25165 $context['pluggable_data'] = $context;
25166 $context['response_type'] = 'sample';
25167 }
25168
25169 return $context;
25170
25171 case 'suredash_item_bookmarked':
25172 $recent_posts = $wpdb->get_results(
25173 $wpdb->prepare(
25174 "SELECT * FROM {$wpdb->posts}
25175 WHERE post_status = 'publish'
25176 AND post_type NOT IN ('revision', 'nav_menu_item', 'attachment')
25177 ORDER BY post_date DESC
25178 LIMIT %d",
25179 1
25180 )
25181 );
25182
25183 if ( ! empty( $recent_posts ) ) {
25184 $post = $recent_posts[0];
25185 $user_context = WordPress::get_user_context( $post->post_author );
25186
25187 $context = array_merge(
25188 [
25189 'item_id' => $post->ID,
25190 'item_type' => $post->post_type,
25191 'bookmark_status' => 'bookmarked',
25192 'is_bookmarked' => true,
25193 'is_unbookmarked' => false,
25194 'item_title' => $post->post_title,
25195 'item_content' => $post->post_content,
25196 'item_excerpt' => $post->post_excerpt,
25197 'item_author' => $post->post_author,
25198 'item_date' => $post->post_date,
25199 'item_status' => $post->post_status,
25200 'item_permalink' => get_permalink( $post->ID ),
25201 ],
25202 $user_context
25203 );
25204
25205 $context['pluggable_data'] = $context;
25206 $context['response_type'] = 'live';
25207 } else {
25208 $context = [
25209 'item_id' => 456,
25210 'item_type' => 'post',
25211 'bookmark_status' => 'bookmarked',
25212 'is_bookmarked' => true,
25213 'is_unbookmarked' => false,
25214 'item_title' => 'Sample Item Title',
25215 'item_content' => 'This is sample item content for bookmarking...',
25216 'item_excerpt' => 'Sample item excerpt',
25217 'item_author' => 1,
25218 'item_date' => '2024-01-15 10:30:00',
25219 'item_status' => 'publish',
25220 'item_permalink' => 'https://example.com/sample-item-title',
25221 ];
25222 $context = array_merge( $context, $sample_user );
25223
25224 $context['pluggable_data'] = $context;
25225 $context['response_type'] = 'sample';
25226 }
25227
25228 return $context;
25229
25230 case 'suredash_space_data':
25231 $recent_posts = $wpdb->get_results(
25232 $wpdb->prepare(
25233 "SELECT * FROM {$wpdb->posts}
25234 WHERE post_status = 'publish'
25235 AND post_type NOT IN ('revision', 'nav_menu_item', 'attachment')
25236 ORDER BY post_date DESC
25237 LIMIT %d",
25238 1
25239 )
25240 );
25241
25242 if ( ! empty( $recent_posts ) ) {
25243 $post = $recent_posts[0];
25244
25245 $context = [
25246 'space_id' => $post->ID,
25247 'space_status' => 'deleted',
25248 ];
25249
25250 $context['pluggable_data'] = $context;
25251 $context['response_type'] = 'live';
25252 } else {
25253 $context = [
25254 'space_id' => 456,
25255 'space_status' => 'deleted',
25256 ];
25257
25258 $context['pluggable_data'] = $context;
25259 $context['response_type'] = 'sample';
25260 }
25261
25262 return $context;
25263
25264 default:
25265 return $context;
25266 }
25267 }
25268 /**
25269 * Get MailerPress contact lists
25270 *
25271 * @param array $data data.
25272 *
25273 * @return array
25274 */
25275 public function search_mailerpress_contact_lists( $data ) {
25276 global $wpdb;
25277
25278 $lists = $wpdb->get_results( "SELECT name, list_id FROM {$wpdb->prefix}mailerpress_lists ORDER BY name ASC" );
25279 $options = [];
25280
25281 if ( ! empty( $lists ) ) {
25282 foreach ( $lists as $list ) {
25283 $options[] = [
25284 'label' => $list->name,
25285 'value' => $list->list_id,
25286 ];
25287 }
25288 }
25289
25290 return [
25291 'options' => $options,
25292 'hasMore' => false,
25293 ];
25294 }
25295
25296 /**
25297 * Get MailerPress contact tags
25298 *
25299 * @param array $data data.
25300 *
25301 * @return array
25302 */
25303 public function search_mailerpress_contact_tags( $data ) {
25304
25305 global $wpdb;
25306
25307 $tags = $wpdb->get_results( "SELECT name, tag_id FROM {$wpdb->prefix}mailerpress_tags ORDER BY name ASC" );
25308 $options = [];
25309
25310 if ( ! empty( $tags ) ) {
25311 foreach ( $tags as $tag ) {
25312 $options[] = [
25313 'label' => $tag->name,
25314 'value' => $tag->tag_id,
25315 ];
25316 }
25317 }
25318
25319 return [
25320 'options' => $options,
25321 'hasMore' => false,
25322 ];
25323 }
25324
25325 /**
25326 * Get MailerPress last data
25327 *
25328 * @param array $data data.
25329 *
25330 * @return array
25331 */
25332 public function search_mailerpress_last_data( $data ) {
25333 $context = [];
25334 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
25335
25336 global $wpdb;
25337
25338 switch ( $term ) {
25339 case 'mailerpress_contact_data':
25340 $recent_contact = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_contact ORDER BY created_at DESC LIMIT 1" );
25341
25342 if ( $recent_contact ) {
25343 $context = [
25344 'contact_id' => $recent_contact->contact_id,
25345 'email' => $recent_contact->email,
25346 'first_name' => $recent_contact->first_name,
25347 'last_name' => $recent_contact->last_name,
25348 'subscription_status' => $recent_contact->subscription_status,
25349 'opt_in_source' => $recent_contact->opt_in_source,
25350 'opt_in_details' => $recent_contact->opt_in_details,
25351 'created_at' => $recent_contact->created_at,
25352 'updated_at' => $recent_contact->updated_at,
25353 ];
25354 $context['pluggable_data'] = $context;
25355 $context['response_type'] = 'live';
25356 } else {
25357 $context = [
25358 'contact_id' => 15,
25359 'email' => 'sarah.johnson@example.com',
25360 'first_name' => 'Sarah',
25361 'last_name' => 'Johnson',
25362 'subscription_status' => 'subscribed',
25363 'opt_in_source' => 'signup_form',
25364 'opt_in_details' => 'Newsletter signup - Homepage',
25365 'created_at' => '2025-12-01 14:23:17',
25366 'updated_at' => '2025-12-01 14:23:17',
25367 ];
25368 $context['pluggable_data'] = $context;
25369 $context['response_type'] = 'sample';
25370 }
25371 break;
25372
25373 case 'list_created':
25374 $recent_list = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_lists ORDER BY created_at DESC LIMIT 1" );
25375
25376 if ( $recent_list ) {
25377 $user_data = get_userdata( 1 );
25378
25379 $context['pluggable_data'] = [
25380 'wp_user_id' => $user_data ? $user_data->ID : 1,
25381 'user_login' => $user_data ? $user_data->user_login : 'root',
25382 'display_name' => $user_data ? $user_data->display_name : 'Arian',
25383 'user_firstname' => get_user_meta( $user_data ? $user_data->ID : 1, 'first_name', true ) ? get_user_meta( $user_data ? $user_data->ID : 1, 'first_name', true ) : '',
25384 'user_lastname' => get_user_meta( $user_data ? $user_data->ID : 1, 'last_name', true ) ? get_user_meta( $user_data ? $user_data->ID : 1, 'last_name', true ) : '',
25385 'user_email' => $user_data ? $user_data->user_email : 'dev-email@wpengine.local',
25386 'user_registered' => $user_data ? $user_data->user_registered : '2025-07-02 10:50:38',
25387 'user_role' => $user_data ? $user_data->roles : [ 'administrator' ],
25388 'list' => [
25389 'id' => $recent_list->list_id,
25390 'name' => $recent_list->name,
25391 'description' => $recent_list->description,
25392 'created_at' => $recent_list->created_at,
25393 'updated_at' => $recent_list->updated_at,
25394 ],
25395 ];
25396 $context['response_type'] = 'live';
25397 } else {
25398 $context['pluggable_data'] = [
25399 'wp_user_id' => 1,
25400 'user_login' => 'root',
25401 'display_name' => 'Arian',
25402 'user_firstname' => '',
25403 'user_lastname' => '',
25404 'user_email' => 'dev-email@wpengine.local',
25405 'user_registered' => '2025-07-02 10:50:38',
25406 'user_role' => [ 'administrator' ],
25407 'list' => [
25408 'id' => 7,
25409 'name' => 'Weekly Newsletter Subscribers',
25410 'description' => 'Subscribers who opted in for weekly newsletter updates',
25411 'created_at' => '2025-12-01 10:15:23',
25412 'updated_at' => '2025-12-01 15:30:45',
25413 ],
25414 ];
25415 $context['response_type'] = 'sample';
25416 }
25417 break;
25418
25419 case 'contact_added_to_list':
25420 $recent_list = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_lists ORDER BY created_at DESC LIMIT 1" );
25421 $recent_contact = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_contact ORDER BY created_at DESC LIMIT 1" );
25422
25423 if ( $recent_contact && $recent_list ) {
25424 $context['pluggable_data'] = [
25425 'contact' => [
25426 'id' => $recent_contact->contact_id,
25427 'email' => $recent_contact->email,
25428 'first_name' => $recent_contact->first_name,
25429 'last_name' => $recent_contact->last_name,
25430 'subscription_status' => $recent_contact->subscription_status,
25431 'created_at' => $recent_contact->created_at,
25432 'updated_at' => $recent_contact->updated_at,
25433 ],
25434 'list' => [
25435 'id' => $recent_list->list_id,
25436 'name' => $recent_list->name,
25437 'description' => $recent_list->description,
25438 'created_at' => $recent_list->created_at,
25439 'updated_at' => $recent_list->updated_at,
25440 ],
25441 ];
25442 $context['response_type'] = 'live';
25443 } else {
25444 $context['pluggable_data'] = [
25445 'contact' => [
25446 'id' => 12,
25447 'email' => 'michael.chen@company.com',
25448 'first_name' => 'Michael',
25449 'last_name' => 'Chen',
25450 'subscription_status' => 'subscribed',
25451 'created_at' => '2025-12-01 09:15:33',
25452 'updated_at' => '2025-12-01 11:22:47',
25453 ],
25454 'list' => [
25455 'id' => 7,
25456 'name' => 'Weekly Newsletter Subscribers',
25457 'description' => 'Subscribers who opted in for weekly newsletter updates',
25458 'created_at' => '2025-12-01 10:15:23',
25459 'updated_at' => '2025-12-01 15:30:45',
25460 ],
25461 ];
25462 $context['response_type'] = 'sample';
25463 }
25464 break;
25465
25466 case 'tag_created':
25467 $recent_tag = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_tags LIMIT 1" );
25468
25469 if ( $recent_tag ) {
25470 $context['pluggable_data'] = [
25471 'tag_id' => $recent_tag->tag_id,
25472 'name' => $recent_tag->name,
25473 ];
25474 $context['response_type'] = 'live';
25475 } else {
25476 $context['pluggable_data'] = [
25477 'tag_id' => 8,
25478 'name' => 'VIP Customers',
25479 ];
25480 $context['response_type'] = 'sample';
25481 }
25482 break;
25483
25484 case 'mailerpress_tag_added_to_contact_data':
25485 $recent_contact = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_contact ORDER BY created_at DESC LIMIT 1" );
25486 $recent_tag = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_tags LIMIT 1" );
25487
25488 if ( $recent_contact && $recent_tag ) {
25489 $user_data = get_userdata( 1 );
25490
25491 $context['pluggable_data'] = [
25492 'contact' => [
25493 'id' => $recent_contact->contact_id,
25494 'email' => $recent_contact->email,
25495 'first_name' => $recent_contact->first_name,
25496 'last_name' => $recent_contact->last_name,
25497 'subscription_status' => $recent_contact->subscription_status,
25498 'created_at' => $recent_contact->created_at,
25499 'updated_at' => $recent_contact->updated_at,
25500 ],
25501 'tag' => [
25502 'id' => $recent_tag->tag_id,
25503 'name' => $recent_tag->name,
25504 ],
25505 ];
25506 $context['response_type'] = 'live';
25507 } else {
25508 $context['pluggable_data'] = [
25509 'contact' => [
25510 'id' => 18,
25511 'email' => 'emma.wilson@startup.io',
25512 'first_name' => 'Emma',
25513 'last_name' => 'Wilson',
25514 'subscription_status' => 'subscribed',
25515 'created_at' => '2025-12-01 13:45:12',
25516 'updated_at' => '2025-12-01 16:18:35',
25517 ],
25518 'tag' => [
25519 'id' => 5,
25520 'name' => 'Premium Member',
25521 ],
25522 ];
25523 $context['response_type'] = 'sample';
25524 }
25525 break;
25526
25527 case 'campaign_created':
25528 $recent_campaign = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_campaigns ORDER BY created_at DESC LIMIT 1" );
25529
25530 if ( $recent_campaign ) {
25531 $user_data = get_userdata( 1 );
25532
25533 $context['pluggable_data'] = [
25534 'wp_user_id' => $user_data ? $user_data->ID : 1,
25535 'user_login' => $user_data ? $user_data->user_login : 'root',
25536 'display_name' => $user_data ? $user_data->display_name : 'Arian',
25537 'user_firstname' => get_user_meta( $user_data ? $user_data->ID : 1, 'first_name', true ) ? get_user_meta( $user_data ? $user_data->ID : 1, 'first_name', true ) : '',
25538 'user_lastname' => get_user_meta( $user_data ? $user_data->ID : 1, 'last_name', true ) ? get_user_meta( $user_data ? $user_data->ID : 1, 'last_name', true ) : '',
25539 'user_email' => $user_data ? $user_data->user_email : 'dev-email@wpengine.local',
25540 'user_registered' => $user_data ? $user_data->user_registered : '2025-07-02 10:50:38',
25541 'user_role' => $user_data ? $user_data->roles : [ 'administrator' ],
25542 'campaign' => [
25543 'campaign_id' => $recent_campaign->campaign_id,
25544 'name' => $recent_campaign->name,
25545 'subject' => $recent_campaign->subject,
25546 'status' => $recent_campaign->status,
25547 'type' => $recent_campaign->type,
25548 'created_at' => $recent_campaign->created_at,
25549 'updated_at' => $recent_campaign->updated_at,
25550 ],
25551 ];
25552 $context['response_type'] = 'live';
25553 } else {
25554 $user_data = get_userdata( 1 );
25555
25556 $context['pluggable_data'] = [
25557 'wp_user_id' => $user_data ? $user_data->ID : 1,
25558 'user_login' => $user_data ? $user_data->user_login : 'root',
25559 'display_name' => $user_data ? $user_data->display_name : 'Arian',
25560 'user_firstname' => get_user_meta( $user_data ? $user_data->ID : 1, 'first_name', true ) ? get_user_meta( $user_data ? $user_data->ID : 1, 'first_name', true ) : '',
25561 'user_lastname' => get_user_meta( $user_data ? $user_data->ID : 1, 'last_name', true ) ? get_user_meta( $user_data ? $user_data->ID : 1, 'last_name', true ) : '',
25562 'user_email' => $user_data ? $user_data->user_email : 'dev-email@wpengine.local',
25563 'user_registered' => $user_data ? $user_data->user_registered : '2025-07-02 10:50:38',
25564 'user_role' => $user_data ? $user_data->roles : [ 'administrator' ],
25565 'campaign' => [
25566 'campaign_id' => 23,
25567 'name' => 'Holiday Special Offer 2025',
25568 'subject' => 'Exclusive 30% OFF - Limited Time Holiday Deal!',
25569 'status' => 'draft',
25570 'type' => 'newsletter',
25571 'created_at' => '2025-12-01 16:45:22',
25572 'updated_at' => '2025-12-01 17:12:38',
25573 ],
25574 ];
25575 $context['response_type'] = 'sample';
25576 }
25577 break;
25578
25579 }
25580 return $context;
25581 }
25582
25583 /**
25584 * Get WPCafe Branch List.
25585 *
25586 * @param array $data data.
25587 *
25588 * @return array
25589 */
25590 public function search_wpcafe_branch_list( $data ) {
25591 $options = [];
25592
25593 if ( ! class_exists( 'WpCafe\Models\Location_Model' ) ) {
25594 return [
25595 'options' => $options,
25596 'hasMore' => false,
25597 ];
25598 }
25599
25600 $locations = \WpCafe\Models\Location_Model::all();
25601
25602 if ( ! empty( $locations ) ) {
25603 foreach ( $locations as $location ) {
25604 $options[] = [
25605 'label' => $location->restaurant_name,
25606 'value' => $location->term_id,
25607 ];
25608 }
25609 }
25610
25611 return [
25612 'options' => $options,
25613 'hasMore' => false,
25614 ];
25615 }
25616
25617
25618 }
25619
25620
25621 GlobalSearchController::get_instance();
25622