Skip to content

Commit e8ea958

Browse files
committed
Update rrze-legal.php
1 parent d5d96e4 commit e8ea958

File tree

1 file changed

+78
-49
lines changed

1 file changed

+78
-49
lines changed

rrze-legal.php

+78-49
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
/*
44
Plugin Name: RRZE Legal
55
Plugin URI: https://gitlab.rrze.fau.de/rrze-webteam/rrze-legal
6-
Version: 2.7.8
6+
Version: 2.7.9
77
Description: Legal Mandatory Information & GDPR.
88
Author: RRZE Webteam
99
Author URI: https://www.rrze.fau.de
1010
License: GNU General Public License Version 3
1111
License URI: https://www.gnu.org/licenses/gpl-3.0.html
1212
Text Domain: rrze-legal
1313
Domain Path: /languages
14-
Requires at least: 6.6
14+
Requires at least: 6.7
1515
Requires PHP: 8.2
1616
*/
1717

@@ -25,9 +25,6 @@
2525
use RRZE\Legal\Consent\Categories\Options as ConsentCategoriesOptions;
2626
use RRZE\Legal\Consent\Cookies\Options as ConsentCookiesOptions;
2727

28-
const RRZE_PHP_VERSION = '8.2';
29-
const RRZE_WP_VERSION = '6.6';
30-
3128
/**
3229
* SPL Autoloader (PSR-4).
3330
* @param string $class The fully-qualified class name.
@@ -50,60 +47,38 @@
5047
}
5148
});
5249

53-
// Register plugin hooks.
54-
register_activation_hook(__FILE__, __NAMESPACE__ . '\activation');
55-
register_deactivation_hook(__FILE__, __NAMESPACE__ . '\deactivation');
50+
// Load the plugin's text domain for localization.
51+
add_action('init', fn() => load_plugin_textdomain('rrze-legal', false, dirname(plugin_basename(__FILE__)) . '/languages'));
5652

57-
add_action('plugins_loaded', __NAMESPACE__ . '\loaded');
5853

59-
/**
60-
* Loads a plugin’s translated strings.
61-
*/
62-
function loadTextdomain()
63-
{
64-
load_plugin_textdomain('rrze-legal', false, dirname(plugin_basename(__FILE__)) . '/languages');
65-
}
54+
// Register activation hook for the plugin
55+
register_activation_hook(__FILE__, __NAMESPACE__ . '\activation');
56+
57+
// Register deactivation hook for the plugin
58+
register_deactivation_hook(__FILE__, __NAMESPACE__ . '\deactivation');
6659

6760
/**
68-
* System requirements verification.
69-
* @return string Return an error message.
61+
* Add an action hook for the 'plugins_loaded' hook.
62+
*
63+
* This code hooks into the 'plugins_loaded' action hook to execute a callback function when
64+
* WordPress has fully loaded all active plugins and the theme's functions.php file.
7065
*/
71-
function systemRequirements(): string
72-
{
73-
$error = '';
74-
if (version_compare(PHP_VERSION, RRZE_PHP_VERSION, '<')) {
75-
$error = sprintf(
76-
/* translators: 1: Server PHP version number, 2: Required PHP version number. */
77-
__('The server is running PHP version %1$s. The Plugin requires at least PHP version %2$s.', 'rrze-legal'),
78-
PHP_VERSION,
79-
RRZE_PHP_VERSION
80-
);
81-
} elseif (version_compare($GLOBALS['wp_version'], RRZE_WP_VERSION, '<')) {
82-
$error = sprintf(
83-
/* translators: 1: Server WordPress version number, 2: Required WordPress version number. */
84-
__('The server is running WordPress version %1$s. The Plugin requires at least WordPress version %2$s.', 'rrze-legal'),
85-
$GLOBALS['wp_version'],
86-
RRZE_WP_VERSION
87-
);
88-
}
89-
return $error;
90-
}
66+
add_action('plugins_loaded', __NAMESPACE__ . '\loaded');
9167

