-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrze-cli.php
142 lines (122 loc) · 5.11 KB
/
rrze-cli.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: RRZE CLI
Plugin URI: https://github.com/RRZE-Webteam/rrze-cli
Version: 1.1.0
Description: A set of WP-CLI commands to help manage a WordPress website.
Author: RRZE-Webteam
Author URI: https://blogs.fau.de/webworking/
License: GNU General Public License v2
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: rrze-cli
Domain Path: /languages
Requires at least: 6.7
Requires PHP: 8.2
*/
namespace RRZE\CLI;
defined('ABSPATH') || exit;
// Autoloader
require_once 'vendor/autoload.php';
// Load the plugin's text domain for localization.
add_action('init', fn() => load_plugin_textdomain('rrze-cli', false, dirname(plugin_basename(__FILE__)) . '/languages'));
/**
* 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');
/**
* Singleton pattern for initializing and accessing the main plugin instance.
*
* This method ensures that only one instance of the Plugin class is created and returned.
*
* @return Plugin The main instance of the Plugin class.
*/
function plugin()
{
// Declare a static variable to hold the instance.
static $instance;
// Check if the instance is not already created.
if (null === $instance) {
// Add a new instance of the Plugin class, passing the current file (__FILE__) as a parameter.
$instance = new Plugin(__FILE__);
}
// Return the main instance of the Plugin class.
return $instance;
}
/**
* 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-settings'),
$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-settings'),
$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()
{
// 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-settings') .
'</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 and trigger its 'loaded' method.
(new Main)->loaded();
}