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