Skip to content

Commit

Permalink
Merge pull request #1235 from Automattic/add/supportable-templates
Browse files Browse the repository at this point in the history
Add support for allowing a site subset to be native AMP

Squashed commit of the following:

commit b54fcc0
Author: Weston Ruter <weston@xwp.co>
Date:   Mon Jul 2 12:32:53 2018 -0700

    Fix initialization of paried mode and Customizer, and fix support options on admin screen

commit 7091b35
Author: Weston Ruter <weston@xwp.co>
Date:   Mon Jul 2 11:44:37 2018 -0700

    Fix generation of AMP permalink for attachments

commit acf9849
Author: Weston Ruter <weston@xwp.co>
Date:   Mon Jul 2 11:31:22 2018 -0700

    Restore available_callback but mark as deprecated

    Use _doing_it_wrong() instead of trigger_error()

commit 38b5546
Author: Weston Ruter <weston@xwp.co>
Date:   Mon Jul 2 00:47:50 2018 -0700

    Add test for custom template

commit c172763
Author: Weston Ruter <weston@xwp.co>
Date:   Mon Jul 2 00:16:57 2018 -0700

    Add test for AMP_Theme_Support::get_template_availability()

commit 4bde27d
Author: Weston Ruter <weston@xwp.co>
Date:   Sun Jul 1 22:44:10 2018 -0700

    Add tests for AMP_Theme_Support::get_supportable_templates()

commit 11ea3d1
Author: Weston Ruter <weston@xwp.co>
Date:   Sun Jul 1 21:48:34 2018 -0700

    Remove unused variable and fix phpcs issue

commit 1bc4251
Author: Weston Ruter <weston@xwp.co>
Date:   Sun Jul 1 21:42:32 2018 -0700

    Restore the availability of AMP components in non-AMP documents in native sites

    Revert "Remove dirty AMP support; rename Disabled to Classic"

    This reverts most of commit 5bae33e.

commit c32d76b
Author: Weston Ruter <weston@xwp.co>
Date:   Sun Jul 1 21:03:53 2018 -0700

    Elimiante Other/unrecognized template option since indistinguishable from is_home

    * Devs should use filter to add support for custom templates
    * When a custom template is added via amp_supportable_templates filter then is_home will be discarded if there is another match

commit 3d07eac
Author: Weston Ruter <weston@xwp.co>
Date:   Sun Jul 1 20:49:53 2018 -0700

    Discard matching is_home when there are other matching templates

commit 221c3f0
Author: Weston Ruter <weston@xwp.co>
Date:   Sun Jul 1 20:22:08 2018 -0700

    Improve testing of finish_init; add test stubs

    * Clean up covers tags
    * Remove unnecessary is_template_support_required

commit 1c759e5
Author: Weston Ruter <weston@xwp.co>
Date:   Sun Jul 1 15:46:12 2018 -0700

    Add tests for read_theme_support, is_support_added_via_option, get_theme_support_args

    Store version with options to keep track of format

commit 2062816
Author: Weston Ruter <weston@xwp.co>
Date:   Sun Jul 1 09:21:46 2018 -0700

    Persist user selection even when checkbox disabled

    * De-duplicate logic

commit bfb202a
Author: Weston Ruter <weston@xwp.co>
Date:   Sat Jun 30 23:10:36 2018 -0700

    Let theme support args define templates_supported

    * Make sure admin screen shows proper options when optional amp theme support has been disabled.
    * Add get_theme_support_args method.

commit 7e56c8b
Author: Weston Ruter <weston@xwp.co>
Date:   Sat Jun 30 21:04:35 2018 -0700

    Add unrecognized_templates_supported for Other templates

    [ci skip]

commit 95fd958
Author: Weston Ruter <weston@xwp.co>
Date:   Sat Jun 30 20:52:08 2018 -0700

    Let template_availability return supported when no templates match but all supported

    * Fix remaining unit tests
    * Mock external HTTP requests

    [ci skip]

commit fafb94e
Author: Weston Ruter <weston@xwp.co>
Date:   Sat Jun 30 16:07:29 2018 -0700

    Mock the gfycat requests during unit tests

    [ci skip]

commit 1adad3e
Author: Weston Ruter <weston@xwp.co>
Date:   Sat Jun 30 15:59:17 2018 -0700

    Fix tests regarding amp status metabox

    [ci skip]

commit a7371d1
Author: Weston Ruter <weston@xwp.co>
Date:   Sat Jun 30 09:57:21 2018 -0700

    Simplify logic in AMP_Post_Meta_Box::render_status

    [ci skip]

commit a616e84
Author: Weston Ruter <weston@xwp.co>
Date:   Sat Jun 30 00:07:05 2018 -0700

    Don't show post enable/disable if template is not available

    Fix up some tests
    Address CR feedback

    [ci skip]

