mainwp-child/class/class-mainwp-child-db.php

181 lines
4.4 KiB
PHP
Raw Normal View History

2015-10-15 22:52:37 +10:00
<?php
2020-05-20 00:26:51 -04:00
/**
* MainWP Child DB
*
* This file handles all of the Child Plugin's DB functions.
2020-06-04 21:05:07 +02:00
*
* @package MainWP\Child
2020-05-20 00:26:51 -04:00
*/
2015-10-15 22:52:37 +10:00
2020-05-05 20:13:38 +07:00
namespace MainWP\Child;
2020-05-20 00:26:51 -04:00
/**
* Class MainWP_Child_DB
*
2020-06-04 21:05:07 +02:00
* Handles all of the Child Plugin's DB functions.
2020-05-20 00:26:51 -04:00
*/
2015-10-15 22:52:37 +10:00
class MainWP_Child_DB {
2020-06-04 21:05:07 +02:00
2020-05-07 19:34:36 +07:00
// phpcs:disable WordPress.DB.RestrictedFunctions, WordPress.DB.PreparedSQL.NotPrepared -- unprepared SQL ok, accessing the database directly to custom database functions.
2020-06-04 21:05:07 +02:00
/**
* Support old & new versions of WordPress (3.9+).
*
* @return bool|object Instantiated object of \mysqli.
*/
public static function use_mysqli() {
2020-05-07 19:34:36 +07:00
if ( ! function_exists( '\mysqli_connect' ) ) {
2015-10-15 22:52:37 +10:00
return false;
}
global $wpdb;
2020-05-07 19:34:36 +07:00
return ( $wpdb->dbh instanceof \mysqli );
2015-10-15 22:52:37 +10:00
}
/**
* Run a mysqli query & get a result.
*
2020-06-04 21:05:07 +02:00
* @param string $query An SQL query.
* @param string $link A link identifier.
*
* @return bool|\mysqli_result|resource For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries, mysqli_query()
* will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
* Returns FALSE on failure.
*/
public static function to_query( $query, $link ) {
2015-10-15 22:52:37 +10:00
if ( self::use_mysqli() ) {
2020-05-07 19:34:36 +07:00
return \mysqli_query( $link, $query );
2015-10-15 22:52:37 +10:00
} else {
2020-05-07 19:34:36 +07:00
return \mysql_query( $query, $link );
2015-10-15 22:52:37 +10:00
}
}
/**
* Fetch an array.
*
2020-06-04 21:05:07 +02:00
* @param array $result A result set identifier.
*
* @return array|false|null Returns an array of strings that corresponds to the fetched row, or false if there are no more rows.
*/
public static function fetch_array( $result ) {
2015-10-15 22:52:37 +10:00
if ( self::use_mysqli() ) {
2020-05-07 19:34:36 +07:00
return \mysqli_fetch_array( $result, MYSQLI_ASSOC );
2015-10-15 22:52:37 +10:00
} else {
2020-05-07 19:34:36 +07:00
return \mysql_fetch_array( $result, MYSQL_ASSOC );
2015-10-15 22:52:37 +10:00
}
}
/**
* Count the number of rows.
*
2020-06-04 21:05:07 +02:00
* @param array $result A result set identifier returned.
*
* @return false|int Returns number of rows in the result set.
*/
public static function num_rows( $result ) {
2015-10-15 22:52:37 +10:00
if ( self::use_mysqli() ) {
2020-05-07 19:34:36 +07:00
return \mysqli_num_rows( $result );
2015-10-15 22:52:37 +10:00
} else {
2020-05-07 19:34:36 +07:00
return \mysql_num_rows( $result );
2015-10-15 22:52:37 +10:00
}
}
/**
* Connect to Child Site Database.
*
2020-06-04 21:05:07 +02:00
* @param string $host Can be either a host name or an IP address.
* @param string $user The MySQL user name.
* @param string $pass The MySQL user password.
*
* @return false|\mysqli|resource object which represents the connection to a MySQL Server or false if an error occurred.
*/
public static function connect( $host, $user, $pass ) {
2015-10-15 22:52:37 +10:00
if ( self::use_mysqli() ) {
2020-05-07 19:34:36 +07:00
return \mysqli_connect( $host, $user, $pass );
2015-10-15 22:52:37 +10:00
} else {
2020-05-07 19:34:36 +07:00
return \mysql_connect( $host, $user, $pass );
2015-10-15 22:52:37 +10:00
}
}
/**
* Select Child Site DB.
*
2020-06-04 21:05:07 +02:00
* @param string $db Database name.
*
* @return bool true on success or false on failure.
*/
public static function select_db( $db ) {
2015-10-15 22:52:37 +10:00
if ( self::use_mysqli() ) {
global $wpdb;
2020-05-07 19:34:36 +07:00
return \mysqli_select_db( $wpdb->dbh, $db );
2015-10-15 22:52:37 +10:00
} else {
2020-05-07 19:34:36 +07:00
return \mysql_select_db( $db );
2015-10-15 22:52:37 +10:00
}
}
/**
* Get any mysqli errors.
*
* @return string the error text from the last MySQL function, or '' (empty string) if no error occurred.
*/
public static function error() {
2015-10-15 22:52:37 +10:00
if ( self::use_mysqli() ) {
global $wpdb;
2020-05-07 19:34:36 +07:00
return \mysqli_error( $wpdb->dbh );
2015-10-15 22:52:37 +10:00
} else {
2020-05-07 19:34:36 +07:00
return \mysql_error();
2015-10-15 22:52:37 +10:00
}
}
/**
* Escape a given string.
*
2020-06-04 21:05:07 +02:00
* @param string $value The string to be escaped. Characters encoded are NUL (ASCII 0), \n, \r, \, ', ", and Control-Z.
*
* @return false|string the escaped string, or false on error.
*/
public static function real_escape_string( $value ) {
2015-10-15 22:52:37 +10:00
global $wpdb;
if ( self::use_mysqli() ) {
2020-05-07 19:34:36 +07:00
return \mysqli_real_escape_string( $wpdb->dbh, $value );
2015-10-15 22:52:37 +10:00
} else {
2020-05-07 19:34:36 +07:00
return \mysql_real_escape_string( $value, $wpdb->dbh );
2015-10-15 22:52:37 +10:00
}
}
/**
* Check if $result is an Instantiated object of \mysqli.
*
2020-06-04 21:05:07 +02:00
* @param resource $result Instantiated object of \mysqli.
*
* @return resource|bool Instantiated object of \mysqli, true if var is a resource, false otherwise.
*/
public static function is_result( $result ) {
2015-10-15 22:52:37 +10:00
if ( self::use_mysqli() ) {
return ( $result instanceof mysqli_result );
} else {
return is_resource( $result );
}
}
/**
* Get the size of the DB.
*
* @return int|mixed Size of the DB or false on failure.
*/
public static function get_size() {
2015-10-15 22:52:37 +10:00
global $wpdb;
2020-05-07 19:34:36 +07:00
$rows = self::to_query( 'SHOW table STATUS', $wpdb->dbh );
2015-10-15 22:52:37 +10:00
$size = 0;
2020-03-26 14:05:04 +00:00
while ( $row = self::fetch_array( $rows ) ) {
2015-10-15 22:52:37 +10:00
$size += $row['Data_length'];
}
return $size;
}
}