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

AMP: fix PHPCS warnings #12054

Merged
merged 3 commits into from
Apr 17, 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
135 changes: 90 additions & 45 deletions 3rd-party/class.jetpack-amp-support.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* Manages compatibility with the amp-wp plugin
*
* @see https://github.com/Automattic/amp-wp
*/
class Jetpack_AMP_Support {

static function init() {
/**
* Apply custom AMP changes onthe frontend.
*/
public static function init() {

// Add Stats tracking pixel on Jetpack sites when the Stats module is active.
if (
Expand All @@ -20,29 +22,43 @@ static function init() {
// Sharing.
add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 );

// enforce freedom mode for videopress
// enforce freedom mode for videopress.
add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) );

// include Jetpack og tags when rendering native AMP head
// include Jetpack og tags when rendering native AMP head.
add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) );

// Post rendering changes for legacy AMP
// Post rendering changes for legacy AMP.
add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) );

// Add post template metadata for legacy AMP
// Add post template metadata for legacy AMP.
add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 );
}

static function admin_init() {
// disable Likes metabox for post editor if AMP canonical disabled
add_filter( 'post_flair_disable', array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 );
/**
* Apply custom AMP changes in wp-admin.
*/
public static function admin_init() {
// disable Likes metabox for post editor if AMP canonical disabled.
add_filter( 'post_flair_disable', array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 );
}

static function is_amp_canonical() {
/**
* Is the page in AMP 'canonical mode'.
* Used when themes register support for AMP with `add_theme_support( 'amp' )`.
*
* @return bool is_amp_canonical
*/
public static function is_amp_canonical() {
return function_exists( 'amp_is_canonical' ) && amp_is_canonical();
}

static function is_amp_request() {
/**
* Does the page return AMP content.
*
* @return bool $is_amp_request Are we on am AMP view.
*/
public static function is_amp_request() {
$is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() );

/**
Expand All @@ -55,7 +71,10 @@ static function is_amp_request() {
return apply_filters( 'jetpack_is_amp_request', $is_amp_request );
}

static function amp_disable_the_content_filters() {
/**
* Remove content filters added by Jetpack.
*/
public static function amp_disable_the_content_filters() {
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
add_filter( 'videopress_show_2015_player', '__return_true' );
add_filter( 'protected_embeds_use_form_post', '__return_false' );
Expand All @@ -71,7 +90,7 @@ static function amp_disable_the_content_filters() {
*
* @since 6.2.1
*/
static function add_stats_pixel() {
public static function add_stats_pixel() {
if ( ! has_action( 'wp_footer', 'stats_footer' ) ) {
return;
}
Expand All @@ -87,7 +106,7 @@ static function add_stats_pixel() {
* @param WP_Post $post Post.
* @return array Modified metadata array.
*/
static function amp_post_template_metadata( $metadata, $post ) {
public static function amp_post_template_metadata( $metadata, $post ) {
if ( isset( $metadata['publisher'] ) && ! isset( $metadata['publisher']['logo'] ) ) {
$metadata = self::add_site_icon_to_metadata( $metadata );
}
Expand All @@ -105,10 +124,12 @@ static function amp_post_template_metadata( $metadata, $post ) {
* @since 6.2.0
*
* @param array $metadata Metadata.
*
* @return array Metadata.
*/
static function add_site_icon_to_metadata( $metadata ) {
$size = 60;
private static function add_site_icon_to_metadata( $metadata ) {
$size = 60;
$site_icon_url = class_exists( 'Jetpack_Sync_Functions' ) ? Jetpack_Sync_Functions::site_icon_url( $size ) : '';

if ( function_exists( 'blavatar_domain' ) ) {
$metadata['publisher']['logo'] = array(
Expand All @@ -117,7 +138,7 @@ static function add_site_icon_to_metadata( $metadata ) {
'width' => $size,
'height' => $size,
);
} else if ( $site_icon_url = Jetpack_Sync_Functions::site_icon_url( $size ) ) {
} elseif ( $site_icon_url ) {
$metadata['publisher']['logo'] = array(
'@type' => 'ImageObject',
'url' => $site_icon_url,
Expand All @@ -138,23 +159,28 @@ static function add_site_icon_to_metadata( $metadata ) {
* @param WP_Post $post Post.
* @return array Metadata.
*/
static function add_image_to_metadata( $metadata, $post ) {
$image = Jetpack_PostImages::get_image( $post->ID, array(
'fallback_to_avatars' => true,
'avatar_size' => 200,
// AMP already attempts these.
'from_thumbnail' => false,
'from_attachment' => false,
) );
private static function add_image_to_metadata( $metadata, $post ) {
$image = Jetpack_PostImages::get_image(
$post->ID,
array(
'fallback_to_avatars' => true,
'avatar_size' => 200,
// AMP already attempts these.
'from_thumbnail' => false,
'from_attachment' => false,
)
);

if ( empty( $image ) ) {
return self::add_fallback_image_to_metadata( $metadata );
}

if ( ! isset( $image['src_width'] ) ) {
$dimensions = self::extract_image_dimensions_from_getimagesize( array(
$image['src'] => false,
) );
$dimensions = self::extract_image_dimensions_from_getimagesize(
array(
$image['src'] => false,
)
);

if ( false !== $dimensions[ $image['src'] ] ) {
$image['src_width'] = $dimensions['width'];
Expand Down Expand Up @@ -184,7 +210,7 @@ static function add_image_to_metadata( $metadata, $post ) {
* @param array $metadata Metadata.
* @return array Metadata.
*/
static function add_fallback_image_to_metadata( $metadata ) {
private static function add_fallback_image_to_metadata( $metadata ) {
/** This filter is documented in functions.opengraph.php */
$default_image = apply_filters( 'jetpack_open_graph_image_default', 'https://wordpress.com/i/blank.jpg' );

Expand All @@ -198,8 +224,13 @@ static function add_fallback_image_to_metadata( $metadata ) {
return $metadata;
}

static function staticize_subdomain( $domain ) {
// deal with WPCOM vs Jetpack
/**
* Return static WordPress.com domain to use to load resources from WordPress.com.
*
* @param string $domain Asset URL.
*/
private static function staticize_subdomain( $domain ) {
// deal with WPCOM vs Jetpack.
if ( function_exists( 'staticize_subdomain' ) ) {
return staticize_subdomain( $domain );
} else {
Expand All @@ -215,7 +246,7 @@ static function staticize_subdomain( $domain ) {
* @param array $dimensions Dimensions.
* @return array Dimensions.
*/
static function extract_image_dimensions_from_getimagesize( $dimensions ) {
private static function extract_image_dimensions_from_getimagesize( $dimensions ) {
if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'require_lib' ) ) ) {
return $dimensions;
}
Expand All @@ -237,7 +268,10 @@ static function extract_image_dimensions_from_getimagesize( $dimensions ) {
return $dimensions;
}

static function amp_post_jetpack_og_tags() {
/**
* Display Open Graph Meta tags in AMP views.
*/
public static function amp_post_jetpack_og_tags() {
if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
Jetpack::init()->check_open_graph();
}
Expand All @@ -247,14 +281,25 @@ static function amp_post_jetpack_og_tags() {
}
}

static function videopress_enable_freedom_mode( $options ) {
/**
* Force Freedom mode in VideoPress.
*
* @param array $options Array of VideoPress shortcode options.
*/
public static function videopress_enable_freedom_mode( $options ) {
if ( self::is_amp_request() ) {
$options['freedom'] = true;
}
return $options;
}

static function render_sharing_html( $markup, $sharing_enabled ) {
/**
* Display custom markup for the sharing buttons when in an AMP view.
*
* @param string $markup Content markup of the Jetpack sharing links.
* @param array $sharing_enabled Array of Sharing Services currently enabled.
*/
public static function render_sharing_html( $markup, $sharing_enabled ) {
if ( ! self::is_amp_request() ) {
return $markup;
}
Expand All @@ -264,23 +309,23 @@ static function render_sharing_html( $markup, $sharing_enabled ) {
return $markup;
}
$supported_services = array(
'facebook' => array(
'facebook' => array(
/** This filter is documented in modules/sharedaddy/sharing-sources.php */
'data-param-app_id' => apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ),
),
'twitter' => array(),
'pinterest' => array(),
'whatsapp' => array(),
'tumblr' => array(),
'linkedin' => array(),
'twitter' => array(),
'pinterest' => array(),
'whatsapp' => array(),
'tumblr' => array(),
'linkedin' => array(),
);
$sharing_links = array();
$sharing_links = array();
foreach ( $sharing_enabled['visible'] as $id => $service ) {
if ( ! isset( $supported_services[ $id ] ) ) {
$sharing_links[] = "<!-- not supported: $id -->";
continue;
}
$args = array_merge(
$args = array_merge(
array(
'type' => $id,
),
Expand All @@ -290,7 +335,7 @@ static function render_sharing_html( $markup, $sharing_enabled ) {
foreach ( $args as $key => $value ) {
$sharing_link .= sprintf( ' %s="%s"', sanitize_key( $key ), esc_attr( $value ) );
}
$sharing_link .= '></amp-social-share>';
$sharing_link .= '></amp-social-share>';
$sharing_links[] = $sharing_link;
}
return preg_replace( '#(?<=<div class="sd-content">).+?(?=</div>)#s', implode( '', $sharing_links ), $markup );
Expand Down
1 change: 1 addition & 0 deletions bin/phpcs-whitelist.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// If the file path starts with anything like in the array below, it should be linted.
module.exports = [
'3rd-party/3rd-party.php',
'3rd-party/class.jetpack-amp-support.php',
'class.jetpack-gutenberg.php',
'class.jetpack-plan.php',
'extensions/',
Expand Down