Skip to content

Commit

Permalink
Create publicize Connections class
Browse files Browse the repository at this point in the history
  • Loading branch information
manzoorwanijk committed Dec 11, 2024
1 parent 2ef3b25 commit 4500584
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions projects/packages/publicize/src/class-connections.php
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 );
}
}

0 comments on commit 4500584

Please sign in to comment.