-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #817 from WordPress/add-version-utils
Add version utilities
- Loading branch information
Showing
2 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/** | ||
* Trait WordPress\Plugin_Check\Traits\Version_Utils | ||
* | ||
* @package plugin-check | ||
*/ | ||
|
||
namespace WordPress\Plugin_Check\Traits; | ||
|
||
/** | ||
* Trait for version utilities. | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
trait Version_Utils { | ||
|
||
/** | ||
* Returns current major WordPress version. | ||
* | ||
* @since 1.4.0 | ||
* | ||
* @return string Stable WordPress version. | ||
*/ | ||
protected function get_wordpress_stable_version(): string { | ||
$version = $this->get_latest_version_info( 'current' ); | ||
|
||
// Strip off any -alpha, -RC, -beta suffixes. | ||
list( $version, ) = explode( '-', $version ); | ||
|
||
if ( preg_match( '#^\d.\d#', $version, $matches ) ) { | ||
$version = $matches[0]; | ||
} | ||
|
||
return $version; | ||
} | ||
|
||
/** | ||
* Returns WordPress latest version. | ||
* | ||
* @since 1.4.0 | ||
* | ||
* @return string WordPress latest version. | ||
*/ | ||
protected function get_wordpress_latest_version(): string { | ||
$version = $this->get_latest_version_info( 'current' ); | ||
|
||
return $version ?? get_bloginfo( 'version' ); | ||
} | ||
|
||
/** | ||
* Returns relative WordPress major version. | ||
* | ||
* @since 1.4.0 | ||
* | ||
* @param string $version WordPress major version. | ||
* @param int $steps Steps to find relative version. Defaults to 1 for next major version. | ||
* @return string Relative WordPress major version. | ||
*/ | ||
protected function get_wordpress_relative_major_version( string $version, int $steps = 1 ): string { | ||
if ( 0 === $steps ) { | ||
return $version; | ||
} | ||
|
||
$new_version = floatval( $version ) + ( 0.1 * $steps ); | ||
|
||
return (string) number_format( $new_version, 1 ); | ||
} | ||
|
||
/** | ||
* Returns specific information. | ||
* | ||
* @since 1.4.0 | ||
* | ||
* @param string $key The information key to retrieve. | ||
* @return mixed The requested information. | ||
*/ | ||
private function get_latest_version_info( string $key ) { | ||
$info = get_transient( 'wp_plugin_check_latest_version_info' ); | ||
|
||
if ( false === $info ) { | ||
$response = wp_remote_get( 'https://api.wordpress.org/core/version-check/1.7/' ); | ||
|
||
if ( ! is_wp_error( $response ) && 200 === wp_remote_retrieve_response_code( $response ) ) { | ||
$body = json_decode( wp_remote_retrieve_body( $response ), true ); | ||
|
||
if ( isset( $body['offers'] ) && ! empty( $body['offers'] ) ) { | ||
$info = reset( $body['offers'] ); | ||
set_transient( 'wp_plugin_check_latest_version_info', $info, DAY_IN_SECONDS ); | ||
} | ||
} | ||
} | ||
|
||
return array_key_exists( $key, $info ) ? $info[ $key ] : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/** | ||
* Tests for the Version_Utils trait. | ||
* | ||
* @package plugin-check | ||
*/ | ||
|
||
use WordPress\Plugin_Check\Traits\Version_Utils; | ||
|
||
class Version_Utils_Tests extends WP_UnitTestCase { | ||
|
||
use Version_Utils; | ||
|
||
protected $info_transient_key = 'wp_plugin_check_latest_version_info'; | ||
|
||
public function set_up() { | ||
parent::set_up(); | ||
|
||
$info_data = array( | ||
'response' => 'upgrade', | ||
'download' => 'https://downloads.wordpress.org/release/wordpress-6.7.1.zip', | ||
'locale' => 'en_US', | ||
'packages' => array( | ||
'full' => 'https://downloads.wordpress.org/release/wordpress-6.7.1.zip', | ||
'no_content' => 'https://downloads.wordpress.org/release/wordpress-6.7.1-no-content.zip', | ||
'new_bundled' => 'https://downloads.wordpress.org/release/wordpress-6.7.1-new-bundled.zip', | ||
'partial' => false, | ||
'rollback' => false, | ||
), | ||
'current' => '6.7.1', | ||
'version' => '6.7.1', | ||
'php_version' => '7.2.24', | ||
'mysql_version' => '5.5.5', | ||
'new_bundled' => '6.7', | ||
'partial_version' => false, | ||
); | ||
|
||
set_transient( $this->info_transient_key, $info_data ); | ||
} | ||
|
||
public function test_wordpress_latest_version() { | ||
$version = $this->get_wordpress_latest_version(); | ||
$this->assertSame( '6.7.1', $version ); | ||
} | ||
|
||
public function test_wordpress_stable_version() { | ||
$version = $this->get_wordpress_stable_version(); | ||
$this->assertSame( '6.7', $version ); | ||
} | ||
|
||
/** | ||
* @dataProvider data_wordpress_version_items | ||
*/ | ||
public function test_wordpress_relative_major_version( $version, $steps, $new_version ) { | ||
$result = $this->get_wordpress_relative_major_version( $version, $steps ); | ||
$this->assertSame( $new_version, $result ); | ||
} | ||
|
||
public function tear_down() { | ||
delete_transient( $this->info_transient_key ); | ||
parent::tear_down(); | ||
} | ||
|
||
public function data_wordpress_version_items() { | ||
return array( | ||
array( '6.7', 1, '6.8' ), | ||
array( '6.7', -1, '6.6' ), | ||
array( '6.7', 2, '6.9' ), | ||
array( '6.7', -2, '6.5' ), | ||
array( '5.9', 1, '6.0' ), | ||
array( '6.0', -1, '5.9' ), | ||
array( '5.9', 2, '6.1' ), | ||
array( '6.0', -2, '5.8' ), | ||
array( '5.8', 2, '6.0' ), | ||
array( '6.1', -2, '5.9' ), | ||
); | ||
} | ||
} |