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