QueryHelper.php
168 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 | } |