Skip to content

Commit

Permalink
Ensure changes to header video cause full-page refreshes when AMP in …
Browse files Browse the repository at this point in the history
…preview

See #4977 (review)
  • Loading branch information
westonruter committed Jul 4, 2020
1 parent 5e7f909 commit 2cec7ec
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 deletions.
71 changes: 49 additions & 22 deletions includes/admin/class-amp-template-customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,66 @@ class AMP_Template_Customizer {
*
* @param WP_Customize_Manager $wp_customize Customizer instance.
*/
public static function init( $wp_customize ) {
public static function init( WP_Customize_Manager $wp_customize ) {
$self = new self();

$self->wp_customize = $wp_customize;

if ( amp_is_legacy() ) {
/**
* Fires when the AMP Template Customizer initializes.
*
* In practice the `customize_register` hook should be used instead.
*
* @since 0.4
* @param AMP_Template_Customizer $self Instance.
*/
do_action( 'amp_customizer_init', $self );

$self->register_legacy_settings();
$self->register_legacy_ui();

add_action( 'customize_controls_print_footer_scripts', [ $self, 'print_legacy_controls_templates' ] );
add_action( 'customize_preview_init', [ $self, 'init_legacy_preview' ] );

add_action( 'customize_controls_enqueue_scripts', [ $self, 'add_legacy_customizer_scripts' ] );
} else {
add_action( 'customize_controls_enqueue_scripts', [ $self, 'add_customizer_scripts' ] );
$is_reader_mode = ( AMP_Theme_Support::READER_MODE_SLUG === AMP_Options_Manager::get_option( Option::THEME_SUPPORT ) );
$has_reader_theme = ( AMP_Reader_Themes::DEFAULT_READER_THEME !== AMP_Options_Manager::get_option( Option::READER_THEME ) );

$is_customizing_reader_theme = (
$is_reader_mode
&&
$has_reader_theme
&&
isset( $_GET[ amp_get_slug() ] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
);

if ( $is_reader_mode ) {
if ( $has_reader_theme ) {
add_action( 'customize_controls_enqueue_scripts', [ $self, 'add_customizer_scripts' ] );
} else {
/**
* Fires when the AMP Template Customizer initializes.
*
* In practice the `customize_register` hook should be used instead.
*
* @param AMP_Template_Customizer $self Instance.
*
* @since 0.4
*/
do_action( 'amp_customizer_init', $self );

$self->register_legacy_settings();
$self->register_legacy_ui();

add_action( 'customize_controls_print_footer_scripts', [ $self, 'print_legacy_controls_templates' ] );
add_action( 'customize_preview_init', [ $self, 'init_legacy_preview' ] );

add_action( 'customize_controls_enqueue_scripts', [ $self, 'add_legacy_customizer_scripts' ] );
}
}

// Force changes to header video to cause refresh since logic in wp-customize-header.js does not construct AMP components.
if ( amp_is_canonical() || $is_customizing_reader_theme ) {
$setting_ids = [
'header_video',
'external_header_video',
];
foreach ( $setting_ids as $setting_id ) {
$setting = $wp_customize->get_setting( $setting_id );
if ( $setting ) {
$setting->transport = 'refresh';
}
}
}
}

/**
* Init Customizer preview for legacy.
*
* @since 0.4
* @global WP_Customize_Manager $wp_customize
*/
public function init_legacy_preview() {
add_action( 'amp_post_template_head', 'wp_no_robots' );
Expand Down
23 changes: 7 additions & 16 deletions includes/admin/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,21 @@

/**
* Sets up the AMP template editor for the Customizer.
*
* If this is not in Reader mode, exit.
* There's no need for the 'AMP' Customizer panel,
* 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( Option::THEME_SUPPORT ) ) {
return;
}

$is_legacy = amp_is_legacy();

if ( $is_legacy || isset( $_GET[ amp_get_slug() ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
// Fire up the AMP Customizer.
add_action( 'customize_register', [ 'AMP_Template_Customizer', 'init' ], 500 );
}
// Fire up the AMP Customizer.
add_action( 'customize_register', [ 'AMP_Template_Customizer', 'init' ], 500 );

if ( $is_legacy ) {
if ( amp_is_legacy() ) {
// Add some basic design settings + controls to the Customizer.
add_action( 'amp_init', [ 'AMP_Customizer_Design_Settings', 'init' ] );
}

// Add a link to the AMP Customizer.
add_action( 'admin_menu', 'amp_add_customizer_link' );
// Add a link to the AMP Customizer in Reader mode.
if ( AMP_Theme_Support::READER_MODE_SLUG === AMP_Options_Manager::get_option( Option::THEME_SUPPORT ) ) {
add_action( 'admin_menu', 'amp_add_customizer_link' );
}
}

/**
Expand Down

0 comments on commit 2cec7ec

Please sign in to comment.