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