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