diff --git a/amp.php b/amp.php index b9c6524fead..f854b6caea3 100644 --- a/amp.php +++ b/amp.php @@ -5,7 +5,7 @@ * Plugin URI: https://github.com/automattic/amp-wp * Author: Automattic * Author URI: https://automattic.com - * Version: 0.6-alpha + * Version: 0.7-alpha * Text Domain: amp * Domain Path: /languages/ * License: GPLv2 or later @@ -15,7 +15,7 @@ define( 'AMP__FILE__', __FILE__ ); define( 'AMP__DIR__', dirname( __FILE__ ) ); -define( 'AMP__VERSION', '0.6-alpha' ); +define( 'AMP__VERSION', '0.7-alpha' ); require_once AMP__DIR__ . '/includes/class-amp-autoloader.php'; AMP_Autoloader::register(); @@ -117,8 +117,17 @@ function amp_force_query_var_value( $query_vars ) { return $query_vars; } +/** + * Conditionally add AMP actions or render the 'paired mode' template(s). + * + * If the request is for an AMP page and this is in 'canonical mode,' redirect to the non-AMP page. + * It won't need this plugin's template system, nor the frontend actions like the 'rel' link. + * + * @since 0.2 + * @return void + */ function amp_maybe_add_actions() { - if ( ! is_singular() || is_feed() ) { + if ( amp_is_canonical() || ! is_singular() || is_feed() ) { return; } @@ -145,6 +154,31 @@ function amp_maybe_add_actions() { } } +/** + * 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_path' => get_template_directory() . 'my-amp-templates/' ) )` + * it will retain 'paired mode. + * + * @return boolean Whether this is in AMP 'canonical mode'. + */ +function amp_is_canonical() { + $support = get_theme_support( 'amp' ); + if ( true === $support ) { + return true; + } + if ( is_array( $support ) ) { + $args = array_shift( $support ); + if ( empty( $args['template_path'] ) ) { + return true; + } + } + return false; +} + function amp_load_classes() { _deprecated_function( __FUNCTION__, '0.6' ); } @@ -225,10 +259,16 @@ function amp_render_post( $post ) { /** * Bootstraps the AMP customizer. * + * Uses the priority of 12 for the 'after_setup_theme' action. + * Many themes run `add_theme_support()` on the 'after_setup_theme' hook, at the default priority of 10. + * And that function's documentation suggests adding it to that action. + * So this enables themes to `add_theme_support( 'amp' )`. + * And `amp_init_customizer()` will be able to recognize theme support by calling `amp_is_canonical()`. + * * @since 0.4 */ function _amp_bootstrap_customizer() { - add_action( 'after_setup_theme', 'amp_init_customizer' ); + add_action( 'after_setup_theme', 'amp_init_customizer', 12 ); } add_action( 'plugins_loaded', '_amp_bootstrap_customizer', 9 ); // Should be hooked before priority 10 on 'plugins_loaded' to properly unhook core panels. diff --git a/assets/js/amp-post-meta-box.js b/assets/js/amp-post-meta-box.js index 8f1816fbc59..fe78c680018 100644 --- a/assets/js/amp-post-meta-box.js +++ b/assets/js/amp-post-meta-box.js @@ -18,6 +18,7 @@ var ampPostMetaBox = ( function( $ ) { * @since 0.6 */ data: { + canonical: false, previewLink: '', disabled: false, statusInputName: '', @@ -58,7 +59,7 @@ var ampPostMetaBox = ( function( $ ) { component.boot = function boot( data ) { component.data = data; $( document ).ready( function() { - if ( ! component.data.disabled ) { + if ( ! component.data.disabled && ! component.data.canonical ) { component.addPreviewButton(); } component.listen(); diff --git a/composer.json b/composer.json index ee52ea6f241..c1cbbeb69fd 100644 --- a/composer.json +++ b/composer.json @@ -4,5 +4,5 @@ "homepage": "https://github.com/Automattic/amp-wp", "type": "wordpress-plugin", "license": "GPL-2.0", - "version": "0.6.0" + "version": "0.7.0" } diff --git a/includes/admin/class-amp-post-meta-box.php b/includes/admin/class-amp-post-meta-box.php index 34ac061e6b7..d0ab6532b61 100644 --- a/includes/admin/class-amp-post-meta-box.php +++ b/includes/admin/class-amp-post-meta-box.php @@ -122,6 +122,7 @@ public function enqueue_admin_assets() { wp_add_inline_script( self::ASSETS_HANDLE, sprintf( 'ampPostMetaBox.boot( %s );', wp_json_encode( array( 'previewLink' => esc_url_raw( add_query_arg( AMP_QUERY_VAR, '', get_preview_post_link( $post ) ) ), + 'canonical' => amp_is_canonical(), 'disabled' => (bool) get_post_meta( $post->ID, self::DISABLED_POST_META_KEY, true ) || ! $this->is_amp_available( $post ), 'statusInputName' => self::STATUS_INPUT_NAME, 'l10n' => array( @@ -142,6 +143,8 @@ public function render_status( $post ) { isset( $post->ID ) && current_user_can( 'edit_post', $post->ID ) + && + ! amp_is_canonical() ); if ( true !== $verify ) { diff --git a/includes/admin/functions.php b/includes/admin/functions.php index 1b07cc21648..6a1d4536e76 100644 --- a/includes/admin/functions.php +++ b/includes/admin/functions.php @@ -15,8 +15,16 @@ /** * Sets up the AMP template editor for the Customizer. + * + * If this is in AMP canonical mode, exit. + * There's no need for the 'AMP' Customizer panel, + * And this does not need to toggle between the AMP and normal display. */ function amp_init_customizer() { + if ( amp_is_canonical() ) { + return; + } + // Fire up the AMP Customizer. add_action( 'customize_register', array( 'AMP_Template_Customizer', 'init' ), 500 ); diff --git a/includes/options/class-amp-options-menu.php b/includes/options/class-amp-options-menu.php index 0db894b48e5..a06d5f4169a 100644 --- a/includes/options/class-amp-options-menu.php +++ b/includes/options/class-amp-options-menu.php @@ -84,7 +84,7 @@ public function render_post_types_support() { name}"; - $is_builtin = in_array( $post_type->name, $builtin_support, true ); + $is_builtin = amp_is_canonical() || in_array( $post_type->name, $builtin_support, true ); ?> @@ -94,7 +94,7 @@ public function render_post_types_support() { id="" name="" value="name ); ?>" - name, AMP_QUERY_VAR ) ); ?> + name, AMP_QUERY_VAR ) ); ?> >
-

