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

25,509 lines 873.0 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 MemberPress membership created trigger.
5837 *
5838 * @param array $data data.
5839 * @return array
5840 */
5841 public function search_mepr_event_member_signup_completed( $data ) {
5842 global $wpdb;
5843
5844 $term = $data['search_term'] ? $data['search_term'] : '';
5845
5846 $context = [];
5847 $context['response_type'] = 'sample';
5848
5849 $user_data = WordPress::get_sample_user_context();
5850
5851 $membership_data = [
5852 'membership_id' => '190',
5853 'membership_title' => 'Sample Membership',
5854 'membership_url' => site_url() . '/register/premium/',
5855 'membership_featured_image_id' => '521',
5856 'membership_featured_image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
5857 'amount' => '12.00',
5858 'total' => '12.00',
5859 'tax_amount' => '0.00',
5860 'tax_rate' => '0.00',
5861 'trans_num' => 't_63a03f3334f44',
5862 'status' => 'complete',
5863 'subscription_id' => '0',
5864 'transaction_id' => '123',
5865 'signup_date' => '2023-12-19 10:30:00',
5866 ];
5867
5868 // Get last completed transaction for live data.
5869 $transaction = $wpdb->get_row(
5870 "SELECT t.*, e.created_at as signup_date FROM {$wpdb->prefix}mepr_transactions t
5871 LEFT JOIN {$wpdb->prefix}mepr_events e ON e.evt_id = t.user_id AND e.event = 'member-signup-completed'
5872 WHERE t.status = 'complete'
5873 ORDER BY t.id DESC LIMIT 1"
5874 );
5875
5876 if ( ! empty( $transaction ) ) {
5877 // Get membership/product details.
5878 $membership = get_post( $transaction->product_id );
5879 if ( $membership ) {
5880 $membership_data = [
5881 'membership_id' => $transaction->product_id,
5882 'membership_title' => $membership->post_title,
5883 'membership_url' => get_permalink( $transaction->product_id ),
5884 'membership_featured_image_id' => get_post_meta( $transaction->product_id, '_thumbnail_id', true ),
5885 'membership_featured_image_url' => get_the_post_thumbnail_url( $transaction->product_id ),
5886 'amount' => $transaction->amount,
5887 'total' => $transaction->total,
5888 'tax_amount' => $transaction->tax_amount,
5889 'tax_rate' => $transaction->tax_rate,
5890 'trans_num' => $transaction->trans_num,
5891 'status' => $transaction->status,
5892 'subscription_id' => $transaction->subscription_id,
5893 'transaction_id' => $transaction->id,
5894 'signup_date' => $transaction->signup_date ? $transaction->signup_date : $transaction->created_at,
5895 ];
5896
5897 $user_data = WordPress::get_user_context( $transaction->user_id );
5898 $context['response_type'] = 'live';
5899 }
5900 }
5901
5902 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5903 return $context;
5904 }
5905
5906 /**
5907 * Get last data for trigger.
5908 *
5909 * @param array $data data.
5910 * @return array
5911 */
5912 public function search_wishlist_member_triggers_last_data( $data ) {
5913 global $wpdb;
5914
5915 $context = [];
5916 $context['response_type'] = 'sample';
5917
5918 $user_data = WordPress::get_sample_user_context();
5919
5920 $membership_data = [
5921 'membership_level_id' => '1',
5922 'membership_level_name' => 'Sample Membership Level',
5923 ];
5924
5925 $membership_level_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
5926
5927 if ( $membership_level_id > 0 ) {
5928 $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 ) );
5929 } else {
5930 $membership = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}wlm_userlevels ORDER BY id DESC LIMIT 1" );
5931 }
5932 if ( ! empty( $membership ) ) {
5933 $membership_data = WishlistMember::get_membership_detail_context( (int) $membership->level_id, (int) $membership->user_id );
5934 $user_data = WordPress::get_user_context( $membership->user_id );
5935
5936 $context['response_type'] = 'live';
5937 }
5938
5939 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5940
5941 return $context;
5942 }
5943
5944 /**
5945 * Get last data for trigger.
5946 *
5947 * @param array $data data.
5948 * @return array
5949 */
5950 public function search_peepso_triggers_last_data( $data ) {
5951 global $wpdb;
5952
5953 $context = [];
5954 $context['response_type'] = 'sample';
5955
5956 $user_data = WordPress::get_sample_user_context();
5957
5958 $post_data = [
5959 'post_id' => '1',
5960 'activity_id' => '2',
5961 'post_author' => '1',
5962 'post_content' => 'New sample post...!',
5963 'post_title' => 'Sample Post',
5964 'post_excerpt' => 'sample',
5965 'post_status' => 'publish',
5966 'post_type' => 'peepso-post',
5967 ];
5968
5969 $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" );
5970
5971 if ( ! empty( $post ) ) {
5972 $post_data = PeepSo::get_pp_activity_context( (int) $post->act_external_id, (int) $post->act_id );
5973 $user_data = WordPress::get_user_context( $post->act_owner_id );
5974
5975 $context['response_type'] = 'live';
5976 }
5977
5978 $context['pluggable_data'] = array_merge( $user_data, $post_data );
5979
5980 return $context;
5981 }
5982
5983 /**
5984 * Get last data for Peepso User triggers.
5985 *
5986 * @param array $data data.
5987 * @return array
5988 */
5989 public function search_peepso_user_triggers_last_data( $data ) {
5990 global $wpdb;
5991
5992 $context = [];
5993 $context['response_type'] = 'sample';
5994 $term = $data['search_term'];
5995
5996 if ( ! class_exists( 'PeepSoUser' ) ) {
5997 return [];
5998 }
5999
6000 if ( 'user_follows_member' === $term || 'user_gains_follower' === $term ) {
6001 $member_id = $data['filter']['follow_user_id']['value'];
6002 if ( -1 === $member_id ) {
6003 $followers = $wpdb->get_results(
6004 $wpdb->prepare(
6005 "SELECT * FROM
6006 {$wpdb->prefix}peepso_user_followers WHERE uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
6007 1
6008 )
6009 );
6010 } else {
6011 $followers = $wpdb->get_results(
6012 $wpdb->prepare(
6013 "SELECT * FROM
6014 {$wpdb->prefix}peepso_user_followers WHERE uf_passive_user_id = %d AND
6015 uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
6016 $member_id,
6017 1
6018 )
6019 );
6020 }
6021 } elseif ( 'user_unfollows_member' === $term || 'user_loses_follower' === $term ) {
6022 $member_id = $data['filter']['follow_user_id']['value'];
6023 if ( -1 === $member_id ) {
6024 $followers = $wpdb->get_results(
6025 $wpdb->prepare(
6026 "SELECT * FROM
6027 {$wpdb->prefix}peepso_user_followers WHERE uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
6028 0
6029 )
6030 );
6031 } else {
6032 $followers = $wpdb->get_results(
6033 $wpdb->prepare(
6034 "SELECT * FROM
6035 {$wpdb->prefix}peepso_user_followers WHERE uf_passive_user_id = %d AND
6036 uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
6037 $member_id,
6038 0
6039 )
6040 );
6041 }
6042 } elseif ( 'user_updates_avatar' === $term ) {
6043 $followers = $wpdb->get_results(
6044 $wpdb->prepare(
6045 "SELECT * FROM
6046 {$wpdb->prefix}peepso_users WHERE usr_avatar_custom = %d AND usr_role = %s
6047 ORDER BY usr_last_activity DESC LIMIT 1",
6048 1,
6049 'member'
6050 )
6051 );
6052 } elseif ( 'user_updates_specific_profile_field' === $term ) {
6053 $field_id = $data['filter']['user_profile_field_id']['value'];
6054 $followers = $wpdb->get_results(
6055 $wpdb->prepare(
6056 "SELECT * FROM
6057 {$wpdb->prefix}peepso_users WHERE usr_role = %s
6058 ORDER BY usr_last_activity DESC LIMIT 1",
6059 'member'
6060 )
6061 );
6062 }
6063 if ( 'user_updates_avatar' === $term ) {
6064 if ( ! empty( $followers ) ) {
6065 $context_data['user'] = WordPress::get_user_context( $followers[0]->usr_id );
6066 $context['response_type'] = 'live';
6067 } else {
6068 $context_data['user'] = WordPress::get_sample_user_context();
6069 }
6070 } elseif ( 'user_updates_specific_profile_field' === $term ) {
6071 if ( ! empty( $followers ) ) {
6072 $user = PeepSoUser::get_instance( $followers[0]->usr_id );
6073 $user->profile_fields->load_fields();
6074 $user_fields = $user->profile_fields->get_fields();
6075 foreach ( $user_fields as $key => $value ) {
6076 $val = get_user_meta( $followers[0]->usr_id, $value->key, true );
6077 if ( '' != $val ) {
6078 $context_data[ $value->title ] = $val;
6079 }
6080 }
6081 $curruser = get_userdata( $followers[0]->usr_id );
6082 $context_data['user_id'] = $followers[0]->usr_id;
6083 $context_data['user_email'] = $user->get_email();
6084 $context_data['avatar_url'] = $user->get_avatar();
6085 $context_data['profile_url'] = $user->get_profileurl();
6086 $context_data['about_me'] = get_user_meta( $followers[0]->usr_id, 'description', true );
6087 if ( $curruser instanceof \WP_User ) {
6088 $context_data['website'] = $curruser->user_url;
6089 }
6090 $context_data['role'] = $user->get_user_role();
6091 $context['response_type'] = 'live';
6092 } else {
6093 $context_data['user'] = WordPress::get_sample_user_context();
6094 }
6095 } else {
6096 if ( ! empty( $followers ) ) {
6097 $context_data['follower_user'] = WordPress::get_user_context( $followers[0]->uf_active_user_id );
6098 $context_data['following_user'] = WordPress::get_user_context( $followers[0]->uf_passive_user_id );
6099 $context['response_type'] = 'live';
6100 } else {
6101 $context_data['follower_user'] = WordPress::get_sample_user_context();
6102 $context_data['following_user'] = WordPress::get_sample_user_context();
6103 }
6104 }
6105
6106 $context['pluggable_data'] = $context_data;
6107
6108 return $context;
6109 }
6110
6111 /**
6112 * Search Peepso User Profile Fields list.
6113 *
6114 * @param array $data data.
6115 * @return array
6116 */
6117 public function search_peepso_profile_field_list( $data ) {
6118 $options = [];
6119 if ( ! class_exists( 'PeepSoUser' ) ) {
6120 return [];
6121 }
6122 $options = [
6123 [
6124 'label' => 'Allow others to "like" my profile',
6125 'value' => 'peepso_is_profile_likable',
6126 ],
6127 [
6128 'label' => 'Hide my birthday year',
6129 'value' => 'peepso_hide_birthday_year',
6130 ],
6131 [
6132 'label' => 'Who can see my profile',
6133 'value' => 'usr_profile_acc',
6134 ],
6135 [
6136 'label' => 'Who can post on my profile',
6137 'value' => 'peepso_profile_post_acc',
6138 ],
6139 [
6140 'label' => "Don't show my online status",
6141 'value' => 'peepso_hide_online_status',
6142 ],
6143 [
6144 'label' => 'My timezone',
6145 'value' => 'peepso_gmt_offset',
6146 ],
6147 ];
6148 $peepsouser = PeepSoUser::get_instance( 0 );
6149 $peepsouser->profile_fields->load_fields();
6150 $fields = $peepsouser->profile_fields->get_fields();
6151 foreach ( $fields as $field ) {
6152 if ( 1 == $field->published ) {
6153 $options[] = [
6154 'label' => $field->title,
6155 'value' => $field->id,
6156 ];
6157 }
6158 }
6159 return [
6160 'options' => $options,
6161 'hasMore' => false,
6162 ];
6163 }
6164
6165 /**
6166 * Search Peepso Roles list.
6167 *
6168 * @param array $data data.
6169 * @return array
6170 */
6171 public function search_peepso_role_list( $data ) {
6172 $options = [
6173 [
6174 'label' => 'Community Member',
6175 'value' => 'member',
6176 ],
6177 [
6178 'label' => 'Community Moderator',
6179 'value' => 'moderator',
6180 ],
6181 [
6182 'label' => 'Community Administrator',
6183 'value' => 'admin',
6184 ],
6185 [
6186 'label' => 'Banned',
6187 'value' => 'ban',
6188 ],
6189 [
6190 'label' => 'Pending user email verification',
6191 'value' => 'register',
6192 ],
6193 [
6194 'label' => 'Pending admin approval',
6195 'value' => 'verified',
6196 ],
6197 ];
6198 return [
6199 'options' => $options,
6200 'hasMore' => false,
6201 ];
6202 }
6203
6204 /**
6205 * Search Peepso Users list.
6206 *
6207 * @param array $data data.
6208 * @return array
6209 */
6210 public function search_peepso_follow_user_list( $data ) {
6211 $options = [];
6212 global $wpdb;
6213 $users = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}peepso_users", ARRAY_A );
6214 if ( count( $users ) > 0 ) {
6215 foreach ( $users as $user ) {
6216 $user_by_id = get_user_by( 'id', $user['usr_id'] );
6217 if ( $user_by_id instanceof \WP_User ) {
6218 $options[] = [
6219 'label' => sprintf( '%s %s [%s]', $user_by_id->last_name, $user_by_id->first_name, $user_by_id->user_email ),
6220 'value' => $user['usr_id'],
6221 ];
6222 } else {
6223 $options[] = [
6224 'label' => '#' . $user['usr_id'],
6225 'value' => $user['usr_id'],
6226 ];
6227 }
6228 }
6229 }
6230 return [
6231 'options' => $options,
6232 'hasMore' => false,
6233 ];
6234 }
6235
6236 /**
6237 * Get last data for trigger
6238 *
6239 * @param array $data data.
6240 * @return array
6241 */
6242 public function search_restrict_content_pro_triggers_last_data( $data ) {
6243 $context = [];
6244 if ( ! function_exists( 'rcp_get_memberships' ) ) {
6245 return [];
6246 }
6247 $context['response_type'] = 'sample';
6248
6249 $user_data = WordPress::get_sample_user_context();
6250
6251 $membership_data = [
6252 'membership_level_id' => '190',
6253 'membership_level' => 'Sample Membership',
6254 'membership_initial_payment' => '0.00',
6255 'membership_recurring_payment' => '0.00',
6256 'membership_expiry_date' => 'January 22, 2023',
6257 ];
6258
6259 $customer_id = (int) ( isset( $data['filter']['membership_customer_id']['value'] ) ? $data['filter']['membership_customer_id']['value'] : '-1' );
6260 $membership_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
6261
6262 $args = [];
6263 if ( 'membership_purchased' === $data['search_term'] ) {
6264 $args = [
6265 'status' => 'active',
6266 'number' => 1,
6267 'orderby' => 'id',
6268 ];
6269 } elseif ( 'membership_cancelled' === $data['search_term'] ) {
6270 $args = [
6271 'status' => 'cancelled',
6272 'number' => 1,
6273 'orderby' => 'id',
6274 ];
6275 } elseif ( 'membership_expired' === $data['search_term'] ) {
6276 $args = [
6277 'status' => 'expired',
6278 'number' => 1,
6279 'orderby' => 'id',
6280 ];
6281 }
6282
6283 if ( 'membership_expired' === $data['search_term'] && -1 !== $customer_id ) {
6284 $args['customer_id'] = $customer_id;
6285 }
6286
6287 if ( -1 !== $membership_id ) {
6288 $args['object_id'] = $membership_id;
6289 }
6290
6291 $memberships = rcp_get_memberships( $args );
6292 if ( count( $memberships ) > 0 ) {
6293 $membership_data = RestrictContent::get_rcp_membership_detail_context( $memberships[0] );
6294 $user_data = WordPress::get_user_context( $memberships[0]->get_user_id() );
6295
6296 $context['response_type'] = 'live';
6297 }
6298
6299 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
6300
6301 return $context;
6302 }
6303
6304 /**
6305 * Get last data for trigger
6306 *
6307 * @param array $data data.
6308 * @return array
6309 */
6310 public function search_events_calendar_triggers_last_data( $data ) {
6311 $context = [];
6312 $context['response_type'] = 'sample';
6313
6314 $event_data = [
6315 'event_id' => 1,
6316 'event' => [
6317 'ID' => 58,
6318 'post_author' => 1,
6319 'post_date' => '2023-01-19 09:27:58',
6320 'post_date_gmt' => '2023-01-19 09:27:58',
6321 'post_content' => '',
6322 'post_title' => 'New event',
6323 'post_excerpt' => '',
6324 'post_status' => 'publish',
6325 'comment_status' => 'open',
6326 'ping_status' => 'closed',
6327 'post_password' => '',
6328 'post_name' => 'new-event',
6329 'to_ping' => '',
6330 'pinged' => '',
6331 'post_modified' => '2023-01-19 09:44:25',
6332 'post_modified_gmt' => '2023-01-19 09:44:25',
6333 'post_content_filtered' => '',
6334 'post_parent' => 0,
6335 'guid' => 'http://connector.com/?post_type=tribe_events&#038;p=58',
6336 'menu_order' => -1,
6337 'post_type' => 'tribe_events',
6338 'post_mime_type' => '',
6339 'comment_count' => 0,
6340 'filter' => 'raw',
6341 ],
6342 'attendies' => [
6343 'order_id' => 68,
6344 'purchaser_name' => 'John Doe',
6345 'purchaser_email' => 'john@test.com',
6346 'provider' => 'Tribe__Tickets__RSVP',
6347 'provider_slug' => 'rsvp',
6348 'purchase_time' => '2023-01-19 09:48:43',
6349 'optout' => 1,
6350 'ticket' => 'Prime',
6351 'attendee_id' => 68,
6352 'security' => '2cefc3b53e',
6353 'product_id' => 65,
6354 'check_in' => '',
6355 'order_status' => 'yes',
6356 'order_status_label' => 'Going',
6357 'user_id' => 1,
6358 'ticket_sent' => 1,
6359 'event_id' => 58,
6360 'ticket_name' => 'Prime',
6361 'holder_name' => 'John Doe',
6362 'holder_email' => 'john@test.com',
6363 'ticket_id' => 68,
6364 'qr_ticket_id' => 68,
6365 'security_code' => '2cefc3b53e',
6366 'attendee_meta' => '',
6367 'is_subscribed' => '',
6368 'is_purchaser' => 1,
6369 'ticket_exists' => 1,
6370 ],
6371 ];
6372
6373 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
6374 $term = $data['search_term'];
6375
6376 if ( 'event_register' === $term || 'attendee_registered_event' === $term ) {
6377 if ( -1 === $event_id ) {
6378 $args = [
6379 'numberposts' => 1,
6380 'orderby' => 'rand',
6381 'post_type' => 'tribe_events',
6382 ];
6383 $posts = get_posts( $args );
6384 $event_id = $posts[0]->ID;
6385 }
6386 $args = [
6387 'post_type' => 'tribe_rsvp_attendees',
6388 'orderby' => 'ID',
6389 'order' => 'DESC',
6390 'post_status' => 'publish',
6391 'numberposts' => 1,
6392 ];
6393
6394 if ( -1 !== $event_id ) {
6395 $args['meta_query'] = [
6396 [
6397 'key' => '_tribe_rsvp_event',
6398 'value' => $event_id,
6399 ],
6400 ];
6401 }
6402
6403 $attendees = get_posts( $args );
6404
6405 if ( count( $attendees ) > 0 ) {
6406 $attendee = $attendees[0];
6407 $attendee_id = $attendee->ID;
6408
6409 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
6410 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
6411 if ( is_numeric( $product_id ) && is_numeric( $order_id ) ) {
6412 $event_context = TheEventCalendar::get_event_context( (int) $product_id, (int) $order_id );
6413 }
6414
6415 if ( ! empty( $event_context ) ) {
6416 $event_data = $event_context;
6417 $context['response_type'] = 'live';
6418 }
6419 } else {
6420 $args = [
6421 'post_type' => 'tec_tc_attendee',
6422 'orderby' => 'ID',
6423 'order' => 'DESC',
6424 'post_status' => 'publish',
6425 'numberposts' => 1,
6426 ];
6427 if ( -1 !== $event_id ) {
6428 $args['meta_query'] = [
6429 [
6430 'key' => '_tec_tickets_commerce_event',
6431 'value' => $event_id,
6432 ],
6433 ];
6434 }
6435 $attendees = get_posts( $args );
6436 $attendee = $attendees[0];
6437 $attendee_id = $attendee->ID;
6438 $product_id = get_post_meta( $attendee_id, '_tec_tickets_commerce_ticket', true );
6439 $order_id = $attendee_id;
6440 if ( is_numeric( $product_id ) ) {
6441 $event_context = TheEventCalendar::get_event_context( (int) $product_id, $order_id );
6442 }
6443 if ( ! empty( $event_context ) ) {
6444 $event_data = $event_context;
6445 $context['response_type'] = 'live';
6446 }
6447 }
6448 } elseif ( 'event_attends' === $term ) {
6449 if ( -1 == $event_id ) {
6450 $args = [
6451 'numberposts' => 1,
6452 'orderby' => 'rand',
6453 'post_type' => 'tribe_events',
6454 ];
6455 $posts = get_posts( $args );
6456 $event_id = $posts[0]->ID;
6457 }
6458 $args = [
6459 'post_type' => 'tribe_rsvp_attendees',
6460 'orderby' => 'ID',
6461 'order' => 'DESC',
6462 'post_status' => 'publish',
6463 'numberposts' => 1,
6464 'meta_query' => [
6465 'relation' => 'AND',
6466 [
6467 'key' => '_tribe_rsvp_checkedin',
6468 'value' => 1,
6469 'compare' => '=',
6470 ],
6471 [
6472 'key' => '_tribe_rsvp_event',
6473 'value' => $event_id,
6474 'compare' => '=',
6475 ],
6476 ],
6477 ];
6478 $attendees = get_posts( $args );
6479 if ( ! function_exists( 'tribe_tickets_get_attendees' ) ) {
6480 return [];
6481 }
6482 if ( ! empty( $attendees ) ) {
6483 $attendee = $attendees[0];
6484 $attendee_id = $attendee->ID;
6485 /**
6486 *
6487 * Ignore line
6488 *
6489 * @phpstan-ignore-next-line
6490 */
6491 $attendee_details = tribe_tickets_get_attendees( $attendee_id, 'rsvp_order' );
6492 foreach ( $attendee_details as $detail ) {
6493 if ( (int) $detail['attendee_id'] !== (int) $attendee_id ) {
6494 continue;
6495 }
6496 $attendee = $detail;
6497 }
6498 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
6499 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
6500 if ( is_numeric( $product_id ) && is_numeric( $order_id ) ) {
6501 $event_context = TheEventCalendar::get_event_context( (int) $product_id, (int) $order_id );
6502 }
6503 if ( ! empty( $event_context ) ) {
6504 $event_data = array_merge( $attendee, $event_context );
6505 $context['response_type'] = 'live';
6506 }
6507 } else {
6508 $order = [
6509 'order_id' => 7962,
6510 'purchaser_name' => 'bella4 bella4',
6511 'purchaser_email' => 'bella4@yopmail.com',
6512 'provider' => 'Tribe__Tickets__RSVP',
6513 'provider_slug' => 'rsvp',
6514 'purchase_time' => '2024 - 03 - 04 07:26:41',
6515 'optout' => 1,
6516 'ticket' => 'test test',
6517 'attendee_id' => 7962,
6518 'security' => 'eb3a2d7bc4',
6519 'product_id' => 7959,
6520 'check_in' => 1,
6521 'order_status' => 'yes',
6522 'order_status_label' => 'Going',
6523 'user_id' => 35,
6524 'ticket_sent' => 1,
6525 'event_id' => 7956,
6526 'ticket_name' => 'test test',
6527 'holder_name' => 'bella4 bella4',
6528 'holder_email' => 'bella4@yopmail.com',
6529 'ticket_id' => 7962,
6530 'qr_ticket_id' => 7962,
6531 'security_code' => 'eb3a2d7bc4',
6532 'is_purchaser' => 1,
6533 'ticket_exists' => 1,
6534 ];
6535 $event_data = array_merge( $order, $event_data );
6536 }
6537 } elseif ( 'event_attendee_wc_register' === $term ) {
6538 if ( ! class_exists( 'Tribe__Tickets__Tickets' ) ) {
6539 return [];
6540 }
6541 if ( -1 === $event_id ) {
6542 $args = [
6543 'numberposts' => 1,
6544 'orderby' => 'rand',
6545 'post_type' => 'tribe_events',
6546 ];
6547 $posts = get_posts( $args );
6548 $event_id = $posts[0]->ID;
6549 }
6550 $event_ticket_id = (int) ( isset( $data['filter']['event_ticket_id']['value'] ) ? $data['filter']['event_ticket_id']['value'] : '-1' );
6551 if ( -1 === $event_ticket_id ) {
6552 $tickets = \Tribe__Tickets__Tickets::get_all_event_tickets( $event_id );
6553 $random_key = array_rand( $tickets );
6554 $random_value = $tickets[ $random_key ];
6555 $event_ticket_id = $random_value;
6556 }
6557 $args = [
6558 'post_type' => 'tribe_wooticket',
6559 'orderby' => 'ID',
6560 'order' => 'DESC',
6561 'post_status' => 'publish',
6562 'meta_query' => [
6563 'relation' => 'AND',
6564 [
6565 'key' => '_tribe_wooticket_event',
6566 'value' => $event_id,
6567 'compare' => '=',
6568 ],
6569 [
6570 'key' => '_tribe_wooticket_product',
6571 'value' => $event_ticket_id,
6572 'compare' => '=',
6573 ],
6574 ],
6575 ];
6576
6577 $woo_attendees = get_posts( $args );
6578 if ( ! empty( $woo_attendees ) ) {
6579 $attendee = $woo_attendees[0];
6580 $attendee_id = $attendee->ID;
6581 /**
6582 *
6583 * Ignore line
6584 *
6585 * @phpstan-ignore-next-line
6586 */
6587 $attendee_details = tribe_tickets_get_attendees( $attendee_id, 'rsvp_order' );
6588 foreach ( $attendee_details as $detail ) {
6589 if ( (int) $detail['attendee_id'] !== (int) $attendee_id ) {
6590 continue;
6591 }
6592 $attendee = $detail;
6593 }
6594 $order_id = get_post_meta( $attendee_id, '_tribe_wooticket_order', true );
6595 $product_id = get_post_meta( $attendee_id, '_tribe_wooticket_product', true );
6596 if ( is_numeric( $product_id ) && is_numeric( $order_id ) ) {
6597 $event_context = TheEventCalendar::get_event_context( (int) $product_id, (int) $order_id );
6598 }
6599 if ( ! empty( $event_context ) ) {
6600 $event_data = $event_context;
6601 $context['response_type'] = 'live';
6602 }
6603 $context['response_type'] = 'live';
6604 }
6605 }
6606
6607 $context['pluggable_data'] = $event_data;
6608
6609 return $context;
6610 }
6611
6612 /**
6613 * Get last data for trigger
6614 *
6615 * @param array $data data.
6616 * @return array
6617 */
6618 public function search_woo_commerce_triggers_last_data( $data ) {
6619 $context = [];
6620 $context['response_type'] = 'sample';
6621 $context['pluggable_data'] = [];
6622 $user_data = WordPress::get_sample_user_context();
6623
6624 $product_data['product'] = [
6625 'id' => '169',
6626 'name' => 'Sample Product',
6627 'description' => 'This is description of sample product.',
6628 'short_description' => 'This is short description of sample product.',
6629 'image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
6630 'slug' => 'sample-product',
6631 'status' => 'publish',
6632 'type' => 'simple',
6633 'price' => '89',
6634 'featured' => '0',
6635 'sku' => 'hoodie-blue-sm',
6636 'regular_price' => '90',
6637 'sale_price' => '89',
6638 'total_sales' => '21',
6639 'category' => 'Uncategorized',
6640 'tags' => 'sample, new, 2022',
6641 ];
6642
6643 $comment_data = [
6644 'comment_id' => '1',
6645 'comment' => 'This is a sample comment..!',
6646 'comment_author' => 'testsure',
6647 'comment_date' => '2023-06-23 10:10:40',
6648 'comment_author_email' => 'testsure@example.com',
6649 ];
6650
6651 $order_data = [
6652 'order_id' => '500',
6653 'total_order_value' => '45',
6654 'currency' => 'USD',
6655 'shipping_total' => '5',
6656 'order_payment_method' => 'cod',
6657 'billing_firstname' => 'John',
6658 'billing_lastname' => 'Doe',
6659 'billing_company' => 'BSF',
6660 'billing_address_1' => '1004 Beaumont',
6661 'billing_address_2' => '',
6662 'billing_city' => 'Casper',
6663 'billing_state' => 'Wyoming',
6664 'billing_state_name' => 'Wyoming',
6665 'billing_postcode' => '82601',
6666 'billing_country' => 'US',
6667 'billing_country_name' => 'US',
6668 'billing_email' => 'john_doe@gmail.com',
6669 'billing_phone' => '(307) 7626541',
6670 'shipping_firstname' => 'John',
6671 'shipping_lastname' => 'Doe',
6672 'shipping_company' => 'BSF',
6673 'shipping_address_1' => '1004 Beaumont',
6674 'shipping_address_2' => '',
6675 'shipping_city' => 'Casper',
6676 'shipping_state' => 'Wyoming',
6677 'shipping_state_name' => 'Wyoming',
6678 'shipping_postcode' => '82601',
6679 'shipping_country' => 'US',
6680 'shipping_country_name' => 'US',
6681 'coupon_codes' => 'e3mstekq, f24sjakb',
6682 'total_items_in_order' => '1',
6683 'user_id' => '1',
6684 ];
6685
6686 $variation_data = [
6687 'product_variation_id' => '626',
6688 'product_variation' => 'Color: Silver',
6689 ];
6690
6691 $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
6692
6693 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
6694 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
6695
6696 $order_status = ( isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1 );
6697 $from_order_status = ( isset( $data['filter']['from_status']['value'] ) ? $data['filter']['from_status']['value'] : -1 );
6698 $order_note_type = ( isset( $data['filter']['note_type']['value'] ) ? $data['filter']['note_type']['value'] : -1 );
6699
6700 if ( in_array( $term, [ 'product_added_to_cart', 'product_viewed' ], true ) ) {
6701 if ( -1 === $product_id ) {
6702 $args = [
6703 'post_type' => 'product',
6704 'orderby' => 'ID',
6705 'order' => 'DESC',
6706 'post_status' => 'publish',
6707 'numberposts' => 1,
6708 ];
6709 $products = get_posts( $args );
6710
6711 if ( count( $products ) > 0 ) {
6712 $product_id = $products[0]->ID;
6713 }
6714 }
6715
6716 if ( -1 !== $product_id ) {
6717 $post = get_post( $product_id );
6718 if ( $post instanceof \WP_Post ) {
6719 $user_data = WordPress::get_user_context( (int) $post->post_author );
6720 }
6721 $product_data['product_id'] = $product_id;
6722 $product_data['product'] = WooCommerce::get_product_context( $product_id );
6723 $terms = get_the_terms( $product_id, 'product_cat' );
6724 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6725 $cat_name = [];
6726 foreach ( $terms as $cat ) {
6727 $cat_name[] = $cat->name;
6728 }
6729 $product_data['product']['category'] = implode( ', ', $cat_name );
6730 }
6731 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6732 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6733 $tag_name = [];
6734 foreach ( $terms_tags as $tag ) {
6735 $tag_name[] = $tag->name;
6736 }
6737 $product_data['product']['tag'] = implode( ', ', $tag_name );
6738 }
6739 unset( $product_data['product']['id'] ); //phpcs:ignore
6740 $context['response_type'] = 'live';
6741 }
6742
6743 if ( 'product_added_to_cart' === $term ) {
6744 $product_data['product_quantity'] = 1;
6745 }
6746
6747 $context['pluggable_data'] = array_merge( $product_data, $user_data );
6748
6749 } elseif ( 'product_reviewed' === $term ) {
6750 $comment_args = [
6751 'number' => 1,
6752 'type' => 'review',
6753 'orderby' => 'comment_ID',
6754 'post_id' => -1 !== $product_id ? $product_id : 0,
6755 ];
6756
6757 $comments = get_comments( $comment_args );
6758
6759 if ( is_array( $comments ) && count( $comments ) > 0 ) {
6760 $comment = $comments[0];
6761 if ( is_object( $comment ) ) {
6762 $comment_data = [
6763 'comment_id' => $comment->comment_ID,
6764 'comment' => $comment->comment_content,
6765 'comment_author' => $comment->comment_author,
6766 'comment_date' => $comment->comment_date,
6767 'comment_author_email' => $comment->comment_author_email,
6768 ];
6769 $product_data = WooCommerce::get_product_context( (int) $comment->comment_post_ID );
6770 $terms = get_the_terms( (int) $comment->comment_post_ID, 'product_cat' );
6771 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6772 $cat_name = [];
6773 foreach ( $terms as $cat ) {
6774 $cat_name[] = $cat->name;
6775 }
6776 $product_data['product']['category'] = implode( ', ', $cat_name );
6777 }
6778 $terms_tags = get_the_terms( (int) $comment->comment_post_ID, 'product_tag' );
6779 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6780 $tag_name = [];
6781 foreach ( $terms_tags as $tag ) {
6782 $tag_name[] = $tag->name;
6783 }
6784 $product_data['product']['tag'] = implode( ', ', $tag_name );
6785 }
6786 }
6787 if ( $comment instanceof \WP_Comment ) {
6788 $user_data = WordPress::get_user_context( (int) $comment->user_id );
6789 }
6790 $context['response_type'] = 'live';
6791 }
6792
6793 $context['pluggable_data'] = array_merge( $product_data, $user_data, $comment_data );
6794
6795 } elseif ( 'product_purchased' === $term ) {
6796 $order_id = 0;
6797 $product_data['quantity'] = '1';
6798 if ( -1 !== $product_id ) {
6799 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
6800 if ( count( $order_ids ) > 0 ) {
6801 $order_id = $order_ids[0];
6802 }
6803 } else {
6804 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
6805 if ( is_array( $orders ) && count( $orders ) > 0 ) {
6806 $order_id = $orders[0]->get_id();
6807 }
6808 }
6809
6810 if ( 0 !== $order_id ) {
6811 $order = wc_get_order( $order_id );
6812
6813 if ( $order && $order instanceof \WC_Order ) {
6814 $user_id = $order->get_customer_id();
6815 $items = $order->get_items();
6816
6817 $product_ids = [];
6818
6819 $iteration = 0;
6820 foreach ( $items as $item ) {
6821 if ( method_exists( $item, 'get_product_id' ) ) {
6822 $item_id = $item->get_product_id();
6823 if ( -1 === $product_id && 0 === $iteration ) {
6824 $product_ids[] = $item_id;
6825 break;
6826 } elseif ( $item_id === $product_id ) {
6827 $product_ids[] = $item_id;
6828 break;
6829 }
6830 }
6831
6832 $iteration++;
6833 }
6834 $order_data = WooCommerce::get_order_context( $order_id );
6835 $user_data = WordPress::get_user_context( $user_id );
6836 $order_data['total_items_in_order'] = count( $product_ids );
6837 $product_data = [];
6838 foreach ( $product_ids as $key => $product_id ) {
6839 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
6840 $terms = get_the_terms( $product_id, 'product_cat' );
6841 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6842 $cat_name = [];
6843 foreach ( $terms as $cat ) {
6844 $cat_name[] = $cat->name;
6845 }
6846 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
6847 }
6848 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6849 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6850 $tag_name = [];
6851 foreach ( $terms_tags as $tag ) {
6852 $tag_name[] = $tag->name;
6853 }
6854 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
6855 }
6856 $product = wc_get_product( $product_id );
6857 /**
6858 *
6859 * Ignore line
6860 *
6861 * @phpstan-ignore-next-line
6862 */
6863 if ( $product->is_downloadable() ) {
6864 /**
6865 *
6866 * Ignore line
6867 *
6868 * @phpstan-ignore-next-line
6869 */
6870 foreach ( $product->get_downloads() as $key_download_id => $download ) {
6871 $download_name = $download->get_name();
6872 $download_link = $download->get_file();
6873 $download_id = $download->get_id();
6874 $download_type = $download->get_file_type();
6875 $download_ext = $download->get_file_extension();
6876 $product_data[ 'product' . $key ]['download'] = [
6877 'download_name' => $download_name,
6878 'download_link' => $download_link,
6879 'download_id' => $download_id,
6880 'download_type' => $download_type,
6881 'download_ext' => $download_ext,
6882 ];
6883 }
6884 }
6885 }
6886 $context['response_type'] = 'live';
6887 }
6888 }
6889
6890 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
6891
6892 } elseif ( 'variable_product_purchased' === $term ) {
6893 $product_variation_id = (int) ( isset( $data['filter']['product_variation_id']['value'] ) ? $data['filter']['product_variation_id']['value'] : -1 );
6894 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
6895
6896 foreach ( $order_ids as $order_id ) {
6897 $order = wc_get_order( $order_id );
6898
6899 if ( $order && $order instanceof \WC_Order ) {
6900 $user_id = $order->get_customer_id();
6901 $items = $order->get_items();
6902 $product_variations = [];
6903
6904 $iteration = 0;
6905 foreach ( $items as $item ) {
6906 if ( method_exists( $item, 'get_variation_id' ) ) {
6907 $variation_id = $item->get_variation_id();
6908 if ( -1 === $product_variation_id && 0 === $iteration ) {
6909 $product_variations[] = $variation_id;
6910 break;
6911 } elseif ( $variation_id === $product_variation_id ) {
6912 $product_variations[] = $variation_id;
6913 break;
6914 }
6915 }
6916
6917 $iteration++;
6918 }
6919
6920 if ( count( $product_variations ) > 0 ) {
6921 $product_data = WooCommerce::get_product_context( $product_variation_id );
6922 $order_data = WooCommerce::get_order_context( $order_id );
6923 $user_data = WordPress::get_user_context( $user_id );
6924 $variation_data = [
6925 'product_variation_id' => $product_variations[0],
6926 'product_variation' => get_the_excerpt( $product_variations[0] ),
6927 ];
6928
6929 $context['response_type'] = 'live';
6930 break;
6931 }
6932 }
6933 }
6934
6935 $context['pluggable_data'] = array_merge(
6936 isset( $order_data ) ? $order_data : [],
6937 $user_data,
6938 $variation_data
6939 );
6940
6941 } elseif ( 'variable_subscription_purchased' === $term ) {
6942 $product_data['quantity'] = '1';
6943 $product_data['product_name'] = 'Sample Product';
6944 $product_data['billing_period'] = '2021-2022';
6945
6946 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
6947
6948 $subscription_order_id = 0;
6949 $order_ids = [];
6950
6951 if ( -1 !== $product_id ) {
6952 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
6953
6954 } else {
6955 $orders = wc_get_orders( [] );
6956 if ( is_array( $orders ) && count( $orders ) > 0 ) {
6957 $order_ids[] = $orders[0]->get_id();
6958 }
6959 }
6960
6961 foreach ( $order_ids as $order_id ) {
6962 $query_args = [
6963 'post_type' => 'shop_subscription',
6964 'orderby' => 'ID',
6965 'order' => 'DESC',
6966 'post_status' => 'wc-active',
6967 'posts_per_page' => 1,
6968 'post_parent' => $order_id,
6969 ];
6970 $query_result = new WP_Query( $query_args );
6971 $subscription_orders = $query_result->get_posts();
6972
6973 if ( count( $subscription_orders ) > 0 ) {
6974 if ( $subscription_orders[0] instanceof \WP_Post ) {
6975 $subscription_order_id = $subscription_orders[0]->ID;
6976 }
6977 break;
6978 }
6979 }
6980
6981 if ( 0 !== $subscription_order_id ) {
6982 if ( function_exists( 'wcs_get_subscription' ) ) {
6983 $subscription = wcs_get_subscription( $subscription_order_id );
6984 if ( class_exists( 'WC_Subscription' ) && $subscription instanceof WC_Subscription ) {
6985 $last_order_id = $subscription->get_last_order();
6986 if ( ! empty( $last_order_id ) && $last_order_id === $subscription->get_parent_id() ) {
6987 $last_order_data = wc_get_order( $last_order_id );
6988 if ( $last_order_data instanceof \WC_Order ) {
6989 $user_id = $last_order_data->get_customer_id();
6990 }
6991 $items = $subscription->get_items();
6992
6993 foreach ( $items as $item ) {
6994 $product = $item->get_product();
6995 if ( class_exists( '\WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product ) ) {
6996 if ( $product->is_type( [ 'subscription', 'subscription_variation', 'variable-subscription' ] ) ) {
6997
6998 $product_data = WooCommerce::get_variable_subscription_product_context( $item, $last_order_id );
6999 if ( isset( $user_id ) ) {
7000 $user_data = WordPress::get_user_context( $user_id );
7001 }
7002
7003 $context['response_type'] = 'live';
7004 $context['pluggable_data'] = array_merge( $product_data, $user_data );
7005 }
7006 }
7007 }
7008 }
7009 }
7010 }
7011 }
7012 } elseif ( 'order_created' === $term ) {
7013 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
7014 $order_id = '';
7015 if ( is_array( $orders ) && count( $orders ) > 0 ) {
7016 $order_id = $orders[0]->get_id();
7017 $order = wc_get_order( $order_id );
7018 if ( $order instanceof \WC_Order ) {
7019 $user_id = $order->get_customer_id();
7020 $order_context = WooCommerce::get_order_context( $order_id );
7021 $order_sample_data = array_merge(
7022 isset( $order_context ) ? $order_context : [],
7023 WordPress::get_user_context( $user_id )
7024 );
7025 $context['response_type'] = 'live';
7026 }
7027 }
7028
7029 // Add dummy coupon details if no coupons exist in the order.
7030 if ( ! isset( $order_sample_data ) ) {
7031 $order_sample_data = [];
7032 }
7033 if ( ! isset( $order_sample_data['coupons'] ) || empty( $order_sample_data['coupons'] ) ) {
7034 $order_sample_data['coupons'] = [
7035 [
7036 'code_name' => 'SAMPLE10',
7037 'discount_type' => 'percent',
7038 'coupon_amount' => '10',
7039 'meta_data' => [],
7040 ],
7041 [
7042 'code_name' => 'SAMPLE20',
7043 'discount_type' => 'fixed_cart',
7044 'coupon_amount' => '20',
7045 'meta_data' => [],
7046 ],
7047 ];
7048 }
7049
7050 $context['pluggable_data'] = $order_sample_data;
7051
7052 } elseif ( 'order_status_changed' === $term ) {
7053 if ( -1 == $order_status ) {
7054 $args = [
7055 'numberposts' => 1,
7056 'orderby' => 'date',
7057 'order' => 'DESC',
7058 ];
7059 } else {
7060 $args = [
7061 'status' => [ $order_status ],
7062 'numberposts' => 1,
7063 'orderby' => 'date',
7064 'order' => 'DESC',
7065 ];
7066 }
7067 $orders = wc_get_orders( $args );
7068 $order_id = '';
7069 if ( is_array( $orders ) && count( $orders ) > 0 ) {
7070 $order_id = $orders[0]->get_id();
7071 $order = wc_get_order( $order_id );
7072 if ( ! empty( $order ) && $order instanceof \WC_Order ) {
7073 $user_id = $order->get_customer_id();
7074 $items = $order->get_items();
7075 $product_ids = [];
7076 foreach ( $items as $item ) {
7077 $product_ids[] = $item['product_id'];
7078 }
7079 $product_data = [];
7080 foreach ( $product_ids as $key => $product_id ) {
7081 /**
7082 *
7083 * Ignore line
7084 *
7085 * @phpstan-ignore-next-line
7086 */
7087 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
7088 /**
7089 *
7090 * Ignore line
7091 *
7092 * @phpstan-ignore-next-line
7093 */
7094 $terms = get_the_terms( $product_id, 'product_cat' );
7095 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
7096 $cat_name = [];
7097 foreach ( $terms as $cat ) {
7098 $cat_name[] = $cat->name;
7099 }
7100 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
7101 }
7102 /**
7103 *
7104 * Ignore line
7105 *
7106 * @phpstan-ignore-next-line
7107 */
7108 $terms_tags = get_the_terms( $product_id, 'product_tag' );
7109 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
7110 $tag_name = [];
7111 foreach ( $terms_tags as $tag ) {
7112 $tag_name[] = $tag->name;
7113 }
7114 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
7115 }
7116 }
7117 $order_context = WooCommerce::get_order_context( $order_id );
7118 $order_sample_data = array_merge(
7119 isset( $order_context ) ? $order_context : [],
7120 $product_data
7121 );
7122 $order_sample_data['user'] = WordPress::get_user_context( $user_id );
7123 $order_sample_data['to_status'] = $order_status;
7124 $order_sample_data['from_status'] = $from_order_status;
7125 $context['response_type'] = 'live';
7126 }
7127 }
7128
7129 $order_sample_data['to_status'] = $order_status;
7130 $order_sample_data['from_status'] = $from_order_status;
7131 $context['pluggable_data'] = $order_sample_data;
7132 } elseif ( 'order_note_added' === $term ) {
7133 global $wpdb;
7134 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type ='order_note' ORDER BY comment_ID DESC LIMIT 1" );
7135 $order_id = '';
7136 if ( ! empty( $result ) ) {
7137 $order_id = $result[0]->comment_post_ID;
7138 $order = wc_get_order( $order_id );
7139 if ( ! empty( $order ) && $order instanceof \WC_Order ) {
7140 $user_id = $order->get_customer_id();
7141 $order_context = WooCommerce::get_order_context( $order_id );
7142 $order_sample_data = array_merge(
7143 isset( $order_context ) ? $order_context : [],
7144 WordPress::get_user_context( $user_id )
7145 );
7146 if ( -1 == $order_note_type ) {
7147 $args = [
7148 'order_id' => $order_id,
7149 'limit' => 1,
7150 ];
7151 } else {
7152 $args = [
7153 'order_id' => $order_id,
7154 'type' => $order_note_type,
7155 'limit' => 1,
7156 ];
7157 }
7158
7159 $notes = wc_get_order_notes( $args );
7160 if ( ! empty( $notes ) ) {
7161 $order_sample_data['note'] = [
7162 'id' => $notes[0]->id,
7163 'date' => $notes[0]->date_created,
7164 'author' => $notes[0]->added_by,
7165 'content' => $notes[0]->content,
7166 ];
7167 } else {
7168 $order_sample_data['note'] = [
7169 'id' => '1',
7170 'date' => [
7171 'date' => '2023-06-23 10:10:40',
7172 'timezone_type' => 1,
7173 'timezone' => '+00:00',
7174 ],
7175 'author' => 'admin',
7176 'content' => 'new note',
7177 ];
7178 }
7179 $order_sample_data['note_type'] = $order_note_type;
7180 $context['response_type'] = 'live';
7181 }
7182 } else {
7183 $order_sample_data['note'] = [
7184 'id' => '1',
7185 'date' => [
7186 'date' => '2023-06-23 10:10:40',
7187 'timezone_type' => 1,
7188 'timezone' => '+00:00',
7189 ],
7190 'author' => 'admin',
7191 'content' => 'new note',
7192 ];
7193 $order_sample_data['note_type'] = 'customer';
7194 }
7195 $context['pluggable_data'] = $order_sample_data;
7196 } elseif ( 'order_paid' === $term ) {
7197 $args = [
7198 'status' => [ 'completed' ],
7199 'numberposts' => 1,
7200 ];
7201 $orders = wc_get_orders( $args );
7202 $order_id = '';
7203 if ( is_array( $orders ) && count( $orders ) > 0 ) {
7204 $order_id = $orders[0]->get_id();
7205 $order = wc_get_order( $order_id );
7206 if ( $order instanceof \WC_Order ) {
7207 $user_id = $order->get_customer_id();
7208 $order_context = WooCommerce::get_order_context( $order_id );
7209 $order_sample_data = array_merge(
7210 isset( $order_context ) ? $order_context : [],
7211 WordPress::get_user_context( $user_id )
7212 );
7213 $context['response_type'] = 'live';
7214 }
7215 }
7216 $context['pluggable_data'] = $order_sample_data;
7217 } elseif ( 'product_category_purchased' === $term ) {
7218 $product_category_id = (int) ( isset( $data['filter']['product_category_id']['value'] ) ? $data['filter']['product_category_id']['value'] : -1 );
7219 $args = [
7220 'post_status' => 'publish',
7221 'product_category_id' => [ $product_category_id ],
7222 'return' => 'ids',
7223 ];
7224 $products = wc_get_products( $args );
7225 if ( ! empty( $products ) ) {
7226 $order_id = 0;
7227 $product_data['quantity'] = '1';
7228 $orders = wc_get_orders(
7229 [
7230 'status' => 'any',
7231 ]
7232 );
7233 $filtered_orders = [];
7234 if ( ! empty( $orders ) && is_array( $orders ) ) {
7235 foreach ( $orders as $order ) {
7236 $order_items = $order->get_items();
7237 foreach ( $order_items as $item ) {
7238 if ( method_exists( $item, 'get_product_id' ) ) {
7239 $product_id = $item->get_product_id();
7240 }
7241 if ( is_array( $products ) && in_array( $product_id, $products ) ) {
7242 $filtered_orders[] = $order;
7243 break;
7244 }
7245 }
7246 }
7247 }
7248 if ( ! empty( $filtered_orders ) ) {
7249 if ( count( $filtered_orders ) > 0 ) {
7250 $order_id = $filtered_orders[0]->get_id();
7251 }
7252 if ( 0 !== $order_id ) {
7253 $order = wc_get_order( $order_id );
7254 if ( $order instanceof WC_Order ) {
7255 $user_id = $order->get_customer_id();
7256 $items = $order->get_items();
7257 $product_ids = [];
7258 $iteration = 0;
7259 foreach ( $items as $item ) {
7260 if ( method_exists( $item, 'get_product_id' ) ) {
7261 $item_id = $item->get_product_id();
7262 if ( -1 === $product_id && 0 === $iteration ) {
7263 $product_ids[] = $item_id;
7264 break;
7265 } elseif ( $item_id === $product_id ) {
7266 $product_ids[] = $item_id;
7267 break;
7268 }
7269 }
7270 $iteration++;
7271 }
7272 $order_data = WooCommerce::get_order_context( $order_id );
7273 $user_data = WordPress::get_user_context( $user_id );
7274 $order_data['total_items_in_order'] = count( $product_ids );
7275 $product_data = [];
7276 $category_ids = [];
7277 foreach ( $product_ids as $key => $product_id ) {
7278 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
7279 $terms = get_the_terms( $product_id, 'product_cat' );
7280 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
7281 $cat_name = [];
7282 foreach ( $terms as $cat ) {
7283 $cat_name[] = $cat->name;
7284 $category_ids[] = $cat->term_id;
7285 }
7286 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
7287 }
7288 $terms_tags = get_the_terms( $product_id, 'product_tag' );
7289 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
7290 $tag_name = [];
7291 foreach ( $terms_tags as $tag ) {
7292 $tag_name[] = $tag->name;
7293 }
7294 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
7295 }
7296 $product = wc_get_product( $product_id );
7297 /**
7298 *
7299 * Ignore line
7300 *
7301 * @phpstan-ignore-next-line
7302 */
7303 if ( $product->is_downloadable() ) {
7304 /**
7305 *
7306 * Ignore line
7307 *
7308 * @phpstan-ignore-next-line
7309 */
7310 foreach ( $product->get_downloads() as $key_download_id => $download ) {
7311 $download_name = $download->get_name();
7312 $download_link = $download->get_file();
7313 $download_id = $download->get_id();
7314 $download_type = $download->get_file_type();
7315 $download_ext = $download->get_file_extension();
7316 $product_data[ 'product' . $key ]['download'] = [
7317 'download_name' => $download_name,
7318 'download_link' => $download_link,
7319 'download_id' => $download_id,
7320 'download_type' => $download_type,
7321 'download_ext' => $download_ext,
7322 ];
7323 }
7324 }
7325 }
7326 $context['response_type'] = 'live';
7327 }
7328 }
7329 }
7330 }
7331 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
7332 if ( ! empty( $category_ids ) ) {
7333 foreach ( $category_ids as $category_id ) {
7334 $context['pluggable_data']['product_category_id'] = $category_id;
7335 }
7336 } else {
7337 $context['pluggable_data']['product_category_id'] = 1;
7338 }
7339 }
7340
7341 return $context;
7342 }
7343
7344 /**
7345 * Get last data for Jet Appointments Booking triggers.
7346 *
7347 * @param array $data data.
7348 * @return array
7349 */
7350 public function search_jet_appointment_triggers_last_data( $data ) {
7351 if ( ! class_exists( '\JET_APB\Plugin' ) ) {
7352 return [];
7353 }
7354
7355 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
7356
7357 // Sample appointment data.
7358 $appointment_data = [
7359 'ID' => 1,
7360 'group_ID' => '',
7361 'status' => 'pending',
7362 'service' => 1,
7363 'provider' => 1,
7364 'order_id' => '',
7365 'user_id' => 1,
7366 'user_name' => 'john',
7367 'user_email' => 'john@example.com',
7368 'date' => time(),
7369 'date_end' => 0,
7370 'slot' => time() - 3600,
7371 'slot_end' => time(),
7372 'type' => 'single',
7373 'appointment_date' => gmdate( 'Y-m-d H:i:s' ),
7374 'service_title' => 'Sample Service',
7375 'provider_title' => 'Sample Provider',
7376 'user_login' => 'john',
7377 'user_display_name' => 'John Doe',
7378 'date_formatted' => date_i18n( is_string( get_option( 'date_format' ) ) ? get_option( 'date_format' ) : 'Y-m-d', time() ),
7379 'slot_formatted' => date_i18n( is_string( get_option( 'time_format' ) ) ? get_option( 'time_format' ) : 'H:i', time() - 3600 ),
7380 'slot_end_formatted' => date_i18n( is_string( get_option( 'time_format' ) ) ? get_option( 'time_format' ) : 'H:i', time() ),
7381 ];
7382
7383 // Add term-specific fields based on search term.
7384 if ( 'appointment_cancelled' === $term || 'appointment_status_changed' === $term ) {
7385 $appointment_data['old_status'] = 'confirmed';
7386 $appointment_data['new_status'] = 'appointment_cancelled' === $term ? 'cancelled' : 'completed';
7387 }
7388
7389 $context = [
7390 'response_type' => 'sample',
7391 ];
7392
7393 // Try to get live data using the same method as triggers.
7394 if ( class_exists( '\JET_APB\Plugin' ) ) {
7395 $live_appointment_data = \JET_APB\Plugin::instance()->db->appointments->query( [], 1 );
7396 if ( ! empty( $live_appointment_data ) ) {
7397 $live_data = $live_appointment_data[0];
7398 $appointment_id = $live_data['ID'];
7399
7400 // Get appointment meta using the same method as triggers.
7401 $appointment_meta = $this->get_appointment_meta( $appointment_id );
7402
7403 // Merge appointment data with meta data.
7404 $appointment_data = array_merge( $live_data, $appointment_meta );
7405
7406 // Add service title.
7407 if ( ! empty( $appointment_data['service'] ) ) {
7408 $service_post = get_post( $appointment_data['service'] );
7409 if ( $service_post ) {
7410 $appointment_data['service_title'] = $service_post->post_title;
7411 }
7412 }
7413
7414 // Add provider title.
7415 if ( ! empty( $appointment_data['provider'] ) && $appointment_data['provider'] > 0 ) {
7416 $provider_post = get_post( $appointment_data['provider'] );
7417 if ( $provider_post ) {
7418 $appointment_data['provider_title'] = $provider_post->post_title;
7419 }
7420 }
7421
7422 // Add user data.
7423 if ( ! empty( $appointment_data['user_id'] ) ) {
7424 $user = get_user_by( 'ID', $appointment_data['user_id'] );
7425 if ( $user ) {
7426 $appointment_data['user_login'] = $user->user_login;
7427 $appointment_data['user_email'] = $user->user_email;
7428 $appointment_data['user_display_name'] = $user->display_name;
7429 }
7430 }
7431
7432 // Add formatted dates.
7433 $date_format = get_option( 'date_format' );
7434 $time_format = get_option( 'time_format' );
7435 $appointment_data['date_formatted'] = is_string( $date_format ) ? date_i18n( $date_format, $appointment_data['date'] ) : '';
7436 $appointment_data['slot_formatted'] = is_string( $time_format ) ? date_i18n( $time_format, $appointment_data['slot'] ) : '';
7437 $appointment_data['slot_end_formatted'] = is_string( $time_format ) ? date_i18n( $time_format, $appointment_data['slot_end'] ) : '';
7438
7439 // Add term-specific fields for live data.
7440 if ( 'appointment_cancelled' === $term || 'appointment_status_changed' === $term ) {
7441 $appointment_data['old_status'] = 'confirmed';
7442 $appointment_data['new_status'] = 'appointment_cancelled' === $term ? 'cancelled' : 'completed';
7443 }
7444
7445 $context['response_type'] = 'live';
7446 }
7447 }
7448
7449 $context['pluggable_data'] = $appointment_data;
7450 return $context;
7451 }
7452
7453 /**
7454 * Get appointment meta data.
7455 *
7456 * @param int $appointment_id Appointment ID.
7457 * @return array
7458 */
7459 private function get_appointment_meta( $appointment_id ) {
7460 if ( ! class_exists( '\JET_APB\Plugin' ) ) {
7461 return [];
7462 }
7463
7464 global $wpdb;
7465 $meta_table = \JET_APB\Plugin::instance()->db->appointments_meta->table();
7466
7467 if ( empty( $meta_table ) || is_array( $meta_table ) || ! is_string( $meta_table ) ) {
7468 return [];
7469 }
7470
7471 $results = $wpdb->get_results(
7472 $wpdb->prepare(
7473 'SELECT meta_key, meta_value FROM ' . esc_sql( $meta_table ) . ' WHERE appointment_id = %d',
7474 $appointment_id
7475 ),
7476 ARRAY_A
7477 );
7478
7479 $meta = [];
7480 if ( ! empty( $results ) ) {
7481 foreach ( $results as $row ) {
7482 $meta[ $row['meta_key'] ] = $row['meta_value'];
7483 }
7484 }
7485
7486 return $meta;
7487 }
7488
7489 /**
7490 * Search LMS data.
7491 *
7492 * @param array $data data.
7493 * @return array
7494 */
7495 public function search_lifter_lms_last_data( $data ) {
7496 global $wpdb;
7497 $post_type = $data['post_type'];
7498 $meta_key = '_is_complete';
7499 $trigger = $data['search_term'];
7500 $context = [];
7501 $post_id = -1;
7502
7503 if ( ! class_exists( 'LLMS_Section' ) ) {
7504 return [];
7505 }
7506 $product_type = '';
7507 if ( 'lifterlms_purchase_course' === $trigger ) {
7508 $product_type = 'course';
7509 $post_id = $data['filter']['course_id']['value'];
7510 } elseif ( 'lifterlms_purchase_membership' === $trigger ) {
7511 $product_type = 'membership';
7512 $post_id = $data['filter']['membership_id']['value'];
7513 } elseif ( 'lifterlms_cancel_membership' === $trigger ) {
7514 $product_type = 'membership';
7515 $post_id = $data['filter']['membership_id']['value'];
7516 } elseif ( 'lifterlms_lesson_completed' === $trigger ) {
7517 $post_id = $data['filter']['lesson']['value'];
7518 } elseif ( 'lifterlms_course_completed' === $trigger || 'lifterlms_course_enrolled' === $trigger || 'lifterlms_course_user_removed' === $trigger ) {
7519 $post_id = $data['filter']['course']['value'];
7520 } elseif ( 'lifterlms_section_completed' === $trigger ) {
7521 $post_id = $data['filter']['section']['value'];
7522 }
7523
7524 $where = 'postmeta.post_id = "' . $post_id . '" AND';
7525
7526 if ( 'llms_order' === $post_type ) {
7527 if ( -1 === $post_id ) {
7528 $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 ) );
7529 } else {
7530 $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 ) );
7531 }
7532 } elseif ( 'lifterlms_course_enrolled' === $trigger ) {
7533 if ( -1 === $post_id ) {
7534 $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 ) );
7535 } else {
7536 $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 ) );
7537 }
7538 } elseif ( 'lifterlms_course_user_removed' === $trigger ) {
7539 if ( -1 === $post_id ) {
7540 $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 ) );
7541 } else {
7542 $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 ) );
7543 }
7544 } elseif ( 'lifterlms_cancel_membership' === $trigger ) {
7545 if ( -1 === $post_id ) {
7546 $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 ) );
7547 } else {
7548 $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 ) );
7549 }
7550 } else {
7551 if ( -1 === $post_id ) {
7552 $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 ) );
7553 } else {
7554 $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 ) );
7555 }
7556 }
7557
7558 $response = [];
7559 if ( ! empty( $result ) ) {
7560 $result_post_id = $result[0]->post_id;
7561 $result_user_id = $result[0]->user_id;
7562
7563 switch ( $trigger ) {
7564 case 'lifterlms_lesson_completed':
7565 $context = array_merge(
7566 WordPress::get_user_context( $result_user_id ),
7567 LifterLMS::get_lms_lesson_context( $result_post_id )
7568 );
7569 $parent_course = get_post_meta( $result_post_id, '_llms_parent_course', true );
7570 if ( is_numeric( $parent_course ) ) {
7571 $context['course'] = get_the_title( (int) $parent_course );
7572 }
7573 $parent_section = get_post_meta( $result_post_id, '_llms_parent_section', true );
7574 if ( '' !== $parent_section && is_numeric( $parent_section ) ) {
7575 $context['parent_section'] = get_the_title( (int) $parent_section );
7576 }
7577 break;
7578 case 'lifterlms_course_enrolled':
7579 case 'lifterlms_course_user_removed':
7580 case 'lifterlms_course_completed':
7581 $context = array_merge(
7582 WordPress::get_user_context( $result_user_id ),
7583 LifterLMS::get_lms_course_context( $result_post_id )
7584 );
7585 break;
7586 case 'lifterlms_section_completed':
7587 $data = new \LLMS_Section( $result_post_id );
7588 $lessons = $data->get_lessons();
7589 $context = array_merge(
7590 WordPress::get_user_context( $result_user_id ),
7591 WordPress::get_post_context( $result_post_id )
7592 );
7593 $context['parent_course'] = $data->get( 'parent_course' );
7594 $context['parent_course_title'] = get_the_title( $data->get( 'parent_course' ) );
7595 if ( ! empty( $lessons ) ) {
7596 foreach ( $lessons as $key => $lesson ) {
7597 $context['section_lesson'][ $key ] = $lesson->id;
7598 $context['section_lesson_title'][ $key ] = get_the_title( $lesson->id );
7599 }
7600 }
7601 $context['section_course'] = $data->get( 'parent_course' );
7602 $context['parent_course_title'] = get_the_title( $data->get( 'parent_course' ) );
7603 break;
7604 case 'lifterlms_purchase_course':
7605 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
7606 $context['course_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
7607 $context['course_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
7608 $context['course_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
7609 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
7610 $context ['order'] = WordPress::get_post_context( $result_post_id );
7611 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
7612 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
7613 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
7614 $context = array_merge( $context, is_numeric( $user_id ) ? WordPress::get_user_context( (int) $user_id ) : [] );
7615 break;
7616 case 'lifterlms_purchase_membership':
7617 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
7618 $context['membership_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
7619 $context['membership_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
7620 $context['membership_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
7621 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
7622 $context ['order'] = WordPress::get_post_context( $result_post_id );
7623 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
7624 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
7625 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
7626 $context = array_merge( $context, is_numeric( $user_id ) ? WordPress::get_user_context( (int) $user_id ) : [] );
7627 break;
7628 case 'lifterlms_cancel_membership':
7629 $context = array_merge( WordPress::get_post_context( $result_post_id ), WordPress::get_user_context( $result[0]->user_id ) );
7630 $context['membership_id'] = $result_post_id;
7631 $context['membership_name'] = get_the_title( $result_post_id );
7632 break;
7633 default:
7634 return [];
7635
7636 }
7637 $response['pluggable_data'] = $context;
7638 $response['response_type'] = 'live';
7639
7640 }
7641
7642 return $response;
7643
7644 }
7645
7646 /**
7647 * Search SM data.
7648 *
7649 * @param array $data data.
7650 * @return array
7651 */
7652 public function search_suremember_last_data( $data ) {
7653 global $wpdb;
7654 $post_type = $data['post_type'];
7655 $meta_key = '_is_complete';
7656 $trigger = $data['search_term'];
7657 $post_id = $data['filter']['group_id']['value'];
7658
7659 if ( 'suremember_updated_group' === $trigger ) {
7660 if ( -1 === $post_id ) {
7661 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts as posts WHERE posts.post_type=%s", $post_type ) );
7662 } else {
7663 $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 ) );
7664 }
7665 } else {
7666 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}usermeta as usermeta WHERE usermeta.meta_key = %s", 'suremembers_user_access_group_' . $post_id ) );
7667 }
7668
7669 $response = [];
7670
7671 if ( ! empty( $result ) ) {
7672 $context = [];
7673 switch ( $trigger ) {
7674 case 'suremember_updated_group':
7675 $group_id = $result[0]->ID;
7676 $suremembers_post['rules'] = get_post_meta( $group_id, 'suremembers_plan_include', true );
7677 $suremembers_post['exclude'] = get_post_meta( $group_id, 'suremembers_plan_exclude', true ); //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
7678 $suremembers_post['suremembers_user_roles'] = get_post_meta( $group_id, 'suremembers_user_roles', true );
7679 $suremembers_post['title'] = get_the_title( $group_id );
7680 $plan_rules = get_post_meta( $group_id, 'suremembers_plan_rules', true );
7681 $suremembers_post['restrict'] = is_array( $plan_rules ) ? $plan_rules['restrict'] : '';
7682 $context['group'] = array_merge( WordPress::get_post_context( $group_id ), $suremembers_post );
7683 $context['group_id'] = $group_id;
7684 unset( $context['group']['ID'] );
7685 $response['pluggable_data'] = $context;
7686 $response['response_type'] = 'live';
7687 break;
7688 case 'suremember_user_added_in_group':
7689 foreach ( $result as $res ) {
7690 $meta_value = unserialize( $res->meta_value );
7691 if ( is_array( $meta_value ) && 'active' === $meta_value['status'] ) {
7692 $context = WordPress::get_user_context( $res->user_id );
7693 $context['group'] = WordPress::get_post_context( $post_id );
7694 $context['group_id'] = $post_id;
7695 unset( $context['group']['ID'] );
7696 $response['pluggable_data'] = $context;
7697 $response['response_type'] = 'live';
7698 }
7699 }
7700 break;
7701 case 'suremember_user_removed_from_group':
7702 foreach ( $result as $res ) {
7703 $meta_value = unserialize( $res->meta_value );
7704 if ( is_array( $meta_value ) && 'revoked' === $meta_value['status'] ) {
7705 $context = WordPress::get_user_context( $res->user_id );
7706 $context['group'] = WordPress::get_post_context( $post_id );
7707 $context['group_id'] = $post_id;
7708 unset( $context['group']['ID'] );
7709 $response['pluggable_data'] = $context;
7710 $response['response_type'] = 'live';
7711 }
7712 }
7713 break;
7714 default:
7715 return [];
7716
7717 }
7718 }
7719
7720 return $response;
7721
7722 }
7723
7724 /**
7725 * Search CartFlows data.
7726 *
7727 * @param array $data data.
7728 * @return array
7729 */
7730 public function search_cartflows_last_data( $data ) {
7731 global $wpdb;
7732 $trigger = $data['search_term'];
7733
7734 if ( ! function_exists( 'wcf_pro' ) ) {
7735 return [];
7736 }
7737
7738 $context = [];
7739 $response = [];
7740 if ( 'cartflows_upsell_offer_accepted' === $trigger || 'cartflows_upsell_offer_rejected' === $trigger ) {
7741 $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'" );
7742 } elseif ( 'cartflows_downsell_offer_accepted' === $trigger || 'cartflows_downsell_offer_rejected' === $trigger ) {
7743 $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'" );
7744 } elseif ( 'wcf_order_bump_item_added' === $trigger || 'wcf_order_bump_item_removed' === $trigger || 'wcf_order_bump_item_replaced' === $trigger ) {
7745 $args = [
7746 'post_type' => 'product',
7747 'orderby' => 'ID',
7748 'order' => 'DESC',
7749 'post_status' => 'publish',
7750 'numberposts' => 1,
7751 ];
7752 $products = get_posts( $args );
7753
7754 if ( count( $products ) > 0 ) {
7755 $product_id = $products[0]->ID;
7756 $product_data['product_id'] = $product_id;
7757 $product_data['product'] = WooCommerce::get_product_context( $product_id );
7758 $terms = get_the_terms( $product_id, 'product_cat' );
7759 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
7760 $cat_name = [];
7761 foreach ( $terms as $cat ) {
7762 $cat_name[] = $cat->name;
7763 }
7764 $product_data['product']['category'] = implode( ', ', $cat_name );
7765 }
7766 $terms_tags = get_the_terms( $product_id, 'product_tag' );
7767 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
7768 $tag_name = [];
7769 foreach ( $terms_tags as $tag ) {
7770 $tag_name[] = $tag->name;
7771 }
7772 $product_data['product']['tag'] = implode( ', ', $tag_name );
7773 }
7774 unset( $product_data['product']['id'] ); //phpcs:ignore
7775 $response['response_type'] = 'live';
7776 } else {
7777 $product_data['product'] = [
7778 'name' => 'Sample Product',
7779 'description' => 'This is description of sample product.',
7780 'short_description' => 'This is short description of sample product.',
7781 'image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
7782 'slug' => 'sample-product',
7783 'status' => 'publish',
7784 'type' => 'simple',
7785 'price' => '89',
7786 'featured' => '0',
7787 'sku' => 'hoodie-blue-sm',
7788 'regular_price' => '90',
7789 'sale_price' => '89',
7790 'total_sales' => '21',
7791 'category' => 'Uncategorized',
7792 'tags' => 'sample, new, 2022',
7793 'category_ids' => [ 40 ],
7794 'date_created' => [
7795 'date' => '2024-09-19 09:28:57.000000',
7796 'timezone_type' => '1',
7797 'timezone' => '+00:00',
7798 ],
7799 'date_modified' => [
7800 'date' => '2024-09-19 09:28:57.000000',
7801 'timezone_type' => '1',
7802 'timezone' => '+00:00',
7803 ],
7804 'product_id' => 9935,
7805 ];
7806 $response['response_type'] = 'sample';
7807 }
7808 $response['pluggable_data'] = $product_data;
7809 }
7810 if ( ! empty( $result ) && ( 'cartflows_upsell_offer_accepted' === $trigger || 'cartflows_upsell_offer_rejected' === $trigger ) ) {
7811 $context = [];
7812 $order_upsell_id = $result[0]->post_id;
7813 $step_id = get_post_meta( $order_upsell_id, '_cartflows_offer_step_id', true );
7814 $order_id = get_post_meta( $order_upsell_id, '_cartflows_offer_parent_id', true );
7815 $order = wc_get_order( $order_id );
7816 $upsell_order = wc_get_order( $order_upsell_id );
7817 if ( $upsell_order instanceof \WC_Order ) {
7818 $items = $upsell_order->get_items();
7819 if ( ! empty( $items ) && isset( $items[0] ) ) {
7820 $variation_id = $items[0]['product_id'];
7821 $input_qty = $items[0]['quantity'];
7822 } else {
7823 $variation_id = null;
7824 $input_qty = null;
7825 }
7826 } else {
7827 $variation_id = null;
7828 $input_qty = null;
7829 }
7830 $offer_product = wcf_pro()->utils->get_offer_data( $step_id, $variation_id, $input_qty, $order_id );
7831 $user_id = get_post_meta( $order_upsell_id, '_customer_user', true );
7832 $context = is_numeric( $user_id ) ? WordPress::get_user_context( (int) $user_id ) : [];
7833 if ( $order instanceof \WC_Order ) {
7834 $context['order'] = $order->get_data();
7835 }
7836 $context['upsell'] = $offer_product;
7837 $context['funnel_step_id'] = $step_id;
7838 if ( is_scalar( $step_id ) ) {
7839 $context['funnel_id'] = get_post_meta( intval( $step_id ), 'wcf-flow-id', true );
7840 } else {
7841 $context['funnel_id'] = null;
7842 }
7843 $response['pluggable_data'] = $context;
7844 $response['response_type'] = 'live';
7845 } elseif ( ! empty( $result ) && ( 'cartflows_downsell_offer_accepted' === $trigger || 'cartflows_downsell_offer_rejected' === $trigger ) ) {
7846 $context = [];
7847 $order_downsell_id = $result[0]->post_id;
7848 $step_id = get_post_meta( $order_downsell_id, '_cartflows_offer_step_id', true );
7849 $order_id = get_post_meta( $order_downsell_id, '_cartflows_offer_parent_id', true );
7850 $order = wc_get_order( $order_id );
7851 $downsell_order = wc_get_order( $order_downsell_id );
7852 if ( $downsell_order instanceof \WC_Order ) {
7853 $items = $downsell_order->get_items();
7854 if ( ! empty( $items ) && isset( $items[0] ) ) {
7855 $variation_id = $items[0]['product_id'];
7856 $input_qty = $items[0]['quantity'];
7857 } else {
7858 $variation_id = null;
7859 $input_qty = null;
7860 }
7861 } else {
7862 $variation_id = null;
7863 $input_qty = null;
7864 }
7865 $offer_product = wcf_pro()->utils->get_offer_data( $step_id, $variation_id, $input_qty, $order_id );
7866 $user_id = get_post_meta( $order_downsell_id, '_customer_user', true );
7867 $context = is_numeric( $user_id ) ? WordPress::get_user_context( (int) $user_id ) : [];
7868 if ( $order instanceof \WC_Order ) {
7869 $context['order'] = $order->get_data();
7870 }
7871 $context['downsell'] = $offer_product;
7872 $context['funnel_step_id'] = $step_id;
7873 if ( is_scalar( $step_id ) ) {
7874 $context['funnel_id'] = get_post_meta( intval( $step_id ), 'wcf-flow-id', true );
7875 } else {
7876 $context['funnel_id'] = null;
7877 }
7878 $response['pluggable_data'] = $context;
7879 $response['response_type'] = 'live';
7880 }
7881 return $response;
7882 }
7883
7884 /**
7885 * Prepare CartFlows Steps list.
7886 *
7887 * @param array $data data.
7888 *
7889 * @return array
7890 */
7891 public function search_cartflows_funnel_step_list( $data ) {
7892 $page = $data['page'];
7893 $limit = Utilities::get_search_page_limit();
7894 $offset = $limit * ( $page - 1 );
7895
7896 $args = [
7897 'posts_per_page' => $limit,
7898 'offset' => $offset,
7899 'orderby' => 'title',
7900 'order' => 'ASC',
7901 'post_type' => 'cartflows_step',
7902 'post_status' => 'publish',
7903 'fields' => 'ids',
7904 ];
7905 $args['meta_query'] = [
7906 [
7907 'key' => 'wcf-flow-id',
7908 'value' => $data['dynamic'],
7909 'compare' => '=',
7910 ],
7911 ];
7912
7913 $flow_step_list = get_posts( $args );
7914 $flow_step_list_count = count(
7915 get_posts(
7916 [
7917 'posts_per_page' => -1,
7918 'orderby' => 'title',
7919 'order' => 'ASC',
7920 'post_type' => 'cartflows_step',
7921 'post_status' => 'publish',
7922 'fields' => 'ids',
7923 'meta_query' => [
7924 [
7925 'key' => 'wcf-flow-id',
7926 'value' => $data['dynamic'],
7927 'compare' => '=',
7928 ],
7929 ],
7930 ]
7931 )
7932 );
7933
7934 $options = [];
7935 if ( ! empty( $flow_step_list ) ) {
7936 foreach ( $flow_step_list as $step ) {
7937 $options[] = [
7938 'label' => html_entity_decode( get_the_title( $step ), ENT_QUOTES, 'UTF-8' ),
7939 'value' => $step,
7940 ];
7941 }
7942 }
7943 return [
7944 'options' => $options,
7945 'hasMore' => $flow_step_list_count > $limit && $flow_step_list_count > $offset,
7946 ];
7947 }
7948
7949
7950 /**
7951 * Fetch user context.
7952 *
7953 * @param int $initiator_id initiator id.
7954 * @param int $friend_id friend id.
7955 * @return array
7956 */
7957 public function get_user_context( $initiator_id, $friend_id ) {
7958 $context = WordPress::get_user_context( $initiator_id );
7959
7960 $friend_context = WordPress::get_user_context( $friend_id );
7961
7962 $avatar = get_avatar_url( $initiator_id );
7963
7964 $context['avatar_url'] = ( $avatar ) ? $avatar : '';
7965
7966 $context['friend_id'] = $friend_id;
7967 $context['friend_first_name'] = $friend_context['user_firstname'];
7968 $context['friend_last_name'] = $friend_context['user_lastname'];
7969 $context['friend_email'] = $friend_context['user_email'];
7970
7971 $friend_avatar = get_avatar_url( $friend_id );
7972 $context['friend_avatar_url'] = $friend_avatar;
7973 return $context;
7974 }
7975
7976 /**
7977 * Search BP data.
7978 *
7979 * @param array $data data.
7980 * @return array
7981 */
7982 public function search_pluggables_bp_friendships( $data ) {
7983 global $wpdb, $bp;
7984 $context = [];
7985 $sample['pluggable_data'] = [
7986 'wp_user_id' => 4,
7987 'user_login' => 'katy1ßßßß',
7988 'display_name' => 'Katy Smith',
7989 'user_firstname' => 'Katy',
7990 'user_lastname' => 'Smith',
7991 'user_email' => 'katy1@gmail.com',
7992 'user_role' => [ 'subscriber' ],
7993 'avatar_url' => 'http://pqr.com/avatar',
7994 'friend_id' => 1,
7995 'friend_first_name' => 'John',
7996 'friend_last_name' => 'Wick',
7997 'friend_email' => 'john@gmail.com',
7998 'friend_avatar_url' => 'http://abc.com/avatar',
7999 ];
8000 $sample['response_type'] = 'sample';
8001
8002 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_friends'" );
8003 if ( $table_exists ) {
8004 $friendships = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_friends LIMIT 1" );
8005 if ( ! empty( $friendships ) ) {
8006 $friendship = $friendships[0];
8007 $initiator_id = $friendship->initiator_user_id;
8008 $friend_user_id = $friendship->friend_user_id;
8009 $context['pluggable_data'] = $this->get_user_context( $initiator_id, $friend_user_id );
8010 $context['response_type'] = 'live';
8011 } else {
8012 $context = $sample;
8013 }
8014 } else {
8015 $context = $sample;
8016 }
8017
8018
8019 return $context;
8020 }
8021
8022 /**
8023 * Search BuddyBoss private messages data.
8024 *
8025 * @param array $data data.
8026 * @return array
8027 */
8028 public function search_pluggables_bb_private_messages( $data ) {
8029 global $wpdb;
8030 $context = [];
8031 $sample['pluggable_data'] = [
8032 'wp_user_id' => 2,
8033 'user_login' => 'johndoe',
8034 'display_name' => 'John Doe',
8035 'user_firstname' => 'John',
8036 'user_lastname' => 'Doe',
8037 'user_email' => 'john@example.com',
8038 'user_role' => [ 'subscriber' ],
8039 'sender_id' => 1,
8040 'sender_first_name' => 'Jane',
8041 'sender_last_name' => 'Smith',
8042 'sender_email' => 'jane@example.com',
8043 'sender_display_name' => 'Jane Smith',
8044 'sender_avatar_url' => 'http://example.com/avatar.jpg',
8045 'message_id' => 1,
8046 'thread_id' => 1,
8047 'message_subject' => 'Hello!',
8048 'message_content' => 'This is a sample private message.',
8049 'message_content_html' => '<p>This is a sample private message.</p>',
8050 'message_date' => '2024-01-01 10:00:00',
8051 ];
8052 $sample['response_type'] = 'sample';
8053
8054 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_messages_messages'" );
8055 if ( $table_exists ) {
8056 $message = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}bp_messages_messages ORDER BY id DESC LIMIT 1" );
8057 if ( ! empty( $message ) ) {
8058 $recipients = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}bp_messages_recipients WHERE thread_id = %d", $message->thread_id ) );
8059 if ( ! empty( $recipients ) ) {
8060 foreach ( $recipients as $recipient ) {
8061 if ( $recipient->user_id != $message->sender_id ) {
8062 $recipient_context = WordPress::get_user_context( $recipient->user_id );
8063 $sender_context = WordPress::get_user_context( $message->sender_id );
8064
8065 $context['pluggable_data'] = array_merge(
8066 $recipient_context,
8067 [
8068 'sender_id' => $message->sender_id,
8069 'sender_first_name' => $sender_context['user_firstname'],
8070 'sender_last_name' => $sender_context['user_lastname'],
8071 'sender_email' => $sender_context['user_email'],
8072 'sender_display_name' => $sender_context['display_name'],
8073 'sender_avatar_url' => get_avatar_url( $message->sender_id ),
8074 'message_id' => $message->id,
8075 'thread_id' => $message->thread_id,
8076 'message_subject' => $message->subject,
8077 'message_content' => wp_strip_all_tags( $message->message ),
8078 'message_content_html' => $message->message,
8079 'message_date' => $message->date_sent,
8080 ]
8081 );
8082 $context['response_type'] = 'live';
8083 break;
8084 }
8085 }
8086 }
8087 }
8088 }
8089
8090 if ( empty( $context ) ) {
8091 $context = $sample;
8092 }
8093
8094 return $context;
8095 }
8096
8097 /**
8098 * Search Buddyboss profile types data.
8099 *
8100 * @param array $data data.
8101 * @return array
8102 */
8103 public function search_pluggables_bp_profile_types( $data ) {
8104 global $wpdb, $bp;
8105 $context = [];
8106 $sample['pluggable_data'] = [
8107 'wp_user_id' => 4,
8108 'user_login' => 'katy1ßßßß',
8109 'display_name' => 'Katy Smith',
8110 'user_firstname' => 'Katy',
8111 'user_lastname' => 'Smith',
8112 'user_email' => 'katy1@gmail.com',
8113 'user_role' => [ 'subscriber' ],
8114 'bb_profile_type' => '10',
8115 'bb_profile_type_name' => 'student',
8116 ];
8117 $sample['response_type'] = 'sample';
8118
8119 $post_id = $data['filter']['bb_profile_type']['value'];
8120 $get_existing = get_post_meta( $post_id, '_bp_member_type_key', true );
8121
8122 $type_term = get_term_by(
8123 'name',
8124 /**
8125 *
8126 * Ignore line
8127 *
8128 * @phpstan-ignore-next-line
8129 */
8130 $get_existing,
8131 'bp_member_type'
8132 );
8133
8134 $results = $wpdb->get_results(
8135 $wpdb->prepare(
8136 "SELECT u.ID FROM {$wpdb->prefix}users u INNER JOIN {$wpdb->prefix}term_relationships r
8137 ON u.ID = r.object_id WHERE u.user_status = 0 AND
8138 r.term_taxonomy_id = %d ORDER BY RAND() LIMIT 1",
8139 /**
8140 *
8141 * Ignore line
8142 *
8143 * @phpstan-ignore-next-line
8144 */
8145 $type_term->term_id
8146 )
8147 );
8148
8149 if ( ! empty( $results ) ) {
8150 $user = $results[0];
8151 $context['pluggable_data'] = WordPress::get_user_context( $user->ID );
8152 $context['pluggable_data']['bb_profile_type'] = $post_id;
8153 $context['pluggable_data']['bb_profile_type_name'] = get_post_meta( $post_id, '_bp_member_type_label_singular_name', true );
8154 $context['response_type'] = 'live';
8155 } else {
8156 $context = $sample;
8157 }
8158
8159 return $context;
8160 }
8161
8162 /**
8163 * Search BP User data.
8164 *
8165 * @param array $data data.
8166 * @return array
8167 */
8168 public function search_pluggables_bb_users( $data ) {
8169 global $wpdb;
8170 $context = [];
8171
8172 if ( ! class_exists( 'BP_Signup' ) ) {
8173 return [];
8174 }
8175
8176 $term = $data['search_term'];
8177
8178 if ( 'account_activated' === $term ) {
8179 $signups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}signups WHERE active = 1 ORDER BY signup_id DESC LIMIT 1" );
8180 } elseif ( 'updates_profile' === $term ) {
8181 $custom_ids = $wpdb->get_var( "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data ORDER BY last_updated DESC LIMIT 1" );
8182 $args = [ 'include' => $custom_ids ];
8183 $users = get_users( $args );
8184 } elseif ( 'gains_follower' === $term ) {
8185 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_follow ORDER BY id DESC LIMIT 1" );
8186 }
8187
8188 if ( ! empty( $signups ) ) {
8189 $pluggable_data = $signups[0];
8190 $pluggable_data = get_object_vars( $pluggable_data );
8191 unset( $pluggable_data['activation_key'] );
8192 if ( is_string( $pluggable_data['meta'] ) ) {
8193 $pluggable_data['meta'] = unserialize( $pluggable_data['meta'] );
8194 }
8195 if ( is_array( $pluggable_data['meta'] ) ) {
8196 unset( $pluggable_data['meta']['password'] );
8197 }
8198 $context['pluggable_data'] = $pluggable_data;
8199 $context['response_type'] = 'live';
8200 } elseif ( 'updates_profile' === $term ) {
8201 if ( ! empty( $users ) ) {
8202 $user = $users[0];
8203 $fields = $wpdb->get_results( $wpdb->prepare( "SELECT field_id, value FROM {$wpdb->prefix}bp_xprofile_data WHERE user_id = %d", $user->ID ) );
8204 $user_data = WordPress::get_user_context( $user->ID );
8205 $pluggable_data['user_id'] = $user->ID;
8206 $pluggable_data['user_email'] = $user_data['user_email'];
8207 foreach ( $user_data['user_role'] as $key => $role ) {
8208 $pluggable_data['user_role'][ $key ] = $role;
8209 }
8210 foreach ( $fields as $field ) {
8211 if ( function_exists( 'xprofile_get_field' ) ) {
8212 $fieldj = xprofile_get_field( $field->field_id );
8213 $pluggable_data[ $fieldj->name ] = $field->value;
8214 }
8215 }
8216 $context['pluggable_data'] = $pluggable_data;
8217 $context['response_type'] = 'live';
8218 } else {
8219 $context['pluggable_data'] = [
8220 'user_login' => 'john',
8221 'display_name' => 'john',
8222 'user_firstname' => 'john',
8223 'user_lastname' => 'd',
8224 'user_email' => 'johnd@yopmail.com',
8225 'user_role' => [
8226 'subscriber',
8227 ],
8228 'Name' => 'john',
8229 'Nickname' => 'johnd',
8230 'wp_user_id' => 16,
8231 ];
8232 $context['response_type'] = 'sample';
8233 }
8234 } elseif ( 'gains_follower' === $term ) {
8235 if ( ! empty( $results ) ) {
8236 $pluggable_data['follower'] = WordPress::get_user_context( $results[0]->follower_id );
8237 $pluggable_data['leader'] = WordPress::get_user_context( $results[0]->leader_id );
8238 $context['pluggable_data'] = $pluggable_data;
8239 $context['response_type'] = 'live';
8240 } else {
8241 $context['pluggable_data'] = [
8242 'follower' => [
8243 'wp_user_id' => 126,
8244 'user_login' => 'belli',
8245 'display_name' => 'belli',
8246 'user_firstname' => 'test',
8247 'user_lastname' => 'test',
8248 'user_email' => 'belli@gmail.com',
8249 'user_role' => [
8250 'subscriber',
8251 'wpamelia-customer',
8252 ],
8253 ],
8254 'leader' => [
8255 'wp_user_id' => 34,
8256 'user_login' => 'bella3@gmail.com',
8257 'display_name' => 'bella3@gmail.com',
8258 'user_firstname' => 'bella3',
8259 'user_lastname' => 'bella3',
8260 'user_email' => 'bellaaa3@gmail.com',
8261 'user_role' => [
8262 'wpamelia-customer',
8263 ],
8264 ],
8265 ];
8266 $context['response_type'] = 'sample';
8267 }
8268 } else {
8269 $context['pluggable_data'] = [
8270 'signup_id' => '16',
8271 'domain' => '',
8272 'path' => '',
8273 'title' => '',
8274 'user_login' => 'johnd',
8275 'user_email' => 'johnd@yopmail.com',
8276 'registered' => '2024-01-29 04:52:13',
8277 'activated' => '0000-00-00 00:00:00',
8278 'active' => '0',
8279 'meta' => [
8280 'field_1' => 'john',
8281 'field_3' => 'd',
8282 'field_4' => '09878988766',
8283 'field_2' => '123 Main Street',
8284 'field_5' => 'johnd',
8285 'profile_field_ids' => '1,3,4,2,5',
8286 ],
8287 'id' => 16,
8288 'user_name' => 'johnd',
8289 'date_sent' => '2024-01-29 04:52:13',
8290 'recently_sent' => true,
8291 'count_sent' => 1,
8292 ];
8293 $context['response_type'] = 'sample';
8294 }
8295
8296 return $context;
8297 }
8298
8299 /**
8300 * Search BP data.
8301 *
8302 * @param array $data data.
8303 * @return array
8304 */
8305 public function search_pluggables_bp_groups( $data ) {
8306 global $wpdb, $bp;
8307 $context = [];
8308 $group_data = [];
8309 $args = [
8310 'orderby' => 'user_nicename',
8311 'order' => 'ASC',
8312 'number' => 1,
8313 ];
8314
8315 $term = $data['search_term'];
8316
8317 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' ) ) {
8318 return [];
8319 }
8320
8321 if ( 'user_joins_specific_type_group' == $term ) {
8322 if ( -1 !== $data['filter']['group_type']['value'] ) {
8323 $group_type = $data['filter']['group_type']['value'];
8324 } else {
8325 $registered_types = bp_groups_get_group_types();
8326 $random_key = array_rand( $registered_types );
8327 $random_value = $registered_types[ $random_key ];
8328 $group_type = $random_value;
8329 }
8330 if ( function_exists( 'bp_get_group_ids_by_group_types' ) ) {
8331 $group_ids = bp_get_group_ids_by_group_types( $group_type );
8332 $random_key = array_rand( $group_ids );
8333 $random_value = $group_ids[ $random_key ];
8334 $group_id = $random_value;
8335 if ( function_exists( 'groups_get_group' ) ) {
8336 $group = groups_get_group( $group_id['id'] );
8337 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
8338 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
8339 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
8340 $group_data['group_type'] = $group_type;
8341 if ( function_exists( 'groups_get_group_members' ) ) {
8342 $members = groups_get_group_members(
8343 [
8344 'group_id' => $group_id,
8345 ]
8346 );
8347 $ids = [];
8348 foreach ( $members['members'] as $member ) {
8349 $ids[] = $member->ID;
8350 }
8351 $args = [
8352 'number' => 1,
8353 'include' => $ids,
8354 ];
8355 $users = get_users( $args );
8356 }
8357 }
8358 }
8359 } elseif ( 'requests_access_private_group' === $data['search_term'] ) {
8360 $group_id = $data['filter']['group_id']['value'];
8361 if ( $group_id > 0 ) {
8362 $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 ) );
8363 } else {
8364 $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' ) );
8365 }
8366 $custom_ids = $results[0]->user_id;
8367 $args = [ 'include' => $custom_ids ];
8368 $users = get_users( $args );
8369 $group_id = $results[0]->item_id;
8370 $group = groups_get_group( $group_id );
8371 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
8372 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
8373 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
8374 } elseif ( 'bb_group_created' == $term ) {
8375 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups ORDER BY id DESC LIMIT 1" );
8376 if ( function_exists( 'groups_get_group' ) ) {
8377 $group = groups_get_group( $results[0]->id );
8378 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
8379 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
8380 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
8381 $current_types = (array) bp_groups_get_group_type( $results[0]->id, false );
8382 $group_data['group_type'] = $current_types;
8383 $group_data['group_status'] = $group->status;
8384 $group_data['group_date_created'] = $group->date_created;
8385 $group_data['group_enabled_forum'] = $group->enable_forum;
8386 $group_data['group_cover_url'] = bp_get_group_cover_url( $group );
8387 $group_data['group_avatar_url'] = bp_get_group_avatar_url( $group );
8388 $group_data['group_creator'] = WordPress::get_user_context( $group->creator_id );
8389 if ( function_exists( 'groups_get_group_members' ) ) {
8390 $members = groups_get_group_members(
8391 [
8392 'group_id' => $results[0]->id,
8393 ]
8394 );
8395 foreach ( $members['members'] as $key => $member ) {
8396 $group_data['group_member'][ $key ] = WordPress::get_user_context( $member->ID );
8397 }
8398 }
8399 $args = [
8400 'item_id' => $results[0]->id,
8401 ];
8402 $invitations = groups_get_invites( $args );
8403 if ( ! empty( $invitations ) ) {
8404 foreach ( $invitations as $key => $invite ) {
8405 $group_data['invitation'][ $key ] = $invite;
8406 }
8407 }
8408 }
8409 } else {
8410 $users = get_users( $args );
8411
8412 if ( isset( $data['filter']['group_id']['value'] ) ) {
8413 $group_id = $data['filter']['group_id']['value'];
8414 $args['group_id'] = $group_id;
8415 if ( $group_id > 0 ) {
8416 $group = groups_get_group( $group_id );
8417 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
8418 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
8419 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
8420 } else {
8421 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_groups'" );
8422 if ( $table_exists ) {
8423 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups LIMIT 1" );
8424 $context['groups'] = $groups;
8425 if ( ! empty( $groups ) ) {
8426 foreach ( $groups as $group ) {
8427 $group_data['group_id'] = $group->id;
8428 $group_data['group_name'] = $group->name;
8429 $group_data['group_description'] = $group->description;
8430 }
8431 }
8432 }
8433 }
8434 }
8435 }
8436
8437 if ( 'bb_group_created' == $term ) {
8438 if ( ! empty( $group_data ) ) {
8439 $pluggable_data = $group_data;
8440 $context['pluggable_data'] = $pluggable_data;
8441 $context['response_type'] = 'live';
8442 } else {
8443 $context['pluggable_data'] = [
8444 'group_id' => 112,
8445 'group_name' => 'New Group',
8446 'group_description' => 'New Group Description',
8447 'group_type' => [ 'business' ],
8448 'group_status' => 'public',
8449 'group_date_created' => '2024-02-16 05:37:22',
8450 'group_enabled_forum' => 1,
8451 'group_cover_url' => 'https:\/\/example.com\/wp-content\/uploads\/buddypress\/groups\/23\/cover-image\/65cef4c3ea9b6-bp-cover-image.jpeg',
8452 'group_avatar_url' => 'https:\/\/example.com\/wp-content\/uploads\/group-avatars\/23\/65cef4d7cee19-bpfull.png',
8453 'group_creator' => [
8454 'wp_user_id' => 183,
8455 'user_login' => 'johnd',
8456 'display_name' => 'john',
8457 'user_firstname' => 'john',
8458 'user_lastname' => 'd',
8459 'user_email' => 'johnd@yopmail.com',
8460 'user_role' => [
8461 'subscriber',
8462 'bbp_participant',
8463 ],
8464 ],
8465 ];
8466 $context['response_type'] = 'sample';
8467 }
8468 } elseif ( ! empty( $users ) ) {
8469 $user = $users[0];
8470 $pluggable_data = $group_data;
8471
8472 $avatar = get_avatar_url( $user->ID );
8473 $pluggable_data['wp_user_id'] = $user->ID;
8474 $pluggable_data['avatar_url'] = ( $avatar ) ? $avatar : '';
8475 $pluggable_data['user_login'] = $user->user_login;
8476 $pluggable_data['display_name'] = $user->display_name;
8477 $pluggable_data['user_firstname'] = $user->user_firstname;
8478 $pluggable_data['user_lastname'] = $user->user_lastname;
8479 $pluggable_data['user_email'] = $user->user_email;
8480 $pluggable_data['user_role'] = $user->roles;
8481 $context['pluggable_data'] = $pluggable_data;
8482 $context['response_type'] = 'live';
8483 } else {
8484 $context['pluggable_data'] = [
8485 'wp_user_id' => 1,
8486 'user_login' => 'admin',
8487 'display_name' => 'Johnd',
8488 'user_firstname' => 'John',
8489 'user_lastname' => 'D',
8490 'user_email' => 'johnd@gmail.com',
8491 'user_role' => [ 'subscriber' ],
8492 'group_id' => 112,
8493 'group_name' => 'New Group',
8494 'group_description' => 'New Group Description',
8495 ];
8496 $context['response_type'] = 'sample';
8497 }
8498
8499 return $context;
8500 }
8501
8502 /**
8503 * Search complete courses.
8504 *
8505 * @param array $data data.
8506 * @return array
8507 */
8508 public function search_pluggables_complete_course( $data ) {
8509 global $wpdb;
8510 $context = [];
8511
8512 if ( isset( $data['filter']['sfwd_course_id']['value'] ) ) {
8513 $course_id = $data['filter']['sfwd_course_id']['value'];
8514 } else {
8515 $course_id = -1;
8516 }
8517 if ( -1 === $course_id ) {
8518 $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 ) );
8519 } else {
8520 $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 ) );
8521 }
8522
8523 if ( ! empty( $courses ) ) {
8524 $course = $courses[0];
8525 $course_data['course_name'] = $course->post_title;
8526 $course_data['sfwd_course_id'] = $course->ID;
8527 $course_data['course_url'] = get_permalink( $course->ID );
8528 $course_data['course_featured_image_id'] = get_post_meta( $course->ID, '_thumbnail_id', true );
8529 $course_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course->ID );
8530 $timestamp = get_user_meta( $course->user_id, 'course_completed_' . $course->ID, true );
8531 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
8532 $date_format = get_option( 'date_format' );
8533 if ( is_string( $date_format ) ) {
8534 $course_data['course_completion_date'] = wp_date( $date_format, $timestamp );
8535 }
8536 if ( function_exists( 'learndash_get_course_certificate_link' ) ) {
8537 $course_data['course_certificate'] = learndash_get_course_certificate_link( $course->ID, $course->user_id );
8538 }
8539 $context['response_type'] = 'live';
8540 } else {
8541 $course_data['course_name'] = 'Test Course';
8542 $course_data['sfwd_course_id'] = 112;
8543 $course_data['course_url'] = 'https://abc.com/test-course';
8544 $course_data['course_featured_image_id'] = 113;
8545 $course_data['course_featured_image_url'] = 'https://pqr.com/test-course-img';
8546 $course_data['course_completion_date'] = '2023-10-20';
8547 $course_data['course_certificate'] = 'https://example.com/certificates/good-performance/?course_id=112&cert-nonce=f80d0f9cc1';
8548 $context['response_type'] = 'sample';
8549 }
8550
8551 $users_data = $this->search_pluggables_add_user_role( [] );
8552 $user_data = $users_data['pluggable_data'];
8553
8554 $context['pluggable_data'] = array_merge( $course_data, $user_data );
8555 return $context;
8556 }
8557
8558 /**
8559 * Search lessons.
8560 *
8561 * @param array $data data.
8562 * @return array
8563 */
8564 public function search_pluggables_complete_lesson( $data ) {
8565 global $wpdb;
8566 $context = [];
8567
8568 if ( isset( $data['filter']['sfwd_lesson_id']['value'] ) ) {
8569 $lesson_id = $data['filter']['sfwd_lesson_id']['value'];
8570 $course_id = $data['filter']['sfwd_course_id']['value'];
8571 } else {
8572 $lesson_id = -1;
8573 $course_id = -1;
8574 }
8575 if ( -1 === $course_id ) {
8576 $courses = get_posts(
8577 [
8578 'posts_per_page' => - 1,
8579 'post_type' => 'sfwd-courses',
8580 'post_status' => 'publish',
8581 'fields' => 'ids',
8582 ]
8583 );
8584 $course_key = array_rand( $courses );
8585 $course_id = $courses[ $course_key ];
8586 }
8587 $course = get_post( $course_id );
8588 if ( $course instanceof WP_Post ) {
8589 $pluggable_data = LearnDash::get_course_context( $course );
8590 }
8591
8592 if ( -1 === $lesson_id ) {
8593 $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 ) );
8594 } else {
8595 $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 ) );
8596 }
8597
8598 if ( ! empty( $lessons ) ) {
8599 $lesson = $lessons[0];
8600
8601 $pluggable_data = WordPress::get_user_context( $lesson->user_id );
8602 $pluggable_data['lesson_name'] = $lesson->post_title;
8603 $pluggable_data['sfwd_lesson_id'] = $lesson->ID;
8604 $pluggable_data['lesson_url'] = get_permalink( $lesson->ID );
8605 $pluggable_data['lesson_featured_image_id'] = get_post_meta( $lesson->ID, '_thumbnail_id', true );
8606 $pluggable_data['lesson_featured_image_url'] = get_the_post_thumbnail_url( $lesson->ID );
8607 $context['response_type'] = 'live';
8608 } else {
8609 $pluggable_data = WordPress::get_sample_user_context();
8610 $pluggable_data['lesson_name'] = 'Test Lesson';
8611 $pluggable_data['sfwd_lesson_id'] = 114;
8612 $pluggable_data['lesson_url'] = 'https://abc.com/test-lesson';
8613 $pluggable_data['lesson_featured_image_id'] = 116;
8614 $pluggable_data['lesson_featured_image_url'] = 'https://pqr.com/test-lesson-img';
8615 $context['response_type'] = 'sample';
8616 }
8617
8618 $context['pluggable_data'] = $pluggable_data;
8619 return $context;
8620 }
8621
8622 /**
8623 * Search topics.
8624 *
8625 * @param array $data data.
8626 * @return array
8627 */
8628 public function search_pluggables_complete_topic( $data ) {
8629 global $wpdb;
8630 $context = [];
8631
8632 if ( isset( $data['filter']['sfwd_topic_id']['value'] ) ) {
8633 $topic_id = $data['filter']['sfwd_topic_id']['value'];
8634 $course_id = $data['filter']['sfwd_course_id']['value'];
8635 } else {
8636 $topic_id = -1;
8637 $course_id = -1;
8638 }
8639 if ( -1 === $course_id ) {
8640 $courses = get_posts(
8641 [
8642 'posts_per_page' => - 1,
8643 'post_type' => 'sfwd-courses',
8644 'post_status' => 'publish',
8645 'fields' => 'ids',
8646 ]
8647 );
8648 $course_key = array_rand( $courses );
8649 $course_id = $courses[ $course_key ];
8650 }
8651 $course = get_post( $course_id );
8652 if ( $course instanceof WP_Post ) {
8653 $pluggable_data = LearnDash::get_course_context( $course );
8654 }
8655
8656 if ( -1 === $topic_id ) {
8657 $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 ) );
8658 } else {
8659 $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 ) );
8660 }
8661
8662 if ( ! empty( $topics ) ) {
8663 $topic = $topics[0];
8664 $pluggable_data = WordPress::get_user_context( $topics[0]->user_id );
8665 $pluggable_data['topic_name'] = $topic->post_title;
8666 $pluggable_data['sfwd_topic_id'] = $topic->ID;
8667 $pluggable_data['topic_url'] = get_permalink( $topic->ID );
8668 $pluggable_data['topic_featured_image_id'] = get_post_meta( $topic->ID, '_thumbnail_id', true );
8669 $pluggable_data['topic_featured_image_url'] = get_the_post_thumbnail_url( $topic->ID );
8670 $context['response_type'] = 'live';
8671 } else {
8672 $pluggable_data = WordPress::get_sample_user_context();
8673 $pluggable_data['topic_name'] = 'Test Topic';
8674 $pluggable_data['sfwd_topic_id'] = 117;
8675 $pluggable_data['topic_url'] = 'https://abc.com/test-topic';
8676 $pluggable_data['topic_featured_image_id'] = 118;
8677 $pluggable_data['topic_featured_image_url'] = 'https://pqr.com/test-topic-img';
8678 $context['response_type'] = 'sample';
8679 }
8680
8681 $context['pluggable_data'] = $pluggable_data;
8682 return $context;
8683 }
8684
8685 /**
8686 * Search purchase courses.
8687 *
8688 * @param array $data data.
8689 * @return array
8690 */
8691 public function search_pluggables_purchase_course( $data ) {
8692 $context = [];
8693 $context['response_type'] = 'sample';
8694
8695 $purchase_data = [
8696 'course_product_id' => '1',
8697 'course_product_name' => 'Sample Course',
8698 'currency' => 'USD',
8699 'total_amount' => '100',
8700 'first_name' => 'John',
8701 'last_name' => 'Doe',
8702 'email' => 'john_doe@gmail.com',
8703 'phone' => '+923007626541',
8704 ];
8705
8706 $product_id = (int) ( isset( $data['filter']['course_product_id']['value'] ) ? $data['filter']['course_product_id']['value'] : '-1' );
8707 $order_id = 0;
8708
8709 if ( -1 !== $product_id ) {
8710 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
8711
8712 if ( is_array( $order_ids ) && count( $order_ids ) > 0 ) {
8713 $order_id = $order_ids[0];
8714 }
8715 } else {
8716 $orders = wc_get_orders( [] );
8717 if ( is_array( $orders ) && count( $orders ) > 0 ) {
8718 foreach ( $orders as $order ) {
8719 $items = $order->get_items();
8720
8721 if ( count( $items ) > 1 ) {
8722 continue;
8723 }
8724
8725 foreach ( $items as $item ) {
8726 if ( method_exists( $item, 'get_product_id' ) ) {
8727 $product_id = $item->get_product_id();
8728 if ( ! empty( get_post_meta( $item->get_product_id(), '_related_course', true ) ) ) {
8729 $order_id = $order->get_id();
8730 break;
8731 }
8732 }
8733 }
8734 }
8735 }
8736 }
8737
8738 if ( 0 !== $order_id ) {
8739 $order = wc_get_order( $order_id );
8740
8741 if ( $order && $order instanceof \WC_Order ) {
8742
8743 $purchase_data = LearnDash::get_purchase_course_context( $order );
8744
8745 $context['response_type'] = 'live';
8746 }
8747 }
8748
8749 $context['pluggable_data'] = $purchase_data;
8750
8751 return $context;
8752 }
8753
8754 /**
8755 * Search quiz data in LearnDash.
8756 *
8757 * @param array $data data.
8758 * @return array
8759 */
8760 public function search_pluggables_ld_quiz( $data ) {
8761 $context = [];
8762 $context['response_type'] = 'sample';
8763 global $wpdb;
8764 $user_data = WordPress::get_sample_user_context();
8765 $quiz_data = [
8766 'quiz' => 7193,
8767 'score' => 1,
8768 'count' => 1,
8769 'question_show_count' => 1,
8770 'pass' => 1,
8771 'rank' => '-',
8772 'time' => 1703595328,
8773 'pro_quizid' => 1,
8774 'course' => 0,
8775 'lesson' => 0,
8776 'topic' => 0,
8777 'points' => 20,
8778 'total_points' => 20,
8779 'percentage' => 100,
8780 'timespent' => 2.7309999999999999,
8781 'has_graded' => false,
8782 'statistic_ref_id' => 9,
8783 'started' => 1703595325,
8784 'completed' => 1703595328,
8785 'ld_version' => '4.8.0.1',
8786 'quiz_key' => '1703595328_1_7193_0',
8787 ];
8788 $output_questions['question'] = [
8789 'ID' => 7195,
8790 'post_content' => '<p>This is a first question<\/p>',
8791 'type' => 'sfwd-question',
8792 'question_type' => 'single',
8793 'points' => 20,
8794 'answers' => [
8795 'sort_answer' => [
8796 [
8797 'answer' => '',
8798 'points' => 0,
8799 'correct' => false,
8800 'type' => 'answer',
8801 ],
8802 ],
8803 'classic_answer' => [
8804 [
8805 'answer' => 'Ans 1',
8806 'points' => 1,
8807 'correct' => false,
8808 'type' => 'answer',
8809 ],
8810 [
8811 'answer' => 'Ans 2',
8812 'points' => 0,
8813 'correct' => true,
8814 'type' => 'answer',
8815 ],
8816 ],
8817 'matrix_sort_answer' => [
8818 [
8819 'answer' => '',
8820 'points' => 0,
8821 'correct' => false,
8822 'type' => 'answer',
8823 ],
8824 ],
8825 'cloze_answer' => [
8826 [
8827 'answer' => '',
8828 'points' => 0,
8829 'correct' => false,
8830 'type' => 'answer',
8831 ],
8832 ],
8833 'free_answer' => [
8834 [
8835 'answer' => '',
8836 'points' => 0,
8837 'correct' => false,
8838 'type' => 'answer',
8839 ],
8840 ],
8841 'assessment_answer' => [
8842 [
8843 'answer' => '',
8844 'points' => 0,
8845 'correct' => false,
8846 'type' => 'answer',
8847 ],
8848 ],
8849 'essay' => [
8850 [
8851 'answer' => '',
8852 'points' => 0,
8853 'correct' => false,
8854 'type' => 'answer',
8855 ],
8856 ],
8857 ],
8858 ];
8859
8860 $quiz_id = (int) ( isset( $data['filter']['sfwd_quiz_id']['value'] ) ? $data['filter']['sfwd_quiz_id']['value'] : '-1' );
8861 $question_id = $data['filter']['sfwd_question_id']['value'];
8862 $term = $data['search_term'];
8863 $mark = 1;
8864 if ( 'passes_quiz' == $term ) {
8865 $mark = 1;
8866 } elseif ( 'fails_quiz' == $term ) {
8867 $mark = 0;
8868 }
8869 if ( 'passes_quiz' == $term || 'fails_quiz' == $term ) {
8870 if ( -1 == $quiz_id ) {
8871 $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 ) );
8872 } else {
8873 $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 ) );
8874 }
8875 } elseif ( 'quiz_essay_submitted' == $term ) {
8876 $question_id = $data['filter']['sfwd_question_id']['value'];
8877 $args = [
8878 'post_type' => 'sfwd-essays',
8879 'posts_per_page' => 1,
8880 'order' => 'DESC',
8881 'post_status' => [ 'graded', 'not_graded' ],
8882 'meta_query' => [
8883 'relation' => 'AND',
8884 [
8885 'key' => 'quiz_post_id',
8886 'value' => $quiz_id,
8887 'compare' => '=',
8888 ],
8889 [
8890 'key' => 'question_post_id',
8891 'value' => $question_id,
8892 'compare' => '=',
8893 ],
8894 ],
8895 ];
8896 $essay = get_posts( $args );
8897 } elseif ( 'quiz_essay_graded' == $term ) {
8898 $args = [
8899 'post_type' => 'sfwd-essays',
8900 'posts_per_page' => 1,
8901 'order' => 'DESC',
8902 'post_status' => 'graded',
8903 'meta_query' => [
8904 'relation' => 'AND',
8905 [
8906 'key' => 'quiz_post_id',
8907 'value' => $quiz_id,
8908 'compare' => '=',
8909 ],
8910 [
8911 'key' => 'question_post_id',
8912 'value' => $question_id,
8913 'compare' => '=',
8914 ],
8915 ],
8916 ];
8917 $essay = get_posts( $args );
8918 }
8919
8920 if ( 'quiz_essay_submitted' == $term || 'quiz_essay_graded' == $term ) {
8921 if ( ! empty( $essay ) ) {
8922 $context = is_numeric( $essay[0]->post_author ) ? WordPress::get_user_context( (int) $essay[0]->post_author ) : [];
8923 $course_id = get_post_meta( $essay[0]->ID, 'course_id', true );
8924 $lesson_id = get_post_meta( $essay[0]->ID, 'lesson_id', true );
8925 $context['quiz_name'] = get_the_title( $quiz_id );
8926 $context['sfwd_quiz_id'] = $quiz_id;
8927 $context['sfwd_question_id'] = $question_id;
8928 $context['question_name'] = is_numeric( $question_id ) ? get_the_title( (int) $question_id ) : null;
8929 $context['course_name'] = is_numeric( $course_id ) ? get_the_title( (int) $course_id ) : null;
8930 $context['course_id'] = $course_id;
8931 $context['lesson_name'] = is_numeric( $lesson_id ) ? get_the_title( (int) $lesson_id ) : null;
8932 $context['lesson_id'] = $lesson_id;
8933 $context['essay_id'] = $essay[0]->ID;
8934 $context['essay'] = WordPress::get_post_context( $essay[0]->ID );
8935 if ( 'quiz_essay_graded' == $term ) {
8936 $users_quiz_data = get_user_meta( (int) $essay[0]->post_author, '_sfwd-quizzes', true );
8937 if ( is_array( $users_quiz_data ) && is_array( $users_quiz_data[0] ) ) {
8938 $essay_post_id = $essay[0]->ID;
8939 $result = array_filter(
8940 $users_quiz_data[0]['graded'],
8941 function( $item ) use ( $essay_post_id ) {
8942 return $item['post_id'] === $essay_post_id;
8943 }
8944 );
8945 $context['essay_points_earned'] = $result[1]['points_awarded'];
8946 }
8947 }
8948 $context['response_type'] = 'live';
8949 } else {
8950 $context = WordPress::get_sample_user_context();
8951 $context['quiz_name'] = 'Test Quiz';
8952 $context['sfwd_quiz_id'] = 11;
8953 $context['sfwd_question_id'] = 12;
8954 $context['course_name'] = 'Test Course';
8955 $context['course_id'] = 13;
8956 $context['lesson_name'] = 'Test Lesson';
8957 $context['lesson_id'] = 14;
8958 $context['essay'] = [
8959 'ID' => 12,
8960 'post_content' => 'demo',
8961 ];
8962 $context['response_type'] = 'sample';
8963 }
8964
8965 $context['pluggable_data'] = $context;
8966 } else {
8967 if ( ! empty( $result ) ) {
8968 $user_data = WordPress::get_user_context( $result[0]->user_id );
8969 $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 ) );
8970
8971 $data = unserialize( $quiz_result[0]->meta_value );
8972 $found_element = null;
8973 foreach ( (array) $data as $element ) {
8974 if ( is_array( $element ) ) {
8975 if ( isset( $element['statistic_ref_id'] ) && $element['statistic_ref_id'] == $result[0]->statistic_ref_id ) {
8976 $found_element = $element;
8977 break;
8978 }
8979 }
8980 }
8981 $quiz_data = (array) $found_element;
8982 $output_questions = LearnDash::get_quiz_questions_answers( $quiz_id );
8983 if ( is_array( $quiz_data ) ) {
8984 $quiz_data['quiz_name'] = get_the_title( $quiz_id );
8985 $quiz_data['sfwd_quiz_id'] = $quiz_id;
8986 }
8987 $context['response_type'] = 'live';
8988 }
8989 $context['pluggable_data'] = array_merge( $quiz_data, $user_data, $output_questions );
8990 }
8991
8992 return $context;
8993 }
8994
8995 /**
8996 * Fetch BB templates.
8997 *
8998 * @return array
8999 */
9000 public function get_beaver_builder_templates() {
9001 $allowed_types = [ 'subscribe-form', 'contact-form' ];
9002 $templates = [];
9003 $all_templates = get_posts(
9004 [
9005 'post_type' => 'fl-builder-template',
9006 'meta_key' => '_fl_builder_data',
9007 'posts_per_page' => -1,
9008 ]
9009 );
9010 $posts = get_posts(
9011 [
9012 'post_type' => 'any',
9013 'meta_key' => '_fl_builder_data',
9014 'posts_per_page' => -1,
9015 ]
9016 );
9017 $posts = array_merge( $all_templates, $posts );
9018
9019 if ( ! empty( $posts ) ) {
9020 foreach ( $posts as $post ) {
9021 $meta = get_post_meta( $post->ID, '_fl_builder_data', true );
9022 foreach ( (array) $meta as $node_id => $node ) {
9023 if ( is_object( $node ) && isset( $node->type ) && 'module' === $node->type && property_exists( $node, 'settings' ) ) {
9024 $settings = $node->settings;
9025 if ( in_array( $settings->type, $allowed_types, true ) ) {
9026 $label = $post->post_title;
9027 if ( '' !== $settings->node_label ) {
9028 $label .= ' - ' . $settings->node_label;
9029 }
9030 $templates[] = [
9031 'label' => $label,
9032 'value' => $node_id,
9033 ];
9034 }
9035 }
9036 }
9037 }
9038 }
9039 return $templates;
9040 }
9041
9042 /**
9043 * Search beaver builder forms.
9044 *
9045 * @param array $data data.
9046 * @return array
9047 */
9048 public function search_beaver_builder_forms( $data ) {
9049 $templates = $this->get_beaver_builder_templates();
9050 return [
9051 'options' => $templates,
9052 'hasMore' => false,
9053 ];
9054 }
9055
9056 /**
9057 * Search FluentCRM fields.
9058 *
9059 * @param array $data data.
9060 * @return array
9061 */
9062 public function search_fluentcrm_custom_fields( $data ) {
9063 $context = [];
9064 if ( ! class_exists( 'FluentCrm\App\Models\CustomContactField' ) ) {
9065 return [];
9066 }
9067 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
9068 $context['fields'] = $custom_fields;
9069 return $context;
9070 }
9071
9072 /**
9073 * Search FluentCRM Company fields.
9074 *
9075 * @param array $data data.
9076 * @return array
9077 */
9078 public function search_fluentcrm_company_custom_fields( $data ) {
9079 $context = [];
9080 if ( function_exists( 'fluentcrm_get_custom_company_fields' ) ) {
9081 $custom_fields = fluentcrm_get_custom_company_fields();
9082 $context['fields'] = $custom_fields;
9083 }
9084 return $context;
9085 }
9086
9087 /**
9088 * Search FluentCRM fields and display it in dropdown for trigger.
9089 *
9090 * @param array $data data.
9091 * @return array
9092 */
9093 public function search_fluentcrm_custom_fields_data( $data ) {
9094
9095 if ( ! class_exists( 'FluentCrm\App\Models\CustomContactField' ) ) {
9096 return [];
9097 }
9098
9099 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
9100 $options = [];
9101
9102 if ( ! empty( $custom_fields ) ) {
9103 foreach ( $custom_fields as $custom_field ) {
9104 $options[] = [
9105 'label' => $custom_field['label'],
9106 'value' => $custom_field['slug'],
9107 ];
9108 }
9109 }
9110
9111 return [
9112 'options' => $options,
9113 'hasMore' => false,
9114 ];
9115 }
9116
9117 /**
9118 * Fetch WP JOB Manager Last Data.
9119 *
9120 * @param array $data data.
9121 * @return array
9122 */
9123 public function search_wp_job_manger_last_data( $data ) {
9124 global $wpdb;
9125 $job_type = $data['filter']['job_type']['value'];
9126 $args = [
9127 'posts_per_page' => 1,
9128 'post_type' => 'job_listing',
9129 'orderby' => 'id',
9130 'order' => 'DESC',
9131 ];
9132
9133 if ( -1 !== $job_type ) {
9134 $args['tax_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
9135 [
9136 'taxonomy' => 'job_listing_type',
9137 'field' => 'term_id',
9138 'terms' => $job_type,
9139 ],
9140 ];
9141 }
9142 $posts = get_posts( $args );
9143 if ( empty( $posts ) ) {
9144 $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 );
9145 return $context;
9146 }
9147
9148 $post = $posts[0];
9149 $post_content = WordPress::get_post_context( $post->ID );
9150 $post_meta = WordPress::get_post_meta( $post->ID );
9151 $job_data = array_merge(
9152 $post_content,
9153 is_array( $post_meta ) ? $post_meta : [],
9154 is_numeric( $post->post_author ) ? WordPress::get_user_context( (int) $post->post_author ) : []
9155 );
9156 foreach ( $job_data as $key => $job ) {
9157 $newkey = str_replace( 'post', 'wpjob', $key );
9158 unset( $job_data[ $key ] );
9159 $job_data[ $newkey ] = $job;
9160 }
9161 $context['response_type'] = 'live';
9162 $context['pluggable_data'] = $job_data;
9163 return $context;
9164
9165 }
9166
9167 /**
9168 * Get Amelia Appointment Category.
9169 *
9170 * @param array $data data.
9171 *
9172 * @return array
9173 */
9174 public function search_amelia_category_list( $data ) {
9175
9176 global $wpdb;
9177
9178 $page = $data['page'];
9179 $limit = Utilities::get_search_page_limit();
9180 $offset = $limit * ( $page - 1 );
9181
9182 $categories = $wpdb->get_results(
9183 $wpdb->prepare(
9184 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_categories WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
9185 [ 'visible', $limit, $offset ]
9186 ),
9187 OBJECT
9188 );
9189
9190 $categories_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9191
9192 $options = [];
9193 if ( ! empty( $categories ) ) {
9194 foreach ( $categories as $category ) {
9195 $options[] = [
9196 'label' => $category->name,
9197 'value' => $category->id,
9198 ];
9199 }
9200 }
9201
9202 return [
9203 'options' => $options,
9204 'hasMore' => $categories_count > $limit && $categories_count > $offset,
9205 ];
9206
9207 }
9208
9209 /**
9210 * Get Amelia Appointment Services.
9211 *
9212 * @param array $data data.
9213 *
9214 * @return array
9215 */
9216 public function search_amelia_service_list( $data ) {
9217
9218 global $wpdb;
9219
9220 $page = $data['page'];
9221 $limit = Utilities::get_search_page_limit();
9222 $offset = $limit * ( $page - 1 );
9223
9224 $services = $wpdb->get_results(
9225 $wpdb->prepare(
9226 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_services
9227 WHERE categoryId = %d AND status = %s
9228 ORDER BY name ASC LIMIT %d OFFSET %d",
9229 [ $data['dynamic'], 'visible', $limit, $offset ]
9230 ),
9231 OBJECT
9232 );
9233
9234 $services_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9235
9236 $options = [];
9237 if ( ! empty( $services ) ) {
9238 foreach ( $services as $category ) {
9239 $options[] = [
9240 'label' => $category->name,
9241 'value' => $category->id,
9242 ];
9243 }
9244 }
9245
9246 return [
9247 'options' => $options,
9248 'hasMore' => $services_count > $limit && $services_count > $offset,
9249 ];
9250
9251 }
9252
9253 /**
9254 * Get Amelia Package List.
9255 *
9256 * @param array $data data.
9257 *
9258 * @return array
9259 */
9260 public function search_amelia_package_list( $data ) {
9261 global $wpdb;
9262
9263 $page = isset( $data['page'] ) ? $data['page'] : 1;
9264 $limit = Utilities::get_search_page_limit();
9265 $offset = $limit * ( $page - 1 );
9266
9267 $packages = $wpdb->get_results(
9268 $wpdb->prepare(
9269 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_packages
9270 WHERE status = %s
9271 ORDER BY name ASC LIMIT %d OFFSET %d",
9272 [ 'visible', $limit, $offset ]
9273 ),
9274 OBJECT
9275 );
9276
9277 $packages_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9278
9279 $options = [];
9280 if ( ! empty( $packages ) ) {
9281 foreach ( $packages as $package ) {
9282 $options[] = [
9283 'label' => $package->name,
9284 'value' => $package->id,
9285 ];
9286 }
9287 }
9288
9289 return [
9290 'options' => $options,
9291 'hasMore' => $packages_count > $limit && $packages_count > $offset,
9292 ];
9293
9294 }
9295
9296 /**
9297 * Get Amelia Events.
9298 *
9299 * @param array $data data.
9300 *
9301 * @return array
9302 */
9303 public function search_amelia_events_list( $data ) {
9304
9305 global $wpdb;
9306
9307 $page = $data['page'];
9308 $limit = Utilities::get_search_page_limit();
9309 $offset = $limit * ( $page - 1 );
9310
9311 $events = $wpdb->get_results(
9312 $wpdb->prepare(
9313 "SELECT SQL_CALC_FOUND_ROWS id, name from {$wpdb->prefix}amelia_events WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
9314 [ 'approved', $limit, $offset ]
9315 ),
9316 OBJECT
9317 );
9318
9319 $list_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9320
9321 $options = [];
9322 if ( ! empty( $events ) ) {
9323 foreach ( $events as $event ) {
9324 $options[] = [
9325 'label' => $event->name,
9326 'value' => $event->id,
9327 ];
9328 }
9329 }
9330
9331 return [
9332 'options' => $options,
9333 'hasMore' => $list_count > $limit && $list_count > $offset,
9334 ];
9335
9336 }
9337
9338 /**
9339 * Get Amelia Events.
9340 *
9341 * @param array $data data.
9342 *
9343 * @return array
9344 */
9345 public function search_amelia_booking_status_list( $data ) {
9346
9347 $options = [];
9348 $options[] = [
9349 'label' => 'Approved',
9350 'value' => 'approved',
9351 ];
9352 $options[] = [
9353 'label' => 'Pending',
9354 'value' => 'pending',
9355 ];
9356 $options[] = [
9357 'label' => 'Rejected',
9358 'value' => 'rejected',
9359 ];
9360 $options[] = [
9361 'label' => 'Canceled',
9362 'value' => 'canceled',
9363 ];
9364 $options[] = [
9365 'label' => 'No-show',
9366 'value' => 'no-show',
9367 ];
9368 return [
9369 'options' => $options,
9370 'hasMore' => false,
9371 ];
9372 }
9373
9374 /**
9375 * Get last data for trigger.
9376 *
9377 * @param array $data data.
9378 * @return array
9379 */
9380 public function search_amelia_appointment_booked_triggers_last_data( $data ) {
9381 global $wpdb;
9382
9383 $context = [];
9384
9385 $appointment_category = $data['filter']['amelia_category_list']['value'];
9386 $appointment_service = $data['filter']['amelia_service_list']['value'];
9387
9388 if ( -1 === $appointment_service ) {
9389 // If service exists as per category selected.
9390 $service_exist = $wpdb->get_row(
9391 $wpdb->prepare(
9392 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
9393 [ $appointment_category ]
9394 ),
9395 ARRAY_A
9396 );
9397
9398 if ( empty( $service_exist ) ) {
9399 $result = [];
9400 } else {
9401 $result = $wpdb->get_row(
9402 '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 )',
9403 ARRAY_A
9404 );
9405 }
9406 } else {
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 customer.appointmentId = ( SELECT max(id) FROM ' . $wpdb->prefix . 'amelia_appointments ) AND appointments.serviceId = %d',
9410 [ $appointment_service ]
9411 ),
9412 ARRAY_A
9413 );
9414 }
9415
9416 if ( ! empty( $result ) ) {
9417
9418 $payment_result = $wpdb->get_row(
9419 $wpdb->prepare(
9420 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
9421 [ $result['id'] ]
9422 ),
9423 ARRAY_A
9424 );
9425
9426 $customer_result = $wpdb->get_row(
9427 $wpdb->prepare(
9428 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
9429 [ $result['customerId'] ]
9430 ),
9431 ARRAY_A
9432 );
9433
9434 $service_result = $wpdb->get_row(
9435 $wpdb->prepare(
9436 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
9437 [ $result['serviceId'] ]
9438 ),
9439 ARRAY_A
9440 );
9441
9442 $category_result = $wpdb->get_row(
9443 $wpdb->prepare(
9444 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
9445 [ $service_result['categoryId'] ]
9446 ),
9447 ARRAY_A
9448 );
9449
9450 if ( $result['couponId'] ) {
9451 $coupon_result = $wpdb->get_row(
9452 $wpdb->prepare(
9453 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9454 [ $result['couponId'] ]
9455 ),
9456 ARRAY_A
9457 );
9458 } else {
9459 $coupon_result = [];
9460 }
9461
9462 if ( ! empty( $result['customFields'] ) ) {
9463 $custom_fields = json_decode( $result['customFields'], true );
9464
9465 $fields_arr = [];
9466 foreach ( (array) $custom_fields as $fields ) {
9467 if ( is_array( $fields ) ) {
9468 $fields_arr[ $fields['label'] ] = $fields['value'];
9469 }
9470 }
9471 unset( $result['customFields'] );
9472 } else {
9473 $fields_arr = [];
9474 }
9475
9476 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
9477 $context['response_type'] = 'live';
9478 } else {
9479
9480 $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 );
9481 }
9482
9483 return $context;
9484 }
9485
9486 /**
9487 * Get last data for package purchase triggers (both purchase and status updated).
9488 *
9489 * @param array $data data.
9490 * @return array
9491 */
9492 public function search_amelia_package_purchase_triggers_last_data( $data ) {
9493 global $wpdb;
9494
9495 $context = [];
9496 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
9497 $package_filter = isset( $data['filter']['amelia_package_list']['value'] ) ? $data['filter']['amelia_package_list']['value'] : -1;
9498
9499
9500 if ( -1 === $package_filter ) {
9501 $result = $wpdb->get_row(
9502 'SELECT packages.*, customer.*, pc.* FROM ' . $wpdb->prefix . 'amelia_packages_to_customers as pc
9503 INNER JOIN ' . $wpdb->prefix . 'amelia_packages as packages ON pc.packageId=packages.id
9504 INNER JOIN ' . $wpdb->prefix . 'amelia_users as customer ON pc.customerId=customer.id
9505 WHERE pc.id = ( SELECT max(id) FROM ' . $wpdb->prefix . 'amelia_packages_to_customers )',
9506 ARRAY_A
9507 );
9508 } else {
9509 $result = $wpdb->get_row(
9510 $wpdb->prepare(
9511 'SELECT packages.*, customer.*, pc.* FROM ' . $wpdb->prefix . 'amelia_packages_to_customers as pc
9512 INNER JOIN ' . $wpdb->prefix . 'amelia_packages as packages ON pc.packageId=packages.id
9513 INNER JOIN ' . $wpdb->prefix . 'amelia_users as customer ON pc.customerId=customer.id
9514 WHERE packages.id = %d AND pc.id = ( SELECT max(id) FROM ' . $wpdb->prefix . 'amelia_packages_to_customers WHERE packageId = %d )',
9515 [ $package_filter, $package_filter ]
9516 ),
9517 ARRAY_A
9518 );
9519 }
9520
9521 if ( ! empty( $result ) ) {
9522 $payment_result = $wpdb->get_row(
9523 $wpdb->prepare(
9524 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE packageCustomerId = %d',
9525 [ $result['id'] ]
9526 ),
9527 ARRAY_A
9528 );
9529
9530 if ( empty( $payment_result ) ) {
9531 $payment_result = [];
9532 }
9533
9534 if ( ! empty( $result['couponId'] ) ) {
9535 $coupon_result = $wpdb->get_row(
9536 $wpdb->prepare(
9537 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9538 [ $result['couponId'] ]
9539 ),
9540 ARRAY_A
9541 );
9542 } else {
9543 $coupon_result = [];
9544 }
9545
9546 $fields_arr = [];
9547 if ( ! empty( $result['customFields'] ) ) {
9548 $custom_fields = json_decode( $result['customFields'], true );
9549 if ( is_array( $custom_fields ) ) {
9550 foreach ( $custom_fields as $fields ) {
9551 if ( is_array( $fields ) && isset( $fields['label'], $fields['value'] ) ) {
9552 $fields_arr[ $fields['label'] ] = $fields['value'];
9553 }
9554 }
9555 }
9556 unset( $result['customFields'] );
9557 }
9558
9559 $json_fields = [ 'limitPerCustomer', 'settings', 'translations' ];
9560 foreach ( $json_fields as $field ) {
9561 if ( isset( $result[ $field ] ) && is_string( $result[ $field ] ) && ! empty( $result[ $field ] ) ) {
9562 $decoded = json_decode( $result[ $field ], true );
9563 if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) {
9564 foreach ( $decoded as $key => $value ) {
9565 if ( is_array( $value ) ) {
9566 foreach ( $value as $subkey => $subvalue ) {
9567 $result[ $field . '_' . $key . '_' . $subkey ] = $subvalue;
9568 }
9569 } else {
9570 $result[ $field . '_' . $key ] = $value;
9571 }
9572 }
9573 $result[ $field . '_original' ] = $decoded;
9574 unset( $result[ $field ] );
9575 }
9576 }
9577 }
9578
9579 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $coupon_result );
9580 $context['pluggable_data']['amelia_package_list'] = $result['packageId'];
9581
9582 switch ( $term ) {
9583 case 'amelia_package_purchase_status_updated':
9584 $context['pluggable_data']['old_status'] = isset( $result['status'] ) ? $result['status'] : 'pending';
9585 $context['pluggable_data']['new_status'] = 'approved';
9586 break;
9587 }
9588
9589 $context['response_type'] = 'live';
9590 } else {
9591 $context = [
9592 'response_type' => 'sample',
9593 'pluggable_data' => [
9594 'id' => '1',
9595 'name' => 'Premium Wellness Package',
9596 'description' => 'Complete wellness package with multiple services',
9597 'color' => '#1788FB',
9598 'price' => '1800',
9599 'status' => 'visible',
9600 'pictureFullPath' => '',
9601 'pictureThumbPath' => '',
9602 'position' => '1',
9603 'calculatedPrice' => '1',
9604 'discount' => '0',
9605 'endDate' => '',
9606 'durationType' => '',
9607 'durationCount' => '',
9608 'depositPayment' => 'disabled',
9609 'deposit' => '0',
9610 'fullPayment' => '0',
9611 'sharedCapacity' => '0',
9612 'quantity' => '1',
9613 'type' => 'customer',
9614 'externalId' => '',
9615 'firstName' => 'John',
9616 'lastName' => 'Doe',
9617 'email' => 'john.doe@example.com',
9618 'birthday' => '1990-05-15',
9619 'phone' => '+1-234-567-8900',
9620 'gender' => 'male',
9621 'note' => 'Regular customer',
9622 'password' => '',
9623 'usedTokens' => '',
9624 'zoomUserId' => '',
9625 'stripeConnect' => '',
9626 'countryPhoneIso' => 'us',
9627 'timeZone' => 'America/New_York',
9628 'appleCalendarId' => '',
9629 'employeeAppleCalendar' => '',
9630 'badgeId' => '',
9631 'error' => '',
9632 'show' => '1',
9633 'customerId' => '4',
9634 'customer' => '',
9635 'couponId' => '',
9636 'coupon' => '',
9637 'tax' => '',
9638 'packageId' => '1',
9639 'payments' => [],
9640 'start' => '2025-12-15 08:39:52',
9641 'end' => '2025-12-29 08:39:52',
9642 'purchased' => '2025-12-15 08:39:52',
9643 'bookingsCount' => '3',
9644 'token' => 'abc123xyz',
9645 'limitPerCustomer_enabled' => '1',
9646 'limitPerCustomer_numberOfApp' => '15',
9647 'limitPerCustomer_timeFrame' => 'day',
9648 'limitPerCustomer_period' => '15',
9649 'limitPerCustomer_original' => [
9650 'enabled' => true,
9651 'numberOfApp' => 15,
9652 'timeFrame' => 'day',
9653 'period' => 15,
9654 ],
9655 'settings_payments_paymentLinks' => [
9656 'enabled' => '',
9657 'changeBookingStatus' => '',
9658 'redirectUrl' => '',
9659 ],
9660 'settings_original' => [
9661 'payments' => [
9662 'paymentLinks' => [
9663 'enabled' => '',
9664 'changeBookingStatus' => '',
9665 'redirectUrl' => '',
9666 ],
9667 ],
9668 ],
9669 'translations_defaultLanguage' => 'en_US',
9670 'translations_original' => [
9671 'defaultLanguage' => 'en_US',
9672 ],
9673 'amelia_package_list' => '1',
9674 ],
9675 ];
9676
9677 switch ( $term ) {
9678 case 'amelia_package_purchase_status_updated':
9679 $context['pluggable_data']['old_status'] = 'pending';
9680 $context['pluggable_data']['new_status'] = 'approved';
9681 break;
9682 }
9683 }
9684
9685 return $context;
9686 }
9687
9688 /**
9689 * Get last data for trigger.
9690 *
9691 * @param array $data data.
9692 * @return array
9693 */
9694 public function search_amelia_appointment_booking_status_changed_triggers_last_data( $data ) {
9695 global $wpdb;
9696
9697 $context = [];
9698
9699 $appointment_category = $data['filter']['amelia_category_list']['value'];
9700 $appointment_service = $data['filter']['amelia_service_list']['value'];
9701 $appointment_status = $data['filter']['appointment_status']['value'];
9702
9703 if ( -1 === $appointment_service ) {
9704 // If service exists as per category selected.
9705 $service_exist = $wpdb->get_row(
9706 $wpdb->prepare(
9707 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
9708 [ $appointment_category ]
9709 ),
9710 ARRAY_A
9711 );
9712
9713 if ( empty( $service_exist ) ) {
9714 $result = [];
9715 } else {
9716 $result = $wpdb->get_row(
9717 '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 )',
9718 ARRAY_A
9719 );
9720 }
9721 } else {
9722 if ( -1 === $appointment_status ) {
9723 $result = $wpdb->get_row(
9724 $wpdb->prepare(
9725 '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',
9726 [ $appointment_service ]
9727 ),
9728 ARRAY_A
9729 );
9730 } else {
9731 $result = $wpdb->get_row(
9732 $wpdb->prepare(
9733 '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',
9734 [ $appointment_status, $appointment_service ]
9735 ),
9736 ARRAY_A
9737 );
9738 }
9739 }
9740
9741 if ( ! empty( $result ) ) {
9742
9743 $payment_result = $wpdb->get_row(
9744 $wpdb->prepare(
9745 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
9746 [ $result['id'] ]
9747 ),
9748 ARRAY_A
9749 );
9750
9751 $customer_result = $wpdb->get_row(
9752 $wpdb->prepare(
9753 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
9754 [ $result['customerId'] ]
9755 ),
9756 ARRAY_A
9757 );
9758
9759 $service_result = $wpdb->get_row(
9760 $wpdb->prepare(
9761 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
9762 [ $result['serviceId'] ]
9763 ),
9764 ARRAY_A
9765 );
9766
9767 $category_result = $wpdb->get_row(
9768 $wpdb->prepare(
9769 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
9770 [ $service_result['categoryId'] ]
9771 ),
9772 ARRAY_A
9773 );
9774
9775 if ( $result['couponId'] ) {
9776 $coupon_result = $wpdb->get_row(
9777 $wpdb->prepare(
9778 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9779 [ $result['couponId'] ]
9780 ),
9781 ARRAY_A
9782 );
9783 } else {
9784 $coupon_result = [];
9785 }
9786
9787 if ( ! empty( $result['customFields'] ) ) {
9788 $custom_fields = json_decode( $result['customFields'], true );
9789
9790 $fields_arr = [];
9791 foreach ( (array) $custom_fields as $fields ) {
9792 if ( is_array( $fields ) ) {
9793 $fields_arr[ $fields['label'] ] = $fields['value'];
9794 }
9795 }
9796 unset( $result['customFields'] );
9797 } else {
9798 $fields_arr = [];
9799 }
9800
9801 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
9802 $context['pluggable_data']['amelia_category_list'] = $appointment_category;
9803 if ( -1 === $appointment_status ) {
9804 $context['pluggable_data']['appointment_status'] = 'approved';
9805 } else {
9806 $context['pluggable_data']['appointment_status'] = $appointment_status;
9807 }
9808 if ( -1 === $appointment_service ) {
9809 $context['pluggable_data']['amelia_service_list'] = $service_result['id'];
9810 } else {
9811 $context['pluggable_data']['amelia_service_list'] = $appointment_service;
9812 }
9813 $context['response_type'] = 'live';
9814 } else {
9815
9816 $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 );
9817 }
9818
9819 return $context;
9820 }
9821
9822 /**
9823 * Get last data for trigger.
9824 *
9825 * @param array $data data.
9826 * @return array
9827 */
9828 public function search_amelia_event_status_changed_triggers_last_data( $data ) {
9829 global $wpdb;
9830
9831 $context = [];
9832
9833 $event_selected = $data['filter']['amelia_events_list']['value'];
9834 $event_status = $data['filter']['event_booking_status']['value'];
9835
9836 $query = 'SELECT *
9837 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
9838 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
9839 ON customer.id = event_period.customerBookingId';
9840
9841 if ( -1 === $event_selected ) {
9842 if ( -1 !== $event_status ) {
9843 $query .= $wpdb->prepare(
9844 ' WHERE event_period.customerBookingId = (
9845 SELECT MAX(customerBookingId)
9846 FROM ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods
9847 ) AND customer.status = %s',
9848 $event_status
9849 );
9850 } else {
9851 $query .= ' WHERE event_period.customerBookingId = (
9852 SELECT MAX(customerBookingId)
9853 FROM ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods
9854 )';
9855 }
9856 } else {
9857 $event_periods_result = $wpdb->get_results(
9858 $wpdb->prepare(
9859 'SELECT id
9860 FROM ' . $wpdb->prefix . 'amelia_events_periods
9861 WHERE eventId = %d',
9862 [ $event_selected ]
9863 ),
9864 ARRAY_A
9865 );
9866 if ( ! empty( $event_periods_result ) ) {
9867 $ids = wp_list_pluck( $event_periods_result, 'id' );
9868 }
9869
9870 if ( ! empty( $ids ) ) {
9871 if ( -1 !== $event_status ) {
9872 $query .= $wpdb->prepare(
9873 ' WHERE event_period.eventPeriodId IN (%s) AND customer.status = %s',
9874 implode( ',', $ids ),
9875 $event_status
9876 );
9877 } else {
9878 $query .= $wpdb->prepare(
9879 ' WHERE event_period.eventPeriodId IN (%s)',
9880 implode( ',', $ids )
9881 );
9882 }
9883 } else {
9884 if ( -1 !== $event_status ) {
9885 $query .= $wpdb->prepare(
9886 ' WHERE event_period.eventPeriodId = %d AND customer.status = %s',
9887 $event_selected,
9888 $event_status
9889 );
9890 } else {
9891 $query .= $wpdb->prepare(
9892 ' WHERE event_period.eventPeriodId = %d',
9893 $event_selected
9894 );
9895 }
9896 }
9897 }
9898
9899 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
9900
9901 if ( ! empty( $result ) ) {
9902 $event = $wpdb->get_row(
9903 $wpdb->prepare(
9904 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
9905 [ $event_selected ]
9906 ),
9907 ARRAY_A
9908 );
9909 $event = isset( $event ) ? $event : [];
9910 $event_tags = $wpdb->get_results(
9911 $wpdb->prepare(
9912 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
9913 [ $event_selected ]
9914 ),
9915 ARRAY_A
9916 );
9917 $tags = [];
9918 if ( ! empty( $event_tags ) ) {
9919 foreach ( $event_tags as $key => $tag ) {
9920 $tags['event_tag'][ $key ] = $tag['name'];
9921 }
9922 } else {
9923 $tags = [];
9924 }
9925 $customer_result = $wpdb->get_row(
9926 $wpdb->prepare(
9927 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
9928 [ $result['customerId'] ]
9929 ),
9930 ARRAY_A
9931 );
9932 $customer_result = isset( $customer_result ) ? $customer_result : [];
9933 if ( $result['couponId'] ) {
9934 $coupon_result = $wpdb->get_row(
9935 $wpdb->prepare(
9936 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9937 [ $result['couponId'] ]
9938 ),
9939 ARRAY_A
9940 );
9941 } else {
9942 $coupon_result = [];
9943 }
9944 if ( ! empty( $result['customFields'] ) ) {
9945 $custom_fields = json_decode( $result['customFields'], true );
9946
9947 $fields_arr = [];
9948 foreach ( (array) $custom_fields as $fields ) {
9949 if ( is_array( $fields ) ) {
9950 $fields_arr[ $fields['label'] ] = $fields['value'];
9951 }
9952 }
9953 unset( $result['customFields'] );
9954 } else {
9955 $fields_arr = [];
9956 }
9957 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
9958 if ( -1 === $event_status ) {
9959 $context['pluggable_data']['event_booking_status'] = 'approved';
9960 } else {
9961 $context['pluggable_data']['event_booking_status'] = $event_status;
9962 }
9963 $context['response_type'] = 'live';
9964 } else {
9965
9966 $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 );
9967 }
9968
9969 return $context;
9970 }
9971
9972 /**
9973 * Get last data for trigger.
9974 *
9975 * @param array $data data.
9976 * @return array
9977 */
9978 public function search_amelia_new_event_attendee_triggers_last_data( $data ) {
9979 global $wpdb;
9980
9981 $context = [];
9982
9983 $event_selected = $data['filter']['amelia_events_list']['value'];
9984
9985 $query = 'SELECT *
9986 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
9987 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
9988 ON customer.id = event_period.customerBookingId';
9989
9990 if ( -1 === $event_selected ) {
9991 $query .= '
9992 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods )';
9993 } else {
9994 $event_periods_result = $wpdb->get_results(
9995 $wpdb->prepare(
9996 'SELECT id
9997 FROM ' . $wpdb->prefix . 'amelia_events_periods
9998 WHERE eventId = %d',
9999 [ $event_selected ]
10000 ),
10001 ARRAY_A
10002 );
10003 if ( ! empty( $event_periods_result ) ) {
10004 $ids = wp_list_pluck( $event_periods_result, 'id' );
10005 }
10006 if ( ! empty( $ids ) ) {
10007 $query .= $wpdb->prepare(
10008 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND event_period.eventPeriodId IN (%s)',
10009 implode( ',', $ids )
10010 );
10011 } else {
10012 $query .= $wpdb->prepare(
10013 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND event_period.eventPeriodId = %d',
10014 [ $event_selected ]
10015 );
10016 }
10017 }
10018 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
10019
10020 if ( ! empty( $result ) ) {
10021
10022 $event = $wpdb->get_row(
10023 $wpdb->prepare(
10024 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
10025 [ $event_selected ]
10026 ),
10027 ARRAY_A
10028 );
10029 $event = isset( $event ) ? $event : [];
10030 $event_tags = $wpdb->get_results(
10031 $wpdb->prepare(
10032 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
10033 [ $event_selected ]
10034 ),
10035 ARRAY_A
10036 );
10037 $tags = [];
10038 if ( ! empty( $event_tags ) ) {
10039 foreach ( $event_tags as $key => $tag ) {
10040 $tags['event_tag'][ $key ] = $tag['name'];
10041 }
10042 } else {
10043 $tags = [];
10044 }
10045
10046 $customer_result = $wpdb->get_row(
10047 $wpdb->prepare(
10048 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
10049 [ $result['customerId'] ]
10050 ),
10051 ARRAY_A
10052 );
10053 $customer_result = isset( $customer_result ) ? $customer_result : [];
10054
10055 if ( $result['couponId'] ) {
10056 $coupon_result = $wpdb->get_row(
10057 $wpdb->prepare(
10058 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
10059 [ $result['couponId'] ]
10060 ),
10061 ARRAY_A
10062 );
10063 } else {
10064 $coupon_result = [];
10065 }
10066
10067 if ( ! empty( $result['customFields'] ) ) {
10068 $custom_fields = json_decode( $result['customFields'], true );
10069
10070 $fields_arr = [];
10071 foreach ( (array) $custom_fields as $fields ) {
10072 if ( is_array( $fields ) ) {
10073 $fields_arr[ $fields['label'] ] = $fields['value'];
10074 }
10075 }
10076 unset( $result['customFields'] );
10077 } else {
10078 $fields_arr = [];
10079 }
10080
10081 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
10082 $context['response_type'] = 'live';
10083 } else {
10084
10085 $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 );
10086 }
10087
10088 return $context;
10089 }
10090
10091 /**
10092 * Get last data for trigger.
10093 *
10094 * @param array $data data.
10095 * @return array
10096 */
10097 public function search_amelia_event_booking_cancelled_triggers_last_data( $data ) {
10098 global $wpdb;
10099
10100 $context = [];
10101
10102 $event_selected = $data['filter']['amelia_events_list']['value'];
10103
10104 if ( -1 === $event_selected ) {
10105 $result = $wpdb->get_row(
10106 'SELECT *
10107 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
10108 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
10109 ON customer.id = event_period.customerBookingId
10110 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND customer.status = "canceled"',
10111 ARRAY_A
10112 );
10113 } else {
10114 $event_periods_result = $wpdb->get_results(
10115 $wpdb->prepare(
10116 'SELECT id
10117 FROM ' . $wpdb->prefix . 'amelia_events_periods
10118 WHERE eventId = %d',
10119 [ $event_selected ]
10120 ),
10121 ARRAY_A
10122 );
10123 if ( ! empty( $event_periods_result ) ) {
10124 $ids = wp_list_pluck( $event_periods_result, 'id' );
10125 }
10126 $query = 'SELECT *
10127 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
10128 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
10129 ON customer.id = event_period.customerBookingId';
10130 if ( ! empty( $ids ) ) {
10131 $query .= $wpdb->prepare(
10132 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId IN (%s) AND customer.status = "canceled"',
10133 implode( ',', $ids )
10134 );
10135 } else {
10136 $query .= $wpdb->prepare(
10137 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId = %d
10138 AND customer.status = "canceled"',
10139 [ $event_selected ]
10140 );
10141 }
10142 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
10143 }
10144
10145 if ( ! empty( $result ) ) {
10146 $event = $wpdb->get_row(
10147 $wpdb->prepare(
10148 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
10149 [ $event_selected ]
10150 ),
10151 ARRAY_A
10152 );
10153 $event = isset( $event ) ? $event : [];
10154 $event_tags = $wpdb->get_results(
10155 $wpdb->prepare(
10156 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
10157 [ $event_selected ]
10158 ),
10159 ARRAY_A
10160 );
10161 $tags = [];
10162 if ( ! empty( $event_tags ) ) {
10163 foreach ( $event_tags as $key => $tag ) {
10164 $tags['event_tag'][ $key ] = $tag['name'];
10165 }
10166 } else {
10167 $tags = [];
10168 }
10169 $customer_result = $wpdb->get_row(
10170 $wpdb->prepare(
10171 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
10172 [ $result['customerId'] ]
10173 ),
10174 ARRAY_A
10175 );
10176 $customer_result = isset( $customer_result ) ? $customer_result : [];
10177 if ( $result['couponId'] ) {
10178 $coupon_result = $wpdb->get_row(
10179 $wpdb->prepare(
10180 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
10181 [ $result['couponId'] ]
10182 ),
10183 ARRAY_A
10184 );
10185 } else {
10186 $coupon_result = [];
10187 }
10188 if ( ! empty( $result['customFields'] ) ) {
10189 $custom_fields = json_decode( $result['customFields'], true );
10190
10191 $fields_arr = [];
10192 foreach ( (array) $custom_fields as $fields ) {
10193 if ( is_array( $fields ) ) {
10194 $fields_arr[ $fields['label'] ] = $fields['value'];
10195 }
10196 }
10197 unset( $result['customFields'] );
10198 } else {
10199 $fields_arr = [];
10200 }
10201 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
10202 $context['response_type'] = 'live';
10203 } else {
10204
10205 $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 );
10206 }
10207
10208 return $context;
10209 }
10210
10211 /**
10212 * Get last data for trigger.
10213 *
10214 * @param array $data data.
10215 * @return array
10216 */
10217 public function search_amelia_appointment_rescheduled_triggers_last_data( $data ) {
10218 global $wpdb;
10219
10220 $context = [];
10221
10222 $appointment_category = $data['filter']['amelia_category_list']['value'];
10223 $appointment_service = $data['filter']['amelia_service_list']['value'];
10224
10225 if ( -1 === $appointment_service ) {
10226 // If service exists as per category selected.
10227 $service_exist = $wpdb->get_row(
10228 $wpdb->prepare(
10229 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
10230 [ $appointment_category ]
10231 ),
10232 ARRAY_A
10233 );
10234
10235 if ( empty( $service_exist ) ) {
10236 $result = [];
10237 } else {
10238 $result = $wpdb->get_row(
10239 '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 )',
10240 ARRAY_A
10241 );
10242 }
10243 } else {
10244 $result = $wpdb->get_row(
10245 $wpdb->prepare(
10246 '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',
10247 [ $appointment_service ]
10248 ),
10249 ARRAY_A
10250 );
10251 }
10252
10253 if ( ! empty( $result ) ) {
10254
10255 $payment_result = $wpdb->get_row(
10256 $wpdb->prepare(
10257 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
10258 [ $result['id'] ]
10259 ),
10260 ARRAY_A
10261 );
10262
10263 $customer_result = $wpdb->get_row(
10264 $wpdb->prepare(
10265 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
10266 [ $result['customerId'] ]
10267 ),
10268 ARRAY_A
10269 );
10270
10271 $service_result = $wpdb->get_row(
10272 $wpdb->prepare(
10273 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
10274 [ $result['serviceId'] ]
10275 ),
10276 ARRAY_A
10277 );
10278
10279 $category_result = $wpdb->get_row(
10280 $wpdb->prepare(
10281 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
10282 [ $service_result['categoryId'] ]
10283 ),
10284 ARRAY_A
10285 );
10286
10287 if ( $result['couponId'] ) {
10288 $coupon_result = $wpdb->get_row(
10289 $wpdb->prepare(
10290 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
10291 [ $result['couponId'] ]
10292 ),
10293 ARRAY_A
10294 );
10295 } else {
10296 $coupon_result = [];
10297 }
10298
10299 if ( ! empty( $result['customFields'] ) ) {
10300 $custom_fields = json_decode( $result['customFields'], true );
10301
10302 $fields_arr = [];
10303 foreach ( (array) $custom_fields as $fields ) {
10304 if ( is_array( $fields ) ) {
10305 $fields_arr[ $fields['label'] ] = $fields['value'];
10306 }
10307 }
10308 unset( $result['customFields'] );
10309 } else {
10310 $fields_arr = [];
10311 }
10312
10313 $appointment_data['isRescheduled'] = '1';
10314 $context['pluggable_data'] = array_merge( $result, $fields_arr, $appointment_data, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
10315 $context['response_type'] = 'live';
10316 } else {
10317
10318 $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 );
10319 }
10320
10321 return $context;
10322 }
10323
10324
10325 /**
10326 * Get last data for trigger.
10327 *
10328 * @param array $data data.
10329 * @return array
10330 */
10331 public function search_amelia_appointment_cancelled_triggers_last_data( $data ) {
10332 global $wpdb;
10333
10334 $context = [];
10335
10336 $appointment_category = $data['filter']['amelia_category_list']['value'];
10337 $appointment_service = $data['filter']['amelia_service_list']['value'];
10338
10339 if ( -1 === $appointment_service ) {
10340 // If service exists as per category selected.
10341 $service_exist = $wpdb->get_row(
10342 $wpdb->prepare(
10343 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
10344 [ $appointment_category ]
10345 ),
10346 ARRAY_A
10347 );
10348
10349 if ( empty( $service_exist ) ) {
10350 $result = [];
10351 } else {
10352 $result = $wpdb->get_row(
10353 $wpdb->prepare(
10354 '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',
10355 [ 'canceled' ]
10356 ),
10357 ARRAY_A
10358 );
10359 }
10360 } else {
10361 $result = $wpdb->get_row(
10362 $wpdb->prepare(
10363 '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',
10364 [ 'canceled', $appointment_service ]
10365 ),
10366 ARRAY_A
10367 );
10368 }
10369
10370 if ( ! empty( $result ) ) {
10371
10372 $payment_result = $wpdb->get_row(
10373 $wpdb->prepare(
10374 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
10375 [ $result['id'] ]
10376 ),
10377 ARRAY_A
10378 );
10379
10380 $customer_result = $wpdb->get_row(
10381 $wpdb->prepare(
10382 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
10383 [ $result['customerId'] ]
10384 ),
10385 ARRAY_A
10386 );
10387
10388 $service_result = $wpdb->get_row(
10389 $wpdb->prepare(
10390 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
10391 [ $result['serviceId'] ]
10392 ),
10393 ARRAY_A
10394 );
10395
10396 $category_result = $wpdb->get_row(
10397 $wpdb->prepare(
10398 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
10399 [ $service_result['categoryId'] ]
10400 ),
10401 ARRAY_A
10402 );
10403
10404 if ( $result['couponId'] ) {
10405 $coupon_result = $wpdb->get_row(
10406 $wpdb->prepare(
10407 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
10408 [ $result['couponId'] ]
10409 ),
10410 ARRAY_A
10411 );
10412 } else {
10413 $coupon_result = [];
10414 }
10415
10416 if ( ! empty( $result['customFields'] ) ) {
10417 $custom_fields = json_decode( $result['customFields'], true );
10418
10419 $fields_arr = [];
10420 foreach ( (array) $custom_fields as $fields ) {
10421 if ( is_array( $fields ) ) {
10422 $fields_arr[ $fields['label'] ] = $fields['value'];
10423 }
10424 }
10425 unset( $result['customFields'] );
10426 } else {
10427 $fields_arr = [];
10428 }
10429
10430 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
10431 $context['response_type'] = 'live';
10432 } else {
10433
10434 $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 );
10435 }
10436
10437 return $context;
10438 }
10439
10440 /**
10441 * Get MailPoet Forms.
10442 *
10443 * @param array $data data.
10444 *
10445 * @return array
10446 */
10447 public function search_mailpoet_forms( $data ) {
10448 if ( ! class_exists( '\MailPoet\API\API' ) ) {
10449 return [];
10450 }
10451
10452 global $wpdb;
10453
10454 $page = $data['page'];
10455 $limit = Utilities::get_search_page_limit();
10456 $offset = $limit * ( $page - 1 );
10457
10458 $forms = $wpdb->get_results(
10459 $wpdb->prepare(
10460 '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',
10461 [ 'enabled', $limit, $offset ]
10462 )
10463 );
10464
10465 $form_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
10466
10467 $options = [];
10468
10469 if ( ! empty( $forms ) ) {
10470 if ( is_array( $forms ) ) {
10471 foreach ( $forms as $form ) {
10472 $options[] = [
10473 'label' => $form->name,
10474 'value' => $form->id,
10475 ];
10476 }
10477 }
10478 }
10479
10480 return [
10481 'options' => $options,
10482 'hasMore' => $form_count > $limit && $form_count > $offset,
10483 ];
10484
10485 }
10486
10487 /**
10488 * Get MailPoet List.
10489 *
10490 * @param array $data data.
10491 *
10492 * @return array
10493 */
10494 public function search_mailpoet_list( $data ) {
10495 if ( ! class_exists( '\MailPoet\API\API' ) ) {
10496 return [];
10497 }
10498
10499 $mailpoet = \MailPoet\API\API::MP( 'v1' );
10500 $lists = $mailpoet->getLists();
10501
10502 $options = [];
10503
10504 if ( ! empty( $lists ) ) {
10505 if ( is_array( $lists ) ) {
10506 foreach ( $lists as $list ) {
10507 $options[] = [
10508 'label' => $list['name'],
10509 'value' => $list['id'],
10510 ];
10511 }
10512 }
10513 }
10514
10515 return [
10516 'options' => $options,
10517 'hasMore' => false,
10518 ];
10519 }
10520
10521 /**
10522 * Get MailPoet Subscriber Status.
10523 *
10524 * @param array $data data.
10525 *
10526 * @return array
10527 */
10528 public function search_mailpoet_subscriber_status( $data ) {
10529 if ( ! class_exists( '\MailPoet\API\API' ) ) {
10530 return [];
10531 }
10532
10533 $subscriber_status = [
10534 'subscribed' => 'Subscribed',
10535 'unconfirmed' => 'Unconfirmed',
10536 'unsubscribed' => 'Unsubscribed',
10537 'inactive' => 'Inactive',
10538 'bounced' => 'Bounced',
10539 ];
10540
10541 $options = [];
10542 foreach ( $subscriber_status as $key => $status ) {
10543 $options[] = [
10544 'label' => $status,
10545 'value' => $key,
10546 ];
10547 }
10548
10549 return [
10550 'options' => $options,
10551 'hasMore' => false,
10552 ];
10553 }
10554
10555 /**
10556 * Get MailPoet Subscribers.
10557 *
10558 * @param array $data data.
10559 *
10560 * @return array
10561 */
10562 public function search_mailpoet_subscribers( $data ) {
10563 if ( ! class_exists( '\MailPoet\API\API' ) ) {
10564 return [];
10565 }
10566
10567 global $wpdb;
10568
10569 $page = $data['page'];
10570 $limit = Utilities::get_search_page_limit();
10571 $offset = $limit * ( $page - 1 );
10572
10573 $subscribers = $wpdb->get_results(
10574 $wpdb->prepare(
10575 'SELECT SQL_CALC_FOUND_ROWS id,email FROM ' . $wpdb->prefix . 'mailpoet_subscribers ORDER BY id DESC LIMIT %d OFFSET %d',
10576 [ $limit, $offset ]
10577 )
10578 );
10579
10580 $subscribers_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
10581
10582 $options = [];
10583
10584 if ( ! empty( $subscribers ) ) {
10585 if ( is_array( $subscribers ) ) {
10586 foreach ( $subscribers as $subscriber ) {
10587 $options[] = [
10588 'label' => $subscriber->email,
10589 'value' => $subscriber->id,
10590 ];
10591 }
10592 }
10593 }
10594
10595 return [
10596 'options' => $options,
10597 'hasMore' => $subscribers_count > $limit && $subscribers_count > $offset,
10598 ];
10599 }
10600
10601 /**
10602 * Get ConvertPro Forms.
10603 *
10604 * @param array $data data.
10605 *
10606 * @return array
10607 */
10608 public function search_convertpro_form_list( $data ) {
10609 if ( ! class_exists( '\Cp_V2_Loader' ) || ! class_exists( 'CP_V2_Popups' ) ) {
10610 return [];
10611 }
10612
10613 $cp_popups_inst = CP_V2_Popups::get_instance();
10614 $popups = $cp_popups_inst->get_all();
10615
10616 $form_count = count( $popups );
10617
10618 $page = $data['page'];
10619 $limit = Utilities::get_search_page_limit();
10620 $offset = $limit * ( $page - 1 );
10621
10622 $options = [];
10623
10624 if ( ! empty( $popups ) ) {
10625 if ( is_array( $popups ) ) {
10626 foreach ( $popups as $form ) {
10627 $options[] = [
10628 'label' => $form->post_title,
10629 'value' => $form->ID,
10630 ];
10631 }
10632 }
10633 }
10634
10635 return [
10636 'options' => $options,
10637 'hasMore' => $form_count > $limit && $form_count > $offset,
10638 ];
10639
10640 }
10641
10642 /**
10643 * Get ProjectHuddle Websites.
10644 *
10645 * @param array $data data.
10646 *
10647 * @return array
10648 */
10649 public function search_project_huddle_websites( $data ) {
10650
10651 $sites = new WP_Query(
10652 [
10653 'post_type' => 'ph-website',
10654 'posts_per_page' => - 1,
10655 'fields' => 'ids',
10656 ]
10657 );
10658
10659 $site_ids = (array) $sites->posts;
10660
10661 $options = [];
10662 if ( ! empty( $site_ids ) ) {
10663 if ( is_array( $site_ids ) ) {
10664 foreach ( $site_ids as $site_id ) {
10665 $options[] = [
10666 'label' => get_the_title( $site_id ),
10667 'value' => $site_id,
10668 ];
10669 }
10670 }
10671 }
10672 return [
10673 'options' => $options,
10674 'hasMore' => false,
10675 ];
10676
10677 }
10678
10679 /**
10680 * Get last data for trigger.
10681 *
10682 * @param array $data data.
10683 * @return mixed
10684 */
10685 public function search_project_huddle_comment_triggers_last_data( $data ) {
10686 global $wpdb;
10687
10688 $context = [];
10689 if ( ! class_exists( 'PH\Models\Post' ) || ! function_exists( 'ph_get_the_title' ) ) {
10690 return [];
10691 }
10692
10693 if ( -1 !== $data['dynamic'] ) {
10694 $threads = get_posts(
10695 [
10696 'post_type' => 'phw_comment_loc',
10697 'posts_per_page' => 1,
10698 'meta_value' => $data['dynamic'],
10699 'meta_key' => 'project_id',
10700 'orderby' => 'asc',
10701 ]
10702 );
10703 } else {
10704 $threads = [];
10705 }
10706
10707 if ( ! empty( $threads ) ) {
10708 $comment_result = $wpdb->get_row(
10709 $wpdb->prepare(
10710 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
10711 FROM ' . $wpdb->prefix . 'comments
10712 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
10713 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
10714 LIMIT 0,1',
10715 $threads[0]->ID
10716 ),
10717 ARRAY_A
10718 );
10719
10720 if ( ! empty( $comment_result ) ) {
10721 $comment_id = get_comment( $comment_result['comment_ID'], ARRAY_A );
10722 $comments['comment_ID'] = isset( $comment_id['comment_ID'] ) ? $comment_id['comment_ID'] : '';
10723 $comments['comment_post_ID'] = isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : '';
10724
10725 $comments['comment_author'] = isset( $comment_id['comment_author'] ) ? $comment_id['comment_author'] : '';
10726
10727 $comments['comment_author_email'] = isset( $comment_id['comment_author_email'] ) ? $comment_id['comment_author_email'] : '';
10728
10729 $comments['comment_date'] = isset( $comment_id['comment_date'] ) ? $comment_id['comment_date'] : '';
10730
10731 $comments['comment_content'] = isset( $comment_id['comment_content'] ) ? $comment_id['comment_content'] : '';
10732
10733 $comments['comment_type'] = isset( $comment_id['comment_type'] ) ? $comment_id['comment_type'] : '';
10734
10735 $comment_item_id = get_comment_meta( $comment_result['comment_ID'], 'item_id' );
10736 if ( ! empty( $comment_item_id ) && is_array( $comment_item_id ) ) {
10737 $comments['comment_item_id'] = $comment_item_id[0];
10738 $comments['comment_item_page_title'] = get_the_title( (int) $comment_item_id[0] );
10739 $comments['comment_item_page_url'] = get_post_meta( (int) $comment_item_id[0], 'page_url', true );
10740 }
10741 if ( isset( $comment_id['comment_post_ID'] ) || isset( $comment_id['comment_author'] ) ) {
10742 $comments['ph_project_name'] = ph_get_the_title( Post::get( $comment_id['comment_post_ID'] )->parentsIds()['project'] );
10743 $comments['ph_commenter_name'] = $comment_id['comment_author'];
10744 $comments['ph_project_type'] = ( get_post_type( $comment_id['comment_post_ID'] ) == 'ph-website' ) ? __( 'Website', 'suretriggers' ) : __( 'Mockup', 'suretriggers' );
10745 $comments['ph_action_status'] = get_post_meta( $comment_id['comment_post_ID'], 'resolved', true ) ? __( 'Resolved', 'suretriggers' ) : __( 'Unresolved', 'suretriggers' );
10746 $comments['ph_project_link'] = get_the_guid( $comment_id['comment_post_ID'] );
10747 }
10748
10749 $context['pluggable_data'] = $comments;
10750 $context['response_type'] = 'live';
10751 } else {
10752 $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 );
10753 }
10754 } else {
10755 $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 );
10756 }
10757
10758 return $context;
10759 }
10760
10761 /**
10762 * Get last data for trigger.
10763 *
10764 * @param array $data data.
10765 * @return mixed
10766 */
10767 public function search_project_huddle_resolved_comment_triggers_last_data( $data ) {
10768 global $wpdb;
10769
10770 $context = [];
10771 if ( ! class_exists( 'PH\Models\Post' ) || ! function_exists( 'ph_get_the_title' ) ) {
10772 return [];
10773 }
10774
10775 $get_comments = $wpdb->get_row(
10776 'SELECT ' . $wpdb->prefix . 'comments.comment_ID, ' . $wpdb->prefix . 'comments.comment_content
10777 FROM ' . $wpdb->prefix . 'comments
10778 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_status") AND comment_content = "Resolved"
10779 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
10780 LIMIT 0,1'
10781 );
10782
10783 if ( ! empty( $get_comments ) ) {
10784 $comment_id = get_comment( $get_comments->comment_ID, ARRAY_A );
10785 $comment_result = $wpdb->get_row(
10786 $wpdb->prepare(
10787 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
10788 FROM ' . $wpdb->prefix . 'comments
10789 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
10790 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
10791 LIMIT 0,1',
10792 isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : ''
10793 ),
10794 ARRAY_A
10795 );
10796
10797 $actual_comment = get_comment( $comment_result['comment_ID'], ARRAY_A );
10798 $comments['comment_ID'] = isset( $actual_comment['comment_ID'] ) ? $actual_comment['comment_ID'] : '';
10799 $comments['comment_post_ID'] = isset( $actual_comment['comment_post_ID'] ) ? $actual_comment['comment_post_ID'] : '';
10800 $comments['comment_author'] = isset( $actual_comment['comment_author'] ) ? $actual_comment['comment_author'] : '';
10801 $comments['comment_author_email'] = isset( $actual_comment['comment_author_email'] ) ? $actual_comment['comment_author_email'] : '';
10802 $comments['comment_date'] = isset( $actual_comment['comment_date'] ) ? $actual_comment['comment_date'] : '';
10803 $comments['comment_content'] = isset( $actual_comment['comment_content'] ) ? $actual_comment['comment_content'] : '';
10804 $comments['comment_type'] = isset( $actual_comment['comment_type'] ) ? $actual_comment['comment_type'] : '';
10805 $comments['comment_status'] = $get_comments->comment_content;
10806 $comment_item_id = get_comment_meta( $comment_result['comment_ID'], 'item_id' );
10807 if ( ! empty( $comment_item_id ) && is_array( $comment_item_id ) ) {
10808 $comments['comment_item_id'] = $comment_item_id[0];
10809 $comments['comment_item_page_title'] = get_the_title( (int) $comment_item_id[0] );
10810 $comments['comment_item_page_url'] = get_post_meta( (int) $comment_item_id[0], 'page_url', true );
10811 }
10812 if ( isset( $comment_id['comment_post_ID'] ) || isset( $comment_id['comment_author'] ) ) {
10813 $comments['ph_project_name'] = ph_get_the_title( Post::get( $comment_id['comment_post_ID'] )->parentsIds()['project'] );
10814 $comments['ph_commenter_name'] = $comment_id['comment_author'];
10815 $comments['ph_project_type'] = ( get_post_type( $comment_id['comment_post_ID'] ) == 'ph-website' ) ? __( 'Website', 'suretriggers' ) : __( 'Mockup', 'suretriggers' );
10816 $comments['ph_action_status'] = get_post_meta( $comment_id['comment_post_ID'], 'resolved', true ) ? __( 'Resolved', 'suretriggers' ) : __( 'Unresolved', 'suretriggers' );
10817 $comments['ph_project_link'] = get_the_guid( $comment_id['comment_post_ID'] );
10818 }
10819 $context['pluggable_data'] = $comments;
10820 $context['response_type'] = 'live';
10821 } else {
10822 $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 );
10823 }
10824
10825 return $context;
10826 }
10827
10828 /**
10829 * Get MasterStudy LMS Courses.
10830 *
10831 * @param array $data data.
10832 *
10833 * @return array
10834 */
10835 public function search_ms_lms_courses( $data ) {
10836
10837 $page = $data['page'];
10838 $limit = Utilities::get_search_page_limit();
10839 $offset = $limit * ( $page - 1 );
10840
10841 $args = [
10842 'post_type' => 'stm-courses',
10843 'posts_per_page' => $limit,
10844 'offset' => $offset,
10845 'orderby' => 'title',
10846 'order' => 'ASC',
10847 'post_status' => 'publish',
10848 ];
10849
10850 $courses = get_posts( $args );
10851
10852 $course_count = wp_count_posts( 'stm-courses' )->publish;
10853
10854 $options = [];
10855 if ( ! empty( $courses ) ) {
10856 if ( is_array( $courses ) ) {
10857 foreach ( $courses as $course ) {
10858 $options[] = [
10859 'label' => $course->post_title,
10860 'value' => $course->ID,
10861 ];
10862 }
10863 }
10864 }
10865 return [
10866 'options' => $options,
10867 'hasMore' => $course_count > $limit && $course_count > $offset,
10868 ];
10869
10870 }
10871
10872 /**
10873 * Get MasterStudy LMS Lessons.
10874 *
10875 * @param array $data data.
10876 *
10877 * @return array
10878 */
10879 public function search_ms_lms_lessons( $data ) {
10880
10881 global $wpdb;
10882 $options = [];
10883 $course_id = $data['dynamic'];
10884
10885 // Use curriculum repository class.
10886 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
10887 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
10888 } else {
10889 $curriculum_repo = false;
10890 }
10891
10892 if ( '-1' === $course_id ) {
10893 $lessons = $wpdb->get_results(
10894 $wpdb->prepare(
10895 "SELECT SQL_CALC_FOUND_ROWS ID, post_title FROM $wpdb->posts WHERE post_type = %s ORDER BY post_title ASC",
10896 'stm-lessons'
10897 )
10898 );
10899
10900 if ( ! empty( $lessons ) ) {
10901 if ( is_array( $lessons ) ) {
10902 foreach ( $lessons as $lesson ) {
10903 $options[] = [
10904 'label' => $lesson->post_title,
10905 'value' => $lesson->ID,
10906 ];
10907 }
10908 }
10909 }
10910 } else {
10911 $lessons = [];
10912 if ( $curriculum_repo ) {
10913 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
10914 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
10915 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
10916 foreach ( $curriculum['materials'] as $material ) {
10917 if ( 'stm-lessons' === $material['post_type'] ) {
10918 $lessons[] = [
10919 'value' => $material['post_id'],
10920 'text' => $material['title'],
10921 ];
10922 }
10923 }
10924 }
10925 }
10926 if ( ! empty( $lessons ) ) {
10927 if ( is_array( $lessons ) ) {
10928 foreach ( $lessons as $lesson ) {
10929 $options[] = [
10930 'label' => $lesson['text'],
10931 'value' => $lesson['value'],
10932 ];
10933 }
10934 }
10935 }
10936 } else {
10937 $lessons = $wpdb->get_results(
10938 $wpdb->prepare(
10939 "SELECT SQL_CALC_FOUND_ROWS ID, post_title
10940 FROM $wpdb->posts
10941 WHERE FIND_IN_SET(
10942 ID,
10943 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
10944 )
10945 AND post_type = 'stm-lessons'
10946 ORDER BY post_title ASC",
10947 absint( $course_id )
10948 )
10949 );
10950 if ( ! empty( $lessons ) ) {
10951 if ( is_array( $lessons ) ) {
10952 foreach ( $lessons as $lesson ) {
10953 $options[] = [
10954 'label' => $lesson->post_title,
10955 'value' => $lesson->ID,
10956 ];
10957 }
10958 }
10959 }
10960 }
10961 }
10962 return [
10963 'options' => $options,
10964 'hasMore' => false,
10965 ];
10966
10967 }
10968
10969 /**
10970 * Get MasterStudy LMS Quiz.
10971 *
10972 * @param array $data data.
10973 *
10974 * @return array
10975 */
10976 public function search_ms_lms_quiz( $data ) {
10977
10978 global $wpdb;
10979
10980 $options = [];
10981 $course_id = $data['dynamic'];
10982
10983 // Use curriculum repository class.
10984 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
10985 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
10986 } else {
10987 $curriculum_repo = false;
10988 }
10989
10990 if ( '-1' === $course_id ) {
10991 $quizzes = $wpdb->get_results(
10992 $wpdb->prepare(
10993 "SELECT ID, post_title
10994 FROM $wpdb->posts
10995 WHERE post_type = %s
10996 ORDER BY post_title ASC",
10997 'stm-quizzes'
10998 )
10999 );
11000 if ( ! empty( $quizzes ) ) {
11001 if ( is_array( $quizzes ) ) {
11002 foreach ( $quizzes as $quiz ) {
11003 $options[] = [
11004 'label' => $quiz->post_title,
11005 'value' => $quiz->ID,
11006 ];
11007 }
11008 }
11009 }
11010 } else {
11011 $quizzes = [];
11012 if ( $curriculum_repo ) {
11013 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
11014 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
11015 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
11016 foreach ( $curriculum['materials'] as $material ) {
11017 if ( 'stm-quizzes' === $material['post_type'] ) {
11018 $quizzes[] = [
11019 'value' => $material['post_id'],
11020 'text' => $material['title'],
11021 ];
11022 }
11023 }
11024 }
11025 }
11026 if ( ! empty( $quizzes ) ) {
11027 if ( is_array( $quizzes ) ) {
11028 foreach ( $quizzes as $quiz ) {
11029 $options[] = [
11030 'label' => $quiz['text'],
11031 'value' => $quiz['value'],
11032 ];
11033 }
11034 }
11035 }
11036 } else {
11037 $quizzes = $wpdb->get_results(
11038 $wpdb->prepare(
11039 "SELECT ID, post_title
11040 FROM $wpdb->posts
11041 WHERE FIND_IN_SET(
11042 ID,
11043 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
11044 )
11045 AND post_type = 'stm-quizzes'
11046 ORDER BY post_title ASC
11047 ",
11048 absint( $course_id )
11049 )
11050 );
11051 if ( ! empty( $quizzes ) ) {
11052 if ( is_array( $quizzes ) ) {
11053 foreach ( $quizzes as $quiz ) {
11054 $options[] = [
11055 'label' => $quiz->post_title,
11056 'value' => $quiz->ID,
11057 ];
11058 }
11059 }
11060 }
11061 }
11062 }
11063 return [
11064 'options' => $options,
11065 'hasMore' => false,
11066 ];
11067
11068 }
11069
11070 /**
11071 * Search MasterStudy LMS data.
11072 *
11073 * @param array $data data.
11074 * @return array|void
11075 */
11076 public function search_ms_lms_last_data( $data ) {
11077 global $wpdb;
11078 $post_type = $data['post_type'];
11079 $trigger = $data['search_term'];
11080 $context = [];
11081
11082 if ( 'stm_lms_course_completed' === $trigger ) {
11083 $post_id = $data['filter']['course']['value'];
11084 if ( -1 === $post_id ) {
11085 $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 ) );
11086 } else {
11087 $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 ) );
11088 }
11089 } elseif ( 'stm_lesson_passed' === $trigger ) {
11090 $post_id = $data['filter']['lesson']['value'];
11091 if ( -1 === $post_id ) {
11092 $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 ) );
11093 } else {
11094 $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 ) );
11095 }
11096 } elseif ( 'stm_quiz_passed' === $trigger ) {
11097 $post_id = $data['filter']['quiz']['value'];
11098 if ( -1 === $post_id ) {
11099 $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 ) );
11100 } else {
11101 $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 ) );
11102 }
11103 } elseif ( 'stm_quiz_failed' === $trigger ) {
11104 $post_id = $data['filter']['quiz']['value'];
11105 if ( -1 === $post_id ) {
11106 $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 ) );
11107 } else {
11108 $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 ) );
11109 }
11110 } elseif ( 'stm_lms_user_enroll_course' === $trigger ) {
11111 $post_id = $data['filter']['course']['value'];
11112 if ( -1 === $post_id ) {
11113 $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 ) );
11114 } else {
11115 $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 ) );
11116 }
11117 }
11118
11119 if ( ! empty( $result ) ) {
11120
11121 switch ( $trigger ) {
11122 case 'stm_lms_course_completed':
11123 $result_course_id = $result[0]->course_id;
11124 $result_user_id = $result[0]->user_id;
11125 $course = get_the_title( $result_course_id );
11126 $course_link = get_the_permalink( $result_course_id );
11127 $featured_image = get_the_post_thumbnail_url( $result_course_id );
11128
11129 $data = [
11130 'course_id' => $result_course_id,
11131 'course_title' => $course,
11132 'course_link' => $course_link,
11133 'course_featured_image' => $featured_image,
11134 'course_progress' => $result[0]->progress_percent,
11135 ];
11136 $context_data = array_merge(
11137 WordPress::get_user_context( $result_user_id ),
11138 $data
11139 );
11140 break;
11141 case 'stm_lesson_passed':
11142 $result_lesson_id = $result[0]->lesson_id;
11143 $result_user_id = $result[0]->user_id;
11144 $lesson = get_the_title( $result_lesson_id );
11145 $lesson_link = get_the_permalink( $result_lesson_id );
11146
11147 $data = [
11148 'lesson_id' => $result_lesson_id,
11149 'lesson_title' => $lesson,
11150 'lesson_link' => $lesson_link,
11151 ];
11152 $context_data = array_merge(
11153 WordPress::get_user_context( $result_user_id ),
11154 $data
11155 );
11156 break;
11157 case 'stm_quiz_passed':
11158 $result_quiz_id = $result[0]->quiz_id;
11159 $result_user_id = $result[0]->user_id;
11160 $quiz_title = get_the_title( $result_quiz_id );
11161 $quiz_link = get_the_permalink( $result_quiz_id );
11162
11163 $data = [
11164 'quiz_id' => $result_quiz_id,
11165 'quiz_title' => $quiz_title,
11166 'quiz_link' => $quiz_link,
11167 'quiz_score' => $result[0]->progress,
11168 'result' => 'passed',
11169 ];
11170 $context_data = array_merge(
11171 WordPress::get_user_context( $result_user_id ),
11172 $data
11173 );
11174 break;
11175 case 'stm_quiz_failed':
11176 $result_quiz_id = $result[0]->quiz_id;
11177 $result_user_id = $result[0]->user_id;
11178 $quiz_title = get_the_title( $result_quiz_id );
11179 $quiz_link = get_the_permalink( $result_quiz_id );
11180
11181 $data = [
11182 'quiz_id' => $result_quiz_id,
11183 'quiz_title' => $quiz_title,
11184 'quiz_link' => $quiz_link,
11185 'quiz_score' => $result[0]->progress,
11186 'result' => 'failed',
11187 ];
11188 $context_data = array_merge(
11189 WordPress::get_user_context( $result_user_id ),
11190 $data
11191 );
11192 break;
11193 case 'stm_lms_user_enroll_course':
11194 $result_course_id = $result[0]->course_id;
11195 $result_user_id = $result[0]->user_id;
11196
11197 $course = get_the_title( $result_course_id );
11198 $course_link = get_the_permalink( $result_course_id );
11199 $featured_image = get_the_post_thumbnail_url( $result_course_id );
11200
11201 $data = [
11202 'course_id' => $result_course_id,
11203 'course_title' => $course,
11204 'course_link' => $course_link,
11205 'course_featured_image' => $featured_image,
11206 ];
11207 $context_data = array_merge(
11208 WordPress::get_user_context( $result_user_id ),
11209 $data
11210 );
11211 break;
11212 default:
11213 return;
11214 }
11215 $context['pluggable_data'] = $context_data;
11216 $context['response_type'] = 'live';
11217 }
11218
11219 return $context;
11220
11221 }
11222
11223 /**
11224 * Prepare Fluent Support Mailbox list.
11225 *
11226 * @param array $data data.
11227 *
11228 * @return array
11229 */
11230 public function search_fs_mailbox_list( $data ) {
11231 $options = [];
11232 global $wpdb;
11233 $mailboxes = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_mail_boxes ORDER BY id DESC", ARRAY_A );
11234 if ( ! empty( $mailboxes ) ) {
11235 foreach ( $mailboxes as $key => $value ) {
11236
11237 $options[] = [
11238 'label' => $value['name'],
11239 'value' => $value['id'],
11240 ];
11241
11242 }
11243 }
11244
11245 return [
11246 'options' => $options,
11247 'hasMore' => false,
11248 ];
11249 }
11250
11251 /**
11252 * Prepare Fluent Support Product list.
11253 *
11254 * @param array $data data.
11255 *
11256 * @return array
11257 */
11258 public function search_fs_product_list( $data ) {
11259 $options = [];
11260
11261 if ( ! class_exists( '\FluentSupport\App\Models\Product' ) ) {
11262 return [];
11263 }
11264
11265 $products = \FluentSupport\App\Models\Product::select( [ 'id', 'title' ] )->get();
11266 if ( ! empty( $products ) ) {
11267 foreach ( $products as $key => $product ) {
11268 $options[] = [
11269 'label' => $product['title'],
11270 'value' => $product['id'],
11271 ];
11272 }
11273 }
11274
11275 return [
11276 'options' => $options,
11277 'hasMore' => false,
11278 ];
11279 }
11280
11281 /**
11282 * Prepare Fluent Support Product list.
11283 *
11284 * @param array $data data.
11285 *
11286 * @return array
11287 */
11288 public function search_fs_person_type_list( $data ) {
11289 $options = [
11290 [
11291 'label' => 'Customer',
11292 'value' => 'customer',
11293 ],
11294 [
11295 'label' => 'Agent',
11296 'value' => 'agent',
11297 ],
11298 ];
11299
11300 return [
11301 'options' => $options,
11302 'hasMore' => false,
11303 ];
11304 }
11305
11306 /**
11307 * Prepare FluentSupport Agents List.
11308 *
11309 * @param array $data Search Params.
11310 * @return array
11311 */
11312 public function search_fluent_support_agents_list( $data ) {
11313 global $wpdb;
11314
11315 $agents = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent' ", ARRAY_A );
11316
11317 $options = [];
11318
11319 if ( ! empty( $agents ) ) {
11320 foreach ( $agents as $agent ) {
11321 $options[] = [
11322 'label' => $agent['email'],
11323 'value' => $agent['id'],
11324 ];
11325 }
11326 }
11327
11328 return [
11329 'options' => $options,
11330 'hasMore' => false,
11331 ];
11332 }
11333
11334 /**
11335 * Get last data for trigger.
11336 *
11337 * @param array $data data.
11338 * @return array
11339 */
11340 public function search_fluent_support_triggers_last_data( $data ) {
11341 global $wpdb;
11342 $context = [];
11343 $context['response_type'] = 'sample';
11344
11345 if ( ! class_exists( '\FluentSupport\App\Models\Ticket' ) ) {
11346 return [];
11347 }
11348
11349 $ticket_data = [
11350 'id' => '1',
11351 'customer_id' => '2',
11352 'agent_id' => '3',
11353 'product_id' => '5',
11354 'product_source' => 'local',
11355 'privacy' => 'private',
11356 'priority' => 'normal',
11357 'client_priority' => 'medium',
11358 'status' => 'active',
11359 'title' => 'Sample Ticket Title',
11360 'slug' => 'sample-ticket-title',
11361 'hash' => 'f8a8cfb946',
11362 'content_hash' => 'd65500d62621be8b493c22b1d888052c',
11363 'content' => '<p>Sample content.</p>',
11364 'last_customer_response' => '2023-04-27 07:30:46',
11365 'waiting_since' => '2023-04-27 07:30:46',
11366 'response_count' => '2',
11367 'total_close_time' => '7042',
11368 'resolved at' => '2023-04-27 09:28:08',
11369 'closed_by' => '1',
11370 'created_at' => '2023-04-27 07:30:46',
11371 'updated_at' => '2023-04-27 10:28:08',
11372 'mailbox_id' => '1',
11373 'mailbox_name' => 'OttoKit',
11374 'mailbox_slug' => 'suretriggers',
11375 'mailbox_box_type' => 'web',
11376 'mailbox email' => 'john_doe@sample.com',
11377 'mailbox_settings_admin_email_address' => 'john_doe@sample.com',
11378 'mailbox_created_by' => '1',
11379 'mailbox_is_default' => 'yes',
11380 'mailbox_created_at' => '2023-04-26 06:29:01',
11381 'mailbox_updated_at' => '2023-04-26 06:29:01',
11382 ];
11383
11384 $customer_data = [
11385 'id' => '1',
11386 'first_name' => 'John',
11387 'last_name' => 'Doe',
11388 'email' => 'john_doe@sample.com',
11389 'person_type' => 'agent',
11390 'status' => 'active',
11391 'hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
11392 'user_id' => '1',
11393 'created_at' => '2023-04-27 07:30:46',
11394 'updated_at' => '2023-04-27 10:28:08',
11395 'full_name' => 'John Doe',
11396 'photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
11397 ];
11398
11399 $reply_data = [
11400 'ticket_id' => '1',
11401 'conversation_type' => 'response',
11402 'content' => '<p>Sample content.</p>',
11403 'source' => 'web',
11404 'content_hash' => '2cc0e35d8fb92a0675d67999b073b3a4',
11405 'created_at' => '2023-04-27 07:30:46',
11406 'updated_at' => '2023-04-27 10:28:08',
11407 'id' => '1',
11408 'person_id' => '2',
11409 'person_first_name' => 'John',
11410 'person_last_name' => 'Doe',
11411 'person_email' => 'john_doe@sample.com',
11412 'person_person_type' => 'agent',
11413 'person_status' => 'active',
11414 'person_hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
11415 'person_user_id' => '1',
11416 'person_created_at' => '2023-04-27 07:30:46',
11417 'person_updated_at' => '2023-04-27 10:28:08',
11418 'person_full_name' => 'John Doe',
11419 'person_photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
11420 ];
11421
11422 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
11423 $mailbox = isset( $data['mailbox_id'] ) ? $data['mailbox_id'] : '';
11424 $product_id = isset( $data['filter']['ticket_product_id']['value'] ) ? $data['filter']['ticket_product_id']['value'] : '';
11425 $person_id = isset( $data['filter']['person_id']['value'] ) ? (string) $data['filter']['person_id']['value'] : '';
11426
11427 if ( in_array( $term, [ 'response_added_by_agent', 'response_added_by_customer', 'ticket_replied_product_agent', 'ticket_replied_product_customer' ], true ) ) {
11428 if ( 'response_added_by_agent' == $term ) {
11429 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
11430 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
11431 $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 ) );
11432 if ( ! empty( $tickets ) ) {
11433 $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 ) );
11434 $ticket_data = $tickets[0];
11435 $customer_data = $customers[0];
11436 $reply_data = $replies[0];
11437 $context['response_type'] = 'live';
11438 }
11439 } elseif ( 'response_added_by_customer' == $term ) {
11440 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
11441 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
11442 $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 ) );
11443 if ( ! empty( $tickets ) ) {
11444 $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 ) );
11445 $ticket_data = $tickets[0];
11446 $customer_data = $customers[0];
11447 $reply_data = $replies[0];
11448 $context['response_type'] = 'live';
11449 }
11450 } elseif ( 'ticket_replied_product_agent' == $term ) {
11451 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
11452 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
11453 if ( -1 == $product_id ) {
11454 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
11455 $products = array_map(
11456 function( $product ) {
11457 return $product['id'];
11458 },
11459 $products
11460 );
11461 $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 ) ) );
11462 } else {
11463 $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 ) );
11464 }
11465 if ( ! empty( $tickets ) ) {
11466 $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 ) );
11467 $ticket_data = $tickets[0];
11468 $customer_data = $customers[0];
11469 $reply_data = $replies[0];
11470 $context['response_type'] = 'live';
11471 }
11472 } elseif ( 'ticket_replied_product_customer' == $term ) {
11473 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
11474 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
11475 if ( -1 == $product_id ) {
11476 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
11477 $products = array_map(
11478 function( $product ) {
11479 return $product['id'];
11480 },
11481 $products
11482 );
11483 $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 ) ) );
11484 } else {
11485 $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 ) );
11486 }
11487 if ( ! empty( $tickets ) ) {
11488 $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 ) );
11489 $ticket_data = $tickets[0];
11490 $customer_data = $customers[0];
11491 $reply_data = $replies[0];
11492 $context['response_type'] = 'live';
11493 }
11494 }
11495 $context['pluggable_data'] = array_merge(
11496 [
11497 'reply' => $reply_data,
11498 'ticket' => $ticket_data,
11499 'customer' => $customer_data,
11500 ]
11501 );
11502 if ( 'ticket_replied_product_customer' == $term || 'ticket_replied_product_agent' == $term ) {
11503 $context['pluggable_data']['ticket_product_id'] = $product_id;
11504 }
11505 if ( is_object( $ticket_data ) ) {
11506 $ticket_data = get_object_vars( $ticket_data );
11507 }
11508 $ticket = \FluentSupport\App\Models\Ticket::find( $ticket_data['id'] );
11509 if ( is_object( $ticket ) && method_exists( $ticket, 'customData' ) ) {
11510 $context['pluggable_data']['custom_fields'] = $ticket->customData();
11511 }
11512 $context['pluggable_data']['ticket_link'] = admin_url( "admin.php?page=fluent-support#/tickets/{$ticket_data['id']}/view" );
11513 } else {
11514 if ( 'ticket_created' == $term ) {
11515 if ( -1 == $mailbox ) {
11516 $tickets = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE status = 'new' ORDER BY id DESC LIMIT 1" );
11517 } else {
11518 $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 ) );
11519 }
11520 if ( ! empty( $tickets ) ) {
11521 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d", $tickets[0]->customer_id ) );
11522 $ticket_data = $tickets[0];
11523 $customer_data = $customers[0];
11524 }
11525 } elseif ( 'ticket_closed_by_customer' == $term ) {
11526 $customers_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
11527 $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] ) );
11528 if ( ! empty( $tickets ) ) {
11529 $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 ) );
11530 $ticket_data = $tickets[0];
11531 $customer_data = $customers[0];
11532 }
11533 } elseif ( 'ticket_closed_by_agent' == $term ) {
11534 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
11535 $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] ) );
11536 if ( ! empty( $tickets ) ) {
11537 $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 ) );
11538 $ticket_data = $tickets[0];
11539 $customer_data = $customers[0];
11540 }
11541 } elseif ( 'ticket_created_product' == $term ) {
11542 $agents_type = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_persons" );
11543 if ( -1 == $product_id ) {
11544 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
11545 $products = array_map(
11546 function( $product ) {
11547 return $product['id'];
11548 },
11549 $products
11550 );
11551 $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 ) ) );
11552 } else {
11553 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id = %d ORDER BY id DESC LIMIT 1", $product_id ) );
11554 }
11555 if ( ! empty( $tickets ) ) {
11556 $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 ) );
11557 $ticket_data = $tickets[0];
11558 $customer_data = $customers[0];
11559 }
11560 } elseif ( 'ticket_closed_product' == $term ) {
11561 $agents_type = $wpdb->get_results( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = %s", $person_id ), ARRAY_A );
11562 $agents = array_map(
11563 function( $agent ) {
11564 return $agent['id'];
11565 },
11566 $agents_type
11567 );
11568 if ( -1 == $product_id ) {
11569 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
11570 $products = array_map(
11571 function( $product ) {
11572 return $product['id'];
11573 },
11574 $products
11575 );
11576 $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 ) ) );
11577 } else {
11578 $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 ) ) );
11579 }
11580 if ( ! empty( $tickets ) ) {
11581 $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 ) );
11582 $ticket_data = $tickets[0];
11583 $customer_data = $customers[0];
11584 }
11585 }
11586 $context['pluggable_data'] = array_merge(
11587 [
11588 'ticket' => $ticket_data,
11589 'customer' => $customer_data,
11590 ]
11591 );
11592 if ( 'ticket_created' == $term ) {
11593 $context['pluggable_data']['mailbox_id'] = $mailbox;
11594 } elseif ( 'ticket_created_product' == $term || 'ticket_replied_product_customer' == $term || 'ticket_replied_product_agent' == $term ) {
11595 $context['pluggable_data']['ticket_product_id'] = $product_id;
11596 } elseif ( 'ticket_closed_product' == $term ) {
11597 $context['pluggable_data']['ticket_product_id'] = $product_id;
11598 $context['pluggable_data']['person_id'] = $person_id;
11599 }
11600 if ( is_object( $ticket_data ) ) {
11601 $ticket_data = get_object_vars( $ticket_data );
11602 }
11603 $ticket = \FluentSupport\App\Models\Ticket::find( $ticket_data['id'] );
11604 if ( is_object( $ticket ) && method_exists( $ticket, 'customData' ) ) {
11605 $context['pluggable_data']['custom_fields'] = $ticket->customData();
11606 }
11607 $context['pluggable_data']['ticket_link'] = admin_url( "admin.php?page=fluent-support#/tickets/{$ticket_data['id']}/view" );
11608 $context['response_type'] = 'live';
11609 }
11610
11611 return $context;
11612 }
11613
11614 /**
11615 * Prepare Ultimate Member user_roles.
11616 *
11617 * @param array $data data.
11618 *
11619 * @return array
11620 */
11621 public function search_um_user_roles( $data ) {
11622 if ( function_exists( 'get_editable_roles' ) ) {
11623 $roles = get_editable_roles();
11624 } else {
11625 $roles = wp_roles()->roles;
11626 $roles = apply_filters( 'editable_roles', $roles );
11627 }
11628
11629 $options = [];
11630 foreach ( $roles as $role => $details ) {
11631
11632 $options[] = [
11633 'label' => $details['name'],
11634 'value' => $role,
11635 ];
11636
11637 }
11638
11639 return [
11640 'options' => $options,
11641 'hasMore' => false,
11642 ];
11643 }
11644
11645 /**
11646 * Prepare Ultimate Member forms_list.
11647 *
11648 * @param array $data data.
11649 *
11650 * @return array
11651 */
11652 public function search_um_forms_list( $data ) {
11653
11654 $page = $data['page'];
11655 $limit = Utilities::get_search_page_limit();
11656 $offset = $limit * ( $page - 1 );
11657
11658 $args = [
11659 'posts_per_page' => $limit,
11660 'offset' => $offset,
11661 'orderby' => 'title',
11662 'order' => 'ASC',
11663 'post_type' => 'um_form',
11664 'post_status' => 'publish',
11665 'fields' => 'ids',
11666 ];
11667 if ( 'register' == $data['filter']['type'] ) {
11668 $args['meta_query'] = [
11669 [
11670 'key' => '_um_mode',
11671 'value' => 'register',
11672 'compare' => 'LIKE',
11673 ],
11674 ];
11675 } elseif ( 'login' == $data['filter']['type'] ) {
11676 $args['meta_query'] = [
11677 [
11678 'key' => '_um_mode',
11679 'value' => 'login',
11680 'compare' => 'LIKE',
11681 ],
11682 ];
11683 }
11684
11685 $forms_list = get_posts( $args );
11686
11687 $forms_list_count = wp_count_posts( 'um_form' )->publish;
11688
11689 $options = [];
11690 if ( ! empty( $forms_list ) ) {
11691 foreach ( $forms_list as $form ) {
11692 $title = html_entity_decode( get_the_title( $form ), ENT_QUOTES, 'UTF-8' );
11693 $options[] = [
11694 'label' => $title,
11695 'value' => $form,
11696 ];
11697 }
11698 }
11699
11700 return [
11701 'options' => $options,
11702 'hasMore' => $forms_list_count > $limit && $forms_list_count > $offset,
11703 ];
11704 }
11705
11706 /**
11707 * Get last data for Ultimate Member Login trigger.
11708 *
11709 * @param array $data data.
11710 * @return mixed
11711 */
11712 public function search_ultimate_member_user_logsin( $data ) {
11713 $context = [];
11714 $args = [
11715 'orderby' => 'meta_value',
11716 'meta_key' => '_um_last_login',
11717 'order' => 'DESC',
11718 'number' => 1,
11719 ];
11720 $users = get_users( $args );
11721
11722 if ( ! empty( $users ) ) {
11723 $user = $users[0];
11724 $submitted_data = get_user_meta( $user->ID, 'submitted', true );
11725 if ( is_array( $submitted_data ) ) {
11726 unset(
11727 $submitted_data['user_password'],
11728 $submitted_data['confirm_user_password']
11729 );
11730 }
11731 $pluggable_data = WordPress::get_user_context( $user->ID );
11732 $pluggable_data['data'] = $submitted_data;
11733 $context['pluggable_data'] = $pluggable_data;
11734 $context['response_type'] = 'live';
11735 } else {
11736 $role = 'subscriber';
11737 $context['pluggable_data'] = [
11738 'wp_user_id' => 1,
11739 'user_login' => 'test',
11740 'display_name' => 'Test User',
11741 'user_firstname' => 'Test',
11742 'user_lastname' => 'User',
11743 'user_email' => 'testuser@gmail.com',
11744 'user_role' => [ $role ],
11745 ];
11746 $context['response_type'] = 'sample';
11747 }
11748 return $context;
11749 }
11750
11751 /**
11752 * Get last data for Ultimate Member Register trigger.
11753 *
11754 * @param array $data data.
11755 * @return mixed
11756 */
11757 public function search_ultimate_member_user_registers( $data ) {
11758 $context = [];
11759 $args = [
11760 'orderby' => 'meta_value',
11761 'meta_key' => 'um_user_profile_url_slug_user_login',
11762 'order' => 'DESC',
11763 'number' => 1,
11764 ];
11765 $users = get_users( $args );
11766
11767 if ( ! empty( $users ) ) {
11768 $user = $users[0];
11769 $submitted_data = get_user_meta( $user->ID, 'submitted', true );
11770 if ( is_array( $submitted_data ) ) {
11771 unset(
11772 $submitted_data['user_password'],
11773 $submitted_data['confirm_user_password']
11774 );
11775 }
11776 $pluggable_data = WordPress::get_user_context( $user->ID );
11777 $pluggable_data['data'] = $submitted_data;
11778 $context['pluggable_data'] = $pluggable_data;
11779 $context['response_type'] = 'live';
11780 } else {
11781 $role = 'subscriber';
11782 $context['pluggable_data'] = [
11783 'wp_user_id' => 1,
11784 'user_login' => 'test',
11785 'display_name' => 'Test User',
11786 'user_firstname' => 'Test',
11787 'user_lastname' => 'User',
11788 'user_email' => 'testuser@gmail.com',
11789 'user_role' => [ $role ],
11790 ];
11791 $context['response_type'] = 'sample';
11792 }
11793 return $context;
11794 }
11795
11796 /**
11797 * Get last data for Ultimate Member Register trigger.
11798 *
11799 * @param array $data data.
11800 * @return mixed
11801 */
11802 public function search_ultimate_member_user_inactive( $data ) {
11803 $context = [];
11804 $args = [
11805 'orderby' => 'user_id',
11806 'meta_key' => 'account_status',
11807 'meta_value' => 'inactive',
11808 'order' => 'ASC',
11809 'number' => 1,
11810 ];
11811 $users = get_users( $args );
11812
11813 if ( ! empty( $users ) ) {
11814 $user = $users[0];
11815 $pluggable_data = [];
11816 $pluggable_data[] = WordPress::get_user_context( $user->ID );
11817 $pluggable_data['user_account_status'] = 'inactive';
11818 $context['pluggable_data'] = $pluggable_data;
11819 $context['response_type'] = 'live';
11820 } else {
11821 $role = 'subscriber';
11822 $context['pluggable_data'] = [
11823 'wp_user_id' => 1,
11824 'user_login' => 'test',
11825 'display_name' => 'Test User',
11826 'user_firstname' => 'Test',
11827 'user_lastname' => 'User',
11828 'user_email' => 'testuser@gmail.com',
11829 'user_role' => [ $role ],
11830 'user_account_status' => 'inactive',
11831 ];
11832 $context['response_type'] = 'sample';
11833 }
11834 return $context;
11835 }
11836
11837 /**
11838 * Get last data for Ultimate Member Change Role trigger.
11839 *
11840 * @param array $data data.
11841 * @return mixed
11842 */
11843 public function search_ultimate_member_user_role_change( $data ) {
11844 $context = [];
11845
11846 $role = $data['filter']['role']['value'];
11847
11848 $args = [
11849 'number' => 1,
11850 'role' => $role,
11851 ];
11852 $users = get_users( $args );
11853 shuffle( $users );
11854 if ( ! empty( $users ) ) {
11855 $user = $users[0];
11856 $pluggable_data = WordPress::get_user_context( $user->ID );
11857 $context['pluggable_data'] = $pluggable_data;
11858 $context['response_type'] = 'live';
11859 } else {
11860 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
11861 $context['pluggable_data'] = [
11862 'wp_user_id' => 1,
11863 'user_login' => 'test',
11864 'display_name' => 'Test User',
11865 'user_firstname' => 'Test',
11866 'user_lastname' => 'User',
11867 'user_email' => 'testuser@gmail.com',
11868 'user_role' => [ $role ],
11869 'user_account_status' => 'inactive',
11870 ];
11871 $context['response_type'] = 'sample';
11872 }
11873 return $context;
11874 }
11875
11876 /**
11877 * Get JetEngine WP Posttypes.
11878 *
11879 * @param array $data data.
11880 *
11881 * @return array
11882 */
11883 public function search_je_posttype_list( $data ) {
11884
11885 $post_types = get_post_types( [ 'public' => true ], 'objects' );
11886 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
11887 if ( isset( $post_types['attachment'] ) ) {
11888 unset( $post_types['attachment'] );
11889 }
11890
11891 $options = [];
11892 foreach ( $post_types as $post_type => $details ) {
11893 $options[] = [
11894 'label' => $details->label,
11895 'value' => $post_type,
11896 ];
11897 }
11898
11899 return [
11900 'options' => $options,
11901 'hasMore' => false,
11902 ];
11903 }
11904
11905 /**
11906 * Get JetEngine WP fields.
11907 *
11908 * @param array $data data.
11909 *
11910 * @return array
11911 */
11912 public function search_je_field_list( $data ) {
11913
11914 $post_type = $data['dynamic'];
11915
11916 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
11917
11918 $post_fields = array_filter(
11919 $metaboxes,
11920 function( $metabox ) {
11921 /**
11922 *
11923 * Ignore line
11924 *
11925 * @phpstan-ignore-next-line
11926 */
11927 return 'post' === $metabox['args']['object_type'];
11928 }
11929 );
11930
11931 $post_fields_count = count( $post_fields );
11932
11933 $options = [];
11934 if ( ! empty( $post_fields ) ) {
11935 if ( is_array( $post_fields ) ) {
11936 foreach ( $post_fields as $post_field ) {
11937 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
11938 foreach ( $post_field['meta_fields'] as $meta_field ) {
11939 $options[] = [
11940 'label' => $meta_field['title'],
11941 'value' => $meta_field['name'],
11942 ];
11943 }
11944 }
11945 }
11946 }
11947 }
11948 return [
11949 'options' => $options,
11950 'hasMore' => false,
11951 ];
11952 }
11953
11954 /**
11955 * Search Last Updated Field Data.
11956 *
11957 * @param array $data data.
11958 * @return array
11959 */
11960 public function search_jet_engine_field_data( $data ) {
11961 global $wpdb;
11962
11963 $context = [];
11964
11965 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
11966
11967 $post_type = $data['filter']['wp_post_type']['value'];
11968
11969 if ( -1 === $field ) {
11970 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
11971
11972 $post_fields = array_filter(
11973 $metaboxes,
11974 function( $metabox ) {
11975 /**
11976 *
11977 * Ignore line
11978 *
11979 * @phpstan-ignore-next-line
11980 */
11981 return 'post' === $metabox['args']['object_type'];
11982 }
11983 );
11984
11985 $options = [];
11986 if ( ! empty( $post_fields ) ) {
11987 if ( is_array( $post_fields ) ) {
11988 foreach ( $post_fields as $post_field ) {
11989 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
11990 foreach ( $post_field['meta_fields'] as $meta_field ) {
11991 $options[] = $meta_field['name'];
11992 }
11993 }
11994 }
11995 }
11996 }
11997 $random_key = array_rand( $options );
11998 $random_value = $options[ $random_key ];
11999 $string = '%' . $random_value . '%';
12000 } else {
12001 $string = '%' . $data['filter']['field_id']['value'] . '%';
12002 }
12003
12004 $result = $wpdb->get_results(
12005 $wpdb->prepare(
12006 'SELECT post_id FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key LIKE %s',
12007 [ $string ]
12008 ),
12009 ARRAY_A
12010 );
12011
12012 $ids = [];
12013
12014 if ( ! empty( $result ) ) {
12015 foreach ( $result as $val ) {
12016 $ids[] = $val['post_id'];
12017 }
12018 }
12019
12020 $lastupdated_args = [
12021 'post_type' => $post_type,
12022 'orderby' => 'modified',
12023 'post__in' => $ids,
12024 'posts_per_page' => 1,
12025 ];
12026 $lastupdated_loop = get_posts( $lastupdated_args );
12027
12028 $response = [];
12029 if ( ! empty( $result ) ) {
12030 $context['post'] = $lastupdated_loop[0];
12031
12032 $meta_value = get_post_meta( $lastupdated_loop[0]->ID, sprintf( '%s', $data['filter']['field_id']['value'] ), true );
12033 $meta_key = sprintf( '%s', $data['filter']['field_id']['value'] );
12034
12035 $context[ $meta_key ] = $meta_value;
12036
12037 $response['pluggable_data'] = $context;
12038 $response['response_type'] = 'live';
12039 } else {
12040 $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 );
12041 }
12042
12043 return $response;
12044
12045 }
12046
12047 /**
12048 * Get Formidable Forms.
12049 *
12050 * @param array $data data.
12051 *
12052 * @return array|void
12053 */
12054 public function search_formidable_form_list( $data ) {
12055 if ( ! class_exists( 'FrmForm' ) ) {
12056 return;
12057 }
12058
12059 $page = $data['page'];
12060 $term = $data['search_term'];
12061 $limit = Utilities::get_search_page_limit();
12062 $offset = $limit * ( $page - 1 );
12063 $per_page = 10;
12064
12065 $query = [
12066 [
12067 'or' => 1,
12068 'name LIKE' => $term,
12069 'parent_form_id' => null,
12070 ],
12071 ];
12072 $query['is_template'] = 0;
12073 $query['status !'] = 'trash';
12074 $forms_list = FrmForm::getAll( $query, '', $offset . ',' . $per_page );
12075 $form_count = FrmForm::getAll( $query );
12076 $form_count = count( $form_count );
12077 $options = [];
12078
12079 if ( ! empty( $forms_list ) ) {
12080 if ( is_array( $forms_list ) ) {
12081 foreach ( $forms_list as $form ) {
12082 $options[] = [
12083 'label' => $form->name,
12084 'value' => $form->id,
12085 ];
12086 }
12087 }
12088 }
12089
12090 return [
12091 'options' => $options,
12092 'hasMore' => $form_count > $limit && $form_count > $offset,
12093 ];
12094 }
12095
12096 /**
12097 * Get JetFormBuilder Form List.
12098 *
12099 * @param array $data data.
12100 *
12101 * @return array|void
12102 */
12103 public function search_jetform_list( $data ) {
12104 if ( ! class_exists( '\Jet_Form_Builder\Classes\Tools' ) ) {
12105 return;
12106 }
12107
12108 $forms = \Jet_Form_Builder\Classes\Tools::get_forms_list_for_js();
12109
12110 $options = [];
12111 foreach ( $forms as $form ) {
12112
12113 if ( ! empty( $form['value'] ) ) {
12114 $options[] = [
12115 'label' => esc_html( $form['label'] ),
12116 'value' => esc_attr( $form['value'] ),
12117 ];
12118 }
12119 }
12120
12121 return [
12122 'options' => $options,
12123 'hasMore' => false,
12124 ];
12125 }
12126
12127 /**
12128 * Get Forminator Form List.
12129 *
12130 * @param array $data data.
12131 *
12132 * @return array|void
12133 */
12134 public function search_forminator_form_list( $data ) {
12135 if ( ! class_exists( 'Forminator_API' ) ) {
12136 return;
12137 }
12138
12139 $forms = Forminator_API::get_forms( null, 1, 10 );
12140
12141 $options = [];
12142 foreach ( $forms as $form ) {
12143 $options[] = [
12144 'label' => isset( $form->settings ) && isset( $form->settings['form_name'] ) ? $form->settings['form_name'] : $form->name,
12145 'value' => $form->id,
12146 ];
12147 }
12148
12149 return [
12150 'options' => $options,
12151 'hasMore' => false,
12152 ];
12153 }
12154
12155
12156 /**
12157 * Get BbPress topics list.
12158 *
12159 * @param array $data data.
12160 *
12161 * @return array|void
12162 */
12163 public function search_bbp_topic_list( $data ) {
12164 $page = $data['page'];
12165 $forum_id = $data['dynamic'];
12166 $limit = Utilities::get_search_page_limit();
12167 $offset = $limit * ( $page - 1 );
12168 $args = [
12169 'post_type' => 'topic',
12170 'offset' => $offset,
12171 'meta_query' => [
12172 [
12173 'key' => '_bbp_forum_id',
12174 'value' => $forum_id,
12175 'compare' => '==',
12176 ],
12177 ],
12178 ];
12179
12180 $topics = get_posts( $args );
12181 $topics_count = wp_count_posts( 'topic' )->publish;
12182
12183 $options = [];
12184 if ( ! empty( $topics ) ) {
12185 if ( is_array( $topics ) ) {
12186 foreach ( $topics as $topic ) {
12187 $options[] = [
12188 'label' => $topic->post_title,
12189 'value' => $topic->ID,
12190 ];
12191 }
12192 }
12193 }
12194 return [
12195 'options' => $options,
12196 'hasMore' => $topics_count > $limit && $topics_count > $offset,
12197 ];
12198 }
12199
12200
12201 /**
12202 * Search Last Updated Field Data.
12203 *
12204 * @param array $data data.
12205 * @return array
12206 */
12207 public function search_bbp_last_data( $data ) {
12208 global $wpdb;
12209
12210 $post_type = $data['post_type'];
12211 $trigger = $data['search_term'];
12212 $context = [];
12213
12214 if ( 'topic' === $post_type ) {
12215 $post_id = $data['filter']['forum']['value'];
12216 if ( -1 === $post_id ) {
12217 $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" );
12218
12219 } else {
12220 $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 ) );
12221
12222 }
12223 } elseif ( 'reply' === $post_type ) {
12224 $post_id = $data['filter']['topic']['value'];
12225
12226 if ( -1 === $post_id ) {
12227 $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" );
12228
12229 } else {
12230 $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 ) );
12231 }
12232 }
12233
12234
12235 $response = [];
12236 if ( ! empty( $result ) ) {
12237 if ( 'bbpress_topic_created' === $trigger ) {
12238 $topic_id = $result[0]->post_id;
12239 $forum_id = $result[0]->meta_value;
12240 $topic = get_the_title( $topic_id );
12241 $topic_link = get_the_permalink( $topic_id );
12242 $topic_description = get_the_content( null, false, $topic_id );
12243 $topic_status = get_post_status( $topic_id );
12244
12245 $forum = get_the_title( $forum_id );
12246 $forum_link = get_the_permalink( $forum_id );
12247 $forum_description = get_the_content( null, false, $forum_id );
12248 $forum_status = get_post_status( $forum_id );
12249
12250 $forum = [
12251 'forum' => $forum_id,
12252 'forum_title' => $forum,
12253 'forum_link' => $forum_link,
12254 'forum_description' => $forum_description,
12255 'forum_status' => $forum_status,
12256 ];
12257
12258 $topic = [
12259 'topic' => $topic_id,
12260 'topic_title' => $topic,
12261 'topic_link' => $topic_link,
12262 'topic_description' => $topic_description,
12263 'topic_status' => $topic_status,
12264 ];
12265
12266 $user_id = $result[0]->post_author;
12267 if ( '0' != $user_id ) {
12268 $context = array_merge(
12269 WordPress::get_user_context( $user_id ),
12270 $forum,
12271 $topic
12272 );
12273 } else {
12274 $anonymous_data = [
12275 'bbp_anonymous_name' => get_post_meta( $result[0]->ID, '_bbp_anonymous_name', true ),
12276 'bbp_anonymous_email' => get_post_meta( $result[0]->ID, '_bbp_anonymous_email', true ),
12277 'bbp_anonymous_website' => get_post_meta( $result[0]->ID, '_bbp_anonymous_website', true ),
12278 ];
12279 $context = array_merge(
12280 $anonymous_data,
12281 $forum,
12282 $topic
12283 );
12284 }
12285
12286 $response['pluggable_data'] = $context;
12287 $response['response_type'] = 'live';
12288 } else {
12289 $reply_id = $result[0]->post_id;
12290 $topic_id = $result[0]->meta_value;
12291 $forum_id = get_post_meta( $topic_id, '_bbp_forum_id', true );
12292 $forum_id = intval( '"' . $forum_id . '"' );
12293 $reply = get_the_title( $reply_id );
12294 $reply_link = get_the_permalink( $reply_id );
12295 $reply_description = get_the_content( null, false, $reply_id );
12296 $reply_status = get_post_status( $reply_id );
12297
12298
12299 $topic = get_the_title( $topic_id );
12300 $topic_link = get_the_permalink( $topic_id );
12301 $topic_description = get_the_content( null, false, $topic_id );
12302 $topic_status = get_post_status( $topic_id );
12303
12304 $forum = get_the_title( $forum_id );
12305 $forum_link = get_the_permalink( $forum_id );
12306 $forum_description = get_the_content( null, false, $forum_id );
12307 $forum_status = get_post_status( $forum_id );
12308
12309 $forum = [
12310 'forum' => $forum_id,
12311 'forum_title' => $forum,
12312 'forum_link' => $forum_link,
12313 'forum_description' => $forum_description,
12314 'forum_status' => $forum_status,
12315 ];
12316
12317 $topic = [
12318 'topic' => $topic_id,
12319 'topic_title' => $topic,
12320 'topic_link' => $topic_link,
12321 'topic_description' => $topic_description,
12322 'topic_status' => $topic_status,
12323 ];
12324
12325 $reply = [
12326 'reply_title' => $reply,
12327 'reply_link' => $reply_link,
12328 'reply_description' => $reply_description,
12329 'reply_status' => $reply_status,
12330 ];
12331 $user_id = $result[0]->post_author;
12332 if ( '0' != $user_id ) {
12333 $context = array_merge(
12334 WordPress::get_user_context( $user_id ),
12335 $forum,
12336 $topic,
12337 $reply
12338 );
12339 } else {
12340 $anonymous_data = [
12341 'bbp_anonymous_name' => get_post_meta( $result[0]->ID, '_bbp_anonymous_name', true ),
12342 'bbp_anonymous_email' => get_post_meta( $result[0]->ID, '_bbp_anonymous_email', true ),
12343 'bbp_anonymous_website' => get_post_meta( $result[0]->ID, '_bbp_anonymous_website', true ),
12344 ];
12345 $context = array_merge(
12346 $anonymous_data,
12347 $forum,
12348 $topic,
12349 $reply
12350 );
12351 }
12352
12353 $response['pluggable_data'] = $context;
12354 $response['response_type'] = 'live';
12355 }
12356 }
12357
12358 return $response;
12359 }
12360
12361 /**
12362 * Search Last Updated Field Data.
12363 *
12364 * @param array $data data.
12365 * @return array|void
12366 */
12367 public function search_happyform_list( $data ) {
12368 if ( ! function_exists( 'happyforms_get_form_controller' ) ) {
12369 return;
12370 }
12371
12372 $form_controller = happyforms_get_form_controller();
12373
12374 $forms = $form_controller->do_get();
12375 $options = [];
12376 if ( ! empty( $forms ) ) {
12377 foreach ( $forms as $form ) {
12378 $options[] = [
12379 'label' => $form['post_title'],
12380 'value' => $form['ID'],
12381 ];
12382 }
12383 }
12384
12385 return [
12386 'options' => $options,
12387 'hasMore' => false,
12388 ];
12389 }
12390
12391 /**
12392 * Get Memberpress Course List.
12393 *
12394 * @param array $data data.
12395 *
12396 * @return array|void
12397 */
12398 public function search_mpc_lessons_list( $data ) {
12399 if ( ! class_exists( '\memberpress\courses\models\Lesson' ) ) {
12400 return;
12401 }
12402 global $wpdb;
12403 $options = [];
12404 $course_id = $data['dynamic'];
12405 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mpcs_sections WHERE course_id =%s", $course_id ) );
12406 $sections = [];
12407 foreach ( $result as $rec ) {
12408 $sections[] = [
12409 'id' => $rec->id,
12410 'title' => $rec->title,
12411 ];
12412 }
12413 if ( is_array( $sections ) && count( $sections ) > 0 ) {
12414 foreach ( $sections as $section ) {
12415 $post_types_string = \memberpress\courses\models\Lesson::lesson_cpts();
12416 $post_types_string = implode( "','", $post_types_string );
12417
12418 $query = $wpdb->prepare(
12419 "SELECT * FROM {$wpdb->posts} AS p
12420 JOIN {$wpdb->postmeta} AS pm
12421 ON p.ID = pm.post_id
12422 AND pm.meta_key = %s
12423 AND pm.meta_value = %s
12424 JOIN {$wpdb->postmeta} AS pm_order
12425 ON p.ID = pm_order.post_id
12426 AND pm_order.meta_key = %s
12427 WHERE p.post_type in ( %s ) AND p.post_status <> 'trash'
12428 ORDER BY pm_order.meta_value * 1",
12429 models\Lesson::$section_id_str,
12430 $section['id'],
12431 models\Lesson::$lesson_order_str,
12432 stripcslashes( $post_types_string )
12433 );
12434
12435 $db_lessons = $wpdb->get_results( stripcslashes( $query ) ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
12436 foreach ( $db_lessons as $lesson ) {
12437 $options[] = [
12438 'label' => $section['title'] . '->' . $lesson->post_title,
12439 'value' => $lesson->ID,
12440 ];
12441 }
12442 }
12443 }
12444 return [
12445 'options' => $options,
12446 'hasMore' => false,
12447 ];
12448 }
12449
12450 /**
12451 * Get Memberpress Course List.
12452 *
12453 * @param array $data data.
12454 *
12455 * @return array|void
12456 */
12457 public function search_gp_rank_type_list( $data ) {
12458 global $wpdb;
12459
12460 $posts = $wpdb->get_results(
12461 "SELECT ID, post_name, post_title, post_type
12462 FROM $wpdb->posts
12463 WHERE post_type LIKE 'rank-type' AND post_status = 'publish' ORDER BY post_title ASC"
12464 );
12465
12466 $posts_count = count( $posts );
12467
12468 $options = [];
12469 if ( $posts ) {
12470 foreach ( $posts as $post ) {
12471 $options[] = [
12472 'label' => $post->post_title,
12473 'value' => $post->post_name,
12474 ];
12475 }
12476 }
12477
12478 return [
12479 'options' => $options,
12480 'hasMore' => false,
12481 ];
12482 }
12483
12484 /**
12485 * Get MPC last data.
12486 *
12487 * @param array $data data.
12488 *
12489 * @return array
12490 */
12491 public function search_mpc_last_data( $data ) {
12492 global $wpdb;
12493 $trigger = $data['search_term'];
12494 $course_data = [];
12495 $lesson_data = [];
12496 $context = [];
12497
12498 if ( 'mpc_course_completed' === $trigger ) {
12499 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course]']['value'] : '-1' );
12500 if ( $course_id > 0 ) {
12501
12502 $course = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $course_id ) );
12503 } else {
12504 $course = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-course' ORDER BY id DESC LIMIT 1" );
12505 }
12506
12507 if ( ! empty( $course ) ) {
12508 $course_data = [
12509 'course_id' => $course->ID,
12510 'course_title' => get_the_title( $course_id ),
12511 'course_url' => get_permalink( $course_id ),
12512 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
12513 'course_featured_image_url' => get_the_post_thumbnail_url( $course_id ),
12514 ];
12515 }
12516 $user_progress = $wpdb->get_row( $wpdb->prepare( "SELECT user_id FROM {$wpdb->prefix}mpcs_user_progress WHERE course_id=%s", $course_id ) );
12517 if ( ! empty( $user_progress ) ) {
12518 $context['response_type'] = 'live';
12519 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
12520 } else {
12521 $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}
12522 ,"response_type":"sample"} ';
12523 $context = json_decode( $sample_data, true );
12524 }
12525 } elseif ( 'mpc_lesson_completed' === $trigger ) {
12526 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
12527 $course_id = (int) $data['filter']['course']['value'];
12528 if ( $lesson_id > 0 ) {
12529
12530 $lesson = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $lesson_id ) );
12531 } else {
12532 $lesson = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-lesson' ORDER BY id DESC LIMIT 1" );
12533 }
12534
12535 if ( ! empty( $lesson ) ) {
12536 $lesson_data = [
12537 'lesson_id' => $lesson->ID,
12538 'lesson_title' => get_the_title( $lesson_id ),
12539 'lesson_url' => get_permalink( $lesson_id ),
12540 'lesson_featured_image_id' => get_post_meta( $lesson_id, '_thumbnail_id', true ),
12541 'lesson_featured_image_url' => get_the_post_thumbnail_url( $lesson_id ),
12542 ];
12543
12544 $lesson_section_id = get_post_meta( $lesson->ID, '_mpcs_lesson_section_id', true );
12545
12546 $section = $wpdb->get_row( $wpdb->prepare( "SELECT course_id FROM {$wpdb->prefix}mpcs_sections WHERE ID= %s", $lesson_section_id ) );
12547
12548 $course_data = [
12549 'course_id' => $course_id,
12550 'course_title' => get_the_title( $course_id ),
12551 'course_url' => get_permalink( $course_id ),
12552 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
12553 'course_featured_image_url' => get_the_post_thumbnail_url( $section->course_id ),
12554 ];
12555 }
12556
12557 $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 ) );
12558 if ( ! empty( $user_progress ) ) {
12559 $context['response_type'] = 'live';
12560 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
12561 } else {
12562 $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"}';
12563 $context = json_decode( $sample_data, true );
12564 }
12565 }
12566
12567
12568 return $context;
12569 }
12570
12571 /** Get GamiPress Rank List.
12572 *
12573 * @param array $data data.
12574 *
12575 * @return array|void
12576 */
12577 public function search_gp_rank_list( $data ) {
12578 global $wpdb;
12579
12580 $page = $data['page'];
12581 $limit = Utilities::get_search_page_limit();
12582 $offset = $limit * ( $page - 1 );
12583
12584 $args = [
12585 'post_type' => $data['dynamic']['rank_type'],
12586 'posts_per_page' => $limit,
12587 'offset' => $offset,
12588 'orderby' => 'title',
12589 'order' => 'ASC',
12590 'post_status' => 'publish',
12591 's' => $data['search_term'],
12592 ];
12593
12594 $rank_type = get_posts( $args );
12595
12596 $count_args = [
12597 'post_type' => $data['dynamic']['rank_type'],
12598 'posts_per_page' => -1,
12599 'post_status' => 'publish',
12600 's' => $data['search_term'],
12601 ];
12602
12603 $rank_posts = get_posts( $count_args );
12604 $rank_type_count = count( $rank_posts );
12605
12606 $options = [];
12607 if ( $rank_type ) {
12608 foreach ( $rank_type as $rank ) {
12609 $options[] = [
12610 'label' => $rank->post_title,
12611 'value' => $rank->ID,
12612 ];
12613 }
12614 }
12615
12616 return [
12617 'options' => $options,
12618 'hasMore' => $rank_type_count > $limit && $rank_type_count > $offset,
12619 ];
12620 }
12621
12622 /**
12623 * Get GamiPress PointType List.
12624 *
12625 * @param array $data data.
12626 *
12627 * @return array|void
12628 */
12629 public function search_gp_point_type_list( $data ) {
12630 global $wpdb;
12631
12632 $page = $data['page'];
12633 $limit = Utilities::get_search_page_limit();
12634 $offset = $limit * ( $page - 1 );
12635
12636 $args = [
12637 'post_type' => 'points-type',
12638 'posts_per_page' => $limit,
12639 'offset' => $offset,
12640 'orderby' => 'title',
12641 'order' => 'ASC',
12642 'post_status' => 'publish',
12643 's' => $data['search_term'],
12644 ];
12645
12646 $point_type = get_posts( $args );
12647
12648 $count_args = [
12649 'post_type' => 'points-type',
12650 'posts_per_page' => -1,
12651 'post_status' => 'publish',
12652 's' => $data['search_term'],
12653 ];
12654
12655 $count_point_type = get_posts( $count_args );
12656 $point_type_count = count( $count_point_type );
12657
12658 $options = [];
12659 if ( $point_type ) {
12660 foreach ( $point_type as $point ) {
12661 $options[] = [
12662 'label' => $point->post_title,
12663 'value' => $point->ID,
12664 ];
12665 }
12666 }
12667
12668 return [
12669 'options' => $options,
12670 'hasMore' => $point_type_count > $limit && $point_type_count > $offset,
12671 ];
12672 }
12673
12674 /**
12675 * Get GamiPress AchievementType List.
12676 *
12677 * @param array $data data.
12678 *
12679 * @return array|void
12680 */
12681 public function search_gp_achivement_type_list( $data ) {
12682 global $wpdb;
12683
12684 $posts = $wpdb->get_results(
12685 "SELECT ID, post_name, post_title, post_type
12686 FROM $wpdb->posts
12687 WHERE post_type LIKE 'achievement-type' AND post_status = 'publish' ORDER BY post_title ASC"
12688 );
12689
12690 $posts_count = count( $posts );
12691
12692 $options = [];
12693 if ( $posts ) {
12694 foreach ( $posts as $post ) {
12695 $options[] = [
12696 'label' => $post->post_title,
12697 'value' => $post->post_name,
12698 ];
12699 }
12700 }
12701
12702 $options[] = [
12703 'label' => 'Points awards',
12704 'value' => 'points-award',
12705 ];
12706 $options[] = [
12707 'label' => 'Step',
12708 'value' => 'step',
12709 ];
12710 $options[] = [
12711 'label' => 'Rank requirement',
12712 'value' => 'rank-requirement',
12713 ];
12714
12715 return [
12716 'options' => $options,
12717 'hasMore' => false,
12718 ];
12719 }
12720
12721 /**
12722 * Get GamiPress Award List.
12723 *
12724 * @param array $data data.
12725 *
12726 * @return array|void
12727 */
12728 public function search_gp_award_list( $data ) {
12729 global $wpdb;
12730
12731 $page = $data['page'];
12732 $limit = Utilities::get_search_page_limit();
12733 $offset = $limit * ( $page - 1 );
12734
12735 $args = [
12736 'post_type' => $data['dynamic']['achivement_type'],
12737 'posts_per_page' => $limit,
12738 'offset' => $offset,
12739 'orderby' => 'title',
12740 'order' => 'ASC',
12741 'post_status' => 'publish',
12742 's' => $data['search_term'],
12743 ];
12744
12745 $award_type = get_posts( $args );
12746 $count_args = [
12747 'post_type' => $data['dynamic']['achivement_type'],
12748 'posts_per_page' => -1,
12749 'post_status' => 'publish',
12750 's' => $data['search_term'],
12751 ];
12752
12753 $count_award_type = get_posts( $count_args );
12754 $award_type_count = count( $count_award_type );
12755 $options = [];
12756 if ( $award_type ) {
12757 foreach ( $award_type as $award ) {
12758 $options[] = [
12759 'label' => $award->post_title,
12760 'value' => $award->ID,
12761 ];
12762 }
12763 }
12764
12765 return [
12766 'options' => $options,
12767 'hasMore' => $award_type_count > $limit && $award_type_count > $offset,
12768 ];
12769 }
12770
12771 /**
12772 * Get Woocommerce Subscription Product List.
12773 *
12774 * @param array $data data.
12775 *
12776 * @return array|void
12777 */
12778 public function search_wc_subscription_product_list( $data ) {
12779 global $wpdb;
12780
12781 $subscriptions = $wpdb->get_results(
12782 $wpdb->prepare(
12783 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
12784 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
12785 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
12786 AND posts.post_type = %s
12787 AND posts.post_status = %s
12788 UNION ALL
12789 SELECT ID, post_title FROM $wpdb->posts
12790 WHERE post_type = %s
12791 AND post_status = %s
12792 ORDER BY post_title",
12793 'product',
12794 'publish',
12795 'shop_subscription',
12796 'publish'
12797 )
12798 );
12799 $page = $data['page'];
12800 $limit = Utilities::get_search_page_limit();
12801 $offset = $limit * ( $page - 1 );
12802 $subscriptions_products = [];
12803 $subscription_products_count = 10;
12804 if ( function_exists( 'wc_get_products' ) ) {
12805 $product_query = wc_get_products(
12806 [
12807 'type' => [ 'subscription', 'variable-subscription' ],
12808 'posts_per_page' => $limit,
12809 'offset' => $offset,
12810 'paginate' => true,
12811 ]
12812 );
12813 if ( is_object( $product_query ) ) {
12814 $subscriptions_products = $product_query->products;
12815 $subscription_products_count = $product_query->total;
12816 }
12817 }
12818
12819 $options = [];
12820 if ( $subscriptions ) {
12821 foreach ( $subscriptions as $post ) {
12822 $options[] = [
12823 'label' => $post->post_title,
12824 'value' => $post->ID,
12825 ];
12826 }
12827 return [
12828 'options' => $options,
12829 'hasMore' => false,
12830 ];
12831 } elseif ( $subscriptions_products ) {
12832 foreach ( $subscriptions_products as $product ) {
12833 $title = $product->get_name();
12834 $options[] = [
12835 'label' => ! empty( $title ) ? $title : $product->get_id(),
12836 'value' => $product->get_id(),
12837 ];
12838 }
12839 return [
12840 'options' => $options,
12841 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
12842 ];
12843 } else {
12844 return [
12845 'options' => $options,
12846 'hasMore' => false,
12847 ];
12848 }
12849 }
12850
12851 /**
12852 * Get Woocommerce Subscriptions Variation list.
12853 *
12854 * @param array $data data.
12855 *
12856 * @return array|void
12857 */
12858 public function search_wc_variable_subscription_list( $data ) {
12859 global $wpdb;
12860
12861 $page = $data['page'];
12862 $limit = Utilities::get_search_page_limit();
12863 $offset = $limit * ( $page - 1 );
12864
12865 if ( ! function_exists( 'wc_get_products' ) ) {
12866 return;
12867 }
12868 $subscription_products = wc_get_products(
12869 [
12870 'type' => [ 'variable-subscription' ],
12871 'posts_per_page' => $limit,
12872 'offset' => $offset,
12873 'orderby' => 'date',
12874 'order' => 'DESC',
12875 ]
12876 );
12877
12878 $subscription_products_count = count( (array) $subscription_products );
12879
12880 $options = [];
12881 if ( $subscription_products ) {
12882 foreach ( (array) $subscription_products as $product ) {
12883 $options[] = [
12884 'label' => $product->get_title(),
12885 'value' => $product->get_id(),
12886 ];
12887 }
12888 }
12889
12890 return [
12891 'options' => $options,
12892 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
12893 ];
12894 }
12895
12896 /**
12897 * Get Woocommerce Variation list.
12898 *
12899 * @param array $data data.
12900 *
12901 * @return array|void
12902 */
12903 public function search_wc_variation_list( $data ) {
12904 global $wpdb;
12905
12906 $page = $data['page'];
12907 $limit = Utilities::get_search_page_limit();
12908 $offset = $limit * ( $page - 1 );
12909
12910 $args = [
12911 'post_type' => 'product_variation',
12912 'post_parent' => $data['dynamic']['variable_subscription'],
12913 'posts_per_page' => $limit,
12914 'offset' => $offset,
12915 'orderby' => 'ID',
12916 'order' => 'ASC',
12917 'post_status' => 'publish',
12918 ];
12919
12920 $variation = get_posts( $args );
12921 $variation_count = count( $variation );
12922
12923 $options = [];
12924 if ( $variation ) {
12925 foreach ( $variation as $product ) {
12926 $options[] = [
12927 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
12928 'value' => $product->ID,
12929 ];
12930 }
12931 }
12932
12933 return [
12934 'options' => $options,
12935 'hasMore' => $variation_count > $limit && $variation_count > $offset,
12936 ];
12937 }
12938
12939 /**
12940 * Get Membership List.
12941 *
12942 * @param array $data data.
12943 *
12944 * @return array
12945 */
12946 public function search_membership_list( $data ) {
12947 global $wpdb;
12948
12949 $levels = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}pmpro_membership_levels ORDER BY id ASC" );
12950 $options = [];
12951 if ( $levels ) {
12952 foreach ( $levels as $level ) {
12953 $options[] = [
12954 'label' => $level->name,
12955 'value' => $level->id,
12956 ];
12957 }
12958 }
12959
12960 return [
12961 'options' => $options,
12962 'hasMore' => false,
12963 ];
12964 }
12965
12966 /**
12967
12968 * Get EventsManager last data.
12969 *
12970 * @param array $data data.
12971 *
12972 * @return array
12973 */
12974 public function search_events_manager_data( $data ) {
12975 global $wpdb;
12976 $trigger = $data['search_term'];
12977 $context = [];
12978
12979 $post_id = (int) ( isset( $data['filter']['post_id']['value'] ) ? $data['filter']['post_id']['value'] : '-1' );
12980 if ( 'em_user_register_in_event' === $trigger ) {
12981 if ( $post_id > 0 ) {
12982 $event_id_id = get_post_meta( $post_id, '_event_id', true );
12983 $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 ) );
12984 } else {
12985 $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" );
12986
12987 }
12988
12989 if ( ! empty( $all_bookings ) ) {
12990 $user_id = $all_bookings->person_id;
12991 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
12992 $bookings = json_decode( (string) wp_json_encode( $all_bookings ), true );
12993 $context['pluggable_data'] = array_merge(
12994 WordPress::get_user_context( $user_id ),
12995 is_array( $bookings ) ? $bookings : []
12996 );
12997 if ( ! empty( $location ) ) {
12998 $locations = json_decode( (string) wp_json_encode( $location ), true );
12999 $context['pluggable_data'] = array_merge(
13000 $context['pluggable_data'],
13001 is_array( $locations ) ? $locations : []
13002 );
13003 }
13004
13005 $context['response_type'] = 'live';
13006 }
13007 } elseif ( 'em_user_unregister_from_event' === $trigger ) {
13008
13009 if ( $post_id > 0 ) {
13010 $event_id_id = get_post_meta( $post_id, '_event_id', true );
13011 $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 ) );
13012 } else {
13013 $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" );
13014
13015 }
13016
13017 if ( ! empty( $all_bookings ) ) {
13018 $user_id = $all_bookings->person_id;
13019 $bookings = json_decode( (string) wp_json_encode( $all_bookings ), true );
13020 $context['pluggable_data'] = array_merge(
13021 WordPress::get_user_context( $user_id ),
13022 is_array( $bookings ) ? $bookings : []
13023 );
13024 $context['response_type'] = 'live';
13025 }
13026 } elseif ( 'em_user_booking_approved' === $trigger ) {
13027
13028 if ( $post_id > 0 ) {
13029 $event_id_id = get_post_meta( $post_id, '_event_id', true );
13030 $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 ) );
13031 } else {
13032 $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" );
13033
13034 }
13035
13036 if ( ! empty( $all_bookings ) ) {
13037 $user_id = $all_bookings->person_id;
13038 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
13039 $bookings = json_decode( (string) wp_json_encode( $all_bookings ), true );
13040 $context['pluggable_data'] = array_merge(
13041 WordPress::get_user_context( $user_id ),
13042 is_array( $bookings ) ? $bookings : []
13043 );
13044 if ( ! empty( $location ) ) {
13045 $locations = json_decode( (string) wp_json_encode( $location ), true );
13046 $context['pluggable_data'] = array_merge(
13047 $context['pluggable_data'],
13048 is_array( $locations ) ? $locations : []
13049 );
13050 }
13051
13052 $context['response_type'] = 'live';
13053
13054 }
13055 } elseif ( 'em_user_registers_event_with_specific_ticket' === $trigger ) {
13056 if ( $post_id > 0 ) {
13057 $event_id_id = get_post_meta( $post_id, '_event_id', true );
13058 $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 ) );
13059 }
13060 if ( ! empty( $all_bookings ) ) {
13061 $ticket_id = (int) $data['filter']['ticket_id']['value'];
13062 $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 ) );
13063 if ( ! empty( $all_ticket_bookings ) ) {
13064 $user_id = $all_bookings->person_id;
13065 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
13066 $bookings_str = wp_json_encode( $all_bookings );
13067 $ticket_bookings_str = wp_json_encode( $all_ticket_bookings );
13068
13069 $context['pluggable_data'] = array_merge(
13070 WordPress::get_user_context( $user_id )
13071 );
13072 if ( is_string( $bookings_str ) && is_string( $ticket_bookings_str ) ) {
13073 $bookings_arr = json_decode( $bookings_str, true );
13074 $ticket_bookings_arr = json_decode( $ticket_bookings_str, true );
13075 if ( is_array( $bookings_arr ) && is_array( $ticket_bookings_arr ) ) {
13076 $context['pluggable_data'] = array_merge(
13077 $context['pluggable_data'],
13078 $bookings_arr,
13079 $ticket_bookings_arr
13080 );
13081 }
13082 }
13083 if ( ! empty( $location ) ) {
13084 $location_str = wp_json_encode( $location );
13085 if ( is_string( $location_str ) ) {
13086 $location_arr = json_decode( $location_str, true );
13087 if ( is_array( $location_arr ) ) {
13088 $context['pluggable_data'] = array_merge( $context['pluggable_data'], $location_arr );
13089 }
13090 }
13091 }
13092 $context['response_type'] = 'live';
13093 }
13094 }
13095 }
13096 return $context;
13097 }
13098
13099 /**
13100 * Get Events Manager Events Ticket list.
13101 *
13102 * @param array $data data.
13103 *
13104 * @return array|void
13105 */
13106 public function search_em_event_tickets( $data ) {
13107 global $wpdb;
13108
13109 $options = [];
13110
13111 $event = $data['dynamic']['post_id'];
13112
13113 $event_id = $wpdb->get_var( $wpdb->prepare( "SELECT event_id FROM {$wpdb->prefix}em_events WHERE post_id = %d", $event ) );
13114 $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] ) );
13115
13116 if ( $tickets ) {
13117 foreach ( $tickets as $ticket ) {
13118 $options[] = [
13119 'label' => $ticket->ticket_name,
13120 'value' => $ticket->ticket_id,
13121 ];
13122 }
13123 }
13124
13125 return [
13126 'options' => $options,
13127 'hasMore' => false,
13128 ];
13129 }
13130
13131 /**
13132
13133 * Get learnpress last data.
13134 *
13135 * @param array $data data.
13136 *
13137 * @return array
13138 */
13139 public function search_learnpress_lms_last_data( $data ) {
13140 global $wpdb;
13141 $trigger = $data['search_term'];
13142 $course_data = [];
13143 $lesson_data = [];
13144 $context = [];
13145
13146
13147 if ( 'learnpress_course_completed' === $trigger ) {
13148 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
13149 if ( $course_id > 0 ) {
13150
13151 $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 ) );
13152 } else {
13153
13154 $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" );
13155 }
13156
13157 if ( ! empty( $course ) ) {
13158 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
13159 $context['response_type'] = 'live';
13160 $context['pluggable_data'] = $course_data;
13161 }
13162 } elseif ( 'learnpress_lesson_completed' === $trigger ) {
13163 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
13164 if ( $lesson_id > 0 ) {
13165
13166 $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 ) );
13167 } else {
13168
13169 $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" );
13170 }
13171
13172 if ( ! empty( $lesson ) ) {
13173 $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 ) );
13174 $context['response_type'] = 'live';
13175 $context['pluggable_data'] = $lesson_data;
13176 }
13177 } elseif ( 'learnpress_user_enrolled_in_course' === $trigger ) {
13178 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
13179 if ( $course_id > 0 ) {
13180
13181 $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 ) );
13182 } else {
13183
13184 $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" );
13185 }
13186
13187 if ( ! empty( $course ) ) {
13188 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
13189 $context['response_type'] = 'live';
13190 $context['pluggable_data'] = $course_data;
13191
13192 }
13193 }
13194
13195 return $context;
13196 }
13197
13198 /**
13199 * Get Woocommerce Memberships Plan List.
13200 *
13201 * @param array $data data.
13202 *
13203 * @return array|void
13204 */
13205 public function search_wc_membership_plan_list( $data ) {
13206 global $wpdb;
13207
13208 $page = $data['page'];
13209 $limit = Utilities::get_search_page_limit();
13210 $offset = $limit * ( $page - 1 );
13211
13212 $args = [
13213 'post_type' => 'wc_membership_plan',
13214 'posts_per_page' => $limit,
13215 'offset' => $offset,
13216 'orderby' => 'title',
13217 'order' => 'ASC',
13218 'post_status' => 'publish',
13219 'fields' => 'ids',
13220 ];
13221 $loop = new WP_Query( $args );
13222
13223 $plans = (array) $loop->posts;
13224 $plans_count = wp_count_posts( 'wc_membership_plan' )->publish;
13225
13226 $options = [];
13227 if ( ! empty( $plans ) ) {
13228 if ( is_array( $plans ) ) {
13229 foreach ( $plans as $plan_id ) {
13230 $options[] = [
13231 'label' => get_the_title( $plan_id ),
13232 'value' => $plan_id,
13233 ];
13234 }
13235 }
13236 }
13237
13238 return [
13239 'options' => $options,
13240 'hasMore' => $plans_count > $limit && $plans_count > $offset,
13241 ];
13242 }
13243
13244 /**
13245 * Get BuddyPress Private group.
13246 *
13247 * @param array $data data.
13248 *
13249 * @return array|void
13250 */
13251 public function search_bp_private_group_list( $data ) {
13252 global $wpdb;
13253
13254 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'private'" );
13255
13256 $options = [];
13257 if ( $groups ) {
13258 foreach ( $groups as $group ) {
13259 $options[] = [
13260 'label' => $group->name,
13261 'value' => $group->id,
13262 ];
13263 }
13264 }
13265
13266 return [
13267 'options' => $options,
13268 'hasMore' => false,
13269 ];
13270 }
13271
13272 /**
13273 * Get BuddyPress Public group.
13274 *
13275 * @param array $data data.
13276 *
13277 * @return array|void
13278 */
13279 public function search_bp_public_group_list( $data ) {
13280 global $wpdb;
13281
13282 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'public'" );
13283
13284 $options = [];
13285 if ( $groups ) {
13286 foreach ( $groups as $group ) {
13287 $options[] = [
13288 'label' => $group->name,
13289 'value' => $group->id,
13290 ];
13291 }
13292 }
13293
13294 return [
13295 'options' => $options,
13296 'hasMore' => false,
13297 ];
13298 }
13299
13300 /**
13301 * Get BuddyPress group.
13302 *
13303 * @param array $data data.
13304 *
13305 * @return array|void
13306 */
13307 public function search_bp_group_list( $data ) {
13308 global $wpdb;
13309
13310 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
13311
13312 $options = [];
13313 if ( $groups ) {
13314 foreach ( $groups as $group ) {
13315 $options[] = [
13316 'label' => $group->name,
13317 'value' => $group->id,
13318 ];
13319 }
13320 }
13321
13322 return [
13323 'options' => $options,
13324 'hasMore' => false,
13325 ];
13326 }
13327
13328 /**
13329 * Get BuddyPress field.
13330 *
13331 * @param array $data data.
13332 *
13333 * @return array|void
13334 */
13335 public function search_bp_field_list( $data ) {
13336 global $wpdb;
13337
13338 $base_group_id = 1;
13339 if ( function_exists( 'bp_xprofile_base_group_id' ) ) {
13340 $base_group_id = bp_xprofile_base_group_id();
13341 }
13342
13343 $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 ) );
13344
13345 $options = [];
13346 if ( ! empty( $xprofile_fields ) ) {
13347 foreach ( $xprofile_fields as $xprofile_field ) {
13348 $options[] = [
13349 'label' => $xprofile_field->name,
13350 'value' => $xprofile_field->id,
13351 ];
13352 }
13353 }
13354
13355 return [
13356 'options' => $options,
13357 'hasMore' => false,
13358 ];
13359 }
13360
13361 /**
13362 * Get BuddyPress member type.
13363 *
13364 * @param array $data data.
13365 *
13366 * @return array|void
13367 */
13368 public function search_bp_member_type_list( $data ) {
13369 $options = [];
13370 if ( function_exists( 'bp_get_member_types' ) ) {
13371 $types = bp_get_member_types( [] );
13372 if ( $types ) {
13373 foreach ( $types as $key => $type ) {
13374 $options[] = [
13375 'label' => $type,
13376 'value' => $key,
13377 ];
13378 }
13379 }
13380 }
13381
13382 return [
13383 'options' => $options,
13384 'hasMore' => false,
13385 ];
13386 }
13387
13388 /**
13389 * Get last data for WP All Import.
13390 *
13391 * @param array $data data.
13392 * @return mixed
13393 */
13394 public function search_wp_all_import_last_data( $data ) {
13395 global $wpdb;
13396 $post_type = $data['filter']['post_type']['value'];
13397 $trigger = $data['search_term'];
13398
13399 if ( 'wp_all_import_post_type_imported' === $trigger ) {
13400 if ( -1 == $post_type ) {
13401 $imports = $wpdb->get_row( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts ORDER BY id DESC LIMIT 1", ARRAY_A );
13402 $posts[0] = $imports['post_id'];
13403 } else {
13404 $imports = $wpdb->get_results( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts", ARRAY_A );
13405 $imports = array_column( $imports, 'post_id' );
13406 $args = [
13407 'posts_per_page' => 1,
13408 'post_type' => $post_type,
13409 'post__in' => $imports,
13410 ];
13411 $posts = get_posts( $args );
13412 }
13413 } elseif ( 'wp_all_import_completed' === $trigger ) {
13414 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 0 ORDER BY id DESC LIMIT 1", ARRAY_A );
13415 } elseif ( 'wp_all_import_failed' === $trigger ) {
13416 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 1 ORDER BY id DESC LIMIT 1", ARRAY_A );
13417 }
13418
13419 if ( 'wp_all_import_post_type_imported' === $trigger && empty( $imports ) ) {
13420 $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 );
13421 return $context;
13422 } elseif ( empty( $imports ) ) {
13423 $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 );
13424 return $context;
13425 }
13426
13427 $context['response_type'] = 'live';
13428 if ( ! empty( $posts ) ) {
13429 $context['pluggable_data'] = WordPress::get_post_context( $posts[0] );
13430 } else {
13431 $context['pluggable_data'] = $imports;
13432 }
13433
13434 return $context;
13435 }
13436
13437 /**
13438 * Get Wp Simple Pay Forms.
13439 *
13440 * @param array $data data.
13441 *
13442 * @return array
13443 */
13444 public function search_wp_simple_pay_forms( $data ) {
13445
13446 $page = $data['page'];
13447 $limit = Utilities::get_search_page_limit();
13448 $offset = $limit * ( $page - 1 );
13449
13450 $forms = get_posts(
13451 [
13452 'post_type' => 'simple-pay',
13453 'posts_per_page' => $limit,
13454 'offset' => $offset,
13455 'fields' => 'ids',
13456 ]
13457 );
13458
13459 $forms_count = wp_count_posts( 'simple-pay' )->publish;
13460
13461 $options = [];
13462
13463 if ( ! empty( $forms ) ) {
13464 foreach ( $forms as $form_id ) {
13465 if ( function_exists( 'simpay_get_form' ) ) {
13466 $form = simpay_get_form( $form_id );
13467 $options[] = [
13468 'label' => null !== get_the_title( $form_id ) ? $form->company_name : get_the_title( $form_id ),
13469 'value' => $form_id,
13470 ];
13471 }
13472 }
13473 }
13474
13475 return [
13476 'options' => $options,
13477 'hasMore' => $forms_count > $limit && $forms_count > $offset,
13478 ];
13479 }
13480
13481 /**
13482 * Get Post list as per post type for metabox.
13483 *
13484 * @param array $data data.
13485 *
13486 * @return array
13487 */
13488 public function search_mb_posts_list( $data ) {
13489
13490 $page = $data['page'];
13491 $limit = Utilities::get_search_page_limit();
13492 $offset = $limit * ( $page - 1 );
13493
13494 $posts = get_posts(
13495 [
13496 'post_type' => $data['dynamic'],
13497 'posts_per_page' => $limit,
13498 'offset' => $offset,
13499 'fields' => 'ids',
13500 ]
13501 );
13502
13503 $all_posts = get_posts(
13504 [
13505 'post_type' => $data['dynamic'],
13506 'posts_per_page' => -1,
13507 'fields' => 'ids',
13508 ]
13509 );
13510
13511 $posts_count = count( $all_posts );
13512
13513 $options = [];
13514
13515 if ( ! empty( $posts ) ) {
13516 foreach ( $posts as $post ) {
13517 $title = html_entity_decode( get_the_title( $post ), ENT_QUOTES, 'UTF-8' );
13518 $options[] = [
13519 'label' => $title,
13520 'value' => $post,
13521 ];
13522 }
13523 }
13524
13525 return [
13526 'options' => $options,
13527 'hasMore' => $posts_count > $limit && $posts_count > $offset,
13528 ];
13529 }
13530
13531 /**
13532 * Get Metabox Custom box in Post list.
13533 *
13534 * @param array $data data.
13535 *
13536 * @return array
13537 */
13538 public function search_mb_field_list( $data ) {
13539
13540 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
13541 return [];
13542 }
13543
13544 $options = [];
13545
13546 $metabox_fields = (array) rwmb_get_object_fields( $data['dynamic'] );
13547
13548 foreach ( $metabox_fields as $metabox_field ) {
13549
13550 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
13551
13552 $options[] = [
13553 'label' => $metabox_field['name'],
13554 'value' => $metabox_field['id'],
13555 ];
13556
13557 }
13558 }
13559
13560 return [
13561 'options' => $options,
13562 'hasMore' => false,
13563 ];
13564 }
13565
13566 /**
13567 * Get Metabox Custom box user list.
13568 *
13569 * @param array $data data.
13570 *
13571 * @return array
13572 */
13573 public function search_mb_user_field_list( $data ) {
13574
13575 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
13576 return [];
13577 }
13578
13579 $options = [];
13580
13581 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
13582
13583 foreach ( $metabox_fields as $metabox_field ) {
13584
13585 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
13586
13587 $options[] = [
13588 'label' => $metabox_field['name'],
13589 'value' => $metabox_field['id'],
13590 ];
13591
13592 }
13593 }
13594
13595 return [
13596 'options' => $options,
13597 'hasMore' => false,
13598 ];
13599 }
13600
13601 /**
13602 * Search Last Updated Field Data for MetaBox.
13603 *
13604 * @param array $data data.
13605 * @return array
13606 */
13607 public function search_meta_box_field_data( $data ) {
13608 global $wpdb;
13609
13610 $context = [];
13611
13612 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
13613
13614 $post_type = $data['filter']['wp_post_type']['value'];
13615 $post = $data['filter']['wp_post']['value'];
13616
13617 if ( -1 === $field ) {
13618 if ( function_exists( 'rwmb_get_object_fields' ) ) {
13619 $metaboxes = rwmb_get_object_fields( $post_type );
13620
13621 if ( ! empty( $metaboxes ) ) {
13622 $random_key = array_rand( $metaboxes );
13623 $field = $random_key;
13624 } else {
13625 $result = '';
13626 }
13627 }
13628 } else {
13629 $field = $data['filter']['field_id']['value'];
13630 }
13631
13632 if ( function_exists( 'rwmb_meta' ) ) {
13633 $result = rwmb_meta( $field, '', $post );
13634 }
13635
13636 $response = [];
13637 if ( ! empty( $result ) ) {
13638 $response['pluggable_data'] = array_merge( [ $field => $result ], WordPress::get_post_context( $post ) );
13639 $response['response_type'] = 'live';
13640 } else {
13641 $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 );
13642 }
13643
13644 return $response;
13645 }
13646
13647 /**
13648 * Search Last Updated User Field Data MetaBox.
13649 *
13650 * @param array $data data.
13651 * @return array
13652 */
13653 public function search_user_meta_box_field_data( $data ) {
13654 global $wpdb;
13655
13656 $context = [];
13657
13658 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
13659
13660 if ( -1 === $field ) {
13661 if ( function_exists( 'rwmb_get_object_fields' ) ) {
13662 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
13663
13664 if ( ! empty( $metabox_fields ) ) {
13665 $random_key = array_rand( $metabox_fields );
13666 $field = $random_key;
13667 } else {
13668 $result = '';
13669 }
13670 }
13671 } else {
13672 $field = $data['filter']['field_id']['value'];
13673 }
13674
13675 $users = get_users(
13676 [
13677 'fields' => 'ID',
13678 'meta_key' => $field,
13679 ]
13680 );
13681
13682 if ( ! empty( $users ) ) {
13683 $user_random_key = array_rand( $users );
13684 $user_id = $user_random_key;
13685 if ( function_exists( 'rwmb_get_value' ) ) {
13686 $result = rwmb_get_value( $field, [ 'object_type' => 'user' ], $users[ $user_id ] );
13687 }
13688
13689 $response = [];
13690 if ( ! empty( $result ) ) {
13691 $context = [
13692 'field_id' => $field,
13693 $field => $result,
13694 'user' => WordPress::get_user_context( $users[ $user_id ] ),
13695 ];
13696 $response['pluggable_data'] = $context;
13697 $response['response_type'] = 'live';
13698 } else {
13699 $response = json_decode(
13700 '{
13701 "response_type": "sample",
13702 "pluggable_data": {
13703 "field_id": "gender",
13704 "user": {
13705 "wp_user_id": 114,
13706 "user_login": "test",
13707 "display_name": "test",
13708 "user_firstname": "test",
13709 "user_lastname": "test",
13710 "user_email": "test@test.com",
13711 "user_role": [ "subscriber" ]
13712 }
13713 }
13714 }',
13715 true
13716 );
13717 }
13718 } else {
13719 $response = json_decode(
13720 '{
13721 "response_type": "sample",
13722 "pluggable_data": {
13723 "field_id": "gender",
13724 "user": {
13725 "wp_user_id": 114,
13726 "user_login": "test",
13727 "display_name": "test",
13728 "user_firstname": "test",
13729 "user_lastname": "test",
13730 "user_email": "test@test.com",
13731 "user_role": [ "subscriber" ]
13732 }
13733 }
13734 }',
13735 true
13736 );
13737 }
13738
13739 return $response;
13740 }
13741
13742 /**
13743 * Search forms of Pie Forms.
13744 *
13745 * @param array $data data.
13746 * @return array
13747 */
13748 public function search_wp_polls_list( $data ) {
13749 global $wpdb;
13750 $options = [];
13751
13752 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsq' ) ) ) {
13753
13754 $results = $wpdb->get_results( 'SELECT pollq_id, pollq_question FROM ' . $wpdb->prefix . 'pollsq WHERE pollq_active = 1' );
13755
13756 if ( $results ) {
13757 foreach ( $results as $result ) {
13758 $options[] = [
13759 'label' => $result->pollq_question,
13760 'value' => $result->pollq_id,
13761 ];
13762 }
13763 }
13764 }
13765
13766 return [
13767 'options' => $options,
13768 'hasMore' => false,
13769 ];
13770 }
13771
13772 /**
13773 * Search answers of WP-Polls questions.
13774 *
13775 * @param array $data data.
13776 * @return array
13777 */
13778 public function search_wp_polls_answers( $data ) {
13779 global $wpdb;
13780
13781 $options = [];
13782 $poll_id = $data['dynamic'];
13783
13784 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsa' ) ) ) {
13785
13786 if ( '-1' !== $poll_id ) {
13787 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa WHERE polla_qid = %d', $poll_id ) );
13788 } else {
13789 $results = $wpdb->get_results( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa' );
13790 }
13791
13792 if ( $results ) {
13793 foreach ( $results as $result ) {
13794 $options[] = [
13795 'label' => $result->polla_answers,
13796 'value' => $result->polla_aid,
13797 ];
13798 }
13799 }
13800 }
13801
13802 return [
13803 'options' => $options,
13804 'hasMore' => false,
13805 ];
13806 }
13807
13808 /**
13809 * Get last data for trigger.
13810 *
13811 * @param array $data data.
13812 * @return array
13813 */
13814 public function search_wp_polls_triggers_last_data( $data ) {
13815 global $wpdb;
13816
13817 $context = [];
13818 $context['response_type'] = 'sample';
13819
13820 $poll = [
13821 'poll_id' => 1,
13822 'question' => 'Which skills are you interested to learn?',
13823 'answers' => 'Web Development, Graphic Designing, Content Writing, Digital Marketing',
13824 'start_date' => '2023-08-29 17:19:13',
13825 'end_date' => 'Not set',
13826 'selected_answers' => 'Content Writing, Web Development',
13827 'selected_answer_id' => 2,
13828 ];
13829
13830 $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" );
13831
13832 if ( ! empty( $poll_data ) ) {
13833 $poll = WpPolls::get_poll_context( (string) $poll_data->answer_id, (int) $poll_data->poll_id );
13834 $poll['selected_answer_id'] = (int) $poll_data->answer_id;
13835
13836 $context['response_type'] = 'live';
13837 }
13838
13839 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
13840
13841 if ( 'poll_submitted' === $term ) {
13842 unset( $poll['selected_answer_id'] );
13843 }
13844
13845 $context['pluggable_data'] = $poll;
13846
13847 return $context;
13848 }
13849
13850 /**
13851 * Get ACF Custom fields list.
13852 *
13853 * @param array $data data.
13854 *
13855 * @return array
13856 */
13857 public function search_acf_post_field_list( $data ) {
13858
13859 $post_id = $data['dynamic']['wp_post'];
13860 $selected_post_type = $data['dynamic']['wp_post_type'];
13861
13862 if ( -1 === $post_id ) {
13863 $args = [
13864 'numberposts' => 1,
13865 'fields' => 'ids',
13866 'orderby' => 'rand',
13867 'post_type' => $selected_post_type,
13868 ];
13869 $posts = get_posts( $args );
13870 $post_id = $posts[0];
13871 }
13872
13873 $args = [
13874 'post_id' => $post_id,
13875 ];
13876 if ( ! is_numeric( $post_id ) ) {
13877 $args = [
13878 'post_type' => $post_id,
13879 ];
13880 }
13881 $options = [];
13882 if ( function_exists( 'acf_get_field_groups' ) ) {
13883 $field_groups_collection = acf_get_field_groups( $args );
13884 foreach ( $field_groups_collection as $field_group ) {
13885 if ( function_exists( 'acf_get_fields' ) ) {
13886 $field_groups[] = acf_get_fields( $field_group['key'] );
13887 }
13888 }
13889
13890 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
13891 foreach ( $field_groups as $field_groups ) {
13892 foreach ( $field_groups as $field_group ) {
13893 $options[] = [
13894 'value' => $field_group['name'],
13895 'label' => ! empty( $field_group['label'] ) ? $field_group['label'] : $field_group['name'],
13896 ];
13897 }
13898 }
13899 }
13900 }
13901
13902 return [
13903 'options' => $options,
13904 'hasMore' => false,
13905 ];
13906 }
13907
13908 /**
13909 * Get ACF Custom fields list.
13910 *
13911 * @param array $data data.
13912 *
13913 * @return array
13914 */
13915 public function search_acf_user_field_list( $data ) {
13916
13917 if ( ! function_exists( 'acf_get_fields' ) ) {
13918 return [];
13919 }
13920 if ( ! function_exists( 'acf_get_field_groups' ) ) {
13921 return [];
13922 }
13923 $groups_user_form = [];
13924 $options = [];
13925 if ( function_exists( 'acf_get_field_groups' ) ) {
13926 $field_groups = acf_get_field_groups();
13927 foreach ( $field_groups as $group ) {
13928 if ( ! empty( $group['location'] ) ) {
13929 foreach ( $group['location'] as $locations ) {
13930 foreach ( $locations as $location ) {
13931 if ( 'user_form' === $location['param'] || 'user_role' === $location['param'] || 'current_user' === $location['param'] || 'current_user_role' === $location['param'] ) {
13932 $groups_user_form[] = $group;
13933 }
13934 }
13935 }
13936 }
13937 }
13938
13939 if ( empty( $groups_user_form ) ) {
13940 return [];
13941 }
13942
13943 $key_values = array_map(
13944 function ( $item ) {
13945 return $item['key'];
13946 },
13947 $groups_user_form
13948 );
13949 $unique_keys = array_unique( $key_values );
13950 $unique_array = array_intersect_key( $groups_user_form, $unique_keys );
13951
13952 foreach ( $unique_array as $group ) {
13953 if ( function_exists( 'acf_get_fields' ) ) {
13954 $group_fields = acf_get_fields( $group['key'] );
13955 }
13956 if ( ! empty( $group_fields ) ) {
13957 foreach ( $group_fields as $field ) {
13958 $options[] = [
13959 'value' => $field['name'],
13960 'label' => $field['label'],
13961 ];
13962
13963 }
13964 }
13965 }
13966 }
13967
13968 return [
13969 'options' => $options,
13970 'hasMore' => false,
13971 ];
13972 }
13973
13974 /**
13975 * Get ACF Custom fields list.
13976 *
13977 * @param array $data data.
13978 *
13979 * @return array
13980 */
13981 public function search_acf_options_field_list( $data ) {
13982
13983 if ( ! function_exists( 'acf_get_fields' ) ) {
13984 return [];
13985 }
13986 if ( ! function_exists( 'acf_get_field_groups' ) ) {
13987 return [];
13988 }
13989 $groups_options_form = [];
13990 $options = [];
13991 if ( function_exists( 'acf_get_field_groups' ) ) {
13992 $field_groups = acf_get_field_groups();
13993 foreach ( $field_groups as $group ) {
13994 if ( ! empty( $group['location'] ) ) {
13995 foreach ( $group['location'] as $locations ) {
13996 foreach ( $locations as $location ) {
13997 if ( 'options_page' === $location['param'] ) {
13998 $groups_options_form[] = $group;
13999 }
14000 }
14001 }
14002 }
14003 }
14004 if ( empty( $groups_options_form ) ) {
14005 return [];
14006 }
14007 $key_values = array_map(
14008 function ( $item ) {
14009 return $item['key'];
14010 },
14011 $groups_options_form
14012 );
14013 $unique_keys = array_unique( $key_values );
14014 $unique_array = array_intersect_key( $groups_options_form, $unique_keys );
14015 foreach ( $unique_array as $group ) {
14016 if ( function_exists( 'acf_get_fields' ) ) {
14017 $group_fields = acf_get_fields( $group['key'] );
14018 }
14019 if ( ! empty( $group_fields ) ) {
14020 foreach ( $group_fields as $field ) {
14021 $options[] = [
14022 'value' => $field['name'],
14023 'label' => $field['label'],
14024 ];
14025
14026 }
14027 }
14028 }
14029 }
14030
14031 return [
14032 'options' => $options,
14033 'hasMore' => false,
14034 ];
14035 }
14036
14037 /**
14038 * Search Last Updated Field Data for ACF.
14039 *
14040 * @param array $data data.
14041 * @return array
14042 */
14043 public function search_acf_post_field_data( $data ) {
14044 $context = [];
14045
14046 $field = ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
14047
14048 $post_type = $data['filter']['wp_post_type']['value'];
14049 $post = $data['filter']['wp_post']['value'];
14050
14051 if ( -1 === $post ) {
14052 $args = [
14053 'numberposts' => 1,
14054 'fields' => 'ids',
14055 'orderby' => 'rand',
14056 'post_type' => $post_type,
14057 ];
14058 $posts = get_posts( $args );
14059 $post = $posts[0];
14060 }
14061 if ( -1 === $field ) {
14062 $args = [
14063 'post_id' => $post,
14064 ];
14065 if ( function_exists( 'acf_get_field_groups' ) ) {
14066 $field_groups_collection = acf_get_field_groups( $args );
14067 }
14068 if ( ! empty( $field_groups_collection ) ) {
14069 foreach ( $field_groups_collection as $field_group ) {
14070 if ( function_exists( 'acf_get_fields' ) ) {
14071 $field_groups[] = acf_get_fields( $field_group['key'] );
14072 }
14073 }
14074 }
14075 $fields = [];
14076 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
14077 foreach ( $field_groups as $field_groups ) {
14078 $fields[] = $field_groups;
14079 }
14080 }
14081 if ( ! empty( $fields ) ) {
14082 $random_key = array_rand( $fields[0] );
14083 $field_key = $fields[0][ $random_key ];
14084 $field = $field_key['name'];
14085 } else {
14086 $result = '';
14087 }
14088 } else {
14089 $field = $data['filter']['field_id']['value'];
14090 }
14091 if ( function_exists( ( 'get_field' ) ) ) {
14092 $result = get_field( $field, $post );
14093 }
14094
14095 $response = [];
14096 if ( ! empty( $result ) ) {
14097 $post_fields = [];
14098 if ( function_exists( 'get_fields' ) ) {
14099 $post_fields = get_fields( $post );
14100 }
14101 $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 ) ] );
14102 $response['response_type'] = 'live';
14103 } else {
14104 $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 );
14105 }
14106
14107 return $response;
14108 }
14109
14110 /**
14111 * Search Last Updated User Field Data ACF.
14112 *
14113 * @param array $data data.
14114 * @return array
14115 */
14116 public function search_acf_user_field_data( $data ) {
14117 global $wpdb;
14118
14119 $context = [];
14120
14121 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
14122
14123 if ( -1 === $field ) {
14124 $groups_user_form = [];
14125 if ( function_exists( 'acf_get_field_groups' ) ) {
14126 $field_groups = acf_get_field_groups();
14127 }
14128 if ( ! empty( $field_groups ) ) {
14129 foreach ( $field_groups as $group ) {
14130 if ( ! empty( $group['location'] ) ) {
14131 foreach ( $group['location'] as $locations ) {
14132 foreach ( $locations as $location ) {
14133 if ( 'user_form' === $location['param'] || 'user_role' === $location['param'] || 'current_user' === $location['param'] || 'current_user_role' === $location['param'] ) {
14134 $groups_user_form[] = $group;
14135 }
14136 }
14137 }
14138 }
14139 }
14140 $field_groups = $groups_user_form;
14141 }
14142 if ( empty( $field_groups ) ) {
14143 $result = '';
14144 }
14145 $fields = [];
14146 if ( ! empty( $field_groups ) ) {
14147 foreach ( $field_groups as $group ) {
14148 if ( function_exists( 'acf_get_fields' ) ) {
14149 $group_fields = acf_get_fields( $group['key'] );
14150 }
14151 if ( ! empty( $group_fields ) ) {
14152 foreach ( $group_fields as $field ) {
14153 $fields[] = $group_fields;
14154 }
14155 }
14156 }
14157 }
14158 if ( ! empty( $fields ) ) {
14159 $random_key = array_rand( $fields );
14160 $field = $random_key;
14161 } else {
14162 $result = '';
14163 }
14164 } else {
14165 $field = $data['filter']['field_id']['value'];
14166 }
14167 $users = get_users(
14168 [
14169 'fields' => 'ID',
14170 'meta_key' => $field,
14171 ]
14172 );
14173
14174 if ( ! empty( $users ) ) {
14175 $user_random_key = array_rand( $users );
14176 $user_id = $user_random_key;
14177 if ( function_exists( 'get_field' ) ) {
14178 $result = get_field( $field, 'user_' . $users[ $user_id ] );
14179 }
14180 $response = [];
14181 if ( ! empty( $result ) ) {
14182 $context = [
14183 'field_id' => $field,
14184 $field => $result,
14185 'user' => WordPress::get_user_context( $users[ $user_id ] ),
14186 ];
14187 $response['pluggable_data'] = $context;
14188 $response['response_type'] = 'live';
14189 } else {
14190 $response = json_decode(
14191 '{
14192 "response_type": "sample",
14193 "pluggable_data": {
14194 "field_id": "gender",
14195 "user": {
14196 "wp_user_id": 114,
14197 "user_login": "test",
14198 "display_name": "test",
14199 "user_firstname": "test",
14200 "user_lastname": "test",
14201 "user_email": "test@test.com",
14202 "user_role": [ "subscriber" ]
14203 }
14204 }
14205 }',
14206 true
14207 );
14208 }
14209 } else {
14210 $response = json_decode(
14211 '{
14212 "response_type": "sample",
14213 "pluggable_data": {
14214 "field_id": "gender",
14215 "user": {
14216 "wp_user_id": 114,
14217 "user_login": "test",
14218 "display_name": "test",
14219 "user_firstname": "test",
14220 "user_lastname": "test",
14221 "user_email": "test@test.com",
14222 "user_role": [ "subscriber" ]
14223 }
14224 }
14225 }',
14226 true
14227 );
14228 }
14229
14230 return $response;
14231 }
14232
14233 /**
14234 * Search Last Updated Options Field Data ACF.
14235 *
14236 * @param array $data data.
14237 * @return array
14238 */
14239 public function search_acf_options_field_data( $data ) {
14240 global $wpdb;
14241 $context = [];
14242 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
14243
14244 if ( -1 === $field ) {
14245 $groups_options_form = [];
14246 if ( function_exists( 'acf_get_field_groups' ) ) {
14247 $field_groups = acf_get_field_groups();
14248 }
14249 if ( ! empty( $field_groups ) ) {
14250 foreach ( $field_groups as $group ) {
14251 if ( ! empty( $group['location'] ) ) {
14252 foreach ( $group['location'] as $locations ) {
14253 foreach ( $locations as $location ) {
14254 if ( 'options_page' === $location['param'] ) {
14255 $groups_options_form[] = $group;
14256 }
14257 }
14258 }
14259 }
14260 }
14261 }
14262 if ( empty( $groups_options_form ) ) {
14263 $result = '';
14264 }
14265 $key_values = array_map(
14266 function ( $item ) {
14267 return $item['key'];
14268 },
14269 $groups_options_form
14270 );
14271 $unique_keys = array_unique( $key_values );
14272 $unique_array = array_intersect_key( $groups_options_form, $unique_keys );
14273 $fields = [];
14274 if ( ! empty( $unique_array ) ) {
14275 foreach ( $unique_array as $group ) {
14276 if ( function_exists( 'acf_get_fields' ) ) {
14277 $group_fields = acf_get_fields( $group['key'] );
14278 }
14279 if ( ! empty( $group_fields ) ) {
14280 foreach ( $group_fields as $field ) {
14281 $fields[] = $group_fields;
14282 }
14283 }
14284 }
14285 }
14286 if ( ! empty( $fields ) ) {
14287 $random_key = array_rand( $fields );
14288 $field = $random_key;
14289 } else {
14290 $result = '';
14291 }
14292 } else {
14293 $field = $data['filter']['field_id']['value'];
14294 }
14295 if ( function_exists( 'get_field' ) ) {
14296 $option_value = get_field( $field, 'option' );
14297 }
14298 if ( ! empty( $option_value ) ) {
14299 if ( function_exists( 'acf_get_field' ) ) {
14300 $options_fields = acf_get_field( $field );
14301 if ( function_exists( 'acf_maybe_get' ) ) {
14302 $options_page = acf_maybe_get( $options_fields, 'parent' );
14303 }
14304 }
14305 $context = [
14306 'field_id' => $field,
14307 $field => $option_value,
14308 ];
14309 $response['pluggable_data'] = $context;
14310 $response['response_type'] = 'live';
14311 } else {
14312 $response = json_decode(
14313 '{
14314 "response_type": "sample",
14315 "pluggable_data": {
14316 "field_id": "optionpage",
14317 "optionpage": "newoption"
14318 }
14319 }',
14320 true
14321 );
14322 }
14323 return $response;
14324 }
14325
14326 /**
14327 * Get WP Fusion Tags list.
14328 *
14329 * @param array $data data.
14330 *
14331 * @return array
14332 */
14333 public function search_wp_fusion_tag_list( $data ) {
14334
14335 if ( ! function_exists( 'wp_fusion' ) ) {
14336 return [];
14337 }
14338
14339 $options = [];
14340 $tags = wp_fusion()->settings->get( 'available_tags' );
14341
14342 if ( $tags ) {
14343 foreach ( $tags as $t_id => $tag ) {
14344 if ( is_array( $tag ) && isset( $tag['label'] ) ) {
14345 $options[] = [
14346 'value' => $t_id,
14347 'label' => $tag['label'],
14348 ];
14349 } else {
14350 $options[] = [
14351 'value' => $t_id,
14352 'label' => $tag,
14353 ];
14354 }
14355 }
14356 }
14357
14358 return [
14359 'options' => $options,
14360 'hasMore' => false,
14361 ];
14362 }
14363
14364 /**
14365 * Get list of events for Modern Events Calendar.
14366 *
14367 * @param array $data data.
14368 * @return array
14369 */
14370 public function search_mec_events_list( $data ) {
14371 $page = $data['page'];
14372 $limit = Utilities::get_search_page_limit();
14373 $offset = $limit * ( $page - 1 );
14374
14375 $args = [
14376 'post_type' => 'mec-events',
14377 'post_status' => [ 'publish', 'private' ],
14378 'posts_per_page' => -1,
14379 ];
14380 $loop = new WP_Query( $args );
14381 $events_count = count( $loop->posts );
14382
14383 $args = [
14384 'post_type' => 'mec-events',
14385 'posts_per_page' => $limit,
14386 'offset' => $offset,
14387 'orderby' => 'title',
14388 'order' => 'ASC',
14389 'post_status' => [ 'publish', 'private' ],
14390 ];
14391
14392 $loop = new WP_Query( $args );
14393 $events = $loop->posts;
14394
14395 $options = [];
14396 if ( ! empty( $events ) ) {
14397 foreach ( $events as $event ) {
14398 if ( isset( $event->ID ) ) {
14399 $options[] = [
14400 'label' => get_the_title( $event ),
14401 'value' => $event->ID,
14402 ];
14403 }
14404 }
14405 }
14406
14407 return [
14408 'options' => $options,
14409 'hasMore' => $events_count > $limit && $events_count > $offset,
14410 ];
14411 }
14412
14413 /**
14414 * Search tickets of MEC events.
14415 *
14416 * @param array $data data.
14417 * @return array
14418 */
14419 public function search_mec_event_tickets( $data ) {
14420 $options = [];
14421 $event_id = $data['dynamic'];
14422
14423 $event_tickets = get_post_meta( $event_id, 'mec_tickets', true );
14424
14425 if ( ! empty( $event_tickets ) && is_array( $event_tickets ) ) {
14426 foreach ( $event_tickets as $ticket_id => $event_ticket ) {
14427 if ( isset( $event_ticket['name'] ) ) {
14428 $options[] = [
14429 'label' => $event_ticket['name'],
14430 'value' => $ticket_id,
14431 ];
14432 }
14433 }
14434 }
14435
14436 return [
14437 'options' => $options,
14438 'hasMore' => false,
14439 ];
14440 }
14441
14442 /**
14443 * Get last data for trigger.
14444 *
14445 * @param array $data data.
14446 * @return array
14447 */
14448 public function search_mec_triggers_last_data( $data ) {
14449 global $wpdb;
14450
14451 $context = [];
14452 $context['response_type'] = 'sample';
14453
14454 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
14455
14456 if ( 'new_event' === $term ) {
14457 $post_id = $wpdb->get_var( "SELECT post_id FROM {$wpdb->prefix}mec_events ORDER BY id DESC LIMIT 1" );
14458
14459 $live_data = [
14460 'post_id' => $post_id,
14461 'event' => get_post_meta( $post_id ),
14462 'post' => get_post( $post_id ),
14463 ];
14464
14465 if ( ! empty( $post_id ) ) {
14466 $context['pluggable_data'] = $live_data;
14467 $context['response_type'] = 'live';
14468 } else {
14469 $context['pluggable_data'] = [
14470 'post_id' => 272,
14471 'event' => [
14472 'mec_color' => [ 'e14d43' ],
14473 '_thumbnail_id' => [ 229 ],
14474 'mec_location_id' => [ 49 ],
14475 'mec_dont_show_map' => [ 0 ],
14476 'mec_organizer_id' => [ 53 ],
14477 'mec_start_date' => [ '2025-04-01' ],
14478 'mec_start_time_hour' => [ 8 ],
14479 'mec_start_time_minutes' => [ 0 ],
14480 'mec_start_time_ampm' => [ 'AM' ],
14481 'mec_start_day_seconds' => [ 32400 ],
14482 'mec_start_datetime' => [ '2025-04-01 08:00 AM' ],
14483 'mec_end_date' => [ '2025-04-01' ],
14484 '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";}' ],
14485 'mec_end_time_hour' => [ 6 ],
14486 'mec_end_time_minutes' => [ 0 ],
14487 'mec_end_time_ampm' => [ 'PM' ],
14488 'mec_end_day_seconds' => [ 68400 ],
14489 'mec_end_datetime' => [ '2025-04-01 06:00 PM' ],
14490 'mec_allday' => [ 1 ],
14491 'mec_fees' => [ 'a:0:{}' ],
14492 'mec_op' => [ 'a:0:{}' ],
14493 'mec_repeat' => [ 'a:0:{}' ],
14494 'mec_booking' => [ 'a:0:{}' ],
14495 'mec_comment' => [ '' ],
14496 'mec_in_days' => [ '' ],
14497 'mec_tickets' => [ 'a:0:{}' ],
14498 'mec_reg_fields' => [ 'a:0:{}' ],
14499 'mec_hourly_schedules' => [ 'a:0:{}' ],
14500 'mec_ticket_variations' => [ 'a:0:{}' ],
14501 'mec_hide_time' => [ 0 ],
14502 'mec_hide_end_time' => [ 0 ],
14503 'mec_repeat_status' => [ 0 ],
14504 'mec_fees_global_inheritance' => [ 1 ],
14505 'mec_ticket_variations_global_inheritance' => [ 1 ],
14506 'mec_reg_fields_global_inheritance' => [ 1 ],
14507 'mec_event_date_submit' => [ '20250331042811' ],
14508 'mec_new_event_notif_sent' => [ 1 ],
14509 ],
14510 'post' => [
14511 'ID' => 272,
14512 'post_author' => 1,
14513 'post_date' => '2025-03-31 04:28:09',
14514 'post_date_gmt' => '2025-03-31 04:28:09',
14515 'post_content' => '<p>Sample Event</p>',
14516 'post_title' => 'Sample Event',
14517 'post_status' => 'publish',
14518 'comment_status' => 'open',
14519 'ping_status' => 'closed',
14520 'post_name' => 'sample-event',
14521 'guid' => 'http://example.local/events/sample-event/',
14522 'post_type' => 'mec-events',
14523 'comment_count' => 0,
14524 ],
14525 ];
14526 $context['response_type'] = 'sample';
14527 }
14528
14529 return $context;
14530 }
14531
14532 $where = '';
14533 if ( 'cancelled' === $term ) {
14534 $where = 'WHERE verified = -1';
14535 } elseif ( 'confirmed' === $term ) {
14536 $where = 'WHERE confirmed = 1';
14537 } elseif ( 'pending' === $term ) {
14538 $where = 'WHERE confirmed = 0';
14539 }
14540
14541 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
14542
14543 if ( -1 !== $event_id ) {
14544 if ( ! empty( $where ) ) {
14545 $where .= ' AND event_id = ' . $event_id;
14546 } else {
14547 $where = 'WHERE event_id = ' . $event_id;
14548 }
14549 }
14550
14551 $event_data = $wpdb->get_row( "SELECT booking_id FROM {$wpdb->prefix}mec_bookings $where ORDER BY id DESC LIMIT 1" ); // @phpcs:ignore
14552
14553 if ( ! empty( $event_data ) ) {
14554 $event = ModernEventsCalendar::get_event_context( (int) $event_data->booking_id );
14555 $context['response_type'] = 'live';
14556 } else {
14557 $event = [
14558 'event_id' => 1,
14559 'title' => 'Sample Event',
14560 'description' => 'Description of the sample event.',
14561 'categories' => 'New, Sample',
14562 'start_date' => 'September 13, 2023',
14563 'start_time' => '8:00 AM',
14564 'end_date' => 'September 13, 2023',
14565 'end_time' => '11:00 AM',
14566 'location' => 'City Hall',
14567 'organizer' => 'John Doe',
14568 'cost' => '5000',
14569 'featured_image_id' => 1,
14570 'featured_image_url' => 'https://example.com/wp-content/uploads/2022/12/Screenshot_20221127_021332.png',
14571 'tickets' => [
14572 [
14573 'id' => 1,
14574 'name' => 'Silver',
14575 'description' => 'Standard seat with reasonable price.',
14576 'price' => '300',
14577 'price_label' => 'USD',
14578 'limit' => '20',
14579 ],
14580 [
14581 'id' => 2,
14582 'name' => 'Premium',
14583 'description' => 'VIP seat with high price.',
14584 'price' => '500',
14585 'price_label' => 'USD',
14586 'limit' => '10',
14587 ],
14588 ],
14589 'attendees' => [
14590 [
14591 'id' => 1,
14592 'email' => 'johndoe@test.com',
14593 'name' => 'John Doe',
14594 ],
14595 [
14596 'id' => 2,
14597 'email' => 'adamsmith@test.com',
14598 'name' => 'Adam Smith',
14599 ],
14600 ],
14601 'booking' => [
14602 'title' => 'johndoe@test.com - John Doe',
14603 'transaction_id' => 'RSH59404',
14604 'amount_payable' => '800',
14605 'price' => '800',
14606 'time' => '2023-09-07 06:40:32',
14607 'payment_gateway' => 'Manual Pay',
14608 'confirmation_status' => 'Pending',
14609 'verification_status' => 'Verified',
14610 'attendees_count' => 2,
14611 ],
14612 ];
14613 }
14614
14615 $context['pluggable_data'] = $event;
14616 return $context;
14617 }
14618
14619 /**
14620 * Get form list Contact Form 7.
14621 *
14622 * @param array $data data.
14623 *
14624 * @return array
14625 */
14626 public function search_contact_form7_list( $data ) {
14627
14628 $page = $data['page'];
14629 $limit = Utilities::get_search_page_limit();
14630 $offset = $limit * ( $page - 1 );
14631
14632 $posts = get_posts(
14633 [
14634 'post_type' => 'wpcf7_contact_form',
14635 'posts_per_page' => $limit,
14636 'offset' => $offset,
14637 ]
14638 );
14639
14640 $all_posts = get_posts(
14641 [
14642 'post_type' => 'wpcf7_contact_form',
14643 'posts_per_page' => -1,
14644 ]
14645 );
14646
14647 $posts_count = count( $all_posts );
14648
14649 $options = [];
14650
14651 if ( ! empty( $posts ) ) {
14652 foreach ( $posts as $post ) {
14653 $options[] = [
14654 'label' => get_the_title( $post->ID ),
14655 'value' => $post->ID,
14656 ];
14657 }
14658 }
14659
14660 return [
14661 'options' => $options,
14662 'hasMore' => $posts_count > $limit && $posts_count > $offset,
14663 ];
14664 }
14665
14666 /**
14667 * Get Thrive Leads form list
14668 *
14669 * @param array $data data.
14670 *
14671 * @return array
14672 */
14673 public function search_thrive_leads_forms_list( $data ) {
14674 $options = [];
14675
14676 $lg_ids = get_posts(
14677 [
14678 'post_type' => '_tcb_form_settings',
14679 'fields' => 'id=>parent',
14680 'posts_per_page' => -1,
14681 'post_status' => 'any',
14682 ]
14683 );
14684
14685 if ( function_exists( 'tve_leads_get_form_variations' ) ) {
14686 foreach ( $lg_ids as $lg_id => $lg_parent ) {
14687 $variations = tve_leads_get_form_variations( $lg_parent );
14688 foreach ( $variations as $variation ) {
14689 $options[] = [
14690 'label' => $variation['post_title'],
14691 'value' => $lg_parent,
14692 ];
14693 }
14694 }
14695 }
14696
14697 return [
14698 'options' => array_unique( $options ),
14699 'hasMore' => false,
14700 ];
14701 }
14702
14703 /**
14704 * Get Store Engine Products list
14705 *
14706 * @param array $data data.
14707 *
14708 * @return array
14709 */
14710 public function search_store_engine_products( $data ) {
14711 $options = [];
14712
14713 if ( ! class_exists( 'StoreEngine' ) ) {
14714 return $options;
14715 }
14716
14717 $products = get_posts(
14718 [
14719 'post_type' => 'storeengine_product',
14720 'post_status' => 'publish',
14721 'posts_per_page' => -1,
14722 'orderby' => 'title',
14723 'order' => 'ASC',
14724 ]
14725 );
14726
14727 foreach ( $products as $product ) {
14728 $current_price = get_post_meta( $product->ID, '_storeengine_product_price', true );
14729
14730 $price_display = '';
14731 if ( ! empty( $current_price ) && is_numeric( $current_price ) ) {
14732 $price_display = ' - $' . number_format( floatval( $current_price ), 2 );
14733 }
14734
14735 $id_display = ' (ID: ' . $product->ID . ')';
14736
14737 $options[] = [
14738 'label' => $product->post_title . $price_display . $id_display,
14739 'value' => $product->ID,
14740 ];
14741 }
14742
14743 return [
14744 'options' => $options,
14745 'hasMore' => false,
14746 ];
14747 }
14748
14749 /**
14750 * Get all countries list from StoreEngine
14751 *
14752 * @param array $data data.
14753 *
14754 * @return array
14755 */
14756 public function search_store_engine_countries( $data ) {
14757 $options = [];
14758
14759 if ( ! class_exists( 'StoreEngine\Classes\Countries' ) ) {
14760 return [
14761 'options' => $options,
14762 'hasMore' => false,
14763 ];
14764 }
14765
14766 $countries_obj = \StoreEngine\Classes\Countries::get_instance();
14767 $countries = $countries_obj->get_countries();
14768
14769 foreach ( $countries as $code => $name ) {
14770 $options[] = [
14771 'label' => $name . ' (' . $code . ')',
14772 'value' => $code,
14773 ];
14774 }
14775
14776 return [
14777 'options' => $options,
14778 'hasMore' => false,
14779 ];
14780 }
14781
14782 /**
14783 * Get Store Engine Last Data
14784 *
14785 * @param array $data data.
14786 *
14787 * @return array
14788 */
14789 public function search_se_triggers_last_data( $data ) {
14790 $context = [];
14791 global $wpdb;
14792
14793 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
14794
14795 if ( 'se_product_purchased' === $term ) {
14796 $product = $wpdb->get_row(
14797 $wpdb->prepare(
14798 "SELECT * FROM {$wpdb->prefix}posts
14799 WHERE post_type = %s
14800 AND post_status = %s
14801 ORDER BY ID DESC
14802 LIMIT 1",
14803 'storeengine_product',
14804 'publish'
14805 ),
14806 ARRAY_A
14807 );
14808
14809 if ( ! empty( $product ) ) {
14810 $product_meta = $wpdb->get_results(
14811 $wpdb->prepare(
14812 "SELECT meta_key, meta_value FROM {$wpdb->prefix}postmeta WHERE post_id = %d",
14813 $product['ID']
14814 ),
14815 ARRAY_A
14816 );
14817
14818 $custom_metas = [];
14819 foreach ( $product_meta as $meta ) {
14820 if ( strpos( $meta['meta_key'], '_storeengine_' ) === 0 ) {
14821 $clean_key = ltrim( $meta['meta_key'], '_' );
14822 $custom_metas[ $clean_key ] = maybe_unserialize( $meta['meta_value'] );
14823 }
14824 }
14825
14826 $table_exists = $wpdb->get_var(
14827 $wpdb->prepare(
14828 'SHOW TABLES LIKE %s',
14829 $wpdb->prefix . 'storeengine_orders'
14830 )
14831 );
14832
14833 $order = null;
14834 if ( $table_exists ) {
14835 $order = $wpdb->get_row(
14836 $wpdb->prepare(
14837 "SELECT * FROM {$wpdb->prefix}storeengine_orders
14838 ORDER BY id DESC
14839 LIMIT %d",
14840 1
14841 ),
14842 ARRAY_A
14843 );
14844 }
14845
14846 $customer_id = isset( $order['customer_id'] ) ? $order['customer_id'] : null;
14847 $user = null;
14848 if ( $customer_id ) {
14849 $user = get_userdata( $customer_id );
14850 } else {
14851 $customers = get_users(
14852 [
14853 'role__in' => [ 'storeengine_customer', 'customer' ],
14854 'number' => 1,
14855 'orderby' => 'ID',
14856 'order' => 'DESC',
14857 ]
14858 );
14859 if ( ! empty( $customers ) ) {
14860 $user = $customers[0];
14861 } else {
14862 $user = get_userdata( $product['post_author'] );
14863 }
14864 }
14865
14866 $product_data = [
14867 'id' => (int) $product['ID'],
14868 'guid' => $product['guid'],
14869 'post' => (int) $product['ID'],
14870 'filter' => 'raw',
14871 'pinged' => $product['pinged'],
14872 'to_ping' => $product['to_ping'],
14873 'order_id' => '',
14874 'permalink' => get_permalink( $product['ID'] ),
14875 'post_date' => $product['post_date'],
14876 'post_name' => $product['post_name'],
14877 'post_type' => $product['post_type'],
14878 'user_role' => $user ? $user->roles : [],
14879 'membership_details' => [
14880 'expiration_specific_date' => '',
14881 'expiration_fixed_date_duration' => 0,
14882 'is_enable_expiration' => 0,
14883 'content_protects_rules' => [],
14884 'membership_plan' => [
14885 'id' => null,
14886 'name' => '',
14887 'slug' => '',
14888 'status' => '',
14889 ],
14890 ],
14891 'menu_order' => (int) $product['menu_order'],
14892 'post_title' => $product['post_title'],
14893 'user_email' => $user ? $user->user_email : '',
14894 'user_login' => $user ? $user->user_login : '',
14895 'wp_user_id' => $user ? $user->ID : 0,
14896 'ping_status' => $product['ping_status'],
14897 'post_author' => (int) $product['post_author'],
14898 'post_parent' => (int) $product['post_parent'],
14899 'post_status' => $product['post_status'],
14900 'custom_metas' => $custom_metas,
14901 'display_name' => $user ? $user->display_name : '',
14902 'post_content' => $product['post_content'],
14903 'post_excerpt' => $product['post_excerpt'],
14904 'comment_count' => (int) $product['comment_count'],
14905 'post_date_gmt' => $product['post_date_gmt'],
14906 'post_modified' => $product['post_modified'],
14907 'post_password' => $product['post_password'],
14908 'user_lastname' => $user ? get_user_meta( $user->ID, 'last_name', true ) : '',
14909 'comment_status' => $product['comment_status'],
14910 'post_mime_type' => $product['post_mime_type'],
14911 'transaction_id' => '',
14912 'user_firstname' => $user ? get_user_meta( $user->ID, 'first_name', true ) : '',
14913 'user_registered' => $user ? $user->user_registered : '',
14914 'post_modified_gmt' => $product['post_modified_gmt'],
14915 'post_content_filtered' => $product['post_content_filtered'],
14916 ];
14917
14918 $integration = $wpdb->get_row(
14919 $wpdb->prepare(
14920 "SELECT integration_id, price_id FROM {$wpdb->prefix}storeengine_integrations
14921 WHERE product_id = %d AND provider = %s
14922 LIMIT 1",
14923 $product['ID'],
14924 'storeengine/membership-addon'
14925 )
14926 );
14927
14928 $membership_plan_id = isset( $integration->integration_id ) ? $integration->integration_id : null;
14929 if ( $membership_plan_id ) {
14930 $access_group = get_post( $membership_plan_id );
14931 if ( 'storeengine_groups' === $access_group && $access_group->post_type ) {
14932 $product_data['membership_details']['membership_plan'] = [
14933 'id' => (int) $membership_plan_id,
14934 'name' => $access_group->post_title,
14935 'slug' => $access_group->post_name,
14936 'status' => $access_group->post_status,
14937 ];
14938
14939 $expiration_data = get_post_meta( $membership_plan_id, '_storeengine_membership_expiration', true );
14940 if ( $expiration_data && is_array( $expiration_data ) ) {
14941 $product_data['membership_details']['expiration_specific_date'] = isset( $expiration_data['specific_date'] ) ? $expiration_data['specific_date'] : '';
14942 $product_data['membership_details']['expiration_fixed_date_duration'] = isset( $expiration_data['fixed_date_duration'] ) ? (int) $expiration_data['fixed_date_duration'] : 0;
14943 $product_data['membership_details']['is_enable_expiration'] = isset( $expiration_data['is_enable'] ) ? (int) $expiration_data['is_enable'] : 0;}
14944
14945 $content_protects = get_post_meta( $membership_plan_id, '_storeengine_membership_content_protect_types', true );
14946 if ( $content_protects && is_array( $content_protects ) ) {
14947 $product_data['membership_details']['content_protects_rules'] = $content_protects;
14948 }
14949 }
14950 }
14951
14952 if ( $order ) {
14953 $product_data['order_id'] = (int) $order['id'];
14954 $product_data['transaction_id'] = isset( $order['transaction_id'] ) ? $order['transaction_id'] : '';
14955 }
14956
14957 $context['pluggable_data'] = $product_data;
14958 $context['response_type'] = 'live';
14959
14960 } else {
14961 $sample_data = [
14962 'id' => 53,
14963 'guid' => 'http://suretriggers-wp.local/product/product-2/',
14964 'post' => 53,
14965 'filter' => 'raw',
14966 'pinged' => '',
14967 'to_ping' => '',
14968 'order_id' => 43,
14969 'permalink' => 'http://suretriggers-wp.local/product/product-2/',
14970 'post_date' => '2025-07-24 07:01:01',
14971 'post_name' => 'product-2',
14972 'post_type' => 'storeengine_product',
14973 'user_role' => [ 'storeengine_customer' ],
14974 'membership_details' => [
14975 'expiration_specific_date' => '',
14976 'expiration_fixed_date_duration' => 0,
14977 'is_enable_expiration' => 0,
14978 'content_protects_rules' => [],
14979 'membership_plan' => [
14980 'id' => null,
14981 'name' => '',
14982 'slug' => '',
14983 'status' => '',
14984 ],
14985 ],
14986 'menu_order' => 0,
14987 'post_title' => 'Product 2',
14988 'user_email' => 'mucool@gmail.com',
14989 'user_login' => 'mucool',
14990 'wp_user_id' => 12,
14991 'ping_status' => 'open',
14992 'post_author' => 1,
14993 'post_parent' => 0,
14994 'post_status' => 'publish',
14995 'custom_metas' => [
14996 'storeengine_product_hide' => '',
14997 'storeengine_product_type' => 'simple',
14998 'storeengine_product_shipping_type' => 'digital',
14999 'storeengine_product_physical_weight' => '',
15000 'storeengine_product_physical_weight_unit' => 'g',
15001 'storeengine_product_digital_auto_complete' => 1,
15002 'storeengine_product_integrated_plan_id' => 99,
15003 'storeengine_product_gallery_ids' => [],
15004 'storeengine_product_downloadable_files' => [
15005 [
15006 'id' => '9bd1194a-0406-46f3-aff3-dc539cdc791f',
15007 'name' => '',
15008 'file' => 'http://suretriggers-wp.local/wp-content/uploads/2025/07/bd0f98d1bede01622fe7f03a4f8fdc2155abafb4.jpg',
15009 'enabled' => 1,
15010 ],
15011 ],
15012 'storeengine_upsell_ids' => [],
15013 'storeengine_crosssell_ids' => [],
15014 ],
15015 'display_name' => 'mucool titirmare',
15016 'post_content' => '<p>sdfdsfsd</p>',
15017 'post_excerpt' => '',
15018 'comment_count' => 0,
15019 'post_date_gmt' => '2025-07-24 07:01:01',
15020 'post_modified' => '2025-07-24 08:14:01',
15021 'post_password' => '',
15022 'user_lastname' => 'titirmare',
15023 'comment_status' => 'open',
15024 'post_mime_type' => '',
15025 'transaction_id' => '',
15026 'user_firstname' => 'mucool',
15027 'user_registered' => '2025-07-24 08:16:48',
15028 'post_modified_gmt' => '2025-07-24 08:14:01',
15029 'post_content_filtered' => '',
15030 ];
15031
15032 $context['pluggable_data'] = $sample_data;
15033 $context['response_type'] = 'sample';
15034 }
15035 }
15036
15037 return $context;
15038 }
15039
15040
15041
15042 /**
15043 * Get list for Woocommerce Subscriptions
15044 *
15045 * @param array $data data.
15046 *
15047 * @return array
15048 */
15049 public function search_wc_subscription_variation_products( $data ) {
15050 $options = [];
15051
15052 global $wpdb;
15053
15054 if ( ! function_exists( 'wc_get_product' ) ) {
15055 return [];
15056 }
15057 $subscriptions = $wpdb->get_results(
15058 $wpdb->prepare(
15059 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
15060 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
15061 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
15062 AND posts.post_type = %s
15063 AND posts.post_status = %s
15064 UNION ALL
15065 SELECT ID, post_title FROM $wpdb->posts
15066 WHERE post_type = %s
15067 AND post_status = %s
15068 ORDER BY post_title",
15069 'product',
15070 'publish',
15071 'shop_subscription',
15072 'publish'
15073 )
15074 );
15075 $page = $data['page'];
15076 $limit = Utilities::get_search_page_limit();
15077 $offset = $limit * ( $page - 1 );
15078 $subscriptions_products = [];
15079 $subscription_products_count = 10;
15080 if ( function_exists( 'wc_get_products' ) ) {
15081 $product_query = wc_get_products(
15082 [
15083 'type' => [ 'subscription', 'variable-subscription' ],
15084 'posts_per_page' => $limit,
15085 'offset' => $offset,
15086 'paginate' => true,
15087 ]
15088 );
15089 if ( is_object( $product_query ) ) {
15090 $subscriptions_products = $product_query->products;
15091 $subscription_products_count = $product_query->total;
15092 }
15093 }
15094
15095 if ( $subscriptions ) {
15096 foreach ( $subscriptions as $product ) {
15097 $options[] = [
15098 'label' => $product->post_title . ' (#' . $product->ID . ')',
15099 'value' => (int) $product->ID,
15100 ];
15101 $product_s = wc_get_product( $product->ID );
15102 /**
15103 *
15104 * Ignore line
15105 *
15106 * @phpstan-ignore-next-line
15107 */
15108 if ( 'variable-subscription' == $product_s->product_type ) {
15109 $args = [
15110 'post_type' => 'product_variation',
15111 'post_parent' => $product->ID,
15112 'posts_per_page' => -1,
15113 'orderby' => 'ID',
15114 'order' => 'ASC',
15115 'post_status' => 'publish',
15116 ];
15117
15118 $variations = get_posts( $args );
15119
15120 foreach ( $variations as $var ) {
15121 $options[] = [
15122 'label' => $var->post_title . ' (#' . $var->ID . ')',
15123 'value' => $var->ID,
15124 ];
15125 }
15126 }
15127 }
15128 return [
15129 'options' => $options,
15130 'hasMore' => false,
15131 ];
15132 } elseif ( ! empty( $subscriptions_products ) ) {
15133 foreach ( $subscriptions_products as $product ) {
15134 $title = $product->get_name();
15135 $options[] = [
15136 'label' => $title . ' (#' . $product->get_id() . ')',
15137 'value' => $product->get_id(),
15138 ];
15139 $product_s = wc_get_product( $product->get_id() );
15140 /**
15141 *
15142 * Ignore line
15143 *
15144 * @phpstan-ignore-next-line
15145 */
15146 if ( 'variable-subscription' == $product_s->product_type ) {
15147 $args = [
15148 'post_type' => 'product_variation',
15149 'post_parent' => $product->get_id(),
15150 'posts_per_page' => -1,
15151 'orderby' => 'ID',
15152 'order' => 'ASC',
15153 'post_status' => 'publish',
15154 ];
15155
15156 $variations = get_posts( $args );
15157
15158 foreach ( $variations as $var ) {
15159 $options[] = [
15160 'label' => $var->post_title . ' (#' . $var->ID . ')',
15161 'value' => $var->ID,
15162 ];
15163 }
15164 }
15165 }
15166 return [
15167 'options' => $options,
15168 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
15169 ];
15170 } else {
15171 return [
15172 'options' => $options,
15173 'hasMore' => false,
15174 ];
15175 }
15176 }
15177
15178 /**
15179 * Get WS Forms form list
15180 *
15181 * @param array $data data.
15182 *
15183 * @return array
15184 */
15185 public function search_ws_forms_list( $data ) {
15186 $options = [];
15187 global $wpdb;
15188
15189 $forms = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wsf_form", 'ARRAY_A' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
15190
15191 foreach ( $forms as $form ) {
15192 $options[] = [
15193 'label' => $form['label'],
15194 'value' => $form['id'],
15195 ];
15196 }
15197
15198 return [
15199 'options' => $options,
15200 'hasMore' => false,
15201 ];
15202 }
15203
15204 /**
15205 * Get Learndash Achievement list
15206 *
15207 * @param array $data data.
15208 *
15209 * @return array
15210 */
15211 public function search_ld_achievements_list( $data ) {
15212 $page = $data['page'];
15213 $limit = Utilities::get_search_page_limit();
15214 $offset = $limit * ( $page - 1 );
15215
15216 $posts = get_posts(
15217 [
15218 'post_type' => 'ld-achievement',
15219 'posts_per_page' => $limit,
15220 'offset' => $offset,
15221 'post_status' => [ 'publish' ],
15222 ]
15223 );
15224
15225 $posts_count = wp_count_posts( 'ld-achievement' )->publish;
15226
15227 $options = [];
15228
15229 if ( ! empty( $posts ) ) {
15230 foreach ( $posts as $post ) {
15231 $options[] = [
15232 'label' => get_the_title( $post->ID ),
15233 'value' => $post->ID,
15234 ];
15235 }
15236 }
15237
15238 return [
15239 'options' => $options,
15240 'hasMore' => $posts_count > $limit && $posts_count > $offset,
15241 ];
15242 }
15243
15244 /**
15245 * Get Advanced Ads list
15246 *
15247 * @param array $data data.
15248 *
15249 * @return array
15250 */
15251 public function search_ads_list( $data ) {
15252 $page = $data['page'];
15253 $limit = Utilities::get_search_page_limit();
15254 $offset = $limit * ( $page - 1 );
15255
15256 $posts = get_posts(
15257 [
15258 'post_type' => 'advanced_ads',
15259 'posts_per_page' => $limit,
15260 'offset' => $offset,
15261 'post_status' => [ 'publish', 'draft' ],
15262 ]
15263 );
15264
15265 $posts_count = wp_count_posts( 'advanced_ads' )->publish;
15266
15267 $options = [];
15268
15269 if ( ! empty( $posts ) ) {
15270 foreach ( $posts as $post ) {
15271 $options[] = [
15272 'label' => get_the_title( $post->ID ),
15273 'value' => $post->ID,
15274 ];
15275 }
15276 }
15277
15278 return [
15279 'options' => $options,
15280 'hasMore' => $posts_count > $limit && $posts_count > $offset,
15281 ];
15282 }
15283
15284 /**
15285 * Advanced Ads pluggable data.
15286 *
15287 * @param array $data data.
15288 * @return array
15289 */
15290 public function search_ad_last_data( $data ) {
15291 $context = [];
15292 $args = [
15293 'post_type' => 'advanced_ads',
15294 'posts_per_page' => 1,
15295 'orderby' => 'modified',
15296 'order' => 'DESC',
15297 ];
15298
15299 if ( isset( $data['filter']['ad_status']['value'] ) ) {
15300 $post_status = $data['filter']['ad_status']['value'];
15301 $args['post_status'] = $post_status;
15302 }
15303
15304 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
15305 $post_status = $data['filter']['ad_new_status']['value'];
15306 $args['post_status'] = $post_status;
15307 }
15308
15309 if ( isset( $data['filter']['ad_id']['value'] ) ) {
15310 $post_id = $data['filter']['ad_id']['value'];
15311 if ( -1 != $post_id ) {
15312 if ( $post_id > 0 ) {
15313 $args['p'] = $post_id;
15314 }
15315 }
15316 }
15317
15318 $posts = get_posts( $args );
15319 if ( ! empty( $posts ) ) {
15320 $context['pluggable_data'] = $posts[0];
15321 $context['pluggable_data'] = WordPress::get_post_context( $posts[0]->ID );
15322 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
15323 $context['pluggable_data']['ad_new_status'] = $posts[0]->post_status;
15324 }
15325 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
15326 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
15327 }
15328 $context['pluggable_data']['ad_id'] = $posts[0]->ID;
15329 if ( isset( $data['filter']['ad_status']['value'] ) ) {
15330 $context['pluggable_data']['ad_status'] = $posts[0]->post_status;
15331 }
15332 $context['response_type'] = 'live';
15333 } else {
15334 $context['pluggable_data'] = [
15335 'ID' => 1,
15336 'post' => 1,
15337 'post_author' => 1,
15338 'post_date' => '2022-11-18 12:18:14',
15339 'post_date_gmt' => '2022-11-18 12:18:14',
15340 'post_content' => 'Ad Post Content',
15341 'post_title' => 'Ad Post',
15342 'post_excerpt' => '',
15343 'post_status' => 'draft',
15344 'comment_status' => 'open',
15345 'ping_status' => 'open',
15346 'post_password' => '',
15347 'post_name' => 'ad-post',
15348 'to_ping' => '',
15349 'pinged' => '',
15350 'post_modified' => '2022-11-18 12:18:14',
15351 'post_modified_gmt' => '2022-11-18 12:18:14',
15352 'post_content_filtered' => '',
15353 'post_parent' => 0,
15354 'guid' => 'https://example.com/ad-post/',
15355 'menu_order' => 0,
15356 'post_type' => 'advanced_ads',
15357 'post_mime_type' => '',
15358 'comment_count' => 0,
15359 'filter' => 'raw',
15360 ];
15361 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
15362 $context['pluggable_data']['ad_new_status'] = $data['filter']['ad_new_status']['value'];
15363 }
15364 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
15365 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
15366 }
15367 $context['pluggable_data']['ad_id'] = 1;
15368 if ( isset( $data['filter']['ad_status']['value'] ) ) {
15369 $context['pluggable_data']['ad_status'] = $data['filter']['ad_status']['value'];
15370 }
15371 $context['response_type'] = 'sample';
15372 }
15373
15374 return $context;
15375 }
15376
15377 /**
15378 * Get Newsletter lists
15379 *
15380 * @param array $data data.
15381 *
15382 * @return array
15383 */
15384 public function search_newsletter_lists( $data ) {
15385
15386 $options = [];
15387 if ( class_exists( '\Newsletter' ) ) {
15388
15389 $lists = \Newsletter::instance()->get_lists();
15390
15391 if ( ! empty( $lists ) ) {
15392 foreach ( $lists as $list ) {
15393 $options[] = [
15394 'label' => $list->name,
15395 'value' => 'list_' . $list->id,
15396 ];
15397 }
15398 }
15399 }
15400
15401 return [
15402 'options' => $options,
15403 'hasMore' => false,
15404 ];
15405 }
15406
15407 /**
15408 * Newsletter pluggable data.
15409 *
15410 * @param array $data data.
15411 * @return array
15412 */
15413 public function search_newsletter_last_data( $data ) {
15414 $context = [];
15415 global $wpdb;
15416
15417 $list = $data['filter']['list_id']['value'];
15418
15419 if ( -1 == $list ) {
15420 $log = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . "newsletter where status='C' ORDER BY id DESC LIMIT 1" );
15421 } else {
15422 $num = $list;
15423 $location = 1;
15424 $sql = 'SELECT * FROM ' . $wpdb->prefix . "newsletter WHERE $num = %d AND status = 'C' ORDER BY id DESC LIMIT 1";
15425 $log = $wpdb->get_results( $wpdb->prepare( $sql, $location ), ARRAY_A );// @phpcs:ignore
15426 }
15427
15428 if ( ! empty( $log ) ) {
15429 $lists_arr = get_option( 'newsletter_lists' );
15430 if ( -1 == $list ) {
15431 foreach ( $log[0] as $key => $val ) {
15432 if ( defined( 'NEWSLETTER_LIST_MAX' ) ) {
15433 for ( $i = 1; $i <= NEWSLETTER_LIST_MAX; $i++ ) {
15434 $list_key = "list_$i";
15435
15436 if ( $key == $list_key ) {
15437 if ( 1 == $val ) {
15438 $context['pluggable_data']['list_id'] = $key;
15439 if ( is_array( $lists_arr ) ) {
15440 if ( isset( $lists_arr[ $key ] ) ) {
15441 $list_name = $lists_arr[ $key ];
15442 $context['pluggable_data']['list_name'] = $list_name;
15443 }
15444 }
15445 continue;
15446 }
15447 }
15448 }
15449 }
15450 if ( 'email' == $key ) {
15451 $context['pluggable_data']['email'] = $val;
15452 }
15453 }
15454 } else {
15455 $context['pluggable_data']['list_id'] = $list;
15456 $context['pluggable_data']['email'] = $log[0]['email'];
15457 if ( is_array( $lists_arr ) ) {
15458 if ( isset( $lists_arr[ $list ] ) ) {
15459 $list_name = $lists_arr[ $list ];
15460 $context['pluggable_data']['list_name'] = $list_name;
15461 }
15462 }
15463 }
15464 $context['response_type'] = 'live';
15465 } else {
15466 $context = json_decode( '{"response_type":"sample","pluggable_data":{"list_id": "list_1","email": "johnd@mailinator.com","list_name": "Contact List"}}', true );
15467 }
15468
15469 return $context;
15470 }
15471
15472 /**
15473 * Get wpForo Forum list
15474 *
15475 * @param array $data data.
15476 *
15477 * @return array
15478 */
15479 public function search_wp_forum_list( $data ) {
15480
15481 if ( ! function_exists( 'WPF' ) ) {
15482 return [];
15483 }
15484
15485 $forums = WPF()->forum->get_forums( [ 'type' => 'forum' ] );
15486
15487 $options = [];
15488
15489 if ( ! empty( $forums ) ) {
15490 foreach ( $forums as $forum ) {
15491 $options[] = [
15492 'label' => $forum['title'],
15493 'value' => $forum['forumid'],
15494 ];
15495 }
15496 }
15497
15498 return [
15499 'options' => $options,
15500 'hasMore' => false,
15501 ];
15502 }
15503
15504 /**
15505 * Get wpForo Topic list
15506 *
15507 * @param array $data data.
15508 *
15509 * @return array
15510 */
15511 public function search_wp_topic_list( $data ) {
15512
15513 if ( ! function_exists( 'WPF' ) ) {
15514 return [];
15515 }
15516
15517 $forum_id = $data['dynamic'];
15518
15519 $topics = WPF()->topic->get_topics( [ 'forumid' => $forum_id ] );
15520
15521 $options = [];
15522
15523 if ( ! empty( $topics ) ) {
15524 foreach ( $topics as $topic ) {
15525 $options[] = [
15526 'label' => $topic['title'],
15527 'value' => $topic['topicid'],
15528 ];
15529 }
15530 }
15531
15532 return [
15533 'options' => $options,
15534 'hasMore' => false,
15535 ];
15536 }
15537
15538 /**
15539 * Get wpForo Groups list
15540 *
15541 * @param array $data data.
15542 *
15543 * @return array
15544 */
15545 public function search_wp_user_groups_list( $data ) {
15546
15547 if ( ! function_exists( 'WPF' ) ) {
15548 return [];
15549 }
15550
15551 $usergroups = WPF()->usergroup->get_usergroups();
15552
15553 $options = [];
15554
15555 if ( ! empty( $usergroups ) ) {
15556 foreach ( $usergroups as $group ) {
15557 $options[] = [
15558 'label' => $group['name'],
15559 'value' => intval( $group['groupid'] ),
15560 ];
15561 }
15562 }
15563
15564 return [
15565 'options' => $options,
15566 'hasMore' => false,
15567 ];
15568 }
15569
15570 /**
15571 * Get wpForo Reputation list
15572 *
15573 * @param array $data data.
15574 *
15575 * @return array
15576 */
15577 public function search_wp_foro_reputation_list( $data ) {
15578
15579 if ( ! function_exists( 'WPF' ) ) {
15580 return [];
15581 }
15582
15583 $levels = WPF()->member->levels();
15584 $options = [];
15585
15586 if ( ! empty( $levels ) ) {
15587 foreach ( $levels as $level ) {
15588 $options[] = [
15589 'label' => esc_attr__( 'Level', 'suretriggers' ) . ' ' . $level . ' - ' . WPF()->member->rating( $level, 'title' ),
15590 'value' => strval( $level ),
15591 ];
15592 }
15593 }
15594
15595 return [
15596 'options' => $options,
15597 'hasMore' => false,
15598 ];
15599 }
15600
15601 /**
15602 * WPForo new topic pluggable data.
15603 *
15604 * @param array $data data.
15605 * @return array
15606 */
15607 public function search_pluggables_wpforo_topic_last_data( $data ) {
15608 $context = [];
15609 global $wpdb;
15610
15611 $forum_id = $data['filter']['forum_id']['value'];
15612
15613 if ( -1 == $forum_id ) {
15614 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'wpforo_topics WHERE closed = 0 ORDER BY topicid DESC LIMIT 1', ARRAY_A );
15615 } else {
15616 $forum = $forum_id;
15617 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_topics WHERE forumid = %d AND closed = 0 ORDER BY topicid DESC LIMIT 1';
15618 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum ), ARRAY_A );// @phpcs:ignore
15619 }
15620
15621 if ( ! empty( $results ) ) {
15622 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
15623 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
15624
15625 if ( function_exists( 'WPF' ) ) {
15626 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
15627 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
15628 }
15629 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
15630 $context['response_type'] = 'live';
15631 } else {
15632 $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
15633 }
15634
15635 return $context;
15636 }
15637
15638 /**
15639 * WpForo Topic Reply pluggable data.
15640 *
15641 * @param array $data data.
15642 * @return array
15643 */
15644 public function search_pluggables_wpforo_topic_reply_last_data( $data ) {
15645 $context = [];
15646 global $wpdb;
15647
15648 $forum_id = $data['filter']['forum_id']['value'];
15649 $topic_id = $data['filter']['topic_id']['value'];
15650
15651 if ( -1 == $forum_id && -1 != $topic_id ) {
15652 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE topicid = %d ORDER BY postid DESC LIMIT 1';
15653 $results = $wpdb->get_results( $wpdb->prepare( $sql, $topic_id ), ARRAY_A );// @phpcs:ignore
15654 } elseif ( -1 != $forum_id && -1 == $topic_id ) {
15655 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d ORDER BY postid DESC LIMIT 1';
15656 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id ), ARRAY_A );// @phpcs:ignore
15657 } elseif ( -1 == $forum_id && -1 == $topic_id ) {
15658 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts ORDER BY postid DESC LIMIT 1';
15659 $results = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );// @phpcs:ignore
15660 } else {
15661 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d AND topicid = %d ORDER BY postid DESC LIMIT 1';
15662 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id, $topic_id ), ARRAY_A );// @phpcs:ignore
15663 }
15664
15665 if ( ! empty( $results ) ) {
15666 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
15667 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
15668 if ( function_exists( 'WPF' ) ) {
15669 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
15670 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
15671 $context['pluggable_data']['reply'] = WPF()->post->get_post( $results[0]['postid'] );
15672 }
15673 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
15674 $context['response_type'] = 'live';
15675 } else {
15676 $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
15677 }
15678
15679 return $context;
15680 }
15681
15682 /**
15683 * Get RafflePress Giveaways list
15684 *
15685 * @param array $data data.
15686 *
15687 * @return array
15688 */
15689 public function search_rp_giveaways_list( $data ) {
15690
15691 global $wpdb;
15692 $options = [];
15693
15694 $giveaways = $wpdb->get_results( "SELECT id,name FROM {$wpdb->prefix}rafflepress_giveaways WHERE deleted_at is null ORDER BY name ASC", ARRAY_A );
15695 foreach ( $giveaways as $giveaway ) {
15696 $options[] = [
15697 'label' => $giveaway['name'],
15698 'value' => $giveaway['id'],
15699 ];
15700 }
15701
15702 return [
15703 'options' => $options,
15704 'hasMore' => false,
15705 ];
15706 }
15707
15708 /**
15709 * Get RafflePress Giveaway Actions list
15710 *
15711 * @param array $data data.
15712 *
15713 * @return array
15714 */
15715 public function search_rp_giveaway_actions_list( $data ) {
15716
15717 global $wpdb;
15718 $options = [];
15719
15720 $giveaway = $wpdb->get_row( $wpdb->prepare( "SELECT settings FROM {$wpdb->prefix}rafflepress_giveaways WHERE id=%d", $data['dynamic'] ), ARRAY_A );
15721
15722 if ( is_array( $giveaway ) && isset( $giveaway['settings'] ) ) {
15723 $settings = json_decode( $giveaway['settings'], true );
15724
15725 if ( is_array( $settings ) && isset( $settings['entry_options'] ) ) {
15726 foreach ( $settings['entry_options'] as $action ) {
15727 $options[] = [
15728 'label' => $action['name'],
15729 'value' => $action['id'],
15730 ];
15731 }
15732 }
15733 }
15734
15735 return [
15736 'options' => $options,
15737 'hasMore' => false,
15738 ];
15739 }
15740
15741 /**
15742 * Get last data for trigger.
15743 *
15744 * @param array $data data.
15745 * @return array
15746 */
15747 public function search_raffle_press_triggers_last_data( $data ) {
15748
15749 global $wpdb;
15750
15751 $context = [];
15752 $context['response_type'] = 'sample';
15753
15754 $pluggable_data = [
15755 'giveaway_id' => 1,
15756 'giveaway_title' => 'Sample Giveaway (ID#1)',
15757 'giveaway_start_date' => 'September 20, 2023',
15758 'giveaway_end_date' => 'October 6, 2023',
15759 'giveaway_entries' => 9,
15760 'giveaway_user_count' => 3,
15761 'giveaway_status' => 'Active',
15762 'contestant_name' => 'John Doe',
15763 'contestant_email' => 'john_doe@gmail.com',
15764 'contestant_email_verified' => 'Yes',
15765 'action_id' => '0jnex',
15766 'action_name' => 'Visit us on Instagram',
15767 ];
15768
15769 $giveaway_id = isset( $data['filter']['giveaway_id'] ) ? $data['filter']['giveaway_id']['value'] : null;
15770 $action_id = isset( $data['filter']['action_id'] ) ? $data['filter']['action_id']['value'] : null;
15771
15772 $query = "SELECT contestant_id, giveaway_id, action_id, meta FROM {$wpdb->prefix}rafflepress_entries";
15773
15774 if ( $giveaway_id && -1 != $giveaway_id ) {
15775 $query .= ' WHERE giveaway_id = ' . $giveaway_id;
15776
15777 if ( $action_id ) {
15778 $query .= " AND action_id = '" . $action_id . "'";
15779 }
15780 }
15781
15782 $query .= ' ORDER BY created_at DESC LIMIT 1';
15783
15784 $giveaway_data = $wpdb->get_row( $query, ARRAY_A ); // @phpcs:ignore
15785
15786 if ( ! empty( $giveaway_data ) ) {
15787 $pluggable_data = array_merge(
15788 RafflePress::get_giveaway_context( $giveaway_data['giveaway_id'] ),
15789 RafflePress::get_contestant_context( $giveaway_data['contestant_id'] )
15790 );
15791
15792 $pluggable_data['performed_action_id'] = isset( $giveaway_data['action_id'] ) ? $giveaway_data['action_id'] : 0;
15793
15794 $giveaway_meta = isset( $giveaway_data['meta'] ) ? json_decode( $giveaway_data['meta'], true ) : [];
15795 $pluggable_data['performed_action_name'] = is_array( $giveaway_meta ) && isset( $giveaway_meta['action'] ) ? $giveaway_meta['action'] : '';
15796
15797 $context['response_type'] = 'live';
15798 }
15799
15800 $context['pluggable_data'] = $pluggable_data;
15801
15802 return $context;
15803 }
15804
15805 /**
15806 * Get last data for trigger
15807 *
15808 * @param array $data data.
15809 * @return array
15810 */
15811 public function search_woo_commerce_shipstation_triggers_last_data( $data ) {
15812 $context = [];
15813 $context['response_type'] = 'sample';
15814 $context['pluggable_data'] = [];
15815
15816 $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
15817
15818 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
15819 $condition = $data['filter']['condition_compare']['value'];
15820 $price = $data['filter']['price']['value'];
15821 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
15822
15823 if ( 'order_shipped' === $term ) {
15824 $orders = wc_get_orders(
15825 [
15826 'numberposts' => 1,
15827 'orderby' => 'date',
15828 'order' => 'DESC',
15829 'status' => 'completed',
15830 'meta_query' => [
15831 [
15832 'key' => '_shipstation_shipped_item_count',
15833 'compare' => 'EXISTS',
15834 ],
15835 ],
15836 ]
15837 );
15838 if ( is_array( $orders ) && count( $orders ) > 0 ) {
15839 $order_id = $orders[0]->get_id();
15840 $order = wc_get_order( $order_id );
15841 $product_ids = [];
15842 if ( $order && $order instanceof \WC_Order ) {
15843 $user_id = $order->get_customer_id();
15844 $items = $order->get_items();
15845 foreach ( $items as $item ) {
15846 if ( method_exists( $item, 'get_product_id' ) ) {
15847 $product_ids[] = $item->get_product_id();
15848 }
15849 }
15850
15851 foreach ( $product_ids as $product_id ) {
15852 $context['product_id'] = $product_id;
15853 }
15854 $order_context = WooCommerce::get_order_context( $order_id );
15855 $context = array_merge(
15856 isset( $order_context ) ? $order_context : [],
15857 isset( $user_id ) ? WordPress::get_user_context( $user_id ) : []
15858 );
15859 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
15860 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
15861 /**
15862 *
15863 * Ignore line
15864 *
15865 * @phpstan-ignore-next-line
15866 */
15867 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
15868 /**
15869 *
15870 * Ignore line
15871 *
15872 * @phpstan-ignore-next-line
15873 */
15874 $date = date_i18n( get_option( 'date_format' ), $timestamp );
15875 $context['ship_date'] = $date;
15876 $context['response_type'] = 'live';
15877 }
15878 }
15879
15880 $context['pluggable_data'] = $context;
15881 } elseif ( 'specific_product_order_shipped' === $term ) {
15882 if ( -1 != $product_id ) {
15883 $product_ids = [ $product_id ];
15884 $orders = wc_get_orders(
15885 [
15886 'numberposts' => 1,
15887 'orderby' => 'date',
15888 'order' => 'DESC',
15889 'status' => 'completed',
15890 'meta_query' => [
15891 [
15892 'key' => '_shipstation_shipped_item_count',
15893 'compare' => 'EXISTS',
15894 ],
15895 [
15896 'key' => '_product_id',
15897 'value' => $product_ids,
15898 'compare' => 'IN',
15899 ],
15900 ],
15901 ]
15902 );
15903 } else {
15904 $orders = wc_get_orders(
15905 [
15906 'numberposts' => 1,
15907 'orderby' => 'date',
15908 'order' => 'DESC',
15909 'status' => 'completed',
15910 'meta_query' => [
15911 [
15912 'key' => '_shipstation_shipped_item_count',
15913 'compare' => 'EXISTS',
15914 ],
15915 ],
15916 ]
15917 );
15918 }
15919 if ( is_array( $orders ) && count( $orders ) > 0 ) {
15920 $order_id = $orders[0]->get_id();
15921 $order = wc_get_order( $order_id );
15922 $productids = [];
15923 if ( $order instanceof \WC_Order ) {
15924 $user_id = $order->get_customer_id();
15925 $items = $order->get_items();
15926 foreach ( $items as $item ) {
15927 if ( method_exists( $item, 'get_product_id' ) ) {
15928 $productids[] = $item->get_product_id();
15929 }
15930 }
15931 $order_context = WooCommerce::get_order_context( $order_id );
15932 $context = array_merge(
15933 isset( $order_context ) ? $order_context : [],
15934 WordPress::get_user_context( $user_id )
15935 );
15936 foreach ( $productids as $product_id ) {
15937 $context['product_id'] = $product_id;
15938 }
15939
15940 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
15941 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
15942 /**
15943 *
15944 * Ignore line
15945 *
15946 * @phpstan-ignore-next-line
15947 */
15948 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
15949 /**
15950 *
15951 * Ignore line
15952 *
15953 * @phpstan-ignore-next-line
15954 */
15955 $date = date_i18n( get_option( 'date_format' ), $timestamp );
15956 $context['ship_date'] = $date;
15957 $context['response_type'] = 'live';
15958 }
15959 }
15960
15961 $context['pluggable_data'] = $context;
15962 } elseif ( 'specific_amount_order_shipped' === $term ) {
15963 $orders = wc_get_orders(
15964 [
15965 'numberposts' => 1,
15966 'orderby' => 'date',
15967 'order' => 'DESC',
15968 'status' => 'completed',
15969 'meta_query' => [
15970 [
15971 'key' => '_shipstation_shipped_item_count',
15972 'compare' => 'EXISTS',
15973 ],
15974 [
15975 'key' => '_order_total',
15976 'value' => $price,
15977 'compare' => $condition,
15978 ],
15979 ],
15980 ]
15981 );
15982 if ( is_array( $orders ) && count( $orders ) > 0 ) {
15983 $order_id = $orders[0]->get_id();
15984 $order = wc_get_order( $order_id );
15985 $product_ids = [];
15986 if ( $order instanceof \WC_Order ) {
15987 $user_id = $order->get_customer_id();
15988 $items = $order->get_items();
15989 foreach ( $items as $item ) {
15990 if ( method_exists( $item, 'get_product_id' ) ) {
15991 $product_ids[] = $item->get_product_id();
15992 }
15993 }
15994 $order_context = WooCommerce::get_order_context( $order_id );
15995 $context = array_merge(
15996 isset( $order_context ) ? $order_context : [],
15997 isset( $user_id ) ? WordPress::get_user_context( $user_id ) : []
15998 );
15999
16000 foreach ( $product_ids as $product_id ) {
16001 $context['product_id'] = $product_id;
16002 }
16003
16004 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
16005 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
16006 /**
16007 *
16008 * Ignore line
16009 *
16010 * @phpstan-ignore-next-line
16011 */
16012 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
16013 /**
16014 *
16015 * Ignore line
16016 *
16017 * @phpstan-ignore-next-line
16018 */
16019 $date = date_i18n( get_option( 'date_format' ), $timestamp );
16020 $context['ship_date'] = $date;
16021 $context['response_type'] = 'live';
16022 }
16023 }
16024
16025 $context['pluggable_data'] = $context;
16026 }
16027
16028 return $context;
16029 }
16030
16031 /**
16032 * Get GroundHogg Tag list
16033 *
16034 * @param array $data data.
16035 *
16036 * @return array
16037 */
16038 public function search_groundhogg_tag_list( $data ) {
16039
16040 if ( ! function_exists( 'Groundhogg\get_db' ) ) {
16041 return [];
16042 }
16043
16044 $tags = \Groundhogg\get_db( 'tags' )->query( [] );
16045 $options = [];
16046
16047 if ( ! empty( $tags ) ) {
16048 foreach ( $tags as $tag ) {
16049 $options[] = [
16050 'label' => $tag->tag_name,
16051 'value' => $tag->tag_id,
16052 ];
16053 }
16054 }
16055
16056 return [
16057 'options' => $options,
16058 'hasMore' => false,
16059 ];
16060 }
16061
16062 /**
16063 * Get WP courseware courses list
16064 *
16065 * @param array $data data.
16066 *
16067 * @return array
16068 */
16069 public function search_wpcw_courses( $data ) {
16070 $options = [];
16071
16072 $page = $data['page'];
16073 $limit = Utilities::get_search_page_limit();
16074 $offset = $limit * ( $page - 1 );
16075
16076 $args = [
16077 'post_type' => 'wpcw_course',
16078 'posts_per_page' => $limit,
16079 'offset' => $offset,
16080 'orderby' => 'title',
16081 'order' => 'ASC',
16082 'post_status' => 'publish',
16083 ];
16084
16085 $courses = get_posts( $args );
16086
16087 $course_count = wp_count_posts( 'wpcw_course' )->publish;
16088
16089 if ( ! empty( $courses ) ) {
16090 if ( is_array( $courses ) ) {
16091 foreach ( $courses as $course ) {
16092 $options[] = [
16093 'label' => $course->post_title,
16094 'value' => $course->ID,
16095 ];
16096 }
16097 }
16098 }
16099 return [
16100 'options' => $options,
16101 'hasMore' => $course_count > $limit && $course_count > $offset,
16102 ];
16103 }
16104
16105 /**
16106 * Get WP courseware courses list
16107 *
16108 * @param array $data data.
16109 *
16110 * @return array
16111 */
16112 public function search_wpcw_modules( $data ) {
16113 $options = [];
16114
16115 if ( function_exists( 'wpcw_get_modules' ) ) {
16116 $modules = wpcw_get_modules();
16117 }
16118
16119 if ( ! empty( $modules ) ) {
16120 if ( is_array( $modules ) ) {
16121 foreach ( $modules as $module ) {
16122 $options[] = [
16123 'label' => $module->module_title,
16124 'value' => $module->module_id,
16125 ];
16126 }
16127 }
16128 }
16129 return [
16130 'options' => $options,
16131 'hasMore' => false,
16132 ];
16133 }
16134
16135 /**
16136 * Get WP courseware unit list
16137 *
16138 * @param array $data data.
16139 *
16140 * @return array
16141 */
16142 public function search_wpcw_units( $data ) {
16143 $options = [];
16144
16145 $page = $data['page'];
16146 $limit = Utilities::get_search_page_limit();
16147 $offset = $limit * ( $page - 1 );
16148
16149 $args = [
16150 'post_type' => 'course_unit',
16151 'posts_per_page' => $limit,
16152 'offset' => $offset,
16153 'orderby' => 'title',
16154 'order' => 'ASC',
16155 'post_status' => 'publish',
16156 ];
16157
16158 $units = get_posts( $args );
16159
16160 $unit_count = wp_count_posts( 'course_unit' )->publish;
16161
16162 if ( ! empty( $units ) ) {
16163 if ( is_array( $units ) ) {
16164 foreach ( $units as $unit ) {
16165 $options[] = [
16166 'label' => $unit->post_title,
16167 'value' => $unit->ID,
16168 ];
16169 }
16170 }
16171 }
16172 return [
16173 'options' => $options,
16174 'hasMore' => $unit_count > $limit && $unit_count > $offset,
16175 ];
16176 }
16177
16178 /**
16179 * Search WP Courseware data.
16180 *
16181 * @param array $data data.
16182 * @return array|void
16183 */
16184 public function search_wpcw_last_data( $data ) {
16185 global $wpdb;
16186 $post_type = $data['post_type'];
16187 $trigger = $data['search_term'];
16188 $context = [];
16189
16190 if ( 'wpcw_course_completed' === $trigger ) {
16191 $post_id = $data['filter']['course_post_id']['value'];
16192 if ( -1 === $post_id ) {
16193 $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" );
16194 } else {
16195 $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 ) );
16196 }
16197 } elseif ( 'wpcw_module_completed' === $trigger ) {
16198 $post_id = $data['filter']['module_id']['value'];
16199 if ( -1 === $post_id ) {
16200 $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" );
16201 } else {
16202 $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 ) );
16203 }
16204 } elseif ( 'wpcw_unit_completed' === $trigger ) {
16205 $post_id = $data['filter']['unit_id']['value'];
16206 if ( -1 === $post_id ) {
16207 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpcw_user_progress WHERE unit_completed_status='complete' order by unit_id DESC LIMIT 1" );
16208 } else {
16209 $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 ) );
16210 }
16211 } elseif ( 'wpcw_enroll_course' === $trigger ) {
16212 $post_id = $data['filter']['course_post_id']['value'];
16213 if ( -1 === $post_id ) {
16214 $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" );
16215 } else {
16216 $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 ) );
16217 }
16218 }
16219
16220 if ( ! empty( $result ) ) {
16221
16222 switch ( $trigger ) {
16223 case 'wpcw_course_completed':
16224 $result_course_id = $result[0]->course_id;
16225 $result_user_id = $result[0]->user_id;
16226 if ( function_exists( 'wpcw_get_course' ) ) {
16227 $course = wpcw_get_course( $result_course_id );
16228 if ( is_object( $course ) ) {
16229 $course = get_object_vars( $course );
16230 }
16231 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course );
16232 }
16233 break;
16234 case 'wpcw_module_completed':
16235 $result_module_id = $result[0]->module_id;
16236 $result_user_id = $result[0]->user_id;
16237 if ( function_exists( 'wpcw_get_module' ) ) {
16238 $module = wpcw_get_module( $result_module_id );
16239 if ( is_object( $module ) ) {
16240 $module = get_object_vars( $module );
16241 }
16242 $context = array_merge( WordPress::get_user_context( $result_user_id ), $module );
16243 }
16244 break;
16245 case 'wpcw_unit_completed':
16246 $result_unit_id = $result[0]->unit_id;
16247 $result_user_id = $result[0]->user_id;
16248 if ( function_exists( 'wpcw_get_unit' ) ) {
16249 $unit = wpcw_get_unit( $result_unit_id );
16250 if ( is_object( $unit ) ) {
16251 $unit = get_object_vars( $unit );
16252 $unit['name'] = get_the_title( $result_unit_id );
16253 }
16254 $context = array_merge( WordPress::get_user_context( $result_user_id ), $unit );
16255 }
16256 break;
16257 case 'wpcw_enroll_course':
16258 $result_course_id = $result[0]->course_id;
16259 $result_user_id = $result[0]->user_id;
16260 if ( function_exists( 'WPCW_courses_getCourseDetails' ) ) {
16261 $course_detail = WPCW_courses_getCourseDetails( $result_course_id );
16262 if ( is_object( $course_detail ) ) {
16263 $course_detail = get_object_vars( $course_detail );
16264 }
16265 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course_detail );
16266 }
16267 break;
16268 default:
16269 return;
16270 }
16271 $context['pluggable_data'] = $context;
16272 $context['response_type'] = 'live';
16273 }
16274
16275 return $context;
16276
16277 }
16278
16279 /**
16280 * Get WooCommerce Order Note list.
16281 *
16282 * @param array $data data.
16283 *
16284 * @return array
16285 */
16286 public function search_note_type_list( $data ) {
16287
16288 $options = [];
16289
16290 $options[] = [
16291 'label' => 'Customer',
16292 'value' => 'customer',
16293 ];
16294 $options[] = [
16295 'label' => 'Private',
16296 'value' => 'internal',
16297 ];
16298
16299 return [
16300 'options' => $options,
16301 'hasMore' => false,
16302 ];
16303 }
16304
16305 /**
16306 * Get last data for trigger
16307 *
16308 * @param array $data data.
16309 * @return array
16310 */
16311 public function search_woo_commerce_customers_triggers_last_data( $data ) {
16312 $context = [];
16313 $context['response_type'] = 'sample';
16314 $context['pluggable_data'] = [];
16315 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
16316 global $wpdb;
16317 if ( 'customer_created' === $term ) {
16318 $customer_query = get_users(
16319 [
16320 'fields' => 'ID',
16321 'role' => 'customer',
16322 'orderby' => 'ID',
16323 'order' => 'DESC',
16324 'number' => 1,
16325 ]
16326 );
16327 $results = $customer_query;
16328 if ( ! empty( $results ) ) {
16329 $customer = new WC_Customer( $results[0] );
16330 $last_order = $customer->get_last_order();
16331 $customer_data = [
16332 'id' => $customer->get_id(),
16333 'email' => $customer->get_email(),
16334 'first_name' => $customer->get_first_name(),
16335 'last_name' => $customer->get_last_name(),
16336 'username' => $customer->get_username(),
16337 'last_order_id' => is_object( $last_order ) ? $last_order->get_id() : null,
16338 'orders_count' => $customer->get_order_count(),
16339 'total_spent' => wc_format_decimal( $customer->get_total_spent(), 2 ),
16340 'avatar_url' => $customer->get_avatar_url(),
16341 'billing_address' => [
16342 'first_name' => $customer->get_billing_first_name(),
16343 'last_name' => $customer->get_billing_last_name(),
16344 'company' => $customer->get_billing_company(),
16345 'address_1' => $customer->get_billing_address_1(),
16346 'address_2' => $customer->get_billing_address_2(),
16347 'city' => $customer->get_billing_city(),
16348 'state' => $customer->get_billing_state(),
16349 'postcode' => $customer->get_billing_postcode(),
16350 'country' => $customer->get_billing_country(),
16351 'email' => $customer->get_billing_email(),
16352 'phone' => $customer->get_billing_phone(),
16353 ],
16354 'shipping_address' => [
16355 'first_name' => $customer->get_shipping_first_name(),
16356 'last_name' => $customer->get_shipping_last_name(),
16357 'company' => $customer->get_shipping_company(),
16358 'address_1' => $customer->get_shipping_address_1(),
16359 'address_2' => $customer->get_shipping_address_2(),
16360 'city' => $customer->get_shipping_city(),
16361 'state' => $customer->get_shipping_state(),
16362 'postcode' => $customer->get_shipping_postcode(),
16363 'country' => $customer->get_shipping_country(),
16364 ],
16365 ];
16366 if ( is_object( $last_order ) && method_exists( $last_order, 'get_date_created' ) ) {
16367 $created_date = $last_order->get_date_created();
16368 if ( is_object( $created_date ) && method_exists( $created_date, 'getTimestamp' ) ) {
16369 $last_order_date = $created_date->getTimestamp();
16370 $customer_data['created_at'] = $last_order_date;
16371 $customer_data['last_order_date'] = $last_order_date;
16372 }
16373 }
16374 $order_sample_data = $customer_data;
16375 $context['response_type'] = 'live';
16376 $context['pluggable_data'] = $order_sample_data;
16377 } else {
16378 $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 );
16379 $context['pluggable_data'] = $order_sample_data;
16380 }
16381 } elseif ( 'total_spend_reach' === $term ) {
16382 $total_spend_selected = isset( $data['filter']['total_spend']['value'] ) ? $data['filter']['total_spend']['value'] : '';
16383 $customers = $wpdb->get_col(
16384 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
16385 WHERE meta_key = '_customer_user' AND meta_value > 0"
16386 );
16387 $target_customer = null;
16388 foreach ( $customers as $customer ) {
16389 $total_spend = (int) wc_get_customer_total_spent( $customer );
16390 if ( $total_spend == $total_spend_selected ) {
16391 $target_customer = $customer;
16392 break; // Exit the loop once a matching customer is found.
16393 }
16394 }
16395 if ( $target_customer ) {
16396 $new_customer = new WC_Customer( $target_customer );
16397 $new_customer_data = [
16398 'id' => $new_customer->get_id(),
16399 'email' => $new_customer->get_email(),
16400 'first_name' => $new_customer->get_first_name(),
16401 'last_name' => $new_customer->get_last_name(),
16402 'username' => $new_customer->get_username(),
16403 'order_count' => $new_customer->get_order_count(),
16404 'total_spend' => wc_format_decimal( $new_customer->get_total_spent(), 2 ),
16405 ];
16406 $context['response_type'] = 'live';
16407 $context['pluggable_data'] = $new_customer_data;
16408 } else {
16409 $sample_customer_data = [
16410 'id' => '101',
16411 'created_at' => '1680675247',
16412 'email' => 'john@d.com',
16413 'first_name' => 'John',
16414 'last_name' => 'D',
16415 'username' => 'johnd',
16416 'order_count' => '3',
16417 'total_spend' => '22.00',
16418 ];
16419 $context['pluggable_data'] = $sample_customer_data;
16420 }
16421 } elseif ( 'order_count_reach' === $term ) {
16422 $total_order_selected = isset( $data['filter']['order_count']['value'] ) ? $data['filter']['order_count']['value'] : '';
16423 $customers = $wpdb->get_col(
16424 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
16425 WHERE meta_key = '_customer_user' AND meta_value > 0"
16426 );
16427 $target_customer = null;
16428 foreach ( $customers as $customer ) {
16429 $args = [
16430 'customer_id' => $customer,
16431 'limit' => -1,
16432 'status' => [ 'wc-completed' ],
16433 ];
16434 $orders = wc_get_orders( $args );
16435 if ( ! empty( $orders ) ) {
16436 $total_order = (int) wc_get_customer_order_count( $customer );
16437 if ( $total_order == $total_order_selected ) {
16438 $target_customer = $customer;
16439 break; // Exit the loop once a matching customer is found.
16440 }
16441 }
16442 }
16443 if ( $target_customer ) {
16444 $new_customer = new WC_Customer( $target_customer );
16445 $new_customer_data = [
16446 'id' => $new_customer->get_id(),
16447 'email' => $new_customer->get_email(),
16448 'first_name' => $new_customer->get_first_name(),
16449 'last_name' => $new_customer->get_last_name(),
16450 'username' => $new_customer->get_username(),
16451 'order_count' => $new_customer->get_order_count(),
16452 ];
16453 $context['response_type'] = 'live';
16454 $context['pluggable_data'] = $new_customer_data;
16455 } else {
16456 $sample_customer_data = [
16457 'id' => '101',
16458 'created_at' => '1680675247',
16459 'email' => 'john@d.com',
16460 'first_name' => 'John',
16461 'last_name' => 'D',
16462 'username' => 'johnd',
16463 'orders_count' => '3',
16464 ];
16465 $context['pluggable_data'] = $sample_customer_data;
16466 }
16467 }
16468
16469 return $context;
16470 }
16471
16472 /**
16473 * Get Affiliate list
16474 *
16475 * @param array $data data.
16476 *
16477 * @return array
16478 */
16479 public function search_affiliate_list( $data ) {
16480 $options = [];
16481
16482 $args = [
16483 'meta_query' => [
16484 [
16485 'key' => 'wafp_is_affiliate',
16486 'value' => '1',
16487 'compare' => '=',
16488 ],
16489 ],
16490 ];
16491
16492 $affiliates = get_users( $args );
16493
16494 foreach ( $affiliates as $user ) {
16495 $options[] = [
16496 'label' => $user->display_name,
16497 'value' => $user->ID,
16498 ];
16499 }
16500 return [
16501 'options' => $options,
16502 'hasMore' => false,
16503 ];
16504 }
16505
16506 /**
16507 * Get Affiliate Source list
16508 *
16509 * @param array $data data.
16510 *
16511 * @return array
16512 */
16513 public function search_affiliate_transaction_source_list( $data ) {
16514 $options = [];
16515
16516 $sources = [
16517 'General',
16518 'MemberPress',
16519 'WooCommerce',
16520 'Easy Digital Downloads',
16521 'WPForms',
16522 'Formidable',
16523 'PayPal',
16524 ];
16525 foreach ( $sources as $source ) {
16526 $options[] = [
16527 'label' => $source,
16528 'value' => str_replace( ' ', '_', strtolower( $source ) ),
16529 ];
16530 }
16531
16532 return [
16533 'options' => $options,
16534 'hasMore' => false,
16535 ];
16536 }
16537
16538 /**
16539 * Search Easy Affiliate data.
16540 *
16541 * @param array $data data.
16542 * @return array|void
16543 */
16544 public function search_easy_affiliate_last_data( $data ) {
16545
16546 global $wpdb;
16547 $trigger = $data['search_term'];
16548 $context = [];
16549
16550 if ( 'sale_recorded' === $trigger ) {
16551 $affiliate_id = $data['filter']['affiliate_id']['value'];
16552 if ( -1 === $affiliate_id ) {
16553 $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" );
16554 } else {
16555 $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 ) );
16556 }
16557 } elseif ( 'sale_added' === $trigger ) {
16558 $orderby = 'signup_date';
16559 $order = 'DESC';
16560 $paged = 1;
16561 $search = '';
16562 $perpage = 1;
16563 /**
16564 *
16565 * Ignore line
16566 *
16567 * @phpstan-ignore-next-line
16568 */
16569 $result = \EasyAffiliate\Models\User::affiliate_list_table( $orderby, $order, $paged, $search, $perpage );
16570 if ( empty( $result['results'] ) ) {
16571 $result = [];
16572 }
16573 } elseif ( 'payout_made' === $trigger ) {
16574 $affiliate_id = $data['filter']['affiliate_id']['value'];
16575 if ( -1 === $affiliate_id ) {
16576 $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" );
16577 } else {
16578 $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 ) );
16579 }
16580 }
16581
16582 if ( ! empty( $result ) ) {
16583 switch ( $trigger ) {
16584 case 'sale_recorded':
16585 $result_affiliate_id = $result[0]->affiliate_id;
16586 /**
16587 *
16588 * Ignore line
16589 *
16590 * @phpstan-ignore-next-line
16591 */
16592 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
16593 $affiliate = get_object_vars( $affiliate->rec );
16594 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
16595 break;
16596 case 'sale_added':
16597 /**
16598 *
16599 * Ignore line
16600 *
16601 * @phpstan-ignore-next-line
16602 */
16603 $data = new \EasyAffiliate\Models\User( $result['results'][0]->ID );
16604 /**
16605 *
16606 * Ignore line
16607 *
16608 * @phpstan-ignore-next-line
16609 */
16610 $context = get_object_vars( $data->rec );
16611 break;
16612 case 'payout_made':
16613 $result_affiliate_id = $result[0]->affiliate_id;
16614 /**
16615 *
16616 * Ignore line
16617 *
16618 * @phpstan-ignore-next-line
16619 */
16620 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
16621 $affiliate = get_object_vars( $affiliate->rec );
16622 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
16623 break;
16624 default:
16625 return;
16626 }
16627 $context['pluggable_data'] = $context;
16628 $context['response_type'] = 'live';
16629 } elseif ( empty( $result ) ) {
16630 switch ( $trigger ) {
16631 case 'sale_added':
16632 $context = json_decode(
16633 '{"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": "",
16634 "city": "london","state": "","zip": "","country": "UK","tax_id_us": "","tax_id_int": "","is_affiliate": "1","is_blocked": "","blocked_message": "","referrer": "0","notes": "","unsubscribed": ""}',
16635 true
16636 );
16637 break;
16638 case 'sale_recorded':
16639 $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 );
16640 break;
16641 case 'payout_made':
16642 $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 );
16643 break;
16644 default:
16645 return;
16646 }
16647 $context['pluggable_data'] = $context;
16648 $context['response_type'] = 'sample';
16649 }
16650
16651 return $context;
16652
16653 }
16654
16655 /**
16656 * Get WooCommerce Subscriptions Coupon list
16657 *
16658 * @param array $data data.
16659 *
16660 * @return array
16661 */
16662 public function search_woo_subscription_coupon_list( $data ) {
16663 $options = [];
16664
16665 $coupon_codes = [];
16666
16667 $query = new \WP_Query(
16668 [
16669 'post_type' => 'shop_coupon',
16670 'posts_per_page' => -1,
16671 'no_found_rows' => true,
16672 'meta_query' => [
16673 [
16674 'key' => '_is_aw_coupon',
16675 'compare' => 'NOT EXISTS',
16676 ],
16677 ],
16678 ]
16679 );
16680
16681 foreach ( $query->posts as $coupon ) {
16682 /**
16683 *
16684 * Ignore line
16685 *
16686 * @phpstan-ignore-next-line
16687 */
16688 $code = wc_format_coupon_code( $coupon->post_title );
16689 $coupon_codes[ $code ] = $code;
16690 }
16691
16692 foreach ( $coupon_codes as $code ) {
16693
16694 $coupon = new \WC_Coupon( $code );
16695
16696 if ( ! in_array( $coupon->get_discount_type(), [ 'recurring_fee', 'recurring_percent' ], true ) ) {
16697 unset( $coupon_codes[ $code ] );
16698 }
16699 }
16700
16701 if ( ! empty( $coupon_codes ) ) {
16702 if ( is_array( $coupon_codes ) ) {
16703 foreach ( $coupon_codes as $coupon_code ) {
16704 $options[] = [
16705 'label' => $coupon_code,
16706 'value' => $coupon_code,
16707 ];
16708 }
16709 }
16710 }
16711 return [
16712 'options' => $options,
16713 'hasMore' => false,
16714 ];
16715 }
16716
16717 /**
16718 * Get BuddyBoss field.
16719 *
16720 * @param array $data data.
16721 *
16722 * @return array|void
16723 */
16724 public function search_bb_field_list( $data ) {
16725 global $wpdb;
16726 $options = [];
16727
16728 $xprofile_fields = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_xprofile_fields WHERE parent_id = 0 ORDER BY field_order ASC" );
16729 if ( ! empty( $xprofile_fields ) ) {
16730 foreach ( $xprofile_fields as $xprofile_field ) {
16731 $options[] = [
16732 'label' => $xprofile_field->name,
16733 'value' => $xprofile_field->id,
16734 ];
16735 }
16736 }
16737
16738 return [
16739 'options' => $options,
16740 'hasMore' => false,
16741 ];
16742 }
16743
16744 /**
16745 * Get Woocommerce Bookings Product List.
16746 *
16747 * @param array $data data.
16748 *
16749 * @return array|void
16750 */
16751 public function search_wb_bookable_product_list( $data ) {
16752 $options = [];
16753
16754 if ( ! class_exists( 'WC_Bookings_Admin' ) ) {
16755 return;
16756 }
16757
16758 $products = WC_Bookings_Admin::get_booking_products();
16759 if ( ! empty( $products ) ) {
16760 if ( is_array( $products ) ) {
16761 foreach ( $products as $product ) {
16762 $options[] = [
16763 'label' => $product->get_name(),
16764 'value' => $product->get_id(),
16765 ];
16766 $resources = $product->get_resources();
16767 if ( ! empty( $resources ) ) {
16768 foreach ( $resources as $resource ) {
16769 $options[] = [
16770 'label' => $resource->get_name(),
16771 'value' => $resource->get_id(),
16772 ];
16773 }
16774 }
16775 }
16776 }
16777 }
16778
16779 return [
16780 'options' => $options,
16781 'hasMore' => false,
16782 ];
16783 }
16784
16785 /**
16786 * Get last data for WooCommerce Booking trigger
16787 *
16788 * @param array $data data.
16789 * @return array
16790 */
16791 public function search_woo_commerce_booking_last_data( $data ) {
16792 $context = [];
16793 $context['response_type'] = 'sample';
16794 $context['pluggable_data'] = [];
16795 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
16796 global $wpdb;
16797 if ( 'booking_created' === $term ) {
16798 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' AND post_status='confirmed' order by ID DESC LIMIT 1" );
16799
16800 if ( ! empty( $results ) ) {
16801 if ( class_exists( 'WC_Booking' ) ) {
16802 $booking = new WC_Booking( $results[0]->ID );
16803 $person_counts = $booking->get_person_counts();
16804 $bookable_product_id = $booking->get_product_id();
16805 if ( method_exists( $booking, 'get_data' ) ) {
16806 $booking = $booking->get_data();
16807 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
16808 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
16809 if ( ! empty( $person_counts ) ) {
16810 $total_count = 0;
16811 foreach ( $person_counts as $key => $value ) {
16812 $total_count += $value;
16813 }
16814 $booking['total_person_counts'] = $total_count;
16815 }
16816 $booking['bookable_product'] = $bookable_product_id;
16817 $context['response_type'] = 'live';
16818 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
16819 }
16820 }
16821 } else {
16822 $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 );
16823 $context['pluggable_data'] = $booking_sample_data;
16824 }
16825 } elseif ( 'booking_status_changed' === $term ) {
16826 $to_status = isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1;
16827
16828 if ( -1 == $to_status ) {
16829 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' order by ID DESC LIMIT 1" );
16830 } else {
16831 $results = $wpdb->get_results(
16832 $wpdb->prepare(
16833 "
16834 SELECT *
16835 FROM {$wpdb->prefix}posts
16836 WHERE `post_status` = %s
16837 AND `post_type` LIKE %s
16838 ",
16839 $to_status,
16840 'wc_booking'
16841 )
16842 );
16843 }
16844
16845 if ( ! empty( $results ) ) {
16846 if ( class_exists( 'WC_Booking' ) ) {
16847 $booking = new WC_Booking( $results[0]->ID );
16848 $person_counts = $booking->get_person_counts();
16849 $bookable_product_id = $booking->get_product_id();
16850 if ( method_exists( $booking, 'get_data' ) ) {
16851 $booking = $booking->get_data();
16852 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
16853 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
16854 if ( ! empty( $person_counts ) ) {
16855 $total_count = 0;
16856 foreach ( $person_counts as $key => $value ) {
16857 $total_count += $value;
16858 }
16859 $booking['total_person_counts'] = $total_count;
16860 }
16861 $booking['bookable_product'] = $bookable_product_id;
16862 $booking['from_status'] = $data['filter']['from_status']['value'];
16863 $booking['to_status'] = $booking['status'];
16864 $context['response_type'] = 'live';
16865 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
16866 }
16867 }
16868 } else {
16869 $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 );
16870 $context['pluggable_data'] = $booking_sample_data;
16871 }
16872 }
16873 return $context;
16874 }
16875
16876 /**
16877 * Get WooCommerce Booking status list.
16878 *
16879 * @param array $data data.
16880 *
16881 * @return array
16882 */
16883 public function search_woo_booking_status_list( $data ) {
16884
16885 $options = [
16886 [
16887 'label' => 'Pending Confirmation',
16888 'value' => 'pending-confirmation',
16889 ],
16890 [
16891 'label' => 'Unpaid',
16892 'value' => 'unpaid',
16893 ],
16894 [
16895 'label' => 'Confirmed',
16896 'value' => 'confirmed',
16897 ],
16898 [
16899 'label' => 'Paid',
16900 'value' => 'paid',
16901 ],
16902 [
16903 'label' => 'Complete',
16904 'value' => 'complete',
16905 ],
16906 [
16907 'label' => 'In Cart',
16908 'value' => 'in-cart',
16909 ],
16910 [
16911 'label' => 'Cancelled',
16912 'value' => 'cancelled',
16913 ],
16914 ];
16915
16916 return [
16917 'options' => $options,
16918 'hasMore' => false,
16919 ];
16920 }
16921
16922 /**
16923 * Get Woocommerce Memberships Status List.
16924 *
16925 * @param array $data data.
16926 *
16927 * @return array|void
16928 */
16929 public function search_wc_membership_status_list( $data ) {
16930 $options = [];
16931 if ( ! function_exists( 'wc_memberships_get_user_membership_statuses' ) ) {
16932 return;
16933 }
16934
16935 $statuses = wc_memberships_get_user_membership_statuses();
16936 if ( ! empty( $statuses ) ) {
16937 if ( is_array( $statuses ) ) {
16938 foreach ( $statuses as $status => $value ) {
16939 $status = 0 === strpos( $status, 'wcm-' ) ? substr( $status, 4 ) : $status;
16940 $options[] = [
16941 'label' => $value['label'],
16942 'value' => $status,
16943 ];
16944 }
16945 }
16946 }
16947
16948 return [
16949 'options' => $options,
16950 'hasMore' => false,
16951 ];
16952 }
16953
16954 /**
16955 * Search Woocommerce Subscription data.
16956 *
16957 * @param array $data data.
16958 * @return array|void
16959 */
16960 public function search_user_subscription_last_data( $data ) {
16961
16962 $context = [];
16963 if (
16964 ! class_exists( 'WC_Subscription' ) ||
16965 ! function_exists( 'wcs_get_subscription' ) ||
16966 ! function_exists( 'wcs_get_subscriptions_for_product' ) ||
16967 ! function_exists( 'wcs_order_contains_renewal' ) ||
16968 ! function_exists( 'wcs_get_subscriptions_for_order' )
16969 ) {
16970 return;
16971 }
16972
16973 if ( 'trial_end' == $data['search_term'] ) {
16974 $subscription_id = $data['filter']['subscription']['value'];
16975 $query_args = [
16976 'post_type' => 'shop_subscription',
16977 'orderby' => 'ID',
16978 'order' => 'DESC',
16979 'post_status' => 'wc-active',
16980 'posts_per_page' => 1,
16981 'meta_query' => [
16982 [
16983 'key' => '_schedule_trial_end',
16984 'value' => gmdate( 'Y-m-d H:i:s' ),
16985 'compare' => '<=',
16986 ],
16987 ],
16988 'post__in' => [ $subscription_id ],
16989 ];
16990 $query_result = new WP_Query( $query_args );
16991 $subscription_orders = $query_result->get_posts();
16992
16993 if ( ! empty( $subscription_orders ) ) {
16994 /**
16995 *
16996 * Ignore line
16997 *
16998 * @phpstan-ignore-next-line
16999 */
17000 $subscription = wcs_get_subscription( $subscription_orders[0]->ID );
17001 $user_id = $subscription->get_user_id();
17002
17003 $items = $subscription->get_items();
17004 $product_ids = [];
17005 foreach ( $items as $item ) {
17006 $product_ids[] = $item->get_product_id();
17007 }
17008
17009 $subscription_status = $subscription->get_status();
17010 $subscription_start_date = $subscription->get_date_created();
17011
17012 $context['subscription_data'] = [
17013 'status' => $subscription_status,
17014 'start_date' => $subscription_start_date,
17015 'trial_end_date' => $subscription->get_date( 'schedule_trial_end' ),
17016 'next_payment_date' => $subscription->get_date( 'next_payment' ),
17017 'end_date' => $subscription->get_date( 'schedule_end' ),
17018 ];
17019 $context['user'] = WordPress::get_user_context( $user_id );
17020
17021 foreach ( $product_ids as $val ) {
17022 $context['subscription'] = $val;
17023 $context['subscription_name'] = get_the_title( $val );
17024 }
17025 $context['pluggable_data'] = $context;
17026 $context['response_type'] = 'live';
17027 } else {
17028 $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 );
17029 $context['pluggable_data'] = $context;
17030 $context['response_type'] = 'sample';
17031 }
17032 } elseif ( 'renewal_payment_failed' == $data['search_term'] ) {
17033 $subscription_item = $data['filter']['subscription_item']['value'];
17034 $subscription_ids = wcs_get_subscriptions_for_product( $subscription_item );
17035 $ids = [];
17036 $related = [];
17037 foreach ( $subscription_ids as $subscription ) {
17038 $ids[] = $subscription;
17039 $subscriptions = new WC_Subscription( $subscription );
17040 $related[] = $subscriptions->get_related_orders();
17041 }
17042
17043 $failed_payment_orders = [];
17044 foreach ( $related as $value ) {
17045 foreach ( $value as $val ) {
17046 if ( wcs_order_contains_renewal( $val ) ) {
17047 $order = wc_get_order( $val );
17048 if ( $order ) {
17049 /**
17050 *
17051 * Ignore line
17052 *
17053 * @phpstan-ignore-next-line
17054 */
17055 $payment_status = $order->get_status();
17056 if ( 'failed' === $payment_status || 'wc-on-hold' === $payment_status ) {
17057 $failed_payment_orders[] = $val;
17058 }
17059 }
17060 }
17061 }
17062 }
17063
17064 if ( ! empty( $failed_payment_orders ) ) {
17065 $failed = array_rand( $failed_payment_orders );
17066 $related_subscriptions = wcs_get_subscriptions_for_order( $failed_payment_orders[ $failed ] );
17067 if ( ! empty( $related_subscriptions ) ) {
17068 $sub_id = get_post_meta( $failed_payment_orders[ $failed ], '_subscription_renewal', true );
17069 $subscription = wcs_get_subscription( $sub_id );
17070
17071 $items = $subscription->get_items();
17072 $product_ids = [];
17073 foreach ( $items as $item ) {
17074 $product_ids[] = $item->get_product_id();
17075 }
17076
17077 $context['user'] = WordPress::get_user_context( $subscription->get_user_id() );
17078 foreach ( [ 'renewal' ] as $order_type ) {
17079 foreach ( $subscription->get_related_orders( 'ids', $order_type ) as $order_id ) {
17080 $context['subscription_related_order'][] = $order_id;
17081 }
17082 }
17083 foreach ( $product_ids as $val ) {
17084 $context['subscription_item'] = $val;
17085 $context['subscription_name'] = get_the_title( $val );
17086 }
17087 $context['data'] = $subscription->get_data();
17088 $context['pluggable_data'] = $context;
17089 $context['response_type'] = 'live';
17090 } else {
17091 $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 );
17092 $context['response_type'] = 'sample';
17093 }
17094 } else {
17095 $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 );
17096 $context['response_type'] = 'sample';
17097 }
17098 }
17099 return $context;
17100 }
17101
17102 /**
17103 * Get Masteriyo LMS Courses.
17104 *
17105 * @param array $data data.
17106 *
17107 * @return array
17108 */
17109 public function search_masteriyo_lms_courses( $data ) {
17110
17111 $page = $data['page'];
17112 $limit = Utilities::get_search_page_limit();
17113 $offset = $limit * ( $page - 1 );
17114
17115 $args = [
17116 'post_type' => 'mto-course',
17117 'posts_per_page' => $limit,
17118 'offset' => $offset,
17119 'orderby' => 'title',
17120 'order' => 'ASC',
17121 'post_status' => 'publish',
17122 ];
17123
17124 $courses = get_posts( $args );
17125
17126 $course_count = wp_count_posts( 'mto-course' )->publish;
17127
17128 $options = [];
17129 if ( ! empty( $courses ) ) {
17130 if ( is_array( $courses ) ) {
17131 foreach ( $courses as $course ) {
17132 $options[] = [
17133 'label' => $course->post_title,
17134 'value' => $course->ID,
17135 ];
17136 }
17137 }
17138 }
17139 return [
17140 'options' => $options,
17141 'hasMore' => $course_count > $limit && $course_count > $offset,
17142 ];
17143 }
17144
17145 /**
17146 * Get Masteriyo LMS Lessons.
17147 *
17148 * @param array $data data.
17149 *
17150 * @return array
17151 */
17152 public function search_masteriyo_lms_lessons( $data ) {
17153
17154 $course_id = $data['dynamic'];
17155 $page = $data['page'];
17156 $limit = Utilities::get_search_page_limit();
17157 $offset = $limit * ( $page - 1 );
17158 $options = [];
17159 $args = [
17160 'post_type' => 'mto-lesson',
17161 'posts_per_page' => $limit,
17162 'offset' => $offset,
17163 'orderby' => 'title',
17164 'order' => 'ASC',
17165 'post_status' => 'publish',
17166 'meta_query' => [
17167 [
17168 'key' => '_course_id',
17169 'value' => $course_id,
17170 'compare' => '=',
17171 ],
17172 ],
17173 ];
17174
17175 $lessons = get_posts( $args );
17176 $lesson_count = wp_count_posts( 'mto-lesson' )->publish;
17177 if ( ! empty( $lessons ) ) {
17178 if ( is_array( $lessons ) ) {
17179 foreach ( $lessons as $lesson ) {
17180 $options[] = [
17181 'label' => $lesson->post_title,
17182 'value' => $lesson->ID,
17183 ];
17184 }
17185 }
17186 }
17187 return [
17188 'options' => $options,
17189 'hasMore' => $lesson_count > $limit && $lesson_count > $offset,
17190 ];
17191 }
17192
17193 /**
17194 * Get Masteriyo LMS Quiz.
17195 *
17196 * @param array $data data.
17197 *
17198 * @return array
17199 */
17200 public function search_masteriyo_lms_quizs( $data ) {
17201
17202 $course_id = $data['dynamic'];
17203 $page = $data['page'];
17204 $limit = Utilities::get_search_page_limit();
17205 $offset = $limit * ( $page - 1 );
17206 $options = [];
17207 $args = [
17208 'post_type' => 'mto-quiz',
17209 'posts_per_page' => $limit,
17210 'offset' => $offset,
17211 'orderby' => 'title',
17212 'order' => 'ASC',
17213 'post_status' => 'publish',
17214 'meta_query' => [
17215 [
17216 'key' => '_course_id',
17217 'value' => $course_id,
17218 'compare' => '=',
17219 ],
17220 ],
17221 ];
17222
17223 $quizs = get_posts( $args );
17224 $quiz_count = wp_count_posts( 'mto-quiz' )->publish;
17225 if ( ! empty( $quizs ) ) {
17226 if ( is_array( $quizs ) ) {
17227 foreach ( $quizs as $quiz ) {
17228 $options[] = [
17229 'label' => $quiz->post_title,
17230 'value' => $quiz->ID,
17231 ];
17232 }
17233 }
17234 }
17235 return [
17236 'options' => $options,
17237 'hasMore' => $quiz_count > $limit && $quiz_count > $offset,
17238 ];
17239 }
17240
17241 /**
17242 * Search Masteriyo LMS data.
17243 *
17244 * @param array $data data.
17245 * @return array|void|mixed
17246 */
17247 public function search_masteriyo_lms_last_data( $data ) {
17248 global $wpdb;
17249 $post_type = $data['post_type'];
17250 $trigger = $data['search_term'];
17251 $context = [];
17252
17253 $post_id = -1;
17254 if ( 'course_completed' === $trigger ) {
17255 $post_id = $data['filter']['course_id']['value'];
17256 } elseif ( 'lesson_completed' === $trigger ) {
17257 $post_id = $data['filter']['lesson_id']['value'];
17258 } elseif ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
17259 $post_id = $data['filter']['quiz_id']['value'];
17260 }
17261
17262 if ( 'course_completed' === $trigger || 'lesson_completed' === $trigger ) {
17263 if ( -1 === $post_id ) {
17264 $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 ) );
17265 } else {
17266 $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 ) );
17267 }
17268 }
17269
17270 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
17271 if ( -1 === $post_id ) {
17272 $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 ) );
17273 } else {
17274 $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 ) );
17275 }
17276 if ( ! empty( $result ) ) {
17277 $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 ) );
17278
17279 if ( function_exists( 'masteriyo_get_quiz' ) ) {
17280 $quiz = masteriyo_get_quiz( $resultt[0]->quiz_id );
17281 if ( is_object( $quiz ) && method_exists( $quiz, 'get_pass_mark' ) ) {
17282 if ( 'quiz_completed' === $trigger && $resultt[0]->earned_marks < $quiz->get_pass_mark() ) {
17283 $result = [];
17284 } elseif ( 'quiz_failed' === $trigger && $resultt[0]->earned_marks > $quiz->get_pass_mark() ) {
17285 $result = [];
17286 }
17287 }
17288 }
17289 }
17290 }
17291
17292 if ( ! empty( $result ) ) {
17293 $result_item_id = $result[0]->item_id;
17294 $result_user_id = $result[0]->user_id;
17295 $quiz_attempt = '';
17296 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
17297 $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 ) );
17298 $quiz_attempt = $resultt[0]->id;
17299 }
17300 switch ( $trigger ) {
17301 case 'course_completed':
17302 if ( function_exists( 'masteriyo_get_course' ) ) {
17303 $course = masteriyo_get_course( $result_item_id );
17304 $context_data = array_merge(
17305 WordPress::get_user_context( $result_user_id ),
17306 $course->get_data()
17307 );
17308 $context_data['course_id'] = $result_item_id;
17309 }
17310 break;
17311 case 'lesson_completed':
17312 if ( function_exists( 'masteriyo_get_lesson' ) ) {
17313 $lesson = masteriyo_get_lesson( $result_item_id );
17314 $context_data = array_merge(
17315 WordPress::get_user_context( $result_user_id ),
17316 $lesson->get_data()
17317 );
17318 $context_data['lesson_id'] = $result_item_id;
17319 }
17320 break;
17321 case 'quiz_completed':
17322 case 'quiz_failed':
17323 if ( function_exists( 'masteriyo_get_quiz' ) ) {
17324 $quiz = masteriyo_get_quiz( $result_item_id );
17325 $context_data = WordPress::get_user_context( $result_user_id );
17326 if ( function_exists( 'masteriyo_get_quiz_attempt' ) ) {
17327 $attempt = masteriyo_get_quiz_attempt( $quiz_attempt );
17328 $context_data['quiz_id'] = $result_item_id;
17329 $context_data['course_id'] = $quiz->get_course_id();
17330 $context_data['quiz'] = $quiz->get_data();
17331 $context_data['attempt'] = $attempt->get_data();
17332 }
17333 }
17334 break;
17335 default:
17336 return;
17337 }
17338 if ( ! empty( $context_data ) ) {
17339 $context['pluggable_data'] = $context_data;
17340 $context['response_type'] = 'live';
17341 }
17342 } elseif ( empty( $result ) ) {
17343 switch ( $trigger ) {
17344 case 'course_completed':
17345 $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"} ';
17346 break;
17347 case 'lesson_completed':
17348 $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"} ';
17349 break;
17350 case 'quiz_completed':
17351 case 'quiz_failed':
17352 $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"}';
17353 break;
17354 default:
17355 return;
17356 }
17357 $context = json_decode( $sample_data, true );
17358 }
17359
17360 return $context;
17361 }
17362
17363 /**
17364 * Search learndash user added in group.
17365 *
17366 * @param array $data data.
17367 * @return array|void
17368 */
17369 public function search_pluggables_user_added_in_group( $data ) {
17370 $context = [];
17371
17372 if ( ! function_exists( 'learndash_get_groups_user_ids' ) ) {
17373 return;
17374 }
17375 $term = $data['search_term'];
17376 $group_id = $data['filter']['sfwd_group_id']['value'];
17377
17378 if ( 'user_added_group' == $term ) {
17379 if ( -1 === $group_id ) {
17380 $args = [
17381 'numberposts' => 1,
17382 'orderby' => 'rand',
17383 'post_type' => 'groups',
17384 ];
17385 $posts = get_posts( $args );
17386 $random_value = $posts[0]->ID;
17387 $group_users = learndash_get_groups_user_ids( $random_value );
17388 $group_id = $random_value;
17389 } else {
17390 $group_users = learndash_get_groups_user_ids( $group_id );
17391 }
17392 $users = get_users(
17393 [
17394 'meta_key' => 'learndash_group_' . $group_id . '_enrolled_at',
17395 'orderby' => 'meta_value',
17396 'order' => 'DESC',
17397 'number' => 1,
17398 'include' => $group_users,
17399 ]
17400 );
17401 } elseif ( 'user_removed_group' == $term ) {
17402 if ( -1 === $group_id ) {
17403 $args = [
17404 'numberposts' => 1,
17405 'orderby' => 'rand',
17406 'post_type' => 'groups',
17407 ];
17408 $posts = get_posts( $args );
17409 $random_value = $posts[0]->ID;
17410 $group_id = $random_value;
17411 }
17412 $args = [
17413 'meta_query' => [
17414 'relation' => 'AND',
17415 [
17416 'key' => 'group_' . $group_id . '_access_from',
17417 'compare' => 'NOT EXISTS',
17418 ],
17419 [
17420 'key' => 'learndash_group_' . $group_id . '_enrolled_at',
17421 'compare' => 'EXISTS',
17422 ],
17423 ],
17424 'orderby' => 'meta_value',
17425 'order' => 'DESC',
17426 'number' => 1,
17427 ];
17428 $users = get_users( $args );
17429 } elseif ( 'leader_added_group' == $term || 'leader_removed_group' == $term ) {
17430 if ( -1 === $group_id ) {
17431 $args = [
17432 'numberposts' => 1,
17433 'fields' => 'ids',
17434 'orderby' => 'rand',
17435 'post_type' => 'groups',
17436 ];
17437 $posts = get_posts( $args );
17438 $random_key = array_rand( $posts );
17439 $random_value = $posts[ $random_key ];
17440 $group_id = $random_value;
17441 }
17442 $user_query_args = [
17443 'orderby' => 'learndash_group_leaders_' . intval( $group_id ),
17444 'order' => 'DESC',
17445 'meta_query' => [
17446 [
17447 'key' => 'learndash_group_leaders_' . intval( $group_id ),
17448 'value' => intval( $group_id ),
17449 'compare' => '=',
17450 'type' => 'NUMERIC',
17451 ],
17452 ],
17453 ];
17454 $users = get_users( $user_query_args );
17455 }
17456
17457 if ( ! empty( $users ) ) {
17458 $context = WordPress::get_user_context( $users[0]->ID );
17459 $context['sfwd_group_id'] = $group_id;
17460 $context['group_title'] = get_the_title( $group_id );
17461 $context['group_url'] = get_permalink( $group_id );
17462 $context['group_featured_image_id'] = get_post_meta( $group_id, '_thumbnail_id', true );
17463 $context['group_featured_image_url'] = get_the_post_thumbnail_url( $group_id );
17464 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
17465 $group_courses_id = learndash_group_enrolled_courses( $group_id );
17466 if ( ! empty( $group_courses_id ) ) {
17467 foreach ( $group_courses_id as $key => $course_id ) {
17468 $context['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
17469 }
17470 }
17471 }
17472 $context['response_type'] = 'live';
17473 } else {
17474 $context = WordPress::get_sample_user_context();
17475 $context['group_title'] = 'Test Group';
17476 $context['sfwd_group_id'] = 112;
17477 $context['group_url'] = 'https://example.com/test-group';
17478 $context['group_featured_image_id'] = 113;
17479 $context['group_featured_image_url'] = 'https://example.com/test-group-img';
17480 $context['group_courses'] = [
17481 [
17482 'ID' => 7915,
17483 'title' => 'Example Course',
17484 'URL' => 'https://example.com/courses/example-course/',
17485 'status' => 'publish',
17486 'featured_image_id' => '',
17487 'featured_image_url' => false,
17488 ],
17489 ];
17490 $context['response_type'] = 'sample';
17491 }
17492
17493 $context['pluggable_data'] = $context;
17494 return $context;
17495 }
17496
17497 /**
17498 * Search learndash user enrolled in course.
17499 *
17500 * @param array $data data.
17501 * @return array|void
17502 */
17503 public function search_pluggables_user_enrolled_course( $data ) {
17504 global $wpdb;
17505 $context = [];
17506
17507 if ( ! function_exists( 'ld_course_access_expires_on' ) ||
17508 ! function_exists( 'learndash_group_enrolled_courses' ) ||
17509 ! function_exists( 'learndash_get_lesson_list' ) ) {
17510 return;
17511 }
17512 $term = $data['search_term'];
17513 $course_id = isset( $data['filter']['sfwd_course_id'] ) ? $data['filter']['sfwd_course_id']['value'] : '';
17514 $group_id = isset( $data['filter']['sfwd_group_id'] ) ? $data['filter']['sfwd_group_id']['value'] : '';
17515
17516 if ( 'course_enrolled' == $term ) {
17517 if ( -1 === $course_id ) {
17518 $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 ) );
17519 } else {
17520 $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 ) );
17521 }
17522 } elseif ( 'course_unenrolled' == $term ) {
17523 if ( -1 === $course_id ) {
17524 $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 ) );
17525 $course_id = $courses[0]->course_id;
17526 }
17527 $args = [
17528 'meta_query' => [
17529 'relation' => 'AND',
17530 [
17531 'key' => 'course_' . $course_id . '_access_from',
17532 'compare' => 'NOT EXISTS',
17533 ],
17534 [
17535 'key' => 'learndash_course_' . $course_id . '_enrolled_at',
17536 'compare' => 'EXISTS',
17537 ],
17538 ],
17539 ];
17540 $users = get_users( $args );
17541 } elseif ( 'course_access_expired' == $term ) {
17542 if ( -1 === $course_id ) {
17543 $course_args = [
17544 'post_type' => 'sfwd-courses',
17545 'posts_per_page' => 1,
17546 'orderby' => 'rand',
17547 'order' => 'ASC',
17548 'post_status' => 'publish',
17549 ];
17550 $courses_posts = get_posts( $course_args );
17551 $course_id = $courses_posts[0]->ID;
17552 }
17553 $args = [
17554 'meta_query' => [
17555 [
17556 'key' => 'learndash_course_expired_' . $course_id,
17557 'compare' => 'EXISTS',
17558 ],
17559 ],
17560 ];
17561 $users = get_users( $args );
17562 } elseif ( 'group_course_completed' == $term ) {
17563 if ( -1 === $group_id ) {
17564 $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 ) );
17565 } else {
17566 $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 ) );
17567 }
17568 $activity_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity_meta WHERE activity_id = %d", $courses[0]->activity_id ) );
17569 } elseif ( 'course_group_added' == $term ) {
17570 if ( -1 === $group_id ) {
17571 $args = [
17572 'numberposts' => 1,
17573 'fields' => 'ids',
17574 'orderby' => 'rand',
17575 'post_type' => 'groups',
17576 ];
17577 $posts = get_posts( $args );
17578 $random_key = array_rand( $posts );
17579 $random_value = $posts[ $random_key ];
17580 $group_id = $random_value;
17581 }
17582 $courses = learndash_group_enrolled_courses( $group_id );
17583 } elseif ( 'assignment_submitted' == $term ) {
17584 $course_id = isset( $data['filter']['sfwd-courses'] ) ? $data['filter']['sfwd-courses']['value'] : '';
17585 $lesson_id = $data['filter']['sfwd_lesson_topic_id']['value'];
17586 if ( -1 === $course_id && -1 === $lesson_id ) {
17587 $args = [
17588 'post_type' => 'sfwd-assignment',
17589 'posts_per_page' => 1,
17590 'order' => 'DESC',
17591 'post_status' => 'publish',
17592 ];
17593 } else {
17594 if ( -1 === $course_id ) {
17595 $courses = get_posts(
17596 [
17597 'posts_per_page' => - 1,
17598 'post_type' => 'sfwd-courses',
17599 'post_status' => 'publish',
17600 'fields' => 'ids',
17601 ]
17602 );
17603 $course_key = array_rand( $courses );
17604 $course_id = $courses[ $course_key ];
17605 }
17606 if ( -1 === $lesson_id ) {
17607 $args = [
17608 'post_type' => 'sfwd-assignment',
17609 'posts_per_page' => 1,
17610 'order' => 'DESC',
17611 'post_status' => 'publish',
17612 'meta_query' => [
17613 [
17614 'key' => 'course_id',
17615 'value' => $course_id,
17616 'compare' => '=',
17617 ],
17618 ],
17619 ];
17620 } else {
17621 $args = [
17622 'post_type' => 'sfwd-assignment',
17623 'posts_per_page' => 1,
17624 'order' => 'DESC',
17625 'post_status' => 'publish',
17626 'meta_query' => [
17627 'relation' => 'AND',
17628 [
17629 'key' => 'lesson_id',
17630 'value' => $lesson_id,
17631 'compare' => '=',
17632 ],
17633 [
17634 'key' => 'course_id',
17635 'value' => $course_id,
17636 'compare' => '=',
17637 ],
17638 ],
17639 ];
17640 }
17641 }
17642 $assignments = get_posts( $args );
17643 } elseif ( 'assignment_graded' == $term ) {
17644 $course_id = isset( $data['filter']['sfwd-courses'] ) ? $data['filter']['sfwd-courses']['value'] : '';
17645 $lesson_id = $data['filter']['sfwd_lesson_topic_id']['value'];
17646 if ( -1 === $course_id && -1 === $lesson_id ) {
17647 $args = [
17648 'post_type' => 'sfwd-assignment',
17649 'posts_per_page' => 1,
17650 'order' => 'DESC',
17651 'post_status' => 'publish',
17652 'meta_query' => [
17653 [
17654 'key' => 'approval_status',
17655 'compare' => 'EXISTS',
17656 ],
17657 ],
17658 ];
17659 } else {
17660 if ( -1 === $course_id ) {
17661 $courses = get_posts(
17662 [
17663 'posts_per_page' => - 1,
17664 'post_type' => 'sfwd-courses',
17665 'post_status' => 'publish',
17666 'fields' => 'ids',
17667 ]
17668 );
17669 $course_key = array_rand( $courses );
17670 $course_id = $courses[ $course_key ];
17671 }
17672 if ( -1 === $lesson_id ) {
17673 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
17674 if ( ! empty( $lessons ) ) {
17675 $random_key = array_rand( $lessons );
17676 $random_value = $lessons[ $random_key ];
17677 $lesson_id = $random_value->ID;
17678 }
17679 }
17680 $args = [
17681 'post_type' => 'sfwd-assignment',
17682 'posts_per_page' => 1,
17683 'order' => 'DESC',
17684 'post_status' => 'publish',
17685 'meta_query' => [
17686 'relation' => 'AND',
17687 [
17688 'key' => 'lesson_id',
17689 'value' => $lesson_id,
17690 'compare' => '=',
17691 ],
17692 [
17693 'key' => 'course_id',
17694 'value' => $course_id,
17695 'compare' => '=',
17696 ],
17697 [
17698 'key' => 'approval_status',
17699 'compare' => 'EXISTS',
17700 ],
17701 ],
17702 ];
17703 }
17704 $assignments = get_posts( $args );
17705 }
17706
17707 if ( 'assignment_submitted' == $term || 'assignment_graded' == $term ) {
17708 if ( ! empty( $assignments ) ) {
17709 $context_data = WordPress::get_user_context( (int) $assignments[0]->post_author );
17710 $context_data['assignment_id'] = $assignments[0]->ID;
17711 $context_data['assignment_title'] = $assignments[0]->post_title;
17712 $context_data['assignment_url'] = get_post_meta( $assignments[0]->ID, 'file_link', true );
17713 $context_data['lesson_id'] = get_post_meta( $assignments[0]->ID, 'lesson_id', true );
17714 $context_data['course_id'] = get_post_meta( $assignments[0]->ID, 'course_id', true );
17715 $context_data['points'] = get_post_meta( $assignments[0]->ID, 'points', true );
17716 $context['response_type'] = 'live';
17717 } else {
17718 $context_data = WordPress::get_sample_user_context();
17719 $context_data['assignment_title'] = 'Test Assignment';
17720 $context_data['assignment_id'] = 112;
17721 $context_data['assignment_url'] = 'https://example.com/test_assignment.pdf';
17722 $context_data['lesson_id'] = 2;
17723 $context_data['course_id'] = 1;
17724 $context_data['points'] = '12';
17725 $context['response_type'] = 'sample';
17726 }
17727 } elseif ( 'course_unenrolled' == $term || 'course_access_expired' == $term ) {
17728 if ( ! empty( $users ) ) {
17729 $context_data = WordPress::get_user_context( $users[0]->ID );
17730 $context_data['sfwd_course_id'] = $course_id;
17731 $context_data['course_title'] = get_the_title( $course_id );
17732 $context_data['course_url'] = get_permalink( $course_id );
17733 $context_data['course_featured_image_id'] = get_post_meta( $course_id, '_thumbnail_id', true );
17734 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course_id );
17735 $timestamp = ld_course_access_expires_on( $course_id, $users[0]->ID );
17736 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
17737 $date_format = get_option( 'date_format' );
17738 if ( is_string( $date_format ) ) {
17739 $context_data['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
17740 }
17741 $context['response_type'] = 'live';
17742 } else {
17743 $context_data = WordPress::get_sample_user_context();
17744 $context_data['course_name'] = 'Test Course';
17745 $context_data['sfwd_course_id'] = 112;
17746 $context_data['course_url'] = 'https://example.com/test-course';
17747 $context_data['course_featured_image_id'] = 113;
17748 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
17749 $context_data['course_access_expiry_date'] = '2023-10-20';
17750 $context['response_type'] = 'sample';
17751 }
17752 } elseif ( 'course_group_added' == $term ) {
17753 if ( ! empty( $courses ) ) {
17754 $context_data['course_id'] = $courses[0];
17755 $context_data['course_title'] = get_the_title( $courses[0] );
17756 $context_data['course_url'] = get_permalink( $courses[0] );
17757 $context_data['course_featured_image_id'] = get_post_meta( $courses[0], '_thumbnail_id', true );
17758 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $courses[0] );
17759 $context_data['sfwd_group_id'] = $group_id;
17760 $context_data['group_name'] = get_the_title( $group_id );
17761 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
17762 $group_courses_id = learndash_group_enrolled_courses( $group_id );
17763 if ( ! empty( $group_courses_id ) ) {
17764 foreach ( $group_courses_id as $key => $course_id ) {
17765 $context_data['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
17766 }
17767 }
17768 }
17769 $context['response_type'] = 'live';
17770 } else {
17771 $context_data = WordPress::get_sample_user_context();
17772 $context_data['course_name'] = 'Test Course';
17773 $context_data['sfwd_course_id'] = 112;
17774 $context_data['course_url'] = 'https://example.com/test-course';
17775 $context_data['course_featured_image_id'] = 113;
17776 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
17777 $context_data['course_access_expiry_date'] = '2023-10-20';
17778 $context_data['sfwd_group_id'] = 12;
17779 $context_data['group_name'] = 'Test Group';
17780 $context_data['group_courses'] = [
17781 [
17782 'ID' => 7915,
17783 'title' => 'Example Course',
17784 'URL' => 'https://example.com/courses/example-course/',
17785 'status' => 'publish',
17786 'featured_image_id' => '',
17787 'featured_image_url' => false,
17788 ],
17789 ];
17790 $context['response_type'] = 'sample';
17791 }
17792 } elseif ( 'group_course_completed' == $term ) {
17793 if ( ! empty( $courses ) && ! empty( $activity_meta ) ) {
17794 $context_data = WordPress::get_user_context( $courses[0]->user_id );
17795 $context_data['sfwd_group_id'] = $courses[0]->post_id;
17796 $context_data['group_title'] = get_the_title( $courses[0]->post_id );
17797 $context_data['group_url'] = get_permalink( $courses[0]->post_id );
17798 $context_data['group_featured_image_id'] = get_post_meta( $courses[0]->post_id, '_thumbnail_id', true );
17799 $context_data['group_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->post_id );
17800 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
17801 $group_courses_id = learndash_group_enrolled_courses( $courses[0]->post_id );
17802 if ( ! empty( $group_courses_id ) ) {
17803 foreach ( $group_courses_id as $key => $course_id ) {
17804 $context_data['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
17805 }
17806 }
17807 }
17808 $course_ids = null;
17809 foreach ( $activity_meta as $item ) {
17810 if ( 'course_ids' === $item->activity_meta_key ) {
17811 $course_ids = unserialize( $item->activity_meta_value );
17812 break;
17813 }
17814 }
17815 if ( ! empty( $course_ids ) && is_array( $course_ids ) ) {
17816 foreach ( $course_ids as $key => $course_id ) {
17817 if ( is_numeric( $course_id ) ) {
17818 $course_id = (int) $course_id;
17819 $args = [
17820 'include' => [ $courses[0]->user_id ],
17821 'meta_query' => [
17822 [
17823 'key' => 'course_completed_' . $course_id,
17824 'compare' => 'EXISTS',
17825 ],
17826 ],
17827 ];
17828 $users = get_users( $args );
17829 if ( ! empty( $users ) ) {
17830 $context_data[ 'completed ' . $key ]['course_id'] = $course_id;
17831 $context_data[ 'completed ' . $key ]['course_title'] = get_the_title( $course_id );
17832 $context_data[ 'completed ' . $key ]['course_url'] = get_permalink( $course_id );
17833 $context_data[ 'completed ' . $key ]['course_featured_image_id'] = get_post_meta( $course_id, '_thumbnail_id', true );
17834 $context_data[ 'completed ' . $key ]['course_featured_image_url'] = get_the_post_thumbnail_url( $course_id );
17835 $timestamp = ld_course_access_expires_on( $course_id, $courses[0]->user_id );
17836 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
17837 $date_format = get_option( 'date_format' );
17838 if ( is_string( $date_format ) ) {
17839 $context_data[ 'completed ' . $key ]['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
17840 }
17841 }
17842 }
17843 }
17844 }
17845 $context['response_type'] = 'live';
17846 } else {
17847 $context_data = WordPress::get_sample_user_context();
17848 $context_data['sfwd_group_id'] = 112;
17849 $context_data['group_title'] = 'Test Group';
17850 $context_data['group_url'] = 113;
17851 $context_data['group_featured_image_id'] = 11;
17852 $context_data['group_featured_image_url'] = 'https://example.com/test-group-img';
17853 $context_data['completed 0'] = [
17854 'course_id' => 10,
17855 'course_title' => 'Test Course',
17856 'course_url' => 'https://example.com/test-course',
17857 'course_featured_image_id' => 14,
17858 'course_featured_image_url' => 'https://example.com/test-course-img',
17859 ];
17860 $context_data['group_courses'] = [
17861 [
17862 'ID' => 7915,
17863 'title' => 'Example Course',
17864 'URL' => 'https://example.com/courses/example-course/',
17865 'status' => 'publish',
17866 'featured_image_id' => '',
17867 'featured_image_url' => false,
17868 ],
17869 ];
17870 $context['response_type'] = 'sample';
17871 }
17872 } else {
17873 if ( ! empty( $courses ) ) {
17874 $context_data = WordPress::get_user_context( $courses[0]->user_id );
17875 $context_data['course_id'] = $courses[0]->course_id;
17876 $context_data['course_title'] = get_the_title( $courses[0]->course_id );
17877 $context_data['course_url'] = get_permalink( $courses[0]->course_id );
17878 $context_data['course_featured_image_id'] = get_post_meta( $courses[0]->course_id, '_thumbnail_id', true );
17879 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->course_id );
17880 $timestamp = ld_course_access_expires_on( $courses[0]->course_id, $courses[0]->user_id );
17881 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
17882 $date_format = get_option( 'date_format' );
17883 if ( is_string( $date_format ) ) {
17884 $context_data['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
17885 }
17886 if ( $courses[0]->post_id ) {
17887 $context_data['sfwd_group_id'] = $courses[0]->post_id;
17888 $context_data['group_title'] = get_the_title( $courses[0]->post_id );
17889 $context_data['group_url'] = get_permalink( $courses[0]->post_id );
17890 $context_data['group_featured_image_id'] = get_post_meta( $courses[0]->post_id, '_thumbnail_id', true );
17891 $context_data['group_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->post_id );
17892 }
17893 $context['response_type'] = 'live';
17894 } else {
17895 $context_data = WordPress::get_sample_user_context();
17896 $context_data['course_name'] = 'Test Course';
17897 $context_data['sfwd_course_id'] = 112;
17898 $context_data['course_url'] = 'https://example.com/test-course';
17899 $context_data['course_featured_image_id'] = 113;
17900 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
17901 $context_data['course_access_expiry_date'] = '2023-10-20';
17902 $context['response_type'] = 'sample';
17903 }
17904 }
17905
17906 $context['pluggable_data'] = $context_data;
17907 return $context;
17908 }
17909
17910 /**
17911 * Search LearnDash Lesson Topic List.
17912 *
17913 * @param array $data Search Params.
17914 *
17915 * @since 1.0.0
17916 *
17917 * @return array
17918 */
17919 public function search_ld_lessons_topics_list( $data ) {
17920 $options = [];
17921 $course_id = $data['dynamic']['sfwd-courses'];
17922
17923 if ( ! function_exists( 'learndash_get_lesson_list' ) || ! function_exists( 'learndash_get_topic_list' ) ) {
17924 return [];
17925 }
17926
17927 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
17928 foreach ( $lessons as $lesson ) {
17929 $options[] = [
17930 'label' => $lesson->post_title,
17931 'value' => $lesson->ID,
17932 ];
17933 $topics = learndash_get_topic_list( $lesson->ID, $course_id );
17934 foreach ( $topics as $topic ) {
17935 $options[] = [
17936 'label' => $topic->post_title,
17937 'value' => $topic->ID,
17938 ];
17939 }
17940 }
17941
17942 return [
17943 'options' => $options,
17944 'hasMore' => false,
17945 ];
17946 }
17947
17948 /**
17949 * Search LearnDash Quiz List.
17950 *
17951 * @param array $data Search Params.
17952 *
17953 * @since 1.0.0
17954 *
17955 * @return array
17956 */
17957 public function search_ld_quiz_list( $data ) {
17958 $options = [];
17959 $course_id = $data['dynamic']['sfwd-courses'];
17960 $lesson_id = $data['dynamic']['sfwd_lessons_topics'];
17961
17962 if ( ! function_exists( 'learndash_get_course_quiz_list' ) || ! function_exists( 'learndash_get_lesson_quiz_list' ) ) {
17963 return [];
17964 }
17965
17966 $quizzes = learndash_get_course_quiz_list( $course_id );
17967 $quizzes = array_merge( $quizzes, learndash_get_lesson_quiz_list( $lesson_id, null, $course_id ) );
17968 if ( ! empty( $quizzes ) ) {
17969 foreach ( $quizzes as $quiz ) {
17970 $options[] = [
17971 'label' => $quiz['post']->post_title,
17972 'value' => $quiz['post']->ID,
17973
17974 ];
17975 }
17976 }
17977
17978 return [
17979 'options' => $options,
17980 'hasMore' => false,
17981 ];
17982 }
17983
17984 /**
17985 * Search LearnDash Quiz Essay Question List.
17986 *
17987 * @param array $data Search Params.
17988 *
17989 * @since 1.0.0
17990 *
17991 * @return array
17992 */
17993 public function search_ld_quiz_essay_question_list( $data ) {
17994 $options = [];
17995 $quiz_id = $data['dynamic'];
17996
17997 if ( ! function_exists( 'learndash_get_quiz_questions' ) ) {
17998 return [];
17999 }
18000
18001 if ( 0 < $quiz_id ) {
18002 $quiz_question_ids = learndash_get_quiz_questions( $quiz_id );
18003 if ( ! empty( $quiz_question_ids ) ) {
18004 foreach ( $quiz_question_ids as $question_post_id => $question_pro_id ) {
18005 $question_type = get_post_meta( $question_post_id, 'question_type', true );
18006 if ( is_string( $question_type ) && 'essay' === $question_type ) {
18007 $title = html_entity_decode( get_the_title( $question_post_id ), ENT_QUOTES, 'UTF-8' );
18008 $options[] = [
18009 'label' => $title,
18010 'value' => $question_post_id,
18011 ];
18012 }
18013 }
18014 }
18015 }
18016
18017 return [
18018 'options' => $options,
18019 'hasMore' => false,
18020 ];
18021 }
18022
18023 /**
18024 * Search LearnDash Lessons List.
18025 *
18026 * @param array $data Search Params.
18027 *
18028 * @since 1.0.0
18029 *
18030 * @return array
18031 */
18032 public function search_ld_lessons_list( $data ) {
18033 $options = [];
18034 $course_id = $data['dynamic']['sfwd-courses'];
18035
18036 if ( ! function_exists( 'learndash_get_lesson_list' ) ) {
18037 return [];
18038 }
18039
18040 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
18041 foreach ( $lessons as $lesson ) {
18042 $options[] = [
18043 'label' => $lesson->post_title,
18044 'value' => $lesson->ID,
18045 ];
18046 }
18047 return [
18048 'options' => $options,
18049 'hasMore' => false,
18050 ];
18051 }
18052
18053 /**
18054 * Search LearnDash Topics List.
18055 *
18056 * @param array $data Search Params.
18057 *
18058 * @since 1.0.0
18059 *
18060 * @return array
18061 */
18062 public function search_ld_topics_list( $data ) {
18063 $options = [];
18064 $course_id = $data['dynamic']['sfwd-courses'];
18065 $lesson_id = $data['dynamic']['sfwd-lessons'];
18066
18067 if ( ! function_exists( 'learndash_get_topic_list' ) ) {
18068 return [];
18069 }
18070
18071 $topics = learndash_get_topic_list( $lesson_id, $course_id );
18072 foreach ( $topics as $topic ) {
18073 $options[] = [
18074 'label' => $topic->post_title,
18075 'value' => $topic->ID,
18076 ];
18077 }
18078 return [
18079 'options' => $options,
18080 'hasMore' => false,
18081 ];
18082 }
18083
18084 /**
18085 * Search LearnDash Assignments List.
18086 *
18087 * @param array $data Search Params.
18088 *
18089 * @since 1.0.0
18090 *
18091 * @return array
18092 */
18093 public function search_ld_assignments_list( $data ) {
18094 $options = [];
18095 $course_id = $data['dynamic']['sfwd-courses'];
18096 $lesson_id = $data['dynamic']['sfwd_lesson_topic_id'];
18097
18098 $args = [
18099 'post_type' => 'sfwd-assignment',
18100 'numberposts' => - 1,
18101 ];
18102 $meta_query = [];
18103 if ( ! empty( $course_id ) ) {
18104 $meta_query[] = [
18105 'key' => 'course_id',
18106 'value' => (int) $course_id,
18107 'compare' => '=',
18108 ];
18109 }
18110 if ( ! empty( $lesson_id ) ) {
18111 $meta_query[] = [
18112 'key' => 'lesson_id',
18113 'value' => (int) $lesson_id,
18114 'compare' => '=',
18115 ];
18116 }
18117 if ( ! empty( $meta_query ) ) {
18118 $args['meta_query'] = $meta_query;
18119 if ( count( $meta_query ) > 1 ) {
18120 $args['meta_query']['relation'] = 'AND';
18121 }
18122 }
18123 $assignments = get_posts( $args );
18124 foreach ( $assignments as $assignment ) {
18125 $options[] = [
18126 'label' => $assignment->post_title,
18127 'value' => $assignment->ID,
18128 ];
18129 }
18130 return [
18131 'options' => $options,
18132 'hasMore' => false,
18133 ];
18134 }
18135
18136 /**
18137 * Search post by post type.
18138 *
18139 * @param array $data Search Params.
18140 *
18141 * @since 1.0.0
18142 *
18143 * @return array
18144 */
18145 public function search_edd_prices( $data ) {
18146 $options = [];
18147 $downlaod_id = $data['dynamic']['download_id'];
18148
18149 $variable_prices = get_post_meta( $downlaod_id, 'edd_variable_prices', true );
18150 if ( ! empty( $variable_prices ) && is_array( $variable_prices ) ) {
18151 foreach ( $variable_prices as $price_id => $price ) {
18152 if ( isset( $price['name'] ) ) {
18153 $options[] = [
18154 'label' => $price['name'] . ' (' . $price['amount'] . ')',
18155 'value' => $price['index'],
18156 ];
18157 }
18158 }
18159 }
18160
18161 return [
18162 'options' => $options,
18163 'hasMore' => false,
18164 ];
18165
18166 }
18167
18168 /**
18169 * GetPowerful Docs list.
18170 *
18171 * @param array $data data.
18172 *
18173 * @return array
18174 */
18175 public function search_pfd_docs_list( $data ) {
18176
18177 $course_id = $data['dynamic'];
18178 $page = $data['page'];
18179 $limit = Utilities::get_search_page_limit();
18180 $offset = $limit * ( $page - 1 );
18181 $options = [];
18182 $args = [
18183 'post_type' => 'docs',
18184 'posts_per_page' => $limit,
18185 'offset' => $offset,
18186 'orderby' => 'title',
18187 'order' => 'ASC',
18188 'post_status' => 'publish',
18189 ];
18190
18191 $docs = get_posts( $args );
18192 $docs_count = wp_count_posts( 'docs' )->publish;
18193 if ( ! empty( $docs ) ) {
18194 if ( is_array( $docs ) ) {
18195 foreach ( $docs as $doc ) {
18196 $options[] = [
18197 'label' => $doc->post_title,
18198 'value' => $doc->ID,
18199 ];
18200 }
18201 }
18202 }
18203 return [
18204 'options' => $options,
18205 'hasMore' => $docs_count > $limit && $docs_count > $offset,
18206 ];
18207 }
18208
18209 /**
18210 * Search Powerful Docs last data.
18211 *
18212 * @param array $data data.
18213 * @return array|void|mixed
18214 */
18215 public function search_pfd_feedback_last_data( $data ) {
18216 $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"}';
18217 $context = json_decode( $sample_data, true );
18218
18219 return $context;
18220 }
18221
18222 /**
18223 * WooCommerce Coupon Discount type list.
18224 *
18225 * @param array $data data.
18226 *
18227 * @return array
18228 */
18229 public function search_woo_coupon_discount_type_list( $data ) {
18230 $options = [];
18231
18232 $discount_types = wc_get_coupon_types();
18233
18234 if ( ! empty( $discount_types ) ) {
18235 if ( is_array( $discount_types ) ) {
18236 foreach ( $discount_types as $key => $value ) {
18237 $options[] = [
18238 'label' => $value,
18239 'value' => $key,
18240 ];
18241 }
18242 }
18243 }
18244
18245 return [
18246 'options' => $options,
18247 'hasMore' => false,
18248 ];
18249 }
18250
18251 /**
18252 * WooCommerce Product list along with variation list.
18253 *
18254 * @param array $data data.
18255 *
18256 * @return array|void
18257 */
18258 public function search_woo_product_variation_list( $data ) {
18259 $options = [];
18260
18261 $page = $data['page'];
18262 $limit = Utilities::get_search_page_limit();
18263 $offset = $limit * ( $page - 1 );
18264
18265 if ( ! function_exists( 'wc_get_products' ) ) {
18266 return;
18267 }
18268 $products = wc_get_products(
18269 [
18270 'posts_per_page' => $limit,
18271 'offset' => $offset,
18272 'orderby' => 'date',
18273 'order' => 'DESC',
18274 ]
18275 );
18276
18277
18278 $prod_count = wp_count_posts( 'product' )->publish;
18279
18280 if ( ! empty( $products ) ) {
18281 if ( is_array( $products ) ) {
18282 foreach ( $products as $product ) {
18283 $options[] = [
18284 'label' => $product->get_name(),
18285 'value' => $product->get_id(),
18286 ];
18287 if ( $product->is_type( 'variable' ) ) {
18288 $variations = Utilities::get_product_variations( $product->get_id() );
18289 foreach ( $variations['result'] as $variation ) {
18290 $options[] = [
18291 'label' => $variation->post_title,
18292 'value' => $variation->ID,
18293 ];
18294 }
18295 }
18296 }
18297 }
18298 }
18299 return [
18300 'options' => $options,
18301 'hasMore' => $prod_count > $limit && $prod_count > $offset,
18302 ];
18303 }
18304
18305 /**
18306 * WooCommerce Product list along with variation list.
18307 *
18308 * @param array $data data.
18309 *
18310 * @return array
18311 */
18312 public function search_woo_coupon_list( $data ) {
18313 $options = [];
18314
18315 $page = $data['page'];
18316 $limit = Utilities::get_search_page_limit();
18317 $offset = $limit * ( $page - 1 );
18318
18319 $coupons = get_posts(
18320 [
18321 'posts_per_page' => - 1,
18322 'orderby' => 'name',
18323 'order' => 'asc',
18324 'post_type' => 'shop_coupon',
18325 'post_status' => 'publish',
18326 ]
18327 );
18328 $coupon_count = wp_count_posts( 'shop_coupon' )->publish;
18329
18330 if ( ! empty( $coupons ) ) {
18331 if ( is_array( $coupons ) ) {
18332 foreach ( $coupons as $coupon ) {
18333 $code = wc_format_coupon_code( $coupon->post_title );
18334 $options[] = [
18335 'label' => $code,
18336 'value' => $code,
18337 ];
18338 }
18339 }
18340 }
18341
18342 return [
18343 'options' => $options,
18344 'hasMore' => $coupon_count > $limit && $coupon_count > $offset,
18345 ];
18346 }
18347
18348 /**
18349 * Prepare LatePoint Bookings List.
18350 *
18351 * @param array $data Search Params.
18352 * @return array
18353 */
18354 public function search_lp_bookings_list( $data ) {
18355
18356 if ( ! class_exists( 'OsBookingHelper' ) ) {
18357 return [];
18358 }
18359
18360 $bookings = OsBookingHelper::get_bookings_for_select();
18361 $options = [];
18362
18363 if ( ! empty( $bookings ) ) {
18364 foreach ( $bookings as $key => $booking ) {
18365 $options[] = [
18366 'label' => $booking['label'],
18367 'value' => $booking['value'],
18368 ];
18369 }
18370 }
18371
18372 return [
18373 'options' => $options,
18374 'hasMore' => false,
18375 ];
18376 }
18377
18378 /**
18379 * Prepare LatePoint Services List.
18380 *
18381 * @param array $data Search Params.
18382 * @return array
18383 */
18384 public function search_lp_services_list( $data ) {
18385
18386 if ( ! class_exists( 'OsServiceHelper' ) ) {
18387 return [];
18388 }
18389
18390 $services = OsServiceHelper::get_services_list();
18391 $options = [];
18392
18393 if ( ! empty( $services ) ) {
18394 foreach ( $services as $key => $service ) {
18395 $options[] = [
18396 'label' => $service['label'],
18397 'value' => $service['value'],
18398 ];
18399 }
18400 }
18401
18402 return [
18403 'options' => $options,
18404 'hasMore' => false,
18405 ];
18406 }
18407
18408 /**
18409 * Prepare LatePoint Bundles List.
18410 *
18411 * @param array $data Search Params.
18412 * @return array
18413 */
18414 public function search_lp_bundles_list( $data ) {
18415 global $wpdb;
18416
18417 $bundles = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}latepoint_bundles", ARRAY_A );
18418
18419 $options = [];
18420
18421 if ( ! empty( $bundles ) ) {
18422 foreach ( $bundles as $bundle ) {
18423 $options[] = [
18424 'label' => $bundle['name'],
18425 'value' => $bundle['id'],
18426 ];
18427 }
18428 }
18429
18430 return [
18431 'options' => $options,
18432 'hasMore' => false,
18433 ];
18434 }
18435
18436 /**
18437 * Prepare LatePoint Agents List.
18438 *
18439 * @param array $data Search Params.
18440 * @return array
18441 */
18442 public function search_lp_agents_list( $data ) {
18443
18444 if ( ! class_exists( 'OsAgentHelper' ) ) {
18445 return [];
18446 }
18447
18448 $agent_ids_for_service = OsAgentHelper::get_agent_ids_for_service_and_location( $data['dynamic'] );
18449 $agents = OsAgentHelper::get_agents_list();
18450 $options = [];
18451
18452 if ( ! empty( $agents ) ) {
18453 foreach ( $agents as $key => $agent ) {
18454 if ( in_array( $agent['value'], $agent_ids_for_service ) ) {
18455 $options[] = [
18456 'label' => $agent['label'],
18457 'value' => $agent['value'],
18458 ];
18459 }
18460 }
18461 }
18462
18463 return [
18464 'options' => $options,
18465 'hasMore' => false,
18466 ];
18467 }
18468
18469 /**
18470 * Prepare LatePoint Agents WP User List.
18471 *
18472 * @param array $data Search Params.
18473 * @return array
18474 */
18475 public function search_lp_user_agents_list( $data ) {
18476
18477 if ( ! class_exists( 'OsWpUserHelper' ) ) {
18478 return [];
18479 }
18480 $agents = OsWpUserHelper::get_wp_users_for_select( [ 'role' => 'latepoint_agent' ] );
18481 $options = [];
18482 if ( ! empty( $agents ) ) {
18483 $options = $agents;
18484 }
18485 return [
18486 'options' => $options,
18487 'hasMore' => false,
18488 ];
18489 }
18490
18491 /**
18492 * Prepare LatePoint Statuses List.
18493 *
18494 * @param array $data Search Params.
18495 * @return array
18496 */
18497 public function search_lp_statuses_list( $data ) {
18498
18499 if ( ! class_exists( 'OsBookingHelper' ) ) {
18500 return [];
18501 }
18502
18503 $statuses = OsBookingHelper::get_statuses_list();
18504 $options = [];
18505
18506 if ( ! empty( $statuses ) ) {
18507 foreach ( $statuses as $key => $label ) {
18508 $options[] = [
18509 'label' => $label,
18510 'value' => $key,
18511 ];
18512 }
18513 }
18514
18515 return [
18516 'options' => $options,
18517 'hasMore' => false,
18518 ];
18519 }
18520
18521 /**
18522 * Prepare LatePoint Customers List.
18523 *
18524 * @param array $data Search Params.
18525 * @return array
18526 */
18527 public function search_lp_customers_list( $data ) {
18528
18529 if ( ! class_exists( 'OsCustomerHelper' ) ) {
18530 return [];
18531 }
18532
18533 $customers = OsCustomerHelper::get_customers_for_select();
18534 $options = [];
18535
18536 if ( ! empty( $customers ) ) {
18537 foreach ( $customers as $key => $customer ) {
18538 $options[] = [
18539 'label' => $customer['label'],
18540 'value' => $customer['value'],
18541 ];
18542 }
18543 }
18544
18545 return [
18546 'options' => $options,
18547 'hasMore' => false,
18548 ];
18549 }
18550
18551 /**
18552 * Prepare SureForms Forms List.
18553 *
18554 * @param array $data Search Params.
18555 * @return array
18556 */
18557 public function search_sureforms_form_list( $data ) {
18558
18559 $options = [];
18560
18561 $page = $data['page'];
18562 $limit = Utilities::get_search_page_limit();
18563 $offset = $limit * ( $page - 1 );
18564
18565 $forms = get_posts(
18566 [
18567 'posts_per_page' => - 1,
18568 'orderby' => 'name',
18569 'order' => 'asc',
18570 'post_type' => 'sureforms_form',
18571 'post_status' => 'publish',
18572 ]
18573 );
18574 $form_count = wp_count_posts( 'sureforms_form' )->publish;
18575
18576 if ( ! empty( $forms ) ) {
18577 if ( is_array( $forms ) ) {
18578 foreach ( $forms as $form ) {
18579 $title = html_entity_decode( get_the_title( $form->ID ), ENT_QUOTES, 'UTF-8' );
18580 $options[] = [
18581 'label' => $title,
18582 'value' => $form->ID,
18583 ];
18584 }
18585 }
18586 }
18587
18588 return [
18589 'options' => $options,
18590 'hasMore' => $form_count > $limit && $form_count > $offset,
18591 ];
18592 }
18593
18594 /**
18595 * Prepare Academy Course List.
18596 *
18597 * @param array $data Search Params.
18598 * @return array
18599 */
18600 public function search_ac_lms_courses( $data ) {
18601
18602 $options = [];
18603
18604 $page = $data['page'];
18605 $limit = Utilities::get_search_page_limit();
18606 $offset = $limit * ( $page - 1 );
18607
18608 $courses = get_posts(
18609 [
18610 'posts_per_page' => - 1,
18611 'orderby' => 'name',
18612 'order' => 'asc',
18613 'post_type' => 'academy_courses',
18614 'post_status' => 'publish',
18615 ]
18616 );
18617 $course_count = wp_count_posts( 'academy_courses' )->publish;
18618
18619 if ( ! empty( $courses ) ) {
18620 if ( is_array( $courses ) ) {
18621 foreach ( $courses as $course ) {
18622 $options[] = [
18623 'label' => get_the_title( $course->ID ),
18624 'value' => $course->ID,
18625 ];
18626 }
18627 }
18628 }
18629
18630 return [
18631 'options' => $options,
18632 'hasMore' => $course_count > $limit && $course_count > $offset,
18633 ];
18634 }
18635
18636 /**
18637 * Prepare Academy Lesson List.
18638 *
18639 * @param array $data Search Params.
18640 * @return array
18641 */
18642 public function search_ac_lms_lessons( $data ) {
18643
18644 $options = [];
18645
18646 if ( ! class_exists( '\Academy\Helper' ) ) {
18647 return [];
18648 }
18649
18650 $curriculums = \Academy\Helper::get_course_curriculum( $data['dynamic'] );
18651
18652 if ( ! empty( $curriculums ) ) {
18653 foreach ( $curriculums as $topic ) {
18654 if ( isset( $topic['topics'] ) && is_array( $topic['topics'] ) ) {
18655 foreach ( $topic['topics'] as $lesson ) {
18656 if ( isset( $lesson['type'] ) && 'lesson' === $lesson['type'] ) {
18657 $options[] = [
18658 'label' => $lesson['name'],
18659 'value' => $lesson['id'],
18660 ];
18661 }
18662 }
18663 }
18664 }
18665 }
18666
18667 return [
18668 'options' => $options,
18669 'hasMore' => false,
18670 ];
18671 }
18672
18673 /**
18674 * Prepare Academy Quiz List.
18675 *
18676 * @param array $data Search Params.
18677 * @return array
18678 */
18679 public function search_ac_lms_quiz( $data ) {
18680
18681 $options = [];
18682
18683 $page = $data['page'];
18684 $limit = Utilities::get_search_page_limit();
18685 $offset = $limit * ( $page - 1 );
18686
18687 $quizs = get_posts(
18688 [
18689 'posts_per_page' => - 1,
18690 'orderby' => 'name',
18691 'order' => 'asc',
18692 'post_type' => 'academy_quiz',
18693 'post_status' => 'publish',
18694 ]
18695 );
18696 $quiz_count = wp_count_posts( 'academy_quiz' )->publish;
18697
18698 if ( ! empty( $quizs ) ) {
18699 if ( is_array( $quizs ) ) {
18700 foreach ( $quizs as $quiz ) {
18701 $options[] = [
18702 'label' => get_the_title( $quiz->ID ),
18703 'value' => $quiz->ID,
18704 ];
18705 }
18706 }
18707 }
18708
18709 return [
18710 'options' => $options,
18711 'hasMore' => $quiz_count > $limit && $quiz_count > $offset,
18712 ];
18713 }
18714
18715 /**
18716 * Search Academy LMS data.
18717 *
18718 * @param array $data data.
18719 * @return array|void
18720 */
18721 public function search_ac_lms_last_data( $data ) {
18722 global $wpdb;
18723 $post_type = $data['post_type'];
18724 $trigger = $data['search_term'];
18725 $context = [];
18726
18727 if ( ! class_exists( '\Academy\Helper' ) ) {
18728 return [];
18729 }
18730
18731 $course_id = -1;
18732 $lesson_id = -1;
18733 $quiz_id = -1;
18734 if ( 'ac_lms_course_completed' === $trigger ) {
18735 $course_id = $data['filter']['course']['value'];
18736 } elseif ( 'ac_lms_lesson_completed' === $trigger ) {
18737 $course_id = $data['filter']['course']['value'];
18738 $lesson_id = $data['filter']['lesson']['value'];
18739 } elseif ( 'ac_lms_quiz_completed' === $trigger || 'ac_lms_quiz_failed' === $trigger ) {
18740 $quiz_id = $data['filter']['quiz']['value'];
18741 } elseif ( 'ac_lms_enrolled_course' === $trigger ) {
18742 $course_id = $data['filter']['course']['value'];
18743 }
18744
18745 $users = get_users(
18746 [
18747 'fields' => 'ID',
18748 'meta_key' => 'is_academy_student',
18749 ]
18750 );
18751 if ( ! empty( $users ) ) {
18752 $user_random_key = array_rand( $users );
18753 $user_id = $users[ $user_random_key ];
18754 }
18755
18756 if ( 'ac_lms_course_completed' === $trigger ) {
18757 if ( -1 === $course_id ) {
18758 $result = $wpdb->get_results(
18759 $wpdb->prepare(
18760 "SELECT * FROM {$wpdb->prefix}comments
18761 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",
18762 $post_type
18763 )
18764 );
18765 } else {
18766 $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 ) );
18767 }
18768 } elseif ( 'ac_lms_lesson_completed' === $trigger ) {
18769 if ( -1 === $course_id ) {
18770 $courses = get_posts(
18771 [
18772 'posts_per_page' => - 1,
18773 'post_type' => 'academy_courses',
18774 'post_status' => 'publish',
18775 'fields' => 'ids',
18776 ]
18777 );
18778 $course_id = array_rand( $courses );
18779 $option_name = 'academy_course_' . $course_id . '_completed_topics';
18780 } else {
18781 $option_name = 'academy_course_' . $course_id . '_completed_topics';
18782 }
18783 if ( ! empty( $users ) ) {
18784 $meta = get_user_meta( $user_id, $option_name, true );
18785 if ( is_string( $meta ) ) {
18786 $saved_topics_lists = (array) json_decode( $meta, true );
18787 if ( -1 === $lesson_id ) {
18788 $result = $saved_topics_lists['lesson'];
18789 } else {
18790 if ( is_array( $saved_topics_lists['lesson'] ) ) {
18791 if ( array_key_exists( $lesson_id, $saved_topics_lists['lesson'] ) ) {
18792 $result = $saved_topics_lists['lesson'];
18793 }
18794 }
18795 }
18796 }
18797 }
18798 } elseif ( 'ac_lms_quiz_completed' === $trigger ) {
18799 if ( -1 === $quiz_id ) {
18800 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE attempt_status='passed' order by attempt_id DESC LIMIT 1" );
18801 } else {
18802 $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 ) );
18803 }
18804 } elseif ( 'ac_lms_quiz_failed' === $trigger ) {
18805 if ( -1 === $quiz_id ) {
18806 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE attempt_status='failed' order by attempt_id DESC LIMIT 1" );
18807 } else {
18808 $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 ) );
18809 }
18810 } elseif ( 'ac_lms_enrolled_course' === $trigger ) {
18811 if ( -1 === $course_id ) {
18812 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type = %s order by ID DESC LIMIT 1", 'academy_enrolled' ) );
18813 } else {
18814 $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 ) );
18815 }
18816 }
18817
18818 if ( ! empty( $result ) ) {
18819 switch ( $trigger ) {
18820 case 'ac_lms_course_completed':
18821 $data = WordPress::get_post_context( $result[0]->comment_post_ID );
18822 $context_data = WordPress::get_user_context( $result[0]->user_id );
18823 $context_data['course_data'] = $data;
18824 $context_data['course'] = $result[0]->comment_post_ID;
18825 break;
18826 case 'ac_lms_enrolled_course':
18827 $data = WordPress::get_post_context( $result[0]->post_parent );
18828 $context_data = WordPress::get_user_context( $result[0]->post_author );
18829 $context_data['course_data'] = $data;
18830 $context_data['enrollment_data'] = $result[0];
18831 $context_data['course'] = $result[0]->post_parent;
18832 break;
18833 case 'ac_lms_lesson_completed':
18834 if ( -1 === $lesson_id ) {
18835 $key = array_rand( $result );
18836 $lesson_data = \Academy\Helper::get_lesson( $key );
18837 } else {
18838 $lesson_data = \Academy\Helper::get_lesson( $lesson_id );
18839 }
18840 if ( is_object( $lesson_data ) ) {
18841 $lesson_data = get_object_vars( $lesson_data );
18842 }
18843 if ( ! empty( $users ) ) {
18844 $context_data = array_merge( $lesson_data, WordPress::get_user_context( $user_id ) );
18845 }
18846 $context_data['course_data'] = WordPress::get_post_context( $course_id );
18847 $context_data['lesson'] = $lesson_id;
18848 $context_data['course'] = $course_id;
18849 break;
18850 case 'ac_lms_quiz_completed':
18851 case 'ac_lms_quiz_failed':
18852 $context_data = WordPress::get_user_context( $result[0]->user_id );
18853 $context_data['quiz_data'] = WordPress::get_post_context( $result[0]->quiz_id );
18854 $context_data['quiz_attempt_details'] = $result;
18855 $context_data['quiz'] = $result[0]->quiz_id;
18856 break;
18857 default:
18858 return;
18859 }
18860 $context['pluggable_data'] = $context_data;
18861 $context['response_type'] = 'live';
18862 } elseif ( empty( $result ) ) {
18863 switch ( $trigger ) {
18864 case 'ac_lms_course_completed':
18865 $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"}';
18866 break;
18867 case 'ac_lms_lesson_completed':
18868 $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"}';
18869 break;
18870 case 'ac_lms_quiz_completed':
18871 $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"}';
18872 break;
18873 case 'ac_lms_quiz_failed':
18874 $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"}';
18875 break;
18876 case 'ac_lms_enrolled_course':
18877 $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"}';
18878 break;
18879 default:
18880 return;
18881 }
18882 $context = (array) json_decode( $sample_data, true );
18883 }
18884 return $context;
18885 }
18886
18887 /**
18888 * Search myCred Point Type List.
18889 *
18890 * @param array $data Search Params.
18891 * @return array
18892 */
18893 public function search_mycred_point_type_list( $data ) {
18894
18895 $options = [];
18896
18897 if ( ! function_exists( 'mycred_get_types' ) ) {
18898 return [];
18899 }
18900
18901 $posts = mycred_get_types();
18902
18903 if ( ! empty( $posts ) ) {
18904 if ( is_array( $posts ) ) {
18905 foreach ( $posts as $key => $post ) {
18906 $options[] = [
18907 'label' => $post,
18908 'value' => $key,
18909 ];
18910 }
18911 }
18912 }
18913
18914 return [
18915 'options' => $options,
18916 'hasMore' => false,
18917 ];
18918 }
18919
18920 /**
18921 * Prepare elementor forms.
18922 *
18923 * @param array $data Search Params.
18924 *
18925 * @return array
18926 */
18927 public function search_new_elementor_form_fields( $data ) {
18928
18929 $fields = [];
18930 $select_form_id = $data['dynamic'];
18931 global $wpdb;
18932 $post_metas = $wpdb->get_results(
18933 $wpdb->prepare(
18934 "SELECT pm.post_id, pm.meta_value
18935 FROM $wpdb->postmeta pm
18936 LEFT JOIN $wpdb->posts p
18937 ON p.ID = pm.post_id
18938 WHERE p.post_type IS NOT NULL
18939 AND p.post_status = %s
18940 AND pm.meta_key = %s
18941 AND pm.`meta_value` LIKE %s",
18942 'publish',
18943 '_elementor_data',
18944 '%%form_fields%%'
18945 )
18946 );
18947
18948 if ( ! empty( $post_metas ) ) {
18949 foreach ( $post_metas as $post_meta ) {
18950 /**
18951 *
18952 * Ignore line
18953 *
18954 * @phpstan-ignore-next-line
18955 */
18956 $inner_forms = Utilities::search_elementor_forms( json_decode( $post_meta->meta_value ) );
18957 if ( ! empty( $inner_forms ) ) {
18958 foreach ( $inner_forms as $form ) {
18959 $form_id = explode( '_', $select_form_id );
18960 if ( is_object( $form ) ) {
18961 if ( $form->id == $form_id[1] ) {
18962 if ( ! empty( $form->settings->form_fields ) ) {
18963 foreach ( $form->settings->form_fields as $field ) {
18964 $fields[] = [
18965 'value' => $field->custom_id,
18966 'text' => ! empty( $field->field_label ) ? $field->field_label : 'unknown',
18967 ];
18968 }
18969 }
18970 }
18971 }
18972 }
18973 }
18974 }
18975 }
18976 $options = [];
18977 if ( ! empty( $fields ) ) {
18978 foreach ( $fields as $key => $value ) {
18979 $options[] = [
18980 'label' => $value['text'],
18981 'value' => $value['value'],
18982 ];
18983 }
18984 }
18985 return [
18986 'options' => $options,
18987 'hasMore' => false,
18988 ];
18989 }
18990 /**
18991 * Get Fluent Booking Appointment Events.
18992 *
18993 * @param array $data data.
18994 *
18995 * @return array
18996 */
18997 public function search_fluent_booking_calendars_list( $data ) {
18998
18999 global $wpdb;
19000
19001 $page = $data['page'];
19002 $limit = Utilities::get_search_page_limit();
19003 $offset = $limit * ( $page - 1 );
19004
19005 $calendars = $wpdb->get_results(
19006 $wpdb->prepare(
19007 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendars WHERE status = %s ORDER BY id DESC LIMIT %d OFFSET %d",
19008 [ 'active', $limit, $offset ]
19009 ),
19010 OBJECT
19011 );
19012
19013 $calendars_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
19014
19015 $options = [];
19016 if ( ! empty( $calendars ) ) {
19017 foreach ( $calendars as $calendar ) {
19018 $options[] = [
19019 'label' => $calendar->title,
19020 'value' => $calendar->id,
19021 ];
19022 }
19023 }
19024 return [
19025 'options' => $options,
19026 'hasMore' => $calendars_count > $limit && $calendars_count > $offset,
19027 ];
19028
19029 }
19030
19031 /**
19032 * Get Fluent Booking Appointment Events.
19033 *
19034 * @param array $data data.
19035 *
19036 * @return array
19037 */
19038 public function search_fluent_booking_events_list( $data ) {
19039
19040 global $wpdb;
19041 $page = $data['page'];
19042 $limit = Utilities::get_search_page_limit();
19043 $offset = $limit * ( $page - 1 );
19044 $calendar_id = sanitize_text_field( $data['dynamic']['calender_id'] );
19045 if ( '-1' === $calendar_id ) {
19046 $events = $wpdb->get_results(
19047 $wpdb->prepare(
19048 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendar_events WHERE status = %s ORDER BY id DESC LIMIT %d OFFSET %d",
19049 [ 'active', $limit, $offset ]
19050 ),
19051 OBJECT
19052 );
19053 } else {
19054 $events = $wpdb->get_results(
19055 $wpdb->prepare(
19056 "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",
19057 [ 'active', $calendar_id, $limit, $offset ]
19058 ),
19059 OBJECT
19060 );
19061 }
19062
19063 $events_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
19064
19065 $options = [];
19066 if ( ! empty( $events ) ) {
19067 foreach ( $events as $event ) {
19068 $options[] = [
19069 'label' => $event->title,
19070 'value' => $event->id,
19071 ];
19072 }
19073 }
19074 return [
19075 'options' => $options,
19076 'hasMore' => $events_count > $limit && $events_count > $offset,
19077 ];
19078
19079 }
19080
19081 /**
19082 * Get Fluent Booking last data.
19083 *
19084 * @param array $data data.
19085 *
19086 * @return array
19087 */
19088 public function search_fluent_booking_last_data( $data ) {
19089 global $wpdb;
19090 $trigger = $data['search_term'];
19091 $course_data = [];
19092 $lesson_data = [];
19093
19094 $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
19095 $context = [
19096 'pluggable_data' => json_decode( $sample_data, true ),
19097 'response_type' => 'sample',
19098 ];
19099 if ( 'fluent_booking_appointment_cancelled' === $trigger ) {
19100 $status = 'cancelled';
19101 } elseif ( 'fluent_booking_appointment_completed' === $trigger ) {
19102 $status = 'completed';
19103 }
19104
19105 $event_id = (int) $data['filter']['event_id']['value'];
19106 if ( 'fluent_booking_appointment_cancelled' === $trigger || 'fluent_booking_appointment_completed' === $trigger ) {
19107 if ( $event_id > 0 ) {
19108 $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 ) );
19109 } else {
19110 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE status= %s ORDER BY id DESC LIMIT 1", $status ) );
19111 }
19112 } elseif ( 'fluent_booking_new_appointment_booked' === $trigger ) {
19113 if ( $event_id > 0 ) {
19114 $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' ) );
19115 } else {
19116 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE status != %s ORDER BY id DESC LIMIT 1", 'cancelled' ) );
19117 }
19118 }
19119
19120 if ( ! empty( $booking ) ) {
19121 $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 ) );
19122
19123 $booking_event = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_calendar_events WHERE id= %d", $booking->event_id ) );
19124 $booking_data = $booking;
19125 $booking_data->custom_fields = $booking_meta->value;
19126 $booking_array = [
19127 'booking' => $booking_data,
19128 'event' => $booking_event,
19129 ];
19130
19131
19132 $booking_array_response = self::recursive_unserialize( $booking_array );
19133 $context['pluggable_data'] = $booking_array_response;
19134 $context['response_type'] = 'live';
19135 }
19136 return $context;
19137 }
19138
19139 /**
19140 * Recursive unserilize.
19141 *
19142 * @param array $data data.
19143 *
19144 * @return array|mixed
19145 */
19146 public static function recursive_unserialize( $data ) {
19147 if ( is_array( $data ) ) {
19148 foreach ( $data as $key => $value ) {
19149 $data[ $key ] = self::recursive_unserialize( $value );
19150 }
19151 return $data;
19152 } elseif ( is_object( $data ) && 'stdClass' === get_class( $data ) ) {
19153 foreach ( $data as $property => $value ) {
19154 $data->$property = self::recursive_unserialize( $value );
19155 }
19156 return $data;
19157 } elseif ( is_string( $data ) && self::is_serialized( strval( $data ) ) ) {
19158 return unserialize( $data );
19159 } else {
19160 return $data;
19161 }
19162
19163 }
19164
19165 /**
19166 * Check if string serialized.
19167 *
19168 * @param string $data data.
19169 *
19170 * @return bool
19171 */
19172 public static function is_serialized( $data ) {
19173 $unserialized = unserialize( $data );
19174 if ( 'b:0;' === $data || false !== $unserialized ) {
19175 return true;
19176 } else {
19177 return false;
19178 }
19179 }
19180
19181 /**
19182 * Search Tutor LMS data.
19183 *
19184 * @param array $data data.
19185 * @return array|void|mixed
19186 */
19187 public function search_tutor_lms_last_data( $data ) {
19188 global $wpdb;
19189 $post_type = $data['post_type'];
19190 $trigger = $data['search_term'];
19191 $context = [];
19192
19193 if ( ! function_exists( 'tutor_utils' ) ) {
19194 return [];
19195 }
19196
19197 $post_id = -1;
19198 if ( 'course_enrolled' === $trigger || 'tutor_courses_question' === $trigger ) {
19199 $post_id = $data['filter']['tutor_course']['value'];
19200 } elseif ( 'quiz_attempt_percentage' === $trigger || 'quiz_failed' === $trigger || 'quiz_passed' === $trigger ) {
19201 $post_id = $data['filter']['quiz_id']['value'];
19202 }
19203
19204 if ( 'course_enrolled' === $trigger ) {
19205 if ( -1 === $post_id ) {
19206 $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 ) );
19207 } else {
19208 $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 ) );
19209 }
19210 } elseif ( 'tutor_courses_question' === $trigger ) {
19211 if ( -1 === $post_id ) {
19212 $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 ) );
19213 } else {
19214 $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 ) );
19215 }
19216 } elseif ( 'quiz_passed' === $trigger ) {
19217 if ( -1 === $post_id ) {
19218 $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 ) );
19219 } else {
19220 $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 ) );
19221 }
19222 } elseif ( 'quiz_failed' === $trigger ) {
19223 if ( -1 === $post_id ) {
19224 $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 ) );
19225 } else {
19226 $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 ) );
19227 }
19228 } elseif ( 'quiz_attempt_percentage' == $trigger ) {
19229 $condition_compare = $data['filter']['condition_compare']['value'];
19230 $percentage = $data['filter']['percentage']['value'];
19231 if ( -1 === $post_id ) {
19232 $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
19233 } else {
19234 $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
19235 }
19236 }
19237
19238 if ( ! empty( $result ) ) {
19239 switch ( $trigger ) {
19240 case 'course_enrolled':
19241 $result_item_id = $result[0]->post_parent;
19242 $result_user_id = $result[0]->post_author;
19243 $context_data = array_merge(
19244 WordPress::get_user_context( $result_user_id ),
19245 WordPress::get_post_context( $result_item_id )
19246 );
19247 $context_data['tutor_course'] = $result_item_id;
19248 break;
19249 case 'tutor_courses_question':
19250 $date = $result[0]->comment_date;
19251 $data = [
19252 'comment_post_ID' => $result[0]->comment_post_ID,
19253 'comment_author' => $result[0]->comment_author,
19254 'comment_date' => $date,
19255 'comment_date_gmt' => get_gmt_from_date( $date ),
19256 'comment_content' => $result[0]->comment_content,
19257 'comment_approved' => 'approved',
19258 'comment_agent' => 'TutorLMSPlugin',
19259 'comment_type' => 'tutor_q_and_a',
19260 'comment_parent' => $result[0]->comment_parent,
19261 'user_id' => $result[0]->user_id,
19262 ];
19263 $context_data['tutor_course'] = $result[0]->comment_post_ID;
19264 $context_data['data'] = $data;
19265 break;
19266 case 'quiz_attempt_percentage':
19267 case 'quiz_failed':
19268 case 'quiz_passed':
19269 $attempt = tutor_utils()->get_attempt( $result[0]->attempt_id );
19270 $context_data = WordPress::get_user_context( $result[0]->user_id );
19271 $context_data['quiz_id'] = $attempt->quiz_id;
19272 $context_data['attempt_id'] = $result[0]->attempt_id;
19273 $context_data['attempt'] = $attempt;
19274 break;
19275 default:
19276 return;
19277 }
19278 if ( ! empty( $context_data ) ) {
19279 $context['pluggable_data'] = $context_data;
19280 $context['response_type'] = 'live';
19281 }
19282 } elseif ( empty( $result ) ) {
19283 switch ( $trigger ) {
19284 case 'course_enrolled':
19285 $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"} ';
19286 break;
19287 case 'tutor_courses_question':
19288 $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"}';
19289 break;
19290 case 'quiz_attempt_percentage':
19291 case 'quiz_failed':
19292 case 'quiz_passed':
19293 $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"}';
19294 break;
19295 default:
19296 return;
19297 }
19298 $context = json_decode( $sample_data, true );
19299 }
19300
19301 return $context;
19302 }
19303
19304 /**
19305 * Get Asgorus Forum list
19306 *
19307 * @param array $data data.
19308 *
19309 * @return array
19310 */
19311 public function search_asgorus_forums_list( $data ) {
19312 if ( ! class_exists( 'AsgarosForum' ) ) {
19313 return [];
19314 }
19315 $category = $data['dynamic'];
19316 if ( is_array( $category ) ) {
19317 $category_id = $category['forum_category'];
19318 } else {
19319 $category_id = $category;
19320 }
19321 $asgaros_forum = new AsgarosForum();
19322 $forums = $asgaros_forum->get_forums( $category_id );
19323 $options = [];
19324 if ( ! empty( $forums ) ) {
19325 foreach ( $forums as $forum ) {
19326 $options[] = [
19327 'label' => $forum->name,
19328 'value' => $forum->id,
19329 ];
19330 }
19331 }
19332 return [
19333 'options' => $options,
19334 'hasMore' => false,
19335 ];
19336 }
19337
19338 /**
19339 * Get Asgorus Categories list
19340 *
19341 * @param array $data data.
19342 *
19343 * @return array
19344 */
19345 public function search_asgorus_categories_list( $data ) {
19346 if ( ! class_exists( 'AsgarosForum' ) ) {
19347 return [];
19348 }
19349 $asgaros_forum = new AsgarosForum();
19350 $categories = (array) $asgaros_forum->content->get_categories();
19351 $options = [];
19352
19353 if ( ! empty( $categories ) ) {
19354 foreach ( $categories as $category ) {
19355 $options[] = [
19356 'label' => $category->name,
19357 'value' => $category->term_id,
19358 ];
19359 }
19360 }
19361 return [
19362 'options' => $options,
19363 'hasMore' => false,
19364 ];
19365 }
19366
19367 /**
19368 * Get Asgorus Topic lists
19369 *
19370 * @param array $data data.
19371 *
19372 * @return array
19373 */
19374 public function search_asgorus_topic_list( $data ) {
19375 if ( ! class_exists( 'AsgarosForum' ) ) {
19376 return [];
19377 }
19378 global $wpdb;
19379 $forum_id = $data['dynamic'];
19380 $asgaros_forum = new AsgarosForum();
19381 $sql = 'SELECT name,id FROM ' . $wpdb->prefix . 'forum_topics WHERE parent_id = %d AND closed = 0 ORDER BY id';
19382 $topics = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id ), ARRAY_A );// @phpcs:ignore
19383 $options = [];
19384
19385 if ( ! empty( $topics ) ) {
19386 foreach ( $topics as $topic ) {
19387 $options[] = [
19388 'label' => $topic['name'],
19389 'value' => $topic['id'],
19390 ];
19391 }
19392 }
19393 return [
19394 'options' => $options,
19395 'hasMore' => false,
19396 ];
19397 }
19398
19399 /**
19400 * Get Asgorus Topic Last Data
19401 *
19402 * @param array $data data.
19403 *
19404 * @return array
19405 */
19406 public function search_pluggables_asgaros_topic_last_data( $data ) {
19407 if ( ! class_exists( 'AsgarosForum' ) ) {
19408 return [];
19409 }
19410 $asgaros_forum = new AsgarosForum();
19411 $context = [];
19412 global $wpdb;
19413 $forum_id = $data['filter']['forum_id']['value'];
19414
19415 if ( -1 == $forum_id ) {
19416 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'forum_topics WHERE closed = 0 ORDER BY id DESC LIMIT 1', ARRAY_A );
19417 } else {
19418 $forum = $forum_id;
19419 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'forum_topics WHERE parent_id = %d AND closed = 0 ORDER BY id DESC LIMIT 1';
19420 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum ), ARRAY_A );// @phpcs:ignore
19421 }
19422
19423 if ( ! empty( $results ) ) {
19424 $sql_post = 'SELECT * FROM ' . $wpdb->prefix . 'forum_posts WHERE parent_id = %d ORDER BY id DESC LIMIT 1';
19425 $results_post = $wpdb->get_results( $wpdb->prepare( $sql_post, $results[0]['id'] ), ARRAY_A ); // @phpcs:ignore
19426 $context['pluggable_data']['forum_id'] = $results[0]['parent_id'];
19427 $context['pluggable_data']['topic_id'] = $results[0]['id'];
19428 $context['pluggable_data']['post_id'] = $results_post[0]['id'];
19429 $context['pluggable_data']['forum'] = $asgaros_forum->content->get_forum( $results[0]['parent_id'] );
19430 $context['pluggable_data']['topic'] = $asgaros_forum->content->get_topic( $results[0]['id'] );
19431 $context['pluggable_data']['post'] = $asgaros_forum->content->get_post( $results_post[0]['id'] );
19432
19433 $context['pluggable_data']['author'] = WordPress::get_user_context( $results[0]['author_id'] );
19434 $context['response_type'] = 'live';
19435 } else {
19436 $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
19437 }
19438 return $context;
19439 }
19440
19441 /**
19442 * Get Asgorus Topic Reply Last Data
19443 *
19444 * @param array $data data.
19445 *
19446 * @return array
19447 */
19448 public function search_pluggables_asgaros_reply_last_data( $data ) {
19449 if ( ! class_exists( 'AsgarosForum' ) ) {
19450 return [];
19451 }
19452 $asgaros_forum = new AsgarosForum();
19453 $context = [];
19454 global $wpdb;
19455 $forum_id = $data['filter']['forum_id']['value'];
19456 $topic_id = $data['filter']['forum_id']['value'];
19457 if ( -1 == $topic_id ) {
19458 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'forum_posts ORDER BY id DESC LIMIT 1', ARRAY_A );
19459 } else {
19460 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'forum_posts WHERE parent_id = %d ORDER BY id DESC LIMIT 1';
19461 $results = $wpdb->get_results( $wpdb->prepare( $sql, $topic_id ), ARRAY_A );// @phpcs:ignore
19462 }
19463
19464 if ( ! empty( $results ) ) {
19465 $topic_id = $results[0]['parent_id'];
19466 $post_id = $results[0]['id'];
19467 $context['pluggable_data']['forum_id'] = $forum_id;
19468 $context['pluggable_data']['topic_id'] = $topic_id;
19469 $context['pluggable_data']['post_id'] = $post_id;
19470 $context['pluggable_data']['forum'] = $asgaros_forum->content->get_forum( $forum_id );
19471 $context['pluggable_data']['topic'] = $asgaros_forum->content->get_topic( $topic_id );
19472 $context['pluggable_data']['post'] = $asgaros_forum->content->get_post( $post_id );
19473
19474 $context['pluggable_data']['author'] = WordPress::get_user_context( $results[0]['author_id'] );
19475 $context['response_type'] = 'live';
19476 } else {
19477 $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
19478 }
19479 return $context;
19480 }
19481
19482 /**
19483 * Get WPLoyalty Points Awarded Last Data
19484 *
19485 * @param array $data data.
19486 *
19487 * @return array|mixed|string
19488 */
19489 public function search_wp_loyalty_points_awarded_customer( $data ) {
19490
19491 $context = [];
19492 global $wpdb;
19493 if ( ! class_exists( 'Wlr\App\Helpers\Base' ) ) {
19494 return [];
19495 }
19496 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wlr_earn_campaign_transaction WHERE transaction_type = %s ORDER BY id DESC LIMIT 1';
19497 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'credit' ), ARRAY_A );// @phpcs:ignore
19498
19499 if ( ! empty( $results ) ) {
19500 $context['pluggable_data']['user_email'] = $results[0]['user_email'];
19501 $context['pluggable_data']['points_earned'] = $results[0]['points'];
19502 $context['pluggable_data']['action_type'] = $results[0]['action_type'];
19503 $base_helper = new \Wlr\App\Helpers\Base();
19504 $user = $base_helper->getPointUserByEmail( $results[0]['user_email'] );
19505 $points_sql = 'SELECT * FROM ' . $wpdb->prefix . 'wlr_expire_points
19506 WHERE user_email = %s ORDER BY id DESC LIMIT 1';
19507 $points_results = $wpdb->get_results( $wpdb->prepare( $points_sql, $results[0]['user_email'] ), ARRAY_A );// @phpcs:ignore
19508 $context['pluggable_data']['user'] = $user;
19509 if ( ! empty( $points_results ) ) {
19510 $expire_date = $points_results[0]['expire_date'];
19511 $timestamp = is_numeric( $expire_date ) ? (int) $expire_date : null;
19512 $date_format = get_option( 'date_format' );
19513 if ( is_string( $date_format ) ) {
19514 $context['pluggable_data']['point_expiry_date'] = wp_date( $date_format, $timestamp );
19515 }
19516 }
19517 $context['response_type'] = 'live';
19518 } else {
19519 $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
19520 }
19521 return $context;
19522 }
19523
19524 /**
19525 * Get WPLoyalty Campaign Type List
19526 *
19527 * @param array $data data.
19528 *
19529 * @return array
19530 */
19531 public function search_wp_loyalty_action_type_list( $data ) {
19532 $options = [];
19533 if ( ! class_exists( 'Wlr\App\Helpers\Woocommerce' ) ) {
19534 return [];
19535 }
19536
19537 $woocommerce_helper = new \Wlr\App\Helpers\Woocommerce();
19538 $action_types = $woocommerce_helper->getActionTypes();
19539
19540 if ( ! empty( $action_types ) ) {
19541 foreach ( $action_types as $key => $type ) {
19542 $options[] = [
19543 'label' => $type,
19544 'value' => $key,
19545 ];
19546 }
19547 }
19548 return [
19549 'options' => $options,
19550 'hasMore' => false,
19551 ];
19552 }
19553
19554 /**
19555 * Get last data for trigger.
19556 *
19557 * @param array $data data.
19558 * @return array
19559 */
19560 public function search_slicewp_last_data( $data ) {
19561 global $wpdb;
19562
19563 $context = [];
19564 $context['response_type'] = 'sample';
19565
19566 $user_data = WordPress::get_sample_user_context();
19567
19568
19569 if ( ! function_exists( 'slicewp_get_affiliate' ) ) {
19570 return [];
19571 }
19572
19573 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
19574
19575 if ( in_array( $term, [ 'slicewp_new_affiliate', 'slicewp_update_affiliate' ], true ) ) {
19576 $affiliate = [
19577 'id' => 14,
19578 'user_id' => 25,
19579 'date_created' => '2024-03-14 12:35:50',
19580 'date_modified' => '2024-03-14 12:36:20',
19581 'payment_email' => 'testcustomer12@gmail.com',
19582 'website' => '',
19583 'status' => 'active',
19584 'parent_id' => 0,
19585 ];
19586 $context['pluggable_data'] = array_merge( $affiliate, [ 'user' => $user_data ] );
19587
19588 if ( ( ! empty( $data['filter'] ) && '-1' === $data['filter']['affiliate_id']['value'] ) || empty( $data['filter'] ) ) {
19589 $query = $wpdb->prepare(
19590 "
19591 SELECT *
19592 FROM {$wpdb->prefix}slicewp_affiliates
19593 WHERE status = %s
19594 ORDER BY id DESC
19595 LIMIT 1",
19596 'active'
19597 );
19598 $affiliate_results = $wpdb->get_row( $query ); //phpcs:ignore
19599
19600 } else {
19601 $affiliate_id = $data['filter']['affiliate_id']['value'];
19602 $query = $wpdb->prepare(
19603 "
19604 SELECT *
19605 FROM {$wpdb->prefix}slicewp_affiliates
19606 WHERE status = %s AND id = %d
19607 ORDER BY id DESC
19608 LIMIT 1",
19609 'active',
19610 $affiliate_id
19611 );
19612 $affiliate_results = $wpdb->get_row( $query ); //phpcs:ignore
19613
19614 }
19615
19616
19617
19618
19619 if ( ! empty( $affiliate_results ) ) {
19620 $context['pluggable_data'] = (array) $affiliate_results;
19621
19622 $user_data = WordPress::get_user_context( $affiliate_results->user_id );
19623 $context['pluggable_data']['user'] = $user_data;
19624 $context['response_type'] = 'live';
19625 }
19626 } elseif ( in_array( $term, [ 'slicewp_new_commission', 'slicewp_update_commission' ], true ) ) {
19627 $commission = [
19628 'id' => 14,
19629 'user_id' => 25,
19630 'date_created' => '2024-03-14 12:35:50',
19631 'date_modified' => '2024-03-14 12:36:20',
19632 'payment_email' => 'testcustomer12@gmail.com',
19633 'website' => '',
19634 'status' => 'active',
19635 'parent_id' => 0,
19636 ];
19637 $context['pluggable_data'] = array_merge( $commission, [ 'user' => $user_data ] );
19638 $affiliate_id = $data['filter']['affiliate_id']['value'];
19639 if ( -1 === $data['filter']['commission_id']['value'] ) {
19640 $query = $wpdb->prepare(
19641 "
19642 SELECT *
19643 FROM {$wpdb->prefix}slicewp_commissions WHERE affiliate_id=%d ORDER BY id DESC limit 1",
19644 $affiliate_id
19645 );
19646 $commission_results = $wpdb->get_row( $query ); //phpcs:ignore
19647 } else {
19648 $commission_id = $data['filter']['commission_id']['value'];
19649 $query = $wpdb->prepare(
19650 "
19651 SELECT *
19652 FROM {$wpdb->prefix}slicewp_commissions WHERE id= %d AND affiliate_id=%d ORDER BY id DESC limit 1",
19653 $commission_id,
19654 $affiliate_id
19655 );
19656 $commission_results = $wpdb->get_row( $query ); //phpcs:ignore
19657 }
19658
19659
19660
19661
19662 if ( ! empty( $commission_results ) ) {
19663 $context['pluggable_data'] = (array) $commission_results;
19664 $affiliate = slicewp_get_affiliate( $commission_results->affiliate_id );
19665 $user_id = $affiliate->get( 'user_id' );
19666 $user_data = WordPress::get_user_context( $user_id );
19667 $context['pluggable_data']['user'] = $user_data;
19668 $context['response_type'] = 'live';
19669 }
19670 }
19671
19672 return $context;
19673 }
19674
19675
19676 /**
19677 * Get Ninja Tables Last Data
19678 *
19679 * @param array $data data.
19680 *
19681 * @return array
19682 */
19683 public function search_ninja_tables_last_data( $data ) {
19684 $context = [];
19685 global $wpdb;
19686 $table_id = isset( $data['filter']['table_id']['value'] ) ? $data['filter']['table_id']['value'] : -1;
19687
19688 if ( -1 == $table_id ) {
19689 $results = $wpdb->get_row( 'SELECT * FROM ' . $wpdb->prefix . 'ninja_table_items ORDER BY id DESC LIMIT 1', ARRAY_A );
19690 } else {
19691 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'ninja_table_items WHERE table_id = %d ORDER BY id DESC LIMIT 1';
19692 $results = $wpdb->get_row( $wpdb->prepare( $sql, $table_id ), ARRAY_A );// @phpcs:ignore
19693 }
19694
19695 if ( ! empty( $results ) ) {
19696 $results['value'] = json_decode( $results['value'], true );
19697 $context['pluggable_data'] = $results;
19698 $context['pluggable_data']['owner'] = WordPress::get_user_context( $results['owner_id'] );
19699 $context['response_type'] = 'live';
19700 } else {
19701 $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 );
19702 }
19703 return $context;
19704 }
19705
19706 /**
19707 * Get Ninja Tables Fields
19708 *
19709 * @param array $data data.
19710 *
19711 * @return array
19712 */
19713 public function search_ninja_table_fields( $data ) {
19714
19715 $context = [];
19716 global $wpdb;
19717 $table_id = isset( $data['dynamic'] ) ? $data['dynamic'] : -1;
19718 $fields = [];
19719 if ( $table_id > 0 ) {
19720 $fields = get_post_meta( $table_id, '_ninja_table_columns', true );
19721 }
19722
19723 return (array) $fields;
19724 }
19725
19726 /**
19727 * Get Late Point Booking Fields
19728 *
19729 * @param array $data data.
19730 *
19731 * @return array|void
19732 */
19733 public function search_late_point_booking_fields( $data ) {
19734 if ( ! class_exists( 'OsCustomFieldsController' ) ) {
19735
19736 return;
19737 }
19738 global $wpdb;
19739 $booking_fields = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}latepoint_settings WHERE name = %s", 'custom_fields_for_booking' ) );
19740 $fields = [];
19741 if ( ! empty( $booking_fields ) ) {
19742 $fields = json_decode( $booking_fields->value, true );
19743 }
19744 return (array) $fields;
19745 }
19746
19747 /** Search Voxel fields for action.
19748 *
19749 * @param array $data data.
19750 * @return array
19751 */
19752 public function search_voxel_custom_fields( $data ) {
19753 $post_type_fields = [];
19754 $post_type = $data['post_type'];
19755 if ( ! class_exists( '\Voxel\Post_Type' ) ) {
19756 return [];
19757 }
19758 $fields = \Voxel\Post_Type::get( $post_type )->get_fields();
19759 foreach ( $fields as $key => $field ) {
19760 if ( 'step-general' == $key ) {
19761 continue;
19762 }
19763 $field_props = $field->get_props();
19764 $post_type_fields[ $key ] = $field_props;
19765 }
19766 $context['fields'] = $post_type_fields;
19767 return $context;
19768 }
19769
19770 /**
19771 * Get Voxel Post Types lists
19772 *
19773 * @param array $data data.
19774 *
19775 * @return array
19776 */
19777 public function search_voxel_posttype_list( $data ) {
19778 $options = [];
19779 $voxel_post_types = [];
19780 if ( class_exists( '\Voxel\Post_Type' ) ) {
19781 $voxel_post_types = \Voxel\Post_Type::get_voxel_types();
19782 }
19783
19784 if ( ! empty( $voxel_post_types ) ) {
19785 foreach ( $voxel_post_types as $key => $voxel_post_type ) {
19786 $post_type = get_post_type_object( $key );
19787 if ( $post_type ) {
19788 if ( in_array( $post_type->name, [ 'collection' ], true ) ) {
19789 continue;
19790 }
19791 $options[] = [
19792 'label' => esc_attr( $post_type->labels->singular_name ),
19793 'value' => $post_type->name,
19794 ];
19795 }
19796 }
19797 }
19798 return [
19799 'options' => $options,
19800 'hasMore' => false,
19801 ];
19802 }
19803
19804 /**
19805 * Get Voxel Users Last Data
19806 *
19807 * @param array $data data.
19808 *
19809 * @return array|mixed|string
19810 */
19811 public function search_voxel_users_triggers_last_data( $data ) {
19812
19813 global $wpdb;
19814 $context = [];
19815 $term = $data['search_term'];
19816 if ( 'direct_message_received' === $term ) {
19817 $sql = "SELECT * FROM {$wpdb->prefix}voxel_messages WHERE receiver_type LIKE %s AND sender_type LIKE %s ORDER BY id DESC LIMIT 1";
19818 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user', 'user' ), ARRAY_A );// @phpcs:ignore
19819 } elseif ( 'user_timeline_post_created' === $term ) {
19820 $sql = "SELECT vt.* FROM {$wpdb->prefix}voxel_timeline vt WHERE vt.feed = %s AND vt.moderation = %d ORDER BY vt.id DESC LIMIT 1";
19821 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user_timeline', 0 ), ARRAY_A );// @phpcs:ignore
19822 } elseif ( 'user_timeline_post_approved' === $term ) {
19823 $sql = "SELECT vt.* FROM {$wpdb->prefix}voxel_timeline vt WHERE vt.feed = %s AND vt.moderation = %d ORDER BY vt.id DESC LIMIT 1";
19824 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user_timeline', 1 ), ARRAY_A );// @phpcs:ignore
19825 }
19826 if ( 'direct_message_received' === $term ) {
19827 if ( ! empty( $results ) ) {
19828 $context['pluggable_data']['sender'] = WordPress::get_user_context( $results[0]['sender_id'] );
19829 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $results[0]['receiver_id'] );
19830 $context['pluggable_data']['content'] = $results[0]['content'];
19831 $context['response_type'] = 'live';
19832 } else {
19833 $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
19834 }
19835 } elseif ( 'user_timeline_post_created' === $term || 'user_timeline_post_approved' === $term ) {
19836 if ( ! empty( $results ) ) {
19837 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['user_id'] );
19838 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
19839 $status_details = \Voxel\Timeline\Status::get( $results[0]['id'] );
19840 foreach ( (array) $status_details as $key => $value ) {
19841 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
19842 $context['pluggable_data'][ $clean_key ] = $value;
19843 }
19844 }
19845 $context['response_type'] = 'live';
19846 } else {
19847 $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
19848 }
19849 }
19850 return $context;
19851 }
19852
19853
19854 /**
19855 * Get Voxel Membership Last Data
19856 *
19857 * @param array $data data.
19858 *
19859 * @return array|mixed|string
19860 */
19861 public function search_voxel_membership_triggers_last_data( $data ) {
19862
19863 global $wpdb;
19864 $context = [];
19865 $term = $data['search_term'];
19866
19867 if ( ! class_exists( 'Voxel\User' ) ) {
19868 // Return sample data when Voxel class is not available.
19869 if ( 'plan_canceled' === $term ) {
19870 $context['pluggable_data'] = WordPress::get_sample_user_context();
19871 $context['pluggable_data']['details'] = [
19872 'plan' => 'premium-plus',
19873 'type' => 'order',
19874 'order_id' => 14,
19875 'order_item_id' => 17,
19876 'billing' => [
19877 'price_key' => 'premium-plus_pz3xfk7z',
19878 'amount' => 20,
19879 'currency' => 'USD',
19880 'interval' => 'month',
19881 'frequency' => 1,
19882 'current_period' => [
19883 'start' => '2025-10-15 09:01:47',
19884 'end' => '2025-11-15 09:01:47',
19885 ],
19886 'is_active' => 0,
19887 'is_canceled' => 1,
19888 ],
19889 ];
19890 $context['response_type'] = 'sample';
19891 } elseif ( 'plan_activated' === $term ) {
19892 $context['pluggable_data'] = WordPress::get_sample_user_context();
19893 $context['pluggable_data']['details'] = [
19894 'plan' => 'premium-plus',
19895 'type' => 'order',
19896 'order_id' => 14,
19897 'order_item_id' => 17,
19898 'billing' => [
19899 'price_key' => 'premium-plus_pz3xfk7z',
19900 'amount' => 20,
19901 'currency' => 'USD',
19902 'interval' => 'month',
19903 'frequency' => 1,
19904 'current_period' => [
19905 'start' => '2025-10-15 09:01:47',
19906 'end' => '2025-11-15 09:01:47',
19907 ],
19908 'is_active' => 1,
19909 'is_canceled' => '',
19910 ],
19911 ];
19912 $context['response_type'] = 'sample';
19913 } elseif ( 'plan_updated' === $term ) {
19914 $context['pluggable_data'] = WordPress::get_sample_user_context();
19915 $context['pluggable_data']['details'] = [
19916 'plan' => 'premium-plus',
19917 'type' => 'subscription',
19918 'price_id' => 'premium-plus_pz3xfk7z',
19919 'status' => 'active',
19920 'metadata' => [
19921 'voxel:payment_for' => 'membership',
19922 'voxel:plan' => 'premium-plus',
19923 ],
19924 ];
19925 $context['response_type'] = 'sample';
19926 } elseif ( 'user_registered' === $term ) {
19927 $context['pluggable_data'] = WordPress::get_sample_user_context();
19928 $context['response_type'] = 'sample';
19929 }
19930 return $context;
19931 }
19932 if ( 'plan_activated' === $term ) {
19933 $meta_key = ( function_exists( '\Voxel\is_test_mode' ) && \Voxel\is_test_mode() ) ? 'voxel:test_plan' : 'voxel:plan';
19934 $sql = "SELECT
19935 m.user_id AS id,
19936 u.user_login AS title,
19937 u.user_email AS email,
19938 m.meta_value AS details,
19939 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) AS plan,
19940 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.amount' ) ) AS SIGNED ) AS amount,
19941 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.status' ) ) AS status,
19942 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.created' ) ) AS DATETIME ) AS created
19943 FROM {$wpdb->prefix}usermeta as m
19944 LEFT JOIN {$wpdb->prefix}users AS u ON m.user_id = u.ID
19945 WHERE m.meta_key = %s AND JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) != 'default'
19946 ORDER BY m.user_id DESC
19947 LIMIT 25 OFFSET 0";
19948 $results = $wpdb->get_results( $wpdb->prepare( $sql, $meta_key ), ARRAY_A );// @phpcs:ignore
19949 } elseif ( 'plan_canceled' === $term ) {
19950 // Simple approach: Get the most recent user with membership plan and modify data to show as cancelled.
19951 $meta_key = ( function_exists( '\Voxel\is_test_mode' ) && \Voxel\is_test_mode() ) ? 'voxel:test_plan' : 'voxel:plan';
19952 $sql = "SELECT
19953 m.user_id AS id,
19954 u.user_login AS title,
19955 u.user_email AS email,
19956 m.meta_value AS details,
19957 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) AS plan
19958 FROM {$wpdb->prefix}usermeta as m
19959 LEFT JOIN {$wpdb->prefix}users AS u ON m.user_id = u.ID
19960 WHERE m.meta_key = %s AND JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) != 'default'
19961 ORDER BY m.user_id DESC
19962 LIMIT 1";
19963 $results = $wpdb->get_results( $wpdb->prepare( $sql, $meta_key ), ARRAY_A );// @phpcs:ignore
19964 } elseif ( 'plan_updated' === $term ) {
19965 $meta_key = ( function_exists( '\Voxel\is_test_mode' ) && \Voxel\is_test_mode() ) ? 'voxel:test_plan' : 'voxel:plan';
19966 $sql = "SELECT
19967 m.user_id AS id,
19968 u.user_login AS title,
19969 u.user_email AS email,
19970 m.meta_value AS details,
19971 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) AS plan,
19972 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.amount' ) ) AS SIGNED ) AS amount,
19973 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.status' ) ) AS status,
19974 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.created' ) ) AS DATETIME ) AS created
19975 FROM {$wpdb->prefix}usermeta as m
19976 LEFT JOIN {$wpdb->prefix}users AS u ON m.user_id = u.ID
19977 WHERE m.meta_key = %s AND JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) != 'default'
19978 ORDER BY m.user_id DESC
19979 LIMIT 25 OFFSET 0";
19980 $results = $wpdb->get_results( $wpdb->prepare( $sql, $meta_key ), ARRAY_A );// @phpcs:ignore
19981 } elseif ( 'user_registered' === $term ) {
19982 $sql = "SELECT DISTINCT u.ID
19983 FROM {$wpdb->prefix}users u
19984 JOIN {$wpdb->prefix}usermeta um ON u.ID = um.user_id
19985 WHERE um.meta_key = 'voxel:profile_id' ORDER BY ID DESC LIMIT 1";
19986 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
19987 }
19988
19989 if ( 'plan_canceled' === $term ) {
19990 if ( ! empty( $results ) ) {
19991 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
19992 $membership_details = json_decode( $results[0]['details'], true );
19993 $membership_details = is_array( $membership_details ) ? $membership_details : [];
19994
19995 if ( ! isset( $membership_details['billing'] ) && isset( $membership_details['type'] ) ) {
19996 $membership_details = [
19997 'plan' => isset( $membership_details['plan'] ) ? $membership_details['plan'] : 'premium-plus',
19998 'type' => 'order',
19999 'order_id' => isset( $membership_details['order_id'] ) ? $membership_details['order_id'] : 14,
20000 'order_item_id' => isset( $membership_details['order_item_id'] ) ? $membership_details['order_item_id'] : 17,
20001 'billing' => [
20002 'price_key' => isset( $membership_details['price_id'] ) ? $membership_details['price_id'] : 'premium-plus_pz3xfk7z',
20003 'amount' => isset( $membership_details['amount'] ) ? $membership_details['amount'] : 20,
20004 'currency' => isset( $membership_details['currency'] ) ? $membership_details['currency'] : 'USD',
20005 'interval' => isset( $membership_details['interval'] ) ? $membership_details['interval'] : 'month',
20006 'frequency' => isset( $membership_details['interval_count'] ) ? $membership_details['interval_count'] : 1,
20007 'current_period' => [
20008 'start' => gmdate( 'Y-m-d H:i:s' ),
20009 'end' => gmdate( 'Y-m-d H:i:s', strtotime( '+1 month' ) ),
20010 ],
20011 'is_active' => 0,
20012 'is_canceled' => 1,
20013 ],
20014 ];
20015 } else {
20016 if ( isset( $membership_details['billing'] ) ) {
20017 $membership_details['billing']['is_active'] = 0;
20018 $membership_details['billing']['is_canceled'] = 1;
20019 }
20020 }
20021
20022 $context['pluggable_data']['details'] = $membership_details;
20023 $context['response_type'] = 'live';
20024 } else {
20025 $context['pluggable_data'] = WordPress::get_sample_user_context();
20026 $context['pluggable_data']['details'] = [
20027 'plan' => 'premium-plus',
20028 'type' => 'order',
20029 'order_id' => 14,
20030 'order_item_id' => 17,
20031 'billing' => [
20032 'price_key' => 'premium-plus_pz3xfk7z',
20033 'amount' => 20,
20034 'currency' => 'USD',
20035 'interval' => 'month',
20036 'frequency' => 1,
20037 'current_period' => [
20038 'start' => '2025-10-15 09:01:47',
20039 'end' => '2025-11-15 09:01:47',
20040 ],
20041 'is_active' => 0,
20042 'is_canceled' => 1,
20043 ],
20044 ];
20045 $context['response_type'] = 'sample';
20046 }
20047 } elseif ( 'plan_activated' === $term ) {
20048 if ( ! empty( $results ) ) {
20049 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
20050 $membership_details = json_decode( $results[0]['details'], true );
20051 $membership_details = is_array( $membership_details ) ? $membership_details : [];
20052
20053 if ( ! isset( $membership_details['billing'] ) && isset( $membership_details['type'] ) ) {
20054 $membership_details = [
20055 'plan' => isset( $membership_details['plan'] ) ? $membership_details['plan'] : 'premium-plus',
20056 'type' => 'order',
20057 'order_id' => isset( $membership_details['order_id'] ) ? $membership_details['order_id'] : 14,
20058 'order_item_id' => isset( $membership_details['order_item_id'] ) ? $membership_details['order_item_id'] : 17,
20059 'billing' => [
20060 'price_key' => isset( $membership_details['price_id'] ) ? $membership_details['price_id'] : 'premium-plus_pz3xfk7z',
20061 'amount' => isset( $membership_details['amount'] ) ? $membership_details['amount'] : 20,
20062 'currency' => isset( $membership_details['currency'] ) ? $membership_details['currency'] : 'USD',
20063 'interval' => isset( $membership_details['interval'] ) ? $membership_details['interval'] : 'month',
20064 'frequency' => isset( $membership_details['interval_count'] ) ? $membership_details['interval_count'] : 1,
20065 'current_period' => [
20066 'start' => gmdate( 'Y-m-d H:i:s' ),
20067 'end' => gmdate( 'Y-m-d H:i:s', strtotime( '+1 month' ) ),
20068 ],
20069 'is_active' => 1,
20070 'is_canceled' => '',
20071 ],
20072 ];
20073 }
20074
20075 $context['pluggable_data']['details'] = $membership_details;
20076 $context['response_type'] = 'live';
20077 } else {
20078 $context['pluggable_data'] = WordPress::get_sample_user_context();
20079 $context['pluggable_data']['details'] = [
20080 'plan' => 'premium-plus',
20081 'type' => 'order',
20082 'order_id' => 14,
20083 'order_item_id' => 17,
20084 'billing' => [
20085 'price_key' => 'premium-plus_pz3xfk7z',
20086 'amount' => 20,
20087 'currency' => 'USD',
20088 'interval' => 'month',
20089 'frequency' => 1,
20090 'current_period' => [
20091 'start' => '2025-10-15 09:01:47',
20092 'end' => '2025-11-15 09:01:47',
20093 ],
20094 'is_active' => 1,
20095 'is_canceled' => '',
20096 ],
20097 ];
20098 $context['response_type'] = 'sample';
20099 }
20100 } elseif ( 'plan_updated' === $term ) {
20101 if ( ! empty( $results ) ) {
20102 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
20103 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
20104 $context['response_type'] = 'live';
20105 } else {
20106 $context['pluggable_data'] = WordPress::get_sample_user_context();
20107 $context['pluggable_data']['details'] = [
20108 'plan' => 'premium-plus',
20109 'type' => 'subscription',
20110 'price_id' => 'premium-plus_pz3xfk7z',
20111 'status' => 'active',
20112 'metadata' => [
20113 'voxel:payment_for' => 'membership',
20114 'voxel:plan' => 'premium-plus',
20115 ],
20116 ];
20117 $context['response_type'] = 'sample';
20118 }
20119 } elseif ( 'user_registered' === $term ) {
20120 if ( ! empty( $results ) ) {
20121 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['ID'] );
20122 $context['response_type'] = 'live';
20123 } else {
20124 $context['pluggable_data'] = WordPress::get_sample_user_context();
20125 $context['response_type'] = 'sample';
20126 }
20127 }
20128 return $context;
20129 }
20130
20131 /**
20132 * Get Voxel Orders Last Data
20133 *
20134 * @param array $data data.
20135 *
20136 * @return array|mixed|string
20137 */
20138 public function search_voxel_order_triggers_last_data( $data ) {
20139
20140 global $wpdb;
20141 $context = [];
20142 $term = $data['search_term'];
20143
20144 if ( ! class_exists( 'Voxel\Product_Types\Orders\Order' ) ) {
20145 return [];
20146 }
20147
20148 if ( 'order_placed' === $term || 'order_canceled' === $term ) {
20149 if ( 'order_placed' === $term ) {
20150 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders ORDER BY id DESC LIMIT 1";
20151 } elseif ( 'order_canceled' === $term ) {
20152 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' ORDER BY id DESC LIMIT 1";
20153 }
20154
20155 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
20156 if ( ! empty( $results ) ) {
20157 // Get Order.
20158 $context['pluggable_data']['id'] = $results[0]['id'];
20159 $context['pluggable_data']['vendor_id'] = $results[0]['vendor_id'];
20160 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
20161 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
20162 $context['pluggable_data']['status'] = $results[0]['status'];
20163 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
20164 $context['pluggable_data']['subtotal'] = $results[0]['subtotal'];
20165 $context['pluggable_data']['total'] = $results[0]['total'];
20166 // Get order items.
20167 $order = \Voxel\Product_Types\Orders\Order::find(
20168 [
20169 'id' => $results[0]['id'],
20170 ]
20171 );
20172 $order_items = $order->get_items();
20173 $context['pluggable_data']['tax_amount'] = $order->get_tax_amount();
20174 $context['pluggable_data']['discount_amount'] = $order->get_discount_amount();
20175 $context['pluggable_data']['shipping_amount'] = $order->get_shipping_amount();
20176 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
20177 foreach ( $order_items as $item ) {
20178 $addon_data = [];
20179 if ( is_object( $item ) && method_exists( $item, 'get_addons' ) ) {
20180 // Get addon details.
20181 $addons = $item->get_addons();
20182 $addon_data = [];
20183
20184 // Add the addons to the simple entry.
20185 if ( $addons && isset( $addons['summary'] ) ) {
20186 $addon_data = $addons['summary'];
20187 }
20188 }
20189 $context['pluggable_data']['order_items'][] = [
20190 'id' => $item->get_id(),
20191 'type' => $item->get_type(),
20192 'currency' => $item->get_currency(),
20193 'quantity' => $item->get_quantity(),
20194 'subtotal' => $item->get_subtotal(),
20195 'product_id' => $item->get_post()->get_id(),
20196 'product_label' => $item->get_product_label(),
20197 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
20198 'product_link' => $item->get_product_link(),
20199 'description' => $item->get_product_description(),
20200 'addon_data' => $addon_data,
20201 ];
20202 // If booking item, get booking details.
20203 if ( 'booking' === $item->get_type() ) {
20204 $details = $item->get_order_page_details();
20205 $context['pluggable_data']['order_items']['booking_type'] = $details['booking']['type'];
20206 if ( isset( $details['booking']['count_mode'] ) ) {
20207 $context['pluggable_data']['order_items']['booking_count_mode'] = $details['booking']['count_mode'];
20208 }
20209 if ( 'date_range' === $details['booking']['type'] ) {
20210 $context['pluggable_data']['order_items']['booking_start_date'] = $details['booking']['start_date'];
20211 $context['pluggable_data']['order_items']['booking_end_date'] = $details['booking']['end_date'];
20212 } elseif ( 'single_day' === $details['booking']['type'] ) {
20213 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
20214 } elseif ( 'timeslots' === $details['booking']['type'] ) {
20215 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
20216 $context['pluggable_data']['order_items']['booking_slot_from'] = $details['booking']['slot']['from'];
20217 $context['pluggable_data']['order_items']['booking_slot_to'] = $details['booking']['slot']['to'];
20218 }
20219 }
20220 }
20221 // Get Customer.
20222 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
20223 $context['response_type'] = 'live';
20224 } else {
20225 $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
20226 }
20227 } elseif ( 'order_approved' === $term || 'order_declined' === $term ) {
20228 if ( 'order_approved' === $term ) {
20229 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'completed' ORDER BY id DESC LIMIT 1";
20230 } elseif ( 'order_declined' === $term ) {
20231 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' ORDER BY id DESC LIMIT 1";
20232 }
20233 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
20234 if ( ! empty( $results ) ) {
20235 // Get Order.
20236 $context['pluggable_data']['id'] = $results[0]['id'];
20237 $context['pluggable_data']['vendor_id'] = $results[0]['vendor_id'];
20238 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
20239 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
20240 $context['pluggable_data']['status'] = $results[0]['status'];
20241 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
20242 $context['pluggable_data']['subtotal'] = $results[0]['subtotal'];
20243 $context['pluggable_data']['total'] = $results[0]['total'];
20244 // Get order items.
20245 $order = \Voxel\Product_Types\Orders\Order::find(
20246 [
20247 'id' => $results[0]['id'],
20248 ]
20249 );
20250 $order_items = $order->get_items();
20251 $context['pluggable_data']['tax_amount'] = $order->get_tax_amount();
20252 $context['pluggable_data']['discount_amount'] = $order->get_discount_amount();
20253 $context['pluggable_data']['shipping_amount'] = $order->get_shipping_amount();
20254 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
20255 foreach ( $order_items as $item ) {
20256 $addon_data = [];
20257 if ( is_object( $item ) && method_exists( $item, 'get_addons' ) ) {
20258 // Get addon details.
20259 $addons = $item->get_addons();
20260 $addon_data = [];
20261
20262 // Add the addons to the simple entry.
20263 if ( $addons && isset( $addons['summary'] ) ) {
20264 $addon_data = $addons['summary'];
20265 }
20266 }
20267 $context['pluggable_data']['order_items'][] = [
20268 'id' => $item->get_id(),
20269 'type' => $item->get_type(),
20270 'currency' => $item->get_currency(),
20271 'quantity' => $item->get_quantity(),
20272 'subtotal' => $item->get_subtotal(),
20273 'product_id' => $item->get_post()->get_id(),
20274 'product_label' => $item->get_product_label(),
20275 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
20276 'product_link' => $item->get_product_link(),
20277 'description' => $item->get_product_description(),
20278 'addon_data' => $addon_data,
20279 ];
20280 // If booking item, get booking details.
20281 if ( 'booking' === $item->get_type() ) {
20282 $details = $item->get_order_page_details();
20283 $context['pluggable_data']['order_items']['booking_type'] = $details['booking']['type'];
20284 if ( isset( $details['booking']['count_mode'] ) ) {
20285 $context['pluggable_data']['order_items']['booking_count_mode'] = $details['booking']['count_mode'];
20286 }
20287 if ( 'date_range' === $details['booking']['type'] ) {
20288 $context['pluggable_data']['order_items']['booking_start_date'] = $details['booking']['start_date'];
20289 $context['pluggable_data']['order_items']['booking_end_date'] = $details['booking']['end_date'];
20290 } elseif ( 'single_day' === $details['booking']['type'] ) {
20291 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
20292 } elseif ( 'timeslots' === $details['booking']['type'] ) {
20293 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
20294 $context['pluggable_data']['order_items']['booking_slot_from'] = $details['booking']['slot']['from'];
20295 $context['pluggable_data']['order_items']['booking_slot_to'] = $details['booking']['slot']['to'];
20296 }
20297 }
20298 }
20299 // Get Customer.
20300 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
20301 // Get Vendor.
20302 $context['pluggable_data']['vendor'] = WordPress::get_user_context( $results[0]['vendor_id'] );
20303 $context['response_type'] = 'live';
20304 } else {
20305 $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
20306 }
20307 } elseif ( 'order_promotion_activated' === $term || 'order_promotion_canceled' === $term || 'order_claim_listing' === $term ) {
20308 if ( 'order_promotion_activated' === $term ) {
20309 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'completed' AND details LIKE '%voxel:promotion%' ORDER BY id DESC LIMIT 1";
20310 } elseif ( 'order_promotion_canceled' === $term ) {
20311 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' AND details LIKE '%voxel:promotion%' ORDER BY id DESC LIMIT 1";
20312 } elseif ( 'order_claim_listing' === $term ) {
20313 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE details LIKE '%voxel:claim%' ORDER BY id DESC LIMIT 1";
20314 }
20315 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
20316 if ( ! empty( $results ) ) {
20317 // Get Order.
20318 $context['pluggable_data']['id'] = $results[0]['id'];
20319 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
20320 $context['pluggable_data']['status'] = $results[0]['status'];
20321 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
20322 // Get order items.
20323 $order = \Voxel\Product_Types\Orders\Order::find(
20324 [
20325 'id' => $results[0]['id'],
20326 ]
20327 );
20328 $order_items = $order->get_items();
20329 $context['pluggable_data']['details'] = $order->get_details();
20330 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
20331 foreach ( $order_items as $item ) {
20332 $context['pluggable_data']['order_items'][] = [
20333 'id' => $item->get_id(),
20334 'type' => $item->get_type(),
20335 'currency' => $item->get_currency(),
20336 'quantity' => $item->get_quantity(),
20337 'subtotal' => $item->get_subtotal(),
20338 'product_id' => $item->get_post()->get_id(),
20339 'product_label' => $item->get_product_label(),
20340 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
20341 'product_link' => $item->get_product_link(),
20342 'description' => $item->get_product_description(),
20343 ];
20344 }
20345 // Get Customer.
20346 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
20347 $context['response_type'] = 'live';
20348 } else {
20349 $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
20350 }
20351 }
20352 return $context;
20353 }
20354
20355 /**
20356 * Get Voxel Timeline Comments Last Data
20357 *
20358 * @param array $data data.
20359 *
20360 * @return array|mixed|string
20361 */
20362 public function search_voxel_timeline_triggers_last_data( $data ) {
20363
20364 global $wpdb;
20365 $context = [];
20366 $term = $data['search_term'];
20367 $sql = '';
20368 if ( 'new_comment_timeline' === $term ) {
20369 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE parent_id IS NULL ORDER BY id DESC LIMIT 1";
20370 } elseif ( 'comment_reply_timeline' === $term ) {
20371 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE parent_id IS NOT NULL ORDER BY id DESC LIMIT 1";
20372 } elseif ( 'comment_liked' === $term ) {
20373 $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";
20374 } elseif ( 'user_timeline_post_liked' === $term ) {
20375 $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";
20376 } elseif ( 'user_timeline_post_quoted' === $term ) {
20377 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline WHERE feed = %s AND quote_of IS NOT NULL ORDER BY id DESC LIMIT 1";
20378 } elseif ( 'user_timeline_post_reposted' === $term ) {
20379 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline WHERE feed = %s AND repost_of IS NOT NULL ORDER BY id DESC LIMIT 1";
20380 }
20381 if ( 'user_timeline_post_reposted' === $term || 'user_timeline_post_quoted' === $term ) {
20382 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user_timeline' ), ARRAY_A );// @phpcs:ignore
20383 } else {
20384 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
20385 }
20386 if ( 'new_comment_timeline' === $term ) {
20387 if ( ! empty( $results ) ) {
20388 $context['pluggable_data']['comment_by'] = WordPress::get_user_context( $results[0]['user_id'] );
20389 $context['pluggable_data']['id'] = $results[0]['id'];
20390 $context['pluggable_data']['status_id'] = $results[0]['status_id'];
20391 $context['pluggable_data']['content'] = $results[0]['content'];
20392 $context['response_type'] = 'live';
20393 } else {
20394 $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
20395 }
20396 } elseif ( 'comment_reply_timeline' === $term ) {
20397 if ( ! empty( $results ) ) {
20398 $comment_sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE id = %d";
20399 $comment_result = $wpdb->get_results( $wpdb->prepare( $comment_sql, $results[0]['parent_id']), ARRAY_A );// @phpcs:ignore
20400 $context['pluggable_data']['replied_by'] = WordPress::get_user_context( $results[0]['user_id'] );
20401 $context['pluggable_data']['comment_by'] = WordPress::get_user_context( $comment_result[0]['user_id'] );
20402 $context['pluggable_data']['comment'] = $comment_result[0]['content'];
20403 $context['pluggable_data']['comment_id'] = $results[0]['parent_id'];
20404 $context['pluggable_data']['reply_id'] = $results[0]['id'];
20405 $context['pluggable_data']['reply'] = $results[0]['content'];
20406 $context['response_type'] = 'live';
20407 } else {
20408 $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
20409 }
20410 } elseif ( 'comment_liked' === $term ) {
20411 if ( ! empty( $results ) ) {
20412 $user = get_userdata( $results[0]['user_id'] );
20413 if ( $user ) {
20414 $user_data = (array) $user->data;
20415 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
20416 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
20417 $context['pluggable_data']['user_email'] = $user_data['user_email'];
20418 }
20419 $recipient_user = get_userdata( $results[0]['like_user_id'] );
20420 if ( $recipient_user ) {
20421 $recipient_user_data = (array) $recipient_user->data;
20422 $context['pluggable_data']['recipient']['user_display_name'] = $recipient_user_data['display_name'];
20423 $context['pluggable_data']['recipient']['user_name'] = $recipient_user_data['user_nicename'];
20424 $context['pluggable_data']['recipient']['user_email'] = $recipient_user_data['user_email'];
20425 }
20426 if ( class_exists( 'Voxel\Timeline\Reply' ) ) {
20427 $reply_details = \Voxel\Timeline\Reply::get( $results[0]['id'] );
20428 foreach ( (array) $reply_details as $key => $value ) {
20429 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
20430 $context['pluggable_data'][ $clean_key ] = $value;
20431 }
20432 }
20433 $context['response_type'] = 'live';
20434 } else {
20435 $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
20436 }
20437 } elseif ( 'user_timeline_post_liked' === $term ) {
20438 if ( ! empty( $results ) ) {
20439 $user = get_userdata( $results[0]['user_id'] );
20440 if ( $user ) {
20441 $user_data = (array) $user->data;
20442 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
20443 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
20444 $context['pluggable_data']['user_email'] = $user_data['user_email'];
20445 }
20446 $recipient_user = get_userdata( $results[0]['like_user_id'] );
20447 if ( $recipient_user ) {
20448 $recipient_user_data = (array) $recipient_user->data;
20449 $context['pluggable_data']['recipient']['user_display_name'] = $recipient_user_data['display_name'];
20450 $context['pluggable_data']['recipient']['user_name'] = $recipient_user_data['user_nicename'];
20451 $context['pluggable_data']['recipient']['user_email'] = $recipient_user_data['user_email'];
20452 }
20453 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
20454 $reply_details = \Voxel\Timeline\Status::get( $results[0]['id'] );
20455 foreach ( (array) $reply_details as $key => $value ) {
20456 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
20457 $context['pluggable_data'][ $clean_key ] = $value;
20458 }
20459 }
20460 $context['response_type'] = 'live';
20461 } else {
20462 $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
20463 }
20464 } elseif ( 'user_timeline_post_reposted' === $term || 'user_timeline_post_quoted' === $term ) {
20465 if ( ! empty( $results ) ) {
20466 $author = get_userdata( $results[0]['user_id'] );
20467 if ( $author ) {
20468 $author_data = (array) $author->data;
20469 $context['pluggable_data']['author_display_name'] = $author_data['display_name'];
20470 $context['pluggable_data']['author_name'] = $author_data['user_nicename'];
20471 $context['pluggable_data']['author_email'] = $author_data['user_email'];
20472 }
20473 $column = 'user_timeline_post_reposted' === $term ? 'repost_of' : 'quote_of';
20474 $recipient_sql = "SELECT user_id FROM {$wpdb->prefix}voxel_timeline WHERE id = %d";
20475 $recipient_results = $wpdb->get_results( $wpdb->prepare( $recipient_sql, $results[0][$column] ), ARRAY_A ); // @phpcs:ignore
20476 if ( ! empty( $recipient_results ) ) {
20477 $recipient_user = get_userdata( $recipient_results[0]['user_id'] );
20478 if ( $recipient_user ) {
20479 $recipient_user_data = (array) $recipient_user->data;
20480 $context['pluggable_data']['recipient']['user_display_name'] = $recipient_user_data['display_name'];
20481 $context['pluggable_data']['recipient']['user_name'] = $recipient_user_data['user_nicename'];
20482 $context['pluggable_data']['recipient']['user_email'] = $recipient_user_data['user_email'];
20483 }
20484 }
20485 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
20486 $status_details = \Voxel\Timeline\Status::get( $results[0]['id'] );
20487 $details_column = 'user_timeline_post_reposted' === $term ? 'repost_of' : 'quote_of';
20488 $related_details = \Voxel\Timeline\Status::get( $results[0][ $details_column ] );
20489 foreach ( [
20490 'status' => $status_details,
20491 $details_column => $related_details,
20492 ] as $key => $details ) {
20493 foreach ( (array) $details as $sub_key => $value ) {
20494 $clean_key = preg_replace( '/^\0.*?\0/', '', $sub_key );
20495 $context['pluggable_data'][ $key ][ $clean_key ] = $value;
20496 }
20497 }
20498 }
20499 $context['response_type'] = 'live';
20500 } else {
20501 if ( 'user_timeline_post_reposted' === $term ) {
20502 $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
20503 } else {
20504 $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
20505 }
20506 }
20507 }
20508 return $context;
20509 }
20510
20511 /**
20512 * Get Voxel Timeline Comments Last Data
20513 *
20514 * @param array $data data.
20515 *
20516 * @return array|mixed|string
20517 */
20518 public function search_voxel_posts_triggers_last_data( $data ) {
20519 $context = [];
20520 global $wpdb;
20521 $post_type = 'post';
20522 $term = $data['search_term'];
20523 if ( isset( $data['filter']['post_type']['value'] ) ) {
20524 $post_type = $data['filter']['post_type']['value'];
20525 }
20526 $results = [];
20527 if ( 'post_approved' === $term ) {
20528 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
20529 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type, 'publish'), ARRAY_A );// @phpcs:ignore
20530 } elseif ( 'post_rejected' === $term ) {
20531 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
20532 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type, 'rejected'), ARRAY_A );// @phpcs:ignore
20533 } elseif ( 'post_reviewed' === $term ) {
20534 $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
20535 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";
20536 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type, '%rating%' ), ARRAY_A );// @phpcs:ignore
20537 } elseif ( 'post_review_approved' === $term ) {
20538 $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
20539 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";
20540 $results = $wpdb->get_results( $wpdb->prepare( $sql, 1, $post_type, '%rating%', 'post_reviews' ), ARRAY_A );// @phpcs:ignore
20541 } elseif ( 'post_submitted' === $term ) {
20542 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
20543 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type), ARRAY_A );// @phpcs:ignore
20544 } elseif ( 'post_updated' === $term ) {
20545 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
20546 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type ), ARRAY_A );// @phpcs:ignore
20547 } elseif ( 'collection_post_submitted' === $term ) {
20548 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
20549 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'collection' ), ARRAY_A );// @phpcs:ignore
20550 } elseif ( 'collection_post_updated' === $term ) {
20551 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
20552 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'collection' ), ARRAY_A );// @phpcs:ignore
20553 } elseif ( 'new_wall_post_by_user' === $term ) {
20554 $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";
20555 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type, '%rating%' ), ARRAY_A );// @phpcs:ignore
20556 } elseif ( 'wall_post_approved' === $term ) {
20557 $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";
20558 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'post_wall', $post_type, 1 ), ARRAY_A );// @phpcs:ignore
20559 } elseif ( 'timeline_post_approved' === $term ) {
20560 $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";
20561 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'post_timeline', 1, $post_type ), ARRAY_A );// @phpcs:ignore
20562 } elseif ( 'taxonomy_updated' === $term ) {
20563 $sql = "SELECT tr.object_id as post_id, tt.taxonomy, tr.term_taxonomy_id
20564 FROM {$wpdb->prefix}term_relationships tr
20565 JOIN {$wpdb->prefix}term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
20566 JOIN {$wpdb->prefix}posts p ON tr.object_id = p.ID
20567 WHERE p.post_type LIKE %s
20568 ORDER BY tr.object_id DESC LIMIT 1";
20569 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type ), ARRAY_A );// @phpcs:ignore
20570 } elseif ( 'post_followed' === $term ) {
20571 $sql = "SELECT * FROM {$wpdb->prefix}voxel_followers WHERE object_type = %s LIMIT 1";
20572 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'post' ), ARRAY_A );// @phpcs:ignore
20573 }
20574 if ( 'post_approved' === $term || 'post_rejected' === $term || 'post_submitted' === $term || 'post_updated' === $term || 'timeline_post_approved' === $term ) {
20575 if ( ! empty( $results ) ) {
20576 $context['pluggable_data'] = WordPress::get_post_context( $results[0]['ID'] );
20577 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['ID'] );
20578 $context['response_type'] = 'live';
20579 } else {
20580 $context['pluggable_data'] = [
20581 'ID' => 557,
20582 'post' => 557,
20583 'post_author' => 1,
20584 'post_date' => '2022-11-18 12:18:14',
20585 'post_date_gmt' => '2022-11-18 12:18:14',
20586 'post_content' => 'Test Post Content',
20587 'post_title' => 'Test Post',
20588 'post_excerpt' => '',
20589 'post_status' => 'published',
20590 'comment_status' => 'open',
20591 'ping_status' => 'open',
20592 'post_password' => '',
20593 'post_name' => 'test-post',
20594 'to_ping' => '',
20595 'pinged' => '',
20596 'post_modified' => '2022-11-18 12:18:14',
20597 'post_modified_gmt' => '2022-11-18 12:18:14',
20598 'post_content_filtered' => '',
20599 'post_parent' => 0,
20600 'guid' => 'https://example.com/test-post/',
20601 'menu_order' => 0,
20602 'post_type' => 'post',
20603 'post_mime_type' => '',
20604 'comment_count' => 0,
20605 'filter' => 'raw',
20606 ];
20607 $context['response_type'] = 'sample';
20608 }
20609 } elseif ( 'post_reviewed' === $term || 'post_review_approved' === $term ) {
20610 if ( ! empty( $results ) ) {
20611 $context['pluggable_data'] = WordPress::get_post_context( $results[0]['post_id'] );
20612 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['post_id'] );
20613 $context['pluggable_data']['review_content'] = $results[0]['content'];
20614 $context['pluggable_data']['review_created_at'] = $results[0]['created_at'];
20615 $context['pluggable_data']['review_details'] = json_decode( $results[0]['details'], true );
20616 $context['pluggable_data']['review_by'] = WordPress::get_user_context( $results[0]['user_id'] );
20617 $context['response_type'] = 'live';
20618 } else {
20619 $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
20620 }
20621 } elseif ( 'collection_post_submitted' === $term || 'collection_post_updated' === $term ) {
20622 if ( ! empty( $results ) ) {
20623 $context['pluggable_data'] = Voxel::get_post_fields( $results[0]['ID'] );
20624 $context['pluggable_data']['collection'] = WordPress::get_post_context( $results[0]['ID'] );
20625 $context['response_type'] = 'live';
20626 } else {
20627 $context = json_decode(
20628 '{"pluggable_data":{"field_step-general": null,"field_title": "First Collection","field_items": [8909,8293],"collection": {"ID": 9142,"post_author": "35",
20629 "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
20630 }
20631 } elseif ( 'new_wall_post_by_user' === $term || 'wall_post_approved' === $term ) {
20632 if ( ! empty( $results ) ) {
20633 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['post_id'] );
20634 $user = get_userdata( $results[0]['user_id'] );
20635 if ( $user ) {
20636 $user_data = (array) $user->data;
20637 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
20638 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
20639 $context['pluggable_data']['user_email'] = $user_data['user_email'];
20640 $context['pluggable_data']['user_id'] = $results[0]['user_id'];
20641 }
20642 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
20643 $args = [
20644 'post_id' => $results[0]['post_id'],
20645 'order_by' => 'created_at',
20646 'order' => 'desc',
20647 'feed' => $results[0]['feed'],
20648 'limit' => 1,
20649 'moderation' => $results[0]['moderation'],
20650 ];
20651 $statuses = \Voxel\Timeline\Status::query( $args );
20652 $status_details = $statuses['items'][0];
20653 foreach ( (array) $status_details as $key => $value ) {
20654 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
20655 if ( is_object( $value ) ) {
20656 $encoded_value = wp_json_encode( $value );
20657 if ( is_string( $encoded_value ) ) {
20658 $value = json_decode( $encoded_value, true );
20659 }
20660 }
20661 $context['pluggable_data']['wall_post'][ $clean_key ] = $value;
20662 }
20663 }
20664 $context['response_type'] = 'live';
20665 } else {
20666 $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
20667 }
20668 } elseif ( 'taxonomy_updated' === $term ) {
20669 if ( ! empty( $results ) ) {
20670 $post_id = $results[0]['post_id'];
20671 $context['pluggable_data'] = WordPress::get_post_context( $post_id );
20672 $context['pluggable_data']['post'] = Voxel::get_post_fields( $post_id );
20673 $context['pluggable_data']['post_type'] = get_post_type( $post_id );
20674
20675 // Get taxonomy details.
20676 $term_obj = get_term( $results[0]['term_taxonomy_id'] );
20677 if ( $term_obj && ! is_wp_error( $term_obj ) ) {
20678 $context['pluggable_data']['taxonomy'] = $results[0]['taxonomy'];
20679 $context['pluggable_data']['update_type'] = 'updated';
20680 $context['pluggable_data']['terms_added'] = [ $results[0]['term_taxonomy_id'] ];
20681 $context['pluggable_data']['terms_removed'] = [];
20682 $context['pluggable_data']['current_terms'] = [ $results[0]['term_taxonomy_id'] ];
20683 $context['pluggable_data']['previous_terms'] = [];
20684 $context['pluggable_data']['added_terms_details'] = [
20685 [
20686 'term_id' => $term_obj->term_id,
20687 'name' => $term_obj->name,
20688 'slug' => $term_obj->slug,
20689 ],
20690 ];
20691 $context['pluggable_data']['removed_terms_details'] = [];
20692 $context['pluggable_data']['current_terms_details'] = [
20693 [
20694 'term_id' => $term_obj->term_id,
20695 'name' => $term_obj->name,
20696 'slug' => $term_obj->slug,
20697 ],
20698 ];
20699 $context['pluggable_data']['is_voxel_taxonomy_field'] = false;
20700 $context['pluggable_data']['voxel_field_data'] = null;
20701 }
20702 $context['response_type'] = 'live';
20703 } else {
20704 $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
20705 }
20706 } elseif ( 'post_followed' === $term ) {
20707 if ( ! empty( $results ) ) {
20708 $follow_data = [
20709 'post_id' => $results[0]['object_id'],
20710 'user_id' => $results[0]['follower_id'],
20711 'status' => $results[0]['status'],
20712 ];
20713 $follow_data = array_merge( $follow_data, Voxel::get_post_fields( $results[0]['object_id'] ), WordPress::get_post_context( $results[0]['object_id'] ) );
20714 $followers_sql = "SELECT COUNT(*) FROM {$wpdb->prefix}voxel_followers WHERE object_id= %d";
20715 $followers = $wpdb->get_var( $wpdb->prepare( $followers_sql, $results[0]['object_id'] ));// @phpcs:ignore
20716 $follow_data['total_followers'] = $followers;
20717 $context['pluggable_data'] = $follow_data;
20718 $context['response_type'] = 'live';
20719 } else {
20720 $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
20721 }
20722 }
20723 return $context;
20724 }
20725
20726 /**
20727 * Get Voxel Profiles Last Data
20728 *
20729 * @param array $data data.
20730 *
20731 * @return array|mixed|string
20732 */
20733 public function search_voxel_profiles_triggers_last_data( $data ) {
20734
20735 global $wpdb;
20736 $context = [];
20737 $term = $data['search_term'];
20738
20739 if ( ! class_exists( 'Voxel\Timeline\Status' ) || ! class_exists( 'Voxel\Post_Type' ) ) {
20740 return [];
20741 }
20742
20743 if ( 'profile_created' === $term ) {
20744 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
20745 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
20746 } elseif ( 'profile_approved' === $term ) {
20747 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
20748 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile', 'publish' ), ARRAY_A );// @phpcs:ignore
20749 } elseif ( 'profile_rejected' === $term ) {
20750 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
20751 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile', 'rejected' ), ARRAY_A );// @phpcs:ignore
20752 } elseif ( 'profile_reviewed' === $term ) {
20753 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline vt LEFT JOIN {$wpdb->prefix}posts pm
20754 ON vt.post_id = pm.ID
20755 WHERE pm.post_type LIKE %s AND vt.details IS NOT NULL ORDER BY vt.id DESC LIMIT 1";
20756 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
20757 } elseif ( 'profile_updated' === $term ) {
20758 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
20759 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
20760 } elseif ( 'profile_new_wall_post' === $term ) {
20761 $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";
20762 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
20763 }
20764 if ( 'profile_created' === $term || 'profile_approved' === $term || 'profile_rejected' === $term || 'profile_updated' === $term ) {
20765 if ( ! empty( $results ) ) {
20766 $context['pluggable_data'] = Voxel::get_post_fields( $results[0]['ID'] );
20767 $user = get_userdata( $results[0]['post_author'] );
20768 if ( $user ) {
20769 $user_data = (array) $user->data;
20770 $context['pluggable_data']['profile_display_name'] = $user_data['display_name'];
20771 $context['pluggable_data']['profile_email'] = $user_data['user_email'];
20772 $context['pluggable_data']['profile_user_id'] = $results[0]['post_author'];
20773 $context['pluggable_data']['profile_id'] = $results[0]['ID'];
20774 }
20775 $context['response_type'] = 'live';
20776 } else {
20777 $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
20778 }
20779 } elseif ( 'profile_new_wall_post' === $term ) {
20780 if ( ! empty( $results ) ) {
20781 $context['pluggable_data']['profile'] = Voxel::get_post_fields( $results[0]['post_id'] );
20782 $user = get_userdata( $results[0]['user_id'] );
20783 if ( $user ) {
20784 $user_data = (array) $user->data;
20785 $context['pluggable_data']['profile_display_name'] = $user_data['display_name'];
20786 $context['pluggable_data']['profile_name'] = $user_data['user_nicename'];
20787 $context['pluggable_data']['profile_email'] = $user_data['user_email'];
20788 $context['pluggable_data']['profile_user_id'] = $results[0]['user_id'];
20789 }
20790 // Get the status details.
20791 $args = [
20792 'post_id' => $results[0]['post_id'],
20793 'order_by' => 'created_at',
20794 'order' => 'desc',
20795 'feed' => $results[0]['feed'],
20796 'limit' => 1,
20797 'moderation' => $results[0]['moderation'],
20798 ];
20799 $statuses = \Voxel\Timeline\Status::query( $args );
20800 $status_details = $statuses['items'][0];
20801 foreach ( (array) $status_details as $key => $value ) {
20802 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
20803 if ( is_object( $value ) ) {
20804 $encoded_value = wp_json_encode( $value );
20805 if ( is_string( $encoded_value ) ) {
20806 $value = json_decode( $encoded_value, true );
20807 }
20808 }
20809 $context['pluggable_data']['wall_post'][ $clean_key ] = $value;
20810 }
20811 $context['response_type'] = 'live';
20812 } else {
20813 $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
20814 }
20815 } elseif ( 'profile_reviewed' === $term ) {
20816 if ( ! empty( $results ) ) {
20817 // Get the review details.
20818 $args = [
20819 'post_id' => $results[0]['post_id'],
20820 'order_by' => 'created_at',
20821 'order' => 'desc',
20822 'feed' => $results[0]['feed'],
20823 'limit' => 1,
20824 'moderation' => $results[0]['moderation'],
20825 ];
20826 $statuses = \Voxel\Timeline\Status::query( $args );
20827 $review_details = $statuses['items'][0];
20828 foreach ( (array) $review_details as $key => $value ) {
20829 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
20830 if ( 'user_can_edit' == $clean_key || 'publisher' == $clean_key || 'user_can_edit' == $clean_key || 'user_can_moderate' == $clean_key ) {
20831 continue;
20832 }
20833 if ( 'files' === $clean_key ) {
20834 $value = wp_json_encode( $value );
20835 } elseif ( 'details' === $clean_key ) {
20836 $review_ratings = isset( $value['rating'] ) && is_array( $value['rating'] ) ? $value['rating'] : [];
20837 $value['rating'] = [];
20838 $type = \Voxel\Post_Type::get( 'profile' );
20839
20840 if ( ! empty( $review_ratings ) ) {
20841 $rating_levels = $type->reviews->get_rating_levels();
20842 $categories = $type->reviews->get_categories();
20843
20844 foreach ( $categories as $category ) {
20845 $category_key = $category['key'];
20846 $category_label = strtolower( $category['label'] );
20847
20848 if ( isset( $review_ratings[ $category_key ] ) && $category_label ) {
20849 foreach ( $rating_levels as $rating_level ) {
20850 if ( $review_ratings[ $category_key ] === $rating_level['score'] ) {
20851 $value['rating'][ $category_label ] = $rating_level['label'];
20852 break;
20853 }
20854 }
20855 }
20856 }
20857 }
20858 } else {
20859 $clean_key = 'review_' . $clean_key;
20860 }
20861 $context['pluggable_data'][ $clean_key ] = $value;
20862 }
20863 $context['response_type'] = 'live';
20864 } else {
20865 $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
20866 }
20867 }
20868 return $context;
20869 }
20870
20871 /**
20872 * Get Voxel Messages Triggers Last Data
20873 *
20874 * @param array $data data.
20875 *
20876 * @return array|mixed|string
20877 */
20878 public function search_voxel_messages_triggers_last_data( $data ) {
20879 global $wpdb;
20880 $context = [];
20881 $term = $data['search_term'];
20882
20883 if ( 'voxel_new_message_post_created' === $term ) {
20884 $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}voxel_messages ORDER BY id DESC LIMIT %d", 1 ), ARRAY_A );
20885
20886 if ( ! empty( $results ) ) {
20887 $message = $results[0];
20888 $context['pluggable_data']['sender'] = WordPress::get_user_context( $message['sender_id'] );
20889 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $message['receiver_id'] );
20890 $context['pluggable_data']['content'] = $message['content'];
20891 $context['pluggable_data']['message_id'] = $message['id'];
20892 $context['pluggable_data']['created_at'] = $message['created_at'];
20893 $context['response_type'] = 'live';
20894 } else {
20895 $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 );
20896 }
20897 }
20898
20899 return $context;
20900 }
20901
20902 /**
20903 * Get Late Point Customer Fields
20904 *
20905 * @param array $data data.
20906 *
20907 * @return array|void
20908 */
20909 public function search_late_point_customer_fields( $data ) {
20910 if ( ! class_exists( 'OsCustomFieldsController' ) ) {
20911
20912 return;
20913 }
20914 global $wpdb;
20915 $booking_fields = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}latepoint_settings WHERE name = %s", 'custom_fields_for_customer' ) );
20916 $fields = [];
20917 if ( ! empty( $booking_fields ) ) {
20918 $fields = json_decode( $booking_fields->value, true );
20919 }
20920 return (array) $fields;
20921 }
20922
20923 /**
20924 * Is multi person booking
20925 *
20926 * @param array $data data.
20927 *
20928 * @return array|void
20929 */
20930 public function search_late_point_multi_person_booking( $data ) {
20931 if ( ! class_exists( 'LatePointAddonGroupBookings' ) ) {
20932
20933 return;
20934 }
20935 $service_id = isset( $data['dynamic'] ) ? $data['dynamic'] : '-1';
20936
20937 if ( -1 == $service_id ) {
20938 return;
20939 }
20940 global $wpdb;
20941 $booking_details = $wpdb->get_row( $wpdb->prepare( "SELECT capacity_max,capacity_min FROM {$wpdb->prefix}latepoint_services WHERE id= %s", intval( $service_id ) ) );
20942 if ( ! empty( $booking_details ) ) {
20943 return (array) $booking_details;
20944 }
20945
20946 }
20947
20948 /**
20949 * Get Mail Mint lists
20950 *
20951 * @param array $data data.
20952 *
20953 * @return array
20954 */
20955 public function search_mail_mint_contact_lists( $data ) {
20956 global $wpdb;
20957
20958 $lists = $wpdb->get_results( $wpdb->prepare( 'SELECT title, id FROM ' . $wpdb->prefix . 'mint_contact_groups WHERE type = %s', 'lists' ) );
20959 $options = [];
20960
20961 if ( ! empty( $lists ) ) {
20962 foreach ( $lists as $list ) {
20963 $options[] = [
20964 'label' => $list->title,
20965 'value' => $list->id,
20966 ];
20967 }
20968 }
20969 return [
20970 'options' => $options,
20971 'hasMore' => false,
20972 ];
20973 }
20974
20975 /**
20976 * Get Mail Mint tags
20977 *
20978 * @param array $data data.
20979 *
20980 * @return array
20981 */
20982 public function search_mail_mint_contact_tags( $data ) {
20983 global $wpdb;
20984
20985 $lists = $wpdb->get_results( $wpdb->prepare( 'SELECT title, id FROM ' . $wpdb->prefix . 'mint_contact_groups WHERE type = %s ', 'tags' ) );
20986 $options = [];
20987
20988 if ( ! empty( $lists ) ) {
20989 foreach ( $lists as $list ) {
20990 $options[] = [
20991 'label' => $list->title,
20992 'value' => $list->id,
20993 ];
20994 }
20995 }
20996 return [
20997 'options' => $options,
20998 'hasMore' => false,
20999 ];
21000 }
21001
21002 /** Get Better Messages trigger Last Data
21003 *
21004 * @param array $data data.
21005 *
21006 * @return array|mixed|string
21007 */
21008 public function search_better_messages_triggers_data( $data ) {
21009 $context = [];
21010 global $wpdb;
21011 if ( ! function_exists( 'Better_Messages' ) ) {
21012 return [];
21013 }
21014 $sql = "SELECT * FROM {$wpdb->prefix}bm_message_messages ORDER BY id DESC LIMIT 1";
21015 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
21016
21017 if ( ! empty( $results ) ) {
21018 $message = Better_Messages()->functions->get_message( $results[0]['id'] );
21019 if ( is_object( $message ) ) {
21020 $message = get_object_vars( $message );
21021 }
21022 $context['pluggable_data'] = $message;
21023 $context['pluggable_data']['sender'] = WordPress::get_user_context( $results[0]['sender_id'] );
21024 $context['response_type'] = 'live';
21025 } else {
21026 $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
21027 }
21028 return $context;
21029 }
21030
21031 /**
21032 * Get Appointment Hour Booking trigger Last Data
21033 *
21034 * @param array $data data.
21035 *
21036 * @return array|mixed|string
21037 */
21038 public function search_ahb_appointment_booked_triggers_last_data( $data ) {
21039 $context = [];
21040 global $wpdb;
21041 $sql = "SELECT * FROM {$wpdb->prefix}cpappbk_messages ORDER BY id DESC LIMIT 1";
21042 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
21043
21044 if ( ! empty( $results ) ) {
21045 $context['pluggable_data'] = unserialize( $results[0]['posted_data'] );
21046 $context['response_type'] = 'live';
21047 } else {
21048 $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
21049 }
21050 return $context;
21051 }
21052
21053 /**
21054 * Prepare mailmint contact status.
21055 *
21056 * @param array $data Search Params.
21057 *
21058 * @return array
21059 */
21060 public function search_mail_mint_fetch_custom_fields( $data ) {
21061
21062 $options = [
21063 [
21064 'label' => 'Yes',
21065 'value' => 'true',
21066 ],
21067 [
21068 'label' => 'No',
21069 'value' => 'false',
21070 ],
21071 ];
21072
21073 return [
21074 'options' => $options,
21075 'hasMore' => false,
21076 ];
21077 }
21078
21079 /**
21080 * Get Mail Mint custom fields
21081 *
21082 * @param array $data data.
21083 *
21084 * @return array
21085 */
21086 public function search_mail_mint_custom_fields( $data ) {
21087 global $wpdb;
21088 $fields = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'mint_custom_fields' );
21089
21090 return (array) $fields;
21091
21092 }
21093
21094 /**
21095 * Get Mail Mint lists
21096 *
21097 * @param array $data data.
21098 *
21099 * @return array|void
21100 */
21101 public function search_mail_mint_contacts( $data ) {
21102 if ( ! class_exists( 'Mint\MRM\DataBase\Models\ContactModel' ) ) {
21103 return [];
21104 }
21105 $contacts = ContactModel::get_all();
21106 $options = [];
21107 if ( ! empty( $contacts ) ) {
21108 foreach ( $contacts['data'] as $contact ) {
21109 $options[] = [
21110 'label' => $contact['email'],
21111 'value' => $contact['id'],
21112 ];
21113 }
21114 }
21115 return [
21116 'options' => $options,
21117 'hasMore' => false,
21118 ];
21119 }
21120
21121 /**
21122 * Get mail mint Last Data
21123 *
21124 * @param array $data data.
21125 *
21126 * @return array
21127 */
21128 public function search_mail_mint_last_data( $data ) {
21129 if ( ! class_exists( 'Mint\MRM\DataBase\Models\ContactModel' ) || ! class_exists( 'Mint\MRM\DataBase\Models\ContactGroupModel' ) ) {
21130 return [];
21131 }
21132 $context = [];
21133 global $wpdb;
21134 $contact_id = isset( $data['filter']['contact_id']['value'] ) ? $data['filter']['contact_id']['value'] : -1;
21135 $term = $data['search_term'] ? $data['search_term'] : '';
21136 $type = 'lists';
21137 if ( 'mail_mint_tags_added_to_contact' === $term ) {
21138 $type = 'tags';
21139 }
21140 $data = [];
21141 if ( -1 == $contact_id ) {
21142 $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 ) );
21143 } else {
21144 $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 ) );
21145 }
21146
21147 if ( ! empty( $result ) ) {
21148 $contact_id = $result->contact_id;
21149 $data['contact'] = ContactModel::get( $contact_id );
21150 $data[ $type ] = ContactGroupModel::get( $result->id );
21151 }
21152 if ( ! empty( $data ) ) {
21153 $context['pluggable_data'] = $data;
21154 $context['response_type'] = 'live';
21155 } else {
21156 $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 );
21157 }
21158 return (array) $context;
21159 }
21160
21161
21162 /**
21163 * Get Simple Schedule Appointment Types
21164 *
21165 * @param array $data data.
21166 *
21167 * @return array
21168 */
21169 public function search_simple_schedule_appointment_types( $data ) {
21170 global $wpdb;
21171
21172 $appointment_types = $wpdb->get_results( 'SELECT title, id FROM ' . $wpdb->prefix . 'ssa_appointment_types' );
21173 $options = [];
21174
21175 if ( ! empty( $appointment_types ) ) {
21176 foreach ( $appointment_types as $appointment_type ) {
21177 $options[] = [
21178 'label' => $appointment_type->title,
21179 'value' => $appointment_type->id,
21180 ];
21181 }
21182 }
21183 return [
21184 'options' => $options,
21185 'hasMore' => false,
21186 ];
21187 }
21188
21189 /**
21190 * Get mail mint Last Data
21191 *
21192 * @param array $data data.
21193 *
21194 * @return array
21195 */
21196 public function search_ssa_last_data( $data ) {
21197 $context = [];
21198 global $wpdb;
21199 $appointment_type_id = isset( $data['filter']['appointment_type_id']['value'] ) ? $data['filter']['appointment_type_id']['value'] : -1;
21200 $term = $data['search_term'] ? $data['search_term'] : '';
21201 $status = 'booked';
21202 if ( 'ssa_appointment_cancelled' === $term ) {
21203 $status = 'canceled';
21204 }
21205 $data = [];
21206 if ( -1 == $appointment_type_id ) {
21207 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointments WHERE status = %s ORDER BY id DESC Limit 1", $status ), ARRAY_A );
21208 } else {
21209 $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 );
21210 }
21211
21212
21213 if ( ! empty( $result ) ) {
21214 $result_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointment_meta where appointment_id=%d", $result['id'] ) );
21215 if ( ! empty( $result_meta ) ) {
21216 foreach ( $result_meta as $meta ) {
21217 $result[ $meta->meta_key ] = $meta->meta_value;
21218 }
21219 }
21220 }
21221 if ( ! empty( $result ) ) {
21222
21223 $result['customer_information'] = json_decode( $result['customer_information'], true );
21224 $context['pluggable_data'] = $result;
21225 $context['response_type'] = 'live';
21226 } else {
21227 $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 );
21228 }
21229 return (array) $context;
21230 }
21231
21232 /**
21233 * Get Simple Schedule Appointment Types
21234 *
21235 * @param array $data data.
21236 *
21237 * @return array
21238 */
21239 public function search_simple_schedule_appointments( $data ) {
21240 global $wpdb;
21241
21242 $appointment_types = $wpdb->get_results( 'SELECT title, id FROM ' . $wpdb->prefix . 'ssa_appointment_types' );
21243 $options = [];
21244
21245 if ( ! empty( $appointment_types ) ) {
21246 foreach ( $appointment_types as $appointment_type ) {
21247 $options[] = [
21248 'label' => $appointment_type->title,
21249 'value' => $appointment_type->id,
21250 ];
21251 }
21252 }
21253 return [
21254 'options' => $options,
21255 'hasMore' => false,
21256 ];
21257 }
21258
21259 /**
21260 * Get Sensei LMS Lessons
21261 *
21262 * @param array $data data.
21263 *
21264 * @return array
21265 */
21266 public function search_sensei_lms_lessons( $data ) {
21267 if ( ! function_exists( 'Sensei' ) ) {
21268 return [];
21269 }
21270 $course_id = $data['dynamic'];
21271 $lessons = \Sensei()->course->course_lessons( $course_id, 'publish', 'ids' );
21272 $options = [];
21273
21274 if ( ! empty( $lessons ) ) {
21275 foreach ( $lessons as $lesson ) {
21276 $options[] = [
21277 'label' => get_the_title( $lesson ),
21278 'value' => $lesson,
21279 ];
21280 }
21281 }
21282 return [
21283 'options' => $options,
21284 'hasMore' => false,
21285 ];
21286 }
21287
21288 /**
21289 * Get Sensei LMS Quiz List
21290 *
21291 * @param array $data data.
21292 *
21293 * @return array
21294 */
21295 public function search_sensei_lms_quiz( $data ) {
21296 $page = $data['page'];
21297 $limit = Utilities::get_search_page_limit();
21298 $offset = $limit * ( $page - 1 );
21299
21300 $quizes = get_posts(
21301 [
21302 'post_type' => 'lesson',
21303 'orderby' => 'title',
21304 'order' => 'ASC',
21305 'post_status' => 'publish',
21306 'posts_per_page' => $limit,
21307 'offset' => $offset,
21308 'fields' => 'ids',
21309 'meta_query' => [
21310 'relation' => 'AND',
21311 [
21312 'key' => '_lesson_quiz',
21313 'compare' => 'EXISTS',
21314 ],
21315 [
21316 'key' => '_quiz_has_questions',
21317 'value' => 1,
21318 'compare' => '=',
21319 ],
21320 ],
21321 ]
21322 );
21323 $options = [];
21324 $total_quizes = get_posts(
21325 [
21326 'post_type' => 'lesson',
21327 'orderby' => 'title',
21328 'order' => 'ASC',
21329 'post_status' => 'publish',
21330 'posts_per_page' => -1,
21331 'fields' => 'ids',
21332 'meta_query' => [
21333 'relation' => 'AND',
21334 [
21335 'key' => '_lesson_quiz',
21336 'compare' => 'EXISTS',
21337 ],
21338 [
21339 'key' => '_quiz_has_questions',
21340 'value' => 1,
21341 'compare' => '=',
21342 ],
21343 ],
21344 ]
21345 );
21346 $count = count( $total_quizes );
21347 if ( ! empty( $quizes ) ) {
21348 foreach ( $quizes as $quiz ) {
21349 $options[] = [
21350 'label' => get_the_title( $quiz ),
21351 'value' => $quiz,
21352 ];
21353 }
21354 }
21355 return [
21356 'options' => $options,
21357 'hasMore' => $count > $limit && $count > $offset,
21358 ];
21359 }
21360
21361 /**
21362 * Get Sensei LMS trigger Last Data
21363 *
21364 * @param array $data data.
21365 *
21366 * @return array|mixed|string
21367 */
21368 public function search_sensei_lms_triggers_last_data( $data ) {
21369 $context = [];
21370 global $wpdb;
21371 $term = $data['search_term'];
21372
21373 if ( ! function_exists( 'Sensei' ) ) {
21374 return;
21375 }
21376
21377 if ( 'course_completed' == $term ) {
21378 $comment_type = 'sensei_course_status';
21379 $course_id = $data['filter']['sensei_course']['value'];
21380 if ( -1 === $course_id ) {
21381 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s ORDER BY comment_ID DESC LIMIT 1";
21382 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type ), ARRAY_A );// @phpcs:ignore
21383 } else {
21384 $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";
21385 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, $course_id ), ARRAY_A );// @phpcs:ignore
21386 }
21387 } elseif ( 'course_enrolled' == $term ) {
21388 $comment_type = 'sensei_course_status';
21389 $course_id = $data['filter']['sensei_course']['value'];
21390 if ( -1 === $course_id ) {
21391 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s ORDER BY comment_ID DESC LIMIT 1";
21392 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'in-progress', $comment_type ), ARRAY_A );// @phpcs:ignore
21393 } else {
21394 $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";
21395 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'in-progress', $comment_type, $course_id ), ARRAY_A );// @phpcs:ignore
21396 }
21397 } elseif ( 'lesson_completed' == $term ) {
21398 $lesson_id = $data['filter']['sensei_lesson']['value'];
21399 $course_id = $data['filter']['sensei_course']['value'];
21400 $comment_type = 'sensei_lesson_status';
21401 if ( -1 === $lesson_id ) {
21402 $sql = "SELECT comments.*, meta.* FROM {$wpdb->prefix}comments as comments
21403 LEFT JOIN {$wpdb->prefix}postmeta as meta
21404 ON comments.comment_post_ID = meta.post_id
21405 WHERE comments.comment_approved = %s
21406 AND comments.comment_type = %s
21407 AND meta.meta_key = %s AND
21408 meta.meta_value = %d
21409 ORDER BY comments.comment_ID DESC LIMIT 1";
21410 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, '_lesson_course', $course_id ), ARRAY_A );// @phpcs:ignore
21411 } else {
21412 $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";
21413 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, $lesson_id ), ARRAY_A );// @phpcs:ignore
21414 }
21415 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term ) {
21416 $quiz_id = $data['filter']['sensei_quiz']['value'];
21417 $comment_type = 'sensei_lesson_status';
21418 if ( 'quiz_passes' == $term ) {
21419 $status = "( comment_approved = 'passed' )";
21420 } elseif ( 'quiz_fails' == $term ) {
21421 $status = "( comment_approved = 'failed' )";
21422 } else {
21423 $status = "( comment_approved = 'failed' OR comment_approved = 'passed' )";
21424 }
21425 if ( -1 === $quiz_id ) {
21426 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type = %s AND $status ORDER BY comment_ID DESC LIMIT 1";
21427 $results = $wpdb->get_results( $wpdb->prepare( $sql, $comment_type ), ARRAY_A );// @phpcs:ignore
21428 } else {
21429 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type = %s AND $status AND comment_post_ID = %d ORDER BY comment_ID DESC LIMIT 1";
21430 $results = $wpdb->get_results( $wpdb->prepare( $sql, $comment_type, $quiz_id ), ARRAY_A );// @phpcs:ignore
21431 }
21432 } elseif ( 'quiz_completion' == $term ) {
21433 $quiz_id = $data['filter']['sensei_quiz']['value'];
21434 $comment_type = 'sensei_lesson_status';
21435 $condition_compare = $data['filter']['condition_compare']['value'];
21436 $percentage = $data['filter']['percentage']['value'];
21437 if ( -1 === $quiz_id ) {
21438 $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
21439 } else {
21440 $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
21441 }
21442 }
21443
21444 if ( ! empty( $results ) ) {
21445 $post = get_post( $results[0]['comment_post_ID'] );
21446 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['user_id'] );
21447 if ( 'course_completed' == $term || 'course_enrolled' == $term ) {
21448 $context['pluggable_data']['sensei_course'] = $results[0]['comment_post_ID'];
21449 if ( $post instanceof \WP_Post ) {
21450 $context['pluggable_data']['course_title'] = $post->post_title;
21451 }
21452 } elseif ( 'lesson_completed' == $term ) {
21453 $context['pluggable_data']['sensei_lesson'] = $results[0]['comment_post_ID'];
21454 if ( $post instanceof \WP_Post ) {
21455 $context['pluggable_data']['lesson_title'] = $post->post_title;
21456 }
21457 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term || 'quiz_completion' == $term ) {
21458 $lesson_quiz = get_post_meta( $results[0]['comment_post_ID'], '_lesson_quiz', true );
21459 $submission = \Sensei()->quiz_submission_repository->get( $lesson_quiz, $results[0]['user_id'] );
21460 if ( $post instanceof \WP_Post ) {
21461 $context['pluggable_data']['quiz_title'] = $post->post_title;
21462 }
21463 $context['pluggable_data']['quiz_status'] = $results[0]['comment_approved'];
21464 $context['pluggable_data']['quiz_data']['id'] = $submission->get_id();
21465 $context['sensei_quiz'] = $submission->get_id();
21466 $context['pluggable_data']['quiz_data']['final_grade'] = $submission->get_final_grade();
21467 $context['pluggable_data']['quiz_data']['created_at'] = $submission->get_created_at();
21468 }
21469 $context['response_type'] = 'live';
21470 } else {
21471 if ( 'course_completed' == $term || 'course_enrolled' == $term ) {
21472 $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
21473 } elseif ( 'lesson_completed' == $term ) {
21474 $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
21475 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term || 'quiz_completion' == $term ) {
21476 $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
21477 }
21478 }
21479 return $context;
21480 }
21481
21482 /**
21483 * Get SurelyWP Services - SureCart Addons Last Data
21484 *
21485 * @param array $data data.
21486 *
21487 * @return array
21488 */
21489 public function search_services_sc_triggers_last_data( $data ) {
21490 $context = [];
21491 global $wpdb;
21492 $term = $data['search_term'] ? $data['search_term'] : '';
21493 $data = [];
21494
21495 if ( 'new_service_created' === $term ) {
21496 $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 );
21497 } elseif ( 'requirement_submitted' === $term ) {
21498 $result = $wpdb->get_row( "SELECT created_at FROM {$wpdb->prefix}surelywp_sv_requirements ORDER BY created_at DESC Limit 1", ARRAY_A );
21499 } elseif ( 'message_sent' === $term ) {
21500 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_messages ORDER BY message_id DESC Limit 1", ARRAY_A );
21501 } elseif ( 'message_final_delivery_sent' === $term ) {
21502 $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 );
21503 } elseif ( 'customer_request_revision' === $term ) {
21504 $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 );
21505 } elseif ( 'customer_approves_final_delivery' === $term ) {
21506 $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 );
21507 } elseif ( 'delivery_date_changed' === $term ) {
21508 $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 );
21509 } elseif ( 'service_cancel' === $term || 'service_marked_canceled' === $term ) {
21510 $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 );
21511 } elseif ( 'service_completed' === $term || 'service_mark_completed' === $term ) {
21512 $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 );
21513 } elseif ( 'contract_signed' === $term ) {
21514 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_contracts ORDER BY contract_id DESC Limit 1", ARRAY_A );
21515 }
21516 if ( ! empty( $result ) ) {
21517 if ( 'new_service_created' === $term ) {
21518 $service_data = [
21519 'service_setting_id' => $result['service_id'],
21520 'order_id' => $result['order_id'],
21521 'product_id' => $result['product_id'],
21522 'service_status' => $result['service_status'],
21523 'delivery_date' => $result['delivery_date'],
21524 ];
21525 $context['pluggable_data'] = array_merge( $service_data, WordPress::get_user_context( $result['user_id'] ) );
21526 $context['response_type'] = 'live';
21527 } elseif ( 'requirement_submitted' === $term ) {
21528 $requirements_data = $wpdb->get_results(
21529 $wpdb->prepare(
21530 "SELECT * FROM {$wpdb->prefix}surelywp_sv_requirements WHERE created_at = %s",
21531 $result['created_at']
21532 ),
21533 ARRAY_A
21534 );
21535 $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 );
21536 $user_data = WordPress::get_user_context( $service_result['user_id'] );
21537 unset( $service_result['user_id'] );
21538 $context['pluggable_data'] = array_merge( $requirements_data, $service_result, $user_data );
21539 foreach ( $requirements_data as $value ) {
21540 if ( 'file' == $value['requirement_type'] ) {
21541 $upload_dir = wp_upload_dir();
21542 $attachment_file_names = json_decode( $value['requirement'], true );
21543 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
21544 $context['pluggable_data']['requirement_attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $value['service_id'] . '/requirement/' . $attachment_file_name;
21545 }
21546 }
21547 }
21548 $context['response_type'] = 'live';
21549 } elseif ( 'message_sent' === $term || 'message_final_delivery_sent' === $term ) {
21550 $message_data = [
21551 'sender' => WordPress::get_user_context( $result['sender_id'] ),
21552 'receiver' => WordPress::get_user_context( $result['receiver_id'] ),
21553 'service_id' => $result['service_id'],
21554 'message_text' => $result['message_text'],
21555 'attachment_file_name' => $result['attachment_file_name'],
21556 'is_final_delivery' => $result['is_final_delivery'],
21557 ];
21558 $context['pluggable_data'] = $message_data;
21559 $upload_dir = wp_upload_dir();
21560 $attachment_file_names = json_decode( $result['attachment_file_name'], true );
21561 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
21562 $context['pluggable_data']['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $result['service_id'] . '/messages/' . $attachment_file_name;
21563 }
21564 $context['response_type'] = 'live';
21565 } elseif ( 'customer_request_revision' === $term ) {
21566 $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 );
21567 if ( ! empty( $message_result ) ) {
21568 global $surelywp_sv_model;
21569 $revision_message = $surelywp_sv_model->surelywp_sv_get_customer_revision_msg( $message_result['service_id'], $message_result['message_id'] );
21570 $context['pluggable_data'] = array_merge( $result, $revision_message );
21571 $context['pluggable_data']['sender'] = WordPress::get_user_context( $revision_message['sender_id'] );
21572 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $revision_message['receiver_id'] );
21573 $upload_dir = wp_upload_dir();
21574 $attachment_file_names = json_decode( $revision_message['attachment_file_name'], true );
21575 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
21576 $context['pluggable_data']['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $revision_message['service_id'] . '/messages/' . $attachment_file_name;
21577 }
21578 $context['response_type'] = 'live';
21579 } else {
21580 $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 );
21581 }
21582 } elseif ( 'customer_approves_final_delivery' === $term || 'delivery_date_changed' === $term || 'service_cancel' === $term || 'service_marked_canceled' === $term || 'service_completed' === $term || 'service_mark_completed' === $term ) {
21583 $user_data = WordPress::get_user_context( $result['user_id'] );
21584 unset( $result['user_id'] );
21585 $context['pluggable_data'] = array_merge( $result, $user_data );
21586 $context['response_type'] = 'live';
21587 } elseif ( 'contract_signed' === $term ) {
21588 $contract_data = [
21589 'service_id' => $result['service_id'],
21590 'signature' => $result['signature'],
21591 'contract_details' => $result['contract_details'],
21592 ];
21593 $service_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_id = %d", $result['service_id'] ), ARRAY_A );
21594 $user_data = WordPress::get_user_context( $service_result['user_id'] );
21595 unset( $service_result['user_id'] );
21596 $context['pluggable_data'] = array_merge( $contract_data, $user_data, $service_result );
21597 $context['response_type'] = 'live';
21598 }
21599 } else {
21600 if ( 'new_service_created' === $term || 'customer_request_revision' === $term || 'customer_approves_final_delivery' === $term || 'delivery_date_changed' === $term ) {
21601 $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 );
21602 } elseif ( 'requirement_submitted' === $term ) {
21603 $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 );
21604 } elseif ( 'message_sent' === $term || 'message_final_delivery_sent' === $term ) {
21605 $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 );
21606 } elseif ( 'service_cancel' === $term ) {
21607 $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 );
21608 } elseif ( 'service_marked_canceled' === $term || 'service_completed' === $term || 'service_mark_completed' === $term ) {
21609 $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 );
21610 } elseif ( 'contract_signed' === $term ) {
21611 $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 );
21612 }
21613 }
21614 return (array) $context;
21615 }
21616
21617 /**
21618 * Get SurelyWP Support Portal - SureCart Addons Last Data
21619 *
21620 * @param array $data data.
21621 *
21622 * @return array
21623 */
21624 public function search_sc_support_portal_triggers_last_data( $data ) {
21625 $context = [];
21626 if ( ! class_exists( 'Surelywp_Support_Portal' ) ) {
21627 return [];
21628 }
21629 global $wpdb;
21630 $term = $data['search_term'] ? $data['search_term'] : '';
21631 $data = [];
21632
21633 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
21634 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sp_support WHERE support_status = 1 ORDER BY support_id DESC Limit 1", ARRAY_A );
21635 } elseif ( 'new_message_sent' === $term ) {
21636 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sp_messages ORDER BY message_id DESC Limit 1", ARRAY_A );
21637 } elseif ( 'tickets_closed' === $term ) {
21638 $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 );
21639 } elseif ( 'tickets_opens' === $term ) {
21640 $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 );
21641 } elseif ( 'new_email_message_fetched' === $term ) {
21642 $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 );
21643 }
21644 if ( ! empty( $result ) ) {
21645 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
21646 $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 );
21647 $support_data = [
21648 'support_id' => $result['support_id'],
21649 'order_id' => $result['order_id'],
21650 'product_id' => $result['product_id'],
21651 'support_title' => $result['support_title'],
21652 'support_status' => Surelywp_Support_Portal::surelywp_sp_get_support_status( $result['support_status'] ),
21653 'support_data' => $support_res,
21654 ];
21655 $context['pluggable_data'] = array_merge( $support_data, WordPress::get_user_context( $result['user_id'] ) );
21656 $context['response_type'] = 'live';
21657 } elseif ( 'tickets_closed' === $term || 'tickets_opens' === $term ) {
21658 $support_data = [];
21659 $support_ids = $result;
21660 foreach ( $support_ids as $key => $id ) {
21661 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sp_support WHERE support_id = %d", $id ), ARRAY_A );
21662 $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 );
21663 $support_data[ $key ] = WordPress::get_user_context( $result['user_id'] );
21664 $support_data[ $key ] = [
21665 'support_id' => $result['support_id'],
21666 'order_id' => $result['order_id'],
21667 'product_id' => $result['product_id'],
21668 'support_title' => $result['support_title'],
21669 'support_status' => Surelywp_Support_Portal::surelywp_sp_get_support_status( $result['support_status'] ),
21670 'support_data' => $support_res,
21671 ];
21672 }
21673 $context['pluggable_data'] = $support_data;
21674 $context['response_type'] = 'live';
21675 } elseif ( 'new_message_sent' === $term ) {
21676 $upload_dir = wp_upload_dir();
21677 $support_data = [
21678 'sender_id' => $result['sender_id'],
21679 'receiver_id' => $result['receiver_id'],
21680 'support_id' => $result['support_id'],
21681 'message_text' => $result['message_text'],
21682 'sender' => WordPress::get_user_context( $result['sender_id'] ),
21683 'receiver' => WordPress::get_user_context( $result['receiver_id'] ),
21684 ];
21685 if ( ! empty( $result['attachment_file_name'] ) ) {
21686 $attachment_file_names = json_decode( $result['attachment_file_name'], true );
21687 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
21688 $support_data['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-support-portal-data/' . $result['support_id'] . '/messages/' . $attachment_file_name;
21689 }
21690 }
21691 $context['pluggable_data'] = $support_data;
21692 $context['response_type'] = 'live';
21693 }
21694 } else {
21695 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
21696 $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 );
21697 } elseif ( 'tickets_closed' === $term || 'tickets_opens' === $term ) {
21698 $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 );
21699 } elseif ( 'new_message_sent' === $term ) {
21700 $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 );
21701 }
21702 }
21703 return (array) $context;
21704 }
21705
21706 /**
21707 * Get Fluent Boards Last Data
21708 *
21709 * @param array $data data.
21710 *
21711 * @return array
21712 */
21713 public function search_fbs_triggers_last_data( $data ) {
21714 if ( ! class_exists( '\FluentBoards\App\Models\Stage' ) ) {
21715 return [];
21716 }
21717 global $wpdb;
21718
21719 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
21720 if ( ! class_exists( 'FluentBoards\App\Models\Board' ) || ! class_exists( 'FluentBoards\App\Models\User' ) ) {
21721 return [];
21722 }
21723
21724 $context = [];
21725 $result = null;
21726
21727 switch ( $term ) {
21728 case 'board_created':
21729 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_boards ORDER BY id DESC LIMIT 1", ARRAY_A );
21730 break;
21731 case 'board_member_added':
21732 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_relations WHERE object_type = 'board_user' ORDER BY id DESC LIMIT 1", ARRAY_A );
21733 break;
21734 case 'task_created':
21735 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_tasks ORDER BY id DESC LIMIT 1", ARRAY_A );
21736 break;
21737 case 'stage_changed':
21738 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_tasks ORDER BY id DESC LIMIT 1", ARRAY_A );
21739 break;
21740
21741 }
21742
21743 if ( ! empty( $result ) ) {
21744 switch ( $term ) {
21745 case 'board_created':
21746 $context['pluggable_data'] = \FluentBoards\App\Models\Board::findOrFail( $result['id'] );
21747 $context['response_type'] = 'live';
21748 break;
21749 case 'board_member_added':
21750 $context['pluggable_data']['board_id'] = $result['object_id'];
21751 $context['pluggable_data']['board_member'] = \FluentBoards\App\Models\User::find( $result['foreign_id'] );
21752 $context['response_type'] = 'live';
21753 break;
21754 case 'task_created':
21755 $stage_data = Stage::where( 'board_id', $result['board_id'] )->whereNull( 'archived_at' )->first();
21756
21757 $context['pluggable_data'] = [
21758 'id' => $result['id'],
21759 'slug' => $result['slug'],
21760 'title' => $result['title'],
21761 'description' => $result['description'],
21762 'type' => $result['type'],
21763 'board_id' => $result['board_id'],
21764 'stage_id' => $result['stage_id'],
21765 'position' => $result['position'],
21766 'priority' => $result['priority'],
21767 'created_at' => $result['created_at'],
21768 'created_by' => $result['created_by'],
21769 'updated_at' => $result['updated_at'],
21770 'settings' => unserialize( $result['settings'] ),
21771 'stage' => $stage_data ? [
21772 'id' => $stage_data->id,
21773 'slug' => $stage_data->slug,
21774 'title' => $stage_data->title,
21775 'type' => $stage_data->type,
21776 'board_id' => $stage_data->board_id,
21777 'position' => $stage_data->position,
21778 'settings' => $stage_data->settings,
21779 'created_at' => '',
21780 'updated_at' => '',
21781 ] : null,
21782 ];
21783 $context['response_type'] = 'live';
21784 break;
21785 case 'stage_changed':
21786 $stage_data = Stage::where( 'board_id', $result['board_id'] )->whereNull( 'archived_at' )->first();
21787 $user_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}users ORDER BY id DESC LIMIT 1", ARRAY_A );
21788
21789 $context['pluggable_data'] = [
21790 'task' => [
21791 'id' => $result['id'],
21792 'slug' => $result['slug'],
21793 'title' => $result['title'],
21794 'description' => $result['description'],
21795 'type' => $result['type'],
21796 'board_id' => $result['board_id'],
21797 'stage_id' => $stage_data->id,
21798 'position' => $result['position'],
21799 'priority' => $result['priority'],
21800 'created_at' => $result['created_at'],
21801 'created_by' => $result['created_by'],
21802 'updated_at' => $result['updated_at'],
21803 'settings' => unserialize( $result['settings'] ),
21804 'stage' => [
21805 'id' => $stage_data->id,
21806 'slug' => $stage_data->slug,
21807 'title' => $stage_data->title,
21808 'type' => $stage_data->type,
21809 'board_id' => $stage_data->board_id,
21810 'position' => $stage_data->position,
21811 'settings' => $stage_data->settings,
21812 'created_at' => '',
21813 'updated_at' => '',
21814 ],
21815 'watchers' => [
21816 [
21817 'ID' => $user_result['ID'],
21818 'photo' => '',
21819 'user_url' => $user_result['user_url'],
21820 'user_email' => $user_result['user_email'],
21821 'user_login' => $user_result['user_login'],
21822 'user_status' => $user_result['user_status'],
21823 'display_name' => $user_result['display_name'],
21824 'user_nicename' => $user_result['user_nicename'],
21825 'user_registered' => $user_result['user_registered'],
21826 'pivot' => [
21827 'object_id' => $user_result['id'],
21828 'created_at' => $user_result['created_at'],
21829 'foreign_id' => $user_result['ID'],
21830 'user_id' => $user_result['ID'],
21831 ],
21832 ],
21833 ],
21834 ],
21835 'old_stage_id' => $stage_data->id,
21836 ];
21837 $context['response_type'] = 'live';
21838 break;
21839 }
21840 } else {
21841 switch ( $term ) {
21842 case 'board_created':
21843 $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 );
21844 break;
21845 case 'board_member_added':
21846 $context = json_decode( '{"pluggable_data":{"board_id":"2","board_member":{"ID":1,"user_login":"johnd","display_name":"johnd"}},"response_type":"sample"}', true );
21847 break;
21848 case 'task_created':
21849 $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 );
21850 break;
21851 case 'stage_changed':
21852 $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 );
21853 break;
21854
21855 }
21856 }
21857
21858 return (array) $context;
21859 }
21860
21861
21862 /**
21863 * Prepare FluentBoards Boards List.
21864 *
21865 * @param array $data Search Params.
21866 * @return array
21867 */
21868 public function search_fbs_boards_list( $data ) {
21869 global $wpdb;
21870
21871 $boards = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fbs_boards", ARRAY_A );
21872
21873 $options = [];
21874
21875 if ( ! empty( $boards ) ) {
21876 foreach ( $boards as $board ) {
21877 $options[] = [
21878 'label' => $board['title'],
21879 'value' => $board['id'],
21880 ];
21881 }
21882 }
21883
21884 return [
21885 'options' => $options,
21886 'hasMore' => false,
21887 ];
21888 }
21889
21890 /**
21891 * Prepare FluentBoards Subtask Groups List.
21892 *
21893 * @param array $data Search Params.
21894 * @return array
21895 */
21896 public function search_fbs_subtask_groups_list( $data ) {
21897 global $wpdb;
21898
21899 $task_id = isset( $data['dynamic'] ) ? sanitize_text_field( $data['dynamic'] ) : '';
21900
21901 $groups = $wpdb->get_results(
21902 $wpdb->prepare(
21903 "SELECT id, value FROM {$wpdb->prefix}fbs_task_metas WHERE task_id = %d AND `key` = %s",
21904 $task_id,
21905 'group_name'
21906 )
21907 );
21908
21909 $options = [];
21910
21911 foreach ( $groups as $group ) {
21912 $options[] = [
21913 'label' => $group->value,
21914 'value' => $group->id,
21915 ];
21916 }
21917
21918 return [
21919 'options' => $options,
21920 'hasMore' => false,
21921 ];
21922 }
21923
21924 /**
21925 * Prepare FluentBoards Stages List.
21926 *
21927 * @param array $data Search Params.
21928 * @return array
21929 */
21930 public function search_fbs_stages_list( $data ) {
21931 if ( ! class_exists( '\FluentBoards\App\Models\Stage' ) ) {
21932 return [];
21933 }
21934 $query = Stage::where( 'board_id', $data['dynamic'] )->whereNull( 'archived_at' );
21935 $stages = $query->get();
21936 $options = [];
21937
21938 if ( ! empty( $stages ) ) {
21939 foreach ( $stages as $stage ) {
21940 $options[] = [
21941 'label' => $stage['title'],
21942 'value' => $stage['id'],
21943 ];
21944 }
21945 }
21946
21947 return [
21948 'options' => $options,
21949 'hasMore' => false,
21950 ];
21951 }
21952
21953 /**
21954 * Get Profile Grid Last Data
21955 *
21956 * @param array $data data.
21957 *
21958 * @return array
21959 */
21960 public function search_profile_grid_triggers_last_data( $data ) {
21961 $context = [];
21962 global $wpdb;
21963 $term = $data['search_term'] ? $data['search_term'] : '';
21964
21965 if ( 'user_added_group' === $term || 'user_removed_from_group' === $term || 'user_assigned_group_manager' === $term || 'user_unassigned_group_manager' === $term || 'membership_request_approved' === $term ) {
21966 $users = get_users(
21967 [
21968 'orderby' => 'meta_value',
21969 'meta_key' => 'pm_group',
21970 'order' => 'DESC',
21971 'number' => 1,
21972 'meta_query' => [
21973 [
21974 'key' => 'pm_group',
21975 'compare' => 'EXISTS',
21976 ],
21977 ],
21978 'fields' => 'ids',
21979 ]
21980 );
21981 } elseif ( 'payment_complete' === $term || 'payment_failed' === $term ) {
21982 $users = get_users(
21983 [
21984 'orderby' => 'meta_value',
21985 'meta_key' => 'pm_group_payment_status',
21986 'order' => 'DESC',
21987 'number' => 1,
21988 'meta_query' => [
21989 [
21990 'key' => 'pm_group_payment_status',
21991 'compare' => 'EXISTS',
21992 ],
21993 ],
21994 'fields' => 'ids',
21995 ]
21996 );
21997 } elseif ( 'membership_request' === $term ) {
21998 $results = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}promag_group_requests WHERE status = 1 ORDER BY id DESC Limit 1", ARRAY_A );
21999 }
22000 if ( ! empty( $users ) ) {
22001 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 ) {
22002 $context = WordPress::get_user_context( $users[0] );
22003 $user_groups = get_user_meta( $users[0], 'pm_group', true );
22004 if ( is_array( $user_groups ) ) {
22005 $last_element = end( $user_groups );
22006 $context['group_id'] = $last_element;
22007 $context = array_merge( $context, ProfileGrid::pg_group_details( $last_element ) );
22008 }
22009 $context['pluggable_data'] = $context;
22010 $context['response_type'] = 'live';
22011 }
22012 } elseif ( ! empty( $results ) && 'membership_request' === $term ) {
22013 $context = WordPress::get_user_context( $results[0]['uid'] );
22014 $context['group_id'] = $results[0]['gid'];
22015 $context = array_merge( $context, ProfileGrid::pg_group_details( $results[0]['gid'] ) );
22016 $context['pluggable_data'] = $context;
22017 $context['response_type'] = 'live';
22018 } elseif ( 'group_manager_resets_password' === $term ) {
22019 $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 );
22020 } elseif ( 'group_deleted' === $term ) {
22021 $context = json_decode( '{"pluggable_data":{"group_name":"Test Group","group_description":"Testing Group","group_id":"2"},"response_type":"sample"}', true );
22022 } else {
22023 $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 );
22024 }
22025 return (array) $context;
22026 }
22027
22028 /**
22029 * Get Fluent SMTP Last Data
22030 *
22031 * @param array $data data.
22032 *
22033 * @return array
22034 */
22035 public function search_fluent_smtp_last_data( $data ) {
22036 $context = [];
22037 global $wpdb;
22038 $results = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fsmpt_email_logs WHERE status = 'failed' ORDER BY id DESC Limit 1", ARRAY_A );
22039 if ( ! empty( $results ) ) {
22040 $context['to'] = unserialize( $results['to'] );
22041 $context['from'] = $results['from'];
22042 $context['subject'] = $results['subject'];
22043 $context['body'] = $results['body'];
22044 $context['attachments'] = unserialize( $results['attachments'] );
22045 $context['status'] = $results['status'];
22046 $context['response'] = unserialize( $results['response'] );
22047 $context['headers'] = unserialize( $results['headers'] );
22048 $context['extra'] = unserialize( $results['extra'] );
22049 $context['pluggable_data'] = $context;
22050 $context['response_type'] = 'live';
22051 } else {
22052 $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 );
22053 }
22054 return (array) $context;
22055 }
22056
22057 /**
22058 * Get Fluent Community Last Data
22059 *
22060 * @param array $data data.
22061 *
22062 * @return array
22063 */
22064 public function search_fc_triggers_last_data( $data ) {
22065 $context = [];
22066 $space_result = [];
22067 $profile_result = [];
22068 $user_result = [];
22069 $feed_data = [];
22070 $course_result = [];
22071 global $wpdb;
22072 $term = $data['search_term'] ? $data['search_term'] : '';
22073
22074 $data = [];
22075
22076 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 ) {
22077 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_spaces ORDER BY id DESC LIMIT 1", ARRAY_A );
22078 $space_result = [
22079 'id' => $result['id'],
22080 'slug' => $result['slug'],
22081 'title' => $result['title'],
22082 'description' => $result['description'],
22083 'type' => $result['type'],
22084 'privacy' => $result['privacy'],
22085 'status' => $result['status'],
22086 'serial' => $result['serial'],
22087 'created_at' => $result['created_at'],
22088 'created_by' => $result['created_by'],
22089 'updated_at' => $result['updated_at'],
22090 'settings' => unserialize( $result['settings'] ),
22091 ];
22092 }
22093
22094 if ( 'feed_created' === $term || 'feed_updated' === $term || 'space_feed_created' === $term ) {
22095 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_posts ORDER BY id DESC LIMIT 1", ARRAY_A );
22096 $profile_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_xprofile ORDER BY id DESC LIMIT 1", ARRAY_A );
22097 $user_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}users ORDER BY id DESC LIMIT 1", ARRAY_A );
22098 $feed_data = [
22099 'feed' => [
22100 'id' => $result['id'],
22101 'user_id' => $result['user_id'],
22102 'parentid' => $result['parent_id'],
22103 'title' => $result['title'],
22104 'slug' => $result['slug'],
22105 'message' => $result['message'],
22106 'message_rendered' => $result['message_rendered'],
22107 'type' => $result['type'],
22108 'content_type' => $result['content_type'],
22109 'space_id' => $result['space_id'],
22110 'privacy' => $result['privacy'],
22111 'status' => $result['status'],
22112 'featured_image' => $result['featured_image'],
22113 'meta' => unserialize( $result['meta'] ),
22114 'issticky' => $result['issticky'],
22115 'comments_count' => $result['comments_count'],
22116 'reactions_count' => $result['reactions_count'],
22117 'priority' => $result['priority'],
22118 'expiredat' => $result['expired_at'],
22119 'scheduledat' => $result['scheduled_at'],
22120 'created_at' => $result['created_at'],
22121 'updated_at' => $result['updated_at'],
22122 'space' => $space_result,
22123 'xprofile' => [
22124 'id' => $profile_result['id'],
22125 'user_id' => $profile_result['user_id'],
22126 'total_points' => $profile_result['total_points'],
22127 'username' => $profile_result['username'],
22128 'status' => $profile_result['status'],
22129 'is_verified' => $profile_result['is_verified'],
22130 'display_name' => $profile_result['display_name'],
22131 'avatar' => $profile_result['avatar'],
22132 'short_description' => $profile_result['short_description'],
22133 'last_activity' => $profile_result['last_activity'],
22134 'meta' => unserialize( $profile_result['meta'] ),
22135 'created_at' => $profile_result['created_at'],
22136 'updated_at' => $profile_result['updated_at'],
22137 ],
22138 ],
22139 ];
22140 }
22141
22142 if ( 'user_joins_space' === $term ) {
22143 $user_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}users ORDER BY id DESC LIMIT 1", ARRAY_A );
22144 }
22145
22146 if ( 'course_created' === $term || 'course_published' === $term || 'course_updated' === $term || 'user_enrolls_course' === $term || 'user_unenrolls_course' === $term || 'user_completes_course' === $term ) {
22147 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_spaces WHERE type = 'course' ORDER BY id DESC LIMIT 1", ARRAY_A );
22148 $course_result = [
22149 'id' => $result['id'],
22150 'slug' => $result['slug'],
22151 'title' => $result['title'],
22152 'serial' => $result['serial'],
22153 'status' => $result['status'],
22154 'privacy' => $result['privacy'],
22155 'created_at' => $result['created_at'],
22156 'created_by' => $result['created_by'],
22157 'updated_at' => $result['updated_at'],
22158 'description' => $result['description'],
22159 'settings' => unserialize( $result['settings'] ),
22160 ];
22161 }
22162
22163 if ( 'course_deleted' === $term ) {
22164 $result = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}fcom_spaces WHERE type = 'course' ORDER BY id DESC LIMIT 1" );
22165 }
22166
22167 if ( 'new_comment_added' === $term || 'comment_updated' === $term ) {
22168 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_post_comments ORDER BY id DESC LIMIT 1", ARRAY_A );
22169 $profile_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_xprofile ORDER BY id DESC Limit 1", ARRAY_A );
22170 }
22171
22172 if ( 'comment_deleted' === $term ) {
22173 $result = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}fcom_post_comments ORDER BY id DESC LIMIT 1" );
22174 }
22175
22176 if ( ! empty( $result ) ) {
22177 if ( 'space_created' === $term ) {
22178 $context['pluggable_data'] = [ 'space' => $space_result ];
22179 $context['response_type'] = 'live';
22180 }
22181
22182 if ( 'feed_created' === $term || 'feed_updated' === $term ) {
22183 $context['pluggable_data'] = $feed_data;
22184 $context['response_type'] = 'live';
22185 }
22186
22187 if ( 'space_feed_created' === $term ) {
22188 $feed_data['feed']['user'] = [
22189 'user_url' => $user_result['user_url'],
22190 'user_email' => $user_result['user_email'],
22191 'user_login' => $user_result['user_login'],
22192 'user_status' => $user_result['user_status'],
22193 'display_name' => $user_result['display_name'],
22194 'user_nicename' => $user_result['user_nicename'],
22195 'user_registered' => $user_result['user_registered'],
22196 ];
22197 $context['pluggable_data'] = $feed_data;
22198 $context['response_type'] = 'live';
22199 }
22200
22201 if ( 'user_joins_space' === $term ) {
22202 $context['pluggable_data'] = [
22203 'space' => [
22204 'id' => $result['id'],
22205 'slug' => $result['slug'],
22206 'title' => $result['title'],
22207 'description' => $result['description'],
22208 'type' => $result['type'],
22209 'privacy' => $result['privacy'],
22210 'status' => $result['status'],
22211 'serial' => $result['serial'],
22212 'created_at' => $result['created_at'],
22213 'created_by' => $result['created_by'],
22214 'updated_at' => $result['updated_at'],
22215 'settings' => unserialize( $result['settings'] ),
22216 'membership' => [
22217 'ID' => $user_result['ID'],
22218 'photo' => '',
22219 'pivot_role' => 'admin',
22220 'pivot_status' => 'active',
22221 'pivot_created_at' => $result['created_at'],
22222 'pivot_user_id' => $user_result['ID'],
22223 'pivot_space_id' => $result['id'],
22224 'user_url' => $user_result['user_url'],
22225 'user_email' => $user_result['user_email'],
22226 'user_login' => $user_result['user_login'],
22227 'user_status' => $user_result['user_status'],
22228 'display_name' => $user_result['display_name'],
22229 'user_nicename' => $user_result['user_nicename'],
22230 'user_registered' => $user_result['user_registered'],
22231 ],
22232 ],
22233 'userId' => 1,
22234 'by' => 'self',
22235 ];
22236 $context['response_type'] = 'live';
22237 }
22238 if ( 'user_leaves_space' === $term ) {
22239 $context['pluggable_data'] = [
22240 'space' => $space_result,
22241 'user' => WordPress::get_sample_user_context(),
22242 'userId' => 1,
22243 'by' => 'self',
22244 ];
22245 $context['response_type'] = 'live';
22246 }
22247 if ( 'course_created' === $term || 'course_updated' === $term || 'course_published' === $term ) {
22248 $context['pluggable_data'] = [
22249 'course' => $course_result,
22250 ];
22251 $context['response_type'] = 'live';
22252 }
22253
22254 if ( 'course_deleted' === $term ) {
22255 $context['pluggable_data'] = [ 'course' => $result ];
22256 $context['response_type'] = 'live';
22257 }
22258
22259 if ( 'comment_deleted' === $term ) {
22260 $context['pluggable_data'] = [ 'comment_id' => $result ];
22261 $context['response_type'] = 'live';
22262 }
22263 if ( 'new_comment_added' === $term || 'comment_updated' === $term ) {
22264 $context['pluggable_data'] = [
22265 'comment' => [
22266 'id' => $result['id'],
22267 'user_id' => $result['user_id'],
22268 'post_id' => $result['post_id'],
22269 'parent_id' => $result['parent_id'],
22270 'reactions_count' => $result['reactions_count'],
22271 'message' => $result['message'],
22272 'message_rendered' => $result['message_rendered'],
22273 'meta' => unserialize( $result['meta'] ),
22274 'type' => $result['type'],
22275 'content_type' => $result['content_type'],
22276 'status' => $result['status'],
22277 'is_sticky' => $result['is_sticky'],
22278 'created_at' => $result['created_at'],
22279 'updated_at' => $result['updated_at'],
22280 'xprofile' => [
22281 'id' => $profile_result['id'],
22282 'user_id' => $profile_result['user_id'],
22283 'total_points' => $profile_result['total_points'],
22284 'username' => $profile_result['username'],
22285 'status' => $profile_result['status'],
22286 'is_verified' => $profile_result['is_verified'],
22287 'display_name' => $profile_result['display_name'],
22288 'avatar' => $profile_result['avatar'],
22289 'short_description' => $profile_result['short_description'],
22290 'last_activity' => $profile_result['last_activity'],
22291 'meta' => unserialize( $profile_result['meta'] ),
22292 'created_at' => $profile_result['created_at'],
22293 'updated_at' => $profile_result['updated_at'],
22294 ],
22295 ],
22296 ];
22297 $context['response_type'] = 'live';
22298 }
22299 if ( 'user_enrolls_course' === $term || 'user_unenrolls_course' === $term || 'user_completes_course' === $term ) {
22300 $context['pluggable_data'] = [
22301 'course' => $course_result,
22302 'user' => WordPress::get_sample_user_context(),
22303 'userId' => 1,
22304 ];
22305 $context['response_type'] = 'live';
22306 }
22307 if ( 'user_requests_join_space' === $term ) {
22308 $context['pluggable_data'] = [
22309 'space' => $space_result,
22310 'user' => WordPress::get_sample_user_context(),
22311 'userId' => 1,
22312 ];
22313 $context['response_type'] = 'live';
22314 }
22315 } else {
22316 $sample_space_data = [
22317 'id' => 101,
22318 'slug' => 'sample-space',
22319 'title' => 'Sample Space',
22320 'description' => 'This is a sample space description.',
22321 'type' => 'community',
22322 'privacy' => 'public',
22323 'status' => 'active',
22324 'serial' => 'SP101',
22325 'created_at' => '2025-01-01 10:00:00',
22326 'created_by' => 1,
22327 'updated_at' => '2025-01-05 12:00:00',
22328 'settings' => [
22329 'coursetype' => 'self_paced',
22330 'emoji' => '😍',
22331 'shapesvg' => null,
22332 'disablecomments' => 'no',
22333 'hidememberscount' => 'no',
22334 ],
22335 ];
22336 $sample_profile_data = [
22337 'id' => 1,
22338 'user_id' => 1,
22339 'total_points' => 100,
22340 'username' => 'sampleuser',
22341 'status' => 'active',
22342 'is_verified' => true,
22343 'display_name' => 'Sample User',
22344 'avatar' => 'https://example.com/avatar.jpg',
22345 'short_description' => 'This is a sample user.',
22346 'last_activity' => '2025-01-08 15:00:00',
22347 'meta' => [
22348 'website' => 'https://example.com',
22349 'coverphoto' => 'https://example.com/coverphoto.jpg',
22350 ],
22351 'created_at' => '2025-01-01 10:00:00',
22352 'updated_at' => '2025-01-05 12:00:00',
22353 ];
22354
22355 if ( 'feed_created' === $term || 'feed_updated' === $term || 'space_feed_created' === $term ) {
22356 $context['pluggable_data'] = [
22357 'feed' => [
22358 'id' => 1,
22359 'userid' => 1,
22360 'parentid' => null,
22361 'title' => 'Sample Feed',
22362 'slug' => 'sample-feed',
22363 'message' => 'This is a sample message for the feed.',
22364 'messagerendered' => '<p>This is a sample message for the feed.</p>',
22365 'type' => 'post',
22366 'contenttype' => 'text',
22367 'space_id' => 101,
22368 'privacy' => 'public',
22369 'status' => 'published',
22370 'featuredimage' => 'https://example.com/image.jpg',
22371 'meta' => [
22372 'previewdata' => 0,
22373 ],
22374 'issticky' => false,
22375 'commentscount' => 10,
22376 'reactionscount' => 25,
22377 'priority' => 'normal',
22378 'expiredat' => null,
22379 'scheduledat' => '2025-01-10 08:00:00',
22380 'createdat' => '2025-01-09 09:00:00',
22381 'updatedat' => '2025-01-09 10:00:00',
22382 'space' => $sample_space_data,
22383 'xprofile' => $sample_profile_data,
22384 ],
22385 ];
22386 $context['response_type'] = 'sample';
22387 }
22388 if ( 'space_feed_created' === $term ) {
22389 $conext['pluggable_data']['feed']['user'] = [
22390 'user_url' => 'https://example.com/user/profile',
22391 'user_email' => 'user@example.com',
22392 'user_login' => 'sampleuser',
22393 'user_status' => 'active',
22394 'display_name' => 'Sample User',
22395 'user_nicename' => 'sample-user',
22396 'user_registered' => '2024-01-01 00:00:00',
22397 ];
22398 $context['pluggable_data']['feed']['space'] = $sample_space_data;
22399 $context['pluggable_data']['feed']['xprofile'] = $sample_profile_data;
22400 $context['response_type'] = 'sample';
22401 }
22402 if ( 'user_joins_space' === $term ) {
22403 $context['pluggable_data'] = [
22404 'space' => [
22405 $sample_space_data,
22406 'membership' => [
22407 'ID' => 25,
22408 'photo' => 'https://example.com/user-photo.jpg',
22409 'pivot_role' => 'admin',
22410 'pivot_status' => 'active',
22411 'pivot_created_at' => '2025-01-24 09:00:00',
22412 'pivot_user_id' => 25,
22413 'pivot_space_id' => 100,
22414 'user_url' => 'https://example.com/user-profile',
22415 'user_email' => 'user@example.com',
22416 'user_login' => 'sampleuser',
22417 'user_status' => 'active',
22418 'display_name' => 'Sample User',
22419 'user_nicename' => 'sample-user',
22420 'user_registered' => '2024-01-01 00:00:00',
22421 ],
22422 ],
22423 'userId' => 1,
22424 'by' => 'self',
22425 ];
22426 $context['response_type'] = 'sample';
22427 }
22428
22429 if ( 'user_leaves_space' === $term ) {
22430 $context['pluggable_data'] = [
22431 'space' => $sample_space_data,
22432 'user' => WordPress::get_sample_user_context(),
22433 'userId' => 35,
22434 'by' => 'self',
22435 ];
22436 $context['response_type'] = 'sample';
22437 }
22438
22439 if ( 'new_comment_added' === $term || 'comment_updated' === $term ) {
22440 $context['pluggable_data'] = [
22441 'comment' => [
22442 'id' => 301,
22443 'user_id' => 45,
22444 'post_id' => 101,
22445 'parent_id' => 0,
22446 'reactions_count' => 5,
22447 'message' => 'This is a sample comment message.',
22448 'message_rendered' => '<p>This is a sample comment message.</p>',
22449 'meta' => [
22450 'likes' => 3,
22451 'shares' => 2,
22452 ],
22453 'type' => 'text',
22454 'content_type' => 'post',
22455 'status' => 'approved',
22456 'is_sticky' => false,
22457 'created_at' => '2025-01-23 10:30:00',
22458 'updated_at' => '2025-01-23 12:00:00',
22459 'xprofile' => $sample_profile_data,
22460 ],
22461 ];
22462 $context['response_type'] = 'sample';
22463 }
22464
22465 if ( 'course_deleted' === $term ) {
22466 $context['pluggable_data'] = [ 'course' => 2 ];
22467 $context['response_type'] = 'sample';
22468 }
22469 if ( 'comment_deleted' === $term ) {
22470 $context['pluggable_data'] = [ 'comment_id' => 1 ];
22471 $context['response_type'] = 'sample';
22472 }
22473 if ( 'user_enrolls_course' === $term || 'user_unenrolls_course' === $term || 'user_completes_course' === $term ) {
22474 $context['pluggable_data'] = [
22475 'course' => $sample_space_data,
22476 'user' => WordPress::get_sample_user_context(),
22477 'userId' => 1,
22478 ];
22479 $context['response_type'] = 'sample';
22480 }
22481 if ( 'course_created' === $term || 'course_updated' === $term || 'course_published' === $term ) {
22482 $context['pluggable_data'] = [
22483 'course' => $sample_space_data,
22484 ];
22485 $context['response_type'] = 'sample';
22486 }
22487 if ( 'user_requests_join_space' === $term ) {
22488 $context['pluggable_data'] = [
22489 'space' => $sample_space_data,
22490 'user' => WordPress::get_sample_user_context(),
22491 'userId' => 1,
22492 ];
22493 $context['response_type'] = 'sample';
22494 }
22495 }
22496 return (array) $context;
22497 }
22498
22499 /**
22500 * Prepare FluentCommunity Courses List.
22501 *
22502 * @param array $data Search Params.
22503 * @return array
22504 */
22505 public function search_fc_courses_list( $data ) {
22506 global $wpdb;
22507
22508 $courses = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fcom_spaces WHERE type = 'course' ", ARRAY_A );
22509
22510 $options = [];
22511
22512 if ( ! empty( $courses ) ) {
22513 foreach ( $courses as $course ) {
22514 $options[] = [
22515 'label' => $course['title'],
22516 'value' => $course['id'],
22517 ];
22518 }
22519 }
22520
22521 return [
22522 'options' => $options,
22523 'hasMore' => false,
22524 ];
22525 }
22526
22527 /**
22528 * Search Thrive Apprentice Courses List.
22529 *
22530 * @param array $data Search Params.
22531 * @return array
22532 */
22533 public function search_thrive_apprentice_courses_list( $data ) {
22534 $page = isset( $data['page'] ) ? $data['page'] : 1;
22535 $limit = Utilities::get_search_page_limit();
22536 $offset = $limit * ( $page - 1 );
22537
22538 if ( ! defined( 'TVA_IS_APPRENTICE' ) && ! class_exists( 'TVA_Const' ) ) {
22539 return [
22540 'options' => [],
22541 'hasMore' => false,
22542 ];
22543 }
22544
22545 $courses = get_terms(
22546 [
22547 'taxonomy' => 'tva_courses',
22548 'hide_empty' => false,
22549 'number' => $limit,
22550 'offset' => $offset,
22551 ]
22552 );
22553
22554 $options = [];
22555
22556 if ( ! empty( $courses ) && ! is_wp_error( $courses ) ) {
22557 foreach ( $courses as $course ) {
22558 $options[] = [
22559 'label' => $course->name,
22560 'value' => $course->term_id,
22561 ];
22562 }
22563 }
22564
22565 $all_courses_count = get_terms(
22566 [
22567 'taxonomy' => 'tva_courses',
22568 'hide_empty' => false,
22569 'fields' => 'count',
22570 ]
22571 );
22572
22573 $has_more = ( $offset + $limit ) < $all_courses_count;
22574
22575 return [
22576 'options' => $options,
22577 'hasMore' => $has_more,
22578 ];
22579 }
22580
22581 /**
22582 * Search Thrive Apprentice Assessments List.
22583 *
22584 * @param array $data Search Params.
22585 * @return array
22586 */
22587 public function search_thrive_apprentice_assessments_list( $data ) {
22588 $page = isset( $data['page'] ) ? $data['page'] : 1;
22589 $limit = Utilities::get_search_page_limit();
22590 $offset = $limit * ( $page - 1 );
22591 $course_id = isset( $data['dynamic']['course_id'] ) ? $data['dynamic']['course_id'] : '';
22592
22593 if ( ! defined( 'TVA_IS_APPRENTICE' ) && ! class_exists( 'TVA_Const' ) ) {
22594 return [
22595 'options' => [],
22596 'hasMore' => false,
22597 ];
22598 }
22599
22600 if ( ! empty( $course_id ) ) {
22601 $course_post_ids = get_objects_in_term( intval( $course_id ), 'tva_courses' );
22602 if ( empty( $course_post_ids ) || is_wp_error( $course_post_ids ) ) {
22603 return [
22604 'options' => [],
22605 'hasMore' => false,
22606 ];
22607 }
22608 $args = [
22609 'post_type' => 'tva_assessment',
22610 'post_status' => 'publish',
22611 'posts_per_page' => $limit,
22612 'offset' => $offset,
22613 'fields' => 'ids',
22614 'post__in' => array_map( 'intval', $course_post_ids ),
22615 ];
22616 } else {
22617 $args = [
22618 'post_type' => 'tva_assessment',
22619 'post_status' => 'publish',
22620 'posts_per_page' => $limit,
22621 'offset' => $offset,
22622 'fields' => 'ids',
22623 ];
22624 }
22625
22626 $assessments = get_posts( $args );
22627 $options = [];
22628
22629 if ( ! empty( $assessments ) ) {
22630 foreach ( $assessments as $assessment_id ) {
22631 $assessment = get_post( $assessment_id );
22632 if ( $assessment ) {
22633 $options[] = [
22634 'label' => $assessment->post_title,
22635 'value' => $assessment->ID,
22636 ];
22637 }
22638 }
22639 }
22640
22641 $count_args = $args;
22642 $count_args['posts_per_page'] = -1;
22643 $count_args['offset'] = 0;
22644 $all_assessments = get_posts( $count_args );
22645 $has_more = ( $offset + $limit ) < count( $all_assessments );
22646
22647 return [
22648 'options' => $options,
22649 'hasMore' => $has_more,
22650 ];
22651 }
22652
22653 /**
22654 * Search Thrive Apprentice Lessons List.
22655 *
22656 * @param array $data Search Params.
22657 * @return array
22658 */
22659 public function search_thrive_apprentice_lessons_list( $data ) {
22660 $page = isset( $data['page'] ) ? $data['page'] : 1;
22661 $limit = Utilities::get_search_page_limit();
22662 $offset = $limit * ( $page - 1 );
22663 $course_id = isset( $data['dynamic']['course_id'] ) ? $data['dynamic']['course_id'] : '';
22664
22665 if ( ! defined( 'TVA_IS_APPRENTICE' ) && ! class_exists( 'TVA_Const' ) ) {
22666 return [
22667 'options' => [],
22668 'hasMore' => false,
22669 ];
22670 }
22671
22672 if ( ! empty( $course_id ) ) {
22673 $course_post_ids = get_objects_in_term( intval( $course_id ), 'tva_courses' );
22674 if ( empty( $course_post_ids ) || is_wp_error( $course_post_ids ) ) {
22675 return [
22676 'options' => [],
22677 'hasMore' => false,
22678 ];
22679 }
22680 $args = [
22681 'post_type' => 'tva_lesson',
22682 'post_status' => 'publish',
22683 'posts_per_page' => $limit,
22684 'offset' => $offset,
22685 'fields' => 'ids',
22686 'post__in' => array_map( 'intval', $course_post_ids ),
22687 ];
22688 } else {
22689 $args = [
22690 'post_type' => 'tva_lesson',
22691 'post_status' => 'publish',
22692 'posts_per_page' => $limit,
22693 'offset' => $offset,
22694 'fields' => 'ids',
22695 ];
22696 }
22697
22698 $lessons = get_posts( $args );
22699 $options = [];
22700
22701 if ( ! empty( $lessons ) ) {
22702 foreach ( $lessons as $lesson_id ) {
22703 $lesson = get_post( $lesson_id );
22704 if ( $lesson ) {
22705 $options[] = [
22706 'label' => $lesson->post_title,
22707 'value' => $lesson->ID,
22708 ];
22709 }
22710 }
22711 }
22712
22713 $count_args = $args;
22714 $count_args['posts_per_page'] = -1;
22715 $count_args['offset'] = 0;
22716 $all_lessons = get_posts( $count_args );
22717 $has_more = ( $offset + $limit ) < count( $all_lessons );
22718
22719 return [
22720 'options' => $options,
22721 'hasMore' => $has_more,
22722 ];
22723 }
22724
22725 /**
22726 * Search Thrive Apprentice Premium Courses List.
22727 *
22728 * @param array $data Search Params.
22729 * @return array
22730 */
22731 public function search_thrive_apprentice_premium_courses_list( $data ) {
22732 $page = isset( $data['page'] ) ? $data['page'] : 1;
22733 $limit = Utilities::get_search_page_limit();
22734 $offset = $limit * ( $page - 1 );
22735
22736 if ( ! defined( 'TVA_IS_APPRENTICE' ) && ! class_exists( 'TVA_Const' ) ) {
22737 return [
22738 'options' => [],
22739 'hasMore' => false,
22740 ];
22741 }
22742
22743 $courses = get_terms(
22744 [
22745 'taxonomy' => 'tva_courses',
22746 'hide_empty' => false,
22747 'number' => $limit,
22748 'offset' => $offset,
22749 ]
22750 );
22751
22752 $options = [];
22753
22754 if ( ! empty( $courses ) && ! is_wp_error( $courses ) ) {
22755 foreach ( $courses as $course ) {
22756 if ( class_exists( 'TVA_Course_V2' ) ) {
22757 $course_obj = new \TVA_Course_V2( $course->term_id );
22758 $product = $course_obj->get_product();
22759 if ( ! empty( $product ) ) {
22760 $options[] = [
22761 'label' => $course->name,
22762 'value' => $course->term_id,
22763 ];
22764 }
22765 }
22766 }
22767 }
22768 $all_courses = get_terms(
22769 [
22770 'taxonomy' => 'tva_courses',
22771 'hide_empty' => false,
22772 'fields' => 'all',
22773 ]
22774 );
22775
22776 $premium_count = 0;
22777 if ( ! empty( $all_courses ) && ! is_wp_error( $all_courses ) ) {
22778 foreach ( $all_courses as $course ) {
22779 if ( class_exists( 'TVA_Course_V2' ) ) {
22780 $course_obj = new \TVA_Course_V2( $course->term_id );
22781 $product = $course_obj->get_product();
22782 if ( ! empty( $product ) ) {
22783 $premium_count++;
22784 }
22785 }
22786 }
22787 }
22788
22789 $has_more = ( $offset + $limit ) < $premium_count;
22790
22791 return [
22792 'options' => $options,
22793 'hasMore' => $has_more,
22794 ];
22795 }
22796
22797 /**
22798 * Search Thrive Apprentice Modules List.
22799 *
22800 * @param array $data Search Params.
22801 * @return array
22802 */
22803 public function search_thrive_apprentice_modules_list( $data ) {
22804 $page = isset( $data['page'] ) ? $data['page'] : 1;
22805 $limit = Utilities::get_search_page_limit();
22806 $offset = $limit * ( $page - 1 );
22807 $course_id = isset( $data['dynamic']['course_id'] ) ? $data['dynamic']['course_id'] : '';
22808
22809 if ( ! defined( 'TVA_IS_APPRENTICE' ) && ! class_exists( 'TVA_Const' ) ) {
22810 return [
22811 'options' => [],
22812 'hasMore' => false,
22813 ];
22814 }
22815
22816 if ( ! empty( $course_id ) ) {
22817 $course_post_ids = get_objects_in_term( intval( $course_id ), 'tva_courses' );
22818 if ( empty( $course_post_ids ) || is_wp_error( $course_post_ids ) ) {
22819 return [
22820 'options' => [],
22821 'hasMore' => false,
22822 ];
22823 }
22824 $args = [
22825 'post_type' => 'tva_module',
22826 'post_status' => 'publish',
22827 'posts_per_page' => $limit,
22828 'offset' => $offset,
22829 'fields' => 'ids',
22830 'post__in' => array_map( 'intval', $course_post_ids ),
22831 ];
22832 } else {
22833 $args = [
22834 'post_type' => 'tva_module',
22835 'post_status' => 'publish',
22836 'posts_per_page' => $limit,
22837 'offset' => $offset,
22838 'fields' => 'ids',
22839 ];
22840 }
22841
22842 $modules = get_posts( $args );
22843 $options = [];
22844
22845 if ( ! empty( $modules ) ) {
22846 foreach ( $modules as $module_id ) {
22847 $module = get_post( $module_id );
22848 if ( $module ) {
22849 $options[] = [
22850 'label' => $module->post_title,
22851 'value' => $module->ID,
22852 ];
22853 }
22854 }
22855 }
22856
22857 if ( ! empty( $course_id ) ) {
22858 $total_count = count( $course_post_ids );
22859 $has_more = ( $offset + $limit ) < $total_count;
22860 } else {
22861 $count_args = $args;
22862 $count_args['posts_per_page'] = -1;
22863 $count_args['offset'] = 0;
22864 $all_modules = get_posts( $count_args );
22865 $has_more = ( $offset + $limit ) < count( $all_modules );
22866 }
22867
22868 return [
22869 'options' => $options,
22870 'hasMore' => $has_more,
22871 ];
22872 }
22873
22874 /**
22875 * Search Thrive Apprentice Products List.
22876 *
22877 * @param array $data Search Params.
22878 * @return array
22879 */
22880 public function search_thrive_apprentice_products_list( $data ) {
22881 $page = isset( $data['page'] ) ? $data['page'] : 1;
22882 $limit = Utilities::get_search_page_limit();
22883 $offset = $limit * ( $page - 1 );
22884
22885 if ( ! defined( 'TVA_IS_APPRENTICE' ) && ! class_exists( 'TVA_Const' ) ) {
22886 return [
22887 'options' => [],
22888 'hasMore' => false,
22889 ];
22890 }
22891
22892 $products = get_terms(
22893 [
22894 'taxonomy' => 'tva_product',
22895 'hide_empty' => false,
22896 'number' => $limit,
22897 'offset' => $offset,
22898 ]
22899 );
22900
22901 $options = [];
22902
22903 if ( ! empty( $products ) && ! is_wp_error( $products ) ) {
22904 foreach ( $products as $product ) {
22905 $options[] = [
22906 'label' => $product->name,
22907 'value' => $product->term_id,
22908 ];
22909 }
22910 }
22911
22912 $all_products_count = get_terms(
22913 [
22914 'taxonomy' => 'tva_product',
22915 'hide_empty' => false,
22916 'fields' => 'count',
22917 ]
22918 );
22919
22920 $has_more = ( $offset + $limit ) < $all_products_count;
22921
22922 return [
22923 'options' => $options,
22924 'hasMore' => $has_more,
22925 ];
22926 }
22927
22928 /**
22929 * Prepare FluentCommunity Spaces List.
22930 *
22931 * @param array $data Search Params.
22932 * @return array
22933 */
22934 public function search_fc_spaces_list( $data ) {
22935 global $wpdb;
22936
22937 $spaces = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fcom_spaces WHERE type NOT IN ('course', 'space_group')", ARRAY_A );
22938
22939 $options = [];
22940
22941 if ( ! empty( $spaces ) ) {
22942 foreach ( $spaces as $space ) {
22943 $options[] = [
22944 'label' => $space['title'],
22945 'value' => $space['id'],
22946 ];
22947 }
22948 }
22949
22950 return [
22951 'options' => $options,
22952 'hasMore' => false,
22953 ];
22954 }
22955
22956 /**
22957 * Prepare FluentCommunity Topics List.
22958 *
22959 * @param array $data Search Params.
22960 * @return array
22961 */
22962 public function search_fc_topics_list( $data ) {
22963 if ( ! class_exists( '\FluentCommunity\App\Functions\Utility' ) ) {
22964 return [];
22965 }
22966
22967 $space_topics = Utility::getTopicsBySpaceId( $data['dynamic'] );
22968
22969 $options = [];
22970
22971 if ( ! empty( $space_topics ) ) {
22972 foreach ( $space_topics as $topic ) {
22973 $options[] = [
22974 'label' => $topic['title'],
22975 'value' => $topic['id'],
22976 ];
22977 }
22978 }
22979
22980 return [
22981 'options' => $options,
22982 'hasMore' => false,
22983 ];
22984 }
22985
22986 /**
22987 * Get WP Travel Engine Last Data
22988 *
22989 * @param array $data data.
22990 *
22991 * @return array
22992 */
22993 public function search_wpte_triggers_last_data( $data ) {
22994 global $wpdb;
22995
22996 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
22997
22998 $context = [];
22999 $result = null;
23000
23001 switch ( $term ) {
23002 case 'enquiry_submitted':
23003 $result = $wpdb->get_row(
23004 "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'enquiry' ORDER BY ID DESC LIMIT 1",
23005 ARRAY_A
23006 );
23007
23008 if ( $result && isset( $result['ID'] ) ) {
23009 $data = get_post( $result['ID'] );
23010 }
23011 break;
23012
23013 case 'booking_created':
23014 case 'booking_confirmed':
23015 case 'booking_cancelled':
23016 $result = $wpdb->get_row(
23017 "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'booking' ORDER BY ID DESC LIMIT 1",
23018 ARRAY_A
23019 );
23020
23021 if ( $result && isset( $result['ID'] ) ) {
23022 $booking_id = $result['ID'];
23023 $booking_data = get_post( $booking_id );
23024 $booking_meta = get_post_meta( $booking_id, 'wp_travel_engine_booking_setting', true );
23025 $booking_status = get_post_meta( $booking_id, 'wp_travel_engine_booking_status', true );
23026
23027 // Set correct booking status based on trigger type.
23028 switch ( $term ) {
23029 case 'booking_confirmed':
23030 $booking_status = 'booked';
23031 break;
23032 case 'booking_cancelled':
23033 $booking_status = 'cancelled';
23034 break;
23035 }
23036
23037 // For booking_created, include payment data as well.
23038 if ( 'booking_created' === $term ) {
23039 $payment_status = get_post_meta( $booking_id, 'wp_travel_engine_booking_payment_status', true );
23040 $payment_gateway = get_post_meta( $booking_id, 'wp_travel_engine_booking_payment_gateway', true );
23041 $payment_details = get_post_meta( $booking_id, 'wp_travel_engine_booking_payment_details', true );
23042
23043 $coupon_details = [];
23044 $cart_info = get_post_meta( $booking_id, 'cart_info', true );
23045
23046 if ( ! empty( $cart_info ) && is_array( $cart_info ) && isset( $cart_info['discounts'] ) ) {
23047 foreach ( $cart_info['discounts'] as $discount ) {
23048 if ( isset( $discount['name'] ) ) {
23049 $coupon_details[] = [
23050 'coupon_code' => $discount['name'],
23051 'discount_type' => isset( $discount['type'] ) ? $discount['type'] : '',
23052 'discount_value' => isset( $discount['value'] ) ? $discount['value'] : 0,
23053 ];
23054 }
23055 }
23056 }
23057
23058 if ( empty( $coupon_details ) ) {
23059 $coupon_details = [
23060 [
23061 'coupon_code' => 'SAVE20',
23062 'discount_type' => 'percentage',
23063 'discount_value' => 20,
23064 ],
23065 ];
23066 }
23067
23068 $data = [
23069 'booking_data' => $booking_data,
23070 'booking_meta' => $booking_meta,
23071 'payment_status' => $payment_status,
23072 'payment_gateway' => $payment_gateway,
23073 'payment_details' => $payment_details,
23074 'coupon_details' => $coupon_details,
23075 ];
23076 } else {
23077 $data = [
23078 'booking_data' => $booking_data,
23079 'booking_meta' => $booking_meta,
23080 'booking_status' => $booking_status,
23081 ];
23082 }
23083 }
23084 break;
23085 }
23086
23087 if ( ! empty( $result ) && isset( $result['ID'] ) ) {
23088 if ( in_array( $term, [ 'booking_created', 'booking_confirmed', 'booking_cancelled' ] ) ) {
23089 $context['pluggable_data'] = $data;
23090 } else {
23091 $context['pluggable_data'] = [
23092 'enquiry_post_id' => $result['ID'],
23093 'post_data' => $data,
23094 ];
23095 }
23096 $context['response_type'] = 'live';
23097 } else {
23098 // Helper function to generate booking sample data.
23099 $get_booking_sample_data = function( $customer, $trip, $booking_status = '', $payment_data = [] ) {
23100 $booking_data = [
23101 'ID' => 123,
23102 'post_title' => $customer['fname'] . ' ' . $customer['lname'] . ' #123',
23103 'post_name' => strtolower( $customer['fname'] . '-' . $customer['lname'] ) . '-123',
23104 'post_date' => '2025-01-15 10:00:00',
23105 'post_status' => 'publish',
23106 'post_type' => 'booking',
23107 'post_author' => 1,
23108 'post_content' => '',
23109 'post_excerpt' => '',
23110 'comment_status' => 'closed',
23111 'ping_status' => 'closed',
23112 'post_modified' => '2025-01-15 10:00:00',
23113 'guid' => 'http://example.com/booking/' . strtolower( $customer['fname'] . '-' . $customer['lname'] ) . '-123',
23114 ];
23115
23116 $booking_meta = [
23117 'place_order' => [
23118 'traveler' => 1,
23119 'cost' => 10,
23120 'due' => 10,
23121 'tid' => 1308,
23122 'tname' => 'New York',
23123 'datetime' => '2025-08-14',
23124 'datewithtime' => '',
23125 'booking' => [
23126 'fname' => 'John',
23127 'lname' => 'Doe',
23128 'email' => 'john.doe@example.com',
23129 'address' => '123 Main St',
23130 'city' => 'New York',
23131 'country' => 'US',
23132 ],
23133 'tax' => '',
23134 'tduration' => '1 days',
23135 'tenddate' => '2025-08-14T00:00',
23136 'trip_package' => 'Camp Trek',
23137 ],
23138 ];
23139
23140 $sample_data = [
23141 'booking_data' => $booking_data,
23142 'booking_meta' => $booking_meta,
23143 ];
23144
23145 if ( $booking_status ) {
23146 $sample_data['booking_status'] = $booking_status;
23147 }
23148
23149 if ( ! empty( $payment_data ) ) {
23150 $sample_data = array_merge( $sample_data, $payment_data );
23151 }
23152
23153 return $sample_data;
23154 };
23155
23156 // Sample data based on term.
23157 if ( in_array( $term, [ 'booking_created', 'booking_confirmed', 'booking_cancelled' ] ) ) {
23158 $sample_customer = [
23159 'fname' => 'John',
23160 'lname' => 'Doe',
23161 'email' => 'john.doe@example.com',
23162 'address' => '123 Main St',
23163 'city' => 'New York',
23164 'country' => 'US',
23165 ];
23166 $sample_trip = [
23167 'travelers' => '2',
23168 'cost' => '800',
23169 'id' => '101',
23170 'name' => 'Sample Trip to Nepal',
23171 'date' => '2025-03-15',
23172 ];
23173
23174 $booking_status = '';
23175
23176 // Set correct booking status based on trigger type.
23177 switch ( $term ) {
23178 case 'booking_confirmed':
23179 $booking_status = 'booked';
23180 break;
23181 case 'booking_cancelled':
23182 $booking_status = 'cancelled';
23183 break;
23184 }
23185
23186 // For booking_created, include payment data as well.
23187 if ( 'booking_created' === $term ) {
23188 $payment_data = [
23189 'payment_status' => 'check-waiting',
23190 'payment_gateway' => 'Check Payment',
23191 'payment_details' => '',
23192 'coupon_details' => [
23193 [
23194 'coupon_code' => 'SAVE20',
23195 'discount_type' => 'percentage',
23196 'discount_value' => 20,
23197 ],
23198 ],
23199 ];
23200 $context['pluggable_data'] = $get_booking_sample_data( $sample_customer, $sample_trip, '', $payment_data );
23201 } else {
23202 $context['pluggable_data'] = $get_booking_sample_data( $sample_customer, $sample_trip, $booking_status );
23203 }
23204 } else {
23205 switch ( $term ) {
23206
23207 default:
23208 $context['pluggable_data'] = [
23209 'enquiry_post_id' => 123,
23210 'post_data' => [
23211 'ID' => 123,
23212 'post_title' => 'Sample Enquiry',
23213 'post_name' => 'sample-enquiry',
23214 'post_date' => '2025-01-01 10:00:00',
23215 'post_status' => 'publish',
23216 'post_type' => 'enquiry',
23217 'post_author' => 1,
23218 'post_content' => '',
23219 'post_excerpt' => '',
23220 'comment_status' => 'closed',
23221 'ping_status' => 'closed',
23222 'post_modified' => '2025-01-01 10:00:00',
23223 'guid' => 'http://example.com/enquiry/sample-enquiry',
23224 ],
23225 ];
23226 break;
23227 }
23228 $context['response_type'] = 'sample';
23229 }
23230 }
23231 return (array) $context;
23232 }
23233
23234 /**
23235 * Prepare WP Travel Engine Trips List.
23236 *
23237 * @param array $data Search Params.
23238 * @return array
23239 */
23240 public function search_wte_trips_list( $data ) {
23241 $page = isset( $data['page'] ) ? max( 1, intval( $data['page'] ) ) : 1;
23242 $limit = Utilities::get_search_page_limit();
23243 $offset = $limit * ( $page - 1 );
23244
23245 $query_args = [
23246 'post_type' => 'trip',
23247 'post_status' => 'publish',
23248 'posts_per_page' => $limit,
23249 'offset' => $offset,
23250 ];
23251
23252 $query = new \WP_Query( $query_args );
23253 $result = [];
23254
23255 foreach ( $query->posts as $post ) {
23256 if ( $post instanceof \WP_Post ) {
23257 $result[] = [
23258 'label' => $post->post_title,
23259 'value' => $post->ID,
23260 ];
23261 }
23262 }
23263
23264 $total_query = new \WP_Query(
23265 [
23266 'post_type' => 'trip',
23267 'post_status' => 'publish',
23268 'posts_per_page' => -1,
23269 'fields' => 'ids',
23270 ]
23271 );
23272 $posts_count = count( $total_query->posts );
23273
23274 return [
23275 'options' => $result,
23276 'hasMore' => $posts_count > ( $offset + $limit ),
23277 ];
23278 }
23279 /**
23280 * Get GeoDirectory trigger last data.
23281 *
23282 * @param array $data data.
23283 * @return array|mixed
23284 */
23285 public function search_geodir_triggers_last_data( $data ) {
23286 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
23287 $context = [];
23288
23289 if ( function_exists( 'geodir_get_post_info' ) ) {
23290 $args = [
23291 'post_type' => 'gd_place',
23292 'posts_per_page' => 1,
23293 'orderby' => 'date',
23294 'order' => 'DESC',
23295 ];
23296
23297 $posts = get_posts( $args );
23298
23299 if ( ! empty( $posts ) ) {
23300 $post = $posts[0];
23301 $post_id = $post->ID;
23302 $user = WordPress::get_user_context( absint( $post->post_author ) );
23303 $gd_post = function_exists( 'geodir_get_post_info' ) ? geodir_get_post_info( $post_id ) : null;
23304
23305 $categories = [];
23306 $cat_taxonomy = $post->post_type . 'category';
23307 if ( taxonomy_exists( $cat_taxonomy ) ) {
23308 $terms = wp_get_post_terms( $post_id, $cat_taxonomy );
23309 if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
23310 foreach ( $terms as $term_obj ) {
23311 $categories[] = $term_obj->name;
23312 }
23313 }
23314 }
23315
23316 $tags = [];
23317 $tag_taxonomy = $post->post_type . '_tags';
23318 if ( taxonomy_exists( $tag_taxonomy ) ) {
23319 $terms = wp_get_post_terms( $post_id, $tag_taxonomy );
23320 if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
23321 foreach ( $terms as $term_obj ) {
23322 $tags[] = $term_obj->name;
23323 }
23324 }
23325 }
23326
23327 $attachments = [];
23328 if ( function_exists( 'geodir_get_images' ) ) {
23329 $images = geodir_get_images( $post_id );
23330 if ( ! empty( $images ) ) {
23331 foreach ( $images as $image ) {
23332 if ( ! empty( $image->file ) ) {
23333 $attachments[] = $image->file;
23334 }
23335 }
23336 }
23337 }
23338
23339 $address = '';
23340 $country = '';
23341 $region = '';
23342 $city = '';
23343 $postal_code = '';
23344 $latitude = '';
23345 $longitude = '';
23346
23347 if ( ! empty( $gd_post ) ) {
23348 if ( isset( $gd_post->street ) ) {
23349 $address = $gd_post->street;
23350 }
23351 if ( isset( $gd_post->country ) ) {
23352 $country = $gd_post->country;
23353 }
23354 if ( isset( $gd_post->region ) ) {
23355 $region = $gd_post->region;
23356 }
23357 if ( isset( $gd_post->city ) ) {
23358 $city = $gd_post->city;
23359 }
23360 if ( isset( $gd_post->zip ) ) {
23361 $postal_code = $gd_post->zip;
23362 }
23363 if ( isset( $gd_post->latitude ) ) {
23364 $latitude = $gd_post->latitude;
23365 }
23366 if ( isset( $gd_post->longitude ) ) {
23367 $longitude = $gd_post->longitude;
23368 }
23369 }
23370
23371 $base_data = [
23372 'listing_id' => $post->ID,
23373 'listing_title' => $post->post_title,
23374 'listing_type' => $post->post_type,
23375 'listing_url' => get_permalink( $post->ID ),
23376 'description' => $post->post_content,
23377 'categories' => $categories,
23378 'tags' => $tags,
23379 'attachments' => $attachments,
23380 'address' => $address,
23381 'country' => $country,
23382 'region' => $region,
23383 'city' => $city,
23384 'postal_code' => $postal_code,
23385 'latitude' => $latitude,
23386 'longitude' => $longitude,
23387 ];
23388
23389 switch ( $term ) {
23390 case 'geodir_claimed_listing_added':
23391 case 'geodir_claim_approved':
23392 case 'geodir_claim_rejected':
23393 $claim = [];
23394 $claim_status_text = 'unknown';
23395
23396 if ( 'geodir_claimed_listing_added' === $term ) {
23397 $claim_status_text = 'pending';
23398 } elseif ( 'geodir_claim_approved' === $term ) {
23399 $claim_status_text = 'approved';
23400 } elseif ( 'geodir_claim_rejected' === $term ) {
23401 $claim_status_text = 'rejected';
23402 }
23403
23404 global $wpdb;
23405 $claim_table = $wpdb->prefix . 'geodir_claim';
23406
23407 $claim_id = $wpdb->get_var(
23408 $wpdb->prepare(
23409 "SELECT id FROM {$wpdb->prefix}geodir_claim WHERE post_id = %d ORDER BY id DESC LIMIT 1",
23410 $post_id
23411 )
23412 );
23413
23414 if ( $claim_id && class_exists( '\GeoDir_Claim_Post' ) && is_callable( [ '\GeoDir_Claim_Post', 'get_item' ] ) ) {
23415 $claim = \GeoDir_Claim_Post::get_item( $claim_id );
23416 }
23417
23418 $context['pluggable_data'] = array_merge(
23419 $base_data,
23420 [
23421 'claim' => array_merge(
23422 (array) $claim,
23423 [
23424 'status' => $claim_status_text,
23425 ]
23426 ),
23427 ],
23428 $user
23429 );
23430 break;
23431 case 'geodir_listing_upgraded':
23432 $context['pluggable_data'] = array_merge(
23433 $base_data,
23434 [
23435 'package_id' => 789,
23436 'package_name' => 'Premium Package',
23437 'package_amount' => '99.99',
23438 'package_days' => '365',
23439 ],
23440 $user
23441 );
23442 break;
23443 default:
23444 $context['pluggable_data'] = array_merge( $base_data, $user );
23445 break;
23446 }
23447
23448 $context['response_type'] = 'live';
23449 return $context;
23450 }
23451 }
23452
23453 $sample_data = [
23454 'listing_id' => 123,
23455 'listing_title' => 'Sample Business Listing',
23456 'listing_type' => 'gd_place',
23457 'listing_url' => site_url( '/places/sample-business-listing/' ),
23458 'description' => 'This is a sample business listing description.',
23459 'categories' => [ 'Restaurant', 'Cafe' ],
23460 'tags' => [ 'Food', 'Coffee', 'Breakfast' ],
23461 'attachments' => [ site_url( '/wp-content/uploads/sample-image.jpg' ) ],
23462 'address' => '123 Main St',
23463 'country' => 'United States',
23464 'region' => 'California',
23465 'city' => 'San Francisco',
23466 'postal_code' => '94105',
23467 'latitude' => '37.7749',
23468 'longitude' => '-122.4194',
23469 'wp_user_id' => 10,
23470 'user_login' => 'sampleuser',
23471 'display_name' => 'Sample User',
23472 'user_firstname' => 'Sample',
23473 'user_lastname' => 'User',
23474 'user_email' => 'sample@example.com',
23475 'user_role' => [ 'subscriber' ],
23476 ];
23477
23478 switch ( $term ) {
23479 case 'geodir_claim_approved':
23480 case 'geodir_claim_rejected':
23481 case 'geodir_claimed_listing_added':
23482 $sample_data['claim'] = [
23483 'id' => 14,
23484 'post_id' => 123,
23485 'post_type' => 'gd_place',
23486 'author_id' => 10,
23487 'user_id' => 10,
23488 'user_fullname' => 'Sample User',
23489 'user_number' => '+1234567890',
23490 'user_position' => 'Business Owner',
23491 'user_ip' => '127.0.0.1',
23492 'user_comments' => 'Hi, I am the owner of this business and I would like to claim it.',
23493 'admin_comments' => '',
23494 'claim_date' => current_time( 'mysql' ),
23495 'status' => 'pending',
23496 'rand_string' => 'sampleclaimstring123',
23497 'payment_id' => 0,
23498 'old_package_id' => 0,
23499 'package_id' => 0,
23500 'meta' => [
23501 'ninja_form_id' => '2',
23502 'ninja_sub_id' => '',
23503 'ninja_post' => 123,
23504 'extra_fields' => [
23505 'user_id' => [
23506 'label' => 'User ID',
23507 'value' => '10',
23508 ],
23509 ],
23510 ],
23511 ];
23512 break;
23513 case 'geodir_listing_upgraded':
23514 $sample_data['package_id'] = 789;
23515 $sample_data['package_name'] = 'Premium Package';
23516 $sample_data['package_amount'] = '99.99';
23517 $sample_data['package_days'] = '365';
23518 break;
23519 }
23520
23521 $context['pluggable_data'] = $sample_data;
23522 $context['response_type'] = 'sample';
23523 return $context;
23524 }
23525
23526 /**
23527 * Get WP Subscription triggers last data.
23528 *
23529 * @param array $data data.
23530 *
23531 * @return array
23532 */
23533 public function search_wp_subscription_triggers_last_data( $data ) {
23534 global $wpdb;
23535
23536 $context = [];
23537 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
23538
23539 $needs_subscription = in_array(
23540 $term,
23541 [
23542 'subscription_activated',
23543 'subscription_cancelled',
23544 'subscription_expired',
23545 'subscription_status_changed',
23546 'subscription_pending',
23547 'subscription_pending_cancellation',
23548 ],
23549 true
23550 );
23551
23552 if ( ! $needs_subscription ) {
23553 return $context;
23554 }
23555
23556 $subscription = $wpdb->get_row(
23557 "SELECT * FROM {$wpdb->prefix}posts WHERE post_type = 'subscrpt_order' ORDER BY post_modified DESC LIMIT 1"
23558 );
23559
23560 $sample_subscription = new \stdClass();
23561 $sample_subscription->ID = 100;
23562 $sample_subscription->post_author = 2;
23563 $sample_subscription->post_date = '2025-07-07 09:51:52';
23564 $sample_subscription->post_date_gmt = '2025-07-07 09:51:52';
23565 $sample_subscription->post_content = '';
23566 $sample_subscription->post_title = 'Subscription #100';
23567 $sample_subscription->post_excerpt = '';
23568 $sample_subscription->post_status = 'active';
23569 $sample_subscription->comment_status = 'closed';
23570 $sample_subscription->ping_status = 'closed';
23571 $sample_subscription->post_password = '';
23572 $sample_subscription->post_name = 'subscription-100';
23573 $sample_subscription->to_ping = '';
23574 $sample_subscription->pinged = '';
23575 $sample_subscription->post_modified = '2025-07-07 10:15:16';
23576 $sample_subscription->post_modified_gmt = '2025-07-07 10:15:16';
23577 $sample_subscription->post_content_filtered = '';
23578 $sample_subscription->post_parent = 0;
23579 $sample_subscription->guid = 'http://example.com/subscrpt_order/';
23580 $sample_subscription->menu_order = 0;
23581 $sample_subscription->post_type = 'subscrpt_order';
23582 $sample_subscription->post_mime_type = '';
23583 $sample_subscription->comment_count = 4;
23584 $sample_subscription->filter = 'raw';
23585
23586 $sub = $subscription ? $subscription : $sample_subscription;
23587 $type = $subscription ? 'live' : 'sample';
23588
23589 $pluggable_data = [
23590 'subscription_id' => $sub->ID,
23591 'subscription' => $sub,
23592 'user_id' => $sub->post_author,
23593 ];
23594
23595 if ( 'subscription_status_changed' === $term ) {
23596 $pluggable_data['old_status'] = 'pending';
23597 $pluggable_data['new_status'] = $sub->post_status;
23598 }
23599
23600 $context['pluggable_data'] = $pluggable_data;
23601 $context['response_type'] = $type;
23602
23603 return $context;
23604 }
23605 /**
23606 * Prepare clickwhale categories.
23607 *
23608 * @param array $data Search Params.
23609 *
23610 * @return array
23611 */
23612 public function search_clickwhale_categories( $data ) {
23613
23614 $options = [];
23615 global $wpdb;
23616
23617 $categories = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}clickwhale_categories ORDER BY id DESC", ARRAY_A );
23618
23619 if ( ! empty( $categories ) ) {
23620 foreach ( $categories as $category ) {
23621 $options[] = [
23622 'label' => $category['title'],
23623 'value' => $category['id'],
23624 ];
23625 }
23626 }
23627
23628 return [
23629 'options' => $options,
23630 'hasMore' => false,
23631 ];
23632 }
23633
23634 /**
23635 * Get ClickWhale Last Data
23636 *
23637 * @param array $data data.
23638 *
23639 * @return array
23640 */
23641 public function search_clickwhale_triggers_last_data( $data ) {
23642 global $wpdb;
23643
23644 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
23645 $context = [];
23646
23647 $latest_link = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}clickwhale_links ORDER BY id DESC LIMIT 1", ARRAY_A );
23648
23649 $get_author_data = function( $author_id ) {
23650 if ( class_exists( '\SureTriggers\Integrations\WordPress\WordPress' ) ) {
23651 return WordPress::get_user_context( $author_id );
23652 }
23653 return [];
23654 };
23655
23656 $format_link = function( $link_data ) use ( $get_author_data ) {
23657 $author_id = isset( $link_data['author'] ) ? $link_data['author'] : 0;
23658
23659 return [
23660 'id' => isset( $link_data['id'] ) ? $link_data['id'] : 17,
23661 'title' => isset( $link_data['title'] ) ? $link_data['title'] : 'Sample Title',
23662 'slug' => isset( $link_data['slug'] ) ? $link_data['slug'] : 'sample-slug',
23663 'url' => isset( $link_data['url'] ) ? $link_data['url'] : 'https://example.com',
23664 'redirection' => isset( $link_data['redirection'] ) ? $link_data['redirection'] : '301',
23665 'link_target' => isset( $link_data['link_target'] ) ? $link_data['link_target'] : '',
23666 'nofollow' => isset( $link_data['nofollow'] ) ? $link_data['nofollow'] : '1',
23667 'sponsored' => isset( $link_data['sponsored'] ) ? $link_data['sponsored'] : '0',
23668 'description' => isset( $link_data['description'] ) ? $link_data['description'] : '',
23669 'categories' => isset( $link_data['categories'] ) ? $link_data['categories'] : '',
23670 'author_id' => $author_id,
23671 'author' => $get_author_data( $author_id ),
23672 'created_at' => isset( $link_data['created_at'] ) ? $link_data['created_at'] : current_time( 'mysql' ),
23673 'updated_at' => isset( $link_data['updated_at'] ) ? $link_data['updated_at'] : current_time( 'mysql' ),
23674 ];
23675 };
23676
23677 switch ( $term ) {
23678 case 'link_created':
23679 case 'link_updated':
23680 $link_data = ! empty( $latest_link ) ? $format_link( $latest_link ) : $format_link( [] );
23681 $context['pluggable_data'] = [ 'link' => $link_data ];
23682 $context['response_type'] = ! empty( $latest_link ) ? 'live' : 'sample';
23683 break;
23684
23685 case 'link_deleted':
23686 if ( ! empty( $latest_link ) ) {
23687 $context['pluggable_data'] = [
23688 'link_id' => $latest_link['id'],
23689 'deleted_at' => current_time( 'mysql' ),
23690 ];
23691 $context['response_type'] = 'live';
23692 } else {
23693 $context['pluggable_data'] = [
23694 'link_id' => 17,
23695 'deleted_at' => current_time( 'mysql' ),
23696 ];
23697 $context['response_type'] = 'sample';
23698 }
23699 break;
23700
23701 case 'link_clicked':
23702 $link_data = ! empty( $latest_link ) ? $format_link( $latest_link ) : $format_link( [] );
23703
23704 $context['pluggable_data'] = [
23705 'link' => $link_data,
23706 ];
23707 $context['response_type'] = ! empty( $latest_link ) ? 'live' : 'sample';
23708 break;
23709 }
23710
23711 return (array) $context;
23712 }
23713
23714 /**
23715 * Get Pretty Links Last Data
23716 *
23717 * @param array $data data.
23718 *
23719 * @return array
23720 */
23721 public function search_pretty_links_triggers_last_data( $data ) {
23722 global $wpdb;
23723
23724 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
23725 $context = [];
23726
23727 // Check if Pretty Links plugin is active.
23728 if ( ! defined( 'PRLI_VERSION' ) ) {
23729 return $context;
23730 }
23731
23732 // Get latest pretty link.
23733 $latest_link = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}prli_links ORDER BY id DESC LIMIT 1", ARRAY_A );
23734
23735 // Get latest click if available.
23736 $latest_click = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}prli_clicks ORDER BY id DESC LIMIT 1", ARRAY_A );
23737
23738 $get_user_data = function( $user_id ) {
23739 if ( class_exists( '\SureTriggers\Integrations\WordPress\WordPress' ) ) {
23740 return WordPress::get_user_context( $user_id );
23741 }
23742 return [
23743 'user_id' => 0,
23744 'user_login' => 'guest',
23745 'display_name' => 'Guest User',
23746 'user_firstname' => '',
23747 'user_lastname' => '',
23748 'user_email' => 'guest@example.com',
23749 'user_role' => [],
23750 ];
23751 };
23752 $format_link = function( $link_data ) {
23753 return [
23754 'id' => isset( $link_data['id'] ) ? $link_data['id'] : 17,
23755 'name' => isset( $link_data['name'] ) ? $link_data['name'] : 'Sample Pretty Link',
23756 'url' => isset( $link_data['url'] ) ? $link_data['url'] : 'https://example.com',
23757 'slug' => isset( $link_data['slug'] ) ? $link_data['slug'] : 'sample-link',
23758 'pretty_url' => home_url( '/' . ( isset( $link_data['slug'] ) ? $link_data['slug'] : 'sample-link' ) ),
23759 'description' => isset( $link_data['description'] ) ? $link_data['description'] : 'Sample description',
23760 'redirect_type' => isset( $link_data['redirect_type'] ) ? $link_data['redirect_type'] : '307',
23761 'clicks' => isset( $link_data['clicks'] ) ? $link_data['clicks'] : '5',
23762 'created_at' => isset( $link_data['created_at'] ) ? $link_data['created_at'] : current_time( 'mysql' ),
23763 ];
23764 };
23765
23766 $format_click = function( $click_data ) {
23767 return [
23768 'click_id' => isset( $click_data['id'] ) ? $click_data['id'] : 42,
23769 'url' => isset( $click_data['url'] ) ? $click_data['url'] : 'https://example.com',
23770 'timestamp' => isset( $click_data['created_at'] ) ? $click_data['created_at'] : current_time( 'mysql' ),
23771 'is_logged_in' => true,
23772 ];
23773 };
23774
23775 switch ( $term ) {
23776 case 'link_clicked':
23777 $link_data = ! empty( $latest_link ) ? $format_link( $latest_link ) : $format_link( [] );
23778 $click_data = ! empty( $latest_click ) ? $format_click( $latest_click ) : $format_click( [] );
23779
23780 // Get user context from latest click or sample data.
23781 $user_id = 0;
23782 if ( ! empty( $latest_click ) ) {
23783 if ( isset( $latest_click['user'] ) && $latest_click['user'] > 0 ) {
23784 $user_id = $latest_click['user'];
23785 } elseif ( isset( $latest_click['user_id'] ) && $latest_click['user_id'] > 0 ) {
23786 $user_id = $latest_click['user_id'];
23787 } elseif ( isset( $latest_click['visitor'] ) && is_numeric( $latest_click['visitor'] ) && $latest_click['visitor'] > 0 ) {
23788 $user_id = $latest_click['visitor'];
23789 }
23790 }
23791
23792 if ( $user_id > 0 ) {
23793 $user_data = $get_user_data( $user_id );
23794 } else {
23795 $user_data = [
23796 'wp_user_id' => 1,
23797 'user_login' => 'sampleuser',
23798 'display_name' => 'Sample User',
23799 'user_firstname' => 'John',
23800 'user_lastname' => 'Doe',
23801 'user_email' => 'sampleuser@example.com',
23802 'user_registered' => current_time( 'mysql' ),
23803 'user_role' => [ 'subscriber' ],
23804 ];
23805 }
23806
23807 $context['pluggable_data'] = array_merge(
23808 $user_data,
23809 [
23810 'link' => $link_data,
23811 'click' => $click_data,
23812 ]
23813 );
23814 $context['response_type'] = ! empty( $latest_link ) ? 'live' : 'sample';
23815 break;
23816 }
23817
23818 return (array) $context;
23819 }
23820
23821 /**
23822 * Get ProfilePress Payment Methods.
23823 *
23824 * @param array $data data.
23825 *
23826 * @return array|void
23827 */
23828 public function search_profilepress_payment_methods( $data ) {
23829
23830 $options = [];
23831 if ( class_exists( '\ProfilePress\Core\Membership\PaymentMethods\PaymentMethods' ) ) {
23832 $payment_methods = \ProfilePress\Core\Membership\PaymentMethods\PaymentMethods::get_instance()->get_all();
23833
23834 $methods = [ '' => 'Default' ];
23835 if ( ! empty( $payment_methods ) ) {
23836 foreach ( $payment_methods as $method ) {
23837 $methods[ $method->get_id() ] = $method->get_method_title();
23838 }
23839 }
23840
23841 $methods = apply_filters( 'ppress_admin_order_page_enabled_payment_methods', $methods );
23842
23843 if ( $methods ) {
23844 foreach ( $methods as $id => $name ) {
23845 $options[] = [
23846 'label' => $name,
23847 'value' => $id,
23848 ];
23849 }
23850 }
23851 }
23852
23853 return [
23854 'options' => $options,
23855 'hasMore' => false,
23856 ];
23857 }
23858
23859 /**
23860 * Get ProfilePress Order Status List.
23861 *
23862 * @param array $data data.
23863 *
23864 * @return array|void
23865 */
23866 public function search_profilepress_order_status_list( $data ) {
23867
23868 $options = [];
23869
23870 if ( class_exists( 'ProfilePress\Core\Membership\Models\Order\OrderStatus' ) ) {
23871 $statuses = \ProfilePress\Core\Membership\Models\Order\OrderStatus::get_all();
23872 if ( $statuses ) {
23873 foreach ( $statuses as $id => $name ) {
23874 $options[] = [
23875 'label' => $name,
23876 'value' => $id,
23877 ];
23878 }
23879 }
23880 }
23881
23882 return [
23883 'options' => $options,
23884 'hasMore' => false,
23885 ];
23886 }
23887
23888 /**
23889 * Get ProfilePress Last Data
23890 *
23891 * @param array $data data.
23892 *
23893 * @return array
23894 */
23895 public function search_profilepress_triggers_last_data( $data ) {
23896 global $wpdb;
23897
23898 $context = [];
23899
23900 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
23901
23902 switch ( $term ) {
23903
23904 /**
23905 * Customer Updated trigger.
23906 */
23907 case 'profilepress_customer_updated':
23908 if ( class_exists( '\ProfilePress\Core\Membership\Models\Customer\CustomerFactory' ) ) {
23909 $customer = $wpdb->get_row(
23910 "SELECT * FROM {$wpdb->prefix}ppress_customers ORDER BY id DESC LIMIT 1",
23911 ARRAY_A
23912 );
23913
23914 if ( $customer ) {
23915 $customer_id = isset( $customer['id'] ) ? (int) $customer['id'] : 0;
23916 $customer = \ProfilePress\Core\Membership\Models\Customer\CustomerFactory::fromId( absint( $customer_id ) );
23917
23918 $pluggable_data = [
23919 'customer' => Utilities::object_to_array( $customer ),
23920 ];
23921
23922 $context = [
23923 'pluggable_data' => $pluggable_data,
23924 'response_type' => 'live',
23925 ];
23926 } else {
23927
23928 $customer = \ProfilePress\Core\Membership\Models\Customer\CustomerFactory::fromId( absint( 0 ) );
23929 $sample_data = [
23930 'customer' => Utilities::object_to_array( $customer ),
23931 ];
23932
23933 $context = [
23934 'pluggable_data' => $sample_data,
23935 'response_type' => 'sample',
23936 ];
23937 }
23938 }
23939
23940 break;
23941
23942 /**
23943 * Subscription Activated trigger.
23944 */
23945 case 'profilepress_subscription_activated':
23946 $latest_subscription = $wpdb->get_row(
23947 'SELECT * FROM ' . $wpdb->prefix . 'ppress_subscriptions ORDER BY id DESC LIMIT 1',
23948 ARRAY_A
23949 );
23950
23951 if ( $latest_subscription ) {
23952 if ( class_exists( '\ProfilePress\Core\Membership\Models\Subscription\SubscriptionFactory' ) ) {
23953 $latest_subscription = \ProfilePress\Core\Membership\Models\Subscription\SubscriptionFactory::fromId( intval( $latest_subscription['id'] ) );
23954 $context['pluggable_data']['subscription'] = Utilities::object_to_array( $latest_subscription );
23955 $context['response_type'] = 'live';
23956 }
23957 } else {
23958 $sample_subscription = [
23959 'id' => 1,
23960 'parent_order_id' => 1,
23961 'plan_id' => 1,
23962 'customer_id' => 7,
23963 'billing_frequency' => 'monthly',
23964 'initial_amount' => '10.00000000',
23965 'initial_tax_rate' => '0',
23966 'initial_tax' => '0',
23967 'recurring_amount' => '10.00000000',
23968 'recurring_tax_rate' => '0',
23969 'recurring_tax' => '0',
23970 'total_payments' => '0',
23971 'trial_period' => 'disabled',
23972 'profile_id' => '',
23973 'status' => 'active',
23974 'notes' => null,
23975 'created_date' => '2025-08-19 11:45:41',
23976 'expiration_date' => '2025-09-19 11:45:41',
23977 ];
23978 $context['pluggable_data']['subscription'] = $sample_subscription;
23979 $context['response_type'] = 'sample';
23980 }
23981 break;
23982
23983 default:
23984 $context['pluggable_data'] = [];
23985 $context['response_type'] = 'error';
23986 break;
23987
23988 }
23989
23990 return $context;
23991 }
23992
23993 /**
23994 * Search FluentCRM Company Data.
23995 *
23996 * @param array $data data.
23997 * @return array
23998 */
23999 public function search_pluggables_fluentcrm_company_created( $data ) {
24000 global $wpdb;
24001
24002 $context = [];
24003 $pluggable_data = [];
24004 $term = ! empty( $data['search_term'] ) ? $data['search_term'] : '';
24005
24006 if ( ! class_exists( 'FluentCrm\App\Models\Company' ) ) {
24007 return [];
24008 }
24009
24010 $result = null;
24011
24012 // Fetch last record based on term.
24013 switch ( $term ) {
24014 case 'contact_deleted':
24015 $result = $wpdb->get_row( "SELECT id FROM {$wpdb->prefix}fc_subscribers ORDER BY id DESC LIMIT 1", ARRAY_A );
24016 break;
24017
24018 case 'campaign_sent':
24019 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fc_campaigns WHERE status = 'archived' ORDER BY updated_at DESC LIMIT 1", ARRAY_A );
24020 break;
24021
24022 case 'company_updated':
24023 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fc_companies ORDER BY updated_at DESC LIMIT 1", ARRAY_A );
24024 break;
24025
24026 case 'company_deleted':
24027 case 'company_assigned':
24028 case 'company_unassigned':
24029 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fc_companies ORDER BY id DESC LIMIT 1", ARRAY_A );
24030 break;
24031
24032 default:
24033 // Default to company_created.
24034 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fc_companies ORDER BY created_at DESC LIMIT 1", ARRAY_A );
24035 break;
24036 }
24037
24038 if ( $result ) {
24039 switch ( $term ) {
24040 case 'contact_deleted':
24041 $pluggable_data = [
24042 'contact_id' => $result['id'],
24043 'deleted_at' => current_time( 'mysql' ),
24044 ];
24045 break;
24046
24047 case 'campaign_sent':
24048 $campaign_data = $result;
24049
24050 if ( ! empty( $campaign_data['settings'] ) ) {
24051 $settings = maybe_unserialize( $campaign_data['settings'] );
24052 if ( is_array( $settings ) ) {
24053 $campaign_data['settings'] = $settings;
24054 }
24055 }
24056
24057 $pluggable_data = [
24058 'campaign' => $campaign_data,
24059 'status' => 'archived',
24060 'archived_at' => current_time( 'mysql' ),
24061 ];
24062
24063 if ( ! empty( $result['id'] ) ) {
24064 $total_emails = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}fc_campaign_emails WHERE campaign_id = %d", $result['id'] ) );
24065 $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'] ) );
24066 $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'] ) );
24067 $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'] ) );
24068
24069 $pluggable_data['total_emails_sent'] = $total_emails;
24070 $pluggable_data['sent_count'] = $sent_count;
24071 $pluggable_data['opened_count'] = $opened_count;
24072 $pluggable_data['clicked_count'] = $clicked_count;
24073 $pluggable_data['open_rate'] = $sent_count > 0 ? round( ( $opened_count / $sent_count ) * 100, 2 ) : 0;
24074 $pluggable_data['click_rate'] = $sent_count > 0 ? round( ( $clicked_count / $sent_count ) * 100, 2 ) : 0;
24075 }
24076 break;
24077
24078 case 'company_deleted':
24079 $pluggable_data = [
24080 'company_id' => $result['id'],
24081 'deleted_at' => current_time( 'mysql' ),
24082 ];
24083 break;
24084
24085 case 'company_assigned':
24086 case 'company_unassigned':
24087 $contact_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fc_subscribers ORDER BY id DESC LIMIT 1", ARRAY_A );
24088 if ( $contact_result ) {
24089 $pluggable_data['contact'] = [
24090 'details' => $contact_result,
24091 'custom' => [],
24092 ];
24093 $pluggable_data['companies'] = [ $result ];
24094
24095 if ( 'company_assigned' === $term ) {
24096 $pluggable_data['assigned_company_ids'] = [ $result['id'] ];
24097 $pluggable_data['assigned_at'] = current_time( 'mysql' );
24098 } else {
24099 $pluggable_data['unassigned_company_ids'] = [ $result['id'] ];
24100 $pluggable_data['unassigned_at'] = current_time( 'mysql' );
24101 }
24102 }
24103 break;
24104
24105 default:
24106 $meta_data = ! empty( $result['meta'] ) ? maybe_unserialize( $result['meta'] ) : [];
24107 $company_result = array_intersect_key(
24108 $result,
24109 array_flip(
24110 [
24111 'id',
24112 'hash',
24113 'name',
24114 'email',
24115 'description',
24116 'address_line_1',
24117 'address_line_2',
24118 'city',
24119 'state',
24120 'postal_code',
24121 'country',
24122 'phone',
24123 'type',
24124 'owner_id',
24125 'employees_number',
24126 'industry',
24127 'website',
24128 'linkedin_url',
24129 'facebook_url',
24130 'twitter_url',
24131 'logo',
24132 'timezone',
24133 'date_of_start',
24134 'created_at',
24135 'updated_at',
24136 ]
24137 )
24138 );
24139
24140 if ( is_array( $meta_data ) && isset( $meta_data['custom_values'] ) ) {
24141 $company_result['custom_fields'] = $meta_data['custom_values'];
24142 }
24143
24144 $pluggable_data['company'] = $company_result;
24145 break;
24146 }
24147 $context['response_type'] = 'live';
24148 } else {
24149 // Sample data for different terms.
24150 switch ( $term ) {
24151 case 'contact_deleted':
24152 $pluggable_data = [
24153 'contact_id' => 1,
24154 'deleted_at' => current_time( 'mysql' ),
24155 ];
24156 break;
24157
24158 case 'campaign_sent':
24159 $pluggable_data = [
24160 'campaign' => [
24161 'id' => 1,
24162 'type' => 'campaign',
24163 'title' => 'Sample Email Campaign',
24164 'status' => 'archived',
24165 'template_id' => 1,
24166 'email_subject' => 'Welcome to Our Newsletter!',
24167 'email_body' => 'Sample campaign email content...',
24168 'utm_status' => 1,
24169 'utm_source' => 'fluentcrm',
24170 'utm_medium' => 'email',
24171 'utm_campaign' => 'sample-campaign',
24172 'created_by' => 1,
24173 'created_at' => current_time( 'mysql' ),
24174 'updated_at' => current_time( 'mysql' ),
24175 'scheduled_at' => current_time( 'mysql' ),
24176 ],
24177 'status' => 'archived',
24178 'archived_at' => current_time( 'mysql' ),
24179 'total_emails_sent' => 1500,
24180 'sent_count' => 1450,
24181 'opened_count' => 725,
24182 'clicked_count' => 145,
24183 'open_rate' => 50.0,
24184 'click_rate' => 10.0,
24185 ];
24186 break;
24187
24188 case 'company_deleted':
24189 $pluggable_data = [
24190 'company_id' => 1,
24191 'deleted_at' => current_time( 'mysql' ),
24192 ];
24193 break;
24194
24195 case 'company_assigned':
24196 case 'company_unassigned':
24197 $pluggable_data['contact']['details'] = [
24198 'id' => 1,
24199 'user_id' => 1,
24200 'hash' => 'sample_hash',
24201 'contact_owner' => 0,
24202 'company_id' => 0,
24203 'prefix' => 'Mr.',
24204 'first_name' => 'John',
24205 'last_name' => 'Doe',
24206 'email' => 'john@example.com',
24207 'timezone' => 'America/New_York',
24208 'address_line_1' => '123 Main Street',
24209 'address_line_2' => 'Apt 4B',
24210 'postal_code' => '10001',
24211 'city' => 'New York',
24212 'state' => 'NY',
24213 'country' => 'USA',
24214 'phone' => '+1-555-123-4567',
24215 'status' => 'subscribed',
24216 'contact_type' => 'customer',
24217 'source' => 'manual',
24218 'lifecycle_stage' => 'customer',
24219 'created_at' => current_time( 'mysql' ),
24220 'updated_at' => current_time( 'mysql' ),
24221 ];
24222 $pluggable_data['contact']['custom'] = [];
24223 $pluggable_data['companies'] = [
24224 [
24225 'id' => 1,
24226 'hash' => 'sample_hash',
24227 'name' => 'Sample Company Inc.',
24228 'email' => 'contact@samplecompany.com',
24229 'description' => 'This is a sample company',
24230 'address_line_1' => '123 Business Street',
24231 'address_line_2' => 'Suite 100',
24232 'city' => 'Business City',
24233 'state' => 'California',
24234 'postal_code' => '90210',
24235 'country' => 'USA',
24236 'phone' => '+1-555-123-4567',
24237 'type' => 'client',
24238 'created_at' => current_time( 'mysql' ),
24239 'updated_at' => current_time( 'mysql' ),
24240 ],
24241 ];
24242 if ( 'company_assigned' === $term ) {
24243 $pluggable_data['assigned_company_ids'] = [ 1 ];
24244 $pluggable_data['assigned_at'] = current_time( 'mysql' );
24245 } else {
24246 $pluggable_data['unassigned_company_ids'] = [ 1 ];
24247 $pluggable_data['unassigned_at'] = current_time( 'mysql' );
24248 }
24249 break;
24250
24251 case 'company_created':
24252 case 'company_updated':
24253 default:
24254 $pluggable_data['company'] = [
24255 'id' => 1,
24256 'hash' => 'sample_hash',
24257 'name' => 'Sample Company Inc.',
24258 'email' => 'contact@samplecompany.com',
24259 'description' => 'This is a sample company',
24260 'address_line_1' => '123 Business Street',
24261 'address_line_2' => 'Suite 100',
24262 'city' => 'Business City',
24263 'state' => 'California',
24264 'postal_code' => '90210',
24265 'country' => 'USA',
24266 'phone' => '+1-555-123-4567',
24267 'type' => 'client',
24268 'owner_id' => 1,
24269 'employees_number' => 50,
24270 'industry' => 'Technology',
24271 'website' => 'https://samplecompany.com',
24272 'linkedin_url' => 'https://linkedin.com/company/samplecompany',
24273 'facebook_url' => 'https://facebook.com/samplecompany',
24274 'twitter_url' => 'https://twitter.com/samplecompany',
24275 'logo' => '',
24276 'timezone' => 'America/Los_Angeles',
24277 'date_of_start' => '2024-01-01',
24278 'created_at' => '2024-01-01 10:00:00',
24279 'updated_at' => '2024-01-01 10:00:00',
24280 'custom_fields' => [
24281 'annual_revenue' => '$1,000,000',
24282 'primary_contact' => 'John Doe',
24283 ],
24284 ];
24285 break;
24286 }
24287
24288 $context['response_type'] = 'sample';
24289 }
24290
24291 $context['pluggable_data'] = $pluggable_data;
24292 return $context;
24293 }
24294
24295 /**
24296 * Get FluentCRM email trigger details.
24297 *
24298 * @param array $data data.
24299 *
24300 * @return array
24301 */
24302 public function search_pluggables_fluentcrm_email_triggers( $data ) {
24303 $context = [];
24304 $pluggable_data = [];
24305 global $wpdb;
24306 $term = $data['search_term'] ? $data['search_term'] : '';
24307
24308 // Helper function to build email data structure.
24309 $build_email_data = function( $email_result, $subscriber_result = null, $subscriber_status = null ) {
24310 $email_data = [
24311 'id' => $email_result['id'],
24312 'status' => $email_result['status'],
24313 'is_open' => $email_result['is_open'],
24314 'is_parsed' => $email_result['is_parsed'],
24315 'created_at' => $email_result['created_at'],
24316 'updated_at' => isset( $email_result['updated_at'] ) ? $email_result['updated_at'] : $email_result['created_at'],
24317 'campaign_id' => isset( $email_result['campaign_id'] ) ? $email_result['campaign_id'] : null,
24318 'scheduled_at' => isset( $email_result['scheduled_at'] ) ? $email_result['scheduled_at'] : $email_result['created_at'],
24319 'email_address' => isset( $email_result['email_address'] ) ? $email_result['email_address'] : ( isset( $subscriber_result['email'] ) ? $subscriber_result['email'] : '' ),
24320 'email_headers' => isset( $email_result['email_headers'] ) ? $email_result['email_headers'] : '',
24321 'email_subject' => isset( $email_result['email_subject'] ) ? $email_result['email_subject'] : '',
24322 'email_body' => $email_result['email_body'],
24323 'email_hash' => $email_result['email_hash'],
24324 'email_type' => $email_result['email_type'],
24325 'subscriber_id' => $email_result['subscriber_id'],
24326 ];
24327
24328 if ( $subscriber_result ) {
24329 $subscriber_fields = [
24330 'hash',
24331 'email',
24332 'phone',
24333 'photo',
24334 'prefix',
24335 'status',
24336 'full_name',
24337 'last_name',
24338 'created_at',
24339 'first_name',
24340 'updated_at',
24341 ];
24342 foreach ( $subscriber_fields as $field ) {
24343 $email_data[ 'subscriber_' . $field ] = $subscriber_result[ $field ];
24344 }
24345 if ( $subscriber_status ) {
24346 $email_data['subscriber_status'] = $subscriber_status;
24347 }
24348 }
24349
24350 return $email_data;
24351 };
24352
24353 // Helper function to get sample email data.
24354 $get_sample_email_data = function( $status = 'subscribed' ) {
24355 return [
24356 'id' => 1,
24357 'status' => 'sent',
24358 'is_open' => 0,
24359 'is_parsed' => 1,
24360 'created_at' => current_time( 'mysql' ),
24361 'updated_at' => current_time( 'mysql' ),
24362 'campaign_id' => 83,
24363 'scheduled_at' => current_time( 'mysql' ),
24364 'email_address' => 'john@example.com',
24365 'email_headers' => 'From: noreply@example.com',
24366 'email_subject' => 'Sample Email Subject',
24367 'email_body' => 'Sample email content here...',
24368 'email_hash' => 'sample-email-hash-123',
24369 'email_type' => 'campaign',
24370 'subscriber_id' => 1,
24371 'subscriber_hash' => 'sample_hash',
24372 'subscriber_email' => 'john@example.com',
24373 'subscriber_phone' => '9876654423',
24374 'subscriber_photo' => 'http://example.com/avatar.png',
24375 'subscriber_prefix' => 'Mr.',
24376 'subscriber_status' => $status,
24377 'subscriber_full_name' => 'John Doe',
24378 'subscriber_last_name' => 'Doe',
24379 'subscriber_created_at' => current_time( 'mysql' ),
24380 'subscriber_first_name' => 'John',
24381 'subscriber_updated_at' => current_time( 'mysql' ),
24382 ];
24383 };
24384
24385 // Get result based on term.
24386 $result = null;
24387 if ( 'email_opened' === $term ) {
24388 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fc_campaign_emails ORDER BY id DESC LIMIT %d", 1 ), ARRAY_A );
24389 } elseif ( 'email_clicked' === $term ) {
24390 $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 );
24391 } elseif ( 'email_unsubscribed' === $term ) {
24392 $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 );
24393 } elseif ( 'email_bounced' === $term ) {
24394 $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 );
24395 }
24396
24397 if ( $result ) {
24398 if ( 'email_opened' === $term || 'email_clicked' === $term ) {
24399 // Get subscriber details.
24400 $subscriber_result = null;
24401 if ( ! empty( $result['subscriber_id'] ) ) {
24402 $subscriber_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fc_subscribers WHERE id = %d", $result['subscriber_id'] ), ARRAY_A );
24403 }
24404
24405 $pluggable_data['email'] = $build_email_data( $result, $subscriber_result );
24406 $pluggable_data[ 'email_opened' === $term ? 'opened_at' : 'clicked_at' ] = current_time( 'mysql' );
24407
24408 } elseif ( 'email_unsubscribed' === $term ) {
24409 // Get email data for subscriber.
24410 $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 );
24411
24412 if ( $email_result ) {
24413 $pluggable_data['email'] = $build_email_data( $email_result, $result );
24414 }
24415
24416 $pluggable_data['source'] = 'from_header';
24417 $pluggable_data['unsubscribed_at'] = current_time( 'mysql' );
24418 } elseif ( 'email_bounced' === $term ) {
24419 // For bounced emails, we only need subscriber object and bounce-specific fields.
24420 $subscriber_data = [];
24421 $subscriber_fields = [
24422 'id',
24423 'hash',
24424 'email',
24425 'phone',
24426 'photo',
24427 'prefix',
24428 'status',
24429 'full_name',
24430 'last_name',
24431 'created_at',
24432 'first_name',
24433 'updated_at',
24434 'contact_type',
24435 'total_points',
24436 'last_activity',
24437 'life_time_value',
24438 'ip',
24439 ];
24440
24441 foreach ( $subscriber_fields as $field ) {
24442 if ( isset( $result[ $field ] ) ) {
24443 $subscriber_data[ $field ] = $result[ $field ];
24444 }
24445 }
24446
24447 $pluggable_data['subscriber'] = $subscriber_data;
24448
24449 // Add bounce-specific data.
24450 $pluggable_data['old_status'] = 'subscribed';
24451 $pluggable_data['new_status'] = 'bounced';
24452 $pluggable_data['bounced_at'] = $result['updated_at'];
24453 }
24454 $context['response_type'] = 'live';
24455 } else {
24456 // Sample data for different email terms.
24457 if ( 'email_opened' === $term || 'email_clicked' === $term ) {
24458 $pluggable_data['email'] = $get_sample_email_data();
24459
24460 if ( 'email_opened' === $term ) {
24461 $pluggable_data['opened_at'] = current_time( 'mysql' );
24462 } else {
24463 $pluggable_data['url'] = [
24464 'id' => 1,
24465 'url' => 'https://example.com/sample-link',
24466 'short_url' => 'https://example.com/r/abc123',
24467 'title' => 'Sample Link',
24468 'created_at' => current_time( 'mysql' ),
24469 ];
24470 $pluggable_data['clicked_at'] = current_time( 'mysql' );
24471 }
24472 } elseif ( 'email_unsubscribed' === $term ) {
24473 $pluggable_data['email'] = $get_sample_email_data( 'unsubscribed' );
24474 $pluggable_data['source'] = 'from_header';
24475 $pluggable_data['unsubscribed_at'] = current_time( 'mysql' );
24476 } elseif ( 'email_bounced' === $term ) {
24477 // Sample subscriber object for bounced email (no email object).
24478 $pluggable_data['subscriber'] = [
24479 'id' => 8,
24480 'ip' => '::1',
24481 'hash' => '6b2c0a83138a7e42ef12e6f87e8c58aa',
24482 'email' => 'mrunalis@bsf.io',
24483 'phone' => '9876654423',
24484 'photo' => 'http://ottokit.local/wp-content/plugins/fluent-crm/assets/images/avatar.png',
24485 'prefix' => 'Ms',
24486 'status' => 'bounced',
24487 'full_name' => 'Mrunali Salunke',
24488 'last_name' => 'Salunke',
24489 'created_at' => '2025-09-18 04:51:26',
24490 'first_name' => 'Mrunali',
24491 'updated_at' => current_time( 'mysql' ),
24492 'contact_type' => 'lead',
24493 'total_points' => 0,
24494 'last_activity' => '2025-09-25 05:47:45',
24495 'life_time_value' => 0,
24496 ];
24497 $pluggable_data['old_status'] = 'subscribed';
24498 $pluggable_data['new_status'] = 'bounced';
24499 $pluggable_data['bounced_at'] = current_time( 'mysql' );
24500 }
24501 $context['response_type'] = 'sample';
24502 }
24503
24504 $context['pluggable_data'] = $pluggable_data;
24505 return $context;
24506 }
24507
24508 /**
24509 * Get WooCommerce Subscription triggers last data.
24510 *
24511 * @param array $data data.
24512 *
24513 * @return array
24514 */
24515 public function search_woocommerce_subscription_triggers_last_data( $data ) {
24516 $context = [];
24517 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
24518
24519 if ( ! class_exists( 'WC_Subscription' ) || ! function_exists( 'wcs_get_subscription' ) ) {
24520 return $context;
24521 }
24522
24523 $needs_subscription = in_array(
24524 $term,
24525 [
24526 'wc_subscription_updated',
24527 'wc_cancels_subscription_product',
24528 'wc_subscribes_product',
24529 'wc_purchases_variable_subscription',
24530 'wc_renews_subscription_product',
24531 'wc_subscription_product_expires',
24532 ],
24533 true
24534 );
24535
24536 if ( ! $needs_subscription ) {
24537 return $context;
24538 }
24539
24540 $subscription = null;
24541 $subscription_obj = null;
24542
24543 if ( function_exists( 'wcs_get_subscriptions' ) ) {
24544 $subscriptions = wcs_get_subscriptions(
24545 [
24546 'subscriptions_per_page' => 1,
24547 'orderby' => 'date',
24548 'order' => 'DESC',
24549 ]
24550 );
24551
24552 if ( ! empty( $subscriptions ) ) {
24553 $subscription_obj = reset( $subscriptions );
24554 $subscription = (object) [ 'ID' => $subscription_obj->get_id() ];
24555 }
24556 }
24557
24558 if ( $subscription && ! $subscription_obj ) {
24559 $subscription_obj = wcs_get_subscription( $subscription->ID );
24560 }
24561
24562 if ( $subscription_obj ) {
24563 $user_id = $subscription_obj->get_user_id();
24564 $items = $subscription_obj->get_items();
24565 $product_id = 0;
24566 $variation_id = 0;
24567 $product_name = '';
24568 $variation_name = '';
24569
24570 foreach ( $items as $item ) {
24571 $product_id = $item->get_product_id();
24572 $variation_id = $item->get_variation_id();
24573 $product_name = $item->get_name();
24574 $variation_name = $item->get_name();
24575 break;
24576 }
24577
24578 $user_billing_data = [
24579 'billing_first_name' => $subscription_obj->get_billing_first_name(),
24580 'billing_last_name' => $subscription_obj->get_billing_last_name(),
24581 'billing_country' => $subscription_obj->get_billing_country(),
24582 'billing_address_1' => $subscription_obj->get_billing_address_1(),
24583 'billing_address_2' => $subscription_obj->get_billing_address_2(),
24584 'billing_city' => $subscription_obj->get_billing_city(),
24585 'billing_state' => $subscription_obj->get_billing_state(),
24586 'billing_postcode' => $subscription_obj->get_billing_postcode(),
24587 'billing_phone' => $subscription_obj->get_billing_phone(),
24588 'billing_email' => $subscription_obj->get_billing_email(),
24589 ];
24590
24591 if ( 'wc_subscription_updated' === $term ) {
24592 $subscription_data = [
24593 'start_date' => $subscription_obj->get_date_created(),
24594 'next_payment_date' => $subscription_obj->get_date( 'next_payment' ) ? $subscription_obj->get_date( 'next_payment' ) : 0,
24595 ];
24596
24597 $context['variation_id'] = $variation_id;
24598 $context['product_id'] = $product_id;
24599 $context['subscription'] = $product_id;
24600 $context['variation_name'] = $variation_name;
24601 $context['product_name'] = $product_name;
24602 $context['subscription_data'] = $subscription_data;
24603 $context['subscription_id'] = $subscription_obj->get_id();
24604 $context['user'] = WordPress::get_user_context( $user_id );
24605 $context['user_billing_data'] = $user_billing_data;
24606 $context['new_status'] = $subscription_obj->get_status();
24607 $context['old_status'] = 'pending-cancel';
24608 $context['status'] = 'wc-' . $subscription_obj->get_status();
24609 } elseif ( 'wc_cancels_subscription_product' === $term ) {
24610 $subscription_data = [
24611 'status' => $subscription_obj->get_status(),
24612 'start_date' => $subscription_obj->get_date_created(),
24613 ];
24614
24615 $context['subscription_data'] = $subscription_data;
24616 $context['user'] = WordPress::get_user_context( $user_id );
24617 $context['user_billing_data'] = $user_billing_data;
24618 $context['subscription_id'] = $subscription_obj->get_id();
24619 } elseif ( 'wc_subscribes_product' === $term ) {
24620 $subscription_data = [
24621 'status' => $subscription_obj->get_status(),
24622 'start_date' => $subscription_obj->get_date_created(),
24623 'next_payment_date' => $subscription_obj->get_date( 'next_payment' ) ? $subscription_obj->get_date( 'next_payment' ) : 0,
24624 ];
24625
24626 $context['subscription_data'] = $subscription_data;
24627 $context['subscription_id'] = $subscription_obj->get_id();
24628 $context['user'] = WordPress::get_user_context( $user_id );
24629 $context['user_billing_data'] = $user_billing_data;
24630 $context['subscription'] = $subscription_obj->get_id();
24631 $context['subscription_name'] = get_the_title( $product_id );
24632 } elseif ( 'wc_purchases_variable_subscription' === $term ) {
24633 $subscription_data = [
24634 'status' => $subscription_obj->get_status(),
24635 'start_date' => $subscription_obj->get_date_created(),
24636 'next_payment_date' => $subscription_obj->get_date( 'next_payment' ) ? $subscription_obj->get_date( 'next_payment' ) : 0,
24637 ];
24638
24639 $context['subscription_data'] = $subscription_data;
24640 $context['user'] = WordPress::get_user_context( $user_id );
24641 $context['user_billing_data'] = $user_billing_data;
24642 $context['variable_subscription_option'] = $variation_id;
24643 $context['variable_subscription'] = $product_id;
24644 $context['subscription_name'] = $variation_name;
24645 $context['subscription_id'] = $subscription_obj->get_id();
24646 } elseif ( 'wc_renews_subscription_product' === $term ) {
24647 $subscription_data = [
24648 'status' => $subscription_obj->get_status(),
24649 'start_date' => $subscription_obj->get_date_created(),
24650 'next_payment_date' => $subscription_obj->get_date( 'next_payment' ) ? $subscription_obj->get_date( 'next_payment' ) : 0,
24651 ];
24652
24653 $context['subscription'] = $product_id;
24654 $context['subscription_name'] = get_the_title( $product_id );
24655 $context['last_order'] = $subscription_obj->get_last_order();
24656 $context['subscription_data'] = $subscription_data;
24657 $context['subscription_id'] = $subscription_obj->get_id();
24658 $context['user'] = WordPress::get_user_context( $user_id );
24659 $context['user_billing_data'] = $user_billing_data;
24660 } elseif ( 'wc_subscription_product_expires' === $term ) {
24661 $subscription_data = [
24662 'status' => $subscription_obj->get_status(),
24663 'start_date' => $subscription_obj->get_date_created(),
24664 'next_payment_date' => $subscription_obj->get_date( 'next_payment' ) ? $subscription_obj->get_date( 'next_payment' ) : 0,
24665 ];
24666
24667 $context['subscription_data'] = $subscription_data;
24668 $context['subscription_id'] = $subscription_obj->get_id();
24669 $context['user'] = WordPress::get_user_context( $user_id );
24670 $context['user_billing_data'] = $user_billing_data;
24671 $context['subscription'] = $product_id;
24672 $context['subscription_name'] = get_the_title( $product_id );
24673 }
24674
24675 $context['pluggable_data'] = $context;
24676 $context['response_type'] = 'live';
24677 } else {
24678 $sample_user = [
24679 'wp_user_id' => 1,
24680 'user_login' => 'johndoe',
24681 'display_name' => 'John Doe',
24682 'user_firstname' => 'John',
24683 'user_lastname' => 'Doe',
24684 'user_email' => 'john.doe@example.com',
24685 'user_registered' => '2024-01-15 10:30:25',
24686 'user_role' => [
24687 'customer',
24688 'subscriber',
24689 ],
24690 ];
24691
24692 $sample_billing_data = [
24693 'billing_first_name' => 'John',
24694 'billing_last_name' => 'Doe',
24695 'billing_country' => 'US',
24696 'billing_address_1' => '123 Main Street',
24697 'billing_address_2' => 'Apt 4B',
24698 'billing_city' => 'New York',
24699 'billing_state' => 'NY',
24700 'billing_postcode' => '10001',
24701 'billing_phone' => '+1-555-123-4567',
24702 'billing_email' => 'john.doe@example.com',
24703 ];
24704
24705 $sample_start_date = [
24706 'utc_offset' => 0,
24707 'date' => '2024-01-15 08:30:25.000000',
24708 'timezone_type' => 1,
24709 'timezone' => '+00:00',
24710 ];
24711
24712 if ( 'wc_subscription_updated' === $term ) {
24713 $context = [
24714 'variation_id' => 0,
24715 'product_id' => 73,
24716 'subscription' => 73,
24717 'variation_name' => 'Hi-Fi Headphones',
24718 'product_name' => 'Hi-Fi Headphones',
24719 'subscription_data' => [
24720 'start_date' => $sample_start_date,
24721 'next_payment_date' => '2024-12-15 08:30:25',
24722 ],
24723 'subscription_id' => 1234,
24724 'user' => $sample_user,
24725 'user_billing_data' => $sample_billing_data,
24726 'new_status' => 'active',
24727 'old_status' => 'pending-cancel',
24728 'status' => 'wc-active',
24729 ];
24730 } elseif ( 'wc_cancels_subscription_product' === $term ) {
24731 $context = [
24732 'subscription_data' => [
24733 'status' => 'cancelled',
24734 'start_date' => $sample_start_date,
24735 ],
24736 'user' => $sample_user,
24737 'user_billing_data' => $sample_billing_data,
24738 'subscription_id' => 1567,
24739 ];
24740 } elseif ( 'wc_subscribes_product' === $term ) {
24741 $context = [
24742 'subscription_data' => [
24743 'status' => 'active',
24744 'start_date' => $sample_start_date,
24745 'next_payment_date' => '2025-10-07 08:38:53',
24746 ],
24747 'subscription_id' => 1798,
24748 'user' => $sample_user,
24749 'user_billing_data' => $sample_billing_data,
24750 'subscription' => 1795,
24751 'subscription_name' => 'Subscription Product',
24752 ];
24753 } elseif ( 'wc_purchases_variable_subscription' === $term ) {
24754 $context = [
24755 'subscription_data' => [
24756 'status' => 'active',
24757 'start_date' => $sample_start_date,
24758 'next_payment_date' => '2025-11-06 05:25:01',
24759 ],
24760 'user' => $sample_user,
24761 'user_billing_data' => $sample_billing_data,
24762 'variable_subscription_option' => 1803,
24763 'variable_subscription' => 1802,
24764 'subscription_name' => 'Variation test product – 1200, 10',
24765 'subscription_id' => 1836,
24766 ];
24767 } elseif ( 'wc_renews_subscription_product' === $term ) {
24768 $context = [
24769 'subscription' => 1234,
24770 'subscription_name' => 'Monthly Subscription Product',
24771 'last_order' => 5678,
24772 'subscription_data' => [
24773 'status' => 'active',
24774 'start_date' => $sample_start_date,
24775 'next_payment_date' => '2025-11-06 08:30:25',
24776 ],
24777 'subscription_id' => 1567,
24778 'user' => $sample_user,
24779 'user_billing_data' => $sample_billing_data,
24780 ];
24781 } elseif ( 'wc_subscription_product_expires' === $term ) {
24782 $context = [
24783 'subscription_data' => [
24784 'status' => 'expired',
24785 'start_date' => $sample_start_date,
24786 'next_payment_date' => '2025-01-15 08:30:25',
24787 ],
24788 'subscription_id' => 1890,
24789 'user' => $sample_user,
24790 'user_billing_data' => $sample_billing_data,
24791 'subscription' => 1567,
24792 'subscription_name' => 'Premium Subscription Product',
24793 ];
24794 }
24795 $context['pluggable_data'] = $context;
24796 $context['response_type'] = 'sample';
24797 }
24798
24799 return $context;
24800 }
24801
24802 /**
24803 * Prepare FluentCart Product list.
24804 *
24805 * @param array $data data.
24806 *
24807 * @return array
24808 */
24809 public function search_fluentcart_product_list( $data ) {
24810 $options = [];
24811
24812 if ( ! class_exists( '\FluentCart\App\Models\Product' ) ) {
24813 return [
24814 'options' => $options,
24815 'hasMore' => false,
24816 ];
24817 }
24818
24819 $page = isset( $data['page'] ) ? max( 1, intval( $data['page'] ) ) : 1;
24820 $limit = Utilities::get_search_page_limit();
24821 $offset = $limit * ( $page - 1 );
24822
24823 $search_term = isset( $data['search_term'] ) ? $data['search_term'] : '';
24824
24825 // Start with a basic query.
24826 $query = \FluentCart\App\Models\Product::where( 'post_status', 'publish' );
24827
24828 if ( ! empty( $search_term ) ) {
24829 $query->where( 'post_title', 'LIKE', '%' . $search_term . '%' );
24830 }
24831
24832 $products = $query->select( [ 'ID', 'post_title' ] )
24833 ->offset( $offset )
24834 ->limit( $limit )
24835 ->get();
24836
24837 if ( ! empty( $products ) ) {
24838 foreach ( $products as $product ) {
24839 $options[] = [
24840 'label' => $product->post_title,
24841 'value' => $product->ID,
24842 ];
24843 }
24844 }
24845
24846 // Get total count for pagination.
24847 $total_query = \FluentCart\App\Models\Product::where( 'post_status', 'publish' );
24848
24849 if ( ! empty( $search_term ) ) {
24850 $total_query->where( 'post_title', 'LIKE', '%' . $search_term . '%' );
24851 }
24852
24853 $posts_count = $total_query->count();
24854
24855 return [
24856 'options' => $options,
24857 'hasMore' => $posts_count > ( $offset + $limit ),
24858 ];
24859 }
24860
24861 /**
24862 * Search SureDash triggers last data.
24863 *
24864 * @param array $data query params.
24865 *
24866 * @return array
24867 * @since 1.0.0
24868 */
24869 public function search_suredash_triggers_last_data( $data ) {
24870 $context = [];
24871 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
24872
24873 global $wpdb;
24874
24875 // Sample user data reused across all cases.
24876 $sample_user = [
24877 'user_id' => 789,
24878 'user_login' => 'john_doe',
24879 'user_email' => 'john@example.com',
24880 'user_nicename' => 'John Doe',
24881 'display_name' => 'John Doe',
24882 ];
24883
24884 switch ( $term ) {
24885 case 'suredash_comments_data':
24886 $recent_comments = $wpdb->get_results(
24887 $wpdb->prepare(
24888 "SELECT c.*, p.post_title, p.post_content, p.post_author, p.post_type, p.post_status
24889 FROM {$wpdb->comments} c
24890 LEFT JOIN {$wpdb->posts} p ON c.comment_post_ID = p.ID
24891 WHERE c.comment_approved = 1
24892 ORDER BY c.comment_date DESC
24893 LIMIT %d",
24894 1
24895 )
24896 );
24897
24898 if ( ! empty( $recent_comments ) ) {
24899 $comment = $recent_comments[0];
24900 $user_context = [];
24901 if ( $comment->user_id ) {
24902 $user_context = WordPress::get_user_context( $comment->user_id );
24903 }
24904
24905 $context = array_merge(
24906 [
24907 'comment_id' => $comment->comment_ID,
24908 'comment_content' => $comment->comment_content,
24909 'comment_author' => $comment->comment_author,
24910 'comment_author_email' => $comment->comment_author_email,
24911 'comment_date' => $comment->comment_date,
24912 'comment_approved' => $comment->comment_approved,
24913 'comment_type' => $comment->comment_type,
24914 'comment_parent' => $comment->comment_parent,
24915 'post_id' => $comment->comment_post_ID,
24916 'post_title' => $comment->post_title,
24917 'post_content' => $comment->post_content,
24918 'post_author' => $comment->post_author,
24919 'post_type' => $comment->post_type,
24920 'post_status' => $comment->post_status,
24921 'post_permalink' => get_permalink( $comment->comment_post_ID ),
24922 'suredash_post' => $comment->comment_post_ID,
24923 ],
24924 $user_context
24925 );
24926 $context['pluggable_data'] = $context;
24927 $context['response_type'] = 'live';
24928 } else {
24929 $context = [
24930 'comment_id' => 123,
24931 'comment_content' => 'This is a sample comment submission',
24932 'comment_author' => 'John Doe',
24933 'comment_author_email' => 'john@example.com',
24934 'comment_date' => '2024-01-15 10:30:00',
24935 'comment_approved' => 1,
24936 'comment_type' => 'comment',
24937 'comment_parent' => 0,
24938 'post_id' => 456,
24939 'post_title' => 'Sample Course Title',
24940 'post_content' => 'Course content here...',
24941 'post_author' => 1,
24942 'post_type' => 'suredash_course',
24943 'post_status' => 'publish',
24944 'post_permalink' => 'https://example.com/course/sample-course',
24945 'suredash_post' => 456,
24946 ];
24947 $context = array_merge( $context, $sample_user );
24948
24949 $context['pluggable_data'] = $context;
24950 $context['response_type'] = 'sample';
24951 }
24952
24953 return $context;
24954
24955 case 'suredash_comment_deleted_data':
24956 $recent_comments = $wpdb->get_results(
24957 $wpdb->prepare(
24958 "SELECT c.comment_ID, c.user_id
24959 FROM {$wpdb->comments} c
24960 ORDER BY c.comment_date DESC
24961 LIMIT %d",
24962 1
24963 )
24964 );
24965
24966 if ( ! empty( $recent_comments ) ) {
24967 $comment = $recent_comments[0];
24968 $user_context = WordPress::get_user_context( $comment->user_id );
24969
24970 $context = array_merge(
24971 [
24972 'comment_id' => $comment->comment_ID,
24973 ],
24974 $user_context
24975 );
24976
24977 $context['pluggable_data'] = $context;
24978 $context['response_type'] = 'live';
24979 } else {
24980 $context = array_merge( [ 'comment_id' => 123 ], $sample_user );
24981
24982 $context['pluggable_data'] = $context;
24983 $context['response_type'] = 'sample';
24984 }
24985
24986 return $context;
24987
24988 case 'suredash_post_deleted_data':
24989 $recent_posts = $wpdb->get_results(
24990 $wpdb->prepare(
24991 "SELECT ID, post_author
24992 FROM {$wpdb->posts}
24993 ORDER BY post_date DESC
24994 LIMIT %d",
24995 1
24996 )
24997 );
24998
24999 if ( ! empty( $recent_posts ) ) {
25000 $post = $recent_posts[0];
25001 $user_context = WordPress::get_user_context( $post->post_author );
25002
25003 $context = array_merge(
25004 [
25005 'post_id' => $post->ID,
25006 'deleter_user_id' => $post->post_author,
25007 ],
25008 $user_context
25009 );
25010
25011 $context['pluggable_data'] = $context;
25012 $context['response_type'] = 'live';
25013 } else {
25014 $context = array_merge(
25015 [
25016 'post_id' => 456,
25017 'deleter_user_id' => 789,
25018 ],
25019 $sample_user
25020 );
25021
25022 $context['pluggable_data'] = $context;
25023 $context['response_type'] = 'sample';
25024 }
25025
25026 return $context;
25027
25028 case 'suredash_post_data':
25029 $recent_posts = $wpdb->get_results(
25030 $wpdb->prepare(
25031 "SELECT * FROM {$wpdb->posts}
25032 WHERE post_status = 'publish'
25033 AND post_type NOT IN ('revision', 'nav_menu_item', 'attachment')
25034 ORDER BY post_date DESC
25035 LIMIT %d",
25036 1
25037 )
25038 );
25039
25040 if ( ! empty( $recent_posts ) ) {
25041 $post = $recent_posts[0];
25042 $user_context = WordPress::get_user_context( $post->post_author );
25043
25044 $context = array_merge(
25045 [
25046 'post_id' => $post->ID,
25047 'post_title' => $post->post_title,
25048 'post_content' => $post->post_content,
25049 'post_excerpt' => $post->post_excerpt,
25050 'post_status' => $post->post_status,
25051 'post_author' => $post->post_author,
25052 'post_date' => $post->post_date,
25053 'post_modified' => $post->post_modified,
25054 'post_type' => $post->post_type,
25055 'post_name' => $post->post_name,
25056 'post_permalink' => get_permalink( $post->ID ),
25057 'suredash_post' => $post->ID,
25058 'form_data' => [],
25059 ],
25060 $user_context
25061 );
25062
25063 $context['pluggable_data'] = $context;
25064 $context['response_type'] = 'live';
25065 } else {
25066 $context = [
25067 'post_id' => 456,
25068 'post_title' => 'Sample Post Title',
25069 'post_content' => 'This is sample post content for SureDash...',
25070 'post_excerpt' => 'Sample excerpt',
25071 'post_status' => 'publish',
25072 'post_author' => 1,
25073 'post_date' => '2024-01-15 10:30:00',
25074 'post_modified' => '2024-01-15 10:35:00',
25075 'post_type' => 'post',
25076 'post_name' => 'sample-post-title',
25077 'post_permalink' => 'https://example.com/sample-post-title',
25078 'suredash_post' => 456,
25079 'form_data' => [
25080 'post_title' => 'Sample Post Title',
25081 'post_content' => 'Sample content',
25082 ],
25083 ];
25084 $context = array_merge( $context, $sample_user );
25085
25086 $context['pluggable_data'] = $context;
25087 $context['response_type'] = 'sample';
25088 }
25089
25090 return $context;
25091
25092 case 'suredash_item_bookmarked':
25093 $recent_posts = $wpdb->get_results(
25094 $wpdb->prepare(
25095 "SELECT * FROM {$wpdb->posts}
25096 WHERE post_status = 'publish'
25097 AND post_type NOT IN ('revision', 'nav_menu_item', 'attachment')
25098 ORDER BY post_date DESC
25099 LIMIT %d",
25100 1
25101 )
25102 );
25103
25104 if ( ! empty( $recent_posts ) ) {
25105 $post = $recent_posts[0];
25106 $user_context = WordPress::get_user_context( $post->post_author );
25107
25108 $context = array_merge(
25109 [
25110 'item_id' => $post->ID,
25111 'item_type' => $post->post_type,
25112 'bookmark_status' => 'bookmarked',
25113 'is_bookmarked' => true,
25114 'is_unbookmarked' => false,
25115 'item_title' => $post->post_title,
25116 'item_content' => $post->post_content,
25117 'item_excerpt' => $post->post_excerpt,
25118 'item_author' => $post->post_author,
25119 'item_date' => $post->post_date,
25120 'item_status' => $post->post_status,
25121 'item_permalink' => get_permalink( $post->ID ),
25122 ],
25123 $user_context
25124 );
25125
25126 $context['pluggable_data'] = $context;
25127 $context['response_type'] = 'live';
25128 } else {
25129 $context = [
25130 'item_id' => 456,
25131 'item_type' => 'post',
25132 'bookmark_status' => 'bookmarked',
25133 'is_bookmarked' => true,
25134 'is_unbookmarked' => false,
25135 'item_title' => 'Sample Item Title',
25136 'item_content' => 'This is sample item content for bookmarking...',
25137 'item_excerpt' => 'Sample item excerpt',
25138 'item_author' => 1,
25139 'item_date' => '2024-01-15 10:30:00',
25140 'item_status' => 'publish',
25141 'item_permalink' => 'https://example.com/sample-item-title',
25142 ];
25143 $context = array_merge( $context, $sample_user );
25144
25145 $context['pluggable_data'] = $context;
25146 $context['response_type'] = 'sample';
25147 }
25148
25149 return $context;
25150
25151 case 'suredash_space_data':
25152 $recent_posts = $wpdb->get_results(
25153 $wpdb->prepare(
25154 "SELECT * FROM {$wpdb->posts}
25155 WHERE post_status = 'publish'
25156 AND post_type NOT IN ('revision', 'nav_menu_item', 'attachment')
25157 ORDER BY post_date DESC
25158 LIMIT %d",
25159 1
25160 )
25161 );
25162
25163 if ( ! empty( $recent_posts ) ) {
25164 $post = $recent_posts[0];
25165
25166 $context = [
25167 'space_id' => $post->ID,
25168 'space_status' => 'deleted',
25169 ];
25170
25171 $context['pluggable_data'] = $context;
25172 $context['response_type'] = 'live';
25173 } else {
25174 $context = [
25175 'space_id' => 456,
25176 'space_status' => 'deleted',
25177 ];
25178
25179 $context['pluggable_data'] = $context;
25180 $context['response_type'] = 'sample';
25181 }
25182
25183 return $context;
25184
25185 default:
25186 return $context;
25187 }
25188 }
25189 /**
25190 * Get MailerPress contact lists
25191 *
25192 * @param array $data data.
25193 *
25194 * @return array
25195 */
25196 public function search_mailerpress_contact_lists( $data ) {
25197 global $wpdb;
25198
25199 $lists = $wpdb->get_results( "SELECT name, list_id FROM {$wpdb->prefix}mailerpress_lists ORDER BY name ASC" );
25200 $options = [];
25201
25202 if ( ! empty( $lists ) ) {
25203 foreach ( $lists as $list ) {
25204 $options[] = [
25205 'label' => $list->name,
25206 'value' => $list->list_id,
25207 ];
25208 }
25209 }
25210
25211 return [
25212 'options' => $options,
25213 'hasMore' => false,
25214 ];
25215 }
25216
25217 /**
25218 * Get MailerPress contact tags
25219 *
25220 * @param array $data data.
25221 *
25222 * @return array
25223 */
25224 public function search_mailerpress_contact_tags( $data ) {
25225
25226 global $wpdb;
25227
25228 $tags = $wpdb->get_results( "SELECT name, tag_id FROM {$wpdb->prefix}mailerpress_tags ORDER BY name ASC" );
25229 $options = [];
25230
25231 if ( ! empty( $tags ) ) {
25232 foreach ( $tags as $tag ) {
25233 $options[] = [
25234 'label' => $tag->name,
25235 'value' => $tag->tag_id,
25236 ];
25237 }
25238 }
25239
25240 return [
25241 'options' => $options,
25242 'hasMore' => false,
25243 ];
25244 }
25245
25246 /**
25247 * Get MailerPress last data
25248 *
25249 * @param array $data data.
25250 *
25251 * @return array
25252 */
25253 public function search_mailerpress_last_data( $data ) {
25254 $context = [];
25255 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
25256
25257 global $wpdb;
25258
25259 switch ( $term ) {
25260 case 'mailerpress_contact_data':
25261 $recent_contact = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_contact ORDER BY created_at DESC LIMIT 1" );
25262
25263 if ( $recent_contact ) {
25264 $context = [
25265 'contact_id' => $recent_contact->contact_id,
25266 'email' => $recent_contact->email,
25267 'first_name' => $recent_contact->first_name,
25268 'last_name' => $recent_contact->last_name,
25269 'subscription_status' => $recent_contact->subscription_status,
25270 'opt_in_source' => $recent_contact->opt_in_source,
25271 'opt_in_details' => $recent_contact->opt_in_details,
25272 'created_at' => $recent_contact->created_at,
25273 'updated_at' => $recent_contact->updated_at,
25274 ];
25275 $context['pluggable_data'] = $context;
25276 $context['response_type'] = 'live';
25277 } else {
25278 $context = [
25279 'contact_id' => 15,
25280 'email' => 'sarah.johnson@example.com',
25281 'first_name' => 'Sarah',
25282 'last_name' => 'Johnson',
25283 'subscription_status' => 'subscribed',
25284 'opt_in_source' => 'signup_form',
25285 'opt_in_details' => 'Newsletter signup - Homepage',
25286 'created_at' => '2025-12-01 14:23:17',
25287 'updated_at' => '2025-12-01 14:23:17',
25288 ];
25289 $context['pluggable_data'] = $context;
25290 $context['response_type'] = 'sample';
25291 }
25292 break;
25293
25294 case 'list_created':
25295 $recent_list = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_lists ORDER BY created_at DESC LIMIT 1" );
25296
25297 if ( $recent_list ) {
25298 $user_data = get_userdata( 1 );
25299
25300 $context['pluggable_data'] = [
25301 'wp_user_id' => $user_data ? $user_data->ID : 1,
25302 'user_login' => $user_data ? $user_data->user_login : 'root',
25303 'display_name' => $user_data ? $user_data->display_name : 'Arian',
25304 'user_firstname' => get_user_meta( $user_data ? $user_data->ID : 1, 'first_name', true ) ? get_user_meta( $user_data ? $user_data->ID : 1, 'first_name', true ) : '',
25305 'user_lastname' => get_user_meta( $user_data ? $user_data->ID : 1, 'last_name', true ) ? get_user_meta( $user_data ? $user_data->ID : 1, 'last_name', true ) : '',
25306 'user_email' => $user_data ? $user_data->user_email : 'dev-email@wpengine.local',
25307 'user_registered' => $user_data ? $user_data->user_registered : '2025-07-02 10:50:38',
25308 'user_role' => $user_data ? $user_data->roles : [ 'administrator' ],
25309 'list' => [
25310 'id' => $recent_list->list_id,
25311 'name' => $recent_list->name,
25312 'description' => $recent_list->description,
25313 'created_at' => $recent_list->created_at,
25314 'updated_at' => $recent_list->updated_at,
25315 ],
25316 ];
25317 $context['response_type'] = 'live';
25318 } else {
25319 $context['pluggable_data'] = [
25320 'wp_user_id' => 1,
25321 'user_login' => 'root',
25322 'display_name' => 'Arian',
25323 'user_firstname' => '',
25324 'user_lastname' => '',
25325 'user_email' => 'dev-email@wpengine.local',
25326 'user_registered' => '2025-07-02 10:50:38',
25327 'user_role' => [ 'administrator' ],
25328 'list' => [
25329 'id' => 7,
25330 'name' => 'Weekly Newsletter Subscribers',
25331 'description' => 'Subscribers who opted in for weekly newsletter updates',
25332 'created_at' => '2025-12-01 10:15:23',
25333 'updated_at' => '2025-12-01 15:30:45',
25334 ],
25335 ];
25336 $context['response_type'] = 'sample';
25337 }
25338 break;
25339
25340 case 'contact_added_to_list':
25341 $recent_list = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_lists ORDER BY created_at DESC LIMIT 1" );
25342 $recent_contact = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_contact ORDER BY created_at DESC LIMIT 1" );
25343
25344 if ( $recent_contact && $recent_list ) {
25345 $context['pluggable_data'] = [
25346 'contact' => [
25347 'id' => $recent_contact->contact_id,
25348 'email' => $recent_contact->email,
25349 'first_name' => $recent_contact->first_name,
25350 'last_name' => $recent_contact->last_name,
25351 'subscription_status' => $recent_contact->subscription_status,
25352 'created_at' => $recent_contact->created_at,
25353 'updated_at' => $recent_contact->updated_at,
25354 ],
25355 'list' => [
25356 'id' => $recent_list->list_id,
25357 'name' => $recent_list->name,
25358 'description' => $recent_list->description,
25359 'created_at' => $recent_list->created_at,
25360 'updated_at' => $recent_list->updated_at,
25361 ],
25362 ];
25363 $context['response_type'] = 'live';
25364 } else {
25365 $context['pluggable_data'] = [
25366 'contact' => [
25367 'id' => 12,
25368 'email' => 'michael.chen@company.com',
25369 'first_name' => 'Michael',
25370 'last_name' => 'Chen',
25371 'subscription_status' => 'subscribed',
25372 'created_at' => '2025-12-01 09:15:33',
25373 'updated_at' => '2025-12-01 11:22:47',
25374 ],
25375 'list' => [
25376 'id' => 7,
25377 'name' => 'Weekly Newsletter Subscribers',
25378 'description' => 'Subscribers who opted in for weekly newsletter updates',
25379 'created_at' => '2025-12-01 10:15:23',
25380 'updated_at' => '2025-12-01 15:30:45',
25381 ],
25382 ];
25383 $context['response_type'] = 'sample';
25384 }
25385 break;
25386
25387 case 'tag_created':
25388 $recent_tag = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_tags LIMIT 1" );
25389
25390 if ( $recent_tag ) {
25391 $context['pluggable_data'] = [
25392 'tag_id' => $recent_tag->tag_id,
25393 'name' => $recent_tag->name,
25394 ];
25395 $context['response_type'] = 'live';
25396 } else {
25397 $context['pluggable_data'] = [
25398 'tag_id' => 8,
25399 'name' => 'VIP Customers',
25400 ];
25401 $context['response_type'] = 'sample';
25402 }
25403 break;
25404
25405 case 'mailerpress_tag_added_to_contact_data':
25406 $recent_contact = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_contact ORDER BY created_at DESC LIMIT 1" );
25407 $recent_tag = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_tags LIMIT 1" );
25408
25409 if ( $recent_contact && $recent_tag ) {
25410 $user_data = get_userdata( 1 );
25411
25412 $context['pluggable_data'] = [
25413 'contact' => [
25414 'id' => $recent_contact->contact_id,
25415 'email' => $recent_contact->email,
25416 'first_name' => $recent_contact->first_name,
25417 'last_name' => $recent_contact->last_name,
25418 'subscription_status' => $recent_contact->subscription_status,
25419 'created_at' => $recent_contact->created_at,
25420 'updated_at' => $recent_contact->updated_at,
25421 ],
25422 'tag' => [
25423 'id' => $recent_tag->tag_id,
25424 'name' => $recent_tag->name,
25425 ],
25426 ];
25427 $context['response_type'] = 'live';
25428 } else {
25429 $context['pluggable_data'] = [
25430 'contact' => [
25431 'id' => 18,
25432 'email' => 'emma.wilson@startup.io',
25433 'first_name' => 'Emma',
25434 'last_name' => 'Wilson',
25435 'subscription_status' => 'subscribed',
25436 'created_at' => '2025-12-01 13:45:12',
25437 'updated_at' => '2025-12-01 16:18:35',
25438 ],
25439 'tag' => [
25440 'id' => 5,
25441 'name' => 'Premium Member',
25442 ],
25443 ];
25444 $context['response_type'] = 'sample';
25445 }
25446 break;
25447
25448 case 'campaign_created':
25449 $recent_campaign = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mailerpress_campaigns ORDER BY created_at DESC LIMIT 1" );
25450
25451 if ( $recent_campaign ) {
25452 $user_data = get_userdata( 1 );
25453
25454 $context['pluggable_data'] = [
25455 'wp_user_id' => $user_data ? $user_data->ID : 1,
25456 'user_login' => $user_data ? $user_data->user_login : 'root',
25457 'display_name' => $user_data ? $user_data->display_name : 'Arian',
25458 'user_firstname' => get_user_meta( $user_data ? $user_data->ID : 1, 'first_name', true ) ? get_user_meta( $user_data ? $user_data->ID : 1, 'first_name', true ) : '',
25459 'user_lastname' => get_user_meta( $user_data ? $user_data->ID : 1, 'last_name', true ) ? get_user_meta( $user_data ? $user_data->ID : 1, 'last_name', true ) : '',
25460 'user_email' => $user_data ? $user_data->user_email : 'dev-email@wpengine.local',
25461 'user_registered' => $user_data ? $user_data->user_registered : '2025-07-02 10:50:38',
25462 'user_role' => $user_data ? $user_data->roles : [ 'administrator' ],
25463 'campaign' => [
25464 'campaign_id' => $recent_campaign->campaign_id,
25465 'name' => $recent_campaign->name,
25466 'subject' => $recent_campaign->subject,
25467 'status' => $recent_campaign->status,
25468 'type' => $recent_campaign->type,
25469 'created_at' => $recent_campaign->created_at,
25470 'updated_at' => $recent_campaign->updated_at,
25471 ],
25472 ];
25473 $context['response_type'] = 'live';
25474 } else {
25475 $user_data = get_userdata( 1 );
25476
25477 $context['pluggable_data'] = [
25478 'wp_user_id' => $user_data ? $user_data->ID : 1,
25479 'user_login' => $user_data ? $user_data->user_login : 'root',
25480 'display_name' => $user_data ? $user_data->display_name : 'Arian',
25481 'user_firstname' => get_user_meta( $user_data ? $user_data->ID : 1, 'first_name', true ) ? get_user_meta( $user_data ? $user_data->ID : 1, 'first_name', true ) : '',
25482 'user_lastname' => get_user_meta( $user_data ? $user_data->ID : 1, 'last_name', true ) ? get_user_meta( $user_data ? $user_data->ID : 1, 'last_name', true ) : '',
25483 'user_email' => $user_data ? $user_data->user_email : 'dev-email@wpengine.local',
25484 'user_registered' => $user_data ? $user_data->user_registered : '2025-07-02 10:50:38',
25485 'user_role' => $user_data ? $user_data->roles : [ 'administrator' ],
25486 'campaign' => [
25487 'campaign_id' => 23,
25488 'name' => 'Holiday Special Offer 2025',
25489 'subject' => 'Exclusive 30% OFF - Limited Time Holiday Deal!',
25490 'status' => 'draft',
25491 'type' => 'newsletter',
25492 'created_at' => '2025-12-01 16:45:22',
25493 'updated_at' => '2025-12-01 17:12:38',
25494 ],
25495 ];
25496 $context['response_type'] = 'sample';
25497 }
25498 break;
25499
25500 }
25501 return $context;
25502 }
25503
25504
25505 }
25506
25507
25508 GlobalSearchController::get_instance();
25509