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

Production release v20240109.0 #5139

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion jetpack
Submodule jetpack updated 694 files
9 changes: 6 additions & 3 deletions jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Plugin URI: https://jetpack.com
* Description: Security, performance, and marketing tools made by WordPress experts. Jetpack keeps your site protected so you can focus on more important things.
* Author: Automattic
* Version: 12.8
* Version: 12.9
* Author URI: https://jetpack.com
* License: GPL2+
* Text Domain: jetpack
Expand All @@ -32,9 +32,12 @@ function vip_default_jetpack_version() {
} elseif ( version_compare( $wp_version, '6.2', '<' ) ) {
// WordPress 6.1.x
return '12.5';
} else {
// WordPress 6.2 and newer.
} elseif ( version_compare( $wp_version, '6.3', '<' ) ) {
// WordPress 6.2.x.
return '12.8';
} else {
// WordPress 6.3 and newer.
return '12.9';
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/test-jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public function test__vip_default_jetpack_version() {
global $wp_version;
$saved_wp_version = $wp_version;

$latest = '12.8';
$latest = '12.9';

$versions_map = [
// WordPress version => Jetpack version
Expand All @@ -16,7 +16,7 @@ public function test__vip_default_jetpack_version() {
'5.9.5' => '11.4',
'6.0' => '12.0',
'6.1' => '12.5',
'6.2' => $latest,
'6.2' => '12.8',
'6.3' => $latest,
'6.3.1' => $latest,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,136 +20,134 @@ protected static function getMethod( $name ) {
/**
* @group jetpack-required
* @preserveGlobalState disabled
* @dataProvider get_test_data__update_backoff_factor
* @dataProvider get_test_data__update_heartbeat_on_failure
*/
public function test__update_backoff_factor( ?int $backoff_factor, int $expected ) {
$option = array(
'site_url' => get_site_url(),
'hashed_site_url' => md5( get_site_url() ),
'cache_site_id' => (int) \Jetpack_Options::get_option( 'id', - 1 ),
'timestamp' => time(),
'backoff_factor' => $backoff_factor,
);
update_option( 'vip_jetpack_connection_pilot_heartbeat', $option, false );

$cp = Connection_Pilot::instance();
$update_backoff_factor = self::getMethod( 'update_backoff_factor' );
$update_backoff_factor->invoke( $cp );

$option = get_option( 'vip_jetpack_connection_pilot_heartbeat' );
$this->assertEquals( $expected, $option['backoff_factor'] );
public function test__update_heartbeat_on_failure( ?int $backoff_factor, int $expected_backoff, int $failed_attempts ) {
$last_failure = time() - 1000;

$this->set_heartbeat( [
'site_url' => 'example.com',
'backoff_factor' => $backoff_factor,
'failed_attempts' => $failed_attempts,
'failure_timestamp' => $last_failure,
] );

$connection_pilot = Connection_Pilot::instance();
$update_backoff_factor = self::getMethod( 'update_heartbeat_on_failure' );
$update_backoff_factor->invoke( $connection_pilot );

$option = $this->get_heartbeat();
$this->assertEquals( $expected_backoff, $option['backoff_factor'] );
$this->assertEquals( $failed_attempts + 1, $option['failed_attempts'] );
$this->assertTrue( $option['failure_timestamp'] > $last_failure );

// Doesn't change.
$this->assertEquals( 'example.com', $option['site_url'] );
}

/**
* @group jetpack-required
* @preserveGlobalState disabled
* @dataProvider get_test_data__update_heartbeat
*/
public function test__update_heartbeat( ?int $backoff_factor, array $expected ) {
$cp = Connection_Pilot::instance();
$update_heartbeat = self::getMethod( 'update_heartbeat' );

if ( $backoff_factor ) {
$update_heartbeat->invokeArgs( $cp, [ $backoff_factor ] );
} else {
$update_heartbeat->invoke( $cp );
}
public function test__update_heartbeat_on_success() {
$this->set_heartbeat( [
'site_url' => 'example.com',
'cache_site_id' => 22,
'backoff_factor' => 24,
'failed_attempts' => 2,
'failure_timestamp' => time() - 1000,
] );

$option = get_option( 'vip_jetpack_connection_pilot_heartbeat' );
$this->assertEquals( $expected['site_url'], $option['site_url'] );
$this->assertEquals( $expected['hashed_site_url'], $option['hashed_site_url'] );
$this->assertEquals( $expected['cache_site_id'], $option['cache_site_id'] );
$this->assertEquals( $expected['backoff_factor'], $option['backoff_factor'] );
$connection_pilot = Connection_Pilot::instance();
$update_heartbeat = self::getMethod( 'update_heartbeat_on_success' );
$update_heartbeat->invoke( $connection_pilot );

$option = $this->get_heartbeat();
$this->assertEquals( get_site_url(), $option['site_url'] );
$this->assertEquals( md5( get_site_url() ), $option['hashed_site_url'] );
$this->assertEquals( \Jetpack_Options::get_option( 'id', - 1 ), $option['cache_site_id'] );

// Resets.
$this->assertEquals( 0, $option['backoff_factor'] );
$this->assertEquals( 0, $option['failed_attempts'] );
$this->assertEquals( 0, $option['failure_timestamp'] );
}

/**
* @group jetpack-required
* @preserveGlobalState disabled
* @dataProvider get_test_data__should_back_off
*/
public function test__should_back_off( ?int $backoff_factor, DateTime $dt, bool $expected ) {
public function test__should_back_off( ?int $backoff_factor, ?DateTime $failure_time, ?DateTime $legacy_time, int $failed_attempts, bool $expected ) {
if ( null !== $backoff_factor ) {
$option = array(
'site_url' => get_site_url(),
'hashed_site_url' => md5( get_site_url() ),
'cache_site_id' => (int) \Jetpack_Options::get_option( 'id', - 1 ),
'timestamp' => $dt->getTimestamp(),
'backoff_factor' => $backoff_factor,
);
update_option( 'vip_jetpack_connection_pilot_heartbeat', $option, false );
$this->set_heartbeat( [
'backoff_factor' => $backoff_factor,
'failed_attempts' => $failed_attempts,
'failure_timestamp' => null === $failure_time ? $legacy_time->getTimestamp() : $failure_time->getTimestamp(),
] );
}

$cp = Connection_Pilot::instance();
$should_back_off = self::getMethod( 'should_back_off' );

$result = $should_back_off->invoke( $cp );
$connection_pilot = Connection_Pilot::instance();
$should_back_off = self::getMethod( 'should_back_off' );

$this->assertEquals( $expected, $result );
$this->assertEquals( $expected, $should_back_off->invoke( $connection_pilot ) );
}

public function get_test_data__update_backoff_factor() {
public function get_test_data__update_heartbeat_on_failure() {
$connection_pilot = Connection_Pilot::instance();
$increments = $connection_pilot::BACKOFF_INCREMENTS;
$max_increment = $connection_pilot::MAX_BACKOFF_FACTOR;

$test_data = [];
foreach ( $increments as $key => $increment ) {
if ( isset( $increments[ $key + 1 ] ) ) {
$test_data[ 'increment_' . $key ] = [ $increment, $increments[ $key + 1 ] ];
$test_data[ 'increment_' . $key ] = [ $increment, $increments[ $key + 1 ], $key ];
}
}

$test_data['null'] = [ null, $increments[0] ];
$test_data['zero'] = [ 0, $increments[0] ];
$test_data['max'] = [ $max_increment, $max_increment ];
$test_data['over_max'] = [ $max_increment + 1000, $max_increment ];
$test_data['null'] = [ null, $increments[0], 1 ];
$test_data['zero'] = [ 0, $increments[0], 2 ];
$test_data['max'] = [ $max_increment, $max_increment, 3 ];
$test_data['over_max'] = [ $max_increment + 1000, $max_increment, 4 ];

return $test_data;
}

public function get_test_data__update_heartbeat() {
public function get_test_data__should_back_off() {
$connection_pilot = Connection_Pilot::instance();
$max_retries = $connection_pilot::MAX_RETRIES;

// [ current backoff factor, last failure's timestamp, legacy timestamp, failed attempts, expected result ]
return [
'null' => [
null,
array(
'site_url' => get_site_url(),
'hashed_site_url' => md5( get_site_url() ),
'cache_site_id' => (int) \Jetpack_Options::get_option( 'id', - 1 ),
'timestamp' => time(),
'backoff_factor' => 0,
),
],
'zero' => [
0,
array(
'site_url' => get_site_url(),
'hashed_site_url' => md5( get_site_url() ),
'cache_site_id' => (int) \Jetpack_Options::get_option( 'id', - 1 ),
'timestamp' => time(),
'backoff_factor' => 0,
),
],
'one' => [
1,
array(
'site_url' => get_site_url(),
'hashed_site_url' => md5( get_site_url() ),
'cache_site_id' => (int) \Jetpack_Options::get_option( 'id', - 1 ),
'timestamp' => time(),
'backoff_factor' => 1,
),
],
'null' => [ null, new DateTime(), null, 0, false ],
'zero' => [ 0, new DateTime(), null, 0, false ],
'one-hour-true' => [ 1, new DateTime(), null, 0, true ],
'one-hour-false' => [ 1, ( new DateTime() )->modify( '-2 hours' ), null, 0, false ],
'eight-hours-true' => [ 8, new DateTime(), null, 0, true ],
'eight-hours-false' => [ 8, ( new DateTime() )->modify( '-9 hours' ), null, 0, false ],
'eight-hours-legacy-true' => [ 8, null, new DateTime(), 0, true ],
'eight-hours-legacy-false' => [ 8, null, ( new DateTime() )->modify( '-9 hours' ), 0, false ],
'exceeds-retry-limit-true' => [ 8, ( new DateTime() )->modify( '-9 hours' ), null, $max_retries + 1, true ],
];
}

public function get_test_data__should_back_off() {
return [
'null' => [ null, new DateTime(), false ],
'zero' => [ 0, new DateTime(), false ],
'one-hour-true' => [ 1, new DateTime(), true ],
'one-hour-false' => [ 1, ( new DateTime() )->modify( '-2 hours' ), false ],
'eight-hours-true' => [ 8, new DateTime(), true ],
'eight-hours-false' => [ 8, ( new DateTime() )->modify( '-9 hours' ), false ],
];
private function get_heartbeat() {
return get_option( 'vip_jetpack_connection_pilot_heartbeat', [] );
}

private function set_heartbeat( $values = [] ) {
$saved = update_option( 'vip_jetpack_connection_pilot_heartbeat', [
'site_url' => $values['site_url'] ?? get_site_url(),
'hashed_site_url' => isset( $values['site_url'] ) ? md5( $values['site_url'] ) : md5( get_site_url() ),
'cache_site_id' => $values['cache_site_id'] ?? (int) \Jetpack_Options::get_option( 'id', - 1 ),
'success_timestamp' => $values['success_timestamp'] ?? time(),
'backoff_factor' => $values['backoff_factor'] ?? 0,
'failed_attempts' => $values['failed_attempts'] ?? 0,
'failure_timestamp' => $values['failure_timestamp'] ?? 0,
], false );

if ( ! $saved ) {
throw new Error( 'Failed to set heartbeat' );
}
}
}
Loading
Loading