Skip to content

Commit

Permalink
WIP3
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jun 27, 2018
1 parent 6149da1 commit fab22d4
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 37 deletions.
63 changes: 47 additions & 16 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,30 +317,61 @@ function is_amp_endpoint() {
return false;
}

/*
* If theme suport is present and there is available_callback, it is what determines whether this is
* an AMP endpoint. The available_callback is is presumed to check post_supports_amp().
* @todo Should available_callback take a WP_Query as its arg? We need to be able to determine whether AMP is supported for another URL.
* @todo Should this not be a filter?
*/
// Otherwise, it is an AMP endpoint if AMP is available.
return amp_get_availability();
}

/**
* Determine availability of AMP for the given query.
*
* @since 1.0
* @global WP_Query $wp_the_query
*
* @param WP_Query|null $query Query. If null then the global query will be used.
* @return bool Whether AMP is available.
*/
function amp_get_availability( $query = null ) {
global $wp_the_query;
if ( ! $query ) {
$query = $wp_the_query;
}

if ( ! ( $query instanceof WP_Query ) ) {
_doing_it_wrong( __FUNCTION__, esc_html__( 'No WP_Query available.', 'amp' ), '1.0' );
return false;
}

$available = null;
if ( current_theme_supports( 'amp' ) ) {
$args = get_theme_support( 'amp' );
if ( isset( $args[0]['available_callback'] ) && is_callable( $args[0]['available_callback'] ) ) {
return call_user_func( $args[0]['available_callback'] );
$callback = $args[0]['available_callback'];

// If the available_callback is a method on the query, then call the method on the query itself.
if ( is_string( $callback ) && 'is_' === substr( $callback, 0, 3 ) && method_exists( $query, $callback ) ) {
$available = call_user_func( array( $query, $callback ) );
} else {
$available = call_user_func( $callback );
}
}
}

// When there is no theme support, then
if ( $did_parse_query ) {
$queried_object = get_queried_object();
return (
is_singular() && $queried_object instanceof WP_Post && post_supports_amp( $queried_object )
||
! is_singular() && AMP_Options_Manager::get_option( 'non_singular_supported' )
);
// Availability has been is programmatically determined via the available_callback.
if ( is_bool( $available ) ) {
return $available;
}

return true;
if ( $query->is_singular() ) {
/**
* Post.
*
* @var WP_Post $queried_object
*/
$queried_object = $query->get_queried_object();
return post_supports_amp( $queried_object ); // @todo FAIL: This is calling get_support_errors(), and get_support_errors() is calling amp_get_availability().
} else {
return (bool) AMP_Options_Manager::get_option( 'non_singular_supported' ); // @todo Consider other kinds of queries.
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions includes/class-amp-post-type-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ public static function get_support_errors( $post ) {
$errors[] = 'skip-post';
}

$query = new WP_Query();

$query->queried_object = $post;
$query->queried_object_id = $post->ID;
if ( 'page' === $post->post_type ) {
$query->set( 'post_id', $post->ID );
} else {
$query->set( 'p', $post->ID );
}
$query->parse_query();

$availability = amp_get_availability( $query );
if ( ! $availability ) {
$errors[] = '';
}


return $errors;
}
}
17 changes: 10 additions & 7 deletions includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ class AMP_Options_Manager {
* @var array
*/
protected static $defaults = array(
'theme_support' => 'disabled',
'supported_post_types' => array(),
'analytics' => array(),
'force_sanitization' => false,
'accept_tree_shaking' => false,
'disable_admin_bar' => false,
'non_singular_supported' => true,
'theme_support' => 'disabled',
'supported_post_types' => array(),
'analytics' => array(),
'force_sanitization' => false,
'accept_tree_shaking' => false,
'disable_admin_bar' => false,
'all_templates_supported' => false,
'supported_templates' => array(
'single',
),
);

/**
Expand Down
56 changes: 42 additions & 14 deletions includes/options/class-amp-options-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ public function add_menu_items() {
);

add_settings_field(
'supported_queries',
__( 'Supported Queries', 'amp' ),
array( $this, 'render_supported_queries' ),
'supported_templates',
__( 'Supported Templates', 'amp' ),
array( $this, 'render_supported_templates' ),
AMP_Options_Manager::OPTION_NAME,
'general',
array(
'class' => 'amp-post-type-support-field',
'class' => 'amp-template-support-field',
)
);

Expand Down Expand Up @@ -272,23 +272,23 @@ public function render_validation_handling() {
}

/**
* Post types support section renderer.
* Supported templates section renderer.
*
* @since 0.6
* @since 1.0
*/
public function render_supported_queries() {
public function render_supported_templates() {
?>
<script>
jQuery( 'input[type=radio][name="amp-options[theme_support]"]' ).change( function() {
jQuery( 'fieldset.non_singular_supported' ).toggleClass( 'hidden', 'disabled' === this.value );
jQuery( 'fieldset.non_singular_supported, fieldset.all_templates_supported' ).toggleClass( 'hidden', 'disabled' === this.value );
} ).filter( ':checked' ).trigger( 'change' );
</script>

<fieldset class="non_singular_supported">
<fieldset class="all_templates_supported">
<p>
<label for="non_singular_supported">
<input id="non_singular_supported" type="checkbox" name="<?php echo esc_attr( AMP_Options_Manager::OPTION_NAME . '[non_singular_supported]' ); ?>" <?php checked( AMP_Options_Manager::get_option( 'non_singular_supported' ) ); ?>>
<?php esc_html_e( 'Serve non-singular templates as AMP.', 'amp' ); ?>
<label for="all_templates_supported">
<input id="all_templates_supported" type="checkbox" name="<?php echo esc_attr( AMP_Options_Manager::OPTION_NAME . '[all_templates_supported]' ); ?>" <?php checked( AMP_Options_Manager::get_option( 'all_templates_supported' ) ); ?>>
<?php esc_html_e( 'Serve all templates as AMP regardless of what is being queried.', 'amp' ); ?>
</label>
</p>
<p class="description">
Expand All @@ -302,10 +302,9 @@ public function render_supported_queries() {
$element_name = AMP_Options_Manager::OPTION_NAME . '[supported_post_types][]';
?>
<legend>
<h2 class="title"><?php esc_html_e( 'Post Types', 'amp' ); ?></h2>
<h2 class="title"><?php esc_html_e( 'Single Templates', 'amp' ); ?></h2>
</legend>

<!-- TODO Checkbox to allow other queries -->
<?php foreach ( array_map( 'get_post_type_object', AMP_Post_Type_Support::get_eligible_post_types() ) as $post_type ) : ?>
<?php
$element_id = AMP_Options_Manager::OPTION_NAME . "-supported_post_types-{$post_type->name}";
Expand All @@ -331,6 +330,35 @@ public function render_supported_queries() {
<?php esc_html_e( 'Select the content types that you would like to be made available in AMP.', 'amp' ); ?>
</p>
</fieldset>

<?php
// This is inspired by Jetpack's Widget Conditions.
$templates = array(
'page' => array(
'front' => array(),
'posts' => array(),
'date' => array(),
),
'author' => array(
'' => __( 'All author pages', 'amp' ),
),
'category' => array(
'' => __( 'All category pages', 'amp' ),
)
);
?>

<fieldset class="non_singular_supported">
<p>
<label for="non_singular_supported">
<input id="non_singular_supported" type="checkbox" name="<?php echo esc_attr( AMP_Options_Manager::OPTION_NAME . '[non_singular_supported]' ); ?>" <?php checked( AMP_Options_Manager::get_option( 'non_singular_supported' ) ); ?>>
<?php esc_html_e( 'Serve non-singular templates as AMP.', 'amp' ); ?>
</label>
</p>
<p class="description">
<?php esc_html_e( 'Non-singular means templates like categories, date archives, author pages, and so on.', 'amp' ); ?>
</p>
</fieldset>
<?php
}

Expand Down

0 comments on commit fab22d4

Please sign in to comment.