| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam; |
| 4 |
|
| 5 |
use SyncBasalam\Migrations\MigrationManager; |
| 6 |
use SyncBasalam\Registrar\AdminRegistrar; |
| 7 |
use SyncBasalam\Registrar\ListenerRegistrar; |
| 8 |
use SyncBasalam\Registrar\OrderRegistrar; |
| 9 |
use SyncBasalam\Registrar\ProductRegistrar; |
| 10 |
use SyncBasalam\Registrar\QueueRegistrar; |
| 11 |
use SyncBasalam\Services\Api\CircuitBreaker; |
| 12 |
use SyncBasalam\Services\FetchVersionDetail; |
| 13 |
|
| 14 |
defined('ABSPATH') || exit; |
| 15 |
|
| 16 |
class Plugin |
| 17 |
{ |
| 18 |
public const VERSION = '1.10.13'; |
| 19 |
|
| 20 |
public function __construct() |
| 21 |
{ |
| 22 |
$this->onboarding(); |
| 23 |
$this->handleVersionUpdate(); |
| 24 |
$this->registrars(); |
| 25 |
$this->notices(); |
| 26 |
} |
| 27 |
|
| 28 |
private function onboarding() |
| 29 |
{ |
| 30 |
if (get_transient('sync_basalam_just_activated')) { |
| 31 |
delete_transient('sync_basalam_just_activated'); |
| 32 |
if (!syncBasalamSettings()->hasToken()) { |
| 33 |
wp_safe_redirect(admin_url('admin.php?page=basalam-onboarding')); |
| 34 |
exit(); |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
private function handleVersionUpdate() |
| 40 |
{ |
| 41 |
$currentVersion = \get_option('sync_basalam_version') ?: '0.0.0'; |
| 42 |
if (version_compare($currentVersion, self::VERSION, '<')) { |
| 43 |
|
| 44 |
$fetchVersionDetail = new FetchVersionDetail(self::VERSION); |
| 45 |
$fetchVersionDetail->checkForceUpdate(); |
| 46 |
|
| 47 |
$manager = new MigrationManager(); |
| 48 |
$manager->runMigrations($currentVersion, self::VERSION); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
private function notices() |
| 53 |
{ |
| 54 |
if (!get_option('sync_basalam_review_never_remind')) { |
| 55 |
add_action('admin_notices', function () { |
| 56 |
if (!AdminRegistrar::isWoosalamRelatedAdminScreen()) return; |
| 57 |
|
| 58 |
$template = syncBasalamPlugin()->templatePath("notifications/LikeAlert.php"); |
| 59 |
require_once $template; |
| 60 |
}); |
| 61 |
} |
| 62 |
|
| 63 |
if (!syncBasalamSettings()->hasToken()) { |
| 64 |
add_action('admin_notices', function () { |
| 65 |
if (!AdminRegistrar::isWoosalamRelatedAdminScreen()) return; |
| 66 |
|
| 67 |
$template = syncBasalamPlugin()->templatePath("notifications/AccessAlert.php"); |
| 68 |
require_once($template); |
| 69 |
}); |
| 70 |
} |
| 71 |
add_action('admin_notices', function () { |
| 72 |
if (!AdminRegistrar::isWoosalamRelatedAdminScreen()) return; |
| 73 |
|
| 74 |
$template = syncBasalamPlugin()->templatePath("notifications/HttpBlock.php"); |
| 75 |
require_once($template); |
| 76 |
}); |
| 77 |
add_action('admin_notices', function () { |
| 78 |
if (!AdminRegistrar::isWoosalamRelatedAdminScreen()) return; |
| 79 |
|
| 80 |
$circuitBreaker = new CircuitBreaker(); |
| 81 |
|
| 82 |
if ($circuitBreaker->getState() === CircuitBreaker::STATE_CLOSED) return; |
| 83 |
|
| 84 |
$template = syncBasalamPlugin()->templatePath("notifications/CircuitBreakerAlert.php"); |
| 85 |
require $template; |
| 86 |
}); |
| 87 |
} |
| 88 |
|
| 89 |
private function registrars() |
| 90 |
{ |
| 91 |
$registrars = [ |
| 92 |
AdminRegistrar::class, |
| 93 |
ProductRegistrar::class, |
| 94 |
OrderRegistrar::class, |
| 95 |
ListenerRegistrar::class, |
| 96 |
QueueRegistrar::class, |
| 97 |
]; |
| 98 |
|
| 99 |
foreach ($registrars as $registrarClass) { |
| 100 |
$registrar = syncBasalamContainer()->get($registrarClass); |
| 101 |
$registrar->register(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public function pluginPath() |
| 106 |
{ |
| 107 |
return untrailingslashit(str_replace("includes", "", \plugin_dir_path(__FILE__))); |
| 108 |
} |
| 109 |
|
| 110 |
public function templatePath($path = null) |
| 111 |
{ |
| 112 |
|
| 113 |
$path = $path ? "/" . $path : null; |
| 114 |
|
| 115 |
return $this->pluginPath() . "/templates" . $path; |
| 116 |
} |
| 117 |
|
| 118 |
public function assetsUrl($path = null) |
| 119 |
{ |
| 120 |
return plugin_dir_url(dirname(__FILE__, 1)) . "assets/" . $path; |
| 121 |
} |
| 122 |
|
| 123 |
public function getVersion() |
| 124 |
{ |
| 125 |
return self::VERSION; |
| 126 |
} |
| 127 |
} |
| 128 |
|