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 2 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
155 changes: 155 additions & 0 deletions includes/Traits/Version_Utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?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.0.0
ernilambar marked this conversation as resolved.
Show resolved Hide resolved
*
* @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 required PHP version.
*
* @since 1.4.0
*
* @return string Required PHP version.
*/
protected function get_wordpress_required_php_version(): string {
$version = $this->get_latest_version_info( 'php_version' );

return $version ?? $this->get_required_php_version();
}

/**
* Returns required MySQL version.
*
* @since 1.4.0
*
* @return string Required MySQL version.
*/
protected function get_wordpress_required_mysql_version(): string {
$version = $this->get_latest_version_info( 'mysql_version' );

return $version ?? $this->get_required_mysql_version();
}

/**
* Returns recommended PHP version.
*
* @since 1.4.0
*
* @return string Recommended PHP version.
*/
protected function get_wordpress_recommended_php_version(): string {
$recommended_version = '7.4'; // Default fallback recommended version.

$version_details = wp_check_php_version();

if ( is_array( $version_details ) && ! empty( $version_details['recommended_version'] ) ) {
$recommended_version = $version_details['recommended_version'];
}

return $recommended_version;
}

/**
* 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 107 in includes/Traits/Version_Utils.php

View check run for this annotation

Codecov / codecov/patch

includes/Traits/Version_Utils.php#L107

Added line #L107 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 110 in includes/Traits/Version_Utils.php

View check run for this annotation

Codecov / codecov/patch

includes/Traits/Version_Utils.php#L109-L110

Added lines #L109 - L110 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 114 in includes/Traits/Version_Utils.php

View check run for this annotation

Codecov / codecov/patch

includes/Traits/Version_Utils.php#L112-L114

Added lines #L112 - L114 were not covered by tests
}
}
}

return array_key_exists( $key, $info ) ? $info[ $key ] : null;
}

/**
* Returns the current WordPress' required PHP version.
ernilambar marked this conversation as resolved.
Show resolved Hide resolved
*
* @since 1.4.0
*
* @return string The current WordPress' required PHP version.
*/
private function get_required_php_version(): string {
static $required_php_version;

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

View check run for this annotation

Codecov / codecov/patch

includes/Traits/Version_Utils.php#L129-L130

Added lines #L129 - L130 were not covered by tests

if ( ! isset( $required_php_version ) ) {
require ABSPATH . WPINC . '/version.php';

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

View check run for this annotation

Codecov / codecov/patch

includes/Traits/Version_Utils.php#L132-L133

Added lines #L132 - L133 were not covered by tests
}

return $required_php_version;

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

View check run for this annotation

Codecov / codecov/patch

includes/Traits/Version_Utils.php#L136

Added line #L136 was not covered by tests
}

/**
* Returns the current WordPress' required MySQL version.
*
* @since 1.4.0
*
* @return string The current WordPress' required MySQL version.
*/
private function get_required_mysql_version(): string {
static $required_mysql_version;

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

View check run for this annotation

Codecov / codecov/patch

includes/Traits/Version_Utils.php#L146-L147

Added lines #L146 - L147 were not covered by tests

if ( ! isset( $required_mysql_version ) ) {
require ABSPATH . WPINC . '/version.php';

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

View check run for this annotation

Codecov / codecov/patch

includes/Traits/Version_Utils.php#L149-L150

Added lines #L149 - L150 were not covered by tests
}

return $required_mysql_version;

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

View check run for this annotation

Codecov / codecov/patch

includes/Traits/Version_Utils.php#L153

Added line #L153 was not covered by tests
}
}
1 change: 1 addition & 0 deletions tests/phpstan/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

define( 'WPINC', '' );
define( 'WP_PLUGIN_CHECK_PLUGIN_DIR_PATH', '' );
define( 'WP_PLUGIN_CHECK_PLUGIN_DIR_URL', '' );
85 changes: 85 additions & 0 deletions tests/phpunit/tests/Traits/Version_Utils_Tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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';

protected $php_check_transient_key = '';

public function set_up() {
parent::set_up();

$this->php_check_transient_key = 'php_check_' . md5( PHP_VERSION );

$php_check_data = array(
'recommended_version' => '7.4',
'minimum_version' => '7.2.24',
'is_supported' => true,
'is_secure' => true,
'is_acceptable' => true,
);

set_site_transient( $this->php_check_transient_key, $php_check_data );

$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 );
}

public function test_wordpress_required_php_version() {
$version = $this->get_wordpress_required_php_version();
$this->assertSame( '7.2.24', $version );
}

public function test_wordpress_required_mysql_version() {
$version = $this->get_wordpress_required_mysql_version();
$this->assertSame( '5.5.5', $version );
}

public function test_wordpress_recommended_php_version() {
$version = $this->get_wordpress_recommended_php_version();
$this->assertSame( '7.4', $version );
}

public function tear_down() {
delete_transient( $this->info_transient_key );
delete_site_transient( $this->php_check_transient_key );
parent::tear_down();
}
}
Loading