PluginProbe
Listdom: AI-powered Business Directory with Classifieds Ads Listings / trunk
Listdom: AI-powered Business Directory with Classifieds Ads Listings vtrunk
6.0.0 5.9.0 5.8.1 5.8.0 5.7.0 5.6.0 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.2.0 1.2.1 1.3.0 1.3.1 1.4.0 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.8.0 1.9.0 All 75 releases
listdom / app / includes / db.php

db.php in Listdom: AI-powered Business Directory with Classifieds Ads Listings trunk, at app/includes/db.php

213 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class LSD_db extends LSD_Base
4 {
5 /**
6 * Runs any query
7 * @param string $query
8 * @param string $type
9 * @return mixed
10 */
11 public function q(string $query, string $type = '')
12 {
13 // Apply DB prefix
14 $query = $this->_prefix($query);
15
16 // Converts query type to lowercase
17 $type = strtolower($type);
18
19 // Calls select function if query type is select
20 if ($type === 'select') return $this->select($query);
21
22 // Get WordPress DB object
23 $database = $this->get_DBO();
24
25 // If query type is insert, return the insert id
26 if ($type === 'insert')
27 {
28 $database->query($query);
29 return $database->insert_id;
30 }
31
32 // Run the query and return the result
33 return $database->query($query);
34 }
35
36 /**
37 * Returns records count of a query
38 * @param string $query
39 * @param string $table
40 * @return int
41 */
42 public function num(string $query, string $table = ''): int
43 {
44 // If table is filled, generate the query
45 if (trim($table) !== '')
46 {
47 $query = "SELECT COUNT(*) FROM `#__$table`";
48 }
49
50 // Apply DB prefix
51 $query = $this->_prefix($query);
52
53 // Get WordPress Db object
54 $database = $this->get_DBO();
55 return (int) $database->get_var($query);
56 }
57
58 /**
59 * Selects records from Database
60 * @param string $query
61 * @param string $result
62 * @return mixed
63 */
64 public function select(string $query, string $result = 'loadObjectList')
65 {
66 // Apply DB prefix
67 $query = $this->_prefix($query);
68
69 // Get WordPress DB object
70 $database = $this->get_DBO();
71
72 if ($result === 'loadObjectList') return $database->get_results($query, OBJECT_K);
73 else if ($result === 'loadObject') return $database->get_row($query);
74 else if ($result === 'loadAssocList') return $database->get_results($query, ARRAY_A);
75 else if ($result === 'loadAssoc') return $database->get_row($query, ARRAY_A);
76 else if ($result === 'loadResult') return $database->get_var($query);
77 else if ($result === 'loadColumn') return $database->get_col($query);
78 else return $database->get_results($query, OBJECT_K);
79 }
80
81 /**
82 * Prepares a database query.
83 * @param string $query
84 * @param mixed ...$args
85 * @return string
86 */
87 public function prepare(string $query, ...$args): string
88 {
89 $query = $this->_prefix($query);
90 $database = $this->get_DBO();
91
92 if (count($args) === 1 && is_array($args[0]))
93 {
94 return $database->prepare($query, $args[0]);
95 }
96
97 return $database->prepare($query, ...$args);
98 }
99
100 /**
101 * Replaces a row in a database table.
102 * @param string $table
103 * @param array $data
104 * @param array $format
105 * @return int|false
106 */
107 public function replace(string $table, array $data, array $format = [])
108 {
109 return $this->get_DBO()->replace($this->_prefix($table), $data, $format);
110 }
111
112 /**
113 * Deletes rows from a database table.
114 * @param string $table
115 * @param array $where
116 * @param array $where_format
117 * @return int|false
118 */
119 public function delete(string $table, array $where, array $where_format = [])
120 {
121 return $this->get_DBO()->delete($this->_prefix($table), $where, $where_format);
122 }
123
124 /**
125 * Get a record from Database
126 * @param string|array $selects
127 * @param string $table
128 * @param string $field
129 * @param string $value
130 * @param boolean $object
131 * @param string $condition
132 * @return mixed
133 */
134 public function get($selects, string $table, string $field, string $value, bool $object = true, string $condition = '')
135 {
136 $fields = '';
137
138 if (is_array($selects))
139 {
140 foreach ($selects as $select) $fields .= '`' . $select . '`,';
141 $fields = trim($fields, ' ,');
142 }
143 else
144 {
145 $fields = $selects;
146 }
147
148 // Generate the condition
149 if (trim($condition) === '') $condition = "`$field`='$value'";
150
151 // Generate the query
152 $query = "SELECT $fields FROM `#__$table` WHERE $condition";
153
154 // Apply DB prefix
155 $query = $this->_prefix($query);
156
157 // Get WordPress DB object
158 $database = $this->get_DBO();
159
160 if ($selects !== '*' && !is_array($selects)) return $database->get_var($query);
161 else if ($object) return $database->get_row($query);
162 else return $database->get_row($query, ARRAY_A);
163 }
164
165 /**
166 * Check if a table exist or not
167 * @param string $table
168 * @return bool
169 */
170 public function exists(string $table): bool
171 {
172 $query = "SHOW TABLES LIKE '#__".$table."'";
173 return (bool) $this->select($query, 'loadObject');
174 }
175
176 /**
177 * Apply WordPress table prefix on queries
178 * @param string $query
179 * @return string
180 */
181 public function _prefix(string $query): string
182 {
183 // Get WordPress DB object
184 $wpdb = $this->get_DBO();
185
186 $query = str_replace('#__users', $wpdb->base_prefix . 'users', $query);
187 $query = str_replace('#__usermeta', $wpdb->base_prefix . 'usermeta', $query);
188 $query = str_replace('#__blogs', $wpdb->base_prefix . 'blogs', $query);
189
190 return str_replace('#__', $wpdb->prefix, $query);
191 }
192
193 /**
194 * Returns WordPress DB Object
195 * @return wpdb
196 * @global wpdb $wpdb
197 */
198 public function get_DBO(): wpdb
199 {
200 global $wpdb;
201 return $wpdb;
202 }
203
204 /**
205 * @return mixed
206 */
207 public function version()
208 {
209 $query = "SELECT VERSION();";
210 return $this->select($query, 'loadResult');
211 }
212 }
213