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.2 6.2.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 All 155 releases
← All changes | class/class-mainwp-db-base.php +304 -349 5.2.2 → 4.4.1 View file →
@@ -13,390 +13,345 @@
13 13 * Class MainWP_DB_Base
14 14 *
15 15 * @package MainWP\Dashboard
16 16 */
17 -class MainWP_DB_Base { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
17 +class MainWP_DB_Base {
18 18
19 - // phpcs:disable WordPress.DB.RestrictedFunctions, WordPress.DB.PreparedSQL.NotPrepared -- unprepared SQL ok, accessing the database directly to custom database functions.
19 + // phpcs:disable WordPress.DB.RestrictedFunctions, WordPress.DB.PreparedSQL.NotPrepared -- unprepared SQL ok, accessing the database directly to custom database functions.
20 20
21 - /**
22 - * Private static instance.
23 - *
24 - * @static
25 - * @var $instance MainWP_DB_Base.
26 - */
27 - private static $instance = null;
21 + /**
22 + * Private static instance.
23 + *
24 + * @static
25 + * @var $instance MainWP_DB_Base.
26 + */
27 + private static $instance = null;
28 28
29 - /**
30 - * Table prefix.
31 - *
32 - * @var string $table_prefix
33 - */
34 - protected $table_prefix;
29 + /**
30 + * Table prefix.
31 + *
32 + * @var string $table_prefix
33 + */
34 + protected $table_prefix;
35 35
36 - /**
37 - * WordPress Database.
38 - *
39 - * @var mixed $wpdb WordPress Database.
40 - */
41 - protected $wpdb;
36 + /**
37 + * WordPress Database.
38 + *
39 + * @var mixed $wpdb WordPress Database.
40 + */
41 + protected $wpdb;
42 42
43 - /**
44 - * MainWP_DB_Base constructor.
45 - *
46 - * Run each time the class is called.
47 - */
48 - public function __construct() {
43 + /**
44 + * MainWP_DB_Base constructor.
45 + *
46 + * Run each time the class is called.
47 + */
48 + public function __construct() {
49 49
50 - self::$instance = $this;
50 + self::$instance = $this;
51 51
52 - /**
53 - * WordPress Database.
54 - *
55 - * @var mixed $wpdb Global WordPress Database.
56 - */
57 - global $wpdb;
52 + /**
53 + * WordPress Database.
54 + *
55 + * @var mixed $wpdb Global WordPress Database.
56 + */
57 + global $wpdb;
58 58
59 - $this->wpdb = &$wpdb;
60 - $this->table_prefix = $wpdb->prefix . 'mainwp_';
61 - }
59 + $this->wpdb = &$wpdb;
60 + $this->table_prefix = $wpdb->prefix . 'mainwp_';
61 + }
62 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 - }
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 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 - }
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 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 + }
91 101
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 - }
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 + }
104 115
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 - }
116 + return $this->wpdb->get_row( $sql, OBJECT );
117 + }
115 118
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 - }
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 + }
129 132
130 - return $this->wpdb->get_row( $sql, OBJECT );
131 - }
133 + return $this->wpdb->get_results( $sql, OBJECT_K );
134 + }
132 135
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 - }
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 + }
146 149
147 - return $this->wpdb->get_results( $sql, OBJECT_K );
148 - }
150 + $result = self::m_query( $sql, $this->wpdb->dbh );
149 151
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 - }
152 + if ( ! $result || ( 0 == self::num_rows( $result ) ) ) {
153 + return false;
154 + }
163 155
164 - $result = static::m_query( $sql, $this->wpdb->dbh ); // NOSONAR - required.
156 + return $result;
157 + }
165 158
166 - if ( ! $result || ( empty( self::num_rows( $result ) ) ) ) { // NOSONAR - required.
167 - return false;
168 - }
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 + }
169 175
170 - return $result;
171 - }
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 + }
172 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 + }
173 206
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 - }
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 + }
182 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 + }
183 238
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 - }
239 + if ( self::use_mysqli() ) {
240 + return \mysqli_fetch_object( $result );
241 + } else {
242 + return \mysql_fetch_object( $result );
243 + }
244 + }
203 245
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 - }
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 + }
220 259
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 - }
260 + if ( self::use_mysqli() ) {
261 + return \mysqli_free_result( $result );
262 + } else {
263 + return \mysql_free_result( $result );
264 + }
265 + }
234 266
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 - }
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 + }
251 281
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 - }
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 + }
269 291
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 - }
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 + }
283 306
284 - if ( self::use_mysqli() ) { // NOSONAR - required.
285 - return \mysqli_fetch_object( $result );
286 - } else {
287 - return \mysql_fetch_object( $result );
288 - }
289 - }
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 + }
290 313
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 314
305 - if ( self::use_mysqli() ) { // NOSONAR - required.
306 - \mysqli_free_result( $result );
307 - } else {
308 - \mysql_free_result( $result );
309 - }
310 - }
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 + }
311 328
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 - }
329 + if ( self::use_mysqli() ) {
330 + return \mysqli_num_rows( $result );
331 + } else {
332 + return \mysql_num_rows( $result );
333 + }
334 + }
326 335
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 + * 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 + }
336 349
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
350 + if ( self::use_mysqli() ) {
351 + return ( $result instanceof \mysqli_result );
352 + } else {
353 + return is_resource( $result );
354 + }
355 + }
356 + // phpcs:enable
402 357 }