Skip to content

Commit

Permalink
WIP10 [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jun 29, 2018
1 parent f440592 commit 8f46ee7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 35 deletions.
1 change: 1 addition & 0 deletions includes/admin/class-amp-post-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public function render_status( $post ) {
is_post_type_viewable( $post->post_type )
&&
current_user_can( 'edit_post', $post->ID )
// @todo What if a site does not have non-AMP pages? Formerly this was indicated via amp_is_canonical(). Now we need a flag for whether AMP is exclusive to a theme, in which case all templates and post types must render AMP. In other words, all_templates_supported.
);

if ( true !== $verify ) {
Expand Down
25 changes: 12 additions & 13 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,22 +320,21 @@ function is_amp_endpoint() {
return false;
}

// Check if the queried object supports AMP.
if ( is_singular() ) {
/**
* Post.
*
* @var WP_Post $queried_object
*/
$queried_object = get_queried_object();
if ( ! post_supports_amp( $queried_object ) ) {
return false;
}
}

if ( current_theme_supports( 'amp' ) ) {
return true === AMP_Theme_Support::get_template_availability();
} else {

// Check if the queried object supports AMP.
if ( is_singular() ) {
/**
* Post.
*
* @var WP_Post $queried_object
*/
$queried_object = get_queried_object();
return post_supports_amp( $queried_object );
}

return false; // Legacy templates only support posts via AMP_Post_Template.
}
}
Expand Down
52 changes: 32 additions & 20 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,25 +366,21 @@ public static function get_template_availability( $query = null ) {
return new WP_Error( 'no_theme_support' );
}

// @todo Add theme support flag
$matching_templates = array();
$supportable_templates = self::get_supportable_templates();

if ( $query->is_singular() ) {
// return true;
/*
* Singular template support is included here though it is not explicitly listed among the templates by default.
* It is the post type support that is used to indicate whether a given singular post template should be served
* via post_supports_amp() including the metabox toggle to enable/disable AMP, and the amp_skip_post filter.
*/
if ( ! isset( $supportable_templates['is_singular'] ) ) {
$supportable_templates['is_singular'] = array(
'callback' => 'is_singular',
'supported' => true,
);
}

// Singular queries
// if ( $query->is_singular() ) {
// /**
// * Queried object.
// *
// * @var WP_Post $queried_object
// */
// $queried_object = $query->get_queried_object();
// return post_supports_amp( $queried_object )
// }

$matching_templates = array();
$supportable_templates = self::get_supportable_templates();
foreach ( $supportable_templates as $id => $supportable_template ) {
if ( empty( $supportable_template['callback'] ) ) {
$callback = $id;
Expand Down Expand Up @@ -436,18 +432,34 @@ 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' );
}

// For singular queries, post_supports_amp() is given the final say.
if ( $query->is_singular() ) {
/**
* Queried object.
*
* @var WP_Post $queried_object
*/
$queried_object = $query->get_queried_object();
if ( ! post_supports_amp( $queried_object ) ) {
return new WP_Error( 'post_lacks_amp_support' );
}
}

// If all checks have passed, then the template is available.
return true;
}

/**
* Get the templates which can be supported.
*
* @todo The callback could have support for running in an admin context.
* @return array Supportable templates.
*/
public static function get_supportable_templates() {
Expand All @@ -466,10 +478,10 @@ public static function get_supportable_templates() {
$templates,
array(
'is_archive' => array(
'label' => __( 'Archives', 'amp' ),
'label' => __( 'Archives', 'amp' ),
),
'is_author' => array(
'label' => __( 'Author', 'amp' ),
'label' => __( 'Author', 'amp' ),
'parent' => 'is_archive',
),
'is_date' => array(
Expand All @@ -491,8 +503,8 @@ public static function get_supportable_templates() {

if ( taxonomy_exists( 'category' ) ) {
$templates['is_category'] = array(
'label' => get_taxonomy( 'category' )->labels->name,
'parent' => 'is_archive',
'label' => get_taxonomy( 'category' )->labels->name,
'parent' => 'is_archive',
);
}
if ( taxonomy_exists( 'post_tag' ) ) {
Expand Down
4 changes: 2 additions & 2 deletions includes/options/class-amp-options-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public function render_supported_templates() {

<fieldset id="singular_templates_fieldset">
<?php $element_name = AMP_Options_Manager::OPTION_NAME . '[supported_post_types][]'; ?>
<h4 class="title"><?php esc_html_e( 'Singular Templates', 'amp' ); ?></h4>
<h4 class="title"><?php esc_html_e( 'Content Types', 'amp' ); ?></h4>
<p>
<?php esc_html_e( 'The following content types will be available as AMP by default, but you can override this on an item-by-item basis:', 'amp' ); ?>
</p>
Expand Down Expand Up @@ -326,7 +326,7 @@ public function render_supported_templates() {
margin-left: 40px;
}
</style>
<h4 class="title"><?php esc_html_e( 'Non-Singular Templates', 'amp' ); ?></h4>
<h4 class="title"><?php esc_html_e( 'Templates', 'amp' ); ?></h4>
<?php
self::list_template_conditional_options( AMP_Theme_Support::get_supportable_templates() );
?>
Expand Down

0 comments on commit 8f46ee7

Please sign in to comment.