Skip to content

Commit

Permalink
Restore available_callback but mark as deprecated
Browse files Browse the repository at this point in the history
Use _doing_it_wrong() instead of trigger_error()
  • Loading branch information
westonruter committed Jul 2, 2018
1 parent 38b5546 commit acf9849
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 110 deletions.
5 changes: 5 additions & 0 deletions includes/admin/class-amp-post-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ public function render_status( $post ) {
return;
}

/*
* When theme support is present then theme templates can be served in AMP and we check first if the template is available.
* Checking for template availability will include a check for get_support_errors. Otherwise, if theme support is not present
* then we just check get_support_errors.
*/
if ( current_theme_supports( 'amp' ) ) {
$availability = AMP_Theme_Support::get_template_availability( $post );
$status = $availability['supported'] ? self::ENABLED_STATUS : self::DISABLED_STATUS;
Expand Down
63 changes: 47 additions & 16 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,8 @@ public static function is_support_added_via_option() {
* Read theme support and apply options from admin for whether theme support is enabled and via what template is enabled.
*
* @see AMP_Post_Type_Support::add_post_type_support() For where post type support is added, since it is irrespective of theme support.
*
* @param bool $check_args Whether the theme support args should be checked.
*/
public static function read_theme_support( $check_args = WP_DEBUG ) {
public static function read_theme_support() {
self::$support_added_via_option = false;

self::$initial_theme_support_args = false;
Expand All @@ -172,18 +170,20 @@ public static function read_theme_support( $check_args = WP_DEBUG ) {
self::$initial_theme_support_args = array_shift( $support );

// Validate theme support usage.
if ( $check_args ) {
$keys = array( 'template_dir', 'comments_live_list', 'mode', 'optional', 'templates_supported' );
if ( ! is_array( self::$initial_theme_support_args ) ) {
trigger_error( esc_html__( 'Expected AMP theme support arg to be array.', 'amp' ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
} elseif ( count( array_diff( array_keys( self::$initial_theme_support_args ), $keys ) ) !== 0 ) {
trigger_error( esc_html( sprintf( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
/* translators: %1$s is expected keys and %2$s is actual keys */
__( 'Expected AMP theme support to keys (%1$s) but saw (%2$s)', 'amp' ),
join( ', ', $keys ),
join( ', ', array_keys( self::$initial_theme_support_args ) )
) ) );
}
$keys = array( 'template_dir', 'comments_live_list', 'mode', 'optional', 'templates_supported', 'available_callback' );
if ( ! is_array( self::$initial_theme_support_args ) ) {
_doing_it_wrong( 'add_theme_support', esc_html__( 'Expected AMP theme support arg to be array.', 'amp' ), '1.0' ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
} elseif ( count( array_diff( array_keys( self::$initial_theme_support_args ), $keys ) ) !== 0 ) {
_doing_it_wrong( 'add_theme_support', esc_html( sprintf( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
/* translators: %1$s is expected keys and %2$s is actual keys */
__( 'Expected AMP theme support to keys (%1$s) but saw (%2$s)', 'amp' ),
join( ', ', $keys ),
join( ', ', array_keys( self::$initial_theme_support_args ) )
) ), '1.0' );
}

if ( isset( self::$initial_theme_support_args['available_callback'] ) ) {
_doing_it_wrong( 'add_theme_support', esc_html__( 'The available_callback is deprecated when adding amp theme support in favor of declaratively setting the supported_templates.', 'amp' ), '1.0' );
}
}
}
Expand Down Expand Up @@ -359,7 +359,6 @@ public static function redirect_ampless_url( $exit = true ) {
* When 'amp' theme support has not been added or canonical mode is enabled, then this returns false.
*
* @since 0.7
* @since 1.0 This no longer looks at the available_callback bit instead calls get_template_availability.
*
* @see amp_is_canonical()
* @return bool Whether available.
Expand Down Expand Up @@ -457,6 +456,38 @@ public static function get_template_availability( $query = null ) {
);
}

// Support available_callback from 0.7, though it is deprecated.
if ( isset( $theme_support_args['available_callback'] ) && is_callable( $theme_support_args['available_callback'] ) ) {
/**
* Queried object.
*
* @var WP_Post $queried_object
*/
$queried_object = $query->get_queried_object();
if ( ( is_singular() || $query->is_posts_page ) && ! post_supports_amp( $queried_object ) ) {
return array_merge(
$default_response,
array(
'errors' => array( 'no-post-support' ),
'supported' => false,
'immutable' => true,
)
);
}

$response = array_merge(
$default_response,
array(
'supported' => call_user_func( $theme_support_args['available_callback'] ),
'immutable' => true,
)
);
if ( ! $response['supported'] ) {
$response['errors'][] = 'available_callback';
}
return $response;
}

$all_templates_supported_by_theme_support = false;
if ( isset( $theme_support_args['templates_supported'] ) ) {
$all_templates_supported_by_theme_support = 'all' === $theme_support_args['templates_supported'];
Expand Down
26 changes: 13 additions & 13 deletions includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,24 @@ public static function validate_options( $new_options ) {
$options['accept_tree_shaking'] = ! empty( $new_options['accept_tree_shaking'] );
$options['disable_admin_bar'] = ! empty( $new_options['disable_admin_bar'] );

// Validate post type support.
$options['supported_post_types'] = array();
if ( isset( $new_options['supported_post_types'] ) ) {
foreach ( $new_options['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;
}
}
}

$theme_support_args = AMP_Theme_Support::get_theme_support_args( array( 'initial' => true ) );

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

// Validate post type support.
$options['supported_post_types'] = array();
if ( isset( $new_options['supported_post_types'] ) ) {
foreach ( $new_options['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;
}
}
}

// Validate supported templates.
$options['supported_templates'] = array();
if ( isset( $new_options['supported_templates'] ) ) {
Expand Down
132 changes: 71 additions & 61 deletions includes/options/class-amp-options-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,27 +287,35 @@ public function render_validation_handling() {
*/
public function render_supported_templates() {
$theme_support_args = AMP_Theme_Support::get_theme_support_args( array( 'initial' => true ) ); // Initial so we can get before removed if optional.

?>
<fieldset id="all_templates_supported_fieldset">
<?php if ( isset( $theme_support_args['templates_supported'] ) && 'all' === $theme_support_args['templates_supported'] ) : ?>
<div class="notice notice-info notice-alt inline">

<?php if ( ! isset( $theme_support_args['available_callback'] ) ) : ?>
<fieldset id="all_templates_supported_fieldset">
<?php if ( isset( $theme_support_args['templates_supported'] ) && 'all' === $theme_support_args['templates_supported'] ) : ?>
<div class="notice notice-info notice-alt inline">
<p>
<?php esc_html_e( 'The current theme requires all templates to support AMP.', 'amp' ); ?>
</p>
</div>
<?php else : ?>
<p>
<?php esc_html_e( 'The current theme requires all templates to support AMP.', 'amp' ); ?>
<label for="all_templates_supported">
<input id="all_templates_supported" type="checkbox" name="<?php echo esc_attr( AMP_Options_Manager::OPTION_NAME . '[all_templates_supported]' ); ?>" <?php checked( AMP_Options_Manager::get_option( 'all_templates_supported' ) ); ?>>
<?php esc_html_e( 'Serve all templates as AMP regardless of what is being queried.', 'amp' ); ?>
</label>
</p>
</div>
<?php else : ?>
<p class="description">
<?php esc_html_e( 'This will allow all of the URLs on your site to be served as AMP by default.', 'amp' ); ?>
</p>
<?php endif; ?>
</fieldset>
<?php else : ?>
<div class="notice notice-warning notice-alt inline">
<p>
<label for="all_templates_supported">
<input id="all_templates_supported" type="checkbox" name="<?php echo esc_attr( AMP_Options_Manager::OPTION_NAME . '[all_templates_supported]' ); ?>" <?php checked( AMP_Options_Manager::get_option( 'all_templates_supported' ) ); ?>>
<?php esc_html_e( 'Serve all templates as AMP regardless of what is being queried.', 'amp' ); ?>
</label>
</p>
<p class="description">
<?php esc_html_e( 'This will allow all of the URLs on your site to be served as AMP by default.', 'amp' ); ?>
<?php esc_html_e( 'Your theme is using the deprecated available_callback argument for AMP theme support.', 'amp' ); ?>
</p>
<?php endif; ?>
</fieldset>
</div>
<?php endif; ?>

<fieldset id="supported_post_types_fieldset">
<?php $element_name = AMP_Options_Manager::OPTION_NAME . '[supported_post_types][]'; ?>
Expand All @@ -334,55 +342,57 @@ public function render_supported_templates() {
</ul>
</fieldset>

<fieldset id="supported_templates_fieldset">
<style>
#supported_templates_fieldset ul ul {
margin-left: 40px;
}
</style>
<h4 class="title"><?php esc_html_e( 'Templates', 'amp' ); ?></h4>
<?php
self::list_template_conditional_options( AMP_Theme_Support::get_supportable_templates() );
?>
<?php if ( ! isset( $theme_support_args['available_callback'] ) ) : ?>
<fieldset id="supported_templates_fieldset">
<style>
#supported_templates_fieldset ul ul {
margin-left: 40px;
}
</style>
<h4 class="title"><?php esc_html_e( 'Templates', 'amp' ); ?></h4>
<?php
self::list_template_conditional_options( AMP_Theme_Support::get_supportable_templates() );
?>
<script>
// Let clicks on parent items automatically cause the children checkboxes to have same checked state applied.
(function ( $ ) {
$( '#supported_templates_fieldset input[type=checkbox]' ).on( 'click', function() {
$( this ).siblings( 'ul' ).find( 'input[type=checkbox]' ).prop( 'checked', this.checked );
} );
})( jQuery );
</script>
</fieldset>

<script>
// Let clicks on parent items automatically cause the children checkboxes to have same checked state applied.
// Update the visibility of the fieldsets based on the selected template mode and then whether all templates are indicated to be supported.
(function ( $ ) {
$( '#supported_templates_fieldset input[type=checkbox]' ).on( 'click', function() {
$( this ).siblings( 'ul' ).find( 'input[type=checkbox]' ).prop( 'checked', this.checked );
} );
var templateModeInputs, themeSupportDisabledInput, allTemplatesSupportedInput;
templateModeInputs = $( 'input[type=radio][name="amp-options[theme_support]"]' );
themeSupportDisabledInput = $( '#theme_support_disabled' );
allTemplatesSupportedInput = $( '#all_templates_supported' );

function updateFieldsetVisibility() {
var allTemplatesSupported = 0 === allTemplatesSupportedInput.length || allTemplatesSupportedInput.prop( 'checked' );
$( '#all_templates_supported_fieldset, #supported_post_types_fieldset > .title' ).toggleClass(
'hidden',
themeSupportDisabledInput.prop( 'checked' )
);
$( '#supported_post_types_fieldset' ).toggleClass(
'hidden',
allTemplatesSupported && ! themeSupportDisabledInput.prop( 'checked' )
);
$( '#supported_templates_fieldset' ).toggleClass(
'hidden',
allTemplatesSupported || themeSupportDisabledInput.prop( 'checked' )
);
}

templateModeInputs.on( 'change', updateFieldsetVisibility );
allTemplatesSupportedInput.on( 'click', updateFieldsetVisibility );
updateFieldsetVisibility();
})( jQuery );
</script>
</fieldset>

<script>
// Update the visibility of the fieldsets based on the selected template mode and then whether all templates are indicated to be supported.
(function ( $ ) {
var templateModeInputs, themeSupportDisabledInput, allTemplatesSupportedInput;
templateModeInputs = $( 'input[type=radio][name="amp-options[theme_support]"]' );
themeSupportDisabledInput = $( '#theme_support_disabled' );
allTemplatesSupportedInput = $( '#all_templates_supported' );

function updateFieldsetVisibility() {
var allTemplatesSupported = 0 === allTemplatesSupportedInput.length || allTemplatesSupportedInput.prop( 'checked' );
$( '#all_templates_supported_fieldset, #supported_post_types_fieldset > .title' ).toggleClass(
'hidden',
themeSupportDisabledInput.prop( 'checked' )
);
$( '#supported_post_types_fieldset' ).toggleClass(
'hidden',
allTemplatesSupported && ! themeSupportDisabledInput.prop( 'checked' )
);
$( '#supported_templates_fieldset' ).toggleClass(
'hidden',
allTemplatesSupported || themeSupportDisabledInput.prop( 'checked' )
);
}

templateModeInputs.on( 'change', updateFieldsetVisibility );
allTemplatesSupportedInput.on( 'click', updateFieldsetVisibility );
updateFieldsetVisibility();
})( jQuery );
</script>
<?php endif; ?>
<?php
}

Expand Down
Loading

0 comments on commit acf9849

Please sign in to comment.