-
Notifications
You must be signed in to change notification settings - Fork 384
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
Add capability to generate AMP-only content (AMP-as-Canonical) #668
Closed
Closed
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
d409c07
Add capability to generate AMP-only content (AMP-as-Canonical)
amedina b306aa1
Add capability to generate AMP-only content (AMP-as-Canonical)
amedina cba87d8
Fix merge conflicts
amedina 06513d4
First steps in postprocessing the AMP canonical html: add amp attr to…
amedina d355e39
Experimenting with getting the AMP Canonical mode working with the 20…
amedina 26b7526
Refactor code to add modulatiry to posprocessing functinoality.
amedina 5c57b70
Merge branch 'master' into amedina/amp-canonical
amedina 74c58c0
Fix canonical link issue for index page
amedina 9558d54
Merge branch 'master' into amedina/amp-canonical
amedina 94deda7
Fix index page
amedina c03dc7c
Expand canonical generation to index page
amedina e386b6a
Reorg of code
amedina 190edf3
Resolve conflict with master
amedina 6d85380
Resolve conflict with master
amedina e07964f
Merge branch 'amedina/amp-canonical' of github.com:Automattic/amp-wp …
amedina 94a5782
Add processing of canonical index page
amedina 34db70f
Progress on AMPing of index page
amedina b0d7ac9
Do not remove sidebars from index page
amedina ee552be
Fixed include of AMPContent class
amedina 30121f6
Rename postprocessing file/class; Define img-to-ampimg coverter
amedina f6799f0
Add carousel sanitzer; add amp link to REST API output
amedina 5d70e94
Fix flush rewrite rules after changing settings
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,126 @@ | ||
<?php | ||
require_once( AMP__DIR__ . '/includes/utils/class-amp-dom-utils.php'); | ||
require_once( AMP__DIR__ . '/includes/utils/class-amp-html-utils.php'); | ||
require_once( AMP__DIR__ . '/includes/utils/class-amp-string-utils.php'); | ||
|
||
require_once( AMP__DIR__ . '/includes/class-amp-content.php'); | ||
|
||
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-blacklist-sanitizer.php'); | ||
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-img-sanitizer.php'); | ||
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-video-sanitizer.php'); | ||
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-iframe-sanitizer.php'); | ||
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-audio-sanitizer.php'); | ||
require_once( AMP__DIR__ . '/includes/sanitizers/class-amp-style-sanitizer.php'); | ||
|
||
require_once( AMP__DIR__ . '/includes/embeds/class-amp-twitter-embed.php'); | ||
require_once( AMP__DIR__ . '/includes/embeds/class-amp-youtube-embed.php'); | ||
require_once( AMP__DIR__ . '/includes/embeds/class-amp-gallery-embed.php'); | ||
require_once( AMP__DIR__ . '/includes/embeds/class-amp-instagram-embed.php'); | ||
require_once( AMP__DIR__ . '/includes/embeds/class-amp-vine-embed.php'); | ||
require_once( AMP__DIR__ . '/includes/embeds/class-amp-facebook-embed.php'); | ||
require_once( AMP__DIR__ . '/includes/embeds/class-amp-dailymotion-embed.php'); | ||
require_once( AMP__DIR__ . '/includes/embeds/class-amp-soundcloud-embed.php'); | ||
|
||
function amp_canonical_retrieve_content() { | ||
|
||
$post = get_post(); | ||
|
||
$content_max_width = 1200; | ||
|
||
if ( isset( $GLOBALS['content_width']) && $GLOBALS['content_width'] > 0 ) { | ||
$content_max_width = $GLOBALS['content_width']; | ||
} | ||
|
||
$amp_content = new AMP_Content( | ||
$post->post_content, | ||
array( | ||
'AMP_Twitter_Embed_Handler' => array(), | ||
'AMP_YouTube_Embed_Handler' => array(), | ||
'AMP_DailyMotion_Embed_Handler' => array(), | ||
'AMP_SoundCloud_Embed_Handler' => array(), | ||
'AMP_Instagram_Embed_Handler' => array(), | ||
'AMP_Vine_Embed_Handler' => array(), | ||
'AMP_Facebook_Embed_Handler' => array(), | ||
'AMP_Gallery_Embed_Handler' => array(), | ||
), | ||
array( | ||
'AMP_Style_Sanitizer' => array(), | ||
'AMP_Blacklist_Sanitizer' => array(), | ||
'AMP_Img_Sanitizer' => array(), | ||
'AMP_Video_Sanitizer' => array(), | ||
'AMP_Audio_Sanitizer' => array(), | ||
'AMP_Iframe_Sanitizer' => array( | ||
'add_placeholder' => true, | ||
), | ||
), | ||
array( | ||
'content_max_width' => $content_max_width, | ||
) | ||
); | ||
|
||
return $amp_content; | ||
|
||
} | ||
|
||
// Generate the AMP post content early on | ||
// Runs the filters for the_content, but skips our filters below | ||
$GLOBALS['amp_content'] = amp_canonical_retrieve_content(); | ||
|
||
// The filter for the_content hook was already invoked, | ||
// attempt to remove all filters | ||
remove_all_filters('the_content'); | ||
|
||
add_action( 'wp_head', 'amp_canonical_add_scripts' ); | ||
add_action( 'wp_head', 'amp_canonical_add_boilerplate_css' ); | ||
add_action( 'wp_footer', 'amp_deregister_scripts' ); | ||
|
||
// Add the filter that replaces the content, and ensure | ||
// that no content filters run afterwards | ||
add_filter( 'the_content', 'amp_the_content_filter', PHP_INT_MAX); | ||
function amp_the_content_filter($content) { | ||
if (isset($GLOBALS['amp_content'])) { | ||
return $GLOBALS['amp_content']->get_amp_content(); | ||
} else { | ||
return $content; | ||
} | ||
} | ||
|
||
function amp_deregister_scripts() { | ||
wp_deregister_script( 'wp_embed'); | ||
} | ||
|
||
function amp_canonical_add_boilerplate_css() { | ||
?> | ||
<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 | ||
} | ||
|
||
function amp_canonical_add_scripts() { | ||
|
||
$amp_runtime_script = 'https://cdn.ampproject.org/v0.js'; | ||
|
||
// Always include AMP form & analytics, as almost every template includes | ||
// a search form and is tracking page views | ||
$scripts = array_merge( | ||
array('amp-form' => 'https://cdn.ampproject.org/v0/amp-form-0.1.js'), | ||
array('amp-analytics' => 'https://cdn.ampproject.org/v0/amp-analytics-0.1.js'), | ||
$GLOBALS['amp_content']->get_amp_scripts() | ||
); | ||
|
||
foreach ( $scripts as $element => $script) : ?> | ||
<script custom-element="<?php echo esc_attr( $element ); ?>" src="<?php echo esc_url( $script ); ?>" async></script> | ||
<?php endforeach; ?> | ||
<script src="<?php echo esc_url( $amp_runtime_script ); ?>" async></script> | ||
<?php | ||
} | ||
|
||
// TODO (@amedina) Get the canonical URL [Check!] | ||
if (!is_singular()) { | ||
add_action( 'wp_head', 'amp_post_template_add_canonical'); | ||
} | ||
function amp_post_template_add_canonical() { | ||
error_log("CANONICAL: Adding canonical link") | ||
?> | ||
<link rel="canonical" href="<?php echo esc_url( get_site_url() ); ?>" /> | ||
<?php | ||
} |
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,15 @@ | ||
<?php | ||
|
||
// before Jetpack ever gets loaded, we need to remove a | ||
// link rel prefetch for canonical AMP support | ||
// TODO[@amedina] These will be allowed by the validator in the future | ||
// TODO[@amedina] remove as soon as it works | ||
|
||
function amp_canonical_disable_jetpack_dns_fetch() { | ||
|
||
if ( class_exists( 'Jetpack') ) { | ||
remove_action( 'wp_head', array( 'Jetpack', 'dns_prefetch' ) ); | ||
} | ||
} | ||
// Hook action early on so we can unhook Jetpack | ||
add_action( 'wp_head', 'amp_canonical_disable_jetpack_dns_fetch', 0); |
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,18 @@ | ||
<?php | ||
|
||
class AMP_CSS_Utils | ||
{ | ||
public static function extract_and_minify_css_file($cssFile) | ||
{ | ||
// Load CSS file contents | ||
$minified_css = file_get_contents($cssFile); | ||
// Remove comments | ||
$minified_css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $minified_css); | ||
// Remove space after colons | ||
$minified_css = str_replace(': ', ':', $minified_css); | ||
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. Instead of lines
|
||
// Remove whitespace | ||
$minified_css = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $minified_css); | ||
|
||
return $minified_css; | ||
} | ||
} |
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
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.
Add spaces inside parentheses.
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.
Definitely; will do.