-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.php
142 lines (123 loc) · 3.65 KB
/
index.php
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* Plugin Name: Inpsyde Google Tag Manager
* Description: Adds the GTM container snippet to your site and populates the Data Layer.
* Plugin URI: https://wordpress.org/plugins/inpsyde-google-tag-manager
* Version: 3.0.0-beta.1
* Author: Syde GmbH
* Author URI: https://syde.com/
* Text Domain: inpsyde-google-tag-manager
*/
declare(strict_types=1);
namespace Inpsyde\GoogleTagManager;
use Inpsyde\GoogleTagManager\Event\LogEvent;
use Inpsyde\Modularity\Package;
use Inpsyde\Modularity\Properties\PluginProperties;
if (!function_exists('add_filter')) {
return;
}
/**
* @return Package
* @throws \Exception
*/
function plugin(): Package
{
/** @var null|Package $package */
static $package;
if (!$package) {
$properties = PluginProperties::new(__FILE__);
$package = Package::new($properties);
$package
->addModule(new Provider\AssetProvider())
->addModule(new Provider\DataLayerProvider())
->addModule(new Provider\RendererProvider())
->addModule(new Provider\RestProvider())
->addModule(new Provider\ServiceProvider())
->addModule(new Provider\SettingsProvider());
}
return $package;
}
add_action(
'plugins_loaded',
static function (): void {
try {
load_plugin_textdomain('inpsyde-google-tag-manager');
if (!checkPluginRequirements()) {
return;
}
plugin()->boot();
} catch (\Throwable $exception) {
if (defined('WP_DEBUG') && WP_DEBUG) {
throw $exception;
}
do_action(LogEvent::ACTION, 'critical', $exception);
}
}
);
do_action(
'inpsyde.modularity.google-tag-manager.failed-boot',
/**
* Display an error message in the WP admin.
*
* @param \Throwable $exception
*/
static function (\Throwable $exception): void {
$message = sprintf(
'<strong>Error:</strong> %s <br><pre>%s</pre>',
$exception->getMessage(),
$exception->getTraceAsString()
);
adminNotice(wp_kses_post($message));
}
);
/**
* @return bool
*/
function checkPluginRequirements(): bool
{
$minPhpVersion = '8.0';
$currentPhpVersion = phpversion();
if (! version_compare($currentPhpVersion, $minPhpVersion, '>=')) {
adminNotice(
sprintf(
/* translators: %1$s is the min PHP-version, %2$s the current PHP-version */
__(
'Inpsyde Google Tag Manager requires PHP version %1$1s or higher. You are running version %2$2s.',
'inpsyde-google-tag-manager'
),
$minPhpVersion,
$currentPhpVersion
)
);
return false;
}
if (! class_exists(Event\LogEvent::class)) {
$autoloader = __DIR__ . '/vendor/autoload.php';
if (! file_exists($autoloader)) {
adminNotice(
__(
'Could not find a working autoloader for Inpsyde Google Tag Manager.',
'inpsyde-google-tag-manager'
)
);
return false;
}
/** @noinspection PhpIncludeInspection */
require $autoloader;
}
return true;
}
/**
* @param string $message
*/
function adminNotice(string $message): void
{
$callback = static function () use ($message): void {
printf(
'<div class="notice notice-error"><p>%1$s</p></div>',
esc_html($message)
);
};
add_action('admin_notices', $callback);
add_action('network_admin_notices', $callback);
}