QueryHelper.php
427 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Query helper class contains static helper methods to perform basic |
| 4 | * operations |
| 5 | * |
| 6 | * @package Tutor\Helper |
| 7 | * @since v2.0.7 |
| 8 | */ |
| 9 | |
| 10 | namespace Tutor\Helpers; |
| 11 | |
| 12 | /** |
| 13 | * Do the common db operations through helper |
| 14 | * methods |
| 15 | */ |
| 16 | class QueryHelper { |
| 17 | |
| 18 | /** |
| 19 | * Insert data in the table |
| 20 | * |
| 21 | * @param string $table table name. |
| 22 | * @param array $data | data to insert in the table. |
| 23 | * |
| 24 | * @return int, inserted id. |
| 25 | * |
| 26 | * @since v2.0.7 |
| 27 | */ |
| 28 | public static function insert( string $table, array $data ): int { |
| 29 | global $wpdb; |
| 30 | // Sanitize text field. |
| 31 | $data = array_map( |
| 32 | function( $value ) { |
| 33 | return sanitize_text_field( $value ); |
| 34 | }, |
| 35 | $data |
| 36 | ); |
| 37 | |
| 38 | $insert = $wpdb->insert( |
| 39 | $table, |
| 40 | $data |
| 41 | ); |
| 42 | return $insert ? $wpdb->insert_id : 0; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Update data |
| 47 | * |
| 48 | * @param string $table table name. |
| 49 | * @param array $data | data to update in the table. |
| 50 | * @param array $where | condition array. |
| 51 | * |
| 52 | * @return bool, true on success false on failure |
| 53 | * |
| 54 | * @since v2.0.7 |
| 55 | */ |
| 56 | public static function update( string $table, array $data, array $where ): bool { |
| 57 | global $wpdb; |
| 58 | // Sanitize text field. |
| 59 | $data = array_map( |
| 60 | function( $value ) { |
| 61 | return sanitize_text_field( $value ); |
| 62 | }, |
| 63 | $data |
| 64 | ); |
| 65 | |
| 66 | $where = array_map( |
| 67 | function( $value ) { |
| 68 | return sanitize_text_field( $value ); |
| 69 | }, |
| 70 | $where |
| 71 | ); |
| 72 | |
| 73 | $update = $wpdb->update( |
| 74 | $table, |
| 75 | $data, |
| 76 | $where |
| 77 | ); |
| 78 | return $update ? true : false; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Delete rows from table |
| 83 | * |
| 84 | * @param string $table table name. |
| 85 | * @param array $where key value pairs.Where key is the name of |
| 86 | * column & value is the value to match. |
| 87 | * For ex: [ 'id' => 1 ]. |
| 88 | * |
| 89 | * @since v2.0.7 |
| 90 | */ |
| 91 | public static function delete( string $table, array $where ): bool { |
| 92 | global $wpdb; |
| 93 | $delete = $wpdb->delete( |
| 94 | $table, |
| 95 | $where |
| 96 | ); |
| 97 | return $delete ? true : false; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Clean everything from table |
| 102 | * |
| 103 | * @since v2.0.7 |
| 104 | * |
| 105 | * @param string $table table name. |
| 106 | * |
| 107 | * @return bool |
| 108 | */ |
| 109 | public static function table_clean( string $table ): bool { |
| 110 | global $wpdb; |
| 111 | $delete = $wpdb->query( |
| 112 | $wpdb->prepare( |
| 113 | "DELETE FROM |
| 114 | {$table} |
| 115 | WHERE 1 = %d |
| 116 | ", |
| 117 | 1 |
| 118 | ) |
| 119 | ); |
| 120 | return $delete ? true : false; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Insert multiple rows without knowing key value |
| 125 | * |
| 126 | * @since v2.0.7 |
| 127 | * |
| 128 | * @param string $table table name. |
| 129 | * @param array $request two dimensional array |
| 130 | * for ex: [ [id => 1], [id => 2] ]. |
| 131 | * |
| 132 | * @return mixed wpdb response true or int on success, |
| 133 | * false on failure |
| 134 | */ |
| 135 | public static function insert_multiple_rows( $table, $request ) { |
| 136 | global $wpdb; |
| 137 | $column_keys = ''; |
| 138 | $column_values = ''; |
| 139 | $sql = ''; |
| 140 | $last_key = array_key_last( $request ); |
| 141 | $first_key = array_key_first( $request ); |
| 142 | foreach ( $request as $k => $value ) { |
| 143 | $keys = array_keys( $value ); |
| 144 | |
| 145 | // Prepare column keys & values. |
| 146 | foreach ( $keys as $v ) { |
| 147 | $column_keys .= sanitize_key( $v ) . ','; |
| 148 | $sanitize_value = sanitize_text_field( $value[ $v ] ); |
| 149 | $column_values .= is_numeric( $sanitize_value ) ? $sanitize_value . ',' : "'$sanitize_value'" . ','; |
| 150 | } |
| 151 | // Trim trailing comma. |
| 152 | $column_keys = rtrim( $column_keys, ',' ); |
| 153 | $column_values = rtrim( $column_values, ',' ); |
| 154 | if ( $first_key === $k ) { |
| 155 | $sql .= "INSERT INTO {$table} ($column_keys) VALUES ($column_values),"; |
| 156 | } elseif ( $last_key == $k ) { |
| 157 | $sql .= "($column_values)"; |
| 158 | } else { |
| 159 | $sql .= "($column_values),"; |
| 160 | } |
| 161 | |
| 162 | // Reset keys & values to avoid duplication. |
| 163 | $column_keys = ''; |
| 164 | $column_values = ''; |
| 165 | } |
| 166 | return $wpdb->query( $sql ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Build where clause string |
| 171 | * |
| 172 | * @param array $where assoc array with field and value |
| 173 | * @return string |
| 174 | * |
| 175 | * @since 2.0.9 |
| 176 | */ |
| 177 | private function build_where_clause( array $where ) { |
| 178 | $arr = array(); |
| 179 | foreach ( $where as $field => $value ) { |
| 180 | $value = is_numeric( $value ) ? ( $value + 0 ) : "'" . $value . "'"; |
| 181 | $arr[] = "{$field}={$value}"; |
| 182 | } |
| 183 | |
| 184 | return implode( ' AND ', $arr ); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Sanitize assoc array |
| 189 | * |
| 190 | * @param array $array an assoc array |
| 191 | * @return array |
| 192 | * |
| 193 | * @since 2.0.9 |
| 194 | */ |
| 195 | private function sanitize_assoc_array( array $array ) { |
| 196 | return array_map( |
| 197 | function( $value ) { |
| 198 | return sanitize_text_field( $value ); |
| 199 | }, |
| 200 | $array |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Delete comment with associate meta data |
| 206 | * |
| 207 | * @param array $where associative array with field and value. |
| 208 | * Example: array( 'comment_type' => 'comment', 'comment_id' => 1 ) |
| 209 | * @return bool |
| 210 | * |
| 211 | * @since 2.0.9 |
| 212 | */ |
| 213 | public static function delete_comment_with_meta( array $where ) { |
| 214 | if ( count( $where ) === 0 || ! tutor_utils()->is_assoc( $where ) ) { |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | $obj = new self(); |
| 219 | $where = $obj->build_where_clause( $obj->sanitize_assoc_array( $where ) ); |
| 220 | |
| 221 | global $wpdb; |
| 222 | $ids = $wpdb->get_col( "SELECT comment_id FROM {$wpdb->comments} WHERE {$where}" ); |
| 223 | |
| 224 | if ( is_array( $ids ) && count( $ids ) ) { |
| 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}" ); |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Delete post with associate meta data |
| 239 | * |
| 240 | * @param array $where associative array with field and value. |
| 241 | * Example: array( 'post_type' => 'post', 'id' => 1 ) |
| 242 | * @return bool |
| 243 | * |
| 244 | * @since 2.0.9 |
| 245 | */ |
| 246 | public static function delete_post_with_meta( array $where ) { |
| 247 | if ( count( $where ) === 0 || ! tutor_utils()->is_assoc( $where ) ) { |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | $obj = new self(); |
| 252 | $where = $obj->build_where_clause( $obj->sanitize_assoc_array( $where ) ); |
| 253 | |
| 254 | global $wpdb; |
| 255 | $ids = $wpdb->get_col( "SELECT id FROM {$wpdb->posts} WHERE {$where}" ); |
| 256 | |
| 257 | if ( is_array( $ids ) && count( $ids ) ) { |
| 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}" ); |
| 263 | |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Get a single row from any table with where clause |
| 272 | * |
| 273 | * @param string $table table name with prefix. |
| 274 | * |
| 275 | * @param array $where assoc_array. For ex: [col_name => value ]. |
| 276 | * @param string $order_by order by column name. |
| 277 | * @param string $order DESC or ASC, default is DESC. |
| 278 | * @param string $output expected output type, default is object. |
| 279 | * |
| 280 | * @return mixed based on output param, default object |
| 281 | */ |
| 282 | public static function get_row( string $table, array $where, string $order_by, string $order = 'DESC', string $output = 'OBJECT' ) { |
| 283 | global $wpdb; |
| 284 | $obj = new self(); |
| 285 | $where_clause = $obj->build_where_clause( $where ); |
| 286 | $query = $wpdb->prepare( |
| 287 | "SELECT * |
| 288 | FROM {$table} |
| 289 | WHERE {$where_clause} |
| 290 | ORDER BY {$order_by} {$order} |
| 291 | LIMIT %d |
| 292 | ", |
| 293 | 1 |
| 294 | ); |
| 295 | return $wpdb->get_row( |
| 296 | $query, |
| 297 | $output |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Get all row from any table with where clause |
| 303 | * |
| 304 | * @since 2.2.1 |
| 305 | * |
| 306 | * @param string $table table name with prefix. |
| 307 | * |
| 308 | * @param array $where assoc_array. For ex: [col_name => value ]. |
| 309 | * @param string $order_by order by column name. |
| 310 | * @param int $limit default is 1000. |
| 311 | * @param string $order DESC or ASC, default is DESC. |
| 312 | * @param string $output expected output type, default is object. |
| 313 | * |
| 314 | * @return mixed based on output param, default object |
| 315 | */ |
| 316 | public static function get_all( string $table, array $where, string $order_by, $limit = 1000, string $order = 'DESC', string $output = 'OBJECT' ) { |
| 317 | global $wpdb; |
| 318 | $obj = new self(); |
| 319 | $where_clause = $obj->build_where_clause( $where ); |
| 320 | $limit = sanitize_text_field( $limit ); |
| 321 | $query = $wpdb->prepare( |
| 322 | "SELECT * |
| 323 | FROM {$table} |
| 324 | WHERE {$where_clause} |
| 325 | ORDER BY {$order_by} {$order} |
| 326 | LIMIT %d |
| 327 | ", |
| 328 | $limit |
| 329 | ); |
| 330 | return $wpdb->get_results( |
| 331 | $query, |
| 332 | $output |
| 333 | ); |
| 334 | } |
| 335 | |
| 336 | |
| 337 | /** |
| 338 | * Update multiple rows by using where in |
| 339 | * clause |
| 340 | * |
| 341 | * @since v2.1.0 |
| 342 | * |
| 343 | * @param string $table table name. |
| 344 | * @param array $data assoc_array data to update |
| 345 | * ex: [id => 2, name => 'john' ]. |
| 346 | * @param string $where_in comma separated values, ex: 1,2,3. |
| 347 | * @param string $where_col default is ID but could be other. |
| 348 | * |
| 349 | * @return bool true on success, false on failure |
| 350 | */ |
| 351 | public static function update_where_in( string $table, array $data, string $where_in, string $where_col = 'ID' ) { |
| 352 | global $wpdb; |
| 353 | if ( empty( $where_in ) || empty( $where_col ) ) { |
| 354 | return false; |
| 355 | } |
| 356 | $set_clause = self::prepare_set_clause( $data ); |
| 357 | if ( '' === $set_clause ) { |
| 358 | return false; |
| 359 | } |
| 360 | // @codingStandardsIgnoreStart |
| 361 | $query = $wpdb->prepare( |
| 362 | "UPDATE {$table} |
| 363 | {$set_clause} |
| 364 | WHERE $where_col IN ( $where_in ) |
| 365 | AND 1 = %d |
| 366 | ", |
| 367 | 1 |
| 368 | ); |
| 369 | return $wpdb->query( $query ) ? true : false; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Prepare MySQL SET clause for update query |
| 374 | * |
| 375 | * @since v2.1.0 |
| 376 | * |
| 377 | * @param array $data single dimension assoc_array. |
| 378 | * |
| 379 | * @return string |
| 380 | */ |
| 381 | public static function prepare_set_clause( array $data ) { |
| 382 | $set = ''; |
| 383 | foreach ( $data as $key => $value ) { |
| 384 | if ( $key === array_key_first ( $data ) ) { |
| 385 | $set .= "SET "; |
| 386 | } |
| 387 | // Multi dimension not allowed. |
| 388 | if ( is_array( $value ) ) { |
| 389 | continue; |
| 390 | } |
| 391 | $value = sanitize_text_field( $value ); |
| 392 | $set .= is_numeric( $value ) ? "$key = $value" : "$key = '" . $value ."'"; |
| 393 | $set .= ","; |
| 394 | } |
| 395 | return rtrim( $set, ',' ); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Make sanitized SQL IN clause value from an array |
| 400 | * |
| 401 | * @param array $arr a sequentital array. |
| 402 | * @return string |
| 403 | * @since 2.1.1 |
| 404 | */ |
| 405 | public static function prepare_in_clause( array $arr ) { |
| 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 |
| 422 | ); |
| 423 | |
| 424 | return implode( ',', $escaped ); |
| 425 | } |
| 426 | } |
| 427 |