instagram-gallery.php
168 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Instagram Gallery Block. |
| 4 | * |
| 5 | * @since 8.5.0 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\Instagram_Gallery; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | use Automattic\Jetpack\External_Connections; |
| 14 | use Jetpack; |
| 15 | use Jetpack_Gutenberg; |
| 16 | use Jetpack_Instagram_Gallery_Helper; |
| 17 | |
| 18 | if ( ! defined( 'ABSPATH' ) ) { |
| 19 | exit( 0 ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Registers the block for use in Gutenberg |
| 24 | * This is done via an action so that we can disable |
| 25 | * registration if we need to. |
| 26 | */ |
| 27 | function register_block() { |
| 28 | if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || Jetpack::is_connection_ready() ) { |
| 29 | Blocks::jetpack_register_block( |
| 30 | __DIR__, |
| 31 | array( 'render_callback' => __NAMESPACE__ . '\render_block' ) |
| 32 | ); |
| 33 | |
| 34 | External_Connections::add_settings_for_service( |
| 35 | 'writing', |
| 36 | array( |
| 37 | 'service' => 'instagram-basic-display', |
| 38 | 'title' => __( 'Instagram', 'jetpack' ), |
| 39 | 'description' => __( 'Display your more recent images from Instagram.', 'jetpack' ), |
| 40 | 'support_link' => array( |
| 41 | 'wpcom' => 'https://wordpress.com/support/instagram/#embed-a-feed-of-instagram-posts', |
| 42 | 'jetpack' => 'latest-instagram-posts-block', |
| 43 | ), |
| 44 | ) |
| 45 | ); |
| 46 | } |
| 47 | } |
| 48 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 49 | |
| 50 | /** |
| 51 | * Instagram Gallery block render callback. |
| 52 | * |
| 53 | * @param array $attributes Array containing the Instagram Gallery block attributes. |
| 54 | * @param string $content The Instagram Gallery block content. |
| 55 | * |
| 56 | * @return string |
| 57 | */ |
| 58 | function render_block( $attributes, $content ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 59 | if ( ! array_key_exists( 'accessToken', $attributes ) ) { |
| 60 | return ''; |
| 61 | } |
| 62 | |
| 63 | $access_token = $attributes['accessToken']; |
| 64 | $columns = get_instagram_gallery_attribute( 'columns', $attributes ); |
| 65 | $count = get_instagram_gallery_attribute( 'count', $attributes ); |
| 66 | $is_stacked_on_mobile = get_instagram_gallery_attribute( 'isStackedOnMobile', $attributes ); |
| 67 | $spacing = get_instagram_gallery_attribute( 'spacing', $attributes ); |
| 68 | |
| 69 | $grid_classes = Blocks::classes( |
| 70 | Blocks::get_block_feature( __DIR__ ), |
| 71 | $attributes, |
| 72 | array( |
| 73 | 'wp-block-jetpack-instagram-gallery__grid', |
| 74 | 'wp-block-jetpack-instagram-gallery__grid-columns-' . $columns, |
| 75 | ( $is_stacked_on_mobile ? 'is-stacked-on-mobile' : null ), |
| 76 | ) |
| 77 | ); |
| 78 | |
| 79 | $grid_style = sprintf( |
| 80 | 'grid-gap: %1$spx; --latest-instagram-posts-spacing: %1$spx;', |
| 81 | $spacing |
| 82 | ); |
| 83 | |
| 84 | if ( ! class_exists( 'Jetpack_Instagram_Gallery_Helper' ) ) { |
| 85 | require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-instagram-gallery-helper.php'; |
| 86 | } |
| 87 | $gallery = Jetpack_Instagram_Gallery_Helper::get_instagram_gallery( $access_token, $count ); |
| 88 | |
| 89 | if ( ! is_object( $gallery ) || is_wp_error( $gallery ) || ! property_exists( $gallery, 'images' ) || 'ERROR' === $gallery->images ) { |
| 90 | if ( ! current_user_can( 'edit_post', get_the_ID() ) ) { |
| 91 | return ''; |
| 92 | } |
| 93 | |
| 94 | $connection_unavailable = is_wp_error( $gallery ) && 'instagram_connection_unavailable' === $gallery->get_error_code(); |
| 95 | |
| 96 | $error_message = $connection_unavailable |
| 97 | ? $gallery->get_error_message() |
| 98 | : esc_html__( 'An error occurred in the Latest Instagram Posts block. Please try again later.', 'jetpack' ); |
| 99 | |
| 100 | $message = $error_message |
| 101 | . '<br />' |
| 102 | . esc_html__( '(Only administrators and the post author will see this message.)', 'jetpack' ); |
| 103 | return Jetpack_Gutenberg::notice( $message, 'error', Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attributes ) ); |
| 104 | } |
| 105 | |
| 106 | if ( empty( $gallery->images ) ) { |
| 107 | return ''; |
| 108 | } |
| 109 | |
| 110 | $images = array_slice( $gallery->images, 0, $count ); |
| 111 | |
| 112 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 113 | |
| 114 | ob_start(); |
| 115 | ?> |
| 116 | <?php if ( Blocks::is_amp_request() ) : ?> |
| 117 | <style> |
| 118 | .wp-block-jetpack-instagram-gallery__grid .wp-block-jetpack-instagram-gallery__grid-post amp-img img { |
| 119 | object-fit: cover; |
| 120 | } |
| 121 | </style> |
| 122 | <?php endif; ?> |
| 123 | <div class="<?php echo esc_attr( $grid_classes ); ?>" style="<?php echo esc_attr( $grid_style ); ?>"> |
| 124 | <?php foreach ( $images as $image ) : ?> |
| 125 | <a |
| 126 | class="wp-block-jetpack-instagram-gallery__grid-post" |
| 127 | href="<?php echo esc_url( $image->link ); ?>" |
| 128 | rel="noopener noreferrer" |
| 129 | target="_blank" |
| 130 | > |
| 131 | <img |
| 132 | alt="<?php echo esc_attr( $image->title ? $image->title : $image->link ); ?>" |
| 133 | src="<?php echo esc_url( $image->url ); ?>" |
| 134 | loading="lazy" |
| 135 | /> |
| 136 | </a> |
| 137 | <?php endforeach; ?> |
| 138 | </div> |
| 139 | |
| 140 | <?php |
| 141 | return ob_get_clean(); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Get Instagram Gallery block attribute. |
| 146 | * |
| 147 | * @param string $attribute String containing the attribute name to get. |
| 148 | * @param array $attributes Array containing the Instagram Gallery block attributes. |
| 149 | * |
| 150 | * @return mixed |
| 151 | */ |
| 152 | function get_instagram_gallery_attribute( $attribute, $attributes ) { |
| 153 | if ( array_key_exists( $attribute, $attributes ) ) { |
| 154 | return $attributes[ $attribute ]; |
| 155 | } |
| 156 | |
| 157 | $default_attributes = array( |
| 158 | 'columns' => 3, |
| 159 | 'count' => 9, |
| 160 | 'isStackedOnMobile' => true, |
| 161 | 'spacing' => 10, |
| 162 | ); |
| 163 | |
| 164 | if ( array_key_exists( $attribute, $default_attributes ) ) { |
| 165 | return $default_attributes[ $attribute ]; |
| 166 | } |
| 167 | } |
| 168 |