-
Notifications
You must be signed in to change notification settings - Fork 2
/
rrze-legal.php
295 lines (261 loc) · 8.14 KB
/
rrze-legal.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/*
Plugin Name: RRZE Legal
Plugin URI: https://gitlab.rrze.fau.de/rrze-webteam/rrze-legal
Version: 2.7.11
Description: Legal Mandatory Information & GDPR.
Author: RRZE Webteam
Author URI: https://www.rrze.fau.de
License: GNU General Public License Version 3
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Text Domain: rrze-legal
Domain Path: /languages
Requires at least: 6.7
Requires PHP: 8.2
*/
namespace RRZE\Legal;
defined('ABSPATH') || exit;
use RRZE\Legal\Network\Options as NetworkOptions;
use RRZE\Legal\TOS\Options as TOSOptions;
use RRZE\Legal\Consent\Options as ConsentOptions;
use RRZE\Legal\Consent\Categories\Options as ConsentCategoriesOptions;
use RRZE\Legal\Consent\Cookies\Options as ConsentCookiesOptions;
/**
* SPL Autoloader (PSR-4).
* @param string $class The fully-qualified class name.
* @return void
*/
spl_autoload_register(function ($class) {
$prefix = __NAMESPACE__;
$baseDir = __DIR__ . '/includes/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$relativeClass = substr($class, $len);
$file = $baseDir . str_replace('\\', '/', $relativeClass) . '.php';
if (file_exists($file)) {
require $file;
}
});
// Load the plugin's text domain for localization.
add_action('init', fn() => load_plugin_textdomain('rrze-legal', false, dirname(plugin_basename(__FILE__)) . '/languages'));
// Register activation hook for the plugin
register_activation_hook(__FILE__, __NAMESPACE__ . '\activation');
// Register deactivation hook for the plugin
register_deactivation_hook(__FILE__, __NAMESPACE__ . '\deactivation');
/**
* Add an action hook for the 'plugins_loaded' hook.
*
* This code hooks into the 'plugins_loaded' action hook to execute a callback function when
* WordPress has fully loaded all active plugins and the theme's functions.php file.
*/
add_action('plugins_loaded', __NAMESPACE__ . '\loaded');
/**
* Activation callback function.
*/
function activation()
{
if ($error = systemRequirements()) {
deactivate_plugins(plugin_basename(__FILE__));
wp_die(
sprintf(
/* translators: 1: The plugin name, 2: The error string. */
__('Plugins: %1$s: %2$s', 'rrze-legal'),
plugin_basename(__FILE__),
$error
)
);
}
}
/**
* Deactivation callback function.
*/
function deactivation()
{
flush_rewrite_rules();
}
/**
* Instantiate Plugin class.
* @return object Plugin
*/
function plugin()
{
static $instance;
if (null === $instance) {
$instance = new Plugin(__FILE__);
}
return $instance;
}
/**
* Instantiate Network Options class.
* @return object Plugin
*/
function network()
{
static $instance;
if (null === $instance) {
$instance = new NetworkOptions();
}
return $instance;
}
/**
* Instantiate TOS Options class.
* @return object Plugin
*/
function tos()
{
static $instance;
if (null === $instance) {
$instance = new TOSOptions();
}
return $instance;
}
/**
* Instantiate Consent Options class.
* @return object Plugin
*/
function consent()
{
static $instance;
if (null === $instance) {
$instance = new ConsentOptions();
}
return $instance;
}
/**
* Instantiate Consent Categories Options class.
* @return object Plugin
*/
function consentCategories()
{
static $instance;
if (null === $instance) {
$instance = new ConsentCategoriesOptions();
}
return $instance;
}
/**
* Instantiate Consent Cookies Options class.
* @return object Plugin
*/
function consentCookies()
{
static $instance;
if (null === $instance) {
$instance = new ConsentCookiesOptions();
}
return $instance;
}
/**
* TOS Plugin Deactivation.
*/
function tosPluginDeactivation()
{
include_once ABSPATH . 'wp-admin/includes/plugin.php';
if (is_plugin_active('rrze-tos/rrze-tos.php')) {
deactivate_plugins('rrze-tos/rrze-tos.php');
}
if (is_plugin_active_for_network('rrze-tos/rrze-tos.php')) {
add_action('network_admin_notices', function () {
echo '<div class="notice notice-warning"><p>',
esc_html(__('The "rrze-tos" plugin is networkwide activated. Please deactivate the "rrze-tos" plugin as it will be replaced by the "rrze-legal" plugin.', 'rrze-legal'), 'post'),
'</p></div>';
});
return false;
}
return true;
}
/**
* Get FAU domains.
* @return array
*/
function fauDomains(): array
{
return apply_filters(
'rrze_legal_fau_domains',
[
'fau.de',
'fau.eu',
'uni-erlangen.de'
]
);
}
/**
* Check system requirements for the plugin.
*
* This method checks if the server environment meets the minimum WordPress and PHP version requirements
* for the plugin to function properly.
*
* @return string An error message string if requirements are not met, or an empty string if requirements are satisfied.
*/
function systemRequirements(): string
{
// Get the global WordPress version.
global $wp_version;
// Get the PHP version.
$phpVersion = phpversion();
// Initialize an error message string.
$error = '';
// Check if the WordPress version is compatible with the plugin's requirement.
if (!is_wp_version_compatible(plugin()->getRequiresWP())) {
$error = sprintf(
/* translators: 1: Server WordPress version number, 2: Required WordPress version number. */
__('The server is running WordPress version %1$s. The plugin requires at least WordPress version %2$s.', 'rrze-legal'),
$wp_version,
plugin()->getRequiresWP()
);
} elseif (!is_php_version_compatible(plugin()->getRequiresPHP())) {
// Check if the PHP version is compatible with the plugin's requirement.
$error = sprintf(
/* translators: 1: Server PHP version number, 2: Required PHP version number. */
__('The server is running PHP version %1$s. The plugin requires at least PHP version %2$s.', 'rrze-legal'),
$phpVersion,
plugin()->getRequiresPHP()
);
}
// Return the error message string, which will be empty if requirements are satisfied.
return $error;
}
/**
* Handle the loading of the plugin.
*
* This function is responsible for initializing the plugin, loading text domains for localization,
* checking system requirements, and displaying error notices if necessary.
*/
function loaded()
{
if (!tosPluginDeactivation()) {
return;
}
// Trigger the 'loaded' method of the main plugin instance.
plugin()->loaded();
// Check system requirements and store any error messages.
if ($error = systemRequirements()) {
// If there is an error, add an action to display an admin notice with the error message.
add_action('admin_init', function () use ($error) {
// Check if the current user has the capability to activate plugins.
if (current_user_can('activate_plugins')) {
// Get plugin data to retrieve the plugin's name.
$pluginName = plugin()->getName();
// Determine the admin notice tag based on network-wide activation.
$tag = is_plugin_active_for_network(plugin()->getBaseName()) ? 'network_admin_notices' : 'admin_notices';
// Add an action to display the admin notice.
add_action($tag, function () use ($pluginName, $error) {
printf(
'<div class="notice notice-error"><p>' .
/* translators: 1: The plugin name, 2: The error string. */
esc_html__('Plugins: %1$s: %2$s', 'rrze-legal') .
'</p></div>',
$pluginName,
$error
);
});
}
});
// Return to prevent further initialization if there is an error.
return;
}
// If there are no errors, create an instance of the 'Main' class
new Main;
}