'ImageObject',
'url' => blavatar_url( blavatar_domain( site_url() ), 'img', $size, self::staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ),
'width' => $size,
'height' => $size,
);
} elseif ( $site_icon_url ) {
$metadata['publisher']['logo'] = array(
'@type' => 'ImageObject',
'url' => $site_icon_url,
'width' => $size,
'height' => $size,
);
}
return $metadata;
}
/**
* Add image to legacy AMP post metadata.
*
* @since 6.2.0
*
* @param array $metadata Metadata.
* @param WP_Post $post Post.
* @return array Metadata.
*/
private static function add_image_to_metadata( $metadata, $post ) {
$image = Jetpack_PostImages::get_image(
$post->ID,
array(
'fallback_to_avatars' => true,
'avatar_size' => 200,
// AMP already attempts these.
'from_thumbnail' => false,
'from_attachment' => false,
)
);
if ( empty( $image ) ) {
return self::add_fallback_image_to_metadata( $metadata );
}
if ( ! isset( $image['src_width'] ) ) {
$dimensions = self::extract_image_dimensions_from_getimagesize(
array(
$image['src'] => false,
)
);
if ( false !== $dimensions[ $image['src'] ] ) {
$image['src_width'] = $dimensions['width'];
$image['src_height'] = $dimensions['height'];
}
}
$metadata['image'] = array(
'@type' => 'ImageObject',
'url' => $image['src'],
);
if ( isset( $image['src_width'] ) ) {
$metadata['image']['width'] = $image['src_width'];
}
if ( isset( $image['src_width'] ) ) {
$metadata['image']['height'] = $image['src_height'];
}
return $metadata;
}
/**
* Add fallback image to legacy AMP post metadata.
*
* @since 6.2.0
*
* @param array $metadata Metadata.
* @return array Metadata.
*/
private static function add_fallback_image_to_metadata( $metadata ) {
/** This filter is documented in functions.opengraph.php */
$default_image = apply_filters( 'jetpack_open_graph_image_default', 'https://wordpress.com/i/blank.jpg' );
$metadata['image'] = array(
'@type' => 'ImageObject',
'url' => self::staticize_subdomain( $default_image ),
'width' => 200,
'height' => 200,
);
return $metadata;
}
/**
* Return static WordPress.com domain to use to load resources from WordPress.com.
*
* @param string $domain Asset URL.
*/
private static function staticize_subdomain( $domain ) {
// deal with WPCOM vs Jetpack.
if ( function_exists( 'staticize_subdomain' ) ) {
return staticize_subdomain( $domain );
} else {
return Assets::staticize_subdomain( $domain );
}
}
/**
* Extract image dimensions via wpcom/imagesize, only on WPCOM
*
* @since 6.2.0
*
* @param array $dimensions Dimensions.
* @return array Dimensions.
*/
private static function extract_image_dimensions_from_getimagesize( $dimensions ) {
if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'require_lib' ) ) ) {
return $dimensions;
}
require_lib( 'wpcom/imagesize' );
foreach ( $dimensions as $url => $value ) {
if ( is_array( $value ) ) {
continue;
}
$result = wpcom_getimagesize( $url );
if ( is_array( $result ) ) {
$dimensions[ $url ] = array(
'width' => $result[0],
'height' => $result[1],
);
}
}
return $dimensions;
}
/**
* Display Open Graph Meta tags in AMP views.
*/
public static function amp_post_jetpack_og_tags() {
if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
Jetpack::init()->check_open_graph();
}
if ( function_exists( 'jetpack_og_tags' ) ) {
jetpack_og_tags();
}
}
/**
* Force Freedom mode in VideoPress.
*
* @param array $options Array of VideoPress shortcode options.
*/
public static function videopress_enable_freedom_mode( $options ) {
if ( self::is_amp_request() ) {
$options['freedom'] = true;
}
return $options;
}
/**
* Display custom markup for the sharing buttons when in an AMP view.
*
* @param string $markup Content markup of the Jetpack sharing links.
* @param array $sharing_enabled Array of Sharing Services currently enabled.
*/
public static function render_sharing_html( $markup, $sharing_enabled ) {
global $post;
if ( empty( $post ) ) {
return '';
}
if ( ! self::is_amp_request() ) {
return $markup;
}
remove_action( 'wp_footer', 'sharing_add_footer' );
if ( empty( $sharing_enabled ) ) {
return $markup;
}
$sharing_links = array();
foreach ( $sharing_enabled['visible'] as $service ) {
$sharing_link = $service->get_amp_display( $post );
if ( ! empty( $sharing_link ) ) {
$sharing_links[] = $sharing_link;
}
}
// Replace the existing unordered list with AMP sharing buttons.
$markup = preg_replace( '#
#', implode( '', $sharing_links ), $markup );
// Remove any lingering share-end list items.
$markup = str_replace( '', '', $markup );
return $markup;
}
/**
* Tells Jetpack not to enqueue CSS for share buttons.
*
* @param bool $enqueue Whether or not to enqueue.
* @return bool Whether or not to enqueue.
*/
public static function amp_disable_sharedaddy_css( $enqueue ) {
if ( self::is_amp_request() ) {
$enqueue = false;
}
return $enqueue;
}
/**
* Enqueues the AMP specific sharing styles for the sharing icons.
*/
public static function amp_enqueue_sharing_css() {
if (
Jetpack::is_module_active( 'sharedaddy' )
&& self::is_amp_request()
&& ! self::is_amp_legacy()
) {
wp_enqueue_style( 'sharedaddy-amp', plugin_dir_url( __DIR__ ) . 'modules/sharedaddy/amp-sharing.css', array( 'social-logos' ), JETPACK__VERSION );
}
}
/**
* For the AMP Reader mode template, include styles that we need.
*/
public static function amp_reader_sharing_css() {
// If sharing is not enabled, we should not proceed to render the CSS.
if ( ! defined( 'JETPACK_SOCIAL_LOGOS_DIR' ) || ! defined( 'JETPACK_SOCIAL_LOGOS_URL' ) || ! defined( 'WP_SHARING_PLUGIN_DIR' ) ) {
return;
}
/*
* We'll need to output the full contents of the 2 files
* in the head on AMP views. We can't rely on regular enqueues here.
* @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.
*
* phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
* phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
*/
$css = file_get_contents( JETPACK_SOCIAL_LOGOS_DIR . 'social-logos.css' );
$css = preg_replace( '#(?<=url\(")(?=social-logos\.)#', JETPACK_SOCIAL_LOGOS_URL, $css ); // Make sure font files get their absolute paths.
echo $css;
echo file_get_contents( WP_SHARING_PLUGIN_DIR . 'amp-sharing.css' );
/*
* phpcs:enable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
* phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
*/
}
/**
* Ensure proper Photon image dimensions for AMP Stories.
*
* @param array $args Array of Photon Arguments.
* @param array $details {
* Array of image details.
*
* @type string $tag Image tag (Image HTML output).
* @type string $src Image URL.
* @type string $src_orig Original Image URL.
* @type int|false $width Image width.
* @type int|false $height Image height.
* @type int|false $width_orig Original image width before constrained by content_width.
* @type int|false $height_orig Original Image height before constrained by content_width.
* @type string $transform_orig Original transform before constrained by content_width.
* }
* @return array Args.
*/
public static function filter_photon_post_image_args_for_stories( $args, $details ) {
if ( ! is_singular( 'amp_story' ) ) {
return $args;
}
// Percentage-based dimensions are not allowed in AMP, so this shouldn't happen, but short-circuit just in case.
if ( str_contains( $details['width_orig'], '%' ) || str_contains( $details['height_orig'], '%' ) ) {
return $args;
}
$max_height = 1280; // See image size with the slug \AMP_Story_Post_Type::MAX_IMAGE_SIZE_SLUG.
$transform = $details['transform_orig'];
$width = $details['width_orig'];
$height = $details['height_orig'];
// If height is available, constrain to $max_height.
if ( false !== $height ) {
if ( $height > $max_height && false !== $height ) {
$width = ( $max_height * $width ) / $height;
$height = $max_height;
} elseif ( $height > $max_height ) {
$height = $max_height;
}
}
/*
* Set a height if none is found.
* If height is set in this manner and height is available, use `fit` instead of `resize` to prevent skewing.
*/
if ( false === $height ) {
$height = $max_height;
if ( false !== $width ) {
$transform = 'fit';
}
}
// Build array of Photon args and expose to filter before passing to Photon URL function.
$args = array();
if ( false !== $width && false !== $height ) {
$args[ $transform ] = $width . ',' . $height;
} elseif ( false !== $width ) {
$args['w'] = $width;
} elseif ( false !== $height ) {
$args['h'] = $height;
}
return $args;
}
/**
* Adds amp-options to the list of options to sync, if AMP is available
*
* @param array $options_safelist Safelist of options to sync.
*
* @return array Updated options safelist
*/
public static function filter_jetpack_options_safelist( $options_safelist ) {
if ( function_exists( 'is_amp_endpoint' ) ) {
$options_safelist[] = 'amp-options';
}
return $options_safelist;
}
}