refactor(Core): refactor and optimize code
This commit is contained in:
137
app/Core/Cache.php
Normal file
137
app/Core/Cache.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
namespace Sodino\Core;
|
||||
|
||||
/**
|
||||
* Cache Manager
|
||||
* Handles caching with WordPress transients and custom database cache
|
||||
*/
|
||||
class Cache {
|
||||
private static $instance = null;
|
||||
private $memory_cache = [];
|
||||
|
||||
public static function getInstance() {
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached value
|
||||
*/
|
||||
public function get($key, $group = 'sodino') {
|
||||
$full_key = $this->buildKey($key, $group);
|
||||
|
||||
// Check memory cache first
|
||||
if (isset($this->memory_cache[$full_key])) {
|
||||
return $this->memory_cache[$full_key];
|
||||
}
|
||||
|
||||
// Check WordPress transient
|
||||
$value = get_transient($full_key);
|
||||
|
||||
if ($value !== false) {
|
||||
$this->memory_cache[$full_key] = $value;
|
||||
return $value;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cached value
|
||||
*/
|
||||
public function set($key, $value, $expiration = 3600, $group = 'sodino') {
|
||||
$full_key = $this->buildKey($key, $group);
|
||||
|
||||
// Set in memory cache
|
||||
$this->memory_cache[$full_key] = $value;
|
||||
|
||||
// Set in WordPress transient
|
||||
return set_transient($full_key, $value, $expiration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete cached value
|
||||
*/
|
||||
public function delete($key, $group = 'sodino') {
|
||||
$full_key = $this->buildKey($key, $group);
|
||||
|
||||
// Remove from memory cache
|
||||
unset($this->memory_cache[$full_key]);
|
||||
|
||||
// Remove from WordPress transient
|
||||
return delete_transient($full_key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all cache for a group
|
||||
*/
|
||||
public function clearGroup($group = 'sodino') {
|
||||
global $wpdb;
|
||||
|
||||
// Clear memory cache for group
|
||||
foreach ($this->memory_cache as $key => $value) {
|
||||
if (strpos($key, "sodino_{$group}_") === 0) {
|
||||
unset($this->memory_cache[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// Clear transients for group
|
||||
$wpdb->query(
|
||||
$wpdb->prepare(
|
||||
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
|
||||
$wpdb->esc_like('_transient_sodino_' . $group . '_') . '%'
|
||||
)
|
||||
);
|
||||
|
||||
$wpdb->query(
|
||||
$wpdb->prepare(
|
||||
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
|
||||
$wpdb->esc_like('_transient_timeout_sodino_' . $group . '_') . '%'
|
||||
)
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all Sodino cache
|
||||
*/
|
||||
public function clearAll() {
|
||||
global $wpdb;
|
||||
|
||||
// Clear memory cache
|
||||
$this->memory_cache = [];
|
||||
|
||||
// Clear all Sodino transients
|
||||
$wpdb->query(
|
||||
"DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_sodino_%' OR option_name LIKE '_transient_timeout_sodino_%'"
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remember pattern - get from cache or execute callback
|
||||
*/
|
||||
public function remember($key, $callback, $expiration = 3600, $group = 'sodino') {
|
||||
$value = $this->get($key, $group);
|
||||
|
||||
if ($value !== false) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$value = call_user_func($callback);
|
||||
$this->set($key, $value, $expiration, $group);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build cache key
|
||||
*/
|
||||
private function buildKey($key, $group) {
|
||||
return "sodino_{$group}_{$key}";
|
||||
}
|
||||
}
|
||||
125
app/Core/Settings.php
Normal file
125
app/Core/Settings.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
namespace Sodino\Core;
|
||||
|
||||
/**
|
||||
* Settings Manager
|
||||
*/
|
||||
class Settings {
|
||||
private static $instance = null;
|
||||
private $settings = null;
|
||||
private $option_name = 'sodino_settings';
|
||||
|
||||
private $defaults = [
|
||||
'plugin_enabled' => 1,
|
||||
'pricing_enabled' => 1,
|
||||
'upsell_enabled' => 1,
|
||||
'banner_enabled' => 1,
|
||||
'allow_multiple_rules' => 0,
|
||||
'strategy' => 'priority',
|
||||
'max_discount_percent' => 100,
|
||||
'min_product_price' => 0,
|
||||
'ab_testing_enabled' => 0,
|
||||
'cart_pricing_enabled' => 1,
|
||||
'scheduled_campaigns_enabled' => 1,
|
||||
'cache_enabled' => 1,
|
||||
'cache_duration' => 3600,
|
||||
'debug_mode' => 0,
|
||||
];
|
||||
|
||||
public static function getInstance() {
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all settings
|
||||
*/
|
||||
public function all() {
|
||||
if ($this->settings === null) {
|
||||
$this->settings = wp_parse_args(
|
||||
get_option($this->option_name, []),
|
||||
$this->defaults
|
||||
);
|
||||
}
|
||||
return $this->settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get single setting
|
||||
*/
|
||||
public function get($key, $default = null) {
|
||||
$settings = $this->all();
|
||||
return $settings[$key] ?? $default ?? $this->defaults[$key] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set single setting
|
||||
*/
|
||||
public function set($key, $value) {
|
||||
$settings = $this->all();
|
||||
$settings[$key] = $value;
|
||||
$this->settings = $settings;
|
||||
return update_option($this->option_name, $settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update multiple settings
|
||||
*/
|
||||
public function update(array $settings) {
|
||||
$current = $this->all();
|
||||
$this->settings = array_merge($current, $settings);
|
||||
return update_option($this->option_name, $this->settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset to defaults
|
||||
*/
|
||||
public function reset() {
|
||||
$this->settings = $this->defaults;
|
||||
return update_option($this->option_name, $this->defaults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if plugin is enabled
|
||||
*/
|
||||
public function isEnabled() {
|
||||
return (bool) $this->get('plugin_enabled');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if pricing is enabled
|
||||
*/
|
||||
public function isPricingEnabled() {
|
||||
return $this->isEnabled() && (bool) $this->get('pricing_enabled');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if upsell is enabled
|
||||
*/
|
||||
public function isUpsellEnabled() {
|
||||
return $this->isEnabled() && (bool) $this->get('upsell_enabled');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if banner is enabled
|
||||
*/
|
||||
public function isBannerEnabled() {
|
||||
return $this->isEnabled() && (bool) $this->get('banner_enabled');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if cache is enabled
|
||||
*/
|
||||
public function isCacheEnabled() {
|
||||
return (bool) $this->get('cache_enabled');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if debug mode is enabled
|
||||
*/
|
||||
public function isDebugMode() {
|
||||
return (bool) $this->get('debug_mode');
|
||||
}
|
||||
}
|
||||
132
app/Core/Validator.php
Normal file
132
app/Core/Validator.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
namespace Sodino\Core;
|
||||
|
||||
/**
|
||||
* Validation Helper
|
||||
*/
|
||||
class Validator {
|
||||
private $errors = [];
|
||||
private $data = [];
|
||||
|
||||
public function __construct(array $data) {
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate required field
|
||||
*/
|
||||
public function required($field, $message = null) {
|
||||
if (!isset($this->data[$field]) || empty($this->data[$field])) {
|
||||
$this->errors[$field][] = $message ?? sprintf(__('فیلد %s الزامی است.', 'sodino'), $field);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate numeric field
|
||||
*/
|
||||
public function numeric($field, $message = null) {
|
||||
if (isset($this->data[$field]) && !is_numeric($this->data[$field])) {
|
||||
$this->errors[$field][] = $message ?? sprintf(__('فیلد %s باید عدد باشد.', 'sodino'), $field);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate min value
|
||||
*/
|
||||
public function min($field, $min, $message = null) {
|
||||
if (isset($this->data[$field]) && $this->data[$field] < $min) {
|
||||
$this->errors[$field][] = $message ?? sprintf(__('فیلد %s باید حداقل %s باشد.', 'sodino'), $field, $min);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate max value
|
||||
*/
|
||||
public function max($field, $max, $message = null) {
|
||||
if (isset($this->data[$field]) && $this->data[$field] > $max) {
|
||||
$this->errors[$field][] = $message ?? sprintf(__('فیلد %s باید حداکثر %s باشد.', 'sodino'), $field, $max);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate email
|
||||
*/
|
||||
public function email($field, $message = null) {
|
||||
if (isset($this->data[$field]) && !filter_var($this->data[$field], FILTER_VALIDATE_EMAIL)) {
|
||||
$this->errors[$field][] = $message ?? sprintf(__('فیلد %s باید یک ایمیل معتبر باشد.', 'sodino'), $field);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate URL
|
||||
*/
|
||||
public function url($field, $message = null) {
|
||||
if (isset($this->data[$field]) && !filter_var($this->data[$field], FILTER_VALIDATE_URL)) {
|
||||
$this->errors[$field][] = $message ?? sprintf(__('فیلد %s باید یک URL معتبر باشد.', 'sodino'), $field);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate in array
|
||||
*/
|
||||
public function in($field, array $values, $message = null) {
|
||||
if (isset($this->data[$field]) && !in_array($this->data[$field], $values, true)) {
|
||||
$this->errors[$field][] = $message ?? sprintf(__('مقدار فیلد %s نامعتبر است.', 'sodino'), $field);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom validation
|
||||
*/
|
||||
public function custom($field, callable $callback, $message = null) {
|
||||
if (isset($this->data[$field]) && !call_user_func($callback, $this->data[$field])) {
|
||||
$this->errors[$field][] = $message ?? sprintf(__('فیلد %s نامعتبر است.', 'sodino'), $field);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if validation passed
|
||||
*/
|
||||
public function passes() {
|
||||
return empty($this->errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if validation failed
|
||||
*/
|
||||
public function fails() {
|
||||
return !$this->passes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all errors
|
||||
*/
|
||||
public function errors() {
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get first error
|
||||
*/
|
||||
public function firstError() {
|
||||
foreach ($this->errors as $field => $messages) {
|
||||
return $messages[0] ?? '';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Static factory method
|
||||
*/
|
||||
public static function make(array $data) {
|
||||
return new self($data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user