diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index 254c264f993..f4a4491647e 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -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. + } } /** diff --git a/includes/class-amp-post-type-support.php b/includes/class-amp-post-type-support.php index 22a1bef580b..cc74157d379 100644 --- a/includes/class-amp-post-type-support.php +++ b/includes/class-amp-post-type-support.php @@ -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; } } diff --git a/includes/options/class-amp-options-manager.php b/includes/options/class-amp-options-manager.php index c41ce3acdc7..099975b8cc0 100644 --- a/includes/options/class-amp-options-manager.php +++ b/includes/options/class-amp-options-manager.php @@ -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', + ), ); /** diff --git a/includes/options/class-amp-options-menu.php b/includes/options/class-amp-options-menu.php index 2c5eb81f3fc..a953786bd78 100644 --- a/includes/options/class-amp-options-menu.php +++ b/includes/options/class-amp-options-menu.php @@ -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', ) ); @@ -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() { ?> -
+

-

@@ -302,10 +302,9 @@ public function render_supported_queries() { $element_name = AMP_Options_Manager::OPTION_NAME . '[supported_post_types][]'; ?> -

+

- name}"; @@ -331,6 +330,35 @@ public function render_supported_queries() {

+ + array( + 'front' => array(), + 'posts' => array(), + 'date' => array(), + ), + 'author' => array( + '' => __( 'All author pages', 'amp' ), + ), + 'category' => array( + '' => __( 'All category pages', 'amp' ), + ) + ); + ?> + +
+

+ +

+

+ +

+