-
Notifications
You must be signed in to change notification settings - Fork 383
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
Changes from 5 commits
5932d55
2a0bacb
f1f4ec4
70373a5
f383a0f
e411ed0
dde7cf5
e8391e5
bfeb2b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 ) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
class AMP_Canonical_Mode_Actions extends AMP_Actions { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
class AMP_Canonical_Post_Actions extends AMP_Canonical_Mode_Actions { | ||
|
||
} |
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' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: spacing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
|
||
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 ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change here renamed the filter from |
||
return; | ||
} | ||
$amp_url = amp_get_permalink( get_queried_object_id() ); | ||
printf( '<link rel="amphtml" href="%s" />', esc_url( $amp_url ) ); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
class AMP_Paired_Mode_Actions extends AMP_Actions { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
require_once( AMP__DIR__ . '/includes/actions/class-amp-paired-mode-actions.php' ); | ||
|
||
class AMP_Paired_Post_Actions extends AMP_Paired_Mode_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 ); | ||
} | ||
} | ||
} |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: spacing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.