initial
This commit is contained in:
+1236
File diff suppressed because it is too large
Load Diff
+16
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
/**
|
||||
* Package description here
|
||||
*
|
||||
* @package automattic/jetpack-post-media
|
||||
*/
|
||||
|
||||
namespace Automattic\Jetpack;
|
||||
|
||||
/**
|
||||
* Class description.
|
||||
*/
|
||||
class Post_Media {
|
||||
|
||||
const PACKAGE_VERSION = '0.1.6';
|
||||
}
|
||||
+254
@@ -0,0 +1,254 @@
|
||||
<?php
|
||||
/**
|
||||
* Shortcode ID extraction methods.
|
||||
*
|
||||
* Provides static methods to extract identifiers from shortcode attributes
|
||||
* for various media types (YouTube, Vimeo, TED, VideoPress, etc.).
|
||||
*
|
||||
* @package automattic/jetpack-post-media
|
||||
*/
|
||||
|
||||
namespace Automattic\Jetpack;
|
||||
|
||||
/**
|
||||
* Extracts media identifiers from shortcode attributes.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class Shortcodes {
|
||||
|
||||
/**
|
||||
* Get an ID from a shortcode attribute by key.
|
||||
*
|
||||
* Used for shortcodes that store their identifier in a single attribute,
|
||||
* e.g. ted and hulu use a named 'id' attribute, while wpvideo and
|
||||
* videopress use the first positional attribute.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param array $atts Shortcode attributes.
|
||||
* @param string|int $key The attribute key to look up. Defaults to 0 (first positional attribute).
|
||||
* @return string|int The attribute value, or 0 if not found.
|
||||
*/
|
||||
public static function get_attribute_id( $atts, $key = 0 ) {
|
||||
return ! empty( $atts[ $key ] ) ? $atts[ $key ] : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands.
|
||||
*
|
||||
* Handles youtu.be short links, /v/ paths, /shorts/ paths, playlist URLs, and encoded ampersands.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string|array $url YouTube URL, or an array with a 'url' key.
|
||||
* @return string|false The normalized URL, or false if input is invalid.
|
||||
*/
|
||||
public static function sanitize_youtube_url( $url ) {
|
||||
if ( is_array( $url ) && isset( $url['url'] ) ) {
|
||||
$url = $url['url'];
|
||||
}
|
||||
if ( ! is_string( $url ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$url = trim( $url, ' "' );
|
||||
$url = trim( $url );
|
||||
$url = str_replace(
|
||||
array( 'youtu.be/', '/v/', '/shorts/', '#!v=', '&', '&', 'playlist' ),
|
||||
array( 'youtu.be/?v=', '/?v=', '/watch?v=', '?v=', '&', '&', 'videoseries' ),
|
||||
$url
|
||||
);
|
||||
|
||||
// Replace any extra question marks with ampersands - the result of a URL like
|
||||
// "https://www.youtube.com/v/dQw4w9WgXcQ?fs=1&hl=en_US" being passed in.
|
||||
$query_string_start = strpos( $url, '?' );
|
||||
|
||||
if ( false !== $query_string_start ) {
|
||||
$url = substr( $url, 0, $query_string_start + 1 ) . str_replace( '?', '&', substr( $url, $query_string_start + 1 ) );
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a YouTube video or playlist ID from shortcode attributes.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string|array $atts Shortcode attributes. Can be just the URL string or the full $atts array.
|
||||
* @return string|false The YouTube video or playlist ID, or false on failure.
|
||||
*/
|
||||
public static function get_youtube_id( $atts ) {
|
||||
// Do we have an $atts array? Get first att.
|
||||
if ( is_array( $atts ) ) {
|
||||
$atts = reset( $atts );
|
||||
}
|
||||
|
||||
$url = self::sanitize_youtube_url( $atts );
|
||||
|
||||
if ( ! is_string( $url ) || '' === $url ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$parsed = parse_url( $url ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
|
||||
|
||||
if ( ! is_array( $parsed ) || ! isset( $parsed['query'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
parse_str( $parsed['query'], $qargs );
|
||||
|
||||
if ( ! isset( $qargs['v'] ) && ! isset( $qargs['list'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$id = '';
|
||||
|
||||
if ( isset( $qargs['list'] ) ) {
|
||||
$id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['list'] );
|
||||
}
|
||||
|
||||
if ( empty( $id ) && isset( $qargs['v'] ) ) {
|
||||
$id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['v'] );
|
||||
}
|
||||
|
||||
return empty( $id ) ? false : $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a Vimeo video ID from shortcode attributes.
|
||||
*
|
||||
* Supports numeric IDs and various Vimeo URL formats including
|
||||
* groups, albums, channels, and player URLs.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param array $atts Shortcode attributes.
|
||||
* @return int The Vimeo video ID, or 0 if not found.
|
||||
*/
|
||||
public static function get_vimeo_id( $atts ) {
|
||||
if ( isset( $atts[0] ) ) {
|
||||
$atts[0] = trim( $atts[0], '=' );
|
||||
if ( is_numeric( $atts[0] ) ) {
|
||||
return (int) $atts[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract Vimeo ID from the URL. For examples:
|
||||
* https://vimeo.com/12345
|
||||
* https://vimeo.com/289091934/cd1f466bcc
|
||||
* https://vimeo.com/album/2838732/video/6342264
|
||||
* https://vimeo.com/groups/758728/videos/897094040
|
||||
* https://vimeo.com/channels/staffpicks/videos/123456789
|
||||
* https://vimeo.com/album/1234567/video/7654321
|
||||
* https://player.vimeo.com/video/18427511
|
||||
*/
|
||||
$pattern = '/(?:https?:\/\/)?vimeo\.com\/(?:groups\/[^\/]+\/videos\/|album\/\d+\/video\/|video\/|channels\/[^\/]+\/videos\/|[^\/]+\/)?([0-9]+)(?:[^\'\"0-9<]|$)/i';
|
||||
$match = array();
|
||||
if ( preg_match( $pattern, $atts[0], $match ) ) {
|
||||
return (int) $match[1];
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the unique ID of a TED video from shortcode attributes.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param array $atts Shortcode attributes.
|
||||
* @return string|int The TED video ID, or 0 if not found.
|
||||
*/
|
||||
public static function get_ted_id( $atts ) {
|
||||
return self::get_attribute_id( $atts, 'id' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a VideoPress ID from wpvideo shortcode attributes.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param array $atts Shortcode attributes.
|
||||
* @return string|int The VideoPress ID, or 0 if not found.
|
||||
*/
|
||||
public static function get_wpvideo_id( $atts ) {
|
||||
return self::get_attribute_id( $atts );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a VideoPress ID from videopress shortcode attributes.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param array $atts Shortcode attributes.
|
||||
* @return string|int The VideoPress ID, or 0 if not found.
|
||||
*/
|
||||
public static function get_videopress_id( $atts ) {
|
||||
return self::get_attribute_id( $atts );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a Hulu video ID from shortcode attributes.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param array $atts Shortcode attributes.
|
||||
* @return string|int The Hulu video ID, or 0 if not found.
|
||||
*/
|
||||
public static function get_hulu_id( $atts ) {
|
||||
return self::get_attribute_id( $atts, 'id' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an Archive.org embed ID from shortcode attributes.
|
||||
*
|
||||
* Extracts the identifier from an Archive.org details or embed URL,
|
||||
* or uses the raw attribute value if it is not a URL.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param array $atts Shortcode attributes.
|
||||
* @return string|int The Archive.org identifier, the raw attribute value
|
||||
* (which may be empty), or 0 if the attribute is not set.
|
||||
*/
|
||||
public static function get_archiveorg_id( $atts ) {
|
||||
if ( isset( $atts[0] ) ) {
|
||||
$atts[0] = trim( $atts[0], '=' );
|
||||
if ( preg_match( '~archive\.org/(details|embed)/([^/?#]+)~i', $atts[0], $match ) ) {
|
||||
$id = $match[2];
|
||||
} else {
|
||||
$id = $atts[0];
|
||||
}
|
||||
return $id;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an Archive.org book embed ID from shortcode attributes.
|
||||
*
|
||||
* Extracts the identifier from an Archive.org stream URL,
|
||||
* or uses the raw attribute value if it is not a URL.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param array $atts Shortcode attributes.
|
||||
* @return string|int The Archive.org book identifier, the raw attribute value
|
||||
* (which may be empty), or 0 if the attribute is not set.
|
||||
*/
|
||||
public static function get_archiveorg_book_id( $atts ) {
|
||||
if ( isset( $atts[0] ) ) {
|
||||
$atts[0] = trim( $atts[0], '=' );
|
||||
if ( preg_match( '~archive\.org/stream/([^/?#]+)~i', $atts[0], $match ) ) {
|
||||
$id = $match[1];
|
||||
} else {
|
||||
$id = $atts[0];
|
||||
}
|
||||
return $id;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
+354
@@ -0,0 +1,354 @@
|
||||
<?php
|
||||
/**
|
||||
* Twitter Cards handling.
|
||||
*
|
||||
* @package automattic/jetpack-post-media
|
||||
*/
|
||||
|
||||
namespace Automattic\Jetpack\Post_Media;
|
||||
|
||||
use WP_Post;
|
||||
|
||||
/**
|
||||
* Twitter Cards
|
||||
*
|
||||
* Hooks onto the Open Graph protocol and extends it by adding only the tags
|
||||
* we need for twitter cards.
|
||||
*
|
||||
* @see https://dev.twitter.com/cards/overview
|
||||
*/
|
||||
class Twitter_Cards {
|
||||
|
||||
/**
|
||||
* Adds Twitter Card tags.
|
||||
*
|
||||
* @param array $og_tags Existing OG tags.
|
||||
*
|
||||
* @return array OG tags inclusive of Twitter Card output.
|
||||
*/
|
||||
public static function twitter_cards_tags( $og_tags ) {
|
||||
global $post;
|
||||
$post_id = ( $post instanceof WP_Post ) ? $post->ID : null;
|
||||
|
||||
/**
|
||||
* Maximum alt text length.
|
||||
*
|
||||
* @see https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/summary-card-with-large-image.html
|
||||
*/
|
||||
$alt_length = 420;
|
||||
|
||||
if ( post_password_required() ) {
|
||||
return $og_tags;
|
||||
}
|
||||
|
||||
/** This action is documented in class.jetpack.php */
|
||||
if ( apply_filters( 'jetpack_disable_twitter_cards', false ) ) {
|
||||
return $og_tags;
|
||||
}
|
||||
|
||||
/*
|
||||
* These tags apply to any page (home, archives, etc).
|
||||
*/
|
||||
|
||||
// If we have information on the author/creator, then include that as well.
|
||||
if ( ! empty( $post ) && ! empty( $post->post_author ) ) {
|
||||
/** This action is documented in modules/sharedaddy/sharing-sources.php */
|
||||
$handle = apply_filters( 'jetpack_sharing_twitter_via', '', $post_id );
|
||||
if ( ! empty( $handle ) && ! self::is_default_site_tag( $handle ) ) {
|
||||
$og_tags['twitter:creator'] = self::sanitize_twitter_user( $handle );
|
||||
}
|
||||
}
|
||||
|
||||
$site_tag = self::site_tag();
|
||||
/** This action is documented in modules/sharedaddy/sharing-sources.php */
|
||||
$site_tag = apply_filters( 'jetpack_sharing_twitter_via', $site_tag, ( is_singular() ? $post_id : null ) );
|
||||
/** This action is documented in modules/sharedaddy/sharing-sources.php */
|
||||
$site_tag = apply_filters( 'jetpack_twitter_cards_site_tag', $site_tag, $og_tags );
|
||||
if ( ! empty( $site_tag ) ) {
|
||||
$og_tags['twitter:site'] = self::sanitize_twitter_user( $site_tag );
|
||||
}
|
||||
|
||||
if ( ! is_singular() || ! empty( $og_tags['twitter:card'] ) ) {
|
||||
/**
|
||||
* Filter the default Twitter card image, used when no image can be found in a post.
|
||||
*
|
||||
* @module sharedaddy
|
||||
*
|
||||
* @since jetpack-5.9.0
|
||||
*
|
||||
* @param string $str Default image URL.
|
||||
*/
|
||||
$image = apply_filters( 'jetpack_twitter_cards_image_default', '' );
|
||||
if ( ! empty( $image ) ) {
|
||||
$og_tags['twitter:image'] = $image;
|
||||
}
|
||||
|
||||
return $og_tags;
|
||||
}
|
||||
|
||||
$the_title = get_the_title();
|
||||
if ( ! $the_title ) {
|
||||
$the_title = get_bloginfo( 'name' );
|
||||
}
|
||||
$og_tags['twitter:text:title'] = $the_title;
|
||||
|
||||
/*
|
||||
* The following tags only apply to single pages.
|
||||
*/
|
||||
|
||||
$card_type = 'summary';
|
||||
|
||||
// Try to give priority to featured images.
|
||||
if ( ! empty( $post_id ) ) {
|
||||
$post_image = Images::get_image(
|
||||
$post_id,
|
||||
array(
|
||||
'width' => 144,
|
||||
'height' => 144,
|
||||
)
|
||||
);
|
||||
if ( ! empty( $post_image ) && is_array( $post_image ) ) {
|
||||
// 4096 is the maximum size for an image per https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/summary .
|
||||
if (
|
||||
isset( $post_image['src_width'] ) && isset( $post_image['src_height'] )
|
||||
&& (int) $post_image['src_width'] <= 4096
|
||||
&& (int) $post_image['src_height'] <= 4096
|
||||
) {
|
||||
// 300x157 is the minimum size for a summary_large_image per https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/summary-card-with-large-image .
|
||||
if ( (int) $post_image['src_width'] >= 300 && (int) $post_image['src_height'] >= 157 ) {
|
||||
$card_type = 'summary_large_image';
|
||||
$og_tags['twitter:image'] = esc_url( add_query_arg( 'w', 640, $post_image['src'] ) );
|
||||
} else {
|
||||
$og_tags['twitter:image'] = esc_url( add_query_arg( 'w', 144, $post_image['src'] ) );
|
||||
}
|
||||
|
||||
// Add the alt tag if we have one.
|
||||
if ( ! empty( $post_image['alt_text'] ) ) {
|
||||
// Shorten it if it is too long.
|
||||
if ( strlen( $post_image['alt_text'] ) > $alt_length ) {
|
||||
$og_tags['twitter:image:alt'] = esc_attr( mb_substr( $post_image['alt_text'], 0, $alt_length ) . '…' );
|
||||
} else {
|
||||
$og_tags['twitter:image:alt'] = esc_attr( $post_image['alt_text'] );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$extract = array();
|
||||
|
||||
// Only proceed with media analysis if a featured image has not superseded it already.
|
||||
if ( empty( $og_tags['twitter:image'] ) && empty( $og_tags['twitter:image:src'] ) ) {
|
||||
// @todo Jetpack_Media_Summary is defined in the Jetpack plugin. It should be moved to this package.
|
||||
if ( ! class_exists( 'Jetpack_Media_Summary' ) && defined( 'JETPACK__PLUGIN_DIR' ) ) {
|
||||
require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.media-summary.php';
|
||||
}
|
||||
|
||||
// Test again, class should already be auto-loaded in Jetpack.
|
||||
// If not, skip extra media analysis and stick with a summary card.
|
||||
if ( class_exists( 'Jetpack_Media_Summary' ) && ! empty( $post_id ) ) {
|
||||
$extract = \Jetpack_Media_Summary::get( $post_id ); // @phan-suppress-current-line PhanUndeclaredClassMethod -- Guarded by class_exists().
|
||||
|
||||
if ( 'gallery' === $extract['type'] ) {
|
||||
list( $og_tags, $card_type ) = self::twitter_cards_define_type_based_on_image_count( $og_tags, $extract );
|
||||
} elseif ( 'video' === $extract['type'] ) {
|
||||
// Leave as summary, but with large pict of poster frame (we know those comply to Twitter's size requirements).
|
||||
$card_type = 'summary_large_image';
|
||||
$og_tags['twitter:image'] = esc_url( add_query_arg( 'w', 640, $extract['image'] ) );
|
||||
} else {
|
||||
list( $og_tags, $card_type ) = self::twitter_cards_define_type_based_on_image_count( $og_tags, $extract );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$og_tags['twitter:card'] = $card_type;
|
||||
|
||||
// Make sure we have a description for Twitter, their validator isn't happy without some content (single space not valid).
|
||||
if ( ! isset( $og_tags['og:description'] ) || '' === trim( $og_tags['og:description'] ) || __( 'Visit the post for more.', 'jetpack-post-media' ) === $og_tags['og:description'] ) { // empty( trim( $og_tags['og:description'] ) ) isn't valid php.
|
||||
$has_creator = ! empty( $og_tags['twitter:creator'] ) && '@wordpressdotcom' !== $og_tags['twitter:creator'];
|
||||
if ( ! empty( $extract ) && 'video' === $extract['type'] ) { // use $extract['type'] since $card_type is 'summary' for video posts.
|
||||
/* translators: %s is the post author */
|
||||
$og_tags['twitter:description'] = ( $has_creator ) ? sprintf( __( 'Video post by %s.', 'jetpack-post-media' ), $og_tags['twitter:creator'] ?? '' ) : __( 'Video post.', 'jetpack-post-media' );
|
||||
} else {
|
||||
/* translators: %s is the post author */
|
||||
$og_tags['twitter:description'] = ( $has_creator ) ? sprintf( __( 'Post by %s.', 'jetpack-post-media' ), $og_tags['twitter:creator'] ?? '' ) : __( 'Visit the post for more.', 'jetpack-post-media' );
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $og_tags['twitter:image'] ) && empty( $og_tags['twitter:image:src'] ) ) {
|
||||
/** This action is documented in class-twitter-cards.php */
|
||||
$image = apply_filters( 'jetpack_twitter_cards_image_default', '' );
|
||||
if ( ! empty( $image ) ) {
|
||||
$og_tags['twitter:image'] = $image;
|
||||
}
|
||||
}
|
||||
|
||||
return $og_tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize the Twitter user by normalizing the @.
|
||||
*
|
||||
* @param string $str Twitter user value.
|
||||
*
|
||||
* @return string Twitter user value.
|
||||
*/
|
||||
public static function sanitize_twitter_user( $str ) {
|
||||
return '@' . preg_replace( '/^@/', '', $str );
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a site tag is one of the default WP.com/Jetpack ones.
|
||||
*
|
||||
* @param string $site_tag Site tag.
|
||||
*
|
||||
* @return bool True if the default site tag is being used.
|
||||
*/
|
||||
public static function is_default_site_tag( $site_tag ) {
|
||||
return in_array( $site_tag, array( '@wordpressdotcom', '@jetpack', 'wordpressdotcom', 'jetpack' ), true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Give priority to the creator tag if using the default site tag.
|
||||
*
|
||||
* @param string $site_tag Site tag.
|
||||
* @param array $og_tags OG tags.
|
||||
*
|
||||
* @return string Site tag.
|
||||
*/
|
||||
public static function prioritize_creator_over_default_site( $site_tag, $og_tags = array() ) {
|
||||
if ( ! empty( $og_tags['twitter:creator'] ) && self::is_default_site_tag( $site_tag ) ) {
|
||||
return $og_tags['twitter:creator'];
|
||||
}
|
||||
return $site_tag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the Twitter Card type based on image count.
|
||||
*
|
||||
* @param array $og_tags Existing OG tags.
|
||||
* @param array $extract Result of the Image Extractor class.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function twitter_cards_define_type_based_on_image_count( $og_tags, $extract ) {
|
||||
$card_type = 'summary';
|
||||
$img_count = $extract['count']['image'];
|
||||
|
||||
if ( empty( $img_count ) ) {
|
||||
|
||||
// No images, use Blavatar as a thumbnail for the summary type.
|
||||
// @todo blavatar_domain, blavatar_exists, and blavatar_url are WordPress.com functions. They should be abstracted.
|
||||
if ( function_exists( 'blavatar_domain' ) ) {
|
||||
$blavatar_domain = blavatar_domain( site_url() ); // @phan-suppress-current-line PhanUndeclaredFunction -- Guarded by function_exists().
|
||||
if ( blavatar_exists( $blavatar_domain ) ) { // @phan-suppress-current-line PhanUndeclaredFunction -- Guarded by function_exists().
|
||||
$og_tags['twitter:image'] = blavatar_url( $blavatar_domain, 'img', 240 ); // @phan-suppress-current-line PhanUndeclaredFunction -- Guarded by function_exists().
|
||||
}
|
||||
}
|
||||
|
||||
// Second fall back, Site Logo.
|
||||
// @todo jetpack_has_site_logo and jetpack_get_site_logo are defined in the Jetpack plugin. They should be abstracted.
|
||||
if ( empty( $og_tags['twitter:image'] ) && ( function_exists( 'jetpack_has_site_logo' ) && jetpack_has_site_logo() ) ) { // @phan-suppress-current-line PhanUndeclaredFunction -- Guarded by function_exists().
|
||||
$og_tags['twitter:image'] = jetpack_get_site_logo( 'url' ); // @phan-suppress-current-line PhanUndeclaredFunction -- Guarded by function_exists().
|
||||
}
|
||||
|
||||
// Third fall back, Site Icon.
|
||||
if ( empty( $og_tags['twitter:image'] ) && has_site_icon() ) {
|
||||
$og_tags['twitter:image'] = get_site_icon_url( 240 );
|
||||
}
|
||||
|
||||
// Not falling back on Gravatar, because there's no way to know if we end up with an auto-generated one.
|
||||
|
||||
} elseif ( 'image' === $extract['type'] || 'gallery' === $extract['type'] ) {
|
||||
// Test for $extract['type'] to limit to image and gallery, so we don't send a potential fallback image like a Gravatar as a photo post.
|
||||
$card_type = 'summary_large_image';
|
||||
$og_tags['twitter:image'] = esc_url( add_query_arg( 'w', 1400, ( empty( $extract['images'] ) ) ? $extract['image'] : $extract['images'][0]['url'] ) );
|
||||
}
|
||||
|
||||
return array( $og_tags, $card_type );
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the Twitter Card output.
|
||||
*
|
||||
* @param string $og_tag A single OG tag.
|
||||
*
|
||||
* @return string Result of the OG tag.
|
||||
*/
|
||||
public static function twitter_cards_output( $og_tag ) {
|
||||
return ( str_contains( $og_tag, 'twitter:' ) ) ? preg_replace( '/property="([^"]+)"/', 'name="\1"', $og_tag ) : $og_tag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds settings section and field.
|
||||
*/
|
||||
public static function settings_init() {
|
||||
add_settings_section( 'jetpack-twitter-cards-settings', 'Twitter Cards', '__return_false', 'sharing' );
|
||||
add_settings_field(
|
||||
'jetpack-twitter-cards-site-tag',
|
||||
__( 'Twitter Site Tag', 'jetpack-post-media' ),
|
||||
array( __CLASS__, 'settings_field' ),
|
||||
'sharing',
|
||||
'jetpack-twitter-cards-settings',
|
||||
array(
|
||||
'label_for' => 'jetpack-twitter-cards-site-tag',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add global sharing options.
|
||||
*/
|
||||
public static function sharing_global_options() {
|
||||
do_settings_fields( 'sharing', 'jetpack-twitter-cards-settings' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Twitter Via tag.
|
||||
*
|
||||
* @return string Twitter via tag.
|
||||
*/
|
||||
public static function site_tag() {
|
||||
$site_tag = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ?
|
||||
trim( get_option( 'twitter_via' ) ) :
|
||||
( class_exists( 'Jetpack_Options' ) ? \Jetpack_Options::get_option_and_ensure_autoload( 'jetpack-twitter-cards-site-tag', '' ) : get_option( 'jetpack-twitter-cards-site-tag', '' ) );
|
||||
if ( empty( $site_tag ) ) {
|
||||
/** This action is documented in modules/sharedaddy/sharing-sources.php */
|
||||
return apply_filters( 'jetpack_sharing_twitter_via', '', null );
|
||||
}
|
||||
return $site_tag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the settings field.
|
||||
*/
|
||||
public static function settings_field() {
|
||||
wp_nonce_field( 'jetpack-twitter-cards-settings', 'jetpack_twitter_cards_nonce', false );
|
||||
?>
|
||||
<input type="text" id="jetpack-twitter-cards-site-tag" class="regular-text" name="jetpack-twitter-cards-site-tag" value="<?php echo esc_attr( get_option( 'jetpack-twitter-cards-site-tag' ) ); ?>" />
|
||||
<p class="description" style="width: auto;"><?php esc_html_e( 'The Twitter username of the owner of this site\'s domain.', 'jetpack-post-media' ); ?></p>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the settings submission.
|
||||
*/
|
||||
public static function settings_validate() {
|
||||
if ( isset( $_POST['jetpack_twitter_cards_nonce'] ) && wp_verify_nonce( $_POST['jetpack_twitter_cards_nonce'], 'jetpack-twitter-cards-settings' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
||||
update_option( 'jetpack-twitter-cards-site-tag', isset( $_POST['jetpack-twitter-cards-site-tag'] ) ? trim( ltrim( wp_strip_all_tags( filter_var( wp_unslash( $_POST['jetpack-twitter-cards-site-tag'] ) ) ), '@' ) ) : '' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates the class by registering all WordPress hooks.
|
||||
*/
|
||||
public static function init() {
|
||||
add_filter( 'jetpack_open_graph_tags', array( __CLASS__, 'twitter_cards_tags' ), 11 ); // $priority=11: this should hook into jetpack_open_graph_tags after 'class.jetpack-seo.php' has done so.
|
||||
add_filter( 'jetpack_open_graph_output', array( __CLASS__, 'twitter_cards_output' ) );
|
||||
add_filter( 'jetpack_twitter_cards_site_tag', array( __CLASS__, 'site_tag' ), -99 );
|
||||
add_filter( 'jetpack_twitter_cards_site_tag', array( __CLASS__, 'prioritize_creator_over_default_site' ), 99, 2 );
|
||||
add_action( 'admin_init', array( __CLASS__, 'settings_init' ) );
|
||||
add_action( 'sharing_global_options', array( __CLASS__, 'sharing_global_options' ) );
|
||||
add_action( 'sharing_admin_update', array( __CLASS__, 'settings_validate' ) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user