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

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