Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jun 27, 2018
1 parent 09dbb8a commit 143cd2a
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 45 deletions.
50 changes: 42 additions & 8 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,36 @@ function amp_correct_query_when_is_front_page( WP_Query $query ) {
}

/**
* Whether this is in 'canonical mode.'
* Whether this is in 'canonical mode'.
*
* Themes can register support for this with `add_theme_support( 'amp' )`.
* Then, this will change the plugin from 'paired mode,' and it won't use its own templates.
* Nor output frontend markup like the 'rel' link. If the theme registers support for AMP with:
* `add_theme_support( 'amp', array( 'template_dir' => 'my-amp-templates' ) )`
* it will retain 'paired mode.
* Themes can register support for this with `add_theme_support( 'amp' )`, or via the
* following so that only so that single blog posts will be native/canonical, pages are paired,
* and everything else has AMP unavailable:
*
* @return boolean Whether this is in AMP 'canonical mode'.
* add_theme_support( 'amp', array(
* 'template_dir' => 'amp-templates/', // Optional. In case you need to override the template as a whole.
* 'available_callback' => function() {
* // @todo Warning: If a plugin or theme calls is_amp_endpoint() before parse_query() then the conditionals will not work!
* if ( is_single() ) {
* return 'native';
* } elseif ( is_page() ) {
* return 'paired'; // Or 'true'.
* } else {
* return false;
* }
* },
* ) );
*
* Then, this will change the plugin so that it won't run in 'paired mode' with separate URLs.
* Neither will it output the rel=amphtml link on the frontend.
*
* Paired mode will be retained if the theme registers support for AMP with just a template_dir and no available_callback:
*
* add_theme_support( 'amp', array(
* 'template_dir' => 'my-amp-templates',
* ) );
*
* @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() {
$support = get_theme_support( 'amp' );
Expand All @@ -289,9 +310,22 @@ function amp_is_canonical() {
}
if ( is_array( $support ) ) {
$args = array_shift( $support );
if ( empty( $args['template_dir'] ) ) {

$is_native = (
isset( $args['available_callback'] )
&&
is_callable( $args['available_callback'] )
&&
'native' === call_user_func( $args['available_callback'] )
);
if ( $is_native ) {
return true;
}

// If there is no available_callback and yet there is a template_dir, then paired mode is implied.
if ( empty( $args['available_callback'] ) && ! empty( $args['template_dir'] ) ) {
return false;
}
}
return false;
}
Expand Down
49 changes: 36 additions & 13 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AMP_Theme_Support {
*
* @var string
*/
const RESPONSE_CACHE_GROUP = 'amp-reponse';
const RESPONSE_CACHE_GROUP = 'amp-response';

