| @@ -3,9 +3,9 @@ | ||
| 3 | 3 | * Query helper class contains static helper methods to perform basic |
| 4 | 4 | * operations |
| 5 | 5 | * |
| 6 | 6 | * @package Tutor\Helper |
| 7 | - * @since 2.0.7 | |
| 7 | + * @since v2.0.7 | |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | namespace Tutor\Helpers; |
| 11 | 11 | |
| @@ -17,34 +17,29 @@ | ||
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * Insert data in the table |
| 20 | 20 | * |
| 21 | - * @since 2.0.7 | |
| 22 | - * @since 3.2.0 sanitize_mapping param added to override sanitize function to specific keys. | |
| 23 | - * | |
| 24 | 21 | * @param string $table table name. |
| 25 | 22 | * @param array $data | data to insert in the table. |
| 26 | - * @param array $sanitize_mapping sanitize mapping. | |
| 27 | 23 | * |
| 28 | - * @return int inserted id. | |
| 24 | + * @return int, inserted id. | |
| 29 | 25 | * |
| 30 | - * @throws \Exception Database error if occur. | |
| 26 | + * @since v2.0.7 | |
| 31 | 27 | */ |
| 32 | - public static function insert( string $table, array $data, array $sanitize_mapping = array() ): int { | |
| 28 | + public static function insert( string $table, array $data ): int { | |
| 33 | 29 | global $wpdb; |
| 30 | + // Sanitize text field. | |
| 31 | + $data = array_map( | |
| 32 | + function( $value ) { | |
| 33 | + return sanitize_text_field( $value ); | |
| 34 | + }, | |
| 35 | + $data | |
| 36 | + ); | |
| 34 | 37 | |
| 35 | - $table = self::prepare_table_name( $table ); | |
| 36 | - $data = \TUTOR\Input::sanitize_array( $data, $sanitize_mapping ); | |
| 37 | - | |
| 38 | 38 | $insert = $wpdb->insert( |
| 39 | 39 | $table, |
| 40 | 40 | $data |
| 41 | 41 | ); |
| 42 | - | |
| 43 | - if ( $wpdb->last_error ) { | |
| 44 | - throw new \Exception( $wpdb->last_error ); | |
| 45 | - } | |
| 46 | - | |
| 47 | 42 | return $insert ? $wpdb->insert_id : 0; |
| 48 | 43 | } |
| 49 | 44 | |
| 50 | 45 | /** |
| @@ -49,41 +44,43 @@ | ||
| 49 | 44 | |
| 50 | 45 | /** |
| 51 | 46 | * Update data |
| 52 | 47 | * |
| 53 | - * @since 2.0.7 | |
| 54 | - * @since 3.2.0 IN clause support added. | |
| 55 | - * | |
| 56 | 48 | * @param string $table table name. |
| 57 | 49 | * @param array $data | data to update in the table. |
| 58 | 50 | * @param array $where | condition array. |
| 59 | 51 | * |
| 60 | - * @return bool true on success false on failure | |
| 52 | + * @return bool, true on success false on failure | |
| 53 | + * | |
| 54 | + * @since v2.0.7 | |
| 61 | 55 | */ |
| 62 | 56 | public static function update( string $table, array $data, array $where ): bool { |
| 63 | 57 | global $wpdb; |
| 58 | + // Sanitize text field. | |
| 59 | + $data = array_map( | |
| 60 | + function( $value ) { | |
| 61 | + return sanitize_text_field( $value ); | |
| 62 | + }, | |
| 63 | + $data | |
| 64 | + ); | |
| 64 | 65 | |
| 65 | - $table = self::prepare_table_name( $table ); | |
| 66 | - $set_clause = self::prepare_set_clause( $data ); | |
| 67 | - $where_clause = self::prepare_where_clause( $where ); | |
| 66 | + $where = array_map( | |
| 67 | + function( $value ) { | |
| 68 | + return sanitize_text_field( $value ); | |
| 69 | + }, | |
| 70 | + $where | |
| 71 | + ); | |
| 68 | 72 | |
| 69 | - // phpcs:ignore | |
| 70 | - $query = $wpdb->prepare( "UPDATE {$table} {$set_clause} WHERE {$where_clause} AND 1 = %d", 1 ); | |
| 71 | - | |
| 72 | - // phpcs:ignore | |
| 73 | - $wpdb->query( $query ); | |
| 74 | - | |
| 75 | - if ( $wpdb->last_error ) { | |
| 76 | - error_log( $wpdb->last_error ); | |
| 77 | - return false; | |
| 78 | - } | |
| 79 | - | |
| 80 | - return true; | |
| 73 | + $update = $wpdb->update( | |
| 74 | + $table, | |
| 75 | + $data, | |
| 76 | + $where | |
| 77 | + ); | |
| 78 | + return $update ? true : false; | |
| 81 | 79 | } |
| 82 | 80 | |
| 83 | 81 | /** |
| 84 | - * Delete a row from table with where clause. | |
| 85 | - * Limitation: It can only delete one row by wpdb::delete | |
| 82 | + * Delete rows from table | |
| 86 | 83 | * |
| 87 | 84 | * @param string $table table name. |
| 88 | 85 | * @param array $where key value pairs.Where key is the name of |
| 89 | 86 | * column & value is the value to match. |
| @@ -92,10 +89,8 @@ | ||
| 92 | 89 | * @since v2.0.7 |
| 93 | 90 | */ |
| 94 | 91 | public static function delete( string $table, array $where ): bool { |
| 95 | 92 | global $wpdb; |
| 96 | - | |
| 97 | - $table = self::prepare_table_name( $table ); | |
| 98 | 93 | $delete = $wpdb->delete( |
| 99 | 94 | $table, |
| 100 | 95 | $where |
| 101 | 96 | ); |
| @@ -102,56 +97,8 @@ | ||
| 102 | 97 | return $delete ? true : false; |
| 103 | 98 | } |
| 104 | 99 | |
| 105 | 100 | /** |
| 106 | - * Bulk record delete by where clause. | |
| 107 | - * | |
| 108 | - * @since 3.7.0 | |
| 109 | - * | |
| 110 | - * @param string $table table name. | |
| 111 | - * @param array $where where clause. | |
| 112 | - * | |
| 113 | - * @return int|boolean | |
| 114 | - */ | |
| 115 | - public static function bulk_delete( $table, array $where ): bool { | |
| 116 | - global $wpdb; | |
| 117 | - | |
| 118 | - $table = self::prepare_table_name( $table ); | |
| 119 | - $where_clause = self::prepare_where_clause( $where ); | |
| 120 | - | |
| 121 | - return $wpdb->query( "DELETE FROM {$table} WHERE {$where_clause}" ); //phpcs:ignore --$where clause sanitized. | |
| 122 | - } | |
| 123 | - | |
| 124 | - /** | |
| 125 | - * Delete rows from table | |
| 126 | - * | |
| 127 | - * @since 3.0.0 | |
| 128 | - * | |
| 129 | - * @param string $table table name. | |
| 130 | - * @param array $ids array of ids. | |
| 131 | - * | |
| 132 | - * @see prepare_in_clause | |
| 133 | - * | |
| 134 | - * @throws \Exception Throw database error if occurred. | |
| 135 | - * | |
| 136 | - * @return true on success | |
| 137 | - */ | |
| 138 | - public static function bulk_delete_by_ids( string $table, array $ids ): bool { | |
| 139 | - global $wpdb; | |
| 140 | - | |
| 141 | - $table = self::prepare_table_name( $table ); | |
| 142 | - $ids = self::prepare_in_clause( $ids ); | |
| 143 | - //phpcs:ignore --ids already sanitized. | |
| 144 | - $wpdb->query( "DELETE FROM {$table} WHERE id IN ( $ids )"); | |
| 145 | - | |
| 146 | - if ( $wpdb->last_error ) { | |
| 147 | - throw new \Exception( $wpdb->last_error ); | |
| 148 | - } | |
| 149 | - | |
| 150 | - return true; | |
| 151 | - } | |
| 152 | - | |
| 153 | - /** | |
| 154 | 101 | * Clean everything from table |
| 155 | 102 | * |
| 156 | 103 | * @since v2.0.7 |
| 157 | 104 | * |
| @@ -160,13 +107,16 @@ | ||
| 160 | 107 | * @return bool |
| 161 | 108 | */ |
| 162 | 109 | public static function table_clean( string $table ): bool { |
| 163 | 110 | global $wpdb; |
| 164 | - | |
| 165 | - $table = self::prepare_table_name( $table ); | |
| 166 | 111 | $delete = $wpdb->query( |
| 167 | - //phpcs:ignore | |
| 168 | - $wpdb->prepare( "DELETE FROM {$table} WHERE 1 = %d", 1 ) | |
| 112 | + $wpdb->prepare( | |
| 113 | + "DELETE FROM | |
| 114 | + {$table} | |
| 115 | + WHERE 1 = %d | |
| 116 | + ", | |
| 117 | + 1 | |
| 118 | + ) | |
| 169 | 119 | ); |
| 170 | 120 | return $delete ? true : false; |
| 171 | 121 | } |
| 172 | 122 | |
| @@ -173,29 +123,20 @@ | ||
| 173 | 123 | /** |
| 174 | 124 | * Insert multiple rows without knowing key value |
| 175 | 125 | * |
| 176 | 126 | * @since v2.0.7 |
| 177 | - * @since 3.6.0 param $return_ids added. | |
| 178 | 127 | * |
| 179 | 128 | * @param string $table table name. |
| 180 | 129 | * @param array $request two dimensional array |
| 181 | 130 | * for ex: [ [id => 1], [id => 2] ]. |
| 182 | - * @param bool $return_ids if true returns the last inserted data ids. | |
| 183 | - * @param bool $do_sanitize sanitize data or not. | |
| 184 | 131 | * |
| 185 | - * @return mixed wpdb response true or int on success, false on failure. | |
| 186 | - * @throws \Exception If error occur. | |
| 132 | + * @return mixed wpdb response true or int on success, | |
| 133 | + * false on failure | |
| 187 | 134 | */ |
| 188 | - public static function insert_multiple_rows( $table, $request, $return_ids = false, $do_sanitize = true ) { | |
| 135 | + public static function insert_multiple_rows( $table, $request ) { | |
| 189 | 136 | global $wpdb; |
| 190 | - | |
| 191 | - if ( ! tutor_utils()->is_multi_dimensional_array( $request ) ) { | |
| 192 | - return self::insert( $table, $request ); | |
| 193 | - } | |
| 194 | - | |
| 195 | - $table = self::prepare_table_name( $table ); | |
| 196 | 137 | $column_keys = ''; |
| 197 | - $column_values = array(); | |
| 138 | + $column_values = ''; | |
| 198 | 139 | $sql = ''; |
| 199 | 140 | $last_key = array_key_last( $request ); |
| 200 | 141 | $first_key = array_key_first( $request ); |
| 201 | 142 | foreach ( $request as $k => $value ) { |
| @@ -200,209 +141,45 @@ | ||
| 200 | 141 | $first_key = array_key_first( $request ); |
| 201 | 142 | foreach ( $request as $k => $value ) { |
| 202 | 143 | $keys = array_keys( $value ); |
| 203 | 144 | |
| 204 | - $value_placeholder = array(); | |
| 205 | - | |
| 206 | 145 | // Prepare column keys & values. |
| 207 | 146 | foreach ( $keys as $v ) { |
| 208 | 147 | $column_keys .= sanitize_key( $v ) . ','; |
| 209 | - $sanitize_value = $value[ $v ]; | |
| 210 | - if ( $sanitize_value && $do_sanitize ) { | |
| 211 | - $sanitize_value = sanitize_text_field( $sanitize_value ); | |
| 212 | - } | |
| 213 | - | |
| 214 | - $column_values[] = $sanitize_value; | |
| 215 | - | |
| 216 | - $value_placeholder[] = '%s'; | |
| 148 | + $sanitize_value = sanitize_text_field( $value[ $v ] ); | |
| 149 | + $column_values .= is_numeric( $sanitize_value ) ? $sanitize_value . ',' : "'$sanitize_value'" . ','; | |
| 217 | 150 | } |
| 218 | - | |
| 219 | - $value_placeholder = implode( ',', $value_placeholder ); | |
| 220 | - | |
| 221 | 151 | // Trim trailing comma. |
| 222 | 152 | $column_keys = rtrim( $column_keys, ',' ); |
| 223 | - $column_values = $wpdb->prepare( $value_placeholder, $column_values ); // Escape values. | |
| 224 | - | |
| 153 | + $column_values = rtrim( $column_values, ',' ); | |
| 225 | 154 | if ( $first_key === $k ) { |
| 226 | - $sql .= "INSERT INTO | |
| 227 | - {$table} | |
| 228 | - ($column_keys) VALUES ($column_values) | |
| 229 | - "; | |
| 230 | - | |
| 231 | - if ( count( $request ) > 1 ) { | |
| 232 | - $sql .= ','; | |
| 233 | - } | |
| 155 | + $sql .= "INSERT INTO {$table} ($column_keys) VALUES ($column_values),"; | |
| 234 | 156 | } elseif ( $last_key == $k ) { |
| 235 | - $sql .= "( $column_values )"; | |
| 157 | + $sql .= "($column_values)"; | |
| 236 | 158 | } else { |
| 237 | - $sql .= "( $column_values ),"; | |
| 238 | - | |
| 159 | + $sql .= "($column_values),"; | |
| 239 | 160 | } |
| 240 | 161 | |
| 241 | 162 | // Reset keys & values to avoid duplication. |
| 242 | 163 | $column_keys = ''; |
| 243 | - $column_values = array(); | |
| 164 | + $column_values = ''; | |
| 244 | 165 | } |
| 245 | - | |
| 246 | - $wpdb->query( $sql );//phpcs:ignore | |
| 247 | - | |
| 248 | - // If error occurred then throw new exception. | |
| 249 | - if ( $wpdb->last_error ) { | |
| 250 | - throw new \Exception( esc_html( $wpdb->last_error ) ); | |
| 251 | - } | |
| 252 | - | |
| 253 | - if ( $return_ids ) { | |
| 254 | - $query_ids = $wpdb->get_results( | |
| 255 | - //phpcs:ignore | |
| 256 | - "SELECT ID FROM {$table} WHERE ID >= LAST_INSERT_ID()", | |
| 257 | - 'ARRAY_N' | |
| 258 | - ); | |
| 259 | - | |
| 260 | - return $query_ids; | |
| 261 | - } | |
| 262 | - | |
| 263 | - return true; | |
| 166 | + return $wpdb->query( $sql ); | |
| 264 | 167 | } |
| 265 | 168 | |
| 266 | 169 | /** |
| 267 | - * Make tge where clause base on its column operator and values. | |
| 170 | + * Build where clause string | |
| 268 | 171 | * |
| 269 | - * If the operator is IN then make the clause like `WHERE column_name IN (value1, value2, ...)` | |
| 270 | - * Otherwise the clause would be `WHERE column_name = 'value'` | |
| 172 | + * @param array $where assoc array with field and value | |
| 173 | + * @return string | |
| 271 | 174 | * |
| 272 | - * @since 3.0.0 | |
| 273 | - * @since 3.9.7 added prepared statement for value. | |
| 274 | - * | |
| 275 | - * @param array $where The where clause array. e.g. array( 'id', 'IN', array(1, 2, 3) ) or array( 'id', '=', 1 ). | |
| 276 | - * | |
| 277 | - * @return string | |
| 278 | - */ | |
| 279 | - public static function make_clause( array $where ) { | |
| 280 | - list ( $field, $operator, $value ) = $where; | |
| 281 | - | |
| 282 | - $upper_operator = strtoupper( $operator ); | |
| 283 | - | |
| 284 | - if ( in_array( $upper_operator, array( 'IN', 'NOT IN' ), true ) ) { | |
| 285 | - $value = '(' . self::prepare_in_clause( $value ) . ')'; | |
| 286 | - } elseif ( in_array( $upper_operator, array( 'BETWEEN', 'NOT BETWEEN' ), true ) ) { | |
| 287 | - $value = array_map( fn( $val ) => self::prepare_value( $val ), $value ); | |
| 288 | - $value = implode( ' AND ', $value ); | |
| 289 | - } elseif ( strtoupper( $value ) === 'NULL' ) { | |
| 290 | - $value = 'NULL'; | |
| 291 | - } else { | |
| 292 | - $value = self::prepare_value( $value ); | |
| 293 | - } | |
| 294 | - | |
| 295 | - return "{$field} {$upper_operator} {$value}"; | |
| 296 | - } | |
| 297 | - | |
| 298 | - /** | |
| 299 | - * Check operator is supported. | |
| 300 | - * | |
| 301 | - * @since 3.5.0 | |
| 302 | - * | |
| 303 | - * @param string $operator operator like =, !=, > , < etc. | |
| 304 | - * | |
| 305 | - * @return boolean | |
| 306 | - */ | |
| 307 | - public static function is_support_operator( $operator ) { | |
| 308 | - $operator = strtoupper( $operator ); | |
| 309 | - | |
| 310 | - return in_array( | |
| 311 | - $operator, | |
| 312 | - array( | |
| 313 | - '=', | |
| 314 | - '!=', | |
| 315 | - '<>', | |
| 316 | - '>', | |
| 317 | - '<', | |
| 318 | - '>=', | |
| 319 | - '<=', | |
| 320 | - 'LIKE', | |
| 321 | - 'NOT LIKE', | |
| 322 | - 'IN', | |
| 323 | - 'NOT IN', | |
| 324 | - 'IS', | |
| 325 | - 'IS NOT', | |
| 326 | - 'BETWEEN', | |
| 327 | - 'NOT BETWEEN', | |
| 328 | - 'RAW', | |
| 329 | - ), | |
| 330 | - true | |
| 331 | - ); | |
| 332 | - } | |
| 333 | - | |
| 334 | - /** | |
| 335 | - * Prepare where clause string | |
| 336 | - * | |
| 337 | 175 | * @since 2.0.9 |
| 338 | - * @since 3.0.0 Null value support added, if need to check with null: [name => 'null'] | |
| 339 | - * @since 3.5.0 All common SQL comparison operators support added. | |
| 340 | - * $where = array( | |
| 341 | - * 'id' => ['BETWEEN', [10, 20]], | |
| 342 | - * 'status' => ['!=', 'draft'], | |
| 343 | - * 'email' => ['LIKE', '%@gmail.com'], | |
| 344 | - * 'type' => ['NOT IN', ['test', 'sample']], | |
| 345 | - * 'age' => ['>=', 18], | |
| 346 | - * 'active' => true, | |
| 347 | - * 'deleted_at' => 'null', | |
| 348 | - * 'role' => 'editor', | |
| 349 | - * ) | |
| 350 | - * @since 3.6.0 Added raw query support. Make sure the query written is not sql injectable. | |
| 351 | - * $where = array( | |
| 352 | - * 'username = %s' => [ 'RAW' , array( 'test' ) ] | |
| 353 | - * ) | |
| 354 | - * @param array $where assoc array with field and value. | |
| 355 | - * | |
| 356 | - * @return string | |
| 357 | 176 | */ |
| 358 | - public static function prepare_where_clause( array $where ) { | |
| 177 | + private function build_where_clause( array $where ) { | |
| 359 | 178 | $arr = array(); |
| 360 | 179 | foreach ( $where as $field => $value ) { |
| 361 | - $operator = null; | |
| 362 | - if ( is_array( $value ) && isset( $value[0] ) && is_string( $value[0] ) && self::is_support_operator( $value[0] ) ) { | |
| 363 | - $operator = strtoupper( $value[0] ); | |
| 364 | - $val = $value[1]; | |
| 365 | - switch ( $operator ) { | |
| 366 | - case 'IN': | |
| 367 | - case 'NOT IN': | |
| 368 | - if ( is_array( $val ) ) { | |
| 369 | - $clause = array( $field, $operator, $val ); | |
| 370 | - } | |
| 371 | - break; | |
| 372 | - | |
| 373 | - case 'BETWEEN': | |
| 374 | - case 'NOT BETWEEN': | |
| 375 | - if ( is_array( $val ) && count( $val ) === 2 ) { | |
| 376 | - $clause = array( $field, $operator, $val ); | |
| 377 | - } | |
| 378 | - break; | |
| 379 | - | |
| 380 | - case 'IS': | |
| 381 | - case 'IS NOT': | |
| 382 | - $val = strtoupper( $val ) === 'NULL' ? 'NULL' : $val; | |
| 383 | - $clause = array( $field, $operator, $val ); | |
| 384 | - break; | |
| 385 | - case 'RAW': | |
| 386 | - $final_query = ''; | |
| 387 | - if ( ! empty( $field ) && is_array( $val ) ) { | |
| 388 | - $final_query = self::prepare_raw_query( $field, $val ); | |
| 389 | - } | |
| 390 | - $clause = $final_query; | |
| 391 | - break; | |
| 392 | - default: // =, !=, <, >, <=, >=, LIKE, NOT LIKE, <> | |
| 393 | - $clause = array( $field, $operator, $val ); | |
| 394 | - break; | |
| 395 | - } | |
| 396 | - } elseif ( is_array( $value ) ) { | |
| 397 | - $clause = array( $field, 'IN', $value ); | |
| 398 | - } elseif ( 'null' === strtolower( $value ) ) { | |
| 399 | - $clause = array( $field, 'IS', 'NULL' ); | |
| 400 | - } else { | |
| 401 | - $clause = array( $field, '=', $value ); | |
| 402 | - } | |
| 403 | - | |
| 404 | - $arr[] = ( 'RAW' === $operator ) ? $clause : self::make_clause( $clause ); | |
| 180 | + $value = is_numeric( $value ) ? ( $value + 0 ) : "'" . $value . "'"; | |
| 181 | + $arr[] = "{$field}={$value}"; | |
| 405 | 182 | } |
| 406 | 183 | |
| 407 | 184 | return implode( ' AND ', $arr ); |
| 408 | 185 | } |
| @@ -407,74 +184,18 @@ | ||
| 407 | 184 | return implode( ' AND ', $arr ); |
| 408 | 185 | } |
| 409 | 186 | |
| 410 | 187 | /** |
| 411 | - * Prepare raw query for query helper. | |
| 412 | - * | |
| 413 | - * @since 3.6.0 | |
| 414 | - * | |
| 415 | - * @param string $raw_query the query to execute. | |
| 416 | - * @param array $parameters the parameters to pass to the query. | |
| 417 | - * | |
| 418 | - * @return string | |
| 419 | - */ | |
| 420 | - public static function prepare_raw_query( $raw_query, $parameters ) { | |
| 421 | - /** | |
| 422 | - * Not allowed unsafe SQL control characters [;, --, /*] | |
| 423 | - * Allowed safe SQL control characters only. | |
| 424 | - */ | |
| 425 | - $is_safe = preg_match( '/^[a-zA-Z0-9_%\.=\s\'"<>\(\)\-\[\],]+$/', $raw_query ); | |
| 426 | - if ( ! $is_safe ) { | |
| 427 | - return ''; | |
| 428 | - } | |
| 429 | - | |
| 430 | - if ( ! count( $parameters ) ) { | |
| 431 | - return $raw_query; | |
| 432 | - } | |
| 433 | - | |
| 434 | - global $wpdb; | |
| 435 | - | |
| 436 | - $final_query = $wpdb->prepare( $raw_query, $parameters ); //phpcs:ignore | |
| 437 | - | |
| 438 | - return $final_query; | |
| 439 | - } | |
| 440 | - | |
| 441 | - /** | |
| 442 | - * Prepare like clause string with or | |
| 443 | - * | |
| 444 | - * @since 1.0.0 | |
| 445 | - * | |
| 446 | - * @param array $where assoc array with field and value. | |
| 447 | - * @param string $relation default is OR. | |
| 448 | - * | |
| 449 | - * @return string | |
| 450 | - */ | |
| 451 | - public static function prepare_like_clause( array $where, $relation = 'OR' ) { | |
| 452 | - global $wpdb; | |
| 453 | - | |
| 454 | - $like_conditions = array(); | |
| 455 | - | |
| 456 | - foreach ( $where as $column_name => $term ) { | |
| 457 | - //phpcs:ignore | |
| 458 | - $like_conditions[] = $wpdb->prepare( "$column_name LIKE %s", '%' . $wpdb->esc_like( $term ) . '%' ); | |
| 459 | - } | |
| 460 | - | |
| 461 | - $where_clause = implode( ' OR ', $like_conditions ); | |
| 462 | - | |
| 463 | - return $where_clause; | |
| 464 | - } | |
| 465 | - | |
| 466 | - /** | |
| 467 | 188 | * Sanitize assoc array |
| 468 | 189 | * |
| 469 | - * @param array $array an assoc array. | |
| 190 | + * @param array $array an assoc array | |
| 470 | 191 | * @return array |
| 471 | 192 | * |
| 472 | 193 | * @since 2.0.9 |
| 473 | 194 | */ |
| 474 | - private static function sanitize_assoc_array( array $array ) { | |
| 195 | + private function sanitize_assoc_array( array $array ) { | |
| 475 | 196 | return array_map( |
| 476 | - function ( $value ) { | |
| 197 | + function( $value ) { | |
| 477 | 198 | return sanitize_text_field( $value ); |
| 478 | 199 | }, |
| 479 | 200 | $array |
| 480 | 201 | ); |
| @@ -483,9 +204,9 @@ | ||
| 483 | 204 | /** |
| 484 | 205 | * Delete comment with associate meta data |
| 485 | 206 | * |
| 486 | 207 | * @param array $where associative array with field and value. |
| 487 | - * Example: array( 'comment_type' => 'comment', 'comment_id' => 1 ). | |
| 208 | + * Example: array( 'comment_type' => 'comment', 'comment_id' => 1 ) | |
| 488 | 209 | * @return bool |
| 489 | 210 | * |
| 490 | 211 | * @since 2.0.9 |
| 491 | 212 | */ |
| @@ -493,19 +214,20 @@ | ||
| 493 | 214 | if ( count( $where ) === 0 || ! tutor_utils()->is_assoc( $where ) ) { |
| 494 | 215 | return false; |
| 495 | 216 | } |
| 496 | 217 | |
| 497 | - $where = self::prepare_where_clause( self::sanitize_assoc_array( $where ) ); | |
| 218 | + $obj = new self(); | |
| 219 | + $where = $obj->build_where_clause( $obj->sanitize_assoc_array( $where ) ); | |
| 498 | 220 | |
| 499 | 221 | global $wpdb; |
| 500 | - $ids = $wpdb->get_col( "SELECT comment_id FROM {$wpdb->comments} WHERE {$where}" );//phpcs:ignore | |
| 222 | + $ids = $wpdb->get_col( "SELECT comment_id FROM {$wpdb->comments} WHERE {$where}" ); | |
| 501 | 223 | |
| 502 | 224 | if ( is_array( $ids ) && count( $ids ) ) { |
| 503 | - $in_clause = self::prepare_in_clause( $ids ); | |
| 504 | - // delete comment metas. | |
| 505 | - $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN({$in_clause}) " ) );//phpcs:ignore | |
| 506 | - // delete comment. | |
| 507 | - $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE {$where}" );//phpcs:ignore | |
| 225 | + $ids_str = "'" . implode( "','", $ids ) . "'"; | |
| 226 | + // delete comment metas | |
| 227 | + $wpdb->query( "DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN({$ids_str}) " ); | |
| 228 | + // delete comment | |
| 229 | + $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE {$where}" ); | |
| 508 | 230 | |
| 509 | 231 | return true; |
| 510 | 232 | } |
| 511 | 233 | |
| @@ -515,9 +237,9 @@ | ||
| 515 | 237 | /** |
| 516 | 238 | * Delete post with associate meta data |
| 517 | 239 | * |
| 518 | 240 | * @param array $where associative array with field and value. |
| 519 | - * Example: array( 'post_type' => 'post', 'id' => 1 ). | |
| 241 | + * Example: array( 'post_type' => 'post', 'id' => 1 ) | |
| 520 | 242 | * @return bool |
| 521 | 243 | * |
| 522 | 244 | * @since 2.0.9 |
| 523 | 245 | */ |
| @@ -525,19 +247,20 @@ | ||
| 525 | 247 | if ( count( $where ) === 0 || ! tutor_utils()->is_assoc( $where ) ) { |
| 526 | 248 | return false; |
| 527 | 249 | } |
| 528 | 250 | |
| 529 | - $where = self::prepare_where_clause( self::sanitize_assoc_array( $where ) ); | |
| 251 | + $obj = new self(); | |
| 252 | + $where = $obj->build_where_clause( $obj->sanitize_assoc_array( $where ) ); | |
| 530 | 253 | |
| 531 | 254 | global $wpdb; |
| 532 | - $ids = $wpdb->get_col( "SELECT id FROM {$wpdb->posts} WHERE {$where}" );//phpcs:ignore | |
| 255 | + $ids = $wpdb->get_col( "SELECT id FROM {$wpdb->posts} WHERE {$where}" ); | |
| 533 | 256 | |
| 534 | 257 | if ( is_array( $ids ) && count( $ids ) ) { |
| 535 | - $in_clause = self::prepare_in_clause( $ids ); | |
| 536 | - // delete post metas. | |
| 537 | - $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN({$in_clause}) " ) );//phpcs:ignore | |
| 538 | - // delete post. | |
| 539 | - $wpdb->query( "DELETE FROM {$wpdb->posts} WHERE {$where}" );//phpcs:ignore | |
| 258 | + $ids_str = "'" . implode( "','", $ids ) . "'"; | |
| 259 | + // delete post metas | |
| 260 | + $wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN({$ids_str}) " ); | |
| 261 | + // delete post | |
| 262 | + $wpdb->query( "DELETE FROM {$wpdb->posts} WHERE {$where}" ); | |
| 540 | 263 | |
| 541 | 264 | return true; |
| 542 | 265 | } |
| 543 | 266 | |
| @@ -544,254 +267,8 @@ | ||
| 544 | 267 | return false; |
| 545 | 268 | } |
| 546 | 269 | |
| 547 | 270 | /** |
| 548 | - * Prepare SELECT clause. | |
| 549 | - * | |
| 550 | - * @since 3.8.0 | |
| 551 | - * | |
| 552 | - * @param mixed $columns Column name or list of columns. | |
| 553 | - * | |
| 554 | - * @return string | |
| 555 | - */ | |
| 556 | - protected static function prepare_select_clause( $columns = '' ) { | |
| 557 | - if ( empty( $columns ) ) { | |
| 558 | - return '*'; | |
| 559 | - } | |
| 560 | - | |
| 561 | - if ( is_array( $columns ) ) { | |
| 562 | - return implode( ',', $columns ); | |
| 563 | - } | |
| 564 | - | |
| 565 | - return $columns; | |
| 566 | - } | |
| 567 | - | |
| 568 | - /** | |
| 569 | - * Prepare JOIN clause. | |
| 570 | - * | |
| 571 | - * @since 3.8.0 | |
| 572 | - * | |
| 573 | - * @param array $joins Array of joins, each item: | |
| 574 | - * - type: join type (LEFT, INNER, RIGHT etc). | |
| 575 | - * - table: table name. | |
| 576 | - * - on: join condition. | |
| 577 | - * | |
| 578 | - * @return string | |
| 579 | - */ | |
| 580 | - protected static function prepare_join_clause( $joins = array() ) { | |
| 581 | - if ( empty( $joins ) || ! is_array( $joins ) ) { | |
| 582 | - return ''; | |
| 583 | - } | |
| 584 | - | |
| 585 | - $clause = ''; | |
| 586 | - foreach ( $joins as $join ) { | |
| 587 | - $type = strtoupper( $join['type'] ?? 'LEFT' ); | |
| 588 | - $table = self::prepare_table_name( $join['table'] ); | |
| 589 | - $on = $join['on']; | |
| 590 | - if ( $table && $on ) { | |
| 591 | - $clause .= " {$type} JOIN {$table} ON {$on} "; | |
| 592 | - } | |
| 593 | - } | |
| 594 | - | |
| 595 | - return $clause; | |
| 596 | - } | |
| 597 | - | |
| 598 | - /** | |
| 599 | - * Prepare WHERE + SEARCH clause together. | |
| 600 | - * | |
| 601 | - * @since 3.8.0 | |
| 602 | - * | |
| 603 | - * @param array $where Array of key => value pairs. | |
| 604 | - * @param array $search Array of key => search string pairs. | |
| 605 | - * @param string $search_operator Operator for search conditions (AND/OR). | |
| 606 | - * | |
| 607 | - * @return string | |
| 608 | - */ | |
| 609 | - protected static function prepare_where_search_clause( $where = array(), $search = array(), $search_operator = 'OR' ) { | |
| 610 | - $clauses = array(); | |
| 611 | - | |
| 612 | - // Handle WHERE conditions. | |
| 613 | - if ( ! empty( $where ) && is_array( $where ) ) { | |
| 614 | - $clauses[] = self::prepare_where_clause( $where ); | |
| 615 | - } | |
| 616 | - | |
| 617 | - // Handle SEARCH conditions. | |
| 618 | - if ( ! empty( $search ) && is_array( $search ) ) { | |
| 619 | - $clauses[] = self::prepare_like_clause( $search, $search_operator ); | |
| 620 | - } | |
| 621 | - | |
| 622 | - if ( empty( $clauses ) ) { | |
| 623 | - return ''; | |
| 624 | - } | |
| 625 | - | |
| 626 | - return 'WHERE ' . implode( ' AND ', $clauses ); | |
| 627 | - } | |
| 628 | - | |
| 629 | - /** | |
| 630 | - * Prepare order by clause. | |
| 631 | - * | |
| 632 | - * @since 3.8.0 | |
| 633 | - * | |
| 634 | - * @param string $orderby order by column. | |
| 635 | - * @param string $order order ASC|DESC. | |
| 636 | - * | |
| 637 | - * @return string | |
| 638 | - */ | |
| 639 | - protected static function prepare_order_clause( $orderby = '', $order = 'DESC' ) { | |
| 640 | - if ( empty( $orderby ) ) { | |
| 641 | - return ''; | |
| 642 | - } | |
| 643 | - | |
| 644 | - // Allowed: foo, foo_bar, _foo, foo.bar etc. | |
| 645 | - if ( ! preg_match( '/^[A-Za-z_][A-Za-z0-9._]*$/', $orderby ) ) { | |
| 646 | - return ''; | |
| 647 | - } | |
| 648 | - | |
| 649 | - $order = strtoupper( $order ) === 'ASC' ? 'ASC' : 'DESC'; | |
| 650 | - return "ORDER BY {$orderby} {$order}"; | |
| 651 | - } | |
| 652 | - | |
| 653 | - /** | |
| 654 | - * Prepare LIMIT clause. | |
| 655 | - * | |
| 656 | - * @since 3.8.0 | |
| 657 | - * | |
| 658 | - * @param int $limit limit. | |
| 659 | - * @param int $offset offset. | |
| 660 | - * | |
| 661 | - * @return string | |
| 662 | - */ | |
| 663 | - protected static function prepare_limit_clause( $limit = 0, $offset = 0 ) { | |
| 664 | - if ( $limit < 1 || $offset < 0 ) { | |
| 665 | - return ''; | |
| 666 | - } | |
| 667 | - | |
| 668 | - return sprintf( 'LIMIT %d OFFSET %d', $limit, $offset ); | |
| 669 | - } | |
| 670 | - | |
| 671 | - /** | |
| 672 | - * Run a database query with flexible arguments. | |
| 673 | - * | |
| 674 | - * Supports SELECT, JOIN, WHERE, SEARCH, GROUP BY, HAVING, ORDER BY, | |
| 675 | - * LIMIT (pagination), and can return count, single row or full result set. | |
| 676 | - * | |
| 677 | - * @since 3.8.0 | |
| 678 | - * | |
| 679 | - * @param string $table table name. | |
| 680 | - * @param array $args { | |
| 681 | - * Query arguments. | |
| 682 | - * | |
| 683 | - * @type string|array $select Columns to select, defaults to "*". | |
| 684 | - * @type string $alias Table alias. | |
| 685 | - * @type array $where WHERE conditions [ 'col' => 'val', ... ]. | |
| 686 | - * @type array $search LIKE conditions [ 'col' => 'keyword', ... ]. | |
| 687 | - * @type array $joins JOIN clauses [ [ 'type' => 'LEFT', 'table' => '...', 'on' => '...' ], ... ]. | |
| 688 | - * @type string $groupby GROUP BY clause. | |
| 689 | - * @type string $having HAVING clause. | |
| 690 | - * @type string $orderby Column to order by. | |
| 691 | - * @type string $order ASC|DESC, default DESC. | |
| 692 | - * @type int $limit Limit. | |
| 693 | - * @type int $offset Offset. | |
| 694 | - * @type int $per_page Results per page for pagination. | |
| 695 | - * @type int $page Current page number for pagination. | |
| 696 | - * @type bool $count If true, return only total count. | |
| 697 | - * @type bool $single If true, return only single row. | |
| 698 | - * @type string $output OBJECT|ARRAY_A default is OBJECT. | |
| 699 | - * } | |
| 700 | - * | |
| 701 | - * @return mixed Result set, count or single row. | |
| 702 | - */ | |
| 703 | - public static function query( $table, $args = array() ) { | |
| 704 | - // Flags. | |
| 705 | - $count = isset( $args['count'] ) && $args['count']; | |
| 706 | - $single = isset( $args['single'] ) && $args['single']; | |
| 707 | - $pagination = isset( $args['per_page'], $args['page'] ); | |
| 708 | - $output = $args['output'] ?? 'OBJECT'; | |
| 709 | - | |
| 710 | - // Primary table. | |
| 711 | - $table = self::prepare_table_name( $table ); | |
| 712 | - $alias = $args['alias'] ?? 'main'; | |
| 713 | - $table_with_alias = "{$table} AS {$alias}"; | |
| 714 | - | |
| 715 | - // Build clauses. | |
| 716 | - $select_clause = self::prepare_select_clause( $args['select'] ?? '' ); | |
| 717 | - $join_clause = self::prepare_join_clause( $args['joins'] ?? array() ); | |
| 718 | - $where_clause = self::prepare_where_search_clause( $args['where'] ?? array(), $args['search'] ?? array() ); | |
| 719 | - $groupby_clause = empty( $args['groupby'] ) ? '' : 'GROUP BY ' . $args['groupby']; | |
| 720 | - $having_clause = empty( $args['having'] ) ? '' : 'HAVING ' . $args['having']; | |
| 721 | - $order_by_clause = self::prepare_order_clause( $args['orderby'] ?? '', $args['order'] ?? 'DESC' ); | |
| 722 | - | |
| 723 | - global $wpdb; | |
| 724 | - | |
| 725 | - // Count only. | |
| 726 | - if ( $count ) { | |
| 727 | - $sql_query = "SELECT COUNT(*) | |
| 728 | - FROM {$table_with_alias} | |
| 729 | - {$join_clause} | |
| 730 | - {$where_clause} | |
| 731 | - {$groupby_clause} | |
| 732 | - {$having_clause}"; | |
| 733 | - | |
| 734 | - return (int) $wpdb->get_var( $sql_query ); //phpcs:ignore | |
| 735 | - } | |
| 736 | - | |
| 737 | - // Single record. | |
| 738 | - if ( $single ) { | |
| 739 | - $sql_query = "SELECT {$select_clause} | |
| 740 | - FROM {$table_with_alias} | |
| 741 | - {$join_clause} | |
| 742 | - {$where_clause} | |
| 743 | - {$groupby_clause} | |
| 744 | - {$having_clause} | |
| 745 | - {$order_by_clause} | |
| 746 | - LIMIT 1"; | |
| 747 | - | |
| 748 | - return $wpdb->get_row( $sql_query, $output ); //phpcs:ignore | |
| 749 | - } | |
| 750 | - | |
| 751 | - $calc_found_rows = $pagination ? 'SQL_CALC_FOUND_ROWS' : ''; | |
| 752 | - $limit = isset( $args['limit'] ) ? (int) $args['limit'] : 0; | |
| 753 | - $offset = isset( $args['offset'] ) ? (int) $args['offset'] : 0; | |
| 754 | - | |
| 755 | - if ( $pagination ) { | |
| 756 | - $limit = (int) $args['per_page']; | |
| 757 | - $offset = (int) ( $args['page'] - 1 ) * $limit; | |
| 758 | - } | |
| 759 | - | |
| 760 | - $limit_clause = self::prepare_limit_clause( $limit, $offset ); | |
| 761 | - | |
| 762 | - $sql_query = "SELECT {$calc_found_rows} {$select_clause} | |
| 763 | - FROM {$table_with_alias} | |
| 764 | - {$join_clause} | |
| 765 | - {$where_clause} | |
| 766 | - {$groupby_clause} | |
| 767 | - {$having_clause} | |
| 768 | - {$order_by_clause} | |
| 769 | - {$limit_clause}"; | |
| 770 | - | |
| 771 | - $rows = $wpdb->get_results( $sql_query, $output ); //phpcs:ignore | |
| 772 | - | |
| 773 | - if ( $pagination ) { | |
| 774 | - $has_records = is_array( $rows ) && count( $rows ); | |
| 775 | - $page = (int) $args['page']; | |
| 776 | - $per_page = (int) $args['per_page']; | |
| 777 | - $total_record = (int) $has_records ? $wpdb->get_var( 'SELECT FOUND_ROWS()' ) : 0; | |
| 778 | - $total_page = (int) ceil( $total_record / $per_page ); | |
| 779 | - | |
| 780 | - return array( | |
| 781 | - 'total_record' => (int) $total_record, | |
| 782 | - 'per_page' => $per_page, | |
| 783 | - 'current_page' => $page, | |
| 784 | - 'total_page' => $total_page, | |
| 785 | - 'data' => $rows, | |
| 786 | - ); | |
| 787 | - | |
| 788 | - } | |
| 789 | - | |
| 790 | - return $rows; | |
| 791 | - } | |
| 792 | - | |
| 793 | - /** | |
| 794 | 271 | * Get a single row from any table with where clause |
| 795 | 272 | * |
| 796 | 273 | * @param string $table table name with prefix. |
| 797 | 274 | * |
| @@ -803,14 +280,11 @@ | ||
| 803 | 280 | * @return mixed based on output param, default object |
| 804 | 281 | */ |
| 805 | 282 | public static function get_row( string $table, array $where, string $order_by, string $order = 'DESC', string $output = 'OBJECT' ) { |
| 806 | 283 | global $wpdb; |
| 807 | - | |
| 808 | - $table = self::prepare_table_name( $table ); | |
| 809 | - $where_clause = self::prepare_where_clause( $where ); | |
| 810 | - | |
| 811 | - //phpcs:disable | |
| 812 | - $query = $wpdb->prepare( | |
| 284 | + $obj = new self(); | |
| 285 | + $where_clause = $obj->build_where_clause( $where ); | |
| 286 | + $query = $wpdb->prepare( | |
| 813 | 287 | "SELECT * |
| 814 | 288 | FROM {$table} |
| 815 | 289 | WHERE {$where_clause} |
| 816 | 290 | ORDER BY {$order_by} {$order} |
| @@ -817,14 +291,12 @@ | ||
| 817 | 291 | LIMIT %d |
| 818 | 292 | ", |
| 819 | 293 | 1 |
| 820 | 294 | ); |
| 821 | - | |
| 822 | 295 | return $wpdb->get_row( |
| 823 | 296 | $query, |
| 824 | 297 | $output |
| 825 | 298 | ); |
| 826 | - //phpcs:enable | |
| 827 | 299 | } |
| 828 | 300 | |
| 829 | 301 | /** |
| 830 | 302 | * Get all row from any table with where clause |
| @@ -829,15 +301,14 @@ | ||
| 829 | 301 | /** |
| 830 | 302 | * Get all row from any table with where clause |
| 831 | 303 | * |
| 832 | 304 | * @since 2.2.1 |
| 833 | - * @since 3.0.0 added support for -1 value in the limit parameter. | |
| 834 | 305 | * |
| 835 | 306 | * @param string $table table name with prefix. |
| 836 | 307 | * |
| 837 | 308 | * @param array $where assoc_array. For ex: [col_name => value ]. |
| 838 | 309 | * @param string $order_by order by column name. |
| 839 | - * @param int $limit default is 1000, -1 for no limit. | |
| 310 | + * @param int $limit default is 1000. | |
| 840 | 311 | * @param string $order DESC or ASC, default is DESC. |
| 841 | 312 | * @param string $output expected output type, default is object. |
| 842 | 313 | * |
| 843 | 314 | * @return mixed based on output param, default object |
| @@ -843,32 +314,27 @@ | ||
| 843 | 314 | * @return mixed based on output param, default object |
| 844 | 315 | */ |
| 845 | 316 | public static function get_all( string $table, array $where, string $order_by, $limit = 1000, string $order = 'DESC', string $output = 'OBJECT' ) { |
| 846 | 317 | global $wpdb; |
| 847 | - | |
| 848 | - $table = self::prepare_table_name( $table ); | |
| 849 | - $where_clause = self::prepare_where_clause( $where ); | |
| 850 | - if ( ! empty( $where_clause ) ) { | |
| 851 | - $where_clause = "WHERE {$where_clause}"; | |
| 852 | - } | |
| 853 | - | |
| 854 | - $limit = (int) sanitize_text_field( $limit ); | |
| 855 | - $limit_clause = ( -1 === $limit ) ? '' : 'LIMIT ' . $limit; | |
| 856 | - | |
| 857 | - //phpcs:disable | |
| 858 | - $query = "SELECT * | |
| 318 | + $obj = new self(); | |
| 319 | + $where_clause = $obj->build_where_clause( $where ); | |
| 320 | + $limit = sanitize_text_field( $limit ); | |
| 321 | + $query = $wpdb->prepare( | |
| 322 | + "SELECT * | |
| 859 | 323 | FROM {$table} |
| 860 | - {$where_clause} | |
| 324 | + WHERE {$where_clause} | |
| 861 | 325 | ORDER BY {$order_by} {$order} |
| 862 | - {$limit_clause}"; | |
| 863 | - | |
| 326 | + LIMIT %d | |
| 327 | + ", | |
| 328 | + $limit | |
| 329 | + ); | |
| 864 | 330 | return $wpdb->get_results( |
| 865 | 331 | $query, |
| 866 | 332 | $output |
| 867 | 333 | ); |
| 868 | - //phpcs:enable | |
| 869 | 334 | } |
| 870 | 335 | |
| 336 | + | |
| 871 | 337 | /** |
| 872 | 338 | * Update multiple rows by using where in |
| 873 | 339 | * clause |
| 874 | 340 | * |
| @@ -883,10 +349,8 @@ | ||
| 883 | 349 | * @return bool true on success, false on failure |
| 884 | 350 | */ |
| 885 | 351 | public static function update_where_in( string $table, array $data, string $where_in, string $where_col = 'ID' ) { |
| 886 | 352 | global $wpdb; |
| 887 | - | |
| 888 | - $table = self::prepare_table_name( $table ); | |
| 889 | 353 | if ( empty( $where_in ) || empty( $where_col ) ) { |
| 890 | 354 | return false; |
| 891 | 355 | } |
| 892 | 356 | $set_clause = self::prepare_set_clause( $data ); |
| @@ -923,16 +387,10 @@ | ||
| 923 | 387 | // Multi dimension not allowed. |
| 924 | 388 | if ( is_array( $value ) ) { |
| 925 | 389 | continue; |
| 926 | 390 | } |
| 927 | - | |
| 928 | - if ( is_null( $value ) ) { | |
| 929 | - $set .= "$key = null"; | |
| 930 | - } else { | |
| 931 | - $value = esc_sql( sanitize_text_field( $value ) ); | |
| 932 | - $set .= is_numeric( $value ) ? "$key = $value" : "$key = '" . $value ."'"; | |
| 933 | - } | |
| 934 | - | |
| 391 | + $value = sanitize_text_field( $value ); | |
| 392 | + $set .= is_numeric( $value ) ? "$key = $value" : "$key = '" . $value ."'"; | |
| 935 | 393 | $set .= ","; |
| 936 | 394 | } |
| 937 | 395 | return rtrim( $set, ',' ); |
| 938 | 396 | } |
| @@ -937,456 +395,32 @@ | ||
| 937 | 395 | return rtrim( $set, ',' ); |
| 938 | 396 | } |
| 939 | 397 | |
| 940 | 398 | /** |
| 941 | - * Prepare value before using in query. | |
| 942 | - * | |
| 943 | - * @since 3.9.7 | |
| 944 | - * | |
| 945 | - * @param string|int|float $value the value to prepare. | |
| 946 | - * | |
| 947 | - * @return mixed | |
| 948 | - */ | |
| 949 | - public static function prepare_value( $value ) { | |
| 950 | - global $wpdb; | |
| 951 | - $escaped_value = null; | |
| 952 | - if ( is_int( $value ) ) { | |
| 953 | - $escaped_value = $wpdb->prepare( '%d', $value ); | |
| 954 | - } elseif ( is_float( $value ) ) { | |
| 955 | - list( $whole, $decimal ) = explode( '.', $value ); | |
| 956 | - $expression = '%.'. strlen( $decimal ) . 'f'; | |
| 957 | - $escaped_value = $wpdb->prepare( $expression, $value ); | |
| 958 | - } else { | |
| 959 | - $escaped_value = $wpdb->prepare( '%s', $value ); | |
| 960 | - } | |
| 961 | - return $escaped_value; | |
| 962 | - } | |
| 963 | - | |
| 964 | - /** | |
| 965 | 399 | * Make sanitized SQL IN clause value from an array |
| 966 | 400 | * |
| 401 | + * @param array $arr a sequentital array. | |
| 402 | + * @return string | |
| 967 | 403 | * @since 2.1.1 |
| 968 | - * | |
| 969 | - * @param array $arr a sequential array. | |
| 970 | - * | |
| 971 | - * @return string | |
| 972 | 404 | */ |
| 973 | 405 | public static function prepare_in_clause( array $arr ) { |
| 974 | - $escaped = array_map( fn( $value ) => self::prepare_value( $value ), $arr ); | |
| 975 | - return implode( ',', $escaped ); | |
| 976 | - } | |
| 977 | - | |
| 978 | - /** | |
| 979 | - * Check table exist in database. | |
| 980 | - * | |
| 981 | - * @since 2.5.0 | |
| 982 | - * | |
| 983 | - * @param string $table table name. | |
| 984 | - * | |
| 985 | - * @return bool | |
| 986 | - */ | |
| 987 | - public static function table_exists( $table ) { | |
| 988 | - global $wpdb; | |
| 989 | - | |
| 990 | - $table = self::prepare_table_name( $table ); | |
| 991 | - $sql = "SHOW TABLES LIKE '{$table}'"; | |
| 992 | - return $wpdb->get_var( $sql ) === $table; | |
| 993 | - } | |
| 994 | - | |
| 995 | - /** | |
| 996 | - * Check column exist in a table | |
| 997 | - * | |
| 998 | - * @since 3.0.0 | |
| 999 | - * | |
| 1000 | - * @param string $table table name. | |
| 1001 | - * @param string $column column name. | |
| 1002 | - * | |
| 1003 | - * @return bool | |
| 1004 | - */ | |
| 1005 | - public static function column_exist( $table, $column ) { | |
| 1006 | - global $wpdb; | |
| 1007 | - | |
| 1008 | - $table = self::prepare_table_name( $table ); | |
| 1009 | - $sql = "SHOW COLUMNS FROM {$table} LIKE '{$column}'"; | |
| 1010 | - return $wpdb->get_var( $sql ) === $column; | |
| 1011 | - } | |
| 1012 | - | |
| 1013 | - /** | |
| 1014 | - * Get data by joining multiple tables with specified join relations. | |
| 1015 | - * | |
| 1016 | - * Argument should be SQL escaped. | |
| 1017 | - * | |
| 1018 | - * @since 3.0.0 | |
| 1019 | - * @since 3.8.2 param $get_row added. | |
| 1020 | - * | |
| 1021 | - * @param string $primary_table The primary table name with prefix. | |
| 1022 | - * @param array $joining_tables An array of join relations. Each relation should be an array with keys 'type', 'table', 'on'. | |
| 1023 | - * @param array $select_columns An array of columns to select. | |
| 1024 | - * @param array $where An associative array for the WHERE clause. For example: [col_name => value]. Without sql esc. | |
| 1025 | - * @param array $search An associative array for the search clause. For example: [col_name => value]. Without sql esc. | |
| 1026 | - * @param string $order_by Order by column name. | |
| 1027 | - * @param int $limit Maximum number of rows to return. | |
| 1028 | - * @param int $offset Offset for pagination. | |
| 1029 | - * @param string $order DESC or ASC, default is DESC. | |
| 1030 | - * @param string $output Expected output type, default is OBJECT. | |
| 1031 | - * @param bool $get_row Get a single row. | |
| 1032 | - * | |
| 1033 | - * @throws \Exception If an error occurred during the query execution. | |
| 1034 | - * | |
| 1035 | - * @return mixed Based on output param, default OBJECT. | |
| 1036 | - */ | |
| 1037 | - public static function get_joined_data( | |
| 1038 | - string $primary_table, | |
| 1039 | - array $joining_tables, | |
| 1040 | - array $select_columns, | |
| 1041 | - array $where = [], | |
| 1042 | - array $search = [], | |
| 1043 | - string $order_by = '', | |
| 1044 | - $limit = 10, | |
| 1045 | - $offset = 0, | |
| 1046 | - string $order = 'DESC', | |
| 1047 | - string $output = 'OBJECT', | |
| 1048 | - bool $get_row = false | |
| 1049 | - ) { | |
| 1050 | - global $wpdb; | |
| 1051 | - | |
| 1052 | - $select_clause = implode( ', ', $select_columns ); | |
| 1053 | - $from_clause = self::prepare_table_name( $primary_table ); | |
| 1054 | - $join_clauses = self::prepare_join_clause( $joining_tables ); | |
| 1055 | - $where_clause = self::prepare_where_search_clause( $where, $search ); | |
| 1056 | - $order_by_clause = self::prepare_order_clause( $order_by, $order ); | |
| 1057 | - $limit_clause = self::prepare_limit_clause( $limit, $offset ); | |
| 1058 | - | |
| 1059 | - $query = "SELECT SQL_CALC_FOUND_ROWS | |
| 1060 | - {$select_clause} | |
| 1061 | - FROM {$from_clause} | |
| 1062 | - {$join_clauses} | |
| 1063 | - {$where_clause} | |
| 1064 | - {$order_by_clause} | |
| 1065 | - {$limit_clause}"; | |
| 1066 | - | |
| 1067 | - if ( $get_row ) { | |
| 1068 | - return $wpdb->get_row( $query, $output ); | |
| 1069 | - } | |
| 1070 | - | |
| 1071 | - $results = $wpdb->get_results( $query, $output ); | |
| 1072 | - $has_records = is_array( $results ) && count( $results ); | |
| 1073 | - $total_count = $has_records ? (int) $wpdb->get_var( 'SELECT FOUND_ROWS()' ) : 0; | |
| 1074 | - | |
| 1075 | - // Throw exception if error occurred. | |
| 1076 | - if ( $wpdb->last_error ) { | |
| 1077 | - throw new \Exception( $wpdb->last_error ); | |
| 1078 | - } | |
| 1079 | - | |
| 1080 | - // Prepare response array. | |
| 1081 | - $response = array( | |
| 1082 | - 'total_count' => $total_count, | |
| 1083 | - 'results' => $results, | |
| 406 | + $escaped = array_map( | |
| 407 | + function( $value ) { | |
| 408 | + global $wpdb; | |
| 409 | + $escaped_value = null; | |
| 410 | + if ( is_int( $value ) ) { | |
| 411 | + $escaped_value = $wpdb->prepare( '%d', $value ); | |
| 412 | + } else if( is_float( $value ) ) { | |
| 413 | + list( $whole, $decimal ) = explode( '.', $value ); | |
| 414 | + $expression = '%.'. strlen( $decimal ) . 'f'; | |
| 415 | + $escaped_value = $wpdb->prepare( $expression, $value ); | |
| 416 | + } else { | |
| 417 | + $escaped_value = $wpdb->prepare( '%s', $value ); | |
| 418 | + } | |
| 419 | + return $escaped_value; | |
| 420 | + }, | |
| 421 | + $arr | |
| 1084 | 422 | ); |
| 1085 | - | |
| 1086 | - return $response; | |
| 1087 | - } | |
| 1088 | - | |
| 1089 | - /** | |
| 1090 | - * Get count var | |
| 1091 | - * | |
| 1092 | - * Argument should be SQL escaped. | |
| 1093 | - * | |
| 1094 | - * @since 1.0.0 | |
| 1095 | - * | |
| 1096 | - * @param string $table table name with prefix. | |
| 1097 | - * @param array $where array of where condition. | |
| 1098 | - * @param array $search array of search conditions for LIKE operator. | |
| 1099 | - * @param string $count_column column name to count, default id. | |
| 1100 | - * | |
| 1101 | - * @return int | |
| 1102 | - */ | |
| 1103 | - public static function get_count( $table, $where = [], $search = [], $count_column = 'id' ): int { | |
| 1104 | - global $wpdb; | |
| 1105 | - | |
| 1106 | - $table = self::prepare_table_name( $table ); | |
| 1107 | - $where_clause = self::prepare_where_search_clause( $where, $search, 'AND' ); | |
| 1108 | - | |
| 1109 | - $count = $wpdb->get_var( | |
| 1110 | - "SELECT COUNT($count_column) | |
| 1111 | - FROM $table | |
| 1112 | - {$where_clause}" | |
| 1113 | - ); | |
| 1114 | - | |
| 1115 | - // If error occurred then throw new exception. | |
| 1116 | - if ( $wpdb->last_error ) { | |
| 1117 | - throw new \Exception( $wpdb->last_error ); | |
| 1118 | - } | |
| 1119 | - | |
| 1120 | - return (int) $count; | |
| 1121 | - } | |
| 1122 | - | |
| 1123 | - /** | |
| 1124 | - * Get count by joining multiple tables with specified join relations. | |
| 1125 | - * | |
| 1126 | - * Argument should be SQL escaped. | |
| 1127 | - * | |
| 1128 | - * @since 3.0.0 | |
| 1129 | - * | |
| 1130 | - * @param string $primary_table The primary table name with prefix. | |
| 1131 | - * @param array $joining_tables An array of join relations. Each relation should be an array with keys 'type', 'table', 'on'. | |
| 1132 | - * @param array $where array of where conditions. | |
| 1133 | - * @param array $search array of search conditions for LIKE operator. | |
| 1134 | - * @param string $count_column column name to count, default id. | |
| 1135 | - * | |
| 1136 | - * @return int | |
| 1137 | - */ | |
| 1138 | - public static function get_joined_count(string $primary_table, array $joining_tables, array $where = [], array $search = [], string $count_column = '*'): int { | |
| 1139 | - global $wpdb; | |
| 1140 | - | |
| 1141 | - $from_clause = self::prepare_table_name( $primary_table ); | |
| 1142 | - $join_clauses = self::prepare_join_clause( $joining_tables ); | |
| 1143 | - $where_clause = self::prepare_where_search_clause( $where, $search, 'AND' ); | |
| 1144 | - | |
| 1145 | - $count_query = " | |
| 1146 | - SELECT COUNT($count_column) as total_count | |
| 1147 | - FROM {$from_clause} | |
| 1148 | - {$join_clauses} | |
| 1149 | - {$where_clause} | |
| 1150 | - "; | |
| 1151 | - | |
| 1152 | - $total_count = $wpdb->get_var( $count_query ); | |
| 1153 | - | |
| 1154 | - // If error occurred then throw new exception. | |
| 1155 | - if ( $wpdb->last_error ) { | |
| 1156 | - throw new \Exception( $wpdb->last_error ); | |
| 1157 | - } | |
| 1158 | - | |
| 1159 | - return (int) $total_count; | |
| 1160 | - } | |
| 1161 | - | |
| 1162 | - /** | |
| 1163 | - * Get all rows from any table with where and search clauses. | |
| 1164 | - * | |
| 1165 | - * @since 3.0.0 | |
| 1166 | - * | |
| 1167 | - * @param string $table Table name with prefix. | |
| 1168 | - * @param array $where Assoc array for exact match. For example: [col_name => value]. Without sql esc. | |
| 1169 | - * @param array $search Assoc array for LIKE match. For example: [col_name => search_term]. Without sql esc. | |
| 1170 | - * @param string $order_by Order by column name. | |
| 1171 | - * @param int $limit Maximum number of rows to return, default is 10. | |
| 1172 | - * @param int $offset Offset for pagination, default is 0. | |
| 1173 | - * @param string $order DESC or ASC, default is DESC. | |
| 1174 | - * @param string $output Expected output type, default is OBJECT. | |
| 1175 | - * | |
| 1176 | - * @throws \Exception Throw exception if error occurred during query execution. | |
| 1177 | - * | |
| 1178 | - * @return mixed Based on output param, default OBJECT. | |
| 1179 | - */ | |
| 1180 | - public static function get_all_with_search( string $table, array $where, array $search, string $order_by, $limit = 10, $offset = 0, string $order = 'DESC', string $output = 'OBJECT' ): array { | |
| 1181 | - global $wpdb; | |
| 1182 | - | |
| 1183 | - $table = self::prepare_table_name( $table ); | |
| 1184 | - $where_clause = self::prepare_where_search_clause( $where, $search, 'AND' ); | |
| 1185 | - $order_by_clause = self::prepare_order_clause( $order_by, $order ); | |
| 1186 | - $limit_clause = self::prepare_limit_clause( $limit, $offset ); | |
| 1187 | 423 | |
| 1188 | - // If error occurred then throw new exception. | |
| 1189 | - if ( $wpdb->last_error ) { | |
| 1190 | - throw new \Exception( $wpdb->last_error ); | |
| 1191 | - } | |
| 1192 | - | |
| 1193 | - $query = "SELECT SQL_CALC_FOUND_ROWS * | |
| 1194 | - FROM {$table} | |
| 1195 | - {$where_clause} | |
| 1196 | - {$order_by_clause} | |
| 1197 | - {$limit_clause}"; | |
| 1198 | - | |
| 1199 | - $results = $wpdb->get_results( $query, $output ); | |
| 1200 | - $has_records = is_array( $results ) && count( $results ); | |
| 1201 | - $total_count = $has_records ? (int) $wpdb->get_var( 'SELECT FOUND_ROWS()' ) : 0; | |
| 1202 | - | |
| 1203 | - // If error occurred then throw new exception. | |
| 1204 | - if ( $wpdb->last_error ) { | |
| 1205 | - throw new \Exception( $wpdb->last_error ); | |
| 1206 | - } | |
| 1207 | - | |
| 1208 | - // Prepare response array. | |
| 1209 | - $response = array( | |
| 1210 | - 'results' => $results, | |
| 1211 | - 'total_count' => $total_count, | |
| 1212 | - ); | |
| 1213 | - | |
| 1214 | - return $response; | |
| 424 | + return implode( ',', $escaped ); | |
| 1215 | 425 | } |
| 1216 | - | |
| 1217 | - /** | |
| 1218 | - * Get period clause based on the provided period. | |
| 1219 | - * | |
| 1220 | - * @since 3.0.0 | |
| 1221 | - * | |
| 1222 | - * @param string $column Table.column name, ex: table.created_at. | |
| 1223 | - * @param string $period Period for filter refund data. | |
| 1224 | - * | |
| 1225 | - * @return string | |
| 1226 | - */ | |
| 1227 | - public static function get_period_clause( string $column, string $period = '' ) { | |
| 1228 | - $period_clause = ''; | |
| 1229 | - switch ( $period ) { | |
| 1230 | - case 'today': | |
| 1231 | - $period_clause = "AND DATE($column) = CURDATE()"; | |
| 1232 | - break; | |
| 1233 | - case 'monthly': | |
| 1234 | - $period_clause = "AND MONTH($column) = MONTH(CURDATE()) AND YEAR($column) = YEAR(CURDATE())"; | |
| 1235 | - break; | |
| 1236 | - case 'yearly': | |
| 1237 | - $period_clause = "AND YEAR($column) = YEAR(CURDATE())"; | |
| 1238 | - break; | |
| 1239 | - case 'last30days': | |
| 1240 | - $period_clause = "AND DATE($column) BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND CURDATE()"; | |
| 1241 | - break; | |
| 1242 | - case 'last90days': | |
| 1243 | - $period_clause = "AND DATE($column) BETWEEN DATE_SUB(CURDATE(), INTERVAL 90 DAY) AND CURDATE()"; | |
| 1244 | - break; | |
| 1245 | - case 'last365days': | |
| 1246 | - $period_clause = "AND DATE($column) BETWEEN DATE_SUB(CURDATE(), INTERVAL 365 DAY) AND CURDATE()"; | |
| 1247 | - break; | |
| 1248 | - default: | |
| 1249 | - break; | |
| 1250 | - } | |
| 1251 | - | |
| 1252 | - return $period_clause; | |
| 1253 | - } | |
| 1254 | - | |
| 1255 | - /** | |
| 1256 | - * Get last executed SQL query. | |
| 1257 | - * | |
| 1258 | - * @since 3.6.0 | |
| 1259 | - * | |
| 1260 | - * @return string | |
| 1261 | - */ | |
| 1262 | - public static function get_last_query(){ | |
| 1263 | - global $wpdb; | |
| 1264 | - return $wpdb->last_query; | |
| 1265 | - } | |
| 1266 | - | |
| 1267 | - /** | |
| 1268 | - * Get table prefix. | |
| 1269 | - * | |
| 1270 | - * @since 3.7.0 | |
| 1271 | - * | |
| 1272 | - * @return string | |
| 1273 | - */ | |
| 1274 | - public static function get_table_prefix() { | |
| 1275 | - global $wpdb; | |
| 1276 | - return $wpdb->prefix; | |
| 1277 | - } | |
| 1278 | - | |
| 1279 | - /** | |
| 1280 | - * Prepare table name with prefix. | |
| 1281 | - * | |
| 1282 | - * @since 3.7.0 | |
| 1283 | - * | |
| 1284 | - * @param string $table_name table name. | |
| 1285 | - * | |
| 1286 | - * @return string | |
| 1287 | - */ | |
| 1288 | - public static function prepare_table_name( string $table_name ) { | |
| 1289 | - $table_prefix = self::get_table_prefix(); | |
| 1290 | - if ( strpos( $table_name,$table_prefix ) !== 0 ) { | |
| 1291 | - $table_name = $table_prefix . $table_name; | |
| 1292 | - } | |
| 1293 | - | |
| 1294 | - return $table_name; | |
| 1295 | - } | |
| 1296 | - | |
| 1297 | - /** | |
| 1298 | - * Duplicate a row with modification callback support. | |
| 1299 | - * | |
| 1300 | - * @since 3.7.0 | |
| 1301 | - * | |
| 1302 | - * @param string $table_name name of the database table (with prefix if needed). | |
| 1303 | - * @param array $where associative array of WHERE conditions. | |
| 1304 | - * @param callable|null $modifier optional callback to modify or exclude fields before insertion. | |
| 1305 | - * | |
| 1306 | - * @return int|WP_Error New row ID on success, or WP_Error on failure. | |
| 1307 | - */ | |
| 1308 | - public static function duplicate_row( $table_name, array $where, ?callable $modifier = null ) { | |
| 1309 | - global $wpdb; | |
| 1310 | - | |
| 1311 | - $table_name = self::prepare_table_name( $table_name ); | |
| 1312 | - if ( empty( $where ) ) { | |
| 1313 | - return new \WP_Error( 'missing_where', 'No WHERE condition provided.' ); | |
| 1314 | - } | |
| 1315 | - | |
| 1316 | - $where_clause = self::prepare_where_clause( $where ); | |
| 1317 | - $sql = $wpdb->prepare( "SELECT * FROM `$table_name` WHERE {$where_clause} LIMIT %d", 1 ); | |
| 1318 | - $row = $wpdb->get_row( $sql, ARRAY_A ); | |
| 1319 | - | |
| 1320 | - if ( ! $row ) { | |
| 1321 | - return new \WP_Error( 'not_found', 'No matching row found to duplicate.' ); | |
| 1322 | - } | |
| 1323 | - | |
| 1324 | - // Apply user-defined modifications (ex: remove ID, change field value) | |
| 1325 | - if ( is_callable( $modifier ) ) { | |
| 1326 | - $row = call_user_func( $modifier, $row ); | |
| 1327 | - | |
| 1328 | - if ( ! is_array( $row ) || empty( $row ) ) { | |
| 1329 | - return new \WP_Error( 'invalid_modified_row', 'Modified row is invalid or empty.' ); | |
| 1330 | - } | |
| 1331 | - } | |
| 1332 | - | |
| 1333 | - // Prepare insert | |
| 1334 | - $columns = array_keys( $row ); | |
| 1335 | - $placeholders = array_fill( 0, count( $columns ), '%s' ); | |
| 1336 | - $values = array_values( $row ); | |
| 1337 | - | |
| 1338 | - $insert_sql = $wpdb->prepare( | |
| 1339 | - "INSERT INTO `$table_name` (`" . implode( '`, `', $columns ) . "`) | |
| 1340 | - VALUES (" . implode( ', ', $placeholders ) . ")", | |
| 1341 | - ...$values | |
| 1342 | - ); | |
| 1343 | - | |
| 1344 | - $result = $wpdb->query( $insert_sql ); | |
| 1345 | - | |
| 1346 | - if ( false === $result ) { | |
| 1347 | - return new \WP_Error( 'insert_failed', 'Failed to insert duplicate row.' ); | |
| 1348 | - } | |
| 1349 | - | |
| 1350 | - return $wpdb->insert_id; | |
| 1351 | - } | |
| 1352 | - | |
| 1353 | - /** | |
| 1354 | - * Get valid sort order. | |
| 1355 | - * | |
| 1356 | - * @since 3.7.1 | |
| 1357 | - * | |
| 1358 | - * @param string $order order. | |
| 1359 | - * | |
| 1360 | - * @return string | |
| 1361 | - */ | |
| 1362 | - public static function get_valid_sort_order( $order ) { | |
| 1363 | - return 'ASC' === strtoupper( $order ) ? 'ASC' : 'DESC'; | |
| 1364 | - } | |
| 1365 | - | |
| 1366 | - /** | |
| 1367 | - * Get the schema of a database table. | |
| 1368 | - * | |
| 1369 | - * @since 3.8.1 | |
| 1370 | - * | |
| 1371 | - * @param string $table_name The name of the database table. | |
| 1372 | - * | |
| 1373 | - * @throws \Exception Throws an exception if there is a database error. | |
| 1374 | - * | |
| 1375 | - * @return array Returns an array of table columns and their details. | |
| 1376 | - */ | |
| 1377 | - public static function get_table_schema( $table_name) { | |
| 1378 | - | |
| 1379 | - global $wpdb; | |
| 1380 | - | |
| 1381 | - $result = $wpdb->get_results( "DESCRIBE {$table_name}", ARRAY_A ); | |
| 1382 | - | |
| 1383 | - // If error occurred then throw new exception. | |
| 1384 | - if ($wpdb->last_error) { | |
| 1385 | - throw new \Exception($wpdb->last_error); | |
| 1386 | - } | |
| 1387 | - | |
| 1388 | - | |
| 1389 | - return $result; | |
| 1390 | - } | |
| 1391 | - | |
| 1392 | 426 | } |