PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 14.9.2
Jetpack – WP Security, Backup, Speed, & Growth v14.9.2
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 14.3.1 14.4.2 All 500 releases
jetpack / extensions / blocks / tiled-gallery / tiled-gallery.php
tiled-gallery.php
218 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Tiled Gallery block.
4 * Relies on Photon, but can be used even when the module is not active.
5 *
6 * @since 6.9.0
7 *
8 * @package automattic/jetpack
9 */
10
11 namespace Automattic\Jetpack\Extensions;
12
13 use Automattic\Jetpack\Blocks;
14 use Automattic\Jetpack\Current_Plan as Jetpack_Plan;
15 use Automattic\Jetpack\Status;
16 use Automattic\Jetpack\Status\Host;
17 use Jetpack;
18 use Jetpack_Gutenberg;
19
20 /**
21 * Jetpack Tiled Gallery Block class
22 *
23 * @since 7.3
24 */
25 class Tiled_Gallery {
26 /* Values for building srcsets */
27 const IMG_SRCSET_WIDTH_MAX = 2000;
28 const IMG_SRCSET_WIDTH_MIN = 600;
29 const IMG_SRCSET_WIDTH_STEP = 300;
30
31 /**
32 * Register the block
33 */
34 public static function register() {
35 if (
36 ( defined( 'IS_WPCOM' ) && IS_WPCOM )
37 || Jetpack::is_connection_ready()
38 || ( new Status() )->is_offline_mode()
39 ) {
40 Blocks::jetpack_register_block(
41 __DIR__,
42 array(
43 'render_callback' => array( __CLASS__, 'render' ),
44 )
45 );
46 }
47 }
48
49 /**
50 * Tiled gallery block registration
51 *
52 * @param array $attr Array containing the block attributes.
53 * @param string $content String containing the block content.
54 *
55 * @return string
56 */
57 public static function render( $attr, $content ) {
58 Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
59
60 $is_squareish_layout = self::is_squareish_layout( $attr );
61 // For backward compatibility (ensuring Tiled Galleries using now deprecated versions of the block are not affected).
62 // See isVIP() in utils/index.js.
63 $jetpack_plan = Jetpack_Plan::get();
64 wp_localize_script( 'jetpack-gallery-settings', 'jetpack_plan', array( 'data' => $jetpack_plan['product_slug'] ) );
65
66 if ( preg_match_all( '/<img [^>]+>/', $content, $images ) ) {
67 /**
68 * This block processes all of the images that are found and builds $find and $replace.
69 *
70 * The original img is added to the $find array and the replacement is made and added
71 * to the $replace array. This is so that the same find and replace operations can be
72 * made on the entire $content.
73 */
74 $find = array();
75 $replace = array();
76 $image_index = 0;
77 $number_images = count( $images[0] );
78
79 foreach ( $images[0] as $image_html ) {
80 if (
81 preg_match( '/data-width="([0-9]+)"/', $image_html, $img_width )
82 && preg_match( '/data-height="([0-9]+)"/', $image_html, $img_height )
83 && preg_match( '/src="([^"]+)"/', $image_html, $img_src )
84 ) {
85 ++$image_index;
86 // Drop img src query string so it can be used as a base to add photon params
87 // for the srcset.
88 $src_parts = explode( '?', $img_src[1], 2 );
89 $orig_src = $src_parts[0];
90 $orig_height = absint( $img_height[1] );
91 $orig_width = absint( $img_width[1] );
92
93 // Because URLs are already "photon", the photon function used short-circuits
94 // before ssl is added. Detect ssl and add is if necessary.
95 $is_ssl = ! empty( $src_parts[1] ) && str_contains( $src_parts[1], 'ssl=1' );
96
97 if ( ! $orig_width || ! $orig_height || ! $orig_src ) {
98 continue;
99 }
100
101 $srcset_parts = array();
102 if ( $is_squareish_layout ) {
103 $min_width = min( self::IMG_SRCSET_WIDTH_MIN, $orig_width, $orig_height );
104 $max_width = min( self::IMG_SRCSET_WIDTH_MAX, $orig_width, $orig_height );
105
106 for ( $w = $min_width; $w <= $max_width; $w = min( $max_width, $w + self::IMG_SRCSET_WIDTH_STEP ) ) {
107 $srcset_src = add_query_arg(
108 array(
109 'resize' => $w . ',' . $w,
110 'strip' => 'info',
111 ),
112 $orig_src
113 );
114 if ( $is_ssl ) {
115 $srcset_src = add_query_arg( 'ssl', '1', $srcset_src );
116 }
117 $srcset_parts[] = esc_url( $srcset_src ) . ' ' . $w . 'w';
118 if ( $w >= $max_width ) {
119 break;
120 }
121 }
122 } else {
123 $min_width = min( self::IMG_SRCSET_WIDTH_MIN, $orig_width );
124 $max_width = min( self::IMG_SRCSET_WIDTH_MAX, $orig_width );
125
126 for ( $w = $min_width; $w <= $max_width; $w = min( $max_width, $w + self::IMG_SRCSET_WIDTH_STEP ) ) {
127 $srcset_src = add_query_arg(
128 array(
129 'strip' => 'info',
130 'w' => $w,
131 ),
132 $orig_src
133 );
134 if ( $is_ssl ) {
135 $srcset_src = add_query_arg( 'ssl', '1', $srcset_src );
136 }
137 $srcset_parts[] = esc_url( $srcset_src ) . ' ' . $w . 'w';
138 if ( $w >= $max_width ) {
139 break;
140 }
141 }
142 }
143
144 $img_element = self::interactive_markup( $image_index, $number_images );
145
146 if ( ! empty( $srcset_parts ) ) {
147 $srcset = 'srcset="' . esc_attr( implode( ',', $srcset_parts ) ) . '"';
148
149 $find[] = $image_html;
150 $replace[] = str_replace( '<img', $img_element . $srcset, $image_html );
151 }
152 }
153 }
154
155 if ( ! empty( $find ) ) {
156 $content = str_replace( $find, $replace, $content );
157 }
158 }
159
160 /**
161 * Filter the output of the Tiled Galleries content.
162 *
163 * @module tiled-gallery
164 *
165 * @since 6.9.0
166 *
167 * @param string $content Tiled Gallery block content.
168 */
169 return apply_filters( 'jetpack_tiled_galleries_block_content', $content );
170 }
171
172 /**
173 * Adds tabindex, role and aria-label markup for images that should be interactive (front-end only).
174 *
175 * @param integer $image_index Integer The current image index.
176 * @param integer $number_images Integer The total number of images.
177 */
178 private static function interactive_markup( $image_index, $number_images ) {
179
180 $host = new Host();
181 $is_module_active = $host->is_wpcom_simple()
182 ? get_option( 'carousel_enable_it' )
183 : Jetpack::is_module_active( 'carousel' );
184
185 if ( $is_module_active ) {
186 $aria_label_content = sprintf(
187 /* Translators: %1$d is the current image index, %2$d is the total number of images. */
188 __( 'Open image %1$d of %2$d in full-screen', 'jetpack' ),
189 $image_index,
190 $number_images
191 );
192 $img_element = '<img role="button" tabindex="0" aria-label="' . esc_attr( $aria_label_content ) . '"';
193 } else {
194 $img_element = '<img ';
195 }
196 return $img_element;
197 }
198
199 /**
200 * Determines whether a Tiled Gallery block uses square or circle images (1:1 ratio)
201 *
202 * Layouts are block styles and will be available as `is-style-[LAYOUT]` in the className
203 * attribute. The default (rectangular) will be omitted.
204 *
205 * @param array $attr Attributes key/value array.
206 * @return boolean True if layout is squareish, otherwise false.
207 */
208 private static function is_squareish_layout( $attr ) {
209 return isset( $attr['className'] )
210 && (
211 'is-style-square' === $attr['className']
212 || 'is-style-circle' === $attr['className']
213 );
214 }
215 }
216
217 add_action( 'init', array( Tiled_Gallery::class, 'register' ) );
218