Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version utilities #817

Merged
merged 8 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions includes/Traits/Version_Utils.php
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;

Check warning on line 61 in includes/Traits/Version_Utils.php

View check run for this annotation

Codecov / codecov/patch

includes/Traits/Version_Utils.php#L61

Added line #L61 was not covered by tests
}

$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/' );

Check warning on line 81 in includes/Traits/Version_Utils.php

View check run for this annotation

Codecov / codecov/patch

includes/Traits/Version_Utils.php#L81

Added line #L81 was not covered by tests

if ( ! is_wp_error( $response ) && 200 === wp_remote_retrieve_response_code( $response ) ) {
$body = json_decode( wp_remote_retrieve_body( $response ), true );

Check warning on line 84 in includes/Traits/Version_Utils.php

View check run for this annotation

Codecov / codecov/patch

includes/Traits/Version_Utils.php#L83-L84

Added lines #L83 - L84 were not covered by tests

if ( isset( $body['offers'] ) && ! empty( $body['offers'] ) ) {
$info = reset( $body['offers'] );
set_transient( 'wp_plugin_check_latest_version_info', $info, DAY_IN_SECONDS );

Check warning on line 88 in includes/Traits/Version_Utils.php

View check run for this annotation

Codecov / codecov/patch

includes/Traits/Version_Utils.php#L86-L88

Added lines #L86 - L88 were not covered by tests
}
}
}

return array_key_exists( $key, $info ) ? $info[ $key ] : null;
}
}
78 changes: 78 additions & 0 deletions tests/phpunit/tests/Traits/Version_Utils_Tests.php
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' ),
);
}
}
Loading