__NAMESPACE__ . '\render_block' )
);
}
}
add_action( 'init', __NAMESPACE__ . '\register_block' );
/**
* Instagram Gallery block render callback.
*
* @param array $attributes Array containing the Instagram Gallery block attributes.
* @param string $content The Instagram Gallery block content.
*
* @return string
*/
function render_block( $attributes, $content ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( ! array_key_exists( 'accessToken', $attributes ) ) {
return '';
}
$access_token = $attributes['accessToken'];
$columns = get_instagram_gallery_attribute( 'columns', $attributes );
$count = get_instagram_gallery_attribute( 'count', $attributes );
$is_stacked_on_mobile = get_instagram_gallery_attribute( 'isStackedOnMobile', $attributes );
$spacing = get_instagram_gallery_attribute( 'spacing', $attributes );
$grid_classes = Blocks::classes(
FEATURE_NAME,
$attributes,
array(
'wp-block-jetpack-instagram-gallery__grid',
'wp-block-jetpack-instagram-gallery__grid-columns-' . $columns,
( $is_stacked_on_mobile ? 'is-stacked-on-mobile' : null ),
)
);
$grid_style = sprintf(
'grid-gap: %1$spx; --latest-instagram-posts-spacing: %1$spx;',
$spacing
);
if ( ! class_exists( 'Jetpack_Instagram_Gallery_Helper' ) ) {
\jetpack_require_lib( 'class-jetpack-instagram-gallery-helper' );
}
$gallery = Jetpack_Instagram_Gallery_Helper::get_instagram_gallery( $access_token, $count );
if ( is_wp_error( $gallery ) || ! property_exists( $gallery, 'images' ) || 'ERROR' === $gallery->images ) {
if ( ! current_user_can( 'edit_post', get_the_ID() ) ) {
return '';
}
$connection_unavailable = is_wp_error( $gallery ) && 'instagram_connection_unavailable' === $gallery->get_error_code();
$error_message = $connection_unavailable
? $gallery->get_error_message()
: esc_html__( 'An error occurred in the Latest Instagram Posts block. Please try again later.', 'jetpack' );
$message = $error_message
. '
'
. esc_html__( '(Only administrators and the post author will see this message.)', 'jetpack' );
return Jetpack_Gutenberg::notice( $message, 'error', Blocks::classes( FEATURE_NAME, $attributes ) );
}
if ( empty( $gallery->images ) ) {
return '';
}
$images = array_slice( $gallery->images, 0, $count );
Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
ob_start();
?>