refactor(Core): refactor and optimize code

This commit is contained in:
2026-05-06 00:54:24 +03:30
parent 32c065e4b6
commit dec4e67b9e
20 changed files with 1787 additions and 361 deletions

View File

@@ -11,14 +11,11 @@ class Rule {
public $actions;
public $priority;
public $usage_limit;
public $usage_count;
public $user_roles;
public $start_date;
public $end_date;
public $enabled;
public $condition_type;
public $condition_value;
public $action_type;
public $action_value;
public $created_at;
public $updated_at;
@@ -32,28 +29,13 @@ class Rule {
$this->actions = $this->parseJsonField($data['actions'] ?? '[]');
$this->priority = isset($data['priority']) ? (int) $data['priority'] : 10;
$this->usage_limit = isset($data['usage_limit']) ? (int) $data['usage_limit'] : 0;
$this->usage_count = isset($data['usage_count']) ? (int) $data['usage_count'] : 0;
$this->user_roles = $this->parseRolesField($data['user_roles'] ?? '');
$this->start_date = $data['start_date'] ?? null;
$this->end_date = $data['end_date'] ?? null;
$this->enabled = isset($data['enabled']) ? (int) $data['enabled'] : 1;
$this->condition_type = $data['condition_type'] ?? '';
$this->condition_value = $data['condition_value'] ?? '';
$this->action_type = $data['action_type'] ?? '';
$this->action_value = $data['action_value'] ?? '';
$this->created_at = $data['created_at'] ?? null;
$this->updated_at = $data['updated_at'] ?? null;
if (empty($this->conditions) && !empty($this->condition_type)) {
$this->conditions = [
['type' => $this->condition_type, 'value' => $this->condition_value],
];
}
if (empty($this->actions) && !empty($this->action_type)) {
$this->actions = [
['type' => $this->action_type, 'value' => $this->action_value],
];
}
}
private function parseJsonField($value) {
@@ -88,16 +70,41 @@ class Rule {
'actions' => wp_json_encode($this->actions),
'priority' => $this->priority,
'usage_limit' => $this->usage_limit,
'usage_count' => $this->usage_count,
'user_roles' => is_array($this->user_roles) ? implode(',', $this->user_roles) : $this->user_roles,
'start_date' => $this->start_date,
'end_date' => $this->end_date,
'enabled' => $this->enabled,
'condition_type' => $this->condition_type,
'condition_value' => $this->condition_value,
'action_type' => $this->action_type,
'action_value' => $this->action_value,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
/**
* Check if rule is active
*/
public function isActive() {
if (!$this->enabled) {
return false;
}
$now = current_time('mysql');
if (!empty($this->start_date) && $now < $this->start_date) {
return false;
}
if (!empty($this->end_date) && $now > $this->end_date) {
return false;
}
return true;
}
/**
* Check if usage limit reached
*/
public function hasReachedLimit() {
return $this->usage_limit > 0 && $this->usage_count >= $this->usage_limit;
}
}