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 first-party support for Crowdsignal poll/survey oEmbeds #1929

Merged
merged 3 commits into from
May 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bin/create-embed-test-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ function amp_get_test_data_entries() {
'content' => 'http://i1259.photobucket.com/albums/ii543/iamnotpeterpan/EditPostlsaquoDennisDoesCricketmdashWordPress_zpsf72cc13d.png',
),
array(
'heading' => 'Polldaddy Poll Embed',
'content' => 'https://polldaddy.com/poll/7012505/',
'heading' => 'Crowdsingal Poll Embed',
'content' => 'https://poll.fm/7012505',
),
array(
'heading' => 'Polldaddy Survey Embed',
'content' => 'https://rydk.polldaddy.com/s/test-survey',
'content' => 'https://rydk.survey.fm/test-survey',
),
array(
'heading' => 'Reddit Embed',
Expand Down
2 changes: 1 addition & 1 deletion includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ function amp_get_content_embed_handlers( $post = null ) {
'AMP_Core_Block_Handler' => array(),
'AMP_Twitter_Embed_Handler' => array(),
'AMP_YouTube_Embed_Handler' => array(),
'AMP_Crowdsignal_Embed_Handler' => array(),
'AMP_DailyMotion_Embed_Handler' => array(),
'AMP_Vimeo_Embed_Handler' => array(),
'AMP_SoundCloud_Embed_Handler' => array(),
Expand All @@ -756,7 +757,6 @@ function amp_get_content_embed_handlers( $post = null ) {
'AMP_Gfycat_Embed_Handler' => array(),
'AMP_Hulu_Embed_Handler' => array(),
'AMP_Imgur_Embed_Handler' => array(),
'WPCOM_AMP_Polldaddy_Embed' => array(),
),
$post
);
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 @@ -42,6 +42,7 @@ class AMP_Autoloader {
'AMP_Admin_Pointers' => 'includes/admin/class-amp-admin-pointers',
'AMP_Post_Type_Support' => 'includes/class-amp-post-type-support',
'AMP_Base_Embed_Handler' => 'includes/embeds/class-amp-base-embed-handler',
'AMP_Crowdsignal_Embed_Handler' => 'includes/embeds/class-amp-crowdsignal-embed-handler',
'AMP_DailyMotion_Embed_Handler' => 'includes/embeds/class-amp-dailymotion-embed',
'AMP_Facebook_Embed_Handler' => 'includes/embeds/class-amp-facebook-embed',
'AMP_Gallery_Embed_Handler' => 'includes/embeds/class-amp-gallery-embed',
Expand Down Expand Up @@ -106,7 +107,6 @@ class AMP_Autoloader {
'AMP_Widget_Archives' => 'includes/widgets/class-amp-widget-archives',
'AMP_Widget_Categories' => 'includes/widgets/class-amp-widget-categories',
'AMP_Widget_Text' => 'includes/widgets/class-amp-widget-text',
'WPCOM_AMP_Polldaddy_Embed' => 'wpcom/class-amp-polldaddy-embed',
'AMP_Test_Stub_Sanitizer' => 'tests/stubs',
'AMP_Test_World_Sanitizer' => 'tests/stubs',
);
Expand Down
66 changes: 66 additions & 0 deletions includes/embeds/class-amp-crowdsignal-embed-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Class AMP_Crowdsignal_Embed_Handler
*
* Handle making polls and surveys from Crowdsignal (formerly Polldaddy) AMP-compatible.
*
* @package AMP
* @since 1.2
*/

/**
* Class AMP_Crowdsignal_Embed_Handler
*/
class AMP_Crowdsignal_Embed_Handler extends AMP_Base_Embed_Handler {

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

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

/**
* Filter oEmbed HTML for Crowdsignal 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 ( empty( $parsed_url['host'] ) || empty( $parsed_url['path'] ) || ! preg_match( '#(^|\.)(?P<host>polldaddy\.com|crowdsignal\.com|survey\.fm|poll\.fm)#', $parsed_url['host'], $matches ) ) {
return $cache;
}

$parsed_url['host'] = $matches['host'];

$output = '';

// Poll oEmbed responses include noscript which can be used as the AMP response.
if ( preg_match( '#<noscript>(.+?)</noscript>#s', $cache, $matches ) ) {
$output = $matches[1];
}

if ( empty( $output ) ) {
if ( ! empty( $attr['title'] ) ) {
$name = $attr['title'];
} elseif ( 'survey.fm' === $parsed_url['host'] || preg_match( '#^/s/#', $parsed_url['path'] ) ) {
$name = __( 'View Survey', 'amp' );
} else {
$name = __( 'View Poll', 'amp' );
}
$output = sprintf( '<a href="%s" target="_blank">%s</a>', esc_url( $url ), esc_html( $name ) );
}

return $output;
}
}
1 change: 0 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<exclude-pattern>includes/embeds/class-amp-vimeo-embed.php</exclude-pattern>
<exclude-pattern>includes/embeds/class-amp-vine-embed.php</exclude-pattern>
<exclude-pattern>includes/embeds/class-amp-youtube-embed.php</exclude-pattern>
<exclude-pattern>wpcom/class-amp-polldaddy-embed.php</exclude-pattern>
</rule>
<rule ref="WordPress.Arrays.MultipleStatementAlignment.LongIndexSpaceBeforeDoubleArrow">
<exclude-pattern>tests/test-tag-and-attribute-sanitizer.php</exclude-pattern>
Expand Down
108 changes: 108 additions & 0 deletions tests/test-amp-crowdsignal-embed-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Class AMP_Crowdsignal_Embed_Test
*
* @package AMP
*/

/**
* Class AMP_Crowdsignal_Embed_Test
*
* @covers AMP_Crowdsignal_Embed_Handler
*/
class AMP_Crowdsignal_Embed_Test extends WP_UnitTestCase {

/**
* Get conversion data.
*
* @return array
*/
public function get_conversion_data() {
$poll_response = array(
'type' => 'rich',
'version' => '1.0',
'provider_name' => 'Crowdsignal',
'provider_url' => 'https://crowdsignal.com',
'title' => 'Which design do you prefer?',
'html' => '<script type="text/javascript" charset="utf-8" src="https://secure.polldaddy.com/p/7012505.js"></script><noscript><a href="https://poll.fm/7012505">Which design do you prefer?</a></noscript>', // phpcs:ignore
);
$survey_response = array(
'type' => 'rich',
'version' => '1.0',
'provider_name' => 'Crowdsignal',
'provider_url' => 'https://crowdsignal.com',
'html' => '<div class="pd-embed" data-settings="{&quot;type&quot;:&quot;iframe&quot;,&quot;auto&quot;:true,&quot;domain&quot;:&quot;rydk.survey.fm&quot;,&quot;id&quot;:&quot;test-survey&quot;}"></div><script type="text/javascript">(function(d,c,j){if(!document.getElementById(j)){var pd=d.createElement(c),s;pd.id=j;pd.src=(\'https:\'==document.location.protocol)?\'https://polldaddy.com/survey.js\':\'http://i0.poll.fm/survey.js\';s=document.getElementsByTagName(c)[0];s.parentNode.insertBefore(pd,s);}}(document,\'script\',\'pd-embed\'));</script>',
);

$data = array(
'poll.fm' => array(
'https://poll.fm/7012505',
'<p><a href="https://poll.fm/7012505">Which design do you prefer?</a></p>',
$poll_response,
),

'polldaddy_poll' => array(
'https://polldaddy.com/poll/7012505/',
'<p><a href="https://poll.fm/7012505">Which design do you prefer?</a></p>',
$poll_response,
),

'polldaddy_survey' => array(
'https://rydk.polldaddy.com/s/test-survey',
'<p><a href="https://rydk.polldaddy.com/s/test-survey" target="_blank">View Survey</a></p>',
$survey_response,
),
);

/*
* There is a bug with WordPress's oEmbed handling for Crowdsignal surveys.
* See <https://core.trac.wordpress.org/ticket/46467>.
*/
if ( version_compare( get_bloginfo( 'version' ), '5.2.0', '>=' ) ) {
$data['survey.fm'] = array(
'https://rydk.survey.fm/test-survey',
'<p><a href="https://rydk.survey.fm/test-survey" target="_blank">View Survey</a></p>',
$survey_response,
);
}

return $data;
}

/**
* Test conversion.
*
* @dataProvider get_conversion_data
*
* @param string $url Source.
* @param string $expected Expected.
* @param string $oembed_response oEmbed response.
*/
public function test_conversion( $url, $expected, $oembed_response ) {
add_filter(
'pre_http_request',
function ( $pre, $r, $request_url ) use ( $url, $oembed_response ) {
unset( $r );
if ( false === strpos( $request_url, 'crowdsignal' ) ) {
return $pre;
}

return array(
'body' => wp_json_encode( $oembed_response ),
'response' => array(
'code' => 200,
'message' => 'OK',
),
);
},
10,
3
);

$embed = new AMP_Crowdsignal_Embed_Handler();
$embed->register_embed();
$filtered_content = apply_filters( 'the_content', $url );

$this->assertEquals( trim( $expected ), trim( $filtered_content ) );
}
}
105 changes: 0 additions & 105 deletions wpcom/class-amp-polldaddy-embed.php

This file was deleted.