-
Notifications
You must be signed in to change notification settings - Fork 385
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
Changes from 1 commit
a6fda64
6ef9768
550ed65
369696b
7c77752
128836c
b8ba836
0b729b1
938be72
45c538c
52b306b
13132ed
eaf5157
8e34745
b22e12a
f1ee3de
a91d7ad
499e580
6309f41
d2dd032
8732936
5bce94f
e885b7a
a485be1
ed6088d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may need to include looking at There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>' ); | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.