From b72a97b3f7ad5dcaac3e81044b3e0f9f61308e97 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Wed, 4 Dec 2024 11:15:58 +0545 Subject: [PATCH] Add method for finding relative WP major version --- includes/Traits/Version_Utils.php | 19 +++++++++++++++ .../tests/Traits/Version_Utils_Tests.php | 23 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/includes/Traits/Version_Utils.php b/includes/Traits/Version_Utils.php index 97f3c4525..de74aba48 100644 --- a/includes/Traits/Version_Utils.php +++ b/includes/Traits/Version_Utils.php @@ -92,6 +92,25 @@ protected function get_php_recommended_version(): string { return $recommended_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. * diff --git a/tests/phpunit/tests/Traits/Version_Utils_Tests.php b/tests/phpunit/tests/Traits/Version_Utils_Tests.php index 1dd7f8a07..e917984e2 100644 --- a/tests/phpunit/tests/Traits/Version_Utils_Tests.php +++ b/tests/phpunit/tests/Traits/Version_Utils_Tests.php @@ -77,9 +77,32 @@ public function test_wordpress_recommended_php_version() { $this->assertSame( '7.4', $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 ); delete_site_transient( $this->php_check_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' ), + ); + } }