Skip to content

Commit

Permalink
Persist user selection even when checkbox disabled
Browse files Browse the repository at this point in the history
* De-duplicate logic
  • Loading branch information
westonruter committed Jul 1, 2018
1 parent bfb202a commit 2062816
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
43 changes: 25 additions & 18 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,7 @@ public static function get_template_availability( $query = null ) {
if ( $is_match ) {
$matching_templates[ $id ] = array(
'template' => $id,
'supported' => (
! empty( $supportable_template['supported'] )
||
( AMP_Options_Manager::get_option( 'all_templates_supported' ) && empty( $supportable_template['immutable'] ) )
||
$all_templates_supported_by_theme_support // Make sure theme support flag is given final say.
),
'supported' => ! empty( $supportable_template['supported'] ),
'immutable' => ! empty( $supportable_template['immutable'] ),
);
}
Expand Down Expand Up @@ -692,16 +686,6 @@ public static function get_supportable_templates() {
);
}

// Pre-populate template supported state by theme support flag.
$theme_support_args = self::get_theme_support_args( array( 'initial' => true ) );
if ( isset( $theme_support_args['templates_supported'] ) && is_array( $theme_support_args['templates_supported'] ) ) {
foreach ( $templates as $id => &$template ) {
if ( isset( $theme_support_args['templates_supported'][ $id ] ) ) {
$templates[ $id ]['supported'] = $theme_support_args['templates_supported'][ $id ];
}
}
}

/**
* Filters list of supportable templates.
*
Expand All @@ -721,11 +705,34 @@ public static function get_supportable_templates() {
*/
$templates = apply_filters( 'amp_supportable_templates', $templates );

// Obtain the initial template supported state by theme support flag.
$theme_support_args = self::get_theme_support_args( array( 'initial' => true ) );
$theme_supported_templates = array();
if ( isset( $theme_support_args['templates_supported'] ) ) {
$theme_supported_templates = $theme_support_args['templates_supported'];
}

$supported_templates = AMP_Options_Manager::get_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.
$template['user_supported'] = in_array( $id, $supported_templates, true );

// Consider supported templates from theme support args.
if ( ! isset( $template['supported'] ) ) {
if ( 'all' === $theme_supported_templates ) {
$template['supported'] = true;
} elseif ( is_array( $theme_supported_templates ) && isset( $theme_supported_templates[ $id ] ) ) {
$template['supported'] = $theme_supported_templates[ $id ];
}
}

// Make supported state immutable if it was programmatically set.
$template['immutable'] = isset( $template['supported'] );

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

Expand Down
6 changes: 4 additions & 2 deletions includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ class AMP_Options_Manager {
*/
protected static $defaults = array(
'theme_support' => 'disabled',
'supported_post_types' => array( 'post' ),
'supported_post_types' => array( 'post' ), // @todo Try updating from 0.7 to 1.0.
'analytics' => array(),
'force_sanitization' => false,
'accept_tree_shaking' => false,
'disable_admin_bar' => false,
'all_templates_supported' => true,
'unrecognized_templates_supported' => true,
'supported_templates' => array(
'is_singular',
'is_singular', // @todo Why?
),
);

Expand Down Expand Up @@ -132,6 +132,7 @@ public static function validate_options( $new_options ) {
$options['unrecognized_templates_supported'] = ! empty( $new_options['unrecognized_templates_supported'] );
}

// @todo Store associative array instead?
// Validate post type support.
$options['supported_post_types'] = array();
if ( isset( $new_options['supported_post_types'] ) ) {
Expand All @@ -144,6 +145,7 @@ public static function validate_options( $new_options ) {
}
}

// @todo Store associative array instead.
// Validate supported templates.
$options['supported_templates'] = array();
if ( isset( $new_options['supported_templates'] ) ) {
Expand Down
27 changes: 19 additions & 8 deletions includes/options/class-amp-options-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,14 +419,25 @@ private function list_template_conditional_options( $options, $parent = null ) {

?>
<li>
<input
type="checkbox"
id="<?php echo esc_attr( $element_id ); ?>"
name="<?php echo esc_attr( $element_name ); ?>"
value="<?php echo esc_attr( $id ); ?>"
<?php checked( ! empty( $option['supported'] ) ); ?>
<?php disabled( ! empty( $option['immutable'] ) ); ?>
>
<?php if ( empty( $option['immutable'] ) ) : ?>
<input
type="checkbox"
id="<?php echo esc_attr( $element_id ); ?>"
name="<?php echo esc_attr( $element_name ); ?>"
value="<?php echo esc_attr( $id ); ?>"
<?php checked( ! empty( $option['user_supported'] ) ); ?>
>
<?php else : // Persist user selection even when checkbox disabled, when selection forced by theme/filter. ?>
<input
type="checkbox"
id="<?php echo esc_attr( $element_id ); ?>"
<?php checked( ! empty( $option['supported'] ) ); ?>
<?php disabled( true ); ?>
>
<?php if ( ! empty( $option['user_supported'] ) ) : ?>
<input type="hidden" name="<?php echo esc_attr( $element_name ); ?>" value="<?php echo esc_attr( $id ); ?>">
<?php endif; ?>
<?php endif; ?>
<label for="<?php echo esc_attr( $element_id ); ?>">
<?php echo esc_html( $option['label'] ); ?>
</label>
Expand Down

0 comments on commit 2062816

Please sign in to comment.