Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract notion of AMP Actions #725

Merged
merged 9 commits into from
Aug 18, 2017
8 changes: 5 additions & 3 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
require_once( AMP__DIR__ . '/includes/settings/class-amp-customizer-settings.php' );
require_once( AMP__DIR__ . '/includes/settings/class-amp-customizer-design-settings.php' );

require_once( AMP__DIR__ . '/includes/actions/class-amp-frontend-actions.php' );
require_once( AMP__DIR__ . '/includes/actions/class-amp-paired-post-actions.php' );

register_activation_hook( __FILE__, 'amp_activate' );
function amp_activate() {
if ( ! did_action( 'amp_init' ) ) {
Expand Down Expand Up @@ -111,13 +114,12 @@ function amp_load_classes() {
}

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

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

function amp_prepare_render() {
Expand Down
12 changes: 12 additions & 0 deletions includes/actions/class-amp-actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

abstract class AMP_Actions {
public static function add_scripts( $template ) {}
public static function add_styles( $template ) {}
public static function add_fonts( $template ) {}
public static function add_boilerplate_css( $template ) {}
public static function add_schemaorg_metadata( $template ) {}
public static function add_analytics_scripts( $template ) {}
public static function add_analytics_data( $template ) {}
public static function add_canonical_link( $template ) {}
}
19 changes: 19 additions & 0 deletions includes/actions/class-amp-frontend-actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
// Callbacks for adding AMP-related things to the main theme

require_once( AMP__DIR__ . '/includes/actions/class-amp-actions.php' );

class AMP_Frontend_Actions {

public static function register_hooks() {
add_action( 'wp_head', 'AMP_Frontend_Actions::add_canonical' );
}

public static function add_canonical() {
if ( false === apply_filters( 'add_canonical_link', true ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change here renamed the filter from amp_frontend_show_canonical to add_canonical_link. This needs to be reverted. I think this needs to be reverted.

return;
}
$amp_url = amp_get_permalink( get_queried_object_id() );
printf( '<link rel="amphtml" href="%s" />', esc_url( $amp_url ) );
}
}
106 changes: 106 additions & 0 deletions includes/actions/class-amp-paired-post-actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

require_once( AMP__DIR__ . '/includes/actions/class-amp-actions.php' );

class AMP_Paired_Post_Actions extends AMP_Actions {

public static function register_hooks() {
add_action( 'amp_post_template_head', 'AMP_Paired_Post_Actions::add_title' );
add_action( 'amp_post_template_head', 'AMP_Paired_Post_Actions::add_canonical_link' );
add_action( 'amp_post_template_head', 'AMP_Paired_Post_Actions::add_scripts' );
add_action( 'amp_post_template_head', 'AMP_Paired_Post_Actions::add_fonts' );
add_action( 'amp_post_template_head', 'AMP_Paired_Post_Actions::add_boilerplate_css' );
add_action( 'amp_post_template_head', 'AMP_Paired_Post_Actions::add_schemaorg_metadata' );
add_action( 'amp_post_template_css', 'AMP_Paired_Post_Actions::add_styles', 99 );
add_action( 'amp_post_template_data', 'AMP_Paired_Post_Actions::add_analytics_scripts' );
add_action( 'amp_post_template_footer', 'AMP_Paired_Post_Actions::add_analytics_data' );
}

public static function add_title( $amp_template ) {
?>
<title><?php echo esc_html( $amp_template->get( 'document_title' ) ); ?></title>
<?php
}

public static function add_canonical_link( $amp_template ) {
?>
<link rel="canonical" href="<?php echo esc_url( $amp_template->get( 'canonical_url' ) ); ?>" />
<?php
}

public static function add_scripts( $amp_template ) {
$scripts = $amp_template->get( 'amp_component_scripts', array() );
foreach ( $scripts as $element => $script ) :
$custom_type = ($element == 'amp-mustache') ? 'template' : 'element'; ?>
<script custom-<?php echo esc_attr( $custom_type ); ?>="<?php echo esc_attr( $element ); ?>" src="<?php echo esc_url( $script ); ?>" async></script>
<?php endforeach; ?>
<script src="<?php echo esc_url( $amp_template->get( 'amp_runtime_script' ) ); ?>" async></script>
<?php
}

public static function add_fonts( $amp_template ) {
$font_urls = $amp_template->get( 'font_urls', array() );
foreach ( $font_urls as $slug => $url ) : ?>
<link rel="stylesheet" href="<?php echo esc_url( $url ); ?>">
<?php endforeach;
}

public static function add_boilerplate_css( $amp_template ) {
?>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<?php
}

public static function add_schemaorg_metadata( $amp_template ) {
$metadata = $amp_template->get( 'metadata' );
if ( empty( $metadata ) ) {
return;
}
?>
<script type="application/ld+json"><?php echo wp_json_encode( $metadata ); ?></script>
<?php
}

public static function add_styles( $amp_template ) {
$styles = $amp_template->get( 'post_amp_styles' );
if ( ! empty( $styles ) ) {
echo '/* Inline styles */' . PHP_EOL;
foreach ( $styles as $selector => $declarations ) {
$declarations = implode( ';', $declarations ) . ';';
printf( '%1$s{%2$s}', $selector, $declarations );
}
}
}

public static function add_analytics_scripts( $data ) {
if ( ! empty( $data['amp_analytics'] ) ) {
$data['amp_component_scripts']['amp-analytics'] = 'https://cdn.ampproject.org/v0/amp-analytics-0.1.js';
}
return $data;
}

public static function add_analytics_data( $amp_template ) {
$analytics_entries = $amp_template->get( 'amp_analytics' );
if ( empty( $analytics_entries ) ) {
return;
}

foreach ( $analytics_entries as $id => $analytics_entry ) {
if ( ! isset( $analytics_entry['type'], $analytics_entry['attributes'], $analytics_entry['config_data'] ) ) {
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'Analytics entry for %s is missing one of the following keys: `type`, `attributes`, or `config_data` (array keys: %s)', 'amp' ), esc_html( $id ), esc_html( implode( ', ', array_keys( $analytics_entry ) ) ) ), '0.3.2' );
continue;
}

$script_element = AMP_HTML_Utils::build_tag( 'script', array(
'type' => 'application/json',
), wp_json_encode( $analytics_entry['config_data'] ) );

$amp_analytics_attr = array_merge( array(
'id' => $id,
'type' => $analytics_entry['type'],
), $analytics_entry['attributes'] );

echo AMP_HTML_Utils::build_tag( 'amp-analytics', $amp_analytics_attr, $script_element );
}
}
}
13 changes: 0 additions & 13 deletions includes/amp-frontend-actions.php

This file was deleted.

101 changes: 0 additions & 101 deletions includes/amp-post-template-actions.php

This file was deleted.