From 90f673dd5aa3b58d1d22dbf130d8c11407fcdcb7 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 1 May 2018 14:33:44 -0700 Subject: [PATCH] Add AMP theme support --- comments.php | 26 ++++++++++++++- functions.php | 54 ++++++++++++++++++++++++++++++-- header.php | 37 ++++++++++++++++++++-- inc/template-functions.php | 12 +++++++ sass/_normalize.scss | 6 ++++ sass/forms/_buttons.scss | 1 + sass/navigation/_menus.scss | 18 ++++++++++- sass/typography/_typography.scss | 1 + style.css | 38 ++++++++++++++++++---- 9 files changed, 180 insertions(+), 13 deletions(-) diff --git a/comments.php b/comments.php index 2c94bfd038..b9eeb2b86d 100644 --- a/comments.php +++ b/comments.php @@ -48,7 +48,16 @@ -
    + + + data-poll-interval="" + data-max-items-per-page="" + > + + +
      > 'ol', @@ -58,8 +67,23 @@
    + +
    + +
    +
    + + + diff --git a/functions.php b/functions.php index b7134e760e..f64e0d45bd 100644 --- a/functions.php +++ b/functions.php @@ -24,6 +24,10 @@ function _s_setup() { */ load_theme_textdomain( '_s', get_template_directory() . '/languages' ); + add_theme_support( 'amp', array( + 'comments_live_list' => true, + ) ); + // Add default posts and comments RSS feed links to head. add_theme_support( 'automatic-feed-links' ); @@ -117,10 +121,56 @@ function _s_widgets_init() { add_action( 'widgets_init', '_s_widgets_init' ); /** - * Enqueue scripts and styles. + * Determine whether this is an AMP response. + * + * Note that this must only be called after the parse_query action. + * + * @link https://github.com/Automattic/amp-wp */ -function _s_scripts() { +function _s_is_amp() { + return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint(); +} + +/** + * Detemrine whether amp-live-list should be used for the comment list. + * + * @return bool Whether to use amp-live-list. + */ +function _s_using_amp_live_list_comments() { + if ( ! _s_is_amp() ) { + return false; + } + $amp_theme_support = get_theme_support( 'amp' ); + return ! empty( $amp_theme_support[0]['comments_live_list'] ); +} + +/** + * Enqueue styles. + */ +function _s_styles() { wp_enqueue_style( '_s-style', get_stylesheet_uri() ); +} +add_action( 'wp_enqueue_scripts', '_s_styles' ); + +/** + * Enqueue scripts. + * + * This short-circuits in AMP because custom scripts are not allowed. There is are AMP equivalents provided elsewhere. + * + * navigation: + * In AMP the :focus-within selector is used to keep submenus displayed while tabbing, + * and amp-bind is used to managed the toggled state of the nav menu on small screens. + * + * skip-link-focus-fix: + * This is not implemented in AMP because it only relates to IE11, a browser which now has a very small market share. + * + * comment-reply: + * Support for comment replies is provided by the AMP plugin. + */ +function _s_scripts() { + if ( _s_is_amp() ) { + return; + } wp_enqueue_script( '_s-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20151215', true ); diff --git a/header.php b/header.php index 776cbdcd9f..0f54ca043f 100644 --- a/header.php +++ b/header.php @@ -14,7 +14,7 @@ > - + @@ -44,8 +44,39 @@ -