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