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