-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrrze-calendar.php
175 lines (151 loc) · 5.59 KB
/
rrze-calendar.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
<?php
/*
Plugin Name: RRZE Calendar
Plugin URI: https://github.com/RRZE-Webteam/rrze-calendar
Version: 2.3.0
Description: Administration of local events and import of public events.
Author: RRZE Webteam
Author URI: https://blogs.fau.de/webworking/
License: GNU General Public License Version 3
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Text Domain: rrze-calendar
Domain Path: /languages
Requires at least: 6.7
Requires PHP: 8.2
*/
namespace RRZE\Calendar;
defined('ABSPATH') || exit;
use RRZE\Calendar\CPT\CalendarEvent;
use RRZE\Calendar\CPT\CalendarFeed;
// Autoloader
require_once 'vendor/autoload.php';
// Load the plugin's text domain for localization.
add_action('init', fn() => load_plugin_textdomain('rrze-calendar', 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()
{
settings()->loaded();
// Register the 'CalendarEvent' and 'CalendarFeed' custom post types and flush rewrite rules.
CalendarEvent::registerPostType();
CalendarFeed::registerPostType();
flush_rewrite_rules();
}
/**
* 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 Settings class.
* @return object Settings
*/
function settings()
{
static $instance;
if (null === $instance) {
$instance = new Settings();
}
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-calendar'),
$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-calendar'),
$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-calendar') .
'</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();
}