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