commit 3f4ef4c
Author: Weston Ruter <weston@xwp.co>
Date:   Fri Jun 29 11:18:10 2018 -0700

    WIP13 [ci skip]

commit 5288ccb
Author: Weston Ruter <weston@xwp.co>
Date:   Fri Jun 29 11:10:46 2018 -0700

    WIP12 [ci skip]

commit 0131c52
Author: Weston Ruter <weston@xwp.co>
Date:   Thu Jun 28 20:44:23 2018 -0700

    WIP11 [ci skip]

commit 8f46ee7
Author: Weston Ruter <weston@xwp.co>
Date:   Thu Jun 28 18:09:52 2018 -0700

    WIP10 [ci skip]

commit f440592
Author: Weston Ruter <weston@xwp.co>
Date:   Thu Jun 28 17:00:58 2018 -0700

    WIP9 [ci skip]

commit a7bf31e
Author: Weston Ruter <weston@xwp.co>
Date:   Wed Jun 27 23:41:34 2018 -0700

    WIP8

commit e5f4b77
Author: Weston Ruter <weston@xwp.co>
Date:   Wed Jun 27 23:09:05 2018 -0700

    Remove needless get_query_template

commit 3377ecf
Author: Weston Ruter <weston@xwp.co>
Date:   Wed Jun 27 22:22:01 2018 -0700

    WIP7

commit 2eda94b
Author: Weston Ruter <weston@xwp.co>
Date:   Wed Jun 27 21:46:34 2018 -0700

    WIP6

commit 1d2053c
Author: Weston Ruter <weston@xwp.co>
Date:   Wed Jun 27 16:42:27 2018 -0700

    WIP5

commit 718e643
Author: Weston Ruter <weston@xwp.co>
Date:   Wed Jun 27 12:40:15 2018 -0700

    WIP4

commit fab22d4
Author: Weston Ruter <weston@xwp.co>
Date:   Tue Jun 26 16:13:27 2018 -0700

    WIP3

commit 6149da1
Author: Weston Ruter <weston@xwp.co>
Date:   Sun Jun 24 14:42:05 2018 -0700

    WIP2

commit 143cd2a
Author: Weston Ruter <weston@xwp.co>
Date:   Thu Jun 21 17:12:35 2018 -0700

    WIP
  • Loading branch information
westonruter committed Jul 2, 2018
1 parent 09dbb8a commit 257c7ae
Show file tree
Hide file tree
Showing 25 changed files with 1,544 additions and 350 deletions.
68 changes: 54 additions & 14 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,28 +272,67 @@ 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' )`:
*
* @return boolean Whether this is in AMP 'canonical mode'.
* add_theme_support( 'amp' );
*
* This will serve templates in native AMP by default but the user would be able to change the template mode
* from native to paired in the admin. To force only native to be available, such as when you are using AMP components
* in your theme templates, do:
*
* add_theme_support( 'amp', array(
* 'mode' => 'native',
* ) );
*
* If you want to force AMP to always be served on a given template, you can use the templates_supported arg,
* for example to always serve the Category template in AMP:
*
* add_theme_support( 'amp', array(
* 'templates_supported' => array(
* 'is_category' => true,
* ),
* ) );
*
* Or if you want to force AMP to be used on all templates:
*
* add_theme_support( 'amp', array(
* 'templates_supported' => 'all',
* ) );
*
* @see AMP_Theme_Support::read_theme_support()
* @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' );
if ( true === $support ) {
return true;
if ( ! current_theme_supports( 'amp' ) ) {
return false;
}

$mode = 'native';
$support = get_theme_support( 'amp' );
if ( is_array( $support ) ) {
$args = array_shift( $support );
if ( empty( $args['template_dir'] ) ) {
return true;
$args = array_shift( $support );
$support = AMP_Options_Manager::get_option( 'theme_support' );

// If support is optional, look at DB option if mode is not explicitly set in theme support.
if ( ! empty( $args['optional'] ) ) {
if ( 'disabled' === $support ) {
return false;
} elseif ( ! isset( $args['mode'] ) ) {
return 'native' === $support;
}
}

if ( isset( $args['mode'] ) ) {
$mode = $args['mode'];
} elseif ( 'disabled' !== $support ) {
$mode = $support; // Supplied via admin screen.
} elseif ( ! empty( $args['template_dir'] ) ) {
$mode = 'paired'; // If there is a template_dir, then paired mode is implied.
}
}
return false;
return 'native' === $mode;
}

/**
Expand Down Expand Up @@ -426,6 +465,7 @@ function _amp_bootstrap_customizer() {

/**
* Redirects the old AMP URL to the new AMP URL.
*
* If post slug is updated the amp page with old post slug will be redirected to the updated url.
*
* @since 0.5
Expand Down
44 changes: 44 additions & 0 deletions includes/admin/class-amp-editor-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ public function init() {
if ( function_exists( 'gutenberg_init' ) ) {
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
add_filter( 'wp_kses_allowed_html', array( $this, 'whitelist_block_atts_in_wp_kses_allowed_html' ), 10, 2 );

/*
* Dirty AMP is required when a site is in native mode but not all templates are being served
* as AMP. In particular, if a single post is using AMP-specific Gutenberg Blocks which make
* use of AMP components, and the singular template is served as AMP but the blog page is not,
* then the non-AMP blog page need to load the AMP runtime scripts so that the AMP components
* in the posts displayed there will be rendered properly. This is only relevant on native AMP
* sites because the AMP Gutenberg blocks are only made available in that mode; they are not
* presented in the Gutenberg inserter in paired mode. In general, using AMP components in
* non-AMP documents is still not officially supported, so it's occurrence is being minimized
* as much as possible. For more, see <https://github.com/Automattic/amp-wp/issues/1192>.
*/
if ( amp_is_canonical() ) {
add_filter( 'the_content', array( $this, 'tally_content_requiring_amp_scripts' ) );
add_action( 'wp_print_footer_scripts', array( $this, 'print_dirty_amp_scripts' ) );
}
}
}

