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