+

+ +

diff --git a/package.json b/package.json index 441e1859ff1..a8d39774a04 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "type": "git", "url": "https://github.com/Automattic/amp-wp.git" }, - "version": "0.6.0", + "version": "0.7.0", "license": "GPL-2.0+", "private": true, "devDependencies": { diff --git a/readme.md b/readme.md index 0cd32bed08c..8339193a366 100644 --- a/readme.md +++ b/readme.md @@ -55,6 +55,9 @@ Follow along with or [contribute](https://github.com/Automattic/amp-wp/blob/deve ## Changelog ## +### 0.7 (unreleased) ### +- Add support for canonical AMP. + ### 0.6 (unreleased) ### - Add: support for the "page" post type, except when used as homepage or page for posts. A new `page.php` is introduced with template parts factored out (`html-start.php`, `header.php`, `footer.php`, `html-end.php`) and re-used from `single.php`. Note that AMP URLs will end in `?amp` instead of `/amp/`. See [#825](https://github.com/Automattic/amp-wp/pull/825). Props technosailor, ThierryA, westonruter. - Add: AMP post preview button alongside non-AMP preview button. See [#813](https://github.com/Automattic/amp-wp/pull/813). Props ThierryA, westonruter. diff --git a/readme.txt b/readme.txt index 23eef7f2505..06dfd8daf9f 100644 --- a/readme.txt +++ b/readme.txt @@ -38,6 +38,10 @@ Follow along with or [contribute](https://github.com/Automattic/amp-wp/blob/deve == Changelog == += 0.7 (unreleased) = + +- Add support for canonical AMP. + = 0.6 (unreleased) = - Add: support for the "page" post type, except when used as homepage or page for posts. A new `page.php` is introduced with template parts factored out (`html-start.php`, `header.php`, `footer.php`, `html-end.php`) and re-used from `single.php`. Note that AMP URLs will end in `?amp` instead of `/amp/`. See [#825](https://github.com/Automattic/amp-wp/pull/825). Props technosailor, ThierryA, westonruter. diff --git a/tests/test-amp.php b/tests/test-amp.php new file mode 100644 index 00000000000..61515354745 --- /dev/null +++ b/tests/test-amp.php @@ -0,0 +1,45 @@ +assertFalse( amp_is_canonical() ); + + add_theme_support( 'amp' ); + $this->assertTrue( amp_is_canonical() ); + + remove_theme_support( 'amp' ); + add_theme_support( 'amp', array( + 'template_path' => get_template_directory() . 'amp-templates/', + ) ); + $this->assertFalse( amp_is_canonical() ); + + remove_theme_support( 'amp' ); + add_theme_support( 'amp', array( + 'custom_prop' => 'something', + ) ); + $this->assertTrue( amp_is_canonical() ); + } +} diff --git a/tests/test-class-amp-meta-box.php b/tests/test-class-amp-meta-box.php index e9b7d7eaa71..14f7e610499 100644 --- a/tests/test-class-amp-meta-box.php +++ b/tests/test-class-amp-meta-box.php @@ -79,10 +79,19 @@ public function test_render_status() { wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator', ) ) ); + $amp_status_markup = '
instance->render_status( $post ); - $this->assertContains( '
assertNotContains( $amp_status_markup, ob_get_clean() ); + + // This is not in AMP 'canonical mode'. + remove_theme_support( 'amp' ); + ob_start(); + $this->instance->render_status( $post ); + $this->assertContains( $amp_status_markup, ob_get_clean() ); remove_post_type_support( 'post', AMP_QUERY_VAR );