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