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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / helpers / QueryHelper.php
tutor / helpers Last commit date
QueryHelper.php 3 years ago
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 }