Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:ampproject/amp-wp into add/block…
Browse files Browse the repository at this point in the history
…-editor-amp-preview

* 'develop' of github.com:ampproject/amp-wp: (30 commits)
  Fix identifying sources for validation errors coming child themes (#3708)
  Fix failing E2E tests (#3707)
  Remove amp_validate query var from Validated URL 'View' row action
  Re-factor get_html_attribute_pattern as match_element_attributes
  Quote variables added to regex pattern
  Escape instances of unescapeed output in AMP settings screen code (#3703)
  Replace incorrect usage of esc_url() with esc_url_raw()
  Remove empty alt attributes
  Add object-fit=contain to amp-youtube placeholder image
  Prevent copying empty title/alt in WP<5.2
  Extract HTML attribute parsing logic into base class, and use for YouTube embeds
  Update doc comment for `get_video_id_from_url`
  Merge separate regexes used to retrieve props into one
  Replace ambiguous `$fallback` with `$fallback_for_expected`
  Refactor `get_video_id_from_url` function
  Replace the fallback with an image placeholder for the iframe
  Remove todo
  HTML entity encoding will be handled by the DOM instead
  Bump `oembed` function deprecation version to 1.5.0
  Bump develop to v1.5.0-alpha
  ...
  • Loading branch information
westonruter committed Nov 11, 2019
2 parents a2e18e7 + fe2cd4e commit 391b9a0
Show file tree
Hide file tree
Showing 20 changed files with 355 additions and 218 deletions.
4 changes: 2 additions & 2 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Plugin URI: https://amp-wp.org
* Author: AMP Project Contributors
* Author URI: https://github.com/ampproject/amp-wp/graphs/contributors
* Version: 1.4.1-alpha
* Version: 1.5.0-alpha
* Text Domain: amp
* Domain Path: /languages/
* License: GPLv2 or later
Expand All @@ -15,7 +15,7 @@

define( 'AMP__FILE__', __FILE__ );
define( 'AMP__DIR__', dirname( __FILE__ ) );
define( 'AMP__VERSION', '1.4.1-alpha' );
define( 'AMP__VERSION', '1.5.0-alpha' );

/**
* Errors encountered while loading the plugin.
Expand Down
2 changes: 1 addition & 1 deletion includes/class-amp-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class AMP_Autoloader {
'AMP_Vimeo_Embed_Handler' => 'includes/embeds/class-amp-vimeo-embed',
'AMP_Vine_Embed_Handler' => 'includes/embeds/class-amp-vine-embed',
'AMP_WordPress_TV_Embed_Handler' => 'includes/embeds/class-amp-wordpress-tv-embed-handler',
'AMP_YouTube_Embed_Handler' => 'includes/embeds/class-amp-youtube-embed',
'AMP_YouTube_Embed_Handler' => 'includes/embeds/class-amp-youtube-embed-handler',
'AMP_Scribd_Embed_Handler' => 'includes/embeds/class-amp-scribd-embed-handler',
'AMP_Analytics_Options_Submenu' => 'includes/options/class-amp-analytics-options-submenu',
'AMP_Options_Menu' => 'includes/options/class-amp-options-menu',
Expand Down
4 changes: 0 additions & 4 deletions includes/class-amp-post-type-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ public static function get_support_errors( $post ) {
$errors[] = 'post-type-support';
}

if ( post_password_required( $post ) ) {
$errors[] = 'password-protected';
}

/**
* Filters whether to skip the post from AMP.
*
Expand Down
30 changes: 30 additions & 0 deletions includes/embeds/class-amp-base-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,34 @@ public function __construct( $args = [] ) {
public function get_scripts() {
return [];
}

/**
* Get regex pattern for matching HTML attributes from a given tag name.
*
* @since 1.5.0
*
* @param string $html HTML source haystack.
* @param string $tag_name Tag name.
* @param string[] $attribute_names Attribute names.
* @return string[]|null Matched attributes, or null if the element was not matched at all.
*/
protected function match_element_attributes( $html, $tag_name, $attribute_names ) {
$pattern = sprintf(
'/<%s%s/',
preg_quote( $tag_name, '/' ),
implode(
'',
array_map(
function ( $attr_name ) {
return sprintf( '(?=[^>]*?%1$s="(?P<%1$s>[^"]+)")?', preg_quote( $attr_name, '/' ) );
},
$attribute_names
)
)
);
if ( ! preg_match( $pattern, $html, $matches ) ) {
return null;
}
return wp_array_slice_assoc( $matches, $attribute_names );
}
}
2 changes: 1 addition & 1 deletion includes/embeds/class-amp-dailymotion-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function render( $args ) {
return AMP_HTML_Utils::build_tag(
'a',
[
'href' => esc_url( $args['url'] ),
'href' => esc_url_raw( $args['url'] ),
'class' => 'amp-wp-embed-fallback',
],
esc_html( $args['url'] )
Expand Down
2 changes: 1 addition & 1 deletion includes/embeds/class-amp-instagram-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function render( $args ) {
return AMP_HTML_Utils::build_tag(
'a',
[
'href' => esc_url( $args['url'] ),
'href' => esc_url_raw( $args['url'] ),
'class' => 'amp-wp-embed-fallback',
],
esc_html( $args['url'] )
Expand Down
65 changes: 31 additions & 34 deletions includes/embeds/class-amp-soundcloud-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,39 +78,37 @@ public function filter_embed_oembed_html( $cache, $url ) {
* @param string|null $url Embed URL, for fallback purposes.
* @return string AMP component or empty if unable to determine SoundCloud ID.
*/
private function parse_amp_component_from_iframe( $html, $url ) {
$embed = '';

if ( preg_match( '#<iframe[^>]*?src="(?P<src>[^"]+)"#s', $html, $matches ) ) {
$src = html_entity_decode( $matches['src'], ENT_QUOTES );
$query = [];
parse_str( wp_parse_url( $src, PHP_URL_QUERY ), $query );
if ( ! empty( $query['url'] ) ) {
$props = $this->extract_params_from_iframe_src( $query['url'] );
if ( isset( $query['visual'] ) ) {
$props['visual'] = $query['visual'];
}

if ( $url && preg_match( '#<iframe[^>]*?title="(?P<title>[^"]+)"#s', $html, $matches ) ) {
$props['fallback'] = sprintf(
'<a fallback href="%s">%s</a>',
esc_url( $url ),
esc_html( $matches['title'] )
);
}

if ( preg_match( '#<iframe[^>]*?height="(?P<height>\d+)"#s', $html, $matches ) ) {
$props['height'] = (int) $matches['height'];
}

if ( preg_match( '#<iframe[^>]*?width="(?P<width>\d+)"#s', $html, $matches ) ) {
$props['width'] = (int) $matches['width'];
}

$embed = $this->render( $props, $url );
}
}
return $embed;
private function parse_amp_component_from_iframe( $html, $url = null ) {
$props = $this->match_element_attributes( $html, 'iframe', [ 'src', 'title', 'width', 'height' ] );
if ( ! isset( $props ) || empty( $props['src'] ) ) {
return $html;
}

$src = html_entity_decode( $props['src'], ENT_QUOTES );
$query = [];
parse_str( wp_parse_url( $src, PHP_URL_QUERY ), $query );

if ( empty( $query['url'] ) ) {
return $html;
}

$props = array_merge(
$props,
$this->extract_params_from_iframe_src( $query['url'] )
);
if ( isset( $query['visual'] ) ) {
$props['visual'] = $query['visual'];
}

if ( $url && ! empty( $props['title'] ) ) {
$props['fallback'] = sprintf(
'<a fallback href="%s">%s</a>',
esc_url( $url ),
esc_html( $props['title'] )
);
}

return $this->render( $props, $url );
}

/**
Expand Down Expand Up @@ -157,7 +155,6 @@ public function shortcode( $attr, $content = null ) {
* @param array $args Args.
* @param string $url Embed URL for fallback purposes. Optional.
* @return string Rendered embed.
* @global WP_Embed $wp_embed
*/
public function render( $args, $url ) {
$args = wp_parse_args(
Expand Down
2 changes: 1 addition & 1 deletion includes/embeds/class-amp-vimeo-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function render( $args ) {
return AMP_HTML_Utils::build_tag(
'a',
[
'href' => esc_url( $args['url'] ),
'href' => esc_url_raw( $args['url'] ),
'class' => 'amp-wp-embed-fallback',
],
esc_html( $args['url'] )
Expand Down
2 changes: 1 addition & 1 deletion includes/embeds/class-amp-vine-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function render( $args ) {
return AMP_HTML_Utils::build_tag(
'a',
[
'href' => esc_url( $args['url'] ),
'href' => esc_url_raw( $args['url'] ),
'class' => 'amp-wp-embed-fallback',
],
esc_html( $args['url'] )
Expand Down
Loading

0 comments on commit 391b9a0

Please sign in to comment.