| 1 |
<?php |
| 2 |
|
| 3 |
class WooMailerLiteModel |
| 4 |
{ |
| 5 |
public array $meta = [ |
| 6 |
'_woo_ml_product_tracked' => 'tracked', |
| 7 |
'_woo_ml_product_ignored' => 'ignored', |
| 8 |
'_woo_ml_category_tracked' => 'tracked' |
| 9 |
]; |
| 10 |
public $attributes; |
| 11 |
|
| 12 |
protected $table; |
| 13 |
|
| 14 |
protected array $updateAttributes = []; |
| 15 |
|
| 16 |
protected $casts = []; |
| 17 |
|
| 18 |
protected $format; |
| 19 |
|
| 20 |
protected $removeEmpty = []; |
| 21 |
|
| 22 |
protected $isResource = false; |
| 23 |
|
| 24 |
public static $customTablesEnabled; |
| 25 |
|
| 26 |
public function __construct($attributes = []) |
| 27 |
{ |
| 28 |
$this->attributes = $attributes; |
| 29 |
} |
| 30 |
|
| 31 |
public function __get($attribute) |
| 32 |
{ |
| 33 |
if (in_array($attribute, $this->casts)) { |
| 34 |
|
| 35 |
if (!empty($this->format[$attribute])) { |
| 36 |
switch ($this->format[$attribute]) { |
| 37 |
case 'array': |
| 38 |
if (!is_array($this->attributes[$attribute])) { |
| 39 |
return json_decode($this->attributes[$attribute], true); |
| 40 |
} |
| 41 |
break; |
| 42 |
case 'object': |
| 43 |
return json_decode($this->attributes[$attribute]); |
| 44 |
case 'boolean': |
| 45 |
return (bool)$this->attributes[$attribute]; |
| 46 |
case 'string': |
| 47 |
return (string)$this->attributes[$attribute]; |
| 48 |
default: |
| 49 |
return $this->attributes[$attribute]; |
| 50 |
} |
| 51 |
} |
| 52 |
return $this->attributes[$attribute]; |
| 53 |
} elseif (isset($this->attributes[$attribute])) { |
| 54 |
return $this->attributes[$attribute]; |
| 55 |
} |
| 56 |
return null; |
| 57 |
} |
| 58 |
|
| 59 |
public function __set($attribute, $value) |
| 60 |
{ |
| 61 |
if (static::class === 'WooMailerLiteCategory') { |
| 62 |
unset($this->meta['_woo_ml_product_tracked']); |
| 63 |
} |
| 64 |
if (in_array($attribute, array_values($this->meta))) { |
| 65 |
$this->attributes[$attribute] = $value; |
| 66 |
$this->updateAttributes[] = $attribute; |
| 67 |
|
| 68 |
} else { |
| 69 |
$this->attributes[$attribute] = $value; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public function __isset($attribute) |
| 74 |
{ |
| 75 |
return isset($this->attributes[$attribute]); |
| 76 |
} |
| 77 |
|
| 78 |
public function __call($method, $parameters) |
| 79 |
{ |
| 80 |
return $this->queryBuilder()->$method(...$parameters); |
| 81 |
} |
| 82 |
|
| 83 |
public static function __callStatic($name, $arguments) |
| 84 |
{ |
| 85 |
return (new static([]))->$name(...$arguments); |
| 86 |
} |
| 87 |
|
| 88 |
public function save() |
| 89 |
{ |
| 90 |
foreach ($this->updateAttributes as $updateAttribute) { |
| 91 |
if (static::class === 'WooMailerLiteCategory') { |
| 92 |
update_term_meta($this->resource_id, array_search($updateAttribute, $this->meta), $this->attributes[$updateAttribute]); |
| 93 |
} else { |
| 94 |
if ($updateAttribute === 'ignored') { |
| 95 |
$ignoredProducts = WooMailerLiteOptions::get('ignored_products', []); |
| 96 |
if (!$this->attributes[$updateAttribute]) { |
| 97 |
unset($ignoredProducts[$this->resource_id]); |
| 98 |
} else { |
| 99 |
$ignoredProducts[$this->resource_id] = $this->name; |
| 100 |
} |
| 101 |
WooMailerLiteOptions::update('ignored_products', $ignoredProducts); |
| 102 |
} |
| 103 |
update_post_meta($this->resource_id, array_search($updateAttribute, $this->meta), $this->attributes[$updateAttribute]); |
| 104 |
|
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
public function exists() |
| 110 |
{ |
| 111 |
return count($this->attributes) > 0; |
| 112 |
} |
| 113 |
|
| 114 |
public function setRelation($relation, $value) |
| 115 |
{ |
| 116 |
$this->attributes[$relation] = $value; |
| 117 |
return $this; |
| 118 |
} |
| 119 |
|
| 120 |
public function queryBuilder() |
| 121 |
{ |
| 122 |
return new WooMailerLiteQueryBuilder($this); |
| 123 |
} |
| 124 |
|
| 125 |
public function getTable() |
| 126 |
{ |
| 127 |
return $this->table; |
| 128 |
} |
| 129 |
|
| 130 |
public function isResource() |
| 131 |
{ |
| 132 |
return $this->isResource; |
| 133 |
} |
| 134 |
|
| 135 |
public function getCastsArray() |
| 136 |
{ |
| 137 |
return $this->casts; |
| 138 |
} |
| 139 |
|
| 140 |
public function getFormatArray() |
| 141 |
{ |
| 142 |
return $this->format; |
| 143 |
} |
| 144 |
|
| 145 |
public function getRemoveEmptyArray() |
| 146 |
{ |
| 147 |
return $this->removeEmpty; |
| 148 |
} |
| 149 |
|
| 150 |
public function setResource() |
| 151 |
{ |
| 152 |
$this->isResource = true; |
| 153 |
} |
| 154 |
|
| 155 |
public function setTable($table) |
| 156 |
{ |
| 157 |
$this->table = $table; |
| 158 |
} |
| 159 |
} |
| 160 |
|