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