PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.22
OttoKit: All-in-One Automation Platform v1.1.22
1.1.38 1.1.37 1.1.36 1.1.35 1.1.34 1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 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 / Models / Utilities.php
suretriggers / src / Models Last commit date
Model.php 1 year ago SaasApiToken.php 1 year ago Utilities.php 11 months ago
Utilities.php
717 lines
1 <?php
2 /**
3 * Automation Modal Class.
4 * php version 5.6
5 *
6 * @category Model
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\Models;
15
16 use WP_User_Query;
17 use WPForms_Form_Handler;
18
19 /**
20 * Responsible for interacting with the database.
21 *
22 * Class Model
23 *
24 * @package SureTriggers\Models
25 *
26 * @psalm-suppress UndefinedClass Model
27 * @psalm-suppress PropertyNotSetInConstructor
28 */
29 class Utilities extends Model {
30
31
32 /**
33 * Find posts by title.
34 *
35 * @param array $data Search Data.
36 * @return array
37 */
38 public static function find_posts_by_title( $data ) {
39 /**
40 * Get object from base model
41 *
42 * @var Utilities $model Utilities Model.
43 */
44 $model = self::init();
45
46 $model->table = $model->db->posts;
47 $where = '1=1';
48
49 global $wpdb;
50
51 /**
52 * Get terms as string
53 *
54 * @var string $terms Terms string
55 */
56 $terms = esc_sql( $data['search_term'] );
57 $dynamic = esc_sql( $data['dynamic'] );
58 $post_type = esc_sql( isset( $data['filter']['post_type'] ) ? sanitize_text_field( $data['filter']['post_type'] ) : 'post' );
59 if ( 'post' === $post_type && ! empty( $dynamic['post_type'] ) ) {
60 $post_type = esc_sql( $dynamic['post_type'] );
61 }
62
63 $page = isset( $data['page'] ) ? absint( sanitize_text_field( $data['page'] ) ) : 1;
64 $limit = self::get_search_page_limit( $post_type );
65 $offset = $limit * ( $page - 1 );
66
67 if ( empty( $post_type ) || 'post_all' === $post_type ) {
68 $where .= " AND p.post_type not in ( 'revision', 'attachment' )";
69 } else {
70 $where .= " AND p.post_type='{$post_type}' AND p.post_status NOT IN ( 'draft', 'pending' ,'trash','auto-draft')";
71 }
72
73 $where .= ' AND p.post_title LIKE %s';
74
75 $sql_query = "SELECT p.ID, p.post_parent, p.post_type, p.post_title FROM {$model->table} p";
76
77 if ( in_array( $post_type, [ 'sfwd-lessons', 'sfwd-topic' ], true ) && ! empty( $dynamic ) && $dynamic > 0 ) {
78 $meta_key = 'sfwd-lessons' === $post_type ? 'course_id' : 'lesson_id';
79
80 $sql_query .= " LEFT JOIN $wpdb->postmeta pm ON pm.post_id = p.ID";
81 $where .= " AND pm.meta_key = '{$meta_key}' AND pm.meta_value = {$dynamic}";
82 }
83
84 $sql_query .= " WHERE {$where}";
85
86 $result = $model->db->get_results(
87 $model->db->prepare( $sql_query . " LIMIT {$offset}, {$limit}", "%%{$terms}%%" ),
88 ARRAY_A
89 );
90
91 $count_result = $model->db->get_results(
92 $model->db->prepare( $sql_query, "%%{$terms}%%" )
93 );
94 $count = is_array( $count_result ) ? count( $count_result ) : 0;
95
96 return [
97 'results' => $result,
98 'has_more' => $count > $limit && $count > $offset,
99 ];
100 }
101
102 /**
103 * Get search page limit.
104 *
105 * @param string $type search type.
106 * @return int
107 * @since 1.0.0
108 */
109 public static function get_search_page_limit( $type = 'options' ) {
110 return apply_filters( 'ap_search_' . $type . '_limit', 10 );
111 }
112
113 /**
114 * Get Divi forms.
115 *
116 * @return array|object|void|null
117 * @psalm-suppress MixedAssignment
118 */
119 public static function get_divi_forms() {
120 /**
121 * Get object from base model
122 *
123 * @var Utilities $model Utilities Model.
124 */
125 $model = self::init();
126
127 $model->table = $model->db->posts;
128
129 $sql_query = "SELECT `ID`, `post_content`, `post_title` FROM {$model->table} WHERE post_status = 'publish' AND post_type = 'page' AND post_content LIKE '%%et_pb_contact_form%%'";
130
131 return $model->db->get_results(
132 $model->db->prepare( $sql_query ),
133 ARRAY_A
134 );
135 }
136
137 /**
138 * Get UAG forms.
139 *
140 * @return array|object|void|null
141 * @psalm-suppress MixedAssignment
142 */
143 public static function get_uag_forms() {
144 /**
145 * Get object from base model
146 *
147 * @var Utilities $model Utilities Model.
148 */
149 $model = self::init();
150
151 $model->table = $model->db->posts;
152
153 $sql_query = "SELECT `ID`, `post_content`, `post_title` FROM {$model->table} WHERE post_status = 'publish' AND post_content LIKE '%%wp-block-uagb-forms%%'";
154
155 return $model->db->get_results(
156 $model->db->prepare( $sql_query ),
157 ARRAY_A
158 );
159 }
160
161 /**
162 * Get users data from DB.
163 *
164 * @param array $data Data Array.
165 * @param int $page Page Number.
166 * @return array
167 */
168 public static function get_users( $data, $page ) {
169 /**
170 * Get terms as string
171 *
172 * @var string $terms Terms String.
173 */
174 $terms = esc_sql( isset( $data['search_term'] ) ? $data['search_term'] : '' );
175 $limit = self::get_search_page_limit( 'users' );
176 $offset = $limit * ( $page - 1 );
177
178 $prepare_params = [
179 'fields' => [ 'ID', 'user_login' ],
180 'offset' => $offset,
181 'search_columns' => [ 'user_login', 'user_email' ],
182 'number' => $limit,
183 'search' => ! empty( $terms ) ? '*' . $terms . '*' : '',
184 ];
185
186 if ( is_array( $data['filter'] ) ) {
187 foreach ( $data['filter'] as $filter_name => $filter ) {
188 $prepare_params [ $filter_name ] = $filter;
189 }
190 }
191
192 $user_search = new WP_User_Query( $prepare_params );
193
194 $users = $user_search->get_results();
195 $count = $user_search->get_total();
196
197 return [
198 'results' => $users,
199 'has_more' => $count > $limit && $count > $offset,
200 ];
201 }
202
203 /**
204 * Get WPForms data.
205 *
206 * @param string $terms search string.
207 * @param int $page Page Number.
208 * @param int $form_id WPForm ID.
209 * @return array|void
210 * @since 1.0.0
211 */
212 public static function get_wpform_fields( $terms, $page, $form_id ) {
213 /**
214 * Get terms as string
215 *
216 * @var string $terms Terms string
217 */
218 $terms = esc_sql( $terms );
219 $limit = self::get_search_page_limit();
220 $offset = $limit * ( $page - 1 );
221 $results = [];
222
223 if ( ! class_exists( 'WPForms_Form_Handler' ) || ! function_exists( 'wpforms_decode' ) ) {
224 return;
225 }
226 $wpforms = new WPForms_Form_Handler();
227 $form = $wpforms->get( $form_id );
228 /**
229 * Ignore false positive
230 *
231 * @psalm-suppress UndefinedFunction
232 */
233 $form_content = wpforms_decode( $form->post_content );
234
235 $wpform_fields = $form_content['fields'];
236 $count = count( $form_content['fields'] );
237
238 /**
239 * Ignore false positive
240 *
241 * @psalm-suppress TooManyArguments
242 */
243 $exclude_fields = apply_filters(
244 'sure_trigger_wpforms_exclude_fields',
245 [
246 'pagebreak',
247 'file-upload',
248 'password',
249 'divider',
250 'entry-preview',
251 'html',
252 'stripe-credit-card',
253 'authorize_net',
254 'square',
255 ],
256 $form_id
257 );
258
259 foreach ( $wpform_fields as $field ) {
260 if ( in_array( (string) $field['type'], $exclude_fields, true ) ) {
261 continue;
262 }
263 if ( empty( $terms ) ) {
264 $results[] = $field;
265 } elseif ( false !== stripos( $field['label'], $terms ) ) {
266 $results[] = $field;
267 }
268 }
269
270 return [
271 'results' => $results,
272 'has_more' => $count > $limit && $count > $offset,
273 ];
274
275 }
276
277 /**
278 * Get Fluent Forms data.
279 *
280 * @param string $terms search string.
281 * @param int $page Page Number.
282 * @param int $form_id WPForm ID.
283 * @return array
284 * @since 1.0.0
285 */
286 public static function get_fluentform_fields( $terms, $page, $form_id ) {
287 /**
288 * Get terms as string
289 *
290 * @var string $terms Terms string
291 */
292 $terms = esc_sql( $terms );
293 $limit = self::get_search_page_limit();
294 $offset = $limit * ( $page - 1 );
295 $results = [];
296
297 if ( ! function_exists( 'wpFluent' ) ) {
298 return [
299 'results' => [],
300 'has_more' => false,
301 ];
302 }
303
304 $form = wpFluent()->table( 'fluentform_forms' )->find( $form_id );
305 $field_data = json_decode( $form->form_fields, true );
306 $count = ( is_array( $field_data ) && isset( $field_data['fields'] ) && is_array( $field_data['fields'] ) ) ? count( $field_data['fields'] ) : 0;
307
308 if ( is_array( $field_data ) && isset( $field_data['fields'] ) ) {
309 foreach ( $field_data['fields'] as $field ) {
310 // check if the field has multiple inputs ...
311 if ( isset( $field['fields'] ) ) {
312 foreach ( $field['fields'] as $field_key => $sub_field ) {
313 if (
314 isset( $sub_field['settings'] )
315 && is_array( $sub_field['settings'] )
316 && isset( $sub_field['settings']['label'] )
317 && isset( $sub_field['settings']['visible'] )
318 && true === $sub_field['settings']['visible']
319 ) {
320 $results[] = [
321 'value' => $field_key,
322 'text' => esc_html( $sub_field['settings']['label'] ),
323 ];
324 }
325 }
326 } elseif ( isset( $field['element'] ) && 'container' === (string) $field['element'] && isset( $field['columns'] ) && is_array( $field['columns'] ) ) {
327 $container_fields = $field['columns'];
328 foreach ( $container_fields as $c_fields ) {
329 foreach ( $c_fields['fields'] as $field_key => $sub_field ) {
330 if ( isset( $sub_field['settings'] ) && isset( $sub_field['settings']['label'] ) ) {
331 $results[] = [
332 'value' => isset( $sub_field['attributes']['name'] ) ? $sub_field['attributes']['name'] : strtolower( $sub_field['settings']['label'] ),
333 'text' => esc_html( $sub_field['settings']['label'] ),
334 ];
335
336 }
337 }
338 }
339 } elseif ( is_array( $field ) && isset( $field['attributes'] ) && isset( $field['attributes']['name'] ) ) {
340 if ( isset( $field['attributes']['placeholder'] ) && ! empty( $field['attributes']['placeholder'] ) ) {
341 $results[] = [
342 'value' => $field['attributes']['name'],
343 'text' => esc_html( $field['attributes']['placeholder'] ),
344 ];
345 } elseif ( isset( $field['settings'] ) && isset( $field['settings']['label'] ) && ! empty( $field['settings']['label'] ) ) {
346 $results[] = [
347 'value' => $field['attributes']['name'],
348 'text' => esc_html( $field['settings']['label'] ),
349 ];
350 }
351 }
352 }
353 }
354
355 return [
356 'results' => $results,
357 'has_more' => $count > $limit && $count > $offset,
358 ];
359
360 }
361
362 /**
363 * Get terms
364 *
365 * @param string $terms search string.
366 * @param int $page Page Number.
367 * @param array $taxonomy Category type.
368 * @return array
369 * @since 1.0.0
370 */
371 public static function get_terms( $terms, $page, $taxonomy ) {
372 $terms = esc_sql( $terms );
373 $limit = self::get_search_page_limit( 'terms' );
374 $offset = $limit * ( $page - 1 );
375
376 $params = [
377 'offset' => $offset,
378 'number' => $limit,
379 'search' => ! empty( $terms ) ? $terms : '',
380 'hide_empty' => false,
381 'fields' => 'all',
382 ];
383
384 if ( ! empty( $taxonomy ) ) {
385 $params['taxonomy'] = $taxonomy;
386 }
387 $result = get_terms( $params );
388
389 $count_params = [
390 'search' => ! empty( $terms ) ? $terms : '',
391 'hide_empty' => false,
392 'fields' => 'count',
393 ];
394
395 if ( ! empty( $taxonomy ) ) {
396 $count_params['taxonomy'] = $taxonomy;
397 }
398 $count = get_terms( $count_params );
399
400 return [
401 'result' => $result,
402 'has_more' => $count > $limit && $count > $offset,
403 ];
404 }
405
406 /**
407 * Product Subscription Variation.
408 *
409 * @param string $terms Search term.
410 * @param int $page Page number.
411 * @return array
412 * @since 1.0.0
413 */
414 public static function get_subscription_variation( $terms, $page ) {
415
416 $terms = esc_sql( $terms );
417 $limit = self::get_search_page_limit( 'subscription' );
418 $offset = $limit * ( $page - 1 );
419 $params = [
420 'type' => [ 'variable-subscription' ],
421 'limit' => $limit,
422 'offset' => $offset,
423 ];
424 $subscription_products = wc_get_products( $params );
425
426 $count_params = [
427 'type' => [ 'variable-subscription' ],
428 ];
429
430 /**
431 * Get wc product as countable array
432 *
433 * @var array $countable_products
434 */
435 $countable_products = wc_get_products( $count_params );
436 $count = count( $countable_products );
437
438 return [
439 'result' => $subscription_products,
440 'has_more' => $count > $limit && $count > $offset,
441 ];
442 }
443
444 /**
445 * Get variable products.
446 *
447 * @param string $terms Search term.
448 * @param int $page Page number.
449 * @return array
450 * @since 1.0.0
451 */
452 public static function get_variable_products( $terms, $page ) {
453
454 $terms = esc_sql( $terms );
455 $limit = self::get_search_page_limit( 'variable_products' );
456 $offset = $limit * ( $page - 1 );
457 $params = [
458 'status' => 'publish',
459 'type' => [ 'variable' ],
460 'limit' => $limit,
461 'offset' => $offset,
462 'sku' => $terms,
463 ];
464
465 $products = wc_get_products( $params );
466
467 $count_params = [
468 'status' => 'publish',
469 'type' => [ 'variable' ],
470 'sku' => $terms,
471 ];
472
473 /**
474 * Get wc product as countable array
475 *
476 * @var array $countable_products
477 */
478 $countable_products = wc_get_products( $count_params );
479 $count = count( $countable_products );
480
481 return [
482 'result' => $products,
483 'has_more' => $count > $limit && $count > $offset,
484 ];
485 }
486
487 /**
488 * Get selected product variations.
489 *
490 * @param int $parent parent id.
491 * @return array
492 * @since 1.0.0
493 */
494 public static function get_product_variations( $parent ) {
495
496 $params = [
497 'post_status' => 'publish',
498 'post_parent' => absint( $parent ),
499 'post_type' => 'product_variation',
500 // Todo: Implement pagination.
501 'posts_per_page' => 999, // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page
502 ];
503
504 $products = get_posts( $params );
505
506 return [
507 'result' => $products,
508 'has_more' => false,
509 ];
510 }
511
512 /**
513 * Get learndash courses.
514 *
515 * @return array
516 */
517 public static function get_product_courses() {
518
519 $courses = get_posts(
520 [
521
522 'post_type' => 'product',
523 'meta_key' => '_related_course',
524 ]
525 );
526
527 return [
528 'result' => $courses,
529 'has_more' => false,
530 ];
531 }
532
533 /**
534 * Get elementor forms.
535 *
536 * @return array
537 */
538 public static function get_elementor_forms() {
539
540 global $wpdb;
541 $elementor_forms = [];
542 $post_metas = $wpdb->get_results(
543 $wpdb->prepare(
544 "SELECT pm.meta_value
545 FROM $wpdb->postmeta pm
546 LEFT JOIN $wpdb->posts p
547 ON p.ID = pm.post_id
548 WHERE p.post_type IS NOT NULL
549 AND p.post_status = %s
550 AND pm.meta_key = %s
551 AND pm.`meta_value` LIKE %s",
552 'publish',
553 '_elementor_data',
554 '%%form_fields%%'
555 )
556 );
557
558 if ( ! empty( $post_metas ) ) {
559 foreach ( $post_metas as $post_meta ) {
560 $inner_forms = self::search_elementor_forms( (array) json_decode( $post_meta->meta_value ) );
561 if ( ! empty( $inner_forms ) ) {
562 foreach ( $inner_forms as $form ) {
563 if ( is_object( $form ) ) {
564 $elementor_forms[ $form->id ] = $form->settings->form_name;
565 }
566 }
567 }
568 }
569 }
570
571 return $elementor_forms;
572 }
573
574 /**
575 * Search elementor forms.
576 *
577 * @param array $elements Search Forms.
578 * @return array[]
579 */
580 public static function search_elementor_forms( $elements ) {
581 $block_is_on_page = [];
582 if ( ! empty( $elements ) ) {
583 foreach ( $elements as $element ) {
584 if ( 'widget' === $element->elType && 'form' === $element->widgetType ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
585 $block_is_on_page[] = $element;
586 }
587 if ( ! empty( $element->elements ) ) {
588 $inner_block_is_on_page = self::search_elementor_forms( $element->elements );
589 if ( ! empty( $inner_block_is_on_page ) ) {
590 $block_is_on_page = array_merge( $block_is_on_page, $inner_block_is_on_page );
591 }
592 }
593 }
594 }
595
596 return $block_is_on_page;
597 }
598
599 /**
600 * Search elementor form fields.
601 *
602 * @param array $data Search Params.
603 * @return array[]
604 */
605 public function get_elementor_form_fields( $data ) {
606
607 global $wpdb;
608 $form_id = $data['dynamic'];
609 $elementor_form_fields = [];
610 $post_metas = $wpdb->get_results(
611 $wpdb->prepare(
612 "SELECT pm.meta_value
613 FROM $wpdb->postmeta pm
614 LEFT JOIN $wpdb->posts p
615 ON p.ID = pm.post_id
616 WHERE p.post_type IS NOT NULL
617 AND p.post_status = %s
618 AND pm.meta_key = %s
619 AND pm.`meta_value` LIKE %s",
620 'publish',
621 '_elementor_data',
622 '%%' . $form_id . '%%'
623 )
624 );
625
626 if ( ! empty( $post_metas ) ) {
627 foreach ( $post_metas as $post_meta ) {
628 $inner_forms = self::search_elementor_forms( (array) json_decode( $post_meta->meta_value ) );
629 if ( ! empty( $inner_forms ) ) {
630 foreach ( $inner_forms as $form ) {
631 if ( is_object( $form ) ) {
632 foreach ( $form->settings->form_fields as $form_field ) {
633 $elementor_form_fields[ $form_field->custom_id ] = $form_field->field_label;
634 }
635 }
636 }
637 }
638 }
639 }
640
641 return $elementor_form_fields;
642 }
643
644 /**
645 * Get All orders IDs for a given product ID.
646 *
647 * @param integer $product_id product id.
648 *
649 * @return array
650 */
651 public function get_orders_ids_by_product_id( $product_id ) {
652 global $wpdb;
653 $table_name = $wpdb->prefix . 'wc_orders';
654
655 // Check if the table exists.
656 $table_exists = $wpdb->get_var("SELECT COUNT(*) FROM information_schema.tables WHERE table_name = '$table_name' AND (SELECT COUNT(*) FROM $table_name) > 0"); // phpcs:ignore
657
658 if ( $table_exists ) {
659
660 $count = $wpdb->get_col(
661 $wpdb->prepare(
662 "
663 SELECT order_items.order_id
664 FROM {$wpdb->prefix}woocommerce_order_items as order_items
665 LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
666 LEFT JOIN {$wpdb->prefix}wc_orders AS orders ON order_items.order_id = orders.id
667 WHERE orders.type = 'shop_order'
668 AND orders.status IN ('wc-completed', 'wc-processing', 'wc-on-hold')
669 AND order_items.order_item_type = 'line_item'
670 AND order_item_meta.meta_key = '_product_id'
671 AND order_item_meta.meta_value = %s
672 ORDER BY order_items.order_id DESC",
673 $product_id
674 )
675 );
676
677 } else {
678 $count = $wpdb->get_col(
679 $wpdb->prepare(
680 "
681 SELECT order_items.order_id
682 FROM {$wpdb->prefix}woocommerce_order_items as order_items
683 LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
684 LEFT JOIN {$wpdb->prefix}posts AS posts ON order_items.order_id = posts.ID
685 WHERE posts.post_type = 'shop_order'
686 AND posts.post_status IN ('wc-completed', 'wc-processing', 'wc-on-hold')
687 AND order_items.order_item_type = 'line_item'
688 AND order_item_meta.meta_key = '_product_id'
689 AND order_item_meta.meta_value = %s
690 ORDER BY order_items.order_id DESC",
691 $product_id
692 )
693 );
694 }
695 return $count;
696 }
697
698 /**
699 * Convert an object to an array.
700 *
701 * @param object $object The object to convert.
702 * @return array The object as an array.
703 */
704 public static function object_to_array( $object ) {
705 $array = [];
706 $reflection = new \ReflectionClass( $object );
707
708 foreach ( $reflection->getProperties() as $property ) {
709 $property->setAccessible( true );
710 $array[ $property->getName() ] = $property->getValue( $object );
711 }
712
713 return $array;
714 }
715
716 }
717