Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_Instagram_Gallery_Helper | |
0.00% |
0 / 45 |
|
0.00% |
0 / 2 |
156 | |
0.00% |
0 / 1 |
| is_instagram_access_token_valid | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
30 | |||
| get_instagram_gallery | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Instagram Gallery block and API helper. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | use Automattic\Jetpack\Connection\Client; |
| 9 | use Automattic\Jetpack\Connection\Manager; |
| 10 | |
| 11 | /** |
| 12 | * Class Jetpack_Instagram_Gallery_Helper |
| 13 | */ |
| 14 | class Jetpack_Instagram_Gallery_Helper { |
| 15 | const TRANSIENT_KEY_PREFIX = 'jetpack_instagram_gallery_block_'; |
| 16 | |
| 17 | /** |
| 18 | * Check whether an Instagram access token is valid, |
| 19 | * or has been permanently deleted elsewhere. |
| 20 | * |
| 21 | * @param string $access_token_id The ID of the external access token for Instagram. |
| 22 | * @return bool |
| 23 | */ |
| 24 | public static function is_instagram_access_token_valid( $access_token_id ) { |
| 25 | $site_id = Manager::get_site_id(); |
| 26 | if ( is_wp_error( $site_id ) ) { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 31 | if ( ! class_exists( 'WPCOM_Instagram_Gallery_Helper' ) ) { |
| 32 | \require_lib( 'instagram-gallery-helper' ); |
| 33 | } |
| 34 | $token = WPCOM_Instagram_Gallery_Helper::get_token( $access_token_id ); |
| 35 | return ! is_wp_error( $token ); |
| 36 | } |
| 37 | |
| 38 | $response = Client::wpcom_json_api_request_as_blog( |
| 39 | sprintf( '/sites/%d/instagram/%d/check-token', $site_id, $access_token_id ), |
| 40 | 2, |
| 41 | array( 'headers' => array( 'content-type' => 'application/json' ) ), |
| 42 | null, |
| 43 | 'wpcom' |
| 44 | ); |
| 45 | return 200 === wp_remote_retrieve_response_code( $response ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get the Instagram Gallery. |
| 50 | * |
| 51 | * @param string $access_token_id The ID of the external access token for Instagram. |
| 52 | * @param int $count The number of Instagram posts to fetch. |
| 53 | * @return mixed |
| 54 | */ |
| 55 | public static function get_instagram_gallery( $access_token_id, $count ) { |
| 56 | $site_id = Manager::get_site_id(); |
| 57 | if ( is_wp_error( $site_id ) ) { |
| 58 | return $site_id; |
| 59 | } |
| 60 | |
| 61 | $transient_key = self::TRANSIENT_KEY_PREFIX . $access_token_id; |
| 62 | |
| 63 | // Check if the connection exists before trying to retrieve the cached gallery. |
| 64 | if ( ! self::is_instagram_access_token_valid( $access_token_id ) ) { |
| 65 | delete_transient( $transient_key ); |
| 66 | return new WP_Error( |
| 67 | 'instagram_connection_unavailable', |
| 68 | __( 'The requested Instagram connection is not available anymore.', 'jetpack' ), |
| 69 | 403 |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | $cached_gallery = get_transient( $transient_key ); |
| 74 | if ( $cached_gallery ) { |
| 75 | $decoded_cached_gallery = json_decode( $cached_gallery ); |
| 76 | // `images` can be an array of images or a string 'ERROR'. |
| 77 | $cached_count = is_array( $decoded_cached_gallery->images ) ? count( $decoded_cached_gallery->images ) : 0; |
| 78 | if ( $cached_count >= $count ) { |
| 79 | return $decoded_cached_gallery; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | $response = Client::wpcom_json_api_request_as_blog( |
| 84 | sprintf( '/sites/%d/instagram/%d?count=%d', $site_id, $access_token_id, $count ), |
| 85 | 2, |
| 86 | array( 'headers' => array( 'content-type' => 'application/json' ) ), |
| 87 | null, |
| 88 | 'wpcom' |
| 89 | ); |
| 90 | if ( is_wp_error( $response ) ) { |
| 91 | return $response; |
| 92 | } |
| 93 | |
| 94 | $gallery = wp_remote_retrieve_body( $response ); |
| 95 | set_transient( $transient_key, $gallery, HOUR_IN_SECONDS ); |
| 96 | return json_decode( $gallery ); |
| 97 | } |
| 98 | } |