Skip to content
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 admin/section/class-convertkit-admin-section-oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private function maybe_get_and_store_access_token() {
array(
'access_token' => $result['access_token'],
'refresh_token' => $result['refresh_token'],
'token_expires' => ( $result['created_at'] + $result['expires_in'] ),
'token_expires' => ( time() + $result['expires_in'] ),
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function process_form( $step ) {
array(
'access_token' => $result['access_token'],
'refresh_token' => $result['refresh_token'],
'token_expires' => ( $result['created_at'] + $result['expires_in'] ),
'token_expires' => ( time() + $result['expires_in'] ),
)
);
break;
Expand Down
4 changes: 2 additions & 2 deletions includes/class-convertkit-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,15 +521,15 @@ public function update_credentials( $result, $client_id ) {
array(
'access_token' => $result['access_token'],
'refresh_token' => $result['refresh_token'],
'token_expires' => ( $result['created_at'] + $result['expires_in'] ),
'token_expires' => ( time() + $result['expires_in'] ),
)
);

// Clear any existing scheduled WordPress Cron event.
wp_clear_scheduled_hook( 'convertkit_refresh_token' );

// Schedule a WordPress Cron event to refresh the token on expiry.
wp_schedule_single_event( ( $result['created_at'] + $result['expires_in'] ), 'convertkit_refresh_token' );
wp_schedule_single_event( ( time() + $result['expires_in'] ), 'convertkit_refresh_token' );

}

Expand Down
2 changes: 1 addition & 1 deletion includes/cron-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function convertkit_refresh_token() {
array(
'access_token' => $result['access_token'],
'refresh_token' => $result['refresh_token'],
'token_expires' => ( $result['created_at'] + $result['expires_in'] ),
'token_expires' => ( time() + $result['expires_in'] ),
)
);

Expand Down
21 changes: 4 additions & 17 deletions tests/Integration/APITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ class APITest extends WPTestCase
*/
private $api;

/**
* Holds the current timestamp, defined in setUp to fix
* it for all tests.
*
* @since 2.8.3
*
* @var int
*/
private $now = 0;

/**
* Performs actions before each test.
*
Expand All @@ -46,9 +36,6 @@ public function setUp(): void
{
parent::setUp();

// Set the current timestamp to the start of the test.
$this->now = strtotime( 'now' );

// Activate Plugin, to include the Plugin's constants in tests.
activate_plugins('convertkit/wp-convertkit.php');

Expand Down Expand Up @@ -118,7 +105,7 @@ public function testCronEventCreatedWhenAccessTokenObtained()
// Confirm the Cron event to refresh the access token was created, and the timestamp to
// run the refresh token call matches the expiry of the access token.
$nextScheduledTimestamp = wp_next_scheduled( 'convertkit_refresh_token' );
$this->assertEquals( $nextScheduledTimestamp, $this->now + 10000 );
$this->assertGreaterThanOrEqual( $nextScheduledTimestamp, time() + 10000 );
}

/**
Expand All @@ -138,7 +125,7 @@ public function testCronEventCreatedWhenTokenRefreshed()
// Confirm the Cron event to refresh the access token was created, and the timestamp to
// run the refresh token call matches the expiry of the access token.
$nextScheduledTimestamp = wp_next_scheduled( 'convertkit_refresh_token' );
$this->assertEquals( $nextScheduledTimestamp, $this->now + 10000 );
$this->assertGreaterThanOrEqual( $nextScheduledTimestamp, time() + 10000 );
}

/**
Expand Down Expand Up @@ -211,8 +198,8 @@ public function mockTokenResponse( $response, $parsed_args, $url )
'access_token' => $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'],
'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'],
'token_type' => 'bearer',
'created_at' => $this->now,
'expires_in' => 10000,
'created_at' => 1735660800, // When the access token was created.
'expires_in' => 10000, // When the access token will expire, relative to the time the request was made.
'scope' => 'public',
)
),
Expand Down