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

419 lines 9.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 { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
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 ( ! static::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 /**
93 * Method get_table_name()
94 *
95 * Create entire table name.
96 *
97 * @param mixed $suffix Table suffix.
98 *
99 * @return string Table name.
100 */
101 public function get_table_name( $suffix ) {
102 return $this->table_name( $suffix );
103 }
104
105 /**
106 * Method get_my_sql_version()
107 *
108 * Get MySQL Version.
109 *
110 * @return mixed MySQL vresion.
111 */
112 public function get_my_sql_version() {
113 return $this->wpdb->get_var( 'SHOW VARIABLES LIKE "version"', 1 );
114 }
115
116 /**
117 * Method get_var_field()
118 *
119 * @param string $sql SQL Query.
120 *
121 * @return mixed
122 */
123 public function get_var_field( $sql ) {
124 return $this->wpdb->get_var( $sql );
125 }
126
127 /**
128 * Method get_row_result()
129 *
130 * Get row result.
131 *
132 * @param mixed $sql SQL Query.
133 * @param int $obj OBJECT|ARRAY_A.
134 *
135 * @return mixed null|Row
136 */
137 public function get_row_result( $sql, $obj = OBJECT ) {
138 if ( null === $sql ) {
139 return null;
140 }
141
142 if ( ! in_array( $obj, array( OBJECT, ARRAY_A ) ) ) {
143 $obj = OBJECT;
144 }
145
146 return $this->wpdb->get_row( $sql, $obj );
147 }
148
149 /**
150 * Method get_results_result()
151 *
152 * Get Results of result.
153 *
154 * @param mixed $sql SQL query.
155 *
156 * @return mixed null|get_results()
157 */
158 public function get_results_result( $sql ) {
159 if ( null === $sql ) {
160 return null;
161 }
162
163 return $this->wpdb->get_results( $sql, OBJECT_K );
164 }
165
166 /**
167 * Method query()
168 *
169 * SQL Query.
170 *
171 * @param mixed $sql SQL Query.
172 *
173 * @return mixed false|$result.
174 */
175 public function query( $sql ) {
176 if ( null === $sql ) {
177 return false;
178 }
179
180 $result = static::m_query( $sql, $this->wpdb->dbh ); // NOSONAR - required.
181
182 if ( ! $result || ( empty( self::num_rows( $result ) ) ) ) { // NOSONAR - required.
183 return false;
184 }
185
186 return $result;
187 }
188
189
190 /**
191 * Method get_last_query()
192 *
193 * @return string Get last query.
194 */
195 public function get_last_query() {
196 return $this->wpdb->last_query;
197 }
198
199
200 /**
201 * Method escape_array()
202 *
203 * Escape SQL Data.
204 *
205 * @param mixed $data_arr Data array to escape.
206 *
207 * @return mixed Escapped SQL Data.
208 */
209 public function escape_array( $data_arr ) {
210 if ( ! is_array( $data_arr ) || empty( $data_arr ) ) {
211 return false;
212 }
213 $tmp_arr = array();
214 foreach ( $data_arr as $dt ) {
215 $tmp_arr[] = $this->escape( $dt );
216 }
217 return $tmp_arr;
218 }
219
220 /**
221 * Method escape()
222 *
223 * Escape SQL Data.
224 *
225 * @param mixed $data Data to escape.
226 *
227 * @return mixed Escapped SQL Data.
228 */
229 public function escape( $data ) {
230 if ( function_exists( 'esc_sql' ) ) {
231 return esc_sql( $data );
232 } else {
233 return $this->wpdb->escape( $data );
234 }
235 }
236
237 /**
238 * Method use_mysqli()
239 *
240 * Use MySQLi, Support old & new versions of WordPress (3.9+).
241 *
242 * @return boolean|self false|$instance Instance of \mysqli
243 */
244 public static function use_mysqli() {
245 if ( ! function_exists( '\mysqli_connect' ) ) {
246 return false;
247 }
248 return self::$instance->wpdb->dbh instanceof \mysqli; // NOSONAR - required.
249 }
250
251 /**
252 * Method ping()
253 *
254 * Ping MySQLi.
255 *
256 * @param mixed $link Query link.
257 *
258 * @return mixed \mysqli_ping
259 */
260 public static function ping( $link ) {
261 if ( self::use_mysqli() ) { // NOSONAR - required.
262 return \mysqli_ping( $link );
263 } else {
264 return \mysql_ping( $link );
265 }
266 }
267
268 /**
269 * Method m_query()
270 *
271 * MySQLi m_query.
272 *
273 * @param mixed $query Query params.
274 * @param mixed $link Query link.
275 *
276 * @return mixed \mysqli_query
277 */
278 public static function m_query( $query, $link ) {
279 if ( self::use_mysqli() ) { // NOSONAR - required.
280 return \mysqli_query( $link, $query );
281 } else {
282 return \mysql_query( $query, $link );
283 }
284 }
285
286 /**
287 * Method fetch_object()
288 *
289 * Fetch object.
290 *
291 * @param mixed $result Query result.
292 *
293 * @return boolean|mixed false|\mysqli_fetch_object
294 */
295 public static function fetch_object( $result ) {
296 if ( is_bool( $result ) ) {
297 return $result;
298 }
299
300 if ( self::use_mysqli() ) { // NOSONAR - required.
301 return \mysqli_fetch_object( $result );
302 } else {
303 return \mysql_fetch_object( $result );
304 }
305 }
306
307 /**
308 * Method free_result()
309 *
310 * MySQLi free result.
311 *
312 * @param mixed $result Query result.
313 *
314 * @return boolean|mixed false|\mysqli_free_result
315 */
316 public static function free_result( $result ) {
317 if ( is_bool( $result ) ) {
318 return $result;
319 }
320
321 if ( self::use_mysqli() ) { // NOSONAR - required.
322 \mysqli_free_result( $result );
323 } else {
324 \mysql_free_result( $result );
325 }
326 }
327
328 /**
329 * Method data_seek()
330 *
331 * MySQLi data seek.
332 *
333 * @param mixed $result Query result.
334 * @param mixed $offset Query offset.
335 *
336 * @return boolean|mixed false|\mysqli_data_seek
337 */
338 public static function data_seek( $result, $offset ) {
339 if ( is_bool( $result ) ) {
340 return $result;
341 }
342
343 if ( self::use_mysqli() ) { // NOSONAR - required.
344 if ( ! ( $result instanceof \mysqli_result ) ) {
345 return $result;
346 }
347 return \mysqli_data_seek( $result, $offset );
348 } else {
349 return \mysql_data_seek( $result, $offset );
350 }
351 }
352
353 /**
354 * Method fetch_array()
355 *
356 * MySQLi fetch array.
357 *
358 * @param mixed $result Query result.
359 * @param null $result_type Query result type.
360 *
361 * @return boolean|mixed false|\mysqli_fetch_array.
362 */
363 public static function fetch_array( $result, $result_type = null ) {
364 if ( is_bool( $result ) ) {
365 return $result;
366 }
367
368 if ( self::use_mysqli() ) { // NOSONAR - required.
369 return \mysqli_fetch_array( $result, ( null === $result_type ? MYSQLI_BOTH : $result_type ) );
370 } else {
371 return \mysql_fetch_array( $result, ( null === $result_type ? MYSQL_BOTH : $result_type ) );
372 }
373 }
374
375
376 /**
377 * Method num_rows()
378 *
379 * MySQLi number of rows.
380 *
381 * @param mixed $result Query result.
382 *
383 * @return boolean|mixed false|\mysqli_num_rows.
384 */
385 public static function num_rows( $result ) {
386 if ( ! self::is_result( $result ) ) { // NOSONAR - required.
387 return false;
388 }
389
390 if ( self::use_mysqli() ) { // NOSONAR - required.
391 return \mysqli_num_rows( $result );
392 } else {
393 return \mysql_num_rows( $result );
394 }
395 }
396
397 /**
398 * Method is_result()
399 *
400 * Return instance of \mysqli_result
401 *
402 * @param mixed $result Query result.
403 *
404 * @return boolean|mixed false|\mysqli_result
405 */
406 public static function is_result( $result ) {
407 if ( is_bool( $result ) ) {
408 return $result;
409 }
410
411 if ( self::use_mysqli() ) { // NOSONAR - required.
412 return $result instanceof \mysqli_result;
413 } else {
414 return is_resource( $result );
415 }
416 }
417 // phpcs:enable
418 }
419