<?php
namespace Koselig\Proxy;
use Cache;
use DB;
use Illuminate\Database\QueryException;
use wpdb;
class WordpressDatabase extends wpdb
{
public function __construct()
{
parent::__construct(null, null, null, null);
}
public function select($db, $dbh = null)
{
}
public function check_connection($allowBail = true)
{
return true;
}
public function db_connect($allowBail = true)
{
$this->is_mysql = true;
$this->has_connected = true;
$this->ready = true;
$this->set_sql_mode();
$this->init_charset();
}
public function db_version()
{
return Cache::remember('sql_version', config('wordpress.caching'), function () {
return DB::selectOne('SELECT version() as v')->v;
});
}
public function set_sql_mode($modes = [])
{
if (empty($modes)) {
$modes = Cache::remember('sql_modes', config('wordpress.caching'), function () {
return explode(',', DB::selectOne('SELECT @@SESSION.sql_mode as sql_mode')->sql_mode);
});
}
$modes = array_change_key_case($modes, CASE_UPPER);
$incompatibleModes = (array) apply_filters('incompatible_sql_modes', $this->incompatible_modes);
foreach ($modes as $i => $mode) {
if (in_array($mode, $incompatibleModes, true)) {
unset($modes[$i]);
}
}
DB::statement('SET SESSION sql_mode=?', [implode(',', $modes)]);
}
public function _real_escape($string)
{
return substr(DB::getPdo()->quote($string), 1, -1);
}
public function query($query)
{
if (!$this->ready) {
$this->check_current_query = true;
return false;
}
$query = apply_filters('query', $query);
$this->flush();
$this->func_call = "\$db->query(\"$query\")";
if ($this->check_current_query && !$this->check_ascii($query)) {
$strippedQuery = $this->strip_invalid_text_from_query($query);
$this->flush();
if ($strippedQuery !== $query) {
$this->insert_id = 0;
return false;
}
}
$this->check_current_query = true;
$this->last_query = $query;
try {
$this->_do_query($query);
} catch (QueryException $e) {
$this->last_error = $e->getMessage();
if ($this->insert_id && preg_match('/^\s*(insert|replace)\s/i', $query)) {
$this->insert_id = 0;
}
$this->print_error($this->last_error);
return false;
}
if (preg_match('/^\s*(create|alter|truncate|drop)\s/i', $query)) {
$return = $this->result;
} elseif (preg_match('/^\s*(insert|delete|update|replace)\s/i', $query)) {
$this->rows_affected = $return = $this->result;
} else {
$this->num_rows = count($this->result);
$return = $this->result;
}
return $return;
}
private function _do_query($query)
{
if (defined('SAVEQUERIES') && SAVEQUERIES) {
$this->timer_start();
}
if (preg_match('/^\s*(insert|create|alter|truncate|drop|set)\s/i', $query)) {
$this->last_result = $this->result = DB::statement($query);
} elseif (preg_match('/^\s*(delete|update|replace)\s/i', $query)) {
$this->last_result = $this->result = DB::affectingStatement($query);
} else {
if (!config('wordpress.caching')) {
Cache::forget('q_' . $query);
}
$this->result = Cache::remember('q_' . $query, config('wordpress.caching'), function () use ($query) {
return DB::select($query);
});
$this->last_result = $this->result;
}
$this->num_queries++;
if (defined('SAVEQUERIES') && SAVEQUERIES) {
$this->queries[] = [$query, $this->timer_stop(), $this->get_caller()];
}
}
}