Skip to content

Commit

Permalink
Prevent defaulting to Transitional mode when child theme of core them…
Browse files Browse the repository at this point in the history
…e active
  • Loading branch information
westonruter committed Jul 14, 2020
1 parent 384f6eb commit f8c8390
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
26 changes: 20 additions & 6 deletions includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,26 @@ public static function get_options() {
}

// Migrate legacy method of specifying the mode.
$theme_support = AMP_Theme_Support::get_theme_support_args();
if ( $theme_support && ! isset( $options[ Option::THEME_SUPPORT ] ) ) {
if ( empty( $theme_support[ AMP_Theme_Support::PAIRED_FLAG ] ) ) {
$defaults[ Option::THEME_SUPPORT ] = AMP_Theme_Support::STANDARD_MODE_SLUG;
} else {
$defaults[ Option::THEME_SUPPORT ] = AMP_Theme_Support::TRANSITIONAL_MODE_SLUG;
if ( ! isset( $options[ Option::THEME_SUPPORT ] ) ) {
$theme_support = AMP_Theme_Support::get_theme_support_args();
$template = get_template();
$stylesheet = get_stylesheet();
if (
$theme_support
&&
(
// If theme support was probably explicitly added by the theme (since not core).
! in_array( $template, AMP_Core_Theme_Sanitizer::get_supported_themes(), true )
||
// If it is a core theme no child theme is being used (which likely won't be AMP-compatible by default).
$template === $stylesheet
)
) {
if ( empty( $theme_support[ AMP_Theme_Support::PAIRED_FLAG ] ) ) {
$defaults[ Option::THEME_SUPPORT ] = AMP_Theme_Support::STANDARD_MODE_SLUG;
} else {
$defaults[ Option::THEME_SUPPORT ] = AMP_Theme_Support::TRANSITIONAL_MODE_SLUG;
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/php/data/themes/custom/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
the_post();
56 changes: 56 additions & 0 deletions tests/php/test-class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Test_AMP_Options_Manager extends WP_UnitTestCase {
*/
private $was_wp_using_ext_object_cache;

private $original_theme_directories;

/**
* Set up.
*/
Expand All @@ -33,6 +35,11 @@ public function setUp() {
delete_option( AMP_Options_Manager::OPTION_NAME ); // Make sure default reader mode option does not override theme support being added.
remove_theme_support( 'amp' );
$GLOBALS['wp_settings_errors'] = [];

global $wp_theme_directories;
$this->original_theme_directories = $wp_theme_directories;
register_theme_directory( ABSPATH . 'wp-content/themes' );
delete_site_transient( 'theme_roots' );
}

/**
Expand All @@ -46,6 +53,10 @@ public function tearDown() {
foreach ( get_post_types() as $post_type ) {
remove_post_type_support( $post_type, 'amp' );
}

global $wp_theme_directories;
$wp_theme_directories = $this->original_theme_directories;
delete_site_transient( 'theme_roots' );
}

/**
Expand Down Expand Up @@ -322,6 +333,51 @@ public function test_get_options_changing_plugin_configured_default() {
$this->assertTrue( AMP_Options_Manager::get_option( Option::PLUGIN_CONFIGURED ) );
}

/** @return array */
public function get_data_for_testing_get_options_default_template_mode() {
return [
'core_theme' => [
'twentytwenty',
AMP_Theme_Support::TRANSITIONAL_MODE_SLUG,
null,
],
'child_of_core' => [
'child-of-core',
AMP_Theme_Support::READER_MODE_SLUG,
null,
],
'custom_theme' => [
'twentytwenty',
AMP_Theme_Support::TRANSITIONAL_MODE_SLUG,
[],
],
];
}

/**
* Test the expected default mode when various themes are active.
*
* @dataProvider get_data_for_testing_get_options_default_template_mode
*
* @covers AMP_Options_Manager::get_options()
* @param string $theme Theme.
* @param string $expected_mode Expected mode.
* @param null|array $added_theme_support Added theme support (or not if null).
*/
public function test_get_options_default_template_mode( $theme, $expected_mode, $added_theme_support ) {
$theme_dir = basename( dirname( AMP__DIR__ ) ) . '/' . basename( AMP__DIR__ ) . '/tests/php/data/themes';
register_theme_directory( $theme_dir );

delete_option( AMP_Options_Manager::OPTION_NAME );
remove_theme_support( 'amp' );
switch_theme( $theme );
if ( is_array( $added_theme_support ) ) {
add_theme_support( 'amp', $added_theme_support );
}
AMP_Core_Theme_Sanitizer::extend_theme_support();
$this->assertEquals( $expected_mode, AMP_Options_Manager::get_option( Option::THEME_SUPPORT ) );
}

/**
* Test get_options when supported_post_types option is list of post types and when post type support is added for default values.
*
Expand Down

0 comments on commit f8c8390

Please sign in to comment.