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