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