Expand Down Expand Up @@ -145,4 +161,32 @@ public function enqueue_block_editor_assets() {
) ) )
);
}

/**
* Tally the AMP component scripts that are needed in a dirty AMP document.
*
* @param string $content Content.
* @return string Content (unmodified).
*/
public function tally_content_requiring_amp_scripts( $content ) {
if ( ! is_amp_endpoint() ) {
$pattern = sprintf( '/<(%s)\b.*?>/s', join( '|', $this->amp_blocks ) );
if ( preg_match_all( $pattern, $content, $matches ) ) {
$this->content_required_amp_scripts = array_merge(
$this->content_required_amp_scripts,
$matches[1]
);
}
}
return $content;
}

/**
* Print AMP scripts required for AMP components used in a non-AMP document (dirty AMP).
*/
public function print_dirty_amp_scripts() {
if ( ! is_amp_endpoint() && ! empty( $this->content_required_amp_scripts ) ) {
wp_scripts()->do_items( $this->content_required_amp_scripts );
}
}
}
36 changes: 29 additions & 7 deletions includes/admin/class-amp-post-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,20 @@ public function enqueue_admin_assets() {
array( 'jquery' ),
AMP__VERSION
);

if ( current_theme_supports( 'amp' ) ) {
$availability = AMP_Theme_Support::get_template_availability( $post );
$support_errors = $availability['errors'];
} else {
$support_errors = AMP_Post_Type_Support::get_support_errors( $post );
}

