| 1 |
<?php |
| 2 |
|
| 3 |
namespace SimpleAnalytics; |
| 4 |
|
| 5 |
use SimpleAnalytics\Actions\AddInactiveComment; |
| 6 |
use SimpleAnalytics\Actions\AddNoScriptTag; |
| 7 |
use SimpleAnalytics\Actions\AddPluginSettingsLink; |
| 8 |
use SimpleAnalytics\Scripts\AnalyticsScript; |
| 9 |
use SimpleAnalytics\Scripts\AutomatedEventsScript; |
| 10 |
use SimpleAnalytics\Scripts\InactiveScript; |
| 11 |
use SimpleAnalytics\Settings\AdminPage; |
| 12 |
|
| 13 |
final class Plugin |
| 14 |
{ |
| 15 |
protected $hooks; |
| 16 |
protected $settings; |
| 17 |
protected $trackingRules; |
| 18 |
protected $scripts; |
| 19 |
protected $adminPage; |
| 20 |
|
| 21 |
public function __construct( |
| 22 |
WordPressHooks $hooks, |
| 23 |
WordPressSettings $settings, |
| 24 |
TrackingRules $trackingRules, |
| 25 |
ScriptRegistry $scripts, |
| 26 |
AdminPage $adminPage |
| 27 |
) { |
| 28 |
$this->hooks = $hooks; |
| 29 |
$this->settings = $settings; |
| 30 |
$this->trackingRules = $trackingRules; |
| 31 |
$this->scripts = $scripts; |
| 32 |
$this->adminPage = $adminPage; |
| 33 |
} |
| 34 |
|
| 35 |
public function boot(): void |
| 36 |
{ |
| 37 |
$this->hooks->addAction('init', \Closure::fromCallable([$this, 'onInit'])); |
| 38 |
$this->hooks->onActivation(\Closure::fromCallable([$this, 'onActivation'])); |
| 39 |
$this->hooks->onDeactivation(\Closure::fromCallable([$this, 'onUninstall'])); |
| 40 |
|
| 41 |
if ($this->hooks->isAdmin()) { |
| 42 |
$this->adminPage->register(); |
| 43 |
AddPluginSettingsLink::register(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public function onInit(): void |
| 48 |
{ |
| 49 |
$hasExcludedIp = $this->trackingRules->hasExcludedIp(); |
| 50 |
$hasExcludedUserRole = $this->trackingRules->hasExcludedUserRole(); |
| 51 |
$tracking = ! $hasExcludedIp && ! $hasExcludedUserRole; |
| 52 |
|
| 53 |
if ($tracking) { |
| 54 |
$this->scripts->push(new AnalyticsScript); |
| 55 |
} else { |
| 56 |
$this->scripts->push(new InactiveScript); |
| 57 |
$reason = $hasExcludedIp ? 'Exclude IP Address' : 'Exclude User Role'; |
| 58 |
AddInactiveComment::register($reason); |
| 59 |
} |
| 60 |
|
| 61 |
if ($tracking && $this->settings->get(SettingName::NOSCRIPT)) { |
| 62 |
AddNoScriptTag::register(); |
| 63 |
} |
| 64 |
|
| 65 |
if ($this->settings->boolean(SettingName::AUTOMATED_EVENTS)) { |
| 66 |
$this->scripts->push(new AutomatedEventsScript); |
| 67 |
} |
| 68 |
|
| 69 |
$this->scripts->register(); |
| 70 |
} |
| 71 |
|
| 72 |
public function onActivation(): void |
| 73 |
{ |
| 74 |
$this->settings->register(SettingName::ENABLED, true); |
| 75 |
|
| 76 |
foreach (array_diff(SettingName::cases(), [SettingName::ENABLED]) as $name) { |
| 77 |
$this->settings->register($name); |
| 78 |
} |
| 79 |
|
| 80 |
// Legacy. Update the option introduced in a previous release to use autoloading. |
| 81 |
$this->settings->update(SettingName::CUSTOM_DOMAIN, $this->settings->get(SettingName::CUSTOM_DOMAIN), true); |
| 82 |
} |
| 83 |
|
| 84 |
public function onUninstall(): void |
| 85 |
{ |
| 86 |
foreach (SettingName::cases() as $key) $this->settings->delete($key); |
| 87 |
} |
| 88 |
} |
| 89 |
|