PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
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 13.9.2 14.0.1 14.1.1 14.2.2 All 502 releases
jetpack / extensions / blocks / instagram-gallery / render.php

render.php in Jetpack – WP Security, Backup, Speed, & Growth 16.2, at extensions/blocks/instagram-gallery/render.php

139 lines 4.3 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 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