diff --git a/bin/create-embed-test-post.php b/bin/create-embed-test-post.php index 99cbb7024b5..36ffb96b125 100644 --- a/bin/create-embed-test-post.php +++ b/bin/create-embed-test-post.php @@ -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', diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index 342985070cb..6cff8366e78 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -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(), @@ -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 ); diff --git a/includes/class-amp-autoloader.php b/includes/class-amp-autoloader.php index 98d8d26a2ae..ee2575c5c43 100644 --- a/includes/class-amp-autoloader.php +++ b/includes/class-amp-autoloader.php @@ -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', @@ -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', ); diff --git a/includes/embeds/class-amp-crowdsignal-embed-handler.php b/includes/embeds/class-amp-crowdsignal-embed-handler.php new file mode 100644 index 00000000000..2c07b69c5ad --- /dev/null +++ b/includes/embeds/class-amp-crowdsignal-embed-handler.php @@ -0,0 +1,66 @@ +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( '##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( '%s', esc_url( $url ), esc_html( $name ) ); + } + + return $output; + } +} diff --git a/phpcs.xml b/phpcs.xml index 280b3a64a9a..d67386b8962 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -26,7 +26,6 @@ includes/embeds/class-amp-vimeo-embed.php includes/embeds/class-amp-vine-embed.php includes/embeds/class-amp-youtube-embed.php - wpcom/class-amp-polldaddy-embed.php tests/test-tag-and-attribute-sanitizer.php diff --git a/tests/test-amp-crowdsignal-embed-handler.php b/tests/test-amp-crowdsignal-embed-handler.php new file mode 100644 index 00000000000..b8516e3dc52 --- /dev/null +++ b/tests/test-amp-crowdsignal-embed-handler.php @@ -0,0 +1,108 @@ + 'rich', + 'version' => '1.0', + 'provider_name' => 'Crowdsignal', + 'provider_url' => 'https://crowdsignal.com', + 'title' => 'Which design do you prefer?', + 'html' => '', // phpcs:ignore + ); + $survey_response = array( + 'type' => 'rich', + 'version' => '1.0', + 'provider_name' => 'Crowdsignal', + 'provider_url' => 'https://crowdsignal.com', + 'html' => '
', + ); + + $data = array( + 'poll.fm' => array( + 'https://poll.fm/7012505', + '

Which design do you prefer?

', + $poll_response, + ), + + 'polldaddy_poll' => array( + 'https://polldaddy.com/poll/7012505/', + '

Which design do you prefer?

', + $poll_response, + ), + + 'polldaddy_survey' => array( + 'https://rydk.polldaddy.com/s/test-survey', + '

View Survey

', + $survey_response, + ), + ); + + /* + * There is a bug with WordPress's oEmbed handling for Crowdsignal surveys. + * See . + */ + if ( version_compare( get_bloginfo( 'version' ), '5.2.0', '>=' ) ) { + $data['survey.fm'] = array( + 'https://rydk.survey.fm/test-survey', + '

View Survey

', + $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 ) ); + } +} diff --git a/wpcom/class-amp-polldaddy-embed.php b/wpcom/class-amp-polldaddy-embed.php deleted file mode 100644 index f0696641363..00000000000 --- a/wpcom/class-amp-polldaddy-embed.php +++ /dev/null @@ -1,105 +0,0 @@ -render_link( $url, $attr['title'] ); - } else { - $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 ( false === strpos( $parsed_url['host'], 'polldaddy.com' ) ) { - return $cache; - } - - $output = ''; - - // Poll oEmbed responses include noscript. - if ( preg_match( '##', $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( '

' . esc_html( $title ) . '

' ); - } -}