Customizer: Introduce WP_Customize_Control::active() method to determine whether the control is relevant to the current context (i.e. to the current URL being previewed).

Control can indicate its active state by a subclass overriding the 'active_callback' method, by supplying a callable 'active_callback' argument into the control's constructor, or by filtering 'customize_control_active'.

props westonruter.
see #27993.

git-svn-id: https://develop.svn.wordpress.org/trunk@29051 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2014-07-09 23:57:29 +00:00
parent ccef0ae5f0
commit 1477277b53
7 changed files with 186 additions and 39 deletions

View File

@@ -85,6 +85,19 @@ class WP_Customize_Control {
*/
public $type = 'text';
/**
* Callback
*
* @since 4.0.0
*
* @access public
* @see WP_Customize_Control::active()
* @var callable Callback is called with one argument, the instance of
* WP_Customize_Control, and returns bool to indicate whether
* the control is active (such as it relates to the URL
* currently being previewed).
*/
public $active_callback = '';
/**
* Constructor.
@@ -102,16 +115,21 @@ class WP_Customize_Control {
public function __construct( $manager, $id, $args = array() ) {
$keys = array_keys( get_object_vars( $this ) );
foreach ( $keys as $key ) {
if ( isset( $args[ $key ] ) )
if ( isset( $args[ $key ] ) ) {
$this->$key = $args[ $key ];
}
}
$this->manager = $manager;
$this->id = $id;
if ( empty( $this->active_callback ) ) {
$this->active_callback = array( $this, 'active_callback' );
}
// Process settings.
if ( empty( $this->settings ) )
if ( empty( $this->settings ) ) {
$this->settings = $id;
}
$settings = array();
if ( is_array( $this->settings ) ) {
@@ -132,6 +150,41 @@ class WP_Customize_Control {
*/
public function enqueue() {}
/**
* Check whether control is active to current customizer preview.
*
* @since 4.0.0
*
* @return bool
*/
public final function active() {
$control = $this;
$active = call_user_func( $this->active_callback, $this );
/**
* Filter response of WP_Customize_Control::active().
*
* @since 4.0.0
*
* @param bool $active
* @param WP_Customize_Control $control
*/
$active = apply_filters( 'customize_control_active', $active, $control );
return $active;
}
/**
* Default callback used when invoking WP_Customize_Control::active().
*
* Subclasses can override this with their specific logic, or they may
* provide an 'active_callback' argument to the constructor.
*
* @return bool
*/
public function active_callback() {
return true;
}
/**
* Fetch a setting's value.
@@ -143,8 +196,9 @@ class WP_Customize_Control {
* @return mixed The requested setting's value, if the setting exists.
*/
public final function value( $setting_key = 'default' ) {
if ( isset( $this->settings[ $setting_key ] ) )
if ( isset( $this->settings[ $setting_key ] ) ) {
return $this->settings[ $setting_key ]->value();
}
}
/**
@@ -159,6 +213,7 @@ class WP_Customize_Control {
}
$this->json['type'] = $this->type;
$this->json['active'] = $this->active();
}
/**
@@ -256,7 +311,7 @@ class WP_Customize_Control {
echo $this->get_link( $setting_key );
}
/**
/**
* Render the custom attributes for the control's input element.
*
* @since 4.0.0
@@ -995,6 +1050,13 @@ class WP_Widget_Area_Customize_Control extends WP_Customize_Control {
</span>
<?php
}
/**
* @return bool
*/
function active_callback() {
return $this->manager->widgets->is_sidebar_rendered( $this->sidebar_id );
}
}
/**
@@ -1035,5 +1097,12 @@ class WP_Widget_Form_Customize_Control extends WP_Customize_Control {
$args = wp_list_widget_controls_dynamic_sidebar( array( 0 => $args, 1 => $widget['params'][0] ) );
echo $this->manager->widgets->get_widget_control( $args );
}
/**
* @return bool
*/
function active_callback() {
return $this->manager->widgets->is_widget_rendered( $this->widget_id );
}
}

View File

@@ -475,7 +475,8 @@ final class WP_Customize_Manager {
public function customize_preview_settings() {
$settings = array(
'values' => array(),
'channel' => esc_js( $_POST['customize_messenger_channel'] ),
'channel' => wp_unslash( $_POST['customize_messenger_channel'] ),
'activeControls' => array(),
);
if ( 2 == $this->nonce_tick ) {
@@ -488,6 +489,9 @@ final class WP_Customize_Manager {
foreach ( $this->settings as $id => $setting ) {
$settings['values'][ $id ] = $setting->js_value();
}
foreach ( $this->controls as $id => $control ) {
$settings['activeControls'][ $id ] = $control->active();
}
?>
<script type="text/javascript">

View File

@@ -1068,7 +1068,33 @@ final class WP_Customize_Widgets {
* @param array $widget Rendered widget to tally.
*/
public function tally_rendered_widgets( $widget ) {
$this->rendered_widgets[$widget['id']] = true;
$this->rendered_widgets[ $widget['id'] ] = true;
}
/**
* Determine if a widget is rendered on the page.
*
* @since 4.0.0
* @access public
*
* @param string $widget_id
* @return bool
*/
public function is_widget_rendered( $widget_id ) {
return in_array( $widget_id, $this->rendered_widgets );
}
/**
* Determine if a sidebar is rendered on the page.
*
* @since 4.0.0
* @access public
*
* @param string $sidebar_id
* @return bool
*/
public function is_sidebar_rendered( $sidebar_id ) {
return in_array( $sidebar_id, $this->rendered_sidebars );
}
/**

View File

@@ -23,11 +23,6 @@
this.buildWidgetSelectors();
this.highlightControls();
this.preview.bind( 'active', function() {
self.preview.send( 'rendered-sidebars', self.renderedSidebars ); // @todo Only send array of IDs
self.preview.send( 'rendered-widgets', self.renderedWidgets ); // @todo Only send array of IDs
} );
this.preview.bind( 'highlight-widget', self.highlightWidget );
},

View File

@@ -95,7 +95,9 @@
preview.send( 'nonce', api.settings.nonce );
});
preview.send( 'ready' );
preview.send( 'ready', {
activeControls: api.settings.activeControls
} );
/* Custom Backgrounds */
bg = $.map(['color', 'image', 'position_x', 'repeat', 'attachment'], function( prop ) {