-
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 support for allowing a site subset to be native AMP #1235
Changes from all commits
143cd2a
6149da1
fab22d4
718e643
1d2053c
2eda94b
3377ecf
e5f4b77
a7bf31e
f440592
8f46ee7
0131c52
5288ccb
3f4ef4c
a616e84
a7371d1
1adad3e
fafb94e
95fd958
7e56c8b
bfb202a
2062816
1c759e5
221c3f0
3d07eac
c32d76b
1bc4251
11ea3d1
4bde27d
c172763
38b5546
acf9849
7091b35
b54fcc0
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 |
---|---|---|
|
@@ -43,6 +43,22 @@ public function init() { | |
if ( function_exists( 'gutenberg_init' ) ) { | ||
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); | ||
add_filter( 'wp_kses_allowed_html', array( $this, 'whitelist_block_atts_in_wp_kses_allowed_html' ), 10, 2 ); | ||
|
||
/* | ||
* Dirty AMP is required when a site is in native mode but not all templates are being served | ||
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 comment is hard to follow; perhaps not mentioning "Dirty AMP" first, but explaining directly the semantics of the filter and action functions may be clearer. Then the concept of Dirty AMP can be expressed. Just a thought. 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. No need to look/address this now. |
||
* as AMP. In particular, if a single post is using AMP-specific Gutenberg Blocks which make | ||
* use of AMP components, and the singular template is served as AMP but the blog page is not, | ||
* then the non-AMP blog page need to load the AMP runtime scripts so that the AMP components | ||
* in the posts displayed there will be rendered properly. This is only relevant on native AMP | ||
* sites because the AMP Gutenberg blocks are only made available in that mode; they are not | ||
* presented in the Gutenberg inserter in paired mode. In general, using AMP components in | ||
* non-AMP documents is still not officially supported, so it's occurrence is being minimized | ||
* as much as possible. For more, see <https://github.com/Automattic/amp-wp/issues/1192>. | ||
*/ | ||
if ( amp_is_canonical() ) { | ||
add_filter( 'the_content', array( $this, 'tally_content_requiring_amp_scripts' ) ); | ||
add_action( 'wp_print_footer_scripts', array( $this, 'print_dirty_amp_scripts' ) ); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -145,4 +161,32 @@ public function enqueue_block_editor_assets() { | |
) ) ) | ||
); | ||
} | ||
|
||
/** | ||
* Tally the AMP component scripts that are needed in a dirty AMP document. | ||
* | ||
* @param string $content Content. | ||
* @return string Content (unmodified). | ||
*/ | ||
public function tally_content_requiring_amp_scripts( $content ) { | ||
if ( ! is_amp_endpoint() ) { | ||
$pattern = sprintf( '/<(%s)\b.*?>/s', join( '|', $this->amp_blocks ) ); | ||
if ( preg_match_all( $pattern, $content, $matches ) ) { | ||
$this->content_required_amp_scripts = array_merge( | ||
$this->content_required_amp_scripts, | ||
$matches[1] | ||
); | ||
} | ||
} | ||
return $content; | ||
} | ||
|
||
/** | ||
* Print AMP scripts required for AMP components used in a non-AMP document (dirty AMP). | ||
*/ | ||
public function print_dirty_amp_scripts() { | ||
if ( ! is_amp_endpoint() && ! empty( $this->content_required_amp_scripts ) ) { | ||
wp_scripts()->do_items( $this->content_required_amp_scripts ); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,12 +143,20 @@ public function enqueue_admin_assets() { | |
array( 'jquery' ), | ||
AMP__VERSION | ||
); | ||
|
||
if ( current_theme_supports( 'amp' ) ) { | ||
$availability = AMP_Theme_Support::get_template_availability( $post ); | ||
$support_errors = $availability['errors']; | ||
} else { | ||
$support_errors = AMP_Post_Type_Support::get_support_errors( $post ); | ||
} | ||
|
||
wp_add_inline_script( self::ASSETS_HANDLE, sprintf( 'ampPostMetaBox.boot( %s );', | ||
wp_json_encode( array( | ||
'previewLink' => esc_url_raw( add_query_arg( amp_get_slug(), '', get_preview_post_link( $post ) ) ), | ||
'canonical' => amp_is_canonical(), | ||
'enabled' => post_supports_amp( $post ), | ||
'canSupport' => count( AMP_Post_Type_Support::get_support_errors( $post ) ) === 0, | ||
'enabled' => empty( $support_errors ), | ||
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. Very nice semantics. |
||
'canSupport' => 0 === count( array_diff( $support_errors, array( 'post-status-disabled' ) ) ), | ||
'statusInputName' => self::STATUS_INPUT_NAME, | ||
'l10n' => array( | ||
'ampPreviewBtnLabel' => __( 'Preview changes in AMP (opens in new window)', 'amp' ), | ||
|
@@ -170,23 +178,37 @@ public function render_status( $post ) { | |
is_post_type_viewable( $post->post_type ) | ||
&& | ||
current_user_can( 'edit_post', $post->ID ) | ||
&& | ||
! amp_is_canonical() | ||
); | ||
|
||
if ( true !== $verify ) { | ||
return; | ||
} | ||
|
||
$errors = AMP_Post_Type_Support::get_support_errors( $post ); | ||
$status = post_supports_amp( $post ) ? self::ENABLED_STATUS : self::DISABLED_STATUS; | ||
/* | ||
* When theme support is present then theme templates can be served in AMP and we check first if the template is available. | ||
* Checking for template availability will include a check for get_support_errors. Otherwise, if theme support is not present | ||
* then we just check get_support_errors. | ||
*/ | ||
if ( current_theme_supports( 'amp' ) ) { | ||
$availability = AMP_Theme_Support::get_template_availability( $post ); | ||
$status = $availability['supported'] ? self::ENABLED_STATUS : self::DISABLED_STATUS; | ||
$errors = array_diff( $availability['errors'], array( 'post-status-disabled' ) ); // Subtract the status which the metabox will allow to be toggled. | ||
if ( true === $availability['immutable'] ) { | ||
$errors[] = 'status_immutable'; | ||
} | ||
} else { | ||
$errors = AMP_Post_Type_Support::get_support_errors( $post ); | ||
$status = empty( $errors ) ? self::ENABLED_STATUS : self::DISABLED_STATUS; | ||
$errors = array_diff( $errors, array( 'post-status-disabled' ) ); // Subtract the status which the metabox will allow to be toggled. | ||
} | ||
|
||
$labels = array( | ||
'enabled' => __( 'Enabled', 'amp' ), | ||
'disabled' => __( 'Disabled', 'amp' ), | ||
); | ||
|
||
// The preceding variables are used inside the following amp-status.php template. | ||
include_once AMP__DIR__ . '/templates/admin/amp-status.php'; | ||
include AMP__DIR__ . '/templates/admin/amp-status.php'; | ||
} | ||
|
||
/** | ||
|
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.
May there be a more differentiating naming for
current_theme_supports
vs.get_theme_support
? Probably I am missing some of the context but the naming appear a little confusing.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.
The
current_theme_supports()
andget_theme_support()
are WordPress core functions. They are somewhat confusing. The former returns whether support has been added (boolean), and applies a filter to let plugins control whether support is present. Theget_theme_support()
on the other hand does not apply a filter, but rather it returns the args that were passed in for the originaladd_theme_support()
call.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.
Got it. Makes sense now.