PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 15.0.3
Jetpack – WP Security, Backup, Speed, & Growth v15.0.3
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 14.5.1 14.6.1 14.7.1 14.8.1 14.9.2 15.0.3 15.1.2 15.2.1 15.3.2 15.4.1 15.5.1 15.6.1 15.7.2 15.8.1 15.9.2 16.0.2 16.1.3 16.2-a.5 16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8.0.1 8.0.2 8.0.3 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.2 8.2.0.1 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.3 8.3.1 8.3.2 8.3.3 8.4 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.5 8.5.1 8.5.2 8.5.3 8.6 8.6.1 8.6.2 8.6.3 8.6.4 8.7 8.7.0.1 8.7.1 8.7.2 8.7.3 8.7.4 8.8 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.9 8.9.1 8.9.2 8.9.3 8.9.4 9.0 9.0.1 9.0.2 9.0.3 9.0.4 9.0.5 9.1 9.1.1 9.1.2 9.1.3 9.2 9.2.1 9.2.2 9.2.3 9.2.4 9.3 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.4 9.4.1 9.4.2 9.4.3 9.4.4 9.5 9.5.1 9.5.2 9.5.3 9.5.4 9.5.5 9.6 9.6.1 9.6.2 9.6.3 9.6.4 9.7 9.7.1 9.7.2 15.7-beta.2 9.7.3 15.7.1 9.8 15.8-a.1 9.8.1 15.8-a.3 9.8.2 2.0.9 9.8.3 2.1.7 9.9 2.2.10 9.9.1 2.3.10 9.9.2 2.4.7 9.9.3 2.5.5 2.6.6 2.7.5 2.8.5 2.9.6 3.0.6 3.1.5 3.2.5 3.3.6 3.4.6 3.5.6 3.6.4 3.7.5 3.8.5 3.9.10 4.0.7 4.1.4 4.2.5 4.3.5 4.4.5 4.5.3 4.6.3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / 3rd-party / class.jetpack-amp-support.php
jetpack / 3rd-party Last commit date
debug-bar 11 months ago 3rd-party.php 1 year ago amp.php 1 year ago atomic.php 1 year ago bbpress.php 1 year ago beaverbuilder.php 1 year ago bitly.php 1 year ago buddypress.php 1 year ago class-domain-mapping.php 1 year ago class-jetpack-bbpress-rest-api.php 2 years ago class.jetpack-amp-support.php 1 year ago creative-mail.php 1 year ago debug-bar.php 1 year ago jetpack-backup.php 1 year ago jetpack-boost.php 1 year ago qtranslate-x.php 1 year ago vaultpress.php 1 year ago web-stories.php 1 year ago woocommerce-services.php 1 year ago woocommerce.php 1 year ago wpcom-reader.php 1 year ago wpml.php 1 year ago
class.jetpack-amp-support.php
540 lines
1 <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 use Automattic\Jetpack\Assets;
4 use Automattic\Jetpack\Stats\Tracking_Pixel as Stats_Tracking_Pixel;
5 use Automattic\Jetpack\Sync\Functions;
6
7 /**
8 * Manages compatibility with the amp-wp plugin
9 *
10 * @see https://github.com/Automattic/amp-wp
11 */
12 class Jetpack_AMP_Support {
13
14 /**
15 * Apply custom AMP changes on the front-end.
16 */
17 public static function init() {
18
19 // Add Stats tracking pixel on Jetpack sites when the Stats module is active.
20 if (
21 Jetpack::is_module_active( 'stats' )
22 && ! ( defined( 'IS_WPCOM' ) && IS_WPCOM )
23 ) {
24 add_action( 'amp_post_template_footer', array( 'Jetpack_AMP_Support', 'add_stats_pixel' ) );
25 }
26
27 // Sharing.
28 add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 );
29 add_filter( 'sharing_enqueue_scripts', array( 'Jetpack_AMP_Support', 'amp_disable_sharedaddy_css' ) );
30 add_action( 'wp_enqueue_scripts', array( 'Jetpack_AMP_Support', 'amp_enqueue_sharing_css' ) );
31
32 // Sharing for Reader mode.
33 if ( function_exists( 'jetpack_social_menu_include_svg_icons' ) ) {
34 add_action( 'amp_post_template_footer', 'jetpack_social_menu_include_svg_icons' );
35 }
36 add_action( 'amp_post_template_css', array( 'Jetpack_AMP_Support', 'amp_reader_sharing_css' ), 10, 0 );
37
38 // enforce freedom mode for videopress.
39 add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) );
40
41 // include Jetpack og tags when rendering native AMP head.
42 add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) );
43
44 // Post rendering changes for legacy AMP.
45 add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) );
46
47 // Disable Comment Likes.
48 add_filter( 'jetpack_comment_likes_enabled', array( 'Jetpack_AMP_Support', 'comment_likes_enabled' ) );
49
50 // Transitional mode AMP should not have comment likes.
51 add_filter( 'the_content', array( 'Jetpack_AMP_Support', 'disable_comment_likes_before_the_content' ) );
52
53 // Add post template metadata for legacy AMP.
54 add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 );
55
56 // Filter photon image args for AMP Stories.
57 add_filter( 'jetpack_photon_post_image_args', array( 'Jetpack_AMP_Support', 'filter_photon_post_image_args_for_stories' ), 10, 2 );
58
59 // Sync the amp-options.
60 add_filter( 'jetpack_options_whitelist', array( 'Jetpack_AMP_Support', 'filter_jetpack_options_safelist' ) );
61 }
62
63 /**
64 * Disable the Comment Likes feature on AMP views.
65 *
66 * @param bool $enabled Should comment likes be enabled.
67 */
68 public static function comment_likes_enabled( $enabled ) {
69 return $enabled && ! self::is_amp_request();
70 }
71
72 /**
73 * Apply custom AMP changes in wp-admin.
74 */
75 public static function admin_init() {
76 // disable Likes metabox for post editor if AMP canonical disabled.
77 add_filter( 'post_flair_disable', array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 );
78 }
79
80 /**
81 * Is the page in AMP 'canonical mode'.
82 * Used when themes register support for AMP with `add_theme_support( 'amp' )`.
83 *
84 * @return bool is_amp_canonical
85 */
86 public static function is_amp_canonical() {
87 return function_exists( 'amp_is_canonical' ) && amp_is_canonical();
88 }
89
90 /**
91 * Is AMP available for this request
92 * This returns false for admin, CLI requests etc.
93 *
94 * @return bool is_amp_available
95 */
96 public static function is_amp_available() {
97 return ( function_exists( 'amp_is_available' ) && amp_is_available() );
98 }
99
100 /**
101 * Does the page return AMP content.
102 *
103 * @return bool $is_amp_request Are we on am AMP view.
104 */
105 public static function is_amp_request() {
106 $is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() );
107
108 /**
109 * Returns true if the current request should return valid AMP content.
110 *
111 * @since 6.2.0
112 *
113 * @param boolean $is_amp_request Is this request supposed to return valid AMP content?
114 */
115 return apply_filters( 'jetpack_is_amp_request', $is_amp_request );
116 }
117
118 /**
119 * Determines whether the legacy AMP post templates are being used.
120 *
121 * @since 10.6.0
122 *
123 * @return bool
124 */
125 public static function is_amp_legacy() {
126 return ( function_exists( 'amp_is_legacy' ) && amp_is_legacy() );
127 }
128
129 /**
130 * Remove content filters added by Jetpack.
131 */
132 public static function amp_disable_the_content_filters() {
133 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
134 add_filter( 'protected_embeds_use_form_post', '__return_false' );
135 remove_filter( 'the_title', 'widont' );
136 }
137
138 remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 );
139 remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 );
140 }
141
142 /**
143 * Do not add comment likes on AMP requests.
144 *
145 * @param string $content Post content.
146 */
147 public static function disable_comment_likes_before_the_content( $content ) {
148 if ( self::is_amp_request() ) {
149 remove_filter( 'comment_text', 'comment_like_button', 12 );
150 }
151 return $content;
152 }
153
154 /**
155 * Add Jetpack stats pixel.
156 *
157 * @since 6.2.1
158 */
159 public static function add_stats_pixel() {
160 if ( ! has_action( 'wp_footer', array( Stats_Tracking_Pixel::class, 'add_amp_pixel' ) ) ) {
161 return;
162 }
163
164 $stats_data = Stats_Tracking_Pixel::build_view_data();
165 Stats_Tracking_Pixel::render_amp_footer( $stats_data );
166 }
167
168 /**
169 * Add publisher and image metadata to legacy AMP post.
170 *
171 * @since 6.2.0
172 *
173 * @param array $metadata Metadata array.
174 * @param WP_Post $post Post.
175 * @return array Modified metadata array.
176 */
177 public static function amp_post_template_metadata( $metadata, $post ) {
178 if ( isset( $metadata['publisher'] ) && ! isset( $metadata['publisher']['logo'] ) ) {
179 $metadata = self::add_site_icon_to_metadata( $metadata );
180 }
181
182 if ( ! isset( $metadata['image'] ) && ! empty( $post ) ) {
183 $metadata = self::add_image_to_metadata( $metadata, $post );
184 }
185
186 return $metadata;
187 }
188
189 /**
190 * Add blavatar to legacy AMP post metadata.
191 *
192 * @since 6.2.0
193 *
194 * @param array $metadata Metadata.
195 *
196 * @return array Metadata.
197 */
198 private static function add_site_icon_to_metadata( $metadata ) {
199 $size = 60;
200 $site_icon_url = class_exists( 'Automattic\\Jetpack\\Sync\\Functions' ) ? Functions::site_icon_url( $size ) : '';
201
202 if ( function_exists( 'blavatar_domain' ) ) {
203 $metadata['publisher']['logo'] = array(
204 '@type' => 'ImageObject',
205 'url' => blavatar_url( blavatar_domain( site_url() ), 'img', $size, self::staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ),
206 'width' => $size,
207 'height' => $size,
208 );
209 } elseif ( $site_icon_url ) {
210 $metadata['publisher']['logo'] = array(
211 '@type' => 'ImageObject',
212 'url' => $site_icon_url,
213 'width' => $size,
214 'height' => $size,
215 );
216 }
217
218 return $metadata;
219 }
220
221 /**
222 * Add image to legacy AMP post metadata.
223 *
224 * @since 6.2.0
225 *
226 * @param array $metadata Metadata.
227 * @param WP_Post $post Post.
228 * @return array Metadata.
229 */
230 private static function add_image_to_metadata( $metadata, $post ) {
231 $image = Jetpack_PostImages::get_image(
232 $post->ID,
233 array(
234 'fallback_to_avatars' => true,
235 'avatar_size' => 200,
236 // AMP already attempts these.
237 'from_thumbnail' => false,
238 'from_attachment' => false,
239 )
240 );
241
242 if ( empty( $image ) ) {
243 return self::add_fallback_image_to_metadata( $metadata );
244 }
245
246 if ( ! isset( $image['src_width'] ) ) {
247 $dimensions = self::extract_image_dimensions_from_getimagesize(
248 array(
249 $image['src'] => false,
250 )
251 );
252
253 if ( false !== $dimensions[ $image['src'] ] ) {
254 $image['src_width'] = $dimensions['width'];
255 $image['src_height'] = $dimensions['height'];
256 }
257 }
258
259 $metadata['image'] = array(
260 '@type' => 'ImageObject',
261 'url' => $image['src'],
262 );
263 if ( isset( $image['src_width'] ) ) {
264 $metadata['image']['width'] = $image['src_width'];
265 }
266 if ( isset( $image['src_width'] ) ) {
267 $metadata['image']['height'] = $image['src_height'];
268 }
269
270 return $metadata;
271 }
272
273 /**
274 * Add fallback image to legacy AMP post metadata.
275 *
276 * @since 6.2.0
277 *
278 * @param array $metadata Metadata.
279 * @return array Metadata.
280 */
281 private static function add_fallback_image_to_metadata( $metadata ) {
282 /** This filter is documented in functions.opengraph.php */
283 $default_image = apply_filters( 'jetpack_open_graph_image_default', 'https://wordpress.com/i/blank.jpg' );
284
285 $metadata['image'] = array(
286 '@type' => 'ImageObject',
287 'url' => self::staticize_subdomain( $default_image ),
288 'width' => 200,
289 'height' => 200,
290 );
291
292 return $metadata;
293 }
294
295 /**
296 * Return static WordPress.com domain to use to load resources from WordPress.com.
297 *
298 * @param string $domain Asset URL.
299 */
300 private static function staticize_subdomain( $domain ) {
301 // deal with WPCOM vs Jetpack.
302 if ( function_exists( 'staticize_subdomain' ) ) {
303 return staticize_subdomain( $domain );
304 } else {
305 return Assets::staticize_subdomain( $domain );
306 }
307 }
308
309 /**
310 * Extract image dimensions via wpcom/imagesize, only on WPCOM
311 *
312 * @since 6.2.0
313 *
314 * @param array $dimensions Dimensions.
315 * @return array Dimensions.
316 */
317 private static function extract_image_dimensions_from_getimagesize( $dimensions ) {
318 if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'require_lib' ) ) ) {
319 return $dimensions;
320 }
321 require_lib( 'wpcom/imagesize' );
322
323 foreach ( $dimensions as $url => $value ) {
324 if ( is_array( $value ) ) {
325 continue;
326 }
327 $result = wpcom_getimagesize( $url );
328 if ( is_array( $result ) ) {
329 $dimensions[ $url ] = array(
330 'width' => $result[0],
331 'height' => $result[1],
332 );
333 }
334 }
335
336 return $dimensions;
337 }
338
339 /**
340 * Display Open Graph Meta tags in AMP views.
341 */
342 public static function amp_post_jetpack_og_tags() {
343 if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
344 Jetpack::init()->check_open_graph();
345 }
346
347 if ( function_exists( 'jetpack_og_tags' ) ) {
348 jetpack_og_tags();
349 }
350 }
351
352 /**
353 * Force Freedom mode in VideoPress.
354 *
355 * @param array $options Array of VideoPress shortcode options.
356 */
357 public static function videopress_enable_freedom_mode( $options ) {
358 if ( self::is_amp_request() ) {
359 $options['freedom'] = true;
360 }
361 return $options;
362 }
363
364 /**
365 * Display custom markup for the sharing buttons when in an AMP view.
366 *
367 * @param string $markup Content markup of the Jetpack sharing links.
368 * @param array $sharing_enabled Array of Sharing Services currently enabled.
369 */
370 public static function render_sharing_html( $markup, $sharing_enabled ) {
371 global $post;
372
373 if ( empty( $post ) ) {
374 return '';
375 }
376
377 if ( ! self::is_amp_request() ) {
378 return $markup;
379 }
380
381 remove_action( 'wp_footer', 'sharing_add_footer' );
382 if ( empty( $sharing_enabled ) ) {
383 return $markup;
384 }
385
386 $sharing_links = array();
387 foreach ( $sharing_enabled['visible'] as $service ) {
388 $sharing_link = $service->get_amp_display( $post );
389 if ( ! empty( $sharing_link ) ) {
390 $sharing_links[] = $sharing_link;
391 }
392 }
393
394 // Replace the existing unordered list with AMP sharing buttons.
395 $markup = preg_replace( '#<ul>(.+)</ul>#', implode( '', $sharing_links ), $markup );
396
397 // Remove any lingering share-end list items.
398 $markup = str_replace( '<li class="share-end"></li>', '', $markup );
399
400 return $markup;
401 }
402
403 /**
404 * Tells Jetpack not to enqueue CSS for share buttons.
405 *
406 * @param bool $enqueue Whether or not to enqueue.
407 * @return bool Whether or not to enqueue.
408 */
409 public static function amp_disable_sharedaddy_css( $enqueue ) {
410 if ( self::is_amp_request() ) {
411 $enqueue = false;
412 }
413
414 return $enqueue;
415 }
416
417 /**
418 * Enqueues the AMP specific sharing styles for the sharing icons.
419 */
420 public static function amp_enqueue_sharing_css() {
421 if (
422 Jetpack::is_module_active( 'sharedaddy' )
423 && self::is_amp_request()
424 && ! self::is_amp_legacy()
425 ) {
426 wp_enqueue_style( 'sharedaddy-amp', plugin_dir_url( __DIR__ ) . 'modules/sharedaddy/amp-sharing.css', array( 'social-logos' ), JETPACK__VERSION );
427 }
428 }
429
430 /**
431 * For the AMP Reader mode template, include styles that we need.
432 */
433 public static function amp_reader_sharing_css() {
434 // If sharing is not enabled, we should not proceed to render the CSS.
435 if ( ! defined( 'JETPACK_SOCIAL_LOGOS_DIR' ) || ! defined( 'JETPACK_SOCIAL_LOGOS_URL' ) || ! defined( 'WP_SHARING_PLUGIN_DIR' ) ) {
436 return;
437 }
438
439 /*
440 * We'll need to output the full contents of the 2 files
441 * in the head on AMP views. We can't rely on regular enqueues here.
442 * @todo As of AMP plugin v1.5, you can actually rely on regular enqueues thanks to https://github.com/ampproject/amp-wp/pull/4299. Once WPCOM upgrades AMP, then this method can be eliminated.
443 *
444 * phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
445 * phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
446 */
447 $css = file_get_contents( JETPACK_SOCIAL_LOGOS_DIR . 'social-logos.css' );
448 $css = preg_replace( '#(?<=url\(")(?=social-logos\.)#', JETPACK_SOCIAL_LOGOS_URL, $css ); // Make sure font files get their absolute paths.
449 echo $css;
450 echo file_get_contents( WP_SHARING_PLUGIN_DIR . 'amp-sharing.css' );
451
452 /*
453 * phpcs:enable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
454 * phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
455 */
456 }
457
458 /**
459 * Ensure proper Photon image dimensions for AMP Stories.
460 *
461 * @param array $args Array of Photon Arguments.
462 * @param array $details {
463 * Array of image details.
464 *
465 * @type string $tag Image tag (Image HTML output).
466 * @type string $src Image URL.
467 * @type string $src_orig Original Image URL.
468 * @type int|false $width Image width.
469 * @type int|false $height Image height.
470 * @type int|false $width_orig Original image width before constrained by content_width.
471 * @type int|false $height_orig Original Image height before constrained by content_width.
472 * @type string $transform_orig Original transform before constrained by content_width.
473 * }
474 * @return array Args.
475 */
476 public static function filter_photon_post_image_args_for_stories( $args, $details ) {
477 if ( ! is_singular( 'amp_story' ) ) {
478 return $args;
479 }
480
481 // Percentage-based dimensions are not allowed in AMP, so this shouldn't happen, but short-circuit just in case.
482 if ( str_contains( $details['width_orig'], '%' ) || str_contains( $details['height_orig'], '%' ) ) {
483 return $args;
484 }
485
486 $max_height = 1280; // See image size with the slug \AMP_Story_Post_Type::MAX_IMAGE_SIZE_SLUG.
487 $transform = $details['transform_orig'];
488 $width = $details['width_orig'];
489 $height = $details['height_orig'];
490
491 // If height is available, constrain to $max_height.
492 if ( false !== $height ) {
493 if ( $height > $max_height && false !== $height ) {
494 $width = ( $max_height * $width ) / $height;
495 $height = $max_height;
496 } elseif ( $height > $max_height ) {
497 $height = $max_height;
498 }
499 }
500
501 /*
502 * Set a height if none is found.
503 * If height is set in this manner and height is available, use `fit` instead of `resize` to prevent skewing.
504 */
505 if ( false === $height ) {
506 $height = $max_height;
507 if ( false !== $width ) {
508 $transform = 'fit';
509 }
510 }
511
512 // Build array of Photon args and expose to filter before passing to Photon URL function.
513 $args = array();
514
515 if ( false !== $width && false !== $height ) {
516 $args[ $transform ] = $width . ',' . $height;
517 } elseif ( false !== $width ) {
518 $args['w'] = $width;
519 } elseif ( false !== $height ) {
520 $args['h'] = $height;
521 }
522
523 return $args;
524 }
525
526 /**
527 * Adds amp-options to the list of options to sync, if AMP is available
528 *
529 * @param array $options_safelist Safelist of options to sync.
530 *
531 * @return array Updated options safelist
532 */
533 public static function filter_jetpack_options_safelist( $options_safelist ) {
534 if ( function_exists( 'is_amp_endpoint' ) ) {
535 $options_safelist[] = 'amp-options';
536 }
537 return $options_safelist;
538 }
539 }
540