Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/Automattic/amp-wp into a…
Browse files Browse the repository at this point in the history
…dd/css-tree-shaking
  • Loading branch information
westonruter committed Apr 5, 2018
2 parents 14c22c2 + 4994830 commit b278bc2
Show file tree
Hide file tree
Showing 24 changed files with 2,204 additions and 590 deletions.
42 changes: 11 additions & 31 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,10 @@ language: php
# Opt to use Travis container-based environment.
sudo: false

# PHP version used in first build configuration.
php:
- "7.1"

# WordPress version used in first build configuration.
env:
- WP_VERSION=latest

# Newer versions like trusty don't have PHP 5.2 or 5.3
# https://blog.travis-ci.com/2017-07-11-trusty-as-default-linux-is-coming
dist: precise

# Next we define our matrix of additional build configurations to test against.
# The versions listed above will automatically create our first configuration,
# so it doesn't need to be re-defined below.

# WP_VERSION specifies the tag to use. The way these tests are configured to run
# requires at least WordPress 3.8. Specify "latest" to test against SVN trunk.

# Note that Travis CI supports listing these above to automatically build a
# matrix of configurations, but we're being nice here by manually building a
# total of four configurations even though we're testing 4 versions of PHP
# along with 2 versions of WordPress (which would build 8 configs otherwise).
# This takes half as long to run while still providing adequate coverage.

notifications:
email:
on_success: never
Expand All @@ -44,19 +23,20 @@ matrix:
include:
- php: "5.3"
env: WP_VERSION=latest DEV_LIB_SKIP=composer,phpcs
- php: "5.3"
env: WP_VERSION=4.7 DEV_LIB_SKIP=composer,phpcs
- php: "5.4"
env: WP_VERSION=latest
- php: "5.4"
env: WP_VERSION=4.7
- php: "7.0"
env: WP_VERSION=latest
env: WP_VERSION=4.7 DEV_LIB_SKIP=composer,phpcs
- php: "5.5"
env: WP_VERSION=latest DEV_LIB_SKIP=phpcs
- php: "5.6"
env: WP_VERSION=4.8 DEV_LIB_SKIP=phpcs
- php: "5.6"
env: WP_VERSION=latest DEV_LIB_SKIP=phpcs
- php: "7.0"
env: WP_VERSION=4.7
# 7.1 / latest already included above as first build.
env: WP_VERSION=latest DEV_LIB_SKIP=phpcs
- php: "7.1"
env: WP_VERSION=4.7
env: WP_VERSION=latest DEV_LIB_SKIP=phpcs
- php: "7.2"
env: WP_VERSION=trunk

install:
- if [[ $DEV_LIB_SKIP =~ composer ]]; then composer install --no-dev; fi
Expand Down
20 changes: 14 additions & 6 deletions includes/amp-frontend-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,31 @@
/**
* Add amphtml link to frontend.
*
* @todo This function's name is incorrect. It's not about adding a canonical link but adding the amphtml link.
*
* @since 0.2
*/
function amp_frontend_add_canonical() {

// Prevent showing amphtml link if theme supports AMP but paired mode is not available.
if ( current_theme_supports( 'amp' ) && ! AMP_Theme_Support::is_paired_available() ) {
return;
}

/**
* Filters whether to show the amphtml link on the frontend.
*
* @todo This filter's name is incorrect. It's not about adding a canonical link but adding the amphtml link.
* @since 0.2
*/
if ( false === apply_filters( 'amp_frontend_show_canonical', true ) ) {
return;
}

printf( '<link rel="amphtml" href="%s">', esc_url( amp_get_permalink( get_queried_object_id() ) ) );
$amp_url = null;
if ( is_singular() ) {
$amp_url = amp_get_permalink( get_queried_object_id() );
} elseif ( isset( $_SERVER['REQUEST_URI'] ) ) {
$host_url = preg_replace( '#(^https?://[^/]+)/.*#', '$1', home_url( '/' ) );
$self_url = esc_url_raw( $host_url . wp_unslash( $_SERVER['REQUEST_URI'] ) );
$amp_url = add_query_arg( amp_get_slug(), '', $self_url );
}
if ( $amp_url ) {
printf( '<link rel="amphtml" href="%s">', esc_url( $amp_url ) );
}
}
70 changes: 29 additions & 41 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public static function init() {

self::purge_amp_query_vars();
self::handle_xhr_request();
self::add_temporary_discussion_restrictions();

require_once AMP__DIR__ . '/includes/amp-post-template-actions.php';

Expand Down Expand Up @@ -121,7 +120,13 @@ public static function init() {
*/
public static function finish_init() {
if ( ! is_amp_endpoint() ) {
amp_add_frontend_actions();
// Add amphtml link when paired mode is available.
if ( self::is_paired_available() ) {
amp_add_frontend_actions(); // @todo This function is poor in how it requires a file that then does add_action().
if ( ! has_action( 'wp_head', 'amp_frontend_add_canonical' ) ) {
add_action( 'wp_head', 'amp_frontend_add_canonical' );
}
}
return;
}

Expand Down Expand Up @@ -228,9 +233,7 @@ public static function add_hooks() {
add_action( 'wp_head', 'amp_add_generator_metadata', 20 );

add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
if ( is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'dequeue_customize_preview_scripts' ), 1000 );
}
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'dequeue_customize_preview_scripts' ), 1000 );
add_filter( 'customize_partial_render', array( __CLASS__, 'filter_customize_partial_render' ) );

