Skip to content

Commit

Permalink
Always allow switching themes with built-in Transitional support to N…
Browse files Browse the repository at this point in the history
…ative

Fixes #2312
  • Loading branch information
westonruter committed Jun 10, 2019
1 parent 82894fb commit bb506c3
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 88 deletions.
114 changes: 107 additions & 7 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ class AMP_Theme_Support {
*/
const CACHE_MISS_URL_OPTION = 'amp_cache_miss_url';

/**
* Slug identifying native mode.
*
* @since 1.2
* @var string
*/
const NATIVE_MODE_SLUG = 'native';

/**
* Slug identifying transitional mode.
*
* @since 1.2
* @var string
*/
const TRANSITIONAL_MODE_SLUG = 'paired';

/**
* Sanitizer classes.
*
Expand Down Expand Up @@ -110,12 +126,23 @@ class AMP_Theme_Support {
protected static $is_output_buffering = false;

/**
* Theme support options that were added via option.
* Theme support mode that was added via option.
*
* This should be either null (reader), 'native', or 'transitional'.
*
* @since 1.0
* @var bool
* @var null|string
*/
protected static $support_added_via_option = false;
protected static $support_added_via_option = null;

/**
* Theme support mode which was added via the theme.
*
* This should be either null (reader), 'native', or 'transitional'.
*
* @var null|string
*/
protected static $support_added_via_theme = null;

/**
* Initialize.
Expand Down Expand Up @@ -159,22 +186,78 @@ function () {
*
* @since 1.0
* @see AMP_Theme_Support::read_theme_support()
* @see AMP_Theme_Support::get_support_mode()
* @deprecated Use AMP_Theme_Support::get_support_mode_added_via_option().
*
* @return bool Support added via option.
*/
public static function is_support_added_via_option() {
_deprecated_function( __METHOD__, '1.2', 'AMP_Theme_Support::get_support_mode_added_via_option' );
return null !== self::$support_added_via_option;
}

/**
* Get the theme support mode added via admin option.
*
* @since 1.2
* @see AMP_Theme_Support::read_theme_support()
* @see AMP_Theme_Support::TRANSITIONAL_MODE_SLUG
* @see AMP_Theme_Support::NATIVE_MODE_SLUG
*
* @return null|string Support added via option, with null meaning Reader, and otherwise being 'native' or 'paired'.
*/
public static function get_support_mode_added_via_option() {
return self::$support_added_via_option;
}

/**
* Get the theme support mode added via admin option.
*
* @since 1.2
* @see AMP_Theme_Support::read_theme_support()
* @see AMP_Theme_Support::TRANSITIONAL_MODE_SLUG
* @see AMP_Theme_Support::NATIVE_MODE_SLUG
*
* @return null|string Support added via option, with null meaning Reader, and otherwise being 'native' or 'paired'.
*/
public static function get_support_mode_added_via_theme() {
return self::$support_added_via_theme;
}

/**
* Get theme support mode.
*
* @since 1.2
* @see AMP_Theme_Support::read_theme_support()
* @see AMP_Theme_Support::TRANSITIONAL_MODE_SLUG
* @see AMP_Theme_Support::NATIVE_MODE_SLUG
*
* @return string Theme support mode.
*/
public static function get_support_mode() {
$theme_support = self::get_support_mode_added_via_option();
if ( ! $theme_support ) {
$theme_support = self::get_support_mode_added_via_theme();
}
if ( ! $theme_support ) {
$theme_support = 'disabled';
}
return $theme_support;
}

/**
* Check theme support args or add theme support if option is set in the admin.
*
* The DB option is only considered if the theme does not already explicitly support AMP.
*
* @see AMP_Theme_Support::is_support_added_via_option()
* @see AMP_Theme_Support::get_support_mode_added_via_theme()
* @see AMP_Theme_Support::get_support_mode_added_via_option()
* @see AMP_Post_Type_Support::add_post_type_support() For where post type support is added, since it is irrespective of theme support.
*/
public static function read_theme_support() {
self::$support_added_via_theme = null;
self::$support_added_via_option = null;

$theme_support_option = AMP_Options_Manager::get_option( 'theme_support' );
if ( current_theme_supports( self::SLUG ) ) {
$args = self::get_theme_support_args();
Expand Down Expand Up @@ -209,16 +292,33 @@ public static function read_theme_support() {
'1.0'
);
}
self::$support_added_via_option = false;

$is_paired = ! empty( $args['paired'] );

self::$support_added_via_theme = $is_paired ? self::TRANSITIONAL_MODE_SLUG : self::NATIVE_MODE_SLUG;

/*
* If the theme has transitional support, allow the user to opt for native mode via an option, since a theme
* in transitional mode entails that it supports serving templates as both AMP and non-AMP, and this it is
* able to serve AMP-first pages just as well as paired pages. Otherwise, consider that the the mode was
* not set at all via option.
*/
self::$support_added_via_option = ( $is_paired && self::NATIVE_MODE_SLUG === $theme_support_option ) ? self::NATIVE_MODE_SLUG : null;
if ( self::NATIVE_MODE_SLUG === self::$support_added_via_option ) {
$args['paired'] = false;
add_theme_support( 'amp', $args );
}
} elseif ( 'disabled' !== $theme_support_option ) {
$is_paired = ( 'paired' === $theme_support_option );
add_theme_support(
self::SLUG,
array(
'paired' => ( 'paired' === $theme_support_option ),
'paired' => $is_paired,
)
);
self::$support_added_via_option = true;
self::$support_added_via_option = $is_paired ? self::TRANSITIONAL_MODE_SLUG : self::NATIVE_MODE_SLUG;
} elseif ( AMP_Validation_Manager::is_theme_support_forced() ) {
self::$support_added_via_option = self::NATIVE_MODE_SLUG;
add_theme_support( self::SLUG );
}
}
Expand Down
145 changes: 74 additions & 71 deletions includes/options/class-amp-options-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,33 +149,27 @@ public function add_menu_items() {
* @since 1.0
*/
public function render_theme_support() {
$theme_support = AMP_Options_Manager::get_option( 'theme_support' );
$theme_support = AMP_Theme_Support::get_support_mode();

/* translators: %s: URL to the documentation. */
$native_description = sprintf( __( 'Integrates AMP as the framework for your site by using the active’s theme templates and styles to render AMP responses. This means your site is <b>AMP-first</b> and your canonical URLs are AMP! Depending on your theme/plugins, a varying level of <a href="%s">development work</a> may be required.', 'amp' ), esc_url( 'https://amp-wp.org/documentation/developing-wordpress-amp-sites/' ) );
$native_description = sprintf( __( 'The active theme integrates AMP as the framework for your site by using its templates and styles to render webpages. This means your site is <b>AMP-first</b> and your canonical URLs are AMP! Depending on your theme/plugins, a varying level of <a href="%s">development work</a> may be required.', 'amp' ), esc_url( 'https://amp-wp.org/documentation/developing-wordpress-amp-sites/' ) );
/* translators: %s: URL to the documentation. */
$transitional_description = sprintf( __( 'Uses the active theme’s templates to generate non-AMP and AMP versions of your content, allowing for each canonical URL to have a corresponding (paired) AMP URL. This mode is useful to progressively transition towards a fully AMP-first site. Depending on your theme/plugins, a varying level of <a href="%s">development work</a> may be required.', 'amp' ), esc_url( 'https://amp-wp.org/documentation/developing-wordpress-amp-sites/' ) );
$transitional_description = sprintf( __( 'The active theme’s templates are used to generate non-AMP and AMP versions of your content, allowing for each canonical URL to have a corresponding (paired) AMP URL. This mode is useful to progressively transition towards a fully AMP-first site. Depending on your theme/plugins, a varying level of <a href="%s">development work</a> may be required.', 'amp' ), esc_url( 'https://amp-wp.org/documentation/developing-wordpress-amp-sites/' ) );
$reader_description = __( 'Formerly called the <b>classic mode</b>, this mode generates paired AMP content using simplified templates which may not match the look-and-feel of your site. Only posts/pages can be served as AMP in Reader mode. No redirection is performed for mobile visitors; AMP pages are served by AMP consumption platforms.', 'amp' );
/* translators: %s: URL to the ecosystem page. */
$ecosystem_description = sprintf( __( 'For a list of themes and plugins that are known to be AMP compatible, please see the <a href="%s">ecosystem page</a>.' ), esc_url( 'https://amp-wp.org/ecosystem/' ) );

$builtin_support = in_array( get_template(), AMP_Core_Theme_Sanitizer::get_supported_themes(), true );
?>
<?php if ( current_theme_supports( AMP_Theme_Support::SLUG ) && ! AMP_Theme_Support::is_support_added_via_option() ) : ?>
<?php if ( AMP_Theme_Support::NATIVE_MODE_SLUG === AMP_Theme_Support::get_support_mode_added_via_theme() ) : ?>
<div class="notice notice-info notice-alt inline">
<p><?php esc_html_e( 'Your active theme has built-in AMP support.', 'amp' ); ?></p>
</div>
<p>
<?php echo wp_kses_post( $ecosystem_description ); ?>
<?php echo wp_kses_post( $native_description ); ?>
</p>
<p>
<?php if ( amp_is_canonical() ) : ?>
<strong><?php esc_html_e( 'Native:', 'amp' ); ?></strong>
<?php echo wp_kses_post( $native_description ); ?>
<?php else : ?>
<strong><?php esc_html_e( 'Transitional:', 'amp' ); ?></strong>
<?php echo wp_kses_post( $transitional_description ); ?>
<?php endif; ?>
<?php echo wp_kses_post( $ecosystem_description ); ?>
</p>
<?php else : ?>
<fieldset <?php disabled( ! current_user_can( 'manage_options' ) ); ?>>
Expand All @@ -184,12 +178,15 @@ public function render_theme_support() {
<p><?php esc_html_e( 'Your active theme is known to work well in transitional or native mode.', 'amp' ); ?></p>
</div>
<?php endif; ?>
<p>
<?php echo wp_kses_post( $ecosystem_description ); ?>
</p>

<?php if ( ! AMP_Theme_Support::get_support_mode_added_via_theme() ) : ?>
<p>
<?php echo wp_kses_post( $ecosystem_description ); ?>
</p>
<?php endif; ?>
<dl>
<dt>
<input type="radio" id="theme_support_native" name="<?php echo esc_attr( AMP_Options_Manager::OPTION_NAME . '[theme_support]' ); ?>" value="native" <?php checked( $theme_support, 'native' ); ?>>
<input type="radio" id="theme_support_native" name="<?php echo esc_attr( AMP_Options_Manager::OPTION_NAME . '[theme_support]' ); ?>" value="native" <?php checked( $theme_support, AMP_Theme_Support::NATIVE_MODE_SLUG ); ?>>
<label for="theme_support_native">
<strong><?php esc_html_e( 'Native', 'amp' ); ?></strong>
</label>
Expand All @@ -198,57 +195,65 @@ public function render_theme_support() {
<?php echo wp_kses_post( $native_description ); ?>
</dd>
<dt>
<input type="radio" id="theme_support_transitional" name="<?php echo esc_attr( AMP_Options_Manager::OPTION_NAME . '[theme_support]' ); ?>" value="paired" <?php checked( $theme_support, 'paired' ); ?>>
<input type="radio" id="theme_support_transitional" name="<?php echo esc_attr( AMP_Options_Manager::OPTION_NAME . '[theme_support]' ); ?>" value="paired" <?php checked( $theme_support, AMP_Theme_Support::TRANSITIONAL_MODE_SLUG ); ?>>
<label for="theme_support_transitional">
<strong><?php esc_html_e( 'Transitional', 'amp' ); ?></strong>
</label>
</dt>
<dd>
<?php echo wp_kses_post( $transitional_description ); ?>
</dd>
<dt>
<input type="radio" id="theme_support_disabled" name="<?php echo esc_attr( AMP_Options_Manager::OPTION_NAME . '[theme_support]' ); ?>" value="disabled" <?php checked( $theme_support, 'disabled' ); ?>>
<label for="theme_support_disabled">
<strong><?php esc_html_e( 'Reader', 'amp' ); ?></strong>
</label>
</dt>
<dd>
<?php echo wp_kses_post( $reader_description ); ?>

<?php if ( ! current_theme_supports( AMP_Theme_Support::SLUG ) && wp_count_posts( AMP_Validated_URL_Post_Type::POST_TYPE_SLUG )->publish > 0 ) : ?>
<div class="notice notice-info inline notice-alt">
<p>
<?php
echo wp_kses_post(
sprintf(
/* translators: %1: link to invalid URLs. 2: link to validation errors. */
__( 'View current site compatibility results for native and transitional modes: %1$s and %2$s.', 'amp' ),
<?php if ( ! AMP_Theme_Support::get_support_mode_added_via_theme() ) : ?>
<dt>
<input type="radio" id="theme_support_disabled" name="<?php echo esc_attr( AMP_Options_Manager::OPTION_NAME . '[theme_support]' ); ?>" value="disabled" <?php checked( $theme_support, 'disabled' ); ?>>
<label for="theme_support_disabled">
<strong><?php esc_html_e( 'Reader', 'amp' ); ?></strong>
</label>
</dt>
<dd>
<?php echo wp_kses_post( $reader_description ); ?>

<?php if ( ! current_theme_supports( AMP_Theme_Support::SLUG ) && wp_count_posts( AMP_Validated_URL_Post_Type::POST_TYPE_SLUG )->publish > 0 ) : ?>
<div class="notice notice-info inline notice-alt">
<p>
<?php
echo wp_kses_post(
sprintf(
'<a href="%s">%s</a>',
esc_url( add_query_arg( 'post_type', AMP_Validated_URL_Post_Type::POST_TYPE_SLUG, admin_url( 'edit.php' ) ) ),
esc_html( get_post_type_object( AMP_Validated_URL_Post_Type::POST_TYPE_SLUG )->labels->name )
),
sprintf(
'<a href="%s">%s</a>',
esc_url(
add_query_arg(
array(
'taxonomy' => AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG,
'post_type' => AMP_Validated_URL_Post_Type::POST_TYPE_SLUG,
),
admin_url( 'edit-tags.php' )
)
/* translators: %1: link to invalid URLs. 2: link to validation errors. */
__( 'View current site compatibility results for native and transitional modes: %1$s and %2$s.', 'amp' ),
sprintf(
'<a href="%s">%s</a>',
esc_url( add_query_arg( 'post_type', AMP_Validated_URL_Post_Type::POST_TYPE_SLUG, admin_url( 'edit.php' ) ) ),
esc_html( get_post_type_object( AMP_Validated_URL_Post_Type::POST_TYPE_SLUG )->labels->name )
),
esc_html( get_taxonomy( AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG )->labels->name )
sprintf(
'<a href="%s">%s</a>',
esc_url(
add_query_arg(
array(
'taxonomy' => AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG,
'post_type' => AMP_Validated_URL_Post_Type::POST_TYPE_SLUG,
),
admin_url( 'edit-tags.php' )
)
),
esc_html( get_taxonomy( AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG )->labels->name )
)
)
)
);
?>
</p>
</div>
<?php endif; ?>
</dd>
);
?>
</p>
</div>
<?php endif; ?>
</dd>
<?php endif; ?>
</dl>

<?php if ( AMP_Theme_Support::get_support_mode_added_via_theme() ) : ?>
<p>
<?php echo wp_kses_post( $ecosystem_description ); ?>
</p>
<?php endif; ?>
</fieldset>
<?php endif; ?>
<?php
Expand Down Expand Up @@ -314,26 +319,26 @@ public function render_validation_handling() {
<?php endif; ?>

<script>
(function( $ ) {
var getThemeSupportMode = function() {
var checkedInput = $( 'input[type=radio][name="amp-options[theme_support]"]:checked' );
(function( $, nativeModeSlug ) {
const getThemeSupportMode = () => {
const checkedInput = $( 'input[type=radio][name="amp-options[theme_support]"]:checked' );
if ( 0 === checkedInput.length ) {
return <?php echo wp_json_encode( amp_is_canonical() ? 'native' : 'paired' ); ?>;
return nativeModeSlug;
}
return checkedInput.val();
};

var updateHiddenClasses = function() {
var themeSupportMode = getThemeSupportMode();
$( '.amp-auto-accept-sanitize' ).toggleClass( 'hidden', 'native' === themeSupportMode );
const themeSupportMode = getThemeSupportMode();
$( '.amp-auto-accept-sanitize' ).toggleClass( 'hidden', nativeModeSlug === themeSupportMode );
$( '.amp-validation-field' ).toggleClass( 'hidden', 'disabled' === themeSupportMode );
$( '.amp-auto-accept-sanitize-canonical' ).toggleClass( 'hidden', 'native' !== themeSupportMode );
$( '.amp-auto-accept-sanitize-canonical' ).toggleClass( 'hidden', nativeModeSlug !== themeSupportMode );
};

$( 'input[type=radio][name="amp-options[theme_support]"]' ).change( updateHiddenClasses );

updateHiddenClasses();
})( jQuery );
})( jQuery, <?php echo wp_json_encode( AMP_Theme_Support::NATIVE_MODE_SLUG ); ?> );
</script>
</fieldset>
<?php
Expand Down Expand Up @@ -431,18 +436,16 @@ public function render_supported_templates() {
<script>
// Update the visibility of the fieldsets based on the selected template mode and then whether all templates are indicated to be supported.
(function ( $ ) {
var templateModeInputs, themeSupportDisabledInput, allTemplatesSupportedInput, supportForced;
templateModeInputs = $( 'input[type=radio][name="amp-options[theme_support]"]' );
themeSupportDisabledInput = $( '#theme_support_disabled' );
allTemplatesSupportedInput = $( '#all_templates_supported' );
supportForced = <?php echo wp_json_encode( current_theme_supports( AMP_Theme_Support::SLUG ) && ! AMP_Theme_Support::is_support_added_via_option() ); ?>;
const templateModeInputs = $( 'input[type=radio][name="amp-options[theme_support]"]' );
const themeSupportDisabledInput = $( '#theme_support_disabled' );
const allTemplatesSupportedInput = $( '#all_templates_supported' );

function isThemeSupportDisabled() {
return ! supportForced && themeSupportDisabledInput.prop( 'checked' );
return Boolean( themeSupportDisabledInput.length && themeSupportDisabledInput.prop( 'checked' ) );
}

function updateFieldsetVisibility() {
var allTemplatesSupported = 0 === allTemplatesSupportedInput.length || allTemplatesSupportedInput.prop( 'checked' );
const allTemplatesSupported = 0 === allTemplatesSupportedInput.length || allTemplatesSupportedInput.prop( 'checked' );
$( '#all_templates_supported_fieldset, #supported_post_types_fieldset > .title' ).toggleClass(
'hidden',
isThemeSupportDisabled()
Expand Down
Loading

0 comments on commit bb506c3

Please sign in to comment.