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

Fix various PHPCS issues #1002

Merged
merged 19 commits into from
Mar 17, 2018
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public static function add_hooks() {
* Disable admin bar because admin-bar.css (28K) and Dashicons (48K) alone
* combine to surpass the 50K limit imposed for the amp-custom style.
*/
add_filter( 'show_admin_bar', '__return_false', 100 );
add_filter( 'show_admin_bar', '__return_false', 100 ); // phpcs:ignore
Copy link
Member

Choose a reason for hiding this comment

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

This actually isn't necessary. The PHPCS ruleset does not include the AdminBarRemoval WordPress.com VIP sniff. You should make sure to use the project's own ruleset when developing.


/*
* Start output buffering at very low priority for sake of plugins and themes that use template_redirect
Expand Down
42 changes: 38 additions & 4 deletions includes/embeds/class-amp-base-embed-handler.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading