| 1 |
<?php |
| 2 |
/** |
| 3 |
* Trait WordPress\Plugin_Check\Traits\Version_Utils |
| 4 |
* |
| 5 |
* @package plugin-check |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WordPress\Plugin_Check\Traits; |
| 9 |
|
| 10 |
/** |
| 11 |
* Trait for version utilities. |
| 12 |
* |
| 13 |
* @since 1.3.1 |
| 14 |
*/ |
| 15 |
trait Version_Utils { |
| 16 |
|
| 17 |
/** |
| 18 |
* Returns current major WordPress version. |
| 19 |
* |
| 20 |
* @since 1.3.1 |
| 21 |
* |
| 22 |
* @return string Stable WordPress version. |
| 23 |
*/ |
| 24 |
protected function get_wordpress_stable_version(): string { |
| 25 |
$version = $this->get_latest_version_info( 'current' ); |
| 26 |
|
| 27 |
// Strip off any -alpha, -RC, -beta suffixes. |
| 28 |
list( $version, ) = explode( '-', $version ); |
| 29 |
|
| 30 |
if ( preg_match( '#^\d.\d#', $version, $matches ) ) { |
| 31 |
$version = $matches[0]; |
| 32 |
} |
| 33 |
|
| 34 |
return $version; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Returns WordPress latest version. |
| 39 |
* |
| 40 |
* @since 1.3.1 |
| 41 |
* |
| 42 |
* @return string WordPress latest version. |
| 43 |
*/ |
| 44 |
protected function get_wordpress_latest_version(): string { |
| 45 |
$version = $this->get_latest_version_info( 'current' ); |
| 46 |
|
| 47 |
return $version ?? get_bloginfo( 'version' ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns relative WordPress major version. |
| 52 |
* |
| 53 |
* @since 1.3.1 |
| 54 |
* |
| 55 |
* @param string $version WordPress major version. |
| 56 |
* @param int $steps Steps to find relative version. Defaults to 1 for next major version. |
| 57 |
* @return string Relative WordPress major version. |
| 58 |
*/ |
| 59 |
protected function get_wordpress_relative_major_version( string $version, int $steps = 1 ): string { |
| 60 |
if ( 0 === $steps ) { |
| 61 |
return $version; |
| 62 |
} |
| 63 |
|
| 64 |
$new_version = floatval( $version ) + ( 0.1 * $steps ); |
| 65 |
|
| 66 |
return (string) number_format( $new_version, 1 ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Returns specific information. |
| 71 |
* |
| 72 |
* @since 1.3.1 |
| 73 |
* |
| 74 |
* @param string $key The information key to retrieve. |
| 75 |
* @return mixed The requested information. |
| 76 |
*/ |
| 77 |
private function get_latest_version_info( string $key ) { |
| 78 |
$info = get_transient( 'wp_plugin_check_latest_version_info' ); |
| 79 |
|
| 80 |
if ( false === $info ) { |
| 81 |
$response = wp_remote_get( 'https://api.wordpress.org/core/version-check/1.7/' ); |
| 82 |
|
| 83 |
if ( ! is_wp_error( $response ) && 200 === wp_remote_retrieve_response_code( $response ) ) { |
| 84 |
$body = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 85 |
|
| 86 |
if ( isset( $body['offers'] ) && ! empty( $body['offers'] ) ) { |
| 87 |
$info = reset( $body['offers'] ); |
| 88 |
set_transient( 'wp_plugin_check_latest_version_info', $info, DAY_IN_SECONDS ); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return array_key_exists( $key, $info ) ? $info[ $key ] : null; |
| 94 |
} |
| 95 |
} |
| 96 |
|