Skip to content

Commit

Permalink
Allow disabling AMP Customizer later
Browse files Browse the repository at this point in the history
Previously, you had to hook very early on `plugins_loaded` to be able to
disable the Customizer. This was not possible for sites that only have
access to the theme. This switches around the load order of the
Customizer so that the filter to disable the AMP Customizer can run any
time before `after_setup_theme`.
  • Loading branch information
mjangda committed Aug 4, 2017
1 parent c0bdac8 commit 0097341
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
21 changes: 7 additions & 14 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require_once( AMP__DIR__ . '/back-compat/back-compat.php' );
require_once( AMP__DIR__ . '/includes/amp-helper-functions.php' );
require_once( AMP__DIR__ . '/includes/admin/functions.php' );
require_once( AMP__DIR__ . '/includes/admin/class-amp-customizer.php' );
require_once( AMP__DIR__ . '/includes/settings/class-amp-customizer-settings.php' );
require_once( AMP__DIR__ . '/includes/settings/class-amp-customizer-design-settings.php' );

Expand Down Expand Up @@ -152,24 +153,16 @@ function amp_render_post( $post_id ) {
* preview page isn't flagged as an AMP template, the core panels will be re-added and the AMP panel
* hidden.
*
* @internal This callback must be hooked before priority 10 on 'plugins_loaded' to properly unhook
* the core panels.
*
* @since 0.4
*/
function _amp_bootstrap_customizer() {
/**
* Filter whether to enable the AMP template customizer functionality.
*
* @param bool $enable Whether to enable the AMP customizer. Default true.
*/
$amp_customizer_enabled = apply_filters( 'amp_customizer_is_enabled', true );

if ( true === $amp_customizer_enabled ) {
amp_init_customizer();
}
// Drop core panels (menus, widgets) from the AMP customizer
// `customize_loaded_components` runs super early so we need to call this regardless of whether the AMP customizer is enabled or not
add_filter( 'customize_loaded_components', array( 'AMP_Template_Customizer', '_unregister_core_panels' ) );

add_action( 'after_setup_theme', 'amp_maybe_init_customizer' );
}
add_action( 'plugins_loaded', '_amp_bootstrap_customizer', 9 );
add_action( 'plugins_loaded', '_amp_bootstrap_customizer', 9 ); // Should be hooked before priority 10 on 'plugins_loaded' to properly unhook core panels.

/**
* Redirects the old AMP URL to the new AMP URL.
Expand Down
14 changes: 10 additions & 4 deletions includes/admin/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@
/**
* Sets up the AMP template editor for the Customizer.
*/
function amp_init_customizer() {
require_once( AMP__DIR__ . '/includes/admin/class-amp-customizer.php' );
function amp_maybe_init_customizer() {
/**
* Filter whether to enable the AMP template customizer functionality.
*
* @param bool $enable Whether to enable the AMP customizer. Default true.
*/
$amp_customizer_enabled = apply_filters( 'amp_customizer_is_enabled', true );

// Drop core panels (menus, widgets) from the AMP customizer
add_filter( 'customize_loaded_components', array( 'AMP_Template_Customizer', '_unregister_core_panels' ) );
if ( true !== $amp_customizer_enabled ) {
return;
}

// Fire up the AMP Customizer
add_action( 'customize_register', array( 'AMP_Template_Customizer', 'init' ), 500 );
Expand Down

0 comments on commit 0097341

Please sign in to comment.