Skip to content

Initialize plugin_configured as true if plugin options are not empty#5042

Merged
westonruter merged 13 commits intodevelopfrom
fix/5020-empty-settings-on-upgrade
Jul 14, 2020
Merged

Initialize plugin_configured as true if plugin options are not empty#5042
westonruter merged 13 commits intodevelopfrom
fix/5020-empty-settings-on-upgrade

Conversation

@johnwatkins0
Copy link
Copy Markdown
Contributor

@johnwatkins0 johnwatkins0 commented Jul 14, 2020

Summary

Fixes #5020

For the setup wizard, we added a plugin_configured option. When false, certain values are initialized to null in the UI until the user makes decisions about plugin configuration.

We need to skip that behavior if the plugin was already configured at some point before plugin_configured was added.

With this change, when the plugin options from the database are not empty, we set plugin_configured to true. As a result, the UI will reflect previously selected options instead of initializing any fields to null.

Test steps

  1. Switch to a pre-1.6 version of the plugin -- e.g., checkout 1.5.5 -- reinstall composer and node modules, and rebuild
  2. Clear your options: wp option delete amp-options
  3. Visit the plugin settings page and save some settings.
  4. Checkout this branch, reinstall composer and node modules, and rebuild
  5. Visit the plugin settings page and confirm that your settings are not empty. They should also be reflected in the wizard

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@googlebot googlebot added the cla: yes Signed the Google CLA label Jul 14, 2020
@johnwatkins0 johnwatkins0 marked this pull request as ready for review July 14, 2020 15:24
@johnwatkins0 johnwatkins0 self-assigned this Jul 14, 2020
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jul 14, 2020

Plugin builds for 19fe024 are ready 🛎️!

@johnwatkins0 johnwatkins0 requested review from pierlon and westonruter and removed request for pierlon and westonruter July 14, 2020 15:55
@johnwatkins0
Copy link
Copy Markdown
Contributor Author

I need to check the failing e2e test.

Comment on lines +142 to +152
$options = get_option( AMP_Options_Manager::OPTION_NAME, [] );

