-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathactivation.php
110 lines (92 loc) · 2.28 KB
/
activation.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
<?php
/**
* The plugin activation hook.
*
* Unlike the rest of the plugin, we want this file to be as lax as possible, to increase the changes of it running on an older version of PHP or WordPress.
*
* @package SnapWP\Helper
*/
namespace SnapWP\Helper;
/**
* The minimum WordPress version required to run this plugin.
*
* @return string
*/
function min_wp_version() {
return '6.7';
}
/**
* The minimum PHP version required to run this plugin.
*
* @return string
*/
function min_php_version() {
return '7.4';
}
/**
* Checks whether the WordPress requirements are met.
*
* @return bool
*/
function has_wp_requirements() {
global $wp_version;
return version_compare( $wp_version, min_wp_version(), '>=' );
}
/**
* Checks whether the PHP requirements are met.
*
* @return bool
*/
function has_php_requirements() {
return version_compare( phpversion(), min_php_version(), '>=' );
}
/**
* Checks the requirements for the plugin, deactivating it if they are not met.
*
* @return void
*/
function check_requirements() {
$unmet_dependencies = [];
if ( ! has_wp_requirements() ) {
$unmet_dependencies['WordPress'] = min_wp_version();
}
if ( ! has_php_requirements() ) {
$unmet_dependencies['PHP'] = min_php_version();
}
$count = count( $unmet_dependencies );
// Return if all requirements are met.
if ( 0 === $count ) {
return;
}
// Deactivate the plugin if the requirements are not met.
deactivate_plugins( plugin_basename( __FILE__ ) );
$plugins = array_keys( $unmet_dependencies );
$versions = array_values( $unmet_dependencies );
$message = __( 'SnapWP Helper: The plugin has been deactivated because it requires ', 'snapwp-helper' );
for ( $i = 0; $i < $count; $i++ ) {
$message .= sprintf(
// translators: 1: Plugin name, 2: Required version.
__( '%1$s version %2$s or higher', 'snapwp-helper' ),
esc_html( $plugins[ $i ] ),
esc_html( $versions[ $i ] )
);
if ( $i < $count - 1 ) {
$message .= __( ' and ', 'snapwp-helper' );
}
}
// Die with the error message.
wp_die( esc_html( $message ) );
}
/**
* Callback for the activation hook.
*
* @return void
*/
function activation_callback() {
// Ensure the requirements are met.
check_requirements();
/**
* Fires when the plugin is activated.
*/
do_action( 'snapwp_helper/activate' );
}