[a-zA-Z0-9_]{1,20})\/status(?:es)?\/(?P\d+)#i'; /** * URL pattern for a Twitter timeline. * * @since 1.0 * @var string */ const URL_PATTERN_TIMELINE = '#https?:\/\/twitter\.com(?:\/\#\!\/|\/)(?P[a-zA-Z0-9_]{1,20})(?:$|\/(?Plikes|lists)(\/(?P[a-zA-Z0-9_-]+))?)#i'; /** * Tag. * * @var string embed HTML blockquote tag to identify and replace with AMP version. */ protected $sanitize_tag = 'blockquote'; /** * Tag. * * @var string AMP amp-facebook tag */ private $amp_tag = 'amp-twitter'; /** * Registers embed. */ public function register_embed() { add_shortcode( 'tweet', array( $this, 'shortcode' ) ); // Note: This is a Jetpack shortcode. wp_embed_register_handler( 'amp-twitter', self::URL_PATTERN, array( $this, 'oembed' ), -1 ); wp_embed_register_handler( 'amp-twitter-timeline', self::URL_PATTERN_TIMELINE, array( $this, 'oembed_timeline' ), -1 ); } /** * Unregisters embed. */ public function unregister_embed() { remove_shortcode( 'tweet' ); // Note: This is a Jetpack shortcode. wp_embed_unregister_handler( 'amp-twitter', -1 ); wp_embed_unregister_handler( 'amp-twitter-timeline', -1 ); } /** * Gets AMP-compliant markup for the Twitter shortcode. * * Note that this shortcode is is defined in Jetpack. * * @param array $attr The Twitter attributes. * @return string Twitter shortcode markup. */ public function shortcode( $attr ) { $attr = wp_parse_args( $attr, array( 'tweet' => false, ) ); if ( empty( $attr['tweet'] ) && ! empty( $attr[0] ) ) { $attr['tweet'] = $attr[0]; } $id = false; if ( is_numeric( $attr['tweet'] ) ) { $id = $attr['tweet']; } else { preg_match( self::URL_PATTERN, $attr['tweet'], $matches ); if ( isset( $matches['tweet'] ) && is_numeric( $matches['tweet'] ) ) { $id = $matches['tweet']; } if ( empty( $id ) ) { return ''; } } $this->did_convert_elements = true; return AMP_HTML_Utils::build_tag( $this->amp_tag, array( 'data-tweetid' => $id, 'layout' => 'responsive', 'width' => $this->args['width'], 'height' => $this->args['height'], ) ); } /** * Render oEmbed. * * @see \WP_Embed::shortcode() * * @param array $matches URL pattern matches. * @return string Rendered oEmbed. */ public function oembed( $matches ) { $id = false; if ( isset( $matches['tweet'] ) && is_numeric( $matches['tweet'] ) ) { $id = $matches['tweet']; } if ( ! $id ) { return ''; } return $this->shortcode( array( 'tweet' => $id ) ); } /** * Render oEmbed for a timeline. * * @since 1.0 * * @param array $matches URL pattern matches. * @return string Rendered oEmbed. */ public function oembed_timeline( $matches ) { if ( ! isset( $matches['username'] ) ) { return ''; } $attributes = array( 'data-timeline-source-type' => 'profile', 'data-timeline-screen-name' => $matches['username'], ); if ( isset( $matches['type'] ) ) { switch ( $matches['type'] ) { case 'likes': $attributes['data-timeline-source-type'] = 'likes'; break; case 'lists': if ( ! isset( $matches['id'] ) ) { return ''; } $attributes['data-timeline-source-type'] = 'list'; $attributes['data-timeline-slug'] = $matches['id']; $attributes['data-timeline-owner-screen-name'] = $attributes['data-timeline-screen-name']; unset( $attributes['data-timeline-screen-name'] ); break; default: return ''; } } $attributes['layout'] = 'responsive'; $attributes['width'] = $this->args['width']; $attributes['height'] = $this->args['height']; $this->did_convert_elements = true; return AMP_HTML_Utils::build_tag( $this->amp_tag, $attributes ); } /** * Sanitized