PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / _inc / lib / class-jetpack-instagram-gallery-helper.php

class-jetpack-instagram-gallery-helper.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.5, at _inc/lib/class-jetpack-instagram-gallery-helper.php

100 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Assuming the json_decode succeeds, `images` can be an array of images or a string 'ERROR'.
77 $cached_images = $decoded_cached_gallery->images ?? null;
78 $cached_count = is_array( $cached_images ) ? count( $cached_images ) : 0;
79 if ( $cached_count >= $count ) {
80 return $decoded_cached_gallery;
81 }
82 }
83
84 $response = Client::wpcom_json_api_request_as_blog(
85 sprintf( '/sites/%d/instagram/%d?count=%d', $site_id, $access_token_id, $count ),
86 2,
87 array( 'headers' => array( 'content-type' => 'application/json' ) ),
88 null,
89 'wpcom'
90 );
91 if ( is_wp_error( $response ) ) {
92 return $response;
93 }
94
95 $gallery = wp_remote_retrieve_body( $response );
96 set_transient( $transient_key, $gallery, HOUR_IN_SECONDS );
97 return json_decode( $gallery );
98 }
99 }
100