| 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 ( false !== $position && ! empty( $supported_php[ $position ] ) ) { |
| 128 |
$this->min_php = $supported_php[ $position ]; |
| 129 |
} else { |
| 130 |
// Fallback to first supported version if search fails. |
| 131 |
$this->min_php = reset( $supported_php ); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Retrieve the tweak's label. |
| 137 |
* |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
public function get_label(): string { |
| 141 |
return esc_html__( 'Update PHP to latest version', 'defender-security' ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Get the error reason. |
| 146 |
* |
| 147 |
* @return string |
| 148 |
*/ |
| 149 |
public function get_error_reason(): string { |
| 150 |
$this->set_php_versions(); |
| 151 |
|
| 152 |
return sprintf( |
| 153 |
/* translators: %s: Min PHP version. %s: Min PHP version. */ |
| 154 |
esc_html__( |
| 155 |
'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.', |
| 156 |
'defender-security' |
| 157 |
), |
| 158 |
$this->min_php, |
| 159 |
$this->min_php |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Return a summary data of this tweak. |
| 165 |
* |
| 166 |
* @return array |
| 167 |
*/ |
| 168 |
public function to_array(): array { |
| 169 |
$this->set_php_versions(); |
| 170 |
|
| 171 |
return array( |
| 172 |
'slug' => $this->slug, |
| 173 |
'title' => $this->get_label(), |
| 174 |
'errorReason' => $this->get_error_reason(), |
| 175 |
'successReason' => esc_html__( 'You have the latest version of PHP installed, good stuff!', 'defender-security' ), |
| 176 |
'misc' => array( |
| 177 |
'php_version' => $this->current_php, |
| 178 |
'min_php_version' => $this->min_php, |
| 179 |
'stable_php_version' => $this->stable_php, |
| 180 |
), |
| 181 |
); |
| 182 |
} |
| 183 |
} |
| 184 |
|