| 1 |
<?php |
| 2 |
global $wpdb; |
| 3 |
|
| 4 |
class WooMailerLiteDBConnection |
| 5 |
{ |
| 6 |
use WooMailerLiteResources; |
| 7 |
|
| 8 |
/** |
| 9 |
* @var wpdb $db |
| 10 |
*/ |
| 11 |
private $db; |
| 12 |
|
| 13 |
/** |
| 14 |
* @var string $wooMailerliteTablePrefix |
| 15 |
*/ |
| 16 |
private $wooMailerliteTablePrefix = 'woo_mailerlite_'; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var string $query |
| 20 |
*/ |
| 21 |
public $query = ''; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var string $column |
| 25 |
*/ |
| 26 |
private $column; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var string $primaryKey |
| 30 |
*/ |
| 31 |
private $primaryKey = ''; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var string $select |
| 35 |
*/ |
| 36 |
private string $select = '*'; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var WooMailerLiteDBConnection|null $db_connection_instance |
| 40 |
*/ |
| 41 |
protected static $db_connection_instance = null; |
| 42 |
|
| 43 |
protected $hasWhere = false; |
| 44 |
|
| 45 |
protected $countOnly = false; |
| 46 |
|
| 47 |
|
| 48 |
private $columnsOnly = false; |
| 49 |
|
| 50 |
/** |
| 51 |
* @var string|null $table |
| 52 |
*/ |
| 53 |
|
| 54 |
|
| 55 |
protected $withRelation = ""; |
| 56 |
|
| 57 |
|
| 58 |
/** |
| 59 |
* Execute the prepared DB query |
| 60 |
* @param $updateStructure |
| 61 |
* @return mixed |
| 62 |
*/ |
| 63 |
public function executeQuery($updateStructure = false) |
| 64 |
{ |
| 65 |
$this->db()->flush(); |
| 66 |
if ($updateStructure) { |
| 67 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 68 |
$result = dbDelta($this->query); |
| 69 |
} else { |
| 70 |
if ($this->columnsOnly) { |
| 71 |
$result = $this->db()->get_col($this->query); |
| 72 |
} else { |
| 73 |
$result = $this->db()->get_results($this->query); |
| 74 |
} |
| 75 |
} |
| 76 |
return $result; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get the wpdb instance |
| 81 |
* @return wpdb |
| 82 |
*/ |
| 83 |
public function db() |
| 84 |
{ |
| 85 |
global $wpdb; |
| 86 |
$this->db = $wpdb; |
| 87 |
return $this->db; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Show all the tables created by the plugin |
| 92 |
* @return array |
| 93 |
*/ |
| 94 |
public function showWooMlTables() |
| 95 |
{ |
| 96 |
$tables = []; |
| 97 |
foreach ($this->db()->get_results('SHOW TABLES LIKE "' . $this->getDbTablePrefix() . '%"', ARRAY_N) as $table) |
| 98 |
{ |
| 99 |
$tables[] = $table[0]; |
| 100 |
} |
| 101 |
return $tables; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get the table prefix |
| 106 |
* @return string |
| 107 |
*/ |
| 108 |
protected function getDbTablePrefix() |
| 109 |
{ |
| 110 |
return $this->db()->prefix . $this->wooMailerliteTablePrefix; |
| 111 |
} |
| 112 |
|
| 113 |
public static function setTable(string $table) |
| 114 |
{ |
| 115 |
static::$db_connection_instance->table = $table; |
| 116 |
} |
| 117 |
|
| 118 |
public function count() |
| 119 |
{ |
| 120 |
$this->countOnly = true; |
| 121 |
$data = $this->get(); |
| 122 |
$this->countOnly = false; |
| 123 |
if ($data instanceof WooMailerLiteCollection) { |
| 124 |
return $data->count(); |
| 125 |
} else { |
| 126 |
return is_array($data) ? count($data) : (is_int($data) ? $data : 0); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public function toArray() |
| 131 |
{ |
| 132 |
return $this->model->attributes; |
| 133 |
} |
| 134 |
|
| 135 |
public static function with($relation) |
| 136 |
{ |
| 137 |
static::db_connection_instance()->withRelation = $relation; |
| 138 |
return static::db_connection_instance(); |
| 139 |
} |
| 140 |
|
| 141 |
public function columnsOnly() |
| 142 |
{ |
| 143 |
$this->columnsOnly = true; |
| 144 |
return $this; |
| 145 |
} |
| 146 |
} |
| 147 |
|