add_action( 'wp_footer', 'amp_print_analytics' );
Expand All @@ -248,12 +251,13 @@ public static function add_hooks() {
add_action( 'template_redirect', array( __CLASS__, 'start_output_buffering' ), 0 );

// Commenting hooks.
add_filter( 'wp_list_comments_args', array( __CLASS__, 'amp_set_comments_walker' ), PHP_INT_MAX );
add_filter( 'wp_list_comments_args', array( __CLASS__, 'set_comments_walker' ), PHP_INT_MAX );
add_filter( 'comment_form_defaults', array( __CLASS__, 'filter_comment_form_defaults' ) );
add_filter( 'comment_reply_link', array( __CLASS__, 'filter_comment_reply_link' ), 10, 4 );
add_filter( 'cancel_comment_reply_link', array( __CLASS__, 'filter_cancel_comment_reply_link' ), 10, 3 );
add_action( 'comment_form', array( __CLASS__, 'amend_comment_form' ), 100 );
remove_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' );
add_filter( 'wp_kses_allowed_html', array( __CLASS__, 'whitelist_layout_in_wp_kses_allowed_html' ), 10 );

if ( AMP_Validation_Utils::should_validate_response() ) {
AMP_Validation_Utils::add_validation_hooks();
Expand Down Expand Up @@ -496,35 +500,6 @@ public static function intercept_post_request_redirect( $location ) {
wp_send_json_success();
}

/**
* Set up some restrictions for commenting based on amp-live-list limitations.
*
* Temporarily force comments to be listed in descending order.
* The following hooks are temporary while waiting for amphtml#5396 to be resolved.
*
* @link https://github.com/ampproject/amphtml/issues/5396
*/
protected static function add_temporary_discussion_restrictions() {
add_filter( 'option_comment_order', function() {
return 'desc';
}, PHP_INT_MAX );

add_action( 'admin_print_footer_scripts-options-discussion.php', function() {
?>
<div class="notice notice-info inline" id="amp-comment-notice"><p><?php echo wp_kses_post( __( 'Note: AMP does not yet <a href="https://github.com/ampproject/amphtml/issues/5396" target="_blank">support ascending</a> comments with newer entries appearing at the bottom.', 'amp' ) ); ?></p></div>
<script>
// Move the notice below the selector and disable selector.
jQuery( function( $ ) {
var orderSelect = $( '#comment_order' ),
notice = $( '#amp-comment-notice' );
orderSelect.prop( 'disabled', true );
orderSelect.closest( 'fieldset' ).append( notice );
} );
</script>
<?php
} );
}

/**
* Register/override widgets.
*
Expand Down Expand Up @@ -595,12 +570,9 @@ public static function register_content_embed_handlers() {
* @param array $args the args for the comments list..
* @return array Args to return.
*/
public static function amp_set_comments_walker( $args ) {
public static function set_comments_walker( $args ) {
$amp_walker = new AMP_Comment_Walker();
$args['walker'] = $amp_walker;
// Add reverse order here as well, in case theme overrides it.
$args['reverse_top_level'] = true;

return $args;
}

Expand Down Expand Up @@ -942,12 +914,12 @@ public static function dequeue_customize_preview_scripts() {
public static function start_output_buffering() {
/*
* Disable the New Relic Browser agent on AMP responses.
* This prevents th New Relic from causing invalid AMP responses due the NREUM script it injects after the meta charset:
* This prevents the New Relic from causing invalid AMP responses due the NREUM script it injects after the meta charset:
* https://docs.newrelic.com/docs/browser/new-relic-browser/troubleshooting/google-amp-validator-fails-due-3rd-party-script
* Sites with New Relic will need to specially configure New Relic for AMP:
* https://docs.newrelic.com/docs/browser/new-relic-browser/installation/monitor-amp-pages-new-relic-browser
*/
if ( extension_loaded( 'newrelic' ) ) {
if ( function_exists( 'newrelic_disable_autorum' ) ) {
newrelic_disable_autorum();
}

Expand Down Expand Up @@ -1120,6 +1092,22 @@ public static function prepare_response( $response, $args = array() ) {
return $response;
}

/**
* Adds 'data-amp-layout' to the allowed <img> attributes for wp_kses().
*
* @since 0.7
*
* @param array $context Allowed tags and their allowed attributes.
* @return array $context Filtered allowed tags and attributes.
*/
public static function whitelist_layout_in_wp_kses_allowed_html( $context ) {
if ( ! empty( $context['img']['width'] ) && ! empty( $context['img']['height'] ) ) {
$context['img']['data-amp-layout'] = true;
}

return $context;
}

/**
* Enqueue AMP assets if this is an AMP endpoint.
*
Expand Down
Loading

0 comments on commit b278bc2

Please sign in to comment.