/**
* Sanitizer classes.
Expand Down Expand Up @@ -148,8 +148,24 @@ public static function apply_options() {
$args = array(
'__added_via_option' => true,
);
if ( 'paired' === $theme_support_option ) {
$args['template_dir'] = './';
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 All @@ -168,8 +184,9 @@ public static function finish_init() {

self::ensure_proper_amp_location();

if ( ! amp_is_canonical() ) {
self::register_paired_hooks();
$theme_support = get_theme_support( 'amp' );
if ( ! empty( $theme_support[0]['template_dir'] ) ) {
self::add_amp_template_filters();
}

self::add_hooks();
Expand Down Expand Up @@ -285,8 +302,14 @@ public static function is_paired_available() {
$args = array_shift( $support );

if ( isset( $args['available_callback'] ) && is_callable( $args['available_callback'] ) ) {
return call_user_func( $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 All @@ -303,13 +326,13 @@ public static function is_customize_preview_iframe() {
}

/**
* Register hooks for paired mode.
* Register filters for loading AMP-specific templates.
*/
public static function register_paired_hooks() {
public static function add_amp_template_filters() {
foreach ( self::$template_types as $template_type ) {
add_filter( "{$template_type}_template_hierarchy", array( __CLASS__, 'filter_paired_template_hierarchy' ) );
add_filter( "{$template_type}_template_hierarchy", array( __CLASS__, 'filter_amp_template_hierarchy' ) );
}
add_filter( 'template_include', array( __CLASS__, 'filter_paired_template_include' ), 100 );
add_filter( 'template_include', array( __CLASS__, 'filter_amp_template_include' ), 100 );
}

/**
Expand Down Expand Up @@ -727,7 +750,7 @@ public static function amend_comment_form() {
* @param array $templates Template hierarchy.
* @return array Templates.
*/
public static function filter_paired_template_hierarchy( $templates ) {
public static function filter_amp_template_hierarchy( $templates ) {
$support = get_theme_support( 'amp' );
$args = array_shift( $support );
if ( isset( $args['template_dir'] ) ) {
Expand All @@ -749,8 +772,8 @@ public static function filter_paired_template_hierarchy( $templates ) {
* @param string $template Template to include.
* @return string Template to include.
*/
public static function filter_paired_template_include( $template ) {
if ( empty( $template ) || ! self::is_paired_available() ) {
public static function filter_amp_template_include( $template ) {
if ( empty( $template ) ) {
wp_safe_redirect( self::get_current_canonical_url(), 302 ); // Temporary redirect because support may come later.
exit;
}
Expand Down
20 changes: 11 additions & 9 deletions includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ 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,
'theme_support' => 'disabled',
'supported_post_types' => array(),
'analytics' => array(),
'force_sanitization' => false,
'accept_tree_shaking' => false,
'disable_admin_bar' => false,
'non_singular_supported' => true,
);

/**
Expand Down Expand Up @@ -116,9 +117,10 @@ public static function validate_options( $new_options ) {
$options['theme_support'] = $new_options['theme_support'];
}

$options['force_sanitization'] = ! empty( $new_options['force_sanitization'] );
$options['accept_tree_shaking'] = ! empty( $new_options['accept_tree_shaking'] );
$options['disable_admin_bar'] = ! empty( $new_options['disable_admin_bar'] );
$options['force_sanitization'] = ! empty( $new_options['force_sanitization'] );
$options['accept_tree_shaking'] = ! empty( $new_options['accept_tree_shaking'] );
$options['disable_admin_bar'] = ! empty( $new_options['disable_admin_bar'] );
$options['non_singular_supported'] = ! empty( $new_options['non_singular_supported'] );

// Validate post type support.
if ( isset( $new_options['supported_post_types'] ) ) {
Expand Down
36 changes: 28 additions & 8 deletions includes/options/class-amp-options-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public function add_menu_items() {
);

add_settings_field(
'supported_post_types',
__( 'Post Type Support', 'amp' ),
array( $this, 'render_post_types_support' ),
'supported_queries',
__( 'Supported Queries', 'amp' ),
array( $this, 'render_supported_queries' ),
AMP_Options_Manager::OPTION_NAME,
'general',
array(
Expand Down Expand Up @@ -276,16 +276,36 @@ public function render_validation_handling() {
*
* @since 0.6
*/
public function render_post_types_support() {
$builtin_support = AMP_Post_Type_Support::get_builtin_supported_post_types();
$element_name = AMP_Options_Manager::OPTION_NAME . '[supported_post_types][]';
public function render_supported_queries() {
?>
<script>
jQuery( 'input[type=radio][name="amp-options[theme_support]"]' ).change( function() {
jQuery( '.amp-post-type-support-field' ).toggleClass( 'hidden', 'paired' !== this.value && 'disabled' !== this.value );
jQuery( 'fieldset.non_singular_supported' ).toggleClass( 'hidden', 'disabled' === this.value );
} ).filter( ':checked' ).trigger( 'change' );
</script>

<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>

<fieldset>
<?php
$builtin_support = AMP_Post_Type_Support::get_builtin_supported_post_types();
$element_name = AMP_Options_Manager::OPTION_NAME . '[supported_post_types][]';
?>
<legend>
<h2 class="title"><?php esc_html_e( 'Post Types', '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 @@ -299,7 +319,7 @@ public function render_post_types_support() {
id="<?php echo esc_attr( $element_id ); ?>"
name="<?php echo esc_attr( $element_name ); ?>"
value="<?php echo esc_attr( $post_type->name ); ?>"
<?php checked( true, amp_is_canonical() || post_type_supports( $post_type->name, amp_get_slug() ) ); ?>
<?php checked( true, post_type_supports( $post_type->name, amp_get_slug() ) ); ?>
<?php disabled( $is_builtin ); ?>
>
<label for="<?php echo esc_attr( $element_id ); ?>">
Expand Down
14 changes: 7 additions & 7 deletions tests/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public function test_is_customize_preview_iframe() {
/**
* Test register_paired_hooks.
*
* @covers AMP_Theme_Support::register_paired_hooks()
* @covers AMP_Theme_Support::add_amp_template_filters()
*/
public function test_register_paired_hooks() {
$template_types = array(
Expand All @@ -309,7 +309,7 @@ public function test_register_paired_hooks() {
'author',
'category',
);
AMP_Theme_Support::register_paired_hooks();
AMP_Theme_Support::add_amp_template_filters();
foreach ( $template_types as $template_type ) {
$this->assertEquals( 10, has_filter( "{$template_type}_template_hierarchy", array( self::TESTED_CLASS, 'filter_paired_template_hierarchy' ) ) );
}
Expand Down Expand Up @@ -761,7 +761,7 @@ public function test_amend_comment_form() {
/**
* Test filter_paired_template_hierarchy.
*
* @covers AMP_Theme_Support::filter_paired_template_hierarchy()
* @covers AMP_Theme_Support::filter_amp_template_hierarchy()
*/
public function test_filter_paired_template_hierarchy() {
$template_dir = 'amp-templates';
Expand All @@ -773,7 +773,7 @@ public function test_filter_paired_template_hierarchy() {
'single-post.php',
'single.php',
);
$filtered_templates = AMP_Theme_Support::filter_paired_template_hierarchy( $templates );
$filtered_templates = AMP_Theme_Support::filter_amp_template_hierarchy( $templates );
foreach ( $filtered_templates as $key => $filtered_template ) {
$this->assertEquals( $template_dir . '/' . $templates[ $key ], $filtered_template );
}
Expand All @@ -782,18 +782,18 @@ public function test_filter_paired_template_hierarchy() {
/**
* Test filter_paired_template_include.
*
* @covers AMP_Theme_Support::filter_paired_template_include()
* @covers AMP_Theme_Support::filter_amp_template_include()
*/
public function test_filter_paired_template_include() {
$template_dir = 'amp-templates';
$template = 'single.php';
add_theme_support( 'amp', array(
'template_dir' => $template_dir,
) );
$this->assertEquals( $template, AMP_Theme_Support::filter_paired_template_include( $template ) );
$this->assertEquals( $template, AMP_Theme_Support::filter_amp_template_include( $template ) );
remove_theme_support( 'amp' );
try {
AMP_Theme_Support::filter_paired_template_include( $template );
AMP_Theme_Support::filter_amp_template_include( $template );
} catch ( Exception $exception ) {
$e = $exception;
}
Expand Down

0 comments on commit 143cd2a

Please sign in to comment.