PluginProbe
OttoKit: All-in-One Automation Platform / 1.1.10
OttoKit: All-in-One Automation Platform v1.1.10
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.10, at src/Controllers/GlobalSearchController.php

24,089 lines 829.5 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 Course.
417 *
418 * @param array $data quesry params.
419 *
420 * @return array
421 * @since 1.0.0
422 */
423 public function search_tutor_course( $data ) {
424 if ( ! function_exists( 'tutor' ) ) {
425 return [];
426 }
427 $courses = get_posts(
428 [
429 'post_type' => tutor()->course_post_type,
430 'post_status' => 'publish',
431 'numberposts' => -1,
432 ]
433 );
434 $options = [];
435 foreach ( $courses as $course ) {
436 $options[] = [
437 'label' => $course->post_title,
438 'value' => $course->ID,
439 ];
440 }
441
442 return [
443 'options' => $options,
444 'hasMore' => false,
445 ];
446 }
447
448 /**
449 * Search Products.
450 *
451 * @param array $data quesry params.
452 *
453 * @return array
454 * @since 1.0.0
455 */
456 public function search_product( $data ) {
457 $result = [];
458 $posts = Utilities::find_posts_by_title( $data );
459
460 foreach ( $posts['results'] as $post ) {
461 $result[] = [
462 'label' => $post['post_title'],
463 'value' => $post['post_title'],
464 ];
465 }
466
467 return [
468 'options' => $result,
469 'hasMore' => $posts['has_more'],
470 ];
471 }
472
473 /**
474 * Search Product Categories.
475 *
476 * @param array $data Search Params.
477 *
478 * @return array
479 * @since 1.0.0
480 */
481 public function search_product_category( $data ) {
482 if ( ! empty( $data['dynamic'] ) ) {
483 $taxonomy = $data['dynamic'];
484 } else {
485 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
486 }
487
488 $term = $data['term'];
489 $result = [];
490 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
491 foreach ( $terms['result'] as $tax_term ) {
492 $result[] = [
493 'label' => $tax_term->name,
494 'value' => $tax_term->name,
495 ];
496 }
497
498 return [
499 'options' => $result,
500 'hasMore' => $terms['has_more'],
501 ];
502 }
503
504 /**
505 * Search Product Tags.
506 *
507 * @param array $data Search Params.
508 *
509 * @return array
510 * @since 1.0.0
511 */
512 public function search_product_tags( $data ) {
513 if ( ! empty( $data['dynamic'] ) ) {
514 $taxonomy = $data['dynamic'];
515 } else {
516 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
517 }
518
519 $term = $data['term'];
520 $result = [];
521 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
522
523 foreach ( $terms['result'] as $tax_term ) {
524 $result[] = [
525 'label' => $tax_term->name,
526 'value' => $tax_term->name,
527 ];
528 }
529
530 return [
531 'options' => $result,
532 'hasMore' => $terms['has_more'],
533 ];
534 }
535
536 /**
537 * Global ajax search.
538 * Here you need to add the field action name to work.
539 *
540 * @param WP_REST_Request $request Request data.
541 *
542 * @return WP_REST_Response|object
543 * @since 1.0.0
544 */
545 public function global_search( $request ) {
546 $post_type = $request->get_param( 'post_type' );
547 $dynamic = $request->get_param( 'dynamic' );
548 $search_term = $request->get_param( 'term' );
549 $identifier = $request->get_param( 'field_identifier' );
550 $page = max( 1, $request->get_param( 'page' ) );
551 $taxonomy = $request->get_param( 'taxonomy' ) ? $request->get_param( 'taxonomy' ) : [];
552
553 $filter = $request->get_param( 'filter' ) ? json_decode( stripslashes( $request->get_param( 'filter' ) ), true ) : [];
554
555 $data = [
556 'dynamic' => $dynamic,
557 'search_term' => $search_term,
558 'page' => $page,
559 'taxonomy' => $taxonomy,
560 'filter' => $filter,
561 'post_type' => $post_type,
562 ];
563 $response = [
564 'hasMore' => false,
565 'options' => [],
566 ];
567
568 $method_name = 'search_' . $identifier;
569
570 if ( method_exists( $this, $method_name ) ) {
571 $response = $this->{$method_name}( $data );
572 } else {
573 return RestController::error_message( 'Invalid field Identifier param.' );
574 }
575
576 return RestController::success_message( $response );
577 }
578
579 /**
580 * Search pluggables post liked data.
581 *
582 * @param array $data data.
583 * @return array
584 */
585 public function search_pluggables_post_liked( $data ) {
586 $context = [];
587 $args = [
588 'order' => 'DESC',
589 'number' => 1,
590 'orderby' => 'ID',
591 ];
592
593 if ( isset( $data['filter']['suredash_post']['value'] ) ) {
594 $post_id = $data['filter']['suredash_post']['value'];
595 }
596
597 $users = get_users( $args );
598
599 if ( ! empty( $users ) ) {
600 $user = $users[0];
601 $post_title = 'Sample Post';
602 if ( isset( $post_id ) ) {
603 $post = get_post( $post_id );
604 if ( $post ) {
605 $post_title = $post->post_title;
606 }
607 }
608
609 $pluggable_data = [
610 'wp_user_id' => $user->ID,
611 'user_login' => $user->user_login,
612 'display_name' => $user->display_name,
613 'user_firstname' => get_user_meta( $user->ID, 'first_name', true ) ? get_user_meta( $user->ID, 'first_name', true ) : '--',
614 'user_lastname' => get_user_meta( $user->ID, 'last_name', true ) ? get_user_meta( $user->ID, 'last_name', true ) : '--',
615 'user_email' => $user->user_email,
616 'user_registered' => $user->user_registered,
617 'user_role' => $user->roles,
618 'post_id' => isset( $post_id ) ? $post_id : 123,
619 'post_title' => $post_title,
620 'post_author' => isset( $post_id ) ? get_post_field( 'post_author', $post_id ) : 1,
621 ];
622 $context['pluggable_data'] = $pluggable_data;
623 $context['response_type'] = 'live';
624 } else {
625 $context['pluggable_data'] = [
626 'wp_user_id' => 1,
627 'user_login' => 'testuser',
628 'display_name' => 'Test User',
629 'user_firstname' => '--',
630 'user_lastname' => '--',
631 'user_email' => 'testuser@gmail.com',
632 'user_registered' => '2024-06-18 09:47:58',
633 'user_role' => [ 'subscriber' ],
634 'post_id' => isset( $post_id ) ? $post_id : 123,
635 'post_title' => 'Sample Post',
636 'post_author' => 1,
637 ];
638 $context['response_type'] = 'sample';
639 }
640 return $context;
641 }
642
643
644 /**
645 * Search Taxonomy Terms.
646 *
647 * @param array $data Search Params.
648 *
649 * @since 1.0.0
650 *
651 * @return array
652 */
653 public function search_term( $data ) {
654 if ( ! empty( $data['dynamic'] ) ) {
655 $taxonomy = $data['dynamic'];
656 } else {
657 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
658 }
659
660 $term = $data['term'];
661 $result = [];
662 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
663 foreach ( $terms['result'] as $tax_term ) {
664 $result[] = [
665 'label' => $tax_term->name,
666 'value' => $tax_term->term_id,
667 ];
668 }
669
670 return [
671 'options' => $result,
672 'hasMore' => $terms['has_more'],
673 ];
674 }
675
676 /**
677 * Search sureemails data.
678 *
679 * @param array $data data.
680 * @return array
681 */
682 public function search_pluggables_sureemails_mail( $data ) {
683 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
684 $context = [];
685
686 if ( 'mail_sent' === $term ) {
687 $context['pluggable_data'] = [
688 'to' => [ 'johnDoe@xyz.com' ],
689 'subject' => 'Test Email',
690 'message' => 'This is a test email',
691 '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',
692 'attachments' => [ 'Sample Attachment' ],
693 ];
694 $context['response_type'] = 'sample';
695 } elseif ( 'mail_failed' === $term ) {
696 $context['pluggable_data'] = [
697 'errors' => [
698 'wp_mail_failed' => [
699 'We were unable to send the email. Please ensure the recipient email address and sender configuration are correct. If the issue persists, contact support.',
700 ],
701 ],
702 'error_data' => [
703 'wp_mail_failed' => [
704 'to' => 'johnDoe@xyz.com',
705 'subject' => 'Test Email',
706 'message' => 'This is a test email.',
707 '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',
708 'attachments' => [ 'Sample Attachment' ],
709 ],
710 ],
711 ];
712 $context['response_type'] = 'sample';
713 } elseif ( 'mail_blocked' === $term ) {
714 $context['pluggable_data'] = [
715 'to' => 'johnDoe@xyz.com',
716 'subject' => 'Test Email',
717 'message' => 'This is a test email.',
718 'headers' => 'From: johnDoe@xyz.com
719 X-Mailer: PHP/8.1.22
720 Content-Type: text/html; charset=utf-8
721 Reply-To: johnDoe@xyz.com
722 Cc:johnDoe@xyz.com Bcc:johnDoe@xyz.com',
723 'attachments' => [ 'Sample Attachment' ],
724 'categories' => [
725 'harassment' => 1,
726 'harassment/threatening' => 1,
727 'sexual' => 0,
728 'hate' => 0,
729 'hate/threatening' => 0,
730 'illicit' => 0,
731 'illicit/violent' => 0,
732 'self-harm/intent' => 0,
733 'self-harm/instructions' => 0,
734 'self-harm' => 0,
735 'sexual/minors' => 0,
736 'violence' => 1,
737 'violence/graphic' => 0,
738 ],
739 ];
740 $context['response_type'] = 'sample';
741 }
742
743 return $context;
744 }
745
746
747
748 /**
749 * List Taxonomy Terms.
750 *
751 * @param array $data Search Params.
752 *
753 * @since 1.0.0
754 *
755 * @return array
756 */
757 public function search_term_list( $data ) {
758 $taxonomy = $data['dynamic'];
759 $result = [];
760 $terms = Utilities::get_terms( '', $data['page'], $taxonomy );
761 foreach ( $terms['result'] as $tax_term ) {
762 $result[] = [
763 'label' => $tax_term->name,
764 'value' => $tax_term->term_id,
765 ];
766 }
767
768 return [
769 'options' => $result,
770 'hasMore' => $terms['has_more'],
771 ];
772 }
773
774 /**
775 * List All Taxonomy Terms.
776 *
777 * @param array $data Search Params.
778 *
779 * @since 1.0.0
780 *
781 * @return array
782 */
783 public function search_term_list_for_all_taxonomy( $data ) {
784 global $wpdb;
785
786 $result = [];
787
788 $all_taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
789 $taxonomy_names = array_keys( $all_taxonomies );
790
791 if ( empty( $taxonomy_names ) ) {
792 return [
793 'options' => [],
794 'hasMore' => false,
795 ];
796 }
797
798 $all_terms = get_terms(
799 [
800 'taxonomy' => $taxonomy_names,
801 'hide_empty' => false,
802 'number' => 0,
803 'orderby' => 'name',
804 'order' => 'ASC',
805 ]
806 );
807
808 $terms = [];
809 if ( ! is_wp_error( $all_terms ) && ! empty( $all_terms ) ) {
810 foreach ( $all_terms as $term ) {
811 $terms[] = (object) [
812 'term_id' => $term->term_id,
813 'name' => $term->name,
814 'taxonomy' => $term->taxonomy,
815 ];
816 }
817 }
818
819 if ( ! empty( $terms ) ) {
820 foreach ( $terms as $term ) {
821 $taxonomy_obj = isset( $all_taxonomies[ $term->taxonomy ] ) ? $all_taxonomies[ $term->taxonomy ] : null;
822 $taxonomy_label = $taxonomy_obj ? $taxonomy_obj->label : $term->taxonomy;
823
824 $result[] = [
825 'label' => $term->name . ' - ' . ucwords( $taxonomy_label ),
826 'value' => $term->term_id . '%-%' . $term->taxonomy,
827 ];
828 }
829 }
830
831 return [
832 'options' => $result,
833 'hasMore' => false,
834 ];
835 }
836
837
838 /**
839 * List Category Terms.
840 *
841 * @param array $data Search Params.
842 *
843 * @since 1.0.0
844 *
845 * @return array
846 */
847 public function search_category_term_list( $data ) {
848 $result = [];
849 $taxonomy = $data['dynamic'];
850 $terms = Utilities::get_terms( '', $data['page'], $taxonomy );
851 foreach ( $terms['result'] as $tax_term ) {
852 if ( 0 == $tax_term->parent ) {
853 $result[] = [
854 'label' => $tax_term->name,
855 'value' => $tax_term->term_id,
856 ];
857 foreach ( $terms['result'] as $child ) {
858 if ( $child->parent == $tax_term->term_id ) {
859 $result[] = [
860 'label' => ' - ' . $child->name,
861 'value' => $child->term_id,
862 ];
863 }
864 }
865 }
866 }
867
868 return [
869 'options' => $result,
870 'hasMore' => $terms['has_more'],
871 ];
872 }
873
874 /**
875 * List Taxonomy Tags.
876 *
877 * @param array $data Search Params.
878 *
879 * @since 1.0.0
880 *
881 * @return array
882 */
883 public function search_term_list_tags( $data ) {
884 $result = [];
885 $terms = Utilities::get_terms( '', $data['page'], [ 'post_tag' ] );
886 foreach ( $terms['result'] as $tax_term ) {
887 $result[] = [
888 'label' => $tax_term->name,
889 'value' => $tax_term->term_id,
890 ];
891 }
892
893 return [
894 'options' => $result,
895 'hasMore' => $terms['has_more'],
896 ];
897 }
898
899 /**
900 * List Taxonomy Product Tags.
901 *
902 * @param array $data Search Params.
903 *
904 * @since 1.0.0
905 *
906 * @return array
907 */
908 public function search_term_list_prod_tags( $data ) {
909 $result = [];
910 $terms = Utilities::get_terms( '', $data['page'], [ 'product_tag' ] );
911 foreach ( $terms['result'] as $tax_term ) {
912 $result[] = [
913 'label' => $tax_term->name,
914 'value' => $tax_term->term_id,
915 ];
916 }
917
918 return [
919 'options' => $result,
920 'hasMore' => $terms['has_more'],
921 ];
922 }
923
924 /**
925 * List Role Capabilities.
926 *
927 * @param array $data Search Params.
928 *
929 * @since 1.0.0
930 *
931 * @return array
932 */
933 public function search_role_capabilities( $data ) {
934 $user_roles = wp_roles();
935 $result = [];
936 $capabilities = [];
937
938 foreach ( $user_roles->roles as $name => $role ) {
939 foreach ( $role['capabilities'] as $key => $cap ) {
940 if ( ! in_array( $key, $capabilities ) ) {
941 $capabilities[] = $key;
942 $result[] = [
943 'label' => $key,
944 'value' => esc_attr( $key ),
945 ];
946 }
947 }
948 }
949
950 return [
951 'options' => $result,
952 'hasMore' => false,
953 ];
954 }
955
956 /**
957 * Search users.
958 *
959 * @param array $data Search Params.
960 *
961 * @since 1.0.0
962 *
963 * @return array
964 */
965 public function search_user( $data ) {
966 $result = [];
967 $page = $data['page'];
968 $users = Utilities::get_users( $data, $page );
969
970 if ( is_array( $users['results'] ) ) {
971 foreach ( $users['results'] as $user ) {
972 $result[] = [
973 'label' => $user->user_login,
974 'value' => $user->ID,
975 ];
976 }
977 }
978
979 return [
980 'options' => $result,
981 'hasMore' => $users['has_more'],
982 ];
983
984 }
985
986 /**
987 * Search WPForm fields.
988 *
989 * @param array $data Search Params.
990 *
991 * @since 1.0.0
992 *
993 * @return array
994 */
995 public function search_pluggable_wpform_fields( $data ) {
996 $result = [];
997 $page = $data['page'];
998 $form_id = absint( $data['dynamic'] );
999 $wpform_fields = Utilities::get_wpform_fields( $data['search_term'], $page, $form_id );
1000
1001 if ( $wpform_fields && is_array( $wpform_fields['results'] ) ) {
1002 foreach ( $wpform_fields['results'] as $field ) {
1003 $result[] = [
1004 'label' => $field['label'],
1005 'value' => '{' . $field['id'] . '}',
1006 ];
1007 }
1008 }
1009
1010 return [
1011 'options' => $result,
1012 'hasMore' => ( $wpform_fields && $wpform_fields['has_more'] ) ? $wpform_fields['has_more'] : false,
1013 ];
1014 }
1015
1016 /**
1017 * Prepare variable products.
1018 *
1019 * @param array $data data.
1020 *
1021 * @return array
1022 */
1023 public function search_variable_products( $data ) {
1024 if ( ! function_exists( 'wc_get_products' ) ) {
1025 return [
1026 'options' => [],
1027 'hasMore' => false,
1028 ];
1029 }
1030
1031 $products = wc_get_products(
1032 [
1033 'type' => 'variable',
1034 'status' => 'publish',
1035 'limit' => -1,
1036 ]
1037 );
1038
1039 $options = [];
1040 if ( is_array( $products ) ) {
1041 foreach ( $products as $product ) {
1042 $options[] = [
1043 'label' => $product->get_name(),
1044 'value' => (string) $product->get_id(),
1045 ];
1046 }
1047 }
1048
1049 return [
1050 'options' => $options,
1051 'hasMore' => false,
1052 ];
1053 }
1054
1055 /**
1056 * Prepare variable products.
1057 *
1058 * @param array $data data.
1059 *
1060 * @return array
1061 */
1062 public function search_product_variations( $data ) {
1063 $variations = Utilities::get_product_variations( $data['dynamic'] );
1064 $options = [];
1065
1066 foreach ( $variations['result'] as $product ) {
1067 $options[] = [
1068 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
1069 'value' => (string) $product->ID,
1070 ];
1071 }
1072
1073 return [
1074 'options' => $options,
1075 'hasMore' => $variations['has_more'],
1076 ];
1077 }
1078
1079 /**
1080 * Search WooCommerce Subscriptions.
1081 *
1082 * @param array $data Search Params.
1083 *
1084 * @since 1.0.0
1085 *
1086 * @return array
1087 */
1088 public function search_subscription_variation( $data ) {
1089 $subscription_products = Utilities::get_subscription_variation( $data['search_term'], $data['page'] );
1090 $result = [];
1091
1092 if ( ! function_exists( 'wc_get_products' ) ) {
1093 return $result;
1094 }
1095
1096 foreach ( $subscription_products['result'] as $post ) {
1097 if ( $data['search_term'] ) {
1098 if ( false !== stripos( $post->get_title(), $data['search_term'] ) ) {
1099 $result[] = [
1100 'label' => $post->get_title(),
1101 'value' => (string) $post->get_id(),
1102 ];
1103 }
1104 } else {
1105 $result[] = [
1106 'label' => $post->get_title(),
1107 'value' => (string) $post->get_id(),
1108 ];
1109 }
1110 }
1111
1112 return [
1113 'options' => $result,
1114 'hasMore' => $subscription_products['has_more'],
1115 ];
1116 }
1117
1118 /**
1119 * Prepare WooCommerce Payment Methods.
1120 *
1121 * @param array $data Search Params.
1122 * @return array
1123 */
1124 public function search_woo_payment_methods( $data ) {
1125 $payment_methods = WC()->payment_gateways()->get_available_payment_gateways();
1126 $options = [];
1127
1128 if ( ! empty( $payment_methods ) ) {
1129 foreach ( $payment_methods as $payment ) {
1130 $options[] = [
1131 'label' => $payment->title,
1132 'value' => $payment->id,
1133 ];
1134 }
1135 }
1136
1137 return [
1138 'options' => $options,
1139 'hasMore' => false,
1140 ];
1141 }
1142
1143 /**
1144 * Prepare WooCommerce Order Status List.
1145 *
1146 * @param array $data Search Params.
1147 * @return array
1148 */
1149 public function search_woo_order_status_list( $data ) {
1150 $order_status = wc_get_order_statuses();
1151 $options = [];
1152
1153 if ( ! empty( $order_status ) ) {
1154 foreach ( $order_status as $key => $status ) {
1155 $options[] = [
1156 'label' => $status,
1157 'value' => $key,
1158 ];
1159 }
1160 }
1161
1162 return [
1163 'options' => $options,
1164 'hasMore' => false,
1165 ];
1166 }
1167
1168 /**
1169 * Prepare WooCommerce Country List.
1170 *
1171 * @param array $data Search Params.
1172 * @return array
1173 */
1174 public function search_woo_country_list( $data ) {
1175 $countries = WC()->countries->get_countries();
1176 $options = [];
1177
1178 if ( ! empty( $countries ) ) {
1179 foreach ( $countries as $key => $country ) {
1180 $options[] = [
1181 'label' => $country,
1182 'value' => $key,
1183 ];
1184 }
1185 }
1186
1187 return [
1188 'options' => $options,
1189 'hasMore' => false,
1190 ];
1191 }
1192
1193 /**
1194 * Prepare WooCommerce Country States List.
1195 *
1196 * @param array $data Search Params.
1197 * @return array
1198 */
1199 public function search_woo_country_state_list( $data ) {
1200 if ( ! empty( $data['dynamic']['shipping_country'] ) ) {
1201 $cc = $data['dynamic']['shipping_country'];
1202 } else {
1203 $cc = $data['dynamic'];
1204 }
1205
1206 $states = WC()->countries->get_states( $cc );
1207
1208 $options = [];
1209 if ( ! empty( $states ) ) {
1210 foreach ( $states as $key => $state ) {
1211 $options[] = [
1212 'label' => $state,
1213 'value' => $key,
1214 ];
1215 }
1216 }
1217
1218 return [
1219 'options' => $options,
1220 'hasMore' => false,
1221 ];
1222 }
1223
1224 /**
1225 * Get Memberpress gatways (payment methods) for subscription.
1226 *
1227 * @param array $data QueryParams.
1228 *
1229 * @return array
1230 * @since 1.0.0
1231 */
1232 public function search_memberpress_gayways( $data ) {
1233 if ( ! class_exists( 'MeprOptions' ) || ! class_exists( 'MeprBaseRealGateway' ) ) {
1234 return [];
1235 }
1236 $mp_options = MeprOptions::fetch();
1237
1238 $pms = array_keys( $mp_options->integrations );
1239 $gateways = [];
1240
1241 foreach ( $pms as $pm_id ) {
1242 $obj = $mp_options->payment_method( $pm_id );
1243 if ( $obj instanceof MeprBaseRealGateway ) {
1244 $gateways[] = [
1245 'label' => sprintf( '%1$s (%2$s)', $obj->label, $obj->name ),
1246 'value' => $obj->id,
1247 ];
1248 }
1249 }
1250
1251 return [
1252 'options' => $gateways,
1253 'hasMore' => false,
1254 ];
1255 }
1256
1257 /**
1258 * Prepare roles.
1259 *
1260 * @param array $data data.
1261 *
1262 * @return array
1263 */
1264 public function search_roles( $data ) {
1265 $roles = wp_roles()->roles;
1266 $options = [];
1267 foreach ( $roles as $role => $details ) {
1268
1269 $options[] = [
1270 'label' => $details['name'],
1271 'value' => $role,
1272 ];
1273
1274 }
1275
1276 return [
1277 'options' => $options,
1278 'hasMore' => false,
1279 ];
1280 }
1281
1282 /**
1283 * Prepare post types.
1284 *
1285 * @param array $data data.
1286 *
1287 * @return array
1288 */
1289 public function search_post_types( $data ) {
1290 $post_types = get_post_types( [ 'public' => true ], 'objects' );
1291 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
1292 if ( isset( $post_types['attachment'] ) ) {
1293 unset( $post_types['attachment'] );
1294 }
1295
1296 $options = [];
1297 foreach ( $post_types as $post_type => $details ) {
1298 $options[] = [
1299 'label' => $details->label,
1300 'value' => $post_type,
1301 ];
1302 }
1303
1304 return [
1305 'options' => $options,
1306 'hasMore' => false,
1307 ];
1308 }
1309
1310 /**
1311 * Get post statuses.
1312 *
1313 * @param array $data data.
1314 *
1315 * @return array
1316 */
1317 public function search_post_statuses( $data ) {
1318 $post_statuses = get_post_stati( [], 'objects' );
1319 $post_statuses = apply_filters( 'suretriggers_post_types', $post_statuses );
1320 $options = [];
1321
1322 foreach ( $post_statuses as $post_status => $details ) {
1323 if ( 'woocommerce' === $details->label_count['domain'] ) {
1324 $options[] = [
1325 'label' => 'WooCommerce - ' . $details->label,
1326 'value' => $post_status,
1327 ];
1328 } else {
1329 $options[] = [
1330 'label' => $details->label,
1331 'value' => $post_status,
1332 ];
1333 }
1334 }
1335 return [
1336 'options' => $options,
1337 'hasMore' => false,
1338 ];
1339 }
1340
1341 /**
1342 * Get Taxonomies.
1343 *
1344 * @param array $data data.
1345 *
1346 * @return array
1347 */
1348 public function search_taxonomy_list( $data ) {
1349 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
1350 $options = [];
1351 $options[0] = [
1352 'label' => 'Any Taxonomy',
1353 'value' => -1,
1354 ];
1355
1356 foreach ( $taxonomies as $taxonomy => $taxonomy_obj ) {
1357 $options[] = [
1358 'label' => $taxonomy_obj->label,
1359 'value' => $taxonomy_obj->name,
1360 ];
1361 }
1362
1363 return [
1364 'options' => $options,
1365 'hasMore' => false,
1366 ];
1367 }
1368
1369 /**
1370 * Get Taxonomies without any option.
1371 *
1372 * @param array $data data.
1373 *
1374 * @return array
1375 */
1376 public function search_taxonomy_list_without_any( $data ) {
1377 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
1378 $options = [];
1379 foreach ( $taxonomies as $taxonomy => $taxonomy_obj ) {
1380 $options[] = [
1381 'label' => $taxonomy_obj->label,
1382 'value' => $taxonomy_obj->name,
1383 ];
1384 }
1385
1386 return [
1387 'options' => $options,
1388 'hasMore' => false,
1389 ];
1390 }
1391
1392 /**
1393 * Get WPForms.
1394 *
1395 * @param array $data data.
1396 *
1397 * @return array
1398 */
1399 public function search_wp_forms( $data ) {
1400 if ( ! class_exists( 'WPForms_Form_Handler' ) ) {
1401 return [];
1402 }
1403
1404 $wpforms = new WPForms_Form_Handler();
1405 $forms = $wpforms->get( '', [ 'orderby' => 'title' ] );
1406 $options = [];
1407
1408 if ( ! empty( $forms ) ) {
1409 foreach ( $forms as $form ) {
1410 $options[] = [
1411 'label' => $form->post_title,
1412 'value' => $form->ID,
1413 ];
1414 }
1415 }
1416
1417 return [
1418 'options' => $options,
1419 'hasMore' => false,
1420 ];
1421 }
1422
1423 /**
1424 * Get Gravity Forms.
1425 *
1426 * @param array $data data.
1427 *
1428 * @return array
1429 */
1430 public function search_gravity_forms( $data ) {
1431 if ( ! class_exists( 'GFFormsModel' ) ) {
1432 return [];
1433 }
1434
1435 $forms = GFFormsModel::get_forms();
1436 $options = [];
1437
1438 if ( ! empty( $forms ) ) {
1439 foreach ( $forms as $form ) {
1440 $options[] = [
1441 'label' => $form->title,
1442 'value' => $form->id,
1443 ];
1444 }
1445 }
1446
1447 return [
1448 'options' => $options,
1449 'hasMore' => false,
1450 ];
1451 }
1452
1453 /**
1454 * Get tag & contact details.
1455 *
1456 * @param array $data data.
1457 *
1458 * @return array
1459 */
1460 public function search_pluggables_fluentcrm_contact_added_to_tags( $data ) {
1461 $context = [];
1462 $pluggable_data = [];
1463 $tag_id = $data['filter'];
1464
1465 if ( ! class_exists( 'FluentCrm\App\Models\Subscriber' ) || ! class_exists( 'FluentCrm\App\Models\Tag' ) ) {
1466 return [];
1467 }
1468
1469 if ( $tag_id > 0 ) {
1470 $tags = Tag::where( 'id', $tag_id )->first();
1471 } else {
1472 $tags = Tag::orderBy( 'id', 'DESC' )->first();
1473 }
1474 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1475 if ( $contact ) {
1476 $pluggable_data['contact'] = $contact;
1477 $context['tag_id'] = $tag_id;
1478 $pluggable_data['tag'] = $tags;
1479 $context['response_type'] = 'live';
1480 } else {
1481 $pluggable_data['conatct']['full_name'] = 'Test User';
1482 $pluggable_data['conatct']['first_name'] = 'Test';
1483 $pluggable_data['conatct']['last_name'] = 'User';
1484 $pluggable_data['conatct']['company_id'] = 112;
1485 $pluggable_data['conatct']['email'] = 'testuser@gmail.com';
1486 $pluggable_data['conatct']['address_line_1'] = '33, Vincent Road';
1487 $pluggable_data['conatct']['address_line_2'] = 'Chicago Street';
1488 $pluggable_data['conatct']['postal_code'] = '212342';
1489 $pluggable_data['conatct']['city'] = 'New York City';
1490 $pluggable_data['conatct']['state'] = 'New York';
1491 $pluggable_data['conatct']['country'] = 'USA';
1492 $pluggable_data['conatct']['phone'] = '9992191911';
1493 $pluggable_data['conatct']['status'] = 'subscribed';
1494 $pluggable_data['conatct']['contact_type'] = 'lead';
1495 $pluggable_data['conatct']['source'] = '';
1496 $pluggable_data['conatct']['date_of_birth'] = '2022-11-09';
1497 $context['tag_id'] = 1;
1498 $pluggable_data['tag'] =
1499 [
1500 'id' => '1',
1501 'title' => 'new',
1502 'slug' => 'new',
1503 'description' => null,
1504 'created_at' => '2023-01-19 10:23:23',
1505 'updated_at' => '2023-01-19 10:23:23',
1506 'pivot' => [
1507 'subscriber_id' => '1',
1508 'object_id' => '1',
1509 'object_type' => 'FluentCrm\\App\\Models\\Tag',
1510 'created_at' => '2023-01-19 10:42:55',
1511 'updated_at' => '2023-01-19 10:42:55',
1512
1513 ],
1514 ];
1515 $context['response_type'] = 'sample';
1516 }
1517
1518 $context['pluggable_data'] = $pluggable_data;
1519 return $context;
1520 }
1521
1522 /**
1523 * Get FluentCRM contact details.
1524 *
1525 * @param array $data data.
1526 *
1527 * @return array
1528 */
1529 public function search_pluggables_fluentcrm_contact_added( $data ) {
1530 $context = [];
1531 $pluggable_data = [];
1532
1533 if ( ! class_exists( 'FluentCrm\App\Models\Subscriber' ) || ! class_exists( 'FluentCrm\App\Models\SubscriberMeta' ) ) {
1534 return [];
1535 }
1536 $contact = [];
1537 if ( 'status_set_to_specific_status' === $data['search_term'] ) {
1538 if ( '-1' === $data['filter']['status']['value'] ) {
1539 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1540 } else {
1541 $contact = Subscriber::where( 'status', $data['filter']['status']['value'] )->first();
1542 }
1543 } elseif ( 'new_contact_added' === $data['search_term'] ) {
1544 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1545 } elseif ( 'contact_updated' === $data['search_term'] ) {
1546 $contact = Subscriber::orderBy( 'updated_at', 'DESC' )->first();
1547 } elseif ( 'contact_field_updated' === $data['search_term'] ) {
1548 if ( '-1' === $data['filter']['field_id']['value'] ) {
1549 $contact = SubscriberMeta::where( 'object_type', 'custom_field' )->orderBy( 'updated_at', 'DESC' )->first();
1550 } else {
1551 $contact = SubscriberMeta::where( 'key', $data['filter']['field_id']['value'] )->orderBy( 'updated_at', 'DESC' )->first();
1552 }
1553 $contact = Subscriber::where( 'id', $contact->subscriber_id )->first();
1554 }
1555
1556 if ( $contact ) {
1557 $subscriber = Subscriber::with( [ 'tags', 'lists' ] )->find( $contact->id );
1558 $customer_fields = $subscriber->custom_fields();
1559 $pluggable_data['contact']['details'] = $subscriber;
1560 $pluggable_data['contact']['custom'] = $customer_fields;
1561 $pluggable_data['field_id'] = $data['filter']['field_id']['value'];
1562 $context['response_type'] = 'live';
1563 } else {
1564 $pluggable_data['contact']['details']['full_name'] = 'Test User';
1565 $pluggable_data['contact']['details']['first_name'] = 'Test';
1566 $pluggable_data['contact']['details']['last_name'] = 'User';
1567 $pluggable_data['contact']['details']['company_id'] = 112;
1568 $pluggable_data['contact']['details']['email'] = 'testuser@gmail.com';
1569 $pluggable_data['contact']['details']['address_line_1'] = '33, Vincent Road';
1570 $pluggable_data['contact']['details']['address_line_2'] = 'Chicago Street';
1571 $pluggable_data['contact']['details']['postal_code'] = '212342';
1572 $pluggable_data['contact']['details']['city'] = 'New York City';
1573 $pluggable_data['contact']['details']['state'] = 'New York';
1574 $pluggable_data['contact']['details']['country'] = 'USA';
1575 $pluggable_data['contact']['details']['phone'] = '9992191911';
1576 $pluggable_data['contact']['details']['status'] = 'subscribed';
1577 $pluggable_data['contact']['details']['contact_type'] = 'lead';
1578 $pluggable_data['contact']['details']['source'] = '';
1579 $pluggable_data['contact']['details']['date_of_birth'] = '2022-11-09';
1580 $context['response_type'] = 'sample';
1581 }
1582
1583 $context['pluggable_data'] = $pluggable_data;
1584 return $context;
1585 }
1586
1587 /**
1588 * Get contact added to list details.
1589 *
1590 * @param array $data data.
1591 *
1592 * @return array
1593 */
1594 public function search_pluggables_fluentcrm_contact_added_to_lists( $data ) {
1595 $context = [];
1596 $pluggable_data = [];
1597 $list_id = $data['filter'];
1598
1599 if ( ! class_exists( 'FluentCrm\App\Models\Lists' ) || ! class_exists( 'FluentCrm\App\Models\Subscriber' ) || ! function_exists( 'FluentCrmApi' ) ) {
1600 return [];
1601 }
1602
1603 $contact_api = FluentCrmApi( 'contacts' );
1604 if ( -1 === $list_id ) {
1605 $lists = Lists::orderBy( 'id', 'DESC' )->first();
1606 $list_id = $lists->id;
1607 } else {
1608 $lists = Lists::where( 'id', $list_id )->first();
1609 }
1610 $list_ids = [ $list_id ];
1611 $list_contacts = $contact_api->getInstance()
1612 ->filterByLists( $list_ids )
1613 ->orderBy( 'id', 'DESC' )
1614 ->first();
1615
1616 $contact = Subscriber::where( 'id', $list_contacts->id )->get();
1617 if ( $contact ) {
1618 $pluggable_data['contact'] = $contact[0];
1619 $pluggable_data['list_id'] = $list_id;
1620 $pluggable_data['list'] = $lists;
1621 $context['response_type'] = 'live';
1622 } else {
1623 $pluggable_data['conatct']['id'] = 6;
1624 $pluggable_data['conatct']['prefix'] = 'Mr';
1625 $pluggable_data['conatct']['full_name'] = 'John Doe';
1626 $pluggable_data['conatct']['first_name'] = 'John';
1627 $pluggable_data['conatct']['last_name'] = 'Doe';
1628 $pluggable_data['conatct']['company_id'] = 112;
1629 $pluggable_data['conatct']['email'] = 'johnde@gmail.com';
1630 $pluggable_data['conatct']['address_line_1'] = '33, Vincent Road';
1631 $pluggable_data['conatct']['address_line_2'] = 'Chicago Street';
1632 $pluggable_data['conatct']['postal_code'] = '212342';
1633 $pluggable_data['conatct']['city'] = 'New York City';
1634 $pluggable_data['conatct']['state'] = 'New York';
1635 $pluggable_data['conatct']['country'] = 'USA';
1636 $pluggable_data['conatct']['phone'] = '9992191911';
1637 $pluggable_data['conatct']['status'] = 'subscribed';
1638 $pluggable_data['conatct']['contact_type'] = 'lead';
1639 $pluggable_data['conatct']['source'] = '';
1640 $pluggable_data['conatct']['date_of_birth'] = '2022-11-09';
1641 $context['list_id'] = 1;
1642 $pluggable_data['list'] =
1643 [
1644 'id' => '1',
1645 'title' => 'new',
1646 'slug' => 'new',
1647 'description' => null,
1648 'created_at' => '2023-01-19 10:23:23',
1649 'updated_at' => '2023-01-19 10:23:23',
1650 ];
1651 $context['response_type'] = 'sample';
1652 }
1653
1654 $context['pluggable_data'] = $pluggable_data;
1655 return $context;
1656 }
1657
1658 /**
1659 * Prepare fluentcrm campaigns.
1660 *
1661 * @param array $data Search Params.
1662 *
1663 * @return array
1664 */
1665 public function search_fluentcrm_campaigns( $data ) {
1666
1667 $options = [];
1668 global $wpdb;
1669
1670 $campaigns = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fc_campaigns ORDER BY id DESC", ARRAY_A );
1671
1672 if ( ! empty( $campaigns ) ) {
1673 foreach ( $campaigns as $campaign ) {
1674 $options[] = [
1675 'label' => $campaign['title'],
1676 'value' => $campaign['id'],
1677 ];
1678 }
1679 }
1680
1681 return [
1682 'options' => $options,
1683 'hasMore' => false,
1684 ];
1685 }
1686
1687 /**
1688 * Get Divi Forms.
1689 *
1690 * @param array $data data.
1691 *
1692 * @return array
1693 */
1694 public function search_divi_forms( $data ) {
1695 $form_posts = Utilities::get_divi_forms();
1696 $options = [];
1697
1698 if ( empty( $form_posts ) ) {
1699 return [
1700 'options' => [],
1701 'hasMore' => false,
1702 ];
1703 }
1704
1705 foreach ( (array) $form_posts as $form_post ) {
1706 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
1707 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
1708 if ( empty( $forms ) ) {
1709 continue;
1710 }
1711
1712 $count = 0;
1713
1714 foreach ( $forms as $form ) {
1715 $pattern_form = get_shortcode_regex( [ 'et_pb_contact_form' ] );
1716 preg_match_all( "/$pattern_form/", $form[0], $forms_extracted, PREG_SET_ORDER );
1717
1718 if ( empty( $forms_extracted ) ) {
1719 continue;
1720 }
1721
1722 foreach ( $forms_extracted as $form_extracted ) {
1723 $form_attrs = shortcode_parse_atts( $form_extracted[3] );
1724 $form_id = isset( $form_attrs['_unique_id'] ) ? $form_attrs['_unique_id'] : '';
1725 if ( empty( $form_id ) ) {
1726 continue;
1727 }
1728 $form_id = sprintf( '%d-%s', $form_post['ID'], $form_id );
1729 $form_title = isset( $form_attrs['title'] ) ? $form_attrs['title'] : '';
1730 $form_title = sprintf( '%s %s', $form_post['post_title'], $form_title );
1731 $options[] = [
1732 'label' => $form_title,
1733 'value' => $form_id,
1734 ];
1735 }
1736 $count++;
1737 }
1738 }
1739
1740 return [
1741 'options' => $options,
1742 'hasMore' => false,
1743 ];
1744 }
1745
1746 /**
1747 * Get Comment Pluggable data.
1748 *
1749 * @param array $data data.
1750 *
1751 * @return array
1752 */
1753 public function search_pluggables_wp_insert_comment( $data ) {
1754 $context = [];
1755 $args = [
1756 'number' => 1,
1757 'status' => 'approve',
1758 'post_type' => $data['filter']['post_type']['value'],
1759 ];
1760
1761 if ( isset( $data['filter']['post']['value'] ) ) {
1762 $post_id = $data['filter']['post']['value'];
1763 if ( $post_id > 0 ) {
1764 $args['post_id'] = $post_id;
1765 }
1766 }
1767
1768 $comments = get_comments( $args );
1769
1770 if ( empty( $comments ) ) {
1771 unset( $args['post_id'] );
1772 $comments = get_comments( $args );
1773 }
1774
1775 $context['context_data'] = $data;
1776 $context['context_args'] = $args;
1777
1778 if ( ! empty( $comments ) && is_array( $comments ) ) {
1779 foreach ( $comments as $comment ) :
1780 if ( is_object( $comment ) ) {
1781 $comment = get_object_vars( $comment );
1782 }
1783
1784 if ( is_array( $comment ) && isset( $comment['comment_post_ID'] ) && is_numeric( $comment['comment_post_ID'] ) ) {
1785 $post_id = absint( $comment['comment_post_ID'] );
1786 $post = get_post( $post_id );
1787
1788 if ( is_object( $post ) ) {
1789 $postauthor = (int) $post->post_author;
1790
1791 $context['pluggable_data'] = [
1792 'post' => $post_id,
1793 'post_title' => $post->post_title,
1794 'post_author' => get_the_author_meta( 'display_name', $postauthor ),
1795 'post_link' => get_the_permalink( $post_id ),
1796 'comment_id' => $comment['comment_ID'],
1797 'comment' => $comment['comment_content'],
1798 'comment_author' => $comment['comment_author'],
1799 'comment_author_email' => $comment['comment_author_email'],
1800 'comment_date' => $comment['comment_date'],
1801 ];
1802 }
1803 }
1804
1805 if ( is_array( $comment ) && isset( $comment['comment_author_email'] ) ) {
1806 $user_email = $comment['comment_author_email'];
1807 /**
1808 *
1809 * Ignore line
1810 *
1811 * @phpstan-ignore-next-line
1812 */
1813 $user = get_user_by( 'email', $user_email );
1814
1815 if ( $user ) {
1816
1817 $context['pluggable_data']['wp_user_id'] = $user->ID;
1818 $context['pluggable_data']['user_login'] = $user->user_login;
1819 $context['pluggable_data']['display_name'] = $user->display_name;
1820 $context['pluggable_data']['user_firstname'] = $user->user_firstname;
1821 $context['pluggable_data']['user_lastname'] = $user->user_lastname;
1822 $context['pluggable_data']['user_email'] = $user->user_email;
1823 $context['pluggable_data']['user_role'] = $user->roles;
1824 }
1825 }
1826
1827 $context['response_type'] = 'live';
1828 endforeach;
1829 } else {
1830
1831 $sample_comment = [
1832 'post' => 100,
1833 'post_title' => 'Sample Post',
1834 'comment_id' => 101,
1835 'comment' => 'Sample Comment',
1836 'post_author' => 'Sample Author',
1837 'post_link' => 'https://example.com/sample-post',
1838 'comment_author' => 'Test User',
1839 'comment_author_email' => 'testuser@example.com',
1840 'comment_date' => '2024-01-01 10:00:00',
1841 ];
1842 $sample_comment['wp_user_id'] = 7;
1843 $sample_comment['user_login'] = 'testuser@gmail.com';
1844 $sample_comment['display_name'] = 'Test User';
1845 $sample_comment['user_firstname'] = 'Test';
1846 $sample_comment['user_lastname'] = 'User';
1847 $sample_comment['user_email'] = 'testuser@gmail.com';
1848 $sample_comment['user_role'] = [ 'Subscriber' ];
1849
1850 $context['pluggable_data'] = $sample_comment;
1851 $context['response_type'] = 'sample';
1852 }
1853
1854 return $context;
1855 }
1856
1857
1858 /**
1859 * User reset password.
1860 *
1861 * @param array $data data.
1862 * @return array
1863 */
1864 public function search_pluggables_user_reset_password( $data ) {
1865 $user_context = $this->search_pluggables_add_user_role( $data );
1866 $user_context['pluggable_data']['new_password'] = '***password***';
1867 return $user_context;
1868 }
1869
1870 /**
1871 * User pluggable data.
1872 *
1873 * @param array $data data.
1874 * @return array
1875 */
1876 public function search_pluggables_add_user_role( $data ) {
1877 $context = [];
1878 $args = [
1879 'order' => 'DESC',
1880 'number' => 1,
1881 'orderby' => 'ID',
1882 ];
1883
1884 if ( isset( $data['filter']['role']['value'] ) ) {
1885 $role = $data['filter']['role']['value'];
1886 $args['role'] = $role;
1887 }
1888 if ( isset( $data['filter']['new_role']['value'] ) ) {
1889 $role = $data['filter']['new_role']['value'];
1890 $args['role'] = $role;
1891 }
1892
1893 $users = get_users( $args );
1894
1895 if ( isset( $data['filter']['meta_key']['value'] ) ) {
1896 $meta_key = $data['filter']['meta_key']['value'];
1897 $args['st_meta_key'] = $meta_key;
1898 }
1899
1900 if ( isset( $data['filter']['profile_field']['value'] ) ) {
1901 $meta_key = $data['filter']['profile_field']['value'];
1902 $args['profile_field'] = $meta_key;
1903 }
1904
1905 if ( ! empty( $users ) ) {
1906 $user = $users[0];
1907 $pluggable_data = [];
1908 $pluggable_data['wp_user_id'] = $user->ID;
1909 $pluggable_data['user_login'] = $user->user_login;
1910 $pluggable_data['display_name'] = $user->display_name;
1911 $pluggable_data['user_firstname'] = $user->user_firstname;
1912 $pluggable_data['user_lastname'] = $user->user_lastname;
1913 $pluggable_data['user_email'] = $user->user_email;
1914 $pluggable_data['user_registered'] = $user->user_registered;
1915 $pluggable_data['user_role'] = $user->roles;
1916 if ( isset( $args['st_meta_key'] ) ) {
1917 $pluggable_data['meta_key'] = $args['st_meta_key'];
1918 $pluggable_data['meta_value'] = get_user_meta( $user->ID, $args['st_meta_key'], true );
1919 }
1920 if ( isset( $args['profile_field'] ) ) {
1921 $userdata = get_userdata( $user->ID );
1922 if ( $userdata instanceof \WP_User ) {
1923 $userdata = json_decode( (string) wp_json_encode( $userdata->data ), true );
1924
1925 $pluggable_data['profile_field'] = $args['profile_field'];
1926 if ( is_array( $userdata ) && is_string( $args['profile_field'] ) ) {
1927 $pluggable_data['profile_field_value'] = $userdata[ $args['profile_field'] ];
1928 }
1929 }
1930 }
1931 $context['pluggable_data'] = $pluggable_data;
1932 $context['response_type'] = 'live';
1933 } else {
1934 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
1935 $context['pluggable_data'] = [
1936 'wp_user_id' => 1,
1937 'user_login' => 'admin',
1938 'display_name' => 'Test User',
1939 'user_firstname' => 'Test',
1940 'user_lastname' => 'User',
1941 'user_email' => 'testuser@gmail.com',
1942 'user_registered' => '2024-06-18 09:47:58',
1943 'user_role' => [ $role ],
1944 ];
1945 if ( isset( $args['st_meta_key'] ) ) {
1946 $context['pluggable_data']['meta_key'] = $args['st_meta_key'];
1947 $context['pluggable_data']['meta_value'] = 'test meta value';
1948 }
1949 if ( isset( $args['profile_field'] ) ) {
1950 $context['pluggable_data']['profile_field'] = $args['profile_field'];
1951 $context['pluggable_data']['profile_field_value'] = 'Profile Field Value';
1952 }
1953 $context['response_type'] = 'sample';
1954 }
1955 return $context;
1956 }
1957
1958 /**
1959 * User pluggable data.
1960 *
1961 * @param array $data data.
1962 * @return array
1963 */
1964 public function search_pluggables_last_user_login( $data ) {
1965 $context = [];
1966 $args = [
1967 'orderby' => 'meta_value',
1968 'meta_key' => 'st_last_login',
1969 'order' => 'DESC',
1970 'number' => 1,
1971 ];
1972 $users = get_users( $args );
1973
1974 if ( ! empty( $users ) ) {
1975 $user = $users[0];
1976 $pluggable_data = [];
1977 $pluggable_data['wp_user_id'] = $user->ID;
1978 $pluggable_data['user_login'] = $user->user_login;
1979 $pluggable_data['display_name'] = $user->display_name;
1980 $pluggable_data['user_firstname'] = $user->user_firstname;
1981 $pluggable_data['user_lastname'] = $user->user_lastname;
1982 $pluggable_data['user_email'] = $user->user_email;
1983 $pluggable_data['user_role'] = $user->roles;
1984
1985 $context['pluggable_data'] = $pluggable_data;
1986 $context['response_type'] = 'live';
1987 } else {
1988 $role = 'subscriber';
1989 $context['pluggable_data'] = [
1990 'wp_user_id' => 1,
1991 'user_login' => 'admin',
1992 'display_name' => 'Test User',
1993 'user_firstname' => 'Test',
1994 'user_lastname' => 'User',
1995 'user_email' => 'testuser@gmail.com',
1996 'user_role' => [ $role ],
1997 ];
1998 $context['response_type'] = 'sample';
1999 }
2000 return $context;
2001 }
2002
2003 /**
2004 * Donation pluggable data.
2005 *
2006 * @param array $data data.
2007 * @return array
2008 */
2009 public function search_pluggables_wordpress_post( $data ) {
2010 $context = [];
2011 $args = [
2012 'post_type' => 'any',
2013 'posts_per_page' => 1,
2014 'orderby' => 'modified',
2015 'order' => 'DESC',
2016 ];
2017
2018 if ( isset( $data['filter']['post_type']['value'] ) ) {
2019 $post_type = $data['filter']['post_type']['value'];
2020 $args['post_type'] = $post_type;
2021 }
2022
2023 if ( isset( $data['filter']['status']['value'] ) ) {
2024 $post_status = $data['filter']['status']['value'];
2025 $args['post_status'] = $post_status;
2026 }
2027
2028 if ( isset( $data['filter']['post_status']['value'] ) ) {
2029 $post_status = $data['filter']['post_status']['value'];
2030 $args['post_status'] = $post_status;
2031 }
2032
2033 if ( isset( $data['filter']['post']['value'] ) ) {
2034 $post_id = $data['filter']['post']['value'];
2035 if ( $post_id > 0 ) {
2036 $args['p'] = $post_id;
2037 unset( $args['post_status'] );
2038 }
2039 }
2040
2041 $posts = get_posts( $args );
2042 if ( ! empty( $posts ) ) {
2043 $context['pluggable_data'] = $posts[0];
2044 $custom_metas = get_post_meta( $posts[0]->ID );
2045 if ( property_exists( $context['pluggable_data'], 'post' ) ) {
2046 $context['pluggable_data']->post = $posts[0]->ID;
2047 }
2048 if ( is_object( $context['pluggable_data'] ) ) {
2049 $context['pluggable_data'] = get_object_vars( $context['pluggable_data'] );
2050 }
2051 $context['pluggable_data']['featured_image'] = wp_get_attachment_image_src( (int) get_post_thumbnail_id( $posts[0]->ID ), 'full' )[0]; // @phpstan-ignore-line
2052 $context['pluggable_data']['permalink'] = get_permalink( $posts[0]->ID );
2053 if ( $posts[0] instanceof WP_Post ) {
2054 $taxonomies = get_object_taxonomies( get_post( $posts[0] ), 'objects' );
2055 if ( ! empty( $taxonomies ) ) {
2056 foreach ( $taxonomies as $taxonomy => $taxonomy_object ) {
2057 $terms = get_the_terms( $posts[0]->ID, $taxonomy );
2058 if ( ! empty( $terms ) && is_array( $terms ) ) {
2059 foreach ( $terms as $term ) {
2060 $context['pluggable_data'][ $taxonomy ] = $term->name;
2061 }
2062 }
2063 }
2064 }
2065 }
2066 $context['pluggable_data'] = array_merge( $context['pluggable_data'], is_numeric( $posts[0]->post_author ) ? WordPress::get_user_context( (int) $posts[0]->post_author ) : [] );
2067 $context['pluggable_data']['post'] = $posts[0]->ID;
2068 $context['pluggable_data']['custom_metas'] = $custom_metas;
2069 $context['response_type'] = 'live';
2070 } else {
2071 $context['pluggable_data'] = [
2072 'ID' => 557,
2073 'post' => 557,
2074 'post_author' => 1,
2075 'post_date' => '2022-11-18 12:18:14',
2076 'post_date_gmt' => '2022-11-18 12:18:14',
2077 'post_content' => 'Test Post Content',
2078 'post_title' => 'Test Post',
2079 'post_excerpt' => '',
2080 'post_status' => 'published',
2081 'comment_status' => 'open',
2082 'ping_status' => 'open',
2083 'post_password' => '',
2084 'post_name' => 'test-post',
2085 'to_ping' => '',
2086 'pinged' => '',
2087 'post_modified' => '2022-11-18 12:18:14',
2088 'post_modified_gmt' => '2022-11-18 12:18:14',
2089 'post_content_filtered' => '',
2090 'post_parent' => 0,
2091 'guid' => 'https://abc.com/?p=1',
2092 'permalink' => 'https://abc.com/test-post/',
2093 'menu_order' => 0,
2094 'post_type' => 'post',
2095 'post_mime_type' => '',
2096 'comment_count' => 0,
2097 'filter' => 'raw',
2098 'featured_image' => 'https://abc.com/test-post/',
2099 ];
2100 $context['response_type'] = 'sample';
2101 }
2102
2103 return $context;
2104 }
2105
2106 /**
2107 * Donation pluggable data
2108 *
2109 * @param array $data data.
2110 * @return array
2111 */
2112 public function search_pluggables_givewp_donation_via_form( $data ) {
2113 global $wpdb;
2114 $context = [];
2115 $pluggable_data = [];
2116 $form_id = $data['filter']['form_id']['value'];
2117 $subscription_id = 1;
2118
2119 if ( ! class_exists( 'Give_Payment' ) || ! class_exists( 'Give_Subscription' ) ) {
2120 return [];
2121 }
2122
2123 if ( ! function_exists( 'give_get_donor_donation_comment' ) ) {
2124 return [];
2125 }
2126
2127 if ( 'donation_specific_field_value' == $data['search_term'] ) {
2128 $field_id = $data['filter']['field_id']['value'];
2129 $field_value = $data['filter']['field_value']['value'];
2130
2131 $donation_meta = $wpdb->get_row(
2132 $wpdb->prepare(
2133 "SELECT * FROM {$wpdb->prefix}give_donationmeta
2134 WHERE meta_key LIKE %s AND
2135 meta_value LIKE %s
2136 ORDER BY donation_id
2137 DESC LIMIT 1",
2138 $field_id,
2139 $field_value
2140 )
2141 );
2142 if ( $donation_meta ) {
2143 $donation_form_meta = $wpdb->get_row(
2144 $wpdb->prepare(
2145 "SELECT * FROM {$wpdb->prefix}give_donationmeta
2146 WHERE donation_id = %d AND
2147 meta_key LIKE %s AND
2148 meta_value LIKE %d
2149 ORDER BY donation_id
2150 DESC LIMIT 1",
2151 $donation_meta->donation_id,
2152 '_give_payment_form_id',
2153 $form_id
2154 )
2155 );
2156
2157 $payment = $wpdb->get_row(
2158 $wpdb->prepare(
2159 "SELECT * FROM {$wpdb->prefix}posts WHERE
2160 ID=%d ORDER BY ID DESC LIMIT 1",
2161 $donation_form_meta->donation_id
2162 )
2163 );
2164 }
2165 } elseif ( 'donation_specific_amount' == $data['search_term'] ) {
2166 $condition_compare = $data['filter']['condition_compare']['value'];
2167 $donation_amount = $data['filter']['donation_amount']['value'];
2168
2169 $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
2170
2171 if ( $donation_meta ) {
2172 $donation_form_meta = $wpdb->get_row(
2173 $wpdb->prepare(
2174 "SELECT * FROM {$wpdb->prefix}give_donationmeta
2175 WHERE donation_id = %d AND
2176 meta_key LIKE %s AND
2177 meta_value LIKE %d
2178 ORDER BY donation_id
2179 DESC LIMIT 1",
2180 $donation_meta->donation_id,
2181 '_give_payment_form_id',
2182 $form_id
2183 )
2184 );
2185
2186 $payment = $wpdb->get_row(
2187 $wpdb->prepare(
2188 "SELECT * FROM {$wpdb->prefix}posts WHERE
2189 ID=%d ORDER BY ID DESC LIMIT 1",
2190 $donation_form_meta->donation_id
2191 )
2192 );
2193 }
2194 } elseif ( 'cancels_recurring_donation' == $data['search_term'] ) {
2195 $donation_meta = $wpdb->get_row(
2196 $wpdb->prepare(
2197 "SELECT * FROM {$wpdb->prefix}give_subscriptions
2198 WHERE product_id=%d AND
2199 status LIKE %s
2200 ORDER BY id
2201 DESC LIMIT 1",
2202 $form_id,
2203 'cancelled'
2204 )
2205 );
2206 if ( $donation_meta ) {
2207 $subscription_id = $donation_meta->id;
2208 $payment = $wpdb->get_row(
2209 $wpdb->prepare(
2210 "SELECT * FROM {$wpdb->prefix}posts WHERE
2211 ID=%d ORDER BY ID DESC LIMIT 1",
2212 $donation_meta->parent_payment_id
2213 )
2214 );
2215 }
2216 } elseif ( 'continues_recurring_donation' == $data['search_term'] ) {
2217 $donation_meta = $wpdb->get_row(
2218 $wpdb->prepare(
2219 "SELECT * FROM {$wpdb->prefix}give_subscriptions
2220 WHERE product_id=%d AND
2221 status LIKE %s
2222 ORDER BY id
2223 DESC LIMIT 1",
2224 $form_id,
2225 'active'
2226 )
2227 );
2228 if ( $donation_meta ) {
2229 $subscription_id = $donation_meta->id;
2230
2231 $payment = $wpdb->get_row(
2232 $wpdb->prepare(
2233 "SELECT * FROM {$wpdb->prefix}posts
2234 WHERE post_parent=%d AND post_status LIKE %s
2235 ORDER BY ID DESC LIMIT 1",
2236 $donation_meta->parent_payment_id,
2237 'give_subscription'
2238 )
2239 );
2240 }
2241 } else {
2242 $payment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type=%s ORDER BY id DESC LIMIT 1", 'give_payment' ) );
2243 }
2244
2245 if ( ! empty( $payment ) ) {
2246 if ( 'continues_recurring_donation' == $data['search_term'] || 'cancels_recurring_donation' == $data['search_term'] ) {
2247 $subscription = new Give_Subscription( $subscription_id );
2248
2249 $pluggable_data['form_id'] = $form_id;
2250 $pluggable_data['subscription'] = $subscription;
2251 } elseif ( 'donation_specific_field_value' == $data['search_term'] ) {
2252 $payment_data = new Give_Payment( $payment->ID );
2253 $input_array = $payment_data->payment_meta;
2254 unset( $input_array['user_info'] );
2255 $pluggable_data = $input_array;
2256 foreach ( $input_array as $key => $value ) {
2257 $pluggable_data['field_id'] = $key;
2258 $pluggable_data['field_value'] = $value;
2259 }
2260 } else {
2261 $payment = new Give_Payment( $payment->ID );
2262 $address_data = $payment->address;
2263
2264 $pluggable_data['first_name'] = $payment->first_name;
2265 $pluggable_data['last_name'] = $payment->last_name;
2266 $pluggable_data['email'] = $payment->email;
2267 $pluggable_data['currency'] = $payment->currency;
2268 $pluggable_data['donated_amount'] = $payment->subtotal;
2269 $pluggable_data['donation_amount'] = $payment->subtotal;
2270 $pluggable_data['form_id'] = (int) $payment->form_id;
2271 $pluggable_data['form_title'] = $payment->form_title;
2272 $pluggable_data['name_title_prefix'] = $payment->title_prefix;
2273 $pluggable_data['date'] = $payment->date;
2274
2275 if ( is_array( $address_data ) ) {
2276 $pluggable_data['address_line_1'] = $address_data['line1'];
2277 $pluggable_data['address_line_2'] = $address_data['line2'];
2278 $pluggable_data['city'] = $address_data['city'];
2279 $pluggable_data['state'] = $address_data['state'];
2280 $pluggable_data['zip'] = $address_data['zip'];
2281 $pluggable_data['country'] = $address_data['country'];
2282 }
2283 // Payment meta.
2284 $payment_meta = $payment->get_meta();
2285 if ( is_array( $payment_meta ) && isset( $payment_meta['user_info'] ) ) {
2286 unset( $payment_meta['user_info'] );
2287 }
2288 $pluggable_data['payment_meta'] = $payment_meta;
2289 $donor_comment = give_get_donor_donation_comment( $payment->ID, $payment->donor_id );
2290 $pluggable_data['comment'] = ( is_array( $donor_comment ) && isset( $donor_comment['comment_content'] ) ) ? $donor_comment : '';
2291 }
2292
2293 $context['response_type'] = 'live';
2294 } else {
2295 if ( 'continues_recurring_donation' == $data['search_term'] || 'cancels_recurring_donation' == $data['search_term'] ) {
2296 $pluggable_data['form_id'] = $form_id;
2297 $pluggable_data['subscription']['id'] = 3;
2298 $pluggable_data['subscription']['donor_id'] = 8;
2299 $pluggable_data['subscription']['period'] = 'month';
2300 $pluggable_data['subscription']['initial_amount'] = '25.0000000000';
2301 $pluggable_data['subscription']['recurring_amount'] = '25.0000000000';
2302 $pluggable_data['subscription']['recurring_fee_amount'] = '0.0000000000';
2303 $pluggable_data['subscription']['transaction_id'] = 'a228ec9c6357963d23079d7d6945dd61';
2304 $pluggable_data['subscription']['parent_payment_id'] = '7492';
2305 $pluggable_data['subscription']['created'] = '2024-01-23 11:12:11';
2306 $pluggable_data['subscription']['expiration'] = '2024-03-23 23:59:59';
2307 $pluggable_data['subscription']['status'] = 'cancelled';
2308 $pluggable_data['subscription']['donor']['id'] = 8;
2309 $pluggable_data['subscription']['donor']['purchase_count'] = '3';
2310 $pluggable_data['subscription']['donor']['purchase_value'] = '75.000000';
2311 $pluggable_data['subscription']['donor']['email'] = 'johndoee@yopmail.com';
2312 $pluggable_data['subscription']['donor']['name'] = 'John Doe';
2313 $pluggable_data['subscription']['donor']['payment_ids'] = '7487,7492,7499';
2314 $pluggable_data['subscription']['donor']['user_id'] = '131';
2315 $pluggable_data['subscription']['customer_id'] = '8';
2316 } elseif ( 'donation_specific_field_value' == $data['search_term'] ) {
2317 $pluggable_data['form_id'] = 23;
2318 $pluggable_data['form_title'] = 'Demo Donation';
2319 $pluggable_data['_give_donor_billing_first_name'] = 'John';
2320 $pluggable_data['_give_donor_billing_last_name'] = 'Doe';
2321 $pluggable_data['_give_payment_donor_email'] = 'johndoee@gmail.com';
2322 $pluggable_data['_give_payment_currency'] = 'USD';
2323 $pluggable_data['_give_payment_total'] = '100';
2324 $pluggable_data['name_title_prefix'] = 'Mr';
2325 $pluggable_data['date'] = '2022-11-07 16:06:05';
2326 $pluggable_data['_give_donor_billing_address1'] = '33, Vincent Road';
2327 $pluggable_data['_give_donor_billing_address2'] = 'Chicago Street';
2328 $pluggable_data['_give_donor_billing_city'] = 'New York City';
2329 $pluggable_data['_give_donor_billing_state'] = 'New York';
2330 $pluggable_data['_give_donor_billing_zip'] = '223131';
2331 $pluggable_data['_give_donor_billing_country'] = 'USA';
2332 $pluggable_data['_give_donation_comment'] = 'Demo Comment';
2333 $pluggable_data['field_id'] = 'last_name';
2334 $pluggable_data['field_value'] = 'Doe';
2335 } else {
2336 $pluggable_data['first_name'] = 'John';
2337 $pluggable_data['last_name'] = 'Doe';
2338 $pluggable_data['email'] = 'johndoee@gmail.com';
2339 $pluggable_data['currency'] = 'USD';
2340 $pluggable_data['donated_amount'] = 100;
2341 $pluggable_data['donation_amount'] = 100;
2342 $pluggable_data['form_id'] = 23;
2343 $pluggable_data['form_title'] = 'Demo Donation';
2344 $pluggable_data['name_title_prefix'] = 'Mr';
2345 $pluggable_data['date'] = '2022-11-07 16:06:05';
2346 $pluggable_data['address_line_1'] = '33, Vincent Road';
2347 $pluggable_data['address_line_2'] = 'Chicago Street';
2348 $pluggable_data['city'] = 'New York City';
2349 $pluggable_data['state'] = 'New York';
2350 $pluggable_data['zip'] = '223131';
2351 $pluggable_data['country'] = 'USA';
2352 $pluggable_data['comment'] = 'Demo Comment';
2353 }
2354 $context['response_type'] = 'sample';
2355 }
2356
2357 $context['pluggable_data'] = $pluggable_data;
2358 return $context;
2359 }
2360
2361 /**
2362 * Search Divi Form fields.
2363 *
2364 * @param array $data Search Params.
2365 *
2366 * @since 1.0.0
2367 *
2368 * @return array
2369 */
2370 public function search_pluggable_divi_form_fields( $data ) {
2371 $result = [];
2372 $form_id = absint( $data['dynamic'] );
2373 $form_posts = Utilities::get_divi_forms();
2374
2375 if ( empty( $form_posts ) ) {
2376 return [
2377 'options' => [],
2378 'hasMore' => false,
2379 ];
2380 }
2381 $fields = [];
2382 foreach ( (array) $form_posts as $form_post ) {
2383 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
2384 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
2385 if ( empty( $forms ) ) {
2386 continue;
2387 }
2388
2389 $count = 0;
2390
2391 foreach ( $forms as $form ) {
2392 $pattern = get_shortcode_regex( [ 'et_pb_contact_field' ] );
2393
2394 preg_match_all( "/$pattern/", $form[0], $contact_fields, PREG_SET_ORDER );
2395
2396 if ( empty( $contact_fields ) ) {
2397 return $fields;
2398 }
2399
2400 foreach ( $contact_fields as $contact_field ) {
2401 $contact_field_attrs = shortcode_parse_atts( $contact_field[3] );
2402 $contact_field_attrs_str = self::array_get( $contact_field_attrs, 'field_id' );
2403 if ( is_string( $contact_field_attrs_str ) ) {
2404 $field_id = strtolower( $contact_field_attrs_str );
2405 $fields[] = [
2406 'field_title' => self::array_get( $contact_field_attrs, 'field_title', __( 'No title', 'suretriggers' ) ),
2407 'field_id' => $field_id,
2408 ];
2409 }
2410 }
2411 }
2412 }
2413
2414 if ( ! empty( $fields ) ) {
2415 foreach ( $fields as $field ) {
2416 $result[] = [
2417 'label' => $field['field_title'],
2418 'value' => '{' . $field['field_id'] . '}',
2419 ];
2420 }
2421 }
2422
2423 return [
2424 'options' => $result,
2425 'hasMore' => false,
2426 ];
2427 }
2428
2429 /**
2430 * Pseudo function copied from Divi
2431 *
2432 * @param array $array An array which contains value located at `$address`.
2433 * @param string|array $address The location of the value within `$array` (dot notation).
2434 * @param mixed $default Value to return if not found. Default is an empty string.
2435 *
2436 * @return mixed The value, if found, otherwise $default.
2437 */
2438 public static function array_get( $array, $address, $default = '' ) {
2439 $keys = is_array( $address ) ? $address : explode( '.', $address );
2440 $value = $array;
2441
2442 foreach ( $keys as $key ) {
2443 if ( ! empty( $key ) && isset( $key[0] ) && '[' === $key[0] ) {
2444 $index = substr( $key, 1, -1 );
2445
2446 if ( is_numeric( $index ) ) {
2447 $key = (int) $index;
2448 }
2449 }
2450
2451 if ( ! isset( $value[ $key ] ) ) {
2452 return $default;
2453 }
2454
2455 $value = $value[ $key ];
2456 }
2457
2458 return $value;
2459 }
2460
2461 /**
2462 * Get UAG Forms.
2463 *
2464 * @param array $data data.
2465 *
2466 * @return array
2467 */
2468 public function search_spectra_forms( $data ) {
2469 global $wpdb;
2470 $form_posts = Utilities::get_uag_forms();
2471
2472 $options = [];
2473 if ( empty( $form_posts ) ) {
2474 return [
2475 'options' => [],
2476 'hasMore' => false,
2477 ];
2478 }
2479
2480 foreach ( (array) $form_posts as $form_post ) {
2481 $blocks = parse_blocks( $form_post['post_content'] );
2482 $i = 1;
2483 // Get form blocks.
2484 $this->process_blocks( $blocks, $form_post, $options, $i );
2485 }
2486
2487 // Below is for Spectra forms from widgets.
2488 $search_string = '%wp-block-uagb-forms%';
2489 $option_results = $wpdb->get_results(
2490 $wpdb->prepare(
2491 "
2492 SELECT `option_name`, `option_value`
2493 FROM {$wpdb->options}
2494 WHERE (
2495 option_name LIKE %s OR
2496 option_name LIKE %s OR
2497 option_name LIKE %s
2498 )
2499 AND option_value LIKE %s
2500 ",
2501 'widget_%',
2502 'theme_mods_%',
2503 'customize_changeset_%',
2504 $search_string
2505 ),
2506 ARRAY_A
2507 );
2508 if ( ! empty( $option_results ) ) {
2509 foreach ( (array) $option_results as $form_post ) {
2510 $blocks = parse_blocks( $form_post['option_value'] );
2511 $i = 1;
2512 $this->process_blocks( $blocks, $form_post, $options, $i );
2513 }
2514 }
2515
2516 return [
2517 'options' => $options,
2518 'hasMore' => false,
2519 ];
2520 }
2521
2522 /**
2523 * Return forms in nested blocks in Spectra.
2524 *
2525 * @param array $blocks data.
2526 * @param array $form_post Form post.
2527 * @param array $options Options.
2528 * @param int $i Number.
2529 *
2530 * @return array
2531 */
2532 public function process_blocks( $blocks, $form_post, &$options, &$i ) {
2533 foreach ( $blocks as $block ) {
2534 if ( 'uagb/forms' === $block['blockName'] ) {
2535 $prefix = '';
2536 if ( isset( $form_post['post_title'] ) && ! empty( $form_post['post_title'] ) ) {
2537 $prefix = $form_post['post_title'] . ' - ';
2538 } elseif (
2539 isset( $form_post['option_value'] ) ) {
2540 $option_value = maybe_unserialize( $form_post['option_value'] );
2541 if ( is_array( $option_value ) ) {
2542 foreach ( $option_value as $widget_block ) {
2543 if (
2544 isset( $widget_block['content'] )
2545 && is_string( $widget_block['content'] )
2546 && strpos( $widget_block['content'], 'uagb/forms' ) !== false
2547 ) {
2548 if ( preg_match( '/"metadata"\s*:\s*{[^}]*"name"\s*:\s*"([^"]+)"/', $widget_block['content'], $matches ) ) {
2549 $prefix = sanitize_text_field( $matches[1] ) . ' - ';
2550 break;
2551 }
2552 }
2553 }
2554 }
2555 }
2556
2557 $options[] = [
2558 'label' => $prefix . $block['attrs']['block_id'] . ' (Form ' . ( $i++ ) . ')',
2559 'value' => $block['attrs']['block_id'],
2560 ];
2561 } elseif ( isset( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) {
2562 $this->process_blocks( $block['innerBlocks'], $form_post, $options, $i );
2563 }
2564 }
2565 return $options;
2566 }
2567
2568 /**
2569 * Check array recursive.
2570 *
2571 * @param array $array Array.
2572 * @param string $value search params.
2573 * @since 1.0.0
2574 *
2575 * @return array|void|int|string
2576 */
2577 public static function get_column_by_value( $array, $value ) {
2578
2579 foreach ( $array as $key => $sub_array ) {
2580
2581 if ( is_array( $sub_array ) ) {
2582 $result = self::get_column_by_value( $sub_array, $value );
2583 if ( null !== $result ) {
2584 return $key;
2585 }
2586 } else {
2587 return $key;
2588 }
2589 }
2590 return null;
2591
2592 }
2593
2594
2595 /**
2596 * Search UAG Form fields.
2597 *
2598 * @param array $data Search Params.
2599 *
2600 * @since 1.0.0
2601 *
2602 * @return array
2603 */
2604 public function search_spectraform_fields( $data ) {
2605 $result = [];
2606 $form_id = absint( $data['dynamic'] );
2607 $form_posts = Utilities::get_uag_forms();
2608
2609 if ( empty( $form_posts ) ) {
2610 return [
2611 'options' => [],
2612 'hasMore' => false,
2613 ];
2614 }
2615
2616 foreach ( (array) $form_posts as $form_post ) {
2617 $blocks = parse_blocks( $form_post['post_content'] );
2618
2619 foreach ( $blocks as $block ) {
2620 if ( (int) $block['attrs']['block_id'] === $form_id ) {
2621 $doc = new DOMDocument();
2622 $rendered_block = render_block( $block );
2623 $doc->loadHTML( $rendered_block );
2624 $child_node_list = $doc->getElementsByTagName( 'div' );
2625 $nodes = [];
2626 for ( $i = 0; $i < $child_node_list->length; $i++ ) {
2627 $temp = $child_node_list->item( $i );
2628 if ( $temp && stripos( $temp->getAttribute( 'class' ), 'uagb-forms-input-label' ) !== false ) {
2629 $nodes[] = $temp;
2630 }
2631 }
2632 if ( ! empty( $nodes ) ) {
2633 foreach ( $nodes as $node ) {
2634 $result[] = [
2635 'label' => $node->textContent, //phpcs:ignore
2636 'value' => $node->textContent, //phpcs:ignore
2637 ];
2638 }
2639 }
2640 }
2641 }
2642 }
2643
2644 return [
2645 'options' => $result,
2646 'hasMore' => false,
2647 ];
2648 }
2649
2650 /**
2651 * Search forms of MetForms.
2652 *
2653 * @param array $data data.
2654 * @return array
2655 */
2656 public function search_met_forms( $data ) {
2657 $args = [
2658 'post_type' => 'metform-form',
2659 'post_status' => 'publish',
2660 'numberposts' => -1,
2661 ];
2662
2663 $forms = get_posts( $args );
2664
2665 $options = [];
2666
2667 if ( ! empty( $forms ) ) {
2668 foreach ( $forms as $form ) {
2669 $options[] = [
2670 'label' => $form->post_title,
2671 'value' => $form->ID,
2672 ];
2673 }
2674 }
2675
2676 return [
2677 'options' => $options,
2678 'hasMore' => false,
2679 ];
2680 }
2681
2682 /**
2683 * Search forms of Ninja Forms.
2684 *
2685 * @param array $data data.
2686 * @return array
2687 */
2688 public function search_ninja_forms( $data ) {
2689 $options = [];
2690
2691 if ( function_exists( 'Ninja_Forms' ) ) {
2692 foreach ( Ninja_Forms()->form()->get_forms() as $form ) {
2693 $options[] = [
2694 'label' => $form->get_setting( 'title' ),
2695 'value' => $form->get_id(),
2696 ];
2697 }
2698 }
2699
2700 return [
2701 'options' => $options,
2702 'hasMore' => false,
2703 ];
2704 }
2705
2706 /**
2707 * Search forms of Pie Forms.
2708 *
2709 * @param array $data data.
2710 * @return array
2711 */
2712 public function search_pie_forms( $data ) {
2713 global $wpdb;
2714 $options = [];
2715
2716 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pf_forms' ) ) ) {
2717
2718 $results = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'pf_forms WHERE post_status = "published"' );
2719
2720 if ( $results ) {
2721 foreach ( $results as $result ) {
2722 $options[] = [
2723 'label' => $result->form_title,
2724 'value' => $result->id,
2725 ];
2726 }
2727 }
2728 }
2729
2730 return [
2731 'options' => $options,
2732 'hasMore' => false,
2733 ];
2734 }
2735
2736 /**
2737 * Get Fluent Forms.
2738 *
2739 * @param array $data data.
2740 *
2741 * @return array
2742 */
2743 public function search_fluent_forms( $data ) {
2744
2745 if ( ! function_exists( 'wpFluent' ) ) {
2746 return [
2747 'options' => [],
2748 'hasMore' => false,
2749 ];
2750 }
2751
2752 $forms = wpFluent()->table( 'fluentform_forms' )
2753 ->select( [ 'id', 'title' ] )
2754 ->orderBy( 'id', 'DESC' )
2755 ->get();
2756
2757 $options = [];
2758 if ( ! empty( $forms ) ) {
2759 foreach ( $forms as $form ) {
2760 $options[] = [
2761 'label' => $form->title,
2762 'value' => $form->id,
2763 ];
2764 }
2765 }
2766
2767 return [
2768 'options' => $options,
2769 'hasMore' => false,
2770 ];
2771
2772 }
2773
2774 /**
2775 * Get Fluent Forms Fields.
2776 *
2777 * @param array $data data.
2778 *
2779 * @return array
2780 */
2781 public function search_fluent_forms_form_fields( $data ) {
2782
2783 $options = [];
2784
2785 if ( ! function_exists( 'wpFluent' ) ) {
2786 return [];
2787 }
2788
2789 $form = wpFluent()->table( 'fluentform_forms' )->find( $data['dynamic'] );
2790 $field_data = json_decode( $form->form_fields, true );
2791 if ( is_array( $field_data ) && ! empty( $field_data['fields'] ) ) {
2792 foreach ( $field_data['fields'] as $field ) {
2793 if ( isset( $field['fields'] ) ) {
2794 foreach ( $field['fields'] as $field_key => $sub_field ) {
2795 if (
2796 isset( $sub_field['settings'] )
2797 && isset( $sub_field['settings']['label'] )
2798 && isset( $sub_field['settings']['visible'] )
2799 && true === $sub_field['settings']['visible']
2800 ) {
2801 $options[] = [
2802 'label' => $sub_field['settings']['label'],
2803 'value' => $field_key,
2804 ];
2805 }
2806 }
2807 } elseif ( isset( $field['element'] ) &&
2808 'container' === (string) $field['element'] &&
2809 isset( $field['columns'] ) && is_array( $field['columns'] ) ) {
2810 $container_fields = $field['columns'];
2811 foreach ( $container_fields as $c_fields ) {
2812 foreach ( $c_fields['fields'] as $field_key => $sub_field ) {
2813 if ( isset( $sub_field['settings'] ) ) {
2814 $options[] = [
2815 'label' => ( '' !== $sub_field['settings']['label'] ) ?
2816 $sub_field['settings']['label'] :
2817 $sub_field['attributes']['name'],
2818 'value' => isset( $sub_field['attributes']['name'] ) ?
2819 $sub_field['attributes']['name'] :
2820 strtolower( $sub_field['settings']['label'] ),
2821 ];
2822 }
2823 }
2824 }
2825 } elseif ( isset( $field['attributes'] ) && isset( $field['attributes']['name'] ) ) {
2826 if ( isset( $field['attributes']['placeholder'] ) && ! empty( $field['attributes']['placeholder'] ) ) {
2827 $options[] = [
2828 'label' => $field['attributes']['placeholder'],
2829 'value' => $field['attributes']['name'],
2830 ];
2831 } elseif ( isset( $field['settings'] ) &&
2832 isset( $field['settings']['label'] ) &&
2833 ! empty( $field['settings']['label'] ) ) {
2834 $options[] = [
2835 'label' => $field['settings']['label'],
2836 'value' => $field['attributes']['name'],
2837 ];
2838 }
2839 }
2840 }
2841 }
2842
2843 return [
2844 'options' => $options,
2845 'hasMore' => false,
2846 ];
2847
2848 }
2849
2850 /**
2851 * Get Fluent Forms.
2852 *
2853 * @param array $data data.
2854 *
2855 * @return array
2856 */
2857 public function search_bricks_builder_forms( $data ) {
2858 $bricks_theme = wp_get_theme( 'bricks' );
2859 if ( ! $bricks_theme->exists() ) {
2860 return [
2861 'options' => [],
2862 'hasMore' => false,
2863 ];
2864 }
2865
2866 $bricks_settings = (array) get_option( BRICKS_DB_GLOBAL_SETTINGS );
2867 if ( array_key_exists( 'postTypes', $bricks_settings ) ) {
2868 $bricks_posts = $bricks_settings['postTypes'];
2869 } else {
2870 $bricks_posts = [];
2871 }
2872 $bricks_posts[] = 'bricks_template';
2873
2874 $args = [
2875 'post_type' => $bricks_posts,
2876 'post_status' => 'publish',
2877 'posts_per_page' => -1,
2878 ];
2879
2880 $templates = get_posts( $args );
2881
2882 $options = [];
2883 if ( ! empty( $templates ) ) {
2884 foreach ( $templates as $template ) {
2885 $fetch_content = get_post_meta( $template->ID, BRICKS_DB_PAGE_CONTENT, true );
2886 if ( is_array( $fetch_content ) ) {
2887 foreach ( $fetch_content as $content ) {
2888 if ( 'form' === $content['name'] ) {
2889 $options[] = [
2890 'label' => $template->post_title . ' - ' . ( isset( $content['label'] ) ? $content['label'] : 'Form' ) . ' ( ID: ' . $content['id'] . ')',
2891 'value' => $content['id'],
2892 ];
2893 }
2894 }
2895 }
2896 }
2897 }
2898
2899 return [
2900 'options' => $options,
2901 'hasMore' => false,
2902 ];
2903
2904 }
2905
2906 /**
2907 * Bricks builder form fields.
2908 *
2909 * @param array $data data.
2910 * @return array
2911 */
2912 public function search_pluggable_bricks_builder_form_fields( $data ) {
2913 $result = [];
2914 $fields = [];
2915 $form_id_str = $data['dynamic'];
2916 $ids = explode( '_', $form_id_str );
2917 $post_id = $ids[0];
2918 $form_id = $ids[1];
2919 $fetch_content = get_post_meta( (int) $post_id, BRICKS_DB_PAGE_CONTENT, true );
2920 if ( is_array( $fetch_content ) ) {
2921 foreach ( $fetch_content as $content ) {
2922 if ( 'form' === $content['name'] && $form_id === $content['id'] ) {
2923 $fields = $content['settings']['fields'];
2924 break;
2925 }
2926 }
2927 }
2928
2929 if ( ! empty( $fields ) ) {
2930 foreach ( $fields as $field ) {
2931 $result[] = [
2932 'label' => $field['label'],
2933 'value' => '{' . strtolower( $field['label'] ) . '}',
2934 ];
2935 }
2936 }
2937
2938 return [
2939 'options' => $result,
2940 'hasMore' => false,
2941 ];
2942 }
2943
2944 /**
2945 * Get fluent form fields
2946 *
2947 * @param array $data Data array.
2948 *
2949 * @return array
2950 */
2951 public function search_pluggable_fluent_form_fields( $data ) {
2952 $result = [];
2953 $form_id = absint( $data['dynamic'] );
2954
2955 $fluentform_fields = Utilities::get_fluentform_fields( $data['search_term'], -1, $form_id );
2956
2957 if ( is_array( $fluentform_fields['results'] ) ) {
2958 foreach ( $fluentform_fields['results'] as $field ) {
2959 $result[] = [
2960 'label' => $field['text'],
2961 'value' => "{{$field['value']}}",
2962 ];
2963 }
2964 }
2965
2966 $result[] = [
2967 'value' => '{form_id}',
2968 'label' => 'Form ID',
2969 ];
2970
2971 $result[] = [
2972 'value' => '{form_title}',
2973 'label' => 'Form Title',
2974 ];
2975 $result[] = [
2976 'value' => '{entry_id}',
2977 'label' => 'Entry ID',
2978 ];
2979
2980 $result[] = [
2981 'value' => '{entry_source_url}',
2982 'label' => 'Entry Source URL',
2983 ];
2984
2985 $result[] = [
2986 'value' => '{submission_date}',
2987 'label' => 'Submission Date',
2988 ];
2989
2990 $result[] = [
2991 'value' => '{user_ip}',
2992 'label' => 'User IP',
2993 ];
2994
2995 return [
2996 'options' => $result,
2997 'hasMore' => false,
2998 ];
2999 }
3000
3001 /**
3002 * Search Gravity Form fields.
3003 *
3004 * @param array $data Search Params.
3005 *
3006 * @return array
3007 * @since 1.0.0
3008 */
3009 public function search_gform_fields( $data ) {
3010 if ( ! class_exists( 'RGFormsModel' ) || ! class_exists( 'GFCommon' ) ) {
3011 return [
3012 'options' => [],
3013 'hasMore' => false,
3014 ];
3015 }
3016 $result = [];
3017 $page = $data['page'];
3018 $form_id = absint( $data['dynamic'] );
3019
3020 $form = RGFormsModel::get_form_meta( $form_id );
3021
3022 if ( is_array( $form['fields'] ) ) {
3023 foreach ( $form['fields'] as $field ) {
3024 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
3025 foreach ( $field['inputs'] as $input ) {
3026 if ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
3027 $result[] = [
3028 'value' => $input['id'],
3029 'label' => GFCommon::get_label( $field, $input['id'] ),
3030 ];
3031 }
3032 }
3033 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
3034 $result[] = [
3035 'value' => (string) $field['id'],
3036 'label' => GFCommon::get_label( $field ),
3037 ];
3038 }
3039 }
3040 }
3041
3042 return [
3043 'options' => $result,
3044 'hasMore' => false,
3045 ];
3046
3047 }
3048
3049 /**
3050 * Search Gravity Form fields.
3051 *
3052 * @param array $data Search Params.
3053 *
3054 * @return array
3055 * @since 1.0.0
3056 */
3057 public function search_pluggable_gravity_form_fields( $data ) {
3058 if ( ! class_exists( 'RGFormsModel' ) || ! class_exists( 'GFCommon' ) ) {
3059 return [
3060 'options' => [],
3061 'hasMore' => false,
3062 ];
3063 }
3064 $result = [];
3065 $form_id = absint( $data['dynamic'] );
3066
3067 $form = RGFormsModel::get_form_meta( $form_id );
3068
3069 if ( is_array( $form['fields'] ) ) {
3070 foreach ( $form['fields'] as $field ) {
3071 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
3072 foreach ( $field['inputs'] as $input ) {
3073 if ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
3074 $result[] = [
3075 'value' => '{' . $input['id'] . '}',
3076 'label' => GFCommon::get_label( $field, $input['id'] ),
3077 ];
3078 }
3079 }
3080 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
3081 $result[] = [
3082 'value' => '{' . $field['id'] . '}',
3083 'label' => GFCommon::get_label( $field ),
3084 ];
3085 }
3086 }
3087 }
3088
3089 $result[] = [
3090 'value' => '{gravity_form}',
3091 'label' => 'Form ID',
3092 ];
3093 $result[] = [
3094 'value' => '{form_title}',
3095 'label' => 'Form Title',
3096 ];
3097 $result[] = [
3098 'value' => '{entry_id}',
3099 'label' => 'Entry ID',
3100 ];
3101 $result[] = [
3102 'value' => '{user_ip}',
3103 'label' => 'User IP',
3104 ];
3105 $result[] = [
3106 'value' => '{entry_source_url}',
3107 'label' => 'Entry Source URL',
3108 ];
3109 $result[] = [
3110 'value' => '{entry_submission_date}',
3111 'label' => 'Entry Submission Date',
3112 ];
3113
3114 return [
3115 'options' => $result,
3116 'hasMore' => false,
3117 ];
3118
3119 }
3120
3121 /**
3122 * Search Gravity Form fields for action.
3123 *
3124 * @param array $data data.
3125 * @return array
3126 */
3127 public function search_gravity_form_custom_fields( $data ) {
3128 $context = [];
3129 if ( ! class_exists( 'RGFormsModel' ) || ! class_exists( 'GFCommon' ) ) {
3130 return [];
3131 }
3132 $form_id = $data['filter'];
3133 $form = RGFormsModel::get_form_meta( $form_id );
3134 $custom_fields = [];
3135 if ( is_array( $form['fields'] ) ) {
3136 foreach ( $form['fields'] as $field ) {
3137 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
3138 foreach ( $field['inputs'] as $input ) {
3139 if ( ! $input['isHidden'] ) {
3140 $custom_fields[] = [
3141 'label' => GFCommon::get_label( $field, $input['id'] ),
3142 'value' => $input['id'],
3143 'type' => 'text',
3144 ];
3145 }
3146 }
3147 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
3148 $custom_fields[] = [
3149 'label' => GFCommon::get_label( $field ),
3150 'value' => $field['id'],
3151 'type' => 'text',
3152 ];
3153 }
3154 }
3155 }
3156 $context['fields'] = $custom_fields;
3157 return $context;
3158 }
3159
3160 /**
3161 * Get user register details via gravity forms.
3162 *
3163 * @param array $data data.
3164 *
3165 * @return array
3166 */
3167 public function search_pluggables_gravity_forms_user_register( $data ) {
3168 global $wpdb;
3169 $context = [];
3170 $pluggable_data = [];
3171
3172 $results = $wpdb->get_results(
3173 $wpdb->prepare(
3174 "SELECT id, form_id, date_created, ip, source_url, created_by FROM {$wpdb->prefix}gf_entry
3175 WHERE form_id = %d AND
3176 status = %s
3177 ORDER BY id
3178 DESC LIMIT 1",
3179 $data['filter']['gravity_form']['value'],
3180 'active'
3181 )
3182 );
3183
3184 if ( $results ) {
3185 $pluggable_data['gravity_form'] = (int) $results[0]->form_id;
3186 $pluggable_data['entry_id'] = $results[0]->id;
3187 $pluggable_data['user_ip'] = $results[0]->ip;
3188 $pluggable_data['entry_source_url'] = $results[0]->source_url;
3189 $pluggable_data['entry_submission_date'] = $results[0]->date_created;
3190 $pluggable_data['user'] = WordPress::get_user_context( $results[0]->created_by );
3191 $context['response_type'] = 'live';
3192 } else {
3193 $pluggable_data['gravity_form'] = '3';
3194 $pluggable_data['entry_id'] = '13';
3195 $pluggable_data['user_ip'] = '127.0.0.0';
3196 $pluggable_data['entry_source_url'] = 'https://example.com';
3197 $pluggable_data['entry_submission_date'] = '2024-02-05 09:41:59';
3198 $pluggable_data['user']['wp_user_id'] = '123';
3199 $pluggable_data['user']['user_login'] = 'johnd';
3200 $pluggable_data['user']['display_name'] = 'johnd';
3201 $pluggable_data['user']['user_firstname'] = 'John';
3202 $pluggable_data['user']['user_lastname'] = 'Doe';
3203 $pluggable_data['user']['user_email'] = 'johnd@yopmail.com';
3204 $pluggable_data['user']['user_role'] = [ 'subscriber' ];
3205 $context['response_type'] = 'sample';
3206 }
3207
3208 $context['pluggable_data'] = $pluggable_data;
3209 return $context;
3210 }
3211
3212 /**
3213 * Get payment form details for gravity forms.
3214 *
3215 * @param array $data data.
3216 *
3217 * @return array
3218 */
3219 public function search_pluggables_gravity_forms_payment( $data ) {
3220 global $wpdb;
3221 $context = [];
3222 $pluggable_data = [];
3223
3224 $results = $wpdb->get_results(
3225 $wpdb->prepare(
3226 "SELECT * FROM {$wpdb->prefix}gf_entry
3227 WHERE form_id = %d AND
3228 status = %s AND
3229 payment_status LIKE %s
3230 ORDER BY id
3231 DESC LIMIT 1",
3232 $data['filter']['gravity_form']['value'],
3233 'active',
3234 'Paid'
3235 )
3236 );
3237
3238 if ( $results ) {
3239 $pluggable_data['gravity_form'] = (int) $results[0]->form_id;
3240 $pluggable_data['entry_id'] = $results[0]->id;
3241 $pluggable_data['user_ip'] = $results[0]->ip;
3242 $pluggable_data['entry_source_url'] = $results[0]->source_url;
3243 $pluggable_data['entry_submission_date'] = $results[0]->date_created;
3244 $pluggable_data['payment_status'] = $results[0]->payment_status;
3245 $pluggable_data['payment_amount'] = $results[0]->payment_amount;
3246 $pluggable_data['currency'] = $results[0]->currency;
3247 $pluggable_data['payment_method'] = $results[0]->payment_method;
3248 $pluggable_data['transaction_id'] = $results[0]->transaction_id;
3249 $pluggable_data['user'] = WordPress::get_user_context( $results[0]->created_by );
3250 $context['response_type'] = 'live';
3251 } else {
3252 $pluggable_data['gravity_form'] = '3';
3253 $pluggable_data['entry_id'] = '13';
3254 $pluggable_data['user_ip'] = '127.0.0.0';
3255 $pluggable_data['entry_source_url'] = 'https://example.com';
3256 $pluggable_data['entry_submission_date'] = '2024-02-05 09:41:59';
3257 $pluggable_data['payment_status'] = 'Paid';
3258 $pluggable_data['payment_amount'] = '10.00';
3259 $pluggable_data['currency'] = 'USD';
3260 $pluggable_data['payment_method'] = 'visa';
3261 $pluggable_data['transaction_id'] = 'st_ooi98';
3262 $pluggable_data['user']['wp_user_id'] = '123';
3263 $pluggable_data['user']['user_login'] = 'johnd';
3264 $pluggable_data['user']['display_name'] = 'johnd';
3265 $pluggable_data['user']['user_firstname'] = 'John';
3266 $pluggable_data['user']['user_lastname'] = 'Doe';
3267 $pluggable_data['user']['user_email'] = 'johnd@yopmail.com';
3268 $pluggable_data['user']['user_role'] = [ 'subscriber' ];
3269 $context['response_type'] = 'sample';
3270 }
3271
3272 $context['pluggable_data'] = $pluggable_data;
3273 return $context;
3274 }
3275
3276 /**
3277 * Prepare fluentcrm lists.
3278 *
3279 * @param array $data Search Params.
3280 *
3281 * @return array
3282 */
3283 public function search_fluentcrm_lists( $data ) {
3284 if ( ! function_exists( 'FluentCrmApi' ) ) {
3285 return [];
3286 }
3287 $list_api = FluentCrmApi( 'lists' );
3288 $all_lists = $list_api->all();
3289 $options = [];
3290
3291 if ( ! empty( $all_lists ) ) {
3292 foreach ( $all_lists as $list ) {
3293 $options[] = [
3294 'label' => $list->title,
3295 'value' => $list->id,
3296 ];
3297 }
3298 }
3299
3300 return [
3301 'options' => $options,
3302 'hasMore' => false,
3303 ];
3304 }
3305
3306 /**
3307 * Prepare fluentcrm segments.
3308 *
3309 * @param array $data Search Params.
3310 *
3311 * @return array
3312 */
3313 public function search_fluentcrm_segments( $data ) {
3314 global $wpdb;
3315 $options = [];
3316
3317 $table = $wpdb->prefix . 'fc_meta';
3318
3319 $segments = $wpdb->get_results(
3320 $wpdb->prepare(
3321 'SELECT id, value FROM ' . $wpdb->prefix . 'fc_meta WHERE object_type = %s',
3322 'custom_segment'
3323 )
3324 );
3325
3326 if ( is_array( $segments ) && ! empty( $segments ) ) {
3327 foreach ( $segments as $segment ) {
3328 $raw_value = is_string( $segment->value ) ? $segment->value : '';
3329 $segment_data = maybe_unserialize( $raw_value );
3330
3331 if ( is_array( $segment_data ) && isset( $segment_data['title'] ) ) {
3332 $options[] = [
3333 'label' => sanitize_text_field( $segment_data['title'] ),
3334 'value' => intval( $segment->id ),
3335 ];
3336 }
3337 }
3338 }
3339
3340 return [
3341 'options' => $options,
3342 'hasMore' => false,
3343 ];
3344 }
3345
3346 /**
3347 * Prepare fluentcrm contact status.
3348 *
3349 * @param array $data Search Params.
3350 *
3351 * @return array
3352 */
3353 public function search_fluentcrm_contact_status( $data ) {
3354
3355 $options = [
3356 [
3357 'label' => __( 'Subscribed', 'suretriggers' ),
3358 'value' => 'subscribed',
3359 ],
3360 [
3361 'label' => __( 'Pending', 'suretriggers' ),
3362 'value' => 'pending',
3363 ],
3364 [
3365 'label' => __( 'Unsubscribed', 'suretriggers' ),
3366 'value' => 'unsubscribed',
3367 ],
3368 [
3369 'label' => __( 'Bounced', 'suretriggers' ),
3370 'value' => 'bounced',
3371 ],
3372 [
3373 'label' => __( 'Complained', 'suretriggers' ),
3374 'value' => 'complained',
3375 ],
3376 ];
3377
3378 return [
3379 'options' => $options,
3380 'hasMore' => false,
3381 ];
3382 }
3383
3384 /**
3385 * Prepare fluentcrm contact status.
3386 *
3387 * @param array $data Search Params.
3388 *
3389 * @return array
3390 */
3391 public function search_fluentcrm_fetch_custom_fields( $data ) {
3392
3393 $options = [
3394 [
3395 'label' => __( 'Yes', 'suretriggers' ),
3396 'value' => 'true',
3397 ],
3398 [
3399 'label' => __( 'No', 'suretriggers' ),
3400 'value' => 'false',
3401 ],
3402 ];
3403
3404 return [
3405 'options' => $options,
3406 'hasMore' => false,
3407 ];
3408 }
3409
3410 /**
3411 * Prepare fluentcrm tags.
3412 *
3413 * @param array $data Search Params.
3414 *
3415 * @return array
3416 */
3417 public function search_fluentcrm_tags( $data ) {
3418
3419 if ( ! function_exists( 'FluentCrmApi' ) ) {
3420 return [];
3421 }
3422
3423 $tag_api = FluentCrmApi( 'tags' );
3424 $all_tags = $tag_api->all();
3425 $options = [];
3426
3427 if ( ! empty( $all_tags ) ) {
3428 foreach ( $all_tags as $tag ) {
3429 $options[] = [
3430 'label' => $tag->title,
3431 'value' => $tag->id,
3432 ];
3433 }
3434 }
3435
3436 return [
3437 'options' => $options,
3438 'hasMore' => false,
3439 ];
3440 }
3441
3442 /**
3443 * Prepare fluentcrm templates.
3444 *
3445 * @param array $data Search Params.
3446 *
3447 * @return array
3448 */
3449 public function search_fluentcrm_templates( $data ) {
3450 if ( ! class_exists( '\FluentCrm\App\Models\Template' ) ) {
3451 return [
3452 'options' => [],
3453 'hasMore' => false,
3454 ];
3455 }
3456
3457 $options = [];
3458 $templates = \FluentCrm\App\Models\Template::emailTemplates( [ 'publish', 'draft' ] )->orderBy( 'ID', 'desc' )->get();
3459
3460 foreach ( $templates as $template ) {
3461 $options[] = [
3462 'label' => $template->post_title,
3463 'value' => $template->ID,
3464 'email_subject' => get_post_meta( $template->ID, '_email_subject', true ),
3465 'design_template' => get_post_meta( $template->ID, '_design_template', true ),
3466 ];
3467 }
3468
3469 return [
3470 'options' => $options,
3471 'hasMore' => false,
3472 ];
3473 }
3474
3475 /**
3476 * Prepare fluentcrm sequences.
3477 *
3478 * @param array $data Search Params.
3479 *
3480 * @return array
3481 */
3482 public function search_fluentcrm_sequences( $data ) {
3483 if ( ! class_exists( '\FluentCampaign\App\Models\Sequence' ) ) {
3484 return [
3485 'options' => [],
3486 'hasMore' => false,
3487 ];
3488 }
3489
3490 $options = [];
3491 $sequences = \FluentCampaign\App\Models\Sequence::orderBy( 'id', 'DESC' )->get();
3492
3493 foreach ( $sequences as $sequence ) {
3494 $options[] = [
3495 'label' => $sequence->title,
3496 'value' => $sequence->id,
3497 ];
3498 }
3499
3500 return [
3501 'options' => $options,
3502 'hasMore' => false,
3503 ];
3504 }
3505
3506 /**
3507 * Prepare JetpackCRM Contact tags.
3508 *
3509 * @param array $data Search Params.
3510 *
3511 * @return array
3512 */
3513 public function search_jetpack_crm_contact_tags( $data ) {
3514
3515 if ( ! function_exists( 'zeroBSCRM_getCustomerTags' ) ) {
3516 return [];
3517 }
3518
3519 $all_tags = zeroBSCRM_getCustomerTags();
3520 $options = [];
3521
3522 if ( ! empty( $all_tags ) ) {
3523 foreach ( $all_tags as $tag ) {
3524 $options[] = [
3525 'label' => $tag['name'],
3526 'value' => $tag['id'],
3527 ];
3528 }
3529 }
3530
3531 return [
3532 'options' => $options,
3533 'hasMore' => false,
3534 ];
3535 }
3536
3537 /**
3538 * Prepare JetpackCRM Company tags.
3539 *
3540 * @param array $data Search Params.
3541 *
3542 * @return array
3543 */
3544 public function search_jetpack_crm_company_tags( $data ) {
3545
3546 if ( ! defined( 'ZBS_TYPE_COMPANY' ) ) {
3547 return [];
3548 }
3549
3550 global $wpdb;
3551 $all_tags = $wpdb->get_results( $wpdb->prepare( "SELECT `ID`,`zbstag_name` FROM `{$wpdb->prefix}zbs_tags` WHERE zbstag_objtype = %d", ZBS_TYPE_COMPANY ) );
3552
3553 $options = [];
3554 if ( ! empty( $all_tags ) ) {
3555 foreach ( $all_tags as $tag ) {
3556 $options[] = [
3557 'label' => $tag->zbstag_name,
3558 'value' => $tag->ID,
3559 ];
3560 }
3561 }
3562
3563 return [
3564 'options' => $options,
3565 'hasMore' => false,
3566 ];
3567 }
3568
3569 /**
3570 * Prepare JetpackCRM Companies list.
3571 *
3572 * @param array $data Search Params.
3573 *
3574 * @return array
3575 */
3576 public function search_jetpack_crm_companies_list( $data ) {
3577
3578 if ( ! function_exists( 'zeroBS_getCompanies' ) ) {
3579 return [];
3580 }
3581
3582 $all_companies = zeroBS_getCompanies();
3583 $options = [];
3584
3585 if ( ! empty( $all_companies ) ) {
3586 foreach ( $all_companies as $company ) {
3587 $options[] = [
3588 'label' => $company['name'],
3589 'value' => $company['id'],
3590 ];
3591 }
3592 }
3593
3594 return [
3595 'options' => $options,
3596 'hasMore' => false,
3597 ];
3598 }
3599
3600 /**
3601 * Prepare JetpackCRM contact status.
3602 *
3603 * @param array $data Search Params.
3604 *
3605 * @return array
3606 */
3607 public function search_jetpack_crm_contact_statuses( $data ) {
3608
3609 $options = [
3610 [
3611 'label' => __( 'Lead', 'suretriggers' ),
3612 'value' => 'Lead',
3613 ],
3614 [
3615 'label' => __( 'Customer', 'suretriggers' ),
3616 'value' => 'Customer',
3617 ],
3618 [
3619 'label' => __( 'Refused', 'suretriggers' ),
3620 'value' => 'Refused',
3621 ],
3622 [
3623 'label' => __( 'Blacklisted', 'suretriggers' ),
3624 'value' => 'Blacklisted',
3625 ],
3626 [
3627 'label' => __( 'Cancelled by Customer', 'suretriggers' ),
3628 'value' => 'Cancelled by Customer',
3629 ],
3630 [
3631 'label' => __( 'Cancelled by Us (Pre-Quote)', 'suretriggers' ),
3632 'value' => 'Cancelled by Us (Pre-Quote)',
3633 ],
3634 [
3635 'label' => __( 'Cancelled by Us (Post-Quote)', 'suretriggers' ),
3636 'value' => 'Cancelled by Us (Post-Quote)',
3637 ],
3638 ];
3639
3640 return [
3641 'options' => $options,
3642 'hasMore' => false,
3643 ];
3644 }
3645
3646 /**
3647 * Prepare FunnelKit Automations' lists.
3648 *
3649 * @param array $data Search Params.
3650 *
3651 * @return array
3652 */
3653 public function search_funnel_kit_automations_lists( $data ) {
3654
3655 if ( ! class_exists( 'BWFCRM_Lists' ) ) {
3656 return [];
3657 }
3658
3659 $bwfcrm_lists = \BWFCRM_Lists::get_lists();
3660
3661 $options = [];
3662
3663 foreach ( $bwfcrm_lists as $list ) {
3664 $options[] = [
3665 'label' => $list['name'],
3666 'value' => $list['ID'],
3667 ];
3668 }
3669
3670 return [
3671 'options' => $options,
3672 'hasMore' => false,
3673 ];
3674 }
3675
3676 /**
3677 * Prepare FunnelKit Automations' tags.
3678 *
3679 * @param array $data Search Params.
3680 *
3681 * @return array
3682 */
3683 public function search_funnel_kit_automations_tags( $data ) {
3684
3685 if ( ! class_exists( 'BWFCRM_Tag' ) ) {
3686 return [];
3687 }
3688
3689 $bwfcrm_tags = \BWFCRM_Tag::get_tags();
3690
3691 $options = [];
3692
3693 foreach ( $bwfcrm_tags as $tag ) {
3694 $options[] = [
3695 'label' => $tag['name'],
3696 'value' => $tag['ID'],
3697 ];
3698 }
3699
3700 return [
3701 'options' => $options,
3702 'hasMore' => false,
3703 ];
3704 }
3705
3706 /**
3707 * Prepare Wishlist Memberlists level.
3708 *
3709 * @param array $data Search Params.
3710 *
3711 * @return array
3712 */
3713 public function search_wishlistmember_lists( $data ) {
3714 if ( ! function_exists( 'wlmapi_get_levels' ) ) {
3715 return [];
3716 }
3717 $wlm_levels = wlmapi_get_levels();
3718 $options = [];
3719
3720 if ( ! empty( $wlm_levels ) ) {
3721 foreach ( $wlm_levels['levels']['level'] as $list ) {
3722 if ( isset( $list['name'] ) && ! empty( $list['name'] ) ) {
3723 $options[] = [
3724 'label' => $list['name'],
3725 'value' => (string) $list['id'],
3726 ];
3727 }
3728 }
3729 }
3730
3731 return [
3732 'options' => $options,
3733 'hasMore' => false,
3734 ];
3735 }
3736
3737 /**
3738 * Prepare Wishlist Memberlists members.
3739 *
3740 * @param array $data Search Params.
3741 *
3742 * @return array<string, array<int<0, max>, array<string, mixed>>|false>
3743 */
3744 public function search_wishlistmember_members( $data ) {
3745
3746 if ( ! function_exists( 'wlmapi_get_members' ) ) {
3747 return [];
3748 }
3749
3750 $wlm_members = wlmapi_get_members();
3751 $options = [];
3752
3753 if ( ! empty( $wlm_members ) ) {
3754 foreach ( $wlm_members['members']['member'] as $list ) {
3755 if ( isset( $list['user_email'] ) && ! empty( $list['user_email'] ) ) {
3756 $options[] = [
3757 'label' => $list['user_email'],
3758 'value' => (string) $list['id'],
3759 ];
3760 }
3761 }
3762 }
3763
3764 return [
3765 'options' => $options,
3766 'hasMore' => false,
3767 ];
3768 }
3769
3770 /**
3771 * Prepare elementor popups.
3772 *
3773 * @param array $data Search Params.
3774 *
3775 * @return array
3776 */
3777 public function search_elementor_popups( $data ) {
3778
3779 $posts = get_posts(
3780 [
3781 'post_type' => 'elementor_library',
3782 'orderby' => 'title',
3783 'order' => 'ASC',
3784 'post_status' => 'publish',
3785 'meta_query' => [
3786 [
3787 'key' => '_elementor_template_type',
3788 'value' => 'popup',
3789 'compare' => '=',
3790 ],
3791 ],
3792 ]
3793 );
3794
3795 $options = [];
3796 if ( ! empty( $posts ) ) {
3797 foreach ( $posts as $post ) {
3798 $options[] = [
3799 'label' => $post->post_title,
3800 'value' => $post->ID,
3801 ];
3802 }
3803 }
3804
3805 return [
3806 'options' => $options,
3807 'hasMore' => false,
3808 ];
3809 }
3810
3811 /**
3812 * Prepare givewp forms.
3813 *
3814 * @param array $data Search Params.
3815 *
3816 * @return array
3817 */
3818 public function search_givewp_forms( $data ) {
3819
3820 $page = $data['page'];
3821 $limit = Utilities::get_search_page_limit();
3822 $offset = $limit * ( $page - 1 );
3823
3824 $posts = get_posts(
3825 [
3826 'post_type' => 'give_forms',
3827 'orderby' => 'title',
3828 'order' => 'ASC',
3829 'post_status' => 'publish',
3830 'posts_per_page' => $limit,
3831 'offset' => $offset,
3832 ]
3833 );
3834
3835 $options = [];
3836 if ( ! empty( $posts ) ) {
3837 foreach ( $posts as $post ) {
3838 $options[] = [
3839 'label' => $post->post_title,
3840 'value' => $post->ID,
3841 ];
3842 }
3843 }
3844
3845 $count = wp_count_posts( 'give_forms' )->publish;
3846
3847 return [
3848 'options' => $options,
3849 'hasMore' => $count > $limit && $count > $offset,
3850 ];
3851 }
3852
3853 /**
3854 * Prepare givewp recurring forms.
3855 *
3856 * @param array $data Search Params.
3857 *
3858 * @return array
3859 */
3860 public function search_givewp_recurring_forms( $data ) {
3861
3862 global $wpdb;
3863 $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 );
3864
3865 $options = [];
3866 if ( ! empty( $recurring ) ) {
3867 foreach ( $recurring as $form_id ) {
3868 $post_status = get_post_status( $form_id['form_id'] );
3869 if ( 'trash' !== $post_status ) {
3870 $options[] = [
3871 'label' => get_the_title( $form_id['form_id'] ),
3872 'value' => $form_id['form_id'],
3873 ];
3874 }
3875 }
3876 }
3877
3878 return [
3879 'options' => $options,
3880 'hasMore' => false,
3881 ];
3882 }
3883
3884 /**
3885 * Prepare givewp forms.
3886 *
3887 * @param array $data Search Params.
3888 *
3889 * @return array
3890 */
3891 public function search_givewp_form_fields( $data ) {
3892 $options = [
3893 [
3894 'label' => 'First Name',
3895 'value' => '_give_donor_billing_first_name',
3896 ],
3897 [
3898 'label' => 'Last Name',
3899 'value' => '_give_donor_billing_last_name',
3900 ],
3901 [
3902 'label' => 'Email',
3903 'value' => '_give_payment_donor_email',
3904 ],
3905 [
3906 'label' => 'Donation Amount',
3907 'value' => '_give_payment_total',
3908 ],
3909 [
3910 'label' => 'Currency',
3911 'value' => '_give_payment_currency',
3912 ],
3913 [
3914 'label' => 'Comment',
3915 'value' => '_give_donation_comment',
3916 ],
3917 [
3918 'label' => 'Zip',
3919 'value' => '_give_donor_billing_zip',
3920 ],
3921 [
3922 'label' => 'Country',
3923 'value' => '_give_donor_billing_country',
3924 ],
3925 [
3926 'label' => 'Address 1',
3927 'value' => '_give_donor_billing_address1',
3928 ],
3929 [
3930 'label' => 'Address 2',
3931 'value' => '_give_donor_billing_address2',
3932 ],
3933 [
3934 'label' => 'City',
3935 'value' => '_give_donor_billing_city',
3936 ],
3937 [
3938 'label' => 'State',
3939 'value' => '_give_donor_billing_state',
3940 ],
3941 ];
3942
3943 if ( class_exists( '\Give_FFM_Render_Form' ) ) {
3944 $form_id = $data['dynamic'];
3945 $custom_form_fields = [];
3946 if ( class_exists( '\GiveFormFieldManager\Helpers\Form' ) ) {
3947 $custom_form_fields = \GiveFormFieldManager\Helpers\Form::get_input_fields( $form_id );
3948 }
3949
3950 if ( ! empty( $custom_form_fields ) ) {
3951 if ( ! empty( $custom_form_fields[2] ) && is_array( $custom_form_fields[2] ) ) {
3952 foreach ( $custom_form_fields[2] as $custom_form_field ) {
3953 $custom_form_field['required'] = ( 'no' === $custom_form_field['required'] ) ? false : true;
3954 $options[] = [
3955 'label' => $custom_form_field['label'],
3956 'value' => $custom_form_field['name'],
3957 ];
3958 }
3959 }
3960 }
3961 }
3962
3963 return [
3964 'options' => $options,
3965 'hasMore' => false,
3966 ];
3967 }
3968
3969 /**
3970 * Prepare buddyboss group users.
3971 *
3972 * @param array $data Search Params.
3973 *
3974 * @return array
3975 */
3976 public function search_bb_group_users( $data ) {
3977 $options = [];
3978 if ( ! function_exists( 'groups_get_group_members' ) || ! function_exists( 'groups_get_group_admins' ) ) {
3979 return [];
3980 }
3981 $group_id = $data['dynamic'];
3982 $admins = groups_get_group_admins( $group_id );
3983
3984 if ( ! empty( $admins ) ) {
3985 foreach ( $admins as $admin ) {
3986 $admin_user = get_user_by( 'id', $admin->user_id );
3987 if ( $admin_user instanceof \WP_User ) {
3988 $options[] = [
3989 'label' => $admin_user->display_name,
3990 'value' => $admin_user->ID,
3991 ];
3992 }
3993 }
3994 }
3995
3996 $members = groups_get_group_members( [ 'group_id' => $group_id ] );
3997
3998 if ( isset( $members['members'] ) && ! empty( $members['members'] ) ) {
3999 foreach ( $members['members'] as $member ) {
4000 $options[] = [
4001 'label' => $member->display_name,
4002 'value' => $member->ID,
4003 ];
4004 }
4005 }
4006 return [
4007 'options' => $options,
4008 'hasMore' => false,
4009 ];
4010 }
4011
4012 /**
4013 * Prepare buddyboss groups.
4014 *
4015 * @param array $data Search Params.
4016 *
4017 * @return array
4018 */
4019 public function search_buddyboss_groups( $data ) {
4020 global $wpdb;
4021
4022 $options = [];
4023 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
4024 if ( ! empty( $groups ) ) {
4025 foreach ( $groups as $group ) {
4026 $options[] = [
4027 'label' => $group->name,
4028 'value' => $group->id,
4029 ];
4030 }
4031 }
4032 return [
4033 'options' => $options,
4034 'hasMore' => false,
4035 ];
4036 }
4037
4038 /**
4039 * Prepare buddyboss public groups.
4040 *
4041 * @param array $data Search Params.
4042 *
4043 * @return array
4044 */
4045 public function search_buddyboss_public_groups( $data ) {
4046 $options = [];
4047 if ( ! function_exists( 'groups_get_groups' ) ) {
4048 return [];
4049 }
4050 $groups = groups_get_groups();
4051 if ( isset( $groups['groups'] ) && ! empty( $groups['groups'] ) ) {
4052 foreach ( $groups['groups'] as $group ) {
4053 if ( 'public' === $group->status ) {
4054 $options[] = [
4055 'label' => $group->name,
4056 'value' => $group->id,
4057 ];
4058 }
4059 }
4060 }
4061 return [
4062 'options' => $options,
4063 'hasMore' => false,
4064 ];
4065 }
4066
4067 /**
4068 * Prepare buddyboss private groups.
4069 *
4070 * @param array $data Search Params.
4071 *
4072 * @return array<string, array<int, array<string, mixed>>|false>
4073 */
4074 public function search_buddyboss_private_groups( $data ) {
4075 global $wpdb;
4076
4077 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'private'" );
4078
4079 $options = [];
4080 if ( $groups ) {
4081 foreach ( $groups as $group ) {
4082 $options[] = [
4083 'label' => $group->name,
4084 'value' => $group->id,
4085 ];
4086 }
4087 }
4088
4089 return [
4090 'options' => $options,
4091 'hasMore' => false,
4092 ];
4093 }
4094
4095 /**
4096 * Prepare buddyboss profile types list.
4097 *
4098 * @param array $data Search Params.
4099 *
4100 * @return array[]
4101 */
4102 public function search_bb_profile_type_list( $data ) {
4103 $options = [];
4104
4105 if ( function_exists( 'bp_get_active_member_types' ) ) {
4106 $types = bp_get_active_member_types(
4107 [
4108 'fields' => '*',
4109 ]
4110 );
4111 if ( $types ) {
4112 foreach ( $types as $type ) {
4113 $options[] = [
4114 'label' => $type->post_title,
4115 'value' => $type->ID,
4116 ];
4117 }
4118 }
4119 }
4120
4121 /**
4122 *
4123 * Ignore line
4124 *
4125 * @phpstan-ignore-next-line
4126 */
4127 return [
4128 'options' => $options,
4129 'hasMore' => false,
4130 ];
4131 }
4132
4133 /**
4134 * Prepare buddyboss public groups.
4135 *
4136 * @param array $data Search Params.
4137 *
4138 * @return array
4139 */
4140 public function search_buddyboss_group_type( $data ) {
4141 $options = [];
4142
4143 if ( ! function_exists( 'bp_groups_get_group_types' ) ) {
4144 return [];
4145 }
4146
4147 $registered_types = bp_groups_get_group_types();
4148 if ( ! empty( $registered_types ) ) {
4149 foreach ( $registered_types as $key => $type ) {
4150 $options[] = [
4151 'label' => $type,
4152 'value' => $key,
4153 ];
4154 }
4155 }
4156 return [
4157 'options' => $options,
4158 'hasMore' => false,
4159 ];
4160 }
4161
4162 /**
4163 * Get BuddyBoss Forum Topic Status.
4164 *
4165 * @param array $data data.
4166 *
4167 * @return array|void
4168 */
4169 public function search_bb_forum_topic_status( $data ) {
4170 $options = [];
4171
4172 if ( ! function_exists( 'bbp_get_topic_post_type' ) ) {
4173 return [];
4174 }
4175
4176 $page = $data['page'];
4177 $limit = Utilities::get_search_page_limit();
4178 $offset = $limit * ( $page - 1 );
4179
4180 $forum_args = [
4181 'post_type' => bbp_get_topic_post_type(),
4182 'orderby' => 'title',
4183 'order' => 'ASC',
4184 'post_status' => [ 'publish', 'private' ],
4185 'posts_per_page' => $limit,
4186 'offset' => $offset,
4187 ];
4188
4189 $forums = get_posts( $forum_args );
4190 if ( ! empty( $forums ) ) {
4191 foreach ( $forums as $key => $forum ) {
4192 $options[] = [
4193 'label' => $forum,
4194 'value' => $key,
4195 ];
4196 }
4197 }
4198 $count = count( $options );
4199 return [
4200 'options' => $options,
4201 'hasMore' => $count > $limit && $count > $offset,
4202 ];
4203 }
4204
4205 /**
4206 * Get BuddyBoss Forums List.
4207 *
4208 * @param array $data data.
4209 *
4210 * @return array|void
4211 */
4212 public function search_bb_forums_list( $data ) {
4213 $options = [];
4214
4215 $page = $data['page'];
4216 $limit = Utilities::get_search_page_limit();
4217 $offset = $limit * ( $page - 1 );
4218
4219 if ( ! function_exists( 'bbp_get_forum_post_type' ) ) {
4220 return [];
4221 }
4222
4223 $forum_args = [
4224 'post_type' => bbp_get_forum_post_type(),
4225 'orderby' => 'title',
4226 'order' => 'ASC',
4227 'post_status' => [ 'publish', 'private' ],
4228 'posts_per_page' => $limit,
4229 'offset' => $offset,
4230 ];
4231
4232 $forums = get_posts( $forum_args );
4233 if ( ! empty( $forums ) ) {
4234 foreach ( $forums as $key => $forum ) {
4235 $options[] = [
4236 'label' => $forum->post_title,
4237 'value' => $forum->ID,
4238 ];
4239 }
4240 }
4241 $count = count( $options );
4242 return [
4243 'options' => $options,
4244 'hasMore' => $count > $limit && $count > $offset,
4245 ];
4246 }
4247
4248 /**
4249 * Get BuddyBoss Forums List.
4250 *
4251 * @param array $data data.
4252 *
4253 * @return array|void
4254 */
4255 public function search_bb_topics_list( $data ) {
4256 $options = [];
4257
4258 $page = $data['page'];
4259 $limit = Utilities::get_search_page_limit();
4260 $offset = $limit * ( $page - 1 );
4261
4262 if ( ! function_exists( 'bbp_get_topic_post_type' ) ) {
4263 return [];
4264 }
4265
4266 $topic_args = [
4267 'post_type' => bbp_get_topic_post_type(),
4268 'post_parent' => $data['dynamic'],
4269 'orderby' => 'title',
4270 'order' => 'ASC',
4271 'post_status' => [ 'publish', 'private' ],
4272 'posts_per_page' => $limit,
4273 'offset' => $offset,
4274 ];
4275
4276 $topics = get_posts( $topic_args );
4277 if ( ! empty( $topics ) ) {
4278 foreach ( $topics as $key => $topic ) {
4279 $options[] = [
4280 'label' => $topic->post_title,
4281 'value' => $topic->ID,
4282 ];
4283 }
4284 }
4285 $count = count( $options );
4286 return [
4287 'options' => $options,
4288 'hasMore' => $count > $limit && $count > $offset,
4289 ];
4290 }
4291
4292 /**
4293 * Prepare elementor forms.
4294 *
4295 * @param array $data Search Params.
4296 *
4297 * @return array
4298 */
4299 public function search_elementor_forms( $data ) {
4300
4301 $elementor_forms = Utilities::get_elementor_forms();
4302
4303 $options = [];
4304 if ( ! empty( $elementor_forms ) ) {
4305 foreach ( $elementor_forms as $key => $value ) {
4306 $options[] = [
4307 'label' => $value,
4308 'value' => $key,
4309 ];
4310 }
4311 }
4312
4313 return [
4314 'options' => $options,
4315 'hasMore' => false,
4316 ];
4317 }
4318
4319 /**
4320 * Prepare elementor forms.
4321 *
4322 * @param array $data Search Params.
4323 *
4324 * @return array[]
4325 */
4326 public function search_new_elementor_forms( $data ) {
4327
4328 global $wpdb;
4329 $elementor_forms = [];
4330 $post_metas = $wpdb->get_results(
4331 $wpdb->prepare(
4332 "SELECT pm.post_id, pm.meta_value
4333 FROM $wpdb->postmeta pm
4334 LEFT JOIN $wpdb->posts p
4335 ON p.ID = pm.post_id
4336 WHERE p.post_type IS NOT NULL
4337 AND p.post_status = %s
4338 AND pm.meta_key = %s
4339 AND pm.`meta_value` LIKE %s",
4340 'publish',
4341 '_elementor_data',
4342 '%%form_fields%%'
4343 )
4344 );
4345
4346 if ( ! empty( $post_metas ) ) {
4347 foreach ( $post_metas as $post_meta ) {
4348 /**
4349 *
4350 * Ignore line
4351 *
4352 * @phpstan-ignore-next-line
4353 */
4354 $inner_forms = Utilities::search_elementor_forms( json_decode( $post_meta->meta_value ) );
4355 if ( ! empty( $inner_forms ) ) {
4356 foreach ( $inner_forms as $form ) {
4357 /**
4358 *
4359 * Ignore line
4360 *
4361 * @phpstan-ignore-next-line
4362 */
4363 $elementor_forms[ $post_meta->post_id . '_' . $form->id ] = $form->settings->form_name . ' (' . $form->id . ')';
4364 }
4365 }
4366 }
4367 }
4368
4369 $options = [];
4370 if ( ! empty( $elementor_forms ) ) {
4371 foreach ( $elementor_forms as $key => $value ) {
4372 $options[] = [
4373 'label' => $value,
4374 'value' => $key,
4375 ];
4376 }
4377 }
4378
4379 /**
4380 *
4381 * Ignore line
4382 *
4383 * @phpstan-ignore-next-line
4384 */
4385 return [
4386 'options' => $options,
4387 'hasMore' => false,
4388 ];
4389 }
4390
4391 /**
4392 * Prepare elementor form fields.
4393 *
4394 * @param array $data Search Params.
4395 *
4396 * @return array
4397 */
4398 public function search_pluggable_elementor_form_fields( $data ) {
4399 $result = [];
4400 $form_id = absint( $data['dynamic'] );
4401 $elementor_form_fields = ( new Utilities() )->get_elementor_form_fields( $data );
4402 $options = [];
4403 if ( ! empty( $elementor_form_fields ) ) {
4404 foreach ( $elementor_form_fields as $key => $value ) {
4405 $options[] = [
4406 'label' => $value,
4407 'value' => '{' . $key . '}',
4408 ];
4409 }
4410 }
4411
4412 return [
4413 'options' => $options,
4414 'hasMore' => false,
4415 ];
4416 }
4417
4418 /**
4419 * Get all events
4420 *
4421 * @param array $data Data array.
4422 *
4423 * @return array
4424 */
4425 public function search_event_calendar_event( $data ) {
4426 $page = $data['page'];
4427 $limit = Utilities::get_search_page_limit();
4428 $offset = $limit * ( $page - 1 );
4429
4430 $posts = get_posts(
4431 [
4432 'post_type' => 'tribe_events',
4433 'orderby' => 'title',
4434 'order' => 'ASC',
4435 'post_status' => 'publish',
4436 'posts_per_page' => $limit,
4437 'offset' => $offset,
4438 ]
4439 );
4440
4441 $options = [];
4442 if ( ! empty( $posts ) ) {
4443 foreach ( $posts as $post ) {
4444 $options[] = [
4445 'label' => $post->post_title,
4446 'value' => $post->ID,
4447 ];
4448 }
4449 }
4450
4451 $count = wp_count_posts( 'tribe_events' )->publish;
4452
4453 return [
4454 'options' => $options,
4455 'hasMore' => $count > $limit && $count > $offset,
4456 ];
4457 }
4458
4459 /**
4460 * Get all events tickets
4461 *
4462 * @param array $data Data array.
4463 *
4464 * @return array
4465 */
4466 public function search_event_calendar_event_tickets( $data ) {
4467 $event_id = $data['dynamic'];
4468 $options = [];
4469
4470 if ( ! class_exists( 'Tribe__Tickets_Plus__Commerce__WooCommerce__Main' ) ) {
4471 return [];
4472 }
4473
4474 $tickets = \Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance()->get_all_event_tickets( $event_id );
4475 if ( ! empty( $tickets ) ) {
4476 foreach ( $tickets as $ticket_object ) {
4477 $options[] = [
4478 'label' => $ticket_object->name,
4479 'value' => $ticket_object->ID,
4480 ];
4481 }
4482 }
4483
4484 return [
4485 'options' => $options,
4486 'hasMore' => false,
4487 ];
4488 }
4489
4490 /**
4491 * Prepare rsvp event calendar events.
4492 *
4493 * @param array $data Search Params.
4494 *
4495 * @return array
4496 */
4497 public function search_event_calendar_rsvp_event( $data ) {
4498
4499 if ( ! class_exists( 'Tribe__Tickets__Tickets_Handler' ) ) {
4500 return [];
4501 }
4502 $page = $data['page'];
4503 $limit = Utilities::get_search_page_limit();
4504 $offset = $limit * ( $page - 1 );
4505
4506 $posts = get_posts(
4507 [
4508 'post_type' => 'tribe_events',
4509 'orderby' => 'title',
4510 'order' => 'ASC',
4511 'post_status' => 'publish',
4512 'posts_per_page' => $limit,
4513 'offset' => $offset,
4514 ]
4515 );
4516
4517 $options = [];
4518 if ( ! empty( $posts ) ) {
4519 $ticket_handler = new Tribe__Tickets__Tickets_Handler();
4520 foreach ( $posts as $post ) {
4521
4522 $get_rsvp_ticket = $ticket_handler->get_event_rsvp_tickets( $post->ID );
4523
4524 if ( ! empty( $get_rsvp_ticket ) ) {
4525 $options[] = [
4526 'label' => $post->post_title,
4527 'value' => $post->ID,
4528 ];
4529 }
4530 }
4531 }
4532 $count = wp_count_posts( 'tribe_events' )->publish;
4533
4534 return [
4535 'options' => $options,
4536 'hasMore' => $count > $limit && $count > $offset,
4537 ];
4538 }
4539
4540 /**
4541 * Prepare Restrict Content Membership Level.
4542 *
4543 * @param array $data Search Params.
4544 *
4545 * @return array
4546 */
4547 public function search_restrictcontent_membership_level( $data ) {
4548 if ( ! function_exists( 'rcp_get_membership_levels' ) ) {
4549 return [];
4550 }
4551 $rcp_memberships = rcp_get_membership_levels();
4552 $options = [];
4553
4554 if ( ! empty( $rcp_memberships ) ) {
4555 foreach ( $rcp_memberships as $list ) {
4556 $options[] = [
4557 'label' => ucfirst( $list->name ),
4558 'value' => $list->id,
4559 ];
4560 }
4561 }
4562
4563 return [
4564 'options' => $options,
4565 'hasMore' => false,
4566 ];
4567 }
4568
4569 /**
4570 * Prepare Restrict Content Customer.
4571 *
4572 * @param array $data Search Params.
4573 *
4574 * @return array
4575 */
4576 public function search_restrictcontent_customer( $data ) {
4577 if ( ! function_exists( 'rcp_get_memberships' ) ) {
4578 return [];
4579 }
4580 $rcp_users = rcp_get_memberships();
4581 $options = [];
4582
4583 if ( ! empty( $rcp_users ) ) {
4584 foreach ( $rcp_users as $list ) {
4585 $user = get_user_by( 'ID', $list->get_user_id() );
4586 if ( $user instanceof \WP_User ) {
4587 $user_label = $user->user_email;
4588
4589 if ( $user->display_name !== $user->user_email ) {
4590 $user_label .= ' (' . $user->display_name . ')';
4591 }
4592
4593 $options[] = [
4594 'label' => $user_label,
4595 'value' => $list->get_customer_id(),
4596 ];
4597 }
4598 }
4599 }
4600
4601 return [
4602 'options' => $options,
4603 'hasMore' => false,
4604 ];
4605 }
4606
4607
4608 /**
4609 * Fetch the Presto Player video List.
4610 *
4611 * @param array $data Search Params.
4612 *
4613 * @return array
4614 */
4615 public function search_ap_presto_player_video_list( $data ) {
4616
4617 if ( ! class_exists( 'PrestoPlayer\Models\Video' ) ) {
4618 return [];
4619 }
4620
4621 $videos = ( new Video() )->all();
4622 $options = [];
4623 if ( ! empty( $videos ) ) {
4624 foreach ( $videos as $video ) {
4625 $options[] = [
4626 'label' => $video->__get( 'title' ),
4627 'value' => (string) $video->__get( 'id' ),
4628 ];
4629 }
4630 }
4631
4632 return [
4633 'options' => $options,
4634 'hasMore' => false,
4635 ];
4636 }
4637
4638 /**
4639 * Presto Player Video percentage.
4640 *
4641 * @param array $data Search Params.
4642 *
4643 * @return array
4644 */
4645 public function search_prestoplayer_video_percent( $data ) {
4646
4647 $percents = [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ];
4648 $options = [];
4649
4650 foreach ( $percents as $percent ) {
4651 $options[] = [
4652 'label' => $percent . '%',
4653 'value' => (string) $percent,
4654 ];
4655 }
4656
4657 return [
4658 'options' => $options,
4659 'hasMore' => false,
4660 ];
4661 }
4662
4663 /**
4664 * Get user profile field options.
4665 *
4666 * @return array
4667 * @since 1.0.0
4668 */
4669 public function search_user_field_options() {
4670
4671 $options = apply_filters(
4672 'sure_trigger_get_user_field_options',
4673 [
4674 [
4675 'label' => __( 'User Name', 'suretriggers' ),
4676 'value' => 'user_login',
4677 ],
4678 [
4679 'label' => __( 'User Email', 'suretriggers' ),
4680 'value' => 'user_email',
4681 ],
4682 [
4683 'label' => __( 'Display Name', 'suretriggers' ),
4684 'value' => 'display_name',
4685 ],
4686 [
4687 'label' => __( 'User Password', 'suretriggers' ),
4688 'value' => 'user_pass',
4689 ],
4690 [
4691 'label' => __( 'Website', 'suretriggers' ),
4692 'value' => 'user_url',
4693 ],
4694 ]
4695 );
4696
4697 return [
4698 'options' => $options,
4699 'hasMore' => false,
4700 ];
4701 }
4702
4703 /**
4704 * Get user post field options.
4705 *
4706 * @return array
4707 * @since 1.0.0
4708 */
4709 public function search_post_field_options() {
4710
4711 return [
4712 'options' => [
4713 [
4714 'label' => __( 'Type', 'suretriggers' ),
4715 'value' => 'post_type',
4716 'dynamic_field' => [
4717 'type' => 'select-creatable',
4718 'ajaxIdentifier' => 'post_types',
4719 ],
4720 ],
4721 [
4722 'label' => __( 'Status', 'suretriggers' ),
4723 'value' => 'post_status',
4724 'dynamic_field' => [
4725 'type' => 'select-async',
4726 'ajaxIdentifier' => 'post_statuses',
4727 ],
4728 ],
4729 [
4730 'label' => __( 'Author', 'suretriggers' ),
4731 'value' => 'post_author',
4732 'dynamic_field' => [
4733 'type' => 'select-async',
4734 'ajaxIdentifier' => 'user',
4735 ],
4736 ],
4737 [
4738 'label' => __( 'Title', 'suretriggers' ),
4739 'value' => 'post_title',
4740 'dynamic_field' => [
4741 'type' => 'select-creatable',
4742 ],
4743 ],
4744 [
4745 'label' => __( 'Slug', 'suretriggers' ),
4746 'value' => 'post_slug',
4747 'dynamic_field' => [
4748 'type' => 'select-creatable',
4749 ],
4750 ],
4751 [
4752 'label' => __( 'Content', 'suretriggers' ),
4753 'value' => 'post_content',
4754 'dynamic_field' => [
4755 'type' => 'html-editor',
4756 ],
4757 ],
4758 ],
4759 'hasMore' => false,
4760 ];
4761 }
4762
4763 /**
4764 * Bricksbuilder grouped data.
4765 *
4766 * @param array $data data.
4767 * @return array
4768 */
4769 public function search_bb_groups( $data ) {
4770
4771 global $wpdb;
4772 $options = [];
4773
4774 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'bp_groups' ) ) ) {
4775
4776 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %s', $wpdb->prefix . 'bp_groups' ) );
4777
4778 if ( $results ) {
4779 foreach ( $results as $result ) {
4780 $options[] = [
4781 'label' => $result->name,
4782 'value' => $result->id,
4783 ];
4784 }
4785 }
4786 }
4787
4788 return [
4789 'options' => $options,
4790 'hasMore' => false,
4791 ];
4792 }
4793
4794 /**
4795 * Search forms.
4796 *
4797 * @return array
4798 */
4799 public function search_bb_forums() {
4800 $options = [];
4801 if ( ! function_exists( 'bbp_get_forum_post_type' ) ) {
4802 return [];
4803 }
4804 $allowed_atatus = [ 'publish', 'private' ];
4805 $forum_args = [
4806 'post_type' => bbp_get_forum_post_type(),
4807 'posts_per_page' => -1,
4808 'orderby' => 'title',
4809 'order' => 'ASC',
4810 'post_status' => 'any',
4811 ];
4812 $forums = get_posts( $forum_args );
4813
4814 if ( ! empty( $forums ) ) {
4815 foreach ( $forums as $forum ) {
4816 if ( in_array( $forum->post_status, $allowed_atatus, true ) ) {
4817 $options[] = [
4818 'label' => $forum->post_title,
4819 'value' => $forum->ID,
4820 ];
4821 }
4822 }
4823 }
4824 return [
4825 'options' => $options,
4826 'hasMore' => false,
4827 ];
4828 }
4829
4830 /**
4831 * Search Affiliate WP Referral Type.
4832 *
4833 * @return array
4834 */
4835 public function search_affwp_referral_type() {
4836 $options = [];
4837
4838 if ( ! function_exists( 'affiliate_wp' ) ) {
4839 return [];
4840 }
4841
4842 $types = affiliate_wp()->referrals->types_registry->get_types();
4843 if ( ! empty( $types ) ) {
4844 foreach ( $types as $type_id => $type ) {
4845 $options[] = [
4846 'label' => $type['label'],
4847 'value' => $type_id,
4848 ];
4849 }
4850 }
4851 return [
4852 'options' => $options,
4853 'hasMore' => false,
4854 ];
4855 }
4856
4857 /**
4858 * Search Affiliate WP Referral Status.
4859 *
4860 * @return array
4861 */
4862 public function search_affwp_referral_status() {
4863 $options = [];
4864
4865 if ( ! function_exists( 'affwp_get_affiliate_statuses' ) ) {
4866 return [];
4867 }
4868
4869 $statuses = affwp_get_affiliate_statuses();
4870 if ( ! empty( $statuses ) ) {
4871 foreach ( $statuses as $key => $status ) {
4872 $options[] = [
4873 'label' => $status,
4874 'value' => $key,
4875 ];
4876 }
4877 }
4878 return [
4879 'options' => $options,
4880 'hasMore' => false,
4881 ];
4882 }
4883
4884 /**
4885 * Search Affiliate WP Affiliates list.
4886 *
4887 * @return array
4888 */
4889 public function search_affwp_affiliates_list() {
4890 $options = [];
4891
4892 global $wpdb;
4893 $affiliates = $wpdb->get_results( "SELECT affiliate_id FROM {$wpdb->prefix}affiliate_wp_affiliates" );
4894
4895 if ( ! function_exists( 'affwp_get_affiliate_name' ) ) {
4896 return [];
4897 }
4898
4899 if ( ! empty( $affiliates ) ) {
4900 foreach ( $affiliates as $affiliate ) {
4901 $options[] = [
4902 'label' => affwp_get_affiliate_name( $affiliate->affiliate_id ),
4903 'value' => $affiliate->affiliate_id,
4904 ];
4905 }
4906 }
4907 return [
4908 'options' => $options,
4909 'hasMore' => false,
4910 ];
4911 }
4912
4913 /**
4914 * Search Slice WP Affiliates list.
4915 *
4916 * @return array
4917 */
4918 public function search_slicewp_affiliates_list() {
4919 $options = [];
4920
4921 global $wpdb;
4922 $affiliates = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}slicewp_affiliates" );
4923
4924 if ( ! function_exists( 'slicewp_get_affiliate_name' ) ) {
4925 return [];
4926 }
4927
4928 if ( ! empty( $affiliates ) ) {
4929 foreach ( $affiliates as $affiliate ) {
4930 $options[] = [
4931 'label' => slicewp_get_affiliate_name( $affiliate->id ),
4932 'value' => $affiliate->id,
4933 ];
4934 }
4935 }
4936 return [
4937 'options' => $options,
4938 'hasMore' => false,
4939 ];
4940 }
4941
4942 /**
4943 * Search for commissions list.
4944 *
4945 * @param array $data data.
4946 *
4947 * @return array
4948 */
4949 public function search_slicewp_commissions_list( $data ) {
4950 $options = [];
4951 global $wpdb;
4952 $affiliate_id = $data['dynamic']['affiliate_id'];
4953 $commissions = $wpdb->get_results(
4954 $wpdb->prepare(
4955 "SELECT *
4956 FROM {$wpdb->prefix}slicewp_commissions WHERE affiliate_id=%d ORDER BY id DESC ",
4957 $affiliate_id
4958 )
4959 );
4960
4961
4962 if ( ! empty( $commissions ) ) {
4963 foreach ( $commissions as $commission ) {
4964 $options[] = [
4965 'label' => $commission->reference . '(' . $commission->type . ')',
4966 'value' => $commission->id,
4967 ];
4968 }
4969 }
4970 return [
4971 'options' => $options,
4972 'hasMore' => false,
4973 ];
4974 }
4975
4976
4977 /**
4978 * Search Affiliate WP Affiliates list.
4979 *
4980 * @return array
4981 */
4982 public function search_affwp_affiliates_rate_type() {
4983 $options = [];
4984
4985 if ( ! function_exists( 'affwp_get_affiliate_rate_types' ) ) {
4986 return [];
4987 }
4988
4989 $rate_types = affwp_get_affiliate_rate_types();
4990
4991 if ( ! empty( $rate_types ) ) {
4992 foreach ( $rate_types as $key => $rate_type ) {
4993 $options[] = [
4994 'label' => $rate_type,
4995 'value' => $key,
4996 ];
4997 }
4998 }
4999 return [
5000 'options' => $options,
5001 'hasMore' => false,
5002 ];
5003 }
5004
5005 /**
5006 * Get last data for trigger.
5007 *
5008 * @param array $data data.
5009 * @return array
5010 */
5011 public function search_affiliate_wp_triggers_last_data( $data ) {
5012 global $wpdb;
5013
5014 $context = [];
5015 $context['response_type'] = 'sample';
5016
5017 $user_data = WordPress::get_sample_user_context();
5018
5019 $affiliate_data = [
5020 'affiliate_id' => 1,
5021 'rest_id' => '',
5022 'user_id' => 1,
5023 'rate' => '',
5024 'rate_type' => '',
5025 'flat_rate_basis' => '',
5026 'payment_email' => 'admin@gmail.com',
5027 'status' => 'active',
5028 'earnings' => 0,
5029 'unpaid_earnings' => 0,
5030 'referrals' => 0,
5031 'visits' => 0,
5032 'date_registered' => '2023-01-18 13:35:22',
5033 'dynamic_coupon' => 'KDJSKS',
5034 ];
5035
5036 $referral_data = [
5037 'referral_id' => 1,
5038 'affiliate_id' => 1,
5039 'visit_id' => 0,
5040 'rest_id' => '',
5041 'customer_id' => 0,
5042 'parent_id' => 0,
5043 'description' => 'Testing',
5044 'status' => 'unpaid',
5045 'amount' => '12.00',
5046 'currency' => '',
5047 'custom' => 'custom',
5048 'context' => '',
5049 'campaign' => '',
5050 'reference' => 'Reference',
5051 'products' => '',
5052 'date' => '2023-01-18 16:36:59',
5053 'type' => 'opt-in',
5054 'payout_id' => 0,
5055 ];
5056
5057 if ( ! function_exists( 'affwp_get_dynamic_affiliate_coupons' ) || ! function_exists( 'affwp_get_affiliate' ) || ! function_exists( 'affwp_get_referral' ) ) {
5058 return [];
5059 }
5060
5061 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
5062
5063 if ( in_array( $term, [ 'affiliate_approved', 'affiliate_awaiting_approval' ], true ) ) {
5064 $status = 'affiliate_approved' === $term ? 'active' : 'pending';
5065 $affiliate = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_affiliates WHERE affiliate_id = ( SELECT max(affiliate_id) FROM {$wpdb->prefix}affiliate_wp_affiliates )" );
5066
5067 if ( ! empty( $affiliate ) ) {
5068 $affiliate = affwp_get_affiliate( $affiliate->affiliate_id );
5069 $affiliate_data = get_object_vars( $affiliate );
5070 $user_data = WordPress::get_user_context( $affiliate->user_id );
5071 $context['response_type'] = 'live';
5072 }
5073 $affiliate_data['status'] = $status;
5074 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data );
5075
5076 } elseif ( 'affiliate_makes_referral' == $term ) {
5077 $type = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
5078 $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 ) );
5079
5080 if ( ! empty( $referral ) ) {
5081 $referral = affwp_get_referral( $referral->referral_id );
5082 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
5083 $affiliate_data = get_object_vars( $affiliate );
5084 $user_data = WordPress::get_user_context( $affiliate->user_id );
5085 $referral_data = get_object_vars( $referral );
5086 $context['response_type'] = 'live';
5087 }
5088 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
5089 } elseif ( 'affiliate_wc_product_purchased' == $term ) {
5090 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
5091 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'woocommerce' ORDER BY referral_id DESC LIMIT 1" );
5092
5093 if ( ! empty( $referral ) ) {
5094 $referral = affwp_get_referral( $referral->referral_id );
5095 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
5096 $affiliate_data = get_object_vars( $affiliate );
5097 if ( ! empty( $referral ) && function_exists( 'wc_get_order' ) ) {
5098 $order_id = $referral->reference;
5099 $order = wc_get_order( $order_id );
5100 if ( $order instanceof \WC_Order ) {
5101 $user_data = WordPress::get_user_context( $order->get_customer_id() );
5102 }
5103 }
5104 $referral_data = get_object_vars( $referral );
5105 $context['response_type'] = 'live';
5106 }
5107 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
5108 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data, $dynamic_coupons );
5109 if ( ! empty( $referral ) && function_exists( 'wc_get_order' ) ) {
5110 $order_id = $referral->reference;
5111 $order = wc_get_order( $order_id );
5112 if ( $order instanceof \WC_Order ) {
5113 $items = $order->get_items();
5114 foreach ( $items as $item ) {
5115 $context['pluggable_data']['product'] = $item['product_id'];
5116 }
5117 }
5118 } else {
5119 $context['pluggable_data']['product'] = 1;
5120 }
5121 } elseif ( 'affiliate_edd_product_purchased' == $term ) {
5122 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
5123 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'edd' ORDER BY referral_id DESC LIMIT 1" );
5124
5125 if ( ! empty( $referral ) ) {
5126 $referral = affwp_get_referral( $referral->referral_id );
5127 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
5128 $affiliate_data = get_object_vars( $affiliate );
5129 $user_data = WordPress::get_user_context( $affiliate->user_id );
5130 $referral_data = get_object_vars( $referral );
5131 $context['response_type'] = 'live';
5132 }
5133 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
5134 if ( ! empty( $referral ) && function_exists( 'edd_get_payment' ) ) {
5135 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
5136 $edd_payment_id = $referral->reference;
5137 $payment = edd_get_payment( $edd_payment_id );
5138 $cart_details = $payment->cart_details;
5139 $payment = get_object_vars( $payment );
5140 $context['pluggable_data'] = array_merge( $context['pluggable_data'], $dynamic_coupons, $payment );
5141 foreach ( $cart_details as $detail ) {
5142 $context['pluggable_data']['product'] = $detail['id'];
5143 }
5144 } else {
5145 $context['pluggable_data']['product'] = 1;
5146 }
5147 } elseif ( 'affiliate_mb_product_purchased' == $term ) {
5148 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
5149 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'memberpress' ORDER BY referral_id DESC LIMIT 1" );
5150
5151 if ( ! empty( $referral ) && class_exists( '\MeprTransaction' ) ) {
5152 $referral = affwp_get_referral( $referral->referral_id );
5153 $reference_id = $referral->reference;
5154 $transaction = new MeprTransaction( $reference_id );
5155 $user_id = $transaction->user_id;
5156 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
5157 $affiliate_data = get_object_vars( $affiliate );
5158 $user_data = WordPress::get_user_context( $user_id );
5159 $referral_data = get_object_vars( $referral );
5160 $context['response_type'] = 'live';
5161 }
5162 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
5163 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data, $dynamic_coupons );
5164 if ( ! empty( $referral ) && class_exists( '\MeprTransaction' ) ) {
5165 $membership_id = $wpdb->get_var(
5166 $wpdb->prepare(
5167 "SELECT product_id FROM
5168 {$wpdb->prefix}mepr_transactions WHERE id = %d",
5169 $referral->reference
5170 )
5171 );
5172 $context['pluggable_data']['product'] = $membership_id;
5173 $context['pluggable_data']['product_name'] = get_the_title( $membership_id );
5174 } else {
5175 $context['pluggable_data']['product'] = 1;
5176 $context['pluggable_data']['product_name'] = 'membership1';
5177 }
5178 } elseif ( 'affiliate_referral_paid' == $term ) {
5179 $type = isset( $data['filter']['type']['value'] ) ? $data['filter']['type']['value'] : '';
5180 $referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE type = %s AND status = 'paid'", $type ) );
5181
5182 if ( ! empty( $referral ) ) {
5183 $referral = affwp_get_referral( $referral->referral_id );
5184 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
5185 $affiliate_data = get_object_vars( $affiliate );
5186 $user_data = WordPress::get_user_context( $affiliate->user_id );
5187 $referral_data = get_object_vars( $referral );
5188 $context['response_type'] = 'live';
5189 }
5190 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
5191 } elseif ( 'affiliate_referral_rejected' == $term ) {
5192 $type = isset( $data['filter']['type']['value'] ) ? $data['filter']['type']['value'] : '';
5193 $referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE type = %s AND status = 'rejected'", $type ) );
5194 if ( ! empty( $referral ) ) {
5195 $referral = affwp_get_referral( $referral->referral_id );
5196 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
5197 $affiliate_data = get_object_vars( $affiliate );
5198 $user_data = WordPress::get_user_context( $affiliate->user_id );
5199 $referral_data = get_object_vars( $referral );
5200 $context['response_type'] = 'live';
5201 }
5202 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
5203 } else {
5204 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE referral_id = ( SELECT max(referral_id) FROM {$wpdb->prefix}affiliate_wp_referrals )" );
5205
5206 if ( ! empty( $referral ) ) {
5207 $referral = affwp_get_referral( $referral->referral_id );
5208 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
5209 $affiliate_data = get_object_vars( $affiliate );
5210 $user_data = WordPress::get_user_context( $affiliate->user_id );
5211 $referral_data = get_object_vars( $referral );
5212 $context['response_type'] = 'live';
5213 }
5214 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
5215 }
5216
5217 return $context;
5218 }
5219
5220 /**
5221 * Get last data for trigger.
5222 *
5223 * @param array $data data.
5224 * @return array
5225 */
5226 public function search_jetpack_crm_triggers_last_data( $data ) {
5227
5228 if ( ! function_exists( 'zeroBS_getCompanyCount' ) || ! function_exists( 'zeroBS_getCustomerCount' ) || ! function_exists( 'zeroBS_getQuoteCount' ) ) {
5229 return [];
5230 }
5231
5232 global $wpdb;
5233
5234 $context = [];
5235 $context['response_type'] = 'sample';
5236
5237 $company_id = [ 'company_id' => 1 ];
5238 $contact_id = [ 'contact_id' => 1 ];
5239 $quote_id = [ 'quote_id' => 1 ];
5240 $event_id = [ 'event_id' => 1 ];
5241 $invoice_id = [ 'invoice_id' => 1 ];
5242 $transaction_id = [ 'transaction_id' => 1 ];
5243
5244 $company = [
5245 'company_id' => 1,
5246 'company_status' => 'Lead',
5247 'company_name' => 'Example Company',
5248 'company_email' => 'info@example.com',
5249 'main_address_line_1' => '123 Main Street',
5250 'main_address_line_2' => 'Suite 456',
5251 'main_address_city' => 'New York',
5252 'main_address_state' => 'NY',
5253 'main_address_postal_code' => '10001',
5254 'main_address_country' => 'United States',
5255 'second_address_line_1' => '789 Second Avenue',
5256 'second_address_line_2' => 'Floor 10',
5257 'second_address_city' => 'Los Angeles',
5258 'second_address_state' => 'CA',
5259 'second_address_postal_code' => '90001',
5260 'second_address_country' => 'United States',
5261 'main_telephone' => '+1 123-456-7890',
5262 'secondary_telephone' => '+1 987-654-3210',
5263 ];
5264
5265 $contact = [
5266 'contact_id' => 1,
5267 'status' => 'Lead',
5268 'prefix' => 'Mr.',
5269 'full_name' => 'John Doe',
5270 'first_name' => 'John',
5271 'last_name' => 'Doe',
5272 'email' => 'johndoe@example.com',
5273 'main_address_line_1' => '123 Main Street',
5274 'main_address_line_2' => 'Suite 456',
5275 'main_address_city' => 'New York',
5276 'main_address_state' => 'NY',
5277 'main_address_postal_code' => '10001',
5278 'main_address_country' => 'United States',
5279 'second_address_line_1' => '789 Second Avenue',
5280 'second_address_line_2' => 'Floor 10',
5281 'second_address_city' => 'Los Angeles',
5282 'second_address_state' => 'CA',
5283 'second_address_postal_code' => '90001',
5284 'second_address_country' => 'United States',
5285 'home_telephone' => '+1 123-456-7890',
5286 'work_telephone' => '+1 987-654-3210',
5287 'mobile_telephone' => '+1 555-555-5555',
5288 ];
5289
5290 $quote = [
5291 'quote_id' => 1,
5292 'contact_id' => 2,
5293 'contact_email' => 'john@example.com',
5294 'contact_name' => 'John Doe',
5295 'status' => '<strong>Created, not yet accepted</strong>',
5296 'title' => 'Sample Quote',
5297 'value' => 1000,
5298 'date' => '2023-05-23',
5299 'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
5300 'notes' => 'Additional notes about the quote.',
5301 ];
5302
5303 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
5304
5305 switch ( $term ) {
5306 case 'company_created':
5307 if ( zeroBS_getCompanyCount() > 0 ) {
5308 $company_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_companies WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_companies )" );
5309 $company = JetpackCRM::get_company_context( $company_data->ID );
5310 $context['response_type'] = 'live';
5311 }
5312 $context['pluggable_data'] = $company;
5313 break;
5314 case 'company_deleted':
5315 $context['pluggable_data'] = $company_id;
5316 break;
5317 case 'contact_created':
5318 if ( zeroBS_getCustomerCount() > 0 ) {
5319 $contact_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_contacts WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_contacts )" );
5320 $contact = JetpackCRM::get_contact_context( $contact_data->ID );
5321 $context['response_type'] = 'live';
5322 }
5323 $context['pluggable_data'] = $contact;
5324 break;
5325 case 'contact_deleted':
5326 $context['pluggable_data'] = $contact_id;
5327 break;
5328 case 'event_deleted':
5329 $context['pluggable_data'] = $event_id;
5330 break;
5331 case 'invoice_deleted':
5332 $context['pluggable_data'] = $invoice_id;
5333 break;
5334 case 'quote_accepted':
5335 case 'quote_created':
5336 if ( zeroBS_getQuoteCount() > 0 ) {
5337 $quote_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_quotes WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_quotes )" );
5338 $quote = JetpackCRM::get_quote_context( $quote_data->ID );
5339 $context['response_type'] = 'live';
5340 }
5341 $context['pluggable_data'] = $quote;
5342 break;
5343 case 'quote_deleted':
5344 $context['pluggable_data'] = $quote_id;
5345 break;
5346 case 'transaction_deleted':
5347 $context['pluggable_data'] = $transaction_id;
5348 break;
5349 }
5350
5351 return $context;
5352 }
5353
5354 /**
5355 * Get last data for trigger.
5356 *
5357 * @param array $data data.
5358 * @return array
5359 */
5360 public function search_funnel_kit_automations_triggers_last_data( $data ) {
5361
5362 if ( ! class_exists( 'BWFCRM_Contact' ) || ! class_exists( 'BWFCRM_Lists' ) || ! class_exists( 'BWFCRM_Tag' ) ) {
5363 return [];
5364 }
5365
5366 $context = [];
5367 $context['response_type'] = 'sample';
5368
5369 $contact = [
5370 'contact_id' => '1',
5371 'wpid' => '0',
5372 'uid' => '9e74246335fd81b1c4a9123842c12549',
5373 'email' => 'johndoe@example.com',
5374 'first_name' => 'John',
5375 'last_name' => 'Doe',
5376 'contact_no' => '+1 555-555-5555',
5377 'state' => 'NY',
5378 'country' => 'United States',
5379 'timezone' => 'New York',
5380 'creation_date' => '2023-05-29 15:26:03',
5381 'last_modified' => '2023-05-29 17:08:30',
5382 'source' => '',
5383 'type' => 'Los Angeles',
5384 'status' => '0',
5385 'tags' => '["1"]',
5386 'lists' => '["2","1"]',
5387 ];
5388
5389 $list = [
5390 'list_id' => 1,
5391 'list_name' => 'Sample List',
5392 ];
5393
5394 $tag = [
5395 'tag_id' => 1,
5396 'tag_name' => 'Sample Tag',
5397 ];
5398
5399 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
5400
5401 $recent_contacts = \BWFCRM_Contact::get_recent_contacts( 0, 1, [] );
5402 $contact_email = count( $recent_contacts['contacts'] ) > 0 && isset( $recent_contacts['contacts'][0]['email'] ) ? $recent_contacts['contacts'][0]['email'] : '';
5403
5404 $real_contact = false;
5405 if ( ! empty( $contact_email ) ) {
5406 $contact_obj = \BWFCRM_Contact::get_contacts( $contact_email, 0, 1, [], [], OBJECT );
5407
5408 if ( isset( $contact_obj['contacts'][0] ) ) {
5409 $contact = FunnelKitAutomations::get_contact_context( $contact_obj['contacts'][0]->contact );
5410 $real_contact = true;
5411 }
5412 }
5413
5414 if ( 'contact_added_to_list' === $term || 'contact_removed_from_list' === $term ) {
5415 $list_id = (int) ( isset( $data['filter']['list_id']['value'] ) ? $data['filter']['list_id']['value'] : '-1' );
5416
5417 if ( -1 === $list_id ) {
5418 $lists = \BWFCRM_Lists::get_lists( [], '', 0, 1 );
5419 $list_id = count( $lists ) > 0 ? $lists[0]['ID'] : -1;
5420 }
5421
5422
5423 if ( -1 !== $list_id ) {
5424 $list = FunnelKitAutomations::get_list_context( $list_id );
5425 $context['response_type'] = $real_contact ? 'live' : 'sample';
5426 }
5427
5428 $context['pluggable_data'] = array_merge( $list, $contact );
5429 } else {
5430 $tag_id = (int) ( isset( $data['filter']['tag_id']['value'] ) ? $data['filter']['tag_id']['value'] : '-1' );
5431
5432 if ( -1 === $tag_id ) {
5433 $tags = \BWFCRM_Tag::get_tags( [], '', 0, 1 );
5434 $tag_id = count( $tags ) > 0 ? $tags[0]['ID'] : -1;
5435 }
5436
5437
5438 if ( -1 !== $tag_id ) {
5439 $tag = FunnelKitAutomations::get_tag_context( $tag_id );
5440 $context['response_type'] = $real_contact ? 'live' : 'sample';
5441 }
5442
5443 $context['pluggable_data'] = array_merge( $tag, $contact );
5444 }
5445
5446 return $context;
5447 }
5448 /**
5449 * Prepare EDD Order Status List.
5450 *
5451 * @param array $data Search Params.
5452 * @return array{options: array<int, array{label: string, value: string}>, hasMore: bool}
5453 */
5454 public function search_edd_order_status_list( $data ) {
5455 $options = [];
5456
5457 if ( function_exists( 'edd_get_payment_statuses' ) ) {
5458 $order_statuses = edd_get_payment_statuses();
5459
5460 if ( ! empty( $order_statuses ) ) {
5461 foreach ( $order_statuses as $key => $label ) {
5462 $options[] = [
5463 'label' => $label,
5464 'value' => $key,
5465 ];
5466 }
5467 }
5468 }
5469
5470 return [
5471 'options' => $options,
5472 'hasMore' => false,
5473 ];
5474 }
5475
5476 /**
5477 * Get last data for trigger.
5478 *
5479 * @param array $data data.
5480 * @return array
5481 */
5482 public function search_edd_triggers_last_data( $data ) {
5483 global $wpdb;
5484 if ( ! function_exists( 'edd_get_payments' ) ) {
5485 return [];
5486 }
5487 $context = [];
5488 $context['response_type'] = 'sample';
5489 $context['pluggable_data'] = [];
5490 $order_data = [
5491 'order_id' => 187,
5492 'customer_email' => 'john_doe@gmail.com',
5493 'customer_id' => 2,
5494 'user_id' => 1,
5495 'customer_first_name' => 'Sure',
5496 'customer_last_name' => 'Dev',
5497 'ordered_items' => 'Price with license — Price one',
5498 'currency' => 'USD',
5499 'status' => 'complete',
5500 'discount_codes' => '',
5501 'order_discounts' => 0.00,
5502 'order_subtotal' => 12.00,
5503 'order_tax' => 0.00,
5504 'order_total' => 12.00,
5505 'payment_method' => 'manual',
5506 'purchase_key' => 'd797b9576a3895e7424bae2417ed87df',
5507 'ordered_items_ids' => 17250,
5508 'download_id' => 17250,
5509 'license_key' => 'f7736093411cfaed18b56ec60227117b',
5510 'license_key_expire_date' => '1697524076',
5511 'license_key_status' => 'inactive',
5512 ];
5513
5514 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
5515 $download_id = isset( $data['filter']['download_id']['value'] ) ? $data['filter']['download_id']['value'] : 0;
5516 if ( 'order_created' === $term || 'order_one_product' === $term ) {
5517 $order_data['purchase_key'] = '06d3b7d923ca922dc889354f9bc33fbb';
5518
5519 $args = [
5520 'number' => 1,
5521 'status' => [ 'complete', 'refunded', 'partially_refunded', 'renewal' ],
5522 ];
5523 if ( $download_id > 0 ) {
5524 $args['download'] = $download_id;
5525 }
5526 $payments = edd_get_payments( $args );
5527 if ( count( $payments ) > 0 ) {
5528 $order_data = EDD::get_product_purchase_context( $payments[0], $term, $download_id );
5529
5530 $context['response_type'] = 'live';
5531 } else {
5532 if ( 'order_one_product' === $term ) {
5533 $order_data['price_id'] = 1;
5534 }
5535 }
5536 } elseif ( 'order_status_changes' === $term ) {
5537 $order_id = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}edd_orders ORDER BY id DESC LIMIT 1" );
5538 if ( ! function_exists( 'edd_get_order' ) ) {
5539 return $context;
5540 }
5541 $order = edd_get_order( $order_id );
5542
5543 if ( $order ) {
5544 $downloads = [];
5545
5546 foreach ( $order->get_items() as $item ) {
5547 $downloads[] = [
5548 'id' => $item->product_id,
5549 'name' => get_the_title( $item->product_id ),
5550 'quantity' => $item->quantity,
5551 'price' => $item->price,
5552 ];
5553 }
5554
5555 $context['pluggable_data'] = [
5556 'order_id' => $order_id,
5557 'customer_id' => $order->customer_id,
5558 'email' => $order->email,
5559 'total' => $order->total,
5560 'downloads' => $downloads,
5561 'date_created' => $order->date_created,
5562 'old_status' => 'completed',
5563 'new_status' => 'pending',
5564 ];
5565 $context['response_type'] = 'live';
5566 } else {
5567 $context['pluggable_data'] = [
5568 'order_id' => 123,
5569 'customer_id' => 5,
5570 'email' => 'sample@example.com',
5571 'total' => 25.00,
5572 'downloads' => [
5573 [
5574 'id' => 101,
5575 'name' => 'Sample Product',
5576 'quantity' => 1,
5577 'price' => 25.00,
5578 ],
5579 ],
5580 'date_created' => current_time( 'mysql' ),
5581 'old_status' => 'completed',
5582 'new_status' => 'pending',
5583 ];
5584 $context['response_type'] = 'sample';
5585 }
5586 } elseif ( 'stripe_payment_refunded' === $term ) {
5587 $args = [
5588 'number' => 1,
5589 'status' => 'complete',
5590 'type' => 'refund',
5591 ];
5592 $payments = edd_get_payments( $args );
5593
5594 if ( count( $payments ) > 0 ) {
5595 $order_data = EDD::get_purchase_refund_context( $payments[0] );
5596 $context['response_type'] = 'live';
5597 }
5598 } else {
5599 $status = isset( $data['post_type'] ) ? $data['post_type'] : '';
5600 if ( ! empty( $status ) ) {
5601 if ( $download_id > 0 ) {
5602 $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 ) );
5603 } else {
5604 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE status= %s ORDER BY id DESC", $status ) );
5605 }
5606 } else {
5607 if ( $download_id > 0 ) {
5608 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE download_id=%d ORDER BY id DESC", $download_id ) );
5609 } else {
5610 $licesnses = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}edd_licenses ORDER BY id DESC" );
5611 }
5612 }
5613 if ( ! empty( $licesnses ) ) {
5614 $order_data = EDD::edd_get_license_data( $licesnses->id, $licesnses->download_id, $licesnses->payment_id );
5615 $context['response_type'] = 'live';
5616 } else {
5617 $order_data = [
5618 'ID' => 1,
5619 'key' => '23232323232',
5620 'customer_email' => 'suretest@example.com',
5621 'customer_name' => 'Sure Test',
5622 'product_id' => 1,
5623 'download_id' => 1,
5624 'product_name' => 'Test',
5625 'activation_limit' => 2,
5626 'activation_count' => 1,
5627 'activated_urls' => 'https://example.com',
5628 'expiration' => '1686297914',
5629 'is_lifetime' => '0',
5630 'status' => $status,
5631 ];
5632
5633 }
5634 }
5635
5636 if ( empty( $context['pluggable_data'] ) ) {
5637 $context['pluggable_data'] = $order_data;
5638 }
5639 return $context;
5640 }
5641
5642 /**
5643 * Get last data for trigger.
5644 *
5645 * @param array $data data.
5646 * @return array
5647 */
5648 public function search_presto_player_triggers_last_data( $data ) {
5649 $context = [];
5650 if ( ! class_exists( 'PrestoPlayer\Models\Video' ) ) {
5651 return [];
5652 }
5653 $context['response_type'] = 'sample';
5654
5655 $user_data = WordPress::get_sample_user_context();
5656
5657 $video_data = [
5658 'pp_video' => '1',
5659 'pp_video_percentage' => '100',
5660 ];
5661
5662 $video_data['video'] = [
5663 'id' => '1',
5664 'title' => 'OttoKit Is Here 🎉 The Easiest Automation Builder WordPress Websites & Apps',
5665 'type' => 'youtube',
5666 'external_id' => '-cYbNYgylLs',
5667 'attachment_id' => '0',
5668 'post_id' => '127',
5669 'src' => 'https://www.youtube.com/watch?v=-cYbNYgylLs',
5670 'created_by' => '1',
5671 'created_at' => '2022-11-10 00:28:25',
5672 'updated_at' => '2022-11-10 00:34:40',
5673 'deleted_at' => '',
5674 ];
5675
5676 $video_data['media']['tag'] = [
5677 'Demo',
5678 'Demo1',
5679 ];
5680
5681 $mediahub_data = [
5682 'post_id' => '1',
5683 'activity_id' => '2',
5684 'post_author' => '1',
5685 'post_content' => 'New sample post...!',
5686 'post_title' => 'Sample Post',
5687 'post_excerpt' => 'sample',
5688 'post_status' => 'publish',
5689 'post_type' => 'pp_video_block',
5690 ];
5691
5692 $videos = ( new Video() )->all();
5693
5694 if ( count( $videos ) > 0 ) {
5695 if ( ! empty( $data['filter'] ) ) {
5696 $video_id = $data['filter']['pp_video']['value'];
5697 if ( -1 === $video_id ) {
5698 global $wpdb;
5699 $args = [
5700 'numberposts' => 1,
5701 'orderby' => 'rand',
5702 'order' => 'ASC',
5703 'post_type' => 'pp_video_block',
5704 ];
5705 $posts = get_posts( $args );
5706 $presto_post_video_id = $posts[0]->ID;
5707 $presto_video_id = $wpdb->get_var( $wpdb->prepare( 'SELECT * FROM wp_presto_player_videos WHERE post_id = %d', $presto_post_video_id ) );
5708 $video_id = $presto_video_id;
5709 }
5710 $pp_videos = ( new Video( $video_id ) )->toArray();
5711 if ( ! empty( $pp_videos ) ) {
5712 $video_data = [];
5713 $video_data['video'] = $pp_videos;
5714 if ( is_array( $video_data ) && array_key_exists( 'post_id', $video_data['video'] ) ) {
5715 $mediahub_data = WordPress::get_post_context( (int) $video_data['video']['post_id'] );
5716 $media_tags = get_the_terms( (int) $video_data['video']['post_id'], 'pp_video_tag' );
5717 if ( ! empty( $media_tags ) && is_array( $media_tags ) && isset( $media_tags[0] ) ) {
5718 $tag_name = [];
5719 foreach ( $media_tags as $tag ) {
5720 $tag_name[] = $tag->name;
5721 }
5722 $video_data['media']['tag'] = $tag_name;
5723 }
5724 }
5725 $video_data['pp_video'] = $video_id;
5726 $video_data['pp_video_percentage'] = isset( $data['filter']['pp_video_percentage']['value'] ) ? $data['filter']['pp_video_percentage']['value'] : '100';
5727 $user_data = WordPress::get_user_context( (int) $video_data['video']['created_by'] );
5728
5729 $context['response_type'] = 'live';
5730 }
5731 }
5732 }
5733
5734 $context['pluggable_data'] = array_merge( $user_data, $video_data, $mediahub_data );
5735
5736 return $context;
5737 }
5738
5739 /**
5740 * Get last data for trigger.
5741 *
5742 * @param array $data data.
5743 * @return array
5744 */
5745 public function search_member_press_triggers_last_data( $data ) {
5746 global $wpdb;
5747
5748 $term = $data['search_term'] ? $data['search_term'] : '';
5749
5750 $context = [];
5751 $context['response_type'] = 'sample';
5752
5753 $user_data = WordPress::get_sample_user_context();
5754
5755 $membership_data = [
5756 'membership_id' => '190',
5757 'membership_title' => 'Sample Membership',
5758 'amount' => '12.00',
5759 'total' => '12.00',
5760 'tax_amount' => '0.00',
5761 'tax_rate' => '0.00',
5762 'trans_num' => 't_63a03f3334f44',
5763 'status' => 'complete',
5764 'subscription_id' => '0',
5765 'membership_url' => site_url() . '/register/premium/',
5766 'membership_featured_image_id' => '521',
5767 'membership_featured_image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
5768 ];
5769
5770 $membership_id = (int) ( isset( $data['filter']['membership_id']['value'] ) ? $data['filter']['membership_id']['value'] : '-1' );
5771
5772 if ( in_array( $term, [ 'mepr-event-transaction-expired', 'mepr_subscription_transition_status', 'mepr-event-transaction-paused' ] ) ) {
5773 $status = 'cancelled';
5774 if ( 'mepr-event-transaction-paused' === $term ) {
5775 $status = 'suspended';
5776 }
5777 if ( $membership_id > 0 ) {
5778 $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 ) );
5779 } else {
5780 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_subscriptions WHERE status=%s ORDER BY id DESC LIMIT 1", $status ) );
5781
5782 }
5783 if ( ! empty( $subscription ) ) {
5784
5785 $membership_data = MemberPress::get_subscription_context( $subscription );
5786 $user_data = WordPress::get_user_context( $subscription->user_id );
5787
5788
5789 $context['response_type'] = 'live';
5790 }
5791 } elseif ( 'mepr-coupon-code-redeemed' === $term ) {
5792
5793 $coupon_id = (int) ( isset( $data['filter']['coupon_id']['value'] ) ? $data['filter']['coupon_id']['value'] : '-1' );
5794
5795 if ( $coupon_id > 0 ) {
5796 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE coupon_id= %s ORDER BY id DESC LIMIT 1", $coupon_id ) );
5797 } else {
5798 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE coupon_id!= %s ORDER BY id DESC LIMIT 1", '0' ) );
5799 }
5800
5801
5802 if ( ! empty( $subscription ) ) {
5803
5804 $membership_data = MemberPress::get_membership_context( $subscription );
5805 $user_data = WordPress::get_user_context( $subscription->user_id );
5806 $membership_data['coupon_id'] = $subscription->coupon_id;
5807 $membership_data['coupon'] = get_post( $subscription->coupon_id );
5808 $context['response_type'] = 'live';
5809 }
5810 } else {
5811
5812 if ( $membership_id > 0 ) {
5813
5814 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE product_id= %s ORDER BY id DESC LIMIT 1", $membership_id ) );
5815 } else {
5816 $subscription = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mepr_transactions ORDER BY id DESC LIMIT 1" );
5817 }
5818
5819
5820 if ( ! empty( $subscription ) ) {
5821
5822 $membership_data = MemberPress::get_membership_context( $subscription );
5823 $user_data = WordPress::get_user_context( $subscription->user_id );
5824
5825
5826 $context['response_type'] = 'live';
5827 }
5828 }
5829
5830
5831 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5832 return $context;
5833 }
5834
5835 /**
5836 * Get last data for trigger.
5837 *
5838 * @param array $data data.
5839 * @return array
5840 */
5841 public function search_wishlist_member_triggers_last_data( $data ) {
5842 global $wpdb;
5843
5844 $context = [];
5845 $context['response_type'] = 'sample';
5846
5847 $user_data = WordPress::get_sample_user_context();
5848
5849 $membership_data = [
5850 'membership_level_id' => '1',
5851 'membership_level_name' => 'Sample Membership Level',
5852 ];
5853
5854 $membership_level_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
5855
5856 if ( $membership_level_id > 0 ) {
5857 $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 ) );
5858 } else {
5859 $membership = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}wlm_userlevels ORDER BY id DESC LIMIT 1" );
5860 }
5861 if ( ! empty( $membership ) ) {
5862 $membership_data = WishlistMember::get_membership_detail_context( (int) $membership->level_id, (int) $membership->user_id );
5863 $user_data = WordPress::get_user_context( $membership->user_id );
5864
5865 $context['response_type'] = 'live';
5866 }
5867
5868 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5869
5870 return $context;
5871 }
5872
5873 /**
5874 * Get last data for trigger.
5875 *
5876 * @param array $data data.
5877 * @return array
5878 */
5879 public function search_peepso_triggers_last_data( $data ) {
5880 global $wpdb;
5881
5882 $context = [];
5883 $context['response_type'] = 'sample';
5884
5885 $user_data = WordPress::get_sample_user_context();
5886
5887 $post_data = [
5888 'post_id' => '1',
5889 'activity_id' => '2',
5890 'post_author' => '1',
5891 'post_content' => 'New sample post...!',
5892 'post_title' => 'Sample Post',
5893 'post_excerpt' => 'sample',
5894 'post_status' => 'publish',
5895 'post_type' => 'peepso-post',
5896 ];
5897
5898 $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" );
5899
5900 if ( ! empty( $post ) ) {
5901 $post_data = PeepSo::get_pp_activity_context( (int) $post->act_external_id, (int) $post->act_id );
5902 $user_data = WordPress::get_user_context( $post->act_owner_id );
5903
5904 $context['response_type'] = 'live';
5905 }
5906
5907 $context['pluggable_data'] = array_merge( $user_data, $post_data );
5908
5909 return $context;
5910 }
5911
5912 /**
5913 * Get last data for Peepso User triggers.
5914 *
5915 * @param array $data data.
5916 * @return array
5917 */
5918 public function search_peepso_user_triggers_last_data( $data ) {
5919 global $wpdb;
5920
5921 $context = [];
5922 $context['response_type'] = 'sample';
5923 $term = $data['search_term'];
5924
5925 if ( ! class_exists( 'PeepSoUser' ) ) {
5926 return [];
5927 }
5928
5929 if ( 'user_follows_member' === $term || 'user_gains_follower' === $term ) {
5930 $member_id = $data['filter']['follow_user_id']['value'];
5931 if ( -1 === $member_id ) {
5932 $followers = $wpdb->get_results(
5933 $wpdb->prepare(
5934 "SELECT * FROM
5935 {$wpdb->prefix}peepso_user_followers WHERE uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5936 1
5937 )
5938 );
5939 } else {
5940 $followers = $wpdb->get_results(
5941 $wpdb->prepare(
5942 "SELECT * FROM
5943 {$wpdb->prefix}peepso_user_followers WHERE uf_passive_user_id = %d AND
5944 uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5945 $member_id,
5946 1
5947 )
5948 );
5949 }
5950 } elseif ( 'user_unfollows_member' === $term || 'user_loses_follower' === $term ) {
5951 $member_id = $data['filter']['follow_user_id']['value'];
5952 if ( -1 === $member_id ) {
5953 $followers = $wpdb->get_results(
5954 $wpdb->prepare(
5955 "SELECT * FROM
5956 {$wpdb->prefix}peepso_user_followers WHERE uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5957 0
5958 )
5959 );
5960 } else {
5961 $followers = $wpdb->get_results(
5962 $wpdb->prepare(
5963 "SELECT * FROM
5964 {$wpdb->prefix}peepso_user_followers WHERE uf_passive_user_id = %d AND
5965 uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5966 $member_id,
5967 0
5968 )
5969 );
5970 }
5971 } elseif ( 'user_updates_avatar' === $term ) {
5972 $followers = $wpdb->get_results(
5973 $wpdb->prepare(
5974 "SELECT * FROM
5975 {$wpdb->prefix}peepso_users WHERE usr_avatar_custom = %d AND usr_role = %s
5976 ORDER BY usr_last_activity DESC LIMIT 1",
5977 1,
5978 'member'
5979 )
5980 );
5981 } elseif ( 'user_updates_specific_profile_field' === $term ) {
5982 $field_id = $data['filter']['user_profile_field_id']['value'];
5983 $followers = $wpdb->get_results(
5984 $wpdb->prepare(
5985 "SELECT * FROM
5986 {$wpdb->prefix}peepso_users WHERE usr_role = %s
5987 ORDER BY usr_last_activity DESC LIMIT 1",
5988 'member'
5989 )
5990 );
5991 }
5992 if ( 'user_updates_avatar' === $term ) {
5993 if ( ! empty( $followers ) ) {
5994 $context_data['user'] = WordPress::get_user_context( $followers[0]->usr_id );
5995 $context['response_type'] = 'live';
5996 } else {
5997 $context_data['user'] = WordPress::get_sample_user_context();
5998 }
5999 } elseif ( 'user_updates_specific_profile_field' === $term ) {
6000 if ( ! empty( $followers ) ) {
6001 $user = PeepSoUser::get_instance( $followers[0]->usr_id );
6002 $user->profile_fields->load_fields();
6003 $user_fields = $user->profile_fields->get_fields();
6004 foreach ( $user_fields as $key => $value ) {
6005 $val = get_user_meta( $followers[0]->usr_id, $value->key, true );
6006 if ( '' != $val ) {
6007 $context_data[ $value->title ] = $val;
6008 }
6009 }
6010 $curruser = get_userdata( $followers[0]->usr_id );
6011 $context_data['user_id'] = $followers[0]->usr_id;
6012 $context_data['user_email'] = $user->get_email();
6013 $context_data['avatar_url'] = $user->get_avatar();
6014 $context_data['profile_url'] = $user->get_profileurl();
6015 $context_data['about_me'] = get_user_meta( $followers[0]->usr_id, 'description', true );
6016 if ( $curruser instanceof \WP_User ) {
6017 $context_data['website'] = $curruser->user_url;
6018 }
6019 $context_data['role'] = $user->get_user_role();
6020 $context['response_type'] = 'live';
6021 } else {
6022 $context_data['user'] = WordPress::get_sample_user_context();
6023 }
6024 } else {
6025 if ( ! empty( $followers ) ) {
6026 $context_data['follower_user'] = WordPress::get_user_context( $followers[0]->uf_active_user_id );
6027 $context_data['following_user'] = WordPress::get_user_context( $followers[0]->uf_passive_user_id );
6028 $context['response_type'] = 'live';
6029 } else {
6030 $context_data['follower_user'] = WordPress::get_sample_user_context();
6031 $context_data['following_user'] = WordPress::get_sample_user_context();
6032 }
6033 }
6034
6035 $context['pluggable_data'] = $context_data;
6036
6037 return $context;
6038 }
6039
6040 /**
6041 * Search Peepso User Profile Fields list.
6042 *
6043 * @param array $data data.
6044 * @return array
6045 */
6046 public function search_peepso_profile_field_list( $data ) {
6047 $options = [];
6048 if ( ! class_exists( 'PeepSoUser' ) ) {
6049 return [];
6050 }
6051 $options = [
6052 [
6053 'label' => 'Allow others to "like" my profile',
6054 'value' => 'peepso_is_profile_likable',
6055 ],
6056 [
6057 'label' => 'Hide my birthday year',
6058 'value' => 'peepso_hide_birthday_year',
6059 ],
6060 [
6061 'label' => 'Who can see my profile',
6062 'value' => 'usr_profile_acc',
6063 ],
6064 [
6065 'label' => 'Who can post on my profile',
6066 'value' => 'peepso_profile_post_acc',
6067 ],
6068 [
6069 'label' => "Don't show my online status",
6070 'value' => 'peepso_hide_online_status',
6071 ],
6072 [
6073 'label' => 'My timezone',
6074 'value' => 'peepso_gmt_offset',
6075 ],
6076 ];
6077 $peepsouser = PeepSoUser::get_instance( 0 );
6078 $peepsouser->profile_fields->load_fields();
6079 $fields = $peepsouser->profile_fields->get_fields();
6080 foreach ( $fields as $field ) {
6081 if ( 1 == $field->published ) {
6082 $options[] = [
6083 'label' => $field->title,
6084 'value' => $field->id,
6085 ];
6086 }
6087 }
6088 return [
6089 'options' => $options,
6090 'hasMore' => false,
6091 ];
6092 }
6093
6094 /**
6095 * Search Peepso Roles list.
6096 *
6097 * @param array $data data.
6098 * @return array
6099 */
6100 public function search_peepso_role_list( $data ) {
6101 $options = [
6102 [
6103 'label' => 'Community Member',
6104 'value' => 'member',
6105 ],
6106 [
6107 'label' => 'Community Moderator',
6108 'value' => 'moderator',
6109 ],
6110 [
6111 'label' => 'Community Administrator',
6112 'value' => 'admin',
6113 ],
6114 [
6115 'label' => 'Banned',
6116 'value' => 'ban',
6117 ],
6118 [
6119 'label' => 'Pending user email verification',
6120 'value' => 'register',
6121 ],
6122 [
6123 'label' => 'Pending admin approval',
6124 'value' => 'verified',
6125 ],
6126 ];
6127 return [
6128 'options' => $options,
6129 'hasMore' => false,
6130 ];
6131 }
6132
6133 /**
6134 * Search Peepso Users list.
6135 *
6136 * @param array $data data.
6137 * @return array
6138 */
6139 public function search_peepso_follow_user_list( $data ) {
6140 $options = [];
6141 global $wpdb;
6142 $users = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}peepso_users", ARRAY_A );
6143 if ( count( $users ) > 0 ) {
6144 foreach ( $users as $user ) {
6145 $user_by_id = get_user_by( 'id', $user['usr_id'] );
6146 if ( $user_by_id instanceof \WP_User ) {
6147 $options[] = [
6148 'label' => sprintf( '%s %s [%s]', $user_by_id->last_name, $user_by_id->first_name, $user_by_id->user_email ),
6149 'value' => $user['usr_id'],
6150 ];
6151 } else {
6152 $options[] = [
6153 'label' => '#' . $user['usr_id'],
6154 'value' => $user['usr_id'],
6155 ];
6156 }
6157 }
6158 }
6159 return [
6160 'options' => $options,
6161 'hasMore' => false,
6162 ];
6163 }
6164
6165 /**
6166 * Get last data for trigger
6167 *
6168 * @param array $data data.
6169 * @return array
6170 */
6171 public function search_restrict_content_pro_triggers_last_data( $data ) {
6172 $context = [];
6173 if ( ! function_exists( 'rcp_get_memberships' ) ) {
6174 return [];
6175 }
6176 $context['response_type'] = 'sample';
6177
6178 $user_data = WordPress::get_sample_user_context();
6179
6180 $membership_data = [
6181 'membership_level_id' => '190',
6182 'membership_level' => 'Sample Membership',
6183 'membership_initial_payment' => '0.00',
6184 'membership_recurring_payment' => '0.00',
6185 'membership_expiry_date' => 'January 22, 2023',
6186 ];
6187
6188 $customer_id = (int) ( isset( $data['filter']['membership_customer_id']['value'] ) ? $data['filter']['membership_customer_id']['value'] : '-1' );
6189 $membership_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
6190
6191 $args = [];
6192 if ( 'membership_purchased' === $data['search_term'] ) {
6193 $args = [
6194 'status' => 'active',
6195 'number' => 1,
6196 'orderby' => 'id',
6197 ];
6198 } elseif ( 'membership_cancelled' === $data['search_term'] ) {
6199 $args = [
6200 'status' => 'cancelled',
6201 'number' => 1,
6202 'orderby' => 'id',
6203 ];
6204 } elseif ( 'membership_expired' === $data['search_term'] ) {
6205 $args = [
6206 'status' => 'expired',
6207 'number' => 1,
6208 'orderby' => 'id',
6209 ];
6210 }
6211
6212 if ( 'membership_expired' === $data['search_term'] && -1 !== $customer_id ) {
6213 $args['customer_id'] = $customer_id;
6214 }
6215
6216 if ( -1 !== $membership_id ) {
6217 $args['object_id'] = $membership_id;
6218 }
6219
6220 $memberships = rcp_get_memberships( $args );
6221 if ( count( $memberships ) > 0 ) {
6222 $membership_data = RestrictContent::get_rcp_membership_detail_context( $memberships[0] );
6223 $user_data = WordPress::get_user_context( $memberships[0]->get_user_id() );
6224
6225 $context['response_type'] = 'live';
6226 }
6227
6228 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
6229
6230 return $context;
6231 }
6232
6233 /**
6234 * Get last data for trigger
6235 *
6236 * @param array $data data.
6237 * @return array
6238 */
6239 public function search_events_calendar_triggers_last_data( $data ) {
6240 $context = [];
6241 $context['response_type'] = 'sample';
6242
6243 $event_data = [
6244 'event_id' => 1,
6245 'event' => [
6246 'ID' => 58,
6247 'post_author' => 1,
6248 'post_date' => '2023-01-19 09:27:58',
6249 'post_date_gmt' => '2023-01-19 09:27:58',
6250 'post_content' => '',
6251 'post_title' => 'New event',
6252 'post_excerpt' => '',
6253 'post_status' => 'publish',
6254 'comment_status' => 'open',
6255 'ping_status' => 'closed',
6256 'post_password' => '',
6257 'post_name' => 'new-event',
6258 'to_ping' => '',
6259 'pinged' => '',
6260 'post_modified' => '2023-01-19 09:44:25',
6261 'post_modified_gmt' => '2023-01-19 09:44:25',
6262 'post_content_filtered' => '',
6263 'post_parent' => 0,
6264 'guid' => 'http://connector.com/?post_type=tribe_events&#038;p=58',
6265 'menu_order' => -1,
6266 'post_type' => 'tribe_events',
6267 'post_mime_type' => '',
6268 'comment_count' => 0,
6269 'filter' => 'raw',
6270 ],
6271 'attendies' => [
6272 'order_id' => 68,
6273 'purchaser_name' => 'John Doe',
6274 'purchaser_email' => 'john@test.com',
6275 'provider' => 'Tribe__Tickets__RSVP',
6276 'provider_slug' => 'rsvp',
6277 'purchase_time' => '2023-01-19 09:48:43',
6278 'optout' => 1,
6279 'ticket' => 'Prime',
6280 'attendee_id' => 68,
6281 'security' => '2cefc3b53e',
6282 'product_id' => 65,
6283 'check_in' => '',
6284 'order_status' => 'yes',
6285 'order_status_label' => 'Going',
6286 'user_id' => 1,
6287 'ticket_sent' => 1,
6288 'event_id' => 58,
6289 'ticket_name' => 'Prime',
6290 'holder_name' => 'John Doe',
6291 'holder_email' => 'john@test.com',
6292 'ticket_id' => 68,
6293 'qr_ticket_id' => 68,
6294 'security_code' => '2cefc3b53e',
6295 'attendee_meta' => '',
6296 'is_subscribed' => '',
6297 'is_purchaser' => 1,
6298 'ticket_exists' => 1,
6299 ],
6300 ];
6301
6302 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
6303 $term = $data['search_term'];
6304
6305 if ( 'event_register' === $term || 'attendee_registered_event' === $term ) {
6306 if ( -1 === $event_id ) {
6307 $args = [
6308 'numberposts' => 1,
6309 'orderby' => 'rand',
6310 'post_type' => 'tribe_events',
6311 ];
6312 $posts = get_posts( $args );
6313 $event_id = $posts[0]->ID;
6314 }
6315 $args = [
6316 'post_type' => 'tribe_rsvp_attendees',
6317 'orderby' => 'ID',
6318 'order' => 'DESC',
6319 'post_status' => 'publish',
6320 'numberposts' => 1,
6321 ];
6322
6323 if ( -1 !== $event_id ) {
6324 $args['meta_query'] = [
6325 [
6326 'key' => '_tribe_rsvp_event',
6327 'value' => $event_id,
6328 ],
6329 ];
6330 }
6331
6332 $attendees = get_posts( $args );
6333
6334 if ( count( $attendees ) > 0 ) {
6335 $attendee = $attendees[0];
6336 $attendee_id = $attendee->ID;
6337
6338 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
6339 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
6340 if ( is_numeric( $product_id ) && is_numeric( $order_id ) ) {
6341 $event_context = TheEventCalendar::get_event_context( (int) $product_id, (int) $order_id );
6342 }
6343
6344 if ( ! empty( $event_context ) ) {
6345 $event_data = $event_context;
6346 $context['response_type'] = 'live';
6347 }
6348 } else {
6349 $args = [
6350 'post_type' => 'tec_tc_attendee',
6351 'orderby' => 'ID',
6352 'order' => 'DESC',
6353 'post_status' => 'publish',
6354 'numberposts' => 1,
6355 ];
6356 if ( -1 !== $event_id ) {
6357 $args['meta_query'] = [
6358 [
6359 'key' => '_tec_tickets_commerce_event',
6360 'value' => $event_id,
6361 ],
6362 ];
6363 }
6364 $attendees = get_posts( $args );
6365 $attendee = $attendees[0];
6366 $attendee_id = $attendee->ID;
6367 $product_id = get_post_meta( $attendee_id, '_tec_tickets_commerce_ticket', true );
6368 $order_id = $attendee_id;
6369 if ( is_numeric( $product_id ) ) {
6370 $event_context = TheEventCalendar::get_event_context( (int) $product_id, $order_id );
6371 }
6372 if ( ! empty( $event_context ) ) {
6373 $event_data = $event_context;
6374 $context['response_type'] = 'live';
6375 }
6376 }
6377 } elseif ( 'event_attends' === $term ) {
6378 if ( -1 == $event_id ) {
6379 $args = [
6380 'numberposts' => 1,
6381 'orderby' => 'rand',
6382 'post_type' => 'tribe_events',
6383 ];
6384 $posts = get_posts( $args );
6385 $event_id = $posts[0]->ID;
6386 }
6387 $args = [
6388 'post_type' => 'tribe_rsvp_attendees',
6389 'orderby' => 'ID',
6390 'order' => 'DESC',
6391 'post_status' => 'publish',
6392 'numberposts' => 1,
6393 'meta_query' => [
6394 'relation' => 'AND',
6395 [
6396 'key' => '_tribe_rsvp_checkedin',
6397 'value' => 1,
6398 'compare' => '=',
6399 ],
6400 [
6401 'key' => '_tribe_rsvp_event',
6402 'value' => $event_id,
6403 'compare' => '=',
6404 ],
6405 ],
6406 ];
6407 $attendees = get_posts( $args );
6408 if ( ! function_exists( 'tribe_tickets_get_attendees' ) ) {
6409 return [];
6410 }
6411 if ( ! empty( $attendees ) ) {
6412 $attendee = $attendees[0];
6413 $attendee_id = $attendee->ID;
6414 /**
6415 *
6416 * Ignore line
6417 *
6418 * @phpstan-ignore-next-line
6419 */
6420 $attendee_details = tribe_tickets_get_attendees( $attendee_id, 'rsvp_order' );
6421 foreach ( $attendee_details as $detail ) {
6422 if ( (int) $detail['attendee_id'] !== (int) $attendee_id ) {
6423 continue;
6424 }
6425 $attendee = $detail;
6426 }
6427 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
6428 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
6429 if ( is_numeric( $product_id ) && is_numeric( $order_id ) ) {
6430 $event_context = TheEventCalendar::get_event_context( (int) $product_id, (int) $order_id );
6431 }
6432 if ( ! empty( $event_context ) ) {
6433 $event_data = array_merge( $attendee, $event_context );
6434 $context['response_type'] = 'live';
6435 }
6436 } else {
6437 $order = [
6438 'order_id' => 7962,
6439 'purchaser_name' => 'bella4 bella4',
6440 'purchaser_email' => 'bella4@yopmail.com',
6441 'provider' => 'Tribe__Tickets__RSVP',
6442 'provider_slug' => 'rsvp',
6443 'purchase_time' => '2024 - 03 - 04 07:26:41',
6444 'optout' => 1,
6445 'ticket' => 'test test',
6446 'attendee_id' => 7962,
6447 'security' => 'eb3a2d7bc4',
6448 'product_id' => 7959,
6449 'check_in' => 1,
6450 'order_status' => 'yes',
6451 'order_status_label' => 'Going',
6452 'user_id' => 35,
6453 'ticket_sent' => 1,
6454 'event_id' => 7956,
6455 'ticket_name' => 'test test',
6456 'holder_name' => 'bella4 bella4',
6457 'holder_email' => 'bella4@yopmail.com',
6458 'ticket_id' => 7962,
6459 'qr_ticket_id' => 7962,
6460 'security_code' => 'eb3a2d7bc4',
6461 'is_purchaser' => 1,
6462 'ticket_exists' => 1,
6463 ];
6464 $event_data = array_merge( $order, $event_data );
6465 }
6466 } elseif ( 'event_attendee_wc_register' === $term ) {
6467 if ( ! class_exists( 'Tribe__Tickets__Tickets' ) ) {
6468 return [];
6469 }
6470 if ( -1 === $event_id ) {
6471 $args = [
6472 'numberposts' => 1,
6473 'orderby' => 'rand',
6474 'post_type' => 'tribe_events',
6475 ];
6476 $posts = get_posts( $args );
6477 $event_id = $posts[0]->ID;
6478 }
6479 $event_ticket_id = (int) ( isset( $data['filter']['event_ticket_id']['value'] ) ? $data['filter']['event_ticket_id']['value'] : '-1' );
6480 if ( -1 === $event_ticket_id ) {
6481 $tickets = \Tribe__Tickets__Tickets::get_all_event_tickets( $event_id );
6482 $random_key = array_rand( $tickets );
6483 $random_value = $tickets[ $random_key ];
6484 $event_ticket_id = $random_value;
6485 }
6486 $args = [
6487 'post_type' => 'tribe_wooticket',
6488 'orderby' => 'ID',
6489 'order' => 'DESC',
6490 'post_status' => 'publish',
6491 'meta_query' => [
6492 'relation' => 'AND',
6493 [
6494 'key' => '_tribe_wooticket_event',
6495 'value' => $event_id,
6496 'compare' => '=',
6497 ],
6498 [
6499 'key' => '_tribe_wooticket_product',
6500 'value' => $event_ticket_id,
6501 'compare' => '=',
6502 ],
6503 ],
6504 ];
6505
6506 $woo_attendees = get_posts( $args );
6507 if ( ! empty( $woo_attendees ) ) {
6508 $attendee = $woo_attendees[0];
6509 $attendee_id = $attendee->ID;
6510 /**
6511 *
6512 * Ignore line
6513 *
6514 * @phpstan-ignore-next-line
6515 */
6516 $attendee_details = tribe_tickets_get_attendees( $attendee_id, 'rsvp_order' );
6517 foreach ( $attendee_details as $detail ) {
6518 if ( (int) $detail['attendee_id'] !== (int) $attendee_id ) {
6519 continue;
6520 }
6521 $attendee = $detail;
6522 }
6523 $order_id = get_post_meta( $attendee_id, '_tribe_wooticket_order', true );
6524 $product_id = get_post_meta( $attendee_id, '_tribe_wooticket_product', true );
6525 if ( is_numeric( $product_id ) && is_numeric( $order_id ) ) {
6526 $event_context = TheEventCalendar::get_event_context( (int) $product_id, (int) $order_id );
6527 }
6528 if ( ! empty( $event_context ) ) {
6529 $event_data = $event_context;
6530 $context['response_type'] = 'live';
6531 }
6532 $context['response_type'] = 'live';
6533 }
6534 }
6535
6536 $context['pluggable_data'] = $event_data;
6537
6538 return $context;
6539 }
6540
6541 /**
6542 * Get last data for trigger
6543 *
6544 * @param array $data data.
6545 * @return array
6546 */
6547 public function search_woo_commerce_triggers_last_data( $data ) {
6548 $context = [];
6549 $context['response_type'] = 'sample';
6550 $context['pluggable_data'] = [];
6551 $user_data = WordPress::get_sample_user_context();
6552
6553 $product_data['product'] = [
6554 'id' => '169',
6555 'name' => 'Sample Product',
6556 'description' => 'This is description of sample product.',
6557 'short_description' => 'This is short description of sample product.',
6558 'image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
6559 'slug' => 'sample-product',
6560 'status' => 'publish',
6561 'type' => 'simple',
6562 'price' => '89',
6563 'featured' => '0',
6564 'sku' => 'hoodie-blue-sm',
6565 'regular_price' => '90',
6566 'sale_price' => '89',
6567 'total_sales' => '21',
6568 'category' => 'Uncategorized',
6569 'tags' => 'sample, new, 2022',
6570 ];
6571
6572 $comment_data = [
6573 'comment_id' => '1',
6574 'comment' => 'This is a sample comment..!',
6575 'comment_author' => 'testsure',
6576 'comment_date' => '2023-06-23 10:10:40',
6577 'comment_author_email' => 'testsure@example.com',
6578 ];
6579
6580 $order_data = [
6581 'order_id' => '500',
6582 'total_order_value' => '45',
6583 'currency' => 'USD',
6584 'shipping_total' => '5',
6585 'order_payment_method' => 'cod',
6586 'billing_firstname' => 'John',
6587 'billing_lastname' => 'Doe',
6588 'billing_company' => 'BSF',
6589 'billing_address_1' => '1004 Beaumont',
6590 'billing_address_2' => '',
6591 'billing_city' => 'Casper',
6592 'billing_state' => 'Wyoming',
6593 'billing_state_name' => 'Wyoming',
6594 'billing_postcode' => '82601',
6595 'billing_country' => 'US',
6596 'billing_country_name' => 'US',
6597 'billing_email' => 'john_doe@gmail.com',
6598 'billing_phone' => '(307) 7626541',
6599 'shipping_firstname' => 'John',
6600 'shipping_lastname' => 'Doe',
6601 'shipping_company' => 'BSF',
6602 'shipping_address_1' => '1004 Beaumont',
6603 'shipping_address_2' => '',
6604 'shipping_city' => 'Casper',
6605 'shipping_state' => 'Wyoming',
6606 'shipping_state_name' => 'Wyoming',
6607 'shipping_postcode' => '82601',
6608 'shipping_country' => 'US',
6609 'shipping_country_name' => 'US',
6610 'coupon_codes' => 'e3mstekq, f24sjakb',
6611 'total_items_in_order' => '1',
6612 'user_id' => '1',
6613 ];
6614
6615 $variation_data = [
6616 'product_variation_id' => '626',
6617 'product_variation' => 'Color: Silver',
6618 ];
6619
6620 $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
6621
6622 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
6623 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
6624
6625 $order_status = ( isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1 );
6626 $from_order_status = ( isset( $data['filter']['from_status']['value'] ) ? $data['filter']['from_status']['value'] : -1 );
6627 $order_note_type = ( isset( $data['filter']['note_type']['value'] ) ? $data['filter']['note_type']['value'] : -1 );
6628
6629 if ( in_array( $term, [ 'product_added_to_cart', 'product_viewed' ], true ) ) {
6630 if ( -1 === $product_id ) {
6631 $args = [
6632 'post_type' => 'product',
6633 'orderby' => 'ID',
6634 'order' => 'DESC',
6635 'post_status' => 'publish',
6636 'numberposts' => 1,
6637 ];
6638 $products = get_posts( $args );
6639
6640 if ( count( $products ) > 0 ) {
6641 $product_id = $products[0]->ID;
6642 }
6643 }
6644
6645 if ( -1 !== $product_id ) {
6646 $post = get_post( $product_id );
6647 if ( $post instanceof \WP_Post ) {
6648 $user_data = WordPress::get_user_context( (int) $post->post_author );
6649 }
6650 $product_data['product_id'] = $product_id;
6651 $product_data['product'] = WooCommerce::get_product_context( $product_id );
6652 $terms = get_the_terms( $product_id, 'product_cat' );
6653 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6654 $cat_name = [];
6655 foreach ( $terms as $cat ) {
6656 $cat_name[] = $cat->name;
6657 }
6658 $product_data['product']['category'] = implode( ', ', $cat_name );
6659 }
6660 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6661 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6662 $tag_name = [];
6663 foreach ( $terms_tags as $tag ) {
6664 $tag_name[] = $tag->name;
6665 }
6666 $product_data['product']['tag'] = implode( ', ', $tag_name );
6667 }
6668 unset( $product_data['product']['id'] ); //phpcs:ignore
6669 $context['response_type'] = 'live';
6670 }
6671
6672 if ( 'product_added_to_cart' === $term ) {
6673 $product_data['product_quantity'] = 1;
6674 }
6675
6676 $context['pluggable_data'] = array_merge( $product_data, $user_data );
6677
6678 } elseif ( 'product_reviewed' === $term ) {
6679 $comment_args = [
6680 'number' => 1,
6681 'type' => 'review',
6682 'orderby' => 'comment_ID',
6683 'post_id' => -1 !== $product_id ? $product_id : 0,
6684 ];
6685
6686 $comments = get_comments( $comment_args );
6687
6688 if ( is_array( $comments ) && count( $comments ) > 0 ) {
6689 $comment = $comments[0];
6690 if ( is_object( $comment ) ) {
6691 $comment_data = [
6692 'comment_id' => $comment->comment_ID,
6693 'comment' => $comment->comment_content,
6694 'comment_author' => $comment->comment_author,
6695 'comment_date' => $comment->comment_date,
6696 'comment_author_email' => $comment->comment_author_email,
6697 ];
6698 $product_data = WooCommerce::get_product_context( (int) $comment->comment_post_ID );
6699 $terms = get_the_terms( (int) $comment->comment_post_ID, 'product_cat' );
6700 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6701 $cat_name = [];
6702 foreach ( $terms as $cat ) {
6703 $cat_name[] = $cat->name;
6704 }
6705 $product_data['product']['category'] = implode( ', ', $cat_name );
6706 }
6707 $terms_tags = get_the_terms( (int) $comment->comment_post_ID, 'product_tag' );
6708 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6709 $tag_name = [];
6710 foreach ( $terms_tags as $tag ) {
6711 $tag_name[] = $tag->name;
6712 }
6713 $product_data['product']['tag'] = implode( ', ', $tag_name );
6714 }
6715 }
6716 if ( $comment instanceof \WP_Comment ) {
6717 $user_data = WordPress::get_user_context( (int) $comment->user_id );
6718 }
6719 $context['response_type'] = 'live';
6720 }
6721
6722 $context['pluggable_data'] = array_merge( $product_data, $user_data, $comment_data );
6723
6724 } elseif ( 'product_purchased' === $term ) {
6725 $order_id = 0;
6726 $product_data['quantity'] = '1';
6727 if ( -1 !== $product_id ) {
6728 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
6729 if ( count( $order_ids ) > 0 ) {
6730 $order_id = $order_ids[0];
6731 }
6732 } else {
6733 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
6734 if ( is_array( $orders ) && count( $orders ) > 0 ) {
6735 $order_id = $orders[0]->get_id();
6736 }
6737 }
6738
6739 if ( 0 !== $order_id ) {
6740 $order = wc_get_order( $order_id );
6741
6742 if ( $order && $order instanceof \WC_Order ) {
6743 $user_id = $order->get_customer_id();
6744 $items = $order->get_items();
6745
6746 $product_ids = [];
6747
6748 $iteration = 0;
6749 foreach ( $items as $item ) {
6750 if ( method_exists( $item, 'get_product_id' ) ) {
6751 $item_id = $item->get_product_id();
6752 if ( -1 === $product_id && 0 === $iteration ) {
6753 $product_ids[] = $item_id;
6754 break;
6755 } elseif ( $item_id === $product_id ) {
6756 $product_ids[] = $item_id;
6757 break;
6758 }
6759 }
6760
6761 $iteration++;
6762 }
6763 $order_data = WooCommerce::get_order_context( $order_id );
6764 $user_data = WordPress::get_user_context( $user_id );
6765 $order_data['total_items_in_order'] = count( $product_ids );
6766 $product_data = [];
6767 foreach ( $product_ids as $key => $product_id ) {
6768 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
6769 $terms = get_the_terms( $product_id, 'product_cat' );
6770 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6771 $cat_name = [];
6772 foreach ( $terms as $cat ) {
6773 $cat_name[] = $cat->name;
6774 }
6775 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
6776 }
6777 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6778 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6779 $tag_name = [];
6780 foreach ( $terms_tags as $tag ) {
6781 $tag_name[] = $tag->name;
6782 }
6783 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
6784 }
6785 $product = wc_get_product( $product_id );
6786 /**
6787 *
6788 * Ignore line
6789 *
6790 * @phpstan-ignore-next-line
6791 */
6792 if ( $product->is_downloadable() ) {
6793 /**
6794 *
6795 * Ignore line
6796 *
6797 * @phpstan-ignore-next-line
6798 */
6799 foreach ( $product->get_downloads() as $key_download_id => $download ) {
6800 $download_name = $download->get_name();
6801 $download_link = $download->get_file();
6802 $download_id = $download->get_id();
6803 $download_type = $download->get_file_type();
6804 $download_ext = $download->get_file_extension();
6805 $product_data[ 'product' . $key ]['download'] = [
6806 'download_name' => $download_name,
6807 'download_link' => $download_link,
6808 'download_id' => $download_id,
6809 'download_type' => $download_type,
6810 'download_ext' => $download_ext,
6811 ];
6812 }
6813 }
6814 }
6815 $context['response_type'] = 'live';
6816 }
6817 }
6818
6819 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
6820
6821 } elseif ( 'variable_product_purchased' === $term ) {
6822 $product_variation_id = (int) ( isset( $data['filter']['product_variation_id']['value'] ) ? $data['filter']['product_variation_id']['value'] : -1 );
6823 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
6824
6825 foreach ( $order_ids as $order_id ) {
6826 $order = wc_get_order( $order_id );
6827
6828 if ( $order && $order instanceof \WC_Order ) {
6829 $user_id = $order->get_customer_id();
6830 $items = $order->get_items();
6831 $product_variations = [];
6832
6833 $iteration = 0;
6834 foreach ( $items as $item ) {
6835 if ( method_exists( $item, 'get_variation_id' ) ) {
6836 $variation_id = $item->get_variation_id();
6837 if ( -1 === $product_variation_id && 0 === $iteration ) {
6838 $product_variations[] = $variation_id;
6839 break;
6840 } elseif ( $variation_id === $product_variation_id ) {
6841 $product_variations[] = $variation_id;
6842 break;
6843 }
6844 }
6845
6846 $iteration++;
6847 }
6848
6849 if ( count( $product_variations ) > 0 ) {
6850 $product_data = WooCommerce::get_product_context( $product_variation_id );
6851 $order_data = WooCommerce::get_order_context( $order_id );
6852 $user_data = WordPress::get_user_context( $user_id );
6853 $variation_data = [
6854 'product_variation_id' => $product_variations[0],
6855 'product_variation' => get_the_excerpt( $product_variations[0] ),
6856 ];
6857
6858 $context['response_type'] = 'live';
6859 break;
6860 }
6861 }
6862 }
6863
6864 $context['pluggable_data'] = array_merge(
6865 isset( $order_data ) ? $order_data : [],
6866 $user_data,
6867 $variation_data
6868 );
6869
6870 } elseif ( 'variable_subscription_purchased' === $term ) {
6871 $product_data['quantity'] = '1';
6872 $product_data['product_name'] = 'Sample Product';
6873 $product_data['billing_period'] = '2021-2022';
6874
6875 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
6876
6877 $subscription_order_id = 0;
6878 $order_ids = [];
6879
6880 if ( -1 !== $product_id ) {
6881 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
6882
6883 } else {
6884 $orders = wc_get_orders( [] );
6885 if ( is_array( $orders ) && count( $orders ) > 0 ) {
6886 $order_ids[] = $orders[0]->get_id();
6887 }
6888 }
6889
6890 foreach ( $order_ids as $order_id ) {
6891 $query_args = [
6892 'post_type' => 'shop_subscription',
6893 'orderby' => 'ID',
6894 'order' => 'DESC',
6895 'post_status' => 'wc-active',
6896 'posts_per_page' => 1,
6897 'post_parent' => $order_id,
6898 ];
6899 $query_result = new WP_Query( $query_args );
6900 $subscription_orders = $query_result->get_posts();
6901
6902 if ( count( $subscription_orders ) > 0 ) {
6903 if ( $subscription_orders[0] instanceof \WP_Post ) {
6904 $subscription_order_id = $subscription_orders[0]->ID;
6905 }
6906 break;
6907 }
6908 }
6909
6910 if ( 0 !== $subscription_order_id ) {
6911 if ( function_exists( 'wcs_get_subscription' ) ) {
6912 $subscription = wcs_get_subscription( $subscription_order_id );
6913 if ( class_exists( 'WC_Subscription' ) && $subscription instanceof WC_Subscription ) {
6914 $last_order_id = $subscription->get_last_order();
6915 if ( ! empty( $last_order_id ) && $last_order_id === $subscription->get_parent_id() ) {
6916 $last_order_data = wc_get_order( $last_order_id );
6917 if ( $last_order_data instanceof \WC_Order ) {
6918 $user_id = $last_order_data->get_customer_id();
6919 }
6920 $items = $subscription->get_items();
6921
6922 foreach ( $items as $item ) {
6923 $product = $item->get_product();
6924 if ( class_exists( '\WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product ) ) {
6925 if ( $product->is_type( [ 'subscription', 'subscription_variation', 'variable-subscription' ] ) ) {
6926
6927 $product_data = WooCommerce::get_variable_subscription_product_context( $item, $last_order_id );
6928 if ( isset( $user_id ) ) {
6929 $user_data = WordPress::get_user_context( $user_id );
6930 }
6931
6932 $context['response_type'] = 'live';
6933 $context['pluggable_data'] = array_merge( $product_data, $user_data );
6934 }
6935 }
6936 }
6937 }
6938 }
6939 }
6940 }
6941 } elseif ( 'order_created' === $term ) {
6942 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
6943 $order_id = '';
6944 if ( is_array( $orders ) && count( $orders ) > 0 ) {
6945 $order_id = $orders[0]->get_id();
6946 $order = wc_get_order( $order_id );
6947 if ( $order instanceof \WC_Order ) {
6948 $user_id = $order->get_customer_id();
6949 $order_context = WooCommerce::get_order_context( $order_id );
6950 $order_sample_data = array_merge(
6951 isset( $order_context ) ? $order_context : [],
6952 WordPress::get_user_context( $user_id )
6953 );
6954 $context['response_type'] = 'live';
6955 }
6956 }
6957
6958 // Add dummy coupon details if no coupons exist in the order.
6959 if ( ! isset( $order_sample_data ) ) {
6960 $order_sample_data = [];
6961 }
6962 if ( ! isset( $order_sample_data['coupons'] ) || empty( $order_sample_data['coupons'] ) ) {
6963 $order_sample_data['coupons'] = [
6964 [
6965 'code_name' => 'SAMPLE10',
6966 'discount_type' => 'percent',
6967 'coupon_amount' => '10',
6968 'meta_data' => [],
6969 ],
6970 [
6971 'code_name' => 'SAMPLE20',
6972 'discount_type' => 'fixed_cart',
6973 'coupon_amount' => '20',
6974 'meta_data' => [],
6975 ],
6976 ];
6977 }
6978
6979 $context['pluggable_data'] = $order_sample_data;
6980
6981 } elseif ( 'order_status_changed' === $term ) {
6982 if ( -1 == $order_status ) {
6983 $args = [
6984 'numberposts' => 1,
6985 'orderby' => 'date',
6986 'order' => 'DESC',
6987 ];
6988 } else {
6989 $args = [
6990 'status' => [ $order_status ],
6991 'numberposts' => 1,
6992 'orderby' => 'date',
6993 'order' => 'DESC',
6994 ];
6995 }
6996 $orders = wc_get_orders( $args );
6997 $order_id = '';
6998 if ( is_array( $orders ) && count( $orders ) > 0 ) {
6999 $order_id = $orders[0]->get_id();
7000 $order = wc_get_order( $order_id );
7001 if ( ! empty( $order ) && $order instanceof \WC_Order ) {
7002 $user_id = $order->get_customer_id();
7003 $items = $order->get_items();
7004 $product_ids = [];
7005 foreach ( $items as $item ) {
7006 $product_ids[] = $item['product_id'];
7007 }
7008 $product_data = [];
7009 foreach ( $product_ids as $key => $product_id ) {
7010 /**
7011 *
7012 * Ignore line
7013 *
7014 * @phpstan-ignore-next-line
7015 */
7016 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
7017 /**
7018 *
7019 * Ignore line
7020 *
7021 * @phpstan-ignore-next-line
7022 */
7023 $terms = get_the_terms( $product_id, 'product_cat' );
7024 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
7025 $cat_name = [];
7026 foreach ( $terms as $cat ) {
7027 $cat_name[] = $cat->name;
7028 }
7029 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
7030 }
7031 /**
7032 *
7033 * Ignore line
7034 *
7035 * @phpstan-ignore-next-line
7036 */
7037 $terms_tags = get_the_terms( $product_id, 'product_tag' );
7038 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
7039 $tag_name = [];
7040 foreach ( $terms_tags as $tag ) {
7041 $tag_name[] = $tag->name;
7042 }
7043 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
7044 }
7045 }
7046 $order_context = WooCommerce::get_order_context( $order_id );
7047 $order_sample_data = array_merge(
7048 isset( $order_context ) ? $order_context : [],
7049 $product_data
7050 );
7051 $order_sample_data['user'] = WordPress::get_user_context( $user_id );
7052 $order_sample_data['to_status'] = $order_status;
7053 $order_sample_data['from_status'] = $from_order_status;
7054 $context['response_type'] = 'live';
7055 }
7056 }
7057
7058 $order_sample_data['to_status'] = $order_status;
7059 $order_sample_data['from_status'] = $from_order_status;
7060 $context['pluggable_data'] = $order_sample_data;
7061 } elseif ( 'order_note_added' === $term ) {
7062 global $wpdb;
7063 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type ='order_note' ORDER BY comment_ID DESC LIMIT 1" );
7064 $order_id = '';
7065 if ( ! empty( $result ) ) {
7066 $order_id = $result[0]->comment_post_ID;
7067 $order = wc_get_order( $order_id );
7068 if ( ! empty( $order ) && $order instanceof \WC_Order ) {
7069 $user_id = $order->get_customer_id();
7070 $order_context = WooCommerce::get_order_context( $order_id );
7071 $order_sample_data = array_merge(
7072 isset( $order_context ) ? $order_context : [],
7073 WordPress::get_user_context( $user_id )
7074 );
7075 if ( -1 == $order_note_type ) {
7076 $args = [
7077 'order_id' => $order_id,
7078 'limit' => 1,
7079 ];
7080 } else {
7081 $args = [
7082 'order_id' => $order_id,
7083 'type' => $order_note_type,
7084 'limit' => 1,
7085 ];
7086 }
7087
7088 $notes = wc_get_order_notes( $args );
7089 if ( ! empty( $notes ) ) {
7090 $order_sample_data['note'] = [
7091 'id' => $notes[0]->id,
7092 'date' => $notes[0]->date_created,
7093 'author' => $notes[0]->added_by,
7094 'content' => $notes[0]->content,
7095 ];
7096 } else {
7097 $order_sample_data['note'] = [
7098 'id' => '1',
7099 'date' => [
7100 'date' => '2023-06-23 10:10:40',
7101 'timezone_type' => 1,
7102 'timezone' => '+00:00',
7103 ],
7104 'author' => 'admin',
7105 'content' => 'new note',
7106 ];
7107 }
7108 $order_sample_data['note_type'] = $order_note_type;
7109 $context['response_type'] = 'live';
7110 }
7111 } else {
7112 $order_sample_data['note'] = [
7113 'id' => '1',
7114 'date' => [
7115 'date' => '2023-06-23 10:10:40',
7116 'timezone_type' => 1,
7117 'timezone' => '+00:00',
7118 ],
7119 'author' => 'admin',
7120 'content' => 'new note',
7121 ];
7122 $order_sample_data['note_type'] = 'customer';
7123 }
7124 $context['pluggable_data'] = $order_sample_data;
7125 } elseif ( 'order_paid' === $term ) {
7126 $args = [
7127 'status' => [ 'completed' ],
7128 'numberposts' => 1,
7129 ];
7130 $orders = wc_get_orders( $args );
7131 $order_id = '';
7132 if ( is_array( $orders ) && count( $orders ) > 0 ) {
7133 $order_id = $orders[0]->get_id();
7134 $order = wc_get_order( $order_id );
7135 if ( $order instanceof \WC_Order ) {
7136 $user_id = $order->get_customer_id();
7137 $order_context = WooCommerce::get_order_context( $order_id );
7138 $order_sample_data = array_merge(
7139 isset( $order_context ) ? $order_context : [],
7140 WordPress::get_user_context( $user_id )
7141 );
7142 $context['response_type'] = 'live';
7143 }
7144 }
7145 $context['pluggable_data'] = $order_sample_data;
7146 } elseif ( 'product_category_purchased' === $term ) {
7147 $product_category_id = (int) ( isset( $data['filter']['product_category_id']['value'] ) ? $data['filter']['product_category_id']['value'] : -1 );
7148 $args = [
7149 'post_status' => 'publish',
7150 'product_category_id' => [ $product_category_id ],
7151 'return' => 'ids',
7152 ];
7153 $products = wc_get_products( $args );
7154 if ( ! empty( $products ) ) {
7155 $order_id = 0;
7156 $product_data['quantity'] = '1';
7157 $orders = wc_get_orders(
7158 [
7159 'status' => 'any',
7160 ]
7161 );
7162 $filtered_orders = [];
7163 if ( ! empty( $orders ) && is_array( $orders ) ) {
7164 foreach ( $orders as $order ) {
7165 $order_items = $order->get_items();
7166 foreach ( $order_items as $item ) {
7167 if ( method_exists( $item, 'get_product_id' ) ) {
7168 $product_id = $item->get_product_id();
7169 }
7170 if ( is_array( $products ) && in_array( $product_id, $products ) ) {
7171 $filtered_orders[] = $order;
7172 break;
7173 }
7174 }
7175 }
7176 }
7177 if ( ! empty( $filtered_orders ) ) {
7178 if ( count( $filtered_orders ) > 0 ) {
7179 $order_id = $filtered_orders[0]->get_id();
7180 }
7181 if ( 0 !== $order_id ) {
7182 $order = wc_get_order( $order_id );
7183 if ( $order instanceof WC_Order ) {
7184 $user_id = $order->get_customer_id();
7185 $items = $order->get_items();
7186 $product_ids = [];
7187 $iteration = 0;
7188 foreach ( $items as $item ) {
7189 if ( method_exists( $item, 'get_product_id' ) ) {
7190 $item_id = $item->get_product_id();
7191 if ( -1 === $product_id && 0 === $iteration ) {
7192 $product_ids[] = $item_id;
7193 break;
7194 } elseif ( $item_id === $product_id ) {
7195 $product_ids[] = $item_id;
7196 break;
7197 }
7198 }
7199 $iteration++;
7200 }
7201 $order_data = WooCommerce::get_order_context( $order_id );
7202 $user_data = WordPress::get_user_context( $user_id );
7203 $order_data['total_items_in_order'] = count( $product_ids );
7204 $product_data = [];
7205 $category_ids = [];
7206 foreach ( $product_ids as $key => $product_id ) {
7207 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
7208 $terms = get_the_terms( $product_id, 'product_cat' );
7209 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
7210 $cat_name = [];
7211 foreach ( $terms as $cat ) {
7212 $cat_name[] = $cat->name;
7213 $category_ids[] = $cat->term_id;
7214 }
7215 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
7216 }
7217 $terms_tags = get_the_terms( $product_id, 'product_tag' );
7218 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
7219 $tag_name = [];
7220 foreach ( $terms_tags as $tag ) {
7221 $tag_name[] = $tag->name;
7222 }
7223 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
7224 }
7225 $product = wc_get_product( $product_id );
7226 /**
7227 *
7228 * Ignore line
7229 *
7230 * @phpstan-ignore-next-line
7231 */
7232 if ( $product->is_downloadable() ) {
7233 /**
7234 *
7235 * Ignore line
7236 *
7237 * @phpstan-ignore-next-line
7238 */
7239 foreach ( $product->get_downloads() as $key_download_id => $download ) {
7240 $download_name = $download->get_name();
7241 $download_link = $download->get_file();
7242 $download_id = $download->get_id();
7243 $download_type = $download->get_file_type();
7244 $download_ext = $download->get_file_extension();
7245 $product_data[ 'product' . $key ]['download'] = [
7246 'download_name' => $download_name,
7247 'download_link' => $download_link,
7248 'download_id' => $download_id,
7249 'download_type' => $download_type,
7250 'download_ext' => $download_ext,
7251 ];
7252 }
7253 }
7254 }
7255 $context['response_type'] = 'live';
7256 }
7257 }
7258 }
7259 }
7260 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
7261 if ( ! empty( $category_ids ) ) {
7262 foreach ( $category_ids as $category_id ) {
7263 $context['pluggable_data']['product_category_id'] = $category_id;
7264 }
7265 } else {
7266 $context['pluggable_data']['product_category_id'] = 1;
7267 }
7268 }
7269
7270 return $context;
7271 }
7272
7273 /**
7274 * Get last data for Jet Appointments Booking triggers.
7275 *
7276 * @param array $data data.
7277 * @return array
7278 */
7279 public function search_jet_appointment_triggers_last_data( $data ) {
7280 if ( ! class_exists( '\JET_APB\Plugin' ) ) {
7281 return [];
7282 }
7283
7284 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
7285
7286 // Sample appointment data.
7287 $appointment_data = [
7288 'ID' => 1,
7289 'group_ID' => '',
7290 'status' => 'pending',
7291 'service' => 1,
7292 'provider' => 1,
7293 'order_id' => '',
7294 'user_id' => 1,
7295 'user_name' => 'john',
7296 'user_email' => 'john@example.com',
7297 'date' => time(),
7298 'date_end' => 0,
7299 'slot' => time() - 3600,
7300 'slot_end' => time(),
7301 'type' => 'single',
7302 'appointment_date' => gmdate( 'Y-m-d H:i:s' ),
7303 'service_title' => 'Sample Service',
7304 'provider_title' => 'Sample Provider',
7305 'user_login' => 'john',
7306 'user_display_name' => 'John Doe',
7307 'date_formatted' => date_i18n( is_string( get_option( 'date_format' ) ) ? get_option( 'date_format' ) : 'Y-m-d', time() ),
7308 'slot_formatted' => date_i18n( is_string( get_option( 'time_format' ) ) ? get_option( 'time_format' ) : 'H:i', time() - 3600 ),
7309 'slot_end_formatted' => date_i18n( is_string( get_option( 'time_format' ) ) ? get_option( 'time_format' ) : 'H:i', time() ),
7310 ];
7311
7312 // Add term-specific fields based on search term.
7313 if ( 'appointment_cancelled' === $term || 'appointment_status_changed' === $term ) {
7314 $appointment_data['old_status'] = 'confirmed';
7315 $appointment_data['new_status'] = 'appointment_cancelled' === $term ? 'cancelled' : 'completed';
7316 }
7317
7318 $context = [
7319 'response_type' => 'sample',
7320 ];
7321
7322 // Try to get live data using the same method as triggers.
7323 if ( class_exists( '\JET_APB\Plugin' ) ) {
7324 $live_appointment_data = \JET_APB\Plugin::instance()->db->appointments->query( [], 1 );
7325 if ( ! empty( $live_appointment_data ) ) {
7326 $live_data = $live_appointment_data[0];
7327 $appointment_id = $live_data['ID'];
7328
7329 // Get appointment meta using the same method as triggers.
7330 $appointment_meta = $this->get_appointment_meta( $appointment_id );
7331
7332 // Merge appointment data with meta data.
7333 $appointment_data = array_merge( $live_data, $appointment_meta );
7334
7335 // Add service title.
7336 if ( ! empty( $appointment_data['service'] ) ) {
7337 $service_post = get_post( $appointment_data['service'] );
7338 if ( $service_post ) {
7339 $appointment_data['service_title'] = $service_post->post_title;
7340 }
7341 }
7342
7343 // Add provider title.
7344 if ( ! empty( $appointment_data['provider'] ) && $appointment_data['provider'] > 0 ) {
7345 $provider_post = get_post( $appointment_data['provider'] );
7346 if ( $provider_post ) {
7347 $appointment_data['provider_title'] = $provider_post->post_title;
7348 }
7349 }
7350
7351 // Add user data.
7352 if ( ! empty( $appointment_data['user_id'] ) ) {
7353 $user = get_user_by( 'ID', $appointment_data['user_id'] );
7354 if ( $user ) {
7355 $appointment_data['user_login'] = $user->user_login;
7356 $appointment_data['user_email'] = $user->user_email;
7357 $appointment_data['user_display_name'] = $user->display_name;
7358 }
7359 }
7360
7361 // Add formatted dates.
7362 $date_format = get_option( 'date_format' );
7363 $time_format = get_option( 'time_format' );
7364 $appointment_data['date_formatted'] = is_string( $date_format ) ? date_i18n( $date_format, $appointment_data['date'] ) : '';
7365 $appointment_data['slot_formatted'] = is_string( $time_format ) ? date_i18n( $time_format, $appointment_data['slot'] ) : '';
7366 $appointment_data['slot_end_formatted'] = is_string( $time_format ) ? date_i18n( $time_format, $appointment_data['slot_end'] ) : '';
7367
7368 // Add term-specific fields for live data.
7369 if ( 'appointment_cancelled' === $term || 'appointment_status_changed' === $term ) {
7370 $appointment_data['old_status'] = 'confirmed';
7371 $appointment_data['new_status'] = 'appointment_cancelled' === $term ? 'cancelled' : 'completed';
7372 }
7373
7374 $context['response_type'] = 'live';
7375 }
7376 }
7377
7378 $context['pluggable_data'] = $appointment_data;
7379 return $context;
7380 }
7381
7382 /**
7383 * Get appointment meta data.
7384 *
7385 * @param int $appointment_id Appointment ID.
7386 * @return array
7387 */
7388 private function get_appointment_meta( $appointment_id ) {
7389 if ( ! class_exists( '\JET_APB\Plugin' ) ) {
7390 return [];
7391 }
7392
7393 global $wpdb;
7394 $meta_table = \JET_APB\Plugin::instance()->db->appointments_meta->table();
7395
7396 if ( empty( $meta_table ) || is_array( $meta_table ) || ! is_string( $meta_table ) ) {
7397 return [];
7398 }
7399
7400 $results = $wpdb->get_results(
7401 $wpdb->prepare(
7402 'SELECT meta_key, meta_value FROM ' . esc_sql( $meta_table ) . ' WHERE appointment_id = %d',
7403 $appointment_id
7404 ),
7405 ARRAY_A
7406 );
7407
7408 $meta = [];
7409 if ( ! empty( $results ) ) {
7410 foreach ( $results as $row ) {
7411 $meta[ $row['meta_key'] ] = $row['meta_value'];
7412 }
7413 }
7414
7415 return $meta;
7416 }
7417
7418 /**
7419 * Search LMS data.
7420 *
7421 * @param array $data data.
7422 * @return array
7423 */
7424 public function search_lifter_lms_last_data( $data ) {
7425 global $wpdb;
7426 $post_type = $data['post_type'];
7427 $meta_key = '_is_complete';
7428 $trigger = $data['search_term'];
7429 $context = [];
7430 $post_id = -1;
7431
7432 if ( ! class_exists( 'LLMS_Section' ) ) {
7433 return [];
7434 }
7435 $product_type = '';
7436 if ( 'lifterlms_purchase_course' === $trigger ) {
7437 $product_type = 'course';
7438 $post_id = $data['filter']['course_id']['value'];
7439 } elseif ( 'lifterlms_purchase_membership' === $trigger ) {
7440 $product_type = 'membership';
7441 $post_id = $data['filter']['membership_id']['value'];
7442 } elseif ( 'lifterlms_cancel_membership' === $trigger ) {
7443 $product_type = 'membership';
7444 $post_id = $data['filter']['membership_id']['value'];
7445 } elseif ( 'lifterlms_lesson_completed' === $trigger ) {
7446 $post_id = $data['filter']['lesson']['value'];
7447 } elseif ( 'lifterlms_course_completed' === $trigger || 'lifterlms_course_enrolled' === $trigger || 'lifterlms_course_user_removed' === $trigger ) {
7448 $post_id = $data['filter']['course']['value'];
7449 } elseif ( 'lifterlms_section_completed' === $trigger ) {
7450 $post_id = $data['filter']['section']['value'];
7451 }
7452
7453 $where = 'postmeta.post_id = "' . $post_id . '" AND';
7454
7455 if ( 'llms_order' === $post_type ) {
7456 if ( -1 === $post_id ) {
7457 $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 ) );
7458 } else {
7459 $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 ) );
7460 }
7461 } elseif ( 'lifterlms_course_enrolled' === $trigger ) {
7462 if ( -1 === $post_id ) {
7463 $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 ) );
7464 } else {
7465 $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 ) );
7466 }
7467 } elseif ( 'lifterlms_course_user_removed' === $trigger ) {
7468 if ( -1 === $post_id ) {
7469 $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 ) );
7470 } else {
7471 $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 ) );
7472 }
7473 } elseif ( 'lifterlms_cancel_membership' === $trigger ) {
7474 if ( -1 === $post_id ) {
7475 $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 ) );
7476 } else {
7477 $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 ) );
7478 }
7479 } else {
7480 if ( -1 === $post_id ) {
7481 $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 ) );
7482 } else {
7483 $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 ) );
7484 }
7485 }
7486
7487 $response = [];
7488 if ( ! empty( $result ) ) {
7489 $result_post_id = $result[0]->post_id;
7490 $result_user_id = $result[0]->user_id;
7491
7492 switch ( $trigger ) {
7493 case 'lifterlms_lesson_completed':
7494 $context = array_merge(
7495 WordPress::get_user_context( $result_user_id ),
7496 LifterLMS::get_lms_lesson_context( $result_post_id )
7497 );
7498 $parent_course = get_post_meta( $result_post_id, '_llms_parent_course', true );
7499 if ( is_numeric( $parent_course ) ) {
7500 $context['course'] = get_the_title( (int) $parent_course );
7501 }
7502 $parent_section = get_post_meta( $result_post_id, '_llms_parent_section', true );
7503 if ( '' !== $parent_section && is_numeric( $parent_section ) ) {
7504 $context['parent_section'] = get_the_title( (int) $parent_section );
7505 }
7506 break;
7507 case 'lifterlms_course_enrolled':
7508 case 'lifterlms_course_user_removed':
7509 case 'lifterlms_course_completed':
7510 $context = array_merge(
7511 WordPress::get_user_context( $result_user_id ),
7512 LifterLMS::get_lms_course_context( $result_post_id )
7513 );
7514 break;
7515 case 'lifterlms_section_completed':
7516 $data = new \LLMS_Section( $result_post_id );
7517 $lessons = $data->get_lessons();
7518 $context = array_merge(
7519 WordPress::get_user_context( $result_user_id ),
7520 WordPress::get_post_context( $result_post_id )
7521 );
7522 $context['parent_course'] = $data->get( 'parent_course' );
7523 $context['parent_course_title'] = get_the_title( $data->get( 'parent_course' ) );
7524 if ( ! empty( $lessons ) ) {
7525 foreach ( $lessons as $key => $lesson ) {
7526 $context['section_lesson'][ $key ] = $lesson->id;
7527 $context['section_lesson_title'][ $key ] = get_the_title( $lesson->id );
7528 }
7529 }
7530 $context['section_course'] = $data->get( 'parent_course' );
7531 $context['parent_course_title'] = get_the_title( $data->get( 'parent_course' ) );
7532 break;
7533 case 'lifterlms_purchase_course':
7534 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
7535 $context['course_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
7536 $context['course_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
7537 $context['course_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
7538 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
7539 $context ['order'] = WordPress::get_post_context( $result_post_id );
7540 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
7541 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
7542 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
7543 $context = array_merge( $context, is_numeric( $user_id ) ? WordPress::get_user_context( (int) $user_id ) : [] );
7544 break;
7545 case 'lifterlms_purchase_membership':
7546 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
7547 $context['membership_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
7548 $context['membership_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
7549 $context['membership_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
7550 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
7551 $context ['order'] = WordPress::get_post_context( $result_post_id );
7552 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
7553 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
7554 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
7555 $context = array_merge( $context, is_numeric( $user_id ) ? WordPress::get_user_context( (int) $user_id ) : [] );
7556 break;
7557 case 'lifterlms_cancel_membership':
7558 $context = array_merge( WordPress::get_post_context( $result_post_id ), WordPress::get_user_context( $result[0]->user_id ) );
7559 $context['membership_id'] = $result_post_id;
7560 $context['membership_name'] = get_the_title( $result_post_id );
7561 break;
7562 default:
7563 return [];
7564
7565 }
7566 $response['pluggable_data'] = $context;
7567 $response['response_type'] = 'live';
7568
7569 }
7570
7571 return $response;
7572
7573 }
7574
7575 /**
7576 * Search SM data.
7577 *
7578 * @param array $data data.
7579 * @return array
7580 */
7581 public function search_suremember_last_data( $data ) {
7582 global $wpdb;
7583 $post_type = $data['post_type'];
7584 $meta_key = '_is_complete';
7585 $trigger = $data['search_term'];
7586 $post_id = $data['filter']['group_id']['value'];
7587
7588 if ( 'suremember_updated_group' === $trigger ) {
7589 if ( -1 === $post_id ) {
7590 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts as posts WHERE posts.post_type=%s", $post_type ) );
7591 } else {
7592 $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 ) );
7593 }
7594 } else {
7595 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}usermeta as usermeta WHERE usermeta.meta_key = %s", 'suremembers_user_access_group_' . $post_id ) );
7596 }
7597
7598 $response = [];
7599
7600 if ( ! empty( $result ) ) {
7601 $context = [];
7602 switch ( $trigger ) {
7603 case 'suremember_updated_group':
7604 $group_id = $result[0]->ID;
7605 $suremembers_post['rules'] = get_post_meta( $group_id, 'suremembers_plan_include', true );
7606 $suremembers_post['exclude'] = get_post_meta( $group_id, 'suremembers_plan_exclude', true ); //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
7607 $suremembers_post['suremembers_user_roles'] = get_post_meta( $group_id, 'suremembers_user_roles', true );
7608 $suremembers_post['title'] = get_the_title( $group_id );
7609 $plan_rules = get_post_meta( $group_id, 'suremembers_plan_rules', true );
7610 $suremembers_post['restrict'] = is_array( $plan_rules ) ? $plan_rules['restrict'] : '';
7611 $context['group'] = array_merge( WordPress::get_post_context( $group_id ), $suremembers_post );
7612 $context['group_id'] = $group_id;
7613 unset( $context['group']['ID'] );
7614 $response['pluggable_data'] = $context;
7615 $response['response_type'] = 'live';
7616 break;
7617 case 'suremember_user_added_in_group':
7618 foreach ( $result as $res ) {
7619 $meta_value = unserialize( $res->meta_value );
7620 if ( is_array( $meta_value ) && 'active' === $meta_value['status'] ) {
7621 $context = WordPress::get_user_context( $res->user_id );
7622 $context['group'] = WordPress::get_post_context( $post_id );
7623 $context['group_id'] = $post_id;
7624 unset( $context['group']['ID'] );
7625 $response['pluggable_data'] = $context;
7626 $response['response_type'] = 'live';
7627 }
7628 }
7629 break;
7630 case 'suremember_user_removed_from_group':
7631 foreach ( $result as $res ) {
7632 $meta_value = unserialize( $res->meta_value );
7633 if ( is_array( $meta_value ) && 'revoked' === $meta_value['status'] ) {
7634 $context = WordPress::get_user_context( $res->user_id );
7635 $context['group'] = WordPress::get_post_context( $post_id );
7636 $context['group_id'] = $post_id;
7637 unset( $context['group']['ID'] );
7638 $response['pluggable_data'] = $context;
7639 $response['response_type'] = 'live';
7640 }
7641 }
7642 break;
7643 default:
7644 return [];
7645
7646 }
7647 }
7648
7649 return $response;
7650
7651 }
7652
7653 /**
7654 * Search CartFlows data.
7655 *
7656 * @param array $data data.
7657 * @return array
7658 */
7659 public function search_cartflows_last_data( $data ) {
7660 global $wpdb;
7661 $trigger = $data['search_term'];
7662
7663 if ( ! function_exists( 'wcf_pro' ) ) {
7664 return [];
7665 }
7666
7667 $context = [];
7668 $response = [];
7669 if ( 'cartflows_upsell_offer_accepted' === $trigger || 'cartflows_upsell_offer_rejected' === $trigger ) {
7670 $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'" );
7671 } elseif ( 'cartflows_downsell_offer_accepted' === $trigger || 'cartflows_downsell_offer_rejected' === $trigger ) {
7672 $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'" );
7673 } elseif ( 'wcf_order_bump_item_added' === $trigger || 'wcf_order_bump_item_removed' === $trigger || 'wcf_order_bump_item_replaced' === $trigger ) {
7674 $args = [
7675 'post_type' => 'product',
7676 'orderby' => 'ID',
7677 'order' => 'DESC',
7678 'post_status' => 'publish',
7679 'numberposts' => 1,
7680 ];
7681 $products = get_posts( $args );
7682
7683 if ( count( $products ) > 0 ) {
7684 $product_id = $products[0]->ID;
7685 $product_data['product_id'] = $product_id;
7686 $product_data['product'] = WooCommerce::get_product_context( $product_id );
7687 $terms = get_the_terms( $product_id, 'product_cat' );
7688 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
7689 $cat_name = [];
7690 foreach ( $terms as $cat ) {
7691 $cat_name[] = $cat->name;
7692 }
7693 $product_data['product']['category'] = implode( ', ', $cat_name );
7694 }
7695 $terms_tags = get_the_terms( $product_id, 'product_tag' );
7696 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
7697 $tag_name = [];
7698 foreach ( $terms_tags as $tag ) {
7699 $tag_name[] = $tag->name;
7700 }
7701 $product_data['product']['tag'] = implode( ', ', $tag_name );
7702 }
7703 unset( $product_data['product']['id'] ); //phpcs:ignore
7704 $response['response_type'] = 'live';
7705 } else {
7706 $product_data['product'] = [
7707 'name' => 'Sample Product',
7708 'description' => 'This is description of sample product.',
7709 'short_description' => 'This is short description of sample product.',
7710 'image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
7711 'slug' => 'sample-product',
7712 'status' => 'publish',
7713 'type' => 'simple',
7714 'price' => '89',
7715 'featured' => '0',
7716 'sku' => 'hoodie-blue-sm',
7717 'regular_price' => '90',
7718 'sale_price' => '89',
7719 'total_sales' => '21',
7720 'category' => 'Uncategorized',
7721 'tags' => 'sample, new, 2022',
7722 'category_ids' => [ 40 ],
7723 'date_created' => [
7724 'date' => '2024-09-19 09:28:57.000000',
7725 'timezone_type' => '1',
7726 'timezone' => '+00:00',
7727 ],
7728 'date_modified' => [
7729 'date' => '2024-09-19 09:28:57.000000',
7730 'timezone_type' => '1',
7731 'timezone' => '+00:00',
7732 ],
7733 'product_id' => 9935,
7734 ];
7735 $response['response_type'] = 'sample';
7736 }
7737 $response['pluggable_data'] = $product_data;
7738 }
7739 if ( ! empty( $result ) && ( 'cartflows_upsell_offer_accepted' === $trigger || 'cartflows_upsell_offer_rejected' === $trigger ) ) {
7740 $context = [];
7741 $order_upsell_id = $result[0]->post_id;
7742 $step_id = get_post_meta( $order_upsell_id, '_cartflows_offer_step_id', true );
7743 $order_id = get_post_meta( $order_upsell_id, '_cartflows_offer_parent_id', true );
7744 $order = wc_get_order( $order_id );
7745 $upsell_order = wc_get_order( $order_upsell_id );
7746 if ( $upsell_order instanceof \WC_Order ) {
7747 $items = $upsell_order->get_items();
7748 if ( ! empty( $items ) && isset( $items[0] ) ) {
7749 $variation_id = $items[0]['product_id'];
7750 $input_qty = $items[0]['quantity'];
7751 } else {
7752 $variation_id = null;
7753 $input_qty = null;
7754 }
7755 } else {
7756 $variation_id = null;
7757 $input_qty = null;
7758 }
7759 $offer_product = wcf_pro()->utils->get_offer_data( $step_id, $variation_id, $input_qty, $order_id );
7760 $user_id = get_post_meta( $order_upsell_id, '_customer_user', true );
7761 $context = is_numeric( $user_id ) ? WordPress::get_user_context( (int) $user_id ) : [];
7762 if ( $order instanceof \WC_Order ) {
7763 $context['order'] = $order->get_data();
7764 }
7765 $context['upsell'] = $offer_product;
7766 $context['funnel_step_id'] = $step_id;
7767 if ( is_scalar( $step_id ) ) {
7768 $context['funnel_id'] = get_post_meta( intval( $step_id ), 'wcf-flow-id', true );
7769 } else {
7770 $context['funnel_id'] = null;
7771 }
7772 $response['pluggable_data'] = $context;
7773 $response['response_type'] = 'live';
7774 } elseif ( ! empty( $result ) && ( 'cartflows_downsell_offer_accepted' === $trigger || 'cartflows_downsell_offer_rejected' === $trigger ) ) {
7775 $context = [];
7776 $order_downsell_id = $result[0]->post_id;
7777 $step_id = get_post_meta( $order_downsell_id, '_cartflows_offer_step_id', true );
7778 $order_id = get_post_meta( $order_downsell_id, '_cartflows_offer_parent_id', true );
7779 $order = wc_get_order( $order_id );
7780 $downsell_order = wc_get_order( $order_downsell_id );
7781 if ( $downsell_order instanceof \WC_Order ) {
7782 $items = $downsell_order->get_items();
7783 if ( ! empty( $items ) && isset( $items[0] ) ) {
7784 $variation_id = $items[0]['product_id'];
7785 $input_qty = $items[0]['quantity'];
7786 } else {
7787 $variation_id = null;
7788 $input_qty = null;
7789 }
7790 } else {
7791 $variation_id = null;
7792 $input_qty = null;
7793 }
7794 $offer_product = wcf_pro()->utils->get_offer_data( $step_id, $variation_id, $input_qty, $order_id );
7795 $user_id = get_post_meta( $order_downsell_id, '_customer_user', true );
7796 $context = is_numeric( $user_id ) ? WordPress::get_user_context( (int) $user_id ) : [];
7797 if ( $order instanceof \WC_Order ) {
7798 $context['order'] = $order->get_data();
7799 }
7800 $context['downsell'] = $offer_product;
7801 $context['funnel_step_id'] = $step_id;
7802 if ( is_scalar( $step_id ) ) {
7803 $context['funnel_id'] = get_post_meta( intval( $step_id ), 'wcf-flow-id', true );
7804 } else {
7805 $context['funnel_id'] = null;
7806 }
7807 $response['pluggable_data'] = $context;
7808 $response['response_type'] = 'live';
7809 }
7810 return $response;
7811 }
7812
7813 /**
7814 * Prepare CartFlows Steps list.
7815 *
7816 * @param array $data data.
7817 *
7818 * @return array
7819 */
7820 public function search_cartflows_funnel_step_list( $data ) {
7821 $page = $data['page'];
7822 $limit = Utilities::get_search_page_limit();
7823 $offset = $limit * ( $page - 1 );
7824
7825 $args = [
7826 'posts_per_page' => $limit,
7827 'offset' => $offset,
7828 'orderby' => 'title',
7829 'order' => 'ASC',
7830 'post_type' => 'cartflows_step',
7831 'post_status' => 'publish',
7832 'fields' => 'ids',
7833 ];
7834 $args['meta_query'] = [
7835 [
7836 'key' => 'wcf-flow-id',
7837 'value' => $data['dynamic'],
7838 'compare' => '=',
7839 ],
7840 ];
7841
7842 $flow_step_list = get_posts( $args );
7843 $flow_step_list_count = count(
7844 get_posts(
7845 [
7846 'posts_per_page' => -1,
7847 'orderby' => 'title',
7848 'order' => 'ASC',
7849 'post_type' => 'cartflows_step',
7850 'post_status' => 'publish',
7851 'fields' => 'ids',
7852 'meta_query' => [
7853 [
7854 'key' => 'wcf-flow-id',
7855 'value' => $data['dynamic'],
7856 'compare' => '=',
7857 ],
7858 ],
7859 ]
7860 )
7861 );
7862
7863 $options = [];
7864 if ( ! empty( $flow_step_list ) ) {
7865 foreach ( $flow_step_list as $step ) {
7866 $options[] = [
7867 'label' => html_entity_decode( get_the_title( $step ), ENT_QUOTES, 'UTF-8' ),
7868 'value' => $step,
7869 ];
7870 }
7871 }
7872 return [
7873 'options' => $options,
7874 'hasMore' => $flow_step_list_count > $limit && $flow_step_list_count > $offset,
7875 ];
7876 }
7877
7878
7879 /**
7880 * Fetch user context.
7881 *
7882 * @param int $initiator_id initiator id.
7883 * @param int $friend_id friend id.
7884 * @return array
7885 */
7886 public function get_user_context( $initiator_id, $friend_id ) {
7887 $context = WordPress::get_user_context( $initiator_id );
7888
7889 $friend_context = WordPress::get_user_context( $friend_id );
7890
7891 $avatar = get_avatar_url( $initiator_id );
7892
7893 $context['avatar_url'] = ( $avatar ) ? $avatar : '';
7894
7895 $context['friend_id'] = $friend_id;
7896 $context['friend_first_name'] = $friend_context['user_firstname'];
7897 $context['friend_last_name'] = $friend_context['user_lastname'];
7898 $context['friend_email'] = $friend_context['user_email'];
7899
7900 $friend_avatar = get_avatar_url( $friend_id );
7901 $context['friend_avatar_url'] = $friend_avatar;
7902 return $context;
7903 }
7904
7905 /**
7906 * Search BP data.
7907 *
7908 * @param array $data data.
7909 * @return array
7910 */
7911 public function search_pluggables_bp_friendships( $data ) {
7912 global $wpdb, $bp;
7913 $context = [];
7914 $sample['pluggable_data'] = [
7915 'wp_user_id' => 4,
7916 'user_login' => 'katy1ßßßß',
7917 'display_name' => 'Katy Smith',
7918 'user_firstname' => 'Katy',
7919 'user_lastname' => 'Smith',
7920 'user_email' => 'katy1@gmail.com',
7921 'user_role' => [ 'subscriber' ],
7922 'avatar_url' => 'http://pqr.com/avatar',
7923 'friend_id' => 1,
7924 'friend_first_name' => 'John',
7925 'friend_last_name' => 'Wick',
7926 'friend_email' => 'john@gmail.com',
7927 'friend_avatar_url' => 'http://abc.com/avatar',
7928 ];
7929 $sample['response_type'] = 'sample';
7930
7931 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_friends'" );
7932 if ( $table_exists ) {
7933 $friendships = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_friends LIMIT 1" );
7934 if ( ! empty( $friendships ) ) {
7935 $friendship = $friendships[0];
7936 $initiator_id = $friendship->initiator_user_id;
7937 $friend_user_id = $friendship->friend_user_id;
7938 $context['pluggable_data'] = $this->get_user_context( $initiator_id, $friend_user_id );
7939 $context['response_type'] = 'live';
7940 } else {
7941 $context = $sample;
7942 }
7943 } else {
7944 $context = $sample;
7945 }
7946
7947
7948 return $context;
7949 }
7950
7951 /**
7952 * Search BuddyBoss private messages data.
7953 *
7954 * @param array $data data.
7955 * @return array
7956 */
7957 public function search_pluggables_bb_private_messages( $data ) {
7958 global $wpdb;
7959 $context = [];
7960 $sample['pluggable_data'] = [
7961 'wp_user_id' => 2,
7962 'user_login' => 'johndoe',
7963 'display_name' => 'John Doe',
7964 'user_firstname' => 'John',
7965 'user_lastname' => 'Doe',
7966 'user_email' => 'john@example.com',
7967 'user_role' => [ 'subscriber' ],
7968 'sender_id' => 1,
7969 'sender_first_name' => 'Jane',
7970 'sender_last_name' => 'Smith',
7971 'sender_email' => 'jane@example.com',
7972 'sender_display_name' => 'Jane Smith',
7973 'sender_avatar_url' => 'http://example.com/avatar.jpg',
7974 'message_id' => 1,
7975 'thread_id' => 1,
7976 'message_subject' => 'Hello!',
7977 'message_content' => 'This is a sample private message.',
7978 'message_content_html' => '<p>This is a sample private message.</p>',
7979 'message_date' => '2024-01-01 10:00:00',
7980 ];
7981 $sample['response_type'] = 'sample';
7982
7983 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_messages_messages'" );
7984 if ( $table_exists ) {
7985 $message = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}bp_messages_messages ORDER BY id DESC LIMIT 1" );
7986 if ( ! empty( $message ) ) {
7987 $recipients = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}bp_messages_recipients WHERE thread_id = %d", $message->thread_id ) );
7988 if ( ! empty( $recipients ) ) {
7989 foreach ( $recipients as $recipient ) {
7990 if ( $recipient->user_id != $message->sender_id ) {
7991 $recipient_context = WordPress::get_user_context( $recipient->user_id );
7992 $sender_context = WordPress::get_user_context( $message->sender_id );
7993
7994 $context['pluggable_data'] = array_merge(
7995 $recipient_context,
7996 [
7997 'sender_id' => $message->sender_id,
7998 'sender_first_name' => $sender_context['user_firstname'],
7999 'sender_last_name' => $sender_context['user_lastname'],
8000 'sender_email' => $sender_context['user_email'],
8001 'sender_display_name' => $sender_context['display_name'],
8002 'sender_avatar_url' => get_avatar_url( $message->sender_id ),
8003 'message_id' => $message->id,
8004 'thread_id' => $message->thread_id,
8005 'message_subject' => $message->subject,
8006 'message_content' => wp_strip_all_tags( $message->message ),
8007 'message_content_html' => $message->message,
8008 'message_date' => $message->date_sent,
8009 ]
8010 );
8011 $context['response_type'] = 'live';
8012 break;
8013 }
8014 }
8015 }
8016 }
8017 }
8018
8019 if ( empty( $context ) ) {
8020 $context = $sample;
8021 }
8022
8023 return $context;
8024 }
8025
8026 /**
8027 * Search Buddyboss profile types data.
8028 *
8029 * @param array $data data.
8030 * @return array
8031 */
8032 public function search_pluggables_bp_profile_types( $data ) {
8033 global $wpdb, $bp;
8034 $context = [];
8035 $sample['pluggable_data'] = [
8036 'wp_user_id' => 4,
8037 'user_login' => 'katy1ßßßß',
8038 'display_name' => 'Katy Smith',
8039 'user_firstname' => 'Katy',
8040 'user_lastname' => 'Smith',
8041 'user_email' => 'katy1@gmail.com',
8042 'user_role' => [ 'subscriber' ],
8043 'bb_profile_type' => '10',
8044 'bb_profile_type_name' => 'student',
8045 ];
8046 $sample['response_type'] = 'sample';
8047
8048 $post_id = $data['filter']['bb_profile_type']['value'];
8049 $get_existing = get_post_meta( $post_id, '_bp_member_type_key', true );
8050
8051 $type_term = get_term_by(
8052 'name',
8053 /**
8054 *
8055 * Ignore line
8056 *
8057 * @phpstan-ignore-next-line
8058 */
8059 $get_existing,
8060 'bp_member_type'
8061 );
8062
8063 $results = $wpdb->get_results(
8064 $wpdb->prepare(
8065 "SELECT u.ID FROM {$wpdb->prefix}users u INNER JOIN {$wpdb->prefix}term_relationships r
8066 ON u.ID = r.object_id WHERE u.user_status = 0 AND
8067 r.term_taxonomy_id = %d ORDER BY RAND() LIMIT 1",
8068 /**
8069 *
8070 * Ignore line
8071 *
8072 * @phpstan-ignore-next-line
8073 */
8074 $type_term->term_id
8075 )
8076 );
8077
8078 if ( ! empty( $results ) ) {
8079 $user = $results[0];
8080 $context['pluggable_data'] = WordPress::get_user_context( $user->ID );
8081 $context['pluggable_data']['bb_profile_type'] = $post_id;
8082 $context['pluggable_data']['bb_profile_type_name'] = get_post_meta( $post_id, '_bp_member_type_label_singular_name', true );
8083 $context['response_type'] = 'live';
8084 } else {
8085 $context = $sample;
8086 }
8087
8088 return $context;
8089 }
8090
8091 /**
8092 * Search BP User data.
8093 *
8094 * @param array $data data.
8095 * @return array
8096 */
8097 public function search_pluggables_bb_users( $data ) {
8098 global $wpdb;
8099 $context = [];
8100
8101 if ( ! class_exists( 'BP_Signup' ) ) {
8102 return [];
8103 }
8104
8105 $term = $data['search_term'];
8106
8107 if ( 'account_activated' === $term ) {
8108 $signups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}signups WHERE active = 1 ORDER BY signup_id DESC LIMIT 1" );
8109 } elseif ( 'updates_profile' === $term ) {
8110 $custom_ids = $wpdb->get_var( "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data ORDER BY last_updated DESC LIMIT 1" );
8111 $args = [ 'include' => $custom_ids ];
8112 $users = get_users( $args );
8113 } elseif ( 'gains_follower' === $term ) {
8114 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_follow ORDER BY id DESC LIMIT 1" );
8115 }
8116
8117 if ( ! empty( $signups ) ) {
8118 $pluggable_data = $signups[0];
8119 $pluggable_data = get_object_vars( $pluggable_data );
8120 unset( $pluggable_data['activation_key'] );
8121 if ( is_string( $pluggable_data['meta'] ) ) {
8122 $pluggable_data['meta'] = unserialize( $pluggable_data['meta'] );
8123 }
8124 if ( is_array( $pluggable_data['meta'] ) ) {
8125 unset( $pluggable_data['meta']['password'] );
8126 }
8127 $context['pluggable_data'] = $pluggable_data;
8128 $context['response_type'] = 'live';
8129 } elseif ( 'updates_profile' === $term ) {
8130 if ( ! empty( $users ) ) {
8131 $user = $users[0];
8132 $fields = $wpdb->get_results( $wpdb->prepare( "SELECT field_id, value FROM {$wpdb->prefix}bp_xprofile_data WHERE user_id = %d", $user->ID ) );
8133 $user_data = WordPress::get_user_context( $user->ID );
8134 $pluggable_data['user_id'] = $user->ID;
8135 $pluggable_data['user_email'] = $user_data['user_email'];
8136 foreach ( $user_data['user_role'] as $key => $role ) {
8137 $pluggable_data['user_role'][ $key ] = $role;
8138 }
8139 foreach ( $fields as $field ) {
8140 if ( function_exists( 'xprofile_get_field' ) ) {
8141 $fieldj = xprofile_get_field( $field->field_id );
8142 $pluggable_data[ $fieldj->name ] = $field->value;
8143 }
8144 }
8145 $context['pluggable_data'] = $pluggable_data;
8146 $context['response_type'] = 'live';
8147 } else {
8148 $context['pluggable_data'] = [
8149 'user_login' => 'john',
8150 'display_name' => 'john',
8151 'user_firstname' => 'john',
8152 'user_lastname' => 'd',
8153 'user_email' => 'johnd@yopmail.com',
8154 'user_role' => [
8155 'subscriber',
8156 ],
8157 'Name' => 'john',
8158 'Nickname' => 'johnd',
8159 'wp_user_id' => 16,
8160 ];
8161 $context['response_type'] = 'sample';
8162 }
8163 } elseif ( 'gains_follower' === $term ) {
8164 if ( ! empty( $results ) ) {
8165 $pluggable_data['follower'] = WordPress::get_user_context( $results[0]->follower_id );
8166 $pluggable_data['leader'] = WordPress::get_user_context( $results[0]->leader_id );
8167 $context['pluggable_data'] = $pluggable_data;
8168 $context['response_type'] = 'live';
8169 } else {
8170 $context['pluggable_data'] = [
8171 'follower' => [
8172 'wp_user_id' => 126,
8173 'user_login' => 'belli',
8174 'display_name' => 'belli',
8175 'user_firstname' => 'test',
8176 'user_lastname' => 'test',
8177 'user_email' => 'belli@gmail.com',
8178 'user_role' => [
8179 'subscriber',
8180 'wpamelia-customer',
8181 ],
8182 ],
8183 'leader' => [
8184 'wp_user_id' => 34,
8185 'user_login' => 'bella3@gmail.com',
8186 'display_name' => 'bella3@gmail.com',
8187 'user_firstname' => 'bella3',
8188 'user_lastname' => 'bella3',
8189 'user_email' => 'bellaaa3@gmail.com',
8190 'user_role' => [
8191 'wpamelia-customer',
8192 ],
8193 ],
8194 ];
8195 $context['response_type'] = 'sample';
8196 }
8197 } else {
8198 $context['pluggable_data'] = [
8199 'signup_id' => '16',
8200 'domain' => '',
8201 'path' => '',
8202 'title' => '',
8203 'user_login' => 'johnd',
8204 'user_email' => 'johnd@yopmail.com',
8205 'registered' => '2024-01-29 04:52:13',
8206 'activated' => '0000-00-00 00:00:00',
8207 'active' => '0',
8208 'meta' => [
8209 'field_1' => 'john',
8210 'field_3' => 'd',
8211 'field_4' => '09878988766',
8212 'field_2' => '123 Main Street',
8213 'field_5' => 'johnd',
8214 'profile_field_ids' => '1,3,4,2,5',
8215 ],
8216 'id' => 16,
8217 'user_name' => 'johnd',
8218 'date_sent' => '2024-01-29 04:52:13',
8219 'recently_sent' => true,
8220 'count_sent' => 1,
8221 ];
8222 $context['response_type'] = 'sample';
8223 }
8224
8225 return $context;
8226 }
8227
8228 /**
8229 * Search BP data.
8230 *
8231 * @param array $data data.
8232 * @return array
8233 */
8234 public function search_pluggables_bp_groups( $data ) {
8235 global $wpdb, $bp;
8236 $context = [];
8237 $group_data = [];
8238 $args = [
8239 'orderby' => 'user_nicename',
8240 'order' => 'ASC',
8241 'number' => 1,
8242 ];
8243
8244 $term = $data['search_term'];
8245
8246 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' ) ) {
8247 return [];
8248 }
8249
8250 if ( 'user_joins_specific_type_group' == $term ) {
8251 if ( -1 !== $data['filter']['group_type']['value'] ) {
8252 $group_type = $data['filter']['group_type']['value'];
8253 } else {
8254 $registered_types = bp_groups_get_group_types();
8255 $random_key = array_rand( $registered_types );
8256 $random_value = $registered_types[ $random_key ];
8257 $group_type = $random_value;
8258 }
8259 if ( function_exists( 'bp_get_group_ids_by_group_types' ) ) {
8260 $group_ids = bp_get_group_ids_by_group_types( $group_type );
8261 $random_key = array_rand( $group_ids );
8262 $random_value = $group_ids[ $random_key ];
8263 $group_id = $random_value;
8264 if ( function_exists( 'groups_get_group' ) ) {
8265 $group = groups_get_group( $group_id['id'] );
8266 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
8267 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
8268 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
8269 $group_data['group_type'] = $group_type;
8270 if ( function_exists( 'groups_get_group_members' ) ) {
8271 $members = groups_get_group_members(
8272 [
8273 'group_id' => $group_id,
8274 ]
8275 );
8276 $ids = [];
8277 foreach ( $members['members'] as $member ) {
8278 $ids[] = $member->ID;
8279 }
8280 $args = [
8281 'number' => 1,
8282 'include' => $ids,
8283 ];
8284 $users = get_users( $args );
8285 }
8286 }
8287 }
8288 } elseif ( 'requests_access_private_group' === $data['search_term'] ) {
8289 $group_id = $data['filter']['group_id']['value'];
8290 if ( $group_id > 0 ) {
8291 $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 ) );
8292 } else {
8293 $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' ) );
8294 }
8295 $custom_ids = $results[0]->user_id;
8296 $args = [ 'include' => $custom_ids ];
8297 $users = get_users( $args );
8298 $group_id = $results[0]->item_id;
8299 $group = groups_get_group( $group_id );
8300 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
8301 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
8302 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
8303 } elseif ( 'bb_group_created' == $term ) {
8304 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups ORDER BY id DESC LIMIT 1" );
8305 if ( function_exists( 'groups_get_group' ) ) {
8306 $group = groups_get_group( $results[0]->id );
8307 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
8308 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
8309 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
8310 $current_types = (array) bp_groups_get_group_type( $results[0]->id, false );
8311 $group_data['group_type'] = $current_types;
8312 $group_data['group_status'] = $group->status;
8313 $group_data['group_date_created'] = $group->date_created;
8314 $group_data['group_enabled_forum'] = $group->enable_forum;
8315 $group_data['group_cover_url'] = bp_get_group_cover_url( $group );
8316 $group_data['group_avatar_url'] = bp_get_group_avatar_url( $group );
8317 $group_data['group_creator'] = WordPress::get_user_context( $group->creator_id );
8318 if ( function_exists( 'groups_get_group_members' ) ) {
8319 $members = groups_get_group_members(
8320 [
8321 'group_id' => $results[0]->id,
8322 ]
8323 );
8324 foreach ( $members['members'] as $key => $member ) {
8325 $group_data['group_member'][ $key ] = WordPress::get_user_context( $member->ID );
8326 }
8327 }
8328 $args = [
8329 'item_id' => $results[0]->id,
8330 ];
8331 $invitations = groups_get_invites( $args );
8332 if ( ! empty( $invitations ) ) {
8333 foreach ( $invitations as $key => $invite ) {
8334 $group_data['invitation'][ $key ] = $invite;
8335 }
8336 }
8337 }
8338 } else {
8339 $users = get_users( $args );
8340
8341 if ( isset( $data['filter']['group_id']['value'] ) ) {
8342 $group_id = $data['filter']['group_id']['value'];
8343 $args['group_id'] = $group_id;
8344 if ( $group_id > 0 ) {
8345 $group = groups_get_group( $group_id );
8346 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
8347 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
8348 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
8349 } else {
8350 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_groups'" );
8351 if ( $table_exists ) {
8352 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups LIMIT 1" );
8353 $context['groups'] = $groups;
8354 if ( ! empty( $groups ) ) {
8355 foreach ( $groups as $group ) {
8356 $group_data['group_id'] = $group->id;
8357 $group_data['group_name'] = $group->name;
8358 $group_data['group_description'] = $group->description;
8359 }
8360 }
8361 }
8362 }
8363 }
8364 }
8365
8366 if ( 'bb_group_created' == $term ) {
8367 if ( ! empty( $group_data ) ) {
8368 $pluggable_data = $group_data;
8369 $context['pluggable_data'] = $pluggable_data;
8370 $context['response_type'] = 'live';
8371 } else {
8372 $context['pluggable_data'] = [
8373 'group_id' => 112,
8374 'group_name' => 'New Group',
8375 'group_description' => 'New Group Description',
8376 'group_type' => [ 'business' ],
8377 'group_status' => 'public',
8378 'group_date_created' => '2024-02-16 05:37:22',
8379 'group_enabled_forum' => 1,
8380 'group_cover_url' => 'https:\/\/example.com\/wp-content\/uploads\/buddypress\/groups\/23\/cover-image\/65cef4c3ea9b6-bp-cover-image.jpeg',
8381 'group_avatar_url' => 'https:\/\/example.com\/wp-content\/uploads\/group-avatars\/23\/65cef4d7cee19-bpfull.png',
8382 'group_creator' => [
8383 'wp_user_id' => 183,
8384 'user_login' => 'johnd',
8385 'display_name' => 'john',
8386 'user_firstname' => 'john',
8387 'user_lastname' => 'd',
8388 'user_email' => 'johnd@yopmail.com',
8389 'user_role' => [
8390 'subscriber',
8391 'bbp_participant',
8392 ],
8393 ],
8394 ];
8395 $context['response_type'] = 'sample';
8396 }
8397 } elseif ( ! empty( $users ) ) {
8398 $user = $users[0];
8399 $pluggable_data = $group_data;
8400
8401 $avatar = get_avatar_url( $user->ID );
8402 $pluggable_data['wp_user_id'] = $user->ID;
8403 $pluggable_data['avatar_url'] = ( $avatar ) ? $avatar : '';
8404 $pluggable_data['user_login'] = $user->user_login;
8405 $pluggable_data['display_name'] = $user->display_name;
8406 $pluggable_data['user_firstname'] = $user->user_firstname;
8407 $pluggable_data['user_lastname'] = $user->user_lastname;
8408 $pluggable_data['user_email'] = $user->user_email;
8409 $pluggable_data['user_role'] = $user->roles;
8410 $context['pluggable_data'] = $pluggable_data;
8411 $context['response_type'] = 'live';
8412 } else {
8413 $context['pluggable_data'] = [
8414 'wp_user_id' => 1,
8415 'user_login' => 'admin',
8416 'display_name' => 'Johnd',
8417 'user_firstname' => 'John',
8418 'user_lastname' => 'D',
8419 'user_email' => 'johnd@gmail.com',
8420 'user_role' => [ 'subscriber' ],
8421 'group_id' => 112,
8422 'group_name' => 'New Group',
8423 'group_description' => 'New Group Description',
8424 ];
8425 $context['response_type'] = 'sample';
8426 }
8427
8428 return $context;
8429 }
8430
8431 /**
8432 * Search complete courses.
8433 *
8434 * @param array $data data.
8435 * @return array
8436 */
8437 public function search_pluggables_complete_course( $data ) {
8438 global $wpdb;
8439 $context = [];
8440
8441 if ( isset( $data['filter']['sfwd_course_id']['value'] ) ) {
8442 $course_id = $data['filter']['sfwd_course_id']['value'];
8443 } else {
8444 $course_id = -1;
8445 }
8446 if ( -1 === $course_id ) {
8447 $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 ) );
8448 } else {
8449 $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 ) );
8450 }
8451
8452 if ( ! empty( $courses ) ) {
8453 $course = $courses[0];
8454 $course_data['course_name'] = $course->post_title;
8455 $course_data['sfwd_course_id'] = $course->ID;
8456 $course_data['course_url'] = get_permalink( $course->ID );
8457 $course_data['course_featured_image_id'] = get_post_meta( $course->ID, '_thumbnail_id', true );
8458 $course_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course->ID );
8459 $timestamp = get_user_meta( $course->user_id, 'course_completed_' . $course->ID, true );
8460 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
8461 $date_format = get_option( 'date_format' );
8462 if ( is_string( $date_format ) ) {
8463 $course_data['course_completion_date'] = wp_date( $date_format, $timestamp );
8464 }
8465 if ( function_exists( 'learndash_get_course_certificate_link' ) ) {
8466 $course_data['course_certificate'] = learndash_get_course_certificate_link( $course->ID, $course->user_id );
8467 }
8468 $context['response_type'] = 'live';
8469 } else {
8470 $course_data['course_name'] = 'Test Course';
8471 $course_data['sfwd_course_id'] = 112;
8472 $course_data['course_url'] = 'https://abc.com/test-course';
8473 $course_data['course_featured_image_id'] = 113;
8474 $course_data['course_featured_image_url'] = 'https://pqr.com/test-course-img';
8475 $course_data['course_completion_date'] = '2023-10-20';
8476 $course_data['course_certificate'] = 'https://example.com/certificates/good-performance/?course_id=112&cert-nonce=f80d0f9cc1';
8477 $context['response_type'] = 'sample';
8478 }
8479
8480 $users_data = $this->search_pluggables_add_user_role( [] );
8481 $user_data = $users_data['pluggable_data'];
8482
8483 $context['pluggable_data'] = array_merge( $course_data, $user_data );
8484 return $context;
8485 }
8486
8487 /**
8488 * Search lessons.
8489 *
8490 * @param array $data data.
8491 * @return array
8492 */
8493 public function search_pluggables_complete_lesson( $data ) {
8494 global $wpdb;
8495 $context = [];
8496
8497 if ( isset( $data['filter']['sfwd_lesson_id']['value'] ) ) {
8498 $lesson_id = $data['filter']['sfwd_lesson_id']['value'];
8499 $course_id = $data['filter']['sfwd_course_id']['value'];
8500 } else {
8501 $lesson_id = -1;
8502 $course_id = -1;
8503 }
8504 if ( -1 === $course_id ) {
8505 $courses = get_posts(
8506 [
8507 'posts_per_page' => - 1,
8508 'post_type' => 'sfwd-courses',
8509 'post_status' => 'publish',
8510 'fields' => 'ids',
8511 ]
8512 );
8513 $course_key = array_rand( $courses );
8514 $course_id = $courses[ $course_key ];
8515 }
8516 $course = get_post( $course_id );
8517 if ( $course instanceof WP_Post ) {
8518 $pluggable_data = LearnDash::get_course_context( $course );
8519 }
8520
8521 if ( -1 === $lesson_id ) {
8522 $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 ) );
8523 } else {
8524 $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 ) );
8525 }
8526
8527 if ( ! empty( $lessons ) ) {
8528 $lesson = $lessons[0];
8529
8530 $pluggable_data = WordPress::get_user_context( $lesson->user_id );
8531 $pluggable_data['lesson_name'] = $lesson->post_title;
8532 $pluggable_data['sfwd_lesson_id'] = $lesson->ID;
8533 $pluggable_data['lesson_url'] = get_permalink( $lesson->ID );
8534 $pluggable_data['lesson_featured_image_id'] = get_post_meta( $lesson->ID, '_thumbnail_id', true );
8535 $pluggable_data['lesson_featured_image_url'] = get_the_post_thumbnail_url( $lesson->ID );
8536 $context['response_type'] = 'live';
8537 } else {
8538 $pluggable_data = WordPress::get_sample_user_context();
8539 $pluggable_data['lesson_name'] = 'Test Lesson';
8540 $pluggable_data['sfwd_lesson_id'] = 114;
8541 $pluggable_data['lesson_url'] = 'https://abc.com/test-lesson';
8542 $pluggable_data['lesson_featured_image_id'] = 116;
8543 $pluggable_data['lesson_featured_image_url'] = 'https://pqr.com/test-lesson-img';
8544 $context['response_type'] = 'sample';
8545 }
8546
8547 $context['pluggable_data'] = $pluggable_data;
8548 return $context;
8549 }
8550
8551 /**
8552 * Search topics.
8553 *
8554 * @param array $data data.
8555 * @return array
8556 */
8557 public function search_pluggables_complete_topic( $data ) {
8558 global $wpdb;
8559 $context = [];
8560
8561 if ( isset( $data['filter']['sfwd_topic_id']['value'] ) ) {
8562 $topic_id = $data['filter']['sfwd_topic_id']['value'];
8563 $course_id = $data['filter']['sfwd_course_id']['value'];
8564 } else {
8565 $topic_id = -1;
8566 $course_id = -1;
8567 }
8568 if ( -1 === $course_id ) {
8569 $courses = get_posts(
8570 [
8571 'posts_per_page' => - 1,
8572 'post_type' => 'sfwd-courses',
8573 'post_status' => 'publish',
8574 'fields' => 'ids',
8575 ]
8576 );
8577 $course_key = array_rand( $courses );
8578 $course_id = $courses[ $course_key ];
8579 }
8580 $course = get_post( $course_id );
8581 if ( $course instanceof WP_Post ) {
8582 $pluggable_data = LearnDash::get_course_context( $course );
8583 }
8584
8585 if ( -1 === $topic_id ) {
8586 $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 ) );
8587 } else {
8588 $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 ) );
8589 }
8590
8591 if ( ! empty( $topics ) ) {
8592 $topic = $topics[0];
8593 $pluggable_data = WordPress::get_user_context( $topics[0]->user_id );
8594 $pluggable_data['topic_name'] = $topic->post_title;
8595 $pluggable_data['sfwd_topic_id'] = $topic->ID;
8596 $pluggable_data['topic_url'] = get_permalink( $topic->ID );
8597 $pluggable_data['topic_featured_image_id'] = get_post_meta( $topic->ID, '_thumbnail_id', true );
8598 $pluggable_data['topic_featured_image_url'] = get_the_post_thumbnail_url( $topic->ID );
8599 $context['response_type'] = 'live';
8600 } else {
8601 $pluggable_data = WordPress::get_sample_user_context();
8602 $pluggable_data['topic_name'] = 'Test Topic';
8603 $pluggable_data['sfwd_topic_id'] = 117;
8604 $pluggable_data['topic_url'] = 'https://abc.com/test-topic';
8605 $pluggable_data['topic_featured_image_id'] = 118;
8606 $pluggable_data['topic_featured_image_url'] = 'https://pqr.com/test-topic-img';
8607 $context['response_type'] = 'sample';
8608 }
8609
8610 $context['pluggable_data'] = $pluggable_data;
8611 return $context;
8612 }
8613
8614 /**
8615 * Search purchase courses.
8616 *
8617 * @param array $data data.
8618 * @return array
8619 */
8620 public function search_pluggables_purchase_course( $data ) {
8621 $context = [];
8622 $context['response_type'] = 'sample';
8623
8624 $purchase_data = [
8625 'course_product_id' => '1',
8626 'course_product_name' => 'Sample Course',
8627 'currency' => 'USD',
8628 'total_amount' => '100',
8629 'first_name' => 'John',
8630 'last_name' => 'Doe',
8631 'email' => 'john_doe@gmail.com',
8632 'phone' => '+923007626541',
8633 ];
8634
8635 $product_id = (int) ( isset( $data['filter']['course_product_id']['value'] ) ? $data['filter']['course_product_id']['value'] : '-1' );
8636 $order_id = 0;
8637
8638 if ( -1 !== $product_id ) {
8639 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
8640
8641 if ( is_array( $order_ids ) && count( $order_ids ) > 0 ) {
8642 $order_id = $order_ids[0];
8643 }
8644 } else {
8645 $orders = wc_get_orders( [] );
8646 if ( is_array( $orders ) && count( $orders ) > 0 ) {
8647 foreach ( $orders as $order ) {
8648 $items = $order->get_items();
8649
8650 if ( count( $items ) > 1 ) {
8651 continue;
8652 }
8653
8654 foreach ( $items as $item ) {
8655 if ( method_exists( $item, 'get_product_id' ) ) {
8656 $product_id = $item->get_product_id();
8657 if ( ! empty( get_post_meta( $item->get_product_id(), '_related_course', true ) ) ) {
8658 $order_id = $order->get_id();
8659 break;
8660 }
8661 }
8662 }
8663 }
8664 }
8665 }
8666
8667 if ( 0 !== $order_id ) {
8668 $order = wc_get_order( $order_id );
8669
8670 if ( $order && $order instanceof \WC_Order ) {
8671
8672 $purchase_data = LearnDash::get_purchase_course_context( $order );
8673
8674 $context['response_type'] = 'live';
8675 }
8676 }
8677
8678 $context['pluggable_data'] = $purchase_data;
8679
8680 return $context;
8681 }
8682
8683 /**
8684 * Search quiz data in LearnDash.
8685 *
8686 * @param array $data data.
8687 * @return array
8688 */
8689 public function search_pluggables_ld_quiz( $data ) {
8690 $context = [];
8691 $context['response_type'] = 'sample';
8692 global $wpdb;
8693 $user_data = WordPress::get_sample_user_context();
8694 $quiz_data = [
8695 'quiz' => 7193,
8696 'score' => 1,
8697 'count' => 1,
8698 'question_show_count' => 1,
8699 'pass' => 1,
8700 'rank' => '-',
8701 'time' => 1703595328,
8702 'pro_quizid' => 1,
8703 'course' => 0,
8704 'lesson' => 0,
8705 'topic' => 0,
8706 'points' => 20,
8707 'total_points' => 20,
8708 'percentage' => 100,
8709 'timespent' => 2.7309999999999999,
8710 'has_graded' => false,
8711 'statistic_ref_id' => 9,
8712 'started' => 1703595325,
8713 'completed' => 1703595328,
8714 'ld_version' => '4.8.0.1',
8715 'quiz_key' => '1703595328_1_7193_0',
8716 ];
8717 $output_questions['question'] = [
8718 'ID' => 7195,
8719 'post_content' => '<p>This is a first question<\/p>',
8720 'type' => 'sfwd-question',
8721 'question_type' => 'single',
8722 'points' => 20,
8723 'answers' => [
8724 'sort_answer' => [
8725 [
8726 'answer' => '',
8727 'points' => 0,
8728 'correct' => false,
8729 'type' => 'answer',
8730 ],
8731 ],
8732 'classic_answer' => [
8733 [
8734 'answer' => 'Ans 1',
8735 'points' => 1,
8736 'correct' => false,
8737 'type' => 'answer',
8738 ],
8739 [
8740 'answer' => 'Ans 2',
8741 'points' => 0,
8742 'correct' => true,
8743 'type' => 'answer',
8744 ],
8745 ],
8746 'matrix_sort_answer' => [
8747 [
8748 'answer' => '',
8749 'points' => 0,
8750 'correct' => false,
8751 'type' => 'answer',
8752 ],
8753 ],
8754 'cloze_answer' => [
8755 [
8756 'answer' => '',
8757 'points' => 0,
8758 'correct' => false,
8759 'type' => 'answer',
8760 ],
8761 ],
8762 'free_answer' => [
8763 [
8764 'answer' => '',
8765 'points' => 0,
8766 'correct' => false,
8767 'type' => 'answer',
8768 ],
8769 ],
8770 'assessment_answer' => [
8771 [
8772 'answer' => '',
8773 'points' => 0,
8774 'correct' => false,
8775 'type' => 'answer',
8776 ],
8777 ],
8778 'essay' => [
8779 [
8780 'answer' => '',
8781 'points' => 0,
8782 'correct' => false,
8783 'type' => 'answer',
8784 ],
8785 ],
8786 ],
8787 ];
8788
8789 $quiz_id = (int) ( isset( $data['filter']['sfwd_quiz_id']['value'] ) ? $data['filter']['sfwd_quiz_id']['value'] : '-1' );
8790 $question_id = $data['filter']['sfwd_question_id']['value'];
8791 $term = $data['search_term'];
8792 $mark = 1;
8793 if ( 'passes_quiz' == $term ) {
8794 $mark = 1;
8795 } elseif ( 'fails_quiz' == $term ) {
8796 $mark = 0;
8797 }
8798 if ( 'passes_quiz' == $term || 'fails_quiz' == $term ) {
8799 if ( -1 == $quiz_id ) {
8800 $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 ) );
8801 } else {
8802 $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 ) );
8803 }
8804 } elseif ( 'quiz_essay_submitted' == $term ) {
8805 $question_id = $data['filter']['sfwd_question_id']['value'];
8806 $args = [
8807 'post_type' => 'sfwd-essays',
8808 'posts_per_page' => 1,
8809 'order' => 'DESC',
8810 'post_status' => [ 'graded', 'not_graded' ],
8811 'meta_query' => [
8812 'relation' => 'AND',
8813 [
8814 'key' => 'quiz_post_id',
8815 'value' => $quiz_id,
8816 'compare' => '=',
8817 ],
8818 [
8819 'key' => 'question_post_id',
8820 'value' => $question_id,
8821 'compare' => '=',
8822 ],
8823 ],
8824 ];
8825 $essay = get_posts( $args );
8826 } elseif ( 'quiz_essay_graded' == $term ) {
8827 $args = [
8828 'post_type' => 'sfwd-essays',
8829 'posts_per_page' => 1,
8830 'order' => 'DESC',
8831 'post_status' => 'graded',
8832 'meta_query' => [
8833 'relation' => 'AND',
8834 [
8835 'key' => 'quiz_post_id',
8836 'value' => $quiz_id,
8837 'compare' => '=',
8838 ],
8839 [
8840 'key' => 'question_post_id',
8841 'value' => $question_id,
8842 'compare' => '=',
8843 ],
8844 ],
8845 ];
8846 $essay = get_posts( $args );
8847 }
8848
8849 if ( 'quiz_essay_submitted' == $term || 'quiz_essay_graded' == $term ) {
8850 if ( ! empty( $essay ) ) {
8851 $context = is_numeric( $essay[0]->post_author ) ? WordPress::get_user_context( (int) $essay[0]->post_author ) : [];
8852 $course_id = get_post_meta( $essay[0]->ID, 'course_id', true );
8853 $lesson_id = get_post_meta( $essay[0]->ID, 'lesson_id', true );
8854 $context['quiz_name'] = get_the_title( $quiz_id );
8855 $context['sfwd_quiz_id'] = $quiz_id;
8856 $context['sfwd_question_id'] = $question_id;
8857 $context['question_name'] = is_numeric( $question_id ) ? get_the_title( (int) $question_id ) : null;
8858 $context['course_name'] = is_numeric( $course_id ) ? get_the_title( (int) $course_id ) : null;
8859 $context['course_id'] = $course_id;
8860 $context['lesson_name'] = is_numeric( $lesson_id ) ? get_the_title( (int) $lesson_id ) : null;
8861 $context['lesson_id'] = $lesson_id;
8862 $context['essay_id'] = $essay[0]->ID;
8863 $context['essay'] = WordPress::get_post_context( $essay[0]->ID );
8864 if ( 'quiz_essay_graded' == $term ) {
8865 $users_quiz_data = get_user_meta( (int) $essay[0]->post_author, '_sfwd-quizzes', true );
8866 if ( is_array( $users_quiz_data ) && is_array( $users_quiz_data[0] ) ) {
8867 $essay_post_id = $essay[0]->ID;
8868 $result = array_filter(
8869 $users_quiz_data[0]['graded'],
8870 function( $item ) use ( $essay_post_id ) {
8871 return $item['post_id'] === $essay_post_id;
8872 }
8873 );
8874 $context['essay_points_earned'] = $result[1]['points_awarded'];
8875 }
8876 }
8877 $context['response_type'] = 'live';
8878 } else {
8879 $context = WordPress::get_sample_user_context();
8880 $context['quiz_name'] = 'Test Quiz';
8881 $context['sfwd_quiz_id'] = 11;
8882 $context['sfwd_question_id'] = 12;
8883 $context['course_name'] = 'Test Course';
8884 $context['course_id'] = 13;
8885 $context['lesson_name'] = 'Test Lesson';
8886 $context['lesson_id'] = 14;
8887 $context['essay'] = [
8888 'ID' => 12,
8889 'post_content' => 'demo',
8890 ];
8891 $context['response_type'] = 'sample';
8892 }
8893
8894 $context['pluggable_data'] = $context;
8895 } else {
8896 if ( ! empty( $result ) ) {
8897 $user_data = WordPress::get_user_context( $result[0]->user_id );
8898 $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 ) );
8899
8900 $data = unserialize( $quiz_result[0]->meta_value );
8901 $found_element = null;
8902 foreach ( (array) $data as $element ) {
8903 if ( is_array( $element ) ) {
8904 if ( isset( $element['statistic_ref_id'] ) && $element['statistic_ref_id'] == $result[0]->statistic_ref_id ) {
8905 $found_element = $element;
8906 break;
8907 }
8908 }
8909 }
8910 $quiz_data = (array) $found_element;
8911 $output_questions = LearnDash::get_quiz_questions_answers( $quiz_id );
8912 if ( is_array( $quiz_data ) ) {
8913 $quiz_data['quiz_name'] = get_the_title( $quiz_id );
8914 $quiz_data['sfwd_quiz_id'] = $quiz_id;
8915 }
8916 $context['response_type'] = 'live';
8917 }
8918 $context['pluggable_data'] = array_merge( $quiz_data, $user_data, $output_questions );
8919 }
8920
8921 return $context;
8922 }
8923
8924 /**
8925 * Fetch BB templates.
8926 *
8927 * @return array
8928 */
8929 public function get_beaver_builder_templates() {
8930 $allowed_types = [ 'subscribe-form', 'contact-form' ];
8931 $templates = [];
8932 $all_templates = get_posts(
8933 [
8934 'post_type' => 'fl-builder-template',
8935 'meta_key' => '_fl_builder_data',
8936 'posts_per_page' => -1,
8937 ]
8938 );
8939 $posts = get_posts(
8940 [
8941 'post_type' => 'any',
8942 'meta_key' => '_fl_builder_data',
8943 'posts_per_page' => -1,
8944 ]
8945 );
8946 $posts = array_merge( $all_templates, $posts );
8947
8948 if ( ! empty( $posts ) ) {
8949 foreach ( $posts as $post ) {
8950 $meta = get_post_meta( $post->ID, '_fl_builder_data', true );
8951 foreach ( (array) $meta as $node_id => $node ) {
8952 if ( is_object( $node ) && isset( $node->type ) && 'module' === $node->type && property_exists( $node, 'settings' ) ) {
8953 $settings = $node->settings;
8954 if ( in_array( $settings->type, $allowed_types, true ) ) {
8955 $label = $post->post_title;
8956 if ( '' !== $settings->node_label ) {
8957 $label .= ' - ' . $settings->node_label;
8958 }
8959 $templates[] = [
8960 'label' => $label,
8961 'value' => $node_id,
8962 ];
8963 }
8964 }
8965 }
8966 }
8967 }
8968 return $templates;
8969 }
8970
8971 /**
8972 * Search beaver builder forms.
8973 *
8974 * @param array $data data.
8975 * @return array
8976 */
8977 public function search_beaver_builder_forms( $data ) {
8978 $templates = $this->get_beaver_builder_templates();
8979 return [
8980 'options' => $templates,
8981 'hasMore' => false,
8982 ];
8983 }
8984
8985 /**
8986 * Search FluentCRM fields.
8987 *
8988 * @param array $data data.
8989 * @return array
8990 */
8991 public function search_fluentcrm_custom_fields( $data ) {
8992 $context = [];
8993 if ( ! class_exists( 'FluentCrm\App\Models\CustomContactField' ) ) {
8994 return [];
8995 }
8996 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
8997 $context['fields'] = $custom_fields;
8998 return $context;
8999 }
9000
9001 /**
9002 * Search FluentCRM Company fields.
9003 *
9004 * @param array $data data.
9005 * @return array
9006 */
9007 public function search_fluentcrm_company_custom_fields( $data ) {
9008 $context = [];
9009 if ( function_exists( 'fluentcrm_get_custom_company_fields' ) ) {
9010 $custom_fields = fluentcrm_get_custom_company_fields();
9011 $context['fields'] = $custom_fields;
9012 }
9013 return $context;
9014 }
9015
9016 /**
9017 * Search FluentCRM fields and display it in dropdown for trigger.
9018 *
9019 * @param array $data data.
9020 * @return array
9021 */
9022 public function search_fluentcrm_custom_fields_data( $data ) {
9023
9024 if ( ! class_exists( 'FluentCrm\App\Models\CustomContactField' ) ) {
9025 return [];
9026 }
9027
9028 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
9029 $options = [];
9030
9031 if ( ! empty( $custom_fields ) ) {
9032 foreach ( $custom_fields as $custom_field ) {
9033 $options[] = [
9034 'label' => $custom_field['label'],
9035 'value' => $custom_field['slug'],
9036 ];
9037 }
9038 }
9039
9040 return [
9041 'options' => $options,
9042 'hasMore' => false,
9043 ];
9044 }
9045
9046 /**
9047 * Fetch WP JOB Manager Last Data.
9048 *
9049 * @param array $data data.
9050 * @return array
9051 */
9052 public function search_wp_job_manger_last_data( $data ) {
9053 global $wpdb;
9054 $job_type = $data['filter']['job_type']['value'];
9055 $args = [
9056 'posts_per_page' => 1,
9057 'post_type' => 'job_listing',
9058 'orderby' => 'id',
9059 'order' => 'DESC',
9060 ];
9061
9062 if ( -1 !== $job_type ) {
9063 $args['tax_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
9064 [
9065 'taxonomy' => 'job_listing_type',
9066 'field' => 'term_id',
9067 'terms' => $job_type,
9068 ],
9069 ];
9070 }
9071 $posts = get_posts( $args );
9072 if ( empty( $posts ) ) {
9073 $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 );
9074 return $context;
9075 }
9076
9077 $post = $posts[0];
9078 $post_content = WordPress::get_post_context( $post->ID );
9079 $post_meta = WordPress::get_post_meta( $post->ID );
9080 $job_data = array_merge(
9081 $post_content,
9082 is_array( $post_meta ) ? $post_meta : [],
9083 is_numeric( $post->post_author ) ? WordPress::get_user_context( (int) $post->post_author ) : []
9084 );
9085 foreach ( $job_data as $key => $job ) {
9086 $newkey = str_replace( 'post', 'wpjob', $key );
9087 unset( $job_data[ $key ] );
9088 $job_data[ $newkey ] = $job;
9089 }
9090 $context['response_type'] = 'live';
9091 $context['pluggable_data'] = $job_data;
9092 return $context;
9093
9094 }
9095
9096 /**
9097 * Get Amelia Appointment Category.
9098 *
9099 * @param array $data data.
9100 *
9101 * @return array
9102 */
9103 public function search_amelia_category_list( $data ) {
9104
9105 global $wpdb;
9106
9107 $page = $data['page'];
9108 $limit = Utilities::get_search_page_limit();
9109 $offset = $limit * ( $page - 1 );
9110
9111 $categories = $wpdb->get_results(
9112 $wpdb->prepare(
9113 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_categories WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
9114 [ 'visible', $limit, $offset ]
9115 ),
9116 OBJECT
9117 );
9118
9119 $categories_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9120
9121 $options = [];
9122 if ( ! empty( $categories ) ) {
9123 foreach ( $categories as $category ) {
9124 $options[] = [
9125 'label' => $category->name,
9126 'value' => $category->id,
9127 ];
9128 }
9129 }
9130
9131 return [
9132 'options' => $options,
9133 'hasMore' => $categories_count > $limit && $categories_count > $offset,
9134 ];
9135
9136 }
9137
9138 /**
9139 * Get Amelia Appointment Services.
9140 *
9141 * @param array $data data.
9142 *
9143 * @return array
9144 */
9145 public function search_amelia_service_list( $data ) {
9146
9147 global $wpdb;
9148
9149 $page = $data['page'];
9150 $limit = Utilities::get_search_page_limit();
9151 $offset = $limit * ( $page - 1 );
9152
9153 $services = $wpdb->get_results(
9154 $wpdb->prepare(
9155 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_services
9156 WHERE categoryId = %d AND status = %s
9157 ORDER BY name ASC LIMIT %d OFFSET %d",
9158 [ $data['dynamic'], 'visible', $limit, $offset ]
9159 ),
9160 OBJECT
9161 );
9162
9163 $services_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9164
9165 $options = [];
9166 if ( ! empty( $services ) ) {
9167 foreach ( $services as $category ) {
9168 $options[] = [
9169 'label' => $category->name,
9170 'value' => $category->id,
9171 ];
9172 }
9173 }
9174
9175 return [
9176 'options' => $options,
9177 'hasMore' => $services_count > $limit && $services_count > $offset,
9178 ];
9179
9180 }
9181
9182 /**
9183 * Get Amelia Events.
9184 *
9185 * @param array $data data.
9186 *
9187 * @return array
9188 */
9189 public function search_amelia_events_list( $data ) {
9190
9191 global $wpdb;
9192
9193 $page = $data['page'];
9194 $limit = Utilities::get_search_page_limit();
9195 $offset = $limit * ( $page - 1 );
9196
9197 $events = $wpdb->get_results(
9198 $wpdb->prepare(
9199 "SELECT SQL_CALC_FOUND_ROWS id, name from {$wpdb->prefix}amelia_events WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
9200 [ 'approved', $limit, $offset ]
9201 ),
9202 OBJECT
9203 );
9204
9205 $list_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9206
9207 $options = [];
9208 if ( ! empty( $events ) ) {
9209 foreach ( $events as $event ) {
9210 $options[] = [
9211 'label' => $event->name,
9212 'value' => $event->id,
9213 ];
9214 }
9215 }
9216
9217 return [
9218 'options' => $options,
9219 'hasMore' => $list_count > $limit && $list_count > $offset,
9220 ];
9221
9222 }
9223
9224 /**
9225 * Get Amelia Events.
9226 *
9227 * @param array $data data.
9228 *
9229 * @return array
9230 */
9231 public function search_amelia_booking_status_list( $data ) {
9232
9233 $options = [];
9234 $options[] = [
9235 'label' => 'Approved',
9236 'value' => 'approved',
9237 ];
9238 $options[] = [
9239 'label' => 'Pending',
9240 'value' => 'pending',
9241 ];
9242 $options[] = [
9243 'label' => 'Rejected',
9244 'value' => 'rejected',
9245 ];
9246 $options[] = [
9247 'label' => 'Canceled',
9248 'value' => 'canceled',
9249 ];
9250 $options[] = [
9251 'label' => 'No-show',
9252 'value' => 'no-show',
9253 ];
9254 return [
9255 'options' => $options,
9256 'hasMore' => false,
9257 ];
9258 }
9259
9260 /**
9261 * Get last data for trigger.
9262 *
9263 * @param array $data data.
9264 * @return array
9265 */
9266 public function search_amelia_appointment_booked_triggers_last_data( $data ) {
9267 global $wpdb;
9268
9269 $context = [];
9270
9271 $appointment_category = $data['filter']['amelia_category_list']['value'];
9272 $appointment_service = $data['filter']['amelia_service_list']['value'];
9273
9274 if ( -1 === $appointment_service ) {
9275 // If service exists as per category selected.
9276 $service_exist = $wpdb->get_row(
9277 $wpdb->prepare(
9278 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
9279 [ $appointment_category ]
9280 ),
9281 ARRAY_A
9282 );
9283
9284 if ( empty( $service_exist ) ) {
9285 $result = [];
9286 } else {
9287 $result = $wpdb->get_row(
9288 '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 )',
9289 ARRAY_A
9290 );
9291 }
9292 } else {
9293 $result = $wpdb->get_row(
9294 $wpdb->prepare(
9295 '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',
9296 [ $appointment_service ]
9297 ),
9298 ARRAY_A
9299 );
9300 }
9301
9302 if ( ! empty( $result ) ) {
9303
9304 $payment_result = $wpdb->get_row(
9305 $wpdb->prepare(
9306 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
9307 [ $result['id'] ]
9308 ),
9309 ARRAY_A
9310 );
9311
9312 $customer_result = $wpdb->get_row(
9313 $wpdb->prepare(
9314 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
9315 [ $result['customerId'] ]
9316 ),
9317 ARRAY_A
9318 );
9319
9320 $service_result = $wpdb->get_row(
9321 $wpdb->prepare(
9322 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
9323 [ $result['serviceId'] ]
9324 ),
9325 ARRAY_A
9326 );
9327
9328 $category_result = $wpdb->get_row(
9329 $wpdb->prepare(
9330 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
9331 [ $service_result['categoryId'] ]
9332 ),
9333 ARRAY_A
9334 );
9335
9336 if ( $result['couponId'] ) {
9337 $coupon_result = $wpdb->get_row(
9338 $wpdb->prepare(
9339 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9340 [ $result['couponId'] ]
9341 ),
9342 ARRAY_A
9343 );
9344 } else {
9345 $coupon_result = [];
9346 }
9347
9348 if ( ! empty( $result['customFields'] ) ) {
9349 $custom_fields = json_decode( $result['customFields'], true );
9350
9351 $fields_arr = [];
9352 foreach ( (array) $custom_fields as $fields ) {
9353 if ( is_array( $fields ) ) {
9354 $fields_arr[ $fields['label'] ] = $fields['value'];
9355 }
9356 }
9357 unset( $result['customFields'] );
9358 } else {
9359 $fields_arr = [];
9360 }
9361
9362 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
9363 $context['response_type'] = 'live';
9364 } else {
9365
9366 $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 );
9367 }
9368
9369 return $context;
9370 }
9371
9372 /**
9373 * Get last data for trigger.
9374 *
9375 * @param array $data data.
9376 * @return array
9377 */
9378 public function search_amelia_appointment_booking_status_changed_triggers_last_data( $data ) {
9379 global $wpdb;
9380
9381 $context = [];
9382
9383 $appointment_category = $data['filter']['amelia_category_list']['value'];
9384 $appointment_service = $data['filter']['amelia_service_list']['value'];
9385 $appointment_status = $data['filter']['appointment_status']['value'];
9386
9387 if ( -1 === $appointment_service ) {
9388 // If service exists as per category selected.
9389 $service_exist = $wpdb->get_row(
9390 $wpdb->prepare(
9391 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
9392 [ $appointment_category ]
9393 ),
9394 ARRAY_A
9395 );
9396
9397 if ( empty( $service_exist ) ) {
9398 $result = [];
9399 } else {
9400 $result = $wpdb->get_row(
9401 '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 )',
9402 ARRAY_A
9403 );
9404 }
9405 } else {
9406 if ( -1 === $appointment_status ) {
9407 $result = $wpdb->get_row(
9408 $wpdb->prepare(
9409 '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',
9410 [ $appointment_service ]
9411 ),
9412 ARRAY_A
9413 );
9414 } else {
9415 $result = $wpdb->get_row(
9416 $wpdb->prepare(
9417 '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',
9418 [ $appointment_status, $appointment_service ]
9419 ),
9420 ARRAY_A
9421 );
9422 }
9423 }
9424
9425 if ( ! empty( $result ) ) {
9426
9427 $payment_result = $wpdb->get_row(
9428 $wpdb->prepare(
9429 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
9430 [ $result['id'] ]
9431 ),
9432 ARRAY_A
9433 );
9434
9435 $customer_result = $wpdb->get_row(
9436 $wpdb->prepare(
9437 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
9438 [ $result['customerId'] ]
9439 ),
9440 ARRAY_A
9441 );
9442
9443 $service_result = $wpdb->get_row(
9444 $wpdb->prepare(
9445 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
9446 [ $result['serviceId'] ]
9447 ),
9448 ARRAY_A
9449 );
9450
9451 $category_result = $wpdb->get_row(
9452 $wpdb->prepare(
9453 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
9454 [ $service_result['categoryId'] ]
9455 ),
9456 ARRAY_A
9457 );
9458
9459 if ( $result['couponId'] ) {
9460 $coupon_result = $wpdb->get_row(
9461 $wpdb->prepare(
9462 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9463 [ $result['couponId'] ]
9464 ),
9465 ARRAY_A
9466 );
9467 } else {
9468 $coupon_result = [];
9469 }
9470
9471 if ( ! empty( $result['customFields'] ) ) {
9472 $custom_fields = json_decode( $result['customFields'], true );
9473
9474 $fields_arr = [];
9475 foreach ( (array) $custom_fields as $fields ) {
9476 if ( is_array( $fields ) ) {
9477 $fields_arr[ $fields['label'] ] = $fields['value'];
9478 }
9479 }
9480 unset( $result['customFields'] );
9481 } else {
9482 $fields_arr = [];
9483 }
9484
9485 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
9486 $context['pluggable_data']['amelia_category_list'] = $appointment_category;
9487 if ( -1 === $appointment_status ) {
9488 $context['pluggable_data']['appointment_status'] = 'approved';
9489 } else {
9490 $context['pluggable_data']['appointment_status'] = $appointment_status;
9491 }
9492 if ( -1 === $appointment_service ) {
9493 $context['pluggable_data']['amelia_service_list'] = $service_result['id'];
9494 } else {
9495 $context['pluggable_data']['amelia_service_list'] = $appointment_service;
9496 }
9497 $context['response_type'] = 'live';
9498 } else {
9499
9500 $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 );
9501 }
9502
9503 return $context;
9504 }
9505
9506 /**
9507 * Get last data for trigger.
9508 *
9509 * @param array $data data.
9510 * @return array
9511 */
9512 public function search_amelia_event_status_changed_triggers_last_data( $data ) {
9513 global $wpdb;
9514
9515 $context = [];
9516
9517 $event_selected = $data['filter']['amelia_events_list']['value'];
9518 $event_status = $data['filter']['event_booking_status']['value'];
9519
9520 $query = 'SELECT *
9521 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
9522 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
9523 ON customer.id = event_period.customerBookingId';
9524
9525 if ( -1 === $event_selected ) {
9526 if ( -1 !== $event_status ) {
9527 $query .= $wpdb->prepare(
9528 ' WHERE event_period.customerBookingId = (
9529 SELECT MAX(customerBookingId)
9530 FROM ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods
9531 ) AND customer.status = %s',
9532 $event_status
9533 );
9534 } else {
9535 $query .= ' WHERE event_period.customerBookingId = (
9536 SELECT MAX(customerBookingId)
9537 FROM ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods
9538 )';
9539 }
9540 } else {
9541 $event_periods_result = $wpdb->get_results(
9542 $wpdb->prepare(
9543 'SELECT id
9544 FROM ' . $wpdb->prefix . 'amelia_events_periods
9545 WHERE eventId = %d',
9546 [ $event_selected ]
9547 ),
9548 ARRAY_A
9549 );
9550 if ( ! empty( $event_periods_result ) ) {
9551 $ids = wp_list_pluck( $event_periods_result, 'id' );
9552 }
9553
9554 if ( ! empty( $ids ) ) {
9555 if ( -1 !== $event_status ) {
9556 $query .= $wpdb->prepare(
9557 ' WHERE event_period.eventPeriodId IN (%s) AND customer.status = %s',
9558 implode( ',', $ids ),
9559 $event_status
9560 );
9561 } else {
9562 $query .= $wpdb->prepare(
9563 ' WHERE event_period.eventPeriodId IN (%s)',
9564 implode( ',', $ids )
9565 );
9566 }
9567 } else {
9568 if ( -1 !== $event_status ) {
9569 $query .= $wpdb->prepare(
9570 ' WHERE event_period.eventPeriodId = %d AND customer.status = %s',
9571 $event_selected,
9572 $event_status
9573 );
9574 } else {
9575 $query .= $wpdb->prepare(
9576 ' WHERE event_period.eventPeriodId = %d',
9577 $event_selected
9578 );
9579 }
9580 }
9581 }
9582
9583 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
9584
9585 if ( ! empty( $result ) ) {
9586 $event = $wpdb->get_row(
9587 $wpdb->prepare(
9588 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
9589 [ $event_selected ]
9590 ),
9591 ARRAY_A
9592 );
9593 $event = isset( $event ) ? $event : [];
9594 $event_tags = $wpdb->get_results(
9595 $wpdb->prepare(
9596 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
9597 [ $event_selected ]
9598 ),
9599 ARRAY_A
9600 );
9601 $tags = [];
9602 if ( ! empty( $event_tags ) ) {
9603 foreach ( $event_tags as $key => $tag ) {
9604 $tags['event_tag'][ $key ] = $tag['name'];
9605 }
9606 } else {
9607 $tags = [];
9608 }
9609 $customer_result = $wpdb->get_row(
9610 $wpdb->prepare(
9611 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
9612 [ $result['customerId'] ]
9613 ),
9614 ARRAY_A
9615 );
9616 $customer_result = isset( $customer_result ) ? $customer_result : [];
9617 if ( $result['couponId'] ) {
9618 $coupon_result = $wpdb->get_row(
9619 $wpdb->prepare(
9620 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9621 [ $result['couponId'] ]
9622 ),
9623 ARRAY_A
9624 );
9625 } else {
9626 $coupon_result = [];
9627 }
9628 if ( ! empty( $result['customFields'] ) ) {
9629 $custom_fields = json_decode( $result['customFields'], true );
9630
9631 $fields_arr = [];
9632 foreach ( (array) $custom_fields as $fields ) {
9633 if ( is_array( $fields ) ) {
9634 $fields_arr[ $fields['label'] ] = $fields['value'];
9635 }
9636 }
9637 unset( $result['customFields'] );
9638 } else {
9639 $fields_arr = [];
9640 }
9641 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
9642 if ( -1 === $event_status ) {
9643 $context['pluggable_data']['event_booking_status'] = 'approved';
9644 } else {
9645 $context['pluggable_data']['event_booking_status'] = $event_status;
9646 }
9647 $context['response_type'] = 'live';
9648 } else {
9649
9650 $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 );
9651 }
9652
9653 return $context;
9654 }
9655
9656 /**
9657 * Get last data for trigger.
9658 *
9659 * @param array $data data.
9660 * @return array
9661 */
9662 public function search_amelia_new_event_attendee_triggers_last_data( $data ) {
9663 global $wpdb;
9664
9665 $context = [];
9666
9667 $event_selected = $data['filter']['amelia_events_list']['value'];
9668
9669 $query = 'SELECT *
9670 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
9671 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
9672 ON customer.id = event_period.customerBookingId';
9673
9674 if ( -1 === $event_selected ) {
9675 $query .= '
9676 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods )';
9677 } else {
9678 $event_periods_result = $wpdb->get_results(
9679 $wpdb->prepare(
9680 'SELECT id
9681 FROM ' . $wpdb->prefix . 'amelia_events_periods
9682 WHERE eventId = %d',
9683 [ $event_selected ]
9684 ),
9685 ARRAY_A
9686 );
9687 if ( ! empty( $event_periods_result ) ) {
9688 $ids = wp_list_pluck( $event_periods_result, 'id' );
9689 }
9690 if ( ! empty( $ids ) ) {
9691 $query .= $wpdb->prepare(
9692 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND event_period.eventPeriodId IN (%s)',
9693 implode( ',', $ids )
9694 );
9695 } else {
9696 $query .= $wpdb->prepare(
9697 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND event_period.eventPeriodId = %d',
9698 [ $event_selected ]
9699 );
9700 }
9701 }
9702 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
9703
9704 if ( ! empty( $result ) ) {
9705
9706 $event = $wpdb->get_row(
9707 $wpdb->prepare(
9708 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
9709 [ $event_selected ]
9710 ),
9711 ARRAY_A
9712 );
9713 $event = isset( $event ) ? $event : [];
9714 $event_tags = $wpdb->get_results(
9715 $wpdb->prepare(
9716 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
9717 [ $event_selected ]
9718 ),
9719 ARRAY_A
9720 );
9721 $tags = [];
9722 if ( ! empty( $event_tags ) ) {
9723 foreach ( $event_tags as $key => $tag ) {
9724 $tags['event_tag'][ $key ] = $tag['name'];
9725 }
9726 } else {
9727 $tags = [];
9728 }
9729
9730 $customer_result = $wpdb->get_row(
9731 $wpdb->prepare(
9732 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
9733 [ $result['customerId'] ]
9734 ),
9735 ARRAY_A
9736 );
9737 $customer_result = isset( $customer_result ) ? $customer_result : [];
9738
9739 if ( $result['couponId'] ) {
9740 $coupon_result = $wpdb->get_row(
9741 $wpdb->prepare(
9742 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9743 [ $result['couponId'] ]
9744 ),
9745 ARRAY_A
9746 );
9747 } else {
9748 $coupon_result = [];
9749 }
9750
9751 if ( ! empty( $result['customFields'] ) ) {
9752 $custom_fields = json_decode( $result['customFields'], true );
9753
9754 $fields_arr = [];
9755 foreach ( (array) $custom_fields as $fields ) {
9756 if ( is_array( $fields ) ) {
9757 $fields_arr[ $fields['label'] ] = $fields['value'];
9758 }
9759 }
9760 unset( $result['customFields'] );
9761 } else {
9762 $fields_arr = [];
9763 }
9764
9765 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
9766 $context['response_type'] = 'live';
9767 } else {
9768
9769 $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 );
9770 }
9771
9772 return $context;
9773 }
9774
9775 /**
9776 * Get last data for trigger.
9777 *
9778 * @param array $data data.
9779 * @return array
9780 */
9781 public function search_amelia_event_booking_cancelled_triggers_last_data( $data ) {
9782 global $wpdb;
9783
9784 $context = [];
9785
9786 $event_selected = $data['filter']['amelia_events_list']['value'];
9787
9788 if ( -1 === $event_selected ) {
9789 $result = $wpdb->get_row(
9790 'SELECT *
9791 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
9792 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
9793 ON customer.id = event_period.customerBookingId
9794 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND customer.status = "canceled"',
9795 ARRAY_A
9796 );
9797 } else {
9798 $event_periods_result = $wpdb->get_results(
9799 $wpdb->prepare(
9800 'SELECT id
9801 FROM ' . $wpdb->prefix . 'amelia_events_periods
9802 WHERE eventId = %d',
9803 [ $event_selected ]
9804 ),
9805 ARRAY_A
9806 );
9807 if ( ! empty( $event_periods_result ) ) {
9808 $ids = wp_list_pluck( $event_periods_result, 'id' );
9809 }
9810 $query = 'SELECT *
9811 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
9812 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
9813 ON customer.id = event_period.customerBookingId';
9814 if ( ! empty( $ids ) ) {
9815 $query .= $wpdb->prepare(
9816 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId IN (%s) AND customer.status = "canceled"',
9817 implode( ',', $ids )
9818 );
9819 } else {
9820 $query .= $wpdb->prepare(
9821 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId = %d
9822 AND customer.status = "canceled"',
9823 [ $event_selected ]
9824 );
9825 }
9826 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
9827 }
9828
9829 if ( ! empty( $result ) ) {
9830 $event = $wpdb->get_row(
9831 $wpdb->prepare(
9832 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
9833 [ $event_selected ]
9834 ),
9835 ARRAY_A
9836 );
9837 $event = isset( $event ) ? $event : [];
9838 $event_tags = $wpdb->get_results(
9839 $wpdb->prepare(
9840 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
9841 [ $event_selected ]
9842 ),
9843 ARRAY_A
9844 );
9845 $tags = [];
9846 if ( ! empty( $event_tags ) ) {
9847 foreach ( $event_tags as $key => $tag ) {
9848 $tags['event_tag'][ $key ] = $tag['name'];
9849 }
9850 } else {
9851 $tags = [];
9852 }
9853 $customer_result = $wpdb->get_row(
9854 $wpdb->prepare(
9855 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
9856 [ $result['customerId'] ]
9857 ),
9858 ARRAY_A
9859 );
9860 $customer_result = isset( $customer_result ) ? $customer_result : [];
9861 if ( $result['couponId'] ) {
9862 $coupon_result = $wpdb->get_row(
9863 $wpdb->prepare(
9864 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9865 [ $result['couponId'] ]
9866 ),
9867 ARRAY_A
9868 );
9869 } else {
9870 $coupon_result = [];
9871 }
9872 if ( ! empty( $result['customFields'] ) ) {
9873 $custom_fields = json_decode( $result['customFields'], true );
9874
9875 $fields_arr = [];
9876 foreach ( (array) $custom_fields as $fields ) {
9877 if ( is_array( $fields ) ) {
9878 $fields_arr[ $fields['label'] ] = $fields['value'];
9879 }
9880 }
9881 unset( $result['customFields'] );
9882 } else {
9883 $fields_arr = [];
9884 }
9885 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
9886 $context['response_type'] = 'live';
9887 } else {
9888
9889 $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 );
9890 }
9891
9892 return $context;
9893 }
9894
9895 /**
9896 * Get last data for trigger.
9897 *
9898 * @param array $data data.
9899 * @return array
9900 */
9901 public function search_amelia_appointment_rescheduled_triggers_last_data( $data ) {
9902 global $wpdb;
9903
9904 $context = [];
9905
9906 $appointment_category = $data['filter']['amelia_category_list']['value'];
9907 $appointment_service = $data['filter']['amelia_service_list']['value'];
9908
9909 if ( -1 === $appointment_service ) {
9910 // If service exists as per category selected.
9911 $service_exist = $wpdb->get_row(
9912 $wpdb->prepare(
9913 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
9914 [ $appointment_category ]
9915 ),
9916 ARRAY_A
9917 );
9918
9919 if ( empty( $service_exist ) ) {
9920 $result = [];
9921 } else {
9922 $result = $wpdb->get_row(
9923 '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 )',
9924 ARRAY_A
9925 );
9926 }
9927 } else {
9928 $result = $wpdb->get_row(
9929 $wpdb->prepare(
9930 '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',
9931 [ $appointment_service ]
9932 ),
9933 ARRAY_A
9934 );
9935 }
9936
9937 if ( ! empty( $result ) ) {
9938
9939 $payment_result = $wpdb->get_row(
9940 $wpdb->prepare(
9941 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
9942 [ $result['id'] ]
9943 ),
9944 ARRAY_A
9945 );
9946
9947 $customer_result = $wpdb->get_row(
9948 $wpdb->prepare(
9949 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
9950 [ $result['customerId'] ]
9951 ),
9952 ARRAY_A
9953 );
9954
9955 $service_result = $wpdb->get_row(
9956 $wpdb->prepare(
9957 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
9958 [ $result['serviceId'] ]
9959 ),
9960 ARRAY_A
9961 );
9962
9963 $category_result = $wpdb->get_row(
9964 $wpdb->prepare(
9965 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
9966 [ $service_result['categoryId'] ]
9967 ),
9968 ARRAY_A
9969 );
9970
9971 if ( $result['couponId'] ) {
9972 $coupon_result = $wpdb->get_row(
9973 $wpdb->prepare(
9974 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9975 [ $result['couponId'] ]
9976 ),
9977 ARRAY_A
9978 );
9979 } else {
9980 $coupon_result = [];
9981 }
9982
9983 if ( ! empty( $result['customFields'] ) ) {
9984 $custom_fields = json_decode( $result['customFields'], true );
9985
9986 $fields_arr = [];
9987 foreach ( (array) $custom_fields as $fields ) {
9988 if ( is_array( $fields ) ) {
9989 $fields_arr[ $fields['label'] ] = $fields['value'];
9990 }
9991 }
9992 unset( $result['customFields'] );
9993 } else {
9994 $fields_arr = [];
9995 }
9996
9997 $appointment_data['isRescheduled'] = '1';
9998 $context['pluggable_data'] = array_merge( $result, $fields_arr, $appointment_data, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
9999 $context['response_type'] = 'live';
10000 } else {
10001
10002 $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 );
10003 }
10004
10005 return $context;
10006 }
10007
10008
10009 /**
10010 * Get last data for trigger.
10011 *
10012 * @param array $data data.
10013 * @return array
10014 */
10015 public function search_amelia_appointment_cancelled_triggers_last_data( $data ) {
10016 global $wpdb;
10017
10018 $context = [];
10019
10020 $appointment_category = $data['filter']['amelia_category_list']['value'];
10021 $appointment_service = $data['filter']['amelia_service_list']['value'];
10022
10023 if ( -1 === $appointment_service ) {
10024 // If service exists as per category selected.
10025 $service_exist = $wpdb->get_row(
10026 $wpdb->prepare(
10027 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
10028 [ $appointment_category ]
10029 ),
10030 ARRAY_A
10031 );
10032
10033 if ( empty( $service_exist ) ) {
10034 $result = [];
10035 } else {
10036 $result = $wpdb->get_row(
10037 $wpdb->prepare(
10038 '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',
10039 [ 'canceled' ]
10040 ),
10041 ARRAY_A
10042 );
10043 }
10044 } else {
10045 $result = $wpdb->get_row(
10046 $wpdb->prepare(
10047 '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',
10048 [ 'canceled', $appointment_service ]
10049 ),
10050 ARRAY_A
10051 );
10052 }
10053
10054 if ( ! empty( $result ) ) {
10055
10056 $payment_result = $wpdb->get_row(
10057 $wpdb->prepare(
10058 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
10059 [ $result['id'] ]
10060 ),
10061 ARRAY_A
10062 );
10063
10064 $customer_result = $wpdb->get_row(
10065 $wpdb->prepare(
10066 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
10067 [ $result['customerId'] ]
10068 ),
10069 ARRAY_A
10070 );
10071
10072 $service_result = $wpdb->get_row(
10073 $wpdb->prepare(
10074 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
10075 [ $result['serviceId'] ]
10076 ),
10077 ARRAY_A
10078 );
10079
10080 $category_result = $wpdb->get_row(
10081 $wpdb->prepare(
10082 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
10083 [ $service_result['categoryId'] ]
10084 ),
10085 ARRAY_A
10086 );
10087
10088 if ( $result['couponId'] ) {
10089 $coupon_result = $wpdb->get_row(
10090 $wpdb->prepare(
10091 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
10092 [ $result['couponId'] ]
10093 ),
10094 ARRAY_A
10095 );
10096 } else {
10097 $coupon_result = [];
10098 }
10099
10100 if ( ! empty( $result['customFields'] ) ) {
10101 $custom_fields = json_decode( $result['customFields'], true );
10102
10103 $fields_arr = [];
10104 foreach ( (array) $custom_fields as $fields ) {
10105 if ( is_array( $fields ) ) {
10106 $fields_arr[ $fields['label'] ] = $fields['value'];
10107 }
10108 }
10109 unset( $result['customFields'] );
10110 } else {
10111 $fields_arr = [];
10112 }
10113
10114 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
10115 $context['response_type'] = 'live';
10116 } else {
10117
10118 $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 );
10119 }
10120
10121 return $context;
10122 }
10123
10124 /**
10125 * Get MailPoet Forms.
10126 *
10127 * @param array $data data.
10128 *
10129 * @return array
10130 */
10131 public function search_mailpoet_forms( $data ) {
10132 if ( ! class_exists( '\MailPoet\API\API' ) ) {
10133 return [];
10134 }
10135
10136 global $wpdb;
10137
10138 $page = $data['page'];
10139 $limit = Utilities::get_search_page_limit();
10140 $offset = $limit * ( $page - 1 );
10141
10142 $forms = $wpdb->get_results(
10143 $wpdb->prepare(
10144 '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',
10145 [ 'enabled', $limit, $offset ]
10146 )
10147 );
10148
10149 $form_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
10150
10151 $options = [];
10152
10153 if ( ! empty( $forms ) ) {
10154 if ( is_array( $forms ) ) {
10155 foreach ( $forms as $form ) {
10156 $options[] = [
10157 'label' => $form->name,
10158 'value' => $form->id,
10159 ];
10160 }
10161 }
10162 }
10163
10164 return [
10165 'options' => $options,
10166 'hasMore' => $form_count > $limit && $form_count > $offset,
10167 ];
10168
10169 }
10170
10171 /**
10172 * Get MailPoet List.
10173 *
10174 * @param array $data data.
10175 *
10176 * @return array
10177 */
10178 public function search_mailpoet_list( $data ) {
10179 if ( ! class_exists( '\MailPoet\API\API' ) ) {
10180 return [];
10181 }
10182
10183 $mailpoet = \MailPoet\API\API::MP( 'v1' );
10184 $lists = $mailpoet->getLists();
10185
10186 $options = [];
10187
10188 if ( ! empty( $lists ) ) {
10189 if ( is_array( $lists ) ) {
10190 foreach ( $lists as $list ) {
10191 $options[] = [
10192 'label' => $list['name'],
10193 'value' => $list['id'],
10194 ];
10195 }
10196 }
10197 }
10198
10199 return [
10200 'options' => $options,
10201 'hasMore' => false,
10202 ];
10203 }
10204
10205 /**
10206 * Get MailPoet Subscriber Status.
10207 *
10208 * @param array $data data.
10209 *
10210 * @return array
10211 */
10212 public function search_mailpoet_subscriber_status( $data ) {
10213 if ( ! class_exists( '\MailPoet\API\API' ) ) {
10214 return [];
10215 }
10216
10217 $subscriber_status = [
10218 'subscribed' => 'Subscribed',
10219 'unconfirmed' => 'Unconfirmed',
10220 'unsubscribed' => 'Unsubscribed',
10221 'inactive' => 'Inactive',
10222 'bounced' => 'Bounced',
10223 ];
10224
10225 $options = [];
10226 foreach ( $subscriber_status as $key => $status ) {
10227 $options[] = [
10228 'label' => $status,
10229 'value' => $key,
10230 ];
10231 }
10232
10233 return [
10234 'options' => $options,
10235 'hasMore' => false,
10236 ];
10237 }
10238
10239 /**
10240 * Get MailPoet Subscribers.
10241 *
10242 * @param array $data data.
10243 *
10244 * @return array
10245 */
10246 public function search_mailpoet_subscribers( $data ) {
10247 if ( ! class_exists( '\MailPoet\API\API' ) ) {
10248 return [];
10249 }
10250
10251 global $wpdb;
10252
10253 $page = $data['page'];
10254 $limit = Utilities::get_search_page_limit();
10255 $offset = $limit * ( $page - 1 );
10256
10257 $subscribers = $wpdb->get_results(
10258 $wpdb->prepare(
10259 'SELECT SQL_CALC_FOUND_ROWS id,email FROM ' . $wpdb->prefix . 'mailpoet_subscribers ORDER BY id DESC LIMIT %d OFFSET %d',
10260 [ $limit, $offset ]
10261 )
10262 );
10263
10264 $subscribers_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
10265
10266 $options = [];
10267
10268 if ( ! empty( $subscribers ) ) {
10269 if ( is_array( $subscribers ) ) {
10270 foreach ( $subscribers as $subscriber ) {
10271 $options[] = [
10272 'label' => $subscriber->email,
10273 'value' => $subscriber->id,
10274 ];
10275 }
10276 }
10277 }
10278
10279 return [
10280 'options' => $options,
10281 'hasMore' => $subscribers_count > $limit && $subscribers_count > $offset,
10282 ];
10283 }
10284
10285 /**
10286 * Get ConvertPro Forms.
10287 *
10288 * @param array $data data.
10289 *
10290 * @return array
10291 */
10292 public function search_convertpro_form_list( $data ) {
10293 if ( ! class_exists( '\Cp_V2_Loader' ) || ! class_exists( 'CP_V2_Popups' ) ) {
10294 return [];
10295 }
10296
10297 $cp_popups_inst = CP_V2_Popups::get_instance();
10298 $popups = $cp_popups_inst->get_all();
10299
10300 $form_count = count( $popups );
10301
10302 $page = $data['page'];
10303 $limit = Utilities::get_search_page_limit();
10304 $offset = $limit * ( $page - 1 );
10305
10306 $options = [];
10307
10308 if ( ! empty( $popups ) ) {
10309 if ( is_array( $popups ) ) {
10310 foreach ( $popups as $form ) {
10311 $options[] = [
10312 'label' => $form->post_title,
10313 'value' => $form->ID,
10314 ];
10315 }
10316 }
10317 }
10318
10319 return [
10320 'options' => $options,
10321 'hasMore' => $form_count > $limit && $form_count > $offset,
10322 ];
10323
10324 }
10325
10326 /**
10327 * Get ProjectHuddle Websites.
10328 *
10329 * @param array $data data.
10330 *
10331 * @return array
10332 */
10333 public function search_project_huddle_websites( $data ) {
10334
10335 $sites = new WP_Query(
10336 [
10337 'post_type' => 'ph-website',
10338 'posts_per_page' => - 1,
10339 'fields' => 'ids',
10340 ]
10341 );
10342
10343 $site_ids = (array) $sites->posts;
10344
10345 $options = [];
10346 if ( ! empty( $site_ids ) ) {
10347 if ( is_array( $site_ids ) ) {
10348 foreach ( $site_ids as $site_id ) {
10349 $options[] = [
10350 'label' => get_the_title( $site_id ),
10351 'value' => $site_id,
10352 ];
10353 }
10354 }
10355 }
10356 return [
10357 'options' => $options,
10358 'hasMore' => false,
10359 ];
10360
10361 }
10362
10363 /**
10364 * Get last data for trigger.
10365 *
10366 * @param array $data data.
10367 * @return mixed
10368 */
10369 public function search_project_huddle_comment_triggers_last_data( $data ) {
10370 global $wpdb;
10371
10372 $context = [];
10373 if ( ! class_exists( 'PH\Models\Post' ) || ! function_exists( 'ph_get_the_title' ) ) {
10374 return [];
10375 }
10376
10377 if ( -1 !== $data['dynamic'] ) {
10378 $threads = get_posts(
10379 [
10380 'post_type' => 'phw_comment_loc',
10381 'posts_per_page' => 1,
10382 'meta_value' => $data['dynamic'],
10383 'meta_key' => 'project_id',
10384 'orderby' => 'asc',
10385 ]
10386 );
10387 } else {
10388 $threads = [];
10389 }
10390
10391 if ( ! empty( $threads ) ) {
10392 $comment_result = $wpdb->get_row(
10393 $wpdb->prepare(
10394 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
10395 FROM ' . $wpdb->prefix . 'comments
10396 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
10397 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
10398 LIMIT 0,1',
10399 $threads[0]->ID
10400 ),
10401 ARRAY_A
10402 );
10403
10404 if ( ! empty( $comment_result ) ) {
10405 $comment_id = get_comment( $comment_result['comment_ID'], ARRAY_A );
10406 $comments['comment_ID'] = isset( $comment_id['comment_ID'] ) ? $comment_id['comment_ID'] : '';
10407 $comments['comment_post_ID'] = isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : '';
10408
10409 $comments['comment_author'] = isset( $comment_id['comment_author'] ) ? $comment_id['comment_author'] : '';
10410
10411 $comments['comment_author_email'] = isset( $comment_id['comment_author_email'] ) ? $comment_id['comment_author_email'] : '';
10412
10413 $comments['comment_date'] = isset( $comment_id['comment_date'] ) ? $comment_id['comment_date'] : '';
10414
10415 $comments['comment_content'] = isset( $comment_id['comment_content'] ) ? $comment_id['comment_content'] : '';
10416
10417 $comments['comment_type'] = isset( $comment_id['comment_type'] ) ? $comment_id['comment_type'] : '';
10418
10419 $comment_item_id = get_comment_meta( $comment_result['comment_ID'], 'item_id' );
10420 if ( ! empty( $comment_item_id ) && is_array( $comment_item_id ) ) {
10421 $comments['comment_item_id'] = $comment_item_id[0];
10422 $comments['comment_item_page_title'] = get_the_title( (int) $comment_item_id[0] );
10423 $comments['comment_item_page_url'] = get_post_meta( (int) $comment_item_id[0], 'page_url', true );
10424 }
10425 if ( isset( $comment_id['comment_post_ID'] ) || isset( $comment_id['comment_author'] ) ) {
10426 $comments['ph_project_name'] = ph_get_the_title( Post::get( $comment_id['comment_post_ID'] )->parentsIds()['project'] );
10427 $comments['ph_commenter_name'] = $comment_id['comment_author'];
10428 $comments['ph_project_type'] = ( get_post_type( $comment_id['comment_post_ID'] ) == 'ph-website' ) ? __( 'Website', 'suretriggers' ) : __( 'Mockup', 'suretriggers' );
10429 $comments['ph_action_status'] = get_post_meta( $comment_id['comment_post_ID'], 'resolved', true ) ? __( 'Resolved', 'suretriggers' ) : __( 'Unresolved', 'suretriggers' );
10430 $comments['ph_project_link'] = get_the_guid( $comment_id['comment_post_ID'] );
10431 }
10432
10433 $context['pluggable_data'] = $comments;
10434 $context['response_type'] = 'live';
10435 } else {
10436 $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 );
10437 }
10438 } else {
10439 $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 );
10440 }
10441
10442 return $context;
10443 }
10444
10445 /**
10446 * Get last data for trigger.
10447 *
10448 * @param array $data data.
10449 * @return mixed
10450 */
10451 public function search_project_huddle_resolved_comment_triggers_last_data( $data ) {
10452 global $wpdb;
10453
10454 $context = [];
10455 if ( ! class_exists( 'PH\Models\Post' ) || ! function_exists( 'ph_get_the_title' ) ) {
10456 return [];
10457 }
10458
10459 $get_comments = $wpdb->get_row(
10460 'SELECT ' . $wpdb->prefix . 'comments.comment_ID, ' . $wpdb->prefix . 'comments.comment_content
10461 FROM ' . $wpdb->prefix . 'comments
10462 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_status") AND comment_content = "Resolved"
10463 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
10464 LIMIT 0,1'
10465 );
10466
10467 if ( ! empty( $get_comments ) ) {
10468 $comment_id = get_comment( $get_comments->comment_ID, ARRAY_A );
10469 $comment_result = $wpdb->get_row(
10470 $wpdb->prepare(
10471 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
10472 FROM ' . $wpdb->prefix . 'comments
10473 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
10474 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
10475 LIMIT 0,1',
10476 isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : ''
10477 ),
10478 ARRAY_A
10479 );
10480
10481 $actual_comment = get_comment( $comment_result['comment_ID'], ARRAY_A );
10482 $comments['comment_ID'] = isset( $actual_comment['comment_ID'] ) ? $actual_comment['comment_ID'] : '';
10483 $comments['comment_post_ID'] = isset( $actual_comment['comment_post_ID'] ) ? $actual_comment['comment_post_ID'] : '';
10484 $comments['comment_author'] = isset( $actual_comment['comment_author'] ) ? $actual_comment['comment_author'] : '';
10485 $comments['comment_author_email'] = isset( $actual_comment['comment_author_email'] ) ? $actual_comment['comment_author_email'] : '';
10486 $comments['comment_date'] = isset( $actual_comment['comment_date'] ) ? $actual_comment['comment_date'] : '';
10487 $comments['comment_content'] = isset( $actual_comment['comment_content'] ) ? $actual_comment['comment_content'] : '';
10488 $comments['comment_type'] = isset( $actual_comment['comment_type'] ) ? $actual_comment['comment_type'] : '';
10489 $comments['comment_status'] = $get_comments->comment_content;
10490 $comment_item_id = get_comment_meta( $comment_result['comment_ID'], 'item_id' );
10491 if ( ! empty( $comment_item_id ) && is_array( $comment_item_id ) ) {
10492 $comments['comment_item_id'] = $comment_item_id[0];
10493 $comments['comment_item_page_title'] = get_the_title( (int) $comment_item_id[0] );
10494 $comments['comment_item_page_url'] = get_post_meta( (int) $comment_item_id[0], 'page_url', true );
10495 }
10496 if ( isset( $comment_id['comment_post_ID'] ) || isset( $comment_id['comment_author'] ) ) {
10497 $comments['ph_project_name'] = ph_get_the_title( Post::get( $comment_id['comment_post_ID'] )->parentsIds()['project'] );
10498 $comments['ph_commenter_name'] = $comment_id['comment_author'];
10499 $comments['ph_project_type'] = ( get_post_type( $comment_id['comment_post_ID'] ) == 'ph-website' ) ? __( 'Website', 'suretriggers' ) : __( 'Mockup', 'suretriggers' );
10500 $comments['ph_action_status'] = get_post_meta( $comment_id['comment_post_ID'], 'resolved', true ) ? __( 'Resolved', 'suretriggers' ) : __( 'Unresolved', 'suretriggers' );
10501 $comments['ph_project_link'] = get_the_guid( $comment_id['comment_post_ID'] );
10502 }
10503 $context['pluggable_data'] = $comments;
10504 $context['response_type'] = 'live';
10505 } else {
10506 $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 );
10507 }
10508
10509 return $context;
10510 }
10511
10512 /**
10513 * Get MasterStudy LMS Courses.
10514 *
10515 * @param array $data data.
10516 *
10517 * @return array
10518 */
10519 public function search_ms_lms_courses( $data ) {
10520
10521 $page = $data['page'];
10522 $limit = Utilities::get_search_page_limit();
10523 $offset = $limit * ( $page - 1 );
10524
10525 $args = [
10526 'post_type' => 'stm-courses',
10527 'posts_per_page' => $limit,
10528 'offset' => $offset,
10529 'orderby' => 'title',
10530 'order' => 'ASC',
10531 'post_status' => 'publish',
10532 ];
10533
10534 $courses = get_posts( $args );
10535
10536 $course_count = wp_count_posts( 'stm-courses' )->publish;
10537
10538 $options = [];
10539 if ( ! empty( $courses ) ) {
10540 if ( is_array( $courses ) ) {
10541 foreach ( $courses as $course ) {
10542 $options[] = [
10543 'label' => $course->post_title,
10544 'value' => $course->ID,
10545 ];
10546 }
10547 }
10548 }
10549 return [
10550 'options' => $options,
10551 'hasMore' => $course_count > $limit && $course_count > $offset,
10552 ];
10553
10554 }
10555
10556 /**
10557 * Get MasterStudy LMS Lessons.
10558 *
10559 * @param array $data data.
10560 *
10561 * @return array
10562 */
10563 public function search_ms_lms_lessons( $data ) {
10564
10565 global $wpdb;
10566 $options = [];
10567 $course_id = $data['dynamic'];
10568
10569 // Use curriculum repository class.
10570 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
10571 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
10572 } else {
10573 $curriculum_repo = false;
10574 }
10575
10576 if ( '-1' === $course_id ) {
10577 $lessons = $wpdb->get_results(
10578 $wpdb->prepare(
10579 "SELECT SQL_CALC_FOUND_ROWS ID, post_title FROM $wpdb->posts WHERE post_type = %s ORDER BY post_title ASC",
10580 'stm-lessons'
10581 )
10582 );
10583
10584 if ( ! empty( $lessons ) ) {
10585 if ( is_array( $lessons ) ) {
10586 foreach ( $lessons as $lesson ) {
10587 $options[] = [
10588 'label' => $lesson->post_title,
10589 'value' => $lesson->ID,
10590 ];
10591 }
10592 }
10593 }
10594 } else {
10595 $lessons = [];
10596 if ( $curriculum_repo ) {
10597 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
10598 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
10599 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
10600 foreach ( $curriculum['materials'] as $material ) {
10601 if ( 'stm-lessons' === $material['post_type'] ) {
10602 $lessons[] = [
10603 'value' => $material['post_id'],
10604 'text' => $material['title'],
10605 ];
10606 }
10607 }
10608 }
10609 }
10610 if ( ! empty( $lessons ) ) {
10611 if ( is_array( $lessons ) ) {
10612 foreach ( $lessons as $lesson ) {
10613 $options[] = [
10614 'label' => $lesson['text'],
10615 'value' => $lesson['value'],
10616 ];
10617 }
10618 }
10619 }
10620 } else {
10621 $lessons = $wpdb->get_results(
10622 $wpdb->prepare(
10623 "SELECT SQL_CALC_FOUND_ROWS ID, post_title
10624 FROM $wpdb->posts
10625 WHERE FIND_IN_SET(
10626 ID,
10627 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
10628 )
10629 AND post_type = 'stm-lessons'
10630 ORDER BY post_title ASC",
10631 absint( $course_id )
10632 )
10633 );
10634 if ( ! empty( $lessons ) ) {
10635 if ( is_array( $lessons ) ) {
10636 foreach ( $lessons as $lesson ) {
10637 $options[] = [
10638 'label' => $lesson->post_title,
10639 'value' => $lesson->ID,
10640 ];
10641 }
10642 }
10643 }
10644 }
10645 }
10646 return [
10647 'options' => $options,
10648 'hasMore' => false,
10649 ];
10650
10651 }
10652
10653 /**
10654 * Get MasterStudy LMS Quiz.
10655 *
10656 * @param array $data data.
10657 *
10658 * @return array
10659 */
10660 public function search_ms_lms_quiz( $data ) {
10661
10662 global $wpdb;
10663
10664 $options = [];
10665 $course_id = $data['dynamic'];
10666
10667 // Use curriculum repository class.
10668 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
10669 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
10670 } else {
10671 $curriculum_repo = false;
10672 }
10673
10674 if ( '-1' === $course_id ) {
10675 $quizzes = $wpdb->get_results(
10676 $wpdb->prepare(
10677 "SELECT ID, post_title
10678 FROM $wpdb->posts
10679 WHERE post_type = %s
10680 ORDER BY post_title ASC",
10681 'stm-quizzes'
10682 )
10683 );
10684 if ( ! empty( $quizzes ) ) {
10685 if ( is_array( $quizzes ) ) {
10686 foreach ( $quizzes as $quiz ) {
10687 $options[] = [
10688 'label' => $quiz->post_title,
10689 'value' => $quiz->ID,
10690 ];
10691 }
10692 }
10693 }
10694 } else {
10695 $quizzes = [];
10696 if ( $curriculum_repo ) {
10697 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
10698 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
10699 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
10700 foreach ( $curriculum['materials'] as $material ) {
10701 if ( 'stm-quizzes' === $material['post_type'] ) {
10702 $quizzes[] = [
10703 'value' => $material['post_id'],
10704 'text' => $material['title'],
10705 ];
10706 }
10707 }
10708 }
10709 }
10710 if ( ! empty( $quizzes ) ) {
10711 if ( is_array( $quizzes ) ) {
10712 foreach ( $quizzes as $quiz ) {
10713 $options[] = [
10714 'label' => $quiz['text'],
10715 'value' => $quiz['value'],
10716 ];
10717 }
10718 }
10719 }
10720 } else {
10721 $quizzes = $wpdb->get_results(
10722 $wpdb->prepare(
10723 "SELECT ID, post_title
10724 FROM $wpdb->posts
10725 WHERE FIND_IN_SET(
10726 ID,
10727 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
10728 )
10729 AND post_type = 'stm-quizzes'
10730 ORDER BY post_title ASC
10731 ",
10732 absint( $course_id )
10733 )
10734 );
10735 if ( ! empty( $quizzes ) ) {
10736 if ( is_array( $quizzes ) ) {
10737 foreach ( $quizzes as $quiz ) {
10738 $options[] = [
10739 'label' => $quiz->post_title,
10740 'value' => $quiz->ID,
10741 ];
10742 }
10743 }
10744 }
10745 }
10746 }
10747 return [
10748 'options' => $options,
10749 'hasMore' => false,
10750 ];
10751
10752 }
10753
10754 /**
10755 * Search MasterStudy LMS data.
10756 *
10757 * @param array $data data.
10758 * @return array|void
10759 */
10760 public function search_ms_lms_last_data( $data ) {
10761 global $wpdb;
10762 $post_type = $data['post_type'];
10763 $trigger = $data['search_term'];
10764 $context = [];
10765
10766 if ( 'stm_lms_course_completed' === $trigger ) {
10767 $post_id = $data['filter']['course']['value'];
10768 if ( -1 === $post_id ) {
10769 $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 ) );
10770 } else {
10771 $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 ) );
10772 }
10773 } elseif ( 'stm_lesson_passed' === $trigger ) {
10774 $post_id = $data['filter']['lesson']['value'];
10775 if ( -1 === $post_id ) {
10776 $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 ) );
10777 } else {
10778 $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 ) );
10779 }
10780 } elseif ( 'stm_quiz_passed' === $trigger ) {
10781 $post_id = $data['filter']['quiz']['value'];
10782 if ( -1 === $post_id ) {
10783 $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 ) );
10784 } else {
10785 $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 ) );
10786 }
10787 } elseif ( 'stm_quiz_failed' === $trigger ) {
10788 $post_id = $data['filter']['quiz']['value'];
10789 if ( -1 === $post_id ) {
10790 $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 ) );
10791 } else {
10792 $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 ) );
10793 }
10794 } elseif ( 'stm_lms_user_enroll_course' === $trigger ) {
10795 $post_id = $data['filter']['course']['value'];
10796 if ( -1 === $post_id ) {
10797 $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 ) );
10798 } else {
10799 $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 ) );
10800 }
10801 }
10802
10803 if ( ! empty( $result ) ) {
10804
10805 switch ( $trigger ) {
10806 case 'stm_lms_course_completed':
10807 $result_course_id = $result[0]->course_id;
10808 $result_user_id = $result[0]->user_id;
10809 $course = get_the_title( $result_course_id );
10810 $course_link = get_the_permalink( $result_course_id );
10811 $featured_image = get_the_post_thumbnail_url( $result_course_id );
10812
10813 $data = [
10814 'course_id' => $result_course_id,
10815 'course_title' => $course,
10816 'course_link' => $course_link,
10817 'course_featured_image' => $featured_image,
10818 'course_progress' => $result[0]->progress_percent,
10819 ];
10820 $context_data = array_merge(
10821 WordPress::get_user_context( $result_user_id ),
10822 $data
10823 );
10824 break;
10825 case 'stm_lesson_passed':
10826 $result_lesson_id = $result[0]->lesson_id;
10827 $result_user_id = $result[0]->user_id;
10828 $lesson = get_the_title( $result_lesson_id );
10829 $lesson_link = get_the_permalink( $result_lesson_id );
10830
10831 $data = [
10832 'lesson_id' => $result_lesson_id,
10833 'lesson_title' => $lesson,
10834 'lesson_link' => $lesson_link,
10835 ];
10836 $context_data = array_merge(
10837 WordPress::get_user_context( $result_user_id ),
10838 $data
10839 );
10840 break;
10841 case 'stm_quiz_passed':
10842 $result_quiz_id = $result[0]->quiz_id;
10843 $result_user_id = $result[0]->user_id;
10844 $quiz_title = get_the_title( $result_quiz_id );
10845 $quiz_link = get_the_permalink( $result_quiz_id );
10846
10847 $data = [
10848 'quiz_id' => $result_quiz_id,
10849 'quiz_title' => $quiz_title,
10850 'quiz_link' => $quiz_link,
10851 'quiz_score' => $result[0]->progress,
10852 'result' => 'passed',
10853 ];
10854 $context_data = array_merge(
10855 WordPress::get_user_context( $result_user_id ),
10856 $data
10857 );
10858 break;
10859 case 'stm_quiz_failed':
10860 $result_quiz_id = $result[0]->quiz_id;
10861 $result_user_id = $result[0]->user_id;
10862 $quiz_title = get_the_title( $result_quiz_id );
10863 $quiz_link = get_the_permalink( $result_quiz_id );
10864
10865 $data = [
10866 'quiz_id' => $result_quiz_id,
10867 'quiz_title' => $quiz_title,
10868 'quiz_link' => $quiz_link,
10869 'quiz_score' => $result[0]->progress,
10870 'result' => 'failed',
10871 ];
10872 $context_data = array_merge(
10873 WordPress::get_user_context( $result_user_id ),
10874 $data
10875 );
10876 break;
10877 case 'stm_lms_user_enroll_course':
10878 $result_course_id = $result[0]->course_id;
10879 $result_user_id = $result[0]->user_id;
10880
10881 $course = get_the_title( $result_course_id );
10882 $course_link = get_the_permalink( $result_course_id );
10883 $featured_image = get_the_post_thumbnail_url( $result_course_id );
10884
10885 $data = [
10886 'course_id' => $result_course_id,
10887 'course_title' => $course,
10888 'course_link' => $course_link,
10889 'course_featured_image' => $featured_image,
10890 ];
10891 $context_data = array_merge(
10892 WordPress::get_user_context( $result_user_id ),
10893 $data
10894 );
10895 break;
10896 default:
10897 return;
10898 }
10899 $context['pluggable_data'] = $context_data;
10900 $context['response_type'] = 'live';
10901 }
10902
10903 return $context;
10904
10905 }
10906
10907 /**
10908 * Prepare Fluent Support Mailbox list.
10909 *
10910 * @param array $data data.
10911 *
10912 * @return array
10913 */
10914 public function search_fs_mailbox_list( $data ) {
10915 $options = [];
10916 global $wpdb;
10917 $mailboxes = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_mail_boxes ORDER BY id DESC", ARRAY_A );
10918 if ( ! empty( $mailboxes ) ) {
10919 foreach ( $mailboxes as $key => $value ) {
10920
10921 $options[] = [
10922 'label' => $value['name'],
10923 'value' => $value['id'],
10924 ];
10925
10926 }
10927 }
10928
10929 return [
10930 'options' => $options,
10931 'hasMore' => false,
10932 ];
10933 }
10934
10935 /**
10936 * Prepare Fluent Support Product list.
10937 *
10938 * @param array $data data.
10939 *
10940 * @return array
10941 */
10942 public function search_fs_product_list( $data ) {
10943 $options = [];
10944
10945 if ( ! class_exists( '\FluentSupport\App\Models\Product' ) ) {
10946 return [];
10947 }
10948
10949 $products = \FluentSupport\App\Models\Product::select( [ 'id', 'title' ] )->get();
10950 if ( ! empty( $products ) ) {
10951 foreach ( $products as $key => $product ) {
10952 $options[] = [
10953 'label' => $product['title'],
10954 'value' => $product['id'],
10955 ];
10956 }
10957 }
10958
10959 return [
10960 'options' => $options,
10961 'hasMore' => false,
10962 ];
10963 }
10964
10965 /**
10966 * Prepare Fluent Support Product list.
10967 *
10968 * @param array $data data.
10969 *
10970 * @return array
10971 */
10972 public function search_fs_person_type_list( $data ) {
10973 $options = [
10974 [
10975 'label' => 'Customer',
10976 'value' => 'customer',
10977 ],
10978 [
10979 'label' => 'Agent',
10980 'value' => 'agent',
10981 ],
10982 ];
10983
10984 return [
10985 'options' => $options,
10986 'hasMore' => false,
10987 ];
10988 }
10989
10990 /**
10991 * Prepare FluentSupport Agents List.
10992 *
10993 * @param array $data Search Params.
10994 * @return array
10995 */
10996 public function search_fluent_support_agents_list( $data ) {
10997 global $wpdb;
10998
10999 $agents = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent' ", ARRAY_A );
11000
11001 $options = [];
11002
11003 if ( ! empty( $agents ) ) {
11004 foreach ( $agents as $agent ) {
11005 $options[] = [
11006 'label' => $agent['email'],
11007 'value' => $agent['id'],
11008 ];
11009 }
11010 }
11011
11012 return [
11013 'options' => $options,
11014 'hasMore' => false,
11015 ];
11016 }
11017
11018 /**
11019 * Get last data for trigger.
11020 *
11021 * @param array $data data.
11022 * @return array
11023 */
11024 public function search_fluent_support_triggers_last_data( $data ) {
11025 global $wpdb;
11026 $context = [];
11027 $context['response_type'] = 'sample';
11028
11029 if ( ! class_exists( '\FluentSupport\App\Models\Ticket' ) ) {
11030 return [];
11031 }
11032
11033 $ticket_data = [
11034 'id' => '1',
11035 'customer_id' => '2',
11036 'agent_id' => '3',
11037 'product_id' => '5',
11038 'product_source' => 'local',
11039 'privacy' => 'private',
11040 'priority' => 'normal',
11041 'client_priority' => 'medium',
11042 'status' => 'active',
11043 'title' => 'Sample Ticket Title',
11044 'slug' => 'sample-ticket-title',
11045 'hash' => 'f8a8cfb946',
11046 'content_hash' => 'd65500d62621be8b493c22b1d888052c',
11047 'content' => '<p>Sample content.</p>',
11048 'last_customer_response' => '2023-04-27 07:30:46',
11049 'waiting_since' => '2023-04-27 07:30:46',
11050 'response_count' => '2',
11051 'total_close_time' => '7042',
11052 'resolved at' => '2023-04-27 09:28:08',
11053 'closed_by' => '1',
11054 'created_at' => '2023-04-27 07:30:46',
11055 'updated_at' => '2023-04-27 10:28:08',
11056 'mailbox_id' => '1',
11057 'mailbox_name' => 'OttoKit',
11058 'mailbox_slug' => 'suretriggers',
11059 'mailbox_box_type' => 'web',
11060 'mailbox email' => 'john_doe@sample.com',
11061 'mailbox_settings_admin_email_address' => 'john_doe@sample.com',
11062 'mailbox_created_by' => '1',
11063 'mailbox_is_default' => 'yes',
11064 'mailbox_created_at' => '2023-04-26 06:29:01',
11065 'mailbox_updated_at' => '2023-04-26 06:29:01',
11066 ];
11067
11068 $customer_data = [
11069 'id' => '1',
11070 'first_name' => 'John',
11071 'last_name' => 'Doe',
11072 'email' => 'john_doe@sample.com',
11073 'person_type' => 'agent',
11074 'status' => 'active',
11075 'hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
11076 'user_id' => '1',
11077 'created_at' => '2023-04-27 07:30:46',
11078 'updated_at' => '2023-04-27 10:28:08',
11079 'full_name' => 'John Doe',
11080 'photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
11081 ];
11082
11083 $reply_data = [
11084 'ticket_id' => '1',
11085 'conversation_type' => 'response',
11086 'content' => '<p>Sample content.</p>',
11087 'source' => 'web',
11088 'content_hash' => '2cc0e35d8fb92a0675d67999b073b3a4',
11089 'created_at' => '2023-04-27 07:30:46',
11090 'updated_at' => '2023-04-27 10:28:08',
11091 'id' => '1',
11092 'person_id' => '2',
11093 'person_first_name' => 'John',
11094 'person_last_name' => 'Doe',
11095 'person_email' => 'john_doe@sample.com',
11096 'person_person_type' => 'agent',
11097 'person_status' => 'active',
11098 'person_hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
11099 'person_user_id' => '1',
11100 'person_created_at' => '2023-04-27 07:30:46',
11101 'person_updated_at' => '2023-04-27 10:28:08',
11102 'person_full_name' => 'John Doe',
11103 'person_photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
11104 ];
11105
11106 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
11107 $mailbox = isset( $data['mailbox_id'] ) ? $data['mailbox_id'] : '';
11108 $product_id = isset( $data['filter']['ticket_product_id']['value'] ) ? $data['filter']['ticket_product_id']['value'] : '';
11109 $person_id = isset( $data['filter']['person_id']['value'] ) ? (string) $data['filter']['person_id']['value'] : '';
11110
11111 if ( in_array( $term, [ 'response_added_by_agent', 'response_added_by_customer', 'ticket_replied_product_agent', 'ticket_replied_product_customer' ], true ) ) {
11112 if ( 'response_added_by_agent' == $term ) {
11113 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
11114 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
11115 $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 ) );
11116 if ( ! empty( $tickets ) ) {
11117 $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 ) );
11118 $ticket_data = $tickets[0];
11119 $customer_data = $customers[0];
11120 $reply_data = $replies[0];
11121 $context['response_type'] = 'live';
11122 }
11123 } elseif ( 'response_added_by_customer' == $term ) {
11124 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
11125 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
11126 $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 ) );
11127 if ( ! empty( $tickets ) ) {
11128 $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 ) );
11129 $ticket_data = $tickets[0];
11130 $customer_data = $customers[0];
11131 $reply_data = $replies[0];
11132 $context['response_type'] = 'live';
11133 }
11134 } elseif ( 'ticket_replied_product_agent' == $term ) {
11135 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
11136 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
11137 if ( -1 == $product_id ) {
11138 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
11139 $products = array_map(
11140 function( $product ) {
11141 return $product['id'];
11142 },
11143 $products
11144 );
11145 $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 ) ) );
11146 } else {
11147 $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 ) );
11148 }
11149 if ( ! empty( $tickets ) ) {
11150 $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 ) );
11151 $ticket_data = $tickets[0];
11152 $customer_data = $customers[0];
11153 $reply_data = $replies[0];
11154 $context['response_type'] = 'live';
11155 }
11156 } elseif ( 'ticket_replied_product_customer' == $term ) {
11157 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
11158 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
11159 if ( -1 == $product_id ) {
11160 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
11161 $products = array_map(
11162 function( $product ) {
11163 return $product['id'];
11164 },
11165 $products
11166 );
11167 $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 ) ) );
11168 } else {
11169 $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 ) );
11170 }
11171 if ( ! empty( $tickets ) ) {
11172 $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 ) );
11173 $ticket_data = $tickets[0];
11174 $customer_data = $customers[0];
11175 $reply_data = $replies[0];
11176 $context['response_type'] = 'live';
11177 }
11178 }
11179 $context['pluggable_data'] = array_merge(
11180 [
11181 'reply' => $reply_data,
11182 'ticket' => $ticket_data,
11183 'customer' => $customer_data,
11184 ]
11185 );
11186 if ( 'ticket_replied_product_customer' == $term || 'ticket_replied_product_agent' == $term ) {
11187 $context['pluggable_data']['ticket_product_id'] = $product_id;
11188 }
11189 if ( is_object( $ticket_data ) ) {
11190 $ticket_data = get_object_vars( $ticket_data );
11191 }
11192 $ticket = \FluentSupport\App\Models\Ticket::find( $ticket_data['id'] );
11193 if ( is_object( $ticket ) && method_exists( $ticket, 'customData' ) ) {
11194 $context['pluggable_data']['custom_fields'] = $ticket->customData();
11195 }
11196 $context['pluggable_data']['ticket_link'] = admin_url( "admin.php?page=fluent-support#/tickets/{$ticket_data['id']}/view" );
11197 } else {
11198 if ( 'ticket_created' == $term ) {
11199 if ( -1 == $mailbox ) {
11200 $tickets = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE status = 'new' ORDER BY id DESC LIMIT 1" );
11201 } else {
11202 $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 ) );
11203 }
11204 if ( ! empty( $tickets ) ) {
11205 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d", $tickets[0]->customer_id ) );
11206 $ticket_data = $tickets[0];
11207 $customer_data = $customers[0];
11208 }
11209 } elseif ( 'ticket_closed_by_customer' == $term ) {
11210 $customers_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
11211 $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] ) );
11212 if ( ! empty( $tickets ) ) {
11213 $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 ) );
11214 $ticket_data = $tickets[0];
11215 $customer_data = $customers[0];
11216 }
11217 } elseif ( 'ticket_closed_by_agent' == $term ) {
11218 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
11219 $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] ) );
11220 if ( ! empty( $tickets ) ) {
11221 $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 ) );
11222 $ticket_data = $tickets[0];
11223 $customer_data = $customers[0];
11224 }
11225 } elseif ( 'ticket_created_product' == $term ) {
11226 $agents_type = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_persons" );
11227 if ( -1 == $product_id ) {
11228 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
11229 $products = array_map(
11230 function( $product ) {
11231 return $product['id'];
11232 },
11233 $products
11234 );
11235 $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 ) ) );
11236 } else {
11237 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id = %d ORDER BY id DESC LIMIT 1", $product_id ) );
11238 }
11239 if ( ! empty( $tickets ) ) {
11240 $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 ) );
11241 $ticket_data = $tickets[0];
11242 $customer_data = $customers[0];
11243 }
11244 } elseif ( 'ticket_closed_product' == $term ) {
11245 $agents_type = $wpdb->get_results( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = %s", $person_id ), ARRAY_A );
11246 $agents = array_map(
11247 function( $agent ) {
11248 return $agent['id'];
11249 },
11250 $agents_type
11251 );
11252 if ( -1 == $product_id ) {
11253 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
11254 $products = array_map(
11255 function( $product ) {
11256 return $product['id'];
11257 },
11258 $products
11259 );
11260 $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 ) ) );
11261 } else {
11262 $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 ) ) );
11263 }
11264 if ( ! empty( $tickets ) ) {
11265 $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 ) );
11266 $ticket_data = $tickets[0];
11267 $customer_data = $customers[0];
11268 }
11269 }
11270 $context['pluggable_data'] = array_merge(
11271 [
11272 'ticket' => $ticket_data,
11273 'customer' => $customer_data,
11274 ]
11275 );
11276 if ( 'ticket_created' == $term ) {
11277 $context['pluggable_data']['mailbox_id'] = $mailbox;
11278 } elseif ( 'ticket_created_product' == $term || 'ticket_replied_product_customer' == $term || 'ticket_replied_product_agent' == $term ) {
11279 $context['pluggable_data']['ticket_product_id'] = $product_id;
11280 } elseif ( 'ticket_closed_product' == $term ) {
11281 $context['pluggable_data']['ticket_product_id'] = $product_id;
11282 $context['pluggable_data']['person_id'] = $person_id;
11283 }
11284 if ( is_object( $ticket_data ) ) {
11285 $ticket_data = get_object_vars( $ticket_data );
11286 }
11287 $ticket = \FluentSupport\App\Models\Ticket::find( $ticket_data['id'] );
11288 if ( is_object( $ticket ) && method_exists( $ticket, 'customData' ) ) {
11289 $context['pluggable_data']['custom_fields'] = $ticket->customData();
11290 }
11291 $context['pluggable_data']['ticket_link'] = admin_url( "admin.php?page=fluent-support#/tickets/{$ticket_data['id']}/view" );
11292 $context['response_type'] = 'live';
11293 }
11294
11295 return $context;
11296 }
11297
11298 /**
11299 * Prepare Ultimate Member user_roles.
11300 *
11301 * @param array $data data.
11302 *
11303 * @return array
11304 */
11305 public function search_um_user_roles( $data ) {
11306 if ( function_exists( 'get_editable_roles' ) ) {
11307 $roles = get_editable_roles();
11308 } else {
11309 $roles = wp_roles()->roles;
11310 $roles = apply_filters( 'editable_roles', $roles );
11311 }
11312
11313 $options = [];
11314 foreach ( $roles as $role => $details ) {
11315
11316 $options[] = [
11317 'label' => $details['name'],
11318 'value' => $role,
11319 ];
11320
11321 }
11322
11323 return [
11324 'options' => $options,
11325 'hasMore' => false,
11326 ];
11327 }
11328
11329 /**
11330 * Prepare Ultimate Member forms_list.
11331 *
11332 * @param array $data data.
11333 *
11334 * @return array
11335 */
11336 public function search_um_forms_list( $data ) {
11337
11338 $page = $data['page'];
11339 $limit = Utilities::get_search_page_limit();
11340 $offset = $limit * ( $page - 1 );
11341
11342 $args = [
11343 'posts_per_page' => $limit,
11344 'offset' => $offset,
11345 'orderby' => 'title',
11346 'order' => 'ASC',
11347 'post_type' => 'um_form',
11348 'post_status' => 'publish',
11349 'fields' => 'ids',
11350 ];
11351 if ( 'register' == $data['filter']['type'] ) {
11352 $args['meta_query'] = [
11353 [
11354 'key' => '_um_mode',
11355 'value' => 'register',
11356 'compare' => 'LIKE',
11357 ],
11358 ];
11359 } elseif ( 'login' == $data['filter']['type'] ) {
11360 $args['meta_query'] = [
11361 [
11362 'key' => '_um_mode',
11363 'value' => 'login',
11364 'compare' => 'LIKE',
11365 ],
11366 ];
11367 }
11368
11369 $forms_list = get_posts( $args );
11370
11371 $forms_list_count = wp_count_posts( 'um_form' )->publish;
11372
11373 $options = [];
11374 if ( ! empty( $forms_list ) ) {
11375 foreach ( $forms_list as $form ) {
11376 $title = html_entity_decode( get_the_title( $form ), ENT_QUOTES, 'UTF-8' );
11377 $options[] = [
11378 'label' => $title,
11379 'value' => $form,
11380 ];
11381 }
11382 }
11383
11384 return [
11385 'options' => $options,
11386 'hasMore' => $forms_list_count > $limit && $forms_list_count > $offset,
11387 ];
11388 }
11389
11390 /**
11391 * Get last data for Ultimate Member Login trigger.
11392 *
11393 * @param array $data data.
11394 * @return mixed
11395 */
11396 public function search_ultimate_member_user_logsin( $data ) {
11397 $context = [];
11398 $args = [
11399 'orderby' => 'meta_value',
11400 'meta_key' => '_um_last_login',
11401 'order' => 'DESC',
11402 'number' => 1,
11403 ];
11404 $users = get_users( $args );
11405
11406 if ( ! empty( $users ) ) {
11407 $user = $users[0];
11408 $submitted_data = get_user_meta( $user->ID, 'submitted', true );
11409 if ( is_array( $submitted_data ) ) {
11410 unset(
11411 $submitted_data['user_password'],
11412 $submitted_data['confirm_user_password']
11413 );
11414 }
11415 $pluggable_data = WordPress::get_user_context( $user->ID );
11416 $pluggable_data['data'] = $submitted_data;
11417 $context['pluggable_data'] = $pluggable_data;
11418 $context['response_type'] = 'live';
11419 } else {
11420 $role = 'subscriber';
11421 $context['pluggable_data'] = [
11422 'wp_user_id' => 1,
11423 'user_login' => 'test',
11424 'display_name' => 'Test User',
11425 'user_firstname' => 'Test',
11426 'user_lastname' => 'User',
11427 'user_email' => 'testuser@gmail.com',
11428 'user_role' => [ $role ],
11429 ];
11430 $context['response_type'] = 'sample';
11431 }
11432 return $context;
11433 }
11434
11435 /**
11436 * Get last data for Ultimate Member Register trigger.
11437 *
11438 * @param array $data data.
11439 * @return mixed
11440 */
11441 public function search_ultimate_member_user_registers( $data ) {
11442 $context = [];
11443 $args = [
11444 'orderby' => 'meta_value',
11445 'meta_key' => 'um_user_profile_url_slug_user_login',
11446 'order' => 'DESC',
11447 'number' => 1,
11448 ];
11449 $users = get_users( $args );
11450
11451 if ( ! empty( $users ) ) {
11452 $user = $users[0];
11453 $submitted_data = get_user_meta( $user->ID, 'submitted', true );
11454 if ( is_array( $submitted_data ) ) {
11455 unset(
11456 $submitted_data['user_password'],
11457 $submitted_data['confirm_user_password']
11458 );
11459 }
11460 $pluggable_data = WordPress::get_user_context( $user->ID );
11461 $pluggable_data['data'] = $submitted_data;
11462 $context['pluggable_data'] = $pluggable_data;
11463 $context['response_type'] = 'live';
11464 } else {
11465 $role = 'subscriber';
11466 $context['pluggable_data'] = [
11467 'wp_user_id' => 1,
11468 'user_login' => 'test',
11469 'display_name' => 'Test User',
11470 'user_firstname' => 'Test',
11471 'user_lastname' => 'User',
11472 'user_email' => 'testuser@gmail.com',
11473 'user_role' => [ $role ],
11474 ];
11475 $context['response_type'] = 'sample';
11476 }
11477 return $context;
11478 }
11479
11480 /**
11481 * Get last data for Ultimate Member Register trigger.
11482 *
11483 * @param array $data data.
11484 * @return mixed
11485 */
11486 public function search_ultimate_member_user_inactive( $data ) {
11487 $context = [];
11488 $args = [
11489 'orderby' => 'user_id',
11490 'meta_key' => 'account_status',
11491 'meta_value' => 'inactive',
11492 'order' => 'ASC',
11493 'number' => 1,
11494 ];
11495 $users = get_users( $args );
11496
11497 if ( ! empty( $users ) ) {
11498 $user = $users[0];
11499 $pluggable_data = [];
11500 $pluggable_data[] = WordPress::get_user_context( $user->ID );
11501 $pluggable_data['user_account_status'] = 'inactive';
11502 $context['pluggable_data'] = $pluggable_data;
11503 $context['response_type'] = 'live';
11504 } else {
11505 $role = 'subscriber';
11506 $context['pluggable_data'] = [
11507 'wp_user_id' => 1,
11508 'user_login' => 'test',
11509 'display_name' => 'Test User',
11510 'user_firstname' => 'Test',
11511 'user_lastname' => 'User',
11512 'user_email' => 'testuser@gmail.com',
11513 'user_role' => [ $role ],
11514 'user_account_status' => 'inactive',
11515 ];
11516 $context['response_type'] = 'sample';
11517 }
11518 return $context;
11519 }
11520
11521 /**
11522 * Get last data for Ultimate Member Change Role trigger.
11523 *
11524 * @param array $data data.
11525 * @return mixed
11526 */
11527 public function search_ultimate_member_user_role_change( $data ) {
11528 $context = [];
11529
11530 $role = $data['filter']['role']['value'];
11531
11532 $args = [
11533 'number' => 1,
11534 'role' => $role,
11535 ];
11536 $users = get_users( $args );
11537 shuffle( $users );
11538 if ( ! empty( $users ) ) {
11539 $user = $users[0];
11540 $pluggable_data = WordPress::get_user_context( $user->ID );
11541 $context['pluggable_data'] = $pluggable_data;
11542 $context['response_type'] = 'live';
11543 } else {
11544 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
11545 $context['pluggable_data'] = [
11546 'wp_user_id' => 1,
11547 'user_login' => 'test',
11548 'display_name' => 'Test User',
11549 'user_firstname' => 'Test',
11550 'user_lastname' => 'User',
11551 'user_email' => 'testuser@gmail.com',
11552 'user_role' => [ $role ],
11553 'user_account_status' => 'inactive',
11554 ];
11555 $context['response_type'] = 'sample';
11556 }
11557 return $context;
11558 }
11559
11560 /**
11561 * Get JetEngine WP Posttypes.
11562 *
11563 * @param array $data data.
11564 *
11565 * @return array
11566 */
11567 public function search_je_posttype_list( $data ) {
11568
11569 $post_types = get_post_types( [ 'public' => true ], 'objects' );
11570 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
11571 if ( isset( $post_types['attachment'] ) ) {
11572 unset( $post_types['attachment'] );
11573 }
11574
11575 $options = [];
11576 foreach ( $post_types as $post_type => $details ) {
11577 $options[] = [
11578 'label' => $details->label,
11579 'value' => $post_type,
11580 ];
11581 }
11582
11583 return [
11584 'options' => $options,
11585 'hasMore' => false,
11586 ];
11587 }
11588
11589 /**
11590 * Get JetEngine WP fields.
11591 *
11592 * @param array $data data.
11593 *
11594 * @return array
11595 */
11596 public function search_je_field_list( $data ) {
11597
11598 $post_type = $data['dynamic'];
11599
11600 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
11601
11602 $post_fields = array_filter(
11603 $metaboxes,
11604 function( $metabox ) {
11605 /**
11606 *
11607 * Ignore line
11608 *
11609 * @phpstan-ignore-next-line
11610 */
11611 return 'post' === $metabox['args']['object_type'];
11612 }
11613 );
11614
11615 $post_fields_count = count( $post_fields );
11616
11617 $options = [];
11618 if ( ! empty( $post_fields ) ) {
11619 if ( is_array( $post_fields ) ) {
11620 foreach ( $post_fields as $post_field ) {
11621 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
11622 foreach ( $post_field['meta_fields'] as $meta_field ) {
11623 $options[] = [
11624 'label' => $meta_field['title'],
11625 'value' => $meta_field['name'],
11626 ];
11627 }
11628 }
11629 }
11630 }
11631 }
11632 return [
11633 'options' => $options,
11634 'hasMore' => false,
11635 ];
11636 }
11637
11638 /**
11639 * Search Last Updated Field Data.
11640 *
11641 * @param array $data data.
11642 * @return array
11643 */
11644 public function search_jet_engine_field_data( $data ) {
11645 global $wpdb;
11646
11647 $context = [];
11648
11649 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
11650
11651 $post_type = $data['filter']['wp_post_type']['value'];
11652
11653 if ( -1 === $field ) {
11654 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
11655
11656 $post_fields = array_filter(
11657 $metaboxes,
11658 function( $metabox ) {
11659 /**
11660 *
11661 * Ignore line
11662 *
11663 * @phpstan-ignore-next-line
11664 */
11665 return 'post' === $metabox['args']['object_type'];
11666 }
11667 );
11668
11669 $options = [];
11670 if ( ! empty( $post_fields ) ) {
11671 if ( is_array( $post_fields ) ) {
11672 foreach ( $post_fields as $post_field ) {
11673 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
11674 foreach ( $post_field['meta_fields'] as $meta_field ) {
11675 $options[] = $meta_field['name'];
11676 }
11677 }
11678 }
11679 }
11680 }
11681 $random_key = array_rand( $options );
11682 $random_value = $options[ $random_key ];
11683 $string = '%' . $random_value . '%';
11684 } else {
11685 $string = '%' . $data['filter']['field_id']['value'] . '%';
11686 }
11687
11688 $result = $wpdb->get_results(
11689 $wpdb->prepare(
11690 'SELECT post_id FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key LIKE %s',
11691 [ $string ]
11692 ),
11693 ARRAY_A
11694 );
11695
11696 $ids = [];
11697
11698 if ( ! empty( $result ) ) {
11699 foreach ( $result as $val ) {
11700 $ids[] = $val['post_id'];
11701 }
11702 }
11703
11704 $lastupdated_args = [
11705 'post_type' => $post_type,
11706 'orderby' => 'modified',
11707 'post__in' => $ids,
11708 'posts_per_page' => 1,
11709 ];
11710 $lastupdated_loop = get_posts( $lastupdated_args );
11711
11712 $response = [];
11713 if ( ! empty( $result ) ) {
11714 $context['post'] = $lastupdated_loop[0];
11715
11716 $meta_value = get_post_meta( $lastupdated_loop[0]->ID, sprintf( '%s', $data['filter']['field_id']['value'] ), true );
11717 $meta_key = sprintf( '%s', $data['filter']['field_id']['value'] );
11718
11719 $context[ $meta_key ] = $meta_value;
11720
11721 $response['pluggable_data'] = $context;
11722 $response['response_type'] = 'live';
11723 } else {
11724 $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 );
11725 }
11726
11727 return $response;
11728
11729 }
11730
11731 /**
11732 * Get Formidable Forms.
11733 *
11734 * @param array $data data.
11735 *
11736 * @return array|void
11737 */
11738 public function search_formidable_form_list( $data ) {
11739 if ( ! class_exists( 'FrmForm' ) ) {
11740 return;
11741 }
11742
11743 $page = $data['page'];
11744 $term = $data['search_term'];
11745 $limit = Utilities::get_search_page_limit();
11746 $offset = $limit * ( $page - 1 );
11747 $per_page = 10;
11748
11749 $query = [
11750 [
11751 'or' => 1,
11752 'name LIKE' => $term,
11753 'parent_form_id' => null,
11754 ],
11755 ];
11756 $query['is_template'] = 0;
11757 $query['status !'] = 'trash';
11758 $forms_list = FrmForm::getAll( $query, '', $offset . ',' . $per_page );
11759 $form_count = FrmForm::getAll( $query );
11760 $form_count = count( $form_count );
11761 $options = [];
11762
11763 if ( ! empty( $forms_list ) ) {
11764 if ( is_array( $forms_list ) ) {
11765 foreach ( $forms_list as $form ) {
11766 $options[] = [
11767 'label' => $form->name,
11768 'value' => $form->id,
11769 ];
11770 }
11771 }
11772 }
11773
11774 return [
11775 'options' => $options,
11776 'hasMore' => $form_count > $limit && $form_count > $offset,
11777 ];
11778 }
11779
11780 /**
11781 * Get JetFormBuilder Form List.
11782 *
11783 * @param array $data data.
11784 *
11785 * @return array|void
11786 */
11787 public function search_jetform_list( $data ) {
11788 if ( ! class_exists( '\Jet_Form_Builder\Classes\Tools' ) ) {
11789 return;
11790 }
11791
11792 $forms = \Jet_Form_Builder\Classes\Tools::get_forms_list_for_js();
11793
11794 $options = [];
11795 foreach ( $forms as $form ) {
11796
11797 if ( ! empty( $form['value'] ) ) {
11798 $options[] = [
11799 'label' => esc_html( $form['label'] ),
11800 'value' => esc_attr( $form['value'] ),
11801 ];
11802 }
11803 }
11804
11805 return [
11806 'options' => $options,
11807 'hasMore' => false,
11808 ];
11809 }
11810
11811 /**
11812 * Get Forminator Form List.
11813 *
11814 * @param array $data data.
11815 *
11816 * @return array|void
11817 */
11818 public function search_forminator_form_list( $data ) {
11819 if ( ! class_exists( 'Forminator_API' ) ) {
11820 return;
11821 }
11822
11823 $forms = Forminator_API::get_forms( null, 1, 10 );
11824
11825 $options = [];
11826 foreach ( $forms as $form ) {
11827 $options[] = [
11828 'label' => isset( $form->settings ) && isset( $form->settings['form_name'] ) ? $form->settings['form_name'] : $form->name,
11829 'value' => $form->id,
11830 ];
11831 }
11832
11833 return [
11834 'options' => $options,
11835 'hasMore' => false,
11836 ];
11837 }
11838
11839
11840 /**
11841 * Get BbPress topics list.
11842 *
11843 * @param array $data data.
11844 *
11845 * @return array|void
11846 */
11847 public function search_bbp_topic_list( $data ) {
11848 $page = $data['page'];
11849 $forum_id = $data['dynamic'];
11850 $limit = Utilities::get_search_page_limit();
11851 $offset = $limit * ( $page - 1 );
11852 $args = [
11853 'post_type' => 'topic',
11854 'offset' => $offset,
11855 'meta_query' => [
11856 [
11857 'key' => '_bbp_forum_id',
11858 'value' => $forum_id,
11859 'compare' => '==',
11860 ],
11861 ],
11862 ];
11863
11864 $topics = get_posts( $args );
11865 $topics_count = wp_count_posts( 'topic' )->publish;
11866
11867 $options = [];
11868 if ( ! empty( $topics ) ) {
11869 if ( is_array( $topics ) ) {
11870 foreach ( $topics as $topic ) {
11871 $options[] = [
11872 'label' => $topic->post_title,
11873 'value' => $topic->ID,
11874 ];
11875 }
11876 }
11877 }
11878 return [
11879 'options' => $options,
11880 'hasMore' => $topics_count > $limit && $topics_count > $offset,
11881 ];
11882 }
11883
11884
11885 /**
11886 * Search Last Updated Field Data.
11887 *
11888 * @param array $data data.
11889 * @return array
11890 */
11891 public function search_bbp_last_data( $data ) {
11892 global $wpdb;
11893
11894 $post_type = $data['post_type'];
11895 $trigger = $data['search_term'];
11896 $context = [];
11897
11898 if ( 'topic' === $post_type ) {
11899 $post_id = $data['filter']['forum']['value'];
11900 if ( -1 === $post_id ) {
11901 $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" );
11902
11903 } else {
11904 $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 ) );
11905
11906 }
11907 } elseif ( 'reply' === $post_type ) {
11908 $post_id = $data['filter']['topic']['value'];
11909
11910 if ( -1 === $post_id ) {
11911 $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" );
11912
11913 } else {
11914 $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 ) );
11915 }
11916 }
11917
11918
11919 $response = [];
11920 if ( ! empty( $result ) ) {
11921 if ( 'bbpress_topic_created' === $trigger ) {
11922 $topic_id = $result[0]->post_id;
11923 $forum_id = $result[0]->meta_value;
11924 $topic = get_the_title( $topic_id );
11925 $topic_link = get_the_permalink( $topic_id );
11926 $topic_description = get_the_content( null, false, $topic_id );
11927 $topic_status = get_post_status( $topic_id );
11928
11929 $forum = get_the_title( $forum_id );
11930 $forum_link = get_the_permalink( $forum_id );
11931 $forum_description = get_the_content( null, false, $forum_id );
11932 $forum_status = get_post_status( $forum_id );
11933
11934 $forum = [
11935 'forum' => $forum_id,
11936 'forum_title' => $forum,
11937 'forum_link' => $forum_link,
11938 'forum_description' => $forum_description,
11939 'forum_status' => $forum_status,
11940 ];
11941
11942 $topic = [
11943 'topic' => $topic_id,
11944 'topic_title' => $topic,
11945 'topic_link' => $topic_link,
11946 'topic_description' => $topic_description,
11947 'topic_status' => $topic_status,
11948 ];
11949
11950 $user_id = $result[0]->post_author;
11951 if ( '0' != $user_id ) {
11952 $context = array_merge(
11953 WordPress::get_user_context( $user_id ),
11954 $forum,
11955 $topic
11956 );
11957 } else {
11958 $anonymous_data = [
11959 'bbp_anonymous_name' => get_post_meta( $result[0]->ID, '_bbp_anonymous_name', true ),
11960 'bbp_anonymous_email' => get_post_meta( $result[0]->ID, '_bbp_anonymous_email', true ),
11961 'bbp_anonymous_website' => get_post_meta( $result[0]->ID, '_bbp_anonymous_website', true ),
11962 ];
11963 $context = array_merge(
11964 $anonymous_data,
11965 $forum,
11966 $topic
11967 );
11968 }
11969
11970 $response['pluggable_data'] = $context;
11971 $response['response_type'] = 'live';
11972 } else {
11973 $reply_id = $result[0]->post_id;
11974 $topic_id = $result[0]->meta_value;
11975 $forum_id = get_post_meta( $topic_id, '_bbp_forum_id', true );
11976 $forum_id = intval( '"' . $forum_id . '"' );
11977 $reply = get_the_title( $reply_id );
11978 $reply_link = get_the_permalink( $reply_id );
11979 $reply_description = get_the_content( null, false, $reply_id );
11980 $reply_status = get_post_status( $reply_id );
11981
11982
11983 $topic = get_the_title( $topic_id );
11984 $topic_link = get_the_permalink( $topic_id );
11985 $topic_description = get_the_content( null, false, $topic_id );
11986 $topic_status = get_post_status( $topic_id );
11987
11988 $forum = get_the_title( $forum_id );
11989 $forum_link = get_the_permalink( $forum_id );
11990 $forum_description = get_the_content( null, false, $forum_id );
11991 $forum_status = get_post_status( $forum_id );
11992
11993 $forum = [
11994 'forum' => $forum_id,
11995 'forum_title' => $forum,
11996 'forum_link' => $forum_link,
11997 'forum_description' => $forum_description,
11998 'forum_status' => $forum_status,
11999 ];
12000
12001 $topic = [
12002 'topic' => $topic_id,
12003 'topic_title' => $topic,
12004 'topic_link' => $topic_link,
12005 'topic_description' => $topic_description,
12006 'topic_status' => $topic_status,
12007 ];
12008
12009 $reply = [
12010 'reply_title' => $reply,
12011 'reply_link' => $reply_link,
12012 'reply_description' => $reply_description,
12013 'reply_status' => $reply_status,
12014 ];
12015 $user_id = $result[0]->post_author;
12016 if ( '0' != $user_id ) {
12017 $context = array_merge(
12018 WordPress::get_user_context( $user_id ),
12019 $forum,
12020 $topic,
12021 $reply
12022 );
12023 } else {
12024 $anonymous_data = [
12025 'bbp_anonymous_name' => get_post_meta( $result[0]->ID, '_bbp_anonymous_name', true ),
12026 'bbp_anonymous_email' => get_post_meta( $result[0]->ID, '_bbp_anonymous_email', true ),
12027 'bbp_anonymous_website' => get_post_meta( $result[0]->ID, '_bbp_anonymous_website', true ),
12028 ];
12029 $context = array_merge(
12030 $anonymous_data,
12031 $forum,
12032 $topic,
12033 $reply
12034 );
12035 }
12036
12037 $response['pluggable_data'] = $context;
12038 $response['response_type'] = 'live';
12039 }
12040 }
12041
12042 return $response;
12043 }
12044
12045 /**
12046 * Search Last Updated Field Data.
12047 *
12048 * @param array $data data.
12049 * @return array|void
12050 */
12051 public function search_happyform_list( $data ) {
12052 if ( ! function_exists( 'happyforms_get_form_controller' ) ) {
12053 return;
12054 }
12055
12056 $form_controller = happyforms_get_form_controller();
12057
12058 $forms = $form_controller->do_get();
12059 $options = [];
12060 if ( ! empty( $forms ) ) {
12061 foreach ( $forms as $form ) {
12062 $options[] = [
12063 'label' => $form['post_title'],
12064 'value' => $form['ID'],
12065 ];
12066 }
12067 }
12068
12069 return [
12070 'options' => $options,
12071 'hasMore' => false,
12072 ];
12073 }
12074
12075 /**
12076 * Get Memberpress Course List.
12077 *
12078 * @param array $data data.
12079 *
12080 * @return array|void
12081 */
12082 public function search_mpc_lessons_list( $data ) {
12083 if ( ! class_exists( '\memberpress\courses\models\Lesson' ) ) {
12084 return;
12085 }
12086 global $wpdb;
12087 $options = [];
12088 $course_id = $data['dynamic'];
12089 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mpcs_sections WHERE course_id =%s", $course_id ) );
12090 $sections = [];
12091 foreach ( $result as $rec ) {
12092 $sections[] = [
12093 'id' => $rec->id,
12094 'title' => $rec->title,
12095 ];
12096 }
12097 if ( is_array( $sections ) && count( $sections ) > 0 ) {
12098 foreach ( $sections as $section ) {
12099 $post_types_string = \memberpress\courses\models\Lesson::lesson_cpts();
12100 $post_types_string = implode( "','", $post_types_string );
12101
12102 $query = $wpdb->prepare(
12103 "SELECT * FROM {$wpdb->posts} AS p
12104 JOIN {$wpdb->postmeta} AS pm
12105 ON p.ID = pm.post_id
12106 AND pm.meta_key = %s
12107 AND pm.meta_value = %s
12108 JOIN {$wpdb->postmeta} AS pm_order
12109 ON p.ID = pm_order.post_id
12110 AND pm_order.meta_key = %s
12111 WHERE p.post_type in ( %s ) AND p.post_status <> 'trash'
12112 ORDER BY pm_order.meta_value * 1",
12113 models\Lesson::$section_id_str,
12114 $section['id'],
12115 models\Lesson::$lesson_order_str,
12116 stripcslashes( $post_types_string )
12117 );
12118
12119 $db_lessons = $wpdb->get_results( stripcslashes( $query ) ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
12120 foreach ( $db_lessons as $lesson ) {
12121 $options[] = [
12122 'label' => $section['title'] . '->' . $lesson->post_title,
12123 'value' => $lesson->ID,
12124 ];
12125 }
12126 }
12127 }
12128 return [
12129 'options' => $options,
12130 'hasMore' => false,
12131 ];
12132 }
12133
12134 /**
12135 * Get Memberpress Course List.
12136 *
12137 * @param array $data data.
12138 *
12139 * @return array|void
12140 */
12141 public function search_gp_rank_type_list( $data ) {
12142 global $wpdb;
12143
12144 $posts = $wpdb->get_results(
12145 "SELECT ID, post_name, post_title, post_type
12146 FROM $wpdb->posts
12147 WHERE post_type LIKE 'rank-type' AND post_status = 'publish' ORDER BY post_title ASC"
12148 );
12149
12150 $posts_count = count( $posts );
12151
12152 $options = [];
12153 if ( $posts ) {
12154 foreach ( $posts as $post ) {
12155 $options[] = [
12156 'label' => $post->post_title,
12157 'value' => $post->post_name,
12158 ];
12159 }
12160 }
12161
12162 return [
12163 'options' => $options,
12164 'hasMore' => false,
12165 ];
12166 }
12167
12168 /**
12169 * Get MPC last data.
12170 *
12171 * @param array $data data.
12172 *
12173 * @return array
12174 */
12175 public function search_mpc_last_data( $data ) {
12176 global $wpdb;
12177 $trigger = $data['search_term'];
12178 $course_data = [];
12179 $lesson_data = [];
12180 $context = [];
12181
12182 if ( 'mpc_course_completed' === $trigger ) {
12183 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course]']['value'] : '-1' );
12184 if ( $course_id > 0 ) {
12185
12186 $course = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $course_id ) );
12187 } else {
12188 $course = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-course' ORDER BY id DESC LIMIT 1" );
12189 }
12190
12191 if ( ! empty( $course ) ) {
12192 $course_data = [
12193 'course_id' => $course->ID,
12194 'course_title' => get_the_title( $course_id ),
12195 'course_url' => get_permalink( $course_id ),
12196 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
12197 'course_featured_image_url' => get_the_post_thumbnail_url( $course_id ),
12198 ];
12199 }
12200 $user_progress = $wpdb->get_row( $wpdb->prepare( "SELECT user_id FROM {$wpdb->prefix}mpcs_user_progress WHERE course_id=%s", $course_id ) );
12201 if ( ! empty( $user_progress ) ) {
12202 $context['response_type'] = 'live';
12203 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
12204 } else {
12205 $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}
12206 ,"response_type":"sample"} ';
12207 $context = json_decode( $sample_data, true );
12208 }
12209 } elseif ( 'mpc_lesson_completed' === $trigger ) {
12210 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
12211 $course_id = (int) $data['filter']['course']['value'];
12212 if ( $lesson_id > 0 ) {
12213
12214 $lesson = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $lesson_id ) );
12215 } else {
12216 $lesson = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-lesson' ORDER BY id DESC LIMIT 1" );
12217 }
12218
12219 if ( ! empty( $lesson ) ) {
12220 $lesson_data = [
12221 'lesson_id' => $lesson->ID,
12222 'lesson_title' => get_the_title( $lesson_id ),
12223 'lesson_url' => get_permalink( $lesson_id ),
12224 'lesson_featured_image_id' => get_post_meta( $lesson_id, '_thumbnail_id', true ),
12225 'lesson_featured_image_url' => get_the_post_thumbnail_url( $lesson_id ),
12226 ];
12227
12228 $lesson_section_id = get_post_meta( $lesson->ID, '_mpcs_lesson_section_id', true );
12229
12230 $section = $wpdb->get_row( $wpdb->prepare( "SELECT course_id FROM {$wpdb->prefix}mpcs_sections WHERE ID= %s", $lesson_section_id ) );
12231
12232 $course_data = [
12233 'course_id' => $course_id,
12234 'course_title' => get_the_title( $course_id ),
12235 'course_url' => get_permalink( $course_id ),
12236 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
12237 'course_featured_image_url' => get_the_post_thumbnail_url( $section->course_id ),
12238 ];
12239 }
12240
12241 $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 ) );
12242 if ( ! empty( $user_progress ) ) {
12243 $context['response_type'] = 'live';
12244 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
12245 } else {
12246 $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"}';
12247 $context = json_decode( $sample_data, true );
12248 }
12249 }
12250
12251
12252 return $context;
12253 }
12254
12255 /** Get GamiPress Rank List.
12256 *
12257 * @param array $data data.
12258 *
12259 * @return array|void
12260 */
12261 public function search_gp_rank_list( $data ) {
12262 global $wpdb;
12263
12264 $page = $data['page'];
12265 $limit = Utilities::get_search_page_limit();
12266 $offset = $limit * ( $page - 1 );
12267
12268 $args = [
12269 'post_type' => $data['dynamic']['rank_type'],
12270 'posts_per_page' => $limit,
12271 'offset' => $offset,
12272 'orderby' => 'title',
12273 'order' => 'ASC',
12274 'post_status' => 'publish',
12275 's' => $data['search_term'],
12276 ];
12277
12278 $rank_type = get_posts( $args );
12279
12280 $count_args = [
12281 'post_type' => $data['dynamic']['rank_type'],
12282 'posts_per_page' => -1,
12283 'post_status' => 'publish',
12284 's' => $data['search_term'],
12285 ];
12286
12287 $rank_posts = get_posts( $count_args );
12288 $rank_type_count = count( $rank_posts );
12289
12290 $options = [];
12291 if ( $rank_type ) {
12292 foreach ( $rank_type as $rank ) {
12293 $options[] = [
12294 'label' => $rank->post_title,
12295 'value' => $rank->ID,
12296 ];
12297 }
12298 }
12299
12300 return [
12301 'options' => $options,
12302 'hasMore' => $rank_type_count > $limit && $rank_type_count > $offset,
12303 ];
12304 }
12305
12306 /**
12307 * Get GamiPress PointType List.
12308 *
12309 * @param array $data data.
12310 *
12311 * @return array|void
12312 */
12313 public function search_gp_point_type_list( $data ) {
12314 global $wpdb;
12315
12316 $page = $data['page'];
12317 $limit = Utilities::get_search_page_limit();
12318 $offset = $limit * ( $page - 1 );
12319
12320 $args = [
12321 'post_type' => 'points-type',
12322 'posts_per_page' => $limit,
12323 'offset' => $offset,
12324 'orderby' => 'title',
12325 'order' => 'ASC',
12326 'post_status' => 'publish',
12327 's' => $data['search_term'],
12328 ];
12329
12330 $point_type = get_posts( $args );
12331
12332 $count_args = [
12333 'post_type' => 'points-type',
12334 'posts_per_page' => -1,
12335 'post_status' => 'publish',
12336 's' => $data['search_term'],
12337 ];
12338
12339 $count_point_type = get_posts( $count_args );
12340 $point_type_count = count( $count_point_type );
12341
12342 $options = [];
12343 if ( $point_type ) {
12344 foreach ( $point_type as $point ) {
12345 $options[] = [
12346 'label' => $point->post_title,
12347 'value' => $point->ID,
12348 ];
12349 }
12350 }
12351
12352 return [
12353 'options' => $options,
12354 'hasMore' => $point_type_count > $limit && $point_type_count > $offset,
12355 ];
12356 }
12357
12358 /**
12359 * Get GamiPress AchievementType List.
12360 *
12361 * @param array $data data.
12362 *
12363 * @return array|void
12364 */
12365 public function search_gp_achivement_type_list( $data ) {
12366 global $wpdb;
12367
12368 $posts = $wpdb->get_results(
12369 "SELECT ID, post_name, post_title, post_type
12370 FROM $wpdb->posts
12371 WHERE post_type LIKE 'achievement-type' AND post_status = 'publish' ORDER BY post_title ASC"
12372 );
12373
12374 $posts_count = count( $posts );
12375
12376 $options = [];
12377 if ( $posts ) {
12378 foreach ( $posts as $post ) {
12379 $options[] = [
12380 'label' => $post->post_title,
12381 'value' => $post->post_name,
12382 ];
12383 }
12384 }
12385
12386 $options[] = [
12387 'label' => 'Points awards',
12388 'value' => 'points-award',
12389 ];
12390 $options[] = [
12391 'label' => 'Step',
12392 'value' => 'step',
12393 ];
12394 $options[] = [
12395 'label' => 'Rank requirement',
12396 'value' => 'rank-requirement',
12397 ];
12398
12399 return [
12400 'options' => $options,
12401 'hasMore' => false,
12402 ];
12403 }
12404
12405 /**
12406 * Get GamiPress Award List.
12407 *
12408 * @param array $data data.
12409 *
12410 * @return array|void
12411 */
12412 public function search_gp_award_list( $data ) {
12413 global $wpdb;
12414
12415 $page = $data['page'];
12416 $limit = Utilities::get_search_page_limit();
12417 $offset = $limit * ( $page - 1 );
12418
12419 $args = [
12420 'post_type' => $data['dynamic']['achivement_type'],
12421 'posts_per_page' => $limit,
12422 'offset' => $offset,
12423 'orderby' => 'title',
12424 'order' => 'ASC',
12425 'post_status' => 'publish',
12426 's' => $data['search_term'],
12427 ];
12428
12429 $award_type = get_posts( $args );
12430 $count_args = [
12431 'post_type' => $data['dynamic']['achivement_type'],
12432 'posts_per_page' => -1,
12433 'post_status' => 'publish',
12434 's' => $data['search_term'],
12435 ];
12436
12437 $count_award_type = get_posts( $count_args );
12438 $award_type_count = count( $count_award_type );
12439 $options = [];
12440 if ( $award_type ) {
12441 foreach ( $award_type as $award ) {
12442 $options[] = [
12443 'label' => $award->post_title,
12444 'value' => $award->ID,
12445 ];
12446 }
12447 }
12448
12449 return [
12450 'options' => $options,
12451 'hasMore' => $award_type_count > $limit && $award_type_count > $offset,
12452 ];
12453 }
12454
12455 /**
12456 * Get Woocommerce Subscription Product List.
12457 *
12458 * @param array $data data.
12459 *
12460 * @return array|void
12461 */
12462 public function search_wc_subscription_product_list( $data ) {
12463 global $wpdb;
12464
12465 $subscriptions = $wpdb->get_results(
12466 $wpdb->prepare(
12467 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
12468 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
12469 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
12470 AND posts.post_type = %s
12471 AND posts.post_status = %s
12472 UNION ALL
12473 SELECT ID, post_title FROM $wpdb->posts
12474 WHERE post_type = %s
12475 AND post_status = %s
12476 ORDER BY post_title",
12477 'product',
12478 'publish',
12479 'shop_subscription',
12480 'publish'
12481 )
12482 );
12483 $page = $data['page'];
12484 $limit = Utilities::get_search_page_limit();
12485 $offset = $limit * ( $page - 1 );
12486 $subscriptions_products = [];
12487 $subscription_products_count = 10;
12488 if ( function_exists( 'wc_get_products' ) ) {
12489 $product_query = wc_get_products(
12490 [
12491 'type' => [ 'subscription', 'variable-subscription' ],
12492 'posts_per_page' => $limit,
12493 'offset' => $offset,
12494 'paginate' => true,
12495 ]
12496 );
12497 if ( is_object( $product_query ) ) {
12498 $subscriptions_products = $product_query->products;
12499 $subscription_products_count = $product_query->total;
12500 }
12501 }
12502
12503 $options = [];
12504 if ( $subscriptions ) {
12505 foreach ( $subscriptions as $post ) {
12506 $options[] = [
12507 'label' => $post->post_title,
12508 'value' => $post->ID,
12509 ];
12510 }
12511 return [
12512 'options' => $options,
12513 'hasMore' => false,
12514 ];
12515 } elseif ( $subscriptions_products ) {
12516 foreach ( $subscriptions_products as $product ) {
12517 $title = $product->get_name();
12518 $options[] = [
12519 'label' => ! empty( $title ) ? $title : $product->get_id(),
12520 'value' => $product->get_id(),
12521 ];
12522 }
12523 return [
12524 'options' => $options,
12525 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
12526 ];
12527 } else {
12528 return [
12529 'options' => $options,
12530 'hasMore' => false,
12531 ];
12532 }
12533 }
12534
12535 /**
12536 * Get Woocommerce Subscriptions Variation list.
12537 *
12538 * @param array $data data.
12539 *
12540 * @return array|void
12541 */
12542 public function search_wc_variable_subscription_list( $data ) {
12543 global $wpdb;
12544
12545 $page = $data['page'];
12546 $limit = Utilities::get_search_page_limit();
12547 $offset = $limit * ( $page - 1 );
12548
12549 if ( ! function_exists( 'wc_get_products' ) ) {
12550 return;
12551 }
12552 $subscription_products = wc_get_products(
12553 [
12554 'type' => [ 'variable-subscription' ],
12555 'posts_per_page' => $limit,
12556 'offset' => $offset,
12557 'orderby' => 'date',
12558 'order' => 'DESC',
12559 ]
12560 );
12561
12562 $subscription_products_count = count( (array) $subscription_products );
12563
12564 $options = [];
12565 if ( $subscription_products ) {
12566 foreach ( (array) $subscription_products as $product ) {
12567 $options[] = [
12568 'label' => $product->get_title(),
12569 'value' => $product->get_id(),
12570 ];
12571 }
12572 }
12573
12574 return [
12575 'options' => $options,
12576 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
12577 ];
12578 }
12579
12580 /**
12581 * Get Woocommerce Variation list.
12582 *
12583 * @param array $data data.
12584 *
12585 * @return array|void
12586 */
12587 public function search_wc_variation_list( $data ) {
12588 global $wpdb;
12589
12590 $page = $data['page'];
12591 $limit = Utilities::get_search_page_limit();
12592 $offset = $limit * ( $page - 1 );
12593
12594 $args = [
12595 'post_type' => 'product_variation',
12596 'post_parent' => $data['dynamic']['variable_subscription'],
12597 'posts_per_page' => $limit,
12598 'offset' => $offset,
12599 'orderby' => 'ID',
12600 'order' => 'ASC',
12601 'post_status' => 'publish',
12602 ];
12603
12604 $variation = get_posts( $args );
12605 $variation_count = count( $variation );
12606
12607 $options = [];
12608 if ( $variation ) {
12609 foreach ( $variation as $product ) {
12610 $options[] = [
12611 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
12612 'value' => $product->ID,
12613 ];
12614 }
12615 }
12616
12617 return [
12618 'options' => $options,
12619 'hasMore' => $variation_count > $limit && $variation_count > $offset,
12620 ];
12621 }
12622
12623 /**
12624 * Get Membership List.
12625 *
12626 * @param array $data data.
12627 *
12628 * @return array
12629 */
12630 public function search_membership_list( $data ) {
12631 global $wpdb;
12632
12633 $levels = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}pmpro_membership_levels ORDER BY id ASC" );
12634 $options = [];
12635 if ( $levels ) {
12636 foreach ( $levels as $level ) {
12637 $options[] = [
12638 'label' => $level->name,
12639 'value' => $level->id,
12640 ];
12641 }
12642 }
12643
12644 return [
12645 'options' => $options,
12646 'hasMore' => false,
12647 ];
12648 }
12649
12650 /**
12651
12652 * Get EventsManager last data.
12653 *
12654 * @param array $data data.
12655 *
12656 * @return array
12657 */
12658 public function search_events_manager_data( $data ) {
12659 global $wpdb;
12660 $trigger = $data['search_term'];
12661 $context = [];
12662
12663 $post_id = (int) ( isset( $data['filter']['post_id']['value'] ) ? $data['filter']['post_id']['value'] : '-1' );
12664 if ( 'em_user_register_in_event' === $trigger ) {
12665 if ( $post_id > 0 ) {
12666 $event_id_id = get_post_meta( $post_id, '_event_id', true );
12667 $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 ) );
12668 } else {
12669 $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" );
12670
12671 }
12672
12673 if ( ! empty( $all_bookings ) ) {
12674 $user_id = $all_bookings->person_id;
12675 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
12676 $bookings = json_decode( (string) wp_json_encode( $all_bookings ), true );
12677 $context['pluggable_data'] = array_merge(
12678 WordPress::get_user_context( $user_id ),
12679 is_array( $bookings ) ? $bookings : []
12680 );
12681 if ( ! empty( $location ) ) {
12682 $locations = json_decode( (string) wp_json_encode( $location ), true );
12683 $context['pluggable_data'] = array_merge(
12684 $context['pluggable_data'],
12685 is_array( $locations ) ? $locations : []
12686 );
12687 }
12688
12689 $context['response_type'] = 'live';
12690 }
12691 } elseif ( 'em_user_unregister_from_event' === $trigger ) {
12692
12693 if ( $post_id > 0 ) {
12694 $event_id_id = get_post_meta( $post_id, '_event_id', true );
12695 $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 ) );
12696 } else {
12697 $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" );
12698
12699 }
12700
12701 if ( ! empty( $all_bookings ) ) {
12702 $user_id = $all_bookings->person_id;
12703 $bookings = json_decode( (string) wp_json_encode( $all_bookings ), true );
12704 $context['pluggable_data'] = array_merge(
12705 WordPress::get_user_context( $user_id ),
12706 is_array( $bookings ) ? $bookings : []
12707 );
12708 $context['response_type'] = 'live';
12709 }
12710 } elseif ( 'em_user_booking_approved' === $trigger ) {
12711
12712 if ( $post_id > 0 ) {
12713 $event_id_id = get_post_meta( $post_id, '_event_id', true );
12714 $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 ) );
12715 } else {
12716 $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" );
12717
12718 }
12719
12720 if ( ! empty( $all_bookings ) ) {
12721 $user_id = $all_bookings->person_id;
12722 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
12723 $bookings = json_decode( (string) wp_json_encode( $all_bookings ), true );
12724 $context['pluggable_data'] = array_merge(
12725 WordPress::get_user_context( $user_id ),
12726 is_array( $bookings ) ? $bookings : []
12727 );
12728 if ( ! empty( $location ) ) {
12729 $locations = json_decode( (string) wp_json_encode( $location ), true );
12730 $context['pluggable_data'] = array_merge(
12731 $context['pluggable_data'],
12732 is_array( $locations ) ? $locations : []
12733 );
12734 }
12735
12736 $context['response_type'] = 'live';
12737
12738 }
12739 } elseif ( 'em_user_registers_event_with_specific_ticket' === $trigger ) {
12740 if ( $post_id > 0 ) {
12741 $event_id_id = get_post_meta( $post_id, '_event_id', true );
12742 $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 ) );
12743 }
12744 if ( ! empty( $all_bookings ) ) {
12745 $ticket_id = (int) $data['filter']['ticket_id']['value'];
12746 $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 ) );
12747 if ( ! empty( $all_ticket_bookings ) ) {
12748 $user_id = $all_bookings->person_id;
12749 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
12750 $bookings_str = wp_json_encode( $all_bookings );
12751 $ticket_bookings_str = wp_json_encode( $all_ticket_bookings );
12752
12753 $context['pluggable_data'] = array_merge(
12754 WordPress::get_user_context( $user_id )
12755 );
12756 if ( is_string( $bookings_str ) && is_string( $ticket_bookings_str ) ) {
12757 $bookings_arr = json_decode( $bookings_str, true );
12758 $ticket_bookings_arr = json_decode( $ticket_bookings_str, true );
12759 if ( is_array( $bookings_arr ) && is_array( $ticket_bookings_arr ) ) {
12760 $context['pluggable_data'] = array_merge(
12761 $context['pluggable_data'],
12762 $bookings_arr,
12763 $ticket_bookings_arr
12764 );
12765 }
12766 }
12767 if ( ! empty( $location ) ) {
12768 $location_str = wp_json_encode( $location );
12769 if ( is_string( $location_str ) ) {
12770 $location_arr = json_decode( $location_str, true );
12771 if ( is_array( $location_arr ) ) {
12772 $context['pluggable_data'] = array_merge( $context['pluggable_data'], $location_arr );
12773 }
12774 }
12775 }
12776 $context['response_type'] = 'live';
12777 }
12778 }
12779 }
12780 return $context;
12781 }
12782
12783 /**
12784 * Get Events Manager Events Ticket list.
12785 *
12786 * @param array $data data.
12787 *
12788 * @return array|void
12789 */
12790 public function search_em_event_tickets( $data ) {
12791 global $wpdb;
12792
12793 $options = [];
12794
12795 $event = $data['dynamic']['post_id'];
12796
12797 $event_id = $wpdb->get_var( $wpdb->prepare( "SELECT event_id FROM {$wpdb->prefix}em_events WHERE post_id = %d", $event ) );
12798 $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] ) );
12799
12800 if ( $tickets ) {
12801 foreach ( $tickets as $ticket ) {
12802 $options[] = [
12803 'label' => $ticket->ticket_name,
12804 'value' => $ticket->ticket_id,
12805 ];
12806 }
12807 }
12808
12809 return [
12810 'options' => $options,
12811 'hasMore' => false,
12812 ];
12813 }
12814
12815 /**
12816
12817 * Get learnpress last data.
12818 *
12819 * @param array $data data.
12820 *
12821 * @return array
12822 */
12823 public function search_learnpress_lms_last_data( $data ) {
12824 global $wpdb;
12825 $trigger = $data['search_term'];
12826 $course_data = [];
12827 $lesson_data = [];
12828 $context = [];
12829
12830
12831 if ( 'learnpress_course_completed' === $trigger ) {
12832 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
12833 if ( $course_id > 0 ) {
12834
12835 $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 ) );
12836 } else {
12837
12838 $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" );
12839 }
12840
12841 if ( ! empty( $course ) ) {
12842 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
12843 $context['response_type'] = 'live';
12844 $context['pluggable_data'] = $course_data;
12845 }
12846 } elseif ( 'learnpress_lesson_completed' === $trigger ) {
12847 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
12848 if ( $lesson_id > 0 ) {
12849
12850 $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 ) );
12851 } else {
12852
12853 $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" );
12854 }
12855
12856 if ( ! empty( $lesson ) ) {
12857 $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 ) );
12858 $context['response_type'] = 'live';
12859 $context['pluggable_data'] = $lesson_data;
12860 }
12861 } elseif ( 'learnpress_user_enrolled_in_course' === $trigger ) {
12862 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
12863 if ( $course_id > 0 ) {
12864
12865 $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 ) );
12866 } else {
12867
12868 $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" );
12869 }
12870
12871 if ( ! empty( $course ) ) {
12872 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
12873 $context['response_type'] = 'live';
12874 $context['pluggable_data'] = $course_data;
12875
12876 }
12877 }
12878
12879 return $context;
12880 }
12881
12882 /**
12883 * Get Woocommerce Memberships Plan List.
12884 *
12885 * @param array $data data.
12886 *
12887 * @return array|void
12888 */
12889 public function search_wc_membership_plan_list( $data ) {
12890 global $wpdb;
12891
12892 $page = $data['page'];
12893 $limit = Utilities::get_search_page_limit();
12894 $offset = $limit * ( $page - 1 );
12895
12896 $args = [
12897 'post_type' => 'wc_membership_plan',
12898 'posts_per_page' => $limit,
12899 'offset' => $offset,
12900 'orderby' => 'title',
12901 'order' => 'ASC',
12902 'post_status' => 'publish',
12903 'fields' => 'ids',
12904 ];
12905 $loop = new WP_Query( $args );
12906
12907 $plans = (array) $loop->posts;
12908 $plans_count = wp_count_posts( 'wc_membership_plan' )->publish;
12909
12910 $options = [];
12911 if ( ! empty( $plans ) ) {
12912 if ( is_array( $plans ) ) {
12913 foreach ( $plans as $plan_id ) {
12914 $options[] = [
12915 'label' => get_the_title( $plan_id ),
12916 'value' => $plan_id,
12917 ];
12918 }
12919 }
12920 }
12921
12922 return [
12923 'options' => $options,
12924 'hasMore' => $plans_count > $limit && $plans_count > $offset,
12925 ];
12926 }
12927
12928 /**
12929 * Get BuddyPress Private group.
12930 *
12931 * @param array $data data.
12932 *
12933 * @return array|void
12934 */
12935 public function search_bp_private_group_list( $data ) {
12936 global $wpdb;
12937
12938 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'private'" );
12939
12940 $options = [];
12941 if ( $groups ) {
12942 foreach ( $groups as $group ) {
12943 $options[] = [
12944 'label' => $group->name,
12945 'value' => $group->id,
12946 ];
12947 }
12948 }
12949
12950 return [
12951 'options' => $options,
12952 'hasMore' => false,
12953 ];
12954 }
12955
12956 /**
12957 * Get BuddyPress Public group.
12958 *
12959 * @param array $data data.
12960 *
12961 * @return array|void
12962 */
12963 public function search_bp_public_group_list( $data ) {
12964 global $wpdb;
12965
12966 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'public'" );
12967
12968 $options = [];
12969 if ( $groups ) {
12970 foreach ( $groups as $group ) {
12971 $options[] = [
12972 'label' => $group->name,
12973 'value' => $group->id,
12974 ];
12975 }
12976 }
12977
12978 return [
12979 'options' => $options,
12980 'hasMore' => false,
12981 ];
12982 }
12983
12984 /**
12985 * Get BuddyPress group.
12986 *
12987 * @param array $data data.
12988 *
12989 * @return array|void
12990 */
12991 public function search_bp_group_list( $data ) {
12992 global $wpdb;
12993
12994 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
12995
12996 $options = [];
12997 if ( $groups ) {
12998 foreach ( $groups as $group ) {
12999 $options[] = [
13000 'label' => $group->name,
13001 'value' => $group->id,
13002 ];
13003 }
13004 }
13005
13006 return [
13007 'options' => $options,
13008 'hasMore' => false,
13009 ];
13010 }
13011
13012 /**
13013 * Get BuddyPress field.
13014 *
13015 * @param array $data data.
13016 *
13017 * @return array|void
13018 */
13019 public function search_bp_field_list( $data ) {
13020 global $wpdb;
13021
13022 $base_group_id = 1;
13023 if ( function_exists( 'bp_xprofile_base_group_id' ) ) {
13024 $base_group_id = bp_xprofile_base_group_id();
13025 }
13026
13027 $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 ) );
13028
13029 $options = [];
13030 if ( ! empty( $xprofile_fields ) ) {
13031 foreach ( $xprofile_fields as $xprofile_field ) {
13032 $options[] = [
13033 'label' => $xprofile_field->name,
13034 'value' => $xprofile_field->id,
13035 ];
13036 }
13037 }
13038
13039 return [
13040 'options' => $options,
13041 'hasMore' => false,
13042 ];
13043 }
13044
13045 /**
13046 * Get BuddyPress member type.
13047 *
13048 * @param array $data data.
13049 *
13050 * @return array|void
13051 */
13052 public function search_bp_member_type_list( $data ) {
13053 $options = [];
13054 if ( function_exists( 'bp_get_member_types' ) ) {
13055 $types = bp_get_member_types( [] );
13056 if ( $types ) {
13057 foreach ( $types as $key => $type ) {
13058 $options[] = [
13059 'label' => $type,
13060 'value' => $key,
13061 ];
13062 }
13063 }
13064 }
13065
13066 return [
13067 'options' => $options,
13068 'hasMore' => false,
13069 ];
13070 }
13071
13072 /**
13073 * Get last data for WP All Import.
13074 *
13075 * @param array $data data.
13076 * @return mixed
13077 */
13078 public function search_wp_all_import_last_data( $data ) {
13079 global $wpdb;
13080 $post_type = $data['filter']['post_type']['value'];
13081 $trigger = $data['search_term'];
13082
13083 if ( 'wp_all_import_post_type_imported' === $trigger ) {
13084 if ( -1 == $post_type ) {
13085 $imports = $wpdb->get_row( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts ORDER BY id DESC LIMIT 1", ARRAY_A );
13086 $posts[0] = $imports['post_id'];
13087 } else {
13088 $imports = $wpdb->get_results( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts", ARRAY_A );
13089 $imports = array_column( $imports, 'post_id' );
13090 $args = [
13091 'posts_per_page' => 1,
13092 'post_type' => $post_type,
13093 'post__in' => $imports,
13094 ];
13095 $posts = get_posts( $args );
13096 }
13097 } elseif ( 'wp_all_import_completed' === $trigger ) {
13098 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 0 ORDER BY id DESC LIMIT 1", ARRAY_A );
13099 } elseif ( 'wp_all_import_failed' === $trigger ) {
13100 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 1 ORDER BY id DESC LIMIT 1", ARRAY_A );
13101 }
13102
13103 if ( 'wp_all_import_post_type_imported' === $trigger && empty( $imports ) ) {
13104 $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 );
13105 return $context;
13106 } elseif ( empty( $imports ) ) {
13107 $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 );
13108 return $context;
13109 }
13110
13111 $context['response_type'] = 'live';
13112 if ( ! empty( $posts ) ) {
13113 $context['pluggable_data'] = WordPress::get_post_context( $posts[0] );
13114 } else {
13115 $context['pluggable_data'] = $imports;
13116 }
13117
13118 return $context;
13119 }
13120
13121 /**
13122 * Get Wp Simple Pay Forms.
13123 *
13124 * @param array $data data.
13125 *
13126 * @return array
13127 */
13128 public function search_wp_simple_pay_forms( $data ) {
13129
13130 $page = $data['page'];
13131 $limit = Utilities::get_search_page_limit();
13132 $offset = $limit * ( $page - 1 );
13133
13134 $forms = get_posts(
13135 [
13136 'post_type' => 'simple-pay',
13137 'posts_per_page' => $limit,
13138 'offset' => $offset,
13139 'fields' => 'ids',
13140 ]
13141 );
13142
13143 $forms_count = wp_count_posts( 'simple-pay' )->publish;
13144
13145 $options = [];
13146
13147 if ( ! empty( $forms ) ) {
13148 foreach ( $forms as $form_id ) {
13149 if ( function_exists( 'simpay_get_form' ) ) {
13150 $form = simpay_get_form( $form_id );
13151 $options[] = [
13152 'label' => null !== get_the_title( $form_id ) ? $form->company_name : get_the_title( $form_id ),
13153 'value' => $form_id,
13154 ];
13155 }
13156 }
13157 }
13158
13159 return [
13160 'options' => $options,
13161 'hasMore' => $forms_count > $limit && $forms_count > $offset,
13162 ];
13163 }
13164
13165 /**
13166 * Get Post list as per post type for metabox.
13167 *
13168 * @param array $data data.
13169 *
13170 * @return array
13171 */
13172 public function search_mb_posts_list( $data ) {
13173
13174 $page = $data['page'];
13175 $limit = Utilities::get_search_page_limit();
13176 $offset = $limit * ( $page - 1 );
13177
13178 $posts = get_posts(
13179 [
13180 'post_type' => $data['dynamic'],
13181 'posts_per_page' => $limit,
13182 'offset' => $offset,
13183 'fields' => 'ids',
13184 ]
13185 );
13186
13187 $all_posts = get_posts(
13188 [
13189 'post_type' => $data['dynamic'],
13190 'posts_per_page' => -1,
13191 'fields' => 'ids',
13192 ]
13193 );
13194
13195 $posts_count = count( $all_posts );
13196
13197 $options = [];
13198
13199 if ( ! empty( $posts ) ) {
13200 foreach ( $posts as $post ) {
13201 $title = html_entity_decode( get_the_title( $post ), ENT_QUOTES, 'UTF-8' );
13202 $options[] = [
13203 'label' => $title,
13204 'value' => $post,
13205 ];
13206 }
13207 }
13208
13209 return [
13210 'options' => $options,
13211 'hasMore' => $posts_count > $limit && $posts_count > $offset,
13212 ];
13213 }
13214
13215 /**
13216 * Get Metabox Custom box in Post list.
13217 *
13218 * @param array $data data.
13219 *
13220 * @return array
13221 */
13222 public function search_mb_field_list( $data ) {
13223
13224 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
13225 return [];
13226 }
13227
13228 $options = [];
13229
13230 $metabox_fields = (array) rwmb_get_object_fields( $data['dynamic'] );
13231
13232 foreach ( $metabox_fields as $metabox_field ) {
13233
13234 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
13235
13236 $options[] = [
13237 'label' => $metabox_field['name'],
13238 'value' => $metabox_field['id'],
13239 ];
13240
13241 }
13242 }
13243
13244 return [
13245 'options' => $options,
13246 'hasMore' => false,
13247 ];
13248 }
13249
13250 /**
13251 * Get Metabox Custom box user list.
13252 *
13253 * @param array $data data.
13254 *
13255 * @return array
13256 */
13257 public function search_mb_user_field_list( $data ) {
13258
13259 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
13260 return [];
13261 }
13262
13263 $options = [];
13264
13265 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
13266
13267 foreach ( $metabox_fields as $metabox_field ) {
13268
13269 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
13270
13271 $options[] = [
13272 'label' => $metabox_field['name'],
13273 'value' => $metabox_field['id'],
13274 ];
13275
13276 }
13277 }
13278
13279 return [
13280 'options' => $options,
13281 'hasMore' => false,
13282 ];
13283 }
13284
13285 /**
13286 * Search Last Updated Field Data for MetaBox.
13287 *
13288 * @param array $data data.
13289 * @return array
13290 */
13291 public function search_meta_box_field_data( $data ) {
13292 global $wpdb;
13293
13294 $context = [];
13295
13296 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
13297
13298 $post_type = $data['filter']['wp_post_type']['value'];
13299 $post = $data['filter']['wp_post']['value'];
13300
13301 if ( -1 === $field ) {
13302 if ( function_exists( 'rwmb_get_object_fields' ) ) {
13303 $metaboxes = rwmb_get_object_fields( $post_type );
13304
13305 if ( ! empty( $metaboxes ) ) {
13306 $random_key = array_rand( $metaboxes );
13307 $field = $random_key;
13308 } else {
13309 $result = '';
13310 }
13311 }
13312 } else {
13313 $field = $data['filter']['field_id']['value'];
13314 }
13315
13316 if ( function_exists( 'rwmb_meta' ) ) {
13317 $result = rwmb_meta( $field, '', $post );
13318 }
13319
13320 $response = [];
13321 if ( ! empty( $result ) ) {
13322 $response['pluggable_data'] = array_merge( [ $field => $result ], WordPress::get_post_context( $post ) );
13323 $response['response_type'] = 'live';
13324 } else {
13325 $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 );
13326 }
13327
13328 return $response;
13329 }
13330
13331 /**
13332 * Search Last Updated User Field Data MetaBox.
13333 *
13334 * @param array $data data.
13335 * @return array
13336 */
13337 public function search_user_meta_box_field_data( $data ) {
13338 global $wpdb;
13339
13340 $context = [];
13341
13342 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
13343
13344 if ( -1 === $field ) {
13345 if ( function_exists( 'rwmb_get_object_fields' ) ) {
13346 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
13347
13348 if ( ! empty( $metabox_fields ) ) {
13349 $random_key = array_rand( $metabox_fields );
13350 $field = $random_key;
13351 } else {
13352 $result = '';
13353 }
13354 }
13355 } else {
13356 $field = $data['filter']['field_id']['value'];
13357 }
13358
13359 $users = get_users(
13360 [
13361 'fields' => 'ID',
13362 'meta_key' => $field,
13363 ]
13364 );
13365
13366 if ( ! empty( $users ) ) {
13367 $user_random_key = array_rand( $users );
13368 $user_id = $user_random_key;
13369 if ( function_exists( 'rwmb_get_value' ) ) {
13370 $result = rwmb_get_value( $field, [ 'object_type' => 'user' ], $users[ $user_id ] );
13371 }
13372
13373 $response = [];
13374 if ( ! empty( $result ) ) {
13375 $context = [
13376 'field_id' => $field,
13377 $field => $result,
13378 'user' => WordPress::get_user_context( $users[ $user_id ] ),
13379 ];
13380 $response['pluggable_data'] = $context;
13381 $response['response_type'] = 'live';
13382 } else {
13383 $response = json_decode(
13384 '{
13385 "response_type": "sample",
13386 "pluggable_data": {
13387 "field_id": "gender",
13388 "user": {
13389 "wp_user_id": 114,
13390 "user_login": "test",
13391 "display_name": "test",
13392 "user_firstname": "test",
13393 "user_lastname": "test",
13394 "user_email": "test@test.com",
13395 "user_role": [ "subscriber" ]
13396 }
13397 }
13398 }',
13399 true
13400 );
13401 }
13402 } else {
13403 $response = json_decode(
13404 '{
13405 "response_type": "sample",
13406 "pluggable_data": {
13407 "field_id": "gender",
13408 "user": {
13409 "wp_user_id": 114,
13410 "user_login": "test",
13411 "display_name": "test",
13412 "user_firstname": "test",
13413 "user_lastname": "test",
13414 "user_email": "test@test.com",
13415 "user_role": [ "subscriber" ]
13416 }
13417 }
13418 }',
13419 true
13420 );
13421 }
13422
13423 return $response;
13424 }
13425
13426 /**
13427 * Search forms of Pie Forms.
13428 *
13429 * @param array $data data.
13430 * @return array
13431 */
13432 public function search_wp_polls_list( $data ) {
13433 global $wpdb;
13434 $options = [];
13435
13436 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsq' ) ) ) {
13437
13438 $results = $wpdb->get_results( 'SELECT pollq_id, pollq_question FROM ' . $wpdb->prefix . 'pollsq WHERE pollq_active = 1' );
13439
13440 if ( $results ) {
13441 foreach ( $results as $result ) {
13442 $options[] = [
13443 'label' => $result->pollq_question,
13444 'value' => $result->pollq_id,
13445 ];
13446 }
13447 }
13448 }
13449
13450 return [
13451 'options' => $options,
13452 'hasMore' => false,
13453 ];
13454 }
13455
13456 /**
13457 * Search answers of WP-Polls questions.
13458 *
13459 * @param array $data data.
13460 * @return array
13461 */
13462 public function search_wp_polls_answers( $data ) {
13463 global $wpdb;
13464
13465 $options = [];
13466 $poll_id = $data['dynamic'];
13467
13468 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsa' ) ) ) {
13469
13470 if ( '-1' !== $poll_id ) {
13471 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa WHERE polla_qid = %d', $poll_id ) );
13472 } else {
13473 $results = $wpdb->get_results( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa' );
13474 }
13475
13476 if ( $results ) {
13477 foreach ( $results as $result ) {
13478 $options[] = [
13479 'label' => $result->polla_answers,
13480 'value' => $result->polla_aid,
13481 ];
13482 }
13483 }
13484 }
13485
13486 return [
13487 'options' => $options,
13488 'hasMore' => false,
13489 ];
13490 }
13491
13492 /**
13493 * Get last data for trigger.
13494 *
13495 * @param array $data data.
13496 * @return array
13497 */
13498 public function search_wp_polls_triggers_last_data( $data ) {
13499 global $wpdb;
13500
13501 $context = [];
13502 $context['response_type'] = 'sample';
13503
13504 $poll = [
13505 'poll_id' => 1,
13506 'question' => 'Which skills are you interested to learn?',
13507 'answers' => 'Web Development, Graphic Designing, Content Writing, Digital Marketing',
13508 'start_date' => '2023-08-29 17:19:13',
13509 'end_date' => 'Not set',
13510 'selected_answers' => 'Content Writing, Web Development',
13511 'selected_answer_id' => 2,
13512 ];
13513
13514 $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" );
13515
13516 if ( ! empty( $poll_data ) ) {
13517 $poll = WpPolls::get_poll_context( (string) $poll_data->answer_id, (int) $poll_data->poll_id );
13518 $poll['selected_answer_id'] = (int) $poll_data->answer_id;
13519
13520 $context['response_type'] = 'live';
13521 }
13522
13523 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
13524
13525 if ( 'poll_submitted' === $term ) {
13526 unset( $poll['selected_answer_id'] );
13527 }
13528
13529 $context['pluggable_data'] = $poll;
13530
13531 return $context;
13532 }
13533
13534 /**
13535 * Get ACF Custom fields list.
13536 *
13537 * @param array $data data.
13538 *
13539 * @return array
13540 */
13541 public function search_acf_post_field_list( $data ) {
13542
13543 $post_id = $data['dynamic']['wp_post'];
13544 $selected_post_type = $data['dynamic']['wp_post_type'];
13545
13546 if ( -1 === $post_id ) {
13547 $args = [
13548 'numberposts' => 1,
13549 'fields' => 'ids',
13550 'orderby' => 'rand',
13551 'post_type' => $selected_post_type,
13552 ];
13553 $posts = get_posts( $args );
13554 $post_id = $posts[0];
13555 }
13556
13557 $args = [
13558 'post_id' => $post_id,
13559 ];
13560 if ( ! is_numeric( $post_id ) ) {
13561 $args = [
13562 'post_type' => $post_id,
13563 ];
13564 }
13565 $options = [];
13566 if ( function_exists( 'acf_get_field_groups' ) ) {
13567 $field_groups_collection = acf_get_field_groups( $args );
13568 foreach ( $field_groups_collection as $field_group ) {
13569 if ( function_exists( 'acf_get_fields' ) ) {
13570 $field_groups[] = acf_get_fields( $field_group['key'] );
13571 }
13572 }
13573
13574 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
13575 foreach ( $field_groups as $field_groups ) {
13576 foreach ( $field_groups as $field_group ) {
13577 $options[] = [
13578 'value' => $field_group['name'],
13579 'label' => ! empty( $field_group['label'] ) ? $field_group['label'] : $field_group['name'],
13580 ];
13581 }
13582 }
13583 }
13584 }
13585
13586 return [
13587 'options' => $options,
13588 'hasMore' => false,
13589 ];
13590 }
13591
13592 /**
13593 * Get ACF Custom fields list.
13594 *
13595 * @param array $data data.
13596 *
13597 * @return array
13598 */
13599 public function search_acf_user_field_list( $data ) {
13600
13601 if ( ! function_exists( 'acf_get_fields' ) ) {
13602 return [];
13603 }
13604 if ( ! function_exists( 'acf_get_field_groups' ) ) {
13605 return [];
13606 }
13607 $groups_user_form = [];
13608 $options = [];
13609 if ( function_exists( 'acf_get_field_groups' ) ) {
13610 $field_groups = acf_get_field_groups();
13611 foreach ( $field_groups as $group ) {
13612 if ( ! empty( $group['location'] ) ) {
13613 foreach ( $group['location'] as $locations ) {
13614 foreach ( $locations as $location ) {
13615 if ( 'user_form' === $location['param'] || 'user_role' === $location['param'] || 'current_user' === $location['param'] || 'current_user_role' === $location['param'] ) {
13616 $groups_user_form[] = $group;
13617 }
13618 }
13619 }
13620 }
13621 }
13622
13623 if ( empty( $groups_user_form ) ) {
13624 return [];
13625 }
13626
13627 $key_values = array_map(
13628 function ( $item ) {
13629 return $item['key'];
13630 },
13631 $groups_user_form
13632 );
13633 $unique_keys = array_unique( $key_values );
13634 $unique_array = array_intersect_key( $groups_user_form, $unique_keys );
13635
13636 foreach ( $unique_array as $group ) {
13637 if ( function_exists( 'acf_get_fields' ) ) {
13638 $group_fields = acf_get_fields( $group['key'] );
13639 }
13640 if ( ! empty( $group_fields ) ) {
13641 foreach ( $group_fields as $field ) {
13642 $options[] = [
13643 'value' => $field['name'],
13644 'label' => $field['label'],
13645 ];
13646
13647 }
13648 }
13649 }
13650 }
13651
13652 return [
13653 'options' => $options,
13654 'hasMore' => false,
13655 ];
13656 }
13657
13658 /**
13659 * Get ACF Custom fields list.
13660 *
13661 * @param array $data data.
13662 *
13663 * @return array
13664 */
13665 public function search_acf_options_field_list( $data ) {
13666
13667 if ( ! function_exists( 'acf_get_fields' ) ) {
13668 return [];
13669 }
13670 if ( ! function_exists( 'acf_get_field_groups' ) ) {
13671 return [];
13672 }
13673 $groups_options_form = [];
13674 $options = [];
13675 if ( function_exists( 'acf_get_field_groups' ) ) {
13676 $field_groups = acf_get_field_groups();
13677 foreach ( $field_groups as $group ) {
13678 if ( ! empty( $group['location'] ) ) {
13679 foreach ( $group['location'] as $locations ) {
13680 foreach ( $locations as $location ) {
13681 if ( 'options_page' === $location['param'] ) {
13682 $groups_options_form[] = $group;
13683 }
13684 }
13685 }
13686 }
13687 }
13688 if ( empty( $groups_options_form ) ) {
13689 return [];
13690 }
13691 $key_values = array_map(
13692 function ( $item ) {
13693 return $item['key'];
13694 },
13695 $groups_options_form
13696 );
13697 $unique_keys = array_unique( $key_values );
13698 $unique_array = array_intersect_key( $groups_options_form, $unique_keys );
13699 foreach ( $unique_array as $group ) {
13700 if ( function_exists( 'acf_get_fields' ) ) {
13701 $group_fields = acf_get_fields( $group['key'] );
13702 }
13703 if ( ! empty( $group_fields ) ) {
13704 foreach ( $group_fields as $field ) {
13705 $options[] = [
13706 'value' => $field['name'],
13707 'label' => $field['label'],
13708 ];
13709
13710 }
13711 }
13712 }
13713 }
13714
13715 return [
13716 'options' => $options,
13717 'hasMore' => false,
13718 ];
13719 }
13720
13721 /**
13722 * Search Last Updated Field Data for ACF.
13723 *
13724 * @param array $data data.
13725 * @return array
13726 */
13727 public function search_acf_post_field_data( $data ) {
13728 $context = [];
13729
13730 $field = ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
13731
13732 $post_type = $data['filter']['wp_post_type']['value'];
13733 $post = $data['filter']['wp_post']['value'];
13734
13735 if ( -1 === $post ) {
13736 $args = [
13737 'numberposts' => 1,
13738 'fields' => 'ids',
13739 'orderby' => 'rand',
13740 'post_type' => $post_type,
13741 ];
13742 $posts = get_posts( $args );
13743 $post = $posts[0];
13744 }
13745 if ( -1 === $field ) {
13746 $args = [
13747 'post_id' => $post,
13748 ];
13749 if ( function_exists( 'acf_get_field_groups' ) ) {
13750 $field_groups_collection = acf_get_field_groups( $args );
13751 }
13752 if ( ! empty( $field_groups_collection ) ) {
13753 foreach ( $field_groups_collection as $field_group ) {
13754 if ( function_exists( 'acf_get_fields' ) ) {
13755 $field_groups[] = acf_get_fields( $field_group['key'] );
13756 }
13757 }
13758 }
13759 $fields = [];
13760 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
13761 foreach ( $field_groups as $field_groups ) {
13762 $fields[] = $field_groups;
13763 }
13764 }
13765 if ( ! empty( $fields ) ) {
13766 $random_key = array_rand( $fields[0] );
13767 $field_key = $fields[0][ $random_key ];
13768 $field = $field_key['name'];
13769 } else {
13770 $result = '';
13771 }
13772 } else {
13773 $field = $data['filter']['field_id']['value'];
13774 }
13775 if ( function_exists( ( 'get_field' ) ) ) {
13776 $result = get_field( $field, $post );
13777 }
13778
13779 $response = [];
13780 if ( ! empty( $result ) ) {
13781 $post_fields = [];
13782 if ( function_exists( 'get_fields' ) ) {
13783 $post_fields = get_fields( $post );
13784 }
13785 $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 ) ] );
13786 $response['response_type'] = 'live';
13787 } else {
13788 $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 );
13789 }
13790
13791 return $response;
13792 }
13793
13794 /**
13795 * Search Last Updated User Field Data ACF.
13796 *
13797 * @param array $data data.
13798 * @return array
13799 */
13800 public function search_acf_user_field_data( $data ) {
13801 global $wpdb;
13802
13803 $context = [];
13804
13805 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
13806
13807 if ( -1 === $field ) {
13808 $groups_user_form = [];
13809 if ( function_exists( 'acf_get_field_groups' ) ) {
13810 $field_groups = acf_get_field_groups();
13811 }
13812 if ( ! empty( $field_groups ) ) {
13813 foreach ( $field_groups as $group ) {
13814 if ( ! empty( $group['location'] ) ) {
13815 foreach ( $group['location'] as $locations ) {
13816 foreach ( $locations as $location ) {
13817 if ( 'user_form' === $location['param'] || 'user_role' === $location['param'] || 'current_user' === $location['param'] || 'current_user_role' === $location['param'] ) {
13818 $groups_user_form[] = $group;
13819 }
13820 }
13821 }
13822 }
13823 }
13824 $field_groups = $groups_user_form;
13825 }
13826 if ( empty( $field_groups ) ) {
13827 $result = '';
13828 }
13829 $fields = [];
13830 if ( ! empty( $field_groups ) ) {
13831 foreach ( $field_groups as $group ) {
13832 if ( function_exists( 'acf_get_fields' ) ) {
13833 $group_fields = acf_get_fields( $group['key'] );
13834 }
13835 if ( ! empty( $group_fields ) ) {
13836 foreach ( $group_fields as $field ) {
13837 $fields[] = $group_fields;
13838 }
13839 }
13840 }
13841 }
13842 if ( ! empty( $fields ) ) {
13843 $random_key = array_rand( $fields );
13844 $field = $random_key;
13845 } else {
13846 $result = '';
13847 }
13848 } else {
13849 $field = $data['filter']['field_id']['value'];
13850 }
13851 $users = get_users(
13852 [
13853 'fields' => 'ID',
13854 'meta_key' => $field,
13855 ]
13856 );
13857
13858 if ( ! empty( $users ) ) {
13859 $user_random_key = array_rand( $users );
13860 $user_id = $user_random_key;
13861 if ( function_exists( 'get_field' ) ) {
13862 $result = get_field( $field, 'user_' . $users[ $user_id ] );
13863 }
13864 $response = [];
13865 if ( ! empty( $result ) ) {
13866 $context = [
13867 'field_id' => $field,
13868 $field => $result,
13869 'user' => WordPress::get_user_context( $users[ $user_id ] ),
13870 ];
13871 $response['pluggable_data'] = $context;
13872 $response['response_type'] = 'live';
13873 } else {
13874 $response = json_decode(
13875 '{
13876 "response_type": "sample",
13877 "pluggable_data": {
13878 "field_id": "gender",
13879 "user": {
13880 "wp_user_id": 114,
13881 "user_login": "test",
13882 "display_name": "test",
13883 "user_firstname": "test",
13884 "user_lastname": "test",
13885 "user_email": "test@test.com",
13886 "user_role": [ "subscriber" ]
13887 }
13888 }
13889 }',
13890 true
13891 );
13892 }
13893 } else {
13894 $response = json_decode(
13895 '{
13896 "response_type": "sample",
13897 "pluggable_data": {
13898 "field_id": "gender",
13899 "user": {
13900 "wp_user_id": 114,
13901 "user_login": "test",
13902 "display_name": "test",
13903 "user_firstname": "test",
13904 "user_lastname": "test",
13905 "user_email": "test@test.com",
13906 "user_role": [ "subscriber" ]
13907 }
13908 }
13909 }',
13910 true
13911 );
13912 }
13913
13914 return $response;
13915 }
13916
13917 /**
13918 * Search Last Updated Options Field Data ACF.
13919 *
13920 * @param array $data data.
13921 * @return array
13922 */
13923 public function search_acf_options_field_data( $data ) {
13924 global $wpdb;
13925 $context = [];
13926 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
13927
13928 if ( -1 === $field ) {
13929 $groups_options_form = [];
13930 if ( function_exists( 'acf_get_field_groups' ) ) {
13931 $field_groups = acf_get_field_groups();
13932 }
13933 if ( ! empty( $field_groups ) ) {
13934 foreach ( $field_groups as $group ) {
13935 if ( ! empty( $group['location'] ) ) {
13936 foreach ( $group['location'] as $locations ) {
13937 foreach ( $locations as $location ) {
13938 if ( 'options_page' === $location['param'] ) {
13939 $groups_options_form[] = $group;
13940 }
13941 }
13942 }
13943 }
13944 }
13945 }
13946 if ( empty( $groups_options_form ) ) {
13947 $result = '';
13948 }
13949 $key_values = array_map(
13950 function ( $item ) {
13951 return $item['key'];
13952 },
13953 $groups_options_form
13954 );
13955 $unique_keys = array_unique( $key_values );
13956 $unique_array = array_intersect_key( $groups_options_form, $unique_keys );
13957 $fields = [];
13958 if ( ! empty( $unique_array ) ) {
13959 foreach ( $unique_array as $group ) {
13960 if ( function_exists( 'acf_get_fields' ) ) {
13961 $group_fields = acf_get_fields( $group['key'] );
13962 }
13963 if ( ! empty( $group_fields ) ) {
13964 foreach ( $group_fields as $field ) {
13965 $fields[] = $group_fields;
13966 }
13967 }
13968 }
13969 }
13970 if ( ! empty( $fields ) ) {
13971 $random_key = array_rand( $fields );
13972 $field = $random_key;
13973 } else {
13974 $result = '';
13975 }
13976 } else {
13977 $field = $data['filter']['field_id']['value'];
13978 }
13979 if ( function_exists( 'get_field' ) ) {
13980 $option_value = get_field( $field, 'option' );
13981 }
13982 if ( ! empty( $option_value ) ) {
13983 if ( function_exists( 'acf_get_field' ) ) {
13984 $options_fields = acf_get_field( $field );
13985 if ( function_exists( 'acf_maybe_get' ) ) {
13986 $options_page = acf_maybe_get( $options_fields, 'parent' );
13987 }
13988 }
13989 $context = [
13990 'field_id' => $field,
13991 $field => $option_value,
13992 ];
13993 $response['pluggable_data'] = $context;
13994 $response['response_type'] = 'live';
13995 } else {
13996 $response = json_decode(
13997 '{
13998 "response_type": "sample",
13999 "pluggable_data": {
14000 "field_id": "optionpage",
14001 "optionpage": "newoption"
14002 }
14003 }',
14004 true
14005 );
14006 }
14007 return $response;
14008 }
14009
14010 /**
14011 * Get WP Fusion Tags list.
14012 *
14013 * @param array $data data.
14014 *
14015 * @return array
14016 */
14017 public function search_wp_fusion_tag_list( $data ) {
14018
14019 if ( ! function_exists( 'wp_fusion' ) ) {
14020 return [];
14021 }
14022
14023 $options = [];
14024 $tags = wp_fusion()->settings->get( 'available_tags' );
14025
14026 if ( $tags ) {
14027 foreach ( $tags as $t_id => $tag ) {
14028 if ( is_array( $tag ) && isset( $tag['label'] ) ) {
14029 $options[] = [
14030 'value' => $t_id,
14031 'label' => $tag['label'],
14032 ];
14033 } else {
14034 $options[] = [
14035 'value' => $t_id,
14036 'label' => $tag,
14037 ];
14038 }
14039 }
14040 }
14041
14042 return [
14043 'options' => $options,
14044 'hasMore' => false,
14045 ];
14046 }
14047
14048 /**
14049 * Get list of events for Modern Events Calendar.
14050 *
14051 * @param array $data data.
14052 * @return array
14053 */
14054 public function search_mec_events_list( $data ) {
14055 $page = $data['page'];
14056 $limit = Utilities::get_search_page_limit();
14057 $offset = $limit * ( $page - 1 );
14058
14059 $args = [
14060 'post_type' => 'mec-events',
14061 'post_status' => [ 'publish', 'private' ],
14062 'posts_per_page' => -1,
14063 ];
14064 $loop = new WP_Query( $args );
14065 $events_count = count( $loop->posts );
14066
14067 $args = [
14068 'post_type' => 'mec-events',
14069 'posts_per_page' => $limit,
14070 'offset' => $offset,
14071 'orderby' => 'title',
14072 'order' => 'ASC',
14073 'post_status' => [ 'publish', 'private' ],
14074 ];
14075
14076 $loop = new WP_Query( $args );
14077 $events = $loop->posts;
14078
14079 $options = [];
14080 if ( ! empty( $events ) ) {
14081 foreach ( $events as $event ) {
14082 if ( isset( $event->ID ) ) {
14083 $options[] = [
14084 'label' => get_the_title( $event ),
14085 'value' => $event->ID,
14086 ];
14087 }
14088 }
14089 }
14090
14091 return [
14092 'options' => $options,
14093 'hasMore' => $events_count > $limit && $events_count > $offset,
14094 ];
14095 }
14096
14097 /**
14098 * Search tickets of MEC events.
14099 *
14100 * @param array $data data.
14101 * @return array
14102 */
14103 public function search_mec_event_tickets( $data ) {
14104 $options = [];
14105 $event_id = $data['dynamic'];
14106
14107 $event_tickets = get_post_meta( $event_id, 'mec_tickets', true );
14108
14109 if ( ! empty( $event_tickets ) && is_array( $event_tickets ) ) {
14110 foreach ( $event_tickets as $ticket_id => $event_ticket ) {
14111 if ( isset( $event_ticket['name'] ) ) {
14112 $options[] = [
14113 'label' => $event_ticket['name'],
14114 'value' => $ticket_id,
14115 ];
14116 }
14117 }
14118 }
14119
14120 return [
14121 'options' => $options,
14122 'hasMore' => false,
14123 ];
14124 }
14125
14126 /**
14127 * Get last data for trigger.
14128 *
14129 * @param array $data data.
14130 * @return array
14131 */
14132 public function search_mec_triggers_last_data( $data ) {
14133 global $wpdb;
14134
14135 $context = [];
14136 $context['response_type'] = 'sample';
14137
14138 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
14139
14140 if ( 'new_event' === $term ) {
14141 $post_id = $wpdb->get_var( "SELECT post_id FROM {$wpdb->prefix}mec_events ORDER BY id DESC LIMIT 1" );
14142
14143 $live_data = [
14144 'post_id' => $post_id,
14145 'event' => get_post_meta( $post_id ),
14146 'post' => get_post( $post_id ),
14147 ];
14148
14149 if ( ! empty( $post_id ) ) {
14150 $context['pluggable_data'] = $live_data;
14151 $context['response_type'] = 'live';
14152 } else {
14153 $context['pluggable_data'] = [
14154 'post_id' => 272,
14155 'event' => [
14156 'mec_color' => [ 'e14d43' ],
14157 '_thumbnail_id' => [ 229 ],
14158 'mec_location_id' => [ 49 ],
14159 'mec_dont_show_map' => [ 0 ],
14160 'mec_organizer_id' => [ 53 ],
14161 'mec_start_date' => [ '2025-04-01' ],
14162 'mec_start_time_hour' => [ 8 ],
14163 'mec_start_time_minutes' => [ 0 ],
14164 'mec_start_time_ampm' => [ 'AM' ],
14165 'mec_start_day_seconds' => [ 32400 ],
14166 'mec_start_datetime' => [ '2025-04-01 08:00 AM' ],
14167 'mec_end_date' => [ '2025-04-01' ],
14168 '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";}' ],
14169 'mec_end_time_hour' => [ 6 ],
14170 'mec_end_time_minutes' => [ 0 ],
14171 'mec_end_time_ampm' => [ 'PM' ],
14172 'mec_end_day_seconds' => [ 68400 ],
14173 'mec_end_datetime' => [ '2025-04-01 06:00 PM' ],
14174 'mec_allday' => [ 1 ],
14175 'mec_fees' => [ 'a:0:{}' ],
14176 'mec_op' => [ 'a:0:{}' ],
14177 'mec_repeat' => [ 'a:0:{}' ],
14178 'mec_booking' => [ 'a:0:{}' ],
14179 'mec_comment' => [ '' ],
14180 'mec_in_days' => [ '' ],
14181 'mec_tickets' => [ 'a:0:{}' ],
14182 'mec_reg_fields' => [ 'a:0:{}' ],
14183 'mec_hourly_schedules' => [ 'a:0:{}' ],
14184 'mec_ticket_variations' => [ 'a:0:{}' ],
14185 'mec_hide_time' => [ 0 ],
14186 'mec_hide_end_time' => [ 0 ],
14187 'mec_repeat_status' => [ 0 ],
14188 'mec_fees_global_inheritance' => [ 1 ],
14189 'mec_ticket_variations_global_inheritance' => [ 1 ],
14190 'mec_reg_fields_global_inheritance' => [ 1 ],
14191 'mec_event_date_submit' => [ '20250331042811' ],
14192 'mec_new_event_notif_sent' => [ 1 ],
14193 ],
14194 'post' => [
14195 'ID' => 272,
14196 'post_author' => 1,
14197 'post_date' => '2025-03-31 04:28:09',
14198 'post_date_gmt' => '2025-03-31 04:28:09',
14199 'post_content' => '<p>Sample Event</p>',
14200 'post_title' => 'Sample Event',
14201 'post_status' => 'publish',
14202 'comment_status' => 'open',
14203 'ping_status' => 'closed',
14204 'post_name' => 'sample-event',
14205 'guid' => 'http://example.local/events/sample-event/',
14206 'post_type' => 'mec-events',
14207 'comment_count' => 0,
14208 ],
14209 ];
14210 $context['response_type'] = 'sample';
14211 }
14212
14213 return $context;
14214 }
14215
14216 $where = '';
14217 if ( 'cancelled' === $term ) {
14218 $where = 'WHERE verified = -1';
14219 } elseif ( 'confirmed' === $term ) {
14220 $where = 'WHERE confirmed = 1';
14221 } elseif ( 'pending' === $term ) {
14222 $where = 'WHERE confirmed = 0';
14223 }
14224
14225 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
14226
14227 if ( -1 !== $event_id ) {
14228 if ( ! empty( $where ) ) {
14229 $where .= ' AND event_id = ' . $event_id;
14230 } else {
14231 $where = 'WHERE event_id = ' . $event_id;
14232 }
14233 }
14234
14235 $event_data = $wpdb->get_row( "SELECT booking_id FROM {$wpdb->prefix}mec_bookings $where ORDER BY id DESC LIMIT 1" ); // @phpcs:ignore
14236
14237 if ( ! empty( $event_data ) ) {
14238 $event = ModernEventsCalendar::get_event_context( (int) $event_data->booking_id );
14239 $context['response_type'] = 'live';
14240 } else {
14241 $event = [
14242 'event_id' => 1,
14243 'title' => 'Sample Event',
14244 'description' => 'Description of the sample event.',
14245 'categories' => 'New, Sample',
14246 'start_date' => 'September 13, 2023',
14247 'start_time' => '8:00 AM',
14248 'end_date' => 'September 13, 2023',
14249 'end_time' => '11:00 AM',
14250 'location' => 'City Hall',
14251 'organizer' => 'John Doe',
14252 'cost' => '5000',
14253 'featured_image_id' => 1,
14254 'featured_image_url' => 'https://example.com/wp-content/uploads/2022/12/Screenshot_20221127_021332.png',
14255 'tickets' => [
14256 [
14257 'id' => 1,
14258 'name' => 'Silver',
14259 'description' => 'Standard seat with reasonable price.',
14260 'price' => '300',
14261 'price_label' => 'USD',
14262 'limit' => '20',
14263 ],
14264 [
14265 'id' => 2,
14266 'name' => 'Premium',
14267 'description' => 'VIP seat with high price.',
14268 'price' => '500',
14269 'price_label' => 'USD',
14270 'limit' => '10',
14271 ],
14272 ],
14273 'attendees' => [
14274 [
14275 'id' => 1,
14276 'email' => 'johndoe@test.com',
14277 'name' => 'John Doe',
14278 ],
14279 [
14280 'id' => 2,
14281 'email' => 'adamsmith@test.com',
14282 'name' => 'Adam Smith',
14283 ],
14284 ],
14285 'booking' => [
14286 'title' => 'johndoe@test.com - John Doe',
14287 'transaction_id' => 'RSH59404',
14288 'amount_payable' => '800',
14289 'price' => '800',
14290 'time' => '2023-09-07 06:40:32',
14291 'payment_gateway' => 'Manual Pay',
14292 'confirmation_status' => 'Pending',
14293 'verification_status' => 'Verified',
14294 'attendees_count' => 2,
14295 ],
14296 ];
14297 }
14298
14299 $context['pluggable_data'] = $event;
14300 return $context;
14301 }
14302
14303 /**
14304 * Get form list Contact Form 7.
14305 *
14306 * @param array $data data.
14307 *
14308 * @return array
14309 */
14310 public function search_contact_form7_list( $data ) {
14311
14312 $page = $data['page'];
14313 $limit = Utilities::get_search_page_limit();
14314 $offset = $limit * ( $page - 1 );
14315
14316 $posts = get_posts(
14317 [
14318 'post_type' => 'wpcf7_contact_form',
14319 'posts_per_page' => $limit,
14320 'offset' => $offset,
14321 ]
14322 );
14323
14324 $all_posts = get_posts(
14325 [
14326 'post_type' => 'wpcf7_contact_form',
14327 'posts_per_page' => -1,
14328 ]
14329 );
14330
14331 $posts_count = count( $all_posts );
14332
14333 $options = [];
14334
14335 if ( ! empty( $posts ) ) {
14336 foreach ( $posts as $post ) {
14337 $options[] = [
14338 'label' => get_the_title( $post->ID ),
14339 'value' => $post->ID,
14340 ];
14341 }
14342 }
14343
14344 return [
14345 'options' => $options,
14346 'hasMore' => $posts_count > $limit && $posts_count > $offset,
14347 ];
14348 }
14349
14350 /**
14351 * Get Thrive Leads form list
14352 *
14353 * @param array $data data.
14354 *
14355 * @return array
14356 */
14357 public function search_thrive_leads_forms_list( $data ) {
14358 $options = [];
14359
14360 $lg_ids = get_posts(
14361 [
14362 'post_type' => '_tcb_form_settings',
14363 'fields' => 'id=>parent',
14364 'posts_per_page' => -1,
14365 'post_status' => 'any',
14366 ]
14367 );
14368
14369 if ( function_exists( 'tve_leads_get_form_variations' ) ) {
14370 foreach ( $lg_ids as $lg_id => $lg_parent ) {
14371 $variations = tve_leads_get_form_variations( $lg_parent );
14372 foreach ( $variations as $variation ) {
14373 $options[] = [
14374 'label' => $variation['post_title'],
14375 'value' => $lg_parent,
14376 ];
14377 }
14378 }
14379 }
14380
14381 return [
14382 'options' => array_unique( $options ),
14383 'hasMore' => false,
14384 ];
14385 }
14386
14387 /**
14388 * Get Store Engine Products list
14389 *
14390 * @param array $data data.
14391 *
14392 * @return array
14393 */
14394 public function search_store_engine_products( $data ) {
14395 $options = [];
14396
14397 if ( ! class_exists( 'StoreEngine' ) ) {
14398 return $options;
14399 }
14400
14401 $products = get_posts(
14402 [
14403 'post_type' => 'storeengine_product',
14404 'post_status' => 'publish',
14405 'posts_per_page' => -1,
14406 'orderby' => 'title',
14407 'order' => 'ASC',
14408 ]
14409 );
14410
14411 foreach ( $products as $product ) {
14412 $current_price = get_post_meta( $product->ID, '_storeengine_product_price', true );
14413
14414 $price_display = '';
14415 if ( ! empty( $current_price ) && is_numeric( $current_price ) ) {
14416 $price_display = ' - $' . number_format( floatval( $current_price ), 2 );
14417 }
14418
14419 $id_display = ' (ID: ' . $product->ID . ')';
14420
14421 $options[] = [
14422 'label' => $product->post_title . $price_display . $id_display,
14423 'value' => $product->ID,
14424 ];
14425 }
14426
14427 return [
14428 'options' => $options,
14429 'hasMore' => false,
14430 ];
14431 }
14432
14433 /**
14434 * Get all countries list from StoreEngine
14435 *
14436 * @param array $data data.
14437 *
14438 * @return array
14439 */
14440 public function search_store_engine_countries( $data ) {
14441 $options = [];
14442
14443 if ( ! class_exists( 'StoreEngine\Classes\Countries' ) ) {
14444 return [
14445 'options' => $options,
14446 'hasMore' => false,
14447 ];
14448 }
14449
14450 $countries_obj = \StoreEngine\Classes\Countries::get_instance();
14451 $countries = $countries_obj->get_countries();
14452
14453 foreach ( $countries as $code => $name ) {
14454 $options[] = [
14455 'label' => $name . ' (' . $code . ')',
14456 'value' => $code,
14457 ];
14458 }
14459
14460 return [
14461 'options' => $options,
14462 'hasMore' => false,
14463 ];
14464 }
14465
14466 /**
14467 * Get Store Engine Last Data
14468 *
14469 * @param array $data data.
14470 *
14471 * @return array
14472 */
14473 public function search_se_triggers_last_data( $data ) {
14474 $context = [];
14475 global $wpdb;
14476
14477 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
14478
14479 if ( 'se_product_purchased' === $term ) {
14480 $product = $wpdb->get_row(
14481 $wpdb->prepare(
14482 "SELECT * FROM {$wpdb->prefix}posts
14483 WHERE post_type = %s
14484 AND post_status = %s
14485 ORDER BY ID DESC
14486 LIMIT 1",
14487 'storeengine_product',
14488 'publish'
14489 ),
14490 ARRAY_A
14491 );
14492
14493 if ( ! empty( $product ) ) {
14494 $product_meta = $wpdb->get_results(
14495 $wpdb->prepare(
14496 "SELECT meta_key, meta_value FROM {$wpdb->prefix}postmeta WHERE post_id = %d",
14497 $product['ID']
14498 ),
14499 ARRAY_A
14500 );
14501
14502 $custom_metas = [];
14503 foreach ( $product_meta as $meta ) {
14504 if ( strpos( $meta['meta_key'], '_storeengine_' ) === 0 ) {
14505 $clean_key = ltrim( $meta['meta_key'], '_' );
14506 $custom_metas[ $clean_key ] = maybe_unserialize( $meta['meta_value'] );
14507 }
14508 }
14509
14510 $table_exists = $wpdb->get_var(
14511 $wpdb->prepare(
14512 'SHOW TABLES LIKE %s',
14513 $wpdb->prefix . 'storeengine_orders'
14514 )
14515 );
14516
14517 $order = null;
14518 if ( $table_exists ) {
14519 $order = $wpdb->get_row(
14520 $wpdb->prepare(
14521 "SELECT * FROM {$wpdb->prefix}storeengine_orders
14522 ORDER BY id DESC
14523 LIMIT %d",
14524 1
14525 ),
14526 ARRAY_A
14527 );
14528 }
14529
14530 $customer_id = isset( $order['customer_id'] ) ? $order['customer_id'] : null;
14531 $user = null;
14532 if ( $customer_id ) {
14533 $user = get_userdata( $customer_id );
14534 } else {
14535 $customers = get_users(
14536 [
14537 'role__in' => [ 'storeengine_customer', 'customer' ],
14538 'number' => 1,
14539 'orderby' => 'ID',
14540 'order' => 'DESC',
14541 ]
14542 );
14543 if ( ! empty( $customers ) ) {
14544 $user = $customers[0];
14545 } else {
14546 $user = get_userdata( $product['post_author'] );
14547 }
14548 }
14549
14550 $product_data = [
14551 'id' => (int) $product['ID'],
14552 'guid' => $product['guid'],
14553 'post' => (int) $product['ID'],
14554 'filter' => 'raw',
14555 'pinged' => $product['pinged'],
14556 'to_ping' => $product['to_ping'],
14557 'order_id' => '',
14558 'permalink' => get_permalink( $product['ID'] ),
14559 'post_date' => $product['post_date'],
14560 'post_name' => $product['post_name'],
14561 'post_type' => $product['post_type'],
14562 'user_role' => $user ? $user->roles : [],
14563 'membership_details' => [
14564 'expiration_specific_date' => '',
14565 'expiration_fixed_date_duration' => 0,
14566 'is_enable_expiration' => 0,
14567 'content_protects_rules' => [],
14568 'membership_plan' => [
14569 'id' => null,
14570 'name' => '',
14571 'slug' => '',
14572 'status' => '',
14573 ],
14574 ],
14575 'menu_order' => (int) $product['menu_order'],
14576 'post_title' => $product['post_title'],
14577 'user_email' => $user ? $user->user_email : '',
14578 'user_login' => $user ? $user->user_login : '',
14579 'wp_user_id' => $user ? $user->ID : 0,
14580 'ping_status' => $product['ping_status'],
14581 'post_author' => (int) $product['post_author'],
14582 'post_parent' => (int) $product['post_parent'],
14583 'post_status' => $product['post_status'],
14584 'custom_metas' => $custom_metas,
14585 'display_name' => $user ? $user->display_name : '',
14586 'post_content' => $product['post_content'],
14587 'post_excerpt' => $product['post_excerpt'],
14588 'comment_count' => (int) $product['comment_count'],
14589 'post_date_gmt' => $product['post_date_gmt'],
14590 'post_modified' => $product['post_modified'],
14591 'post_password' => $product['post_password'],
14592 'user_lastname' => $user ? get_user_meta( $user->ID, 'last_name', true ) : '',
14593 'comment_status' => $product['comment_status'],
14594 'post_mime_type' => $product['post_mime_type'],
14595 'transaction_id' => '',
14596 'user_firstname' => $user ? get_user_meta( $user->ID, 'first_name', true ) : '',
14597 'user_registered' => $user ? $user->user_registered : '',
14598 'post_modified_gmt' => $product['post_modified_gmt'],
14599 'post_content_filtered' => $product['post_content_filtered'],
14600 ];
14601
14602 $integration = $wpdb->get_row(
14603 $wpdb->prepare(
14604 "SELECT integration_id, price_id FROM {$wpdb->prefix}storeengine_integrations
14605 WHERE product_id = %d AND provider = %s
14606 LIMIT 1",
14607 $product['ID'],
14608 'storeengine/membership-addon'
14609 )
14610 );
14611
14612 $membership_plan_id = isset( $integration->integration_id ) ? $integration->integration_id : null;
14613 if ( $membership_plan_id ) {
14614 $access_group = get_post( $membership_plan_id );
14615 if ( 'storeengine_groups' === $access_group && $access_group->post_type ) {
14616 $product_data['membership_details']['membership_plan'] = [
14617 'id' => (int) $membership_plan_id,
14618 'name' => $access_group->post_title,
14619 'slug' => $access_group->post_name,
14620 'status' => $access_group->post_status,
14621 ];
14622
14623 $expiration_data = get_post_meta( $membership_plan_id, '_storeengine_membership_expiration', true );
14624 if ( $expiration_data && is_array( $expiration_data ) ) {
14625 $product_data['membership_details']['expiration_specific_date'] = isset( $expiration_data['specific_date'] ) ? $expiration_data['specific_date'] : '';
14626 $product_data['membership_details']['expiration_fixed_date_duration'] = isset( $expiration_data['fixed_date_duration'] ) ? (int) $expiration_data['fixed_date_duration'] : 0;
14627 $product_data['membership_details']['is_enable_expiration'] = isset( $expiration_data['is_enable'] ) ? (int) $expiration_data['is_enable'] : 0;}
14628
14629 $content_protects = get_post_meta( $membership_plan_id, '_storeengine_membership_content_protect_types', true );
14630 if ( $content_protects && is_array( $content_protects ) ) {
14631 $product_data['membership_details']['content_protects_rules'] = $content_protects;
14632 }
14633 }
14634 }
14635
14636 if ( $order ) {
14637 $product_data['order_id'] = (int) $order['id'];
14638 $product_data['transaction_id'] = isset( $order['transaction_id'] ) ? $order['transaction_id'] : '';
14639 }
14640
14641 $context['pluggable_data'] = $product_data;
14642 $context['response_type'] = 'live';
14643
14644 } else {
14645 $sample_data = [
14646 'id' => 53,
14647 'guid' => 'http://suretriggers-wp.local/product/product-2/',
14648 'post' => 53,
14649 'filter' => 'raw',
14650 'pinged' => '',
14651 'to_ping' => '',
14652 'order_id' => 43,
14653 'permalink' => 'http://suretriggers-wp.local/product/product-2/',
14654 'post_date' => '2025-07-24 07:01:01',
14655 'post_name' => 'product-2',
14656 'post_type' => 'storeengine_product',
14657 'user_role' => [ 'storeengine_customer' ],
14658 'membership_details' => [
14659 'expiration_specific_date' => '',
14660 'expiration_fixed_date_duration' => 0,
14661 'is_enable_expiration' => 0,
14662 'content_protects_rules' => [],
14663 'membership_plan' => [
14664 'id' => null,
14665 'name' => '',
14666 'slug' => '',
14667 'status' => '',
14668 ],
14669 ],
14670 'menu_order' => 0,
14671 'post_title' => 'Product 2',
14672 'user_email' => 'mucool@gmail.com',
14673 'user_login' => 'mucool',
14674 'wp_user_id' => 12,
14675 'ping_status' => 'open',
14676 'post_author' => 1,
14677 'post_parent' => 0,
14678 'post_status' => 'publish',
14679 'custom_metas' => [
14680 'storeengine_product_hide' => '',
14681 'storeengine_product_type' => 'simple',
14682 'storeengine_product_shipping_type' => 'digital',
14683 'storeengine_product_physical_weight' => '',
14684 'storeengine_product_physical_weight_unit' => 'g',
14685 'storeengine_product_digital_auto_complete' => 1,
14686 'storeengine_product_integrated_plan_id' => 99,
14687 'storeengine_product_gallery_ids' => [],
14688 'storeengine_product_downloadable_files' => [
14689 [
14690 'id' => '9bd1194a-0406-46f3-aff3-dc539cdc791f',
14691 'name' => '',
14692 'file' => 'http://suretriggers-wp.local/wp-content/uploads/2025/07/bd0f98d1bede01622fe7f03a4f8fdc2155abafb4.jpg',
14693 'enabled' => 1,
14694 ],
14695 ],
14696 'storeengine_upsell_ids' => [],
14697 'storeengine_crosssell_ids' => [],
14698 ],
14699 'display_name' => 'mucool titirmare',
14700 'post_content' => '<p>sdfdsfsd</p>',
14701 'post_excerpt' => '',
14702 'comment_count' => 0,
14703 'post_date_gmt' => '2025-07-24 07:01:01',
14704 'post_modified' => '2025-07-24 08:14:01',
14705 'post_password' => '',
14706 'user_lastname' => 'titirmare',
14707 'comment_status' => 'open',
14708 'post_mime_type' => '',
14709 'transaction_id' => '',
14710 'user_firstname' => 'mucool',
14711 'user_registered' => '2025-07-24 08:16:48',
14712 'post_modified_gmt' => '2025-07-24 08:14:01',
14713 'post_content_filtered' => '',
14714 ];
14715
14716 $context['pluggable_data'] = $sample_data;
14717 $context['response_type'] = 'sample';
14718 }
14719 }
14720
14721 return $context;
14722 }
14723
14724
14725
14726 /**
14727 * Get list for Woocommerce Subscriptions
14728 *
14729 * @param array $data data.
14730 *
14731 * @return array
14732 */
14733 public function search_wc_subscription_variation_products( $data ) {
14734 $options = [];
14735
14736 global $wpdb;
14737
14738 if ( ! function_exists( 'wc_get_product' ) ) {
14739 return [];
14740 }
14741 $subscriptions = $wpdb->get_results(
14742 $wpdb->prepare(
14743 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
14744 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
14745 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
14746 AND posts.post_type = %s
14747 AND posts.post_status = %s
14748 UNION ALL
14749 SELECT ID, post_title FROM $wpdb->posts
14750 WHERE post_type = %s
14751 AND post_status = %s
14752 ORDER BY post_title",
14753 'product',
14754 'publish',
14755 'shop_subscription',
14756 'publish'
14757 )
14758 );
14759 $page = $data['page'];
14760 $limit = Utilities::get_search_page_limit();
14761 $offset = $limit * ( $page - 1 );
14762 $subscriptions_products = [];
14763 $subscription_products_count = 10;
14764 if ( function_exists( 'wc_get_products' ) ) {
14765 $product_query = wc_get_products(
14766 [
14767 'type' => [ 'subscription', 'variable-subscription' ],
14768 'posts_per_page' => $limit,
14769 'offset' => $offset,
14770 'paginate' => true,
14771 ]
14772 );
14773 if ( is_object( $product_query ) ) {
14774 $subscriptions_products = $product_query->products;
14775 $subscription_products_count = $product_query->total;
14776 }
14777 }
14778
14779 if ( $subscriptions ) {
14780 foreach ( $subscriptions as $product ) {
14781 $options[] = [
14782 'label' => $product->post_title . ' (#' . $product->ID . ')',
14783 'value' => (int) $product->ID,
14784 ];
14785 $product_s = wc_get_product( $product->ID );
14786 /**
14787 *
14788 * Ignore line
14789 *
14790 * @phpstan-ignore-next-line
14791 */
14792 if ( 'variable-subscription' == $product_s->product_type ) {
14793 $args = [
14794 'post_type' => 'product_variation',
14795 'post_parent' => $product->ID,
14796 'posts_per_page' => -1,
14797 'orderby' => 'ID',
14798 'order' => 'ASC',
14799 'post_status' => 'publish',
14800 ];
14801
14802 $variations = get_posts( $args );
14803
14804 foreach ( $variations as $var ) {
14805 $options[] = [
14806 'label' => $var->post_title . ' (#' . $var->ID . ')',
14807 'value' => $var->ID,
14808 ];
14809 }
14810 }
14811 }
14812 return [
14813 'options' => $options,
14814 'hasMore' => false,
14815 ];
14816 } elseif ( ! empty( $subscriptions_products ) ) {
14817 foreach ( $subscriptions_products as $product ) {
14818 $title = $product->get_name();
14819 $options[] = [
14820 'label' => $title . ' (#' . $product->get_id() . ')',
14821 'value' => $product->get_id(),
14822 ];
14823 $product_s = wc_get_product( $product->get_id() );
14824 /**
14825 *
14826 * Ignore line
14827 *
14828 * @phpstan-ignore-next-line
14829 */
14830 if ( 'variable-subscription' == $product_s->product_type ) {
14831 $args = [
14832 'post_type' => 'product_variation',
14833 'post_parent' => $product->get_id(),
14834 'posts_per_page' => -1,
14835 'orderby' => 'ID',
14836 'order' => 'ASC',
14837 'post_status' => 'publish',
14838 ];
14839
14840 $variations = get_posts( $args );
14841
14842 foreach ( $variations as $var ) {
14843 $options[] = [
14844 'label' => $var->post_title . ' (#' . $var->ID . ')',
14845 'value' => $var->ID,
14846 ];
14847 }
14848 }
14849 }
14850 return [
14851 'options' => $options,
14852 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
14853 ];
14854 } else {
14855 return [
14856 'options' => $options,
14857 'hasMore' => false,
14858 ];
14859 }
14860 }
14861
14862 /**
14863 * Get WS Forms form list
14864 *
14865 * @param array $data data.
14866 *
14867 * @return array
14868 */
14869 public function search_ws_forms_list( $data ) {
14870 $options = [];
14871 global $wpdb;
14872
14873 $forms = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wsf_form", 'ARRAY_A' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
14874
14875 foreach ( $forms as $form ) {
14876 $options[] = [
14877 'label' => $form['label'],
14878 'value' => $form['id'],
14879 ];
14880 }
14881
14882 return [
14883 'options' => $options,
14884 'hasMore' => false,
14885 ];
14886 }
14887
14888 /**
14889 * Get Learndash Achievement list
14890 *
14891 * @param array $data data.
14892 *
14893 * @return array
14894 */
14895 public function search_ld_achievements_list( $data ) {
14896 $page = $data['page'];
14897 $limit = Utilities::get_search_page_limit();
14898 $offset = $limit * ( $page - 1 );
14899
14900 $posts = get_posts(
14901 [
14902 'post_type' => 'ld-achievement',
14903 'posts_per_page' => $limit,
14904 'offset' => $offset,
14905 'post_status' => [ 'publish' ],
14906 ]
14907 );
14908
14909 $posts_count = wp_count_posts( 'ld-achievement' )->publish;
14910
14911 $options = [];
14912
14913 if ( ! empty( $posts ) ) {
14914 foreach ( $posts as $post ) {
14915 $options[] = [
14916 'label' => get_the_title( $post->ID ),
14917 'value' => $post->ID,
14918 ];
14919 }
14920 }
14921
14922 return [
14923 'options' => $options,
14924 'hasMore' => $posts_count > $limit && $posts_count > $offset,
14925 ];
14926 }
14927
14928 /**
14929 * Get Advanced Ads list
14930 *
14931 * @param array $data data.
14932 *
14933 * @return array
14934 */
14935 public function search_ads_list( $data ) {
14936 $page = $data['page'];
14937 $limit = Utilities::get_search_page_limit();
14938 $offset = $limit * ( $page - 1 );
14939
14940 $posts = get_posts(
14941 [
14942 'post_type' => 'advanced_ads',
14943 'posts_per_page' => $limit,
14944 'offset' => $offset,
14945 'post_status' => [ 'publish', 'draft' ],
14946 ]
14947 );
14948
14949 $posts_count = wp_count_posts( 'advanced_ads' )->publish;
14950
14951 $options = [];
14952
14953 if ( ! empty( $posts ) ) {
14954 foreach ( $posts as $post ) {
14955 $options[] = [
14956 'label' => get_the_title( $post->ID ),
14957 'value' => $post->ID,
14958 ];
14959 }
14960 }
14961
14962 return [
14963 'options' => $options,
14964 'hasMore' => $posts_count > $limit && $posts_count > $offset,
14965 ];
14966 }
14967
14968 /**
14969 * Advanced Ads pluggable data.
14970 *
14971 * @param array $data data.
14972 * @return array
14973 */
14974 public function search_ad_last_data( $data ) {
14975 $context = [];
14976 $args = [
14977 'post_type' => 'advanced_ads',
14978 'posts_per_page' => 1,
14979 'orderby' => 'modified',
14980 'order' => 'DESC',
14981 ];
14982
14983 if ( isset( $data['filter']['ad_status']['value'] ) ) {
14984 $post_status = $data['filter']['ad_status']['value'];
14985 $args['post_status'] = $post_status;
14986 }
14987
14988 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
14989 $post_status = $data['filter']['ad_new_status']['value'];
14990 $args['post_status'] = $post_status;
14991 }
14992
14993 if ( isset( $data['filter']['ad_id']['value'] ) ) {
14994 $post_id = $data['filter']['ad_id']['value'];
14995 if ( -1 != $post_id ) {
14996 if ( $post_id > 0 ) {
14997 $args['p'] = $post_id;
14998 }
14999 }
15000 }
15001
15002 $posts = get_posts( $args );
15003 if ( ! empty( $posts ) ) {
15004 $context['pluggable_data'] = $posts[0];
15005 $context['pluggable_data'] = WordPress::get_post_context( $posts[0]->ID );
15006 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
15007 $context['pluggable_data']['ad_new_status'] = $posts[0]->post_status;
15008 }
15009 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
15010 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
15011 }
15012 $context['pluggable_data']['ad_id'] = $posts[0]->ID;
15013 if ( isset( $data['filter']['ad_status']['value'] ) ) {
15014 $context['pluggable_data']['ad_status'] = $posts[0]->post_status;
15015 }
15016 $context['response_type'] = 'live';
15017 } else {
15018 $context['pluggable_data'] = [
15019 'ID' => 1,
15020 'post' => 1,
15021 'post_author' => 1,
15022 'post_date' => '2022-11-18 12:18:14',
15023 'post_date_gmt' => '2022-11-18 12:18:14',
15024 'post_content' => 'Ad Post Content',
15025 'post_title' => 'Ad Post',
15026 'post_excerpt' => '',
15027 'post_status' => 'draft',
15028 'comment_status' => 'open',
15029 'ping_status' => 'open',
15030 'post_password' => '',
15031 'post_name' => 'ad-post',
15032 'to_ping' => '',
15033 'pinged' => '',
15034 'post_modified' => '2022-11-18 12:18:14',
15035 'post_modified_gmt' => '2022-11-18 12:18:14',
15036 'post_content_filtered' => '',
15037 'post_parent' => 0,
15038 'guid' => 'https://example.com/ad-post/',
15039 'menu_order' => 0,
15040 'post_type' => 'advanced_ads',
15041 'post_mime_type' => '',
15042 'comment_count' => 0,
15043 'filter' => 'raw',
15044 ];
15045 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
15046 $context['pluggable_data']['ad_new_status'] = $data['filter']['ad_new_status']['value'];
15047 }
15048 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
15049 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
15050 }
15051 $context['pluggable_data']['ad_id'] = 1;
15052 if ( isset( $data['filter']['ad_status']['value'] ) ) {
15053 $context['pluggable_data']['ad_status'] = $data['filter']['ad_status']['value'];
15054 }
15055 $context['response_type'] = 'sample';
15056 }
15057
15058 return $context;
15059 }
15060
15061 /**
15062 * Get Newsletter lists
15063 *
15064 * @param array $data data.
15065 *
15066 * @return array
15067 */
15068 public function search_newsletter_lists( $data ) {
15069
15070 $options = [];
15071 if ( class_exists( '\Newsletter' ) ) {
15072
15073 $lists = \Newsletter::instance()->get_lists();
15074
15075 if ( ! empty( $lists ) ) {
15076 foreach ( $lists as $list ) {
15077 $options[] = [
15078 'label' => $list->name,
15079 'value' => 'list_' . $list->id,
15080 ];
15081 }
15082 }
15083 }
15084
15085 return [
15086 'options' => $options,
15087 'hasMore' => false,
15088 ];
15089 }
15090
15091 /**
15092 * Newsletter pluggable data.
15093 *
15094 * @param array $data data.
15095 * @return array
15096 */
15097 public function search_newsletter_last_data( $data ) {
15098 $context = [];
15099 global $wpdb;
15100
15101 $list = $data['filter']['list_id']['value'];
15102
15103 if ( -1 == $list ) {
15104 $log = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . "newsletter where status='C' ORDER BY id DESC LIMIT 1" );
15105 } else {
15106 $num = $list;
15107 $location = 1;
15108 $sql = 'SELECT * FROM ' . $wpdb->prefix . "newsletter WHERE $num = %d AND status = 'C' ORDER BY id DESC LIMIT 1";
15109 $log = $wpdb->get_results( $wpdb->prepare( $sql, $location ), ARRAY_A );// @phpcs:ignore
15110 }
15111
15112 if ( ! empty( $log ) ) {
15113 $lists_arr = get_option( 'newsletter_lists' );
15114 if ( -1 == $list ) {
15115 foreach ( $log[0] as $key => $val ) {
15116 if ( defined( 'NEWSLETTER_LIST_MAX' ) ) {
15117 for ( $i = 1; $i <= NEWSLETTER_LIST_MAX; $i++ ) {
15118 $list_key = "list_$i";
15119
15120 if ( $key == $list_key ) {
15121 if ( 1 == $val ) {
15122 $context['pluggable_data']['list_id'] = $key;
15123 if ( is_array( $lists_arr ) ) {
15124 if ( isset( $lists_arr[ $key ] ) ) {
15125 $list_name = $lists_arr[ $key ];
15126 $context['pluggable_data']['list_name'] = $list_name;
15127 }
15128 }
15129 continue;
15130 }
15131 }
15132 }
15133 }
15134 if ( 'email' == $key ) {
15135 $context['pluggable_data']['email'] = $val;
15136 }
15137 }
15138 } else {
15139 $context['pluggable_data']['list_id'] = $list;
15140 $context['pluggable_data']['email'] = $log[0]['email'];
15141 if ( is_array( $lists_arr ) ) {
15142 if ( isset( $lists_arr[ $list ] ) ) {
15143 $list_name = $lists_arr[ $list ];
15144 $context['pluggable_data']['list_name'] = $list_name;
15145 }
15146 }
15147 }
15148 $context['response_type'] = 'live';
15149 } else {
15150 $context = json_decode( '{"response_type":"sample","pluggable_data":{"list_id": "list_1","email": "johnd@mailinator.com","list_name": "Contact List"}}', true );
15151 }
15152
15153 return $context;
15154 }
15155
15156 /**
15157 * Get wpForo Forum list
15158 *
15159 * @param array $data data.
15160 *
15161 * @return array
15162 */
15163 public function search_wp_forum_list( $data ) {
15164
15165 if ( ! function_exists( 'WPF' ) ) {
15166 return [];
15167 }
15168
15169 $forums = WPF()->forum->get_forums( [ 'type' => 'forum' ] );
15170
15171 $options = [];
15172
15173 if ( ! empty( $forums ) ) {
15174 foreach ( $forums as $forum ) {
15175 $options[] = [
15176 'label' => $forum['title'],
15177 'value' => $forum['forumid'],
15178 ];
15179 }
15180 }
15181
15182 return [
15183 'options' => $options,
15184 'hasMore' => false,
15185 ];
15186 }
15187
15188 /**
15189 * Get wpForo Topic list
15190 *
15191 * @param array $data data.
15192 *
15193 * @return array
15194 */
15195 public function search_wp_topic_list( $data ) {
15196
15197 if ( ! function_exists( 'WPF' ) ) {
15198 return [];
15199 }
15200
15201 $forum_id = $data['dynamic'];
15202
15203 $topics = WPF()->topic->get_topics( [ 'forumid' => $forum_id ] );
15204
15205 $options = [];
15206
15207 if ( ! empty( $topics ) ) {
15208 foreach ( $topics as $topic ) {
15209 $options[] = [
15210 'label' => $topic['title'],
15211 'value' => $topic['topicid'],
15212 ];
15213 }
15214 }
15215
15216 return [
15217 'options' => $options,
15218 'hasMore' => false,
15219 ];
15220 }
15221
15222 /**
15223 * Get wpForo Groups list
15224 *
15225 * @param array $data data.
15226 *
15227 * @return array
15228 */
15229 public function search_wp_user_groups_list( $data ) {
15230
15231 if ( ! function_exists( 'WPF' ) ) {
15232 return [];
15233 }
15234
15235 $usergroups = WPF()->usergroup->get_usergroups();
15236
15237 $options = [];
15238
15239 if ( ! empty( $usergroups ) ) {
15240 foreach ( $usergroups as $group ) {
15241 $options[] = [
15242 'label' => $group['name'],
15243 'value' => intval( $group['groupid'] ),
15244 ];
15245 }
15246 }
15247
15248 return [
15249 'options' => $options,
15250 'hasMore' => false,
15251 ];
15252 }
15253
15254 /**
15255 * Get wpForo Reputation list
15256 *
15257 * @param array $data data.
15258 *
15259 * @return array
15260 */
15261 public function search_wp_foro_reputation_list( $data ) {
15262
15263 if ( ! function_exists( 'WPF' ) ) {
15264 return [];
15265 }
15266
15267 $levels = WPF()->member->levels();
15268 $options = [];
15269
15270 if ( ! empty( $levels ) ) {
15271 foreach ( $levels as $level ) {
15272 $options[] = [
15273 'label' => esc_attr__( 'Level', 'suretriggers' ) . ' ' . $level . ' - ' . WPF()->member->rating( $level, 'title' ),
15274 'value' => strval( $level ),
15275 ];
15276 }
15277 }
15278
15279 return [
15280 'options' => $options,
15281 'hasMore' => false,
15282 ];
15283 }
15284
15285 /**
15286 * WPForo new topic pluggable data.
15287 *
15288 * @param array $data data.
15289 * @return array
15290 */
15291 public function search_pluggables_wpforo_topic_last_data( $data ) {
15292 $context = [];
15293 global $wpdb;
15294
15295 $forum_id = $data['filter']['forum_id']['value'];
15296
15297 if ( -1 == $forum_id ) {
15298 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'wpforo_topics WHERE closed = 0 ORDER BY topicid DESC LIMIT 1', ARRAY_A );
15299 } else {
15300 $forum = $forum_id;
15301 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_topics WHERE forumid = %d AND closed = 0 ORDER BY topicid DESC LIMIT 1';
15302 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum ), ARRAY_A );// @phpcs:ignore
15303 }
15304
15305 if ( ! empty( $results ) ) {
15306 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
15307 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
15308
15309 if ( function_exists( 'WPF' ) ) {
15310 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
15311 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
15312 }
15313 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
15314 $context['response_type'] = 'live';
15315 } else {
15316 $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
15317 }
15318
15319 return $context;
15320 }
15321
15322 /**
15323 * WpForo Topic Reply pluggable data.
15324 *
15325 * @param array $data data.
15326 * @return array
15327 */
15328 public function search_pluggables_wpforo_topic_reply_last_data( $data ) {
15329 $context = [];
15330 global $wpdb;
15331
15332 $forum_id = $data['filter']['forum_id']['value'];
15333 $topic_id = $data['filter']['topic_id']['value'];
15334
15335 if ( -1 == $forum_id && -1 != $topic_id ) {
15336 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE topicid = %d ORDER BY postid DESC LIMIT 1';
15337 $results = $wpdb->get_results( $wpdb->prepare( $sql, $topic_id ), ARRAY_A );// @phpcs:ignore
15338 } elseif ( -1 != $forum_id && -1 == $topic_id ) {
15339 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d ORDER BY postid DESC LIMIT 1';
15340 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id ), ARRAY_A );// @phpcs:ignore
15341 } elseif ( -1 == $forum_id && -1 == $topic_id ) {
15342 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts ORDER BY postid DESC LIMIT 1';
15343 $results = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );// @phpcs:ignore
15344 } else {
15345 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d AND topicid = %d ORDER BY postid DESC LIMIT 1';
15346 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id, $topic_id ), ARRAY_A );// @phpcs:ignore
15347 }
15348
15349 if ( ! empty( $results ) ) {
15350 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
15351 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
15352 if ( function_exists( 'WPF' ) ) {
15353 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
15354 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
15355 $context['pluggable_data']['reply'] = WPF()->post->get_post( $results[0]['postid'] );
15356 }
15357 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
15358 $context['response_type'] = 'live';
15359 } else {
15360 $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
15361 }
15362
15363 return $context;
15364 }
15365
15366 /**
15367 * Get RafflePress Giveaways list
15368 *
15369 * @param array $data data.
15370 *
15371 * @return array
15372 */
15373 public function search_rp_giveaways_list( $data ) {
15374
15375 global $wpdb;
15376 $options = [];
15377
15378 $giveaways = $wpdb->get_results( "SELECT id,name FROM {$wpdb->prefix}rafflepress_giveaways WHERE deleted_at is null ORDER BY name ASC", ARRAY_A );
15379 foreach ( $giveaways as $giveaway ) {
15380 $options[] = [
15381 'label' => $giveaway['name'],
15382 'value' => $giveaway['id'],
15383 ];
15384 }
15385
15386 return [
15387 'options' => $options,
15388 'hasMore' => false,
15389 ];
15390 }
15391
15392 /**
15393 * Get RafflePress Giveaway Actions list
15394 *
15395 * @param array $data data.
15396 *
15397 * @return array
15398 */
15399 public function search_rp_giveaway_actions_list( $data ) {
15400
15401 global $wpdb;
15402 $options = [];
15403
15404 $giveaway = $wpdb->get_row( $wpdb->prepare( "SELECT settings FROM {$wpdb->prefix}rafflepress_giveaways WHERE id=%d", $data['dynamic'] ), ARRAY_A );
15405
15406 if ( is_array( $giveaway ) && isset( $giveaway['settings'] ) ) {
15407 $settings = json_decode( $giveaway['settings'], true );
15408
15409 if ( is_array( $settings ) && isset( $settings['entry_options'] ) ) {
15410 foreach ( $settings['entry_options'] as $action ) {
15411 $options[] = [
15412 'label' => $action['name'],
15413 'value' => $action['id'],
15414 ];
15415 }
15416 }
15417 }
15418
15419 return [
15420 'options' => $options,
15421 'hasMore' => false,
15422 ];
15423 }
15424
15425 /**
15426 * Get last data for trigger.
15427 *
15428 * @param array $data data.
15429 * @return array
15430 */
15431 public function search_raffle_press_triggers_last_data( $data ) {
15432
15433 global $wpdb;
15434
15435 $context = [];
15436 $context['response_type'] = 'sample';
15437
15438 $pluggable_data = [
15439 'giveaway_id' => 1,
15440 'giveaway_title' => 'Sample Giveaway (ID#1)',
15441 'giveaway_start_date' => 'September 20, 2023',
15442 'giveaway_end_date' => 'October 6, 2023',
15443 'giveaway_entries' => 9,
15444 'giveaway_user_count' => 3,
15445 'giveaway_status' => 'Active',
15446 'contestant_name' => 'John Doe',
15447 'contestant_email' => 'john_doe@gmail.com',
15448 'contestant_email_verified' => 'Yes',
15449 'action_id' => '0jnex',
15450 'action_name' => 'Visit us on Instagram',
15451 ];
15452
15453 $giveaway_id = isset( $data['filter']['giveaway_id'] ) ? $data['filter']['giveaway_id']['value'] : null;
15454 $action_id = isset( $data['filter']['action_id'] ) ? $data['filter']['action_id']['value'] : null;
15455
15456 $query = "SELECT contestant_id, giveaway_id, action_id, meta FROM {$wpdb->prefix}rafflepress_entries";
15457
15458 if ( $giveaway_id && -1 != $giveaway_id ) {
15459 $query .= ' WHERE giveaway_id = ' . $giveaway_id;
15460
15461 if ( $action_id ) {
15462 $query .= " AND action_id = '" . $action_id . "'";
15463 }
15464 }
15465
15466 $query .= ' ORDER BY created_at DESC LIMIT 1';
15467
15468 $giveaway_data = $wpdb->get_row( $query, ARRAY_A ); // @phpcs:ignore
15469
15470 if ( ! empty( $giveaway_data ) ) {
15471 $pluggable_data = array_merge(
15472 RafflePress::get_giveaway_context( $giveaway_data['giveaway_id'] ),
15473 RafflePress::get_contestant_context( $giveaway_data['contestant_id'] )
15474 );
15475
15476 $pluggable_data['performed_action_id'] = isset( $giveaway_data['action_id'] ) ? $giveaway_data['action_id'] : 0;
15477
15478 $giveaway_meta = isset( $giveaway_data['meta'] ) ? json_decode( $giveaway_data['meta'], true ) : [];
15479 $pluggable_data['performed_action_name'] = is_array( $giveaway_meta ) && isset( $giveaway_meta['action'] ) ? $giveaway_meta['action'] : '';
15480
15481 $context['response_type'] = 'live';
15482 }
15483
15484 $context['pluggable_data'] = $pluggable_data;
15485
15486 return $context;
15487 }
15488
15489 /**
15490 * Get last data for trigger
15491 *
15492 * @param array $data data.
15493 * @return array
15494 */
15495 public function search_woo_commerce_shipstation_triggers_last_data( $data ) {
15496 $context = [];
15497 $context['response_type'] = 'sample';
15498 $context['pluggable_data'] = [];
15499
15500 $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
15501
15502 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
15503 $condition = $data['filter']['condition_compare']['value'];
15504 $price = $data['filter']['price']['value'];
15505 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
15506
15507 if ( 'order_shipped' === $term ) {
15508 $orders = wc_get_orders(
15509 [
15510 'numberposts' => 1,
15511 'orderby' => 'date',
15512 'order' => 'DESC',
15513 'status' => 'completed',
15514 'meta_query' => [
15515 [
15516 'key' => '_shipstation_shipped_item_count',
15517 'compare' => 'EXISTS',
15518 ],
15519 ],
15520 ]
15521 );
15522 if ( is_array( $orders ) && count( $orders ) > 0 ) {
15523 $order_id = $orders[0]->get_id();
15524 $order = wc_get_order( $order_id );
15525 $product_ids = [];
15526 if ( $order && $order instanceof \WC_Order ) {
15527 $user_id = $order->get_customer_id();
15528 $items = $order->get_items();
15529 foreach ( $items as $item ) {
15530 if ( method_exists( $item, 'get_product_id' ) ) {
15531 $product_ids[] = $item->get_product_id();
15532 }
15533 }
15534
15535 foreach ( $product_ids as $product_id ) {
15536 $context['product_id'] = $product_id;
15537 }
15538 $order_context = WooCommerce::get_order_context( $order_id );
15539 $context = array_merge(
15540 isset( $order_context ) ? $order_context : [],
15541 isset( $user_id ) ? WordPress::get_user_context( $user_id ) : []
15542 );
15543 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
15544 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
15545 /**
15546 *
15547 * Ignore line
15548 *
15549 * @phpstan-ignore-next-line
15550 */
15551 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
15552 /**
15553 *
15554 * Ignore line
15555 *
15556 * @phpstan-ignore-next-line
15557 */
15558 $date = date_i18n( get_option( 'date_format' ), $timestamp );
15559 $context['ship_date'] = $date;
15560 $context['response_type'] = 'live';
15561 }
15562 }
15563
15564 $context['pluggable_data'] = $context;
15565 } elseif ( 'specific_product_order_shipped' === $term ) {
15566 if ( -1 != $product_id ) {
15567 $product_ids = [ $product_id ];
15568 $orders = wc_get_orders(
15569 [
15570 'numberposts' => 1,
15571 'orderby' => 'date',
15572 'order' => 'DESC',
15573 'status' => 'completed',
15574 'meta_query' => [
15575 [
15576 'key' => '_shipstation_shipped_item_count',
15577 'compare' => 'EXISTS',
15578 ],
15579 [
15580 'key' => '_product_id',
15581 'value' => $product_ids,
15582 'compare' => 'IN',
15583 ],
15584 ],
15585 ]
15586 );
15587 } else {
15588 $orders = wc_get_orders(
15589 [
15590 'numberposts' => 1,
15591 'orderby' => 'date',
15592 'order' => 'DESC',
15593 'status' => 'completed',
15594 'meta_query' => [
15595 [
15596 'key' => '_shipstation_shipped_item_count',
15597 'compare' => 'EXISTS',
15598 ],
15599 ],
15600 ]
15601 );
15602 }
15603 if ( is_array( $orders ) && count( $orders ) > 0 ) {
15604 $order_id = $orders[0]->get_id();
15605 $order = wc_get_order( $order_id );
15606 $productids = [];
15607 if ( $order instanceof \WC_Order ) {
15608 $user_id = $order->get_customer_id();
15609 $items = $order->get_items();
15610 foreach ( $items as $item ) {
15611 if ( method_exists( $item, 'get_product_id' ) ) {
15612 $productids[] = $item->get_product_id();
15613 }
15614 }
15615 $order_context = WooCommerce::get_order_context( $order_id );
15616 $context = array_merge(
15617 isset( $order_context ) ? $order_context : [],
15618 WordPress::get_user_context( $user_id )
15619 );
15620 foreach ( $productids as $product_id ) {
15621 $context['product_id'] = $product_id;
15622 }
15623
15624 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
15625 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
15626 /**
15627 *
15628 * Ignore line
15629 *
15630 * @phpstan-ignore-next-line
15631 */
15632 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
15633 /**
15634 *
15635 * Ignore line
15636 *
15637 * @phpstan-ignore-next-line
15638 */
15639 $date = date_i18n( get_option( 'date_format' ), $timestamp );
15640 $context['ship_date'] = $date;
15641 $context['response_type'] = 'live';
15642 }
15643 }
15644
15645 $context['pluggable_data'] = $context;
15646 } elseif ( 'specific_amount_order_shipped' === $term ) {
15647 $orders = wc_get_orders(
15648 [
15649 'numberposts' => 1,
15650 'orderby' => 'date',
15651 'order' => 'DESC',
15652 'status' => 'completed',
15653 'meta_query' => [
15654 [
15655 'key' => '_shipstation_shipped_item_count',
15656 'compare' => 'EXISTS',
15657 ],
15658 [
15659 'key' => '_order_total',
15660 'value' => $price,
15661 'compare' => $condition,
15662 ],
15663 ],
15664 ]
15665 );
15666 if ( is_array( $orders ) && count( $orders ) > 0 ) {
15667 $order_id = $orders[0]->get_id();
15668 $order = wc_get_order( $order_id );
15669 $product_ids = [];
15670 if ( $order instanceof \WC_Order ) {
15671 $user_id = $order->get_customer_id();
15672 $items = $order->get_items();
15673 foreach ( $items as $item ) {
15674 if ( method_exists( $item, 'get_product_id' ) ) {
15675 $product_ids[] = $item->get_product_id();
15676 }
15677 }
15678 $order_context = WooCommerce::get_order_context( $order_id );
15679 $context = array_merge(
15680 isset( $order_context ) ? $order_context : [],
15681 isset( $user_id ) ? WordPress::get_user_context( $user_id ) : []
15682 );
15683
15684 foreach ( $product_ids as $product_id ) {
15685 $context['product_id'] = $product_id;
15686 }
15687
15688 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
15689 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
15690 /**
15691 *
15692 * Ignore line
15693 *
15694 * @phpstan-ignore-next-line
15695 */
15696 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
15697 /**
15698 *
15699 * Ignore line
15700 *
15701 * @phpstan-ignore-next-line
15702 */
15703 $date = date_i18n( get_option( 'date_format' ), $timestamp );
15704 $context['ship_date'] = $date;
15705 $context['response_type'] = 'live';
15706 }
15707 }
15708
15709 $context['pluggable_data'] = $context;
15710 }
15711
15712 return $context;
15713 }
15714
15715 /**
15716 * Get GroundHogg Tag list
15717 *
15718 * @param array $data data.
15719 *
15720 * @return array
15721 */
15722 public function search_groundhogg_tag_list( $data ) {
15723
15724 if ( ! function_exists( 'Groundhogg\get_db' ) ) {
15725 return [];
15726 }
15727
15728 $tags = \Groundhogg\get_db( 'tags' )->query( [] );
15729 $options = [];
15730
15731 if ( ! empty( $tags ) ) {
15732 foreach ( $tags as $tag ) {
15733 $options[] = [
15734 'label' => $tag->tag_name,
15735 'value' => $tag->tag_id,
15736 ];
15737 }
15738 }
15739
15740 return [
15741 'options' => $options,
15742 'hasMore' => false,
15743 ];
15744 }
15745
15746 /**
15747 * Get WP courseware courses list
15748 *
15749 * @param array $data data.
15750 *
15751 * @return array
15752 */
15753 public function search_wpcw_courses( $data ) {
15754 $options = [];
15755
15756 $page = $data['page'];
15757 $limit = Utilities::get_search_page_limit();
15758 $offset = $limit * ( $page - 1 );
15759
15760 $args = [
15761 'post_type' => 'wpcw_course',
15762 'posts_per_page' => $limit,
15763 'offset' => $offset,
15764 'orderby' => 'title',
15765 'order' => 'ASC',
15766 'post_status' => 'publish',
15767 ];
15768
15769 $courses = get_posts( $args );
15770
15771 $course_count = wp_count_posts( 'wpcw_course' )->publish;
15772
15773 if ( ! empty( $courses ) ) {
15774 if ( is_array( $courses ) ) {
15775 foreach ( $courses as $course ) {
15776 $options[] = [
15777 'label' => $course->post_title,
15778 'value' => $course->ID,
15779 ];
15780 }
15781 }
15782 }
15783 return [
15784 'options' => $options,
15785 'hasMore' => $course_count > $limit && $course_count > $offset,
15786 ];
15787 }
15788
15789 /**
15790 * Get WP courseware courses list
15791 *
15792 * @param array $data data.
15793 *
15794 * @return array
15795 */
15796 public function search_wpcw_modules( $data ) {
15797 $options = [];
15798
15799 if ( function_exists( 'wpcw_get_modules' ) ) {
15800 $modules = wpcw_get_modules();
15801 }
15802
15803 if ( ! empty( $modules ) ) {
15804 if ( is_array( $modules ) ) {
15805 foreach ( $modules as $module ) {
15806 $options[] = [
15807 'label' => $module->module_title,
15808 'value' => $module->module_id,
15809 ];
15810 }
15811 }
15812 }
15813 return [
15814 'options' => $options,
15815 'hasMore' => false,
15816 ];
15817 }
15818
15819 /**
15820 * Get WP courseware unit list
15821 *
15822 * @param array $data data.
15823 *
15824 * @return array
15825 */
15826 public function search_wpcw_units( $data ) {
15827 $options = [];
15828
15829 $page = $data['page'];
15830 $limit = Utilities::get_search_page_limit();
15831 $offset = $limit * ( $page - 1 );
15832
15833 $args = [
15834 'post_type' => 'course_unit',
15835 'posts_per_page' => $limit,
15836 'offset' => $offset,
15837 'orderby' => 'title',
15838 'order' => 'ASC',
15839 'post_status' => 'publish',
15840 ];
15841
15842 $units = get_posts( $args );
15843
15844 $unit_count = wp_count_posts( 'course_unit' )->publish;
15845
15846 if ( ! empty( $units ) ) {
15847 if ( is_array( $units ) ) {
15848 foreach ( $units as $unit ) {
15849 $options[] = [
15850 'label' => $unit->post_title,
15851 'value' => $unit->ID,
15852 ];
15853 }
15854 }
15855 }
15856 return [
15857 'options' => $options,
15858 'hasMore' => $unit_count > $limit && $unit_count > $offset,
15859 ];
15860 }
15861
15862 /**
15863 * Search WP Courseware data.
15864 *
15865 * @param array $data data.
15866 * @return array|void
15867 */
15868 public function search_wpcw_last_data( $data ) {
15869 global $wpdb;
15870 $post_type = $data['post_type'];
15871 $trigger = $data['search_term'];
15872 $context = [];
15873
15874 if ( 'wpcw_course_completed' === $trigger ) {
15875 $post_id = $data['filter']['course_post_id']['value'];
15876 if ( -1 === $post_id ) {
15877 $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" );
15878 } else {
15879 $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 ) );
15880 }
15881 } elseif ( 'wpcw_module_completed' === $trigger ) {
15882 $post_id = $data['filter']['module_id']['value'];
15883 if ( -1 === $post_id ) {
15884 $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" );
15885 } else {
15886 $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 ) );
15887 }
15888 } elseif ( 'wpcw_unit_completed' === $trigger ) {
15889 $post_id = $data['filter']['unit_id']['value'];
15890 if ( -1 === $post_id ) {
15891 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpcw_user_progress WHERE unit_completed_status='complete' order by unit_id DESC LIMIT 1" );
15892 } else {
15893 $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 ) );
15894 }
15895 } elseif ( 'wpcw_enroll_course' === $trigger ) {
15896 $post_id = $data['filter']['course_post_id']['value'];
15897 if ( -1 === $post_id ) {
15898 $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" );
15899 } else {
15900 $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 ) );
15901 }
15902 }
15903
15904 if ( ! empty( $result ) ) {
15905
15906 switch ( $trigger ) {
15907 case 'wpcw_course_completed':
15908 $result_course_id = $result[0]->course_id;
15909 $result_user_id = $result[0]->user_id;
15910 if ( function_exists( 'wpcw_get_course' ) ) {
15911 $course = wpcw_get_course( $result_course_id );
15912 if ( is_object( $course ) ) {
15913 $course = get_object_vars( $course );
15914 }
15915 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course );
15916 }
15917 break;
15918 case 'wpcw_module_completed':
15919 $result_module_id = $result[0]->module_id;
15920 $result_user_id = $result[0]->user_id;
15921 if ( function_exists( 'wpcw_get_module' ) ) {
15922 $module = wpcw_get_module( $result_module_id );
15923 if ( is_object( $module ) ) {
15924 $module = get_object_vars( $module );
15925 }
15926 $context = array_merge( WordPress::get_user_context( $result_user_id ), $module );
15927 }
15928 break;
15929 case 'wpcw_unit_completed':
15930 $result_unit_id = $result[0]->unit_id;
15931 $result_user_id = $result[0]->user_id;
15932 if ( function_exists( 'wpcw_get_unit' ) ) {
15933 $unit = wpcw_get_unit( $result_unit_id );
15934 if ( is_object( $unit ) ) {
15935 $unit = get_object_vars( $unit );
15936 $unit['name'] = get_the_title( $result_unit_id );
15937 }
15938 $context = array_merge( WordPress::get_user_context( $result_user_id ), $unit );
15939 }
15940 break;
15941 case 'wpcw_enroll_course':
15942 $result_course_id = $result[0]->course_id;
15943 $result_user_id = $result[0]->user_id;
15944 if ( function_exists( 'WPCW_courses_getCourseDetails' ) ) {
15945 $course_detail = WPCW_courses_getCourseDetails( $result_course_id );
15946 if ( is_object( $course_detail ) ) {
15947 $course_detail = get_object_vars( $course_detail );
15948 }
15949 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course_detail );
15950 }
15951 break;
15952 default:
15953 return;
15954 }
15955 $context['pluggable_data'] = $context;
15956 $context['response_type'] = 'live';
15957 }
15958
15959 return $context;
15960
15961 }
15962
15963 /**
15964 * Get WooCommerce Order Note list.
15965 *
15966 * @param array $data data.
15967 *
15968 * @return array
15969 */
15970 public function search_note_type_list( $data ) {
15971
15972 $options = [];
15973
15974 $options[] = [
15975 'label' => 'Customer',
15976 'value' => 'customer',
15977 ];
15978 $options[] = [
15979 'label' => 'Private',
15980 'value' => 'internal',
15981 ];
15982
15983 return [
15984 'options' => $options,
15985 'hasMore' => false,
15986 ];
15987 }
15988
15989 /**
15990 * Get last data for trigger
15991 *
15992 * @param array $data data.
15993 * @return array
15994 */
15995 public function search_woo_commerce_customers_triggers_last_data( $data ) {
15996 $context = [];
15997 $context['response_type'] = 'sample';
15998 $context['pluggable_data'] = [];
15999 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
16000 global $wpdb;
16001 if ( 'customer_created' === $term ) {
16002 $customer_query = get_users(
16003 [
16004 'fields' => 'ID',
16005 'role' => 'customer',
16006 'orderby' => 'ID',
16007 'order' => 'DESC',
16008 'number' => 1,
16009 ]
16010 );
16011 $results = $customer_query;
16012 if ( ! empty( $results ) ) {
16013 $customer = new WC_Customer( $results[0] );
16014 $last_order = $customer->get_last_order();
16015 $customer_data = [
16016 'id' => $customer->get_id(),
16017 'email' => $customer->get_email(),
16018 'first_name' => $customer->get_first_name(),
16019 'last_name' => $customer->get_last_name(),
16020 'username' => $customer->get_username(),
16021 'last_order_id' => is_object( $last_order ) ? $last_order->get_id() : null,
16022 'orders_count' => $customer->get_order_count(),
16023 'total_spent' => wc_format_decimal( $customer->get_total_spent(), 2 ),
16024 'avatar_url' => $customer->get_avatar_url(),
16025 'billing_address' => [
16026 'first_name' => $customer->get_billing_first_name(),
16027 'last_name' => $customer->get_billing_last_name(),
16028 'company' => $customer->get_billing_company(),
16029 'address_1' => $customer->get_billing_address_1(),
16030 'address_2' => $customer->get_billing_address_2(),
16031 'city' => $customer->get_billing_city(),
16032 'state' => $customer->get_billing_state(),
16033 'postcode' => $customer->get_billing_postcode(),
16034 'country' => $customer->get_billing_country(),
16035 'email' => $customer->get_billing_email(),
16036 'phone' => $customer->get_billing_phone(),
16037 ],
16038 'shipping_address' => [
16039 'first_name' => $customer->get_shipping_first_name(),
16040 'last_name' => $customer->get_shipping_last_name(),
16041 'company' => $customer->get_shipping_company(),
16042 'address_1' => $customer->get_shipping_address_1(),
16043 'address_2' => $customer->get_shipping_address_2(),
16044 'city' => $customer->get_shipping_city(),
16045 'state' => $customer->get_shipping_state(),
16046 'postcode' => $customer->get_shipping_postcode(),
16047 'country' => $customer->get_shipping_country(),
16048 ],
16049 ];
16050 if ( is_object( $last_order ) && method_exists( $last_order, 'get_date_created' ) ) {
16051 $created_date = $last_order->get_date_created();
16052 if ( is_object( $created_date ) && method_exists( $created_date, 'getTimestamp' ) ) {
16053 $last_order_date = $created_date->getTimestamp();
16054 $customer_data['created_at'] = $last_order_date;
16055 $customer_data['last_order_date'] = $last_order_date;
16056 }
16057 }
16058 $order_sample_data = $customer_data;
16059 $context['response_type'] = 'live';
16060 $context['pluggable_data'] = $order_sample_data;
16061 } else {
16062 $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 );
16063 $context['pluggable_data'] = $order_sample_data;
16064 }
16065 } elseif ( 'total_spend_reach' === $term ) {
16066 $total_spend_selected = isset( $data['filter']['total_spend']['value'] ) ? $data['filter']['total_spend']['value'] : '';
16067 $customers = $wpdb->get_col(
16068 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
16069 WHERE meta_key = '_customer_user' AND meta_value > 0"
16070 );
16071 $target_customer = null;
16072 foreach ( $customers as $customer ) {
16073 $total_spend = (int) wc_get_customer_total_spent( $customer );
16074 if ( $total_spend == $total_spend_selected ) {
16075 $target_customer = $customer;
16076 break; // Exit the loop once a matching customer is found.
16077 }
16078 }
16079 if ( $target_customer ) {
16080 $new_customer = new WC_Customer( $target_customer );
16081 $new_customer_data = [
16082 'id' => $new_customer->get_id(),
16083 'email' => $new_customer->get_email(),
16084 'first_name' => $new_customer->get_first_name(),
16085 'last_name' => $new_customer->get_last_name(),
16086 'username' => $new_customer->get_username(),
16087 'order_count' => $new_customer->get_order_count(),
16088 'total_spend' => wc_format_decimal( $new_customer->get_total_spent(), 2 ),
16089 ];
16090 $context['response_type'] = 'live';
16091 $context['pluggable_data'] = $new_customer_data;
16092 } else {
16093 $sample_customer_data = [
16094 'id' => '101',
16095 'created_at' => '1680675247',
16096 'email' => 'john@d.com',
16097 'first_name' => 'John',
16098 'last_name' => 'D',
16099 'username' => 'johnd',
16100 'order_count' => '3',
16101 'total_spend' => '22.00',
16102 ];
16103 $context['pluggable_data'] = $sample_customer_data;
16104 }
16105 } elseif ( 'order_count_reach' === $term ) {
16106 $total_order_selected = isset( $data['filter']['order_count']['value'] ) ? $data['filter']['order_count']['value'] : '';
16107 $customers = $wpdb->get_col(
16108 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
16109 WHERE meta_key = '_customer_user' AND meta_value > 0"
16110 );
16111 $target_customer = null;
16112 foreach ( $customers as $customer ) {
16113 $args = [
16114 'customer_id' => $customer,
16115 'limit' => -1,
16116 'status' => [ 'wc-completed' ],
16117 ];
16118 $orders = wc_get_orders( $args );
16119 if ( ! empty( $orders ) ) {
16120 $total_order = (int) wc_get_customer_order_count( $customer );
16121 if ( $total_order == $total_order_selected ) {
16122 $target_customer = $customer;
16123 break; // Exit the loop once a matching customer is found.
16124 }
16125 }
16126 }
16127 if ( $target_customer ) {
16128 $new_customer = new WC_Customer( $target_customer );
16129 $new_customer_data = [
16130 'id' => $new_customer->get_id(),
16131 'email' => $new_customer->get_email(),
16132 'first_name' => $new_customer->get_first_name(),
16133 'last_name' => $new_customer->get_last_name(),
16134 'username' => $new_customer->get_username(),
16135 'order_count' => $new_customer->get_order_count(),
16136 ];
16137 $context['response_type'] = 'live';
16138 $context['pluggable_data'] = $new_customer_data;
16139 } else {
16140 $sample_customer_data = [
16141 'id' => '101',
16142 'created_at' => '1680675247',
16143 'email' => 'john@d.com',
16144 'first_name' => 'John',
16145 'last_name' => 'D',
16146 'username' => 'johnd',
16147 'orders_count' => '3',
16148 ];
16149 $context['pluggable_data'] = $sample_customer_data;
16150 }
16151 }
16152
16153 return $context;
16154 }
16155
16156 /**
16157 * Get Affiliate list
16158 *
16159 * @param array $data data.
16160 *
16161 * @return array
16162 */
16163 public function search_affiliate_list( $data ) {
16164 $options = [];
16165
16166 $args = [
16167 'meta_query' => [
16168 [
16169 'key' => 'wafp_is_affiliate',
16170 'value' => '1',
16171 'compare' => '=',
16172 ],
16173 ],
16174 ];
16175
16176 $affiliates = get_users( $args );
16177
16178 foreach ( $affiliates as $user ) {
16179 $options[] = [
16180 'label' => $user->display_name,
16181 'value' => $user->ID,
16182 ];
16183 }
16184 return [
16185 'options' => $options,
16186 'hasMore' => false,
16187 ];
16188 }
16189
16190 /**
16191 * Get Affiliate Source list
16192 *
16193 * @param array $data data.
16194 *
16195 * @return array
16196 */
16197 public function search_affiliate_transaction_source_list( $data ) {
16198 $options = [];
16199
16200 $sources = [
16201 'General',
16202 'MemberPress',
16203 'WooCommerce',
16204 'Easy Digital Downloads',
16205 'WPForms',
16206 'Formidable',
16207 'PayPal',
16208 ];
16209 foreach ( $sources as $source ) {
16210 $options[] = [
16211 'label' => $source,
16212 'value' => str_replace( ' ', '_', strtolower( $source ) ),
16213 ];
16214 }
16215
16216 return [
16217 'options' => $options,
16218 'hasMore' => false,
16219 ];
16220 }
16221
16222 /**
16223 * Search Easy Affiliate data.
16224 *
16225 * @param array $data data.
16226 * @return array|void
16227 */
16228 public function search_easy_affiliate_last_data( $data ) {
16229
16230 global $wpdb;
16231 $trigger = $data['search_term'];
16232 $context = [];
16233
16234 if ( 'sale_recorded' === $trigger ) {
16235 $affiliate_id = $data['filter']['affiliate_id']['value'];
16236 if ( -1 === $affiliate_id ) {
16237 $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" );
16238 } else {
16239 $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 ) );
16240 }
16241 } elseif ( 'sale_added' === $trigger ) {
16242 $orderby = 'signup_date';
16243 $order = 'DESC';
16244 $paged = 1;
16245 $search = '';
16246 $perpage = 1;
16247 /**
16248 *
16249 * Ignore line
16250 *
16251 * @phpstan-ignore-next-line
16252 */
16253 $result = \EasyAffiliate\Models\User::affiliate_list_table( $orderby, $order, $paged, $search, $perpage );
16254 if ( empty( $result['results'] ) ) {
16255 $result = [];
16256 }
16257 } elseif ( 'payout_made' === $trigger ) {
16258 $affiliate_id = $data['filter']['affiliate_id']['value'];
16259 if ( -1 === $affiliate_id ) {
16260 $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" );
16261 } else {
16262 $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 ) );
16263 }
16264 }
16265
16266 if ( ! empty( $result ) ) {
16267 switch ( $trigger ) {
16268 case 'sale_recorded':
16269 $result_affiliate_id = $result[0]->affiliate_id;
16270 /**
16271 *
16272 * Ignore line
16273 *
16274 * @phpstan-ignore-next-line
16275 */
16276 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
16277 $affiliate = get_object_vars( $affiliate->rec );
16278 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
16279 break;
16280 case 'sale_added':
16281 /**
16282 *
16283 * Ignore line
16284 *
16285 * @phpstan-ignore-next-line
16286 */
16287 $data = new \EasyAffiliate\Models\User( $result['results'][0]->ID );
16288 /**
16289 *
16290 * Ignore line
16291 *
16292 * @phpstan-ignore-next-line
16293 */
16294 $context = get_object_vars( $data->rec );
16295 break;
16296 case 'payout_made':
16297 $result_affiliate_id = $result[0]->affiliate_id;
16298 /**
16299 *
16300 * Ignore line
16301 *
16302 * @phpstan-ignore-next-line
16303 */
16304 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
16305 $affiliate = get_object_vars( $affiliate->rec );
16306 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
16307 break;
16308 default:
16309 return;
16310 }
16311 $context['pluggable_data'] = $context;
16312 $context['response_type'] = 'live';
16313 } elseif ( empty( $result ) ) {
16314 switch ( $trigger ) {
16315 case 'sale_added':
16316 $context = json_decode(
16317 '{"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": "",
16318 "city": "london","state": "","zip": "","country": "UK","tax_id_us": "","tax_id_int": "","is_affiliate": "1","is_blocked": "","blocked_message": "","referrer": "0","notes": "","unsubscribed": ""}',
16319 true
16320 );
16321 break;
16322 case 'sale_recorded':
16323 $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 );
16324 break;
16325 case 'payout_made':
16326 $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 );
16327 break;
16328 default:
16329 return;
16330 }
16331 $context['pluggable_data'] = $context;
16332 $context['response_type'] = 'sample';
16333 }
16334
16335 return $context;
16336
16337 }
16338
16339 /**
16340 * Get WooCommerce Subscriptions Coupon list
16341 *
16342 * @param array $data data.
16343 *
16344 * @return array
16345 */
16346 public function search_woo_subscription_coupon_list( $data ) {
16347 $options = [];
16348
16349 $coupon_codes = [];
16350
16351 $query = new \WP_Query(
16352 [
16353 'post_type' => 'shop_coupon',
16354 'posts_per_page' => -1,
16355 'no_found_rows' => true,
16356 'meta_query' => [
16357 [
16358 'key' => '_is_aw_coupon',
16359 'compare' => 'NOT EXISTS',
16360 ],
16361 ],
16362 ]
16363 );
16364
16365 foreach ( $query->posts as $coupon ) {
16366 /**
16367 *
16368 * Ignore line
16369 *
16370 * @phpstan-ignore-next-line
16371 */
16372 $code = wc_format_coupon_code( $coupon->post_title );
16373 $coupon_codes[ $code ] = $code;
16374 }
16375
16376 foreach ( $coupon_codes as $code ) {
16377
16378 $coupon = new \WC_Coupon( $code );
16379
16380 if ( ! in_array( $coupon->get_discount_type(), [ 'recurring_fee', 'recurring_percent' ], true ) ) {
16381 unset( $coupon_codes[ $code ] );
16382 }
16383 }
16384
16385 if ( ! empty( $coupon_codes ) ) {
16386 if ( is_array( $coupon_codes ) ) {
16387 foreach ( $coupon_codes as $coupon_code ) {
16388 $options[] = [
16389 'label' => $coupon_code,
16390 'value' => $coupon_code,
16391 ];
16392 }
16393 }
16394 }
16395 return [
16396 'options' => $options,
16397 'hasMore' => false,
16398 ];
16399 }
16400
16401 /**
16402 * Get BuddyBoss field.
16403 *
16404 * @param array $data data.
16405 *
16406 * @return array|void
16407 */
16408 public function search_bb_field_list( $data ) {
16409 global $wpdb;
16410 $options = [];
16411
16412 $xprofile_fields = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_xprofile_fields WHERE parent_id = 0 ORDER BY field_order ASC" );
16413 if ( ! empty( $xprofile_fields ) ) {
16414 foreach ( $xprofile_fields as $xprofile_field ) {
16415 $options[] = [
16416 'label' => $xprofile_field->name,
16417 'value' => $xprofile_field->id,
16418 ];
16419 }
16420 }
16421
16422 return [
16423 'options' => $options,
16424 'hasMore' => false,
16425 ];
16426 }
16427
16428 /**
16429 * Get Woocommerce Bookings Product List.
16430 *
16431 * @param array $data data.
16432 *
16433 * @return array|void
16434 */
16435 public function search_wb_bookable_product_list( $data ) {
16436 $options = [];
16437
16438 if ( ! class_exists( 'WC_Bookings_Admin' ) ) {
16439 return;
16440 }
16441
16442 $products = WC_Bookings_Admin::get_booking_products();
16443 if ( ! empty( $products ) ) {
16444 if ( is_array( $products ) ) {
16445 foreach ( $products as $product ) {
16446 $options[] = [
16447 'label' => $product->get_name(),
16448 'value' => $product->get_id(),
16449 ];
16450 $resources = $product->get_resources();
16451 if ( ! empty( $resources ) ) {
16452 foreach ( $resources as $resource ) {
16453 $options[] = [
16454 'label' => $resource->get_name(),
16455 'value' => $resource->get_id(),
16456 ];
16457 }
16458 }
16459 }
16460 }
16461 }
16462
16463 return [
16464 'options' => $options,
16465 'hasMore' => false,
16466 ];
16467 }
16468
16469 /**
16470 * Get last data for WooCommerce Booking trigger
16471 *
16472 * @param array $data data.
16473 * @return array
16474 */
16475 public function search_woo_commerce_booking_last_data( $data ) {
16476 $context = [];
16477 $context['response_type'] = 'sample';
16478 $context['pluggable_data'] = [];
16479 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
16480 global $wpdb;
16481 if ( 'booking_created' === $term ) {
16482 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' AND post_status='confirmed' order by ID DESC LIMIT 1" );
16483
16484 if ( ! empty( $results ) ) {
16485 if ( class_exists( 'WC_Booking' ) ) {
16486 $booking = new WC_Booking( $results[0]->ID );
16487 $person_counts = $booking->get_person_counts();
16488 $bookable_product_id = $booking->get_product_id();
16489 if ( method_exists( $booking, 'get_data' ) ) {
16490 $booking = $booking->get_data();
16491 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
16492 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
16493 if ( ! empty( $person_counts ) ) {
16494 $total_count = 0;
16495 foreach ( $person_counts as $key => $value ) {
16496 $total_count += $value;
16497 }
16498 $booking['total_person_counts'] = $total_count;
16499 }
16500 $booking['bookable_product'] = $bookable_product_id;
16501 $context['response_type'] = 'live';
16502 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
16503 }
16504 }
16505 } else {
16506 $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 );
16507 $context['pluggable_data'] = $booking_sample_data;
16508 }
16509 } elseif ( 'booking_status_changed' === $term ) {
16510 $to_status = isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1;
16511
16512 if ( -1 == $to_status ) {
16513 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' order by ID DESC LIMIT 1" );
16514 } else {
16515 $results = $wpdb->get_results(
16516 $wpdb->prepare(
16517 "
16518 SELECT *
16519 FROM {$wpdb->prefix}posts
16520 WHERE `post_status` = %s
16521 AND `post_type` LIKE %s
16522 ",
16523 $to_status,
16524 'wc_booking'
16525 )
16526 );
16527 }
16528
16529 if ( ! empty( $results ) ) {
16530 if ( class_exists( 'WC_Booking' ) ) {
16531 $booking = new WC_Booking( $results[0]->ID );
16532 $person_counts = $booking->get_person_counts();
16533 $bookable_product_id = $booking->get_product_id();
16534 if ( method_exists( $booking, 'get_data' ) ) {
16535 $booking = $booking->get_data();
16536 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
16537 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
16538 if ( ! empty( $person_counts ) ) {
16539 $total_count = 0;
16540 foreach ( $person_counts as $key => $value ) {
16541 $total_count += $value;
16542 }
16543 $booking['total_person_counts'] = $total_count;
16544 }
16545 $booking['bookable_product'] = $bookable_product_id;
16546 $booking['from_status'] = $data['filter']['from_status']['value'];
16547 $booking['to_status'] = $booking['status'];
16548 $context['response_type'] = 'live';
16549 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
16550 }
16551 }
16552 } else {
16553 $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 );
16554 $context['pluggable_data'] = $booking_sample_data;
16555 }
16556 }
16557 return $context;
16558 }
16559
16560 /**
16561 * Get WooCommerce Booking status list.
16562 *
16563 * @param array $data data.
16564 *
16565 * @return array
16566 */
16567 public function search_woo_booking_status_list( $data ) {
16568
16569 $options = [
16570 [
16571 'label' => 'Pending Confirmation',
16572 'value' => 'pending-confirmation',
16573 ],
16574 [
16575 'label' => 'Unpaid',
16576 'value' => 'unpaid',
16577 ],
16578 [
16579 'label' => 'Confirmed',
16580 'value' => 'confirmed',
16581 ],
16582 [
16583 'label' => 'Paid',
16584 'value' => 'paid',
16585 ],
16586 [
16587 'label' => 'Complete',
16588 'value' => 'complete',
16589 ],
16590 [
16591 'label' => 'In Cart',
16592 'value' => 'in-cart',
16593 ],
16594 [
16595 'label' => 'Cancelled',
16596 'value' => 'cancelled',
16597 ],
16598 ];
16599
16600 return [
16601 'options' => $options,
16602 'hasMore' => false,
16603 ];
16604 }
16605
16606 /**
16607 * Get Woocommerce Memberships Status List.
16608 *
16609 * @param array $data data.
16610 *
16611 * @return array|void
16612 */
16613 public function search_wc_membership_status_list( $data ) {
16614 $options = [];
16615 if ( ! function_exists( 'wc_memberships_get_user_membership_statuses' ) ) {
16616 return;
16617 }
16618
16619 $statuses = wc_memberships_get_user_membership_statuses();
16620 if ( ! empty( $statuses ) ) {
16621 if ( is_array( $statuses ) ) {
16622 foreach ( $statuses as $status => $value ) {
16623 $status = 0 === strpos( $status, 'wcm-' ) ? substr( $status, 4 ) : $status;
16624 $options[] = [
16625 'label' => $value['label'],
16626 'value' => $status,
16627 ];
16628 }
16629 }
16630 }
16631
16632 return [
16633 'options' => $options,
16634 'hasMore' => false,
16635 ];
16636 }
16637
16638 /**
16639 * Search Woocommerce Subscription data.
16640 *
16641 * @param array $data data.
16642 * @return array|void
16643 */
16644 public function search_user_subscription_last_data( $data ) {
16645
16646 $context = [];
16647 if (
16648 ! class_exists( 'WC_Subscription' ) ||
16649 ! function_exists( 'wcs_get_subscription' ) ||
16650 ! function_exists( 'wcs_get_subscriptions_for_product' ) ||
16651 ! function_exists( 'wcs_order_contains_renewal' ) ||
16652 ! function_exists( 'wcs_get_subscriptions_for_order' )
16653 ) {
16654 return;
16655 }
16656
16657 if ( 'trial_end' == $data['search_term'] ) {
16658 $subscription_id = $data['filter']['subscription']['value'];
16659 $query_args = [
16660 'post_type' => 'shop_subscription',
16661 'orderby' => 'ID',
16662 'order' => 'DESC',
16663 'post_status' => 'wc-active',
16664 'posts_per_page' => 1,
16665 'meta_query' => [
16666 [
16667 'key' => '_schedule_trial_end',
16668 'value' => gmdate( 'Y-m-d H:i:s' ),
16669 'compare' => '<=',
16670 ],
16671 ],
16672 'post__in' => [ $subscription_id ],
16673 ];
16674 $query_result = new WP_Query( $query_args );
16675 $subscription_orders = $query_result->get_posts();
16676
16677 if ( ! empty( $subscription_orders ) ) {
16678 /**
16679 *
16680 * Ignore line
16681 *
16682 * @phpstan-ignore-next-line
16683 */
16684 $subscription = wcs_get_subscription( $subscription_orders[0]->ID );
16685 $user_id = $subscription->get_user_id();
16686
16687 $items = $subscription->get_items();
16688 $product_ids = [];
16689 foreach ( $items as $item ) {
16690 $product_ids[] = $item->get_product_id();
16691 }
16692
16693 $subscription_status = $subscription->get_status();
16694 $subscription_start_date = $subscription->get_date_created();
16695
16696 $context['subscription_data'] = [
16697 'status' => $subscription_status,
16698 'start_date' => $subscription_start_date,
16699 'trial_end_date' => $subscription->get_date( 'schedule_trial_end' ),
16700 'next_payment_date' => $subscription->get_date( 'next_payment' ),
16701 'end_date' => $subscription->get_date( 'schedule_end' ),
16702 ];
16703 $context['user'] = WordPress::get_user_context( $user_id );
16704
16705 foreach ( $product_ids as $val ) {
16706 $context['subscription'] = $val;
16707 $context['subscription_name'] = get_the_title( $val );
16708 }
16709 $context['pluggable_data'] = $context;
16710 $context['response_type'] = 'live';
16711 } else {
16712 $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 );
16713 $context['pluggable_data'] = $context;
16714 $context['response_type'] = 'sample';
16715 }
16716 } elseif ( 'renewal_payment_failed' == $data['search_term'] ) {
16717 $subscription_item = $data['filter']['subscription_item']['value'];
16718 $subscription_ids = wcs_get_subscriptions_for_product( $subscription_item );
16719 $ids = [];
16720 $related = [];
16721 foreach ( $subscription_ids as $subscription ) {
16722 $ids[] = $subscription;
16723 $subscriptions = new WC_Subscription( $subscription );
16724 $related[] = $subscriptions->get_related_orders();
16725 }
16726
16727 $failed_payment_orders = [];
16728 foreach ( $related as $value ) {
16729 foreach ( $value as $val ) {
16730 if ( wcs_order_contains_renewal( $val ) ) {
16731 $order = wc_get_order( $val );
16732 if ( $order ) {
16733 /**
16734 *
16735 * Ignore line
16736 *
16737 * @phpstan-ignore-next-line
16738 */
16739 $payment_status = $order->get_status();
16740 if ( 'failed' === $payment_status || 'wc-on-hold' === $payment_status ) {
16741 $failed_payment_orders[] = $val;
16742 }
16743 }
16744 }
16745 }
16746 }
16747
16748 if ( ! empty( $failed_payment_orders ) ) {
16749 $failed = array_rand( $failed_payment_orders );
16750 $related_subscriptions = wcs_get_subscriptions_for_order( $failed_payment_orders[ $failed ] );
16751 if ( ! empty( $related_subscriptions ) ) {
16752 $sub_id = get_post_meta( $failed_payment_orders[ $failed ], '_subscription_renewal', true );
16753 $subscription = wcs_get_subscription( $sub_id );
16754
16755 $items = $subscription->get_items();
16756 $product_ids = [];
16757 foreach ( $items as $item ) {
16758 $product_ids[] = $item->get_product_id();
16759 }
16760
16761 $context['user'] = WordPress::get_user_context( $subscription->get_user_id() );
16762 foreach ( [ 'renewal' ] as $order_type ) {
16763 foreach ( $subscription->get_related_orders( 'ids', $order_type ) as $order_id ) {
16764 $context['subscription_related_order'][] = $order_id;
16765 }
16766 }
16767 foreach ( $product_ids as $val ) {
16768 $context['subscription_item'] = $val;
16769 $context['subscription_name'] = get_the_title( $val );
16770 }
16771 $context['data'] = $subscription->get_data();
16772 $context['pluggable_data'] = $context;
16773 $context['response_type'] = 'live';
16774 } else {
16775 $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 );
16776 $context['response_type'] = 'sample';
16777 }
16778 } else {
16779 $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 );
16780 $context['response_type'] = 'sample';
16781 }
16782 }
16783 return $context;
16784 }
16785
16786 /**
16787 * Get Masteriyo LMS Courses.
16788 *
16789 * @param array $data data.
16790 *
16791 * @return array
16792 */
16793 public function search_masteriyo_lms_courses( $data ) {
16794
16795 $page = $data['page'];
16796 $limit = Utilities::get_search_page_limit();
16797 $offset = $limit * ( $page - 1 );
16798
16799 $args = [
16800 'post_type' => 'mto-course',
16801 'posts_per_page' => $limit,
16802 'offset' => $offset,
16803 'orderby' => 'title',
16804 'order' => 'ASC',
16805 'post_status' => 'publish',
16806 ];
16807
16808 $courses = get_posts( $args );
16809
16810 $course_count = wp_count_posts( 'mto-course' )->publish;
16811
16812 $options = [];
16813 if ( ! empty( $courses ) ) {
16814 if ( is_array( $courses ) ) {
16815 foreach ( $courses as $course ) {
16816 $options[] = [
16817 'label' => $course->post_title,
16818 'value' => $course->ID,
16819 ];
16820 }
16821 }
16822 }
16823 return [
16824 'options' => $options,
16825 'hasMore' => $course_count > $limit && $course_count > $offset,
16826 ];
16827 }
16828
16829 /**
16830 * Get Masteriyo LMS Lessons.
16831 *
16832 * @param array $data data.
16833 *
16834 * @return array
16835 */
16836 public function search_masteriyo_lms_lessons( $data ) {
16837
16838 $course_id = $data['dynamic'];
16839 $page = $data['page'];
16840 $limit = Utilities::get_search_page_limit();
16841 $offset = $limit * ( $page - 1 );
16842 $options = [];
16843 $args = [
16844 'post_type' => 'mto-lesson',
16845 'posts_per_page' => $limit,
16846 'offset' => $offset,
16847 'orderby' => 'title',
16848 'order' => 'ASC',
16849 'post_status' => 'publish',
16850 'meta_query' => [
16851 [
16852 'key' => '_course_id',
16853 'value' => $course_id,
16854 'compare' => '=',
16855 ],
16856 ],
16857 ];
16858
16859 $lessons = get_posts( $args );
16860 $lesson_count = wp_count_posts( 'mto-lesson' )->publish;
16861 if ( ! empty( $lessons ) ) {
16862 if ( is_array( $lessons ) ) {
16863 foreach ( $lessons as $lesson ) {
16864 $options[] = [
16865 'label' => $lesson->post_title,
16866 'value' => $lesson->ID,
16867 ];
16868 }
16869 }
16870 }
16871 return [
16872 'options' => $options,
16873 'hasMore' => $lesson_count > $limit && $lesson_count > $offset,
16874 ];
16875 }
16876
16877 /**
16878 * Get Masteriyo LMS Quiz.
16879 *
16880 * @param array $data data.
16881 *
16882 * @return array
16883 */
16884 public function search_masteriyo_lms_quizs( $data ) {
16885
16886 $course_id = $data['dynamic'];
16887 $page = $data['page'];
16888 $limit = Utilities::get_search_page_limit();
16889 $offset = $limit * ( $page - 1 );
16890 $options = [];
16891 $args = [
16892 'post_type' => 'mto-quiz',
16893 'posts_per_page' => $limit,
16894 'offset' => $offset,
16895 'orderby' => 'title',
16896 'order' => 'ASC',
16897 'post_status' => 'publish',
16898 'meta_query' => [
16899 [
16900 'key' => '_course_id',
16901 'value' => $course_id,
16902 'compare' => '=',
16903 ],
16904 ],
16905 ];
16906
16907 $quizs = get_posts( $args );
16908 $quiz_count = wp_count_posts( 'mto-quiz' )->publish;
16909 if ( ! empty( $quizs ) ) {
16910 if ( is_array( $quizs ) ) {
16911 foreach ( $quizs as $quiz ) {
16912 $options[] = [
16913 'label' => $quiz->post_title,
16914 'value' => $quiz->ID,
16915 ];
16916 }
16917 }
16918 }
16919 return [
16920 'options' => $options,
16921 'hasMore' => $quiz_count > $limit && $quiz_count > $offset,
16922 ];
16923 }
16924
16925 /**
16926 * Search Masteriyo LMS data.
16927 *
16928 * @param array $data data.
16929 * @return array|void|mixed
16930 */
16931 public function search_masteriyo_lms_last_data( $data ) {
16932 global $wpdb;
16933 $post_type = $data['post_type'];
16934 $trigger = $data['search_term'];
16935 $context = [];
16936
16937 $post_id = -1;
16938 if ( 'course_completed' === $trigger ) {
16939 $post_id = $data['filter']['course_id']['value'];
16940 } elseif ( 'lesson_completed' === $trigger ) {
16941 $post_id = $data['filter']['lesson_id']['value'];
16942 } elseif ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
16943 $post_id = $data['filter']['quiz_id']['value'];
16944 }
16945
16946 if ( 'course_completed' === $trigger || 'lesson_completed' === $trigger ) {
16947 if ( -1 === $post_id ) {
16948 $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 ) );
16949 } else {
16950 $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 ) );
16951 }
16952 }
16953
16954 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
16955 if ( -1 === $post_id ) {
16956 $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 ) );
16957 } else {
16958 $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 ) );
16959 }
16960 if ( ! empty( $result ) ) {
16961 $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 ) );
16962
16963 if ( function_exists( 'masteriyo_get_quiz' ) ) {
16964 $quiz = masteriyo_get_quiz( $resultt[0]->quiz_id );
16965 if ( is_object( $quiz ) && method_exists( $quiz, 'get_pass_mark' ) ) {
16966 if ( 'quiz_completed' === $trigger && $resultt[0]->earned_marks < $quiz->get_pass_mark() ) {
16967 $result = [];
16968 } elseif ( 'quiz_failed' === $trigger && $resultt[0]->earned_marks > $quiz->get_pass_mark() ) {
16969 $result = [];
16970 }
16971 }
16972 }
16973 }
16974 }
16975
16976 if ( ! empty( $result ) ) {
16977 $result_item_id = $result[0]->item_id;
16978 $result_user_id = $result[0]->user_id;
16979 $quiz_attempt = '';
16980 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
16981 $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 ) );
16982 $quiz_attempt = $resultt[0]->id;
16983 }
16984 switch ( $trigger ) {
16985 case 'course_completed':
16986 if ( function_exists( 'masteriyo_get_course' ) ) {
16987 $course = masteriyo_get_course( $result_item_id );
16988 $context_data = array_merge(
16989 WordPress::get_user_context( $result_user_id ),
16990 $course->get_data()
16991 );
16992 $context_data['course_id'] = $result_item_id;
16993 }
16994 break;
16995 case 'lesson_completed':
16996 if ( function_exists( 'masteriyo_get_lesson' ) ) {
16997 $lesson = masteriyo_get_lesson( $result_item_id );
16998 $context_data = array_merge(
16999 WordPress::get_user_context( $result_user_id ),
17000 $lesson->get_data()
17001 );
17002 $context_data['lesson_id'] = $result_item_id;
17003 }
17004 break;
17005 case 'quiz_completed':
17006 case 'quiz_failed':
17007 if ( function_exists( 'masteriyo_get_quiz' ) ) {
17008 $quiz = masteriyo_get_quiz( $result_item_id );
17009 $context_data = WordPress::get_user_context( $result_user_id );
17010 if ( function_exists( 'masteriyo_get_quiz_attempt' ) ) {
17011 $attempt = masteriyo_get_quiz_attempt( $quiz_attempt );
17012 $context_data['quiz_id'] = $result_item_id;
17013 $context_data['course_id'] = $quiz->get_course_id();
17014 $context_data['quiz'] = $quiz->get_data();
17015 $context_data['attempt'] = $attempt->get_data();
17016 }
17017 }
17018 break;
17019 default:
17020 return;
17021 }
17022 if ( ! empty( $context_data ) ) {
17023 $context['pluggable_data'] = $context_data;
17024 $context['response_type'] = 'live';
17025 }
17026 } elseif ( empty( $result ) ) {
17027 switch ( $trigger ) {
17028 case 'course_completed':
17029 $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"} ';
17030 break;
17031 case 'lesson_completed':
17032 $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"} ';
17033 break;
17034 case 'quiz_completed':
17035 case 'quiz_failed':
17036 $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"}';
17037 break;
17038 default:
17039 return;
17040 }
17041 $context = json_decode( $sample_data, true );
17042 }
17043
17044 return $context;
17045 }
17046
17047 /**
17048 * Search learndash user added in group.
17049 *
17050 * @param array $data data.
17051 * @return array|void
17052 */
17053 public function search_pluggables_user_added_in_group( $data ) {
17054 $context = [];
17055
17056 if ( ! function_exists( 'learndash_get_groups_user_ids' ) ) {
17057 return;
17058 }
17059 $term = $data['search_term'];
17060 $group_id = $data['filter']['sfwd_group_id']['value'];
17061
17062 if ( 'user_added_group' == $term ) {
17063 if ( -1 === $group_id ) {
17064 $args = [
17065 'numberposts' => 1,
17066 'orderby' => 'rand',
17067 'post_type' => 'groups',
17068 ];
17069 $posts = get_posts( $args );
17070 $random_value = $posts[0]->ID;
17071 $group_users = learndash_get_groups_user_ids( $random_value );
17072 $group_id = $random_value;
17073 } else {
17074 $group_users = learndash_get_groups_user_ids( $group_id );
17075 }
17076 $users = get_users(
17077 [
17078 'meta_key' => 'learndash_group_' . $group_id . '_enrolled_at',
17079 'orderby' => 'meta_value',
17080 'order' => 'DESC',
17081 'number' => 1,
17082 'include' => $group_users,
17083 ]
17084 );
17085 } elseif ( 'user_removed_group' == $term ) {
17086 if ( -1 === $group_id ) {
17087 $args = [
17088 'numberposts' => 1,
17089 'orderby' => 'rand',
17090 'post_type' => 'groups',
17091 ];
17092 $posts = get_posts( $args );
17093 $random_value = $posts[0]->ID;
17094 $group_id = $random_value;
17095 }
17096 $args = [
17097 'meta_query' => [
17098 'relation' => 'AND',
17099 [
17100 'key' => 'group_' . $group_id . '_access_from',
17101 'compare' => 'NOT EXISTS',
17102 ],
17103 [
17104 'key' => 'learndash_group_' . $group_id . '_enrolled_at',
17105 'compare' => 'EXISTS',
17106 ],
17107 ],
17108 'orderby' => 'meta_value',
17109 'order' => 'DESC',
17110 'number' => 1,
17111 ];
17112 $users = get_users( $args );
17113 } elseif ( 'leader_added_group' == $term || 'leader_removed_group' == $term ) {
17114 if ( -1 === $group_id ) {
17115 $args = [
17116 'numberposts' => 1,
17117 'fields' => 'ids',
17118 'orderby' => 'rand',
17119 'post_type' => 'groups',
17120 ];
17121 $posts = get_posts( $args );
17122 $random_key = array_rand( $posts );
17123 $random_value = $posts[ $random_key ];
17124 $group_id = $random_value;
17125 }
17126 $user_query_args = [
17127 'orderby' => 'learndash_group_leaders_' . intval( $group_id ),
17128 'order' => 'DESC',
17129 'meta_query' => [
17130 [
17131 'key' => 'learndash_group_leaders_' . intval( $group_id ),
17132 'value' => intval( $group_id ),
17133 'compare' => '=',
17134 'type' => 'NUMERIC',
17135 ],
17136 ],
17137 ];
17138 $users = get_users( $user_query_args );
17139 }
17140
17141 if ( ! empty( $users ) ) {
17142 $context = WordPress::get_user_context( $users[0]->ID );
17143 $context['sfwd_group_id'] = $group_id;
17144 $context['group_title'] = get_the_title( $group_id );
17145 $context['group_url'] = get_permalink( $group_id );
17146 $context['group_featured_image_id'] = get_post_meta( $group_id, '_thumbnail_id', true );
17147 $context['group_featured_image_url'] = get_the_post_thumbnail_url( $group_id );
17148 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
17149 $group_courses_id = learndash_group_enrolled_courses( $group_id );
17150 if ( ! empty( $group_courses_id ) ) {
17151 foreach ( $group_courses_id as $key => $course_id ) {
17152 $context['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
17153 }
17154 }
17155 }
17156 $context['response_type'] = 'live';
17157 } else {
17158 $context = WordPress::get_sample_user_context();
17159 $context['group_title'] = 'Test Group';
17160 $context['sfwd_group_id'] = 112;
17161 $context['group_url'] = 'https://example.com/test-group';
17162 $context['group_featured_image_id'] = 113;
17163 $context['group_featured_image_url'] = 'https://example.com/test-group-img';
17164 $context['group_courses'] = [
17165 [
17166 'ID' => 7915,
17167 'title' => 'Example Course',
17168 'URL' => 'https://example.com/courses/example-course/',
17169 'status' => 'publish',
17170 'featured_image_id' => '',
17171 'featured_image_url' => false,
17172 ],
17173 ];
17174 $context['response_type'] = 'sample';
17175 }
17176
17177 $context['pluggable_data'] = $context;
17178 return $context;
17179 }
17180
17181 /**
17182 * Search learndash user enrolled in course.
17183 *
17184 * @param array $data data.
17185 * @return array|void
17186 */
17187 public function search_pluggables_user_enrolled_course( $data ) {
17188 global $wpdb;
17189 $context = [];
17190
17191 if ( ! function_exists( 'ld_course_access_expires_on' ) ||
17192 ! function_exists( 'learndash_group_enrolled_courses' ) ||
17193 ! function_exists( 'learndash_get_lesson_list' ) ) {
17194 return;
17195 }
17196 $term = $data['search_term'];
17197 $course_id = isset( $data['filter']['sfwd_course_id'] ) ? $data['filter']['sfwd_course_id']['value'] : '';
17198 $group_id = isset( $data['filter']['sfwd_group_id'] ) ? $data['filter']['sfwd_group_id']['value'] : '';
17199
17200 if ( 'course_enrolled' == $term ) {
17201 if ( -1 === $course_id ) {
17202 $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 ) );
17203 } else {
17204 $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 ) );
17205 }
17206 } elseif ( 'course_unenrolled' == $term ) {
17207 if ( -1 === $course_id ) {
17208 $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 ) );
17209 $course_id = $courses[0]->course_id;
17210 }
17211 $args = [
17212 'meta_query' => [
17213 'relation' => 'AND',
17214 [
17215 'key' => 'course_' . $course_id . '_access_from',
17216 'compare' => 'NOT EXISTS',
17217 ],
17218 [
17219 'key' => 'learndash_course_' . $course_id . '_enrolled_at',
17220 'compare' => 'EXISTS',
17221 ],
17222 ],
17223 ];
17224 $users = get_users( $args );
17225 } elseif ( 'course_access_expired' == $term ) {
17226 if ( -1 === $course_id ) {
17227 $course_args = [
17228 'post_type' => 'sfwd-courses',
17229 'posts_per_page' => 1,
17230 'orderby' => 'rand',
17231 'order' => 'ASC',
17232 'post_status' => 'publish',
17233 ];
17234 $courses_posts = get_posts( $course_args );
17235 $course_id = $courses_posts[0]->ID;
17236 }
17237 $args = [
17238 'meta_query' => [
17239 [
17240 'key' => 'learndash_course_expired_' . $course_id,
17241 'compare' => 'EXISTS',
17242 ],
17243 ],
17244 ];
17245 $users = get_users( $args );
17246 } elseif ( 'group_course_completed' == $term ) {
17247 if ( -1 === $group_id ) {
17248 $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 ) );
17249 } else {
17250 $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 ) );
17251 }
17252 $activity_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity_meta WHERE activity_id = %d", $courses[0]->activity_id ) );
17253 } elseif ( 'course_group_added' == $term ) {
17254 if ( -1 === $group_id ) {
17255 $args = [
17256 'numberposts' => 1,
17257 'fields' => 'ids',
17258 'orderby' => 'rand',
17259 'post_type' => 'groups',
17260 ];
17261 $posts = get_posts( $args );
17262 $random_key = array_rand( $posts );
17263 $random_value = $posts[ $random_key ];
17264 $group_id = $random_value;
17265 }
17266 $courses = learndash_group_enrolled_courses( $group_id );
17267 } elseif ( 'assignment_submitted' == $term ) {
17268 $course_id = isset( $data['filter']['sfwd-courses'] ) ? $data['filter']['sfwd-courses']['value'] : '';
17269 $lesson_id = $data['filter']['sfwd_lesson_topic_id']['value'];
17270 if ( -1 === $course_id && -1 === $lesson_id ) {
17271 $args = [
17272 'post_type' => 'sfwd-assignment',
17273 'posts_per_page' => 1,
17274 'order' => 'DESC',
17275 'post_status' => 'publish',
17276 ];
17277 } else {
17278 if ( -1 === $course_id ) {
17279 $courses = get_posts(
17280 [
17281 'posts_per_page' => - 1,
17282 'post_type' => 'sfwd-courses',
17283 'post_status' => 'publish',
17284 'fields' => 'ids',
17285 ]
17286 );
17287 $course_key = array_rand( $courses );
17288 $course_id = $courses[ $course_key ];
17289 }
17290 if ( -1 === $lesson_id ) {
17291 $args = [
17292 'post_type' => 'sfwd-assignment',
17293 'posts_per_page' => 1,
17294 'order' => 'DESC',
17295 'post_status' => 'publish',
17296 'meta_query' => [
17297 [
17298 'key' => 'course_id',
17299 'value' => $course_id,
17300 'compare' => '=',
17301 ],
17302 ],
17303 ];
17304 } else {
17305 $args = [
17306 'post_type' => 'sfwd-assignment',
17307 'posts_per_page' => 1,
17308 'order' => 'DESC',
17309 'post_status' => 'publish',
17310 'meta_query' => [
17311 'relation' => 'AND',
17312 [
17313 'key' => 'lesson_id',
17314 'value' => $lesson_id,
17315 'compare' => '=',
17316 ],
17317 [
17318 'key' => 'course_id',
17319 'value' => $course_id,
17320 'compare' => '=',
17321 ],
17322 ],
17323 ];
17324 }
17325 }
17326 $assignments = get_posts( $args );
17327 } elseif ( 'assignment_graded' == $term ) {
17328 $course_id = isset( $data['filter']['sfwd-courses'] ) ? $data['filter']['sfwd-courses']['value'] : '';
17329 $lesson_id = $data['filter']['sfwd_lesson_topic_id']['value'];
17330 if ( -1 === $course_id && -1 === $lesson_id ) {
17331 $args = [
17332 'post_type' => 'sfwd-assignment',
17333 'posts_per_page' => 1,
17334 'order' => 'DESC',
17335 'post_status' => 'publish',
17336 'meta_query' => [
17337 [
17338 'key' => 'approval_status',
17339 'compare' => 'EXISTS',
17340 ],
17341 ],
17342 ];
17343 } else {
17344 if ( -1 === $course_id ) {
17345 $courses = get_posts(
17346 [
17347 'posts_per_page' => - 1,
17348 'post_type' => 'sfwd-courses',
17349 'post_status' => 'publish',
17350 'fields' => 'ids',
17351 ]
17352 );
17353 $course_key = array_rand( $courses );
17354 $course_id = $courses[ $course_key ];
17355 }
17356 if ( -1 === $lesson_id ) {
17357 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
17358 if ( ! empty( $lessons ) ) {
17359 $random_key = array_rand( $lessons );
17360 $random_value = $lessons[ $random_key ];
17361 $lesson_id = $random_value->ID;
17362 }
17363 }
17364 $args = [
17365 'post_type' => 'sfwd-assignment',
17366 'posts_per_page' => 1,
17367 'order' => 'DESC',
17368 'post_status' => 'publish',
17369 'meta_query' => [
17370 'relation' => 'AND',
17371 [
17372 'key' => 'lesson_id',
17373 'value' => $lesson_id,
17374 'compare' => '=',
17375 ],
17376 [
17377 'key' => 'course_id',
17378 'value' => $course_id,
17379 'compare' => '=',
17380 ],
17381 [
17382 'key' => 'approval_status',
17383 'compare' => 'EXISTS',
17384 ],
17385 ],
17386 ];
17387 }
17388 $assignments = get_posts( $args );
17389 }
17390
17391 if ( 'assignment_submitted' == $term || 'assignment_graded' == $term ) {
17392 if ( ! empty( $assignments ) ) {
17393 $context_data = WordPress::get_user_context( (int) $assignments[0]->post_author );
17394 $context_data['assignment_id'] = $assignments[0]->ID;
17395 $context_data['assignment_title'] = $assignments[0]->post_title;
17396 $context_data['assignment_url'] = get_post_meta( $assignments[0]->ID, 'file_link', true );
17397 $context_data['lesson_id'] = get_post_meta( $assignments[0]->ID, 'lesson_id', true );
17398 $context_data['course_id'] = get_post_meta( $assignments[0]->ID, 'course_id', true );
17399 $context_data['points'] = get_post_meta( $assignments[0]->ID, 'points', true );
17400 $context['response_type'] = 'live';
17401 } else {
17402 $context_data = WordPress::get_sample_user_context();
17403 $context_data['assignment_title'] = 'Test Assignment';
17404 $context_data['assignment_id'] = 112;
17405 $context_data['assignment_url'] = 'https://example.com/test_assignment.pdf';
17406 $context_data['lesson_id'] = 2;
17407 $context_data['course_id'] = 1;
17408 $context_data['points'] = '12';
17409 $context['response_type'] = 'sample';
17410 }
17411 } elseif ( 'course_unenrolled' == $term || 'course_access_expired' == $term ) {
17412 if ( ! empty( $users ) ) {
17413 $context_data = WordPress::get_user_context( $users[0]->ID );
17414 $context_data['sfwd_course_id'] = $course_id;
17415 $context_data['course_title'] = get_the_title( $course_id );
17416 $context_data['course_url'] = get_permalink( $course_id );
17417 $context_data['course_featured_image_id'] = get_post_meta( $course_id, '_thumbnail_id', true );
17418 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course_id );
17419 $timestamp = ld_course_access_expires_on( $course_id, $users[0]->ID );
17420 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
17421 $date_format = get_option( 'date_format' );
17422 if ( is_string( $date_format ) ) {
17423 $context_data['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
17424 }
17425 $context['response_type'] = 'live';
17426 } else {
17427 $context_data = WordPress::get_sample_user_context();
17428 $context_data['course_name'] = 'Test Course';
17429 $context_data['sfwd_course_id'] = 112;
17430 $context_data['course_url'] = 'https://example.com/test-course';
17431 $context_data['course_featured_image_id'] = 113;
17432 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
17433 $context_data['course_access_expiry_date'] = '2023-10-20';
17434 $context['response_type'] = 'sample';
17435 }
17436 } elseif ( 'course_group_added' == $term ) {
17437 if ( ! empty( $courses ) ) {
17438 $context_data['course_id'] = $courses[0];
17439 $context_data['course_title'] = get_the_title( $courses[0] );
17440 $context_data['course_url'] = get_permalink( $courses[0] );
17441 $context_data['course_featured_image_id'] = get_post_meta( $courses[0], '_thumbnail_id', true );
17442 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $courses[0] );
17443 $context_data['sfwd_group_id'] = $group_id;
17444 $context_data['group_name'] = get_the_title( $group_id );
17445 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
17446 $group_courses_id = learndash_group_enrolled_courses( $group_id );
17447 if ( ! empty( $group_courses_id ) ) {
17448 foreach ( $group_courses_id as $key => $course_id ) {
17449 $context_data['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
17450 }
17451 }
17452 }
17453 $context['response_type'] = 'live';
17454 } else {
17455 $context_data = WordPress::get_sample_user_context();
17456 $context_data['course_name'] = 'Test Course';
17457 $context_data['sfwd_course_id'] = 112;
17458 $context_data['course_url'] = 'https://example.com/test-course';
17459 $context_data['course_featured_image_id'] = 113;
17460 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
17461 $context_data['course_access_expiry_date'] = '2023-10-20';
17462 $context_data['sfwd_group_id'] = 12;
17463 $context_data['group_name'] = 'Test Group';
17464 $context_data['group_courses'] = [
17465 [
17466 'ID' => 7915,
17467 'title' => 'Example Course',
17468 'URL' => 'https://example.com/courses/example-course/',
17469 'status' => 'publish',
17470 'featured_image_id' => '',
17471 'featured_image_url' => false,
17472 ],
17473 ];
17474 $context['response_type'] = 'sample';
17475 }
17476 } elseif ( 'group_course_completed' == $term ) {
17477 if ( ! empty( $courses ) && ! empty( $activity_meta ) ) {
17478 $context_data = WordPress::get_user_context( $courses[0]->user_id );
17479 $context_data['sfwd_group_id'] = $courses[0]->post_id;
17480 $context_data['group_title'] = get_the_title( $courses[0]->post_id );
17481 $context_data['group_url'] = get_permalink( $courses[0]->post_id );
17482 $context_data['group_featured_image_id'] = get_post_meta( $courses[0]->post_id, '_thumbnail_id', true );
17483 $context_data['group_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->post_id );
17484 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
17485 $group_courses_id = learndash_group_enrolled_courses( $courses[0]->post_id );
17486 if ( ! empty( $group_courses_id ) ) {
17487 foreach ( $group_courses_id as $key => $course_id ) {
17488 $context_data['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
17489 }
17490 }
17491 }
17492 $course_ids = null;
17493 foreach ( $activity_meta as $item ) {
17494 if ( 'course_ids' === $item->activity_meta_key ) {
17495 $course_ids = unserialize( $item->activity_meta_value );
17496 break;
17497 }
17498 }
17499 if ( ! empty( $course_ids ) && is_array( $course_ids ) ) {
17500 foreach ( $course_ids as $key => $course_id ) {
17501 if ( is_numeric( $course_id ) ) {
17502 $course_id = (int) $course_id;
17503 $args = [
17504 'include' => [ $courses[0]->user_id ],
17505 'meta_query' => [
17506 [
17507 'key' => 'course_completed_' . $course_id,
17508 'compare' => 'EXISTS',
17509 ],
17510 ],
17511 ];
17512 $users = get_users( $args );
17513 if ( ! empty( $users ) ) {
17514 $context_data[ 'completed ' . $key ]['course_id'] = $course_id;
17515 $context_data[ 'completed ' . $key ]['course_title'] = get_the_title( $course_id );
17516 $context_data[ 'completed ' . $key ]['course_url'] = get_permalink( $course_id );
17517 $context_data[ 'completed ' . $key ]['course_featured_image_id'] = get_post_meta( $course_id, '_thumbnail_id', true );
17518 $context_data[ 'completed ' . $key ]['course_featured_image_url'] = get_the_post_thumbnail_url( $course_id );
17519 $timestamp = ld_course_access_expires_on( $course_id, $courses[0]->user_id );
17520 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
17521 $date_format = get_option( 'date_format' );
17522 if ( is_string( $date_format ) ) {
17523 $context_data[ 'completed ' . $key ]['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
17524 }
17525 }
17526 }
17527 }
17528 }
17529 $context['response_type'] = 'live';
17530 } else {
17531 $context_data = WordPress::get_sample_user_context();
17532 $context_data['sfwd_group_id'] = 112;
17533 $context_data['group_title'] = 'Test Group';
17534 $context_data['group_url'] = 113;
17535 $context_data['group_featured_image_id'] = 11;
17536 $context_data['group_featured_image_url'] = 'https://example.com/test-group-img';
17537 $context_data['completed 0'] = [
17538 'course_id' => 10,
17539 'course_title' => 'Test Course',
17540 'course_url' => 'https://example.com/test-course',
17541 'course_featured_image_id' => 14,
17542 'course_featured_image_url' => 'https://example.com/test-course-img',
17543 ];
17544 $context_data['group_courses'] = [
17545 [
17546 'ID' => 7915,
17547 'title' => 'Example Course',
17548 'URL' => 'https://example.com/courses/example-course/',
17549 'status' => 'publish',
17550 'featured_image_id' => '',
17551 'featured_image_url' => false,
17552 ],
17553 ];
17554 $context['response_type'] = 'sample';
17555 }
17556 } else {
17557 if ( ! empty( $courses ) ) {
17558 $context_data = WordPress::get_user_context( $courses[0]->user_id );
17559 $context_data['course_id'] = $courses[0]->course_id;
17560 $context_data['course_title'] = get_the_title( $courses[0]->course_id );
17561 $context_data['course_url'] = get_permalink( $courses[0]->course_id );
17562 $context_data['course_featured_image_id'] = get_post_meta( $courses[0]->course_id, '_thumbnail_id', true );
17563 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->course_id );
17564 $timestamp = ld_course_access_expires_on( $courses[0]->course_id, $courses[0]->user_id );
17565 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
17566 $date_format = get_option( 'date_format' );
17567 if ( is_string( $date_format ) ) {
17568 $context_data['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
17569 }
17570 if ( $courses[0]->post_id ) {
17571 $context_data['sfwd_group_id'] = $courses[0]->post_id;
17572 $context_data['group_title'] = get_the_title( $courses[0]->post_id );
17573 $context_data['group_url'] = get_permalink( $courses[0]->post_id );
17574 $context_data['group_featured_image_id'] = get_post_meta( $courses[0]->post_id, '_thumbnail_id', true );
17575 $context_data['group_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->post_id );
17576 }
17577 $context['response_type'] = 'live';
17578 } else {
17579 $context_data = WordPress::get_sample_user_context();
17580 $context_data['course_name'] = 'Test Course';
17581 $context_data['sfwd_course_id'] = 112;
17582 $context_data['course_url'] = 'https://example.com/test-course';
17583 $context_data['course_featured_image_id'] = 113;
17584 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
17585 $context_data['course_access_expiry_date'] = '2023-10-20';
17586 $context['response_type'] = 'sample';
17587 }
17588 }
17589
17590 $context['pluggable_data'] = $context_data;
17591 return $context;
17592 }
17593
17594 /**
17595 * Search LearnDash Lesson Topic List.
17596 *
17597 * @param array $data Search Params.
17598 *
17599 * @since 1.0.0
17600 *
17601 * @return array
17602 */
17603 public function search_ld_lessons_topics_list( $data ) {
17604 $options = [];
17605 $course_id = $data['dynamic']['sfwd-courses'];
17606
17607 if ( ! function_exists( 'learndash_get_lesson_list' ) || ! function_exists( 'learndash_get_topic_list' ) ) {
17608 return [];
17609 }
17610
17611 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
17612 foreach ( $lessons as $lesson ) {
17613 $options[] = [
17614 'label' => $lesson->post_title,
17615 'value' => $lesson->ID,
17616 ];
17617 $topics = learndash_get_topic_list( $lesson->ID, $course_id );
17618 foreach ( $topics as $topic ) {
17619 $options[] = [
17620 'label' => $topic->post_title,
17621 'value' => $topic->ID,
17622 ];
17623 }
17624 }
17625
17626 return [
17627 'options' => $options,
17628 'hasMore' => false,
17629 ];
17630 }
17631
17632 /**
17633 * Search LearnDash Quiz List.
17634 *
17635 * @param array $data Search Params.
17636 *
17637 * @since 1.0.0
17638 *
17639 * @return array
17640 */
17641 public function search_ld_quiz_list( $data ) {
17642 $options = [];
17643 $course_id = $data['dynamic']['sfwd-courses'];
17644 $lesson_id = $data['dynamic']['sfwd_lessons_topics'];
17645
17646 if ( ! function_exists( 'learndash_get_course_quiz_list' ) || ! function_exists( 'learndash_get_lesson_quiz_list' ) ) {
17647 return [];
17648 }
17649
17650 $quizzes = learndash_get_course_quiz_list( $course_id );
17651 $quizzes = array_merge( $quizzes, learndash_get_lesson_quiz_list( $lesson_id, null, $course_id ) );
17652 if ( ! empty( $quizzes ) ) {
17653 foreach ( $quizzes as $quiz ) {
17654 $options[] = [
17655 'label' => $quiz['post']->post_title,
17656 'value' => $quiz['post']->ID,
17657
17658 ];
17659 }
17660 }
17661
17662 return [
17663 'options' => $options,
17664 'hasMore' => false,
17665 ];
17666 }
17667
17668 /**
17669 * Search LearnDash Quiz Essay Question List.
17670 *
17671 * @param array $data Search Params.
17672 *
17673 * @since 1.0.0
17674 *
17675 * @return array
17676 */
17677 public function search_ld_quiz_essay_question_list( $data ) {
17678 $options = [];
17679 $quiz_id = $data['dynamic'];
17680
17681 if ( ! function_exists( 'learndash_get_quiz_questions' ) ) {
17682 return [];
17683 }
17684
17685 if ( 0 < $quiz_id ) {
17686 $quiz_question_ids = learndash_get_quiz_questions( $quiz_id );
17687 if ( ! empty( $quiz_question_ids ) ) {
17688 foreach ( $quiz_question_ids as $question_post_id => $question_pro_id ) {
17689 $question_type = get_post_meta( $question_post_id, 'question_type', true );
17690 if ( is_string( $question_type ) && 'essay' === $question_type ) {
17691 $title = html_entity_decode( get_the_title( $question_post_id ), ENT_QUOTES, 'UTF-8' );
17692 $options[] = [
17693 'label' => $title,
17694 'value' => $question_post_id,
17695 ];
17696 }
17697 }
17698 }
17699 }
17700
17701 return [
17702 'options' => $options,
17703 'hasMore' => false,
17704 ];
17705 }
17706
17707 /**
17708 * Search LearnDash Lessons List.
17709 *
17710 * @param array $data Search Params.
17711 *
17712 * @since 1.0.0
17713 *
17714 * @return array
17715 */
17716 public function search_ld_lessons_list( $data ) {
17717 $options = [];
17718 $course_id = $data['dynamic']['sfwd-courses'];
17719
17720 if ( ! function_exists( 'learndash_get_lesson_list' ) ) {
17721 return [];
17722 }
17723
17724 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
17725 foreach ( $lessons as $lesson ) {
17726 $options[] = [
17727 'label' => $lesson->post_title,
17728 'value' => $lesson->ID,
17729 ];
17730 }
17731 return [
17732 'options' => $options,
17733 'hasMore' => false,
17734 ];
17735 }
17736
17737 /**
17738 * Search LearnDash Topics List.
17739 *
17740 * @param array $data Search Params.
17741 *
17742 * @since 1.0.0
17743 *
17744 * @return array
17745 */
17746 public function search_ld_topics_list( $data ) {
17747 $options = [];
17748 $course_id = $data['dynamic']['sfwd-courses'];
17749 $lesson_id = $data['dynamic']['sfwd-lessons'];
17750
17751 if ( ! function_exists( 'learndash_get_topic_list' ) ) {
17752 return [];
17753 }
17754
17755 $topics = learndash_get_topic_list( $lesson_id, $course_id );
17756 foreach ( $topics as $topic ) {
17757 $options[] = [
17758 'label' => $topic->post_title,
17759 'value' => $topic->ID,
17760 ];
17761 }
17762 return [
17763 'options' => $options,
17764 'hasMore' => false,
17765 ];
17766 }
17767
17768 /**
17769 * Search LearnDash Assignments List.
17770 *
17771 * @param array $data Search Params.
17772 *
17773 * @since 1.0.0
17774 *
17775 * @return array
17776 */
17777 public function search_ld_assignments_list( $data ) {
17778 $options = [];
17779 $course_id = $data['dynamic']['sfwd-courses'];
17780 $lesson_id = $data['dynamic']['sfwd_lesson_topic_id'];
17781
17782 $args = [
17783 'post_type' => 'sfwd-assignment',
17784 'numberposts' => - 1,
17785 ];
17786 $meta_query = [];
17787 if ( ! empty( $course_id ) ) {
17788 $meta_query[] = [
17789 'key' => 'course_id',
17790 'value' => (int) $course_id,
17791 'compare' => '=',
17792 ];
17793 }
17794 if ( ! empty( $lesson_id ) ) {
17795 $meta_query[] = [
17796 'key' => 'lesson_id',
17797 'value' => (int) $lesson_id,
17798 'compare' => '=',
17799 ];
17800 }
17801 if ( ! empty( $meta_query ) ) {
17802 $args['meta_query'] = $meta_query;
17803 if ( count( $meta_query ) > 1 ) {
17804 $args['meta_query']['relation'] = 'AND';
17805 }
17806 }
17807 $assignments = get_posts( $args );
17808 foreach ( $assignments as $assignment ) {
17809 $options[] = [
17810 'label' => $assignment->post_title,
17811 'value' => $assignment->ID,
17812 ];
17813 }
17814 return [
17815 'options' => $options,
17816 'hasMore' => false,
17817 ];
17818 }
17819
17820 /**
17821 * Search post by post type.
17822 *
17823 * @param array $data Search Params.
17824 *
17825 * @since 1.0.0
17826 *
17827 * @return array
17828 */
17829 public function search_edd_prices( $data ) {
17830 $options = [];
17831 $downlaod_id = $data['dynamic']['download_id'];
17832
17833 $variable_prices = get_post_meta( $downlaod_id, 'edd_variable_prices', true );
17834 if ( ! empty( $variable_prices ) && is_array( $variable_prices ) ) {
17835 foreach ( $variable_prices as $price_id => $price ) {
17836 if ( isset( $price['name'] ) ) {
17837 $options[] = [
17838 'label' => $price['name'] . '(' . $price['amount'] . ')',
17839 'value' => $price['index'],
17840 ];
17841 }
17842 }
17843 }
17844
17845 return [
17846 'options' => $options,
17847 'hasMore' => false,
17848 ];
17849
17850 }
17851
17852 /**
17853 * GetPowerful Docs list.
17854 *
17855 * @param array $data data.
17856 *
17857 * @return array
17858 */
17859 public function search_pfd_docs_list( $data ) {
17860
17861 $course_id = $data['dynamic'];
17862 $page = $data['page'];
17863 $limit = Utilities::get_search_page_limit();
17864 $offset = $limit * ( $page - 1 );
17865 $options = [];
17866 $args = [
17867 'post_type' => 'docs',
17868 'posts_per_page' => $limit,
17869 'offset' => $offset,
17870 'orderby' => 'title',
17871 'order' => 'ASC',
17872 'post_status' => 'publish',
17873 ];
17874
17875 $docs = get_posts( $args );
17876 $docs_count = wp_count_posts( 'docs' )->publish;
17877 if ( ! empty( $docs ) ) {
17878 if ( is_array( $docs ) ) {
17879 foreach ( $docs as $doc ) {
17880 $options[] = [
17881 'label' => $doc->post_title,
17882 'value' => $doc->ID,
17883 ];
17884 }
17885 }
17886 }
17887 return [
17888 'options' => $options,
17889 'hasMore' => $docs_count > $limit && $docs_count > $offset,
17890 ];
17891 }
17892
17893 /**
17894 * Search Powerful Docs last data.
17895 *
17896 * @param array $data data.
17897 * @return array|void|mixed
17898 */
17899 public function search_pfd_feedback_last_data( $data ) {
17900 $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"}';
17901 $context = json_decode( $sample_data, true );
17902
17903 return $context;
17904 }
17905
17906 /**
17907 * WooCommerce Coupon Discount type list.
17908 *
17909 * @param array $data data.
17910 *
17911 * @return array
17912 */
17913 public function search_woo_coupon_discount_type_list( $data ) {
17914 $options = [];
17915
17916 $discount_types = wc_get_coupon_types();
17917
17918 if ( ! empty( $discount_types ) ) {
17919 if ( is_array( $discount_types ) ) {
17920 foreach ( $discount_types as $key => $value ) {
17921 $options[] = [
17922 'label' => $value,
17923 'value' => $key,
17924 ];
17925 }
17926 }
17927 }
17928
17929 return [
17930 'options' => $options,
17931 'hasMore' => false,
17932 ];
17933 }
17934
17935 /**
17936 * WooCommerce Product list along with variation list.
17937 *
17938 * @param array $data data.
17939 *
17940 * @return array|void
17941 */
17942 public function search_woo_product_variation_list( $data ) {
17943 $options = [];
17944
17945 $page = $data['page'];
17946 $limit = Utilities::get_search_page_limit();
17947 $offset = $limit * ( $page - 1 );
17948
17949 if ( ! function_exists( 'wc_get_products' ) ) {
17950 return;
17951 }
17952 $products = wc_get_products(
17953 [
17954 'posts_per_page' => $limit,
17955 'offset' => $offset,
17956 'orderby' => 'date',
17957 'order' => 'DESC',
17958 ]
17959 );
17960
17961
17962 $prod_count = wp_count_posts( 'product' )->publish;
17963
17964 if ( ! empty( $products ) ) {
17965 if ( is_array( $products ) ) {
17966 foreach ( $products as $product ) {
17967 $options[] = [
17968 'label' => $product->get_name(),
17969 'value' => $product->get_id(),
17970 ];
17971 if ( $product->is_type( 'variable' ) ) {
17972 $variations = Utilities::get_product_variations( $product->get_id() );
17973 foreach ( $variations['result'] as $variation ) {
17974 $options[] = [
17975 'label' => $variation->post_title,
17976 'value' => $variation->ID,
17977 ];
17978 }
17979 }
17980 }
17981 }
17982 }
17983 return [
17984 'options' => $options,
17985 'hasMore' => $prod_count > $limit && $prod_count > $offset,
17986 ];
17987 }
17988
17989 /**
17990 * WooCommerce Product list along with variation list.
17991 *
17992 * @param array $data data.
17993 *
17994 * @return array
17995 */
17996 public function search_woo_coupon_list( $data ) {
17997 $options = [];
17998
17999 $page = $data['page'];
18000 $limit = Utilities::get_search_page_limit();
18001 $offset = $limit * ( $page - 1 );
18002
18003 $coupons = get_posts(
18004 [
18005 'posts_per_page' => - 1,
18006 'orderby' => 'name',
18007 'order' => 'asc',
18008 'post_type' => 'shop_coupon',
18009 'post_status' => 'publish',
18010 ]
18011 );
18012 $coupon_count = wp_count_posts( 'shop_coupon' )->publish;
18013
18014 if ( ! empty( $coupons ) ) {
18015 if ( is_array( $coupons ) ) {
18016 foreach ( $coupons as $coupon ) {
18017 $code = wc_format_coupon_code( $coupon->post_title );
18018 $options[] = [
18019 'label' => $code,
18020 'value' => $code,
18021 ];
18022 }
18023 }
18024 }
18025
18026 return [
18027 'options' => $options,
18028 'hasMore' => $coupon_count > $limit && $coupon_count > $offset,
18029 ];
18030 }
18031
18032 /**
18033 * Prepare LatePoint Bookings List.
18034 *
18035 * @param array $data Search Params.
18036 * @return array
18037 */
18038 public function search_lp_bookings_list( $data ) {
18039
18040 if ( ! class_exists( 'OsBookingHelper' ) ) {
18041 return [];
18042 }
18043
18044 $bookings = OsBookingHelper::get_bookings_for_select();
18045 $options = [];
18046
18047 if ( ! empty( $bookings ) ) {
18048 foreach ( $bookings as $key => $booking ) {
18049 $options[] = [
18050 'label' => $booking['label'],
18051 'value' => $booking['value'],
18052 ];
18053 }
18054 }
18055
18056 return [
18057 'options' => $options,
18058 'hasMore' => false,
18059 ];
18060 }
18061
18062 /**
18063 * Prepare LatePoint Services List.
18064 *
18065 * @param array $data Search Params.
18066 * @return array
18067 */
18068 public function search_lp_services_list( $data ) {
18069
18070 if ( ! class_exists( 'OsServiceHelper' ) ) {
18071 return [];
18072 }
18073
18074 $services = OsServiceHelper::get_services_list();
18075 $options = [];
18076
18077 if ( ! empty( $services ) ) {
18078 foreach ( $services as $key => $service ) {
18079 $options[] = [
18080 'label' => $service['label'],
18081 'value' => $service['value'],
18082 ];
18083 }
18084 }
18085
18086 return [
18087 'options' => $options,
18088 'hasMore' => false,
18089 ];
18090 }
18091
18092 /**
18093 * Prepare LatePoint Bundles List.
18094 *
18095 * @param array $data Search Params.
18096 * @return array
18097 */
18098 public function search_lp_bundles_list( $data ) {
18099 global $wpdb;
18100
18101 $bundles = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}latepoint_bundles", ARRAY_A );
18102
18103 $options = [];
18104
18105 if ( ! empty( $bundles ) ) {
18106 foreach ( $bundles as $bundle ) {
18107 $options[] = [
18108 'label' => $bundle['name'],
18109 'value' => $bundle['id'],
18110 ];
18111 }
18112 }
18113
18114 return [
18115 'options' => $options,
18116 'hasMore' => false,
18117 ];
18118 }
18119
18120 /**
18121 * Prepare LatePoint Agents List.
18122 *
18123 * @param array $data Search Params.
18124 * @return array
18125 */
18126 public function search_lp_agents_list( $data ) {
18127
18128 if ( ! class_exists( 'OsAgentHelper' ) ) {
18129 return [];
18130 }
18131
18132 $agent_ids_for_service = OsAgentHelper::get_agent_ids_for_service_and_location( $data['dynamic'] );
18133 $agents = OsAgentHelper::get_agents_list();
18134 $options = [];
18135
18136 if ( ! empty( $agents ) ) {
18137 foreach ( $agents as $key => $agent ) {
18138 if ( in_array( $agent['value'], $agent_ids_for_service ) ) {
18139 $options[] = [
18140 'label' => $agent['label'],
18141 'value' => $agent['value'],
18142 ];
18143 }
18144 }
18145 }
18146
18147 return [
18148 'options' => $options,
18149 'hasMore' => false,
18150 ];
18151 }
18152
18153 /**
18154 * Prepare LatePoint Agents WP User List.
18155 *
18156 * @param array $data Search Params.
18157 * @return array
18158 */
18159 public function search_lp_user_agents_list( $data ) {
18160
18161 if ( ! class_exists( 'OsWpUserHelper' ) ) {
18162 return [];
18163 }
18164 $agents = OsWpUserHelper::get_wp_users_for_select( [ 'role' => 'latepoint_agent' ] );
18165 $options = [];
18166 if ( ! empty( $agents ) ) {
18167 $options = $agents;
18168 }
18169 return [
18170 'options' => $options,
18171 'hasMore' => false,
18172 ];
18173 }
18174
18175 /**
18176 * Prepare LatePoint Statuses List.
18177 *
18178 * @param array $data Search Params.
18179 * @return array
18180 */
18181 public function search_lp_statuses_list( $data ) {
18182
18183 if ( ! class_exists( 'OsBookingHelper' ) ) {
18184 return [];
18185 }
18186
18187 $statuses = OsBookingHelper::get_statuses_list();
18188 $options = [];
18189
18190 if ( ! empty( $statuses ) ) {
18191 foreach ( $statuses as $key => $label ) {
18192 $options[] = [
18193 'label' => $label,
18194 'value' => $key,
18195 ];
18196 }
18197 }
18198
18199 return [
18200 'options' => $options,
18201 'hasMore' => false,
18202 ];
18203 }
18204
18205 /**
18206 * Prepare LatePoint Customers List.
18207 *
18208 * @param array $data Search Params.
18209 * @return array
18210 */
18211 public function search_lp_customers_list( $data ) {
18212
18213 if ( ! class_exists( 'OsCustomerHelper' ) ) {
18214 return [];
18215 }
18216
18217 $customers = OsCustomerHelper::get_customers_for_select();
18218 $options = [];
18219
18220 if ( ! empty( $customers ) ) {
18221 foreach ( $customers as $key => $customer ) {
18222 $options[] = [
18223 'label' => $customer['label'],
18224 'value' => $customer['value'],
18225 ];
18226 }
18227 }
18228
18229 return [
18230 'options' => $options,
18231 'hasMore' => false,
18232 ];
18233 }
18234
18235 /**
18236 * Prepare SureForms Forms List.
18237 *
18238 * @param array $data Search Params.
18239 * @return array
18240 */
18241 public function search_sureforms_form_list( $data ) {
18242
18243 $options = [];
18244
18245 $page = $data['page'];
18246 $limit = Utilities::get_search_page_limit();
18247 $offset = $limit * ( $page - 1 );
18248
18249 $forms = get_posts(
18250 [
18251 'posts_per_page' => - 1,
18252 'orderby' => 'name',
18253 'order' => 'asc',
18254 'post_type' => 'sureforms_form',
18255 'post_status' => 'publish',
18256 ]
18257 );
18258 $form_count = wp_count_posts( 'sureforms_form' )->publish;
18259
18260 if ( ! empty( $forms ) ) {
18261 if ( is_array( $forms ) ) {
18262 foreach ( $forms as $form ) {
18263 $title = html_entity_decode( get_the_title( $form->ID ), ENT_QUOTES, 'UTF-8' );
18264 $options[] = [
18265 'label' => $title,
18266 'value' => $form->ID,
18267 ];
18268 }
18269 }
18270 }
18271
18272 return [
18273 'options' => $options,
18274 'hasMore' => $form_count > $limit && $form_count > $offset,
18275 ];
18276 }
18277
18278 /**
18279 * Prepare Academy Course List.
18280 *
18281 * @param array $data Search Params.
18282 * @return array
18283 */
18284 public function search_ac_lms_courses( $data ) {
18285
18286 $options = [];
18287
18288 $page = $data['page'];
18289 $limit = Utilities::get_search_page_limit();
18290 $offset = $limit * ( $page - 1 );
18291
18292 $courses = get_posts(
18293 [
18294 'posts_per_page' => - 1,
18295 'orderby' => 'name',
18296 'order' => 'asc',
18297 'post_type' => 'academy_courses',
18298 'post_status' => 'publish',
18299 ]
18300 );
18301 $course_count = wp_count_posts( 'academy_courses' )->publish;
18302
18303 if ( ! empty( $courses ) ) {
18304 if ( is_array( $courses ) ) {
18305 foreach ( $courses as $course ) {
18306 $options[] = [
18307 'label' => get_the_title( $course->ID ),
18308 'value' => $course->ID,
18309 ];
18310 }
18311 }
18312 }
18313
18314 return [
18315 'options' => $options,
18316 'hasMore' => $course_count > $limit && $course_count > $offset,
18317 ];
18318 }
18319
18320 /**
18321 * Prepare Academy Lesson List.
18322 *
18323 * @param array $data Search Params.
18324 * @return array
18325 */
18326 public function search_ac_lms_lessons( $data ) {
18327
18328 $options = [];
18329
18330 if ( ! class_exists( '\Academy\Helper' ) ) {
18331 return [];
18332 }
18333
18334 $curriculums = \Academy\Helper::get_course_curriculum( $data['dynamic'] );
18335
18336 if ( ! empty( $curriculums ) ) {
18337 foreach ( $curriculums as $topic ) {
18338 if ( isset( $topic['topics'] ) && is_array( $topic['topics'] ) ) {
18339 foreach ( $topic['topics'] as $lesson ) {
18340 if ( isset( $lesson['type'] ) && 'lesson' === $lesson['type'] ) {
18341 $options[] = [
18342 'label' => $lesson['name'],
18343 'value' => $lesson['id'],
18344 ];
18345 }
18346 }
18347 }
18348 }
18349 }
18350
18351 return [
18352 'options' => $options,
18353 'hasMore' => false,
18354 ];
18355 }
18356
18357 /**
18358 * Prepare Academy Quiz List.
18359 *
18360 * @param array $data Search Params.
18361 * @return array
18362 */
18363 public function search_ac_lms_quiz( $data ) {
18364
18365 $options = [];
18366
18367 $page = $data['page'];
18368 $limit = Utilities::get_search_page_limit();
18369 $offset = $limit * ( $page - 1 );
18370
18371 $quizs = get_posts(
18372 [
18373 'posts_per_page' => - 1,
18374 'orderby' => 'name',
18375 'order' => 'asc',
18376 'post_type' => 'academy_quiz',
18377 'post_status' => 'publish',
18378 ]
18379 );
18380 $quiz_count = wp_count_posts( 'academy_quiz' )->publish;
18381
18382 if ( ! empty( $quizs ) ) {
18383 if ( is_array( $quizs ) ) {
18384 foreach ( $quizs as $quiz ) {
18385 $options[] = [
18386 'label' => get_the_title( $quiz->ID ),
18387 'value' => $quiz->ID,
18388 ];
18389 }
18390 }
18391 }
18392
18393 return [
18394 'options' => $options,
18395 'hasMore' => $quiz_count > $limit && $quiz_count > $offset,
18396 ];
18397 }
18398
18399 /**
18400 * Search Academy LMS data.
18401 *
18402 * @param array $data data.
18403 * @return array|void
18404 */
18405 public function search_ac_lms_last_data( $data ) {
18406 global $wpdb;
18407 $post_type = $data['post_type'];
18408 $trigger = $data['search_term'];
18409 $context = [];
18410
18411 if ( ! class_exists( '\Academy\Helper' ) ) {
18412 return [];
18413 }
18414
18415 $course_id = -1;
18416 $lesson_id = -1;
18417 $quiz_id = -1;
18418 if ( 'ac_lms_course_completed' === $trigger ) {
18419 $course_id = $data['filter']['course']['value'];
18420 } elseif ( 'ac_lms_lesson_completed' === $trigger ) {
18421 $course_id = $data['filter']['course']['value'];
18422 $lesson_id = $data['filter']['lesson']['value'];
18423 } elseif ( 'ac_lms_quiz_completed' === $trigger || 'ac_lms_quiz_failed' === $trigger ) {
18424 $quiz_id = $data['filter']['quiz']['value'];
18425 } elseif ( 'ac_lms_enrolled_course' === $trigger ) {
18426 $course_id = $data['filter']['course']['value'];
18427 }
18428
18429 $users = get_users(
18430 [
18431 'fields' => 'ID',
18432 'meta_key' => 'is_academy_student',
18433 ]
18434 );
18435 if ( ! empty( $users ) ) {
18436 $user_random_key = array_rand( $users );
18437 $user_id = $users[ $user_random_key ];
18438 }
18439
18440 if ( 'ac_lms_course_completed' === $trigger ) {
18441 if ( -1 === $course_id ) {
18442 $result = $wpdb->get_results(
18443 $wpdb->prepare(
18444 "SELECT * FROM {$wpdb->prefix}comments
18445 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",
18446 $post_type
18447 )
18448 );
18449 } else {
18450 $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 ) );
18451 }
18452 } elseif ( 'ac_lms_lesson_completed' === $trigger ) {
18453 if ( -1 === $course_id ) {
18454 $courses = get_posts(
18455 [
18456 'posts_per_page' => - 1,
18457 'post_type' => 'academy_courses',
18458 'post_status' => 'publish',
18459 'fields' => 'ids',
18460 ]
18461 );
18462 $course_id = array_rand( $courses );
18463 $option_name = 'academy_course_' . $course_id . '_completed_topics';
18464 } else {
18465 $option_name = 'academy_course_' . $course_id . '_completed_topics';
18466 }
18467 if ( ! empty( $users ) ) {
18468 $meta = get_user_meta( $user_id, $option_name, true );
18469 if ( is_string( $meta ) ) {
18470 $saved_topics_lists = (array) json_decode( $meta, true );
18471 if ( -1 === $lesson_id ) {
18472 $result = $saved_topics_lists['lesson'];
18473 } else {
18474 if ( is_array( $saved_topics_lists['lesson'] ) ) {
18475 if ( array_key_exists( $lesson_id, $saved_topics_lists['lesson'] ) ) {
18476 $result = $saved_topics_lists['lesson'];
18477 }
18478 }
18479 }
18480 }
18481 }
18482 } elseif ( 'ac_lms_quiz_completed' === $trigger ) {
18483 if ( -1 === $quiz_id ) {
18484 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE attempt_status='passed' order by attempt_id DESC LIMIT 1" );
18485 } else {
18486 $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 ) );
18487 }
18488 } elseif ( 'ac_lms_quiz_failed' === $trigger ) {
18489 if ( -1 === $quiz_id ) {
18490 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE attempt_status='failed' order by attempt_id DESC LIMIT 1" );
18491 } else {
18492 $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 ) );
18493 }
18494 } elseif ( 'ac_lms_enrolled_course' === $trigger ) {
18495 if ( -1 === $course_id ) {
18496 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type = %s order by ID DESC LIMIT 1", 'academy_enrolled' ) );
18497 } else {
18498 $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 ) );
18499 }
18500 }
18501
18502 if ( ! empty( $result ) ) {
18503 switch ( $trigger ) {
18504 case 'ac_lms_course_completed':
18505 $data = WordPress::get_post_context( $result[0]->comment_post_ID );
18506 $context_data = WordPress::get_user_context( $result[0]->user_id );
18507 $context_data['course_data'] = $data;
18508 $context_data['course'] = $result[0]->comment_post_ID;
18509 break;
18510 case 'ac_lms_enrolled_course':
18511 $data = WordPress::get_post_context( $result[0]->post_parent );
18512 $context_data = WordPress::get_user_context( $result[0]->post_author );
18513 $context_data['course_data'] = $data;
18514 $context_data['enrollment_data'] = $result[0];
18515 $context_data['course'] = $result[0]->post_parent;
18516 break;
18517 case 'ac_lms_lesson_completed':
18518 if ( -1 === $lesson_id ) {
18519 $key = array_rand( $result );
18520 $lesson_data = \Academy\Helper::get_lesson( $key );
18521 } else {
18522 $lesson_data = \Academy\Helper::get_lesson( $lesson_id );
18523 }
18524 if ( is_object( $lesson_data ) ) {
18525 $lesson_data = get_object_vars( $lesson_data );
18526 }
18527 if ( ! empty( $users ) ) {
18528 $context_data = array_merge( $lesson_data, WordPress::get_user_context( $user_id ) );
18529 }
18530 $context_data['course_data'] = WordPress::get_post_context( $course_id );
18531 $context_data['lesson'] = $lesson_id;
18532 $context_data['course'] = $course_id;
18533 break;
18534 case 'ac_lms_quiz_completed':
18535 case 'ac_lms_quiz_failed':
18536 $context_data = WordPress::get_user_context( $result[0]->user_id );
18537 $context_data['quiz_data'] = WordPress::get_post_context( $result[0]->quiz_id );
18538 $context_data['quiz_attempt_details'] = $result;
18539 $context_data['quiz'] = $result[0]->quiz_id;
18540 break;
18541 default:
18542 return;
18543 }
18544 $context['pluggable_data'] = $context_data;
18545 $context['response_type'] = 'live';
18546 } elseif ( empty( $result ) ) {
18547 switch ( $trigger ) {
18548 case 'ac_lms_course_completed':
18549 $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"}';
18550 break;
18551 case 'ac_lms_lesson_completed':
18552 $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"}';
18553 break;
18554 case 'ac_lms_quiz_completed':
18555 $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"}';
18556 break;
18557 case 'ac_lms_quiz_failed':
18558 $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"}';
18559 break;
18560 case 'ac_lms_enrolled_course':
18561 $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"}';
18562 break;
18563 default:
18564 return;
18565 }
18566 $context = (array) json_decode( $sample_data, true );
18567 }
18568 return $context;
18569 }
18570
18571 /**
18572 * Search myCred Point Type List.
18573 *
18574 * @param array $data Search Params.
18575 * @return array
18576 */
18577 public function search_mycred_point_type_list( $data ) {
18578
18579 $options = [];
18580
18581 if ( ! function_exists( 'mycred_get_types' ) ) {
18582 return [];
18583 }
18584
18585 $posts = mycred_get_types();
18586
18587 if ( ! empty( $posts ) ) {
18588 if ( is_array( $posts ) ) {
18589 foreach ( $posts as $key => $post ) {
18590 $options[] = [
18591 'label' => $post,
18592 'value' => $key,
18593 ];
18594 }
18595 }
18596 }
18597
18598 return [
18599 'options' => $options,
18600 'hasMore' => false,
18601 ];
18602 }
18603
18604 /**
18605 * Prepare elementor forms.
18606 *
18607 * @param array $data Search Params.
18608 *
18609 * @return array
18610 */
18611 public function search_new_elementor_form_fields( $data ) {
18612
18613 $fields = [];
18614 $select_form_id = $data['dynamic'];
18615 global $wpdb;
18616 $post_metas = $wpdb->get_results(
18617 $wpdb->prepare(
18618 "SELECT pm.post_id, pm.meta_value
18619 FROM $wpdb->postmeta pm
18620 LEFT JOIN $wpdb->posts p
18621 ON p.ID = pm.post_id
18622 WHERE p.post_type IS NOT NULL
18623 AND p.post_status = %s
18624 AND pm.meta_key = %s
18625 AND pm.`meta_value` LIKE %s",
18626 'publish',
18627 '_elementor_data',
18628 '%%form_fields%%'
18629 )
18630 );
18631
18632 if ( ! empty( $post_metas ) ) {
18633 foreach ( $post_metas as $post_meta ) {
18634 /**
18635 *
18636 * Ignore line
18637 *
18638 * @phpstan-ignore-next-line
18639 */
18640 $inner_forms = Utilities::search_elementor_forms( json_decode( $post_meta->meta_value ) );
18641 if ( ! empty( $inner_forms ) ) {
18642 foreach ( $inner_forms as $form ) {
18643 $form_id = explode( '_', $select_form_id );
18644 if ( is_object( $form ) ) {
18645 if ( $form->id == $form_id[1] ) {
18646 if ( ! empty( $form->settings->form_fields ) ) {
18647 foreach ( $form->settings->form_fields as $field ) {
18648 $fields[] = [
18649 'value' => $field->custom_id,
18650 'text' => ! empty( $field->field_label ) ? $field->field_label : 'unknown',
18651 ];
18652 }
18653 }
18654 }
18655 }
18656 }
18657 }
18658 }
18659 }
18660 $options = [];
18661 if ( ! empty( $fields ) ) {
18662 foreach ( $fields as $key => $value ) {
18663 $options[] = [
18664 'label' => $value['text'],
18665 'value' => $value['value'],
18666 ];
18667 }
18668 }
18669 return [
18670 'options' => $options,
18671 'hasMore' => false,
18672 ];
18673 }
18674 /**
18675 * Get Fluent Booking Appointment Events.
18676 *
18677 * @param array $data data.
18678 *
18679 * @return array
18680 */
18681 public function search_fluent_booking_calendars_list( $data ) {
18682
18683 global $wpdb;
18684
18685 $page = $data['page'];
18686 $limit = Utilities::get_search_page_limit();
18687 $offset = $limit * ( $page - 1 );
18688
18689 $calendars = $wpdb->get_results(
18690 $wpdb->prepare(
18691 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendars WHERE status = %s ORDER BY id DESC LIMIT %d OFFSET %d",
18692 [ 'active', $limit, $offset ]
18693 ),
18694 OBJECT
18695 );
18696
18697 $calendars_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
18698
18699 $options = [];
18700 if ( ! empty( $calendars ) ) {
18701 foreach ( $calendars as $calendar ) {
18702 $options[] = [
18703 'label' => $calendar->title,
18704 'value' => $calendar->id,
18705 ];
18706 }
18707 }
18708 return [
18709 'options' => $options,
18710 'hasMore' => $calendars_count > $limit && $calendars_count > $offset,
18711 ];
18712
18713 }
18714
18715 /**
18716 * Get Fluent Booking Appointment Events.
18717 *
18718 * @param array $data data.
18719 *
18720 * @return array
18721 */
18722 public function search_fluent_booking_events_list( $data ) {
18723
18724 global $wpdb;
18725 $page = $data['page'];
18726 $limit = Utilities::get_search_page_limit();
18727 $offset = $limit * ( $page - 1 );
18728 $calendar_id = sanitize_text_field( $data['dynamic']['calender_id'] );
18729 if ( '-1' === $calendar_id ) {
18730 $events = $wpdb->get_results(
18731 $wpdb->prepare(
18732 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendar_events WHERE status = %s ORDER BY id DESC LIMIT %d OFFSET %d",
18733 [ 'active', $limit, $offset ]
18734 ),
18735 OBJECT
18736 );
18737 } else {
18738 $events = $wpdb->get_results(
18739 $wpdb->prepare(
18740 "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",
18741 [ 'active', $calendar_id, $limit, $offset ]
18742 ),
18743 OBJECT
18744 );
18745 }
18746
18747 $events_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
18748
18749 $options = [];
18750 if ( ! empty( $events ) ) {
18751 foreach ( $events as $event ) {
18752 $options[] = [
18753 'label' => $event->title,
18754 'value' => $event->id,
18755 ];
18756 }
18757 }
18758 return [
18759 'options' => $options,
18760 'hasMore' => $events_count > $limit && $events_count > $offset,
18761 ];
18762
18763 }
18764
18765 /**
18766 * Get Fluent Booking last data.
18767 *
18768 * @param array $data data.
18769 *
18770 * @return array
18771 */
18772 public function search_fluent_booking_last_data( $data ) {
18773 global $wpdb;
18774 $trigger = $data['search_term'];
18775 $course_data = [];
18776 $lesson_data = [];
18777
18778 $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
18779 $context = [
18780 'pluggable_data' => json_decode( $sample_data, true ),
18781 'response_type' => 'sample',
18782 ];
18783 if ( 'fluent_booking_appointment_cancelled' === $trigger ) {
18784 $status = 'cancelled';
18785 } elseif ( 'fluent_booking_appointment_completed' === $trigger ) {
18786 $status = 'completed';
18787 }
18788
18789 $event_id = (int) $data['filter']['event_id']['value'];
18790 if ( 'fluent_booking_appointment_cancelled' === $trigger || 'fluent_booking_appointment_completed' === $trigger ) {
18791 if ( $event_id > 0 ) {
18792 $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 ) );
18793 } else {
18794 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE status= %s ORDER BY id DESC LIMIT 1", $status ) );
18795 }
18796 } elseif ( 'fluent_booking_new_appointment_booked' === $trigger ) {
18797 if ( $event_id > 0 ) {
18798 $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' ) );
18799 } else {
18800 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE status != %s ORDER BY id DESC LIMIT 1", 'cancelled' ) );
18801 }
18802 }
18803
18804 if ( ! empty( $booking ) ) {
18805 $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 ) );
18806
18807 $booking_event = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_calendar_events WHERE id= %d", $booking->event_id ) );
18808 $booking_data = $booking;
18809 $booking_data->custom_fields = $booking_meta->value;
18810 $booking_array = [
18811 'booking' => $booking_data,
18812 'event' => $booking_event,
18813 ];
18814
18815
18816 $booking_array_response = self::recursive_unserialize( $booking_array );
18817 $context['pluggable_data'] = $booking_array_response;
18818 $context['response_type'] = 'live';
18819 }
18820 return $context;
18821 }
18822
18823 /**
18824 * Recursive unserilize.
18825 *
18826 * @param array $data data.
18827 *
18828 * @return array|mixed
18829 */
18830 public static function recursive_unserialize( $data ) {
18831 if ( is_array( $data ) ) {
18832 foreach ( $data as $key => $value ) {
18833 $data[ $key ] = self::recursive_unserialize( $value );
18834 }
18835 return $data;
18836 } elseif ( is_object( $data ) && 'stdClass' === get_class( $data ) ) {
18837 foreach ( $data as $property => $value ) {
18838 $data->$property = self::recursive_unserialize( $value );
18839 }
18840 return $data;
18841 } elseif ( is_string( $data ) && self::is_serialized( strval( $data ) ) ) {
18842 return unserialize( $data );
18843 } else {
18844 return $data;
18845 }
18846
18847 }
18848
18849 /**
18850 * Check if string serialized.
18851 *
18852 * @param string $data data.
18853 *
18854 * @return bool
18855 */
18856 public static function is_serialized( $data ) {
18857 $unserialized = unserialize( $data );
18858 if ( 'b:0;' === $data || false !== $unserialized ) {
18859 return true;
18860 } else {
18861 return false;
18862 }
18863 }
18864
18865 /**
18866 * Search Tutor LMS data.
18867 *
18868 * @param array $data data.
18869 * @return array|void|mixed
18870 */
18871 public function search_tutor_lms_last_data( $data ) {
18872 global $wpdb;
18873 $post_type = $data['post_type'];
18874 $trigger = $data['search_term'];
18875 $context = [];
18876
18877 if ( ! function_exists( 'tutor_utils' ) ) {
18878 return [];
18879 }
18880
18881 $post_id = -1;
18882 if ( 'course_enrolled' === $trigger || 'tutor_courses_question' === $trigger ) {
18883 $post_id = $data['filter']['tutor_course']['value'];
18884 } elseif ( 'quiz_attempt_percentage' === $trigger || 'quiz_failed' === $trigger || 'quiz_passed' === $trigger ) {
18885 $post_id = $data['filter']['quiz_id']['value'];
18886 }
18887
18888 if ( 'course_enrolled' === $trigger ) {
18889 if ( -1 === $post_id ) {
18890 $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 ) );
18891 } else {
18892 $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 ) );
18893 }
18894 } elseif ( 'tutor_courses_question' === $trigger ) {
18895 if ( -1 === $post_id ) {
18896 $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 ) );
18897 } else {
18898 $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 ) );
18899 }
18900 } elseif ( 'quiz_passed' === $trigger ) {
18901 if ( -1 === $post_id ) {
18902 $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 ) );
18903 } else {
18904 $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 ) );
18905 }
18906 } elseif ( 'quiz_failed' === $trigger ) {
18907 if ( -1 === $post_id ) {
18908 $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 ) );
18909 } else {
18910 $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 ) );
18911 }
18912 } elseif ( 'quiz_attempt_percentage' == $trigger ) {
18913 $condition_compare = $data['filter']['condition_compare']['value'];
18914 $percentage = $data['filter']['percentage']['value'];
18915 if ( -1 === $post_id ) {
18916 $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
18917 } else {
18918 $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
18919 }
18920 }
18921
18922 if ( ! empty( $result ) ) {
18923 switch ( $trigger ) {
18924 case 'course_enrolled':
18925 $result_item_id = $result[0]->post_parent;
18926 $result_user_id = $result[0]->post_author;
18927 $context_data = array_merge(
18928 WordPress::get_user_context( $result_user_id ),
18929 WordPress::get_post_context( $result_item_id )
18930 );
18931 $context_data['tutor_course'] = $result_item_id;
18932 break;
18933 case 'tutor_courses_question':
18934 $date = $result[0]->comment_date;
18935 $data = [
18936 'comment_post_ID' => $result[0]->comment_post_ID,
18937 'comment_author' => $result[0]->comment_author,
18938 'comment_date' => $date,
18939 'comment_date_gmt' => get_gmt_from_date( $date ),
18940 'comment_content' => $result[0]->comment_content,
18941 'comment_approved' => 'approved',
18942 'comment_agent' => 'TutorLMSPlugin',
18943 'comment_type' => 'tutor_q_and_a',
18944 'comment_parent' => $result[0]->comment_parent,
18945 'user_id' => $result[0]->user_id,
18946 ];
18947 $context_data['tutor_course'] = $result[0]->comment_post_ID;
18948 $context_data['data'] = $data;
18949 break;
18950 case 'quiz_attempt_percentage':
18951 case 'quiz_failed':
18952 case 'quiz_passed':
18953 $attempt = tutor_utils()->get_attempt( $result[0]->attempt_id );
18954 $context_data = WordPress::get_user_context( $result[0]->user_id );
18955 $context_data['quiz_id'] = $attempt->quiz_id;
18956 $context_data['attempt_id'] = $result[0]->attempt_id;
18957 $context_data['attempt'] = $attempt;
18958 break;
18959 default:
18960 return;
18961 }
18962 if ( ! empty( $context_data ) ) {
18963 $context['pluggable_data'] = $context_data;
18964 $context['response_type'] = 'live';
18965 }
18966 } elseif ( empty( $result ) ) {
18967 switch ( $trigger ) {
18968 case 'course_enrolled':
18969 $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"} ';
18970 break;
18971 case 'tutor_courses_question':
18972 $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"}';
18973 break;
18974 case 'quiz_attempt_percentage':
18975 case 'quiz_failed':
18976 case 'quiz_passed':
18977 $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"}';
18978 break;
18979 default:
18980 return;
18981 }
18982 $context = json_decode( $sample_data, true );
18983 }
18984
18985 return $context;
18986 }
18987
18988 /**
18989 * Get Asgorus Forum list
18990 *
18991 * @param array $data data.
18992 *
18993 * @return array
18994 */
18995 public function search_asgorus_forums_list( $data ) {
18996 if ( ! class_exists( 'AsgarosForum' ) ) {
18997 return [];
18998 }
18999 $category = $data['dynamic'];
19000 if ( is_array( $category ) ) {
19001 $category_id = $category['forum_category'];
19002 } else {
19003 $category_id = $category;
19004 }
19005 $asgaros_forum = new AsgarosForum();
19006 $forums = $asgaros_forum->get_forums( $category_id );
19007 $options = [];
19008 if ( ! empty( $forums ) ) {
19009 foreach ( $forums as $forum ) {
19010 $options[] = [
19011 'label' => $forum->name,
19012 'value' => $forum->id,
19013 ];
19014 }
19015 }
19016 return [
19017 'options' => $options,
19018 'hasMore' => false,
19019 ];
19020 }
19021
19022 /**
19023 * Get Asgorus Categories list
19024 *
19025 * @param array $data data.
19026 *
19027 * @return array
19028 */
19029 public function search_asgorus_categories_list( $data ) {
19030 if ( ! class_exists( 'AsgarosForum' ) ) {
19031 return [];
19032 }
19033 $asgaros_forum = new AsgarosForum();
19034 $categories = (array) $asgaros_forum->content->get_categories();
19035 $options = [];
19036
19037 if ( ! empty( $categories ) ) {
19038 foreach ( $categories as $category ) {
19039 $options[] = [
19040 'label' => $category->name,
19041 'value' => $category->term_id,
19042 ];
19043 }
19044 }
19045 return [
19046 'options' => $options,
19047 'hasMore' => false,
19048 ];
19049 }
19050
19051 /**
19052 * Get Asgorus Topic lists
19053 *
19054 * @param array $data data.
19055 *
19056 * @return array
19057 */
19058 public function search_asgorus_topic_list( $data ) {
19059 if ( ! class_exists( 'AsgarosForum' ) ) {
19060 return [];
19061 }
19062 global $wpdb;
19063 $forum_id = $data['dynamic'];
19064 $asgaros_forum = new AsgarosForum();
19065 $sql = 'SELECT name,id FROM ' . $wpdb->prefix . 'forum_topics WHERE parent_id = %d AND closed = 0 ORDER BY id';
19066 $topics = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id ), ARRAY_A );// @phpcs:ignore
19067 $options = [];
19068
19069 if ( ! empty( $topics ) ) {
19070 foreach ( $topics as $topic ) {
19071 $options[] = [
19072 'label' => $topic['name'],
19073 'value' => $topic['id'],
19074 ];
19075 }
19076 }
19077 return [
19078 'options' => $options,
19079 'hasMore' => false,
19080 ];
19081 }
19082
19083 /**
19084 * Get Asgorus Topic Last Data
19085 *
19086 * @param array $data data.
19087 *
19088 * @return array
19089 */
19090 public function search_pluggables_asgaros_topic_last_data( $data ) {
19091 if ( ! class_exists( 'AsgarosForum' ) ) {
19092 return [];
19093 }
19094 $asgaros_forum = new AsgarosForum();
19095 $context = [];
19096 global $wpdb;
19097 $forum_id = $data['filter']['forum_id']['value'];
19098
19099 if ( -1 == $forum_id ) {
19100 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'forum_topics WHERE closed = 0 ORDER BY id DESC LIMIT 1', ARRAY_A );
19101 } else {
19102 $forum = $forum_id;
19103 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'forum_topics WHERE parent_id = %d AND closed = 0 ORDER BY id DESC LIMIT 1';
19104 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum ), ARRAY_A );// @phpcs:ignore
19105 }
19106
19107 if ( ! empty( $results ) ) {
19108 $sql_post = 'SELECT * FROM ' . $wpdb->prefix . 'forum_posts WHERE parent_id = %d ORDER BY id DESC LIMIT 1';
19109 $results_post = $wpdb->get_results( $wpdb->prepare( $sql_post, $results[0]['id'] ), ARRAY_A ); // @phpcs:ignore
19110 $context['pluggable_data']['forum_id'] = $results[0]['parent_id'];
19111 $context['pluggable_data']['topic_id'] = $results[0]['id'];
19112 $context['pluggable_data']['post_id'] = $results_post[0]['id'];
19113 $context['pluggable_data']['forum'] = $asgaros_forum->content->get_forum( $results[0]['parent_id'] );
19114 $context['pluggable_data']['topic'] = $asgaros_forum->content->get_topic( $results[0]['id'] );
19115 $context['pluggable_data']['post'] = $asgaros_forum->content->get_post( $results_post[0]['id'] );
19116
19117 $context['pluggable_data']['author'] = WordPress::get_user_context( $results[0]['author_id'] );
19118 $context['response_type'] = 'live';
19119 } else {
19120 $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
19121 }
19122 return $context;
19123 }
19124
19125 /**
19126 * Get Asgorus Topic Reply Last Data
19127 *
19128 * @param array $data data.
19129 *
19130 * @return array
19131 */
19132 public function search_pluggables_asgaros_reply_last_data( $data ) {
19133 if ( ! class_exists( 'AsgarosForum' ) ) {
19134 return [];
19135 }
19136 $asgaros_forum = new AsgarosForum();
19137 $context = [];
19138 global $wpdb;
19139 $forum_id = $data['filter']['forum_id']['value'];
19140 $topic_id = $data['filter']['forum_id']['value'];
19141 if ( -1 == $topic_id ) {
19142 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'forum_posts ORDER BY id DESC LIMIT 1', ARRAY_A );
19143 } else {
19144 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'forum_posts WHERE parent_id = %d ORDER BY id DESC LIMIT 1';
19145 $results = $wpdb->get_results( $wpdb->prepare( $sql, $topic_id ), ARRAY_A );// @phpcs:ignore
19146 }
19147
19148 if ( ! empty( $results ) ) {
19149 $topic_id = $results[0]['parent_id'];
19150 $post_id = $results[0]['id'];
19151 $context['pluggable_data']['forum_id'] = $forum_id;
19152 $context['pluggable_data']['topic_id'] = $topic_id;
19153 $context['pluggable_data']['post_id'] = $post_id;
19154 $context['pluggable_data']['forum'] = $asgaros_forum->content->get_forum( $forum_id );
19155 $context['pluggable_data']['topic'] = $asgaros_forum->content->get_topic( $topic_id );
19156 $context['pluggable_data']['post'] = $asgaros_forum->content->get_post( $post_id );
19157
19158 $context['pluggable_data']['author'] = WordPress::get_user_context( $results[0]['author_id'] );
19159 $context['response_type'] = 'live';
19160 } else {
19161 $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
19162 }
19163 return $context;
19164 }
19165
19166 /**
19167 * Get WPLoyalty Points Awarded Last Data
19168 *
19169 * @param array $data data.
19170 *
19171 * @return array|mixed|string
19172 */
19173 public function search_wp_loyalty_points_awarded_customer( $data ) {
19174
19175 $context = [];
19176 global $wpdb;
19177 if ( ! class_exists( 'Wlr\App\Helpers\Base' ) ) {
19178 return [];
19179 }
19180 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wlr_earn_campaign_transaction WHERE transaction_type = %s ORDER BY id DESC LIMIT 1';
19181 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'credit' ), ARRAY_A );// @phpcs:ignore
19182
19183 if ( ! empty( $results ) ) {
19184 $context['pluggable_data']['user_email'] = $results[0]['user_email'];
19185 $context['pluggable_data']['points_earned'] = $results[0]['points'];
19186 $context['pluggable_data']['action_type'] = $results[0]['action_type'];
19187 $base_helper = new \Wlr\App\Helpers\Base();
19188 $user = $base_helper->getPointUserByEmail( $results[0]['user_email'] );
19189 $points_sql = 'SELECT * FROM ' . $wpdb->prefix . 'wlr_expire_points
19190 WHERE user_email = %s ORDER BY id DESC LIMIT 1';
19191 $points_results = $wpdb->get_results( $wpdb->prepare( $points_sql, $results[0]['user_email'] ), ARRAY_A );// @phpcs:ignore
19192 $context['pluggable_data']['user'] = $user;
19193 if ( ! empty( $points_results ) ) {
19194 $expire_date = $points_results[0]['expire_date'];
19195 $timestamp = is_numeric( $expire_date ) ? (int) $expire_date : null;
19196 $date_format = get_option( 'date_format' );
19197 if ( is_string( $date_format ) ) {
19198 $context['pluggable_data']['point_expiry_date'] = wp_date( $date_format, $timestamp );
19199 }
19200 }
19201 $context['response_type'] = 'live';
19202 } else {
19203 $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
19204 }
19205 return $context;
19206 }
19207
19208 /**
19209 * Get WPLoyalty Campaign Type List
19210 *
19211 * @param array $data data.
19212 *
19213 * @return array
19214 */
19215 public function search_wp_loyalty_action_type_list( $data ) {
19216 $options = [];
19217 if ( ! class_exists( 'Wlr\App\Helpers\Woocommerce' ) ) {
19218 return [];
19219 }
19220
19221 $woocommerce_helper = new \Wlr\App\Helpers\Woocommerce();
19222 $action_types = $woocommerce_helper->getActionTypes();
19223
19224 if ( ! empty( $action_types ) ) {
19225 foreach ( $action_types as $key => $type ) {
19226 $options[] = [
19227 'label' => $type,
19228 'value' => $key,
19229 ];
19230 }
19231 }
19232 return [
19233 'options' => $options,
19234 'hasMore' => false,
19235 ];
19236 }
19237
19238 /**
19239 * Get last data for trigger.
19240 *
19241 * @param array $data data.
19242 * @return array
19243 */
19244 public function search_slicewp_last_data( $data ) {
19245 global $wpdb;
19246
19247 $context = [];
19248 $context['response_type'] = 'sample';
19249
19250 $user_data = WordPress::get_sample_user_context();
19251
19252
19253 if ( ! function_exists( 'slicewp_get_affiliate' ) ) {
19254 return [];
19255 }
19256
19257 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
19258
19259 if ( in_array( $term, [ 'slicewp_new_affiliate', 'slicewp_update_affiliate' ], true ) ) {
19260 $affiliate = [
19261 'id' => 14,
19262 'user_id' => 25,
19263 'date_created' => '2024-03-14 12:35:50',
19264 'date_modified' => '2024-03-14 12:36:20',
19265 'payment_email' => 'testcustomer12@gmail.com',
19266 'website' => '',
19267 'status' => 'active',
19268 'parent_id' => 0,
19269 ];
19270 $context['pluggable_data'] = array_merge( $affiliate, [ 'user' => $user_data ] );
19271
19272 if ( ( ! empty( $data['filter'] ) && '-1' === $data['filter']['affiliate_id']['value'] ) || empty( $data['filter'] ) ) {
19273 $query = $wpdb->prepare(
19274 "
19275 SELECT *
19276 FROM {$wpdb->prefix}slicewp_affiliates
19277 WHERE status = %s
19278 ORDER BY id DESC
19279 LIMIT 1",
19280 'active'
19281 );
19282 $affiliate_results = $wpdb->get_row( $query ); //phpcs:ignore
19283
19284 } else {
19285 $affiliate_id = $data['filter']['affiliate_id']['value'];
19286 $query = $wpdb->prepare(
19287 "
19288 SELECT *
19289 FROM {$wpdb->prefix}slicewp_affiliates
19290 WHERE status = %s AND id = %d
19291 ORDER BY id DESC
19292 LIMIT 1",
19293 'active',
19294 $affiliate_id
19295 );
19296 $affiliate_results = $wpdb->get_row( $query ); //phpcs:ignore
19297
19298 }
19299
19300
19301
19302
19303 if ( ! empty( $affiliate_results ) ) {
19304 $context['pluggable_data'] = (array) $affiliate_results;
19305
19306 $user_data = WordPress::get_user_context( $affiliate_results->user_id );
19307 $context['pluggable_data']['user'] = $user_data;
19308 $context['response_type'] = 'live';
19309 }
19310 } elseif ( in_array( $term, [ 'slicewp_new_commission', 'slicewp_update_commission' ], true ) ) {
19311 $commission = [
19312 'id' => 14,
19313 'user_id' => 25,
19314 'date_created' => '2024-03-14 12:35:50',
19315 'date_modified' => '2024-03-14 12:36:20',
19316 'payment_email' => 'testcustomer12@gmail.com',
19317 'website' => '',
19318 'status' => 'active',
19319 'parent_id' => 0,
19320 ];
19321 $context['pluggable_data'] = array_merge( $commission, [ 'user' => $user_data ] );
19322 $affiliate_id = $data['filter']['affiliate_id']['value'];
19323 if ( -1 === $data['filter']['commission_id']['value'] ) {
19324 $query = $wpdb->prepare(
19325 "
19326 SELECT *
19327 FROM {$wpdb->prefix}slicewp_commissions WHERE affiliate_id=%d ORDER BY id DESC limit 1",
19328 $affiliate_id
19329 );
19330 $commission_results = $wpdb->get_row( $query ); //phpcs:ignore
19331 } else {
19332 $commission_id = $data['filter']['commission_id']['value'];
19333 $query = $wpdb->prepare(
19334 "
19335 SELECT *
19336 FROM {$wpdb->prefix}slicewp_commissions WHERE id= %d AND affiliate_id=%d ORDER BY id DESC limit 1",
19337 $commission_id,
19338 $affiliate_id
19339 );
19340 $commission_results = $wpdb->get_row( $query ); //phpcs:ignore
19341 }
19342
19343
19344
19345
19346 if ( ! empty( $commission_results ) ) {
19347 $context['pluggable_data'] = (array) $commission_results;
19348 $affiliate = slicewp_get_affiliate( $commission_results->affiliate_id );
19349 $user_id = $affiliate->get( 'user_id' );
19350 $user_data = WordPress::get_user_context( $user_id );
19351 $context['pluggable_data']['user'] = $user_data;
19352 $context['response_type'] = 'live';
19353 }
19354 }
19355
19356 return $context;
19357 }
19358
19359
19360 /**
19361 * Get Ninja Tables Last Data
19362 *
19363 * @param array $data data.
19364 *
19365 * @return array
19366 */
19367 public function search_ninja_tables_last_data( $data ) {
19368 $context = [];
19369 global $wpdb;
19370 $table_id = isset( $data['filter']['table_id']['value'] ) ? $data['filter']['table_id']['value'] : -1;
19371
19372 if ( -1 == $table_id ) {
19373 $results = $wpdb->get_row( 'SELECT * FROM ' . $wpdb->prefix . 'ninja_table_items ORDER BY id DESC LIMIT 1', ARRAY_A );
19374 } else {
19375 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'ninja_table_items WHERE table_id = %d ORDER BY id DESC LIMIT 1';
19376 $results = $wpdb->get_row( $wpdb->prepare( $sql, $table_id ), ARRAY_A );// @phpcs:ignore
19377 }
19378
19379 if ( ! empty( $results ) ) {
19380 $results['value'] = json_decode( $results['value'], true );
19381 $context['pluggable_data'] = $results;
19382 $context['pluggable_data']['owner'] = WordPress::get_user_context( $results['owner_id'] );
19383 $context['response_type'] = 'live';
19384 } else {
19385 $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 );
19386 }
19387 return $context;
19388 }
19389
19390 /**
19391 * Get Ninja Tables Fields
19392 *
19393 * @param array $data data.
19394 *
19395 * @return array
19396 */
19397 public function search_ninja_table_fields( $data ) {
19398
19399 $context = [];
19400 global $wpdb;
19401 $table_id = isset( $data['dynamic'] ) ? $data['dynamic'] : -1;
19402 $fields = [];
19403 if ( $table_id > 0 ) {
19404 $fields = get_post_meta( $table_id, '_ninja_table_columns', true );
19405 }
19406
19407 return (array) $fields;
19408 }
19409
19410 /**
19411 * Get Late Point Booking Fields
19412 *
19413 * @param array $data data.
19414 *
19415 * @return array|void
19416 */
19417 public function search_late_point_booking_fields( $data ) {
19418 if ( ! class_exists( 'OsCustomFieldsController' ) ) {
19419
19420 return;
19421 }
19422 global $wpdb;
19423 $booking_fields = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}latepoint_settings WHERE name = %s", 'custom_fields_for_booking' ) );
19424 $fields = [];
19425 if ( ! empty( $booking_fields ) ) {
19426 $fields = json_decode( $booking_fields->value, true );
19427 }
19428 return (array) $fields;
19429 }
19430
19431 /** Search Voxel fields for action.
19432 *
19433 * @param array $data data.
19434 * @return array
19435 */
19436 public function search_voxel_custom_fields( $data ) {
19437 $post_type_fields = [];
19438 $post_type = $data['post_type'];
19439 if ( ! class_exists( '\Voxel\Post_Type' ) ) {
19440 return [];
19441 }
19442 $fields = \Voxel\Post_Type::get( $post_type )->get_fields();
19443 foreach ( $fields as $key => $field ) {
19444 if ( 'step-general' == $key ) {
19445 continue;
19446 }
19447 $field_props = $field->get_props();
19448 $post_type_fields[ $key ] = $field_props;
19449 }
19450 $context['fields'] = $post_type_fields;
19451 return $context;
19452 }
19453
19454 /**
19455 * Get Voxel Post Types lists
19456 *
19457 * @param array $data data.
19458 *
19459 * @return array
19460 */
19461 public function search_voxel_posttype_list( $data ) {
19462 $options = [];
19463 $voxel_post_types = [];
19464 if ( class_exists( '\Voxel\Post_Type' ) ) {
19465 $voxel_post_types = \Voxel\Post_Type::get_voxel_types();
19466 }
19467
19468 if ( ! empty( $voxel_post_types ) ) {
19469 foreach ( $voxel_post_types as $key => $voxel_post_type ) {
19470 $post_type = get_post_type_object( $key );
19471 if ( $post_type ) {
19472 if ( in_array( $post_type->name, [ 'collection' ], true ) ) {
19473 continue;
19474 }
19475 $options[] = [
19476 'label' => esc_attr( $post_type->labels->singular_name ),
19477 'value' => $post_type->name,
19478 ];
19479 }
19480 }
19481 }
19482 return [
19483 'options' => $options,
19484 'hasMore' => false,
19485 ];
19486 }
19487
19488 /**
19489 * Get Voxel Users Last Data
19490 *
19491 * @param array $data data.
19492 *
19493 * @return array|mixed|string
19494 */
19495 public function search_voxel_users_triggers_last_data( $data ) {
19496
19497 global $wpdb;
19498 $context = [];
19499 $term = $data['search_term'];
19500 if ( 'direct_message_received' === $term ) {
19501 $sql = "SELECT * FROM {$wpdb->prefix}voxel_messages WHERE receiver_type LIKE %s AND sender_type LIKE %s ORDER BY id DESC LIMIT 1";
19502 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user', 'user' ), ARRAY_A );// @phpcs:ignore
19503 } elseif ( 'user_timeline_post_created' === $term ) {
19504 $sql = "SELECT vt.* FROM {$wpdb->prefix}voxel_timeline vt WHERE vt.feed = %s AND vt.moderation = %d ORDER BY vt.id DESC LIMIT 1";
19505 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user_timeline', 0 ), ARRAY_A );// @phpcs:ignore
19506 } elseif ( 'user_timeline_post_approved' === $term ) {
19507 $sql = "SELECT vt.* FROM {$wpdb->prefix}voxel_timeline vt WHERE vt.feed = %s AND vt.moderation = %d ORDER BY vt.id DESC LIMIT 1";
19508 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user_timeline', 1 ), ARRAY_A );// @phpcs:ignore
19509 }
19510 if ( 'direct_message_received' === $term ) {
19511 if ( ! empty( $results ) ) {
19512 $context['pluggable_data']['sender'] = WordPress::get_user_context( $results[0]['sender_id'] );
19513 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $results[0]['receiver_id'] );
19514 $context['pluggable_data']['content'] = $results[0]['content'];
19515 $context['response_type'] = 'live';
19516 } else {
19517 $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
19518 }
19519 } elseif ( 'user_timeline_post_created' === $term || 'user_timeline_post_approved' === $term ) {
19520 if ( ! empty( $results ) ) {
19521 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['user_id'] );
19522 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
19523 $status_details = \Voxel\Timeline\Status::get( $results[0]['id'] );
19524 foreach ( (array) $status_details as $key => $value ) {
19525 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
19526 $context['pluggable_data'][ $clean_key ] = $value;
19527 }
19528 }
19529 $context['response_type'] = 'live';
19530 } else {
19531 $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
19532 }
19533 }
19534 return $context;
19535 }
19536
19537
19538 /**
19539 * Get Voxel Membership Last Data
19540 *
19541 * @param array $data data.
19542 *
19543 * @return array|mixed|string
19544 */
19545 public function search_voxel_membership_triggers_last_data( $data ) {
19546
19547 global $wpdb;
19548 $context = [];
19549 $term = $data['search_term'];
19550
19551 if ( ! class_exists( 'Voxel\User' ) ) {
19552 // Return sample data when Voxel class is not available.
19553 if ( 'plan_canceled' === $term ) {
19554 $context['pluggable_data'] = WordPress::get_sample_user_context();
19555 $context['pluggable_data']['details'] = [
19556 'plan' => 'premium-plus',
19557 'type' => 'order',
19558 'order_id' => 14,
19559 'order_item_id' => 17,
19560 'billing' => [
19561 'price_key' => 'premium-plus_pz3xfk7z',
19562 'amount' => 20,
19563 'currency' => 'USD',
19564 'interval' => 'month',
19565 'frequency' => 1,
19566 'current_period' => [
19567 'start' => '2025-10-15 09:01:47',
19568 'end' => '2025-11-15 09:01:47',
19569 ],
19570 'is_active' => 0,
19571 'is_canceled' => 1,
19572 ],
19573 ];
19574 $context['response_type'] = 'sample';
19575 } elseif ( 'plan_activated' === $term ) {
19576 $context['pluggable_data'] = WordPress::get_sample_user_context();
19577 $context['pluggable_data']['details'] = [
19578 'plan' => 'premium-plus',
19579 'type' => 'order',
19580 'order_id' => 14,
19581 'order_item_id' => 17,
19582 'billing' => [
19583 'price_key' => 'premium-plus_pz3xfk7z',
19584 'amount' => 20,
19585 'currency' => 'USD',
19586 'interval' => 'month',
19587 'frequency' => 1,
19588 'current_period' => [
19589 'start' => '2025-10-15 09:01:47',
19590 'end' => '2025-11-15 09:01:47',
19591 ],
19592 'is_active' => 1,
19593 'is_canceled' => '',
19594 ],
19595 ];
19596 $context['response_type'] = 'sample';
19597 } elseif ( 'plan_updated' === $term ) {
19598 $context['pluggable_data'] = WordPress::get_sample_user_context();
19599 $context['pluggable_data']['details'] = [
19600 'plan' => 'premium-plus',
19601 'type' => 'subscription',
19602 'price_id' => 'premium-plus_pz3xfk7z',
19603 'status' => 'active',
19604 'metadata' => [
19605 'voxel:payment_for' => 'membership',
19606 'voxel:plan' => 'premium-plus',
19607 ],
19608 ];
19609 $context['response_type'] = 'sample';
19610 } elseif ( 'user_registered' === $term ) {
19611 $context['pluggable_data'] = WordPress::get_sample_user_context();
19612 $context['response_type'] = 'sample';
19613 }
19614 return $context;
19615 }
19616 if ( 'plan_activated' === $term ) {
19617 $meta_key = ( function_exists( '\Voxel\is_test_mode' ) && \Voxel\is_test_mode() ) ? 'voxel:test_plan' : 'voxel:plan';
19618 $sql = "SELECT
19619 m.user_id AS id,
19620 u.user_login AS title,
19621 u.user_email AS email,
19622 m.meta_value AS details,
19623 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) AS plan,
19624 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.amount' ) ) AS SIGNED ) AS amount,
19625 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.status' ) ) AS status,
19626 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.created' ) ) AS DATETIME ) AS created
19627 FROM {$wpdb->prefix}usermeta as m
19628 LEFT JOIN {$wpdb->prefix}users AS u ON m.user_id = u.ID
19629 WHERE m.meta_key = %s AND JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) != 'default'
19630 ORDER BY m.user_id DESC
19631 LIMIT 25 OFFSET 0";
19632 $results = $wpdb->get_results( $wpdb->prepare( $sql, $meta_key ), ARRAY_A );// @phpcs:ignore
19633 } elseif ( 'plan_canceled' === $term ) {
19634 // Simple approach: Get the most recent user with membership plan and modify data to show as cancelled.
19635 $meta_key = ( function_exists( '\Voxel\is_test_mode' ) && \Voxel\is_test_mode() ) ? 'voxel:test_plan' : 'voxel:plan';
19636 $sql = "SELECT
19637 m.user_id AS id,
19638 u.user_login AS title,
19639 u.user_email AS email,
19640 m.meta_value AS details,
19641 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) AS plan
19642 FROM {$wpdb->prefix}usermeta as m
19643 LEFT JOIN {$wpdb->prefix}users AS u ON m.user_id = u.ID
19644 WHERE m.meta_key = %s AND JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) != 'default'
19645 ORDER BY m.user_id DESC
19646 LIMIT 1";
19647 $results = $wpdb->get_results( $wpdb->prepare( $sql, $meta_key ), ARRAY_A );// @phpcs:ignore
19648 } elseif ( 'plan_updated' === $term ) {
19649 $meta_key = ( function_exists( '\Voxel\is_test_mode' ) && \Voxel\is_test_mode() ) ? 'voxel:test_plan' : 'voxel:plan';
19650 $sql = "SELECT
19651 m.user_id AS id,
19652 u.user_login AS title,
19653 u.user_email AS email,
19654 m.meta_value AS details,
19655 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) AS plan,
19656 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.amount' ) ) AS SIGNED ) AS amount,
19657 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.status' ) ) AS status,
19658 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.created' ) ) AS DATETIME ) AS created
19659 FROM {$wpdb->prefix}usermeta as m
19660 LEFT JOIN {$wpdb->prefix}users AS u ON m.user_id = u.ID
19661 WHERE m.meta_key = %s AND JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) != 'default'
19662 ORDER BY m.user_id DESC
19663 LIMIT 25 OFFSET 0";
19664 $results = $wpdb->get_results( $wpdb->prepare( $sql, $meta_key ), ARRAY_A );// @phpcs:ignore
19665 } elseif ( 'user_registered' === $term ) {
19666 $sql = "SELECT DISTINCT u.ID
19667 FROM {$wpdb->prefix}users u
19668 JOIN {$wpdb->prefix}usermeta um ON u.ID = um.user_id
19669 WHERE um.meta_key = 'voxel:profile_id' ORDER BY ID DESC LIMIT 1";
19670 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
19671 }
19672
19673 if ( 'plan_canceled' === $term ) {
19674 if ( ! empty( $results ) ) {
19675 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
19676 $membership_details = json_decode( $results[0]['details'], true );
19677 $membership_details = is_array( $membership_details ) ? $membership_details : [];
19678
19679 if ( ! isset( $membership_details['billing'] ) && isset( $membership_details['type'] ) ) {
19680 $membership_details = [
19681 'plan' => isset( $membership_details['plan'] ) ? $membership_details['plan'] : 'premium-plus',
19682 'type' => 'order',
19683 'order_id' => isset( $membership_details['order_id'] ) ? $membership_details['order_id'] : 14,
19684 'order_item_id' => isset( $membership_details['order_item_id'] ) ? $membership_details['order_item_id'] : 17,
19685 'billing' => [
19686 'price_key' => isset( $membership_details['price_id'] ) ? $membership_details['price_id'] : 'premium-plus_pz3xfk7z',
19687 'amount' => isset( $membership_details['amount'] ) ? $membership_details['amount'] : 20,
19688 'currency' => isset( $membership_details['currency'] ) ? $membership_details['currency'] : 'USD',
19689 'interval' => isset( $membership_details['interval'] ) ? $membership_details['interval'] : 'month',
19690 'frequency' => isset( $membership_details['interval_count'] ) ? $membership_details['interval_count'] : 1,
19691 'current_period' => [
19692 'start' => gmdate( 'Y-m-d H:i:s' ),
19693 'end' => gmdate( 'Y-m-d H:i:s', strtotime( '+1 month' ) ),
19694 ],
19695 'is_active' => 0,
19696 'is_canceled' => 1,
19697 ],
19698 ];
19699 } else {
19700 if ( isset( $membership_details['billing'] ) ) {
19701 $membership_details['billing']['is_active'] = 0;
19702 $membership_details['billing']['is_canceled'] = 1;
19703 }
19704 }
19705
19706 $context['pluggable_data']['details'] = $membership_details;
19707 $context['response_type'] = 'live';
19708 } else {
19709 $context['pluggable_data'] = WordPress::get_sample_user_context();
19710 $context['pluggable_data']['details'] = [
19711 'plan' => 'premium-plus',
19712 'type' => 'order',
19713 'order_id' => 14,
19714 'order_item_id' => 17,
19715 'billing' => [
19716 'price_key' => 'premium-plus_pz3xfk7z',
19717 'amount' => 20,
19718 'currency' => 'USD',
19719 'interval' => 'month',
19720 'frequency' => 1,
19721 'current_period' => [
19722 'start' => '2025-10-15 09:01:47',
19723 'end' => '2025-11-15 09:01:47',
19724 ],
19725 'is_active' => 0,
19726 'is_canceled' => 1,
19727 ],
19728 ];
19729 $context['response_type'] = 'sample';
19730 }
19731 } elseif ( 'plan_activated' === $term ) {
19732 if ( ! empty( $results ) ) {
19733 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
19734 $membership_details = json_decode( $results[0]['details'], true );
19735 $membership_details = is_array( $membership_details ) ? $membership_details : [];
19736
19737 if ( ! isset( $membership_details['billing'] ) && isset( $membership_details['type'] ) ) {
19738 $membership_details = [
19739 'plan' => isset( $membership_details['plan'] ) ? $membership_details['plan'] : 'premium-plus',
19740 'type' => 'order',
19741 'order_id' => isset( $membership_details['order_id'] ) ? $membership_details['order_id'] : 14,
19742 'order_item_id' => isset( $membership_details['order_item_id'] ) ? $membership_details['order_item_id'] : 17,
19743 'billing' => [
19744 'price_key' => isset( $membership_details['price_id'] ) ? $membership_details['price_id'] : 'premium-plus_pz3xfk7z',
19745 'amount' => isset( $membership_details['amount'] ) ? $membership_details['amount'] : 20,
19746 'currency' => isset( $membership_details['currency'] ) ? $membership_details['currency'] : 'USD',
19747 'interval' => isset( $membership_details['interval'] ) ? $membership_details['interval'] : 'month',
19748 'frequency' => isset( $membership_details['interval_count'] ) ? $membership_details['interval_count'] : 1,
19749 'current_period' => [
19750 'start' => gmdate( 'Y-m-d H:i:s' ),
19751 'end' => gmdate( 'Y-m-d H:i:s', strtotime( '+1 month' ) ),
19752 ],
19753 'is_active' => 1,
19754 'is_canceled' => '',
19755 ],
19756 ];
19757 }
19758
19759 $context['pluggable_data']['details'] = $membership_details;
19760 $context['response_type'] = 'live';
19761 } else {
19762 $context['pluggable_data'] = WordPress::get_sample_user_context();
19763 $context['pluggable_data']['details'] = [
19764 'plan' => 'premium-plus',
19765 'type' => 'order',
19766 'order_id' => 14,
19767 'order_item_id' => 17,
19768 'billing' => [
19769 'price_key' => 'premium-plus_pz3xfk7z',
19770 'amount' => 20,
19771 'currency' => 'USD',
19772 'interval' => 'month',
19773 'frequency' => 1,
19774 'current_period' => [
19775 'start' => '2025-10-15 09:01:47',
19776 'end' => '2025-11-15 09:01:47',
19777 ],
19778 'is_active' => 1,
19779 'is_canceled' => '',
19780 ],
19781 ];
19782 $context['response_type'] = 'sample';
19783 }
19784 } elseif ( 'plan_updated' === $term ) {
19785 if ( ! empty( $results ) ) {
19786 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
19787 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
19788 $context['response_type'] = 'live';
19789 } else {
19790 $context['pluggable_data'] = WordPress::get_sample_user_context();
19791 $context['pluggable_data']['details'] = [
19792 'plan' => 'premium-plus',
19793 'type' => 'subscription',
19794 'price_id' => 'premium-plus_pz3xfk7z',
19795 'status' => 'active',
19796 'metadata' => [
19797 'voxel:payment_for' => 'membership',
19798 'voxel:plan' => 'premium-plus',
19799 ],
19800 ];
19801 $context['response_type'] = 'sample';
19802 }
19803 } elseif ( 'user_registered' === $term ) {
19804 if ( ! empty( $results ) ) {
19805 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['ID'] );
19806 $context['response_type'] = 'live';
19807 } else {
19808 $context['pluggable_data'] = WordPress::get_sample_user_context();
19809 $context['response_type'] = 'sample';
19810 }
19811 }
19812 return $context;
19813 }
19814
19815 /**
19816 * Get Voxel Orders Last Data
19817 *
19818 * @param array $data data.
19819 *
19820 * @return array|mixed|string
19821 */
19822 public function search_voxel_order_triggers_last_data( $data ) {
19823
19824 global $wpdb;
19825 $context = [];
19826 $term = $data['search_term'];
19827
19828 if ( ! class_exists( 'Voxel\Product_Types\Orders\Order' ) ) {
19829 return [];
19830 }
19831
19832 if ( 'order_placed' === $term || 'order_canceled' === $term ) {
19833 if ( 'order_placed' === $term ) {
19834 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders ORDER BY id DESC LIMIT 1";
19835 } elseif ( 'order_canceled' === $term ) {
19836 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' ORDER BY id DESC LIMIT 1";
19837 }
19838
19839 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
19840 if ( ! empty( $results ) ) {
19841 // Get Order.
19842 $context['pluggable_data']['id'] = $results[0]['id'];
19843 $context['pluggable_data']['vendor_id'] = $results[0]['vendor_id'];
19844 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
19845 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
19846 $context['pluggable_data']['status'] = $results[0]['status'];
19847 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
19848 $context['pluggable_data']['subtotal'] = $results[0]['subtotal'];
19849 $context['pluggable_data']['total'] = $results[0]['total'];
19850 // Get order items.
19851 $order = \Voxel\Product_Types\Orders\Order::find(
19852 [
19853 'id' => $results[0]['id'],
19854 ]
19855 );
19856 $order_items = $order->get_items();
19857 $context['pluggable_data']['tax_amount'] = $order->get_tax_amount();
19858 $context['pluggable_data']['discount_amount'] = $order->get_discount_amount();
19859 $context['pluggable_data']['shipping_amount'] = $order->get_shipping_amount();
19860 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
19861 foreach ( $order_items as $item ) {
19862 $addon_data = [];
19863 if ( is_object( $item ) && method_exists( $item, 'get_addons' ) ) {
19864 // Get addon details.
19865 $addons = $item->get_addons();
19866 $addon_data = [];
19867
19868 // Add the addons to the simple entry.
19869 if ( $addons && isset( $addons['summary'] ) ) {
19870 $addon_data = $addons['summary'];
19871 }
19872 }
19873 $context['pluggable_data']['order_items'][] = [
19874 'id' => $item->get_id(),
19875 'type' => $item->get_type(),
19876 'currency' => $item->get_currency(),
19877 'quantity' => $item->get_quantity(),
19878 'subtotal' => $item->get_subtotal(),
19879 'product_id' => $item->get_post()->get_id(),
19880 'product_label' => $item->get_product_label(),
19881 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
19882 'product_link' => $item->get_product_link(),
19883 'description' => $item->get_product_description(),
19884 'addon_data' => $addon_data,
19885 ];
19886 // If booking item, get booking details.
19887 if ( 'booking' === $item->get_type() ) {
19888 $details = $item->get_order_page_details();
19889 $context['pluggable_data']['order_items']['booking_type'] = $details['booking']['type'];
19890 if ( isset( $details['booking']['count_mode'] ) ) {
19891 $context['pluggable_data']['order_items']['booking_count_mode'] = $details['booking']['count_mode'];
19892 }
19893 if ( 'date_range' === $details['booking']['type'] ) {
19894 $context['pluggable_data']['order_items']['booking_start_date'] = $details['booking']['start_date'];
19895 $context['pluggable_data']['order_items']['booking_end_date'] = $details['booking']['end_date'];
19896 } elseif ( 'single_day' === $details['booking']['type'] ) {
19897 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
19898 } elseif ( 'timeslots' === $details['booking']['type'] ) {
19899 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
19900 $context['pluggable_data']['order_items']['booking_slot_from'] = $details['booking']['slot']['from'];
19901 $context['pluggable_data']['order_items']['booking_slot_to'] = $details['booking']['slot']['to'];
19902 }
19903 }
19904 }
19905 // Get Customer.
19906 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
19907 $context['response_type'] = 'live';
19908 } else {
19909 $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
19910 }
19911 } elseif ( 'order_approved' === $term || 'order_declined' === $term ) {
19912 if ( 'order_approved' === $term ) {
19913 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'completed' ORDER BY id DESC LIMIT 1";
19914 } elseif ( 'order_declined' === $term ) {
19915 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' ORDER BY id DESC LIMIT 1";
19916 }
19917 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
19918 if ( ! empty( $results ) ) {
19919 // Get Order.
19920 $context['pluggable_data']['id'] = $results[0]['id'];
19921 $context['pluggable_data']['vendor_id'] = $results[0]['vendor_id'];
19922 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
19923 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
19924 $context['pluggable_data']['status'] = $results[0]['status'];
19925 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
19926 $context['pluggable_data']['subtotal'] = $results[0]['subtotal'];
19927 $context['pluggable_data']['total'] = $results[0]['total'];
19928 // Get order items.
19929 $order = \Voxel\Product_Types\Orders\Order::find(
19930 [
19931 'id' => $results[0]['id'],
19932 ]
19933 );
19934 $order_items = $order->get_items();
19935 $context['pluggable_data']['tax_amount'] = $order->get_tax_amount();
19936 $context['pluggable_data']['discount_amount'] = $order->get_discount_amount();
19937 $context['pluggable_data']['shipping_amount'] = $order->get_shipping_amount();
19938 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
19939 foreach ( $order_items as $item ) {
19940 $addon_data = [];
19941 if ( is_object( $item ) && method_exists( $item, 'get_addons' ) ) {
19942 // Get addon details.
19943 $addons = $item->get_addons();
19944 $addon_data = [];
19945
19946 // Add the addons to the simple entry.
19947 if ( $addons && isset( $addons['summary'] ) ) {
19948 $addon_data = $addons['summary'];
19949 }
19950 }
19951 $context['pluggable_data']['order_items'][] = [
19952 'id' => $item->get_id(),
19953 'type' => $item->get_type(),
19954 'currency' => $item->get_currency(),
19955 'quantity' => $item->get_quantity(),
19956 'subtotal' => $item->get_subtotal(),
19957 'product_id' => $item->get_post()->get_id(),
19958 'product_label' => $item->get_product_label(),
19959 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
19960 'product_link' => $item->get_product_link(),
19961 'description' => $item->get_product_description(),
19962 'addon_data' => $addon_data,
19963 ];
19964 // If booking item, get booking details.
19965 if ( 'booking' === $item->get_type() ) {
19966 $details = $item->get_order_page_details();
19967 $context['pluggable_data']['order_items']['booking_type'] = $details['booking']['type'];
19968 if ( isset( $details['booking']['count_mode'] ) ) {
19969 $context['pluggable_data']['order_items']['booking_count_mode'] = $details['booking']['count_mode'];
19970 }
19971 if ( 'date_range' === $details['booking']['type'] ) {
19972 $context['pluggable_data']['order_items']['booking_start_date'] = $details['booking']['start_date'];
19973 $context['pluggable_data']['order_items']['booking_end_date'] = $details['booking']['end_date'];
19974 } elseif ( 'single_day' === $details['booking']['type'] ) {
19975 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
19976 } elseif ( 'timeslots' === $details['booking']['type'] ) {
19977 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
19978 $context['pluggable_data']['order_items']['booking_slot_from'] = $details['booking']['slot']['from'];
19979 $context['pluggable_data']['order_items']['booking_slot_to'] = $details['booking']['slot']['to'];
19980 }
19981 }
19982 }
19983 // Get Customer.
19984 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
19985 // Get Vendor.
19986 $context['pluggable_data']['vendor'] = WordPress::get_user_context( $results[0]['vendor_id'] );
19987 $context['response_type'] = 'live';
19988 } else {
19989 $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
19990 }
19991 } elseif ( 'order_promotion_activated' === $term || 'order_promotion_canceled' === $term || 'order_claim_listing' === $term ) {
19992 if ( 'order_promotion_activated' === $term ) {
19993 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'completed' AND details LIKE '%voxel:promotion%' ORDER BY id DESC LIMIT 1";
19994 } elseif ( 'order_promotion_canceled' === $term ) {
19995 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' AND details LIKE '%voxel:promotion%' ORDER BY id DESC LIMIT 1";
19996 } elseif ( 'order_claim_listing' === $term ) {
19997 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE details LIKE '%voxel:claim%' ORDER BY id DESC LIMIT 1";
19998 }
19999 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
20000 if ( ! empty( $results ) ) {
20001 // Get Order.
20002 $context['pluggable_data']['id'] = $results[0]['id'];
20003 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
20004 $context['pluggable_data']['status'] = $results[0]['status'];
20005 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
20006 // Get order items.
20007 $order = \Voxel\Product_Types\Orders\Order::find(
20008 [
20009 'id' => $results[0]['id'],
20010 ]
20011 );
20012 $order_items = $order->get_items();
20013 $context['pluggable_data']['details'] = $order->get_details();
20014 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
20015 foreach ( $order_items as $item ) {
20016 $context['pluggable_data']['order_items'][] = [
20017 'id' => $item->get_id(),
20018 'type' => $item->get_type(),
20019 'currency' => $item->get_currency(),
20020 'quantity' => $item->get_quantity(),
20021 'subtotal' => $item->get_subtotal(),
20022 'product_id' => $item->get_post()->get_id(),
20023 'product_label' => $item->get_product_label(),
20024 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
20025 'product_link' => $item->get_product_link(),
20026 'description' => $item->get_product_description(),
20027 ];
20028 }
20029 // Get Customer.
20030 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
20031 $context['response_type'] = 'live';
20032 } else {
20033 $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
20034 }
20035 }
20036 return $context;
20037 }
20038
20039 /**
20040 * Get Voxel Timeline Comments Last Data
20041 *
20042 * @param array $data data.
20043 *
20044 * @return array|mixed|string
20045 */
20046 public function search_voxel_timeline_triggers_last_data( $data ) {
20047
20048 global $wpdb;
20049 $context = [];
20050 $term = $data['search_term'];
20051 $sql = '';
20052 if ( 'new_comment_timeline' === $term ) {
20053 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE parent_id IS NULL ORDER BY id DESC LIMIT 1";
20054 } elseif ( 'comment_reply_timeline' === $term ) {
20055 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE parent_id IS NOT NULL ORDER BY id DESC LIMIT 1";
20056 } elseif ( 'comment_liked' === $term ) {
20057 $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";
20058 } elseif ( 'user_timeline_post_liked' === $term ) {
20059 $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";
20060 } elseif ( 'user_timeline_post_quoted' === $term ) {
20061 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline WHERE feed = %s AND quote_of IS NOT NULL ORDER BY id DESC LIMIT 1";
20062 } elseif ( 'user_timeline_post_reposted' === $term ) {
20063 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline WHERE feed = %s AND repost_of IS NOT NULL ORDER BY id DESC LIMIT 1";
20064 }
20065 if ( 'user_timeline_post_reposted' === $term || 'user_timeline_post_quoted' === $term ) {
20066 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user_timeline' ), ARRAY_A );// @phpcs:ignore
20067 } else {
20068 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
20069 }
20070 if ( 'new_comment_timeline' === $term ) {
20071 if ( ! empty( $results ) ) {
20072 $context['pluggable_data']['comment_by'] = WordPress::get_user_context( $results[0]['user_id'] );
20073 $context['pluggable_data']['id'] = $results[0]['id'];
20074 $context['pluggable_data']['status_id'] = $results[0]['status_id'];
20075 $context['pluggable_data']['content'] = $results[0]['content'];
20076 $context['response_type'] = 'live';
20077 } else {
20078 $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
20079 }
20080 } elseif ( 'comment_reply_timeline' === $term ) {
20081 if ( ! empty( $results ) ) {
20082 $comment_sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE id = %d";
20083 $comment_result = $wpdb->get_results( $wpdb->prepare( $comment_sql, $results[0]['parent_id']), ARRAY_A );// @phpcs:ignore
20084 $context['pluggable_data']['replied_by'] = WordPress::get_user_context( $results[0]['user_id'] );
20085 $context['pluggable_data']['comment_by'] = WordPress::get_user_context( $comment_result[0]['user_id'] );
20086 $context['pluggable_data']['comment'] = $comment_result[0]['content'];
20087 $context['pluggable_data']['comment_id'] = $results[0]['parent_id'];
20088 $context['pluggable_data']['reply_id'] = $results[0]['id'];
20089 $context['pluggable_data']['reply'] = $results[0]['content'];
20090 $context['response_type'] = 'live';
20091 } else {
20092 $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
20093 }
20094 } elseif ( 'comment_liked' === $term ) {
20095 if ( ! empty( $results ) ) {
20096 $user = get_userdata( $results[0]['user_id'] );
20097 if ( $user ) {
20098 $user_data = (array) $user->data;
20099 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
20100 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
20101 $context['pluggable_data']['user_email'] = $user_data['user_email'];
20102 }
20103 $recipient_user = get_userdata( $results[0]['like_user_id'] );
20104 if ( $recipient_user ) {
20105 $recipient_user_data = (array) $recipient_user->data;
20106 $context['pluggable_data']['recipient']['user_display_name'] = $recipient_user_data['display_name'];
20107 $context['pluggable_data']['recipient']['user_name'] = $recipient_user_data['user_nicename'];
20108 $context['pluggable_data']['recipient']['user_email'] = $recipient_user_data['user_email'];
20109 }
20110 if ( class_exists( 'Voxel\Timeline\Reply' ) ) {
20111 $reply_details = \Voxel\Timeline\Reply::get( $results[0]['id'] );
20112 foreach ( (array) $reply_details as $key => $value ) {
20113 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
20114 $context['pluggable_data'][ $clean_key ] = $value;
20115 }
20116 }
20117 $context['response_type'] = 'live';
20118 } else {
20119 $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
20120 }
20121 } elseif ( 'user_timeline_post_liked' === $term ) {
20122 if ( ! empty( $results ) ) {
20123 $user = get_userdata( $results[0]['user_id'] );
20124 if ( $user ) {
20125 $user_data = (array) $user->data;
20126 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
20127 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
20128 $context['pluggable_data']['user_email'] = $user_data['user_email'];
20129 }
20130 $recipient_user = get_userdata( $results[0]['like_user_id'] );
20131 if ( $recipient_user ) {
20132 $recipient_user_data = (array) $recipient_user->data;
20133 $context['pluggable_data']['recipient']['user_display_name'] = $recipient_user_data['display_name'];
20134 $context['pluggable_data']['recipient']['user_name'] = $recipient_user_data['user_nicename'];
20135 $context['pluggable_data']['recipient']['user_email'] = $recipient_user_data['user_email'];
20136 }
20137 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
20138 $reply_details = \Voxel\Timeline\Status::get( $results[0]['id'] );
20139 foreach ( (array) $reply_details as $key => $value ) {
20140 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
20141 $context['pluggable_data'][ $clean_key ] = $value;
20142 }
20143 }
20144 $context['response_type'] = 'live';
20145 } else {
20146 $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
20147 }
20148 } elseif ( 'user_timeline_post_reposted' === $term || 'user_timeline_post_quoted' === $term ) {
20149 if ( ! empty( $results ) ) {
20150 $author = get_userdata( $results[0]['user_id'] );
20151 if ( $author ) {
20152 $author_data = (array) $author->data;
20153 $context['pluggable_data']['author_display_name'] = $author_data['display_name'];
20154 $context['pluggable_data']['author_name'] = $author_data['user_nicename'];
20155 $context['pluggable_data']['author_email'] = $author_data['user_email'];
20156 }
20157 $column = 'user_timeline_post_reposted' === $term ? 'repost_of' : 'quote_of';
20158 $recipient_sql = "SELECT user_id FROM {$wpdb->prefix}voxel_timeline WHERE id = %d";
20159 $recipient_results = $wpdb->get_results( $wpdb->prepare( $recipient_sql, $results[0][$column] ), ARRAY_A ); // @phpcs:ignore
20160 if ( ! empty( $recipient_results ) ) {
20161 $recipient_user = get_userdata( $recipient_results[0]['user_id'] );
20162 if ( $recipient_user ) {
20163 $recipient_user_data = (array) $recipient_user->data;
20164 $context['pluggable_data']['recipient']['user_display_name'] = $recipient_user_data['display_name'];
20165 $context['pluggable_data']['recipient']['user_name'] = $recipient_user_data['user_nicename'];
20166 $context['pluggable_data']['recipient']['user_email'] = $recipient_user_data['user_email'];
20167 }
20168 }
20169 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
20170 $status_details = \Voxel\Timeline\Status::get( $results[0]['id'] );
20171 $details_column = 'user_timeline_post_reposted' === $term ? 'repost_of' : 'quote_of';
20172 $related_details = \Voxel\Timeline\Status::get( $results[0][ $details_column ] );
20173 foreach ( [
20174 'status' => $status_details,
20175 $details_column => $related_details,
20176 ] as $key => $details ) {
20177 foreach ( (array) $details as $sub_key => $value ) {
20178 $clean_key = preg_replace( '/^\0.*?\0/', '', $sub_key );
20179 $context['pluggable_data'][ $key ][ $clean_key ] = $value;
20180 }
20181 }
20182 }
20183 $context['response_type'] = 'live';
20184 } else {
20185 if ( 'user_timeline_post_reposted' === $term ) {
20186 $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
20187 } else {
20188 $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
20189 }
20190 }
20191 }
20192 return $context;
20193 }
20194
20195 /**
20196 * Get Voxel Timeline Comments Last Data
20197 *
20198 * @param array $data data.
20199 *
20200 * @return array|mixed|string
20201 */
20202 public function search_voxel_posts_triggers_last_data( $data ) {
20203 $context = [];
20204 global $wpdb;
20205 $post_type = 'post';
20206 $term = $data['search_term'];
20207 if ( isset( $data['filter']['post_type']['value'] ) ) {
20208 $post_type = $data['filter']['post_type']['value'];
20209 }
20210 $results = [];
20211 if ( 'post_approved' === $term ) {
20212 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
20213 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type, 'publish'), ARRAY_A );// @phpcs:ignore
20214 } elseif ( 'post_rejected' === $term ) {
20215 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
20216 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type, 'rejected'), ARRAY_A );// @phpcs:ignore
20217 } elseif ( 'post_reviewed' === $term ) {
20218 $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
20219 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";
20220 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type, '%rating%' ), ARRAY_A );// @phpcs:ignore
20221 } elseif ( 'post_review_approved' === $term ) {
20222 $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
20223 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";
20224 $results = $wpdb->get_results( $wpdb->prepare( $sql, 1, $post_type, '%rating%', 'post_reviews' ), ARRAY_A );// @phpcs:ignore
20225 } elseif ( 'post_submitted' === $term ) {
20226 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
20227 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type), ARRAY_A );// @phpcs:ignore
20228 } elseif ( 'post_updated' === $term ) {
20229 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
20230 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type ), ARRAY_A );// @phpcs:ignore
20231 } elseif ( 'collection_post_submitted' === $term ) {
20232 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
20233 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'collection' ), ARRAY_A );// @phpcs:ignore
20234 } elseif ( 'collection_post_updated' === $term ) {
20235 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
20236 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'collection' ), ARRAY_A );// @phpcs:ignore
20237 } elseif ( 'new_wall_post_by_user' === $term ) {
20238 $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";
20239 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type, '%rating%' ), ARRAY_A );// @phpcs:ignore
20240 } elseif ( 'wall_post_approved' === $term ) {
20241 $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";
20242 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'post_wall', $post_type, 1 ), ARRAY_A );// @phpcs:ignore
20243 } elseif ( 'timeline_post_approved' === $term ) {
20244 $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";
20245 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'post_timeline', 1, $post_type ), ARRAY_A );// @phpcs:ignore
20246 } elseif ( 'taxonomy_updated' === $term ) {
20247 $sql = "SELECT tr.object_id as post_id, tt.taxonomy, tr.term_taxonomy_id
20248 FROM {$wpdb->prefix}term_relationships tr
20249 JOIN {$wpdb->prefix}term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
20250 JOIN {$wpdb->prefix}posts p ON tr.object_id = p.ID
20251 WHERE p.post_type LIKE %s
20252 ORDER BY tr.object_id DESC LIMIT 1";
20253 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type ), ARRAY_A );// @phpcs:ignore
20254 } elseif ( 'post_followed' === $term ) {
20255 $sql = "SELECT * FROM {$wpdb->prefix}voxel_followers WHERE object_type = %s LIMIT 1";
20256 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'post' ), ARRAY_A );// @phpcs:ignore
20257 }
20258 if ( 'post_approved' === $term || 'post_rejected' === $term || 'post_submitted' === $term || 'post_updated' === $term || 'timeline_post_approved' === $term ) {
20259 if ( ! empty( $results ) ) {
20260 $context['pluggable_data'] = WordPress::get_post_context( $results[0]['ID'] );
20261 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['ID'] );
20262 $context['response_type'] = 'live';
20263 } else {
20264 $context['pluggable_data'] = [
20265 'ID' => 557,
20266 'post' => 557,
20267 'post_author' => 1,
20268 'post_date' => '2022-11-18 12:18:14',
20269 'post_date_gmt' => '2022-11-18 12:18:14',
20270 'post_content' => 'Test Post Content',
20271 'post_title' => 'Test Post',
20272 'post_excerpt' => '',
20273 'post_status' => 'published',
20274 'comment_status' => 'open',
20275 'ping_status' => 'open',
20276 'post_password' => '',
20277 'post_name' => 'test-post',
20278 'to_ping' => '',
20279 'pinged' => '',
20280 'post_modified' => '2022-11-18 12:18:14',
20281 'post_modified_gmt' => '2022-11-18 12:18:14',
20282 'post_content_filtered' => '',
20283 'post_parent' => 0,
20284 'guid' => 'https://example.com/test-post/',
20285 'menu_order' => 0,
20286 'post_type' => 'post',
20287 'post_mime_type' => '',
20288 'comment_count' => 0,
20289 'filter' => 'raw',
20290 ];
20291 $context['response_type'] = 'sample';
20292 }
20293 } elseif ( 'post_reviewed' === $term || 'post_review_approved' === $term ) {
20294 if ( ! empty( $results ) ) {
20295 $context['pluggable_data'] = WordPress::get_post_context( $results[0]['post_id'] );
20296 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['post_id'] );
20297 $context['pluggable_data']['review_content'] = $results[0]['content'];
20298 $context['pluggable_data']['review_created_at'] = $results[0]['created_at'];
20299 $context['pluggable_data']['review_details'] = json_decode( $results[0]['details'], true );
20300 $context['pluggable_data']['review_by'] = WordPress::get_user_context( $results[0]['user_id'] );
20301 $context['response_type'] = 'live';
20302 } else {
20303 $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
20304 }
20305 } elseif ( 'collection_post_submitted' === $term || 'collection_post_updated' === $term ) {
20306 if ( ! empty( $results ) ) {
20307 $context['pluggable_data'] = Voxel::get_post_fields( $results[0]['ID'] );
20308 $context['pluggable_data']['collection'] = WordPress::get_post_context( $results[0]['ID'] );
20309 $context['response_type'] = 'live';
20310 } else {
20311 $context = json_decode(
20312 '{"pluggable_data":{"field_step-general": null,"field_title": "First Collection","field_items": [8909,8293],"collection": {"ID": 9142,"post_author": "35",
20313 "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
20314 }
20315 } elseif ( 'new_wall_post_by_user' === $term || 'wall_post_approved' === $term ) {
20316 if ( ! empty( $results ) ) {
20317 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['post_id'] );
20318 $user = get_userdata( $results[0]['user_id'] );
20319 if ( $user ) {
20320 $user_data = (array) $user->data;
20321 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
20322 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
20323 $context['pluggable_data']['user_email'] = $user_data['user_email'];
20324 $context['pluggable_data']['user_id'] = $results[0]['user_id'];
20325 }
20326 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
20327 $args = [
20328 'post_id' => $results[0]['post_id'],
20329 'order_by' => 'created_at',
20330 'order' => 'desc',
20331 'feed' => $results[0]['feed'],
20332 'limit' => 1,
20333 'moderation' => $results[0]['moderation'],
20334 ];
20335 $statuses = \Voxel\Timeline\Status::query( $args );
20336 $status_details = $statuses['items'][0];
20337 foreach ( (array) $status_details as $key => $value ) {
20338 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
20339 if ( is_object( $value ) ) {
20340 $encoded_value = wp_json_encode( $value );
20341 if ( is_string( $encoded_value ) ) {
20342 $value = json_decode( $encoded_value, true );
20343 }
20344 }
20345 $context['pluggable_data']['wall_post'][ $clean_key ] = $value;
20346 }
20347 }
20348 $context['response_type'] = 'live';
20349 } else {
20350 $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
20351 }
20352 } elseif ( 'taxonomy_updated' === $term ) {
20353 if ( ! empty( $results ) ) {
20354 $post_id = $results[0]['post_id'];
20355 $context['pluggable_data'] = WordPress::get_post_context( $post_id );
20356 $context['pluggable_data']['post'] = Voxel::get_post_fields( $post_id );
20357 $context['pluggable_data']['post_type'] = get_post_type( $post_id );
20358
20359 // Get taxonomy details.
20360 $term_obj = get_term( $results[0]['term_taxonomy_id'] );
20361 if ( $term_obj && ! is_wp_error( $term_obj ) ) {
20362 $context['pluggable_data']['taxonomy'] = $results[0]['taxonomy'];
20363 $context['pluggable_data']['update_type'] = 'updated';
20364 $context['pluggable_data']['terms_added'] = [ $results[0]['term_taxonomy_id'] ];
20365 $context['pluggable_data']['terms_removed'] = [];
20366 $context['pluggable_data']['current_terms'] = [ $results[0]['term_taxonomy_id'] ];
20367 $context['pluggable_data']['previous_terms'] = [];
20368 $context['pluggable_data']['added_terms_details'] = [
20369 [
20370 'term_id' => $term_obj->term_id,
20371 'name' => $term_obj->name,
20372 'slug' => $term_obj->slug,
20373 ],
20374 ];
20375 $context['pluggable_data']['removed_terms_details'] = [];
20376 $context['pluggable_data']['current_terms_details'] = [
20377 [
20378 'term_id' => $term_obj->term_id,
20379 'name' => $term_obj->name,
20380 'slug' => $term_obj->slug,
20381 ],
20382 ];
20383 $context['pluggable_data']['is_voxel_taxonomy_field'] = false;
20384 $context['pluggable_data']['voxel_field_data'] = null;
20385 }
20386 $context['response_type'] = 'live';
20387 } else {
20388 $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
20389 }
20390 } elseif ( 'post_followed' === $term ) {
20391 if ( ! empty( $results ) ) {
20392 $follow_data = [
20393 'post_id' => $results[0]['object_id'],
20394 'user_id' => $results[0]['follower_id'],
20395 'status' => $results[0]['status'],
20396 ];
20397 $follow_data = array_merge( $follow_data, Voxel::get_post_fields( $results[0]['object_id'] ), WordPress::get_post_context( $results[0]['object_id'] ) );
20398 $followers_sql = "SELECT COUNT(*) FROM {$wpdb->prefix}voxel_followers WHERE object_id= %d";
20399 $followers = $wpdb->get_var( $wpdb->prepare( $followers_sql, $results[0]['object_id'] ));// @phpcs:ignore
20400 $follow_data['total_followers'] = $followers;
20401 $context['pluggable_data'] = $follow_data;
20402 $context['response_type'] = 'live';
20403 } else {
20404 $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
20405 }
20406 }
20407 return $context;
20408 }
20409
20410 /**
20411 * Get Voxel Profiles Last Data
20412 *
20413 * @param array $data data.
20414 *
20415 * @return array|mixed|string
20416 */
20417 public function search_voxel_profiles_triggers_last_data( $data ) {
20418
20419 global $wpdb;
20420 $context = [];
20421 $term = $data['search_term'];
20422
20423 if ( ! class_exists( 'Voxel\Timeline\Status' ) || ! class_exists( 'Voxel\Post_Type' ) ) {
20424 return [];
20425 }
20426
20427 if ( 'profile_created' === $term ) {
20428 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
20429 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
20430 } elseif ( 'profile_approved' === $term ) {
20431 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
20432 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile', 'publish' ), ARRAY_A );// @phpcs:ignore
20433 } elseif ( 'profile_rejected' === $term ) {
20434 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
20435 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile', 'rejected' ), ARRAY_A );// @phpcs:ignore
20436 } elseif ( 'profile_reviewed' === $term ) {
20437 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline vt LEFT JOIN {$wpdb->prefix}posts pm
20438 ON vt.post_id = pm.ID
20439 WHERE pm.post_type LIKE %s AND vt.details IS NOT NULL ORDER BY vt.id DESC LIMIT 1";
20440 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
20441 } elseif ( 'profile_updated' === $term ) {
20442 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
20443 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
20444 } elseif ( 'profile_new_wall_post' === $term ) {
20445 $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";
20446 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
20447 }
20448 if ( 'profile_created' === $term || 'profile_approved' === $term || 'profile_rejected' === $term || 'profile_updated' === $term ) {
20449 if ( ! empty( $results ) ) {
20450 $context['pluggable_data'] = Voxel::get_post_fields( $results[0]['ID'] );
20451 $user = get_userdata( $results[0]['post_author'] );
20452 if ( $user ) {
20453 $user_data = (array) $user->data;
20454 $context['pluggable_data']['profile_display_name'] = $user_data['display_name'];
20455 $context['pluggable_data']['profile_email'] = $user_data['user_email'];
20456 $context['pluggable_data']['profile_user_id'] = $results[0]['post_author'];
20457 $context['pluggable_data']['profile_id'] = $results[0]['ID'];
20458 }
20459 $context['response_type'] = 'live';
20460 } else {
20461 $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
20462 }
20463 } elseif ( 'profile_new_wall_post' === $term ) {
20464 if ( ! empty( $results ) ) {
20465 $context['pluggable_data']['profile'] = Voxel::get_post_fields( $results[0]['post_id'] );
20466 $user = get_userdata( $results[0]['user_id'] );
20467 if ( $user ) {
20468 $user_data = (array) $user->data;
20469 $context['pluggable_data']['profile_display_name'] = $user_data['display_name'];
20470 $context['pluggable_data']['profile_name'] = $user_data['user_nicename'];
20471 $context['pluggable_data']['profile_email'] = $user_data['user_email'];
20472 $context['pluggable_data']['profile_user_id'] = $results[0]['user_id'];
20473 }
20474 // Get the status details.
20475 $args = [
20476 'post_id' => $results[0]['post_id'],
20477 'order_by' => 'created_at',
20478 'order' => 'desc',
20479 'feed' => $results[0]['feed'],
20480 'limit' => 1,
20481 'moderation' => $results[0]['moderation'],
20482 ];
20483 $statuses = \Voxel\Timeline\Status::query( $args );
20484 $status_details = $statuses['items'][0];
20485 foreach ( (array) $status_details as $key => $value ) {
20486 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
20487 if ( is_object( $value ) ) {
20488 $encoded_value = wp_json_encode( $value );
20489 if ( is_string( $encoded_value ) ) {
20490 $value = json_decode( $encoded_value, true );
20491 }
20492 }
20493 $context['pluggable_data']['wall_post'][ $clean_key ] = $value;
20494 }
20495 $context['response_type'] = 'live';
20496 } else {
20497 $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
20498 }
20499 } elseif ( 'profile_reviewed' === $term ) {
20500 if ( ! empty( $results ) ) {
20501 // Get the review details.
20502 $args = [
20503 'post_id' => $results[0]['post_id'],
20504 'order_by' => 'created_at',
20505 'order' => 'desc',
20506 'feed' => $results[0]['feed'],
20507 'limit' => 1,
20508 'moderation' => $results[0]['moderation'],
20509 ];
20510 $statuses = \Voxel\Timeline\Status::query( $args );
20511 $review_details = $statuses['items'][0];
20512 foreach ( (array) $review_details as $key => $value ) {
20513 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
20514 if ( 'user_can_edit' == $clean_key || 'publisher' == $clean_key || 'user_can_edit' == $clean_key || 'user_can_moderate' == $clean_key ) {
20515 continue;
20516 }
20517 if ( 'files' === $clean_key ) {
20518 $value = wp_json_encode( $value );
20519 } elseif ( 'details' === $clean_key ) {
20520 $review_ratings = isset( $value['rating'] ) && is_array( $value['rating'] ) ? $value['rating'] : [];
20521 $value['rating'] = [];
20522 $type = \Voxel\Post_Type::get( 'profile' );
20523
20524 if ( ! empty( $review_ratings ) ) {
20525 $rating_levels = $type->reviews->get_rating_levels();
20526 $categories = $type->reviews->get_categories();
20527
20528 foreach ( $categories as $category ) {
20529 $category_key = $category['key'];
20530 $category_label = strtolower( $category['label'] );
20531
20532 if ( isset( $review_ratings[ $category_key ] ) && $category_label ) {
20533 foreach ( $rating_levels as $rating_level ) {
20534 if ( $review_ratings[ $category_key ] === $rating_level['score'] ) {
20535 $value['rating'][ $category_label ] = $rating_level['label'];
20536 break;
20537 }
20538 }
20539 }
20540 }
20541 }
20542 } else {
20543 $clean_key = 'review_' . $clean_key;
20544 }
20545 $context['pluggable_data'][ $clean_key ] = $value;
20546 }
20547 $context['response_type'] = 'live';
20548 } else {
20549 $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
20550 }
20551 }
20552 return $context;
20553 }
20554
20555 /**
20556 * Get Voxel Messages Triggers Last Data
20557 *
20558 * @param array $data data.
20559 *
20560 * @return array|mixed|string
20561 */
20562 public function search_voxel_messages_triggers_last_data( $data ) {
20563 global $wpdb;
20564 $context = [];
20565 $term = $data['search_term'];
20566
20567 if ( 'voxel_new_message_post_created' === $term ) {
20568 $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}voxel_messages ORDER BY id DESC LIMIT %d", 1 ), ARRAY_A );
20569
20570 if ( ! empty( $results ) ) {
20571 $message = $results[0];
20572 $context['pluggable_data']['sender'] = WordPress::get_user_context( $message['sender_id'] );
20573 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $message['receiver_id'] );
20574 $context['pluggable_data']['content'] = $message['content'];
20575 $context['pluggable_data']['message_id'] = $message['id'];
20576 $context['pluggable_data']['created_at'] = $message['created_at'];
20577 $context['response_type'] = 'live';
20578 } else {
20579 $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 );
20580 }
20581 }
20582
20583 return $context;
20584 }
20585
20586 /**
20587 * Get Late Point Customer Fields
20588 *
20589 * @param array $data data.
20590 *
20591 * @return array|void
20592 */
20593 public function search_late_point_customer_fields( $data ) {
20594 if ( ! class_exists( 'OsCustomFieldsController' ) ) {
20595
20596 return;
20597 }
20598 global $wpdb;
20599 $booking_fields = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}latepoint_settings WHERE name = %s", 'custom_fields_for_customer' ) );
20600 $fields = [];
20601 if ( ! empty( $booking_fields ) ) {
20602 $fields = json_decode( $booking_fields->value, true );
20603 }
20604 return (array) $fields;
20605 }
20606
20607 /**
20608 * Is multi person booking
20609 *
20610 * @param array $data data.
20611 *
20612 * @return array|void
20613 */
20614 public function search_late_point_multi_person_booking( $data ) {
20615 if ( ! class_exists( 'LatePointAddonGroupBookings' ) ) {
20616
20617 return;
20618 }
20619 $service_id = isset( $data['dynamic'] ) ? $data['dynamic'] : '-1';
20620
20621 if ( -1 == $service_id ) {
20622 return;
20623 }
20624 global $wpdb;
20625 $booking_details = $wpdb->get_row( $wpdb->prepare( "SELECT capacity_max,capacity_min FROM {$wpdb->prefix}latepoint_services WHERE id= %s", intval( $service_id ) ) );
20626 if ( ! empty( $booking_details ) ) {
20627 return (array) $booking_details;
20628 }
20629
20630 }
20631
20632 /**
20633 * Get Mail Mint lists
20634 *
20635 * @param array $data data.
20636 *
20637 * @return array
20638 */
20639 public function search_mail_mint_contact_lists( $data ) {
20640 global $wpdb;
20641
20642 $lists = $wpdb->get_results( $wpdb->prepare( 'SELECT title, id FROM ' . $wpdb->prefix . 'mint_contact_groups WHERE type = %s', 'lists' ) );
20643 $options = [];
20644
20645 if ( ! empty( $lists ) ) {
20646 foreach ( $lists as $list ) {
20647 $options[] = [
20648 'label' => $list->title,
20649 'value' => $list->id,
20650 ];
20651 }
20652 }
20653 return [
20654 'options' => $options,
20655 'hasMore' => false,
20656 ];
20657 }
20658
20659 /**
20660 * Get Mail Mint tags
20661 *
20662 * @param array $data data.
20663 *
20664 * @return array
20665 */
20666 public function search_mail_mint_contact_tags( $data ) {
20667 global $wpdb;
20668
20669 $lists = $wpdb->get_results( $wpdb->prepare( 'SELECT title, id FROM ' . $wpdb->prefix . 'mint_contact_groups WHERE type = %s ', 'tags' ) );
20670 $options = [];
20671
20672 if ( ! empty( $lists ) ) {
20673 foreach ( $lists as $list ) {
20674 $options[] = [
20675 'label' => $list->title,
20676 'value' => $list->id,
20677 ];
20678 }
20679 }
20680 return [
20681 'options' => $options,
20682 'hasMore' => false,
20683 ];
20684 }
20685
20686 /** Get Better Messages trigger Last Data
20687 *
20688 * @param array $data data.
20689 *
20690 * @return array|mixed|string
20691 */
20692 public function search_better_messages_triggers_data( $data ) {
20693 $context = [];
20694 global $wpdb;
20695 if ( ! function_exists( 'Better_Messages' ) ) {
20696 return [];
20697 }
20698 $sql = "SELECT * FROM {$wpdb->prefix}bm_message_messages ORDER BY id DESC LIMIT 1";
20699 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
20700
20701 if ( ! empty( $results ) ) {
20702 $message = Better_Messages()->functions->get_message( $results[0]['id'] );
20703 if ( is_object( $message ) ) {
20704 $message = get_object_vars( $message );
20705 }
20706 $context['pluggable_data'] = $message;
20707 $context['pluggable_data']['sender'] = WordPress::get_user_context( $results[0]['sender_id'] );
20708 $context['response_type'] = 'live';
20709 } else {
20710 $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
20711 }
20712 return $context;
20713 }
20714
20715 /**
20716 * Get Appointment Hour Booking trigger Last Data
20717 *
20718 * @param array $data data.
20719 *
20720 * @return array|mixed|string
20721 */
20722 public function search_ahb_appointment_booked_triggers_last_data( $data ) {
20723 $context = [];
20724 global $wpdb;
20725 $sql = "SELECT * FROM {$wpdb->prefix}cpappbk_messages ORDER BY id DESC LIMIT 1";
20726 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
20727
20728 if ( ! empty( $results ) ) {
20729 $context['pluggable_data'] = unserialize( $results[0]['posted_data'] );
20730 $context['response_type'] = 'live';
20731 } else {
20732 $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
20733 }
20734 return $context;
20735 }
20736
20737 /**
20738 * Prepare mailmint contact status.
20739 *
20740 * @param array $data Search Params.
20741 *
20742 * @return array
20743 */
20744 public function search_mail_mint_fetch_custom_fields( $data ) {
20745
20746 $options = [
20747 [
20748 'label' => 'Yes',
20749 'value' => 'true',
20750 ],
20751 [
20752 'label' => 'No',
20753 'value' => 'false',
20754 ],
20755 ];
20756
20757 return [
20758 'options' => $options,
20759 'hasMore' => false,
20760 ];
20761 }
20762
20763 /**
20764 * Get Mail Mint custom fields
20765 *
20766 * @param array $data data.
20767 *
20768 * @return array
20769 */
20770 public function search_mail_mint_custom_fields( $data ) {
20771 global $wpdb;
20772 $fields = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'mint_custom_fields' );
20773
20774 return (array) $fields;
20775
20776 }
20777
20778 /**
20779 * Get Mail Mint lists
20780 *
20781 * @param array $data data.
20782 *
20783 * @return array|void
20784 */
20785 public function search_mail_mint_contacts( $data ) {
20786 if ( ! class_exists( 'Mint\MRM\DataBase\Models\ContactModel' ) ) {
20787 return [];
20788 }
20789 $contacts = ContactModel::get_all();
20790 $options = [];
20791 if ( ! empty( $contacts ) ) {
20792 foreach ( $contacts['data'] as $contact ) {
20793 $options[] = [
20794 'label' => $contact['email'],
20795 'value' => $contact['id'],
20796 ];
20797 }
20798 }
20799 return [
20800 'options' => $options,
20801 'hasMore' => false,
20802 ];
20803 }
20804
20805 /**
20806 * Get mail mint Last Data
20807 *
20808 * @param array $data data.
20809 *
20810 * @return array
20811 */
20812 public function search_mail_mint_last_data( $data ) {
20813 if ( ! class_exists( 'Mint\MRM\DataBase\Models\ContactModel' ) || ! class_exists( 'Mint\MRM\DataBase\Models\ContactGroupModel' ) ) {
20814 return [];
20815 }
20816 $context = [];
20817 global $wpdb;
20818 $contact_id = isset( $data['filter']['contact_id']['value'] ) ? $data['filter']['contact_id']['value'] : -1;
20819 $term = $data['search_term'] ? $data['search_term'] : '';
20820 $type = 'lists';
20821 if ( 'mail_mint_tags_added_to_contact' === $term ) {
20822 $type = 'tags';
20823 }
20824 $data = [];
20825 if ( -1 == $contact_id ) {
20826 $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 ) );
20827 } else {
20828 $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 ) );
20829 }
20830
20831 if ( ! empty( $result ) ) {
20832 $contact_id = $result->contact_id;
20833 $data['contact'] = ContactModel::get( $contact_id );
20834 $data[ $type ] = ContactGroupModel::get( $result->id );
20835 }
20836 if ( ! empty( $data ) ) {
20837 $context['pluggable_data'] = $data;
20838 $context['response_type'] = 'live';
20839 } else {
20840 $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 );
20841 }
20842 return (array) $context;
20843 }
20844
20845
20846 /**
20847 * Get Simple Schedule Appointment Types
20848 *
20849 * @param array $data data.
20850 *
20851 * @return array
20852 */
20853 public function search_simple_schedule_appointment_types( $data ) {
20854 global $wpdb;
20855
20856 $appointment_types = $wpdb->get_results( 'SELECT title, id FROM ' . $wpdb->prefix . 'ssa_appointment_types' );
20857 $options = [];
20858
20859 if ( ! empty( $appointment_types ) ) {
20860 foreach ( $appointment_types as $appointment_type ) {
20861 $options[] = [
20862 'label' => $appointment_type->title,
20863 'value' => $appointment_type->id,
20864 ];
20865 }
20866 }
20867 return [
20868 'options' => $options,
20869 'hasMore' => false,
20870 ];
20871 }
20872
20873 /**
20874 * Get mail mint Last Data
20875 *
20876 * @param array $data data.
20877 *
20878 * @return array
20879 */
20880 public function search_ssa_last_data( $data ) {
20881 $context = [];
20882 global $wpdb;
20883 $appointment_type_id = isset( $data['filter']['appointment_type_id']['value'] ) ? $data['filter']['appointment_type_id']['value'] : -1;
20884 $term = $data['search_term'] ? $data['search_term'] : '';
20885 $status = 'booked';
20886 if ( 'ssa_appointment_cancelled' === $term ) {
20887 $status = 'canceled';
20888 }
20889 $data = [];
20890 if ( -1 == $appointment_type_id ) {
20891 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointments WHERE status = %s ORDER BY id DESC Limit 1", $status ), ARRAY_A );
20892 } else {
20893 $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 );
20894 }
20895
20896
20897 if ( ! empty( $result ) ) {
20898 $result_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointment_meta where appointment_id=%d", $result['id'] ) );
20899 if ( ! empty( $result_meta ) ) {
20900 foreach ( $result_meta as $meta ) {
20901 $result[ $meta->meta_key ] = $meta->meta_value;
20902 }
20903 }
20904 }
20905 if ( ! empty( $result ) ) {
20906
20907 $result['customer_information'] = json_decode( $result['customer_information'], true );
20908 $context['pluggable_data'] = $result;
20909 $context['response_type'] = 'live';
20910 } else {
20911 $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 );
20912 }
20913 return (array) $context;
20914 }
20915
20916 /**
20917 * Get Simple Schedule Appointment Types
20918 *
20919 * @param array $data data.
20920 *
20921 * @return array
20922 */
20923 public function search_simple_schedule_appointments( $data ) {
20924 global $wpdb;
20925
20926 $appointment_types = $wpdb->get_results( 'SELECT title, id FROM ' . $wpdb->prefix . 'ssa_appointment_types' );
20927 $options = [];
20928
20929 if ( ! empty( $appointment_types ) ) {
20930 foreach ( $appointment_types as $appointment_type ) {
20931 $options[] = [
20932 'label' => $appointment_type->title,
20933 'value' => $appointment_type->id,
20934 ];
20935 }
20936 }
20937 return [
20938 'options' => $options,
20939 'hasMore' => false,
20940 ];
20941 }
20942
20943 /**
20944 * Get Sensei LMS Lessons
20945 *
20946 * @param array $data data.
20947 *
20948 * @return array
20949 */
20950 public function search_sensei_lms_lessons( $data ) {
20951 if ( ! function_exists( 'Sensei' ) ) {
20952 return [];
20953 }
20954 $course_id = $data['dynamic'];
20955 $lessons = \Sensei()->course->course_lessons( $course_id, 'publish', 'ids' );
20956 $options = [];
20957
20958 if ( ! empty( $lessons ) ) {
20959 foreach ( $lessons as $lesson ) {
20960 $options[] = [
20961 'label' => get_the_title( $lesson ),
20962 'value' => $lesson,
20963 ];
20964 }
20965 }
20966 return [
20967 'options' => $options,
20968 'hasMore' => false,
20969 ];
20970 }
20971
20972 /**
20973 * Get Sensei LMS Quiz List
20974 *
20975 * @param array $data data.
20976 *
20977 * @return array
20978 */
20979 public function search_sensei_lms_quiz( $data ) {
20980 $page = $data['page'];
20981 $limit = Utilities::get_search_page_limit();
20982 $offset = $limit * ( $page - 1 );
20983
20984 $quizes = get_posts(
20985 [
20986 'post_type' => 'lesson',
20987 'orderby' => 'title',
20988 'order' => 'ASC',
20989 'post_status' => 'publish',
20990 'posts_per_page' => $limit,
20991 'offset' => $offset,
20992 'fields' => 'ids',
20993 'meta_query' => [
20994 'relation' => 'AND',
20995 [
20996 'key' => '_lesson_quiz',
20997 'compare' => 'EXISTS',
20998 ],
20999 [
21000 'key' => '_quiz_has_questions',
21001 'value' => 1,
21002 'compare' => '=',
21003 ],
21004 ],
21005 ]
21006 );
21007 $options = [];
21008 $total_quizes = get_posts(
21009 [
21010 'post_type' => 'lesson',
21011 'orderby' => 'title',
21012 'order' => 'ASC',
21013 'post_status' => 'publish',
21014 'posts_per_page' => -1,
21015 'fields' => 'ids',
21016 'meta_query' => [
21017 'relation' => 'AND',
21018 [
21019 'key' => '_lesson_quiz',
21020 'compare' => 'EXISTS',
21021 ],
21022 [
21023 'key' => '_quiz_has_questions',
21024 'value' => 1,
21025 'compare' => '=',
21026 ],
21027 ],
21028 ]
21029 );
21030 $count = count( $total_quizes );
21031 if ( ! empty( $quizes ) ) {
21032 foreach ( $quizes as $quiz ) {
21033 $options[] = [
21034 'label' => get_the_title( $quiz ),
21035 'value' => $quiz,
21036 ];
21037 }
21038 }
21039 return [
21040 'options' => $options,
21041 'hasMore' => $count > $limit && $count > $offset,
21042 ];
21043 }
21044
21045 /**
21046 * Get Sensei LMS trigger Last Data
21047 *
21048 * @param array $data data.
21049 *
21050 * @return array|mixed|string
21051 */
21052 public function search_sensei_lms_triggers_last_data( $data ) {
21053 $context = [];
21054 global $wpdb;
21055 $term = $data['search_term'];
21056
21057 if ( ! function_exists( 'Sensei' ) ) {
21058 return;
21059 }
21060
21061 if ( 'course_completed' == $term ) {
21062 $comment_type = 'sensei_course_status';
21063 $course_id = $data['filter']['sensei_course']['value'];
21064 if ( -1 === $course_id ) {
21065 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s ORDER BY comment_ID DESC LIMIT 1";
21066 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type ), ARRAY_A );// @phpcs:ignore
21067 } else {
21068 $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";
21069 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, $course_id ), ARRAY_A );// @phpcs:ignore
21070 }
21071 } elseif ( 'course_enrolled' == $term ) {
21072 $comment_type = 'sensei_course_status';
21073 $course_id = $data['filter']['sensei_course']['value'];
21074 if ( -1 === $course_id ) {
21075 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s ORDER BY comment_ID DESC LIMIT 1";
21076 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'in-progress', $comment_type ), ARRAY_A );// @phpcs:ignore
21077 } else {
21078 $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";
21079 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'in-progress', $comment_type, $course_id ), ARRAY_A );// @phpcs:ignore
21080 }
21081 } elseif ( 'lesson_completed' == $term ) {
21082 $lesson_id = $data['filter']['sensei_lesson']['value'];
21083 $course_id = $data['filter']['sensei_course']['value'];
21084 $comment_type = 'sensei_lesson_status';
21085 if ( -1 === $lesson_id ) {
21086 $sql = "SELECT comments.*, meta.* FROM {$wpdb->prefix}comments as comments
21087 LEFT JOIN {$wpdb->prefix}postmeta as meta
21088 ON comments.comment_post_ID = meta.post_id
21089 WHERE comments.comment_approved = %s
21090 AND comments.comment_type = %s
21091 AND meta.meta_key = %s AND
21092 meta.meta_value = %d
21093 ORDER BY comments.comment_ID DESC LIMIT 1";
21094 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, '_lesson_course', $course_id ), ARRAY_A );// @phpcs:ignore
21095 } else {
21096 $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";
21097 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, $lesson_id ), ARRAY_A );// @phpcs:ignore
21098 }
21099 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term ) {
21100 $quiz_id = $data['filter']['sensei_quiz']['value'];
21101 $comment_type = 'sensei_lesson_status';
21102 if ( 'quiz_passes' == $term ) {
21103 $status = "( comment_approved = 'passed' )";
21104 } elseif ( 'quiz_fails' == $term ) {
21105 $status = "( comment_approved = 'failed' )";
21106 } else {
21107 $status = "( comment_approved = 'failed' OR comment_approved = 'passed' )";
21108 }
21109 if ( -1 === $quiz_id ) {
21110 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type = %s AND $status ORDER BY comment_ID DESC LIMIT 1";
21111 $results = $wpdb->get_results( $wpdb->prepare( $sql, $comment_type ), ARRAY_A );// @phpcs:ignore
21112 } else {
21113 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type = %s AND $status AND comment_post_ID = %d ORDER BY comment_ID DESC LIMIT 1";
21114 $results = $wpdb->get_results( $wpdb->prepare( $sql, $comment_type, $quiz_id ), ARRAY_A );// @phpcs:ignore
21115 }
21116 } elseif ( 'quiz_completion' == $term ) {
21117 $quiz_id = $data['filter']['sensei_quiz']['value'];
21118 $comment_type = 'sensei_lesson_status';
21119 $condition_compare = $data['filter']['condition_compare']['value'];
21120 $percentage = $data['filter']['percentage']['value'];
21121 if ( -1 === $quiz_id ) {
21122 $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
21123 } else {
21124 $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
21125 }
21126 }
21127
21128 if ( ! empty( $results ) ) {
21129 $post = get_post( $results[0]['comment_post_ID'] );
21130 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['user_id'] );
21131 if ( 'course_completed' == $term || 'course_enrolled' == $term ) {
21132 $context['pluggable_data']['sensei_course'] = $results[0]['comment_post_ID'];
21133 if ( $post instanceof \WP_Post ) {
21134 $context['pluggable_data']['course_title'] = $post->post_title;
21135 }
21136 } elseif ( 'lesson_completed' == $term ) {
21137 $context['pluggable_data']['sensei_lesson'] = $results[0]['comment_post_ID'];
21138 if ( $post instanceof \WP_Post ) {
21139 $context['pluggable_data']['lesson_title'] = $post->post_title;
21140 }
21141 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term || 'quiz_completion' == $term ) {
21142 $lesson_quiz = get_post_meta( $results[0]['comment_post_ID'], '_lesson_quiz', true );
21143 $submission = \Sensei()->quiz_submission_repository->get( $lesson_quiz, $results[0]['user_id'] );
21144 if ( $post instanceof \WP_Post ) {
21145 $context['pluggable_data']['quiz_title'] = $post->post_title;
21146 }
21147 $context['pluggable_data']['quiz_status'] = $results[0]['comment_approved'];
21148 $context['pluggable_data']['quiz_data']['id'] = $submission->get_id();
21149 $context['sensei_quiz'] = $submission->get_id();
21150 $context['pluggable_data']['quiz_data']['final_grade'] = $submission->get_final_grade();
21151 $context['pluggable_data']['quiz_data']['created_at'] = $submission->get_created_at();
21152 }
21153 $context['response_type'] = 'live';
21154 } else {
21155 if ( 'course_completed' == $term || 'course_enrolled' == $term ) {
21156 $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
21157 } elseif ( 'lesson_completed' == $term ) {
21158 $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
21159 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term || 'quiz_completion' == $term ) {
21160 $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
21161 }
21162 }
21163 return $context;
21164 }
21165
21166 /**
21167 * Get SurelyWP Services - SureCart Addons Last Data
21168 *
21169 * @param array $data data.
21170 *
21171 * @return array
21172 */
21173 public function search_services_sc_triggers_last_data( $data ) {
21174 $context = [];
21175 global $wpdb;
21176 $term = $data['search_term'] ? $data['search_term'] : '';
21177 $data = [];
21178
21179 if ( 'new_service_created' === $term ) {
21180 $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 );
21181 } elseif ( 'requirement_submitted' === $term ) {
21182 $result = $wpdb->get_row( "SELECT created_at FROM {$wpdb->prefix}surelywp_sv_requirements ORDER BY created_at DESC Limit 1", ARRAY_A );
21183 } elseif ( 'message_sent' === $term ) {
21184 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_messages ORDER BY message_id DESC Limit 1", ARRAY_A );
21185 } elseif ( 'message_final_delivery_sent' === $term ) {
21186 $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 );
21187 } elseif ( 'customer_request_revision' === $term ) {
21188 $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 );
21189 } elseif ( 'customer_approves_final_delivery' === $term ) {
21190 $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 );
21191 } elseif ( 'delivery_date_changed' === $term ) {
21192 $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 );
21193 } elseif ( 'service_cancel' === $term || 'service_marked_canceled' === $term ) {
21194 $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 );
21195 } elseif ( 'service_completed' === $term || 'service_mark_completed' === $term ) {
21196 $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 );
21197 } elseif ( 'contract_signed' === $term ) {
21198 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_contracts ORDER BY contract_id DESC Limit 1", ARRAY_A );
21199 }
21200 if ( ! empty( $result ) ) {
21201 if ( 'new_service_created' === $term ) {
21202 $service_data = [
21203 'service_setting_id' => $result['service_id'],
21204 'order_id' => $result['order_id'],
21205 'product_id' => $result['product_id'],
21206 'service_status' => $result['service_status'],
21207 'delivery_date' => $result['delivery_date'],
21208 ];
21209 $context['pluggable_data'] = array_merge( $service_data, WordPress::get_user_context( $result['user_id'] ) );
21210 $context['response_type'] = 'live';
21211 } elseif ( 'requirement_submitted' === $term ) {
21212 $requirements_data = $wpdb->get_results(
21213 $wpdb->prepare(
21214 "SELECT * FROM {$wpdb->prefix}surelywp_sv_requirements WHERE created_at = %s",
21215 $result['created_at']
21216 ),
21217 ARRAY_A
21218 );
21219 $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 );
21220 $user_data = WordPress::get_user_context( $service_result['user_id'] );
21221 unset( $service_result['user_id'] );
21222 $context['pluggable_data'] = array_merge( $requirements_data, $service_result, $user_data );
21223 foreach ( $requirements_data as $value ) {
21224 if ( 'file' == $value['requirement_type'] ) {
21225 $upload_dir = wp_upload_dir();
21226 $attachment_file_names = json_decode( $value['requirement'], true );
21227 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
21228 $context['pluggable_data']['requirement_attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $value['service_id'] . '/requirement/' . $attachment_file_name;
21229 }
21230 }
21231 }
21232 $context['response_type'] = 'live';
21233 } elseif ( 'message_sent' === $term || 'message_final_delivery_sent' === $term ) {
21234 $message_data = [
21235 'sender' => WordPress::get_user_context( $result['sender_id'] ),
21236 'receiver' => WordPress::get_user_context( $result['receiver_id'] ),
21237 'service_id' => $result['service_id'],
21238 'message_text' => $result['message_text'],
21239 'attachment_file_name' => $result['attachment_file_name'],
21240 'is_final_delivery' => $result['is_final_delivery'],
21241 ];
21242 $context['pluggable_data'] = $message_data;
21243 $upload_dir = wp_upload_dir();
21244 $attachment_file_names = json_decode( $result['attachment_file_name'], true );
21245 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
21246 $context['pluggable_data']['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $result['service_id'] . '/messages/' . $attachment_file_name;
21247 }
21248 $context['response_type'] = 'live';
21249 } elseif ( 'customer_request_revision' === $term ) {
21250 $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 );
21251 if ( ! empty( $message_result ) ) {
21252 global $surelywp_sv_model;
21253 $revision_message = $surelywp_sv_model->surelywp_sv_get_customer_revision_msg( $message_result['service_id'], $message_result['message_id'] );
21254 $context['pluggable_data'] = array_merge( $result, $revision_message );
21255 $context['pluggable_data']['sender'] = WordPress::get_user_context( $revision_message['sender_id'] );
21256 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $revision_message['receiver_id'] );
21257 $upload_dir = wp_upload_dir();
21258 $attachment_file_names = json_decode( $revision_message['attachment_file_name'], true );
21259 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
21260 $context['pluggable_data']['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $revision_message['service_id'] . '/messages/' . $attachment_file_name;
21261 }
21262 $context['response_type'] = 'live';
21263 } else {
21264 $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 );
21265 }
21266 } elseif ( 'customer_approves_final_delivery' === $term || 'delivery_date_changed' === $term || 'service_cancel' === $term || 'service_marked_canceled' === $term || 'service_completed' === $term || 'service_mark_completed' === $term ) {
21267 $user_data = WordPress::get_user_context( $result['user_id'] );
21268 unset( $result['user_id'] );
21269 $context['pluggable_data'] = array_merge( $result, $user_data );
21270 $context['response_type'] = 'live';
21271 } elseif ( 'contract_signed' === $term ) {
21272 $contract_data = [
21273 'service_id' => $result['service_id'],
21274 'signature' => $result['signature'],
21275 'contract_details' => $result['contract_details'],
21276 ];
21277 $service_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_id = %d", $result['service_id'] ), ARRAY_A );
21278 $user_data = WordPress::get_user_context( $service_result['user_id'] );
21279 unset( $service_result['user_id'] );
21280 $context['pluggable_data'] = array_merge( $contract_data, $user_data, $service_result );
21281 $context['response_type'] = 'live';
21282 }
21283 } else {
21284 if ( 'new_service_created' === $term || 'customer_request_revision' === $term || 'customer_approves_final_delivery' === $term || 'delivery_date_changed' === $term ) {
21285 $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 );
21286 } elseif ( 'requirement_submitted' === $term ) {
21287 $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 );
21288 } elseif ( 'message_sent' === $term || 'message_final_delivery_sent' === $term ) {
21289 $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 );
21290 } elseif ( 'service_cancel' === $term ) {
21291 $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 );
21292 } elseif ( 'service_marked_canceled' === $term || 'service_completed' === $term || 'service_mark_completed' === $term ) {
21293 $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 );
21294 } elseif ( 'contract_signed' === $term ) {
21295 $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 );
21296 }
21297 }
21298 return (array) $context;
21299 }
21300
21301 /**
21302 * Get SurelyWP Support Portal - SureCart Addons Last Data
21303 *
21304 * @param array $data data.
21305 *
21306 * @return array
21307 */
21308 public function search_sc_support_portal_triggers_last_data( $data ) {
21309 $context = [];
21310 if ( ! class_exists( 'Surelywp_Support_Portal' ) ) {
21311 return [];
21312 }
21313 global $wpdb;
21314 $term = $data['search_term'] ? $data['search_term'] : '';
21315 $data = [];
21316
21317 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
21318 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sp_support WHERE support_status = 1 ORDER BY support_id DESC Limit 1", ARRAY_A );
21319 } elseif ( 'new_message_sent' === $term ) {
21320 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sp_messages ORDER BY message_id DESC Limit 1", ARRAY_A );
21321 } elseif ( 'tickets_closed' === $term ) {
21322 $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 );
21323 } elseif ( 'tickets_opens' === $term ) {
21324 $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 );
21325 } elseif ( 'new_email_message_fetched' === $term ) {
21326 $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 );
21327 }
21328 if ( ! empty( $result ) ) {
21329 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
21330 $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 );
21331 $support_data = [
21332 'support_id' => $result['support_id'],
21333 'order_id' => $result['order_id'],
21334 'product_id' => $result['product_id'],
21335 'support_title' => $result['support_title'],
21336 'support_status' => Surelywp_Support_Portal::surelywp_sp_get_support_status( $result['support_status'] ),
21337 'support_data' => $support_res,
21338 ];
21339 $context['pluggable_data'] = array_merge( $support_data, WordPress::get_user_context( $result['user_id'] ) );
21340 $context['response_type'] = 'live';
21341 } elseif ( 'tickets_closed' === $term || 'tickets_opens' === $term ) {
21342 $support_data = [];
21343 $support_ids = $result;
21344 foreach ( $support_ids as $key => $id ) {
21345 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sp_support WHERE support_id = %d", $id ), ARRAY_A );
21346 $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 );
21347 $support_data[ $key ] = WordPress::get_user_context( $result['user_id'] );
21348 $support_data[ $key ] = [
21349 'support_id' => $result['support_id'],
21350 'order_id' => $result['order_id'],
21351 'product_id' => $result['product_id'],
21352 'support_title' => $result['support_title'],
21353 'support_status' => Surelywp_Support_Portal::surelywp_sp_get_support_status( $result['support_status'] ),
21354 'support_data' => $support_res,
21355 ];
21356 }
21357 $context['pluggable_data'] = $support_data;
21358 $context['response_type'] = 'live';
21359 } elseif ( 'new_message_sent' === $term ) {
21360 $upload_dir = wp_upload_dir();
21361 $support_data = [
21362 'sender_id' => $result['sender_id'],
21363 'receiver_id' => $result['receiver_id'],
21364 'support_id' => $result['support_id'],
21365 'message_text' => $result['message_text'],
21366 'sender' => WordPress::get_user_context( $result['sender_id'] ),
21367 'receiver' => WordPress::get_user_context( $result['receiver_id'] ),
21368 ];
21369 if ( ! empty( $result['attachment_file_name'] ) ) {
21370 $attachment_file_names = json_decode( $result['attachment_file_name'], true );
21371 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
21372 $support_data['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-support-portal-data/' . $result['support_id'] . '/messages/' . $attachment_file_name;
21373 }
21374 }
21375 $context['pluggable_data'] = $support_data;
21376 $context['response_type'] = 'live';
21377 }
21378 } else {
21379 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
21380 $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 );
21381 } elseif ( 'tickets_closed' === $term || 'tickets_opens' === $term ) {
21382 $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 );
21383 } elseif ( 'new_message_sent' === $term ) {
21384 $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 );
21385 }
21386 }
21387 return (array) $context;
21388 }
21389
21390 /**
21391 * Get Fluent Boards Last Data
21392 *
21393 * @param array $data data.
21394 *
21395 * @return array
21396 */
21397 public function search_fbs_triggers_last_data( $data ) {
21398 if ( ! class_exists( '\FluentBoards\App\Models\Stage' ) ) {
21399 return [];
21400 }
21401 global $wpdb;
21402
21403 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
21404 if ( ! class_exists( 'FluentBoards\App\Models\Board' ) || ! class_exists( 'FluentBoards\App\Models\User' ) ) {
21405 return [];
21406 }
21407
21408 $context = [];
21409 $result = null;
21410
21411 switch ( $term ) {
21412 case 'board_created':
21413 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_boards ORDER BY id DESC LIMIT 1", ARRAY_A );
21414 break;
21415 case 'board_member_added':
21416 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_relations WHERE object_type = 'board_user' ORDER BY id DESC LIMIT 1", ARRAY_A );
21417 break;
21418 case 'task_created':
21419 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_tasks ORDER BY id DESC LIMIT 1", ARRAY_A );
21420 break;
21421 case 'stage_changed':
21422 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_tasks ORDER BY id DESC LIMIT 1", ARRAY_A );
21423 break;
21424
21425 }
21426
21427 if ( ! empty( $result ) ) {
21428 switch ( $term ) {
21429 case 'board_created':
21430 $context['pluggable_data'] = \FluentBoards\App\Models\Board::findOrFail( $result['id'] );
21431 $context['response_type'] = 'live';
21432 break;
21433 case 'board_member_added':
21434 $context['pluggable_data']['board_id'] = $result['object_id'];
21435 $context['pluggable_data']['board_member'] = \FluentBoards\App\Models\User::find( $result['foreign_id'] );
21436 $context['response_type'] = 'live';
21437 break;
21438 case 'task_created':
21439 $stage_data = Stage::where( 'board_id', $result['board_id'] )->whereNull( 'archived_at' )->first();
21440
21441 $context['pluggable_data'] = [
21442 'id' => $result['id'],
21443 'slug' => $result['slug'],
21444 'title' => $result['title'],
21445 'description' => $result['description'],
21446 'type' => $result['type'],
21447 'board_id' => $result['board_id'],
21448 'stage_id' => $result['stage_id'],
21449 'position' => $result['position'],
21450 'priority' => $result['priority'],
21451 'created_at' => $result['created_at'],
21452 'created_by' => $result['created_by'],
21453 'updated_at' => $result['updated_at'],
21454 'settings' => unserialize( $result['settings'] ),
21455 'stage' => $stage_data ? [
21456 'id' => $stage_data->id,
21457 'slug' => $stage_data->slug,
21458 'title' => $stage_data->title,
21459 'type' => $stage_data->type,
21460 'board_id' => $stage_data->board_id,
21461 'position' => $stage_data->position,
21462 'settings' => $stage_data->settings,
21463 'created_at' => '',
21464 'updated_at' => '',
21465 ] : null,
21466 ];
21467 $context['response_type'] = 'live';
21468 break;
21469 case 'stage_changed':
21470 $stage_data = Stage::where( 'board_id', $result['board_id'] )->whereNull( 'archived_at' )->first();
21471 $user_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}users ORDER BY id DESC LIMIT 1", ARRAY_A );
21472
21473 $context['pluggable_data'] = [
21474 'task' => [
21475 'id' => $result['id'],
21476 'slug' => $result['slug'],
21477 'title' => $result['title'],
21478 'description' => $result['description'],
21479 'type' => $result['type'],
21480 'board_id' => $result['board_id'],
21481 'stage_id' => $stage_data->id,
21482 'position' => $result['position'],
21483 'priority' => $result['priority'],
21484 'created_at' => $result['created_at'],
21485 'created_by' => $result['created_by'],
21486 'updated_at' => $result['updated_at'],
21487 'settings' => unserialize( $result['settings'] ),
21488 'stage' => [
21489 'id' => $stage_data->id,
21490 'slug' => $stage_data->slug,
21491 'title' => $stage_data->title,
21492 'type' => $stage_data->type,
21493 'board_id' => $stage_data->board_id,
21494 'position' => $stage_data->position,
21495 'settings' => $stage_data->settings,
21496 'created_at' => '',
21497 'updated_at' => '',
21498 ],
21499 'watchers' => [
21500 [
21501 'ID' => $user_result['ID'],
21502 'photo' => '',
21503 'user_url' => $user_result['user_url'],
21504 'user_email' => $user_result['user_email'],
21505 'user_login' => $user_result['user_login'],
21506 'user_status' => $user_result['user_status'],
21507 'display_name' => $user_result['display_name'],
21508 'user_nicename' => $user_result['user_nicename'],
21509 'user_registered' => $user_result['user_registered'],
21510 'pivot' => [
21511 'object_id' => $user_result['id'],
21512 'created_at' => $user_result['created_at'],
21513 'foreign_id' => $user_result['ID'],
21514 'user_id' => $user_result['ID'],
21515 ],
21516 ],
21517 ],
21518 ],
21519 'old_stage_id' => $stage_data->id,
21520 ];
21521 $context['response_type'] = 'live';
21522 break;
21523 }
21524 } else {
21525 switch ( $term ) {
21526 case 'board_created':
21527 $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 );
21528 break;
21529 case 'board_member_added':
21530 $context = json_decode( '{"pluggable_data":{"board_id":"2","board_member":{"ID":1,"user_login":"johnd","display_name":"johnd"}},"response_type":"sample"}', true );
21531 break;
21532 case 'task_created':
21533 $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 );
21534 break;
21535 case 'stage_changed':
21536 $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 );
21537 break;
21538
21539 }
21540 }
21541
21542 return (array) $context;
21543 }
21544
21545
21546 /**
21547 * Prepare FluentBoards Boards List.
21548 *
21549 * @param array $data Search Params.
21550 * @return array
21551 */
21552 public function search_fbs_boards_list( $data ) {
21553 global $wpdb;
21554
21555 $boards = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fbs_boards", ARRAY_A );
21556
21557 $options = [];
21558
21559 if ( ! empty( $boards ) ) {
21560 foreach ( $boards as $board ) {
21561 $options[] = [
21562 'label' => $board['title'],
21563 'value' => $board['id'],
21564 ];
21565 }
21566 }
21567
21568 return [
21569 'options' => $options,
21570 'hasMore' => false,
21571 ];
21572 }
21573
21574 /**
21575 * Prepare FluentBoards Subtask Groups List.
21576 *
21577 * @param array $data Search Params.
21578 * @return array
21579 */
21580 public function search_fbs_subtask_groups_list( $data ) {
21581 global $wpdb;
21582
21583 $task_id = isset( $data['dynamic'] ) ? sanitize_text_field( $data['dynamic'] ) : '';
21584
21585 $groups = $wpdb->get_results(
21586 $wpdb->prepare(
21587 "SELECT id, value FROM {$wpdb->prefix}fbs_task_metas WHERE task_id = %d AND `key` = %s",
21588 $task_id,
21589 'group_name'
21590 )
21591 );
21592
21593 $options = [];
21594
21595 foreach ( $groups as $group ) {
21596 $options[] = [
21597 'label' => $group->value,
21598 'value' => $group->id,
21599 ];
21600 }
21601
21602 return [
21603 'options' => $options,
21604 'hasMore' => false,
21605 ];
21606 }
21607
21608 /**
21609 * Prepare FluentBoards Stages List.
21610 *
21611 * @param array $data Search Params.
21612 * @return array
21613 */
21614 public function search_fbs_stages_list( $data ) {
21615 if ( ! class_exists( '\FluentBoards\App\Models\Stage' ) ) {
21616 return [];
21617 }
21618 $query = Stage::where( 'board_id', $data['dynamic'] )->whereNull( 'archived_at' );
21619 $stages = $query->get();
21620 $options = [];
21621
21622 if ( ! empty( $stages ) ) {
21623 foreach ( $stages as $stage ) {
21624 $options[] = [
21625 'label' => $stage['title'],
21626 'value' => $stage['id'],
21627 ];
21628 }
21629 }
21630
21631 return [
21632 'options' => $options,
21633 'hasMore' => false,
21634 ];
21635 }
21636
21637 /**
21638 * Get Profile Grid Last Data
21639 *
21640 * @param array $data data.
21641 *
21642 * @return array
21643 */
21644 public function search_profile_grid_triggers_last_data( $data ) {
21645 $context = [];
21646 global $wpdb;
21647 $term = $data['search_term'] ? $data['search_term'] : '';
21648
21649 if ( 'user_added_group' === $term || 'user_removed_from_group' === $term || 'user_assigned_group_manager' === $term || 'user_unassigned_group_manager' === $term || 'membership_request_approved' === $term ) {
21650 $users = get_users(
21651 [
21652 'orderby' => 'meta_value',
21653 'meta_key' => 'pm_group',
21654 'order' => 'DESC',
21655 'number' => 1,
21656 'meta_query' => [
21657 [
21658 'key' => 'pm_group',
21659 'compare' => 'EXISTS',
21660 ],
21661 ],
21662 'fields' => 'ids',
21663 ]
21664 );
21665 } elseif ( 'payment_complete' === $term || 'payment_failed' === $term ) {
21666 $users = get_users(
21667 [
21668 'orderby' => 'meta_value',
21669 'meta_key' => 'pm_group_payment_status',
21670 'order' => 'DESC',
21671 'number' => 1,
21672 'meta_query' => [
21673 [
21674 'key' => 'pm_group_payment_status',
21675 'compare' => 'EXISTS',
21676 ],
21677 ],
21678 'fields' => 'ids',
21679 ]
21680 );
21681 } elseif ( 'membership_request' === $term ) {
21682 $results = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}promag_group_requests WHERE status = 1 ORDER BY id DESC Limit 1", ARRAY_A );
21683 }
21684 if ( ! empty( $users ) ) {
21685 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 ) {
21686 $context = WordPress::get_user_context( $users[0] );
21687 $user_groups = get_user_meta( $users[0], 'pm_group', true );
21688 if ( is_array( $user_groups ) ) {
21689 $last_element = end( $user_groups );
21690 $context['group_id'] = $last_element;
21691 $context = array_merge( $context, ProfileGrid::pg_group_details( $last_element ) );
21692 }
21693 $context['pluggable_data'] = $context;
21694 $context['response_type'] = 'live';
21695 }
21696 } elseif ( ! empty( $results ) && 'membership_request' === $term ) {
21697 $context = WordPress::get_user_context( $results[0]['uid'] );
21698 $context['group_id'] = $results[0]['gid'];
21699 $context = array_merge( $context, ProfileGrid::pg_group_details( $results[0]['gid'] ) );
21700 $context['pluggable_data'] = $context;
21701 $context['response_type'] = 'live';
21702 } elseif ( 'group_manager_resets_password' === $term ) {
21703 $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 );
21704 } elseif ( 'group_deleted' === $term ) {
21705 $context = json_decode( '{"pluggable_data":{"group_name":"Test Group","group_description":"Testing Group","group_id":"2"},"response_type":"sample"}', true );
21706 } else {
21707 $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 );
21708 }
21709 return (array) $context;
21710 }
21711
21712 /**
21713 * Get Fluent SMTP Last Data
21714 *
21715 * @param array $data data.
21716 *
21717 * @return array
21718 */
21719 public function search_fluent_smtp_last_data( $data ) {
21720 $context = [];
21721 global $wpdb;
21722 $results = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fsmpt_email_logs WHERE status = 'failed' ORDER BY id DESC Limit 1", ARRAY_A );
21723 if ( ! empty( $results ) ) {
21724 $context['to'] = unserialize( $results['to'] );
21725 $context['from'] = $results['from'];
21726 $context['subject'] = $results['subject'];
21727 $context['body'] = $results['body'];
21728 $context['attachments'] = unserialize( $results['attachments'] );
21729 $context['status'] = $results['status'];
21730 $context['response'] = unserialize( $results['response'] );
21731 $context['headers'] = unserialize( $results['headers'] );
21732 $context['extra'] = unserialize( $results['extra'] );
21733 $context['pluggable_data'] = $context;
21734 $context['response_type'] = 'live';
21735 } else {
21736 $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 );
21737 }
21738 return (array) $context;
21739 }
21740
21741 /**
21742 * Get Fluent Community Last Data
21743 *
21744 * @param array $data data.
21745 *
21746 * @return array
21747 */
21748 public function search_fc_triggers_last_data( $data ) {
21749 $context = [];
21750 $space_result = [];
21751 $profile_result = [];
21752 $user_result = [];
21753 $feed_data = [];
21754 $course_result = [];
21755 global $wpdb;
21756 $term = $data['search_term'] ? $data['search_term'] : '';
21757
21758 $data = [];
21759
21760 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 ) {
21761 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_spaces ORDER BY id DESC LIMIT 1", ARRAY_A );
21762 $space_result = [
21763 'id' => $result['id'],
21764 'slug' => $result['slug'],
21765 'title' => $result['title'],
21766 'description' => $result['description'],
21767 'type' => $result['type'],
21768 'privacy' => $result['privacy'],
21769 'status' => $result['status'],
21770 'serial' => $result['serial'],
21771 'created_at' => $result['created_at'],
21772 'created_by' => $result['created_by'],
21773 'updated_at' => $result['updated_at'],
21774 'settings' => unserialize( $result['settings'] ),
21775 ];
21776 }
21777
21778 if ( 'feed_created' === $term || 'feed_updated' === $term || 'space_feed_created' === $term ) {
21779 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_posts ORDER BY id DESC LIMIT 1", ARRAY_A );
21780 $profile_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_xprofile ORDER BY id DESC LIMIT 1", ARRAY_A );
21781 $user_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}users ORDER BY id DESC LIMIT 1", ARRAY_A );
21782 $feed_data = [
21783 'feed' => [
21784 'id' => $result['id'],
21785 'user_id' => $result['user_id'],
21786 'parentid' => $result['parent_id'],
21787 'title' => $result['title'],
21788 'slug' => $result['slug'],
21789 'message' => $result['message'],
21790 'message_rendered' => $result['message_rendered'],
21791 'type' => $result['type'],
21792 'content_type' => $result['content_type'],
21793 'space_id' => $result['space_id'],
21794 'privacy' => $result['privacy'],
21795 'status' => $result['status'],
21796 'featured_image' => $result['featured_image'],
21797 'meta' => unserialize( $result['meta'] ),
21798 'issticky' => $result['issticky'],
21799 'comments_count' => $result['comments_count'],
21800 'reactions_count' => $result['reactions_count'],
21801 'priority' => $result['priority'],
21802 'expiredat' => $result['expired_at'],
21803 'scheduledat' => $result['scheduled_at'],
21804 'created_at' => $result['created_at'],
21805 'updated_at' => $result['updated_at'],
21806 'space' => $space_result,
21807 'xprofile' => [
21808 'id' => $profile_result['id'],
21809 'user_id' => $profile_result['user_id'],
21810 'total_points' => $profile_result['total_points'],
21811 'username' => $profile_result['username'],
21812 'status' => $profile_result['status'],
21813 'is_verified' => $profile_result['is_verified'],
21814 'display_name' => $profile_result['display_name'],
21815 'avatar' => $profile_result['avatar'],
21816 'short_description' => $profile_result['short_description'],
21817 'last_activity' => $profile_result['last_activity'],
21818 'meta' => unserialize( $profile_result['meta'] ),
21819 'created_at' => $profile_result['created_at'],
21820 'updated_at' => $profile_result['updated_at'],
21821 ],
21822 ],
21823 ];
21824 }
21825
21826 if ( 'user_joins_space' === $term ) {
21827 $user_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}users ORDER BY id DESC LIMIT 1", ARRAY_A );
21828 }
21829
21830 if ( 'course_created' === $term || 'course_published' === $term || 'course_updated' === $term || 'user_enrolls_course' === $term || 'user_unenrolls_course' === $term || 'user_completes_course' === $term ) {
21831 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_spaces WHERE type = 'course' ORDER BY id DESC LIMIT 1", ARRAY_A );
21832 $course_result = [
21833 'id' => $result['id'],
21834 'slug' => $result['slug'],
21835 'title' => $result['title'],
21836 'serial' => $result['serial'],
21837 'status' => $result['status'],
21838 'privacy' => $result['privacy'],
21839 'created_at' => $result['created_at'],
21840 'created_by' => $result['created_by'],
21841 'updated_at' => $result['updated_at'],
21842 'description' => $result['description'],
21843 'settings' => unserialize( $result['settings'] ),
21844 ];
21845 }
21846
21847 if ( 'course_deleted' === $term ) {
21848 $result = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}fcom_spaces WHERE type = 'course' ORDER BY id DESC LIMIT 1" );
21849 }
21850
21851 if ( 'new_comment_added' === $term || 'comment_updated' === $term ) {
21852 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_post_comments ORDER BY id DESC LIMIT 1", ARRAY_A );
21853 $profile_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_xprofile ORDER BY id DESC Limit 1", ARRAY_A );
21854 }
21855
21856 if ( 'comment_deleted' === $term ) {
21857 $result = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}fcom_post_comments ORDER BY id DESC LIMIT 1" );
21858 }
21859
21860 if ( ! empty( $result ) ) {
21861 if ( 'space_created' === $term ) {
21862 $context['pluggable_data'] = [ 'space' => $space_result ];
21863 $context['response_type'] = 'live';
21864 }
21865
21866 if ( 'feed_created' === $term || 'feed_updated' === $term ) {
21867 $context['pluggable_data'] = $feed_data;
21868 $context['response_type'] = 'live';
21869 }
21870
21871 if ( 'space_feed_created' === $term ) {
21872 $feed_data['feed']['user'] = [
21873 'user_url' => $user_result['user_url'],
21874 'user_email' => $user_result['user_email'],
21875 'user_login' => $user_result['user_login'],
21876 'user_status' => $user_result['user_status'],
21877 'display_name' => $user_result['display_name'],
21878 'user_nicename' => $user_result['user_nicename'],
21879 'user_registered' => $user_result['user_registered'],
21880 ];
21881 $context['pluggable_data'] = $feed_data;
21882 $context['response_type'] = 'live';
21883 }
21884
21885 if ( 'user_joins_space' === $term ) {
21886 $context['pluggable_data'] = [
21887 'space' => [
21888 'id' => $result['id'],
21889 'slug' => $result['slug'],
21890 'title' => $result['title'],
21891 'description' => $result['description'],
21892 'type' => $result['type'],
21893 'privacy' => $result['privacy'],
21894 'status' => $result['status'],
21895 'serial' => $result['serial'],
21896 'created_at' => $result['created_at'],
21897 'created_by' => $result['created_by'],
21898 'updated_at' => $result['updated_at'],
21899 'settings' => unserialize( $result['settings'] ),
21900 'membership' => [
21901 'ID' => $user_result['ID'],
21902 'photo' => '',
21903 'pivot_role' => 'admin',
21904 'pivot_status' => 'active',
21905 'pivot_created_at' => $result['created_at'],
21906 'pivot_user_id' => $user_result['ID'],
21907 'pivot_space_id' => $result['id'],
21908 'user_url' => $user_result['user_url'],
21909 'user_email' => $user_result['user_email'],
21910 'user_login' => $user_result['user_login'],
21911 'user_status' => $user_result['user_status'],
21912 'display_name' => $user_result['display_name'],
21913 'user_nicename' => $user_result['user_nicename'],
21914 'user_registered' => $user_result['user_registered'],
21915 ],
21916 ],
21917 'userId' => 1,
21918 'by' => 'self',
21919 ];
21920 $context['response_type'] = 'live';
21921 }
21922 if ( 'user_leaves_space' === $term ) {
21923 $context['pluggable_data'] = [
21924 'space' => $space_result,
21925 'user' => WordPress::get_sample_user_context(),
21926 'userId' => 1,
21927 'by' => 'self',
21928 ];
21929 $context['response_type'] = 'live';
21930 }
21931 if ( 'course_created' === $term || 'course_updated' === $term || 'course_published' === $term ) {
21932 $context['pluggable_data'] = [
21933 'course' => $course_result,
21934 ];
21935 $context['response_type'] = 'live';
21936 }
21937
21938 if ( 'course_deleted' === $term ) {
21939 $context['pluggable_data'] = [ 'course' => $result ];
21940 $context['response_type'] = 'live';
21941 }
21942
21943 if ( 'comment_deleted' === $term ) {
21944 $context['pluggable_data'] = [ 'comment_id' => $result ];
21945 $context['response_type'] = 'live';
21946 }
21947 if ( 'new_comment_added' === $term || 'comment_updated' === $term ) {
21948 $context['pluggable_data'] = [
21949 'comment' => [
21950 'id' => $result['id'],
21951 'user_id' => $result['user_id'],
21952 'post_id' => $result['post_id'],
21953 'parent_id' => $result['parent_id'],
21954 'reactions_count' => $result['reactions_count'],
21955 'message' => $result['message'],
21956 'message_rendered' => $result['message_rendered'],
21957 'meta' => unserialize( $result['meta'] ),
21958 'type' => $result['type'],
21959 'content_type' => $result['content_type'],
21960 'status' => $result['status'],
21961 'is_sticky' => $result['is_sticky'],
21962 'created_at' => $result['created_at'],
21963 'updated_at' => $result['updated_at'],
21964 'xprofile' => [
21965 'id' => $profile_result['id'],
21966 'user_id' => $profile_result['user_id'],
21967 'total_points' => $profile_result['total_points'],
21968 'username' => $profile_result['username'],
21969 'status' => $profile_result['status'],
21970 'is_verified' => $profile_result['is_verified'],
21971 'display_name' => $profile_result['display_name'],
21972 'avatar' => $profile_result['avatar'],
21973 'short_description' => $profile_result['short_description'],
21974 'last_activity' => $profile_result['last_activity'],
21975 'meta' => unserialize( $profile_result['meta'] ),
21976 'created_at' => $profile_result['created_at'],
21977 'updated_at' => $profile_result['updated_at'],
21978 ],
21979 ],
21980 ];
21981 $context['response_type'] = 'live';
21982 }
21983 if ( 'user_enrolls_course' === $term || 'user_unenrolls_course' === $term || 'user_completes_course' === $term ) {
21984 $context['pluggable_data'] = [
21985 'course' => $course_result,
21986 'user' => WordPress::get_sample_user_context(),
21987 'userId' => 1,
21988 ];
21989 $context['response_type'] = 'live';
21990 }
21991 if ( 'user_requests_join_space' === $term ) {
21992 $context['pluggable_data'] = [
21993 'space' => $space_result,
21994 'user' => WordPress::get_sample_user_context(),
21995 'userId' => 1,
21996 ];
21997 $context['response_type'] = 'live';
21998 }
21999 } else {
22000 $sample_space_data = [
22001 'id' => 101,
22002 'slug' => 'sample-space',
22003 'title' => 'Sample Space',
22004 'description' => 'This is a sample space description.',
22005 'type' => 'community',
22006 'privacy' => 'public',
22007 'status' => 'active',
22008 'serial' => 'SP101',
22009 'created_at' => '2025-01-01 10:00:00',
22010 'created_by' => 1,
22011 'updated_at' => '2025-01-05 12:00:00',
22012 'settings' => [
22013 'coursetype' => 'self_paced',
22014 'emoji' => '😍',
22015 'shapesvg' => null,
22016 'disablecomments' => 'no',
22017 'hidememberscount' => 'no',
22018 ],
22019 ];
22020 $sample_profile_data = [
22021 'id' => 1,
22022 'user_id' => 1,
22023 'total_points' => 100,
22024 'username' => 'sampleuser',
22025 'status' => 'active',
22026 'is_verified' => true,
22027 'display_name' => 'Sample User',
22028 'avatar' => 'https://example.com/avatar.jpg',
22029 'short_description' => 'This is a sample user.',
22030 'last_activity' => '2025-01-08 15:00:00',
22031 'meta' => [
22032 'website' => 'https://example.com',
22033 'coverphoto' => 'https://example.com/coverphoto.jpg',
22034 ],
22035 'created_at' => '2025-01-01 10:00:00',
22036 'updated_at' => '2025-01-05 12:00:00',
22037 ];
22038
22039 if ( 'feed_created' === $term || 'feed_updated' === $term || 'space_feed_created' === $term ) {
22040 $context['pluggable_data'] = [
22041 'feed' => [
22042 'id' => 1,
22043 'userid' => 1,
22044 'parentid' => null,
22045 'title' => 'Sample Feed',
22046 'slug' => 'sample-feed',
22047 'message' => 'This is a sample message for the feed.',
22048 'messagerendered' => '<p>This is a sample message for the feed.</p>',
22049 'type' => 'post',
22050 'contenttype' => 'text',
22051 'space_id' => 101,
22052 'privacy' => 'public',
22053 'status' => 'published',
22054 'featuredimage' => 'https://example.com/image.jpg',
22055 'meta' => [
22056 'previewdata' => 0,
22057 ],
22058 'issticky' => false,
22059 'commentscount' => 10,
22060 'reactionscount' => 25,
22061 'priority' => 'normal',
22062 'expiredat' => null,
22063 'scheduledat' => '2025-01-10 08:00:00',
22064 'createdat' => '2025-01-09 09:00:00',
22065 'updatedat' => '2025-01-09 10:00:00',
22066 'space' => $sample_space_data,
22067 'xprofile' => $sample_profile_data,
22068 ],
22069 ];
22070 $context['response_type'] = 'sample';
22071 }
22072 if ( 'space_feed_created' === $term ) {
22073 $conext['pluggable_data']['feed']['user'] = [
22074 'user_url' => 'https://example.com/user/profile',
22075 'user_email' => 'user@example.com',
22076 'user_login' => 'sampleuser',
22077 'user_status' => 'active',
22078 'display_name' => 'Sample User',
22079 'user_nicename' => 'sample-user',
22080 'user_registered' => '2024-01-01 00:00:00',
22081 ];
22082 $context['pluggable_data']['feed']['space'] = $sample_space_data;
22083 $context['pluggable_data']['feed']['xprofile'] = $sample_profile_data;
22084 $context['response_type'] = 'sample';
22085 }
22086 if ( 'user_joins_space' === $term ) {
22087 $context['pluggable_data'] = [
22088 'space' => [
22089 $sample_space_data,
22090 'membership' => [
22091 'ID' => 25,
22092 'photo' => 'https://example.com/user-photo.jpg',
22093 'pivot_role' => 'admin',
22094 'pivot_status' => 'active',
22095 'pivot_created_at' => '2025-01-24 09:00:00',
22096 'pivot_user_id' => 25,
22097 'pivot_space_id' => 100,
22098 'user_url' => 'https://example.com/user-profile',
22099 'user_email' => 'user@example.com',
22100 'user_login' => 'sampleuser',
22101 'user_status' => 'active',
22102 'display_name' => 'Sample User',
22103 'user_nicename' => 'sample-user',
22104 'user_registered' => '2024-01-01 00:00:00',
22105 ],
22106 ],
22107 'userId' => 1,
22108 'by' => 'self',
22109 ];
22110 $context['response_type'] = 'sample';
22111 }
22112
22113 if ( 'user_leaves_space' === $term ) {
22114 $context['pluggable_data'] = [
22115 'space' => $sample_space_data,
22116 'user' => WordPress::get_sample_user_context(),
22117 'userId' => 35,
22118 'by' => 'self',
22119 ];
22120 $context['response_type'] = 'sample';
22121 }
22122
22123 if ( 'new_comment_added' === $term || 'comment_updated' === $term ) {
22124 $context['pluggable_data'] = [
22125 'comment' => [
22126 'id' => 301,
22127 'user_id' => 45,
22128 'post_id' => 101,
22129 'parent_id' => 0,
22130 'reactions_count' => 5,
22131 'message' => 'This is a sample comment message.',
22132 'message_rendered' => '<p>This is a sample comment message.</p>',
22133 'meta' => [
22134 'likes' => 3,
22135 'shares' => 2,
22136 ],
22137 'type' => 'text',
22138 'content_type' => 'post',
22139 'status' => 'approved',
22140 'is_sticky' => false,
22141 'created_at' => '2025-01-23 10:30:00',
22142 'updated_at' => '2025-01-23 12:00:00',
22143 'xprofile' => $sample_profile_data,
22144 ],
22145 ];
22146 $context['response_type'] = 'sample';
22147 }
22148
22149 if ( 'course_deleted' === $term ) {
22150 $context['pluggable_data'] = [ 'course' => 2 ];
22151 $context['response_type'] = 'sample';
22152 }
22153 if ( 'comment_deleted' === $term ) {
22154 $context['pluggable_data'] = [ 'comment_id' => 1 ];
22155 $context['response_type'] = 'sample';
22156 }
22157 if ( 'user_enrolls_course' === $term || 'user_unenrolls_course' === $term || 'user_completes_course' === $term ) {
22158 $context['pluggable_data'] = [
22159 'course' => $sample_space_data,
22160 'user' => WordPress::get_sample_user_context(),
22161 'userId' => 1,
22162 ];
22163 $context['response_type'] = 'sample';
22164 }
22165 if ( 'course_created' === $term || 'course_updated' === $term || 'course_published' === $term ) {
22166 $context['pluggable_data'] = [
22167 'course' => $sample_space_data,
22168 ];
22169 $context['response_type'] = 'sample';
22170 }
22171 if ( 'user_requests_join_space' === $term ) {
22172 $context['pluggable_data'] = [
22173 'space' => $sample_space_data,
22174 'user' => WordPress::get_sample_user_context(),
22175 'userId' => 1,
22176 ];
22177 $context['response_type'] = 'sample';
22178 }
22179 }
22180 return (array) $context;
22181 }
22182
22183 /**
22184 * Prepare FluentCommunity Courses List.
22185 *
22186 * @param array $data Search Params.
22187 * @return array
22188 */
22189 public function search_fc_courses_list( $data ) {
22190 global $wpdb;
22191
22192 $courses = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fcom_spaces WHERE type = 'course' ", ARRAY_A );
22193
22194 $options = [];
22195
22196 if ( ! empty( $courses ) ) {
22197 foreach ( $courses as $course ) {
22198 $options[] = [
22199 'label' => $course['title'],
22200 'value' => $course['id'],
22201 ];
22202 }
22203 }
22204
22205 return [
22206 'options' => $options,
22207 'hasMore' => false,
22208 ];
22209 }
22210
22211 /**
22212 * Prepare FluentCommunity Spaces List.
22213 *
22214 * @param array $data Search Params.
22215 * @return array
22216 */
22217 public function search_fc_spaces_list( $data ) {
22218 global $wpdb;
22219
22220 $spaces = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fcom_spaces WHERE type NOT IN ('course', 'space_group')", ARRAY_A );
22221
22222 $options = [];
22223
22224 if ( ! empty( $spaces ) ) {
22225 foreach ( $spaces as $space ) {
22226 $options[] = [
22227 'label' => $space['title'],
22228 'value' => $space['id'],
22229 ];
22230 }
22231 }
22232
22233 return [
22234 'options' => $options,
22235 'hasMore' => false,
22236 ];
22237 }
22238
22239 /**
22240 * Prepare FluentCommunity Topics List.
22241 *
22242 * @param array $data Search Params.
22243 * @return array
22244 */
22245 public function search_fc_topics_list( $data ) {
22246 if ( ! class_exists( '\FluentCommunity\App\Functions\Utility' ) ) {
22247 return [];
22248 }
22249
22250 $space_topics = Utility::getTopicsBySpaceId( $data['dynamic'] );
22251
22252 $options = [];
22253
22254 if ( ! empty( $space_topics ) ) {
22255 foreach ( $space_topics as $topic ) {
22256 $options[] = [
22257 'label' => $topic['title'],
22258 'value' => $topic['id'],
22259 ];
22260 }
22261 }
22262
22263 return [
22264 'options' => $options,
22265 'hasMore' => false,
22266 ];
22267 }
22268
22269 /**
22270 * Get WP Travel Engine Last Data
22271 *
22272 * @param array $data data.
22273 *
22274 * @return array
22275 */
22276 public function search_wpte_triggers_last_data( $data ) {
22277 global $wpdb;
22278
22279 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
22280
22281 $context = [];
22282 $result = null;
22283
22284 switch ( $term ) {
22285 case 'enquiry_submitted':
22286 $result = $wpdb->get_row(
22287 "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'enquiry' ORDER BY ID DESC LIMIT 1",
22288 ARRAY_A
22289 );
22290
22291 if ( $result && isset( $result['ID'] ) ) {
22292 $data = get_post( $result['ID'] );
22293 }
22294 break;
22295
22296 case 'booking_created':
22297 case 'booking_confirmed':
22298 case 'booking_cancelled':
22299 $result = $wpdb->get_row(
22300 "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'booking' ORDER BY ID DESC LIMIT 1",
22301 ARRAY_A
22302 );
22303
22304 if ( $result && isset( $result['ID'] ) ) {
22305 $booking_id = $result['ID'];
22306 $booking_data = get_post( $booking_id );
22307 $booking_meta = get_post_meta( $booking_id, 'wp_travel_engine_booking_setting', true );
22308 $booking_status = get_post_meta( $booking_id, 'wp_travel_engine_booking_status', true );
22309
22310 // Set correct booking status based on trigger type.
22311 switch ( $term ) {
22312 case 'booking_confirmed':
22313 $booking_status = 'booked';
22314 break;
22315 case 'booking_cancelled':
22316 $booking_status = 'cancelled';
22317 break;
22318 }
22319
22320 // For booking_created, include payment data as well.
22321 if ( 'booking_created' === $term ) {
22322 $payment_status = get_post_meta( $booking_id, 'wp_travel_engine_booking_payment_status', true );
22323 $payment_gateway = get_post_meta( $booking_id, 'wp_travel_engine_booking_payment_gateway', true );
22324 $payment_details = get_post_meta( $booking_id, 'wp_travel_engine_booking_payment_details', true );
22325
22326 $coupon_details = [];
22327 $cart_info = get_post_meta( $booking_id, 'cart_info', true );
22328
22329 if ( ! empty( $cart_info ) && is_array( $cart_info ) && isset( $cart_info['discounts'] ) ) {
22330 foreach ( $cart_info['discounts'] as $discount ) {
22331 if ( isset( $discount['name'] ) ) {
22332 $coupon_details[] = [
22333 'coupon_code' => $discount['name'],
22334 'discount_type' => isset( $discount['type'] ) ? $discount['type'] : '',
22335 'discount_value' => isset( $discount['value'] ) ? $discount['value'] : 0,
22336 ];
22337 }
22338 }
22339 }
22340
22341 if ( empty( $coupon_details ) ) {
22342 $coupon_details = [
22343 [
22344 'coupon_code' => 'SAVE20',
22345 'discount_type' => 'percentage',
22346 'discount_value' => 20,
22347 ],
22348 ];
22349 }
22350
22351 $data = [
22352 'booking_data' => $booking_data,
22353 'booking_meta' => $booking_meta,
22354 'payment_status' => $payment_status,
22355 'payment_gateway' => $payment_gateway,
22356 'payment_details' => $payment_details,
22357 'coupon_details' => $coupon_details,
22358 ];
22359 } else {
22360 $data = [
22361 'booking_data' => $booking_data,
22362 'booking_meta' => $booking_meta,
22363 'booking_status' => $booking_status,
22364 ];
22365 }
22366 }
22367 break;
22368 }
22369
22370 if ( ! empty( $result ) && isset( $result['ID'] ) ) {
22371 if ( in_array( $term, [ 'booking_created', 'booking_confirmed', 'booking_cancelled' ] ) ) {
22372 $context['pluggable_data'] = $data;
22373 } else {
22374 $context['pluggable_data'] = [
22375 'enquiry_post_id' => $result['ID'],
22376 'post_data' => $data,
22377 ];
22378 }
22379 $context['response_type'] = 'live';
22380 } else {
22381 // Helper function to generate booking sample data.
22382 $get_booking_sample_data = function( $customer, $trip, $booking_status = '', $payment_data = [] ) {
22383 $booking_data = [
22384 'ID' => 123,
22385 'post_title' => $customer['fname'] . ' ' . $customer['lname'] . ' #123',
22386 'post_name' => strtolower( $customer['fname'] . '-' . $customer['lname'] ) . '-123',
22387 'post_date' => '2025-01-15 10:00:00',
22388 'post_status' => 'publish',
22389 'post_type' => 'booking',
22390 'post_author' => 1,
22391 'post_content' => '',
22392 'post_excerpt' => '',
22393 'comment_status' => 'closed',
22394 'ping_status' => 'closed',
22395 'post_modified' => '2025-01-15 10:00:00',
22396 'guid' => 'http://example.com/booking/' . strtolower( $customer['fname'] . '-' . $customer['lname'] ) . '-123',
22397 ];
22398
22399 $booking_meta = [
22400 'place_order' => [
22401 'traveler' => 1,
22402 'cost' => 10,
22403 'due' => 10,
22404 'tid' => 1308,
22405 'tname' => 'New York',
22406 'datetime' => '2025-08-14',
22407 'datewithtime' => '',
22408 'booking' => [
22409 'fname' => 'John',
22410 'lname' => 'Doe',
22411 'email' => 'john.doe@example.com',
22412 'address' => '123 Main St',
22413 'city' => 'New York',
22414 'country' => 'US',
22415 ],
22416 'tax' => '',
22417 'tduration' => '1 days',
22418 'tenddate' => '2025-08-14T00:00',
22419 'trip_package' => 'Camp Trek',
22420 ],
22421 ];
22422
22423 $sample_data = [
22424 'booking_data' => $booking_data,
22425 'booking_meta' => $booking_meta,
22426 ];
22427
22428 if ( $booking_status ) {
22429 $sample_data['booking_status'] = $booking_status;
22430 }
22431
22432 if ( ! empty( $payment_data ) ) {
22433 $sample_data = array_merge( $sample_data, $payment_data );
22434 }
22435
22436 return $sample_data;
22437 };
22438
22439 // Sample data based on term.
22440 if ( in_array( $term, [ 'booking_created', 'booking_confirmed', 'booking_cancelled' ] ) ) {
22441 $sample_customer = [
22442 'fname' => 'John',
22443 'lname' => 'Doe',
22444 'email' => 'john.doe@example.com',
22445 'address' => '123 Main St',
22446 'city' => 'New York',
22447 'country' => 'US',
22448 ];
22449 $sample_trip = [
22450 'travelers' => '2',
22451 'cost' => '800',
22452 'id' => '101',
22453 'name' => 'Sample Trip to Nepal',
22454 'date' => '2025-03-15',
22455 ];
22456
22457 $booking_status = '';
22458
22459 // Set correct booking status based on trigger type.
22460 switch ( $term ) {
22461 case 'booking_confirmed':
22462 $booking_status = 'booked';
22463 break;
22464 case 'booking_cancelled':
22465 $booking_status = 'cancelled';
22466 break;
22467 }
22468
22469 // For booking_created, include payment data as well.
22470 if ( 'booking_created' === $term ) {
22471 $payment_data = [
22472 'payment_status' => 'check-waiting',
22473 'payment_gateway' => 'Check Payment',
22474 'payment_details' => '',
22475 'coupon_details' => [
22476 [
22477 'coupon_code' => 'SAVE20',
22478 'discount_type' => 'percentage',
22479 'discount_value' => 20,
22480 ],
22481 ],
22482 ];
22483 $context['pluggable_data'] = $get_booking_sample_data( $sample_customer, $sample_trip, '', $payment_data );
22484 } else {
22485 $context['pluggable_data'] = $get_booking_sample_data( $sample_customer, $sample_trip, $booking_status );
22486 }
22487 } else {
22488 switch ( $term ) {
22489
22490 default:
22491 $context['pluggable_data'] = [
22492 'enquiry_post_id' => 123,
22493 'post_data' => [
22494 'ID' => 123,
22495 'post_title' => 'Sample Enquiry',
22496 'post_name' => 'sample-enquiry',
22497 'post_date' => '2025-01-01 10:00:00',
22498 'post_status' => 'publish',
22499 'post_type' => 'enquiry',
22500 'post_author' => 1,
22501 'post_content' => '',
22502 'post_excerpt' => '',
22503 'comment_status' => 'closed',
22504 'ping_status' => 'closed',
22505 'post_modified' => '2025-01-01 10:00:00',
22506 'guid' => 'http://example.com/enquiry/sample-enquiry',
22507 ],
22508 ];
22509 break;
22510 }
22511 $context['response_type'] = 'sample';
22512 }
22513 }
22514 return (array) $context;
22515 }
22516
22517 /**
22518 * Prepare WP Travel Engine Trips List.
22519 *
22520 * @param array $data Search Params.
22521 * @return array
22522 */
22523 public function search_wte_trips_list( $data ) {
22524 $page = isset( $data['page'] ) ? max( 1, intval( $data['page'] ) ) : 1;
22525 $limit = Utilities::get_search_page_limit();
22526 $offset = $limit * ( $page - 1 );
22527
22528 $query_args = [
22529 'post_type' => 'trip',
22530 'post_status' => 'publish',
22531 'posts_per_page' => $limit,
22532 'offset' => $offset,
22533 ];
22534
22535 $query = new \WP_Query( $query_args );
22536 $result = [];
22537
22538 foreach ( $query->posts as $post ) {
22539 if ( $post instanceof \WP_Post ) {
22540 $result[] = [
22541 'label' => $post->post_title,
22542 'value' => $post->ID,
22543 ];
22544 }
22545 }
22546
22547 $total_query = new \WP_Query(
22548 [
22549 'post_type' => 'trip',
22550 'post_status' => 'publish',
22551 'posts_per_page' => -1,
22552 'fields' => 'ids',
22553 ]
22554 );
22555 $posts_count = count( $total_query->posts );
22556
22557 return [
22558 'options' => $result,
22559 'hasMore' => $posts_count > ( $offset + $limit ),
22560 ];
22561 }
22562 /**
22563 * Get GeoDirectory trigger last data.
22564 *
22565 * @param array $data data.
22566 * @return array|mixed
22567 */
22568 public function search_geodir_triggers_last_data( $data ) {
22569 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
22570 $context = [];
22571
22572 if ( function_exists( 'geodir_get_post_info' ) ) {
22573 $args = [
22574 'post_type' => 'gd_place',
22575 'posts_per_page' => 1,
22576 'orderby' => 'date',
22577 'order' => 'DESC',
22578 ];
22579
22580 $posts = get_posts( $args );
22581
22582 if ( ! empty( $posts ) ) {
22583 $post = $posts[0];
22584 $post_id = $post->ID;
22585 $user = WordPress::get_user_context( absint( $post->post_author ) );
22586 $gd_post = function_exists( 'geodir_get_post_info' ) ? geodir_get_post_info( $post_id ) : null;
22587
22588 $categories = [];
22589 $cat_taxonomy = $post->post_type . 'category';
22590 if ( taxonomy_exists( $cat_taxonomy ) ) {
22591 $terms = wp_get_post_terms( $post_id, $cat_taxonomy );
22592 if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
22593 foreach ( $terms as $term_obj ) {
22594 $categories[] = $term_obj->name;
22595 }
22596 }
22597 }
22598
22599 $tags = [];
22600 $tag_taxonomy = $post->post_type . '_tags';
22601 if ( taxonomy_exists( $tag_taxonomy ) ) {
22602 $terms = wp_get_post_terms( $post_id, $tag_taxonomy );
22603 if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
22604 foreach ( $terms as $term_obj ) {
22605 $tags[] = $term_obj->name;
22606 }
22607 }
22608 }
22609
22610 $attachments = [];
22611 if ( function_exists( 'geodir_get_images' ) ) {
22612 $images = geodir_get_images( $post_id );
22613 if ( ! empty( $images ) ) {
22614 foreach ( $images as $image ) {
22615 if ( ! empty( $image->file ) ) {
22616 $attachments[] = $image->file;
22617 }
22618 }
22619 }
22620 }
22621
22622 $address = '';
22623 $country = '';
22624 $region = '';
22625 $city = '';
22626 $postal_code = '';
22627 $latitude = '';
22628 $longitude = '';
22629
22630 if ( ! empty( $gd_post ) ) {
22631 if ( isset( $gd_post->street ) ) {
22632 $address = $gd_post->street;
22633 }
22634 if ( isset( $gd_post->country ) ) {
22635 $country = $gd_post->country;
22636 }
22637 if ( isset( $gd_post->region ) ) {
22638 $region = $gd_post->region;
22639 }
22640 if ( isset( $gd_post->city ) ) {
22641 $city = $gd_post->city;
22642 }
22643 if ( isset( $gd_post->zip ) ) {
22644 $postal_code = $gd_post->zip;
22645 }
22646 if ( isset( $gd_post->latitude ) ) {
22647 $latitude = $gd_post->latitude;
22648 }
22649 if ( isset( $gd_post->longitude ) ) {
22650 $longitude = $gd_post->longitude;
22651 }
22652 }
22653
22654 $base_data = [
22655 'listing_id' => $post->ID,
22656 'listing_title' => $post->post_title,
22657 'listing_type' => $post->post_type,
22658 'listing_url' => get_permalink( $post->ID ),
22659 'description' => $post->post_content,
22660 'categories' => $categories,
22661 'tags' => $tags,
22662 'attachments' => $attachments,
22663 'address' => $address,
22664 'country' => $country,
22665 'region' => $region,
22666 'city' => $city,
22667 'postal_code' => $postal_code,
22668 'latitude' => $latitude,
22669 'longitude' => $longitude,
22670 ];
22671
22672 switch ( $term ) {
22673 case 'geodir_claimed_listing_added':
22674 case 'geodir_claim_approved':
22675 case 'geodir_claim_rejected':
22676 $claim = [];
22677 $claim_status_text = 'unknown';
22678
22679 if ( 'geodir_claimed_listing_added' === $term ) {
22680 $claim_status_text = 'pending';
22681 } elseif ( 'geodir_claim_approved' === $term ) {
22682 $claim_status_text = 'approved';
22683 } elseif ( 'geodir_claim_rejected' === $term ) {
22684 $claim_status_text = 'rejected';
22685 }
22686
22687 global $wpdb;
22688 $claim_table = $wpdb->prefix . 'geodir_claim';
22689
22690 $claim_id = $wpdb->get_var(
22691 $wpdb->prepare(
22692 "SELECT id FROM {$wpdb->prefix}geodir_claim WHERE post_id = %d ORDER BY id DESC LIMIT 1",
22693 $post_id
22694 )
22695 );
22696
22697 if ( $claim_id && class_exists( '\GeoDir_Claim_Post' ) && is_callable( [ '\GeoDir_Claim_Post', 'get_item' ] ) ) {
22698 $claim = \GeoDir_Claim_Post::get_item( $claim_id );
22699 }
22700
22701 $context['pluggable_data'] = array_merge(
22702 $base_data,
22703 [
22704 'claim' => array_merge(
22705 (array) $claim,
22706 [
22707 'status' => $claim_status_text,
22708 ]
22709 ),
22710 ],
22711 $user
22712 );
22713 break;
22714 case 'geodir_listing_upgraded':
22715 $context['pluggable_data'] = array_merge(
22716 $base_data,
22717 [
22718 'package_id' => 789,
22719 'package_name' => 'Premium Package',
22720 'package_amount' => '99.99',
22721 'package_days' => '365',
22722 ],
22723 $user
22724 );
22725 break;
22726 default:
22727 $context['pluggable_data'] = array_merge( $base_data, $user );
22728 break;
22729 }
22730
22731 $context['response_type'] = 'live';
22732 return $context;
22733 }
22734 }
22735
22736 $sample_data = [
22737 'listing_id' => 123,
22738 'listing_title' => 'Sample Business Listing',
22739 'listing_type' => 'gd_place',
22740 'listing_url' => site_url( '/places/sample-business-listing/' ),
22741 'description' => 'This is a sample business listing description.',
22742 'categories' => [ 'Restaurant', 'Cafe' ],
22743 'tags' => [ 'Food', 'Coffee', 'Breakfast' ],
22744 'attachments' => [ site_url( '/wp-content/uploads/sample-image.jpg' ) ],
22745 'address' => '123 Main St',
22746 'country' => 'United States',
22747 'region' => 'California',
22748 'city' => 'San Francisco',
22749 'postal_code' => '94105',
22750 'latitude' => '37.7749',
22751 'longitude' => '-122.4194',
22752 'wp_user_id' => 10,
22753 'user_login' => 'sampleuser',
22754 'display_name' => 'Sample User',
22755 'user_firstname' => 'Sample',
22756 'user_lastname' => 'User',
22757 'user_email' => 'sample@example.com',
22758 'user_role' => [ 'subscriber' ],
22759 ];
22760
22761 switch ( $term ) {
22762 case 'geodir_claim_approved':
22763 case 'geodir_claim_rejected':
22764 case 'geodir_claimed_listing_added':
22765 $sample_data['claim'] = [
22766 'id' => 14,
22767 'post_id' => 123,
22768 'post_type' => 'gd_place',
22769 'author_id' => 10,
22770 'user_id' => 10,
22771 'user_fullname' => 'Sample User',
22772 'user_number' => '+1234567890',
22773 'user_position' => 'Business Owner',
22774 'user_ip' => '127.0.0.1',
22775 'user_comments' => 'Hi, I am the owner of this business and I would like to claim it.',
22776 'admin_comments' => '',
22777 'claim_date' => current_time( 'mysql' ),
22778 'status' => 'pending',
22779 'rand_string' => 'sampleclaimstring123',
22780 'payment_id' => 0,
22781 'old_package_id' => 0,
22782 'package_id' => 0,
22783 'meta' => [
22784 'ninja_form_id' => '2',
22785 'ninja_sub_id' => '',
22786 'ninja_post' => 123,
22787 'extra_fields' => [
22788 'user_id' => [
22789 'label' => 'User ID',
22790 'value' => '10',
22791 ],
22792 ],
22793 ],
22794 ];
22795 break;
22796 case 'geodir_listing_upgraded':
22797 $sample_data['package_id'] = 789;
22798 $sample_data['package_name'] = 'Premium Package';
22799 $sample_data['package_amount'] = '99.99';
22800 $sample_data['package_days'] = '365';
22801 break;
22802 }
22803
22804 $context['pluggable_data'] = $sample_data;
22805 $context['response_type'] = 'sample';
22806 return $context;
22807 }
22808
22809 /**
22810 * Get WP Subscription triggers last data.
22811 *
22812 * @param array $data data.
22813 *
22814 * @return array
22815 */
22816 public function search_wp_subscription_triggers_last_data( $data ) {
22817 global $wpdb;
22818
22819 $context = [];
22820 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
22821
22822 $needs_subscription = in_array(
22823 $term,
22824 [
22825 'subscription_activated',
22826 'subscription_cancelled',
22827 'subscription_expired',
22828 'subscription_status_changed',
22829 'subscription_pending',
22830 'subscription_pending_cancellation',
22831 ],
22832 true
22833 );
22834
22835 if ( ! $needs_subscription ) {
22836 return $context;
22837 }
22838
22839 $subscription = $wpdb->get_row(
22840 "SELECT * FROM {$wpdb->prefix}posts WHERE post_type = 'subscrpt_order' ORDER BY post_modified DESC LIMIT 1"
22841 );
22842
22843 $sample_subscription = new \stdClass();
22844 $sample_subscription->ID = 100;
22845 $sample_subscription->post_author = 2;
22846 $sample_subscription->post_date = '2025-07-07 09:51:52';
22847 $sample_subscription->post_date_gmt = '2025-07-07 09:51:52';
22848 $sample_subscription->post_content = '';
22849 $sample_subscription->post_title = 'Subscription #100';
22850 $sample_subscription->post_excerpt = '';
22851 $sample_subscription->post_status = 'active';
22852 $sample_subscription->comment_status = 'closed';
22853 $sample_subscription->ping_status = 'closed';
22854 $sample_subscription->post_password = '';
22855 $sample_subscription->post_name = 'subscription-100';
22856 $sample_subscription->to_ping = '';
22857 $sample_subscription->pinged = '';
22858 $sample_subscription->post_modified = '2025-07-07 10:15:16';
22859 $sample_subscription->post_modified_gmt = '2025-07-07 10:15:16';
22860 $sample_subscription->post_content_filtered = '';
22861 $sample_subscription->post_parent = 0;
22862 $sample_subscription->guid = 'http://example.com/subscrpt_order/';
22863 $sample_subscription->menu_order = 0;
22864 $sample_subscription->post_type = 'subscrpt_order';
22865 $sample_subscription->post_mime_type = '';
22866 $sample_subscription->comment_count = 4;
22867 $sample_subscription->filter = 'raw';
22868
22869 $sub = $subscription ? $subscription : $sample_subscription;
22870 $type = $subscription ? 'live' : 'sample';
22871
22872 $pluggable_data = [
22873 'subscription_id' => $sub->ID,
22874 'subscription' => $sub,
22875 'user_id' => $sub->post_author,
22876 ];
22877
22878 if ( 'subscription_status_changed' === $term ) {
22879 $pluggable_data['old_status'] = 'pending';
22880 $pluggable_data['new_status'] = $sub->post_status;
22881 }
22882
22883 $context['pluggable_data'] = $pluggable_data;
22884 $context['response_type'] = $type;
22885
22886 return $context;
22887 }
22888 /**
22889 * Prepare clickwhale categories.
22890 *
22891 * @param array $data Search Params.
22892 *
22893 * @return array
22894 */
22895 public function search_clickwhale_categories( $data ) {
22896
22897 $options = [];
22898 global $wpdb;
22899
22900 $categories = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}clickwhale_categories ORDER BY id DESC", ARRAY_A );
22901
22902 if ( ! empty( $categories ) ) {
22903 foreach ( $categories as $category ) {
22904 $options[] = [
22905 'label' => $category['title'],
22906 'value' => $category['id'],
22907 ];
22908 }
22909 }
22910
22911 return [
22912 'options' => $options,
22913 'hasMore' => false,
22914 ];
22915 }
22916
22917 /**
22918 * Get ClickWhale Last Data
22919 *
22920 * @param array $data data.
22921 *
22922 * @return array
22923 */
22924 public function search_clickwhale_triggers_last_data( $data ) {
22925 global $wpdb;
22926
22927 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
22928 $context = [];
22929
22930 $latest_link = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}clickwhale_links ORDER BY id DESC LIMIT 1", ARRAY_A );
22931
22932 $get_author_data = function( $author_id ) {
22933 if ( class_exists( '\SureTriggers\Integrations\WordPress\WordPress' ) ) {
22934 return WordPress::get_user_context( $author_id );
22935 }
22936 return [];
22937 };
22938
22939 $format_link = function( $link_data ) use ( $get_author_data ) {
22940 $author_id = isset( $link_data['author'] ) ? $link_data['author'] : 0;
22941
22942 return [
22943 'id' => isset( $link_data['id'] ) ? $link_data['id'] : 17,
22944 'title' => isset( $link_data['title'] ) ? $link_data['title'] : 'Sample Title',
22945 'slug' => isset( $link_data['slug'] ) ? $link_data['slug'] : 'sample-slug',
22946 'url' => isset( $link_data['url'] ) ? $link_data['url'] : 'https://example.com',
22947 'redirection' => isset( $link_data['redirection'] ) ? $link_data['redirection'] : '301',
22948 'link_target' => isset( $link_data['link_target'] ) ? $link_data['link_target'] : '',
22949 'nofollow' => isset( $link_data['nofollow'] ) ? $link_data['nofollow'] : '1',
22950 'sponsored' => isset( $link_data['sponsored'] ) ? $link_data['sponsored'] : '0',
22951 'description' => isset( $link_data['description'] ) ? $link_data['description'] : '',
22952 'categories' => isset( $link_data['categories'] ) ? $link_data['categories'] : '',
22953 'author_id' => $author_id,
22954 'author' => $get_author_data( $author_id ),
22955 'created_at' => isset( $link_data['created_at'] ) ? $link_data['created_at'] : current_time( 'mysql' ),
22956 'updated_at' => isset( $link_data['updated_at'] ) ? $link_data['updated_at'] : current_time( 'mysql' ),
22957 ];
22958 };
22959
22960 switch ( $term ) {
22961 case 'link_created':
22962 case 'link_updated':
22963 $link_data = ! empty( $latest_link ) ? $format_link( $latest_link ) : $format_link( [] );
22964 $context['pluggable_data'] = [ 'link' => $link_data ];
22965 $context['response_type'] = ! empty( $latest_link ) ? 'live' : 'sample';
22966 break;
22967
22968 case 'link_deleted':
22969 if ( ! empty( $latest_link ) ) {
22970 $context['pluggable_data'] = [
22971 'link_id' => $latest_link['id'],
22972 'deleted_at' => current_time( 'mysql' ),
22973 ];
22974 $context['response_type'] = 'live';
22975 } else {
22976 $context['pluggable_data'] = [
22977 'link_id' => 17,
22978 'deleted_at' => current_time( 'mysql' ),
22979 ];
22980 $context['response_type'] = 'sample';
22981 }
22982 break;
22983
22984 case 'link_clicked':
22985 $link_data = ! empty( $latest_link ) ? $format_link( $latest_link ) : $format_link( [] );
22986
22987 $context['pluggable_data'] = [
22988 'link' => $link_data,
22989 ];
22990 $context['response_type'] = ! empty( $latest_link ) ? 'live' : 'sample';
22991 break;
22992 }
22993
22994 return (array) $context;
22995 }
22996
22997 /**
22998 * Get Pretty Links Last Data
22999 *
23000 * @param array $data data.
23001 *
23002 * @return array
23003 */
23004 public function search_pretty_links_triggers_last_data( $data ) {
23005 global $wpdb;
23006
23007 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
23008 $context = [];
23009
23010 // Check if Pretty Links plugin is active.
23011 if ( ! defined( 'PRLI_VERSION' ) ) {
23012 return $context;
23013 }
23014
23015 // Get latest pretty link.
23016 $latest_link = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}prli_links ORDER BY id DESC LIMIT 1", ARRAY_A );
23017
23018 // Get latest click if available.
23019 $latest_click = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}prli_clicks ORDER BY id DESC LIMIT 1", ARRAY_A );
23020
23021 $get_user_data = function( $user_id ) {
23022 if ( class_exists( '\SureTriggers\Integrations\WordPress\WordPress' ) ) {
23023 return WordPress::get_user_context( $user_id );
23024 }
23025 return [
23026 'user_id' => 0,
23027 'user_login' => 'guest',
23028 'display_name' => 'Guest User',
23029 'user_firstname' => '',
23030 'user_lastname' => '',
23031 'user_email' => 'guest@example.com',
23032 'user_role' => [],
23033 ];
23034 };
23035 $format_link = function( $link_data ) {
23036 return [
23037 'id' => isset( $link_data['id'] ) ? $link_data['id'] : 17,
23038 'name' => isset( $link_data['name'] ) ? $link_data['name'] : 'Sample Pretty Link',
23039 'url' => isset( $link_data['url'] ) ? $link_data['url'] : 'https://example.com',
23040 'slug' => isset( $link_data['slug'] ) ? $link_data['slug'] : 'sample-link',
23041 'pretty_url' => home_url( '/' . ( isset( $link_data['slug'] ) ? $link_data['slug'] : 'sample-link' ) ),
23042 'description' => isset( $link_data['description'] ) ? $link_data['description'] : 'Sample description',
23043 'redirect_type' => isset( $link_data['redirect_type'] ) ? $link_data['redirect_type'] : '307',
23044 'clicks' => isset( $link_data['clicks'] ) ? $link_data['clicks'] : '5',
23045 'created_at' => isset( $link_data['created_at'] ) ? $link_data['created_at'] : current_time( 'mysql' ),
23046 ];
23047 };
23048
23049 $format_click = function( $click_data ) {
23050 return [
23051 'click_id' => isset( $click_data['id'] ) ? $click_data['id'] : 42,
23052 'url' => isset( $click_data['url'] ) ? $click_data['url'] : 'https://example.com',
23053 'timestamp' => isset( $click_data['created_at'] ) ? $click_data['created_at'] : current_time( 'mysql' ),
23054 'is_logged_in' => true,
23055 ];
23056 };
23057
23058 switch ( $term ) {
23059 case 'link_clicked':
23060 $link_data = ! empty( $latest_link ) ? $format_link( $latest_link ) : $format_link( [] );
23061 $click_data = ! empty( $latest_click ) ? $format_click( $latest_click ) : $format_click( [] );
23062
23063 // Get user context from latest click or sample data.
23064 $user_id = 0;
23065 if ( ! empty( $latest_click ) ) {
23066 if ( isset( $latest_click['user'] ) && $latest_click['user'] > 0 ) {
23067 $user_id = $latest_click['user'];
23068 } elseif ( isset( $latest_click['user_id'] ) && $latest_click['user_id'] > 0 ) {
23069 $user_id = $latest_click['user_id'];
23070 } elseif ( isset( $latest_click['visitor'] ) && is_numeric( $latest_click['visitor'] ) && $latest_click['visitor'] > 0 ) {
23071 $user_id = $latest_click['visitor'];
23072 }
23073 }
23074
23075 if ( $user_id > 0 ) {
23076 $user_data = $get_user_data( $user_id );
23077 } else {
23078 $user_data = [
23079 'wp_user_id' => 1,
23080 'user_login' => 'sampleuser',
23081 'display_name' => 'Sample User',
23082 'user_firstname' => 'John',
23083 'user_lastname' => 'Doe',
23084 'user_email' => 'sampleuser@example.com',
23085 'user_registered' => current_time( 'mysql' ),
23086 'user_role' => [ 'subscriber' ],
23087 ];
23088 }
23089
23090 $context['pluggable_data'] = array_merge(
23091 $user_data,
23092 [
23093 'link' => $link_data,
23094 'click' => $click_data,
23095 ]
23096 );
23097 $context['response_type'] = ! empty( $latest_link ) ? 'live' : 'sample';
23098 break;
23099 }
23100
23101 return (array) $context;
23102 }
23103
23104 /**
23105 * Get ProfilePress Payment Methods.
23106 *
23107 * @param array $data data.
23108 *
23109 * @return array|void
23110 */
23111 public function search_profilepress_payment_methods( $data ) {
23112
23113 $options = [];
23114 if ( class_exists( '\ProfilePress\Core\Membership\PaymentMethods\PaymentMethods' ) ) {
23115 $payment_methods = \ProfilePress\Core\Membership\PaymentMethods\PaymentMethods::get_instance()->get_all();
23116
23117 $methods = [ '' => 'Default' ];
23118 if ( ! empty( $payment_methods ) ) {
23119 foreach ( $payment_methods as $method ) {
23120 $methods[ $method->get_id() ] = $method->get_method_title();
23121 }
23122 }
23123
23124 $methods = apply_filters( 'ppress_admin_order_page_enabled_payment_methods', $methods );
23125
23126 if ( $methods ) {
23127 foreach ( $methods as $id => $name ) {
23128 $options[] = [
23129 'label' => $name,
23130 'value' => $id,
23131 ];
23132 }
23133 }
23134 }
23135
23136 return [
23137 'options' => $options,
23138 'hasMore' => false,
23139 ];
23140 }
23141
23142 /**
23143 * Get ProfilePress Order Status List.
23144 *
23145 * @param array $data data.
23146 *
23147 * @return array|void
23148 */
23149 public function search_profilepress_order_status_list( $data ) {
23150
23151 $options = [];
23152
23153 if ( class_exists( 'ProfilePress\Core\Membership\Models\Order\OrderStatus' ) ) {
23154 $statuses = \ProfilePress\Core\Membership\Models\Order\OrderStatus::get_all();
23155 if ( $statuses ) {
23156 foreach ( $statuses as $id => $name ) {
23157 $options[] = [
23158 'label' => $name,
23159 'value' => $id,
23160 ];
23161 }
23162 }
23163 }
23164
23165 return [
23166 'options' => $options,
23167 'hasMore' => false,
23168 ];
23169 }
23170
23171 /**
23172 * Get ProfilePress Last Data
23173 *
23174 * @param array $data data.
23175 *
23176 * @return array
23177 */
23178 public function search_profilepress_triggers_last_data( $data ) {
23179 global $wpdb;
23180
23181 $context = [];
23182
23183 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
23184
23185 switch ( $term ) {
23186
23187 /**
23188 * Customer Updated trigger.
23189 */
23190 case 'profilepress_customer_updated':
23191 if ( class_exists( '\ProfilePress\Core\Membership\Models\Customer\CustomerFactory' ) ) {
23192 $customer = $wpdb->get_row(
23193 "SELECT * FROM {$wpdb->prefix}ppress_customers ORDER BY id DESC LIMIT 1",
23194 ARRAY_A
23195 );
23196
23197 if ( $customer ) {
23198 $customer_id = isset( $customer['id'] ) ? (int) $customer['id'] : 0;
23199 $customer = \ProfilePress\Core\Membership\Models\Customer\CustomerFactory::fromId( absint( $customer_id ) );
23200
23201 $pluggable_data = [
23202 'customer' => Utilities::object_to_array( $customer ),
23203 ];
23204
23205 $context = [
23206 'pluggable_data' => $pluggable_data,
23207 'response_type' => 'live',
23208 ];
23209 } else {
23210
23211 $customer = \ProfilePress\Core\Membership\Models\Customer\CustomerFactory::fromId( absint( 0 ) );
23212 $sample_data = [
23213 'customer' => Utilities::object_to_array( $customer ),
23214 ];
23215
23216 $context = [
23217 'pluggable_data' => $sample_data,
23218 'response_type' => 'sample',
23219 ];
23220 }
23221 }
23222
23223 break;
23224
23225 /**
23226 * Subscription Activated trigger.
23227 */
23228 case 'profilepress_subscription_activated':
23229 $latest_subscription = $wpdb->get_row(
23230 'SELECT * FROM ' . $wpdb->prefix . 'ppress_subscriptions ORDER BY id DESC LIMIT 1',
23231 ARRAY_A
23232 );
23233
23234 if ( $latest_subscription ) {
23235 if ( class_exists( '\ProfilePress\Core\Membership\Models\Subscription\SubscriptionFactory' ) ) {
23236 $latest_subscription = \ProfilePress\Core\Membership\Models\Subscription\SubscriptionFactory::fromId( intval( $latest_subscription['id'] ) );
23237 $context['pluggable_data']['subscription'] = Utilities::object_to_array( $latest_subscription );
23238 $context['response_type'] = 'live';
23239 }
23240 } else {
23241 $sample_subscription = [
23242 'id' => 1,
23243 'parent_order_id' => 1,
23244 'plan_id' => 1,
23245 'customer_id' => 7,
23246 'billing_frequency' => 'monthly',
23247 'initial_amount' => '10.00000000',
23248 'initial_tax_rate' => '0',
23249 'initial_tax' => '0',
23250 'recurring_amount' => '10.00000000',
23251 'recurring_tax_rate' => '0',
23252 'recurring_tax' => '0',
23253 'total_payments' => '0',
23254 'trial_period' => 'disabled',
23255 'profile_id' => '',
23256 'status' => 'active',
23257 'notes' => null,
23258 'created_date' => '2025-08-19 11:45:41',
23259 'expiration_date' => '2025-09-19 11:45:41',
23260 ];
23261 $context['pluggable_data']['subscription'] = $sample_subscription;
23262 $context['response_type'] = 'sample';
23263 }
23264 break;
23265
23266 default:
23267 $context['pluggable_data'] = [];
23268 $context['response_type'] = 'error';
23269 break;
23270
23271 }
23272
23273 return $context;
23274 }
23275
23276 /**
23277 * Search FluentCRM Company Data.
23278 *
23279 * @param array $data data.
23280 * @return array
23281 */
23282 public function search_pluggables_fluentcrm_company_created( $data ) {
23283 global $wpdb;
23284
23285 $context = [];
23286 $pluggable_data = [];
23287 $term = ! empty( $data['search_term'] ) ? $data['search_term'] : '';
23288
23289 if ( ! class_exists( 'FluentCrm\App\Models\Company' ) ) {
23290 return [];
23291 }
23292
23293 $result = null;
23294
23295 // Fetch last record based on term.
23296 switch ( $term ) {
23297 case 'contact_deleted':
23298 $result = $wpdb->get_row( "SELECT id FROM {$wpdb->prefix}fc_subscribers ORDER BY id DESC LIMIT 1", ARRAY_A );
23299 break;
23300
23301 case 'campaign_sent':
23302 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fc_campaigns WHERE status = 'archived' ORDER BY updated_at DESC LIMIT 1", ARRAY_A );
23303 break;
23304
23305 case 'company_updated':
23306 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fc_companies ORDER BY updated_at DESC LIMIT 1", ARRAY_A );
23307 break;
23308
23309 case 'company_deleted':
23310 case 'company_assigned':
23311 case 'company_unassigned':
23312 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fc_companies ORDER BY id DESC LIMIT 1", ARRAY_A );
23313 break;
23314
23315 default:
23316 // Default to company_created.
23317 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fc_companies ORDER BY created_at DESC LIMIT 1", ARRAY_A );
23318 break;
23319 }
23320
23321 if ( $result ) {
23322 switch ( $term ) {
23323 case 'contact_deleted':
23324 $pluggable_data = [
23325 'contact_id' => $result['id'],
23326 'deleted_at' => current_time( 'mysql' ),
23327 ];
23328 break;
23329
23330 case 'campaign_sent':
23331 $campaign_data = $result;
23332
23333 if ( ! empty( $campaign_data['settings'] ) ) {
23334 $settings = maybe_unserialize( $campaign_data['settings'] );
23335 if ( is_array( $settings ) ) {
23336 $campaign_data['settings'] = $settings;
23337 }
23338 }
23339
23340 $pluggable_data = [
23341 'campaign' => $campaign_data,
23342 'status' => 'archived',
23343 'archived_at' => current_time( 'mysql' ),
23344 ];
23345
23346 if ( ! empty( $result['id'] ) ) {
23347 $total_emails = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}fc_campaign_emails WHERE campaign_id = %d", $result['id'] ) );
23348 $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'] ) );
23349 $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'] ) );
23350 $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'] ) );
23351
23352 $pluggable_data['total_emails_sent'] = $total_emails;
23353 $pluggable_data['sent_count'] = $sent_count;
23354 $pluggable_data['opened_count'] = $opened_count;
23355 $pluggable_data['clicked_count'] = $clicked_count;
23356 $pluggable_data['open_rate'] = $sent_count > 0 ? round( ( $opened_count / $sent_count ) * 100, 2 ) : 0;
23357 $pluggable_data['click_rate'] = $sent_count > 0 ? round( ( $clicked_count / $sent_count ) * 100, 2 ) : 0;
23358 }
23359 break;
23360
23361 case 'company_deleted':
23362 $pluggable_data = [
23363 'company_id' => $result['id'],
23364 'deleted_at' => current_time( 'mysql' ),
23365 ];
23366 break;
23367
23368 case 'company_assigned':
23369 case 'company_unassigned':
23370 $contact_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fc_subscribers ORDER BY id DESC LIMIT 1", ARRAY_A );
23371 if ( $contact_result ) {
23372 $pluggable_data['contact'] = [
23373 'details' => $contact_result,
23374 'custom' => [],
23375 ];
23376 $pluggable_data['companies'] = [ $result ];
23377
23378 if ( 'company_assigned' === $term ) {
23379 $pluggable_data['assigned_company_ids'] = [ $result['id'] ];
23380 $pluggable_data['assigned_at'] = current_time( 'mysql' );
23381 } else {
23382 $pluggable_data['unassigned_company_ids'] = [ $result['id'] ];
23383 $pluggable_data['unassigned_at'] = current_time( 'mysql' );
23384 }
23385 }
23386 break;
23387
23388 default:
23389 $meta_data = ! empty( $result['meta'] ) ? maybe_unserialize( $result['meta'] ) : [];
23390 $company_result = array_intersect_key(
23391 $result,
23392 array_flip(
23393 [
23394 'id',
23395 'hash',
23396 'name',
23397 'email',
23398 'description',
23399 'address_line_1',
23400 'address_line_2',
23401 'city',
23402 'state',
23403 'postal_code',
23404 'country',
23405 'phone',
23406 'type',
23407 'owner_id',
23408 'employees_number',
23409 'industry',
23410 'website',
23411 'linkedin_url',
23412 'facebook_url',
23413 'twitter_url',
23414 'logo',
23415 'timezone',
23416 'date_of_start',
23417 'created_at',
23418 'updated_at',
23419 ]
23420 )
23421 );
23422
23423 if ( is_array( $meta_data ) && isset( $meta_data['custom_values'] ) ) {
23424 $company_result['custom_fields'] = $meta_data['custom_values'];
23425 }
23426
23427 $pluggable_data['company'] = $company_result;
23428 break;
23429 }
23430 $context['response_type'] = 'live';
23431 } else {
23432 // Sample data for different terms.
23433 switch ( $term ) {
23434 case 'contact_deleted':
23435 $pluggable_data = [
23436 'contact_id' => 1,
23437 'deleted_at' => current_time( 'mysql' ),
23438 ];
23439 break;
23440
23441 case 'campaign_sent':
23442 $pluggable_data = [
23443 'campaign' => [
23444 'id' => 1,
23445 'type' => 'campaign',
23446 'title' => 'Sample Email Campaign',
23447 'status' => 'archived',
23448 'template_id' => 1,
23449 'email_subject' => 'Welcome to Our Newsletter!',
23450 'email_body' => 'Sample campaign email content...',
23451 'utm_status' => 1,
23452 'utm_source' => 'fluentcrm',
23453 'utm_medium' => 'email',
23454 'utm_campaign' => 'sample-campaign',
23455 'created_by' => 1,
23456 'created_at' => current_time( 'mysql' ),
23457 'updated_at' => current_time( 'mysql' ),
23458 'scheduled_at' => current_time( 'mysql' ),
23459 ],
23460 'status' => 'archived',
23461 'archived_at' => current_time( 'mysql' ),
23462 'total_emails_sent' => 1500,
23463 'sent_count' => 1450,
23464 'opened_count' => 725,
23465 'clicked_count' => 145,
23466 'open_rate' => 50.0,
23467 'click_rate' => 10.0,
23468 ];
23469 break;
23470
23471 case 'company_deleted':
23472 $pluggable_data = [
23473 'company_id' => 1,
23474 'deleted_at' => current_time( 'mysql' ),
23475 ];
23476 break;
23477
23478 case 'company_assigned':
23479 case 'company_unassigned':
23480 $pluggable_data['contact']['details'] = [
23481 'id' => 1,
23482 'user_id' => 1,
23483 'hash' => 'sample_hash',
23484 'contact_owner' => 0,
23485 'company_id' => 0,
23486 'prefix' => 'Mr.',
23487 'first_name' => 'John',
23488 'last_name' => 'Doe',
23489 'email' => 'john@example.com',
23490 'timezone' => 'America/New_York',
23491 'address_line_1' => '123 Main Street',
23492 'address_line_2' => 'Apt 4B',
23493 'postal_code' => '10001',
23494 'city' => 'New York',
23495 'state' => 'NY',
23496 'country' => 'USA',
23497 'phone' => '+1-555-123-4567',
23498 'status' => 'subscribed',
23499 'contact_type' => 'customer',
23500 'source' => 'manual',
23501 'lifecycle_stage' => 'customer',
23502 'created_at' => current_time( 'mysql' ),
23503 'updated_at' => current_time( 'mysql' ),
23504 ];
23505 $pluggable_data['contact']['custom'] = [];
23506 $pluggable_data['companies'] = [
23507 [
23508 'id' => 1,
23509 'hash' => 'sample_hash',
23510 'name' => 'Sample Company Inc.',
23511 'email' => 'contact@samplecompany.com',
23512 'description' => 'This is a sample company',
23513 'address_line_1' => '123 Business Street',
23514 'address_line_2' => 'Suite 100',
23515 'city' => 'Business City',
23516 'state' => 'California',
23517 'postal_code' => '90210',
23518 'country' => 'USA',
23519 'phone' => '+1-555-123-4567',
23520 'type' => 'client',
23521 'created_at' => current_time( 'mysql' ),
23522 'updated_at' => current_time( 'mysql' ),
23523 ],
23524 ];
23525 if ( 'company_assigned' === $term ) {
23526 $pluggable_data['assigned_company_ids'] = [ 1 ];
23527 $pluggable_data['assigned_at'] = current_time( 'mysql' );
23528 } else {
23529 $pluggable_data['unassigned_company_ids'] = [ 1 ];
23530 $pluggable_data['unassigned_at'] = current_time( 'mysql' );
23531 }
23532 break;
23533
23534 case 'company_created':
23535 case 'company_updated':
23536 default:
23537 $pluggable_data['company'] = [
23538 'id' => 1,
23539 'hash' => 'sample_hash',
23540 'name' => 'Sample Company Inc.',
23541 'email' => 'contact@samplecompany.com',
23542 'description' => 'This is a sample company',
23543 'address_line_1' => '123 Business Street',
23544 'address_line_2' => 'Suite 100',
23545 'city' => 'Business City',
23546 'state' => 'California',
23547 'postal_code' => '90210',
23548 'country' => 'USA',
23549 'phone' => '+1-555-123-4567',
23550 'type' => 'client',
23551 'owner_id' => 1,
23552 'employees_number' => 50,
23553 'industry' => 'Technology',
23554 'website' => 'https://samplecompany.com',
23555 'linkedin_url' => 'https://linkedin.com/company/samplecompany',
23556 'facebook_url' => 'https://facebook.com/samplecompany',
23557 'twitter_url' => 'https://twitter.com/samplecompany',
23558 'logo' => '',
23559 'timezone' => 'America/Los_Angeles',
23560 'date_of_start' => '2024-01-01',
23561 'created_at' => '2024-01-01 10:00:00',
23562 'updated_at' => '2024-01-01 10:00:00',
23563 'custom_fields' => [
23564 'annual_revenue' => '$1,000,000',
23565 'primary_contact' => 'John Doe',
23566 ],
23567 ];
23568 break;
23569 }
23570
23571 $context['response_type'] = 'sample';
23572 }
23573
23574 $context['pluggable_data'] = $pluggable_data;
23575 return $context;
23576 }
23577
23578 /**
23579 * Get FluentCRM email trigger details.
23580 *
23581 * @param array $data data.
23582 *
23583 * @return array
23584 */
23585 public function search_pluggables_fluentcrm_email_triggers( $data ) {
23586 $context = [];
23587 $pluggable_data = [];
23588 global $wpdb;
23589 $term = $data['search_term'] ? $data['search_term'] : '';
23590
23591 // Helper function to build email data structure.
23592 $build_email_data = function( $email_result, $subscriber_result = null, $subscriber_status = null ) {
23593 $email_data = [
23594 'id' => $email_result['id'],
23595 'status' => $email_result['status'],
23596 'is_open' => $email_result['is_open'],
23597 'is_parsed' => $email_result['is_parsed'],
23598 'created_at' => $email_result['created_at'],
23599 'updated_at' => isset( $email_result['updated_at'] ) ? $email_result['updated_at'] : $email_result['created_at'],
23600 'campaign_id' => isset( $email_result['campaign_id'] ) ? $email_result['campaign_id'] : null,
23601 'scheduled_at' => isset( $email_result['scheduled_at'] ) ? $email_result['scheduled_at'] : $email_result['created_at'],
23602 'email_address' => isset( $email_result['email_address'] ) ? $email_result['email_address'] : ( isset( $subscriber_result['email'] ) ? $subscriber_result['email'] : '' ),
23603 'email_headers' => isset( $email_result['email_headers'] ) ? $email_result['email_headers'] : '',
23604 'email_subject' => isset( $email_result['email_subject'] ) ? $email_result['email_subject'] : '',
23605 'email_body' => $email_result['email_body'],
23606 'email_hash' => $email_result['email_hash'],
23607 'email_type' => $email_result['email_type'],
23608 'subscriber_id' => $email_result['subscriber_id'],
23609 ];
23610
23611 if ( $subscriber_result ) {
23612 $subscriber_fields = [
23613 'hash',
23614 'email',
23615 'phone',
23616 'photo',
23617 'prefix',
23618 'status',
23619 'full_name',
23620 'last_name',
23621 'created_at',
23622 'first_name',
23623 'updated_at',
23624 ];
23625 foreach ( $subscriber_fields as $field ) {
23626 $email_data[ 'subscriber_' . $field ] = $subscriber_result[ $field ];
23627 }
23628 if ( $subscriber_status ) {
23629 $email_data['subscriber_status'] = $subscriber_status;
23630 }
23631 }
23632
23633 return $email_data;
23634 };
23635
23636 // Helper function to get sample email data.
23637 $get_sample_email_data = function( $status = 'subscribed' ) {
23638 return [
23639 'id' => 1,
23640 'status' => 'sent',
23641 'is_open' => 0,
23642 'is_parsed' => 1,
23643 'created_at' => current_time( 'mysql' ),
23644 'updated_at' => current_time( 'mysql' ),
23645 'campaign_id' => 83,
23646 'scheduled_at' => current_time( 'mysql' ),
23647 'email_address' => 'john@example.com',
23648 'email_headers' => 'From: noreply@example.com',
23649 'email_subject' => 'Sample Email Subject',
23650 'email_body' => 'Sample email content here...',
23651 'email_hash' => 'sample-email-hash-123',
23652 'email_type' => 'campaign',
23653 'subscriber_id' => 1,
23654 'subscriber_hash' => 'sample_hash',
23655 'subscriber_email' => 'john@example.com',
23656 'subscriber_phone' => '9876654423',
23657 'subscriber_photo' => 'http://example.com/avatar.png',
23658 'subscriber_prefix' => 'Mr.',
23659 'subscriber_status' => $status,
23660 'subscriber_full_name' => 'John Doe',
23661 'subscriber_last_name' => 'Doe',
23662 'subscriber_created_at' => current_time( 'mysql' ),
23663 'subscriber_first_name' => 'John',
23664 'subscriber_updated_at' => current_time( 'mysql' ),
23665 ];
23666 };
23667
23668 // Get result based on term.
23669 $result = null;
23670 if ( 'email_opened' === $term ) {
23671 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fc_campaign_emails ORDER BY id DESC LIMIT %d", 1 ), ARRAY_A );
23672 } elseif ( 'email_clicked' === $term ) {
23673 $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 );
23674 } elseif ( 'email_unsubscribed' === $term ) {
23675 $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 );
23676 } elseif ( 'email_bounced' === $term ) {
23677 $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 );
23678 }
23679
23680 if ( $result ) {
23681 if ( 'email_opened' === $term || 'email_clicked' === $term ) {
23682 // Get subscriber details.
23683 $subscriber_result = null;
23684 if ( ! empty( $result['subscriber_id'] ) ) {
23685 $subscriber_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fc_subscribers WHERE id = %d", $result['subscriber_id'] ), ARRAY_A );
23686 }
23687
23688 $pluggable_data['email'] = $build_email_data( $result, $subscriber_result );
23689 $pluggable_data[ 'email_opened' === $term ? 'opened_at' : 'clicked_at' ] = current_time( 'mysql' );
23690
23691 } elseif ( 'email_unsubscribed' === $term ) {
23692 // Get email data for subscriber.
23693 $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 );
23694
23695 if ( $email_result ) {
23696 $pluggable_data['email'] = $build_email_data( $email_result, $result );
23697 }
23698
23699 $pluggable_data['source'] = 'from_header';
23700 $pluggable_data['unsubscribed_at'] = current_time( 'mysql' );
23701 } elseif ( 'email_bounced' === $term ) {
23702 // For bounced emails, we only need subscriber object and bounce-specific fields.
23703 $subscriber_data = [];
23704 $subscriber_fields = [
23705 'id',
23706 'hash',
23707 'email',
23708 'phone',
23709 'photo',
23710 'prefix',
23711 'status',
23712 'full_name',
23713 'last_name',
23714 'created_at',
23715 'first_name',
23716 'updated_at',
23717 'contact_type',
23718 'total_points',
23719 'last_activity',
23720 'life_time_value',
23721 'ip',
23722 ];
23723
23724 foreach ( $subscriber_fields as $field ) {
23725 if ( isset( $result[ $field ] ) ) {
23726 $subscriber_data[ $field ] = $result[ $field ];
23727 }
23728 }
23729
23730 $pluggable_data['subscriber'] = $subscriber_data;
23731
23732 // Add bounce-specific data.
23733 $pluggable_data['old_status'] = 'subscribed';
23734 $pluggable_data['new_status'] = 'bounced';
23735 $pluggable_data['bounced_at'] = $result['updated_at'];
23736 }
23737 $context['response_type'] = 'live';
23738 } else {
23739 // Sample data for different email terms.
23740 if ( 'email_opened' === $term || 'email_clicked' === $term ) {
23741 $pluggable_data['email'] = $get_sample_email_data();
23742
23743 if ( 'email_opened' === $term ) {
23744 $pluggable_data['opened_at'] = current_time( 'mysql' );
23745 } else {
23746 $pluggable_data['url'] = [
23747 'id' => 1,
23748 'url' => 'https://example.com/sample-link',
23749 'short_url' => 'https://example.com/r/abc123',
23750 'title' => 'Sample Link',
23751 'created_at' => current_time( 'mysql' ),
23752 ];
23753 $pluggable_data['clicked_at'] = current_time( 'mysql' );
23754 }
23755 } elseif ( 'email_unsubscribed' === $term ) {
23756 $pluggable_data['email'] = $get_sample_email_data( 'unsubscribed' );
23757 $pluggable_data['source'] = 'from_header';
23758 $pluggable_data['unsubscribed_at'] = current_time( 'mysql' );
23759 } elseif ( 'email_bounced' === $term ) {
23760 // Sample subscriber object for bounced email (no email object).
23761 $pluggable_data['subscriber'] = [
23762 'id' => 8,
23763 'ip' => '::1',
23764 'hash' => '6b2c0a83138a7e42ef12e6f87e8c58aa',
23765 'email' => 'mrunalis@bsf.io',
23766 'phone' => '9876654423',
23767 'photo' => 'http://ottokit.local/wp-content/plugins/fluent-crm/assets/images/avatar.png',
23768 'prefix' => 'Ms',
23769 'status' => 'bounced',
23770 'full_name' => 'Mrunali Salunke',
23771 'last_name' => 'Salunke',
23772 'created_at' => '2025-09-18 04:51:26',
23773 'first_name' => 'Mrunali',
23774 'updated_at' => current_time( 'mysql' ),
23775 'contact_type' => 'lead',
23776 'total_points' => 0,
23777 'last_activity' => '2025-09-25 05:47:45',
23778 'life_time_value' => 0,
23779 ];
23780 $pluggable_data['old_status'] = 'subscribed';
23781 $pluggable_data['new_status'] = 'bounced';
23782 $pluggable_data['bounced_at'] = current_time( 'mysql' );
23783 }
23784 $context['response_type'] = 'sample';
23785 }
23786
23787 $context['pluggable_data'] = $pluggable_data;
23788 return $context;
23789 }
23790
23791 /**
23792 * Get WooCommerce Subscription triggers last data.
23793 *
23794 * @param array $data data.
23795 *
23796 * @return array
23797 */
23798 public function search_woocommerce_subscription_triggers_last_data( $data ) {
23799 $context = [];
23800 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
23801
23802 if ( ! class_exists( 'WC_Subscription' ) || ! function_exists( 'wcs_get_subscription' ) ) {
23803 return $context;
23804 }
23805
23806 $needs_subscription = in_array(
23807 $term,
23808 [
23809 'wc_subscription_updated',
23810 'wc_cancels_subscription_product',
23811 'wc_subscribes_product',
23812 'wc_purchases_variable_subscription',
23813 'wc_renews_subscription_product',
23814 'wc_subscription_product_expires',
23815 ],
23816 true
23817 );
23818
23819 if ( ! $needs_subscription ) {
23820 return $context;
23821 }
23822
23823 $subscription = null;
23824 $subscription_obj = null;
23825
23826 if ( function_exists( 'wcs_get_subscriptions' ) ) {
23827 $subscriptions = wcs_get_subscriptions(
23828 [
23829 'subscriptions_per_page' => 1,
23830 'orderby' => 'date',
23831 'order' => 'DESC',
23832 ]
23833 );
23834
23835 if ( ! empty( $subscriptions ) ) {
23836 $subscription_obj = reset( $subscriptions );
23837 $subscription = (object) [ 'ID' => $subscription_obj->get_id() ];
23838 }
23839 }
23840
23841 if ( $subscription && ! $subscription_obj ) {
23842 $subscription_obj = wcs_get_subscription( $subscription->ID );
23843 }
23844
23845 if ( $subscription_obj ) {
23846 $user_id = $subscription_obj->get_user_id();
23847 $items = $subscription_obj->get_items();
23848 $product_id = 0;
23849 $variation_id = 0;
23850 $product_name = '';
23851 $variation_name = '';
23852
23853 foreach ( $items as $item ) {
23854 $product_id = $item->get_product_id();
23855 $variation_id = $item->get_variation_id();
23856 $product_name = $item->get_name();
23857 $variation_name = $item->get_name();
23858 break;
23859 }
23860
23861 $user_billing_data = [
23862 'billing_first_name' => $subscription_obj->get_billing_first_name(),
23863 'billing_last_name' => $subscription_obj->get_billing_last_name(),
23864 'billing_country' => $subscription_obj->get_billing_country(),
23865 'billing_address_1' => $subscription_obj->get_billing_address_1(),
23866 'billing_address_2' => $subscription_obj->get_billing_address_2(),
23867 'billing_city' => $subscription_obj->get_billing_city(),
23868 'billing_state' => $subscription_obj->get_billing_state(),
23869 'billing_postcode' => $subscription_obj->get_billing_postcode(),
23870 'billing_phone' => $subscription_obj->get_billing_phone(),
23871 'billing_email' => $subscription_obj->get_billing_email(),
23872 ];
23873
23874 if ( 'wc_subscription_updated' === $term ) {
23875 $subscription_data = [
23876 'start_date' => $subscription_obj->get_date_created(),
23877 'next_payment_date' => $subscription_obj->get_date( 'next_payment' ) ? $subscription_obj->get_date( 'next_payment' ) : 0,
23878 ];
23879
23880 $context['variation_id'] = $variation_id;
23881 $context['product_id'] = $product_id;
23882 $context['subscription'] = $product_id;
23883 $context['variation_name'] = $variation_name;
23884 $context['product_name'] = $product_name;
23885 $context['subscription_data'] = $subscription_data;
23886 $context['subscription_id'] = $subscription_obj->get_id();
23887 $context['user'] = WordPress::get_user_context( $user_id );
23888 $context['user_billing_data'] = $user_billing_data;
23889 $context['new_status'] = $subscription_obj->get_status();
23890 $context['old_status'] = 'pending-cancel';
23891 $context['status'] = 'wc-' . $subscription_obj->get_status();
23892 } elseif ( 'wc_cancels_subscription_product' === $term ) {
23893 $subscription_data = [
23894 'status' => $subscription_obj->get_status(),
23895 'start_date' => $subscription_obj->get_date_created(),
23896 ];
23897
23898 $context['subscription_data'] = $subscription_data;
23899 $context['user'] = WordPress::get_user_context( $user_id );
23900 $context['user_billing_data'] = $user_billing_data;
23901 $context['subscription_id'] = $subscription_obj->get_id();
23902 } elseif ( 'wc_subscribes_product' === $term ) {
23903 $subscription_data = [
23904 'status' => $subscription_obj->get_status(),
23905 'start_date' => $subscription_obj->get_date_created(),
23906 'next_payment_date' => $subscription_obj->get_date( 'next_payment' ) ? $subscription_obj->get_date( 'next_payment' ) : 0,
23907 ];
23908
23909 $context['subscription_data'] = $subscription_data;
23910 $context['subscription_id'] = $subscription_obj->get_id();
23911 $context['user'] = WordPress::get_user_context( $user_id );
23912 $context['user_billing_data'] = $user_billing_data;
23913 $context['subscription'] = $subscription_obj->get_id();
23914 $context['subscription_name'] = get_the_title( $product_id );
23915 } elseif ( 'wc_purchases_variable_subscription' === $term ) {
23916 $subscription_data = [
23917 'status' => $subscription_obj->get_status(),
23918 'start_date' => $subscription_obj->get_date_created(),
23919 'next_payment_date' => $subscription_obj->get_date( 'next_payment' ) ? $subscription_obj->get_date( 'next_payment' ) : 0,
23920 ];
23921
23922 $context['subscription_data'] = $subscription_data;
23923 $context['user'] = WordPress::get_user_context( $user_id );
23924 $context['user_billing_data'] = $user_billing_data;
23925 $context['variable_subscription_option'] = $variation_id;
23926 $context['variable_subscription'] = $product_id;
23927 $context['subscription_name'] = $variation_name;
23928 $context['subscription_id'] = $subscription_obj->get_id();
23929 } elseif ( 'wc_renews_subscription_product' === $term ) {
23930 $subscription_data = [
23931 'status' => $subscription_obj->get_status(),
23932 'start_date' => $subscription_obj->get_date_created(),
23933 'next_payment_date' => $subscription_obj->get_date( 'next_payment' ) ? $subscription_obj->get_date( 'next_payment' ) : 0,
23934 ];
23935
23936 $context['subscription'] = $product_id;
23937 $context['subscription_name'] = get_the_title( $product_id );
23938 $context['last_order'] = $subscription_obj->get_last_order();
23939 $context['subscription_data'] = $subscription_data;
23940 $context['subscription_id'] = $subscription_obj->get_id();
23941 $context['user'] = WordPress::get_user_context( $user_id );
23942 $context['user_billing_data'] = $user_billing_data;
23943 } elseif ( 'wc_subscription_product_expires' === $term ) {
23944 $subscription_data = [
23945 'status' => $subscription_obj->get_status(),
23946 'start_date' => $subscription_obj->get_date_created(),
23947 'next_payment_date' => $subscription_obj->get_date( 'next_payment' ) ? $subscription_obj->get_date( 'next_payment' ) : 0,
23948 ];
23949
23950 $context['subscription_data'] = $subscription_data;
23951 $context['subscription_id'] = $subscription_obj->get_id();
23952 $context['user'] = WordPress::get_user_context( $user_id );
23953 $context['user_billing_data'] = $user_billing_data;
23954 $context['subscription'] = $product_id;
23955 $context['subscription_name'] = get_the_title( $product_id );
23956 }
23957
23958 $context['pluggable_data'] = $context;
23959 $context['response_type'] = 'live';
23960 } else {
23961 $sample_user = [
23962 'wp_user_id' => 1,
23963 'user_login' => 'johndoe',
23964 'display_name' => 'John Doe',
23965 'user_firstname' => 'John',
23966 'user_lastname' => 'Doe',
23967 'user_email' => 'john.doe@example.com',
23968 'user_registered' => '2024-01-15 10:30:25',
23969 'user_role' => [
23970 'customer',
23971 'subscriber',
23972 ],
23973 ];
23974
23975 $sample_billing_data = [
23976 'billing_first_name' => 'John',
23977 'billing_last_name' => 'Doe',
23978 'billing_country' => 'US',
23979 'billing_address_1' => '123 Main Street',
23980 'billing_address_2' => 'Apt 4B',
23981 'billing_city' => 'New York',
23982 'billing_state' => 'NY',
23983 'billing_postcode' => '10001',
23984 'billing_phone' => '+1-555-123-4567',
23985 'billing_email' => 'john.doe@example.com',
23986 ];
23987
23988 $sample_start_date = [
23989 'utc_offset' => 0,
23990 'date' => '2024-01-15 08:30:25.000000',
23991 'timezone_type' => 1,
23992 'timezone' => '+00:00',
23993 ];
23994
23995 if ( 'wc_subscription_updated' === $term ) {
23996 $context = [
23997 'variation_id' => 0,
23998 'product_id' => 73,
23999 'subscription' => 73,
24000 'variation_name' => 'Hi-Fi Headphones',
24001 'product_name' => 'Hi-Fi Headphones',
24002 'subscription_data' => [
24003 'start_date' => $sample_start_date,
24004 'next_payment_date' => '2024-12-15 08:30:25',
24005 ],
24006 'subscription_id' => 1234,
24007 'user' => $sample_user,
24008 'user_billing_data' => $sample_billing_data,
24009 'new_status' => 'active',
24010 'old_status' => 'pending-cancel',
24011 'status' => 'wc-active',
24012 ];
24013 } elseif ( 'wc_cancels_subscription_product' === $term ) {
24014 $context = [
24015 'subscription_data' => [
24016 'status' => 'cancelled',
24017 'start_date' => $sample_start_date,
24018 ],
24019 'user' => $sample_user,
24020 'user_billing_data' => $sample_billing_data,
24021 'subscription_id' => 1567,
24022 ];
24023 } elseif ( 'wc_subscribes_product' === $term ) {
24024 $context = [
24025 'subscription_data' => [
24026 'status' => 'active',
24027 'start_date' => $sample_start_date,
24028 'next_payment_date' => '2025-10-07 08:38:53',
24029 ],
24030 'subscription_id' => 1798,
24031 'user' => $sample_user,
24032 'user_billing_data' => $sample_billing_data,
24033 'subscription' => 1795,
24034 'subscription_name' => 'Subscription Product',
24035 ];
24036 } elseif ( 'wc_purchases_variable_subscription' === $term ) {
24037 $context = [
24038 'subscription_data' => [
24039 'status' => 'active',
24040 'start_date' => $sample_start_date,
24041 'next_payment_date' => '2025-11-06 05:25:01',
24042 ],
24043 'user' => $sample_user,
24044 'user_billing_data' => $sample_billing_data,
24045 'variable_subscription_option' => 1803,
24046 'variable_subscription' => 1802,
24047 'subscription_name' => 'Variation test product – 1200, 10',
24048 'subscription_id' => 1836,
24049 ];
24050 } elseif ( 'wc_renews_subscription_product' === $term ) {
24051 $context = [
24052 'subscription' => 1234,
24053 'subscription_name' => 'Monthly Subscription Product',
24054 'last_order' => 5678,
24055 'subscription_data' => [
24056 'status' => 'active',
24057 'start_date' => $sample_start_date,
24058 'next_payment_date' => '2025-11-06 08:30:25',
24059 ],
24060 'subscription_id' => 1567,
24061 'user' => $sample_user,
24062 'user_billing_data' => $sample_billing_data,
24063 ];
24064 } elseif ( 'wc_subscription_product_expires' === $term ) {
24065 $context = [
24066 'subscription_data' => [
24067 'status' => 'expired',
24068 'start_date' => $sample_start_date,
24069 'next_payment_date' => '2025-01-15 08:30:25',
24070 ],
24071 'subscription_id' => 1890,
24072 'user' => $sample_user,
24073 'user_billing_data' => $sample_billing_data,
24074 'subscription' => 1567,
24075 'subscription_name' => 'Premium Subscription Product',
24076 ];
24077 }
24078 $context['pluggable_data'] = $context;
24079 $context['response_type'] = 'sample';
24080 }
24081
24082 return $context;
24083 }
24084
24085 }
24086
24087
24088 GlobalSearchController::get_instance();
24089