Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create-embed-test-post script and fix support for various embeds #829

Merged
merged 25 commits into from Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a6fda64
Add skeleton for create-embed-test-post script
westonruter Dec 12, 2017
6ef9768
Fix syntax of PATH_EXCLUDES_PATTERN and add PHPCS exclude-pattern
westonruter Dec 12, 2017
550ed65
Issue 806 : Add media and embeds to test page.
Dec 13, 2017
369696b
Merge branch 'add/806-add-script-for-testing-content' of https://gith…
Dec 13, 2017
7c77752
Issue 806 : Align equals signs vertically.
Dec 13, 2017
128836c
Issue 804 : Test WordPress post embeds.
Dec 13, 2017
b8ba836
Issue 806 : code improvement
ThierryA Dec 13, 2017
0b729b1
Fix rendering of SoundCloud embeds and correct parameters for soundcl…
westonruter Dec 14, 2017
938be72
Fix phpunit tests in WP<4.9 due to oEmbeds formerly requiring post co…
westonruter Dec 14, 2017
45c538c
Fix handling of polldaddy oEmbeds in AMP
westonruter Dec 14, 2017
52b306b
Issue 806 : Add instructions for using the wp-cli test page script.
Dec 14, 2017
13132ed
Merge branch 'add/806-add-script-for-testing-content' of https://gith…
Dec 14, 2017
eaf5157
Issue 804 : Add Amazon, Animoto, and Speakerdeck URLs
Dec 14, 2017
8e34745
Issue 804 : Update the Scribd URL.
Dec 14, 2017
b22e12a
Restore required attachment count in create-embed-test-post; refactor…
westonruter Dec 14, 2017
f1ee3de
Remove DoubleArrowNotAligned PHPCS suppression
westonruter Dec 14, 2017
a91d7ad
Fix order of sanitizers to ensure whitelist applies at end, after ifr…
westonruter Dec 14, 2017
499e580
Issue 806 : Update the Photobucket and Scribd URLs.
Dec 14, 2017
6309f41
Issue 806 : Merge in feature branch, resolve conflicts.
Dec 14, 2017
d2dd032
Issue 806 : Remove Vine embed, add Screencast embed.
Dec 14, 2017
8732936
Issue 806 : Add tests for Someecards.
Dec 14, 2017
5bce94f
Fix phpcs issues in class-amp-rule-spec.php
westonruter Dec 14, 2017
e885b7a
Issue 806 : Correct embeds, including Imgur, Polldaddy, Screencast.
Dec 14, 2017
a485be1
Merge branch 'add/806-add-script-for-testing-content' of https://gith…
Dec 14, 2017
ed6088d
Issue 806 : Update Photobucket, Tumblr, remove Imgur.
Dec 14, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions bin/create-embed-test-post.php
Expand Up @@ -61,7 +61,7 @@
),
array(
'heading' => 'DailyMotion Embed',
'content' => 'http://www.dailymotion.com/embed/video/x6bacgf',
'content' => 'http://www.dailymotion.com/video/x6bacgf',
),
array(
'heading' => 'Facebook Post Embed',
Expand All @@ -81,7 +81,7 @@
),
array(
'heading' => 'Funny Or Die Video Embed',
'content' => 'http://FunnyOrDie.com/m/b1dn',
'content' => 'http://www.funnyordie.com/videos/2977012a20/i-still-haven-t-found-the-droids-i-m-looking-for',
),
array(
'heading' => 'Hulu Embed',
Expand Down Expand Up @@ -116,9 +116,21 @@
'content' => 'http://s1284.photobucket.com/user/adonchin/media/20171116_181841_zpsrjuop6u7.jpg.html',
),
array(
'heading' => 'Polldaddy Embed',
'heading' => 'Polldaddy Poll oEmbed',
'content' => 'https://polldaddy.com/poll/7012505/',
),
array(
'heading' => 'Polldaddy Survey oEmbed',
'content' => 'https://polldaddy.com/s/689f136963604d4f',
),
array(
'heading' => 'Polldaddy Poll Shortcode',
'content' => '[polldaddy poll=7012505 title="Custom poll title"]',
),
array(
'heading' => 'Polldaddy Survey Shortcode',
'content' => '[polldaddy survey=689f136963604d4f title="Custom survey title"]',
),
array(
'heading' => 'Reddit Embed',
'content' => 'https://www.reddit.com/r/Android/comments/7jbkub/google_maps_will_soon_tell_you_when_its_time_to/?ref=share&ref_source=link',
Expand Down
2 changes: 1 addition & 1 deletion includes/class-amp-autoloader.php
Expand Up @@ -80,7 +80,7 @@ class AMP_Autoloader {
'AMP_Image_Dimension_Extractor' => 'includes/utils/class-amp-image-dimension-extractor',
'AMP_String_Utils' => 'includes/utils/class-amp-string-utils',
'AMP_WP_Utils' => 'includes/utils/class-amp-wp-utils',
'WPCOM_AMP_Polldaddy_Embed' => 'includes/wpcom/class-amp-polldaddy-embed',
'WPCOM_AMP_Polldaddy_Embed' => 'wpcom/class-amp-polldaddy-embed',
'AMP_Test_Stub_Sanitizer' => 'tests/stubs',
'AMP_Test_World_Sanitizer' => 'tests/stubs',
);
Expand Down
4 changes: 2 additions & 2 deletions includes/embeds/class-amp-soundcloud-embed.php
Expand Up @@ -82,10 +82,10 @@ public function oembed( $matches, $attr, $url ) {
* Filter oEmbed HTML for SoundCloud to convert to AMP.
*
* @param string $cache Cache for oEmbed.
* @param string $url Embed URL. Optional.
* @param string $url Embed URL.
* @return string Embed.
*/
public function filter_embed_oembed_html( $cache, $url = null ) {
public function filter_embed_oembed_html( $cache, $url ) {
$parsed_url = wp_parse_url( $url );
if ( $url && false === strpos( $parsed_url['host'], 'soundcloud.com' ) ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the risk is minimal, this could throw a PHP warning if the url passed doesn't have an host.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this can ever happen.

return $cache;
Expand Down
1 change: 1 addition & 0 deletions includes/templates/class-amp-post-template.php
Expand Up @@ -362,6 +362,7 @@ private function build_post_content() {
'AMP_Facebook_Embed_Handler' => array(),
'AMP_Pinterest_Embed_Handler' => array(),
'AMP_Gallery_Embed_Handler' => array(),
'WPCOM_AMP_Polldaddy_Embed' => array(),
), $this->post ),
apply_filters( 'amp_content_sanitizers', array(
'AMP_Style_Sanitizer' => array(),
Expand Down
8 changes: 5 additions & 3 deletions wpcom-helper.php
Expand Up @@ -6,10 +6,12 @@
// Disable admin menu
add_filter( 'amp_options_menu_is_enabled', '__return_false', 9999 );

/**
* Disable the_content filters for Jetpack.
*
* @param int $post_id Post ID.
*/
function jetpack_amp_disable_the_content_filters( $post_id ) {
// Shortcode overrides.
require_once( dirname( __FILE__ ) . '/wpcom/shortcodes.php' );

add_filter( 'post_flair_disable', '__return_true', 99 );
add_filter( 'videopress_show_2015_player', '__return_true' );
add_filter( 'protected_embeds_use_form_post', '__return_false' );
Expand Down
86 changes: 80 additions & 6 deletions wpcom/class-amp-polldaddy-embed.php
@@ -1,26 +1,100 @@
<?php
/**
* Class WPCOM_AMP_Polldaddy_Embed
*
* @package AMP
*/

/**
* Class WPCOM_AMP_Polldaddy_Embed
*/
class WPCOM_AMP_Polldaddy_Embed extends AMP_Base_Embed_Handler {

/**
* Register embed.
*/
public function register_embed() {
add_shortcode( 'polldaddy', array( $this, 'shortcode' ) );
add_filter( 'embed_oembed_html', array( $this, 'filter_embed_oembed_html' ), 10, 3 );
}

/**
* Unregister embed.
*/
public function unregister_embed() {
remove_shortcode( 'polldaddy' );
remove_filter( 'embed_oembed_html', array( $this, 'filter_embed_oembed_html' ), 10 );
}

/**
* Shortcode.
*
* @param array $attr Shortcode attributes.
* @return string Shortcode.
*/
public function shortcode( $attr ) {
global $wp_embed;

$output = '';
$url = 'https://polldaddy.com/';
if ( ! empty( $attr['poll'] ) ) {
$url .= 'poll/' . $attr['poll'] . '/';
$name = ! empty( $attr['title'] ) ? $attr['title'] : __( 'View Poll', 'amp' );
} elseif ( ! empty( $attr['survey'] ) ) { // Surveys and Quizzes both use attr survey
} elseif ( ! empty( $attr['survey'] ) ) {
$url .= 's/' . $attr['survey'] . '/';
$name = ! empty( $attr['title'] ) ? $attr['title'] : __( 'View Survey', 'amp' );
} else {
return ''; // We can't embed anything useful for rating
}

return '<p><a href="' . esc_url( $url ) . '">' . esc_html( $name ) . '</a></p>';
if ( ! empty( $attr['title'] ) ) {
$output = $this->render_link( $url, $attr['title'] );
} elseif ( $url ) {
$output = $wp_embed->shortcode( $attr, $url );
}

return $output;
}

/**
* Filter oEmbed HTML for PollDaddy to for AMP output.
*
* @param string $cache Cache for oEmbed.
* @param string $url Embed URL.
* @param array $attr Shortcode attributes.
* @return string Embed.
*/
public function filter_embed_oembed_html( $cache, $url, $attr ) {
$parsed_url = wp_parse_url( $url );
if ( $url && false === strpos( $parsed_url['host'], 'polldaddy.com' ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may need to include looking at poll.fm, but I've never seen any PollDaddy URLs look like that: https://github.com/WordPress/wordpress-develop/blob/c6326694380722ccbdd78e21a2bd0d079383291c/src/wp-includes/class-oembed.php#L69

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the risk is minimal, this could throw a PHP warning if the url passed doesn't have an host.

return $cache;
}

$output = '';

// Poll oEmbed responses include noscript.
if ( preg_match( '#<noscript>(.+?)</noscript>#', $cache, $matches ) ) {
$output = $matches[1];
}

if ( empty( $output ) ) {
if ( ! empty( $attr['title'] ) ) {
$name = $attr['title'];
} elseif ( false !== strpos( $url, 'polldaddy.com/s' ) ) {
$name = __( 'View Survey', 'amp' );
} else {
$name = __( 'View Poll', 'amp' );
}
$output = $this->render_link( $url, $name );
}

return $output;
}

/**
* Render poll/survey link.
*
* @param string $url Link URL.
* @param string $title Link Text.
* @return string Link.
*/
private function render_link( $url, $title ) {
return sprintf( '<p><a href="' . esc_url( $url ) . '">' . esc_html( $title ) . '</a></p>' );
}
}
10 changes: 5 additions & 5 deletions wpcom/shortcodes.php
Expand Up @@ -5,16 +5,16 @@
* @package AMP
*/

add_filter( 'amp_content_embed_handlers', 'wpcom_amp_add_custom_embeds', 10, 2 );
_deprecated_file( __FILE__, '0.6.0' );

/**
* Add custom embeds for WordPress.com.
*
* @param array $embed_handler_classes Embed handler classes.
* @param WP_Post $post Post.
* @deprecated Now PollDaddy is supported in core AMP.
* @param array $embed_handler_classes Embed handler classes.
* @return mixed
*/
function wpcom_amp_add_custom_embeds( $embed_handler_classes, $post ) {
$embed_handler_classes['WPCOM_AMP_Polldaddy_Embed'] = array();
function wpcom_amp_add_custom_embeds( $embed_handler_classes ) {
_deprecated_function( __FUNCTION__, '0.6.0' );
return $embed_handler_classes;
}