<?php
namespace MainWP\Dashboard;
class MainWP_DB_Base {
private static $instance = null;
protected $table_prefix;
protected $wpdb;
public function __construct() {
self::$instance = $this;
global $wpdb;
$this->wpdb = &$wpdb;
$this->table_prefix = $wpdb->prefix . 'mainwp_';
}
protected function test_connection() {
if ( ! self::ping( $this->wpdb->dbh ) ) {
MainWP_Logger::instance()->info( esc_html__( 'Trying to reconnect WordPress database connection...', 'mainwp' ) );
$this->wpdb->db_connect();
}
}
protected function table_name( $suffix, $tablePrefix = null ) {
return ( null === $tablePrefix ? $this->table_prefix : $tablePrefix ) . $suffix;
}
public function get_table_name( $suffix ) {
return $this->table_name( $suffix );
}
public function get_my_sql_version() {
return $this->wpdb->get_var( 'SHOW VARIABLES LIKE "version"', 1 );
}
public function get_row_result( $sql ) {
if ( null === $sql ) {
return null;
}
return $this->wpdb->get_row( $sql, OBJECT );
}
public function get_results_result( $sql ) {
if ( null === $sql ) {
return null;
}
return $this->wpdb->get_results( $sql, OBJECT_K );
}
public function query( $sql ) {
if ( null === $sql ) {
return false;
}
$result = self::m_query( $sql, $this->wpdb->dbh );
if ( ! $result || ( empty( self::num_rows( $result ) ) ) ) {
return false;
}
return $result;
}
public function escape( $data ) {
if ( function_exists( 'esc_sql' ) ) {
return esc_sql( $data );
} else {
return $this->wpdb->escape( $data );
}
}
public static function use_mysqli() {
if ( ! function_exists( '\mysqli_connect' ) ) {
return false;
}
return ( self::$instance->wpdb->dbh instanceof \mysqli );
}
public static function ping( $link ) {
if ( self::use_mysqli() ) {
return \mysqli_ping( $link );
} else {
return \mysql_ping( $link );
}
}
public static function m_query( $query, $link ) {
if ( self::use_mysqli() ) {
return \mysqli_query( $link, $query );
} else {
return \mysql_query( $query, $link );
}
}
public static function fetch_object( $result ) {
if ( is_bool( $result ) ) {
return $result;
}
if ( self::use_mysqli() ) {
return \mysqli_fetch_object( $result );
} else {
return \mysql_fetch_object( $result );
}
}
public static function free_result( $result ) {
if ( is_bool( $result ) ) {
return $result;
}
if ( self::use_mysqli() ) {
return \mysqli_free_result( $result );
} else {
return \mysql_free_result( $result );
}
}
public static function data_seek( $result, $offset ) {
if ( is_bool( $result ) ) {
return $result;
}
if ( self::use_mysqli() ) {
if ( ! ( $result instanceof \mysqli_result ) ) {
return $result;
}
return \mysqli_data_seek( $result, $offset );
} else {
return \mysql_data_seek( $result, $offset );
}
}
public static function fetch_array( $result, $result_type = null ) {
if ( is_bool( $result ) ) {
return $result;
}
if ( self::use_mysqli() ) {
return \mysqli_fetch_array( $result, ( null === $result_type ? MYSQLI_BOTH : $result_type ) );
} else {
return \mysql_fetch_array( $result, ( null === $result_type ? MYSQL_BOTH : $result_type ) );
}
}
public static function num_rows( $result ) {
if ( ! self::is_result( $result ) ) {
return false;
}
if ( self::use_mysqli() ) {
return \mysqli_num_rows( $result );
} else {
return \mysql_num_rows( $result );
}
}
public static function is_result( $result ) {
if ( is_bool( $result ) ) {
return $result;
}
if ( self::use_mysqli() ) {
return ( $result instanceof \mysqli_result );
} else {
return is_resource( $result );
}
}
}