PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.13
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.13
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/FrmDb.php +103 -168 6.306.13 View file →
@@ -3,20 +3,36 @@
3 3 die( 'You are not allowed to call this page directly.' );
4 4 }
5 5
6 6 class FrmDb {
7 + public $fields;
8 + public $forms;
9 + public $entries;
10 + public $entry_metas;
7 11
12 + public function __construct() {
13 + if ( ! defined( 'ABSPATH' ) ) {
14 + die( 'You are not allowed to call this page directly.' );
15 + }
16 +
17 + _deprecated_function( __METHOD__, '2.05.06', 'FrmMigrate' );
18 + global $wpdb;
19 + $this->fields = $wpdb->prefix . 'frm_fields';
20 + $this->forms = $wpdb->prefix . 'frm_forms';
21 + $this->entries = $wpdb->prefix . 'frm_items';
22 + $this->entry_metas = $wpdb->prefix . 'frm_item_metas';
23 + }
24 +
8 25 /**
9 26 * Change array into format $wpdb->prepare can use
10 27 *
11 28 * @param array $args
12 29 * @param string $starts_with
13 - *
14 30 * @return void
15 31 */
16 32 public static function get_where_clause_and_values( &$args, $starts_with = ' WHERE ' ) {
17 - if ( ! $args ) {
18 - // Add an arg to prevent prepare from failing
33 + if ( empty( $args ) ) {
34 + // add an arg to prevent prepare from failing
19 35 $args = array(
20 36 'where' => $starts_with . '1=%d',
21 37 'values' => array( 1 ),
22 38 );
@@ -39,14 +55,11 @@
39 55 * @param array $args
40 56 * @param string $base_where
41 57 * @param string $where
42 58 * @param array $values
43 - *
44 - * @return void
45 59 */
46 60 public static function parse_where_from_array( $args, $base_where, &$where, &$values ) {
47 61 $condition = ' AND';
48 -
49 62 if ( isset( $args['or'] ) ) {
50 63 $condition = ' OR';
51 64 unset( $args['or'] );
52 65 }
@@ -51,16 +64,13 @@
51 64 unset( $args['or'] );
52 65 }
53 66
54 67 foreach ( $args as $key => $value ) {
55 - $where .= $where ? $condition : $base_where;
56 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
57 - $array_inc_null = ! is_numeric( $key ) && is_array( $value ) && in_array( null, $value );
58 -
68 + $where .= empty( $where ) ? $base_where : $condition;
69 + $array_inc_null = ( ! is_numeric( $key ) && is_array( $value ) && in_array( null, $value ) );
59 70 if ( is_numeric( $key ) || $array_inc_null ) {
60 71 $where .= ' ( ';
61 72 $nested_where = '';
62 -
63 73 if ( $array_inc_null ) {
64 74 foreach ( $value as $val ) {
65 75 $parse_where = array(
66 76 $key => $val,
@@ -70,29 +80,27 @@
70 80 }
71 81 } else {
72 82 self::parse_where_from_array( $value, '', $nested_where, $values );
73 83 }
74 -
75 84 $where .= $nested_where;
76 85 $where .= ' ) ';
77 86 } else {
78 87 self::interpret_array_to_sql( $key, $value, $where, $values );
79 - }//end if
88 + }
80 89 }//end foreach
81 90 }
82 91
83 92 /**
84 - * @param string $key
85 - * @param array|string|null $value
86 - * @param string $where
87 - * @param array $values
88 - *
93 + * @param string $key
94 + * @param array|string $value
95 + * @param string $where
96 + * @param array $values
89 97 * @return void
90 98 */
91 99 private static function interpret_array_to_sql( $key, $value, &$where, &$values ) {
92 100 $key = trim( $key );
93 101
94 - if ( str_contains( $key, 'created_at' ) || str_contains( $key, 'updated_at' ) ) {
102 + if ( strpos( $key, 'created_at' ) !== false || strpos( $key, 'updated_at' ) !== false ) {
95 103 $k = explode( ' ', $key );
96 104 $where .= ' CAST(' . reset( $k ) . ' as CHAR) ' . str_replace( reset( $k ), '', $key );
97 105 } else {
98 106 $where .= ' ' . $key;
@@ -101,30 +109,27 @@
101 109 $lowercase_key = explode( ' ', strtolower( $key ) );
102 110 $lowercase_key = end( $lowercase_key );
103 111
104 112 if ( is_array( $value ) ) {
105 - // Translate array of values to "in"
106 - if ( str_contains( $lowercase_key, 'like' ) ) {
113 + // translate array of values to "in"
114 + if ( strpos( $lowercase_key, 'like' ) !== false ) {
107 115 $where = preg_replace( '/' . $key . '$/', '', $where );
108 116 $where .= '(';
109 117 $start = true;
110 -
111 118 foreach ( $value as $v ) {
112 119 if ( ! $start ) {
113 120 $where .= ' OR ';
114 121 }
115 -
116 122 $start = false;
117 123 $where .= $key . ' %s';
118 124 $values[] = '%' . self::esc_like( $v ) . '%';
119 125 }
120 -
121 126 $where .= ')';
122 - } elseif ( $value ) {
127 + } elseif ( ! empty( $value ) ) {
123 128 $where .= ' in (' . self::prepare_array_values( $value, '%s' ) . ')';
124 129 $values = array_merge( $values, $value );
125 130 }
126 - } elseif ( str_contains( $lowercase_key, 'like' ) ) {
131 + } elseif ( strpos( $lowercase_key, 'like' ) !== false ) {
127 132 /**
128 133 * Allow string to start or end with the value
129 134 * If the key is like% then skip the first % for starts with
130 135 * If the key is %like then skip the last % for ends with
@@ -130,13 +135,12 @@
130 135 * If the key is %like then skip the last % for ends with
131 136 */
132 137 $start = '%';
133 138 $end = '%';
134 -
135 139 if ( $lowercase_key === 'like%' ) {
136 140 $start = '';
137 141 $where = rtrim( $where, '%' );
138 - } elseif ( $lowercase_key === '%like' ) {
142 + } elseif ( $lowercase_key == '%like' ) {
139 143 $end = '';
140 144 $where = rtrim( rtrim( $where, '%like' ), '%LIKE' );
141 145 $where .= 'like';
142 146 }
@@ -142,13 +146,14 @@
142 146 }
143 147
144 148 $where .= ' %s';
145 149 $values[] = $start . self::esc_like( $value ) . $end;
150 +
146 151 } elseif ( $value === null ) {
147 152 $where .= ' IS NULL';
148 153 } else {
149 154 // allow a - to prevent = from being added
150 - if ( str_ends_with( $key, '-' ) ) {
155 + if ( substr( $key, - 1 ) === '-' ) {
151 156 $where = rtrim( $where, '-' );
152 157 } else {
153 158 $where .= '=';
154 159 }
@@ -166,13 +171,11 @@
166 171 *
167 172 * @param string $key
168 173 * @param int|string $value
169 174 * @param string $where
170 - *
171 - * @return void
172 175 */
173 176 private static function add_query_placeholder( $key, $value, &$where ) {
174 - if ( is_numeric( $value ) && ( ! str_contains( $key, 'meta_value' ) || str_contains( $key, '+0' ) ) ) {
177 + if ( is_numeric( $value ) && ( strpos( $key, 'meta_value' ) === false || strpos( $key, '+0' ) !== false ) ) {
175 178 // Switch string to number.
176 179 $value = $value + 0;
177 180 $where .= is_float( $value ) ? '%f' : '%d';
178 181 } else {
@@ -188,8 +191,9 @@
188 191 * @return int
189 192 */
190 193 public static function get_count( $table, $where = array(), $args = array() ) {
191 194 $count = self::get_var( $table, $where, 'COUNT(*)', $args );
195 +
192 196 return (int) $count;
193 197 }
194 198
195 199 /**
@@ -205,17 +209,18 @@
205 209 public static function get_var( $table, $where = array(), $field = 'id', $args = array(), $limit = '', $type = 'var' ) {
206 210 $group = '';
207 211 self::get_group_and_table_name( $table, $group );
208 212 self::convert_options_to_array( $args, '', $limit );
209 -
210 213 if ( $type === 'var' && ! isset( $args['limit'] ) ) {
211 214 $args['limit'] = 1;
212 215 }
213 216
214 - $query = self::generate_query_string_from_pieces( $field, $table, $where, $args );
217 + $query = self::generate_query_string_from_pieces( $field, $table, $where, $args );
218 +
215 219 $cache_key = self::generate_cache_key( $where, $args, $field, $type );
220 + $results = self::check_cache( $cache_key, $group, $query, 'get_' . $type );
216 221
217 - return self::check_cache( $cache_key, $group, $query, 'get_' . $type );
222 + return $results;
218 223 }
219 224
220 225 /**
221 226 * Generate a cache key from the where query, field, type, and other arguments
@@ -231,16 +236,15 @@
231 236 */
232 237 public static function generate_cache_key( $where, $args, $field, $type ) {
233 238 $cache_key = '';
234 239 $where = FrmAppHelper::array_flatten( $where );
235 -
236 240 foreach ( $where as $key => $value ) {
237 241 $cache_key .= $key . '_' . $value;
238 242 }
239 -
240 243 $cache_key .= implode( '_', $args ) . $field . '_' . $type;
244 + $cache_key = str_replace( array( ' ', ',' ), '_', $cache_key );
241 245
242 - return str_replace( array( ' ', ',' ), '_', $cache_key );
246 + return $cache_key;
243 247 }
244 248
245 249 /**
246 250 * @param string $table
@@ -248,9 +252,9 @@
248 252 * @param string $field
249 253 * @param array $args
250 254 * @param string $limit
251 255 *
252 - * @return array
256 + * @return mixed
253 257 */
254 258 public static function get_col( $table, $where = array(), $field = 'id', $args = array(), $limit = '' ) {
255 259 return self::get_var( $table, $where, $field, $args, $limit, 'col' );
256 260 }
@@ -266,8 +270,9 @@
266 270 * @return mixed
267 271 */
268 272 public static function get_row( $table, $where = array(), $fields = '*', $args = array() ) {
269 273 $args['limit'] = 1;
274 +
270 275 return self::get_var( $table, $where, $fields, $args, '', 'row' );
271 276 }
272 277
273 278 /**
@@ -279,9 +284,9 @@
279 284 * @param array $where
280 285 * @param string $fields
281 286 * @param array $args
282 287 *
283 - * @return array
288 + * @return mixed
284 289 */
285 290 public static function get_results( $table, $where = array(), $fields = '*', $args = array() ) {
286 291 return self::get_var( $table, $where, $fields, $args, '', 'results' );
287 292 }
@@ -308,9 +313,8 @@
308 313 '%like' => '%like',
309 314 );
310 315
311 316 $where_is = strtolower( $where_is );
312 -
313 317 if ( isset( $switch_to[ $where_is ] ) ) {
314 318 return ' ' . $switch_to[ $where_is ];
315 319 }
316 320
@@ -329,26 +333,24 @@
329 333 * Also add the wpdb->prefix to the table if it's missing
330 334 *
331 335 * @param string $table
332 336 * @param string $group
333 - *
334 - * @return void
335 337 */
336 338 private static function get_group_and_table_name( &$table, &$group ) {
337 - global $wpdb;
339 + global $wpdb, $wpmuBaseTablePrefix;
338 340
339 341 $table_parts = explode( ' ', $table );
340 342 $group = reset( $table_parts );
341 343 self::maybe_remove_prefix( $wpdb->prefix, $group );
342 344
343 - $prefix = $wpdb->base_prefix;
345 + $prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix : $wpdb->base_prefix;
344 346 self::maybe_remove_prefix( $prefix, $group );
345 347
346 - if ( $group === $table ) {
348 + if ( $group == $table ) {
347 349 $table = $wpdb->prefix . $table;
348 350 }
349 351
350 - // Switch to singular group name
352 + // switch to singular group name
351 353 $group = rtrim( $group, 's' );
352 354 }
353 355
354 356 /**
@@ -354,44 +356,30 @@
354 356 /**
355 357 * Only remove the db prefix when at the beginning.
356 358 *
357 359 * @since 4.04.02
358 - *
359 - * @param string $prefix Prefix to remove.
360 - * @param string $name Name to strip prefix from, passed by reference.
361 - *
362 - * @return void
363 360 */
364 361 private static function maybe_remove_prefix( $prefix, &$name ) {
365 - if ( str_starts_with( $name, $prefix ) ) {
362 + if ( substr( $name, 0, strlen( $prefix ) ) === $prefix ) {
366 363 $name = substr( $name, strlen( $prefix ) );
367 364 }
368 365 }
369 366
370 - /**
371 - * @param array|string $args
372 - * @param string $order_by
373 - * @param int|string $limit
374 - *
375 - * @return void
376 - */
377 367 private static function convert_options_to_array( &$args, $order_by = '', $limit = '' ) {
378 368 if ( ! is_array( $args ) ) {
379 369 $args = array( 'order_by' => $args );
380 370 }
381 371
382 - if ( $order_by ) {
372 + if ( ! empty( $order_by ) ) {
383 373 $args['order_by'] = $order_by;
384 374 }
385 375
386 - if ( $limit ) {
376 + if ( ! empty( $limit ) ) {
387 377 $args['limit'] = $limit;
388 378 }
389 379
390 380 $temp_args = $args;
391 -
392 381 foreach ( $temp_args as $k => $v ) {
393 - // phpcs:ignore Universal.Operators.StrictComparisons
394 382 if ( $v == '' ) {
395 383 unset( $args[ $k ] );
396 384 continue;
397 385 }
@@ -396,22 +384,19 @@
396 384 continue;
397 385 }
398 386
399 387 $db_name = strtoupper( str_replace( '_', ' ', $k ) );
400 -
401 - if ( ! str_contains( $v, $db_name ) ) {
388 + if ( strpos( $v, $db_name ) === false ) {
402 389 $args[ $k ] = $db_name . ' ' . $v;
403 390 }
404 391 }
405 392
406 393 // Make sure LIMIT is the last argument
407 - if ( ! isset( $args['order_by'] ) || ! isset( $args['limit'] ) ) {
408 - return;
394 + if ( isset( $args['order_by'] ) && isset( $args['limit'] ) ) {
395 + $temp_limit = $args['limit'];
396 + unset( $args['limit'] );
397 + $args['limit'] = $temp_limit;
409 398 }
410 -
411 - $temp_limit = $args['limit'];
412 - unset( $args['limit'] );
413 - $args['limit'] = $temp_limit;
414 399 }
415 400
416 401 /**
417 402 * Get the associative array results for the given columns, table, and where query
@@ -427,12 +412,14 @@
427 412 public static function get_associative_array_results( $columns, $table, $where ) {
428 413 $group = '';
429 414 self::get_group_and_table_name( $table, $group );
430 415
431 - $query = self::generate_query_string_from_pieces( $columns, $table, $where );
416 + $query = self::generate_query_string_from_pieces( $columns, $table, $where );
417 +
432 418 $cache_key = str_replace( array( ' ', ',' ), '_', trim( implode( '_', FrmAppHelper::array_flatten( $where ) ) . $columns . '_results_ARRAY_A', ' WHERE' ) );
419 + $results = self::check_cache( $cache_key, $group, $query, 'get_associative_results' );
433 420
434 - return self::check_cache( $cache_key, $group, $query, 'get_associative_results' );
421 + return $results;
435 422 }
436 423
437 424 /**
438 425 * Combine the pieces of a query to form a full, prepared query
@@ -450,25 +437,26 @@
450 437 $query = 'SELECT ' . $columns . ' FROM ' . $table;
451 438
452 439 self::esc_query_args( $args );
453 440
454 - if ( ! is_array( $where ) && $where ) {
455 - return $query;
441 + if ( is_array( $where ) || empty( $where ) ) {
442 + self::get_where_clause_and_values( $where );
443 + global $wpdb;
444 + $query = $wpdb->prepare( $query . $where['where'] . ' ' . implode( ' ', $args ), $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
445 + } else {
446 + /**
447 + * Allow the $where to be prepared before we receive it here.
448 + * This is a fallback for reverse compatibility, but is not recommended
449 + */
450 + _deprecated_argument( 'where', '2.0', esc_html__( 'Use the query in an array format so it can be properly prepared.', 'formidable' ) );
451 + $query .= $where . ' ' . implode( ' ', $args );
456 452 }
457 453
458 - self::get_where_clause_and_values( $where );
459 - global $wpdb;
460 -
461 - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
462 - return $wpdb->prepare( $query . $where['where'] . ' ' . implode( ' ', $args ), $where['values'] );
454 + return $query;
463 455 }
464 456
465 457 /**
466 458 * @since 2.05.07
467 - *
468 - * @param array $args Query arguments, passed by reference.
469 - *
470 - * @return void
471 459 */
472 460 private static function esc_query_args( &$args ) {
473 461 foreach ( $args as $param => $value ) {
474 462 if ( $param === 'order_by' ) {
@@ -476,9 +464,8 @@
476 464 } elseif ( $param === 'limit' ) {
477 465 $args[ $param ] = self::esc_limit( $value );
478 466 }
479 467
480 - // phpcs:ignore Universal.Operators.StrictComparisons
481 468 if ( $args[ $param ] == '' ) {
482 469 unset( $args[ $param ] );
483 470 }
484 471 }
@@ -494,8 +481,9 @@
494 481 * @return string The escaped value
495 482 */
496 483 public static function esc_like( $term ) {
497 484 global $wpdb;
485 +
498 486 return $wpdb->esc_like( $term );
499 487 }
500 488
501 489 /**
@@ -501,33 +489,29 @@
501 489 /**
502 490 * @since 2.05.06
503 491 *
504 492 * @param string $order_query
505 - *
506 - * @return string
507 493 */
508 494 public static function esc_order( $order_query ) {
509 - if ( ! $order_query ) {
495 + if ( empty( $order_query ) ) {
510 496 return '';
511 497 }
512 498
513 499 // Remove ORDER BY before sanitizing.
514 500 $order_query = strtolower( $order_query );
515 -
516 - if ( str_contains( $order_query, 'order by' ) ) {
501 + if ( strpos( $order_query, 'order by' ) !== false ) {
517 502 $order_query = str_replace( 'order by', '', $order_query );
518 503 }
519 504
520 505 $order_query = explode( ' ', trim( $order_query ) );
521 - $order = trim( reset( $order_query ) );
522 - $safe_order = array( 'count(*)' );
523 506
524 - if ( ! in_array( strtolower( $order ), $safe_order, true ) ) {
507 + $order = trim( reset( $order_query ) );
508 + $safe_order = array( 'count(*)' );
509 + if ( ! in_array( strtolower( $order ), $safe_order ) ) {
525 510 $order = preg_replace( '/[^a-zA-Z0-9\-\_\.\+]/', '', $order );
526 511 }
527 512
528 513 $order_by = '';
529 -
530 514 if ( count( $order_query ) > 1 ) {
531 515 $order_by = end( $order_query );
532 516 self::esc_order_by( $order_by );
533 517 }
@@ -538,16 +522,11 @@
538 522 /**
539 523 * Make sure this is ordering by either ASC or DESC
540 524 *
541 525 * @since 2.05.06
542 - *
543 - * @param string $order_by Sort direction, passed by reference.
544 - *
545 - * @return void
546 526 */
547 527 public static function esc_order_by( &$order_by ) {
548 528 $sort_options = array( 'asc', 'desc' );
549 -
550 529 if ( ! in_array( strtolower( $order_by ), $sort_options, true ) ) {
551 530 $order_by = 'asc';
552 531 }
553 532 }
@@ -553,26 +532,21 @@
553 532 }
554 533
555 534 /**
556 535 * @since 2.05.06
557 - *
558 536 * @param string $limit
559 - *
560 - * @return string
561 537 */
562 538 public static function esc_limit( $limit ) {
563 - if ( ! $limit ) {
539 + if ( empty( $limit ) ) {
564 540 return '';
565 541 }
566 542
567 543 $limit = trim( str_replace( 'limit ', '', strtolower( $limit ) ) );
568 -
569 544 if ( is_numeric( $limit ) ) {
570 545 return ' LIMIT ' . $limit;
571 546 }
572 547
573 548 $limit = explode( ',', trim( $limit ) );
574 -
575 549 foreach ( $limit as $k => $l ) {
576 550 if ( is_numeric( $l ) ) {
577 551 $limit[ $k ] = $l;
578 552 }
@@ -586,16 +560,12 @@
586 560 /**
587 561 * Get an array of values ready to go through $wpdb->prepare
588 562 *
589 563 * @since 2.05.06
590 - *
591 - * @param array $array Array of values.
592 - * @param string $type Placeholder type.
593 - *
594 - * @return string
595 564 */
596 565 public static function prepare_array_values( $array, $type = '%s' ) {
597 566 $placeholders = array_fill( 0, count( $array ), $type );
567 +
598 568 return implode( ', ', $placeholders );
599 569 }
600 570
601 571 /**
@@ -602,18 +572,17 @@
602 572 * @since 2.05.06
603 573 *
604 574 * @param string $starts_with
605 575 * @param array|string $where
606 - *
607 576 * @return string
608 577 */
609 578 public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) {
610 - if ( ! $where ) {
579 + if ( empty( $where ) ) {
611 580 $where = '';
612 581 } elseif ( is_array( $where ) ) {
613 - global $wpdb;
614 - self::get_where_clause_and_values( $where, $starts_with );
615 - $where = $wpdb->prepare( $where['where'], $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
582 + global $wpdb;
583 + self::get_where_clause_and_values( $where, $starts_with );
584 + $where = $wpdb->prepare( $where['where'], $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
616 585 } else {
617 586 $where = $starts_with . $where;
618 587 }
619 588
@@ -631,12 +600,10 @@
631 600 /**
632 601 * Prepare and save settings in styles and actions
633 602 *
634 603 * @since 2.05.06
635 - *
636 - * @param array|object $settings
637 - * @param string $group
638 - *
604 + * @param array $settings
605 + * @param string $group
639 606 * @return int|WP_Error
640 607 */
641 608 public static function save_settings( $settings, $group ) {
642 609 $settings = (array) $settings;
@@ -645,9 +612,9 @@
645 612 if ( empty( $settings['ID'] ) ) {
646 613 unset( $settings['ID'] );
647 614 }
648 615
649 - // Delete all caches for this group
616 + // delete all caches for this group
650 617 self::cache_delete_group( $group );
651 618
652 619 return self::save_json_post( $settings );
653 620 }
@@ -661,14 +628,12 @@
661 628 *
662 629 * @since 2.05.06
663 630 *
664 631 * @param array $settings
665 - *
666 632 * @return int|WP_Error
667 633 */
668 634 public static function save_json_post( $settings ) {
669 635 global $wp_filter;
670 -
671 636 if ( isset( $wp_filter['content_save_pre'] ) ) {
672 637 $filters = $wp_filter['content_save_pre'];
673 638 }
674 639
@@ -676,9 +641,9 @@
676 641 remove_all_filters( 'content_save_pre' );
677 642
678 643 $post = wp_insert_post( $settings );
679 644
680 - // Add the content filters back for views or posts
645 + // add the content filters back for views or posts
681 646 if ( isset( $filters ) ) {
682 647 $wp_filter['content_save_pre'] = $filters;
683 648 }
684 649
@@ -693,23 +658,20 @@
693 658 * @param string $cache_key The unique name for this cache.
694 659 * @param string $group The name of the cache group.
695 660 * @param string $query If blank, don't run a db call.
696 661 * @param string $type The wpdb function to use with this query.
697 - * @param int $time Cache expiration time in seconds.
698 662 *
699 663 * @return mixed $results The cache or query results
700 664 */
701 665 public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) {
702 - $found = null;
703 - $results = wp_cache_get( $cache_key, $group, false, $found );
704 -
705 - if ( ( $found === true && $results !== false ) || ! $query ) {
666 + $results = wp_cache_get( $cache_key, $group );
667 + if ( ! FrmAppHelper::is_empty_value( $results, false ) || empty( $query ) ) {
706 668 return $results;
707 669 }
708 670
709 - if ( 'get_posts' === $type ) {
671 + if ( 'get_posts' == $type ) {
710 672 $results = get_posts( $query );
711 - } elseif ( 'get_associative_results' === $type ) {
673 + } elseif ( 'get_associative_results' == $type ) {
712 674 global $wpdb;
713 675 $results = $wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
714 676 } else {
715 677 global $wpdb;
@@ -722,23 +684,14 @@
722 684 }
723 685
724 686 /**
725 687 * @since 2.05.06
726 - *
727 - * @param string $cache_key The unique name for this cache.
728 - * @param mixed $results Cached results.
729 - * @param string $group The name of the cache group.
730 - * @param int $time Cache expiration time in seconds.
731 - *
732 - * @return void
733 688 */
734 689 public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) {
735 - if ( FrmAppHelper::prevent_caching() ) {
736 - return;
690 + if ( ! FrmAppHelper::prevent_caching() ) {
691 + self::add_key_to_group_cache( $cache_key, $group );
692 + wp_cache_set( $cache_key, $results, $group, $time );
737 693 }
738 -
739 - self::add_key_to_group_cache( $cache_key, $group );
740 - wp_cache_set( $cache_key, $results, $group, $time );
741 694 }
742 695
743 696 /**
744 697 * Keep track of the keys cached in each group so they can be deleted
@@ -744,13 +697,8 @@
744 697 * Keep track of the keys cached in each group so they can be deleted
745 698 * in Redis and Memcache
746 699 *
747 700 * @since 2.05.06
748 - *
749 - * @param string $key Cache key.
750 - * @param string $group Cache group name.
751 - *
752 - * @return void
753 701 */
754 702 public static function add_key_to_group_cache( $key, $group ) {
755 703 $cached = self::get_group_cached_keys( $group );
756 704 $cached[ $key ] = $key;
@@ -758,18 +706,13 @@
758 706 }
759 707
760 708 /**
761 709 * @since 2.05.06
762 - *
763 - * @param string $group Cache group name.
764 - *
765 - * @return array
766 710 */
767 711 public static function get_group_cached_keys( $group ) {
768 712 $cached = wp_cache_get( 'cached_keys', $group );
769 -
770 713 if ( ! $cached || ! is_array( $cached ) ) {
771 - return array();
714 + $cached = array();
772 715 }
773 716
774 717 return $cached;
775 718 }
@@ -776,12 +719,9 @@
776 719
777 720 /**
778 721 * @since 2.05.06
779 722 *
780 - * @param string $cache_key Cache key to delete.
781 - * @param string $group Cache group name.
782 - *
783 - * @return void
723 + * @param string $cache_key
784 724 */
785 725 public static function delete_cache_and_transient( $cache_key, $group = 'default' ) {
786 726 delete_transient( $cache_key );
787 727 wp_cache_delete( $cache_key, $group );
@@ -792,23 +732,19 @@
792 732 *
793 733 * @since 2.05.06
794 734 *
795 735 * @param string $group The name of the cache group.
796 - *
797 - * @return void
798 736 */
799 737 public static function cache_delete_group( $group ) {
800 738 $cached_keys = self::get_group_cached_keys( $group );
801 739
802 - if ( ! $cached_keys ) {
803 - return;
804 - }
740 + if ( ! empty( $cached_keys ) ) {
741 + foreach ( $cached_keys as $key ) {
742 + wp_cache_delete( $key, $group );
743 + }
805 744
806 - foreach ( $cached_keys as $key ) {
807 - wp_cache_delete( $key, $group );
745 + wp_cache_delete( 'cached_keys', $group );
808 746 }
809 -
810 - wp_cache_delete( 'cached_keys', $group );
811 747 }
812 748
813 749 /**
814 750 * Checks if a DB column exists.
@@ -816,9 +752,8 @@
816 752 * @since 6.7
817 753 *
818 754 * @param string $table Table name without `$wpdb->prefix`.
819 755 * @param string $column Column name.
820 - *
821 756 * @return bool
822 757 */
823 758 public static function db_column_exists( $table, $column ) {
824 759 global $wpdb;