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