-
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
Add support for allowing a site subset to be native AMP #1235
Changes from 14 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 |
---|---|---|
|
@@ -170,16 +170,38 @@ public function render_status( $post ) { | |
is_post_type_viewable( $post->post_type ) | ||
&& | ||
current_user_can( 'edit_post', $post->ID ) | ||
&& | ||
! amp_is_canonical() | ||
// @todo What if a site does not have non-AMP pages? Formerly this was indicated via amp_is_canonical(). Now we need a flag for whether AMP is exclusive to a theme, in which case all templates and post types must render AMP. In other words, all_templates_supported. | ||
); | ||
|
||
if ( true !== $verify ) { | ||
return; | ||
} | ||
|
||
$errors = AMP_Post_Type_Support::get_support_errors( $post ); | ||
$status = post_supports_amp( $post ) ? self::ENABLED_STATUS : self::DISABLED_STATUS; | ||
$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. | ||
|
||
// @todo If ! supported and immutable, don't show the toggle at all? | ||
// Handle special case of the static front page or page for posts. | ||
if ( current_theme_supports( 'amp' ) ) { | ||
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. Would it be possible having a comment for this 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. To explain why the condition is here? |
||
|
||
$query = new WP_Query(); | ||
if ( 'page' === $post->post_type ) { | ||
$query->set( 'page_id', $post->ID ); | ||
} else { | ||
$query->set( 'p', $post->ID ); | ||
} | ||
$query->queried_object = $post; | ||
$query->queried_object_id = $post->ID; | ||
$query->parse_query_vars(); | ||
|
||
$availability = AMP_Theme_Support::get_template_availability( $query ); | ||
if ( $availability instanceof WP_Error && 0 !== count( array_diff( $availability->get_error_codes(), $errors, array( 'post-status-disabled' ) ) ) ) { | ||
$errors[] = 'template-unavailable'; | ||
$status = self::DISABLED_STATUS; | ||
} | ||
} | ||
|
||
$labels = array( | ||
'enabled' => __( 'Enabled', 'amp' ), | ||
'disabled' => __( 'Disabled', 'amp' ), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -239,80 +239,62 @@ function amp_add_amphtml_link() { | |
* @see AMP_Post_Type_Support::get_support_errors() | ||
* | ||
* @param WP_Post $post Post. | ||
* | ||
* @return bool Whether the post supports AMP. | ||
*/ | ||
function post_supports_amp( $post ) { | ||
if ( amp_is_canonical() ) { | ||
return true; | ||
} | ||
|
||
$errors = AMP_Post_Type_Support::get_support_errors( $post ); | ||
|
||
// Return false if an error is found. | ||
if ( ! empty( $errors ) ) { | ||
return false; | ||
} | ||
|
||
switch ( get_post_meta( $post->ID, AMP_Post_Meta_Box::STATUS_POST_META_KEY, true ) ) { | ||
case AMP_Post_Meta_Box::ENABLED_STATUS: | ||
return true; | ||
|
||
case AMP_Post_Meta_Box::DISABLED_STATUS: | ||
return false; | ||
|
||
// Disabled by default for custom page templates, page on front and page for posts. | ||
default: | ||
$enabled = ( | ||
! (bool) get_page_template_slug( $post ) | ||
&& | ||
! ( | ||
'page' === $post->post_type | ||
&& | ||
'page' === get_option( 'show_on_front' ) | ||
&& | ||
in_array( (int) $post->ID, array( | ||
(int) get_option( 'page_on_front' ), | ||
(int) get_option( 'page_for_posts' ), | ||
), true ) | ||
) | ||
); | ||
|
||
/** | ||
* Filters whether default AMP status should be enabled or not. | ||
* | ||
* @since 0.6 | ||
* | ||
* @param string $status Status. | ||
* @param WP_Post $post Post. | ||
*/ | ||
return apply_filters( 'amp_post_status_default_enabled', $enabled, $post ); | ||
} | ||
return 0 === count( AMP_Post_Type_Support::get_support_errors( $post ) ); | ||
} | ||
|
||
/** | ||
* Are we currently on an AMP URL? | ||
* Determine whether the current response being served as AMP. | ||
* | ||
* @since 1.0 This function can be called before the `parse_query` action because the 'amp' query var is specifically and exclusively used when 'amp' theme support is added. | ||
* This function cannot be called before the parse_query action because it needs to be able | ||
* to determine the queried object is able to be served as AMP. | ||
* | ||
* @see is_header_video_active() | ||
* @return bool Whether it is the AMP endpoint. | ||
*/ | ||
function is_amp_endpoint() { | ||
global $wp_query; | ||
if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { | ||
return false; | ||
} | ||
|
||
// When 'amp' theme support is (or will be added) then these are the conditions that are key to be checked. | ||
if ( amp_is_canonical() || isset( $_GET[ amp_get_slug() ] ) ) { // WPCS: CSRF OK. | ||
return true; | ||
$did_parse_query = did_action( 'parse_query' ); | ||
|
||
if ( ! $did_parse_query ) { | ||
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( "is_amp_endpoint() was called before the 'parse_query' hook was called. This function will always return 'false' before the 'parse_query' hook is called.", 'amp' ) ), '0.4.2' ); | ||
} | ||
|
||
// Condition for non-theme support when /amp/ endpoint is used. | ||
if ( false !== get_query_var( amp_get_slug(), false ) ) { | ||
return true; | ||
$has_amp_query_var = ( | ||
isset( $_GET[ amp_get_slug() ] ) // WPCS: CSRF OK. | ||
|| | ||
false !== get_query_var( amp_get_slug(), false ) | ||
); | ||
|
||
// When there is no query var and AMP is not canonical/native, then this is definitely not an AMP endpoint. | ||
if ( ! $has_amp_query_var && ! amp_is_canonical() ) { | ||
return false; | ||
} | ||
|
||
return false; | ||
if ( current_theme_supports( 'amp' ) ) { | ||
$template_available = true === AMP_Theme_Support::get_template_availability(); | ||
return amp_is_canonical() ? $template_available : $has_amp_query_var; | ||
} else { | ||
|
||
// Check if the queried object supports AMP. | ||
if ( is_singular() || ( $wp_query instanceof WP_Query && $wp_query->is_posts_page ) ) { | ||
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. Good idea to check that |
||
/** | ||
* Post. | ||
* | ||
* @var WP_Post $queried_object | ||
*/ | ||
$queried_object = get_queried_object(); | ||
return post_supports_amp( $queried_object ); | ||
} | ||
|
||
return false; // Legacy templates only support posts via AMP_Post_Template. | ||
} | ||
} | ||
|
||
/** | ||
|
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.