| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles |
| 4 |
* |
| 5 |
* @package WP_Defender\Component\Security_Tweaks |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component\Security_Tweaks; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class WP_Version |
| 12 |
*/ |
| 13 |
class WP_Version extends Abstract_Security_Tweaks { |
| 14 |
|
| 15 |
/** |
| 16 |
* Component slug name. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
public string $slug = 'wp-version'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Check whether the issue has been resolved or not. |
| 24 |
* |
| 25 |
* @return bool |
| 26 |
*/ |
| 27 |
public function check() { |
| 28 |
return $this->is_resolved(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Here is the code for processing. |
| 33 |
* |
| 34 |
* @return bool |
| 35 |
*/ |
| 36 |
public function process() { |
| 37 |
return true; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* This is for un-do stuff that has be done in @process. |
| 42 |
* |
| 43 |
* @return bool |
| 44 |
*/ |
| 45 |
public function revert() { |
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Shield up. |
| 51 |
* |
| 52 |
* @return bool |
| 53 |
*/ |
| 54 |
public function shield_up() { |
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Check whether the issue is resolved or not. |
| 60 |
* |
| 61 |
* @return bool |
| 62 |
*/ |
| 63 |
private function is_resolved() { |
| 64 |
global $wp_version; |
| 65 |
|
| 66 |
return version_compare( $wp_version, $this->get_latest_version(), '=' ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get the latest WordPress version. |
| 71 |
* |
| 72 |
* @return string|false on failure |
| 73 |
*/ |
| 74 |
public function get_latest_version() { |
| 75 |
if ( ! function_exists( 'get_core_updates' ) ) { |
| 76 |
include_once ABSPATH . 'wp-admin/includes/update.php'; |
| 77 |
} |
| 78 |
|
| 79 |
$data = get_core_updates(); |
| 80 |
|
| 81 |
if ( ! is_array( $data ) || array() === $data ) { |
| 82 |
wp_version_check( array(), true ); |
| 83 |
$data = get_core_updates( array( 'dismissed' => true ) ); |
| 84 |
} |
| 85 |
|
| 86 |
// For bool value and empty array. |
| 87 |
if ( ! is_array( $data ) || array() === $data ) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
return reset( $data )->version; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Retrieve the tweak's label. |
| 96 |
* |
| 97 |
* @return string |
| 98 |
*/ |
| 99 |
public function get_label(): string { |
| 100 |
return esc_html__( 'Update WordPress to latest version', 'defender-security' ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get the error reason. |
| 105 |
* |
| 106 |
* @return string |
| 107 |
*/ |
| 108 |
public function get_error_reason(): string { |
| 109 |
return sprintf( |
| 110 |
/* translators: %s: WP Version */ |
| 111 |
esc_html__( |
| 112 |
'Your current WordPress version is out of date, which means you could be missing out on the latest security patches in v%s', |
| 113 |
'defender-security' |
| 114 |
), |
| 115 |
$this->get_latest_version() |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Return a summary data of this tweak. |
| 121 |
* |
| 122 |
* @return array |
| 123 |
*/ |
| 124 |
public function to_array(): array { |
| 125 |
return array( |
| 126 |
'slug' => $this->slug, |
| 127 |
'title' => $this->get_label(), |
| 128 |
'errorReason' => $this->get_error_reason(), |
| 129 |
'successReason' => esc_html__( 'You are using the latest version of WordPress, great job!', 'defender-security' ), |
| 130 |
'misc' => array( |
| 131 |
'latest_wp' => $this->get_latest_version(), |
| 132 |
'core_update_url' => network_admin_url( 'update-core.php' ), |
| 133 |
), |
| 134 |
'bulk_description' => esc_html__( |
| 135 |
'Your current WordPress version is out of date, which means you could be missing out on the latest update. We will upgrade WordPress version to the latest.', |
| 136 |
'defender-security' |
| 137 |
), |
| 138 |
'bulk_title' => esc_html__( 'WordPress Version', 'defender-security' ), |
| 139 |
); |
| 140 |
} |
| 141 |
} |
| 142 |
|