From 57926fb2e6a2aa46d5c0b6b0e6d47c13632ccb39 Mon Sep 17 00:00:00 2001 From: ThierryA Date: Wed, 20 Dec 2017 10:41:38 +0100 Subject: [PATCH 01/16] Bumped plugin version to 0.6-beta --- amp.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/amp.php b/amp.php index b9c6524fead..1d03c274cd2 100644 --- a/amp.php +++ b/amp.php @@ -5,7 +5,7 @@ * Plugin URI: https://github.com/automattic/amp-wp * Author: Automattic * Author URI: https://automattic.com - * Version: 0.6-alpha + * Version: 0.6-beta * Text Domain: amp * Domain Path: /languages/ * License: GPLv2 or later @@ -15,7 +15,7 @@ define( 'AMP__FILE__', __FILE__ ); define( 'AMP__DIR__', dirname( __FILE__ ) ); -define( 'AMP__VERSION', '0.6-alpha' ); +define( 'AMP__VERSION', '0.6-beta' ); require_once AMP__DIR__ . '/includes/class-amp-autoloader.php'; AMP_Autoloader::register(); From 309af3d21bd5f404d18bb58d7ae3b54afff02e45 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 9 Jan 2018 14:31:47 -0800 Subject: [PATCH 02/16] Update version and readme --- amp.php | 4 ++-- readme.md | 24 ++++++++++-------------- readme.txt | 24 ++++++++++-------------- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/amp.php b/amp.php index 1d03c274cd2..c357adb6409 100644 --- a/amp.php +++ b/amp.php @@ -5,7 +5,7 @@ * Plugin URI: https://github.com/automattic/amp-wp * Author: Automattic * Author URI: https://automattic.com - * Version: 0.6-beta + * Version: 0.6-beta1 * Text Domain: amp * Domain Path: /languages/ * License: GPLv2 or later @@ -15,7 +15,7 @@ define( 'AMP__FILE__', __FILE__ ); define( 'AMP__DIR__', dirname( __FILE__ ) ); -define( 'AMP__VERSION', '0.6-beta' ); +define( 'AMP__VERSION', '0.6-beta1' ); require_once AMP__DIR__ . '/includes/class-amp-autoloader.php'; AMP_Autoloader::register(); diff --git a/readme.md b/readme.md index 0cd32bed08c..3aa1e8ebd04 100644 --- a/readme.md +++ b/readme.md @@ -56,20 +56,16 @@ Follow along with or [contribute](https://github.com/Automattic/amp-wp/blob/deve ## Changelog ## ### 0.6 (unreleased) ### -- Add: support for the "page" post type, except when used as homepage or page for posts. A new `page.php` is introduced with template parts factored out (`html-start.php`, `header.php`, `footer.php`, `html-end.php`) and re-used from `single.php`. Note that AMP URLs will end in `?amp` instead of `/amp/`. See [#825](https://github.com/Automattic/amp-wp/pull/825). Props technosailor, ThierryA, westonruter. -- Add: AMP post preview button alongside non-AMP preview button. See [#813](https://github.com/Automattic/amp-wp/pull/813). Props ThierryA, westonruter. -- Add: ability to disable AMP on a per-post basis via toggle in publish metabox. See [#813](https://github.com/Automattic/amp-wp/pull/813). Props ThierryA, westonruter. -- Add: AMP settings admin screen for managing which post types have AMP support, eliminating the requirement to add `add_post_type_support()` calls in theme or plugin. See [#811](https://github.com/Automattic/amp-wp/pull/811). Props ThierryA, westonruter. -- Add: generator meta tag for AMP. See [#810](https://github.com/Automattic/amp-wp/pull/810). Props vaporwavre. -- Add: code quality checking via phpcs, eslint, jscs, and jshint. See [#795](https://github.com/Automattic/amp-wp/pull/795). Props westonruter. -- Add: autoloader to reduce complexity. See [#828](https://github.com/Automattic/amp-wp/pull/828). Props mikeschinkel, westonruter, ThierryA. -- Add: Vanilla WordPress "embed" test page. A new `bin/create-embed-test-post.php` wp-cli script is introduced. See [#829](https://github.com/Automattic/amp-wp/pull/829). Props kienstra, westonruter, ThierryA. -- Move: AMP Customizer into main Customizer. See [#819](https://github.com/Automattic/amp-wp/pull/819). Props kaitnyl, westonruter. -- Update: AMP HTML tags and attributes. A new `bin/amphtml-update.sh` bash script is introduced. See [#823](https://github.com/Automattic/amp-wp/pull/823). Props kienstra, ThierryA, westonruter. -- Fix: SoundCloud embed -- Fix: Polldaddy embed -- Fix: Playbuzz not working -- Fix: not allowed script tag +- Add support for the "page" post type, except when used as homepage or page for posts. A new `page.php` is introduced with template parts factored out (`html-start.php`, `header.php`, `footer.php`, `html-end.php`) and re-used from `single.php`. Note that AMP URLs will end in `?amp` instead of `/amp/`. See [#825](https://github.com/Automattic/amp-wp/pull/825). Props technosailor, ThierryA, westonruter. +- Add AMP post preview button alongside non-AMP preview button. See [#813](https://github.com/Automattic/amp-wp/pull/813). Props ThierryA, westonruter. +- Add ability to disable AMP on a per-post basis via toggle in publish metabox. See [#813](https://github.com/Automattic/amp-wp/pull/813). Props ThierryA, westonruter. +- Add AMP settings admin screen for managing which post types have AMP support, eliminating the requirement to add `add_post_type_support()` calls in theme or plugin. See [#811](https://github.com/Automattic/amp-wp/pull/811). Props ThierryA, westonruter. +- Add generator meta tag for AMP. See [#810](https://github.com/Automattic/amp-wp/pull/810). Props vaporwavre. +- Add code quality checking via phpcs, eslint, jscs, and jshint. See [#795](https://github.com/Automattic/amp-wp/pull/795). Props westonruter. +- Add autoloader to reduce complexity. See [#828](https://github.com/Automattic/amp-wp/pull/828). Props mikeschinkel, westonruter, ThierryA. +- Fix Polldaddy amd SoundCloud embeds. Add vanilla WordPress "embed" test page. A new `bin/create-embed-test-post.php` wp-cli script is introduced. See [#829](https://github.com/Automattic/amp-wp/pull/829). Props kienstra, westonruter, ThierryA. +- Merge AMP Customizer into main Customizer. See [#819](https://github.com/Automattic/amp-wp/pull/819). Props kaitnyl, westonruter. +- Update AMP HTML tags and attributes. A new `bin/amphtml-update.sh` bash script is introduced. Fixes Playbuzz. See [#823](https://github.com/Automattic/amp-wp/pull/823). Props kienstra, ThierryA, westonruter. See [0.6 milestone](https://github.com/Automattic/amp-wp/milestone/5?closed=1). diff --git a/readme.txt b/readme.txt index 23eef7f2505..1f7ec3d4c30 100644 --- a/readme.txt +++ b/readme.txt @@ -40,20 +40,16 @@ Follow along with or [contribute](https://github.com/Automattic/amp-wp/blob/deve = 0.6 (unreleased) = -- Add: support for the "page" post type, except when used as homepage or page for posts. A new `page.php` is introduced with template parts factored out (`html-start.php`, `header.php`, `footer.php`, `html-end.php`) and re-used from `single.php`. Note that AMP URLs will end in `?amp` instead of `/amp/`. See [#825](https://github.com/Automattic/amp-wp/pull/825). Props technosailor, ThierryA, westonruter. -- Add: AMP post preview button alongside non-AMP preview button. See [#813](https://github.com/Automattic/amp-wp/pull/813). Props ThierryA, westonruter. -- Add: ability to disable AMP on a per-post basis via toggle in publish metabox. See [#813](https://github.com/Automattic/amp-wp/pull/813). Props ThierryA, westonruter. -- Add: AMP settings admin screen for managing which post types have AMP support, eliminating the requirement to add `add_post_type_support()` calls in theme or plugin. See [#811](https://github.com/Automattic/amp-wp/pull/811). Props ThierryA, westonruter. -- Add: generator meta tag for AMP. See [#810](https://github.com/Automattic/amp-wp/pull/810). Props vaporwavre. -- Add: code quality checking via phpcs, eslint, jscs, and jshint. See [#795](https://github.com/Automattic/amp-wp/pull/795). Props westonruter. -- Add: autoloader to reduce complexity. See [#828](https://github.com/Automattic/amp-wp/pull/828). Props mikeschinkel, westonruter, ThierryA. -- Add: Vanilla WordPress "embed" test page. A new `bin/create-embed-test-post.php` wp-cli script is introduced. See [#829](https://github.com/Automattic/amp-wp/pull/829). Props kienstra, westonruter, ThierryA. -- Move: AMP Customizer into main Customizer. See [#819](https://github.com/Automattic/amp-wp/pull/819). Props kaitnyl, westonruter. -- Update: AMP HTML tags and attributes. A new `bin/amphtml-update.sh` bash script is introduced. See [#823](https://github.com/Automattic/amp-wp/pull/823). Props kienstra, ThierryA, westonruter. -- Fix: SoundCloud embed -- Fix: Polldaddy embed -- Fix: Playbuzz not working -- Fix: not allowed script tag +- Add support for the "page" post type, except when used as homepage or page for posts. A new `page.php` is introduced with template parts factored out (`html-start.php`, `header.php`, `footer.php`, `html-end.php`) and re-used from `single.php`. Note that AMP URLs will end in `?amp` instead of `/amp/`. See [#825](https://github.com/Automattic/amp-wp/pull/825). Props technosailor, ThierryA, westonruter. +- Add AMP post preview button alongside non-AMP preview button. See [#813](https://github.com/Automattic/amp-wp/pull/813). Props ThierryA, westonruter. +- Add ability to disable AMP on a per-post basis via toggle in publish metabox. See [#813](https://github.com/Automattic/amp-wp/pull/813). Props ThierryA, westonruter. +- Add AMP settings admin screen for managing which post types have AMP support, eliminating the requirement to add `add_post_type_support()` calls in theme or plugin. See [#811](https://github.com/Automattic/amp-wp/pull/811). Props ThierryA, westonruter. +- Add generator meta tag for AMP. See [#810](https://github.com/Automattic/amp-wp/pull/810). Props vaporwavre. +- Add code quality checking via phpcs, eslint, jscs, and jshint. See [#795](https://github.com/Automattic/amp-wp/pull/795). Props westonruter. +- Add autoloader to reduce complexity. See [#828](https://github.com/Automattic/amp-wp/pull/828). Props mikeschinkel, westonruter, ThierryA. +- Fix Polldaddy amd SoundCloud embeds. Add vanilla WordPress "embed" test page. A new `bin/create-embed-test-post.php` wp-cli script is introduced. See [#829](https://github.com/Automattic/amp-wp/pull/829). Props kienstra, westonruter, ThierryA. +- Merge AMP Customizer into main Customizer. See [#819](https://github.com/Automattic/amp-wp/pull/819). Props kaitnyl, westonruter. +- Update AMP HTML tags and attributes. A new `bin/amphtml-update.sh` bash script is introduced. Fixes Playbuzz. See [#823](https://github.com/Automattic/amp-wp/pull/823). Props kienstra, ThierryA, westonruter. See [0.6 milestone](https://github.com/Automattic/amp-wp/milestone/5?closed=1). From 63dae3ebd907d71ece367eaebef442add72271c3 Mon Sep 17 00:00:00 2001 From: Kaitlyn Date: Thu, 11 Jan 2018 11:29:41 -0400 Subject: [PATCH 03/16] Allow custom AMP template folder to be specified from a theme. --- amp.php | 1 + .../templates/class-amp-post-template.php | 43 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/amp.php b/amp.php index f854b6caea3..a7245ff3359 100644 --- a/amp.php +++ b/amp.php @@ -176,6 +176,7 @@ function amp_is_canonical() { return true; } } + return false; } diff --git a/includes/templates/class-amp-post-template.php b/includes/templates/class-amp-post-template.php index 2f88c6545f4..1a6bfce3284 100644 --- a/includes/templates/class-amp-post-template.php +++ b/includes/templates/class-amp-post-template.php @@ -89,6 +89,15 @@ public function __construct( $post ) { $this->template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' ); + // Custom template root added via theme support will take priority over above filter. + $support = get_theme_support( 'amp' ); + if ( is_array( $support ) ) { + $args = array_shift( $support ); + if ( ! empty( $args['template_path'] ) ) { + $this->template_dir = $args['template_path']; + } + } + if ( $post instanceof WP_Post ) { $this->post = $post; } else { @@ -200,8 +209,38 @@ public function get_customizer_setting( $name, $default = null ) { * Load and print the template parts for the given post. */ public function load() { - $template = is_page() ? 'page' : 'single'; - $this->load_parts( array( $template ) ); + $template = false; + + /** + * Follow WP core hierarchy process for template decisions. + * + * @see template-loader.php + */ + if ( is_embed() && $template = get_query_template( 'embed' ) ) : + elseif ( is_404() && $template = get_query_template( '404' ) ) : + elseif ( is_search() && $template = get_query_template( 'search' ) ) : + elseif ( is_front_page() && $template = get_query_template( 'frontpage' ) ) : + elseif ( is_home() && $template = get_query_template( 'home') ) : + elseif ( is_post_type_archive() && $template = get_query_template( 'archive' ) ) : + elseif ( is_tax() && $template = get_query_template( 'taxonomy' ) ) : + elseif ( is_attachment() && $template = get_query_template( 'attachment' ) ) : + remove_filter( 'the_content', 'prepend_attachment' ); + elseif ( is_single() && $template = get_query_template( 'single' ) ) : + elseif ( is_page() && $template = get_query_template( 'page' ) ) : + elseif ( is_singular() && $template = get_query_template( 'singular' ) ) : + elseif ( is_category() && $template = get_query_template( 'category' ) ) : + elseif ( is_tag() && $template = get_query_template( 'tag' ) ) : + elseif ( is_author() && $template = get_query_template( 'author' ) ) : + elseif ( is_date() && $template = get_query_template( 'date' ) ) : + elseif ( is_archive() && $template = get_query_template( 'archive' ) ) : + else : + $template = get_query_template( 'index' ); + endif; + + $parts = explode( '/', $template ); + $file_name = str_replace( '.php', '', end( $parts ) ); + + $this->load_parts( array( $file_name ) ); } /** From 2bb0bb4b813cfba0eb70b2bce3bf449208814afb Mon Sep 17 00:00:00 2001 From: Kaitlyn Date: Thu, 11 Jan 2018 15:09:32 -0400 Subject: [PATCH 04/16] Move to filtering template_include when it comes to custom templates in order to better follow WP hierarchy. --- amp.php | 13 ++-- includes/class-amp-post-type-support.php | 4 +- .../templates/class-amp-post-template.php | 63 ++++++++----------- 3 files changed, 38 insertions(+), 42 deletions(-) diff --git a/amp.php b/amp.php index a7245ff3359..9d3171b0f5c 100644 --- a/amp.php +++ b/amp.php @@ -127,7 +127,7 @@ function amp_force_query_var_value( $query_vars ) { * @return void */ function amp_maybe_add_actions() { - if ( amp_is_canonical() || ! is_singular() || is_feed() ) { + if ( amp_is_canonical() || is_feed() ) { return; } @@ -140,7 +140,7 @@ function amp_maybe_add_actions() { $supports = post_supports_amp( $post ); if ( ! $supports ) { - if ( $is_amp_endpoint ) { + if ( $is_amp_endpoint && isset( $post->ID ) ) { wp_safe_redirect( get_permalink( $post->ID ), 301 ); exit; } @@ -207,7 +207,6 @@ function amp_render() { $post = get_queried_object(); if ( $post instanceof WP_Post ) { amp_render_post( $post ); - exit; } } @@ -250,7 +249,13 @@ function amp_render_post( $post ) { amp_add_post_template_actions(); $template = new AMP_Post_Template( $post ); - $template->load(); + + if ( $template->custom_template ) { + add_filter( 'template_include', array( $template, 'load_custom' ) ); + } else { + $template->load_default(); + exit; + } if ( ! $was_set ) { unset( $wp_query->query_vars[ AMP_QUERY_VAR ] ); diff --git a/includes/class-amp-post-type-support.php b/includes/class-amp-post-type-support.php index 12b10c73f59..54c899a1098 100644 --- a/includes/class-amp-post-type-support.php +++ b/includes/class-amp-post-type-support.php @@ -72,7 +72,7 @@ public static function get_support_errors( $post ) { $errors = array(); // Because `add_rewrite_endpoint` doesn't let us target specific post_types. - if ( ! post_type_supports( $post->post_type, AMP_QUERY_VAR ) ) { + if ( isset( $post->post_type ) && ! post_type_supports( $post->post_type, AMP_QUERY_VAR ) ) { $errors[] = 'post-type-support'; } @@ -103,7 +103,7 @@ public static function get_support_errors( $post ) { * @param int $post_id Post ID. * @param WP_Post $post Post. */ - if ( true === apply_filters( 'amp_skip_post', false, $post->ID, $post ) ) { + if ( isset( $post->ID ) && true === apply_filters( 'amp_skip_post', false, $post->ID, $post ) ) { $errors[] = 'skip-post'; } diff --git a/includes/templates/class-amp-post-template.php b/includes/templates/class-amp-post-template.php index 1a6bfce3284..1447fa5aad4 100644 --- a/includes/templates/class-amp-post-template.php +++ b/includes/templates/class-amp-post-template.php @@ -80,21 +80,30 @@ class AMP_Post_Template { */ public $post; + /** + * Loading custom template. + * + * @since 0.7 + * @var bool + */ + public $custom_template; + /** * AMP_Post_Template constructor. * * @param WP_Post|int $post Post. */ public function __construct( $post ) { - - $this->template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' ); + $this->template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' ); + $this->custom_template = false; // Custom template root added via theme support will take priority over above filter. $support = get_theme_support( 'amp' ); if ( is_array( $support ) ) { $args = array_shift( $support ); if ( ! empty( $args['template_path'] ) ) { - $this->template_dir = $args['template_path']; + $this->template_dir = $args['template_path']; + $this->custom_template = true; } } @@ -206,41 +215,23 @@ public function get_customizer_setting( $name, $default = null ) { } /** - * Load and print the template parts for the given post. + * Load and print the default template parts for the given post. */ - public function load() { - $template = false; + public function load_default() { + $template = is_page() ? 'page' : 'single'; + $this->load_parts( array( $template ) ); + } - /** - * Follow WP core hierarchy process for template decisions. - * - * @see template-loader.php - */ - if ( is_embed() && $template = get_query_template( 'embed' ) ) : - elseif ( is_404() && $template = get_query_template( '404' ) ) : - elseif ( is_search() && $template = get_query_template( 'search' ) ) : - elseif ( is_front_page() && $template = get_query_template( 'frontpage' ) ) : - elseif ( is_home() && $template = get_query_template( 'home') ) : - elseif ( is_post_type_archive() && $template = get_query_template( 'archive' ) ) : - elseif ( is_tax() && $template = get_query_template( 'taxonomy' ) ) : - elseif ( is_attachment() && $template = get_query_template( 'attachment' ) ) : - remove_filter( 'the_content', 'prepend_attachment' ); - elseif ( is_single() && $template = get_query_template( 'single' ) ) : - elseif ( is_page() && $template = get_query_template( 'page' ) ) : - elseif ( is_singular() && $template = get_query_template( 'singular' ) ) : - elseif ( is_category() && $template = get_query_template( 'category' ) ) : - elseif ( is_tag() && $template = get_query_template( 'tag' ) ) : - elseif ( is_author() && $template = get_query_template( 'author' ) ) : - elseif ( is_date() && $template = get_query_template( 'date' ) ) : - elseif ( is_archive() && $template = get_query_template( 'archive' ) ) : - else : - $template = get_query_template( 'index' ); - endif; - - $parts = explode( '/', $template ); - $file_name = str_replace( '.php', '', end( $parts ) ); - - $this->load_parts( array( $file_name ) ); + /** + * Use default WP template that should load and replace root dir with custom. + * + * @param string $default_template Template that WP would be referencing. + */ + public function load_custom( $default_template ) { + $parts = explode( '/', $default_template ); + $file_name = end( $parts ); + + return sprintf( '%s%s', $this->template_dir, $file_name ); } /** From 6bbe96531564c09cd2eefc3c20ce238b27cadc60 Mon Sep 17 00:00:00 2001 From: Kaitlyn Date: Thu, 11 Jan 2018 16:41:37 -0400 Subject: [PATCH 05/16] Callback to further control when custom paired mode templates should be used. --- amp.php | 13 ++++--- .../templates/class-amp-post-template.php | 34 ++++++++++++++----- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/amp.php b/amp.php index 9d3171b0f5c..c43c6b222c7 100644 --- a/amp.php +++ b/amp.php @@ -250,11 +250,14 @@ function amp_render_post( $post ) { amp_add_post_template_actions(); $template = new AMP_Post_Template( $post ); - if ( $template->custom_template ) { - add_filter( 'template_include', array( $template, 'load_custom' ) ); - } else { - $template->load_default(); - exit; + // Default AMP templates or use custom folder and follow WP template hierarchy. + if ( $template->active ) { + if ( $template->custom_template_dir ) { + add_filter( 'template_include', array( $template, 'load_custom' ) ); + } else { + $template->load_default(); + exit; + } } if ( ! $was_set ) { diff --git a/includes/templates/class-amp-post-template.php b/includes/templates/class-amp-post-template.php index 1447fa5aad4..0aefa029977 100644 --- a/includes/templates/class-amp-post-template.php +++ b/includes/templates/class-amp-post-template.php @@ -81,12 +81,20 @@ class AMP_Post_Template { public $post; /** - * Loading custom template. + * Custom template root. * * @since 0.7 * @var bool */ - public $custom_template; + public $custom_template_dir; + + /** + * If template system is currently active. + * + * @since 0.7 + * @var bool + */ + public $active; /** * AMP_Post_Template constructor. @@ -94,16 +102,26 @@ class AMP_Post_Template { * @param WP_Post|int $post Post. */ public function __construct( $post ) { - $this->template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' ); - $this->custom_template = false; + $this->template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' ); + $this->custom_template_dir = false; + $this->custom_template_active = false; - // Custom template root added via theme support will take priority over above filter. + /** + * These may be configured within theme code. + * + * Custom template root added via theme support. + * Can also further control if custom templates are applicable depending on $post. + */ $support = get_theme_support( 'amp' ); if ( is_array( $support ) ) { $args = array_shift( $support ); + if ( ! empty( $args['template_path'] ) ) { - $this->template_dir = $args['template_path']; - $this->custom_template = true; + $this->custom_template_dir = $args['template_path']; + } + + if ( isset( $args['active_callback'] ) ) { + $this->active = $args['active_callback'](); } } @@ -231,7 +249,7 @@ public function load_custom( $default_template ) { $parts = explode( '/', $default_template ); $file_name = end( $parts ); - return sprintf( '%s%s', $this->template_dir, $file_name ); + return sprintf( '%s%s', $this->custom_template_dir, $file_name ); } /** From ca4536ceb9505b3873d0b1e36980ede037f990d3 Mon Sep 17 00:00:00 2001 From: Kaitlyn Date: Thu, 11 Jan 2018 16:48:31 -0400 Subject: [PATCH 06/16] Fix default if no callback given. --- amp.php | 1 - includes/templates/class-amp-post-template.php | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/amp.php b/amp.php index c43c6b222c7..936582c512f 100644 --- a/amp.php +++ b/amp.php @@ -176,7 +176,6 @@ function amp_is_canonical() { return true; } } - return false; } diff --git a/includes/templates/class-amp-post-template.php b/includes/templates/class-amp-post-template.php index 0aefa029977..c7ef503813e 100644 --- a/includes/templates/class-amp-post-template.php +++ b/includes/templates/class-amp-post-template.php @@ -102,9 +102,9 @@ class AMP_Post_Template { * @param WP_Post|int $post Post. */ public function __construct( $post ) { - $this->template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' ); - $this->custom_template_dir = false; - $this->custom_template_active = false; + $this->active = true; + $this->template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' ); + $this->custom_template_dir = false; /** * These may be configured within theme code. From 95af9e7bc8a2e2d117d57befbd38385700218e9f Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 12 Jan 2018 16:52:46 -0800 Subject: [PATCH 07/16] Move paired mode template loading into AMP_Theme_Support class Implement full template hierarchy support --- amp.php | 14 +- includes/class-amp-theme-support.php | 139 ++++++++++++++++-- .../templates/class-amp-post-template.php | 54 +------ 3 files changed, 136 insertions(+), 71 deletions(-) diff --git a/amp.php b/amp.php index 20714e41adf..1b576c6b45c 100644 --- a/amp.php +++ b/amp.php @@ -131,7 +131,7 @@ function amp_maybe_add_actions() { // Add hooks for when a themes that support AMP. if ( current_theme_supports( 'amp' ) ) { if ( amp_is_canonical() || is_amp_endpoint() ) { - AMP_Theme_Support::register_hooks(); + AMP_Theme_Support::init(); } else { AMP_Frontend_Actions::register_hooks(); } @@ -218,6 +218,7 @@ function amp_render() { $post = get_queried_object(); if ( $post instanceof WP_Post ) { amp_render_post( $post ); + exit; } } @@ -260,16 +261,7 @@ function amp_render_post( $post ) { amp_add_post_template_actions(); $template = new AMP_Post_Template( $post ); - - // Default AMP templates or use custom folder and follow WP template hierarchy. - if ( $template->active ) { - if ( $template->custom_template_dir ) { - add_filter( 'template_include', array( $template, 'load_custom' ) ); - } else { - $template->load_default(); - exit; - } - } + $template->load(); if ( ! $was_set ) { unset( $wp_query->query_vars[ AMP_QUERY_VAR ] ); diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 2693358cf57..3907151c803 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -14,6 +14,77 @@ class AMP_Theme_Support { const COMPONENT_SCRIPTS_PLACEHOLDER = ''; + /** + * Template types. + * + * @var array + */ + protected static $template_types = array( + 'paged', // Deprecated. + 'index', + '404', + 'archive', + 'author', + 'category', + 'tag', + 'taxonomy', + 'date', + 'home', + 'front_page', + 'page', + 'search', + 'single', + 'embed', + 'singular', + 'attachment', + ); + + /** + * Initialize. + */ + public static function init() { + if ( ! amp_is_canonical() ) { + self::register_paired_hooks(); + } + self::register_hooks(); + } + + /** + * Determines whether paired mode is available. + * + * When 'amp' theme support has not been added or canonical mode is enabled, then this returns false. + * Returns true when there is a template_path defined in theme support, and if a defined active_callback + * returns true. + * + * @todo Make sure this is used when determining whether to show the amphtml link. + * + * @return bool Whether available. + */ + public static function is_paired_available() { + $support = get_theme_support( 'amp' ); + if ( empty( $support ) || amp_is_canonical() ) { + return false; + } + + $args = array_shift( $support ); + + // @todo We might want to rename active_callback to available_callback.. + if ( isset( $args['active_callback'] ) && is_callable( $args['active_callback'] ) ) { + return $args['active_callback'](); + } + return true; + } + + /** + * Register hooks for paired mode. + */ + public static function register_paired_hooks() { + foreach ( self::$template_types as $template_type ) { + add_filter( "{$template_type}_template_hierarchy", array( __CLASS__, 'filter_paired_template_hierarchy' ) ); + } + add_filter( 'template_include', array( __CLASS__, 'filter_paired_template_include' ), 100 ); + } + /** * Register hooks. */ @@ -54,6 +125,44 @@ public static function register_hooks() { // @todo Add character conversion. } + /** + * Prepends template hierarchy with template_path for AMP paired mode templates. + * + * @see get_query_template() + * + * @param array $templates Template hierarchy. + * @returns array Templates. + */ + public static function filter_paired_template_hierarchy( $templates ) { + $support = get_theme_support( 'amp' ); + $args = array_shift( $support ); + if ( isset( $args['template_path'] ) ) { + $amp_templates = array(); + foreach ( $templates as $template ) { + $amp_templates[] = $args['template_path'] . '/' . $template; + } + $templates = $amp_templates; + } + return $templates; + } + + /** + * Redirect to the non-canonical URL when the template to include is empty. + * + * This is a failsafe in case an index.php is not located in the AMP template_path, + * and the active_callback fails to omit a given request from being available in AMP. + * + * @param string $template Template to include. + * @return string Template to include. + */ + public static function filter_paired_template_include( $template ) { + if ( empty( $template ) || ! self::is_paired_available() ) { + wp_safe_redirect( self::get_current_canonical_url() ); + exit; + } + return $template; + } + /** * Print meta charset tag. * @@ -85,19 +194,15 @@ public static function add_scripts() { } /** - * Add canonical link. - * - * Replaces `rel_canonical()` which only outputs canonical URLs for singular posts and pages. - * This can be removed once WP Core #18660 lands. - * - * @link https://www.ampproject.org/docs/reference/spec#canon. - * @link https://core.trac.wordpress.org/ticket/18660 + * Get canonical URL for current request. * * @see rel_canonical() * @global WP $wp * @global WP_Rewrite $wp_rewrite + * + * @return string Canonical non-AMP URL. */ - public static function add_canonical_link() { + public static function get_current_canonical_url() { global $wp, $wp_rewrite; $url = null; @@ -136,7 +241,23 @@ public static function add_canonical_link() { $url = remove_query_arg( AMP_QUERY_VAR, $url ); } - echo '' . "\n"; + return $url; + } + + /** + * Add canonical link. + * + * Replaces `rel_canonical()` which only outputs canonical URLs for singular posts and pages. + * This can be removed once WP Core #18660 lands. + * + * @link https://www.ampproject.org/docs/reference/spec#canon. + * @link https://core.trac.wordpress.org/ticket/18660 + */ + public static function add_canonical_link() { + $url = self::get_current_canonical_url(); + if ( ! empty( $url ) ) { + printf( '', esc_url( $url ) ); + } } /** diff --git a/includes/templates/class-amp-post-template.php b/includes/templates/class-amp-post-template.php index c7ef503813e..2f88c6545f4 100644 --- a/includes/templates/class-amp-post-template.php +++ b/includes/templates/class-amp-post-template.php @@ -80,50 +80,14 @@ class AMP_Post_Template { */ public $post; - /** - * Custom template root. - * - * @since 0.7 - * @var bool - */ - public $custom_template_dir; - - /** - * If template system is currently active. - * - * @since 0.7 - * @var bool - */ - public $active; - /** * AMP_Post_Template constructor. * * @param WP_Post|int $post Post. */ public function __construct( $post ) { - $this->active = true; - $this->template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' ); - $this->custom_template_dir = false; - - /** - * These may be configured within theme code. - * - * Custom template root added via theme support. - * Can also further control if custom templates are applicable depending on $post. - */ - $support = get_theme_support( 'amp' ); - if ( is_array( $support ) ) { - $args = array_shift( $support ); - - if ( ! empty( $args['template_path'] ) ) { - $this->custom_template_dir = $args['template_path']; - } - if ( isset( $args['active_callback'] ) ) { - $this->active = $args['active_callback'](); - } - } + $this->template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' ); if ( $post instanceof WP_Post ) { $this->post = $post; @@ -233,25 +197,13 @@ public function get_customizer_setting( $name, $default = null ) { } /** - * Load and print the default template parts for the given post. + * Load and print the template parts for the given post. */ - public function load_default() { + public function load() { $template = is_page() ? 'page' : 'single'; $this->load_parts( array( $template ) ); } - /** - * Use default WP template that should load and replace root dir with custom. - * - * @param string $default_template Template that WP would be referencing. - */ - public function load_custom( $default_template ) { - $parts = explode( '/', $default_template ); - $file_name = end( $parts ); - - return sprintf( '%s%s', $this->custom_template_dir, $file_name ); - } - /** * Load template parts. * From 184e89ab81ada0aa965881ce796566618d199832 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 12 Jan 2018 16:53:49 -0800 Subject: [PATCH 08/16] Ensure post_supports_amp() and is_amp_endpoint() return true when canonical --- includes/amp-helper-functions.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index 95b01f16a56..4f078702273 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -63,6 +63,10 @@ function amp_get_permalink( $post_id ) { * @return bool Whether the post supports AMP. */ function post_supports_amp( $post ) { + if ( amp_is_canonical() ) { + return true; + } + return 0 === count( AMP_Post_Type_Support::get_support_errors( $post ) ); } @@ -74,6 +78,10 @@ function post_supports_amp( $post ) { * @return bool Whether it is the AMP endpoint. */ function is_amp_endpoint() { + if ( amp_is_canonical() ) { + return true; + } + if ( 0 === did_action( 'parse_query' ) ) { _doing_it_wrong( __FUNCTION__, sprintf( esc_html__( "is_amp_endpoint() was called before the 'parse_query' hook was called. This function will always return 'false' before the 'parse_query' hook is called.", 'amp' ) ), '0.4.2' ); } From 13bbfabe50e03f6c28dac70d743316681a64a94f Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 12 Jan 2018 17:49:54 -0800 Subject: [PATCH 09/16] Revert "Merge pull request #725 from Automattic/amedina/add-amp-actions-class-hierarchy" This reverts commit c5237e6b87411da0aa66f5dcc0d1e6b0c9c219f9, reversing changes made to 41af09690eae54a3e832bf31ad5938a1a2d26dfa. Fixes merge conflicts introduced by #810 and #828. --- amp.php | 7 +- includes/actions/class-amp-actions.php | 20 -- .../actions/class-amp-frontend-actions.php | 32 ---- .../actions/class-amp-paired-post-actions.php | 178 ------------------ includes/amp-frontend-actions.php | 28 +++ includes/amp-post-template-actions.php | 170 +++++++++++++++++ includes/class-amp-autoloader.php | 3 - .../templates/class-amp-post-template.php | 1 - 8 files changed, 202 insertions(+), 237 deletions(-) delete mode 100644 includes/actions/class-amp-actions.php delete mode 100644 includes/actions/class-amp-frontend-actions.php delete mode 100644 includes/actions/class-amp-paired-post-actions.php create mode 100644 includes/amp-frontend-actions.php create mode 100644 includes/amp-post-template-actions.php diff --git a/amp.php b/amp.php index c357adb6409..b773c77e7a7 100644 --- a/amp.php +++ b/amp.php @@ -150,12 +150,13 @@ function amp_load_classes() { } function amp_add_frontend_actions() { - AMP_Frontend_Actions::register_hooks(); + require_once AMP__DIR__ . '/includes/amp-frontend-actions.php'; } function amp_add_post_template_actions() { - AMP_Paired_Post_Actions::register_hooks(); - require_once( AMP__DIR__ . '/includes/amp-post-template-functions.php' ); + require_once AMP__DIR__ . '/includes/amp-post-template-actions.php'; + require_once AMP__DIR__ . '/includes/amp-post-template-functions.php'; + amp_post_template_init_hooks(); } function amp_prepare_render() { diff --git a/includes/actions/class-amp-actions.php b/includes/actions/class-amp-actions.php deleted file mode 100644 index 4a95034e66d..00000000000 --- a/includes/actions/class-amp-actions.php +++ /dev/null @@ -1,20 +0,0 @@ -', esc_url( $amp_url ) ); - } -} diff --git a/includes/actions/class-amp-paired-post-actions.php b/includes/actions/class-amp-paired-post-actions.php deleted file mode 100644 index a544ac878f6..00000000000 --- a/includes/actions/class-amp-paired-post-actions.php +++ /dev/null @@ -1,178 +0,0 @@ - - <?php echo esc_html( $amp_template->get( 'document_title' ) ); ?> - - - get( 'amp_component_scripts', array() ); - foreach ( $scripts as $element => $script ) : - $custom_type = ( 'amp-mustache' === $element ) ? 'template' : 'element'; - ?> - - - - get( 'font_urls', array() ); - ?> - $url ) : ?> - - - - - get( 'metadata' ); - if ( empty( $metadata ) ) { - return; - } - ?> - - get( 'post_amp_styles' ); - if ( ! empty( $styles ) ) { - echo '/* Inline styles */' . PHP_EOL; // WPCS: XSS OK. - foreach ( $styles as $selector => $declarations ) { - $declarations = implode( ';', $declarations ) . ';'; - printf( '%1$s{%2$s}', $selector, $declarations ); // WPCS: XSS OK. - } - } - } - - /** - * Add analytics scripts. - * - * @param array $data Data. - * @return array Data. - */ - public static function add_analytics_scripts( $data ) { - if ( ! empty( $data['amp_analytics'] ) ) { - $data['amp_component_scripts']['amp-analytics'] = 'https://cdn.ampproject.org/v0/amp-analytics-0.1.js'; - } - return $data; - } - - /** - * Print analytics data. - * - * @param AMP_Post_Template $amp_template Template. - */ - public static function add_analytics_data( $amp_template ) { - $analytics_entries = $amp_template->get( 'amp_analytics' ); - if ( empty( $analytics_entries ) ) { - return; - } - - foreach ( $analytics_entries as $id => $analytics_entry ) { - if ( ! isset( $analytics_entry['type'], $analytics_entry['attributes'], $analytics_entry['config_data'] ) ) { - /* translators: %1$s is analytics entry ID, %2$s is actual entry keys. */ - _doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'Analytics entry for %1$s is missing one of the following keys: `type`, `attributes`, or `config_data` (array keys: %2$s)', 'amp' ), esc_html( $id ), esc_html( implode( ', ', array_keys( $analytics_entry ) ) ) ), '0.3.2' ); - continue; - } - - $script_element = AMP_HTML_Utils::build_tag( 'script', array( - 'type' => 'application/json', - ), wp_json_encode( $analytics_entry['config_data'] ) ); - - $amp_analytics_attr = array_merge( array( - 'id' => $id, - 'type' => $analytics_entry['type'], - ), $analytics_entry['attributes'] ); - - echo AMP_HTML_Utils::build_tag( 'amp-analytics', $amp_analytics_attr, $script_element ); // WPCS: XSS OK. - } - } - - /** - * Print AMP generator metadata. - * - * @param AMP_Post_Template $amp_template AMP_Post_Template object. - * @since 0.6 - */ - public static function add_generator_metadata( $amp_template ) { - ?> - - ', esc_url( $amp_url ) ); +} diff --git a/includes/amp-post-template-actions.php b/includes/amp-post-template-actions.php new file mode 100644 index 00000000000..711e5bbe023 --- /dev/null +++ b/includes/amp-post-template-actions.php @@ -0,0 +1,170 @@ + + <?php echo esc_html( $amp_template->get( 'document_title' ) ); ?> + + + get( 'amp_component_scripts', array() ); + foreach ( $scripts as $element => $script ) { + $custom_type = ( 'amp-mustache' === $element ) ? 'template' : 'element'; + printf( + '', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript + esc_attr( $custom_type ), + esc_attr( $element ), + esc_url( $script ) + ); + } + printf( + '', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript + esc_url( $amp_template->get( 'amp_runtime_script' ) ) + ); +} + +/** + * Print fonts. + * + * @param AMP_Post_Template $amp_template Template. + */ +function amp_post_template_add_fonts( $amp_template ) { + $font_urls = $amp_template->get( 'font_urls', array() ); + foreach ( $font_urls as $slug => $url ) { + printf( '', esc_url( esc_url( $url ) ) ); // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet + } +} + +/** + * Print boilerplate CSS. + */ +function amp_post_template_add_boilerplate_css() { + ?> + + get( 'metadata' ); + if ( empty( $metadata ) ) { + return; + } + ?> + + get( 'post_amp_styles' ); + if ( ! empty( $styles ) ) { + echo '/* Inline styles */' . PHP_EOL; // WPCS: XSS OK. + foreach ( $styles as $selector => $declarations ) { + $declarations = implode( ';', $declarations ) . ';'; + printf( '%1$s{%2$s}', $selector, $declarations ); // WPCS: XSS OK. + } + } +} + +/** + * Add analytics scripts. + * + * @param array $data Data. + * @return array Data. + */ +function amp_post_template_add_analytics_script( $data ) { + if ( ! empty( $data['amp_analytics'] ) ) { + $data['amp_component_scripts']['amp-analytics'] = 'https://cdn.ampproject.org/v0/amp-analytics-0.1.js'; + } + return $data; +} + +/** + * Print analytics data. + * + * @param AMP_Post_Template $amp_template Template. + */ +function amp_post_template_add_analytics_data( $amp_template ) { + $analytics_entries = $amp_template->get( 'amp_analytics' ); + if ( empty( $analytics_entries ) ) { + return; + } + + foreach ( $analytics_entries as $id => $analytics_entry ) { + if ( ! isset( $analytics_entry['type'], $analytics_entry['attributes'], $analytics_entry['config_data'] ) ) { + /* translators: %1$s is analytics entry ID, %2$s is actual entry keys. */ + _doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'Analytics entry for %1$s is missing one of the following keys: `type`, `attributes`, or `config_data` (array keys: %2$s)', 'amp' ), esc_html( $id ), esc_html( implode( ', ', array_keys( $analytics_entry ) ) ) ), '0.3.2' ); + continue; + } + $script_element = AMP_HTML_Utils::build_tag( 'script', array( + 'type' => 'application/json', + ), wp_json_encode( $analytics_entry['config_data'] ) ); + + $amp_analytics_attr = array_merge( array( + 'id' => $id, + 'type' => $analytics_entry['type'], + ), $analytics_entry['attributes'] ); + + echo AMP_HTML_Utils::build_tag( 'amp-analytics', $amp_analytics_attr, $script_element ); // WPCS: XSS OK. + } +} + +/** + * Add generator metadata. + * + * @since 6.0 + */ +function amp_add_generator_metadata() { + printf( '', esc_attr( 'AMP Plugin v' . AMP__VERSION ) ); +} diff --git a/includes/class-amp-autoloader.php b/includes/class-amp-autoloader.php index 082988c34a4..281126786f8 100644 --- a/includes/class-amp-autoloader.php +++ b/includes/class-amp-autoloader.php @@ -29,9 +29,6 @@ class AMP_Autoloader { * @var string[] */ private static $_classmap = array( - 'AMP_Actions' => 'includes/actions/class-amp-actions', - 'AMP_Frontend_Actions' => 'includes/actions/class-amp-frontend-actions', - 'AMP_Paired_Post_Actions' => 'includes/actions/class-amp-paired-post-actions', 'AMP_Template_Customizer' => 'includes/admin/class-amp-customizer', 'AMP_Post_Meta_Box' => 'includes/admin/class-amp-post-meta-box', 'AMP_Post_Type_Support' => 'includes/class-amp-post-type-support', diff --git a/includes/templates/class-amp-post-template.php b/includes/templates/class-amp-post-template.php index 2f88c6545f4..30624a746db 100644 --- a/includes/templates/class-amp-post-template.php +++ b/includes/templates/class-amp-post-template.php @@ -109,7 +109,6 @@ public function __construct( $post ) { 'canonical_url' => get_permalink( $this->ID ), 'home_url' => home_url(), 'blog_name' => get_bloginfo( 'name' ), - 'generator_metadata' => 'AMP Plugin v' . AMP__VERSION, 'html_tag_attributes' => array(), 'body_class' => '', From 624cdb63356cc023a46b5dee6ecf74038b55f5e7 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 12 Jan 2018 18:21:52 -0800 Subject: [PATCH 10/16] Redirect from AMP endpoint to canonical when AMP is canonical --- includes/class-amp-theme-support.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 3907151c803..edd2289d4c1 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -43,7 +43,15 @@ class AMP_Theme_Support { * Initialize. */ public static function init() { - if ( ! amp_is_canonical() ) { + if ( amp_is_canonical() ) { + $is_amp_endpoint = ( false !== get_query_var( AMP_QUERY_VAR, false ) ); // Because is_amp_endpoint() now returns true if amp_is_canonical(). + + // Permanently redirect to canonical URL if the AMP URL was loaded, since canonical is now AMP. + if ( $is_amp_endpoint ) { + wp_safe_redirect( self::get_current_canonical_url(), 301 ); + exit; + } + } else { self::register_paired_hooks(); } self::register_hooks(); From 2af460e6237dc6837384489b08abbb277ffcf4fd Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 12 Jan 2018 18:22:30 -0800 Subject: [PATCH 11/16] Re-use amp_add_generator_metadata for AMP theme support --- includes/class-amp-theme-support.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index edd2289d4c1..b8a3b57b25d 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -43,6 +43,7 @@ class AMP_Theme_Support { * Initialize. */ public static function init() { + require_once AMP__DIR__ . '/includes/amp-post-template-actions.php'; if ( amp_is_canonical() ) { $is_amp_endpoint = ( false !== get_query_var( AMP_QUERY_VAR, false ) ); // Because is_amp_endpoint() now returns true if amp_is_canonical(). @@ -118,7 +119,7 @@ public static function register_hooks() { add_action( 'wp_head', 'amp_print_boilerplate_code', 3 ); add_action( 'wp_head', array( __CLASS__, 'add_scripts' ), 4 ); add_action( 'wp_head', array( __CLASS__, 'add_styles' ), 5 ); - add_action( 'wp_head', array( __CLASS__, 'add_meta_generator' ), 6 ); + add_action( 'wp_head', 'amp_add_generator_metadata', 6 ); /* * Disable admin bar because admin-bar.css (28K) and Dashicons (48K) alone @@ -293,13 +294,6 @@ public static function add_styles() { echo ''; } - /** - * Print AMP meta generator tag. - */ - public static function add_meta_generator() { - printf( '', esc_attr( 'AMP Plugin v' . AMP__VERSION ) ); - } - /** * Determine required AMP scripts. * From e5b5490fcf62056d6ca0cfa13b6bffd156d6e098 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 12 Jan 2018 18:26:57 -0800 Subject: [PATCH 12/16] Prevent showing amphtml link if theme supports AMP but paired mode is not available --- includes/amp-frontend-actions.php | 5 +++++ includes/class-amp-theme-support.php | 2 -- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/includes/amp-frontend-actions.php b/includes/amp-frontend-actions.php index 76a6af178fe..8252bdb5700 100644 --- a/includes/amp-frontend-actions.php +++ b/includes/amp-frontend-actions.php @@ -14,6 +14,11 @@ */ 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. * diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index b8a3b57b25d..f5d07046c17 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -65,8 +65,6 @@ public static function init() { * Returns true when there is a template_path defined in theme support, and if a defined active_callback * returns true. * - * @todo Make sure this is used when determining whether to show the amphtml link. - * * @return bool Whether available. */ public static function is_paired_available() { From 6285f9f1a219c7158f1e31b089d72afaf829979d Mon Sep 17 00:00:00 2001 From: Kaitlyn Date: Mon, 15 Jan 2018 15:47:48 -0400 Subject: [PATCH 13/16] Update name template_path to template_dir when it comes to custom paired mode templates to match AMP_Post_Template naming. --- amp.php | 4 ++-- includes/class-amp-theme-support.php | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/amp.php b/amp.php index 7b0e2ef3a9c..c3b48806efd 100644 --- a/amp.php +++ b/amp.php @@ -172,7 +172,7 @@ function amp_maybe_add_actions() { * Themes can register support for this with `add_theme_support( 'amp' )`. * Then, this will change the plugin from 'paired mode,' and it won't use its own templates. * Nor output frontend markup like the 'rel' link. If the theme registers support for AMP with: - * `add_theme_support( 'amp', array( 'template_path' => get_template_directory() . 'my-amp-templates/' ) )` + * `add_theme_support( 'amp', array( 'template_dir' => 'my-amp-templates' ) )` * it will retain 'paired mode. * * @return boolean Whether this is in AMP 'canonical mode'. @@ -184,7 +184,7 @@ function amp_is_canonical() { } if ( is_array( $support ) ) { $args = array_shift( $support ); - if ( empty( $args['template_path'] ) ) { + if ( empty( $args['template_dir'] ) ) { return true; } } diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index f5d07046c17..1f27f13147b 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -62,7 +62,7 @@ public static function init() { * Determines whether paired mode is available. * * When 'amp' theme support has not been added or canonical mode is enabled, then this returns false. - * Returns true when there is a template_path defined in theme support, and if a defined active_callback + * Returns true when there is a template_dir defined in theme support, and if a defined active_callback * returns true. * * @return bool Whether available. @@ -133,7 +133,7 @@ public static function register_hooks() { } /** - * Prepends template hierarchy with template_path for AMP paired mode templates. + * Prepends template hierarchy with template_dir for AMP paired mode templates. * * @see get_query_template() * @@ -143,10 +143,10 @@ public static function register_hooks() { public static function filter_paired_template_hierarchy( $templates ) { $support = get_theme_support( 'amp' ); $args = array_shift( $support ); - if ( isset( $args['template_path'] ) ) { + if ( isset( $args['template_dir'] ) ) { $amp_templates = array(); foreach ( $templates as $template ) { - $amp_templates[] = $args['template_path'] . '/' . $template; + $amp_templates[] = $args['template_dir'] . '/' . $template; } $templates = $amp_templates; } @@ -156,7 +156,7 @@ public static function filter_paired_template_hierarchy( $templates ) { /** * Redirect to the non-canonical URL when the template to include is empty. * - * This is a failsafe in case an index.php is not located in the AMP template_path, + * This is a failsafe in case an index.php is not located in the AMP template_dir, * and the active_callback fails to omit a given request from being available in AMP. * * @param string $template Template to include. From b10c0fee9ecaf879b2a677f3d9ed1606391e91c2 Mon Sep 17 00:00:00 2001 From: Kaitlyn Date: Mon, 15 Jan 2018 15:52:13 -0400 Subject: [PATCH 14/16] Address callback param name. --- includes/class-amp-theme-support.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 1f27f13147b..6592e5aa04f 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -62,7 +62,7 @@ public static function init() { * Determines whether paired mode is available. * * When 'amp' theme support has not been added or canonical mode is enabled, then this returns false. - * Returns true when there is a template_dir defined in theme support, and if a defined active_callback + * Returns true when there is a template_dir defined in theme support, and if a defined available_callback * returns true. * * @return bool Whether available. @@ -75,9 +75,8 @@ public static function is_paired_available() { $args = array_shift( $support ); - // @todo We might want to rename active_callback to available_callback.. - if ( isset( $args['active_callback'] ) && is_callable( $args['active_callback'] ) ) { - return $args['active_callback'](); + if ( isset( $args['available_callback'] ) && is_callable( $args['available_callback'] ) ) { + return $args['available_callback'](); } return true; } @@ -157,7 +156,7 @@ public static function filter_paired_template_hierarchy( $templates ) { * Redirect to the non-canonical URL when the template to include is empty. * * This is a failsafe in case an index.php is not located in the AMP template_dir, - * and the active_callback fails to omit a given request from being available in AMP. + * and the available_callback fails to omit a given request from being available in AMP. * * @param string $template Template to include. * @return string Template to include. From 9b1828dbdbd8bf83db734c6c7ae30edadf1337ee Mon Sep 17 00:00:00 2001 From: Kaitlyn Date: Mon, 15 Jan 2018 16:00:10 -0400 Subject: [PATCH 15/16] Update test_amp_is_canonical() with new naming. --- tests/test-amp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-amp.php b/tests/test-amp.php index 61515354745..19b68d9ad26 100644 --- a/tests/test-amp.php +++ b/tests/test-amp.php @@ -32,7 +32,7 @@ public function test_amp_is_canonical() { remove_theme_support( 'amp' ); add_theme_support( 'amp', array( - 'template_path' => get_template_directory() . 'amp-templates/', + 'template_dir' => 'amp-templates', ) ); $this->assertFalse( amp_is_canonical() ); From 34c18ccc39d8971f8c98f577b93dc7ee04192ba1 Mon Sep 17 00:00:00 2001 From: Kaitlyn Date: Mon, 15 Jan 2018 17:48:58 -0400 Subject: [PATCH 16/16] Cleanup: removing unnecessary variables, add PHPDoc for new const, readability improvements. --- amp.php | 5 ++--- includes/amp-frontend-actions.php | 3 +-- includes/class-amp-theme-support.php | 10 +++++++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/amp.php b/amp.php index c3b48806efd..0b6001e75ca 100644 --- a/amp.php +++ b/amp.php @@ -127,10 +127,11 @@ function amp_force_query_var_value( $query_vars ) { * @return void */ function amp_maybe_add_actions() { + $is_amp_endpoint = is_amp_endpoint(); // Add hooks for when a themes that support AMP. if ( current_theme_supports( 'amp' ) ) { - if ( amp_is_canonical() || is_amp_endpoint() ) { + if ( $is_amp_endpoint ) { AMP_Theme_Support::init(); } else { amp_add_frontend_actions(); @@ -143,8 +144,6 @@ function amp_maybe_add_actions() { return; } - $is_amp_endpoint = is_amp_endpoint(); - // Cannot use `get_queried_object` before canonical redirect; see . global $wp_query; $post = $wp_query->post; diff --git a/includes/amp-frontend-actions.php b/includes/amp-frontend-actions.php index 8252bdb5700..f62b93f6b70 100644 --- a/includes/amp-frontend-actions.php +++ b/includes/amp-frontend-actions.php @@ -28,6 +28,5 @@ function amp_frontend_add_canonical() { return; } - $amp_url = amp_get_permalink( get_queried_object_id() ); - printf( '', esc_url( $amp_url ) ); + printf( '', esc_url( amp_get_permalink( get_queried_object_id() ) ) ); } diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 6592e5aa04f..889e7e91f8d 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -12,6 +12,11 @@ */ class AMP_Theme_Support { + /** + * Replaced with the necessary scripts depending on components used in output. + * + * @var string + */ const COMPONENT_SCRIPTS_PLACEHOLDER = ''; /** @@ -45,10 +50,9 @@ class AMP_Theme_Support { public static function init() { require_once AMP__DIR__ . '/includes/amp-post-template-actions.php'; if ( amp_is_canonical() ) { - $is_amp_endpoint = ( false !== get_query_var( AMP_QUERY_VAR, false ) ); // Because is_amp_endpoint() now returns true if amp_is_canonical(). // Permanently redirect to canonical URL if the AMP URL was loaded, since canonical is now AMP. - if ( $is_amp_endpoint ) { + if ( false !== get_query_var( AMP_QUERY_VAR, false ) ) { // Because is_amp_endpoint() now returns true if amp_is_canonical(). wp_safe_redirect( self::get_current_canonical_url(), 301 ); exit; } @@ -76,7 +80,7 @@ public static function is_paired_available() { $args = array_shift( $support ); if ( isset( $args['available_callback'] ) && is_callable( $args['available_callback'] ) ) { - return $args['available_callback'](); + return call_user_func( $args['available_callback'] ); } return true; }