PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.4.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.4.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-db-base.php

class-mainwp-db-base.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 4.4.1, at class/class-mainwp-db-base.php

358 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Database Controller
4 *
5 * This file handles all interactions with the DB.
6 *
7 * @package MainWP/Dashboard
8 */
9
10 namespace MainWP\Dashboard;
11
12 /**
13 * Class MainWP_DB_Base
14 *
15 * @package MainWP\Dashboard
16 */
17 class MainWP_DB_Base {
18
19 // phpcs:disable WordPress.DB.RestrictedFunctions, WordPress.DB.PreparedSQL.NotPrepared -- unprepared SQL ok, accessing the database directly to custom database functions.
20
21 /**
22 * Private static instance.
23 *
24 * @static
25 * @var $instance MainWP_DB_Base.
26 */
27 private static $instance = null;
28
29 /**
30 * Table prefix.
31 *
32 * @var string $table_prefix
33 */
34 protected $table_prefix;
35
36 /**
37 * WordPress Database.
38 *
39 * @var mixed $wpdb WordPress Database.
40 */
41 protected $wpdb;
42
43 /**
44 * MainWP_DB_Base constructor.
45 *
46 * Run each time the class is called.
47 */
48 public function __construct() {
49
50 self::$instance = $this;
51
52 /**
53 * WordPress Database.
54 *
55 * @var mixed $wpdb Global WordPress Database.
56 */
57 global $wpdb;
58
59 $this->wpdb = &$wpdb;
60 $this->table_prefix = $wpdb->prefix . 'mainwp_';
61 }
62
63 /**
64 * Method test_connection()
65 *
66 * Test db connection.
67 *
68 * @uses \MainWP\Dashboard\MainWP_Logger::info()
69 */
70 protected function test_connection() {
71 if ( ! self::ping( $this->wpdb->dbh ) ) {
72 MainWP_Logger::instance()->info( esc_html__( 'Trying to reconnect WordPress database connection...', 'mainwp' ) );
73 $this->wpdb->db_connect();
74 }
75 }
76
77 /**
78 * Method table_name()
79 *
80 * Create entire table name.
81 *
82 * @param mixed $suffix Table suffix.
83 * @param null $tablePrefix Table prefix.
84 *
85 * @return string Table name.
86 */
87 protected function table_name( $suffix, $tablePrefix = null ) {
88 return ( null == $tablePrefix ? $this->table_prefix : $tablePrefix ) . $suffix;
89 }
90
91 /**
92 * Method get_my_sql_version()
93 *
94 * Get MySQL Version.
95 *
96 * @return mixed MySQL vresion.
97 */
98 public function get_my_sql_version() {
99 return $this->wpdb->get_var( 'SHOW VARIABLES LIKE "version"', 1 );
100 }
101
102 /**
103 * Method get_row_result()
104 *
105 * Get row result.
106 *
107 * @param mixed $sql SQL Query.
108 *
109 * @return mixed null|Row
110 */
111 public function get_row_result( $sql ) {
112 if ( null == $sql ) {
113 return null;
114 }
115
116 return $this->wpdb->get_row( $sql, OBJECT );
117 }
118
119 /**
120 * Method get_results_result()
121 *
122 * Get Results of result.
123 *
124 * @param mixed $sql SQL query.
125 *
126 * @return mixed null|get_results()
127 */
128 public function get_results_result( $sql ) {
129 if ( null == $sql ) {
130 return null;
131 }
132
133 return $this->wpdb->get_results( $sql, OBJECT_K );
134 }
135
136 /**
137 * Method query()
138 *
139 * SQL Query.
140 *
141 * @param mixed $sql SQL Query.
142 *
143 * @return mixed false|$result.
144 */
145 public function query( $sql ) {
146 if ( null == $sql ) {
147 return false;
148 }
149
150 $result = self::m_query( $sql, $this->wpdb->dbh );
151
152 if ( ! $result || ( 0 == self::num_rows( $result ) ) ) {
153 return false;
154 }
155
156 return $result;
157 }
158
159 /**
160 * Method escape()
161 *
162 * Escape SQL Data.
163 *
164 * @param mixed $data Data to escape.
165 *
166 * @return mixed Escapped SQL Data.
167 */
168 public function escape( $data ) {
169 if ( function_exists( 'esc_sql' ) ) {
170 return esc_sql( $data );
171 } else {
172 return $this->wpdb->escape( $data );
173 }
174 }
175
176 /**
177 * Method use_mysqli()
178 *
179 * Use MySQLi, Support old & new versions of WordPress (3.9+).
180 *
181 * @return boolean|self false|$instance Instance of \mysqli
182 */
183 public static function use_mysqli() {
184 if ( ! function_exists( '\mysqli_connect' ) ) {
185 return false;
186 }
187 return ( self::$instance->wpdb->dbh instanceof \mysqli );
188 }
189
190 /**
191 * Method ping()
192 *
193 * Ping MySQLi.
194 *
195 * @param mixed $link Query link.
196 *
197 * @return mixed \mysqli_ping
198 */
199 public static function ping( $link ) {
200 if ( self::use_mysqli() ) {
201 return \mysqli_ping( $link );
202 } else {
203 return \mysql_ping( $link );
204 }
205 }
206
207 /**
208 * Method m_query()
209 *
210 * MySQLi m_query.
211 *
212 * @param mixed $query Query params.
213 * @param mixed $link Query link.
214 *
215 * @return mixed \mysqli_query
216 */
217 public static function m_query( $query, $link ) {
218 if ( self::use_mysqli() ) {
219 return \mysqli_query( $link, $query );
220 } else {
221 return \mysql_query( $query, $link );
222 }
223 }
224
225 /**
226 * Method fetch_object()
227 *
228 * Fetch object.
229 *
230 * @param mixed $result Query result.
231 *
232 * @return boolean|mixed false|\mysqli_fetch_object
233 */
234 public static function fetch_object( $result ) {
235 if ( is_bool( $result ) ) {
236 return $result;
237 }
238
239 if ( self::use_mysqli() ) {
240 return \mysqli_fetch_object( $result );
241 } else {
242 return \mysql_fetch_object( $result );
243 }
244 }
245
246 /**
247 * Method free_result()
248 *
249 * MySQLi free result.
250 *
251 * @param mixed $result Query result.
252 *
253 * @return boolean|mixed false|\mysqli_free_result
254 */
255 public static function free_result( $result ) {
256 if ( is_bool( $result ) ) {
257 return $result;
258 }
259
260 if ( self::use_mysqli() ) {
261 return \mysqli_free_result( $result );
262 } else {
263 return \mysql_free_result( $result );
264 }
265 }
266
267 /**
268 * Method data_seek()
269 *
270 * MySQLi data seek.
271 *
272 * @param mixed $result Query result.
273 * @param mixed $offset Query offset.
274 *
275 * @return boolean|mixed false|\mysqli_data_seek
276 */
277 public static function data_seek( $result, $offset ) {
278 if ( is_bool( $result ) ) {
279 return $result;
280 }
281
282 if ( self::use_mysqli() ) {
283 if ( ! ( $result instanceof \mysqli_result ) ) {
284 return $result;
285 }
286 return \mysqli_data_seek( $result, $offset );
287 } else {
288 return \mysql_data_seek( $result, $offset );
289 }
290 }
291
292 /**
293 * Method fetch_array()
294 *
295 * MySQLi fetch array.
296 *
297 * @param mixed $result Query result.
298 * @param null $result_type Query result type.
299 *
300 * @return boolean|mixed false|\mysqli_fetch_array.
301 */
302 public static function fetch_array( $result, $result_type = null ) {
303 if ( is_bool( $result ) ) {
304 return $result;
305 }
306
307 if ( self::use_mysqli() ) {
308 return \mysqli_fetch_array( $result, ( null == $result_type ? MYSQLI_BOTH : $result_type ) );
309 } else {
310 return \mysql_fetch_array( $result, ( null == $result_type ? MYSQL_BOTH : $result_type ) );
311 }
312 }
313
314
315 /**
316 * Method num_rows()
317 *
318 * MySQLi number of rows.
319 *
320 * @param mixed $result Query result.
321 *
322 * @return boolean|mixed false|\mysqli_num_rows.
323 */
324 public static function num_rows( $result ) {
325 if ( ! self::is_result( $result ) ) {
326 return false;
327 }
328
329 if ( self::use_mysqli() ) {
330 return \mysqli_num_rows( $result );
331 } else {
332 return \mysql_num_rows( $result );
333 }
334 }
335
336 /**
337 * Method is_result()
338 *
339 * Return instance of \mysqli_result
340 *
341 * @param mixed $result Query result.
342 *
343 * @return boolean|mixed false|\mysqli_result
344 */
345 public static function is_result( $result ) {
346 if ( is_bool( $result ) ) {
347 return $result;
348 }
349
350 if ( self::use_mysqli() ) {
351 return ( $result instanceof \mysqli_result );
352 } else {
353 return is_resource( $result );
354 }
355 }
356 // phpcs:enable
357 }
358