Skip to content

Commit

Permalink
Add the Package_Version_Tracker class (#20365)
Browse files Browse the repository at this point in the history
* 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
kbrown9 authored Aug 11, 2021
1 parent df461db commit 8f7b5b4
Show file tree
Hide file tree
Showing 14 changed files with 590 additions and 1 deletion.
3 changes: 3 additions & 0 deletions projects/packages/backup/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@

// Register REST routes.
add_action( 'rest_api_init', array( 'Automattic\\Jetpack\\Backup\\REST_Controller', 'register_rest_routes' ) );

// Set up package version hook.
add_filter( 'jetpack_package_versions', 'Automattic\\Jetpack\\Backup\\Package_Version::send_package_version_to_tracker' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Add package version tracking.
14 changes: 14 additions & 0 deletions projects/packages/backup/src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,18 @@ class Package_Version {

const PACKAGE_VERSION = '1.1.2-alpha';

const PACKAGE_SLUG = 'backup';

/**
* Adds the package slug and version to the package version tracker's data.
*
* @param array $package_versions The package version array.
*
* @return array The packge version array.
*/
public static function send_package_version_to_tracker( $package_versions ) {
$package_versions[ self::PACKAGE_SLUG ] = self::PACKAGE_VERSION;
return $package_versions;
}

}
46 changes: 46 additions & 0 deletions projects/packages/backup/tests/php/test-package-version.php
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 ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Add package version tracking.
4 changes: 4 additions & 0 deletions projects/packages/connection/src/class-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public static function configure() {

if ( $manager->is_connected() ) {
add_filter( 'xmlrpc_methods', array( $manager, 'public_xmlrpc_methods' ) );
add_filter( 'plugins_loaded', array( new Package_Version_Tracker(), 'maybe_update_package_versions' ) );
}

add_action( 'rest_api_init', array( $manager, 'initialize_rest_api_registration_connector' ) );
Expand All @@ -108,6 +109,9 @@ public static function configure() {
add_filter( 'jetpack_heartbeat_stats_array', array( $manager, 'add_stats_to_heartbeat' ) );

Webhooks::init( $manager );

// Set up package version hook.
add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' );
}

/**
Expand Down
83 changes: 83 additions & 0 deletions projects/packages/connection/src/class-package-version-tracker.php
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 );
}
}

}
16 changes: 15 additions & 1 deletion projects/packages/connection/src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,19 @@
*/
class Package_Version {

const PACKAGE_VERSION = '1.30.1';
const PACKAGE_VERSION = '1.30.2-alpha';

const PACKAGE_SLUG = 'connection';

/**
* Adds the package slug and version to the package version tracker's data.
*
* @param array $package_versions The package version array.
*
* @return array The packge version array.
*/
public static function send_package_version_to_tracker( $package_versions ) {
$package_versions[ self::PACKAGE_SLUG ] = self::PACKAGE_VERSION;
return $package_versions;
}
}
46 changes: 46 additions & 0 deletions projects/packages/connection/tests/php/test-package-version.php
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 ) );
}
}
Loading

0 comments on commit 8f7b5b4

Please sign in to comment.