-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ef3b25
commit 4500584
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/** | ||
* Publicize Connections class. | ||
* | ||
* @package automattic/jetpack-publicize | ||
*/ | ||
|
||
namespace Automattic\Jetpack\Publicize; | ||
|
||
use Automattic\Jetpack\Publicize\Rest_Endpoints\Connections_Controller; | ||
use Automattic\Jetpack\Status\Host; | ||
|
||
/** | ||
* Publicize Connections class. | ||
*/ | ||
class Connections { | ||
|
||
const CONNECTIONS_TRANSIENT = 'jetpack_social_connections_list'; | ||
|
||
/** | ||
* Get all connections. | ||
* | ||
* @param array $args Arguments | ||
* - 'clear_cache': bool Whether to clear the cache. | ||
* - 'test_connections': bool Whether to test connections. | ||
* @return array | ||
*/ | ||
public static function get_all( $args = array() ) { | ||
|
||
$run_tests = $args['test_connections'] ?? false; | ||
|
||
$is_wpcom = ( new Host() )->is_wpcom_simple(); | ||
|
||
if ( $is_wpcom ) { | ||
// We don't need to cache connections for simple sites. | ||
return Connections_Controller::get_connections( $run_tests ); | ||
} | ||
|
||
$clear_cache = $args['clear_cache'] ?? false; | ||
|
||
if ( $clear_cache || $run_tests ) { | ||
self::clear_transient(); | ||
} | ||
|
||
$connections = get_transient( self::CONNECTIONS_TRANSIENT ); | ||
|
||
// This can be an empty array, so we need to check for false. | ||
if ( false === $connections ) { | ||
$connections = self::fetch_and_cache_connections( $run_tests ); | ||
} | ||
|
||
return $connections; | ||
} | ||
|
||
/** | ||
* Fetch connections from the REST API and cache them. | ||
* | ||
* @param bool $run_tests Whether to run connection tests. | ||
* | ||
* @return array | ||
*/ | ||
public static function fetch_and_cache_connections( $run_tests = false ) { | ||
$connections = Connections_Controller::get_connections( $run_tests ); | ||
|
||
if ( is_array( $connections ) ) { | ||
set_transient( self::CONNECTIONS_TRANSIENT, $connections, HOUR_IN_SECONDS * 4 ); | ||
} | ||
|
||
return $connections; | ||
} | ||
|
||
/** | ||
* Delete the transient. | ||
*/ | ||
protected static function clear_transient() { | ||
delete_transient( self::CONNECTIONS_TRANSIENT ); | ||
} | ||
} |