This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathRequirements.php
125 lines (119 loc) · 3.32 KB
/
Requirements.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
<?php
/**
* {{The Plugin Name}}
*
* @package {{the-plugin-name}}
* @author {{author_name}} <{{author_email}}>
* @copyright {{author_copyright}}
* @license {{author_license}}
* @link {{author_url}}
*/
declare( strict_types = 1 );
namespace ThePluginName\Config;
use ThePluginName\Common\Abstracts\Base;
use ThePluginName\Common\Utils\Errors;
/**
* Check if any requirements are needed to run this plugin. We use the
* "Requirements" package from "MicroPackage" to check if any PHP Extensions,
* plugins, themes or PHP/WP version are required.
* @docs https://github.com/micropackage/requirements
*
* @package ThePluginName\Config
* @since 1.0.0
*/
final class Requirements extends Base {
/**
* Specifications for the requirements
*
* @return array : used to specify the requirements
* @since 1.0.0
*/
public function specifications(): array {
return apply_filters( 'the_plugin_name_plugin_requirements', [
'php' => $this->plugin->requiredPhp(),
'php_extensions' => [
/**
* 'mbstring'
*/
],
'wp' => $this->plugin->requiredWp(),
'plugins' => [
/**
* [
* 'file' => 'hello-dolly/hello.php',
* 'name' => 'Hello Dolly',
* 'version' => '1.5'
* ],
*/
],
] );
}
/**
* Plugin requirements checker
*
* @since 1.0.0
*/
public function check() {
// We use "Requirements" if the package is required and installed by composer.json
if ( class_exists( '\Micropackage\Requirements\Requirements' ) ) {
$this->requirements = new \Micropackage\Requirements\Requirements(
$this->plugin->name(),
$this->specifications()
);
if ( ! $this->requirements->satisfied() ) {
// Print notice
$this->requirements->print_notice();
// Kill plugin
Errors::pluginDie();
}
} else {
// Else we do a version check based on version_compare
$this->versionCompare();
}
}
/**
* Compares PHP & WP versions and kills plugin if it's not compatible
*
* @since 1.0.0
*/
public function versionCompare() {
foreach (
[
// PHP version check
[
'current' => phpversion(),
'compare' => $this->plugin->requiredPhp(),
'title' => __( 'Invalid PHP version', 'the-plugin-name-text-domain' ),
'message' => sprintf( /* translators: %1$1s: required php version, %2$2s: current php version */
__( 'You must be using PHP %1$1s or greater. You are currently using PHP %2$2s.', 'the-plugin-name-text-domain' ),
$this->plugin->requiredPhp(),
phpversion()
),
],
// WP version check
[
'current' => get_bloginfo( 'version' ),
'compare' => $this->plugin->requiredWp(),
'title' => __( 'Invalid WordPress version', 'the-plugin-name-text-domain' ),
'message' => sprintf( /* translators: %1$1s: required wordpress version, %2$2s: current wordpress version */
__( 'You must be using WordPress %1$1s or greater. You are currently using WordPress %2$2s.', 'the-plugin-name-text-domain' ),
$this->plugin->requiredWp(),
get_bloginfo( 'version' )
),
],
] as $compat_check ) {
if ( version_compare(
$compat_check['compare'],
$compat_check['current'],
'>='
) ) {
// Kill plugin
Errors::pluginDie(
$compat_check['message'],
$compat_check['title'],
plugin_basename( __FILE__ )
);
}
}
}
}