| @@ -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,14 +26,13 @@ | ||
| 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 ) { | |
| 59 | - // Add an arg to prevent prepare from failing | |
| 33 | + if ( empty( $args ) ) { | |
| 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 ), |
| 63 | 38 | ); |
| @@ -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 .= $where ? $condition : $base_where; | |
| 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,29 +80,27 @@ | ||
| 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 | - * @param string $key | |
| 126 | - * @param array|string|null $value | |
| 127 | - * @param string $where | |
| 128 | - * @param array $values | |
| 129 | - * | |
| 93 | + * @param string $key | |
| 94 | + * @param array|string $value | |
| 95 | + * @param string $where | |
| 96 | + * @param array $values | |
| 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; |
| @@ -142,30 +109,27 @@ | ||
| 142 | 109 | $lowercase_key = explode( ' ', strtolower( $key ) ); |
| 143 | 110 | $lowercase_key = end( $lowercase_key ); |
| 144 | 111 | |
| 145 | 112 | if ( is_array( $value ) ) { |
| 146 | - // Translate array of values to "in" | |
| 147 | - if ( str_contains( $lowercase_key, 'like' ) ) { | |
| 113 | + // translate array of values to "in" | |
| 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 | } |
| @@ -183,13 +146,14 @@ | ||
| 183 | 146 | } |
| 184 | 147 | |
| 185 | 148 | $where .= ' %s'; |
| 186 | 149 | $values[] = $start . self::esc_like( $value ) . $end; |
| 150 | + | |
| 187 | 151 | } elseif ( $value === null ) { |
| 188 | 152 | $where .= ' IS NULL'; |
| 189 | 153 | } else { |
| 190 | 154 | // allow a - to prevent = from being added |
| 191 | - if ( str_ends_with( $key, '-' ) ) { | |
| 155 | + if ( substr( $key, - 1 ) === '-' ) { | |
| 192 | 156 | $where = rtrim( $where, '-' ); |
| 193 | 157 | } else { |
| 194 | 158 | $where .= '='; |
| 195 | 159 | } |
| @@ -207,13 +171,11 @@ | ||
| 207 | 171 | * |
| 208 | 172 | * @param string $key |
| 209 | 173 | * @param int|string $value |
| 210 | 174 | * @param string $where |
| 211 | - * | |
| 212 | - * @return void | |
| 213 | 175 | */ |
| 214 | 176 | private static function add_query_placeholder( $key, $value, &$where ) { |
| 215 | - 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 ) ) { | |
| 216 | 178 | // Switch string to number. |
| 217 | 179 | $value = $value + 0; |
| 218 | 180 | $where .= is_float( $value ) ? '%f' : '%d'; |
| 219 | 181 | } else { |
| @@ -229,8 +191,9 @@ | ||
| 229 | 191 | * @return int |
| 230 | 192 | */ |
| 231 | 193 | public static function get_count( $table, $where = array(), $args = array() ) { |
| 232 | 194 | $count = self::get_var( $table, $where, 'COUNT(*)', $args ); |
| 195 | + | |
| 233 | 196 | return (int) $count; |
| 234 | 197 | } |
| 235 | 198 | |
| 236 | 199 | /** |
| @@ -246,17 +209,18 @@ | ||
| 246 | 209 | public static function get_var( $table, $where = array(), $field = 'id', $args = array(), $limit = '', $type = 'var' ) { |
| 247 | 210 | $group = ''; |
| 248 | 211 | self::get_group_and_table_name( $table, $group ); |
| 249 | 212 | self::convert_options_to_array( $args, '', $limit ); |
| 250 | - | |
| 251 | 213 | if ( $type === 'var' && ! isset( $args['limit'] ) ) { |
| 252 | 214 | $args['limit'] = 1; |
| 253 | 215 | } |
| 254 | 216 | |
| 255 | - $query = self::generate_query_string_from_pieces( $field, $table, $where, $args ); | |
| 217 | + $query = self::generate_query_string_from_pieces( $field, $table, $where, $args ); | |
| 218 | + | |
| 256 | 219 | $cache_key = self::generate_cache_key( $where, $args, $field, $type ); |
| 220 | + $results = self::check_cache( $cache_key, $group, $query, 'get_' . $type ); | |
| 257 | 221 | |
| 258 | - return self::check_cache( $cache_key, $group, $query, 'get_' . $type ); | |
| 222 | + return $results; | |
| 259 | 223 | } |
| 260 | 224 | |
| 261 | 225 | /** |
| 262 | 226 | * Generate a cache key from the where query, field, type, and other arguments |
| @@ -272,16 +236,15 @@ | ||
| 272 | 236 | */ |
| 273 | 237 | public static function generate_cache_key( $where, $args, $field, $type ) { |
| 274 | 238 | $cache_key = ''; |
| 275 | 239 | $where = FrmAppHelper::array_flatten( $where ); |
| 276 | - | |
| 277 | 240 | foreach ( $where as $key => $value ) { |
| 278 | 241 | $cache_key .= $key . '_' . $value; |
| 279 | 242 | } |
| 280 | - | |
| 281 | 243 | $cache_key .= implode( '_', $args ) . $field . '_' . $type; |
| 244 | + $cache_key = str_replace( array( ' ', ',' ), '_', $cache_key ); | |
| 282 | 245 | |
| 283 | - return str_replace( array( ' ', ',' ), '_', $cache_key ); | |
| 246 | + return $cache_key; | |
| 284 | 247 | } |
| 285 | 248 | |
| 286 | 249 | /** |
| 287 | 250 | * @param string $table |
| @@ -349,9 +312,8 @@ | ||
| 349 | 312 | '%like' => '%like', |
| 350 | 313 | ); |
| 351 | 314 | |
| 352 | 315 | $where_is = strtolower( $where_is ); |
| 353 | - | |
| 354 | 316 | if ( isset( $switch_to[ $where_is ] ) ) { |
| 355 | 317 | return ' ' . $switch_to[ $where_is ]; |
| 356 | 318 | } |
| 357 | 319 | |
| @@ -370,26 +332,24 @@ | ||
| 370 | 332 | * Also add the wpdb->prefix to the table if it's missing |
| 371 | 333 | * |
| 372 | 334 | * @param string $table |
| 373 | 335 | * @param string $group |
| 374 | - * | |
| 375 | - * @return void | |
| 376 | 336 | */ |
| 377 | 337 | private static function get_group_and_table_name( &$table, &$group ) { |
| 378 | - global $wpdb; | |
| 338 | + global $wpdb, $wpmuBaseTablePrefix; | |
| 379 | 339 | |
| 380 | 340 | $table_parts = explode( ' ', $table ); |
| 381 | 341 | $group = reset( $table_parts ); |
| 382 | 342 | self::maybe_remove_prefix( $wpdb->prefix, $group ); |
| 383 | 343 | |
| 384 | - $prefix = $wpdb->base_prefix; | |
| 344 | + $prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix : $wpdb->base_prefix; | |
| 385 | 345 | self::maybe_remove_prefix( $prefix, $group ); |
| 386 | 346 | |
| 387 | - if ( $group === $table ) { | |
| 347 | + if ( $group == $table ) { | |
| 388 | 348 | $table = $wpdb->prefix . $table; |
| 389 | 349 | } |
| 390 | 350 | |
| 391 | - // Switch to singular group name | |
| 351 | + // switch to singular group name | |
| 392 | 352 | $group = rtrim( $group, 's' ); |
| 393 | 353 | } |
| 394 | 354 | |
| 395 | 355 | /** |
| @@ -395,44 +355,30 @@ | ||
| 395 | 355 | /** |
| 396 | 356 | * Only remove the db prefix when at the beginning. |
| 397 | 357 | * |
| 398 | 358 | * @since 4.04.02 |
| 399 | - * | |
| 400 | - * @param string $prefix Prefix to remove. | |
| 401 | - * @param string $name Name to strip prefix from, passed by reference. | |
| 402 | - * | |
| 403 | - * @return void | |
| 404 | 359 | */ |
| 405 | 360 | private static function maybe_remove_prefix( $prefix, &$name ) { |
| 406 | - if ( str_starts_with( $name, $prefix ) ) { | |
| 361 | + if ( substr( $name, 0, strlen( $prefix ) ) === $prefix ) { | |
| 407 | 362 | $name = substr( $name, strlen( $prefix ) ); |
| 408 | 363 | } |
| 409 | 364 | } |
| 410 | 365 | |
| 411 | - /** | |
| 412 | - * @param array|string $args | |
| 413 | - * @param string $order_by | |
| 414 | - * @param int|string $limit | |
| 415 | - * | |
| 416 | - * @return void | |
| 417 | - */ | |
| 418 | 366 | private static function convert_options_to_array( &$args, $order_by = '', $limit = '' ) { |
| 419 | 367 | if ( ! is_array( $args ) ) { |
| 420 | 368 | $args = array( 'order_by' => $args ); |
| 421 | 369 | } |
| 422 | 370 | |
| 423 | - if ( $order_by ) { | |
| 371 | + if ( ! empty( $order_by ) ) { | |
| 424 | 372 | $args['order_by'] = $order_by; |
| 425 | 373 | } |
| 426 | 374 | |
| 427 | - if ( $limit ) { | |
| 375 | + if ( ! empty( $limit ) ) { | |
| 428 | 376 | $args['limit'] = $limit; |
| 429 | 377 | } |
| 430 | 378 | |
| 431 | 379 | $temp_args = $args; |
| 432 | - | |
| 433 | 380 | foreach ( $temp_args as $k => $v ) { |
| 434 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 435 | 381 | if ( $v == '' ) { |
| 436 | 382 | unset( $args[ $k ] ); |
| 437 | 383 | continue; |
| 438 | 384 | } |
| @@ -437,22 +383,19 @@ | ||
| 437 | 383 | continue; |
| 438 | 384 | } |
| 439 | 385 | |
| 440 | 386 | $db_name = strtoupper( str_replace( '_', ' ', $k ) ); |
| 441 | - | |
| 442 | - if ( ! str_contains( $v, $db_name ) ) { | |
| 387 | + if ( strpos( $v, $db_name ) === false ) { | |
| 443 | 388 | $args[ $k ] = $db_name . ' ' . $v; |
| 444 | 389 | } |
| 445 | 390 | } |
| 446 | 391 | |
| 447 | 392 | // Make sure LIMIT is the last argument |
| 448 | - if ( ! isset( $args['order_by'] ) || ! isset( $args['limit'] ) ) { | |
| 449 | - return; | |
| 393 | + if ( isset( $args['order_by'] ) && isset( $args['limit'] ) ) { | |
| 394 | + $temp_limit = $args['limit']; | |
| 395 | + unset( $args['limit'] ); | |
| 396 | + $args['limit'] = $temp_limit; | |
| 450 | 397 | } |
| 451 | - | |
| 452 | - $temp_limit = $args['limit']; | |
| 453 | - unset( $args['limit'] ); | |
| 454 | - $args['limit'] = $temp_limit; | |
| 455 | 398 | } |
| 456 | 399 | |
| 457 | 400 | /** |
| 458 | 401 | * Get the associative array results for the given columns, table, and where query |
| @@ -468,12 +411,14 @@ | ||
| 468 | 411 | public static function get_associative_array_results( $columns, $table, $where ) { |
| 469 | 412 | $group = ''; |
| 470 | 413 | self::get_group_and_table_name( $table, $group ); |
| 471 | 414 | |
| 472 | - $query = self::generate_query_string_from_pieces( $columns, $table, $where ); | |
| 415 | + $query = self::generate_query_string_from_pieces( $columns, $table, $where ); | |
| 416 | + | |
| 473 | 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' ); | |
| 474 | 419 | |
| 475 | - return self::check_cache( $cache_key, $group, $query, 'get_associative_results' ); | |
| 420 | + return $results; | |
| 476 | 421 | } |
| 477 | 422 | |
| 478 | 423 | /** |
| 479 | 424 | * Combine the pieces of a query to form a full, prepared query |
| @@ -491,25 +436,19 @@ | ||
| 491 | 436 | $query = 'SELECT ' . $columns . ' FROM ' . $table; |
| 492 | 437 | |
| 493 | 438 | self::esc_query_args( $args ); |
| 494 | 439 | |
| 495 | - if ( ! is_array( $where ) && $where ) { | |
| 496 | - return $query; | |
| 440 | + if ( is_array( $where ) || empty( $where ) ) { | |
| 441 | + self::get_where_clause_and_values( $where ); | |
| 442 | + global $wpdb; | |
| 443 | + $query = $wpdb->prepare( $query . $where['where'] . ' ' . implode( ' ', $args ), $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 497 | 444 | } |
| 498 | 445 | |
| 499 | - self::get_where_clause_and_values( $where ); | |
| 500 | - global $wpdb; | |
| 501 | - | |
| 502 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 503 | - return $wpdb->prepare( $query . $where['where'] . ' ' . implode( ' ', $args ), $where['values'] ); | |
| 446 | + return $query; | |
| 504 | 447 | } |
| 505 | 448 | |
| 506 | 449 | /** |
| 507 | 450 | * @since 2.05.07 |
| 508 | - * | |
| 509 | - * @param array $args Query arguments, passed by reference. | |
| 510 | - * | |
| 511 | - * @return void | |
| 512 | 451 | */ |
| 513 | 452 | private static function esc_query_args( &$args ) { |
| 514 | 453 | foreach ( $args as $param => $value ) { |
| 515 | 454 | if ( $param === 'order_by' ) { |
| @@ -517,9 +456,8 @@ | ||
| 517 | 456 | } elseif ( $param === 'limit' ) { |
| 518 | 457 | $args[ $param ] = self::esc_limit( $value ); |
| 519 | 458 | } |
| 520 | 459 | |
| 521 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 522 | 460 | if ( $args[ $param ] == '' ) { |
| 523 | 461 | unset( $args[ $param ] ); |
| 524 | 462 | } |
| 525 | 463 | } |
| @@ -535,8 +473,9 @@ | ||
| 535 | 473 | * @return string The escaped value |
| 536 | 474 | */ |
| 537 | 475 | public static function esc_like( $term ) { |
| 538 | 476 | global $wpdb; |
| 477 | + | |
| 539 | 478 | return $wpdb->esc_like( $term ); |
| 540 | 479 | } |
| 541 | 480 | |
| 542 | 481 | /** |
| @@ -542,33 +481,29 @@ | ||
| 542 | 481 | /** |
| 543 | 482 | * @since 2.05.06 |
| 544 | 483 | * |
| 545 | 484 | * @param string $order_query |
| 546 | - * | |
| 547 | - * @return string | |
| 548 | 485 | */ |
| 549 | 486 | public static function esc_order( $order_query ) { |
| 550 | - if ( ! $order_query ) { | |
| 487 | + if ( empty( $order_query ) ) { | |
| 551 | 488 | return ''; |
| 552 | 489 | } |
| 553 | 490 | |
| 554 | 491 | // Remove ORDER BY before sanitizing. |
| 555 | 492 | $order_query = strtolower( $order_query ); |
| 556 | - | |
| 557 | - if ( str_contains( $order_query, 'order by' ) ) { | |
| 493 | + if ( strpos( $order_query, 'order by' ) !== false ) { | |
| 558 | 494 | $order_query = str_replace( 'order by', '', $order_query ); |
| 559 | 495 | } |
| 560 | 496 | |
| 561 | 497 | $order_query = explode( ' ', trim( $order_query ) ); |
| 562 | - $order = trim( reset( $order_query ) ); | |
| 563 | - $safe_order = array( 'count(*)' ); | |
| 564 | 498 | |
| 565 | - if ( ! in_array( strtolower( $order ), $safe_order, true ) ) { | |
| 499 | + $order = trim( reset( $order_query ) ); | |
| 500 | + $safe_order = array( 'count(*)' ); | |
| 501 | + if ( ! in_array( strtolower( $order ), $safe_order ) ) { | |
| 566 | 502 | $order = preg_replace( '/[^a-zA-Z0-9\-\_\.\+]/', '', $order ); |
| 567 | 503 | } |
| 568 | 504 | |
| 569 | 505 | $order_by = ''; |
| 570 | - | |
| 571 | 506 | if ( count( $order_query ) > 1 ) { |
| 572 | 507 | $order_by = end( $order_query ); |
| 573 | 508 | self::esc_order_by( $order_by ); |
| 574 | 509 | } |
| @@ -579,16 +514,11 @@ | ||
| 579 | 514 | /** |
| 580 | 515 | * Make sure this is ordering by either ASC or DESC |
| 581 | 516 | * |
| 582 | 517 | * @since 2.05.06 |
| 583 | - * | |
| 584 | - * @param string $order_by Sort direction, passed by reference. | |
| 585 | - * | |
| 586 | - * @return void | |
| 587 | 518 | */ |
| 588 | 519 | public static function esc_order_by( &$order_by ) { |
| 589 | 520 | $sort_options = array( 'asc', 'desc' ); |
| 590 | - | |
| 591 | 521 | if ( ! in_array( strtolower( $order_by ), $sort_options, true ) ) { |
| 592 | 522 | $order_by = 'asc'; |
| 593 | 523 | } |
| 594 | 524 | } |
| @@ -594,26 +524,21 @@ | ||
| 594 | 524 | } |
| 595 | 525 | |
| 596 | 526 | /** |
| 597 | 527 | * @since 2.05.06 |
| 598 | - * | |
| 599 | 528 | * @param string $limit |
| 600 | - * | |
| 601 | - * @return string | |
| 602 | 529 | */ |
| 603 | 530 | public static function esc_limit( $limit ) { |
| 604 | - if ( ! $limit ) { | |
| 531 | + if ( empty( $limit ) ) { | |
| 605 | 532 | return ''; |
| 606 | 533 | } |
| 607 | 534 | |
| 608 | 535 | $limit = trim( str_replace( 'limit ', '', strtolower( $limit ) ) ); |
| 609 | - | |
| 610 | 536 | if ( is_numeric( $limit ) ) { |
| 611 | 537 | return ' LIMIT ' . $limit; |
| 612 | 538 | } |
| 613 | 539 | |
| 614 | 540 | $limit = explode( ',', trim( $limit ) ); |
| 615 | - | |
| 616 | 541 | foreach ( $limit as $k => $l ) { |
| 617 | 542 | if ( is_numeric( $l ) ) { |
| 618 | 543 | $limit[ $k ] = $l; |
| 619 | 544 | } |
| @@ -627,16 +552,12 @@ | ||
| 627 | 552 | /** |
| 628 | 553 | * Get an array of values ready to go through $wpdb->prepare |
| 629 | 554 | * |
| 630 | 555 | * @since 2.05.06 |
| 631 | - * | |
| 632 | - * @param array $array Array of values. | |
| 633 | - * @param string $type Placeholder type. | |
| 634 | - * | |
| 635 | - * @return string | |
| 636 | 556 | */ |
| 637 | 557 | public static function prepare_array_values( $array, $type = '%s' ) { |
| 638 | 558 | $placeholders = array_fill( 0, count( $array ), $type ); |
| 559 | + | |
| 639 | 560 | return implode( ', ', $placeholders ); |
| 640 | 561 | } |
| 641 | 562 | |
| 642 | 563 | /** |
| @@ -643,18 +564,17 @@ | ||
| 643 | 564 | * @since 2.05.06 |
| 644 | 565 | * |
| 645 | 566 | * @param string $starts_with |
| 646 | 567 | * @param array|string $where |
| 647 | - * | |
| 648 | 568 | * @return string |
| 649 | 569 | */ |
| 650 | 570 | public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) { |
| 651 | - if ( ! $where ) { | |
| 571 | + if ( empty( $where ) ) { | |
| 652 | 572 | $where = ''; |
| 653 | 573 | } elseif ( is_array( $where ) ) { |
| 654 | - global $wpdb; | |
| 655 | - self::get_where_clause_and_values( $where, $starts_with ); | |
| 656 | - $where = $wpdb->prepare( $where['where'], $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 574 | + global $wpdb; | |
| 575 | + self::get_where_clause_and_values( $where, $starts_with ); | |
| 576 | + $where = $wpdb->prepare( $where['where'], $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 657 | 577 | } else { |
| 658 | 578 | $where = $starts_with . $where; |
| 659 | 579 | } |
| 660 | 580 | |
| @@ -672,12 +592,10 @@ | ||
| 672 | 592 | /** |
| 673 | 593 | * Prepare and save settings in styles and actions |
| 674 | 594 | * |
| 675 | 595 | * @since 2.05.06 |
| 676 | - * | |
| 677 | - * @param array|object $settings | |
| 678 | - * @param string $group | |
| 679 | - * | |
| 596 | + * @param array $settings | |
| 597 | + * @param string $group | |
| 680 | 598 | * @return int|WP_Error |
| 681 | 599 | */ |
| 682 | 600 | public static function save_settings( $settings, $group ) { |
| 683 | 601 | $settings = (array) $settings; |
| @@ -686,9 +604,9 @@ | ||
| 686 | 604 | if ( empty( $settings['ID'] ) ) { |
| 687 | 605 | unset( $settings['ID'] ); |
| 688 | 606 | } |
| 689 | 607 | |
| 690 | - // Delete all caches for this group | |
| 608 | + // delete all caches for this group | |
| 691 | 609 | self::cache_delete_group( $group ); |
| 692 | 610 | |
| 693 | 611 | return self::save_json_post( $settings ); |
| 694 | 612 | } |
| @@ -702,14 +620,12 @@ | ||
| 702 | 620 | * |
| 703 | 621 | * @since 2.05.06 |
| 704 | 622 | * |
| 705 | 623 | * @param array $settings |
| 706 | - * | |
| 707 | 624 | * @return int|WP_Error |
| 708 | 625 | */ |
| 709 | 626 | public static function save_json_post( $settings ) { |
| 710 | 627 | global $wp_filter; |
| 711 | - | |
| 712 | 628 | if ( isset( $wp_filter['content_save_pre'] ) ) { |
| 713 | 629 | $filters = $wp_filter['content_save_pre']; |
| 714 | 630 | } |
| 715 | 631 | |
| @@ -717,9 +633,9 @@ | ||
| 717 | 633 | remove_all_filters( 'content_save_pre' ); |
| 718 | 634 | |
| 719 | 635 | $post = wp_insert_post( $settings ); |
| 720 | 636 | |
| 721 | - // Add the content filters back for views or posts | |
| 637 | + // add the content filters back for views or posts | |
| 722 | 638 | if ( isset( $filters ) ) { |
| 723 | 639 | $wp_filter['content_save_pre'] = $filters; |
| 724 | 640 | } |
| 725 | 641 | |
| @@ -734,9 +650,8 @@ | ||
| 734 | 650 | * @param string $cache_key The unique name for this cache. |
| 735 | 651 | * @param string $group The name of the cache group. |
| 736 | 652 | * @param string $query If blank, don't run a db call. |
| 737 | 653 | * @param string $type The wpdb function to use with this query. |
| 738 | - * @param int $time Cache expiration time in seconds. | |
| 739 | 654 | * |
| 740 | 655 | * @return mixed $results The cache or query results |
| 741 | 656 | */ |
| 742 | 657 | public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) { |
| @@ -742,13 +657,13 @@ | ||
| 742 | 657 | public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) { |
| 743 | 658 | $found = null; |
| 744 | 659 | $results = wp_cache_get( $cache_key, $group, false, $found ); |
| 745 | 660 | |
| 746 | - if ( ( $found === true && $results !== false ) || ! $query ) { | |
| 661 | + if ( ( $found === true && $results !== false ) || empty( $query ) ) { | |
| 747 | 662 | return $results; |
| 748 | 663 | } |
| 749 | 664 | |
| 750 | - if ( 'get_posts' === $type ) { | |
| 665 | + if ( 'get_posts' == $type ) { | |
| 751 | 666 | $results = get_posts( $query ); |
| 752 | 667 | } elseif ( 'get_associative_results' === $type ) { |
| 753 | 668 | global $wpdb; |
| 754 | 669 | $results = $wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| @@ -763,15 +678,8 @@ | ||
| 763 | 678 | } |
| 764 | 679 | |
| 765 | 680 | /** |
| 766 | 681 | * @since 2.05.06 |
| 767 | - * | |
| 768 | - * @param string $cache_key The unique name for this cache. | |
| 769 | - * @param mixed $results Cached results. | |
| 770 | - * @param string $group The name of the cache group. | |
| 771 | - * @param int $time Cache expiration time in seconds. | |
| 772 | - * | |
| 773 | - * @return void | |
| 774 | 682 | */ |
| 775 | 683 | public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) { |
| 776 | 684 | if ( ! FrmAppHelper::prevent_caching() ) { |
| 777 | 685 | self::add_key_to_group_cache( $cache_key, $group ); |
| @@ -783,13 +691,8 @@ | ||
| 783 | 691 | * Keep track of the keys cached in each group so they can be deleted |
| 784 | 692 | * in Redis and Memcache |
| 785 | 693 | * |
| 786 | 694 | * @since 2.05.06 |
| 787 | - * | |
| 788 | - * @param string $key Cache key. | |
| 789 | - * @param string $group Cache group name. | |
| 790 | - * | |
| 791 | - * @return void | |
| 792 | 695 | */ |
| 793 | 696 | public static function add_key_to_group_cache( $key, $group ) { |
| 794 | 697 | $cached = self::get_group_cached_keys( $group ); |
| 795 | 698 | $cached[ $key ] = $key; |
| @@ -797,18 +700,13 @@ | ||
| 797 | 700 | } |
| 798 | 701 | |
| 799 | 702 | /** |
| 800 | 703 | * @since 2.05.06 |
| 801 | - * | |
| 802 | - * @param string $group Cache group name. | |
| 803 | - * | |
| 804 | - * @return array | |
| 805 | 704 | */ |
| 806 | 705 | public static function get_group_cached_keys( $group ) { |
| 807 | 706 | $cached = wp_cache_get( 'cached_keys', $group ); |
| 808 | - | |
| 809 | 707 | if ( ! $cached || ! is_array( $cached ) ) { |
| 810 | - return array(); | |
| 708 | + $cached = array(); | |
| 811 | 709 | } |
| 812 | 710 | |
| 813 | 711 | return $cached; |
| 814 | 712 | } |
| @@ -815,12 +713,9 @@ | ||
| 815 | 713 | |
| 816 | 714 | /** |
| 817 | 715 | * @since 2.05.06 |
| 818 | 716 | * |
| 819 | - * @param string $cache_key Cache key to delete. | |
| 820 | - * @param string $group Cache group name. | |
| 821 | - * | |
| 822 | - * @return void | |
| 717 | + * @param string $cache_key | |
| 823 | 718 | */ |
| 824 | 719 | public static function delete_cache_and_transient( $cache_key, $group = 'default' ) { |
| 825 | 720 | delete_transient( $cache_key ); |
| 826 | 721 | wp_cache_delete( $cache_key, $group ); |
| @@ -831,23 +726,19 @@ | ||
| 831 | 726 | * |
| 832 | 727 | * @since 2.05.06 |
| 833 | 728 | * |
| 834 | 729 | * @param string $group The name of the cache group. |
| 835 | - * | |
| 836 | - * @return void | |
| 837 | 730 | */ |
| 838 | 731 | public static function cache_delete_group( $group ) { |
| 839 | 732 | $cached_keys = self::get_group_cached_keys( $group ); |
| 840 | 733 | |
| 841 | - if ( ! $cached_keys ) { | |
| 842 | - return; | |
| 843 | - } | |
| 734 | + if ( ! empty( $cached_keys ) ) { | |
| 735 | + foreach ( $cached_keys as $key ) { | |
| 736 | + wp_cache_delete( $key, $group ); | |
| 737 | + } | |
| 844 | 738 | |
| 845 | - foreach ( $cached_keys as $key ) { | |
| 846 | - wp_cache_delete( $key, $group ); | |
| 739 | + wp_cache_delete( 'cached_keys', $group ); | |
| 847 | 740 | } |
| 848 | - | |
| 849 | - wp_cache_delete( 'cached_keys', $group ); | |
| 850 | 741 | } |
| 851 | 742 | |
| 852 | 743 | /** |
| 853 | 744 | * Checks if a DB column exists. |
| @@ -855,9 +746,8 @@ | ||
| 855 | 746 | * @since 6.7 |
| 856 | 747 | * |
| 857 | 748 | * @param string $table Table name without `$wpdb->prefix`. |
| 858 | 749 | * @param string $column Column name. |
| 859 | - * | |
| 860 | 750 | * @return bool |
| 861 | 751 | */ |
| 862 | 752 | public static function db_column_exists( $table, $column ) { |
| 863 | 753 | global $wpdb; |