Files
sodino/sodino.php

199 lines
5.5 KiB
PHP

<?php
/**
* Plugin Name: Sodino (سودینو)
* Plugin URI: https://sodino.com
* Description: افزونه هوشمند قیمت‌گذاری و بهینه‌سازی درآمد برای ووکامرس. قیمت محصولات را بر اساس رفتار کاربر و قوانین تعریف‌شده به صورت پویا تنظیم می‌کند.
* Version: 2.0.0
* Author: Soheil Khaledabadi
* License: GPL v2 or later
* Text Domain: sodino
* Requires at least: 5.0
* Tested up to: 6.9
* Requires PHP: 7.4
* WC requires at least: 5.0
* WC tested up to: 10.7
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Define plugin constants
define('SODINO_VERSION', '2.0.0');
define('SODINO_DB_VERSION', '2.0');
define('SODINO_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('SODINO_PLUGIN_URL', plugin_dir_url(__FILE__));
define('SODINO_PLUGIN_BASENAME', plugin_basename(__FILE__));
// Autoloader for PSR-4
spl_autoload_register(function ($class) {
// Namespace prefix
$prefix = 'Sodino\\';
// Base directory for the namespace prefix
$base_dir = SODINO_PLUGIN_DIR . 'app/';
// Does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
// Get the relative class name
$relative_class = substr($class, $len);
// Replace namespace separators with directory separators
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
// If the file exists, require it
if (file_exists($file)) {
require $file;
}
});
// Activation hook
register_activation_hook(__FILE__, 'sodino_activate');
function sodino_activate() {
// Include database migration
require_once SODINO_PLUGIN_DIR . 'database/migrations.php';
sodino_create_tables();
// Flush rewrite rules if needed
flush_rewrite_rules();
// Set default settings
if (!get_option('sodino_settings')) {
update_option('sodino_settings', [
'plugin_enabled' => 1,
'pricing_enabled' => 1,
'upsell_enabled' => 1,
'banner_enabled' => 1,
'cache_enabled' => 1,
'cache_duration' => 3600,
]);
}
}
/**
* Run database migrations after code updates, not only on activation.
*/
function sodino_maybe_run_migrations() {
$current_version = get_option('sodino_db_version', '0');
if (version_compare($current_version, SODINO_DB_VERSION, '>=')) {
return;
}
require_once SODINO_PLUGIN_DIR . 'database/migrations.php';
sodino_create_tables();
}
// Deactivation hook
register_deactivation_hook(__FILE__, 'sodino_deactivate');
function sodino_deactivate() {
// Flush rewrite rules
flush_rewrite_rules();
// Clear analytics cron
wp_clear_scheduled_hook('sodino_hourly_analytics');
// Clear all cache
$cache = \Sodino\Core\Cache::getInstance();
$cache->clearAll();
}
// Bootstrap the plugin
function sodino_init() {
// Check if WooCommerce is active
if (!class_exists('WooCommerce')) {
add_action('admin_notices', 'sodino_woocommerce_missing_notice');
return;
}
// Load text domain
load_plugin_textdomain('sodino', false, dirname(SODINO_PLUGIN_BASENAME) . '/languages/');
sodino_maybe_run_migrations();
// Initialize admin
if (is_admin()) {
require_once SODINO_PLUGIN_DIR . 'admin/admin.php';
}
// Initialize public hooks
sodino_init_public_hooks();
// Schedule analytics aggregation if needed
sodino_schedule_analytics();
}
add_action('plugins_loaded', 'sodino_init');
/**
* Initialize public hooks
*/
function sodino_init_public_hooks() {
$settings = \Sodino\Core\Settings::getInstance();
if (!$settings->isEnabled()) {
return;
}
if ($settings->isPricingEnabled()) {
require_once SODINO_PLUGIN_DIR . 'public/hooks/pricing-hooks.php';
}
if ($settings->isUpsellEnabled()) {
require_once SODINO_PLUGIN_DIR . 'public/hooks/upsell-hooks.php';
}
if ($settings->isBannerEnabled()) {
require_once SODINO_PLUGIN_DIR . 'public/hooks/banner-hooks.php';
}
// Load analytics while Sodino is enabled.
require_once SODINO_PLUGIN_DIR . 'public/hooks/analytics-hooks.php';
}
/**
* Schedule analytics cron job
*/
function sodino_schedule_analytics() {
if (!wp_next_scheduled('sodino_hourly_analytics')) {
wp_schedule_event(time(), 'hourly', 'sodino_hourly_analytics');
}
}
/**
* Run analytics aggregation cron job
*/
function sodino_run_analytics_aggregation() {
if (!class_exists('Sodino\Services\AnalyticsService')) {
return;
}
$eventRepository = new Sodino\Repositories\EventRepository();
$ruleRepository = new Sodino\Repositories\RuleRepository();
$analyticsService = new Sodino\Services\AnalyticsService($eventRepository, $ruleRepository);
$analyticsService->primeCache();
}
add_action('sodino_hourly_analytics', 'sodino_run_analytics_aggregation');
// WooCommerce missing notice
function sodino_woocommerce_missing_notice() {
?>
<div class="error">
<p><?php _e('Sodino requires WooCommerce to be installed and active.', 'sodino'); ?></p>
</div>
<?php
}
/**
* Add settings link on plugin page
*/
add_filter('plugin_action_links_' . SODINO_PLUGIN_BASENAME, function($links) {
$settings_link = '<a href="' . admin_url('admin.php?page=sodino-settings') . '">' . __('تنظیمات', 'sodino') . '</a>';
array_unshift($links, $settings_link);
return $links;
});