Skip to content

Commit

Permalink
Merge pull request #1002 from paulschreiber/fix/admin-bar
Browse files Browse the repository at this point in the history
Fix various PHPCS issues
  • Loading branch information
westonruter committed Mar 17, 2018
2 parents 66e12d7 + ac33200 commit 79526a4
Show file tree
Hide file tree
Showing 12 changed files with 493 additions and 174 deletions.
42 changes: 38 additions & 4 deletions includes/embeds/class-amp-base-embed-handler.php
Expand Up @@ -11,18 +11,52 @@
* Class AMP_Base_Embed_Handler
*/
abstract class AMP_Base_Embed_Handler {
/**
* Default width.
*
* @var int
*/
protected $DEFAULT_WIDTH = 600;

/**
* Default height.
*
* @var int
*/
protected $DEFAULT_HEIGHT = 480;

/**
* Default arguments.
*
* @var array
*/
protected $args = array();

/**
* Whether or not conversion was completed.
*
* @var boolean
*/
protected $did_convert_elements = false;

abstract function register_embed();
abstract function unregister_embed();
/**
* Register embed.
*/
abstract public function register_embed();

function __construct( $args = array() ) {
/**
* Unregister embed.
*/
abstract public function unregister_embed();

/**
* AMP_Base_Embed_Handler constructor.
*
* @param array $args Height and width for embed.
*/
public function __construct( $args = array() ) {
$this->args = wp_parse_args( $args, array(
'width' => $this->DEFAULT_WIDTH,
'width' => $this->DEFAULT_WIDTH,
'height' => $this->DEFAULT_HEIGHT,
) );
}
Expand Down
102 changes: 82 additions & 20 deletions includes/embeds/class-amp-dailymotion-embed.php
Expand Up @@ -13,31 +13,59 @@
class AMP_DailyMotion_Embed_Handler extends AMP_Base_Embed_Handler {

const URL_PATTERN = '#https?:\/\/(www\.)?dailymotion\.com\/video\/.*#i';
const RATIO = 0.5625;
const RATIO = 0.5625;

/**
* Default width.
*
* @var int
*/
protected $DEFAULT_WIDTH = 600;

/**
* Default height.
*
* @var int
*/
protected $DEFAULT_HEIGHT = 338;

function __construct( $args = array() ) {
/**
* AMP_DailyMotion_Embed_Handler constructor.
*
* @param array $args Height, width and maximum width for embed.
*/
public function __construct( $args = array() ) {
parent::__construct( $args );

if ( isset( $this->args['content_max_width'] ) ) {
$max_width = $this->args['content_max_width'];
$this->args['width'] = $max_width;
$max_width = $this->args['content_max_width'];
$this->args['width'] = $max_width;
$this->args['height'] = round( $max_width * self::RATIO );
}
}

function register_embed() {
/**
* Register embed.
*/
public function register_embed() {
wp_embed_register_handler( 'amp-dailymotion', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
add_shortcode( 'dailymotion', array( $this, 'shortcode' ) );
}

/**
* Unregister embed.
*/
public function unregister_embed() {
wp_embed_unregister_handler( 'amp-dailymotion', -1 );
remove_shortcode( 'dailymotion' );
}

/**
* Gets AMP-compliant markup for the Dailymotion shortcode.
*
* @param array $attr The Dailymotion attributes.
* @return string Dailymotion shortcode markup.
*/
public function shortcode( $attr ) {
$video_id = false;

Expand All @@ -53,25 +81,53 @@ public function shortcode( $attr ) {
return '';
}

return $this->render( array(
'video_id' => $video_id,
) );
return $this->render(
array(
'video_id' => $video_id,
)
);
}

/**
* Render oEmbed.
*
* @see \WP_Embed::shortcode()
*
* @param array $matches URL pattern matches.
* @param array $attr Shortcode attribues.
* @param string $url URL.
* @param string $rawattr Unmodified shortcode attributes.
* @return string Rendered oEmbed.
*/
public function oembed( $matches, $attr, $url, $rawattr ) {
$video_id = $this->get_video_id_from_url( $url );
return $this->render( array(
'video_id' => $video_id,
) );
return $this->render(
array(
'video_id' => $video_id,
)
);
}

/**
* Render.
*
* @param array $args Args.
* @return string Rendered.
*/
public function render( $args ) {
$args = wp_parse_args( $args, array(
'video_id' => false,
) );
$args = wp_parse_args(
$args, array(
'video_id' => false,
)
);

if ( empty( $args['video_id'] ) ) {
return AMP_HTML_Utils::build_tag( 'a', array( 'href' => esc_url( $args['url'] ), 'class' => 'amp-wp-embed-fallback' ), esc_html( $args['url'] ) );
return AMP_HTML_Utils::build_tag(
'a', array(
'href' => esc_url( $args['url'] ),
'class' => 'amp-wp-embed-fallback',
), esc_html( $args['url'] )
);
}

$this->did_convert_elements = true;
Expand All @@ -80,18 +136,24 @@ public function render( $args ) {
'amp-dailymotion',
array(
'data-videoid' => $args['video_id'],
'layout' => 'responsive',
'width' => $this->args['width'],
'height' => $this->args['height'],
'layout' => 'responsive',
'width' => $this->args['width'],
'height' => $this->args['height'],
)
);
}

/**
* Determine the video ID from the URL.
*
* @param string $url URL.
* @return integer Video ID.
*/
private function get_video_id_from_url( $url ) {
$parsed_url = AMP_WP_Utils::parse_url( $url );
parse_str( $parsed_url['path'], $path );
$tok = explode( '/', $parsed_url['path'] );
$tok = explode( '_', $tok[2] );
$tok = explode( '/', $parsed_url['path'] );
$tok = explode( '_', $tok[2] );
$video_id = $tok[0];

return $video_id;
Expand Down
33 changes: 28 additions & 5 deletions includes/embeds/class-amp-twitter-embed.php
Expand Up @@ -13,17 +13,29 @@
class AMP_Twitter_Embed_Handler extends AMP_Base_Embed_Handler {
const URL_PATTERN = '#http(s|):\/\/twitter\.com(\/\#\!\/|\/)([a-zA-Z0-9_]{1,20})\/status(es)*\/(\d+)#i';

/**
* Register embed.
*/
public function register_embed() {
add_shortcode( 'tweet', array( $this, 'shortcode' ) );
wp_embed_register_handler( 'amp-twitter', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
}

/**
* Unregister embed.
*/
public function unregister_embed() {
remove_shortcode( 'tweet' );
wp_embed_unregister_handler( 'amp-twitter', -1 );
}

function shortcode( $attr ) {
/**
* Gets AMP-compliant markup for the Twitter shortcode.
*
* @param array $attr The Twitter attributes.
* @return string Twitter shortcode markup.
*/
public function shortcode( $attr ) {
$attr = wp_parse_args( $attr, array(
'tweet' => false,
) );
Expand Down Expand Up @@ -52,14 +64,25 @@ function shortcode( $attr ) {
'amp-twitter',
array(
'data-tweetid' => $id,
'layout' => 'responsive',
'width' => $this->args['width'],
'height' => $this->args['height'],
'layout' => 'responsive',
'width' => $this->args['width'],
'height' => $this->args['height'],
)
);
}

function oembed( $matches, $attr, $url, $rawattr ) {
/**
* Render oEmbed.
*
* @see \WP_Embed::shortcode()
*
* @param array $matches URL pattern matches.
* @param array $attr Shortcode attribues.
* @param string $url URL.
* @param string $rawattr Unmodified shortcode attributes.
* @return string Rendered oEmbed.
*/
public function oembed( $matches, $attr, $url, $rawattr ) {
$id = false;

if ( isset( $matches[5] ) && is_numeric( $matches[5] ) ) {
Expand Down

0 comments on commit 79526a4

Please sign in to comment.