PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.3
Defender Security – Malware Scanner, Login Security & Firewall v6.2.3
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / component / security-tweaks / class-php-version.php

class-php-version.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.3, at src/component/security-tweaks/class-php-version.php

194 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles checks and operations related to the PHP version used by the server.
4 *
5 * @package WP_Defender\Component\Security_Tweaks
6 */
7
8 namespace WP_Defender\Component\Security_Tweaks;
9
10 /**
11 * Handles checks and operations related to the PHP version used by the server.
12 */
13 class PHP_Version extends Abstract_Security_Tweaks {
14
15 /**
16 * Identifier slug for the component.
17 *
18 * @var string
19 */
20 public string $slug = 'php-version';
21 /**
22 * Minimum PHP version required.
23 *
24 * @var string|null
25 */
26 public $min_php = null;
27 /**
28 * Recommended stable PHP version.
29 *
30 * @var string|null
31 */
32 public $stable_php = null;
33
34 /**
35 * Current PHP version of the server.
36 *
37 * @var string
38 */
39 public $current_php = PHP_VERSION;
40
41 /**
42 * Check whether the issue has been resolved or not.
43 *
44 * @return bool
45 */
46 public function check() {
47 return $this->is_resolved();
48 }
49
50 /**
51 * Here is the code for processing.
52 *
53 * @return bool
54 */
55 public function process(): bool {
56 return true;
57 }
58
59 /**
60 * This is for un-do stuff that has to be done in @process.
61 *
62 * @return bool
63 */
64 public function revert(): bool {
65 return true;
66 }
67
68 /**
69 * Shield up.
70 *
71 * @return bool
72 */
73 public function shield_up(): bool {
74 return true;
75 }
76
77 /**
78 * Check wheter the issue is resolved or not.
79 *
80 * @return bool
81 */
82 private function is_resolved() {
83 $this->set_php_versions();
84
85 return version_compare( $this->current_php, $this->min_php, '>' );
86 }
87
88 /**
89 * Setup PHP versions.
90 *
91 * @return void
92 */
93 private function set_php_versions() {
94 if ( ! function_exists( 'wp_check_php_version' ) ) {
95 require_once ABSPATH . 'wp-admin/includes/misc.php';
96 }
97 $info = wp_check_php_version();
98
99 if ( ! $info ) {
100 $this->min_php = 'n/a';
101 $this->stable_php = 'n/a';
102
103 return;
104 }
105 // Ref: https://make.wordpress.org/core/handbook/references/php-compatibility-and-wordpress-versions/.
106 $supported_php = apply_filters(
107 "defender_{$this->slug}_supported_php",
108 array(
109 '8.0',
110 '8.1',
111 '8.2',
112 '8.3',
113 '8.4',
114 )
115 );
116 $this->stable_php = $info['recommended_version'];
117
118 // If the recommended version is lower than plugin's minimum requirement.
119 if ( version_compare( $this->stable_php, WP_DEFENDER_MIN_PHP_VERSION, '<' ) ) {
120 // WordPress may recommend a PHP version older than what plugin requires.
121 // In that case, use plugin's minimum requirement instead.
122 $this->stable_php = WP_DEFENDER_MIN_PHP_VERSION;
123 }
124
125 $position = array_search( $this->stable_php, $supported_php, true );
126
127 if (
128 false !== $position
129 && isset( $supported_php[ $position ] )
130 && is_string( $supported_php[ $position ] )
131 && '' !== $supported_php[ $position ]
132 ) {
133 $this->min_php = $supported_php[ $position ];
134 } else {
135 // Fallback to first supported version if search fails.
136 $this->min_php = reset( $supported_php );
137 }
138 }
139
140 /**
141 * Retrieve the tweak's label.
142 *
143 * @return string
144 */
145 public function get_label(): string {
146 return esc_html__( 'Update PHP', 'defender-security' );
147 }
148
149 /**
150 * Get the error reason.
151 *
152 * @return string
153 */
154 public function get_error_reason(): string {
155 $this->set_php_versions();
156
157 return sprintf(
158 /* translators: %s: Min PHP version. %s: Min PHP version. */
159 wp_strip_all_tags(
160 'PHP versions older than %1$s are no longer supported. For security and stability we strongly recommend you upgrade your PHP version to version %2$s or newer as soon as possible.',
161 'wpdef'
162 ),
163 $this->min_php,
164 $this->min_php
165 );
166 }
167
168 /**
169 * Return a summary data of this tweak.
170 *
171 * @return array
172 */
173 public function to_array(): array {
174 $this->set_php_versions();
175
176 return array(
177 'slug' => $this->slug,
178 'title' => $this->get_label(),
179 'errorReason' => $this->get_error_reason(),
180 'successReason' => wp_strip_all_tags( __( 'You have the latest version of PHP installed, good stuff!', 'defender-security' ) ),
181 'misc' => array(
182 'php_version' => $this->current_php,
183 'min_php_version' => $this->min_php,
184 'stable_php_version' => $this->stable_php,
185 'show_revert_button' => false,
186 'show_action_button' => false,
187 ),
188 'bulk_description' => array(
189 wp_strip_all_tags( __( 'PHP powers WordPress by processing code and generating your site’s pages. Update PHP to keep your site fast, reliable, secure, and compatible with supported versions.', 'defender-security' ) ),
190 ),
191 );
192 }
193 }
194