9268
/**
9369
* Activation callback function.
9470
*/
9571
function activation()
9672
{
97-
loadTextdomain();
9873
if ($error = systemRequirements()) {
9974
deactivate_plugins(plugin_basename(__FILE__));
10075
wp_die(
101-
esc_html(sprintf(
76+
sprintf(
10277
/* translators: 1: The plugin name, 2: The error string. */
10378
__('Plugins: %1$s: %2$s', 'rrze-legal'),
10479
plugin_basename(__FILE__),
10580
$error
106-
), 'post')
81+
)
10782
);
10883
}
10984
}
@@ -232,35 +207,89 @@ function fauDomains(): array
232207
}
233208

234209
/**
235-
* Execute on 'plugins_loaded' API/action.
236-
* @return void
210+
* Check system requirements for the plugin.
211+
*
212+
* This method checks if the server environment meets the minimum WordPress and PHP version requirements
213+
* for the plugin to function properly.
214+
*
215+
* @return string An error message string if requirements are not met, or an empty string if requirements are satisfied.
216+
*/
217+
function systemRequirements(): string
218+
{
219+
// Get the global WordPress version.
220+
global $wp_version;
221+
222+
// Get the PHP version.
223+
$phpVersion = phpversion();
224+
225+
// Initialize an error message string.
226+
$error = '';
227+
228+
// Check if the WordPress version is compatible with the plugin's requirement.
229+
if (!is_wp_version_compatible(plugin()->getRequiresWP())) {
230+
$error = sprintf(
231+
/* translators: 1: Server WordPress version number, 2: Required WordPress version number. */
232+
__('The server is running WordPress version %1$s. The plugin requires at least WordPress version %2$s.', 'rrze-legal'),
233+
$wp_version,
234+
plugin()->getRequiresWP()
235+
);
236+
} elseif (!is_php_version_compatible(plugin()->getRequiresPHP())) {
237+
// Check if the PHP version is compatible with the plugin's requirement.
238+
$error = sprintf(
239+
/* translators: 1: Server PHP version number, 2: Required PHP version number. */
240+
__('The server is running PHP version %1$s. The plugin requires at least PHP version %2$s.', 'rrze-legal'),
241+
$phpVersion,
242+
plugin()->getRequiresPHP()
243+
);
244+
}
245+
246+
// Return the error message string, which will be empty if requirements are satisfied.
247+
return $error;
248+
}
249+
250+
/**
251+
* Handle the loading of the plugin.
252+
*
253+
* This function is responsible for initializing the plugin, loading text domains for localization,
254+
* checking system requirements, and displaying error notices if necessary.
237255
*/
238256
function loaded()
239257
{
240258
if (!tosPluginDeactivation()) {
241259
return;
242260
}
243-
loadTextdomain();
261+
// Trigger the 'loaded' method of the main plugin instance.
244262
plugin()->loaded();
263+
// Check system requirements and store any error messages.
245264
if ($error = systemRequirements()) {
265+
// If there is an error, add an action to display an admin notice with the error message.
246266
add_action('admin_init', function () use ($error) {
267+
// Check if the current user has the capability to activate plugins.
247268
if (current_user_can('activate_plugins')) {
248-
$pluginData = get_plugin_data(plugin()->getFile());
249-
$pluginName = $pluginData['Name'];
269+
// Get plugin data to retrieve the plugin's name.
270+
$pluginName = plugin()->getName();
271+
272+
// Determine the admin notice tag based on network-wide activation.
250273
$tag = is_plugin_active_for_network(plugin()->getBaseName()) ? 'network_admin_notices' : 'admin_notices';
274+
275+
// Add an action to display the admin notice.
251276
add_action($tag, function () use ($pluginName, $error) {
252277
printf(
253278
'<div class="notice notice-error"><p>' .
254279
/* translators: 1: The plugin name, 2: The error string. */
255-
esc_html(__('Plugins: %1$s: %2$s', 'rrze-legal'), 'post') .
280+
esc_html__('Plugins: %1$s: %2$s', 'rrze-legal') .
256281
'</p></div>',
257-
esc_html($pluginName),
258-
esc_html($error)
282+
$pluginName,
283+
$error
259284
);
260285
});
261286
}
262287
});
288+
289+
// Return to prevent further initialization if there is an error.
263290
return;
264291
}
292+
293+
// If there are no errors, create an instance of the 'Main' class
265294
new Main;
266295
}

0 commit comments

Comments
 (0)