// Initialize the plugin_configured option, setting it to true if options already exist.
if ( is_admin() && current_user_can( 'manage_options' ) ) {
if ( empty( $options ) ) {
AMP_Options_Manager::update_option( Option::PLUGIN_CONFIGURED, false );
} elseif ( ! isset( $options[ Option::PLUGIN_CONFIGURED ] ) ) {
AMP_Options_Manager::update_option( Option::PLUGIN_CONFIGURED, true );
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can handle this with default values in AMP_Options_Manager::get_options(). I'll make the change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go for it. I'll leave it alone.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does afaf9a6 do the trick?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite…

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I tried at first but it didn't work, I think because of this: https://github.com/ampproject/amp-wp/blob/develop/includes/amp-helper-functions.php#L145-L160

Once AMP_Options_Manager::update_option( Option::VERSION, AMP__VERSION ); is called, the options will no longer be empty, and that runs way before get_options is called again.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why I was trying to squeeze in the initialization of plugin_configured before that point.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be working for me.

If I reset the option to an old value:

echo '{"theme_support":"transitional","supported_post_types":["post"],"analytics":[],"all_templates_supported":false,"supported_templates":["is_singular"],"version":"1.5.5"}' | wp option set amp-options --format=json

Then:

$ wp eval 'var_dump(AMP_Options_Manager::get_option("plugin_configured"));'
bool(true)

Whereas if I delete the option entirely:

$ wp option delete amp-options
Success: Deleted 'amp-options' option.

Then I get:

$ wp eval 'var_dump(AMP_Options_Manager::get_option("plugin_configured"));'
bool(false)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, all right, I'll verify when I make the change for the settings page.

@westonruter
Copy link
Copy Markdown
Member

@johnwatkins0 One more thing:

When deleting the amp-options setting, the admin screen should show up with the default values populating the form, as opposed to now:

image

The wizard, however, should have its fields empty if plugin_configured is false.

@westonruter
Copy link
Copy Markdown
Member

The wizard, however, should have its fields empty if plugin_configured is false.

I think we need to pass some flag in \AmpProject\AmpWP\Admin\OptionsMenu::enqueue_assets() in POPULATE_DEFAULT_VALUES which will force the components to populate the values even if ! fetchedOptions.plugin_configured. So including something like this:

diff --git a/assets/src/components/options-context-provider/index.js b/assets/src/components/options-context-provider/index.js
index 678ab0205..7990d4ef2 100644
--- a/assets/src/components/options-context-provider/index.js
+++ b/assets/src/components/options-context-provider/index.js
@@ -68,7 +68,7 @@ export function OptionsContextProvider( { children, optionsRestEndpoint } ) {
 					return;
 				}
 
-				if ( fetchedOptions.plugin_configured === false ) {
+				if ( ! POPULATE_DEFAULT_VALUES && fetchedOptions.plugin_configured === false ) {
 					fetchedOptions.mobile_redirect = true;
 					fetchedOptions.reader_theme = null;
 					fetchedOptions.theme_support = null;
diff --git a/src/Admin/OptionsMenu.php b/src/Admin/OptionsMenu.php
index 828f62b0b..46e614776 100644
--- a/src/Admin/OptionsMenu.php
+++ b/src/Admin/OptionsMenu.php
@@ -241,6 +241,7 @@ class OptionsMenu implements Conditional, Service, Registerable {
 			],
 			'OPTIONS_REST_ENDPOINT'              => rest_url( 'amp/v1/options' ),
 			'READER_THEMES_REST_ENDPOINT'        => rest_url( 'amp/v1/reader-themes' ),
+			'POPULATE_DEFAULT_VALUES'            => true,
 			'IS_CORE_THEME'                      => in_array(
 				get_stylesheet(),
 				AMP_Core_Theme_Sanitizer::get_supported_themes(),

@westonruter
Copy link
Copy Markdown
Member

I think we need to pass some flag in \AmpProject\AmpWP\Admin\OptionsMenu::enqueue_assets() in POPULATE_DEFAULT_VALUES which will force the components to populate the values even if ! fetchedOptions.plugin_configured. So including something like this:

I'm giving this a shot.

@westonruter
Copy link
Copy Markdown
Member

@johnwatkins0 there, please check cdc872e.

Here's the matrix for how the wizard and settings screen look (as expected):

Screen Options Empty (new install) Pre-populated Options (upgrading from 1.5.5)
Settings image image
Wizard image image

@westonruter
Copy link
Copy Markdown
Member

(I'll need help with adapting/adding E2E tests.)

@johnwatkins0
Copy link
Copy Markdown
Contributor Author

johnwatkins0 commented Jul 14, 2020

@westonruter The code looks right. I'll fix these e2e tests that are now expectedly failing. But -- isn't it supposed to default to Reader mode when options are empty? That's how mine works locally, which differs from your screenshot.

@johnwatkins0
Copy link
Copy Markdown
Contributor Author

For me, on initial load with empty options, the settings screen has Reader mode selected with the legacy theme selected.

@westonruter
Copy link
Copy Markdown
Member

If you have an AMP-compatible theme active, then the default mode should be Standard or Transitional per this logic here:

// 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;
}
}

What theme do you have active?

@westonruter
Copy link
Copy Markdown
Member

I see there is also a bug in AMP_Core_Theme_Sanitizer::extend_theme_support() in that it will fail to add amp theme support if a core theme doesn't have any special theme support args. This resulted in Twenty Sixteen getting Transitional mode by default, but Twenty Twenty did not. I'll fix.

@johnwatkins0
Copy link
Copy Markdown
Contributor Author

Yeah, I was using Twenty Twenty and getting Reader by default.

@westonruter
Copy link
Copy Markdown
Member

@johnwatkins0 Please check if 677c20e fixes the issue with the expected default mode.

@johnwatkins0
Copy link
Copy Markdown
Contributor Author

@johnwatkins0 Please check if 677c20e fixes the issue with the expected default mode.

Success:

  1. Using theme twenty twenty
  2. Pulled branch and rebuilt
  3. wp option delete amp-options
  4. Visit plugin settings screen
  5. Transitional selected

Screen Shot 2020-07-14 at 3 28 35 PM

@westonruter
Copy link
Copy Markdown
Member

There's one more bug I found that was missed from #5010: when a child theme of a core theme is active, we should not default to Transitional or Standard because all bets are off as to whether it actually supports AMP. Fixed in f8c8390. We'll only be able to change that once we have the theme scan feature (#4795).

@westonruter westonruter added this to the v1.6 milestone Jul 14, 2020
@westonruter westonruter merged commit 6c5cca2 into develop Jul 14, 2020
@westonruter westonruter deleted the fix/5020-empty-settings-on-upgrade branch July 14, 2020 21:15
@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Jul 17, 2020
@johnwatkins0 johnwatkins0 added the WS:UX Work stream for UX/Front-end label Aug 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelogged Whether the issue/PR has been added to release notes. cla: yes Signed the Google CLA WS:UX Work stream for UX/Front-end

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Settings screen fields are empty when first activating the plugin

3 participants