Skip to content
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 amp option wp cli command to manage plugin options #7430

Merged
merged 59 commits into from
Feb 3, 2023

Conversation

thelovekesh
Copy link
Collaborator

@thelovekesh thelovekesh commented Jan 30, 2023

Summary

Fixes #5783
Previously #7368

Checklist

  • 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).

@thelovekesh thelovekesh changed the title Add amp option wp cli command to manage amp settings Add amp option wp cli command to manage plugin options Jan 30, 2023
@thelovekesh thelovekesh self-assigned this Jan 30, 2023
@thelovekesh thelovekesh added Enhancement New feature or improvement of an existing one WP-CLI WS:Core Work stream for Plugin core labels Jan 30, 2023
@thelovekesh thelovekesh added this to the v2.4 milestone Jan 30, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jan 30, 2023

Plugin builds for 68bc2ac are ready 🛎️!

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Just pulling in this previous PR review: #7368 (review)

src/Cli/OptionCommand.php Outdated Show resolved Hide resolved
src/Cli/OptionCommand.php Outdated Show resolved Hide resolved
src/Cli/OptionCommand.php Outdated Show resolved Hide resolved
Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Code looks good! Just a few more bits of feedback.

Co-authored-by: Weston Ruter <westonruter@google.com>
@thelovekesh thelovekesh force-pushed the add/plugin-settings-wp-cli-command branch from 8e82e22 to 0afc408 Compare February 2, 2023 19:22
* Remove .babelrc, .eslintrc files and  add *.feature file extension
… options to be managed by amp option command
Comment on lines +185 to +188
// Update type for some options.
if ( Option::SANDBOXING_LEVEL === $option_name ) {
$option_value = (int) $option_value;
}
Copy link
Member

Choose a reason for hiding this comment

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

Why was this specific cast needed? Is it because it is a string and the following logic requires it to be an int:

amp-wp/src/Sandboxing.php

Lines 123 to 129 in c28b8f1

if (
isset( $new_options[ Option::SANDBOXING_LEVEL ] )
&&
in_array( $new_options[ Option::SANDBOXING_LEVEL ], self::LEVELS, true )
) {
$options[ Option::SANDBOXING_LEVEL ] = $new_options[ Option::SANDBOXING_LEVEL ];
}

It seems like the Sandboxing service should be doing that cast to int as well, since it is already doing so for bool:

amp-wp/src/Sandboxing.php

Lines 120 to 122 in c28b8f1

if ( isset( $new_options[ Option::SANDBOXING_ENABLED ] ) ) {
$options[ Option::SANDBOXING_ENABLED ] = (bool) $new_options[ Option::SANDBOXING_ENABLED ];
}

Copy link
Collaborator Author

@thelovekesh thelovekesh Feb 3, 2023

Choose a reason for hiding this comment

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

I believe when we update options using REST API then it first validates the options(and return any validation failures) through provided schema. In the case of '0' and '1' and 'true' and 'false' with schema type as boolean, it runs it through the rest_sanitize_boolean first hence type cast is not required but in the case of enums it fails:

const LEVELS = [ 1, 2, 3 ];

Option::SANDBOXING_LEVEL => [
'type' => 'int',
'enum' => self::LEVELS,
'default' => 1,
],

In other words, it doesn't reach here:

$options = apply_filters( 'amp_options_updating', $options, $new_options );


It seems like the Sandboxing service should be doing that cast to int as well since it is already doing so for bool:

Type cast here is done by REST schema's rest_sanitize_boolean IMO.

@westonruter westonruter merged commit 68d9bb3 into develop Feb 3, 2023
@westonruter westonruter deleted the add/plugin-settings-wp-cli-command branch February 3, 2023 18:20
@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Feb 8, 2023
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. Enhancement New feature or improvement of an existing one WP-CLI WS:Core Work stream for Plugin core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add WP-CLI commands for managing AMP settings
2 participants