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 the Package_Version_Tracker class #20365

Merged
merged 14 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
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.
16 changes: 15 additions & 1 deletion projects/packages/backup/src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
*/
class Package_Version {

const PACKAGE_VERSION = '1.1.1';
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.
5 changes: 5 additions & 0 deletions projects/packages/connection/src/class-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public static function configure() {
add_filter( 'jetpack_heartbeat_stats_array', array( $manager, 'add_stats_to_heartbeat' ) );

Webhooks::init( $manager );

add_filter( 'plugins_loaded', array( new Package_Version_Tracker(), 'maybe_update_package_versions' ) );

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

/**
Expand Down
107 changes: 107 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,107 @@
<?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 x.x.x
kbrown9 marked this conversation as resolved.
Show resolved Hide resolved
*
* @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() );

$package_versions = static::prep_package_versions( $option_versions, $filter_versions );

if ( ! is_array( $option_versions ) ) {
$this->update_package_versions_option( $package_versions );
return;
}

if ( count( array_diff_assoc( $package_versions, $option_versions ) ) || count( array_diff_assoc( $option_versions, $package_versions ) ) ) {
$this->update_package_versions_option( $package_versions );
}
}

/**
* Prepares the package versions by verifying that the package versions provided by the filter are
* strings. If the filter provided a version that isn't a string, check whether the existing option version
* is a string and, if it is, use that.
*
* @param array $option_versions The package versions stored in the option.
* @param array $filter_versions The package versions provided by the filter.
*
* @return array The package versions.
*/
protected function prep_package_versions( $option_versions, $filter_versions ) {
$new_versions = array();

foreach ( $filter_versions as $package => $version ) {
if ( ! is_string( $package ) ) {
continue;
}

if ( is_string( $version ) ) {
$new_versions[ $package ] = $version;
} elseif ( isset( $option_versions[ $package ] ) && is_string( $option_versions[ $package ] ) ) {
$new_versions[ $package ] = $option_versions[ $package ];
kbrown9 marked this conversation as resolved.
Show resolved Hide resolved
}
}

return $new_versions;
}

/**
* Updates the package versions:
* - Updates the 'jetpack_package_versions' option.
* - Sends the updated package versions to wpcom.
*
* @param array $package_versions The package versions.
*/
protected function update_package_versions_option( $package_versions ) {
update_option( self::PACKAGE_VERSION_OPTION, $package_versions );
fgiannar marked this conversation as resolved.
Show resolved Hide resolved

$site_id = \Jetpack_Options::get_option( 'id' );

$body = wp_json_encode(
array(
'package_versions' => $package_versions,
)
);

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

}
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