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