Skip to content

Commit

Permalink
Merge pull request #4545 from ampproject/task/4526-elevate-known-opti…
Browse files Browse the repository at this point in the history
…ons-to-interface

Elevate all known options into AmpProject\AmpWP\Option interface
  • Loading branch information
westonruter authored Apr 15, 2020
2 parents 4151308 + 08380ff commit 49915b4
Show file tree
Hide file tree
Showing 21 changed files with 314 additions and 161 deletions.
3 changes: 2 additions & 1 deletion includes/admin/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use AmpProject\AmpWP\Admin\SiteHealth;
use AmpProject\AmpWP\Option;

/**
* Obsolete constant for flagging when Customizer is opened for AMP.
Expand All @@ -22,7 +23,7 @@
* And this does not need to toggle between the AMP and normal display.
*/
function amp_init_customizer() {
if ( AMP_Theme_Support::READER_MODE_SLUG !== AMP_Options_Manager::get_option( 'theme_support' ) ) {
if ( AMP_Theme_Support::READER_MODE_SLUG !== AMP_Options_Manager::get_option( Option::THEME_SUPPORT ) ) {
return;
}

Expand Down
9 changes: 5 additions & 4 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @package AMP
*/

use AmpProject\AmpWP\Option;
use AmpProject\AmpWP\Services;

/**
Expand Down Expand Up @@ -118,20 +119,20 @@ function amp_init() {

/*
* Broadcast plugin updates.
* Note that AMP_Options_Manager::get_option( 'version', '0.0' ) cannot be used because
* Note that AMP_Options_Manager::get_option( Option::VERSION, '0.0' ) cannot be used because
* version was new option added, and in that case default would never be used for a site
* upgrading from a version prior to 1.0. So this is why get_option() is currently used.
*/
$options = get_option( AMP_Options_Manager::OPTION_NAME, [] );
$old_version = isset( $options['version'] ) ? $options['version'] : '0.0';
$old_version = isset( $options[ Option::VERSION ] ) ? $options[ Option::VERSION ] : '0.0';
if ( AMP__VERSION !== $old_version && is_admin() && current_user_can( 'manage_options' ) ) {
/**
* Triggers when after amp_init when the plugin version has updated.
*
* @param string $old_version Old version.
*/
do_action( 'amp_plugin_update', $old_version );
AMP_Options_Manager::update_option( 'version', AMP__VERSION );
AMP_Options_Manager::update_option( Option::VERSION, AMP__VERSION );
}
}

Expand Down Expand Up @@ -948,7 +949,7 @@ function amp_filter_font_style_loader_tag_with_crossorigin_anonymous( $tag, $han
* @return array Analytics.
*/
function amp_get_analytics( $analytics = [] ) {
$analytics_entries = AMP_Options_Manager::get_option( 'analytics', [] );
$analytics_entries = AMP_Options_Manager::get_option( Option::ANALYTICS, [] );

/**
* Add amp-analytics tags.
Expand Down
6 changes: 4 additions & 2 deletions includes/class-amp-post-type-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* @since 0.6
*/

use AmpProject\AmpWP\Option;

/**
* Class AMP_Post_Type_Support.
*/
Expand Down Expand Up @@ -56,10 +58,10 @@ public static function get_eligible_post_types() {
* @since 0.6
*/
public static function add_post_type_support() {
if ( current_theme_supports( AMP_Theme_Support::SLUG ) && AMP_Options_Manager::get_option( 'all_templates_supported' ) ) {
if ( current_theme_supports( AMP_Theme_Support::SLUG ) && AMP_Options_Manager::get_option( Option::ALL_TEMPLATES_SUPPORTED ) ) {
$post_types = self::get_eligible_post_types();
} else {
$post_types = AMP_Options_Manager::get_option( 'supported_post_types', [] );
$post_types = AMP_Options_Manager::get_option( Option::SUPPORTED_POST_TYPES, [] );
}
foreach ( $post_types as $post_type ) {
add_post_type_support( $post_type, self::SLUG );
Expand Down
11 changes: 6 additions & 5 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use AmpProject\Amp;
use AmpProject\AmpWP\Option;
use AmpProject\AmpWP\RemoteRequest\CachedRemoteGetRequest;
use AmpProject\AmpWP\ConfigurationArgument;
use AmpProject\AmpWP\Transformer;
Expand Down Expand Up @@ -253,7 +254,7 @@ 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' );
$theme_support_option = AMP_Options_Manager::get_option( Option::THEME_SUPPORT );
if ( current_theme_supports( self::SLUG ) ) {
$args = self::get_theme_support_args();

Expand Down Expand Up @@ -282,7 +283,7 @@ public static function read_theme_support() {
/* translators: 1: available_callback. 2: supported_templates */
esc_html__( 'The %1$s is deprecated when adding amp theme support in favor of declaratively setting the %2$s.', 'amp' ),
'available_callback',
'supported_templates'
Option::SUPPORTED_TEMPLATES // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
),
'1.0'
);
Expand Down Expand Up @@ -669,7 +670,7 @@ public static function get_template_availability( $query = null ) {
$all_templates_supported_by_theme_support = 'all' === $theme_support_args['templates_supported'];
}
$all_templates_supported = (
$all_templates_supported_by_theme_support || AMP_Options_Manager::get_option( 'all_templates_supported' )
$all_templates_supported_by_theme_support || AMP_Options_Manager::get_option( Option::ALL_TEMPLATES_SUPPORTED )
);

// Make sure global $wp_query is set in case of conditionals that unfortunately look at global scope.
Expand Down Expand Up @@ -1008,7 +1009,7 @@ public static function get_supportable_templates() {
$theme_supported_templates = $theme_support_args['templates_supported'];
}

$supported_templates = AMP_Options_Manager::get_option( 'supported_templates' );
$supported_templates = AMP_Options_Manager::get_option( Option::SUPPORTED_TEMPLATES );
foreach ( $templates as $id => &$template ) {

// Capture user-elected support from options. This allows us to preserve the original user selection through programmatic overrides.
Expand All @@ -1028,7 +1029,7 @@ public static function get_supportable_templates() {

// Set supported state from user preference.
if ( ! $template['immutable'] ) {
$template['supported'] = AMP_Options_Manager::get_option( 'all_templates_supported' ) || $template['user_supported'];
$template['supported'] = AMP_Options_Manager::get_option( Option::ALL_TEMPLATES_SUPPORTED ) || $template['user_supported'];
}
}

Expand Down
94 changes: 48 additions & 46 deletions includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class AMP_Options_Manager {
* @var array
*/
protected static $defaults = [
'theme_support' => AMP_Theme_Support::READER_MODE_SLUG,
'supported_post_types' => [ 'post' ],
'analytics' => [],
'all_templates_supported' => true,
'supported_templates' => [ 'is_singular' ],
'version' => AMP__VERSION,
Option::THEME_SUPPORT => AMP_Theme_Support::READER_MODE_SLUG,
Option::SUPPORTED_POST_TYPES => [ 'post' ],
Option::ANALYTICS => [],
Option::ALL_TEMPLATES_SUPPORTED => true,
Option::SUPPORTED_TEMPLATES => [ 'is_singular' ],
Option::VERSION => AMP__VERSION,
];

/**
Expand Down Expand Up @@ -61,8 +61,8 @@ public static function register_settings() {
* @param array $new_options New options.
*/
public static function maybe_flush_rewrite_rules( $old_options, $new_options ) {
$old_post_types = isset( $old_options['supported_post_types'] ) ? $old_options['supported_post_types'] : [];
$new_post_types = isset( $new_options['supported_post_types'] ) ? $new_options['supported_post_types'] : [];
$old_post_types = isset( $old_options[ Option::SUPPORTED_POST_TYPES ] ) ? $old_options[ Option::SUPPORTED_POST_TYPES ] : [];
$new_post_types = isset( $new_options[ Option::SUPPORTED_POST_TYPES ] ) ? $new_options[ Option::SUPPORTED_POST_TYPES ] : [];
sort( $old_post_types );
sort( $new_post_types );
if ( $old_post_types !== $new_post_types ) {
Expand All @@ -86,17 +86,17 @@ public static function get_options() {
$defaults = self::$defaults;

if ( current_theme_supports( 'amp' ) ) {
$defaults['theme_support'] = amp_is_canonical() ? AMP_Theme_Support::STANDARD_MODE_SLUG : AMP_Theme_Support::TRANSITIONAL_MODE_SLUG;
$defaults[ Option::THEME_SUPPORT ] = amp_is_canonical() ? AMP_Theme_Support::STANDARD_MODE_SLUG : AMP_Theme_Support::TRANSITIONAL_MODE_SLUG;
}

$options = array_merge( $defaults, $options );

// Migrate theme support slugs.
if ( 'native' === $options['theme_support'] ) {
$options['theme_support'] = AMP_Theme_Support::STANDARD_MODE_SLUG;
} elseif ( 'paired' === $options['theme_support'] ) {
$options['theme_support'] = AMP_Theme_Support::TRANSITIONAL_MODE_SLUG;
} elseif ( 'disabled' === $options['theme_support'] ) {
if ( 'native' === $options[ Option::THEME_SUPPORT ] ) {
$options[ Option::THEME_SUPPORT ] = AMP_Theme_Support::STANDARD_MODE_SLUG;
} elseif ( 'paired' === $options[ Option::THEME_SUPPORT ] ) {
$options[ Option::THEME_SUPPORT ] = AMP_Theme_Support::TRANSITIONAL_MODE_SLUG;
} elseif ( 'disabled' === $options[ Option::THEME_SUPPORT ] ) {
/*
* Prior to 1.2, the theme support slug for Reader mode was 'disabled'. This would be saved in options for
* themes that had 'amp' theme support defined. Also prior to 1.2, the user could not switch between modes
Expand All @@ -108,7 +108,7 @@ public static function get_options() {
* become 'transitional'. Otherwise, if the theme lacks 'amp' theme support, then this will become the
* default 'reader' mode.
*/
$options['theme_support'] = $defaults['theme_support'];
$options[ Option::THEME_SUPPORT ] = $defaults[ Option::THEME_SUPPORT ];
}

unset(
Expand All @@ -117,28 +117,30 @@ public static function get_options() {
*
* @since 1.4.0
*/
$options['auto_accept_sanitization'],
$options[ Option::AUTO_ACCEPT_SANITIZATION ],
/**
* Remove Story related options.
*
* Option::ENABLE_AMP_STORIES was added in 1.2-beta and later migrated into the `experiences` option.
*
* @since 1.5.0
*/
$options['story_templates_version'],
$options['story_export_base_url'],
$options['story_settings'],
$options['enable_amp_stories'], // This was added in 1.2-beta and later migrated into the `experiences` option.
$options[ Option::STORY_TEMPLATES_VERSION ],
$options[ Option::STORY_EXPORT_BASE_URL ],
$options[ Option::STORY_SETTINGS ],
$options[ Option::ENABLE_AMP_STORIES ],
/**
* Remove 'experiences' option.
*
* @since 1.5.0
*/
$options['experiences'],
$options[ Option::EXPERIENCES ],
/**
* Remove 'enable_response_caching' option.
*
* @since 1.5.0
*/
$options['enable_response_caching']
$options[ Option::ENABLE_RESPONSE_CACHING ]
);

return $options;
Expand Down Expand Up @@ -181,24 +183,24 @@ public static function validate_options( $new_options ) {
AMP_Theme_Support::TRANSITIONAL_MODE_SLUG,
AMP_Theme_Support::STANDARD_MODE_SLUG,
];
if ( isset( $new_options['theme_support'] ) && in_array( $new_options['theme_support'], $recognized_theme_supports, true ) ) {
$options['theme_support'] = $new_options['theme_support'];
if ( isset( $new_options[ Option::THEME_SUPPORT ] ) && in_array( $new_options[ Option::THEME_SUPPORT ], $recognized_theme_supports, true ) ) {
$options[ Option::THEME_SUPPORT ] = $new_options[ Option::THEME_SUPPORT ];

// If this option was changed, display a notice with the new template mode.
if ( self::get_option( 'theme_support' ) !== $new_options['theme_support'] ) {
if ( self::get_option( Option::THEME_SUPPORT ) !== $new_options[ Option::THEME_SUPPORT ] ) {
add_action( 'update_option_' . self::OPTION_NAME, [ __CLASS__, 'handle_updated_theme_support_option' ] );
}
}

// Validate post type support.
if ( isset( $new_options['supported_post_types'] ) ) {
$options['supported_post_types'] = [];
if ( isset( $new_options[ Option::SUPPORTED_POST_TYPES ] ) ) {
$options[ Option::SUPPORTED_POST_TYPES ] = [];

foreach ( $new_options['supported_post_types'] as $post_type ) {
foreach ( $new_options[ Option::SUPPORTED_POST_TYPES ] as $post_type ) {
if ( ! post_type_exists( $post_type ) ) {
add_settings_error( self::OPTION_NAME, 'unknown_post_type', __( 'Unrecognized post type.', 'amp' ) );
} else {
$options['supported_post_types'][] = $post_type;
$options[ Option::SUPPORTED_POST_TYPES ][] = $post_type;
}
}
}
Expand All @@ -207,21 +209,21 @@ public static function validate_options( $new_options ) {

$is_template_support_required = ( isset( $theme_support_args['templates_supported'] ) && 'all' === $theme_support_args['templates_supported'] );
if ( ! $is_template_support_required && ! isset( $theme_support_args['available_callback'] ) ) {
$options['all_templates_supported'] = ! empty( $new_options['all_templates_supported'] );
$options[ Option::ALL_TEMPLATES_SUPPORTED ] = ! empty( $new_options[ Option::ALL_TEMPLATES_SUPPORTED ] );

// Validate supported templates.
$options['supported_templates'] = [];
if ( isset( $new_options['supported_templates'] ) ) {
$options['supported_templates'] = array_intersect(
$new_options['supported_templates'],
$options[ Option::SUPPORTED_TEMPLATES ] = [];
if ( isset( $new_options[ Option::SUPPORTED_TEMPLATES ] ) ) {
$options[ Option::SUPPORTED_TEMPLATES ] = array_intersect(
$new_options[ Option::SUPPORTED_TEMPLATES ],
array_keys( AMP_Theme_Support::get_supportable_templates() )
);
}
}

// Validate analytics.
if ( isset( $new_options['analytics'] ) ) {
foreach ( $new_options['analytics'] as $id => $data ) {
if ( isset( $new_options[ Option::ANALYTICS ] ) ) {
foreach ( $new_options[ Option::ANALYTICS ] as $id => $data ) {

// Check save/delete pre-conditions and proceed if correct.
if ( empty( $data['type'] ) || empty( $data['config'] ) ) {
Expand All @@ -247,16 +249,16 @@ public static function validate_options( $new_options ) {
$entry_id = substr( md5( $entry_vendor_type . $entry_config ), 0, 12 );

// Avoid duplicates.
if ( isset( $options['analytics'][ $entry_id ] ) ) {
if ( isset( $options[ Option::ANALYTICS ][ $entry_id ] ) ) {
add_settings_error( self::OPTION_NAME, 'duplicate_analytics_entry', __( 'Duplicate analytics entry found.', 'amp' ) );
continue;
}
}

if ( isset( $data['delete'] ) ) {
unset( $options['analytics'][ $entry_id ] );
unset( $options[ Option::ANALYTICS ][ $entry_id ] );
} else {
$options['analytics'][ $entry_id ] = [
$options[ Option::ANALYTICS ][ $entry_id ] = [
'type' => $entry_vendor_type,
'config' => $entry_config,
];
Expand All @@ -271,7 +273,7 @@ public static function validate_options( $new_options ) {
}

// Store the current version with the options so we know the format.
$options['version'] = AMP__VERSION;
$options[ Option::VERSION ] = AMP__VERSION;

return $options;
}
Expand All @@ -284,11 +286,11 @@ public static function validate_options( $new_options ) {
*/
public static function check_supported_post_type_update_errors() {
// If all templates are supported then skip check since all post types are also supported. This option only applies with standard/transitional theme support.
if ( self::get_option( 'all_templates_supported', false ) && AMP_Theme_Support::READER_MODE_SLUG !== self::get_option( 'theme_support' ) ) {
if ( self::get_option( Option::ALL_TEMPLATES_SUPPORTED, false ) && AMP_Theme_Support::READER_MODE_SLUG !== self::get_option( Option::THEME_SUPPORT ) ) {
return;
}

$supported_types = self::get_option( 'supported_post_types', [] );
$supported_types = self::get_option( Option::SUPPORTED_POST_TYPES, [] );
foreach ( AMP_Post_Type_Support::get_eligible_post_types() as $name ) {
$post_type = get_post_type_object( $name );
if ( empty( $post_type ) ) {
Expand Down Expand Up @@ -354,8 +356,8 @@ public static function handle_analytics_submit() {
// Ensure request is coming from analytics option form.
check_admin_referer( 'analytics-options', 'analytics-options' );

if ( isset( $_POST['amp-options']['analytics'] ) ) {
self::update_option( 'analytics', wp_unslash( $_POST['amp-options']['analytics'] ) );
if ( isset( $_POST[ self::OPTION_NAME ][ Option::ANALYTICS ] ) ) {
self::update_option( Option::ANALYTICS, wp_unslash( $_POST[ self::OPTION_NAME ][ Option::ANALYTICS ] ) );

$errors = get_settings_errors( self::OPTION_NAME );
if ( empty( $errors ) ) {
Expand Down Expand Up @@ -383,7 +385,7 @@ public static function handle_analytics_submit() {
*/
public static function update_analytics_options( $data ) {
_deprecated_function( __METHOD__, '0.6', __CLASS__ . '::update_option' );
return self::update_option( 'analytics', wp_unslash( $data ) );
return self::update_option( Option::ANALYTICS, wp_unslash( $data ) );
}

/**
Expand Down Expand Up @@ -537,7 +539,7 @@ public static function insecure_connection_notice() {
* Adds a message for an update of the theme support setting.
*/
public static function handle_updated_theme_support_option() {
$template_mode = self::get_option( 'theme_support' );
$template_mode = self::get_option( Option::THEME_SUPPORT );

// Make sure post type support has been added for sake of amp_admin_get_preview_permalink().
foreach ( AMP_Post_Type_Support::get_eligible_post_types() as $post_type ) {
Expand Down
Loading

0 comments on commit 49915b4

Please sign in to comment.