Skip to content

Commit

Permalink
Merge pull request #6042 from ampproject/fix/5229-theme-color-not-ava…
Browse files Browse the repository at this point in the history
…il-in-legacy-reader-template

Reader mode: Generate styles for color palette, gradient presets, and font sizes from the theme support features of the primary theme
  • Loading branch information
westonruter committed Apr 23, 2021
2 parents ae70adc + 8462653 commit bd9f563
Show file tree
Hide file tree
Showing 11 changed files with 898 additions and 10 deletions.
1 change: 1 addition & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'plugin_registry' => \AmpProject\AmpWP\PluginRegistry::class,
'plugin_suppression' => \AmpProject\AmpWP\PluginSuppression::class,
'reader_theme_loader' => \AmpProject\AmpWP\ReaderThemeLoader::class,
'reader_theme_support_features' => \AmpProject\AmpWP\ReaderThemeSupportFeatures::class,
'rest.options_controller' => \AmpProject\AmpWP\OptionsRESTController::class,
'rest.validation_counts_controller' => \AmpProject\AmpWP\Validation\ValidationCountsRestController::class,
'server_timing' => \AmpProject\AmpWP\Instrumentation\ServerTiming::class,
Expand Down
5 changes: 3 additions & 2 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ function () {
$old_version = isset( $options[ Option::VERSION ] ) ? $options[ Option::VERSION ] : '0.0';

if ( AMP__VERSION !== $old_version && is_admin() && current_user_can( 'manage_options' ) ) {
// This waits to happen until the very end of init to ensure that amp theme support and amp post type support have all been added.
// This waits to happen until the very end of admin_init to ensure that amp theme support and amp post type
// support have all been added, and that the settings have been registered.
add_action(
'init',
'admin_init',
static function () use ( $old_version ) {
/**
* Triggers when after amp_init when the plugin version has updated.
Expand Down
2 changes: 1 addition & 1 deletion includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ static function ( $supported ) {
public static function get_option( $option, $default = false ) {
$amp_options = self::get_options();

if ( ! isset( $amp_options[ $option ] ) ) {
if ( ! array_key_exists( $option, $amp_options ) ) {
return $default;
}

Expand Down
2 changes: 2 additions & 0 deletions src/AmpWpPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ final class AmpWpPlugin extends ServiceBasedPlugin {
'plugin_registry' => PluginRegistry::class,
'plugin_suppression' => PluginSuppression::class,
'reader_theme_loader' => ReaderThemeLoader::class,
'reader_theme_support_features' => ReaderThemeSupportFeatures::class,
'rest.options_controller' => OptionsRESTController::class,
'rest.validation_counts_controller' => Validation\ValidationCountsRestController::class,
'server_timing' => Instrumentation\ServerTiming::class,
Expand Down Expand Up @@ -193,6 +194,7 @@ protected function get_shared_instances() {
DevTools\CallbackReflection::class,
DevTools\FileReflection::class,
ReaderThemeLoader::class,
ReaderThemeSupportFeatures::class,
BackgroundTask\BackgroundTaskDeactivator::class,
PairedRouting::class,
Injector::class,
Expand Down
10 changes: 10 additions & 0 deletions src/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ interface Option {
*/
const READER_THEME = 'reader_theme';

/**
* Theme support features from the primary theme.
*
* When using a Reader theme, the theme support features from the primary theme are stored in this option so that
* they will be available when the Reader theme is active.
*
* @var string
*/
const PRIMARY_THEME_SUPPORT = 'primary_theme_support';

/**
* The key of the option storing whether the setup wizard has been completed.
*
Expand Down
18 changes: 15 additions & 3 deletions src/ReaderThemeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,34 @@ public function __construct( PairedRouting $paired_routing ) {
/**
* Is Reader mode with a Reader theme selected.
*
* @param array $options Options to check. If omitted, the currently-saved options are used.
* @return bool Whether new Reader mode.
*/
public function is_enabled() {
public function is_enabled( $options = null ) {
// If the theme was overridden then we know it is enabled. We can't check get_template() at this point because
// it will be identical to $reader_theme.
if ( $this->is_theme_overridden() ) {
return true;
}

if ( null === $options ) {
$options = AMP_Options_Manager::get_options();
}

// If Reader mode is not enabled, then a Reader theme is definitely not going to be served.
if ( AMP_Theme_Support::READER_MODE_SLUG !== AMP_Options_Manager::get_option( Option::THEME_SUPPORT ) ) {
if (
! isset( $options[ Option::THEME_SUPPORT ] )
||
AMP_Theme_Support::READER_MODE_SLUG !== $options[ Option::THEME_SUPPORT ]
) {
return false;
}

// If the Legacy Reader mode is active, then a Reader theme is not going to be served.
$reader_theme = AMP_Options_Manager::get_option( Option::READER_THEME );
if ( ! isset( $options[ Option::READER_THEME ] ) ) {
return false;
}
$reader_theme = $options[ Option::READER_THEME ];
if ( ReaderThemes::DEFAULT_READER_THEME === $reader_theme ) {
return false;
}
Expand Down
Loading

0 comments on commit bd9f563

Please sign in to comment.