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