Skip to content

Commit

Permalink
Initialise the transient to random value as the Jetpack plugin updates
Browse files Browse the repository at this point in the history
  • Loading branch information
pablinos committed Oct 28, 2021
1 parent 5f5409a commit 6901c00
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions projects/plugins/jetpack/modules/publicize/publicize-jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

class Publicize extends Publicize_Base {

const CONNECTION_REFRESH_WAIT_TRANSIENT = 'jetpack_publicize_connection_refresh_wait';

function __construct() {
parent::__construct();

Expand Down Expand Up @@ -37,6 +39,8 @@ function __construct() {

add_filter( 'jetpack_sharing_twitter_via', array( $this, 'get_publicized_twitter_account' ), 10, 2 );

add_action( 'updating_jetpack_version', array( $this, 'init_refresh_transient' ) );

include_once( JETPACK__PLUGIN_DIR . 'modules/publicize/enhanced-open-graph.php' );

jetpack_require_lib( 'class.jetpack-keyring-service-helper' );
Expand Down Expand Up @@ -273,17 +277,29 @@ function unglobalize_connection( $connection_id ) {
}
}

/**
* As Jetpack updates set the refresh transient to a random amount
* in order to spread out updates to the connection data.
*
* @param string $version The Jetpack version being updated to.
*/
public function init_refresh_transient( $version ) {
if ( version_compare( $version, '10.2.1', '>=' ) && ! get_transient( self::CONNECTION_REFRESH_WAIT_TRANSIENT ) ) {
$this->set_refresh_wait_transient( wp_rand( 10, HOUR_IN_SECONDS * 24 ) );
}
}

/**
* Grabs a fresh copy of the publicize connections data.
* Only refreshes once every 12 hours or retries after an hour with an error.
*/
public function refresh_connections() {
if ( get_transient( 'jetpack_publicize_connection_refresh_wait' ) ) {
if ( get_transient( self::CONNECTION_REFRESH_WAIT_TRANSIENT ) ) {
return;
}
$xml = new Jetpack_IXR_Client();
$xml->query( 'jetpack.fetchPublicizeConnections' );
$wait_time = HOUR_IN_SECONDS * 12;
$wait_time = HOUR_IN_SECONDS * 24;

if ( ! $xml->isError() ) {
$response = $xml->getResponse();
Expand All @@ -293,7 +309,17 @@ public function refresh_connections() {
$wait_time = HOUR_IN_SECONDS;
}

set_transient( 'jetpack_publicize_connection_refresh_wait', microtime( true ), $wait_time );
$this->set_refresh_wait_transient( $wait_time );
}

/**
* Sets the transient to expire at the specified time in seconds.
* This prevents us from attempting to refresh the data too often.
*
* @param int $wait_time The number of seconds before the transient should expire.
*/
public function set_refresh_wait_transient( $wait_time ) {
set_transient( self::CONNECTION_REFRESH_WAIT_TRANSIENT, microtime( true ), $wait_time );
}

function connect_url( $service_name, $for = 'publicize' ) {
Expand Down

0 comments on commit 6901c00

Please sign in to comment.