Skip to content

Commit

Permalink
WIP11 [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jun 29, 2018
1 parent 8f46ee7 commit 0131c52
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 48 deletions.
8 changes: 6 additions & 2 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
54 changes: 9 additions & 45 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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 );
}
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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' );
Expand Down

0 comments on commit 0131c52

Please sign in to comment.