From 0131c52b03f1a190e0963c9fab54bd33dd0706b9 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 28 Jun 2018 20:44:23 -0700 Subject: [PATCH] WIP11 [ci skip] --- amp.php | 8 +++-- includes/amp-helper-functions.php | 3 +- includes/class-amp-theme-support.php | 54 +++++----------------------- 3 files changed, 17 insertions(+), 48 deletions(-) diff --git a/amp.php b/amp.php index e4aa3a69529..413fa62248d 100644 --- a/amp.php +++ b/amp.php @@ -301,6 +301,10 @@ function amp_correct_query_when_is_front_page( WP_Query $query ) { * @return boolean Whether this is in AMP 'canonical' mode, that is whether it is native and there is not separate AMP URL current URL. */ function amp_is_canonical() { + if ( ! current_theme_supports( 'amp' ) ) { + return false; + } + $support = get_theme_support( 'amp' ); if ( true === $support ) { return true; @@ -310,8 +314,8 @@ function amp_is_canonical() { $args = array_shift( $support ); // If paired arg is supplied, then it is never canonical. - if ( ! empty( $args['paired'] ) ) { - return false; + if ( isset( $args['paired'] ) ) { + return ! $args['paired']; } // If there is a template_dir, then paired mode is implied. diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index 61a198c148c..623c3a7e40a 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -321,7 +321,8 @@ function is_amp_endpoint() { } if ( current_theme_supports( 'amp' ) ) { - return true === AMP_Theme_Support::get_template_availability(); + $template_available = true === AMP_Theme_Support::get_template_availability(); + return amp_is_canonical() ? $template_available : $has_amp_query_var; } else { // Check if the queried object supports AMP. diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 6917eef0121..0c15885cebb 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -118,7 +118,7 @@ public static function init() { $args = array_shift( $support ); if ( ! is_array( $args ) ) { trigger_error( esc_html__( 'Expected AMP theme support arg to be array.', 'amp' ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error - } elseif ( count( array_diff( array_keys( $args ), array( 'template_dir', 'available_callback', 'comments_live_list', '__added_via_option' ) ) ) !== 0 ) { + } elseif ( count( array_diff( array_keys( $args ), array( 'template_dir', 'available_callback', 'comments_live_list', 'paired', '__added_via_option' ) ) ) !== 0 ) { trigger_error( esc_html__( 'Expected AMP theme support to only have template_dir and/or available_callback.', 'amp' ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error } } @@ -147,26 +147,8 @@ public static function apply_options() { $args = array( '__added_via_option' => true, + 'paired' => ( 'paired' === $theme_support_option ), ); - if ( 'native' === $theme_support_option ) { - $args['available_callback'] = function() { - /** - * Queried object. - * - * @var WP_Post $queried_object - */ - $queried_object = get_queried_object(); - if ( is_singular() && post_supports_amp( $queried_object ) ) { - return 'native'; - } - - if ( AMP_Options_Manager::get_option( 'non_singular_supported' ) ) { - return 'native'; - } - - return false; - }; - } add_theme_support( 'amp', $args ); } } @@ -278,43 +260,27 @@ public static function redirect_ampless_url( $exit = true ) { * 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 available_callback - * returns true. + * + * @since 0.7 + * @since 1.0 This no longer looks at the available_callback bit instead calls get_template_availability. * * @see amp_is_canonical() * @return bool Whether available. */ public static function is_paired_available() { - $support = get_theme_support( 'amp' ); - if ( empty( $support ) || amp_is_canonical() ) { + if ( ! current_theme_supports( 'amp' ) ) { return false; } - /** - * Queried object. - * - * @var WP_Post $queried_object - */ - $queried_object = get_queried_object(); - if ( is_singular() && ! post_supports_amp( $queried_object ) ) { + if ( amp_is_canonical() ) { return false; } - if ( ! is_singular() && ! AMP_Options_Manager::get_option( 'non_singular_supported' ) ) { + $availability = self::get_template_availability(); + if ( is_wp_error( $availability ) ) { return false; } - $args = array_shift( $support ); - - if ( isset( $args['available_callback'] ) && is_callable( $args['available_callback'] ) ) { - /* - * The available_callback here will return a bool or the string 'paired'. - * If it returns 'native' then `amp_is_canonical()` above would have short-circuited. - */ - return (bool) call_user_func( $args['available_callback'] ); - } - - // This is the same as if there is a template_dir defined with no available_callback. return true; } @@ -432,8 +398,6 @@ public static function get_template_availability( $query = null ) { } } - error_log( json_encode( $matching_templates ) ); - // If there aren't any matching templates left that are supported, then we consider it to not be available. if ( ! in_array( true, $matching_templates, true ) ) { return new WP_Error( 'no_supported_template' );