-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5932d55
Abstract notion of Actions
amedina 2a0bacb
Make AMP_Frontend_Actions class standalone
amedina f1f4ec4
Resolve conflicts from merging with master
amedina 70373a5
Add EOF newlines; rename register_hooks function
amedina f383a0f
Remove redundant file and corresponding file include and function cal…
amedina e411ed0
Merge branch 'master' into amedina/add-amp-actions-class-hierarchy
amedina dde7cf5
Fix minor formatting issues; remove classes in Actions hierarchy not …
amedina e8391e5
Merge branch 'master' into amedina/add-amp-actions-class-hierarchy
amedina bfeb2b7
Merge branch 'master' into amedina/add-amp-actions-class-hierarchy
amedina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ) { | ||
return; | ||
} | ||
$amp_url = amp_get_permalink( get_queried_object_id() ); | ||
printf( '<link rel="amphtml" href="%s" />', esc_url( $amp_url ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This change here renamed the filter from
amp_frontend_show_canonical
toadd_canonical_link
. This needs to be reverted. I think this needs to be reverted.