PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.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.1, at class/class-mainwp-db-base.php

353 lines 6.6 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 protected function test_connection() {
69 if ( ! self::ping( $this->wpdb->dbh ) ) {
70 MainWP_Logger::instance()->info( __( 'Trying to reconnect WordPress database connection...', 'mainwp' ) );
71 $this->wpdb->db_connect();
72 }
73 }
74
75 /**
76 * Method table_name()
77 *
78 * Create entire table name.
79 *
80 * @param mixed $suffix Table suffix.
81 * @param null $tablePrefix Table prefix.
82 *
83 * @return string Table name.
84 */
85 protected function table_name( $suffix, $tablePrefix = null ) {
86 return ( null == $tablePrefix ? $this->table_prefix : $tablePrefix ) . $suffix;
87 }
88
89 /**
90 * Method get_my_sql_version()
91 *
92 * Get MySQL Version.
93 *
94 * @return mixed MySQL vresion.
95 */
96 public function get_my_sql_version() {
97 return $this->wpdb->get_var( 'SHOW VARIABLES LIKE "version"', 1 );
98 }
99
100 /**
101 * Method get_row_result()
102 *
103 * Get row result.
104 *
105 * @param mixed $sql SQL Query.
106 *
107 * @return mixed null|Row
108 */
109 public function get_row_result( $sql ) {
110 if ( null == $sql ) {
111 return null;
112 }
113
114 return $this->wpdb->get_row( $sql, OBJECT );
115 }
116
117 /**
118 * Method get_results_result()
119 *
120 * Get Results of result.
121 *
122 * @param mixed $sql SQL query.
123 *
124 * @return mixed null|get_results()
125 */
126 public function get_results_result( $sql ) {
127 if ( null == $sql ) {
128 return null;
129 }
130
131 return $this->wpdb->get_results( $sql, OBJECT_K );
132 }
133
134 /**
135 * Method query()
136 *
137 * SQL Query.
138 *
139 * @param mixed $sql SQL Query.
140 *
141 * @return mixed false|$result.
142 */
143 public function query( $sql ) {
144 if ( null == $sql ) {
145 return false;
146 }
147
148 $result = self::m_query( $sql, $this->wpdb->dbh );
149
150 if ( ! $result || ( 0 == self::num_rows( $result ) ) ) {
151 return false;
152 }
153
154 return $result;
155 }
156
157 /**
158 * Method escape()
159 *
160 * Escape SQL Data.
161 *
162 * @param mixed $data Data to escape.
163 *
164 * @return mixed Escapped SQL Data.
165 */
166 protected function escape( $data ) {
167 if ( function_exists( 'esc_sql' ) ) {
168 return esc_sql( $data );
169 } else {
170 return $this->wpdb->escape( $data );
171 }
172 }
173
174 /**
175 * Method use_mysqli()
176 *
177 * Use MySQLi, Support old & new versions of WordPress (3.9+).
178 *
179 * @return boolean|self false|$instance Instance of \mysqli
180 */
181 public static function use_mysqli() {
182 if ( ! function_exists( '\mysqli_connect' ) ) {
183 return false;
184 }
185 return ( self::$instance->wpdb->dbh instanceof \mysqli );
186 }
187
188 /**
189 * Method ping()
190 *
191 * Ping MySQLi.
192 *
193 * @param mixed $link Query link.
194 *
195 * @return mixed \mysqli_ping
196 */
197 public static function ping( $link ) {
198 if ( self::use_mysqli() ) {
199 return \mysqli_ping( $link );
200 } else {
201 return \mysql_ping( $link );
202 }
203 }
204
205 /**
206 * Method m_query()
207 *
208 * MySQLi m_query.
209 *
210 * @param mixed $query Query params.
211 * @param mixed $link Query link.
212 *
213 * @return mixed \mysqli_query
214 */
215 public static function m_query( $query, $link ) {
216 if ( self::use_mysqli() ) {
217 return \mysqli_query( $link, $query );
218 } else {
219 return \mysql_query( $query, $link );
220 }
221 }
222
223 /**
224 * Method fetch_object()
225 *
226 * Fetch object.
227 *
228 * @param mixed $result Query result.
229 *
230 * @return boolean|mixed false|\mysqli_fetch_object
231 */
232 public static function fetch_object( $result ) {
233 if ( false === $result ) {
234 return false;
235 }
236
237 if ( self::use_mysqli() ) {
238 return \mysqli_fetch_object( $result );
239 } else {
240 return \mysql_fetch_object( $result );
241 }
242 }
243
244 /**
245 * Method free_result()
246 *
247 * MySQLi free result.
248 *
249 * @param mixed $result Query result.
250 *
251 * @return boolean|mixed false|\mysqli_free_result
252 */
253 public static function free_result( $result ) {
254 if ( false === $result ) {
255 return false;
256 }
257
258 if ( self::use_mysqli() ) {
259 return \mysqli_free_result( $result );
260 } else {
261 return \mysql_free_result( $result );
262 }
263 }
264
265 /**
266 * Method data_seek()
267 *
268 * MySQLi data seek.
269 *
270 * @param mixed $result Query result.
271 * @param mixed $offset Query offset.
272 *
273 * @return boolean|mixed false|\mysqli_data_seek
274 */
275 public static function data_seek( $result, $offset ) {
276 if ( false === $result ) {
277 return false;
278 }
279
280 if ( self::use_mysqli() ) {
281 return \mysqli_data_seek( $result, $offset );
282 } else {
283 return \mysql_data_seek( $result, $offset );
284 }
285 }
286
287 /**
288 * Method fetch_array()
289 *
290 * MySQLi fetch array.
291 *
292 * @param mixed $result Query result.
293 * @param null $result_type Query result type.
294 *
295 * @return boolean|mixed false|\mysqli_fetch_array.
296 */
297 public static function fetch_array( $result, $result_type = null ) {
298 if ( false === $result ) {
299 return false;
300 }
301
302 if ( self::use_mysqli() ) {
303 return \mysqli_fetch_array( $result, ( null == $result_type ? MYSQLI_BOTH : $result_type ) );
304 } else {
305 return \mysql_fetch_array( $result, ( null == $result_type ? MYSQL_BOTH : $result_type ) );
306 }
307 }
308
309
310 /**
311 * Method num_rows()
312 *
313 * MySQLi number of rows.
314 *
315 * @param mixed $result Query result.
316 *
317 * @return boolean|mixed false|\mysqli_num_rows.
318 */
319 public static function num_rows( $result ) {
320 if ( false === $result ) {
321 return 0;
322 }
323
324 if ( self::use_mysqli() ) {
325 return \mysqli_num_rows( $result );
326 } else {
327 return \mysql_num_rows( $result );
328 }
329 }
330
331 /**
332 * Method is_result()
333 *
334 * Return instance of \mysqli_result
335 *
336 * @param mixed $result Query result.
337 *
338 * @return boolean|mixed false|\mysqli_result
339 */
340 public static function is_result( $result ) {
341 if ( false === $result ) {
342 return false;
343 }
344
345 if ( self::use_mysqli() ) {
346 return ( $result instanceof \mysqli_result );
347 } else {
348 return is_resource( $result );
349 }
350 }
351 // phpcs:enable
352 }
353