PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.0.73
OttoKit: All-in-One Automation Platform v1.0.73
1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Controllers / GlobalSearchController.php
suretriggers / src / Controllers Last commit date
AuthController.php 1 year ago AutomationController.php 3 years ago EventController.php 3 years ago GlobalSearchController.php 1 year ago IntegrationsController.php 2 years ago OptionController.php 3 years ago RestController.php 1 year ago RoutesController.php 3 years ago SettingsController.php 3 years ago WebhookRequestsController.php 1 year ago
GlobalSearchController.php
19756 lines
1 <?php
2 /**
3 * GlobalSearchController.
4 * php version 5.6
5 *
6 * @category GlobalSearchController
7 * @package SureTriggers
8 * @author BSF <username@example.com>
9 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
10 * @link https://www.brainstormforce.com/
11 * @since 1.0.0
12 */
13
14 namespace SureTriggers\Controllers;
15
16 use DOMDocument;
17 use FluentCrm\App\Models\CustomContactField;
18 use FluentCrm\App\Models\Subscriber;
19 use FluentCrm\App\Models\Tag;
20 use FluentCrm\App\Models\SubscriberMeta;
21 use FluentCrm\App\Models\Lists;
22 use memberpress\courses\lib as lib;
23 use memberpress\courses\models as models;
24 use FluentCrm\Framework\Support\Arr;
25 use GFCommon;
26 use GFFormsModel;
27 use Give_Payment;
28 use Give_Subscription;
29 use MeprBaseRealGateway;
30 use MeprOptions;
31 use OsAgentHelper;
32 use OsBookingHelper;
33 use OsCustomerHelper;
34 use OsServiceHelper;
35 use 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 Surelywp_Support_Portal;
87 use SureTriggers\Integrations\ProfileGrid\ProfileGrid;
88
89 /**
90 * GlobalSearchController- Add ajax related functions here.
91 *
92 * @category GlobalSearchController
93 * @package SureTriggers
94 * @author BSF <username@example.com>
95 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
96 * @link https://www.brainstormforce.com/
97 * @since 1.0.0
98 *
99 * @psalm-suppress UndefinedTrait
100 */
101 class GlobalSearchController {
102
103 use SingletonLoader;
104
105 /**
106 * Search post by post type.
107 *
108 * @param array $data Search Params.
109 *
110 * @since 1.0.0
111 *
112 * @return array
113 */
114 public function search_post( $data ) {
115 $result = [];
116 $posts = Utilities::find_posts_by_title( $data );
117
118 foreach ( $posts['results'] as $post ) {
119 $result[] = [
120 'label' => $post['post_title'],
121 'value' => $post['ID'],
122 ];
123 }
124
125 return [
126 'options' => $result,
127 'hasMore' => $posts['has_more'],
128 ];
129 }
130
131 /**
132 * Search Course.
133 *
134 * @param array $data quesry params.
135 *
136 * @return array
137 * @since 1.0.0
138 */
139 public function search_ld_course( $data ) {
140
141 $page = $data['page'];
142 $limit = Utilities::get_search_page_limit();
143 $offset = $limit * ( $page - 1 );
144
145 $courses = get_posts(
146 [
147
148 'post_type' => 'product',
149 'meta_key' => '_related_course',
150 'post_status' => 'publish',
151 'posts_per_page' => $limit,
152 'offset' => $offset,
153 ]
154 );
155 $options = [];
156 foreach ( $courses as $course ) {
157 $options[] = [
158 'label' => $course->post_title,
159 'value' => $course->ID,
160 ];
161 }
162
163 $all_courses = get_posts(
164 [
165 'post_type' => 'product',
166 'meta_key' => '_related_course',
167 'post_status' => 'publish',
168 'fields' => 'ids',
169 'posts_per_page' => -1,
170 ]
171 );
172 $posts_count = count( $all_courses );
173
174 return [
175 'options' => $options,
176 'hasMore' => $posts_count > $limit && $posts_count > $offset,
177 ];
178 }
179
180 /**
181 * Search achievement by post type.
182 *
183 * @param array $data Search Params.
184 *
185 * @since 1.0.0
186 *
187 * @return array
188 */
189 public function search_achievements( $data ) {
190
191 $page = $data['page'];
192 $limit = Utilities::get_search_page_limit();
193 $offset = $limit * ( $page - 1 );
194
195 $post = get_post( $data['dynamic'] );
196 $slug = $post->post_name;
197
198 $achievements = get_posts(
199 [
200 'post_type' => $slug,
201 'post_status' => 'publish',
202 'posts_per_page' => $limit,
203 'offset' => $offset,
204 ]
205 );
206 $options = [];
207 foreach ( $achievements as $achievement ) {
208 $options[] = [
209 'label' => $achievement->post_title,
210 'value' => (string) $achievement->ID,
211 ];
212 }
213
214 $count = wp_count_posts( $slug )->publish;
215
216 return [
217 'options' => $options,
218 'hasMore' => $count > $limit && $count > $offset,
219 ];
220 }
221
222 /**
223 * Search Course.
224 *
225 * @param array $data quesry params.
226 *
227 * @return array
228 * @since 1.0.0
229 */
230 public function search_tutor_course( $data ) {
231 $courses = get_posts(
232 [
233 'post_type' => tutor()->course_post_type,
234 'post_status' => 'publish',
235 'numberposts' => '-1',
236 ]
237 );
238 $options = [];
239 foreach ( $courses as $course ) {
240 $options[] = [
241 'label' => $course->post_title,
242 'value' => $course->ID,
243 ];
244 }
245
246 return [
247 'options' => $options,
248 'hasMore' => false,
249 ];
250 }
251
252 /**
253 * Search Products.
254 *
255 * @param array $data quesry params.
256 *
257 * @return array
258 * @since 1.0.0
259 */
260 public function search_product( $data ) {
261 $result = [];
262 $posts = Utilities::find_posts_by_title( $data );
263
264 foreach ( $posts['results'] as $post ) {
265 $result[] = [
266 'label' => $post['post_title'],
267 'value' => $post['post_title'],
268 ];
269 }
270
271 return [
272 'options' => $result,
273 'hasMore' => $posts['has_more'],
274 ];
275 }
276
277 /**
278 * Search Product Categories.
279 *
280 * @param array $data Search Params.
281 *
282 * @since 1.0.0
283 */
284 public function search_product_category( $data ) {
285 if ( ! empty( $data['dynamic'] ) ) {
286 $taxonomy = $data['dynamic'];
287 } else {
288 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
289 }
290
291 $term = $data['term'];
292 $result = [];
293 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
294 foreach ( $terms['result'] as $tax_term ) {
295 $result[] = [
296 'label' => $tax_term->name,
297 'value' => $tax_term->name,
298 ];
299 }
300
301 return [
302 'options' => $result,
303 'hasMore' => $terms['has_more'],
304 ];
305 }
306
307 /**
308 * Search Product Tags.
309 *
310 * @param array $data Search Params.
311 *
312 * @since 1.0.0
313 */
314 public function search_product_tags( $data ) {
315 if ( ! empty( $data['dynamic'] ) ) {
316 $taxonomy = $data['dynamic'];
317 } else {
318 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
319 }
320
321 $term = $data['term'];
322 $result = [];
323 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
324
325 foreach ( $terms['result'] as $tax_term ) {
326 $result[] = [
327 'label' => $tax_term->name,
328 'value' => $tax_term->name,
329 ];
330 }
331
332 return [
333 'options' => $result,
334 'hasMore' => $terms['has_more'],
335 ];
336 }
337
338 /**
339 * Global ajax search.
340 * Here you need to add the field action name to work.
341 *
342 * @param WP_REST_Request $request Request data.
343 *
344 * @return WP_REST_Response
345 * @since 1.0.0
346 */
347 public function global_search( $request ) {
348 $post_type = $request->get_param( 'post_type' );
349 $dynamic = $request->get_param( 'dynamic' );
350 $search_term = $request->get_param( 'term' );
351 $identifier = $request->get_param( 'field_identifier' );
352 $page = max( 1, $request->get_param( 'page' ) );
353 $taxonomy = $request->get_param( 'taxonomy' ) ? $request->get_param( 'taxonomy' ) : [];
354
355 $filter = $request->get_param( 'filter' ) ? json_decode( stripslashes( $request->get_param( 'filter' ) ), true ) : [];
356
357 $data = [
358 'dynamic' => $dynamic,
359 'search_term' => $search_term,
360 'page' => $page,
361 'taxonomy' => $taxonomy,
362 'filter' => $filter,
363 'post_type' => $post_type,
364 ];
365 $response = [
366 'hasMore' => false,
367 'options' => [],
368 ];
369
370 $method_name = 'search_' . $identifier;
371
372 if ( method_exists( $this, $method_name ) ) {
373 $response = $this->{$method_name}( $data );
374 } else {
375 return RestController::error_message( 'Invalid field Identifier param.' );
376 }
377
378 return RestController::success_message( $response );
379 }
380
381 /**
382 * Search Taxonomy Terms.
383 *
384 * @param array $data Search Params.
385 *
386 * @since 1.0.0
387 *
388 * @return array
389 */
390 public function search_term( $data ) {
391 if ( ! empty( $data['dynamic'] ) ) {
392 $taxonomy = $data['dynamic'];
393 } else {
394 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
395 }
396
397 $term = $data['term'];
398 $result = [];
399 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
400 foreach ( $terms['result'] as $tax_term ) {
401 $result[] = [
402 'label' => $tax_term->name,
403 'value' => $tax_term->term_id,
404 ];
405 }
406
407 return [
408 'options' => $result,
409 'hasMore' => $terms['has_more'],
410 ];
411 }
412
413 /**
414 * Search sureemails data.
415 *
416 * @param array $data data.
417 * @return array
418 */
419 public function search_pluggables_sureemails_mail( $data ) {
420 $context['pluggable_data'] = [
421 'to' => [ 'johnDoe@xyz.com' ],
422 'subject' => 'Test Email',
423 'message' => 'This is a test email',
424 'headers' => [ 'X-Mailer' => 'PHP/8.1.29' ],
425 'attachments' => [ 'Sample Attachment' ],
426 ];
427 $context['response_type'] = 'sample';
428 return $context;
429 }
430
431 /**
432 * List Taxonomy Terms.
433 *
434 * @param array $data Search Params.
435 *
436 * @since 1.0.0
437 *
438 * @return array
439 */
440 public function search_term_list( $data ) {
441 $taxonomy = $data['dynamic'];
442 $result = [];
443 $terms = Utilities::get_terms( '', $data['page'], $taxonomy );
444 foreach ( $terms['result'] as $tax_term ) {
445 $result[] = [
446 'label' => $tax_term->name,
447 'value' => $tax_term->term_id,
448 ];
449 }
450
451 return [
452 'options' => $result,
453 'hasMore' => $terms['has_more'],
454 ];
455 }
456
457 /**
458 * List Category Terms.
459 *
460 * @param array $data Search Params.
461 *
462 * @since 1.0.0
463 *
464 * @return array
465 */
466 public function search_category_term_list( $data ) {
467 $result = [];
468 $taxonomy = $data['dynamic'];
469 $terms = Utilities::get_terms( '', $data['page'], $taxonomy );
470 foreach ( $terms['result'] as $tax_term ) {
471 if ( 0 == $tax_term->parent ) {
472 $result[] = [
473 'label' => $tax_term->name,
474 'value' => $tax_term->term_id,
475 ];
476 foreach ( $terms['result'] as $child ) {
477 if ( $child->parent == $tax_term->term_id ) {
478 $result[] = [
479 'label' => ' - ' . $child->name,
480 'value' => $child->term_id,
481 ];
482 }
483 }
484 }
485 }
486
487 return [
488 'options' => $result,
489 'hasMore' => $terms['has_more'],
490 ];
491 }
492
493 /**
494 * List Taxonomy Tags.
495 *
496 * @param array $data Search Params.
497 *
498 * @since 1.0.0
499 *
500 * @return array
501 */
502 public function search_term_list_tags( $data ) {
503 $result = [];
504 $terms = Utilities::get_terms( '', $data['page'], [ 'post_tag' ] );
505 foreach ( $terms['result'] as $tax_term ) {
506 $result[] = [
507 'label' => $tax_term->name,
508 'value' => $tax_term->term_id,
509 ];
510 }
511
512 return [
513 'options' => $result,
514 'hasMore' => $terms['has_more'],
515 ];
516 }
517
518 /**
519 * List Role Capabilities.
520 *
521 * @param array $data Search Params.
522 *
523 * @since 1.0.0
524 *
525 * @return array
526 */
527 public function search_role_capabilities( $data ) {
528 $user_roles = wp_roles();
529 $result = [];
530
531 foreach ( $user_roles->roles as $name => $role ) {
532 foreach ( $role['capabilities'] as $key => $cap ) {
533 $result[] = [
534 'label' => $key,
535 'value' => esc_attr( $key ),
536 ];
537 }
538 }
539 $result = array_unique( $result, SORT_REGULAR );
540
541 return [
542 'options' => $result,
543 'hasMore' => false,
544 ];
545 }
546
547 /**
548 * Search users.
549 *
550 * @param array $data Search Params.
551 *
552 * @since 1.0.0
553 *
554 * @return array
555 */
556 public function search_user( $data ) {
557 $result = [];
558 $page = $data['page'];
559 $users = Utilities::get_users( $data, $page );
560
561 if ( is_array( $users['results'] ) ) {
562 foreach ( $users['results'] as $user ) {
563 $result[] = [
564 'label' => $user->user_login,
565 'value' => $user->ID,
566 ];
567 }
568 }
569
570 return [
571 'options' => $result,
572 'hasMore' => $users['has_more'],
573 ];
574
575 }
576
577 /**
578 * Search WPForm fields.
579 *
580 * @param array $data Search Params.
581 *
582 * @since 1.0.0
583 *
584 * @return array
585 */
586 public function search_pluggable_wpform_fields( $data ) {
587 $result = [];
588 $page = $data['page'];
589 $form_id = absint( $data['dynamic'] );
590 $wpform_fields = Utilities::get_wpform_fields( $data['search_term'], $page, $form_id );
591
592 if ( is_array( $wpform_fields['results'] ) ) {
593 foreach ( $wpform_fields['results'] as $field ) {
594 $result[] = [
595 'label' => $field['label'],
596 'value' => '{' . $field['id'] . '}',
597 ];
598 }
599 }
600
601 return [
602 'options' => $result,
603 'hasMore' => $wpform_fields['has_more'],
604 ];
605 }
606
607 /**
608 * Prepare variable products.
609 *
610 * @param array $data data.
611 *
612 * @return array
613 */
614 public function search_variable_products( $data ) {
615 $products = Utilities::get_variable_products( $data['search_term'], $data['page'] );
616 $options = [];
617
618 foreach ( $products['result'] as $product ) {
619 $options[] = [
620 'label' => $product->get_title(),
621 'value' => (string) $product->get_id(),
622 ];
623 }
624
625 return [
626 'options' => $options,
627 'hasMore' => $products['has_more'],
628 ];
629 }
630
631 /**
632 * Prepare variable products.
633 *
634 * @param array $data data.
635 *
636 * @return array
637 */
638 public function search_product_variations( $data ) {
639 $variations = Utilities::get_product_variations( $data['dynamic'] );
640 $options = [];
641
642 foreach ( $variations['result'] as $product ) {
643 $options[] = [
644 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
645 'value' => (string) $product->ID,
646 ];
647 }
648
649 return [
650 'options' => $options,
651 'hasMore' => $variations['has_more'],
652 ];
653 }
654
655 /**
656 * Search WooCommerce Subscriptions.
657 *
658 * @param array $data Search Params.
659 *
660 * @since 1.0.0
661 *
662 * @return array
663 */
664 public function search_subscription_variation( $data ) {
665 $subscription_products = Utilities::get_subscription_variation( $data['search_term'], $data['page'] );
666 $result = [];
667
668 if ( ! function_exists( 'wc_get_products' ) ) {
669 return $result;
670 }
671
672 foreach ( $subscription_products['result'] as $post ) {
673 if ( $data['search_term'] ) {
674 if ( false !== stripos( $post->get_title(), $data['search_term'] ) ) {
675 $result[] = [
676 'label' => $post->get_title(),
677 'value' => (string) $post->get_id(),
678 ];
679 }
680 } else {
681 $result[] = [
682 'label' => $post->get_title(),
683 'value' => (string) $post->get_id(),
684 ];
685 }
686 }
687
688 return [
689 'options' => $result,
690 'hasMore' => $subscription_products['has_more'],
691 ];
692 }
693
694 /**
695 * Prepare WooCommerce Payment Methods.
696 *
697 * @param array $data Search Params.
698 * @return array[]
699 */
700 public function search_woo_payment_methods( $data ) {
701 $payment_methods = WC()->payment_gateways->get_available_payment_gateways();
702 $options = [];
703
704 if ( ! empty( $payment_methods ) ) {
705 foreach ( $payment_methods as $payment ) {
706 $options[] = [
707 'label' => $payment->title,
708 'value' => $payment->id,
709 ];
710 }
711 }
712
713 return [
714 'options' => $options,
715 'hasMore' => false,
716 ];
717 }
718
719 /**
720 * Prepare WooCommerce Order Status List.
721 *
722 * @param array $data Search Params.
723 * @return array[]
724 */
725 public function search_woo_order_status_list( $data ) {
726 $order_status = wc_get_order_statuses();
727 $options = [];
728
729 if ( ! empty( $order_status ) ) {
730 foreach ( $order_status as $key => $status ) {
731 $options[] = [
732 'label' => $status,
733 'value' => $key,
734 ];
735 }
736 }
737
738 return [
739 'options' => $options,
740 'hasMore' => false,
741 ];
742 }
743
744 /**
745 * Prepare WooCommerce Country List.
746 *
747 * @param array $data Search Params.
748 * @return array[]
749 */
750 public function search_woo_country_list( $data ) {
751 $countries = WC()->countries->get_countries();
752 $options = [];
753
754 if ( ! empty( $countries ) ) {
755 foreach ( $countries as $key => $country ) {
756 $options[] = [
757 'label' => $country,
758 'value' => $key,
759 ];
760 }
761 }
762
763 return [
764 'options' => $options,
765 'hasMore' => false,
766 ];
767 }
768
769 /**
770 * Prepare WooCommerce Country States List.
771 *
772 * @param array $data Search Params.
773 * @return array[]
774 */
775 public function search_woo_country_state_list( $data ) {
776 if ( ! empty( $data['dynamic']['shipping_country'] ) ) {
777 $cc = $data['dynamic']['shipping_country'];
778 } else {
779 $cc = $data['dynamic'];
780 }
781
782 $states = WC()->countries->get_states( $cc );
783
784 $options = [];
785 if ( ! empty( $states ) ) {
786 foreach ( $states as $key => $state ) {
787 $options[] = [
788 'label' => $state,
789 'value' => $key,
790 ];
791 }
792 }
793
794 return [
795 'options' => $options,
796 'hasMore' => false,
797 ];
798 }
799
800 /**
801 * Get Memberpress gatways (payment methods) for subscription.
802 *
803 * @param array $data QueryParams.
804 *
805 * @return array
806 * @since 1.0.0
807 */
808 public function search_memberpress_gayways( $data ) {
809 $mp_options = MeprOptions::fetch();
810
811 $pms = array_keys( $mp_options->integrations );
812 $gateways = [];
813
814 foreach ( $pms as $pm_id ) {
815 $obj = $mp_options->payment_method( $pm_id );
816 if ( $obj instanceof MeprBaseRealGateway ) {
817 $gateways[] = [
818 'label' => sprintf( '%1$s (%2$s)', $obj->label, $obj->name ),
819 'value' => $obj->id,
820 ];
821 }
822 }
823
824 return [
825 'options' => $gateways,
826 'hasMore' => false,
827 ];
828 }
829
830 /**
831 * Prepare roles.
832 *
833 * @param array $data data.
834 *
835 * @return array
836 */
837 public function search_roles( $data ) {
838 $roles = wp_roles()->roles;
839 $options = [];
840 foreach ( $roles as $role => $details ) {
841
842 $options[] = [
843 'label' => $details['name'],
844 'value' => $role,
845 ];
846
847 }
848
849 return [
850 'options' => $options,
851 'hasMore' => false,
852 ];
853 }
854
855 /**
856 * Prepare post types.
857 *
858 * @param array $data data.
859 *
860 * @return array
861 */
862 public function search_post_types( $data ) {
863 $post_types = get_post_types( [ 'public' => true ], 'objects' );
864 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
865 if ( isset( $post_types['attachment'] ) ) {
866 unset( $post_types['attachment'] );
867 }
868
869 $options = [];
870 foreach ( $post_types as $post_type => $details ) {
871 $options[] = [
872 'label' => $details->label,
873 'value' => $post_type,
874 ];
875 }
876
877 return [
878 'options' => $options,
879 'hasMore' => false,
880 ];
881 }
882
883 /**
884 * Get post statuses.
885 *
886 * @param array $data data.
887 *
888 * @return array
889 */
890 public function search_post_statuses( $data ) {
891 $post_statuses = get_post_stati( [], 'objects' );
892 $post_statuses = apply_filters( 'suretriggers_post_types', $post_statuses );
893 $options = [];
894
895 foreach ( $post_statuses as $post_status => $details ) {
896 if ( 'woocommerce' === $details->label_count['domain'] ) {
897 $options[] = [
898 'label' => 'WooCommerce - ' . $details->label,
899 'value' => $post_status,
900 ];
901 } else {
902 $options[] = [
903 'label' => $details->label,
904 'value' => $post_status,
905 ];
906 }
907 }
908
909 return [
910 'options' => $options,
911 'hasMore' => false,
912 ];
913 }
914
915 /**
916 * Get Taxonomies.
917 *
918 * @param array $data data.
919 *
920 * @return array
921 */
922 public function search_taxonomy_list( $data ) {
923 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
924 $options = [];
925 $options[0] = [
926 'label' => 'Any Taxonomy',
927 'value' => -1,
928 ];
929
930 foreach ( $taxonomies as $taxonomy => $taxonomy_obj ) {
931 $options[] = [
932 'label' => $taxonomy_obj->label,
933 'value' => $taxonomy_obj->name,
934 ];
935 }
936
937 return [
938 'options' => $options,
939 'hasMore' => false,
940 ];
941 }
942
943 /**
944 * Get Taxonomies without any option.
945 *
946 * @param array $data data.
947 *
948 * @return array
949 */
950 public function search_taxonomy_list_without_any( $data ) {
951 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
952 $options = [];
953 foreach ( $taxonomies as $taxonomy => $taxonomy_obj ) {
954 $options[] = [
955 'label' => $taxonomy_obj->label,
956 'value' => $taxonomy_obj->name,
957 ];
958 }
959
960 return [
961 'options' => $options,
962 'hasMore' => false,
963 ];
964 }
965
966 /**
967 * Get WPForms.
968 *
969 * @param array $data data.
970 *
971 * @return array
972 */
973 public function search_wp_forms( $data ) {
974 if ( ! class_exists( 'WPForms_Form_Handler' ) ) {
975 return;
976 }
977
978 $wpforms = new WPForms_Form_Handler();
979 $forms = $wpforms->get( '', [ 'orderby' => 'title' ] );
980 $options = [];
981
982 if ( ! empty( $forms ) ) {
983 foreach ( $forms as $form ) {
984 $options[] = [
985 'label' => $form->post_title,
986 'value' => $form->ID,
987 ];
988 }
989 }
990
991 return [
992 'options' => $options,
993 'hasMore' => false,
994 ];
995 }
996
997 /**
998 * Get Gravity Forms.
999 *
1000 * @param array $data data.
1001 *
1002 * @return array
1003 */
1004 public function search_gravity_forms( $data ) {
1005 if ( ! class_exists( 'GFFormsModel' ) ) {
1006 return;
1007 }
1008
1009 $forms = GFFormsModel::get_forms();
1010 $options = [];
1011
1012 if ( ! empty( $forms ) ) {
1013 foreach ( $forms as $form ) {
1014 $options[] = [
1015 'label' => $form->title,
1016 'value' => $form->id,
1017 ];
1018 }
1019 }
1020
1021 return [
1022 'options' => $options,
1023 'hasMore' => false,
1024 ];
1025 }
1026
1027 /**
1028 * Get tag & contact details.
1029 *
1030 * @param array $data data.
1031 *
1032 * @return array
1033 */
1034 public function search_pluggables_fluentcrm_contact_added_to_tags( $data ) {
1035 $context = [];
1036 $pluggable_data = [];
1037 $tag_id = $data['filter'];
1038
1039 if ( ! class_exists( 'FluentCrm\App\Models\Subscriber' ) || ! class_exists( 'FluentCrm\App\Models\Tag' ) ) {
1040 return [];
1041 }
1042
1043 if ( $tag_id > 0 ) {
1044 $tags = Tag::where( 'id', $tag_id )->first();
1045 } else {
1046 $tags = Tag::orderBy( 'id', 'DESC' )->first();
1047 }
1048 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1049 if ( $contact ) {
1050 $pluggable_data['contact'] = $contact;
1051 $context['tag_id'] = $tag_id;
1052 $pluggable_data['tag'] = $tags;
1053 $context['response_type'] = 'live';
1054 } else {
1055 $pluggable_data['conatct']['full_name'] = 'Test User';
1056 $pluggable_data['conatct']['first_name'] = 'Test';
1057 $pluggable_data['conatct']['last_name'] = 'User';
1058 $pluggable_data['conatct']['company_id'] = 112;
1059 $pluggable_data['conatct']['email'] = 'testuser@gmail.com';
1060 $pluggable_data['conatct']['address_line_1'] = '33, Vincent Road';
1061 $pluggable_data['conatct']['address_line_2'] = 'Chicago Street';
1062 $pluggable_data['conatct']['postal_code'] = '212342';
1063 $pluggable_data['conatct']['city'] = 'New York City';
1064 $pluggable_data['conatct']['state'] = 'New York';
1065 $pluggable_data['conatct']['country'] = 'USA';
1066 $pluggable_data['conatct']['phone'] = '9992191911';
1067 $pluggable_data['conatct']['status'] = 'subscribed';
1068 $pluggable_data['conatct']['contact_type'] = 'lead';
1069 $pluggable_data['conatct']['source'] = '';
1070 $pluggable_data['conatct']['date_of_birth'] = '2022-11-09';
1071 $context['tag_id'] = 1;
1072 $pluggable_data['tag'] =
1073 [
1074 'id' => '1',
1075 'title' => 'new',
1076 'slug' => 'new',
1077 'description' => null,
1078 'created_at' => '2023-01-19 10:23:23',
1079 'updated_at' => '2023-01-19 10:23:23',
1080 'pivot' => [
1081 'subscriber_id' => '1',
1082 'object_id' => '1',
1083 'object_type' => 'FluentCrm\\App\\Models\\Tag',
1084 'created_at' => '2023-01-19 10:42:55',
1085 'updated_at' => '2023-01-19 10:42:55',
1086
1087 ],
1088 ];
1089 $context['response_type'] = 'sample';
1090 }
1091
1092 $context['pluggable_data'] = $pluggable_data;
1093 return $context;
1094 }
1095
1096 /**
1097 * Get FluentCRM contact details.
1098 *
1099 * @param array $data data.
1100 *
1101 * @return array
1102 */
1103 public function search_pluggables_fluentcrm_contact_added( $data ) {
1104 $context = [];
1105 $pluggable_data = [];
1106
1107 if ( ! class_exists( 'FluentCrm\App\Models\Subscriber' ) || ! class_exists( 'FluentCrm\App\Models\SubscriberMeta' ) ) {
1108 return [];
1109 }
1110 $contact = [];
1111 if ( 'status_set_to_specific_status' === $data['search_term'] ) {
1112 if ( '-1' === $data['filter']['status']['value'] ) {
1113 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1114 } else {
1115 $contact = Subscriber::where( 'status', $data['filter']['status']['value'] )->first();
1116 }
1117 } elseif ( 'new_contact_added' === $data['search_term'] ) {
1118 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1119 } elseif ( 'contact_updated' === $data['search_term'] ) {
1120 $contact = Subscriber::orderBy( 'updated_at', 'DESC' )->first();
1121 } elseif ( 'contact_field_updated' === $data['search_term'] ) {
1122 if ( '-1' === $data['filter']['field_id']['value'] ) {
1123 $contact = SubscriberMeta::where( 'object_type', 'custom_field' )->orderBy( 'updated_at', 'DESC' )->first();
1124 } else {
1125 $contact = SubscriberMeta::where( 'key', $data['filter']['field_id']['value'] )->orderBy( 'updated_at', 'DESC' )->first();
1126 }
1127 $contact = Subscriber::where( 'id', $contact->subscriber_id )->first();
1128 }
1129
1130 if ( $contact ) {
1131 $subscriber = Subscriber::with( [ 'tags', 'lists' ] )->find( $contact->id );
1132 $customer_fields = $subscriber->custom_fields();
1133 $pluggable_data['contact']['details'] = $subscriber;
1134 $pluggable_data['contact']['custom'] = $customer_fields;
1135 $pluggable_data['field_id'] = $data['filter']['field_id']['value'];
1136 $context['response_type'] = 'live';
1137 } else {
1138 $pluggable_data['contact']['details']['full_name'] = 'Test User';
1139 $pluggable_data['contact']['details']['first_name'] = 'Test';
1140 $pluggable_data['contact']['details']['last_name'] = 'User';
1141 $pluggable_data['contact']['details']['company_id'] = 112;
1142 $pluggable_data['contact']['details']['email'] = 'testuser@gmail.com';
1143 $pluggable_data['contact']['details']['address_line_1'] = '33, Vincent Road';
1144 $pluggable_data['contact']['details']['address_line_2'] = 'Chicago Street';
1145 $pluggable_data['contact']['details']['postal_code'] = '212342';
1146 $pluggable_data['contact']['details']['city'] = 'New York City';
1147 $pluggable_data['contact']['details']['state'] = 'New York';
1148 $pluggable_data['contact']['details']['country'] = 'USA';
1149 $pluggable_data['contact']['details']['phone'] = '9992191911';
1150 $pluggable_data['contact']['details']['status'] = 'subscribed';
1151 $pluggable_data['contact']['details']['contact_type'] = 'lead';
1152 $pluggable_data['contact']['details']['source'] = '';
1153 $pluggable_data['contact']['details']['date_of_birth'] = '2022-11-09';
1154 $context['response_type'] = 'sample';
1155 }
1156
1157 $context['pluggable_data'] = $pluggable_data;
1158 return $context;
1159 }
1160
1161 /**
1162 * Get contact added to list details.
1163 *
1164 * @param array $data data.
1165 *
1166 * @return array
1167 */
1168 public function search_pluggables_fluentcrm_contact_added_to_lists( $data ) {
1169 $context = [];
1170 $pluggable_data = [];
1171 $list_id = $data['filter'];
1172
1173 if ( ! class_exists( 'FluentCrm\App\Models\Lists' ) || ! class_exists( 'FluentCrm\App\Models\Subscriber' ) ) {
1174 return [];
1175 }
1176
1177 $contact_api = FluentCrmApi( 'contacts' );
1178 if ( -1 === $list_id ) {
1179 $lists = Lists::orderBy( 'id', 'DESC' )->first();
1180 $list_id = $lists->id;
1181 } else {
1182 $lists = Lists::where( 'id', $list_id )->first();
1183 }
1184 $list_ids = [ $list_id ];
1185 $list_contacts = $contact_api->getInstance()
1186 ->filterByLists( $list_ids )
1187 ->orderBy( 'id', 'DESC' )
1188 ->first();
1189
1190 $contact = Subscriber::where( 'id', $list_contacts->id )->get();
1191 if ( $contact ) {
1192 $pluggable_data['contact'] = $contact[0];
1193 $pluggable_data['list_id'] = $list_id;
1194 $pluggable_data['list'] = $lists;
1195 $context['response_type'] = 'live';
1196 } else {
1197 $pluggable_data['conatct']['id'] = 6;
1198 $pluggable_data['conatct']['prefix'] = 'Mr';
1199 $pluggable_data['conatct']['full_name'] = 'John Doe';
1200 $pluggable_data['conatct']['first_name'] = 'John';
1201 $pluggable_data['conatct']['last_name'] = 'Doe';
1202 $pluggable_data['conatct']['company_id'] = 112;
1203 $pluggable_data['conatct']['email'] = 'johnde@gmail.com';
1204 $pluggable_data['conatct']['address_line_1'] = '33, Vincent Road';
1205 $pluggable_data['conatct']['address_line_2'] = 'Chicago Street';
1206 $pluggable_data['conatct']['postal_code'] = '212342';
1207 $pluggable_data['conatct']['city'] = 'New York City';
1208 $pluggable_data['conatct']['state'] = 'New York';
1209 $pluggable_data['conatct']['country'] = 'USA';
1210 $pluggable_data['conatct']['phone'] = '9992191911';
1211 $pluggable_data['conatct']['status'] = 'subscribed';
1212 $pluggable_data['conatct']['contact_type'] = 'lead';
1213 $pluggable_data['conatct']['source'] = '';
1214 $pluggable_data['conatct']['date_of_birth'] = '2022-11-09';
1215 $context['list_id'] = 1;
1216 $pluggable_data['list'] =
1217 [
1218 'id' => '1',
1219 'title' => 'new',
1220 'slug' => 'new',
1221 'description' => null,
1222 'created_at' => '2023-01-19 10:23:23',
1223 'updated_at' => '2023-01-19 10:23:23',
1224 ];
1225 $context['response_type'] = 'sample';
1226 }
1227
1228 $context['pluggable_data'] = $pluggable_data;
1229 return $context;
1230 }
1231
1232 /**
1233 * Prepare fluentcrm campaigns.
1234 *
1235 * @param array $data Search Params.
1236 *
1237 * @return array
1238 */
1239 public function search_fluentcrm_campaigns( $data ) {
1240
1241 $options = [];
1242 global $wpdb;
1243
1244 $campaigns = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fc_campaigns ORDER BY id DESC", ARRAY_A );
1245
1246 if ( ! empty( $campaigns ) ) {
1247 foreach ( $campaigns as $campaign ) {
1248 $options[] = [
1249 'label' => $campaign['title'],
1250 'value' => $campaign['id'],
1251 ];
1252 }
1253 }
1254
1255 return [
1256 'options' => $options,
1257 'hasMore' => false,
1258 ];
1259 }
1260
1261 /**
1262 * Get Divi Forms.
1263 *
1264 * @param array $data data.
1265 *
1266 * @return array
1267 */
1268 public function search_divi_forms( $data ) {
1269 $form_posts = Utilities::get_divi_forms();
1270 $options = [];
1271
1272 if ( empty( $form_posts ) ) {
1273 return [
1274 'options' => [],
1275 'hasMore' => false,
1276 ];
1277 }
1278
1279 foreach ( (array) $form_posts as $form_post ) {
1280 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
1281 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
1282 if ( empty( $forms ) ) {
1283 continue;
1284 }
1285
1286 $count = 0;
1287
1288 foreach ( $forms as $form ) {
1289 $pattern_form = get_shortcode_regex( [ 'et_pb_contact_form' ] );
1290 preg_match_all( "/$pattern_form/", $form[0], $forms_extracted, PREG_SET_ORDER );
1291
1292 if ( empty( $forms_extracted ) ) {
1293 continue;
1294 }
1295
1296 foreach ( $forms_extracted as $form_extracted ) {
1297 $form_attrs = shortcode_parse_atts( $form_extracted[3] );
1298 $form_id = isset( $form_attrs['_unique_id'] ) ? $form_attrs['_unique_id'] : '';
1299 if ( empty( $form_id ) ) {
1300 continue;
1301 }
1302 $form_id = sprintf( '%d-%s', $form_post['ID'], $form_id );
1303 $form_title = isset( $form_attrs['title'] ) ? $form_attrs['title'] : '';
1304 $form_title = sprintf( '%s %s', $form_post['post_title'], $form_title );
1305 $options[] = [
1306 'label' => $form_title,
1307 'value' => $form_id,
1308 ];
1309 }
1310 $count++;
1311 }
1312 }
1313
1314 return [
1315 'options' => $options,
1316 'hasMore' => false,
1317 ];
1318 }
1319
1320 /**
1321 * Get Comment Pluggable data.
1322 *
1323 * @param array $data data.
1324 *
1325 * @return array
1326 */
1327 public function search_pluggables_wp_insert_comment( $data ) {
1328 $context = [];
1329 $post_data = [];
1330 $args = [
1331 'number' => '1',
1332 'status' => 'approve',
1333 'post_type' => $data['filter']['post_type']['value'],
1334 ];
1335
1336 if ( isset( $data['filter']['post']['value'] ) ) {
1337 $post_id = $data['filter']['post']['value'];
1338 if ( $post_id > 0 ) {
1339 $args['post_id'] = $post_id;
1340 }
1341 }
1342
1343 $comments = get_comments( $args );
1344 if ( empty( $comments ) ) {
1345 unset( $args['post_id'] );
1346 $comments = get_comments( $args );
1347 }
1348 $context['context_data'] = $data;
1349 $context['context_args'] = $args;
1350 if ( ! empty( $comments ) ) {
1351 foreach ( $comments as $comment ) :
1352 if ( is_object( $comment ) ) {
1353 $comment = get_object_vars( $comment );
1354 }
1355 if ( is_array( $comment ) && isset( $comment['comment_post_ID'] ) && is_int( $comment['comment_post_ID'] ) ) {
1356 $post = get_post( absint( $comment['comment_post_ID'] ) );
1357 if ( is_object( $post ) ) {
1358 if ( property_exists( $post, 'ID' ) || property_exists( $post, 'post_author' ) || property_exists( $post, 'post_title' ) ) {
1359 $post_id = $post->ID;
1360 $postauthor = (int) $post->post_author;
1361 if ( is_array( $comment ) ) {
1362 $context['pluggable_data'] = [
1363 'post' => $post_id,
1364 'post_title' => $post->post_title,
1365 'post_author' => get_the_author_meta( 'display_name', $postauthor ),
1366 'post_link' => get_the_permalink( $post_id ),
1367 'comment_id' => $comment['comment_ID'],
1368 'comment' => $comment['comment_content'],
1369 'comment_author' => $comment['comment_author'],
1370 'comment_author_email' => $comment['comment_author_email'],
1371 'comment_date' => $comment['comment_date'],
1372 ];
1373 }
1374 }
1375 }
1376 }
1377 if ( is_array( $comment ) && isset( $comment['comment_author_email'] ) ) {
1378 $user_email = $comment['comment_author_email'];
1379 /**
1380 *
1381 * Ignore line
1382 *
1383 * @phpstan-ignore-next-line
1384 */
1385 $user = get_user_by( 'email', $user_email );
1386 if ( $user ) {
1387 $context['pluggable_data']['wp_user_id'] = $user->ID;
1388 $context['pluggable_data']['user_login'] = $user->user_login;
1389 $context['pluggable_data']['display_name'] = $user->display_name;
1390 $context['pluggable_data']['user_firstname'] = $user->user_firstname;
1391 $context['pluggable_data']['user_lastname'] = $user->user_lastname;
1392 $context['pluggable_data']['user_email'] = $user->user_email;
1393 $context['pluggable_data']['user_role'] = $user->roles;
1394 }
1395 }
1396
1397 $context['response_type'] = 'live';
1398 endforeach;
1399 } else {
1400 $sample_comment = [
1401 'post' => 100,
1402 'post_title' => 'Sample Post',
1403 'comment_id' => 101,
1404 'comment' => 'Sample Comment',
1405 ];
1406 $sample_comment['wp_user_id'] = 7;
1407 $sample_comment['user_login'] = 'testuser@gmail.com';
1408 $sample_comment['display_name'] = 'Test User';
1409 $sample_comment['user_firstname'] = 'Test';
1410 $sample_comment['user_lastname'] = 'User';
1411 $sample_comment['user_email'] = 'testuser@gmail.com';
1412 $sample_comment['user_role'] = [ 'Subscriber' ];
1413
1414 $context['pluggable_data'] = $sample_comment;
1415 $context['response_type'] = 'sample';
1416 }
1417 return $context;
1418 }
1419
1420 /**
1421 * User reset password.
1422 *
1423 * @param array $data data.
1424 * @return array
1425 */
1426 public function search_pluggables_user_reset_password( $data ) {
1427 $user_context = $this->search_pluggables_add_user_role( $data );
1428 $user_context['pluggable_data']['new_password'] = '***password***';
1429 return $user_context;
1430 }
1431
1432 /**
1433 * User pluggable data.
1434 *
1435 * @param array $data data.
1436 * @return array
1437 */
1438 public function search_pluggables_add_user_role( $data ) {
1439 $context = [];
1440 $args = [
1441 'order' => 'DESC',
1442 'number' => 1,
1443 'orderby' => 'ID',
1444 ];
1445
1446 if ( isset( $data['filter']['role']['value'] ) ) {
1447 $role = $data['filter']['role']['value'];
1448 $args['role'] = $role;
1449 }
1450 if ( isset( $data['filter']['new_role']['value'] ) ) {
1451 $role = $data['filter']['new_role']['value'];
1452 $args['role'] = $role;
1453 }
1454
1455 $users = get_users( $args );
1456
1457 if ( isset( $data['filter']['meta_key']['value'] ) ) {
1458 $meta_key = $data['filter']['meta_key']['value'];
1459 $args['st_meta_key'] = $meta_key;
1460 }
1461
1462 if ( isset( $data['filter']['profile_field']['value'] ) ) {
1463 $meta_key = $data['filter']['profile_field']['value'];
1464 $args['profile_field'] = $meta_key;
1465 }
1466
1467 if ( ! empty( $users ) ) {
1468 $user = $users[0];
1469 $pluggable_data = [];
1470 $pluggable_data['wp_user_id'] = $user->ID;
1471 $pluggable_data['user_login'] = $user->user_login;
1472 $pluggable_data['display_name'] = $user->display_name;
1473 $pluggable_data['user_firstname'] = $user->user_firstname;
1474 $pluggable_data['user_lastname'] = $user->user_lastname;
1475 $pluggable_data['user_email'] = $user->user_email;
1476 $pluggable_data['user_registered'] = $user->user_registered;
1477 $pluggable_data['user_role'] = $user->roles;
1478 if ( isset( $args['st_meta_key'] ) ) {
1479 $pluggable_data['meta_key'] = $args['st_meta_key'];
1480 $pluggable_data['meta_value'] = get_user_meta( $user->ID, $args['st_meta_key'], true );
1481 }
1482 if ( isset( $args['profile_field'] ) ) {
1483 $userdata = get_userdata( $user->ID );
1484 $userdata = json_decode( wp_json_encode( $userdata->data ), true );
1485
1486 $pluggable_data['profile_field'] = $args['profile_field'];
1487 $pluggable_data['profile_field_value'] = $userdata[ $args['profile_field'] ];
1488 }
1489 $context['pluggable_data'] = $pluggable_data;
1490 $context['response_type'] = 'live';
1491 } else {
1492 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
1493 $context['pluggable_data'] = [
1494 'wp_user_id' => 1,
1495 'user_login' => 'admin',
1496 'display_name' => 'Test User',
1497 'user_firstname' => 'Test',
1498 'user_lastname' => 'User',
1499 'user_email' => 'testuser@gmail.com',
1500 'user_registered' => '2024-06-18 09:47:58',
1501 'user_role' => [ $role ],
1502 ];
1503 if ( isset( $args['st_meta_key'] ) ) {
1504 $context['pluggable_data']['meta_key'] = $args['st_meta_key'];
1505 $context['pluggable_data']['meta_value'] = 'test meta value';
1506 }
1507 if ( isset( $args['profile_field'] ) ) {
1508 $context['pluggable_data']['profile_field'] = $args['profile_field'];
1509 $context['pluggable_data']['profile_field_value'] = 'Profile Field Value';
1510 }
1511 $context['response_type'] = 'sample';
1512 }
1513 return $context;
1514 }
1515
1516 /**
1517 * User pluggable data.
1518 *
1519 * @param array $data data.
1520 * @return array
1521 */
1522 public function search_pluggables_last_user_login( $data ) {
1523 $context = [];
1524 $args = [
1525 'orderby' => 'meta_value',
1526 'meta_key' => 'st_last_login',
1527 'order' => 'DESC',
1528 'number' => 1,
1529 ];
1530 $users = get_users( $args );
1531
1532 if ( ! empty( $users ) ) {
1533 $user = $users[0];
1534 $pluggable_data = [];
1535 $pluggable_data['wp_user_id'] = $user->ID;
1536 $pluggable_data['user_login'] = $user->user_login;
1537 $pluggable_data['display_name'] = $user->display_name;
1538 $pluggable_data['user_firstname'] = $user->user_firstname;
1539 $pluggable_data['user_lastname'] = $user->user_lastname;
1540 $pluggable_data['user_email'] = $user->user_email;
1541 $pluggable_data['user_role'] = $user->roles;
1542
1543 $context['pluggable_data'] = $pluggable_data;
1544 $context['response_type'] = 'live';
1545 } else {
1546 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
1547 $context['pluggable_data'] = [
1548 'wp_user_id' => 1,
1549 'user_login' => 'admin',
1550 'display_name' => 'Test User',
1551 'user_firstname' => 'Test',
1552 'user_lastname' => 'User',
1553 'user_email' => 'testuser@gmail.com',
1554 'user_role' => [ $role ],
1555 ];
1556 $context['response_type'] = 'sample';
1557 }
1558 return $context;
1559 }
1560
1561 /**
1562 * Donation pluggable data.
1563 *
1564 * @param array $data data.
1565 * @return array
1566 */
1567 public function search_pluggables_wordpress_post( $data ) {
1568 $context = [];
1569 $args = [
1570 'post_type' => 'any',
1571 'posts_per_page' => 1,
1572 'orderby' => 'modified',
1573 'order' => 'DESC',
1574 ];
1575
1576 if ( isset( $data['filter']['post_type']['value'] ) ) {
1577 $post_type = $data['filter']['post_type']['value'];
1578 $args['post_type'] = $post_type;
1579 }
1580
1581 if ( isset( $data['filter']['status']['value'] ) ) {
1582 $post_status = $data['filter']['status']['value'];
1583 $args['post_status'] = $post_status;
1584 }
1585
1586 if ( isset( $data['filter']['post_status']['value'] ) ) {
1587 $post_status = $data['filter']['post_status']['value'];
1588 $args['post_status'] = $post_status;
1589 }
1590
1591 if ( isset( $data['filter']['post']['value'] ) ) {
1592 $post_id = $data['filter']['post']['value'];
1593 if ( $post_id > 0 ) {
1594 $args['p'] = $post_id;
1595 unset( $args['post_status'] );
1596 }
1597 }
1598
1599 $posts = get_posts( $args );
1600 if ( ! empty( $posts ) ) {
1601 $context['pluggable_data'] = $posts[0];
1602 $custom_metas = get_post_meta( $posts[0]->ID );
1603 if ( property_exists( $context['pluggable_data'], 'post' ) ) {
1604 $context['pluggable_data']->post = $posts[0]->ID;
1605 }
1606 if ( is_object( $context['pluggable_data'] ) ) {
1607 $context['pluggable_data'] = get_object_vars( $context['pluggable_data'] );
1608 }
1609 $context['pluggable_data']['featured_image'] = wp_get_attachment_image_src( (int) get_post_thumbnail_id( $posts[0]->ID ), 'full' )[0]; // @phpstan-ignore-line
1610 if ( $posts[0] instanceof WP_Post ) {
1611 $taxonomies = get_object_taxonomies( get_post( $posts[0] ), 'objects' );
1612 if ( ! empty( $taxonomies ) ) {
1613 foreach ( $taxonomies as $taxonomy => $taxonomy_object ) {
1614 $terms = get_the_terms( $posts[0]->ID, $taxonomy );
1615 if ( ! empty( $terms ) && is_array( $terms ) ) {
1616 foreach ( $terms as $term ) {
1617 $context['pluggable_data'][ $taxonomy ] = $term->name;
1618 }
1619 }
1620 }
1621 }
1622 }
1623 $context['pluggable_data'] = array_merge( $context['pluggable_data'], WordPress::get_user_context( $posts[0]->post_author ) );
1624 $context['pluggable_data']['post'] = $posts[0]->ID;
1625 $context['pluggable_data']['custom_metas'] = $custom_metas;
1626 $context['response_type'] = 'live';
1627 } else {
1628 $context['pluggable_data'] = [
1629 'ID' => 557,
1630 'post' => 557,
1631 'post_author' => 1,
1632 'post_date' => '2022-11-18 12:18:14',
1633 'post_date_gmt' => '2022-11-18 12:18:14',
1634 'post_content' => 'Test Post Content',
1635 'post_title' => 'Test Post',
1636 'post_excerpt' => '',
1637 'post_status' => $args['post_status'],
1638 'comment_status' => 'open',
1639 'ping_status' => 'open',
1640 'post_password' => '',
1641 'post_name' => 'test-post',
1642 'to_ping' => '',
1643 'pinged' => '',
1644 'post_modified' => '2022-11-18 12:18:14',
1645 'post_modified_gmt' => '2022-11-18 12:18:14',
1646 'post_content_filtered' => '',
1647 'post_parent' => 0,
1648 'guid' => 'https://abc.com/test-post/',
1649 'menu_order' => 0,
1650 'post_type' => 'post',
1651 'post_mime_type' => '',
1652 'comment_count' => 0,
1653 'filter' => 'raw',
1654 'featured_image' => 'https://abc.com/test-post/',
1655 ];
1656 $context['response_type'] = 'sample';
1657 }
1658
1659 return $context;
1660 }
1661
1662 /**
1663 * Donation pluggable data
1664 *
1665 * @param array $data data.
1666 * @return array
1667 */
1668 public function search_pluggables_givewp_donation_via_form( $data ) {
1669 global $wpdb;
1670 $context = [];
1671 $pluggable_data = [];
1672 $form_id = $data['filter']['form_id']['value'];
1673 $subscription_id = 1;
1674
1675 if ( ! class_exists( 'Give_Payment' ) || ! class_exists( 'Give_Subscription' ) ) {
1676 return [];
1677 }
1678
1679 if ( ! function_exists( 'give_get_donor_donation_comment' ) ) {
1680 return [];
1681 }
1682
1683 if ( 'donation_specific_field_value' == $data['search_term'] ) {
1684 $field_id = $data['filter']['field_id']['value'];
1685 $field_value = $data['filter']['field_value']['value'];
1686
1687 $donation_meta = $wpdb->get_row(
1688 $wpdb->prepare(
1689 "SELECT * FROM {$wpdb->prefix}give_donationmeta
1690 WHERE meta_key LIKE %s AND
1691 meta_value LIKE %s
1692 ORDER BY donation_id
1693 DESC LIMIT 1",
1694 $field_id,
1695 $field_value
1696 )
1697 );
1698 if ( $donation_meta ) {
1699 $donation_form_meta = $wpdb->get_row(
1700 $wpdb->prepare(
1701 "SELECT * FROM {$wpdb->prefix}give_donationmeta
1702 WHERE donation_id = %d AND
1703 meta_key LIKE %s AND
1704 meta_value LIKE %d
1705 ORDER BY donation_id
1706 DESC LIMIT 1",
1707 $donation_meta->donation_id,
1708 '_give_payment_form_id',
1709 $form_id
1710 )
1711 );
1712
1713 $payment = $wpdb->get_row(
1714 $wpdb->prepare(
1715 "SELECT * FROM {$wpdb->prefix}posts WHERE
1716 ID=%d ORDER BY ID DESC LIMIT 1",
1717 $donation_form_meta->donation_id
1718 )
1719 );
1720 }
1721 } elseif ( 'donation_specific_amount' == $data['search_term'] ) {
1722 $condition_compare = $data['filter']['condition_compare']['value'];
1723 $donation_amount = $data['filter']['donation_amount']['value'];
1724
1725 $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
1726
1727 if ( $donation_meta ) {
1728 $donation_form_meta = $wpdb->get_row(
1729 $wpdb->prepare(
1730 "SELECT * FROM {$wpdb->prefix}give_donationmeta
1731 WHERE donation_id = %d AND
1732 meta_key LIKE %s AND
1733 meta_value LIKE %d
1734 ORDER BY donation_id
1735 DESC LIMIT 1",
1736 $donation_meta->donation_id,
1737 '_give_payment_form_id',
1738 $form_id
1739 )
1740 );
1741
1742 $payment = $wpdb->get_row(
1743 $wpdb->prepare(
1744 "SELECT * FROM {$wpdb->prefix}posts WHERE
1745 ID=%d ORDER BY ID DESC LIMIT 1",
1746 $donation_form_meta->donation_id
1747 )
1748 );
1749 }
1750 } elseif ( 'cancels_recurring_donation' == $data['search_term'] ) {
1751 $donation_meta = $wpdb->get_row(
1752 $wpdb->prepare(
1753 "SELECT * FROM {$wpdb->prefix}give_subscriptions
1754 WHERE product_id=%d AND
1755 status LIKE %s
1756 ORDER BY id
1757 DESC LIMIT 1",
1758 $form_id,
1759 'cancelled'
1760 )
1761 );
1762 if ( $donation_meta ) {
1763 $subscription_id = $donation_meta->id;
1764 $payment = $wpdb->get_row(
1765 $wpdb->prepare(
1766 "SELECT * FROM {$wpdb->prefix}posts WHERE
1767 ID=%d ORDER BY ID DESC LIMIT 1",
1768 $donation_meta->parent_payment_id
1769 )
1770 );
1771 }
1772 } elseif ( 'continues_recurring_donation' == $data['search_term'] ) {
1773 $donation_meta = $wpdb->get_row(
1774 $wpdb->prepare(
1775 "SELECT * FROM {$wpdb->prefix}give_subscriptions
1776 WHERE product_id=%d AND
1777 status LIKE %s
1778 ORDER BY id
1779 DESC LIMIT 1",
1780 $form_id,
1781 'active'
1782 )
1783 );
1784 if ( $donation_meta ) {
1785 $subscription_id = $donation_meta->id;
1786
1787 $payment = $wpdb->get_row(
1788 $wpdb->prepare(
1789 "SELECT * FROM {$wpdb->prefix}posts
1790 WHERE post_parent=%d AND post_status LIKE %s
1791 ORDER BY ID DESC LIMIT 1",
1792 $donation_meta->parent_payment_id,
1793 'give_subscription'
1794 )
1795 );
1796 }
1797 } else {
1798 $payment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type=%s ORDER BY id DESC LIMIT 1", 'give_payment' ) );
1799 }
1800
1801 if ( ! empty( $payment ) ) {
1802 if ( 'continues_recurring_donation' == $data['search_term'] || 'cancels_recurring_donation' == $data['search_term'] ) {
1803 $subscription = new Give_Subscription( $subscription_id );
1804
1805 $pluggable_data['form_id'] = $form_id;
1806 $pluggable_data['subscription'] = $subscription;
1807 } elseif ( 'donation_specific_field_value' == $data['search_term'] ) {
1808 $payment_data = new Give_Payment( $payment->ID );
1809 $input_array = $payment_data->payment_meta;
1810 unset( $input_array['user_info'] );
1811 $pluggable_data = $input_array;
1812 foreach ( $input_array as $key => $value ) {
1813 $pluggable_data['field_id'] = $key;
1814 $pluggable_data['field_value'] = $value;
1815 }
1816 } else {
1817 $payment = new Give_Payment( $payment->ID );
1818 $address_data = $payment->address;
1819
1820 $pluggable_data['first_name'] = $payment->first_name;
1821 $pluggable_data['last_name'] = $payment->last_name;
1822 $pluggable_data['email'] = $payment->email;
1823 $pluggable_data['currency'] = $payment->currency;
1824 $pluggable_data['donated_amount'] = $payment->subtotal;
1825 $pluggable_data['donation_amount'] = $payment->subtotal;
1826 $pluggable_data['form_id'] = (int) $payment->form_id;
1827 $pluggable_data['form_title'] = $payment->form_title;
1828 $pluggable_data['name_title_prefix'] = $payment->title_prefix;
1829 $pluggable_data['date'] = $payment->date;
1830
1831 if ( is_array( $address_data ) ) {
1832 $pluggable_data['address_line_1'] = $address_data['line1'];
1833 $pluggable_data['address_line_2'] = $address_data['line2'];
1834 $pluggable_data['city'] = $address_data['city'];
1835 $pluggable_data['state'] = $address_data['state'];
1836 $pluggable_data['zip'] = $address_data['zip'];
1837 $pluggable_data['country'] = $address_data['country'];
1838 }
1839 // Payment meta.
1840 $payment_meta = $payment->get_meta();
1841 if ( is_array( $payment_meta ) && isset( $payment_meta['user_info'] ) ) {
1842 unset( $payment_meta['user_info'] );
1843 }
1844 $pluggable_data['payment_meta'] = $payment_meta;
1845 $donor_comment = give_get_donor_donation_comment( $payment->ID, $payment->donor_id );
1846 $pluggable_data['comment'] = ( is_array( $donor_comment ) && isset( $donor_comment['comment_content'] ) ) ? $donor_comment : '';
1847 }
1848
1849 $context['response_type'] = 'live';
1850 } else {
1851 if ( 'continues_recurring_donation' == $data['search_term'] || 'cancels_recurring_donation' == $data['search_term'] ) {
1852 $pluggable_data['form_id'] = $form_id;
1853 $pluggable_data['subscription']['id'] = 3;
1854 $pluggable_data['subscription']['donor_id'] = 8;
1855 $pluggable_data['subscription']['period'] = 'month';
1856 $pluggable_data['subscription']['initial_amount'] = '25.0000000000';
1857 $pluggable_data['subscription']['recurring_amount'] = '25.0000000000';
1858 $pluggable_data['subscription']['recurring_fee_amount'] = '0.0000000000';
1859 $pluggable_data['subscription']['transaction_id'] = 'a228ec9c6357963d23079d7d6945dd61';
1860 $pluggable_data['subscription']['parent_payment_id'] = '7492';
1861 $pluggable_data['subscription']['created'] = '2024-01-23 11:12:11';
1862 $pluggable_data['subscription']['expiration'] = '2024-03-23 23:59:59';
1863 $pluggable_data['subscription']['status'] = 'cancelled';
1864 $pluggable_data['subscription']['donor']['id'] = 8;
1865 $pluggable_data['subscription']['donor']['purchase_count'] = '3';
1866 $pluggable_data['subscription']['donor']['purchase_value'] = '75.000000';
1867 $pluggable_data['subscription']['donor']['email'] = 'johndoee@yopmail.com';
1868 $pluggable_data['subscription']['donor']['name'] = 'John Doe';
1869 $pluggable_data['subscription']['donor']['payment_ids'] = '7487,7492,7499';
1870 $pluggable_data['subscription']['donor']['user_id'] = '131';
1871 $pluggable_data['subscription']['customer_id'] = '8';
1872 } elseif ( 'donation_specific_field_value' == $data['search_term'] ) {
1873 $pluggable_data['form_id'] = 23;
1874 $pluggable_data['form_title'] = 'Demo Donation';
1875 $pluggable_data['_give_donor_billing_first_name'] = 'John';
1876 $pluggable_data['_give_donor_billing_last_name'] = 'Doe';
1877 $pluggable_data['_give_payment_donor_email'] = 'johndoee@gmail.com';
1878 $pluggable_data['_give_payment_currency'] = 'USD';
1879 $pluggable_data['_give_payment_total'] = '100';
1880 $pluggable_data['name_title_prefix'] = 'Mr';
1881 $pluggable_data['date'] = '2022-11-07 16:06:05';
1882 $pluggable_data['_give_donor_billing_address1'] = '33, Vincent Road';
1883 $pluggable_data['_give_donor_billing_address2'] = 'Chicago Street';
1884 $pluggable_data['_give_donor_billing_city'] = 'New York City';
1885 $pluggable_data['_give_donor_billing_state'] = 'New York';
1886 $pluggable_data['_give_donor_billing_zip'] = '223131';
1887 $pluggable_data['_give_donor_billing_country'] = 'USA';
1888 $pluggable_data['_give_donation_comment'] = 'Demo Comment';
1889 $pluggable_data['field_id'] = 'last_name';
1890 $pluggable_data['field_value'] = 'Doe';
1891 } else {
1892 $pluggable_data['first_name'] = 'John';
1893 $pluggable_data['last_name'] = 'Doe';
1894 $pluggable_data['email'] = 'johndoee@gmail.com';
1895 $pluggable_data['currency'] = 'USD';
1896 $pluggable_data['donated_amount'] = 100;
1897 $pluggable_data['donation_amount'] = 100;
1898 $pluggable_data['form_id'] = 23;
1899 $pluggable_data['form_title'] = 'Demo Donation';
1900 $pluggable_data['name_title_prefix'] = 'Mr';
1901 $pluggable_data['date'] = '2022-11-07 16:06:05';
1902 $pluggable_data['address_line_1'] = '33, Vincent Road';
1903 $pluggable_data['address_line_2'] = 'Chicago Street';
1904 $pluggable_data['city'] = 'New York City';
1905 $pluggable_data['state'] = 'New York';
1906 $pluggable_data['zip'] = '223131';
1907 $pluggable_data['country'] = 'USA';
1908 $pluggable_data['comment'] = 'Demo Comment';
1909 }
1910 $context['response_type'] = 'sample';
1911 }
1912
1913 $context['pluggable_data'] = $pluggable_data;
1914 return $context;
1915 }
1916
1917 /**
1918 * Search Divi Form fields.
1919 *
1920 * @param array $data Search Params.
1921 *
1922 * @since 1.0.0
1923 *
1924 * @return array
1925 */
1926 public function search_pluggable_divi_form_fields( $data ) {
1927 $result = [];
1928 $form_id = absint( $data['dynamic'] );
1929 $form_posts = Utilities::get_divi_forms();
1930
1931 if ( empty( $form_posts ) ) {
1932 return [
1933 'options' => [],
1934 'hasMore' => false,
1935 ];
1936 }
1937 $fields = [];
1938 foreach ( (array) $form_posts as $form_post ) {
1939 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
1940 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
1941 if ( empty( $forms ) ) {
1942 continue;
1943 }
1944
1945 $count = 0;
1946
1947 foreach ( $forms as $form ) {
1948 $pattern = get_shortcode_regex( [ 'et_pb_contact_field' ] );
1949
1950 preg_match_all( "/$pattern/", $form[0], $contact_fields, PREG_SET_ORDER );
1951
1952 if ( empty( $contact_fields ) ) {
1953 return $fields;
1954 }
1955
1956 foreach ( $contact_fields as $contact_field ) {
1957 $contact_field_attrs = shortcode_parse_atts( $contact_field[3] );
1958 $field_id = strtolower( self::array_get( $contact_field_attrs, 'field_id' ) );
1959 $fields[] = [
1960 'field_title' => self::array_get( $contact_field_attrs, 'field_title', __( 'No title', 'suretriggers' ) ),
1961 'field_id' => $field_id,
1962 ];
1963 }
1964 }
1965 }
1966
1967 if ( ! empty( $fields ) ) {
1968 foreach ( $fields as $field ) {
1969 $result[] = [
1970 'label' => $field['field_title'],
1971 'value' => '{' . $field['field_id'] . '}',
1972 ];
1973 }
1974 }
1975
1976 return [
1977 'options' => $result,
1978 'hasMore' => false,
1979 ];
1980 }
1981
1982 /**
1983 * Pseudo function copied from Divi
1984 *
1985 * @param array $array An array which contains value located at `$address`.
1986 * @param string|array $address The location of the value within `$array` (dot notation).
1987 * @param mixed $default Value to return if not found. Default is an empty string.
1988 *
1989 * @return mixed The value, if found, otherwise $default.
1990 */
1991 public static function array_get( $array, $address, $default = '' ) {
1992 $keys = is_array( $address ) ? $address : explode( '.', $address );
1993 $value = $array;
1994
1995 foreach ( $keys as $key ) {
1996 if ( ! empty( $key ) && isset( $key[0] ) && '[' === $key[0] ) {
1997 $index = substr( $key, 1, -1 );
1998
1999 if ( is_numeric( $index ) ) {
2000 $key = (int) $index;
2001 }
2002 }
2003
2004 if ( ! isset( $value[ $key ] ) ) {
2005 return $default;
2006 }
2007
2008 $value = $value[ $key ];
2009 }
2010
2011 return $value;
2012 }
2013
2014 /**
2015 * Get UAG Forms.
2016 *
2017 * @param array $data data.
2018 *
2019 * @return array
2020 */
2021 public function search_spectra_forms( $data ) {
2022 $form_posts = Utilities::get_uag_forms();
2023
2024 $options = [];
2025 if ( empty( $form_posts ) ) {
2026 return [
2027 'options' => [],
2028 'hasMore' => false,
2029 ];
2030 }
2031
2032 foreach ( (array) $form_posts as $form_post ) {
2033 $blocks = parse_blocks( $form_post['post_content'] );
2034 $i = 1;
2035 // Get form blocks.
2036 $this->process_blocks( $blocks, $form_post, $options, $i );
2037 }
2038
2039 return [
2040 'options' => $options,
2041 'hasMore' => false,
2042 ];
2043 }
2044
2045 /**
2046 * Return forms in nested blocks in Spectra.
2047 *
2048 * @param array $blocks data.
2049 * @param array $form_post Form post.
2050 * @param array $options Options.
2051 * @param int $i Number.
2052 *
2053 * @return array
2054 */
2055 public function process_blocks( $blocks, $form_post, &$options, &$i ) {
2056 foreach ( $blocks as $block ) {
2057 if ( 'uagb/forms' === $block['blockName'] ) {
2058 $options[] = [
2059 'label' => $form_post['post_title'] . ' (Form ' . ( $i++ ) . ')',
2060 'value' => $block['attrs']['block_id'],
2061 ];
2062 } elseif ( isset( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) {
2063 $this->process_blocks( $block['innerBlocks'], $form_post, $options, $i );
2064 }
2065 }
2066 return $options;
2067 }
2068
2069 /**
2070 * Check array recursive.
2071 *
2072 * @param array $array Array.
2073 * @param string $value search params.
2074 * @since 1.0.0
2075 *
2076 * @return array|void
2077 */
2078 public static function get_column_by_value( $array, $value ) {
2079
2080 foreach ( $array as $key => $sub_array ) {
2081
2082 if ( is_array( $sub_array ) ) {
2083 $result = self::get_column_by_value( $sub_array, $value );
2084 if ( null !== $result ) {
2085 return $key;
2086 }
2087 } else {
2088 return $key;
2089 }
2090 }
2091 return null;
2092
2093 }
2094
2095
2096 /**
2097 * Search UAG Form fields.
2098 *
2099 * @param array $data Search Params.
2100 *
2101 * @since 1.0.0
2102 *
2103 * @return array
2104 */
2105 public function search_spectraform_fields( $data ) {
2106 $result = [];
2107 $form_id = absint( $data['dynamic'] );
2108 $form_posts = Utilities::get_uag_forms();
2109
2110 if ( empty( $form_posts ) ) {
2111 return [
2112 'options' => [],
2113 'hasMore' => false,
2114 ];
2115 }
2116
2117 foreach ( (array) $form_posts as $form_post ) {
2118 $blocks = parse_blocks( $form_post['post_content'] );
2119
2120 foreach ( $blocks as $block ) {
2121 if ( (int) $block['attrs']['block_id'] === $form_id ) {
2122 $doc = new DOMDocument();
2123 $rendered_block = render_block( $block );
2124 $doc->loadHTML( $rendered_block );
2125 $child_node_list = $doc->getElementsByTagName( 'div' );
2126 for ( $i = 0; $i < $child_node_list->length; $i++ ) {
2127 $temp = $child_node_list->item( $i );
2128 if ( $temp && stripos( $temp->getAttribute( 'class' ), 'uagb-forms-input-label' ) !== false ) {
2129 $nodes[] = $temp;
2130 }
2131 }
2132
2133 foreach ( $nodes as $node ) {
2134 $result[] = [
2135 'label' => $node->textContent, //phpcs:ignore
2136 'value' => $node->textContent, //phpcs:ignore
2137 ];
2138 }
2139 }
2140 }
2141 }
2142
2143 return [
2144 'options' => $result,
2145 'hasMore' => false,
2146 ];
2147 }
2148
2149 /**
2150 * Search forms of MetForms.
2151 *
2152 * @param array $data data.
2153 * @return array
2154 */
2155 public function search_met_forms( $data ) {
2156 $args = [
2157 'post_type' => 'metform-form',
2158 'post_status' => 'publish',
2159 'numberposts' => -1,
2160 ];
2161
2162 $forms = get_posts( $args );
2163
2164 $options = [];
2165
2166 if ( ! empty( $forms ) ) {
2167 foreach ( $forms as $form ) {
2168 $options[] = [
2169 'label' => $form->post_title,
2170 'value' => $form->ID,
2171 ];
2172 }
2173 }
2174
2175 return [
2176 'options' => $options,
2177 'hasMore' => false,
2178 ];
2179 }
2180
2181 /**
2182 * Search forms of Ninja Forms.
2183 *
2184 * @param array $data data.
2185 * @return array
2186 */
2187 public function search_ninja_forms( $data ) {
2188 $options = [];
2189
2190 if ( function_exists( 'Ninja_Forms' ) ) {
2191 foreach ( Ninja_Forms()->form()->get_forms() as $form ) {
2192 $options[] = [
2193 'label' => $form->get_setting( 'title' ),
2194 'value' => $form->get_id(),
2195 ];
2196 }
2197 }
2198
2199 return [
2200 'options' => $options,
2201 'hasMore' => false,
2202 ];
2203 }
2204
2205 /**
2206 * Search forms of Pie Forms.
2207 *
2208 * @param array $data data.
2209 * @return array
2210 */
2211 public function search_pie_forms( $data ) {
2212 global $wpdb;
2213 $options = [];
2214
2215 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pf_forms' ) ) ) {
2216
2217 $results = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'pf_forms WHERE post_status = "published"' );
2218
2219 if ( $results ) {
2220 foreach ( $results as $result ) {
2221 $options[] = [
2222 'label' => $result->form_title,
2223 'value' => $result->id,
2224 ];
2225 }
2226 }
2227 }
2228
2229 return [
2230 'options' => $options,
2231 'hasMore' => false,
2232 ];
2233 }
2234
2235 /**
2236 * Get Fluent Forms.
2237 *
2238 * @param array $data data.
2239 *
2240 * @return array
2241 */
2242 public function search_fluent_forms( $data ) {
2243
2244 if ( ! function_exists( 'wpFluent' ) ) {
2245 return [
2246 'options' => [],
2247 'hasMore' => false,
2248 ];
2249 }
2250
2251 $forms = wpFluent()->table( 'fluentform_forms' )
2252 ->select( [ 'id', 'title' ] )
2253 ->orderBy( 'id', 'DESC' )
2254 ->get();
2255
2256 $options = [];
2257 if ( ! empty( $forms ) ) {
2258 foreach ( $forms as $form ) {
2259 $options[] = [
2260 'label' => $form->title,
2261 'value' => $form->id,
2262 ];
2263 }
2264 }
2265
2266 return [
2267 'options' => $options,
2268 'hasMore' => false,
2269 ];
2270
2271 }
2272
2273 /**
2274 * Get Fluent Forms Fields.
2275 *
2276 * @param array $data data.
2277 *
2278 * @return array
2279 */
2280 public function search_fluent_forms_form_fields( $data ) {
2281
2282 $options = [];
2283
2284 if ( ! function_exists( 'wpFluent' ) ) {
2285 return [];
2286 }
2287
2288 $form = wpFluent()->table( 'fluentform_forms' )->find( $data['dynamic'] );
2289 $field_data = json_decode( $form->form_fields, true );
2290 if ( is_array( $field_data ) && ! empty( $field_data['fields'] ) ) {
2291 foreach ( $field_data['fields'] as $field ) {
2292 if ( isset( $field['fields'] ) ) {
2293 foreach ( $field['fields'] as $field_key => $sub_field ) {
2294 if (
2295 isset( $sub_field['settings'] )
2296 && isset( $sub_field['settings']['label'] )
2297 && isset( $sub_field['settings']['visible'] )
2298 && true === $sub_field['settings']['visible']
2299 ) {
2300 $options[] = [
2301 'label' => $sub_field['settings']['label'],
2302 'value' => $field_key,
2303 ];
2304 }
2305 }
2306 } elseif ( isset( $field['element'] ) &&
2307 'container' === (string) $field['element'] &&
2308 isset( $field['columns'] ) && is_array( $field['columns'] ) ) {
2309 $container_fields = $field['columns'];
2310 foreach ( $container_fields as $c_fields ) {
2311 foreach ( $c_fields['fields'] as $field_key => $sub_field ) {
2312 if ( isset( $sub_field['settings'] ) ) {
2313 $options[] = [
2314 'label' => ( '' !== $sub_field['settings']['label'] ) ?
2315 $sub_field['settings']['label'] :
2316 $sub_field['attributes']['name'],
2317 'value' => isset( $sub_field['attributes']['name'] ) ?
2318 $sub_field['attributes']['name'] :
2319 strtolower( $sub_field['settings']['label'] ),
2320 ];
2321 }
2322 }
2323 }
2324 } elseif ( isset( $field['attributes'] ) && isset( $field['attributes']['name'] ) ) {
2325 if ( isset( $field['attributes']['placeholder'] ) && ! empty( $field['attributes']['placeholder'] ) ) {
2326 $options[] = [
2327 'label' => $field['attributes']['placeholder'],
2328 'value' => $field['attributes']['name'],
2329 ];
2330 } elseif ( isset( $field['settings'] ) &&
2331 isset( $field['settings']['label'] ) &&
2332 ! empty( $field['settings']['label'] ) ) {
2333 $options[] = [
2334 'label' => $field['settings']['label'],
2335 'value' => $field['attributes']['name'],
2336 ];
2337 }
2338 }
2339 }
2340 }
2341
2342 return [
2343 'options' => $options,
2344 'hasMore' => false,
2345 ];
2346
2347 }
2348
2349 /**
2350 * Get Fluent Forms.
2351 *
2352 * @param array $data data.
2353 *
2354 * @return array
2355 */
2356 public function search_bricks_builder_forms( $data ) {
2357 $bricks_theme = wp_get_theme( 'bricks' );
2358 if ( ! $bricks_theme->exists() ) {
2359 return [
2360 'options' => [],
2361 'hasMore' => false,
2362 ];
2363 }
2364
2365 $bricks_settings = (array) get_option( BRICKS_DB_GLOBAL_SETTINGS );
2366 if ( array_key_exists( 'postTypes', $bricks_settings ) ) {
2367 $bricks_posts = $bricks_settings['postTypes'];
2368 } else {
2369 $bricks_posts = [];
2370 }
2371 $bricks_posts[] = 'bricks_template';
2372
2373 $args = [
2374 'post_type' => $bricks_posts,
2375 'post_status' => 'publish',
2376 'posts_per_page' => -1,
2377 ];
2378
2379 $templates = get_posts( $args );
2380
2381 $options = [];
2382 if ( ! empty( $templates ) ) {
2383 foreach ( $templates as $template ) {
2384 $fetch_content = get_post_meta( $template->ID, BRICKS_DB_PAGE_CONTENT, true );
2385 if ( is_array( $fetch_content ) ) {
2386 foreach ( $fetch_content as $content ) {
2387 if ( 'form' === $content['name'] ) {
2388 $options[] = [
2389 'label' => $template->post_title . ' - ' . ( isset( $content['label'] ) ? $content['label'] : 'Form' ),
2390 'value' => $content['id'],
2391 ];
2392 }
2393 }
2394 }
2395 }
2396 }
2397
2398 return [
2399 'options' => $options,
2400 'hasMore' => false,
2401 ];
2402
2403 }
2404
2405 /**
2406 * Bricks builder form fields.
2407 *
2408 * @param array $data data.
2409 * @return array
2410 */
2411 public function search_pluggable_bricks_builder_form_fields( $data ) {
2412 $result = [];
2413 $fields = [];
2414 $form_id_str = $data['dynamic'];
2415 $ids = explode( '_', $form_id_str );
2416 $post_id = $ids[0];
2417 $form_id = $ids[1];
2418 $fetch_content = get_post_meta( $post_id, BRICKS_DB_PAGE_CONTENT, true );
2419 if ( is_array( $fetch_content ) ) {
2420 foreach ( $fetch_content as $content ) {
2421 if ( 'form' === $content['name'] && $form_id === $content['id'] ) {
2422 $fields = $content['settings']['fields'];
2423 break;
2424 }
2425 }
2426 }
2427
2428 if ( ! empty( $fields ) ) {
2429 foreach ( $fields as $field ) {
2430 $result[] = [
2431 'label' => $field['label'],
2432 'value' => '{' . strtolower( $field['label'] ) . '}',
2433 ];
2434 }
2435 }
2436
2437 return [
2438 'options' => $result,
2439 'hasMore' => false,
2440 ];
2441 }
2442
2443 /**
2444 * Get fluent form fields
2445 *
2446 * @param array $data Data array.
2447 *
2448 * @return array
2449 */
2450 public function search_pluggable_fluent_form_fields( $data ) {
2451 $result = [];
2452 $form_id = absint( $data['dynamic'] );
2453
2454 $fluentform_fields = Utilities::get_fluentform_fields( $data['search_term'], -1, $form_id );
2455
2456 if ( is_array( $fluentform_fields['results'] ) ) {
2457 foreach ( $fluentform_fields['results'] as $field ) {
2458 $result[] = [
2459 'label' => $field['text'],
2460 'value' => "{{$field['value']}}",
2461 ];
2462 }
2463 }
2464
2465 $result[] = [
2466 'value' => '{form_id}',
2467 'label' => 'Form ID',
2468 ];
2469
2470 $result[] = [
2471 'value' => '{form_title}',
2472 'label' => 'Form Title',
2473 ];
2474 $result[] = [
2475 'value' => '{entry_id}',
2476 'label' => 'Entry ID',
2477 ];
2478
2479 $result[] = [
2480 'value' => '{entry_source_url}',
2481 'label' => 'Entry Source URL',
2482 ];
2483
2484 $result[] = [
2485 'value' => '{submission_date}',
2486 'label' => 'Submission Date',
2487 ];
2488
2489 $result[] = [
2490 'value' => '{user_ip}',
2491 'label' => 'User IP',
2492 ];
2493
2494 return [
2495 'options' => $result,
2496 'hasMore' => false,
2497 ];
2498 }
2499
2500 /**
2501 * Search Gravity Form fields.
2502 *
2503 * @param array $data Search Params.
2504 *
2505 * @since 1.0.0
2506 */
2507 public function search_gform_fields( $data ) {
2508 if ( ! class_exists( 'RGFormsModel' ) ) {
2509 return [
2510 'options' => [],
2511 'hasMore' => false,
2512 ];
2513 }
2514 $result = [];
2515 $page = $data['page'];
2516 $form_id = absint( $data['dynamic'] );
2517
2518 $form = RGFormsModel::get_form_meta( $form_id );
2519
2520 if ( is_array( $form['fields'] ) ) {
2521 foreach ( $form['fields'] as $field ) {
2522 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
2523 foreach ( $field['inputs'] as $input ) {
2524 if ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
2525 $result[] = [
2526 'value' => $input['id'],
2527 'label' => GFCommon::get_label( $field, $input['id'] ),
2528 ];
2529 }
2530 }
2531 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
2532 $result[] = [
2533 'value' => (string) $field['id'],
2534 'label' => GFCommon::get_label( $field ),
2535 ];
2536 }
2537 }
2538 }
2539
2540 return [
2541 'options' => $result,
2542 'hasMore' => false,
2543 ];
2544
2545 }
2546
2547 /**
2548 * Search Gravity Form fields.
2549 *
2550 * @param array $data Search Params.
2551 *
2552 * @since 1.0.0
2553 */
2554 public function search_pluggable_gravity_form_fields( $data ) {
2555 if ( ! class_exists( 'RGFormsModel' ) ) {
2556 return [
2557 'options' => [],
2558 'hasMore' => false,
2559 ];
2560 }
2561 $result = [];
2562 $form_id = absint( $data['dynamic'] );
2563
2564 $form = RGFormsModel::get_form_meta( $form_id );
2565
2566 if ( is_array( $form['fields'] ) ) {
2567 foreach ( $form['fields'] as $field ) {
2568 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
2569 foreach ( $field['inputs'] as $input ) {
2570 if ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
2571 $result[] = [
2572 'value' => '{' . $input['id'] . '}',
2573 'label' => GFCommon::get_label( $field, $input['id'] ),
2574 ];
2575 }
2576 }
2577 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
2578 $result[] = [
2579 'value' => '{' . $field['id'] . '}',
2580 'label' => GFCommon::get_label( $field ),
2581 ];
2582 }
2583 }
2584 }
2585
2586 $result[] = [
2587 'value' => '{gravity_form}',
2588 'label' => 'Form ID',
2589 ];
2590 $result[] = [
2591 'value' => '{form_title}',
2592 'label' => 'Form Title',
2593 ];
2594 $result[] = [
2595 'value' => '{entry_id}',
2596 'label' => 'Entry ID',
2597 ];
2598 $result[] = [
2599 'value' => '{user_ip}',
2600 'label' => 'User IP',
2601 ];
2602 $result[] = [
2603 'value' => '{entry_source_url}',
2604 'label' => 'Entry Source URL',
2605 ];
2606 $result[] = [
2607 'value' => '{entry_submission_date}',
2608 'label' => 'Entry Submission Date',
2609 ];
2610
2611 return [
2612 'options' => $result,
2613 'hasMore' => false,
2614 ];
2615
2616 }
2617
2618 /**
2619 * Search Gravity Form fields for action.
2620 *
2621 * @param array $data data.
2622 * @return array
2623 */
2624 public function search_gravity_form_custom_fields( $data ) {
2625 $context = [];
2626 if ( ! class_exists( 'RGFormsModel' ) || ! class_exists( 'GFCommon' ) ) {
2627 return [];
2628 }
2629 $form_id = $data['filter'];
2630 $form = RGFormsModel::get_form_meta( $form_id );
2631 $custom_fields = [];
2632 if ( is_array( $form['fields'] ) ) {
2633 foreach ( $form['fields'] as $field ) {
2634 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
2635 foreach ( $field['inputs'] as $input ) {
2636 if ( ! $input['isHidden'] ) {
2637 $custom_fields[] = [
2638 'label' => GFCommon::get_label( $field, $input['id'] ),
2639 'value' => $input['id'],
2640 'type' => 'text',
2641 ];
2642 }
2643 }
2644 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
2645 $custom_fields[] = [
2646 'label' => GFCommon::get_label( $field ),
2647 'value' => $field['id'],
2648 'type' => 'text',
2649 ];
2650 }
2651 }
2652 }
2653 $context['fields'] = $custom_fields;
2654 return $context;
2655 }
2656
2657 /**
2658 * Get user register details via gravity forms.
2659 *
2660 * @param array $data data.
2661 *
2662 * @return array
2663 */
2664 public function search_pluggables_gravity_forms_user_register( $data ) {
2665 global $wpdb;
2666 $context = [];
2667 $pluggable_data = [];
2668
2669 $results = $wpdb->get_results(
2670 $wpdb->prepare(
2671 "SELECT id, form_id, date_created, ip, source_url, created_by FROM {$wpdb->prefix}gf_entry
2672 WHERE form_id = %d AND
2673 status = %s
2674 ORDER BY id
2675 DESC LIMIT 1",
2676 $data['filter']['gravity_form']['value'],
2677 'active'
2678 )
2679 );
2680
2681 if ( $results ) {
2682 $pluggable_data['gravity_form'] = (int) $results[0]->form_id;
2683 $pluggable_data['entry_id'] = $results[0]->id;
2684 $pluggable_data['user_ip'] = $results[0]->ip;
2685 $pluggable_data['entry_source_url'] = $results[0]->source_url;
2686 $pluggable_data['entry_submission_date'] = $results[0]->date_created;
2687 $pluggable_data['user'] = WordPress::get_user_context( $results[0]->created_by );
2688 $context['response_type'] = 'live';
2689 } else {
2690 $pluggable_data['gravity_form'] = '3';
2691 $pluggable_data['entry_id'] = '13';
2692 $pluggable_data['user_ip'] = '127.0.0.0';
2693 $pluggable_data['entry_source_url'] = 'https://example.com';
2694 $pluggable_data['entry_submission_date'] = '2024-02-05 09:41:59';
2695 $pluggable_data['user']['wp_user_id'] = '123';
2696 $pluggable_data['user']['user_login'] = 'johnd';
2697 $pluggable_data['user']['display_name'] = 'johnd';
2698 $pluggable_data['user']['user_firstname'] = 'John';
2699 $pluggable_data['user']['user_lastname'] = 'Doe';
2700 $pluggable_data['user']['user_email'] = 'johnd@yopmail.com';
2701 $pluggable_data['user']['user_role'] = [ 'subscriber' ];
2702 $context['response_type'] = 'sample';
2703 }
2704
2705 $context['pluggable_data'] = $pluggable_data;
2706 return $context;
2707 }
2708
2709 /**
2710 * Get payment form details for gravity forms.
2711 *
2712 * @param array $data data.
2713 *
2714 * @return array
2715 */
2716 public function search_pluggables_gravity_forms_payment( $data ) {
2717 global $wpdb;
2718 $context = [];
2719 $pluggable_data = [];
2720
2721 $results = $wpdb->get_results(
2722 $wpdb->prepare(
2723 "SELECT * FROM {$wpdb->prefix}gf_entry
2724 WHERE form_id = %d AND
2725 status = %s AND
2726 payment_status LIKE %s
2727 ORDER BY id
2728 DESC LIMIT 1",
2729 $data['filter']['gravity_form']['value'],
2730 'active',
2731 'Paid'
2732 )
2733 );
2734
2735 if ( $results ) {
2736 $pluggable_data['gravity_form'] = (int) $results[0]->form_id;
2737 $pluggable_data['entry_id'] = $results[0]->id;
2738 $pluggable_data['user_ip'] = $results[0]->ip;
2739 $pluggable_data['entry_source_url'] = $results[0]->source_url;
2740 $pluggable_data['entry_submission_date'] = $results[0]->date_created;
2741 $pluggable_data['payment_status'] = $results[0]->payment_status;
2742 $pluggable_data['payment_amount'] = $results[0]->payment_amount;
2743 $pluggable_data['currency'] = $results[0]->currency;
2744 $pluggable_data['payment_method'] = $results[0]->payment_method;
2745 $pluggable_data['transaction_id'] = $results[0]->transaction_id;
2746 $pluggable_data['user'] = WordPress::get_user_context( $results[0]->created_by );
2747 $context['response_type'] = 'live';
2748 } else {
2749 $pluggable_data['gravity_form'] = '3';
2750 $pluggable_data['entry_id'] = '13';
2751 $pluggable_data['user_ip'] = '127.0.0.0';
2752 $pluggable_data['entry_source_url'] = 'https://example.com';
2753 $pluggable_data['entry_submission_date'] = '2024-02-05 09:41:59';
2754 $pluggable_data['payment_status'] = 'Paid';
2755 $pluggable_data['payment_amount'] = '10.00';
2756 $pluggable_data['currency'] = 'USD';
2757 $pluggable_data['payment_method'] = 'visa';
2758 $pluggable_data['transaction_id'] = 'st_ooi98';
2759 $pluggable_data['user']['wp_user_id'] = '123';
2760 $pluggable_data['user']['user_login'] = 'johnd';
2761 $pluggable_data['user']['display_name'] = 'johnd';
2762 $pluggable_data['user']['user_firstname'] = 'John';
2763 $pluggable_data['user']['user_lastname'] = 'Doe';
2764 $pluggable_data['user']['user_email'] = 'johnd@yopmail.com';
2765 $pluggable_data['user']['user_role'] = [ 'subscriber' ];
2766 $context['response_type'] = 'sample';
2767 }
2768
2769 $context['pluggable_data'] = $pluggable_data;
2770 return $context;
2771 }
2772
2773 /**
2774 * Prepare fluentcrm lists.
2775 *
2776 * @param array $data Search Params.
2777 *
2778 * @return array[]
2779 */
2780 public function search_fluentcrm_lists( $data ) {
2781
2782 $list_api = FluentCrmApi( 'lists' );
2783 $all_lists = $list_api->all();
2784 $options = [];
2785
2786 if ( ! empty( $all_lists ) ) {
2787 foreach ( $all_lists as $list ) {
2788 $options[] = [
2789 'label' => $list->title,
2790 'value' => $list->id,
2791 ];
2792 }
2793 }
2794
2795 return [
2796 'options' => $options,
2797 'hasMore' => false,
2798 ];
2799 }
2800
2801 /**
2802 * Prepare fluentcrm contact status.
2803 *
2804 * @param array $data Search Params.
2805 *
2806 * @return array[]
2807 */
2808 public function search_fluentcrm_contact_status( $data ) {
2809
2810 $options = [
2811 [
2812 'label' => __( 'Subscribed', 'suretriggers' ),
2813 'value' => 'subscribed',
2814 ],
2815 [
2816 'label' => __( 'Pending', 'suretriggers' ),
2817 'value' => 'pending',
2818 ],
2819 [
2820 'label' => __( 'Unsubscribed', 'suretriggers' ),
2821 'value' => 'unsubscribed',
2822 ],
2823 [
2824 'label' => __( 'Bounced', 'suretriggers' ),
2825 'value' => 'bounced',
2826 ],
2827 [
2828 'label' => __( 'Complained', 'suretriggers' ),
2829 'value' => 'complained',
2830 ],
2831 ];
2832
2833 return [
2834 'options' => $options,
2835 'hasMore' => false,
2836 ];
2837 }
2838
2839 /**
2840 * Prepare fluentcrm contact status.
2841 *
2842 * @param array $data Search Params.
2843 *
2844 * @return array[]
2845 */
2846 public function search_fluentcrm_fetch_custom_fields( $data ) {
2847
2848 $options = [
2849 [
2850 'label' => __( 'Yes', 'suretriggers' ),
2851 'value' => 'true',
2852 ],
2853 [
2854 'label' => __( 'No', 'suretriggers' ),
2855 'value' => 'false',
2856 ],
2857 ];
2858
2859 return [
2860 'options' => $options,
2861 'hasMore' => false,
2862 ];
2863 }
2864
2865 /**
2866 * Prepare fluentcrm tags.
2867 *
2868 * @param array $data Search Params.
2869 *
2870 * @return array[]
2871 */
2872 public function search_fluentcrm_tags( $data ) {
2873
2874 if ( ! function_exists( 'FluentCrmApi' ) ) {
2875 return [];
2876 }
2877
2878 $tag_api = FluentCrmApi( 'tags' );
2879 $all_tags = $tag_api->all();
2880 $options = [];
2881
2882 if ( ! empty( $all_tags ) ) {
2883 foreach ( $all_tags as $tag ) {
2884 $options[] = [
2885 'label' => $tag->title,
2886 'value' => $tag->id,
2887 ];
2888 }
2889 }
2890
2891 return [
2892 'options' => $options,
2893 'hasMore' => false,
2894 ];
2895 }
2896
2897 /**
2898 * Prepare JetpackCRM Contact tags.
2899 *
2900 * @param array $data Search Params.
2901 *
2902 * @return array
2903 */
2904 public function search_jetpack_crm_contact_tags( $data ) {
2905
2906 if ( ! function_exists( 'zeroBSCRM_getCustomerTags' ) ) {
2907 return [];
2908 }
2909
2910 $all_tags = zeroBSCRM_getCustomerTags();
2911 $options = [];
2912
2913 if ( ! empty( $all_tags ) ) {
2914 foreach ( $all_tags as $tag ) {
2915 $options[] = [
2916 'label' => $tag['name'],
2917 'value' => $tag['id'],
2918 ];
2919 }
2920 }
2921
2922 return [
2923 'options' => $options,
2924 'hasMore' => false,
2925 ];
2926 }
2927
2928 /**
2929 * Prepare JetpackCRM Company tags.
2930 *
2931 * @param array $data Search Params.
2932 *
2933 * @return array
2934 */
2935 public function search_jetpack_crm_company_tags( $data ) {
2936
2937 if ( ! defined( 'ZBS_TYPE_COMPANY' ) ) {
2938 return [];
2939 }
2940
2941 global $wpdb;
2942 $all_tags = $wpdb->get_results( $wpdb->prepare( "SELECT `ID`,`zbstag_name` FROM `{$wpdb->prefix}zbs_tags` WHERE zbstag_objtype = %d", ZBS_TYPE_COMPANY ) );
2943
2944 $options = [];
2945 if ( ! empty( $all_tags ) ) {
2946 foreach ( $all_tags as $tag ) {
2947 $options[] = [
2948 'label' => $tag->zbstag_name,
2949 'value' => $tag->ID,
2950 ];
2951 }
2952 }
2953
2954 return [
2955 'options' => $options,
2956 'hasMore' => false,
2957 ];
2958 }
2959
2960 /**
2961 * Prepare JetpackCRM Companies list.
2962 *
2963 * @param array $data Search Params.
2964 *
2965 * @return array
2966 */
2967 public function search_jetpack_crm_companies_list( $data ) {
2968
2969 if ( ! function_exists( 'zeroBS_getCompanies' ) ) {
2970 return [];
2971 }
2972
2973 $all_companies = zeroBS_getCompanies();
2974 $options = [];
2975
2976 if ( ! empty( $all_companies ) ) {
2977 foreach ( $all_companies as $company ) {
2978 $options[] = [
2979 'label' => $company['name'],
2980 'value' => $company['id'],
2981 ];
2982 }
2983 }
2984
2985 return [
2986 'options' => $options,
2987 'hasMore' => false,
2988 ];
2989 }
2990
2991 /**
2992 * Prepare JetpackCRM contact status.
2993 *
2994 * @param array $data Search Params.
2995 *
2996 * @return array
2997 */
2998 public function search_jetpack_crm_contact_statuses( $data ) {
2999
3000 $options = [
3001 [
3002 'label' => __( 'Lead', 'suretriggers' ),
3003 'value' => 'Lead',
3004 ],
3005 [
3006 'label' => __( 'Customer', 'suretriggers' ),
3007 'value' => 'Customer',
3008 ],
3009 [
3010 'label' => __( 'Refused', 'suretriggers' ),
3011 'value' => 'Refused',
3012 ],
3013 [
3014 'label' => __( 'Blacklisted', 'suretriggers' ),
3015 'value' => 'Blacklisted',
3016 ],
3017 [
3018 'label' => __( 'Cancelled by Customer', 'suretriggers' ),
3019 'value' => 'Cancelled by Customer',
3020 ],
3021 [
3022 'label' => __( 'Cancelled by Us (Pre-Quote)', 'suretriggers' ),
3023 'value' => 'Cancelled by Us (Pre-Quote)',
3024 ],
3025 [
3026 'label' => __( 'Cancelled by Us (Post-Quote)', 'suretriggers' ),
3027 'value' => 'Cancelled by Us (Post-Quote)',
3028 ],
3029 ];
3030
3031 return [
3032 'options' => $options,
3033 'hasMore' => false,
3034 ];
3035 }
3036
3037 /**
3038 * Prepare FunnelKit Automations' lists.
3039 *
3040 * @param array $data Search Params.
3041 *
3042 * @return array
3043 */
3044 public function search_funnel_kit_automations_lists( $data ) {
3045
3046 if ( ! class_exists( 'BWFCRM_Lists' ) ) {
3047 return [];
3048 }
3049
3050 $bwfcrm_lists = \BWFCRM_Lists::get_lists();
3051
3052 $options = [];
3053
3054 foreach ( $bwfcrm_lists as $list ) {
3055 $options[] = [
3056 'label' => $list['name'],
3057 'value' => $list['ID'],
3058 ];
3059 }
3060
3061 return [
3062 'options' => $options,
3063 'hasMore' => false,
3064 ];
3065 }
3066
3067 /**
3068 * Prepare FunnelKit Automations' tags.
3069 *
3070 * @param array $data Search Params.
3071 *
3072 * @return array
3073 */
3074 public function search_funnel_kit_automations_tags( $data ) {
3075
3076 if ( ! class_exists( 'BWFCRM_Tag' ) ) {
3077 return [];
3078 }
3079
3080 $bwfcrm_tags = \BWFCRM_Tag::get_tags();
3081
3082 $options = [];
3083
3084 foreach ( $bwfcrm_tags as $tag ) {
3085 $options[] = [
3086 'label' => $tag['name'],
3087 'value' => $tag['ID'],
3088 ];
3089 }
3090
3091 return [
3092 'options' => $options,
3093 'hasMore' => false,
3094 ];
3095 }
3096
3097 /**
3098 * Prepare Wishlist Memberlists level.
3099 *
3100 * @param array $data Search Params.
3101 *
3102 * @return array[]
3103 */
3104 public function search_wishlistmember_lists( $data ) {
3105
3106 $wlm_levels = wlmapi_get_levels();
3107 $options = [];
3108
3109 if ( ! empty( $wlm_levels ) ) {
3110 foreach ( $wlm_levels['levels']['level'] as $list ) {
3111 if ( isset( $list['name'] ) && ! empty( $list['name'] ) ) {
3112 $options[] = [
3113 'label' => $list['name'],
3114 'value' => (string) $list['id'],
3115 ];
3116 }
3117 }
3118 }
3119
3120 return [
3121 'options' => $options,
3122 'hasMore' => false,
3123 ];
3124 }
3125
3126 /**
3127 * Prepare Wishlist Memberlists members.
3128 *
3129 * @param array $data Search Params.
3130 *
3131 * @return array<string, array<int<0, max>, array<string, mixed>>|false>
3132 */
3133 public function search_wishlistmember_members( $data ) {
3134
3135 if ( ! function_exists( 'wlmapi_get_members' ) ) {
3136 return [];
3137 }
3138
3139 $wlm_members = wlmapi_get_members();
3140 $options = [];
3141
3142 if ( ! empty( $wlm_members ) ) {
3143 foreach ( $wlm_members['members']['member'] as $list ) {
3144 if ( isset( $list['user_email'] ) && ! empty( $list['user_email'] ) ) {
3145 $options[] = [
3146 'label' => $list['user_email'],
3147 'value' => (string) $list['id'],
3148 ];
3149 }
3150 }
3151 }
3152
3153 return [
3154 'options' => $options,
3155 'hasMore' => false,
3156 ];
3157 }
3158
3159 /**
3160 * Prepare elementor popups.
3161 *
3162 * @param array $data Search Params.
3163 *
3164 * @return array[]
3165 */
3166 public function search_elementor_popups( $data ) {
3167
3168 $posts = get_posts(
3169 [
3170 'post_type' => 'elementor_library',
3171 'orderby' => 'title',
3172 'order' => 'ASC',
3173 'post_status' => 'publish',
3174 'meta_query' => [
3175 [
3176 'key' => '_elementor_template_type',
3177 'value' => 'popup',
3178 'compare' => '=',
3179 ],
3180 ],
3181 ]
3182 );
3183
3184 $options = [];
3185 if ( ! empty( $posts ) ) {
3186 foreach ( $posts as $post ) {
3187 $options[] = [
3188 'label' => $post->post_title,
3189 'value' => $post->ID,
3190 ];
3191 }
3192 }
3193
3194 return [
3195 'options' => $options,
3196 'hasMore' => false,
3197 ];
3198 }
3199
3200 /**
3201 * Prepare givewp forms.
3202 *
3203 * @param array $data Search Params.
3204 *
3205 * @return array
3206 */
3207 public function search_givewp_forms( $data ) {
3208
3209 $page = $data['page'];
3210 $limit = Utilities::get_search_page_limit();
3211 $offset = $limit * ( $page - 1 );
3212
3213 $posts = get_posts(
3214 [
3215 'post_type' => 'give_forms',
3216 'orderby' => 'title',
3217 'order' => 'ASC',
3218 'post_status' => 'publish',
3219 'posts_per_page' => $limit,
3220 'offset' => $offset,
3221 ]
3222 );
3223
3224 $options = [];
3225 if ( ! empty( $posts ) ) {
3226 foreach ( $posts as $post ) {
3227 $options[] = [
3228 'label' => $post->post_title,
3229 'value' => $post->ID,
3230 ];
3231 }
3232 }
3233
3234 $count = wp_count_posts( 'give_forms' )->publish;
3235
3236 return [
3237 'options' => $options,
3238 'hasMore' => $count > $limit && $count > $offset,
3239 ];
3240 }
3241
3242 /**
3243 * Prepare givewp recurring forms.
3244 *
3245 * @param array $data Search Params.
3246 *
3247 * @return array
3248 */
3249 public function search_givewp_recurring_forms( $data ) {
3250
3251 global $wpdb;
3252 $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 );
3253
3254 $options = [];
3255 if ( ! empty( $recurring ) ) {
3256 foreach ( $recurring as $form_id ) {
3257 $post_status = get_post_status( $form_id['form_id'] );
3258 if ( 'trash' !== $post_status ) {
3259 $options[] = [
3260 'label' => get_the_title( $form_id['form_id'] ),
3261 'value' => $form_id['form_id'],
3262 ];
3263 }
3264 }
3265 }
3266
3267 return [
3268 'options' => $options,
3269 'hasMore' => false,
3270 ];
3271 }
3272
3273 /**
3274 * Prepare givewp forms.
3275 *
3276 * @param array $data Search Params.
3277 *
3278 * @return array
3279 */
3280 public function search_givewp_form_fields( $data ) {
3281 $options = [
3282 [
3283 'label' => 'First Name',
3284 'value' => '_give_donor_billing_first_name',
3285 ],
3286 [
3287 'label' => 'Last Name',
3288 'value' => '_give_donor_billing_last_name',
3289 ],
3290 [
3291 'label' => 'Email',
3292 'value' => '_give_payment_donor_email',
3293 ],
3294 [
3295 'label' => 'Donation Amount',
3296 'value' => '_give_payment_total',
3297 ],
3298 [
3299 'label' => 'Currency',
3300 'value' => '_give_payment_currency',
3301 ],
3302 [
3303 'label' => 'Comment',
3304 'value' => '_give_donation_comment',
3305 ],
3306 [
3307 'label' => 'Zip',
3308 'value' => '_give_donor_billing_zip',
3309 ],
3310 [
3311 'label' => 'Country',
3312 'value' => '_give_donor_billing_country',
3313 ],
3314 [
3315 'label' => 'Address 1',
3316 'value' => '_give_donor_billing_address1',
3317 ],
3318 [
3319 'label' => 'Address 2',
3320 'value' => '_give_donor_billing_address2',
3321 ],
3322 [
3323 'label' => 'City',
3324 'value' => '_give_donor_billing_city',
3325 ],
3326 [
3327 'label' => 'State',
3328 'value' => '_give_donor_billing_state',
3329 ],
3330 ];
3331
3332 if ( class_exists( '\Give_FFM_Render_Form' ) ) {
3333 $form_id = $data['dynamic'];
3334 $custom_form_fields = [];
3335 if ( class_exists( '\GiveFormFieldManager\Helpers\Form' ) ) {
3336 $custom_form_fields = \GiveFormFieldManager\Helpers\Form::get_input_fields( $form_id );
3337 }
3338
3339 if ( ! empty( $custom_form_fields ) ) {
3340 if ( ! empty( $custom_form_fields[2] ) && is_array( $custom_form_fields[2] ) ) {
3341 foreach ( $custom_form_fields[2] as $custom_form_field ) {
3342 $custom_form_field['required'] = ( 'no' === $custom_form_field['required'] ) ? false : true;
3343 $options[] = [
3344 'label' => $custom_form_field['label'],
3345 'value' => $custom_form_field['name'],
3346 ];
3347 }
3348 }
3349 }
3350 }
3351
3352 return [
3353 'options' => $options,
3354 'hasMore' => false,
3355 ];
3356 }
3357
3358 /**
3359 * Prepare buddyboss group users.
3360 *
3361 * @param array $data Search Params.
3362 *
3363 * @return array[]
3364 */
3365 public function search_bb_group_users( $data ) {
3366 $options = [];
3367
3368 $group_id = $data['dynamic'];
3369 $admins = groups_get_group_admins( $group_id );
3370
3371 if ( ! empty( $admins ) ) {
3372 foreach ( $admins as $admin ) {
3373 $admin_user = get_user_by( 'id', $admin->user_id );
3374 $options[] = [
3375 'label' => $admin_user->display_name,
3376 'value' => $admin_user->ID,
3377 ];
3378 }
3379 }
3380
3381 $members = groups_get_group_members( [ 'group_id' => $group_id ] );
3382
3383 if ( isset( $members['members'] ) && ! empty( $members['members'] ) ) {
3384 foreach ( $members['members'] as $member ) {
3385 $options[] = [
3386 'label' => $member->display_name,
3387 'value' => $member->ID,
3388 ];
3389 }
3390 }
3391 return [
3392 'options' => $options,
3393 'hasMore' => false,
3394 ];
3395 }
3396
3397 /**
3398 * Prepare buddyboss groups.
3399 *
3400 * @param array $data Search Params.
3401 *
3402 * @return array[]
3403 */
3404 public function search_buddyboss_groups( $data ) {
3405 global $wpdb;
3406
3407 $options = [];
3408 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
3409 if ( ! empty( $groups ) ) {
3410 foreach ( $groups as $group ) {
3411 $options[] = [
3412 'label' => $group->name,
3413 'value' => $group->id,
3414 ];
3415 }
3416 }
3417 return [
3418 'options' => $options,
3419 'hasMore' => false,
3420 ];
3421 }
3422
3423 /**
3424 * Prepare buddyboss public groups.
3425 *
3426 * @param array $data Search Params.
3427 *
3428 * @return array[]
3429 */
3430 public function search_buddyboss_public_groups( $data ) {
3431 $options = [];
3432 $groups = groups_get_groups();
3433 if ( isset( $groups['groups'] ) && ! empty( $groups['groups'] ) ) {
3434 foreach ( $groups['groups'] as $group ) {
3435 if ( 'public' === $group->status ) {
3436 $options[] = [
3437 'label' => $group->name,
3438 'value' => $group->id,
3439 ];
3440 }
3441 }
3442 }
3443 return [
3444 'options' => $options,
3445 'hasMore' => false,
3446 ];
3447 }
3448
3449 /**
3450 * Prepare buddyboss private groups.
3451 *
3452 * @param array $data Search Params.
3453 *
3454 * @return array<string, array<int, array<string, mixed>>|false>
3455 */
3456 public function search_buddyboss_private_groups( $data ) {
3457 global $wpdb;
3458
3459 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'private'" );
3460
3461 $options = [];
3462 if ( $groups ) {
3463 foreach ( $groups as $group ) {
3464 $options[] = [
3465 'label' => $group->name,
3466 'value' => $group->id,
3467 ];
3468 }
3469 }
3470
3471 return [
3472 'options' => $options,
3473 'hasMore' => false,
3474 ];
3475 }
3476
3477 /**
3478 * Prepare buddyboss profile types list.
3479 *
3480 * @param array $data Search Params.
3481 *
3482 * @return array[]
3483 */
3484 public function search_bb_profile_type_list( $data ) {
3485 $options = [];
3486
3487 if ( function_exists( 'bp_get_active_member_types' ) ) {
3488 $types = bp_get_active_member_types(
3489 [
3490 'fields' => '*',
3491 ]
3492 );
3493 if ( $types ) {
3494 foreach ( $types as $type ) {
3495 $options[] = [
3496 'label' => $type->post_title,
3497 'value' => $type->ID,
3498 ];
3499 }
3500 }
3501 }
3502
3503 /**
3504 *
3505 * Ignore line
3506 *
3507 * @phpstan-ignore-next-line
3508 */
3509 return [
3510 'options' => $options,
3511 'hasMore' => false,
3512 ];
3513 }
3514
3515 /**
3516 * Prepare buddyboss public groups.
3517 *
3518 * @param array $data Search Params.
3519 *
3520 * @return array
3521 */
3522 public function search_buddyboss_group_type( $data ) {
3523 $options = [];
3524
3525 if ( ! function_exists( 'bp_groups_get_group_types' ) ) {
3526 return [];
3527 }
3528
3529 $registered_types = bp_groups_get_group_types();
3530 if ( ! empty( $registered_types ) ) {
3531 foreach ( $registered_types as $key => $type ) {
3532 $options[] = [
3533 'label' => $type,
3534 'value' => $key,
3535 ];
3536 }
3537 }
3538 return [
3539 'options' => $options,
3540 'hasMore' => false,
3541 ];
3542 }
3543
3544 /**
3545 * Get BuddyBoss Forum Topic Status.
3546 *
3547 * @param array $data data.
3548 *
3549 * @return array|void
3550 */
3551 public function search_bb_forum_topic_status( $data ) {
3552 $options = [];
3553
3554 if ( ! function_exists( 'bbp_get_topic_post_type' ) ) {
3555 return [];
3556 }
3557
3558 $page = $data['page'];
3559 $limit = Utilities::get_search_page_limit();
3560 $offset = $limit * ( $page - 1 );
3561
3562 $forum_args = [
3563 'post_type' => bbp_get_topic_post_type(),
3564 'orderby' => 'title',
3565 'order' => 'ASC',
3566 'post_status' => [ 'publish', 'private' ],
3567 'posts_per_page' => $limit,
3568 'offset' => $offset,
3569 ];
3570
3571 $forums = get_posts( $forum_args );
3572 if ( ! empty( $forums ) ) {
3573 foreach ( $forums as $key => $forum ) {
3574 $options[] = [
3575 'label' => $forum,
3576 'value' => $key,
3577 ];
3578 }
3579 }
3580 $count = count( $options );
3581 return [
3582 'options' => $options,
3583 'hasMore' => $count > $limit && $count > $offset,
3584 ];
3585 }
3586
3587 /**
3588 * Get BuddyBoss Forums List.
3589 *
3590 * @param array $data data.
3591 *
3592 * @return array|void
3593 */
3594 public function search_bb_forums_list( $data ) {
3595 $options = [];
3596
3597 $page = $data['page'];
3598 $limit = Utilities::get_search_page_limit();
3599 $offset = $limit * ( $page - 1 );
3600
3601 if ( ! function_exists( 'bbp_get_forum_post_type' ) ) {
3602 return [];
3603 }
3604
3605 $forum_args = [
3606 'post_type' => bbp_get_forum_post_type(),
3607 'orderby' => 'title',
3608 'order' => 'ASC',
3609 'post_status' => [ 'publish', 'private' ],
3610 'posts_per_page' => $limit,
3611 'offset' => $offset,
3612 ];
3613
3614 $forums = get_posts( $forum_args );
3615 if ( ! empty( $forums ) ) {
3616 foreach ( $forums as $key => $forum ) {
3617 $options[] = [
3618 'label' => $forum->post_title,
3619 'value' => $forum->ID,
3620 ];
3621 }
3622 }
3623 $count = count( $options );
3624 return [
3625 'options' => $options,
3626 'hasMore' => $count > $limit && $count > $offset,
3627 ];
3628 }
3629
3630 /**
3631 * Get BuddyBoss Forums List.
3632 *
3633 * @param array $data data.
3634 *
3635 * @return array|void
3636 */
3637 public function search_bb_topics_list( $data ) {
3638 $options = [];
3639
3640 $page = $data['page'];
3641 $limit = Utilities::get_search_page_limit();
3642 $offset = $limit * ( $page - 1 );
3643
3644 if ( ! function_exists( 'bbp_get_topic_post_type' ) ) {
3645 return [];
3646 }
3647
3648 $topic_args = [
3649 'post_type' => bbp_get_topic_post_type(),
3650 'post_parent' => $data['dynamic'],
3651 'orderby' => 'title',
3652 'order' => 'ASC',
3653 'post_status' => [ 'publish', 'private' ],
3654 'posts_per_page' => $limit,
3655 'offset' => $offset,
3656 ];
3657
3658 $topics = get_posts( $topic_args );
3659 if ( ! empty( $topics ) ) {
3660 foreach ( $topics as $key => $topic ) {
3661 $options[] = [
3662 'label' => $topic->post_title,
3663 'value' => $topic->ID,
3664 ];
3665 }
3666 }
3667 $count = count( $options );
3668 return [
3669 'options' => $options,
3670 'hasMore' => $count > $limit && $count > $offset,
3671 ];
3672 }
3673
3674 /**
3675 * Prepare elementor forms.
3676 *
3677 * @param array $data Search Params.
3678 *
3679 * @return array[]
3680 */
3681 public function search_elementor_forms( $data ) {
3682
3683 $elementor_forms = Utilities::get_elementor_forms();
3684
3685 $options = [];
3686 if ( ! empty( $elementor_forms ) ) {
3687 foreach ( $elementor_forms as $key => $value ) {
3688 $options[] = [
3689 'label' => $value,
3690 'value' => $key,
3691 ];
3692 }
3693 }
3694
3695 return [
3696 'options' => $options,
3697 'hasMore' => false,
3698 ];
3699 }
3700
3701 /**
3702 * Prepare elementor forms.
3703 *
3704 * @param array $data Search Params.
3705 *
3706 * @return array[]
3707 */
3708 public function search_new_elementor_forms( $data ) {
3709
3710 global $wpdb;
3711 $elementor_forms = [];
3712 $post_metas = $wpdb->get_results(
3713 $wpdb->prepare(
3714 "SELECT pm.post_id, pm.meta_value
3715 FROM $wpdb->postmeta pm
3716 LEFT JOIN $wpdb->posts p
3717 ON p.ID = pm.post_id
3718 WHERE p.post_type IS NOT NULL
3719 AND p.post_status = %s
3720 AND pm.meta_key = %s
3721 AND pm.`meta_value` LIKE %s",
3722 'publish',
3723 '_elementor_data',
3724 '%%form_fields%%'
3725 )
3726 );
3727
3728 if ( ! empty( $post_metas ) ) {
3729 foreach ( $post_metas as $post_meta ) {
3730 /**
3731 *
3732 * Ignore line
3733 *
3734 * @phpstan-ignore-next-line
3735 */
3736 $inner_forms = Utilities::search_elementor_forms( json_decode( $post_meta->meta_value ) );
3737 if ( ! empty( $inner_forms ) ) {
3738 foreach ( $inner_forms as $form ) {
3739 /**
3740 *
3741 * Ignore line
3742 *
3743 * @phpstan-ignore-next-line
3744 */
3745 $elementor_forms[ $post_meta->post_id . '_' . $form->id ] = $form->settings->form_name . ' (' . $form->id . ')';
3746 }
3747 }
3748 }
3749 }
3750
3751 $options = [];
3752 if ( ! empty( $elementor_forms ) ) {
3753 foreach ( $elementor_forms as $key => $value ) {
3754 $options[] = [
3755 'label' => $value,
3756 'value' => $key,
3757 ];
3758 }
3759 }
3760
3761 /**
3762 *
3763 * Ignore line
3764 *
3765 * @phpstan-ignore-next-line
3766 */
3767 return [
3768 'options' => $options,
3769 'hasMore' => false,
3770 ];
3771 }
3772
3773 /**
3774 * Prepare elementor form fields.
3775 *
3776 * @param array $data Search Params.
3777 *
3778 * @return array[]
3779 */
3780 public function search_pluggable_elementor_form_fields( $data ) {
3781 $result = [];
3782 $form_id = absint( $data['dynamic'] );
3783 $elementor_form_fields = ( new Utilities() )->get_elementor_form_fields( $data );
3784 $options = [];
3785 if ( ! empty( $elementor_form_fields ) ) {
3786 foreach ( $elementor_form_fields as $key => $value ) {
3787 $options[] = [
3788 'label' => $value,
3789 'value' => '{' . $key . '}',
3790 ];
3791 }
3792 }
3793
3794 return [
3795 'options' => $options,
3796 'hasMore' => false,
3797 ];
3798 }
3799
3800 /**
3801 * Get all events
3802 *
3803 * @param array $data Data array.
3804 *
3805 * @return array
3806 */
3807 public function search_event_calendar_event( $data ) {
3808 $page = $data['page'];
3809 $limit = Utilities::get_search_page_limit();
3810 $offset = $limit * ( $page - 1 );
3811
3812 $posts = get_posts(
3813 [
3814 'post_type' => 'tribe_events',
3815 'orderby' => 'title',
3816 'order' => 'ASC',
3817 'post_status' => 'publish',
3818 'posts_per_page' => $limit,
3819 'offset' => $offset,
3820 ]
3821 );
3822
3823 $options = [];
3824 if ( ! empty( $posts ) ) {
3825 foreach ( $posts as $post ) {
3826 $options[] = [
3827 'label' => $post->post_title,
3828 'value' => $post->ID,
3829 ];
3830 }
3831 }
3832
3833 $count = wp_count_posts( 'tribe_events' )->publish;
3834
3835 return [
3836 'options' => $options,
3837 'hasMore' => $count > $limit && $count > $offset,
3838 ];
3839 }
3840
3841 /**
3842 * Get all events tickets
3843 *
3844 * @param array $data Data array.
3845 *
3846 * @return array
3847 */
3848 public function search_event_calendar_event_tickets( $data ) {
3849 $event_id = $data['dynamic'];
3850 $options = [];
3851
3852 if ( ! class_exists( 'Tribe__Tickets_Plus__Commerce__WooCommerce__Main' ) ) {
3853 return [];
3854 }
3855
3856 $tickets = \Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance()->get_all_event_tickets( $event_id );
3857 if ( ! empty( $tickets ) ) {
3858 foreach ( $tickets as $ticket_object ) {
3859 $options[] = [
3860 'label' => $ticket_object->name,
3861 'value' => $ticket_object->ID,
3862 ];
3863 }
3864 }
3865
3866 return [
3867 'options' => $options,
3868 'hasMore' => false,
3869 ];
3870 }
3871
3872 /**
3873 * Prepare rsvp event calendar events.
3874 *
3875 * @param array $data Search Params.
3876 *
3877 * @return array
3878 */
3879 public function search_event_calendar_rsvp_event( $data ) {
3880
3881 $page = $data['page'];
3882 $limit = Utilities::get_search_page_limit();
3883 $offset = $limit * ( $page - 1 );
3884
3885 $posts = get_posts(
3886 [
3887 'post_type' => 'tribe_events',
3888 'orderby' => 'title',
3889 'order' => 'ASC',
3890 'post_status' => 'publish',
3891 'posts_per_page' => $limit,
3892 'offset' => $offset,
3893 ]
3894 );
3895
3896 $options = [];
3897 if ( ! empty( $posts ) ) {
3898 $ticket_handler = new Tribe__Tickets__Tickets_Handler();
3899 foreach ( $posts as $post ) {
3900
3901 $get_rsvp_ticket = $ticket_handler->get_event_rsvp_tickets( $post->ID );
3902
3903 if ( ! empty( $get_rsvp_ticket ) ) {
3904 $options[] = [
3905 'label' => $post->post_title,
3906 'value' => $post->ID,
3907 ];
3908 }
3909 }
3910 }
3911 $count = wp_count_posts( 'tribe_events' )->publish;
3912
3913 return [
3914 'options' => $options,
3915 'hasMore' => $count > $limit && $count > $offset,
3916 ];
3917 }
3918
3919 /**
3920 * Prepare Restrict Content Membership Level.
3921 *
3922 * @param array $data Search Params.
3923 *
3924 * @return array[]
3925 */
3926 public function search_restrictcontent_membership_level( $data ) {
3927
3928 $rcp_memberships = rcp_get_membership_levels();
3929 $options = [];
3930
3931 if ( ! empty( $rcp_memberships ) ) {
3932 foreach ( $rcp_memberships as $list ) {
3933 $options[] = [
3934 'label' => ucfirst( $list->name ),
3935 'value' => $list->id,
3936 ];
3937 }
3938 }
3939
3940 return [
3941 'options' => $options,
3942 'hasMore' => false,
3943 ];
3944 }
3945
3946 /**
3947 * Prepare Restrict Content Customer.
3948 *
3949 * @param array $data Search Params.
3950 *
3951 * @return array[]
3952 */
3953 public function search_restrictcontent_customer( $data ) {
3954
3955 $rcp_users = rcp_get_memberships();
3956 $options = [];
3957
3958 if ( ! empty( $rcp_users ) ) {
3959 foreach ( $rcp_users as $list ) {
3960 $user = get_user_by( 'ID', $list->get_user_id() );
3961 $user_label = $user->user_email;
3962
3963 if ( $user->display_name !== $user->user_email ) {
3964 $user_label .= ' (' . $user->display_name . ')';
3965 }
3966
3967 $options[] = [
3968 'label' => $user_label,
3969 'value' => $list->get_customer_id(),
3970 ];
3971 }
3972 }
3973
3974 return [
3975 'options' => $options,
3976 'hasMore' => false,
3977 ];
3978 }
3979
3980
3981 /**
3982 * Fetch the Presto Player video List.
3983 *
3984 * @param array $data Search Params.
3985 *
3986 * @return array[]
3987 */
3988 public function search_ap_presto_player_video_list( $data ) {
3989
3990 $videos = ( new Video() )->all();
3991 $options = [];
3992 if ( ! empty( $videos ) ) {
3993 foreach ( $videos as $video ) {
3994 $options[] = [
3995 'label' => $video->__get( 'title' ),
3996 'value' => (string) $video->__get( 'id' ),
3997 ];
3998 }
3999 }
4000
4001 return [
4002 'options' => $options,
4003 'hasMore' => false,
4004 ];
4005 }
4006
4007 /**
4008 * Presto Player Video percentage.
4009 *
4010 * @param array $data Search Params.
4011 *
4012 * @return array[]
4013 */
4014 public function search_prestoplayer_video_percent( $data ) {
4015
4016 $percents = [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ];
4017 $options = [];
4018
4019 foreach ( $percents as $percent ) {
4020 $options[] = [
4021 'label' => $percent . '%',
4022 'value' => (string) $percent,
4023 ];
4024 }
4025
4026 return [
4027 'options' => $options,
4028 'hasMore' => false,
4029 ];
4030 }
4031
4032 /**
4033 * Get user profile field options.
4034 *
4035 * @return array
4036 * @since 1.0.0
4037 */
4038 public function search_user_field_options() {
4039
4040 $options = apply_filters(
4041 'sure_trigger_get_user_field_options',
4042 [
4043 [
4044 'label' => __( 'User Name', 'suretriggers' ),
4045 'value' => 'user_login',
4046 ],
4047 [
4048 'label' => __( 'User Email', 'suretriggers' ),
4049 'value' => 'user_email',
4050 ],
4051 [
4052 'label' => __( 'Display Name', 'suretriggers' ),
4053 'value' => 'display_name',
4054 ],
4055 [
4056 'label' => __( 'User Password', 'suretriggers' ),
4057 'value' => 'user_pass',
4058 ],
4059 [
4060 'label' => __( 'Website', 'suretriggers' ),
4061 'value' => 'user_url',
4062 ],
4063 ]
4064 );
4065
4066 return [
4067 'options' => $options,
4068 'hasMore' => false,
4069 ];
4070 }
4071
4072 /**
4073 * Get user post field options.
4074 *
4075 * @return array
4076 * @since 1.0.0
4077 */
4078 public function search_post_field_options() {
4079
4080 return [
4081 'options' => [
4082 [
4083 'label' => __( 'Type', 'suretriggers' ),
4084 'value' => 'post_type',
4085 'dynamic_field' => [
4086 'type' => 'select-creatable',
4087 'ajaxIdentifier' => 'post_types',
4088 ],
4089 ],
4090 [
4091 'label' => __( 'Status', 'suretriggers' ),
4092 'value' => 'post_status',
4093 'dynamic_field' => [
4094 'type' => 'select-async',
4095 'ajaxIdentifier' => 'post_statuses',
4096 ],
4097 ],
4098 [
4099 'label' => __( 'Author', 'suretriggers' ),
4100 'value' => 'post_author',
4101 'dynamic_field' => [
4102 'type' => 'select-async',
4103 'ajaxIdentifier' => 'user',
4104 ],
4105 ],
4106 [
4107 'label' => __( 'Title', 'suretriggers' ),
4108 'value' => 'post_title',
4109 'dynamic_field' => [
4110 'type' => 'select-creatable',
4111 ],
4112 ],
4113 [
4114 'label' => __( 'Slug', 'suretriggers' ),
4115 'value' => 'post_slug',
4116 'dynamic_field' => [
4117 'type' => 'select-creatable',
4118 ],
4119 ],
4120 [
4121 'label' => __( 'Content', 'suretriggers' ),
4122 'value' => 'post_content',
4123 'dynamic_field' => [
4124 'type' => 'html-editor',
4125 ],
4126 ],
4127 ],
4128 'hasMore' => false,
4129 ];
4130 }
4131
4132 /**
4133 * Bricksbuilder grouped data.
4134 *
4135 * @param array $data data.
4136 * @return array
4137 */
4138 public function search_bb_groups( $data ) {
4139
4140 global $wpdb;
4141 $options = [];
4142
4143 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'bp_groups' ) ) ) {
4144
4145 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %s', $wpdb->prefix . 'bp_groups' ) );
4146
4147 if ( $results ) {
4148 foreach ( $results as $result ) {
4149 $options[] = [
4150 'label' => $result->name,
4151 'value' => $result->id,
4152 ];
4153 }
4154 }
4155 }
4156
4157 return [
4158 'options' => $options,
4159 'hasMore' => false,
4160 ];
4161 }
4162
4163 /**
4164 * Search forms.
4165 *
4166 * @return array
4167 */
4168 public function search_bb_forums() {
4169 $options = [];
4170 $allowed_atatus = [ 'publish', 'private' ];
4171 $forum_args = [
4172 'post_type' => bbp_get_forum_post_type(),
4173 'posts_per_page' => -1,
4174 'orderby' => 'title',
4175 'order' => 'ASC',
4176 'post_status' => 'any',
4177 ];
4178 $forums = get_posts( $forum_args );
4179
4180 if ( ! empty( $forums ) ) {
4181 foreach ( $forums as $forum ) {
4182 if ( in_array( $forum->post_status, $allowed_atatus, true ) ) {
4183 $options[] = [
4184 'label' => $forum->post_title,
4185 'value' => $forum->ID,
4186 ];
4187 }
4188 }
4189 }
4190 return [
4191 'options' => $options,
4192 'hasMore' => false,
4193 ];
4194 }
4195
4196 /**
4197 * Search Affiliate WP Referral Type.
4198 *
4199 * @return array
4200 */
4201 public function search_affwp_referral_type() {
4202 $options = [];
4203
4204 if ( ! function_exists( 'affiliate_wp' ) ) {
4205 return [];
4206 }
4207
4208 $types = affiliate_wp()->referrals->types_registry->get_types();
4209 if ( ! empty( $types ) ) {
4210 foreach ( $types as $type_id => $type ) {
4211 $options[] = [
4212 'label' => $type['label'],
4213 'value' => $type_id,
4214 ];
4215 }
4216 }
4217 return [
4218 'options' => $options,
4219 'hasMore' => false,
4220 ];
4221 }
4222
4223 /**
4224 * Search Affiliate WP Referral Status.
4225 *
4226 * @return array
4227 */
4228 public function search_affwp_referral_status() {
4229 $options = [];
4230
4231 if ( ! function_exists( 'affwp_get_affiliate_statuses' ) ) {
4232 return [];
4233 }
4234
4235 $statuses = affwp_get_affiliate_statuses();
4236 if ( ! empty( $statuses ) ) {
4237 foreach ( $statuses as $key => $status ) {
4238 $options[] = [
4239 'label' => $status,
4240 'value' => $key,
4241 ];
4242 }
4243 }
4244 return [
4245 'options' => $options,
4246 'hasMore' => false,
4247 ];
4248 }
4249
4250 /**
4251 * Search Affiliate WP Affiliates list.
4252 *
4253 * @return array
4254 */
4255 public function search_affwp_affiliates_list() {
4256 $options = [];
4257
4258 global $wpdb;
4259 $affiliates = $wpdb->get_results( "SELECT affiliate_id FROM {$wpdb->prefix}affiliate_wp_affiliates" );
4260
4261 if ( ! function_exists( 'affwp_get_affiliate_name' ) ) {
4262 return [];
4263 }
4264
4265 if ( ! empty( $affiliates ) ) {
4266 foreach ( $affiliates as $affiliate ) {
4267 $options[] = [
4268 'label' => affwp_get_affiliate_name( $affiliate->affiliate_id ),
4269 'value' => $affiliate->affiliate_id,
4270 ];
4271 }
4272 }
4273 return [
4274 'options' => $options,
4275 'hasMore' => false,
4276 ];
4277 }
4278
4279 /**
4280 * Search Slice WP Affiliates list.
4281 *
4282 * @return array
4283 */
4284 public function search_slicewp_affiliates_list() {
4285 $options = [];
4286
4287 global $wpdb;
4288 $affiliates = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}slicewp_affiliates" );
4289
4290 if ( ! function_exists( 'slicewp_get_affiliate_name' ) ) {
4291 return [];
4292 }
4293
4294 if ( ! empty( $affiliates ) ) {
4295 foreach ( $affiliates as $affiliate ) {
4296 $options[] = [
4297 'label' => slicewp_get_affiliate_name( $affiliate->id ),
4298 'value' => $affiliate->id,
4299 ];
4300 }
4301 }
4302 return [
4303 'options' => $options,
4304 'hasMore' => false,
4305 ];
4306 }
4307
4308 /**
4309 * Search for commissions list.
4310 *
4311 * @param array $data data.
4312 *
4313 * @return array
4314 */
4315 public function search_slicewp_commissions_list( $data ) {
4316 $options = [];
4317 global $wpdb;
4318 $affiliate_id = $data['dynamic']['affiliate_id'];
4319 $commissions = $wpdb->get_results(
4320 $wpdb->prepare(
4321 "SELECT *
4322 FROM {$wpdb->prefix}slicewp_commissions WHERE affiliate_id=%d ORDER BY id DESC ",
4323 $affiliate_id
4324 )
4325 );
4326
4327
4328 if ( ! empty( $commissions ) ) {
4329 foreach ( $commissions as $commission ) {
4330 $options[] = [
4331 'label' => $commission->reference . '(' . $commission->type . ')',
4332 'value' => $commission->id,
4333 ];
4334 }
4335 }
4336 return [
4337 'options' => $options,
4338 'hasMore' => false,
4339 ];
4340 }
4341
4342
4343 /**
4344 * Search Affiliate WP Affiliates list.
4345 *
4346 * @return array
4347 */
4348 public function search_affwp_affiliates_rate_type() {
4349 $options = [];
4350
4351 if ( ! function_exists( 'affwp_get_affiliate_rate_types' ) ) {
4352 return [];
4353 }
4354
4355 $rate_types = affwp_get_affiliate_rate_types();
4356
4357 if ( ! empty( $rate_types ) ) {
4358 foreach ( $rate_types as $key => $rate_type ) {
4359 $options[] = [
4360 'label' => $rate_type,
4361 'value' => $key,
4362 ];
4363 }
4364 }
4365 return [
4366 'options' => $options,
4367 'hasMore' => false,
4368 ];
4369 }
4370
4371 /**
4372 * Get last data for trigger.
4373 *
4374 * @param array $data data.
4375 * @return array
4376 */
4377 public function search_affiliate_wp_triggers_last_data( $data ) {
4378 global $wpdb;
4379
4380 $context = [];
4381 $context['response_type'] = 'sample';
4382
4383 $user_data = WordPress::get_sample_user_context();
4384
4385 $affiliate_data = [
4386 'affiliate_id' => 1,
4387 'rest_id' => '',
4388 'user_id' => 1,
4389 'rate' => '',
4390 'rate_type' => '',
4391 'flat_rate_basis' => '',
4392 'payment_email' => 'admin@gmail.com',
4393 'status' => 'active',
4394 'earnings' => 0,
4395 'unpaid_earnings' => 0,
4396 'referrals' => 0,
4397 'visits' => 0,
4398 'date_registered' => '2023-01-18 13:35:22',
4399 'dynamic_coupon' => 'KDJSKS',
4400 ];
4401
4402 $referral_data = [
4403 'referral_id' => 1,
4404 'affiliate_id' => 1,
4405 'visit_id' => 0,
4406 'rest_id' => '',
4407 'customer_id' => 0,
4408 'parent_id' => 0,
4409 'description' => 'Testing',
4410 'status' => 'unpaid',
4411 'amount' => '12.00',
4412 'currency' => '',
4413 'custom' => 'custom',
4414 'context' => '',
4415 'campaign' => '',
4416 'reference' => 'Reference',
4417 'products' => '',
4418 'date' => '2023-01-18 16:36:59',
4419 'type' => 'opt-in',
4420 'payout_id' => 0,
4421 ];
4422
4423 if ( ! function_exists( 'affwp_get_dynamic_affiliate_coupons' ) || ! function_exists( 'affwp_get_affiliate' ) || ! function_exists( 'affwp_get_referral' ) ) {
4424 return [];
4425 }
4426
4427 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4428
4429 if ( in_array( $term, [ 'affiliate_approved', 'affiliate_awaiting_approval' ], true ) ) {
4430 $status = 'affiliate_approved' === $term ? 'active' : 'pending';
4431 $affiliate = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_affiliates WHERE affiliate_id = ( SELECT max(affiliate_id) FROM {$wpdb->prefix}affiliate_wp_affiliates )" );
4432
4433 if ( ! empty( $affiliate ) ) {
4434 $affiliate = affwp_get_affiliate( $affiliate->affiliate_id );
4435 $affiliate_data = get_object_vars( $affiliate );
4436 $user_data = WordPress::get_user_context( $affiliate->user_id );
4437 $context['response_type'] = 'live';
4438 }
4439 $affiliate_data['status'] = $status;
4440 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data );
4441
4442 } elseif ( 'affiliate_makes_referral' == $term ) {
4443 $type = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4444 $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 ) );
4445
4446 if ( ! empty( $referral ) ) {
4447 $referral = affwp_get_referral( $referral->referral_id );
4448 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4449 $affiliate_data = get_object_vars( $affiliate );
4450 $user_data = WordPress::get_user_context( $affiliate->user_id );
4451 $referral_data = get_object_vars( $referral );
4452 $context['response_type'] = 'live';
4453 }
4454 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4455 } elseif ( 'affiliate_wc_product_purchased' == $term ) {
4456 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4457 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'woocommerce' ORDER BY referral_id DESC LIMIT 1" );
4458
4459 if ( ! empty( $referral ) ) {
4460 $referral = affwp_get_referral( $referral->referral_id );
4461 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4462 $affiliate_data = get_object_vars( $affiliate );
4463 if ( ! empty( $referral ) && function_exists( 'wc_get_order' ) ) {
4464 $order_id = $referral->reference;
4465 $order = wc_get_order( $order_id );
4466 if ( $order instanceof \WC_Order ) {
4467 $user_data = WordPress::get_user_context( $order->get_customer_id() );
4468 }
4469 }
4470 $referral_data = get_object_vars( $referral );
4471 $context['response_type'] = 'live';
4472 }
4473 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
4474 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data, $dynamic_coupons );
4475 if ( ! empty( $referral ) && function_exists( 'wc_get_order' ) ) {
4476 $order_id = $referral->reference;
4477 $order = wc_get_order( $order_id );
4478 $items = $order->get_items();
4479 foreach ( $items as $item ) {
4480 $context['pluggable_data']['product'] = $item['product_id'];
4481 }
4482 } else {
4483 $context['pluggable_data']['product'] = 1;
4484 }
4485 } elseif ( 'affiliate_edd_product_purchased' == $term ) {
4486 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4487 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'edd' ORDER BY referral_id DESC LIMIT 1" );
4488
4489 if ( ! empty( $referral ) ) {
4490 $referral = affwp_get_referral( $referral->referral_id );
4491 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4492 $affiliate_data = get_object_vars( $affiliate );
4493 $user_data = WordPress::get_user_context( $affiliate->user_id );
4494 $referral_data = get_object_vars( $referral );
4495 $context['response_type'] = 'live';
4496 }
4497 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4498 if ( ! empty( $referral ) && function_exists( 'edd_get_payment' ) ) {
4499 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
4500 $edd_payment_id = $referral->reference;
4501 $payment = edd_get_payment( $edd_payment_id );
4502 $cart_details = $payment->cart_details;
4503 $payment = get_object_vars( $payment );
4504 $context['pluggable_data'] = array_merge( $context['pluggable_data'], $dynamic_coupons, $payment );
4505 foreach ( $cart_details as $detail ) {
4506 $context['pluggable_data']['product'] = $detail['id'];
4507 }
4508 } else {
4509 $context['pluggable_data']['product'] = 1;
4510 }
4511 } elseif ( 'affiliate_mb_product_purchased' == $term ) {
4512 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4513 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'memberpress' ORDER BY referral_id DESC LIMIT 1" );
4514
4515 if ( ! empty( $referral ) && class_exists( '\MeprTransaction' ) ) {
4516 $referral = affwp_get_referral( $referral->referral_id );
4517 $reference_id = $referral->reference;
4518 $transaction = new MeprTransaction( $reference_id );
4519 $user_id = $transaction->user_id;
4520 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4521 $affiliate_data = get_object_vars( $affiliate );
4522 $user_data = WordPress::get_user_context( $user_id );
4523 $referral_data = get_object_vars( $referral );
4524 $context['response_type'] = 'live';
4525 }
4526 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
4527 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data, $dynamic_coupons );
4528 if ( ! empty( $referral ) && class_exists( '\MeprTransaction' ) ) {
4529 $membership_id = $wpdb->get_var(
4530 $wpdb->prepare(
4531 "SELECT product_id FROM
4532 {$wpdb->prefix}mepr_transactions WHERE id = %d",
4533 $referral->reference
4534 )
4535 );
4536 $context['pluggable_data']['product'] = $membership_id;
4537 $context['pluggable_data']['product_name'] = get_the_title( $membership_id );
4538 } else {
4539 $context['pluggable_data']['product'] = 1;
4540 $context['pluggable_data']['product_name'] = 'membership1';
4541 }
4542 } elseif ( 'affiliate_referral_paid' == $term ) {
4543 $type = isset( $data['filter']['type']['value'] ) ? $data['filter']['type']['value'] : '';
4544 $referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE type = %s AND status = 'paid'", $type ) );
4545
4546 if ( ! empty( $referral ) ) {
4547 $referral = affwp_get_referral( $referral->referral_id );
4548 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4549 $affiliate_data = get_object_vars( $affiliate );
4550 $user_data = WordPress::get_user_context( $affiliate->user_id );
4551 $referral_data = get_object_vars( $referral );
4552 $context['response_type'] = 'live';
4553 }
4554 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4555 } elseif ( 'affiliate_referral_rejected' == $term ) {
4556 $type = isset( $data['filter']['type']['value'] ) ? $data['filter']['type']['value'] : '';
4557 $referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE type = %s AND status = 'rejected'", $type ) );
4558 if ( ! empty( $referral ) ) {
4559 $referral = affwp_get_referral( $referral->referral_id );
4560 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4561 $affiliate_data = get_object_vars( $affiliate );
4562 $user_data = WordPress::get_user_context( $affiliate->user_id );
4563 $referral_data = get_object_vars( $referral );
4564 $context['response_type'] = 'live';
4565 }
4566 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4567 } else {
4568 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE referral_id = ( SELECT max(referral_id) FROM {$wpdb->prefix}affiliate_wp_referrals )" );
4569
4570 if ( ! empty( $referral ) ) {
4571 $referral = affwp_get_referral( $referral->referral_id );
4572 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4573 $affiliate_data = get_object_vars( $affiliate );
4574 $user_data = WordPress::get_user_context( $affiliate->user_id );
4575 $referral_data = get_object_vars( $referral );
4576 $context['response_type'] = 'live';
4577 }
4578 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4579 }
4580
4581 return $context;
4582 }
4583
4584 /**
4585 * Get last data for trigger.
4586 *
4587 * @param array $data data.
4588 * @return array
4589 */
4590 public function search_jetpack_crm_triggers_last_data( $data ) {
4591
4592 if ( ! function_exists( 'zeroBS_getCompanyCount' ) || ! function_exists( 'zeroBS_getCustomerCount' ) || ! function_exists( 'zeroBS_getQuoteCount' ) ) {
4593 return [];
4594 }
4595
4596 global $wpdb;
4597
4598 $context = [];
4599 $context['response_type'] = 'sample';
4600
4601 $company_id = [ 'company_id' => 1 ];
4602 $contact_id = [ 'contact_id' => 1 ];
4603 $quote_id = [ 'quote_id' => 1 ];
4604 $event_id = [ 'event_id' => 1 ];
4605 $invoice_id = [ 'invoice_id' => 1 ];
4606 $transaction_id = [ 'transaction_id' => 1 ];
4607
4608 $company = [
4609 'company_id' => 1,
4610 'company_status' => 'Lead',
4611 'company_name' => 'Example Company',
4612 'company_email' => 'info@example.com',
4613 'main_address_line_1' => '123 Main Street',
4614 'main_address_line_2' => 'Suite 456',
4615 'main_address_city' => 'New York',
4616 'main_address_state' => 'NY',
4617 'main_address_postal_code' => '10001',
4618 'main_address_country' => 'United States',
4619 'second_address_line_1' => '789 Second Avenue',
4620 'second_address_line_2' => 'Floor 10',
4621 'second_address_city' => 'Los Angeles',
4622 'second_address_state' => 'CA',
4623 'second_address_postal_code' => '90001',
4624 'second_address_country' => 'United States',
4625 'main_telephone' => '+1 123-456-7890',
4626 'secondary_telephone' => '+1 987-654-3210',
4627 ];
4628
4629 $contact = [
4630 'contact_id' => 1,
4631 'status' => 'Lead',
4632 'prefix' => 'Mr.',
4633 'full_name' => 'John Doe',
4634 'first_name' => 'John',
4635 'last_name' => 'Doe',
4636 'email' => 'johndoe@example.com',
4637 'main_address_line_1' => '123 Main Street',
4638 'main_address_line_2' => 'Suite 456',
4639 'main_address_city' => 'New York',
4640 'main_address_state' => 'NY',
4641 'main_address_postal_code' => '10001',
4642 'main_address_country' => 'United States',
4643 'second_address_line_1' => '789 Second Avenue',
4644 'second_address_line_2' => 'Floor 10',
4645 'second_address_city' => 'Los Angeles',
4646 'second_address_state' => 'CA',
4647 'second_address_postal_code' => '90001',
4648 'second_address_country' => 'United States',
4649 'home_telephone' => '+1 123-456-7890',
4650 'work_telephone' => '+1 987-654-3210',
4651 'mobile_telephone' => '+1 555-555-5555',
4652 ];
4653
4654 $quote = [
4655 'quote_id' => 1,
4656 'contact_id' => 2,
4657 'contact_email' => 'john@example.com',
4658 'contact_name' => 'John Doe',
4659 'status' => '<strong>Created, not yet accepted</strong>',
4660 'title' => 'Sample Quote',
4661 'value' => 1000,
4662 'date' => '2023-05-23',
4663 'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
4664 'notes' => 'Additional notes about the quote.',
4665 ];
4666
4667 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4668
4669 switch ( $term ) {
4670 case 'company_created':
4671 if ( zeroBS_getCompanyCount() > 0 ) {
4672 $company_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_companies WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_companies )" );
4673 $company = JetpackCRM::get_company_context( $company_data->ID );
4674 $context['response_type'] = 'live';
4675 }
4676 $context['pluggable_data'] = $company;
4677 break;
4678 case 'company_deleted':
4679 $context['pluggable_data'] = $company_id;
4680 break;
4681 case 'contact_created':
4682 if ( zeroBS_getCustomerCount() > 0 ) {
4683 $contact_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_contacts WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_contacts )" );
4684 $contact = JetpackCRM::get_contact_context( $contact_data->ID );
4685 $context['response_type'] = 'live';
4686 }
4687 $context['pluggable_data'] = $contact;
4688 break;
4689 case 'contact_deleted':
4690 $context['pluggable_data'] = $contact_id;
4691 break;
4692 case 'event_deleted':
4693 $context['pluggable_data'] = $event_id;
4694 break;
4695 case 'invoice_deleted':
4696 $context['pluggable_data'] = $invoice_id;
4697 break;
4698 case 'quote_accepted':
4699 case 'quote_created':
4700 if ( zeroBS_getQuoteCount() > 0 ) {
4701 $quote_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_quotes WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_quotes )" );
4702 $quote = JetpackCRM::get_quote_context( $quote_data->ID );
4703 $context['response_type'] = 'live';
4704 }
4705 $context['pluggable_data'] = $quote;
4706 break;
4707 case 'quote_deleted':
4708 $context['pluggable_data'] = $quote_id;
4709 break;
4710 case 'transaction_deleted':
4711 $context['pluggable_data'] = $transaction_id;
4712 break;
4713 }
4714
4715 return $context;
4716 }
4717
4718 /**
4719 * Get last data for trigger.
4720 *
4721 * @param array $data data.
4722 * @return array
4723 */
4724 public function search_funnel_kit_automations_triggers_last_data( $data ) {
4725
4726 if ( ! class_exists( 'BWFCRM_Contact' ) || ! class_exists( 'BWFCRM_Lists' ) || ! class_exists( 'BWFCRM_Tag' ) ) {
4727 return [];
4728 }
4729
4730 $context = [];
4731 $context['response_type'] = 'sample';
4732
4733 $contact = [
4734 'contact_id' => '1',
4735 'wpid' => '0',
4736 'uid' => '9e74246335fd81b1c4a9123842c12549',
4737 'email' => 'johndoe@example.com',
4738 'first_name' => 'John',
4739 'last_name' => 'Doe',
4740 'contact_no' => '+1 555-555-5555',
4741 'state' => 'NY',
4742 'country' => 'United States',
4743 'timezone' => 'New York',
4744 'creation_date' => '2023-05-29 15:26:03',
4745 'last_modified' => '2023-05-29 17:08:30',
4746 'source' => '',
4747 'type' => 'Los Angeles',
4748 'status' => '0',
4749 'tags' => '["1"]',
4750 'lists' => '["2","1"]',
4751 ];
4752
4753 $list = [
4754 'list_id' => 1,
4755 'list_name' => 'Sample List',
4756 ];
4757
4758 $tag = [
4759 'tag_id' => 1,
4760 'tag_name' => 'Sample Tag',
4761 ];
4762
4763 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4764
4765 $recent_contacts = \BWFCRM_Contact::get_recent_contacts( 0, 1, [] );
4766 $contact_email = count( $recent_contacts['contacts'] ) > 0 && isset( $recent_contacts['contacts'][0]['email'] ) ? $recent_contacts['contacts'][0]['email'] : '';
4767
4768 $real_contact = false;
4769 if ( ! empty( $contact_email ) ) {
4770 $contact_obj = \BWFCRM_Contact::get_contacts( $contact_email, 0, 1, [], [], OBJECT );
4771
4772 if ( isset( $contact_obj['contacts'][0] ) ) {
4773 $contact = FunnelKitAutomations::get_contact_context( $contact_obj['contacts'][0]->contact );
4774 $real_contact = true;
4775 }
4776 }
4777
4778 if ( 'contact_added_to_list' === $term || 'contact_removed_from_list' === $term ) {
4779 $list_id = (int) ( isset( $data['filter']['list_id']['value'] ) ? $data['filter']['list_id']['value'] : '-1' );
4780
4781 if ( -1 === $list_id ) {
4782 $lists = \BWFCRM_Lists::get_lists( [], '', 0, 1 );
4783 $list_id = count( $lists ) > 0 ? $lists[0]['ID'] : -1;
4784 }
4785
4786
4787 if ( -1 !== $list_id ) {
4788 $list = FunnelKitAutomations::get_list_context( $list_id );
4789 $context['response_type'] = $real_contact ? 'live' : 'sample';
4790 }
4791
4792 $context['pluggable_data'] = array_merge( $list, $contact );
4793 } else {
4794 $tag_id = (int) ( isset( $data['filter']['tag_id']['value'] ) ? $data['filter']['tag_id']['value'] : '-1' );
4795
4796 if ( -1 === $tag_id ) {
4797 $tags = \BWFCRM_Tag::get_tags( [], '', 0, 1 );
4798 $tag_id = count( $tags ) > 0 ? $tags[0]['ID'] : -1;
4799 }
4800
4801
4802 if ( -1 !== $tag_id ) {
4803 $tag = FunnelKitAutomations::get_tag_context( $tag_id );
4804 $context['response_type'] = $real_contact ? 'live' : 'sample';
4805 }
4806
4807 $context['pluggable_data'] = array_merge( $tag, $contact );
4808 }
4809
4810 return $context;
4811 }
4812
4813 /**
4814 * Get last data for trigger.
4815 *
4816 * @param array $data data.
4817 * @return array
4818 */
4819 public function search_edd_triggers_last_data( $data ) {
4820 global $wpdb;
4821 $context = [];
4822 $context['response_type'] = 'sample';
4823 $context['pluggable_data'] = [];
4824 $order_data = [
4825 'order_id' => 187,
4826 'customer_email' => 'john_doe@gmail.com',
4827 'customer_id' => 2,
4828 'user_id' => 1,
4829 'customer_first_name' => 'Sure',
4830 'customer_last_name' => 'Dev',
4831 'ordered_items' => 'Price with license — Price one',
4832 'currency' => 'USD',
4833 'status' => 'complete',
4834 'discount_codes' => '',
4835 'order_discounts' => 0.00,
4836 'order_subtotal' => 12.00,
4837 'order_tax' => 0.00,
4838 'order_total' => 12.00,
4839 'payment_method' => 'manual',
4840 'purchase_key' => 'd797b9576a3895e7424bae2417ed87df',
4841 'ordered_items_ids' => 17250,
4842 'download_id' => 17250,
4843 'license_key' => 'f7736093411cfaed18b56ec60227117b',
4844 'license_key_expire_date' => '1697524076',
4845 'license_key_status' => 'inactive',
4846 ];
4847
4848 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4849 $download_id = isset( $data['filter']['download_id']['value'] ) ? $data['filter']['download_id']['value'] : 0;
4850 if ( 'order_created' === $term || 'order_one_product' === $term ) {
4851 $order_data['purchase_key'] = '06d3b7d923ca922dc889354f9bc33fbb';
4852
4853 $args = [
4854 'number' => 1,
4855 'status' => [ 'complete', 'refunded', 'partially_refunded', 'renewal' ],
4856 ];
4857 if ( $download_id > 0 ) {
4858 $args['download'] = $download_id;
4859 }
4860 $payments = edd_get_payments( $args );
4861 if ( count( $payments ) > 0 ) {
4862 $order_data = EDD::get_product_purchase_context( $payments[0], $term, $download_id );
4863
4864 $context['response_type'] = 'live';
4865 } else {
4866 if ( 'order_one_product' === $term ) {
4867 $order_data['price_id'] = 1;
4868 }
4869 }
4870 } elseif ( 'stripe_payment_refunded' === $term ) {
4871 $args = [
4872 'number' => 1,
4873 'status' => 'complete',
4874 'type' => 'refund',
4875 ];
4876 $payments = edd_get_payments( $args );
4877
4878 if ( count( $payments ) > 0 ) {
4879 $order_data = EDD::get_purchase_refund_context( $payments[0] );
4880 $context['response_type'] = 'live';
4881 }
4882 } else {
4883 $status = isset( $data['post_type'] ) ? $data['post_type'] : '';
4884 if ( ! empty( $status ) ) {
4885 if ( $download_id > 0 ) {
4886 $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 ) );
4887 } else {
4888 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE status= %s ORDER BY id DESC", $status ) );
4889 }
4890 } else {
4891 if ( $download_id > 0 ) {
4892 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE download_id=%d ORDER BY id DESC", $download_id ) );
4893 } else {
4894 $licesnses = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}edd_licenses ORDER BY id DESC" );
4895 }
4896 }
4897 if ( ! empty( $licesnses ) ) {
4898 $order_data = EDD::edd_get_license_data( $licesnses->id, $licesnses->download_id, $licesnses->payment_id );
4899 $context['response_type'] = 'live';
4900 } else {
4901 $order_data = [
4902 'ID' => 1,
4903 'key' => '23232323232',
4904 'customer_email' => 'suretest@example.com',
4905 'customer_name' => 'Sure Test',
4906 'product_id' => 1,
4907 'download_id' => 1,
4908 'product_name' => 'Test',
4909 'activation_limit' => 2,
4910 'activation_count' => 1,
4911 'activated_urls' => 'https://example.com',
4912 'expiration' => '1686297914',
4913 'is_lifetime' => '0',
4914 'status' => $status,
4915 ];
4916
4917 }
4918 }
4919
4920 $context['pluggable_data'] = $order_data;
4921 return $context;
4922 }
4923
4924 /**
4925 * Get last data for trigger.
4926 *
4927 * @param array $data data.
4928 * @return array
4929 */
4930 public function search_presto_player_triggers_last_data( $data ) {
4931 $context = [];
4932 $context['response_type'] = 'sample';
4933
4934 $user_data = WordPress::get_sample_user_context();
4935
4936 $video_data = [
4937 'pp_video' => '1',
4938 'pp_video_percentage' => '100',
4939 ];
4940
4941 $video_data['video'] = [
4942 'id' => '1',
4943 'title' => 'SureTriggers Is Here 🎉 The Easiest Automation Builder WordPress Websites & Apps',
4944 'type' => 'youtube',
4945 'external_id' => '-cYbNYgylLs',
4946 'attachment_id' => '0',
4947 'post_id' => '127',
4948 'src' => 'https://www.youtube.com/watch?v=-cYbNYgylLs',
4949 'created_by' => '1',
4950 'created_at' => '2022-11-10 00:28:25',
4951 'updated_at' => '2022-11-10 00:34:40',
4952 'deleted_at' => '',
4953 ];
4954
4955 $video_data['media']['tag'] = [
4956 'Demo',
4957 'Demo1',
4958 ];
4959
4960 $mediahub_data = [
4961 'post_id' => '1',
4962 'activity_id' => '2',
4963 'post_author' => '1',
4964 'post_content' => 'New sample post...!',
4965 'post_title' => 'Sample Post',
4966 'post_excerpt' => 'sample',
4967 'post_status' => 'publish',
4968 'post_type' => 'pp_video_block',
4969 ];
4970
4971 $videos = ( new Video() )->all();
4972
4973 if ( count( $videos ) > 0 ) {
4974 if ( ! empty( $data['filter'] ) ) {
4975 $video_id = $data['filter']['pp_video']['value'];
4976 if ( -1 === $video_id ) {
4977 global $wpdb;
4978 $args = [
4979 'numberposts' => 1,
4980 'orderby' => 'rand',
4981 'order' => 'ASC',
4982 'post_type' => 'pp_video_block',
4983 ];
4984 $posts = get_posts( $args );
4985 $presto_post_video_id = $posts[0]->ID;
4986 $presto_video_id = $wpdb->get_var( $wpdb->prepare( 'SELECT * FROM wp_presto_player_videos WHERE post_id = %d', $presto_post_video_id ) );
4987 $video_id = $presto_video_id;
4988 }
4989 $pp_videos = ( new Video( $video_id ) )->toArray();
4990 if ( ! empty( $pp_videos ) ) {
4991 $video_data = [];
4992 $video_data['video'] = $pp_videos;
4993 if ( is_array( $video_data ) && array_key_exists( 'post_id', $video_data['video'] ) ) {
4994 $mediahub_data = WordPress::get_post_context( (int) $video_data['video']['post_id'] );
4995 $media_tags = get_the_terms( (int) $video_data['video']['post_id'], 'pp_video_tag' );
4996 if ( ! empty( $media_tags ) && is_array( $media_tags ) && isset( $media_tags[0] ) ) {
4997 $tag_name = [];
4998 foreach ( $media_tags as $tag ) {
4999 $tag_name[] = $tag->name;
5000 }
5001 $video_data['media']['tag'] = $tag_name;
5002 }
5003 }
5004 $video_data['pp_video'] = $video_id;
5005 $video_data['pp_video_percentage'] = isset( $data['filter']['pp_video_percentage']['value'] ) ? $data['filter']['pp_video_percentage']['value'] : '100';
5006 $user_data = WordPress::get_user_context( (int) $video_data['video']['created_by'] );
5007
5008 $context['response_type'] = 'live';
5009 }
5010 }
5011 }
5012
5013 $context['pluggable_data'] = array_merge( $user_data, $video_data, $mediahub_data );
5014
5015 return $context;
5016 }
5017
5018 /**
5019 * Get last data for trigger.
5020 *
5021 * @param array $data data.
5022 * @return array
5023 */
5024 public function search_member_press_triggers_last_data( $data ) {
5025 global $wpdb;
5026
5027 $term = $data['search_term'] ? $data['search_term'] : '';
5028
5029 $context = [];
5030 $context['response_type'] = 'sample';
5031
5032 $user_data = WordPress::get_sample_user_context();
5033
5034 $membership_data = [
5035 'membership_id' => '190',
5036 'membership_title' => 'Sample Membership',
5037 'amount' => '12.00',
5038 'total' => '12.00',
5039 'tax_amount' => '0.00',
5040 'tax_rate' => '0.00',
5041 'trans_num' => 't_63a03f3334f44',
5042 'status' => 'complete',
5043 'subscription_id' => '0',
5044 'membership_url' => site_url() . '/register/premium/',
5045 'membership_featured_image_id' => '521',
5046 'membership_featured_image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
5047 ];
5048
5049 $membership_id = (int) ( isset( $data['filter']['membership_id']['value'] ) ? $data['filter']['membership_id']['value'] : '-1' );
5050
5051 if ( in_array( $term, [ 'mepr-event-transaction-expired', 'mepr_subscription_transition_status', 'mepr-event-transaction-paused' ] ) ) {
5052 $status = 'cancelled';
5053 if ( 'mepr-event-transaction-paused' === $term ) {
5054 $status = 'suspended';
5055 }
5056 if ( $membership_id > 0 ) {
5057 $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 ) );
5058 } else {
5059 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_subscriptions WHERE status=%s ORDER BY id DESC LIMIT 1", $status ) );
5060
5061 }
5062 if ( ! empty( $subscription ) ) {
5063
5064 $membership_data = MemberPress::get_subscription_context( $subscription );
5065 $user_data = WordPress::get_user_context( $subscription->user_id );
5066
5067
5068 $context['response_type'] = 'live';
5069 }
5070 } elseif ( 'mepr-coupon-code-redeemed' === $term ) {
5071
5072 $coupon_id = (int) ( isset( $data['filter']['coupon_id']['value'] ) ? $data['filter']['coupon_id']['value'] : '-1' );
5073
5074 if ( $coupon_id > 0 ) {
5075 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE coupon_id= %s ORDER BY id DESC LIMIT 1", $coupon_id ) );
5076 } else {
5077 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE coupon_id!= %s ORDER BY id DESC LIMIT 1", '0' ) );
5078 }
5079
5080
5081 if ( ! empty( $subscription ) ) {
5082
5083 $membership_data = MemberPress::get_membership_context( $subscription );
5084 $user_data = WordPress::get_user_context( $subscription->user_id );
5085 $membership_data['coupon_id'] = $subscription->coupon_id;
5086 $membership_data['coupon'] = get_post( $subscription->coupon_id );
5087 $context['response_type'] = 'live';
5088 }
5089 } else {
5090
5091 if ( $membership_id > 0 ) {
5092
5093 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE product_id= %s ORDER BY id DESC LIMIT 1", $membership_id ) );
5094 } else {
5095 $subscription = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mepr_transactions ORDER BY id DESC LIMIT 1" );
5096 }
5097
5098
5099 if ( ! empty( $subscription ) ) {
5100
5101 $membership_data = MemberPress::get_membership_context( $subscription );
5102 $user_data = WordPress::get_user_context( $subscription->user_id );
5103
5104
5105 $context['response_type'] = 'live';
5106 }
5107 }
5108
5109
5110 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5111 return $context;
5112 }
5113
5114 /**
5115 * Get last data for trigger.
5116 *
5117 * @param array $data data.
5118 * @return array
5119 */
5120 public function search_wishlist_member_triggers_last_data( $data ) {
5121 global $wpdb;
5122
5123 $context = [];
5124 $context['response_type'] = 'sample';
5125
5126 $user_data = WordPress::get_sample_user_context();
5127
5128 $membership_data = [
5129 'membership_level_id' => '1',
5130 'membership_level_name' => 'Sample Membership Level',
5131 ];
5132
5133 $membership_level_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
5134
5135 if ( $membership_level_id > 0 ) {
5136 $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 ) );
5137 } else {
5138 $membership = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}wlm_userlevels ORDER BY id DESC LIMIT 1" );
5139 }
5140 if ( ! empty( $membership ) ) {
5141 $membership_data = WishlistMember::get_membership_detail_context( (int) $membership->level_id, (int) $membership->user_id );
5142 $user_data = WordPress::get_user_context( $membership->user_id );
5143
5144 $context['response_type'] = 'live';
5145 }
5146
5147 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5148
5149 return $context;
5150 }
5151
5152 /**
5153 * Get last data for trigger.
5154 *
5155 * @param array $data data.
5156 * @return array
5157 */
5158 public function search_peepso_triggers_last_data( $data ) {
5159 global $wpdb;
5160
5161 $context = [];
5162 $context['response_type'] = 'sample';
5163
5164 $user_data = WordPress::get_sample_user_context();
5165
5166 $post_data = [
5167 'post_id' => '1',
5168 'activity_id' => '2',
5169 'post_author' => '1',
5170 'post_content' => 'New sample post...!',
5171 'post_title' => 'Sample Post',
5172 'post_excerpt' => 'sample',
5173 'post_status' => 'publish',
5174 'post_type' => 'peepso-post',
5175 ];
5176
5177 $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" );
5178
5179 if ( ! empty( $post ) ) {
5180 $post_data = PeepSo::get_pp_activity_context( (int) $post->act_external_id, (int) $post->act_id );
5181 $user_data = WordPress::get_user_context( $post->act_owner_id );
5182
5183 $context['response_type'] = 'live';
5184 }
5185
5186 $context['pluggable_data'] = array_merge( $user_data, $post_data );
5187
5188 return $context;
5189 }
5190
5191 /**
5192 * Get last data for Peepso User triggers.
5193 *
5194 * @param array $data data.
5195 * @return array
5196 */
5197 public function search_peepso_user_triggers_last_data( $data ) {
5198 global $wpdb;
5199
5200 $context = [];
5201 $context['response_type'] = 'sample';
5202 $term = $data['search_term'];
5203
5204 if ( ! class_exists( 'PeepSoUser' ) ) {
5205 return [];
5206 }
5207
5208 if ( 'user_follows_member' === $term || 'user_gains_follower' === $term ) {
5209 $member_id = $data['filter']['follow_user_id']['value'];
5210 if ( -1 === $member_id ) {
5211 $followers = $wpdb->get_results(
5212 $wpdb->prepare(
5213 "SELECT * FROM
5214 {$wpdb->prefix}peepso_user_followers WHERE uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5215 1
5216 )
5217 );
5218 } else {
5219 $followers = $wpdb->get_results(
5220 $wpdb->prepare(
5221 "SELECT * FROM
5222 {$wpdb->prefix}peepso_user_followers WHERE uf_passive_user_id = %d AND
5223 uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5224 $member_id,
5225 1
5226 )
5227 );
5228 }
5229 } elseif ( 'user_unfollows_member' === $term || 'user_loses_follower' === $term ) {
5230 $member_id = $data['filter']['follow_user_id']['value'];
5231 if ( -1 === $member_id ) {
5232 $followers = $wpdb->get_results(
5233 $wpdb->prepare(
5234 "SELECT * FROM
5235 {$wpdb->prefix}peepso_user_followers WHERE uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5236 0
5237 )
5238 );
5239 } else {
5240 $followers = $wpdb->get_results(
5241 $wpdb->prepare(
5242 "SELECT * FROM
5243 {$wpdb->prefix}peepso_user_followers WHERE uf_passive_user_id = %d AND
5244 uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5245 $member_id,
5246 0
5247 )
5248 );
5249 }
5250 } elseif ( 'user_updates_avatar' === $term ) {
5251 $followers = $wpdb->get_results(
5252 $wpdb->prepare(
5253 "SELECT * FROM
5254 {$wpdb->prefix}peepso_users WHERE usr_avatar_custom = %d AND usr_role = %s
5255 ORDER BY usr_last_activity DESC LIMIT 1",
5256 1,
5257 'member'
5258 )
5259 );
5260 } elseif ( 'user_updates_specific_profile_field' === $term ) {
5261 $field_id = $data['filter']['user_profile_field_id']['value'];
5262 $followers = $wpdb->get_results(
5263 $wpdb->prepare(
5264 "SELECT * FROM
5265 {$wpdb->prefix}peepso_users WHERE usr_role = %s
5266 ORDER BY usr_last_activity DESC LIMIT 1",
5267 'member'
5268 )
5269 );
5270 }
5271 if ( 'user_updates_avatar' === $term ) {
5272 if ( ! empty( $followers ) ) {
5273 $context_data['user'] = WordPress::get_user_context( $followers[0]->usr_id );
5274 $context['response_type'] = 'live';
5275 } else {
5276 $context_data['user'] = WordPress::get_sample_user_context();
5277 }
5278 } elseif ( 'user_updates_specific_profile_field' === $term ) {
5279 if ( ! empty( $followers ) ) {
5280 $user = PeepSoUser::get_instance( $followers[0]->usr_id );
5281 $user->profile_fields->load_fields();
5282 $user_fields = $user->profile_fields->get_fields();
5283 foreach ( $user_fields as $key => $value ) {
5284 $val = get_user_meta( $followers[0]->usr_id, $value->key, true );
5285 if ( '' != $val ) {
5286 $context_data[ $value->title ] = $val;
5287 }
5288 }
5289 $curruser = get_userdata( $followers[0]->usr_id );
5290 $context_data['user_id'] = $followers[0]->usr_id;
5291 $context_data['user_email'] = $user->get_email();
5292 $context_data['avatar_url'] = $user->get_avatar();
5293 $context_data['profile_url'] = $user->get_profileurl();
5294 $context_data['about_me'] = get_user_meta( $followers[0]->usr_id, 'description', true );
5295 if ( $curruser instanceof \WP_User ) {
5296 $context_data['website'] = $curruser->user_url;
5297 }
5298 $context_data['role'] = $user->get_user_role();
5299 $context['response_type'] = 'live';
5300 } else {
5301 $context_data['user'] = WordPress::get_sample_user_context();
5302 }
5303 } else {
5304 if ( ! empty( $followers ) ) {
5305 $context_data['follower_user'] = WordPress::get_user_context( $followers[0]->uf_active_user_id );
5306 $context_data['following_user'] = WordPress::get_user_context( $followers[0]->uf_passive_user_id );
5307 $context['response_type'] = 'live';
5308 } else {
5309 $context_data['follower_user'] = WordPress::get_sample_user_context();
5310 $context_data['following_user'] = WordPress::get_sample_user_context();
5311 }
5312 }
5313
5314 $context['pluggable_data'] = $context_data;
5315
5316 return $context;
5317 }
5318
5319 /**
5320 * Search Peepso User Profile Fields list.
5321 *
5322 * @param array $data data.
5323 * @return array
5324 */
5325 public function search_peepso_profile_field_list( $data ) {
5326 $options = [];
5327 if ( ! class_exists( 'PeepSoUser' ) ) {
5328 return [];
5329 }
5330 $options = [
5331 [
5332 'label' => 'Allow others to "like" my profile',
5333 'value' => 'peepso_is_profile_likable',
5334 ],
5335 [
5336 'label' => 'Hide my birthday year',
5337 'value' => 'peepso_hide_birthday_year',
5338 ],
5339 [
5340 'label' => 'Who can see my profile',
5341 'value' => 'usr_profile_acc',
5342 ],
5343 [
5344 'label' => 'Who can post on my profile',
5345 'value' => 'peepso_profile_post_acc',
5346 ],
5347 [
5348 'label' => "Don't show my online status",
5349 'value' => 'peepso_hide_online_status',
5350 ],
5351 [
5352 'label' => 'My timezone',
5353 'value' => 'peepso_gmt_offset',
5354 ],
5355 ];
5356 $peepsouser = PeepSoUser::get_instance( 0 );
5357 $peepsouser->profile_fields->load_fields();
5358 $fields = $peepsouser->profile_fields->get_fields();
5359 foreach ( $fields as $field ) {
5360 if ( 1 == $field->published ) {
5361 $options[] = [
5362 'label' => $field->title,
5363 'value' => $field->id,
5364 ];
5365 }
5366 }
5367 return [
5368 'options' => $options,
5369 'hasMore' => false,
5370 ];
5371 }
5372
5373 /**
5374 * Search Peepso Roles list.
5375 *
5376 * @param array $data data.
5377 * @return array
5378 */
5379 public function search_peepso_role_list( $data ) {
5380 $options = [
5381 [
5382 'label' => 'Community Member',
5383 'value' => 'member',
5384 ],
5385 [
5386 'label' => 'Community Moderator',
5387 'value' => 'moderator',
5388 ],
5389 [
5390 'label' => 'Community Administrator',
5391 'value' => 'admin',
5392 ],
5393 [
5394 'label' => 'Banned',
5395 'value' => 'ban',
5396 ],
5397 [
5398 'label' => 'Pending user email verification',
5399 'value' => 'register',
5400 ],
5401 [
5402 'label' => 'Pending admin approval',
5403 'value' => 'verified',
5404 ],
5405 ];
5406 return [
5407 'options' => $options,
5408 'hasMore' => false,
5409 ];
5410 }
5411
5412 /**
5413 * Search Peepso Users list.
5414 *
5415 * @param array $data data.
5416 * @return array
5417 */
5418 public function search_peepso_follow_user_list( $data ) {
5419 $options = [];
5420 global $wpdb;
5421 $users = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}peepso_users", ARRAY_A );
5422 if ( count( $users ) > 0 ) {
5423 foreach ( $users as $user ) {
5424 $user_by_id = get_user_by( 'id', $user['usr_id'] );
5425 if ( $user_by_id instanceof \WP_User ) {
5426 $options[] = [
5427 'label' => sprintf( '%s %s [%s]', $user_by_id->last_name, $user_by_id->first_name, $user_by_id->user_email ),
5428 'value' => $user['usr_id'],
5429 ];
5430 } else {
5431 $options[] = [
5432 'label' => '#' . $user['usr_id'],
5433 'value' => $user['usr_id'],
5434 ];
5435 }
5436 }
5437 }
5438 return [
5439 'options' => $options,
5440 'hasMore' => false,
5441 ];
5442 }
5443
5444 /**
5445 * Get last data for trigger
5446 *
5447 * @param array $data data.
5448 * @return array
5449 */
5450 public function search_restrict_content_pro_triggers_last_data( $data ) {
5451 $context = [];
5452 $context['response_type'] = 'sample';
5453
5454 $user_data = WordPress::get_sample_user_context();
5455
5456 $membership_data = [
5457 'membership_level_id' => '190',
5458 'membership_level' => 'Sample Membership',
5459 'membership_initial_payment' => '0.00',
5460 'membership_recurring_payment' => '0.00',
5461 'membership_expiry_date' => 'January 22, 2023',
5462 ];
5463
5464 $customer_id = (int) ( isset( $data['filter']['membership_customer_id']['value'] ) ? $data['filter']['membership_customer_id']['value'] : '-1' );
5465 $membership_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
5466
5467 $args = [];
5468 if ( 'membership_purchased' === $data['search_term'] ) {
5469 $args = [
5470 'status' => 'active',
5471 'number' => 1,
5472 'orderby' => 'id',
5473 ];
5474 } elseif ( 'membership_cancelled' === $data['search_term'] ) {
5475 $args = [
5476 'status' => 'cancelled',
5477 'number' => 1,
5478 'orderby' => 'id',
5479 ];
5480 } elseif ( 'membership_expired' === $data['search_term'] ) {
5481 $args = [
5482 'status' => 'expired',
5483 'number' => 1,
5484 'orderby' => 'id',
5485 ];
5486 }
5487
5488 if ( 'membership_expired' === $data['search_term'] && -1 !== $customer_id ) {
5489 $args['customer_id'] = $customer_id;
5490 }
5491
5492 if ( -1 !== $membership_id ) {
5493 $args['object_id'] = $membership_id;
5494 }
5495
5496 $memberships = rcp_get_memberships( $args );
5497 if ( count( $memberships ) > 0 ) {
5498 $membership_data = RestrictContent::get_rcp_membership_detail_context( $memberships[0] );
5499 $user_data = WordPress::get_user_context( $memberships[0]->get_user_id() );
5500
5501 $context['response_type'] = 'live';
5502 }
5503
5504 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5505
5506 return $context;
5507 }
5508
5509 /**
5510 * Get last data for trigger
5511 *
5512 * @param array $data data.
5513 * @return array
5514 */
5515 public function search_events_calendar_triggers_last_data( $data ) {
5516 $context = [];
5517 $context['response_type'] = 'sample';
5518
5519 $event_data = [
5520 'event_id' => 1,
5521 'event' => [
5522 'ID' => 58,
5523 'post_author' => 1,
5524 'post_date' => '2023-01-19 09:27:58',
5525 'post_date_gmt' => '2023-01-19 09:27:58',
5526 'post_content' => '',
5527 'post_title' => 'New event',
5528 'post_excerpt' => '',
5529 'post_status' => 'publish',
5530 'comment_status' => 'open',
5531 'ping_status' => 'closed',
5532 'post_password' => '',
5533 'post_name' => 'new-event',
5534 'to_ping' => '',
5535 'pinged' => '',
5536 'post_modified' => '2023-01-19 09:44:25',
5537 'post_modified_gmt' => '2023-01-19 09:44:25',
5538 'post_content_filtered' => '',
5539 'post_parent' => 0,
5540 'guid' => 'http://connector.com/?post_type=tribe_events&#038;p=58',
5541 'menu_order' => -1,
5542 'post_type' => 'tribe_events',
5543 'post_mime_type' => '',
5544 'comment_count' => 0,
5545 'filter' => 'raw',
5546 ],
5547 'attendies' => [
5548 'order_id' => 68,
5549 'purchaser_name' => 'John Doe',
5550 'purchaser_email' => 'john@test.com',
5551 'provider' => 'Tribe__Tickets__RSVP',
5552 'provider_slug' => 'rsvp',
5553 'purchase_time' => '2023-01-19 09:48:43',
5554 'optout' => 1,
5555 'ticket' => 'Prime',
5556 'attendee_id' => 68,
5557 'security' => '2cefc3b53e',
5558 'product_id' => 65,
5559 'check_in' => '',
5560 'order_status' => 'yes',
5561 'order_status_label' => 'Going',
5562 'user_id' => 1,
5563 'ticket_sent' => 1,
5564 'event_id' => 58,
5565 'ticket_name' => 'Prime',
5566 'holder_name' => 'John Doe',
5567 'holder_email' => 'john@test.com',
5568 'ticket_id' => 68,
5569 'qr_ticket_id' => 68,
5570 'security_code' => '2cefc3b53e',
5571 'attendee_meta' => '',
5572 'is_subscribed' => '',
5573 'is_purchaser' => 1,
5574 'ticket_exists' => 1,
5575 ],
5576 ];
5577
5578 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
5579 $term = $data['search_term'];
5580
5581 if ( 'event_register' === $term || 'attendee_registered_event' === $term ) {
5582 if ( -1 === $event_id ) {
5583 $args = [
5584 'numberposts' => 1,
5585 'orderby' => 'rand',
5586 'post_type' => 'tribe_events',
5587 ];
5588 $posts = get_posts( $args );
5589 $event_id = $posts[0]->ID;
5590 }
5591 $args = [
5592 'post_type' => 'tribe_rsvp_attendees',
5593 'orderby' => 'ID',
5594 'order' => 'DESC',
5595 'post_status' => 'publish',
5596 'numberposts' => 1,
5597 ];
5598
5599 if ( -1 !== $event_id ) {
5600 $args['meta_query'] = [
5601 [
5602 'key' => '_tribe_rsvp_event',
5603 'value' => $event_id,
5604 ],
5605 ];
5606 }
5607
5608 $attendees = get_posts( $args );
5609
5610 if ( count( $attendees ) > 0 ) {
5611 $attendee = $attendees[0];
5612 $attendee_id = $attendee->ID;
5613
5614 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
5615 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
5616
5617 $event_context = TheEventCalendar::get_event_context( $product_id, $order_id );
5618
5619 if ( ! empty( $event_context ) ) {
5620 $event_data = $event_context;
5621 $context['response_type'] = 'live';
5622 }
5623 } else {
5624 $args = [
5625 'post_type' => 'tec_tc_attendee',
5626 'orderby' => 'ID',
5627 'order' => 'DESC',
5628 'post_status' => 'publish',
5629 'numberposts' => 1,
5630 ];
5631 if ( -1 !== $event_id ) {
5632 $args['meta_query'] = [
5633 [
5634 'key' => '_tec_tickets_commerce_event',
5635 'value' => $event_id,
5636 ],
5637 ];
5638 }
5639 $attendees = get_posts( $args );
5640 $attendee = $attendees[0];
5641 $attendee_id = $attendee->ID;
5642 $product_id = get_post_meta( $attendee_id, '_tec_tickets_commerce_ticket', true );
5643 $order_id = $attendee_id;
5644 $event_context = TheEventCalendar::get_event_context( $product_id, $order_id );
5645 if ( ! empty( $event_context ) ) {
5646 $event_data = $event_context;
5647 $context['response_type'] = 'live';
5648 }
5649 }
5650 } elseif ( 'event_attends' === $term ) {
5651 if ( -1 == $event_id ) {
5652 $args = [
5653 'numberposts' => 1,
5654 'orderby' => 'rand',
5655 'post_type' => 'tribe_events',
5656 ];
5657 $posts = get_posts( $args );
5658 $event_id = $posts[0]->ID;
5659 }
5660 $args = [
5661 'post_type' => 'tribe_rsvp_attendees',
5662 'orderby' => 'ID',
5663 'order' => 'DESC',
5664 'post_status' => 'publish',
5665 'numberposts' => 1,
5666 'meta_query' => [
5667 'relation' => 'AND',
5668 [
5669 'key' => '_tribe_rsvp_checkedin',
5670 'value' => 1,
5671 'compare' => '=',
5672 ],
5673 [
5674 'key' => '_tribe_rsvp_event',
5675 'value' => $event_id,
5676 'compare' => '=',
5677 ],
5678 ],
5679 ];
5680 $attendees = get_posts( $args );
5681 if ( ! function_exists( 'tribe_tickets_get_attendees' ) ) {
5682 return [];
5683 }
5684 if ( ! empty( $attendees ) ) {
5685 $attendee = $attendees[0];
5686 $attendee_id = $attendee->ID;
5687 /**
5688 *
5689 * Ignore line
5690 *
5691 * @phpstan-ignore-next-line
5692 */
5693 $attendee_details = tribe_tickets_get_attendees( $attendee_id, 'rsvp_order' );
5694 foreach ( $attendee_details as $detail ) {
5695 if ( (int) $detail['attendee_id'] !== (int) $attendee_id ) {
5696 continue;
5697 }
5698 $attendee = $detail;
5699 }
5700 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
5701 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
5702 $event_context = TheEventCalendar::get_event_context( $product_id, $order_id );
5703 if ( ! empty( $event_context ) ) {
5704 $event_data = array_merge( $attendee, $event_context );
5705 $context['response_type'] = 'live';
5706 }
5707 } else {
5708 $order = [
5709 'order_id' => 7962,
5710 'purchaser_name' => 'bella4 bella4',
5711 'purchaser_email' => 'bella4@yopmail.com',
5712 'provider' => 'Tribe__Tickets__RSVP',
5713 'provider_slug' => 'rsvp',
5714 'purchase_time' => '2024 - 03 - 04 07:26:41',
5715 'optout' => 1,
5716 'ticket' => 'test test',
5717 'attendee_id' => 7962,
5718 'security' => 'eb3a2d7bc4',
5719 'product_id' => 7959,
5720 'check_in' => 1,
5721 'order_status' => 'yes',
5722 'order_status_label' => 'Going',
5723 'user_id' => 35,
5724 'ticket_sent' => 1,
5725 'event_id' => 7956,
5726 'ticket_name' => 'test test',
5727 'holder_name' => 'bella4 bella4',
5728 'holder_email' => 'bella4@yopmail.com',
5729 'ticket_id' => 7962,
5730 'qr_ticket_id' => 7962,
5731 'security_code' => 'eb3a2d7bc4',
5732 'is_purchaser' => 1,
5733 'ticket_exists' => 1,
5734 ];
5735 $event_data = array_merge( $order, $event_data );
5736 }
5737 } elseif ( 'event_attendee_wc_register' === $term ) {
5738 if ( ! class_exists( 'Tribe__Tickets__Tickets' ) ) {
5739 return [];
5740 }
5741 if ( -1 === $event_id ) {
5742 $args = [
5743 'numberposts' => 1,
5744 'orderby' => 'rand',
5745 'post_type' => 'tribe_events',
5746 ];
5747 $posts = get_posts( $args );
5748 $event_id = $posts[0]->ID;
5749 }
5750 $event_ticket_id = (int) ( isset( $data['filter']['event_ticket_id']['value'] ) ? $data['filter']['event_ticket_id']['value'] : '-1' );
5751 if ( -1 === $event_ticket_id ) {
5752 $tickets = \Tribe__Tickets__Tickets::get_all_event_tickets( $event_id );
5753 $random_key = array_rand( $tickets );
5754 $random_value = $tickets[ $random_key ];
5755 $event_ticket_id = $random_value;
5756 }
5757 $args = [
5758 'post_type' => 'tribe_wooticket',
5759 'orderby' => 'ID',
5760 'order' => 'DESC',
5761 'post_status' => 'publish',
5762 'meta_query' => [
5763 'relation' => 'AND',
5764 [
5765 'key' => '_tribe_wooticket_event',
5766 'value' => $event_id,
5767 'compare' => '=',
5768 ],
5769 [
5770 'key' => '_tribe_wooticket_product',
5771 'value' => $event_ticket_id,
5772 'compare' => '=',
5773 ],
5774 ],
5775 ];
5776
5777 $woo_attendees = get_posts( $args );
5778 if ( ! empty( $woo_attendees ) ) {
5779 $attendee = $woo_attendees[0];
5780 $attendee_id = $attendee->ID;
5781 /**
5782 *
5783 * Ignore line
5784 *
5785 * @phpstan-ignore-next-line
5786 */
5787 $attendee_details = tribe_tickets_get_attendees( $attendee_id, 'rsvp_order' );
5788 foreach ( $attendee_details as $detail ) {
5789 if ( (int) $detail['attendee_id'] !== (int) $attendee_id ) {
5790 continue;
5791 }
5792 $attendee = $detail;
5793 }
5794 $order_id = get_post_meta( $attendee_id, '_tribe_wooticket_order', true );
5795 $product_id = get_post_meta( $attendee_id, '_tribe_wooticket_product', true );
5796 $event_context = TheEventCalendar::get_event_context( $product_id, $order_id );
5797 if ( ! empty( $event_context ) ) {
5798 $event_data = $event_context;
5799 $context['response_type'] = 'live';
5800 }
5801 $context['response_type'] = 'live';
5802 }
5803 }
5804
5805 $context['pluggable_data'] = $event_data;
5806
5807 return $context;
5808 }
5809
5810 /**
5811 * Get last data for trigger
5812 *
5813 * @param array $data data.
5814 * @return array
5815 */
5816 public function search_woo_commerce_triggers_last_data( $data ) {
5817 $context = [];
5818 $context['response_type'] = 'sample';
5819 $context['pluggable_data'] = [];
5820 $user_data = WordPress::get_sample_user_context();
5821
5822 $product_data['product'] = [
5823 'id' => '169',
5824 'name' => 'Sample Product',
5825 'description' => 'This is description of sample product.',
5826 'short_description' => 'This is short description of sample product.',
5827 'image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
5828 'slug' => 'sample-product',
5829 'status' => 'publish',
5830 'type' => 'simple',
5831 'price' => '89',
5832 'featured' => '0',
5833 'sku' => 'hoodie-blue-sm',
5834 'regular_price' => '90',
5835 'sale_price' => '89',
5836 'total_sales' => '21',
5837 'category' => 'Uncategorized',
5838 'tags' => 'sample, new, 2022',
5839 ];
5840
5841 $comment_data = [
5842 'comment_id' => '1',
5843 'comment' => 'This is a sample comment..!',
5844 'comment_author' => 'testsure',
5845 'comment_date' => '2023-06-23 10:10:40',
5846 'comment_author_email' => 'testsure@example.com',
5847 ];
5848
5849 $order_data = [
5850 'order_id' => '500',
5851 'total_order_value' => '45',
5852 'currency' => 'USD',
5853 'shipping_total' => '5',
5854 'order_payment_method' => 'cod',
5855 'billing_firstname' => 'John',
5856 'billing_lastname' => 'Doe',
5857 'billing_company' => 'BSF',
5858 'billing_address_1' => '1004 Beaumont',
5859 'billing_address_2' => '',
5860 'billing_city' => 'Casper',
5861 'billing_state' => 'Wyoming',
5862 'billing_state_name' => 'Wyoming',
5863 'billing_postcode' => '82601',
5864 'billing_country' => 'US',
5865 'billing_country_name' => 'US',
5866 'billing_email' => 'john_doe@gmail.com',
5867 'billing_phone' => '(307) 7626541',
5868 'shipping_firstname' => 'John',
5869 'shipping_lastname' => 'Doe',
5870 'shipping_company' => 'BSF',
5871 'shipping_address_1' => '1004 Beaumont',
5872 'shipping_address_2' => '',
5873 'shipping_city' => 'Casper',
5874 'shipping_state' => 'Wyoming',
5875 'shipping_state_name' => 'Wyoming',
5876 'shipping_postcode' => '82601',
5877 'shipping_country' => 'US',
5878 'shipping_country_name' => 'US',
5879 'coupon_codes' => 'e3mstekq, f24sjakb',
5880 'total_items_in_order' => '1',
5881 'user_id' => '1',
5882 ];
5883
5884 $variation_data = [
5885 'product_variation_id' => '626',
5886 'product_variation' => 'Color: Silver',
5887 ];
5888
5889 $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
5890
5891 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
5892 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
5893
5894 $order_status = ( isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1 );
5895 $from_order_status = ( isset( $data['filter']['from_status']['value'] ) ? $data['filter']['from_status']['value'] : -1 );
5896 $order_note_type = ( isset( $data['filter']['note_type']['value'] ) ? $data['filter']['note_type']['value'] : -1 );
5897
5898 if ( in_array( $term, [ 'product_added_to_cart', 'product_viewed' ], true ) ) {
5899 if ( -1 === $product_id ) {
5900 $args = [
5901 'post_type' => 'product',
5902 'orderby' => 'ID',
5903 'order' => 'DESC',
5904 'post_status' => 'publish',
5905 'numberposts' => 1,
5906 ];
5907 $products = get_posts( $args );
5908
5909 if ( count( $products ) > 0 ) {
5910 $product_id = $products[0]->ID;
5911 }
5912 }
5913
5914 if ( -1 !== $product_id ) {
5915 $post = get_post( $product_id );
5916 $user_data = WordPress::get_user_context( $post->post_author );
5917 $product_data['product_id'] = $product_id;
5918 $product_data['product'] = WooCommerce::get_product_context( $product_id );
5919 $terms = get_the_terms( $product_id, 'product_cat' );
5920 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
5921 $cat_name = [];
5922 foreach ( $terms as $cat ) {
5923 $cat_name[] = $cat->name;
5924 }
5925 $product_data['product']['category'] = implode( ', ', $cat_name );
5926 }
5927 $terms_tags = get_the_terms( $product_id, 'product_tag' );
5928 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
5929 $tag_name = [];
5930 foreach ( $terms_tags as $tag ) {
5931 $tag_name[] = $tag->name;
5932 }
5933 $product_data['product']['tag'] = implode( ', ', $tag_name );
5934 }
5935 unset( $product_data['product']['id'] ); //phpcs:ignore
5936 $context['response_type'] = 'live';
5937 }
5938
5939 if ( 'product_added_to_cart' === $term ) {
5940 $product_data['product_quantity'] = 1;
5941 }
5942
5943 $context['pluggable_data'] = array_merge( $product_data, $user_data );
5944
5945 } elseif ( 'product_reviewed' === $term ) {
5946 $comment_args = [
5947 'number' => 1,
5948 'type' => 'review',
5949 'orderby' => 'comment_ID',
5950 'post_id' => -1 !== $product_id ? $product_id : 0,
5951 ];
5952
5953 $comments = get_comments( $comment_args );
5954
5955 if ( count( $comments ) > 0 ) {
5956 $comment = $comments[0];
5957 $comment_data = [
5958 'comment_id' => $comment->comment_ID,
5959 'comment' => $comment->comment_content,
5960 'comment_author' => $comment->comment_author,
5961 'comment_date' => $comment->comment_date,
5962 'comment_author_email' => $comment->comment_author_email,
5963 ];
5964 $product_data = WooCommerce::get_product_context( $comment->comment_post_ID );
5965 if ( is_object( $comment ) ) {
5966 $terms = get_the_terms( (int) $comment->comment_post_ID, 'product_cat' );
5967 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
5968 $cat_name = [];
5969 foreach ( $terms as $cat ) {
5970 $cat_name[] = $cat->name;
5971 }
5972 $product_data['product']['category'] = implode( ', ', $cat_name );
5973 }
5974 $terms_tags = get_the_terms( (int) $comment->comment_post_ID, 'product_tag' );
5975 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
5976 $tag_name = [];
5977 foreach ( $terms_tags as $tag ) {
5978 $tag_name[] = $tag->name;
5979 }
5980 $product_data['product']['tag'] = implode( ', ', $tag_name );
5981 }
5982 }
5983 $user_data = WordPress::get_user_context( $comment->user_id );
5984 $context['response_type'] = 'live';
5985 }
5986
5987 $context['pluggable_data'] = array_merge( $product_data, $user_data, $comment_data );
5988
5989 } elseif ( 'product_purchased' === $term ) {
5990 $order_id = 0;
5991 $product_data['quantity'] = '1';
5992 if ( -1 !== $product_id ) {
5993 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
5994 if ( count( $order_ids ) > 0 ) {
5995 $order_id = $order_ids[0];
5996 }
5997 } else {
5998 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
5999 if ( count( $orders ) > 0 ) {
6000 $order_id = $orders[0]->get_id();
6001 }
6002 }
6003
6004 if ( 0 !== $order_id ) {
6005 $order = wc_get_order( $order_id );
6006
6007 if ( $order ) {
6008 $user_id = $order->get_customer_id();
6009 $items = $order->get_items();
6010
6011 $product_ids = [];
6012
6013 $iteration = 0;
6014 foreach ( $items as $item ) {
6015 if ( method_exists( $item, 'get_product_id' ) ) {
6016 $item_id = $item->get_product_id();
6017 if ( -1 === $product_id && 0 === $iteration ) {
6018 $product_ids[] = $item_id;
6019 break;
6020 } elseif ( $item_id === $product_id ) {
6021 $product_ids[] = $item_id;
6022 break;
6023 }
6024 }
6025
6026 $iteration++;
6027 }
6028 $order_data = WooCommerce::get_order_context( $order_id );
6029 $user_data = WordPress::get_user_context( $user_id );
6030 $order_data['total_items_in_order'] = count( $product_ids );
6031 $product_data = [];
6032 foreach ( $product_ids as $key => $product_id ) {
6033 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
6034 $terms = get_the_terms( $product_id, 'product_cat' );
6035 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6036 $cat_name = [];
6037 foreach ( $terms as $cat ) {
6038 $cat_name[] = $cat->name;
6039 }
6040 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
6041 }
6042 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6043 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6044 $tag_name = [];
6045 foreach ( $terms_tags as $tag ) {
6046 $tag_name[] = $tag->name;
6047 }
6048 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
6049 }
6050 $product = wc_get_product( $product_id );
6051 /**
6052 *
6053 * Ignore line
6054 *
6055 * @phpstan-ignore-next-line
6056 */
6057 if ( $product->is_downloadable() ) {
6058 /**
6059 *
6060 * Ignore line
6061 *
6062 * @phpstan-ignore-next-line
6063 */
6064 foreach ( $product->get_downloads() as $key_download_id => $download ) {
6065 $download_name = $download->get_name();
6066 $download_link = $download->get_file();
6067 $download_id = $download->get_id();
6068 $download_type = $download->get_file_type();
6069 $download_ext = $download->get_file_extension();
6070 $product_data[ 'product' . $key ]['download'] = [
6071 'download_name' => $download_name,
6072 'download_link' => $download_link,
6073 'download_id' => $download_id,
6074 'download_type' => $download_type,
6075 'download_ext' => $download_ext,
6076 ];
6077 }
6078 }
6079 }
6080 $context['response_type'] = 'live';
6081 }
6082 }
6083
6084 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
6085
6086 } elseif ( 'variable_product_purchased' === $term ) {
6087 $product_variation_id = (int) ( isset( $data['filter']['product_variation_id']['value'] ) ? $data['filter']['product_variation_id']['value'] : -1 );
6088 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
6089
6090 foreach ( $order_ids as $order_id ) {
6091 $order = wc_get_order( $order_id );
6092
6093 if ( $order ) {
6094 $user_id = $order->get_customer_id();
6095 $items = $order->get_items();
6096 $product_variations = [];
6097
6098 $iteration = 0;
6099 foreach ( $items as $item ) {
6100 if ( method_exists( $item, 'get_variation_id' ) ) {
6101 $variation_id = $item->get_variation_id();
6102 if ( -1 === $product_variation_id && 0 === $iteration ) {
6103 $product_variations[] = $variation_id;
6104 break;
6105 } elseif ( $variation_id === $product_variation_id ) {
6106 $product_variations[] = $variation_id;
6107 break;
6108 }
6109 }
6110
6111 $iteration++;
6112 }
6113
6114 if ( count( $product_variations ) > 0 ) {
6115 $product_data = WooCommerce::get_product_context( $product_variation_id );
6116 $order_data = WooCommerce::get_order_context( $order_id );
6117 $user_data = WordPress::get_user_context( $user_id );
6118 $variation_data = [
6119 'product_variation_id' => $product_variations[0],
6120 'product_variation' => get_the_excerpt( $product_variations[0] ),
6121 ];
6122
6123 $context['response_type'] = 'live';
6124 break;
6125 }
6126 }
6127 }
6128
6129 $context['pluggable_data'] = array_merge( $order_data, $user_data, $variation_data );
6130
6131 } elseif ( 'variable_subscription_purchased' === $term ) {
6132 $product_data['quantity'] = '1';
6133 $product_data['product_name'] = 'Sample Product';
6134 $product_data['billing_period'] = '2021-2022';
6135
6136 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
6137
6138 $subscription_order_id = 0;
6139 $order_ids = [];
6140
6141 if ( -1 !== $product_id ) {
6142 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
6143
6144 } else {
6145 $orders = wc_get_orders( [] );
6146 if ( count( $orders ) > 0 ) {
6147 $order_ids[] = $orders[0]->get_id();
6148 }
6149 }
6150
6151 foreach ( $order_ids as $order_id ) {
6152 $query_args = [
6153 'post_type' => 'shop_subscription',
6154 'orderby' => 'ID',
6155 'order' => 'DESC',
6156 'post_status' => 'wc-active',
6157 'posts_per_page' => 1,
6158 'post_parent' => $order_id,
6159 ];
6160 $query_result = new WP_Query( $query_args );
6161 $subscription_orders = $query_result->get_posts();
6162
6163 if ( count( $subscription_orders ) > 0 ) {
6164 $subscription_order_id = $subscription_orders[0]->ID;
6165 break;
6166 }
6167 }
6168
6169 if ( 0 !== $subscription_order_id ) {
6170 $subscription = wcs_get_subscription( $subscription_order_id );
6171 if ( $subscription instanceof WC_Subscription ) {
6172 $last_order_id = $subscription->get_last_order();
6173 if ( ! empty( $last_order_id ) && $last_order_id === $subscription->get_parent_id() ) {
6174 $user_id = wc_get_order( $last_order_id )->get_customer_id();
6175 $items = $subscription->get_items();
6176
6177 foreach ( $items as $item ) {
6178 $product = $item->get_product();
6179 if ( class_exists( '\WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product ) ) {
6180 if ( $product->is_type( [ 'subscription', 'subscription_variation', 'variable-subscription' ] ) ) {
6181
6182 $product_data = WooCommerce::get_variable_subscription_product_context( $item, $last_order_id );
6183 $user_data = WordPress::get_user_context( $user_id );
6184
6185 $context['response_type'] = 'live';
6186 $context['pluggable_data'] = array_merge( $product_data, $user_data );
6187 }
6188 }
6189 }
6190 }
6191 }
6192 }
6193 } elseif ( 'order_created' === $term ) {
6194 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
6195 $order_id = '';
6196 if ( count( $orders ) > 0 ) {
6197 $order_id = $orders[0]->get_id();
6198 $order = wc_get_order( $order_id );
6199 $user_id = $order->get_customer_id();
6200 $order_sample_data = array_merge(
6201 WooCommerce::get_order_context( $order_id ),
6202 WordPress::get_user_context( $user_id )
6203 );
6204 $context['response_type'] = 'live';
6205 }
6206
6207 $context['pluggable_data'] = $order_sample_data;
6208
6209 } elseif ( 'order_status_changed' === $term ) {
6210 if ( -1 == $order_status ) {
6211 $args = [
6212 'numberposts' => 1,
6213 'orderby' => 'date',
6214 'order' => 'DESC',
6215 ];
6216 } else {
6217 $args = [
6218 'status' => [ $order_status ],
6219 'numberposts' => 1,
6220 'orderby' => 'date',
6221 'order' => 'DESC',
6222 ];
6223 }
6224 $orders = wc_get_orders( $args );
6225 $order_id = '';
6226 if ( count( $orders ) > 0 ) {
6227 $order_id = $orders[0]->get_id();
6228 $order = wc_get_order( $order_id );
6229 $user_id = $order->get_customer_id();
6230 $items = $order->get_items();
6231 $product_ids = [];
6232 foreach ( $items as $item ) {
6233 $product_ids[] = $item['product_id'];
6234 }
6235 $product_data = [];
6236 foreach ( $product_ids as $key => $product_id ) {
6237 /**
6238 *
6239 * Ignore line
6240 *
6241 * @phpstan-ignore-next-line
6242 */
6243 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
6244 /**
6245 *
6246 * Ignore line
6247 *
6248 * @phpstan-ignore-next-line
6249 */
6250 $terms = get_the_terms( $product_id, 'product_cat' );
6251 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6252 $cat_name = [];
6253 foreach ( $terms as $cat ) {
6254 $cat_name[] = $cat->name;
6255 }
6256 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
6257 }
6258 /**
6259 *
6260 * Ignore line
6261 *
6262 * @phpstan-ignore-next-line
6263 */
6264 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6265 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6266 $tag_name = [];
6267 foreach ( $terms_tags as $tag ) {
6268 $tag_name[] = $tag->name;
6269 }
6270 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
6271 }
6272 }
6273 $order_sample_data = array_merge(
6274 WooCommerce::get_order_context( $order_id ),
6275 $product_data
6276 );
6277 $order_sample_data['user'] = WordPress::get_user_context( $user_id );
6278 $order_sample_data['to_status'] = $order_status;
6279 $order_sample_data['from_status'] = $from_order_status;
6280 $context['response_type'] = 'live';
6281 }
6282
6283 $order_sample_data['to_status'] = $order_status;
6284 $order_sample_data['from_status'] = $from_order_status;
6285 $context['pluggable_data'] = $order_sample_data;
6286 } elseif ( 'order_note_added' === $term ) {
6287 global $wpdb;
6288 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type ='order_note' ORDER BY comment_ID DESC LIMIT 1" );
6289 $order_id = '';
6290 if ( ! empty( $result ) ) {
6291 $order_id = $result[0]->comment_post_ID;
6292 $order = wc_get_order( $order_id );
6293 if ( ! empty( $order ) ) {
6294 $user_id = $order->get_customer_id();
6295 $order_sample_data = array_merge(
6296 WooCommerce::get_order_context( $order_id ),
6297 WordPress::get_user_context( $user_id )
6298 );
6299 if ( -1 == $order_note_type ) {
6300 $args = [
6301 'order_id' => $order_id,
6302 'limit' => 1,
6303 ];
6304 } else {
6305 $args = [
6306 'order_id' => $order_id,
6307 'type' => $order_note_type,
6308 'limit' => 1,
6309 ];
6310 }
6311
6312 $notes = wc_get_order_notes( $args );
6313 if ( ! empty( $notes ) ) {
6314 $order_sample_data['note'] = [
6315 'id' => $notes[0]->id,
6316 'date' => $notes[0]->date_created,
6317 'author' => $notes[0]->added_by,
6318 'content' => $notes[0]->content,
6319 ];
6320 } else {
6321 $order_sample_data['note'] = [
6322 'id' => '1',
6323 'date' => [
6324 'date' => '2023-06-23 10:10:40',
6325 'timezone_type' => 1,
6326 'timezone' => '+00:00',
6327 ],
6328 'author' => 'admin',
6329 'content' => 'new note',
6330 ];
6331 }
6332 $order_sample_data['note_type'] = $order_note_type;
6333 $context['response_type'] = 'live';
6334 }
6335 } else {
6336 $order_sample_data['note'] = [
6337 'id' => '1',
6338 'date' => [
6339 'date' => '2023-06-23 10:10:40',
6340 'timezone_type' => 1,
6341 'timezone' => '+00:00',
6342 ],
6343 'author' => 'admin',
6344 'content' => 'new note',
6345 ];
6346 $order_sample_data['note_type'] = 'customer';
6347 }
6348 $context['pluggable_data'] = $order_sample_data;
6349 } elseif ( 'order_paid' === $term ) {
6350 $args = [
6351 'status' => [ 'completed' ],
6352 'numberposts' => 1,
6353 ];
6354 $orders = wc_get_orders( $args );
6355 $order_id = '';
6356 if ( count( $orders ) > 0 ) {
6357 $order_id = $orders[0]->get_id();
6358 $order = wc_get_order( $order_id );
6359 $user_id = $order->get_customer_id();
6360 $order_sample_data = array_merge(
6361 WooCommerce::get_order_context( $order_id ),
6362 WordPress::get_user_context( $user_id )
6363 );
6364 $context['response_type'] = 'live';
6365 }
6366 $context['pluggable_data'] = $order_sample_data;
6367 } elseif ( 'product_category_purchased' === $term ) {
6368 $product_category_id = (int) ( isset( $data['filter']['product_category_id']['value'] ) ? $data['filter']['product_category_id']['value'] : -1 );
6369 $args = [
6370 'post_status' => 'publish',
6371 'product_category_id' => [ $product_category_id ],
6372 'return' => 'ids',
6373 ];
6374 $products = wc_get_products( $args );
6375 if ( ! empty( $products ) ) {
6376 $order_id = 0;
6377 $product_data['quantity'] = '1';
6378 $orders = wc_get_orders(
6379 [
6380 'status' => 'any',
6381 ]
6382 );
6383 $filtered_orders = [];
6384 if ( ! empty( $orders ) ) {
6385 foreach ( $orders as $order ) {
6386 $order_items = $order->get_items();
6387 foreach ( $order_items as $item ) {
6388 if ( method_exists( $item, 'get_product_id' ) ) {
6389 $product_id = $item->get_product_id();
6390 }
6391 if ( is_array( $products ) && in_array( $product_id, $products ) ) {
6392 $filtered_orders[] = $order;
6393 break;
6394 }
6395 }
6396 }
6397 }
6398 if ( ! empty( $filtered_orders ) ) {
6399 if ( count( $filtered_orders ) > 0 ) {
6400 $order_id = $filtered_orders[0]->get_id();
6401 }
6402 if ( 0 !== $order_id ) {
6403 $order = wc_get_order( $order_id );
6404 if ( $order instanceof WC_Order ) {
6405 $user_id = $order->get_customer_id();
6406 $items = $order->get_items();
6407 $product_ids = [];
6408 $iteration = 0;
6409 foreach ( $items as $item ) {
6410 if ( method_exists( $item, 'get_product_id' ) ) {
6411 $item_id = $item->get_product_id();
6412 if ( -1 === $product_id && 0 === $iteration ) {
6413 $product_ids[] = $item_id;
6414 break;
6415 } elseif ( $item_id === $product_id ) {
6416 $product_ids[] = $item_id;
6417 break;
6418 }
6419 }
6420 $iteration++;
6421 }
6422 $order_data = WooCommerce::get_order_context( $order_id );
6423 $user_data = WordPress::get_user_context( $user_id );
6424 $order_data['total_items_in_order'] = count( $product_ids );
6425 $product_data = [];
6426 $category_ids = [];
6427 foreach ( $product_ids as $key => $product_id ) {
6428 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
6429 $terms = get_the_terms( $product_id, 'product_cat' );
6430 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6431 $cat_name = [];
6432 foreach ( $terms as $cat ) {
6433 $cat_name[] = $cat->name;
6434 $category_ids[] = $cat->term_id;
6435 }
6436 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
6437 }
6438 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6439 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6440 $tag_name = [];
6441 foreach ( $terms_tags as $tag ) {
6442 $tag_name[] = $tag->name;
6443 }
6444 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
6445 }
6446 $product = wc_get_product( $product_id );
6447 /**
6448 *
6449 * Ignore line
6450 *
6451 * @phpstan-ignore-next-line
6452 */
6453 if ( $product->is_downloadable() ) {
6454 /**
6455 *
6456 * Ignore line
6457 *
6458 * @phpstan-ignore-next-line
6459 */
6460 foreach ( $product->get_downloads() as $key_download_id => $download ) {
6461 $download_name = $download->get_name();
6462 $download_link = $download->get_file();
6463 $download_id = $download->get_id();
6464 $download_type = $download->get_file_type();
6465 $download_ext = $download->get_file_extension();
6466 $product_data[ 'product' . $key ]['download'] = [
6467 'download_name' => $download_name,
6468 'download_link' => $download_link,
6469 'download_id' => $download_id,
6470 'download_type' => $download_type,
6471 'download_ext' => $download_ext,
6472 ];
6473 }
6474 }
6475 }
6476 $context['response_type'] = 'live';
6477 }
6478 }
6479 }
6480 }
6481 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
6482 if ( ! empty( $category_ids ) ) {
6483 foreach ( $category_ids as $category_id ) {
6484 $context['pluggable_data']['product_category_id'] = $category_id;
6485 }
6486 } else {
6487 $context['pluggable_data']['product_category_id'] = 1;
6488 }
6489 }
6490
6491 return $context;
6492 }
6493
6494 /**
6495 * Search LMS data.
6496 *
6497 * @param array $data data.
6498 * @return array
6499 */
6500 public function search_lifter_lms_last_data( $data ) {
6501 global $wpdb;
6502 $post_type = $data['post_type'];
6503 $meta_key = '_is_complete';
6504 $trigger = $data['search_term'];
6505 $context = [];
6506 $post_id = -1;
6507
6508 if ( ! class_exists( 'LLMS_Section' ) ) {
6509 return [];
6510 }
6511
6512 if ( 'lifterlms_purchase_course' === $trigger ) {
6513 $product_type = 'course';
6514 $post_id = $data['filter']['course_id']['value'];
6515 } elseif ( 'lifterlms_purchase_membership' === $trigger ) {
6516 $product_type = 'membership';
6517 $post_id = $data['filter']['membership_id']['value'];
6518 } elseif ( 'lifterlms_cancel_membership' === $trigger ) {
6519 $product_type = 'membership';
6520 $post_id = $data['filter']['membership_id']['value'];
6521 } elseif ( 'lifterlms_lesson_completed' === $trigger ) {
6522 $post_id = $data['filter']['lesson']['value'];
6523 } elseif ( 'lifterlms_course_completed' === $trigger || 'lifterlms_course_enrolled' === $trigger || 'lifterlms_course_user_removed' === $trigger ) {
6524 $post_id = $data['filter']['course']['value'];
6525 } elseif ( 'lifterlms_section_completed' === $trigger ) {
6526 $post_id = $data['filter']['section']['value'];
6527 }
6528
6529 $where = 'postmeta.post_id = "' . $post_id . '" AND';
6530
6531 if ( 'llms_order' === $post_type ) {
6532 if ( -1 === $post_id ) {
6533 $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 ) );
6534 } else {
6535 $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 ) );
6536 }
6537 } elseif ( 'lifterlms_course_enrolled' === $trigger ) {
6538 if ( -1 === $post_id ) {
6539 $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 ) );
6540 } else {
6541 $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 ) );
6542 }
6543 } elseif ( 'lifterlms_course_user_removed' === $trigger ) {
6544 if ( -1 === $post_id ) {
6545 $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 ) );
6546 } else {
6547 $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 ) );
6548 }
6549 } elseif ( 'lifterlms_cancel_membership' === $trigger ) {
6550 if ( -1 === $post_id ) {
6551 $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 ) );
6552 } else {
6553 $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 ) );
6554 }
6555 } else {
6556 if ( -1 === $post_id ) {
6557 $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 ) );
6558 } else {
6559 $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 ) );
6560 }
6561 }
6562
6563 $response = [];
6564 if ( ! empty( $result ) ) {
6565 $result_post_id = $result[0]->post_id;
6566 $result_user_id = $result[0]->user_id;
6567
6568 switch ( $trigger ) {
6569 case 'lifterlms_lesson_completed':
6570 $context = array_merge(
6571 WordPress::get_user_context( $result_user_id ),
6572 LifterLMS::get_lms_lesson_context( $result_post_id )
6573 );
6574
6575 $context['course'] = get_the_title( get_post_meta( $result_post_id, '_llms_parent_course', true ) );
6576 if ( '' !== ( get_post_meta( $result_post_id, '_llms_parent_section', true ) ) ) {
6577 $context['parent_section'] = get_the_title( get_post_meta( $result_post_id, '_llms_parent_section', true ) );
6578 }
6579 break;
6580 case 'lifterlms_course_enrolled':
6581 case 'lifterlms_course_user_removed':
6582 case 'lifterlms_course_completed':
6583 $context = array_merge(
6584 WordPress::get_user_context( $result_user_id ),
6585 LifterLMS::get_lms_course_context( $result_post_id )
6586 );
6587 break;
6588 case 'lifterlms_section_completed':
6589 $data = new \LLMS_Section( $result_post_id );
6590 $lessons = $data->get_lessons();
6591 $context = array_merge(
6592 WordPress::get_user_context( $result_user_id ),
6593 WordPress::get_post_context( $result_post_id )
6594 );
6595 $context['parent_course'] = $data->get( 'parent_course' );
6596 $context['parent_course_title'] = get_the_title( $data->get( 'parent_course' ) );
6597 if ( ! empty( $lessons ) ) {
6598 foreach ( $lessons as $key => $lesson ) {
6599 $context['section_lesson'][ $key ] = $lesson->id;
6600 $context['section_lesson_title'][ $key ] = get_the_title( $lesson->id );
6601 }
6602 }
6603 $context['section_course'] = $data->get( 'parent_course' );
6604 $context['parent_course_title'] = get_the_title( $data->get( 'parent_course' ) );
6605 break;
6606 case 'lifterlms_purchase_course':
6607 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
6608 $context['course_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
6609 $context['course_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
6610 $context['course_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
6611 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
6612 $context ['order'] = WordPress::get_post_context( $result_post_id );
6613 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
6614 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
6615 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
6616 $context = array_merge( $context, WordPress::get_user_context( $user_id ) );
6617 break;
6618 case 'lifterlms_purchase_membership':
6619 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
6620 $context['membership_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
6621 $context['membership_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
6622 $context['membership_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
6623 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
6624 $context ['order'] = WordPress::get_post_context( $result_post_id );
6625 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
6626 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
6627 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
6628 $context = array_merge( $context, WordPress::get_user_context( $user_id ) );
6629 break;
6630 case 'lifterlms_cancel_membership':
6631 $context = array_merge( WordPress::get_post_context( $result_post_id ), WordPress::get_user_context( $result[0]->user_id ) );
6632 $context['membership_id'] = $result_post_id;
6633 $context['membership_name'] = get_the_title( $result_post_id );
6634 break;
6635 default:
6636 return;
6637
6638 }
6639 $response['pluggable_data'] = $context;
6640 $response['response_type'] = 'live';
6641
6642 }
6643
6644 return $response;
6645
6646 }
6647
6648 /**
6649 * Search SM data.
6650 *
6651 * @param array $data data.
6652 * @return array
6653 */
6654 public function search_suremember_last_data( $data ) {
6655 global $wpdb;
6656 $post_type = $data['post_type'];
6657 $meta_key = '_is_complete';
6658 $trigger = $data['search_term'];
6659 $post_id = $data['filter']['group_id']['value'];
6660
6661 if ( 'suremember_updated_group' === $trigger ) {
6662 if ( -1 === $post_id ) {
6663 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts as posts WHERE posts.post_type=%s", $post_type ) );
6664 } else {
6665 $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 ) );
6666 }
6667 } else {
6668 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}usermeta as usermeta WHERE usermeta.meta_key = %s", 'suremembers_user_access_group_' . $post_id ) );
6669 }
6670
6671 $response = [];
6672
6673 if ( ! empty( $result ) ) {
6674 $context = [];
6675 switch ( $trigger ) {
6676 case 'suremember_updated_group':
6677 $group_id = $result[0]->ID;
6678 $suremembers_post['rules'] = get_post_meta( $group_id, 'suremembers_plan_include', true );
6679 $suremembers_post['exclude'] = get_post_meta( $group_id, 'suremembers_plan_exclude', true ); //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
6680 $suremembers_post['suremembers_user_roles'] = get_post_meta( $group_id, 'suremembers_user_roles', true );
6681 $suremembers_post['title'] = get_the_title( $group_id );
6682 $suremembers_post['restrict'] = get_post_meta( $group_id, 'suremembers_plan_rules', true )['restrict'];
6683 $context['group'] = array_merge( WordPress::get_post_context( $group_id ), $suremembers_post );
6684 $context['group_id'] = $group_id;
6685 unset( $context['group']['ID'] );
6686 $response['pluggable_data'] = $context;
6687 $response['response_type'] = 'live';
6688 break;
6689 case 'suremember_user_added_in_group':
6690 foreach ( $result as $res ) {
6691 $meta_value = unserialize( $res->meta_value );
6692 if ( 'active' === $meta_value['status'] ) {
6693 $context = WordPress::get_user_context( $res->user_id );
6694 $context['group'] = WordPress::get_post_context( $post_id );
6695 $context['group_id'] = $post_id;
6696 unset( $context['group']['ID'] );
6697 $response['pluggable_data'] = $context;
6698 $response['response_type'] = 'live';
6699 }
6700 }
6701 break;
6702 case 'suremember_user_removed_from_group':
6703 foreach ( $result as $res ) {
6704 $meta_value = unserialize( $res->meta_value );
6705 if ( 'revoked' === $meta_value['status'] ) {
6706 $context = WordPress::get_user_context( $res->user_id );
6707 $context['group'] = WordPress::get_post_context( $post_id );
6708 $context['group_id'] = $post_id;
6709 unset( $context['group']['ID'] );
6710 $response['pluggable_data'] = $context;
6711 $response['response_type'] = 'live';
6712 }
6713 }
6714 break;
6715 default:
6716 return;
6717
6718 }
6719 }
6720
6721 return $response;
6722
6723 }
6724
6725 /**
6726 * Search CartFlows data.
6727 *
6728 * @param array $data data.
6729 * @return array
6730 */
6731 public function search_cartflows_last_data( $data ) {
6732 global $wpdb;
6733 $trigger = $data['search_term'];
6734
6735 if ( ! function_exists( 'wcf_pro' ) ) {
6736 return [];
6737 }
6738
6739 $context = [];
6740 $response = [];
6741 if ( 'cartflows_upsell_offer_accepted' === $trigger || 'cartflows_upsell_offer_rejected' === $trigger ) {
6742 $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'" );
6743 } elseif ( 'cartflows_downsell_offer_accepted' === $trigger || 'cartflows_downsell_offer_rejected' === $trigger ) {
6744 $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'" );
6745 } elseif ( 'wcf_order_bump_item_added' === $trigger || 'wcf_order_bump_item_removed' === $trigger || 'wcf_order_bump_item_replaced' === $trigger ) {
6746 $args = [
6747 'post_type' => 'product',
6748 'orderby' => 'ID',
6749 'order' => 'DESC',
6750 'post_status' => 'publish',
6751 'numberposts' => 1,
6752 ];
6753 $products = get_posts( $args );
6754
6755 if ( count( $products ) > 0 ) {
6756 $product_id = $products[0]->ID;
6757 $product_data['product_id'] = $product_id;
6758 $product_data['product'] = WooCommerce::get_product_context( $product_id );
6759 $terms = get_the_terms( $product_id, 'product_cat' );
6760 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6761 $cat_name = [];
6762 foreach ( $terms as $cat ) {
6763 $cat_name[] = $cat->name;
6764 }
6765 $product_data['product']['category'] = implode( ', ', $cat_name );
6766 }
6767 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6768 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6769 $tag_name = [];
6770 foreach ( $terms_tags as $tag ) {
6771 $tag_name[] = $tag->name;
6772 }
6773 $product_data['product']['tag'] = implode( ', ', $tag_name );
6774 }
6775 unset( $product_data['product']['id'] ); //phpcs:ignore
6776 $response['response_type'] = 'live';
6777 } else {
6778 $product_data['product'] = [
6779 'name' => 'Sample Product',
6780 'description' => 'This is description of sample product.',
6781 'short_description' => 'This is short description of sample product.',
6782 'image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
6783 'slug' => 'sample-product',
6784 'status' => 'publish',
6785 'type' => 'simple',
6786 'price' => '89',
6787 'featured' => '0',
6788 'sku' => 'hoodie-blue-sm',
6789 'regular_price' => '90',
6790 'sale_price' => '89',
6791 'total_sales' => '21',
6792 'category' => 'Uncategorized',
6793 'tags' => 'sample, new, 2022',
6794 'category_ids' => [ 40 ],
6795 'date_created' => [
6796 'date' => '2024-09-19 09:28:57.000000',
6797 'timezone_type' => '1',
6798 'timezone' => '+00:00',
6799 ],
6800 'date_modified' => [
6801 'date' => '2024-09-19 09:28:57.000000',
6802 'timezone_type' => '1',
6803 'timezone' => '+00:00',
6804 ],
6805 'product_id' => 9935,
6806 ];
6807 $response['response_type'] = 'sample';
6808 }
6809 $response['pluggable_data'] = $product_data;
6810 }
6811 if ( ! empty( $result ) && ( 'cartflows_upsell_offer_accepted' === $trigger || 'cartflows_upsell_offer_rejected' === $trigger ) ) {
6812 $context = [];
6813 $order_upsell_id = $result[0]->post_id;
6814 $step_id = get_post_meta( $order_upsell_id, '_cartflows_offer_step_id', true );
6815 $order_id = get_post_meta( $order_upsell_id, '_cartflows_offer_parent_id', true );
6816 $order = wc_get_order( $order_id );
6817 $upsell_order = wc_get_order( $order_upsell_id );
6818 $items = $upsell_order->get_items();
6819 if ( ! empty( $items ) && isset( $items[0] ) ) {
6820 $variation_id = $items[0]['product_id'];
6821 $input_qty = $items[0]['quantity'];
6822 } else {
6823 $variation_id = null;
6824 $input_qty = null;
6825 }
6826 $offer_product = wcf_pro()->utils->get_offer_data( $step_id, $variation_id, $input_qty, $order_id );
6827 $user_id = get_post_meta( $order_upsell_id, '_customer_user', true );
6828 $context = WordPress::get_user_context( $user_id );
6829 $context['order'] = $order->get_data();
6830 $context['upsell'] = $offer_product;
6831 $context['funnel_step_id'] = $step_id;
6832 if ( is_scalar( $step_id ) ) {
6833 $context['funnel_id'] = get_post_meta( intval( $step_id ), 'wcf-flow-id', true );
6834 } else {
6835 $context['funnel_id'] = null;
6836 }
6837 $response['pluggable_data'] = $context;
6838 $response['response_type'] = 'live';
6839 } elseif ( ! empty( $result ) && ( 'cartflows_downsell_offer_accepted' === $trigger || 'cartflows_downsell_offer_rejected' === $trigger ) ) {
6840 $context = [];
6841 $order_downsell_id = $result[0]->post_id;
6842 $step_id = get_post_meta( $order_downsell_id, '_cartflows_offer_step_id', true );
6843 $order_id = get_post_meta( $order_downsell_id, '_cartflows_offer_parent_id', true );
6844 $order = wc_get_order( $order_id );
6845 $downsell_order = wc_get_order( $order_downsell_id );
6846 $items = $downsell_order->get_items();
6847 if ( ! empty( $items ) && isset( $items[0] ) ) {
6848 $variation_id = $items[0]['product_id'];
6849 $input_qty = $items[0]['quantity'];
6850 } else {
6851 $variation_id = null;
6852 $input_qty = null;
6853 }
6854 $offer_product = wcf_pro()->utils->get_offer_data( $step_id, $variation_id, $input_qty, $order_id );
6855 $user_id = get_post_meta( $order_downsell_id, '_customer_user', true );
6856 $context = WordPress::get_user_context( $user_id );
6857 $context['order'] = $order->get_data();
6858 $context['downsell'] = $offer_product;
6859 $context['funnel_step_id'] = $step_id;
6860 if ( is_scalar( $step_id ) ) {
6861 $context['funnel_id'] = get_post_meta( intval( $step_id ), 'wcf-flow-id', true );
6862 } else {
6863 $context['funnel_id'] = null;
6864 }
6865 $response['pluggable_data'] = $context;
6866 $response['response_type'] = 'live';
6867 }
6868 return $response;
6869 }
6870
6871 /**
6872 * Prepare CartFlows Steps list.
6873 *
6874 * @param array $data data.
6875 *
6876 * @return array
6877 */
6878 public function search_cartflows_funnel_step_list( $data ) {
6879 $page = $data['page'];
6880 $limit = Utilities::get_search_page_limit();
6881 $offset = $limit * ( $page - 1 );
6882
6883 $args = [
6884 'posts_per_page' => $limit,
6885 'offset' => $offset,
6886 'orderby' => 'title',
6887 'order' => 'ASC',
6888 'post_type' => 'cartflows_step',
6889 'post_status' => 'publish',
6890 'fields' => 'ids',
6891 ];
6892 $args['meta_query'] = [
6893 [
6894 'key' => 'wcf-flow-id',
6895 'value' => $data['dynamic'],
6896 'compare' => '=',
6897 ],
6898 ];
6899
6900 $flow_step_list = get_posts( $args );
6901 $flow_step_list_count = count(
6902 get_posts(
6903 [
6904 'posts_per_page' => -1,
6905 'orderby' => 'title',
6906 'order' => 'ASC',
6907 'post_type' => 'cartflows_step',
6908 'post_status' => 'publish',
6909 'fields' => 'ids',
6910 'meta_query' => [
6911 [
6912 'key' => 'wcf-flow-id',
6913 'value' => $data['dynamic'],
6914 'compare' => '=',
6915 ],
6916 ],
6917 ]
6918 )
6919 );
6920
6921 $options = [];
6922 if ( ! empty( $flow_step_list ) ) {
6923 foreach ( $flow_step_list as $step ) {
6924 $options[] = [
6925 'label' => html_entity_decode( get_the_title( $step ), ENT_QUOTES, 'UTF-8' ),
6926 'value' => $step,
6927 ];
6928 }
6929 }
6930 return [
6931 'options' => $options,
6932 'hasMore' => $flow_step_list_count > $limit && $flow_step_list_count > $offset,
6933 ];
6934 }
6935
6936
6937 /**
6938 * Fetch user context.
6939 *
6940 * @param int $initiator_id initiator id.
6941 * @param int $friend_id friend id.
6942 * @return array
6943 */
6944 public function get_user_context( $initiator_id, $friend_id ) {
6945 $context = WordPress::get_user_context( $initiator_id );
6946
6947 $friend_context = WordPress::get_user_context( $friend_id );
6948
6949 $avatar = get_avatar_url( $initiator_id );
6950
6951 $context['avatar_url'] = ( $avatar ) ? $avatar : '';
6952
6953 $context['friend_id'] = $friend_id;
6954 $context['friend_first_name'] = $friend_context['user_firstname'];
6955 $context['friend_last_name'] = $friend_context['user_lastname'];
6956 $context['friend_email'] = $friend_context['user_email'];
6957
6958 $friend_avatar = get_avatar_url( $friend_id );
6959 $context['friend_avatar_url'] = $friend_avatar;
6960 return $context;
6961 }
6962
6963 /**
6964 * Search BP data.
6965 *
6966 * @param array $data data.
6967 * @return array
6968 */
6969 public function search_pluggables_bp_friendships( $data ) {
6970 global $wpdb, $bp;
6971 $context = [];
6972 $sample['pluggable_data'] = [
6973 'wp_user_id' => 4,
6974 'user_login' => 'katy1ßßßß',
6975 'display_name' => 'Katy Smith',
6976 'user_firstname' => 'Katy',
6977 'user_lastname' => 'Smith',
6978 'user_email' => 'katy1@gmail.com',
6979 'user_role' => [ 'subscriber' ],
6980 'avatar_url' => 'http://pqr.com/avatar',
6981 'friend_id' => 1,
6982 'friend_first_name' => 'John',
6983 'friend_last_name' => 'Wick',
6984 'friend_email' => 'john@gmail.com',
6985 'friend_avatar_url' => 'http://abc.com/avatar',
6986 ];
6987 $sample['response_type'] = 'sample';
6988
6989 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_friends'" );
6990 if ( $table_exists ) {
6991 $friendships = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_friends LIMIT 1" );
6992 if ( ! empty( $friendships ) ) {
6993 $friendship = $friendships[0];
6994 $initiator_id = $friendship->initiator_user_id;
6995 $friend_user_id = $friendship->friend_user_id;
6996 $context['pluggable_data'] = $this->get_user_context( $initiator_id, $friend_user_id );
6997 $context['response_type'] = 'live';
6998 } else {
6999 $context = $sample;
7000 }
7001 } else {
7002 $context = $sample;
7003 }
7004
7005
7006 return $context;
7007 }
7008
7009 /**
7010 * Search Buddyboss profile types data.
7011 *
7012 * @param array $data data.
7013 * @return array
7014 */
7015 public function search_pluggables_bp_profile_types( $data ) {
7016 global $wpdb, $bp;
7017 $context = [];
7018 $sample['pluggable_data'] = [
7019 'wp_user_id' => 4,
7020 'user_login' => 'katy1ßßßß',
7021 'display_name' => 'Katy Smith',
7022 'user_firstname' => 'Katy',
7023 'user_lastname' => 'Smith',
7024 'user_email' => 'katy1@gmail.com',
7025 'user_role' => [ 'subscriber' ],
7026 'bb_profile_type' => '10',
7027 'bb_profile_type_name' => 'student',
7028 ];
7029 $sample['response_type'] = 'sample';
7030
7031 $post_id = $data['filter']['bb_profile_type']['value'];
7032 $get_existing = get_post_meta( $post_id, '_bp_member_type_key', true );
7033
7034 $type_term = get_term_by(
7035 'name',
7036 /**
7037 *
7038 * Ignore line
7039 *
7040 * @phpstan-ignore-next-line
7041 */
7042 $get_existing,
7043 'bp_member_type'
7044 );
7045
7046 $results = $wpdb->get_results(
7047 $wpdb->prepare(
7048 "SELECT u.ID FROM {$wpdb->prefix}users u INNER JOIN {$wpdb->prefix}term_relationships r
7049 ON u.ID = r.object_id WHERE u.user_status = 0 AND
7050 r.term_taxonomy_id = %d ORDER BY RAND() LIMIT 1",
7051 /**
7052 *
7053 * Ignore line
7054 *
7055 * @phpstan-ignore-next-line
7056 */
7057 $type_term->term_id
7058 )
7059 );
7060
7061 if ( ! empty( $results ) ) {
7062 $user = $results[0];
7063 $context['pluggable_data'] = WordPress::get_user_context( $user->ID );
7064 $context['pluggable_data']['bb_profile_type'] = $post_id;
7065 $context['pluggable_data']['bb_profile_type_name'] = get_post_meta( $post_id, '_bp_member_type_label_singular_name', true );
7066 $context['response_type'] = 'live';
7067 } else {
7068 $context = $sample;
7069 }
7070
7071 return $context;
7072 }
7073
7074 /**
7075 * Search BP User data.
7076 *
7077 * @param array $data data.
7078 * @return array
7079 */
7080 public function search_pluggables_bb_users( $data ) {
7081 global $wpdb;
7082 $context = [];
7083
7084 if ( ! class_exists( 'BP_Signup' ) ) {
7085 return [];
7086 }
7087
7088 $term = $data['search_term'];
7089
7090 if ( 'account_activated' === $term ) {
7091 $signups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}signups WHERE active = 1 ORDER BY signup_id DESC LIMIT 1" );
7092 } elseif ( 'updates_profile' === $term ) {
7093 $custom_ids = $wpdb->get_var( "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data ORDER BY last_updated DESC LIMIT 1" );
7094 $args = [ 'include' => $custom_ids ];
7095 $users = get_users( $args );
7096 } elseif ( 'gains_follower' === $term ) {
7097 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_follow ORDER BY id DESC LIMIT 1" );
7098 }
7099
7100 if ( ! empty( $signups ) ) {
7101 $pluggable_data = $signups[0];
7102 $pluggable_data = get_object_vars( $pluggable_data );
7103 unset( $pluggable_data['activation_key'] );
7104 if ( is_string( $pluggable_data['meta'] ) ) {
7105 $pluggable_data['meta'] = unserialize( $pluggable_data['meta'] );
7106 }
7107 if ( is_array( $pluggable_data['meta'] ) ) {
7108 unset( $pluggable_data['meta']['password'] );
7109 }
7110 $context['pluggable_data'] = $pluggable_data;
7111 $context['response_type'] = 'live';
7112 } elseif ( 'updates_profile' === $term ) {
7113 if ( ! empty( $users ) ) {
7114 $user = $users[0];
7115 $fields = $wpdb->get_results( $wpdb->prepare( "SELECT field_id, value FROM {$wpdb->prefix}bp_xprofile_data WHERE user_id = %d", $user->ID ) );
7116 $user_data = WordPress::get_user_context( $user->ID );
7117 $pluggable_data['user_id'] = $user->ID;
7118 $pluggable_data['user_email'] = $user_data['user_email'];
7119 foreach ( $user_data['user_role'] as $key => $role ) {
7120 $pluggable_data['user_role'][ $key ] = $role;
7121 }
7122 foreach ( $fields as $field ) {
7123 if ( function_exists( 'xprofile_get_field' ) ) {
7124 $fieldj = xprofile_get_field( $field->field_id );
7125 $pluggable_data[ $fieldj->name ] = $field->value;
7126 }
7127 }
7128 $context['pluggable_data'] = $pluggable_data;
7129 $context['response_type'] = 'live';
7130 } else {
7131 $context['pluggable_data'] = [
7132 'user_login' => 'john',
7133 'display_name' => 'john',
7134 'user_firstname' => 'john',
7135 'user_lastname' => 'd',
7136 'user_email' => 'johnd@yopmail.com',
7137 'user_role' => [
7138 'subscriber',
7139 ],
7140 'Name' => 'john',
7141 'Nickname' => 'johnd',
7142 'wp_user_id' => 16,
7143 ];
7144 $context['response_type'] = 'sample';
7145 }
7146 } elseif ( 'gains_follower' === $term ) {
7147 if ( ! empty( $results ) ) {
7148 $pluggable_data['follower'] = WordPress::get_user_context( $results[0]->follower_id );
7149 $pluggable_data['leader'] = WordPress::get_user_context( $results[0]->leader_id );
7150 $context['pluggable_data'] = $pluggable_data;
7151 $context['response_type'] = 'live';
7152 } else {
7153 $context['pluggable_data'] = [
7154 'follower' => [
7155 'wp_user_id' => 126,
7156 'user_login' => 'belli',
7157 'display_name' => 'belli',
7158 'user_firstname' => 'test',
7159 'user_lastname' => 'test',
7160 'user_email' => 'belli@gmail.com',
7161 'user_role' => [
7162 'subscriber',
7163 'wpamelia-customer',
7164 ],
7165 ],
7166 'leader' => [
7167 'wp_user_id' => 34,
7168 'user_login' => 'bella3@gmail.com',
7169 'display_name' => 'bella3@gmail.com',
7170 'user_firstname' => 'bella3',
7171 'user_lastname' => 'bella3',
7172 'user_email' => 'bellaaa3@gmail.com',
7173 'user_role' => [
7174 'wpamelia-customer',
7175 ],
7176 ],
7177 ];
7178 $context['response_type'] = 'sample';
7179 }
7180 } else {
7181 $context['pluggable_data'] = [
7182 'signup_id' => '16',
7183 'domain' => '',
7184 'path' => '',
7185 'title' => '',
7186 'user_login' => 'johnd',
7187 'user_email' => 'johnd@yopmail.com',
7188 'registered' => '2024-01-29 04:52:13',
7189 'activated' => '0000-00-00 00:00:00',
7190 'active' => '0',
7191 'meta' => [
7192 'field_1' => 'john',
7193 'field_3' => 'd',
7194 'field_4' => '09878988766',
7195 'field_2' => '123 Main Street',
7196 'field_5' => 'johnd',
7197 'profile_field_ids' => '1,3,4,2,5',
7198 ],
7199 'id' => 16,
7200 'user_name' => 'johnd',
7201 'date_sent' => '2024-01-29 04:52:13',
7202 'recently_sent' => true,
7203 'count_sent' => 1,
7204 ];
7205 $context['response_type'] = 'sample';
7206 }
7207
7208 return $context;
7209 }
7210
7211 /**
7212 * Search BP data.
7213 *
7214 * @param array $data data.
7215 * @return array
7216 */
7217 public function search_pluggables_bp_groups( $data ) {
7218 global $wpdb, $bp;
7219 $context = [];
7220 $group_data = [];
7221 $args = [
7222 'orderby' => 'user_nicename',
7223 'order' => 'ASC',
7224 'number' => 1,
7225 ];
7226
7227 $term = $data['search_term'];
7228
7229 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' ) ) {
7230 return [];
7231 }
7232
7233 if ( 'user_joins_specific_type_group' == $term ) {
7234 if ( -1 !== $data['filter']['group_type']['value'] ) {
7235 $group_type = $data['filter']['group_type']['value'];
7236 } else {
7237 $registered_types = bp_groups_get_group_types();
7238 $random_key = array_rand( $registered_types );
7239 $random_value = $registered_types[ $random_key ];
7240 $group_type = $random_value;
7241 }
7242 if ( function_exists( 'bp_get_group_ids_by_group_types' ) ) {
7243 $group_ids = bp_get_group_ids_by_group_types( $group_type );
7244 $random_key = array_rand( $group_ids );
7245 $random_value = $group_ids[ $random_key ];
7246 $group_id = $random_value;
7247 if ( function_exists( 'groups_get_group' ) ) {
7248 $group = groups_get_group( $group_id['id'] );
7249 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
7250 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
7251 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
7252 $group_data['group_type'] = $group_type;
7253 if ( function_exists( 'groups_get_group_members' ) ) {
7254 $members = groups_get_group_members(
7255 [
7256 'group_id' => $group_id,
7257 ]
7258 );
7259 $ids = [];
7260 foreach ( $members['members'] as $member ) {
7261 $ids[] = $member->ID;
7262 }
7263 $args = [
7264 'number' => 1,
7265 'include' => $ids,
7266 ];
7267 $users = get_users( $args );
7268 }
7269 }
7270 }
7271 } elseif ( 'requests_access_private_group' === $data['search_term'] ) {
7272 $group_id = $data['filter']['group_id']['value'];
7273 if ( $group_id > 0 ) {
7274 $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 ) );
7275 } else {
7276 $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' ) );
7277 }
7278 $custom_ids = $results[0]->user_id;
7279 $args = [ 'include' => $custom_ids ];
7280 $users = get_users( $args );
7281 $group_id = $results[0]->item_id;
7282 $group = groups_get_group( $group_id );
7283 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
7284 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
7285 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
7286 } elseif ( 'bb_group_created' == $term ) {
7287 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups ORDER BY id DESC LIMIT 1" );
7288 if ( function_exists( 'groups_get_group' ) ) {
7289 $group = groups_get_group( $results[0]->id );
7290 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
7291 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
7292 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
7293 $current_types = (array) bp_groups_get_group_type( $results[0]->id, false );
7294 $group_data['group_type'] = $current_types;
7295 $group_data['group_status'] = $group->status;
7296 $group_data['group_date_created'] = $group->date_created;
7297 $group_data['group_enabled_forum'] = $group->enable_forum;
7298 $group_data['group_cover_url'] = bp_get_group_cover_url( $group );
7299 $group_data['group_avatar_url'] = bp_get_group_avatar_url( $group );
7300 $group_data['group_creator'] = WordPress::get_user_context( $group->creator_id );
7301 if ( function_exists( 'groups_get_group_members' ) ) {
7302 $members = groups_get_group_members(
7303 [
7304 'group_id' => $results[0]->id,
7305 ]
7306 );
7307 foreach ( $members['members'] as $key => $member ) {
7308 $group_data['group_member'][ $key ] = WordPress::get_user_context( $member->ID );
7309 }
7310 }
7311 $args = [
7312 'item_id' => $results[0]->id,
7313 ];
7314 $invitations = groups_get_invites( $args );
7315 if ( ! empty( $invitations ) ) {
7316 foreach ( $invitations as $key => $invite ) {
7317 $group_data['invitation'][ $key ] = $invite;
7318 }
7319 }
7320 }
7321 } else {
7322 $users = get_users( $args );
7323
7324 if ( isset( $data['filter']['group_id']['value'] ) ) {
7325 $group_id = $data['filter']['group_id']['value'];
7326 $args['group_id'] = $group_id;
7327 if ( $group_id > 0 ) {
7328 $group = groups_get_group( $group_id );
7329 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
7330 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
7331 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
7332 } else {
7333 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_groups'" );
7334 if ( $table_exists ) {
7335 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups LIMIT 1" );
7336 $context['groups'] = $groups;
7337 if ( ! empty( $groups ) ) {
7338 foreach ( $groups as $group ) {
7339 $group_data['group_id'] = $group->id;
7340 $group_data['group_name'] = $group->name;
7341 $group_data['group_description'] = $group->description;
7342 }
7343 }
7344 }
7345 }
7346 }
7347 }
7348
7349 if ( 'bb_group_created' == $term ) {
7350 if ( ! empty( $group_data ) ) {
7351 $pluggable_data = $group_data;
7352 $context['pluggable_data'] = $pluggable_data;
7353 $context['response_type'] = 'live';
7354 } else {
7355 $context['pluggable_data'] = [
7356 'group_id' => 112,
7357 'group_name' => 'New Group',
7358 'group_description' => 'New Group Description',
7359 'group_type' => [ 'business' ],
7360 'group_status' => 'public',
7361 'group_date_created' => '2024-02-16 05:37:22',
7362 'group_enabled_forum' => 1,
7363 'group_cover_url' => 'https:\/\/example.com\/wp-content\/uploads\/buddypress\/groups\/23\/cover-image\/65cef4c3ea9b6-bp-cover-image.jpeg',
7364 'group_avatar_url' => 'https:\/\/example.com\/wp-content\/uploads\/group-avatars\/23\/65cef4d7cee19-bpfull.png',
7365 'group_creator' => [
7366 'wp_user_id' => 183,
7367 'user_login' => 'johnd',
7368 'display_name' => 'john',
7369 'user_firstname' => 'john',
7370 'user_lastname' => 'd',
7371 'user_email' => 'johnd@yopmail.com',
7372 'user_role' => [
7373 'subscriber',
7374 'bbp_participant',
7375 ],
7376 ],
7377 ];
7378 $context['response_type'] = 'sample';
7379 }
7380 } elseif ( ! empty( $users ) ) {
7381 $user = $users[0];
7382 $pluggable_data = $group_data;
7383
7384 $avatar = get_avatar_url( $user->ID );
7385 $pluggable_data['wp_user_id'] = $user->ID;
7386 $pluggable_data['avatar_url'] = ( $avatar ) ? $avatar : '';
7387 $pluggable_data['user_login'] = $user->user_login;
7388 $pluggable_data['display_name'] = $user->display_name;
7389 $pluggable_data['user_firstname'] = $user->user_firstname;
7390 $pluggable_data['user_lastname'] = $user->user_lastname;
7391 $pluggable_data['user_email'] = $user->user_email;
7392 $pluggable_data['user_role'] = $user->roles;
7393 $context['pluggable_data'] = $pluggable_data;
7394 $context['response_type'] = 'live';
7395 } else {
7396 $context['pluggable_data'] = [
7397 'wp_user_id' => 1,
7398 'user_login' => 'admin',
7399 'display_name' => 'Johnd',
7400 'user_firstname' => 'John',
7401 'user_lastname' => 'D',
7402 'user_email' => 'johnd@gmail.com',
7403 'user_role' => [ 'subscriber' ],
7404 'group_id' => 112,
7405 'group_name' => 'New Group',
7406 'group_description' => 'New Group Description',
7407 ];
7408 $context['response_type'] = 'sample';
7409 }
7410
7411 return $context;
7412 }
7413
7414 /**
7415 * Search complete courses.
7416 *
7417 * @param array $data data.
7418 * @return array
7419 */
7420 public function search_pluggables_complete_course( $data ) {
7421 global $wpdb;
7422 $context = [];
7423
7424 if ( isset( $data['filter']['sfwd_course_id']['value'] ) ) {
7425 $course_id = $data['filter']['sfwd_course_id']['value'];
7426 }
7427 if ( -1 === $course_id ) {
7428 $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 ) );
7429 } else {
7430 $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 ) );
7431 }
7432
7433 if ( ! empty( $courses ) ) {
7434 $course = $courses[0];
7435 $course_data['course_name'] = $course->post_title;
7436 $course_data['sfwd_course_id'] = $course->ID;
7437 $course_data['course_url'] = get_permalink( $course->ID );
7438 $course_data['course_featured_image_id'] = get_post_meta( $course->ID, '_thumbnail_id', true );
7439 $course_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course->ID );
7440 $timestamp = get_user_meta( $course->user_id, 'course_completed_' . $course->ID, true );
7441 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
7442 $date_format = get_option( 'date_format' );
7443 if ( is_string( $date_format ) ) {
7444 $course_data['course_completion_date'] = wp_date( $date_format, $timestamp );
7445 }
7446 if ( function_exists( 'learndash_get_course_certificate_link' ) ) {
7447 $course_data['course_certificate'] = learndash_get_course_certificate_link( $course->ID, $course->user_id );
7448 }
7449 $context['response_type'] = 'live';
7450 } else {
7451 $course_data['course_name'] = 'Test Course';
7452 $course_data['sfwd_course_id'] = 112;
7453 $course_data['course_url'] = 'https://abc.com/test-course';
7454 $course_data['course_featured_image_id'] = 113;
7455 $course_data['course_featured_image_url'] = 'https://pqr.com/test-course-img';
7456 $course_data['course_completion_date'] = '2023-10-20';
7457 $course_data['course_certificate'] = 'https://example.com/certificates/good-performance/?course_id=112&cert-nonce=f80d0f9cc1';
7458 $context['response_type'] = 'sample';
7459 }
7460
7461 $users_data = $this->search_pluggables_add_user_role( [] );
7462 $user_data = $users_data['pluggable_data'];
7463
7464 $context['pluggable_data'] = array_merge( $course_data, $user_data );
7465 return $context;
7466 }
7467
7468 /**
7469 * Search lessons.
7470 *
7471 * @param array $data data.
7472 * @return array
7473 */
7474 public function search_pluggables_complete_lesson( $data ) {
7475 global $wpdb;
7476 $context = [];
7477
7478 if ( isset( $data['filter']['sfwd_lesson_id']['value'] ) ) {
7479 $lesson_id = $data['filter']['sfwd_lesson_id']['value'];
7480 $course_id = $data['filter']['sfwd_course_id']['value'];
7481 }
7482 if ( -1 === $course_id ) {
7483 $courses = get_posts(
7484 [
7485 'posts_per_page' => - 1,
7486 'post_type' => 'sfwd-courses',
7487 'post_status' => 'publish',
7488 'fields' => 'ids',
7489 ]
7490 );
7491 $course_key = array_rand( $courses );
7492 $course_id = $courses[ $course_key ];
7493 }
7494 $course = get_post( $course_id );
7495 $pluggable_data = LearnDash::get_course_context( $course );
7496
7497 if ( -1 === $lesson_id ) {
7498 $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 ) );
7499 } else {
7500 $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 ) );
7501 }
7502
7503 if ( ! empty( $lessons ) ) {
7504 $lesson = $lessons[0];
7505
7506 $pluggable_data = WordPress::get_user_context( $lesson->user_id );
7507 $pluggable_data['lesson_name'] = $lesson->post_title;
7508 $pluggable_data['sfwd_lesson_id'] = $lesson->ID;
7509 $pluggable_data['lesson_url'] = get_permalink( $lesson->ID );
7510 $pluggable_data['lesson_featured_image_id'] = get_post_meta( $lesson->ID, '_thumbnail_id', true );
7511 $pluggable_data['lesson_featured_image_url'] = get_the_post_thumbnail_url( $lesson->ID );
7512 $context['response_type'] = 'live';
7513 } else {
7514 $pluggable_data = WordPress::get_sample_user_context();
7515 $pluggable_data['lesson_name'] = 'Test Lesson';
7516 $pluggable_data['sfwd_lesson_id'] = 114;
7517 $pluggable_data['lesson_url'] = 'https://abc.com/test-lesson';
7518 $pluggable_data['lesson_featured_image_id'] = 116;
7519 $pluggable_data['lesson_featured_image_url'] = 'https://pqr.com/test-lesson-img';
7520 $context['response_type'] = 'sample';
7521 }
7522
7523 $context['pluggable_data'] = $pluggable_data;
7524 return $context;
7525 }
7526
7527 /**
7528 * Search topics.
7529 *
7530 * @param array $data data.
7531 * @return array
7532 */
7533 public function search_pluggables_complete_topic( $data ) {
7534 global $wpdb;
7535 $context = [];
7536
7537 if ( isset( $data['filter']['sfwd_topic_id']['value'] ) ) {
7538 $topic_id = $data['filter']['sfwd_topic_id']['value'];
7539 $course_id = $data['filter']['sfwd_course_id']['value'];
7540 }
7541 if ( -1 === $course_id ) {
7542 $courses = get_posts(
7543 [
7544 'posts_per_page' => - 1,
7545 'post_type' => 'sfwd-courses',
7546 'post_status' => 'publish',
7547 'fields' => 'ids',
7548 ]
7549 );
7550 $course_key = array_rand( $courses );
7551 $course_id = $courses[ $course_key ];
7552 }
7553 $course = get_post( $course_id );
7554 $pluggable_data = LearnDash::get_course_context( $course );
7555
7556 if ( -1 === $topic_id ) {
7557 $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 ) );
7558 } else {
7559 $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 ) );
7560 }
7561
7562 if ( ! empty( $topics ) ) {
7563 $topic = $topics[0];
7564 $pluggable_data = WordPress::get_user_context( $topics[0]->user_id );
7565 $pluggable_data['topic_name'] = $topic->post_title;
7566 $pluggable_data['sfwd_topic_id'] = $topic->ID;
7567 $pluggable_data['topic_url'] = get_permalink( $topic->ID );
7568 $pluggable_data['topic_featured_image_id'] = get_post_meta( $topic->ID, '_thumbnail_id', true );
7569 $pluggable_data['topic_featured_image_url'] = get_the_post_thumbnail_url( $topic->ID );
7570 $context['response_type'] = 'live';
7571 } else {
7572 $pluggable_data = WordPress::get_sample_user_context();
7573 $pluggable_data['topic_name'] = 'Test Topic';
7574 $pluggable_data['sfwd_topic_id'] = 117;
7575 $pluggable_data['topic_url'] = 'https://abc.com/test-topic';
7576 $pluggable_data['topic_featured_image_id'] = 118;
7577 $pluggable_data['topic_featured_image_url'] = 'https://pqr.com/test-topic-img';
7578 $context['response_type'] = 'sample';
7579 }
7580
7581 $context['pluggable_data'] = $pluggable_data;
7582 return $context;
7583 }
7584
7585 /**
7586 * Search purchase courses.
7587 *
7588 * @param array $data data.
7589 * @return array
7590 */
7591 public function search_pluggables_purchase_course( $data ) {
7592 $context = [];
7593 $context['response_type'] = 'sample';
7594
7595 $purchase_data = [
7596 'course_product_id' => '1',
7597 'course_product_name' => 'Sample Course',
7598 'currency' => 'USD',
7599 'total_amount' => '100',
7600 'first_name' => 'John',
7601 'last_name' => 'Doe',
7602 'email' => 'john_doe@gmail.com',
7603 'phone' => '+923007626541',
7604 ];
7605
7606 $product_id = (int) ( isset( $data['filter']['course_product_id']['value'] ) ? $data['filter']['course_product_id']['value'] : '-1' );
7607 $order_id = 0;
7608
7609 if ( -1 !== $product_id ) {
7610 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
7611
7612 if ( count( $order_ids ) > 0 ) {
7613 $order_id = $order_ids[0];
7614 }
7615 } else {
7616 $orders = wc_get_orders( [] );
7617 if ( count( $orders ) > 0 ) {
7618 foreach ( $orders as $order ) {
7619 $items = $order->get_items();
7620
7621 if ( count( $items ) > 1 ) {
7622 continue;
7623 }
7624
7625 foreach ( $items as $item ) {
7626 if ( method_exists( $item, 'get_product_id' ) ) {
7627 $product_id = $item->get_product_id();
7628 if ( ! empty( get_post_meta( $item->get_product_id(), '_related_course', true ) ) ) {
7629 $order_id = $order->get_id();
7630 break;
7631 }
7632 }
7633 }
7634 }
7635 }
7636 }
7637
7638 if ( 0 !== $order_id ) {
7639 $order = wc_get_order( $order_id );
7640
7641 if ( $order ) {
7642
7643 $purchase_data = LearnDash::get_purchase_course_context( $order );
7644
7645 $context['response_type'] = 'live';
7646 }
7647 }
7648
7649 $context['pluggable_data'] = $purchase_data;
7650
7651 return $context;
7652 }
7653
7654 /**
7655 * Search quiz data in LearnDash.
7656 *
7657 * @param array $data data.
7658 * @return array
7659 */
7660 public function search_pluggables_ld_quiz( $data ) {
7661 $context = [];
7662 $context['response_type'] = 'sample';
7663 global $wpdb;
7664 $user_data = WordPress::get_sample_user_context();
7665 $quiz_data = [
7666 'quiz' => 7193,
7667 'score' => 1,
7668 'count' => 1,
7669 'question_show_count' => 1,
7670 'pass' => 1,
7671 'rank' => '-',
7672 'time' => 1703595328,
7673 'pro_quizid' => 1,
7674 'course' => 0,
7675 'lesson' => 0,
7676 'topic' => 0,
7677 'points' => 20,
7678 'total_points' => 20,
7679 'percentage' => 100,
7680 'timespent' => 2.7309999999999999,
7681 'has_graded' => false,
7682 'statistic_ref_id' => 9,
7683 'started' => 1703595325,
7684 'completed' => 1703595328,
7685 'ld_version' => '4.8.0.1',
7686 'quiz_key' => '1703595328_1_7193_0',
7687 ];
7688 $output_questions['question'] = [
7689 'ID' => 7195,
7690 'post_content' => '<p>This is a first question<\/p>',
7691 'type' => 'sfwd-question',
7692 'question_type' => 'single',
7693 'points' => 20,
7694 'answers' => [
7695 'sort_answer' => [
7696 [
7697 'answer' => '',
7698 'points' => 0,
7699 'correct' => false,
7700 'type' => 'answer',
7701 ],
7702 ],
7703 'classic_answer' => [
7704 [
7705 'answer' => 'Ans 1',
7706 'points' => 1,
7707 'correct' => false,
7708 'type' => 'answer',
7709 ],
7710 [
7711 'answer' => 'Ans 2',
7712 'points' => 0,
7713 'correct' => true,
7714 'type' => 'answer',
7715 ],
7716 ],
7717 'matrix_sort_answer' => [
7718 [
7719 'answer' => '',
7720 'points' => 0,
7721 'correct' => false,
7722 'type' => 'answer',
7723 ],
7724 ],
7725 'cloze_answer' => [
7726 [
7727 'answer' => '',
7728 'points' => 0,
7729 'correct' => false,
7730 'type' => 'answer',
7731 ],
7732 ],
7733 'free_answer' => [
7734 [
7735 'answer' => '',
7736 'points' => 0,
7737 'correct' => false,
7738 'type' => 'answer',
7739 ],
7740 ],
7741 'assessment_answer' => [
7742 [
7743 'answer' => '',
7744 'points' => 0,
7745 'correct' => false,
7746 'type' => 'answer',
7747 ],
7748 ],
7749 'essay' => [
7750 [
7751 'answer' => '',
7752 'points' => 0,
7753 'correct' => false,
7754 'type' => 'answer',
7755 ],
7756 ],
7757 ],
7758 ];
7759
7760 $quiz_id = (int) ( isset( $data['filter']['sfwd_quiz_id']['value'] ) ? $data['filter']['sfwd_quiz_id']['value'] : '-1' );
7761 $question_id = $data['filter']['sfwd_question_id']['value'];
7762 $term = $data['search_term'];
7763 $mark = 1;
7764 if ( 'passes_quiz' == $term ) {
7765 $mark = 1;
7766 } elseif ( 'fails_quiz' == $term ) {
7767 $mark = 0;
7768 }
7769 if ( 'passes_quiz' == $term || 'fails_quiz' == $term ) {
7770 if ( -1 == $quiz_id ) {
7771 $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 ) );
7772 } else {
7773 $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 ) );
7774 }
7775 } elseif ( 'quiz_essay_submitted' == $term ) {
7776 $question_id = $data['filter']['sfwd_question_id']['value'];
7777 $args = [
7778 'post_type' => 'sfwd-essays',
7779 'posts_per_page' => 1,
7780 'order' => 'DESC',
7781 'post_status' => [ 'graded', 'not_graded' ],
7782 'meta_query' => [
7783 'relation' => 'AND',
7784 [
7785 'key' => 'quiz_post_id',
7786 'value' => $quiz_id,
7787 'compare' => '=',
7788 ],
7789 [
7790 'key' => 'question_post_id',
7791 'value' => $question_id,
7792 'compare' => '=',
7793 ],
7794 ],
7795 ];
7796 $essay = get_posts( $args );
7797 } elseif ( 'quiz_essay_graded' == $term ) {
7798 $args = [
7799 'post_type' => 'sfwd-essays',
7800 'posts_per_page' => 1,
7801 'order' => 'DESC',
7802 'post_status' => 'graded',
7803 'meta_query' => [
7804 'relation' => 'AND',
7805 [
7806 'key' => 'quiz_post_id',
7807 'value' => $quiz_id,
7808 'compare' => '=',
7809 ],
7810 [
7811 'key' => 'question_post_id',
7812 'value' => $question_id,
7813 'compare' => '=',
7814 ],
7815 ],
7816 ];
7817 $essay = get_posts( $args );
7818 }
7819
7820 if ( 'quiz_essay_submitted' == $term || 'quiz_essay_graded' == $term ) {
7821 if ( ! empty( $essay ) ) {
7822 $context = WordPress::get_user_context( $essay[0]->post_author );
7823 $course_id = get_post_meta( $essay[0]->ID, 'course_id', true );
7824 $lesson_id = get_post_meta( $essay[0]->ID, 'lesson_id', true );
7825 $context['quiz_name'] = get_the_title( $quiz_id );
7826 $context['sfwd_quiz_id'] = $quiz_id;
7827 $context['sfwd_question_id'] = $question_id;
7828 $context['question_name'] = is_int( $question_id ) ? (int) get_the_title( $question_id ) : null;
7829 $context['course_name'] = is_int( $course_id ) ? (int) get_the_title( $course_id ) : null;
7830 $context['course_id'] = $course_id;
7831 $context['lesson_name'] = is_int( $lesson_id ) ? (int) get_the_title( $lesson_id ) : null;
7832 $context['lesson_id'] = $lesson_id;
7833 $context['essay_id'] = $essay[0]->ID;
7834 $context['essay'] = WordPress::get_post_context( $essay[0]->ID );
7835 if ( 'quiz_essay_graded' == $term ) {
7836 $users_quiz_data = get_user_meta( (int) $essay[0]->post_author, '_sfwd-quizzes', true );
7837 if ( is_array( $users_quiz_data ) && is_array( $users_quiz_data[0] ) ) {
7838 $essay_post_id = $essay[0]->ID;
7839 $result = array_filter(
7840 $users_quiz_data[0]['graded'],
7841 function( $item ) use ( $essay_post_id ) {
7842 return $item['post_id'] === $essay_post_id;
7843 }
7844 );
7845 $context['essay_points_earned'] = $result[1]['points_awarded'];
7846 }
7847 }
7848 $context['response_type'] = 'live';
7849 } else {
7850 $context = WordPress::get_sample_user_context();
7851 $context['quiz_name'] = 'Test Quiz';
7852 $context['sfwd_quiz_id'] = 11;
7853 $context['sfwd_question_id'] = 12;
7854 $context['course_name'] = 'Test Course';
7855 $context['course_id'] = 13;
7856 $context['lesson_name'] = 'Test Lesson';
7857 $context['lesson_id'] = 14;
7858 $context['essay'] = [
7859 'ID' => 12,
7860 'post_content' => 'demo',
7861 ];
7862 $context['response_type'] = 'sample';
7863 }
7864
7865 $context['pluggable_data'] = $context;
7866 } else {
7867 if ( ! empty( $result ) ) {
7868 $user_data = WordPress::get_user_context( $result[0]->user_id );
7869 $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 ) );
7870
7871 $data = unserialize( $quiz_result[0]->meta_value );
7872 $found_element = null;
7873 foreach ( (array) $data as $element ) {
7874 if ( is_array( $element ) ) {
7875 if ( isset( $element['statistic_ref_id'] ) && $element['statistic_ref_id'] == $result[0]->statistic_ref_id ) {
7876 $found_element = $element;
7877 break;
7878 }
7879 }
7880 }
7881 $quiz_data = (array) $found_element;
7882 $output_questions = LearnDash::get_quiz_questions_answers( $quiz_id );
7883 if ( is_array( $quiz_data ) ) {
7884 $quiz_data['quiz_name'] = get_the_title( $quiz_id );
7885 $quiz_data['sfwd_quiz_id'] = $quiz_id;
7886 }
7887 $context['response_type'] = 'live';
7888 }
7889 $context['pluggable_data'] = array_merge( $quiz_data, $user_data, $output_questions );
7890 }
7891
7892 return $context;
7893 }
7894
7895 /**
7896 * Fetch BB templates.
7897 *
7898 * @return array
7899 */
7900 public function get_beaver_builder_templates() {
7901 $allowed_types = [ 'subscribe-form', 'contact-form' ];
7902 $templates = [];
7903 $all_templates = get_posts(
7904 [
7905 'post_type' => 'fl-builder-template',
7906 'meta_key' => '_fl_builder_data',
7907 'posts_per_page' => -1,
7908 ]
7909 );
7910 $posts = get_posts(
7911 [
7912 'post_type' => 'any',
7913 'meta_key' => '_fl_builder_data',
7914 'posts_per_page' => -1,
7915 ]
7916 );
7917 $posts = array_merge( $all_templates, $posts );
7918
7919 if ( ! empty( $posts ) ) {
7920 foreach ( $posts as $post ) {
7921 $meta = get_post_meta( $post->ID, '_fl_builder_data', true );
7922 foreach ( (array) $meta as $node_id => $node ) {
7923 if ( isset( $node->type ) && 'module' === $node->type ) {
7924 $settings = $node->settings;
7925 if ( in_array( $settings->type, $allowed_types, true ) ) {
7926 $label = $post->post_title;
7927 if ( '' !== $settings->node_label ) {
7928 $label .= ' - ' . $settings->node_label;
7929 }
7930 $templates[] = [
7931 'label' => $label,
7932 'value' => $node_id,
7933 ];
7934 }
7935 }
7936 }
7937 }
7938 }
7939 return $templates;
7940 }
7941
7942 /**
7943 * Search beaver builder forms.
7944 *
7945 * @param array $data data.
7946 * @return array
7947 */
7948 public function search_beaver_builder_forms( $data ) {
7949 $templates = $this->get_beaver_builder_templates();
7950 return [
7951 'options' => $templates,
7952 'hasMore' => false,
7953 ];
7954 }
7955
7956 /**
7957 * Search FluentCRM fields.
7958 *
7959 * @param array $data data.
7960 * @return array
7961 */
7962 public function search_fluentcrm_custom_fields( $data ) {
7963 $context = [];
7964 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
7965 $context['fields'] = $custom_fields;
7966 return $context;
7967 }
7968
7969 /**
7970 * Search FluentCRM Company fields.
7971 *
7972 * @param array $data data.
7973 * @return array
7974 */
7975 public function search_fluentcrm_company_custom_fields( $data ) {
7976 $context = [];
7977 if ( function_exists( 'fluentcrm_get_custom_company_fields' ) ) {
7978 $custom_fields = fluentcrm_get_custom_company_fields();
7979 $context['fields'] = $custom_fields;
7980 }
7981 return $context;
7982 }
7983
7984 /**
7985 * Search FluentCRM fields and display it in dropdown for trigger.
7986 *
7987 * @param array $data data.
7988 * @return array
7989 */
7990 public function search_fluentcrm_custom_fields_data( $data ) {
7991
7992 if ( ! class_exists( 'FluentCrm\App\Models\CustomContactField' ) ) {
7993 return [];
7994 }
7995
7996 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
7997 $options = [];
7998
7999 if ( ! empty( $custom_fields ) ) {
8000 foreach ( $custom_fields as $custom_field ) {
8001 $options[] = [
8002 'label' => $custom_field['label'],
8003 'value' => $custom_field['slug'],
8004 ];
8005 }
8006 }
8007
8008 return [
8009 'options' => $options,
8010 'hasMore' => false,
8011 ];
8012 }
8013
8014 /**
8015 * Fetch WP JOB Manager Last Data.
8016 *
8017 * @param array $data data.
8018 * @return array
8019 */
8020 public function search_wp_job_manger_last_data( $data ) {
8021 global $wpdb;
8022 $job_type = $data['filter']['job_type']['value'];
8023 $args = [
8024 'posts_per_page' => 1,
8025 'post_type' => 'job_listing',
8026 'orderby' => 'id',
8027 'order' => 'DESC',
8028 ];
8029
8030 if ( -1 !== $job_type ) {
8031 $args['tax_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
8032 [
8033 'taxonomy' => 'job_listing_type',
8034 'field' => 'term_id',
8035 'terms' => $job_type,
8036 ],
8037 ];
8038 }
8039 $posts = get_posts( $args );
8040 if ( empty( $posts ) ) {
8041 $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 );
8042 return $context;
8043 }
8044
8045 $post = $posts[0];
8046 $post_content = WordPress::get_post_context( $post->ID );
8047 $post_meta = WordPress::get_post_meta( $post->ID );
8048 $job_data = array_merge( $post_content, $post_meta, WordPress::get_user_context( $post->post_author ) );
8049 foreach ( $job_data as $key => $job ) {
8050 $newkey = str_replace( 'post', 'wpjob', $key );
8051 unset( $job_data[ $key ] );
8052 $job_data[ $newkey ] = $job;
8053 }
8054 $context['response_type'] = 'live';
8055 $context['pluggable_data'] = $job_data;
8056 return $context;
8057
8058 }
8059
8060 /**
8061 * Get Amelia Appointment Category.
8062 *
8063 * @param array $data data.
8064 *
8065 * @return array
8066 */
8067 public function search_amelia_category_list( $data ) {
8068
8069 global $wpdb;
8070
8071 $page = $data['page'];
8072 $limit = Utilities::get_search_page_limit();
8073 $offset = $limit * ( $page - 1 );
8074
8075 $categories = $wpdb->get_results(
8076 $wpdb->prepare(
8077 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_categories WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
8078 [ 'visible', $limit, $offset ]
8079 ),
8080 OBJECT
8081 );
8082
8083 $categories_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
8084
8085 $options = [];
8086 if ( ! empty( $categories ) ) {
8087 foreach ( $categories as $category ) {
8088 $options[] = [
8089 'label' => $category->name,
8090 'value' => $category->id,
8091 ];
8092 }
8093 }
8094
8095 return [
8096 'options' => $options,
8097 'hasMore' => $categories_count > $limit && $categories_count > $offset,
8098 ];
8099
8100 }
8101
8102 /**
8103 * Get Amelia Appointment Services.
8104 *
8105 * @param array $data data.
8106 *
8107 * @return array
8108 */
8109 public function search_amelia_service_list( $data ) {
8110
8111 global $wpdb;
8112
8113 $page = $data['page'];
8114 $limit = Utilities::get_search_page_limit();
8115 $offset = $limit * ( $page - 1 );
8116
8117 $services = $wpdb->get_results(
8118 $wpdb->prepare(
8119 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_services
8120 WHERE categoryId = %d AND status = %s
8121 ORDER BY name ASC LIMIT %d OFFSET %d",
8122 [ $data['dynamic'], 'visible', $limit, $offset ]
8123 ),
8124 OBJECT
8125 );
8126
8127 $services_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
8128
8129 $options = [];
8130 if ( ! empty( $services ) ) {
8131 foreach ( $services as $category ) {
8132 $options[] = [
8133 'label' => $category->name,
8134 'value' => $category->id,
8135 ];
8136 }
8137 }
8138
8139 return [
8140 'options' => $options,
8141 'hasMore' => $services_count > $limit && $services_count > $offset,
8142 ];
8143
8144 }
8145
8146 /**
8147 * Get Amelia Events.
8148 *
8149 * @param array $data data.
8150 *
8151 * @return array
8152 */
8153 public function search_amelia_events_list( $data ) {
8154
8155 global $wpdb;
8156
8157 $page = $data['page'];
8158 $limit = Utilities::get_search_page_limit();
8159 $offset = $limit * ( $page - 1 );
8160
8161 $events = $wpdb->get_results(
8162 $wpdb->prepare(
8163 "SELECT SQL_CALC_FOUND_ROWS id, name from {$wpdb->prefix}amelia_events WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
8164 [ 'approved', $limit, $offset ]
8165 ),
8166 OBJECT
8167 );
8168
8169 $list_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
8170
8171 $options = [];
8172 if ( ! empty( $events ) ) {
8173 foreach ( $events as $event ) {
8174 $options[] = [
8175 'label' => $event->name,
8176 'value' => $event->id,
8177 ];
8178 }
8179 }
8180
8181 return [
8182 'options' => $options,
8183 'hasMore' => $list_count > $limit && $list_count > $offset,
8184 ];
8185
8186 }
8187
8188 /**
8189 * Get Amelia Events.
8190 *
8191 * @param array $data data.
8192 *
8193 * @return array
8194 */
8195 public function search_amelia_booking_status_list( $data ) {
8196
8197 $options = [];
8198 $options[] = [
8199 'label' => 'Approved',
8200 'value' => 'approved',
8201 ];
8202 $options[] = [
8203 'label' => 'Pending',
8204 'value' => 'pending',
8205 ];
8206 $options[] = [
8207 'label' => 'Rejected',
8208 'value' => 'rejected',
8209 ];
8210 $options[] = [
8211 'label' => 'Canceled',
8212 'value' => 'canceled',
8213 ];
8214 $options[] = [
8215 'label' => 'No-show',
8216 'value' => 'no-show',
8217 ];
8218 return [
8219 'options' => $options,
8220 'hasMore' => false,
8221 ];
8222 }
8223
8224 /**
8225 * Get last data for trigger.
8226 *
8227 * @param array $data data.
8228 * @return array
8229 */
8230 public function search_amelia_appointment_booked_triggers_last_data( $data ) {
8231 global $wpdb;
8232
8233 $context = [];
8234
8235 $appointment_category = $data['filter']['amelia_category_list']['value'];
8236 $appointment_service = $data['filter']['amelia_service_list']['value'];
8237
8238 if ( -1 === $appointment_service ) {
8239 // If service exists as per category selected.
8240 $service_exist = $wpdb->get_row(
8241 $wpdb->prepare(
8242 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
8243 [ $appointment_category ]
8244 ),
8245 ARRAY_A
8246 );
8247
8248 if ( empty( $service_exist ) ) {
8249 $result = [];
8250 } else {
8251 $result = $wpdb->get_row(
8252 '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 )',
8253 ARRAY_A
8254 );
8255 }
8256 } else {
8257 $result = $wpdb->get_row(
8258 $wpdb->prepare(
8259 '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',
8260 [ $appointment_service ]
8261 ),
8262 ARRAY_A
8263 );
8264 }
8265
8266 if ( ! empty( $result ) ) {
8267
8268 $payment_result = $wpdb->get_row(
8269 $wpdb->prepare(
8270 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
8271 [ $result['id'] ]
8272 ),
8273 ARRAY_A
8274 );
8275
8276 $customer_result = $wpdb->get_row(
8277 $wpdb->prepare(
8278 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8279 [ $result['customerId'] ]
8280 ),
8281 ARRAY_A
8282 );
8283
8284 $service_result = $wpdb->get_row(
8285 $wpdb->prepare(
8286 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
8287 [ $result['serviceId'] ]
8288 ),
8289 ARRAY_A
8290 );
8291
8292 $category_result = $wpdb->get_row(
8293 $wpdb->prepare(
8294 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
8295 [ $service_result['categoryId'] ]
8296 ),
8297 ARRAY_A
8298 );
8299
8300 if ( $result['couponId'] ) {
8301 $coupon_result = $wpdb->get_row(
8302 $wpdb->prepare(
8303 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8304 [ $result['couponId'] ]
8305 ),
8306 ARRAY_A
8307 );
8308 } else {
8309 $coupon_result = [];
8310 }
8311
8312 if ( ! empty( $result['customFields'] ) ) {
8313 $custom_fields = json_decode( $result['customFields'], true );
8314
8315 $fields_arr = [];
8316 foreach ( (array) $custom_fields as $fields ) {
8317 if ( is_array( $fields ) ) {
8318 $fields_arr[ $fields['label'] ] = $fields['value'];
8319 }
8320 }
8321 unset( $result['customFields'] );
8322 } else {
8323 $fields_arr = [];
8324 }
8325
8326 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
8327 $context['response_type'] = 'live';
8328 } else {
8329
8330 $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 );
8331 }
8332
8333 return $context;
8334 }
8335
8336 /**
8337 * Get last data for trigger.
8338 *
8339 * @param array $data data.
8340 * @return array
8341 */
8342 public function search_amelia_appointment_booking_status_changed_triggers_last_data( $data ) {
8343 global $wpdb;
8344
8345 $context = [];
8346
8347 $appointment_category = $data['filter']['amelia_category_list']['value'];
8348 $appointment_service = $data['filter']['amelia_service_list']['value'];
8349 $appointment_status = $data['filter']['appointment_status']['value'];
8350
8351 if ( -1 === $appointment_service ) {
8352 // If service exists as per category selected.
8353 $service_exist = $wpdb->get_row(
8354 $wpdb->prepare(
8355 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
8356 [ $appointment_category ]
8357 ),
8358 ARRAY_A
8359 );
8360
8361 if ( empty( $service_exist ) ) {
8362 $result = [];
8363 } else {
8364 $result = $wpdb->get_row(
8365 '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 )',
8366 ARRAY_A
8367 );
8368 }
8369 } else {
8370 if ( -1 === $appointment_status ) {
8371 $result = $wpdb->get_row(
8372 $wpdb->prepare(
8373 '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',
8374 [ $appointment_service ]
8375 ),
8376 ARRAY_A
8377 );
8378 } else {
8379 $result = $wpdb->get_row(
8380 $wpdb->prepare(
8381 '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',
8382 [ $appointment_status, $appointment_service ]
8383 ),
8384 ARRAY_A
8385 );
8386 }
8387 }
8388
8389 if ( ! empty( $result ) ) {
8390
8391 $payment_result = $wpdb->get_row(
8392 $wpdb->prepare(
8393 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
8394 [ $result['id'] ]
8395 ),
8396 ARRAY_A
8397 );
8398
8399 $customer_result = $wpdb->get_row(
8400 $wpdb->prepare(
8401 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8402 [ $result['customerId'] ]
8403 ),
8404 ARRAY_A
8405 );
8406
8407 $service_result = $wpdb->get_row(
8408 $wpdb->prepare(
8409 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
8410 [ $result['serviceId'] ]
8411 ),
8412 ARRAY_A
8413 );
8414
8415 $category_result = $wpdb->get_row(
8416 $wpdb->prepare(
8417 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
8418 [ $service_result['categoryId'] ]
8419 ),
8420 ARRAY_A
8421 );
8422
8423 if ( $result['couponId'] ) {
8424 $coupon_result = $wpdb->get_row(
8425 $wpdb->prepare(
8426 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8427 [ $result['couponId'] ]
8428 ),
8429 ARRAY_A
8430 );
8431 } else {
8432 $coupon_result = [];
8433 }
8434
8435 if ( ! empty( $result['customFields'] ) ) {
8436 $custom_fields = json_decode( $result['customFields'], true );
8437
8438 $fields_arr = [];
8439 foreach ( (array) $custom_fields as $fields ) {
8440 if ( is_array( $fields ) ) {
8441 $fields_arr[ $fields['label'] ] = $fields['value'];
8442 }
8443 }
8444 unset( $result['customFields'] );
8445 } else {
8446 $fields_arr = [];
8447 }
8448
8449 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
8450 $context['pluggable_data']['amelia_category_list'] = $appointment_category;
8451 if ( -1 === $appointment_status ) {
8452 $context['pluggable_data']['appointment_status'] = 'approved';
8453 } else {
8454 $context['pluggable_data']['appointment_status'] = $appointment_status;
8455 }
8456 if ( -1 === $appointment_service ) {
8457 $context['pluggable_data']['amelia_service_list'] = $service_result['id'];
8458 } else {
8459 $context['pluggable_data']['amelia_service_list'] = $appointment_service;
8460 }
8461 $context['response_type'] = 'live';
8462 } else {
8463
8464 $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 );
8465 }
8466
8467 return $context;
8468 }
8469
8470 /**
8471 * Get last data for trigger.
8472 *
8473 * @param array $data data.
8474 * @return array
8475 */
8476 public function search_amelia_event_status_changed_triggers_last_data( $data ) {
8477 global $wpdb;
8478
8479 $context = [];
8480
8481 $event_selected = $data['filter']['amelia_events_list']['value'];
8482 $event_status = $data['filter']['event_booking_status']['value'];
8483
8484 $query = 'SELECT *
8485 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8486 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8487 ON customer.id = event_period.customerBookingId';
8488
8489 if ( -1 === $event_selected ) {
8490 if ( -1 !== $event_status ) {
8491 $query .= $wpdb->prepare(
8492 ' WHERE event_period.customerBookingId = (
8493 SELECT MAX(customerBookingId)
8494 FROM ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods
8495 ) AND customer.status = %s',
8496 $event_status
8497 );
8498 } else {
8499 $query .= ' WHERE event_period.customerBookingId = (
8500 SELECT MAX(customerBookingId)
8501 FROM ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods
8502 )';
8503 }
8504 } else {
8505 $event_periods_result = $wpdb->get_results(
8506 $wpdb->prepare(
8507 'SELECT id
8508 FROM ' . $wpdb->prefix . 'amelia_events_periods
8509 WHERE eventId = %d',
8510 [ $event_selected ]
8511 ),
8512 ARRAY_A
8513 );
8514 if ( ! empty( $event_periods_result ) ) {
8515 $ids = wp_list_pluck( $event_periods_result, 'id' );
8516 }
8517
8518 if ( ! empty( $ids ) ) {
8519 if ( -1 !== $event_status ) {
8520 $query .= $wpdb->prepare(
8521 ' WHERE event_period.eventPeriodId IN (%s) AND customer.status = %s',
8522 implode( ',', $ids ),
8523 $event_status
8524 );
8525 } else {
8526 $query .= $wpdb->prepare(
8527 ' WHERE event_period.eventPeriodId IN (%s)',
8528 implode( ',', $ids )
8529 );
8530 }
8531 } else {
8532 if ( -1 !== $event_status ) {
8533 $query .= $wpdb->prepare(
8534 ' WHERE event_period.eventPeriodId = %d AND customer.status = %s',
8535 $event_selected,
8536 $event_status
8537 );
8538 } else {
8539 $query .= $wpdb->prepare(
8540 ' WHERE event_period.eventPeriodId = %d',
8541 $event_selected
8542 );
8543 }
8544 }
8545 }
8546
8547 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
8548
8549 if ( ! empty( $result ) ) {
8550 $event = $wpdb->get_row(
8551 $wpdb->prepare(
8552 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
8553 [ $event_selected ]
8554 ),
8555 ARRAY_A
8556 );
8557 $event = isset( $event ) ? $event : [];
8558 $event_tags = $wpdb->get_results(
8559 $wpdb->prepare(
8560 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
8561 [ $event_selected ]
8562 ),
8563 ARRAY_A
8564 );
8565 $tags = [];
8566 if ( ! empty( $event_tags ) ) {
8567 foreach ( $event_tags as $key => $tag ) {
8568 $tags['event_tag'][ $key ] = $tag['name'];
8569 }
8570 } else {
8571 $tags = [];
8572 }
8573 $customer_result = $wpdb->get_row(
8574 $wpdb->prepare(
8575 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8576 [ $result['customerId'] ]
8577 ),
8578 ARRAY_A
8579 );
8580 $customer_result = isset( $customer_result ) ? $customer_result : [];
8581 if ( $result['couponId'] ) {
8582 $coupon_result = $wpdb->get_row(
8583 $wpdb->prepare(
8584 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8585 [ $result['couponId'] ]
8586 ),
8587 ARRAY_A
8588 );
8589 } else {
8590 $coupon_result = [];
8591 }
8592 if ( ! empty( $result['customFields'] ) ) {
8593 $custom_fields = json_decode( $result['customFields'], true );
8594
8595 $fields_arr = [];
8596 foreach ( (array) $custom_fields as $fields ) {
8597 if ( is_array( $fields ) ) {
8598 $fields_arr[ $fields['label'] ] = $fields['value'];
8599 }
8600 }
8601 unset( $result['customFields'] );
8602 } else {
8603 $fields_arr = [];
8604 }
8605 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
8606 if ( -1 === $event_status ) {
8607 $context['pluggable_data']['event_booking_status'] = 'approved';
8608 } else {
8609 $context['pluggable_data']['event_booking_status'] = $event_status;
8610 }
8611 $context['response_type'] = 'live';
8612 } else {
8613
8614 $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 );
8615 }
8616
8617 return $context;
8618 }
8619
8620 /**
8621 * Get last data for trigger.
8622 *
8623 * @param array $data data.
8624 * @return array
8625 */
8626 public function search_amelia_new_event_attendee_triggers_last_data( $data ) {
8627 global $wpdb;
8628
8629 $context = [];
8630
8631 $event_selected = $data['filter']['amelia_events_list']['value'];
8632
8633 $query = 'SELECT *
8634 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8635 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8636 ON customer.id = event_period.customerBookingId';
8637
8638 if ( -1 === $event_selected ) {
8639 $query .= '
8640 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods )';
8641 } else {
8642 $event_periods_result = $wpdb->get_results(
8643 $wpdb->prepare(
8644 'SELECT id
8645 FROM ' . $wpdb->prefix . 'amelia_events_periods
8646 WHERE eventId = %d',
8647 [ $event_selected ]
8648 ),
8649 ARRAY_A
8650 );
8651 if ( ! empty( $event_periods_result ) ) {
8652 $ids = wp_list_pluck( $event_periods_result, 'id' );
8653 }
8654 if ( ! empty( $ids ) ) {
8655 $query .= $wpdb->prepare(
8656 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND event_period.eventPeriodId IN (%s)',
8657 implode( ',', $ids )
8658 );
8659 } else {
8660 $query .= $wpdb->prepare(
8661 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND event_period.eventPeriodId = %d',
8662 [ $event_selected ]
8663 );
8664 }
8665 }
8666 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
8667
8668 if ( ! empty( $result ) ) {
8669
8670 $event = $wpdb->get_row(
8671 $wpdb->prepare(
8672 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
8673 [ $event_selected ]
8674 ),
8675 ARRAY_A
8676 );
8677 $event = isset( $event ) ? $event : [];
8678 $event_tags = $wpdb->get_results(
8679 $wpdb->prepare(
8680 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
8681 [ $event_selected ]
8682 ),
8683 ARRAY_A
8684 );
8685 $tags = [];
8686 if ( ! empty( $event_tags ) ) {
8687 foreach ( $event_tags as $key => $tag ) {
8688 $tags['event_tag'][ $key ] = $tag['name'];
8689 }
8690 } else {
8691 $tags = [];
8692 }
8693
8694 $customer_result = $wpdb->get_row(
8695 $wpdb->prepare(
8696 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8697 [ $result['customerId'] ]
8698 ),
8699 ARRAY_A
8700 );
8701 $customer_result = isset( $customer_result ) ? $customer_result : [];
8702
8703 if ( $result['couponId'] ) {
8704 $coupon_result = $wpdb->get_row(
8705 $wpdb->prepare(
8706 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8707 [ $result['couponId'] ]
8708 ),
8709 ARRAY_A
8710 );
8711 } else {
8712 $coupon_result = [];
8713 }
8714
8715 if ( ! empty( $result['customFields'] ) ) {
8716 $custom_fields = json_decode( $result['customFields'], true );
8717
8718 $fields_arr = [];
8719 foreach ( (array) $custom_fields as $fields ) {
8720 if ( is_array( $fields ) ) {
8721 $fields_arr[ $fields['label'] ] = $fields['value'];
8722 }
8723 }
8724 unset( $result['customFields'] );
8725 } else {
8726 $fields_arr = [];
8727 }
8728
8729 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
8730 $context['response_type'] = 'live';
8731 } else {
8732
8733 $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 );
8734 }
8735
8736 return $context;
8737 }
8738
8739 /**
8740 * Get last data for trigger.
8741 *
8742 * @param array $data data.
8743 * @return array
8744 */
8745 public function search_amelia_event_booking_cancelled_triggers_last_data( $data ) {
8746 global $wpdb;
8747
8748 $context = [];
8749
8750 $event_selected = $data['filter']['amelia_events_list']['value'];
8751
8752 if ( -1 === $event_selected ) {
8753 $result = $wpdb->get_row(
8754 'SELECT *
8755 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8756 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8757 ON customer.id = event_period.customerBookingId
8758 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND customer.status = "canceled"',
8759 ARRAY_A
8760 );
8761 } else {
8762 $event_periods_result = $wpdb->get_results(
8763 $wpdb->prepare(
8764 'SELECT id
8765 FROM ' . $wpdb->prefix . 'amelia_events_periods
8766 WHERE eventId = %d',
8767 [ $event_selected ]
8768 ),
8769 ARRAY_A
8770 );
8771 if ( ! empty( $event_periods_result ) ) {
8772 $ids = wp_list_pluck( $event_periods_result, 'id' );
8773 }
8774 $query = 'SELECT *
8775 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8776 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8777 ON customer.id = event_period.customerBookingId';
8778 if ( ! empty( $ids ) ) {
8779 $query .= $wpdb->prepare(
8780 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId IN (%s) AND customer.status = "canceled"',
8781 implode( ',', $ids )
8782 );
8783 } else {
8784 $query .= $wpdb->prepare(
8785 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId = %d
8786 AND customer.status = "canceled"',
8787 [ $event_selected ]
8788 );
8789 }
8790 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
8791 }
8792
8793 if ( ! empty( $result ) ) {
8794 $event = $wpdb->get_row(
8795 $wpdb->prepare(
8796 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
8797 [ $event_selected ]
8798 ),
8799 ARRAY_A
8800 );
8801 $event = isset( $event ) ? $event : [];
8802 $event_tags = $wpdb->get_results(
8803 $wpdb->prepare(
8804 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
8805 [ $event_selected ]
8806 ),
8807 ARRAY_A
8808 );
8809 $tags = [];
8810 if ( ! empty( $event_tags ) ) {
8811 foreach ( $event_tags as $key => $tag ) {
8812 $tags['event_tag'][ $key ] = $tag['name'];
8813 }
8814 } else {
8815 $tags = [];
8816 }
8817 $customer_result = $wpdb->get_row(
8818 $wpdb->prepare(
8819 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8820 [ $result['customerId'] ]
8821 ),
8822 ARRAY_A
8823 );
8824 $customer_result = isset( $customer_result ) ? $customer_result : [];
8825 if ( $result['couponId'] ) {
8826 $coupon_result = $wpdb->get_row(
8827 $wpdb->prepare(
8828 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8829 [ $result['couponId'] ]
8830 ),
8831 ARRAY_A
8832 );
8833 } else {
8834 $coupon_result = [];
8835 }
8836 if ( ! empty( $result['customFields'] ) ) {
8837 $custom_fields = json_decode( $result['customFields'], true );
8838
8839 $fields_arr = [];
8840 foreach ( (array) $custom_fields as $fields ) {
8841 if ( is_array( $fields ) ) {
8842 $fields_arr[ $fields['label'] ] = $fields['value'];
8843 }
8844 }
8845 unset( $result['customFields'] );
8846 } else {
8847 $fields_arr = [];
8848 }
8849 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
8850 $context['response_type'] = 'live';
8851 } else {
8852
8853 $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 );
8854 }
8855
8856 return $context;
8857 }
8858
8859 /**
8860 * Get last data for trigger.
8861 *
8862 * @param array $data data.
8863 * @return array
8864 */
8865 public function search_amelia_appointment_rescheduled_triggers_last_data( $data ) {
8866 global $wpdb;
8867
8868 $context = [];
8869
8870 $appointment_category = $data['filter']['amelia_category_list']['value'];
8871 $appointment_service = $data['filter']['amelia_service_list']['value'];
8872
8873 if ( -1 === $appointment_service ) {
8874 // If service exists as per category selected.
8875 $service_exist = $wpdb->get_row(
8876 $wpdb->prepare(
8877 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
8878 [ $appointment_category ]
8879 ),
8880 ARRAY_A
8881 );
8882
8883 if ( empty( $service_exist ) ) {
8884 $result = [];
8885 } else {
8886 $result = $wpdb->get_row(
8887 '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 )',
8888 ARRAY_A
8889 );
8890 }
8891 } else {
8892 $result = $wpdb->get_row(
8893 $wpdb->prepare(
8894 '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',
8895 [ $appointment_service ]
8896 ),
8897 ARRAY_A
8898 );
8899 }
8900
8901 if ( ! empty( $result ) ) {
8902
8903 $payment_result = $wpdb->get_row(
8904 $wpdb->prepare(
8905 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
8906 [ $result['id'] ]
8907 ),
8908 ARRAY_A
8909 );
8910
8911 $customer_result = $wpdb->get_row(
8912 $wpdb->prepare(
8913 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8914 [ $result['customerId'] ]
8915 ),
8916 ARRAY_A
8917 );
8918
8919 $service_result = $wpdb->get_row(
8920 $wpdb->prepare(
8921 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
8922 [ $result['serviceId'] ]
8923 ),
8924 ARRAY_A
8925 );
8926
8927 $category_result = $wpdb->get_row(
8928 $wpdb->prepare(
8929 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
8930 [ $service_result['categoryId'] ]
8931 ),
8932 ARRAY_A
8933 );
8934
8935 if ( $result['couponId'] ) {
8936 $coupon_result = $wpdb->get_row(
8937 $wpdb->prepare(
8938 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8939 [ $result['couponId'] ]
8940 ),
8941 ARRAY_A
8942 );
8943 } else {
8944 $coupon_result = [];
8945 }
8946
8947 if ( ! empty( $result['customFields'] ) ) {
8948 $custom_fields = json_decode( $result['customFields'], true );
8949
8950 $fields_arr = [];
8951 foreach ( (array) $custom_fields as $fields ) {
8952 if ( is_array( $fields ) ) {
8953 $fields_arr[ $fields['label'] ] = $fields['value'];
8954 }
8955 }
8956 unset( $result['customFields'] );
8957 } else {
8958 $fields_arr = [];
8959 }
8960
8961 $appointment_data['isRescheduled'] = '1';
8962 $context['pluggable_data'] = array_merge( $result, $fields_arr, $appointment_data, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
8963 $context['response_type'] = 'live';
8964 } else {
8965
8966 $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 );
8967 }
8968
8969 return $context;
8970 }
8971
8972
8973 /**
8974 * Get last data for trigger.
8975 *
8976 * @param array $data data.
8977 * @return array
8978 */
8979 public function search_amelia_appointment_cancelled_triggers_last_data( $data ) {
8980 global $wpdb;
8981
8982 $context = [];
8983
8984 $appointment_category = $data['filter']['amelia_category_list']['value'];
8985 $appointment_service = $data['filter']['amelia_service_list']['value'];
8986
8987 if ( -1 === $appointment_service ) {
8988 // If service exists as per category selected.
8989 $service_exist = $wpdb->get_row(
8990 $wpdb->prepare(
8991 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
8992 [ $appointment_category ]
8993 ),
8994 ARRAY_A
8995 );
8996
8997 if ( empty( $service_exist ) ) {
8998 $result = [];
8999 } else {
9000 $result = $wpdb->get_row(
9001 $wpdb->prepare(
9002 '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',
9003 [ 'canceled' ]
9004 ),
9005 ARRAY_A
9006 );
9007 }
9008 } else {
9009 $result = $wpdb->get_row(
9010 $wpdb->prepare(
9011 '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',
9012 [ 'canceled', $appointment_service ]
9013 ),
9014 ARRAY_A
9015 );
9016 }
9017
9018 if ( ! empty( $result ) ) {
9019
9020 $payment_result = $wpdb->get_row(
9021 $wpdb->prepare(
9022 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
9023 [ $result['id'] ]
9024 ),
9025 ARRAY_A
9026 );
9027
9028 $customer_result = $wpdb->get_row(
9029 $wpdb->prepare(
9030 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
9031 [ $result['customerId'] ]
9032 ),
9033 ARRAY_A
9034 );
9035
9036 $service_result = $wpdb->get_row(
9037 $wpdb->prepare(
9038 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
9039 [ $result['serviceId'] ]
9040 ),
9041 ARRAY_A
9042 );
9043
9044 $category_result = $wpdb->get_row(
9045 $wpdb->prepare(
9046 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
9047 [ $service_result['categoryId'] ]
9048 ),
9049 ARRAY_A
9050 );
9051
9052 if ( $result['couponId'] ) {
9053 $coupon_result = $wpdb->get_row(
9054 $wpdb->prepare(
9055 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9056 [ $result['couponId'] ]
9057 ),
9058 ARRAY_A
9059 );
9060 } else {
9061 $coupon_result = [];
9062 }
9063
9064 if ( ! empty( $result['customFields'] ) ) {
9065 $custom_fields = json_decode( $result['customFields'], true );
9066
9067 $fields_arr = [];
9068 foreach ( (array) $custom_fields as $fields ) {
9069 if ( is_array( $fields ) ) {
9070 $fields_arr[ $fields['label'] ] = $fields['value'];
9071 }
9072 }
9073 unset( $result['customFields'] );
9074 } else {
9075 $fields_arr = [];
9076 }
9077
9078 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
9079 $context['response_type'] = 'live';
9080 } else {
9081
9082 $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 );
9083 }
9084
9085 return $context;
9086 }
9087
9088 /**
9089 * Get MailPoet Forms.
9090 *
9091 * @param array $data data.
9092 *
9093 * @return array
9094 */
9095 public function search_mailpoet_forms( $data ) {
9096 if ( ! class_exists( '\MailPoet\API\API' ) ) {
9097 return;
9098 }
9099
9100 global $wpdb;
9101
9102 $page = $data['page'];
9103 $limit = Utilities::get_search_page_limit();
9104 $offset = $limit * ( $page - 1 );
9105
9106 $forms = $wpdb->get_results(
9107 $wpdb->prepare(
9108 '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',
9109 [ 'enabled', $limit, $offset ]
9110 )
9111 );
9112
9113 $form_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9114
9115 $options = [];
9116
9117 if ( ! empty( $forms ) ) {
9118 if ( is_array( $forms ) ) {
9119 foreach ( $forms as $form ) {
9120 $options[] = [
9121 'label' => $form->name,
9122 'value' => $form->id,
9123 ];
9124 }
9125 }
9126 }
9127
9128 return [
9129 'options' => $options,
9130 'hasMore' => $form_count > $limit && $form_count > $offset,
9131 ];
9132
9133 }
9134
9135 /**
9136 * Get MailPoet List.
9137 *
9138 * @param array $data data.
9139 *
9140 * @return array
9141 */
9142 public function search_mailpoet_list( $data ) {
9143 if ( ! class_exists( '\MailPoet\API\API' ) ) {
9144 return;
9145 }
9146
9147 $mailpoet = \MailPoet\API\API::MP( 'v1' );
9148 $lists = $mailpoet->getLists();
9149
9150 $options = [];
9151
9152 if ( ! empty( $lists ) ) {
9153 if ( is_array( $lists ) ) {
9154 foreach ( $lists as $list ) {
9155 $options[] = [
9156 'label' => $list['name'],
9157 'value' => $list['id'],
9158 ];
9159 }
9160 }
9161 }
9162
9163 return [
9164 'options' => $options,
9165 'hasMore' => false,
9166 ];
9167 }
9168
9169 /**
9170 * Get MailPoet Subscriber Status.
9171 *
9172 * @param array $data data.
9173 *
9174 * @return array
9175 */
9176 public function search_mailpoet_subscriber_status( $data ) {
9177 if ( ! class_exists( '\MailPoet\API\API' ) ) {
9178 return;
9179 }
9180
9181 $subscriber_status = [
9182 'subscribed' => 'Subscribed',
9183 'unconfirmed' => 'Unconfirmed',
9184 'unsubscribed' => 'Unsubscribed',
9185 'inactive' => 'Inactive',
9186 'bounced' => 'Bounced',
9187 ];
9188
9189 $options = [];
9190 foreach ( $subscriber_status as $key => $status ) {
9191 $options[] = [
9192 'label' => $status,
9193 'value' => $key,
9194 ];
9195 }
9196
9197 return [
9198 'options' => $options,
9199 'hasMore' => false,
9200 ];
9201 }
9202
9203 /**
9204 * Get MailPoet Subscribers.
9205 *
9206 * @param array $data data.
9207 *
9208 * @return array
9209 */
9210 public function search_mailpoet_subscribers( $data ) {
9211 if ( ! class_exists( '\MailPoet\API\API' ) ) {
9212 return;
9213 }
9214
9215 global $wpdb;
9216
9217 $page = $data['page'];
9218 $limit = Utilities::get_search_page_limit();
9219 $offset = $limit * ( $page - 1 );
9220
9221 $subscribers = $wpdb->get_results(
9222 $wpdb->prepare(
9223 'SELECT SQL_CALC_FOUND_ROWS id,email FROM ' . $wpdb->prefix . 'mailpoet_subscribers ORDER BY id DESC LIMIT %d OFFSET %d',
9224 [ $limit, $offset ]
9225 )
9226 );
9227
9228 $subscribers_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9229
9230 $options = [];
9231
9232 if ( ! empty( $subscribers ) ) {
9233 if ( is_array( $subscribers ) ) {
9234 foreach ( $subscribers as $subscriber ) {
9235 $options[] = [
9236 'label' => $subscriber->email,
9237 'value' => $subscriber->id,
9238 ];
9239 }
9240 }
9241 }
9242
9243 return [
9244 'options' => $options,
9245 'hasMore' => $subscribers_count > $limit && $subscribers_count > $offset,
9246 ];
9247 }
9248
9249 /**
9250 * Get ConvertPro Forms.
9251 *
9252 * @param array $data data.
9253 *
9254 * @return array
9255 */
9256 public function search_convertpro_form_list( $data ) {
9257 if ( ! class_exists( '\Cp_V2_Loader' ) ) {
9258 return;
9259 }
9260
9261 $cp_popups_inst = CP_V2_Popups::get_instance();
9262 $popups = $cp_popups_inst->get_all();
9263
9264 $form_count = count( $popups );
9265
9266 $page = $data['page'];
9267 $limit = Utilities::get_search_page_limit();
9268 $offset = $limit * ( $page - 1 );
9269
9270 $options = [];
9271
9272 if ( ! empty( $popups ) ) {
9273 if ( is_array( $popups ) ) {
9274 foreach ( $popups as $form ) {
9275 $options[] = [
9276 'label' => $form->post_title,
9277 'value' => $form->ID,
9278 ];
9279 }
9280 }
9281 }
9282
9283 return [
9284 'options' => $options,
9285 'hasMore' => $form_count > $limit && $form_count > $offset,
9286 ];
9287
9288 }
9289
9290 /**
9291 * Get ProjectHuddle Websites.
9292 *
9293 * @param array $data data.
9294 *
9295 * @return array
9296 */
9297 public function search_project_huddle_websites( $data ) {
9298
9299 $sites = new WP_Query(
9300 [
9301 'post_type' => 'ph-website',
9302 'posts_per_page' => - 1,
9303 'fields' => 'ids',
9304 ]
9305 );
9306
9307 $site_ids = (array) $sites->posts;
9308
9309 $options = [];
9310 if ( ! empty( $site_ids ) ) {
9311 if ( is_array( $site_ids ) ) {
9312 foreach ( $site_ids as $site_id ) {
9313 $options[] = [
9314 'label' => get_the_title( $site_id ),
9315 'value' => $site_id,
9316 ];
9317 }
9318 }
9319 }
9320 return [
9321 'options' => $options,
9322 'hasMore' => false,
9323 ];
9324
9325 }
9326
9327 /**
9328 * Get last data for trigger.
9329 *
9330 * @param array $data data.
9331 * @return mixed
9332 */
9333 public function search_project_huddle_comment_triggers_last_data( $data ) {
9334 global $wpdb;
9335
9336 $context = [];
9337 if ( ! class_exists( 'PH\Models\Post' ) || ! function_exists( 'ph_get_the_title' ) ) {
9338 return [];
9339 }
9340
9341 if ( -1 !== $data['dynamic'] ) {
9342 $threads = get_posts(
9343 [
9344 'post_type' => 'phw_comment_loc',
9345 'posts_per_page' => 1,
9346 'meta_value' => $data['dynamic'],
9347 'meta_key' => 'project_id',
9348 'orderby' => 'asc',
9349 ]
9350 );
9351 } else {
9352 $threads = [];
9353 }
9354
9355 if ( ! empty( $threads ) ) {
9356 $comment_result = $wpdb->get_row(
9357 $wpdb->prepare(
9358 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
9359 FROM ' . $wpdb->prefix . 'comments
9360 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
9361 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
9362 LIMIT 0,1',
9363 $threads[0]->ID
9364 ),
9365 ARRAY_A
9366 );
9367
9368 if ( ! empty( $comment_result ) ) {
9369 $comment_id = get_comment( $comment_result['comment_ID'], ARRAY_A );
9370 $comments['comment_ID'] = isset( $comment_id['comment_ID'] ) ? $comment_id['comment_ID'] : '';
9371 $comments['comment_post_ID'] = isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : '';
9372
9373 $comments['comment_author'] = isset( $comment_id['comment_author'] ) ? $comment_id['comment_author'] : '';
9374
9375 $comments['comment_author_email'] = isset( $comment_id['comment_author_email'] ) ? $comment_id['comment_author_email'] : '';
9376
9377 $comments['comment_date'] = isset( $comment_id['comment_date'] ) ? $comment_id['comment_date'] : '';
9378
9379 $comments['comment_content'] = isset( $comment_id['comment_content'] ) ? $comment_id['comment_content'] : '';
9380
9381 $comments['comment_type'] = isset( $comment_id['comment_type'] ) ? $comment_id['comment_type'] : '';
9382
9383 $comment_item_id = get_comment_meta( $comment_result['comment_ID'], 'item_id' );
9384 if ( ! empty( $comment_item_id ) && is_array( $comment_item_id ) ) {
9385 $comments['comment_item_id'] = $comment_item_id[0];
9386 $comments['comment_item_page_title'] = get_the_title( (int) $comment_item_id[0] );
9387 $comments['comment_item_page_url'] = get_post_meta( (int) $comment_item_id[0], 'page_url', true );
9388 }
9389 if ( isset( $comment_id['comment_post_ID'] ) || isset( $comment_id['comment_author'] ) ) {
9390 $comments['ph_project_name'] = ph_get_the_title( Post::get( $comment_id['comment_post_ID'] )->parentsIds()['project'] );
9391 $comments['ph_commenter_name'] = $comment_id['comment_author'];
9392 $comments['ph_project_type'] = ( get_post_type( $comment_id['comment_post_ID'] ) == 'ph-website' ) ? __( 'Website', 'suretriggers' ) : __( 'Mockup', 'suretriggers' );
9393 $comments['ph_action_status'] = get_post_meta( $comment_id['comment_post_ID'], 'resolved', true ) ? __( 'Resolved', 'suretriggers' ) : __( 'Unresolved', 'suretriggers' );
9394 $comments['ph_project_link'] = get_the_guid( $comment_id['comment_post_ID'] );
9395 }
9396
9397 $context['pluggable_data'] = $comments;
9398 $context['response_type'] = 'live';
9399 } else {
9400 $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 );
9401 }
9402 } else {
9403 $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 );
9404 }
9405
9406 return $context;
9407 }
9408
9409 /**
9410 * Get last data for trigger.
9411 *
9412 * @param array $data data.
9413 * @return mixed
9414 */
9415 public function search_project_huddle_resolved_comment_triggers_last_data( $data ) {
9416 global $wpdb;
9417
9418 $context = [];
9419 if ( ! class_exists( 'PH\Models\Post' ) || ! function_exists( 'ph_get_the_title' ) ) {
9420 return [];
9421 }
9422
9423 $get_comments = $wpdb->get_row(
9424 'SELECT ' . $wpdb->prefix . 'comments.comment_ID, ' . $wpdb->prefix . 'comments.comment_content
9425 FROM ' . $wpdb->prefix . 'comments
9426 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_status") AND comment_content = "Resolved"
9427 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
9428 LIMIT 0,1'
9429 );
9430
9431 if ( ! empty( $get_comments ) ) {
9432 $comment_id = get_comment( $get_comments->comment_ID, ARRAY_A );
9433 $comment_result = $wpdb->get_row(
9434 $wpdb->prepare(
9435 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
9436 FROM ' . $wpdb->prefix . 'comments
9437 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
9438 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
9439 LIMIT 0,1',
9440 isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : ''
9441 ),
9442 ARRAY_A
9443 );
9444
9445 $actual_comment = get_comment( $comment_result['comment_ID'], ARRAY_A );
9446 $comments['comment_ID'] = isset( $actual_comment['comment_ID'] ) ? $actual_comment['comment_ID'] : '';
9447 $comments['comment_post_ID'] = isset( $actual_comment['comment_post_ID'] ) ? $actual_comment['comment_post_ID'] : '';
9448 $comments['comment_author'] = isset( $actual_comment['comment_author'] ) ? $actual_comment['comment_author'] : '';
9449 $comments['comment_author_email'] = isset( $actual_comment['comment_author_email'] ) ? $actual_comment['comment_author_email'] : '';
9450 $comments['comment_date'] = isset( $actual_comment['comment_date'] ) ? $actual_comment['comment_date'] : '';
9451 $comments['comment_content'] = isset( $actual_comment['comment_content'] ) ? $actual_comment['comment_content'] : '';
9452 $comments['comment_type'] = isset( $actual_comment['comment_type'] ) ? $actual_comment['comment_type'] : '';
9453 $comments['comment_status'] = $get_comments->comment_content;
9454 $comment_item_id = get_comment_meta( $comment_result['comment_ID'], 'item_id' );
9455 if ( ! empty( $comment_item_id ) && is_array( $comment_item_id ) ) {
9456 $comments['comment_item_id'] = $comment_item_id[0];
9457 $comments['comment_item_page_title'] = get_the_title( (int) $comment_item_id[0] );
9458 $comments['comment_item_page_url'] = get_post_meta( (int) $comment_item_id[0], 'page_url', true );
9459 }
9460 if ( isset( $comment_id['comment_post_ID'] ) || isset( $comment_id['comment_author'] ) ) {
9461 $comments['ph_project_name'] = ph_get_the_title( Post::get( $comment_id['comment_post_ID'] )->parentsIds()['project'] );
9462 $comments['ph_commenter_name'] = $comment_id['comment_author'];
9463 $comments['ph_project_type'] = ( get_post_type( $comment_id['comment_post_ID'] ) == 'ph-website' ) ? __( 'Website', 'suretriggers' ) : __( 'Mockup', 'suretriggers' );
9464 $comments['ph_action_status'] = get_post_meta( $comment_id['comment_post_ID'], 'resolved', true ) ? __( 'Resolved', 'suretriggers' ) : __( 'Unresolved', 'suretriggers' );
9465 $comments['ph_project_link'] = get_the_guid( $comment_id['comment_post_ID'] );
9466 }
9467 $context['pluggable_data'] = $comments;
9468 $context['response_type'] = 'live';
9469 } else {
9470 $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 );
9471 }
9472
9473 return $context;
9474 }
9475
9476 /**
9477 * Get MasterStudy LMS Courses.
9478 *
9479 * @param array $data data.
9480 *
9481 * @return array
9482 */
9483 public function search_ms_lms_courses( $data ) {
9484
9485 $page = $data['page'];
9486 $limit = Utilities::get_search_page_limit();
9487 $offset = $limit * ( $page - 1 );
9488
9489 $args = [
9490 'post_type' => 'stm-courses',
9491 'posts_per_page' => $limit,
9492 'offset' => $offset,
9493 'orderby' => 'title',
9494 'order' => 'ASC',
9495 'post_status' => 'publish',
9496 ];
9497
9498 $courses = get_posts( $args );
9499
9500 $course_count = wp_count_posts( 'stm-courses' )->publish;
9501
9502 $options = [];
9503 if ( ! empty( $courses ) ) {
9504 if ( is_array( $courses ) ) {
9505 foreach ( $courses as $course ) {
9506 $options[] = [
9507 'label' => $course->post_title,
9508 'value' => $course->ID,
9509 ];
9510 }
9511 }
9512 }
9513 return [
9514 'options' => $options,
9515 'hasMore' => $course_count > $limit && $course_count > $offset,
9516 ];
9517
9518 }
9519
9520 /**
9521 * Get MasterStudy LMS Lessons.
9522 *
9523 * @param array $data data.
9524 *
9525 * @return array
9526 */
9527 public function search_ms_lms_lessons( $data ) {
9528
9529 global $wpdb;
9530 $options = [];
9531 $course_id = $data['dynamic'];
9532
9533 // Use curriculum repository class.
9534 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
9535 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
9536 } else {
9537 $curriculum_repo = false;
9538 }
9539
9540 if ( '-1' === $course_id ) {
9541 $lessons = $wpdb->get_results(
9542 $wpdb->prepare(
9543 "SELECT SQL_CALC_FOUND_ROWS ID, post_title FROM $wpdb->posts WHERE post_type = %s ORDER BY post_title ASC",
9544 'stm-lessons'
9545 )
9546 );
9547
9548 if ( ! empty( $lessons ) ) {
9549 if ( is_array( $lessons ) ) {
9550 foreach ( $lessons as $lesson ) {
9551 $options[] = [
9552 'label' => $lesson->post_title,
9553 'value' => $lesson->ID,
9554 ];
9555 }
9556 }
9557 }
9558 } else {
9559 $lessons = [];
9560 if ( $curriculum_repo ) {
9561 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
9562 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
9563 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
9564 foreach ( $curriculum['materials'] as $material ) {
9565 if ( 'stm-lessons' === $material['post_type'] ) {
9566 $lessons[] = [
9567 'value' => $material['post_id'],
9568 'text' => $material['title'],
9569 ];
9570 }
9571 }
9572 }
9573 }
9574 if ( ! empty( $lessons ) ) {
9575 if ( is_array( $lessons ) ) {
9576 foreach ( $lessons as $lesson ) {
9577 $options[] = [
9578 'label' => $lesson['text'],
9579 'value' => $lesson['value'],
9580 ];
9581 }
9582 }
9583 }
9584 } else {
9585 $lessons = $wpdb->get_results(
9586 $wpdb->prepare(
9587 "SELECT SQL_CALC_FOUND_ROWS ID, post_title
9588 FROM $wpdb->posts
9589 WHERE FIND_IN_SET(
9590 ID,
9591 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
9592 )
9593 AND post_type = 'stm-lessons'
9594 ORDER BY post_title ASC",
9595 absint( $course_id )
9596 )
9597 );
9598 if ( ! empty( $lessons ) ) {
9599 if ( is_array( $lessons ) ) {
9600 foreach ( $lessons as $lesson ) {
9601 $options[] = [
9602 'label' => $lesson->post_title,
9603 'value' => $lesson->ID,
9604 ];
9605 }
9606 }
9607 }
9608 }
9609 }
9610 return [
9611 'options' => $options,
9612 'hasMore' => false,
9613 ];
9614
9615 }
9616
9617 /**
9618 * Get MasterStudy LMS Quiz.
9619 *
9620 * @param array $data data.
9621 *
9622 * @return array
9623 */
9624 public function search_ms_lms_quiz( $data ) {
9625
9626 global $wpdb;
9627
9628 $options = [];
9629 $course_id = $data['dynamic'];
9630
9631 // Use curriculum repository class.
9632 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
9633 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
9634 } else {
9635 $curriculum_repo = false;
9636 }
9637
9638 if ( '-1' === $course_id ) {
9639 $quizzes = $wpdb->get_results(
9640 $wpdb->prepare(
9641 "SELECT ID, post_title
9642 FROM $wpdb->posts
9643 WHERE post_type = %s
9644 ORDER BY post_title ASC",
9645 'stm-quizzes'
9646 )
9647 );
9648 if ( ! empty( $quizzes ) ) {
9649 if ( is_array( $quizzes ) ) {
9650 foreach ( $quizzes as $quiz ) {
9651 $options[] = [
9652 'label' => $quiz->post_title,
9653 'value' => $quiz->ID,
9654 ];
9655 }
9656 }
9657 }
9658 } else {
9659 $quizzes = [];
9660 if ( $curriculum_repo ) {
9661 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
9662 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
9663 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
9664 foreach ( $curriculum['materials'] as $material ) {
9665 if ( 'stm-quizzes' === $material['post_type'] ) {
9666 $quizzes[] = [
9667 'value' => $material['post_id'],
9668 'text' => $material['title'],
9669 ];
9670 }
9671 }
9672 }
9673 }
9674 if ( ! empty( $quizzes ) ) {
9675 if ( is_array( $quizzes ) ) {
9676 foreach ( $quizzes as $quiz ) {
9677 $options[] = [
9678 'label' => $quiz['text'],
9679 'value' => $quiz['value'],
9680 ];
9681 }
9682 }
9683 }
9684 } else {
9685 $quizzes = $wpdb->get_results(
9686 $wpdb->prepare(
9687 "SELECT ID, post_title
9688 FROM $wpdb->posts
9689 WHERE FIND_IN_SET(
9690 ID,
9691 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
9692 )
9693 AND post_type = 'stm-quizzes'
9694 ORDER BY post_title ASC
9695 ",
9696 absint( $course_id )
9697 )
9698 );
9699 if ( ! empty( $quizzes ) ) {
9700 if ( is_array( $quizzes ) ) {
9701 foreach ( $quizzes as $quiz ) {
9702 $options[] = [
9703 'label' => $quiz->post_title,
9704 'value' => $quiz->ID,
9705 ];
9706 }
9707 }
9708 }
9709 }
9710 }
9711 return [
9712 'options' => $options,
9713 'hasMore' => false,
9714 ];
9715
9716 }
9717
9718 /**
9719 * Search MasterStudy LMS data.
9720 *
9721 * @param array $data data.
9722 * @return array|void
9723 */
9724 public function search_ms_lms_last_data( $data ) {
9725 global $wpdb;
9726 $post_type = $data['post_type'];
9727 $trigger = $data['search_term'];
9728 $context = [];
9729
9730 if ( 'stm_lms_course_completed' === $trigger ) {
9731 $post_id = $data['filter']['course']['value'];
9732 if ( -1 === $post_id ) {
9733 $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 ) );
9734 } else {
9735 $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 ) );
9736 }
9737 } elseif ( 'stm_lesson_passed' === $trigger ) {
9738 $post_id = $data['filter']['lesson']['value'];
9739 if ( -1 === $post_id ) {
9740 $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 ) );
9741 } else {
9742 $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 ) );
9743 }
9744 } elseif ( 'stm_quiz_passed' === $trigger ) {
9745 $post_id = $data['filter']['quiz']['value'];
9746 if ( -1 === $post_id ) {
9747 $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 ) );
9748 } else {
9749 $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 ) );
9750 }
9751 } elseif ( 'stm_quiz_failed' === $trigger ) {
9752 $post_id = $data['filter']['quiz']['value'];
9753 if ( -1 === $post_id ) {
9754 $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 ) );
9755 } else {
9756 $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 ) );
9757 }
9758 } elseif ( 'stm_lms_user_enroll_course' === $trigger ) {
9759 $post_id = $data['filter']['course']['value'];
9760 if ( -1 === $post_id ) {
9761 $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 ) );
9762 } else {
9763 $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 ) );
9764 }
9765 }
9766
9767 if ( ! empty( $result ) ) {
9768
9769 switch ( $trigger ) {
9770 case 'stm_lms_course_completed':
9771 $result_course_id = $result[0]->course_id;
9772 $result_user_id = $result[0]->user_id;
9773 $course = get_the_title( $result_course_id );
9774 $course_link = get_the_permalink( $result_course_id );
9775 $featured_image = get_the_post_thumbnail_url( $result_course_id );
9776
9777 $data = [
9778 'course_id' => $result_course_id,
9779 'course_title' => $course,
9780 'course_link' => $course_link,
9781 'course_featured_image' => $featured_image,
9782 'course_progress' => $result[0]->progress_percent,
9783 ];
9784 $context_data = array_merge(
9785 WordPress::get_user_context( $result_user_id ),
9786 $data
9787 );
9788 break;
9789 case 'stm_lesson_passed':
9790 $result_lesson_id = $result[0]->lesson_id;
9791 $result_user_id = $result[0]->user_id;
9792 $lesson = get_the_title( $result_lesson_id );
9793 $lesson_link = get_the_permalink( $result_lesson_id );
9794
9795 $data = [
9796 'lesson_id' => $result_lesson_id,
9797 'lesson_title' => $lesson,
9798 'lesson_link' => $lesson_link,
9799 ];
9800 $context_data = array_merge(
9801 WordPress::get_user_context( $result_user_id ),
9802 $data
9803 );
9804 break;
9805 case 'stm_quiz_passed':
9806 $result_quiz_id = $result[0]->quiz_id;
9807 $result_user_id = $result[0]->user_id;
9808 $quiz_title = get_the_title( $result_quiz_id );
9809 $quiz_link = get_the_permalink( $result_quiz_id );
9810
9811 $data = [
9812 'quiz_id' => $result_quiz_id,
9813 'quiz_title' => $quiz_title,
9814 'quiz_link' => $quiz_link,
9815 'quiz_score' => $result[0]->progress,
9816 'result' => 'passed',
9817 ];
9818 $context_data = array_merge(
9819 WordPress::get_user_context( $result_user_id ),
9820 $data
9821 );
9822 break;
9823 case 'stm_quiz_failed':
9824 $result_quiz_id = $result[0]->quiz_id;
9825 $result_user_id = $result[0]->user_id;
9826 $quiz_title = get_the_title( $result_quiz_id );
9827 $quiz_link = get_the_permalink( $result_quiz_id );
9828
9829 $data = [
9830 'quiz_id' => $result_quiz_id,
9831 'quiz_title' => $quiz_title,
9832 'quiz_link' => $quiz_link,
9833 'quiz_score' => $result[0]->progress,
9834 'result' => 'failed',
9835 ];
9836 $context_data = array_merge(
9837 WordPress::get_user_context( $result_user_id ),
9838 $data
9839 );
9840 break;
9841 case 'stm_lms_user_enroll_course':
9842 $result_course_id = $result[0]->course_id;
9843 $result_user_id = $result[0]->user_id;
9844
9845 $course = get_the_title( $result_course_id );
9846 $course_link = get_the_permalink( $result_course_id );
9847 $featured_image = get_the_post_thumbnail_url( $result_course_id );
9848
9849 $data = [
9850 'course_id' => $result_course_id,
9851 'course_title' => $course,
9852 'course_link' => $course_link,
9853 'course_featured_image' => $featured_image,
9854 ];
9855 $context_data = array_merge(
9856 WordPress::get_user_context( $result_user_id ),
9857 $data
9858 );
9859 break;
9860 default:
9861 return;
9862 }
9863 $context['pluggable_data'] = $context_data;
9864 $context['response_type'] = 'live';
9865 }
9866
9867 return $context;
9868
9869 }
9870
9871 /**
9872 * Prepare Fluent Support Mailbox list.
9873 *
9874 * @param array $data data.
9875 *
9876 * @return array
9877 */
9878 public function search_fs_mailbox_list( $data ) {
9879 $options = [];
9880 global $wpdb;
9881 $mailboxes = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_mail_boxes ORDER BY id DESC", ARRAY_A );
9882 if ( ! empty( $mailboxes ) ) {
9883 foreach ( $mailboxes as $key => $value ) {
9884
9885 $options[] = [
9886 'label' => $value['name'],
9887 'value' => $value['id'],
9888 ];
9889
9890 }
9891 }
9892
9893 return [
9894 'options' => $options,
9895 'hasMore' => false,
9896 ];
9897 }
9898
9899 /**
9900 * Prepare Fluent Support Product list.
9901 *
9902 * @param array $data data.
9903 *
9904 * @return array
9905 */
9906 public function search_fs_product_list( $data ) {
9907 $options = [];
9908
9909 if ( ! class_exists( '\FluentSupport\App\Models\Product' ) ) {
9910 return [];
9911 }
9912
9913 $products = \FluentSupport\App\Models\Product::select( [ 'id', 'title' ] )->get();
9914 if ( ! empty( $products ) ) {
9915 foreach ( $products as $key => $product ) {
9916 $options[] = [
9917 'label' => $product['title'],
9918 'value' => $product['id'],
9919 ];
9920 }
9921 }
9922
9923 return [
9924 'options' => $options,
9925 'hasMore' => false,
9926 ];
9927 }
9928
9929 /**
9930 * Prepare Fluent Support Product list.
9931 *
9932 * @param array $data data.
9933 *
9934 * @return array
9935 */
9936 public function search_fs_person_type_list( $data ) {
9937 $options = [
9938 [
9939 'label' => 'Customer',
9940 'value' => 'customer',
9941 ],
9942 [
9943 'label' => 'Agent',
9944 'value' => 'agent',
9945 ],
9946 ];
9947
9948 return [
9949 'options' => $options,
9950 'hasMore' => false,
9951 ];
9952 }
9953
9954 /**
9955 * Get last data for trigger.
9956 *
9957 * @param array $data data.
9958 * @return array
9959 */
9960 public function search_fluent_support_triggers_last_data( $data ) {
9961 global $wpdb;
9962 $context = [];
9963 $context['response_type'] = 'sample';
9964
9965 if ( ! class_exists( '\FluentSupport\App\Models\Ticket' ) ) {
9966 return [];
9967 }
9968
9969 $ticket_data = [
9970 'id' => '1',
9971 'customer_id' => '2',
9972 'agent_id' => '3',
9973 'product_id' => '5',
9974 'product_source' => 'local',
9975 'privacy' => 'private',
9976 'priority' => 'normal',
9977 'client_priority' => 'medium',
9978 'status' => 'active',
9979 'title' => 'Sample Ticket Title',
9980 'slug' => 'sample-ticket-title',
9981 'hash' => 'f8a8cfb946',
9982 'content_hash' => 'd65500d62621be8b493c22b1d888052c',
9983 'content' => '<p>Sample content.</p>',
9984 'last_customer_response' => '2023-04-27 07:30:46',
9985 'waiting_since' => '2023-04-27 07:30:46',
9986 'response_count' => '2',
9987 'total_close_time' => '7042',
9988 'resolved at' => '2023-04-27 09:28:08',
9989 'closed_by' => '1',
9990 'created_at' => '2023-04-27 07:30:46',
9991 'updated_at' => '2023-04-27 10:28:08',
9992 'mailbox_id' => '1',
9993 'mailbox_name' => 'SureTriggers',
9994 'mailbox_slug' => 'suretriggers',
9995 'mailbox_box_type' => 'web',
9996 'mailbox email' => 'john_doe@sample.com',
9997 'mailbox_settings_admin_email_address' => 'john_doe@sample.com',
9998 'mailbox_created_by' => '1',
9999 'mailbox_is_default' => 'yes',
10000 'mailbox_created_at' => '2023-04-26 06:29:01',
10001 'mailbox_updated_at' => '2023-04-26 06:29:01',
10002 ];
10003
10004 $customer_data = [
10005 'id' => '1',
10006 'first_name' => 'John',
10007 'last_name' => 'Doe',
10008 'email' => 'john_doe@sample.com',
10009 'person_type' => 'agent',
10010 'status' => 'active',
10011 'hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
10012 'user_id' => '1',
10013 'created_at' => '2023-04-27 07:30:46',
10014 'updated_at' => '2023-04-27 10:28:08',
10015 'full_name' => 'John Doe',
10016 'photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
10017 ];
10018
10019 $reply_data = [
10020 'ticket_id' => '1',
10021 'conversation_type' => 'response',
10022 'content' => '<p>Sample content.</p>',
10023 'source' => 'web',
10024 'content_hash' => '2cc0e35d8fb92a0675d67999b073b3a4',
10025 'created_at' => '2023-04-27 07:30:46',
10026 'updated_at' => '2023-04-27 10:28:08',
10027 'id' => '1',
10028 'person_id' => '2',
10029 'person_first_name' => 'John',
10030 'person_last_name' => 'Doe',
10031 'person_email' => 'john_doe@sample.com',
10032 'person_person_type' => 'agent',
10033 'person_status' => 'active',
10034 'person_hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
10035 'person_user_id' => '1',
10036 'person_created_at' => '2023-04-27 07:30:46',
10037 'person_updated_at' => '2023-04-27 10:28:08',
10038 'person_full_name' => 'John Doe',
10039 'person_photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
10040 ];
10041
10042 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
10043 $mailbox = isset( $data['mailbox_id'] ) ? $data['mailbox_id'] : '';
10044 $product_id = isset( $data['filter']['ticket_product_id']['value'] ) ? $data['filter']['ticket_product_id']['value'] : '';
10045 $person_id = isset( $data['filter']['person_id']['value'] ) ? (string) $data['filter']['person_id']['value'] : '';
10046
10047 if ( in_array( $term, [ 'response_added_by_agent', 'response_added_by_customer', 'ticket_replied_product_agent', 'ticket_replied_product_customer' ], true ) ) {
10048 if ( 'response_added_by_agent' == $term ) {
10049 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
10050 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
10051 $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 ) );
10052 if ( ! empty( $tickets ) ) {
10053 $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 ) );
10054 $ticket_data = $tickets[0];
10055 $customer_data = $customers[0];
10056 $reply_data = $replies[0];
10057 $context['response_type'] = 'live';
10058 }
10059 } elseif ( 'response_added_by_customer' == $term ) {
10060 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
10061 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
10062 $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 ) );
10063 if ( ! empty( $tickets ) ) {
10064 $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 ) );
10065 $ticket_data = $tickets[0];
10066 $customer_data = $customers[0];
10067 $reply_data = $replies[0];
10068 $context['response_type'] = 'live';
10069 }
10070 } elseif ( 'ticket_replied_product_agent' == $term ) {
10071 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
10072 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
10073 if ( -1 == $product_id ) {
10074 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
10075 $products = array_map(
10076 function( $product ) {
10077 return $product['id'];
10078 },
10079 $products
10080 );
10081 $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 ) ) );
10082 } else {
10083 $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 ) );
10084 }
10085 if ( ! empty( $tickets ) ) {
10086 $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 ) );
10087 $ticket_data = $tickets[0];
10088 $customer_data = $customers[0];
10089 $reply_data = $replies[0];
10090 $context['response_type'] = 'live';
10091 }
10092 } elseif ( 'ticket_replied_product_customer' == $term ) {
10093 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
10094 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
10095 if ( -1 == $product_id ) {
10096 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
10097 $products = array_map(
10098 function( $product ) {
10099 return $product['id'];
10100 },
10101 $products
10102 );
10103 $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 ) ) );
10104 } else {
10105 $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 ) );
10106 }
10107 if ( ! empty( $tickets ) ) {
10108 $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 ) );
10109 $ticket_data = $tickets[0];
10110 $customer_data = $customers[0];
10111 $reply_data = $replies[0];
10112 $context['response_type'] = 'live';
10113 }
10114 }
10115 $context['pluggable_data'] = array_merge(
10116 [
10117 'reply' => $reply_data,
10118 'ticket' => $ticket_data,
10119 'customer' => $customer_data,
10120 ]
10121 );
10122 if ( 'ticket_replied_product_customer' == $term || 'ticket_replied_product_agent' == $term ) {
10123 $context['pluggable_data']['ticket_product_id'] = $product_id;
10124 }
10125 if ( is_object( $ticket_data ) ) {
10126 $ticket_data = get_object_vars( $ticket_data );
10127 }
10128 $ticket = \FluentSupport\App\Models\Ticket::find( $ticket_data['id'] );
10129 if ( is_object( $ticket ) && method_exists( $ticket, 'customData' ) ) {
10130 $context['pluggable_data']['custom_fields'] = $ticket->customData();
10131 }
10132 $context['pluggable_data']['ticket_link'] = admin_url( "admin.php?page=fluent-support#/tickets/{$ticket_data['id']}/view" );
10133 } else {
10134 if ( 'ticket_created' == $term ) {
10135 if ( -1 == $mailbox ) {
10136 $tickets = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE status = 'new' ORDER BY id DESC LIMIT 1" );
10137 } else {
10138 $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 ) );
10139 }
10140 if ( ! empty( $tickets ) ) {
10141 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d", $tickets[0]->customer_id ) );
10142 $ticket_data = $tickets[0];
10143 $customer_data = $customers[0];
10144 }
10145 } elseif ( 'ticket_closed_by_customer' == $term ) {
10146 $customers_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
10147 $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] ) );
10148 if ( ! empty( $tickets ) ) {
10149 $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 ) );
10150 $ticket_data = $tickets[0];
10151 $customer_data = $customers[0];
10152 }
10153 } elseif ( 'ticket_closed_by_agent' == $term ) {
10154 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
10155 $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] ) );
10156 if ( ! empty( $tickets ) ) {
10157 $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 ) );
10158 $ticket_data = $tickets[0];
10159 $customer_data = $customers[0];
10160 }
10161 } elseif ( 'ticket_created_product' == $term ) {
10162 $agents_type = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_persons" );
10163 if ( -1 == $product_id ) {
10164 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
10165 $products = array_map(
10166 function( $product ) {
10167 return $product['id'];
10168 },
10169 $products
10170 );
10171 $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 ) ) );
10172 } else {
10173 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id = %d ORDER BY id DESC LIMIT 1", $product_id ) );
10174 }
10175 if ( ! empty( $tickets ) ) {
10176 $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 ) );
10177 $ticket_data = $tickets[0];
10178 $customer_data = $customers[0];
10179 }
10180 } elseif ( 'ticket_closed_product' == $term ) {
10181 $agents_type = $wpdb->get_results( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = %s", $person_id ), ARRAY_A );
10182 $agents = array_map(
10183 function( $agent ) {
10184 return $agent['id'];
10185 },
10186 $agents_type
10187 );
10188 if ( -1 == $product_id ) {
10189 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
10190 $products = array_map(
10191 function( $product ) {
10192 return $product['id'];
10193 },
10194 $products
10195 );
10196 $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 ) ) );
10197 } else {
10198 $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 ) ) );
10199 }
10200 if ( ! empty( $tickets ) ) {
10201 $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 ) );
10202 $ticket_data = $tickets[0];
10203 $customer_data = $customers[0];
10204 }
10205 }
10206 $context['pluggable_data'] = array_merge(
10207 [
10208 'ticket' => $ticket_data,
10209 'customer' => $customer_data,
10210 ]
10211 );
10212 if ( 'ticket_created' == $term ) {
10213 $context['pluggable_data']['mailbox_id'] = $mailbox;
10214 } elseif ( 'ticket_created_product' == $term || 'ticket_replied_product_customer' == $term || 'ticket_replied_product_agent' == $term ) {
10215 $context['pluggable_data']['ticket_product_id'] = $product_id;
10216 } elseif ( 'ticket_closed_product' == $term ) {
10217 $context['pluggable_data']['ticket_product_id'] = $product_id;
10218 $context['pluggable_data']['person_id'] = $person_id;
10219 }
10220 if ( is_object( $ticket_data ) ) {
10221 $ticket_data = get_object_vars( $ticket_data );
10222 }
10223 $ticket = \FluentSupport\App\Models\Ticket::find( $ticket_data['id'] );
10224 if ( is_object( $ticket ) && method_exists( $ticket, 'customData' ) ) {
10225 $context['pluggable_data']['custom_fields'] = $ticket->customData();
10226 }
10227 $context['pluggable_data']['ticket_link'] = admin_url( "admin.php?page=fluent-support#/tickets/{$ticket_data['id']}/view" );
10228 $context['response_type'] = 'live';
10229 }
10230
10231 return $context;
10232 }
10233
10234 /**
10235 * Prepare Ultimate Member user_roles.
10236 *
10237 * @param array $data data.
10238 *
10239 * @return array
10240 */
10241 public function search_um_user_roles( $data ) {
10242 if ( function_exists( 'get_editable_roles' ) ) {
10243 $roles = get_editable_roles();
10244 } else {
10245 $roles = wp_roles()->roles;
10246 $roles = apply_filters( 'editable_roles', $roles );
10247 }
10248
10249 $options = [];
10250 foreach ( $roles as $role => $details ) {
10251
10252 $options[] = [
10253 'label' => $details['name'],
10254 'value' => $role,
10255 ];
10256
10257 }
10258
10259 return [
10260 'options' => $options,
10261 'hasMore' => false,
10262 ];
10263 }
10264
10265 /**
10266 * Prepare Ultimate Member forms_list.
10267 *
10268 * @param array $data data.
10269 *
10270 * @return array
10271 */
10272 public function search_um_forms_list( $data ) {
10273
10274 $page = $data['page'];
10275 $limit = Utilities::get_search_page_limit();
10276 $offset = $limit * ( $page - 1 );
10277
10278 $args = [
10279 'posts_per_page' => $limit,
10280 'offset' => $offset,
10281 'orderby' => 'title',
10282 'order' => 'ASC',
10283 'post_type' => 'um_form',
10284 'post_status' => 'publish',
10285 'fields' => 'ids',
10286 ];
10287 if ( 'register' == $data['filter']['type'] ) {
10288 $args['meta_query'] = [
10289 [
10290 'key' => '_um_mode',
10291 'value' => 'register',
10292 'compare' => 'LIKE',
10293 ],
10294 ];
10295 } elseif ( 'login' == $data['filter']['type'] ) {
10296 $args['meta_query'] = [
10297 [
10298 'key' => '_um_mode',
10299 'value' => 'login',
10300 'compare' => 'LIKE',
10301 ],
10302 ];
10303 }
10304
10305 $forms_list = get_posts( $args );
10306
10307 $forms_list_count = wp_count_posts( 'um_form' )->publish;
10308
10309 $options = [];
10310 if ( ! empty( $forms_list ) ) {
10311 foreach ( $forms_list as $form ) {
10312 $title = html_entity_decode( get_the_title( $form ), ENT_QUOTES, 'UTF-8' );
10313 $options[] = [
10314 'label' => $title,
10315 'value' => $form,
10316 ];
10317 }
10318 }
10319
10320 return [
10321 'options' => $options,
10322 'hasMore' => $forms_list_count > $limit && $forms_list_count > $offset,
10323 ];
10324 }
10325
10326 /**
10327 * Get last data for Ultimate Member Login trigger.
10328 *
10329 * @param array $data data.
10330 * @return mixed
10331 */
10332 public function search_ultimate_member_user_logsin( $data ) {
10333 $context = [];
10334 $args = [
10335 'orderby' => 'meta_value',
10336 'meta_key' => '_um_last_login',
10337 'order' => 'DESC',
10338 'number' => 1,
10339 ];
10340 $users = get_users( $args );
10341
10342 if ( ! empty( $users ) ) {
10343 $user = $users[0];
10344 $submitted_data = get_user_meta( $user->ID, 'submitted', true );
10345 if ( is_array( $submitted_data ) ) {
10346 unset(
10347 $submitted_data['user_password'],
10348 $submitted_data['confirm_user_password']
10349 );
10350 }
10351 $pluggable_data = WordPress::get_user_context( $user->ID );
10352 $pluggable_data['data'] = $submitted_data;
10353 $context['pluggable_data'] = $pluggable_data;
10354 $context['response_type'] = 'live';
10355 } else {
10356 $role = 'subscriber';
10357 $context['pluggable_data'] = [
10358 'wp_user_id' => 1,
10359 'user_login' => 'test',
10360 'display_name' => 'Test User',
10361 'user_firstname' => 'Test',
10362 'user_lastname' => 'User',
10363 'user_email' => 'testuser@gmail.com',
10364 'user_role' => [ $role ],
10365 ];
10366 $context['response_type'] = 'sample';
10367 }
10368 return $context;
10369 }
10370
10371 /**
10372 * Get last data for Ultimate Member Register trigger.
10373 *
10374 * @param array $data data.
10375 * @return mixed
10376 */
10377 public function search_ultimate_member_user_registers( $data ) {
10378 $context = [];
10379 $args = [
10380 'orderby' => 'meta_value',
10381 'meta_key' => 'um_user_profile_url_slug_user_login',
10382 'order' => 'DESC',
10383 'number' => 1,
10384 ];
10385 $users = get_users( $args );
10386
10387 if ( ! empty( $users ) ) {
10388 $user = $users[0];
10389 $submitted_data = get_user_meta( $user->ID, 'submitted', true );
10390 if ( is_array( $submitted_data ) ) {
10391 unset(
10392 $submitted_data['user_password'],
10393 $submitted_data['confirm_user_password']
10394 );
10395 }
10396 $pluggable_data = WordPress::get_user_context( $user->ID );
10397 $pluggable_data['data'] = $submitted_data;
10398 $context['pluggable_data'] = $pluggable_data;
10399 $context['response_type'] = 'live';
10400 } else {
10401 $role = 'subscriber';
10402 $context['pluggable_data'] = [
10403 'wp_user_id' => 1,
10404 'user_login' => 'test',
10405 'display_name' => 'Test User',
10406 'user_firstname' => 'Test',
10407 'user_lastname' => 'User',
10408 'user_email' => 'testuser@gmail.com',
10409 'user_role' => [ $role ],
10410 ];
10411 $context['response_type'] = 'sample';
10412 }
10413 return $context;
10414 }
10415
10416 /**
10417 * Get last data for Ultimate Member Register trigger.
10418 *
10419 * @param array $data data.
10420 * @return mixed
10421 */
10422 public function search_ultimate_member_user_inactive( $data ) {
10423 $context = [];
10424 $args = [
10425 'orderby' => 'user_id',
10426 'meta_key' => 'account_status',
10427 'meta_value' => 'inactive',
10428 'order' => 'ASC',
10429 'number' => 1,
10430 ];
10431 $users = get_users( $args );
10432
10433 if ( ! empty( $users ) ) {
10434 $user = $users[0];
10435 $pluggable_data = [];
10436 $pluggable_data[] = WordPress::get_user_context( $user->ID );
10437 $pluggable_data['user_account_status'] = 'inactive';
10438 $context['pluggable_data'] = $pluggable_data;
10439 $context['response_type'] = 'live';
10440 } else {
10441 $role = 'subscriber';
10442 $context['pluggable_data'] = [
10443 'wp_user_id' => 1,
10444 'user_login' => 'test',
10445 'display_name' => 'Test User',
10446 'user_firstname' => 'Test',
10447 'user_lastname' => 'User',
10448 'user_email' => 'testuser@gmail.com',
10449 'user_role' => [ $role ],
10450 'user_account_status' => 'inactive',
10451 ];
10452 $context['response_type'] = 'sample';
10453 }
10454 return $context;
10455 }
10456
10457 /**
10458 * Get last data for Ultimate Member Change Role trigger.
10459 *
10460 * @param array $data data.
10461 * @return mixed
10462 */
10463 public function search_ultimate_member_user_role_change( $data ) {
10464 $context = [];
10465
10466 $role = $data['filter']['role']['value'];
10467
10468 $args = [
10469 'number' => 1,
10470 'role' => $role,
10471 ];
10472 $users = get_users( $args );
10473 shuffle( $users );
10474 if ( ! empty( $users ) ) {
10475 $user = $users[0];
10476 $pluggable_data = WordPress::get_user_context( $user->ID );
10477 $context['pluggable_data'] = $pluggable_data;
10478 $context['response_type'] = 'live';
10479 } else {
10480 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
10481 $context['pluggable_data'] = [
10482 'wp_user_id' => 1,
10483 'user_login' => 'test',
10484 'display_name' => 'Test User',
10485 'user_firstname' => 'Test',
10486 'user_lastname' => 'User',
10487 'user_email' => 'testuser@gmail.com',
10488 'user_role' => [ $role ],
10489 'user_account_status' => 'inactive',
10490 ];
10491 $context['response_type'] = 'sample';
10492 }
10493 return $context;
10494 }
10495
10496 /**
10497 * Get JetEngine WP Posttypes.
10498 *
10499 * @param array $data data.
10500 *
10501 * @return array
10502 */
10503 public function search_je_posttype_list( $data ) {
10504
10505 $post_types = get_post_types( [ 'public' => true ], 'objects' );
10506 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
10507 if ( isset( $post_types['attachment'] ) ) {
10508 unset( $post_types['attachment'] );
10509 }
10510
10511 $options = [];
10512 foreach ( $post_types as $post_type => $details ) {
10513 $options[] = [
10514 'label' => $details->label,
10515 'value' => $post_type,
10516 ];
10517 }
10518
10519 return [
10520 'options' => $options,
10521 'hasMore' => false,
10522 ];
10523 }
10524
10525 /**
10526 * Get JetEngine WP fields.
10527 *
10528 * @param array $data data.
10529 *
10530 * @return array
10531 */
10532 public function search_je_field_list( $data ) {
10533
10534 $post_type = $data['dynamic'];
10535
10536 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
10537
10538 $post_fields = array_filter(
10539 $metaboxes,
10540 function( $metabox ) {
10541 /**
10542 *
10543 * Ignore line
10544 *
10545 * @phpstan-ignore-next-line
10546 */
10547 return 'post' === $metabox['args']['object_type'];
10548 }
10549 );
10550
10551 $post_fields_count = count( $post_fields );
10552
10553 $options = [];
10554 if ( ! empty( $post_fields ) ) {
10555 if ( is_array( $post_fields ) ) {
10556 foreach ( $post_fields as $post_field ) {
10557 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
10558 foreach ( $post_field['meta_fields'] as $meta_field ) {
10559 $options[] = [
10560 'label' => $meta_field['title'],
10561 'value' => $meta_field['name'],
10562 ];
10563 }
10564 }
10565 }
10566 }
10567 }
10568 return [
10569 'options' => $options,
10570 'hasMore' => false,
10571 ];
10572 }
10573
10574 /**
10575 * Search Last Updated Field Data.
10576 *
10577 * @param array $data data.
10578 * @return array
10579 */
10580 public function search_jet_engine_field_data( $data ) {
10581 global $wpdb;
10582
10583 $context = [];
10584
10585 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
10586
10587 $post_type = $data['filter']['wp_post_type']['value'];
10588
10589 if ( -1 === $field ) {
10590 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
10591
10592 $post_fields = array_filter(
10593 $metaboxes,
10594 function( $metabox ) {
10595 /**
10596 *
10597 * Ignore line
10598 *
10599 * @phpstan-ignore-next-line
10600 */
10601 return 'post' === $metabox['args']['object_type'];
10602 }
10603 );
10604
10605 $options = [];
10606 if ( ! empty( $post_fields ) ) {
10607 if ( is_array( $post_fields ) ) {
10608 foreach ( $post_fields as $post_field ) {
10609 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
10610 foreach ( $post_field['meta_fields'] as $meta_field ) {
10611 $options[] = $meta_field['name'];
10612 }
10613 }
10614 }
10615 }
10616 }
10617 $random_key = array_rand( $options );
10618 $random_value = $options[ $random_key ];
10619 $string = '%' . $random_value . '%';
10620 } else {
10621 $string = '%' . $data['filter']['field_id']['value'] . '%';
10622 }
10623
10624 $result = $wpdb->get_results(
10625 $wpdb->prepare(
10626 'SELECT post_id FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key LIKE %s',
10627 [ $string ]
10628 ),
10629 ARRAY_A
10630 );
10631
10632 $ids = [];
10633
10634 if ( ! empty( $result ) ) {
10635 foreach ( $result as $val ) {
10636 $ids[] = $val['post_id'];
10637 }
10638 }
10639
10640 $lastupdated_args = [
10641 'post_type' => $post_type,
10642 'orderby' => 'modified',
10643 'post__in' => $ids,
10644 'posts_per_page' => 1,
10645 ];
10646 $lastupdated_loop = get_posts( $lastupdated_args );
10647
10648 $response = [];
10649 if ( ! empty( $result ) ) {
10650 $context['post'] = $lastupdated_loop[0];
10651
10652 $meta_value = get_post_meta( $lastupdated_loop[0]->ID, sprintf( '%s', $data['filter']['field_id']['value'] ), true );
10653 $meta_key = sprintf( '%s', $data['filter']['field_id']['value'] );
10654
10655 $context[ $meta_key ] = $meta_value;
10656
10657 $response['pluggable_data'] = $context;
10658 $response['response_type'] = 'live';
10659 } else {
10660 $response = json_decode( '{"response_type":"sample","pluggable_data":{"post":{"ID":198,"post_author":"1","post_date":"2023-02-08 13:31:13","post_date_gmt":"2023-02-08 13:31:13","post_content":"New Category1 - content","post_title":"jennjennn - Post - jenn","post_excerpt":"","post_status":"publish","comment_status":"open","ping_status":"open","post_password":"","post_name":"jennjennn-post-jenn","to_ping":"","pinged":"","post_modified":"2023-04-10 06:23:40","post_modified_gmt":"2023-04-10 06:23:40","post_content_filtered":"","post_parent":0,"guid":"https:\/\/suretriggerswp.local\/jennjennn-post-jenn\/","menu_order":0,"post_type":"post","post_mime_type":"","comment_count":"0","filter":"raw"},"enter-post-extra-content-title":"dummy"}}', true );
10661 }
10662
10663 return $response;
10664
10665 }
10666
10667 /**
10668 * Get Formidable Forms.
10669 *
10670 * @param array $data data.
10671 *
10672 * @return array|void
10673 */
10674 public function search_formidable_form_list( $data ) {
10675 if ( ! class_exists( 'FrmForm' ) ) {
10676 return;
10677 }
10678
10679 $page = $data['page'];
10680 $term = $data['search_term'];
10681 $limit = Utilities::get_search_page_limit();
10682 $offset = $limit * ( $page - 1 );
10683 $per_page = 10;
10684
10685 $query = [
10686 [
10687 'or' => 1,
10688 'name LIKE' => $term,
10689 'parent_form_id' => null,
10690 ],
10691 ];
10692 $query['is_template'] = 0;
10693 $query['status !'] = 'trash';
10694 $forms_list = FrmForm::getAll( $query, '', $offset . ',' . $per_page );
10695 $form_count = FrmForm::getAll( $query );
10696 $form_count = count( $form_count );
10697 $options = [];
10698
10699 if ( ! empty( $forms_list ) ) {
10700 if ( is_array( $forms_list ) ) {
10701 foreach ( $forms_list as $form ) {
10702 $options[] = [
10703 'label' => $form->name,
10704 'value' => $form->id,
10705 ];
10706 }
10707 }
10708 }
10709
10710 return [
10711 'options' => $options,
10712 'hasMore' => $form_count > $limit && $form_count > $offset,
10713 ];
10714 }
10715
10716 /**
10717 * Get JetFormBuilder Form List.
10718 *
10719 * @param array $data data.
10720 *
10721 * @return array|void
10722 */
10723 public function search_jetform_list( $data ) {
10724 if ( ! class_exists( '\Jet_Form_Builder\Classes\Tools' ) ) {
10725 return;
10726 }
10727
10728 $forms = \Jet_Form_Builder\Classes\Tools::get_forms_list_for_js();
10729
10730 $options = [];
10731 foreach ( $forms as $form ) {
10732
10733 if ( ! empty( $form['value'] ) ) {
10734 $options[] = [
10735 'label' => esc_html( $form['label'] ),
10736 'value' => esc_attr( $form['value'] ),
10737 ];
10738 }
10739 }
10740
10741 return [
10742 'options' => $options,
10743 'hasMore' => false,
10744 ];
10745 }
10746
10747 /**
10748 * Get Forminator Form List.
10749 *
10750 * @param array $data data.
10751 *
10752 * @return array|void
10753 */
10754 public function search_forminator_form_list( $data ) {
10755 if ( ! class_exists( 'Forminator_API' ) ) {
10756 return;
10757 }
10758
10759 $forms = Forminator_API::get_forms( null, 1, 10 );
10760
10761 $options = [];
10762 foreach ( $forms as $form ) {
10763 $options[] = [
10764 'label' => isset( $form->settings ) && isset( $form->settings['form_name'] ) ? $form->settings['form_name'] : $form->name,
10765 'value' => $form->id,
10766 ];
10767 }
10768
10769 return [
10770 'options' => $options,
10771 'hasMore' => false,
10772 ];
10773 }
10774
10775
10776 /**
10777 * Get BbPress topics list.
10778 *
10779 * @param array $data data.
10780 *
10781 * @return array|void
10782 */
10783 public function search_bbp_topic_list( $data ) {
10784 $page = $data['page'];
10785 $forum_id = $data['dynamic'];
10786 $limit = Utilities::get_search_page_limit();
10787 $offset = $limit * ( $page - 1 );
10788 $args = [
10789 'post_type' => 'topic',
10790 'offset' => $offset,
10791 'meta_query' => [
10792 [
10793 'key' => '_bbp_forum_id',
10794 'value' => $forum_id,
10795 'compare' => '==',
10796 ],
10797 ],
10798 ];
10799
10800 $topics = get_posts( $args );
10801 $topics_count = wp_count_posts( 'topic' )->publish;
10802
10803 $options = [];
10804 if ( ! empty( $topics ) ) {
10805 if ( is_array( $topics ) ) {
10806 foreach ( $topics as $topic ) {
10807 $options[] = [
10808 'label' => $topic->post_title,
10809 'value' => $topic->ID,
10810 ];
10811 }
10812 }
10813 }
10814 return [
10815 'options' => $options,
10816 'hasMore' => $topics_count > $limit && $topics_count > $offset,
10817 ];
10818 }
10819
10820
10821 /**
10822 * Search Last Updated Field Data.
10823 *
10824 * @param array $data data.
10825 * @return array
10826 */
10827 public function search_bbp_last_data( $data ) {
10828 global $wpdb;
10829
10830 $post_type = $data['post_type'];
10831 $trigger = $data['search_term'];
10832 $context = [];
10833
10834 if ( 'topic' === $post_type ) {
10835 $post_id = $data['filter']['forum']['value'];
10836 if ( -1 === $post_id ) {
10837 $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" );
10838
10839 } else {
10840 $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 ) );
10841
10842 }
10843 } elseif ( 'reply' === $post_type ) {
10844 $post_id = $data['filter']['topic']['value'];
10845
10846 if ( -1 === $post_id ) {
10847 $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" );
10848
10849 } else {
10850 $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 ) );
10851 }
10852 }
10853
10854
10855 $response = [];
10856 if ( ! empty( $result ) ) {
10857 if ( 'bbpress_topic_created' === $trigger ) {
10858 $topic_id = $result[0]->post_id;
10859 $forum_id = $result[0]->meta_value;
10860 $topic = get_the_title( $topic_id );
10861 $topic_link = get_the_permalink( $topic_id );
10862 $topic_description = get_the_content( null, false, $topic_id );
10863 $topic_status = get_post_status( $topic_id );
10864
10865 $forum = get_the_title( $forum_id );
10866 $forum_link = get_the_permalink( $forum_id );
10867 $forum_description = get_the_content( null, false, $forum_id );
10868 $forum_status = get_post_status( $forum_id );
10869
10870 $forum = [
10871 'forum' => $forum_id,
10872 'forum_title' => $forum,
10873 'forum_link' => $forum_link,
10874 'forum_description' => $forum_description,
10875 'forum_status' => $forum_status,
10876 ];
10877
10878 $topic = [
10879 'topic_title' => $topic,
10880 'topic_link' => $topic_link,
10881 'topic_description' => $topic_description,
10882 'topic_status' => $topic_status,
10883 ];
10884
10885 $user_id = $result[0]->post_author;
10886 if ( '0' != $user_id ) {
10887 $context = array_merge(
10888 WordPress::get_user_context( $user_id ),
10889 $forum,
10890 $topic
10891 );
10892 } else {
10893 $anonymous_data = [
10894 'bbp_anonymous_name' => get_post_meta( $result[0]->ID, '_bbp_anonymous_name', true ),
10895 'bbp_anonymous_email' => get_post_meta( $result[0]->ID, '_bbp_anonymous_email', true ),
10896 'bbp_anonymous_website' => get_post_meta( $result[0]->ID, '_bbp_anonymous_website', true ),
10897 ];
10898 $context = array_merge(
10899 $anonymous_data,
10900 $forum,
10901 $topic
10902 );
10903 }
10904
10905 $response['pluggable_data'] = $context;
10906 $response['response_type'] = 'live';
10907 } else {
10908 $reply_id = $result[0]->post_id;
10909 $topic_id = $result[0]->meta_value;
10910 $forum_id = get_post_meta( $topic_id, '_bbp_forum_id', true );
10911 $forum_id = intval( '"' . $forum_id . '"' );
10912 $reply = get_the_title( $reply_id );
10913 $reply_link = get_the_permalink( $reply_id );
10914 $reply_description = get_the_content( null, false, $reply_id );
10915 $reply_status = get_post_status( $reply_id );
10916
10917
10918 $topic = get_the_title( $topic_id );
10919 $topic_link = get_the_permalink( $topic_id );
10920 $topic_description = get_the_content( null, false, $topic_id );
10921 $topic_status = get_post_status( $topic_id );
10922
10923 $forum = get_the_title( $forum_id );
10924 $forum_link = get_the_permalink( $forum_id );
10925 $forum_description = get_the_content( null, false, $forum_id );
10926 $forum_status = get_post_status( $forum_id );
10927
10928 $forum = [
10929 'forum' => $forum_id,
10930 'forum_title' => $forum,
10931 'forum_link' => $forum_link,
10932 'forum_description' => $forum_description,
10933 'forum_status' => $forum_status,
10934 ];
10935
10936 $topic = [
10937 'topic_title' => $topic,
10938 'topic_link' => $topic_link,
10939 'topic_description' => $topic_description,
10940 'topic_status' => $topic_status,
10941 ];
10942
10943 $reply = [
10944 'reply_title' => $reply,
10945 'reply_link' => $reply_link,
10946 'reply_description' => $reply_description,
10947 'reply_status' => $reply_status,
10948 ];
10949 $user_id = $result[0]->post_author;
10950 if ( '0' != $user_id ) {
10951 $context = array_merge(
10952 WordPress::get_user_context( $user_id ),
10953 $forum,
10954 $topic,
10955 $reply
10956 );
10957 } else {
10958 $anonymous_data = [
10959 'bbp_anonymous_name' => get_post_meta( $result[0]->ID, '_bbp_anonymous_name', true ),
10960 'bbp_anonymous_email' => get_post_meta( $result[0]->ID, '_bbp_anonymous_email', true ),
10961 'bbp_anonymous_website' => get_post_meta( $result[0]->ID, '_bbp_anonymous_website', true ),
10962 ];
10963 $context = array_merge(
10964 $anonymous_data,
10965 $forum,
10966 $topic,
10967 $reply
10968 );
10969 }
10970
10971 $response['pluggable_data'] = $context;
10972 $response['response_type'] = 'live';
10973 }
10974 }
10975
10976 return $response;
10977 }
10978
10979 /**
10980 * Search Last Updated Field Data.
10981 *
10982 * @param array $data data.
10983 * @return array|void
10984 */
10985 public function search_happyform_list( $data ) {
10986 if ( ! function_exists( 'happyforms_get_form_controller' ) ) {
10987 return;
10988 }
10989
10990 $form_controller = happyforms_get_form_controller();
10991
10992 $forms = $form_controller->do_get();
10993 $options = [];
10994 if ( ! empty( $forms ) ) {
10995 foreach ( $forms as $form ) {
10996 $options[] = [
10997 'label' => $form['post_title'],
10998 'value' => $form['ID'],
10999 ];
11000 }
11001 }
11002
11003 return [
11004 'options' => $options,
11005 'hasMore' => false,
11006 ];
11007 }
11008
11009 /**
11010 * Get Memberpress Course List.
11011 *
11012 * @param array $data data.
11013 *
11014 * @return array|void
11015 */
11016 public function search_mpc_lessons_list( $data ) {
11017 if ( ! class_exists( '\memberpress\courses\models\Lesson' ) ) {
11018 return;
11019 }
11020 global $wpdb;
11021 $options = [];
11022 $course_id = $data['dynamic'];
11023 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mpcs_sections WHERE course_id =%s", $course_id ) );
11024 $sections = [];
11025 foreach ( $result as $rec ) {
11026 $sections[] = [
11027 'id' => $rec->id,
11028 'title' => $rec->title,
11029 ];
11030 }
11031 if ( is_array( $sections ) && count( $sections ) > 0 ) {
11032 foreach ( $sections as $section ) {
11033 $post_types_string = \memberpress\courses\models\Lesson::lesson_cpts();
11034 $post_types_string = implode( "','", $post_types_string );
11035
11036 $query = $wpdb->prepare(
11037 "SELECT * FROM {$wpdb->posts} AS p
11038 JOIN {$wpdb->postmeta} AS pm
11039 ON p.ID = pm.post_id
11040 AND pm.meta_key = %s
11041 AND pm.meta_value = %s
11042 JOIN {$wpdb->postmeta} AS pm_order
11043 ON p.ID = pm_order.post_id
11044 AND pm_order.meta_key = %s
11045 WHERE p.post_type in ( %s ) AND p.post_status <> 'trash'
11046 ORDER BY pm_order.meta_value * 1",
11047 models\Lesson::$section_id_str,
11048 $section['id'],
11049 models\Lesson::$lesson_order_str,
11050 stripcslashes( $post_types_string )
11051 );
11052
11053 $db_lessons = $wpdb->get_results( stripcslashes( $query ) ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
11054 foreach ( $db_lessons as $lesson ) {
11055 $options[] = [
11056 'label' => $section['title'] . '->' . $lesson->post_title,
11057 'value' => $lesson->ID,
11058 ];
11059 }
11060 }
11061 }
11062 return [
11063 'options' => $options,
11064 'hasMore' => false,
11065 ];
11066 }
11067
11068 /**
11069 * Get Memberpress Course List.
11070 *
11071 * @param array $data data.
11072 *
11073 * @return array|void
11074 */
11075 public function search_gp_rank_type_list( $data ) {
11076 global $wpdb;
11077
11078 $posts = $wpdb->get_results(
11079 "SELECT ID, post_name, post_title, post_type
11080 FROM $wpdb->posts
11081 WHERE post_type LIKE 'rank-type' AND post_status = 'publish' ORDER BY post_title ASC"
11082 );
11083
11084 $posts_count = count( $posts );
11085
11086 $options = [];
11087 if ( $posts ) {
11088 foreach ( $posts as $post ) {
11089 $options[] = [
11090 'label' => $post->post_title,
11091 'value' => $post->post_name,
11092 ];
11093 }
11094 }
11095
11096 return [
11097 'options' => $options,
11098 'hasMore' => false,
11099 ];
11100 }
11101
11102 /**
11103 * Get MPC last data.
11104 *
11105 * @param array $data data.
11106 *
11107 * @return array
11108 */
11109 public function search_mpc_last_data( $data ) {
11110 global $wpdb;
11111 $trigger = $data['search_term'];
11112 $course_data = [];
11113 $lesson_data = [];
11114 $context = [];
11115
11116 if ( 'mpc_course_completed' === $trigger ) {
11117 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course]']['value'] : '-1' );
11118 if ( $course_id > 0 ) {
11119
11120 $course = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $course_id ) );
11121 } else {
11122 $course = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-course' ORDER BY id DESC LIMIT 1" );
11123 }
11124
11125 if ( ! empty( $course ) ) {
11126 $course_data = [
11127 'course_id' => $course->ID,
11128 'course_title' => get_the_title( $course_id ),
11129 'course_url' => get_permalink( $course_id ),
11130 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
11131 'course_featured_image_url' => get_the_post_thumbnail_url( $course_id ),
11132 ];
11133 }
11134 $user_progress = $wpdb->get_row( $wpdb->prepare( "SELECT user_id FROM {$wpdb->prefix}mpcs_user_progress WHERE course_id=%s", $course_id ) );
11135 if ( ! empty( $user_progress ) ) {
11136 $context['response_type'] = 'live';
11137 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
11138 } else {
11139 $sample_data = '{"pluggable_data":{"wp_user_id":1,"user_login":"suretriggers","display_name":"suretriggers","user_firstname":"suretriggers","user_lastname":"suretriggers","user_email":"hello@suretriggers.io","user_role":["administrator","subscriber","tutor_instructor","bbp_keymaster"],"course_id":617,"course_title":"Course One","course_url":"https:\/\/connector.com\/courses\/course-one\/","course_featured_image_id":"","course_featured_image_url":false}
11140 ,"response_type":"sample"} ';
11141 $context = json_decode( $sample_data, true );
11142 }
11143 } elseif ( 'mpc_lesson_completed' === $trigger ) {
11144 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
11145 $course_id = (int) $data['filter']['course']['value'];
11146 if ( $lesson_id > 0 ) {
11147
11148 $lesson = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $lesson_id ) );
11149 } else {
11150 $lesson = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-lesson' ORDER BY id DESC LIMIT 1" );
11151 }
11152
11153 if ( ! empty( $lesson ) ) {
11154 $lesson_data = [
11155 'lesson_id' => $lesson->ID,
11156 'lesson_title' => get_the_title( $lesson_id ),
11157 'lesson_url' => get_permalink( $lesson_id ),
11158 'lesson_featured_image_id' => get_post_meta( $lesson_id, '_thumbnail_id', true ),
11159 'lesson_featured_image_url' => get_the_post_thumbnail_url( $lesson_id ),
11160 ];
11161
11162 $lesson_section_id = get_post_meta( $lesson->ID, '_mpcs_lesson_section_id', true );
11163
11164 $section = $wpdb->get_row( $wpdb->prepare( "SELECT course_id FROM {$wpdb->prefix}mpcs_sections WHERE ID= %s", $lesson_section_id ) );
11165
11166 $course_data = [
11167 'course_id' => $course_id,
11168 'course_title' => get_the_title( $course_id ),
11169 'course_url' => get_permalink( $course_id ),
11170 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
11171 'course_featured_image_url' => get_the_post_thumbnail_url( $section->course_id ),
11172 ];
11173 }
11174
11175 $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 ) );
11176 if ( ! empty( $user_progress ) ) {
11177 $context['response_type'] = 'live';
11178 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
11179 } else {
11180 $sample_data = '{"pluggable_data":{"wp_user_id":1,"user_login":"suretriggers","display_name":"suretriggers","user_firstname":"suretriggers","user_lastname":"dev","user_email":"hello@suretriggers.com","user_role":["administrator","subscriber","tutor_instructor","bbp_keymaster"],"lesson_id":620,"lesson_title":"second section","lesson_url":"https:\/\/connector.com\/courses\/course-one\/lessons\/second-section\/","lesson_featured_image_id":"","lesson_featured_image_url":false,"course_id":617,"course_title":"Course One","course_url":"https:\/\/connector.com\/courses\/course-one\/","course_featured_image_id":"","course_featured_image_url":false},"response_type":"sample"}';
11181 $context = json_decode( $sample_data, true );
11182 }
11183 }
11184
11185
11186 return $context;
11187 }
11188
11189 /** Get GamiPress Rank List.
11190 *
11191 * @param array $data data.
11192 *
11193 * @return array|void
11194 */
11195 public function search_gp_rank_list( $data ) {
11196 global $wpdb;
11197
11198 $page = $data['page'];
11199 $limit = Utilities::get_search_page_limit();
11200 $offset = $limit * ( $page - 1 );
11201
11202 $args = [
11203 'post_type' => $data['dynamic']['rank_type'],
11204 'posts_per_page' => $limit,
11205 'offset' => $offset,
11206 'orderby' => 'title',
11207 'order' => 'ASC',
11208 'post_status' => 'publish',
11209 's' => $data['search_term'],
11210 ];
11211
11212 $rank_type = get_posts( $args );
11213
11214 $count_args = [
11215 'post_type' => $data['dynamic']['rank_type'],
11216 'posts_per_page' => -1,
11217 'post_status' => 'publish',
11218 's' => $data['search_term'],
11219 ];
11220
11221 $rank_posts = get_posts( $count_args );
11222 $rank_type_count = count( $rank_posts );
11223
11224 $options = [];
11225 if ( $rank_type ) {
11226 foreach ( $rank_type as $rank ) {
11227 $options[] = [
11228 'label' => $rank->post_title,
11229 'value' => $rank->ID,
11230 ];
11231 }
11232 }
11233
11234 return [
11235 'options' => $options,
11236 'hasMore' => $rank_type_count > $limit && $rank_type_count > $offset,
11237 ];
11238 }
11239
11240 /**
11241 * Get GamiPress PointType List.
11242 *
11243 * @param array $data data.
11244 *
11245 * @return array|void
11246 */
11247 public function search_gp_point_type_list( $data ) {
11248 global $wpdb;
11249
11250 $page = $data['page'];
11251 $limit = Utilities::get_search_page_limit();
11252 $offset = $limit * ( $page - 1 );
11253
11254 $args = [
11255 'post_type' => 'points-type',
11256 'posts_per_page' => $limit,
11257 'offset' => $offset,
11258 'orderby' => 'title',
11259 'order' => 'ASC',
11260 'post_status' => 'publish',
11261 's' => $data['search_term'],
11262 ];
11263
11264 $point_type = get_posts( $args );
11265
11266 $count_args = [
11267 'post_type' => 'points-type',
11268 'posts_per_page' => -1,
11269 'post_status' => 'publish',
11270 's' => $data['search_term'],
11271 ];
11272
11273 $count_point_type = get_posts( $count_args );
11274 $point_type_count = count( $count_point_type );
11275
11276 $options = [];
11277 if ( $point_type ) {
11278 foreach ( $point_type as $point ) {
11279 $options[] = [
11280 'label' => $point->post_title,
11281 'value' => $point->ID,
11282 ];
11283 }
11284 }
11285
11286 return [
11287 'options' => $options,
11288 'hasMore' => $point_type_count > $limit && $point_type_count > $offset,
11289 ];
11290 }
11291
11292 /**
11293 * Get GamiPress AchievementType List.
11294 *
11295 * @param array $data data.
11296 *
11297 * @return array|void
11298 */
11299 public function search_gp_achivement_type_list( $data ) {
11300 global $wpdb;
11301
11302 $posts = $wpdb->get_results(
11303 "SELECT ID, post_name, post_title, post_type
11304 FROM $wpdb->posts
11305 WHERE post_type LIKE 'achievement-type' AND post_status = 'publish' ORDER BY post_title ASC"
11306 );
11307
11308 $posts_count = count( $posts );
11309
11310 $options = [];
11311 if ( $posts ) {
11312 foreach ( $posts as $post ) {
11313 $options[] = [
11314 'label' => $post->post_title,
11315 'value' => $post->post_name,
11316 ];
11317 }
11318 }
11319
11320 $options[] = [
11321 'label' => 'Points awards',
11322 'value' => 'points-award',
11323 ];
11324 $options[] = [
11325 'label' => 'Step',
11326 'value' => 'step',
11327 ];
11328 $options[] = [
11329 'label' => 'Rank requirement',
11330 'value' => 'rank-requirement',
11331 ];
11332
11333 return [
11334 'options' => $options,
11335 'hasMore' => false,
11336 ];
11337 }
11338
11339 /**
11340 * Get GamiPress Award List.
11341 *
11342 * @param array $data data.
11343 *
11344 * @return array|void
11345 */
11346 public function search_gp_award_list( $data ) {
11347 global $wpdb;
11348
11349 $page = $data['page'];
11350 $limit = Utilities::get_search_page_limit();
11351 $offset = $limit * ( $page - 1 );
11352
11353 $args = [
11354 'post_type' => $data['dynamic']['achivement_type'],
11355 'posts_per_page' => $limit,
11356 'offset' => $offset,
11357 'orderby' => 'title',
11358 'order' => 'ASC',
11359 'post_status' => 'publish',
11360 's' => $data['search_term'],
11361 ];
11362
11363 $award_type = get_posts( $args );
11364 $count_args = [
11365 'post_type' => $data['dynamic']['achivement_type'],
11366 'posts_per_page' => -1,
11367 'post_status' => 'publish',
11368 's' => $data['search_term'],
11369 ];
11370
11371 $count_award_type = get_posts( $count_args );
11372 $award_type_count = count( $count_award_type );
11373 $options = [];
11374 if ( $award_type ) {
11375 foreach ( $award_type as $award ) {
11376 $options[] = [
11377 'label' => $award->post_title,
11378 'value' => $award->ID,
11379 ];
11380 }
11381 }
11382
11383 return [
11384 'options' => $options,
11385 'hasMore' => $award_type_count > $limit && $award_type_count > $offset,
11386 ];
11387 }
11388
11389 /**
11390 * Get Woocommerce Subscription Product List.
11391 *
11392 * @param array $data data.
11393 *
11394 * @return array|void
11395 */
11396 public function search_wc_subscription_product_list( $data ) {
11397 global $wpdb;
11398
11399 $subscriptions = $wpdb->get_results(
11400 $wpdb->prepare(
11401 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
11402 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
11403 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
11404 AND posts.post_type = %s
11405 AND posts.post_status = %s
11406 UNION ALL
11407 SELECT ID, post_title FROM $wpdb->posts
11408 WHERE post_type = %s
11409 AND post_status = %s
11410 ORDER BY post_title",
11411 'product',
11412 'publish',
11413 'shop_subscription',
11414 'publish'
11415 )
11416 );
11417 $page = $data['page'];
11418 $limit = Utilities::get_search_page_limit();
11419 $offset = $limit * ( $page - 1 );
11420 $subscriptions_products = [];
11421 $subscription_products_count = 10;
11422 if ( function_exists( 'wc_get_products' ) ) {
11423 $product_query = wc_get_products(
11424 [
11425 'type' => [ 'subscription', 'variable-subscription' ],
11426 'posts_per_page' => $limit,
11427 'offset' => $offset,
11428 'paginate' => true,
11429 ]
11430 );
11431 if ( is_object( $product_query ) ) {
11432 $subscriptions_products = $product_query->products;
11433 $subscription_products_count = $product_query->total;
11434 }
11435 }
11436
11437 $options = [];
11438 if ( $subscriptions ) {
11439 foreach ( $subscriptions as $post ) {
11440 $options[] = [
11441 'label' => $post->post_title,
11442 'value' => $post->ID,
11443 ];
11444 }
11445 return [
11446 'options' => $options,
11447 'hasMore' => false,
11448 ];
11449 } elseif ( $subscriptions_products ) {
11450 foreach ( $subscriptions_products as $product ) {
11451 $title = $product->get_name();
11452 $options[] = [
11453 'label' => ! empty( $title ) ? $title : $product->get_id(),
11454 'value' => $product->get_id(),
11455 ];
11456 }
11457 return [
11458 'options' => $options,
11459 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
11460 ];
11461 } else {
11462 return [
11463 'options' => $options,
11464 'hasMore' => false,
11465 ];
11466 }
11467 }
11468
11469 /**
11470 * Get Woocommerce Subscriptions Variation list.
11471 *
11472 * @param array $data data.
11473 *
11474 * @return array|void
11475 */
11476 public function search_wc_variable_subscription_list( $data ) {
11477 global $wpdb;
11478
11479 $page = $data['page'];
11480 $limit = Utilities::get_search_page_limit();
11481 $offset = $limit * ( $page - 1 );
11482
11483 if ( ! function_exists( 'wc_get_products' ) ) {
11484 return;
11485 }
11486 $subscription_products = wc_get_products(
11487 [
11488 'type' => [ 'variable-subscription' ],
11489 'posts_per_page' => $limit,
11490 'offset' => $offset,
11491 'orderby' => 'date',
11492 'order' => 'DESC',
11493 ]
11494 );
11495
11496 $subscription_products_count = count( (array) $subscription_products );
11497
11498 $options = [];
11499 if ( $subscription_products ) {
11500 foreach ( (array) $subscription_products as $product ) {
11501 $options[] = [
11502 'label' => $product->get_title(),
11503 'value' => $product->get_id(),
11504 ];
11505 }
11506 }
11507
11508 return [
11509 'options' => $options,
11510 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
11511 ];
11512 }
11513
11514 /**
11515 * Get Woocommerce Variation list.
11516 *
11517 * @param array $data data.
11518 *
11519 * @return array|void
11520 */
11521 public function search_wc_variation_list( $data ) {
11522 global $wpdb;
11523
11524 $page = $data['page'];
11525 $limit = Utilities::get_search_page_limit();
11526 $offset = $limit * ( $page - 1 );
11527
11528 $args = [
11529 'post_type' => 'product_variation',
11530 'post_parent' => $data['dynamic']['variable_subscription'],
11531 'posts_per_page' => $limit,
11532 'offset' => $offset,
11533 'orderby' => 'ID',
11534 'order' => 'ASC',
11535 'post_status' => 'publish',
11536 ];
11537
11538 $variation = get_posts( $args );
11539 $variation_count = count( $variation );
11540
11541 $options = [];
11542 if ( $variation ) {
11543 foreach ( $variation as $product ) {
11544 $options[] = [
11545 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
11546 'value' => $product->ID,
11547 ];
11548 }
11549 }
11550
11551 return [
11552 'options' => $options,
11553 'hasMore' => $variation_count > $limit && $variation_count > $offset,
11554 ];
11555 }
11556
11557 /**
11558 * Get Membership List.
11559 *
11560 * @param array $data data.
11561 *
11562 * @return array
11563 */
11564 public function search_membership_list( $data ) {
11565 global $wpdb;
11566
11567 $levels = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}pmpro_membership_levels ORDER BY id ASC" );
11568 $options = [];
11569 if ( $levels ) {
11570 foreach ( $levels as $level ) {
11571 $options[] = [
11572 'label' => $level->name,
11573 'value' => $level->id,
11574 ];
11575 }
11576 }
11577
11578 return [
11579 'options' => $options,
11580 'hasMore' => false,
11581 ];
11582 }
11583
11584 /**
11585
11586 * Get EventsManager last data.
11587 *
11588 * @param array $data data.
11589 *
11590 * @return array
11591 */
11592 public function search_events_manager_data( $data ) {
11593 global $wpdb;
11594 $trigger = $data['search_term'];
11595 $context = [];
11596
11597 $post_id = (int) ( isset( $data['filter']['post_id']['value'] ) ? $data['filter']['post_id']['value'] : '-1' );
11598 if ( 'em_user_register_in_event' === $trigger ) {
11599 if ( $post_id > 0 ) {
11600 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11601 $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 ) );
11602 } else {
11603 $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" );
11604
11605 }
11606
11607 if ( ! empty( $all_bookings ) ) {
11608 $user_id = $all_bookings->person_id;
11609 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
11610 $context['pluggable_data'] = array_merge(
11611 WordPress::get_user_context( $user_id ),
11612 json_decode( wp_json_encode( $all_bookings ), true )
11613 );
11614 if ( ! empty( $location ) ) {
11615 $context['pluggable_data'] = array_merge( $context['pluggable_data'], json_decode( wp_json_encode( $location ), true ) );
11616 }
11617
11618 $context['response_type'] = 'live';
11619 }
11620 } elseif ( 'em_user_unregister_from_event' === $trigger ) {
11621
11622 if ( $post_id > 0 ) {
11623 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11624 $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 ) );
11625 } else {
11626 $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" );
11627
11628 }
11629
11630 if ( ! empty( $all_bookings ) ) {
11631 $user_id = $all_bookings->person_id;
11632 $context['pluggable_data'] = array_merge(
11633 WordPress::get_user_context( $user_id ),
11634 json_decode( wp_json_encode( $all_bookings ), true )
11635 );
11636 $context['response_type'] = 'live';
11637 }
11638 } elseif ( 'em_user_booking_approved' === $trigger ) {
11639
11640 if ( $post_id > 0 ) {
11641 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11642 $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 ) );
11643 } else {
11644 $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" );
11645
11646 }
11647
11648 if ( ! empty( $all_bookings ) ) {
11649 $user_id = $all_bookings->person_id;
11650 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
11651 $context['pluggable_data'] = array_merge(
11652 WordPress::get_user_context( $user_id ),
11653 json_decode( wp_json_encode( $all_bookings ), true )
11654 );
11655 if ( ! empty( $location ) ) {
11656 $context['pluggable_data'] = array_merge( $context['pluggable_data'], json_decode( wp_json_encode( $location ), true ) );
11657 }
11658
11659 $context['response_type'] = 'live';
11660
11661 }
11662 } elseif ( 'em_user_registers_event_with_specific_ticket' === $trigger ) {
11663 if ( $post_id > 0 ) {
11664 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11665 $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 ) );
11666 }
11667 if ( ! empty( $all_bookings ) ) {
11668 $ticket_id = (int) $data['filter']['ticket_id']['value'];
11669 $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 ) );
11670 if ( ! empty( $all_ticket_bookings ) ) {
11671 $user_id = $all_bookings->person_id;
11672 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
11673 $bookings_str = wp_json_encode( $all_bookings );
11674 $ticket_bookings_str = wp_json_encode( $all_ticket_bookings );
11675
11676 $context['pluggable_data'] = array_merge(
11677 WordPress::get_user_context( $user_id )
11678 );
11679 if ( is_string( $bookings_str ) && is_string( $ticket_bookings_str ) ) {
11680 $bookings_arr = json_decode( $bookings_str, true );
11681 $ticket_bookings_arr = json_decode( $ticket_bookings_str, true );
11682 if ( is_array( $bookings_arr ) && is_array( $ticket_bookings_arr ) ) {
11683 $context['pluggable_data'] = array_merge(
11684 $context['pluggable_data'],
11685 $bookings_arr,
11686 $ticket_bookings_arr
11687 );
11688 }
11689 }
11690 if ( ! empty( $location ) ) {
11691 $location_str = wp_json_encode( $location );
11692 if ( is_string( $location_str ) ) {
11693 $location_arr = json_decode( $location_str, true );
11694 if ( is_array( $location_arr ) ) {
11695 $context['pluggable_data'] = array_merge( $context['pluggable_data'], $location_arr );
11696 }
11697 }
11698 }
11699 $context['response_type'] = 'live';
11700 }
11701 }
11702 }
11703 return $context;
11704 }
11705
11706 /**
11707 * Get Events Manager Events Ticket list.
11708 *
11709 * @param array $data data.
11710 *
11711 * @return array|void
11712 */
11713 public function search_em_event_tickets( $data ) {
11714 global $wpdb;
11715
11716 $options = [];
11717
11718 $event = $data['dynamic']['post_id'];
11719
11720 $event_id = $wpdb->get_var( $wpdb->prepare( "SELECT event_id FROM {$wpdb->prefix}em_events WHERE post_id = %d", $event ) );
11721 $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] ) );
11722
11723 if ( $tickets ) {
11724 foreach ( $tickets as $ticket ) {
11725 $options[] = [
11726 'label' => $ticket->ticket_name,
11727 'value' => $ticket->ticket_id,
11728 ];
11729 }
11730 }
11731
11732 return [
11733 'options' => $options,
11734 'hasMore' => false,
11735 ];
11736 }
11737
11738 /**
11739
11740 * Get learnpress last data.
11741 *
11742 * @param array $data data.
11743 *
11744 * @return array
11745 */
11746 public function search_learnpress_lms_last_data( $data ) {
11747 global $wpdb;
11748 $trigger = $data['search_term'];
11749 $course_data = [];
11750 $lesson_data = [];
11751 $context = [];
11752
11753
11754 if ( 'learnpress_course_completed' === $trigger ) {
11755 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
11756 if ( $course_id > 0 ) {
11757
11758 $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 ) );
11759 } else {
11760
11761 $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" );
11762 }
11763
11764 if ( ! empty( $course ) ) {
11765 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
11766 $context['response_type'] = 'live';
11767 $context['pluggable_data'] = $course_data;
11768 }
11769 } elseif ( 'learnpress_lesson_completed' === $trigger ) {
11770 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
11771 if ( $lesson_id > 0 ) {
11772
11773 $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 ) );
11774 } else {
11775
11776 $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" );
11777 }
11778
11779 if ( ! empty( $lesson ) ) {
11780 $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 ) );
11781 $context['response_type'] = 'live';
11782 $context['pluggable_data'] = $lesson_data;
11783 }
11784 } elseif ( 'learnpress_user_enrolled_in_course' === $trigger ) {
11785 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
11786 if ( $course_id > 0 ) {
11787
11788 $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 ) );
11789 } else {
11790
11791 $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" );
11792 }
11793
11794 if ( ! empty( $course ) ) {
11795 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
11796 $context['response_type'] = 'live';
11797 $context['pluggable_data'] = $course_data;
11798
11799 }
11800 }
11801
11802 return $context;
11803 }
11804
11805 /**
11806 * Get Woocommerce Memberships Plan List.
11807 *
11808 * @param array $data data.
11809 *
11810 * @return array|void
11811 */
11812 public function search_wc_membership_plan_list( $data ) {
11813 global $wpdb;
11814
11815 $page = $data['page'];
11816 $limit = Utilities::get_search_page_limit();
11817 $offset = $limit * ( $page - 1 );
11818
11819 $args = [
11820 'post_type' => 'wc_membership_plan',
11821 'posts_per_page' => $limit,
11822 'offset' => $offset,
11823 'orderby' => 'title',
11824 'order' => 'ASC',
11825 'post_status' => 'publish',
11826 'fields' => 'ids',
11827 ];
11828 $loop = new WP_Query( $args );
11829
11830 $plans = (array) $loop->posts;
11831 $plans_count = wp_count_posts( 'wc_membership_plan' )->publish;
11832
11833 $options = [];
11834 if ( ! empty( $plans ) ) {
11835 if ( is_array( $plans ) ) {
11836 foreach ( $plans as $plan_id ) {
11837 $options[] = [
11838 'label' => get_the_title( $plan_id ),
11839 'value' => $plan_id,
11840 ];
11841 }
11842 }
11843 }
11844
11845 return [
11846 'options' => $options,
11847 'hasMore' => $plans_count > $limit && $plans_count > $offset,
11848 ];
11849 }
11850
11851 /**
11852 * Get BuddyPress Private group.
11853 *
11854 * @param array $data data.
11855 *
11856 * @return array|void
11857 */
11858 public function search_bp_private_group_list( $data ) {
11859 global $wpdb;
11860
11861 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'private'" );
11862
11863 $options = [];
11864 if ( $groups ) {
11865 foreach ( $groups as $group ) {
11866 $options[] = [
11867 'label' => $group->name,
11868 'value' => $group->id,
11869 ];
11870 }
11871 }
11872
11873 return [
11874 'options' => $options,
11875 'hasMore' => false,
11876 ];
11877 }
11878
11879 /**
11880 * Get BuddyPress Public group.
11881 *
11882 * @param array $data data.
11883 *
11884 * @return array|void
11885 */
11886 public function search_bp_public_group_list( $data ) {
11887 global $wpdb;
11888
11889 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'public'" );
11890
11891 $options = [];
11892 if ( $groups ) {
11893 foreach ( $groups as $group ) {
11894 $options[] = [
11895 'label' => $group->name,
11896 'value' => $group->id,
11897 ];
11898 }
11899 }
11900
11901 return [
11902 'options' => $options,
11903 'hasMore' => false,
11904 ];
11905 }
11906
11907 /**
11908 * Get BuddyPress group.
11909 *
11910 * @param array $data data.
11911 *
11912 * @return array|void
11913 */
11914 public function search_bp_group_list( $data ) {
11915 global $wpdb;
11916
11917 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
11918
11919 $options = [];
11920 if ( $groups ) {
11921 foreach ( $groups as $group ) {
11922 $options[] = [
11923 'label' => $group->name,
11924 'value' => $group->id,
11925 ];
11926 }
11927 }
11928
11929 return [
11930 'options' => $options,
11931 'hasMore' => false,
11932 ];
11933 }
11934
11935 /**
11936 * Get BuddyPress field.
11937 *
11938 * @param array $data data.
11939 *
11940 * @return array|void
11941 */
11942 public function search_bp_field_list( $data ) {
11943 global $wpdb;
11944
11945 $base_group_id = 1;
11946 if ( function_exists( 'bp_xprofile_base_group_id' ) ) {
11947 $base_group_id = bp_xprofile_base_group_id();
11948 }
11949
11950 $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 ) );
11951
11952 $options = [];
11953 if ( ! empty( $xprofile_fields ) ) {
11954 foreach ( $xprofile_fields as $xprofile_field ) {
11955 $options[] = [
11956 'label' => $xprofile_field->name,
11957 'value' => $xprofile_field->id,
11958 ];
11959 }
11960 }
11961
11962 return [
11963 'options' => $options,
11964 'hasMore' => false,
11965 ];
11966 }
11967
11968 /**
11969 * Get BuddyPress member type.
11970 *
11971 * @param array $data data.
11972 *
11973 * @return array|void
11974 */
11975 public function search_bp_member_type_list( $data ) {
11976 $options = [];
11977 if ( function_exists( 'bp_get_member_types' ) ) {
11978 $types = bp_get_member_types( [] );
11979 if ( $types ) {
11980 foreach ( $types as $key => $type ) {
11981 $options[] = [
11982 'label' => $type,
11983 'value' => $key,
11984 ];
11985 }
11986 }
11987 }
11988
11989 return [
11990 'options' => $options,
11991 'hasMore' => false,
11992 ];
11993 }
11994
11995 /**
11996 * Get last data for WP All Import.
11997 *
11998 * @param array $data data.
11999 * @return mixed
12000 */
12001 public function search_wp_all_import_last_data( $data ) {
12002 global $wpdb;
12003 $post_type = $data['filter']['post_type']['value'];
12004 $trigger = $data['search_term'];
12005
12006 if ( 'wp_all_import_post_type_imported' === $trigger ) {
12007 if ( -1 == $post_type ) {
12008 $imports = $wpdb->get_row( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts ORDER BY id DESC LIMIT 1", ARRAY_A );
12009 $posts[0] = $imports['post_id'];
12010 } else {
12011 $imports = $wpdb->get_results( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts", ARRAY_A );
12012 $imports = array_column( $imports, 'post_id' );
12013 $args = [
12014 'posts_per_page' => 1,
12015 'post_type' => $post_type,
12016 'post__in' => $imports,
12017 ];
12018 $posts = get_posts( $args );
12019 }
12020 } elseif ( 'wp_all_import_completed' === $trigger ) {
12021 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 0 ORDER BY id DESC LIMIT 1", ARRAY_A );
12022 } elseif ( 'wp_all_import_failed' === $trigger ) {
12023 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 1 ORDER BY id DESC LIMIT 1", ARRAY_A );
12024 }
12025
12026 if ( 'wp_all_import_post_type_imported' === $trigger && empty( $imports ) ) {
12027 $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 );
12028 return $context;
12029 } elseif ( empty( $imports ) ) {
12030 $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 );
12031 return $context;
12032 }
12033
12034 $context['response_type'] = 'live';
12035 if ( ! empty( $posts ) ) {
12036 $context['pluggable_data'] = WordPress::get_post_context( $posts[0] );
12037 } else {
12038 $context['pluggable_data'] = $imports;
12039 }
12040
12041 return $context;
12042 }
12043
12044 /**
12045 * Get Wp Simple Pay Forms.
12046 *
12047 * @param array $data data.
12048 *
12049 * @return array
12050 */
12051 public function search_wp_simple_pay_forms( $data ) {
12052
12053 $page = $data['page'];
12054 $limit = Utilities::get_search_page_limit();
12055 $offset = $limit * ( $page - 1 );
12056
12057 $forms = get_posts(
12058 [
12059 'post_type' => 'simple-pay',
12060 'posts_per_page' => $limit,
12061 'offset' => $offset,
12062 'fields' => 'ids',
12063 ]
12064 );
12065
12066 $forms_count = wp_count_posts( 'simple-pay' )->publish;
12067
12068 $options = [];
12069
12070 if ( ! empty( $forms ) ) {
12071 foreach ( $forms as $form_id ) {
12072 if ( function_exists( 'simpay_get_form' ) ) {
12073 $form = simpay_get_form( $form_id );
12074 $options[] = [
12075 'label' => null !== get_the_title( $form_id ) ? $form->company_name : get_the_title( $form_id ),
12076 'value' => $form_id,
12077 ];
12078 }
12079 }
12080 }
12081
12082 return [
12083 'options' => $options,
12084 'hasMore' => $forms_count > $limit && $forms_count > $offset,
12085 ];
12086 }
12087
12088 /**
12089 * Get Post list as per post type for metabox.
12090 *
12091 * @param array $data data.
12092 *
12093 * @return array
12094 */
12095 public function search_mb_posts_list( $data ) {
12096
12097 $page = $data['page'];
12098 $limit = Utilities::get_search_page_limit();
12099 $offset = $limit * ( $page - 1 );
12100
12101 $posts = get_posts(
12102 [
12103 'post_type' => $data['dynamic'],
12104 'posts_per_page' => $limit,
12105 'offset' => $offset,
12106 'fields' => 'ids',
12107 ]
12108 );
12109
12110 $all_posts = get_posts(
12111 [
12112 'post_type' => $data['dynamic'],
12113 'posts_per_page' => -1,
12114 'fields' => 'ids',
12115 ]
12116 );
12117
12118 $posts_count = count( $all_posts );
12119
12120 $options = [];
12121
12122 if ( ! empty( $posts ) ) {
12123 foreach ( $posts as $post ) {
12124 $title = html_entity_decode( get_the_title( $post ), ENT_QUOTES, 'UTF-8' );
12125 $options[] = [
12126 'label' => $title,
12127 'value' => $post,
12128 ];
12129 }
12130 }
12131
12132 return [
12133 'options' => $options,
12134 'hasMore' => $posts_count > $limit && $posts_count > $offset,
12135 ];
12136 }
12137
12138 /**
12139 * Get Metabox Custom box in Post list.
12140 *
12141 * @param array $data data.
12142 *
12143 * @return array
12144 */
12145 public function search_mb_field_list( $data ) {
12146
12147 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
12148 return [];
12149 }
12150
12151 $options = [];
12152
12153 $metabox_fields = (array) rwmb_get_object_fields( $data['dynamic'] );
12154
12155 foreach ( $metabox_fields as $metabox_field ) {
12156
12157 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
12158
12159 $options[] = [
12160 'label' => $metabox_field['name'],
12161 'value' => $metabox_field['id'],
12162 ];
12163
12164 }
12165 }
12166
12167 return [
12168 'options' => $options,
12169 'hasMore' => false,
12170 ];
12171 }
12172
12173 /**
12174 * Get Metabox Custom box user list.
12175 *
12176 * @param array $data data.
12177 *
12178 * @return array
12179 */
12180 public function search_mb_user_field_list( $data ) {
12181
12182 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
12183 return [];
12184 }
12185
12186 $options = [];
12187
12188 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
12189
12190 foreach ( $metabox_fields as $metabox_field ) {
12191
12192 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
12193
12194 $options[] = [
12195 'label' => $metabox_field['name'],
12196 'value' => $metabox_field['id'],
12197 ];
12198
12199 }
12200 }
12201
12202 return [
12203 'options' => $options,
12204 'hasMore' => false,
12205 ];
12206 }
12207
12208 /**
12209 * Search Last Updated Field Data for MetaBox.
12210 *
12211 * @param array $data data.
12212 * @return array
12213 */
12214 public function search_meta_box_field_data( $data ) {
12215 global $wpdb;
12216
12217 $context = [];
12218
12219 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12220
12221 $post_type = $data['filter']['wp_post_type']['value'];
12222 $post = $data['filter']['wp_post']['value'];
12223
12224 if ( -1 === $field ) {
12225 if ( function_exists( 'rwmb_get_object_fields' ) ) {
12226 $metaboxes = rwmb_get_object_fields( $post_type );
12227
12228 if ( ! empty( $metaboxes ) ) {
12229 $random_key = array_rand( $metaboxes );
12230 $field = $random_key;
12231 } else {
12232 $result = '';
12233 }
12234 }
12235 } else {
12236 $field = $data['filter']['field_id']['value'];
12237 }
12238
12239 if ( function_exists( 'rwmb_meta' ) ) {
12240 $result = rwmb_meta( $field, '', $post );
12241 }
12242
12243 $response = [];
12244 if ( ! empty( $result ) ) {
12245 $response['pluggable_data'] = array_merge( [ $field => $result ], WordPress::get_post_context( $post ) );
12246 $response['response_type'] = 'live';
12247 } else {
12248 $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 );
12249 }
12250
12251 return $response;
12252 }
12253
12254 /**
12255 * Search Last Updated User Field Data MetaBox.
12256 *
12257 * @param array $data data.
12258 * @return array
12259 */
12260 public function search_user_meta_box_field_data( $data ) {
12261 global $wpdb;
12262
12263 $context = [];
12264
12265 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12266
12267 if ( -1 === $field ) {
12268 if ( function_exists( 'rwmb_get_object_fields' ) ) {
12269 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
12270
12271 if ( ! empty( $metabox_fields ) ) {
12272 $random_key = array_rand( $metabox_fields );
12273 $field = $random_key;
12274 } else {
12275 $result = '';
12276 }
12277 }
12278 } else {
12279 $field = $data['filter']['field_id']['value'];
12280 }
12281
12282 $users = get_users(
12283 [
12284 'fields' => 'ID',
12285 'meta_key' => $field,
12286 ]
12287 );
12288
12289 if ( ! empty( $users ) ) {
12290 $user_random_key = array_rand( $users );
12291 $user_id = $user_random_key;
12292 if ( function_exists( 'rwmb_get_value' ) ) {
12293 $result = rwmb_get_value( $field, [ 'object_type' => 'user' ], $users[ $user_id ] );
12294 }
12295
12296 $response = [];
12297 if ( ! empty( $result ) ) {
12298 $context = [
12299 'field_id' => $field,
12300 $field => $result,
12301 'user' => WordPress::get_user_context( $users[ $user_id ] ),
12302 ];
12303 $response['pluggable_data'] = $context;
12304 $response['response_type'] = 'live';
12305 } else {
12306 $response = json_decode(
12307 '{
12308 "response_type": "sample",
12309 "pluggable_data": {
12310 "field_id": "gender",
12311 "user": {
12312 "wp_user_id": 114,
12313 "user_login": "test",
12314 "display_name": "test",
12315 "user_firstname": "test",
12316 "user_lastname": "test",
12317 "user_email": "test@test.com",
12318 "user_role": [ "subscriber" ]
12319 }
12320 }
12321 }',
12322 true
12323 );
12324 }
12325 } else {
12326 $response = json_decode(
12327 '{
12328 "response_type": "sample",
12329 "pluggable_data": {
12330 "field_id": "gender",
12331 "user": {
12332 "wp_user_id": 114,
12333 "user_login": "test",
12334 "display_name": "test",
12335 "user_firstname": "test",
12336 "user_lastname": "test",
12337 "user_email": "test@test.com",
12338 "user_role": [ "subscriber" ]
12339 }
12340 }
12341 }',
12342 true
12343 );
12344 }
12345
12346 return $response;
12347 }
12348
12349 /**
12350 * Search forms of Pie Forms.
12351 *
12352 * @param array $data data.
12353 * @return array
12354 */
12355 public function search_wp_polls_list( $data ) {
12356 global $wpdb;
12357 $options = [];
12358
12359 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsq' ) ) ) {
12360
12361 $results = $wpdb->get_results( 'SELECT pollq_id, pollq_question FROM ' . $wpdb->prefix . 'pollsq WHERE pollq_active = 1' );
12362
12363 if ( $results ) {
12364 foreach ( $results as $result ) {
12365 $options[] = [
12366 'label' => $result->pollq_question,
12367 'value' => $result->pollq_id,
12368 ];
12369 }
12370 }
12371 }
12372
12373 return [
12374 'options' => $options,
12375 'hasMore' => false,
12376 ];
12377 }
12378
12379 /**
12380 * Search answers of WP-Polls questions.
12381 *
12382 * @param array $data data.
12383 * @return array
12384 */
12385 public function search_wp_polls_answers( $data ) {
12386 global $wpdb;
12387
12388 $options = [];
12389 $poll_id = $data['dynamic'];
12390
12391 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsa' ) ) ) {
12392
12393 if ( '-1' !== $poll_id ) {
12394 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa WHERE polla_qid = %d', $poll_id ) );
12395 } else {
12396 $results = $wpdb->get_results( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa' );
12397 }
12398
12399 if ( $results ) {
12400 foreach ( $results as $result ) {
12401 $options[] = [
12402 'label' => $result->polla_answers,
12403 'value' => $result->polla_aid,
12404 ];
12405 }
12406 }
12407 }
12408
12409 return [
12410 'options' => $options,
12411 'hasMore' => false,
12412 ];
12413 }
12414
12415 /**
12416 * Get last data for trigger.
12417 *
12418 * @param array $data data.
12419 * @return array
12420 */
12421 public function search_wp_polls_triggers_last_data( $data ) {
12422 global $wpdb;
12423
12424 $context = [];
12425 $context['response_type'] = 'sample';
12426
12427 $poll = [
12428 'poll_id' => 1,
12429 'question' => 'Which skills are you interested to learn?',
12430 'answers' => 'Web Development, Graphic Designing, Content Writing, Digital Marketing',
12431 'start_date' => '2023-08-29 17:19:13',
12432 'end_date' => 'Not set',
12433 'selected_answers' => 'Content Writing, Web Development',
12434 'selected_answer_id' => 2,
12435 ];
12436
12437 $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" );
12438
12439 if ( ! empty( $poll_data ) ) {
12440 $poll = WpPolls::get_poll_context( (string) $poll_data->answer_id, (int) $poll_data->poll_id );
12441 $poll['selected_answer_id'] = (int) $poll_data->answer_id;
12442
12443 $context['response_type'] = 'live';
12444 }
12445
12446 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
12447
12448 if ( 'poll_submitted' === $term ) {
12449 unset( $poll['selected_answer_id'] );
12450 }
12451
12452 $context['pluggable_data'] = $poll;
12453
12454 return $context;
12455 }
12456
12457 /**
12458 * Get ACF Custom fields list.
12459 *
12460 * @param array $data data.
12461 *
12462 * @return array
12463 */
12464 public function search_acf_post_field_list( $data ) {
12465
12466 $post_id = $data['dynamic']['wp_post'];
12467 $selected_post_type = $data['dynamic']['wp_post_type'];
12468
12469 if ( -1 === $post_id ) {
12470 $args = [
12471 'numberposts' => 1,
12472 'fields' => 'ids',
12473 'orderby' => 'rand',
12474 'post_type' => $selected_post_type,
12475 ];
12476 $posts = get_posts( $args );
12477 $post_id = $posts[0];
12478 }
12479
12480 $args = [
12481 'post_id' => $post_id,
12482 ];
12483 if ( ! is_numeric( $post_id ) ) {
12484 $args = [
12485 'post_type' => $post_id,
12486 ];
12487 }
12488 $options = [];
12489 if ( function_exists( 'acf_get_field_groups' ) ) {
12490 $field_groups_collection = acf_get_field_groups( $args );
12491 foreach ( $field_groups_collection as $field_group ) {
12492 if ( function_exists( 'acf_get_fields' ) ) {
12493 $field_groups[] = acf_get_fields( $field_group['key'] );
12494 }
12495 }
12496
12497 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
12498 foreach ( $field_groups as $field_groups ) {
12499 foreach ( $field_groups as $field_group ) {
12500 $options[] = [
12501 'value' => $field_group['name'],
12502 'label' => ! empty( $field_group['label'] ) ? $field_group['label'] : $field_group['name'],
12503 ];
12504 }
12505 }
12506 }
12507 }
12508
12509 return [
12510 'options' => $options,
12511 'hasMore' => false,
12512 ];
12513 }
12514
12515 /**
12516 * Get ACF Custom fields list.
12517 *
12518 * @param array $data data.
12519 *
12520 * @return array
12521 */
12522 public function search_acf_user_field_list( $data ) {
12523
12524 if ( ! function_exists( 'acf_get_fields' ) ) {
12525 return [];
12526 }
12527 if ( ! function_exists( 'acf_get_field_groups' ) ) {
12528 return [];
12529 }
12530 $groups_user_form = [];
12531 $options = [];
12532 if ( function_exists( 'acf_get_field_groups' ) ) {
12533 $field_groups = acf_get_field_groups();
12534 foreach ( $field_groups as $group ) {
12535 if ( ! empty( $group['location'] ) ) {
12536 foreach ( $group['location'] as $locations ) {
12537 foreach ( $locations as $location ) {
12538 if ( 'user_form' === $location['param'] || 'user_role' === $location['param'] || 'current_user' === $location['param'] || 'current_user_role' === $location['param'] ) {
12539 $groups_user_form[] = $group;
12540 }
12541 }
12542 }
12543 }
12544 }
12545
12546 if ( empty( $groups_user_form ) ) {
12547 return [];
12548 }
12549
12550 $key_values = array_map(
12551 function ( $item ) {
12552 return $item['key'];
12553 },
12554 $groups_user_form
12555 );
12556 $unique_keys = array_unique( $key_values );
12557 $unique_array = array_intersect_key( $groups_user_form, $unique_keys );
12558
12559 foreach ( $unique_array as $group ) {
12560 if ( function_exists( 'acf_get_fields' ) ) {
12561 $group_fields = acf_get_fields( $group['key'] );
12562 }
12563 if ( ! empty( $group_fields ) ) {
12564 foreach ( $group_fields as $field ) {
12565 $options[] = [
12566 'value' => $field['name'],
12567 'label' => $field['label'],
12568 ];
12569
12570 }
12571 }
12572 }
12573 }
12574
12575 return [
12576 'options' => $options,
12577 'hasMore' => false,
12578 ];
12579 }
12580
12581 /**
12582 * Get ACF Custom fields list.
12583 *
12584 * @param array $data data.
12585 *
12586 * @return array
12587 */
12588 public function search_acf_options_field_list( $data ) {
12589
12590 if ( ! function_exists( 'acf_get_fields' ) ) {
12591 return [];
12592 }
12593 if ( ! function_exists( 'acf_get_field_groups' ) ) {
12594 return [];
12595 }
12596 $groups_options_form = [];
12597 $options = [];
12598 if ( function_exists( 'acf_get_field_groups' ) ) {
12599 $field_groups = acf_get_field_groups();
12600 foreach ( $field_groups as $group ) {
12601 if ( ! empty( $group['location'] ) ) {
12602 foreach ( $group['location'] as $locations ) {
12603 foreach ( $locations as $location ) {
12604 if ( 'options_page' === $location['param'] ) {
12605 $groups_options_form[] = $group;
12606 }
12607 }
12608 }
12609 }
12610 }
12611 if ( empty( $groups_options_form ) ) {
12612 return [];
12613 }
12614 $key_values = array_map(
12615 function ( $item ) {
12616 return $item['key'];
12617 },
12618 $groups_options_form
12619 );
12620 $unique_keys = array_unique( $key_values );
12621 $unique_array = array_intersect_key( $groups_options_form, $unique_keys );
12622 foreach ( $unique_array as $group ) {
12623 if ( function_exists( 'acf_get_fields' ) ) {
12624 $group_fields = acf_get_fields( $group['key'] );
12625 }
12626 if ( ! empty( $group_fields ) ) {
12627 foreach ( $group_fields as $field ) {
12628 $options[] = [
12629 'value' => $field['name'],
12630 'label' => $field['label'],
12631 ];
12632
12633 }
12634 }
12635 }
12636 }
12637
12638 return [
12639 'options' => $options,
12640 'hasMore' => false,
12641 ];
12642 }
12643
12644 /**
12645 * Search Last Updated Field Data for ACF.
12646 *
12647 * @param array $data data.
12648 * @return array
12649 */
12650 public function search_acf_post_field_data( $data ) {
12651 $context = [];
12652
12653 $field = ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12654
12655 $post_type = $data['filter']['wp_post_type']['value'];
12656 $post = $data['filter']['wp_post']['value'];
12657
12658 if ( -1 === $post ) {
12659 $args = [
12660 'numberposts' => 1,
12661 'fields' => 'ids',
12662 'orderby' => 'rand',
12663 'post_type' => $post_type,
12664 ];
12665 $posts = get_posts( $args );
12666 $post = $posts[0];
12667 }
12668 if ( -1 === $field ) {
12669 $args = [
12670 'post_id' => $post,
12671 ];
12672 if ( function_exists( 'acf_get_field_groups' ) ) {
12673 $field_groups_collection = acf_get_field_groups( $args );
12674 }
12675 if ( ! empty( $field_groups_collection ) ) {
12676 foreach ( $field_groups_collection as $field_group ) {
12677 if ( function_exists( 'acf_get_fields' ) ) {
12678 $field_groups[] = acf_get_fields( $field_group['key'] );
12679 }
12680 }
12681 }
12682 $fields = [];
12683 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
12684 foreach ( $field_groups as $field_groups ) {
12685 $fields[] = $field_groups;
12686 }
12687 }
12688 if ( ! empty( $fields ) ) {
12689 $random_key = array_rand( $fields[0] );
12690 $field_key = $fields[0][ $random_key ];
12691 $field = $field_key['name'];
12692 } else {
12693 $result = '';
12694 }
12695 } else {
12696 $field = $data['filter']['field_id']['value'];
12697 }
12698 if ( function_exists( ( 'get_field' ) ) ) {
12699 $result = get_field( $field, $post );
12700 }
12701
12702 $response = [];
12703 if ( ! empty( $result ) ) {
12704 $post_fields = [];
12705 if ( function_exists( 'get_fields' ) ) {
12706 $post_fields = get_fields( $post );
12707 }
12708 $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 ) ] );
12709 $response['response_type'] = 'live';
12710 } else {
12711 $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 );
12712 }
12713
12714 return $response;
12715 }
12716
12717 /**
12718 * Search Last Updated User Field Data ACF.
12719 *
12720 * @param array $data data.
12721 * @return array
12722 */
12723 public function search_acf_user_field_data( $data ) {
12724 global $wpdb;
12725
12726 $context = [];
12727
12728 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12729
12730 if ( -1 === $field ) {
12731 $groups_user_form = [];
12732 if ( function_exists( 'acf_get_field_groups' ) ) {
12733 $field_groups = acf_get_field_groups();
12734 }
12735 if ( ! empty( $field_groups ) ) {
12736 foreach ( $field_groups as $group ) {
12737 if ( ! empty( $group['location'] ) ) {
12738 foreach ( $group['location'] as $locations ) {
12739 foreach ( $locations as $location ) {
12740 if ( 'user_form' === $location['param'] || 'user_role' === $location['param'] || 'current_user' === $location['param'] || 'current_user_role' === $location['param'] ) {
12741 $groups_user_form[] = $group;
12742 }
12743 }
12744 }
12745 }
12746 }
12747 $field_groups = $groups_user_form;
12748 }
12749 if ( empty( $field_groups ) ) {
12750 $result = '';
12751 }
12752 $fields = [];
12753 if ( ! empty( $field_groups ) ) {
12754 foreach ( $field_groups as $group ) {
12755 if ( function_exists( 'acf_get_fields' ) ) {
12756 $group_fields = acf_get_fields( $group['key'] );
12757 }
12758 if ( ! empty( $group_fields ) ) {
12759 foreach ( $group_fields as $field ) {
12760 $fields[] = $group_fields;
12761 }
12762 }
12763 }
12764 }
12765 if ( ! empty( $fields ) ) {
12766 $random_key = array_rand( $fields );
12767 $field = $random_key;
12768 } else {
12769 $result = '';
12770 }
12771 } else {
12772 $field = $data['filter']['field_id']['value'];
12773 }
12774 $users = get_users(
12775 [
12776 'fields' => 'ID',
12777 'meta_key' => $field,
12778 ]
12779 );
12780
12781 if ( ! empty( $users ) ) {
12782 $user_random_key = array_rand( $users );
12783 $user_id = $user_random_key;
12784 if ( function_exists( 'get_field' ) ) {
12785 $result = get_field( $field, 'user_' . $users[ $user_id ] );
12786 }
12787 $response = [];
12788 if ( ! empty( $result ) ) {
12789 $context = [
12790 'field_id' => $field,
12791 $field => $result,
12792 'user' => WordPress::get_user_context( $users[ $user_id ] ),
12793 ];
12794 $response['pluggable_data'] = $context;
12795 $response['response_type'] = 'live';
12796 } else {
12797 $response = json_decode(
12798 '{
12799 "response_type": "sample",
12800 "pluggable_data": {
12801 "field_id": "gender",
12802 "user": {
12803 "wp_user_id": 114,
12804 "user_login": "test",
12805 "display_name": "test",
12806 "user_firstname": "test",
12807 "user_lastname": "test",
12808 "user_email": "test@test.com",
12809 "user_role": [ "subscriber" ]
12810 }
12811 }
12812 }',
12813 true
12814 );
12815 }
12816 } else {
12817 $response = json_decode(
12818 '{
12819 "response_type": "sample",
12820 "pluggable_data": {
12821 "field_id": "gender",
12822 "user": {
12823 "wp_user_id": 114,
12824 "user_login": "test",
12825 "display_name": "test",
12826 "user_firstname": "test",
12827 "user_lastname": "test",
12828 "user_email": "test@test.com",
12829 "user_role": [ "subscriber" ]
12830 }
12831 }
12832 }',
12833 true
12834 );
12835 }
12836
12837 return $response;
12838 }
12839
12840 /**
12841 * Search Last Updated Options Field Data ACF.
12842 *
12843 * @param array $data data.
12844 * @return array
12845 */
12846 public function search_acf_options_field_data( $data ) {
12847 global $wpdb;
12848 $context = [];
12849 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12850
12851 if ( -1 === $field ) {
12852 $groups_options_form = [];
12853 if ( function_exists( 'acf_get_field_groups' ) ) {
12854 $field_groups = acf_get_field_groups();
12855 }
12856 if ( ! empty( $field_groups ) ) {
12857 foreach ( $field_groups as $group ) {
12858 if ( ! empty( $group['location'] ) ) {
12859 foreach ( $group['location'] as $locations ) {
12860 foreach ( $locations as $location ) {
12861 if ( 'options_page' === $location['param'] ) {
12862 $groups_options_form[] = $group;
12863 }
12864 }
12865 }
12866 }
12867 }
12868 }
12869 if ( empty( $groups_options_form ) ) {
12870 $result = '';
12871 }
12872 $key_values = array_map(
12873 function ( $item ) {
12874 return $item['key'];
12875 },
12876 $groups_options_form
12877 );
12878 $unique_keys = array_unique( $key_values );
12879 $unique_array = array_intersect_key( $groups_options_form, $unique_keys );
12880 $fields = [];
12881 if ( ! empty( $unique_array ) ) {
12882 foreach ( $unique_array as $group ) {
12883 if ( function_exists( 'acf_get_fields' ) ) {
12884 $group_fields = acf_get_fields( $group['key'] );
12885 }
12886 if ( ! empty( $group_fields ) ) {
12887 foreach ( $group_fields as $field ) {
12888 $fields[] = $group_fields;
12889 }
12890 }
12891 }
12892 }
12893 if ( ! empty( $fields ) ) {
12894 $random_key = array_rand( $fields );
12895 $field = $random_key;
12896 } else {
12897 $result = '';
12898 }
12899 } else {
12900 $field = $data['filter']['field_id']['value'];
12901 }
12902 if ( function_exists( 'get_field' ) ) {
12903 $option_value = get_field( $field, 'option' );
12904 }
12905 if ( ! empty( $option_value ) ) {
12906 if ( function_exists( 'acf_get_field' ) ) {
12907 $options_fields = acf_get_field( $field );
12908 if ( function_exists( 'acf_maybe_get' ) ) {
12909 $options_page = acf_maybe_get( $options_fields, 'parent' );
12910 }
12911 }
12912 $context = [
12913 'field_id' => $field,
12914 $field => $option_value,
12915 ];
12916 $response['pluggable_data'] = $context;
12917 $response['response_type'] = 'live';
12918 } else {
12919 $response = json_decode(
12920 '{
12921 "response_type": "sample",
12922 "pluggable_data": {
12923 "field_id": "optionpage",
12924 "optionpage": "newoption"
12925 }
12926 }',
12927 true
12928 );
12929 }
12930 return $response;
12931 }
12932
12933 /**
12934 * Get WP Fusion Tags list.
12935 *
12936 * @param array $data data.
12937 *
12938 * @return array
12939 */
12940 public function search_wp_fusion_tag_list( $data ) {
12941
12942 if ( ! function_exists( 'wp_fusion' ) ) {
12943 return [];
12944 }
12945
12946 $options = [];
12947 $tags = wp_fusion()->settings->get( 'available_tags' );
12948
12949 if ( $tags ) {
12950 foreach ( $tags as $t_id => $tag ) {
12951 if ( is_array( $tag ) && isset( $tag['label'] ) ) {
12952 $options[] = [
12953 'value' => $t_id,
12954 'label' => $tag['label'],
12955 ];
12956 } else {
12957 $options[] = [
12958 'value' => $t_id,
12959 'label' => $tag,
12960 ];
12961 }
12962 }
12963 }
12964
12965 return [
12966 'options' => $options,
12967 'hasMore' => false,
12968 ];
12969 }
12970
12971 /**
12972 * Get list of events for Modern Events Calendar.
12973 *
12974 * @param array $data data.
12975 * @return array
12976 */
12977 public function search_mec_events_list( $data ) {
12978 $page = $data['page'];
12979 $limit = Utilities::get_search_page_limit();
12980 $offset = $limit * ( $page - 1 );
12981
12982 $args = [
12983 'post_type' => 'mec-events',
12984 'post_status' => [ 'publish', 'private' ],
12985 'posts_per_page' => -1,
12986 ];
12987 $loop = new WP_Query( $args );
12988 $events_count = count( $loop->posts );
12989
12990 $args = [
12991 'post_type' => 'mec-events',
12992 'posts_per_page' => $limit,
12993 'offset' => $offset,
12994 'orderby' => 'title',
12995 'order' => 'ASC',
12996 'post_status' => [ 'publish', 'private' ],
12997 ];
12998
12999 $loop = new WP_Query( $args );
13000 $events = $loop->posts;
13001
13002 $options = [];
13003 if ( ! empty( $events ) ) {
13004 foreach ( $events as $event ) {
13005 if ( isset( $event->ID ) ) {
13006 $options[] = [
13007 'label' => get_the_title( $event ),
13008 'value' => $event->ID,
13009 ];
13010 }
13011 }
13012 }
13013
13014 return [
13015 'options' => $options,
13016 'hasMore' => $events_count > $limit && $events_count > $offset,
13017 ];
13018 }
13019
13020 /**
13021 * Search tickets of MEC events.
13022 *
13023 * @param array $data data.
13024 * @return array
13025 */
13026 public function search_mec_event_tickets( $data ) {
13027 $options = [];
13028 $event_id = $data['dynamic'];
13029
13030 $event_tickets = get_post_meta( $event_id, 'mec_tickets', true );
13031
13032 if ( ! empty( $event_tickets ) && is_array( $event_tickets ) ) {
13033 foreach ( $event_tickets as $ticket_id => $event_ticket ) {
13034 if ( isset( $event_ticket['name'] ) ) {
13035 $options[] = [
13036 'label' => $event_ticket['name'],
13037 'value' => $ticket_id,
13038 ];
13039 }
13040 }
13041 }
13042
13043 return [
13044 'options' => $options,
13045 'hasMore' => false,
13046 ];
13047 }
13048
13049 /**
13050 * Get last data for trigger.
13051 *
13052 * @param array $data data.
13053 * @return array
13054 */
13055 public function search_mec_triggers_last_data( $data ) {
13056 global $wpdb;
13057
13058 $context = [];
13059 $context['response_type'] = 'sample';
13060
13061 $event = [
13062 'event_id' => 1,
13063 'title' => 'Sample Event',
13064 'description' => 'Description of the sample event.',
13065 'categories' => 'New, Sample',
13066 'start_date' => 'September 13, 2023',
13067 'start_time' => '8:00 AM',
13068 'end_date' => 'September 13, 2023',
13069 'end_time' => '11:00 AM',
13070 'location' => 'City Hall',
13071 'organizer' => 'John Doe',
13072 'cost' => '5000',
13073 'featured_image_id' => 1,
13074 'featured_image_url' => 'https://suretriggers.com/wp-content/uploads/2022/12/Screenshot_20221127_021332.png',
13075 'tickets' => [
13076 [
13077 'id' => 1,
13078 'name' => 'Silver',
13079 'description' => 'Standard seat with reasonable price.',
13080 'price' => '300',
13081 'price_label' => 'USD',
13082 'limit' => '20',
13083 ],
13084 [
13085 'id' => 2,
13086 'name' => 'Premium',
13087 'description' => 'VIP seat with high price.',
13088 'price' => '500',
13089 'price_label' => 'USD',
13090 'limit' => '10',
13091 ],
13092 ],
13093 'attendees' => [
13094 [
13095 'id' => 1,
13096 'email' => 'johndoe@test.com',
13097 'name' => 'John Doe',
13098 ],
13099 [
13100 'id' => 2,
13101 'email' => 'adamsmith@test.com',
13102 'name' => 'Adam Smith',
13103 ],
13104 ],
13105 'booking' => [
13106 'title' => 'johndoe@test.com - John Doe',
13107 'transaction_id' => 'RSH59404',
13108 'amount_payable' => '800',
13109 'price' => '800',
13110 'time' => '2023-09-07 06:40:32',
13111 'payment_gateway' => 'Manual Pay',
13112 'confirmation_status' => 'Pending',
13113 'verification_status' => 'Verified',
13114 'attendees_count' => 2,
13115 ],
13116 ];
13117
13118 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
13119
13120 $where = '';
13121
13122 if ( 'cancelled' === $term ) {
13123 $where = 'WHERE verified = -1';
13124 } elseif ( 'confirmed' === $term ) {
13125 $where = 'WHERE confirmed = 1';
13126 } elseif ( 'pending' === $term ) {
13127 $where = 'WHERE confirmed = 0';
13128 }
13129
13130 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
13131
13132 if ( -1 !== $event_id ) {
13133 if ( ! empty( $where ) ) {
13134 $where .= ' AND event_id = ' . $event_id;
13135 } else {
13136 $where = 'WHERE event_id = ' . $event_id;
13137 }
13138 }
13139
13140 $event_data = $wpdb->get_row( "SELECT booking_id FROM {$wpdb->prefix}mec_bookings $where ORDER BY id DESC LIMIT 1" ); // @phpcs:ignore
13141
13142 if ( ! empty( $event_data ) ) {
13143 $event = ModernEventsCalendar::get_event_context( (int) $event_data->booking_id );
13144 $context['response_type'] = 'live';
13145 }
13146
13147 $context['pluggable_data'] = $event;
13148
13149 return $context;
13150 }
13151
13152 /**
13153 * Get form list Contact Form 7.
13154 *
13155 * @param array $data data.
13156 *
13157 * @return array
13158 */
13159 public function search_contact_form7_list( $data ) {
13160
13161 $page = $data['page'];
13162 $limit = Utilities::get_search_page_limit();
13163 $offset = $limit * ( $page - 1 );
13164
13165 $posts = get_posts(
13166 [
13167 'post_type' => 'wpcf7_contact_form',
13168 'posts_per_page' => $limit,
13169 'offset' => $offset,
13170 ]
13171 );
13172
13173 $all_posts = get_posts(
13174 [
13175 'post_type' => 'wpcf7_contact_form',
13176 'posts_per_page' => -1,
13177 ]
13178 );
13179
13180 $posts_count = count( $all_posts );
13181
13182 $options = [];
13183
13184 if ( ! empty( $posts ) ) {
13185 foreach ( $posts as $post ) {
13186 $options[] = [
13187 'label' => get_the_title( $post->ID ),
13188 'value' => $post->ID,
13189 ];
13190 }
13191 }
13192
13193 return [
13194 'options' => $options,
13195 'hasMore' => $posts_count > $limit && $posts_count > $offset,
13196 ];
13197 }
13198
13199 /**
13200 * Get Thrive Leads form list
13201 *
13202 * @param array $data data.
13203 *
13204 * @return array
13205 */
13206 public function search_thrive_leads_forms_list( $data ) {
13207 $options = [];
13208
13209 $lg_ids = get_posts(
13210 [
13211 'post_type' => '_tcb_form_settings',
13212 'fields' => 'id=>parent',
13213 'posts_per_page' => -1,
13214 'post_status' => 'any',
13215 ]
13216 );
13217
13218 if ( function_exists( 'tve_leads_get_form_variations' ) ) {
13219 foreach ( $lg_ids as $lg_id => $lg_parent ) {
13220 $variations = tve_leads_get_form_variations( $lg_parent );
13221 foreach ( $variations as $variation ) {
13222 $options[] = [
13223 'label' => $variation['post_title'],
13224 'value' => $lg_parent,
13225 ];
13226 }
13227 }
13228 }
13229
13230 return [
13231 'options' => array_unique( $options ),
13232 'hasMore' => false,
13233 ];
13234 }
13235
13236 /**
13237 * Get list for Woocommerce Subscriptions
13238 *
13239 * @param array $data data.
13240 *
13241 * @return array
13242 */
13243 public function search_wc_subscription_variation_products( $data ) {
13244 $options = [];
13245
13246 global $wpdb;
13247
13248 if ( ! function_exists( 'wc_get_product' ) ) {
13249 return [];
13250 }
13251 $subscriptions = $wpdb->get_results(
13252 $wpdb->prepare(
13253 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
13254 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
13255 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
13256 AND posts.post_type = %s
13257 AND posts.post_status = %s
13258 UNION ALL
13259 SELECT ID, post_title FROM $wpdb->posts
13260 WHERE post_type = %s
13261 AND post_status = %s
13262 ORDER BY post_title",
13263 'product',
13264 'publish',
13265 'shop_subscription',
13266 'publish'
13267 )
13268 );
13269 $page = $data['page'];
13270 $limit = Utilities::get_search_page_limit();
13271 $offset = $limit * ( $page - 1 );
13272 $subscriptions_products = [];
13273 $subscription_products_count = 10;
13274 if ( function_exists( 'wc_get_products' ) ) {
13275 $product_query = wc_get_products(
13276 [
13277 'type' => [ 'subscription', 'variable-subscription' ],
13278 'posts_per_page' => $limit,
13279 'offset' => $offset,
13280 'paginate' => true,
13281 ]
13282 );
13283 if ( is_object( $product_query ) ) {
13284 $subscriptions_products = $product_query->products;
13285 $subscription_products_count = $product_query->total;
13286 }
13287 }
13288
13289 if ( $subscriptions ) {
13290 foreach ( $subscriptions as $product ) {
13291 $options[] = [
13292 'label' => $product->post_title . ' (#' . $product->ID . ')',
13293 'value' => (int) $product->ID,
13294 ];
13295 $product_s = wc_get_product( $product->ID );
13296 /**
13297 *
13298 * Ignore line
13299 *
13300 * @phpstan-ignore-next-line
13301 */
13302 if ( 'variable-subscription' == $product_s->product_type ) {
13303 $args = [
13304 'post_type' => 'product_variation',
13305 'post_parent' => $product->ID,
13306 'posts_per_page' => -1,
13307 'orderby' => 'ID',
13308 'order' => 'ASC',
13309 'post_status' => 'publish',
13310 ];
13311
13312 $variations = get_posts( $args );
13313
13314 foreach ( $variations as $var ) {
13315 $options[] = [
13316 'label' => $var->post_title . ' (#' . $var->ID . ')',
13317 'value' => $var->ID,
13318 ];
13319 }
13320 }
13321 }
13322 return [
13323 'options' => $options,
13324 'hasMore' => false,
13325 ];
13326 } elseif ( ! empty( $subscriptions_products ) ) {
13327 foreach ( $subscriptions_products as $product ) {
13328 $title = $product->get_name();
13329 $options[] = [
13330 'label' => $title . ' (#' . $product->get_id() . ')',
13331 'value' => $product->get_id(),
13332 ];
13333 $product_s = wc_get_product( $product->get_id() );
13334 /**
13335 *
13336 * Ignore line
13337 *
13338 * @phpstan-ignore-next-line
13339 */
13340 if ( 'variable-subscription' == $product_s->product_type ) {
13341 $args = [
13342 'post_type' => 'product_variation',
13343 'post_parent' => $product->get_id(),
13344 'posts_per_page' => -1,
13345 'orderby' => 'ID',
13346 'order' => 'ASC',
13347 'post_status' => 'publish',
13348 ];
13349
13350 $variations = get_posts( $args );
13351
13352 foreach ( $variations as $var ) {
13353 $options[] = [
13354 'label' => $var->post_title . ' (#' . $var->ID . ')',
13355 'value' => $var->ID,
13356 ];
13357 }
13358 }
13359 }
13360 return [
13361 'options' => $options,
13362 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
13363 ];
13364 } else {
13365 return [
13366 'options' => $options,
13367 'hasMore' => false,
13368 ];
13369 }
13370 }
13371
13372 /**
13373 * Get WS Forms form list
13374 *
13375 * @param array $data data.
13376 *
13377 * @return array
13378 */
13379 public function search_ws_forms_list( $data ) {
13380 $options = [];
13381 global $wpdb;
13382
13383 $forms = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wsf_form", 'ARRAY_A' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
13384
13385 foreach ( $forms as $form ) {
13386 $options[] = [
13387 'label' => $form['label'],
13388 'value' => $form['id'],
13389 ];
13390 }
13391
13392 return [
13393 'options' => $options,
13394 'hasMore' => false,
13395 ];
13396 }
13397
13398 /**
13399 * Get Learndash Achievement list
13400 *
13401 * @param array $data data.
13402 *
13403 * @return array
13404 */
13405 public function search_ld_achievements_list( $data ) {
13406 $page = $data['page'];
13407 $limit = Utilities::get_search_page_limit();
13408 $offset = $limit * ( $page - 1 );
13409
13410 $posts = get_posts(
13411 [
13412 'post_type' => 'ld-achievement',
13413 'posts_per_page' => $limit,
13414 'offset' => $offset,
13415 'post_status' => [ 'publish' ],
13416 ]
13417 );
13418
13419 $posts_count = wp_count_posts( 'ld-achievement' )->publish;
13420
13421 $options = [];
13422
13423 if ( ! empty( $posts ) ) {
13424 foreach ( $posts as $post ) {
13425 $options[] = [
13426 'label' => get_the_title( $post->ID ),
13427 'value' => $post->ID,
13428 ];
13429 }
13430 }
13431
13432 return [
13433 'options' => $options,
13434 'hasMore' => $posts_count > $limit && $posts_count > $offset,
13435 ];
13436 }
13437
13438 /**
13439 * Get Advanced Ads list
13440 *
13441 * @param array $data data.
13442 *
13443 * @return array
13444 */
13445 public function search_ads_list( $data ) {
13446 $page = $data['page'];
13447 $limit = Utilities::get_search_page_limit();
13448 $offset = $limit * ( $page - 1 );
13449
13450 $posts = get_posts(
13451 [
13452 'post_type' => 'advanced_ads',
13453 'posts_per_page' => $limit,
13454 'offset' => $offset,
13455 'post_status' => [ 'publish', 'draft' ],
13456 ]
13457 );
13458
13459 $posts_count = wp_count_posts( 'advanced_ads' )->publish;
13460
13461 $options = [];
13462
13463 if ( ! empty( $posts ) ) {
13464 foreach ( $posts as $post ) {
13465 $options[] = [
13466 'label' => get_the_title( $post->ID ),
13467 'value' => $post->ID,
13468 ];
13469 }
13470 }
13471
13472 return [
13473 'options' => $options,
13474 'hasMore' => $posts_count > $limit && $posts_count > $offset,
13475 ];
13476 }
13477
13478 /**
13479 * Advanced Ads pluggable data.
13480 *
13481 * @param array $data data.
13482 * @return array
13483 */
13484 public function search_ad_last_data( $data ) {
13485 $context = [];
13486 $args = [
13487 'post_type' => 'advanced_ads',
13488 'posts_per_page' => 1,
13489 'orderby' => 'modified',
13490 'order' => 'DESC',
13491 ];
13492
13493 if ( isset( $data['filter']['ad_status']['value'] ) ) {
13494 $post_status = $data['filter']['ad_status']['value'];
13495 $args['post_status'] = $post_status;
13496 }
13497
13498 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
13499 $post_status = $data['filter']['ad_new_status']['value'];
13500 $args['post_status'] = $post_status;
13501 }
13502
13503 if ( isset( $data['filter']['ad_id']['value'] ) ) {
13504 $post_id = $data['filter']['ad_id']['value'];
13505 if ( -1 != $post_id ) {
13506 if ( $post_id > 0 ) {
13507 $args['p'] = $post_id;
13508 }
13509 }
13510 }
13511
13512 $posts = get_posts( $args );
13513 if ( ! empty( $posts ) ) {
13514 $context['pluggable_data'] = $posts[0];
13515 $context['pluggable_data'] = WordPress::get_post_context( $posts[0]->ID );
13516 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
13517 $context['pluggable_data']['ad_new_status'] = $posts[0]->post_status;
13518 }
13519 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
13520 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
13521 }
13522 $context['pluggable_data']['ad_id'] = $posts[0]->ID;
13523 if ( isset( $data['filter']['ad_status']['value'] ) ) {
13524 $context['pluggable_data']['ad_status'] = $posts[0]->post_status;
13525 }
13526 $context['response_type'] = 'live';
13527 } else {
13528 $context['pluggable_data'] = [
13529 'ID' => 1,
13530 'post' => 1,
13531 'post_author' => 1,
13532 'post_date' => '2022-11-18 12:18:14',
13533 'post_date_gmt' => '2022-11-18 12:18:14',
13534 'post_content' => 'Ad Post Content',
13535 'post_title' => 'Ad Post',
13536 'post_excerpt' => '',
13537 'post_status' => 'draft',
13538 'comment_status' => 'open',
13539 'ping_status' => 'open',
13540 'post_password' => '',
13541 'post_name' => 'ad-post',
13542 'to_ping' => '',
13543 'pinged' => '',
13544 'post_modified' => '2022-11-18 12:18:14',
13545 'post_modified_gmt' => '2022-11-18 12:18:14',
13546 'post_content_filtered' => '',
13547 'post_parent' => 0,
13548 'guid' => 'https://example.com/ad-post/',
13549 'menu_order' => 0,
13550 'post_type' => 'advanced_ads',
13551 'post_mime_type' => '',
13552 'comment_count' => 0,
13553 'filter' => 'raw',
13554 ];
13555 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
13556 $context['pluggable_data']['ad_new_status'] = $data['filter']['ad_new_status']['value'];
13557 }
13558 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
13559 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
13560 }
13561 $context['pluggable_data']['ad_id'] = 1;
13562 if ( isset( $data['filter']['ad_status']['value'] ) ) {
13563 $context['pluggable_data']['ad_status'] = $data['filter']['ad_status']['value'];
13564 }
13565 $context['response_type'] = 'sample';
13566 }
13567
13568 return $context;
13569 }
13570
13571 /**
13572 * Get Newsletter lists
13573 *
13574 * @param array $data data.
13575 *
13576 * @return array
13577 */
13578 public function search_newsletter_lists( $data ) {
13579
13580 $options = [];
13581 if ( class_exists( '\Newsletter' ) ) {
13582
13583 $lists = \Newsletter::instance()->get_lists();
13584
13585 if ( ! empty( $lists ) ) {
13586 foreach ( $lists as $list ) {
13587 $options[] = [
13588 'label' => $list->name,
13589 'value' => 'list_' . $list->id,
13590 ];
13591 }
13592 }
13593 }
13594
13595 return [
13596 'options' => $options,
13597 'hasMore' => false,
13598 ];
13599 }
13600
13601 /**
13602 * Newsletter pluggable data.
13603 *
13604 * @param array $data data.
13605 * @return array
13606 */
13607 public function search_newsletter_last_data( $data ) {
13608 $context = [];
13609 global $wpdb;
13610
13611 $list = $data['filter']['list_id']['value'];
13612
13613 if ( -1 == $list ) {
13614 $log = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . "newsletter where status='C' ORDER BY id DESC LIMIT 1" );
13615 } else {
13616 $num = $list;
13617 $location = 1;
13618 $sql = 'SELECT * FROM ' . $wpdb->prefix . "newsletter WHERE $num = %d AND status = 'C' ORDER BY id DESC LIMIT 1";
13619 $log = $wpdb->get_results( $wpdb->prepare( $sql, $location ), ARRAY_A );// @phpcs:ignore
13620 }
13621
13622 if ( ! empty( $log ) ) {
13623 $lists_arr = get_option( 'newsletter_lists' );
13624 if ( -1 == $list ) {
13625 foreach ( $log[0] as $key => $val ) {
13626 if ( defined( 'NEWSLETTER_LIST_MAX' ) ) {
13627 for ( $i = 1; $i <= NEWSLETTER_LIST_MAX; $i++ ) {
13628 $list_key = "list_$i";
13629
13630 if ( $key == $list_key ) {
13631 if ( 1 == $val ) {
13632 $context['pluggable_data']['list_id'] = $key;
13633 if ( is_array( $lists_arr ) ) {
13634 if ( isset( $lists_arr[ $key ] ) ) {
13635 $list_name = $lists_arr[ $key ];
13636 $context['pluggable_data']['list_name'] = $list_name;
13637 }
13638 }
13639 continue;
13640 }
13641 }
13642 }
13643 }
13644 if ( 'email' == $key ) {
13645 $context['pluggable_data']['email'] = $val;
13646 }
13647 }
13648 } else {
13649 $context['pluggable_data']['list_id'] = $list;
13650 $context['pluggable_data']['email'] = $log[0]['email'];
13651 if ( is_array( $lists_arr ) ) {
13652 if ( isset( $lists_arr[ $list ] ) ) {
13653 $list_name = $lists_arr[ $list ];
13654 $context['pluggable_data']['list_name'] = $list_name;
13655 }
13656 }
13657 }
13658 $context['response_type'] = 'live';
13659 } else {
13660 $context = json_decode( '{"response_type":"sample","pluggable_data":{"list_id": "list_1","email": "johnd@mailinator.com","list_name": "Contact List"}}', true );
13661 }
13662
13663 return $context;
13664 }
13665
13666 /**
13667 * Get wpForo Forum list
13668 *
13669 * @param array $data data.
13670 *
13671 * @return array
13672 */
13673 public function search_wp_forum_list( $data ) {
13674
13675 if ( ! function_exists( 'WPF' ) ) {
13676 return [];
13677 }
13678
13679 $forums = WPF()->forum->get_forums( [ 'type' => 'forum' ] );
13680
13681 $options = [];
13682
13683 if ( ! empty( $forums ) ) {
13684 foreach ( $forums as $forum ) {
13685 $options[] = [
13686 'label' => $forum['title'],
13687 'value' => $forum['forumid'],
13688 ];
13689 }
13690 }
13691
13692 return [
13693 'options' => $options,
13694 'hasMore' => false,
13695 ];
13696 }
13697
13698 /**
13699 * Get wpForo Topic list
13700 *
13701 * @param array $data data.
13702 *
13703 * @return array
13704 */
13705 public function search_wp_topic_list( $data ) {
13706
13707 if ( ! function_exists( 'WPF' ) ) {
13708 return [];
13709 }
13710
13711 $forum_id = $data['dynamic'];
13712
13713 $topics = WPF()->topic->get_topics( [ 'forumid' => $forum_id ] );
13714
13715 $options = [];
13716
13717 if ( ! empty( $topics ) ) {
13718 foreach ( $topics as $topic ) {
13719 $options[] = [
13720 'label' => $topic['title'],
13721 'value' => $topic['topicid'],
13722 ];
13723 }
13724 }
13725
13726 return [
13727 'options' => $options,
13728 'hasMore' => false,
13729 ];
13730 }
13731
13732 /**
13733 * Get wpForo Groups list
13734 *
13735 * @param array $data data.
13736 *
13737 * @return array
13738 */
13739 public function search_wp_user_groups_list( $data ) {
13740
13741 if ( ! function_exists( 'WPF' ) ) {
13742 return [];
13743 }
13744
13745 $usergroups = WPF()->usergroup->get_usergroups();
13746
13747 $options = [];
13748
13749 if ( ! empty( $usergroups ) ) {
13750 foreach ( $usergroups as $group ) {
13751 $options[] = [
13752 'label' => $group['name'],
13753 'value' => intval( $group['groupid'] ),
13754 ];
13755 }
13756 }
13757
13758 return [
13759 'options' => $options,
13760 'hasMore' => false,
13761 ];
13762 }
13763
13764 /**
13765 * Get wpForo Reputation list
13766 *
13767 * @param array $data data.
13768 *
13769 * @return array
13770 */
13771 public function search_wp_foro_reputation_list( $data ) {
13772
13773 if ( ! function_exists( 'WPF' ) ) {
13774 return [];
13775 }
13776
13777 $levels = WPF()->member->levels();
13778 $options = [];
13779
13780 if ( ! empty( $levels ) ) {
13781 foreach ( $levels as $level ) {
13782 $options[] = [
13783 'label' => esc_attr__( 'Level', 'suretriggers' ) . ' ' . $level . ' - ' . WPF()->member->rating( $level, 'title' ),
13784 'value' => strval( $level ),
13785 ];
13786 }
13787 }
13788
13789 return [
13790 'options' => $options,
13791 'hasMore' => false,
13792 ];
13793 }
13794
13795 /**
13796 * WPForo new topic pluggable data.
13797 *
13798 * @param array $data data.
13799 * @return array
13800 */
13801 public function search_pluggables_wpforo_topic_last_data( $data ) {
13802 $context = [];
13803 global $wpdb;
13804
13805 $forum_id = $data['filter']['forum_id']['value'];
13806
13807 if ( -1 == $forum_id ) {
13808 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'wpforo_topics WHERE closed = 0 ORDER BY topicid DESC LIMIT 1', ARRAY_A );
13809 } else {
13810 $forum = $forum_id;
13811 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_topics WHERE forumid = %d AND closed = 0 ORDER BY topicid DESC LIMIT 1';
13812 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum ), ARRAY_A );// @phpcs:ignore
13813 }
13814
13815 if ( ! empty( $results ) ) {
13816 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
13817 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
13818
13819 if ( function_exists( 'WPF' ) ) {
13820 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
13821 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
13822 }
13823 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
13824 $context['response_type'] = 'live';
13825 } else {
13826 $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
13827 }
13828
13829 return $context;
13830 }
13831
13832 /**
13833 * WpForo Topic Reply pluggable data.
13834 *
13835 * @param array $data data.
13836 * @return array
13837 */
13838 public function search_pluggables_wpforo_topic_reply_last_data( $data ) {
13839 $context = [];
13840 global $wpdb;
13841
13842 $forum_id = $data['filter']['forum_id']['value'];
13843 $topic_id = $data['filter']['topic_id']['value'];
13844
13845 if ( -1 == $forum_id && -1 != $topic_id ) {
13846 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE topicid = %d ORDER BY postid DESC LIMIT 1';
13847 $results = $wpdb->get_results( $wpdb->prepare( $sql, $topic_id ), ARRAY_A );// @phpcs:ignore
13848 } elseif ( -1 != $forum_id && -1 == $topic_id ) {
13849 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d ORDER BY postid DESC LIMIT 1';
13850 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id ), ARRAY_A );// @phpcs:ignore
13851 } elseif ( -1 == $forum_id && -1 == $topic_id ) {
13852 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts ORDER BY postid DESC LIMIT 1';
13853 $results = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );// @phpcs:ignore
13854 } else {
13855 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d AND topicid = %d ORDER BY postid DESC LIMIT 1';
13856 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id, $topic_id ), ARRAY_A );// @phpcs:ignore
13857 }
13858
13859 if ( ! empty( $results ) ) {
13860 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
13861 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
13862 if ( function_exists( 'WPF' ) ) {
13863 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
13864 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
13865 $context['pluggable_data']['reply'] = WPF()->post->get_post( $results[0]['postid'] );
13866 }
13867 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
13868 $context['response_type'] = 'live';
13869 } else {
13870 $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
13871 }
13872
13873 return $context;
13874 }
13875
13876 /**
13877 * Get RafflePress Giveaways list
13878 *
13879 * @param array $data data.
13880 *
13881 * @return array
13882 */
13883 public function search_rp_giveaways_list( $data ) {
13884
13885 global $wpdb;
13886 $options = [];
13887
13888 $giveaways = $wpdb->get_results( "SELECT id,name FROM {$wpdb->prefix}rafflepress_giveaways WHERE deleted_at is null ORDER BY name ASC", ARRAY_A );
13889 foreach ( $giveaways as $giveaway ) {
13890 $options[] = [
13891 'label' => $giveaway['name'],
13892 'value' => $giveaway['id'],
13893 ];
13894 }
13895
13896 return [
13897 'options' => $options,
13898 'hasMore' => false,
13899 ];
13900 }
13901
13902 /**
13903 * Get RafflePress Giveaway Actions list
13904 *
13905 * @param array $data data.
13906 *
13907 * @return array
13908 */
13909 public function search_rp_giveaway_actions_list( $data ) {
13910
13911 global $wpdb;
13912 $options = [];
13913
13914 $giveaway = $wpdb->get_row( $wpdb->prepare( "SELECT settings FROM {$wpdb->prefix}rafflepress_giveaways WHERE id=%d", $data['dynamic'] ), ARRAY_A );
13915
13916 if ( is_array( $giveaway ) && isset( $giveaway['settings'] ) ) {
13917 $settings = json_decode( $giveaway['settings'], true );
13918
13919 if ( is_array( $settings ) && isset( $settings['entry_options'] ) ) {
13920 foreach ( $settings['entry_options'] as $action ) {
13921 $options[] = [
13922 'label' => $action['name'],
13923 'value' => $action['id'],
13924 ];
13925 }
13926 }
13927 }
13928
13929 return [
13930 'options' => $options,
13931 'hasMore' => false,
13932 ];
13933 }
13934
13935 /**
13936 * Get last data for trigger.
13937 *
13938 * @param array $data data.
13939 * @return array
13940 */
13941 public function search_raffle_press_triggers_last_data( $data ) {
13942
13943 global $wpdb;
13944
13945 $context = [];
13946 $context['response_type'] = 'sample';
13947
13948 $pluggable_data = [
13949 'giveaway_id' => 1,
13950 'giveaway_title' => 'Sample Giveaway (ID#1)',
13951 'giveaway_start_date' => 'September 20, 2023',
13952 'giveaway_end_date' => 'October 6, 2023',
13953 'giveaway_entries' => 9,
13954 'giveaway_user_count' => 3,
13955 'giveaway_status' => 'Active',
13956 'contestant_name' => 'John Doe',
13957 'contestant_email' => 'john_doe@gmail.com',
13958 'contestant_email_verified' => 'Yes',
13959 'action_id' => '0jnex',
13960 'action_name' => 'Visit us on Instagram',
13961 ];
13962
13963 $giveaway_id = isset( $data['filter']['giveaway_id'] ) ? $data['filter']['giveaway_id']['value'] : null;
13964 $action_id = isset( $data['filter']['action_id'] ) ? $data['filter']['action_id']['value'] : null;
13965
13966 $query = "SELECT contestant_id, giveaway_id, action_id, meta FROM {$wpdb->prefix}rafflepress_entries";
13967
13968 if ( $giveaway_id && -1 != $giveaway_id ) {
13969 $query .= ' WHERE giveaway_id = ' . $giveaway_id;
13970
13971 if ( $action_id ) {
13972 $query .= " AND action_id = '" . $action_id . "'";
13973 }
13974 }
13975
13976 $query .= ' ORDER BY created_at DESC LIMIT 1';
13977
13978 $giveaway_data = $wpdb->get_row( $query, ARRAY_A ); // @phpcs:ignore
13979
13980 if ( ! empty( $giveaway_data ) ) {
13981 $pluggable_data = array_merge(
13982 RafflePress::get_giveaway_context( $giveaway_data['giveaway_id'] ),
13983 RafflePress::get_contestant_context( $giveaway_data['contestant_id'] )
13984 );
13985
13986 $pluggable_data['performed_action_id'] = isset( $giveaway_data['action_id'] ) ? $giveaway_data['action_id'] : 0;
13987
13988 $giveaway_meta = isset( $giveaway_data['meta'] ) ? json_decode( $giveaway_data['meta'], true ) : [];
13989 $pluggable_data['performed_action_name'] = is_array( $giveaway_meta ) && isset( $giveaway_meta['action'] ) ? $giveaway_meta['action'] : '';
13990
13991 $context['response_type'] = 'live';
13992 }
13993
13994 $context['pluggable_data'] = $pluggable_data;
13995
13996 return $context;
13997 }
13998
13999 /**
14000 * Get last data for trigger
14001 *
14002 * @param array $data data.
14003 * @return array
14004 */
14005 public function search_woo_commerce_shipstation_triggers_last_data( $data ) {
14006 $context = [];
14007 $context['response_type'] = 'sample';
14008 $context['pluggable_data'] = [];
14009
14010 $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
14011
14012 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
14013 $condition = $data['filter']['condition_compare']['value'];
14014 $price = $data['filter']['price']['value'];
14015 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
14016
14017 if ( 'order_shipped' === $term ) {
14018 $orders = wc_get_orders(
14019 [
14020 'numberposts' => 1,
14021 'orderby' => 'date',
14022 'order' => 'DESC',
14023 'status' => 'completed',
14024 'meta_query' => [
14025 [
14026 'key' => '_shipstation_shipped_item_count',
14027 'compare' => 'EXISTS',
14028 ],
14029 ],
14030 ]
14031 );
14032 if ( count( $orders ) > 0 ) {
14033 $order_id = $orders[0]->get_id();
14034 $order = wc_get_order( $order_id );
14035 $user_id = $order->get_customer_id();
14036 $product_ids = [];
14037 if ( $order ) {
14038 $items = $order->get_items();
14039 foreach ( $items as $item ) {
14040 if ( method_exists( $item, 'get_product_id' ) ) {
14041 $product_ids[] = $item->get_product_id();
14042 }
14043 }
14044
14045 foreach ( $product_ids as $product_id ) {
14046 $context['product_id'] = $product_id;
14047 }
14048 }
14049 $context = array_merge(
14050 WooCommerce::get_order_context( $order_id ),
14051 WordPress::get_user_context( $user_id )
14052 );
14053 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
14054 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
14055 /**
14056 *
14057 * Ignore line
14058 *
14059 * @phpstan-ignore-next-line
14060 */
14061 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
14062 /**
14063 *
14064 * Ignore line
14065 *
14066 * @phpstan-ignore-next-line
14067 */
14068 $date = date_i18n( get_option( 'date_format' ), $timestamp );
14069 $context['ship_date'] = $date;
14070 $context['response_type'] = 'live';
14071 }
14072
14073 $context['pluggable_data'] = $context;
14074 } elseif ( 'specific_product_order_shipped' === $term ) {
14075 if ( -1 != $product_id ) {
14076 $product_ids = [ $product_id ];
14077 $orders = wc_get_orders(
14078 [
14079 'numberposts' => 1,
14080 'orderby' => 'date',
14081 'order' => 'DESC',
14082 'status' => 'completed',
14083 'meta_query' => [
14084 [
14085 'key' => '_shipstation_shipped_item_count',
14086 'compare' => 'EXISTS',
14087 ],
14088 [
14089 'key' => '_product_id',
14090 'value' => $product_ids,
14091 'compare' => 'IN',
14092 ],
14093 ],
14094 ]
14095 );
14096 } else {
14097 $orders = wc_get_orders(
14098 [
14099 'numberposts' => 1,
14100 'orderby' => 'date',
14101 'order' => 'DESC',
14102 'status' => 'completed',
14103 'meta_query' => [
14104 [
14105 'key' => '_shipstation_shipped_item_count',
14106 'compare' => 'EXISTS',
14107 ],
14108 ],
14109 ]
14110 );
14111 }
14112 if ( count( $orders ) > 0 ) {
14113 $order_id = $orders[0]->get_id();
14114 $order = wc_get_order( $order_id );
14115 $user_id = $order->get_customer_id();
14116 $productids = [];
14117 $items = $order->get_items();
14118 foreach ( $items as $item ) {
14119 if ( method_exists( $item, 'get_product_id' ) ) {
14120 $productids[] = $item->get_product_id();
14121 }
14122 }
14123 $context = array_merge(
14124 WooCommerce::get_order_context( $order_id ),
14125 WordPress::get_user_context( $user_id )
14126 );
14127
14128 foreach ( $productids as $product_id ) {
14129 $context['product_id'] = $product_id;
14130 }
14131
14132 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
14133 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
14134 /**
14135 *
14136 * Ignore line
14137 *
14138 * @phpstan-ignore-next-line
14139 */
14140 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
14141 /**
14142 *
14143 * Ignore line
14144 *
14145 * @phpstan-ignore-next-line
14146 */
14147 $date = date_i18n( get_option( 'date_format' ), $timestamp );
14148 $context['ship_date'] = $date;
14149 $context['response_type'] = 'live';
14150 }
14151
14152 $context['pluggable_data'] = $context;
14153 } elseif ( 'specific_amount_order_shipped' === $term ) {
14154 $orders = wc_get_orders(
14155 [
14156 'numberposts' => 1,
14157 'orderby' => 'date',
14158 'order' => 'DESC',
14159 'status' => 'completed',
14160 'meta_query' => [
14161 [
14162 'key' => '_shipstation_shipped_item_count',
14163 'compare' => 'EXISTS',
14164 ],
14165 [
14166 'key' => '_order_total',
14167 'value' => $price,
14168 'compare' => $condition,
14169 ],
14170 ],
14171 ]
14172 );
14173 if ( count( $orders ) > 0 ) {
14174 $order_id = $orders[0]->get_id();
14175 $order = wc_get_order( $order_id );
14176 $user_id = $order->get_customer_id();
14177 $product_ids = [];
14178 $items = $order->get_items();
14179 foreach ( $items as $item ) {
14180 if ( method_exists( $item, 'get_product_id' ) ) {
14181 $product_ids[] = $item->get_product_id();
14182 }
14183 }
14184 $context = array_merge(
14185 WooCommerce::get_order_context( $order_id ),
14186 WordPress::get_user_context( $user_id )
14187 );
14188
14189 foreach ( $product_ids as $product_id ) {
14190 $context['product_id'] = $product_id;
14191 }
14192
14193 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
14194 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
14195 /**
14196 *
14197 * Ignore line
14198 *
14199 * @phpstan-ignore-next-line
14200 */
14201 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
14202 /**
14203 *
14204 * Ignore line
14205 *
14206 * @phpstan-ignore-next-line
14207 */
14208 $date = date_i18n( get_option( 'date_format' ), $timestamp );
14209 $context['ship_date'] = $date;
14210 $context['response_type'] = 'live';
14211 }
14212
14213 $context['pluggable_data'] = $context;
14214 }
14215
14216 return $context;
14217 }
14218
14219 /**
14220 * Get GroundHogg Tag list
14221 *
14222 * @param array $data data.
14223 *
14224 * @return array
14225 */
14226 public function search_groundhogg_tag_list( $data ) {
14227
14228 if ( ! function_exists( 'Groundhogg\get_db' ) ) {
14229 return [];
14230 }
14231
14232 $tags = \Groundhogg\get_db( 'tags' )->query( [] );
14233 $options = [];
14234
14235 if ( ! empty( $tags ) ) {
14236 foreach ( $tags as $tag ) {
14237 $options[] = [
14238 'label' => $tag->tag_name,
14239 'value' => $tag->tag_id,
14240 ];
14241 }
14242 }
14243
14244 return [
14245 'options' => $options,
14246 'hasMore' => false,
14247 ];
14248 }
14249
14250 /**
14251 * Get WP courseware courses list
14252 *
14253 * @param array $data data.
14254 *
14255 * @return array
14256 */
14257 public function search_wpcw_courses( $data ) {
14258 $options = [];
14259
14260 $page = $data['page'];
14261 $limit = Utilities::get_search_page_limit();
14262 $offset = $limit * ( $page - 1 );
14263
14264 $args = [
14265 'post_type' => 'wpcw_course',
14266 'posts_per_page' => $limit,
14267 'offset' => $offset,
14268 'orderby' => 'title',
14269 'order' => 'ASC',
14270 'post_status' => 'publish',
14271 ];
14272
14273 $courses = get_posts( $args );
14274
14275 $course_count = wp_count_posts( 'wpcw_course' )->publish;
14276
14277 if ( ! empty( $courses ) ) {
14278 if ( is_array( $courses ) ) {
14279 foreach ( $courses as $course ) {
14280 $options[] = [
14281 'label' => $course->post_title,
14282 'value' => $course->ID,
14283 ];
14284 }
14285 }
14286 }
14287 return [
14288 'options' => $options,
14289 'hasMore' => $course_count > $limit && $course_count > $offset,
14290 ];
14291 }
14292
14293 /**
14294 * Get WP courseware courses list
14295 *
14296 * @param array $data data.
14297 *
14298 * @return array
14299 */
14300 public function search_wpcw_modules( $data ) {
14301 $options = [];
14302
14303 if ( function_exists( 'wpcw_get_modules' ) ) {
14304 $modules = wpcw_get_modules();
14305 }
14306
14307 if ( ! empty( $modules ) ) {
14308 if ( is_array( $modules ) ) {
14309 foreach ( $modules as $module ) {
14310 $options[] = [
14311 'label' => $module->module_title,
14312 'value' => $module->module_id,
14313 ];
14314 }
14315 }
14316 }
14317 return [
14318 'options' => $options,
14319 'hasMore' => false,
14320 ];
14321 }
14322
14323 /**
14324 * Get WP courseware unit list
14325 *
14326 * @param array $data data.
14327 *
14328 * @return array
14329 */
14330 public function search_wpcw_units( $data ) {
14331 $options = [];
14332
14333 $page = $data['page'];
14334 $limit = Utilities::get_search_page_limit();
14335 $offset = $limit * ( $page - 1 );
14336
14337 $args = [
14338 'post_type' => 'course_unit',
14339 'posts_per_page' => $limit,
14340 'offset' => $offset,
14341 'orderby' => 'title',
14342 'order' => 'ASC',
14343 'post_status' => 'publish',
14344 ];
14345
14346 $units = get_posts( $args );
14347
14348 $unit_count = wp_count_posts( 'course_unit' )->publish;
14349
14350 if ( ! empty( $units ) ) {
14351 if ( is_array( $units ) ) {
14352 foreach ( $units as $unit ) {
14353 $options[] = [
14354 'label' => $unit->post_title,
14355 'value' => $unit->ID,
14356 ];
14357 }
14358 }
14359 }
14360 return [
14361 'options' => $options,
14362 'hasMore' => $unit_count > $limit && $unit_count > $offset,
14363 ];
14364 }
14365
14366 /**
14367 * Search WP Courseware data.
14368 *
14369 * @param array $data data.
14370 * @return array|void
14371 */
14372 public function search_wpcw_last_data( $data ) {
14373 global $wpdb;
14374 $post_type = $data['post_type'];
14375 $trigger = $data['search_term'];
14376 $context = [];
14377
14378 if ( 'wpcw_course_completed' === $trigger ) {
14379 $post_id = $data['filter']['course_post_id']['value'];
14380 if ( -1 === $post_id ) {
14381 $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" );
14382 } else {
14383 $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 ) );
14384 }
14385 } elseif ( 'wpcw_module_completed' === $trigger ) {
14386 $post_id = $data['filter']['module_id']['value'];
14387 if ( -1 === $post_id ) {
14388 $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" );
14389 } else {
14390 $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 ) );
14391 }
14392 } elseif ( 'wpcw_unit_completed' === $trigger ) {
14393 $post_id = $data['filter']['unit_id']['value'];
14394 if ( -1 === $post_id ) {
14395 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpcw_user_progress WHERE unit_completed_status='complete' order by unit_id DESC LIMIT 1" );
14396 } else {
14397 $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 ) );
14398 }
14399 } elseif ( 'wpcw_enroll_course' === $trigger ) {
14400 $post_id = $data['filter']['course_post_id']['value'];
14401 if ( -1 === $post_id ) {
14402 $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" );
14403 } else {
14404 $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 ) );
14405 }
14406 }
14407
14408 if ( ! empty( $result ) ) {
14409
14410 switch ( $trigger ) {
14411 case 'wpcw_course_completed':
14412 $result_course_id = $result[0]->course_id;
14413 $result_user_id = $result[0]->user_id;
14414 if ( function_exists( 'wpcw_get_course' ) ) {
14415 $course = wpcw_get_course( $result_course_id );
14416 if ( is_object( $course ) ) {
14417 $course = get_object_vars( $course );
14418 }
14419 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course );
14420 }
14421 break;
14422 case 'wpcw_module_completed':
14423 $result_module_id = $result[0]->module_id;
14424 $result_user_id = $result[0]->user_id;
14425 if ( function_exists( 'wpcw_get_module' ) ) {
14426 $module = wpcw_get_module( $result_module_id );
14427 if ( is_object( $module ) ) {
14428 $module = get_object_vars( $module );
14429 }
14430 $context = array_merge( WordPress::get_user_context( $result_user_id ), $module );
14431 }
14432 break;
14433 case 'wpcw_unit_completed':
14434 $result_unit_id = $result[0]->unit_id;
14435 $result_user_id = $result[0]->user_id;
14436 if ( function_exists( 'wpcw_get_unit' ) ) {
14437 $unit = wpcw_get_unit( $result_unit_id );
14438 if ( is_object( $unit ) ) {
14439 $unit = get_object_vars( $unit );
14440 $unit['name'] = get_the_title( $result_unit_id );
14441 }
14442 $context = array_merge( WordPress::get_user_context( $result_user_id ), $unit );
14443 }
14444 break;
14445 case 'wpcw_enroll_course':
14446 $result_course_id = $result[0]->course_id;
14447 $result_user_id = $result[0]->user_id;
14448 if ( function_exists( 'WPCW_courses_getCourseDetails' ) ) {
14449 $course_detail = WPCW_courses_getCourseDetails( $result_course_id );
14450 if ( is_object( $course_detail ) ) {
14451 $course_detail = get_object_vars( $course_detail );
14452 }
14453 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course_detail );
14454 }
14455 break;
14456 default:
14457 return;
14458 }
14459 $context['pluggable_data'] = $context;
14460 $context['response_type'] = 'live';
14461 }
14462
14463 return $context;
14464
14465 }
14466
14467 /**
14468 * Get WooCommerce Order Note list.
14469 *
14470 * @param array $data data.
14471 *
14472 * @return array
14473 */
14474 public function search_note_type_list( $data ) {
14475
14476 $options = [];
14477
14478 $options[] = [
14479 'label' => 'Customer',
14480 'value' => 'customer',
14481 ];
14482 $options[] = [
14483 'label' => 'Private',
14484 'value' => 'internal',
14485 ];
14486
14487 return [
14488 'options' => $options,
14489 'hasMore' => false,
14490 ];
14491 }
14492
14493 /**
14494 * Get last data for trigger
14495 *
14496 * @param array $data data.
14497 * @return array
14498 */
14499 public function search_woo_commerce_customers_triggers_last_data( $data ) {
14500 $context = [];
14501 $context['response_type'] = 'sample';
14502 $context['pluggable_data'] = [];
14503 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
14504 global $wpdb;
14505 if ( 'customer_created' === $term ) {
14506 $customer_query = get_users(
14507 [
14508 'fields' => 'ID',
14509 'role' => 'customer',
14510 'orderby' => 'ID',
14511 'order' => 'DESC',
14512 'number' => 1,
14513 ]
14514 );
14515 $results = $customer_query;
14516 if ( ! empty( $results ) ) {
14517 $customer = new WC_Customer( $results[0] );
14518 $last_order = $customer->get_last_order();
14519 $customer_data = [
14520 'id' => $customer->get_id(),
14521 'email' => $customer->get_email(),
14522 'first_name' => $customer->get_first_name(),
14523 'last_name' => $customer->get_last_name(),
14524 'username' => $customer->get_username(),
14525 'last_order_id' => is_object( $last_order ) ? $last_order->get_id() : null,
14526 'orders_count' => $customer->get_order_count(),
14527 'total_spent' => wc_format_decimal( $customer->get_total_spent(), 2 ),
14528 'avatar_url' => $customer->get_avatar_url(),
14529 'billing_address' => [
14530 'first_name' => $customer->get_billing_first_name(),
14531 'last_name' => $customer->get_billing_last_name(),
14532 'company' => $customer->get_billing_company(),
14533 'address_1' => $customer->get_billing_address_1(),
14534 'address_2' => $customer->get_billing_address_2(),
14535 'city' => $customer->get_billing_city(),
14536 'state' => $customer->get_billing_state(),
14537 'postcode' => $customer->get_billing_postcode(),
14538 'country' => $customer->get_billing_country(),
14539 'email' => $customer->get_billing_email(),
14540 'phone' => $customer->get_billing_phone(),
14541 ],
14542 'shipping_address' => [
14543 'first_name' => $customer->get_shipping_first_name(),
14544 'last_name' => $customer->get_shipping_last_name(),
14545 'company' => $customer->get_shipping_company(),
14546 'address_1' => $customer->get_shipping_address_1(),
14547 'address_2' => $customer->get_shipping_address_2(),
14548 'city' => $customer->get_shipping_city(),
14549 'state' => $customer->get_shipping_state(),
14550 'postcode' => $customer->get_shipping_postcode(),
14551 'country' => $customer->get_shipping_country(),
14552 ],
14553 ];
14554 if ( is_object( $last_order ) && method_exists( $last_order, 'get_date_created' ) ) {
14555 $created_date = $last_order->get_date_created();
14556 if ( is_object( $created_date ) && method_exists( $created_date, 'getTimestamp' ) ) {
14557 $last_order_date = $created_date->getTimestamp();
14558 $customer_data['created_at'] = $last_order_date;
14559 $customer_data['last_order_date'] = $last_order_date;
14560 }
14561 }
14562 $order_sample_data = $customer_data;
14563 $context['response_type'] = 'live';
14564 $context['pluggable_data'] = $order_sample_data;
14565 } else {
14566 $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 );
14567 $context['pluggable_data'] = $order_sample_data;
14568 }
14569 } elseif ( 'total_spend_reach' === $term ) {
14570 $total_spend_selected = isset( $data['filter']['total_spend']['value'] ) ? $data['filter']['total_spend']['value'] : '';
14571 $customers = $wpdb->get_col(
14572 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
14573 WHERE meta_key = '_customer_user' AND meta_value > 0"
14574 );
14575 $target_customer = null;
14576 foreach ( $customers as $customer ) {
14577 $total_spend = (int) wc_get_customer_total_spent( $customer );
14578 if ( $total_spend == $total_spend_selected ) {
14579 $target_customer = $customer;
14580 break; // Exit the loop once a matching customer is found.
14581 }
14582 }
14583 if ( $target_customer ) {
14584 $new_customer = new WC_Customer( $target_customer );
14585 $new_customer_data = [
14586 'id' => $new_customer->get_id(),
14587 'email' => $new_customer->get_email(),
14588 'first_name' => $new_customer->get_first_name(),
14589 'last_name' => $new_customer->get_last_name(),
14590 'username' => $new_customer->get_username(),
14591 'order_count' => $new_customer->get_order_count(),
14592 'total_spend' => wc_format_decimal( $new_customer->get_total_spent(), 2 ),
14593 ];
14594 $context['response_type'] = 'live';
14595 $context['pluggable_data'] = $new_customer_data;
14596 } else {
14597 $sample_customer_data = [
14598 'id' => '101',
14599 'created_at' => '1680675247',
14600 'email' => 'john@d.com',
14601 'first_name' => 'John',
14602 'last_name' => 'D',
14603 'username' => 'johnd',
14604 'order_count' => '3',
14605 'total_spend' => '22.00',
14606 ];
14607 $context['pluggable_data'] = $sample_customer_data;
14608 }
14609 } elseif ( 'order_count_reach' === $term ) {
14610 $total_order_selected = isset( $data['filter']['order_count']['value'] ) ? $data['filter']['order_count']['value'] : '';
14611 $customers = $wpdb->get_col(
14612 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
14613 WHERE meta_key = '_customer_user' AND meta_value > 0"
14614 );
14615 $target_customer = null;
14616 foreach ( $customers as $customer ) {
14617 $args = [
14618 'customer_id' => $customer,
14619 'limit' => -1,
14620 'status' => [ 'wc-completed' ],
14621 ];
14622 $orders = wc_get_orders( $args );
14623 if ( ! empty( $orders ) ) {
14624 $total_order = (int) wc_get_customer_order_count( $customer );
14625 if ( $total_order == $total_order_selected ) {
14626 $target_customer = $customer;
14627 break; // Exit the loop once a matching customer is found.
14628 }
14629 }
14630 }
14631 if ( $target_customer ) {
14632 $new_customer = new WC_Customer( $target_customer );
14633 $new_customer_data = [
14634 'id' => $new_customer->get_id(),
14635 'email' => $new_customer->get_email(),
14636 'first_name' => $new_customer->get_first_name(),
14637 'last_name' => $new_customer->get_last_name(),
14638 'username' => $new_customer->get_username(),
14639 'order_count' => $new_customer->get_order_count(),
14640 ];
14641 $context['response_type'] = 'live';
14642 $context['pluggable_data'] = $new_customer_data;
14643 } else {
14644 $sample_customer_data = [
14645 'id' => '101',
14646 'created_at' => '1680675247',
14647 'email' => 'john@d.com',
14648 'first_name' => 'John',
14649 'last_name' => 'D',
14650 'username' => 'johnd',
14651 'orders_count' => '3',
14652 ];
14653 $context['pluggable_data'] = $sample_customer_data;
14654 }
14655 }
14656
14657 return $context;
14658 }
14659
14660 /**
14661 * Get Affiliate list
14662 *
14663 * @param array $data data.
14664 *
14665 * @return array
14666 */
14667 public function search_affiliate_list( $data ) {
14668 $options = [];
14669
14670 $args = [
14671 'meta_query' => [
14672 [
14673 'key' => 'wafp_is_affiliate',
14674 'value' => '1',
14675 'compare' => '=',
14676 ],
14677 ],
14678 ];
14679
14680 $affiliates = get_users( $args );
14681
14682 foreach ( $affiliates as $user ) {
14683 $options[] = [
14684 'label' => $user->display_name,
14685 'value' => $user->ID,
14686 ];
14687 }
14688 return [
14689 'options' => $options,
14690 'hasMore' => false,
14691 ];
14692 }
14693
14694 /**
14695 * Get Affiliate Source list
14696 *
14697 * @param array $data data.
14698 *
14699 * @return array
14700 */
14701 public function search_affiliate_transaction_source_list( $data ) {
14702 $options = [];
14703
14704 $sources = [
14705 'General',
14706 'MemberPress',
14707 'WooCommerce',
14708 'Easy Digital Downloads',
14709 'WPForms',
14710 'Formidable',
14711 'PayPal',
14712 ];
14713 foreach ( $sources as $source ) {
14714 $options[] = [
14715 'label' => $source,
14716 'value' => str_replace( ' ', '_', strtolower( $source ) ),
14717 ];
14718 }
14719
14720 return [
14721 'options' => $options,
14722 'hasMore' => false,
14723 ];
14724 }
14725
14726 /**
14727 * Search Easy Affiliate data.
14728 *
14729 * @param array $data data.
14730 * @return array|void
14731 */
14732 public function search_easy_affiliate_last_data( $data ) {
14733
14734 global $wpdb;
14735 $trigger = $data['search_term'];
14736 $context = [];
14737
14738 if ( 'sale_recorded' === $trigger ) {
14739 $affiliate_id = $data['filter']['affiliate_id']['value'];
14740 if ( -1 === $affiliate_id ) {
14741 $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" );
14742 } else {
14743 $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 ) );
14744 }
14745 } elseif ( 'sale_added' === $trigger ) {
14746 $orderby = 'signup_date';
14747 $order = 'DESC';
14748 $paged = 1;
14749 $search = '';
14750 $perpage = 1;
14751 /**
14752 *
14753 * Ignore line
14754 *
14755 * @phpstan-ignore-next-line
14756 */
14757 $result = \EasyAffiliate\Models\User::affiliate_list_table( $orderby, $order, $paged, $search, $perpage );
14758 if ( empty( $result['results'] ) ) {
14759 $result = [];
14760 }
14761 } elseif ( 'payout_made' === $trigger ) {
14762 $affiliate_id = $data['filter']['affiliate_id']['value'];
14763 if ( -1 === $affiliate_id ) {
14764 $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" );
14765 } else {
14766 $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 ) );
14767 }
14768 }
14769
14770 if ( ! empty( $result ) ) {
14771 switch ( $trigger ) {
14772 case 'sale_recorded':
14773 $result_affiliate_id = $result[0]->affiliate_id;
14774 /**
14775 *
14776 * Ignore line
14777 *
14778 * @phpstan-ignore-next-line
14779 */
14780 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
14781 $affiliate = get_object_vars( $affiliate->rec );
14782 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
14783 break;
14784 case 'sale_added':
14785 /**
14786 *
14787 * Ignore line
14788 *
14789 * @phpstan-ignore-next-line
14790 */
14791 $data = new \EasyAffiliate\Models\User( $result['results'][0]->ID );
14792 /**
14793 *
14794 * Ignore line
14795 *
14796 * @phpstan-ignore-next-line
14797 */
14798 $context = get_object_vars( $data->rec );
14799 break;
14800 case 'payout_made':
14801 $result_affiliate_id = $result[0]->affiliate_id;
14802 /**
14803 *
14804 * Ignore line
14805 *
14806 * @phpstan-ignore-next-line
14807 */
14808 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
14809 $affiliate = get_object_vars( $affiliate->rec );
14810 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
14811 break;
14812 default:
14813 return;
14814 }
14815 $context['pluggable_data'] = $context;
14816 $context['response_type'] = 'live';
14817 } elseif ( empty( $result ) ) {
14818 switch ( $trigger ) {
14819 case 'sale_added':
14820 $context = json_decode(
14821 '{"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": "",
14822 "city": "london","state": "","zip": "","country": "UK","tax_id_us": "","tax_id_int": "","is_affiliate": "1","is_blocked": "","blocked_message": "","referrer": "0","notes": "","unsubscribed": ""}',
14823 true
14824 );
14825 break;
14826 case 'sale_recorded':
14827 $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 );
14828 break;
14829 case 'payout_made':
14830 $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 );
14831 break;
14832 default:
14833 return;
14834 }
14835 $context['pluggable_data'] = $context;
14836 $context['response_type'] = 'sample';
14837 }
14838
14839 return $context;
14840
14841 }
14842
14843 /**
14844 * Get WooCommerce Subscriptions Coupon list
14845 *
14846 * @param array $data data.
14847 *
14848 * @return array
14849 */
14850 public function search_woo_subscription_coupon_list( $data ) {
14851 $options = [];
14852
14853 $coupon_codes = [];
14854
14855 $query = new \WP_Query(
14856 [
14857 'post_type' => 'shop_coupon',
14858 'posts_per_page' => -1,
14859 'no_found_rows' => true,
14860 'meta_query' => [
14861 [
14862 'key' => '_is_aw_coupon',
14863 'compare' => 'NOT EXISTS',
14864 ],
14865 ],
14866 ]
14867 );
14868
14869 foreach ( $query->posts as $coupon ) {
14870 /**
14871 *
14872 * Ignore line
14873 *
14874 * @phpstan-ignore-next-line
14875 */
14876 $code = wc_format_coupon_code( $coupon->post_title );
14877 $coupon_codes[ $code ] = $code;
14878 }
14879
14880 foreach ( $coupon_codes as $code ) {
14881
14882 $coupon = new \WC_Coupon( $code );
14883
14884 if ( ! in_array( $coupon->get_discount_type(), [ 'recurring_fee', 'recurring_percent' ], true ) ) {
14885 unset( $coupon_codes[ $code ] );
14886 }
14887 }
14888
14889 if ( ! empty( $coupon_codes ) ) {
14890 if ( is_array( $coupon_codes ) ) {
14891 foreach ( $coupon_codes as $coupon_code ) {
14892 $options[] = [
14893 'label' => $coupon_code,
14894 'value' => $coupon_code,
14895 ];
14896 }
14897 }
14898 }
14899 return [
14900 'options' => $options,
14901 'hasMore' => false,
14902 ];
14903 }
14904
14905 /**
14906 * Get BuddyBoss field.
14907 *
14908 * @param array $data data.
14909 *
14910 * @return array|void
14911 */
14912 public function search_bb_field_list( $data ) {
14913 global $wpdb;
14914 $options = [];
14915
14916 $xprofile_fields = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_xprofile_fields WHERE parent_id = 0 ORDER BY field_order ASC" );
14917 if ( ! empty( $xprofile_fields ) ) {
14918 foreach ( $xprofile_fields as $xprofile_field ) {
14919 $options[] = [
14920 'label' => $xprofile_field->name,
14921 'value' => $xprofile_field->id,
14922 ];
14923 }
14924 }
14925
14926 return [
14927 'options' => $options,
14928 'hasMore' => false,
14929 ];
14930 }
14931
14932 /**
14933 * Get Woocommerce Bookings Product List.
14934 *
14935 * @param array $data data.
14936 *
14937 * @return array|void
14938 */
14939 public function search_wb_bookable_product_list( $data ) {
14940 $options = [];
14941
14942 if ( ! class_exists( 'WC_Bookings_Admin' ) ) {
14943 return;
14944 }
14945
14946 $products = WC_Bookings_Admin::get_booking_products();
14947 if ( ! empty( $products ) ) {
14948 if ( is_array( $products ) ) {
14949 foreach ( $products as $product ) {
14950 $options[] = [
14951 'label' => $product->get_name(),
14952 'value' => $product->get_id(),
14953 ];
14954 $resources = $product->get_resources();
14955 if ( ! empty( $resources ) ) {
14956 foreach ( $resources as $resource ) {
14957 $options[] = [
14958 'label' => $resource->get_name(),
14959 'value' => $resource->get_id(),
14960 ];
14961 }
14962 }
14963 }
14964 }
14965 }
14966
14967 return [
14968 'options' => $options,
14969 'hasMore' => false,
14970 ];
14971 }
14972
14973 /**
14974 * Get last data for WooCommerce Booking trigger
14975 *
14976 * @param array $data data.
14977 * @return array
14978 */
14979 public function search_woo_commerce_booking_last_data( $data ) {
14980 $context = [];
14981 $context['response_type'] = 'sample';
14982 $context['pluggable_data'] = [];
14983 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
14984 global $wpdb;
14985 if ( 'booking_created' === $term ) {
14986 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' AND post_status='confirmed' order by ID DESC LIMIT 1" );
14987
14988 if ( ! empty( $results ) ) {
14989 if ( class_exists( 'WC_Booking' ) ) {
14990 $booking = new WC_Booking( $results[0]->ID );
14991 $person_counts = $booking->get_person_counts();
14992 $bookable_product_id = $booking->get_product_id();
14993 if ( method_exists( $booking, 'get_data' ) ) {
14994 $booking = $booking->get_data();
14995 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
14996 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
14997 if ( ! empty( $person_counts ) ) {
14998 $total_count = 0;
14999 foreach ( $person_counts as $key => $value ) {
15000 $total_count += $value;
15001 }
15002 $booking['total_person_counts'] = $total_count;
15003 }
15004 $booking['bookable_product'] = $bookable_product_id;
15005 $context['response_type'] = 'live';
15006 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
15007 }
15008 }
15009 } else {
15010 $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 );
15011 $context['pluggable_data'] = $booking_sample_data;
15012 }
15013 } elseif ( 'booking_status_changed' === $term ) {
15014 $to_status = isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1;
15015
15016 if ( -1 == $to_status ) {
15017 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' order by ID DESC LIMIT 1" );
15018 } else {
15019 $results = $wpdb->get_results(
15020 $wpdb->prepare(
15021 "
15022 SELECT *
15023 FROM {$wpdb->prefix}posts
15024 WHERE `post_status` = %s
15025 AND `post_type` LIKE %s
15026 ",
15027 $to_status,
15028 'wc_booking'
15029 )
15030 );
15031 }
15032
15033 if ( ! empty( $results ) ) {
15034 if ( class_exists( 'WC_Booking' ) ) {
15035 $booking = new WC_Booking( $results[0]->ID );
15036 $person_counts = $booking->get_person_counts();
15037 $bookable_product_id = $booking->get_product_id();
15038 if ( method_exists( $booking, 'get_data' ) ) {
15039 $booking = $booking->get_data();
15040 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
15041 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
15042 if ( ! empty( $person_counts ) ) {
15043 $total_count = 0;
15044 foreach ( $person_counts as $key => $value ) {
15045 $total_count += $value;
15046 }
15047 $booking['total_person_counts'] = $total_count;
15048 }
15049 $booking['bookable_product'] = $bookable_product_id;
15050 $booking['from_status'] = $data['filter']['from_status']['value'];
15051 $booking['to_status'] = $booking['status'];
15052 $context['response_type'] = 'live';
15053 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
15054 }
15055 }
15056 } else {
15057 $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 );
15058 $context['pluggable_data'] = $booking_sample_data;
15059 }
15060 }
15061 return $context;
15062 }
15063
15064 /**
15065 * Get WooCommerce Booking status list.
15066 *
15067 * @param array $data data.
15068 *
15069 * @return array
15070 */
15071 public function search_woo_booking_status_list( $data ) {
15072
15073 $options = [
15074 [
15075 'label' => 'Pending Confirmation',
15076 'value' => 'pending-confirmation',
15077 ],
15078 [
15079 'label' => 'Unpaid',
15080 'value' => 'unpaid',
15081 ],
15082 [
15083 'label' => 'Confirmed',
15084 'value' => 'confirmed',
15085 ],
15086 [
15087 'label' => 'Paid',
15088 'value' => 'paid',
15089 ],
15090 [
15091 'label' => 'Complete',
15092 'value' => 'complete',
15093 ],
15094 [
15095 'label' => 'In Cart',
15096 'value' => 'in-cart',
15097 ],
15098 [
15099 'label' => 'Cancelled',
15100 'value' => 'cancelled',
15101 ],
15102 ];
15103
15104 return [
15105 'options' => $options,
15106 'hasMore' => false,
15107 ];
15108 }
15109
15110 /**
15111 * Get Woocommerce Memberships Status List.
15112 *
15113 * @param array $data data.
15114 *
15115 * @return array|void
15116 */
15117 public function search_wc_membership_status_list( $data ) {
15118 $options = [];
15119 if ( ! function_exists( 'wc_memberships_get_user_membership_statuses' ) ) {
15120 return;
15121 }
15122
15123 $statuses = wc_memberships_get_user_membership_statuses();
15124 if ( ! empty( $statuses ) ) {
15125 if ( is_array( $statuses ) ) {
15126 foreach ( $statuses as $status => $value ) {
15127 $status = 0 === strpos( $status, 'wcm-' ) ? substr( $status, 4 ) : $status;
15128 $options[] = [
15129 'label' => $value['label'],
15130 'value' => $status,
15131 ];
15132 }
15133 }
15134 }
15135
15136 return [
15137 'options' => $options,
15138 'hasMore' => false,
15139 ];
15140 }
15141
15142 /**
15143 * Search Woocommerce Subscription data.
15144 *
15145 * @param array $data data.
15146 * @return array|void
15147 */
15148 public function search_user_subscription_last_data( $data ) {
15149
15150 $context = [];
15151
15152 if ( ! function_exists( 'wcs_get_subscription' ) ) {
15153 return;
15154 }
15155 if ( ! function_exists( 'wcs_get_subscriptions_for_product' ) ) {
15156 return;
15157 }
15158 if ( ! function_exists( 'wcs_order_contains_renewal' ) ) {
15159 return;
15160 }
15161 if ( ! function_exists( 'wcs_get_subscriptions_for_order' ) ) {
15162 return;
15163 }
15164
15165 if ( 'trial_end' == $data['search_term'] ) {
15166 $subscription_id = $data['filter']['subscription']['value'];
15167 $query_args = [
15168 'post_type' => 'shop_subscription',
15169 'orderby' => 'ID',
15170 'order' => 'DESC',
15171 'post_status' => 'wc-active',
15172 'posts_per_page' => 1,
15173 'meta_query' => [
15174 [
15175 'key' => '_schedule_trial_end',
15176 'value' => gmdate( 'Y-m-d H:i:s' ),
15177 'compare' => '<=',
15178 ],
15179 ],
15180 'post__in' => [ $subscription_id ],
15181 ];
15182 $query_result = new WP_Query( $query_args );
15183 $subscription_orders = $query_result->get_posts();
15184
15185 if ( ! empty( $subscription_orders ) ) {
15186 /**
15187 *
15188 * Ignore line
15189 *
15190 * @phpstan-ignore-next-line
15191 */
15192 $subscription = wcs_get_subscription( $subscription_orders[0]->ID );
15193 $user_id = $subscription->get_user_id();
15194
15195 $items = $subscription->get_items();
15196 $product_ids = [];
15197 foreach ( $items as $item ) {
15198 $product_ids[] = $item->get_product_id();
15199 }
15200
15201 $subscription_status = $subscription->get_status();
15202 $subscription_start_date = $subscription->get_date_created();
15203
15204 $context['subscription_data'] = [
15205 'status' => $subscription_status,
15206 'start_date' => $subscription_start_date,
15207 'trial_end_date' => $subscription->get_date( 'schedule_trial_end' ),
15208 'next_payment_date' => $subscription->get_date( 'next_payment' ),
15209 'end_date' => $subscription->get_date( 'schedule_end' ),
15210 ];
15211 $context['user'] = WordPress::get_user_context( $user_id );
15212
15213 foreach ( $product_ids as $val ) {
15214 $context['subscription'] = $val;
15215 $context['subscription_name'] = get_the_title( $val );
15216 }
15217 $context['pluggable_data'] = $context;
15218 $context['response_type'] = 'live';
15219 } else {
15220 $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 );
15221 $context['pluggable_data'] = $context;
15222 $context['response_type'] = 'sample';
15223 }
15224 } elseif ( 'renewal_payment_failed' == $data['search_term'] ) {
15225 $subscription_item = $data['filter']['subscription_item']['value'];
15226 $subscription_ids = wcs_get_subscriptions_for_product( $subscription_item );
15227 $ids = [];
15228 $related = [];
15229 foreach ( $subscription_ids as $subscription ) {
15230 $ids[] = $subscription;
15231 /**
15232 *
15233 * Ignore line
15234 *
15235 * @phpstan-ignore-next-line
15236 */
15237 $subscriptions = new WC_Subscription( $subscription );
15238 /**
15239 *
15240 * Ignore line
15241 *
15242 * @phpstan-ignore-next-line
15243 */
15244 $related[] = $subscriptions->get_related_orders();
15245 }
15246
15247 $failed_payment_orders = [];
15248 foreach ( $related as $value ) {
15249 foreach ( $value as $val ) {
15250 if ( wcs_order_contains_renewal( $val ) ) {
15251 $order = wc_get_order( $val );
15252 if ( $order ) {
15253 /**
15254 *
15255 * Ignore line
15256 *
15257 * @phpstan-ignore-next-line
15258 */
15259 $payment_status = $order->get_status();
15260 if ( 'failed' === $payment_status || 'wc-on-hold' === $payment_status ) {
15261 $failed_payment_orders[] = $val;
15262 }
15263 }
15264 }
15265 }
15266 }
15267
15268 if ( ! empty( $failed_payment_orders ) ) {
15269 $failed = array_rand( $failed_payment_orders );
15270 $related_subscriptions = wcs_get_subscriptions_for_order( $failed_payment_orders[ $failed ] );
15271 if ( ! empty( $related_subscriptions ) ) {
15272 $sub_id = get_post_meta( $failed_payment_orders[ $failed ], '_subscription_renewal', true );
15273 $subscription = wcs_get_subscription( $sub_id );
15274
15275 $items = $subscription->get_items();
15276 $product_ids = [];
15277 foreach ( $items as $item ) {
15278 $product_ids[] = $item->get_product_id();
15279 }
15280
15281 $context['user'] = WordPress::get_user_context( $subscription->get_user_id() );
15282 foreach ( [ 'renewal' ] as $order_type ) {
15283 foreach ( $subscription->get_related_orders( 'ids', $order_type ) as $order_id ) {
15284 $context['subscription_related_order'][] = $order_id;
15285 }
15286 }
15287 foreach ( $product_ids as $val ) {
15288 $context['subscription_item'] = $val;
15289 $context['subscription_name'] = get_the_title( $val );
15290 }
15291 $context['data'] = $subscription->get_data();
15292 $context['pluggable_data'] = $context;
15293 $context['response_type'] = 'live';
15294 } else {
15295 $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 );
15296 $context['response_type'] = 'sample';
15297 }
15298 } else {
15299 $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 );
15300 $context['response_type'] = 'sample';
15301 }
15302 }
15303 return $context;
15304 }
15305
15306 /**
15307 * Get Masteriyo LMS Courses.
15308 *
15309 * @param array $data data.
15310 *
15311 * @return array
15312 */
15313 public function search_masteriyo_lms_courses( $data ) {
15314
15315 $page = $data['page'];
15316 $limit = Utilities::get_search_page_limit();
15317 $offset = $limit * ( $page - 1 );
15318
15319 $args = [
15320 'post_type' => 'mto-course',
15321 'posts_per_page' => $limit,
15322 'offset' => $offset,
15323 'orderby' => 'title',
15324 'order' => 'ASC',
15325 'post_status' => 'publish',
15326 ];
15327
15328 $courses = get_posts( $args );
15329
15330 $course_count = wp_count_posts( 'mto-course' )->publish;
15331
15332 $options = [];
15333 if ( ! empty( $courses ) ) {
15334 if ( is_array( $courses ) ) {
15335 foreach ( $courses as $course ) {
15336 $options[] = [
15337 'label' => $course->post_title,
15338 'value' => $course->ID,
15339 ];
15340 }
15341 }
15342 }
15343 return [
15344 'options' => $options,
15345 'hasMore' => $course_count > $limit && $course_count > $offset,
15346 ];
15347 }
15348
15349 /**
15350 * Get Masteriyo LMS Lessons.
15351 *
15352 * @param array $data data.
15353 *
15354 * @return array
15355 */
15356 public function search_masteriyo_lms_lessons( $data ) {
15357
15358 $course_id = $data['dynamic'];
15359 $page = $data['page'];
15360 $limit = Utilities::get_search_page_limit();
15361 $offset = $limit * ( $page - 1 );
15362 $options = [];
15363 $args = [
15364 'post_type' => 'mto-lesson',
15365 'posts_per_page' => $limit,
15366 'offset' => $offset,
15367 'orderby' => 'title',
15368 'order' => 'ASC',
15369 'post_status' => 'publish',
15370 'meta_query' => [
15371 [
15372 'key' => '_course_id',
15373 'value' => $course_id,
15374 'compare' => '=',
15375 ],
15376 ],
15377 ];
15378
15379 $lessons = get_posts( $args );
15380 $lesson_count = wp_count_posts( 'mto-lesson' )->publish;
15381 if ( ! empty( $lessons ) ) {
15382 if ( is_array( $lessons ) ) {
15383 foreach ( $lessons as $lesson ) {
15384 $options[] = [
15385 'label' => $lesson->post_title,
15386 'value' => $lesson->ID,
15387 ];
15388 }
15389 }
15390 }
15391 return [
15392 'options' => $options,
15393 'hasMore' => $lesson_count > $limit && $lesson_count > $offset,
15394 ];
15395 }
15396
15397 /**
15398 * Get Masteriyo LMS Quiz.
15399 *
15400 * @param array $data data.
15401 *
15402 * @return array
15403 */
15404 public function search_masteriyo_lms_quizs( $data ) {
15405
15406 $course_id = $data['dynamic'];
15407 $page = $data['page'];
15408 $limit = Utilities::get_search_page_limit();
15409 $offset = $limit * ( $page - 1 );
15410 $options = [];
15411 $args = [
15412 'post_type' => 'mto-quiz',
15413 'posts_per_page' => $limit,
15414 'offset' => $offset,
15415 'orderby' => 'title',
15416 'order' => 'ASC',
15417 'post_status' => 'publish',
15418 'meta_query' => [
15419 [
15420 'key' => '_course_id',
15421 'value' => $course_id,
15422 'compare' => '=',
15423 ],
15424 ],
15425 ];
15426
15427 $quizs = get_posts( $args );
15428 $quiz_count = wp_count_posts( 'mto-quiz' )->publish;
15429 if ( ! empty( $quizs ) ) {
15430 if ( is_array( $quizs ) ) {
15431 foreach ( $quizs as $quiz ) {
15432 $options[] = [
15433 'label' => $quiz->post_title,
15434 'value' => $quiz->ID,
15435 ];
15436 }
15437 }
15438 }
15439 return [
15440 'options' => $options,
15441 'hasMore' => $quiz_count > $limit && $quiz_count > $offset,
15442 ];
15443 }
15444
15445 /**
15446 * Search Masteriyo LMS data.
15447 *
15448 * @param array $data data.
15449 * @return array|void|mixed
15450 */
15451 public function search_masteriyo_lms_last_data( $data ) {
15452 global $wpdb;
15453 $post_type = $data['post_type'];
15454 $trigger = $data['search_term'];
15455 $context = [];
15456
15457 $post_id = -1;
15458 if ( 'course_completed' === $trigger ) {
15459 $post_id = $data['filter']['course_id']['value'];
15460 } elseif ( 'lesson_completed' === $trigger ) {
15461 $post_id = $data['filter']['lesson_id']['value'];
15462 } elseif ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
15463 $post_id = $data['filter']['quiz_id']['value'];
15464 }
15465
15466 if ( 'course_completed' === $trigger || 'lesson_completed' === $trigger ) {
15467 if ( -1 === $post_id ) {
15468 $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 ) );
15469 } else {
15470 $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 ) );
15471 }
15472 }
15473
15474 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
15475 if ( -1 === $post_id ) {
15476 $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 ) );
15477 } else {
15478 $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 ) );
15479 }
15480 if ( ! empty( $result ) ) {
15481 $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 ) );
15482
15483 if ( function_exists( 'masteriyo_get_quiz' ) ) {
15484 $quiz = masteriyo_get_quiz( $resultt[0]->quiz_id );
15485 if ( is_object( $quiz ) && method_exists( $quiz, 'get_pass_mark' ) ) {
15486 if ( 'quiz_completed' === $trigger && $resultt[0]->earned_marks < $quiz->get_pass_mark() ) {
15487 $result = [];
15488 } elseif ( 'quiz_failed' === $trigger && $resultt[0]->earned_marks > $quiz->get_pass_mark() ) {
15489 $result = [];
15490 }
15491 }
15492 }
15493 }
15494 }
15495
15496 if ( ! empty( $result ) ) {
15497 $result_item_id = $result[0]->item_id;
15498 $result_user_id = $result[0]->user_id;
15499 $quiz_attempt = '';
15500 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
15501 $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 ) );
15502 $quiz_attempt = $resultt[0]->id;
15503 }
15504 switch ( $trigger ) {
15505 case 'course_completed':
15506 if ( function_exists( 'masteriyo_get_course' ) ) {
15507 $course = masteriyo_get_course( $result_item_id );
15508 $context_data = array_merge(
15509 WordPress::get_user_context( $result_user_id ),
15510 $course->get_data()
15511 );
15512 $context_data['course_id'] = $result_item_id;
15513 }
15514 break;
15515 case 'lesson_completed':
15516 if ( function_exists( 'masteriyo_get_lesson' ) ) {
15517 $lesson = masteriyo_get_lesson( $result_item_id );
15518 $context_data = array_merge(
15519 WordPress::get_user_context( $result_user_id ),
15520 $lesson->get_data()
15521 );
15522 $context_data['lesson_id'] = $result_item_id;
15523 }
15524 break;
15525 case 'quiz_completed':
15526 case 'quiz_failed':
15527 if ( function_exists( 'masteriyo_get_quiz' ) ) {
15528 $quiz = masteriyo_get_quiz( $result_item_id );
15529 $context_data = WordPress::get_user_context( $result_user_id );
15530 if ( function_exists( 'masteriyo_get_quiz_attempt' ) ) {
15531 $attempt = masteriyo_get_quiz_attempt( $quiz_attempt );
15532 $context_data['quiz_id'] = $result_item_id;
15533 $context_data['course_id'] = $quiz->get_course_id();
15534 $context_data['quiz'] = $quiz->get_data();
15535 $context_data['attempt'] = $attempt->get_data();
15536 }
15537 }
15538 break;
15539 default:
15540 return;
15541 }
15542 if ( ! empty( $context_data ) ) {
15543 $context['pluggable_data'] = $context_data;
15544 $context['response_type'] = 'live';
15545 }
15546 } elseif ( empty( $result ) ) {
15547 switch ( $trigger ) {
15548 case 'course_completed':
15549 $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"} ';
15550 break;
15551 case 'lesson_completed':
15552 $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"} ';
15553 break;
15554 case 'quiz_completed':
15555 case 'quiz_failed':
15556 $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"}';
15557 break;
15558 default:
15559 return;
15560 }
15561 $context = json_decode( $sample_data, true );
15562 }
15563
15564 return $context;
15565 }
15566
15567 /**
15568 * Search learndash user added in group.
15569 *
15570 * @param array $data data.
15571 * @return array|void
15572 */
15573 public function search_pluggables_user_added_in_group( $data ) {
15574 $context = [];
15575
15576 if ( ! function_exists( 'learndash_get_groups_user_ids' ) ) {
15577 return;
15578 }
15579 $term = $data['search_term'];
15580 $group_id = $data['filter']['sfwd_group_id']['value'];
15581
15582 if ( 'user_added_group' == $term ) {
15583 if ( -1 === $group_id ) {
15584 $args = [
15585 'numberposts' => 1,
15586 'orderby' => 'rand',
15587 'post_type' => 'groups',
15588 ];
15589 $posts = get_posts( $args );
15590 $random_value = $posts[0]->ID;
15591 $group_users = learndash_get_groups_user_ids( $random_value );
15592 $group_id = $random_value;
15593 } else {
15594 $group_users = learndash_get_groups_user_ids( $group_id );
15595 }
15596 $users = get_users(
15597 [
15598 'meta_key' => 'learndash_group_' . $group_id . '_enrolled_at',
15599 'orderby' => 'meta_value',
15600 'order' => 'DESC',
15601 'number' => 1,
15602 'include' => $group_users,
15603 ]
15604 );
15605 } elseif ( 'user_removed_group' == $term ) {
15606 if ( -1 === $group_id ) {
15607 $args = [
15608 'numberposts' => 1,
15609 'orderby' => 'rand',
15610 'post_type' => 'groups',
15611 ];
15612 $posts = get_posts( $args );
15613 $random_value = $posts[0]->ID;
15614 $group_id = $random_value;
15615 }
15616 $args = [
15617 'meta_query' => [
15618 'relation' => 'AND',
15619 [
15620 'key' => 'group_' . $group_id . '_access_from',
15621 'compare' => 'NOT EXISTS',
15622 ],
15623 [
15624 'key' => 'learndash_group_' . $group_id . '_enrolled_at',
15625 'compare' => 'EXISTS',
15626 ],
15627 ],
15628 'orderby' => 'meta_value',
15629 'order' => 'DESC',
15630 'number' => 1,
15631 ];
15632 $users = get_users( $args );
15633 } elseif ( 'leader_added_group' == $term || 'leader_removed_group' == $term ) {
15634 if ( -1 === $group_id ) {
15635 $args = [
15636 'numberposts' => 1,
15637 'fields' => 'ids',
15638 'orderby' => 'rand',
15639 'post_type' => 'groups',
15640 ];
15641 $posts = get_posts( $args );
15642 $random_key = array_rand( $posts );
15643 $random_value = $posts[ $random_key ];
15644 $group_id = $random_value;
15645 }
15646 $user_query_args = [
15647 'orderby' => 'learndash_group_leaders_' . intval( $group_id ),
15648 'order' => 'DESC',
15649 'meta_query' => [
15650 [
15651 'key' => 'learndash_group_leaders_' . intval( $group_id ),
15652 'value' => intval( $group_id ),
15653 'compare' => '=',
15654 'type' => 'NUMERIC',
15655 ],
15656 ],
15657 ];
15658 $users = get_users( $user_query_args );
15659 }
15660
15661 if ( ! empty( $users ) ) {
15662 $context = WordPress::get_user_context( $users[0]->ID );
15663 $context['sfwd_group_id'] = $group_id;
15664 $context['group_title'] = get_the_title( $group_id );
15665 $context['group_url'] = get_permalink( $group_id );
15666 $context['group_featured_image_id'] = get_post_meta( $group_id, '_thumbnail_id', true );
15667 $context['group_featured_image_url'] = get_the_post_thumbnail_url( $group_id );
15668 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
15669 $group_courses_id = learndash_group_enrolled_courses( $group_id );
15670 if ( ! empty( $group_courses_id ) ) {
15671 foreach ( $group_courses_id as $key => $course_id ) {
15672 $context['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
15673 }
15674 }
15675 }
15676 $context['response_type'] = 'live';
15677 } else {
15678 $context = WordPress::get_sample_user_context();
15679 $context['group_title'] = 'Test Group';
15680 $context['sfwd_group_id'] = 112;
15681 $context['group_url'] = 'https://example.com/test-group';
15682 $context['group_featured_image_id'] = 113;
15683 $context['group_featured_image_url'] = 'https://example.com/test-group-img';
15684 $context['group_courses'] = [
15685 [
15686 'ID' => 7915,
15687 'title' => 'Example Course',
15688 'URL' => 'https://example.com/courses/example-course/',
15689 'status' => 'publish',
15690 'featured_image_id' => '',
15691 'featured_image_url' => false,
15692 ],
15693 ];
15694 $context['response_type'] = 'sample';
15695 }
15696
15697 $context['pluggable_data'] = $context;
15698 return $context;
15699 }
15700
15701 /**
15702 * Search learndash user enrolled in course.
15703 *
15704 * @param array $data data.
15705 * @return array|void
15706 */
15707 public function search_pluggables_user_enrolled_course( $data ) {
15708 global $wpdb;
15709 $context = [];
15710
15711 if ( ! function_exists( 'ld_course_access_expires_on' ) ||
15712 ! function_exists( 'learndash_group_enrolled_courses' ) ||
15713 ! function_exists( 'learndash_get_lesson_list' ) ) {
15714 return;
15715 }
15716 $term = $data['search_term'];
15717 $course_id = isset( $data['filter']['sfwd_course_id'] ) ? $data['filter']['sfwd_course_id']['value'] : '';
15718 $group_id = isset( $data['filter']['sfwd_group_id'] ) ? $data['filter']['sfwd_group_id']['value'] : '';
15719
15720 if ( 'course_enrolled' == $term ) {
15721 if ( -1 === $course_id ) {
15722 $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 ) );
15723 } else {
15724 $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 ) );
15725 }
15726 } elseif ( 'course_unenrolled' == $term ) {
15727 if ( -1 === $course_id ) {
15728 $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 ) );
15729 $course_id = $courses[0]->course_id;
15730 }
15731 $args = [
15732 'meta_query' => [
15733 'relation' => 'AND',
15734 [
15735 'key' => 'course_' . $course_id . '_access_from',
15736 'compare' => 'NOT EXISTS',
15737 ],
15738 [
15739 'key' => 'learndash_course_' . $course_id . '_enrolled_at',
15740 'compare' => 'EXISTS',
15741 ],
15742 ],
15743 ];
15744 $users = get_users( $args );
15745 } elseif ( 'course_access_expired' == $term ) {
15746 if ( -1 === $course_id ) {
15747 $course_args = [
15748 'post_type' => 'sfwd-courses',
15749 'posts_per_page' => 1,
15750 'orderby' => 'rand',
15751 'order' => 'ASC',
15752 'post_status' => 'publish',
15753 ];
15754 $courses_posts = get_posts( $course_args );
15755 $course_id = $courses_posts[0]->ID;
15756 }
15757 $args = [
15758 'meta_query' => [
15759 [
15760 'key' => 'learndash_course_expired_' . $course_id,
15761 'compare' => 'EXISTS',
15762 ],
15763 ],
15764 ];
15765 $users = get_users( $args );
15766 } elseif ( 'group_course_completed' == $term ) {
15767 if ( -1 === $group_id ) {
15768 $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 ) );
15769 } else {
15770 $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 ) );
15771 }
15772 $activity_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity_meta WHERE activity_id = %d", $courses[0]->activity_id ) );
15773 } elseif ( 'course_group_added' == $term ) {
15774 if ( -1 === $group_id ) {
15775 $args = [
15776 'numberposts' => 1,
15777 'fields' => 'ids',
15778 'orderby' => 'rand',
15779 'post_type' => 'groups',
15780 ];
15781 $posts = get_posts( $args );
15782 $random_key = array_rand( $posts );
15783 $random_value = $posts[ $random_key ];
15784 $group_id = $random_value;
15785 }
15786 $courses = learndash_group_enrolled_courses( $group_id );
15787 } elseif ( 'assignment_submitted' == $term ) {
15788 $course_id = isset( $data['filter']['sfwd-courses'] ) ? $data['filter']['sfwd-courses']['value'] : '';
15789 $lesson_id = $data['filter']['sfwd_lesson_topic_id']['value'];
15790 if ( -1 === $course_id && -1 === $lesson_id ) {
15791 $args = [
15792 'post_type' => 'sfwd-assignment',
15793 'posts_per_page' => 1,
15794 'order' => 'DESC',
15795 'post_status' => 'publish',
15796 ];
15797 } else {
15798 if ( -1 === $course_id ) {
15799 $courses = get_posts(
15800 [
15801 'posts_per_page' => - 1,
15802 'post_type' => 'sfwd-courses',
15803 'post_status' => 'publish',
15804 'fields' => 'ids',
15805 ]
15806 );
15807 $course_key = array_rand( $courses );
15808 $course_id = $courses[ $course_key ];
15809 }
15810 if ( -1 === $lesson_id ) {
15811 $args = [
15812 'post_type' => 'sfwd-assignment',
15813 'posts_per_page' => 1,
15814 'order' => 'DESC',
15815 'post_status' => 'publish',
15816 'meta_query' => [
15817 [
15818 'key' => 'course_id',
15819 'value' => $course_id,
15820 'compare' => '=',
15821 ],
15822 ],
15823 ];
15824 } else {
15825 $args = [
15826 'post_type' => 'sfwd-assignment',
15827 'posts_per_page' => 1,
15828 'order' => 'DESC',
15829 'post_status' => 'publish',
15830 'meta_query' => [
15831 'relation' => 'AND',
15832 [
15833 'key' => 'lesson_id',
15834 'value' => $lesson_id,
15835 'compare' => '=',
15836 ],
15837 [
15838 'key' => 'course_id',
15839 'value' => $course_id,
15840 'compare' => '=',
15841 ],
15842 ],
15843 ];
15844 }
15845 }
15846 $assignments = get_posts( $args );
15847 } elseif ( 'assignment_graded' == $term ) {
15848 $course_id = isset( $data['filter']['sfwd-courses'] ) ? $data['filter']['sfwd-courses']['value'] : '';
15849 $lesson_id = $data['filter']['sfwd_lesson_topic_id']['value'];
15850 if ( -1 === $course_id && -1 === $lesson_id ) {
15851 $args = [
15852 'post_type' => 'sfwd-assignment',
15853 'posts_per_page' => 1,
15854 'order' => 'DESC',
15855 'post_status' => 'publish',
15856 'meta_query' => [
15857 [
15858 'key' => 'approval_status',
15859 'compare' => 'EXISTS',
15860 ],
15861 ],
15862 ];
15863 } else {
15864 if ( -1 === $course_id ) {
15865 $courses = get_posts(
15866 [
15867 'posts_per_page' => - 1,
15868 'post_type' => 'sfwd-courses',
15869 'post_status' => 'publish',
15870 'fields' => 'ids',
15871 ]
15872 );
15873 $course_key = array_rand( $courses );
15874 $course_id = $courses[ $course_key ];
15875 }
15876 if ( -1 === $lesson_id ) {
15877 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
15878 if ( ! empty( $lessons ) ) {
15879 $random_key = array_rand( $lessons );
15880 $random_value = $lessons[ $random_key ];
15881 $lesson_id = $random_value->ID;
15882 }
15883 }
15884 $args = [
15885 'post_type' => 'sfwd-assignment',
15886 'posts_per_page' => 1,
15887 'order' => 'DESC',
15888 'post_status' => 'publish',
15889 'meta_query' => [
15890 'relation' => 'AND',
15891 [
15892 'key' => 'lesson_id',
15893 'value' => $lesson_id,
15894 'compare' => '=',
15895 ],
15896 [
15897 'key' => 'course_id',
15898 'value' => $course_id,
15899 'compare' => '=',
15900 ],
15901 [
15902 'key' => 'approval_status',
15903 'compare' => 'EXISTS',
15904 ],
15905 ],
15906 ];
15907 }
15908 $assignments = get_posts( $args );
15909 }
15910
15911 if ( 'assignment_submitted' == $term || 'assignment_graded' == $term ) {
15912 if ( ! empty( $assignments ) ) {
15913 $context_data = WordPress::get_user_context( (int) $assignments[0]->post_author );
15914 $context_data['assignment_id'] = $assignments[0]->ID;
15915 $context_data['assignment_title'] = $assignments[0]->post_title;
15916 $context_data['assignment_url'] = get_post_meta( $assignments[0]->ID, 'file_link', true );
15917 $context_data['lesson_id'] = get_post_meta( $assignments[0]->ID, 'lesson_id', true );
15918 $context_data['course_id'] = get_post_meta( $assignments[0]->ID, 'course_id', true );
15919 $context_data['points'] = get_post_meta( $assignments[0]->ID, 'points', true );
15920 $context['response_type'] = 'live';
15921 } else {
15922 $context_data = WordPress::get_sample_user_context();
15923 $context_data['assignment_title'] = 'Test Assignment';
15924 $context_data['assignment_id'] = 112;
15925 $context_data['assignment_url'] = 'https://example.com/test_assignment.pdf';
15926 $context_data['lesson_id'] = 2;
15927 $context_data['course_id'] = 1;
15928 $context_data['points'] = '12';
15929 $context['response_type'] = 'sample';
15930 }
15931 } elseif ( 'course_unenrolled' == $term || 'course_access_expired' == $term ) {
15932 if ( ! empty( $users ) ) {
15933 $context_data = WordPress::get_user_context( $users[0]->ID );
15934 $context_data['sfwd_course_id'] = $course_id;
15935 $context_data['course_title'] = get_the_title( $course_id );
15936 $context_data['course_url'] = get_permalink( $course_id );
15937 $context_data['course_featured_image_id'] = get_post_meta( $course_id, '_thumbnail_id', true );
15938 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course_id );
15939 $timestamp = ld_course_access_expires_on( $course_id, $users[0]->ID );
15940 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
15941 $date_format = get_option( 'date_format' );
15942 if ( is_string( $date_format ) ) {
15943 $context_data['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
15944 }
15945 $context['response_type'] = 'live';
15946 } else {
15947 $context_data = WordPress::get_sample_user_context();
15948 $context_data['course_name'] = 'Test Course';
15949 $context_data['sfwd_course_id'] = 112;
15950 $context_data['course_url'] = 'https://example.com/test-course';
15951 $context_data['course_featured_image_id'] = 113;
15952 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
15953 $context_data['course_access_expiry_date'] = '2023-10-20';
15954 $context['response_type'] = 'sample';
15955 }
15956 } elseif ( 'course_group_added' == $term ) {
15957 if ( ! empty( $courses ) ) {
15958 $context_data['course_id'] = $courses[0];
15959 $context_data['course_title'] = get_the_title( $courses[0] );
15960 $context_data['course_url'] = get_permalink( $courses[0] );
15961 $context_data['course_featured_image_id'] = get_post_meta( $courses[0], '_thumbnail_id', true );
15962 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $courses[0] );
15963 $context_data['sfwd_group_id'] = $group_id;
15964 $context_data['group_name'] = get_the_title( $group_id );
15965 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
15966 $group_courses_id = learndash_group_enrolled_courses( $group_id );
15967 if ( ! empty( $group_courses_id ) ) {
15968 foreach ( $group_courses_id as $key => $course_id ) {
15969 $context_data['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
15970 }
15971 }
15972 }
15973 $context['response_type'] = 'live';
15974 } else {
15975 $context_data = WordPress::get_sample_user_context();
15976 $context_data['course_name'] = 'Test Course';
15977 $context_data['sfwd_course_id'] = 112;
15978 $context_data['course_url'] = 'https://example.com/test-course';
15979 $context_data['course_featured_image_id'] = 113;
15980 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
15981 $context_data['course_access_expiry_date'] = '2023-10-20';
15982 $context_data['sfwd_group_id'] = 12;
15983 $context_data['group_name'] = 'Test Group';
15984 $context_data['group_courses'] = [
15985 [
15986 'ID' => 7915,
15987 'title' => 'Example Course',
15988 'URL' => 'https://example.com/courses/example-course/',
15989 'status' => 'publish',
15990 'featured_image_id' => '',
15991 'featured_image_url' => false,
15992 ],
15993 ];
15994 $context['response_type'] = 'sample';
15995 }
15996 } elseif ( 'group_course_completed' == $term ) {
15997 if ( ! empty( $courses ) && ! empty( $activity_meta ) ) {
15998 $context_data = WordPress::get_user_context( $courses[0]->user_id );
15999 $context_data['sfwd_group_id'] = $courses[0]->post_id;
16000 $context_data['group_title'] = get_the_title( $courses[0]->post_id );
16001 $context_data['group_url'] = get_permalink( $courses[0]->post_id );
16002 $context_data['group_featured_image_id'] = get_post_meta( $courses[0]->post_id, '_thumbnail_id', true );
16003 $context_data['group_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->post_id );
16004 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
16005 $group_courses_id = learndash_group_enrolled_courses( $courses[0]->post_id );
16006 if ( ! empty( $group_courses_id ) ) {
16007 foreach ( $group_courses_id as $key => $course_id ) {
16008 $context_data['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
16009 }
16010 }
16011 }
16012 $course_ids = null;
16013 foreach ( $activity_meta as $item ) {
16014 if ( 'course_ids' === $item->activity_meta_key ) {
16015 $course_ids = unserialize( $item->activity_meta_value );
16016 break;
16017 }
16018 }
16019 if ( ! empty( $course_ids ) && is_array( $course_ids ) ) {
16020 foreach ( $course_ids as $key => $course_id ) {
16021 if ( is_int( $course_id ) ) {
16022 $args = [
16023 'include' => [ $courses[0]->user_id ],
16024 'meta_query' => [
16025 [
16026 'key' => 'course_completed_' . $course_id,
16027 'compare' => 'EXISTS',
16028 ],
16029 ],
16030 ];
16031 $users = get_users( $args );
16032 if ( ! empty( $users ) ) {
16033 $context_data[ 'completed ' . $key ]['course_id'] = $course_id;
16034 $context_data[ 'completed ' . $key ]['course_title'] = get_the_title( $course_id );
16035 $context_data[ 'completed ' . $key ]['course_url'] = get_permalink( $course_id );
16036 $context_data[ 'completed ' . $key ]['course_featured_image_id'] = get_post_meta( $course_id, '_thumbnail_id', true );
16037 $context_data[ 'completed ' . $key ]['course_featured_image_url'] = get_the_post_thumbnail_url( $course_id );
16038 $timestamp = ld_course_access_expires_on( $course_id, $courses[0]->user_id );
16039 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
16040 $date_format = get_option( 'date_format' );
16041 if ( is_string( $date_format ) ) {
16042 $context_data[ 'completed ' . $key ]['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
16043 }
16044 }
16045 }
16046 }
16047 }
16048 $context['response_type'] = 'live';
16049 } else {
16050 $context_data = WordPress::get_sample_user_context();
16051 $context_data['sfwd_group_id'] = 112;
16052 $context_data['group_title'] = 'Test Group';
16053 $context_data['group_url'] = 113;
16054 $context_data['group_featured_image_id'] = 11;
16055 $context_data['group_featured_image_url'] = 'https://example.com/test-group-img';
16056 $context_data['completed 0'] = [
16057 'course_id' => 10,
16058 'course_title' => 'Test Course',
16059 'course_url' => 'https://example.com/test-course',
16060 'course_featured_image_id' => 14,
16061 'course_featured_image_url' => 'https://example.com/test-course-img',
16062 ];
16063 $context_data['group_courses'] = [
16064 [
16065 'ID' => 7915,
16066 'title' => 'Example Course',
16067 'URL' => 'https://example.com/courses/example-course/',
16068 'status' => 'publish',
16069 'featured_image_id' => '',
16070 'featured_image_url' => false,
16071 ],
16072 ];
16073 $context['response_type'] = 'sample';
16074 }
16075 } else {
16076 if ( ! empty( $courses ) ) {
16077 $context_data = WordPress::get_user_context( $courses[0]->user_id );
16078 $context_data['course_id'] = $courses[0]->course_id;
16079 $context_data['course_title'] = get_the_title( $courses[0]->course_id );
16080 $context_data['course_url'] = get_permalink( $courses[0]->course_id );
16081 $context_data['course_featured_image_id'] = get_post_meta( $courses[0]->course_id, '_thumbnail_id', true );
16082 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->course_id );
16083 $timestamp = ld_course_access_expires_on( $courses[0]->course_id, $courses[0]->user_id );
16084 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
16085 $date_format = get_option( 'date_format' );
16086 if ( is_string( $date_format ) ) {
16087 $context_data['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
16088 }
16089 if ( $courses[0]->post_id ) {
16090 $context_data['sfwd_group_id'] = $courses[0]->post_id;
16091 $context_data['group_title'] = get_the_title( $courses[0]->post_id );
16092 $context_data['group_url'] = get_permalink( $courses[0]->post_id );
16093 $context_data['group_featured_image_id'] = get_post_meta( $courses[0]->post_id, '_thumbnail_id', true );
16094 $context_data['group_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->post_id );
16095 }
16096 $context['response_type'] = 'live';
16097 } else {
16098 $context_data = WordPress::get_sample_user_context();
16099 $context_data['course_name'] = 'Test Course';
16100 $context_data['sfwd_course_id'] = 112;
16101 $context_data['course_url'] = 'https://example.com/test-course';
16102 $context_data['course_featured_image_id'] = 113;
16103 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
16104 $context_data['course_access_expiry_date'] = '2023-10-20';
16105 $context['response_type'] = 'sample';
16106 }
16107 }
16108
16109 $context['pluggable_data'] = $context_data;
16110 return $context;
16111 }
16112
16113 /**
16114 * Search LearnDash Lesson Topic List.
16115 *
16116 * @param array $data Search Params.
16117 *
16118 * @since 1.0.0
16119 *
16120 * @return array
16121 */
16122 public function search_ld_lessons_topics_list( $data ) {
16123 $options = [];
16124 $course_id = $data['dynamic']['sfwd-courses'];
16125
16126 if ( ! function_exists( 'learndash_get_lesson_list' ) || ! function_exists( 'learndash_get_topic_list' ) ) {
16127 return [];
16128 }
16129
16130 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
16131 foreach ( $lessons as $lesson ) {
16132 $options[] = [
16133 'label' => $lesson->post_title,
16134 'value' => $lesson->ID,
16135 ];
16136 $topics = learndash_get_topic_list( $lesson->ID, $course_id );
16137 foreach ( $topics as $topic ) {
16138 $options[] = [
16139 'label' => $topic->post_title,
16140 'value' => $topic->ID,
16141 ];
16142 }
16143 }
16144
16145 return [
16146 'options' => $options,
16147 'hasMore' => false,
16148 ];
16149 }
16150
16151 /**
16152 * Search LearnDash Quiz List.
16153 *
16154 * @param array $data Search Params.
16155 *
16156 * @since 1.0.0
16157 *
16158 * @return array
16159 */
16160 public function search_ld_quiz_list( $data ) {
16161 $options = [];
16162 $course_id = $data['dynamic']['sfwd-courses'];
16163 $lesson_id = $data['dynamic']['sfwd_lessons_topics'];
16164
16165 if ( ! function_exists( 'learndash_get_course_quiz_list' ) || ! function_exists( 'learndash_get_lesson_quiz_list' ) ) {
16166 return [];
16167 }
16168
16169 $quizzes = learndash_get_course_quiz_list( $course_id );
16170 $quizzes = array_merge( $quizzes, learndash_get_lesson_quiz_list( $lesson_id, null, $course_id ) );
16171 if ( ! empty( $quizzes ) ) {
16172 foreach ( $quizzes as $quiz ) {
16173 $options[] = [
16174 'label' => $quiz['post']->post_title,
16175 'value' => $quiz['post']->ID,
16176
16177 ];
16178 }
16179 }
16180
16181 return [
16182 'options' => $options,
16183 'hasMore' => false,
16184 ];
16185 }
16186
16187 /**
16188 * Search LearnDash Quiz Essay Question List.
16189 *
16190 * @param array $data Search Params.
16191 *
16192 * @since 1.0.0
16193 *
16194 * @return array
16195 */
16196 public function search_ld_quiz_essay_question_list( $data ) {
16197 $options = [];
16198 $quiz_id = $data['dynamic'];
16199
16200 if ( ! function_exists( 'learndash_get_quiz_questions' ) ) {
16201 return [];
16202 }
16203
16204 if ( 0 < $quiz_id ) {
16205 $quiz_question_ids = learndash_get_quiz_questions( $quiz_id );
16206 if ( ! empty( $quiz_question_ids ) ) {
16207 foreach ( $quiz_question_ids as $question_post_id => $question_pro_id ) {
16208 $question_type = get_post_meta( $question_post_id, 'question_type', true );
16209 if ( is_string( $question_type ) && 'essay' === $question_type ) {
16210 $title = html_entity_decode( get_the_title( $question_post_id ), ENT_QUOTES, 'UTF-8' );
16211 $options[] = [
16212 'label' => $title,
16213 'value' => $question_post_id,
16214 ];
16215 }
16216 }
16217 }
16218 }
16219
16220 return [
16221 'options' => $options,
16222 'hasMore' => false,
16223 ];
16224 }
16225
16226 /**
16227 * Search LearnDash Lessons List.
16228 *
16229 * @param array $data Search Params.
16230 *
16231 * @since 1.0.0
16232 *
16233 * @return array
16234 */
16235 public function search_ld_lessons_list( $data ) {
16236 $options = [];
16237 $course_id = $data['dynamic']['sfwd-courses'];
16238
16239 if ( ! function_exists( 'learndash_get_lesson_list' ) ) {
16240 return [];
16241 }
16242
16243 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
16244 foreach ( $lessons as $lesson ) {
16245 $options[] = [
16246 'label' => $lesson->post_title,
16247 'value' => $lesson->ID,
16248 ];
16249 }
16250 return [
16251 'options' => $options,
16252 'hasMore' => false,
16253 ];
16254 }
16255
16256 /**
16257 * Search LearnDash Topics List.
16258 *
16259 * @param array $data Search Params.
16260 *
16261 * @since 1.0.0
16262 *
16263 * @return array
16264 */
16265 public function search_ld_topics_list( $data ) {
16266 $options = [];
16267 $course_id = $data['dynamic']['sfwd-courses'];
16268 $lesson_id = $data['dynamic']['sfwd-lessons'];
16269
16270 if ( ! function_exists( 'learndash_get_topic_list' ) ) {
16271 return [];
16272 }
16273
16274 $topics = learndash_get_topic_list( $lesson_id, $course_id );
16275 foreach ( $topics as $topic ) {
16276 $options[] = [
16277 'label' => $topic->post_title,
16278 'value' => $topic->ID,
16279 ];
16280 }
16281 return [
16282 'options' => $options,
16283 'hasMore' => false,
16284 ];
16285 }
16286
16287 /**
16288 * Search LearnDash Assignments List.
16289 *
16290 * @param array $data Search Params.
16291 *
16292 * @since 1.0.0
16293 *
16294 * @return array
16295 */
16296 public function search_ld_assignments_list( $data ) {
16297 $options = [];
16298 $course_id = $data['dynamic']['sfwd-courses'];
16299 $lesson_id = $data['dynamic']['sfwd_lesson_topic_id'];
16300
16301 $args = [
16302 'post_type' => 'sfwd-assignment',
16303 'numberposts' => - 1,
16304 ];
16305 $meta_query = [];
16306 if ( ! empty( $course_id ) ) {
16307 $meta_query[] = [
16308 'key' => 'course_id',
16309 'value' => (int) $course_id,
16310 'compare' => '=',
16311 ];
16312 }
16313 if ( ! empty( $lesson_id ) ) {
16314 $meta_query[] = [
16315 'key' => 'lesson_id',
16316 'value' => (int) $lesson_id,
16317 'compare' => '=',
16318 ];
16319 }
16320 if ( ! empty( $meta_query ) ) {
16321 $args['meta_query'] = $meta_query;
16322 if ( count( $meta_query ) > 1 ) {
16323 $args['meta_query']['relation'] = 'AND';
16324 }
16325 }
16326 $assignments = get_posts( $args );
16327 foreach ( $assignments as $assignment ) {
16328 $options[] = [
16329 'label' => $assignment->post_title,
16330 'value' => $assignment->ID,
16331 ];
16332 }
16333 return [
16334 'options' => $options,
16335 'hasMore' => false,
16336 ];
16337 }
16338
16339 /**
16340 * Search post by post type.
16341 *
16342 * @param array $data Search Params.
16343 *
16344 * @since 1.0.0
16345 *
16346 * @return array
16347 */
16348 public function search_edd_prices( $data ) {
16349 $options = [];
16350 $downlaod_id = $data['dynamic']['download_id'];
16351
16352 $variable_prices = get_post_meta( $downlaod_id, 'edd_variable_prices', true );
16353 if ( ! empty( $variable_prices ) && is_array( $variable_prices ) ) {
16354 foreach ( $variable_prices as $price_id => $price ) {
16355 if ( isset( $price['name'] ) ) {
16356 $options[] = [
16357 'label' => $price['name'] . '(' . $price['amount'] . ')',
16358 'value' => $price['index'],
16359 ];
16360 }
16361 }
16362 }
16363
16364 return [
16365 'options' => $options,
16366 'hasMore' => false,
16367 ];
16368
16369 }
16370
16371 /**
16372 * GetPowerful Docs list.
16373 *
16374 * @param array $data data.
16375 *
16376 * @return array
16377 */
16378 public function search_pfd_docs_list( $data ) {
16379
16380 $course_id = $data['dynamic'];
16381 $page = $data['page'];
16382 $limit = Utilities::get_search_page_limit();
16383 $offset = $limit * ( $page - 1 );
16384 $options = [];
16385 $args = [
16386 'post_type' => 'docs',
16387 'posts_per_page' => $limit,
16388 'offset' => $offset,
16389 'orderby' => 'title',
16390 'order' => 'ASC',
16391 'post_status' => 'publish',
16392 ];
16393
16394 $docs = get_posts( $args );
16395 $docs_count = wp_count_posts( 'docs' )->publish;
16396 if ( ! empty( $docs ) ) {
16397 if ( is_array( $docs ) ) {
16398 foreach ( $docs as $doc ) {
16399 $options[] = [
16400 'label' => $doc->post_title,
16401 'value' => $doc->ID,
16402 ];
16403 }
16404 }
16405 }
16406 return [
16407 'options' => $options,
16408 'hasMore' => $docs_count > $limit && $docs_count > $offset,
16409 ];
16410 }
16411
16412 /**
16413 * Search Powerful Docs last data.
16414 *
16415 * @param array $data data.
16416 * @return array|void|mixed
16417 */
16418 public function search_pfd_feedback_last_data( $data ) {
16419 $sample_data = '{"pluggable_data":{"feedback": "no","questions": "- I need help with something else...","comment": "help me out!!","doc_id": "2409","time": "2024-09-11 11:56:48","doc_name": "Sample doc","doc_link": "https://example.com","doc_author_email": "john@example.com"},"response_type":"sample"}';
16420 $context = json_decode( $sample_data, true );
16421
16422 return $context;
16423 }
16424
16425 /**
16426 * WooCommerce Coupon Discount type list.
16427 *
16428 * @param array $data data.
16429 *
16430 * @return array
16431 */
16432 public function search_woo_coupon_discount_type_list( $data ) {
16433 $options = [];
16434
16435 $discount_types = wc_get_coupon_types();
16436
16437 if ( ! empty( $discount_types ) ) {
16438 if ( is_array( $discount_types ) ) {
16439 foreach ( $discount_types as $key => $value ) {
16440 $options[] = [
16441 'label' => $value,
16442 'value' => $key,
16443 ];
16444 }
16445 }
16446 }
16447
16448 return [
16449 'options' => $options,
16450 'hasMore' => false,
16451 ];
16452 }
16453
16454 /**
16455 * WooCommerce Product list along with variation list.
16456 *
16457 * @param array $data data.
16458 *
16459 * @return array|void
16460 */
16461 public function search_woo_product_variation_list( $data ) {
16462 $options = [];
16463
16464 $page = $data['page'];
16465 $limit = Utilities::get_search_page_limit();
16466 $offset = $limit * ( $page - 1 );
16467
16468 if ( ! function_exists( 'wc_get_products' ) ) {
16469 return;
16470 }
16471 $products = wc_get_products(
16472 [
16473 'posts_per_page' => $limit,
16474 'offset' => $offset,
16475 'orderby' => 'date',
16476 'order' => 'DESC',
16477 ]
16478 );
16479
16480
16481 $prod_count = wp_count_posts( 'product' )->publish;
16482
16483 if ( ! empty( $products ) ) {
16484 if ( is_array( $products ) ) {
16485 foreach ( $products as $product ) {
16486 $options[] = [
16487 'label' => $product->get_name(),
16488 'value' => $product->get_id(),
16489 ];
16490 if ( $product->is_type( 'variable' ) ) {
16491 $variations = Utilities::get_product_variations( $product->get_id() );
16492 foreach ( $variations['result'] as $variation ) {
16493 $options[] = [
16494 'label' => $variation->post_title,
16495 'value' => $variation->ID,
16496 ];
16497 }
16498 }
16499 }
16500 }
16501 }
16502 return [
16503 'options' => $options,
16504 'hasMore' => $prod_count > $limit && $prod_count > $offset,
16505 ];
16506 }
16507
16508 /**
16509 * WooCommerce Product list along with variation list.
16510 *
16511 * @param array $data data.
16512 *
16513 * @return array
16514 */
16515 public function search_woo_coupon_list( $data ) {
16516 $options = [];
16517
16518 $page = $data['page'];
16519 $limit = Utilities::get_search_page_limit();
16520 $offset = $limit * ( $page - 1 );
16521
16522 $coupons = get_posts(
16523 [
16524 'posts_per_page' => - 1,
16525 'orderby' => 'name',
16526 'order' => 'asc',
16527 'post_type' => 'shop_coupon',
16528 'post_status' => 'publish',
16529 ]
16530 );
16531 $coupon_count = wp_count_posts( 'shop_coupon' )->publish;
16532
16533 if ( ! empty( $coupons ) ) {
16534 if ( is_array( $coupons ) ) {
16535 foreach ( $coupons as $coupon ) {
16536 $code = wc_format_coupon_code( $coupon->post_title );
16537 $options[] = [
16538 'label' => $code,
16539 'value' => $code,
16540 ];
16541 }
16542 }
16543 }
16544
16545 return [
16546 'options' => $options,
16547 'hasMore' => $coupon_count > $limit && $coupon_count > $offset,
16548 ];
16549 }
16550
16551 /**
16552 * Prepare LatePoint Bookings List.
16553 *
16554 * @param array $data Search Params.
16555 * @return array
16556 */
16557 public function search_lp_bookings_list( $data ) {
16558
16559 if ( ! class_exists( 'OsBookingHelper' ) ) {
16560 return [];
16561 }
16562
16563 $bookings = OsBookingHelper::get_bookings_for_select();
16564 $options = [];
16565
16566 if ( ! empty( $bookings ) ) {
16567 foreach ( $bookings as $key => $booking ) {
16568 $options[] = [
16569 'label' => $booking['label'],
16570 'value' => $booking['value'],
16571 ];
16572 }
16573 }
16574
16575 return [
16576 'options' => $options,
16577 'hasMore' => false,
16578 ];
16579 }
16580
16581 /**
16582 * Prepare LatePoint Services List.
16583 *
16584 * @param array $data Search Params.
16585 * @return array
16586 */
16587 public function search_lp_services_list( $data ) {
16588
16589 if ( ! class_exists( 'OsServiceHelper' ) ) {
16590 return [];
16591 }
16592
16593 $services = OsServiceHelper::get_services_list();
16594 $options = [];
16595
16596 if ( ! empty( $services ) ) {
16597 foreach ( $services as $key => $service ) {
16598 $options[] = [
16599 'label' => $service['label'],
16600 'value' => $service['value'],
16601 ];
16602 }
16603 }
16604
16605 return [
16606 'options' => $options,
16607 'hasMore' => false,
16608 ];
16609 }
16610
16611 /**
16612 * Prepare LatePoint Agents List.
16613 *
16614 * @param array $data Search Params.
16615 * @return array
16616 */
16617 public function search_lp_agents_list( $data ) {
16618
16619 if ( ! class_exists( 'OsAgentHelper' ) ) {
16620 return [];
16621 }
16622
16623 $agent_ids_for_service = OsAgentHelper::get_agent_ids_for_service_and_location( $data['dynamic'] );
16624 $agents = OsAgentHelper::get_agents_list();
16625 $options = [];
16626
16627 if ( ! empty( $agents ) ) {
16628 foreach ( $agents as $key => $agent ) {
16629 if ( in_array( $agent['value'], $agent_ids_for_service ) ) {
16630 $options[] = [
16631 'label' => $agent['label'],
16632 'value' => $agent['value'],
16633 ];
16634 }
16635 }
16636 }
16637
16638 return [
16639 'options' => $options,
16640 'hasMore' => false,
16641 ];
16642 }
16643
16644 /**
16645 * Prepare LatePoint Agents WP User List.
16646 *
16647 * @param array $data Search Params.
16648 * @return array
16649 */
16650 public function search_lp_user_agents_list( $data ) {
16651
16652 if ( ! class_exists( 'OsWpUserHelper' ) ) {
16653 return [];
16654 }
16655 $agents = OsWpUserHelper::get_wp_users_for_select( [ 'role' => 'latepoint_agent' ] );
16656 $options = [];
16657 if ( ! empty( $agents ) ) {
16658 $options = $agents;
16659 }
16660 return [
16661 'options' => $options,
16662 'hasMore' => false,
16663 ];
16664 }
16665
16666 /**
16667 * Prepare LatePoint Statuses List.
16668 *
16669 * @param array $data Search Params.
16670 * @return array
16671 */
16672 public function search_lp_statuses_list( $data ) {
16673
16674 if ( ! class_exists( 'OsBookingHelper' ) ) {
16675 return [];
16676 }
16677
16678 $statuses = OsBookingHelper::get_statuses_list();
16679 $options = [];
16680
16681 if ( ! empty( $statuses ) ) {
16682 foreach ( $statuses as $key => $label ) {
16683 $options[] = [
16684 'label' => $label,
16685 'value' => $key,
16686 ];
16687 }
16688 }
16689
16690 return [
16691 'options' => $options,
16692 'hasMore' => false,
16693 ];
16694 }
16695
16696 /**
16697 * Prepare LatePoint Customers List.
16698 *
16699 * @param array $data Search Params.
16700 * @return array
16701 */
16702 public function search_lp_customers_list( $data ) {
16703
16704 if ( ! class_exists( 'OsCustomerHelper' ) ) {
16705 return [];
16706 }
16707
16708 $customers = OsCustomerHelper::get_customers_for_select();
16709 $options = [];
16710
16711 if ( ! empty( $customers ) ) {
16712 foreach ( $customers as $key => $customer ) {
16713 $options[] = [
16714 'label' => $customer['label'],
16715 'value' => $customer['value'],
16716 ];
16717 }
16718 }
16719
16720 return [
16721 'options' => $options,
16722 'hasMore' => false,
16723 ];
16724 }
16725
16726 /**
16727 * Prepare SureForms Forms List.
16728 *
16729 * @param array $data Search Params.
16730 * @return array
16731 */
16732 public function search_sureforms_form_list( $data ) {
16733
16734 $options = [];
16735
16736 $page = $data['page'];
16737 $limit = Utilities::get_search_page_limit();
16738 $offset = $limit * ( $page - 1 );
16739
16740 $forms = get_posts(
16741 [
16742 'posts_per_page' => - 1,
16743 'orderby' => 'name',
16744 'order' => 'asc',
16745 'post_type' => 'sureforms_form',
16746 'post_status' => 'publish',
16747 ]
16748 );
16749 $form_count = wp_count_posts( 'sureforms_form' )->publish;
16750
16751 if ( ! empty( $forms ) ) {
16752 if ( is_array( $forms ) ) {
16753 foreach ( $forms as $form ) {
16754 $title = html_entity_decode( get_the_title( $form->ID ), ENT_QUOTES, 'UTF-8' );
16755 $options[] = [
16756 'label' => $title,
16757 'value' => $form->ID,
16758 ];
16759 }
16760 }
16761 }
16762
16763 return [
16764 'options' => $options,
16765 'hasMore' => $form_count > $limit && $form_count > $offset,
16766 ];
16767 }
16768
16769 /**
16770 * Prepare Academy Course List.
16771 *
16772 * @param array $data Search Params.
16773 * @return array
16774 */
16775 public function search_ac_lms_courses( $data ) {
16776
16777 $options = [];
16778
16779 $page = $data['page'];
16780 $limit = Utilities::get_search_page_limit();
16781 $offset = $limit * ( $page - 1 );
16782
16783 $courses = get_posts(
16784 [
16785 'posts_per_page' => - 1,
16786 'orderby' => 'name',
16787 'order' => 'asc',
16788 'post_type' => 'academy_courses',
16789 'post_status' => 'publish',
16790 ]
16791 );
16792 $course_count = wp_count_posts( 'academy_courses' )->publish;
16793
16794 if ( ! empty( $courses ) ) {
16795 if ( is_array( $courses ) ) {
16796 foreach ( $courses as $course ) {
16797 $options[] = [
16798 'label' => get_the_title( $course->ID ),
16799 'value' => $course->ID,
16800 ];
16801 }
16802 }
16803 }
16804
16805 return [
16806 'options' => $options,
16807 'hasMore' => $course_count > $limit && $course_count > $offset,
16808 ];
16809 }
16810
16811 /**
16812 * Prepare Academy Lesson List.
16813 *
16814 * @param array $data Search Params.
16815 * @return array
16816 */
16817 public function search_ac_lms_lessons( $data ) {
16818
16819 $options = [];
16820
16821 if ( ! class_exists( '\Academy\Helper' ) ) {
16822 return [];
16823 }
16824
16825 $curriculums = \Academy\Helper::get_course_curriculum( $data['dynamic'] );
16826
16827 if ( ! empty( $curriculums ) ) {
16828 foreach ( $curriculums as $topic ) {
16829 if ( isset( $topic['topics'] ) && is_array( $topic['topics'] ) ) {
16830 foreach ( $topic['topics'] as $lesson ) {
16831 if ( isset( $lesson['type'] ) && 'lesson' === $lesson['type'] ) {
16832 $options[] = [
16833 'label' => $lesson['name'],
16834 'value' => $lesson['id'],
16835 ];
16836 }
16837 }
16838 }
16839 }
16840 }
16841
16842 return [
16843 'options' => $options,
16844 'hasMore' => false,
16845 ];
16846 }
16847
16848 /**
16849 * Prepare Academy Quiz List.
16850 *
16851 * @param array $data Search Params.
16852 * @return array
16853 */
16854 public function search_ac_lms_quiz( $data ) {
16855
16856 $options = [];
16857
16858 $page = $data['page'];
16859 $limit = Utilities::get_search_page_limit();
16860 $offset = $limit * ( $page - 1 );
16861
16862 $quizs = get_posts(
16863 [
16864 'posts_per_page' => - 1,
16865 'orderby' => 'name',
16866 'order' => 'asc',
16867 'post_type' => 'academy_quiz',
16868 'post_status' => 'publish',
16869 ]
16870 );
16871 $quiz_count = wp_count_posts( 'academy_quiz' )->publish;
16872
16873 if ( ! empty( $quizs ) ) {
16874 if ( is_array( $quizs ) ) {
16875 foreach ( $quizs as $quiz ) {
16876 $options[] = [
16877 'label' => get_the_title( $quiz->ID ),
16878 'value' => $quiz->ID,
16879 ];
16880 }
16881 }
16882 }
16883
16884 return [
16885 'options' => $options,
16886 'hasMore' => $quiz_count > $limit && $quiz_count > $offset,
16887 ];
16888 }
16889
16890 /**
16891 * Search Academy LMS data.
16892 *
16893 * @param array $data data.
16894 * @return array|void
16895 */
16896 public function search_ac_lms_last_data( $data ) {
16897 global $wpdb;
16898 $post_type = $data['post_type'];
16899 $trigger = $data['search_term'];
16900 $context = [];
16901
16902 if ( ! class_exists( '\Academy\Helper' ) ) {
16903 return [];
16904 }
16905
16906 $course_id = -1;
16907 $lesson_id = -1;
16908 $quiz_id = -1;
16909 if ( 'ac_lms_course_completed' === $trigger ) {
16910 $course_id = $data['filter']['course']['value'];
16911 } elseif ( 'ac_lms_lesson_completed' === $trigger ) {
16912 $course_id = $data['filter']['course']['value'];
16913 $lesson_id = $data['filter']['lesson']['value'];
16914 } elseif ( 'ac_lms_quiz_completed' === $trigger || 'ac_lms_quiz_failed' === $trigger ) {
16915 $quiz_id = $data['filter']['quiz']['value'];
16916 } elseif ( 'ac_lms_enrolled_course' === $trigger ) {
16917 $course_id = $data['filter']['course']['value'];
16918 }
16919
16920 $users = get_users(
16921 [
16922 'fields' => 'ID',
16923 'meta_key' => 'is_academy_student',
16924 ]
16925 );
16926 if ( ! empty( $users ) ) {
16927 $user_random_key = array_rand( $users );
16928 $user_id = $users[ $user_random_key ];
16929 }
16930
16931 if ( 'ac_lms_course_completed' === $trigger ) {
16932 if ( -1 === $course_id ) {
16933 $result = $wpdb->get_results(
16934 $wpdb->prepare(
16935 "SELECT * FROM {$wpdb->prefix}comments
16936 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",
16937 $post_type
16938 )
16939 );
16940 } else {
16941 $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 ) );
16942 }
16943 } elseif ( 'ac_lms_lesson_completed' === $trigger ) {
16944 if ( -1 === $course_id ) {
16945 $courses = get_posts(
16946 [
16947 'posts_per_page' => - 1,
16948 'post_type' => 'academy_courses',
16949 'post_status' => 'publish',
16950 'fields' => 'ids',
16951 ]
16952 );
16953 $course_id = array_rand( $courses );
16954 $option_name = 'academy_course_' . $course_id . '_completed_topics';
16955 } else {
16956 $option_name = 'academy_course_' . $course_id . '_completed_topics';
16957 }
16958 if ( ! empty( $users ) ) {
16959 $meta = get_user_meta( $user_id, $option_name, true );
16960 if ( is_string( $meta ) ) {
16961 $saved_topics_lists = (array) json_decode( $meta, true );
16962 if ( -1 === $lesson_id ) {
16963 $result = $saved_topics_lists['lesson'];
16964 } else {
16965 if ( is_array( $saved_topics_lists['lesson'] ) ) {
16966 if ( array_key_exists( $lesson_id, $saved_topics_lists['lesson'] ) ) {
16967 $result = $saved_topics_lists['lesson'];
16968 }
16969 }
16970 }
16971 }
16972 }
16973 } elseif ( 'ac_lms_quiz_completed' === $trigger ) {
16974 if ( -1 === $quiz_id ) {
16975 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE attempt_status='passed' order by attempt_id DESC LIMIT 1" );
16976 } else {
16977 $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 ) );
16978 }
16979 } elseif ( 'ac_lms_quiz_failed' === $trigger ) {
16980 if ( -1 === $quiz_id ) {
16981 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE attempt_status='failed' order by attempt_id DESC LIMIT 1" );
16982 } else {
16983 $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 ) );
16984 }
16985 } elseif ( 'ac_lms_enrolled_course' === $trigger ) {
16986 if ( -1 === $course_id ) {
16987 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type = %s order by ID DESC LIMIT 1", 'academy_enrolled' ) );
16988 } else {
16989 $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 ) );
16990 }
16991 }
16992
16993 if ( ! empty( $result ) ) {
16994 switch ( $trigger ) {
16995 case 'ac_lms_course_completed':
16996 $data = WordPress::get_post_context( $result[0]->comment_post_ID );
16997 $context_data = WordPress::get_user_context( $result[0]->user_id );
16998 $context_data['course_data'] = $data;
16999 $context_data['course'] = $result[0]->comment_post_ID;
17000 break;
17001 case 'ac_lms_enrolled_course':
17002 $data = WordPress::get_post_context( $result[0]->post_parent );
17003 $context_data = WordPress::get_user_context( $result[0]->post_author );
17004 $context_data['course_data'] = $data;
17005 $context_data['enrollment_data'] = $result[0];
17006 $context_data['course'] = $result[0]->post_parent;
17007 break;
17008 case 'ac_lms_lesson_completed':
17009 if ( -1 === $lesson_id ) {
17010 $key = array_rand( $result );
17011 $lesson_data = \Academy\Helper::get_lesson( $key );
17012 } else {
17013 $lesson_data = \Academy\Helper::get_lesson( $lesson_id );
17014 }
17015 if ( is_object( $lesson_data ) ) {
17016 $lesson_data = get_object_vars( $lesson_data );
17017 }
17018 if ( ! empty( $users ) ) {
17019 $context_data = array_merge( $lesson_data, WordPress::get_user_context( $user_id ) );
17020 }
17021 $context_data['course_data'] = WordPress::get_post_context( $course_id );
17022 $context_data['lesson'] = $lesson_id;
17023 $context_data['course'] = $course_id;
17024 break;
17025 case 'ac_lms_quiz_completed':
17026 case 'ac_lms_quiz_failed':
17027 $context_data = WordPress::get_user_context( $result[0]->user_id );
17028 $context_data['quiz_data'] = WordPress::get_post_context( $result[0]->quiz_id );
17029 $context_data['quiz_attempt_details'] = $result;
17030 $context_data['quiz'] = $result[0]->quiz_id;
17031 break;
17032 default:
17033 return;
17034 }
17035 $context['pluggable_data'] = $context_data;
17036 $context['response_type'] = 'live';
17037 } elseif ( empty( $result ) ) {
17038 switch ( $trigger ) {
17039 case 'ac_lms_course_completed':
17040 $sample_data = '{"pluggable_data":{"wp_user_id": 73, "user_login": "abc@yopmail.com","display_name": "data data1", "user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"course_data": {"ID": 6949,"post_author": "1","post_date": "2023-11-29 05:37:50","post_date_gmt": "2023-11-29 05:37:50","post_content": "<!-- wp:paragraph -->\n<p>this is a business course.<\/p>\n<!-- \/wp:paragraph -->","post_title": "Business Course","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "open","post_password": "","post_name": "business-course","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:27","post_modified_gmt": "2023-11-29 09:50:27","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/course\/business-course\/","menu_order": 0,"post_type": "academy_courses","post_mime_type": "","comment_count": "0","filter": "raw"},"course": 1},"response_type":"sample"}';
17041 break;
17042 case 'ac_lms_lesson_completed':
17043 $sample_data = '{"pluggable_data":{"ID": "1","lesson_author": "1","lesson_date": "2023-11-29 07:01:03","lesson_date_gmt": "2023-11-29 07:01:03","lesson_title": "Lesson 1","lesson_name": "","lesson_content": "","lesson_excerpt": "","lesson_status": "publish","comment_status": "close","comment_count": "0","lesson_password": "","lesson_modified": "2023-11-29 07:01:03","lesson_modified_gmt": "2023-11-29 07:01:03","wp_user_id": 73,"user_login": "abc@yopmail.com","display_name": "data data1","user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"course_data": {"ID": 6949,"post_author": "1","post_date": "2023-11-29 05:37:50","post_date_gmt": "2023-11-29 05:37:50","post_content": "<!-- wp:paragraph -->\n<p>this is a business course.<\/p>\n<!-- \/wp:paragraph -->","post_title": "Business Course","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "open","post_password": "","post_name": "business-course","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:27","post_modified_gmt": "2023-11-29 09:50:27","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/course\/business-course\/","menu_order": 0,"post_type": "academy_courses","post_mime_type": "","comment_count": "0","filter": "raw"},"lesson": 1,"course":1},"response_type":"sample"}';
17044 break;
17045 case 'ac_lms_quiz_completed':
17046 $sample_data = '{"pluggable_data":{"wp_user_id": 73,"user_login": "abc@yopmail.com","display_name": "data data1","user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"quiz_data": {"ID": 6960,"post_author": "1","post_date": "2023-11-29 09:49:42","post_date_gmt": "2023-11-29 09:49:42","post_content": "","post_title": "First Quiz","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "closed","post_password": "","post_name": "first-quiz","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:11","post_modified_gmt": "2023-11-29 09:50:11","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/?post_type=academy_quiz&#038;p=6960","menu_order": 0,"post_type": "academy_quiz","post_mime_type": "","comment_count": "0","filter": "raw"}, "quiz_attempt_details": [{"attempt_id": "2","course_id": "6949","quiz_id": "6960","user_id": "126","total_questions": "1","total_answered_questions": "1","total_marks": "20.00","earned_marks": "20.00","attempt_info": "{\"total_correct_answers\":1}","attempt_status": "passed","attempt_ip": "127.0.0.1","attempt_started_at": "2023-11-30 06:20:10","attempt_ended_at": "2023-11-30 06:20:10","is_manually_reviewed": null,"manually_reviewed_at": null}],"quiz":1},"response_type":"sample"}';
17047 break;
17048 case 'ac_lms_quiz_failed':
17049 $sample_data = '{"pluggable_data":{"wp_user_id": 73,"user_login": "abc@yopmail.com","display_name": "data data1","user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"quiz_data": {"ID": 6960,"post_author": "1","post_date": "2023-11-29 09:49:42","post_date_gmt": "2023-11-29 09:49:42","post_content": "","post_title": "First Quiz","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "closed","post_password": "","post_name": "first-quiz","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:11","post_modified_gmt": "2023-11-29 09:50:11","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/?post_type=academy_quiz&#038;p=6960","menu_order": 0,"post_type": "academy_quiz","post_mime_type": "","comment_count": "0","filter": "raw"}, "quiz_attempt_details": [{"attempt_id": "2","course_id": "6949","quiz_id": "6960","user_id": "126","total_questions": "1","total_answered_questions": "1","total_marks": "20.00","earned_marks": "0.00","attempt_info": "{\"total_correct_answers\":0}","attempt_status": "failed","attempt_ip": "127.0.0.1","attempt_started_at": "2023-11-30 06:20:10","attempt_ended_at": "2023-11-30 06:20:10","is_manually_reviewed": null,"manually_reviewed_at": null}],"quiz":1},"response_type":"sample"}';
17050 break;
17051 case 'ac_lms_enrolled_course':
17052 $sample_data = '{"pluggable_data":{"course_data": {"ID": 6949,"post_author": "1","post_date": "2023-11-29 05:37:50","post_date_gmt": "2023-11-29 05:37:50","post_content": "<!-- wp:paragraph -->\n<p>this is a business course.<\/p>\n<!-- \/wp:paragraph -->","post_title": "Business Course","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "open","post_password": "","post_name": "business-course","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:27","post_modified_gmt": "2023-11-29 09:50:27","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/course\/business-course\/","menu_order": 0,"post_type": "academy_courses","post_mime_type": "","comment_count": "0","filter": "raw"},"enrollment_data": {"ID": "6971","post_author": "126","post_date": "2023-11-30 05:52:54","post_date_gmt": "2023-11-30 05:52:54","post_content": "","post_title": "Course Enrolled November 30, 2023 @ 5:52 am","post_excerpt": "","post_status": "completed","comment_status": "closed","ping_status": "closed","post_password": "","post_name": "course-enrolled-november-30-2023-552-am","to_ping": "","pinged": "","post_modified": "2023-11-30 05:52:54","post_modified_gmt": "2023-11-30 05:52:54","post_content_filtered": "","post_parent": "6949","guid": "https:\/\/suretriggers-wpnew.local\/?p=6971","menu_order": "0","post_type": "academy_enrolled","post_mime_type": "","comment_count": "0"},"course":1, "wp_user_id": 2,"user_login": "test","display_name": "test test","user_firstname": "test","user_lastname": "test", "user_email": "test@yopmail.com","user_role": ["academy_student"]},"response_type":"sample"}';
17053 break;
17054 default:
17055 return;
17056 }
17057 $context = (array) json_decode( $sample_data, true );
17058 }
17059 return $context;
17060 }
17061
17062 /**
17063 * Search myCred Point Type List.
17064 *
17065 * @param array $data Search Params.
17066 * @return array
17067 */
17068 public function search_mycred_point_type_list( $data ) {
17069
17070 $options = [];
17071
17072 if ( ! function_exists( 'mycred_get_types' ) ) {
17073 return [];
17074 }
17075
17076 $posts = mycred_get_types();
17077
17078 if ( ! empty( $posts ) ) {
17079 if ( is_array( $posts ) ) {
17080 foreach ( $posts as $key => $post ) {
17081 $options[] = [
17082 'label' => $post,
17083 'value' => $key,
17084 ];
17085 }
17086 }
17087 }
17088
17089 return [
17090 'options' => $options,
17091 'hasMore' => false,
17092 ];
17093 }
17094
17095 /**
17096 * Prepare elementor forms.
17097 *
17098 * @param array $data Search Params.
17099 *
17100 * @return array
17101 */
17102 public function search_new_elementor_form_fields( $data ) {
17103
17104 $fields = [];
17105 $select_form_id = $data['dynamic'];
17106 global $wpdb;
17107 $post_metas = $wpdb->get_results(
17108 $wpdb->prepare(
17109 "SELECT pm.post_id, pm.meta_value
17110 FROM $wpdb->postmeta pm
17111 LEFT JOIN $wpdb->posts p
17112 ON p.ID = pm.post_id
17113 WHERE p.post_type IS NOT NULL
17114 AND p.post_status = %s
17115 AND pm.meta_key = %s
17116 AND pm.`meta_value` LIKE %s",
17117 'publish',
17118 '_elementor_data',
17119 '%%form_fields%%'
17120 )
17121 );
17122
17123 if ( ! empty( $post_metas ) ) {
17124 foreach ( $post_metas as $post_meta ) {
17125 /**
17126 *
17127 * Ignore line
17128 *
17129 * @phpstan-ignore-next-line
17130 */
17131 $inner_forms = Utilities::search_elementor_forms( json_decode( $post_meta->meta_value ) );
17132 if ( ! empty( $inner_forms ) ) {
17133 foreach ( $inner_forms as $form ) {
17134 $form_id = explode( '_', $select_form_id );
17135 if ( is_object( $form ) ) {
17136 if ( $form->id == $form_id[1] ) {
17137 if ( ! empty( $form->settings->form_fields ) ) {
17138 foreach ( $form->settings->form_fields as $field ) {
17139 $fields[] = [
17140 'value' => $field->custom_id,
17141 'text' => ! empty( $field->field_label ) ? $field->field_label : 'unknown',
17142 ];
17143 }
17144 }
17145 }
17146 }
17147 }
17148 }
17149 }
17150 }
17151 $options = [];
17152 if ( ! empty( $fields ) ) {
17153 foreach ( $fields as $key => $value ) {
17154 $options[] = [
17155 'label' => $value['text'],
17156 'value' => $value['value'],
17157 ];
17158 }
17159 }
17160 return [
17161 'options' => $options,
17162 'hasMore' => false,
17163 ];
17164 }
17165 /**
17166 * Get Fluent Booking Appointment Events.
17167 *
17168 * @param array $data data.
17169 *
17170 * @return array
17171 */
17172 public function search_fluent_booking_calendars_list( $data ) {
17173
17174 global $wpdb;
17175
17176 $page = $data['page'];
17177 $limit = Utilities::get_search_page_limit();
17178 $offset = $limit * ( $page - 1 );
17179
17180 $calendars = $wpdb->get_results(
17181 $wpdb->prepare(
17182 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendars WHERE status = %s ORDER BY id DESC LIMIT %d OFFSET %d",
17183 [ 'active', $limit, $offset ]
17184 ),
17185 OBJECT
17186 );
17187
17188 $calendars_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
17189
17190 $options = [];
17191 if ( ! empty( $calendars ) ) {
17192 foreach ( $calendars as $calendar ) {
17193 $options[] = [
17194 'label' => $calendar->title,
17195 'value' => $calendar->id,
17196 ];
17197 }
17198 }
17199 return [
17200 'options' => $options,
17201 'hasMore' => $calendars_count > $limit && $calendars_count > $offset,
17202 ];
17203
17204 }
17205
17206 /**
17207 * Get Fluent Booking Appointment Events.
17208 *
17209 * @param array $data data.
17210 *
17211 * @return array
17212 */
17213 public function search_fluent_booking_events_list( $data ) {
17214
17215 global $wpdb;
17216 $page = $data['page'];
17217 $limit = Utilities::get_search_page_limit();
17218 $offset = $limit * ( $page - 1 );
17219 $calendar_id = sanitize_text_field( $data['dynamic']['calender_id'] );
17220 if ( '-1' === $calendar_id ) {
17221 $events = $wpdb->get_results(
17222 $wpdb->prepare(
17223 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendar_events WHERE status = %s ORDER BY id DESC LIMIT %d OFFSET %d",
17224 [ 'active', $limit, $offset ]
17225 ),
17226 OBJECT
17227 );
17228 } else {
17229 $events = $wpdb->get_results(
17230 $wpdb->prepare(
17231 "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",
17232 [ 'active', $calendar_id, $limit, $offset ]
17233 ),
17234 OBJECT
17235 );
17236 }
17237
17238 $events_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
17239
17240 $options = [];
17241 if ( ! empty( $events ) ) {
17242 foreach ( $events as $event ) {
17243 $options[] = [
17244 'label' => $event->title,
17245 'value' => $event->id,
17246 ];
17247 }
17248 }
17249 return [
17250 'options' => $options,
17251 'hasMore' => $events_count > $limit && $events_count > $offset,
17252 ];
17253
17254 }
17255
17256 /**
17257 * Get Fluent Booking last data.
17258 *
17259 * @param array $data data.
17260 *
17261 * @return array
17262 */
17263 public function search_fluent_booking_last_data( $data ) {
17264 global $wpdb;
17265 $trigger = $data['search_term'];
17266 $course_data = [];
17267 $lesson_data = [];
17268
17269 $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
17270 $context = [
17271 'pluggable_data' => json_decode( $sample_data, true ),
17272 'response_type' => 'sample',
17273 ];
17274 if ( 'fluent_booking_appointment_cancelled' === $trigger ) {
17275 $status = 'cancelled';
17276 } elseif ( 'fluent_booking_appointment_completed' === $trigger ) {
17277 $status = 'completed';
17278 }
17279
17280 $event_id = (int) $data['filter']['event_id']['value'];
17281 if ( 'fluent_booking_appointment_cancelled' === $trigger || 'fluent_booking_appointment_completed' === $trigger ) {
17282 if ( $event_id > 0 ) {
17283 $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 ) );
17284 } else {
17285 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE status= %s ORDER BY id DESC LIMIT 1", $status ) );
17286 }
17287 } elseif ( 'fluent_booking_new_appointment_booked' === $trigger ) {
17288 if ( $event_id > 0 ) {
17289 $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' ) );
17290 } else {
17291 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE status != %s ORDER BY id DESC LIMIT 1", 'cancelled' ) );
17292 }
17293 }
17294
17295 if ( ! empty( $booking ) ) {
17296 $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 ) );
17297
17298 $booking_event = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_calendar_events WHERE id= %d", $booking->event_id ) );
17299 $booking_data = $booking;
17300 $booking_data->custom_fields = $booking_meta->value;
17301 $booking_array = [
17302 'booking' => $booking_data,
17303 'event' => $booking_event,
17304 ];
17305
17306
17307 $booking_array_response = self::recursive_unserialize( $booking_array );
17308 $context['pluggable_data'] = $booking_array_response;
17309 $context['response_type'] = 'live';
17310 }
17311 return $context;
17312 }
17313
17314 /**
17315 * Recursive unserilize.
17316 *
17317 * @param array $data data.
17318 *
17319 * @return array|mixed
17320 */
17321 public static function recursive_unserialize( $data ) {
17322 if ( is_array( $data ) ) {
17323 foreach ( $data as $key => $value ) {
17324 $data[ $key ] = self::recursive_unserialize( $value );
17325 }
17326 return $data;
17327 } elseif ( is_object( $data ) && 'stdClass' === get_class( $data ) ) {
17328 foreach ( $data as $property => $value ) {
17329 $data->$property = self::recursive_unserialize( $value );
17330 }
17331 return $data;
17332 } elseif ( is_string( $data ) && self::is_serialized( strval( $data ) ) ) {
17333 return unserialize( $data );
17334 } else {
17335 return $data;
17336 }
17337
17338 }
17339
17340 /**
17341 * Check if string serialized.
17342 *
17343 * @param string $data data.
17344 *
17345 * @return bool
17346 */
17347 public static function is_serialized( $data ) {
17348 $unserialized = unserialize( $data );
17349 if ( 'b:0;' === $data || false !== $unserialized ) {
17350 return true;
17351 } else {
17352 return false;
17353 }
17354 }
17355
17356 /**
17357 * Search Tutor LMS data.
17358 *
17359 * @param array $data data.
17360 * @return array|void|mixed
17361 */
17362 public function search_tutor_lms_last_data( $data ) {
17363 global $wpdb;
17364 $post_type = $data['post_type'];
17365 $trigger = $data['search_term'];
17366 $context = [];
17367
17368 if ( ! function_exists( 'tutor_utils' ) ) {
17369 return [];
17370 }
17371
17372 $post_id = -1;
17373 if ( 'course_enrolled' === $trigger || 'tutor_courses_question' === $trigger ) {
17374 $post_id = $data['filter']['tutor_course']['value'];
17375 } elseif ( 'quiz_attempt_percentage' === $trigger || 'quiz_failed' === $trigger || 'quiz_passed' === $trigger ) {
17376 $post_id = $data['filter']['quiz_id']['value'];
17377 }
17378
17379 if ( 'course_enrolled' === $trigger ) {
17380 if ( -1 === $post_id ) {
17381 $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 ) );
17382 } else {
17383 $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 ) );
17384 }
17385 } elseif ( 'tutor_courses_question' === $trigger ) {
17386 if ( -1 === $post_id ) {
17387 $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 ) );
17388 } else {
17389 $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 ) );
17390 }
17391 } elseif ( 'quiz_passed' === $trigger ) {
17392 if ( -1 === $post_id ) {
17393 $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 ) );
17394 } else {
17395 $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 ) );
17396 }
17397 } elseif ( 'quiz_failed' === $trigger ) {
17398 if ( -1 === $post_id ) {
17399 $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 ) );
17400 } else {
17401 $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 ) );
17402 }
17403 } elseif ( 'quiz_attempt_percentage' == $trigger ) {
17404 $condition_compare = $data['filter']['condition_compare']['value'];
17405 $percentage = $data['filter']['percentage']['value'];
17406 if ( -1 === $post_id ) {
17407 $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
17408 } else {
17409 $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
17410 }
17411 }
17412
17413 if ( ! empty( $result ) ) {
17414 switch ( $trigger ) {
17415 case 'course_enrolled':
17416 $result_item_id = $result[0]->post_parent;
17417 $result_user_id = $result[0]->post_author;
17418 $context_data = array_merge(
17419 WordPress::get_user_context( $result_user_id ),
17420 WordPress::get_post_context( $result_item_id )
17421 );
17422 $context_data['tutor_course'] = $result_item_id;
17423 break;
17424 case 'tutor_courses_question':
17425 $date = $result[0]->comment_date;
17426 $data = [
17427 'comment_post_ID' => $result[0]->comment_post_ID,
17428 'comment_author' => $result[0]->comment_author,
17429 'comment_date' => $date,
17430 'comment_date_gmt' => get_gmt_from_date( $date ),
17431 'comment_content' => $result[0]->comment_content,
17432 'comment_approved' => 'approved',
17433 'comment_agent' => 'TutorLMSPlugin',
17434 'comment_type' => 'tutor_q_and_a',
17435 'comment_parent' => $result[0]->comment_parent,
17436 'user_id' => $result[0]->user_id,
17437 ];
17438 $context_data['tutor_course'] = $result[0]->comment_post_ID;
17439 $context_data['data'] = $data;
17440 break;
17441 case 'quiz_attempt_percentage':
17442 case 'quiz_failed':
17443 case 'quiz_passed':
17444 $attempt = tutor_utils()->get_attempt( $result[0]->attempt_id );
17445 $context_data = WordPress::get_user_context( $result[0]->user_id );
17446 $context_data['quiz_id'] = $attempt->quiz_id;
17447 $context_data['attempt_id'] = $result[0]->attempt_id;
17448 $context_data['attempt'] = $attempt;
17449 break;
17450 default:
17451 return;
17452 }
17453 if ( ! empty( $context_data ) ) {
17454 $context['pluggable_data'] = $context_data;
17455 $context['response_type'] = 'live';
17456 }
17457 } elseif ( empty( $result ) ) {
17458 switch ( $trigger ) {
17459 case 'course_enrolled':
17460 $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"} ';
17461 break;
17462 case 'tutor_courses_question':
17463 $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"}';
17464 break;
17465 case 'quiz_attempt_percentage':
17466 case 'quiz_failed':
17467 case 'quiz_passed':
17468 $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"}';
17469 break;
17470 default:
17471 return;
17472 }
17473 $context = json_decode( $sample_data, true );
17474 }
17475
17476 return $context;
17477 }
17478
17479 /**
17480 * Get Asgorus Forum list
17481 *
17482 * @param array $data data.
17483 *
17484 * @return array
17485 */
17486 public function search_asgorus_forums_list( $data ) {
17487 if ( ! class_exists( 'AsgarosForum' ) ) {
17488 return [];
17489 }
17490 $category = $data['dynamic'];
17491 if ( is_array( $category ) ) {
17492 $category_id = $category['forum_category'];
17493 } else {
17494 $category_id = $category;
17495 }
17496 $asgaros_forum = new AsgarosForum();
17497 $forums = $asgaros_forum->get_forums( $category_id );
17498 $options = [];
17499 if ( ! empty( $forums ) ) {
17500 foreach ( $forums as $forum ) {
17501 $options[] = [
17502 'label' => $forum->name,
17503 'value' => $forum->id,
17504 ];
17505 }
17506 }
17507 return [
17508 'options' => $options,
17509 'hasMore' => false,
17510 ];
17511 }
17512
17513 /**
17514 * Get Asgorus Categories list
17515 *
17516 * @param array $data data.
17517 *
17518 * @return array
17519 */
17520 public function search_asgorus_categories_list( $data ) {
17521 if ( ! class_exists( 'AsgarosForum' ) ) {
17522 return [];
17523 }
17524 $asgaros_forum = new AsgarosForum();
17525 $categories = (array) $asgaros_forum->content->get_categories();
17526 $options = [];
17527
17528 if ( ! empty( $categories ) ) {
17529 foreach ( $categories as $category ) {
17530 $options[] = [
17531 'label' => $category->name,
17532 'value' => $category->term_id,
17533 ];
17534 }
17535 }
17536 return [
17537 'options' => $options,
17538 'hasMore' => false,
17539 ];
17540 }
17541
17542 /**
17543 * Get Asgorus Topic lists
17544 *
17545 * @param array $data data.
17546 *
17547 * @return array
17548 */
17549 public function search_asgorus_topic_list( $data ) {
17550 if ( ! class_exists( 'AsgarosForum' ) ) {
17551 return [];
17552 }
17553 global $wpdb;
17554 $forum_id = $data['dynamic'];
17555 $asgaros_forum = new AsgarosForum();
17556 $sql = 'SELECT name,id FROM ' . $wpdb->prefix . 'forum_topics WHERE parent_id = %d AND closed = 0 ORDER BY id';
17557 $topics = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id ), ARRAY_A );// @phpcs:ignore
17558 $options = [];
17559
17560 if ( ! empty( $topics ) ) {
17561 foreach ( $topics as $topic ) {
17562 $options[] = [
17563 'label' => $topic['name'],
17564 'value' => $topic['id'],
17565 ];
17566 }
17567 }
17568 return [
17569 'options' => $options,
17570 'hasMore' => false,
17571 ];
17572 }
17573
17574 /**
17575 * Get Asgorus Topic Last Data
17576 *
17577 * @param array $data data.
17578 *
17579 * @return array
17580 */
17581 public function search_pluggables_asgaros_topic_last_data( $data ) {
17582 if ( ! class_exists( 'AsgarosForum' ) ) {
17583 return [];
17584 }
17585 $asgaros_forum = new AsgarosForum();
17586 $context = [];
17587 global $wpdb;
17588 $forum_id = $data['filter']['forum_id']['value'];
17589
17590 if ( -1 == $forum_id ) {
17591 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'forum_topics WHERE closed = 0 ORDER BY id DESC LIMIT 1', ARRAY_A );
17592 } else {
17593 $forum = $forum_id;
17594 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'forum_topics WHERE parent_id = %d AND closed = 0 ORDER BY id DESC LIMIT 1';
17595 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum ), ARRAY_A );// @phpcs:ignore
17596 }
17597
17598 if ( ! empty( $results ) ) {
17599 $sql_post = 'SELECT * FROM ' . $wpdb->prefix . 'forum_posts WHERE parent_id = %d ORDER BY id DESC LIMIT 1';
17600 $results_post = $wpdb->get_results( $wpdb->prepare( $sql_post, $results[0]['id'] ), ARRAY_A ); // @phpcs:ignore
17601 $context['pluggable_data']['forum_id'] = $results[0]['parent_id'];
17602 $context['pluggable_data']['topic_id'] = $results[0]['id'];
17603 $context['pluggable_data']['post_id'] = $results_post[0]['id'];
17604 $context['pluggable_data']['forum'] = $asgaros_forum->content->get_forum( $results[0]['parent_id'] );
17605 $context['pluggable_data']['topic'] = $asgaros_forum->content->get_topic( $results[0]['id'] );
17606 $context['pluggable_data']['post'] = $asgaros_forum->content->get_post( $results_post[0]['id'] );
17607
17608 $context['pluggable_data']['author'] = WordPress::get_user_context( $results[0]['author_id'] );
17609 $context['response_type'] = 'live';
17610 } else {
17611 $context = json_decode( '{"pluggable_data":{"forum_id":"1","topic_id":"2","forum":{"id":"1","name":"First Forum","parent_id":"64","parent_forum":"0","description":"My first forum.","icon":"fas fa-comments","sort":"1","forum_status":"normal","slug":"first-forum"},"topic":{"id":"2","parent_id":"1","author_id":"1","views":"2","name":"other topic","sticky":"0","closed":"0","approved":"1","slug":"other-topic"},"user":{"wp_user_id":1,"user_login":"suredev","display_name":"SureDev","user_firstname":"Sure","user_lastname":"Dev","user_email":"dev-suretest@suretriggers.com","user_role":["administrator"]}},"response_type":"sample"}', true );// @phpcs:ignore
17612 }
17613 return $context;
17614 }
17615
17616 /**
17617 * Get Asgorus Topic Reply Last Data
17618 *
17619 * @param array $data data.
17620 *
17621 * @return array
17622 */
17623 public function search_pluggables_asgaros_reply_last_data( $data ) {
17624 if ( ! class_exists( 'AsgarosForum' ) ) {
17625 return [];
17626 }
17627 $asgaros_forum = new AsgarosForum();
17628 $context = [];
17629 global $wpdb;
17630 $forum_id = $data['filter']['forum_id']['value'];
17631 $topic_id = $data['filter']['forum_id']['value'];
17632 if ( -1 == $topic_id ) {
17633 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'forum_posts ORDER BY id DESC LIMIT 1', ARRAY_A );
17634 } else {
17635 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'forum_posts WHERE parent_id = %d ORDER BY id DESC LIMIT 1';
17636 $results = $wpdb->get_results( $wpdb->prepare( $sql, $topic_id ), ARRAY_A );// @phpcs:ignore
17637 }
17638
17639 if ( ! empty( $results ) ) {
17640 $topic_id = $results[0]['parent_id'];
17641 $post_id = $results[0]['id'];
17642 $context['pluggable_data']['forum_id'] = $forum_id;
17643 $context['pluggable_data']['topic_id'] = $topic_id;
17644 $context['pluggable_data']['post_id'] = $post_id;
17645 $context['pluggable_data']['forum'] = $asgaros_forum->content->get_forum( $forum_id );
17646 $context['pluggable_data']['topic'] = $asgaros_forum->content->get_topic( $topic_id );
17647 $context['pluggable_data']['post'] = $asgaros_forum->content->get_post( $post_id );
17648
17649 $context['pluggable_data']['author'] = WordPress::get_user_context( $results[0]['author_id'] );
17650 $context['response_type'] = 'live';
17651 } else {
17652 $context = json_decode( '{"pluggable_data":{"forum_id":"1","topic_id":"2","forum":{"id":"1","name":"First Forum","parent_id":"64","parent_forum":"0","description":"My first forum.","icon":"fas fa-comments","sort":"1","forum_status":"normal","slug":"first-forum"},"topic":{"id":"2","parent_id":"1","author_id":"1","views":"2","name":"other topic","sticky":"0","closed":"0","approved":"1","slug":"other-topic"},"user":{"wp_user_id":1,"user_login":"suredev","display_name":"SureDev","user_firstname":"Sure","user_lastname":"Dev","user_email":"dev-suretest@suretriggers.com","user_role":["administrator"]}},"response_type":"sample"}', true );// @phpcs:ignore
17653 }
17654 return $context;
17655 }
17656
17657 /**
17658 * Get WPLoyalty Points Awarded Last Data
17659 *
17660 * @param array $data data.
17661 *
17662 * @return array|mixed|string
17663 */
17664 public function search_wp_loyalty_points_awarded_customer( $data ) {
17665
17666 $context = [];
17667 global $wpdb;
17668 if ( ! class_exists( 'Wlr\App\Helpers\Base' ) ) {
17669 return [];
17670 }
17671 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wlr_earn_campaign_transaction WHERE transaction_type = %s ORDER BY id DESC LIMIT 1';
17672 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'credit' ), ARRAY_A );// @phpcs:ignore
17673
17674 if ( ! empty( $results ) ) {
17675 $context['pluggable_data']['user_email'] = $results[0]['user_email'];
17676 $context['pluggable_data']['points_earned'] = $results[0]['points'];
17677 $context['pluggable_data']['action_type'] = $results[0]['action_type'];
17678 $base_helper = new \Wlr\App\Helpers\Base();
17679 $user = $base_helper->getPointUserByEmail( $results[0]['user_email'] );
17680 $points_sql = 'SELECT * FROM ' . $wpdb->prefix . 'wlr_expire_points
17681 WHERE user_email = %s ORDER BY id DESC LIMIT 1';
17682 $points_results = $wpdb->get_results( $wpdb->prepare( $points_sql, $results[0]['user_email'] ), ARRAY_A );// @phpcs:ignore
17683 $context['pluggable_data']['user'] = $user;
17684 if ( ! empty( $points_results ) ) {
17685 $expire_date = $points_results[0]['expire_date'];
17686 $timestamp = is_numeric( $expire_date ) ? (int) $expire_date : null;
17687 $date_format = get_option( 'date_format' );
17688 if ( is_string( $date_format ) ) {
17689 $context['pluggable_data']['point_expiry_date'] = wp_date( $date_format, $timestamp );
17690 }
17691 }
17692 $context['response_type'] = 'live';
17693 } else {
17694 $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
17695 }
17696 return $context;
17697 }
17698
17699 /**
17700 * Get WPLoyalty Campaign Type List
17701 *
17702 * @param array $data data.
17703 *
17704 * @return array
17705 */
17706 public function search_wp_loyalty_action_type_list( $data ) {
17707 $options = [];
17708 if ( ! class_exists( 'Wlr\App\Helpers\Woocommerce' ) ) {
17709 return [];
17710 }
17711
17712 $woocommerce_helper = new \Wlr\App\Helpers\Woocommerce();
17713 $action_types = $woocommerce_helper->getActionTypes();
17714
17715 if ( ! empty( $action_types ) ) {
17716 foreach ( $action_types as $key => $type ) {
17717 $options[] = [
17718 'label' => $type,
17719 'value' => $key,
17720 ];
17721 }
17722 }
17723 return [
17724 'options' => $options,
17725 'hasMore' => false,
17726 ];
17727 }
17728
17729 /**
17730 * Get last data for trigger.
17731 *
17732 * @param array $data data.
17733 * @return array
17734 */
17735 public function search_slicewp_last_data( $data ) {
17736 global $wpdb;
17737
17738 $context = [];
17739 $context['response_type'] = 'sample';
17740
17741 $user_data = WordPress::get_sample_user_context();
17742
17743
17744 if ( ! function_exists( 'slicewp_get_affiliate' ) ) {
17745 return [];
17746 }
17747
17748 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
17749
17750 if ( in_array( $term, [ 'slicewp_new_affiliate', 'slicewp_update_affiliate' ], true ) ) {
17751 $affiliate = [
17752 'id' => 14,
17753 'user_id' => 25,
17754 'date_created' => '2024-03-14 12:35:50',
17755 'date_modified' => '2024-03-14 12:36:20',
17756 'payment_email' => 'testcustomer12@gmail.com',
17757 'website' => '',
17758 'status' => 'active',
17759 'parent_id' => 0,
17760 ];
17761 $context['pluggable_data'] = array_merge( $affiliate, [ 'user' => $user_data ] );
17762
17763 if ( ( ! empty( $data['filter'] ) && '-1' === $data['filter']['affiliate_id']['value'] ) || empty( $data['filter'] ) ) {
17764 $query = $wpdb->prepare(
17765 "
17766 SELECT *
17767 FROM {$wpdb->prefix}slicewp_affiliates
17768 WHERE status = %s
17769 ORDER BY id DESC
17770 LIMIT 1",
17771 'active'
17772 );
17773 $affiliate_results = $wpdb->get_row( $query ); //phpcs:ignore
17774
17775 } else {
17776 $affiliate_id = $data['filter']['affiliate_id']['value'];
17777 $query = $wpdb->prepare(
17778 "
17779 SELECT *
17780 FROM {$wpdb->prefix}slicewp_affiliates
17781 WHERE status = %s AND id = %d
17782 ORDER BY id DESC
17783 LIMIT 1",
17784 'active',
17785 $affiliate_id
17786 );
17787 $affiliate_results = $wpdb->get_row( $query ); //phpcs:ignore
17788
17789 }
17790
17791
17792
17793
17794 if ( ! empty( $affiliate_results ) ) {
17795 $context['pluggable_data'] = (array) $affiliate_results;
17796
17797 $user_data = WordPress::get_user_context( $affiliate_results->user_id );
17798 $context['pluggable_data']['user'] = $user_data;
17799 $context['response_type'] = 'live';
17800 }
17801 } elseif ( in_array( $term, [ 'slicewp_new_commission', 'slicewp_update_commission' ], true ) ) {
17802 $commission = [
17803 'id' => 14,
17804 'user_id' => 25,
17805 'date_created' => '2024-03-14 12:35:50',
17806 'date_modified' => '2024-03-14 12:36:20',
17807 'payment_email' => 'testcustomer12@gmail.com',
17808 'website' => '',
17809 'status' => 'active',
17810 'parent_id' => 0,
17811 ];
17812 $context['pluggable_data'] = array_merge( $commission, [ 'user' => $user_data ] );
17813 $affiliate_id = $data['filter']['affiliate_id']['value'];
17814 if ( -1 === $data['filter']['commission_id']['value'] ) {
17815 $query = $wpdb->prepare(
17816 "
17817 SELECT *
17818 FROM {$wpdb->prefix}slicewp_commissions WHERE affiliate_id=%d ORDER BY id DESC limit 1",
17819 $affiliate_id
17820 );
17821 $commission_results = $wpdb->get_row( $query ); //phpcs:ignore
17822 } else {
17823 $commission_id = $data['filter']['commission_id']['value'];
17824 $query = $wpdb->prepare(
17825 "
17826 SELECT *
17827 FROM {$wpdb->prefix}slicewp_commissions WHERE id= %d AND affiliate_id=%d ORDER BY id DESC limit 1",
17828 $commission_id,
17829 $affiliate_id
17830 );
17831 $commission_results = $wpdb->get_row( $query ); //phpcs:ignore
17832 }
17833
17834
17835
17836
17837 if ( ! empty( $commission_results ) ) {
17838 $context['pluggable_data'] = (array) $commission_results;
17839 $affiliate = slicewp_get_affiliate( $commission_results->affiliate_id );
17840 $user_id = $affiliate->get( 'user_id' );
17841 $user_data = WordPress::get_user_context( $user_id );
17842 $context['pluggable_data']['user'] = $user_data;
17843 $context['response_type'] = 'live';
17844 }
17845 }
17846
17847 return $context;
17848 }
17849
17850
17851 /**
17852 * Get Ninja Tables Last Data
17853 *
17854 * @param array $data data.
17855 *
17856 * @return array
17857 */
17858 public function search_ninja_tables_last_data( $data ) {
17859 $context = [];
17860 global $wpdb;
17861 $table_id = isset( $data['filter']['table_id']['value'] ) ? $data['filter']['table_id']['value'] : -1;
17862
17863 if ( -1 == $table_id ) {
17864 $results = $wpdb->get_row( 'SELECT * FROM ' . $wpdb->prefix . 'ninja_table_items ORDER BY id DESC LIMIT 1', ARRAY_A );
17865 } else {
17866 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'ninja_table_items WHERE table_id = %d ORDER BY id DESC LIMIT 1';
17867 $results = $wpdb->get_row( $wpdb->prepare( $sql, $table_id ), ARRAY_A );// @phpcs:ignore
17868 }
17869
17870 if ( ! empty( $results ) ) {
17871 $results['value'] = json_decode( $results['value'], true );
17872 $context['pluggable_data'] = $results;
17873 $context['pluggable_data']['owner'] = WordPress::get_user_context( $results['owner_id'] );
17874 $context['response_type'] = 'live';
17875 } else {
17876 $context = json_decode( '{"pluggable_data":{"id":"24","position":null,"table_id":"17484","owner_id":"29","attribute":"value","settings":null,"value":{"id":"3","name":"Suretriggers","class":"Dev","gender":"Female"},"created_at":"2024-03-21 13:11:25","updated_at":"2024-03-21 13:11:25","owner":{"wp_user_id":29,"user_login":"testingdsd","display_name":"suretest","user_firstname":"Suretrigger","user_lastname":"Dev","user_email":"johndoe@email.com","user_role":["editor"]}},"response_type":"sample"}', true );
17877 }
17878 return $context;
17879 }
17880
17881 /**
17882 * Get Ninja Tables Fields
17883 *
17884 * @param array $data data.
17885 *
17886 * @return array
17887 */
17888 public function search_ninja_table_fields( $data ) {
17889
17890 $context = [];
17891 global $wpdb;
17892 $table_id = isset( $data['dynamic'] ) ? $data['dynamic'] : -1;
17893 $fields = [];
17894 if ( $table_id > 0 ) {
17895 $fields = get_post_meta( $table_id, '_ninja_table_columns', true );
17896 }
17897
17898 return (array) $fields;
17899 }
17900
17901 /**
17902 * Get Late Point Booking Fields
17903 *
17904 * @param array $data data.
17905 *
17906 * @return array|void
17907 */
17908 public function search_late_point_booking_fields( $data ) {
17909 if ( ! class_exists( 'OsCustomFieldsController' ) ) {
17910
17911 return;
17912 }
17913 global $wpdb;
17914 $booking_fields = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}latepoint_settings WHERE name = %s", 'custom_fields_for_booking' ) );
17915 $fields = [];
17916 if ( ! empty( $booking_fields ) ) {
17917 $fields = json_decode( $booking_fields->value, true );
17918 }
17919 return (array) $fields;
17920 }
17921
17922 /** Search Voxel fields for action.
17923 *
17924 * @param array $data data.
17925 * @return array
17926 */
17927 public function search_voxel_custom_fields( $data ) {
17928 $post_type_fields = [];
17929 $post_type = $data['post_type'];
17930 if ( ! class_exists( '\Voxel\Post_Type' ) ) {
17931 return [];
17932 }
17933 $fields = \Voxel\Post_Type::get( $post_type )->get_fields();
17934 foreach ( $fields as $key => $field ) {
17935 if ( 'step-general' == $key ) {
17936 continue;
17937 }
17938 $field_props = $field->get_props();
17939 $post_type_fields[ $key ] = $field_props;
17940 }
17941 $context['fields'] = $post_type_fields;
17942 return $context;
17943 }
17944
17945 /**
17946 * Get Voxel Post Types lists
17947 *
17948 * @param array $data data.
17949 *
17950 * @return array
17951 */
17952 public function search_voxel_posttype_list( $data ) {
17953 $options = [];
17954 $voxel_post_types = [];
17955 if ( class_exists( '\Voxel\Post_Type' ) ) {
17956 $voxel_post_types = \Voxel\Post_Type::get_voxel_types();
17957 }
17958
17959 if ( ! empty( $voxel_post_types ) ) {
17960 foreach ( $voxel_post_types as $key => $voxel_post_type ) {
17961 $post_type = get_post_type_object( $key );
17962 if ( $post_type ) {
17963 if ( in_array( $post_type->name, [ 'collection' ], true ) ) {
17964 continue;
17965 }
17966 $options[] = [
17967 'label' => esc_attr( $post_type->labels->singular_name ),
17968 'value' => $post_type->name,
17969 ];
17970 }
17971 }
17972 }
17973 return [
17974 'options' => $options,
17975 'hasMore' => false,
17976 ];
17977 }
17978
17979 /**
17980 * Get Voxel Users Last Data
17981 *
17982 * @param array $data data.
17983 *
17984 * @return array|mixed|string
17985 */
17986 public function search_voxel_users_triggers_last_data( $data ) {
17987
17988 global $wpdb;
17989 $context = [];
17990 $term = $data['search_term'];
17991 if ( 'direct_message_received' === $term ) {
17992 $sql = "SELECT * FROM {$wpdb->prefix}voxel_messages WHERE receiver_type LIKE %s AND sender_type LIKE %s ORDER BY id DESC LIMIT 1";
17993 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user', 'user' ), ARRAY_A );// @phpcs:ignore
17994 } elseif ( 'user_timeline_post_created' === $term ) {
17995 $sql = "SELECT vt.* FROM {$wpdb->prefix}voxel_timeline vt WHERE vt.feed = %s AND vt.moderation = %d ORDER BY vt.id DESC LIMIT 1";
17996 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user_timeline', 0 ), ARRAY_A );// @phpcs:ignore
17997 } elseif ( 'user_timeline_post_approved' === $term ) {
17998 $sql = "SELECT vt.* FROM {$wpdb->prefix}voxel_timeline vt WHERE vt.feed = %s AND vt.moderation = %d ORDER BY vt.id DESC LIMIT 1";
17999 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user_timeline', 1 ), ARRAY_A );// @phpcs:ignore
18000 }
18001 if ( 'direct_message_received' === $term ) {
18002 if ( ! empty( $results ) ) {
18003 $context['pluggable_data']['sender'] = WordPress::get_user_context( $results[0]['sender_id'] );
18004 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $results[0]['receiver_id'] );
18005 $context['pluggable_data']['content'] = $results[0]['content'];
18006 $context['response_type'] = 'live';
18007 } else {
18008 $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
18009 }
18010 } elseif ( 'user_timeline_post_created' === $term || 'user_timeline_post_approved' === $term ) {
18011 if ( ! empty( $results ) ) {
18012 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['user_id'] );
18013 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
18014 $status_details = \Voxel\Timeline\Status::get( $results[0]['id'] );
18015 foreach ( (array) $status_details as $key => $value ) {
18016 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
18017 $context['pluggable_data'][ $clean_key ] = $value;
18018 }
18019 }
18020 $context['response_type'] = 'live';
18021 } else {
18022 $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
18023 }
18024 }
18025 return $context;
18026 }
18027
18028
18029 /**
18030 * Get Voxel Membership Last Data
18031 *
18032 * @param array $data data.
18033 *
18034 * @return array|mixed|string
18035 */
18036 public function search_voxel_membership_triggers_last_data( $data ) {
18037
18038 global $wpdb;
18039 $context = [];
18040 $term = $data['search_term'];
18041 if ( ! class_exists( 'Voxel\Stripe' ) ) {
18042 return [];
18043 }
18044 if ( 'plan_activated' === $term || 'plan_canceled' === $term ) {
18045 $meta_key = \Voxel\Stripe::is_test_mode() ? 'voxel:test_plan' : 'voxel:plan';
18046 $sql = "SELECT
18047 m.user_id AS id,
18048 u.user_login AS title,
18049 u.user_email AS email,
18050 m.meta_value AS details,
18051 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) AS plan,
18052 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.amount' ) ) AS SIGNED ) AS amount,
18053 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.status' ) ) AS status,
18054 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.created' ) ) AS DATETIME ) AS created
18055 FROM {$wpdb->prefix}usermeta as m
18056 LEFT JOIN {$wpdb->prefix}users AS u ON m.user_id = u.ID
18057 WHERE m.meta_key = %s AND JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) != 'default'
18058 ORDER BY m.user_id DESC
18059 LIMIT 25 OFFSET 0";
18060 $results = $wpdb->get_results( $wpdb->prepare( $sql, $meta_key ), ARRAY_A );// @phpcs:ignore
18061 } elseif ( 'user_registered' === $term ) {
18062 $sql = "SELECT DISTINCT u.ID
18063 FROM {$wpdb->prefix}users u
18064 JOIN {$wpdb->prefix}usermeta um ON u.ID = um.user_id
18065 WHERE um.meta_key = 'voxel:profile_id' ORDER BY ID DESC LIMIT 1";
18066 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18067 }
18068
18069 if ( 'plan_canceled' === $term ) {
18070 if ( ! empty( $results ) ) {
18071 if ( 'cancelled' == $results[0]['status'] ) {
18072 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
18073 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
18074 $context['response_type'] = 'live';
18075 } else {
18076 $context = json_decode( '{"pluggable_data":{"wp_user_id": 101,"user_login": "benni","display_name": "John D","user_firstname": "johnd","user_lastname": "D","user_email": "johnd@gmail.com","user_role": ["subscriber"],"details": {"plan": "learningmembership","type": "subscription","subscription_id": "sub_1OwOMySHDFghoeM1sInxPrG7","price_id": "price_1OwOLJSHDFghoeM177Vf8kgt","status": "cancelled","trial_end": null,"current_period_end": 1711542948,"cancel_at_period_end": true,"amount": 800,"currency": "usd","interval": "week","interval_count": 1,"created": "2024-03-20 12:35:48","metadata": {"voxel:payment_for": "membership","voxel:plan": "learningmembership"}}},"response_type":"sample"}', true );// @phpcs:ignore
18077 }
18078 } else {
18079 $context = json_decode( '{"pluggable_data":{"wp_user_id": 101,"user_login": "benni","display_name": "John D","user_firstname": "johnd","user_lastname": "D","user_email": "johnd@gmail.com","user_role": ["subscriber"],"details": {"plan": "learningmembership","type": "subscription","subscription_id": "sub_1OwOMySHDFghoeM1sInxPrG7","price_id": "price_1OwOLJSHDFghoeM177Vf8kgt","status": "cancelled","trial_end": null,"current_period_end": 1711542948,"cancel_at_period_end": true,"amount": 800,"currency": "usd","interval": "week","interval_count": 1,"created": "2024-03-20 12:35:48","metadata": {"voxel:payment_for": "membership","voxel:plan": "learningmembership"}}},"response_type":"sample"}', true );// @phpcs:ignore
18080 }
18081 } elseif ( 'plan_activated' === $term ) {
18082 if ( ! empty( $results ) ) {
18083 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
18084 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
18085 $context['response_type'] = 'live';
18086 } else {
18087 $context = json_decode( '{"pluggable_data":{"wp_user_id": 101,"user_login": "benni","display_name": "John D","user_firstname": "johnd","user_lastname": "D","user_email": "johnd@gmail.com","user_role": ["subscriber"],"details": {"plan": "learningmembership","type": "subscription","subscription_id": "sub_1OwOMySHDFghoeM1sInxPrG7","price_id": "price_1OwOLJSHDFghoeM177Vf8kgt","status": "active","trial_end": null,"current_period_end": 1711542948,"cancel_at_period_end": true,"amount": 800,"currency": "usd","interval": "week","interval_count": 1,"created": "2024-03-20 12:35:48","metadata": {"voxel:payment_for": "membership","voxel:plan": "learningmembership"}}},"response_type":"sample"}', true );// @phpcs:ignore
18088 }
18089 } elseif ( 'user_registered' === $term ) {
18090 if ( ! empty( $results ) ) {
18091 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['ID'] );
18092 $context['response_type'] = 'live';
18093 } else {
18094 $context['pluggable_data'] = WordPress::get_sample_user_context();
18095 $context['response_type'] = 'sample';
18096 }
18097 }
18098 return $context;
18099 }
18100
18101 /**
18102 * Get Voxel Orders Last Data
18103 *
18104 * @param array $data data.
18105 *
18106 * @return array|mixed|string
18107 */
18108 public function search_voxel_order_triggers_last_data( $data ) {
18109
18110 global $wpdb;
18111 $context = [];
18112 $term = $data['search_term'];
18113
18114 if ( ! class_exists( 'Voxel\Product_Types\Orders\Order' ) ) {
18115 return [];
18116 }
18117
18118 if ( 'order_placed' === $term || 'order_canceled' === $term ) {
18119 if ( 'order_placed' === $term ) {
18120 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders ORDER BY id DESC LIMIT 1";
18121 } elseif ( 'order_canceled' === $term ) {
18122 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' ORDER BY id DESC LIMIT 1";
18123 }
18124
18125 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18126 if ( ! empty( $results ) ) {
18127 // Get Order.
18128 $context['pluggable_data']['id'] = $results[0]['id'];
18129 $context['pluggable_data']['vendor_id'] = $results[0]['vendor_id'];
18130 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
18131 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
18132 $context['pluggable_data']['status'] = $results[0]['status'];
18133 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
18134 $context['pluggable_data']['subtotal'] = $results[0]['subtotal'];
18135 $context['pluggable_data']['total'] = $results[0]['total'];
18136 // Get order items.
18137 $order = \Voxel\Product_Types\Orders\Order::find(
18138 [
18139 'id' => $results[0]['id'],
18140 ]
18141 );
18142 $order_items = $order->get_items();
18143 $context['pluggable_data']['tax_amount'] = $order->get_tax_amount();
18144 $context['pluggable_data']['discount_amount'] = $order->get_discount_amount();
18145 $context['pluggable_data']['shipping_amount'] = $order->get_shipping_amount();
18146 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
18147 foreach ( $order_items as $item ) {
18148 $addon_data = [];
18149 if ( is_object( $item ) && method_exists( $item, 'get_addons' ) ) {
18150 // Get addon details.
18151 $addons = $item->get_addons();
18152 $addon_data = [];
18153
18154 // Add the addons to the simple entry.
18155 if ( $addons && isset( $addons['summary'] ) ) {
18156 $addon_data = $addons['summary'];
18157 }
18158 }
18159 $context['pluggable_data']['order_items'][] = [
18160 'id' => $item->get_id(),
18161 'type' => $item->get_type(),
18162 'currency' => $item->get_currency(),
18163 'quantity' => $item->get_quantity(),
18164 'subtotal' => $item->get_subtotal(),
18165 'product_id' => $item->get_post()->get_id(),
18166 'product_label' => $item->get_product_label(),
18167 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
18168 'product_link' => $item->get_product_link(),
18169 'description' => $item->get_product_description(),
18170 'addon_data' => $addon_data,
18171 ];
18172 // If booking item, get booking details.
18173 if ( 'booking' === $item->get_type() ) {
18174 $details = $item->get_order_page_details();
18175 $context['pluggable_data']['order_items']['booking_type'] = $details['booking']['type'];
18176 if ( isset( $details['booking']['count_mode'] ) ) {
18177 $context['pluggable_data']['order_items']['booking_count_mode'] = $details['booking']['count_mode'];
18178 }
18179 if ( 'date_range' === $details['booking']['type'] ) {
18180 $context['pluggable_data']['order_items']['booking_start_date'] = $details['booking']['start_date'];
18181 $context['pluggable_data']['order_items']['booking_end_date'] = $details['booking']['end_date'];
18182 } elseif ( 'single_day' === $details['booking']['type'] ) {
18183 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
18184 } elseif ( 'timeslots' === $details['booking']['type'] ) {
18185 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
18186 $context['pluggable_data']['order_items']['booking_slot_from'] = $details['booking']['slot']['from'];
18187 $context['pluggable_data']['order_items']['booking_slot_to'] = $details['booking']['slot']['to'];
18188 }
18189 }
18190 }
18191 // Get Customer.
18192 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
18193 $context['response_type'] = 'live';
18194 } else {
18195 $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
18196 }
18197 } elseif ( 'order_approved' === $term || 'order_declined' === $term ) {
18198 if ( 'order_approved' === $term ) {
18199 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'completed' ORDER BY id DESC LIMIT 1";
18200 } elseif ( 'order_declined' === $term ) {
18201 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' ORDER BY id DESC LIMIT 1";
18202 }
18203 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18204 if ( ! empty( $results ) ) {
18205 // Get Order.
18206 $context['pluggable_data']['id'] = $results[0]['id'];
18207 $context['pluggable_data']['vendor_id'] = $results[0]['vendor_id'];
18208 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
18209 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
18210 $context['pluggable_data']['status'] = $results[0]['status'];
18211 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
18212 $context['pluggable_data']['subtotal'] = $results[0]['subtotal'];
18213 $context['pluggable_data']['total'] = $results[0]['total'];
18214 // Get order items.
18215 $order = \Voxel\Product_Types\Orders\Order::find(
18216 [
18217 'id' => $results[0]['id'],
18218 ]
18219 );
18220 $order_items = $order->get_items();
18221 $context['pluggable_data']['tax_amount'] = $order->get_tax_amount();
18222 $context['pluggable_data']['discount_amount'] = $order->get_discount_amount();
18223 $context['pluggable_data']['shipping_amount'] = $order->get_shipping_amount();
18224 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
18225 foreach ( $order_items as $item ) {
18226 $addon_data = [];
18227 if ( is_object( $item ) && method_exists( $item, 'get_addons' ) ) {
18228 // Get addon details.
18229 $addons = $item->get_addons();
18230 $addon_data = [];
18231
18232 // Add the addons to the simple entry.
18233 if ( $addons && isset( $addons['summary'] ) ) {
18234 $addon_data = $addons['summary'];
18235 }
18236 }
18237 $context['pluggable_data']['order_items'][] = [
18238 'id' => $item->get_id(),
18239 'type' => $item->get_type(),
18240 'currency' => $item->get_currency(),
18241 'quantity' => $item->get_quantity(),
18242 'subtotal' => $item->get_subtotal(),
18243 'product_id' => $item->get_post()->get_id(),
18244 'product_label' => $item->get_product_label(),
18245 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
18246 'product_link' => $item->get_product_link(),
18247 'description' => $item->get_product_description(),
18248 'addon_data' => $addon_data,
18249 ];
18250 // If booking item, get booking details.
18251 if ( 'booking' === $item->get_type() ) {
18252 $details = $item->get_order_page_details();
18253 $context['pluggable_data']['order_items']['booking_type'] = $details['booking']['type'];
18254 if ( isset( $details['booking']['count_mode'] ) ) {
18255 $context['pluggable_data']['order_items']['booking_count_mode'] = $details['booking']['count_mode'];
18256 }
18257 if ( 'date_range' === $details['booking']['type'] ) {
18258 $context['pluggable_data']['order_items']['booking_start_date'] = $details['booking']['start_date'];
18259 $context['pluggable_data']['order_items']['booking_end_date'] = $details['booking']['end_date'];
18260 } elseif ( 'single_day' === $details['booking']['type'] ) {
18261 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
18262 } elseif ( 'timeslots' === $details['booking']['type'] ) {
18263 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
18264 $context['pluggable_data']['order_items']['booking_slot_from'] = $details['booking']['slot']['from'];
18265 $context['pluggable_data']['order_items']['booking_slot_to'] = $details['booking']['slot']['to'];
18266 }
18267 }
18268 }
18269 // Get Customer.
18270 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
18271 // Get Vendor.
18272 $context['pluggable_data']['vendor'] = WordPress::get_user_context( $results[0]['vendor_id'] );
18273 $context['response_type'] = 'live';
18274 } else {
18275 $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
18276 }
18277 } elseif ( 'order_promotion_activated' === $term || 'order_promotion_canceled' === $term || 'order_claim_listing' === $term ) {
18278 if ( 'order_promotion_activated' === $term ) {
18279 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'completed' AND details LIKE '%voxel:promotion%' ORDER BY id DESC LIMIT 1";
18280 } elseif ( 'order_promotion_canceled' === $term ) {
18281 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' AND details LIKE '%voxel:promotion%' ORDER BY id DESC LIMIT 1";
18282 } elseif ( 'order_claim_listing' === $term ) {
18283 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE details LIKE '%voxel:claim%' ORDER BY id DESC LIMIT 1";
18284 }
18285 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18286 if ( ! empty( $results ) ) {
18287 // Get Order.
18288 $context['pluggable_data']['id'] = $results[0]['id'];
18289 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
18290 $context['pluggable_data']['status'] = $results[0]['status'];
18291 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
18292 // Get order items.
18293 $order = \Voxel\Product_Types\Orders\Order::find(
18294 [
18295 'id' => $results[0]['id'],
18296 ]
18297 );
18298 $order_items = $order->get_items();
18299 $context['pluggable_data']['details'] = $order->get_details();
18300 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
18301 foreach ( $order_items as $item ) {
18302 $context['pluggable_data']['order_items'][] = [
18303 'id' => $item->get_id(),
18304 'type' => $item->get_type(),
18305 'currency' => $item->get_currency(),
18306 'quantity' => $item->get_quantity(),
18307 'subtotal' => $item->get_subtotal(),
18308 'product_id' => $item->get_post()->get_id(),
18309 'product_label' => $item->get_product_label(),
18310 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
18311 'product_link' => $item->get_product_link(),
18312 'description' => $item->get_product_description(),
18313 ];
18314 }
18315 // Get Customer.
18316 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
18317 $context['response_type'] = 'live';
18318 } else {
18319 $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
18320 }
18321 }
18322 return $context;
18323 }
18324
18325 /**
18326 * Get Voxel Timeline Comments Last Data
18327 *
18328 * @param array $data data.
18329 *
18330 * @return array|mixed|string
18331 */
18332 public function search_voxel_timeline_triggers_last_data( $data ) {
18333
18334 global $wpdb;
18335 $context = [];
18336 $term = $data['search_term'];
18337 $sql = '';
18338 if ( 'new_comment_timeline' === $term ) {
18339 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE parent_id IS NULL ORDER BY id DESC LIMIT 1";
18340 } elseif ( 'comment_reply_timeline' === $term ) {
18341 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE parent_id IS NOT NULL ORDER BY id DESC LIMIT 1";
18342 } elseif ( 'comment_liked' === $term ) {
18343 $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";
18344 } elseif ( 'user_timeline_post_liked' === $term ) {
18345 $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";
18346 } elseif ( 'user_timeline_post_quoted' === $term ) {
18347 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline WHERE feed = %s AND quote_of IS NOT NULL ORDER BY id DESC LIMIT 1";
18348 } elseif ( 'user_timeline_post_reposted' === $term ) {
18349 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline WHERE feed = %s AND repost_of IS NOT NULL ORDER BY id DESC LIMIT 1";
18350 }
18351 if ( 'user_timeline_post_reposted' === $term || 'user_timeline_post_quoted' === $term ) {
18352 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user_timeline' ), ARRAY_A );// @phpcs:ignore
18353 } else {
18354 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18355 }
18356 if ( 'new_comment_timeline' === $term ) {
18357 if ( ! empty( $results ) ) {
18358 $context['pluggable_data']['comment_by'] = WordPress::get_user_context( $results[0]['user_id'] );
18359 $context['pluggable_data']['id'] = $results[0]['id'];
18360 $context['pluggable_data']['status_id'] = $results[0]['status_id'];
18361 $context['pluggable_data']['content'] = $results[0]['content'];
18362 $context['response_type'] = 'live';
18363 } else {
18364 $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
18365 }
18366 } elseif ( 'comment_reply_timeline' === $term ) {
18367 if ( ! empty( $results ) ) {
18368 $comment_sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE id = %d";
18369 $comment_result = $wpdb->get_results( $wpdb->prepare( $comment_sql, $results[0]['parent_id']), ARRAY_A );// @phpcs:ignore
18370 $context['pluggable_data']['replied_by'] = WordPress::get_user_context( $results[0]['user_id'] );
18371 $context['pluggable_data']['comment_by'] = WordPress::get_user_context( $comment_result[0]['user_id'] );
18372 $context['pluggable_data']['comment'] = $comment_result[0]['content'];
18373 $context['pluggable_data']['comment_id'] = $results[0]['parent_id'];
18374 $context['pluggable_data']['reply_id'] = $results[0]['id'];
18375 $context['pluggable_data']['reply'] = $results[0]['content'];
18376 $context['response_type'] = 'live';
18377 } else {
18378 $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
18379 }
18380 } elseif ( 'comment_liked' === $term ) {
18381 if ( ! empty( $results ) ) {
18382 $user = get_userdata( $results[0]['user_id'] );
18383 if ( $user ) {
18384 $user_data = (array) $user->data;
18385 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
18386 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
18387 $context['pluggable_data']['user_email'] = $user_data['user_email'];
18388 }
18389 $recipient_user = get_userdata( $results[0]['like_user_id'] );
18390 if ( $recipient_user ) {
18391 $recipient_user_data = (array) $recipient_user->data;
18392 $context['pluggable_data']['recipient']['user_display_name'] = $recipient_user_data['display_name'];
18393 $context['pluggable_data']['recipient']['user_name'] = $recipient_user_data['user_nicename'];
18394 $context['pluggable_data']['recipient']['user_email'] = $recipient_user_data['user_email'];
18395 }
18396 if ( class_exists( 'Voxel\Timeline\Reply' ) ) {
18397 $reply_details = \Voxel\Timeline\Reply::get( $results[0]['id'] );
18398 foreach ( (array) $reply_details as $key => $value ) {
18399 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
18400 $context['pluggable_data'][ $clean_key ] = $value;
18401 }
18402 }
18403 $context['response_type'] = 'live';
18404 } else {
18405 $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
18406 }
18407 } elseif ( 'user_timeline_post_liked' === $term ) {
18408 if ( ! empty( $results ) ) {
18409 $user = get_userdata( $results[0]['user_id'] );
18410 if ( $user ) {
18411 $user_data = (array) $user->data;
18412 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
18413 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
18414 $context['pluggable_data']['user_email'] = $user_data['user_email'];
18415 }
18416 $recipient_user = get_userdata( $results[0]['like_user_id'] );
18417 if ( $recipient_user ) {
18418 $recipient_user_data = (array) $recipient_user->data;
18419 $context['pluggable_data']['recipient']['user_display_name'] = $recipient_user_data['display_name'];
18420 $context['pluggable_data']['recipient']['user_name'] = $recipient_user_data['user_nicename'];
18421 $context['pluggable_data']['recipient']['user_email'] = $recipient_user_data['user_email'];
18422 }
18423 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
18424 $reply_details = \Voxel\Timeline\Status::get( $results[0]['id'] );
18425 foreach ( (array) $reply_details as $key => $value ) {
18426 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
18427 $context['pluggable_data'][ $clean_key ] = $value;
18428 }
18429 }
18430 $context['response_type'] = 'live';
18431 } else {
18432 $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
18433 }
18434 } elseif ( 'user_timeline_post_reposted' === $term || 'user_timeline_post_quoted' === $term ) {
18435 if ( ! empty( $results ) ) {
18436 $author = get_userdata( $results[0]['user_id'] );
18437 if ( $author ) {
18438 $author_data = (array) $author->data;
18439 $context['pluggable_data']['author_display_name'] = $author_data['display_name'];
18440 $context['pluggable_data']['author_name'] = $author_data['user_nicename'];
18441 $context['pluggable_data']['author_email'] = $author_data['user_email'];
18442 }
18443 $column = 'user_timeline_post_reposted' === $term ? 'repost_of' : 'quote_of';
18444 $recipient_sql = "SELECT user_id FROM {$wpdb->prefix}voxel_timeline WHERE id = %d";
18445 $recipient_results = $wpdb->get_results( $wpdb->prepare( $recipient_sql, $results[0][$column] ), ARRAY_A ); // @phpcs:ignore
18446 if ( ! empty( $recipient_results ) ) {
18447 $recipient_user = get_userdata( $recipient_results[0]['user_id'] );
18448 if ( $recipient_user ) {
18449 $recipient_user_data = (array) $recipient_user->data;
18450 $context['pluggable_data']['recipient']['user_display_name'] = $recipient_user_data['display_name'];
18451 $context['pluggable_data']['recipient']['user_name'] = $recipient_user_data['user_nicename'];
18452 $context['pluggable_data']['recipient']['user_email'] = $recipient_user_data['user_email'];
18453 }
18454 }
18455 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
18456 $status_details = \Voxel\Timeline\Status::get( $results[0]['id'] );
18457 $details_column = 'user_timeline_post_reposted' === $term ? 'repost_of' : 'quote_of';
18458 $related_details = \Voxel\Timeline\Status::get( $results[0][ $details_column ] );
18459 foreach ( [
18460 'status' => $status_details,
18461 $details_column => $related_details,
18462 ] as $key => $details ) {
18463 foreach ( (array) $details as $sub_key => $value ) {
18464 $clean_key = preg_replace( '/^\0.*?\0/', '', $sub_key );
18465 $context['pluggable_data'][ $key ][ $clean_key ] = $value;
18466 }
18467 }
18468 }
18469 $context['response_type'] = 'live';
18470 } else {
18471 if ( 'user_timeline_post_reposted' === $term ) {
18472 $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
18473 } else {
18474 $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
18475 }
18476 }
18477 }
18478 return $context;
18479 }
18480
18481 /**
18482 * Get Voxel Timeline Comments Last Data
18483 *
18484 * @param array $data data.
18485 *
18486 * @return array|mixed|string
18487 */
18488 public function search_voxel_posts_triggers_last_data( $data ) {
18489 $context = [];
18490 global $wpdb;
18491 $post_type = 'post';
18492 $term = $data['search_term'];
18493 if ( isset( $data['filter']['post_type']['value'] ) ) {
18494 $post_type = $data['filter']['post_type']['value'];
18495 }
18496 $results = [];
18497 if ( 'post_approved' === $term ) {
18498 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
18499 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type, 'publish'), ARRAY_A );// @phpcs:ignore
18500 } elseif ( 'post_rejected' === $term ) {
18501 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
18502 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type, 'rejected'), ARRAY_A );// @phpcs:ignore
18503 } elseif ( 'post_reviewed' === $term ) {
18504 $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
18505 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";
18506 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type, '%rating%' ), ARRAY_A );// @phpcs:ignore
18507 } elseif ( 'post_review_approved' === $term ) {
18508 $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
18509 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";
18510 $results = $wpdb->get_results( $wpdb->prepare( $sql, 1, $post_type, '%rating%', 'post_reviews' ), ARRAY_A );// @phpcs:ignore
18511 } elseif ( 'post_submitted' === $term ) {
18512 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
18513 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type), ARRAY_A );// @phpcs:ignore
18514 } elseif ( 'post_updated' === $term ) {
18515 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
18516 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type ), ARRAY_A );// @phpcs:ignore
18517 } elseif ( 'collection_post_submitted' === $term ) {
18518 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
18519 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'collection' ), ARRAY_A );// @phpcs:ignore
18520 } elseif ( 'collection_post_updated' === $term ) {
18521 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
18522 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'collection' ), ARRAY_A );// @phpcs:ignore
18523 } elseif ( 'new_wall_post_by_user' === $term ) {
18524 $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";
18525 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type, '%rating%' ), ARRAY_A );// @phpcs:ignore
18526 } elseif ( 'wall_post_approved' === $term ) {
18527 $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";
18528 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'post_wall', $post_type, 1 ), ARRAY_A );// @phpcs:ignore
18529 } elseif ( 'timeline_post_approved' === $term ) {
18530 $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";
18531 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'post_timeline', 1, $post_type ), ARRAY_A );// @phpcs:ignore
18532 } elseif ( 'post_followed' === $term ) {
18533 $sql = "SELECT * FROM {$wpdb->prefix}voxel_followers WHERE object_type = %s LIMIT 1";
18534 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'post' ), ARRAY_A );// @phpcs:ignore
18535 }
18536 if ( 'post_approved' === $term || 'post_rejected' === $term || 'post_submitted' === $term || 'post_updated' === $term || 'timeline_post_approved' === $term ) {
18537 if ( ! empty( $results ) ) {
18538 $context['pluggable_data'] = WordPress::get_post_context( $results[0]['ID'] );
18539 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['ID'] );
18540 $context['response_type'] = 'live';
18541 } else {
18542 $context['pluggable_data'] = [
18543 'ID' => 557,
18544 'post' => 557,
18545 'post_author' => 1,
18546 'post_date' => '2022-11-18 12:18:14',
18547 'post_date_gmt' => '2022-11-18 12:18:14',
18548 'post_content' => 'Test Post Content',
18549 'post_title' => 'Test Post',
18550 'post_excerpt' => '',
18551 'post_status' => 'published',
18552 'comment_status' => 'open',
18553 'ping_status' => 'open',
18554 'post_password' => '',
18555 'post_name' => 'test-post',
18556 'to_ping' => '',
18557 'pinged' => '',
18558 'post_modified' => '2022-11-18 12:18:14',
18559 'post_modified_gmt' => '2022-11-18 12:18:14',
18560 'post_content_filtered' => '',
18561 'post_parent' => 0,
18562 'guid' => 'https://example.com/test-post/',
18563 'menu_order' => 0,
18564 'post_type' => 'post',
18565 'post_mime_type' => '',
18566 'comment_count' => 0,
18567 'filter' => 'raw',
18568 ];
18569 $context['response_type'] = 'sample';
18570 }
18571 } elseif ( 'post_reviewed' === $term || 'post_review_approved' === $term ) {
18572 if ( ! empty( $results ) ) {
18573 $context['pluggable_data'] = WordPress::get_post_context( $results[0]['post_id'] );
18574 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['post_id'] );
18575 $context['pluggable_data']['review_content'] = $results[0]['content'];
18576 $context['pluggable_data']['review_created_at'] = $results[0]['created_at'];
18577 $context['pluggable_data']['review_details'] = json_decode( $results[0]['details'], true );
18578 $context['pluggable_data']['review_by'] = WordPress::get_user_context( $results[0]['user_id'] );
18579 $context['response_type'] = 'live';
18580 } else {
18581 $context = json_decode( '{"pluggable_data":{"ID": 8291,"post_author": "1","post_date": "2024-03-18 09:01:54","post_date_gmt": "2024-03-18 09:01:54","post_content": "<p>PizzaCrust - Since 2009! Whether it\u2019s our iconic Sandwiches, wooden baked pizzas, signature sauce, original fresh dough or toppings, we invest in bringing the freshest and best ingredients to bring you the tastiest meal.<\/p>","post_title": "Pizza Crust","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "closed","post_password": "","post_name": "sach-pizza","to_ping": "","pinged": "","post_modified": "2024-03-18 09:01:54","post_modified_gmt": "2024-03-18 09:01:54","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/suretriggers-wpnew.local\/places\/sach-pizza\/","menu_order": 0,"post_type": "places","post_mime_type": "","comment_count": "0","filter": "raw","review_content": "Nice one","review_created_at": "2024-04-04 12:59:22","review_details": {"rating": {"score": -1,"custom-660": -1,"custom-978": -1,"custom-271": -1}},"review_by": {"wp_user_id": 188,"user_login": "dev4","display_name": "dev4","user_firstname": "","user_lastname": "","user_email": "dev4@yopmail.com","user_role": ["subscriber"]}},"response_type":"sample"}', true );// @phpcs:ignore
18582 }
18583 } elseif ( 'collection_post_submitted' === $term || 'collection_post_updated' === $term ) {
18584 if ( ! empty( $results ) ) {
18585 $context['pluggable_data'] = Voxel::get_post_fields( $results[0]['ID'] );
18586 $context['pluggable_data']['collection'] = WordPress::get_post_context( $results[0]['ID'] );
18587 $context['response_type'] = 'live';
18588 } else {
18589 $context = json_decode(
18590 '{"pluggable_data":{"field_step-general": null,"field_title": "First Collection","field_items": [8909,8293],"collection": {"ID": 9142,"post_author": "35",
18591 "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
18592 }
18593 } elseif ( 'new_wall_post_by_user' === $term || 'wall_post_approved' === $term ) {
18594 if ( ! empty( $results ) ) {
18595 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['post_id'] );
18596 $user = get_userdata( $results[0]['user_id'] );
18597 if ( $user ) {
18598 $user_data = (array) $user->data;
18599 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
18600 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
18601 $context['pluggable_data']['user_email'] = $user_data['user_email'];
18602 $context['pluggable_data']['user_id'] = $results[0]['user_id'];
18603 }
18604 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
18605 $args = [
18606 'post_id' => $results[0]['post_id'],
18607 'order_by' => 'created_at',
18608 'order' => 'desc',
18609 'feed' => $results[0]['feed'],
18610 'limit' => 1,
18611 'moderation' => $results[0]['moderation'],
18612 ];
18613 $statuses = \Voxel\Timeline\Status::query( $args );
18614 $status_details = $statuses['items'][0];
18615 foreach ( (array) $status_details as $key => $value ) {
18616 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
18617 if ( is_object( $value ) ) {
18618 $encoded_value = wp_json_encode( $value );
18619 if ( is_string( $encoded_value ) ) {
18620 $value = json_decode( $encoded_value, true );
18621 }
18622 }
18623 $context['pluggable_data']['wall_post'][ $clean_key ] = $value;
18624 }
18625 }
18626 $context['response_type'] = 'live';
18627 } else {
18628 $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
18629 }
18630 } elseif ( 'post_followed' === $term ) {
18631 if ( ! empty( $results ) ) {
18632 $follow_data = [
18633 'post_id' => $results[0]['object_id'],
18634 'user_id' => $results[0]['follower_id'],
18635 'status' => $results[0]['status'],
18636 ];
18637 $follow_data = array_merge( $follow_data, Voxel::get_post_fields( $results[0]['object_id'] ), WordPress::get_post_context( $results[0]['object_id'] ) );
18638 $followers_sql = "SELECT COUNT(*) FROM {$wpdb->prefix}voxel_followers WHERE object_id= %d";
18639 $followers = $wpdb->get_var( $wpdb->prepare( $followers_sql, $results[0]['object_id'] ));// @phpcs:ignore
18640 $follow_data['total_followers'] = $followers;
18641 $context['pluggable_data'] = $follow_data;
18642 $context['response_type'] = 'live';
18643 } else {
18644 $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:\/\/suretriggers-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
18645 }
18646 }
18647 return $context;
18648 }
18649
18650 /**
18651 * Get Voxel Profiles Last Data
18652 *
18653 * @param array $data data.
18654 *
18655 * @return array|mixed|string
18656 */
18657 public function search_voxel_profiles_triggers_last_data( $data ) {
18658
18659 global $wpdb;
18660 $context = [];
18661 $term = $data['search_term'];
18662
18663 if ( ! class_exists( 'Voxel\Timeline\Status' ) || ! class_exists( 'Voxel\Post_Type' ) ) {
18664 return [];
18665 }
18666
18667 if ( 'profile_created' === $term ) {
18668 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
18669 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
18670 } elseif ( 'profile_approved' === $term ) {
18671 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
18672 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile', 'publish' ), ARRAY_A );// @phpcs:ignore
18673 } elseif ( 'profile_rejected' === $term ) {
18674 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
18675 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile', 'rejected' ), ARRAY_A );// @phpcs:ignore
18676 } elseif ( 'profile_reviewed' === $term ) {
18677 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline vt LEFT JOIN {$wpdb->prefix}posts pm
18678 ON vt.post_id = pm.ID
18679 WHERE pm.post_type LIKE %s AND vt.details IS NOT NULL ORDER BY vt.id DESC LIMIT 1";
18680 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
18681 } elseif ( 'profile_updated' === $term ) {
18682 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
18683 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
18684 } elseif ( 'profile_new_wall_post' === $term ) {
18685 $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";
18686 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
18687 }
18688 if ( 'profile_created' === $term || 'profile_approved' === $term || 'profile_rejected' === $term || 'profile_updated' === $term ) {
18689 if ( ! empty( $results ) ) {
18690 $context['pluggable_data'] = Voxel::get_post_fields( $results[0]['ID'] );
18691 $user = get_userdata( $results[0]['post_author'] );
18692 if ( $user ) {
18693 $user_data = (array) $user->data;
18694 $context['pluggable_data']['profile_display_name'] = $user_data['display_name'];
18695 $context['pluggable_data']['profile_email'] = $user_data['user_email'];
18696 $context['pluggable_data']['profile_user_id'] = $results[0]['post_author'];
18697 $context['pluggable_data']['profile_id'] = $results[0]['ID'];
18698 }
18699 $context['response_type'] = 'live';
18700 } else {
18701 $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
18702 }
18703 } elseif ( 'profile_new_wall_post' === $term ) {
18704 if ( ! empty( $results ) ) {
18705 $context['pluggable_data']['profile'] = Voxel::get_post_fields( $results[0]['post_id'] );
18706 $user = get_userdata( $results[0]['user_id'] );
18707 if ( $user ) {
18708 $user_data = (array) $user->data;
18709 $context['pluggable_data']['profile_display_name'] = $user_data['display_name'];
18710 $context['pluggable_data']['profile_name'] = $user_data['user_nicename'];
18711 $context['pluggable_data']['profile_email'] = $user_data['user_email'];
18712 $context['pluggable_data']['profile_user_id'] = $results[0]['user_id'];
18713 }
18714 // Get the status details.
18715 $args = [
18716 'post_id' => $results[0]['post_id'],
18717 'order_by' => 'created_at',
18718 'order' => 'desc',
18719 'feed' => $results[0]['feed'],
18720 'limit' => 1,
18721 'moderation' => $results[0]['moderation'],
18722 ];
18723 $statuses = \Voxel\Timeline\Status::query( $args );
18724 $status_details = $statuses['items'][0];
18725 foreach ( (array) $status_details as $key => $value ) {
18726 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
18727 if ( is_object( $value ) ) {
18728 $encoded_value = wp_json_encode( $value );
18729 if ( is_string( $encoded_value ) ) {
18730 $value = json_decode( $encoded_value, true );
18731 }
18732 }
18733 $context['pluggable_data']['wall_post'][ $clean_key ] = $value;
18734 }
18735 $context['response_type'] = 'live';
18736 } else {
18737 $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
18738 }
18739 } elseif ( 'profile_reviewed' === $term ) {
18740 if ( ! empty( $results ) ) {
18741 // Get the review details.
18742 $args = [
18743 'post_id' => $results[0]['post_id'],
18744 'order_by' => 'created_at',
18745 'order' => 'desc',
18746 'feed' => $results[0]['feed'],
18747 'limit' => 1,
18748 'moderation' => $results[0]['moderation'],
18749 ];
18750 $statuses = \Voxel\Timeline\Status::query( $args );
18751 $review_details = $statuses['items'][0];
18752 foreach ( (array) $review_details as $key => $value ) {
18753 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
18754 if ( 'user_can_edit' == $clean_key || 'publisher' == $clean_key || 'user_can_edit' == $clean_key || 'user_can_moderate' == $clean_key ) {
18755 continue;
18756 }
18757 if ( 'files' === $clean_key ) {
18758 $value = wp_json_encode( $value );
18759 } elseif ( 'details' === $clean_key ) {
18760 $review_ratings = isset( $value['rating'] ) && is_array( $value['rating'] ) ? $value['rating'] : [];
18761 $value['rating'] = [];
18762 $type = \Voxel\Post_Type::get( 'profile' );
18763
18764 if ( ! empty( $review_ratings ) ) {
18765 $rating_levels = $type->reviews->get_rating_levels();
18766 $categories = $type->reviews->get_categories();
18767
18768 foreach ( $categories as $category ) {
18769 $category_key = $category['key'];
18770 $category_label = strtolower( $category['label'] );
18771
18772 if ( isset( $review_ratings[ $category_key ] ) && $category_label ) {
18773 foreach ( $rating_levels as $rating_level ) {
18774 if ( $review_ratings[ $category_key ] === $rating_level['score'] ) {
18775 $value['rating'][ $category_label ] = $rating_level['label'];
18776 break;
18777 }
18778 }
18779 }
18780 }
18781 }
18782 } else {
18783 $clean_key = 'review_' . $clean_key;
18784 }
18785 $context['pluggable_data'][ $clean_key ] = $value;
18786 }
18787 $context['response_type'] = 'live';
18788 } else {
18789 $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
18790 }
18791 }
18792 return $context;
18793 }
18794
18795
18796 /**
18797 * Get Late Point Customer Fields
18798 *
18799 * @param array $data data.
18800 *
18801 * @return array|void
18802 */
18803 public function search_late_point_customer_fields( $data ) {
18804 if ( ! class_exists( 'OsCustomFieldsController' ) ) {
18805
18806 return;
18807 }
18808 global $wpdb;
18809 $booking_fields = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}latepoint_settings WHERE name = %s", 'custom_fields_for_customer' ) );
18810 $fields = [];
18811 if ( ! empty( $booking_fields ) ) {
18812 $fields = json_decode( $booking_fields->value, true );
18813 }
18814 return (array) $fields;
18815 }
18816
18817 /**
18818 * Is multi person booking
18819 *
18820 * @param array $data data.
18821 *
18822 * @return array|void
18823 */
18824 public function search_late_point_multi_person_booking( $data ) {
18825 if ( ! class_exists( 'LatePointAddonGroupBookings' ) ) {
18826
18827 return;
18828 }
18829 $service_id = isset( $data['dynamic'] ) ? $data['dynamic'] : '-1';
18830
18831 if ( -1 == $service_id ) {
18832 return;
18833 }
18834 global $wpdb;
18835 $booking_details = $wpdb->get_row( $wpdb->prepare( "SELECT capacity_max,capacity_min FROM {$wpdb->prefix}latepoint_services WHERE id= %s", intval( $service_id ) ) );
18836 if ( ! empty( $booking_details ) ) {
18837 return (array) $booking_details;
18838 }
18839
18840 }
18841
18842 /**
18843 * Get Mail Mint lists
18844 *
18845 * @param array $data data.
18846 *
18847 * @return array
18848 */
18849 public function search_mail_mint_contact_lists( $data ) {
18850 global $wpdb;
18851
18852 $lists = $wpdb->get_results( $wpdb->prepare( 'SELECT title, id FROM ' . $wpdb->prefix . 'mint_contact_groups WHERE type = %s', 'lists' ) );
18853 $options = [];
18854
18855 if ( ! empty( $lists ) ) {
18856 foreach ( $lists as $list ) {
18857 $options[] = [
18858 'label' => $list->title,
18859 'value' => $list->id,
18860 ];
18861 }
18862 }
18863 return [
18864 'options' => $options,
18865 'hasMore' => false,
18866 ];
18867 }
18868
18869 /**
18870 * Get Mail Mint tags
18871 *
18872 * @param array $data data.
18873 *
18874 * @return array
18875 */
18876 public function search_mail_mint_contact_tags( $data ) {
18877 global $wpdb;
18878
18879 $lists = $wpdb->get_results( $wpdb->prepare( 'SELECT title, id FROM ' . $wpdb->prefix . 'mint_contact_groups WHERE type = %s ', 'tags' ) );
18880 $options = [];
18881
18882 if ( ! empty( $lists ) ) {
18883 foreach ( $lists as $list ) {
18884 $options[] = [
18885 'label' => $list->title,
18886 'value' => $list->id,
18887 ];
18888 }
18889 }
18890 return [
18891 'options' => $options,
18892 'hasMore' => false,
18893 ];
18894 }
18895
18896 /** Get Better Messages trigger Last Data
18897 *
18898 * @param array $data data.
18899 *
18900 * @return array|mixed|string
18901 */
18902 public function search_better_messages_triggers_data( $data ) {
18903 $context = [];
18904 global $wpdb;
18905 if ( ! function_exists( 'Better_Messages' ) ) {
18906 return [];
18907 }
18908 $sql = "SELECT * FROM {$wpdb->prefix}bm_message_messages ORDER BY id DESC LIMIT 1";
18909 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18910
18911 if ( ! empty( $results ) ) {
18912 $message = Better_Messages()->functions->get_message( $results[0]['id'] );
18913 if ( is_object( $message ) ) {
18914 $message = get_object_vars( $message );
18915 }
18916 $context['pluggable_data'] = $message;
18917 $context['pluggable_data']['sender'] = WordPress::get_user_context( $results[0]['sender_id'] );
18918 $context['response_type'] = 'live';
18919 } else {
18920 $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
18921 }
18922 return $context;
18923 }
18924
18925 /**
18926 * Get Appointment Hour Booking trigger Last Data
18927 *
18928 * @param array $data data.
18929 *
18930 * @return array|mixed|string
18931 */
18932 public function search_ahb_appointment_booked_triggers_last_data( $data ) {
18933 $context = [];
18934 global $wpdb;
18935 $sql = "SELECT * FROM {$wpdb->prefix}cpappbk_messages ORDER BY id DESC LIMIT 1";
18936 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18937
18938 if ( ! empty( $results ) ) {
18939 $context['pluggable_data'] = unserialize( $results[0]['posted_data'] );
18940 $context['response_type'] = 'live';
18941 } else {
18942 $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
18943 }
18944 return $context;
18945 }
18946
18947 /**
18948 * Prepare mailmint contact status.
18949 *
18950 * @param array $data Search Params.
18951 *
18952 * @return array
18953 */
18954 public function search_mail_mint_fetch_custom_fields( $data ) {
18955
18956 $options = [
18957 [
18958 'label' => 'Yes',
18959 'value' => 'true',
18960 ],
18961 [
18962 'label' => 'No',
18963 'value' => 'false',
18964 ],
18965 ];
18966
18967 return [
18968 'options' => $options,
18969 'hasMore' => false,
18970 ];
18971 }
18972
18973 /**
18974 * Get Mail Mint custom fields
18975 *
18976 * @param array $data data.
18977 *
18978 * @return array
18979 */
18980 public function search_mail_mint_custom_fields( $data ) {
18981 global $wpdb;
18982 $fields = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'mint_custom_fields' );
18983
18984 return (array) $fields;
18985
18986 }
18987
18988 /**
18989 * Get Mail Mint lists
18990 *
18991 * @param array $data data.
18992 *
18993 * @return array|void
18994 */
18995 public function search_mail_mint_contacts( $data ) {
18996 if ( ! class_exists( 'Mint\MRM\DataBase\Models\ContactModel' ) ) {
18997 return [];
18998 }
18999 $contacts = ContactModel::get_all();
19000 $options = [];
19001 if ( ! empty( $contacts ) ) {
19002 foreach ( $contacts['data'] as $contact ) {
19003 $options[] = [
19004 'label' => $contact['email'],
19005 'value' => $contact['id'],
19006 ];
19007 }
19008 }
19009 return [
19010 'options' => $options,
19011 'hasMore' => false,
19012 ];
19013 }
19014
19015 /**
19016 * Get mail mint Last Data
19017 *
19018 * @param array $data data.
19019 *
19020 * @return array
19021 */
19022 public function search_mail_mint_last_data( $data ) {
19023 if ( ! class_exists( 'Mint\MRM\DataBase\Models\ContactModel' ) || ! class_exists( 'Mint\MRM\DataBase\Models\ContactGroupModel' ) ) {
19024 return [];
19025 }
19026 $context = [];
19027 global $wpdb;
19028 $contact_id = isset( $data['filter']['contact_id']['value'] ) ? $data['filter']['contact_id']['value'] : -1;
19029 $term = $data['search_term'] ? $data['search_term'] : '';
19030 $type = 'lists';
19031 if ( 'mail_mint_tags_added_to_contact' === $term ) {
19032 $type = 'tags';
19033 }
19034 $data = [];
19035 if ( -1 == $contact_id ) {
19036 $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 ) );
19037 } else {
19038 $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 ) );
19039 }
19040
19041 if ( ! empty( $result ) ) {
19042 $contact_id = $result->contact_id;
19043 $data['contact'] = ContactModel::get( $contact_id );
19044 $data[ $type ] = ContactGroupModel::get( $result->id );
19045 }
19046 if ( ! empty( $data ) ) {
19047 $context['pluggable_data'] = $data;
19048 $context['response_type'] = 'live';
19049 } else {
19050 $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 );
19051 }
19052 return (array) $context;
19053 }
19054
19055
19056 /**
19057 * Get Simple Schedule Appointment Types
19058 *
19059 * @param array $data data.
19060 *
19061 * @return array
19062 */
19063 public function search_simple_schedule_appointment_types( $data ) {
19064 global $wpdb;
19065
19066 $appointment_types = $wpdb->get_results( 'SELECT title, id FROM ' . $wpdb->prefix . 'ssa_appointment_types' );
19067 $options = [];
19068
19069 if ( ! empty( $appointment_types ) ) {
19070 foreach ( $appointment_types as $appointment_type ) {
19071 $options[] = [
19072 'label' => $appointment_type->title,
19073 'value' => $appointment_type->id,
19074 ];
19075 }
19076 }
19077 return [
19078 'options' => $options,
19079 'hasMore' => false,
19080 ];
19081 }
19082
19083 /**
19084 * Get mail mint Last Data
19085 *
19086 * @param array $data data.
19087 *
19088 * @return array
19089 */
19090 public function search_ssa_last_data( $data ) {
19091 $context = [];
19092 global $wpdb;
19093 $appointment_type_id = isset( $data['filter']['appointment_type_id']['value'] ) ? $data['filter']['appointment_type_id']['value'] : -1;
19094 $term = $data['search_term'] ? $data['search_term'] : '';
19095 $status = 'booked';
19096 if ( 'ssa_appointment_cancelled' === $term ) {
19097 $status = 'canceled';
19098 }
19099 $data = [];
19100 if ( -1 == $appointment_type_id ) {
19101 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointments WHERE status = %s ORDER BY id DESC Limit 1", $status ), ARRAY_A );
19102 } else {
19103 $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 );
19104 }
19105
19106
19107 if ( ! empty( $result ) ) {
19108 $result_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointment_meta where appointment_id=%d", $result['id'] ) );
19109 if ( ! empty( $result_meta ) ) {
19110 foreach ( $result_meta as $meta ) {
19111 $result[ $meta->meta_key ] = $meta->meta_value;
19112 }
19113 }
19114 }
19115 if ( ! empty( $result ) ) {
19116
19117 $result['customer_information'] = json_decode( $result['customer_information'], true );
19118 $context['pluggable_data'] = $result;
19119 $context['response_type'] = 'live';
19120 } else {
19121 $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 );
19122 }
19123 return (array) $context;
19124 }
19125
19126 /**
19127 * Get Simple Schedule Appointment Types
19128 *
19129 * @param array $data data.
19130 *
19131 * @return array
19132 */
19133 public function search_simple_schedule_appointments( $data ) {
19134 global $wpdb;
19135
19136 $appointment_types = $wpdb->get_results( 'SELECT title, id FROM ' . $wpdb->prefix . 'ssa_appointment_types' );
19137 $options = [];
19138
19139 if ( ! empty( $appointment_types ) ) {
19140 foreach ( $appointment_types as $appointment_type ) {
19141 $options[] = [
19142 'label' => $appointment_type->title,
19143 'value' => $appointment_type->id,
19144 ];
19145 }
19146 }
19147 return [
19148 'options' => $options,
19149 'hasMore' => false,
19150 ];
19151 }
19152
19153 /**
19154 * Get Sensei LMS Lessons
19155 *
19156 * @param array $data data.
19157 *
19158 * @return array
19159 */
19160 public function search_sensei_lms_lessons( $data ) {
19161 if ( ! function_exists( 'Sensei' ) ) {
19162 return [];
19163 }
19164 $course_id = $data['dynamic'];
19165 $lessons = \Sensei()->course->course_lessons( $course_id, 'publish', 'ids' );
19166 $options = [];
19167
19168 if ( ! empty( $lessons ) ) {
19169 foreach ( $lessons as $lesson ) {
19170 $options[] = [
19171 'label' => get_the_title( $lesson ),
19172 'value' => $lesson,
19173 ];
19174 }
19175 }
19176 return [
19177 'options' => $options,
19178 'hasMore' => false,
19179 ];
19180 }
19181
19182 /**
19183 * Get Sensei LMS Quiz List
19184 *
19185 * @param array $data data.
19186 *
19187 * @return array
19188 */
19189 public function search_sensei_lms_quiz( $data ) {
19190 $page = $data['page'];
19191 $limit = Utilities::get_search_page_limit();
19192 $offset = $limit * ( $page - 1 );
19193
19194 $quizes = get_posts(
19195 [
19196 'post_type' => 'lesson',
19197 'orderby' => 'title',
19198 'order' => 'ASC',
19199 'post_status' => 'publish',
19200 'posts_per_page' => $limit,
19201 'offset' => $offset,
19202 'fields' => 'ids',
19203 'meta_query' => [
19204 'relation' => 'AND',
19205 [
19206 'key' => '_lesson_quiz',
19207 'compare' => 'EXISTS',
19208 ],
19209 [
19210 'key' => '_quiz_has_questions',
19211 'value' => 1,
19212 'compare' => '=',
19213 ],
19214 ],
19215 ]
19216 );
19217 $options = [];
19218 $total_quizes = get_posts(
19219 [
19220 'post_type' => 'lesson',
19221 'orderby' => 'title',
19222 'order' => 'ASC',
19223 'post_status' => 'publish',
19224 'posts_per_page' => -1,
19225 'fields' => 'ids',
19226 'meta_query' => [
19227 'relation' => 'AND',
19228 [
19229 'key' => '_lesson_quiz',
19230 'compare' => 'EXISTS',
19231 ],
19232 [
19233 'key' => '_quiz_has_questions',
19234 'value' => 1,
19235 'compare' => '=',
19236 ],
19237 ],
19238 ]
19239 );
19240 $count = count( $total_quizes );
19241 if ( ! empty( $quizes ) ) {
19242 foreach ( $quizes as $quiz ) {
19243 $options[] = [
19244 'label' => get_the_title( $quiz ),
19245 'value' => $quiz,
19246 ];
19247 }
19248 }
19249 return [
19250 'options' => $options,
19251 'hasMore' => $count > $limit && $count > $offset,
19252 ];
19253 }
19254
19255 /**
19256 * Get Sensei LMS trigger Last Data
19257 *
19258 * @param array $data data.
19259 *
19260 * @return array|mixed|string
19261 */
19262 public function search_sensei_lms_triggers_last_data( $data ) {
19263 $context = [];
19264 global $wpdb;
19265 $term = $data['search_term'];
19266
19267 if ( ! function_exists( 'Sensei' ) ) {
19268 return;
19269 }
19270
19271 if ( 'course_completed' == $term ) {
19272 $comment_type = 'sensei_course_status';
19273 $course_id = $data['filter']['sensei_course']['value'];
19274 if ( -1 === $course_id ) {
19275 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s ORDER BY comment_ID DESC LIMIT 1";
19276 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type ), ARRAY_A );// @phpcs:ignore
19277 } else {
19278 $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";
19279 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, $course_id ), ARRAY_A );// @phpcs:ignore
19280 }
19281 } elseif ( 'course_enrolled' == $term ) {
19282 $comment_type = 'sensei_course_status';
19283 $course_id = $data['filter']['sensei_course']['value'];
19284 if ( -1 === $course_id ) {
19285 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s ORDER BY comment_ID DESC LIMIT 1";
19286 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'in-progress', $comment_type ), ARRAY_A );// @phpcs:ignore
19287 } else {
19288 $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";
19289 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'in-progress', $comment_type, $course_id ), ARRAY_A );// @phpcs:ignore
19290 }
19291 } elseif ( 'lesson_completed' == $term ) {
19292 $lesson_id = $data['filter']['sensei_lesson']['value'];
19293 $course_id = $data['filter']['sensei_course']['value'];
19294 $comment_type = 'sensei_lesson_status';
19295 if ( -1 === $lesson_id ) {
19296 $sql = "SELECT comments.*, meta.* FROM {$wpdb->prefix}comments as comments
19297 LEFT JOIN {$wpdb->prefix}postmeta as meta
19298 ON comments.comment_post_ID = meta.post_id
19299 WHERE comments.comment_approved = %s
19300 AND comments.comment_type = %s
19301 AND meta.meta_key = %s AND
19302 meta.meta_value = %d
19303 ORDER BY comments.comment_ID DESC LIMIT 1";
19304 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, '_lesson_course', $course_id ), ARRAY_A );// @phpcs:ignore
19305 } else {
19306 $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";
19307 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, $lesson_id ), ARRAY_A );// @phpcs:ignore
19308 }
19309 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term ) {
19310 $quiz_id = $data['filter']['sensei_quiz']['value'];
19311 $comment_type = 'sensei_lesson_status';
19312 if ( 'quiz_passes' == $term ) {
19313 $status = "( comment_approved = 'passed' )";
19314 } elseif ( 'quiz_fails' == $term ) {
19315 $status = "( comment_approved = 'failed' )";
19316 } else {
19317 $status = "( comment_approved = 'failed' OR comment_approved = 'passed' )";
19318 }
19319 if ( -1 === $quiz_id ) {
19320 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type = %s AND $status ORDER BY comment_ID DESC LIMIT 1";
19321 $results = $wpdb->get_results( $wpdb->prepare( $sql, $comment_type ), ARRAY_A );// @phpcs:ignore
19322 } else {
19323 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type = %s AND $status AND comment_post_ID = %d ORDER BY comment_ID DESC LIMIT 1";
19324 $results = $wpdb->get_results( $wpdb->prepare( $sql, $comment_type, $quiz_id ), ARRAY_A );// @phpcs:ignore
19325 }
19326 } elseif ( 'quiz_completion' == $term ) {
19327 $quiz_id = $data['filter']['sensei_quiz']['value'];
19328 $comment_type = 'sensei_lesson_status';
19329 $condition_compare = $data['filter']['condition_compare']['value'];
19330 $percentage = $data['filter']['percentage']['value'];
19331 if ( -1 === $quiz_id ) {
19332 $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
19333 } else {
19334 $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
19335 }
19336 }
19337
19338 if ( ! empty( $results ) ) {
19339 $post = get_post( $results[0]['comment_post_ID'] );
19340 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['user_id'] );
19341 if ( 'course_completed' == $term || 'course_enrolled' == $term ) {
19342 $context['pluggable_data']['sensei_course'] = $results[0]['comment_post_ID'];
19343 if ( $post instanceof \WP_Post ) {
19344 $context['pluggable_data']['course_title'] = $post->post_title;
19345 }
19346 } elseif ( 'lesson_completed' == $term ) {
19347 $context['pluggable_data']['sensei_lesson'] = $results[0]['comment_post_ID'];
19348 if ( $post instanceof \WP_Post ) {
19349 $context['pluggable_data']['lesson_title'] = $post->post_title;
19350 }
19351 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term || 'quiz_completion' == $term ) {
19352 $lesson_quiz = get_post_meta( $results[0]['comment_post_ID'], '_lesson_quiz', true );
19353 $submission = \Sensei()->quiz_submission_repository->get( $lesson_quiz, $results[0]['user_id'] );
19354 if ( $post instanceof \WP_Post ) {
19355 $context['pluggable_data']['quiz_title'] = $post->post_title;
19356 }
19357 $context['pluggable_data']['quiz_status'] = $results[0]['comment_approved'];
19358 $context['pluggable_data']['quiz_data']['id'] = $submission->get_id();
19359 $context['sensei_quiz'] = $submission->get_id();
19360 $context['pluggable_data']['quiz_data']['final_grade'] = $submission->get_final_grade();
19361 $context['pluggable_data']['quiz_data']['created_at'] = $submission->get_created_at();
19362 }
19363 $context['response_type'] = 'live';
19364 } else {
19365 if ( 'course_completed' == $term || 'course_enrolled' == $term ) {
19366 $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
19367 } elseif ( 'lesson_completed' == $term ) {
19368 $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
19369 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term || 'quiz_completion' == $term ) {
19370 $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
19371 }
19372 }
19373 return $context;
19374 }
19375
19376 /**
19377 * Get SurelyWP Services - SureCart Addons Last Data
19378 *
19379 * @param array $data data.
19380 *
19381 * @return array
19382 */
19383 public function search_services_sc_triggers_last_data( $data ) {
19384 $context = [];
19385 global $wpdb;
19386 $term = $data['search_term'] ? $data['search_term'] : '';
19387 $data = [];
19388
19389 if ( 'new_service_created' === $term ) {
19390 $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 );
19391 } elseif ( 'requirement_submitted' === $term ) {
19392 $result = $wpdb->get_row( "SELECT created_at FROM {$wpdb->prefix}surelywp_sv_requirements ORDER BY created_at DESC Limit 1", ARRAY_A );
19393 } elseif ( 'message_sent' === $term ) {
19394 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_messages ORDER BY message_id DESC Limit 1", ARRAY_A );
19395 } elseif ( 'message_final_delivery_sent' === $term ) {
19396 $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 );
19397 } elseif ( 'customer_request_revision' === $term ) {
19398 $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 );
19399 } elseif ( 'customer_approves_final_delivery' === $term ) {
19400 $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 );
19401 } elseif ( 'delivery_date_changed' === $term ) {
19402 $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 );
19403 } elseif ( 'service_cancel' === $term || 'service_marked_canceled' === $term ) {
19404 $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 );
19405 } elseif ( 'service_completed' === $term || 'service_mark_completed' === $term ) {
19406 $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 );
19407 } elseif ( 'contract_signed' === $term ) {
19408 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_contracts ORDER BY contract_id DESC Limit 1", ARRAY_A );
19409 }
19410 if ( ! empty( $result ) ) {
19411 if ( 'new_service_created' === $term ) {
19412 $service_data = [
19413 'service_setting_id' => $result['service_id'],
19414 'order_id' => $result['order_id'],
19415 'product_id' => $result['product_id'],
19416 'service_status' => $result['service_status'],
19417 'delivery_date' => $result['delivery_date'],
19418 ];
19419 $context['pluggable_data'] = array_merge( $service_data, WordPress::get_user_context( $result['user_id'] ) );
19420 $context['response_type'] = 'live';
19421 } elseif ( 'requirement_submitted' === $term ) {
19422 $requirements_data = $wpdb->get_results(
19423 $wpdb->prepare(
19424 "SELECT * FROM {$wpdb->prefix}surelywp_sv_requirements WHERE created_at = %s",
19425 $result['created_at']
19426 ),
19427 ARRAY_A
19428 );
19429 $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 );
19430 $user_data = WordPress::get_user_context( $service_result['user_id'] );
19431 unset( $service_result['user_id'] );
19432 $context['pluggable_data'] = array_merge( $requirements_data, $service_result, $user_data );
19433 foreach ( $requirements_data as $value ) {
19434 if ( 'file' == $value['requirement_type'] ) {
19435 $upload_dir = wp_upload_dir();
19436 $attachment_file_names = json_decode( $value['requirement'], true );
19437 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
19438 $context['pluggable_data']['requirement_attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $value['service_id'] . '/requirement/' . $attachment_file_name;
19439 }
19440 }
19441 }
19442 $context['response_type'] = 'live';
19443 } elseif ( 'message_sent' === $term || 'message_final_delivery_sent' === $term ) {
19444 $message_data = [
19445 'sender' => WordPress::get_user_context( $result['sender_id'] ),
19446 'receiver' => WordPress::get_user_context( $result['receiver_id'] ),
19447 'service_id' => $result['service_id'],
19448 'message_text' => $result['message_text'],
19449 'attachment_file_name' => $result['attachment_file_name'],
19450 'is_final_delivery' => $result['is_final_delivery'],
19451 ];
19452 $context['pluggable_data'] = $message_data;
19453 $upload_dir = wp_upload_dir();
19454 $attachment_file_names = json_decode( $result['attachment_file_name'], true );
19455 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
19456 $context['pluggable_data']['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $result['service_id'] . '/messages/' . $attachment_file_name;
19457 }
19458 $context['response_type'] = 'live';
19459 } elseif ( 'customer_request_revision' === $term ) {
19460 $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 );
19461 if ( ! empty( $message_result ) ) {
19462 global $surelywp_sv_model;
19463 $revision_message = $surelywp_sv_model->surelywp_sv_get_customer_revision_msg( $message_result['service_id'], $message_result['message_id'] );
19464 $context['pluggable_data'] = array_merge( $result, $revision_message );
19465 $context['pluggable_data']['sender'] = WordPress::get_user_context( $revision_message['sender_id'] );
19466 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $revision_message['receiver_id'] );
19467 $upload_dir = wp_upload_dir();
19468 $attachment_file_names = json_decode( $revision_message['attachment_file_name'], true );
19469 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
19470 $context['pluggable_data']['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $revision_message['service_id'] . '/messages/' . $attachment_file_name;
19471 }
19472 $context['response_type'] = 'live';
19473 } else {
19474 $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 );
19475 }
19476 } elseif ( 'customer_approves_final_delivery' === $term || 'delivery_date_changed' === $term || 'service_cancel' === $term || 'service_marked_canceled' === $term || 'service_completed' === $term || 'service_mark_completed' === $term ) {
19477 $user_data = WordPress::get_user_context( $result['user_id'] );
19478 unset( $result['user_id'] );
19479 $context['pluggable_data'] = array_merge( $result, $user_data );
19480 $context['response_type'] = 'live';
19481 } elseif ( 'contract_signed' === $term ) {
19482 $contract_data = [
19483 'service_id' => $result['service_id'],
19484 'signature' => $result['signature'],
19485 'contract_details' => $result['contract_details'],
19486 ];
19487 $service_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_id = %d", $result['service_id'] ), ARRAY_A );
19488 $user_data = WordPress::get_user_context( $service_result['user_id'] );
19489 unset( $service_result['user_id'] );
19490 $context['pluggable_data'] = array_merge( $contract_data, $user_data, $service_result );
19491 $context['response_type'] = 'live';
19492 }
19493 } else {
19494 if ( 'new_service_created' === $term || 'customer_request_revision' === $term || 'customer_approves_final_delivery' === $term || 'delivery_date_changed' === $term ) {
19495 $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 );
19496 } elseif ( 'requirement_submitted' === $term ) {
19497 $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 );
19498 } elseif ( 'message_sent' === $term || 'message_final_delivery_sent' === $term ) {
19499 $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 );
19500 } elseif ( 'service_cancel' === $term ) {
19501 $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 );
19502 } elseif ( 'service_marked_canceled' === $term || 'service_completed' === $term || 'service_mark_completed' === $term ) {
19503 $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 );
19504 } elseif ( 'contract_signed' === $term ) {
19505 $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 );
19506 }
19507 }
19508 return (array) $context;
19509 }
19510
19511 /**
19512 * Get SurelyWP Support Portal - SureCart Addons Last Data
19513 *
19514 * @param array $data data.
19515 *
19516 * @return array
19517 */
19518 public function search_sc_support_portal_triggers_last_data( $data ) {
19519 $context = [];
19520 if ( ! class_exists( 'Surelywp_Support_Portal' ) ) {
19521 return [];
19522 }
19523 global $wpdb;
19524 $term = $data['search_term'] ? $data['search_term'] : '';
19525 $data = [];
19526
19527 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
19528 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sp_support WHERE support_status = 1 ORDER BY support_id DESC Limit 1", ARRAY_A );
19529 } elseif ( 'new_message_sent' === $term ) {
19530 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sp_messages ORDER BY message_id DESC Limit 1", ARRAY_A );
19531 } elseif ( 'tickets_closed' === $term ) {
19532 $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 );
19533 } elseif ( 'tickets_opens' === $term ) {
19534 $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 );
19535 } elseif ( 'new_email_message_fetched' === $term ) {
19536 $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 );
19537 }
19538 if ( ! empty( $result ) ) {
19539 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
19540 $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 );
19541 $support_data = [
19542 'support_id' => $result['support_id'],
19543 'order_id' => $result['order_id'],
19544 'product_id' => $result['product_id'],
19545 'support_title' => $result['support_title'],
19546 'support_status' => Surelywp_Support_Portal::surelywp_sp_get_support_status( $result['support_status'] ),
19547 'support_data' => $support_res,
19548 ];
19549 $context['pluggable_data'] = array_merge( $support_data, WordPress::get_user_context( $result['user_id'] ) );
19550 $context['response_type'] = 'live';
19551 } elseif ( 'tickets_closed' === $term || 'tickets_opens' === $term ) {
19552 $support_data = [];
19553 $support_ids = $result;
19554 foreach ( $support_ids as $key => $id ) {
19555 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sp_support WHERE support_id = %d", $id ), ARRAY_A );
19556 $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 );
19557 $support_data[ $key ] = WordPress::get_user_context( $result['user_id'] );
19558 $support_data[ $key ] = [
19559 'support_id' => $result['support_id'],
19560 'order_id' => $result['order_id'],
19561 'product_id' => $result['product_id'],
19562 'support_title' => $result['support_title'],
19563 'support_status' => Surelywp_Support_Portal::surelywp_sp_get_support_status( $result['support_status'] ),
19564 'support_data' => $support_res,
19565 ];
19566 }
19567 $context['pluggable_data'] = $support_data;
19568 $context['response_type'] = 'live';
19569 } elseif ( 'new_message_sent' === $term ) {
19570 $upload_dir = wp_upload_dir();
19571 $support_data = [
19572 'sender_id' => $result['sender_id'],
19573 'receiver_id' => $result['receiver_id'],
19574 'support_id' => $result['support_id'],
19575 'message_text' => $result['message_text'],
19576 'sender' => WordPress::get_user_context( $result['sender_id'] ),
19577 'receiver' => WordPress::get_user_context( $result['receiver_id'] ),
19578 ];
19579 if ( ! empty( $result['attachment_file_name'] ) ) {
19580 $attachment_file_names = json_decode( $result['attachment_file_name'], true );
19581 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
19582 $support_data['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-support-portal-data/' . $result['support_id'] . '/messages/' . $attachment_file_name;
19583 }
19584 }
19585 $context['pluggable_data'] = $support_data;
19586 $context['response_type'] = 'live';
19587 }
19588 } else {
19589 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
19590 $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 );
19591 } elseif ( 'tickets_closed' === $term || 'tickets_opens' === $term ) {
19592 $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 );
19593 } elseif ( 'new_message_sent' === $term ) {
19594 $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 );
19595 }
19596 }
19597 return (array) $context;
19598 }
19599
19600 /**
19601 * Get Fluent Boards Last Data
19602 *
19603 * @param array $data data.
19604 *
19605 * @return array
19606 */
19607 public function search_fbs_triggers_last_data( $data ) {
19608 $context = [];
19609 global $wpdb;
19610 $term = $data['search_term'] ? $data['search_term'] : '';
19611 if ( ! class_exists( 'FluentBoards\App\Models\Board' ) || ! class_exists( 'FluentBoards\App\Models\User' ) ) {
19612 return [];
19613 }
19614 $data = [];
19615
19616 if ( 'board_created' === $term ) {
19617 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_boards ORDER BY id DESC Limit 1", ARRAY_A );
19618 } elseif ( 'board_member_added' === $term ) {
19619 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_relations WHERE object_type = 'board_user' ORDER BY id DESC Limit 1", ARRAY_A );
19620 } elseif ( 'task_created' === $term ) {
19621 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_tasks ORDER BY id DESC Limit 1", ARRAY_A );
19622 }
19623 if ( ! empty( $result ) ) {
19624 if ( 'board_created' === $term ) {
19625 $board_id = $result['id'];
19626 $context['pluggable_data'] = \FluentBoards\App\Models\Board::findOrFail( $board_id );
19627 $context['response_type'] = 'live';
19628 } elseif ( 'board_member_added' === $term ) {
19629 $context['pluggable_data']['board_id'] = $result['object_id'];
19630 $context['pluggable_data']['board_member'] = \FluentBoards\App\Models\User::find( $result['foreign_id'] );
19631 $context['response_type'] = 'live';
19632 } elseif ( 'task_created' === $term ) {
19633 $context['pluggable_data'] = $result;
19634 $context['response_type'] = 'live';
19635 }
19636 } else {
19637 if ( 'board_created' === $term ) {
19638 $context = json_decode( '{"pluggable_data":{"id":2,"parent_id":null,"title":"testing","description":"testing","type":"to-do","currency":"USD","background":{"id":"solid_1","is_image":false,"image_url":null,"color":"#d1d8e0"},"settings":null,"created_by":"1","archived_at":null,"meta":[]},"response_type":"sample"}', true );
19639 } elseif ( 'board_member_added' === $term ) {
19640 $context = json_decode( '{"pluggable_data":{"board_id":"2","board_member":{"ID":1,"user_login":"johnd","user_nicename":"johnd","user_email":"johnd@example.com","user_url":"https://example.com","user_registered":"2023-01-16 09:23:31","user_status":"0","display_name":"johnd","photo":"https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2Fodeploll/128"}},"response_type":"sample"}', true );
19641 } elseif ( 'task_created' === $term ) {
19642 $context = json_decode( '{"pluggable_data":{"id":"1","parent_id":null,"board_id":"1","crm_contact_id":null,"title":"Task1","slug":"task1","type":"task","status":"open","stage_id":"6","source":"web","source_id":null,"priority":"low","description":null,"lead_value":"0.00","created_by":"1","position":"1.00","comments_count":"0","issue_number":null,"reminder_type":"none","settings":"a:4:{s:5:\"cover\";a:1:{s:15:\"backgroundColor\";s:0:\"\";}s:13:\"subtask_count\";i:0;s:16:\"attachment_count\";i:0;s:23:\"subtask_completed_count\";i:0;}","remind_at":null,"started_at":null,"due_at":null,"last_completed_at":null,"archived_at":null,"created_at":"2024-10-14 17:11:20","updated_at":"2024-10-14 17:11:20"},"response_type":"sample"}', true );
19643 }
19644 }
19645 return (array) $context;
19646 }
19647
19648 /**
19649 * Get Profile Grid Last Data
19650 *
19651 * @param array $data data.
19652 *
19653 * @return array
19654 */
19655 public function search_profile_grid_triggers_last_data( $data ) {
19656 $context = [];
19657 global $wpdb;
19658 $term = $data['search_term'] ? $data['search_term'] : '';
19659
19660 if ( 'user_added_group' === $term || 'user_removed_from_group' === $term || 'user_assigned_group_manager' === $term || 'user_unassigned_group_manager' === $term || 'membership_request_approved' === $term ) {
19661 $users = get_users(
19662 [
19663 'orderby' => 'meta_value',
19664 'meta_key' => 'pm_group',
19665 'order' => 'DESC',
19666 'number' => 1,
19667 'meta_query' => [
19668 [
19669 'key' => 'pm_group',
19670 'compare' => 'EXISTS',
19671 ],
19672 ],
19673 'fields' => 'ids',
19674 ]
19675 );
19676 } elseif ( 'payment_complete' === $term || 'payment_failed' === $term ) {
19677 $users = get_users(
19678 [
19679 'orderby' => 'meta_value',
19680 'meta_key' => 'pm_group_payment_status',
19681 'order' => 'DESC',
19682 'number' => 1,
19683 'meta_query' => [
19684 [
19685 'key' => 'pm_group_payment_status',
19686 'compare' => 'EXISTS',
19687 ],
19688 ],
19689 'fields' => 'ids',
19690 ]
19691 );
19692 } elseif ( 'membership_request' === $term ) {
19693 $results = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}promag_group_requests WHERE status = 1 ORDER BY id DESC Limit 1", ARRAY_A );
19694 }
19695 if ( ! empty( $users ) ) {
19696 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 ) {
19697 $context = WordPress::get_user_context( $users[0] );
19698 $user_groups = get_user_meta( $users[0], 'pm_group', true );
19699 if ( is_array( $user_groups ) ) {
19700 $last_element = end( $user_groups );
19701 $context['group_id'] = $last_element;
19702 $context = array_merge( $context, ProfileGrid::pg_group_details( $last_element ) );
19703 }
19704 $context['pluggable_data'] = $context;
19705 $context['response_type'] = 'live';
19706 }
19707 } elseif ( ! empty( $results ) && 'membership_request' === $term ) {
19708 $context = WordPress::get_user_context( $results[0]['uid'] );
19709 $context['group_id'] = $results[0]['gid'];
19710 $context = array_merge( $context, ProfileGrid::pg_group_details( $results[0]['gid'] ) );
19711 $context['pluggable_data'] = $context;
19712 $context['response_type'] = 'live';
19713 } elseif ( 'group_manager_resets_password' === $term ) {
19714 $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 );
19715 } elseif ( 'group_deleted' === $term ) {
19716 $context = json_decode( '{"pluggable_data":{"group_name":"Test Group","group_description":"Testing Group","group_id":"2"},"response_type":"sample"}', true );
19717 } else {
19718 $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 );
19719 }
19720 return (array) $context;
19721 }
19722
19723 /**
19724 * Get Fluent SMTP Last Data
19725 *
19726 * @param array $data data.
19727 *
19728 * @return array
19729 */
19730 public function search_fluent_smtp_last_data( $data ) {
19731 $context = [];
19732 global $wpdb;
19733 $results = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fsmpt_email_logs WHERE status = 'failed' ORDER BY id DESC Limit 1", ARRAY_A );
19734 if ( ! empty( $results ) ) {
19735 $context['to'] = unserialize( $results['to'] );
19736 $context['from'] = $results['from'];
19737 $context['subject'] = $results['subject'];
19738 $context['body'] = $results['body'];
19739 $context['attachments'] = unserialize( $results['attachments'] );
19740 $context['status'] = $results['status'];
19741 $context['response'] = unserialize( $results['response'] );
19742 $context['headers'] = unserialize( $results['headers'] );
19743 $context['extra'] = unserialize( $results['extra'] );
19744 $context['pluggable_data'] = $context;
19745 $context['response_type'] = 'live';
19746 } else {
19747 $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 );
19748 }
19749 return (array) $context;
19750 }
19751
19752 }
19753
19754
19755 GlobalSearchController::get_instance();
19756