| @@ -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 |
| @@ -289,9 +252,9 @@ | ||
| 289 | 252 | * @param string $field |
| 290 | 253 | * @param array $args |
| 291 | 254 | * @param string $limit |
| 292 | 255 | * |
| 293 | - * @return array | |
| 256 | + * @return mixed | |
| 294 | 257 | */ |
| 295 | 258 | public static function get_col( $table, $where = array(), $field = 'id', $args = array(), $limit = '' ) { |
| 296 | 259 | return self::get_var( $table, $where, $field, $args, $limit, 'col' ); |
| 297 | 260 | } |
| @@ -307,8 +270,9 @@ | ||
| 307 | 270 | * @return mixed |
| 308 | 271 | */ |
| 309 | 272 | public static function get_row( $table, $where = array(), $fields = '*', $args = array() ) { |
| 310 | 273 | $args['limit'] = 1; |
| 274 | + | |
| 311 | 275 | return self::get_var( $table, $where, $fields, $args, '', 'row' ); |
| 312 | 276 | } |
| 313 | 277 | |
| 314 | 278 | /** |
| @@ -320,9 +284,9 @@ | ||
| 320 | 284 | * @param array $where |
| 321 | 285 | * @param string $fields |
| 322 | 286 | * @param array $args |
| 323 | 287 | * |
| 324 | - * @return array | |
| 288 | + * @return mixed | |
| 325 | 289 | */ |
| 326 | 290 | public static function get_results( $table, $where = array(), $fields = '*', $args = array() ) { |
| 327 | 291 | return self::get_var( $table, $where, $fields, $args, '', 'results' ); |
| 328 | 292 | } |
| @@ -349,9 +313,8 @@ | ||
| 349 | 313 | '%like' => '%like', |
| 350 | 314 | ); |
| 351 | 315 | |
| 352 | 316 | $where_is = strtolower( $where_is ); |
| 353 | - | |
| 354 | 317 | if ( isset( $switch_to[ $where_is ] ) ) { |
| 355 | 318 | return ' ' . $switch_to[ $where_is ]; |
| 356 | 319 | } |
| 357 | 320 | |
| @@ -370,26 +333,24 @@ | ||
| 370 | 333 | * Also add the wpdb->prefix to the table if it's missing |
| 371 | 334 | * |
| 372 | 335 | * @param string $table |
| 373 | 336 | * @param string $group |
| 374 | - * | |
| 375 | - * @return void | |
| 376 | 337 | */ |
| 377 | 338 | private static function get_group_and_table_name( &$table, &$group ) { |
| 378 | - global $wpdb; | |
| 339 | + global $wpdb, $wpmuBaseTablePrefix; | |
| 379 | 340 | |
| 380 | 341 | $table_parts = explode( ' ', $table ); |
| 381 | 342 | $group = reset( $table_parts ); |
| 382 | 343 | self::maybe_remove_prefix( $wpdb->prefix, $group ); |
| 383 | 344 | |
| 384 | - $prefix = $wpdb->base_prefix; | |
| 345 | + $prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix : $wpdb->base_prefix; | |
| 385 | 346 | self::maybe_remove_prefix( $prefix, $group ); |
| 386 | 347 | |
| 387 | - if ( $group === $table ) { | |
| 348 | + if ( $group == $table ) { | |
| 388 | 349 | $table = $wpdb->prefix . $table; |
| 389 | 350 | } |
| 390 | 351 | |
| 391 | - // Switch to singular group name | |
| 352 | + // switch to singular group name | |
| 392 | 353 | $group = rtrim( $group, 's' ); |
| 393 | 354 | } |
| 394 | 355 | |
| 395 | 356 | /** |
| @@ -395,44 +356,30 @@ | ||
| 395 | 356 | /** |
| 396 | 357 | * Only remove the db prefix when at the beginning. |
| 397 | 358 | * |
| 398 | 359 | * @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 | 360 | */ |
| 405 | 361 | private static function maybe_remove_prefix( $prefix, &$name ) { |
| 406 | - if ( str_starts_with( $name, $prefix ) ) { | |
| 362 | + if ( substr( $name, 0, strlen( $prefix ) ) === $prefix ) { | |
| 407 | 363 | $name = substr( $name, strlen( $prefix ) ); |
| 408 | 364 | } |
| 409 | 365 | } |
| 410 | 366 | |
| 411 | - /** | |
| 412 | - * @param array|string $args | |
| 413 | - * @param string $order_by | |
| 414 | - * @param int|string $limit | |
| 415 | - * | |
| 416 | - * @return void | |
| 417 | - */ | |
| 418 | 367 | private static function convert_options_to_array( &$args, $order_by = '', $limit = '' ) { |
| 419 | 368 | if ( ! is_array( $args ) ) { |
| 420 | 369 | $args = array( 'order_by' => $args ); |
| 421 | 370 | } |
| 422 | 371 | |
| 423 | - if ( $order_by ) { | |
| 372 | + if ( ! empty( $order_by ) ) { | |
| 424 | 373 | $args['order_by'] = $order_by; |
| 425 | 374 | } |
| 426 | 375 | |
| 427 | - if ( $limit ) { | |
| 376 | + if ( ! empty( $limit ) ) { | |
| 428 | 377 | $args['limit'] = $limit; |
| 429 | 378 | } |
| 430 | 379 | |
| 431 | 380 | $temp_args = $args; |
| 432 | - | |
| 433 | 381 | foreach ( $temp_args as $k => $v ) { |
| 434 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 435 | 382 | if ( $v == '' ) { |
| 436 | 383 | unset( $args[ $k ] ); |
| 437 | 384 | continue; |
| 438 | 385 | } |
| @@ -437,22 +384,19 @@ | ||
| 437 | 384 | continue; |
| 438 | 385 | } |
| 439 | 386 | |
| 440 | 387 | $db_name = strtoupper( str_replace( '_', ' ', $k ) ); |
| 441 | - | |
| 442 | - if ( ! str_contains( $v, $db_name ) ) { | |
| 388 | + if ( strpos( $v, $db_name ) === false ) { | |
| 443 | 389 | $args[ $k ] = $db_name . ' ' . $v; |
| 444 | 390 | } |
| 445 | 391 | } |
| 446 | 392 | |
| 447 | 393 | // Make sure LIMIT is the last argument |
| 448 | - if ( ! isset( $args['order_by'] ) || ! isset( $args['limit'] ) ) { | |
| 449 | - return; | |
| 394 | + if ( isset( $args['order_by'] ) && isset( $args['limit'] ) ) { | |
| 395 | + $temp_limit = $args['limit']; | |
| 396 | + unset( $args['limit'] ); | |
| 397 | + $args['limit'] = $temp_limit; | |
| 450 | 398 | } |
| 451 | - | |
| 452 | - $temp_limit = $args['limit']; | |
| 453 | - unset( $args['limit'] ); | |
| 454 | - $args['limit'] = $temp_limit; | |
| 455 | 399 | } |
| 456 | 400 | |
| 457 | 401 | /** |
| 458 | 402 | * Get the associative array results for the given columns, table, and where query |
| @@ -468,12 +412,14 @@ | ||
| 468 | 412 | public static function get_associative_array_results( $columns, $table, $where ) { |
| 469 | 413 | $group = ''; |
| 470 | 414 | self::get_group_and_table_name( $table, $group ); |
| 471 | 415 | |
| 472 | - $query = self::generate_query_string_from_pieces( $columns, $table, $where ); | |
| 416 | + $query = self::generate_query_string_from_pieces( $columns, $table, $where ); | |
| 417 | + | |
| 473 | 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' ); | |
| 474 | 420 | |
| 475 | - return self::check_cache( $cache_key, $group, $query, 'get_associative_results' ); | |
| 421 | + return $results; | |
| 476 | 422 | } |
| 477 | 423 | |
| 478 | 424 | /** |
| 479 | 425 | * Combine the pieces of a query to form a full, prepared query |
| @@ -491,25 +437,26 @@ | ||
| 491 | 437 | $query = 'SELECT ' . $columns . ' FROM ' . $table; |
| 492 | 438 | |
| 493 | 439 | self::esc_query_args( $args ); |
| 494 | 440 | |
| 495 | - if ( ! is_array( $where ) && $where ) { | |
| 496 | - return $query; | |
| 441 | + if ( is_array( $where ) || empty( $where ) ) { | |
| 442 | + self::get_where_clause_and_values( $where ); | |
| 443 | + global $wpdb; | |
| 444 | + $query = $wpdb->prepare( $query . $where['where'] . ' ' . implode( ' ', $args ), $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 445 | + } else { | |
| 446 | + /** | |
| 447 | + * Allow the $where to be prepared before we recieve it here. | |
| 448 | + * This is a fallback for reverse compatibility, but is not recommended | |
| 449 | + */ | |
| 450 | + _deprecated_argument( 'where', '2.0', esc_html__( 'Use the query in an array format so it can be properly prepared.', 'formidable' ) ); | |
| 451 | + $query .= $where . ' ' . implode( ' ', $args ); | |
| 497 | 452 | } |
| 498 | 453 | |
| 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'] ); | |
| 454 | + return $query; | |
| 504 | 455 | } |
| 505 | 456 | |
| 506 | 457 | /** |
| 507 | 458 | * @since 2.05.07 |
| 508 | - * | |
| 509 | - * @param array $args Query arguments, passed by reference. | |
| 510 | - * | |
| 511 | - * @return void | |
| 512 | 459 | */ |
| 513 | 460 | private static function esc_query_args( &$args ) { |
| 514 | 461 | foreach ( $args as $param => $value ) { |
| 515 | 462 | if ( $param === 'order_by' ) { |
| @@ -517,9 +464,8 @@ | ||
| 517 | 464 | } elseif ( $param === 'limit' ) { |
| 518 | 465 | $args[ $param ] = self::esc_limit( $value ); |
| 519 | 466 | } |
| 520 | 467 | |
| 521 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 522 | 468 | if ( $args[ $param ] == '' ) { |
| 523 | 469 | unset( $args[ $param ] ); |
| 524 | 470 | } |
| 525 | 471 | } |
| @@ -525,9 +471,9 @@ | ||
| 525 | 471 | } |
| 526 | 472 | } |
| 527 | 473 | |
| 528 | 474 | /** |
| 529 | - * Added for < WP 4.0 compatibility | |
| 475 | + * Added for < WP 4.0 compatability | |
| 530 | 476 | * |
| 531 | 477 | * @since 2.05.06 |
| 532 | 478 | * |
| 533 | 479 | * @param string $term The value to escape. |
| @@ -535,8 +481,9 @@ | ||
| 535 | 481 | * @return string The escaped value |
| 536 | 482 | */ |
| 537 | 483 | public static function esc_like( $term ) { |
| 538 | 484 | global $wpdb; |
| 485 | + | |
| 539 | 486 | return $wpdb->esc_like( $term ); |
| 540 | 487 | } |
| 541 | 488 | |
| 542 | 489 | /** |
| @@ -542,33 +489,29 @@ | ||
| 542 | 489 | /** |
| 543 | 490 | * @since 2.05.06 |
| 544 | 491 | * |
| 545 | 492 | * @param string $order_query |
| 546 | - * | |
| 547 | - * @return string | |
| 548 | 493 | */ |
| 549 | 494 | public static function esc_order( $order_query ) { |
| 550 | - if ( ! $order_query ) { | |
| 495 | + if ( empty( $order_query ) ) { | |
| 551 | 496 | return ''; |
| 552 | 497 | } |
| 553 | 498 | |
| 554 | - // Remove ORDER BY before sanitizing. | |
| 499 | + // remove ORDER BY before santizing | |
| 555 | 500 | $order_query = strtolower( $order_query ); |
| 556 | - | |
| 557 | - if ( str_contains( $order_query, 'order by' ) ) { | |
| 501 | + if ( strpos( $order_query, 'order by' ) !== false ) { | |
| 558 | 502 | $order_query = str_replace( 'order by', '', $order_query ); |
| 559 | 503 | } |
| 560 | 504 | |
| 561 | 505 | $order_query = explode( ' ', trim( $order_query ) ); |
| 562 | - $order = trim( reset( $order_query ) ); | |
| 563 | - $safe_order = array( 'count(*)' ); | |
| 564 | 506 | |
| 565 | - if ( ! in_array( strtolower( $order ), $safe_order, true ) ) { | |
| 507 | + $order = trim( reset( $order_query ) ); | |
| 508 | + $safe_order = array( 'count(*)' ); | |
| 509 | + if ( ! in_array( strtolower( $order ), $safe_order ) ) { | |
| 566 | 510 | $order = preg_replace( '/[^a-zA-Z0-9\-\_\.\+]/', '', $order ); |
| 567 | 511 | } |
| 568 | 512 | |
| 569 | 513 | $order_by = ''; |
| 570 | - | |
| 571 | 514 | if ( count( $order_query ) > 1 ) { |
| 572 | 515 | $order_by = end( $order_query ); |
| 573 | 516 | self::esc_order_by( $order_by ); |
| 574 | 517 | } |
| @@ -579,16 +522,11 @@ | ||
| 579 | 522 | /** |
| 580 | 523 | * Make sure this is ordering by either ASC or DESC |
| 581 | 524 | * |
| 582 | 525 | * @since 2.05.06 |
| 583 | - * | |
| 584 | - * @param string $order_by Sort direction, passed by reference. | |
| 585 | - * | |
| 586 | - * @return void | |
| 587 | 526 | */ |
| 588 | 527 | public static function esc_order_by( &$order_by ) { |
| 589 | 528 | $sort_options = array( 'asc', 'desc' ); |
| 590 | - | |
| 591 | 529 | if ( ! in_array( strtolower( $order_by ), $sort_options, true ) ) { |
| 592 | 530 | $order_by = 'asc'; |
| 593 | 531 | } |
| 594 | 532 | } |
| @@ -594,26 +532,21 @@ | ||
| 594 | 532 | } |
| 595 | 533 | |
| 596 | 534 | /** |
| 597 | 535 | * @since 2.05.06 |
| 598 | - * | |
| 599 | 536 | * @param string $limit |
| 600 | - * | |
| 601 | - * @return string | |
| 602 | 537 | */ |
| 603 | 538 | public static function esc_limit( $limit ) { |
| 604 | - if ( ! $limit ) { | |
| 539 | + if ( empty( $limit ) ) { | |
| 605 | 540 | return ''; |
| 606 | 541 | } |
| 607 | 542 | |
| 608 | 543 | $limit = trim( str_replace( 'limit ', '', strtolower( $limit ) ) ); |
| 609 | - | |
| 610 | 544 | if ( is_numeric( $limit ) ) { |
| 611 | 545 | return ' LIMIT ' . $limit; |
| 612 | 546 | } |
| 613 | 547 | |
| 614 | 548 | $limit = explode( ',', trim( $limit ) ); |
| 615 | - | |
| 616 | 549 | foreach ( $limit as $k => $l ) { |
| 617 | 550 | if ( is_numeric( $l ) ) { |
| 618 | 551 | $limit[ $k ] = $l; |
| 619 | 552 | } |
| @@ -627,16 +560,12 @@ | ||
| 627 | 560 | /** |
| 628 | 561 | * Get an array of values ready to go through $wpdb->prepare |
| 629 | 562 | * |
| 630 | 563 | * @since 2.05.06 |
| 631 | - * | |
| 632 | - * @param array $array Array of values. | |
| 633 | - * @param string $type Placeholder type. | |
| 634 | - * | |
| 635 | - * @return string | |
| 636 | 564 | */ |
| 637 | 565 | public static function prepare_array_values( $array, $type = '%s' ) { |
| 638 | 566 | $placeholders = array_fill( 0, count( $array ), $type ); |
| 567 | + | |
| 639 | 568 | return implode( ', ', $placeholders ); |
| 640 | 569 | } |
| 641 | 570 | |
| 642 | 571 | /** |
| @@ -643,18 +572,17 @@ | ||
| 643 | 572 | * @since 2.05.06 |
| 644 | 573 | * |
| 645 | 574 | * @param string $starts_with |
| 646 | 575 | * @param array|string $where |
| 647 | - * | |
| 648 | 576 | * @return string |
| 649 | 577 | */ |
| 650 | 578 | public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) { |
| 651 | - if ( ! $where ) { | |
| 579 | + if ( empty( $where ) ) { | |
| 652 | 580 | $where = ''; |
| 653 | 581 | } 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 | |
| 582 | + global $wpdb; | |
| 583 | + self::get_where_clause_and_values( $where, $starts_with ); | |
| 584 | + $where = $wpdb->prepare( $where['where'], $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 657 | 585 | } else { |
| 658 | 586 | $where = $starts_with . $where; |
| 659 | 587 | } |
| 660 | 588 | |
| @@ -672,12 +600,10 @@ | ||
| 672 | 600 | /** |
| 673 | 601 | * Prepare and save settings in styles and actions |
| 674 | 602 | * |
| 675 | 603 | * @since 2.05.06 |
| 676 | - * | |
| 677 | - * @param array|object $settings | |
| 678 | - * @param string $group | |
| 679 | - * | |
| 604 | + * @param array $settings | |
| 605 | + * @param string $group | |
| 680 | 606 | * @return int|WP_Error |
| 681 | 607 | */ |
| 682 | 608 | public static function save_settings( $settings, $group ) { |
| 683 | 609 | $settings = (array) $settings; |
| @@ -686,9 +612,9 @@ | ||
| 686 | 612 | if ( empty( $settings['ID'] ) ) { |
| 687 | 613 | unset( $settings['ID'] ); |
| 688 | 614 | } |
| 689 | 615 | |
| 690 | - // Delete all caches for this group | |
| 616 | + // delete all caches for this group | |
| 691 | 617 | self::cache_delete_group( $group ); |
| 692 | 618 | |
| 693 | 619 | return self::save_json_post( $settings ); |
| 694 | 620 | } |
| @@ -702,14 +628,12 @@ | ||
| 702 | 628 | * |
| 703 | 629 | * @since 2.05.06 |
| 704 | 630 | * |
| 705 | 631 | * @param array $settings |
| 706 | - * | |
| 707 | 632 | * @return int|WP_Error |
| 708 | 633 | */ |
| 709 | 634 | public static function save_json_post( $settings ) { |
| 710 | 635 | global $wp_filter; |
| 711 | - | |
| 712 | 636 | if ( isset( $wp_filter['content_save_pre'] ) ) { |
| 713 | 637 | $filters = $wp_filter['content_save_pre']; |
| 714 | 638 | } |
| 715 | 639 | |
| @@ -717,9 +641,9 @@ | ||
| 717 | 641 | remove_all_filters( 'content_save_pre' ); |
| 718 | 642 | |
| 719 | 643 | $post = wp_insert_post( $settings ); |
| 720 | 644 | |
| 721 | - // Add the content filters back for views or posts | |
| 645 | + // add the content filters back for views or posts | |
| 722 | 646 | if ( isset( $filters ) ) { |
| 723 | 647 | $wp_filter['content_save_pre'] = $filters; |
| 724 | 648 | } |
| 725 | 649 | |
| @@ -734,23 +658,20 @@ | ||
| 734 | 658 | * @param string $cache_key The unique name for this cache. |
| 735 | 659 | * @param string $group The name of the cache group. |
| 736 | 660 | * @param string $query If blank, don't run a db call. |
| 737 | 661 | * @param string $type The wpdb function to use with this query. |
| 738 | - * @param int $time Cache expiration time in seconds. | |
| 739 | 662 | * |
| 740 | 663 | * @return mixed $results The cache or query results |
| 741 | 664 | */ |
| 742 | 665 | public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) { |
| 743 | - $found = null; | |
| 744 | - $results = wp_cache_get( $cache_key, $group, false, $found ); | |
| 745 | - | |
| 746 | - if ( ( $found === true && $results !== false ) || ! $query ) { | |
| 666 | + $results = wp_cache_get( $cache_key, $group ); | |
| 667 | + if ( ! FrmAppHelper::is_empty_value( $results, false ) || empty( $query ) ) { | |
| 747 | 668 | return $results; |
| 748 | 669 | } |
| 749 | 670 | |
| 750 | - if ( 'get_posts' === $type ) { | |
| 671 | + if ( 'get_posts' == $type ) { | |
| 751 | 672 | $results = get_posts( $query ); |
| 752 | - } elseif ( 'get_associative_results' === $type ) { | |
| 673 | + } elseif ( 'get_associative_results' == $type ) { | |
| 753 | 674 | global $wpdb; |
| 754 | 675 | $results = $wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 755 | 676 | } else { |
| 756 | 677 | global $wpdb; |
| @@ -763,15 +684,8 @@ | ||
| 763 | 684 | } |
| 764 | 685 | |
| 765 | 686 | /** |
| 766 | 687 | * @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 | 688 | */ |
| 775 | 689 | public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) { |
| 776 | 690 | if ( ! FrmAppHelper::prevent_caching() ) { |
| 777 | 691 | self::add_key_to_group_cache( $cache_key, $group ); |
| @@ -783,13 +697,8 @@ | ||
| 783 | 697 | * Keep track of the keys cached in each group so they can be deleted |
| 784 | 698 | * in Redis and Memcache |
| 785 | 699 | * |
| 786 | 700 | * @since 2.05.06 |
| 787 | - * | |
| 788 | - * @param string $key Cache key. | |
| 789 | - * @param string $group Cache group name. | |
| 790 | - * | |
| 791 | - * @return void | |
| 792 | 701 | */ |
| 793 | 702 | public static function add_key_to_group_cache( $key, $group ) { |
| 794 | 703 | $cached = self::get_group_cached_keys( $group ); |
| 795 | 704 | $cached[ $key ] = $key; |
| @@ -797,18 +706,13 @@ | ||
| 797 | 706 | } |
| 798 | 707 | |
| 799 | 708 | /** |
| 800 | 709 | * @since 2.05.06 |
| 801 | - * | |
| 802 | - * @param string $group Cache group name. | |
| 803 | - * | |
| 804 | - * @return array | |
| 805 | 710 | */ |
| 806 | 711 | public static function get_group_cached_keys( $group ) { |
| 807 | 712 | $cached = wp_cache_get( 'cached_keys', $group ); |
| 808 | - | |
| 809 | 713 | if ( ! $cached || ! is_array( $cached ) ) { |
| 810 | - return array(); | |
| 714 | + $cached = array(); | |
| 811 | 715 | } |
| 812 | 716 | |
| 813 | 717 | return $cached; |
| 814 | 718 | } |
| @@ -815,12 +719,9 @@ | ||
| 815 | 719 | |
| 816 | 720 | /** |
| 817 | 721 | * @since 2.05.06 |
| 818 | 722 | * |
| 819 | - * @param string $cache_key Cache key to delete. | |
| 820 | - * @param string $group Cache group name. | |
| 821 | - * | |
| 822 | - * @return void | |
| 723 | + * @param string $cache_key | |
| 823 | 724 | */ |
| 824 | 725 | public static function delete_cache_and_transient( $cache_key, $group = 'default' ) { |
| 825 | 726 | delete_transient( $cache_key ); |
| 826 | 727 | wp_cache_delete( $cache_key, $group ); |
| @@ -831,23 +732,19 @@ | ||
| 831 | 732 | * |
| 832 | 733 | * @since 2.05.06 |
| 833 | 734 | * |
| 834 | 735 | * @param string $group The name of the cache group. |
| 835 | - * | |
| 836 | - * @return void | |
| 837 | 736 | */ |
| 838 | 737 | public static function cache_delete_group( $group ) { |
| 839 | 738 | $cached_keys = self::get_group_cached_keys( $group ); |
| 840 | 739 | |
| 841 | - if ( ! $cached_keys ) { | |
| 842 | - return; | |
| 843 | - } | |
| 740 | + if ( ! empty( $cached_keys ) ) { | |
| 741 | + foreach ( $cached_keys as $key ) { | |
| 742 | + wp_cache_delete( $key, $group ); | |
| 743 | + } | |
| 844 | 744 | |
| 845 | - foreach ( $cached_keys as $key ) { | |
| 846 | - wp_cache_delete( $key, $group ); | |
| 745 | + wp_cache_delete( 'cached_keys', $group ); | |
| 847 | 746 | } |
| 848 | - | |
| 849 | - wp_cache_delete( 'cached_keys', $group ); | |
| 850 | 747 | } |
| 851 | 748 | |
| 852 | 749 | /** |
| 853 | 750 | * Checks if a DB column exists. |
| @@ -855,9 +752,8 @@ | ||
| 855 | 752 | * @since 6.7 |
| 856 | 753 | * |
| 857 | 754 | * @param string $table Table name without `$wpdb->prefix`. |
| 858 | 755 | * @param string $column Column name. |
| 859 | - * | |
| 860 | 756 | * @return bool |
| 861 | 757 | */ |
| 862 | 758 | public static function db_column_exists( $table, $column ) { |
| 863 | 759 | global $wpdb; |