-
Notifications
You must be signed in to change notification settings - Fork 805
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the Package_Version_Tracker class (#20365)
* Add the jetpack_package_versions filter and the Package_Version_Tracker class. * Add the new Package_Version_Tracker class, which will obtain the package versions using the package versions using the jetpack_package_versions filter.
- Loading branch information
Showing
14 changed files
with
590 additions
and
1 deletion.
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
4 changes: 4 additions & 0 deletions
4
projects/packages/backup/changelog/update-package_version_filter
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,4 @@ | ||
Significance: patch | ||
Type: added | ||
|
||
Add package version tracking. |
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
46 changes: 46 additions & 0 deletions
46
projects/packages/backup/tests/php/test-package-version.php
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,46 @@ | ||
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | ||
|
||
namespace Automattic\Jetpack\Backup; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Unit tests for the Package_Version class. | ||
* | ||
* @package automattic/jetpack-backup | ||
*/ | ||
class Test_Package_Version extends TestCase { | ||
|
||
/** | ||
* Tests that the backup package version is added to the package verions array obtained by the | ||
* Package_Version_Tracker. | ||
*/ | ||
public function test_send_package_version_to_tracker_empty_array() { | ||
$expected = array( | ||
Package_Version::PACKAGE_SLUG => Package_Version::PACKAGE_VERSION, | ||
); | ||
|
||
add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' ); | ||
|
||
$this->assertSame( $expected, apply_filters( 'jetpack_package_versions', array() ) ); | ||
} | ||
|
||
/** | ||
* Tests that the backup package version is added to the package verions array obtained by the | ||
* Package_Version_Tracker. | ||
*/ | ||
public function test_send_package_version_to_tracker_existing_array() { | ||
$existing_array = array( | ||
'test-package-slug' => '1.0.0', | ||
); | ||
|
||
$expected = array_merge( | ||
$existing_array, | ||
array( Package_Version::PACKAGE_SLUG => Package_Version::PACKAGE_VERSION ) | ||
); | ||
|
||
add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' ); | ||
|
||
$this->assertSame( $expected, apply_filters( 'jetpack_package_versions', $existing_array ) ); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
projects/packages/connection/changelog/update-package_version_filter
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,4 @@ | ||
Significance: patch | ||
Type: added | ||
|
||
Add package version tracking. |
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
83 changes: 83 additions & 0 deletions
83
projects/packages/connection/src/class-package-version-tracker.php
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,83 @@ | ||
<?php | ||
/** | ||
* The Package_Version_Tracker class. | ||
* | ||
* @package automattic/jetpack-connection | ||
*/ | ||
|
||
namespace Automattic\Jetpack\Connection; | ||
|
||
/** | ||
* The Package_Version_Tracker class. | ||
*/ | ||
class Package_Version_Tracker { | ||
|
||
const PACKAGE_VERSION_OPTION = 'jetpack_package_versions'; | ||
|
||
/** | ||
* Uses the jetpack_package_versions filter to obtain the package versions from packages that need | ||
* version tracking. If the package versions have changed, updates the option and notifies WPCOM. | ||
*/ | ||
public function maybe_update_package_versions() { | ||
/** | ||
* Obtains the package versions. | ||
* | ||
* @since $$next_version$$ | ||
* | ||
* @param array An associative array of Jetpack package slugs and their corresponding versions as key/value pairs. | ||
*/ | ||
$filter_versions = apply_filters( 'jetpack_package_versions', array() ); | ||
|
||
if ( ! is_array( $filter_versions ) ) { | ||
return; | ||
} | ||
|
||
$option_versions = get_option( self::PACKAGE_VERSION_OPTION, array() ); | ||
|
||
foreach ( $filter_versions as $package => $version ) { | ||
if ( ! is_string( $package ) || ! is_string( $version ) ) { | ||
unset( $filter_versions[ $package ] ); | ||
} | ||
} | ||
|
||
if ( ! is_array( $option_versions ) | ||
|| count( array_diff_assoc( $filter_versions, $option_versions ) ) | ||
|| count( array_diff_assoc( $option_versions, $filter_versions ) ) | ||
) { | ||
$this->update_package_versions_option( $filter_versions ); | ||
} | ||
} | ||
|
||
/** | ||
* Updates the package versions: | ||
* - Sends the updated package versions to wpcom. | ||
* - Updates the 'jetpack_package_versions' option. | ||
* | ||
* @param array $package_versions The package versions. | ||
*/ | ||
protected function update_package_versions_option( $package_versions ) { | ||
$site_id = \Jetpack_Options::get_option( 'id' ); | ||
|
||
$body = wp_json_encode( | ||
array( | ||
'package_versions' => $package_versions, | ||
) | ||
); | ||
|
||
$response = Client::wpcom_json_api_request_as_blog( | ||
sprintf( '/sites/%d/jetpack-package-versions', $site_id ), | ||
'2', | ||
array( | ||
'headers' => array( 'content-type' => 'application/json' ), | ||
'method' => 'POST', | ||
), | ||
$body, | ||
'wpcom' | ||
); | ||
|
||
if ( 200 === wp_remote_retrieve_response_code( $response ) ) { | ||
update_option( self::PACKAGE_VERSION_OPTION, $package_versions ); | ||
} | ||
} | ||
|
||
} |
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
46 changes: 46 additions & 0 deletions
46
projects/packages/connection/tests/php/test-package-version.php
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,46 @@ | ||
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | ||
|
||
namespace Automattic\Jetpack\Connection; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Unit tests for the Package_Version class. | ||
* | ||
* @package automattic/jetpack-connection | ||
*/ | ||
class Test_Package_Version extends TestCase { | ||
|
||
/** | ||
* Tests that the connction package version is added to the package verions array obtained by the | ||
* Package_Version_Tracker. | ||
*/ | ||
public function test_send_package_version_to_tracker_empty_array() { | ||
$expected = array( | ||
Package_Version::PACKAGE_SLUG => Package_Version::PACKAGE_VERSION, | ||
); | ||
|
||
add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' ); | ||
|
||
$this->assertSame( $expected, apply_filters( 'jetpack_package_versions', array() ) ); | ||
} | ||
|
||
/** | ||
* Tests that the connection package version is added to the package verions array obtained by the | ||
* Package_Version_Tracker. | ||
*/ | ||
public function test_send_package_version_to_tracker_existing_array() { | ||
$existing_array = array( | ||
'test-package-slug' => '1.0.0', | ||
); | ||
|
||
$expected = array_merge( | ||
$existing_array, | ||
array( Package_Version::PACKAGE_SLUG => Package_Version::PACKAGE_VERSION ) | ||
); | ||
|
||
add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' ); | ||
|
||
$this->assertSame( $expected, apply_filters( 'jetpack_package_versions', $existing_array ) ); | ||
} | ||
} |
Oops, something went wrong.