wp_add_inline_script( self::ASSETS_HANDLE, sprintf( 'ampPostMetaBox.boot( %s );',
wp_json_encode( array(
'previewLink' => esc_url_raw( add_query_arg( amp_get_slug(), '', get_preview_post_link( $post ) ) ),
'canonical' => amp_is_canonical(),
'enabled' => post_supports_amp( $post ),
'canSupport' => count( AMP_Post_Type_Support::get_support_errors( $post ) ) === 0,
'enabled' => empty( $support_errors ),
'canSupport' => 0 === count( array_diff( $support_errors, array( 'post-status-disabled' ) ) ),
'statusInputName' => self::STATUS_INPUT_NAME,
'l10n' => array(
'ampPreviewBtnLabel' => __( 'Preview changes in AMP (opens in new window)', 'amp' ),
Expand All @@ -170,23 +178,37 @@ public function render_status( $post ) {
is_post_type_viewable( $post->post_type )
&&
current_user_can( 'edit_post', $post->ID )
&&
! amp_is_canonical()
);

if ( true !== $verify ) {
return;
}

$errors = AMP_Post_Type_Support::get_support_errors( $post );
$status = post_supports_amp( $post ) ? self::ENABLED_STATUS : self::DISABLED_STATUS;
/*
* When theme support is present then theme templates can be served in AMP and we check first if the template is available.
* Checking for template availability will include a check for get_support_errors. Otherwise, if theme support is not present
* then we just check get_support_errors.
*/
if ( current_theme_supports( 'amp' ) ) {
$availability = AMP_Theme_Support::get_template_availability( $post );
$status = $availability['supported'] ? self::ENABLED_STATUS : self::DISABLED_STATUS;
$errors = array_diff( $availability['errors'], array( 'post-status-disabled' ) ); // Subtract the status which the metabox will allow to be toggled.
if ( true === $availability['immutable'] ) {
$errors[] = 'status_immutable';
}
} else {
$errors = AMP_Post_Type_Support::get_support_errors( $post );
$status = empty( $errors ) ? self::ENABLED_STATUS : self::DISABLED_STATUS;
$errors = array_diff( $errors, array( 'post-status-disabled' ) ); // Subtract the status which the metabox will allow to be toggled.
}

$labels = array(
'enabled' => __( 'Enabled', 'amp' ),
'disabled' => __( 'Disabled', 'amp' ),
);

// The preceding variables are used inside the following amp-status.php template.
include_once AMP__DIR__ . '/templates/admin/amp-status.php';
include AMP__DIR__ . '/templates/admin/amp-status.php';
}

/**
Expand Down
84 changes: 29 additions & 55 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ function amp_get_permalink( $post_id ) {
||
// If the post type is hierarchical then the /amp/ endpoint isn't available.
is_post_type_hierarchical( get_post_type( $post_id ) )
||
// Attachment pages don't accept the /amp/ endpoint.
'attachment' === get_post_type( $post_id )
);
if ( $use_query_var ) {
$amp_url = add_query_arg( amp_get_slug(), '', $permalink );
Expand Down Expand Up @@ -239,61 +242,20 @@ function amp_add_amphtml_link() {
* @see AMP_Post_Type_Support::get_support_errors()
*
* @param WP_Post $post Post.
*
* @return bool Whether the post supports AMP.
*/
function post_supports_amp( $post ) {
if ( amp_is_canonical() ) {
return true;
}

$errors = AMP_Post_Type_Support::get_support_errors( $post );

// Return false if an error is found.
if ( ! empty( $errors ) ) {
return false;
}

switch ( get_post_meta( $post->ID, AMP_Post_Meta_Box::STATUS_POST_META_KEY, true ) ) {
case AMP_Post_Meta_Box::ENABLED_STATUS:
return true;

case AMP_Post_Meta_Box::DISABLED_STATUS:
return false;

// Disabled by default for custom page templates, page on front and page for posts.
default:
$enabled = (
! (bool) get_page_template_slug( $post )
&&
! (
'page' === $post->post_type
&&
'page' === get_option( 'show_on_front' )
&&
in_array( (int) $post->ID, array(
(int) get_option( 'page_on_front' ),
(int) get_option( 'page_for_posts' ),
), true )
)
);

/**
* Filters whether default AMP status should be enabled or not.
*
* @since 0.6
*
* @param string $status Status.
* @param WP_Post $post Post.
*/
return apply_filters( 'amp_post_status_default_enabled', $enabled, $post );
}
return 0 === count( AMP_Post_Type_Support::get_support_errors( $post ) );
}

/**
* Are we currently on an AMP URL?
* Determine whether the current response being served as AMP.
*
* @since 1.0 This function can be called before the `parse_query` action because the 'amp' query var is specifically and exclusively used when 'amp' theme support is added.
* This function cannot be called before the parse_query action because it needs to be able
* to determine the queried object is able to be served as AMP. If 'amp' theme support is not
* present, this function returns true just if the query var is present. If theme support is
* present, then it returns true in paired mode if an AMP template is available and the query
* var is present, or else in native mode if just the template is available.
*
* @return bool Whether it is the AMP endpoint.
*/
Expand All @@ -302,17 +264,29 @@ function is_amp_endpoint() {
return false;
}

// When 'amp' theme support is (or will be added) then these are the conditions that are key to be checked.
if ( amp_is_canonical() || isset( $_GET[ amp_get_slug() ] ) ) { // WPCS: CSRF OK.
return true;
$did_parse_query = did_action( 'parse_query' );

if ( ! $did_parse_query ) {
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( "is_amp_endpoint() was called before the 'parse_query' hook was called. This function will always return 'false' before the 'parse_query' hook is called.", 'amp' ) ), '0.4.2' );
}

// Condition for non-theme support when /amp/ endpoint is used.
if ( false !== get_query_var( amp_get_slug(), false ) ) {
return true;
$has_amp_query_var = (
isset( $_GET[ amp_get_slug() ] ) // WPCS: CSRF OK.
||
false !== get_query_var( amp_get_slug(), false )
);

if ( ! current_theme_supports( 'amp' ) ) {
return $has_amp_query_var;
}

// When there is no query var and AMP is not canonical/native, then this is definitely not an AMP endpoint.
if ( ! $has_amp_query_var && ! amp_is_canonical() ) {
return false;
}

return false;
$availability = AMP_Theme_Support::get_template_availability();
return amp_is_canonical() ? $availability['supported'] : ( $has_amp_query_var && $availability['supported'] );
}

/**
Expand Down
Loading

0 comments on commit 257c7ae

Please sign in to comment.