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