Skip to content

Commit

Permalink
Merge pull request #856 from Automattic/add/849-paired-mode-templates
Browse files Browse the repository at this point in the history
Add paired mode custom templates
  • Loading branch information
westonruter committed Jan 15, 2018
2 parents 75ed4a9 + 34c18cc commit a4c8f36
Show file tree
Hide file tree
Showing 14 changed files with 387 additions and 322 deletions.
22 changes: 11 additions & 11 deletions amp.php
Expand Up @@ -127,13 +127,14 @@ function amp_force_query_var_value( $query_vars ) {
* @return void
*/
function amp_maybe_add_actions() {
$is_amp_endpoint = is_amp_endpoint();

// Add hooks for when a themes that support AMP.
if ( current_theme_supports( 'amp' ) ) {
if ( amp_is_canonical() || is_amp_endpoint() ) {
AMP_Theme_Support::register_hooks();
if ( $is_amp_endpoint ) {
AMP_Theme_Support::init();
} else {
AMP_Frontend_Actions::register_hooks();
amp_add_frontend_actions();
}
return;
}
Expand All @@ -143,16 +144,14 @@ function amp_maybe_add_actions() {
return;
}

$is_amp_endpoint = is_amp_endpoint();

// Cannot use `get_queried_object` before canonical redirect; see <https://core.trac.wordpress.org/ticket/35344>.
global $wp_query;
$post = $wp_query->post;

$supports = post_supports_amp( $post );

if ( ! $supports ) {
if ( $is_amp_endpoint ) {
if ( $is_amp_endpoint && isset( $post->ID ) ) {
wp_safe_redirect( get_permalink( $post->ID ), 301 );
exit;
}
Expand All @@ -172,7 +171,7 @@ function amp_maybe_add_actions() {
* 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/' ) )`
* `add_theme_support( 'amp', array( 'template_dir' => 'my-amp-templates' ) )`
* it will retain 'paired mode.
*
* @return boolean Whether this is in AMP 'canonical mode'.
Expand All @@ -184,7 +183,7 @@ function amp_is_canonical() {
}
if ( is_array( $support ) ) {
$args = array_shift( $support );
if ( empty( $args['template_path'] ) ) {
if ( empty( $args['template_dir'] ) ) {
return true;
}
}
Expand All @@ -196,12 +195,13 @@ function amp_load_classes() {
}

function amp_add_frontend_actions() {
AMP_Frontend_Actions::register_hooks();
require_once AMP__DIR__ . '/includes/amp-frontend-actions.php';
}

function amp_add_post_template_actions() {
AMP_Paired_Post_Actions::register_hooks();
require_once( AMP__DIR__ . '/includes/amp-post-template-functions.php' );
require_once AMP__DIR__ . '/includes/amp-post-template-actions.php';
require_once AMP__DIR__ . '/includes/amp-post-template-functions.php';
amp_post_template_init_hooks();
}

function amp_prepare_render() {
Expand Down
20 changes: 0 additions & 20 deletions includes/actions/class-amp-actions.php

This file was deleted.

63 changes: 0 additions & 63 deletions includes/actions/class-amp-frontend-actions.php

This file was deleted.

176 changes: 0 additions & 176 deletions includes/actions/class-amp-paired-post-actions.php

This file was deleted.

32 changes: 32 additions & 0 deletions includes/amp-frontend-actions.php
@@ -0,0 +1,32 @@
<?php
/**
* Callbacks for adding AMP-related things to the main theme.
*
* @package AMP
*/

add_action( 'wp_head', 'amp_frontend_add_canonical' );

/**
* Add amphtml link to frontend.
*
* @since 0.2
*/
function amp_frontend_add_canonical() {

// Prevent showing amphtml link if theme supports AMP but paired mode is not available.
if ( current_theme_supports( 'amp' ) && ! AMP_Theme_Support::is_paired_available() ) {
return;
}

/**
* Filters whether to show the amphtml link on the frontend.
*
* @since 0.2
*/
if ( false === apply_filters( 'amp_frontend_show_canonical', true ) ) {
return;
}

printf( '<link rel="amphtml" href="%s">', esc_url( amp_get_permalink( get_queried_object_id() ) ) );
}
8 changes: 8 additions & 0 deletions includes/amp-helper-functions.php
Expand Up @@ -63,6 +63,10 @@ function amp_get_permalink( $post_id ) {
* @return bool Whether the post supports AMP.
*/
function post_supports_amp( $post ) {
if ( amp_is_canonical() ) {
return true;
}

return 0 === count( AMP_Post_Type_Support::get_support_errors( $post ) );
}

Expand All @@ -74,6 +78,10 @@ function post_supports_amp( $post ) {
* @return bool Whether it is the AMP endpoint.
*/
function is_amp_endpoint() {
if ( amp_is_canonical() ) {
return true;
}

if ( 0 === did_action( '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' );
}
Expand Down

0 comments on commit a4c8f36

Please sign in to comment.