| 1 |
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
|
| 3 |
use Automattic\Jetpack\Assets; |
| 4 |
use Automattic\Jetpack\Post_Media\Images; |
| 5 |
use Automattic\Jetpack\Stats\Tracking_Pixel as Stats_Tracking_Pixel; |
| 6 |
use Automattic\Jetpack\Sync\Functions; |
| 7 |
|
| 8 |
/** |
| 9 |
* Manages compatibility with the amp-wp plugin |
| 10 |
* |
| 11 |
* @see https://github.com/Automattic/amp-wp |
| 12 |
*/ |
| 13 |
class Jetpack_AMP_Support { |
| 14 |
|
| 15 |
/** |
| 16 |
* Apply custom AMP changes on the front-end. |
| 17 |
*/ |
| 18 |
public static function init() { |
| 19 |
|
| 20 |
// Add Stats tracking pixel on Jetpack sites when the Stats module is active. |
| 21 |
if ( |
| 22 |
Jetpack::is_module_active( 'stats' ) |
| 23 |
&& ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) |
| 24 |
) { |
| 25 |
add_action( 'amp_post_template_footer', array( 'Jetpack_AMP_Support', 'add_stats_pixel' ) ); |
| 26 |
} |
| 27 |
|
| 28 |
// Sharing. |
| 29 |
add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 ); |
| 30 |
add_filter( 'sharing_enqueue_scripts', array( 'Jetpack_AMP_Support', 'amp_disable_sharedaddy_css' ) ); |
| 31 |
add_action( 'wp_enqueue_scripts', array( 'Jetpack_AMP_Support', 'amp_enqueue_sharing_css' ) ); |
| 32 |
|
| 33 |
// Sharing for Reader mode. |
| 34 |
if ( function_exists( 'jetpack_social_menu_include_svg_icons' ) ) { |
| 35 |
add_action( 'amp_post_template_footer', 'jetpack_social_menu_include_svg_icons' ); |
| 36 |
} |
| 37 |
add_action( 'amp_post_template_css', array( 'Jetpack_AMP_Support', 'amp_reader_sharing_css' ), 10, 0 ); |
| 38 |
|
| 39 |
// enforce freedom mode for videopress. |
| 40 |
add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) ); |
| 41 |
|
| 42 |
// include Jetpack og tags when rendering native AMP head. |
| 43 |
add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) ); |
| 44 |
|
| 45 |
// Post rendering changes for legacy AMP. |
| 46 |
add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) ); |
| 47 |
|
| 48 |
// Disable Comment Likes. |
| 49 |
add_filter( 'jetpack_comment_likes_enabled', array( 'Jetpack_AMP_Support', 'comment_likes_enabled' ) ); |
| 50 |
|
| 51 |
// Transitional mode AMP should not have comment likes. |
| 52 |
add_filter( 'the_content', array( 'Jetpack_AMP_Support', 'disable_comment_likes_before_the_content' ) ); |
| 53 |
|
| 54 |
// Add post template metadata for legacy AMP. |
| 55 |
add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 ); |
| 56 |
|
| 57 |
// Filter photon image args for AMP Stories. |
| 58 |
add_filter( 'jetpack_photon_post_image_args', array( 'Jetpack_AMP_Support', 'filter_photon_post_image_args_for_stories' ), 10, 2 ); |
| 59 |
|
| 60 |
// Sync the amp-options. |
| 61 |
add_filter( 'jetpack_options_whitelist', array( 'Jetpack_AMP_Support', 'filter_jetpack_options_safelist' ) ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Disable the Comment Likes feature on AMP views. |
| 66 |
* |
| 67 |
* @param bool $enabled Should comment likes be enabled. |
| 68 |
*/ |
| 69 |
public static function comment_likes_enabled( $enabled ) { |
| 70 |
return $enabled && ! self::is_amp_request(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Apply custom AMP changes in wp-admin. |
| 75 |
*/ |
| 76 |
public static function admin_init() { |
| 77 |
// disable Likes metabox for post editor if AMP canonical disabled. |
| 78 |
add_filter( 'post_flair_disable', array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Is the page in AMP 'canonical mode'. |
| 83 |
* Used when themes register support for AMP with `add_theme_support( 'amp' )`. |
| 84 |
* |
| 85 |
* @return bool is_amp_canonical |
| 86 |
*/ |
| 87 |
public static function is_amp_canonical() { |
| 88 |
return function_exists( 'amp_is_canonical' ) && amp_is_canonical(); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Is AMP available for this request |
| 93 |
* This returns false for admin, CLI requests etc. |
| 94 |
* |
| 95 |
* @return bool is_amp_available |
| 96 |
*/ |
| 97 |
public static function is_amp_available() { |
| 98 |
return ( function_exists( 'amp_is_available' ) && amp_is_available() ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Does the page return AMP content. |
| 103 |
* |
| 104 |
* @return bool $is_amp_request Are we on am AMP view. |
| 105 |
*/ |
| 106 |
public static function is_amp_request() { |
| 107 |
$is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ); |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns true if the current request should return valid AMP content. |
| 111 |
* |
| 112 |
* @since 6.2.0 |
| 113 |
* |
| 114 |
* @param boolean $is_amp_request Is this request supposed to return valid AMP content? |
| 115 |
*/ |
| 116 |
return apply_filters( 'jetpack_is_amp_request', $is_amp_request ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Determines whether the legacy AMP post templates are being used. |
| 121 |
* |
| 122 |
* @since 10.6.0 |
| 123 |
* |
| 124 |
* @return bool |
| 125 |
*/ |
| 126 |
public static function is_amp_legacy() { |
| 127 |
return ( function_exists( 'amp_is_legacy' ) && amp_is_legacy() ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Remove content filters added by Jetpack. |
| 132 |
*/ |
| 133 |
public static function amp_disable_the_content_filters() { |
| 134 |
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 135 |
add_filter( 'protected_embeds_use_form_post', '__return_false' ); |
| 136 |
remove_filter( 'the_title', 'widont' ); |
| 137 |
} |
| 138 |
|
| 139 |
remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 ); |
| 140 |
remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Do not add comment likes on AMP requests. |
| 145 |
* |
| 146 |
* @param string $content Post content. |
| 147 |
*/ |
| 148 |
public static function disable_comment_likes_before_the_content( $content ) { |
| 149 |
if ( self::is_amp_request() ) { |
| 150 |
remove_filter( 'comment_text', 'comment_like_button', 12 ); |
| 151 |
} |
| 152 |
return $content; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Add Jetpack stats pixel. |
| 157 |
* |
| 158 |
* @since 6.2.1 |
| 159 |
*/ |
| 160 |
public static function add_stats_pixel() { |
| 161 |
if ( ! has_action( 'wp_footer', array( Stats_Tracking_Pixel::class, 'add_amp_pixel' ) ) ) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
$stats_data = Stats_Tracking_Pixel::build_view_data(); |
| 166 |
Stats_Tracking_Pixel::render_amp_footer( $stats_data ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Add publisher and image metadata to legacy AMP post. |
| 171 |
* |
| 172 |
* @since 6.2.0 |
| 173 |
* |
| 174 |
* @param array $metadata Metadata array. |
| 175 |
* @param WP_Post $post Post. |
| 176 |
* @return array Modified metadata array. |
| 177 |
*/ |
| 178 |
public static function amp_post_template_metadata( $metadata, $post ) { |
| 179 |
if ( isset( $metadata['publisher'] ) && ! isset( $metadata['publisher']['logo'] ) ) { |
| 180 |
$metadata = self::add_site_icon_to_metadata( $metadata ); |
| 181 |
} |
| 182 |
|
| 183 |
if ( ! isset( $metadata['image'] ) && ! empty( $post ) ) { |
| 184 |
$metadata = self::add_image_to_metadata( $metadata, $post ); |
| 185 |
} |
| 186 |
|
| 187 |
return $metadata; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Add blavatar to legacy AMP post metadata. |
| 192 |
* |
| 193 |
* @since 6.2.0 |
| 194 |
* |
| 195 |
* @param array $metadata Metadata. |
| 196 |
* |
| 197 |
* @return array Metadata. |
| 198 |
*/ |
| 199 |
private static function add_site_icon_to_metadata( $metadata ) { |
| 200 |
$size = 60; |
| 201 |
$site_icon_url = class_exists( 'Automattic\\Jetpack\\Sync\\Functions' ) ? Functions::site_icon_url( $size ) : ''; |
| 202 |
|
| 203 |
if ( function_exists( 'blavatar_domain' ) ) { |
| 204 |
$metadata['publisher']['logo'] = array( |
| 205 |
'@type' => 'ImageObject', |
| 206 |
'url' => blavatar_url( blavatar_domain( site_url() ), 'img', $size, self::staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ), |
| 207 |
'width' => $size, |
| 208 |
'height' => $size, |
| 209 |
); |
| 210 |
} elseif ( $site_icon_url ) { |
| 211 |
$metadata['publisher']['logo'] = array( |
| 212 |
'@type' => 'ImageObject', |
| 213 |
'url' => $site_icon_url, |
| 214 |
'width' => $size, |
| 215 |
'height' => $size, |
| 216 |
); |
| 217 |
} |
| 218 |
|
| 219 |
return $metadata; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Add image to legacy AMP post metadata. |
| 224 |
* |
| 225 |
* @since 6.2.0 |
| 226 |
* |
| 227 |
* @param array $metadata Metadata. |
| 228 |
* @param WP_Post $post Post. |
| 229 |
* @return array Metadata. |
| 230 |
*/ |
| 231 |
private static function add_image_to_metadata( $metadata, $post ) { |
| 232 |
$image_args = array( |
| 233 |
'fallback_to_avatars' => true, |
| 234 |
'avatar_size' => 200, |
| 235 |
// AMP already attempts these. |
| 236 |
'from_thumbnail' => false, |
| 237 |
'from_attachment' => false, |
| 238 |
); |
| 239 |
|
| 240 |
// Every source left enabled above is parsed out of the body. |
| 241 |
if ( \Automattic\Jetpack\SEO\Content_Gate::is_gated( $post ) ) { |
| 242 |
$image_args += array( |
| 243 |
'from_slideshow' => false, |
| 244 |
'from_gallery' => false, |
| 245 |
'from_blocks' => false, |
| 246 |
'from_html' => false, |
| 247 |
); |
| 248 |
} |
| 249 |
|
| 250 |
$image = Images::get_image( $post->ID, $image_args ); |
| 251 |
|
| 252 |
if ( empty( $image ) ) { |
| 253 |
return self::add_fallback_image_to_metadata( $metadata ); |
| 254 |
} |
| 255 |
|
| 256 |
if ( ! isset( $image['src_width'] ) ) { |
| 257 |
$dimensions = self::extract_image_dimensions_from_getimagesize( |
| 258 |
array( |
| 259 |
$image['src'] => false, |
| 260 |
) |
| 261 |
); |
| 262 |
|
| 263 |
if ( false !== $dimensions[ $image['src'] ] ) { |
| 264 |
$image['src_width'] = $dimensions['width']; |
| 265 |
$image['src_height'] = $dimensions['height']; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
$metadata['image'] = array( |
| 270 |
'@type' => 'ImageObject', |
| 271 |
'url' => $image['src'], |
| 272 |
); |
| 273 |
if ( isset( $image['src_width'] ) ) { |
| 274 |
$metadata['image']['width'] = $image['src_width']; |
| 275 |
} |
| 276 |
if ( isset( $image['src_width'] ) ) { |
| 277 |
$metadata['image']['height'] = $image['src_height']; |
| 278 |
} |
| 279 |
|
| 280 |
return $metadata; |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Add fallback image to legacy AMP post metadata. |
| 285 |
* |
| 286 |
* @since 6.2.0 |
| 287 |
* |
| 288 |
* @param array $metadata Metadata. |
| 289 |
* @return array Metadata. |
| 290 |
*/ |
| 291 |
private static function add_fallback_image_to_metadata( $metadata ) { |
| 292 |
/** This filter is documented in functions.opengraph.php */ |
| 293 |
$default_image = apply_filters( 'jetpack_open_graph_image_default', 'https://wordpress.com/i/blank.jpg' ); |
| 294 |
|
| 295 |
$metadata['image'] = array( |
| 296 |
'@type' => 'ImageObject', |
| 297 |
'url' => self::staticize_subdomain( $default_image ), |
| 298 |
'width' => 200, |
| 299 |
'height' => 200, |
| 300 |
); |
| 301 |
|
| 302 |
return $metadata; |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Return static WordPress.com domain to use to load resources from WordPress.com. |
| 307 |
* |
| 308 |
* @param string $domain Asset URL. |
| 309 |
*/ |
| 310 |
private static function staticize_subdomain( $domain ) { |
| 311 |
// deal with WPCOM vs Jetpack. |
| 312 |
if ( function_exists( 'staticize_subdomain' ) ) { |
| 313 |
return staticize_subdomain( $domain ); |
| 314 |
} else { |
| 315 |
return Assets::staticize_subdomain( $domain ); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Extract image dimensions via wpcom/imagesize, only on WPCOM |
| 321 |
* |
| 322 |
* @since 6.2.0 |
| 323 |
* |
| 324 |
* @param array $dimensions Dimensions. |
| 325 |
* @return array Dimensions. |
| 326 |
*/ |
| 327 |
private static function extract_image_dimensions_from_getimagesize( $dimensions ) { |
| 328 |
if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'require_lib' ) ) ) { |
| 329 |
return $dimensions; |
| 330 |
} |
| 331 |
require_lib( 'wpcom/imagesize' ); |
| 332 |
|
| 333 |
foreach ( $dimensions as $url => $value ) { |
| 334 |
if ( is_array( $value ) ) { |
| 335 |
continue; |
| 336 |
} |
| 337 |
$result = wpcom_getimagesize( $url ); |
| 338 |
if ( is_array( $result ) ) { |
| 339 |
$dimensions[ $url ] = array( |
| 340 |
'width' => $result[0], |
| 341 |
'height' => $result[1], |
| 342 |
); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
return $dimensions; |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Display Open Graph Meta tags in AMP views. |
| 351 |
*/ |
| 352 |
public static function amp_post_jetpack_og_tags() { |
| 353 |
if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { |
| 354 |
Jetpack::init()->check_open_graph(); |
| 355 |
} |
| 356 |
|
| 357 |
if ( function_exists( 'jetpack_og_tags' ) ) { |
| 358 |
jetpack_og_tags(); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Force Freedom mode in VideoPress. |
| 364 |
* |
| 365 |
* @param array $options Array of VideoPress shortcode options. |
| 366 |
*/ |
| 367 |
public static function videopress_enable_freedom_mode( $options ) { |
| 368 |
if ( self::is_amp_request() ) { |
| 369 |
$options['freedom'] = true; |
| 370 |
} |
| 371 |
return $options; |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* Display custom markup for the sharing buttons when in an AMP view. |
| 376 |
* |
| 377 |
* @param string $markup Content markup of the Jetpack sharing links. |
| 378 |
* @param array $sharing_enabled Array of Sharing Services currently enabled. |
| 379 |
*/ |
| 380 |
public static function render_sharing_html( $markup, $sharing_enabled ) { |
| 381 |
global $post; |
| 382 |
|
| 383 |
if ( empty( $post ) ) { |
| 384 |
return ''; |
| 385 |
} |
| 386 |
|
| 387 |
if ( ! self::is_amp_request() ) { |
| 388 |
return $markup; |
| 389 |
} |
| 390 |
|
| 391 |
remove_action( 'wp_footer', 'sharing_add_footer' ); |
| 392 |
if ( empty( $sharing_enabled ) ) { |
| 393 |
return $markup; |
| 394 |
} |
| 395 |
|
| 396 |
$sharing_links = array(); |
| 397 |
foreach ( $sharing_enabled['visible'] as $service ) { |
| 398 |
$sharing_link = $service->get_amp_display( $post ); |
| 399 |
if ( ! empty( $sharing_link ) ) { |
| 400 |
$sharing_links[] = $sharing_link; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
// Replace the existing unordered list with AMP sharing buttons. |
| 405 |
$markup = preg_replace( '#<ul>(.+)</ul>#', implode( '', $sharing_links ), $markup ); |
| 406 |
|
| 407 |
// Remove any lingering share-end list items. |
| 408 |
$markup = str_replace( '<li class="share-end"></li>', '', $markup ); |
| 409 |
|
| 410 |
return $markup; |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Tells Jetpack not to enqueue CSS for share buttons. |
| 415 |
* |
| 416 |
* @param bool $enqueue Whether or not to enqueue. |
| 417 |
* @return bool Whether or not to enqueue. |
| 418 |
*/ |
| 419 |
public static function amp_disable_sharedaddy_css( $enqueue ) { |
| 420 |
if ( self::is_amp_request() ) { |
| 421 |
$enqueue = false; |
| 422 |
} |
| 423 |
|
| 424 |
return $enqueue; |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* Enqueues the AMP specific sharing styles for the sharing icons. |
| 429 |
*/ |
| 430 |
public static function amp_enqueue_sharing_css() { |
| 431 |
if ( |
| 432 |
Jetpack::is_module_active( 'sharedaddy' ) |
| 433 |
&& self::is_amp_request() |
| 434 |
&& ! self::is_amp_legacy() |
| 435 |
) { |
| 436 |
wp_enqueue_style( 'sharedaddy-amp', plugin_dir_url( __DIR__ ) . 'modules/sharedaddy/amp-sharing.css', array( 'social-logos' ), JETPACK__VERSION ); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
/** |
| 441 |
* For the AMP Reader mode template, include styles that we need. |
| 442 |
*/ |
| 443 |
public static function amp_reader_sharing_css() { |
| 444 |
// If sharing is not enabled, we should not proceed to render the CSS. |
| 445 |
if ( ! defined( 'JETPACK_SOCIAL_LOGOS_DIR' ) || ! defined( 'JETPACK_SOCIAL_LOGOS_URL' ) || ! defined( 'WP_SHARING_PLUGIN_DIR' ) ) { |
| 446 |
return; |
| 447 |
} |
| 448 |
|
| 449 |
/* |
| 450 |
* We'll need to output the full contents of the 2 files |
| 451 |
* in the head on AMP views. We can't rely on regular enqueues here. |
| 452 |
* @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. |
| 453 |
* |
| 454 |
* phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 455 |
* phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 456 |
*/ |
| 457 |
$css = file_get_contents( JETPACK_SOCIAL_LOGOS_DIR . 'social-logos.css' ); |
| 458 |
$css = preg_replace( '#(?<=url\(")(?=social-logos\.)#', JETPACK_SOCIAL_LOGOS_URL, $css ); // Make sure font files get their absolute paths. |
| 459 |
echo $css; |
| 460 |
echo file_get_contents( WP_SHARING_PLUGIN_DIR . 'amp-sharing.css' ); |
| 461 |
|
| 462 |
/* |
| 463 |
* phpcs:enable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 464 |
* phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 465 |
*/ |
| 466 |
} |
| 467 |
|
| 468 |
/** |
| 469 |
* Ensure proper Photon image dimensions for AMP Stories. |
| 470 |
* |
| 471 |
* @param array $args Array of Photon Arguments. |
| 472 |
* @param array $details { |
| 473 |
* Array of image details. |
| 474 |
* |
| 475 |
* @type string $tag Image tag (Image HTML output). |
| 476 |
* @type string $src Image URL. |
| 477 |
* @type string $src_orig Original Image URL. |
| 478 |
* @type int|false $width Image width. |
| 479 |
* @type int|false $height Image height. |
| 480 |
* @type int|false $width_orig Original image width before constrained by content_width. |
| 481 |
* @type int|false $height_orig Original Image height before constrained by content_width. |
| 482 |
* @type string $transform_orig Original transform before constrained by content_width. |
| 483 |
* } |
| 484 |
* @return array Args. |
| 485 |
*/ |
| 486 |
public static function filter_photon_post_image_args_for_stories( $args, $details ) { |
| 487 |
if ( ! is_singular( 'amp_story' ) ) { |
| 488 |
return $args; |
| 489 |
} |
| 490 |
|
| 491 |
// Percentage-based dimensions are not allowed in AMP, so this shouldn't happen, but short-circuit just in case. |
| 492 |
if ( str_contains( $details['width_orig'], '%' ) || str_contains( $details['height_orig'], '%' ) ) { |
| 493 |
return $args; |
| 494 |
} |
| 495 |
|
| 496 |
$max_height = 1280; // See image size with the slug \AMP_Story_Post_Type::MAX_IMAGE_SIZE_SLUG. |
| 497 |
$transform = $details['transform_orig']; |
| 498 |
$width = $details['width_orig']; |
| 499 |
$height = $details['height_orig']; |
| 500 |
|
| 501 |
// If height is available, constrain to $max_height. |
| 502 |
if ( false !== $height ) { |
| 503 |
if ( $height > $max_height && false !== $height ) { |
| 504 |
$width = ( $max_height * $width ) / $height; |
| 505 |
$height = $max_height; |
| 506 |
} elseif ( $height > $max_height ) { |
| 507 |
$height = $max_height; |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
/* |
| 512 |
* Set a height if none is found. |
| 513 |
* If height is set in this manner and height is available, use `fit` instead of `resize` to prevent skewing. |
| 514 |
*/ |
| 515 |
if ( false === $height ) { |
| 516 |
$height = $max_height; |
| 517 |
if ( false !== $width ) { |
| 518 |
$transform = 'fit'; |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
// Build array of Photon args and expose to filter before passing to Photon URL function. |
| 523 |
$args = array(); |
| 524 |
|
| 525 |
if ( false !== $width && false !== $height ) { |
| 526 |
$args[ $transform ] = $width . ',' . $height; |
| 527 |
} elseif ( false !== $width ) { |
| 528 |
$args['w'] = $width; |
| 529 |
} elseif ( false !== $height ) { |
| 530 |
$args['h'] = $height; |
| 531 |
} |
| 532 |
|
| 533 |
return $args; |
| 534 |
} |
| 535 |
|
| 536 |
/** |
| 537 |
* Adds amp-options to the list of options to sync, if AMP is available |
| 538 |
* |
| 539 |
* @param array $options_safelist Safelist of options to sync. |
| 540 |
* |
| 541 |
* @return array Updated options safelist |
| 542 |
*/ |
| 543 |
public static function filter_jetpack_options_safelist( $options_safelist ) { |
| 544 |
if ( function_exists( 'is_amp_endpoint' ) ) { |
| 545 |
$options_safelist[] = 'amp-options'; |
| 546 |
} |
| 547 |
return $options_safelist; |
| 548 |
} |
| 549 |
} |
| 550 |
|