PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.0.83
OttoKit: All-in-One Automation Platform v1.0.83
1.1.34 1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 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 1 year ago SettingsController.php 1 year ago WebhookRequestsController.php 1 year ago
GlobalSearchController.php
20604 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 FluentBoards\App\Models\Stage;
87 use FluentCommunity\App\Functions\Utility;
88 use Surelywp_Support_Portal;
89 use SureTriggers\Integrations\ProfileGrid\ProfileGrid;
90
91 /**
92 * GlobalSearchController- Add ajax related functions here.
93 *
94 * @category GlobalSearchController
95 * @package SureTriggers
96 * @author BSF <username@example.com>
97 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
98 * @link https://www.brainstormforce.com/
99 * @since 1.0.0
100 *
101 * @psalm-suppress UndefinedTrait
102 */
103 class GlobalSearchController {
104
105 use SingletonLoader;
106
107 /**
108 * Search post by post type.
109 *
110 * @param array $data Search Params.
111 *
112 * @since 1.0.0
113 *
114 * @return array
115 */
116 public function search_post( $data ) {
117 $result = [];
118 $posts = Utilities::find_posts_by_title( $data );
119
120 foreach ( $posts['results'] as $post ) {
121 $result[] = [
122 'label' => $post['post_title'],
123 'value' => $post['ID'],
124 ];
125 }
126
127 return [
128 'options' => $result,
129 'hasMore' => $posts['has_more'],
130 ];
131 }
132
133 /**
134 * Search Course.
135 *
136 * @param array $data quesry params.
137 *
138 * @return array
139 * @since 1.0.0
140 */
141 public function search_ld_course( $data ) {
142
143 $page = $data['page'];
144 $limit = Utilities::get_search_page_limit();
145 $offset = $limit * ( $page - 1 );
146
147 $courses = get_posts(
148 [
149
150 'post_type' => 'product',
151 'meta_key' => '_related_course',
152 'post_status' => 'publish',
153 'posts_per_page' => $limit,
154 'offset' => $offset,
155 ]
156 );
157 $options = [];
158 foreach ( $courses as $course ) {
159 $options[] = [
160 'label' => $course->post_title,
161 'value' => $course->ID,
162 ];
163 }
164
165 $all_courses = get_posts(
166 [
167 'post_type' => 'product',
168 'meta_key' => '_related_course',
169 'post_status' => 'publish',
170 'fields' => 'ids',
171 'posts_per_page' => -1,
172 ]
173 );
174 $posts_count = count( $all_courses );
175
176 return [
177 'options' => $options,
178 'hasMore' => $posts_count > $limit && $posts_count > $offset,
179 ];
180 }
181
182 /**
183 * Search achievement by post type.
184 *
185 * @param array $data Search Params.
186 *
187 * @since 1.0.0
188 *
189 * @return array
190 */
191 public function search_achievements( $data ) {
192
193 $page = $data['page'];
194 $limit = Utilities::get_search_page_limit();
195 $offset = $limit * ( $page - 1 );
196
197 $post = get_post( $data['dynamic'] );
198 $slug = $post->post_name;
199
200 $achievements = get_posts(
201 [
202 'post_type' => $slug,
203 'post_status' => 'publish',
204 'posts_per_page' => $limit,
205 'offset' => $offset,
206 ]
207 );
208 $options = [];
209 foreach ( $achievements as $achievement ) {
210 $options[] = [
211 'label' => $achievement->post_title,
212 'value' => (string) $achievement->ID,
213 ];
214 }
215
216 $count = wp_count_posts( $slug )->publish;
217
218 return [
219 'options' => $options,
220 'hasMore' => $count > $limit && $count > $offset,
221 ];
222 }
223
224 /**
225 * Search Course.
226 *
227 * @param array $data quesry params.
228 *
229 * @return array
230 * @since 1.0.0
231 */
232 public function search_tutor_course( $data ) {
233 $courses = get_posts(
234 [
235 'post_type' => tutor()->course_post_type,
236 'post_status' => 'publish',
237 'numberposts' => '-1',
238 ]
239 );
240 $options = [];
241 foreach ( $courses as $course ) {
242 $options[] = [
243 'label' => $course->post_title,
244 'value' => $course->ID,
245 ];
246 }
247
248 return [
249 'options' => $options,
250 'hasMore' => false,
251 ];
252 }
253
254 /**
255 * Search Products.
256 *
257 * @param array $data quesry params.
258 *
259 * @return array
260 * @since 1.0.0
261 */
262 public function search_product( $data ) {
263 $result = [];
264 $posts = Utilities::find_posts_by_title( $data );
265
266 foreach ( $posts['results'] as $post ) {
267 $result[] = [
268 'label' => $post['post_title'],
269 'value' => $post['post_title'],
270 ];
271 }
272
273 return [
274 'options' => $result,
275 'hasMore' => $posts['has_more'],
276 ];
277 }
278
279 /**
280 * Search Product Categories.
281 *
282 * @param array $data Search Params.
283 *
284 * @since 1.0.0
285 */
286 public function search_product_category( $data ) {
287 if ( ! empty( $data['dynamic'] ) ) {
288 $taxonomy = $data['dynamic'];
289 } else {
290 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
291 }
292
293 $term = $data['term'];
294 $result = [];
295 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
296 foreach ( $terms['result'] as $tax_term ) {
297 $result[] = [
298 'label' => $tax_term->name,
299 'value' => $tax_term->name,
300 ];
301 }
302
303 return [
304 'options' => $result,
305 'hasMore' => $terms['has_more'],
306 ];
307 }
308
309 /**
310 * Search Product Tags.
311 *
312 * @param array $data Search Params.
313 *
314 * @since 1.0.0
315 */
316 public function search_product_tags( $data ) {
317 if ( ! empty( $data['dynamic'] ) ) {
318 $taxonomy = $data['dynamic'];
319 } else {
320 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
321 }
322
323 $term = $data['term'];
324 $result = [];
325 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
326
327 foreach ( $terms['result'] as $tax_term ) {
328 $result[] = [
329 'label' => $tax_term->name,
330 'value' => $tax_term->name,
331 ];
332 }
333
334 return [
335 'options' => $result,
336 'hasMore' => $terms['has_more'],
337 ];
338 }
339
340 /**
341 * Global ajax search.
342 * Here you need to add the field action name to work.
343 *
344 * @param WP_REST_Request $request Request data.
345 *
346 * @return WP_REST_Response
347 * @since 1.0.0
348 */
349 public function global_search( $request ) {
350 $post_type = $request->get_param( 'post_type' );
351 $dynamic = $request->get_param( 'dynamic' );
352 $search_term = $request->get_param( 'term' );
353 $identifier = $request->get_param( 'field_identifier' );
354 $page = max( 1, $request->get_param( 'page' ) );
355 $taxonomy = $request->get_param( 'taxonomy' ) ? $request->get_param( 'taxonomy' ) : [];
356
357 $filter = $request->get_param( 'filter' ) ? json_decode( stripslashes( $request->get_param( 'filter' ) ), true ) : [];
358
359 $data = [
360 'dynamic' => $dynamic,
361 'search_term' => $search_term,
362 'page' => $page,
363 'taxonomy' => $taxonomy,
364 'filter' => $filter,
365 'post_type' => $post_type,
366 ];
367 $response = [
368 'hasMore' => false,
369 'options' => [],
370 ];
371
372 $method_name = 'search_' . $identifier;
373
374 if ( method_exists( $this, $method_name ) ) {
375 $response = $this->{$method_name}( $data );
376 } else {
377 return RestController::error_message( 'Invalid field Identifier param.' );
378 }
379
380 return RestController::success_message( $response );
381 }
382
383 /**
384 * Search Taxonomy Terms.
385 *
386 * @param array $data Search Params.
387 *
388 * @since 1.0.0
389 *
390 * @return array
391 */
392 public function search_term( $data ) {
393 if ( ! empty( $data['dynamic'] ) ) {
394 $taxonomy = $data['dynamic'];
395 } else {
396 $taxonomy = isset( $data['taxonomy'] ) ? $data['taxonomy'] : '';
397 }
398
399 $term = $data['term'];
400 $result = [];
401 $terms = Utilities::get_terms( $term, $data['page'], $taxonomy );
402 foreach ( $terms['result'] as $tax_term ) {
403 $result[] = [
404 'label' => $tax_term->name,
405 'value' => $tax_term->term_id,
406 ];
407 }
408
409 return [
410 'options' => $result,
411 'hasMore' => $terms['has_more'],
412 ];
413 }
414
415 /**
416 * Search sureemails data.
417 *
418 * @param array $data data.
419 * @return array
420 */
421 public function search_pluggables_sureemails_mail( $data ) {
422 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
423 $context = [];
424
425 if ( 'mail_sent' === $term ) {
426 $context['pluggable_data'] = [
427 'to' => [ 'johnDoe@xyz.com' ],
428 'subject' => 'Test Email',
429 'message' => 'This is a test email',
430 'headers' => 'From: johnDoe@xyz.com,X-Mailer: PHP/8.1.22,Content-Type: text/html; charset=utf-8,Reply-To: johnDoe@xyz.com,Cc:johnDoe@xyz.com,Bcc:johnDoe@xyz.com',
431 'attachments' => [ 'Sample Attachment' ],
432 ];
433 $context['response_type'] = 'sample';
434 } elseif ( 'mail_failed' === $term ) {
435 $context['pluggable_data'] = [
436 'errors' => [
437 'wp_mail_failed' => [
438 'We were unable to send the email. Please ensure the recipient email address and sender configuration are correct. If the issue persists, contact support.',
439 ],
440 ],
441 'error_data' => [
442 'wp_mail_failed' => [
443 'to' => 'johnDoe@xyz.com',
444 'subject' => 'Test Email',
445 'message' => 'This is a test email.',
446 'headers' => 'From: johnDoe@xyz.com,X-Mailer: PHP/8.1.22,Content-Type: text/html; charset=utf-8,Reply-To: johnDoe@xyz.com,Cc:johnDoe@xyz.com,Bcc:johnDoe@xyz.com',
447 'attachments' => [ 'Sample Attachment' ],
448 ],
449 ],
450 ];
451 $context['response_type'] = 'sample';
452 } elseif ( 'mail_blocked' === $term ) {
453 $context['pluggable_data'] = [
454 'to' => 'johnDoe@xyz.com',
455 'subject' => 'Test Email',
456 'message' => 'This is a test email.',
457 'headers' => 'From: johnDoe@xyz.com
458 X-Mailer: PHP/8.1.22
459 Content-Type: text/html; charset=utf-8
460 Reply-To: johnDoe@xyz.com
461 Cc:johnDoe@xyz.com Bcc:johnDoe@xyz.com',
462 'attachments' => [ 'Sample Attachment' ],
463 'categories' => [
464 'harassment' => 1,
465 'harassment/threatening' => 1,
466 'sexual' => 0,
467 'hate' => 0,
468 'hate/threatening' => 0,
469 'illicit' => 0,
470 'illicit/violent' => 0,
471 'self-harm/intent' => 0,
472 'self-harm/instructions' => 0,
473 'self-harm' => 0,
474 'sexual/minors' => 0,
475 'violence' => 1,
476 'violence/graphic' => 0,
477 ],
478 ];
479 $context['response_type'] = 'sample';
480 }
481
482 return $context;
483 }
484
485
486
487 /**
488 * List Taxonomy Terms.
489 *
490 * @param array $data Search Params.
491 *
492 * @since 1.0.0
493 *
494 * @return array
495 */
496 public function search_term_list( $data ) {
497 $taxonomy = $data['dynamic'];
498 $result = [];
499 $terms = Utilities::get_terms( '', $data['page'], $taxonomy );
500 foreach ( $terms['result'] as $tax_term ) {
501 $result[] = [
502 'label' => $tax_term->name,
503 'value' => $tax_term->term_id,
504 ];
505 }
506
507 return [
508 'options' => $result,
509 'hasMore' => $terms['has_more'],
510 ];
511 }
512
513 /**
514 * List All Taxonomy Terms.
515 *
516 * @param array $data Search Params.
517 *
518 * @since 1.0.0
519 *
520 * @return array
521 */
522 public function search_term_list_for_all_taxonomy( $data ) {
523 $taxonomy = $data['dynamic'];
524 if ( ! is_array( $taxonomy ) ) {
525 $taxonomy = explode( ',', $taxonomy );
526 $taxonomy = array_map(
527 function ( $tax ) {
528 return [
529 'label' => trim( $tax ),
530 'value' => trim( $tax ),
531 ];
532 },
533 $taxonomy
534 );
535 }
536
537 $result = [];
538 $terms = [];
539 foreach ( $taxonomy as $tax ) {
540 $terms = Utilities::get_terms( '', $data['page'], $tax['value'] );
541
542 foreach ( $terms['result'] as $tax_term ) {
543 $result[] = [
544 'label' => $tax_term->name . ' - ' . ucwords( $tax['label'] ),
545 'value' => $tax_term->term_id . '%-%' . $tax['value'],
546 ];
547 }
548 }
549
550 return [
551 'options' => $result,
552 'hasMore' => $terms['has_more'],
553 ];
554 }
555
556
557 /**
558 * List Category Terms.
559 *
560 * @param array $data Search Params.
561 *
562 * @since 1.0.0
563 *
564 * @return array
565 */
566 public function search_category_term_list( $data ) {
567 $result = [];
568 $taxonomy = $data['dynamic'];
569 $terms = Utilities::get_terms( '', $data['page'], $taxonomy );
570 foreach ( $terms['result'] as $tax_term ) {
571 if ( 0 == $tax_term->parent ) {
572 $result[] = [
573 'label' => $tax_term->name,
574 'value' => $tax_term->term_id,
575 ];
576 foreach ( $terms['result'] as $child ) {
577 if ( $child->parent == $tax_term->term_id ) {
578 $result[] = [
579 'label' => ' - ' . $child->name,
580 'value' => $child->term_id,
581 ];
582 }
583 }
584 }
585 }
586
587 return [
588 'options' => $result,
589 'hasMore' => $terms['has_more'],
590 ];
591 }
592
593 /**
594 * List Taxonomy Tags.
595 *
596 * @param array $data Search Params.
597 *
598 * @since 1.0.0
599 *
600 * @return array
601 */
602 public function search_term_list_tags( $data ) {
603 $result = [];
604 $terms = Utilities::get_terms( '', $data['page'], [ 'post_tag' ] );
605 foreach ( $terms['result'] as $tax_term ) {
606 $result[] = [
607 'label' => $tax_term->name,
608 'value' => $tax_term->term_id,
609 ];
610 }
611
612 return [
613 'options' => $result,
614 'hasMore' => $terms['has_more'],
615 ];
616 }
617
618 /**
619 * List Role Capabilities.
620 *
621 * @param array $data Search Params.
622 *
623 * @since 1.0.0
624 *
625 * @return array
626 */
627 public function search_role_capabilities( $data ) {
628 $user_roles = wp_roles();
629 $result = [];
630
631 foreach ( $user_roles->roles as $name => $role ) {
632 foreach ( $role['capabilities'] as $key => $cap ) {
633 $result[] = [
634 'label' => $key,
635 'value' => esc_attr( $key ),
636 ];
637 }
638 }
639 $result = array_unique( $result, SORT_REGULAR );
640
641 return [
642 'options' => $result,
643 'hasMore' => false,
644 ];
645 }
646
647 /**
648 * Search users.
649 *
650 * @param array $data Search Params.
651 *
652 * @since 1.0.0
653 *
654 * @return array
655 */
656 public function search_user( $data ) {
657 $result = [];
658 $page = $data['page'];
659 $users = Utilities::get_users( $data, $page );
660
661 if ( is_array( $users['results'] ) ) {
662 foreach ( $users['results'] as $user ) {
663 $result[] = [
664 'label' => $user->user_login,
665 'value' => $user->ID,
666 ];
667 }
668 }
669
670 return [
671 'options' => $result,
672 'hasMore' => $users['has_more'],
673 ];
674
675 }
676
677 /**
678 * Search WPForm fields.
679 *
680 * @param array $data Search Params.
681 *
682 * @since 1.0.0
683 *
684 * @return array
685 */
686 public function search_pluggable_wpform_fields( $data ) {
687 $result = [];
688 $page = $data['page'];
689 $form_id = absint( $data['dynamic'] );
690 $wpform_fields = Utilities::get_wpform_fields( $data['search_term'], $page, $form_id );
691
692 if ( is_array( $wpform_fields['results'] ) ) {
693 foreach ( $wpform_fields['results'] as $field ) {
694 $result[] = [
695 'label' => $field['label'],
696 'value' => '{' . $field['id'] . '}',
697 ];
698 }
699 }
700
701 return [
702 'options' => $result,
703 'hasMore' => $wpform_fields['has_more'],
704 ];
705 }
706
707 /**
708 * Prepare variable products.
709 *
710 * @param array $data data.
711 *
712 * @return array
713 */
714 public function search_variable_products( $data ) {
715 $products = Utilities::get_variable_products( $data['search_term'], $data['page'] );
716 $options = [];
717
718 foreach ( $products['result'] as $product ) {
719 $options[] = [
720 'label' => $product->get_title(),
721 'value' => (string) $product->get_id(),
722 ];
723 }
724
725 return [
726 'options' => $options,
727 'hasMore' => $products['has_more'],
728 ];
729 }
730
731 /**
732 * Prepare variable products.
733 *
734 * @param array $data data.
735 *
736 * @return array
737 */
738 public function search_product_variations( $data ) {
739 $variations = Utilities::get_product_variations( $data['dynamic'] );
740 $options = [];
741
742 foreach ( $variations['result'] as $product ) {
743 $options[] = [
744 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
745 'value' => (string) $product->ID,
746 ];
747 }
748
749 return [
750 'options' => $options,
751 'hasMore' => $variations['has_more'],
752 ];
753 }
754
755 /**
756 * Search WooCommerce Subscriptions.
757 *
758 * @param array $data Search Params.
759 *
760 * @since 1.0.0
761 *
762 * @return array
763 */
764 public function search_subscription_variation( $data ) {
765 $subscription_products = Utilities::get_subscription_variation( $data['search_term'], $data['page'] );
766 $result = [];
767
768 if ( ! function_exists( 'wc_get_products' ) ) {
769 return $result;
770 }
771
772 foreach ( $subscription_products['result'] as $post ) {
773 if ( $data['search_term'] ) {
774 if ( false !== stripos( $post->get_title(), $data['search_term'] ) ) {
775 $result[] = [
776 'label' => $post->get_title(),
777 'value' => (string) $post->get_id(),
778 ];
779 }
780 } else {
781 $result[] = [
782 'label' => $post->get_title(),
783 'value' => (string) $post->get_id(),
784 ];
785 }
786 }
787
788 return [
789 'options' => $result,
790 'hasMore' => $subscription_products['has_more'],
791 ];
792 }
793
794 /**
795 * Prepare WooCommerce Payment Methods.
796 *
797 * @param array $data Search Params.
798 * @return array[]
799 */
800 public function search_woo_payment_methods( $data ) {
801 $payment_methods = WC()->payment_gateways->get_available_payment_gateways();
802 $options = [];
803
804 if ( ! empty( $payment_methods ) ) {
805 foreach ( $payment_methods as $payment ) {
806 $options[] = [
807 'label' => $payment->title,
808 'value' => $payment->id,
809 ];
810 }
811 }
812
813 return [
814 'options' => $options,
815 'hasMore' => false,
816 ];
817 }
818
819 /**
820 * Prepare WooCommerce Order Status List.
821 *
822 * @param array $data Search Params.
823 * @return array[]
824 */
825 public function search_woo_order_status_list( $data ) {
826 $order_status = wc_get_order_statuses();
827 $options = [];
828
829 if ( ! empty( $order_status ) ) {
830 foreach ( $order_status as $key => $status ) {
831 $options[] = [
832 'label' => $status,
833 'value' => $key,
834 ];
835 }
836 }
837
838 return [
839 'options' => $options,
840 'hasMore' => false,
841 ];
842 }
843
844 /**
845 * Prepare WooCommerce Country List.
846 *
847 * @param array $data Search Params.
848 * @return array[]
849 */
850 public function search_woo_country_list( $data ) {
851 $countries = WC()->countries->get_countries();
852 $options = [];
853
854 if ( ! empty( $countries ) ) {
855 foreach ( $countries as $key => $country ) {
856 $options[] = [
857 'label' => $country,
858 'value' => $key,
859 ];
860 }
861 }
862
863 return [
864 'options' => $options,
865 'hasMore' => false,
866 ];
867 }
868
869 /**
870 * Prepare WooCommerce Country States List.
871 *
872 * @param array $data Search Params.
873 * @return array[]
874 */
875 public function search_woo_country_state_list( $data ) {
876 if ( ! empty( $data['dynamic']['shipping_country'] ) ) {
877 $cc = $data['dynamic']['shipping_country'];
878 } else {
879 $cc = $data['dynamic'];
880 }
881
882 $states = WC()->countries->get_states( $cc );
883
884 $options = [];
885 if ( ! empty( $states ) ) {
886 foreach ( $states as $key => $state ) {
887 $options[] = [
888 'label' => $state,
889 'value' => $key,
890 ];
891 }
892 }
893
894 return [
895 'options' => $options,
896 'hasMore' => false,
897 ];
898 }
899
900 /**
901 * Get Memberpress gatways (payment methods) for subscription.
902 *
903 * @param array $data QueryParams.
904 *
905 * @return array
906 * @since 1.0.0
907 */
908 public function search_memberpress_gayways( $data ) {
909 $mp_options = MeprOptions::fetch();
910
911 $pms = array_keys( $mp_options->integrations );
912 $gateways = [];
913
914 foreach ( $pms as $pm_id ) {
915 $obj = $mp_options->payment_method( $pm_id );
916 if ( $obj instanceof MeprBaseRealGateway ) {
917 $gateways[] = [
918 'label' => sprintf( '%1$s (%2$s)', $obj->label, $obj->name ),
919 'value' => $obj->id,
920 ];
921 }
922 }
923
924 return [
925 'options' => $gateways,
926 'hasMore' => false,
927 ];
928 }
929
930 /**
931 * Prepare roles.
932 *
933 * @param array $data data.
934 *
935 * @return array
936 */
937 public function search_roles( $data ) {
938 $roles = wp_roles()->roles;
939 $options = [];
940 foreach ( $roles as $role => $details ) {
941
942 $options[] = [
943 'label' => $details['name'],
944 'value' => $role,
945 ];
946
947 }
948
949 return [
950 'options' => $options,
951 'hasMore' => false,
952 ];
953 }
954
955 /**
956 * Prepare post types.
957 *
958 * @param array $data data.
959 *
960 * @return array
961 */
962 public function search_post_types( $data ) {
963 $post_types = get_post_types( [ 'public' => true ], 'objects' );
964 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
965 if ( isset( $post_types['attachment'] ) ) {
966 unset( $post_types['attachment'] );
967 }
968
969 $options = [];
970 foreach ( $post_types as $post_type => $details ) {
971 $options[] = [
972 'label' => $details->label,
973 'value' => $post_type,
974 ];
975 }
976
977 return [
978 'options' => $options,
979 'hasMore' => false,
980 ];
981 }
982
983 /**
984 * Get post statuses.
985 *
986 * @param array $data data.
987 *
988 * @return array
989 */
990 public function search_post_statuses( $data ) {
991 $post_statuses = get_post_stati( [], 'objects' );
992 $post_statuses = apply_filters( 'suretriggers_post_types', $post_statuses );
993 $options = [];
994
995 foreach ( $post_statuses as $post_status => $details ) {
996 if ( 'woocommerce' === $details->label_count['domain'] ) {
997 $options[] = [
998 'label' => 'WooCommerce - ' . $details->label,
999 'value' => $post_status,
1000 ];
1001 } else {
1002 $options[] = [
1003 'label' => $details->label,
1004 'value' => $post_status,
1005 ];
1006 }
1007 }
1008
1009 return [
1010 'options' => $options,
1011 'hasMore' => false,
1012 ];
1013 }
1014
1015 /**
1016 * Get Taxonomies.
1017 *
1018 * @param array $data data.
1019 *
1020 * @return array
1021 */
1022 public function search_taxonomy_list( $data ) {
1023 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
1024 $options = [];
1025 $options[0] = [
1026 'label' => 'Any Taxonomy',
1027 'value' => -1,
1028 ];
1029
1030 foreach ( $taxonomies as $taxonomy => $taxonomy_obj ) {
1031 $options[] = [
1032 'label' => $taxonomy_obj->label,
1033 'value' => $taxonomy_obj->name,
1034 ];
1035 }
1036
1037 return [
1038 'options' => $options,
1039 'hasMore' => false,
1040 ];
1041 }
1042
1043 /**
1044 * Get Taxonomies without any option.
1045 *
1046 * @param array $data data.
1047 *
1048 * @return array
1049 */
1050 public function search_taxonomy_list_without_any( $data ) {
1051 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
1052 $options = [];
1053 foreach ( $taxonomies as $taxonomy => $taxonomy_obj ) {
1054 $options[] = [
1055 'label' => $taxonomy_obj->label,
1056 'value' => $taxonomy_obj->name,
1057 ];
1058 }
1059
1060 return [
1061 'options' => $options,
1062 'hasMore' => false,
1063 ];
1064 }
1065
1066 /**
1067 * Get WPForms.
1068 *
1069 * @param array $data data.
1070 *
1071 * @return array
1072 */
1073 public function search_wp_forms( $data ) {
1074 if ( ! class_exists( 'WPForms_Form_Handler' ) ) {
1075 return;
1076 }
1077
1078 $wpforms = new WPForms_Form_Handler();
1079 $forms = $wpforms->get( '', [ 'orderby' => 'title' ] );
1080 $options = [];
1081
1082 if ( ! empty( $forms ) ) {
1083 foreach ( $forms as $form ) {
1084 $options[] = [
1085 'label' => $form->post_title,
1086 'value' => $form->ID,
1087 ];
1088 }
1089 }
1090
1091 return [
1092 'options' => $options,
1093 'hasMore' => false,
1094 ];
1095 }
1096
1097 /**
1098 * Get Gravity Forms.
1099 *
1100 * @param array $data data.
1101 *
1102 * @return array
1103 */
1104 public function search_gravity_forms( $data ) {
1105 if ( ! class_exists( 'GFFormsModel' ) ) {
1106 return;
1107 }
1108
1109 $forms = GFFormsModel::get_forms();
1110 $options = [];
1111
1112 if ( ! empty( $forms ) ) {
1113 foreach ( $forms as $form ) {
1114 $options[] = [
1115 'label' => $form->title,
1116 'value' => $form->id,
1117 ];
1118 }
1119 }
1120
1121 return [
1122 'options' => $options,
1123 'hasMore' => false,
1124 ];
1125 }
1126
1127 /**
1128 * Get tag & contact details.
1129 *
1130 * @param array $data data.
1131 *
1132 * @return array
1133 */
1134 public function search_pluggables_fluentcrm_contact_added_to_tags( $data ) {
1135 $context = [];
1136 $pluggable_data = [];
1137 $tag_id = $data['filter'];
1138
1139 if ( ! class_exists( 'FluentCrm\App\Models\Subscriber' ) || ! class_exists( 'FluentCrm\App\Models\Tag' ) ) {
1140 return [];
1141 }
1142
1143 if ( $tag_id > 0 ) {
1144 $tags = Tag::where( 'id', $tag_id )->first();
1145 } else {
1146 $tags = Tag::orderBy( 'id', 'DESC' )->first();
1147 }
1148 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1149 if ( $contact ) {
1150 $pluggable_data['contact'] = $contact;
1151 $context['tag_id'] = $tag_id;
1152 $pluggable_data['tag'] = $tags;
1153 $context['response_type'] = 'live';
1154 } else {
1155 $pluggable_data['conatct']['full_name'] = 'Test User';
1156 $pluggable_data['conatct']['first_name'] = 'Test';
1157 $pluggable_data['conatct']['last_name'] = 'User';
1158 $pluggable_data['conatct']['company_id'] = 112;
1159 $pluggable_data['conatct']['email'] = 'testuser@gmail.com';
1160 $pluggable_data['conatct']['address_line_1'] = '33, Vincent Road';
1161 $pluggable_data['conatct']['address_line_2'] = 'Chicago Street';
1162 $pluggable_data['conatct']['postal_code'] = '212342';
1163 $pluggable_data['conatct']['city'] = 'New York City';
1164 $pluggable_data['conatct']['state'] = 'New York';
1165 $pluggable_data['conatct']['country'] = 'USA';
1166 $pluggable_data['conatct']['phone'] = '9992191911';
1167 $pluggable_data['conatct']['status'] = 'subscribed';
1168 $pluggable_data['conatct']['contact_type'] = 'lead';
1169 $pluggable_data['conatct']['source'] = '';
1170 $pluggable_data['conatct']['date_of_birth'] = '2022-11-09';
1171 $context['tag_id'] = 1;
1172 $pluggable_data['tag'] =
1173 [
1174 'id' => '1',
1175 'title' => 'new',
1176 'slug' => 'new',
1177 'description' => null,
1178 'created_at' => '2023-01-19 10:23:23',
1179 'updated_at' => '2023-01-19 10:23:23',
1180 'pivot' => [
1181 'subscriber_id' => '1',
1182 'object_id' => '1',
1183 'object_type' => 'FluentCrm\\App\\Models\\Tag',
1184 'created_at' => '2023-01-19 10:42:55',
1185 'updated_at' => '2023-01-19 10:42:55',
1186
1187 ],
1188 ];
1189 $context['response_type'] = 'sample';
1190 }
1191
1192 $context['pluggable_data'] = $pluggable_data;
1193 return $context;
1194 }
1195
1196 /**
1197 * Get FluentCRM contact details.
1198 *
1199 * @param array $data data.
1200 *
1201 * @return array
1202 */
1203 public function search_pluggables_fluentcrm_contact_added( $data ) {
1204 $context = [];
1205 $pluggable_data = [];
1206
1207 if ( ! class_exists( 'FluentCrm\App\Models\Subscriber' ) || ! class_exists( 'FluentCrm\App\Models\SubscriberMeta' ) ) {
1208 return [];
1209 }
1210 $contact = [];
1211 if ( 'status_set_to_specific_status' === $data['search_term'] ) {
1212 if ( '-1' === $data['filter']['status']['value'] ) {
1213 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1214 } else {
1215 $contact = Subscriber::where( 'status', $data['filter']['status']['value'] )->first();
1216 }
1217 } elseif ( 'new_contact_added' === $data['search_term'] ) {
1218 $contact = Subscriber::orderBy( 'id', 'DESC' )->first();
1219 } elseif ( 'contact_updated' === $data['search_term'] ) {
1220 $contact = Subscriber::orderBy( 'updated_at', 'DESC' )->first();
1221 } elseif ( 'contact_field_updated' === $data['search_term'] ) {
1222 if ( '-1' === $data['filter']['field_id']['value'] ) {
1223 $contact = SubscriberMeta::where( 'object_type', 'custom_field' )->orderBy( 'updated_at', 'DESC' )->first();
1224 } else {
1225 $contact = SubscriberMeta::where( 'key', $data['filter']['field_id']['value'] )->orderBy( 'updated_at', 'DESC' )->first();
1226 }
1227 $contact = Subscriber::where( 'id', $contact->subscriber_id )->first();
1228 }
1229
1230 if ( $contact ) {
1231 $subscriber = Subscriber::with( [ 'tags', 'lists' ] )->find( $contact->id );
1232 $customer_fields = $subscriber->custom_fields();
1233 $pluggable_data['contact']['details'] = $subscriber;
1234 $pluggable_data['contact']['custom'] = $customer_fields;
1235 $pluggable_data['field_id'] = $data['filter']['field_id']['value'];
1236 $context['response_type'] = 'live';
1237 } else {
1238 $pluggable_data['contact']['details']['full_name'] = 'Test User';
1239 $pluggable_data['contact']['details']['first_name'] = 'Test';
1240 $pluggable_data['contact']['details']['last_name'] = 'User';
1241 $pluggable_data['contact']['details']['company_id'] = 112;
1242 $pluggable_data['contact']['details']['email'] = 'testuser@gmail.com';
1243 $pluggable_data['contact']['details']['address_line_1'] = '33, Vincent Road';
1244 $pluggable_data['contact']['details']['address_line_2'] = 'Chicago Street';
1245 $pluggable_data['contact']['details']['postal_code'] = '212342';
1246 $pluggable_data['contact']['details']['city'] = 'New York City';
1247 $pluggable_data['contact']['details']['state'] = 'New York';
1248 $pluggable_data['contact']['details']['country'] = 'USA';
1249 $pluggable_data['contact']['details']['phone'] = '9992191911';
1250 $pluggable_data['contact']['details']['status'] = 'subscribed';
1251 $pluggable_data['contact']['details']['contact_type'] = 'lead';
1252 $pluggable_data['contact']['details']['source'] = '';
1253 $pluggable_data['contact']['details']['date_of_birth'] = '2022-11-09';
1254 $context['response_type'] = 'sample';
1255 }
1256
1257 $context['pluggable_data'] = $pluggable_data;
1258 return $context;
1259 }
1260
1261 /**
1262 * Get contact added to list details.
1263 *
1264 * @param array $data data.
1265 *
1266 * @return array
1267 */
1268 public function search_pluggables_fluentcrm_contact_added_to_lists( $data ) {
1269 $context = [];
1270 $pluggable_data = [];
1271 $list_id = $data['filter'];
1272
1273 if ( ! class_exists( 'FluentCrm\App\Models\Lists' ) || ! class_exists( 'FluentCrm\App\Models\Subscriber' ) ) {
1274 return [];
1275 }
1276
1277 $contact_api = FluentCrmApi( 'contacts' );
1278 if ( -1 === $list_id ) {
1279 $lists = Lists::orderBy( 'id', 'DESC' )->first();
1280 $list_id = $lists->id;
1281 } else {
1282 $lists = Lists::where( 'id', $list_id )->first();
1283 }
1284 $list_ids = [ $list_id ];
1285 $list_contacts = $contact_api->getInstance()
1286 ->filterByLists( $list_ids )
1287 ->orderBy( 'id', 'DESC' )
1288 ->first();
1289
1290 $contact = Subscriber::where( 'id', $list_contacts->id )->get();
1291 if ( $contact ) {
1292 $pluggable_data['contact'] = $contact[0];
1293 $pluggable_data['list_id'] = $list_id;
1294 $pluggable_data['list'] = $lists;
1295 $context['response_type'] = 'live';
1296 } else {
1297 $pluggable_data['conatct']['id'] = 6;
1298 $pluggable_data['conatct']['prefix'] = 'Mr';
1299 $pluggable_data['conatct']['full_name'] = 'John Doe';
1300 $pluggable_data['conatct']['first_name'] = 'John';
1301 $pluggable_data['conatct']['last_name'] = 'Doe';
1302 $pluggable_data['conatct']['company_id'] = 112;
1303 $pluggable_data['conatct']['email'] = 'johnde@gmail.com';
1304 $pluggable_data['conatct']['address_line_1'] = '33, Vincent Road';
1305 $pluggable_data['conatct']['address_line_2'] = 'Chicago Street';
1306 $pluggable_data['conatct']['postal_code'] = '212342';
1307 $pluggable_data['conatct']['city'] = 'New York City';
1308 $pluggable_data['conatct']['state'] = 'New York';
1309 $pluggable_data['conatct']['country'] = 'USA';
1310 $pluggable_data['conatct']['phone'] = '9992191911';
1311 $pluggable_data['conatct']['status'] = 'subscribed';
1312 $pluggable_data['conatct']['contact_type'] = 'lead';
1313 $pluggable_data['conatct']['source'] = '';
1314 $pluggable_data['conatct']['date_of_birth'] = '2022-11-09';
1315 $context['list_id'] = 1;
1316 $pluggable_data['list'] =
1317 [
1318 'id' => '1',
1319 'title' => 'new',
1320 'slug' => 'new',
1321 'description' => null,
1322 'created_at' => '2023-01-19 10:23:23',
1323 'updated_at' => '2023-01-19 10:23:23',
1324 ];
1325 $context['response_type'] = 'sample';
1326 }
1327
1328 $context['pluggable_data'] = $pluggable_data;
1329 return $context;
1330 }
1331
1332 /**
1333 * Prepare fluentcrm campaigns.
1334 *
1335 * @param array $data Search Params.
1336 *
1337 * @return array
1338 */
1339 public function search_fluentcrm_campaigns( $data ) {
1340
1341 $options = [];
1342 global $wpdb;
1343
1344 $campaigns = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fc_campaigns ORDER BY id DESC", ARRAY_A );
1345
1346 if ( ! empty( $campaigns ) ) {
1347 foreach ( $campaigns as $campaign ) {
1348 $options[] = [
1349 'label' => $campaign['title'],
1350 'value' => $campaign['id'],
1351 ];
1352 }
1353 }
1354
1355 return [
1356 'options' => $options,
1357 'hasMore' => false,
1358 ];
1359 }
1360
1361 /**
1362 * Get Divi Forms.
1363 *
1364 * @param array $data data.
1365 *
1366 * @return array
1367 */
1368 public function search_divi_forms( $data ) {
1369 $form_posts = Utilities::get_divi_forms();
1370 $options = [];
1371
1372 if ( empty( $form_posts ) ) {
1373 return [
1374 'options' => [],
1375 'hasMore' => false,
1376 ];
1377 }
1378
1379 foreach ( (array) $form_posts as $form_post ) {
1380 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
1381 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
1382 if ( empty( $forms ) ) {
1383 continue;
1384 }
1385
1386 $count = 0;
1387
1388 foreach ( $forms as $form ) {
1389 $pattern_form = get_shortcode_regex( [ 'et_pb_contact_form' ] );
1390 preg_match_all( "/$pattern_form/", $form[0], $forms_extracted, PREG_SET_ORDER );
1391
1392 if ( empty( $forms_extracted ) ) {
1393 continue;
1394 }
1395
1396 foreach ( $forms_extracted as $form_extracted ) {
1397 $form_attrs = shortcode_parse_atts( $form_extracted[3] );
1398 $form_id = isset( $form_attrs['_unique_id'] ) ? $form_attrs['_unique_id'] : '';
1399 if ( empty( $form_id ) ) {
1400 continue;
1401 }
1402 $form_id = sprintf( '%d-%s', $form_post['ID'], $form_id );
1403 $form_title = isset( $form_attrs['title'] ) ? $form_attrs['title'] : '';
1404 $form_title = sprintf( '%s %s', $form_post['post_title'], $form_title );
1405 $options[] = [
1406 'label' => $form_title,
1407 'value' => $form_id,
1408 ];
1409 }
1410 $count++;
1411 }
1412 }
1413
1414 return [
1415 'options' => $options,
1416 'hasMore' => false,
1417 ];
1418 }
1419
1420 /**
1421 * Get Comment Pluggable data.
1422 *
1423 * @param array $data data.
1424 *
1425 * @return array
1426 */
1427 public function search_pluggables_wp_insert_comment( $data ) {
1428 $context = [];
1429 $args = [
1430 'number' => '1',
1431 'status' => 'approve',
1432 'post_type' => $data['filter']['post_type']['value'],
1433 ];
1434
1435 if ( isset( $data['filter']['post']['value'] ) ) {
1436 $post_id = $data['filter']['post']['value'];
1437 if ( $post_id > 0 ) {
1438 $args['post_id'] = $post_id;
1439 }
1440 }
1441
1442 $comments = get_comments( $args );
1443
1444 if ( empty( $comments ) ) {
1445 unset( $args['post_id'] );
1446 $comments = get_comments( $args );
1447 }
1448
1449 $context['context_data'] = $data;
1450 $context['context_args'] = $args;
1451
1452 if ( ! empty( $comments ) ) {
1453 foreach ( $comments as $comment ) :
1454 if ( is_object( $comment ) ) {
1455 $comment = get_object_vars( $comment );
1456 }
1457
1458 if ( is_array( $comment ) && isset( $comment['comment_post_ID'] ) && is_numeric( $comment['comment_post_ID'] ) ) {
1459 $post_id = absint( $comment['comment_post_ID'] );
1460 $post = get_post( $post_id );
1461
1462 if ( is_object( $post ) ) {
1463 $postauthor = (int) $post->post_author;
1464
1465 $context['pluggable_data'] = [
1466 'post' => $post_id,
1467 'post_title' => $post->post_title,
1468 'post_author' => get_the_author_meta( 'display_name', $postauthor ),
1469 'post_link' => get_the_permalink( $post_id ),
1470 'comment_id' => $comment['comment_ID'],
1471 'comment' => $comment['comment_content'],
1472 'comment_author' => $comment['comment_author'],
1473 'comment_author_email' => $comment['comment_author_email'],
1474 'comment_date' => $comment['comment_date'],
1475 ];
1476 }
1477 }
1478
1479 if ( is_array( $comment ) && isset( $comment['comment_author_email'] ) ) {
1480 $user_email = $comment['comment_author_email'];
1481 /**
1482 *
1483 * Ignore line
1484 *
1485 * @phpstan-ignore-next-line
1486 */
1487 $user = get_user_by( 'email', $user_email );
1488
1489 if ( $user ) {
1490
1491 $context['pluggable_data']['wp_user_id'] = $user->ID;
1492 $context['pluggable_data']['user_login'] = $user->user_login;
1493 $context['pluggable_data']['display_name'] = $user->display_name;
1494 $context['pluggable_data']['user_firstname'] = $user->user_firstname;
1495 $context['pluggable_data']['user_lastname'] = $user->user_lastname;
1496 $context['pluggable_data']['user_email'] = $user->user_email;
1497 $context['pluggable_data']['user_role'] = $user->roles;
1498 }
1499 }
1500
1501 $context['response_type'] = 'live';
1502 endforeach;
1503 } else {
1504
1505 $sample_comment = [
1506 'post' => 100,
1507 'post_title' => 'Sample Post',
1508 'comment_id' => 101,
1509 'comment' => 'Sample Comment',
1510 'post_author' => 'Sample Author',
1511 'post_link' => 'https://example.com/sample-post',
1512 'comment_author' => 'Test User',
1513 'comment_author_email' => 'testuser@example.com',
1514 'comment_date' => '2024-01-01 10:00:00',
1515 ];
1516 $sample_comment['wp_user_id'] = 7;
1517 $sample_comment['user_login'] = 'testuser@gmail.com';
1518 $sample_comment['display_name'] = 'Test User';
1519 $sample_comment['user_firstname'] = 'Test';
1520 $sample_comment['user_lastname'] = 'User';
1521 $sample_comment['user_email'] = 'testuser@gmail.com';
1522 $sample_comment['user_role'] = [ 'Subscriber' ];
1523
1524 $context['pluggable_data'] = $sample_comment;
1525 $context['response_type'] = 'sample';
1526 }
1527
1528 return $context;
1529 }
1530
1531
1532 /**
1533 * User reset password.
1534 *
1535 * @param array $data data.
1536 * @return array
1537 */
1538 public function search_pluggables_user_reset_password( $data ) {
1539 $user_context = $this->search_pluggables_add_user_role( $data );
1540 $user_context['pluggable_data']['new_password'] = '***password***';
1541 return $user_context;
1542 }
1543
1544 /**
1545 * User pluggable data.
1546 *
1547 * @param array $data data.
1548 * @return array
1549 */
1550 public function search_pluggables_add_user_role( $data ) {
1551 $context = [];
1552 $args = [
1553 'order' => 'DESC',
1554 'number' => 1,
1555 'orderby' => 'ID',
1556 ];
1557
1558 if ( isset( $data['filter']['role']['value'] ) ) {
1559 $role = $data['filter']['role']['value'];
1560 $args['role'] = $role;
1561 }
1562 if ( isset( $data['filter']['new_role']['value'] ) ) {
1563 $role = $data['filter']['new_role']['value'];
1564 $args['role'] = $role;
1565 }
1566
1567 $users = get_users( $args );
1568
1569 if ( isset( $data['filter']['meta_key']['value'] ) ) {
1570 $meta_key = $data['filter']['meta_key']['value'];
1571 $args['st_meta_key'] = $meta_key;
1572 }
1573
1574 if ( isset( $data['filter']['profile_field']['value'] ) ) {
1575 $meta_key = $data['filter']['profile_field']['value'];
1576 $args['profile_field'] = $meta_key;
1577 }
1578
1579 if ( ! empty( $users ) ) {
1580 $user = $users[0];
1581 $pluggable_data = [];
1582 $pluggable_data['wp_user_id'] = $user->ID;
1583 $pluggable_data['user_login'] = $user->user_login;
1584 $pluggable_data['display_name'] = $user->display_name;
1585 $pluggable_data['user_firstname'] = $user->user_firstname;
1586 $pluggable_data['user_lastname'] = $user->user_lastname;
1587 $pluggable_data['user_email'] = $user->user_email;
1588 $pluggable_data['user_registered'] = $user->user_registered;
1589 $pluggable_data['user_role'] = $user->roles;
1590 if ( isset( $args['st_meta_key'] ) ) {
1591 $pluggable_data['meta_key'] = $args['st_meta_key'];
1592 $pluggable_data['meta_value'] = get_user_meta( $user->ID, $args['st_meta_key'], true );
1593 }
1594 if ( isset( $args['profile_field'] ) ) {
1595 $userdata = get_userdata( $user->ID );
1596 $userdata = json_decode( wp_json_encode( $userdata->data ), true );
1597
1598 $pluggable_data['profile_field'] = $args['profile_field'];
1599 $pluggable_data['profile_field_value'] = $userdata[ $args['profile_field'] ];
1600 }
1601 $context['pluggable_data'] = $pluggable_data;
1602 $context['response_type'] = 'live';
1603 } else {
1604 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
1605 $context['pluggable_data'] = [
1606 'wp_user_id' => 1,
1607 'user_login' => 'admin',
1608 'display_name' => 'Test User',
1609 'user_firstname' => 'Test',
1610 'user_lastname' => 'User',
1611 'user_email' => 'testuser@gmail.com',
1612 'user_registered' => '2024-06-18 09:47:58',
1613 'user_role' => [ $role ],
1614 ];
1615 if ( isset( $args['st_meta_key'] ) ) {
1616 $context['pluggable_data']['meta_key'] = $args['st_meta_key'];
1617 $context['pluggable_data']['meta_value'] = 'test meta value';
1618 }
1619 if ( isset( $args['profile_field'] ) ) {
1620 $context['pluggable_data']['profile_field'] = $args['profile_field'];
1621 $context['pluggable_data']['profile_field_value'] = 'Profile Field Value';
1622 }
1623 $context['response_type'] = 'sample';
1624 }
1625 return $context;
1626 }
1627
1628 /**
1629 * User pluggable data.
1630 *
1631 * @param array $data data.
1632 * @return array
1633 */
1634 public function search_pluggables_last_user_login( $data ) {
1635 $context = [];
1636 $args = [
1637 'orderby' => 'meta_value',
1638 'meta_key' => 'st_last_login',
1639 'order' => 'DESC',
1640 'number' => 1,
1641 ];
1642 $users = get_users( $args );
1643
1644 if ( ! empty( $users ) ) {
1645 $user = $users[0];
1646 $pluggable_data = [];
1647 $pluggable_data['wp_user_id'] = $user->ID;
1648 $pluggable_data['user_login'] = $user->user_login;
1649 $pluggable_data['display_name'] = $user->display_name;
1650 $pluggable_data['user_firstname'] = $user->user_firstname;
1651 $pluggable_data['user_lastname'] = $user->user_lastname;
1652 $pluggable_data['user_email'] = $user->user_email;
1653 $pluggable_data['user_role'] = $user->roles;
1654
1655 $context['pluggable_data'] = $pluggable_data;
1656 $context['response_type'] = 'live';
1657 } else {
1658 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
1659 $context['pluggable_data'] = [
1660 'wp_user_id' => 1,
1661 'user_login' => 'admin',
1662 'display_name' => 'Test User',
1663 'user_firstname' => 'Test',
1664 'user_lastname' => 'User',
1665 'user_email' => 'testuser@gmail.com',
1666 'user_role' => [ $role ],
1667 ];
1668 $context['response_type'] = 'sample';
1669 }
1670 return $context;
1671 }
1672
1673 /**
1674 * Donation pluggable data.
1675 *
1676 * @param array $data data.
1677 * @return array
1678 */
1679 public function search_pluggables_wordpress_post( $data ) {
1680 $context = [];
1681 $args = [
1682 'post_type' => 'any',
1683 'posts_per_page' => 1,
1684 'orderby' => 'modified',
1685 'order' => 'DESC',
1686 ];
1687
1688 if ( isset( $data['filter']['post_type']['value'] ) ) {
1689 $post_type = $data['filter']['post_type']['value'];
1690 $args['post_type'] = $post_type;
1691 }
1692
1693 if ( isset( $data['filter']['status']['value'] ) ) {
1694 $post_status = $data['filter']['status']['value'];
1695 $args['post_status'] = $post_status;
1696 }
1697
1698 if ( isset( $data['filter']['post_status']['value'] ) ) {
1699 $post_status = $data['filter']['post_status']['value'];
1700 $args['post_status'] = $post_status;
1701 }
1702
1703 if ( isset( $data['filter']['post']['value'] ) ) {
1704 $post_id = $data['filter']['post']['value'];
1705 if ( $post_id > 0 ) {
1706 $args['p'] = $post_id;
1707 unset( $args['post_status'] );
1708 }
1709 }
1710
1711 $posts = get_posts( $args );
1712 if ( ! empty( $posts ) ) {
1713 $context['pluggable_data'] = $posts[0];
1714 $custom_metas = get_post_meta( $posts[0]->ID );
1715 if ( property_exists( $context['pluggable_data'], 'post' ) ) {
1716 $context['pluggable_data']->post = $posts[0]->ID;
1717 }
1718 if ( is_object( $context['pluggable_data'] ) ) {
1719 $context['pluggable_data'] = get_object_vars( $context['pluggable_data'] );
1720 }
1721 $context['pluggable_data']['featured_image'] = wp_get_attachment_image_src( (int) get_post_thumbnail_id( $posts[0]->ID ), 'full' )[0]; // @phpstan-ignore-line
1722 if ( $posts[0] instanceof WP_Post ) {
1723 $taxonomies = get_object_taxonomies( get_post( $posts[0] ), 'objects' );
1724 if ( ! empty( $taxonomies ) ) {
1725 foreach ( $taxonomies as $taxonomy => $taxonomy_object ) {
1726 $terms = get_the_terms( $posts[0]->ID, $taxonomy );
1727 if ( ! empty( $terms ) && is_array( $terms ) ) {
1728 foreach ( $terms as $term ) {
1729 $context['pluggable_data'][ $taxonomy ] = $term->name;
1730 }
1731 }
1732 }
1733 }
1734 }
1735 $context['pluggable_data'] = array_merge( $context['pluggable_data'], WordPress::get_user_context( $posts[0]->post_author ) );
1736 $context['pluggable_data']['post'] = $posts[0]->ID;
1737 $context['pluggable_data']['custom_metas'] = $custom_metas;
1738 $context['response_type'] = 'live';
1739 } else {
1740 $context['pluggable_data'] = [
1741 'ID' => 557,
1742 'post' => 557,
1743 'post_author' => 1,
1744 'post_date' => '2022-11-18 12:18:14',
1745 'post_date_gmt' => '2022-11-18 12:18:14',
1746 'post_content' => 'Test Post Content',
1747 'post_title' => 'Test Post',
1748 'post_excerpt' => '',
1749 'post_status' => $args['post_status'],
1750 'comment_status' => 'open',
1751 'ping_status' => 'open',
1752 'post_password' => '',
1753 'post_name' => 'test-post',
1754 'to_ping' => '',
1755 'pinged' => '',
1756 'post_modified' => '2022-11-18 12:18:14',
1757 'post_modified_gmt' => '2022-11-18 12:18:14',
1758 'post_content_filtered' => '',
1759 'post_parent' => 0,
1760 'guid' => 'https://abc.com/test-post/',
1761 'menu_order' => 0,
1762 'post_type' => 'post',
1763 'post_mime_type' => '',
1764 'comment_count' => 0,
1765 'filter' => 'raw',
1766 'featured_image' => 'https://abc.com/test-post/',
1767 ];
1768 $context['response_type'] = 'sample';
1769 }
1770
1771 return $context;
1772 }
1773
1774 /**
1775 * Donation pluggable data
1776 *
1777 * @param array $data data.
1778 * @return array
1779 */
1780 public function search_pluggables_givewp_donation_via_form( $data ) {
1781 global $wpdb;
1782 $context = [];
1783 $pluggable_data = [];
1784 $form_id = $data['filter']['form_id']['value'];
1785 $subscription_id = 1;
1786
1787 if ( ! class_exists( 'Give_Payment' ) || ! class_exists( 'Give_Subscription' ) ) {
1788 return [];
1789 }
1790
1791 if ( ! function_exists( 'give_get_donor_donation_comment' ) ) {
1792 return [];
1793 }
1794
1795 if ( 'donation_specific_field_value' == $data['search_term'] ) {
1796 $field_id = $data['filter']['field_id']['value'];
1797 $field_value = $data['filter']['field_value']['value'];
1798
1799 $donation_meta = $wpdb->get_row(
1800 $wpdb->prepare(
1801 "SELECT * FROM {$wpdb->prefix}give_donationmeta
1802 WHERE meta_key LIKE %s AND
1803 meta_value LIKE %s
1804 ORDER BY donation_id
1805 DESC LIMIT 1",
1806 $field_id,
1807 $field_value
1808 )
1809 );
1810 if ( $donation_meta ) {
1811 $donation_form_meta = $wpdb->get_row(
1812 $wpdb->prepare(
1813 "SELECT * FROM {$wpdb->prefix}give_donationmeta
1814 WHERE donation_id = %d AND
1815 meta_key LIKE %s AND
1816 meta_value LIKE %d
1817 ORDER BY donation_id
1818 DESC LIMIT 1",
1819 $donation_meta->donation_id,
1820 '_give_payment_form_id',
1821 $form_id
1822 )
1823 );
1824
1825 $payment = $wpdb->get_row(
1826 $wpdb->prepare(
1827 "SELECT * FROM {$wpdb->prefix}posts WHERE
1828 ID=%d ORDER BY ID DESC LIMIT 1",
1829 $donation_form_meta->donation_id
1830 )
1831 );
1832 }
1833 } elseif ( 'donation_specific_amount' == $data['search_term'] ) {
1834 $condition_compare = $data['filter']['condition_compare']['value'];
1835 $donation_amount = $data['filter']['donation_amount']['value'];
1836
1837 $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
1838
1839 if ( $donation_meta ) {
1840 $donation_form_meta = $wpdb->get_row(
1841 $wpdb->prepare(
1842 "SELECT * FROM {$wpdb->prefix}give_donationmeta
1843 WHERE donation_id = %d AND
1844 meta_key LIKE %s AND
1845 meta_value LIKE %d
1846 ORDER BY donation_id
1847 DESC LIMIT 1",
1848 $donation_meta->donation_id,
1849 '_give_payment_form_id',
1850 $form_id
1851 )
1852 );
1853
1854 $payment = $wpdb->get_row(
1855 $wpdb->prepare(
1856 "SELECT * FROM {$wpdb->prefix}posts WHERE
1857 ID=%d ORDER BY ID DESC LIMIT 1",
1858 $donation_form_meta->donation_id
1859 )
1860 );
1861 }
1862 } elseif ( 'cancels_recurring_donation' == $data['search_term'] ) {
1863 $donation_meta = $wpdb->get_row(
1864 $wpdb->prepare(
1865 "SELECT * FROM {$wpdb->prefix}give_subscriptions
1866 WHERE product_id=%d AND
1867 status LIKE %s
1868 ORDER BY id
1869 DESC LIMIT 1",
1870 $form_id,
1871 'cancelled'
1872 )
1873 );
1874 if ( $donation_meta ) {
1875 $subscription_id = $donation_meta->id;
1876 $payment = $wpdb->get_row(
1877 $wpdb->prepare(
1878 "SELECT * FROM {$wpdb->prefix}posts WHERE
1879 ID=%d ORDER BY ID DESC LIMIT 1",
1880 $donation_meta->parent_payment_id
1881 )
1882 );
1883 }
1884 } elseif ( 'continues_recurring_donation' == $data['search_term'] ) {
1885 $donation_meta = $wpdb->get_row(
1886 $wpdb->prepare(
1887 "SELECT * FROM {$wpdb->prefix}give_subscriptions
1888 WHERE product_id=%d AND
1889 status LIKE %s
1890 ORDER BY id
1891 DESC LIMIT 1",
1892 $form_id,
1893 'active'
1894 )
1895 );
1896 if ( $donation_meta ) {
1897 $subscription_id = $donation_meta->id;
1898
1899 $payment = $wpdb->get_row(
1900 $wpdb->prepare(
1901 "SELECT * FROM {$wpdb->prefix}posts
1902 WHERE post_parent=%d AND post_status LIKE %s
1903 ORDER BY ID DESC LIMIT 1",
1904 $donation_meta->parent_payment_id,
1905 'give_subscription'
1906 )
1907 );
1908 }
1909 } else {
1910 $payment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type=%s ORDER BY id DESC LIMIT 1", 'give_payment' ) );
1911 }
1912
1913 if ( ! empty( $payment ) ) {
1914 if ( 'continues_recurring_donation' == $data['search_term'] || 'cancels_recurring_donation' == $data['search_term'] ) {
1915 $subscription = new Give_Subscription( $subscription_id );
1916
1917 $pluggable_data['form_id'] = $form_id;
1918 $pluggable_data['subscription'] = $subscription;
1919 } elseif ( 'donation_specific_field_value' == $data['search_term'] ) {
1920 $payment_data = new Give_Payment( $payment->ID );
1921 $input_array = $payment_data->payment_meta;
1922 unset( $input_array['user_info'] );
1923 $pluggable_data = $input_array;
1924 foreach ( $input_array as $key => $value ) {
1925 $pluggable_data['field_id'] = $key;
1926 $pluggable_data['field_value'] = $value;
1927 }
1928 } else {
1929 $payment = new Give_Payment( $payment->ID );
1930 $address_data = $payment->address;
1931
1932 $pluggable_data['first_name'] = $payment->first_name;
1933 $pluggable_data['last_name'] = $payment->last_name;
1934 $pluggable_data['email'] = $payment->email;
1935 $pluggable_data['currency'] = $payment->currency;
1936 $pluggable_data['donated_amount'] = $payment->subtotal;
1937 $pluggable_data['donation_amount'] = $payment->subtotal;
1938 $pluggable_data['form_id'] = (int) $payment->form_id;
1939 $pluggable_data['form_title'] = $payment->form_title;
1940 $pluggable_data['name_title_prefix'] = $payment->title_prefix;
1941 $pluggable_data['date'] = $payment->date;
1942
1943 if ( is_array( $address_data ) ) {
1944 $pluggable_data['address_line_1'] = $address_data['line1'];
1945 $pluggable_data['address_line_2'] = $address_data['line2'];
1946 $pluggable_data['city'] = $address_data['city'];
1947 $pluggable_data['state'] = $address_data['state'];
1948 $pluggable_data['zip'] = $address_data['zip'];
1949 $pluggable_data['country'] = $address_data['country'];
1950 }
1951 // Payment meta.
1952 $payment_meta = $payment->get_meta();
1953 if ( is_array( $payment_meta ) && isset( $payment_meta['user_info'] ) ) {
1954 unset( $payment_meta['user_info'] );
1955 }
1956 $pluggable_data['payment_meta'] = $payment_meta;
1957 $donor_comment = give_get_donor_donation_comment( $payment->ID, $payment->donor_id );
1958 $pluggable_data['comment'] = ( is_array( $donor_comment ) && isset( $donor_comment['comment_content'] ) ) ? $donor_comment : '';
1959 }
1960
1961 $context['response_type'] = 'live';
1962 } else {
1963 if ( 'continues_recurring_donation' == $data['search_term'] || 'cancels_recurring_donation' == $data['search_term'] ) {
1964 $pluggable_data['form_id'] = $form_id;
1965 $pluggable_data['subscription']['id'] = 3;
1966 $pluggable_data['subscription']['donor_id'] = 8;
1967 $pluggable_data['subscription']['period'] = 'month';
1968 $pluggable_data['subscription']['initial_amount'] = '25.0000000000';
1969 $pluggable_data['subscription']['recurring_amount'] = '25.0000000000';
1970 $pluggable_data['subscription']['recurring_fee_amount'] = '0.0000000000';
1971 $pluggable_data['subscription']['transaction_id'] = 'a228ec9c6357963d23079d7d6945dd61';
1972 $pluggable_data['subscription']['parent_payment_id'] = '7492';
1973 $pluggable_data['subscription']['created'] = '2024-01-23 11:12:11';
1974 $pluggable_data['subscription']['expiration'] = '2024-03-23 23:59:59';
1975 $pluggable_data['subscription']['status'] = 'cancelled';
1976 $pluggable_data['subscription']['donor']['id'] = 8;
1977 $pluggable_data['subscription']['donor']['purchase_count'] = '3';
1978 $pluggable_data['subscription']['donor']['purchase_value'] = '75.000000';
1979 $pluggable_data['subscription']['donor']['email'] = 'johndoee@yopmail.com';
1980 $pluggable_data['subscription']['donor']['name'] = 'John Doe';
1981 $pluggable_data['subscription']['donor']['payment_ids'] = '7487,7492,7499';
1982 $pluggable_data['subscription']['donor']['user_id'] = '131';
1983 $pluggable_data['subscription']['customer_id'] = '8';
1984 } elseif ( 'donation_specific_field_value' == $data['search_term'] ) {
1985 $pluggable_data['form_id'] = 23;
1986 $pluggable_data['form_title'] = 'Demo Donation';
1987 $pluggable_data['_give_donor_billing_first_name'] = 'John';
1988 $pluggable_data['_give_donor_billing_last_name'] = 'Doe';
1989 $pluggable_data['_give_payment_donor_email'] = 'johndoee@gmail.com';
1990 $pluggable_data['_give_payment_currency'] = 'USD';
1991 $pluggable_data['_give_payment_total'] = '100';
1992 $pluggable_data['name_title_prefix'] = 'Mr';
1993 $pluggable_data['date'] = '2022-11-07 16:06:05';
1994 $pluggable_data['_give_donor_billing_address1'] = '33, Vincent Road';
1995 $pluggable_data['_give_donor_billing_address2'] = 'Chicago Street';
1996 $pluggable_data['_give_donor_billing_city'] = 'New York City';
1997 $pluggable_data['_give_donor_billing_state'] = 'New York';
1998 $pluggable_data['_give_donor_billing_zip'] = '223131';
1999 $pluggable_data['_give_donor_billing_country'] = 'USA';
2000 $pluggable_data['_give_donation_comment'] = 'Demo Comment';
2001 $pluggable_data['field_id'] = 'last_name';
2002 $pluggable_data['field_value'] = 'Doe';
2003 } else {
2004 $pluggable_data['first_name'] = 'John';
2005 $pluggable_data['last_name'] = 'Doe';
2006 $pluggable_data['email'] = 'johndoee@gmail.com';
2007 $pluggable_data['currency'] = 'USD';
2008 $pluggable_data['donated_amount'] = 100;
2009 $pluggable_data['donation_amount'] = 100;
2010 $pluggable_data['form_id'] = 23;
2011 $pluggable_data['form_title'] = 'Demo Donation';
2012 $pluggable_data['name_title_prefix'] = 'Mr';
2013 $pluggable_data['date'] = '2022-11-07 16:06:05';
2014 $pluggable_data['address_line_1'] = '33, Vincent Road';
2015 $pluggable_data['address_line_2'] = 'Chicago Street';
2016 $pluggable_data['city'] = 'New York City';
2017 $pluggable_data['state'] = 'New York';
2018 $pluggable_data['zip'] = '223131';
2019 $pluggable_data['country'] = 'USA';
2020 $pluggable_data['comment'] = 'Demo Comment';
2021 }
2022 $context['response_type'] = 'sample';
2023 }
2024
2025 $context['pluggable_data'] = $pluggable_data;
2026 return $context;
2027 }
2028
2029 /**
2030 * Search Divi Form fields.
2031 *
2032 * @param array $data Search Params.
2033 *
2034 * @since 1.0.0
2035 *
2036 * @return array
2037 */
2038 public function search_pluggable_divi_form_fields( $data ) {
2039 $result = [];
2040 $form_id = absint( $data['dynamic'] );
2041 $form_posts = Utilities::get_divi_forms();
2042
2043 if ( empty( $form_posts ) ) {
2044 return [
2045 'options' => [],
2046 'hasMore' => false,
2047 ];
2048 }
2049 $fields = [];
2050 foreach ( (array) $form_posts as $form_post ) {
2051 $pattern_regex = '/\[et_pb_contact_form(.*?)](.+?)\[\/et_pb_contact_form]/';
2052 preg_match_all( $pattern_regex, $form_post['post_content'], $forms, PREG_SET_ORDER );
2053 if ( empty( $forms ) ) {
2054 continue;
2055 }
2056
2057 $count = 0;
2058
2059 foreach ( $forms as $form ) {
2060 $pattern = get_shortcode_regex( [ 'et_pb_contact_field' ] );
2061
2062 preg_match_all( "/$pattern/", $form[0], $contact_fields, PREG_SET_ORDER );
2063
2064 if ( empty( $contact_fields ) ) {
2065 return $fields;
2066 }
2067
2068 foreach ( $contact_fields as $contact_field ) {
2069 $contact_field_attrs = shortcode_parse_atts( $contact_field[3] );
2070 $field_id = strtolower( self::array_get( $contact_field_attrs, 'field_id' ) );
2071 $fields[] = [
2072 'field_title' => self::array_get( $contact_field_attrs, 'field_title', __( 'No title', 'suretriggers' ) ),
2073 'field_id' => $field_id,
2074 ];
2075 }
2076 }
2077 }
2078
2079 if ( ! empty( $fields ) ) {
2080 foreach ( $fields as $field ) {
2081 $result[] = [
2082 'label' => $field['field_title'],
2083 'value' => '{' . $field['field_id'] . '}',
2084 ];
2085 }
2086 }
2087
2088 return [
2089 'options' => $result,
2090 'hasMore' => false,
2091 ];
2092 }
2093
2094 /**
2095 * Pseudo function copied from Divi
2096 *
2097 * @param array $array An array which contains value located at `$address`.
2098 * @param string|array $address The location of the value within `$array` (dot notation).
2099 * @param mixed $default Value to return if not found. Default is an empty string.
2100 *
2101 * @return mixed The value, if found, otherwise $default.
2102 */
2103 public static function array_get( $array, $address, $default = '' ) {
2104 $keys = is_array( $address ) ? $address : explode( '.', $address );
2105 $value = $array;
2106
2107 foreach ( $keys as $key ) {
2108 if ( ! empty( $key ) && isset( $key[0] ) && '[' === $key[0] ) {
2109 $index = substr( $key, 1, -1 );
2110
2111 if ( is_numeric( $index ) ) {
2112 $key = (int) $index;
2113 }
2114 }
2115
2116 if ( ! isset( $value[ $key ] ) ) {
2117 return $default;
2118 }
2119
2120 $value = $value[ $key ];
2121 }
2122
2123 return $value;
2124 }
2125
2126 /**
2127 * Get UAG Forms.
2128 *
2129 * @param array $data data.
2130 *
2131 * @return array
2132 */
2133 public function search_spectra_forms( $data ) {
2134 $form_posts = Utilities::get_uag_forms();
2135
2136 $options = [];
2137 if ( empty( $form_posts ) ) {
2138 return [
2139 'options' => [],
2140 'hasMore' => false,
2141 ];
2142 }
2143
2144 foreach ( (array) $form_posts as $form_post ) {
2145 $blocks = parse_blocks( $form_post['post_content'] );
2146 $i = 1;
2147 // Get form blocks.
2148 $this->process_blocks( $blocks, $form_post, $options, $i );
2149 }
2150
2151 return [
2152 'options' => $options,
2153 'hasMore' => false,
2154 ];
2155 }
2156
2157 /**
2158 * Return forms in nested blocks in Spectra.
2159 *
2160 * @param array $blocks data.
2161 * @param array $form_post Form post.
2162 * @param array $options Options.
2163 * @param int $i Number.
2164 *
2165 * @return array
2166 */
2167 public function process_blocks( $blocks, $form_post, &$options, &$i ) {
2168 foreach ( $blocks as $block ) {
2169 if ( 'uagb/forms' === $block['blockName'] ) {
2170 $options[] = [
2171 'label' => $form_post['post_title'] . ' (Form ' . ( $i++ ) . ')',
2172 'value' => $block['attrs']['block_id'],
2173 ];
2174 } elseif ( isset( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) {
2175 $this->process_blocks( $block['innerBlocks'], $form_post, $options, $i );
2176 }
2177 }
2178 return $options;
2179 }
2180
2181 /**
2182 * Check array recursive.
2183 *
2184 * @param array $array Array.
2185 * @param string $value search params.
2186 * @since 1.0.0
2187 *
2188 * @return array|void
2189 */
2190 public static function get_column_by_value( $array, $value ) {
2191
2192 foreach ( $array as $key => $sub_array ) {
2193
2194 if ( is_array( $sub_array ) ) {
2195 $result = self::get_column_by_value( $sub_array, $value );
2196 if ( null !== $result ) {
2197 return $key;
2198 }
2199 } else {
2200 return $key;
2201 }
2202 }
2203 return null;
2204
2205 }
2206
2207
2208 /**
2209 * Search UAG Form fields.
2210 *
2211 * @param array $data Search Params.
2212 *
2213 * @since 1.0.0
2214 *
2215 * @return array
2216 */
2217 public function search_spectraform_fields( $data ) {
2218 $result = [];
2219 $form_id = absint( $data['dynamic'] );
2220 $form_posts = Utilities::get_uag_forms();
2221
2222 if ( empty( $form_posts ) ) {
2223 return [
2224 'options' => [],
2225 'hasMore' => false,
2226 ];
2227 }
2228
2229 foreach ( (array) $form_posts as $form_post ) {
2230 $blocks = parse_blocks( $form_post['post_content'] );
2231
2232 foreach ( $blocks as $block ) {
2233 if ( (int) $block['attrs']['block_id'] === $form_id ) {
2234 $doc = new DOMDocument();
2235 $rendered_block = render_block( $block );
2236 $doc->loadHTML( $rendered_block );
2237 $child_node_list = $doc->getElementsByTagName( 'div' );
2238 for ( $i = 0; $i < $child_node_list->length; $i++ ) {
2239 $temp = $child_node_list->item( $i );
2240 if ( $temp && stripos( $temp->getAttribute( 'class' ), 'uagb-forms-input-label' ) !== false ) {
2241 $nodes[] = $temp;
2242 }
2243 }
2244
2245 foreach ( $nodes as $node ) {
2246 $result[] = [
2247 'label' => $node->textContent, //phpcs:ignore
2248 'value' => $node->textContent, //phpcs:ignore
2249 ];
2250 }
2251 }
2252 }
2253 }
2254
2255 return [
2256 'options' => $result,
2257 'hasMore' => false,
2258 ];
2259 }
2260
2261 /**
2262 * Search forms of MetForms.
2263 *
2264 * @param array $data data.
2265 * @return array
2266 */
2267 public function search_met_forms( $data ) {
2268 $args = [
2269 'post_type' => 'metform-form',
2270 'post_status' => 'publish',
2271 'numberposts' => -1,
2272 ];
2273
2274 $forms = get_posts( $args );
2275
2276 $options = [];
2277
2278 if ( ! empty( $forms ) ) {
2279 foreach ( $forms as $form ) {
2280 $options[] = [
2281 'label' => $form->post_title,
2282 'value' => $form->ID,
2283 ];
2284 }
2285 }
2286
2287 return [
2288 'options' => $options,
2289 'hasMore' => false,
2290 ];
2291 }
2292
2293 /**
2294 * Search forms of Ninja Forms.
2295 *
2296 * @param array $data data.
2297 * @return array
2298 */
2299 public function search_ninja_forms( $data ) {
2300 $options = [];
2301
2302 if ( function_exists( 'Ninja_Forms' ) ) {
2303 foreach ( Ninja_Forms()->form()->get_forms() as $form ) {
2304 $options[] = [
2305 'label' => $form->get_setting( 'title' ),
2306 'value' => $form->get_id(),
2307 ];
2308 }
2309 }
2310
2311 return [
2312 'options' => $options,
2313 'hasMore' => false,
2314 ];
2315 }
2316
2317 /**
2318 * Search forms of Pie Forms.
2319 *
2320 * @param array $data data.
2321 * @return array
2322 */
2323 public function search_pie_forms( $data ) {
2324 global $wpdb;
2325 $options = [];
2326
2327 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pf_forms' ) ) ) {
2328
2329 $results = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'pf_forms WHERE post_status = "published"' );
2330
2331 if ( $results ) {
2332 foreach ( $results as $result ) {
2333 $options[] = [
2334 'label' => $result->form_title,
2335 'value' => $result->id,
2336 ];
2337 }
2338 }
2339 }
2340
2341 return [
2342 'options' => $options,
2343 'hasMore' => false,
2344 ];
2345 }
2346
2347 /**
2348 * Get Fluent Forms.
2349 *
2350 * @param array $data data.
2351 *
2352 * @return array
2353 */
2354 public function search_fluent_forms( $data ) {
2355
2356 if ( ! function_exists( 'wpFluent' ) ) {
2357 return [
2358 'options' => [],
2359 'hasMore' => false,
2360 ];
2361 }
2362
2363 $forms = wpFluent()->table( 'fluentform_forms' )
2364 ->select( [ 'id', 'title' ] )
2365 ->orderBy( 'id', 'DESC' )
2366 ->get();
2367
2368 $options = [];
2369 if ( ! empty( $forms ) ) {
2370 foreach ( $forms as $form ) {
2371 $options[] = [
2372 'label' => $form->title,
2373 'value' => $form->id,
2374 ];
2375 }
2376 }
2377
2378 return [
2379 'options' => $options,
2380 'hasMore' => false,
2381 ];
2382
2383 }
2384
2385 /**
2386 * Get Fluent Forms Fields.
2387 *
2388 * @param array $data data.
2389 *
2390 * @return array
2391 */
2392 public function search_fluent_forms_form_fields( $data ) {
2393
2394 $options = [];
2395
2396 if ( ! function_exists( 'wpFluent' ) ) {
2397 return [];
2398 }
2399
2400 $form = wpFluent()->table( 'fluentform_forms' )->find( $data['dynamic'] );
2401 $field_data = json_decode( $form->form_fields, true );
2402 if ( is_array( $field_data ) && ! empty( $field_data['fields'] ) ) {
2403 foreach ( $field_data['fields'] as $field ) {
2404 if ( isset( $field['fields'] ) ) {
2405 foreach ( $field['fields'] as $field_key => $sub_field ) {
2406 if (
2407 isset( $sub_field['settings'] )
2408 && isset( $sub_field['settings']['label'] )
2409 && isset( $sub_field['settings']['visible'] )
2410 && true === $sub_field['settings']['visible']
2411 ) {
2412 $options[] = [
2413 'label' => $sub_field['settings']['label'],
2414 'value' => $field_key,
2415 ];
2416 }
2417 }
2418 } elseif ( isset( $field['element'] ) &&
2419 'container' === (string) $field['element'] &&
2420 isset( $field['columns'] ) && is_array( $field['columns'] ) ) {
2421 $container_fields = $field['columns'];
2422 foreach ( $container_fields as $c_fields ) {
2423 foreach ( $c_fields['fields'] as $field_key => $sub_field ) {
2424 if ( isset( $sub_field['settings'] ) ) {
2425 $options[] = [
2426 'label' => ( '' !== $sub_field['settings']['label'] ) ?
2427 $sub_field['settings']['label'] :
2428 $sub_field['attributes']['name'],
2429 'value' => isset( $sub_field['attributes']['name'] ) ?
2430 $sub_field['attributes']['name'] :
2431 strtolower( $sub_field['settings']['label'] ),
2432 ];
2433 }
2434 }
2435 }
2436 } elseif ( isset( $field['attributes'] ) && isset( $field['attributes']['name'] ) ) {
2437 if ( isset( $field['attributes']['placeholder'] ) && ! empty( $field['attributes']['placeholder'] ) ) {
2438 $options[] = [
2439 'label' => $field['attributes']['placeholder'],
2440 'value' => $field['attributes']['name'],
2441 ];
2442 } elseif ( isset( $field['settings'] ) &&
2443 isset( $field['settings']['label'] ) &&
2444 ! empty( $field['settings']['label'] ) ) {
2445 $options[] = [
2446 'label' => $field['settings']['label'],
2447 'value' => $field['attributes']['name'],
2448 ];
2449 }
2450 }
2451 }
2452 }
2453
2454 return [
2455 'options' => $options,
2456 'hasMore' => false,
2457 ];
2458
2459 }
2460
2461 /**
2462 * Get Fluent Forms.
2463 *
2464 * @param array $data data.
2465 *
2466 * @return array
2467 */
2468 public function search_bricks_builder_forms( $data ) {
2469 $bricks_theme = wp_get_theme( 'bricks' );
2470 if ( ! $bricks_theme->exists() ) {
2471 return [
2472 'options' => [],
2473 'hasMore' => false,
2474 ];
2475 }
2476
2477 $bricks_settings = (array) get_option( BRICKS_DB_GLOBAL_SETTINGS );
2478 if ( array_key_exists( 'postTypes', $bricks_settings ) ) {
2479 $bricks_posts = $bricks_settings['postTypes'];
2480 } else {
2481 $bricks_posts = [];
2482 }
2483 $bricks_posts[] = 'bricks_template';
2484
2485 $args = [
2486 'post_type' => $bricks_posts,
2487 'post_status' => 'publish',
2488 'posts_per_page' => -1,
2489 ];
2490
2491 $templates = get_posts( $args );
2492
2493 $options = [];
2494 if ( ! empty( $templates ) ) {
2495 foreach ( $templates as $template ) {
2496 $fetch_content = get_post_meta( $template->ID, BRICKS_DB_PAGE_CONTENT, true );
2497 if ( is_array( $fetch_content ) ) {
2498 foreach ( $fetch_content as $content ) {
2499 if ( 'form' === $content['name'] ) {
2500 $options[] = [
2501 'label' => $template->post_title . ' - ' . ( isset( $content['label'] ) ? $content['label'] : 'Form' ),
2502 'value' => $content['id'],
2503 ];
2504 }
2505 }
2506 }
2507 }
2508 }
2509
2510 return [
2511 'options' => $options,
2512 'hasMore' => false,
2513 ];
2514
2515 }
2516
2517 /**
2518 * Bricks builder form fields.
2519 *
2520 * @param array $data data.
2521 * @return array
2522 */
2523 public function search_pluggable_bricks_builder_form_fields( $data ) {
2524 $result = [];
2525 $fields = [];
2526 $form_id_str = $data['dynamic'];
2527 $ids = explode( '_', $form_id_str );
2528 $post_id = $ids[0];
2529 $form_id = $ids[1];
2530 $fetch_content = get_post_meta( $post_id, BRICKS_DB_PAGE_CONTENT, true );
2531 if ( is_array( $fetch_content ) ) {
2532 foreach ( $fetch_content as $content ) {
2533 if ( 'form' === $content['name'] && $form_id === $content['id'] ) {
2534 $fields = $content['settings']['fields'];
2535 break;
2536 }
2537 }
2538 }
2539
2540 if ( ! empty( $fields ) ) {
2541 foreach ( $fields as $field ) {
2542 $result[] = [
2543 'label' => $field['label'],
2544 'value' => '{' . strtolower( $field['label'] ) . '}',
2545 ];
2546 }
2547 }
2548
2549 return [
2550 'options' => $result,
2551 'hasMore' => false,
2552 ];
2553 }
2554
2555 /**
2556 * Get fluent form fields
2557 *
2558 * @param array $data Data array.
2559 *
2560 * @return array
2561 */
2562 public function search_pluggable_fluent_form_fields( $data ) {
2563 $result = [];
2564 $form_id = absint( $data['dynamic'] );
2565
2566 $fluentform_fields = Utilities::get_fluentform_fields( $data['search_term'], -1, $form_id );
2567
2568 if ( is_array( $fluentform_fields['results'] ) ) {
2569 foreach ( $fluentform_fields['results'] as $field ) {
2570 $result[] = [
2571 'label' => $field['text'],
2572 'value' => "{{$field['value']}}",
2573 ];
2574 }
2575 }
2576
2577 $result[] = [
2578 'value' => '{form_id}',
2579 'label' => 'Form ID',
2580 ];
2581
2582 $result[] = [
2583 'value' => '{form_title}',
2584 'label' => 'Form Title',
2585 ];
2586 $result[] = [
2587 'value' => '{entry_id}',
2588 'label' => 'Entry ID',
2589 ];
2590
2591 $result[] = [
2592 'value' => '{entry_source_url}',
2593 'label' => 'Entry Source URL',
2594 ];
2595
2596 $result[] = [
2597 'value' => '{submission_date}',
2598 'label' => 'Submission Date',
2599 ];
2600
2601 $result[] = [
2602 'value' => '{user_ip}',
2603 'label' => 'User IP',
2604 ];
2605
2606 return [
2607 'options' => $result,
2608 'hasMore' => false,
2609 ];
2610 }
2611
2612 /**
2613 * Search Gravity Form fields.
2614 *
2615 * @param array $data Search Params.
2616 *
2617 * @since 1.0.0
2618 */
2619 public function search_gform_fields( $data ) {
2620 if ( ! class_exists( 'RGFormsModel' ) ) {
2621 return [
2622 'options' => [],
2623 'hasMore' => false,
2624 ];
2625 }
2626 $result = [];
2627 $page = $data['page'];
2628 $form_id = absint( $data['dynamic'] );
2629
2630 $form = RGFormsModel::get_form_meta( $form_id );
2631
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 ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
2637 $result[] = [
2638 'value' => $input['id'],
2639 'label' => GFCommon::get_label( $field, $input['id'] ),
2640 ];
2641 }
2642 }
2643 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
2644 $result[] = [
2645 'value' => (string) $field['id'],
2646 'label' => GFCommon::get_label( $field ),
2647 ];
2648 }
2649 }
2650 }
2651
2652 return [
2653 'options' => $result,
2654 'hasMore' => false,
2655 ];
2656
2657 }
2658
2659 /**
2660 * Search Gravity Form fields.
2661 *
2662 * @param array $data Search Params.
2663 *
2664 * @since 1.0.0
2665 */
2666 public function search_pluggable_gravity_form_fields( $data ) {
2667 if ( ! class_exists( 'RGFormsModel' ) ) {
2668 return [
2669 'options' => [],
2670 'hasMore' => false,
2671 ];
2672 }
2673 $result = [];
2674 $form_id = absint( $data['dynamic'] );
2675
2676 $form = RGFormsModel::get_form_meta( $form_id );
2677
2678 if ( is_array( $form['fields'] ) ) {
2679 foreach ( $form['fields'] as $field ) {
2680 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
2681 foreach ( $field['inputs'] as $input ) {
2682 if ( ! isset( $input['isHidden'] ) || ( isset( $input['isHidden'] ) && ! $input['isHidden'] ) ) {
2683 $result[] = [
2684 'value' => '{' . $input['id'] . '}',
2685 'label' => GFCommon::get_label( $field, $input['id'] ),
2686 ];
2687 }
2688 }
2689 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
2690 $result[] = [
2691 'value' => '{' . $field['id'] . '}',
2692 'label' => GFCommon::get_label( $field ),
2693 ];
2694 }
2695 }
2696 }
2697
2698 $result[] = [
2699 'value' => '{gravity_form}',
2700 'label' => 'Form ID',
2701 ];
2702 $result[] = [
2703 'value' => '{form_title}',
2704 'label' => 'Form Title',
2705 ];
2706 $result[] = [
2707 'value' => '{entry_id}',
2708 'label' => 'Entry ID',
2709 ];
2710 $result[] = [
2711 'value' => '{user_ip}',
2712 'label' => 'User IP',
2713 ];
2714 $result[] = [
2715 'value' => '{entry_source_url}',
2716 'label' => 'Entry Source URL',
2717 ];
2718 $result[] = [
2719 'value' => '{entry_submission_date}',
2720 'label' => 'Entry Submission Date',
2721 ];
2722
2723 return [
2724 'options' => $result,
2725 'hasMore' => false,
2726 ];
2727
2728 }
2729
2730 /**
2731 * Search Gravity Form fields for action.
2732 *
2733 * @param array $data data.
2734 * @return array
2735 */
2736 public function search_gravity_form_custom_fields( $data ) {
2737 $context = [];
2738 if ( ! class_exists( 'RGFormsModel' ) || ! class_exists( 'GFCommon' ) ) {
2739 return [];
2740 }
2741 $form_id = $data['filter'];
2742 $form = RGFormsModel::get_form_meta( $form_id );
2743 $custom_fields = [];
2744 if ( is_array( $form['fields'] ) ) {
2745 foreach ( $form['fields'] as $field ) {
2746 if ( isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
2747 foreach ( $field['inputs'] as $input ) {
2748 if ( ! $input['isHidden'] ) {
2749 $custom_fields[] = [
2750 'label' => GFCommon::get_label( $field, $input['id'] ),
2751 'value' => $input['id'],
2752 'type' => 'text',
2753 ];
2754 }
2755 }
2756 } elseif ( ! rgar( $field, 'displayOnly' ) ) {
2757 $custom_fields[] = [
2758 'label' => GFCommon::get_label( $field ),
2759 'value' => $field['id'],
2760 'type' => 'text',
2761 ];
2762 }
2763 }
2764 }
2765 $context['fields'] = $custom_fields;
2766 return $context;
2767 }
2768
2769 /**
2770 * Get user register details via gravity forms.
2771 *
2772 * @param array $data data.
2773 *
2774 * @return array
2775 */
2776 public function search_pluggables_gravity_forms_user_register( $data ) {
2777 global $wpdb;
2778 $context = [];
2779 $pluggable_data = [];
2780
2781 $results = $wpdb->get_results(
2782 $wpdb->prepare(
2783 "SELECT id, form_id, date_created, ip, source_url, created_by FROM {$wpdb->prefix}gf_entry
2784 WHERE form_id = %d AND
2785 status = %s
2786 ORDER BY id
2787 DESC LIMIT 1",
2788 $data['filter']['gravity_form']['value'],
2789 'active'
2790 )
2791 );
2792
2793 if ( $results ) {
2794 $pluggable_data['gravity_form'] = (int) $results[0]->form_id;
2795 $pluggable_data['entry_id'] = $results[0]->id;
2796 $pluggable_data['user_ip'] = $results[0]->ip;
2797 $pluggable_data['entry_source_url'] = $results[0]->source_url;
2798 $pluggable_data['entry_submission_date'] = $results[0]->date_created;
2799 $pluggable_data['user'] = WordPress::get_user_context( $results[0]->created_by );
2800 $context['response_type'] = 'live';
2801 } else {
2802 $pluggable_data['gravity_form'] = '3';
2803 $pluggable_data['entry_id'] = '13';
2804 $pluggable_data['user_ip'] = '127.0.0.0';
2805 $pluggable_data['entry_source_url'] = 'https://example.com';
2806 $pluggable_data['entry_submission_date'] = '2024-02-05 09:41:59';
2807 $pluggable_data['user']['wp_user_id'] = '123';
2808 $pluggable_data['user']['user_login'] = 'johnd';
2809 $pluggable_data['user']['display_name'] = 'johnd';
2810 $pluggable_data['user']['user_firstname'] = 'John';
2811 $pluggable_data['user']['user_lastname'] = 'Doe';
2812 $pluggable_data['user']['user_email'] = 'johnd@yopmail.com';
2813 $pluggable_data['user']['user_role'] = [ 'subscriber' ];
2814 $context['response_type'] = 'sample';
2815 }
2816
2817 $context['pluggable_data'] = $pluggable_data;
2818 return $context;
2819 }
2820
2821 /**
2822 * Get payment form details for gravity forms.
2823 *
2824 * @param array $data data.
2825 *
2826 * @return array
2827 */
2828 public function search_pluggables_gravity_forms_payment( $data ) {
2829 global $wpdb;
2830 $context = [];
2831 $pluggable_data = [];
2832
2833 $results = $wpdb->get_results(
2834 $wpdb->prepare(
2835 "SELECT * FROM {$wpdb->prefix}gf_entry
2836 WHERE form_id = %d AND
2837 status = %s AND
2838 payment_status LIKE %s
2839 ORDER BY id
2840 DESC LIMIT 1",
2841 $data['filter']['gravity_form']['value'],
2842 'active',
2843 'Paid'
2844 )
2845 );
2846
2847 if ( $results ) {
2848 $pluggable_data['gravity_form'] = (int) $results[0]->form_id;
2849 $pluggable_data['entry_id'] = $results[0]->id;
2850 $pluggable_data['user_ip'] = $results[0]->ip;
2851 $pluggable_data['entry_source_url'] = $results[0]->source_url;
2852 $pluggable_data['entry_submission_date'] = $results[0]->date_created;
2853 $pluggable_data['payment_status'] = $results[0]->payment_status;
2854 $pluggable_data['payment_amount'] = $results[0]->payment_amount;
2855 $pluggable_data['currency'] = $results[0]->currency;
2856 $pluggable_data['payment_method'] = $results[0]->payment_method;
2857 $pluggable_data['transaction_id'] = $results[0]->transaction_id;
2858 $pluggable_data['user'] = WordPress::get_user_context( $results[0]->created_by );
2859 $context['response_type'] = 'live';
2860 } else {
2861 $pluggable_data['gravity_form'] = '3';
2862 $pluggable_data['entry_id'] = '13';
2863 $pluggable_data['user_ip'] = '127.0.0.0';
2864 $pluggable_data['entry_source_url'] = 'https://example.com';
2865 $pluggable_data['entry_submission_date'] = '2024-02-05 09:41:59';
2866 $pluggable_data['payment_status'] = 'Paid';
2867 $pluggable_data['payment_amount'] = '10.00';
2868 $pluggable_data['currency'] = 'USD';
2869 $pluggable_data['payment_method'] = 'visa';
2870 $pluggable_data['transaction_id'] = 'st_ooi98';
2871 $pluggable_data['user']['wp_user_id'] = '123';
2872 $pluggable_data['user']['user_login'] = 'johnd';
2873 $pluggable_data['user']['display_name'] = 'johnd';
2874 $pluggable_data['user']['user_firstname'] = 'John';
2875 $pluggable_data['user']['user_lastname'] = 'Doe';
2876 $pluggable_data['user']['user_email'] = 'johnd@yopmail.com';
2877 $pluggable_data['user']['user_role'] = [ 'subscriber' ];
2878 $context['response_type'] = 'sample';
2879 }
2880
2881 $context['pluggable_data'] = $pluggable_data;
2882 return $context;
2883 }
2884
2885 /**
2886 * Prepare fluentcrm lists.
2887 *
2888 * @param array $data Search Params.
2889 *
2890 * @return array[]
2891 */
2892 public function search_fluentcrm_lists( $data ) {
2893
2894 $list_api = FluentCrmApi( 'lists' );
2895 $all_lists = $list_api->all();
2896 $options = [];
2897
2898 if ( ! empty( $all_lists ) ) {
2899 foreach ( $all_lists as $list ) {
2900 $options[] = [
2901 'label' => $list->title,
2902 'value' => $list->id,
2903 ];
2904 }
2905 }
2906
2907 return [
2908 'options' => $options,
2909 'hasMore' => false,
2910 ];
2911 }
2912
2913 /**
2914 * Prepare fluentcrm contact status.
2915 *
2916 * @param array $data Search Params.
2917 *
2918 * @return array[]
2919 */
2920 public function search_fluentcrm_contact_status( $data ) {
2921
2922 $options = [
2923 [
2924 'label' => __( 'Subscribed', 'suretriggers' ),
2925 'value' => 'subscribed',
2926 ],
2927 [
2928 'label' => __( 'Pending', 'suretriggers' ),
2929 'value' => 'pending',
2930 ],
2931 [
2932 'label' => __( 'Unsubscribed', 'suretriggers' ),
2933 'value' => 'unsubscribed',
2934 ],
2935 [
2936 'label' => __( 'Bounced', 'suretriggers' ),
2937 'value' => 'bounced',
2938 ],
2939 [
2940 'label' => __( 'Complained', 'suretriggers' ),
2941 'value' => 'complained',
2942 ],
2943 ];
2944
2945 return [
2946 'options' => $options,
2947 'hasMore' => false,
2948 ];
2949 }
2950
2951 /**
2952 * Prepare fluentcrm contact status.
2953 *
2954 * @param array $data Search Params.
2955 *
2956 * @return array[]
2957 */
2958 public function search_fluentcrm_fetch_custom_fields( $data ) {
2959
2960 $options = [
2961 [
2962 'label' => __( 'Yes', 'suretriggers' ),
2963 'value' => 'true',
2964 ],
2965 [
2966 'label' => __( 'No', 'suretriggers' ),
2967 'value' => 'false',
2968 ],
2969 ];
2970
2971 return [
2972 'options' => $options,
2973 'hasMore' => false,
2974 ];
2975 }
2976
2977 /**
2978 * Prepare fluentcrm tags.
2979 *
2980 * @param array $data Search Params.
2981 *
2982 * @return array[]
2983 */
2984 public function search_fluentcrm_tags( $data ) {
2985
2986 if ( ! function_exists( 'FluentCrmApi' ) ) {
2987 return [];
2988 }
2989
2990 $tag_api = FluentCrmApi( 'tags' );
2991 $all_tags = $tag_api->all();
2992 $options = [];
2993
2994 if ( ! empty( $all_tags ) ) {
2995 foreach ( $all_tags as $tag ) {
2996 $options[] = [
2997 'label' => $tag->title,
2998 'value' => $tag->id,
2999 ];
3000 }
3001 }
3002
3003 return [
3004 'options' => $options,
3005 'hasMore' => false,
3006 ];
3007 }
3008
3009 /**
3010 * Prepare JetpackCRM Contact tags.
3011 *
3012 * @param array $data Search Params.
3013 *
3014 * @return array
3015 */
3016 public function search_jetpack_crm_contact_tags( $data ) {
3017
3018 if ( ! function_exists( 'zeroBSCRM_getCustomerTags' ) ) {
3019 return [];
3020 }
3021
3022 $all_tags = zeroBSCRM_getCustomerTags();
3023 $options = [];
3024
3025 if ( ! empty( $all_tags ) ) {
3026 foreach ( $all_tags as $tag ) {
3027 $options[] = [
3028 'label' => $tag['name'],
3029 'value' => $tag['id'],
3030 ];
3031 }
3032 }
3033
3034 return [
3035 'options' => $options,
3036 'hasMore' => false,
3037 ];
3038 }
3039
3040 /**
3041 * Prepare JetpackCRM Company tags.
3042 *
3043 * @param array $data Search Params.
3044 *
3045 * @return array
3046 */
3047 public function search_jetpack_crm_company_tags( $data ) {
3048
3049 if ( ! defined( 'ZBS_TYPE_COMPANY' ) ) {
3050 return [];
3051 }
3052
3053 global $wpdb;
3054 $all_tags = $wpdb->get_results( $wpdb->prepare( "SELECT `ID`,`zbstag_name` FROM `{$wpdb->prefix}zbs_tags` WHERE zbstag_objtype = %d", ZBS_TYPE_COMPANY ) );
3055
3056 $options = [];
3057 if ( ! empty( $all_tags ) ) {
3058 foreach ( $all_tags as $tag ) {
3059 $options[] = [
3060 'label' => $tag->zbstag_name,
3061 'value' => $tag->ID,
3062 ];
3063 }
3064 }
3065
3066 return [
3067 'options' => $options,
3068 'hasMore' => false,
3069 ];
3070 }
3071
3072 /**
3073 * Prepare JetpackCRM Companies list.
3074 *
3075 * @param array $data Search Params.
3076 *
3077 * @return array
3078 */
3079 public function search_jetpack_crm_companies_list( $data ) {
3080
3081 if ( ! function_exists( 'zeroBS_getCompanies' ) ) {
3082 return [];
3083 }
3084
3085 $all_companies = zeroBS_getCompanies();
3086 $options = [];
3087
3088 if ( ! empty( $all_companies ) ) {
3089 foreach ( $all_companies as $company ) {
3090 $options[] = [
3091 'label' => $company['name'],
3092 'value' => $company['id'],
3093 ];
3094 }
3095 }
3096
3097 return [
3098 'options' => $options,
3099 'hasMore' => false,
3100 ];
3101 }
3102
3103 /**
3104 * Prepare JetpackCRM contact status.
3105 *
3106 * @param array $data Search Params.
3107 *
3108 * @return array
3109 */
3110 public function search_jetpack_crm_contact_statuses( $data ) {
3111
3112 $options = [
3113 [
3114 'label' => __( 'Lead', 'suretriggers' ),
3115 'value' => 'Lead',
3116 ],
3117 [
3118 'label' => __( 'Customer', 'suretriggers' ),
3119 'value' => 'Customer',
3120 ],
3121 [
3122 'label' => __( 'Refused', 'suretriggers' ),
3123 'value' => 'Refused',
3124 ],
3125 [
3126 'label' => __( 'Blacklisted', 'suretriggers' ),
3127 'value' => 'Blacklisted',
3128 ],
3129 [
3130 'label' => __( 'Cancelled by Customer', 'suretriggers' ),
3131 'value' => 'Cancelled by Customer',
3132 ],
3133 [
3134 'label' => __( 'Cancelled by Us (Pre-Quote)', 'suretriggers' ),
3135 'value' => 'Cancelled by Us (Pre-Quote)',
3136 ],
3137 [
3138 'label' => __( 'Cancelled by Us (Post-Quote)', 'suretriggers' ),
3139 'value' => 'Cancelled by Us (Post-Quote)',
3140 ],
3141 ];
3142
3143 return [
3144 'options' => $options,
3145 'hasMore' => false,
3146 ];
3147 }
3148
3149 /**
3150 * Prepare FunnelKit Automations' lists.
3151 *
3152 * @param array $data Search Params.
3153 *
3154 * @return array
3155 */
3156 public function search_funnel_kit_automations_lists( $data ) {
3157
3158 if ( ! class_exists( 'BWFCRM_Lists' ) ) {
3159 return [];
3160 }
3161
3162 $bwfcrm_lists = \BWFCRM_Lists::get_lists();
3163
3164 $options = [];
3165
3166 foreach ( $bwfcrm_lists as $list ) {
3167 $options[] = [
3168 'label' => $list['name'],
3169 'value' => $list['ID'],
3170 ];
3171 }
3172
3173 return [
3174 'options' => $options,
3175 'hasMore' => false,
3176 ];
3177 }
3178
3179 /**
3180 * Prepare FunnelKit Automations' tags.
3181 *
3182 * @param array $data Search Params.
3183 *
3184 * @return array
3185 */
3186 public function search_funnel_kit_automations_tags( $data ) {
3187
3188 if ( ! class_exists( 'BWFCRM_Tag' ) ) {
3189 return [];
3190 }
3191
3192 $bwfcrm_tags = \BWFCRM_Tag::get_tags();
3193
3194 $options = [];
3195
3196 foreach ( $bwfcrm_tags as $tag ) {
3197 $options[] = [
3198 'label' => $tag['name'],
3199 'value' => $tag['ID'],
3200 ];
3201 }
3202
3203 return [
3204 'options' => $options,
3205 'hasMore' => false,
3206 ];
3207 }
3208
3209 /**
3210 * Prepare Wishlist Memberlists level.
3211 *
3212 * @param array $data Search Params.
3213 *
3214 * @return array[]
3215 */
3216 public function search_wishlistmember_lists( $data ) {
3217
3218 $wlm_levels = wlmapi_get_levels();
3219 $options = [];
3220
3221 if ( ! empty( $wlm_levels ) ) {
3222 foreach ( $wlm_levels['levels']['level'] as $list ) {
3223 if ( isset( $list['name'] ) && ! empty( $list['name'] ) ) {
3224 $options[] = [
3225 'label' => $list['name'],
3226 'value' => (string) $list['id'],
3227 ];
3228 }
3229 }
3230 }
3231
3232 return [
3233 'options' => $options,
3234 'hasMore' => false,
3235 ];
3236 }
3237
3238 /**
3239 * Prepare Wishlist Memberlists members.
3240 *
3241 * @param array $data Search Params.
3242 *
3243 * @return array<string, array<int<0, max>, array<string, mixed>>|false>
3244 */
3245 public function search_wishlistmember_members( $data ) {
3246
3247 if ( ! function_exists( 'wlmapi_get_members' ) ) {
3248 return [];
3249 }
3250
3251 $wlm_members = wlmapi_get_members();
3252 $options = [];
3253
3254 if ( ! empty( $wlm_members ) ) {
3255 foreach ( $wlm_members['members']['member'] as $list ) {
3256 if ( isset( $list['user_email'] ) && ! empty( $list['user_email'] ) ) {
3257 $options[] = [
3258 'label' => $list['user_email'],
3259 'value' => (string) $list['id'],
3260 ];
3261 }
3262 }
3263 }
3264
3265 return [
3266 'options' => $options,
3267 'hasMore' => false,
3268 ];
3269 }
3270
3271 /**
3272 * Prepare elementor popups.
3273 *
3274 * @param array $data Search Params.
3275 *
3276 * @return array[]
3277 */
3278 public function search_elementor_popups( $data ) {
3279
3280 $posts = get_posts(
3281 [
3282 'post_type' => 'elementor_library',
3283 'orderby' => 'title',
3284 'order' => 'ASC',
3285 'post_status' => 'publish',
3286 'meta_query' => [
3287 [
3288 'key' => '_elementor_template_type',
3289 'value' => 'popup',
3290 'compare' => '=',
3291 ],
3292 ],
3293 ]
3294 );
3295
3296 $options = [];
3297 if ( ! empty( $posts ) ) {
3298 foreach ( $posts as $post ) {
3299 $options[] = [
3300 'label' => $post->post_title,
3301 'value' => $post->ID,
3302 ];
3303 }
3304 }
3305
3306 return [
3307 'options' => $options,
3308 'hasMore' => false,
3309 ];
3310 }
3311
3312 /**
3313 * Prepare givewp forms.
3314 *
3315 * @param array $data Search Params.
3316 *
3317 * @return array
3318 */
3319 public function search_givewp_forms( $data ) {
3320
3321 $page = $data['page'];
3322 $limit = Utilities::get_search_page_limit();
3323 $offset = $limit * ( $page - 1 );
3324
3325 $posts = get_posts(
3326 [
3327 'post_type' => 'give_forms',
3328 'orderby' => 'title',
3329 'order' => 'ASC',
3330 'post_status' => 'publish',
3331 'posts_per_page' => $limit,
3332 'offset' => $offset,
3333 ]
3334 );
3335
3336 $options = [];
3337 if ( ! empty( $posts ) ) {
3338 foreach ( $posts as $post ) {
3339 $options[] = [
3340 'label' => $post->post_title,
3341 'value' => $post->ID,
3342 ];
3343 }
3344 }
3345
3346 $count = wp_count_posts( 'give_forms' )->publish;
3347
3348 return [
3349 'options' => $options,
3350 'hasMore' => $count > $limit && $count > $offset,
3351 ];
3352 }
3353
3354 /**
3355 * Prepare givewp recurring forms.
3356 *
3357 * @param array $data Search Params.
3358 *
3359 * @return array
3360 */
3361 public function search_givewp_recurring_forms( $data ) {
3362
3363 global $wpdb;
3364 $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 );
3365
3366 $options = [];
3367 if ( ! empty( $recurring ) ) {
3368 foreach ( $recurring as $form_id ) {
3369 $post_status = get_post_status( $form_id['form_id'] );
3370 if ( 'trash' !== $post_status ) {
3371 $options[] = [
3372 'label' => get_the_title( $form_id['form_id'] ),
3373 'value' => $form_id['form_id'],
3374 ];
3375 }
3376 }
3377 }
3378
3379 return [
3380 'options' => $options,
3381 'hasMore' => false,
3382 ];
3383 }
3384
3385 /**
3386 * Prepare givewp forms.
3387 *
3388 * @param array $data Search Params.
3389 *
3390 * @return array
3391 */
3392 public function search_givewp_form_fields( $data ) {
3393 $options = [
3394 [
3395 'label' => 'First Name',
3396 'value' => '_give_donor_billing_first_name',
3397 ],
3398 [
3399 'label' => 'Last Name',
3400 'value' => '_give_donor_billing_last_name',
3401 ],
3402 [
3403 'label' => 'Email',
3404 'value' => '_give_payment_donor_email',
3405 ],
3406 [
3407 'label' => 'Donation Amount',
3408 'value' => '_give_payment_total',
3409 ],
3410 [
3411 'label' => 'Currency',
3412 'value' => '_give_payment_currency',
3413 ],
3414 [
3415 'label' => 'Comment',
3416 'value' => '_give_donation_comment',
3417 ],
3418 [
3419 'label' => 'Zip',
3420 'value' => '_give_donor_billing_zip',
3421 ],
3422 [
3423 'label' => 'Country',
3424 'value' => '_give_donor_billing_country',
3425 ],
3426 [
3427 'label' => 'Address 1',
3428 'value' => '_give_donor_billing_address1',
3429 ],
3430 [
3431 'label' => 'Address 2',
3432 'value' => '_give_donor_billing_address2',
3433 ],
3434 [
3435 'label' => 'City',
3436 'value' => '_give_donor_billing_city',
3437 ],
3438 [
3439 'label' => 'State',
3440 'value' => '_give_donor_billing_state',
3441 ],
3442 ];
3443
3444 if ( class_exists( '\Give_FFM_Render_Form' ) ) {
3445 $form_id = $data['dynamic'];
3446 $custom_form_fields = [];
3447 if ( class_exists( '\GiveFormFieldManager\Helpers\Form' ) ) {
3448 $custom_form_fields = \GiveFormFieldManager\Helpers\Form::get_input_fields( $form_id );
3449 }
3450
3451 if ( ! empty( $custom_form_fields ) ) {
3452 if ( ! empty( $custom_form_fields[2] ) && is_array( $custom_form_fields[2] ) ) {
3453 foreach ( $custom_form_fields[2] as $custom_form_field ) {
3454 $custom_form_field['required'] = ( 'no' === $custom_form_field['required'] ) ? false : true;
3455 $options[] = [
3456 'label' => $custom_form_field['label'],
3457 'value' => $custom_form_field['name'],
3458 ];
3459 }
3460 }
3461 }
3462 }
3463
3464 return [
3465 'options' => $options,
3466 'hasMore' => false,
3467 ];
3468 }
3469
3470 /**
3471 * Prepare buddyboss group users.
3472 *
3473 * @param array $data Search Params.
3474 *
3475 * @return array[]
3476 */
3477 public function search_bb_group_users( $data ) {
3478 $options = [];
3479
3480 $group_id = $data['dynamic'];
3481 $admins = groups_get_group_admins( $group_id );
3482
3483 if ( ! empty( $admins ) ) {
3484 foreach ( $admins as $admin ) {
3485 $admin_user = get_user_by( 'id', $admin->user_id );
3486 $options[] = [
3487 'label' => $admin_user->display_name,
3488 'value' => $admin_user->ID,
3489 ];
3490 }
3491 }
3492
3493 $members = groups_get_group_members( [ 'group_id' => $group_id ] );
3494
3495 if ( isset( $members['members'] ) && ! empty( $members['members'] ) ) {
3496 foreach ( $members['members'] as $member ) {
3497 $options[] = [
3498 'label' => $member->display_name,
3499 'value' => $member->ID,
3500 ];
3501 }
3502 }
3503 return [
3504 'options' => $options,
3505 'hasMore' => false,
3506 ];
3507 }
3508
3509 /**
3510 * Prepare buddyboss groups.
3511 *
3512 * @param array $data Search Params.
3513 *
3514 * @return array[]
3515 */
3516 public function search_buddyboss_groups( $data ) {
3517 global $wpdb;
3518
3519 $options = [];
3520 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
3521 if ( ! empty( $groups ) ) {
3522 foreach ( $groups as $group ) {
3523 $options[] = [
3524 'label' => $group->name,
3525 'value' => $group->id,
3526 ];
3527 }
3528 }
3529 return [
3530 'options' => $options,
3531 'hasMore' => false,
3532 ];
3533 }
3534
3535 /**
3536 * Prepare buddyboss public groups.
3537 *
3538 * @param array $data Search Params.
3539 *
3540 * @return array[]
3541 */
3542 public function search_buddyboss_public_groups( $data ) {
3543 $options = [];
3544 $groups = groups_get_groups();
3545 if ( isset( $groups['groups'] ) && ! empty( $groups['groups'] ) ) {
3546 foreach ( $groups['groups'] as $group ) {
3547 if ( 'public' === $group->status ) {
3548 $options[] = [
3549 'label' => $group->name,
3550 'value' => $group->id,
3551 ];
3552 }
3553 }
3554 }
3555 return [
3556 'options' => $options,
3557 'hasMore' => false,
3558 ];
3559 }
3560
3561 /**
3562 * Prepare buddyboss private groups.
3563 *
3564 * @param array $data Search Params.
3565 *
3566 * @return array<string, array<int, array<string, mixed>>|false>
3567 */
3568 public function search_buddyboss_private_groups( $data ) {
3569 global $wpdb;
3570
3571 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'private'" );
3572
3573 $options = [];
3574 if ( $groups ) {
3575 foreach ( $groups as $group ) {
3576 $options[] = [
3577 'label' => $group->name,
3578 'value' => $group->id,
3579 ];
3580 }
3581 }
3582
3583 return [
3584 'options' => $options,
3585 'hasMore' => false,
3586 ];
3587 }
3588
3589 /**
3590 * Prepare buddyboss profile types list.
3591 *
3592 * @param array $data Search Params.
3593 *
3594 * @return array[]
3595 */
3596 public function search_bb_profile_type_list( $data ) {
3597 $options = [];
3598
3599 if ( function_exists( 'bp_get_active_member_types' ) ) {
3600 $types = bp_get_active_member_types(
3601 [
3602 'fields' => '*',
3603 ]
3604 );
3605 if ( $types ) {
3606 foreach ( $types as $type ) {
3607 $options[] = [
3608 'label' => $type->post_title,
3609 'value' => $type->ID,
3610 ];
3611 }
3612 }
3613 }
3614
3615 /**
3616 *
3617 * Ignore line
3618 *
3619 * @phpstan-ignore-next-line
3620 */
3621 return [
3622 'options' => $options,
3623 'hasMore' => false,
3624 ];
3625 }
3626
3627 /**
3628 * Prepare buddyboss public groups.
3629 *
3630 * @param array $data Search Params.
3631 *
3632 * @return array
3633 */
3634 public function search_buddyboss_group_type( $data ) {
3635 $options = [];
3636
3637 if ( ! function_exists( 'bp_groups_get_group_types' ) ) {
3638 return [];
3639 }
3640
3641 $registered_types = bp_groups_get_group_types();
3642 if ( ! empty( $registered_types ) ) {
3643 foreach ( $registered_types as $key => $type ) {
3644 $options[] = [
3645 'label' => $type,
3646 'value' => $key,
3647 ];
3648 }
3649 }
3650 return [
3651 'options' => $options,
3652 'hasMore' => false,
3653 ];
3654 }
3655
3656 /**
3657 * Get BuddyBoss Forum Topic Status.
3658 *
3659 * @param array $data data.
3660 *
3661 * @return array|void
3662 */
3663 public function search_bb_forum_topic_status( $data ) {
3664 $options = [];
3665
3666 if ( ! function_exists( 'bbp_get_topic_post_type' ) ) {
3667 return [];
3668 }
3669
3670 $page = $data['page'];
3671 $limit = Utilities::get_search_page_limit();
3672 $offset = $limit * ( $page - 1 );
3673
3674 $forum_args = [
3675 'post_type' => bbp_get_topic_post_type(),
3676 'orderby' => 'title',
3677 'order' => 'ASC',
3678 'post_status' => [ 'publish', 'private' ],
3679 'posts_per_page' => $limit,
3680 'offset' => $offset,
3681 ];
3682
3683 $forums = get_posts( $forum_args );
3684 if ( ! empty( $forums ) ) {
3685 foreach ( $forums as $key => $forum ) {
3686 $options[] = [
3687 'label' => $forum,
3688 'value' => $key,
3689 ];
3690 }
3691 }
3692 $count = count( $options );
3693 return [
3694 'options' => $options,
3695 'hasMore' => $count > $limit && $count > $offset,
3696 ];
3697 }
3698
3699 /**
3700 * Get BuddyBoss Forums List.
3701 *
3702 * @param array $data data.
3703 *
3704 * @return array|void
3705 */
3706 public function search_bb_forums_list( $data ) {
3707 $options = [];
3708
3709 $page = $data['page'];
3710 $limit = Utilities::get_search_page_limit();
3711 $offset = $limit * ( $page - 1 );
3712
3713 if ( ! function_exists( 'bbp_get_forum_post_type' ) ) {
3714 return [];
3715 }
3716
3717 $forum_args = [
3718 'post_type' => bbp_get_forum_post_type(),
3719 'orderby' => 'title',
3720 'order' => 'ASC',
3721 'post_status' => [ 'publish', 'private' ],
3722 'posts_per_page' => $limit,
3723 'offset' => $offset,
3724 ];
3725
3726 $forums = get_posts( $forum_args );
3727 if ( ! empty( $forums ) ) {
3728 foreach ( $forums as $key => $forum ) {
3729 $options[] = [
3730 'label' => $forum->post_title,
3731 'value' => $forum->ID,
3732 ];
3733 }
3734 }
3735 $count = count( $options );
3736 return [
3737 'options' => $options,
3738 'hasMore' => $count > $limit && $count > $offset,
3739 ];
3740 }
3741
3742 /**
3743 * Get BuddyBoss Forums List.
3744 *
3745 * @param array $data data.
3746 *
3747 * @return array|void
3748 */
3749 public function search_bb_topics_list( $data ) {
3750 $options = [];
3751
3752 $page = $data['page'];
3753 $limit = Utilities::get_search_page_limit();
3754 $offset = $limit * ( $page - 1 );
3755
3756 if ( ! function_exists( 'bbp_get_topic_post_type' ) ) {
3757 return [];
3758 }
3759
3760 $topic_args = [
3761 'post_type' => bbp_get_topic_post_type(),
3762 'post_parent' => $data['dynamic'],
3763 'orderby' => 'title',
3764 'order' => 'ASC',
3765 'post_status' => [ 'publish', 'private' ],
3766 'posts_per_page' => $limit,
3767 'offset' => $offset,
3768 ];
3769
3770 $topics = get_posts( $topic_args );
3771 if ( ! empty( $topics ) ) {
3772 foreach ( $topics as $key => $topic ) {
3773 $options[] = [
3774 'label' => $topic->post_title,
3775 'value' => $topic->ID,
3776 ];
3777 }
3778 }
3779 $count = count( $options );
3780 return [
3781 'options' => $options,
3782 'hasMore' => $count > $limit && $count > $offset,
3783 ];
3784 }
3785
3786 /**
3787 * Prepare elementor forms.
3788 *
3789 * @param array $data Search Params.
3790 *
3791 * @return array[]
3792 */
3793 public function search_elementor_forms( $data ) {
3794
3795 $elementor_forms = Utilities::get_elementor_forms();
3796
3797 $options = [];
3798 if ( ! empty( $elementor_forms ) ) {
3799 foreach ( $elementor_forms as $key => $value ) {
3800 $options[] = [
3801 'label' => $value,
3802 'value' => $key,
3803 ];
3804 }
3805 }
3806
3807 return [
3808 'options' => $options,
3809 'hasMore' => false,
3810 ];
3811 }
3812
3813 /**
3814 * Prepare elementor forms.
3815 *
3816 * @param array $data Search Params.
3817 *
3818 * @return array[]
3819 */
3820 public function search_new_elementor_forms( $data ) {
3821
3822 global $wpdb;
3823 $elementor_forms = [];
3824 $post_metas = $wpdb->get_results(
3825 $wpdb->prepare(
3826 "SELECT pm.post_id, pm.meta_value
3827 FROM $wpdb->postmeta pm
3828 LEFT JOIN $wpdb->posts p
3829 ON p.ID = pm.post_id
3830 WHERE p.post_type IS NOT NULL
3831 AND p.post_status = %s
3832 AND pm.meta_key = %s
3833 AND pm.`meta_value` LIKE %s",
3834 'publish',
3835 '_elementor_data',
3836 '%%form_fields%%'
3837 )
3838 );
3839
3840 if ( ! empty( $post_metas ) ) {
3841 foreach ( $post_metas as $post_meta ) {
3842 /**
3843 *
3844 * Ignore line
3845 *
3846 * @phpstan-ignore-next-line
3847 */
3848 $inner_forms = Utilities::search_elementor_forms( json_decode( $post_meta->meta_value ) );
3849 if ( ! empty( $inner_forms ) ) {
3850 foreach ( $inner_forms as $form ) {
3851 /**
3852 *
3853 * Ignore line
3854 *
3855 * @phpstan-ignore-next-line
3856 */
3857 $elementor_forms[ $post_meta->post_id . '_' . $form->id ] = $form->settings->form_name . ' (' . $form->id . ')';
3858 }
3859 }
3860 }
3861 }
3862
3863 $options = [];
3864 if ( ! empty( $elementor_forms ) ) {
3865 foreach ( $elementor_forms as $key => $value ) {
3866 $options[] = [
3867 'label' => $value,
3868 'value' => $key,
3869 ];
3870 }
3871 }
3872
3873 /**
3874 *
3875 * Ignore line
3876 *
3877 * @phpstan-ignore-next-line
3878 */
3879 return [
3880 'options' => $options,
3881 'hasMore' => false,
3882 ];
3883 }
3884
3885 /**
3886 * Prepare elementor form fields.
3887 *
3888 * @param array $data Search Params.
3889 *
3890 * @return array[]
3891 */
3892 public function search_pluggable_elementor_form_fields( $data ) {
3893 $result = [];
3894 $form_id = absint( $data['dynamic'] );
3895 $elementor_form_fields = ( new Utilities() )->get_elementor_form_fields( $data );
3896 $options = [];
3897 if ( ! empty( $elementor_form_fields ) ) {
3898 foreach ( $elementor_form_fields as $key => $value ) {
3899 $options[] = [
3900 'label' => $value,
3901 'value' => '{' . $key . '}',
3902 ];
3903 }
3904 }
3905
3906 return [
3907 'options' => $options,
3908 'hasMore' => false,
3909 ];
3910 }
3911
3912 /**
3913 * Get all events
3914 *
3915 * @param array $data Data array.
3916 *
3917 * @return array
3918 */
3919 public function search_event_calendar_event( $data ) {
3920 $page = $data['page'];
3921 $limit = Utilities::get_search_page_limit();
3922 $offset = $limit * ( $page - 1 );
3923
3924 $posts = get_posts(
3925 [
3926 'post_type' => 'tribe_events',
3927 'orderby' => 'title',
3928 'order' => 'ASC',
3929 'post_status' => 'publish',
3930 'posts_per_page' => $limit,
3931 'offset' => $offset,
3932 ]
3933 );
3934
3935 $options = [];
3936 if ( ! empty( $posts ) ) {
3937 foreach ( $posts as $post ) {
3938 $options[] = [
3939 'label' => $post->post_title,
3940 'value' => $post->ID,
3941 ];
3942 }
3943 }
3944
3945 $count = wp_count_posts( 'tribe_events' )->publish;
3946
3947 return [
3948 'options' => $options,
3949 'hasMore' => $count > $limit && $count > $offset,
3950 ];
3951 }
3952
3953 /**
3954 * Get all events tickets
3955 *
3956 * @param array $data Data array.
3957 *
3958 * @return array
3959 */
3960 public function search_event_calendar_event_tickets( $data ) {
3961 $event_id = $data['dynamic'];
3962 $options = [];
3963
3964 if ( ! class_exists( 'Tribe__Tickets_Plus__Commerce__WooCommerce__Main' ) ) {
3965 return [];
3966 }
3967
3968 $tickets = \Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance()->get_all_event_tickets( $event_id );
3969 if ( ! empty( $tickets ) ) {
3970 foreach ( $tickets as $ticket_object ) {
3971 $options[] = [
3972 'label' => $ticket_object->name,
3973 'value' => $ticket_object->ID,
3974 ];
3975 }
3976 }
3977
3978 return [
3979 'options' => $options,
3980 'hasMore' => false,
3981 ];
3982 }
3983
3984 /**
3985 * Prepare rsvp event calendar events.
3986 *
3987 * @param array $data Search Params.
3988 *
3989 * @return array
3990 */
3991 public function search_event_calendar_rsvp_event( $data ) {
3992
3993 $page = $data['page'];
3994 $limit = Utilities::get_search_page_limit();
3995 $offset = $limit * ( $page - 1 );
3996
3997 $posts = get_posts(
3998 [
3999 'post_type' => 'tribe_events',
4000 'orderby' => 'title',
4001 'order' => 'ASC',
4002 'post_status' => 'publish',
4003 'posts_per_page' => $limit,
4004 'offset' => $offset,
4005 ]
4006 );
4007
4008 $options = [];
4009 if ( ! empty( $posts ) ) {
4010 $ticket_handler = new Tribe__Tickets__Tickets_Handler();
4011 foreach ( $posts as $post ) {
4012
4013 $get_rsvp_ticket = $ticket_handler->get_event_rsvp_tickets( $post->ID );
4014
4015 if ( ! empty( $get_rsvp_ticket ) ) {
4016 $options[] = [
4017 'label' => $post->post_title,
4018 'value' => $post->ID,
4019 ];
4020 }
4021 }
4022 }
4023 $count = wp_count_posts( 'tribe_events' )->publish;
4024
4025 return [
4026 'options' => $options,
4027 'hasMore' => $count > $limit && $count > $offset,
4028 ];
4029 }
4030
4031 /**
4032 * Prepare Restrict Content Membership Level.
4033 *
4034 * @param array $data Search Params.
4035 *
4036 * @return array[]
4037 */
4038 public function search_restrictcontent_membership_level( $data ) {
4039
4040 $rcp_memberships = rcp_get_membership_levels();
4041 $options = [];
4042
4043 if ( ! empty( $rcp_memberships ) ) {
4044 foreach ( $rcp_memberships as $list ) {
4045 $options[] = [
4046 'label' => ucfirst( $list->name ),
4047 'value' => $list->id,
4048 ];
4049 }
4050 }
4051
4052 return [
4053 'options' => $options,
4054 'hasMore' => false,
4055 ];
4056 }
4057
4058 /**
4059 * Prepare Restrict Content Customer.
4060 *
4061 * @param array $data Search Params.
4062 *
4063 * @return array[]
4064 */
4065 public function search_restrictcontent_customer( $data ) {
4066
4067 $rcp_users = rcp_get_memberships();
4068 $options = [];
4069
4070 if ( ! empty( $rcp_users ) ) {
4071 foreach ( $rcp_users as $list ) {
4072 $user = get_user_by( 'ID', $list->get_user_id() );
4073 $user_label = $user->user_email;
4074
4075 if ( $user->display_name !== $user->user_email ) {
4076 $user_label .= ' (' . $user->display_name . ')';
4077 }
4078
4079 $options[] = [
4080 'label' => $user_label,
4081 'value' => $list->get_customer_id(),
4082 ];
4083 }
4084 }
4085
4086 return [
4087 'options' => $options,
4088 'hasMore' => false,
4089 ];
4090 }
4091
4092
4093 /**
4094 * Fetch the Presto Player video List.
4095 *
4096 * @param array $data Search Params.
4097 *
4098 * @return array[]
4099 */
4100 public function search_ap_presto_player_video_list( $data ) {
4101
4102 $videos = ( new Video() )->all();
4103 $options = [];
4104 if ( ! empty( $videos ) ) {
4105 foreach ( $videos as $video ) {
4106 $options[] = [
4107 'label' => $video->__get( 'title' ),
4108 'value' => (string) $video->__get( 'id' ),
4109 ];
4110 }
4111 }
4112
4113 return [
4114 'options' => $options,
4115 'hasMore' => false,
4116 ];
4117 }
4118
4119 /**
4120 * Presto Player Video percentage.
4121 *
4122 * @param array $data Search Params.
4123 *
4124 * @return array[]
4125 */
4126 public function search_prestoplayer_video_percent( $data ) {
4127
4128 $percents = [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ];
4129 $options = [];
4130
4131 foreach ( $percents as $percent ) {
4132 $options[] = [
4133 'label' => $percent . '%',
4134 'value' => (string) $percent,
4135 ];
4136 }
4137
4138 return [
4139 'options' => $options,
4140 'hasMore' => false,
4141 ];
4142 }
4143
4144 /**
4145 * Get user profile field options.
4146 *
4147 * @return array
4148 * @since 1.0.0
4149 */
4150 public function search_user_field_options() {
4151
4152 $options = apply_filters(
4153 'sure_trigger_get_user_field_options',
4154 [
4155 [
4156 'label' => __( 'User Name', 'suretriggers' ),
4157 'value' => 'user_login',
4158 ],
4159 [
4160 'label' => __( 'User Email', 'suretriggers' ),
4161 'value' => 'user_email',
4162 ],
4163 [
4164 'label' => __( 'Display Name', 'suretriggers' ),
4165 'value' => 'display_name',
4166 ],
4167 [
4168 'label' => __( 'User Password', 'suretriggers' ),
4169 'value' => 'user_pass',
4170 ],
4171 [
4172 'label' => __( 'Website', 'suretriggers' ),
4173 'value' => 'user_url',
4174 ],
4175 ]
4176 );
4177
4178 return [
4179 'options' => $options,
4180 'hasMore' => false,
4181 ];
4182 }
4183
4184 /**
4185 * Get user post field options.
4186 *
4187 * @return array
4188 * @since 1.0.0
4189 */
4190 public function search_post_field_options() {
4191
4192 return [
4193 'options' => [
4194 [
4195 'label' => __( 'Type', 'suretriggers' ),
4196 'value' => 'post_type',
4197 'dynamic_field' => [
4198 'type' => 'select-creatable',
4199 'ajaxIdentifier' => 'post_types',
4200 ],
4201 ],
4202 [
4203 'label' => __( 'Status', 'suretriggers' ),
4204 'value' => 'post_status',
4205 'dynamic_field' => [
4206 'type' => 'select-async',
4207 'ajaxIdentifier' => 'post_statuses',
4208 ],
4209 ],
4210 [
4211 'label' => __( 'Author', 'suretriggers' ),
4212 'value' => 'post_author',
4213 'dynamic_field' => [
4214 'type' => 'select-async',
4215 'ajaxIdentifier' => 'user',
4216 ],
4217 ],
4218 [
4219 'label' => __( 'Title', 'suretriggers' ),
4220 'value' => 'post_title',
4221 'dynamic_field' => [
4222 'type' => 'select-creatable',
4223 ],
4224 ],
4225 [
4226 'label' => __( 'Slug', 'suretriggers' ),
4227 'value' => 'post_slug',
4228 'dynamic_field' => [
4229 'type' => 'select-creatable',
4230 ],
4231 ],
4232 [
4233 'label' => __( 'Content', 'suretriggers' ),
4234 'value' => 'post_content',
4235 'dynamic_field' => [
4236 'type' => 'html-editor',
4237 ],
4238 ],
4239 ],
4240 'hasMore' => false,
4241 ];
4242 }
4243
4244 /**
4245 * Bricksbuilder grouped data.
4246 *
4247 * @param array $data data.
4248 * @return array
4249 */
4250 public function search_bb_groups( $data ) {
4251
4252 global $wpdb;
4253 $options = [];
4254
4255 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'bp_groups' ) ) ) {
4256
4257 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %s', $wpdb->prefix . 'bp_groups' ) );
4258
4259 if ( $results ) {
4260 foreach ( $results as $result ) {
4261 $options[] = [
4262 'label' => $result->name,
4263 'value' => $result->id,
4264 ];
4265 }
4266 }
4267 }
4268
4269 return [
4270 'options' => $options,
4271 'hasMore' => false,
4272 ];
4273 }
4274
4275 /**
4276 * Search forms.
4277 *
4278 * @return array
4279 */
4280 public function search_bb_forums() {
4281 $options = [];
4282 $allowed_atatus = [ 'publish', 'private' ];
4283 $forum_args = [
4284 'post_type' => bbp_get_forum_post_type(),
4285 'posts_per_page' => -1,
4286 'orderby' => 'title',
4287 'order' => 'ASC',
4288 'post_status' => 'any',
4289 ];
4290 $forums = get_posts( $forum_args );
4291
4292 if ( ! empty( $forums ) ) {
4293 foreach ( $forums as $forum ) {
4294 if ( in_array( $forum->post_status, $allowed_atatus, true ) ) {
4295 $options[] = [
4296 'label' => $forum->post_title,
4297 'value' => $forum->ID,
4298 ];
4299 }
4300 }
4301 }
4302 return [
4303 'options' => $options,
4304 'hasMore' => false,
4305 ];
4306 }
4307
4308 /**
4309 * Search Affiliate WP Referral Type.
4310 *
4311 * @return array
4312 */
4313 public function search_affwp_referral_type() {
4314 $options = [];
4315
4316 if ( ! function_exists( 'affiliate_wp' ) ) {
4317 return [];
4318 }
4319
4320 $types = affiliate_wp()->referrals->types_registry->get_types();
4321 if ( ! empty( $types ) ) {
4322 foreach ( $types as $type_id => $type ) {
4323 $options[] = [
4324 'label' => $type['label'],
4325 'value' => $type_id,
4326 ];
4327 }
4328 }
4329 return [
4330 'options' => $options,
4331 'hasMore' => false,
4332 ];
4333 }
4334
4335 /**
4336 * Search Affiliate WP Referral Status.
4337 *
4338 * @return array
4339 */
4340 public function search_affwp_referral_status() {
4341 $options = [];
4342
4343 if ( ! function_exists( 'affwp_get_affiliate_statuses' ) ) {
4344 return [];
4345 }
4346
4347 $statuses = affwp_get_affiliate_statuses();
4348 if ( ! empty( $statuses ) ) {
4349 foreach ( $statuses as $key => $status ) {
4350 $options[] = [
4351 'label' => $status,
4352 'value' => $key,
4353 ];
4354 }
4355 }
4356 return [
4357 'options' => $options,
4358 'hasMore' => false,
4359 ];
4360 }
4361
4362 /**
4363 * Search Affiliate WP Affiliates list.
4364 *
4365 * @return array
4366 */
4367 public function search_affwp_affiliates_list() {
4368 $options = [];
4369
4370 global $wpdb;
4371 $affiliates = $wpdb->get_results( "SELECT affiliate_id FROM {$wpdb->prefix}affiliate_wp_affiliates" );
4372
4373 if ( ! function_exists( 'affwp_get_affiliate_name' ) ) {
4374 return [];
4375 }
4376
4377 if ( ! empty( $affiliates ) ) {
4378 foreach ( $affiliates as $affiliate ) {
4379 $options[] = [
4380 'label' => affwp_get_affiliate_name( $affiliate->affiliate_id ),
4381 'value' => $affiliate->affiliate_id,
4382 ];
4383 }
4384 }
4385 return [
4386 'options' => $options,
4387 'hasMore' => false,
4388 ];
4389 }
4390
4391 /**
4392 * Search Slice WP Affiliates list.
4393 *
4394 * @return array
4395 */
4396 public function search_slicewp_affiliates_list() {
4397 $options = [];
4398
4399 global $wpdb;
4400 $affiliates = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}slicewp_affiliates" );
4401
4402 if ( ! function_exists( 'slicewp_get_affiliate_name' ) ) {
4403 return [];
4404 }
4405
4406 if ( ! empty( $affiliates ) ) {
4407 foreach ( $affiliates as $affiliate ) {
4408 $options[] = [
4409 'label' => slicewp_get_affiliate_name( $affiliate->id ),
4410 'value' => $affiliate->id,
4411 ];
4412 }
4413 }
4414 return [
4415 'options' => $options,
4416 'hasMore' => false,
4417 ];
4418 }
4419
4420 /**
4421 * Search for commissions list.
4422 *
4423 * @param array $data data.
4424 *
4425 * @return array
4426 */
4427 public function search_slicewp_commissions_list( $data ) {
4428 $options = [];
4429 global $wpdb;
4430 $affiliate_id = $data['dynamic']['affiliate_id'];
4431 $commissions = $wpdb->get_results(
4432 $wpdb->prepare(
4433 "SELECT *
4434 FROM {$wpdb->prefix}slicewp_commissions WHERE affiliate_id=%d ORDER BY id DESC ",
4435 $affiliate_id
4436 )
4437 );
4438
4439
4440 if ( ! empty( $commissions ) ) {
4441 foreach ( $commissions as $commission ) {
4442 $options[] = [
4443 'label' => $commission->reference . '(' . $commission->type . ')',
4444 'value' => $commission->id,
4445 ];
4446 }
4447 }
4448 return [
4449 'options' => $options,
4450 'hasMore' => false,
4451 ];
4452 }
4453
4454
4455 /**
4456 * Search Affiliate WP Affiliates list.
4457 *
4458 * @return array
4459 */
4460 public function search_affwp_affiliates_rate_type() {
4461 $options = [];
4462
4463 if ( ! function_exists( 'affwp_get_affiliate_rate_types' ) ) {
4464 return [];
4465 }
4466
4467 $rate_types = affwp_get_affiliate_rate_types();
4468
4469 if ( ! empty( $rate_types ) ) {
4470 foreach ( $rate_types as $key => $rate_type ) {
4471 $options[] = [
4472 'label' => $rate_type,
4473 'value' => $key,
4474 ];
4475 }
4476 }
4477 return [
4478 'options' => $options,
4479 'hasMore' => false,
4480 ];
4481 }
4482
4483 /**
4484 * Get last data for trigger.
4485 *
4486 * @param array $data data.
4487 * @return array
4488 */
4489 public function search_affiliate_wp_triggers_last_data( $data ) {
4490 global $wpdb;
4491
4492 $context = [];
4493 $context['response_type'] = 'sample';
4494
4495 $user_data = WordPress::get_sample_user_context();
4496
4497 $affiliate_data = [
4498 'affiliate_id' => 1,
4499 'rest_id' => '',
4500 'user_id' => 1,
4501 'rate' => '',
4502 'rate_type' => '',
4503 'flat_rate_basis' => '',
4504 'payment_email' => 'admin@gmail.com',
4505 'status' => 'active',
4506 'earnings' => 0,
4507 'unpaid_earnings' => 0,
4508 'referrals' => 0,
4509 'visits' => 0,
4510 'date_registered' => '2023-01-18 13:35:22',
4511 'dynamic_coupon' => 'KDJSKS',
4512 ];
4513
4514 $referral_data = [
4515 'referral_id' => 1,
4516 'affiliate_id' => 1,
4517 'visit_id' => 0,
4518 'rest_id' => '',
4519 'customer_id' => 0,
4520 'parent_id' => 0,
4521 'description' => 'Testing',
4522 'status' => 'unpaid',
4523 'amount' => '12.00',
4524 'currency' => '',
4525 'custom' => 'custom',
4526 'context' => '',
4527 'campaign' => '',
4528 'reference' => 'Reference',
4529 'products' => '',
4530 'date' => '2023-01-18 16:36:59',
4531 'type' => 'opt-in',
4532 'payout_id' => 0,
4533 ];
4534
4535 if ( ! function_exists( 'affwp_get_dynamic_affiliate_coupons' ) || ! function_exists( 'affwp_get_affiliate' ) || ! function_exists( 'affwp_get_referral' ) ) {
4536 return [];
4537 }
4538
4539 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4540
4541 if ( in_array( $term, [ 'affiliate_approved', 'affiliate_awaiting_approval' ], true ) ) {
4542 $status = 'affiliate_approved' === $term ? 'active' : 'pending';
4543 $affiliate = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_affiliates WHERE affiliate_id = ( SELECT max(affiliate_id) FROM {$wpdb->prefix}affiliate_wp_affiliates )" );
4544
4545 if ( ! empty( $affiliate ) ) {
4546 $affiliate = affwp_get_affiliate( $affiliate->affiliate_id );
4547 $affiliate_data = get_object_vars( $affiliate );
4548 $user_data = WordPress::get_user_context( $affiliate->user_id );
4549 $context['response_type'] = 'live';
4550 }
4551 $affiliate_data['status'] = $status;
4552 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data );
4553
4554 } elseif ( 'affiliate_makes_referral' == $term ) {
4555 $type = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4556 $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 ) );
4557
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 } elseif ( 'affiliate_wc_product_purchased' == $term ) {
4568 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4569 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'woocommerce' ORDER BY referral_id DESC LIMIT 1" );
4570
4571 if ( ! empty( $referral ) ) {
4572 $referral = affwp_get_referral( $referral->referral_id );
4573 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4574 $affiliate_data = get_object_vars( $affiliate );
4575 if ( ! empty( $referral ) && function_exists( 'wc_get_order' ) ) {
4576 $order_id = $referral->reference;
4577 $order = wc_get_order( $order_id );
4578 if ( $order instanceof \WC_Order ) {
4579 $user_data = WordPress::get_user_context( $order->get_customer_id() );
4580 }
4581 }
4582 $referral_data = get_object_vars( $referral );
4583 $context['response_type'] = 'live';
4584 }
4585 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
4586 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data, $dynamic_coupons );
4587 if ( ! empty( $referral ) && function_exists( 'wc_get_order' ) ) {
4588 $order_id = $referral->reference;
4589 $order = wc_get_order( $order_id );
4590 $items = $order->get_items();
4591 foreach ( $items as $item ) {
4592 $context['pluggable_data']['product'] = $item['product_id'];
4593 }
4594 } else {
4595 $context['pluggable_data']['product'] = 1;
4596 }
4597 } elseif ( 'affiliate_edd_product_purchased' == $term ) {
4598 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4599 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'edd' ORDER BY referral_id DESC LIMIT 1" );
4600
4601 if ( ! empty( $referral ) ) {
4602 $referral = affwp_get_referral( $referral->referral_id );
4603 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4604 $affiliate_data = get_object_vars( $affiliate );
4605 $user_data = WordPress::get_user_context( $affiliate->user_id );
4606 $referral_data = get_object_vars( $referral );
4607 $context['response_type'] = 'live';
4608 }
4609 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4610 if ( ! empty( $referral ) && function_exists( 'edd_get_payment' ) ) {
4611 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
4612 $edd_payment_id = $referral->reference;
4613 $payment = edd_get_payment( $edd_payment_id );
4614 $cart_details = $payment->cart_details;
4615 $payment = get_object_vars( $payment );
4616 $context['pluggable_data'] = array_merge( $context['pluggable_data'], $dynamic_coupons, $payment );
4617 foreach ( $cart_details as $detail ) {
4618 $context['pluggable_data']['product'] = $detail['id'];
4619 }
4620 } else {
4621 $context['pluggable_data']['product'] = 1;
4622 }
4623 } elseif ( 'affiliate_mb_product_purchased' == $term ) {
4624 $product = isset( $data['dynamic'] ) ? $data['dynamic'] : '';
4625 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE context = 'memberpress' ORDER BY referral_id DESC LIMIT 1" );
4626
4627 if ( ! empty( $referral ) && class_exists( '\MeprTransaction' ) ) {
4628 $referral = affwp_get_referral( $referral->referral_id );
4629 $reference_id = $referral->reference;
4630 $transaction = new MeprTransaction( $reference_id );
4631 $user_id = $transaction->user_id;
4632 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4633 $affiliate_data = get_object_vars( $affiliate );
4634 $user_data = WordPress::get_user_context( $user_id );
4635 $referral_data = get_object_vars( $referral );
4636 $context['response_type'] = 'live';
4637 }
4638 $dynamic_coupons = affwp_get_dynamic_affiliate_coupons( $referral->affiliate_id, false );
4639 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data, $dynamic_coupons );
4640 if ( ! empty( $referral ) && class_exists( '\MeprTransaction' ) ) {
4641 $membership_id = $wpdb->get_var(
4642 $wpdb->prepare(
4643 "SELECT product_id FROM
4644 {$wpdb->prefix}mepr_transactions WHERE id = %d",
4645 $referral->reference
4646 )
4647 );
4648 $context['pluggable_data']['product'] = $membership_id;
4649 $context['pluggable_data']['product_name'] = get_the_title( $membership_id );
4650 } else {
4651 $context['pluggable_data']['product'] = 1;
4652 $context['pluggable_data']['product_name'] = 'membership1';
4653 }
4654 } elseif ( 'affiliate_referral_paid' == $term ) {
4655 $type = isset( $data['filter']['type']['value'] ) ? $data['filter']['type']['value'] : '';
4656 $referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE type = %s AND status = 'paid'", $type ) );
4657
4658 if ( ! empty( $referral ) ) {
4659 $referral = affwp_get_referral( $referral->referral_id );
4660 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4661 $affiliate_data = get_object_vars( $affiliate );
4662 $user_data = WordPress::get_user_context( $affiliate->user_id );
4663 $referral_data = get_object_vars( $referral );
4664 $context['response_type'] = 'live';
4665 }
4666 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4667 } elseif ( 'affiliate_referral_rejected' == $term ) {
4668 $type = isset( $data['filter']['type']['value'] ) ? $data['filter']['type']['value'] : '';
4669 $referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE type = %s AND status = 'rejected'", $type ) );
4670 if ( ! empty( $referral ) ) {
4671 $referral = affwp_get_referral( $referral->referral_id );
4672 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4673 $affiliate_data = get_object_vars( $affiliate );
4674 $user_data = WordPress::get_user_context( $affiliate->user_id );
4675 $referral_data = get_object_vars( $referral );
4676 $context['response_type'] = 'live';
4677 }
4678 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4679 } else {
4680 $referral = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}affiliate_wp_referrals WHERE referral_id = ( SELECT max(referral_id) FROM {$wpdb->prefix}affiliate_wp_referrals )" );
4681
4682 if ( ! empty( $referral ) ) {
4683 $referral = affwp_get_referral( $referral->referral_id );
4684 $affiliate = affwp_get_affiliate( $referral->affiliate_id );
4685 $affiliate_data = get_object_vars( $affiliate );
4686 $user_data = WordPress::get_user_context( $affiliate->user_id );
4687 $referral_data = get_object_vars( $referral );
4688 $context['response_type'] = 'live';
4689 }
4690 $context['pluggable_data'] = array_merge( $user_data, $affiliate_data, $referral_data );
4691 }
4692
4693 return $context;
4694 }
4695
4696 /**
4697 * Get last data for trigger.
4698 *
4699 * @param array $data data.
4700 * @return array
4701 */
4702 public function search_jetpack_crm_triggers_last_data( $data ) {
4703
4704 if ( ! function_exists( 'zeroBS_getCompanyCount' ) || ! function_exists( 'zeroBS_getCustomerCount' ) || ! function_exists( 'zeroBS_getQuoteCount' ) ) {
4705 return [];
4706 }
4707
4708 global $wpdb;
4709
4710 $context = [];
4711 $context['response_type'] = 'sample';
4712
4713 $company_id = [ 'company_id' => 1 ];
4714 $contact_id = [ 'contact_id' => 1 ];
4715 $quote_id = [ 'quote_id' => 1 ];
4716 $event_id = [ 'event_id' => 1 ];
4717 $invoice_id = [ 'invoice_id' => 1 ];
4718 $transaction_id = [ 'transaction_id' => 1 ];
4719
4720 $company = [
4721 'company_id' => 1,
4722 'company_status' => 'Lead',
4723 'company_name' => 'Example Company',
4724 'company_email' => 'info@example.com',
4725 'main_address_line_1' => '123 Main Street',
4726 'main_address_line_2' => 'Suite 456',
4727 'main_address_city' => 'New York',
4728 'main_address_state' => 'NY',
4729 'main_address_postal_code' => '10001',
4730 'main_address_country' => 'United States',
4731 'second_address_line_1' => '789 Second Avenue',
4732 'second_address_line_2' => 'Floor 10',
4733 'second_address_city' => 'Los Angeles',
4734 'second_address_state' => 'CA',
4735 'second_address_postal_code' => '90001',
4736 'second_address_country' => 'United States',
4737 'main_telephone' => '+1 123-456-7890',
4738 'secondary_telephone' => '+1 987-654-3210',
4739 ];
4740
4741 $contact = [
4742 'contact_id' => 1,
4743 'status' => 'Lead',
4744 'prefix' => 'Mr.',
4745 'full_name' => 'John Doe',
4746 'first_name' => 'John',
4747 'last_name' => 'Doe',
4748 'email' => 'johndoe@example.com',
4749 'main_address_line_1' => '123 Main Street',
4750 'main_address_line_2' => 'Suite 456',
4751 'main_address_city' => 'New York',
4752 'main_address_state' => 'NY',
4753 'main_address_postal_code' => '10001',
4754 'main_address_country' => 'United States',
4755 'second_address_line_1' => '789 Second Avenue',
4756 'second_address_line_2' => 'Floor 10',
4757 'second_address_city' => 'Los Angeles',
4758 'second_address_state' => 'CA',
4759 'second_address_postal_code' => '90001',
4760 'second_address_country' => 'United States',
4761 'home_telephone' => '+1 123-456-7890',
4762 'work_telephone' => '+1 987-654-3210',
4763 'mobile_telephone' => '+1 555-555-5555',
4764 ];
4765
4766 $quote = [
4767 'quote_id' => 1,
4768 'contact_id' => 2,
4769 'contact_email' => 'john@example.com',
4770 'contact_name' => 'John Doe',
4771 'status' => '<strong>Created, not yet accepted</strong>',
4772 'title' => 'Sample Quote',
4773 'value' => 1000,
4774 'date' => '2023-05-23',
4775 'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
4776 'notes' => 'Additional notes about the quote.',
4777 ];
4778
4779 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4780
4781 switch ( $term ) {
4782 case 'company_created':
4783 if ( zeroBS_getCompanyCount() > 0 ) {
4784 $company_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_companies WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_companies )" );
4785 $company = JetpackCRM::get_company_context( $company_data->ID );
4786 $context['response_type'] = 'live';
4787 }
4788 $context['pluggable_data'] = $company;
4789 break;
4790 case 'company_deleted':
4791 $context['pluggable_data'] = $company_id;
4792 break;
4793 case 'contact_created':
4794 if ( zeroBS_getCustomerCount() > 0 ) {
4795 $contact_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_contacts WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_contacts )" );
4796 $contact = JetpackCRM::get_contact_context( $contact_data->ID );
4797 $context['response_type'] = 'live';
4798 }
4799 $context['pluggable_data'] = $contact;
4800 break;
4801 case 'contact_deleted':
4802 $context['pluggable_data'] = $contact_id;
4803 break;
4804 case 'event_deleted':
4805 $context['pluggable_data'] = $event_id;
4806 break;
4807 case 'invoice_deleted':
4808 $context['pluggable_data'] = $invoice_id;
4809 break;
4810 case 'quote_accepted':
4811 case 'quote_created':
4812 if ( zeroBS_getQuoteCount() > 0 ) {
4813 $quote_data = $wpdb->get_row( "SELECT ID FROM {$wpdb->prefix}zbs_quotes WHERE ID = ( SELECT max(ID) FROM {$wpdb->prefix}zbs_quotes )" );
4814 $quote = JetpackCRM::get_quote_context( $quote_data->ID );
4815 $context['response_type'] = 'live';
4816 }
4817 $context['pluggable_data'] = $quote;
4818 break;
4819 case 'quote_deleted':
4820 $context['pluggable_data'] = $quote_id;
4821 break;
4822 case 'transaction_deleted':
4823 $context['pluggable_data'] = $transaction_id;
4824 break;
4825 }
4826
4827 return $context;
4828 }
4829
4830 /**
4831 * Get last data for trigger.
4832 *
4833 * @param array $data data.
4834 * @return array
4835 */
4836 public function search_funnel_kit_automations_triggers_last_data( $data ) {
4837
4838 if ( ! class_exists( 'BWFCRM_Contact' ) || ! class_exists( 'BWFCRM_Lists' ) || ! class_exists( 'BWFCRM_Tag' ) ) {
4839 return [];
4840 }
4841
4842 $context = [];
4843 $context['response_type'] = 'sample';
4844
4845 $contact = [
4846 'contact_id' => '1',
4847 'wpid' => '0',
4848 'uid' => '9e74246335fd81b1c4a9123842c12549',
4849 'email' => 'johndoe@example.com',
4850 'first_name' => 'John',
4851 'last_name' => 'Doe',
4852 'contact_no' => '+1 555-555-5555',
4853 'state' => 'NY',
4854 'country' => 'United States',
4855 'timezone' => 'New York',
4856 'creation_date' => '2023-05-29 15:26:03',
4857 'last_modified' => '2023-05-29 17:08:30',
4858 'source' => '',
4859 'type' => 'Los Angeles',
4860 'status' => '0',
4861 'tags' => '["1"]',
4862 'lists' => '["2","1"]',
4863 ];
4864
4865 $list = [
4866 'list_id' => 1,
4867 'list_name' => 'Sample List',
4868 ];
4869
4870 $tag = [
4871 'tag_id' => 1,
4872 'tag_name' => 'Sample Tag',
4873 ];
4874
4875 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4876
4877 $recent_contacts = \BWFCRM_Contact::get_recent_contacts( 0, 1, [] );
4878 $contact_email = count( $recent_contacts['contacts'] ) > 0 && isset( $recent_contacts['contacts'][0]['email'] ) ? $recent_contacts['contacts'][0]['email'] : '';
4879
4880 $real_contact = false;
4881 if ( ! empty( $contact_email ) ) {
4882 $contact_obj = \BWFCRM_Contact::get_contacts( $contact_email, 0, 1, [], [], OBJECT );
4883
4884 if ( isset( $contact_obj['contacts'][0] ) ) {
4885 $contact = FunnelKitAutomations::get_contact_context( $contact_obj['contacts'][0]->contact );
4886 $real_contact = true;
4887 }
4888 }
4889
4890 if ( 'contact_added_to_list' === $term || 'contact_removed_from_list' === $term ) {
4891 $list_id = (int) ( isset( $data['filter']['list_id']['value'] ) ? $data['filter']['list_id']['value'] : '-1' );
4892
4893 if ( -1 === $list_id ) {
4894 $lists = \BWFCRM_Lists::get_lists( [], '', 0, 1 );
4895 $list_id = count( $lists ) > 0 ? $lists[0]['ID'] : -1;
4896 }
4897
4898
4899 if ( -1 !== $list_id ) {
4900 $list = FunnelKitAutomations::get_list_context( $list_id );
4901 $context['response_type'] = $real_contact ? 'live' : 'sample';
4902 }
4903
4904 $context['pluggable_data'] = array_merge( $list, $contact );
4905 } else {
4906 $tag_id = (int) ( isset( $data['filter']['tag_id']['value'] ) ? $data['filter']['tag_id']['value'] : '-1' );
4907
4908 if ( -1 === $tag_id ) {
4909 $tags = \BWFCRM_Tag::get_tags( [], '', 0, 1 );
4910 $tag_id = count( $tags ) > 0 ? $tags[0]['ID'] : -1;
4911 }
4912
4913
4914 if ( -1 !== $tag_id ) {
4915 $tag = FunnelKitAutomations::get_tag_context( $tag_id );
4916 $context['response_type'] = $real_contact ? 'live' : 'sample';
4917 }
4918
4919 $context['pluggable_data'] = array_merge( $tag, $contact );
4920 }
4921
4922 return $context;
4923 }
4924
4925 /**
4926 * Get last data for trigger.
4927 *
4928 * @param array $data data.
4929 * @return array
4930 */
4931 public function search_edd_triggers_last_data( $data ) {
4932 global $wpdb;
4933 $context = [];
4934 $context['response_type'] = 'sample';
4935 $context['pluggable_data'] = [];
4936 $order_data = [
4937 'order_id' => 187,
4938 'customer_email' => 'john_doe@gmail.com',
4939 'customer_id' => 2,
4940 'user_id' => 1,
4941 'customer_first_name' => 'Sure',
4942 'customer_last_name' => 'Dev',
4943 'ordered_items' => 'Price with license — Price one',
4944 'currency' => 'USD',
4945 'status' => 'complete',
4946 'discount_codes' => '',
4947 'order_discounts' => 0.00,
4948 'order_subtotal' => 12.00,
4949 'order_tax' => 0.00,
4950 'order_total' => 12.00,
4951 'payment_method' => 'manual',
4952 'purchase_key' => 'd797b9576a3895e7424bae2417ed87df',
4953 'ordered_items_ids' => 17250,
4954 'download_id' => 17250,
4955 'license_key' => 'f7736093411cfaed18b56ec60227117b',
4956 'license_key_expire_date' => '1697524076',
4957 'license_key_status' => 'inactive',
4958 ];
4959
4960 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
4961 $download_id = isset( $data['filter']['download_id']['value'] ) ? $data['filter']['download_id']['value'] : 0;
4962 if ( 'order_created' === $term || 'order_one_product' === $term ) {
4963 $order_data['purchase_key'] = '06d3b7d923ca922dc889354f9bc33fbb';
4964
4965 $args = [
4966 'number' => 1,
4967 'status' => [ 'complete', 'refunded', 'partially_refunded', 'renewal' ],
4968 ];
4969 if ( $download_id > 0 ) {
4970 $args['download'] = $download_id;
4971 }
4972 $payments = edd_get_payments( $args );
4973 if ( count( $payments ) > 0 ) {
4974 $order_data = EDD::get_product_purchase_context( $payments[0], $term, $download_id );
4975
4976 $context['response_type'] = 'live';
4977 } else {
4978 if ( 'order_one_product' === $term ) {
4979 $order_data['price_id'] = 1;
4980 }
4981 }
4982 } elseif ( 'stripe_payment_refunded' === $term ) {
4983 $args = [
4984 'number' => 1,
4985 'status' => 'complete',
4986 'type' => 'refund',
4987 ];
4988 $payments = edd_get_payments( $args );
4989
4990 if ( count( $payments ) > 0 ) {
4991 $order_data = EDD::get_purchase_refund_context( $payments[0] );
4992 $context['response_type'] = 'live';
4993 }
4994 } else {
4995 $status = isset( $data['post_type'] ) ? $data['post_type'] : '';
4996 if ( ! empty( $status ) ) {
4997 if ( $download_id > 0 ) {
4998 $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 ) );
4999 } else {
5000 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE status= %s ORDER BY id DESC", $status ) );
5001 }
5002 } else {
5003 if ( $download_id > 0 ) {
5004 $licesnses = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}edd_licenses WHERE download_id=%d ORDER BY id DESC", $download_id ) );
5005 } else {
5006 $licesnses = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}edd_licenses ORDER BY id DESC" );
5007 }
5008 }
5009 if ( ! empty( $licesnses ) ) {
5010 $order_data = EDD::edd_get_license_data( $licesnses->id, $licesnses->download_id, $licesnses->payment_id );
5011 $context['response_type'] = 'live';
5012 } else {
5013 $order_data = [
5014 'ID' => 1,
5015 'key' => '23232323232',
5016 'customer_email' => 'suretest@example.com',
5017 'customer_name' => 'Sure Test',
5018 'product_id' => 1,
5019 'download_id' => 1,
5020 'product_name' => 'Test',
5021 'activation_limit' => 2,
5022 'activation_count' => 1,
5023 'activated_urls' => 'https://example.com',
5024 'expiration' => '1686297914',
5025 'is_lifetime' => '0',
5026 'status' => $status,
5027 ];
5028
5029 }
5030 }
5031
5032 $context['pluggable_data'] = $order_data;
5033 return $context;
5034 }
5035
5036 /**
5037 * Get last data for trigger.
5038 *
5039 * @param array $data data.
5040 * @return array
5041 */
5042 public function search_presto_player_triggers_last_data( $data ) {
5043 $context = [];
5044 $context['response_type'] = 'sample';
5045
5046 $user_data = WordPress::get_sample_user_context();
5047
5048 $video_data = [
5049 'pp_video' => '1',
5050 'pp_video_percentage' => '100',
5051 ];
5052
5053 $video_data['video'] = [
5054 'id' => '1',
5055 'title' => 'OttoKit Is Here 🎉 The Easiest Automation Builder WordPress Websites & Apps',
5056 'type' => 'youtube',
5057 'external_id' => '-cYbNYgylLs',
5058 'attachment_id' => '0',
5059 'post_id' => '127',
5060 'src' => 'https://www.youtube.com/watch?v=-cYbNYgylLs',
5061 'created_by' => '1',
5062 'created_at' => '2022-11-10 00:28:25',
5063 'updated_at' => '2022-11-10 00:34:40',
5064 'deleted_at' => '',
5065 ];
5066
5067 $video_data['media']['tag'] = [
5068 'Demo',
5069 'Demo1',
5070 ];
5071
5072 $mediahub_data = [
5073 'post_id' => '1',
5074 'activity_id' => '2',
5075 'post_author' => '1',
5076 'post_content' => 'New sample post...!',
5077 'post_title' => 'Sample Post',
5078 'post_excerpt' => 'sample',
5079 'post_status' => 'publish',
5080 'post_type' => 'pp_video_block',
5081 ];
5082
5083 $videos = ( new Video() )->all();
5084
5085 if ( count( $videos ) > 0 ) {
5086 if ( ! empty( $data['filter'] ) ) {
5087 $video_id = $data['filter']['pp_video']['value'];
5088 if ( -1 === $video_id ) {
5089 global $wpdb;
5090 $args = [
5091 'numberposts' => 1,
5092 'orderby' => 'rand',
5093 'order' => 'ASC',
5094 'post_type' => 'pp_video_block',
5095 ];
5096 $posts = get_posts( $args );
5097 $presto_post_video_id = $posts[0]->ID;
5098 $presto_video_id = $wpdb->get_var( $wpdb->prepare( 'SELECT * FROM wp_presto_player_videos WHERE post_id = %d', $presto_post_video_id ) );
5099 $video_id = $presto_video_id;
5100 }
5101 $pp_videos = ( new Video( $video_id ) )->toArray();
5102 if ( ! empty( $pp_videos ) ) {
5103 $video_data = [];
5104 $video_data['video'] = $pp_videos;
5105 if ( is_array( $video_data ) && array_key_exists( 'post_id', $video_data['video'] ) ) {
5106 $mediahub_data = WordPress::get_post_context( (int) $video_data['video']['post_id'] );
5107 $media_tags = get_the_terms( (int) $video_data['video']['post_id'], 'pp_video_tag' );
5108 if ( ! empty( $media_tags ) && is_array( $media_tags ) && isset( $media_tags[0] ) ) {
5109 $tag_name = [];
5110 foreach ( $media_tags as $tag ) {
5111 $tag_name[] = $tag->name;
5112 }
5113 $video_data['media']['tag'] = $tag_name;
5114 }
5115 }
5116 $video_data['pp_video'] = $video_id;
5117 $video_data['pp_video_percentage'] = isset( $data['filter']['pp_video_percentage']['value'] ) ? $data['filter']['pp_video_percentage']['value'] : '100';
5118 $user_data = WordPress::get_user_context( (int) $video_data['video']['created_by'] );
5119
5120 $context['response_type'] = 'live';
5121 }
5122 }
5123 }
5124
5125 $context['pluggable_data'] = array_merge( $user_data, $video_data, $mediahub_data );
5126
5127 return $context;
5128 }
5129
5130 /**
5131 * Get last data for trigger.
5132 *
5133 * @param array $data data.
5134 * @return array
5135 */
5136 public function search_member_press_triggers_last_data( $data ) {
5137 global $wpdb;
5138
5139 $term = $data['search_term'] ? $data['search_term'] : '';
5140
5141 $context = [];
5142 $context['response_type'] = 'sample';
5143
5144 $user_data = WordPress::get_sample_user_context();
5145
5146 $membership_data = [
5147 'membership_id' => '190',
5148 'membership_title' => 'Sample Membership',
5149 'amount' => '12.00',
5150 'total' => '12.00',
5151 'tax_amount' => '0.00',
5152 'tax_rate' => '0.00',
5153 'trans_num' => 't_63a03f3334f44',
5154 'status' => 'complete',
5155 'subscription_id' => '0',
5156 'membership_url' => site_url() . '/register/premium/',
5157 'membership_featured_image_id' => '521',
5158 'membership_featured_image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
5159 ];
5160
5161 $membership_id = (int) ( isset( $data['filter']['membership_id']['value'] ) ? $data['filter']['membership_id']['value'] : '-1' );
5162
5163 if ( in_array( $term, [ 'mepr-event-transaction-expired', 'mepr_subscription_transition_status', 'mepr-event-transaction-paused' ] ) ) {
5164 $status = 'cancelled';
5165 if ( 'mepr-event-transaction-paused' === $term ) {
5166 $status = 'suspended';
5167 }
5168 if ( $membership_id > 0 ) {
5169 $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 ) );
5170 } else {
5171 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_subscriptions WHERE status=%s ORDER BY id DESC LIMIT 1", $status ) );
5172
5173 }
5174 if ( ! empty( $subscription ) ) {
5175
5176 $membership_data = MemberPress::get_subscription_context( $subscription );
5177 $user_data = WordPress::get_user_context( $subscription->user_id );
5178
5179
5180 $context['response_type'] = 'live';
5181 }
5182 } elseif ( 'mepr-coupon-code-redeemed' === $term ) {
5183
5184 $coupon_id = (int) ( isset( $data['filter']['coupon_id']['value'] ) ? $data['filter']['coupon_id']['value'] : '-1' );
5185
5186 if ( $coupon_id > 0 ) {
5187 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE coupon_id= %s ORDER BY id DESC LIMIT 1", $coupon_id ) );
5188 } else {
5189 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE coupon_id!= %s ORDER BY id DESC LIMIT 1", '0' ) );
5190 }
5191
5192
5193 if ( ! empty( $subscription ) ) {
5194
5195 $membership_data = MemberPress::get_membership_context( $subscription );
5196 $user_data = WordPress::get_user_context( $subscription->user_id );
5197 $membership_data['coupon_id'] = $subscription->coupon_id;
5198 $membership_data['coupon'] = get_post( $subscription->coupon_id );
5199 $context['response_type'] = 'live';
5200 }
5201 } else {
5202
5203 if ( $membership_id > 0 ) {
5204
5205 $subscription = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mepr_transactions WHERE product_id= %s ORDER BY id DESC LIMIT 1", $membership_id ) );
5206 } else {
5207 $subscription = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}mepr_transactions ORDER BY id DESC LIMIT 1" );
5208 }
5209
5210
5211 if ( ! empty( $subscription ) ) {
5212
5213 $membership_data = MemberPress::get_membership_context( $subscription );
5214 $user_data = WordPress::get_user_context( $subscription->user_id );
5215
5216
5217 $context['response_type'] = 'live';
5218 }
5219 }
5220
5221
5222 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5223 return $context;
5224 }
5225
5226 /**
5227 * Get last data for trigger.
5228 *
5229 * @param array $data data.
5230 * @return array
5231 */
5232 public function search_wishlist_member_triggers_last_data( $data ) {
5233 global $wpdb;
5234
5235 $context = [];
5236 $context['response_type'] = 'sample';
5237
5238 $user_data = WordPress::get_sample_user_context();
5239
5240 $membership_data = [
5241 'membership_level_id' => '1',
5242 'membership_level_name' => 'Sample Membership Level',
5243 ];
5244
5245 $membership_level_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
5246
5247 if ( $membership_level_id > 0 ) {
5248 $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 ) );
5249 } else {
5250 $membership = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}wlm_userlevels ORDER BY id DESC LIMIT 1" );
5251 }
5252 if ( ! empty( $membership ) ) {
5253 $membership_data = WishlistMember::get_membership_detail_context( (int) $membership->level_id, (int) $membership->user_id );
5254 $user_data = WordPress::get_user_context( $membership->user_id );
5255
5256 $context['response_type'] = 'live';
5257 }
5258
5259 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5260
5261 return $context;
5262 }
5263
5264 /**
5265 * Get last data for trigger.
5266 *
5267 * @param array $data data.
5268 * @return array
5269 */
5270 public function search_peepso_triggers_last_data( $data ) {
5271 global $wpdb;
5272
5273 $context = [];
5274 $context['response_type'] = 'sample';
5275
5276 $user_data = WordPress::get_sample_user_context();
5277
5278 $post_data = [
5279 'post_id' => '1',
5280 'activity_id' => '2',
5281 'post_author' => '1',
5282 'post_content' => 'New sample post...!',
5283 'post_title' => 'Sample Post',
5284 'post_excerpt' => 'sample',
5285 'post_status' => 'publish',
5286 'post_type' => 'peepso-post',
5287 ];
5288
5289 $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" );
5290
5291 if ( ! empty( $post ) ) {
5292 $post_data = PeepSo::get_pp_activity_context( (int) $post->act_external_id, (int) $post->act_id );
5293 $user_data = WordPress::get_user_context( $post->act_owner_id );
5294
5295 $context['response_type'] = 'live';
5296 }
5297
5298 $context['pluggable_data'] = array_merge( $user_data, $post_data );
5299
5300 return $context;
5301 }
5302
5303 /**
5304 * Get last data for Peepso User triggers.
5305 *
5306 * @param array $data data.
5307 * @return array
5308 */
5309 public function search_peepso_user_triggers_last_data( $data ) {
5310 global $wpdb;
5311
5312 $context = [];
5313 $context['response_type'] = 'sample';
5314 $term = $data['search_term'];
5315
5316 if ( ! class_exists( 'PeepSoUser' ) ) {
5317 return [];
5318 }
5319
5320 if ( 'user_follows_member' === $term || 'user_gains_follower' === $term ) {
5321 $member_id = $data['filter']['follow_user_id']['value'];
5322 if ( -1 === $member_id ) {
5323 $followers = $wpdb->get_results(
5324 $wpdb->prepare(
5325 "SELECT * FROM
5326 {$wpdb->prefix}peepso_user_followers WHERE uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5327 1
5328 )
5329 );
5330 } else {
5331 $followers = $wpdb->get_results(
5332 $wpdb->prepare(
5333 "SELECT * FROM
5334 {$wpdb->prefix}peepso_user_followers WHERE uf_passive_user_id = %d AND
5335 uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5336 $member_id,
5337 1
5338 )
5339 );
5340 }
5341 } elseif ( 'user_unfollows_member' === $term || 'user_loses_follower' === $term ) {
5342 $member_id = $data['filter']['follow_user_id']['value'];
5343 if ( -1 === $member_id ) {
5344 $followers = $wpdb->get_results(
5345 $wpdb->prepare(
5346 "SELECT * FROM
5347 {$wpdb->prefix}peepso_user_followers WHERE uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5348 0
5349 )
5350 );
5351 } else {
5352 $followers = $wpdb->get_results(
5353 $wpdb->prepare(
5354 "SELECT * FROM
5355 {$wpdb->prefix}peepso_user_followers WHERE uf_passive_user_id = %d AND
5356 uf_follow = %d ORDER BY uf_id DESC LIMIT 1",
5357 $member_id,
5358 0
5359 )
5360 );
5361 }
5362 } elseif ( 'user_updates_avatar' === $term ) {
5363 $followers = $wpdb->get_results(
5364 $wpdb->prepare(
5365 "SELECT * FROM
5366 {$wpdb->prefix}peepso_users WHERE usr_avatar_custom = %d AND usr_role = %s
5367 ORDER BY usr_last_activity DESC LIMIT 1",
5368 1,
5369 'member'
5370 )
5371 );
5372 } elseif ( 'user_updates_specific_profile_field' === $term ) {
5373 $field_id = $data['filter']['user_profile_field_id']['value'];
5374 $followers = $wpdb->get_results(
5375 $wpdb->prepare(
5376 "SELECT * FROM
5377 {$wpdb->prefix}peepso_users WHERE usr_role = %s
5378 ORDER BY usr_last_activity DESC LIMIT 1",
5379 'member'
5380 )
5381 );
5382 }
5383 if ( 'user_updates_avatar' === $term ) {
5384 if ( ! empty( $followers ) ) {
5385 $context_data['user'] = WordPress::get_user_context( $followers[0]->usr_id );
5386 $context['response_type'] = 'live';
5387 } else {
5388 $context_data['user'] = WordPress::get_sample_user_context();
5389 }
5390 } elseif ( 'user_updates_specific_profile_field' === $term ) {
5391 if ( ! empty( $followers ) ) {
5392 $user = PeepSoUser::get_instance( $followers[0]->usr_id );
5393 $user->profile_fields->load_fields();
5394 $user_fields = $user->profile_fields->get_fields();
5395 foreach ( $user_fields as $key => $value ) {
5396 $val = get_user_meta( $followers[0]->usr_id, $value->key, true );
5397 if ( '' != $val ) {
5398 $context_data[ $value->title ] = $val;
5399 }
5400 }
5401 $curruser = get_userdata( $followers[0]->usr_id );
5402 $context_data['user_id'] = $followers[0]->usr_id;
5403 $context_data['user_email'] = $user->get_email();
5404 $context_data['avatar_url'] = $user->get_avatar();
5405 $context_data['profile_url'] = $user->get_profileurl();
5406 $context_data['about_me'] = get_user_meta( $followers[0]->usr_id, 'description', true );
5407 if ( $curruser instanceof \WP_User ) {
5408 $context_data['website'] = $curruser->user_url;
5409 }
5410 $context_data['role'] = $user->get_user_role();
5411 $context['response_type'] = 'live';
5412 } else {
5413 $context_data['user'] = WordPress::get_sample_user_context();
5414 }
5415 } else {
5416 if ( ! empty( $followers ) ) {
5417 $context_data['follower_user'] = WordPress::get_user_context( $followers[0]->uf_active_user_id );
5418 $context_data['following_user'] = WordPress::get_user_context( $followers[0]->uf_passive_user_id );
5419 $context['response_type'] = 'live';
5420 } else {
5421 $context_data['follower_user'] = WordPress::get_sample_user_context();
5422 $context_data['following_user'] = WordPress::get_sample_user_context();
5423 }
5424 }
5425
5426 $context['pluggable_data'] = $context_data;
5427
5428 return $context;
5429 }
5430
5431 /**
5432 * Search Peepso User Profile Fields list.
5433 *
5434 * @param array $data data.
5435 * @return array
5436 */
5437 public function search_peepso_profile_field_list( $data ) {
5438 $options = [];
5439 if ( ! class_exists( 'PeepSoUser' ) ) {
5440 return [];
5441 }
5442 $options = [
5443 [
5444 'label' => 'Allow others to "like" my profile',
5445 'value' => 'peepso_is_profile_likable',
5446 ],
5447 [
5448 'label' => 'Hide my birthday year',
5449 'value' => 'peepso_hide_birthday_year',
5450 ],
5451 [
5452 'label' => 'Who can see my profile',
5453 'value' => 'usr_profile_acc',
5454 ],
5455 [
5456 'label' => 'Who can post on my profile',
5457 'value' => 'peepso_profile_post_acc',
5458 ],
5459 [
5460 'label' => "Don't show my online status",
5461 'value' => 'peepso_hide_online_status',
5462 ],
5463 [
5464 'label' => 'My timezone',
5465 'value' => 'peepso_gmt_offset',
5466 ],
5467 ];
5468 $peepsouser = PeepSoUser::get_instance( 0 );
5469 $peepsouser->profile_fields->load_fields();
5470 $fields = $peepsouser->profile_fields->get_fields();
5471 foreach ( $fields as $field ) {
5472 if ( 1 == $field->published ) {
5473 $options[] = [
5474 'label' => $field->title,
5475 'value' => $field->id,
5476 ];
5477 }
5478 }
5479 return [
5480 'options' => $options,
5481 'hasMore' => false,
5482 ];
5483 }
5484
5485 /**
5486 * Search Peepso Roles list.
5487 *
5488 * @param array $data data.
5489 * @return array
5490 */
5491 public function search_peepso_role_list( $data ) {
5492 $options = [
5493 [
5494 'label' => 'Community Member',
5495 'value' => 'member',
5496 ],
5497 [
5498 'label' => 'Community Moderator',
5499 'value' => 'moderator',
5500 ],
5501 [
5502 'label' => 'Community Administrator',
5503 'value' => 'admin',
5504 ],
5505 [
5506 'label' => 'Banned',
5507 'value' => 'ban',
5508 ],
5509 [
5510 'label' => 'Pending user email verification',
5511 'value' => 'register',
5512 ],
5513 [
5514 'label' => 'Pending admin approval',
5515 'value' => 'verified',
5516 ],
5517 ];
5518 return [
5519 'options' => $options,
5520 'hasMore' => false,
5521 ];
5522 }
5523
5524 /**
5525 * Search Peepso Users list.
5526 *
5527 * @param array $data data.
5528 * @return array
5529 */
5530 public function search_peepso_follow_user_list( $data ) {
5531 $options = [];
5532 global $wpdb;
5533 $users = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}peepso_users", ARRAY_A );
5534 if ( count( $users ) > 0 ) {
5535 foreach ( $users as $user ) {
5536 $user_by_id = get_user_by( 'id', $user['usr_id'] );
5537 if ( $user_by_id instanceof \WP_User ) {
5538 $options[] = [
5539 'label' => sprintf( '%s %s [%s]', $user_by_id->last_name, $user_by_id->first_name, $user_by_id->user_email ),
5540 'value' => $user['usr_id'],
5541 ];
5542 } else {
5543 $options[] = [
5544 'label' => '#' . $user['usr_id'],
5545 'value' => $user['usr_id'],
5546 ];
5547 }
5548 }
5549 }
5550 return [
5551 'options' => $options,
5552 'hasMore' => false,
5553 ];
5554 }
5555
5556 /**
5557 * Get last data for trigger
5558 *
5559 * @param array $data data.
5560 * @return array
5561 */
5562 public function search_restrict_content_pro_triggers_last_data( $data ) {
5563 $context = [];
5564 $context['response_type'] = 'sample';
5565
5566 $user_data = WordPress::get_sample_user_context();
5567
5568 $membership_data = [
5569 'membership_level_id' => '190',
5570 'membership_level' => 'Sample Membership',
5571 'membership_initial_payment' => '0.00',
5572 'membership_recurring_payment' => '0.00',
5573 'membership_expiry_date' => 'January 22, 2023',
5574 ];
5575
5576 $customer_id = (int) ( isset( $data['filter']['membership_customer_id']['value'] ) ? $data['filter']['membership_customer_id']['value'] : '-1' );
5577 $membership_id = (int) ( isset( $data['filter']['membership_level_id']['value'] ) ? $data['filter']['membership_level_id']['value'] : '-1' );
5578
5579 $args = [];
5580 if ( 'membership_purchased' === $data['search_term'] ) {
5581 $args = [
5582 'status' => 'active',
5583 'number' => 1,
5584 'orderby' => 'id',
5585 ];
5586 } elseif ( 'membership_cancelled' === $data['search_term'] ) {
5587 $args = [
5588 'status' => 'cancelled',
5589 'number' => 1,
5590 'orderby' => 'id',
5591 ];
5592 } elseif ( 'membership_expired' === $data['search_term'] ) {
5593 $args = [
5594 'status' => 'expired',
5595 'number' => 1,
5596 'orderby' => 'id',
5597 ];
5598 }
5599
5600 if ( 'membership_expired' === $data['search_term'] && -1 !== $customer_id ) {
5601 $args['customer_id'] = $customer_id;
5602 }
5603
5604 if ( -1 !== $membership_id ) {
5605 $args['object_id'] = $membership_id;
5606 }
5607
5608 $memberships = rcp_get_memberships( $args );
5609 if ( count( $memberships ) > 0 ) {
5610 $membership_data = RestrictContent::get_rcp_membership_detail_context( $memberships[0] );
5611 $user_data = WordPress::get_user_context( $memberships[0]->get_user_id() );
5612
5613 $context['response_type'] = 'live';
5614 }
5615
5616 $context['pluggable_data'] = array_merge( $user_data, $membership_data );
5617
5618 return $context;
5619 }
5620
5621 /**
5622 * Get last data for trigger
5623 *
5624 * @param array $data data.
5625 * @return array
5626 */
5627 public function search_events_calendar_triggers_last_data( $data ) {
5628 $context = [];
5629 $context['response_type'] = 'sample';
5630
5631 $event_data = [
5632 'event_id' => 1,
5633 'event' => [
5634 'ID' => 58,
5635 'post_author' => 1,
5636 'post_date' => '2023-01-19 09:27:58',
5637 'post_date_gmt' => '2023-01-19 09:27:58',
5638 'post_content' => '',
5639 'post_title' => 'New event',
5640 'post_excerpt' => '',
5641 'post_status' => 'publish',
5642 'comment_status' => 'open',
5643 'ping_status' => 'closed',
5644 'post_password' => '',
5645 'post_name' => 'new-event',
5646 'to_ping' => '',
5647 'pinged' => '',
5648 'post_modified' => '2023-01-19 09:44:25',
5649 'post_modified_gmt' => '2023-01-19 09:44:25',
5650 'post_content_filtered' => '',
5651 'post_parent' => 0,
5652 'guid' => 'http://connector.com/?post_type=tribe_events&#038;p=58',
5653 'menu_order' => -1,
5654 'post_type' => 'tribe_events',
5655 'post_mime_type' => '',
5656 'comment_count' => 0,
5657 'filter' => 'raw',
5658 ],
5659 'attendies' => [
5660 'order_id' => 68,
5661 'purchaser_name' => 'John Doe',
5662 'purchaser_email' => 'john@test.com',
5663 'provider' => 'Tribe__Tickets__RSVP',
5664 'provider_slug' => 'rsvp',
5665 'purchase_time' => '2023-01-19 09:48:43',
5666 'optout' => 1,
5667 'ticket' => 'Prime',
5668 'attendee_id' => 68,
5669 'security' => '2cefc3b53e',
5670 'product_id' => 65,
5671 'check_in' => '',
5672 'order_status' => 'yes',
5673 'order_status_label' => 'Going',
5674 'user_id' => 1,
5675 'ticket_sent' => 1,
5676 'event_id' => 58,
5677 'ticket_name' => 'Prime',
5678 'holder_name' => 'John Doe',
5679 'holder_email' => 'john@test.com',
5680 'ticket_id' => 68,
5681 'qr_ticket_id' => 68,
5682 'security_code' => '2cefc3b53e',
5683 'attendee_meta' => '',
5684 'is_subscribed' => '',
5685 'is_purchaser' => 1,
5686 'ticket_exists' => 1,
5687 ],
5688 ];
5689
5690 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
5691 $term = $data['search_term'];
5692
5693 if ( 'event_register' === $term || 'attendee_registered_event' === $term ) {
5694 if ( -1 === $event_id ) {
5695 $args = [
5696 'numberposts' => 1,
5697 'orderby' => 'rand',
5698 'post_type' => 'tribe_events',
5699 ];
5700 $posts = get_posts( $args );
5701 $event_id = $posts[0]->ID;
5702 }
5703 $args = [
5704 'post_type' => 'tribe_rsvp_attendees',
5705 'orderby' => 'ID',
5706 'order' => 'DESC',
5707 'post_status' => 'publish',
5708 'numberposts' => 1,
5709 ];
5710
5711 if ( -1 !== $event_id ) {
5712 $args['meta_query'] = [
5713 [
5714 'key' => '_tribe_rsvp_event',
5715 'value' => $event_id,
5716 ],
5717 ];
5718 }
5719
5720 $attendees = get_posts( $args );
5721
5722 if ( count( $attendees ) > 0 ) {
5723 $attendee = $attendees[0];
5724 $attendee_id = $attendee->ID;
5725
5726 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
5727 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
5728
5729 $event_context = TheEventCalendar::get_event_context( $product_id, $order_id );
5730
5731 if ( ! empty( $event_context ) ) {
5732 $event_data = $event_context;
5733 $context['response_type'] = 'live';
5734 }
5735 } else {
5736 $args = [
5737 'post_type' => 'tec_tc_attendee',
5738 'orderby' => 'ID',
5739 'order' => 'DESC',
5740 'post_status' => 'publish',
5741 'numberposts' => 1,
5742 ];
5743 if ( -1 !== $event_id ) {
5744 $args['meta_query'] = [
5745 [
5746 'key' => '_tec_tickets_commerce_event',
5747 'value' => $event_id,
5748 ],
5749 ];
5750 }
5751 $attendees = get_posts( $args );
5752 $attendee = $attendees[0];
5753 $attendee_id = $attendee->ID;
5754 $product_id = get_post_meta( $attendee_id, '_tec_tickets_commerce_ticket', true );
5755 $order_id = $attendee_id;
5756 $event_context = TheEventCalendar::get_event_context( $product_id, $order_id );
5757 if ( ! empty( $event_context ) ) {
5758 $event_data = $event_context;
5759 $context['response_type'] = 'live';
5760 }
5761 }
5762 } elseif ( 'event_attends' === $term ) {
5763 if ( -1 == $event_id ) {
5764 $args = [
5765 'numberposts' => 1,
5766 'orderby' => 'rand',
5767 'post_type' => 'tribe_events',
5768 ];
5769 $posts = get_posts( $args );
5770 $event_id = $posts[0]->ID;
5771 }
5772 $args = [
5773 'post_type' => 'tribe_rsvp_attendees',
5774 'orderby' => 'ID',
5775 'order' => 'DESC',
5776 'post_status' => 'publish',
5777 'numberposts' => 1,
5778 'meta_query' => [
5779 'relation' => 'AND',
5780 [
5781 'key' => '_tribe_rsvp_checkedin',
5782 'value' => 1,
5783 'compare' => '=',
5784 ],
5785 [
5786 'key' => '_tribe_rsvp_event',
5787 'value' => $event_id,
5788 'compare' => '=',
5789 ],
5790 ],
5791 ];
5792 $attendees = get_posts( $args );
5793 if ( ! function_exists( 'tribe_tickets_get_attendees' ) ) {
5794 return [];
5795 }
5796 if ( ! empty( $attendees ) ) {
5797 $attendee = $attendees[0];
5798 $attendee_id = $attendee->ID;
5799 /**
5800 *
5801 * Ignore line
5802 *
5803 * @phpstan-ignore-next-line
5804 */
5805 $attendee_details = tribe_tickets_get_attendees( $attendee_id, 'rsvp_order' );
5806 foreach ( $attendee_details as $detail ) {
5807 if ( (int) $detail['attendee_id'] !== (int) $attendee_id ) {
5808 continue;
5809 }
5810 $attendee = $detail;
5811 }
5812 $product_id = get_post_meta( $attendee_id, '_tribe_rsvp_product', true );
5813 $order_id = get_post_meta( $attendee_id, '_tribe_rsvp_order', true );
5814 $event_context = TheEventCalendar::get_event_context( $product_id, $order_id );
5815 if ( ! empty( $event_context ) ) {
5816 $event_data = array_merge( $attendee, $event_context );
5817 $context['response_type'] = 'live';
5818 }
5819 } else {
5820 $order = [
5821 'order_id' => 7962,
5822 'purchaser_name' => 'bella4 bella4',
5823 'purchaser_email' => 'bella4@yopmail.com',
5824 'provider' => 'Tribe__Tickets__RSVP',
5825 'provider_slug' => 'rsvp',
5826 'purchase_time' => '2024 - 03 - 04 07:26:41',
5827 'optout' => 1,
5828 'ticket' => 'test test',
5829 'attendee_id' => 7962,
5830 'security' => 'eb3a2d7bc4',
5831 'product_id' => 7959,
5832 'check_in' => 1,
5833 'order_status' => 'yes',
5834 'order_status_label' => 'Going',
5835 'user_id' => 35,
5836 'ticket_sent' => 1,
5837 'event_id' => 7956,
5838 'ticket_name' => 'test test',
5839 'holder_name' => 'bella4 bella4',
5840 'holder_email' => 'bella4@yopmail.com',
5841 'ticket_id' => 7962,
5842 'qr_ticket_id' => 7962,
5843 'security_code' => 'eb3a2d7bc4',
5844 'is_purchaser' => 1,
5845 'ticket_exists' => 1,
5846 ];
5847 $event_data = array_merge( $order, $event_data );
5848 }
5849 } elseif ( 'event_attendee_wc_register' === $term ) {
5850 if ( ! class_exists( 'Tribe__Tickets__Tickets' ) ) {
5851 return [];
5852 }
5853 if ( -1 === $event_id ) {
5854 $args = [
5855 'numberposts' => 1,
5856 'orderby' => 'rand',
5857 'post_type' => 'tribe_events',
5858 ];
5859 $posts = get_posts( $args );
5860 $event_id = $posts[0]->ID;
5861 }
5862 $event_ticket_id = (int) ( isset( $data['filter']['event_ticket_id']['value'] ) ? $data['filter']['event_ticket_id']['value'] : '-1' );
5863 if ( -1 === $event_ticket_id ) {
5864 $tickets = \Tribe__Tickets__Tickets::get_all_event_tickets( $event_id );
5865 $random_key = array_rand( $tickets );
5866 $random_value = $tickets[ $random_key ];
5867 $event_ticket_id = $random_value;
5868 }
5869 $args = [
5870 'post_type' => 'tribe_wooticket',
5871 'orderby' => 'ID',
5872 'order' => 'DESC',
5873 'post_status' => 'publish',
5874 'meta_query' => [
5875 'relation' => 'AND',
5876 [
5877 'key' => '_tribe_wooticket_event',
5878 'value' => $event_id,
5879 'compare' => '=',
5880 ],
5881 [
5882 'key' => '_tribe_wooticket_product',
5883 'value' => $event_ticket_id,
5884 'compare' => '=',
5885 ],
5886 ],
5887 ];
5888
5889 $woo_attendees = get_posts( $args );
5890 if ( ! empty( $woo_attendees ) ) {
5891 $attendee = $woo_attendees[0];
5892 $attendee_id = $attendee->ID;
5893 /**
5894 *
5895 * Ignore line
5896 *
5897 * @phpstan-ignore-next-line
5898 */
5899 $attendee_details = tribe_tickets_get_attendees( $attendee_id, 'rsvp_order' );
5900 foreach ( $attendee_details as $detail ) {
5901 if ( (int) $detail['attendee_id'] !== (int) $attendee_id ) {
5902 continue;
5903 }
5904 $attendee = $detail;
5905 }
5906 $order_id = get_post_meta( $attendee_id, '_tribe_wooticket_order', true );
5907 $product_id = get_post_meta( $attendee_id, '_tribe_wooticket_product', true );
5908 $event_context = TheEventCalendar::get_event_context( $product_id, $order_id );
5909 if ( ! empty( $event_context ) ) {
5910 $event_data = $event_context;
5911 $context['response_type'] = 'live';
5912 }
5913 $context['response_type'] = 'live';
5914 }
5915 }
5916
5917 $context['pluggable_data'] = $event_data;
5918
5919 return $context;
5920 }
5921
5922 /**
5923 * Get last data for trigger
5924 *
5925 * @param array $data data.
5926 * @return array
5927 */
5928 public function search_woo_commerce_triggers_last_data( $data ) {
5929 $context = [];
5930 $context['response_type'] = 'sample';
5931 $context['pluggable_data'] = [];
5932 $user_data = WordPress::get_sample_user_context();
5933
5934 $product_data['product'] = [
5935 'id' => '169',
5936 'name' => 'Sample Product',
5937 'description' => 'This is description of sample product.',
5938 'short_description' => 'This is short description of sample product.',
5939 'image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
5940 'slug' => 'sample-product',
5941 'status' => 'publish',
5942 'type' => 'simple',
5943 'price' => '89',
5944 'featured' => '0',
5945 'sku' => 'hoodie-blue-sm',
5946 'regular_price' => '90',
5947 'sale_price' => '89',
5948 'total_sales' => '21',
5949 'category' => 'Uncategorized',
5950 'tags' => 'sample, new, 2022',
5951 ];
5952
5953 $comment_data = [
5954 'comment_id' => '1',
5955 'comment' => 'This is a sample comment..!',
5956 'comment_author' => 'testsure',
5957 'comment_date' => '2023-06-23 10:10:40',
5958 'comment_author_email' => 'testsure@example.com',
5959 ];
5960
5961 $order_data = [
5962 'order_id' => '500',
5963 'total_order_value' => '45',
5964 'currency' => 'USD',
5965 'shipping_total' => '5',
5966 'order_payment_method' => 'cod',
5967 'billing_firstname' => 'John',
5968 'billing_lastname' => 'Doe',
5969 'billing_company' => 'BSF',
5970 'billing_address_1' => '1004 Beaumont',
5971 'billing_address_2' => '',
5972 'billing_city' => 'Casper',
5973 'billing_state' => 'Wyoming',
5974 'billing_state_name' => 'Wyoming',
5975 'billing_postcode' => '82601',
5976 'billing_country' => 'US',
5977 'billing_country_name' => 'US',
5978 'billing_email' => 'john_doe@gmail.com',
5979 'billing_phone' => '(307) 7626541',
5980 'shipping_firstname' => 'John',
5981 'shipping_lastname' => 'Doe',
5982 'shipping_company' => 'BSF',
5983 'shipping_address_1' => '1004 Beaumont',
5984 'shipping_address_2' => '',
5985 'shipping_city' => 'Casper',
5986 'shipping_state' => 'Wyoming',
5987 'shipping_state_name' => 'Wyoming',
5988 'shipping_postcode' => '82601',
5989 'shipping_country' => 'US',
5990 'shipping_country_name' => 'US',
5991 'coupon_codes' => 'e3mstekq, f24sjakb',
5992 'total_items_in_order' => '1',
5993 'user_id' => '1',
5994 ];
5995
5996 $variation_data = [
5997 'product_variation_id' => '626',
5998 'product_variation' => 'Color: Silver',
5999 ];
6000
6001 $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
6002
6003 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
6004 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
6005
6006 $order_status = ( isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1 );
6007 $from_order_status = ( isset( $data['filter']['from_status']['value'] ) ? $data['filter']['from_status']['value'] : -1 );
6008 $order_note_type = ( isset( $data['filter']['note_type']['value'] ) ? $data['filter']['note_type']['value'] : -1 );
6009
6010 if ( in_array( $term, [ 'product_added_to_cart', 'product_viewed' ], true ) ) {
6011 if ( -1 === $product_id ) {
6012 $args = [
6013 'post_type' => 'product',
6014 'orderby' => 'ID',
6015 'order' => 'DESC',
6016 'post_status' => 'publish',
6017 'numberposts' => 1,
6018 ];
6019 $products = get_posts( $args );
6020
6021 if ( count( $products ) > 0 ) {
6022 $product_id = $products[0]->ID;
6023 }
6024 }
6025
6026 if ( -1 !== $product_id ) {
6027 $post = get_post( $product_id );
6028 $user_data = WordPress::get_user_context( $post->post_author );
6029 $product_data['product_id'] = $product_id;
6030 $product_data['product'] = WooCommerce::get_product_context( $product_id );
6031 $terms = get_the_terms( $product_id, 'product_cat' );
6032 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6033 $cat_name = [];
6034 foreach ( $terms as $cat ) {
6035 $cat_name[] = $cat->name;
6036 }
6037 $product_data['product']['category'] = implode( ', ', $cat_name );
6038 }
6039 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6040 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6041 $tag_name = [];
6042 foreach ( $terms_tags as $tag ) {
6043 $tag_name[] = $tag->name;
6044 }
6045 $product_data['product']['tag'] = implode( ', ', $tag_name );
6046 }
6047 unset( $product_data['product']['id'] ); //phpcs:ignore
6048 $context['response_type'] = 'live';
6049 }
6050
6051 if ( 'product_added_to_cart' === $term ) {
6052 $product_data['product_quantity'] = 1;
6053 }
6054
6055 $context['pluggable_data'] = array_merge( $product_data, $user_data );
6056
6057 } elseif ( 'product_reviewed' === $term ) {
6058 $comment_args = [
6059 'number' => 1,
6060 'type' => 'review',
6061 'orderby' => 'comment_ID',
6062 'post_id' => -1 !== $product_id ? $product_id : 0,
6063 ];
6064
6065 $comments = get_comments( $comment_args );
6066
6067 if ( count( $comments ) > 0 ) {
6068 $comment = $comments[0];
6069 $comment_data = [
6070 'comment_id' => $comment->comment_ID,
6071 'comment' => $comment->comment_content,
6072 'comment_author' => $comment->comment_author,
6073 'comment_date' => $comment->comment_date,
6074 'comment_author_email' => $comment->comment_author_email,
6075 ];
6076 $product_data = WooCommerce::get_product_context( $comment->comment_post_ID );
6077 if ( is_object( $comment ) ) {
6078 $terms = get_the_terms( (int) $comment->comment_post_ID, 'product_cat' );
6079 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6080 $cat_name = [];
6081 foreach ( $terms as $cat ) {
6082 $cat_name[] = $cat->name;
6083 }
6084 $product_data['product']['category'] = implode( ', ', $cat_name );
6085 }
6086 $terms_tags = get_the_terms( (int) $comment->comment_post_ID, 'product_tag' );
6087 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6088 $tag_name = [];
6089 foreach ( $terms_tags as $tag ) {
6090 $tag_name[] = $tag->name;
6091 }
6092 $product_data['product']['tag'] = implode( ', ', $tag_name );
6093 }
6094 }
6095 $user_data = WordPress::get_user_context( $comment->user_id );
6096 $context['response_type'] = 'live';
6097 }
6098
6099 $context['pluggable_data'] = array_merge( $product_data, $user_data, $comment_data );
6100
6101 } elseif ( 'product_purchased' === $term ) {
6102 $order_id = 0;
6103 $product_data['quantity'] = '1';
6104 if ( -1 !== $product_id ) {
6105 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
6106 if ( count( $order_ids ) > 0 ) {
6107 $order_id = $order_ids[0];
6108 }
6109 } else {
6110 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
6111 if ( count( $orders ) > 0 ) {
6112 $order_id = $orders[0]->get_id();
6113 }
6114 }
6115
6116 if ( 0 !== $order_id ) {
6117 $order = wc_get_order( $order_id );
6118
6119 if ( $order ) {
6120 $user_id = $order->get_customer_id();
6121 $items = $order->get_items();
6122
6123 $product_ids = [];
6124
6125 $iteration = 0;
6126 foreach ( $items as $item ) {
6127 if ( method_exists( $item, 'get_product_id' ) ) {
6128 $item_id = $item->get_product_id();
6129 if ( -1 === $product_id && 0 === $iteration ) {
6130 $product_ids[] = $item_id;
6131 break;
6132 } elseif ( $item_id === $product_id ) {
6133 $product_ids[] = $item_id;
6134 break;
6135 }
6136 }
6137
6138 $iteration++;
6139 }
6140 $order_data = WooCommerce::get_order_context( $order_id );
6141 $user_data = WordPress::get_user_context( $user_id );
6142 $order_data['total_items_in_order'] = count( $product_ids );
6143 $product_data = [];
6144 foreach ( $product_ids as $key => $product_id ) {
6145 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
6146 $terms = get_the_terms( $product_id, 'product_cat' );
6147 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6148 $cat_name = [];
6149 foreach ( $terms as $cat ) {
6150 $cat_name[] = $cat->name;
6151 }
6152 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
6153 }
6154 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6155 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6156 $tag_name = [];
6157 foreach ( $terms_tags as $tag ) {
6158 $tag_name[] = $tag->name;
6159 }
6160 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
6161 }
6162 $product = wc_get_product( $product_id );
6163 /**
6164 *
6165 * Ignore line
6166 *
6167 * @phpstan-ignore-next-line
6168 */
6169 if ( $product->is_downloadable() ) {
6170 /**
6171 *
6172 * Ignore line
6173 *
6174 * @phpstan-ignore-next-line
6175 */
6176 foreach ( $product->get_downloads() as $key_download_id => $download ) {
6177 $download_name = $download->get_name();
6178 $download_link = $download->get_file();
6179 $download_id = $download->get_id();
6180 $download_type = $download->get_file_type();
6181 $download_ext = $download->get_file_extension();
6182 $product_data[ 'product' . $key ]['download'] = [
6183 'download_name' => $download_name,
6184 'download_link' => $download_link,
6185 'download_id' => $download_id,
6186 'download_type' => $download_type,
6187 'download_ext' => $download_ext,
6188 ];
6189 }
6190 }
6191 }
6192 $context['response_type'] = 'live';
6193 }
6194 }
6195
6196 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
6197
6198 } elseif ( 'variable_product_purchased' === $term ) {
6199 $product_variation_id = (int) ( isset( $data['filter']['product_variation_id']['value'] ) ? $data['filter']['product_variation_id']['value'] : -1 );
6200 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
6201
6202 foreach ( $order_ids as $order_id ) {
6203 $order = wc_get_order( $order_id );
6204
6205 if ( $order ) {
6206 $user_id = $order->get_customer_id();
6207 $items = $order->get_items();
6208 $product_variations = [];
6209
6210 $iteration = 0;
6211 foreach ( $items as $item ) {
6212 if ( method_exists( $item, 'get_variation_id' ) ) {
6213 $variation_id = $item->get_variation_id();
6214 if ( -1 === $product_variation_id && 0 === $iteration ) {
6215 $product_variations[] = $variation_id;
6216 break;
6217 } elseif ( $variation_id === $product_variation_id ) {
6218 $product_variations[] = $variation_id;
6219 break;
6220 }
6221 }
6222
6223 $iteration++;
6224 }
6225
6226 if ( count( $product_variations ) > 0 ) {
6227 $product_data = WooCommerce::get_product_context( $product_variation_id );
6228 $order_data = WooCommerce::get_order_context( $order_id );
6229 $user_data = WordPress::get_user_context( $user_id );
6230 $variation_data = [
6231 'product_variation_id' => $product_variations[0],
6232 'product_variation' => get_the_excerpt( $product_variations[0] ),
6233 ];
6234
6235 $context['response_type'] = 'live';
6236 break;
6237 }
6238 }
6239 }
6240
6241 $context['pluggable_data'] = array_merge( $order_data, $user_data, $variation_data );
6242
6243 } elseif ( 'variable_subscription_purchased' === $term ) {
6244 $product_data['quantity'] = '1';
6245 $product_data['product_name'] = 'Sample Product';
6246 $product_data['billing_period'] = '2021-2022';
6247
6248 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
6249
6250 $subscription_order_id = 0;
6251 $order_ids = [];
6252
6253 if ( -1 !== $product_id ) {
6254 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
6255
6256 } else {
6257 $orders = wc_get_orders( [] );
6258 if ( count( $orders ) > 0 ) {
6259 $order_ids[] = $orders[0]->get_id();
6260 }
6261 }
6262
6263 foreach ( $order_ids as $order_id ) {
6264 $query_args = [
6265 'post_type' => 'shop_subscription',
6266 'orderby' => 'ID',
6267 'order' => 'DESC',
6268 'post_status' => 'wc-active',
6269 'posts_per_page' => 1,
6270 'post_parent' => $order_id,
6271 ];
6272 $query_result = new WP_Query( $query_args );
6273 $subscription_orders = $query_result->get_posts();
6274
6275 if ( count( $subscription_orders ) > 0 ) {
6276 $subscription_order_id = $subscription_orders[0]->ID;
6277 break;
6278 }
6279 }
6280
6281 if ( 0 !== $subscription_order_id ) {
6282 $subscription = wcs_get_subscription( $subscription_order_id );
6283 if ( $subscription instanceof WC_Subscription ) {
6284 $last_order_id = $subscription->get_last_order();
6285 if ( ! empty( $last_order_id ) && $last_order_id === $subscription->get_parent_id() ) {
6286 $user_id = wc_get_order( $last_order_id )->get_customer_id();
6287 $items = $subscription->get_items();
6288
6289 foreach ( $items as $item ) {
6290 $product = $item->get_product();
6291 if ( class_exists( '\WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product ) ) {
6292 if ( $product->is_type( [ 'subscription', 'subscription_variation', 'variable-subscription' ] ) ) {
6293
6294 $product_data = WooCommerce::get_variable_subscription_product_context( $item, $last_order_id );
6295 $user_data = WordPress::get_user_context( $user_id );
6296
6297 $context['response_type'] = 'live';
6298 $context['pluggable_data'] = array_merge( $product_data, $user_data );
6299 }
6300 }
6301 }
6302 }
6303 }
6304 }
6305 } elseif ( 'order_created' === $term ) {
6306 $orders = wc_get_orders( [ 'numberposts' => 1 ] );
6307 $order_id = '';
6308 if ( count( $orders ) > 0 ) {
6309 $order_id = $orders[0]->get_id();
6310 $order = wc_get_order( $order_id );
6311 $user_id = $order->get_customer_id();
6312 $order_sample_data = array_merge(
6313 WooCommerce::get_order_context( $order_id ),
6314 WordPress::get_user_context( $user_id )
6315 );
6316 $context['response_type'] = 'live';
6317 }
6318
6319 $context['pluggable_data'] = $order_sample_data;
6320
6321 } elseif ( 'order_status_changed' === $term ) {
6322 if ( -1 == $order_status ) {
6323 $args = [
6324 'numberposts' => 1,
6325 'orderby' => 'date',
6326 'order' => 'DESC',
6327 ];
6328 } else {
6329 $args = [
6330 'status' => [ $order_status ],
6331 'numberposts' => 1,
6332 'orderby' => 'date',
6333 'order' => 'DESC',
6334 ];
6335 }
6336 $orders = wc_get_orders( $args );
6337 $order_id = '';
6338 if ( count( $orders ) > 0 ) {
6339 $order_id = $orders[0]->get_id();
6340 $order = wc_get_order( $order_id );
6341 $user_id = $order->get_customer_id();
6342 $items = $order->get_items();
6343 $product_ids = [];
6344 foreach ( $items as $item ) {
6345 $product_ids[] = $item['product_id'];
6346 }
6347 $product_data = [];
6348 foreach ( $product_ids as $key => $product_id ) {
6349 /**
6350 *
6351 * Ignore line
6352 *
6353 * @phpstan-ignore-next-line
6354 */
6355 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
6356 /**
6357 *
6358 * Ignore line
6359 *
6360 * @phpstan-ignore-next-line
6361 */
6362 $terms = get_the_terms( $product_id, 'product_cat' );
6363 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6364 $cat_name = [];
6365 foreach ( $terms as $cat ) {
6366 $cat_name[] = $cat->name;
6367 }
6368 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
6369 }
6370 /**
6371 *
6372 * Ignore line
6373 *
6374 * @phpstan-ignore-next-line
6375 */
6376 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6377 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6378 $tag_name = [];
6379 foreach ( $terms_tags as $tag ) {
6380 $tag_name[] = $tag->name;
6381 }
6382 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
6383 }
6384 }
6385 $order_sample_data = array_merge(
6386 WooCommerce::get_order_context( $order_id ),
6387 $product_data
6388 );
6389 $order_sample_data['user'] = WordPress::get_user_context( $user_id );
6390 $order_sample_data['to_status'] = $order_status;
6391 $order_sample_data['from_status'] = $from_order_status;
6392 $context['response_type'] = 'live';
6393 }
6394
6395 $order_sample_data['to_status'] = $order_status;
6396 $order_sample_data['from_status'] = $from_order_status;
6397 $context['pluggable_data'] = $order_sample_data;
6398 } elseif ( 'order_note_added' === $term ) {
6399 global $wpdb;
6400 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type ='order_note' ORDER BY comment_ID DESC LIMIT 1" );
6401 $order_id = '';
6402 if ( ! empty( $result ) ) {
6403 $order_id = $result[0]->comment_post_ID;
6404 $order = wc_get_order( $order_id );
6405 if ( ! empty( $order ) ) {
6406 $user_id = $order->get_customer_id();
6407 $order_sample_data = array_merge(
6408 WooCommerce::get_order_context( $order_id ),
6409 WordPress::get_user_context( $user_id )
6410 );
6411 if ( -1 == $order_note_type ) {
6412 $args = [
6413 'order_id' => $order_id,
6414 'limit' => 1,
6415 ];
6416 } else {
6417 $args = [
6418 'order_id' => $order_id,
6419 'type' => $order_note_type,
6420 'limit' => 1,
6421 ];
6422 }
6423
6424 $notes = wc_get_order_notes( $args );
6425 if ( ! empty( $notes ) ) {
6426 $order_sample_data['note'] = [
6427 'id' => $notes[0]->id,
6428 'date' => $notes[0]->date_created,
6429 'author' => $notes[0]->added_by,
6430 'content' => $notes[0]->content,
6431 ];
6432 } else {
6433 $order_sample_data['note'] = [
6434 'id' => '1',
6435 'date' => [
6436 'date' => '2023-06-23 10:10:40',
6437 'timezone_type' => 1,
6438 'timezone' => '+00:00',
6439 ],
6440 'author' => 'admin',
6441 'content' => 'new note',
6442 ];
6443 }
6444 $order_sample_data['note_type'] = $order_note_type;
6445 $context['response_type'] = 'live';
6446 }
6447 } else {
6448 $order_sample_data['note'] = [
6449 'id' => '1',
6450 'date' => [
6451 'date' => '2023-06-23 10:10:40',
6452 'timezone_type' => 1,
6453 'timezone' => '+00:00',
6454 ],
6455 'author' => 'admin',
6456 'content' => 'new note',
6457 ];
6458 $order_sample_data['note_type'] = 'customer';
6459 }
6460 $context['pluggable_data'] = $order_sample_data;
6461 } elseif ( 'order_paid' === $term ) {
6462 $args = [
6463 'status' => [ 'completed' ],
6464 'numberposts' => 1,
6465 ];
6466 $orders = wc_get_orders( $args );
6467 $order_id = '';
6468 if ( count( $orders ) > 0 ) {
6469 $order_id = $orders[0]->get_id();
6470 $order = wc_get_order( $order_id );
6471 $user_id = $order->get_customer_id();
6472 $order_sample_data = array_merge(
6473 WooCommerce::get_order_context( $order_id ),
6474 WordPress::get_user_context( $user_id )
6475 );
6476 $context['response_type'] = 'live';
6477 }
6478 $context['pluggable_data'] = $order_sample_data;
6479 } elseif ( 'product_category_purchased' === $term ) {
6480 $product_category_id = (int) ( isset( $data['filter']['product_category_id']['value'] ) ? $data['filter']['product_category_id']['value'] : -1 );
6481 $args = [
6482 'post_status' => 'publish',
6483 'product_category_id' => [ $product_category_id ],
6484 'return' => 'ids',
6485 ];
6486 $products = wc_get_products( $args );
6487 if ( ! empty( $products ) ) {
6488 $order_id = 0;
6489 $product_data['quantity'] = '1';
6490 $orders = wc_get_orders(
6491 [
6492 'status' => 'any',
6493 ]
6494 );
6495 $filtered_orders = [];
6496 if ( ! empty( $orders ) ) {
6497 foreach ( $orders as $order ) {
6498 $order_items = $order->get_items();
6499 foreach ( $order_items as $item ) {
6500 if ( method_exists( $item, 'get_product_id' ) ) {
6501 $product_id = $item->get_product_id();
6502 }
6503 if ( is_array( $products ) && in_array( $product_id, $products ) ) {
6504 $filtered_orders[] = $order;
6505 break;
6506 }
6507 }
6508 }
6509 }
6510 if ( ! empty( $filtered_orders ) ) {
6511 if ( count( $filtered_orders ) > 0 ) {
6512 $order_id = $filtered_orders[0]->get_id();
6513 }
6514 if ( 0 !== $order_id ) {
6515 $order = wc_get_order( $order_id );
6516 if ( $order instanceof WC_Order ) {
6517 $user_id = $order->get_customer_id();
6518 $items = $order->get_items();
6519 $product_ids = [];
6520 $iteration = 0;
6521 foreach ( $items as $item ) {
6522 if ( method_exists( $item, 'get_product_id' ) ) {
6523 $item_id = $item->get_product_id();
6524 if ( -1 === $product_id && 0 === $iteration ) {
6525 $product_ids[] = $item_id;
6526 break;
6527 } elseif ( $item_id === $product_id ) {
6528 $product_ids[] = $item_id;
6529 break;
6530 }
6531 }
6532 $iteration++;
6533 }
6534 $order_data = WooCommerce::get_order_context( $order_id );
6535 $user_data = WordPress::get_user_context( $user_id );
6536 $order_data['total_items_in_order'] = count( $product_ids );
6537 $product_data = [];
6538 $category_ids = [];
6539 foreach ( $product_ids as $key => $product_id ) {
6540 $product_data[ 'product' . $key ] = WooCommerce::get_product_context( $product_id );
6541 $terms = get_the_terms( $product_id, 'product_cat' );
6542 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6543 $cat_name = [];
6544 foreach ( $terms as $cat ) {
6545 $cat_name[] = $cat->name;
6546 $category_ids[] = $cat->term_id;
6547 }
6548 $product_data[ 'product' . $key ]['category'] = implode( ', ', $cat_name );
6549 }
6550 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6551 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6552 $tag_name = [];
6553 foreach ( $terms_tags as $tag ) {
6554 $tag_name[] = $tag->name;
6555 }
6556 $product_data[ 'product' . $key ]['tag'] = implode( ', ', $tag_name );
6557 }
6558 $product = wc_get_product( $product_id );
6559 /**
6560 *
6561 * Ignore line
6562 *
6563 * @phpstan-ignore-next-line
6564 */
6565 if ( $product->is_downloadable() ) {
6566 /**
6567 *
6568 * Ignore line
6569 *
6570 * @phpstan-ignore-next-line
6571 */
6572 foreach ( $product->get_downloads() as $key_download_id => $download ) {
6573 $download_name = $download->get_name();
6574 $download_link = $download->get_file();
6575 $download_id = $download->get_id();
6576 $download_type = $download->get_file_type();
6577 $download_ext = $download->get_file_extension();
6578 $product_data[ 'product' . $key ]['download'] = [
6579 'download_name' => $download_name,
6580 'download_link' => $download_link,
6581 'download_id' => $download_id,
6582 'download_type' => $download_type,
6583 'download_ext' => $download_ext,
6584 ];
6585 }
6586 }
6587 }
6588 $context['response_type'] = 'live';
6589 }
6590 }
6591 }
6592 }
6593 $context['pluggable_data'] = array_merge( $order_data, $product_data, $user_data );
6594 if ( ! empty( $category_ids ) ) {
6595 foreach ( $category_ids as $category_id ) {
6596 $context['pluggable_data']['product_category_id'] = $category_id;
6597 }
6598 } else {
6599 $context['pluggable_data']['product_category_id'] = 1;
6600 }
6601 }
6602
6603 return $context;
6604 }
6605
6606 /**
6607 * Search LMS data.
6608 *
6609 * @param array $data data.
6610 * @return array
6611 */
6612 public function search_lifter_lms_last_data( $data ) {
6613 global $wpdb;
6614 $post_type = $data['post_type'];
6615 $meta_key = '_is_complete';
6616 $trigger = $data['search_term'];
6617 $context = [];
6618 $post_id = -1;
6619
6620 if ( ! class_exists( 'LLMS_Section' ) ) {
6621 return [];
6622 }
6623
6624 if ( 'lifterlms_purchase_course' === $trigger ) {
6625 $product_type = 'course';
6626 $post_id = $data['filter']['course_id']['value'];
6627 } elseif ( 'lifterlms_purchase_membership' === $trigger ) {
6628 $product_type = 'membership';
6629 $post_id = $data['filter']['membership_id']['value'];
6630 } elseif ( 'lifterlms_cancel_membership' === $trigger ) {
6631 $product_type = 'membership';
6632 $post_id = $data['filter']['membership_id']['value'];
6633 } elseif ( 'lifterlms_lesson_completed' === $trigger ) {
6634 $post_id = $data['filter']['lesson']['value'];
6635 } elseif ( 'lifterlms_course_completed' === $trigger || 'lifterlms_course_enrolled' === $trigger || 'lifterlms_course_user_removed' === $trigger ) {
6636 $post_id = $data['filter']['course']['value'];
6637 } elseif ( 'lifterlms_section_completed' === $trigger ) {
6638 $post_id = $data['filter']['section']['value'];
6639 }
6640
6641 $where = 'postmeta.post_id = "' . $post_id . '" AND';
6642
6643 if ( 'llms_order' === $post_type ) {
6644 if ( -1 === $post_id ) {
6645 $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 ) );
6646 } else {
6647 $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 ) );
6648 }
6649 } elseif ( 'lifterlms_course_enrolled' === $trigger ) {
6650 if ( -1 === $post_id ) {
6651 $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 ) );
6652 } else {
6653 $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 ) );
6654 }
6655 } elseif ( 'lifterlms_course_user_removed' === $trigger ) {
6656 if ( -1 === $post_id ) {
6657 $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 ) );
6658 } else {
6659 $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 ) );
6660 }
6661 } elseif ( 'lifterlms_cancel_membership' === $trigger ) {
6662 if ( -1 === $post_id ) {
6663 $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 ) );
6664 } else {
6665 $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 ) );
6666 }
6667 } else {
6668 if ( -1 === $post_id ) {
6669 $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 ) );
6670 } else {
6671 $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 ) );
6672 }
6673 }
6674
6675 $response = [];
6676 if ( ! empty( $result ) ) {
6677 $result_post_id = $result[0]->post_id;
6678 $result_user_id = $result[0]->user_id;
6679
6680 switch ( $trigger ) {
6681 case 'lifterlms_lesson_completed':
6682 $context = array_merge(
6683 WordPress::get_user_context( $result_user_id ),
6684 LifterLMS::get_lms_lesson_context( $result_post_id )
6685 );
6686
6687 $context['course'] = get_the_title( get_post_meta( $result_post_id, '_llms_parent_course', true ) );
6688 if ( '' !== ( get_post_meta( $result_post_id, '_llms_parent_section', true ) ) ) {
6689 $context['parent_section'] = get_the_title( get_post_meta( $result_post_id, '_llms_parent_section', true ) );
6690 }
6691 break;
6692 case 'lifterlms_course_enrolled':
6693 case 'lifterlms_course_user_removed':
6694 case 'lifterlms_course_completed':
6695 $context = array_merge(
6696 WordPress::get_user_context( $result_user_id ),
6697 LifterLMS::get_lms_course_context( $result_post_id )
6698 );
6699 break;
6700 case 'lifterlms_section_completed':
6701 $data = new \LLMS_Section( $result_post_id );
6702 $lessons = $data->get_lessons();
6703 $context = array_merge(
6704 WordPress::get_user_context( $result_user_id ),
6705 WordPress::get_post_context( $result_post_id )
6706 );
6707 $context['parent_course'] = $data->get( 'parent_course' );
6708 $context['parent_course_title'] = get_the_title( $data->get( 'parent_course' ) );
6709 if ( ! empty( $lessons ) ) {
6710 foreach ( $lessons as $key => $lesson ) {
6711 $context['section_lesson'][ $key ] = $lesson->id;
6712 $context['section_lesson_title'][ $key ] = get_the_title( $lesson->id );
6713 }
6714 }
6715 $context['section_course'] = $data->get( 'parent_course' );
6716 $context['parent_course_title'] = get_the_title( $data->get( 'parent_course' ) );
6717 break;
6718 case 'lifterlms_purchase_course':
6719 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
6720 $context['course_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
6721 $context['course_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
6722 $context['course_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
6723 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
6724 $context ['order'] = WordPress::get_post_context( $result_post_id );
6725 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
6726 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
6727 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
6728 $context = array_merge( $context, WordPress::get_user_context( $user_id ) );
6729 break;
6730 case 'lifterlms_purchase_membership':
6731 $user_id = get_post_meta( $result_post_id, '_llms_user_id', true );
6732 $context['membership_id'] = get_post_meta( $result_post_id, '_llms_product_id', true );
6733 $context['membership_name'] = get_post_meta( $result_post_id, '_llms_product_title', true );
6734 $context['membership_amount'] = get_post_meta( $result_post_id, '_llms_original_total', true );
6735 $context['currency'] = get_post_meta( $result_post_id, '_llms_currency', true );
6736 $context ['order'] = WordPress::get_post_context( $result_post_id );
6737 $context['order_type'] = get_post_meta( $result_post_id, '_llms_order_type', true );
6738 $context['trial_offer'] = get_post_meta( $result_post_id, '_llms_trial_offer', true );
6739 $context['billing_frequency'] = get_post_meta( $result_post_id, '_llms_billing_frequency', true );
6740 $context = array_merge( $context, WordPress::get_user_context( $user_id ) );
6741 break;
6742 case 'lifterlms_cancel_membership':
6743 $context = array_merge( WordPress::get_post_context( $result_post_id ), WordPress::get_user_context( $result[0]->user_id ) );
6744 $context['membership_id'] = $result_post_id;
6745 $context['membership_name'] = get_the_title( $result_post_id );
6746 break;
6747 default:
6748 return;
6749
6750 }
6751 $response['pluggable_data'] = $context;
6752 $response['response_type'] = 'live';
6753
6754 }
6755
6756 return $response;
6757
6758 }
6759
6760 /**
6761 * Search SM data.
6762 *
6763 * @param array $data data.
6764 * @return array
6765 */
6766 public function search_suremember_last_data( $data ) {
6767 global $wpdb;
6768 $post_type = $data['post_type'];
6769 $meta_key = '_is_complete';
6770 $trigger = $data['search_term'];
6771 $post_id = $data['filter']['group_id']['value'];
6772
6773 if ( 'suremember_updated_group' === $trigger ) {
6774 if ( -1 === $post_id ) {
6775 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts as posts WHERE posts.post_type=%s", $post_type ) );
6776 } else {
6777 $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 ) );
6778 }
6779 } else {
6780 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}usermeta as usermeta WHERE usermeta.meta_key = %s", 'suremembers_user_access_group_' . $post_id ) );
6781 }
6782
6783 $response = [];
6784
6785 if ( ! empty( $result ) ) {
6786 $context = [];
6787 switch ( $trigger ) {
6788 case 'suremember_updated_group':
6789 $group_id = $result[0]->ID;
6790 $suremembers_post['rules'] = get_post_meta( $group_id, 'suremembers_plan_include', true );
6791 $suremembers_post['exclude'] = get_post_meta( $group_id, 'suremembers_plan_exclude', true ); //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
6792 $suremembers_post['suremembers_user_roles'] = get_post_meta( $group_id, 'suremembers_user_roles', true );
6793 $suremembers_post['title'] = get_the_title( $group_id );
6794 $suremembers_post['restrict'] = get_post_meta( $group_id, 'suremembers_plan_rules', true )['restrict'];
6795 $context['group'] = array_merge( WordPress::get_post_context( $group_id ), $suremembers_post );
6796 $context['group_id'] = $group_id;
6797 unset( $context['group']['ID'] );
6798 $response['pluggable_data'] = $context;
6799 $response['response_type'] = 'live';
6800 break;
6801 case 'suremember_user_added_in_group':
6802 foreach ( $result as $res ) {
6803 $meta_value = unserialize( $res->meta_value );
6804 if ( 'active' === $meta_value['status'] ) {
6805 $context = WordPress::get_user_context( $res->user_id );
6806 $context['group'] = WordPress::get_post_context( $post_id );
6807 $context['group_id'] = $post_id;
6808 unset( $context['group']['ID'] );
6809 $response['pluggable_data'] = $context;
6810 $response['response_type'] = 'live';
6811 }
6812 }
6813 break;
6814 case 'suremember_user_removed_from_group':
6815 foreach ( $result as $res ) {
6816 $meta_value = unserialize( $res->meta_value );
6817 if ( 'revoked' === $meta_value['status'] ) {
6818 $context = WordPress::get_user_context( $res->user_id );
6819 $context['group'] = WordPress::get_post_context( $post_id );
6820 $context['group_id'] = $post_id;
6821 unset( $context['group']['ID'] );
6822 $response['pluggable_data'] = $context;
6823 $response['response_type'] = 'live';
6824 }
6825 }
6826 break;
6827 default:
6828 return;
6829
6830 }
6831 }
6832
6833 return $response;
6834
6835 }
6836
6837 /**
6838 * Search CartFlows data.
6839 *
6840 * @param array $data data.
6841 * @return array
6842 */
6843 public function search_cartflows_last_data( $data ) {
6844 global $wpdb;
6845 $trigger = $data['search_term'];
6846
6847 if ( ! function_exists( 'wcf_pro' ) ) {
6848 return [];
6849 }
6850
6851 $context = [];
6852 $response = [];
6853 if ( 'cartflows_upsell_offer_accepted' === $trigger || 'cartflows_upsell_offer_rejected' === $trigger ) {
6854 $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'" );
6855 } elseif ( 'cartflows_downsell_offer_accepted' === $trigger || 'cartflows_downsell_offer_rejected' === $trigger ) {
6856 $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'" );
6857 } elseif ( 'wcf_order_bump_item_added' === $trigger || 'wcf_order_bump_item_removed' === $trigger || 'wcf_order_bump_item_replaced' === $trigger ) {
6858 $args = [
6859 'post_type' => 'product',
6860 'orderby' => 'ID',
6861 'order' => 'DESC',
6862 'post_status' => 'publish',
6863 'numberposts' => 1,
6864 ];
6865 $products = get_posts( $args );
6866
6867 if ( count( $products ) > 0 ) {
6868 $product_id = $products[0]->ID;
6869 $product_data['product_id'] = $product_id;
6870 $product_data['product'] = WooCommerce::get_product_context( $product_id );
6871 $terms = get_the_terms( $product_id, 'product_cat' );
6872 if ( ! empty( $terms ) && is_array( $terms ) && isset( $terms[0] ) ) {
6873 $cat_name = [];
6874 foreach ( $terms as $cat ) {
6875 $cat_name[] = $cat->name;
6876 }
6877 $product_data['product']['category'] = implode( ', ', $cat_name );
6878 }
6879 $terms_tags = get_the_terms( $product_id, 'product_tag' );
6880 if ( ! empty( $terms_tags ) && is_array( $terms_tags ) && isset( $terms_tags[0] ) ) {
6881 $tag_name = [];
6882 foreach ( $terms_tags as $tag ) {
6883 $tag_name[] = $tag->name;
6884 }
6885 $product_data['product']['tag'] = implode( ', ', $tag_name );
6886 }
6887 unset( $product_data['product']['id'] ); //phpcs:ignore
6888 $response['response_type'] = 'live';
6889 } else {
6890 $product_data['product'] = [
6891 'name' => 'Sample Product',
6892 'description' => 'This is description of sample product.',
6893 'short_description' => 'This is short description of sample product.',
6894 'image_url' => SURE_TRIGGERS_URL . 'assets/images/sample.svg',
6895 'slug' => 'sample-product',
6896 'status' => 'publish',
6897 'type' => 'simple',
6898 'price' => '89',
6899 'featured' => '0',
6900 'sku' => 'hoodie-blue-sm',
6901 'regular_price' => '90',
6902 'sale_price' => '89',
6903 'total_sales' => '21',
6904 'category' => 'Uncategorized',
6905 'tags' => 'sample, new, 2022',
6906 'category_ids' => [ 40 ],
6907 'date_created' => [
6908 'date' => '2024-09-19 09:28:57.000000',
6909 'timezone_type' => '1',
6910 'timezone' => '+00:00',
6911 ],
6912 'date_modified' => [
6913 'date' => '2024-09-19 09:28:57.000000',
6914 'timezone_type' => '1',
6915 'timezone' => '+00:00',
6916 ],
6917 'product_id' => 9935,
6918 ];
6919 $response['response_type'] = 'sample';
6920 }
6921 $response['pluggable_data'] = $product_data;
6922 }
6923 if ( ! empty( $result ) && ( 'cartflows_upsell_offer_accepted' === $trigger || 'cartflows_upsell_offer_rejected' === $trigger ) ) {
6924 $context = [];
6925 $order_upsell_id = $result[0]->post_id;
6926 $step_id = get_post_meta( $order_upsell_id, '_cartflows_offer_step_id', true );
6927 $order_id = get_post_meta( $order_upsell_id, '_cartflows_offer_parent_id', true );
6928 $order = wc_get_order( $order_id );
6929 $upsell_order = wc_get_order( $order_upsell_id );
6930 $items = $upsell_order->get_items();
6931 if ( ! empty( $items ) && isset( $items[0] ) ) {
6932 $variation_id = $items[0]['product_id'];
6933 $input_qty = $items[0]['quantity'];
6934 } else {
6935 $variation_id = null;
6936 $input_qty = null;
6937 }
6938 $offer_product = wcf_pro()->utils->get_offer_data( $step_id, $variation_id, $input_qty, $order_id );
6939 $user_id = get_post_meta( $order_upsell_id, '_customer_user', true );
6940 $context = WordPress::get_user_context( $user_id );
6941 $context['order'] = $order->get_data();
6942 $context['upsell'] = $offer_product;
6943 $context['funnel_step_id'] = $step_id;
6944 if ( is_scalar( $step_id ) ) {
6945 $context['funnel_id'] = get_post_meta( intval( $step_id ), 'wcf-flow-id', true );
6946 } else {
6947 $context['funnel_id'] = null;
6948 }
6949 $response['pluggable_data'] = $context;
6950 $response['response_type'] = 'live';
6951 } elseif ( ! empty( $result ) && ( 'cartflows_downsell_offer_accepted' === $trigger || 'cartflows_downsell_offer_rejected' === $trigger ) ) {
6952 $context = [];
6953 $order_downsell_id = $result[0]->post_id;
6954 $step_id = get_post_meta( $order_downsell_id, '_cartflows_offer_step_id', true );
6955 $order_id = get_post_meta( $order_downsell_id, '_cartflows_offer_parent_id', true );
6956 $order = wc_get_order( $order_id );
6957 $downsell_order = wc_get_order( $order_downsell_id );
6958 $items = $downsell_order->get_items();
6959 if ( ! empty( $items ) && isset( $items[0] ) ) {
6960 $variation_id = $items[0]['product_id'];
6961 $input_qty = $items[0]['quantity'];
6962 } else {
6963 $variation_id = null;
6964 $input_qty = null;
6965 }
6966 $offer_product = wcf_pro()->utils->get_offer_data( $step_id, $variation_id, $input_qty, $order_id );
6967 $user_id = get_post_meta( $order_downsell_id, '_customer_user', true );
6968 $context = WordPress::get_user_context( $user_id );
6969 $context['order'] = $order->get_data();
6970 $context['downsell'] = $offer_product;
6971 $context['funnel_step_id'] = $step_id;
6972 if ( is_scalar( $step_id ) ) {
6973 $context['funnel_id'] = get_post_meta( intval( $step_id ), 'wcf-flow-id', true );
6974 } else {
6975 $context['funnel_id'] = null;
6976 }
6977 $response['pluggable_data'] = $context;
6978 $response['response_type'] = 'live';
6979 }
6980 return $response;
6981 }
6982
6983 /**
6984 * Prepare CartFlows Steps list.
6985 *
6986 * @param array $data data.
6987 *
6988 * @return array
6989 */
6990 public function search_cartflows_funnel_step_list( $data ) {
6991 $page = $data['page'];
6992 $limit = Utilities::get_search_page_limit();
6993 $offset = $limit * ( $page - 1 );
6994
6995 $args = [
6996 'posts_per_page' => $limit,
6997 'offset' => $offset,
6998 'orderby' => 'title',
6999 'order' => 'ASC',
7000 'post_type' => 'cartflows_step',
7001 'post_status' => 'publish',
7002 'fields' => 'ids',
7003 ];
7004 $args['meta_query'] = [
7005 [
7006 'key' => 'wcf-flow-id',
7007 'value' => $data['dynamic'],
7008 'compare' => '=',
7009 ],
7010 ];
7011
7012 $flow_step_list = get_posts( $args );
7013 $flow_step_list_count = count(
7014 get_posts(
7015 [
7016 'posts_per_page' => -1,
7017 'orderby' => 'title',
7018 'order' => 'ASC',
7019 'post_type' => 'cartflows_step',
7020 'post_status' => 'publish',
7021 'fields' => 'ids',
7022 'meta_query' => [
7023 [
7024 'key' => 'wcf-flow-id',
7025 'value' => $data['dynamic'],
7026 'compare' => '=',
7027 ],
7028 ],
7029 ]
7030 )
7031 );
7032
7033 $options = [];
7034 if ( ! empty( $flow_step_list ) ) {
7035 foreach ( $flow_step_list as $step ) {
7036 $options[] = [
7037 'label' => html_entity_decode( get_the_title( $step ), ENT_QUOTES, 'UTF-8' ),
7038 'value' => $step,
7039 ];
7040 }
7041 }
7042 return [
7043 'options' => $options,
7044 'hasMore' => $flow_step_list_count > $limit && $flow_step_list_count > $offset,
7045 ];
7046 }
7047
7048
7049 /**
7050 * Fetch user context.
7051 *
7052 * @param int $initiator_id initiator id.
7053 * @param int $friend_id friend id.
7054 * @return array
7055 */
7056 public function get_user_context( $initiator_id, $friend_id ) {
7057 $context = WordPress::get_user_context( $initiator_id );
7058
7059 $friend_context = WordPress::get_user_context( $friend_id );
7060
7061 $avatar = get_avatar_url( $initiator_id );
7062
7063 $context['avatar_url'] = ( $avatar ) ? $avatar : '';
7064
7065 $context['friend_id'] = $friend_id;
7066 $context['friend_first_name'] = $friend_context['user_firstname'];
7067 $context['friend_last_name'] = $friend_context['user_lastname'];
7068 $context['friend_email'] = $friend_context['user_email'];
7069
7070 $friend_avatar = get_avatar_url( $friend_id );
7071 $context['friend_avatar_url'] = $friend_avatar;
7072 return $context;
7073 }
7074
7075 /**
7076 * Search BP data.
7077 *
7078 * @param array $data data.
7079 * @return array
7080 */
7081 public function search_pluggables_bp_friendships( $data ) {
7082 global $wpdb, $bp;
7083 $context = [];
7084 $sample['pluggable_data'] = [
7085 'wp_user_id' => 4,
7086 'user_login' => 'katy1ßßßß',
7087 'display_name' => 'Katy Smith',
7088 'user_firstname' => 'Katy',
7089 'user_lastname' => 'Smith',
7090 'user_email' => 'katy1@gmail.com',
7091 'user_role' => [ 'subscriber' ],
7092 'avatar_url' => 'http://pqr.com/avatar',
7093 'friend_id' => 1,
7094 'friend_first_name' => 'John',
7095 'friend_last_name' => 'Wick',
7096 'friend_email' => 'john@gmail.com',
7097 'friend_avatar_url' => 'http://abc.com/avatar',
7098 ];
7099 $sample['response_type'] = 'sample';
7100
7101 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_friends'" );
7102 if ( $table_exists ) {
7103 $friendships = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_friends LIMIT 1" );
7104 if ( ! empty( $friendships ) ) {
7105 $friendship = $friendships[0];
7106 $initiator_id = $friendship->initiator_user_id;
7107 $friend_user_id = $friendship->friend_user_id;
7108 $context['pluggable_data'] = $this->get_user_context( $initiator_id, $friend_user_id );
7109 $context['response_type'] = 'live';
7110 } else {
7111 $context = $sample;
7112 }
7113 } else {
7114 $context = $sample;
7115 }
7116
7117
7118 return $context;
7119 }
7120
7121 /**
7122 * Search Buddyboss profile types data.
7123 *
7124 * @param array $data data.
7125 * @return array
7126 */
7127 public function search_pluggables_bp_profile_types( $data ) {
7128 global $wpdb, $bp;
7129 $context = [];
7130 $sample['pluggable_data'] = [
7131 'wp_user_id' => 4,
7132 'user_login' => 'katy1ßßßß',
7133 'display_name' => 'Katy Smith',
7134 'user_firstname' => 'Katy',
7135 'user_lastname' => 'Smith',
7136 'user_email' => 'katy1@gmail.com',
7137 'user_role' => [ 'subscriber' ],
7138 'bb_profile_type' => '10',
7139 'bb_profile_type_name' => 'student',
7140 ];
7141 $sample['response_type'] = 'sample';
7142
7143 $post_id = $data['filter']['bb_profile_type']['value'];
7144 $get_existing = get_post_meta( $post_id, '_bp_member_type_key', true );
7145
7146 $type_term = get_term_by(
7147 'name',
7148 /**
7149 *
7150 * Ignore line
7151 *
7152 * @phpstan-ignore-next-line
7153 */
7154 $get_existing,
7155 'bp_member_type'
7156 );
7157
7158 $results = $wpdb->get_results(
7159 $wpdb->prepare(
7160 "SELECT u.ID FROM {$wpdb->prefix}users u INNER JOIN {$wpdb->prefix}term_relationships r
7161 ON u.ID = r.object_id WHERE u.user_status = 0 AND
7162 r.term_taxonomy_id = %d ORDER BY RAND() LIMIT 1",
7163 /**
7164 *
7165 * Ignore line
7166 *
7167 * @phpstan-ignore-next-line
7168 */
7169 $type_term->term_id
7170 )
7171 );
7172
7173 if ( ! empty( $results ) ) {
7174 $user = $results[0];
7175 $context['pluggable_data'] = WordPress::get_user_context( $user->ID );
7176 $context['pluggable_data']['bb_profile_type'] = $post_id;
7177 $context['pluggable_data']['bb_profile_type_name'] = get_post_meta( $post_id, '_bp_member_type_label_singular_name', true );
7178 $context['response_type'] = 'live';
7179 } else {
7180 $context = $sample;
7181 }
7182
7183 return $context;
7184 }
7185
7186 /**
7187 * Search BP User data.
7188 *
7189 * @param array $data data.
7190 * @return array
7191 */
7192 public function search_pluggables_bb_users( $data ) {
7193 global $wpdb;
7194 $context = [];
7195
7196 if ( ! class_exists( 'BP_Signup' ) ) {
7197 return [];
7198 }
7199
7200 $term = $data['search_term'];
7201
7202 if ( 'account_activated' === $term ) {
7203 $signups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}signups WHERE active = 1 ORDER BY signup_id DESC LIMIT 1" );
7204 } elseif ( 'updates_profile' === $term ) {
7205 $custom_ids = $wpdb->get_var( "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data ORDER BY last_updated DESC LIMIT 1" );
7206 $args = [ 'include' => $custom_ids ];
7207 $users = get_users( $args );
7208 } elseif ( 'gains_follower' === $term ) {
7209 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_follow ORDER BY id DESC LIMIT 1" );
7210 }
7211
7212 if ( ! empty( $signups ) ) {
7213 $pluggable_data = $signups[0];
7214 $pluggable_data = get_object_vars( $pluggable_data );
7215 unset( $pluggable_data['activation_key'] );
7216 if ( is_string( $pluggable_data['meta'] ) ) {
7217 $pluggable_data['meta'] = unserialize( $pluggable_data['meta'] );
7218 }
7219 if ( is_array( $pluggable_data['meta'] ) ) {
7220 unset( $pluggable_data['meta']['password'] );
7221 }
7222 $context['pluggable_data'] = $pluggable_data;
7223 $context['response_type'] = 'live';
7224 } elseif ( 'updates_profile' === $term ) {
7225 if ( ! empty( $users ) ) {
7226 $user = $users[0];
7227 $fields = $wpdb->get_results( $wpdb->prepare( "SELECT field_id, value FROM {$wpdb->prefix}bp_xprofile_data WHERE user_id = %d", $user->ID ) );
7228 $user_data = WordPress::get_user_context( $user->ID );
7229 $pluggable_data['user_id'] = $user->ID;
7230 $pluggable_data['user_email'] = $user_data['user_email'];
7231 foreach ( $user_data['user_role'] as $key => $role ) {
7232 $pluggable_data['user_role'][ $key ] = $role;
7233 }
7234 foreach ( $fields as $field ) {
7235 if ( function_exists( 'xprofile_get_field' ) ) {
7236 $fieldj = xprofile_get_field( $field->field_id );
7237 $pluggable_data[ $fieldj->name ] = $field->value;
7238 }
7239 }
7240 $context['pluggable_data'] = $pluggable_data;
7241 $context['response_type'] = 'live';
7242 } else {
7243 $context['pluggable_data'] = [
7244 'user_login' => 'john',
7245 'display_name' => 'john',
7246 'user_firstname' => 'john',
7247 'user_lastname' => 'd',
7248 'user_email' => 'johnd@yopmail.com',
7249 'user_role' => [
7250 'subscriber',
7251 ],
7252 'Name' => 'john',
7253 'Nickname' => 'johnd',
7254 'wp_user_id' => 16,
7255 ];
7256 $context['response_type'] = 'sample';
7257 }
7258 } elseif ( 'gains_follower' === $term ) {
7259 if ( ! empty( $results ) ) {
7260 $pluggable_data['follower'] = WordPress::get_user_context( $results[0]->follower_id );
7261 $pluggable_data['leader'] = WordPress::get_user_context( $results[0]->leader_id );
7262 $context['pluggable_data'] = $pluggable_data;
7263 $context['response_type'] = 'live';
7264 } else {
7265 $context['pluggable_data'] = [
7266 'follower' => [
7267 'wp_user_id' => 126,
7268 'user_login' => 'belli',
7269 'display_name' => 'belli',
7270 'user_firstname' => 'test',
7271 'user_lastname' => 'test',
7272 'user_email' => 'belli@gmail.com',
7273 'user_role' => [
7274 'subscriber',
7275 'wpamelia-customer',
7276 ],
7277 ],
7278 'leader' => [
7279 'wp_user_id' => 34,
7280 'user_login' => 'bella3@gmail.com',
7281 'display_name' => 'bella3@gmail.com',
7282 'user_firstname' => 'bella3',
7283 'user_lastname' => 'bella3',
7284 'user_email' => 'bellaaa3@gmail.com',
7285 'user_role' => [
7286 'wpamelia-customer',
7287 ],
7288 ],
7289 ];
7290 $context['response_type'] = 'sample';
7291 }
7292 } else {
7293 $context['pluggable_data'] = [
7294 'signup_id' => '16',
7295 'domain' => '',
7296 'path' => '',
7297 'title' => '',
7298 'user_login' => 'johnd',
7299 'user_email' => 'johnd@yopmail.com',
7300 'registered' => '2024-01-29 04:52:13',
7301 'activated' => '0000-00-00 00:00:00',
7302 'active' => '0',
7303 'meta' => [
7304 'field_1' => 'john',
7305 'field_3' => 'd',
7306 'field_4' => '09878988766',
7307 'field_2' => '123 Main Street',
7308 'field_5' => 'johnd',
7309 'profile_field_ids' => '1,3,4,2,5',
7310 ],
7311 'id' => 16,
7312 'user_name' => 'johnd',
7313 'date_sent' => '2024-01-29 04:52:13',
7314 'recently_sent' => true,
7315 'count_sent' => 1,
7316 ];
7317 $context['response_type'] = 'sample';
7318 }
7319
7320 return $context;
7321 }
7322
7323 /**
7324 * Search BP data.
7325 *
7326 * @param array $data data.
7327 * @return array
7328 */
7329 public function search_pluggables_bp_groups( $data ) {
7330 global $wpdb, $bp;
7331 $context = [];
7332 $group_data = [];
7333 $args = [
7334 'orderby' => 'user_nicename',
7335 'order' => 'ASC',
7336 'number' => 1,
7337 ];
7338
7339 $term = $data['search_term'];
7340
7341 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' ) ) {
7342 return [];
7343 }
7344
7345 if ( 'user_joins_specific_type_group' == $term ) {
7346 if ( -1 !== $data['filter']['group_type']['value'] ) {
7347 $group_type = $data['filter']['group_type']['value'];
7348 } else {
7349 $registered_types = bp_groups_get_group_types();
7350 $random_key = array_rand( $registered_types );
7351 $random_value = $registered_types[ $random_key ];
7352 $group_type = $random_value;
7353 }
7354 if ( function_exists( 'bp_get_group_ids_by_group_types' ) ) {
7355 $group_ids = bp_get_group_ids_by_group_types( $group_type );
7356 $random_key = array_rand( $group_ids );
7357 $random_value = $group_ids[ $random_key ];
7358 $group_id = $random_value;
7359 if ( function_exists( 'groups_get_group' ) ) {
7360 $group = groups_get_group( $group_id['id'] );
7361 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
7362 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
7363 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
7364 $group_data['group_type'] = $group_type;
7365 if ( function_exists( 'groups_get_group_members' ) ) {
7366 $members = groups_get_group_members(
7367 [
7368 'group_id' => $group_id,
7369 ]
7370 );
7371 $ids = [];
7372 foreach ( $members['members'] as $member ) {
7373 $ids[] = $member->ID;
7374 }
7375 $args = [
7376 'number' => 1,
7377 'include' => $ids,
7378 ];
7379 $users = get_users( $args );
7380 }
7381 }
7382 }
7383 } elseif ( 'requests_access_private_group' === $data['search_term'] ) {
7384 $group_id = $data['filter']['group_id']['value'];
7385 if ( $group_id > 0 ) {
7386 $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 ) );
7387 } else {
7388 $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' ) );
7389 }
7390 $custom_ids = $results[0]->user_id;
7391 $args = [ 'include' => $custom_ids ];
7392 $users = get_users( $args );
7393 $group_id = $results[0]->item_id;
7394 $group = groups_get_group( $group_id );
7395 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
7396 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
7397 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
7398 } elseif ( 'bb_group_created' == $term ) {
7399 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups ORDER BY id DESC LIMIT 1" );
7400 if ( function_exists( 'groups_get_group' ) ) {
7401 $group = groups_get_group( $results[0]->id );
7402 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
7403 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
7404 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
7405 $current_types = (array) bp_groups_get_group_type( $results[0]->id, false );
7406 $group_data['group_type'] = $current_types;
7407 $group_data['group_status'] = $group->status;
7408 $group_data['group_date_created'] = $group->date_created;
7409 $group_data['group_enabled_forum'] = $group->enable_forum;
7410 $group_data['group_cover_url'] = bp_get_group_cover_url( $group );
7411 $group_data['group_avatar_url'] = bp_get_group_avatar_url( $group );
7412 $group_data['group_creator'] = WordPress::get_user_context( $group->creator_id );
7413 if ( function_exists( 'groups_get_group_members' ) ) {
7414 $members = groups_get_group_members(
7415 [
7416 'group_id' => $results[0]->id,
7417 ]
7418 );
7419 foreach ( $members['members'] as $key => $member ) {
7420 $group_data['group_member'][ $key ] = WordPress::get_user_context( $member->ID );
7421 }
7422 }
7423 $args = [
7424 'item_id' => $results[0]->id,
7425 ];
7426 $invitations = groups_get_invites( $args );
7427 if ( ! empty( $invitations ) ) {
7428 foreach ( $invitations as $key => $invite ) {
7429 $group_data['invitation'][ $key ] = $invite;
7430 }
7431 }
7432 }
7433 } else {
7434 $users = get_users( $args );
7435
7436 if ( isset( $data['filter']['group_id']['value'] ) ) {
7437 $group_id = $data['filter']['group_id']['value'];
7438 $args['group_id'] = $group_id;
7439 if ( $group_id > 0 ) {
7440 $group = groups_get_group( $group_id );
7441 $group_data['group_id'] = ( property_exists( $group, 'id' ) ) ? (int) $group->id : '';
7442 $group_data['group_name'] = ( property_exists( $group, 'name' ) ) ? $group->name : '';
7443 $group_data['group_description'] = ( property_exists( $group, 'description' ) ) ? $group->description : '';
7444 } else {
7445 $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}bp_groups'" );
7446 if ( $table_exists ) {
7447 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups LIMIT 1" );
7448 $context['groups'] = $groups;
7449 if ( ! empty( $groups ) ) {
7450 foreach ( $groups as $group ) {
7451 $group_data['group_id'] = $group->id;
7452 $group_data['group_name'] = $group->name;
7453 $group_data['group_description'] = $group->description;
7454 }
7455 }
7456 }
7457 }
7458 }
7459 }
7460
7461 if ( 'bb_group_created' == $term ) {
7462 if ( ! empty( $group_data ) ) {
7463 $pluggable_data = $group_data;
7464 $context['pluggable_data'] = $pluggable_data;
7465 $context['response_type'] = 'live';
7466 } else {
7467 $context['pluggable_data'] = [
7468 'group_id' => 112,
7469 'group_name' => 'New Group',
7470 'group_description' => 'New Group Description',
7471 'group_type' => [ 'business' ],
7472 'group_status' => 'public',
7473 'group_date_created' => '2024-02-16 05:37:22',
7474 'group_enabled_forum' => 1,
7475 'group_cover_url' => 'https:\/\/example.com\/wp-content\/uploads\/buddypress\/groups\/23\/cover-image\/65cef4c3ea9b6-bp-cover-image.jpeg',
7476 'group_avatar_url' => 'https:\/\/example.com\/wp-content\/uploads\/group-avatars\/23\/65cef4d7cee19-bpfull.png',
7477 'group_creator' => [
7478 'wp_user_id' => 183,
7479 'user_login' => 'johnd',
7480 'display_name' => 'john',
7481 'user_firstname' => 'john',
7482 'user_lastname' => 'd',
7483 'user_email' => 'johnd@yopmail.com',
7484 'user_role' => [
7485 'subscriber',
7486 'bbp_participant',
7487 ],
7488 ],
7489 ];
7490 $context['response_type'] = 'sample';
7491 }
7492 } elseif ( ! empty( $users ) ) {
7493 $user = $users[0];
7494 $pluggable_data = $group_data;
7495
7496 $avatar = get_avatar_url( $user->ID );
7497 $pluggable_data['wp_user_id'] = $user->ID;
7498 $pluggable_data['avatar_url'] = ( $avatar ) ? $avatar : '';
7499 $pluggable_data['user_login'] = $user->user_login;
7500 $pluggable_data['display_name'] = $user->display_name;
7501 $pluggable_data['user_firstname'] = $user->user_firstname;
7502 $pluggable_data['user_lastname'] = $user->user_lastname;
7503 $pluggable_data['user_email'] = $user->user_email;
7504 $pluggable_data['user_role'] = $user->roles;
7505 $context['pluggable_data'] = $pluggable_data;
7506 $context['response_type'] = 'live';
7507 } else {
7508 $context['pluggable_data'] = [
7509 'wp_user_id' => 1,
7510 'user_login' => 'admin',
7511 'display_name' => 'Johnd',
7512 'user_firstname' => 'John',
7513 'user_lastname' => 'D',
7514 'user_email' => 'johnd@gmail.com',
7515 'user_role' => [ 'subscriber' ],
7516 'group_id' => 112,
7517 'group_name' => 'New Group',
7518 'group_description' => 'New Group Description',
7519 ];
7520 $context['response_type'] = 'sample';
7521 }
7522
7523 return $context;
7524 }
7525
7526 /**
7527 * Search complete courses.
7528 *
7529 * @param array $data data.
7530 * @return array
7531 */
7532 public function search_pluggables_complete_course( $data ) {
7533 global $wpdb;
7534 $context = [];
7535
7536 if ( isset( $data['filter']['sfwd_course_id']['value'] ) ) {
7537 $course_id = $data['filter']['sfwd_course_id']['value'];
7538 }
7539 if ( -1 === $course_id ) {
7540 $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 ) );
7541 } else {
7542 $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 ) );
7543 }
7544
7545 if ( ! empty( $courses ) ) {
7546 $course = $courses[0];
7547 $course_data['course_name'] = $course->post_title;
7548 $course_data['sfwd_course_id'] = $course->ID;
7549 $course_data['course_url'] = get_permalink( $course->ID );
7550 $course_data['course_featured_image_id'] = get_post_meta( $course->ID, '_thumbnail_id', true );
7551 $course_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course->ID );
7552 $timestamp = get_user_meta( $course->user_id, 'course_completed_' . $course->ID, true );
7553 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
7554 $date_format = get_option( 'date_format' );
7555 if ( is_string( $date_format ) ) {
7556 $course_data['course_completion_date'] = wp_date( $date_format, $timestamp );
7557 }
7558 if ( function_exists( 'learndash_get_course_certificate_link' ) ) {
7559 $course_data['course_certificate'] = learndash_get_course_certificate_link( $course->ID, $course->user_id );
7560 }
7561 $context['response_type'] = 'live';
7562 } else {
7563 $course_data['course_name'] = 'Test Course';
7564 $course_data['sfwd_course_id'] = 112;
7565 $course_data['course_url'] = 'https://abc.com/test-course';
7566 $course_data['course_featured_image_id'] = 113;
7567 $course_data['course_featured_image_url'] = 'https://pqr.com/test-course-img';
7568 $course_data['course_completion_date'] = '2023-10-20';
7569 $course_data['course_certificate'] = 'https://example.com/certificates/good-performance/?course_id=112&cert-nonce=f80d0f9cc1';
7570 $context['response_type'] = 'sample';
7571 }
7572
7573 $users_data = $this->search_pluggables_add_user_role( [] );
7574 $user_data = $users_data['pluggable_data'];
7575
7576 $context['pluggable_data'] = array_merge( $course_data, $user_data );
7577 return $context;
7578 }
7579
7580 /**
7581 * Search lessons.
7582 *
7583 * @param array $data data.
7584 * @return array
7585 */
7586 public function search_pluggables_complete_lesson( $data ) {
7587 global $wpdb;
7588 $context = [];
7589
7590 if ( isset( $data['filter']['sfwd_lesson_id']['value'] ) ) {
7591 $lesson_id = $data['filter']['sfwd_lesson_id']['value'];
7592 $course_id = $data['filter']['sfwd_course_id']['value'];
7593 }
7594 if ( -1 === $course_id ) {
7595 $courses = get_posts(
7596 [
7597 'posts_per_page' => - 1,
7598 'post_type' => 'sfwd-courses',
7599 'post_status' => 'publish',
7600 'fields' => 'ids',
7601 ]
7602 );
7603 $course_key = array_rand( $courses );
7604 $course_id = $courses[ $course_key ];
7605 }
7606 $course = get_post( $course_id );
7607 $pluggable_data = LearnDash::get_course_context( $course );
7608
7609 if ( -1 === $lesson_id ) {
7610 $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 ) );
7611 } else {
7612 $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 ) );
7613 }
7614
7615 if ( ! empty( $lessons ) ) {
7616 $lesson = $lessons[0];
7617
7618 $pluggable_data = WordPress::get_user_context( $lesson->user_id );
7619 $pluggable_data['lesson_name'] = $lesson->post_title;
7620 $pluggable_data['sfwd_lesson_id'] = $lesson->ID;
7621 $pluggable_data['lesson_url'] = get_permalink( $lesson->ID );
7622 $pluggable_data['lesson_featured_image_id'] = get_post_meta( $lesson->ID, '_thumbnail_id', true );
7623 $pluggable_data['lesson_featured_image_url'] = get_the_post_thumbnail_url( $lesson->ID );
7624 $context['response_type'] = 'live';
7625 } else {
7626 $pluggable_data = WordPress::get_sample_user_context();
7627 $pluggable_data['lesson_name'] = 'Test Lesson';
7628 $pluggable_data['sfwd_lesson_id'] = 114;
7629 $pluggable_data['lesson_url'] = 'https://abc.com/test-lesson';
7630 $pluggable_data['lesson_featured_image_id'] = 116;
7631 $pluggable_data['lesson_featured_image_url'] = 'https://pqr.com/test-lesson-img';
7632 $context['response_type'] = 'sample';
7633 }
7634
7635 $context['pluggable_data'] = $pluggable_data;
7636 return $context;
7637 }
7638
7639 /**
7640 * Search topics.
7641 *
7642 * @param array $data data.
7643 * @return array
7644 */
7645 public function search_pluggables_complete_topic( $data ) {
7646 global $wpdb;
7647 $context = [];
7648
7649 if ( isset( $data['filter']['sfwd_topic_id']['value'] ) ) {
7650 $topic_id = $data['filter']['sfwd_topic_id']['value'];
7651 $course_id = $data['filter']['sfwd_course_id']['value'];
7652 }
7653 if ( -1 === $course_id ) {
7654 $courses = get_posts(
7655 [
7656 'posts_per_page' => - 1,
7657 'post_type' => 'sfwd-courses',
7658 'post_status' => 'publish',
7659 'fields' => 'ids',
7660 ]
7661 );
7662 $course_key = array_rand( $courses );
7663 $course_id = $courses[ $course_key ];
7664 }
7665 $course = get_post( $course_id );
7666 $pluggable_data = LearnDash::get_course_context( $course );
7667
7668 if ( -1 === $topic_id ) {
7669 $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 ) );
7670 } else {
7671 $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 ) );
7672 }
7673
7674 if ( ! empty( $topics ) ) {
7675 $topic = $topics[0];
7676 $pluggable_data = WordPress::get_user_context( $topics[0]->user_id );
7677 $pluggable_data['topic_name'] = $topic->post_title;
7678 $pluggable_data['sfwd_topic_id'] = $topic->ID;
7679 $pluggable_data['topic_url'] = get_permalink( $topic->ID );
7680 $pluggable_data['topic_featured_image_id'] = get_post_meta( $topic->ID, '_thumbnail_id', true );
7681 $pluggable_data['topic_featured_image_url'] = get_the_post_thumbnail_url( $topic->ID );
7682 $context['response_type'] = 'live';
7683 } else {
7684 $pluggable_data = WordPress::get_sample_user_context();
7685 $pluggable_data['topic_name'] = 'Test Topic';
7686 $pluggable_data['sfwd_topic_id'] = 117;
7687 $pluggable_data['topic_url'] = 'https://abc.com/test-topic';
7688 $pluggable_data['topic_featured_image_id'] = 118;
7689 $pluggable_data['topic_featured_image_url'] = 'https://pqr.com/test-topic-img';
7690 $context['response_type'] = 'sample';
7691 }
7692
7693 $context['pluggable_data'] = $pluggable_data;
7694 return $context;
7695 }
7696
7697 /**
7698 * Search purchase courses.
7699 *
7700 * @param array $data data.
7701 * @return array
7702 */
7703 public function search_pluggables_purchase_course( $data ) {
7704 $context = [];
7705 $context['response_type'] = 'sample';
7706
7707 $purchase_data = [
7708 'course_product_id' => '1',
7709 'course_product_name' => 'Sample Course',
7710 'currency' => 'USD',
7711 'total_amount' => '100',
7712 'first_name' => 'John',
7713 'last_name' => 'Doe',
7714 'email' => 'john_doe@gmail.com',
7715 'phone' => '+923007626541',
7716 ];
7717
7718 $product_id = (int) ( isset( $data['filter']['course_product_id']['value'] ) ? $data['filter']['course_product_id']['value'] : '-1' );
7719 $order_id = 0;
7720
7721 if ( -1 !== $product_id ) {
7722 $order_ids = ( new Utilities() )->get_orders_ids_by_product_id( $product_id );
7723
7724 if ( count( $order_ids ) > 0 ) {
7725 $order_id = $order_ids[0];
7726 }
7727 } else {
7728 $orders = wc_get_orders( [] );
7729 if ( count( $orders ) > 0 ) {
7730 foreach ( $orders as $order ) {
7731 $items = $order->get_items();
7732
7733 if ( count( $items ) > 1 ) {
7734 continue;
7735 }
7736
7737 foreach ( $items as $item ) {
7738 if ( method_exists( $item, 'get_product_id' ) ) {
7739 $product_id = $item->get_product_id();
7740 if ( ! empty( get_post_meta( $item->get_product_id(), '_related_course', true ) ) ) {
7741 $order_id = $order->get_id();
7742 break;
7743 }
7744 }
7745 }
7746 }
7747 }
7748 }
7749
7750 if ( 0 !== $order_id ) {
7751 $order = wc_get_order( $order_id );
7752
7753 if ( $order ) {
7754
7755 $purchase_data = LearnDash::get_purchase_course_context( $order );
7756
7757 $context['response_type'] = 'live';
7758 }
7759 }
7760
7761 $context['pluggable_data'] = $purchase_data;
7762
7763 return $context;
7764 }
7765
7766 /**
7767 * Search quiz data in LearnDash.
7768 *
7769 * @param array $data data.
7770 * @return array
7771 */
7772 public function search_pluggables_ld_quiz( $data ) {
7773 $context = [];
7774 $context['response_type'] = 'sample';
7775 global $wpdb;
7776 $user_data = WordPress::get_sample_user_context();
7777 $quiz_data = [
7778 'quiz' => 7193,
7779 'score' => 1,
7780 'count' => 1,
7781 'question_show_count' => 1,
7782 'pass' => 1,
7783 'rank' => '-',
7784 'time' => 1703595328,
7785 'pro_quizid' => 1,
7786 'course' => 0,
7787 'lesson' => 0,
7788 'topic' => 0,
7789 'points' => 20,
7790 'total_points' => 20,
7791 'percentage' => 100,
7792 'timespent' => 2.7309999999999999,
7793 'has_graded' => false,
7794 'statistic_ref_id' => 9,
7795 'started' => 1703595325,
7796 'completed' => 1703595328,
7797 'ld_version' => '4.8.0.1',
7798 'quiz_key' => '1703595328_1_7193_0',
7799 ];
7800 $output_questions['question'] = [
7801 'ID' => 7195,
7802 'post_content' => '<p>This is a first question<\/p>',
7803 'type' => 'sfwd-question',
7804 'question_type' => 'single',
7805 'points' => 20,
7806 'answers' => [
7807 'sort_answer' => [
7808 [
7809 'answer' => '',
7810 'points' => 0,
7811 'correct' => false,
7812 'type' => 'answer',
7813 ],
7814 ],
7815 'classic_answer' => [
7816 [
7817 'answer' => 'Ans 1',
7818 'points' => 1,
7819 'correct' => false,
7820 'type' => 'answer',
7821 ],
7822 [
7823 'answer' => 'Ans 2',
7824 'points' => 0,
7825 'correct' => true,
7826 'type' => 'answer',
7827 ],
7828 ],
7829 'matrix_sort_answer' => [
7830 [
7831 'answer' => '',
7832 'points' => 0,
7833 'correct' => false,
7834 'type' => 'answer',
7835 ],
7836 ],
7837 'cloze_answer' => [
7838 [
7839 'answer' => '',
7840 'points' => 0,
7841 'correct' => false,
7842 'type' => 'answer',
7843 ],
7844 ],
7845 'free_answer' => [
7846 [
7847 'answer' => '',
7848 'points' => 0,
7849 'correct' => false,
7850 'type' => 'answer',
7851 ],
7852 ],
7853 'assessment_answer' => [
7854 [
7855 'answer' => '',
7856 'points' => 0,
7857 'correct' => false,
7858 'type' => 'answer',
7859 ],
7860 ],
7861 'essay' => [
7862 [
7863 'answer' => '',
7864 'points' => 0,
7865 'correct' => false,
7866 'type' => 'answer',
7867 ],
7868 ],
7869 ],
7870 ];
7871
7872 $quiz_id = (int) ( isset( $data['filter']['sfwd_quiz_id']['value'] ) ? $data['filter']['sfwd_quiz_id']['value'] : '-1' );
7873 $question_id = $data['filter']['sfwd_question_id']['value'];
7874 $term = $data['search_term'];
7875 $mark = 1;
7876 if ( 'passes_quiz' == $term ) {
7877 $mark = 1;
7878 } elseif ( 'fails_quiz' == $term ) {
7879 $mark = 0;
7880 }
7881 if ( 'passes_quiz' == $term || 'fails_quiz' == $term ) {
7882 if ( -1 == $quiz_id ) {
7883 $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 ) );
7884 } else {
7885 $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 ) );
7886 }
7887 } elseif ( 'quiz_essay_submitted' == $term ) {
7888 $question_id = $data['filter']['sfwd_question_id']['value'];
7889 $args = [
7890 'post_type' => 'sfwd-essays',
7891 'posts_per_page' => 1,
7892 'order' => 'DESC',
7893 'post_status' => [ 'graded', 'not_graded' ],
7894 'meta_query' => [
7895 'relation' => 'AND',
7896 [
7897 'key' => 'quiz_post_id',
7898 'value' => $quiz_id,
7899 'compare' => '=',
7900 ],
7901 [
7902 'key' => 'question_post_id',
7903 'value' => $question_id,
7904 'compare' => '=',
7905 ],
7906 ],
7907 ];
7908 $essay = get_posts( $args );
7909 } elseif ( 'quiz_essay_graded' == $term ) {
7910 $args = [
7911 'post_type' => 'sfwd-essays',
7912 'posts_per_page' => 1,
7913 'order' => 'DESC',
7914 'post_status' => 'graded',
7915 'meta_query' => [
7916 'relation' => 'AND',
7917 [
7918 'key' => 'quiz_post_id',
7919 'value' => $quiz_id,
7920 'compare' => '=',
7921 ],
7922 [
7923 'key' => 'question_post_id',
7924 'value' => $question_id,
7925 'compare' => '=',
7926 ],
7927 ],
7928 ];
7929 $essay = get_posts( $args );
7930 }
7931
7932 if ( 'quiz_essay_submitted' == $term || 'quiz_essay_graded' == $term ) {
7933 if ( ! empty( $essay ) ) {
7934 $context = WordPress::get_user_context( $essay[0]->post_author );
7935 $course_id = get_post_meta( $essay[0]->ID, 'course_id', true );
7936 $lesson_id = get_post_meta( $essay[0]->ID, 'lesson_id', true );
7937 $context['quiz_name'] = get_the_title( $quiz_id );
7938 $context['sfwd_quiz_id'] = $quiz_id;
7939 $context['sfwd_question_id'] = $question_id;
7940 $context['question_name'] = is_int( $question_id ) ? (int) get_the_title( $question_id ) : null;
7941 $context['course_name'] = is_int( $course_id ) ? (int) get_the_title( $course_id ) : null;
7942 $context['course_id'] = $course_id;
7943 $context['lesson_name'] = is_int( $lesson_id ) ? (int) get_the_title( $lesson_id ) : null;
7944 $context['lesson_id'] = $lesson_id;
7945 $context['essay_id'] = $essay[0]->ID;
7946 $context['essay'] = WordPress::get_post_context( $essay[0]->ID );
7947 if ( 'quiz_essay_graded' == $term ) {
7948 $users_quiz_data = get_user_meta( (int) $essay[0]->post_author, '_sfwd-quizzes', true );
7949 if ( is_array( $users_quiz_data ) && is_array( $users_quiz_data[0] ) ) {
7950 $essay_post_id = $essay[0]->ID;
7951 $result = array_filter(
7952 $users_quiz_data[0]['graded'],
7953 function( $item ) use ( $essay_post_id ) {
7954 return $item['post_id'] === $essay_post_id;
7955 }
7956 );
7957 $context['essay_points_earned'] = $result[1]['points_awarded'];
7958 }
7959 }
7960 $context['response_type'] = 'live';
7961 } else {
7962 $context = WordPress::get_sample_user_context();
7963 $context['quiz_name'] = 'Test Quiz';
7964 $context['sfwd_quiz_id'] = 11;
7965 $context['sfwd_question_id'] = 12;
7966 $context['course_name'] = 'Test Course';
7967 $context['course_id'] = 13;
7968 $context['lesson_name'] = 'Test Lesson';
7969 $context['lesson_id'] = 14;
7970 $context['essay'] = [
7971 'ID' => 12,
7972 'post_content' => 'demo',
7973 ];
7974 $context['response_type'] = 'sample';
7975 }
7976
7977 $context['pluggable_data'] = $context;
7978 } else {
7979 if ( ! empty( $result ) ) {
7980 $user_data = WordPress::get_user_context( $result[0]->user_id );
7981 $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 ) );
7982
7983 $data = unserialize( $quiz_result[0]->meta_value );
7984 $found_element = null;
7985 foreach ( (array) $data as $element ) {
7986 if ( is_array( $element ) ) {
7987 if ( isset( $element['statistic_ref_id'] ) && $element['statistic_ref_id'] == $result[0]->statistic_ref_id ) {
7988 $found_element = $element;
7989 break;
7990 }
7991 }
7992 }
7993 $quiz_data = (array) $found_element;
7994 $output_questions = LearnDash::get_quiz_questions_answers( $quiz_id );
7995 if ( is_array( $quiz_data ) ) {
7996 $quiz_data['quiz_name'] = get_the_title( $quiz_id );
7997 $quiz_data['sfwd_quiz_id'] = $quiz_id;
7998 }
7999 $context['response_type'] = 'live';
8000 }
8001 $context['pluggable_data'] = array_merge( $quiz_data, $user_data, $output_questions );
8002 }
8003
8004 return $context;
8005 }
8006
8007 /**
8008 * Fetch BB templates.
8009 *
8010 * @return array
8011 */
8012 public function get_beaver_builder_templates() {
8013 $allowed_types = [ 'subscribe-form', 'contact-form' ];
8014 $templates = [];
8015 $all_templates = get_posts(
8016 [
8017 'post_type' => 'fl-builder-template',
8018 'meta_key' => '_fl_builder_data',
8019 'posts_per_page' => -1,
8020 ]
8021 );
8022 $posts = get_posts(
8023 [
8024 'post_type' => 'any',
8025 'meta_key' => '_fl_builder_data',
8026 'posts_per_page' => -1,
8027 ]
8028 );
8029 $posts = array_merge( $all_templates, $posts );
8030
8031 if ( ! empty( $posts ) ) {
8032 foreach ( $posts as $post ) {
8033 $meta = get_post_meta( $post->ID, '_fl_builder_data', true );
8034 foreach ( (array) $meta as $node_id => $node ) {
8035 if ( isset( $node->type ) && 'module' === $node->type ) {
8036 $settings = $node->settings;
8037 if ( in_array( $settings->type, $allowed_types, true ) ) {
8038 $label = $post->post_title;
8039 if ( '' !== $settings->node_label ) {
8040 $label .= ' - ' . $settings->node_label;
8041 }
8042 $templates[] = [
8043 'label' => $label,
8044 'value' => $node_id,
8045 ];
8046 }
8047 }
8048 }
8049 }
8050 }
8051 return $templates;
8052 }
8053
8054 /**
8055 * Search beaver builder forms.
8056 *
8057 * @param array $data data.
8058 * @return array
8059 */
8060 public function search_beaver_builder_forms( $data ) {
8061 $templates = $this->get_beaver_builder_templates();
8062 return [
8063 'options' => $templates,
8064 'hasMore' => false,
8065 ];
8066 }
8067
8068 /**
8069 * Search FluentCRM fields.
8070 *
8071 * @param array $data data.
8072 * @return array
8073 */
8074 public function search_fluentcrm_custom_fields( $data ) {
8075 $context = [];
8076 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
8077 $context['fields'] = $custom_fields;
8078 return $context;
8079 }
8080
8081 /**
8082 * Search FluentCRM Company fields.
8083 *
8084 * @param array $data data.
8085 * @return array
8086 */
8087 public function search_fluentcrm_company_custom_fields( $data ) {
8088 $context = [];
8089 if ( function_exists( 'fluentcrm_get_custom_company_fields' ) ) {
8090 $custom_fields = fluentcrm_get_custom_company_fields();
8091 $context['fields'] = $custom_fields;
8092 }
8093 return $context;
8094 }
8095
8096 /**
8097 * Search FluentCRM fields and display it in dropdown for trigger.
8098 *
8099 * @param array $data data.
8100 * @return array
8101 */
8102 public function search_fluentcrm_custom_fields_data( $data ) {
8103
8104 if ( ! class_exists( 'FluentCrm\App\Models\CustomContactField' ) ) {
8105 return [];
8106 }
8107
8108 $custom_fields = ( new CustomContactField() )->getGlobalFields()['fields'];
8109 $options = [];
8110
8111 if ( ! empty( $custom_fields ) ) {
8112 foreach ( $custom_fields as $custom_field ) {
8113 $options[] = [
8114 'label' => $custom_field['label'],
8115 'value' => $custom_field['slug'],
8116 ];
8117 }
8118 }
8119
8120 return [
8121 'options' => $options,
8122 'hasMore' => false,
8123 ];
8124 }
8125
8126 /**
8127 * Fetch WP JOB Manager Last Data.
8128 *
8129 * @param array $data data.
8130 * @return array
8131 */
8132 public function search_wp_job_manger_last_data( $data ) {
8133 global $wpdb;
8134 $job_type = $data['filter']['job_type']['value'];
8135 $args = [
8136 'posts_per_page' => 1,
8137 'post_type' => 'job_listing',
8138 'orderby' => 'id',
8139 'order' => 'DESC',
8140 ];
8141
8142 if ( -1 !== $job_type ) {
8143 $args['tax_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
8144 [
8145 'taxonomy' => 'job_listing_type',
8146 'field' => 'term_id',
8147 'terms' => $job_type,
8148 ],
8149 ];
8150 }
8151 $posts = get_posts( $args );
8152 if ( empty( $posts ) ) {
8153 $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 );
8154 return $context;
8155 }
8156
8157 $post = $posts[0];
8158 $post_content = WordPress::get_post_context( $post->ID );
8159 $post_meta = WordPress::get_post_meta( $post->ID );
8160 $job_data = array_merge( $post_content, $post_meta, WordPress::get_user_context( $post->post_author ) );
8161 foreach ( $job_data as $key => $job ) {
8162 $newkey = str_replace( 'post', 'wpjob', $key );
8163 unset( $job_data[ $key ] );
8164 $job_data[ $newkey ] = $job;
8165 }
8166 $context['response_type'] = 'live';
8167 $context['pluggable_data'] = $job_data;
8168 return $context;
8169
8170 }
8171
8172 /**
8173 * Get Amelia Appointment Category.
8174 *
8175 * @param array $data data.
8176 *
8177 * @return array
8178 */
8179 public function search_amelia_category_list( $data ) {
8180
8181 global $wpdb;
8182
8183 $page = $data['page'];
8184 $limit = Utilities::get_search_page_limit();
8185 $offset = $limit * ( $page - 1 );
8186
8187 $categories = $wpdb->get_results(
8188 $wpdb->prepare(
8189 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_categories WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
8190 [ 'visible', $limit, $offset ]
8191 ),
8192 OBJECT
8193 );
8194
8195 $categories_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
8196
8197 $options = [];
8198 if ( ! empty( $categories ) ) {
8199 foreach ( $categories as $category ) {
8200 $options[] = [
8201 'label' => $category->name,
8202 'value' => $category->id,
8203 ];
8204 }
8205 }
8206
8207 return [
8208 'options' => $options,
8209 'hasMore' => $categories_count > $limit && $categories_count > $offset,
8210 ];
8211
8212 }
8213
8214 /**
8215 * Get Amelia Appointment Services.
8216 *
8217 * @param array $data data.
8218 *
8219 * @return array
8220 */
8221 public function search_amelia_service_list( $data ) {
8222
8223 global $wpdb;
8224
8225 $page = $data['page'];
8226 $limit = Utilities::get_search_page_limit();
8227 $offset = $limit * ( $page - 1 );
8228
8229 $services = $wpdb->get_results(
8230 $wpdb->prepare(
8231 "SELECT SQL_CALC_FOUND_ROWS id, name FROM {$wpdb->prefix}amelia_services
8232 WHERE categoryId = %d AND status = %s
8233 ORDER BY name ASC LIMIT %d OFFSET %d",
8234 [ $data['dynamic'], 'visible', $limit, $offset ]
8235 ),
8236 OBJECT
8237 );
8238
8239 $services_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
8240
8241 $options = [];
8242 if ( ! empty( $services ) ) {
8243 foreach ( $services as $category ) {
8244 $options[] = [
8245 'label' => $category->name,
8246 'value' => $category->id,
8247 ];
8248 }
8249 }
8250
8251 return [
8252 'options' => $options,
8253 'hasMore' => $services_count > $limit && $services_count > $offset,
8254 ];
8255
8256 }
8257
8258 /**
8259 * Get Amelia Events.
8260 *
8261 * @param array $data data.
8262 *
8263 * @return array
8264 */
8265 public function search_amelia_events_list( $data ) {
8266
8267 global $wpdb;
8268
8269 $page = $data['page'];
8270 $limit = Utilities::get_search_page_limit();
8271 $offset = $limit * ( $page - 1 );
8272
8273 $events = $wpdb->get_results(
8274 $wpdb->prepare(
8275 "SELECT SQL_CALC_FOUND_ROWS id, name from {$wpdb->prefix}amelia_events WHERE status = %s ORDER BY name ASC LIMIT %d OFFSET %d",
8276 [ 'approved', $limit, $offset ]
8277 ),
8278 OBJECT
8279 );
8280
8281 $list_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
8282
8283 $options = [];
8284 if ( ! empty( $events ) ) {
8285 foreach ( $events as $event ) {
8286 $options[] = [
8287 'label' => $event->name,
8288 'value' => $event->id,
8289 ];
8290 }
8291 }
8292
8293 return [
8294 'options' => $options,
8295 'hasMore' => $list_count > $limit && $list_count > $offset,
8296 ];
8297
8298 }
8299
8300 /**
8301 * Get Amelia Events.
8302 *
8303 * @param array $data data.
8304 *
8305 * @return array
8306 */
8307 public function search_amelia_booking_status_list( $data ) {
8308
8309 $options = [];
8310 $options[] = [
8311 'label' => 'Approved',
8312 'value' => 'approved',
8313 ];
8314 $options[] = [
8315 'label' => 'Pending',
8316 'value' => 'pending',
8317 ];
8318 $options[] = [
8319 'label' => 'Rejected',
8320 'value' => 'rejected',
8321 ];
8322 $options[] = [
8323 'label' => 'Canceled',
8324 'value' => 'canceled',
8325 ];
8326 $options[] = [
8327 'label' => 'No-show',
8328 'value' => 'no-show',
8329 ];
8330 return [
8331 'options' => $options,
8332 'hasMore' => false,
8333 ];
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_booked_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
8350 if ( -1 === $appointment_service ) {
8351 // If service exists as per category selected.
8352 $service_exist = $wpdb->get_row(
8353 $wpdb->prepare(
8354 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
8355 [ $appointment_category ]
8356 ),
8357 ARRAY_A
8358 );
8359
8360 if ( empty( $service_exist ) ) {
8361 $result = [];
8362 } else {
8363 $result = $wpdb->get_row(
8364 '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 )',
8365 ARRAY_A
8366 );
8367 }
8368 } else {
8369 $result = $wpdb->get_row(
8370 $wpdb->prepare(
8371 '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',
8372 [ $appointment_service ]
8373 ),
8374 ARRAY_A
8375 );
8376 }
8377
8378 if ( ! empty( $result ) ) {
8379
8380 $payment_result = $wpdb->get_row(
8381 $wpdb->prepare(
8382 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
8383 [ $result['id'] ]
8384 ),
8385 ARRAY_A
8386 );
8387
8388 $customer_result = $wpdb->get_row(
8389 $wpdb->prepare(
8390 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8391 [ $result['customerId'] ]
8392 ),
8393 ARRAY_A
8394 );
8395
8396 $service_result = $wpdb->get_row(
8397 $wpdb->prepare(
8398 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
8399 [ $result['serviceId'] ]
8400 ),
8401 ARRAY_A
8402 );
8403
8404 $category_result = $wpdb->get_row(
8405 $wpdb->prepare(
8406 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
8407 [ $service_result['categoryId'] ]
8408 ),
8409 ARRAY_A
8410 );
8411
8412 if ( $result['couponId'] ) {
8413 $coupon_result = $wpdb->get_row(
8414 $wpdb->prepare(
8415 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8416 [ $result['couponId'] ]
8417 ),
8418 ARRAY_A
8419 );
8420 } else {
8421 $coupon_result = [];
8422 }
8423
8424 if ( ! empty( $result['customFields'] ) ) {
8425 $custom_fields = json_decode( $result['customFields'], true );
8426
8427 $fields_arr = [];
8428 foreach ( (array) $custom_fields as $fields ) {
8429 if ( is_array( $fields ) ) {
8430 $fields_arr[ $fields['label'] ] = $fields['value'];
8431 }
8432 }
8433 unset( $result['customFields'] );
8434 } else {
8435 $fields_arr = [];
8436 }
8437
8438 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
8439 $context['response_type'] = 'live';
8440 } else {
8441
8442 $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 );
8443 }
8444
8445 return $context;
8446 }
8447
8448 /**
8449 * Get last data for trigger.
8450 *
8451 * @param array $data data.
8452 * @return array
8453 */
8454 public function search_amelia_appointment_booking_status_changed_triggers_last_data( $data ) {
8455 global $wpdb;
8456
8457 $context = [];
8458
8459 $appointment_category = $data['filter']['amelia_category_list']['value'];
8460 $appointment_service = $data['filter']['amelia_service_list']['value'];
8461 $appointment_status = $data['filter']['appointment_status']['value'];
8462
8463 if ( -1 === $appointment_service ) {
8464 // If service exists as per category selected.
8465 $service_exist = $wpdb->get_row(
8466 $wpdb->prepare(
8467 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
8468 [ $appointment_category ]
8469 ),
8470 ARRAY_A
8471 );
8472
8473 if ( empty( $service_exist ) ) {
8474 $result = [];
8475 } else {
8476 $result = $wpdb->get_row(
8477 '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 )',
8478 ARRAY_A
8479 );
8480 }
8481 } else {
8482 if ( -1 === $appointment_status ) {
8483 $result = $wpdb->get_row(
8484 $wpdb->prepare(
8485 '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',
8486 [ $appointment_service ]
8487 ),
8488 ARRAY_A
8489 );
8490 } else {
8491 $result = $wpdb->get_row(
8492 $wpdb->prepare(
8493 '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',
8494 [ $appointment_status, $appointment_service ]
8495 ),
8496 ARRAY_A
8497 );
8498 }
8499 }
8500
8501 if ( ! empty( $result ) ) {
8502
8503 $payment_result = $wpdb->get_row(
8504 $wpdb->prepare(
8505 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
8506 [ $result['id'] ]
8507 ),
8508 ARRAY_A
8509 );
8510
8511 $customer_result = $wpdb->get_row(
8512 $wpdb->prepare(
8513 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8514 [ $result['customerId'] ]
8515 ),
8516 ARRAY_A
8517 );
8518
8519 $service_result = $wpdb->get_row(
8520 $wpdb->prepare(
8521 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
8522 [ $result['serviceId'] ]
8523 ),
8524 ARRAY_A
8525 );
8526
8527 $category_result = $wpdb->get_row(
8528 $wpdb->prepare(
8529 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
8530 [ $service_result['categoryId'] ]
8531 ),
8532 ARRAY_A
8533 );
8534
8535 if ( $result['couponId'] ) {
8536 $coupon_result = $wpdb->get_row(
8537 $wpdb->prepare(
8538 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8539 [ $result['couponId'] ]
8540 ),
8541 ARRAY_A
8542 );
8543 } else {
8544 $coupon_result = [];
8545 }
8546
8547 if ( ! empty( $result['customFields'] ) ) {
8548 $custom_fields = json_decode( $result['customFields'], true );
8549
8550 $fields_arr = [];
8551 foreach ( (array) $custom_fields as $fields ) {
8552 if ( is_array( $fields ) ) {
8553 $fields_arr[ $fields['label'] ] = $fields['value'];
8554 }
8555 }
8556 unset( $result['customFields'] );
8557 } else {
8558 $fields_arr = [];
8559 }
8560
8561 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
8562 $context['pluggable_data']['amelia_category_list'] = $appointment_category;
8563 if ( -1 === $appointment_status ) {
8564 $context['pluggable_data']['appointment_status'] = 'approved';
8565 } else {
8566 $context['pluggable_data']['appointment_status'] = $appointment_status;
8567 }
8568 if ( -1 === $appointment_service ) {
8569 $context['pluggable_data']['amelia_service_list'] = $service_result['id'];
8570 } else {
8571 $context['pluggable_data']['amelia_service_list'] = $appointment_service;
8572 }
8573 $context['response_type'] = 'live';
8574 } else {
8575
8576 $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 );
8577 }
8578
8579 return $context;
8580 }
8581
8582 /**
8583 * Get last data for trigger.
8584 *
8585 * @param array $data data.
8586 * @return array
8587 */
8588 public function search_amelia_event_status_changed_triggers_last_data( $data ) {
8589 global $wpdb;
8590
8591 $context = [];
8592
8593 $event_selected = $data['filter']['amelia_events_list']['value'];
8594 $event_status = $data['filter']['event_booking_status']['value'];
8595
8596 $query = 'SELECT *
8597 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8598 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8599 ON customer.id = event_period.customerBookingId';
8600
8601 if ( -1 === $event_selected ) {
8602 if ( -1 !== $event_status ) {
8603 $query .= $wpdb->prepare(
8604 ' WHERE event_period.customerBookingId = (
8605 SELECT MAX(customerBookingId)
8606 FROM ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods
8607 ) AND customer.status = %s',
8608 $event_status
8609 );
8610 } else {
8611 $query .= ' WHERE event_period.customerBookingId = (
8612 SELECT MAX(customerBookingId)
8613 FROM ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods
8614 )';
8615 }
8616 } else {
8617 $event_periods_result = $wpdb->get_results(
8618 $wpdb->prepare(
8619 'SELECT id
8620 FROM ' . $wpdb->prefix . 'amelia_events_periods
8621 WHERE eventId = %d',
8622 [ $event_selected ]
8623 ),
8624 ARRAY_A
8625 );
8626 if ( ! empty( $event_periods_result ) ) {
8627 $ids = wp_list_pluck( $event_periods_result, 'id' );
8628 }
8629
8630 if ( ! empty( $ids ) ) {
8631 if ( -1 !== $event_status ) {
8632 $query .= $wpdb->prepare(
8633 ' WHERE event_period.eventPeriodId IN (%s) AND customer.status = %s',
8634 implode( ',', $ids ),
8635 $event_status
8636 );
8637 } else {
8638 $query .= $wpdb->prepare(
8639 ' WHERE event_period.eventPeriodId IN (%s)',
8640 implode( ',', $ids )
8641 );
8642 }
8643 } else {
8644 if ( -1 !== $event_status ) {
8645 $query .= $wpdb->prepare(
8646 ' WHERE event_period.eventPeriodId = %d AND customer.status = %s',
8647 $event_selected,
8648 $event_status
8649 );
8650 } else {
8651 $query .= $wpdb->prepare(
8652 ' WHERE event_period.eventPeriodId = %d',
8653 $event_selected
8654 );
8655 }
8656 }
8657 }
8658
8659 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
8660
8661 if ( ! empty( $result ) ) {
8662 $event = $wpdb->get_row(
8663 $wpdb->prepare(
8664 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
8665 [ $event_selected ]
8666 ),
8667 ARRAY_A
8668 );
8669 $event = isset( $event ) ? $event : [];
8670 $event_tags = $wpdb->get_results(
8671 $wpdb->prepare(
8672 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
8673 [ $event_selected ]
8674 ),
8675 ARRAY_A
8676 );
8677 $tags = [];
8678 if ( ! empty( $event_tags ) ) {
8679 foreach ( $event_tags as $key => $tag ) {
8680 $tags['event_tag'][ $key ] = $tag['name'];
8681 }
8682 } else {
8683 $tags = [];
8684 }
8685 $customer_result = $wpdb->get_row(
8686 $wpdb->prepare(
8687 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8688 [ $result['customerId'] ]
8689 ),
8690 ARRAY_A
8691 );
8692 $customer_result = isset( $customer_result ) ? $customer_result : [];
8693 if ( $result['couponId'] ) {
8694 $coupon_result = $wpdb->get_row(
8695 $wpdb->prepare(
8696 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8697 [ $result['couponId'] ]
8698 ),
8699 ARRAY_A
8700 );
8701 } else {
8702 $coupon_result = [];
8703 }
8704 if ( ! empty( $result['customFields'] ) ) {
8705 $custom_fields = json_decode( $result['customFields'], true );
8706
8707 $fields_arr = [];
8708 foreach ( (array) $custom_fields as $fields ) {
8709 if ( is_array( $fields ) ) {
8710 $fields_arr[ $fields['label'] ] = $fields['value'];
8711 }
8712 }
8713 unset( $result['customFields'] );
8714 } else {
8715 $fields_arr = [];
8716 }
8717 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
8718 if ( -1 === $event_status ) {
8719 $context['pluggable_data']['event_booking_status'] = 'approved';
8720 } else {
8721 $context['pluggable_data']['event_booking_status'] = $event_status;
8722 }
8723 $context['response_type'] = 'live';
8724 } else {
8725
8726 $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 );
8727 }
8728
8729 return $context;
8730 }
8731
8732 /**
8733 * Get last data for trigger.
8734 *
8735 * @param array $data data.
8736 * @return array
8737 */
8738 public function search_amelia_new_event_attendee_triggers_last_data( $data ) {
8739 global $wpdb;
8740
8741 $context = [];
8742
8743 $event_selected = $data['filter']['amelia_events_list']['value'];
8744
8745 $query = 'SELECT *
8746 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8747 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8748 ON customer.id = event_period.customerBookingId';
8749
8750 if ( -1 === $event_selected ) {
8751 $query .= '
8752 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods )';
8753 } else {
8754 $event_periods_result = $wpdb->get_results(
8755 $wpdb->prepare(
8756 'SELECT id
8757 FROM ' . $wpdb->prefix . 'amelia_events_periods
8758 WHERE eventId = %d',
8759 [ $event_selected ]
8760 ),
8761 ARRAY_A
8762 );
8763 if ( ! empty( $event_periods_result ) ) {
8764 $ids = wp_list_pluck( $event_periods_result, 'id' );
8765 }
8766 if ( ! empty( $ids ) ) {
8767 $query .= $wpdb->prepare(
8768 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND event_period.eventPeriodId IN (%s)',
8769 implode( ',', $ids )
8770 );
8771 } else {
8772 $query .= $wpdb->prepare(
8773 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND event_period.eventPeriodId = %d',
8774 [ $event_selected ]
8775 );
8776 }
8777 }
8778 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
8779
8780 if ( ! empty( $result ) ) {
8781
8782 $event = $wpdb->get_row(
8783 $wpdb->prepare(
8784 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
8785 [ $event_selected ]
8786 ),
8787 ARRAY_A
8788 );
8789 $event = isset( $event ) ? $event : [];
8790 $event_tags = $wpdb->get_results(
8791 $wpdb->prepare(
8792 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
8793 [ $event_selected ]
8794 ),
8795 ARRAY_A
8796 );
8797 $tags = [];
8798 if ( ! empty( $event_tags ) ) {
8799 foreach ( $event_tags as $key => $tag ) {
8800 $tags['event_tag'][ $key ] = $tag['name'];
8801 }
8802 } else {
8803 $tags = [];
8804 }
8805
8806 $customer_result = $wpdb->get_row(
8807 $wpdb->prepare(
8808 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8809 [ $result['customerId'] ]
8810 ),
8811 ARRAY_A
8812 );
8813 $customer_result = isset( $customer_result ) ? $customer_result : [];
8814
8815 if ( $result['couponId'] ) {
8816 $coupon_result = $wpdb->get_row(
8817 $wpdb->prepare(
8818 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8819 [ $result['couponId'] ]
8820 ),
8821 ARRAY_A
8822 );
8823 } else {
8824 $coupon_result = [];
8825 }
8826
8827 if ( ! empty( $result['customFields'] ) ) {
8828 $custom_fields = json_decode( $result['customFields'], true );
8829
8830 $fields_arr = [];
8831 foreach ( (array) $custom_fields as $fields ) {
8832 if ( is_array( $fields ) ) {
8833 $fields_arr[ $fields['label'] ] = $fields['value'];
8834 }
8835 }
8836 unset( $result['customFields'] );
8837 } else {
8838 $fields_arr = [];
8839 }
8840
8841 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
8842 $context['response_type'] = 'live';
8843 } else {
8844
8845 $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 );
8846 }
8847
8848 return $context;
8849 }
8850
8851 /**
8852 * Get last data for trigger.
8853 *
8854 * @param array $data data.
8855 * @return array
8856 */
8857 public function search_amelia_event_booking_cancelled_triggers_last_data( $data ) {
8858 global $wpdb;
8859
8860 $context = [];
8861
8862 $event_selected = $data['filter']['amelia_events_list']['value'];
8863
8864 if ( -1 === $event_selected ) {
8865 $result = $wpdb->get_row(
8866 'SELECT *
8867 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8868 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8869 ON customer.id = event_period.customerBookingId
8870 WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND customer.status = "canceled"',
8871 ARRAY_A
8872 );
8873 } else {
8874 $event_periods_result = $wpdb->get_results(
8875 $wpdb->prepare(
8876 'SELECT id
8877 FROM ' . $wpdb->prefix . 'amelia_events_periods
8878 WHERE eventId = %d',
8879 [ $event_selected ]
8880 ),
8881 ARRAY_A
8882 );
8883 if ( ! empty( $event_periods_result ) ) {
8884 $ids = wp_list_pluck( $event_periods_result, 'id' );
8885 }
8886 $query = 'SELECT *
8887 FROM ' . $wpdb->prefix . 'amelia_customer_bookings as customer
8888 INNER JOIN ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods as event_period
8889 ON customer.id = event_period.customerBookingId';
8890 if ( ! empty( $ids ) ) {
8891 $query .= $wpdb->prepare(
8892 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId IN (%s) AND customer.status = "canceled"',
8893 implode( ',', $ids )
8894 );
8895 } else {
8896 $query .= $wpdb->prepare(
8897 ' WHERE event_period.customerBookingId = ( Select max(customerBookingId) From ' . $wpdb->prefix . 'amelia_customer_bookings_to_events_periods ) AND eventPeriodId = %d
8898 AND customer.status = "canceled"',
8899 [ $event_selected ]
8900 );
8901 }
8902 $result = $wpdb->get_row( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
8903 }
8904
8905 if ( ! empty( $result ) ) {
8906 $event = $wpdb->get_row(
8907 $wpdb->prepare(
8908 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events WHERE id = %d',
8909 [ $event_selected ]
8910 ),
8911 ARRAY_A
8912 );
8913 $event = isset( $event ) ? $event : [];
8914 $event_tags = $wpdb->get_results(
8915 $wpdb->prepare(
8916 'SELECT * FROM ' . $wpdb->prefix . 'amelia_events_tags WHERE eventId = %d',
8917 [ $event_selected ]
8918 ),
8919 ARRAY_A
8920 );
8921 $tags = [];
8922 if ( ! empty( $event_tags ) ) {
8923 foreach ( $event_tags as $key => $tag ) {
8924 $tags['event_tag'][ $key ] = $tag['name'];
8925 }
8926 } else {
8927 $tags = [];
8928 }
8929 $customer_result = $wpdb->get_row(
8930 $wpdb->prepare(
8931 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
8932 [ $result['customerId'] ]
8933 ),
8934 ARRAY_A
8935 );
8936 $customer_result = isset( $customer_result ) ? $customer_result : [];
8937 if ( $result['couponId'] ) {
8938 $coupon_result = $wpdb->get_row(
8939 $wpdb->prepare(
8940 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
8941 [ $result['couponId'] ]
8942 ),
8943 ARRAY_A
8944 );
8945 } else {
8946 $coupon_result = [];
8947 }
8948 if ( ! empty( $result['customFields'] ) ) {
8949 $custom_fields = json_decode( $result['customFields'], true );
8950
8951 $fields_arr = [];
8952 foreach ( (array) $custom_fields as $fields ) {
8953 if ( is_array( $fields ) ) {
8954 $fields_arr[ $fields['label'] ] = $fields['value'];
8955 }
8956 }
8957 unset( $result['customFields'] );
8958 } else {
8959 $fields_arr = [];
8960 }
8961 $context['pluggable_data'] = array_merge( $result, $fields_arr, $event, $customer_result, $coupon_result, $tags );
8962 $context['response_type'] = 'live';
8963 } else {
8964
8965 $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 );
8966 }
8967
8968 return $context;
8969 }
8970
8971 /**
8972 * Get last data for trigger.
8973 *
8974 * @param array $data data.
8975 * @return array
8976 */
8977 public function search_amelia_appointment_rescheduled_triggers_last_data( $data ) {
8978 global $wpdb;
8979
8980 $context = [];
8981
8982 $appointment_category = $data['filter']['amelia_category_list']['value'];
8983 $appointment_service = $data['filter']['amelia_service_list']['value'];
8984
8985 if ( -1 === $appointment_service ) {
8986 // If service exists as per category selected.
8987 $service_exist = $wpdb->get_row(
8988 $wpdb->prepare(
8989 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
8990 [ $appointment_category ]
8991 ),
8992 ARRAY_A
8993 );
8994
8995 if ( empty( $service_exist ) ) {
8996 $result = [];
8997 } else {
8998 $result = $wpdb->get_row(
8999 '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 )',
9000 ARRAY_A
9001 );
9002 }
9003 } else {
9004 $result = $wpdb->get_row(
9005 $wpdb->prepare(
9006 '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',
9007 [ $appointment_service ]
9008 ),
9009 ARRAY_A
9010 );
9011 }
9012
9013 if ( ! empty( $result ) ) {
9014
9015 $payment_result = $wpdb->get_row(
9016 $wpdb->prepare(
9017 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
9018 [ $result['id'] ]
9019 ),
9020 ARRAY_A
9021 );
9022
9023 $customer_result = $wpdb->get_row(
9024 $wpdb->prepare(
9025 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
9026 [ $result['customerId'] ]
9027 ),
9028 ARRAY_A
9029 );
9030
9031 $service_result = $wpdb->get_row(
9032 $wpdb->prepare(
9033 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
9034 [ $result['serviceId'] ]
9035 ),
9036 ARRAY_A
9037 );
9038
9039 $category_result = $wpdb->get_row(
9040 $wpdb->prepare(
9041 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
9042 [ $service_result['categoryId'] ]
9043 ),
9044 ARRAY_A
9045 );
9046
9047 if ( $result['couponId'] ) {
9048 $coupon_result = $wpdb->get_row(
9049 $wpdb->prepare(
9050 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9051 [ $result['couponId'] ]
9052 ),
9053 ARRAY_A
9054 );
9055 } else {
9056 $coupon_result = [];
9057 }
9058
9059 if ( ! empty( $result['customFields'] ) ) {
9060 $custom_fields = json_decode( $result['customFields'], true );
9061
9062 $fields_arr = [];
9063 foreach ( (array) $custom_fields as $fields ) {
9064 if ( is_array( $fields ) ) {
9065 $fields_arr[ $fields['label'] ] = $fields['value'];
9066 }
9067 }
9068 unset( $result['customFields'] );
9069 } else {
9070 $fields_arr = [];
9071 }
9072
9073 $appointment_data['isRescheduled'] = '1';
9074 $context['pluggable_data'] = array_merge( $result, $fields_arr, $appointment_data, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
9075 $context['response_type'] = 'live';
9076 } else {
9077
9078 $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 );
9079 }
9080
9081 return $context;
9082 }
9083
9084
9085 /**
9086 * Get last data for trigger.
9087 *
9088 * @param array $data data.
9089 * @return array
9090 */
9091 public function search_amelia_appointment_cancelled_triggers_last_data( $data ) {
9092 global $wpdb;
9093
9094 $context = [];
9095
9096 $appointment_category = $data['filter']['amelia_category_list']['value'];
9097 $appointment_service = $data['filter']['amelia_service_list']['value'];
9098
9099 if ( -1 === $appointment_service ) {
9100 // If service exists as per category selected.
9101 $service_exist = $wpdb->get_row(
9102 $wpdb->prepare(
9103 'SELECT id, name, description FROM ' . $wpdb->prefix . 'amelia_services WHERE categoryId = %d',
9104 [ $appointment_category ]
9105 ),
9106 ARRAY_A
9107 );
9108
9109 if ( empty( $service_exist ) ) {
9110 $result = [];
9111 } else {
9112 $result = $wpdb->get_row(
9113 $wpdb->prepare(
9114 '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',
9115 [ 'canceled' ]
9116 ),
9117 ARRAY_A
9118 );
9119 }
9120 } else {
9121 $result = $wpdb->get_row(
9122 $wpdb->prepare(
9123 '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',
9124 [ 'canceled', $appointment_service ]
9125 ),
9126 ARRAY_A
9127 );
9128 }
9129
9130 if ( ! empty( $result ) ) {
9131
9132 $payment_result = $wpdb->get_row(
9133 $wpdb->prepare(
9134 'SELECT * FROM ' . $wpdb->prefix . 'amelia_payments WHERE customerBookingId = %d',
9135 [ $result['id'] ]
9136 ),
9137 ARRAY_A
9138 );
9139
9140 $customer_result = $wpdb->get_row(
9141 $wpdb->prepare(
9142 'SELECT * FROM ' . $wpdb->prefix . 'amelia_users WHERE id = %d',
9143 [ $result['customerId'] ]
9144 ),
9145 ARRAY_A
9146 );
9147
9148 $service_result = $wpdb->get_row(
9149 $wpdb->prepare(
9150 'SELECT name AS serviceName, description AS serviceDescription, categoryId FROM ' . $wpdb->prefix . 'amelia_services WHERE id = %d',
9151 [ $result['serviceId'] ]
9152 ),
9153 ARRAY_A
9154 );
9155
9156 $category_result = $wpdb->get_row(
9157 $wpdb->prepare(
9158 'SELECT name AS categoryName FROM ' . $wpdb->prefix . 'amelia_categories WHERE id = %d',
9159 [ $service_result['categoryId'] ]
9160 ),
9161 ARRAY_A
9162 );
9163
9164 if ( $result['couponId'] ) {
9165 $coupon_result = $wpdb->get_row(
9166 $wpdb->prepare(
9167 'SELECT code AS couponCode, expirationDate AS couponExpirationDate FROM ' . $wpdb->prefix . 'amelia_coupons WHERE id = %d',
9168 [ $result['couponId'] ]
9169 ),
9170 ARRAY_A
9171 );
9172 } else {
9173 $coupon_result = [];
9174 }
9175
9176 if ( ! empty( $result['customFields'] ) ) {
9177 $custom_fields = json_decode( $result['customFields'], true );
9178
9179 $fields_arr = [];
9180 foreach ( (array) $custom_fields as $fields ) {
9181 if ( is_array( $fields ) ) {
9182 $fields_arr[ $fields['label'] ] = $fields['value'];
9183 }
9184 }
9185 unset( $result['customFields'] );
9186 } else {
9187 $fields_arr = [];
9188 }
9189
9190 $context['pluggable_data'] = array_merge( $result, $fields_arr, $payment_result, $customer_result, $service_result, $category_result, $coupon_result );
9191 $context['response_type'] = 'live';
9192 } else {
9193
9194 $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 );
9195 }
9196
9197 return $context;
9198 }
9199
9200 /**
9201 * Get MailPoet Forms.
9202 *
9203 * @param array $data data.
9204 *
9205 * @return array
9206 */
9207 public function search_mailpoet_forms( $data ) {
9208 if ( ! class_exists( '\MailPoet\API\API' ) ) {
9209 return;
9210 }
9211
9212 global $wpdb;
9213
9214 $page = $data['page'];
9215 $limit = Utilities::get_search_page_limit();
9216 $offset = $limit * ( $page - 1 );
9217
9218 $forms = $wpdb->get_results(
9219 $wpdb->prepare(
9220 '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',
9221 [ 'enabled', $limit, $offset ]
9222 )
9223 );
9224
9225 $form_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9226
9227 $options = [];
9228
9229 if ( ! empty( $forms ) ) {
9230 if ( is_array( $forms ) ) {
9231 foreach ( $forms as $form ) {
9232 $options[] = [
9233 'label' => $form->name,
9234 'value' => $form->id,
9235 ];
9236 }
9237 }
9238 }
9239
9240 return [
9241 'options' => $options,
9242 'hasMore' => $form_count > $limit && $form_count > $offset,
9243 ];
9244
9245 }
9246
9247 /**
9248 * Get MailPoet List.
9249 *
9250 * @param array $data data.
9251 *
9252 * @return array
9253 */
9254 public function search_mailpoet_list( $data ) {
9255 if ( ! class_exists( '\MailPoet\API\API' ) ) {
9256 return;
9257 }
9258
9259 $mailpoet = \MailPoet\API\API::MP( 'v1' );
9260 $lists = $mailpoet->getLists();
9261
9262 $options = [];
9263
9264 if ( ! empty( $lists ) ) {
9265 if ( is_array( $lists ) ) {
9266 foreach ( $lists as $list ) {
9267 $options[] = [
9268 'label' => $list['name'],
9269 'value' => $list['id'],
9270 ];
9271 }
9272 }
9273 }
9274
9275 return [
9276 'options' => $options,
9277 'hasMore' => false,
9278 ];
9279 }
9280
9281 /**
9282 * Get MailPoet Subscriber Status.
9283 *
9284 * @param array $data data.
9285 *
9286 * @return array
9287 */
9288 public function search_mailpoet_subscriber_status( $data ) {
9289 if ( ! class_exists( '\MailPoet\API\API' ) ) {
9290 return;
9291 }
9292
9293 $subscriber_status = [
9294 'subscribed' => 'Subscribed',
9295 'unconfirmed' => 'Unconfirmed',
9296 'unsubscribed' => 'Unsubscribed',
9297 'inactive' => 'Inactive',
9298 'bounced' => 'Bounced',
9299 ];
9300
9301 $options = [];
9302 foreach ( $subscriber_status as $key => $status ) {
9303 $options[] = [
9304 'label' => $status,
9305 'value' => $key,
9306 ];
9307 }
9308
9309 return [
9310 'options' => $options,
9311 'hasMore' => false,
9312 ];
9313 }
9314
9315 /**
9316 * Get MailPoet Subscribers.
9317 *
9318 * @param array $data data.
9319 *
9320 * @return array
9321 */
9322 public function search_mailpoet_subscribers( $data ) {
9323 if ( ! class_exists( '\MailPoet\API\API' ) ) {
9324 return;
9325 }
9326
9327 global $wpdb;
9328
9329 $page = $data['page'];
9330 $limit = Utilities::get_search_page_limit();
9331 $offset = $limit * ( $page - 1 );
9332
9333 $subscribers = $wpdb->get_results(
9334 $wpdb->prepare(
9335 'SELECT SQL_CALC_FOUND_ROWS id,email FROM ' . $wpdb->prefix . 'mailpoet_subscribers ORDER BY id DESC LIMIT %d OFFSET %d',
9336 [ $limit, $offset ]
9337 )
9338 );
9339
9340 $subscribers_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
9341
9342 $options = [];
9343
9344 if ( ! empty( $subscribers ) ) {
9345 if ( is_array( $subscribers ) ) {
9346 foreach ( $subscribers as $subscriber ) {
9347 $options[] = [
9348 'label' => $subscriber->email,
9349 'value' => $subscriber->id,
9350 ];
9351 }
9352 }
9353 }
9354
9355 return [
9356 'options' => $options,
9357 'hasMore' => $subscribers_count > $limit && $subscribers_count > $offset,
9358 ];
9359 }
9360
9361 /**
9362 * Get ConvertPro Forms.
9363 *
9364 * @param array $data data.
9365 *
9366 * @return array
9367 */
9368 public function search_convertpro_form_list( $data ) {
9369 if ( ! class_exists( '\Cp_V2_Loader' ) ) {
9370 return;
9371 }
9372
9373 $cp_popups_inst = CP_V2_Popups::get_instance();
9374 $popups = $cp_popups_inst->get_all();
9375
9376 $form_count = count( $popups );
9377
9378 $page = $data['page'];
9379 $limit = Utilities::get_search_page_limit();
9380 $offset = $limit * ( $page - 1 );
9381
9382 $options = [];
9383
9384 if ( ! empty( $popups ) ) {
9385 if ( is_array( $popups ) ) {
9386 foreach ( $popups as $form ) {
9387 $options[] = [
9388 'label' => $form->post_title,
9389 'value' => $form->ID,
9390 ];
9391 }
9392 }
9393 }
9394
9395 return [
9396 'options' => $options,
9397 'hasMore' => $form_count > $limit && $form_count > $offset,
9398 ];
9399
9400 }
9401
9402 /**
9403 * Get ProjectHuddle Websites.
9404 *
9405 * @param array $data data.
9406 *
9407 * @return array
9408 */
9409 public function search_project_huddle_websites( $data ) {
9410
9411 $sites = new WP_Query(
9412 [
9413 'post_type' => 'ph-website',
9414 'posts_per_page' => - 1,
9415 'fields' => 'ids',
9416 ]
9417 );
9418
9419 $site_ids = (array) $sites->posts;
9420
9421 $options = [];
9422 if ( ! empty( $site_ids ) ) {
9423 if ( is_array( $site_ids ) ) {
9424 foreach ( $site_ids as $site_id ) {
9425 $options[] = [
9426 'label' => get_the_title( $site_id ),
9427 'value' => $site_id,
9428 ];
9429 }
9430 }
9431 }
9432 return [
9433 'options' => $options,
9434 'hasMore' => false,
9435 ];
9436
9437 }
9438
9439 /**
9440 * Get last data for trigger.
9441 *
9442 * @param array $data data.
9443 * @return mixed
9444 */
9445 public function search_project_huddle_comment_triggers_last_data( $data ) {
9446 global $wpdb;
9447
9448 $context = [];
9449 if ( ! class_exists( 'PH\Models\Post' ) || ! function_exists( 'ph_get_the_title' ) ) {
9450 return [];
9451 }
9452
9453 if ( -1 !== $data['dynamic'] ) {
9454 $threads = get_posts(
9455 [
9456 'post_type' => 'phw_comment_loc',
9457 'posts_per_page' => 1,
9458 'meta_value' => $data['dynamic'],
9459 'meta_key' => 'project_id',
9460 'orderby' => 'asc',
9461 ]
9462 );
9463 } else {
9464 $threads = [];
9465 }
9466
9467 if ( ! empty( $threads ) ) {
9468 $comment_result = $wpdb->get_row(
9469 $wpdb->prepare(
9470 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
9471 FROM ' . $wpdb->prefix . 'comments
9472 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
9473 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
9474 LIMIT 0,1',
9475 $threads[0]->ID
9476 ),
9477 ARRAY_A
9478 );
9479
9480 if ( ! empty( $comment_result ) ) {
9481 $comment_id = get_comment( $comment_result['comment_ID'], ARRAY_A );
9482 $comments['comment_ID'] = isset( $comment_id['comment_ID'] ) ? $comment_id['comment_ID'] : '';
9483 $comments['comment_post_ID'] = isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : '';
9484
9485 $comments['comment_author'] = isset( $comment_id['comment_author'] ) ? $comment_id['comment_author'] : '';
9486
9487 $comments['comment_author_email'] = isset( $comment_id['comment_author_email'] ) ? $comment_id['comment_author_email'] : '';
9488
9489 $comments['comment_date'] = isset( $comment_id['comment_date'] ) ? $comment_id['comment_date'] : '';
9490
9491 $comments['comment_content'] = isset( $comment_id['comment_content'] ) ? $comment_id['comment_content'] : '';
9492
9493 $comments['comment_type'] = isset( $comment_id['comment_type'] ) ? $comment_id['comment_type'] : '';
9494
9495 $comment_item_id = get_comment_meta( $comment_result['comment_ID'], 'item_id' );
9496 if ( ! empty( $comment_item_id ) && is_array( $comment_item_id ) ) {
9497 $comments['comment_item_id'] = $comment_item_id[0];
9498 $comments['comment_item_page_title'] = get_the_title( (int) $comment_item_id[0] );
9499 $comments['comment_item_page_url'] = get_post_meta( (int) $comment_item_id[0], 'page_url', true );
9500 }
9501 if ( isset( $comment_id['comment_post_ID'] ) || isset( $comment_id['comment_author'] ) ) {
9502 $comments['ph_project_name'] = ph_get_the_title( Post::get( $comment_id['comment_post_ID'] )->parentsIds()['project'] );
9503 $comments['ph_commenter_name'] = $comment_id['comment_author'];
9504 $comments['ph_project_type'] = ( get_post_type( $comment_id['comment_post_ID'] ) == 'ph-website' ) ? __( 'Website', 'suretriggers' ) : __( 'Mockup', 'suretriggers' );
9505 $comments['ph_action_status'] = get_post_meta( $comment_id['comment_post_ID'], 'resolved', true ) ? __( 'Resolved', 'suretriggers' ) : __( 'Unresolved', 'suretriggers' );
9506 $comments['ph_project_link'] = get_the_guid( $comment_id['comment_post_ID'] );
9507 }
9508
9509 $context['pluggable_data'] = $comments;
9510 $context['response_type'] = 'live';
9511 } else {
9512 $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 );
9513 }
9514 } else {
9515 $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 );
9516 }
9517
9518 return $context;
9519 }
9520
9521 /**
9522 * Get last data for trigger.
9523 *
9524 * @param array $data data.
9525 * @return mixed
9526 */
9527 public function search_project_huddle_resolved_comment_triggers_last_data( $data ) {
9528 global $wpdb;
9529
9530 $context = [];
9531 if ( ! class_exists( 'PH\Models\Post' ) || ! function_exists( 'ph_get_the_title' ) ) {
9532 return [];
9533 }
9534
9535 $get_comments = $wpdb->get_row(
9536 'SELECT ' . $wpdb->prefix . 'comments.comment_ID, ' . $wpdb->prefix . 'comments.comment_content
9537 FROM ' . $wpdb->prefix . 'comments
9538 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_status") AND comment_content = "Resolved"
9539 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
9540 LIMIT 0,1'
9541 );
9542
9543 if ( ! empty( $get_comments ) ) {
9544 $comment_id = get_comment( $get_comments->comment_ID, ARRAY_A );
9545 $comment_result = $wpdb->get_row(
9546 $wpdb->prepare(
9547 'SELECT ' . $wpdb->prefix . 'comments.comment_ID
9548 FROM ' . $wpdb->prefix . 'comments
9549 WHERE ( ( comment_approved = "0" OR comment_approved = "1" ) ) AND comment_type IN ("ph_comment") AND comment_post_ID = %d
9550 ORDER BY ' . $wpdb->prefix . 'comments.comment_date_gmt DESC
9551 LIMIT 0,1',
9552 isset( $comment_id['comment_post_ID'] ) ? $comment_id['comment_post_ID'] : ''
9553 ),
9554 ARRAY_A
9555 );
9556
9557 $actual_comment = get_comment( $comment_result['comment_ID'], ARRAY_A );
9558 $comments['comment_ID'] = isset( $actual_comment['comment_ID'] ) ? $actual_comment['comment_ID'] : '';
9559 $comments['comment_post_ID'] = isset( $actual_comment['comment_post_ID'] ) ? $actual_comment['comment_post_ID'] : '';
9560 $comments['comment_author'] = isset( $actual_comment['comment_author'] ) ? $actual_comment['comment_author'] : '';
9561 $comments['comment_author_email'] = isset( $actual_comment['comment_author_email'] ) ? $actual_comment['comment_author_email'] : '';
9562 $comments['comment_date'] = isset( $actual_comment['comment_date'] ) ? $actual_comment['comment_date'] : '';
9563 $comments['comment_content'] = isset( $actual_comment['comment_content'] ) ? $actual_comment['comment_content'] : '';
9564 $comments['comment_type'] = isset( $actual_comment['comment_type'] ) ? $actual_comment['comment_type'] : '';
9565 $comments['comment_status'] = $get_comments->comment_content;
9566 $comment_item_id = get_comment_meta( $comment_result['comment_ID'], 'item_id' );
9567 if ( ! empty( $comment_item_id ) && is_array( $comment_item_id ) ) {
9568 $comments['comment_item_id'] = $comment_item_id[0];
9569 $comments['comment_item_page_title'] = get_the_title( (int) $comment_item_id[0] );
9570 $comments['comment_item_page_url'] = get_post_meta( (int) $comment_item_id[0], 'page_url', true );
9571 }
9572 if ( isset( $comment_id['comment_post_ID'] ) || isset( $comment_id['comment_author'] ) ) {
9573 $comments['ph_project_name'] = ph_get_the_title( Post::get( $comment_id['comment_post_ID'] )->parentsIds()['project'] );
9574 $comments['ph_commenter_name'] = $comment_id['comment_author'];
9575 $comments['ph_project_type'] = ( get_post_type( $comment_id['comment_post_ID'] ) == 'ph-website' ) ? __( 'Website', 'suretriggers' ) : __( 'Mockup', 'suretriggers' );
9576 $comments['ph_action_status'] = get_post_meta( $comment_id['comment_post_ID'], 'resolved', true ) ? __( 'Resolved', 'suretriggers' ) : __( 'Unresolved', 'suretriggers' );
9577 $comments['ph_project_link'] = get_the_guid( $comment_id['comment_post_ID'] );
9578 }
9579 $context['pluggable_data'] = $comments;
9580 $context['response_type'] = 'live';
9581 } else {
9582 $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 );
9583 }
9584
9585 return $context;
9586 }
9587
9588 /**
9589 * Get MasterStudy LMS Courses.
9590 *
9591 * @param array $data data.
9592 *
9593 * @return array
9594 */
9595 public function search_ms_lms_courses( $data ) {
9596
9597 $page = $data['page'];
9598 $limit = Utilities::get_search_page_limit();
9599 $offset = $limit * ( $page - 1 );
9600
9601 $args = [
9602 'post_type' => 'stm-courses',
9603 'posts_per_page' => $limit,
9604 'offset' => $offset,
9605 'orderby' => 'title',
9606 'order' => 'ASC',
9607 'post_status' => 'publish',
9608 ];
9609
9610 $courses = get_posts( $args );
9611
9612 $course_count = wp_count_posts( 'stm-courses' )->publish;
9613
9614 $options = [];
9615 if ( ! empty( $courses ) ) {
9616 if ( is_array( $courses ) ) {
9617 foreach ( $courses as $course ) {
9618 $options[] = [
9619 'label' => $course->post_title,
9620 'value' => $course->ID,
9621 ];
9622 }
9623 }
9624 }
9625 return [
9626 'options' => $options,
9627 'hasMore' => $course_count > $limit && $course_count > $offset,
9628 ];
9629
9630 }
9631
9632 /**
9633 * Get MasterStudy LMS Lessons.
9634 *
9635 * @param array $data data.
9636 *
9637 * @return array
9638 */
9639 public function search_ms_lms_lessons( $data ) {
9640
9641 global $wpdb;
9642 $options = [];
9643 $course_id = $data['dynamic'];
9644
9645 // Use curriculum repository class.
9646 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
9647 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
9648 } else {
9649 $curriculum_repo = false;
9650 }
9651
9652 if ( '-1' === $course_id ) {
9653 $lessons = $wpdb->get_results(
9654 $wpdb->prepare(
9655 "SELECT SQL_CALC_FOUND_ROWS ID, post_title FROM $wpdb->posts WHERE post_type = %s ORDER BY post_title ASC",
9656 'stm-lessons'
9657 )
9658 );
9659
9660 if ( ! empty( $lessons ) ) {
9661 if ( is_array( $lessons ) ) {
9662 foreach ( $lessons as $lesson ) {
9663 $options[] = [
9664 'label' => $lesson->post_title,
9665 'value' => $lesson->ID,
9666 ];
9667 }
9668 }
9669 }
9670 } else {
9671 $lessons = [];
9672 if ( $curriculum_repo ) {
9673 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
9674 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
9675 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
9676 foreach ( $curriculum['materials'] as $material ) {
9677 if ( 'stm-lessons' === $material['post_type'] ) {
9678 $lessons[] = [
9679 'value' => $material['post_id'],
9680 'text' => $material['title'],
9681 ];
9682 }
9683 }
9684 }
9685 }
9686 if ( ! empty( $lessons ) ) {
9687 if ( is_array( $lessons ) ) {
9688 foreach ( $lessons as $lesson ) {
9689 $options[] = [
9690 'label' => $lesson['text'],
9691 'value' => $lesson['value'],
9692 ];
9693 }
9694 }
9695 }
9696 } else {
9697 $lessons = $wpdb->get_results(
9698 $wpdb->prepare(
9699 "SELECT SQL_CALC_FOUND_ROWS ID, post_title
9700 FROM $wpdb->posts
9701 WHERE FIND_IN_SET(
9702 ID,
9703 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
9704 )
9705 AND post_type = 'stm-lessons'
9706 ORDER BY post_title ASC",
9707 absint( $course_id )
9708 )
9709 );
9710 if ( ! empty( $lessons ) ) {
9711 if ( is_array( $lessons ) ) {
9712 foreach ( $lessons as $lesson ) {
9713 $options[] = [
9714 'label' => $lesson->post_title,
9715 'value' => $lesson->ID,
9716 ];
9717 }
9718 }
9719 }
9720 }
9721 }
9722 return [
9723 'options' => $options,
9724 'hasMore' => false,
9725 ];
9726
9727 }
9728
9729 /**
9730 * Get MasterStudy LMS Quiz.
9731 *
9732 * @param array $data data.
9733 *
9734 * @return array
9735 */
9736 public function search_ms_lms_quiz( $data ) {
9737
9738 global $wpdb;
9739
9740 $options = [];
9741 $course_id = $data['dynamic'];
9742
9743 // Use curriculum repository class.
9744 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
9745 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
9746 } else {
9747 $curriculum_repo = false;
9748 }
9749
9750 if ( '-1' === $course_id ) {
9751 $quizzes = $wpdb->get_results(
9752 $wpdb->prepare(
9753 "SELECT ID, post_title
9754 FROM $wpdb->posts
9755 WHERE post_type = %s
9756 ORDER BY post_title ASC",
9757 'stm-quizzes'
9758 )
9759 );
9760 if ( ! empty( $quizzes ) ) {
9761 if ( is_array( $quizzes ) ) {
9762 foreach ( $quizzes as $quiz ) {
9763 $options[] = [
9764 'label' => $quiz->post_title,
9765 'value' => $quiz->ID,
9766 ];
9767 }
9768 }
9769 }
9770 } else {
9771 $quizzes = [];
9772 if ( $curriculum_repo ) {
9773 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
9774 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
9775 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
9776 foreach ( $curriculum['materials'] as $material ) {
9777 if ( 'stm-quizzes' === $material['post_type'] ) {
9778 $quizzes[] = [
9779 'value' => $material['post_id'],
9780 'text' => $material['title'],
9781 ];
9782 }
9783 }
9784 }
9785 }
9786 if ( ! empty( $quizzes ) ) {
9787 if ( is_array( $quizzes ) ) {
9788 foreach ( $quizzes as $quiz ) {
9789 $options[] = [
9790 'label' => $quiz['text'],
9791 'value' => $quiz['value'],
9792 ];
9793 }
9794 }
9795 }
9796 } else {
9797 $quizzes = $wpdb->get_results(
9798 $wpdb->prepare(
9799 "SELECT ID, post_title
9800 FROM $wpdb->posts
9801 WHERE FIND_IN_SET(
9802 ID,
9803 (SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'curriculum')
9804 )
9805 AND post_type = 'stm-quizzes'
9806 ORDER BY post_title ASC
9807 ",
9808 absint( $course_id )
9809 )
9810 );
9811 if ( ! empty( $quizzes ) ) {
9812 if ( is_array( $quizzes ) ) {
9813 foreach ( $quizzes as $quiz ) {
9814 $options[] = [
9815 'label' => $quiz->post_title,
9816 'value' => $quiz->ID,
9817 ];
9818 }
9819 }
9820 }
9821 }
9822 }
9823 return [
9824 'options' => $options,
9825 'hasMore' => false,
9826 ];
9827
9828 }
9829
9830 /**
9831 * Search MasterStudy LMS data.
9832 *
9833 * @param array $data data.
9834 * @return array|void
9835 */
9836 public function search_ms_lms_last_data( $data ) {
9837 global $wpdb;
9838 $post_type = $data['post_type'];
9839 $trigger = $data['search_term'];
9840 $context = [];
9841
9842 if ( 'stm_lms_course_completed' === $trigger ) {
9843 $post_id = $data['filter']['course']['value'];
9844 if ( -1 === $post_id ) {
9845 $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 ) );
9846 } else {
9847 $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 ) );
9848 }
9849 } elseif ( 'stm_lesson_passed' === $trigger ) {
9850 $post_id = $data['filter']['lesson']['value'];
9851 if ( -1 === $post_id ) {
9852 $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 ) );
9853 } else {
9854 $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 ) );
9855 }
9856 } elseif ( 'stm_quiz_passed' === $trigger ) {
9857 $post_id = $data['filter']['quiz']['value'];
9858 if ( -1 === $post_id ) {
9859 $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 ) );
9860 } else {
9861 $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 ) );
9862 }
9863 } elseif ( 'stm_quiz_failed' === $trigger ) {
9864 $post_id = $data['filter']['quiz']['value'];
9865 if ( -1 === $post_id ) {
9866 $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 ) );
9867 } else {
9868 $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 ) );
9869 }
9870 } elseif ( 'stm_lms_user_enroll_course' === $trigger ) {
9871 $post_id = $data['filter']['course']['value'];
9872 if ( -1 === $post_id ) {
9873 $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 ) );
9874 } else {
9875 $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 ) );
9876 }
9877 }
9878
9879 if ( ! empty( $result ) ) {
9880
9881 switch ( $trigger ) {
9882 case 'stm_lms_course_completed':
9883 $result_course_id = $result[0]->course_id;
9884 $result_user_id = $result[0]->user_id;
9885 $course = get_the_title( $result_course_id );
9886 $course_link = get_the_permalink( $result_course_id );
9887 $featured_image = get_the_post_thumbnail_url( $result_course_id );
9888
9889 $data = [
9890 'course_id' => $result_course_id,
9891 'course_title' => $course,
9892 'course_link' => $course_link,
9893 'course_featured_image' => $featured_image,
9894 'course_progress' => $result[0]->progress_percent,
9895 ];
9896 $context_data = array_merge(
9897 WordPress::get_user_context( $result_user_id ),
9898 $data
9899 );
9900 break;
9901 case 'stm_lesson_passed':
9902 $result_lesson_id = $result[0]->lesson_id;
9903 $result_user_id = $result[0]->user_id;
9904 $lesson = get_the_title( $result_lesson_id );
9905 $lesson_link = get_the_permalink( $result_lesson_id );
9906
9907 $data = [
9908 'lesson_id' => $result_lesson_id,
9909 'lesson_title' => $lesson,
9910 'lesson_link' => $lesson_link,
9911 ];
9912 $context_data = array_merge(
9913 WordPress::get_user_context( $result_user_id ),
9914 $data
9915 );
9916 break;
9917 case 'stm_quiz_passed':
9918 $result_quiz_id = $result[0]->quiz_id;
9919 $result_user_id = $result[0]->user_id;
9920 $quiz_title = get_the_title( $result_quiz_id );
9921 $quiz_link = get_the_permalink( $result_quiz_id );
9922
9923 $data = [
9924 'quiz_id' => $result_quiz_id,
9925 'quiz_title' => $quiz_title,
9926 'quiz_link' => $quiz_link,
9927 'quiz_score' => $result[0]->progress,
9928 'result' => 'passed',
9929 ];
9930 $context_data = array_merge(
9931 WordPress::get_user_context( $result_user_id ),
9932 $data
9933 );
9934 break;
9935 case 'stm_quiz_failed':
9936 $result_quiz_id = $result[0]->quiz_id;
9937 $result_user_id = $result[0]->user_id;
9938 $quiz_title = get_the_title( $result_quiz_id );
9939 $quiz_link = get_the_permalink( $result_quiz_id );
9940
9941 $data = [
9942 'quiz_id' => $result_quiz_id,
9943 'quiz_title' => $quiz_title,
9944 'quiz_link' => $quiz_link,
9945 'quiz_score' => $result[0]->progress,
9946 'result' => 'failed',
9947 ];
9948 $context_data = array_merge(
9949 WordPress::get_user_context( $result_user_id ),
9950 $data
9951 );
9952 break;
9953 case 'stm_lms_user_enroll_course':
9954 $result_course_id = $result[0]->course_id;
9955 $result_user_id = $result[0]->user_id;
9956
9957 $course = get_the_title( $result_course_id );
9958 $course_link = get_the_permalink( $result_course_id );
9959 $featured_image = get_the_post_thumbnail_url( $result_course_id );
9960
9961 $data = [
9962 'course_id' => $result_course_id,
9963 'course_title' => $course,
9964 'course_link' => $course_link,
9965 'course_featured_image' => $featured_image,
9966 ];
9967 $context_data = array_merge(
9968 WordPress::get_user_context( $result_user_id ),
9969 $data
9970 );
9971 break;
9972 default:
9973 return;
9974 }
9975 $context['pluggable_data'] = $context_data;
9976 $context['response_type'] = 'live';
9977 }
9978
9979 return $context;
9980
9981 }
9982
9983 /**
9984 * Prepare Fluent Support Mailbox list.
9985 *
9986 * @param array $data data.
9987 *
9988 * @return array
9989 */
9990 public function search_fs_mailbox_list( $data ) {
9991 $options = [];
9992 global $wpdb;
9993 $mailboxes = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_mail_boxes ORDER BY id DESC", ARRAY_A );
9994 if ( ! empty( $mailboxes ) ) {
9995 foreach ( $mailboxes as $key => $value ) {
9996
9997 $options[] = [
9998 'label' => $value['name'],
9999 'value' => $value['id'],
10000 ];
10001
10002 }
10003 }
10004
10005 return [
10006 'options' => $options,
10007 'hasMore' => false,
10008 ];
10009 }
10010
10011 /**
10012 * Prepare Fluent Support Product list.
10013 *
10014 * @param array $data data.
10015 *
10016 * @return array
10017 */
10018 public function search_fs_product_list( $data ) {
10019 $options = [];
10020
10021 if ( ! class_exists( '\FluentSupport\App\Models\Product' ) ) {
10022 return [];
10023 }
10024
10025 $products = \FluentSupport\App\Models\Product::select( [ 'id', 'title' ] )->get();
10026 if ( ! empty( $products ) ) {
10027 foreach ( $products as $key => $product ) {
10028 $options[] = [
10029 'label' => $product['title'],
10030 'value' => $product['id'],
10031 ];
10032 }
10033 }
10034
10035 return [
10036 'options' => $options,
10037 'hasMore' => false,
10038 ];
10039 }
10040
10041 /**
10042 * Prepare Fluent Support Product list.
10043 *
10044 * @param array $data data.
10045 *
10046 * @return array
10047 */
10048 public function search_fs_person_type_list( $data ) {
10049 $options = [
10050 [
10051 'label' => 'Customer',
10052 'value' => 'customer',
10053 ],
10054 [
10055 'label' => 'Agent',
10056 'value' => 'agent',
10057 ],
10058 ];
10059
10060 return [
10061 'options' => $options,
10062 'hasMore' => false,
10063 ];
10064 }
10065
10066 /**
10067 * Get last data for trigger.
10068 *
10069 * @param array $data data.
10070 * @return array
10071 */
10072 public function search_fluent_support_triggers_last_data( $data ) {
10073 global $wpdb;
10074 $context = [];
10075 $context['response_type'] = 'sample';
10076
10077 if ( ! class_exists( '\FluentSupport\App\Models\Ticket' ) ) {
10078 return [];
10079 }
10080
10081 $ticket_data = [
10082 'id' => '1',
10083 'customer_id' => '2',
10084 'agent_id' => '3',
10085 'product_id' => '5',
10086 'product_source' => 'local',
10087 'privacy' => 'private',
10088 'priority' => 'normal',
10089 'client_priority' => 'medium',
10090 'status' => 'active',
10091 'title' => 'Sample Ticket Title',
10092 'slug' => 'sample-ticket-title',
10093 'hash' => 'f8a8cfb946',
10094 'content_hash' => 'd65500d62621be8b493c22b1d888052c',
10095 'content' => '<p>Sample content.</p>',
10096 'last_customer_response' => '2023-04-27 07:30:46',
10097 'waiting_since' => '2023-04-27 07:30:46',
10098 'response_count' => '2',
10099 'total_close_time' => '7042',
10100 'resolved at' => '2023-04-27 09:28:08',
10101 'closed_by' => '1',
10102 'created_at' => '2023-04-27 07:30:46',
10103 'updated_at' => '2023-04-27 10:28:08',
10104 'mailbox_id' => '1',
10105 'mailbox_name' => 'OttoKit',
10106 'mailbox_slug' => 'suretriggers',
10107 'mailbox_box_type' => 'web',
10108 'mailbox email' => 'john_doe@sample.com',
10109 'mailbox_settings_admin_email_address' => 'john_doe@sample.com',
10110 'mailbox_created_by' => '1',
10111 'mailbox_is_default' => 'yes',
10112 'mailbox_created_at' => '2023-04-26 06:29:01',
10113 'mailbox_updated_at' => '2023-04-26 06:29:01',
10114 ];
10115
10116 $customer_data = [
10117 'id' => '1',
10118 'first_name' => 'John',
10119 'last_name' => 'Doe',
10120 'email' => 'john_doe@sample.com',
10121 'person_type' => 'agent',
10122 'status' => 'active',
10123 'hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
10124 'user_id' => '1',
10125 'created_at' => '2023-04-27 07:30:46',
10126 'updated_at' => '2023-04-27 10:28:08',
10127 'full_name' => 'John Doe',
10128 'photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
10129 ];
10130
10131 $reply_data = [
10132 'ticket_id' => '1',
10133 'conversation_type' => 'response',
10134 'content' => '<p>Sample content.</p>',
10135 'source' => 'web',
10136 'content_hash' => '2cc0e35d8fb92a0675d67999b073b3a4',
10137 'created_at' => '2023-04-27 07:30:46',
10138 'updated_at' => '2023-04-27 10:28:08',
10139 'id' => '1',
10140 'person_id' => '2',
10141 'person_first_name' => 'John',
10142 'person_last_name' => 'Doe',
10143 'person_email' => 'john_doe@sample.com',
10144 'person_person_type' => 'agent',
10145 'person_status' => 'active',
10146 'person_hash' => '3b2b5f0432561cb81b1302b8a16b93a0',
10147 'person_user_id' => '1',
10148 'person_created_at' => '2023-04-27 07:30:46',
10149 'person_updated_at' => '2023-04-27 10:28:08',
10150 'person_full_name' => 'John Doe',
10151 'person_photo' => 'https://www.gravatar.com/avatar/c2b06ae950033b392998ada50767b50e?s=128',
10152 ];
10153
10154 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
10155 $mailbox = isset( $data['mailbox_id'] ) ? $data['mailbox_id'] : '';
10156 $product_id = isset( $data['filter']['ticket_product_id']['value'] ) ? $data['filter']['ticket_product_id']['value'] : '';
10157 $person_id = isset( $data['filter']['person_id']['value'] ) ? (string) $data['filter']['person_id']['value'] : '';
10158
10159 if ( in_array( $term, [ 'response_added_by_agent', 'response_added_by_customer', 'ticket_replied_product_agent', 'ticket_replied_product_customer' ], true ) ) {
10160 if ( 'response_added_by_agent' == $term ) {
10161 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
10162 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
10163 $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 ) );
10164 if ( ! empty( $tickets ) ) {
10165 $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 ) );
10166 $ticket_data = $tickets[0];
10167 $customer_data = $customers[0];
10168 $reply_data = $replies[0];
10169 $context['response_type'] = 'live';
10170 }
10171 } elseif ( 'response_added_by_customer' == $term ) {
10172 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
10173 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
10174 $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 ) );
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 $reply_data = $replies[0];
10180 $context['response_type'] = 'live';
10181 }
10182 } elseif ( 'ticket_replied_product_agent' == $term ) {
10183 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
10184 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
10185 if ( -1 == $product_id ) {
10186 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
10187 $products = array_map(
10188 function( $product ) {
10189 return $product['id'];
10190 },
10191 $products
10192 );
10193 $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 ) ) );
10194 } else {
10195 $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 ) );
10196 }
10197 if ( ! empty( $tickets ) ) {
10198 $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 ) );
10199 $ticket_data = $tickets[0];
10200 $customer_data = $customers[0];
10201 $reply_data = $replies[0];
10202 $context['response_type'] = 'live';
10203 }
10204 } elseif ( 'ticket_replied_product_customer' == $term ) {
10205 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
10206 $replies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_conversations WHERE conversation_type = 'response' ORDER BY id DESC LIMIT 1" );
10207 if ( -1 == $product_id ) {
10208 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
10209 $products = array_map(
10210 function( $product ) {
10211 return $product['id'];
10212 },
10213 $products
10214 );
10215 $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 ) ) );
10216 } else {
10217 $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 ) );
10218 }
10219 if ( ! empty( $tickets ) ) {
10220 $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 ) );
10221 $ticket_data = $tickets[0];
10222 $customer_data = $customers[0];
10223 $reply_data = $replies[0];
10224 $context['response_type'] = 'live';
10225 }
10226 }
10227 $context['pluggable_data'] = array_merge(
10228 [
10229 'reply' => $reply_data,
10230 'ticket' => $ticket_data,
10231 'customer' => $customer_data,
10232 ]
10233 );
10234 if ( 'ticket_replied_product_customer' == $term || 'ticket_replied_product_agent' == $term ) {
10235 $context['pluggable_data']['ticket_product_id'] = $product_id;
10236 }
10237 if ( is_object( $ticket_data ) ) {
10238 $ticket_data = get_object_vars( $ticket_data );
10239 }
10240 $ticket = \FluentSupport\App\Models\Ticket::find( $ticket_data['id'] );
10241 if ( is_object( $ticket ) && method_exists( $ticket, 'customData' ) ) {
10242 $context['pluggable_data']['custom_fields'] = $ticket->customData();
10243 }
10244 $context['pluggable_data']['ticket_link'] = admin_url( "admin.php?page=fluent-support#/tickets/{$ticket_data['id']}/view" );
10245 } else {
10246 if ( 'ticket_created' == $term ) {
10247 if ( -1 == $mailbox ) {
10248 $tickets = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE status = 'new' ORDER BY id DESC LIMIT 1" );
10249 } else {
10250 $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 ) );
10251 }
10252 if ( ! empty( $tickets ) ) {
10253 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_persons WHERE id = %d", $tickets[0]->customer_id ) );
10254 $ticket_data = $tickets[0];
10255 $customer_data = $customers[0];
10256 }
10257 } elseif ( 'ticket_closed_by_customer' == $term ) {
10258 $customers_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'customer'" );
10259 $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] ) );
10260 if ( ! empty( $tickets ) ) {
10261 $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 ) );
10262 $ticket_data = $tickets[0];
10263 $customer_data = $customers[0];
10264 }
10265 } elseif ( 'ticket_closed_by_agent' == $term ) {
10266 $agents_type = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = 'agent'" );
10267 $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] ) );
10268 if ( ! empty( $tickets ) ) {
10269 $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 ) );
10270 $ticket_data = $tickets[0];
10271 $customer_data = $customers[0];
10272 }
10273 } elseif ( 'ticket_created_product' == $term ) {
10274 $agents_type = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fs_persons" );
10275 if ( -1 == $product_id ) {
10276 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
10277 $products = array_map(
10278 function( $product ) {
10279 return $product['id'];
10280 },
10281 $products
10282 );
10283 $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 ) ) );
10284 } else {
10285 $tickets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fs_tickets WHERE product_id = %d ORDER BY id DESC LIMIT 1", $product_id ) );
10286 }
10287 if ( ! empty( $tickets ) ) {
10288 $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 ) );
10289 $ticket_data = $tickets[0];
10290 $customer_data = $customers[0];
10291 }
10292 } elseif ( 'ticket_closed_product' == $term ) {
10293 $agents_type = $wpdb->get_results( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}fs_persons WHERE person_type = %s", $person_id ), ARRAY_A );
10294 $agents = array_map(
10295 function( $agent ) {
10296 return $agent['id'];
10297 },
10298 $agents_type
10299 );
10300 if ( -1 == $product_id ) {
10301 $products = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}fs_products", ARRAY_A );
10302 $products = array_map(
10303 function( $product ) {
10304 return $product['id'];
10305 },
10306 $products
10307 );
10308 $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 ) ) );
10309 } else {
10310 $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 ) ) );
10311 }
10312 if ( ! empty( $tickets ) ) {
10313 $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 ) );
10314 $ticket_data = $tickets[0];
10315 $customer_data = $customers[0];
10316 }
10317 }
10318 $context['pluggable_data'] = array_merge(
10319 [
10320 'ticket' => $ticket_data,
10321 'customer' => $customer_data,
10322 ]
10323 );
10324 if ( 'ticket_created' == $term ) {
10325 $context['pluggable_data']['mailbox_id'] = $mailbox;
10326 } elseif ( 'ticket_created_product' == $term || 'ticket_replied_product_customer' == $term || 'ticket_replied_product_agent' == $term ) {
10327 $context['pluggable_data']['ticket_product_id'] = $product_id;
10328 } elseif ( 'ticket_closed_product' == $term ) {
10329 $context['pluggable_data']['ticket_product_id'] = $product_id;
10330 $context['pluggable_data']['person_id'] = $person_id;
10331 }
10332 if ( is_object( $ticket_data ) ) {
10333 $ticket_data = get_object_vars( $ticket_data );
10334 }
10335 $ticket = \FluentSupport\App\Models\Ticket::find( $ticket_data['id'] );
10336 if ( is_object( $ticket ) && method_exists( $ticket, 'customData' ) ) {
10337 $context['pluggable_data']['custom_fields'] = $ticket->customData();
10338 }
10339 $context['pluggable_data']['ticket_link'] = admin_url( "admin.php?page=fluent-support#/tickets/{$ticket_data['id']}/view" );
10340 $context['response_type'] = 'live';
10341 }
10342
10343 return $context;
10344 }
10345
10346 /**
10347 * Prepare Ultimate Member user_roles.
10348 *
10349 * @param array $data data.
10350 *
10351 * @return array
10352 */
10353 public function search_um_user_roles( $data ) {
10354 if ( function_exists( 'get_editable_roles' ) ) {
10355 $roles = get_editable_roles();
10356 } else {
10357 $roles = wp_roles()->roles;
10358 $roles = apply_filters( 'editable_roles', $roles );
10359 }
10360
10361 $options = [];
10362 foreach ( $roles as $role => $details ) {
10363
10364 $options[] = [
10365 'label' => $details['name'],
10366 'value' => $role,
10367 ];
10368
10369 }
10370
10371 return [
10372 'options' => $options,
10373 'hasMore' => false,
10374 ];
10375 }
10376
10377 /**
10378 * Prepare Ultimate Member forms_list.
10379 *
10380 * @param array $data data.
10381 *
10382 * @return array
10383 */
10384 public function search_um_forms_list( $data ) {
10385
10386 $page = $data['page'];
10387 $limit = Utilities::get_search_page_limit();
10388 $offset = $limit * ( $page - 1 );
10389
10390 $args = [
10391 'posts_per_page' => $limit,
10392 'offset' => $offset,
10393 'orderby' => 'title',
10394 'order' => 'ASC',
10395 'post_type' => 'um_form',
10396 'post_status' => 'publish',
10397 'fields' => 'ids',
10398 ];
10399 if ( 'register' == $data['filter']['type'] ) {
10400 $args['meta_query'] = [
10401 [
10402 'key' => '_um_mode',
10403 'value' => 'register',
10404 'compare' => 'LIKE',
10405 ],
10406 ];
10407 } elseif ( 'login' == $data['filter']['type'] ) {
10408 $args['meta_query'] = [
10409 [
10410 'key' => '_um_mode',
10411 'value' => 'login',
10412 'compare' => 'LIKE',
10413 ],
10414 ];
10415 }
10416
10417 $forms_list = get_posts( $args );
10418
10419 $forms_list_count = wp_count_posts( 'um_form' )->publish;
10420
10421 $options = [];
10422 if ( ! empty( $forms_list ) ) {
10423 foreach ( $forms_list as $form ) {
10424 $title = html_entity_decode( get_the_title( $form ), ENT_QUOTES, 'UTF-8' );
10425 $options[] = [
10426 'label' => $title,
10427 'value' => $form,
10428 ];
10429 }
10430 }
10431
10432 return [
10433 'options' => $options,
10434 'hasMore' => $forms_list_count > $limit && $forms_list_count > $offset,
10435 ];
10436 }
10437
10438 /**
10439 * Get last data for Ultimate Member Login trigger.
10440 *
10441 * @param array $data data.
10442 * @return mixed
10443 */
10444 public function search_ultimate_member_user_logsin( $data ) {
10445 $context = [];
10446 $args = [
10447 'orderby' => 'meta_value',
10448 'meta_key' => '_um_last_login',
10449 'order' => 'DESC',
10450 'number' => 1,
10451 ];
10452 $users = get_users( $args );
10453
10454 if ( ! empty( $users ) ) {
10455 $user = $users[0];
10456 $submitted_data = get_user_meta( $user->ID, 'submitted', true );
10457 if ( is_array( $submitted_data ) ) {
10458 unset(
10459 $submitted_data['user_password'],
10460 $submitted_data['confirm_user_password']
10461 );
10462 }
10463 $pluggable_data = WordPress::get_user_context( $user->ID );
10464 $pluggable_data['data'] = $submitted_data;
10465 $context['pluggable_data'] = $pluggable_data;
10466 $context['response_type'] = 'live';
10467 } else {
10468 $role = 'subscriber';
10469 $context['pluggable_data'] = [
10470 'wp_user_id' => 1,
10471 'user_login' => 'test',
10472 'display_name' => 'Test User',
10473 'user_firstname' => 'Test',
10474 'user_lastname' => 'User',
10475 'user_email' => 'testuser@gmail.com',
10476 'user_role' => [ $role ],
10477 ];
10478 $context['response_type'] = 'sample';
10479 }
10480 return $context;
10481 }
10482
10483 /**
10484 * Get last data for Ultimate Member Register trigger.
10485 *
10486 * @param array $data data.
10487 * @return mixed
10488 */
10489 public function search_ultimate_member_user_registers( $data ) {
10490 $context = [];
10491 $args = [
10492 'orderby' => 'meta_value',
10493 'meta_key' => 'um_user_profile_url_slug_user_login',
10494 'order' => 'DESC',
10495 'number' => 1,
10496 ];
10497 $users = get_users( $args );
10498
10499 if ( ! empty( $users ) ) {
10500 $user = $users[0];
10501 $submitted_data = get_user_meta( $user->ID, 'submitted', true );
10502 if ( is_array( $submitted_data ) ) {
10503 unset(
10504 $submitted_data['user_password'],
10505 $submitted_data['confirm_user_password']
10506 );
10507 }
10508 $pluggable_data = WordPress::get_user_context( $user->ID );
10509 $pluggable_data['data'] = $submitted_data;
10510 $context['pluggable_data'] = $pluggable_data;
10511 $context['response_type'] = 'live';
10512 } else {
10513 $role = 'subscriber';
10514 $context['pluggable_data'] = [
10515 'wp_user_id' => 1,
10516 'user_login' => 'test',
10517 'display_name' => 'Test User',
10518 'user_firstname' => 'Test',
10519 'user_lastname' => 'User',
10520 'user_email' => 'testuser@gmail.com',
10521 'user_role' => [ $role ],
10522 ];
10523 $context['response_type'] = 'sample';
10524 }
10525 return $context;
10526 }
10527
10528 /**
10529 * Get last data for Ultimate Member Register trigger.
10530 *
10531 * @param array $data data.
10532 * @return mixed
10533 */
10534 public function search_ultimate_member_user_inactive( $data ) {
10535 $context = [];
10536 $args = [
10537 'orderby' => 'user_id',
10538 'meta_key' => 'account_status',
10539 'meta_value' => 'inactive',
10540 'order' => 'ASC',
10541 'number' => 1,
10542 ];
10543 $users = get_users( $args );
10544
10545 if ( ! empty( $users ) ) {
10546 $user = $users[0];
10547 $pluggable_data = [];
10548 $pluggable_data[] = WordPress::get_user_context( $user->ID );
10549 $pluggable_data['user_account_status'] = 'inactive';
10550 $context['pluggable_data'] = $pluggable_data;
10551 $context['response_type'] = 'live';
10552 } else {
10553 $role = 'subscriber';
10554 $context['pluggable_data'] = [
10555 'wp_user_id' => 1,
10556 'user_login' => 'test',
10557 'display_name' => 'Test User',
10558 'user_firstname' => 'Test',
10559 'user_lastname' => 'User',
10560 'user_email' => 'testuser@gmail.com',
10561 'user_role' => [ $role ],
10562 'user_account_status' => 'inactive',
10563 ];
10564 $context['response_type'] = 'sample';
10565 }
10566 return $context;
10567 }
10568
10569 /**
10570 * Get last data for Ultimate Member Change Role trigger.
10571 *
10572 * @param array $data data.
10573 * @return mixed
10574 */
10575 public function search_ultimate_member_user_role_change( $data ) {
10576 $context = [];
10577
10578 $role = $data['filter']['role']['value'];
10579
10580 $args = [
10581 'number' => 1,
10582 'role' => $role,
10583 ];
10584 $users = get_users( $args );
10585 shuffle( $users );
10586 if ( ! empty( $users ) ) {
10587 $user = $users[0];
10588 $pluggable_data = WordPress::get_user_context( $user->ID );
10589 $context['pluggable_data'] = $pluggable_data;
10590 $context['response_type'] = 'live';
10591 } else {
10592 $role = isset( $args['role'] ) ? $args['role'] : 'subscriber';
10593 $context['pluggable_data'] = [
10594 'wp_user_id' => 1,
10595 'user_login' => 'test',
10596 'display_name' => 'Test User',
10597 'user_firstname' => 'Test',
10598 'user_lastname' => 'User',
10599 'user_email' => 'testuser@gmail.com',
10600 'user_role' => [ $role ],
10601 'user_account_status' => 'inactive',
10602 ];
10603 $context['response_type'] = 'sample';
10604 }
10605 return $context;
10606 }
10607
10608 /**
10609 * Get JetEngine WP Posttypes.
10610 *
10611 * @param array $data data.
10612 *
10613 * @return array
10614 */
10615 public function search_je_posttype_list( $data ) {
10616
10617 $post_types = get_post_types( [ 'public' => true ], 'objects' );
10618 $post_types = apply_filters( 'suretriggers_post_types', $post_types );
10619 if ( isset( $post_types['attachment'] ) ) {
10620 unset( $post_types['attachment'] );
10621 }
10622
10623 $options = [];
10624 foreach ( $post_types as $post_type => $details ) {
10625 $options[] = [
10626 'label' => $details->label,
10627 'value' => $post_type,
10628 ];
10629 }
10630
10631 return [
10632 'options' => $options,
10633 'hasMore' => false,
10634 ];
10635 }
10636
10637 /**
10638 * Get JetEngine WP fields.
10639 *
10640 * @param array $data data.
10641 *
10642 * @return array
10643 */
10644 public function search_je_field_list( $data ) {
10645
10646 $post_type = $data['dynamic'];
10647
10648 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
10649
10650 $post_fields = array_filter(
10651 $metaboxes,
10652 function( $metabox ) {
10653 /**
10654 *
10655 * Ignore line
10656 *
10657 * @phpstan-ignore-next-line
10658 */
10659 return 'post' === $metabox['args']['object_type'];
10660 }
10661 );
10662
10663 $post_fields_count = count( $post_fields );
10664
10665 $options = [];
10666 if ( ! empty( $post_fields ) ) {
10667 if ( is_array( $post_fields ) ) {
10668 foreach ( $post_fields as $post_field ) {
10669 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
10670 foreach ( $post_field['meta_fields'] as $meta_field ) {
10671 $options[] = [
10672 'label' => $meta_field['title'],
10673 'value' => $meta_field['name'],
10674 ];
10675 }
10676 }
10677 }
10678 }
10679 }
10680 return [
10681 'options' => $options,
10682 'hasMore' => false,
10683 ];
10684 }
10685
10686 /**
10687 * Search Last Updated Field Data.
10688 *
10689 * @param array $data data.
10690 * @return array
10691 */
10692 public function search_jet_engine_field_data( $data ) {
10693 global $wpdb;
10694
10695 $context = [];
10696
10697 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
10698
10699 $post_type = $data['filter']['wp_post_type']['value'];
10700
10701 if ( -1 === $field ) {
10702 $metaboxes = (array) get_option( 'jet_engine_meta_boxes', [] );
10703
10704 $post_fields = array_filter(
10705 $metaboxes,
10706 function( $metabox ) {
10707 /**
10708 *
10709 * Ignore line
10710 *
10711 * @phpstan-ignore-next-line
10712 */
10713 return 'post' === $metabox['args']['object_type'];
10714 }
10715 );
10716
10717 $options = [];
10718 if ( ! empty( $post_fields ) ) {
10719 if ( is_array( $post_fields ) ) {
10720 foreach ( $post_fields as $post_field ) {
10721 if ( in_array( $post_type, $post_field['args']['allowed_post_type'], true ) ) {
10722 foreach ( $post_field['meta_fields'] as $meta_field ) {
10723 $options[] = $meta_field['name'];
10724 }
10725 }
10726 }
10727 }
10728 }
10729 $random_key = array_rand( $options );
10730 $random_value = $options[ $random_key ];
10731 $string = '%' . $random_value . '%';
10732 } else {
10733 $string = '%' . $data['filter']['field_id']['value'] . '%';
10734 }
10735
10736 $result = $wpdb->get_results(
10737 $wpdb->prepare(
10738 'SELECT post_id FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key LIKE %s',
10739 [ $string ]
10740 ),
10741 ARRAY_A
10742 );
10743
10744 $ids = [];
10745
10746 if ( ! empty( $result ) ) {
10747 foreach ( $result as $val ) {
10748 $ids[] = $val['post_id'];
10749 }
10750 }
10751
10752 $lastupdated_args = [
10753 'post_type' => $post_type,
10754 'orderby' => 'modified',
10755 'post__in' => $ids,
10756 'posts_per_page' => 1,
10757 ];
10758 $lastupdated_loop = get_posts( $lastupdated_args );
10759
10760 $response = [];
10761 if ( ! empty( $result ) ) {
10762 $context['post'] = $lastupdated_loop[0];
10763
10764 $meta_value = get_post_meta( $lastupdated_loop[0]->ID, sprintf( '%s', $data['filter']['field_id']['value'] ), true );
10765 $meta_key = sprintf( '%s', $data['filter']['field_id']['value'] );
10766
10767 $context[ $meta_key ] = $meta_value;
10768
10769 $response['pluggable_data'] = $context;
10770 $response['response_type'] = 'live';
10771 } else {
10772 $response = json_decode( '{"response_type":"sample","pluggable_data":{"post":{"ID":198,"post_author":"1","post_date":"2023-02-08 13:31:13","post_date_gmt":"2023-02-08 13:31:13","post_content":"New Category1 - content","post_title":"jennjennn - Post - jenn","post_excerpt":"","post_status":"publish","comment_status":"open","ping_status":"open","post_password":"","post_name":"jennjennn-post-jenn","to_ping":"","pinged":"","post_modified":"2023-04-10 06:23:40","post_modified_gmt":"2023-04-10 06:23:40","post_content_filtered":"","post_parent":0,"guid":"https:\/\/examplewp.local\/jennjennn-post-jenn\/","menu_order":0,"post_type":"post","post_mime_type":"","comment_count":"0","filter":"raw"},"enter-post-extra-content-title":"dummy"}}', true );
10773 }
10774
10775 return $response;
10776
10777 }
10778
10779 /**
10780 * Get Formidable Forms.
10781 *
10782 * @param array $data data.
10783 *
10784 * @return array|void
10785 */
10786 public function search_formidable_form_list( $data ) {
10787 if ( ! class_exists( 'FrmForm' ) ) {
10788 return;
10789 }
10790
10791 $page = $data['page'];
10792 $term = $data['search_term'];
10793 $limit = Utilities::get_search_page_limit();
10794 $offset = $limit * ( $page - 1 );
10795 $per_page = 10;
10796
10797 $query = [
10798 [
10799 'or' => 1,
10800 'name LIKE' => $term,
10801 'parent_form_id' => null,
10802 ],
10803 ];
10804 $query['is_template'] = 0;
10805 $query['status !'] = 'trash';
10806 $forms_list = FrmForm::getAll( $query, '', $offset . ',' . $per_page );
10807 $form_count = FrmForm::getAll( $query );
10808 $form_count = count( $form_count );
10809 $options = [];
10810
10811 if ( ! empty( $forms_list ) ) {
10812 if ( is_array( $forms_list ) ) {
10813 foreach ( $forms_list as $form ) {
10814 $options[] = [
10815 'label' => $form->name,
10816 'value' => $form->id,
10817 ];
10818 }
10819 }
10820 }
10821
10822 return [
10823 'options' => $options,
10824 'hasMore' => $form_count > $limit && $form_count > $offset,
10825 ];
10826 }
10827
10828 /**
10829 * Get JetFormBuilder Form List.
10830 *
10831 * @param array $data data.
10832 *
10833 * @return array|void
10834 */
10835 public function search_jetform_list( $data ) {
10836 if ( ! class_exists( '\Jet_Form_Builder\Classes\Tools' ) ) {
10837 return;
10838 }
10839
10840 $forms = \Jet_Form_Builder\Classes\Tools::get_forms_list_for_js();
10841
10842 $options = [];
10843 foreach ( $forms as $form ) {
10844
10845 if ( ! empty( $form['value'] ) ) {
10846 $options[] = [
10847 'label' => esc_html( $form['label'] ),
10848 'value' => esc_attr( $form['value'] ),
10849 ];
10850 }
10851 }
10852
10853 return [
10854 'options' => $options,
10855 'hasMore' => false,
10856 ];
10857 }
10858
10859 /**
10860 * Get Forminator Form List.
10861 *
10862 * @param array $data data.
10863 *
10864 * @return array|void
10865 */
10866 public function search_forminator_form_list( $data ) {
10867 if ( ! class_exists( 'Forminator_API' ) ) {
10868 return;
10869 }
10870
10871 $forms = Forminator_API::get_forms( null, 1, 10 );
10872
10873 $options = [];
10874 foreach ( $forms as $form ) {
10875 $options[] = [
10876 'label' => isset( $form->settings ) && isset( $form->settings['form_name'] ) ? $form->settings['form_name'] : $form->name,
10877 'value' => $form->id,
10878 ];
10879 }
10880
10881 return [
10882 'options' => $options,
10883 'hasMore' => false,
10884 ];
10885 }
10886
10887
10888 /**
10889 * Get BbPress topics list.
10890 *
10891 * @param array $data data.
10892 *
10893 * @return array|void
10894 */
10895 public function search_bbp_topic_list( $data ) {
10896 $page = $data['page'];
10897 $forum_id = $data['dynamic'];
10898 $limit = Utilities::get_search_page_limit();
10899 $offset = $limit * ( $page - 1 );
10900 $args = [
10901 'post_type' => 'topic',
10902 'offset' => $offset,
10903 'meta_query' => [
10904 [
10905 'key' => '_bbp_forum_id',
10906 'value' => $forum_id,
10907 'compare' => '==',
10908 ],
10909 ],
10910 ];
10911
10912 $topics = get_posts( $args );
10913 $topics_count = wp_count_posts( 'topic' )->publish;
10914
10915 $options = [];
10916 if ( ! empty( $topics ) ) {
10917 if ( is_array( $topics ) ) {
10918 foreach ( $topics as $topic ) {
10919 $options[] = [
10920 'label' => $topic->post_title,
10921 'value' => $topic->ID,
10922 ];
10923 }
10924 }
10925 }
10926 return [
10927 'options' => $options,
10928 'hasMore' => $topics_count > $limit && $topics_count > $offset,
10929 ];
10930 }
10931
10932
10933 /**
10934 * Search Last Updated Field Data.
10935 *
10936 * @param array $data data.
10937 * @return array
10938 */
10939 public function search_bbp_last_data( $data ) {
10940 global $wpdb;
10941
10942 $post_type = $data['post_type'];
10943 $trigger = $data['search_term'];
10944 $context = [];
10945
10946 if ( 'topic' === $post_type ) {
10947 $post_id = $data['filter']['forum']['value'];
10948 if ( -1 === $post_id ) {
10949 $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" );
10950
10951 } else {
10952 $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 ) );
10953
10954 }
10955 } elseif ( 'reply' === $post_type ) {
10956 $post_id = $data['filter']['topic']['value'];
10957
10958 if ( -1 === $post_id ) {
10959 $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" );
10960
10961 } else {
10962 $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 ) );
10963 }
10964 }
10965
10966
10967 $response = [];
10968 if ( ! empty( $result ) ) {
10969 if ( 'bbpress_topic_created' === $trigger ) {
10970 $topic_id = $result[0]->post_id;
10971 $forum_id = $result[0]->meta_value;
10972 $topic = get_the_title( $topic_id );
10973 $topic_link = get_the_permalink( $topic_id );
10974 $topic_description = get_the_content( null, false, $topic_id );
10975 $topic_status = get_post_status( $topic_id );
10976
10977 $forum = get_the_title( $forum_id );
10978 $forum_link = get_the_permalink( $forum_id );
10979 $forum_description = get_the_content( null, false, $forum_id );
10980 $forum_status = get_post_status( $forum_id );
10981
10982 $forum = [
10983 'forum' => $forum_id,
10984 'forum_title' => $forum,
10985 'forum_link' => $forum_link,
10986 'forum_description' => $forum_description,
10987 'forum_status' => $forum_status,
10988 ];
10989
10990 $topic = [
10991 'topic' => $topic_id,
10992 'topic_title' => $topic,
10993 'topic_link' => $topic_link,
10994 'topic_description' => $topic_description,
10995 'topic_status' => $topic_status,
10996 ];
10997
10998 $user_id = $result[0]->post_author;
10999 if ( '0' != $user_id ) {
11000 $context = array_merge(
11001 WordPress::get_user_context( $user_id ),
11002 $forum,
11003 $topic
11004 );
11005 } else {
11006 $anonymous_data = [
11007 'bbp_anonymous_name' => get_post_meta( $result[0]->ID, '_bbp_anonymous_name', true ),
11008 'bbp_anonymous_email' => get_post_meta( $result[0]->ID, '_bbp_anonymous_email', true ),
11009 'bbp_anonymous_website' => get_post_meta( $result[0]->ID, '_bbp_anonymous_website', true ),
11010 ];
11011 $context = array_merge(
11012 $anonymous_data,
11013 $forum,
11014 $topic
11015 );
11016 }
11017
11018 $response['pluggable_data'] = $context;
11019 $response['response_type'] = 'live';
11020 } else {
11021 $reply_id = $result[0]->post_id;
11022 $topic_id = $result[0]->meta_value;
11023 $forum_id = get_post_meta( $topic_id, '_bbp_forum_id', true );
11024 $forum_id = intval( '"' . $forum_id . '"' );
11025 $reply = get_the_title( $reply_id );
11026 $reply_link = get_the_permalink( $reply_id );
11027 $reply_description = get_the_content( null, false, $reply_id );
11028 $reply_status = get_post_status( $reply_id );
11029
11030
11031 $topic = get_the_title( $topic_id );
11032 $topic_link = get_the_permalink( $topic_id );
11033 $topic_description = get_the_content( null, false, $topic_id );
11034 $topic_status = get_post_status( $topic_id );
11035
11036 $forum = get_the_title( $forum_id );
11037 $forum_link = get_the_permalink( $forum_id );
11038 $forum_description = get_the_content( null, false, $forum_id );
11039 $forum_status = get_post_status( $forum_id );
11040
11041 $forum = [
11042 'forum' => $forum_id,
11043 'forum_title' => $forum,
11044 'forum_link' => $forum_link,
11045 'forum_description' => $forum_description,
11046 'forum_status' => $forum_status,
11047 ];
11048
11049 $topic = [
11050 'topic' => $topic_id,
11051 'topic_title' => $topic,
11052 'topic_link' => $topic_link,
11053 'topic_description' => $topic_description,
11054 'topic_status' => $topic_status,
11055 ];
11056
11057 $reply = [
11058 'reply_title' => $reply,
11059 'reply_link' => $reply_link,
11060 'reply_description' => $reply_description,
11061 'reply_status' => $reply_status,
11062 ];
11063 $user_id = $result[0]->post_author;
11064 if ( '0' != $user_id ) {
11065 $context = array_merge(
11066 WordPress::get_user_context( $user_id ),
11067 $forum,
11068 $topic,
11069 $reply
11070 );
11071 } else {
11072 $anonymous_data = [
11073 'bbp_anonymous_name' => get_post_meta( $result[0]->ID, '_bbp_anonymous_name', true ),
11074 'bbp_anonymous_email' => get_post_meta( $result[0]->ID, '_bbp_anonymous_email', true ),
11075 'bbp_anonymous_website' => get_post_meta( $result[0]->ID, '_bbp_anonymous_website', true ),
11076 ];
11077 $context = array_merge(
11078 $anonymous_data,
11079 $forum,
11080 $topic,
11081 $reply
11082 );
11083 }
11084
11085 $response['pluggable_data'] = $context;
11086 $response['response_type'] = 'live';
11087 }
11088 }
11089
11090 return $response;
11091 }
11092
11093 /**
11094 * Search Last Updated Field Data.
11095 *
11096 * @param array $data data.
11097 * @return array|void
11098 */
11099 public function search_happyform_list( $data ) {
11100 if ( ! function_exists( 'happyforms_get_form_controller' ) ) {
11101 return;
11102 }
11103
11104 $form_controller = happyforms_get_form_controller();
11105
11106 $forms = $form_controller->do_get();
11107 $options = [];
11108 if ( ! empty( $forms ) ) {
11109 foreach ( $forms as $form ) {
11110 $options[] = [
11111 'label' => $form['post_title'],
11112 'value' => $form['ID'],
11113 ];
11114 }
11115 }
11116
11117 return [
11118 'options' => $options,
11119 'hasMore' => false,
11120 ];
11121 }
11122
11123 /**
11124 * Get Memberpress Course List.
11125 *
11126 * @param array $data data.
11127 *
11128 * @return array|void
11129 */
11130 public function search_mpc_lessons_list( $data ) {
11131 if ( ! class_exists( '\memberpress\courses\models\Lesson' ) ) {
11132 return;
11133 }
11134 global $wpdb;
11135 $options = [];
11136 $course_id = $data['dynamic'];
11137 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mpcs_sections WHERE course_id =%s", $course_id ) );
11138 $sections = [];
11139 foreach ( $result as $rec ) {
11140 $sections[] = [
11141 'id' => $rec->id,
11142 'title' => $rec->title,
11143 ];
11144 }
11145 if ( is_array( $sections ) && count( $sections ) > 0 ) {
11146 foreach ( $sections as $section ) {
11147 $post_types_string = \memberpress\courses\models\Lesson::lesson_cpts();
11148 $post_types_string = implode( "','", $post_types_string );
11149
11150 $query = $wpdb->prepare(
11151 "SELECT * FROM {$wpdb->posts} AS p
11152 JOIN {$wpdb->postmeta} AS pm
11153 ON p.ID = pm.post_id
11154 AND pm.meta_key = %s
11155 AND pm.meta_value = %s
11156 JOIN {$wpdb->postmeta} AS pm_order
11157 ON p.ID = pm_order.post_id
11158 AND pm_order.meta_key = %s
11159 WHERE p.post_type in ( %s ) AND p.post_status <> 'trash'
11160 ORDER BY pm_order.meta_value * 1",
11161 models\Lesson::$section_id_str,
11162 $section['id'],
11163 models\Lesson::$lesson_order_str,
11164 stripcslashes( $post_types_string )
11165 );
11166
11167 $db_lessons = $wpdb->get_results( stripcslashes( $query ) ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
11168 foreach ( $db_lessons as $lesson ) {
11169 $options[] = [
11170 'label' => $section['title'] . '->' . $lesson->post_title,
11171 'value' => $lesson->ID,
11172 ];
11173 }
11174 }
11175 }
11176 return [
11177 'options' => $options,
11178 'hasMore' => false,
11179 ];
11180 }
11181
11182 /**
11183 * Get Memberpress Course List.
11184 *
11185 * @param array $data data.
11186 *
11187 * @return array|void
11188 */
11189 public function search_gp_rank_type_list( $data ) {
11190 global $wpdb;
11191
11192 $posts = $wpdb->get_results(
11193 "SELECT ID, post_name, post_title, post_type
11194 FROM $wpdb->posts
11195 WHERE post_type LIKE 'rank-type' AND post_status = 'publish' ORDER BY post_title ASC"
11196 );
11197
11198 $posts_count = count( $posts );
11199
11200 $options = [];
11201 if ( $posts ) {
11202 foreach ( $posts as $post ) {
11203 $options[] = [
11204 'label' => $post->post_title,
11205 'value' => $post->post_name,
11206 ];
11207 }
11208 }
11209
11210 return [
11211 'options' => $options,
11212 'hasMore' => false,
11213 ];
11214 }
11215
11216 /**
11217 * Get MPC last data.
11218 *
11219 * @param array $data data.
11220 *
11221 * @return array
11222 */
11223 public function search_mpc_last_data( $data ) {
11224 global $wpdb;
11225 $trigger = $data['search_term'];
11226 $course_data = [];
11227 $lesson_data = [];
11228 $context = [];
11229
11230 if ( 'mpc_course_completed' === $trigger ) {
11231 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course]']['value'] : '-1' );
11232 if ( $course_id > 0 ) {
11233
11234 $course = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $course_id ) );
11235 } else {
11236 $course = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-course' ORDER BY id DESC LIMIT 1" );
11237 }
11238
11239 if ( ! empty( $course ) ) {
11240 $course_data = [
11241 'course_id' => $course->ID,
11242 'course_title' => get_the_title( $course_id ),
11243 'course_url' => get_permalink( $course_id ),
11244 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
11245 'course_featured_image_url' => get_the_post_thumbnail_url( $course_id ),
11246 ];
11247 }
11248 $user_progress = $wpdb->get_row( $wpdb->prepare( "SELECT user_id FROM {$wpdb->prefix}mpcs_user_progress WHERE course_id=%s", $course_id ) );
11249 if ( ! empty( $user_progress ) ) {
11250 $context['response_type'] = 'live';
11251 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
11252 } else {
11253 $sample_data = '{"pluggable_data":{"wp_user_id":1,"user_login":"johnd","display_name":"johnd","user_firstname":"johnd","user_lastname":"johnd","user_email":"hello@example.io","user_role":["administrator","subscriber","tutor_instructor","bbp_keymaster"],"course_id":617,"course_title":"Course One","course_url":"https:\/\/connector.com\/courses\/course-one\/","course_featured_image_id":"","course_featured_image_url":false}
11254 ,"response_type":"sample"} ';
11255 $context = json_decode( $sample_data, true );
11256 }
11257 } elseif ( 'mpc_lesson_completed' === $trigger ) {
11258 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
11259 $course_id = (int) $data['filter']['course']['value'];
11260 if ( $lesson_id > 0 ) {
11261
11262 $lesson = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE ID= %s ORDER BY id DESC LIMIT 1", $lesson_id ) );
11263 } else {
11264 $lesson = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}posts where post_type = 'mpcs-lesson' ORDER BY id DESC LIMIT 1" );
11265 }
11266
11267 if ( ! empty( $lesson ) ) {
11268 $lesson_data = [
11269 'lesson_id' => $lesson->ID,
11270 'lesson_title' => get_the_title( $lesson_id ),
11271 'lesson_url' => get_permalink( $lesson_id ),
11272 'lesson_featured_image_id' => get_post_meta( $lesson_id, '_thumbnail_id', true ),
11273 'lesson_featured_image_url' => get_the_post_thumbnail_url( $lesson_id ),
11274 ];
11275
11276 $lesson_section_id = get_post_meta( $lesson->ID, '_mpcs_lesson_section_id', true );
11277
11278 $section = $wpdb->get_row( $wpdb->prepare( "SELECT course_id FROM {$wpdb->prefix}mpcs_sections WHERE ID= %s", $lesson_section_id ) );
11279
11280 $course_data = [
11281 'course_id' => $course_id,
11282 'course_title' => get_the_title( $course_id ),
11283 'course_url' => get_permalink( $course_id ),
11284 'course_featured_image_id' => get_post_meta( $course_id, '_thumbnail_id', true ),
11285 'course_featured_image_url' => get_the_post_thumbnail_url( $section->course_id ),
11286 ];
11287 }
11288
11289 $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 ) );
11290 if ( ! empty( $user_progress ) ) {
11291 $context['response_type'] = 'live';
11292 $context['pluggable_data'] = array_merge( WordPress::get_user_context( $user_progress->user_id ), $course_data, $lesson_data );
11293 } else {
11294 $sample_data = '{"pluggable_data":{"wp_user_id":1,"user_login":"johnd","display_name":"johnd","user_firstname":"john","user_lastname":"dev","user_email":"johnd@example.com","user_role":["administrator","subscriber","tutor_instructor","bbp_keymaster"],"lesson_id":620,"lesson_title":"second section","lesson_url":"https:\/\/connector.com\/courses\/course-one\/lessons\/second-section\/","lesson_featured_image_id":"","lesson_featured_image_url":false,"course_id":617,"course_title":"Course One","course_url":"https:\/\/connector.com\/courses\/course-one\/","course_featured_image_id":"","course_featured_image_url":false},"response_type":"sample"}';
11295 $context = json_decode( $sample_data, true );
11296 }
11297 }
11298
11299
11300 return $context;
11301 }
11302
11303 /** Get GamiPress Rank List.
11304 *
11305 * @param array $data data.
11306 *
11307 * @return array|void
11308 */
11309 public function search_gp_rank_list( $data ) {
11310 global $wpdb;
11311
11312 $page = $data['page'];
11313 $limit = Utilities::get_search_page_limit();
11314 $offset = $limit * ( $page - 1 );
11315
11316 $args = [
11317 'post_type' => $data['dynamic']['rank_type'],
11318 'posts_per_page' => $limit,
11319 'offset' => $offset,
11320 'orderby' => 'title',
11321 'order' => 'ASC',
11322 'post_status' => 'publish',
11323 's' => $data['search_term'],
11324 ];
11325
11326 $rank_type = get_posts( $args );
11327
11328 $count_args = [
11329 'post_type' => $data['dynamic']['rank_type'],
11330 'posts_per_page' => -1,
11331 'post_status' => 'publish',
11332 's' => $data['search_term'],
11333 ];
11334
11335 $rank_posts = get_posts( $count_args );
11336 $rank_type_count = count( $rank_posts );
11337
11338 $options = [];
11339 if ( $rank_type ) {
11340 foreach ( $rank_type as $rank ) {
11341 $options[] = [
11342 'label' => $rank->post_title,
11343 'value' => $rank->ID,
11344 ];
11345 }
11346 }
11347
11348 return [
11349 'options' => $options,
11350 'hasMore' => $rank_type_count > $limit && $rank_type_count > $offset,
11351 ];
11352 }
11353
11354 /**
11355 * Get GamiPress PointType List.
11356 *
11357 * @param array $data data.
11358 *
11359 * @return array|void
11360 */
11361 public function search_gp_point_type_list( $data ) {
11362 global $wpdb;
11363
11364 $page = $data['page'];
11365 $limit = Utilities::get_search_page_limit();
11366 $offset = $limit * ( $page - 1 );
11367
11368 $args = [
11369 'post_type' => 'points-type',
11370 'posts_per_page' => $limit,
11371 'offset' => $offset,
11372 'orderby' => 'title',
11373 'order' => 'ASC',
11374 'post_status' => 'publish',
11375 's' => $data['search_term'],
11376 ];
11377
11378 $point_type = get_posts( $args );
11379
11380 $count_args = [
11381 'post_type' => 'points-type',
11382 'posts_per_page' => -1,
11383 'post_status' => 'publish',
11384 's' => $data['search_term'],
11385 ];
11386
11387 $count_point_type = get_posts( $count_args );
11388 $point_type_count = count( $count_point_type );
11389
11390 $options = [];
11391 if ( $point_type ) {
11392 foreach ( $point_type as $point ) {
11393 $options[] = [
11394 'label' => $point->post_title,
11395 'value' => $point->ID,
11396 ];
11397 }
11398 }
11399
11400 return [
11401 'options' => $options,
11402 'hasMore' => $point_type_count > $limit && $point_type_count > $offset,
11403 ];
11404 }
11405
11406 /**
11407 * Get GamiPress AchievementType List.
11408 *
11409 * @param array $data data.
11410 *
11411 * @return array|void
11412 */
11413 public function search_gp_achivement_type_list( $data ) {
11414 global $wpdb;
11415
11416 $posts = $wpdb->get_results(
11417 "SELECT ID, post_name, post_title, post_type
11418 FROM $wpdb->posts
11419 WHERE post_type LIKE 'achievement-type' AND post_status = 'publish' ORDER BY post_title ASC"
11420 );
11421
11422 $posts_count = count( $posts );
11423
11424 $options = [];
11425 if ( $posts ) {
11426 foreach ( $posts as $post ) {
11427 $options[] = [
11428 'label' => $post->post_title,
11429 'value' => $post->post_name,
11430 ];
11431 }
11432 }
11433
11434 $options[] = [
11435 'label' => 'Points awards',
11436 'value' => 'points-award',
11437 ];
11438 $options[] = [
11439 'label' => 'Step',
11440 'value' => 'step',
11441 ];
11442 $options[] = [
11443 'label' => 'Rank requirement',
11444 'value' => 'rank-requirement',
11445 ];
11446
11447 return [
11448 'options' => $options,
11449 'hasMore' => false,
11450 ];
11451 }
11452
11453 /**
11454 * Get GamiPress Award List.
11455 *
11456 * @param array $data data.
11457 *
11458 * @return array|void
11459 */
11460 public function search_gp_award_list( $data ) {
11461 global $wpdb;
11462
11463 $page = $data['page'];
11464 $limit = Utilities::get_search_page_limit();
11465 $offset = $limit * ( $page - 1 );
11466
11467 $args = [
11468 'post_type' => $data['dynamic']['achivement_type'],
11469 'posts_per_page' => $limit,
11470 'offset' => $offset,
11471 'orderby' => 'title',
11472 'order' => 'ASC',
11473 'post_status' => 'publish',
11474 's' => $data['search_term'],
11475 ];
11476
11477 $award_type = get_posts( $args );
11478 $count_args = [
11479 'post_type' => $data['dynamic']['achivement_type'],
11480 'posts_per_page' => -1,
11481 'post_status' => 'publish',
11482 's' => $data['search_term'],
11483 ];
11484
11485 $count_award_type = get_posts( $count_args );
11486 $award_type_count = count( $count_award_type );
11487 $options = [];
11488 if ( $award_type ) {
11489 foreach ( $award_type as $award ) {
11490 $options[] = [
11491 'label' => $award->post_title,
11492 'value' => $award->ID,
11493 ];
11494 }
11495 }
11496
11497 return [
11498 'options' => $options,
11499 'hasMore' => $award_type_count > $limit && $award_type_count > $offset,
11500 ];
11501 }
11502
11503 /**
11504 * Get Woocommerce Subscription Product List.
11505 *
11506 * @param array $data data.
11507 *
11508 * @return array|void
11509 */
11510 public function search_wc_subscription_product_list( $data ) {
11511 global $wpdb;
11512
11513 $subscriptions = $wpdb->get_results(
11514 $wpdb->prepare(
11515 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
11516 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
11517 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
11518 AND posts.post_type = %s
11519 AND posts.post_status = %s
11520 UNION ALL
11521 SELECT ID, post_title FROM $wpdb->posts
11522 WHERE post_type = %s
11523 AND post_status = %s
11524 ORDER BY post_title",
11525 'product',
11526 'publish',
11527 'shop_subscription',
11528 'publish'
11529 )
11530 );
11531 $page = $data['page'];
11532 $limit = Utilities::get_search_page_limit();
11533 $offset = $limit * ( $page - 1 );
11534 $subscriptions_products = [];
11535 $subscription_products_count = 10;
11536 if ( function_exists( 'wc_get_products' ) ) {
11537 $product_query = wc_get_products(
11538 [
11539 'type' => [ 'subscription', 'variable-subscription' ],
11540 'posts_per_page' => $limit,
11541 'offset' => $offset,
11542 'paginate' => true,
11543 ]
11544 );
11545 if ( is_object( $product_query ) ) {
11546 $subscriptions_products = $product_query->products;
11547 $subscription_products_count = $product_query->total;
11548 }
11549 }
11550
11551 $options = [];
11552 if ( $subscriptions ) {
11553 foreach ( $subscriptions as $post ) {
11554 $options[] = [
11555 'label' => $post->post_title,
11556 'value' => $post->ID,
11557 ];
11558 }
11559 return [
11560 'options' => $options,
11561 'hasMore' => false,
11562 ];
11563 } elseif ( $subscriptions_products ) {
11564 foreach ( $subscriptions_products as $product ) {
11565 $title = $product->get_name();
11566 $options[] = [
11567 'label' => ! empty( $title ) ? $title : $product->get_id(),
11568 'value' => $product->get_id(),
11569 ];
11570 }
11571 return [
11572 'options' => $options,
11573 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
11574 ];
11575 } else {
11576 return [
11577 'options' => $options,
11578 'hasMore' => false,
11579 ];
11580 }
11581 }
11582
11583 /**
11584 * Get Woocommerce Subscriptions Variation list.
11585 *
11586 * @param array $data data.
11587 *
11588 * @return array|void
11589 */
11590 public function search_wc_variable_subscription_list( $data ) {
11591 global $wpdb;
11592
11593 $page = $data['page'];
11594 $limit = Utilities::get_search_page_limit();
11595 $offset = $limit * ( $page - 1 );
11596
11597 if ( ! function_exists( 'wc_get_products' ) ) {
11598 return;
11599 }
11600 $subscription_products = wc_get_products(
11601 [
11602 'type' => [ 'variable-subscription' ],
11603 'posts_per_page' => $limit,
11604 'offset' => $offset,
11605 'orderby' => 'date',
11606 'order' => 'DESC',
11607 ]
11608 );
11609
11610 $subscription_products_count = count( (array) $subscription_products );
11611
11612 $options = [];
11613 if ( $subscription_products ) {
11614 foreach ( (array) $subscription_products as $product ) {
11615 $options[] = [
11616 'label' => $product->get_title(),
11617 'value' => $product->get_id(),
11618 ];
11619 }
11620 }
11621
11622 return [
11623 'options' => $options,
11624 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
11625 ];
11626 }
11627
11628 /**
11629 * Get Woocommerce Variation list.
11630 *
11631 * @param array $data data.
11632 *
11633 * @return array|void
11634 */
11635 public function search_wc_variation_list( $data ) {
11636 global $wpdb;
11637
11638 $page = $data['page'];
11639 $limit = Utilities::get_search_page_limit();
11640 $offset = $limit * ( $page - 1 );
11641
11642 $args = [
11643 'post_type' => 'product_variation',
11644 'post_parent' => $data['dynamic']['variable_subscription'],
11645 'posts_per_page' => $limit,
11646 'offset' => $offset,
11647 'orderby' => 'ID',
11648 'order' => 'ASC',
11649 'post_status' => 'publish',
11650 ];
11651
11652 $variation = get_posts( $args );
11653 $variation_count = count( $variation );
11654
11655 $options = [];
11656 if ( $variation ) {
11657 foreach ( $variation as $product ) {
11658 $options[] = [
11659 'label' => ! empty( $product->post_excerpt ) ? $product->post_excerpt : $product->post_title,
11660 'value' => $product->ID,
11661 ];
11662 }
11663 }
11664
11665 return [
11666 'options' => $options,
11667 'hasMore' => $variation_count > $limit && $variation_count > $offset,
11668 ];
11669 }
11670
11671 /**
11672 * Get Membership List.
11673 *
11674 * @param array $data data.
11675 *
11676 * @return array
11677 */
11678 public function search_membership_list( $data ) {
11679 global $wpdb;
11680
11681 $levels = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}pmpro_membership_levels ORDER BY id ASC" );
11682 $options = [];
11683 if ( $levels ) {
11684 foreach ( $levels as $level ) {
11685 $options[] = [
11686 'label' => $level->name,
11687 'value' => $level->id,
11688 ];
11689 }
11690 }
11691
11692 return [
11693 'options' => $options,
11694 'hasMore' => false,
11695 ];
11696 }
11697
11698 /**
11699
11700 * Get EventsManager last data.
11701 *
11702 * @param array $data data.
11703 *
11704 * @return array
11705 */
11706 public function search_events_manager_data( $data ) {
11707 global $wpdb;
11708 $trigger = $data['search_term'];
11709 $context = [];
11710
11711 $post_id = (int) ( isset( $data['filter']['post_id']['value'] ) ? $data['filter']['post_id']['value'] : '-1' );
11712 if ( 'em_user_register_in_event' === $trigger ) {
11713 if ( $post_id > 0 ) {
11714 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11715 $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 ) );
11716 } else {
11717 $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" );
11718
11719 }
11720
11721 if ( ! empty( $all_bookings ) ) {
11722 $user_id = $all_bookings->person_id;
11723 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
11724 $context['pluggable_data'] = array_merge(
11725 WordPress::get_user_context( $user_id ),
11726 json_decode( wp_json_encode( $all_bookings ), true )
11727 );
11728 if ( ! empty( $location ) ) {
11729 $context['pluggable_data'] = array_merge( $context['pluggable_data'], json_decode( wp_json_encode( $location ), true ) );
11730 }
11731
11732 $context['response_type'] = 'live';
11733 }
11734 } elseif ( 'em_user_unregister_from_event' === $trigger ) {
11735
11736 if ( $post_id > 0 ) {
11737 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11738 $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 ) );
11739 } else {
11740 $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" );
11741
11742 }
11743
11744 if ( ! empty( $all_bookings ) ) {
11745 $user_id = $all_bookings->person_id;
11746 $context['pluggable_data'] = array_merge(
11747 WordPress::get_user_context( $user_id ),
11748 json_decode( wp_json_encode( $all_bookings ), true )
11749 );
11750 $context['response_type'] = 'live';
11751 }
11752 } elseif ( 'em_user_booking_approved' === $trigger ) {
11753
11754 if ( $post_id > 0 ) {
11755 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11756 $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 ) );
11757 } else {
11758 $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" );
11759
11760 }
11761
11762 if ( ! empty( $all_bookings ) ) {
11763 $user_id = $all_bookings->person_id;
11764 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
11765 $context['pluggable_data'] = array_merge(
11766 WordPress::get_user_context( $user_id ),
11767 json_decode( wp_json_encode( $all_bookings ), true )
11768 );
11769 if ( ! empty( $location ) ) {
11770 $context['pluggable_data'] = array_merge( $context['pluggable_data'], json_decode( wp_json_encode( $location ), true ) );
11771 }
11772
11773 $context['response_type'] = 'live';
11774
11775 }
11776 } elseif ( 'em_user_registers_event_with_specific_ticket' === $trigger ) {
11777 if ( $post_id > 0 ) {
11778 $event_id_id = get_post_meta( $post_id, '_event_id', true );
11779 $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 ) );
11780 }
11781 if ( ! empty( $all_bookings ) ) {
11782 $ticket_id = (int) $data['filter']['ticket_id']['value'];
11783 $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 ) );
11784 if ( ! empty( $all_ticket_bookings ) ) {
11785 $user_id = $all_bookings->person_id;
11786 $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations as b WHERE b.location_id = %s", $all_bookings->location_id ) );
11787 $bookings_str = wp_json_encode( $all_bookings );
11788 $ticket_bookings_str = wp_json_encode( $all_ticket_bookings );
11789
11790 $context['pluggable_data'] = array_merge(
11791 WordPress::get_user_context( $user_id )
11792 );
11793 if ( is_string( $bookings_str ) && is_string( $ticket_bookings_str ) ) {
11794 $bookings_arr = json_decode( $bookings_str, true );
11795 $ticket_bookings_arr = json_decode( $ticket_bookings_str, true );
11796 if ( is_array( $bookings_arr ) && is_array( $ticket_bookings_arr ) ) {
11797 $context['pluggable_data'] = array_merge(
11798 $context['pluggable_data'],
11799 $bookings_arr,
11800 $ticket_bookings_arr
11801 );
11802 }
11803 }
11804 if ( ! empty( $location ) ) {
11805 $location_str = wp_json_encode( $location );
11806 if ( is_string( $location_str ) ) {
11807 $location_arr = json_decode( $location_str, true );
11808 if ( is_array( $location_arr ) ) {
11809 $context['pluggable_data'] = array_merge( $context['pluggable_data'], $location_arr );
11810 }
11811 }
11812 }
11813 $context['response_type'] = 'live';
11814 }
11815 }
11816 }
11817 return $context;
11818 }
11819
11820 /**
11821 * Get Events Manager Events Ticket list.
11822 *
11823 * @param array $data data.
11824 *
11825 * @return array|void
11826 */
11827 public function search_em_event_tickets( $data ) {
11828 global $wpdb;
11829
11830 $options = [];
11831
11832 $event = $data['dynamic']['post_id'];
11833
11834 $event_id = $wpdb->get_var( $wpdb->prepare( "SELECT event_id FROM {$wpdb->prefix}em_events WHERE post_id = %d", $event ) );
11835 $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] ) );
11836
11837 if ( $tickets ) {
11838 foreach ( $tickets as $ticket ) {
11839 $options[] = [
11840 'label' => $ticket->ticket_name,
11841 'value' => $ticket->ticket_id,
11842 ];
11843 }
11844 }
11845
11846 return [
11847 'options' => $options,
11848 'hasMore' => false,
11849 ];
11850 }
11851
11852 /**
11853
11854 * Get learnpress last data.
11855 *
11856 * @param array $data data.
11857 *
11858 * @return array
11859 */
11860 public function search_learnpress_lms_last_data( $data ) {
11861 global $wpdb;
11862 $trigger = $data['search_term'];
11863 $course_data = [];
11864 $lesson_data = [];
11865 $context = [];
11866
11867
11868 if ( 'learnpress_course_completed' === $trigger ) {
11869 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
11870 if ( $course_id > 0 ) {
11871
11872 $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 ) );
11873 } else {
11874
11875 $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" );
11876 }
11877
11878 if ( ! empty( $course ) ) {
11879 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
11880 $context['response_type'] = 'live';
11881 $context['pluggable_data'] = $course_data;
11882 }
11883 } elseif ( 'learnpress_lesson_completed' === $trigger ) {
11884 $lesson_id = (int) ( isset( $data['filter']['lesson']['value'] ) ? $data['filter']['lesson']['value'] : '-1' );
11885 if ( $lesson_id > 0 ) {
11886
11887 $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 ) );
11888 } else {
11889
11890 $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" );
11891 }
11892
11893 if ( ! empty( $lesson ) ) {
11894 $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 ) );
11895 $context['response_type'] = 'live';
11896 $context['pluggable_data'] = $lesson_data;
11897 }
11898 } elseif ( 'learnpress_user_enrolled_in_course' === $trigger ) {
11899 $course_id = (int) ( isset( $data['filter']['course']['value'] ) ? $data['filter']['course']['value'] : '-1' );
11900 if ( $course_id > 0 ) {
11901
11902 $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 ) );
11903 } else {
11904
11905 $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" );
11906 }
11907
11908 if ( ! empty( $course ) ) {
11909 $course_data = array_merge( WordPress::get_user_context( $course->user_id ), LearnPress::get_lpc_course_context( $course->item_id ) );
11910 $context['response_type'] = 'live';
11911 $context['pluggable_data'] = $course_data;
11912
11913 }
11914 }
11915
11916 return $context;
11917 }
11918
11919 /**
11920 * Get Woocommerce Memberships Plan List.
11921 *
11922 * @param array $data data.
11923 *
11924 * @return array|void
11925 */
11926 public function search_wc_membership_plan_list( $data ) {
11927 global $wpdb;
11928
11929 $page = $data['page'];
11930 $limit = Utilities::get_search_page_limit();
11931 $offset = $limit * ( $page - 1 );
11932
11933 $args = [
11934 'post_type' => 'wc_membership_plan',
11935 'posts_per_page' => $limit,
11936 'offset' => $offset,
11937 'orderby' => 'title',
11938 'order' => 'ASC',
11939 'post_status' => 'publish',
11940 'fields' => 'ids',
11941 ];
11942 $loop = new WP_Query( $args );
11943
11944 $plans = (array) $loop->posts;
11945 $plans_count = wp_count_posts( 'wc_membership_plan' )->publish;
11946
11947 $options = [];
11948 if ( ! empty( $plans ) ) {
11949 if ( is_array( $plans ) ) {
11950 foreach ( $plans as $plan_id ) {
11951 $options[] = [
11952 'label' => get_the_title( $plan_id ),
11953 'value' => $plan_id,
11954 ];
11955 }
11956 }
11957 }
11958
11959 return [
11960 'options' => $options,
11961 'hasMore' => $plans_count > $limit && $plans_count > $offset,
11962 ];
11963 }
11964
11965 /**
11966 * Get BuddyPress Private group.
11967 *
11968 * @param array $data data.
11969 *
11970 * @return array|void
11971 */
11972 public function search_bp_private_group_list( $data ) {
11973 global $wpdb;
11974
11975 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'private'" );
11976
11977 $options = [];
11978 if ( $groups ) {
11979 foreach ( $groups as $group ) {
11980 $options[] = [
11981 'label' => $group->name,
11982 'value' => $group->id,
11983 ];
11984 }
11985 }
11986
11987 return [
11988 'options' => $options,
11989 'hasMore' => false,
11990 ];
11991 }
11992
11993 /**
11994 * Get BuddyPress Public group.
11995 *
11996 * @param array $data data.
11997 *
11998 * @return array|void
11999 */
12000 public function search_bp_public_group_list( $data ) {
12001 global $wpdb;
12002
12003 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups WHERE status = 'public'" );
12004
12005 $options = [];
12006 if ( $groups ) {
12007 foreach ( $groups as $group ) {
12008 $options[] = [
12009 'label' => $group->name,
12010 'value' => $group->id,
12011 ];
12012 }
12013 }
12014
12015 return [
12016 'options' => $options,
12017 'hasMore' => false,
12018 ];
12019 }
12020
12021 /**
12022 * Get BuddyPress group.
12023 *
12024 * @param array $data data.
12025 *
12026 * @return array|void
12027 */
12028 public function search_bp_group_list( $data ) {
12029 global $wpdb;
12030
12031 $groups = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_groups" );
12032
12033 $options = [];
12034 if ( $groups ) {
12035 foreach ( $groups as $group ) {
12036 $options[] = [
12037 'label' => $group->name,
12038 'value' => $group->id,
12039 ];
12040 }
12041 }
12042
12043 return [
12044 'options' => $options,
12045 'hasMore' => false,
12046 ];
12047 }
12048
12049 /**
12050 * Get BuddyPress field.
12051 *
12052 * @param array $data data.
12053 *
12054 * @return array|void
12055 */
12056 public function search_bp_field_list( $data ) {
12057 global $wpdb;
12058
12059 $base_group_id = 1;
12060 if ( function_exists( 'bp_xprofile_base_group_id' ) ) {
12061 $base_group_id = bp_xprofile_base_group_id();
12062 }
12063
12064 $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 ) );
12065
12066 $options = [];
12067 if ( ! empty( $xprofile_fields ) ) {
12068 foreach ( $xprofile_fields as $xprofile_field ) {
12069 $options[] = [
12070 'label' => $xprofile_field->name,
12071 'value' => $xprofile_field->id,
12072 ];
12073 }
12074 }
12075
12076 return [
12077 'options' => $options,
12078 'hasMore' => false,
12079 ];
12080 }
12081
12082 /**
12083 * Get BuddyPress member type.
12084 *
12085 * @param array $data data.
12086 *
12087 * @return array|void
12088 */
12089 public function search_bp_member_type_list( $data ) {
12090 $options = [];
12091 if ( function_exists( 'bp_get_member_types' ) ) {
12092 $types = bp_get_member_types( [] );
12093 if ( $types ) {
12094 foreach ( $types as $key => $type ) {
12095 $options[] = [
12096 'label' => $type,
12097 'value' => $key,
12098 ];
12099 }
12100 }
12101 }
12102
12103 return [
12104 'options' => $options,
12105 'hasMore' => false,
12106 ];
12107 }
12108
12109 /**
12110 * Get last data for WP All Import.
12111 *
12112 * @param array $data data.
12113 * @return mixed
12114 */
12115 public function search_wp_all_import_last_data( $data ) {
12116 global $wpdb;
12117 $post_type = $data['filter']['post_type']['value'];
12118 $trigger = $data['search_term'];
12119
12120 if ( 'wp_all_import_post_type_imported' === $trigger ) {
12121 if ( -1 == $post_type ) {
12122 $imports = $wpdb->get_row( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts ORDER BY id DESC LIMIT 1", ARRAY_A );
12123 $posts[0] = $imports['post_id'];
12124 } else {
12125 $imports = $wpdb->get_results( "SELECT post_id FROM {$wpdb->prefix}pmxi_posts", ARRAY_A );
12126 $imports = array_column( $imports, 'post_id' );
12127 $args = [
12128 'posts_per_page' => 1,
12129 'post_type' => $post_type,
12130 'post__in' => $imports,
12131 ];
12132 $posts = get_posts( $args );
12133 }
12134 } elseif ( 'wp_all_import_completed' === $trigger ) {
12135 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 0 ORDER BY id DESC LIMIT 1", ARRAY_A );
12136 } elseif ( 'wp_all_import_failed' === $trigger ) {
12137 $imports = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}pmxi_imports WHERE failed = 1 ORDER BY id DESC LIMIT 1", ARRAY_A );
12138 }
12139
12140 if ( 'wp_all_import_post_type_imported' === $trigger && empty( $imports ) ) {
12141 $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 );
12142 return $context;
12143 } elseif ( empty( $imports ) ) {
12144 $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 );
12145 return $context;
12146 }
12147
12148 $context['response_type'] = 'live';
12149 if ( ! empty( $posts ) ) {
12150 $context['pluggable_data'] = WordPress::get_post_context( $posts[0] );
12151 } else {
12152 $context['pluggable_data'] = $imports;
12153 }
12154
12155 return $context;
12156 }
12157
12158 /**
12159 * Get Wp Simple Pay Forms.
12160 *
12161 * @param array $data data.
12162 *
12163 * @return array
12164 */
12165 public function search_wp_simple_pay_forms( $data ) {
12166
12167 $page = $data['page'];
12168 $limit = Utilities::get_search_page_limit();
12169 $offset = $limit * ( $page - 1 );
12170
12171 $forms = get_posts(
12172 [
12173 'post_type' => 'simple-pay',
12174 'posts_per_page' => $limit,
12175 'offset' => $offset,
12176 'fields' => 'ids',
12177 ]
12178 );
12179
12180 $forms_count = wp_count_posts( 'simple-pay' )->publish;
12181
12182 $options = [];
12183
12184 if ( ! empty( $forms ) ) {
12185 foreach ( $forms as $form_id ) {
12186 if ( function_exists( 'simpay_get_form' ) ) {
12187 $form = simpay_get_form( $form_id );
12188 $options[] = [
12189 'label' => null !== get_the_title( $form_id ) ? $form->company_name : get_the_title( $form_id ),
12190 'value' => $form_id,
12191 ];
12192 }
12193 }
12194 }
12195
12196 return [
12197 'options' => $options,
12198 'hasMore' => $forms_count > $limit && $forms_count > $offset,
12199 ];
12200 }
12201
12202 /**
12203 * Get Post list as per post type for metabox.
12204 *
12205 * @param array $data data.
12206 *
12207 * @return array
12208 */
12209 public function search_mb_posts_list( $data ) {
12210
12211 $page = $data['page'];
12212 $limit = Utilities::get_search_page_limit();
12213 $offset = $limit * ( $page - 1 );
12214
12215 $posts = get_posts(
12216 [
12217 'post_type' => $data['dynamic'],
12218 'posts_per_page' => $limit,
12219 'offset' => $offset,
12220 'fields' => 'ids',
12221 ]
12222 );
12223
12224 $all_posts = get_posts(
12225 [
12226 'post_type' => $data['dynamic'],
12227 'posts_per_page' => -1,
12228 'fields' => 'ids',
12229 ]
12230 );
12231
12232 $posts_count = count( $all_posts );
12233
12234 $options = [];
12235
12236 if ( ! empty( $posts ) ) {
12237 foreach ( $posts as $post ) {
12238 $title = html_entity_decode( get_the_title( $post ), ENT_QUOTES, 'UTF-8' );
12239 $options[] = [
12240 'label' => $title,
12241 'value' => $post,
12242 ];
12243 }
12244 }
12245
12246 return [
12247 'options' => $options,
12248 'hasMore' => $posts_count > $limit && $posts_count > $offset,
12249 ];
12250 }
12251
12252 /**
12253 * Get Metabox Custom box in Post list.
12254 *
12255 * @param array $data data.
12256 *
12257 * @return array
12258 */
12259 public function search_mb_field_list( $data ) {
12260
12261 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
12262 return [];
12263 }
12264
12265 $options = [];
12266
12267 $metabox_fields = (array) rwmb_get_object_fields( $data['dynamic'] );
12268
12269 foreach ( $metabox_fields as $metabox_field ) {
12270
12271 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
12272
12273 $options[] = [
12274 'label' => $metabox_field['name'],
12275 'value' => $metabox_field['id'],
12276 ];
12277
12278 }
12279 }
12280
12281 return [
12282 'options' => $options,
12283 'hasMore' => false,
12284 ];
12285 }
12286
12287 /**
12288 * Get Metabox Custom box user list.
12289 *
12290 * @param array $data data.
12291 *
12292 * @return array
12293 */
12294 public function search_mb_user_field_list( $data ) {
12295
12296 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
12297 return [];
12298 }
12299
12300 $options = [];
12301
12302 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
12303
12304 foreach ( $metabox_fields as $metabox_field ) {
12305
12306 if ( ! empty( $metabox_field['id'] ) && ! empty( $metabox_field['name'] ) ) {
12307
12308 $options[] = [
12309 'label' => $metabox_field['name'],
12310 'value' => $metabox_field['id'],
12311 ];
12312
12313 }
12314 }
12315
12316 return [
12317 'options' => $options,
12318 'hasMore' => false,
12319 ];
12320 }
12321
12322 /**
12323 * Search Last Updated Field Data for MetaBox.
12324 *
12325 * @param array $data data.
12326 * @return array
12327 */
12328 public function search_meta_box_field_data( $data ) {
12329 global $wpdb;
12330
12331 $context = [];
12332
12333 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12334
12335 $post_type = $data['filter']['wp_post_type']['value'];
12336 $post = $data['filter']['wp_post']['value'];
12337
12338 if ( -1 === $field ) {
12339 if ( function_exists( 'rwmb_get_object_fields' ) ) {
12340 $metaboxes = rwmb_get_object_fields( $post_type );
12341
12342 if ( ! empty( $metaboxes ) ) {
12343 $random_key = array_rand( $metaboxes );
12344 $field = $random_key;
12345 } else {
12346 $result = '';
12347 }
12348 }
12349 } else {
12350 $field = $data['filter']['field_id']['value'];
12351 }
12352
12353 if ( function_exists( 'rwmb_meta' ) ) {
12354 $result = rwmb_meta( $field, '', $post );
12355 }
12356
12357 $response = [];
12358 if ( ! empty( $result ) ) {
12359 $response['pluggable_data'] = array_merge( [ $field => $result ], WordPress::get_post_context( $post ) );
12360 $response['response_type'] = 'live';
12361 } else {
12362 $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 );
12363 }
12364
12365 return $response;
12366 }
12367
12368 /**
12369 * Search Last Updated User Field Data MetaBox.
12370 *
12371 * @param array $data data.
12372 * @return array
12373 */
12374 public function search_user_meta_box_field_data( $data ) {
12375 global $wpdb;
12376
12377 $context = [];
12378
12379 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12380
12381 if ( -1 === $field ) {
12382 if ( function_exists( 'rwmb_get_object_fields' ) ) {
12383 $metabox_fields = (array) rwmb_get_object_fields( null, 'user' );
12384
12385 if ( ! empty( $metabox_fields ) ) {
12386 $random_key = array_rand( $metabox_fields );
12387 $field = $random_key;
12388 } else {
12389 $result = '';
12390 }
12391 }
12392 } else {
12393 $field = $data['filter']['field_id']['value'];
12394 }
12395
12396 $users = get_users(
12397 [
12398 'fields' => 'ID',
12399 'meta_key' => $field,
12400 ]
12401 );
12402
12403 if ( ! empty( $users ) ) {
12404 $user_random_key = array_rand( $users );
12405 $user_id = $user_random_key;
12406 if ( function_exists( 'rwmb_get_value' ) ) {
12407 $result = rwmb_get_value( $field, [ 'object_type' => 'user' ], $users[ $user_id ] );
12408 }
12409
12410 $response = [];
12411 if ( ! empty( $result ) ) {
12412 $context = [
12413 'field_id' => $field,
12414 $field => $result,
12415 'user' => WordPress::get_user_context( $users[ $user_id ] ),
12416 ];
12417 $response['pluggable_data'] = $context;
12418 $response['response_type'] = 'live';
12419 } else {
12420 $response = json_decode(
12421 '{
12422 "response_type": "sample",
12423 "pluggable_data": {
12424 "field_id": "gender",
12425 "user": {
12426 "wp_user_id": 114,
12427 "user_login": "test",
12428 "display_name": "test",
12429 "user_firstname": "test",
12430 "user_lastname": "test",
12431 "user_email": "test@test.com",
12432 "user_role": [ "subscriber" ]
12433 }
12434 }
12435 }',
12436 true
12437 );
12438 }
12439 } else {
12440 $response = json_decode(
12441 '{
12442 "response_type": "sample",
12443 "pluggable_data": {
12444 "field_id": "gender",
12445 "user": {
12446 "wp_user_id": 114,
12447 "user_login": "test",
12448 "display_name": "test",
12449 "user_firstname": "test",
12450 "user_lastname": "test",
12451 "user_email": "test@test.com",
12452 "user_role": [ "subscriber" ]
12453 }
12454 }
12455 }',
12456 true
12457 );
12458 }
12459
12460 return $response;
12461 }
12462
12463 /**
12464 * Search forms of Pie Forms.
12465 *
12466 * @param array $data data.
12467 * @return array
12468 */
12469 public function search_wp_polls_list( $data ) {
12470 global $wpdb;
12471 $options = [];
12472
12473 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsq' ) ) ) {
12474
12475 $results = $wpdb->get_results( 'SELECT pollq_id, pollq_question FROM ' . $wpdb->prefix . 'pollsq WHERE pollq_active = 1' );
12476
12477 if ( $results ) {
12478 foreach ( $results as $result ) {
12479 $options[] = [
12480 'label' => $result->pollq_question,
12481 'value' => $result->pollq_id,
12482 ];
12483 }
12484 }
12485 }
12486
12487 return [
12488 'options' => $options,
12489 'hasMore' => false,
12490 ];
12491 }
12492
12493 /**
12494 * Search answers of WP-Polls questions.
12495 *
12496 * @param array $data data.
12497 * @return array
12498 */
12499 public function search_wp_polls_answers( $data ) {
12500 global $wpdb;
12501
12502 $options = [];
12503 $poll_id = $data['dynamic'];
12504
12505 if ( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'pollsa' ) ) ) {
12506
12507 if ( '-1' !== $poll_id ) {
12508 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa WHERE polla_qid = %d', $poll_id ) );
12509 } else {
12510 $results = $wpdb->get_results( 'SELECT polla_aid, polla_answers FROM ' . $wpdb->prefix . 'pollsa' );
12511 }
12512
12513 if ( $results ) {
12514 foreach ( $results as $result ) {
12515 $options[] = [
12516 'label' => $result->polla_answers,
12517 'value' => $result->polla_aid,
12518 ];
12519 }
12520 }
12521 }
12522
12523 return [
12524 'options' => $options,
12525 'hasMore' => false,
12526 ];
12527 }
12528
12529 /**
12530 * Get last data for trigger.
12531 *
12532 * @param array $data data.
12533 * @return array
12534 */
12535 public function search_wp_polls_triggers_last_data( $data ) {
12536 global $wpdb;
12537
12538 $context = [];
12539 $context['response_type'] = 'sample';
12540
12541 $poll = [
12542 'poll_id' => 1,
12543 'question' => 'Which skills are you interested to learn?',
12544 'answers' => 'Web Development, Graphic Designing, Content Writing, Digital Marketing',
12545 'start_date' => '2023-08-29 17:19:13',
12546 'end_date' => 'Not set',
12547 'selected_answers' => 'Content Writing, Web Development',
12548 'selected_answer_id' => 2,
12549 ];
12550
12551 $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" );
12552
12553 if ( ! empty( $poll_data ) ) {
12554 $poll = WpPolls::get_poll_context( (string) $poll_data->answer_id, (int) $poll_data->poll_id );
12555 $poll['selected_answer_id'] = (int) $poll_data->answer_id;
12556
12557 $context['response_type'] = 'live';
12558 }
12559
12560 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
12561
12562 if ( 'poll_submitted' === $term ) {
12563 unset( $poll['selected_answer_id'] );
12564 }
12565
12566 $context['pluggable_data'] = $poll;
12567
12568 return $context;
12569 }
12570
12571 /**
12572 * Get ACF Custom fields list.
12573 *
12574 * @param array $data data.
12575 *
12576 * @return array
12577 */
12578 public function search_acf_post_field_list( $data ) {
12579
12580 $post_id = $data['dynamic']['wp_post'];
12581 $selected_post_type = $data['dynamic']['wp_post_type'];
12582
12583 if ( -1 === $post_id ) {
12584 $args = [
12585 'numberposts' => 1,
12586 'fields' => 'ids',
12587 'orderby' => 'rand',
12588 'post_type' => $selected_post_type,
12589 ];
12590 $posts = get_posts( $args );
12591 $post_id = $posts[0];
12592 }
12593
12594 $args = [
12595 'post_id' => $post_id,
12596 ];
12597 if ( ! is_numeric( $post_id ) ) {
12598 $args = [
12599 'post_type' => $post_id,
12600 ];
12601 }
12602 $options = [];
12603 if ( function_exists( 'acf_get_field_groups' ) ) {
12604 $field_groups_collection = acf_get_field_groups( $args );
12605 foreach ( $field_groups_collection as $field_group ) {
12606 if ( function_exists( 'acf_get_fields' ) ) {
12607 $field_groups[] = acf_get_fields( $field_group['key'] );
12608 }
12609 }
12610
12611 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
12612 foreach ( $field_groups as $field_groups ) {
12613 foreach ( $field_groups as $field_group ) {
12614 $options[] = [
12615 'value' => $field_group['name'],
12616 'label' => ! empty( $field_group['label'] ) ? $field_group['label'] : $field_group['name'],
12617 ];
12618 }
12619 }
12620 }
12621 }
12622
12623 return [
12624 'options' => $options,
12625 'hasMore' => false,
12626 ];
12627 }
12628
12629 /**
12630 * Get ACF Custom fields list.
12631 *
12632 * @param array $data data.
12633 *
12634 * @return array
12635 */
12636 public function search_acf_user_field_list( $data ) {
12637
12638 if ( ! function_exists( 'acf_get_fields' ) ) {
12639 return [];
12640 }
12641 if ( ! function_exists( 'acf_get_field_groups' ) ) {
12642 return [];
12643 }
12644 $groups_user_form = [];
12645 $options = [];
12646 if ( function_exists( 'acf_get_field_groups' ) ) {
12647 $field_groups = acf_get_field_groups();
12648 foreach ( $field_groups as $group ) {
12649 if ( ! empty( $group['location'] ) ) {
12650 foreach ( $group['location'] as $locations ) {
12651 foreach ( $locations as $location ) {
12652 if ( 'user_form' === $location['param'] || 'user_role' === $location['param'] || 'current_user' === $location['param'] || 'current_user_role' === $location['param'] ) {
12653 $groups_user_form[] = $group;
12654 }
12655 }
12656 }
12657 }
12658 }
12659
12660 if ( empty( $groups_user_form ) ) {
12661 return [];
12662 }
12663
12664 $key_values = array_map(
12665 function ( $item ) {
12666 return $item['key'];
12667 },
12668 $groups_user_form
12669 );
12670 $unique_keys = array_unique( $key_values );
12671 $unique_array = array_intersect_key( $groups_user_form, $unique_keys );
12672
12673 foreach ( $unique_array as $group ) {
12674 if ( function_exists( 'acf_get_fields' ) ) {
12675 $group_fields = acf_get_fields( $group['key'] );
12676 }
12677 if ( ! empty( $group_fields ) ) {
12678 foreach ( $group_fields as $field ) {
12679 $options[] = [
12680 'value' => $field['name'],
12681 'label' => $field['label'],
12682 ];
12683
12684 }
12685 }
12686 }
12687 }
12688
12689 return [
12690 'options' => $options,
12691 'hasMore' => false,
12692 ];
12693 }
12694
12695 /**
12696 * Get ACF Custom fields list.
12697 *
12698 * @param array $data data.
12699 *
12700 * @return array
12701 */
12702 public function search_acf_options_field_list( $data ) {
12703
12704 if ( ! function_exists( 'acf_get_fields' ) ) {
12705 return [];
12706 }
12707 if ( ! function_exists( 'acf_get_field_groups' ) ) {
12708 return [];
12709 }
12710 $groups_options_form = [];
12711 $options = [];
12712 if ( function_exists( 'acf_get_field_groups' ) ) {
12713 $field_groups = acf_get_field_groups();
12714 foreach ( $field_groups as $group ) {
12715 if ( ! empty( $group['location'] ) ) {
12716 foreach ( $group['location'] as $locations ) {
12717 foreach ( $locations as $location ) {
12718 if ( 'options_page' === $location['param'] ) {
12719 $groups_options_form[] = $group;
12720 }
12721 }
12722 }
12723 }
12724 }
12725 if ( empty( $groups_options_form ) ) {
12726 return [];
12727 }
12728 $key_values = array_map(
12729 function ( $item ) {
12730 return $item['key'];
12731 },
12732 $groups_options_form
12733 );
12734 $unique_keys = array_unique( $key_values );
12735 $unique_array = array_intersect_key( $groups_options_form, $unique_keys );
12736 foreach ( $unique_array as $group ) {
12737 if ( function_exists( 'acf_get_fields' ) ) {
12738 $group_fields = acf_get_fields( $group['key'] );
12739 }
12740 if ( ! empty( $group_fields ) ) {
12741 foreach ( $group_fields as $field ) {
12742 $options[] = [
12743 'value' => $field['name'],
12744 'label' => $field['label'],
12745 ];
12746
12747 }
12748 }
12749 }
12750 }
12751
12752 return [
12753 'options' => $options,
12754 'hasMore' => false,
12755 ];
12756 }
12757
12758 /**
12759 * Search Last Updated Field Data for ACF.
12760 *
12761 * @param array $data data.
12762 * @return array
12763 */
12764 public function search_acf_post_field_data( $data ) {
12765 $context = [];
12766
12767 $field = ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12768
12769 $post_type = $data['filter']['wp_post_type']['value'];
12770 $post = $data['filter']['wp_post']['value'];
12771
12772 if ( -1 === $post ) {
12773 $args = [
12774 'numberposts' => 1,
12775 'fields' => 'ids',
12776 'orderby' => 'rand',
12777 'post_type' => $post_type,
12778 ];
12779 $posts = get_posts( $args );
12780 $post = $posts[0];
12781 }
12782 if ( -1 === $field ) {
12783 $args = [
12784 'post_id' => $post,
12785 ];
12786 if ( function_exists( 'acf_get_field_groups' ) ) {
12787 $field_groups_collection = acf_get_field_groups( $args );
12788 }
12789 if ( ! empty( $field_groups_collection ) ) {
12790 foreach ( $field_groups_collection as $field_group ) {
12791 if ( function_exists( 'acf_get_fields' ) ) {
12792 $field_groups[] = acf_get_fields( $field_group['key'] );
12793 }
12794 }
12795 }
12796 $fields = [];
12797 if ( ! empty( $field_groups ) && is_array( $field_groups ) ) {
12798 foreach ( $field_groups as $field_groups ) {
12799 $fields[] = $field_groups;
12800 }
12801 }
12802 if ( ! empty( $fields ) ) {
12803 $random_key = array_rand( $fields[0] );
12804 $field_key = $fields[0][ $random_key ];
12805 $field = $field_key['name'];
12806 } else {
12807 $result = '';
12808 }
12809 } else {
12810 $field = $data['filter']['field_id']['value'];
12811 }
12812 if ( function_exists( ( 'get_field' ) ) ) {
12813 $result = get_field( $field, $post );
12814 }
12815
12816 $response = [];
12817 if ( ! empty( $result ) ) {
12818 $post_fields = [];
12819 if ( function_exists( 'get_fields' ) ) {
12820 $post_fields = get_fields( $post );
12821 }
12822 $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 ) ] );
12823 $response['response_type'] = 'live';
12824 } else {
12825 $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 );
12826 }
12827
12828 return $response;
12829 }
12830
12831 /**
12832 * Search Last Updated User Field Data ACF.
12833 *
12834 * @param array $data data.
12835 * @return array
12836 */
12837 public function search_acf_user_field_data( $data ) {
12838 global $wpdb;
12839
12840 $context = [];
12841
12842 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12843
12844 if ( -1 === $field ) {
12845 $groups_user_form = [];
12846 if ( function_exists( 'acf_get_field_groups' ) ) {
12847 $field_groups = acf_get_field_groups();
12848 }
12849 if ( ! empty( $field_groups ) ) {
12850 foreach ( $field_groups as $group ) {
12851 if ( ! empty( $group['location'] ) ) {
12852 foreach ( $group['location'] as $locations ) {
12853 foreach ( $locations as $location ) {
12854 if ( 'user_form' === $location['param'] || 'user_role' === $location['param'] || 'current_user' === $location['param'] || 'current_user_role' === $location['param'] ) {
12855 $groups_user_form[] = $group;
12856 }
12857 }
12858 }
12859 }
12860 }
12861 $field_groups = $groups_user_form;
12862 }
12863 if ( empty( $field_groups ) ) {
12864 $result = '';
12865 }
12866 $fields = [];
12867 if ( ! empty( $field_groups ) ) {
12868 foreach ( $field_groups as $group ) {
12869 if ( function_exists( 'acf_get_fields' ) ) {
12870 $group_fields = acf_get_fields( $group['key'] );
12871 }
12872 if ( ! empty( $group_fields ) ) {
12873 foreach ( $group_fields as $field ) {
12874 $fields[] = $group_fields;
12875 }
12876 }
12877 }
12878 }
12879 if ( ! empty( $fields ) ) {
12880 $random_key = array_rand( $fields );
12881 $field = $random_key;
12882 } else {
12883 $result = '';
12884 }
12885 } else {
12886 $field = $data['filter']['field_id']['value'];
12887 }
12888 $users = get_users(
12889 [
12890 'fields' => 'ID',
12891 'meta_key' => $field,
12892 ]
12893 );
12894
12895 if ( ! empty( $users ) ) {
12896 $user_random_key = array_rand( $users );
12897 $user_id = $user_random_key;
12898 if ( function_exists( 'get_field' ) ) {
12899 $result = get_field( $field, 'user_' . $users[ $user_id ] );
12900 }
12901 $response = [];
12902 if ( ! empty( $result ) ) {
12903 $context = [
12904 'field_id' => $field,
12905 $field => $result,
12906 'user' => WordPress::get_user_context( $users[ $user_id ] ),
12907 ];
12908 $response['pluggable_data'] = $context;
12909 $response['response_type'] = 'live';
12910 } else {
12911 $response = json_decode(
12912 '{
12913 "response_type": "sample",
12914 "pluggable_data": {
12915 "field_id": "gender",
12916 "user": {
12917 "wp_user_id": 114,
12918 "user_login": "test",
12919 "display_name": "test",
12920 "user_firstname": "test",
12921 "user_lastname": "test",
12922 "user_email": "test@test.com",
12923 "user_role": [ "subscriber" ]
12924 }
12925 }
12926 }',
12927 true
12928 );
12929 }
12930 } else {
12931 $response = json_decode(
12932 '{
12933 "response_type": "sample",
12934 "pluggable_data": {
12935 "field_id": "gender",
12936 "user": {
12937 "wp_user_id": 114,
12938 "user_login": "test",
12939 "display_name": "test",
12940 "user_firstname": "test",
12941 "user_lastname": "test",
12942 "user_email": "test@test.com",
12943 "user_role": [ "subscriber" ]
12944 }
12945 }
12946 }',
12947 true
12948 );
12949 }
12950
12951 return $response;
12952 }
12953
12954 /**
12955 * Search Last Updated Options Field Data ACF.
12956 *
12957 * @param array $data data.
12958 * @return array
12959 */
12960 public function search_acf_options_field_data( $data ) {
12961 global $wpdb;
12962 $context = [];
12963 $field = (int) ( isset( $data['filter']['field_id']['value'] ) ? $data['filter']['field_id']['value'] : -1 );
12964
12965 if ( -1 === $field ) {
12966 $groups_options_form = [];
12967 if ( function_exists( 'acf_get_field_groups' ) ) {
12968 $field_groups = acf_get_field_groups();
12969 }
12970 if ( ! empty( $field_groups ) ) {
12971 foreach ( $field_groups as $group ) {
12972 if ( ! empty( $group['location'] ) ) {
12973 foreach ( $group['location'] as $locations ) {
12974 foreach ( $locations as $location ) {
12975 if ( 'options_page' === $location['param'] ) {
12976 $groups_options_form[] = $group;
12977 }
12978 }
12979 }
12980 }
12981 }
12982 }
12983 if ( empty( $groups_options_form ) ) {
12984 $result = '';
12985 }
12986 $key_values = array_map(
12987 function ( $item ) {
12988 return $item['key'];
12989 },
12990 $groups_options_form
12991 );
12992 $unique_keys = array_unique( $key_values );
12993 $unique_array = array_intersect_key( $groups_options_form, $unique_keys );
12994 $fields = [];
12995 if ( ! empty( $unique_array ) ) {
12996 foreach ( $unique_array as $group ) {
12997 if ( function_exists( 'acf_get_fields' ) ) {
12998 $group_fields = acf_get_fields( $group['key'] );
12999 }
13000 if ( ! empty( $group_fields ) ) {
13001 foreach ( $group_fields as $field ) {
13002 $fields[] = $group_fields;
13003 }
13004 }
13005 }
13006 }
13007 if ( ! empty( $fields ) ) {
13008 $random_key = array_rand( $fields );
13009 $field = $random_key;
13010 } else {
13011 $result = '';
13012 }
13013 } else {
13014 $field = $data['filter']['field_id']['value'];
13015 }
13016 if ( function_exists( 'get_field' ) ) {
13017 $option_value = get_field( $field, 'option' );
13018 }
13019 if ( ! empty( $option_value ) ) {
13020 if ( function_exists( 'acf_get_field' ) ) {
13021 $options_fields = acf_get_field( $field );
13022 if ( function_exists( 'acf_maybe_get' ) ) {
13023 $options_page = acf_maybe_get( $options_fields, 'parent' );
13024 }
13025 }
13026 $context = [
13027 'field_id' => $field,
13028 $field => $option_value,
13029 ];
13030 $response['pluggable_data'] = $context;
13031 $response['response_type'] = 'live';
13032 } else {
13033 $response = json_decode(
13034 '{
13035 "response_type": "sample",
13036 "pluggable_data": {
13037 "field_id": "optionpage",
13038 "optionpage": "newoption"
13039 }
13040 }',
13041 true
13042 );
13043 }
13044 return $response;
13045 }
13046
13047 /**
13048 * Get WP Fusion Tags list.
13049 *
13050 * @param array $data data.
13051 *
13052 * @return array
13053 */
13054 public function search_wp_fusion_tag_list( $data ) {
13055
13056 if ( ! function_exists( 'wp_fusion' ) ) {
13057 return [];
13058 }
13059
13060 $options = [];
13061 $tags = wp_fusion()->settings->get( 'available_tags' );
13062
13063 if ( $tags ) {
13064 foreach ( $tags as $t_id => $tag ) {
13065 if ( is_array( $tag ) && isset( $tag['label'] ) ) {
13066 $options[] = [
13067 'value' => $t_id,
13068 'label' => $tag['label'],
13069 ];
13070 } else {
13071 $options[] = [
13072 'value' => $t_id,
13073 'label' => $tag,
13074 ];
13075 }
13076 }
13077 }
13078
13079 return [
13080 'options' => $options,
13081 'hasMore' => false,
13082 ];
13083 }
13084
13085 /**
13086 * Get list of events for Modern Events Calendar.
13087 *
13088 * @param array $data data.
13089 * @return array
13090 */
13091 public function search_mec_events_list( $data ) {
13092 $page = $data['page'];
13093 $limit = Utilities::get_search_page_limit();
13094 $offset = $limit * ( $page - 1 );
13095
13096 $args = [
13097 'post_type' => 'mec-events',
13098 'post_status' => [ 'publish', 'private' ],
13099 'posts_per_page' => -1,
13100 ];
13101 $loop = new WP_Query( $args );
13102 $events_count = count( $loop->posts );
13103
13104 $args = [
13105 'post_type' => 'mec-events',
13106 'posts_per_page' => $limit,
13107 'offset' => $offset,
13108 'orderby' => 'title',
13109 'order' => 'ASC',
13110 'post_status' => [ 'publish', 'private' ],
13111 ];
13112
13113 $loop = new WP_Query( $args );
13114 $events = $loop->posts;
13115
13116 $options = [];
13117 if ( ! empty( $events ) ) {
13118 foreach ( $events as $event ) {
13119 if ( isset( $event->ID ) ) {
13120 $options[] = [
13121 'label' => get_the_title( $event ),
13122 'value' => $event->ID,
13123 ];
13124 }
13125 }
13126 }
13127
13128 return [
13129 'options' => $options,
13130 'hasMore' => $events_count > $limit && $events_count > $offset,
13131 ];
13132 }
13133
13134 /**
13135 * Search tickets of MEC events.
13136 *
13137 * @param array $data data.
13138 * @return array
13139 */
13140 public function search_mec_event_tickets( $data ) {
13141 $options = [];
13142 $event_id = $data['dynamic'];
13143
13144 $event_tickets = get_post_meta( $event_id, 'mec_tickets', true );
13145
13146 if ( ! empty( $event_tickets ) && is_array( $event_tickets ) ) {
13147 foreach ( $event_tickets as $ticket_id => $event_ticket ) {
13148 if ( isset( $event_ticket['name'] ) ) {
13149 $options[] = [
13150 'label' => $event_ticket['name'],
13151 'value' => $ticket_id,
13152 ];
13153 }
13154 }
13155 }
13156
13157 return [
13158 'options' => $options,
13159 'hasMore' => false,
13160 ];
13161 }
13162
13163 /**
13164 * Get last data for trigger.
13165 *
13166 * @param array $data data.
13167 * @return array
13168 */
13169 public function search_mec_triggers_last_data( $data ) {
13170 global $wpdb;
13171
13172 $context = [];
13173 $context['response_type'] = 'sample';
13174
13175 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
13176
13177 if ( 'new_event' === $term ) {
13178 $post_id = $wpdb->get_var( "SELECT post_id FROM {$wpdb->prefix}mec_events ORDER BY id DESC LIMIT 1" );
13179
13180 $live_data = [
13181 'post_id' => $post_id,
13182 'event' => get_post_meta( $post_id ),
13183 'post' => get_post( $post_id ),
13184 ];
13185
13186 if ( ! empty( $post_id ) ) {
13187 $context['pluggable_data'] = $live_data;
13188 $context['response_type'] = 'live';
13189 } else {
13190 $context['pluggable_data'] = [
13191 'post_id' => 272,
13192 'event' => [
13193 'mec_color' => [ 'e14d43' ],
13194 '_thumbnail_id' => [ 229 ],
13195 'mec_location_id' => [ 49 ],
13196 'mec_dont_show_map' => [ 0 ],
13197 'mec_organizer_id' => [ 53 ],
13198 'mec_start_date' => [ '2025-04-01' ],
13199 'mec_start_time_hour' => [ 8 ],
13200 'mec_start_time_minutes' => [ 0 ],
13201 'mec_start_time_ampm' => [ 'AM' ],
13202 'mec_start_day_seconds' => [ 32400 ],
13203 'mec_start_datetime' => [ '2025-04-01 08:00 AM' ],
13204 'mec_end_date' => [ '2025-04-01' ],
13205 'mec_date' => [ 'a:3:{s:5:"start";a:4:{s:4:"date";s:10:"2025-03-29";s:4:"hour";s:1:"8";s:7:"minutes";s:1:"0";s:4:"ampm";s:2:"AM";}s:3:"end";a:4:{s:4:"date";s:10:"2025-03-29";s:4:"hour";s:1:"6";s:7:"minutes";s:1:"0";s:4:"ampm";s:2:"PM";}s:6:"allday";s:1:"1";}' ],
13206 'mec_end_time_hour' => [ 6 ],
13207 'mec_end_time_minutes' => [ 0 ],
13208 'mec_end_time_ampm' => [ 'PM' ],
13209 'mec_end_day_seconds' => [ 68400 ],
13210 'mec_end_datetime' => [ '2025-04-01 06:00 PM' ],
13211 'mec_allday' => [ 1 ],
13212 'mec_fees' => [ 'a:0:{}' ],
13213 'mec_op' => [ 'a:0:{}' ],
13214 'mec_repeat' => [ 'a:0:{}' ],
13215 'mec_booking' => [ 'a:0:{}' ],
13216 'mec_comment' => [ '' ],
13217 'mec_in_days' => [ '' ],
13218 'mec_tickets' => [ 'a:0:{}' ],
13219 'mec_reg_fields' => [ 'a:0:{}' ],
13220 'mec_hourly_schedules' => [ 'a:0:{}' ],
13221 'mec_ticket_variations' => [ 'a:0:{}' ],
13222 'mec_hide_time' => [ 0 ],
13223 'mec_hide_end_time' => [ 0 ],
13224 'mec_repeat_status' => [ 0 ],
13225 'mec_fees_global_inheritance' => [ 1 ],
13226 'mec_ticket_variations_global_inheritance' => [ 1 ],
13227 'mec_reg_fields_global_inheritance' => [ 1 ],
13228 'mec_event_date_submit' => [ '20250331042811' ],
13229 'mec_new_event_notif_sent' => [ 1 ],
13230 ],
13231 'post' => [
13232 'ID' => 272,
13233 'post_author' => 1,
13234 'post_date' => '2025-03-31 04:28:09',
13235 'post_date_gmt' => '2025-03-31 04:28:09',
13236 'post_content' => '<p>Sample Event</p>',
13237 'post_title' => 'Sample Event',
13238 'post_status' => 'publish',
13239 'comment_status' => 'open',
13240 'ping_status' => 'closed',
13241 'post_name' => 'sample-event',
13242 'guid' => 'http://example.local/events/sample-event/',
13243 'post_type' => 'mec-events',
13244 'comment_count' => 0,
13245 ],
13246 ];
13247 $context['response_type'] = 'sample';
13248 }
13249
13250 return $context;
13251 }
13252
13253 $where = '';
13254 if ( 'cancelled' === $term ) {
13255 $where = 'WHERE verified = -1';
13256 } elseif ( 'confirmed' === $term ) {
13257 $where = 'WHERE confirmed = 1';
13258 } elseif ( 'pending' === $term ) {
13259 $where = 'WHERE confirmed = 0';
13260 }
13261
13262 $event_id = (int) ( isset( $data['filter']['event_id']['value'] ) ? $data['filter']['event_id']['value'] : '-1' );
13263
13264 if ( -1 !== $event_id ) {
13265 if ( ! empty( $where ) ) {
13266 $where .= ' AND event_id = ' . $event_id;
13267 } else {
13268 $where = 'WHERE event_id = ' . $event_id;
13269 }
13270 }
13271
13272 $event_data = $wpdb->get_row( "SELECT booking_id FROM {$wpdb->prefix}mec_bookings $where ORDER BY id DESC LIMIT 1" ); // @phpcs:ignore
13273
13274 if ( ! empty( $event_data ) ) {
13275 $event = ModernEventsCalendar::get_event_context( (int) $event_data->booking_id );
13276 $context['response_type'] = 'live';
13277 } else {
13278 $event = [
13279 'event_id' => 1,
13280 'title' => 'Sample Event',
13281 'description' => 'Description of the sample event.',
13282 'categories' => 'New, Sample',
13283 'start_date' => 'September 13, 2023',
13284 'start_time' => '8:00 AM',
13285 'end_date' => 'September 13, 2023',
13286 'end_time' => '11:00 AM',
13287 'location' => 'City Hall',
13288 'organizer' => 'John Doe',
13289 'cost' => '5000',
13290 'featured_image_id' => 1,
13291 'featured_image_url' => 'https://example.com/wp-content/uploads/2022/12/Screenshot_20221127_021332.png',
13292 'tickets' => [
13293 [
13294 'id' => 1,
13295 'name' => 'Silver',
13296 'description' => 'Standard seat with reasonable price.',
13297 'price' => '300',
13298 'price_label' => 'USD',
13299 'limit' => '20',
13300 ],
13301 [
13302 'id' => 2,
13303 'name' => 'Premium',
13304 'description' => 'VIP seat with high price.',
13305 'price' => '500',
13306 'price_label' => 'USD',
13307 'limit' => '10',
13308 ],
13309 ],
13310 'attendees' => [
13311 [
13312 'id' => 1,
13313 'email' => 'johndoe@test.com',
13314 'name' => 'John Doe',
13315 ],
13316 [
13317 'id' => 2,
13318 'email' => 'adamsmith@test.com',
13319 'name' => 'Adam Smith',
13320 ],
13321 ],
13322 'booking' => [
13323 'title' => 'johndoe@test.com - John Doe',
13324 'transaction_id' => 'RSH59404',
13325 'amount_payable' => '800',
13326 'price' => '800',
13327 'time' => '2023-09-07 06:40:32',
13328 'payment_gateway' => 'Manual Pay',
13329 'confirmation_status' => 'Pending',
13330 'verification_status' => 'Verified',
13331 'attendees_count' => 2,
13332 ],
13333 ];
13334 }
13335
13336 $context['pluggable_data'] = $event;
13337 return $context;
13338 }
13339
13340 /**
13341 * Get form list Contact Form 7.
13342 *
13343 * @param array $data data.
13344 *
13345 * @return array
13346 */
13347 public function search_contact_form7_list( $data ) {
13348
13349 $page = $data['page'];
13350 $limit = Utilities::get_search_page_limit();
13351 $offset = $limit * ( $page - 1 );
13352
13353 $posts = get_posts(
13354 [
13355 'post_type' => 'wpcf7_contact_form',
13356 'posts_per_page' => $limit,
13357 'offset' => $offset,
13358 ]
13359 );
13360
13361 $all_posts = get_posts(
13362 [
13363 'post_type' => 'wpcf7_contact_form',
13364 'posts_per_page' => -1,
13365 ]
13366 );
13367
13368 $posts_count = count( $all_posts );
13369
13370 $options = [];
13371
13372 if ( ! empty( $posts ) ) {
13373 foreach ( $posts as $post ) {
13374 $options[] = [
13375 'label' => get_the_title( $post->ID ),
13376 'value' => $post->ID,
13377 ];
13378 }
13379 }
13380
13381 return [
13382 'options' => $options,
13383 'hasMore' => $posts_count > $limit && $posts_count > $offset,
13384 ];
13385 }
13386
13387 /**
13388 * Get Thrive Leads form list
13389 *
13390 * @param array $data data.
13391 *
13392 * @return array
13393 */
13394 public function search_thrive_leads_forms_list( $data ) {
13395 $options = [];
13396
13397 $lg_ids = get_posts(
13398 [
13399 'post_type' => '_tcb_form_settings',
13400 'fields' => 'id=>parent',
13401 'posts_per_page' => -1,
13402 'post_status' => 'any',
13403 ]
13404 );
13405
13406 if ( function_exists( 'tve_leads_get_form_variations' ) ) {
13407 foreach ( $lg_ids as $lg_id => $lg_parent ) {
13408 $variations = tve_leads_get_form_variations( $lg_parent );
13409 foreach ( $variations as $variation ) {
13410 $options[] = [
13411 'label' => $variation['post_title'],
13412 'value' => $lg_parent,
13413 ];
13414 }
13415 }
13416 }
13417
13418 return [
13419 'options' => array_unique( $options ),
13420 'hasMore' => false,
13421 ];
13422 }
13423
13424 /**
13425 * Get list for Woocommerce Subscriptions
13426 *
13427 * @param array $data data.
13428 *
13429 * @return array
13430 */
13431 public function search_wc_subscription_variation_products( $data ) {
13432 $options = [];
13433
13434 global $wpdb;
13435
13436 if ( ! function_exists( 'wc_get_product' ) ) {
13437 return [];
13438 }
13439 $subscriptions = $wpdb->get_results(
13440 $wpdb->prepare(
13441 "SELECT posts.ID, posts.post_title FROM $wpdb->posts as posts
13442 LEFT JOIN $wpdb->term_relationships as rel ON (posts.ID = rel.object_id)
13443 WHERE rel.term_taxonomy_id IN (SELECT term_id FROM $wpdb->terms WHERE slug IN ('subscription','variable-subscription'))
13444 AND posts.post_type = %s
13445 AND posts.post_status = %s
13446 UNION ALL
13447 SELECT ID, post_title FROM $wpdb->posts
13448 WHERE post_type = %s
13449 AND post_status = %s
13450 ORDER BY post_title",
13451 'product',
13452 'publish',
13453 'shop_subscription',
13454 'publish'
13455 )
13456 );
13457 $page = $data['page'];
13458 $limit = Utilities::get_search_page_limit();
13459 $offset = $limit * ( $page - 1 );
13460 $subscriptions_products = [];
13461 $subscription_products_count = 10;
13462 if ( function_exists( 'wc_get_products' ) ) {
13463 $product_query = wc_get_products(
13464 [
13465 'type' => [ 'subscription', 'variable-subscription' ],
13466 'posts_per_page' => $limit,
13467 'offset' => $offset,
13468 'paginate' => true,
13469 ]
13470 );
13471 if ( is_object( $product_query ) ) {
13472 $subscriptions_products = $product_query->products;
13473 $subscription_products_count = $product_query->total;
13474 }
13475 }
13476
13477 if ( $subscriptions ) {
13478 foreach ( $subscriptions as $product ) {
13479 $options[] = [
13480 'label' => $product->post_title . ' (#' . $product->ID . ')',
13481 'value' => (int) $product->ID,
13482 ];
13483 $product_s = wc_get_product( $product->ID );
13484 /**
13485 *
13486 * Ignore line
13487 *
13488 * @phpstan-ignore-next-line
13489 */
13490 if ( 'variable-subscription' == $product_s->product_type ) {
13491 $args = [
13492 'post_type' => 'product_variation',
13493 'post_parent' => $product->ID,
13494 'posts_per_page' => -1,
13495 'orderby' => 'ID',
13496 'order' => 'ASC',
13497 'post_status' => 'publish',
13498 ];
13499
13500 $variations = get_posts( $args );
13501
13502 foreach ( $variations as $var ) {
13503 $options[] = [
13504 'label' => $var->post_title . ' (#' . $var->ID . ')',
13505 'value' => $var->ID,
13506 ];
13507 }
13508 }
13509 }
13510 return [
13511 'options' => $options,
13512 'hasMore' => false,
13513 ];
13514 } elseif ( ! empty( $subscriptions_products ) ) {
13515 foreach ( $subscriptions_products as $product ) {
13516 $title = $product->get_name();
13517 $options[] = [
13518 'label' => $title . ' (#' . $product->get_id() . ')',
13519 'value' => $product->get_id(),
13520 ];
13521 $product_s = wc_get_product( $product->get_id() );
13522 /**
13523 *
13524 * Ignore line
13525 *
13526 * @phpstan-ignore-next-line
13527 */
13528 if ( 'variable-subscription' == $product_s->product_type ) {
13529 $args = [
13530 'post_type' => 'product_variation',
13531 'post_parent' => $product->get_id(),
13532 'posts_per_page' => -1,
13533 'orderby' => 'ID',
13534 'order' => 'ASC',
13535 'post_status' => 'publish',
13536 ];
13537
13538 $variations = get_posts( $args );
13539
13540 foreach ( $variations as $var ) {
13541 $options[] = [
13542 'label' => $var->post_title . ' (#' . $var->ID . ')',
13543 'value' => $var->ID,
13544 ];
13545 }
13546 }
13547 }
13548 return [
13549 'options' => $options,
13550 'hasMore' => $subscription_products_count > $limit && $subscription_products_count > $offset,
13551 ];
13552 } else {
13553 return [
13554 'options' => $options,
13555 'hasMore' => false,
13556 ];
13557 }
13558 }
13559
13560 /**
13561 * Get WS Forms form list
13562 *
13563 * @param array $data data.
13564 *
13565 * @return array
13566 */
13567 public function search_ws_forms_list( $data ) {
13568 $options = [];
13569 global $wpdb;
13570
13571 $forms = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wsf_form", 'ARRAY_A' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
13572
13573 foreach ( $forms as $form ) {
13574 $options[] = [
13575 'label' => $form['label'],
13576 'value' => $form['id'],
13577 ];
13578 }
13579
13580 return [
13581 'options' => $options,
13582 'hasMore' => false,
13583 ];
13584 }
13585
13586 /**
13587 * Get Learndash Achievement list
13588 *
13589 * @param array $data data.
13590 *
13591 * @return array
13592 */
13593 public function search_ld_achievements_list( $data ) {
13594 $page = $data['page'];
13595 $limit = Utilities::get_search_page_limit();
13596 $offset = $limit * ( $page - 1 );
13597
13598 $posts = get_posts(
13599 [
13600 'post_type' => 'ld-achievement',
13601 'posts_per_page' => $limit,
13602 'offset' => $offset,
13603 'post_status' => [ 'publish' ],
13604 ]
13605 );
13606
13607 $posts_count = wp_count_posts( 'ld-achievement' )->publish;
13608
13609 $options = [];
13610
13611 if ( ! empty( $posts ) ) {
13612 foreach ( $posts as $post ) {
13613 $options[] = [
13614 'label' => get_the_title( $post->ID ),
13615 'value' => $post->ID,
13616 ];
13617 }
13618 }
13619
13620 return [
13621 'options' => $options,
13622 'hasMore' => $posts_count > $limit && $posts_count > $offset,
13623 ];
13624 }
13625
13626 /**
13627 * Get Advanced Ads list
13628 *
13629 * @param array $data data.
13630 *
13631 * @return array
13632 */
13633 public function search_ads_list( $data ) {
13634 $page = $data['page'];
13635 $limit = Utilities::get_search_page_limit();
13636 $offset = $limit * ( $page - 1 );
13637
13638 $posts = get_posts(
13639 [
13640 'post_type' => 'advanced_ads',
13641 'posts_per_page' => $limit,
13642 'offset' => $offset,
13643 'post_status' => [ 'publish', 'draft' ],
13644 ]
13645 );
13646
13647 $posts_count = wp_count_posts( 'advanced_ads' )->publish;
13648
13649 $options = [];
13650
13651 if ( ! empty( $posts ) ) {
13652 foreach ( $posts as $post ) {
13653 $options[] = [
13654 'label' => get_the_title( $post->ID ),
13655 'value' => $post->ID,
13656 ];
13657 }
13658 }
13659
13660 return [
13661 'options' => $options,
13662 'hasMore' => $posts_count > $limit && $posts_count > $offset,
13663 ];
13664 }
13665
13666 /**
13667 * Advanced Ads pluggable data.
13668 *
13669 * @param array $data data.
13670 * @return array
13671 */
13672 public function search_ad_last_data( $data ) {
13673 $context = [];
13674 $args = [
13675 'post_type' => 'advanced_ads',
13676 'posts_per_page' => 1,
13677 'orderby' => 'modified',
13678 'order' => 'DESC',
13679 ];
13680
13681 if ( isset( $data['filter']['ad_status']['value'] ) ) {
13682 $post_status = $data['filter']['ad_status']['value'];
13683 $args['post_status'] = $post_status;
13684 }
13685
13686 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
13687 $post_status = $data['filter']['ad_new_status']['value'];
13688 $args['post_status'] = $post_status;
13689 }
13690
13691 if ( isset( $data['filter']['ad_id']['value'] ) ) {
13692 $post_id = $data['filter']['ad_id']['value'];
13693 if ( -1 != $post_id ) {
13694 if ( $post_id > 0 ) {
13695 $args['p'] = $post_id;
13696 }
13697 }
13698 }
13699
13700 $posts = get_posts( $args );
13701 if ( ! empty( $posts ) ) {
13702 $context['pluggable_data'] = $posts[0];
13703 $context['pluggable_data'] = WordPress::get_post_context( $posts[0]->ID );
13704 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
13705 $context['pluggable_data']['ad_new_status'] = $posts[0]->post_status;
13706 }
13707 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
13708 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
13709 }
13710 $context['pluggable_data']['ad_id'] = $posts[0]->ID;
13711 if ( isset( $data['filter']['ad_status']['value'] ) ) {
13712 $context['pluggable_data']['ad_status'] = $posts[0]->post_status;
13713 }
13714 $context['response_type'] = 'live';
13715 } else {
13716 $context['pluggable_data'] = [
13717 'ID' => 1,
13718 'post' => 1,
13719 'post_author' => 1,
13720 'post_date' => '2022-11-18 12:18:14',
13721 'post_date_gmt' => '2022-11-18 12:18:14',
13722 'post_content' => 'Ad Post Content',
13723 'post_title' => 'Ad Post',
13724 'post_excerpt' => '',
13725 'post_status' => 'draft',
13726 'comment_status' => 'open',
13727 'ping_status' => 'open',
13728 'post_password' => '',
13729 'post_name' => 'ad-post',
13730 'to_ping' => '',
13731 'pinged' => '',
13732 'post_modified' => '2022-11-18 12:18:14',
13733 'post_modified_gmt' => '2022-11-18 12:18:14',
13734 'post_content_filtered' => '',
13735 'post_parent' => 0,
13736 'guid' => 'https://example.com/ad-post/',
13737 'menu_order' => 0,
13738 'post_type' => 'advanced_ads',
13739 'post_mime_type' => '',
13740 'comment_count' => 0,
13741 'filter' => 'raw',
13742 ];
13743 if ( isset( $data['filter']['ad_new_status']['value'] ) ) {
13744 $context['pluggable_data']['ad_new_status'] = $data['filter']['ad_new_status']['value'];
13745 }
13746 if ( isset( $data['filter']['ad_old_status']['value'] ) ) {
13747 $context['pluggable_data']['ad_old_status'] = $data['filter']['ad_old_status']['value'];
13748 }
13749 $context['pluggable_data']['ad_id'] = 1;
13750 if ( isset( $data['filter']['ad_status']['value'] ) ) {
13751 $context['pluggable_data']['ad_status'] = $data['filter']['ad_status']['value'];
13752 }
13753 $context['response_type'] = 'sample';
13754 }
13755
13756 return $context;
13757 }
13758
13759 /**
13760 * Get Newsletter lists
13761 *
13762 * @param array $data data.
13763 *
13764 * @return array
13765 */
13766 public function search_newsletter_lists( $data ) {
13767
13768 $options = [];
13769 if ( class_exists( '\Newsletter' ) ) {
13770
13771 $lists = \Newsletter::instance()->get_lists();
13772
13773 if ( ! empty( $lists ) ) {
13774 foreach ( $lists as $list ) {
13775 $options[] = [
13776 'label' => $list->name,
13777 'value' => 'list_' . $list->id,
13778 ];
13779 }
13780 }
13781 }
13782
13783 return [
13784 'options' => $options,
13785 'hasMore' => false,
13786 ];
13787 }
13788
13789 /**
13790 * Newsletter pluggable data.
13791 *
13792 * @param array $data data.
13793 * @return array
13794 */
13795 public function search_newsletter_last_data( $data ) {
13796 $context = [];
13797 global $wpdb;
13798
13799 $list = $data['filter']['list_id']['value'];
13800
13801 if ( -1 == $list ) {
13802 $log = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . "newsletter where status='C' ORDER BY id DESC LIMIT 1" );
13803 } else {
13804 $num = $list;
13805 $location = 1;
13806 $sql = 'SELECT * FROM ' . $wpdb->prefix . "newsletter WHERE $num = %d AND status = 'C' ORDER BY id DESC LIMIT 1";
13807 $log = $wpdb->get_results( $wpdb->prepare( $sql, $location ), ARRAY_A );// @phpcs:ignore
13808 }
13809
13810 if ( ! empty( $log ) ) {
13811 $lists_arr = get_option( 'newsletter_lists' );
13812 if ( -1 == $list ) {
13813 foreach ( $log[0] as $key => $val ) {
13814 if ( defined( 'NEWSLETTER_LIST_MAX' ) ) {
13815 for ( $i = 1; $i <= NEWSLETTER_LIST_MAX; $i++ ) {
13816 $list_key = "list_$i";
13817
13818 if ( $key == $list_key ) {
13819 if ( 1 == $val ) {
13820 $context['pluggable_data']['list_id'] = $key;
13821 if ( is_array( $lists_arr ) ) {
13822 if ( isset( $lists_arr[ $key ] ) ) {
13823 $list_name = $lists_arr[ $key ];
13824 $context['pluggable_data']['list_name'] = $list_name;
13825 }
13826 }
13827 continue;
13828 }
13829 }
13830 }
13831 }
13832 if ( 'email' == $key ) {
13833 $context['pluggable_data']['email'] = $val;
13834 }
13835 }
13836 } else {
13837 $context['pluggable_data']['list_id'] = $list;
13838 $context['pluggable_data']['email'] = $log[0]['email'];
13839 if ( is_array( $lists_arr ) ) {
13840 if ( isset( $lists_arr[ $list ] ) ) {
13841 $list_name = $lists_arr[ $list ];
13842 $context['pluggable_data']['list_name'] = $list_name;
13843 }
13844 }
13845 }
13846 $context['response_type'] = 'live';
13847 } else {
13848 $context = json_decode( '{"response_type":"sample","pluggable_data":{"list_id": "list_1","email": "johnd@mailinator.com","list_name": "Contact List"}}', true );
13849 }
13850
13851 return $context;
13852 }
13853
13854 /**
13855 * Get wpForo Forum list
13856 *
13857 * @param array $data data.
13858 *
13859 * @return array
13860 */
13861 public function search_wp_forum_list( $data ) {
13862
13863 if ( ! function_exists( 'WPF' ) ) {
13864 return [];
13865 }
13866
13867 $forums = WPF()->forum->get_forums( [ 'type' => 'forum' ] );
13868
13869 $options = [];
13870
13871 if ( ! empty( $forums ) ) {
13872 foreach ( $forums as $forum ) {
13873 $options[] = [
13874 'label' => $forum['title'],
13875 'value' => $forum['forumid'],
13876 ];
13877 }
13878 }
13879
13880 return [
13881 'options' => $options,
13882 'hasMore' => false,
13883 ];
13884 }
13885
13886 /**
13887 * Get wpForo Topic list
13888 *
13889 * @param array $data data.
13890 *
13891 * @return array
13892 */
13893 public function search_wp_topic_list( $data ) {
13894
13895 if ( ! function_exists( 'WPF' ) ) {
13896 return [];
13897 }
13898
13899 $forum_id = $data['dynamic'];
13900
13901 $topics = WPF()->topic->get_topics( [ 'forumid' => $forum_id ] );
13902
13903 $options = [];
13904
13905 if ( ! empty( $topics ) ) {
13906 foreach ( $topics as $topic ) {
13907 $options[] = [
13908 'label' => $topic['title'],
13909 'value' => $topic['topicid'],
13910 ];
13911 }
13912 }
13913
13914 return [
13915 'options' => $options,
13916 'hasMore' => false,
13917 ];
13918 }
13919
13920 /**
13921 * Get wpForo Groups list
13922 *
13923 * @param array $data data.
13924 *
13925 * @return array
13926 */
13927 public function search_wp_user_groups_list( $data ) {
13928
13929 if ( ! function_exists( 'WPF' ) ) {
13930 return [];
13931 }
13932
13933 $usergroups = WPF()->usergroup->get_usergroups();
13934
13935 $options = [];
13936
13937 if ( ! empty( $usergroups ) ) {
13938 foreach ( $usergroups as $group ) {
13939 $options[] = [
13940 'label' => $group['name'],
13941 'value' => intval( $group['groupid'] ),
13942 ];
13943 }
13944 }
13945
13946 return [
13947 'options' => $options,
13948 'hasMore' => false,
13949 ];
13950 }
13951
13952 /**
13953 * Get wpForo Reputation list
13954 *
13955 * @param array $data data.
13956 *
13957 * @return array
13958 */
13959 public function search_wp_foro_reputation_list( $data ) {
13960
13961 if ( ! function_exists( 'WPF' ) ) {
13962 return [];
13963 }
13964
13965 $levels = WPF()->member->levels();
13966 $options = [];
13967
13968 if ( ! empty( $levels ) ) {
13969 foreach ( $levels as $level ) {
13970 $options[] = [
13971 'label' => esc_attr__( 'Level', 'suretriggers' ) . ' ' . $level . ' - ' . WPF()->member->rating( $level, 'title' ),
13972 'value' => strval( $level ),
13973 ];
13974 }
13975 }
13976
13977 return [
13978 'options' => $options,
13979 'hasMore' => false,
13980 ];
13981 }
13982
13983 /**
13984 * WPForo new topic pluggable data.
13985 *
13986 * @param array $data data.
13987 * @return array
13988 */
13989 public function search_pluggables_wpforo_topic_last_data( $data ) {
13990 $context = [];
13991 global $wpdb;
13992
13993 $forum_id = $data['filter']['forum_id']['value'];
13994
13995 if ( -1 == $forum_id ) {
13996 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'wpforo_topics WHERE closed = 0 ORDER BY topicid DESC LIMIT 1', ARRAY_A );
13997 } else {
13998 $forum = $forum_id;
13999 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_topics WHERE forumid = %d AND closed = 0 ORDER BY topicid DESC LIMIT 1';
14000 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum ), ARRAY_A );// @phpcs:ignore
14001 }
14002
14003 if ( ! empty( $results ) ) {
14004 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
14005 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
14006
14007 if ( function_exists( 'WPF' ) ) {
14008 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
14009 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
14010 }
14011 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
14012 $context['response_type'] = 'live';
14013 } else {
14014 $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
14015 }
14016
14017 return $context;
14018 }
14019
14020 /**
14021 * WpForo Topic Reply pluggable data.
14022 *
14023 * @param array $data data.
14024 * @return array
14025 */
14026 public function search_pluggables_wpforo_topic_reply_last_data( $data ) {
14027 $context = [];
14028 global $wpdb;
14029
14030 $forum_id = $data['filter']['forum_id']['value'];
14031 $topic_id = $data['filter']['topic_id']['value'];
14032
14033 if ( -1 == $forum_id && -1 != $topic_id ) {
14034 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE topicid = %d ORDER BY postid DESC LIMIT 1';
14035 $results = $wpdb->get_results( $wpdb->prepare( $sql, $topic_id ), ARRAY_A );// @phpcs:ignore
14036 } elseif ( -1 != $forum_id && -1 == $topic_id ) {
14037 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d ORDER BY postid DESC LIMIT 1';
14038 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id ), ARRAY_A );// @phpcs:ignore
14039 } elseif ( -1 == $forum_id && -1 == $topic_id ) {
14040 $sql = 'SELECT * from ' . $wpdb->prefix . 'wpforo_posts ORDER BY postid DESC LIMIT 1';
14041 $results = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );// @phpcs:ignore
14042 } else {
14043 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wpforo_posts WHERE forumid = %d AND topicid = %d ORDER BY postid DESC LIMIT 1';
14044 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id, $topic_id ), ARRAY_A );// @phpcs:ignore
14045 }
14046
14047 if ( ! empty( $results ) ) {
14048 $context['pluggable_data']['forum_id'] = $results[0]['forumid'];
14049 $context['pluggable_data']['topic_id'] = $results[0]['topicid'];
14050 if ( function_exists( 'WPF' ) ) {
14051 $context['pluggable_data']['forum'] = WPF()->forum->get_forum( $results[0]['forumid'] );
14052 $context['pluggable_data']['topic'] = WPF()->topic->get_topic( $results[0]['topicid'] );
14053 $context['pluggable_data']['reply'] = WPF()->post->get_post( $results[0]['postid'] );
14054 }
14055 $context['pluggable_data']['user'] = WordPress::get_user_context( $results[0]['userid'] );
14056 $context['response_type'] = 'live';
14057 } else {
14058 $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
14059 }
14060
14061 return $context;
14062 }
14063
14064 /**
14065 * Get RafflePress Giveaways list
14066 *
14067 * @param array $data data.
14068 *
14069 * @return array
14070 */
14071 public function search_rp_giveaways_list( $data ) {
14072
14073 global $wpdb;
14074 $options = [];
14075
14076 $giveaways = $wpdb->get_results( "SELECT id,name FROM {$wpdb->prefix}rafflepress_giveaways WHERE deleted_at is null ORDER BY name ASC", ARRAY_A );
14077 foreach ( $giveaways as $giveaway ) {
14078 $options[] = [
14079 'label' => $giveaway['name'],
14080 'value' => $giveaway['id'],
14081 ];
14082 }
14083
14084 return [
14085 'options' => $options,
14086 'hasMore' => false,
14087 ];
14088 }
14089
14090 /**
14091 * Get RafflePress Giveaway Actions list
14092 *
14093 * @param array $data data.
14094 *
14095 * @return array
14096 */
14097 public function search_rp_giveaway_actions_list( $data ) {
14098
14099 global $wpdb;
14100 $options = [];
14101
14102 $giveaway = $wpdb->get_row( $wpdb->prepare( "SELECT settings FROM {$wpdb->prefix}rafflepress_giveaways WHERE id=%d", $data['dynamic'] ), ARRAY_A );
14103
14104 if ( is_array( $giveaway ) && isset( $giveaway['settings'] ) ) {
14105 $settings = json_decode( $giveaway['settings'], true );
14106
14107 if ( is_array( $settings ) && isset( $settings['entry_options'] ) ) {
14108 foreach ( $settings['entry_options'] as $action ) {
14109 $options[] = [
14110 'label' => $action['name'],
14111 'value' => $action['id'],
14112 ];
14113 }
14114 }
14115 }
14116
14117 return [
14118 'options' => $options,
14119 'hasMore' => false,
14120 ];
14121 }
14122
14123 /**
14124 * Get last data for trigger.
14125 *
14126 * @param array $data data.
14127 * @return array
14128 */
14129 public function search_raffle_press_triggers_last_data( $data ) {
14130
14131 global $wpdb;
14132
14133 $context = [];
14134 $context['response_type'] = 'sample';
14135
14136 $pluggable_data = [
14137 'giveaway_id' => 1,
14138 'giveaway_title' => 'Sample Giveaway (ID#1)',
14139 'giveaway_start_date' => 'September 20, 2023',
14140 'giveaway_end_date' => 'October 6, 2023',
14141 'giveaway_entries' => 9,
14142 'giveaway_user_count' => 3,
14143 'giveaway_status' => 'Active',
14144 'contestant_name' => 'John Doe',
14145 'contestant_email' => 'john_doe@gmail.com',
14146 'contestant_email_verified' => 'Yes',
14147 'action_id' => '0jnex',
14148 'action_name' => 'Visit us on Instagram',
14149 ];
14150
14151 $giveaway_id = isset( $data['filter']['giveaway_id'] ) ? $data['filter']['giveaway_id']['value'] : null;
14152 $action_id = isset( $data['filter']['action_id'] ) ? $data['filter']['action_id']['value'] : null;
14153
14154 $query = "SELECT contestant_id, giveaway_id, action_id, meta FROM {$wpdb->prefix}rafflepress_entries";
14155
14156 if ( $giveaway_id && -1 != $giveaway_id ) {
14157 $query .= ' WHERE giveaway_id = ' . $giveaway_id;
14158
14159 if ( $action_id ) {
14160 $query .= " AND action_id = '" . $action_id . "'";
14161 }
14162 }
14163
14164 $query .= ' ORDER BY created_at DESC LIMIT 1';
14165
14166 $giveaway_data = $wpdb->get_row( $query, ARRAY_A ); // @phpcs:ignore
14167
14168 if ( ! empty( $giveaway_data ) ) {
14169 $pluggable_data = array_merge(
14170 RafflePress::get_giveaway_context( $giveaway_data['giveaway_id'] ),
14171 RafflePress::get_contestant_context( $giveaway_data['contestant_id'] )
14172 );
14173
14174 $pluggable_data['performed_action_id'] = isset( $giveaway_data['action_id'] ) ? $giveaway_data['action_id'] : 0;
14175
14176 $giveaway_meta = isset( $giveaway_data['meta'] ) ? json_decode( $giveaway_data['meta'], true ) : [];
14177 $pluggable_data['performed_action_name'] = is_array( $giveaway_meta ) && isset( $giveaway_meta['action'] ) ? $giveaway_meta['action'] : '';
14178
14179 $context['response_type'] = 'live';
14180 }
14181
14182 $context['pluggable_data'] = $pluggable_data;
14183
14184 return $context;
14185 }
14186
14187 /**
14188 * Get last data for trigger
14189 *
14190 * @param array $data data.
14191 * @return array
14192 */
14193 public function search_woo_commerce_shipstation_triggers_last_data( $data ) {
14194 $context = [];
14195 $context['response_type'] = 'sample';
14196 $context['pluggable_data'] = [];
14197
14198 $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
14199
14200 $product_id = (int) ( isset( $data['filter']['product_id']['value'] ) ? $data['filter']['product_id']['value'] : -1 );
14201 $condition = $data['filter']['condition_compare']['value'];
14202 $price = $data['filter']['price']['value'];
14203 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
14204
14205 if ( 'order_shipped' === $term ) {
14206 $orders = wc_get_orders(
14207 [
14208 'numberposts' => 1,
14209 'orderby' => 'date',
14210 'order' => 'DESC',
14211 'status' => 'completed',
14212 'meta_query' => [
14213 [
14214 'key' => '_shipstation_shipped_item_count',
14215 'compare' => 'EXISTS',
14216 ],
14217 ],
14218 ]
14219 );
14220 if ( count( $orders ) > 0 ) {
14221 $order_id = $orders[0]->get_id();
14222 $order = wc_get_order( $order_id );
14223 $user_id = $order->get_customer_id();
14224 $product_ids = [];
14225 if ( $order ) {
14226 $items = $order->get_items();
14227 foreach ( $items as $item ) {
14228 if ( method_exists( $item, 'get_product_id' ) ) {
14229 $product_ids[] = $item->get_product_id();
14230 }
14231 }
14232
14233 foreach ( $product_ids as $product_id ) {
14234 $context['product_id'] = $product_id;
14235 }
14236 }
14237 $context = array_merge(
14238 WooCommerce::get_order_context( $order_id ),
14239 WordPress::get_user_context( $user_id )
14240 );
14241 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
14242 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
14243 /**
14244 *
14245 * Ignore line
14246 *
14247 * @phpstan-ignore-next-line
14248 */
14249 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
14250 /**
14251 *
14252 * Ignore line
14253 *
14254 * @phpstan-ignore-next-line
14255 */
14256 $date = date_i18n( get_option( 'date_format' ), $timestamp );
14257 $context['ship_date'] = $date;
14258 $context['response_type'] = 'live';
14259 }
14260
14261 $context['pluggable_data'] = $context;
14262 } elseif ( 'specific_product_order_shipped' === $term ) {
14263 if ( -1 != $product_id ) {
14264 $product_ids = [ $product_id ];
14265 $orders = wc_get_orders(
14266 [
14267 'numberposts' => 1,
14268 'orderby' => 'date',
14269 'order' => 'DESC',
14270 'status' => 'completed',
14271 'meta_query' => [
14272 [
14273 'key' => '_shipstation_shipped_item_count',
14274 'compare' => 'EXISTS',
14275 ],
14276 [
14277 'key' => '_product_id',
14278 'value' => $product_ids,
14279 'compare' => 'IN',
14280 ],
14281 ],
14282 ]
14283 );
14284 } else {
14285 $orders = wc_get_orders(
14286 [
14287 'numberposts' => 1,
14288 'orderby' => 'date',
14289 'order' => 'DESC',
14290 'status' => 'completed',
14291 'meta_query' => [
14292 [
14293 'key' => '_shipstation_shipped_item_count',
14294 'compare' => 'EXISTS',
14295 ],
14296 ],
14297 ]
14298 );
14299 }
14300 if ( count( $orders ) > 0 ) {
14301 $order_id = $orders[0]->get_id();
14302 $order = wc_get_order( $order_id );
14303 $user_id = $order->get_customer_id();
14304 $productids = [];
14305 $items = $order->get_items();
14306 foreach ( $items as $item ) {
14307 if ( method_exists( $item, 'get_product_id' ) ) {
14308 $productids[] = $item->get_product_id();
14309 }
14310 }
14311 $context = array_merge(
14312 WooCommerce::get_order_context( $order_id ),
14313 WordPress::get_user_context( $user_id )
14314 );
14315
14316 foreach ( $productids as $product_id ) {
14317 $context['product_id'] = $product_id;
14318 }
14319
14320 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
14321 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
14322 /**
14323 *
14324 * Ignore line
14325 *
14326 * @phpstan-ignore-next-line
14327 */
14328 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
14329 /**
14330 *
14331 * Ignore line
14332 *
14333 * @phpstan-ignore-next-line
14334 */
14335 $date = date_i18n( get_option( 'date_format' ), $timestamp );
14336 $context['ship_date'] = $date;
14337 $context['response_type'] = 'live';
14338 }
14339
14340 $context['pluggable_data'] = $context;
14341 } elseif ( 'specific_amount_order_shipped' === $term ) {
14342 $orders = wc_get_orders(
14343 [
14344 'numberposts' => 1,
14345 'orderby' => 'date',
14346 'order' => 'DESC',
14347 'status' => 'completed',
14348 'meta_query' => [
14349 [
14350 'key' => '_shipstation_shipped_item_count',
14351 'compare' => 'EXISTS',
14352 ],
14353 [
14354 'key' => '_order_total',
14355 'value' => $price,
14356 'compare' => $condition,
14357 ],
14358 ],
14359 ]
14360 );
14361 if ( count( $orders ) > 0 ) {
14362 $order_id = $orders[0]->get_id();
14363 $order = wc_get_order( $order_id );
14364 $user_id = $order->get_customer_id();
14365 $product_ids = [];
14366 $items = $order->get_items();
14367 foreach ( $items as $item ) {
14368 if ( method_exists( $item, 'get_product_id' ) ) {
14369 $product_ids[] = $item->get_product_id();
14370 }
14371 }
14372 $context = array_merge(
14373 WooCommerce::get_order_context( $order_id ),
14374 WordPress::get_user_context( $user_id )
14375 );
14376
14377 foreach ( $product_ids as $product_id ) {
14378 $context['product_id'] = $product_id;
14379 }
14380
14381 $context['shipping_tracking_number'] = $order->get_meta( '_tracking_number', true );
14382 $context['shipping_carrier'] = $order->get_meta( '_tracking_provider', true );
14383 /**
14384 *
14385 * Ignore line
14386 *
14387 * @phpstan-ignore-next-line
14388 */
14389 $timestamp = strtotime( $order->get_meta( '_date_shipped', true ) );
14390 /**
14391 *
14392 * Ignore line
14393 *
14394 * @phpstan-ignore-next-line
14395 */
14396 $date = date_i18n( get_option( 'date_format' ), $timestamp );
14397 $context['ship_date'] = $date;
14398 $context['response_type'] = 'live';
14399 }
14400
14401 $context['pluggable_data'] = $context;
14402 }
14403
14404 return $context;
14405 }
14406
14407 /**
14408 * Get GroundHogg Tag list
14409 *
14410 * @param array $data data.
14411 *
14412 * @return array
14413 */
14414 public function search_groundhogg_tag_list( $data ) {
14415
14416 if ( ! function_exists( 'Groundhogg\get_db' ) ) {
14417 return [];
14418 }
14419
14420 $tags = \Groundhogg\get_db( 'tags' )->query( [] );
14421 $options = [];
14422
14423 if ( ! empty( $tags ) ) {
14424 foreach ( $tags as $tag ) {
14425 $options[] = [
14426 'label' => $tag->tag_name,
14427 'value' => $tag->tag_id,
14428 ];
14429 }
14430 }
14431
14432 return [
14433 'options' => $options,
14434 'hasMore' => false,
14435 ];
14436 }
14437
14438 /**
14439 * Get WP courseware courses list
14440 *
14441 * @param array $data data.
14442 *
14443 * @return array
14444 */
14445 public function search_wpcw_courses( $data ) {
14446 $options = [];
14447
14448 $page = $data['page'];
14449 $limit = Utilities::get_search_page_limit();
14450 $offset = $limit * ( $page - 1 );
14451
14452 $args = [
14453 'post_type' => 'wpcw_course',
14454 'posts_per_page' => $limit,
14455 'offset' => $offset,
14456 'orderby' => 'title',
14457 'order' => 'ASC',
14458 'post_status' => 'publish',
14459 ];
14460
14461 $courses = get_posts( $args );
14462
14463 $course_count = wp_count_posts( 'wpcw_course' )->publish;
14464
14465 if ( ! empty( $courses ) ) {
14466 if ( is_array( $courses ) ) {
14467 foreach ( $courses as $course ) {
14468 $options[] = [
14469 'label' => $course->post_title,
14470 'value' => $course->ID,
14471 ];
14472 }
14473 }
14474 }
14475 return [
14476 'options' => $options,
14477 'hasMore' => $course_count > $limit && $course_count > $offset,
14478 ];
14479 }
14480
14481 /**
14482 * Get WP courseware courses list
14483 *
14484 * @param array $data data.
14485 *
14486 * @return array
14487 */
14488 public function search_wpcw_modules( $data ) {
14489 $options = [];
14490
14491 if ( function_exists( 'wpcw_get_modules' ) ) {
14492 $modules = wpcw_get_modules();
14493 }
14494
14495 if ( ! empty( $modules ) ) {
14496 if ( is_array( $modules ) ) {
14497 foreach ( $modules as $module ) {
14498 $options[] = [
14499 'label' => $module->module_title,
14500 'value' => $module->module_id,
14501 ];
14502 }
14503 }
14504 }
14505 return [
14506 'options' => $options,
14507 'hasMore' => false,
14508 ];
14509 }
14510
14511 /**
14512 * Get WP courseware unit list
14513 *
14514 * @param array $data data.
14515 *
14516 * @return array
14517 */
14518 public function search_wpcw_units( $data ) {
14519 $options = [];
14520
14521 $page = $data['page'];
14522 $limit = Utilities::get_search_page_limit();
14523 $offset = $limit * ( $page - 1 );
14524
14525 $args = [
14526 'post_type' => 'course_unit',
14527 'posts_per_page' => $limit,
14528 'offset' => $offset,
14529 'orderby' => 'title',
14530 'order' => 'ASC',
14531 'post_status' => 'publish',
14532 ];
14533
14534 $units = get_posts( $args );
14535
14536 $unit_count = wp_count_posts( 'course_unit' )->publish;
14537
14538 if ( ! empty( $units ) ) {
14539 if ( is_array( $units ) ) {
14540 foreach ( $units as $unit ) {
14541 $options[] = [
14542 'label' => $unit->post_title,
14543 'value' => $unit->ID,
14544 ];
14545 }
14546 }
14547 }
14548 return [
14549 'options' => $options,
14550 'hasMore' => $unit_count > $limit && $unit_count > $offset,
14551 ];
14552 }
14553
14554 /**
14555 * Search WP Courseware data.
14556 *
14557 * @param array $data data.
14558 * @return array|void
14559 */
14560 public function search_wpcw_last_data( $data ) {
14561 global $wpdb;
14562 $post_type = $data['post_type'];
14563 $trigger = $data['search_term'];
14564 $context = [];
14565
14566 if ( 'wpcw_course_completed' === $trigger ) {
14567 $post_id = $data['filter']['course_post_id']['value'];
14568 if ( -1 === $post_id ) {
14569 $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" );
14570 } else {
14571 $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 ) );
14572 }
14573 } elseif ( 'wpcw_module_completed' === $trigger ) {
14574 $post_id = $data['filter']['module_id']['value'];
14575 if ( -1 === $post_id ) {
14576 $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" );
14577 } else {
14578 $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 ) );
14579 }
14580 } elseif ( 'wpcw_unit_completed' === $trigger ) {
14581 $post_id = $data['filter']['unit_id']['value'];
14582 if ( -1 === $post_id ) {
14583 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpcw_user_progress WHERE unit_completed_status='complete' order by unit_id DESC LIMIT 1" );
14584 } else {
14585 $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 ) );
14586 }
14587 } elseif ( 'wpcw_enroll_course' === $trigger ) {
14588 $post_id = $data['filter']['course_post_id']['value'];
14589 if ( -1 === $post_id ) {
14590 $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" );
14591 } else {
14592 $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 ) );
14593 }
14594 }
14595
14596 if ( ! empty( $result ) ) {
14597
14598 switch ( $trigger ) {
14599 case 'wpcw_course_completed':
14600 $result_course_id = $result[0]->course_id;
14601 $result_user_id = $result[0]->user_id;
14602 if ( function_exists( 'wpcw_get_course' ) ) {
14603 $course = wpcw_get_course( $result_course_id );
14604 if ( is_object( $course ) ) {
14605 $course = get_object_vars( $course );
14606 }
14607 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course );
14608 }
14609 break;
14610 case 'wpcw_module_completed':
14611 $result_module_id = $result[0]->module_id;
14612 $result_user_id = $result[0]->user_id;
14613 if ( function_exists( 'wpcw_get_module' ) ) {
14614 $module = wpcw_get_module( $result_module_id );
14615 if ( is_object( $module ) ) {
14616 $module = get_object_vars( $module );
14617 }
14618 $context = array_merge( WordPress::get_user_context( $result_user_id ), $module );
14619 }
14620 break;
14621 case 'wpcw_unit_completed':
14622 $result_unit_id = $result[0]->unit_id;
14623 $result_user_id = $result[0]->user_id;
14624 if ( function_exists( 'wpcw_get_unit' ) ) {
14625 $unit = wpcw_get_unit( $result_unit_id );
14626 if ( is_object( $unit ) ) {
14627 $unit = get_object_vars( $unit );
14628 $unit['name'] = get_the_title( $result_unit_id );
14629 }
14630 $context = array_merge( WordPress::get_user_context( $result_user_id ), $unit );
14631 }
14632 break;
14633 case 'wpcw_enroll_course':
14634 $result_course_id = $result[0]->course_id;
14635 $result_user_id = $result[0]->user_id;
14636 if ( function_exists( 'WPCW_courses_getCourseDetails' ) ) {
14637 $course_detail = WPCW_courses_getCourseDetails( $result_course_id );
14638 if ( is_object( $course_detail ) ) {
14639 $course_detail = get_object_vars( $course_detail );
14640 }
14641 $context = array_merge( WordPress::get_user_context( $result_user_id ), $course_detail );
14642 }
14643 break;
14644 default:
14645 return;
14646 }
14647 $context['pluggable_data'] = $context;
14648 $context['response_type'] = 'live';
14649 }
14650
14651 return $context;
14652
14653 }
14654
14655 /**
14656 * Get WooCommerce Order Note list.
14657 *
14658 * @param array $data data.
14659 *
14660 * @return array
14661 */
14662 public function search_note_type_list( $data ) {
14663
14664 $options = [];
14665
14666 $options[] = [
14667 'label' => 'Customer',
14668 'value' => 'customer',
14669 ];
14670 $options[] = [
14671 'label' => 'Private',
14672 'value' => 'internal',
14673 ];
14674
14675 return [
14676 'options' => $options,
14677 'hasMore' => false,
14678 ];
14679 }
14680
14681 /**
14682 * Get last data for trigger
14683 *
14684 * @param array $data data.
14685 * @return array
14686 */
14687 public function search_woo_commerce_customers_triggers_last_data( $data ) {
14688 $context = [];
14689 $context['response_type'] = 'sample';
14690 $context['pluggable_data'] = [];
14691 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
14692 global $wpdb;
14693 if ( 'customer_created' === $term ) {
14694 $customer_query = get_users(
14695 [
14696 'fields' => 'ID',
14697 'role' => 'customer',
14698 'orderby' => 'ID',
14699 'order' => 'DESC',
14700 'number' => 1,
14701 ]
14702 );
14703 $results = $customer_query;
14704 if ( ! empty( $results ) ) {
14705 $customer = new WC_Customer( $results[0] );
14706 $last_order = $customer->get_last_order();
14707 $customer_data = [
14708 'id' => $customer->get_id(),
14709 'email' => $customer->get_email(),
14710 'first_name' => $customer->get_first_name(),
14711 'last_name' => $customer->get_last_name(),
14712 'username' => $customer->get_username(),
14713 'last_order_id' => is_object( $last_order ) ? $last_order->get_id() : null,
14714 'orders_count' => $customer->get_order_count(),
14715 'total_spent' => wc_format_decimal( $customer->get_total_spent(), 2 ),
14716 'avatar_url' => $customer->get_avatar_url(),
14717 'billing_address' => [
14718 'first_name' => $customer->get_billing_first_name(),
14719 'last_name' => $customer->get_billing_last_name(),
14720 'company' => $customer->get_billing_company(),
14721 'address_1' => $customer->get_billing_address_1(),
14722 'address_2' => $customer->get_billing_address_2(),
14723 'city' => $customer->get_billing_city(),
14724 'state' => $customer->get_billing_state(),
14725 'postcode' => $customer->get_billing_postcode(),
14726 'country' => $customer->get_billing_country(),
14727 'email' => $customer->get_billing_email(),
14728 'phone' => $customer->get_billing_phone(),
14729 ],
14730 'shipping_address' => [
14731 'first_name' => $customer->get_shipping_first_name(),
14732 'last_name' => $customer->get_shipping_last_name(),
14733 'company' => $customer->get_shipping_company(),
14734 'address_1' => $customer->get_shipping_address_1(),
14735 'address_2' => $customer->get_shipping_address_2(),
14736 'city' => $customer->get_shipping_city(),
14737 'state' => $customer->get_shipping_state(),
14738 'postcode' => $customer->get_shipping_postcode(),
14739 'country' => $customer->get_shipping_country(),
14740 ],
14741 ];
14742 if ( is_object( $last_order ) && method_exists( $last_order, 'get_date_created' ) ) {
14743 $created_date = $last_order->get_date_created();
14744 if ( is_object( $created_date ) && method_exists( $created_date, 'getTimestamp' ) ) {
14745 $last_order_date = $created_date->getTimestamp();
14746 $customer_data['created_at'] = $last_order_date;
14747 $customer_data['last_order_date'] = $last_order_date;
14748 }
14749 }
14750 $order_sample_data = $customer_data;
14751 $context['response_type'] = 'live';
14752 $context['pluggable_data'] = $order_sample_data;
14753 } else {
14754 $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 );
14755 $context['pluggable_data'] = $order_sample_data;
14756 }
14757 } elseif ( 'total_spend_reach' === $term ) {
14758 $total_spend_selected = isset( $data['filter']['total_spend']['value'] ) ? $data['filter']['total_spend']['value'] : '';
14759 $customers = $wpdb->get_col(
14760 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
14761 WHERE meta_key = '_customer_user' AND meta_value > 0"
14762 );
14763 $target_customer = null;
14764 foreach ( $customers as $customer ) {
14765 $total_spend = (int) wc_get_customer_total_spent( $customer );
14766 if ( $total_spend == $total_spend_selected ) {
14767 $target_customer = $customer;
14768 break; // Exit the loop once a matching customer is found.
14769 }
14770 }
14771 if ( $target_customer ) {
14772 $new_customer = new WC_Customer( $target_customer );
14773 $new_customer_data = [
14774 'id' => $new_customer->get_id(),
14775 'email' => $new_customer->get_email(),
14776 'first_name' => $new_customer->get_first_name(),
14777 'last_name' => $new_customer->get_last_name(),
14778 'username' => $new_customer->get_username(),
14779 'order_count' => $new_customer->get_order_count(),
14780 'total_spend' => wc_format_decimal( $new_customer->get_total_spent(), 2 ),
14781 ];
14782 $context['response_type'] = 'live';
14783 $context['pluggable_data'] = $new_customer_data;
14784 } else {
14785 $sample_customer_data = [
14786 'id' => '101',
14787 'created_at' => '1680675247',
14788 'email' => 'john@d.com',
14789 'first_name' => 'John',
14790 'last_name' => 'D',
14791 'username' => 'johnd',
14792 'order_count' => '3',
14793 'total_spend' => '22.00',
14794 ];
14795 $context['pluggable_data'] = $sample_customer_data;
14796 }
14797 } elseif ( 'order_count_reach' === $term ) {
14798 $total_order_selected = isset( $data['filter']['order_count']['value'] ) ? $data['filter']['order_count']['value'] : '';
14799 $customers = $wpdb->get_col(
14800 "SELECT DISTINCT meta_value FROM $wpdb->postmeta
14801 WHERE meta_key = '_customer_user' AND meta_value > 0"
14802 );
14803 $target_customer = null;
14804 foreach ( $customers as $customer ) {
14805 $args = [
14806 'customer_id' => $customer,
14807 'limit' => -1,
14808 'status' => [ 'wc-completed' ],
14809 ];
14810 $orders = wc_get_orders( $args );
14811 if ( ! empty( $orders ) ) {
14812 $total_order = (int) wc_get_customer_order_count( $customer );
14813 if ( $total_order == $total_order_selected ) {
14814 $target_customer = $customer;
14815 break; // Exit the loop once a matching customer is found.
14816 }
14817 }
14818 }
14819 if ( $target_customer ) {
14820 $new_customer = new WC_Customer( $target_customer );
14821 $new_customer_data = [
14822 'id' => $new_customer->get_id(),
14823 'email' => $new_customer->get_email(),
14824 'first_name' => $new_customer->get_first_name(),
14825 'last_name' => $new_customer->get_last_name(),
14826 'username' => $new_customer->get_username(),
14827 'order_count' => $new_customer->get_order_count(),
14828 ];
14829 $context['response_type'] = 'live';
14830 $context['pluggable_data'] = $new_customer_data;
14831 } else {
14832 $sample_customer_data = [
14833 'id' => '101',
14834 'created_at' => '1680675247',
14835 'email' => 'john@d.com',
14836 'first_name' => 'John',
14837 'last_name' => 'D',
14838 'username' => 'johnd',
14839 'orders_count' => '3',
14840 ];
14841 $context['pluggable_data'] = $sample_customer_data;
14842 }
14843 }
14844
14845 return $context;
14846 }
14847
14848 /**
14849 * Get Affiliate list
14850 *
14851 * @param array $data data.
14852 *
14853 * @return array
14854 */
14855 public function search_affiliate_list( $data ) {
14856 $options = [];
14857
14858 $args = [
14859 'meta_query' => [
14860 [
14861 'key' => 'wafp_is_affiliate',
14862 'value' => '1',
14863 'compare' => '=',
14864 ],
14865 ],
14866 ];
14867
14868 $affiliates = get_users( $args );
14869
14870 foreach ( $affiliates as $user ) {
14871 $options[] = [
14872 'label' => $user->display_name,
14873 'value' => $user->ID,
14874 ];
14875 }
14876 return [
14877 'options' => $options,
14878 'hasMore' => false,
14879 ];
14880 }
14881
14882 /**
14883 * Get Affiliate Source list
14884 *
14885 * @param array $data data.
14886 *
14887 * @return array
14888 */
14889 public function search_affiliate_transaction_source_list( $data ) {
14890 $options = [];
14891
14892 $sources = [
14893 'General',
14894 'MemberPress',
14895 'WooCommerce',
14896 'Easy Digital Downloads',
14897 'WPForms',
14898 'Formidable',
14899 'PayPal',
14900 ];
14901 foreach ( $sources as $source ) {
14902 $options[] = [
14903 'label' => $source,
14904 'value' => str_replace( ' ', '_', strtolower( $source ) ),
14905 ];
14906 }
14907
14908 return [
14909 'options' => $options,
14910 'hasMore' => false,
14911 ];
14912 }
14913
14914 /**
14915 * Search Easy Affiliate data.
14916 *
14917 * @param array $data data.
14918 * @return array|void
14919 */
14920 public function search_easy_affiliate_last_data( $data ) {
14921
14922 global $wpdb;
14923 $trigger = $data['search_term'];
14924 $context = [];
14925
14926 if ( 'sale_recorded' === $trigger ) {
14927 $affiliate_id = $data['filter']['affiliate_id']['value'];
14928 if ( -1 === $affiliate_id ) {
14929 $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" );
14930 } else {
14931 $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 ) );
14932 }
14933 } elseif ( 'sale_added' === $trigger ) {
14934 $orderby = 'signup_date';
14935 $order = 'DESC';
14936 $paged = 1;
14937 $search = '';
14938 $perpage = 1;
14939 /**
14940 *
14941 * Ignore line
14942 *
14943 * @phpstan-ignore-next-line
14944 */
14945 $result = \EasyAffiliate\Models\User::affiliate_list_table( $orderby, $order, $paged, $search, $perpage );
14946 if ( empty( $result['results'] ) ) {
14947 $result = [];
14948 }
14949 } elseif ( 'payout_made' === $trigger ) {
14950 $affiliate_id = $data['filter']['affiliate_id']['value'];
14951 if ( -1 === $affiliate_id ) {
14952 $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" );
14953 } else {
14954 $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 ) );
14955 }
14956 }
14957
14958 if ( ! empty( $result ) ) {
14959 switch ( $trigger ) {
14960 case 'sale_recorded':
14961 $result_affiliate_id = $result[0]->affiliate_id;
14962 /**
14963 *
14964 * Ignore line
14965 *
14966 * @phpstan-ignore-next-line
14967 */
14968 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
14969 $affiliate = get_object_vars( $affiliate->rec );
14970 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
14971 break;
14972 case 'sale_added':
14973 /**
14974 *
14975 * Ignore line
14976 *
14977 * @phpstan-ignore-next-line
14978 */
14979 $data = new \EasyAffiliate\Models\User( $result['results'][0]->ID );
14980 /**
14981 *
14982 * Ignore line
14983 *
14984 * @phpstan-ignore-next-line
14985 */
14986 $context = get_object_vars( $data->rec );
14987 break;
14988 case 'payout_made':
14989 $result_affiliate_id = $result[0]->affiliate_id;
14990 /**
14991 *
14992 * Ignore line
14993 *
14994 * @phpstan-ignore-next-line
14995 */
14996 $affiliate = \EasyAffiliate\Lib\ModelFactory::fetch( $result[0]->evt_id_type, $result[0]->evt_id );
14997 $affiliate = get_object_vars( $affiliate->rec );
14998 $context = array_merge( WordPress::get_user_context( $result_affiliate_id ), $affiliate );
14999 break;
15000 default:
15001 return;
15002 }
15003 $context['pluggable_data'] = $context;
15004 $context['response_type'] = 'live';
15005 } elseif ( empty( $result ) ) {
15006 switch ( $trigger ) {
15007 case 'sale_added':
15008 $context = json_decode(
15009 '{"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": "",
15010 "city": "london","state": "","zip": "","country": "UK","tax_id_us": "","tax_id_int": "","is_affiliate": "1","is_blocked": "","blocked_message": "","referrer": "0","notes": "","unsubscribed": ""}',
15011 true
15012 );
15013 break;
15014 case 'sale_recorded':
15015 $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 );
15016 break;
15017 case 'payout_made':
15018 $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 );
15019 break;
15020 default:
15021 return;
15022 }
15023 $context['pluggable_data'] = $context;
15024 $context['response_type'] = 'sample';
15025 }
15026
15027 return $context;
15028
15029 }
15030
15031 /**
15032 * Get WooCommerce Subscriptions Coupon list
15033 *
15034 * @param array $data data.
15035 *
15036 * @return array
15037 */
15038 public function search_woo_subscription_coupon_list( $data ) {
15039 $options = [];
15040
15041 $coupon_codes = [];
15042
15043 $query = new \WP_Query(
15044 [
15045 'post_type' => 'shop_coupon',
15046 'posts_per_page' => -1,
15047 'no_found_rows' => true,
15048 'meta_query' => [
15049 [
15050 'key' => '_is_aw_coupon',
15051 'compare' => 'NOT EXISTS',
15052 ],
15053 ],
15054 ]
15055 );
15056
15057 foreach ( $query->posts as $coupon ) {
15058 /**
15059 *
15060 * Ignore line
15061 *
15062 * @phpstan-ignore-next-line
15063 */
15064 $code = wc_format_coupon_code( $coupon->post_title );
15065 $coupon_codes[ $code ] = $code;
15066 }
15067
15068 foreach ( $coupon_codes as $code ) {
15069
15070 $coupon = new \WC_Coupon( $code );
15071
15072 if ( ! in_array( $coupon->get_discount_type(), [ 'recurring_fee', 'recurring_percent' ], true ) ) {
15073 unset( $coupon_codes[ $code ] );
15074 }
15075 }
15076
15077 if ( ! empty( $coupon_codes ) ) {
15078 if ( is_array( $coupon_codes ) ) {
15079 foreach ( $coupon_codes as $coupon_code ) {
15080 $options[] = [
15081 'label' => $coupon_code,
15082 'value' => $coupon_code,
15083 ];
15084 }
15085 }
15086 }
15087 return [
15088 'options' => $options,
15089 'hasMore' => false,
15090 ];
15091 }
15092
15093 /**
15094 * Get BuddyBoss field.
15095 *
15096 * @param array $data data.
15097 *
15098 * @return array|void
15099 */
15100 public function search_bb_field_list( $data ) {
15101 global $wpdb;
15102 $options = [];
15103
15104 $xprofile_fields = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}bp_xprofile_fields WHERE parent_id = 0 ORDER BY field_order ASC" );
15105 if ( ! empty( $xprofile_fields ) ) {
15106 foreach ( $xprofile_fields as $xprofile_field ) {
15107 $options[] = [
15108 'label' => $xprofile_field->name,
15109 'value' => $xprofile_field->id,
15110 ];
15111 }
15112 }
15113
15114 return [
15115 'options' => $options,
15116 'hasMore' => false,
15117 ];
15118 }
15119
15120 /**
15121 * Get Woocommerce Bookings Product List.
15122 *
15123 * @param array $data data.
15124 *
15125 * @return array|void
15126 */
15127 public function search_wb_bookable_product_list( $data ) {
15128 $options = [];
15129
15130 if ( ! class_exists( 'WC_Bookings_Admin' ) ) {
15131 return;
15132 }
15133
15134 $products = WC_Bookings_Admin::get_booking_products();
15135 if ( ! empty( $products ) ) {
15136 if ( is_array( $products ) ) {
15137 foreach ( $products as $product ) {
15138 $options[] = [
15139 'label' => $product->get_name(),
15140 'value' => $product->get_id(),
15141 ];
15142 $resources = $product->get_resources();
15143 if ( ! empty( $resources ) ) {
15144 foreach ( $resources as $resource ) {
15145 $options[] = [
15146 'label' => $resource->get_name(),
15147 'value' => $resource->get_id(),
15148 ];
15149 }
15150 }
15151 }
15152 }
15153 }
15154
15155 return [
15156 'options' => $options,
15157 'hasMore' => false,
15158 ];
15159 }
15160
15161 /**
15162 * Get last data for WooCommerce Booking trigger
15163 *
15164 * @param array $data data.
15165 * @return array
15166 */
15167 public function search_woo_commerce_booking_last_data( $data ) {
15168 $context = [];
15169 $context['response_type'] = 'sample';
15170 $context['pluggable_data'] = [];
15171 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
15172 global $wpdb;
15173 if ( 'booking_created' === $term ) {
15174 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' AND post_status='confirmed' order by ID DESC LIMIT 1" );
15175
15176 if ( ! empty( $results ) ) {
15177 if ( class_exists( 'WC_Booking' ) ) {
15178 $booking = new WC_Booking( $results[0]->ID );
15179 $person_counts = $booking->get_person_counts();
15180 $bookable_product_id = $booking->get_product_id();
15181 if ( method_exists( $booking, 'get_data' ) ) {
15182 $booking = $booking->get_data();
15183 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
15184 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
15185 if ( ! empty( $person_counts ) ) {
15186 $total_count = 0;
15187 foreach ( $person_counts as $key => $value ) {
15188 $total_count += $value;
15189 }
15190 $booking['total_person_counts'] = $total_count;
15191 }
15192 $booking['bookable_product'] = $bookable_product_id;
15193 $context['response_type'] = 'live';
15194 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
15195 }
15196 }
15197 } else {
15198 $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 );
15199 $context['pluggable_data'] = $booking_sample_data;
15200 }
15201 } elseif ( 'booking_status_changed' === $term ) {
15202 $to_status = isset( $data['filter']['to_status']['value'] ) ? $data['filter']['to_status']['value'] : -1;
15203
15204 if ( -1 == $to_status ) {
15205 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type='wc_booking' order by ID DESC LIMIT 1" );
15206 } else {
15207 $results = $wpdb->get_results(
15208 $wpdb->prepare(
15209 "
15210 SELECT *
15211 FROM {$wpdb->prefix}posts
15212 WHERE `post_status` = %s
15213 AND `post_type` LIKE %s
15214 ",
15215 $to_status,
15216 'wc_booking'
15217 )
15218 );
15219 }
15220
15221 if ( ! empty( $results ) ) {
15222 if ( class_exists( 'WC_Booking' ) ) {
15223 $booking = new WC_Booking( $results[0]->ID );
15224 $person_counts = $booking->get_person_counts();
15225 $bookable_product_id = $booking->get_product_id();
15226 if ( method_exists( $booking, 'get_data' ) ) {
15227 $booking = $booking->get_data();
15228 $booking['start'] = gmdate( 'Y-m-d H:i:s', $booking['start'] );
15229 $booking['end'] = gmdate( 'Y-m-d H:i:s', $booking['end'] );
15230 if ( ! empty( $person_counts ) ) {
15231 $total_count = 0;
15232 foreach ( $person_counts as $key => $value ) {
15233 $total_count += $value;
15234 }
15235 $booking['total_person_counts'] = $total_count;
15236 }
15237 $booking['bookable_product'] = $bookable_product_id;
15238 $booking['from_status'] = $data['filter']['from_status']['value'];
15239 $booking['to_status'] = $booking['status'];
15240 $context['response_type'] = 'live';
15241 $context['pluggable_data'] = array_merge( $booking, WordPress::get_user_context( $booking['customer_id'] ) );
15242 }
15243 }
15244 } else {
15245 $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 );
15246 $context['pluggable_data'] = $booking_sample_data;
15247 }
15248 }
15249 return $context;
15250 }
15251
15252 /**
15253 * Get WooCommerce Booking status list.
15254 *
15255 * @param array $data data.
15256 *
15257 * @return array
15258 */
15259 public function search_woo_booking_status_list( $data ) {
15260
15261 $options = [
15262 [
15263 'label' => 'Pending Confirmation',
15264 'value' => 'pending-confirmation',
15265 ],
15266 [
15267 'label' => 'Unpaid',
15268 'value' => 'unpaid',
15269 ],
15270 [
15271 'label' => 'Confirmed',
15272 'value' => 'confirmed',
15273 ],
15274 [
15275 'label' => 'Paid',
15276 'value' => 'paid',
15277 ],
15278 [
15279 'label' => 'Complete',
15280 'value' => 'complete',
15281 ],
15282 [
15283 'label' => 'In Cart',
15284 'value' => 'in-cart',
15285 ],
15286 [
15287 'label' => 'Cancelled',
15288 'value' => 'cancelled',
15289 ],
15290 ];
15291
15292 return [
15293 'options' => $options,
15294 'hasMore' => false,
15295 ];
15296 }
15297
15298 /**
15299 * Get Woocommerce Memberships Status List.
15300 *
15301 * @param array $data data.
15302 *
15303 * @return array|void
15304 */
15305 public function search_wc_membership_status_list( $data ) {
15306 $options = [];
15307 if ( ! function_exists( 'wc_memberships_get_user_membership_statuses' ) ) {
15308 return;
15309 }
15310
15311 $statuses = wc_memberships_get_user_membership_statuses();
15312 if ( ! empty( $statuses ) ) {
15313 if ( is_array( $statuses ) ) {
15314 foreach ( $statuses as $status => $value ) {
15315 $status = 0 === strpos( $status, 'wcm-' ) ? substr( $status, 4 ) : $status;
15316 $options[] = [
15317 'label' => $value['label'],
15318 'value' => $status,
15319 ];
15320 }
15321 }
15322 }
15323
15324 return [
15325 'options' => $options,
15326 'hasMore' => false,
15327 ];
15328 }
15329
15330 /**
15331 * Search Woocommerce Subscription data.
15332 *
15333 * @param array $data data.
15334 * @return array|void
15335 */
15336 public function search_user_subscription_last_data( $data ) {
15337
15338 $context = [];
15339
15340 if ( ! function_exists( 'wcs_get_subscription' ) ) {
15341 return;
15342 }
15343 if ( ! function_exists( 'wcs_get_subscriptions_for_product' ) ) {
15344 return;
15345 }
15346 if ( ! function_exists( 'wcs_order_contains_renewal' ) ) {
15347 return;
15348 }
15349 if ( ! function_exists( 'wcs_get_subscriptions_for_order' ) ) {
15350 return;
15351 }
15352
15353 if ( 'trial_end' == $data['search_term'] ) {
15354 $subscription_id = $data['filter']['subscription']['value'];
15355 $query_args = [
15356 'post_type' => 'shop_subscription',
15357 'orderby' => 'ID',
15358 'order' => 'DESC',
15359 'post_status' => 'wc-active',
15360 'posts_per_page' => 1,
15361 'meta_query' => [
15362 [
15363 'key' => '_schedule_trial_end',
15364 'value' => gmdate( 'Y-m-d H:i:s' ),
15365 'compare' => '<=',
15366 ],
15367 ],
15368 'post__in' => [ $subscription_id ],
15369 ];
15370 $query_result = new WP_Query( $query_args );
15371 $subscription_orders = $query_result->get_posts();
15372
15373 if ( ! empty( $subscription_orders ) ) {
15374 /**
15375 *
15376 * Ignore line
15377 *
15378 * @phpstan-ignore-next-line
15379 */
15380 $subscription = wcs_get_subscription( $subscription_orders[0]->ID );
15381 $user_id = $subscription->get_user_id();
15382
15383 $items = $subscription->get_items();
15384 $product_ids = [];
15385 foreach ( $items as $item ) {
15386 $product_ids[] = $item->get_product_id();
15387 }
15388
15389 $subscription_status = $subscription->get_status();
15390 $subscription_start_date = $subscription->get_date_created();
15391
15392 $context['subscription_data'] = [
15393 'status' => $subscription_status,
15394 'start_date' => $subscription_start_date,
15395 'trial_end_date' => $subscription->get_date( 'schedule_trial_end' ),
15396 'next_payment_date' => $subscription->get_date( 'next_payment' ),
15397 'end_date' => $subscription->get_date( 'schedule_end' ),
15398 ];
15399 $context['user'] = WordPress::get_user_context( $user_id );
15400
15401 foreach ( $product_ids as $val ) {
15402 $context['subscription'] = $val;
15403 $context['subscription_name'] = get_the_title( $val );
15404 }
15405 $context['pluggable_data'] = $context;
15406 $context['response_type'] = 'live';
15407 } else {
15408 $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 );
15409 $context['pluggable_data'] = $context;
15410 $context['response_type'] = 'sample';
15411 }
15412 } elseif ( 'renewal_payment_failed' == $data['search_term'] ) {
15413 $subscription_item = $data['filter']['subscription_item']['value'];
15414 $subscription_ids = wcs_get_subscriptions_for_product( $subscription_item );
15415 $ids = [];
15416 $related = [];
15417 foreach ( $subscription_ids as $subscription ) {
15418 $ids[] = $subscription;
15419 /**
15420 *
15421 * Ignore line
15422 *
15423 * @phpstan-ignore-next-line
15424 */
15425 $subscriptions = new WC_Subscription( $subscription );
15426 /**
15427 *
15428 * Ignore line
15429 *
15430 * @phpstan-ignore-next-line
15431 */
15432 $related[] = $subscriptions->get_related_orders();
15433 }
15434
15435 $failed_payment_orders = [];
15436 foreach ( $related as $value ) {
15437 foreach ( $value as $val ) {
15438 if ( wcs_order_contains_renewal( $val ) ) {
15439 $order = wc_get_order( $val );
15440 if ( $order ) {
15441 /**
15442 *
15443 * Ignore line
15444 *
15445 * @phpstan-ignore-next-line
15446 */
15447 $payment_status = $order->get_status();
15448 if ( 'failed' === $payment_status || 'wc-on-hold' === $payment_status ) {
15449 $failed_payment_orders[] = $val;
15450 }
15451 }
15452 }
15453 }
15454 }
15455
15456 if ( ! empty( $failed_payment_orders ) ) {
15457 $failed = array_rand( $failed_payment_orders );
15458 $related_subscriptions = wcs_get_subscriptions_for_order( $failed_payment_orders[ $failed ] );
15459 if ( ! empty( $related_subscriptions ) ) {
15460 $sub_id = get_post_meta( $failed_payment_orders[ $failed ], '_subscription_renewal', true );
15461 $subscription = wcs_get_subscription( $sub_id );
15462
15463 $items = $subscription->get_items();
15464 $product_ids = [];
15465 foreach ( $items as $item ) {
15466 $product_ids[] = $item->get_product_id();
15467 }
15468
15469 $context['user'] = WordPress::get_user_context( $subscription->get_user_id() );
15470 foreach ( [ 'renewal' ] as $order_type ) {
15471 foreach ( $subscription->get_related_orders( 'ids', $order_type ) as $order_id ) {
15472 $context['subscription_related_order'][] = $order_id;
15473 }
15474 }
15475 foreach ( $product_ids as $val ) {
15476 $context['subscription_item'] = $val;
15477 $context['subscription_name'] = get_the_title( $val );
15478 }
15479 $context['data'] = $subscription->get_data();
15480 $context['pluggable_data'] = $context;
15481 $context['response_type'] = 'live';
15482 } else {
15483 $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 );
15484 $context['response_type'] = 'sample';
15485 }
15486 } else {
15487 $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 );
15488 $context['response_type'] = 'sample';
15489 }
15490 }
15491 return $context;
15492 }
15493
15494 /**
15495 * Get Masteriyo LMS Courses.
15496 *
15497 * @param array $data data.
15498 *
15499 * @return array
15500 */
15501 public function search_masteriyo_lms_courses( $data ) {
15502
15503 $page = $data['page'];
15504 $limit = Utilities::get_search_page_limit();
15505 $offset = $limit * ( $page - 1 );
15506
15507 $args = [
15508 'post_type' => 'mto-course',
15509 'posts_per_page' => $limit,
15510 'offset' => $offset,
15511 'orderby' => 'title',
15512 'order' => 'ASC',
15513 'post_status' => 'publish',
15514 ];
15515
15516 $courses = get_posts( $args );
15517
15518 $course_count = wp_count_posts( 'mto-course' )->publish;
15519
15520 $options = [];
15521 if ( ! empty( $courses ) ) {
15522 if ( is_array( $courses ) ) {
15523 foreach ( $courses as $course ) {
15524 $options[] = [
15525 'label' => $course->post_title,
15526 'value' => $course->ID,
15527 ];
15528 }
15529 }
15530 }
15531 return [
15532 'options' => $options,
15533 'hasMore' => $course_count > $limit && $course_count > $offset,
15534 ];
15535 }
15536
15537 /**
15538 * Get Masteriyo LMS Lessons.
15539 *
15540 * @param array $data data.
15541 *
15542 * @return array
15543 */
15544 public function search_masteriyo_lms_lessons( $data ) {
15545
15546 $course_id = $data['dynamic'];
15547 $page = $data['page'];
15548 $limit = Utilities::get_search_page_limit();
15549 $offset = $limit * ( $page - 1 );
15550 $options = [];
15551 $args = [
15552 'post_type' => 'mto-lesson',
15553 'posts_per_page' => $limit,
15554 'offset' => $offset,
15555 'orderby' => 'title',
15556 'order' => 'ASC',
15557 'post_status' => 'publish',
15558 'meta_query' => [
15559 [
15560 'key' => '_course_id',
15561 'value' => $course_id,
15562 'compare' => '=',
15563 ],
15564 ],
15565 ];
15566
15567 $lessons = get_posts( $args );
15568 $lesson_count = wp_count_posts( 'mto-lesson' )->publish;
15569 if ( ! empty( $lessons ) ) {
15570 if ( is_array( $lessons ) ) {
15571 foreach ( $lessons as $lesson ) {
15572 $options[] = [
15573 'label' => $lesson->post_title,
15574 'value' => $lesson->ID,
15575 ];
15576 }
15577 }
15578 }
15579 return [
15580 'options' => $options,
15581 'hasMore' => $lesson_count > $limit && $lesson_count > $offset,
15582 ];
15583 }
15584
15585 /**
15586 * Get Masteriyo LMS Quiz.
15587 *
15588 * @param array $data data.
15589 *
15590 * @return array
15591 */
15592 public function search_masteriyo_lms_quizs( $data ) {
15593
15594 $course_id = $data['dynamic'];
15595 $page = $data['page'];
15596 $limit = Utilities::get_search_page_limit();
15597 $offset = $limit * ( $page - 1 );
15598 $options = [];
15599 $args = [
15600 'post_type' => 'mto-quiz',
15601 'posts_per_page' => $limit,
15602 'offset' => $offset,
15603 'orderby' => 'title',
15604 'order' => 'ASC',
15605 'post_status' => 'publish',
15606 'meta_query' => [
15607 [
15608 'key' => '_course_id',
15609 'value' => $course_id,
15610 'compare' => '=',
15611 ],
15612 ],
15613 ];
15614
15615 $quizs = get_posts( $args );
15616 $quiz_count = wp_count_posts( 'mto-quiz' )->publish;
15617 if ( ! empty( $quizs ) ) {
15618 if ( is_array( $quizs ) ) {
15619 foreach ( $quizs as $quiz ) {
15620 $options[] = [
15621 'label' => $quiz->post_title,
15622 'value' => $quiz->ID,
15623 ];
15624 }
15625 }
15626 }
15627 return [
15628 'options' => $options,
15629 'hasMore' => $quiz_count > $limit && $quiz_count > $offset,
15630 ];
15631 }
15632
15633 /**
15634 * Search Masteriyo LMS data.
15635 *
15636 * @param array $data data.
15637 * @return array|void|mixed
15638 */
15639 public function search_masteriyo_lms_last_data( $data ) {
15640 global $wpdb;
15641 $post_type = $data['post_type'];
15642 $trigger = $data['search_term'];
15643 $context = [];
15644
15645 $post_id = -1;
15646 if ( 'course_completed' === $trigger ) {
15647 $post_id = $data['filter']['course_id']['value'];
15648 } elseif ( 'lesson_completed' === $trigger ) {
15649 $post_id = $data['filter']['lesson_id']['value'];
15650 } elseif ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
15651 $post_id = $data['filter']['quiz_id']['value'];
15652 }
15653
15654 if ( 'course_completed' === $trigger || 'lesson_completed' === $trigger ) {
15655 if ( -1 === $post_id ) {
15656 $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 ) );
15657 } else {
15658 $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 ) );
15659 }
15660 }
15661
15662 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
15663 if ( -1 === $post_id ) {
15664 $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 ) );
15665 } else {
15666 $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 ) );
15667 }
15668 if ( ! empty( $result ) ) {
15669 $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 ) );
15670
15671 if ( function_exists( 'masteriyo_get_quiz' ) ) {
15672 $quiz = masteriyo_get_quiz( $resultt[0]->quiz_id );
15673 if ( is_object( $quiz ) && method_exists( $quiz, 'get_pass_mark' ) ) {
15674 if ( 'quiz_completed' === $trigger && $resultt[0]->earned_marks < $quiz->get_pass_mark() ) {
15675 $result = [];
15676 } elseif ( 'quiz_failed' === $trigger && $resultt[0]->earned_marks > $quiz->get_pass_mark() ) {
15677 $result = [];
15678 }
15679 }
15680 }
15681 }
15682 }
15683
15684 if ( ! empty( $result ) ) {
15685 $result_item_id = $result[0]->item_id;
15686 $result_user_id = $result[0]->user_id;
15687 $quiz_attempt = '';
15688 if ( 'quiz_completed' === $trigger || 'quiz_failed' === $trigger ) {
15689 $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 ) );
15690 $quiz_attempt = $resultt[0]->id;
15691 }
15692 switch ( $trigger ) {
15693 case 'course_completed':
15694 if ( function_exists( 'masteriyo_get_course' ) ) {
15695 $course = masteriyo_get_course( $result_item_id );
15696 $context_data = array_merge(
15697 WordPress::get_user_context( $result_user_id ),
15698 $course->get_data()
15699 );
15700 $context_data['course_id'] = $result_item_id;
15701 }
15702 break;
15703 case 'lesson_completed':
15704 if ( function_exists( 'masteriyo_get_lesson' ) ) {
15705 $lesson = masteriyo_get_lesson( $result_item_id );
15706 $context_data = array_merge(
15707 WordPress::get_user_context( $result_user_id ),
15708 $lesson->get_data()
15709 );
15710 $context_data['lesson_id'] = $result_item_id;
15711 }
15712 break;
15713 case 'quiz_completed':
15714 case 'quiz_failed':
15715 if ( function_exists( 'masteriyo_get_quiz' ) ) {
15716 $quiz = masteriyo_get_quiz( $result_item_id );
15717 $context_data = WordPress::get_user_context( $result_user_id );
15718 if ( function_exists( 'masteriyo_get_quiz_attempt' ) ) {
15719 $attempt = masteriyo_get_quiz_attempt( $quiz_attempt );
15720 $context_data['quiz_id'] = $result_item_id;
15721 $context_data['course_id'] = $quiz->get_course_id();
15722 $context_data['quiz'] = $quiz->get_data();
15723 $context_data['attempt'] = $attempt->get_data();
15724 }
15725 }
15726 break;
15727 default:
15728 return;
15729 }
15730 if ( ! empty( $context_data ) ) {
15731 $context['pluggable_data'] = $context_data;
15732 $context['response_type'] = 'live';
15733 }
15734 } elseif ( empty( $result ) ) {
15735 switch ( $trigger ) {
15736 case 'course_completed':
15737 $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"} ';
15738 break;
15739 case 'lesson_completed':
15740 $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"} ';
15741 break;
15742 case 'quiz_completed':
15743 case 'quiz_failed':
15744 $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"}';
15745 break;
15746 default:
15747 return;
15748 }
15749 $context = json_decode( $sample_data, true );
15750 }
15751
15752 return $context;
15753 }
15754
15755 /**
15756 * Search learndash user added in group.
15757 *
15758 * @param array $data data.
15759 * @return array|void
15760 */
15761 public function search_pluggables_user_added_in_group( $data ) {
15762 $context = [];
15763
15764 if ( ! function_exists( 'learndash_get_groups_user_ids' ) ) {
15765 return;
15766 }
15767 $term = $data['search_term'];
15768 $group_id = $data['filter']['sfwd_group_id']['value'];
15769
15770 if ( 'user_added_group' == $term ) {
15771 if ( -1 === $group_id ) {
15772 $args = [
15773 'numberposts' => 1,
15774 'orderby' => 'rand',
15775 'post_type' => 'groups',
15776 ];
15777 $posts = get_posts( $args );
15778 $random_value = $posts[0]->ID;
15779 $group_users = learndash_get_groups_user_ids( $random_value );
15780 $group_id = $random_value;
15781 } else {
15782 $group_users = learndash_get_groups_user_ids( $group_id );
15783 }
15784 $users = get_users(
15785 [
15786 'meta_key' => 'learndash_group_' . $group_id . '_enrolled_at',
15787 'orderby' => 'meta_value',
15788 'order' => 'DESC',
15789 'number' => 1,
15790 'include' => $group_users,
15791 ]
15792 );
15793 } elseif ( 'user_removed_group' == $term ) {
15794 if ( -1 === $group_id ) {
15795 $args = [
15796 'numberposts' => 1,
15797 'orderby' => 'rand',
15798 'post_type' => 'groups',
15799 ];
15800 $posts = get_posts( $args );
15801 $random_value = $posts[0]->ID;
15802 $group_id = $random_value;
15803 }
15804 $args = [
15805 'meta_query' => [
15806 'relation' => 'AND',
15807 [
15808 'key' => 'group_' . $group_id . '_access_from',
15809 'compare' => 'NOT EXISTS',
15810 ],
15811 [
15812 'key' => 'learndash_group_' . $group_id . '_enrolled_at',
15813 'compare' => 'EXISTS',
15814 ],
15815 ],
15816 'orderby' => 'meta_value',
15817 'order' => 'DESC',
15818 'number' => 1,
15819 ];
15820 $users = get_users( $args );
15821 } elseif ( 'leader_added_group' == $term || 'leader_removed_group' == $term ) {
15822 if ( -1 === $group_id ) {
15823 $args = [
15824 'numberposts' => 1,
15825 'fields' => 'ids',
15826 'orderby' => 'rand',
15827 'post_type' => 'groups',
15828 ];
15829 $posts = get_posts( $args );
15830 $random_key = array_rand( $posts );
15831 $random_value = $posts[ $random_key ];
15832 $group_id = $random_value;
15833 }
15834 $user_query_args = [
15835 'orderby' => 'learndash_group_leaders_' . intval( $group_id ),
15836 'order' => 'DESC',
15837 'meta_query' => [
15838 [
15839 'key' => 'learndash_group_leaders_' . intval( $group_id ),
15840 'value' => intval( $group_id ),
15841 'compare' => '=',
15842 'type' => 'NUMERIC',
15843 ],
15844 ],
15845 ];
15846 $users = get_users( $user_query_args );
15847 }
15848
15849 if ( ! empty( $users ) ) {
15850 $context = WordPress::get_user_context( $users[0]->ID );
15851 $context['sfwd_group_id'] = $group_id;
15852 $context['group_title'] = get_the_title( $group_id );
15853 $context['group_url'] = get_permalink( $group_id );
15854 $context['group_featured_image_id'] = get_post_meta( $group_id, '_thumbnail_id', true );
15855 $context['group_featured_image_url'] = get_the_post_thumbnail_url( $group_id );
15856 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
15857 $group_courses_id = learndash_group_enrolled_courses( $group_id );
15858 if ( ! empty( $group_courses_id ) ) {
15859 foreach ( $group_courses_id as $key => $course_id ) {
15860 $context['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
15861 }
15862 }
15863 }
15864 $context['response_type'] = 'live';
15865 } else {
15866 $context = WordPress::get_sample_user_context();
15867 $context['group_title'] = 'Test Group';
15868 $context['sfwd_group_id'] = 112;
15869 $context['group_url'] = 'https://example.com/test-group';
15870 $context['group_featured_image_id'] = 113;
15871 $context['group_featured_image_url'] = 'https://example.com/test-group-img';
15872 $context['group_courses'] = [
15873 [
15874 'ID' => 7915,
15875 'title' => 'Example Course',
15876 'URL' => 'https://example.com/courses/example-course/',
15877 'status' => 'publish',
15878 'featured_image_id' => '',
15879 'featured_image_url' => false,
15880 ],
15881 ];
15882 $context['response_type'] = 'sample';
15883 }
15884
15885 $context['pluggable_data'] = $context;
15886 return $context;
15887 }
15888
15889 /**
15890 * Search learndash user enrolled in course.
15891 *
15892 * @param array $data data.
15893 * @return array|void
15894 */
15895 public function search_pluggables_user_enrolled_course( $data ) {
15896 global $wpdb;
15897 $context = [];
15898
15899 if ( ! function_exists( 'ld_course_access_expires_on' ) ||
15900 ! function_exists( 'learndash_group_enrolled_courses' ) ||
15901 ! function_exists( 'learndash_get_lesson_list' ) ) {
15902 return;
15903 }
15904 $term = $data['search_term'];
15905 $course_id = isset( $data['filter']['sfwd_course_id'] ) ? $data['filter']['sfwd_course_id']['value'] : '';
15906 $group_id = isset( $data['filter']['sfwd_group_id'] ) ? $data['filter']['sfwd_group_id']['value'] : '';
15907
15908 if ( 'course_enrolled' == $term ) {
15909 if ( -1 === $course_id ) {
15910 $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 ) );
15911 } else {
15912 $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 ) );
15913 }
15914 } elseif ( 'course_unenrolled' == $term ) {
15915 if ( -1 === $course_id ) {
15916 $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 ) );
15917 $course_id = $courses[0]->course_id;
15918 }
15919 $args = [
15920 'meta_query' => [
15921 'relation' => 'AND',
15922 [
15923 'key' => 'course_' . $course_id . '_access_from',
15924 'compare' => 'NOT EXISTS',
15925 ],
15926 [
15927 'key' => 'learndash_course_' . $course_id . '_enrolled_at',
15928 'compare' => 'EXISTS',
15929 ],
15930 ],
15931 ];
15932 $users = get_users( $args );
15933 } elseif ( 'course_access_expired' == $term ) {
15934 if ( -1 === $course_id ) {
15935 $course_args = [
15936 'post_type' => 'sfwd-courses',
15937 'posts_per_page' => 1,
15938 'orderby' => 'rand',
15939 'order' => 'ASC',
15940 'post_status' => 'publish',
15941 ];
15942 $courses_posts = get_posts( $course_args );
15943 $course_id = $courses_posts[0]->ID;
15944 }
15945 $args = [
15946 'meta_query' => [
15947 [
15948 'key' => 'learndash_course_expired_' . $course_id,
15949 'compare' => 'EXISTS',
15950 ],
15951 ],
15952 ];
15953 $users = get_users( $args );
15954 } elseif ( 'group_course_completed' == $term ) {
15955 if ( -1 === $group_id ) {
15956 $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 ) );
15957 } else {
15958 $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 ) );
15959 }
15960 $activity_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}learndash_user_activity_meta WHERE activity_id = %d", $courses[0]->activity_id ) );
15961 } elseif ( 'course_group_added' == $term ) {
15962 if ( -1 === $group_id ) {
15963 $args = [
15964 'numberposts' => 1,
15965 'fields' => 'ids',
15966 'orderby' => 'rand',
15967 'post_type' => 'groups',
15968 ];
15969 $posts = get_posts( $args );
15970 $random_key = array_rand( $posts );
15971 $random_value = $posts[ $random_key ];
15972 $group_id = $random_value;
15973 }
15974 $courses = learndash_group_enrolled_courses( $group_id );
15975 } elseif ( 'assignment_submitted' == $term ) {
15976 $course_id = isset( $data['filter']['sfwd-courses'] ) ? $data['filter']['sfwd-courses']['value'] : '';
15977 $lesson_id = $data['filter']['sfwd_lesson_topic_id']['value'];
15978 if ( -1 === $course_id && -1 === $lesson_id ) {
15979 $args = [
15980 'post_type' => 'sfwd-assignment',
15981 'posts_per_page' => 1,
15982 'order' => 'DESC',
15983 'post_status' => 'publish',
15984 ];
15985 } else {
15986 if ( -1 === $course_id ) {
15987 $courses = get_posts(
15988 [
15989 'posts_per_page' => - 1,
15990 'post_type' => 'sfwd-courses',
15991 'post_status' => 'publish',
15992 'fields' => 'ids',
15993 ]
15994 );
15995 $course_key = array_rand( $courses );
15996 $course_id = $courses[ $course_key ];
15997 }
15998 if ( -1 === $lesson_id ) {
15999 $args = [
16000 'post_type' => 'sfwd-assignment',
16001 'posts_per_page' => 1,
16002 'order' => 'DESC',
16003 'post_status' => 'publish',
16004 'meta_query' => [
16005 [
16006 'key' => 'course_id',
16007 'value' => $course_id,
16008 'compare' => '=',
16009 ],
16010 ],
16011 ];
16012 } else {
16013 $args = [
16014 'post_type' => 'sfwd-assignment',
16015 'posts_per_page' => 1,
16016 'order' => 'DESC',
16017 'post_status' => 'publish',
16018 'meta_query' => [
16019 'relation' => 'AND',
16020 [
16021 'key' => 'lesson_id',
16022 'value' => $lesson_id,
16023 'compare' => '=',
16024 ],
16025 [
16026 'key' => 'course_id',
16027 'value' => $course_id,
16028 'compare' => '=',
16029 ],
16030 ],
16031 ];
16032 }
16033 }
16034 $assignments = get_posts( $args );
16035 } elseif ( 'assignment_graded' == $term ) {
16036 $course_id = isset( $data['filter']['sfwd-courses'] ) ? $data['filter']['sfwd-courses']['value'] : '';
16037 $lesson_id = $data['filter']['sfwd_lesson_topic_id']['value'];
16038 if ( -1 === $course_id && -1 === $lesson_id ) {
16039 $args = [
16040 'post_type' => 'sfwd-assignment',
16041 'posts_per_page' => 1,
16042 'order' => 'DESC',
16043 'post_status' => 'publish',
16044 'meta_query' => [
16045 [
16046 'key' => 'approval_status',
16047 'compare' => 'EXISTS',
16048 ],
16049 ],
16050 ];
16051 } else {
16052 if ( -1 === $course_id ) {
16053 $courses = get_posts(
16054 [
16055 'posts_per_page' => - 1,
16056 'post_type' => 'sfwd-courses',
16057 'post_status' => 'publish',
16058 'fields' => 'ids',
16059 ]
16060 );
16061 $course_key = array_rand( $courses );
16062 $course_id = $courses[ $course_key ];
16063 }
16064 if ( -1 === $lesson_id ) {
16065 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
16066 if ( ! empty( $lessons ) ) {
16067 $random_key = array_rand( $lessons );
16068 $random_value = $lessons[ $random_key ];
16069 $lesson_id = $random_value->ID;
16070 }
16071 }
16072 $args = [
16073 'post_type' => 'sfwd-assignment',
16074 'posts_per_page' => 1,
16075 'order' => 'DESC',
16076 'post_status' => 'publish',
16077 'meta_query' => [
16078 'relation' => 'AND',
16079 [
16080 'key' => 'lesson_id',
16081 'value' => $lesson_id,
16082 'compare' => '=',
16083 ],
16084 [
16085 'key' => 'course_id',
16086 'value' => $course_id,
16087 'compare' => '=',
16088 ],
16089 [
16090 'key' => 'approval_status',
16091 'compare' => 'EXISTS',
16092 ],
16093 ],
16094 ];
16095 }
16096 $assignments = get_posts( $args );
16097 }
16098
16099 if ( 'assignment_submitted' == $term || 'assignment_graded' == $term ) {
16100 if ( ! empty( $assignments ) ) {
16101 $context_data = WordPress::get_user_context( (int) $assignments[0]->post_author );
16102 $context_data['assignment_id'] = $assignments[0]->ID;
16103 $context_data['assignment_title'] = $assignments[0]->post_title;
16104 $context_data['assignment_url'] = get_post_meta( $assignments[0]->ID, 'file_link', true );
16105 $context_data['lesson_id'] = get_post_meta( $assignments[0]->ID, 'lesson_id', true );
16106 $context_data['course_id'] = get_post_meta( $assignments[0]->ID, 'course_id', true );
16107 $context_data['points'] = get_post_meta( $assignments[0]->ID, 'points', true );
16108 $context['response_type'] = 'live';
16109 } else {
16110 $context_data = WordPress::get_sample_user_context();
16111 $context_data['assignment_title'] = 'Test Assignment';
16112 $context_data['assignment_id'] = 112;
16113 $context_data['assignment_url'] = 'https://example.com/test_assignment.pdf';
16114 $context_data['lesson_id'] = 2;
16115 $context_data['course_id'] = 1;
16116 $context_data['points'] = '12';
16117 $context['response_type'] = 'sample';
16118 }
16119 } elseif ( 'course_unenrolled' == $term || 'course_access_expired' == $term ) {
16120 if ( ! empty( $users ) ) {
16121 $context_data = WordPress::get_user_context( $users[0]->ID );
16122 $context_data['sfwd_course_id'] = $course_id;
16123 $context_data['course_title'] = get_the_title( $course_id );
16124 $context_data['course_url'] = get_permalink( $course_id );
16125 $context_data['course_featured_image_id'] = get_post_meta( $course_id, '_thumbnail_id', true );
16126 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $course_id );
16127 $timestamp = ld_course_access_expires_on( $course_id, $users[0]->ID );
16128 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
16129 $date_format = get_option( 'date_format' );
16130 if ( is_string( $date_format ) ) {
16131 $context_data['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
16132 }
16133 $context['response_type'] = 'live';
16134 } else {
16135 $context_data = WordPress::get_sample_user_context();
16136 $context_data['course_name'] = 'Test Course';
16137 $context_data['sfwd_course_id'] = 112;
16138 $context_data['course_url'] = 'https://example.com/test-course';
16139 $context_data['course_featured_image_id'] = 113;
16140 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
16141 $context_data['course_access_expiry_date'] = '2023-10-20';
16142 $context['response_type'] = 'sample';
16143 }
16144 } elseif ( 'course_group_added' == $term ) {
16145 if ( ! empty( $courses ) ) {
16146 $context_data['course_id'] = $courses[0];
16147 $context_data['course_title'] = get_the_title( $courses[0] );
16148 $context_data['course_url'] = get_permalink( $courses[0] );
16149 $context_data['course_featured_image_id'] = get_post_meta( $courses[0], '_thumbnail_id', true );
16150 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $courses[0] );
16151 $context_data['sfwd_group_id'] = $group_id;
16152 $context_data['group_name'] = get_the_title( $group_id );
16153 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
16154 $group_courses_id = learndash_group_enrolled_courses( $group_id );
16155 if ( ! empty( $group_courses_id ) ) {
16156 foreach ( $group_courses_id as $key => $course_id ) {
16157 $context_data['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
16158 }
16159 }
16160 }
16161 $context['response_type'] = 'live';
16162 } else {
16163 $context_data = WordPress::get_sample_user_context();
16164 $context_data['course_name'] = 'Test Course';
16165 $context_data['sfwd_course_id'] = 112;
16166 $context_data['course_url'] = 'https://example.com/test-course';
16167 $context_data['course_featured_image_id'] = 113;
16168 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
16169 $context_data['course_access_expiry_date'] = '2023-10-20';
16170 $context_data['sfwd_group_id'] = 12;
16171 $context_data['group_name'] = 'Test Group';
16172 $context_data['group_courses'] = [
16173 [
16174 'ID' => 7915,
16175 'title' => 'Example Course',
16176 'URL' => 'https://example.com/courses/example-course/',
16177 'status' => 'publish',
16178 'featured_image_id' => '',
16179 'featured_image_url' => false,
16180 ],
16181 ];
16182 $context['response_type'] = 'sample';
16183 }
16184 } elseif ( 'group_course_completed' == $term ) {
16185 if ( ! empty( $courses ) && ! empty( $activity_meta ) ) {
16186 $context_data = WordPress::get_user_context( $courses[0]->user_id );
16187 $context_data['sfwd_group_id'] = $courses[0]->post_id;
16188 $context_data['group_title'] = get_the_title( $courses[0]->post_id );
16189 $context_data['group_url'] = get_permalink( $courses[0]->post_id );
16190 $context_data['group_featured_image_id'] = get_post_meta( $courses[0]->post_id, '_thumbnail_id', true );
16191 $context_data['group_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->post_id );
16192 if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
16193 $group_courses_id = learndash_group_enrolled_courses( $courses[0]->post_id );
16194 if ( ! empty( $group_courses_id ) ) {
16195 foreach ( $group_courses_id as $key => $course_id ) {
16196 $context_data['group_courses'][ $key ] = LearnDash::get_course_pluggable_data( $course_id );
16197 }
16198 }
16199 }
16200 $course_ids = null;
16201 foreach ( $activity_meta as $item ) {
16202 if ( 'course_ids' === $item->activity_meta_key ) {
16203 $course_ids = unserialize( $item->activity_meta_value );
16204 break;
16205 }
16206 }
16207 if ( ! empty( $course_ids ) && is_array( $course_ids ) ) {
16208 foreach ( $course_ids as $key => $course_id ) {
16209 if ( is_int( $course_id ) ) {
16210 $args = [
16211 'include' => [ $courses[0]->user_id ],
16212 'meta_query' => [
16213 [
16214 'key' => 'course_completed_' . $course_id,
16215 'compare' => 'EXISTS',
16216 ],
16217 ],
16218 ];
16219 $users = get_users( $args );
16220 if ( ! empty( $users ) ) {
16221 $context_data[ 'completed ' . $key ]['course_id'] = $course_id;
16222 $context_data[ 'completed ' . $key ]['course_title'] = get_the_title( $course_id );
16223 $context_data[ 'completed ' . $key ]['course_url'] = get_permalink( $course_id );
16224 $context_data[ 'completed ' . $key ]['course_featured_image_id'] = get_post_meta( $course_id, '_thumbnail_id', true );
16225 $context_data[ 'completed ' . $key ]['course_featured_image_url'] = get_the_post_thumbnail_url( $course_id );
16226 $timestamp = ld_course_access_expires_on( $course_id, $courses[0]->user_id );
16227 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
16228 $date_format = get_option( 'date_format' );
16229 if ( is_string( $date_format ) ) {
16230 $context_data[ 'completed ' . $key ]['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
16231 }
16232 }
16233 }
16234 }
16235 }
16236 $context['response_type'] = 'live';
16237 } else {
16238 $context_data = WordPress::get_sample_user_context();
16239 $context_data['sfwd_group_id'] = 112;
16240 $context_data['group_title'] = 'Test Group';
16241 $context_data['group_url'] = 113;
16242 $context_data['group_featured_image_id'] = 11;
16243 $context_data['group_featured_image_url'] = 'https://example.com/test-group-img';
16244 $context_data['completed 0'] = [
16245 'course_id' => 10,
16246 'course_title' => 'Test Course',
16247 'course_url' => 'https://example.com/test-course',
16248 'course_featured_image_id' => 14,
16249 'course_featured_image_url' => 'https://example.com/test-course-img',
16250 ];
16251 $context_data['group_courses'] = [
16252 [
16253 'ID' => 7915,
16254 'title' => 'Example Course',
16255 'URL' => 'https://example.com/courses/example-course/',
16256 'status' => 'publish',
16257 'featured_image_id' => '',
16258 'featured_image_url' => false,
16259 ],
16260 ];
16261 $context['response_type'] = 'sample';
16262 }
16263 } else {
16264 if ( ! empty( $courses ) ) {
16265 $context_data = WordPress::get_user_context( $courses[0]->user_id );
16266 $context_data['course_id'] = $courses[0]->course_id;
16267 $context_data['course_title'] = get_the_title( $courses[0]->course_id );
16268 $context_data['course_url'] = get_permalink( $courses[0]->course_id );
16269 $context_data['course_featured_image_id'] = get_post_meta( $courses[0]->course_id, '_thumbnail_id', true );
16270 $context_data['course_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->course_id );
16271 $timestamp = ld_course_access_expires_on( $courses[0]->course_id, $courses[0]->user_id );
16272 $timestamp = is_numeric( $timestamp ) ? (int) $timestamp : null;
16273 $date_format = get_option( 'date_format' );
16274 if ( is_string( $date_format ) ) {
16275 $context_data['course_access_expiry_date'] = wp_date( $date_format, $timestamp );
16276 }
16277 if ( $courses[0]->post_id ) {
16278 $context_data['sfwd_group_id'] = $courses[0]->post_id;
16279 $context_data['group_title'] = get_the_title( $courses[0]->post_id );
16280 $context_data['group_url'] = get_permalink( $courses[0]->post_id );
16281 $context_data['group_featured_image_id'] = get_post_meta( $courses[0]->post_id, '_thumbnail_id', true );
16282 $context_data['group_featured_image_url'] = get_the_post_thumbnail_url( $courses[0]->post_id );
16283 }
16284 $context['response_type'] = 'live';
16285 } else {
16286 $context_data = WordPress::get_sample_user_context();
16287 $context_data['course_name'] = 'Test Course';
16288 $context_data['sfwd_course_id'] = 112;
16289 $context_data['course_url'] = 'https://example.com/test-course';
16290 $context_data['course_featured_image_id'] = 113;
16291 $context_data['course_featured_image_url'] = 'https://example.com/test-course-img';
16292 $context_data['course_access_expiry_date'] = '2023-10-20';
16293 $context['response_type'] = 'sample';
16294 }
16295 }
16296
16297 $context['pluggable_data'] = $context_data;
16298 return $context;
16299 }
16300
16301 /**
16302 * Search LearnDash Lesson Topic List.
16303 *
16304 * @param array $data Search Params.
16305 *
16306 * @since 1.0.0
16307 *
16308 * @return array
16309 */
16310 public function search_ld_lessons_topics_list( $data ) {
16311 $options = [];
16312 $course_id = $data['dynamic']['sfwd-courses'];
16313
16314 if ( ! function_exists( 'learndash_get_lesson_list' ) || ! function_exists( 'learndash_get_topic_list' ) ) {
16315 return [];
16316 }
16317
16318 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
16319 foreach ( $lessons as $lesson ) {
16320 $options[] = [
16321 'label' => $lesson->post_title,
16322 'value' => $lesson->ID,
16323 ];
16324 $topics = learndash_get_topic_list( $lesson->ID, $course_id );
16325 foreach ( $topics as $topic ) {
16326 $options[] = [
16327 'label' => $topic->post_title,
16328 'value' => $topic->ID,
16329 ];
16330 }
16331 }
16332
16333 return [
16334 'options' => $options,
16335 'hasMore' => false,
16336 ];
16337 }
16338
16339 /**
16340 * Search LearnDash Quiz List.
16341 *
16342 * @param array $data Search Params.
16343 *
16344 * @since 1.0.0
16345 *
16346 * @return array
16347 */
16348 public function search_ld_quiz_list( $data ) {
16349 $options = [];
16350 $course_id = $data['dynamic']['sfwd-courses'];
16351 $lesson_id = $data['dynamic']['sfwd_lessons_topics'];
16352
16353 if ( ! function_exists( 'learndash_get_course_quiz_list' ) || ! function_exists( 'learndash_get_lesson_quiz_list' ) ) {
16354 return [];
16355 }
16356
16357 $quizzes = learndash_get_course_quiz_list( $course_id );
16358 $quizzes = array_merge( $quizzes, learndash_get_lesson_quiz_list( $lesson_id, null, $course_id ) );
16359 if ( ! empty( $quizzes ) ) {
16360 foreach ( $quizzes as $quiz ) {
16361 $options[] = [
16362 'label' => $quiz['post']->post_title,
16363 'value' => $quiz['post']->ID,
16364
16365 ];
16366 }
16367 }
16368
16369 return [
16370 'options' => $options,
16371 'hasMore' => false,
16372 ];
16373 }
16374
16375 /**
16376 * Search LearnDash Quiz Essay Question List.
16377 *
16378 * @param array $data Search Params.
16379 *
16380 * @since 1.0.0
16381 *
16382 * @return array
16383 */
16384 public function search_ld_quiz_essay_question_list( $data ) {
16385 $options = [];
16386 $quiz_id = $data['dynamic'];
16387
16388 if ( ! function_exists( 'learndash_get_quiz_questions' ) ) {
16389 return [];
16390 }
16391
16392 if ( 0 < $quiz_id ) {
16393 $quiz_question_ids = learndash_get_quiz_questions( $quiz_id );
16394 if ( ! empty( $quiz_question_ids ) ) {
16395 foreach ( $quiz_question_ids as $question_post_id => $question_pro_id ) {
16396 $question_type = get_post_meta( $question_post_id, 'question_type', true );
16397 if ( is_string( $question_type ) && 'essay' === $question_type ) {
16398 $title = html_entity_decode( get_the_title( $question_post_id ), ENT_QUOTES, 'UTF-8' );
16399 $options[] = [
16400 'label' => $title,
16401 'value' => $question_post_id,
16402 ];
16403 }
16404 }
16405 }
16406 }
16407
16408 return [
16409 'options' => $options,
16410 'hasMore' => false,
16411 ];
16412 }
16413
16414 /**
16415 * Search LearnDash Lessons List.
16416 *
16417 * @param array $data Search Params.
16418 *
16419 * @since 1.0.0
16420 *
16421 * @return array
16422 */
16423 public function search_ld_lessons_list( $data ) {
16424 $options = [];
16425 $course_id = $data['dynamic']['sfwd-courses'];
16426
16427 if ( ! function_exists( 'learndash_get_lesson_list' ) ) {
16428 return [];
16429 }
16430
16431 $lessons = learndash_get_lesson_list( $course_id, [ 'num' => 0 ] );
16432 foreach ( $lessons as $lesson ) {
16433 $options[] = [
16434 'label' => $lesson->post_title,
16435 'value' => $lesson->ID,
16436 ];
16437 }
16438 return [
16439 'options' => $options,
16440 'hasMore' => false,
16441 ];
16442 }
16443
16444 /**
16445 * Search LearnDash Topics List.
16446 *
16447 * @param array $data Search Params.
16448 *
16449 * @since 1.0.0
16450 *
16451 * @return array
16452 */
16453 public function search_ld_topics_list( $data ) {
16454 $options = [];
16455 $course_id = $data['dynamic']['sfwd-courses'];
16456 $lesson_id = $data['dynamic']['sfwd-lessons'];
16457
16458 if ( ! function_exists( 'learndash_get_topic_list' ) ) {
16459 return [];
16460 }
16461
16462 $topics = learndash_get_topic_list( $lesson_id, $course_id );
16463 foreach ( $topics as $topic ) {
16464 $options[] = [
16465 'label' => $topic->post_title,
16466 'value' => $topic->ID,
16467 ];
16468 }
16469 return [
16470 'options' => $options,
16471 'hasMore' => false,
16472 ];
16473 }
16474
16475 /**
16476 * Search LearnDash Assignments List.
16477 *
16478 * @param array $data Search Params.
16479 *
16480 * @since 1.0.0
16481 *
16482 * @return array
16483 */
16484 public function search_ld_assignments_list( $data ) {
16485 $options = [];
16486 $course_id = $data['dynamic']['sfwd-courses'];
16487 $lesson_id = $data['dynamic']['sfwd_lesson_topic_id'];
16488
16489 $args = [
16490 'post_type' => 'sfwd-assignment',
16491 'numberposts' => - 1,
16492 ];
16493 $meta_query = [];
16494 if ( ! empty( $course_id ) ) {
16495 $meta_query[] = [
16496 'key' => 'course_id',
16497 'value' => (int) $course_id,
16498 'compare' => '=',
16499 ];
16500 }
16501 if ( ! empty( $lesson_id ) ) {
16502 $meta_query[] = [
16503 'key' => 'lesson_id',
16504 'value' => (int) $lesson_id,
16505 'compare' => '=',
16506 ];
16507 }
16508 if ( ! empty( $meta_query ) ) {
16509 $args['meta_query'] = $meta_query;
16510 if ( count( $meta_query ) > 1 ) {
16511 $args['meta_query']['relation'] = 'AND';
16512 }
16513 }
16514 $assignments = get_posts( $args );
16515 foreach ( $assignments as $assignment ) {
16516 $options[] = [
16517 'label' => $assignment->post_title,
16518 'value' => $assignment->ID,
16519 ];
16520 }
16521 return [
16522 'options' => $options,
16523 'hasMore' => false,
16524 ];
16525 }
16526
16527 /**
16528 * Search post by post type.
16529 *
16530 * @param array $data Search Params.
16531 *
16532 * @since 1.0.0
16533 *
16534 * @return array
16535 */
16536 public function search_edd_prices( $data ) {
16537 $options = [];
16538 $downlaod_id = $data['dynamic']['download_id'];
16539
16540 $variable_prices = get_post_meta( $downlaod_id, 'edd_variable_prices', true );
16541 if ( ! empty( $variable_prices ) && is_array( $variable_prices ) ) {
16542 foreach ( $variable_prices as $price_id => $price ) {
16543 if ( isset( $price['name'] ) ) {
16544 $options[] = [
16545 'label' => $price['name'] . '(' . $price['amount'] . ')',
16546 'value' => $price['index'],
16547 ];
16548 }
16549 }
16550 }
16551
16552 return [
16553 'options' => $options,
16554 'hasMore' => false,
16555 ];
16556
16557 }
16558
16559 /**
16560 * GetPowerful Docs list.
16561 *
16562 * @param array $data data.
16563 *
16564 * @return array
16565 */
16566 public function search_pfd_docs_list( $data ) {
16567
16568 $course_id = $data['dynamic'];
16569 $page = $data['page'];
16570 $limit = Utilities::get_search_page_limit();
16571 $offset = $limit * ( $page - 1 );
16572 $options = [];
16573 $args = [
16574 'post_type' => 'docs',
16575 'posts_per_page' => $limit,
16576 'offset' => $offset,
16577 'orderby' => 'title',
16578 'order' => 'ASC',
16579 'post_status' => 'publish',
16580 ];
16581
16582 $docs = get_posts( $args );
16583 $docs_count = wp_count_posts( 'docs' )->publish;
16584 if ( ! empty( $docs ) ) {
16585 if ( is_array( $docs ) ) {
16586 foreach ( $docs as $doc ) {
16587 $options[] = [
16588 'label' => $doc->post_title,
16589 'value' => $doc->ID,
16590 ];
16591 }
16592 }
16593 }
16594 return [
16595 'options' => $options,
16596 'hasMore' => $docs_count > $limit && $docs_count > $offset,
16597 ];
16598 }
16599
16600 /**
16601 * Search Powerful Docs last data.
16602 *
16603 * @param array $data data.
16604 * @return array|void|mixed
16605 */
16606 public function search_pfd_feedback_last_data( $data ) {
16607 $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"}';
16608 $context = json_decode( $sample_data, true );
16609
16610 return $context;
16611 }
16612
16613 /**
16614 * WooCommerce Coupon Discount type list.
16615 *
16616 * @param array $data data.
16617 *
16618 * @return array
16619 */
16620 public function search_woo_coupon_discount_type_list( $data ) {
16621 $options = [];
16622
16623 $discount_types = wc_get_coupon_types();
16624
16625 if ( ! empty( $discount_types ) ) {
16626 if ( is_array( $discount_types ) ) {
16627 foreach ( $discount_types as $key => $value ) {
16628 $options[] = [
16629 'label' => $value,
16630 'value' => $key,
16631 ];
16632 }
16633 }
16634 }
16635
16636 return [
16637 'options' => $options,
16638 'hasMore' => false,
16639 ];
16640 }
16641
16642 /**
16643 * WooCommerce Product list along with variation list.
16644 *
16645 * @param array $data data.
16646 *
16647 * @return array|void
16648 */
16649 public function search_woo_product_variation_list( $data ) {
16650 $options = [];
16651
16652 $page = $data['page'];
16653 $limit = Utilities::get_search_page_limit();
16654 $offset = $limit * ( $page - 1 );
16655
16656 if ( ! function_exists( 'wc_get_products' ) ) {
16657 return;
16658 }
16659 $products = wc_get_products(
16660 [
16661 'posts_per_page' => $limit,
16662 'offset' => $offset,
16663 'orderby' => 'date',
16664 'order' => 'DESC',
16665 ]
16666 );
16667
16668
16669 $prod_count = wp_count_posts( 'product' )->publish;
16670
16671 if ( ! empty( $products ) ) {
16672 if ( is_array( $products ) ) {
16673 foreach ( $products as $product ) {
16674 $options[] = [
16675 'label' => $product->get_name(),
16676 'value' => $product->get_id(),
16677 ];
16678 if ( $product->is_type( 'variable' ) ) {
16679 $variations = Utilities::get_product_variations( $product->get_id() );
16680 foreach ( $variations['result'] as $variation ) {
16681 $options[] = [
16682 'label' => $variation->post_title,
16683 'value' => $variation->ID,
16684 ];
16685 }
16686 }
16687 }
16688 }
16689 }
16690 return [
16691 'options' => $options,
16692 'hasMore' => $prod_count > $limit && $prod_count > $offset,
16693 ];
16694 }
16695
16696 /**
16697 * WooCommerce Product list along with variation list.
16698 *
16699 * @param array $data data.
16700 *
16701 * @return array
16702 */
16703 public function search_woo_coupon_list( $data ) {
16704 $options = [];
16705
16706 $page = $data['page'];
16707 $limit = Utilities::get_search_page_limit();
16708 $offset = $limit * ( $page - 1 );
16709
16710 $coupons = get_posts(
16711 [
16712 'posts_per_page' => - 1,
16713 'orderby' => 'name',
16714 'order' => 'asc',
16715 'post_type' => 'shop_coupon',
16716 'post_status' => 'publish',
16717 ]
16718 );
16719 $coupon_count = wp_count_posts( 'shop_coupon' )->publish;
16720
16721 if ( ! empty( $coupons ) ) {
16722 if ( is_array( $coupons ) ) {
16723 foreach ( $coupons as $coupon ) {
16724 $code = wc_format_coupon_code( $coupon->post_title );
16725 $options[] = [
16726 'label' => $code,
16727 'value' => $code,
16728 ];
16729 }
16730 }
16731 }
16732
16733 return [
16734 'options' => $options,
16735 'hasMore' => $coupon_count > $limit && $coupon_count > $offset,
16736 ];
16737 }
16738
16739 /**
16740 * Prepare LatePoint Bookings List.
16741 *
16742 * @param array $data Search Params.
16743 * @return array
16744 */
16745 public function search_lp_bookings_list( $data ) {
16746
16747 if ( ! class_exists( 'OsBookingHelper' ) ) {
16748 return [];
16749 }
16750
16751 $bookings = OsBookingHelper::get_bookings_for_select();
16752 $options = [];
16753
16754 if ( ! empty( $bookings ) ) {
16755 foreach ( $bookings as $key => $booking ) {
16756 $options[] = [
16757 'label' => $booking['label'],
16758 'value' => $booking['value'],
16759 ];
16760 }
16761 }
16762
16763 return [
16764 'options' => $options,
16765 'hasMore' => false,
16766 ];
16767 }
16768
16769 /**
16770 * Prepare LatePoint Services List.
16771 *
16772 * @param array $data Search Params.
16773 * @return array
16774 */
16775 public function search_lp_services_list( $data ) {
16776
16777 if ( ! class_exists( 'OsServiceHelper' ) ) {
16778 return [];
16779 }
16780
16781 $services = OsServiceHelper::get_services_list();
16782 $options = [];
16783
16784 if ( ! empty( $services ) ) {
16785 foreach ( $services as $key => $service ) {
16786 $options[] = [
16787 'label' => $service['label'],
16788 'value' => $service['value'],
16789 ];
16790 }
16791 }
16792
16793 return [
16794 'options' => $options,
16795 'hasMore' => false,
16796 ];
16797 }
16798
16799 /**
16800 * Prepare LatePoint Bundles List.
16801 *
16802 * @param array $data Search Params.
16803 * @return array
16804 */
16805 public function search_lp_bundles_list( $data ) {
16806 global $wpdb;
16807
16808 $bundles = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}latepoint_bundles", ARRAY_A );
16809
16810 $options = [];
16811
16812 if ( ! empty( $bundles ) ) {
16813 foreach ( $bundles as $bundle ) {
16814 $options[] = [
16815 'label' => $bundle['name'],
16816 'value' => $bundle['id'],
16817 ];
16818 }
16819 }
16820
16821 return [
16822 'options' => $options,
16823 'hasMore' => false,
16824 ];
16825 }
16826
16827 /**
16828 * Prepare LatePoint Agents List.
16829 *
16830 * @param array $data Search Params.
16831 * @return array
16832 */
16833 public function search_lp_agents_list( $data ) {
16834
16835 if ( ! class_exists( 'OsAgentHelper' ) ) {
16836 return [];
16837 }
16838
16839 $agent_ids_for_service = OsAgentHelper::get_agent_ids_for_service_and_location( $data['dynamic'] );
16840 $agents = OsAgentHelper::get_agents_list();
16841 $options = [];
16842
16843 if ( ! empty( $agents ) ) {
16844 foreach ( $agents as $key => $agent ) {
16845 if ( in_array( $agent['value'], $agent_ids_for_service ) ) {
16846 $options[] = [
16847 'label' => $agent['label'],
16848 'value' => $agent['value'],
16849 ];
16850 }
16851 }
16852 }
16853
16854 return [
16855 'options' => $options,
16856 'hasMore' => false,
16857 ];
16858 }
16859
16860 /**
16861 * Prepare LatePoint Agents WP User List.
16862 *
16863 * @param array $data Search Params.
16864 * @return array
16865 */
16866 public function search_lp_user_agents_list( $data ) {
16867
16868 if ( ! class_exists( 'OsWpUserHelper' ) ) {
16869 return [];
16870 }
16871 $agents = OsWpUserHelper::get_wp_users_for_select( [ 'role' => 'latepoint_agent' ] );
16872 $options = [];
16873 if ( ! empty( $agents ) ) {
16874 $options = $agents;
16875 }
16876 return [
16877 'options' => $options,
16878 'hasMore' => false,
16879 ];
16880 }
16881
16882 /**
16883 * Prepare LatePoint Statuses List.
16884 *
16885 * @param array $data Search Params.
16886 * @return array
16887 */
16888 public function search_lp_statuses_list( $data ) {
16889
16890 if ( ! class_exists( 'OsBookingHelper' ) ) {
16891 return [];
16892 }
16893
16894 $statuses = OsBookingHelper::get_statuses_list();
16895 $options = [];
16896
16897 if ( ! empty( $statuses ) ) {
16898 foreach ( $statuses as $key => $label ) {
16899 $options[] = [
16900 'label' => $label,
16901 'value' => $key,
16902 ];
16903 }
16904 }
16905
16906 return [
16907 'options' => $options,
16908 'hasMore' => false,
16909 ];
16910 }
16911
16912 /**
16913 * Prepare LatePoint Customers List.
16914 *
16915 * @param array $data Search Params.
16916 * @return array
16917 */
16918 public function search_lp_customers_list( $data ) {
16919
16920 if ( ! class_exists( 'OsCustomerHelper' ) ) {
16921 return [];
16922 }
16923
16924 $customers = OsCustomerHelper::get_customers_for_select();
16925 $options = [];
16926
16927 if ( ! empty( $customers ) ) {
16928 foreach ( $customers as $key => $customer ) {
16929 $options[] = [
16930 'label' => $customer['label'],
16931 'value' => $customer['value'],
16932 ];
16933 }
16934 }
16935
16936 return [
16937 'options' => $options,
16938 'hasMore' => false,
16939 ];
16940 }
16941
16942 /**
16943 * Prepare SureForms Forms List.
16944 *
16945 * @param array $data Search Params.
16946 * @return array
16947 */
16948 public function search_sureforms_form_list( $data ) {
16949
16950 $options = [];
16951
16952 $page = $data['page'];
16953 $limit = Utilities::get_search_page_limit();
16954 $offset = $limit * ( $page - 1 );
16955
16956 $forms = get_posts(
16957 [
16958 'posts_per_page' => - 1,
16959 'orderby' => 'name',
16960 'order' => 'asc',
16961 'post_type' => 'sureforms_form',
16962 'post_status' => 'publish',
16963 ]
16964 );
16965 $form_count = wp_count_posts( 'sureforms_form' )->publish;
16966
16967 if ( ! empty( $forms ) ) {
16968 if ( is_array( $forms ) ) {
16969 foreach ( $forms as $form ) {
16970 $title = html_entity_decode( get_the_title( $form->ID ), ENT_QUOTES, 'UTF-8' );
16971 $options[] = [
16972 'label' => $title,
16973 'value' => $form->ID,
16974 ];
16975 }
16976 }
16977 }
16978
16979 return [
16980 'options' => $options,
16981 'hasMore' => $form_count > $limit && $form_count > $offset,
16982 ];
16983 }
16984
16985 /**
16986 * Prepare Academy Course List.
16987 *
16988 * @param array $data Search Params.
16989 * @return array
16990 */
16991 public function search_ac_lms_courses( $data ) {
16992
16993 $options = [];
16994
16995 $page = $data['page'];
16996 $limit = Utilities::get_search_page_limit();
16997 $offset = $limit * ( $page - 1 );
16998
16999 $courses = get_posts(
17000 [
17001 'posts_per_page' => - 1,
17002 'orderby' => 'name',
17003 'order' => 'asc',
17004 'post_type' => 'academy_courses',
17005 'post_status' => 'publish',
17006 ]
17007 );
17008 $course_count = wp_count_posts( 'academy_courses' )->publish;
17009
17010 if ( ! empty( $courses ) ) {
17011 if ( is_array( $courses ) ) {
17012 foreach ( $courses as $course ) {
17013 $options[] = [
17014 'label' => get_the_title( $course->ID ),
17015 'value' => $course->ID,
17016 ];
17017 }
17018 }
17019 }
17020
17021 return [
17022 'options' => $options,
17023 'hasMore' => $course_count > $limit && $course_count > $offset,
17024 ];
17025 }
17026
17027 /**
17028 * Prepare Academy Lesson List.
17029 *
17030 * @param array $data Search Params.
17031 * @return array
17032 */
17033 public function search_ac_lms_lessons( $data ) {
17034
17035 $options = [];
17036
17037 if ( ! class_exists( '\Academy\Helper' ) ) {
17038 return [];
17039 }
17040
17041 $curriculums = \Academy\Helper::get_course_curriculum( $data['dynamic'] );
17042
17043 if ( ! empty( $curriculums ) ) {
17044 foreach ( $curriculums as $topic ) {
17045 if ( isset( $topic['topics'] ) && is_array( $topic['topics'] ) ) {
17046 foreach ( $topic['topics'] as $lesson ) {
17047 if ( isset( $lesson['type'] ) && 'lesson' === $lesson['type'] ) {
17048 $options[] = [
17049 'label' => $lesson['name'],
17050 'value' => $lesson['id'],
17051 ];
17052 }
17053 }
17054 }
17055 }
17056 }
17057
17058 return [
17059 'options' => $options,
17060 'hasMore' => false,
17061 ];
17062 }
17063
17064 /**
17065 * Prepare Academy Quiz List.
17066 *
17067 * @param array $data Search Params.
17068 * @return array
17069 */
17070 public function search_ac_lms_quiz( $data ) {
17071
17072 $options = [];
17073
17074 $page = $data['page'];
17075 $limit = Utilities::get_search_page_limit();
17076 $offset = $limit * ( $page - 1 );
17077
17078 $quizs = get_posts(
17079 [
17080 'posts_per_page' => - 1,
17081 'orderby' => 'name',
17082 'order' => 'asc',
17083 'post_type' => 'academy_quiz',
17084 'post_status' => 'publish',
17085 ]
17086 );
17087 $quiz_count = wp_count_posts( 'academy_quiz' )->publish;
17088
17089 if ( ! empty( $quizs ) ) {
17090 if ( is_array( $quizs ) ) {
17091 foreach ( $quizs as $quiz ) {
17092 $options[] = [
17093 'label' => get_the_title( $quiz->ID ),
17094 'value' => $quiz->ID,
17095 ];
17096 }
17097 }
17098 }
17099
17100 return [
17101 'options' => $options,
17102 'hasMore' => $quiz_count > $limit && $quiz_count > $offset,
17103 ];
17104 }
17105
17106 /**
17107 * Search Academy LMS data.
17108 *
17109 * @param array $data data.
17110 * @return array|void
17111 */
17112 public function search_ac_lms_last_data( $data ) {
17113 global $wpdb;
17114 $post_type = $data['post_type'];
17115 $trigger = $data['search_term'];
17116 $context = [];
17117
17118 if ( ! class_exists( '\Academy\Helper' ) ) {
17119 return [];
17120 }
17121
17122 $course_id = -1;
17123 $lesson_id = -1;
17124 $quiz_id = -1;
17125 if ( 'ac_lms_course_completed' === $trigger ) {
17126 $course_id = $data['filter']['course']['value'];
17127 } elseif ( 'ac_lms_lesson_completed' === $trigger ) {
17128 $course_id = $data['filter']['course']['value'];
17129 $lesson_id = $data['filter']['lesson']['value'];
17130 } elseif ( 'ac_lms_quiz_completed' === $trigger || 'ac_lms_quiz_failed' === $trigger ) {
17131 $quiz_id = $data['filter']['quiz']['value'];
17132 } elseif ( 'ac_lms_enrolled_course' === $trigger ) {
17133 $course_id = $data['filter']['course']['value'];
17134 }
17135
17136 $users = get_users(
17137 [
17138 'fields' => 'ID',
17139 'meta_key' => 'is_academy_student',
17140 ]
17141 );
17142 if ( ! empty( $users ) ) {
17143 $user_random_key = array_rand( $users );
17144 $user_id = $users[ $user_random_key ];
17145 }
17146
17147 if ( 'ac_lms_course_completed' === $trigger ) {
17148 if ( -1 === $course_id ) {
17149 $result = $wpdb->get_results(
17150 $wpdb->prepare(
17151 "SELECT * FROM {$wpdb->prefix}comments
17152 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",
17153 $post_type
17154 )
17155 );
17156 } else {
17157 $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 ) );
17158 }
17159 } elseif ( 'ac_lms_lesson_completed' === $trigger ) {
17160 if ( -1 === $course_id ) {
17161 $courses = get_posts(
17162 [
17163 'posts_per_page' => - 1,
17164 'post_type' => 'academy_courses',
17165 'post_status' => 'publish',
17166 'fields' => 'ids',
17167 ]
17168 );
17169 $course_id = array_rand( $courses );
17170 $option_name = 'academy_course_' . $course_id . '_completed_topics';
17171 } else {
17172 $option_name = 'academy_course_' . $course_id . '_completed_topics';
17173 }
17174 if ( ! empty( $users ) ) {
17175 $meta = get_user_meta( $user_id, $option_name, true );
17176 if ( is_string( $meta ) ) {
17177 $saved_topics_lists = (array) json_decode( $meta, true );
17178 if ( -1 === $lesson_id ) {
17179 $result = $saved_topics_lists['lesson'];
17180 } else {
17181 if ( is_array( $saved_topics_lists['lesson'] ) ) {
17182 if ( array_key_exists( $lesson_id, $saved_topics_lists['lesson'] ) ) {
17183 $result = $saved_topics_lists['lesson'];
17184 }
17185 }
17186 }
17187 }
17188 }
17189 } elseif ( 'ac_lms_quiz_completed' === $trigger ) {
17190 if ( -1 === $quiz_id ) {
17191 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE attempt_status='passed' order by attempt_id DESC LIMIT 1" );
17192 } else {
17193 $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 ) );
17194 }
17195 } elseif ( 'ac_lms_quiz_failed' === $trigger ) {
17196 if ( -1 === $quiz_id ) {
17197 $result = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}academy_quiz_attempts WHERE attempt_status='failed' order by attempt_id DESC LIMIT 1" );
17198 } else {
17199 $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 ) );
17200 }
17201 } elseif ( 'ac_lms_enrolled_course' === $trigger ) {
17202 if ( -1 === $course_id ) {
17203 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type = %s order by ID DESC LIMIT 1", 'academy_enrolled' ) );
17204 } else {
17205 $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 ) );
17206 }
17207 }
17208
17209 if ( ! empty( $result ) ) {
17210 switch ( $trigger ) {
17211 case 'ac_lms_course_completed':
17212 $data = WordPress::get_post_context( $result[0]->comment_post_ID );
17213 $context_data = WordPress::get_user_context( $result[0]->user_id );
17214 $context_data['course_data'] = $data;
17215 $context_data['course'] = $result[0]->comment_post_ID;
17216 break;
17217 case 'ac_lms_enrolled_course':
17218 $data = WordPress::get_post_context( $result[0]->post_parent );
17219 $context_data = WordPress::get_user_context( $result[0]->post_author );
17220 $context_data['course_data'] = $data;
17221 $context_data['enrollment_data'] = $result[0];
17222 $context_data['course'] = $result[0]->post_parent;
17223 break;
17224 case 'ac_lms_lesson_completed':
17225 if ( -1 === $lesson_id ) {
17226 $key = array_rand( $result );
17227 $lesson_data = \Academy\Helper::get_lesson( $key );
17228 } else {
17229 $lesson_data = \Academy\Helper::get_lesson( $lesson_id );
17230 }
17231 if ( is_object( $lesson_data ) ) {
17232 $lesson_data = get_object_vars( $lesson_data );
17233 }
17234 if ( ! empty( $users ) ) {
17235 $context_data = array_merge( $lesson_data, WordPress::get_user_context( $user_id ) );
17236 }
17237 $context_data['course_data'] = WordPress::get_post_context( $course_id );
17238 $context_data['lesson'] = $lesson_id;
17239 $context_data['course'] = $course_id;
17240 break;
17241 case 'ac_lms_quiz_completed':
17242 case 'ac_lms_quiz_failed':
17243 $context_data = WordPress::get_user_context( $result[0]->user_id );
17244 $context_data['quiz_data'] = WordPress::get_post_context( $result[0]->quiz_id );
17245 $context_data['quiz_attempt_details'] = $result;
17246 $context_data['quiz'] = $result[0]->quiz_id;
17247 break;
17248 default:
17249 return;
17250 }
17251 $context['pluggable_data'] = $context_data;
17252 $context['response_type'] = 'live';
17253 } elseif ( empty( $result ) ) {
17254 switch ( $trigger ) {
17255 case 'ac_lms_course_completed':
17256 $sample_data = '{"pluggable_data":{"wp_user_id": 73, "user_login": "abc@yopmail.com","display_name": "data data1", "user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"course_data": {"ID": 6949,"post_author": "1","post_date": "2023-11-29 05:37:50","post_date_gmt": "2023-11-29 05:37:50","post_content": "<!-- wp:paragraph -->\n<p>this is a business course.<\/p>\n<!-- \/wp:paragraph -->","post_title": "Business Course","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "open","post_password": "","post_name": "business-course","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:27","post_modified_gmt": "2023-11-29 09:50:27","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/example-wpnew.local\/course\/business-course\/","menu_order": 0,"post_type": "academy_courses","post_mime_type": "","comment_count": "0","filter": "raw"},"course": 1},"response_type":"sample"}';
17257 break;
17258 case 'ac_lms_lesson_completed':
17259 $sample_data = '{"pluggable_data":{"ID": "1","lesson_author": "1","lesson_date": "2023-11-29 07:01:03","lesson_date_gmt": "2023-11-29 07:01:03","lesson_title": "Lesson 1","lesson_name": "","lesson_content": "","lesson_excerpt": "","lesson_status": "publish","comment_status": "close","comment_count": "0","lesson_password": "","lesson_modified": "2023-11-29 07:01:03","lesson_modified_gmt": "2023-11-29 07:01:03","wp_user_id": 73,"user_login": "abc@yopmail.com","display_name": "data data1","user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"course_data": {"ID": 6949,"post_author": "1","post_date": "2023-11-29 05:37:50","post_date_gmt": "2023-11-29 05:37:50","post_content": "<!-- wp:paragraph -->\n<p>this is a business course.<\/p>\n<!-- \/wp:paragraph -->","post_title": "Business Course","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "open","post_password": "","post_name": "business-course","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:27","post_modified_gmt": "2023-11-29 09:50:27","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/example-wpnew.local\/course\/business-course\/","menu_order": 0,"post_type": "academy_courses","post_mime_type": "","comment_count": "0","filter": "raw"},"lesson": 1,"course":1},"response_type":"sample"}';
17260 break;
17261 case 'ac_lms_quiz_completed':
17262 $sample_data = '{"pluggable_data":{"wp_user_id": 73,"user_login": "abc@yopmail.com","display_name": "data data1","user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"quiz_data": {"ID": 6960,"post_author": "1","post_date": "2023-11-29 09:49:42","post_date_gmt": "2023-11-29 09:49:42","post_content": "","post_title": "First Quiz","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "closed","post_password": "","post_name": "first-quiz","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:11","post_modified_gmt": "2023-11-29 09:50:11","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/example-wpnew.local\/?post_type=academy_quiz&#038;p=6960","menu_order": 0,"post_type": "academy_quiz","post_mime_type": "","comment_count": "0","filter": "raw"}, "quiz_attempt_details": [{"attempt_id": "2","course_id": "6949","quiz_id": "6960","user_id": "126","total_questions": "1","total_answered_questions": "1","total_marks": "20.00","earned_marks": "20.00","attempt_info": "{\"total_correct_answers\":1}","attempt_status": "passed","attempt_ip": "127.0.0.1","attempt_started_at": "2023-11-30 06:20:10","attempt_ended_at": "2023-11-30 06:20:10","is_manually_reviewed": null,"manually_reviewed_at": null}],"quiz":1},"response_type":"sample"}';
17263 break;
17264 case 'ac_lms_quiz_failed':
17265 $sample_data = '{"pluggable_data":{"wp_user_id": 73,"user_login": "abc@yopmail.com","display_name": "data data1","user_firstname": "data","user_lastname": "data1","user_email": "abc@yopmail.com","user_role": [],"quiz_data": {"ID": 6960,"post_author": "1","post_date": "2023-11-29 09:49:42","post_date_gmt": "2023-11-29 09:49:42","post_content": "","post_title": "First Quiz","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "closed","post_password": "","post_name": "first-quiz","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:11","post_modified_gmt": "2023-11-29 09:50:11","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/example-wpnew.local\/?post_type=academy_quiz&#038;p=6960","menu_order": 0,"post_type": "academy_quiz","post_mime_type": "","comment_count": "0","filter": "raw"}, "quiz_attempt_details": [{"attempt_id": "2","course_id": "6949","quiz_id": "6960","user_id": "126","total_questions": "1","total_answered_questions": "1","total_marks": "20.00","earned_marks": "0.00","attempt_info": "{\"total_correct_answers\":0}","attempt_status": "failed","attempt_ip": "127.0.0.1","attempt_started_at": "2023-11-30 06:20:10","attempt_ended_at": "2023-11-30 06:20:10","is_manually_reviewed": null,"manually_reviewed_at": null}],"quiz":1},"response_type":"sample"}';
17266 break;
17267 case 'ac_lms_enrolled_course':
17268 $sample_data = '{"pluggable_data":{"course_data": {"ID": 6949,"post_author": "1","post_date": "2023-11-29 05:37:50","post_date_gmt": "2023-11-29 05:37:50","post_content": "<!-- wp:paragraph -->\n<p>this is a business course.<\/p>\n<!-- \/wp:paragraph -->","post_title": "Business Course","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "open","post_password": "","post_name": "business-course","to_ping": "","pinged": "","post_modified": "2023-11-29 09:50:27","post_modified_gmt": "2023-11-29 09:50:27","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/example-wpnew.local\/course\/business-course\/","menu_order": 0,"post_type": "academy_courses","post_mime_type": "","comment_count": "0","filter": "raw"},"enrollment_data": {"ID": "6971","post_author": "126","post_date": "2023-11-30 05:52:54","post_date_gmt": "2023-11-30 05:52:54","post_content": "","post_title": "Course Enrolled November 30, 2023 @ 5:52 am","post_excerpt": "","post_status": "completed","comment_status": "closed","ping_status": "closed","post_password": "","post_name": "course-enrolled-november-30-2023-552-am","to_ping": "","pinged": "","post_modified": "2023-11-30 05:52:54","post_modified_gmt": "2023-11-30 05:52:54","post_content_filtered": "","post_parent": "6949","guid": "https:\/\/example-wpnew.local\/?p=6971","menu_order": "0","post_type": "academy_enrolled","post_mime_type": "","comment_count": "0"},"course":1, "wp_user_id": 2,"user_login": "test","display_name": "test test","user_firstname": "test","user_lastname": "test", "user_email": "test@yopmail.com","user_role": ["academy_student"]},"response_type":"sample"}';
17269 break;
17270 default:
17271 return;
17272 }
17273 $context = (array) json_decode( $sample_data, true );
17274 }
17275 return $context;
17276 }
17277
17278 /**
17279 * Search myCred Point Type List.
17280 *
17281 * @param array $data Search Params.
17282 * @return array
17283 */
17284 public function search_mycred_point_type_list( $data ) {
17285
17286 $options = [];
17287
17288 if ( ! function_exists( 'mycred_get_types' ) ) {
17289 return [];
17290 }
17291
17292 $posts = mycred_get_types();
17293
17294 if ( ! empty( $posts ) ) {
17295 if ( is_array( $posts ) ) {
17296 foreach ( $posts as $key => $post ) {
17297 $options[] = [
17298 'label' => $post,
17299 'value' => $key,
17300 ];
17301 }
17302 }
17303 }
17304
17305 return [
17306 'options' => $options,
17307 'hasMore' => false,
17308 ];
17309 }
17310
17311 /**
17312 * Prepare elementor forms.
17313 *
17314 * @param array $data Search Params.
17315 *
17316 * @return array
17317 */
17318 public function search_new_elementor_form_fields( $data ) {
17319
17320 $fields = [];
17321 $select_form_id = $data['dynamic'];
17322 global $wpdb;
17323 $post_metas = $wpdb->get_results(
17324 $wpdb->prepare(
17325 "SELECT pm.post_id, pm.meta_value
17326 FROM $wpdb->postmeta pm
17327 LEFT JOIN $wpdb->posts p
17328 ON p.ID = pm.post_id
17329 WHERE p.post_type IS NOT NULL
17330 AND p.post_status = %s
17331 AND pm.meta_key = %s
17332 AND pm.`meta_value` LIKE %s",
17333 'publish',
17334 '_elementor_data',
17335 '%%form_fields%%'
17336 )
17337 );
17338
17339 if ( ! empty( $post_metas ) ) {
17340 foreach ( $post_metas as $post_meta ) {
17341 /**
17342 *
17343 * Ignore line
17344 *
17345 * @phpstan-ignore-next-line
17346 */
17347 $inner_forms = Utilities::search_elementor_forms( json_decode( $post_meta->meta_value ) );
17348 if ( ! empty( $inner_forms ) ) {
17349 foreach ( $inner_forms as $form ) {
17350 $form_id = explode( '_', $select_form_id );
17351 if ( is_object( $form ) ) {
17352 if ( $form->id == $form_id[1] ) {
17353 if ( ! empty( $form->settings->form_fields ) ) {
17354 foreach ( $form->settings->form_fields as $field ) {
17355 $fields[] = [
17356 'value' => $field->custom_id,
17357 'text' => ! empty( $field->field_label ) ? $field->field_label : 'unknown',
17358 ];
17359 }
17360 }
17361 }
17362 }
17363 }
17364 }
17365 }
17366 }
17367 $options = [];
17368 if ( ! empty( $fields ) ) {
17369 foreach ( $fields as $key => $value ) {
17370 $options[] = [
17371 'label' => $value['text'],
17372 'value' => $value['value'],
17373 ];
17374 }
17375 }
17376 return [
17377 'options' => $options,
17378 'hasMore' => false,
17379 ];
17380 }
17381 /**
17382 * Get Fluent Booking Appointment Events.
17383 *
17384 * @param array $data data.
17385 *
17386 * @return array
17387 */
17388 public function search_fluent_booking_calendars_list( $data ) {
17389
17390 global $wpdb;
17391
17392 $page = $data['page'];
17393 $limit = Utilities::get_search_page_limit();
17394 $offset = $limit * ( $page - 1 );
17395
17396 $calendars = $wpdb->get_results(
17397 $wpdb->prepare(
17398 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendars WHERE status = %s ORDER BY id DESC LIMIT %d OFFSET %d",
17399 [ 'active', $limit, $offset ]
17400 ),
17401 OBJECT
17402 );
17403
17404 $calendars_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
17405
17406 $options = [];
17407 if ( ! empty( $calendars ) ) {
17408 foreach ( $calendars as $calendar ) {
17409 $options[] = [
17410 'label' => $calendar->title,
17411 'value' => $calendar->id,
17412 ];
17413 }
17414 }
17415 return [
17416 'options' => $options,
17417 'hasMore' => $calendars_count > $limit && $calendars_count > $offset,
17418 ];
17419
17420 }
17421
17422 /**
17423 * Get Fluent Booking Appointment Events.
17424 *
17425 * @param array $data data.
17426 *
17427 * @return array
17428 */
17429 public function search_fluent_booking_events_list( $data ) {
17430
17431 global $wpdb;
17432 $page = $data['page'];
17433 $limit = Utilities::get_search_page_limit();
17434 $offset = $limit * ( $page - 1 );
17435 $calendar_id = sanitize_text_field( $data['dynamic']['calender_id'] );
17436 if ( '-1' === $calendar_id ) {
17437 $events = $wpdb->get_results(
17438 $wpdb->prepare(
17439 "SELECT SQL_CALC_FOUND_ROWS id, title FROM {$wpdb->prefix}fcal_calendar_events WHERE status = %s ORDER BY id DESC LIMIT %d OFFSET %d",
17440 [ 'active', $limit, $offset ]
17441 ),
17442 OBJECT
17443 );
17444 } else {
17445 $events = $wpdb->get_results(
17446 $wpdb->prepare(
17447 "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",
17448 [ 'active', $calendar_id, $limit, $offset ]
17449 ),
17450 OBJECT
17451 );
17452 }
17453
17454 $events_count = $wpdb->get_var( 'SELECT FOUND_ROWS();' );
17455
17456 $options = [];
17457 if ( ! empty( $events ) ) {
17458 foreach ( $events as $event ) {
17459 $options[] = [
17460 'label' => $event->title,
17461 'value' => $event->id,
17462 ];
17463 }
17464 }
17465 return [
17466 'options' => $options,
17467 'hasMore' => $events_count > $limit && $events_count > $offset,
17468 ];
17469
17470 }
17471
17472 /**
17473 * Get Fluent Booking last data.
17474 *
17475 * @param array $data data.
17476 *
17477 * @return array
17478 */
17479 public function search_fluent_booking_last_data( $data ) {
17480 global $wpdb;
17481 $trigger = $data['search_term'];
17482 $course_data = [];
17483 $lesson_data = [];
17484
17485 $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
17486 $context = [
17487 'pluggable_data' => json_decode( $sample_data, true ),
17488 'response_type' => 'sample',
17489 ];
17490 if ( 'fluent_booking_appointment_cancelled' === $trigger ) {
17491 $status = 'cancelled';
17492 } elseif ( 'fluent_booking_appointment_completed' === $trigger ) {
17493 $status = 'completed';
17494 }
17495
17496 $event_id = (int) $data['filter']['event_id']['value'];
17497 if ( 'fluent_booking_appointment_cancelled' === $trigger || 'fluent_booking_appointment_completed' === $trigger ) {
17498 if ( $event_id > 0 ) {
17499 $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 ) );
17500 } else {
17501 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE status= %s ORDER BY id DESC LIMIT 1", $status ) );
17502 }
17503 } elseif ( 'fluent_booking_new_appointment_booked' === $trigger ) {
17504 if ( $event_id > 0 ) {
17505 $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' ) );
17506 } else {
17507 $booking = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_bookings WHERE status != %s ORDER BY id DESC LIMIT 1", 'cancelled' ) );
17508 }
17509 }
17510
17511 if ( ! empty( $booking ) ) {
17512 $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 ) );
17513
17514 $booking_event = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}fcal_calendar_events WHERE id= %d", $booking->event_id ) );
17515 $booking_data = $booking;
17516 $booking_data->custom_fields = $booking_meta->value;
17517 $booking_array = [
17518 'booking' => $booking_data,
17519 'event' => $booking_event,
17520 ];
17521
17522
17523 $booking_array_response = self::recursive_unserialize( $booking_array );
17524 $context['pluggable_data'] = $booking_array_response;
17525 $context['response_type'] = 'live';
17526 }
17527 return $context;
17528 }
17529
17530 /**
17531 * Recursive unserilize.
17532 *
17533 * @param array $data data.
17534 *
17535 * @return array|mixed
17536 */
17537 public static function recursive_unserialize( $data ) {
17538 if ( is_array( $data ) ) {
17539 foreach ( $data as $key => $value ) {
17540 $data[ $key ] = self::recursive_unserialize( $value );
17541 }
17542 return $data;
17543 } elseif ( is_object( $data ) && 'stdClass' === get_class( $data ) ) {
17544 foreach ( $data as $property => $value ) {
17545 $data->$property = self::recursive_unserialize( $value );
17546 }
17547 return $data;
17548 } elseif ( is_string( $data ) && self::is_serialized( strval( $data ) ) ) {
17549 return unserialize( $data );
17550 } else {
17551 return $data;
17552 }
17553
17554 }
17555
17556 /**
17557 * Check if string serialized.
17558 *
17559 * @param string $data data.
17560 *
17561 * @return bool
17562 */
17563 public static function is_serialized( $data ) {
17564 $unserialized = unserialize( $data );
17565 if ( 'b:0;' === $data || false !== $unserialized ) {
17566 return true;
17567 } else {
17568 return false;
17569 }
17570 }
17571
17572 /**
17573 * Search Tutor LMS data.
17574 *
17575 * @param array $data data.
17576 * @return array|void|mixed
17577 */
17578 public function search_tutor_lms_last_data( $data ) {
17579 global $wpdb;
17580 $post_type = $data['post_type'];
17581 $trigger = $data['search_term'];
17582 $context = [];
17583
17584 if ( ! function_exists( 'tutor_utils' ) ) {
17585 return [];
17586 }
17587
17588 $post_id = -1;
17589 if ( 'course_enrolled' === $trigger || 'tutor_courses_question' === $trigger ) {
17590 $post_id = $data['filter']['tutor_course']['value'];
17591 } elseif ( 'quiz_attempt_percentage' === $trigger || 'quiz_failed' === $trigger || 'quiz_passed' === $trigger ) {
17592 $post_id = $data['filter']['quiz_id']['value'];
17593 }
17594
17595 if ( 'course_enrolled' === $trigger ) {
17596 if ( -1 === $post_id ) {
17597 $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 ) );
17598 } else {
17599 $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 ) );
17600 }
17601 } elseif ( 'tutor_courses_question' === $trigger ) {
17602 if ( -1 === $post_id ) {
17603 $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 ) );
17604 } else {
17605 $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 ) );
17606 }
17607 } elseif ( 'quiz_passed' === $trigger ) {
17608 if ( -1 === $post_id ) {
17609 $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 ) );
17610 } else {
17611 $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 ) );
17612 }
17613 } elseif ( 'quiz_failed' === $trigger ) {
17614 if ( -1 === $post_id ) {
17615 $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 ) );
17616 } else {
17617 $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 ) );
17618 }
17619 } elseif ( 'quiz_attempt_percentage' == $trigger ) {
17620 $condition_compare = $data['filter']['condition_compare']['value'];
17621 $percentage = $data['filter']['percentage']['value'];
17622 if ( -1 === $post_id ) {
17623 $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
17624 } else {
17625 $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
17626 }
17627 }
17628
17629 if ( ! empty( $result ) ) {
17630 switch ( $trigger ) {
17631 case 'course_enrolled':
17632 $result_item_id = $result[0]->post_parent;
17633 $result_user_id = $result[0]->post_author;
17634 $context_data = array_merge(
17635 WordPress::get_user_context( $result_user_id ),
17636 WordPress::get_post_context( $result_item_id )
17637 );
17638 $context_data['tutor_course'] = $result_item_id;
17639 break;
17640 case 'tutor_courses_question':
17641 $date = $result[0]->comment_date;
17642 $data = [
17643 'comment_post_ID' => $result[0]->comment_post_ID,
17644 'comment_author' => $result[0]->comment_author,
17645 'comment_date' => $date,
17646 'comment_date_gmt' => get_gmt_from_date( $date ),
17647 'comment_content' => $result[0]->comment_content,
17648 'comment_approved' => 'approved',
17649 'comment_agent' => 'TutorLMSPlugin',
17650 'comment_type' => 'tutor_q_and_a',
17651 'comment_parent' => $result[0]->comment_parent,
17652 'user_id' => $result[0]->user_id,
17653 ];
17654 $context_data['tutor_course'] = $result[0]->comment_post_ID;
17655 $context_data['data'] = $data;
17656 break;
17657 case 'quiz_attempt_percentage':
17658 case 'quiz_failed':
17659 case 'quiz_passed':
17660 $attempt = tutor_utils()->get_attempt( $result[0]->attempt_id );
17661 $context_data = WordPress::get_user_context( $result[0]->user_id );
17662 $context_data['quiz_id'] = $attempt->quiz_id;
17663 $context_data['attempt_id'] = $result[0]->attempt_id;
17664 $context_data['attempt'] = $attempt;
17665 break;
17666 default:
17667 return;
17668 }
17669 if ( ! empty( $context_data ) ) {
17670 $context['pluggable_data'] = $context_data;
17671 $context['response_type'] = 'live';
17672 }
17673 } elseif ( empty( $result ) ) {
17674 switch ( $trigger ) {
17675 case 'course_enrolled':
17676 $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"} ';
17677 break;
17678 case 'tutor_courses_question':
17679 $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"}';
17680 break;
17681 case 'quiz_attempt_percentage':
17682 case 'quiz_failed':
17683 case 'quiz_passed':
17684 $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"}';
17685 break;
17686 default:
17687 return;
17688 }
17689 $context = json_decode( $sample_data, true );
17690 }
17691
17692 return $context;
17693 }
17694
17695 /**
17696 * Get Asgorus Forum list
17697 *
17698 * @param array $data data.
17699 *
17700 * @return array
17701 */
17702 public function search_asgorus_forums_list( $data ) {
17703 if ( ! class_exists( 'AsgarosForum' ) ) {
17704 return [];
17705 }
17706 $category = $data['dynamic'];
17707 if ( is_array( $category ) ) {
17708 $category_id = $category['forum_category'];
17709 } else {
17710 $category_id = $category;
17711 }
17712 $asgaros_forum = new AsgarosForum();
17713 $forums = $asgaros_forum->get_forums( $category_id );
17714 $options = [];
17715 if ( ! empty( $forums ) ) {
17716 foreach ( $forums as $forum ) {
17717 $options[] = [
17718 'label' => $forum->name,
17719 'value' => $forum->id,
17720 ];
17721 }
17722 }
17723 return [
17724 'options' => $options,
17725 'hasMore' => false,
17726 ];
17727 }
17728
17729 /**
17730 * Get Asgorus Categories list
17731 *
17732 * @param array $data data.
17733 *
17734 * @return array
17735 */
17736 public function search_asgorus_categories_list( $data ) {
17737 if ( ! class_exists( 'AsgarosForum' ) ) {
17738 return [];
17739 }
17740 $asgaros_forum = new AsgarosForum();
17741 $categories = (array) $asgaros_forum->content->get_categories();
17742 $options = [];
17743
17744 if ( ! empty( $categories ) ) {
17745 foreach ( $categories as $category ) {
17746 $options[] = [
17747 'label' => $category->name,
17748 'value' => $category->term_id,
17749 ];
17750 }
17751 }
17752 return [
17753 'options' => $options,
17754 'hasMore' => false,
17755 ];
17756 }
17757
17758 /**
17759 * Get Asgorus Topic lists
17760 *
17761 * @param array $data data.
17762 *
17763 * @return array
17764 */
17765 public function search_asgorus_topic_list( $data ) {
17766 if ( ! class_exists( 'AsgarosForum' ) ) {
17767 return [];
17768 }
17769 global $wpdb;
17770 $forum_id = $data['dynamic'];
17771 $asgaros_forum = new AsgarosForum();
17772 $sql = 'SELECT name,id FROM ' . $wpdb->prefix . 'forum_topics WHERE parent_id = %d AND closed = 0 ORDER BY id';
17773 $topics = $wpdb->get_results( $wpdb->prepare( $sql, $forum_id ), ARRAY_A );// @phpcs:ignore
17774 $options = [];
17775
17776 if ( ! empty( $topics ) ) {
17777 foreach ( $topics as $topic ) {
17778 $options[] = [
17779 'label' => $topic['name'],
17780 'value' => $topic['id'],
17781 ];
17782 }
17783 }
17784 return [
17785 'options' => $options,
17786 'hasMore' => false,
17787 ];
17788 }
17789
17790 /**
17791 * Get Asgorus Topic Last Data
17792 *
17793 * @param array $data data.
17794 *
17795 * @return array
17796 */
17797 public function search_pluggables_asgaros_topic_last_data( $data ) {
17798 if ( ! class_exists( 'AsgarosForum' ) ) {
17799 return [];
17800 }
17801 $asgaros_forum = new AsgarosForum();
17802 $context = [];
17803 global $wpdb;
17804 $forum_id = $data['filter']['forum_id']['value'];
17805
17806 if ( -1 == $forum_id ) {
17807 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'forum_topics WHERE closed = 0 ORDER BY id DESC LIMIT 1', ARRAY_A );
17808 } else {
17809 $forum = $forum_id;
17810 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'forum_topics WHERE parent_id = %d AND closed = 0 ORDER BY id DESC LIMIT 1';
17811 $results = $wpdb->get_results( $wpdb->prepare( $sql, $forum ), ARRAY_A );// @phpcs:ignore
17812 }
17813
17814 if ( ! empty( $results ) ) {
17815 $sql_post = 'SELECT * FROM ' . $wpdb->prefix . 'forum_posts WHERE parent_id = %d ORDER BY id DESC LIMIT 1';
17816 $results_post = $wpdb->get_results( $wpdb->prepare( $sql_post, $results[0]['id'] ), ARRAY_A ); // @phpcs:ignore
17817 $context['pluggable_data']['forum_id'] = $results[0]['parent_id'];
17818 $context['pluggable_data']['topic_id'] = $results[0]['id'];
17819 $context['pluggable_data']['post_id'] = $results_post[0]['id'];
17820 $context['pluggable_data']['forum'] = $asgaros_forum->content->get_forum( $results[0]['parent_id'] );
17821 $context['pluggable_data']['topic'] = $asgaros_forum->content->get_topic( $results[0]['id'] );
17822 $context['pluggable_data']['post'] = $asgaros_forum->content->get_post( $results_post[0]['id'] );
17823
17824 $context['pluggable_data']['author'] = WordPress::get_user_context( $results[0]['author_id'] );
17825 $context['response_type'] = 'live';
17826 } else {
17827 $context = json_decode( '{"pluggable_data":{"forum_id":"1","topic_id":"2","forum":{"id":"1","name":"First Forum","parent_id":"64","parent_forum":"0","description":"My first forum.","icon":"fas fa-comments","sort":"1","forum_status":"normal","slug":"first-forum"},"topic":{"id":"2","parent_id":"1","author_id":"1","views":"2","name":"other topic","sticky":"0","closed":"0","approved":"1","slug":"other-topic"},"user":{"wp_user_id":1,"user_login":"suredev","display_name":"SureDev","user_firstname":"Sure","user_lastname":"Dev","user_email":"dev-test@example.com","user_role":["administrator"]}},"response_type":"sample"}', true );// @phpcs:ignore
17828 }
17829 return $context;
17830 }
17831
17832 /**
17833 * Get Asgorus Topic Reply Last Data
17834 *
17835 * @param array $data data.
17836 *
17837 * @return array
17838 */
17839 public function search_pluggables_asgaros_reply_last_data( $data ) {
17840 if ( ! class_exists( 'AsgarosForum' ) ) {
17841 return [];
17842 }
17843 $asgaros_forum = new AsgarosForum();
17844 $context = [];
17845 global $wpdb;
17846 $forum_id = $data['filter']['forum_id']['value'];
17847 $topic_id = $data['filter']['forum_id']['value'];
17848 if ( -1 == $topic_id ) {
17849 $results = $wpdb->get_results( 'SELECT * from ' . $wpdb->prefix . 'forum_posts ORDER BY id DESC LIMIT 1', ARRAY_A );
17850 } else {
17851 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'forum_posts WHERE parent_id = %d ORDER BY id DESC LIMIT 1';
17852 $results = $wpdb->get_results( $wpdb->prepare( $sql, $topic_id ), ARRAY_A );// @phpcs:ignore
17853 }
17854
17855 if ( ! empty( $results ) ) {
17856 $topic_id = $results[0]['parent_id'];
17857 $post_id = $results[0]['id'];
17858 $context['pluggable_data']['forum_id'] = $forum_id;
17859 $context['pluggable_data']['topic_id'] = $topic_id;
17860 $context['pluggable_data']['post_id'] = $post_id;
17861 $context['pluggable_data']['forum'] = $asgaros_forum->content->get_forum( $forum_id );
17862 $context['pluggable_data']['topic'] = $asgaros_forum->content->get_topic( $topic_id );
17863 $context['pluggable_data']['post'] = $asgaros_forum->content->get_post( $post_id );
17864
17865 $context['pluggable_data']['author'] = WordPress::get_user_context( $results[0]['author_id'] );
17866 $context['response_type'] = 'live';
17867 } else {
17868 $context = json_decode( '{"pluggable_data":{"forum_id":"1","topic_id":"2","forum":{"id":"1","name":"First Forum","parent_id":"64","parent_forum":"0","description":"My first forum.","icon":"fas fa-comments","sort":"1","forum_status":"normal","slug":"first-forum"},"topic":{"id":"2","parent_id":"1","author_id":"1","views":"2","name":"other topic","sticky":"0","closed":"0","approved":"1","slug":"other-topic"},"user":{"wp_user_id":1,"user_login":"suredev","display_name":"SureDev","user_firstname":"Sure","user_lastname":"Dev","user_email":"dev-test@example.com","user_role":["administrator"]}},"response_type":"sample"}', true );// @phpcs:ignore
17869 }
17870 return $context;
17871 }
17872
17873 /**
17874 * Get WPLoyalty Points Awarded Last Data
17875 *
17876 * @param array $data data.
17877 *
17878 * @return array|mixed|string
17879 */
17880 public function search_wp_loyalty_points_awarded_customer( $data ) {
17881
17882 $context = [];
17883 global $wpdb;
17884 if ( ! class_exists( 'Wlr\App\Helpers\Base' ) ) {
17885 return [];
17886 }
17887 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'wlr_earn_campaign_transaction WHERE transaction_type = %s ORDER BY id DESC LIMIT 1';
17888 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'credit' ), ARRAY_A );// @phpcs:ignore
17889
17890 if ( ! empty( $results ) ) {
17891 $context['pluggable_data']['user_email'] = $results[0]['user_email'];
17892 $context['pluggable_data']['points_earned'] = $results[0]['points'];
17893 $context['pluggable_data']['action_type'] = $results[0]['action_type'];
17894 $base_helper = new \Wlr\App\Helpers\Base();
17895 $user = $base_helper->getPointUserByEmail( $results[0]['user_email'] );
17896 $points_sql = 'SELECT * FROM ' . $wpdb->prefix . 'wlr_expire_points
17897 WHERE user_email = %s ORDER BY id DESC LIMIT 1';
17898 $points_results = $wpdb->get_results( $wpdb->prepare( $points_sql, $results[0]['user_email'] ), ARRAY_A );// @phpcs:ignore
17899 $context['pluggable_data']['user'] = $user;
17900 if ( ! empty( $points_results ) ) {
17901 $expire_date = $points_results[0]['expire_date'];
17902 $timestamp = is_numeric( $expire_date ) ? (int) $expire_date : null;
17903 $date_format = get_option( 'date_format' );
17904 if ( is_string( $date_format ) ) {
17905 $context['pluggable_data']['point_expiry_date'] = wp_date( $date_format, $timestamp );
17906 }
17907 }
17908 $context['response_type'] = 'live';
17909 } else {
17910 $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
17911 }
17912 return $context;
17913 }
17914
17915 /**
17916 * Get WPLoyalty Campaign Type List
17917 *
17918 * @param array $data data.
17919 *
17920 * @return array
17921 */
17922 public function search_wp_loyalty_action_type_list( $data ) {
17923 $options = [];
17924 if ( ! class_exists( 'Wlr\App\Helpers\Woocommerce' ) ) {
17925 return [];
17926 }
17927
17928 $woocommerce_helper = new \Wlr\App\Helpers\Woocommerce();
17929 $action_types = $woocommerce_helper->getActionTypes();
17930
17931 if ( ! empty( $action_types ) ) {
17932 foreach ( $action_types as $key => $type ) {
17933 $options[] = [
17934 'label' => $type,
17935 'value' => $key,
17936 ];
17937 }
17938 }
17939 return [
17940 'options' => $options,
17941 'hasMore' => false,
17942 ];
17943 }
17944
17945 /**
17946 * Get last data for trigger.
17947 *
17948 * @param array $data data.
17949 * @return array
17950 */
17951 public function search_slicewp_last_data( $data ) {
17952 global $wpdb;
17953
17954 $context = [];
17955 $context['response_type'] = 'sample';
17956
17957 $user_data = WordPress::get_sample_user_context();
17958
17959
17960 if ( ! function_exists( 'slicewp_get_affiliate' ) ) {
17961 return [];
17962 }
17963
17964 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
17965
17966 if ( in_array( $term, [ 'slicewp_new_affiliate', 'slicewp_update_affiliate' ], true ) ) {
17967 $affiliate = [
17968 'id' => 14,
17969 'user_id' => 25,
17970 'date_created' => '2024-03-14 12:35:50',
17971 'date_modified' => '2024-03-14 12:36:20',
17972 'payment_email' => 'testcustomer12@gmail.com',
17973 'website' => '',
17974 'status' => 'active',
17975 'parent_id' => 0,
17976 ];
17977 $context['pluggable_data'] = array_merge( $affiliate, [ 'user' => $user_data ] );
17978
17979 if ( ( ! empty( $data['filter'] ) && '-1' === $data['filter']['affiliate_id']['value'] ) || empty( $data['filter'] ) ) {
17980 $query = $wpdb->prepare(
17981 "
17982 SELECT *
17983 FROM {$wpdb->prefix}slicewp_affiliates
17984 WHERE status = %s
17985 ORDER BY id DESC
17986 LIMIT 1",
17987 'active'
17988 );
17989 $affiliate_results = $wpdb->get_row( $query ); //phpcs:ignore
17990
17991 } else {
17992 $affiliate_id = $data['filter']['affiliate_id']['value'];
17993 $query = $wpdb->prepare(
17994 "
17995 SELECT *
17996 FROM {$wpdb->prefix}slicewp_affiliates
17997 WHERE status = %s AND id = %d
17998 ORDER BY id DESC
17999 LIMIT 1",
18000 'active',
18001 $affiliate_id
18002 );
18003 $affiliate_results = $wpdb->get_row( $query ); //phpcs:ignore
18004
18005 }
18006
18007
18008
18009
18010 if ( ! empty( $affiliate_results ) ) {
18011 $context['pluggable_data'] = (array) $affiliate_results;
18012
18013 $user_data = WordPress::get_user_context( $affiliate_results->user_id );
18014 $context['pluggable_data']['user'] = $user_data;
18015 $context['response_type'] = 'live';
18016 }
18017 } elseif ( in_array( $term, [ 'slicewp_new_commission', 'slicewp_update_commission' ], true ) ) {
18018 $commission = [
18019 'id' => 14,
18020 'user_id' => 25,
18021 'date_created' => '2024-03-14 12:35:50',
18022 'date_modified' => '2024-03-14 12:36:20',
18023 'payment_email' => 'testcustomer12@gmail.com',
18024 'website' => '',
18025 'status' => 'active',
18026 'parent_id' => 0,
18027 ];
18028 $context['pluggable_data'] = array_merge( $commission, [ 'user' => $user_data ] );
18029 $affiliate_id = $data['filter']['affiliate_id']['value'];
18030 if ( -1 === $data['filter']['commission_id']['value'] ) {
18031 $query = $wpdb->prepare(
18032 "
18033 SELECT *
18034 FROM {$wpdb->prefix}slicewp_commissions WHERE affiliate_id=%d ORDER BY id DESC limit 1",
18035 $affiliate_id
18036 );
18037 $commission_results = $wpdb->get_row( $query ); //phpcs:ignore
18038 } else {
18039 $commission_id = $data['filter']['commission_id']['value'];
18040 $query = $wpdb->prepare(
18041 "
18042 SELECT *
18043 FROM {$wpdb->prefix}slicewp_commissions WHERE id= %d AND affiliate_id=%d ORDER BY id DESC limit 1",
18044 $commission_id,
18045 $affiliate_id
18046 );
18047 $commission_results = $wpdb->get_row( $query ); //phpcs:ignore
18048 }
18049
18050
18051
18052
18053 if ( ! empty( $commission_results ) ) {
18054 $context['pluggable_data'] = (array) $commission_results;
18055 $affiliate = slicewp_get_affiliate( $commission_results->affiliate_id );
18056 $user_id = $affiliate->get( 'user_id' );
18057 $user_data = WordPress::get_user_context( $user_id );
18058 $context['pluggable_data']['user'] = $user_data;
18059 $context['response_type'] = 'live';
18060 }
18061 }
18062
18063 return $context;
18064 }
18065
18066
18067 /**
18068 * Get Ninja Tables Last Data
18069 *
18070 * @param array $data data.
18071 *
18072 * @return array
18073 */
18074 public function search_ninja_tables_last_data( $data ) {
18075 $context = [];
18076 global $wpdb;
18077 $table_id = isset( $data['filter']['table_id']['value'] ) ? $data['filter']['table_id']['value'] : -1;
18078
18079 if ( -1 == $table_id ) {
18080 $results = $wpdb->get_row( 'SELECT * FROM ' . $wpdb->prefix . 'ninja_table_items ORDER BY id DESC LIMIT 1', ARRAY_A );
18081 } else {
18082 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'ninja_table_items WHERE table_id = %d ORDER BY id DESC LIMIT 1';
18083 $results = $wpdb->get_row( $wpdb->prepare( $sql, $table_id ), ARRAY_A );// @phpcs:ignore
18084 }
18085
18086 if ( ! empty( $results ) ) {
18087 $results['value'] = json_decode( $results['value'], true );
18088 $context['pluggable_data'] = $results;
18089 $context['pluggable_data']['owner'] = WordPress::get_user_context( $results['owner_id'] );
18090 $context['response_type'] = 'live';
18091 } else {
18092 $context = json_decode( '{"pluggable_data":{"id":"24","position":null,"table_id":"17484","owner_id":"29","attribute":"value","settings":null,"value":{"id":"3","name":"Table1","class":"Dev","gender":"Female"},"created_at":"2024-03-21 13:11:25","updated_at":"2024-03-21 13:11:25","owner":{"wp_user_id":29,"user_login":"testingdsd","display_name":"suretest","user_firstname":"John","user_lastname":"Dev","user_email":"johndoe@email.com","user_role":["editor"]}},"response_type":"sample"}', true );
18093 }
18094 return $context;
18095 }
18096
18097 /**
18098 * Get Ninja Tables Fields
18099 *
18100 * @param array $data data.
18101 *
18102 * @return array
18103 */
18104 public function search_ninja_table_fields( $data ) {
18105
18106 $context = [];
18107 global $wpdb;
18108 $table_id = isset( $data['dynamic'] ) ? $data['dynamic'] : -1;
18109 $fields = [];
18110 if ( $table_id > 0 ) {
18111 $fields = get_post_meta( $table_id, '_ninja_table_columns', true );
18112 }
18113
18114 return (array) $fields;
18115 }
18116
18117 /**
18118 * Get Late Point Booking Fields
18119 *
18120 * @param array $data data.
18121 *
18122 * @return array|void
18123 */
18124 public function search_late_point_booking_fields( $data ) {
18125 if ( ! class_exists( 'OsCustomFieldsController' ) ) {
18126
18127 return;
18128 }
18129 global $wpdb;
18130 $booking_fields = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}latepoint_settings WHERE name = %s", 'custom_fields_for_booking' ) );
18131 $fields = [];
18132 if ( ! empty( $booking_fields ) ) {
18133 $fields = json_decode( $booking_fields->value, true );
18134 }
18135 return (array) $fields;
18136 }
18137
18138 /** Search Voxel fields for action.
18139 *
18140 * @param array $data data.
18141 * @return array
18142 */
18143 public function search_voxel_custom_fields( $data ) {
18144 $post_type_fields = [];
18145 $post_type = $data['post_type'];
18146 if ( ! class_exists( '\Voxel\Post_Type' ) ) {
18147 return [];
18148 }
18149 $fields = \Voxel\Post_Type::get( $post_type )->get_fields();
18150 foreach ( $fields as $key => $field ) {
18151 if ( 'step-general' == $key ) {
18152 continue;
18153 }
18154 $field_props = $field->get_props();
18155 $post_type_fields[ $key ] = $field_props;
18156 }
18157 $context['fields'] = $post_type_fields;
18158 return $context;
18159 }
18160
18161 /**
18162 * Get Voxel Post Types lists
18163 *
18164 * @param array $data data.
18165 *
18166 * @return array
18167 */
18168 public function search_voxel_posttype_list( $data ) {
18169 $options = [];
18170 $voxel_post_types = [];
18171 if ( class_exists( '\Voxel\Post_Type' ) ) {
18172 $voxel_post_types = \Voxel\Post_Type::get_voxel_types();
18173 }
18174
18175 if ( ! empty( $voxel_post_types ) ) {
18176 foreach ( $voxel_post_types as $key => $voxel_post_type ) {
18177 $post_type = get_post_type_object( $key );
18178 if ( $post_type ) {
18179 if ( in_array( $post_type->name, [ 'collection' ], true ) ) {
18180 continue;
18181 }
18182 $options[] = [
18183 'label' => esc_attr( $post_type->labels->singular_name ),
18184 'value' => $post_type->name,
18185 ];
18186 }
18187 }
18188 }
18189 return [
18190 'options' => $options,
18191 'hasMore' => false,
18192 ];
18193 }
18194
18195 /**
18196 * Get Voxel Users Last Data
18197 *
18198 * @param array $data data.
18199 *
18200 * @return array|mixed|string
18201 */
18202 public function search_voxel_users_triggers_last_data( $data ) {
18203
18204 global $wpdb;
18205 $context = [];
18206 $term = $data['search_term'];
18207 if ( 'direct_message_received' === $term ) {
18208 $sql = "SELECT * FROM {$wpdb->prefix}voxel_messages WHERE receiver_type LIKE %s AND sender_type LIKE %s ORDER BY id DESC LIMIT 1";
18209 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user', 'user' ), ARRAY_A );// @phpcs:ignore
18210 } elseif ( 'user_timeline_post_created' === $term ) {
18211 $sql = "SELECT vt.* FROM {$wpdb->prefix}voxel_timeline vt WHERE vt.feed = %s AND vt.moderation = %d ORDER BY vt.id DESC LIMIT 1";
18212 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user_timeline', 0 ), ARRAY_A );// @phpcs:ignore
18213 } elseif ( 'user_timeline_post_approved' === $term ) {
18214 $sql = "SELECT vt.* FROM {$wpdb->prefix}voxel_timeline vt WHERE vt.feed = %s AND vt.moderation = %d ORDER BY vt.id DESC LIMIT 1";
18215 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user_timeline', 1 ), ARRAY_A );// @phpcs:ignore
18216 }
18217 if ( 'direct_message_received' === $term ) {
18218 if ( ! empty( $results ) ) {
18219 $context['pluggable_data']['sender'] = WordPress::get_user_context( $results[0]['sender_id'] );
18220 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $results[0]['receiver_id'] );
18221 $context['pluggable_data']['content'] = $results[0]['content'];
18222 $context['response_type'] = 'live';
18223 } else {
18224 $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
18225 }
18226 } elseif ( 'user_timeline_post_created' === $term || 'user_timeline_post_approved' === $term ) {
18227 if ( ! empty( $results ) ) {
18228 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['user_id'] );
18229 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
18230 $status_details = \Voxel\Timeline\Status::get( $results[0]['id'] );
18231 foreach ( (array) $status_details as $key => $value ) {
18232 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
18233 $context['pluggable_data'][ $clean_key ] = $value;
18234 }
18235 }
18236 $context['response_type'] = 'live';
18237 } else {
18238 $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
18239 }
18240 }
18241 return $context;
18242 }
18243
18244
18245 /**
18246 * Get Voxel Membership Last Data
18247 *
18248 * @param array $data data.
18249 *
18250 * @return array|mixed|string
18251 */
18252 public function search_voxel_membership_triggers_last_data( $data ) {
18253
18254 global $wpdb;
18255 $context = [];
18256 $term = $data['search_term'];
18257 if ( ! class_exists( 'Voxel\Stripe' ) ) {
18258 return [];
18259 }
18260 if ( 'plan_activated' === $term || 'plan_canceled' === $term ) {
18261 $meta_key = \Voxel\Stripe::is_test_mode() ? 'voxel:test_plan' : 'voxel:plan';
18262 $sql = "SELECT
18263 m.user_id AS id,
18264 u.user_login AS title,
18265 u.user_email AS email,
18266 m.meta_value AS details,
18267 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) AS plan,
18268 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.amount' ) ) AS SIGNED ) AS amount,
18269 JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.status' ) ) AS status,
18270 CAST( JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.created' ) ) AS DATETIME ) AS created
18271 FROM {$wpdb->prefix}usermeta as m
18272 LEFT JOIN {$wpdb->prefix}users AS u ON m.user_id = u.ID
18273 WHERE m.meta_key = %s AND JSON_UNQUOTE( JSON_EXTRACT( m.meta_value, '$.plan' ) ) != 'default'
18274 ORDER BY m.user_id DESC
18275 LIMIT 25 OFFSET 0";
18276 $results = $wpdb->get_results( $wpdb->prepare( $sql, $meta_key ), ARRAY_A );// @phpcs:ignore
18277 } elseif ( 'user_registered' === $term ) {
18278 $sql = "SELECT DISTINCT u.ID
18279 FROM {$wpdb->prefix}users u
18280 JOIN {$wpdb->prefix}usermeta um ON u.ID = um.user_id
18281 WHERE um.meta_key = 'voxel:profile_id' ORDER BY ID DESC LIMIT 1";
18282 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18283 }
18284
18285 if ( 'plan_canceled' === $term ) {
18286 if ( ! empty( $results ) ) {
18287 if ( 'cancelled' == $results[0]['status'] ) {
18288 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
18289 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
18290 $context['response_type'] = 'live';
18291 } else {
18292 $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
18293 }
18294 } else {
18295 $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
18296 }
18297 } elseif ( 'plan_activated' === $term ) {
18298 if ( ! empty( $results ) ) {
18299 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['id'] );
18300 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
18301 $context['response_type'] = 'live';
18302 } else {
18303 $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
18304 }
18305 } elseif ( 'user_registered' === $term ) {
18306 if ( ! empty( $results ) ) {
18307 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['ID'] );
18308 $context['response_type'] = 'live';
18309 } else {
18310 $context['pluggable_data'] = WordPress::get_sample_user_context();
18311 $context['response_type'] = 'sample';
18312 }
18313 }
18314 return $context;
18315 }
18316
18317 /**
18318 * Get Voxel Orders Last Data
18319 *
18320 * @param array $data data.
18321 *
18322 * @return array|mixed|string
18323 */
18324 public function search_voxel_order_triggers_last_data( $data ) {
18325
18326 global $wpdb;
18327 $context = [];
18328 $term = $data['search_term'];
18329
18330 if ( ! class_exists( 'Voxel\Product_Types\Orders\Order' ) ) {
18331 return [];
18332 }
18333
18334 if ( 'order_placed' === $term || 'order_canceled' === $term ) {
18335 if ( 'order_placed' === $term ) {
18336 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders ORDER BY id DESC LIMIT 1";
18337 } elseif ( 'order_canceled' === $term ) {
18338 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' ORDER BY id DESC LIMIT 1";
18339 }
18340
18341 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18342 if ( ! empty( $results ) ) {
18343 // Get Order.
18344 $context['pluggable_data']['id'] = $results[0]['id'];
18345 $context['pluggable_data']['vendor_id'] = $results[0]['vendor_id'];
18346 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
18347 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
18348 $context['pluggable_data']['status'] = $results[0]['status'];
18349 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
18350 $context['pluggable_data']['subtotal'] = $results[0]['subtotal'];
18351 $context['pluggable_data']['total'] = $results[0]['total'];
18352 // Get order items.
18353 $order = \Voxel\Product_Types\Orders\Order::find(
18354 [
18355 'id' => $results[0]['id'],
18356 ]
18357 );
18358 $order_items = $order->get_items();
18359 $context['pluggable_data']['tax_amount'] = $order->get_tax_amount();
18360 $context['pluggable_data']['discount_amount'] = $order->get_discount_amount();
18361 $context['pluggable_data']['shipping_amount'] = $order->get_shipping_amount();
18362 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
18363 foreach ( $order_items as $item ) {
18364 $addon_data = [];
18365 if ( is_object( $item ) && method_exists( $item, 'get_addons' ) ) {
18366 // Get addon details.
18367 $addons = $item->get_addons();
18368 $addon_data = [];
18369
18370 // Add the addons to the simple entry.
18371 if ( $addons && isset( $addons['summary'] ) ) {
18372 $addon_data = $addons['summary'];
18373 }
18374 }
18375 $context['pluggable_data']['order_items'][] = [
18376 'id' => $item->get_id(),
18377 'type' => $item->get_type(),
18378 'currency' => $item->get_currency(),
18379 'quantity' => $item->get_quantity(),
18380 'subtotal' => $item->get_subtotal(),
18381 'product_id' => $item->get_post()->get_id(),
18382 'product_label' => $item->get_product_label(),
18383 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
18384 'product_link' => $item->get_product_link(),
18385 'description' => $item->get_product_description(),
18386 'addon_data' => $addon_data,
18387 ];
18388 // If booking item, get booking details.
18389 if ( 'booking' === $item->get_type() ) {
18390 $details = $item->get_order_page_details();
18391 $context['pluggable_data']['order_items']['booking_type'] = $details['booking']['type'];
18392 if ( isset( $details['booking']['count_mode'] ) ) {
18393 $context['pluggable_data']['order_items']['booking_count_mode'] = $details['booking']['count_mode'];
18394 }
18395 if ( 'date_range' === $details['booking']['type'] ) {
18396 $context['pluggable_data']['order_items']['booking_start_date'] = $details['booking']['start_date'];
18397 $context['pluggable_data']['order_items']['booking_end_date'] = $details['booking']['end_date'];
18398 } elseif ( 'single_day' === $details['booking']['type'] ) {
18399 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
18400 } elseif ( 'timeslots' === $details['booking']['type'] ) {
18401 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
18402 $context['pluggable_data']['order_items']['booking_slot_from'] = $details['booking']['slot']['from'];
18403 $context['pluggable_data']['order_items']['booking_slot_to'] = $details['booking']['slot']['to'];
18404 }
18405 }
18406 }
18407 // Get Customer.
18408 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
18409 $context['response_type'] = 'live';
18410 } else {
18411 $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
18412 }
18413 } elseif ( 'order_approved' === $term || 'order_declined' === $term ) {
18414 if ( 'order_approved' === $term ) {
18415 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'completed' ORDER BY id DESC LIMIT 1";
18416 } elseif ( 'order_declined' === $term ) {
18417 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' ORDER BY id DESC LIMIT 1";
18418 }
18419 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18420 if ( ! empty( $results ) ) {
18421 // Get Order.
18422 $context['pluggable_data']['id'] = $results[0]['id'];
18423 $context['pluggable_data']['vendor_id'] = $results[0]['vendor_id'];
18424 $context['pluggable_data']['details'] = json_decode( $results[0]['details'], true );
18425 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
18426 $context['pluggable_data']['status'] = $results[0]['status'];
18427 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
18428 $context['pluggable_data']['subtotal'] = $results[0]['subtotal'];
18429 $context['pluggable_data']['total'] = $results[0]['total'];
18430 // Get order items.
18431 $order = \Voxel\Product_Types\Orders\Order::find(
18432 [
18433 'id' => $results[0]['id'],
18434 ]
18435 );
18436 $order_items = $order->get_items();
18437 $context['pluggable_data']['tax_amount'] = $order->get_tax_amount();
18438 $context['pluggable_data']['discount_amount'] = $order->get_discount_amount();
18439 $context['pluggable_data']['shipping_amount'] = $order->get_shipping_amount();
18440 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
18441 foreach ( $order_items as $item ) {
18442 $addon_data = [];
18443 if ( is_object( $item ) && method_exists( $item, 'get_addons' ) ) {
18444 // Get addon details.
18445 $addons = $item->get_addons();
18446 $addon_data = [];
18447
18448 // Add the addons to the simple entry.
18449 if ( $addons && isset( $addons['summary'] ) ) {
18450 $addon_data = $addons['summary'];
18451 }
18452 }
18453 $context['pluggable_data']['order_items'][] = [
18454 'id' => $item->get_id(),
18455 'type' => $item->get_type(),
18456 'currency' => $item->get_currency(),
18457 'quantity' => $item->get_quantity(),
18458 'subtotal' => $item->get_subtotal(),
18459 'product_id' => $item->get_post()->get_id(),
18460 'product_label' => $item->get_product_label(),
18461 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
18462 'product_link' => $item->get_product_link(),
18463 'description' => $item->get_product_description(),
18464 'addon_data' => $addon_data,
18465 ];
18466 // If booking item, get booking details.
18467 if ( 'booking' === $item->get_type() ) {
18468 $details = $item->get_order_page_details();
18469 $context['pluggable_data']['order_items']['booking_type'] = $details['booking']['type'];
18470 if ( isset( $details['booking']['count_mode'] ) ) {
18471 $context['pluggable_data']['order_items']['booking_count_mode'] = $details['booking']['count_mode'];
18472 }
18473 if ( 'date_range' === $details['booking']['type'] ) {
18474 $context['pluggable_data']['order_items']['booking_start_date'] = $details['booking']['start_date'];
18475 $context['pluggable_data']['order_items']['booking_end_date'] = $details['booking']['end_date'];
18476 } elseif ( 'single_day' === $details['booking']['type'] ) {
18477 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
18478 } elseif ( 'timeslots' === $details['booking']['type'] ) {
18479 $context['pluggable_data']['order_items']['booking_date'] = $details['booking']['date'];
18480 $context['pluggable_data']['order_items']['booking_slot_from'] = $details['booking']['slot']['from'];
18481 $context['pluggable_data']['order_items']['booking_slot_to'] = $details['booking']['slot']['to'];
18482 }
18483 }
18484 }
18485 // Get Customer.
18486 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
18487 // Get Vendor.
18488 $context['pluggable_data']['vendor'] = WordPress::get_user_context( $results[0]['vendor_id'] );
18489 $context['response_type'] = 'live';
18490 } else {
18491 $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
18492 }
18493 } elseif ( 'order_promotion_activated' === $term || 'order_promotion_canceled' === $term || 'order_claim_listing' === $term ) {
18494 if ( 'order_promotion_activated' === $term ) {
18495 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'completed' AND details LIKE '%voxel:promotion%' ORDER BY id DESC LIMIT 1";
18496 } elseif ( 'order_promotion_canceled' === $term ) {
18497 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE status = 'canceled' AND details LIKE '%voxel:promotion%' ORDER BY id DESC LIMIT 1";
18498 } elseif ( 'order_claim_listing' === $term ) {
18499 $sql = "SELECT * FROM {$wpdb->prefix}vx_orders WHERE details LIKE '%voxel:claim%' ORDER BY id DESC LIMIT 1";
18500 }
18501 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18502 if ( ! empty( $results ) ) {
18503 // Get Order.
18504 $context['pluggable_data']['id'] = $results[0]['id'];
18505 $context['pluggable_data']['payment_method'] = $results[0]['payment_method'];
18506 $context['pluggable_data']['status'] = $results[0]['status'];
18507 $context['pluggable_data']['created_at'] = $results[0]['created_at'];
18508 // Get order items.
18509 $order = \Voxel\Product_Types\Orders\Order::find(
18510 [
18511 'id' => $results[0]['id'],
18512 ]
18513 );
18514 $order_items = $order->get_items();
18515 $context['pluggable_data']['details'] = $order->get_details();
18516 $context['pluggable_data']['order_item_count'] = $order->get_item_count();
18517 foreach ( $order_items as $item ) {
18518 $context['pluggable_data']['order_items'][] = [
18519 'id' => $item->get_id(),
18520 'type' => $item->get_type(),
18521 'currency' => $item->get_currency(),
18522 'quantity' => $item->get_quantity(),
18523 'subtotal' => $item->get_subtotal(),
18524 'product_id' => $item->get_post()->get_id(),
18525 'product_label' => $item->get_product_label(),
18526 'product_thumbnail_url' => $item->get_product_thumbnail_url(),
18527 'product_link' => $item->get_product_link(),
18528 'description' => $item->get_product_description(),
18529 ];
18530 }
18531 // Get Customer.
18532 $context['pluggable_data']['customer'] = WordPress::get_user_context( $results[0]['customer_id'] );
18533 $context['response_type'] = 'live';
18534 } else {
18535 $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
18536 }
18537 }
18538 return $context;
18539 }
18540
18541 /**
18542 * Get Voxel Timeline Comments Last Data
18543 *
18544 * @param array $data data.
18545 *
18546 * @return array|mixed|string
18547 */
18548 public function search_voxel_timeline_triggers_last_data( $data ) {
18549
18550 global $wpdb;
18551 $context = [];
18552 $term = $data['search_term'];
18553 $sql = '';
18554 if ( 'new_comment_timeline' === $term ) {
18555 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE parent_id IS NULL ORDER BY id DESC LIMIT 1";
18556 } elseif ( 'comment_reply_timeline' === $term ) {
18557 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE parent_id IS NOT NULL ORDER BY id DESC LIMIT 1";
18558 } elseif ( 'comment_liked' === $term ) {
18559 $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";
18560 } elseif ( 'user_timeline_post_liked' === $term ) {
18561 $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";
18562 } elseif ( 'user_timeline_post_quoted' === $term ) {
18563 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline WHERE feed = %s AND quote_of IS NOT NULL ORDER BY id DESC LIMIT 1";
18564 } elseif ( 'user_timeline_post_reposted' === $term ) {
18565 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline WHERE feed = %s AND repost_of IS NOT NULL ORDER BY id DESC LIMIT 1";
18566 }
18567 if ( 'user_timeline_post_reposted' === $term || 'user_timeline_post_quoted' === $term ) {
18568 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'user_timeline' ), ARRAY_A );// @phpcs:ignore
18569 } else {
18570 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
18571 }
18572 if ( 'new_comment_timeline' === $term ) {
18573 if ( ! empty( $results ) ) {
18574 $context['pluggable_data']['comment_by'] = WordPress::get_user_context( $results[0]['user_id'] );
18575 $context['pluggable_data']['id'] = $results[0]['id'];
18576 $context['pluggable_data']['status_id'] = $results[0]['status_id'];
18577 $context['pluggable_data']['content'] = $results[0]['content'];
18578 $context['response_type'] = 'live';
18579 } else {
18580 $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
18581 }
18582 } elseif ( 'comment_reply_timeline' === $term ) {
18583 if ( ! empty( $results ) ) {
18584 $comment_sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline_replies WHERE id = %d";
18585 $comment_result = $wpdb->get_results( $wpdb->prepare( $comment_sql, $results[0]['parent_id']), ARRAY_A );// @phpcs:ignore
18586 $context['pluggable_data']['replied_by'] = WordPress::get_user_context( $results[0]['user_id'] );
18587 $context['pluggable_data']['comment_by'] = WordPress::get_user_context( $comment_result[0]['user_id'] );
18588 $context['pluggable_data']['comment'] = $comment_result[0]['content'];
18589 $context['pluggable_data']['comment_id'] = $results[0]['parent_id'];
18590 $context['pluggable_data']['reply_id'] = $results[0]['id'];
18591 $context['pluggable_data']['reply'] = $results[0]['content'];
18592 $context['response_type'] = 'live';
18593 } else {
18594 $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
18595 }
18596 } elseif ( 'comment_liked' === $term ) {
18597 if ( ! empty( $results ) ) {
18598 $user = get_userdata( $results[0]['user_id'] );
18599 if ( $user ) {
18600 $user_data = (array) $user->data;
18601 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
18602 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
18603 $context['pluggable_data']['user_email'] = $user_data['user_email'];
18604 }
18605 $recipient_user = get_userdata( $results[0]['like_user_id'] );
18606 if ( $recipient_user ) {
18607 $recipient_user_data = (array) $recipient_user->data;
18608 $context['pluggable_data']['recipient']['user_display_name'] = $recipient_user_data['display_name'];
18609 $context['pluggable_data']['recipient']['user_name'] = $recipient_user_data['user_nicename'];
18610 $context['pluggable_data']['recipient']['user_email'] = $recipient_user_data['user_email'];
18611 }
18612 if ( class_exists( 'Voxel\Timeline\Reply' ) ) {
18613 $reply_details = \Voxel\Timeline\Reply::get( $results[0]['id'] );
18614 foreach ( (array) $reply_details as $key => $value ) {
18615 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
18616 $context['pluggable_data'][ $clean_key ] = $value;
18617 }
18618 }
18619 $context['response_type'] = 'live';
18620 } else {
18621 $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
18622 }
18623 } elseif ( 'user_timeline_post_liked' === $term ) {
18624 if ( ! empty( $results ) ) {
18625 $user = get_userdata( $results[0]['user_id'] );
18626 if ( $user ) {
18627 $user_data = (array) $user->data;
18628 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
18629 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
18630 $context['pluggable_data']['user_email'] = $user_data['user_email'];
18631 }
18632 $recipient_user = get_userdata( $results[0]['like_user_id'] );
18633 if ( $recipient_user ) {
18634 $recipient_user_data = (array) $recipient_user->data;
18635 $context['pluggable_data']['recipient']['user_display_name'] = $recipient_user_data['display_name'];
18636 $context['pluggable_data']['recipient']['user_name'] = $recipient_user_data['user_nicename'];
18637 $context['pluggable_data']['recipient']['user_email'] = $recipient_user_data['user_email'];
18638 }
18639 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
18640 $reply_details = \Voxel\Timeline\Status::get( $results[0]['id'] );
18641 foreach ( (array) $reply_details as $key => $value ) {
18642 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
18643 $context['pluggable_data'][ $clean_key ] = $value;
18644 }
18645 }
18646 $context['response_type'] = 'live';
18647 } else {
18648 $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
18649 }
18650 } elseif ( 'user_timeline_post_reposted' === $term || 'user_timeline_post_quoted' === $term ) {
18651 if ( ! empty( $results ) ) {
18652 $author = get_userdata( $results[0]['user_id'] );
18653 if ( $author ) {
18654 $author_data = (array) $author->data;
18655 $context['pluggable_data']['author_display_name'] = $author_data['display_name'];
18656 $context['pluggable_data']['author_name'] = $author_data['user_nicename'];
18657 $context['pluggable_data']['author_email'] = $author_data['user_email'];
18658 }
18659 $column = 'user_timeline_post_reposted' === $term ? 'repost_of' : 'quote_of';
18660 $recipient_sql = "SELECT user_id FROM {$wpdb->prefix}voxel_timeline WHERE id = %d";
18661 $recipient_results = $wpdb->get_results( $wpdb->prepare( $recipient_sql, $results[0][$column] ), ARRAY_A ); // @phpcs:ignore
18662 if ( ! empty( $recipient_results ) ) {
18663 $recipient_user = get_userdata( $recipient_results[0]['user_id'] );
18664 if ( $recipient_user ) {
18665 $recipient_user_data = (array) $recipient_user->data;
18666 $context['pluggable_data']['recipient']['user_display_name'] = $recipient_user_data['display_name'];
18667 $context['pluggable_data']['recipient']['user_name'] = $recipient_user_data['user_nicename'];
18668 $context['pluggable_data']['recipient']['user_email'] = $recipient_user_data['user_email'];
18669 }
18670 }
18671 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
18672 $status_details = \Voxel\Timeline\Status::get( $results[0]['id'] );
18673 $details_column = 'user_timeline_post_reposted' === $term ? 'repost_of' : 'quote_of';
18674 $related_details = \Voxel\Timeline\Status::get( $results[0][ $details_column ] );
18675 foreach ( [
18676 'status' => $status_details,
18677 $details_column => $related_details,
18678 ] as $key => $details ) {
18679 foreach ( (array) $details as $sub_key => $value ) {
18680 $clean_key = preg_replace( '/^\0.*?\0/', '', $sub_key );
18681 $context['pluggable_data'][ $key ][ $clean_key ] = $value;
18682 }
18683 }
18684 }
18685 $context['response_type'] = 'live';
18686 } else {
18687 if ( 'user_timeline_post_reposted' === $term ) {
18688 $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
18689 } else {
18690 $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
18691 }
18692 }
18693 }
18694 return $context;
18695 }
18696
18697 /**
18698 * Get Voxel Timeline Comments Last Data
18699 *
18700 * @param array $data data.
18701 *
18702 * @return array|mixed|string
18703 */
18704 public function search_voxel_posts_triggers_last_data( $data ) {
18705 $context = [];
18706 global $wpdb;
18707 $post_type = 'post';
18708 $term = $data['search_term'];
18709 if ( isset( $data['filter']['post_type']['value'] ) ) {
18710 $post_type = $data['filter']['post_type']['value'];
18711 }
18712 $results = [];
18713 if ( 'post_approved' === $term ) {
18714 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
18715 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type, 'publish'), ARRAY_A );// @phpcs:ignore
18716 } elseif ( 'post_rejected' === $term ) {
18717 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
18718 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type, 'rejected'), ARRAY_A );// @phpcs:ignore
18719 } elseif ( 'post_reviewed' === $term ) {
18720 $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
18721 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";
18722 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type, '%rating%' ), ARRAY_A );// @phpcs:ignore
18723 } elseif ( 'post_review_approved' === $term ) {
18724 $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
18725 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";
18726 $results = $wpdb->get_results( $wpdb->prepare( $sql, 1, $post_type, '%rating%', 'post_reviews' ), ARRAY_A );// @phpcs:ignore
18727 } elseif ( 'post_submitted' === $term ) {
18728 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
18729 $results = $wpdb->get_results( $wpdb->prepare($sql, $post_type), ARRAY_A );// @phpcs:ignore
18730 } elseif ( 'post_updated' === $term ) {
18731 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
18732 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type ), ARRAY_A );// @phpcs:ignore
18733 } elseif ( 'collection_post_submitted' === $term ) {
18734 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
18735 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'collection' ), ARRAY_A );// @phpcs:ignore
18736 } elseif ( 'collection_post_updated' === $term ) {
18737 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
18738 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'collection' ), ARRAY_A );// @phpcs:ignore
18739 } elseif ( 'new_wall_post_by_user' === $term ) {
18740 $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";
18741 $results = $wpdb->get_results( $wpdb->prepare( $sql, $post_type, '%rating%' ), ARRAY_A );// @phpcs:ignore
18742 } elseif ( 'wall_post_approved' === $term ) {
18743 $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";
18744 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'post_wall', $post_type, 1 ), ARRAY_A );// @phpcs:ignore
18745 } elseif ( 'timeline_post_approved' === $term ) {
18746 $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";
18747 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'post_timeline', 1, $post_type ), ARRAY_A );// @phpcs:ignore
18748 } elseif ( 'post_followed' === $term ) {
18749 $sql = "SELECT * FROM {$wpdb->prefix}voxel_followers WHERE object_type = %s LIMIT 1";
18750 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'post' ), ARRAY_A );// @phpcs:ignore
18751 }
18752 if ( 'post_approved' === $term || 'post_rejected' === $term || 'post_submitted' === $term || 'post_updated' === $term || 'timeline_post_approved' === $term ) {
18753 if ( ! empty( $results ) ) {
18754 $context['pluggable_data'] = WordPress::get_post_context( $results[0]['ID'] );
18755 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['ID'] );
18756 $context['response_type'] = 'live';
18757 } else {
18758 $context['pluggable_data'] = [
18759 'ID' => 557,
18760 'post' => 557,
18761 'post_author' => 1,
18762 'post_date' => '2022-11-18 12:18:14',
18763 'post_date_gmt' => '2022-11-18 12:18:14',
18764 'post_content' => 'Test Post Content',
18765 'post_title' => 'Test Post',
18766 'post_excerpt' => '',
18767 'post_status' => 'published',
18768 'comment_status' => 'open',
18769 'ping_status' => 'open',
18770 'post_password' => '',
18771 'post_name' => 'test-post',
18772 'to_ping' => '',
18773 'pinged' => '',
18774 'post_modified' => '2022-11-18 12:18:14',
18775 'post_modified_gmt' => '2022-11-18 12:18:14',
18776 'post_content_filtered' => '',
18777 'post_parent' => 0,
18778 'guid' => 'https://example.com/test-post/',
18779 'menu_order' => 0,
18780 'post_type' => 'post',
18781 'post_mime_type' => '',
18782 'comment_count' => 0,
18783 'filter' => 'raw',
18784 ];
18785 $context['response_type'] = 'sample';
18786 }
18787 } elseif ( 'post_reviewed' === $term || 'post_review_approved' === $term ) {
18788 if ( ! empty( $results ) ) {
18789 $context['pluggable_data'] = WordPress::get_post_context( $results[0]['post_id'] );
18790 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['post_id'] );
18791 $context['pluggable_data']['review_content'] = $results[0]['content'];
18792 $context['pluggable_data']['review_created_at'] = $results[0]['created_at'];
18793 $context['pluggable_data']['review_details'] = json_decode( $results[0]['details'], true );
18794 $context['pluggable_data']['review_by'] = WordPress::get_user_context( $results[0]['user_id'] );
18795 $context['response_type'] = 'live';
18796 } else {
18797 $context = json_decode( '{"pluggable_data":{"ID": 8291,"post_author": "1","post_date": "2024-03-18 09:01:54","post_date_gmt": "2024-03-18 09:01:54","post_content": "<p>PizzaCrust - Since 2009! Whether it\u2019s our iconic Sandwiches, wooden baked pizzas, signature sauce, original fresh dough or toppings, we invest in bringing the freshest and best ingredients to bring you the tastiest meal.<\/p>","post_title": "Pizza Crust","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "closed","post_password": "","post_name": "sach-pizza","to_ping": "","pinged": "","post_modified": "2024-03-18 09:01:54","post_modified_gmt": "2024-03-18 09:01:54","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/example-wpnew.local\/places\/sach-pizza\/","menu_order": 0,"post_type": "places","post_mime_type": "","comment_count": "0","filter": "raw","review_content": "Nice one","review_created_at": "2024-04-04 12:59:22","review_details": {"rating": {"score": -1,"custom-660": -1,"custom-978": -1,"custom-271": -1}},"review_by": {"wp_user_id": 188,"user_login": "dev4","display_name": "dev4","user_firstname": "","user_lastname": "","user_email": "dev4@yopmail.com","user_role": ["subscriber"]}},"response_type":"sample"}', true );// @phpcs:ignore
18798 }
18799 } elseif ( 'collection_post_submitted' === $term || 'collection_post_updated' === $term ) {
18800 if ( ! empty( $results ) ) {
18801 $context['pluggable_data'] = Voxel::get_post_fields( $results[0]['ID'] );
18802 $context['pluggable_data']['collection'] = WordPress::get_post_context( $results[0]['ID'] );
18803 $context['response_type'] = 'live';
18804 } else {
18805 $context = json_decode(
18806 '{"pluggable_data":{"field_step-general": null,"field_title": "First Collection","field_items": [8909,8293],"collection": {"ID": 9142,"post_author": "35",
18807 "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
18808 }
18809 } elseif ( 'new_wall_post_by_user' === $term || 'wall_post_approved' === $term ) {
18810 if ( ! empty( $results ) ) {
18811 $context['pluggable_data']['post'] = Voxel::get_post_fields( $results[0]['post_id'] );
18812 $user = get_userdata( $results[0]['user_id'] );
18813 if ( $user ) {
18814 $user_data = (array) $user->data;
18815 $context['pluggable_data']['user_display_name'] = $user_data['display_name'];
18816 $context['pluggable_data']['user_name'] = $user_data['user_nicename'];
18817 $context['pluggable_data']['user_email'] = $user_data['user_email'];
18818 $context['pluggable_data']['user_id'] = $results[0]['user_id'];
18819 }
18820 if ( class_exists( 'Voxel\Timeline\Status' ) ) {
18821 $args = [
18822 'post_id' => $results[0]['post_id'],
18823 'order_by' => 'created_at',
18824 'order' => 'desc',
18825 'feed' => $results[0]['feed'],
18826 'limit' => 1,
18827 'moderation' => $results[0]['moderation'],
18828 ];
18829 $statuses = \Voxel\Timeline\Status::query( $args );
18830 $status_details = $statuses['items'][0];
18831 foreach ( (array) $status_details as $key => $value ) {
18832 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
18833 if ( is_object( $value ) ) {
18834 $encoded_value = wp_json_encode( $value );
18835 if ( is_string( $encoded_value ) ) {
18836 $value = json_decode( $encoded_value, true );
18837 }
18838 }
18839 $context['pluggable_data']['wall_post'][ $clean_key ] = $value;
18840 }
18841 }
18842 $context['response_type'] = 'live';
18843 } else {
18844 $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
18845 }
18846 } elseif ( 'post_followed' === $term ) {
18847 if ( ! empty( $results ) ) {
18848 $follow_data = [
18849 'post_id' => $results[0]['object_id'],
18850 'user_id' => $results[0]['follower_id'],
18851 'status' => $results[0]['status'],
18852 ];
18853 $follow_data = array_merge( $follow_data, Voxel::get_post_fields( $results[0]['object_id'] ), WordPress::get_post_context( $results[0]['object_id'] ) );
18854 $followers_sql = "SELECT COUNT(*) FROM {$wpdb->prefix}voxel_followers WHERE object_id= %d";
18855 $followers = $wpdb->get_var( $wpdb->prepare( $followers_sql, $results[0]['object_id'] ));// @phpcs:ignore
18856 $follow_data['total_followers'] = $followers;
18857 $context['pluggable_data'] = $follow_data;
18858 $context['response_type'] = 'live';
18859 } else {
18860 $context = json_decode( '{"pluggable_data":{"field_step-general":null,"field_title":"How Data Fundamentally Changed Marketing","field__thumbnail_id":{"_thumbnail_id_0_url":"https://example.com/wp-content/uploads/2025/01/169cbc8.jpg"},"field_taxonomy":"General","ID": 8291,"post_author": "1","post_date": "2024-03-18 09:01:54","post_date_gmt": "2024-03-18 09:01:54","post_content": "<p>PizzaCrust - Since 2009! Whether it\u2019s our iconic Sandwiches, wooden baked pizzas, signature sauce, original fresh dough or toppings, we invest in bringing the freshest and best ingredients to bring you the tastiest meal.<\/p>","post_title": "Pizza Crust","post_excerpt": "","post_status": "publish","comment_status": "open","ping_status": "closed","post_password": "","post_name": "sach-pizza","to_ping": "","pinged": "","post_modified": "2024-03-18 09:01:54","post_modified_gmt": "2024-03-18 09:01:54","post_content_filtered": "","post_parent": 0,"guid": "https:\/\/example-wpnew.local\/places\/sach-pizza\/","menu_order": 0,"post_type": "places","post_mime_type": "","comment_count": "0","filter": "raw","total_followers":4,"post_id": 447,"user_id": 2,"status":1},"response_type":"sample"}', true );// @phpcs:ignore
18861 }
18862 }
18863 return $context;
18864 }
18865
18866 /**
18867 * Get Voxel Profiles Last Data
18868 *
18869 * @param array $data data.
18870 *
18871 * @return array|mixed|string
18872 */
18873 public function search_voxel_profiles_triggers_last_data( $data ) {
18874
18875 global $wpdb;
18876 $context = [];
18877 $term = $data['search_term'];
18878
18879 if ( ! class_exists( 'Voxel\Timeline\Status' ) || ! class_exists( 'Voxel\Post_Type' ) ) {
18880 return [];
18881 }
18882
18883 if ( 'profile_created' === $term ) {
18884 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY ID DESC LIMIT 1";
18885 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
18886 } elseif ( 'profile_approved' === $term ) {
18887 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
18888 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile', 'publish' ), ARRAY_A );// @phpcs:ignore
18889 } elseif ( 'profile_rejected' === $term ) {
18890 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s AND post_status LIKE %s ORDER BY ID DESC LIMIT 1";
18891 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile', 'rejected' ), ARRAY_A );// @phpcs:ignore
18892 } elseif ( 'profile_reviewed' === $term ) {
18893 $sql = "SELECT * FROM {$wpdb->prefix}voxel_timeline vt LEFT JOIN {$wpdb->prefix}posts pm
18894 ON vt.post_id = pm.ID
18895 WHERE pm.post_type LIKE %s AND vt.details IS NOT NULL ORDER BY vt.id DESC LIMIT 1";
18896 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
18897 } elseif ( 'profile_updated' === $term ) {
18898 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE %s ORDER BY post_modified DESC LIMIT 1";
18899 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
18900 } elseif ( 'profile_new_wall_post' === $term ) {
18901 $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";
18902 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'profile' ), ARRAY_A );// @phpcs:ignore
18903 }
18904 if ( 'profile_created' === $term || 'profile_approved' === $term || 'profile_rejected' === $term || 'profile_updated' === $term ) {
18905 if ( ! empty( $results ) ) {
18906 $context['pluggable_data'] = Voxel::get_post_fields( $results[0]['ID'] );
18907 $user = get_userdata( $results[0]['post_author'] );
18908 if ( $user ) {
18909 $user_data = (array) $user->data;
18910 $context['pluggable_data']['profile_display_name'] = $user_data['display_name'];
18911 $context['pluggable_data']['profile_email'] = $user_data['user_email'];
18912 $context['pluggable_data']['profile_user_id'] = $results[0]['post_author'];
18913 $context['pluggable_data']['profile_id'] = $results[0]['ID'];
18914 }
18915 $context['response_type'] = 'live';
18916 } else {
18917 $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
18918 }
18919 } elseif ( 'profile_new_wall_post' === $term ) {
18920 if ( ! empty( $results ) ) {
18921 $context['pluggable_data']['profile'] = Voxel::get_post_fields( $results[0]['post_id'] );
18922 $user = get_userdata( $results[0]['user_id'] );
18923 if ( $user ) {
18924 $user_data = (array) $user->data;
18925 $context['pluggable_data']['profile_display_name'] = $user_data['display_name'];
18926 $context['pluggable_data']['profile_name'] = $user_data['user_nicename'];
18927 $context['pluggable_data']['profile_email'] = $user_data['user_email'];
18928 $context['pluggable_data']['profile_user_id'] = $results[0]['user_id'];
18929 }
18930 // Get the status details.
18931 $args = [
18932 'post_id' => $results[0]['post_id'],
18933 'order_by' => 'created_at',
18934 'order' => 'desc',
18935 'feed' => $results[0]['feed'],
18936 'limit' => 1,
18937 'moderation' => $results[0]['moderation'],
18938 ];
18939 $statuses = \Voxel\Timeline\Status::query( $args );
18940 $status_details = $statuses['items'][0];
18941 foreach ( (array) $status_details as $key => $value ) {
18942 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
18943 if ( is_object( $value ) ) {
18944 $encoded_value = wp_json_encode( $value );
18945 if ( is_string( $encoded_value ) ) {
18946 $value = json_decode( $encoded_value, true );
18947 }
18948 }
18949 $context['pluggable_data']['wall_post'][ $clean_key ] = $value;
18950 }
18951 $context['response_type'] = 'live';
18952 } else {
18953 $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
18954 }
18955 } elseif ( 'profile_reviewed' === $term ) {
18956 if ( ! empty( $results ) ) {
18957 // Get the review details.
18958 $args = [
18959 'post_id' => $results[0]['post_id'],
18960 'order_by' => 'created_at',
18961 'order' => 'desc',
18962 'feed' => $results[0]['feed'],
18963 'limit' => 1,
18964 'moderation' => $results[0]['moderation'],
18965 ];
18966 $statuses = \Voxel\Timeline\Status::query( $args );
18967 $review_details = $statuses['items'][0];
18968 foreach ( (array) $review_details as $key => $value ) {
18969 $clean_key = preg_replace( '/^\0.*?\0/', '', $key );
18970 if ( 'user_can_edit' == $clean_key || 'publisher' == $clean_key || 'user_can_edit' == $clean_key || 'user_can_moderate' == $clean_key ) {
18971 continue;
18972 }
18973 if ( 'files' === $clean_key ) {
18974 $value = wp_json_encode( $value );
18975 } elseif ( 'details' === $clean_key ) {
18976 $review_ratings = isset( $value['rating'] ) && is_array( $value['rating'] ) ? $value['rating'] : [];
18977 $value['rating'] = [];
18978 $type = \Voxel\Post_Type::get( 'profile' );
18979
18980 if ( ! empty( $review_ratings ) ) {
18981 $rating_levels = $type->reviews->get_rating_levels();
18982 $categories = $type->reviews->get_categories();
18983
18984 foreach ( $categories as $category ) {
18985 $category_key = $category['key'];
18986 $category_label = strtolower( $category['label'] );
18987
18988 if ( isset( $review_ratings[ $category_key ] ) && $category_label ) {
18989 foreach ( $rating_levels as $rating_level ) {
18990 if ( $review_ratings[ $category_key ] === $rating_level['score'] ) {
18991 $value['rating'][ $category_label ] = $rating_level['label'];
18992 break;
18993 }
18994 }
18995 }
18996 }
18997 }
18998 } else {
18999 $clean_key = 'review_' . $clean_key;
19000 }
19001 $context['pluggable_data'][ $clean_key ] = $value;
19002 }
19003 $context['response_type'] = 'live';
19004 } else {
19005 $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
19006 }
19007 }
19008 return $context;
19009 }
19010
19011
19012 /**
19013 * Get Late Point Customer Fields
19014 *
19015 * @param array $data data.
19016 *
19017 * @return array|void
19018 */
19019 public function search_late_point_customer_fields( $data ) {
19020 if ( ! class_exists( 'OsCustomFieldsController' ) ) {
19021
19022 return;
19023 }
19024 global $wpdb;
19025 $booking_fields = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}latepoint_settings WHERE name = %s", 'custom_fields_for_customer' ) );
19026 $fields = [];
19027 if ( ! empty( $booking_fields ) ) {
19028 $fields = json_decode( $booking_fields->value, true );
19029 }
19030 return (array) $fields;
19031 }
19032
19033 /**
19034 * Is multi person booking
19035 *
19036 * @param array $data data.
19037 *
19038 * @return array|void
19039 */
19040 public function search_late_point_multi_person_booking( $data ) {
19041 if ( ! class_exists( 'LatePointAddonGroupBookings' ) ) {
19042
19043 return;
19044 }
19045 $service_id = isset( $data['dynamic'] ) ? $data['dynamic'] : '-1';
19046
19047 if ( -1 == $service_id ) {
19048 return;
19049 }
19050 global $wpdb;
19051 $booking_details = $wpdb->get_row( $wpdb->prepare( "SELECT capacity_max,capacity_min FROM {$wpdb->prefix}latepoint_services WHERE id= %s", intval( $service_id ) ) );
19052 if ( ! empty( $booking_details ) ) {
19053 return (array) $booking_details;
19054 }
19055
19056 }
19057
19058 /**
19059 * Get Mail Mint lists
19060 *
19061 * @param array $data data.
19062 *
19063 * @return array
19064 */
19065 public function search_mail_mint_contact_lists( $data ) {
19066 global $wpdb;
19067
19068 $lists = $wpdb->get_results( $wpdb->prepare( 'SELECT title, id FROM ' . $wpdb->prefix . 'mint_contact_groups WHERE type = %s', 'lists' ) );
19069 $options = [];
19070
19071 if ( ! empty( $lists ) ) {
19072 foreach ( $lists as $list ) {
19073 $options[] = [
19074 'label' => $list->title,
19075 'value' => $list->id,
19076 ];
19077 }
19078 }
19079 return [
19080 'options' => $options,
19081 'hasMore' => false,
19082 ];
19083 }
19084
19085 /**
19086 * Get Mail Mint tags
19087 *
19088 * @param array $data data.
19089 *
19090 * @return array
19091 */
19092 public function search_mail_mint_contact_tags( $data ) {
19093 global $wpdb;
19094
19095 $lists = $wpdb->get_results( $wpdb->prepare( 'SELECT title, id FROM ' . $wpdb->prefix . 'mint_contact_groups WHERE type = %s ', 'tags' ) );
19096 $options = [];
19097
19098 if ( ! empty( $lists ) ) {
19099 foreach ( $lists as $list ) {
19100 $options[] = [
19101 'label' => $list->title,
19102 'value' => $list->id,
19103 ];
19104 }
19105 }
19106 return [
19107 'options' => $options,
19108 'hasMore' => false,
19109 ];
19110 }
19111
19112 /** Get Better Messages trigger Last Data
19113 *
19114 * @param array $data data.
19115 *
19116 * @return array|mixed|string
19117 */
19118 public function search_better_messages_triggers_data( $data ) {
19119 $context = [];
19120 global $wpdb;
19121 if ( ! function_exists( 'Better_Messages' ) ) {
19122 return [];
19123 }
19124 $sql = "SELECT * FROM {$wpdb->prefix}bm_message_messages ORDER BY id DESC LIMIT 1";
19125 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
19126
19127 if ( ! empty( $results ) ) {
19128 $message = Better_Messages()->functions->get_message( $results[0]['id'] );
19129 if ( is_object( $message ) ) {
19130 $message = get_object_vars( $message );
19131 }
19132 $context['pluggable_data'] = $message;
19133 $context['pluggable_data']['sender'] = WordPress::get_user_context( $results[0]['sender_id'] );
19134 $context['response_type'] = 'live';
19135 } else {
19136 $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
19137 }
19138 return $context;
19139 }
19140
19141 /**
19142 * Get Appointment Hour Booking trigger Last Data
19143 *
19144 * @param array $data data.
19145 *
19146 * @return array|mixed|string
19147 */
19148 public function search_ahb_appointment_booked_triggers_last_data( $data ) {
19149 $context = [];
19150 global $wpdb;
19151 $sql = "SELECT * FROM {$wpdb->prefix}cpappbk_messages ORDER BY id DESC LIMIT 1";
19152 $results = $wpdb->get_results( $sql, ARRAY_A );// @phpcs:ignore
19153
19154 if ( ! empty( $results ) ) {
19155 $context['pluggable_data'] = unserialize( $results[0]['posted_data'] );
19156 $context['response_type'] = 'live';
19157 } else {
19158 $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
19159 }
19160 return $context;
19161 }
19162
19163 /**
19164 * Prepare mailmint contact status.
19165 *
19166 * @param array $data Search Params.
19167 *
19168 * @return array
19169 */
19170 public function search_mail_mint_fetch_custom_fields( $data ) {
19171
19172 $options = [
19173 [
19174 'label' => 'Yes',
19175 'value' => 'true',
19176 ],
19177 [
19178 'label' => 'No',
19179 'value' => 'false',
19180 ],
19181 ];
19182
19183 return [
19184 'options' => $options,
19185 'hasMore' => false,
19186 ];
19187 }
19188
19189 /**
19190 * Get Mail Mint custom fields
19191 *
19192 * @param array $data data.
19193 *
19194 * @return array
19195 */
19196 public function search_mail_mint_custom_fields( $data ) {
19197 global $wpdb;
19198 $fields = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'mint_custom_fields' );
19199
19200 return (array) $fields;
19201
19202 }
19203
19204 /**
19205 * Get Mail Mint lists
19206 *
19207 * @param array $data data.
19208 *
19209 * @return array|void
19210 */
19211 public function search_mail_mint_contacts( $data ) {
19212 if ( ! class_exists( 'Mint\MRM\DataBase\Models\ContactModel' ) ) {
19213 return [];
19214 }
19215 $contacts = ContactModel::get_all();
19216 $options = [];
19217 if ( ! empty( $contacts ) ) {
19218 foreach ( $contacts['data'] as $contact ) {
19219 $options[] = [
19220 'label' => $contact['email'],
19221 'value' => $contact['id'],
19222 ];
19223 }
19224 }
19225 return [
19226 'options' => $options,
19227 'hasMore' => false,
19228 ];
19229 }
19230
19231 /**
19232 * Get mail mint Last Data
19233 *
19234 * @param array $data data.
19235 *
19236 * @return array
19237 */
19238 public function search_mail_mint_last_data( $data ) {
19239 if ( ! class_exists( 'Mint\MRM\DataBase\Models\ContactModel' ) || ! class_exists( 'Mint\MRM\DataBase\Models\ContactGroupModel' ) ) {
19240 return [];
19241 }
19242 $context = [];
19243 global $wpdb;
19244 $contact_id = isset( $data['filter']['contact_id']['value'] ) ? $data['filter']['contact_id']['value'] : -1;
19245 $term = $data['search_term'] ? $data['search_term'] : '';
19246 $type = 'lists';
19247 if ( 'mail_mint_tags_added_to_contact' === $term ) {
19248 $type = 'tags';
19249 }
19250 $data = [];
19251 if ( -1 == $contact_id ) {
19252 $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 ) );
19253 } else {
19254 $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 ) );
19255 }
19256
19257 if ( ! empty( $result ) ) {
19258 $contact_id = $result->contact_id;
19259 $data['contact'] = ContactModel::get( $contact_id );
19260 $data[ $type ] = ContactGroupModel::get( $result->id );
19261 }
19262 if ( ! empty( $data ) ) {
19263 $context['pluggable_data'] = $data;
19264 $context['response_type'] = 'live';
19265 } else {
19266 $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 );
19267 }
19268 return (array) $context;
19269 }
19270
19271
19272 /**
19273 * Get Simple Schedule Appointment Types
19274 *
19275 * @param array $data data.
19276 *
19277 * @return array
19278 */
19279 public function search_simple_schedule_appointment_types( $data ) {
19280 global $wpdb;
19281
19282 $appointment_types = $wpdb->get_results( 'SELECT title, id FROM ' . $wpdb->prefix . 'ssa_appointment_types' );
19283 $options = [];
19284
19285 if ( ! empty( $appointment_types ) ) {
19286 foreach ( $appointment_types as $appointment_type ) {
19287 $options[] = [
19288 'label' => $appointment_type->title,
19289 'value' => $appointment_type->id,
19290 ];
19291 }
19292 }
19293 return [
19294 'options' => $options,
19295 'hasMore' => false,
19296 ];
19297 }
19298
19299 /**
19300 * Get mail mint Last Data
19301 *
19302 * @param array $data data.
19303 *
19304 * @return array
19305 */
19306 public function search_ssa_last_data( $data ) {
19307 $context = [];
19308 global $wpdb;
19309 $appointment_type_id = isset( $data['filter']['appointment_type_id']['value'] ) ? $data['filter']['appointment_type_id']['value'] : -1;
19310 $term = $data['search_term'] ? $data['search_term'] : '';
19311 $status = 'booked';
19312 if ( 'ssa_appointment_cancelled' === $term ) {
19313 $status = 'canceled';
19314 }
19315 $data = [];
19316 if ( -1 == $appointment_type_id ) {
19317 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointments WHERE status = %s ORDER BY id DESC Limit 1", $status ), ARRAY_A );
19318 } else {
19319 $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 );
19320 }
19321
19322
19323 if ( ! empty( $result ) ) {
19324 $result_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ssa_appointment_meta where appointment_id=%d", $result['id'] ) );
19325 if ( ! empty( $result_meta ) ) {
19326 foreach ( $result_meta as $meta ) {
19327 $result[ $meta->meta_key ] = $meta->meta_value;
19328 }
19329 }
19330 }
19331 if ( ! empty( $result ) ) {
19332
19333 $result['customer_information'] = json_decode( $result['customer_information'], true );
19334 $context['pluggable_data'] = $result;
19335 $context['response_type'] = 'live';
19336 } else {
19337 $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 );
19338 }
19339 return (array) $context;
19340 }
19341
19342 /**
19343 * Get Simple Schedule Appointment Types
19344 *
19345 * @param array $data data.
19346 *
19347 * @return array
19348 */
19349 public function search_simple_schedule_appointments( $data ) {
19350 global $wpdb;
19351
19352 $appointment_types = $wpdb->get_results( 'SELECT title, id FROM ' . $wpdb->prefix . 'ssa_appointment_types' );
19353 $options = [];
19354
19355 if ( ! empty( $appointment_types ) ) {
19356 foreach ( $appointment_types as $appointment_type ) {
19357 $options[] = [
19358 'label' => $appointment_type->title,
19359 'value' => $appointment_type->id,
19360 ];
19361 }
19362 }
19363 return [
19364 'options' => $options,
19365 'hasMore' => false,
19366 ];
19367 }
19368
19369 /**
19370 * Get Sensei LMS Lessons
19371 *
19372 * @param array $data data.
19373 *
19374 * @return array
19375 */
19376 public function search_sensei_lms_lessons( $data ) {
19377 if ( ! function_exists( 'Sensei' ) ) {
19378 return [];
19379 }
19380 $course_id = $data['dynamic'];
19381 $lessons = \Sensei()->course->course_lessons( $course_id, 'publish', 'ids' );
19382 $options = [];
19383
19384 if ( ! empty( $lessons ) ) {
19385 foreach ( $lessons as $lesson ) {
19386 $options[] = [
19387 'label' => get_the_title( $lesson ),
19388 'value' => $lesson,
19389 ];
19390 }
19391 }
19392 return [
19393 'options' => $options,
19394 'hasMore' => false,
19395 ];
19396 }
19397
19398 /**
19399 * Get Sensei LMS Quiz List
19400 *
19401 * @param array $data data.
19402 *
19403 * @return array
19404 */
19405 public function search_sensei_lms_quiz( $data ) {
19406 $page = $data['page'];
19407 $limit = Utilities::get_search_page_limit();
19408 $offset = $limit * ( $page - 1 );
19409
19410 $quizes = get_posts(
19411 [
19412 'post_type' => 'lesson',
19413 'orderby' => 'title',
19414 'order' => 'ASC',
19415 'post_status' => 'publish',
19416 'posts_per_page' => $limit,
19417 'offset' => $offset,
19418 'fields' => 'ids',
19419 'meta_query' => [
19420 'relation' => 'AND',
19421 [
19422 'key' => '_lesson_quiz',
19423 'compare' => 'EXISTS',
19424 ],
19425 [
19426 'key' => '_quiz_has_questions',
19427 'value' => 1,
19428 'compare' => '=',
19429 ],
19430 ],
19431 ]
19432 );
19433 $options = [];
19434 $total_quizes = get_posts(
19435 [
19436 'post_type' => 'lesson',
19437 'orderby' => 'title',
19438 'order' => 'ASC',
19439 'post_status' => 'publish',
19440 'posts_per_page' => -1,
19441 'fields' => 'ids',
19442 'meta_query' => [
19443 'relation' => 'AND',
19444 [
19445 'key' => '_lesson_quiz',
19446 'compare' => 'EXISTS',
19447 ],
19448 [
19449 'key' => '_quiz_has_questions',
19450 'value' => 1,
19451 'compare' => '=',
19452 ],
19453 ],
19454 ]
19455 );
19456 $count = count( $total_quizes );
19457 if ( ! empty( $quizes ) ) {
19458 foreach ( $quizes as $quiz ) {
19459 $options[] = [
19460 'label' => get_the_title( $quiz ),
19461 'value' => $quiz,
19462 ];
19463 }
19464 }
19465 return [
19466 'options' => $options,
19467 'hasMore' => $count > $limit && $count > $offset,
19468 ];
19469 }
19470
19471 /**
19472 * Get Sensei LMS trigger Last Data
19473 *
19474 * @param array $data data.
19475 *
19476 * @return array|mixed|string
19477 */
19478 public function search_sensei_lms_triggers_last_data( $data ) {
19479 $context = [];
19480 global $wpdb;
19481 $term = $data['search_term'];
19482
19483 if ( ! function_exists( 'Sensei' ) ) {
19484 return;
19485 }
19486
19487 if ( 'course_completed' == $term ) {
19488 $comment_type = 'sensei_course_status';
19489 $course_id = $data['filter']['sensei_course']['value'];
19490 if ( -1 === $course_id ) {
19491 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s ORDER BY comment_ID DESC LIMIT 1";
19492 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type ), ARRAY_A );// @phpcs:ignore
19493 } else {
19494 $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";
19495 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, $course_id ), ARRAY_A );// @phpcs:ignore
19496 }
19497 } elseif ( 'course_enrolled' == $term ) {
19498 $comment_type = 'sensei_course_status';
19499 $course_id = $data['filter']['sensei_course']['value'];
19500 if ( -1 === $course_id ) {
19501 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_approved = %s AND comment_type = %s ORDER BY comment_ID DESC LIMIT 1";
19502 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'in-progress', $comment_type ), ARRAY_A );// @phpcs:ignore
19503 } else {
19504 $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";
19505 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'in-progress', $comment_type, $course_id ), ARRAY_A );// @phpcs:ignore
19506 }
19507 } elseif ( 'lesson_completed' == $term ) {
19508 $lesson_id = $data['filter']['sensei_lesson']['value'];
19509 $course_id = $data['filter']['sensei_course']['value'];
19510 $comment_type = 'sensei_lesson_status';
19511 if ( -1 === $lesson_id ) {
19512 $sql = "SELECT comments.*, meta.* FROM {$wpdb->prefix}comments as comments
19513 LEFT JOIN {$wpdb->prefix}postmeta as meta
19514 ON comments.comment_post_ID = meta.post_id
19515 WHERE comments.comment_approved = %s
19516 AND comments.comment_type = %s
19517 AND meta.meta_key = %s AND
19518 meta.meta_value = %d
19519 ORDER BY comments.comment_ID DESC LIMIT 1";
19520 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, '_lesson_course', $course_id ), ARRAY_A );// @phpcs:ignore
19521 } else {
19522 $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";
19523 $results = $wpdb->get_results( $wpdb->prepare( $sql, 'complete', $comment_type, $lesson_id ), ARRAY_A );// @phpcs:ignore
19524 }
19525 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term ) {
19526 $quiz_id = $data['filter']['sensei_quiz']['value'];
19527 $comment_type = 'sensei_lesson_status';
19528 if ( 'quiz_passes' == $term ) {
19529 $status = "( comment_approved = 'passed' )";
19530 } elseif ( 'quiz_fails' == $term ) {
19531 $status = "( comment_approved = 'failed' )";
19532 } else {
19533 $status = "( comment_approved = 'failed' OR comment_approved = 'passed' )";
19534 }
19535 if ( -1 === $quiz_id ) {
19536 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type = %s AND $status ORDER BY comment_ID DESC LIMIT 1";
19537 $results = $wpdb->get_results( $wpdb->prepare( $sql, $comment_type ), ARRAY_A );// @phpcs:ignore
19538 } else {
19539 $sql = "SELECT * FROM {$wpdb->prefix}comments WHERE comment_type = %s AND $status AND comment_post_ID = %d ORDER BY comment_ID DESC LIMIT 1";
19540 $results = $wpdb->get_results( $wpdb->prepare( $sql, $comment_type, $quiz_id ), ARRAY_A );// @phpcs:ignore
19541 }
19542 } elseif ( 'quiz_completion' == $term ) {
19543 $quiz_id = $data['filter']['sensei_quiz']['value'];
19544 $comment_type = 'sensei_lesson_status';
19545 $condition_compare = $data['filter']['condition_compare']['value'];
19546 $percentage = $data['filter']['percentage']['value'];
19547 if ( -1 === $quiz_id ) {
19548 $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
19549 } else {
19550 $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
19551 }
19552 }
19553
19554 if ( ! empty( $results ) ) {
19555 $post = get_post( $results[0]['comment_post_ID'] );
19556 $context['pluggable_data'] = WordPress::get_user_context( $results[0]['user_id'] );
19557 if ( 'course_completed' == $term || 'course_enrolled' == $term ) {
19558 $context['pluggable_data']['sensei_course'] = $results[0]['comment_post_ID'];
19559 if ( $post instanceof \WP_Post ) {
19560 $context['pluggable_data']['course_title'] = $post->post_title;
19561 }
19562 } elseif ( 'lesson_completed' == $term ) {
19563 $context['pluggable_data']['sensei_lesson'] = $results[0]['comment_post_ID'];
19564 if ( $post instanceof \WP_Post ) {
19565 $context['pluggable_data']['lesson_title'] = $post->post_title;
19566 }
19567 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term || 'quiz_completion' == $term ) {
19568 $lesson_quiz = get_post_meta( $results[0]['comment_post_ID'], '_lesson_quiz', true );
19569 $submission = \Sensei()->quiz_submission_repository->get( $lesson_quiz, $results[0]['user_id'] );
19570 if ( $post instanceof \WP_Post ) {
19571 $context['pluggable_data']['quiz_title'] = $post->post_title;
19572 }
19573 $context['pluggable_data']['quiz_status'] = $results[0]['comment_approved'];
19574 $context['pluggable_data']['quiz_data']['id'] = $submission->get_id();
19575 $context['sensei_quiz'] = $submission->get_id();
19576 $context['pluggable_data']['quiz_data']['final_grade'] = $submission->get_final_grade();
19577 $context['pluggable_data']['quiz_data']['created_at'] = $submission->get_created_at();
19578 }
19579 $context['response_type'] = 'live';
19580 } else {
19581 if ( 'course_completed' == $term || 'course_enrolled' == $term ) {
19582 $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
19583 } elseif ( 'lesson_completed' == $term ) {
19584 $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
19585 } elseif ( 'quiz_attempted' == $term || 'quiz_passes' == $term || 'quiz_fails' == $term || 'quiz_completion' == $term ) {
19586 $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
19587 }
19588 }
19589 return $context;
19590 }
19591
19592 /**
19593 * Get SurelyWP Services - SureCart Addons Last Data
19594 *
19595 * @param array $data data.
19596 *
19597 * @return array
19598 */
19599 public function search_services_sc_triggers_last_data( $data ) {
19600 $context = [];
19601 global $wpdb;
19602 $term = $data['search_term'] ? $data['search_term'] : '';
19603 $data = [];
19604
19605 if ( 'new_service_created' === $term ) {
19606 $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 );
19607 } elseif ( 'requirement_submitted' === $term ) {
19608 $result = $wpdb->get_row( "SELECT created_at FROM {$wpdb->prefix}surelywp_sv_requirements ORDER BY created_at DESC Limit 1", ARRAY_A );
19609 } elseif ( 'message_sent' === $term ) {
19610 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_messages ORDER BY message_id DESC Limit 1", ARRAY_A );
19611 } elseif ( 'message_final_delivery_sent' === $term ) {
19612 $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 );
19613 } elseif ( 'customer_request_revision' === $term ) {
19614 $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 );
19615 } elseif ( 'customer_approves_final_delivery' === $term ) {
19616 $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 );
19617 } elseif ( 'delivery_date_changed' === $term ) {
19618 $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 );
19619 } elseif ( 'service_cancel' === $term || 'service_marked_canceled' === $term ) {
19620 $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 );
19621 } elseif ( 'service_completed' === $term || 'service_mark_completed' === $term ) {
19622 $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 );
19623 } elseif ( 'contract_signed' === $term ) {
19624 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sv_contracts ORDER BY contract_id DESC Limit 1", ARRAY_A );
19625 }
19626 if ( ! empty( $result ) ) {
19627 if ( 'new_service_created' === $term ) {
19628 $service_data = [
19629 'service_setting_id' => $result['service_id'],
19630 'order_id' => $result['order_id'],
19631 'product_id' => $result['product_id'],
19632 'service_status' => $result['service_status'],
19633 'delivery_date' => $result['delivery_date'],
19634 ];
19635 $context['pluggable_data'] = array_merge( $service_data, WordPress::get_user_context( $result['user_id'] ) );
19636 $context['response_type'] = 'live';
19637 } elseif ( 'requirement_submitted' === $term ) {
19638 $requirements_data = $wpdb->get_results(
19639 $wpdb->prepare(
19640 "SELECT * FROM {$wpdb->prefix}surelywp_sv_requirements WHERE created_at = %s",
19641 $result['created_at']
19642 ),
19643 ARRAY_A
19644 );
19645 $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 );
19646 $user_data = WordPress::get_user_context( $service_result['user_id'] );
19647 unset( $service_result['user_id'] );
19648 $context['pluggable_data'] = array_merge( $requirements_data, $service_result, $user_data );
19649 foreach ( $requirements_data as $value ) {
19650 if ( 'file' == $value['requirement_type'] ) {
19651 $upload_dir = wp_upload_dir();
19652 $attachment_file_names = json_decode( $value['requirement'], true );
19653 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
19654 $context['pluggable_data']['requirement_attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $value['service_id'] . '/requirement/' . $attachment_file_name;
19655 }
19656 }
19657 }
19658 $context['response_type'] = 'live';
19659 } elseif ( 'message_sent' === $term || 'message_final_delivery_sent' === $term ) {
19660 $message_data = [
19661 'sender' => WordPress::get_user_context( $result['sender_id'] ),
19662 'receiver' => WordPress::get_user_context( $result['receiver_id'] ),
19663 'service_id' => $result['service_id'],
19664 'message_text' => $result['message_text'],
19665 'attachment_file_name' => $result['attachment_file_name'],
19666 'is_final_delivery' => $result['is_final_delivery'],
19667 ];
19668 $context['pluggable_data'] = $message_data;
19669 $upload_dir = wp_upload_dir();
19670 $attachment_file_names = json_decode( $result['attachment_file_name'], true );
19671 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
19672 $context['pluggable_data']['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $result['service_id'] . '/messages/' . $attachment_file_name;
19673 }
19674 $context['response_type'] = 'live';
19675 } elseif ( 'customer_request_revision' === $term ) {
19676 $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 );
19677 if ( ! empty( $message_result ) ) {
19678 global $surelywp_sv_model;
19679 $revision_message = $surelywp_sv_model->surelywp_sv_get_customer_revision_msg( $message_result['service_id'], $message_result['message_id'] );
19680 $context['pluggable_data'] = array_merge( $result, $revision_message );
19681 $context['pluggable_data']['sender'] = WordPress::get_user_context( $revision_message['sender_id'] );
19682 $context['pluggable_data']['receiver'] = WordPress::get_user_context( $revision_message['receiver_id'] );
19683 $upload_dir = wp_upload_dir();
19684 $attachment_file_names = json_decode( $revision_message['attachment_file_name'], true );
19685 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
19686 $context['pluggable_data']['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-services-data/' . $revision_message['service_id'] . '/messages/' . $attachment_file_name;
19687 }
19688 $context['response_type'] = 'live';
19689 } else {
19690 $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 );
19691 }
19692 } elseif ( 'customer_approves_final_delivery' === $term || 'delivery_date_changed' === $term || 'service_cancel' === $term || 'service_marked_canceled' === $term || 'service_completed' === $term || 'service_mark_completed' === $term ) {
19693 $user_data = WordPress::get_user_context( $result['user_id'] );
19694 unset( $result['user_id'] );
19695 $context['pluggable_data'] = array_merge( $result, $user_data );
19696 $context['response_type'] = 'live';
19697 } elseif ( 'contract_signed' === $term ) {
19698 $contract_data = [
19699 'service_id' => $result['service_id'],
19700 'signature' => $result['signature'],
19701 'contract_details' => $result['contract_details'],
19702 ];
19703 $service_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sv_services WHERE service_id = %d", $result['service_id'] ), ARRAY_A );
19704 $user_data = WordPress::get_user_context( $service_result['user_id'] );
19705 unset( $service_result['user_id'] );
19706 $context['pluggable_data'] = array_merge( $contract_data, $user_data, $service_result );
19707 $context['response_type'] = 'live';
19708 }
19709 } else {
19710 if ( 'new_service_created' === $term || 'customer_request_revision' === $term || 'customer_approves_final_delivery' === $term || 'delivery_date_changed' === $term ) {
19711 $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 );
19712 } elseif ( 'requirement_submitted' === $term ) {
19713 $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 );
19714 } elseif ( 'message_sent' === $term || 'message_final_delivery_sent' === $term ) {
19715 $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 );
19716 } elseif ( 'service_cancel' === $term ) {
19717 $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 );
19718 } elseif ( 'service_marked_canceled' === $term || 'service_completed' === $term || 'service_mark_completed' === $term ) {
19719 $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 );
19720 } elseif ( 'contract_signed' === $term ) {
19721 $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 );
19722 }
19723 }
19724 return (array) $context;
19725 }
19726
19727 /**
19728 * Get SurelyWP Support Portal - SureCart Addons Last Data
19729 *
19730 * @param array $data data.
19731 *
19732 * @return array
19733 */
19734 public function search_sc_support_portal_triggers_last_data( $data ) {
19735 $context = [];
19736 if ( ! class_exists( 'Surelywp_Support_Portal' ) ) {
19737 return [];
19738 }
19739 global $wpdb;
19740 $term = $data['search_term'] ? $data['search_term'] : '';
19741 $data = [];
19742
19743 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
19744 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sp_support WHERE support_status = 1 ORDER BY support_id DESC Limit 1", ARRAY_A );
19745 } elseif ( 'new_message_sent' === $term ) {
19746 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}surelywp_sp_messages ORDER BY message_id DESC Limit 1", ARRAY_A );
19747 } elseif ( 'tickets_closed' === $term ) {
19748 $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 );
19749 } elseif ( 'tickets_opens' === $term ) {
19750 $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 );
19751 } elseif ( 'new_email_message_fetched' === $term ) {
19752 $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 );
19753 }
19754 if ( ! empty( $result ) ) {
19755 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
19756 $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 );
19757 $support_data = [
19758 'support_id' => $result['support_id'],
19759 'order_id' => $result['order_id'],
19760 'product_id' => $result['product_id'],
19761 'support_title' => $result['support_title'],
19762 'support_status' => Surelywp_Support_Portal::surelywp_sp_get_support_status( $result['support_status'] ),
19763 'support_data' => $support_res,
19764 ];
19765 $context['pluggable_data'] = array_merge( $support_data, WordPress::get_user_context( $result['user_id'] ) );
19766 $context['response_type'] = 'live';
19767 } elseif ( 'tickets_closed' === $term || 'tickets_opens' === $term ) {
19768 $support_data = [];
19769 $support_ids = $result;
19770 foreach ( $support_ids as $key => $id ) {
19771 $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}surelywp_sp_support WHERE support_id = %d", $id ), ARRAY_A );
19772 $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 );
19773 $support_data[ $key ] = WordPress::get_user_context( $result['user_id'] );
19774 $support_data[ $key ] = [
19775 'support_id' => $result['support_id'],
19776 'order_id' => $result['order_id'],
19777 'product_id' => $result['product_id'],
19778 'support_title' => $result['support_title'],
19779 'support_status' => Surelywp_Support_Portal::surelywp_sp_get_support_status( $result['support_status'] ),
19780 'support_data' => $support_res,
19781 ];
19782 }
19783 $context['pluggable_data'] = $support_data;
19784 $context['response_type'] = 'live';
19785 } elseif ( 'new_message_sent' === $term ) {
19786 $upload_dir = wp_upload_dir();
19787 $support_data = [
19788 'sender_id' => $result['sender_id'],
19789 'receiver_id' => $result['receiver_id'],
19790 'support_id' => $result['support_id'],
19791 'message_text' => $result['message_text'],
19792 'sender' => WordPress::get_user_context( $result['sender_id'] ),
19793 'receiver' => WordPress::get_user_context( $result['receiver_id'] ),
19794 ];
19795 if ( ! empty( $result['attachment_file_name'] ) ) {
19796 $attachment_file_names = json_decode( $result['attachment_file_name'], true );
19797 foreach ( (array) $attachment_file_names as $attachment_file_name ) {
19798 $support_data['attachment_file'][] = $upload_dir['baseurl'] . '/surelywp-support-portal-data/' . $result['support_id'] . '/messages/' . $attachment_file_name;
19799 }
19800 }
19801 $context['pluggable_data'] = $support_data;
19802 $context['response_type'] = 'live';
19803 }
19804 } else {
19805 if ( 'new_ticket_created' === $term || 'ticket_status_changed' === $term ) {
19806 $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 );
19807 } elseif ( 'tickets_closed' === $term || 'tickets_opens' === $term ) {
19808 $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 );
19809 } elseif ( 'new_message_sent' === $term ) {
19810 $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 );
19811 }
19812 }
19813 return (array) $context;
19814 }
19815
19816 /**
19817 * Get Fluent Boards Last Data
19818 *
19819 * @param array $data data.
19820 *
19821 * @return array
19822 */
19823 public function search_fbs_triggers_last_data( $data ) {
19824 if ( ! class_exists( '\FluentBoards\App\Models\Stage' ) ) {
19825 return [];
19826 }
19827 global $wpdb;
19828
19829 $term = isset( $data['search_term'] ) ? $data['search_term'] : '';
19830 if ( ! class_exists( 'FluentBoards\App\Models\Board' ) || ! class_exists( 'FluentBoards\App\Models\User' ) ) {
19831 return [];
19832 }
19833
19834 $context = [];
19835 $result = null;
19836
19837 switch ( $term ) {
19838 case 'board_created':
19839 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_boards ORDER BY id DESC LIMIT 1", ARRAY_A );
19840 break;
19841 case 'board_member_added':
19842 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_relations WHERE object_type = 'board_user' ORDER BY id DESC LIMIT 1", ARRAY_A );
19843 break;
19844 case 'task_created':
19845 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fbs_tasks ORDER BY id DESC LIMIT 1", ARRAY_A );
19846 break;
19847 }
19848
19849 if ( ! empty( $result ) ) {
19850 switch ( $term ) {
19851 case 'board_created':
19852 $context['pluggable_data'] = \FluentBoards\App\Models\Board::findOrFail( $result['id'] );
19853 $context['response_type'] = 'live';
19854 break;
19855 case 'board_member_added':
19856 $context['pluggable_data']['board_id'] = $result['object_id'];
19857 $context['pluggable_data']['board_member'] = \FluentBoards\App\Models\User::find( $result['foreign_id'] );
19858 $context['response_type'] = 'live';
19859 break;
19860 case 'task_created':
19861 $stage_data = Stage::where( 'board_id', $result['board_id'] )->whereNull( 'archived_at' )->first();
19862
19863 $context['pluggable_data'] = [
19864 'id' => $result['id'],
19865 'slug' => $result['slug'],
19866 'title' => $result['title'],
19867 'description' => $result['description'],
19868 'type' => $result['type'],
19869 'board_id' => $result['board_id'],
19870 'stage_id' => $result['stage_id'],
19871 'position' => $result['position'],
19872 'priority' => $result['priority'],
19873 'created_at' => $result['created_at'],
19874 'created_by' => $result['created_by'],
19875 'updated_at' => $result['updated_at'],
19876 'settings' => unserialize( $result['settings'] ),
19877 'stage' => $stage_data ? [
19878 'id' => $stage_data->id,
19879 'slug' => $stage_data->slug,
19880 'title' => $stage_data->title,
19881 'type' => $stage_data->type,
19882 'board_id' => $stage_data->board_id,
19883 'position' => $stage_data->position,
19884 'settings' => $stage_data->settings,
19885 'created_at' => '',
19886 'updated_at' => '',
19887 ] : null,
19888 ];
19889 $context['response_type'] = 'live';
19890 break;
19891 }
19892 } else {
19893 switch ( $term ) {
19894 case 'board_created':
19895 $context = json_decode( '{"pluggable_data":{"id":2,"title":"testing","description":"This is a sample board.","type":"to-do","currency":"USD","background":{"color":"#d1d8e0","id":"solid_10"},"created_by":"1","isUserOnlyViewer":0},"response_type":"sample"}', true );
19896 break;
19897 case 'board_member_added':
19898 $context = json_decode( '{"pluggable_data":{"board_id":"2","board_member":{"ID":1,"user_login":"johnd","display_name":"johnd"}},"response_type":"sample"}', true );
19899 break;
19900 case 'task_created':
19901 $context = json_decode( '{"pluggable_data":{"id":"1001","slug":"sample-task","title":"Sample Task","description":"This is a sample task.","type":"task","board_id":"10","stage_id":"3","position":"1","priority":"medium","created_at":"2024-03-20 12:00:00","created_by":"1","updated_at":"2024-03-20 12:30:00","settings":[],"stage":{"id":"3","slug":"sample-stage","title":"Sample Stage","type":"default","board_id":"10","position":"1","settings":[],"created_at":"2024-03-19 10:00:00","updated_at":"2024-03-20 11:00:00"}},"response_type":"sample"}', true );
19902 break;
19903
19904 }
19905 }
19906
19907 return (array) $context;
19908 }
19909
19910
19911 /**
19912 * Prepare FluentBoards Boards List.
19913 *
19914 * @param array $data Search Params.
19915 * @return array
19916 */
19917 public function search_fbs_boards_list( $data ) {
19918 global $wpdb;
19919
19920 $boards = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fbs_boards", ARRAY_A );
19921
19922 $options = [];
19923
19924 if ( ! empty( $boards ) ) {
19925 foreach ( $boards as $board ) {
19926 $options[] = [
19927 'label' => $board['title'],
19928 'value' => $board['id'],
19929 ];
19930 }
19931 }
19932
19933 return [
19934 'options' => $options,
19935 'hasMore' => false,
19936 ];
19937 }
19938
19939 /**
19940 * Prepare FluentBoards Stages List.
19941 *
19942 * @param array $data Search Params.
19943 * @return array
19944 */
19945 public function search_fbs_stages_list( $data ) {
19946 if ( ! class_exists( '\FluentBoards\App\Models\Stage' ) ) {
19947 return [];
19948 }
19949 $query = Stage::where( 'board_id', $data['dynamic'] )->whereNull( 'archived_at' );
19950 $stages = $query->get();
19951 $options = [];
19952
19953 if ( ! empty( $stages ) ) {
19954 foreach ( $stages as $stage ) {
19955 $options[] = [
19956 'label' => $stage['title'],
19957 'value' => $stage['id'],
19958 ];
19959 }
19960 }
19961
19962 return [
19963 'options' => $options,
19964 'hasMore' => false,
19965 ];
19966 }
19967
19968 /**
19969 * Get Profile Grid Last Data
19970 *
19971 * @param array $data data.
19972 *
19973 * @return array
19974 */
19975 public function search_profile_grid_triggers_last_data( $data ) {
19976 $context = [];
19977 global $wpdb;
19978 $term = $data['search_term'] ? $data['search_term'] : '';
19979
19980 if ( 'user_added_group' === $term || 'user_removed_from_group' === $term || 'user_assigned_group_manager' === $term || 'user_unassigned_group_manager' === $term || 'membership_request_approved' === $term ) {
19981 $users = get_users(
19982 [
19983 'orderby' => 'meta_value',
19984 'meta_key' => 'pm_group',
19985 'order' => 'DESC',
19986 'number' => 1,
19987 'meta_query' => [
19988 [
19989 'key' => 'pm_group',
19990 'compare' => 'EXISTS',
19991 ],
19992 ],
19993 'fields' => 'ids',
19994 ]
19995 );
19996 } elseif ( 'payment_complete' === $term || 'payment_failed' === $term ) {
19997 $users = get_users(
19998 [
19999 'orderby' => 'meta_value',
20000 'meta_key' => 'pm_group_payment_status',
20001 'order' => 'DESC',
20002 'number' => 1,
20003 'meta_query' => [
20004 [
20005 'key' => 'pm_group_payment_status',
20006 'compare' => 'EXISTS',
20007 ],
20008 ],
20009 'fields' => 'ids',
20010 ]
20011 );
20012 } elseif ( 'membership_request' === $term ) {
20013 $results = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}promag_group_requests WHERE status = 1 ORDER BY id DESC Limit 1", ARRAY_A );
20014 }
20015 if ( ! empty( $users ) ) {
20016 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 ) {
20017 $context = WordPress::get_user_context( $users[0] );
20018 $user_groups = get_user_meta( $users[0], 'pm_group', true );
20019 if ( is_array( $user_groups ) ) {
20020 $last_element = end( $user_groups );
20021 $context['group_id'] = $last_element;
20022 $context = array_merge( $context, ProfileGrid::pg_group_details( $last_element ) );
20023 }
20024 $context['pluggable_data'] = $context;
20025 $context['response_type'] = 'live';
20026 }
20027 } elseif ( ! empty( $results ) && 'membership_request' === $term ) {
20028 $context = WordPress::get_user_context( $results[0]['uid'] );
20029 $context['group_id'] = $results[0]['gid'];
20030 $context = array_merge( $context, ProfileGrid::pg_group_details( $results[0]['gid'] ) );
20031 $context['pluggable_data'] = $context;
20032 $context['response_type'] = 'live';
20033 } elseif ( 'group_manager_resets_password' === $term ) {
20034 $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 );
20035 } elseif ( 'group_deleted' === $term ) {
20036 $context = json_decode( '{"pluggable_data":{"group_name":"Test Group","group_description":"Testing Group","group_id":"2"},"response_type":"sample"}', true );
20037 } else {
20038 $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 );
20039 }
20040 return (array) $context;
20041 }
20042
20043 /**
20044 * Get Fluent SMTP Last Data
20045 *
20046 * @param array $data data.
20047 *
20048 * @return array
20049 */
20050 public function search_fluent_smtp_last_data( $data ) {
20051 $context = [];
20052 global $wpdb;
20053 $results = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fsmpt_email_logs WHERE status = 'failed' ORDER BY id DESC Limit 1", ARRAY_A );
20054 if ( ! empty( $results ) ) {
20055 $context['to'] = unserialize( $results['to'] );
20056 $context['from'] = $results['from'];
20057 $context['subject'] = $results['subject'];
20058 $context['body'] = $results['body'];
20059 $context['attachments'] = unserialize( $results['attachments'] );
20060 $context['status'] = $results['status'];
20061 $context['response'] = unserialize( $results['response'] );
20062 $context['headers'] = unserialize( $results['headers'] );
20063 $context['extra'] = unserialize( $results['extra'] );
20064 $context['pluggable_data'] = $context;
20065 $context['response_type'] = 'live';
20066 } else {
20067 $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 );
20068 }
20069 return (array) $context;
20070 }
20071
20072 /**
20073 * Get Fluent Community Last Data
20074 *
20075 * @param array $data data.
20076 *
20077 * @return array
20078 */
20079 public function search_fc_triggers_last_data( $data ) {
20080 $context = [];
20081 $space_result = [];
20082 $profile_result = [];
20083 $user_result = [];
20084 $feed_data = [];
20085 $course_result = [];
20086 global $wpdb;
20087 $term = $data['search_term'] ? $data['search_term'] : '';
20088
20089 $data = [];
20090
20091 if ( 'space_created' === $term || 'user_leaves_space' === $term || 'user_requests_join_space' === $term || 'feed_created' === $term || 'feed_updated' === $term || 'space_feed_created' === $term || 'user_joins_space' === $term ) {
20092 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_spaces ORDER BY id DESC LIMIT 1", ARRAY_A );
20093 $space_result = [
20094 'id' => $result['id'],
20095 'slug' => $result['slug'],
20096 'title' => $result['title'],
20097 'description' => $result['description'],
20098 'type' => $result['type'],
20099 'privacy' => $result['privacy'],
20100 'status' => $result['status'],
20101 'serial' => $result['serial'],
20102 'created_at' => $result['created_at'],
20103 'created_by' => $result['created_by'],
20104 'updated_at' => $result['updated_at'],
20105 'settings' => unserialize( $result['settings'] ),
20106 ];
20107 }
20108
20109 if ( 'feed_created' === $term || 'feed_updated' === $term || 'space_feed_created' === $term ) {
20110 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_posts ORDER BY id DESC LIMIT 1", ARRAY_A );
20111 $profile_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_xprofile ORDER BY id DESC LIMIT 1", ARRAY_A );
20112 $user_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}users ORDER BY id DESC LIMIT 1", ARRAY_A );
20113 $feed_data = [
20114 'feed' => [
20115 'id' => $result['id'],
20116 'user_id' => $result['user_id'],
20117 'parentid' => $result['parent_id'],
20118 'title' => $result['title'],
20119 'slug' => $result['slug'],
20120 'message' => $result['message'],
20121 'message_rendered' => $result['message_rendered'],
20122 'type' => $result['type'],
20123 'content_type' => $result['content_type'],
20124 'space_id' => $result['space_id'],
20125 'privacy' => $result['privacy'],
20126 'status' => $result['status'],
20127 'featured_image' => $result['featured_image'],
20128 'meta' => unserialize( $result['meta'] ),
20129 'issticky' => $result['issticky'],
20130 'comments_count' => $result['comments_count'],
20131 'reactions_count' => $result['reactions_count'],
20132 'priority' => $result['priority'],
20133 'expiredat' => $result['expired_at'],
20134 'scheduledat' => $result['scheduled_at'],
20135 'created_at' => $result['created_at'],
20136 'updated_at' => $result['updated_at'],
20137 'space' => $space_result,
20138 'xprofile' => [
20139 'id' => $profile_result['id'],
20140 'user_id' => $profile_result['user_id'],
20141 'total_points' => $profile_result['total_points'],
20142 'username' => $profile_result['username'],
20143 'status' => $profile_result['status'],
20144 'is_verified' => $profile_result['is_verified'],
20145 'display_name' => $profile_result['display_name'],
20146 'avatar' => $profile_result['avatar'],
20147 'short_description' => $profile_result['short_description'],
20148 'last_activity' => $profile_result['last_activity'],
20149 'meta' => unserialize( $profile_result['meta'] ),
20150 'created_at' => $profile_result['created_at'],
20151 'updated_at' => $profile_result['updated_at'],
20152 ],
20153 ],
20154 ];
20155 }
20156
20157 if ( 'user_joins_space' === $term ) {
20158 $user_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}users ORDER BY id DESC LIMIT 1", ARRAY_A );
20159 }
20160
20161 if ( 'course_created' === $term || 'course_published' === $term || 'course_updated' === $term || 'user_enrolls_course' === $term || 'user_unenrolls_course' === $term || 'user_completes_course' === $term ) {
20162 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_spaces WHERE type = 'course' ORDER BY id DESC LIMIT 1", ARRAY_A );
20163 $course_result = [
20164 'id' => $result['id'],
20165 'slug' => $result['slug'],
20166 'title' => $result['title'],
20167 'serial' => $result['serial'],
20168 'status' => $result['status'],
20169 'privacy' => $result['privacy'],
20170 'created_at' => $result['created_at'],
20171 'created_by' => $result['created_by'],
20172 'updated_at' => $result['updated_at'],
20173 'description' => $result['description'],
20174 'settings' => unserialize( $result['settings'] ),
20175 ];
20176 }
20177
20178 if ( 'course_deleted' === $term ) {
20179 $result = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}fcom_spaces WHERE type = 'course' ORDER BY id DESC LIMIT 1" );
20180 }
20181
20182 if ( 'new_comment_added' === $term || 'comment_updated' === $term ) {
20183 $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_post_comments ORDER BY id DESC LIMIT 1", ARRAY_A );
20184 $profile_result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}fcom_xprofile ORDER BY id DESC Limit 1", ARRAY_A );
20185 }
20186
20187 if ( 'comment_deleted' === $term ) {
20188 $result = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}fcom_post_comments ORDER BY id DESC LIMIT 1" );
20189 }
20190
20191 if ( ! empty( $result ) ) {
20192 if ( 'space_created' === $term ) {
20193 $context['pluggable_data'] = [ 'space' => $space_result ];
20194 $context['response_type'] = 'live';
20195 }
20196
20197 if ( 'feed_created' === $term || 'feed_updated' === $term ) {
20198 $context['pluggable_data'] = $feed_data;
20199 $context['response_type'] = 'live';
20200 }
20201
20202 if ( 'space_feed_created' === $term ) {
20203 $feed_data['feed']['user'] = [
20204 'user_url' => $user_result['user_url'],
20205 'user_email' => $user_result['user_email'],
20206 'user_login' => $user_result['user_login'],
20207 'user_status' => $user_result['user_status'],
20208 'display_name' => $user_result['display_name'],
20209 'user_nicename' => $user_result['user_nicename'],
20210 'user_registered' => $user_result['user_registered'],
20211 ];
20212 $context['pluggable_data'] = $feed_data;
20213 $context['response_type'] = 'live';
20214 }
20215
20216 if ( 'user_joins_space' === $term ) {
20217 $context['pluggable_data'] = [
20218 'space' => [
20219 'id' => $result['id'],
20220 'slug' => $result['slug'],
20221 'title' => $result['title'],
20222 'description' => $result['description'],
20223 'type' => $result['type'],
20224 'privacy' => $result['privacy'],
20225 'status' => $result['status'],
20226 'serial' => $result['serial'],
20227 'created_at' => $result['created_at'],
20228 'created_by' => $result['created_by'],
20229 'updated_at' => $result['updated_at'],
20230 'settings' => unserialize( $result['settings'] ),
20231 'membership' => [
20232 'ID' => $user_result['ID'],
20233 'photo' => '',
20234 'pivot_role' => 'admin',
20235 'pivot_status' => 'active',
20236 'pivot_created_at' => $result['created_at'],
20237 'pivot_user_id' => $user_result['ID'],
20238 'pivot_space_id' => $result['id'],
20239 'user_url' => $user_result['user_url'],
20240 'user_email' => $user_result['user_email'],
20241 'user_login' => $user_result['user_login'],
20242 'user_status' => $user_result['user_status'],
20243 'display_name' => $user_result['display_name'],
20244 'user_nicename' => $user_result['user_nicename'],
20245 'user_registered' => $user_result['user_registered'],
20246 ],
20247 ],
20248 'userId' => 1,
20249 'by' => 'self',
20250 ];
20251 $context['response_type'] = 'live';
20252 }
20253 if ( 'user_leaves_space' === $term ) {
20254 $context['pluggable_data'] = [
20255 'space' => $space_result,
20256 'user' => WordPress::get_sample_user_context(),
20257 'userId' => 1,
20258 'by' => 'self',
20259 ];
20260 $context['response_type'] = 'live';
20261 }
20262 if ( 'course_created' === $term || 'course_updated' === $term || 'course_published' === $term ) {
20263 $context['pluggable_data'] = [
20264 'course' => $course_result,
20265 ];
20266 $context['response_type'] = 'live';
20267 }
20268
20269 if ( 'course_deleted' === $term ) {
20270 $context['pluggable_data'] = [ 'course' => $result ];
20271 $context['response_type'] = 'live';
20272 }
20273
20274 if ( 'comment_deleted' === $term ) {
20275 $context['pluggable_data'] = [ 'comment_id' => $result ];
20276 $context['response_type'] = 'live';
20277 }
20278 if ( 'new_comment_added' === $term || 'comment_updated' === $term ) {
20279 $context['pluggable_data'] = [
20280 'comment' => [
20281 'id' => $result['id'],
20282 'user_id' => $result['user_id'],
20283 'post_id' => $result['post_id'],
20284 'parent_id' => $result['parent_id'],
20285 'reactions_count' => $result['reactions_count'],
20286 'message' => $result['message'],
20287 'message_rendered' => $result['message_rendered'],
20288 'meta' => unserialize( $result['meta'] ),
20289 'type' => $result['type'],
20290 'content_type' => $result['content_type'],
20291 'status' => $result['status'],
20292 'is_sticky' => $result['is_sticky'],
20293 'created_at' => $result['created_at'],
20294 'updated_at' => $result['updated_at'],
20295 'xprofile' => [
20296 'id' => $profile_result['id'],
20297 'user_id' => $profile_result['user_id'],
20298 'total_points' => $profile_result['total_points'],
20299 'username' => $profile_result['username'],
20300 'status' => $profile_result['status'],
20301 'is_verified' => $profile_result['is_verified'],
20302 'display_name' => $profile_result['display_name'],
20303 'avatar' => $profile_result['avatar'],
20304 'short_description' => $profile_result['short_description'],
20305 'last_activity' => $profile_result['last_activity'],
20306 'meta' => unserialize( $profile_result['meta'] ),
20307 'created_at' => $profile_result['created_at'],
20308 'updated_at' => $profile_result['updated_at'],
20309 ],
20310 ],
20311 ];
20312 $context['response_type'] = 'live';
20313 }
20314 if ( 'user_enrolls_course' === $term || 'user_unenrolls_course' === $term || 'user_completes_course' === $term ) {
20315 $context['pluggable_data'] = [
20316 'course' => $course_result,
20317 'user' => WordPress::get_sample_user_context(),
20318 'userId' => 1,
20319 ];
20320 $context['response_type'] = 'live';
20321 }
20322 if ( 'user_requests_join_space' === $term ) {
20323 $context['pluggable_data'] = [
20324 'space' => $space_result,
20325 'user' => WordPress::get_sample_user_context(),
20326 'userId' => 1,
20327 ];
20328 $context['response_type'] = 'live';
20329 }
20330 } else {
20331 $sample_space_data = [
20332 'id' => 101,
20333 'slug' => 'sample-space',
20334 'title' => 'Sample Space',
20335 'description' => 'This is a sample space description.',
20336 'type' => 'community',
20337 'privacy' => 'public',
20338 'status' => 'active',
20339 'serial' => 'SP101',
20340 'created_at' => '2025-01-01 10:00:00',
20341 'created_by' => 1,
20342 'updated_at' => '2025-01-05 12:00:00',
20343 'settings' => [
20344 'coursetype' => 'self_paced',
20345 'emoji' => '😍',
20346 'shapesvg' => null,
20347 'disablecomments' => 'no',
20348 'hidememberscount' => 'no',
20349 ],
20350 ];
20351 $sample_profile_data = [
20352 'id' => 1,
20353 'user_id' => 1,
20354 'total_points' => 100,
20355 'username' => 'sampleuser',
20356 'status' => 'active',
20357 'is_verified' => true,
20358 'display_name' => 'Sample User',
20359 'avatar' => 'https://example.com/avatar.jpg',
20360 'short_description' => 'This is a sample user.',
20361 'last_activity' => '2025-01-08 15:00:00',
20362 'meta' => [
20363 'website' => 'https://example.com',
20364 'coverphoto' => 'https://example.com/coverphoto.jpg',
20365 ],
20366 'created_at' => '2025-01-01 10:00:00',
20367 'updated_at' => '2025-01-05 12:00:00',
20368 ];
20369
20370 if ( 'feed_created' === $term || 'feed_updated' === $term || 'space_feed_created' === $term ) {
20371 $context['pluggable_data'] = [
20372 'feed' => [
20373 'id' => 1,
20374 'userid' => 1,
20375 'parentid' => null,
20376 'title' => 'Sample Feed',
20377 'slug' => 'sample-feed',
20378 'message' => 'This is a sample message for the feed.',
20379 'messagerendered' => '<p>This is a sample message for the feed.</p>',
20380 'type' => 'post',
20381 'contenttype' => 'text',
20382 'space_id' => 101,
20383 'privacy' => 'public',
20384 'status' => 'published',
20385 'featuredimage' => 'https://example.com/image.jpg',
20386 'meta' => [
20387 'previewdata' => 0,
20388 ],
20389 'issticky' => false,
20390 'commentscount' => 10,
20391 'reactionscount' => 25,
20392 'priority' => 'normal',
20393 'expiredat' => null,
20394 'scheduledat' => '2025-01-10 08:00:00',
20395 'createdat' => '2025-01-09 09:00:00',
20396 'updatedat' => '2025-01-09 10:00:00',
20397 'space' => $sample_space_data,
20398 'xprofile' => $sample_profile_data,
20399 ],
20400 ];
20401 $context['response_type'] = 'sample';
20402 }
20403 if ( 'space_feed_created' === $term ) {
20404 $conext['pluggable_data']['feed']['user'] = [
20405 'user_url' => 'https://example.com/user/profile',
20406 'user_email' => 'user@example.com',
20407 'user_login' => 'sampleuser',
20408 'user_status' => 'active',
20409 'display_name' => 'Sample User',
20410 'user_nicename' => 'sample-user',
20411 'user_registered' => '2024-01-01 00:00:00',
20412 ];
20413 $context['pluggable_data']['feed']['space'] = $sample_space_data;
20414 $context['pluggable_data']['feed']['xprofile'] = $sample_profile_data;
20415 $context['response_type'] = 'sample';
20416 }
20417 if ( 'user_joins_space' === $term ) {
20418 $context['pluggable_data'] = [
20419 'space' => [
20420 $sample_space_data,
20421 'membership' => [
20422 'ID' => 25,
20423 'photo' => 'https://example.com/user-photo.jpg',
20424 'pivot_role' => 'admin',
20425 'pivot_status' => 'active',
20426 'pivot_created_at' => '2025-01-24 09:00:00',
20427 'pivot_user_id' => 25,
20428 'pivot_space_id' => 100,
20429 'user_url' => 'https://example.com/user-profile',
20430 'user_email' => 'user@example.com',
20431 'user_login' => 'sampleuser',
20432 'user_status' => 'active',
20433 'display_name' => 'Sample User',
20434 'user_nicename' => 'sample-user',
20435 'user_registered' => '2024-01-01 00:00:00',
20436 ],
20437 ],
20438 'userId' => 1,
20439 'by' => 'self',
20440 ];
20441 $context['response_type'] = 'sample';
20442 }
20443
20444 if ( 'user_leaves_space' === $term ) {
20445 $context['pluggable_data'] = [
20446 'space' => $sample_space_data,
20447 'user' => WordPress::get_sample_user_context(),
20448 'userId' => 35,
20449 'by' => 'self',
20450 ];
20451 $context['response_type'] = 'sample';
20452 }
20453
20454 if ( 'new_comment_added' === $term || 'comment_updated' === $term ) {
20455 $context['pluggable_data'] = [
20456 'comment' => [
20457 'id' => 301,
20458 'user_id' => 45,
20459 'post_id' => 101,
20460 'parent_id' => 0,
20461 'reactions_count' => 5,
20462 'message' => 'This is a sample comment message.',
20463 'message_rendered' => '<p>This is a sample comment message.</p>',
20464 'meta' => [
20465 'likes' => 3,
20466 'shares' => 2,
20467 ],
20468 'type' => 'text',
20469 'content_type' => 'post',
20470 'status' => 'approved',
20471 'is_sticky' => false,
20472 'created_at' => '2025-01-23 10:30:00',
20473 'updated_at' => '2025-01-23 12:00:00',
20474 'xprofile' => $sample_profile_data,
20475 ],
20476 ];
20477 $context['response_type'] = 'sample';
20478 }
20479
20480 if ( 'course_deleted' === $term ) {
20481 $context['pluggable_data'] = [ 'course' => 2 ];
20482 $context['response_type'] = 'sample';
20483 }
20484 if ( 'comment_deleted' === $term ) {
20485 $context['pluggable_data'] = [ 'comment_id' => 1 ];
20486 $context['response_type'] = 'sample';
20487 }
20488 if ( 'user_enrolls_course' === $term || 'user_unenrolls_course' === $term || 'user_completes_course' === $term ) {
20489 $context['pluggable_data'] = [
20490 'course' => $sample_space_data,
20491 'user' => WordPress::get_sample_user_context(),
20492 'userId' => 1,
20493 ];
20494 $context['response_type'] = 'sample';
20495 }
20496 if ( 'course_created' === $term || 'course_updated' === $term || 'course_published' === $term ) {
20497 $context['pluggable_data'] = [
20498 'course' => $sample_space_data,
20499 ];
20500 $context['response_type'] = 'sample';
20501 }
20502 if ( 'user_requests_join_space' === $term ) {
20503 $context['pluggable_data'] = [
20504 'space' => $sample_space_data,
20505 'user' => WordPress::get_sample_user_context(),
20506 'userId' => 1,
20507 ];
20508 $context['response_type'] = 'sample';
20509 }
20510 }
20511 return (array) $context;
20512 }
20513
20514 /**
20515 * Prepare FluentCommunity Courses List.
20516 *
20517 * @param array $data Search Params.
20518 * @return array
20519 */
20520 public function search_fc_courses_list( $data ) {
20521 global $wpdb;
20522
20523 $courses = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fcom_spaces WHERE type = 'course' ", ARRAY_A );
20524
20525 $options = [];
20526
20527 if ( ! empty( $courses ) ) {
20528 foreach ( $courses as $course ) {
20529 $options[] = [
20530 'label' => $course['title'],
20531 'value' => $course['id'],
20532 ];
20533 }
20534 }
20535
20536 return [
20537 'options' => $options,
20538 'hasMore' => false,
20539 ];
20540 }
20541
20542 /**
20543 * Prepare FluentCommunity Spaces List.
20544 *
20545 * @param array $data Search Params.
20546 * @return array
20547 */
20548 public function search_fc_spaces_list( $data ) {
20549 global $wpdb;
20550
20551 $spaces = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fcom_spaces WHERE type NOT IN ('course', 'space_group')", ARRAY_A );
20552
20553 $options = [];
20554
20555 if ( ! empty( $spaces ) ) {
20556 foreach ( $spaces as $space ) {
20557 $options[] = [
20558 'label' => $space['title'],
20559 'value' => $space['id'],
20560 ];
20561 }
20562 }
20563
20564 return [
20565 'options' => $options,
20566 'hasMore' => false,
20567 ];
20568 }
20569
20570 /**
20571 * Prepare FluentCommunity Topics List.
20572 *
20573 * @param array $data Search Params.
20574 * @return array
20575 */
20576 public function search_fc_topics_list( $data ) {
20577 if ( ! class_exists( '\FluentCommunity\App\Functions\Utility' ) ) {
20578 return [];
20579 }
20580
20581 $space_topics = Utility::getTopicsBySpaceId( $data['dynamic'] );
20582
20583 $options = [];
20584
20585 if ( ! empty( $space_topics ) ) {
20586 foreach ( $space_topics as $topic ) {
20587 $options[] = [
20588 'label' => $topic['title'],
20589 'value' => $topic['id'],
20590 ];
20591 }
20592 }
20593
20594 return [
20595 'options' => $options,
20596 'hasMore' => false,
20597 ];
20598 }
20599
20600 }
20601
20602
20603 GlobalSearchController::get_instance();
20604