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