<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[29051] trunk/src: Customizer: Introduce WP_Customize_Control::active() method to determine whether the control is relevant to the current context (i.e.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/29051">29051</a></dd>
<dt>Author</dt> <dd>SergeyBiryukov</dd>
<dt>Date</dt> <dd>2014-07-09 23:57:29 +0000 (Wed, 09 Jul 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>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 <a href="http://core.trac.wordpress.org/ticket/27993">#27993</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminjscustomizecontrolsjs">trunk/src/wp-admin/js/customize-controls.js</a></li>
<li><a href="#trunksrcwpadminjscustomizewidgetsjs">trunk/src/wp-admin/js/customize-widgets.js</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizecontrolphp">trunk/src/wp-includes/class-wp-customize-control.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizemanagerphp">trunk/src/wp-includes/class-wp-customize-manager.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizewidgetsphp">trunk/src/wp-includes/class-wp-customize-widgets.php</a></li>
<li><a href="#trunksrcwpincludesjscustomizepreviewwidgetsjs">trunk/src/wp-includes/js/customize-preview-widgets.js</a></li>
<li><a href="#trunksrcwpincludesjscustomizepreviewjs">trunk/src/wp-includes/js/customize-preview.js</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminjscustomizecontrolsjs"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/js/customize-controls.js (29050 => 29051)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/js/customize-controls.js 2014-07-09 22:42:28 UTC (rev 29050)
+++ trunk/src/wp-admin/js/customize-controls.js 2014-07-09 23:57:29 UTC (rev 29051)
</span><span class="lines">@@ -37,6 +37,7 @@
</span><span class="cx"> this.id = id;
</span><span class="cx"> this.selector = '#customize-control-' + id.replace( /\]/g, '' ).replace( /\[/g, '-' );
</span><span class="cx"> this.container = $( this.selector );
</span><ins>+ this.active = new api.Value( this.params.active );
</ins><span class="cx">
</span><span class="cx"> settings = $.map( this.params.settings, function( value ) {
</span><span class="cx"> return value;
</span><span class="lines">@@ -79,10 +80,30 @@
</span><span class="cx"> element.set( setting() );
</span><span class="cx"> });
</span><span class="cx"> });
</span><ins>+
+ control.active.bind( function ( active ) {
+ control.toggle( active );
+ } );
+ control.toggle( control.active() );
</ins><span class="cx"> },
</span><span class="cx">
</span><span class="cx"> ready: function() {},
</span><span class="cx">
</span><ins>+ /**
+ * Callback for change to the control's active state.
+ *
+ * Override function for custom behavior for the control being active/inactive.
+ *
+ * @param {Boolean} active
+ */
+ toggle: function ( active ) {
+ if ( active ) {
+ this.container.slideDown();
+ } else {
+ this.container.slideUp();
+ }
+ },
+
</ins><span class="cx"> dropdownInit: function() {
</span><span class="cx"> var control = this,
</span><span class="cx"> statuses = this.container.find('.dropdown-status'),
</span><span class="lines">@@ -563,6 +584,26 @@
</span><span class="cx">
</span><span class="cx"> this.bind( 'ready', this._ready );
</span><span class="cx">
</span><ins>+ this.bind( 'ready', function ( data ) {
+ if ( ! data || ! data.activeControls ) {
+ return;
+ }
+
+ // Any controls not even registered on the previewed URL are not active either
+ api.control.each( function ( control ) {
+ if ( typeof data.activeControls[ control.id ] === 'undefined' ) {
+ data.activeControls[ control.id ] = false;
+ }
+ } );
+
+ $.each( data.activeControls, function ( id, active ) {
+ var control = api.control( id );
+ if ( control ) {
+ control.active( active );
+ }
+ } );
+ } );
+
</ins><span class="cx"> this.request = $.ajax( this.previewUrl(), {
</span><span class="cx"> type: 'POST',
</span><span class="cx"> data: this.query,
</span></span></pre></div>
<a id="trunksrcwpadminjscustomizewidgetsjs"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/js/customize-widgets.js (29050 => 29051)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/js/customize-widgets.js 2014-07-09 22:42:28 UTC (rev 29050)
+++ trunk/src/wp-admin/js/customize-widgets.js 2014-07-09 23:57:29 UTC (rev 29051)
</span><span class="lines">@@ -751,13 +751,6 @@
</span><span class="cx"> }
</span><span class="cx"> } );
</span><span class="cx">
</span><del>- // Update widget control to indicate whether it is currently rendered
- api.previewer.bind( 'rendered-widgets', function( renderedWidgets ) {
- var isRendered = !! renderedWidgets[self.params.widget_id];
-
- self.container.toggleClass( 'widget-rendered', isRendered );
- } );
-
</del><span class="cx"> formSyncHandler = api.Widgets.formSyncHandlers[ this.params.widget_id_base ];
</span><span class="cx"> if ( formSyncHandler ) {
</span><span class="cx"> $( document ).on( 'widget-synced', function( e, widget ) {
</span><span class="lines">@@ -769,6 +762,17 @@
</span><span class="cx"> },
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Update widget control to indicate whether it is currently rendered.
+ *
+ * Overrides api.Control.toggle()
+ *
+ * @param {Boolean} active
+ */
+ toggle: function ( active ) {
+ this.container.toggleClass( 'widget-rendered', active );
+ },
+
+ /**
</ins><span class="cx"> * Set up event handlers for widget removal
</span><span class="cx"> */
</span><span class="cx"> _setupRemoveUI: function() {
</span><span class="lines">@@ -1418,32 +1422,38 @@
</span><span class="cx"> } );
</span><span class="cx">
</span><span class="cx"> // Update the model with whether or not the sidebar is rendered
</span><del>- api.previewer.bind( 'rendered-sidebars', function( renderedSidebars ) {
- var isRendered = !! renderedSidebars[self.params.sidebar_id];
-
- registeredSidebar.set( 'is_rendered', isRendered );
</del><ins>+ self.active.bind( function ( active ) {
+ registeredSidebar.set( 'is_rendered', active );
</ins><span class="cx"> } );
</span><ins>+ },
</ins><span class="cx">
</span><del>- // Show the sidebar section when it becomes visible
- registeredSidebar.on( 'change:is_rendered', function( ) {
- var sectionSelector = '#accordion-section-sidebar-widgets-' + this.get( 'id' ), $section;
</del><ins>+ /**
+ * Show the sidebar section when it becomes visible.
+ *
+ * Overrides api.Control.toggle()
+ *
+ * @param {Boolean} active
+ */
+ toggle: function ( active ) {
+ var $section, sectionSelector;
</ins><span class="cx">
</span><del>- $section = $( sectionSelector );
- if ( this.get( 'is_rendered' ) ) {
- $section.stop().slideDown( function() {
- $( this ).css( 'height', 'auto' ); // so that the .accordion-section-content won't overflow
- } );
</del><ins>+ sectionSelector = '#accordion-section-sidebar-widgets-' + this.params.sidebar_id;
+ $section = $( sectionSelector );
</ins><span class="cx">
</span><del>- } else {
- // Make sure that hidden sections get closed first
- if ( $section.hasClass( 'open' ) ) {
- // it would be nice if accordionSwitch() in accordion.js was public
- $section.find( '.accordion-section-title' ).trigger( 'click' );
- }
</del><ins>+ if ( active ) {
+ $section.stop().slideDown( function() {
+ $( this ).css( 'height', 'auto' ); // so that the .accordion-section-content won't overflow
+ } );
</ins><span class="cx">
</span><del>- $section.stop().slideUp();
</del><ins>+ } else {
+ // Make sure that hidden sections get closed first
+ if ( $section.hasClass( 'open' ) ) {
+ // it would be nice if accordionSwitch() in accordion.js was public
+ $section.find( '.accordion-section-title' ).trigger( 'click' );
</ins><span class="cx"> }
</span><del>- } );
</del><ins>+
+ $section.stop().slideUp();
+ }
</ins><span class="cx"> },
</span><span class="cx">
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizecontrolphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/class-wp-customize-control.php (29050 => 29051)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/class-wp-customize-control.php 2014-07-09 22:42:28 UTC (rev 29050)
+++ trunk/src/wp-includes/class-wp-customize-control.php 2014-07-09 23:57:29 UTC (rev 29051)
</span><span class="lines">@@ -85,6 +85,19 @@
</span><span class="cx"> */
</span><span class="cx"> public $type = 'text';
</span><span class="cx">
</span><ins>+ /**
+ * 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 = '';
</ins><span class="cx">
</span><span class="cx"> /**
</span><span class="cx"> * Constructor.
</span><span class="lines">@@ -102,16 +115,21 @@
</span><span class="cx"> public function __construct( $manager, $id, $args = array() ) {
</span><span class="cx"> $keys = array_keys( get_object_vars( $this ) );
</span><span class="cx"> foreach ( $keys as $key ) {
</span><del>- if ( isset( $args[ $key ] ) )
</del><ins>+ if ( isset( $args[ $key ] ) ) {
</ins><span class="cx"> $this->$key = $args[ $key ];
</span><ins>+ }
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> $this->manager = $manager;
</span><span class="cx"> $this->id = $id;
</span><ins>+ if ( empty( $this->active_callback ) ) {
+ $this->active_callback = array( $this, 'active_callback' );
+ }
</ins><span class="cx">
</span><span class="cx"> // Process settings.
</span><del>- if ( empty( $this->settings ) )
</del><ins>+ if ( empty( $this->settings ) ) {
</ins><span class="cx"> $this->settings = $id;
</span><ins>+ }
</ins><span class="cx">
</span><span class="cx"> $settings = array();
</span><span class="cx"> if ( is_array( $this->settings ) ) {
</span><span class="lines">@@ -132,8 +150,43 @@
</span><span class="cx"> */
</span><span class="cx"> public function enqueue() {}
</span><span class="cx">
</span><ins>+ /**
+ * 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 );
</ins><span class="cx">
</span><ins>+ /**
+ * 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;
+ }
+
</ins><span class="cx"> /**
</span><ins>+ * 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;
+ }
+
+ /**
</ins><span class="cx"> * Fetch a setting's value.
</span><span class="cx"> * Grabs the main setting by default.
</span><span class="cx"> *
</span><span class="lines">@@ -143,8 +196,9 @@
</span><span class="cx"> * @return mixed The requested setting's value, if the setting exists.
</span><span class="cx"> */
</span><span class="cx"> public final function value( $setting_key = 'default' ) {
</span><del>- if ( isset( $this->settings[ $setting_key ] ) )
</del><ins>+ if ( isset( $this->settings[ $setting_key ] ) ) {
</ins><span class="cx"> return $this->settings[ $setting_key ]->value();
</span><ins>+ }
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -159,6 +213,7 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> $this->json['type'] = $this->type;
</span><ins>+ $this->json['active'] = $this->active();
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -256,7 +311,7 @@
</span><span class="cx"> echo $this->get_link( $setting_key );
</span><span class="cx"> }
</span><span class="cx">
</span><del>- /**
</del><ins>+ /**
</ins><span class="cx"> * Render the custom attributes for the control's input element.
</span><span class="cx"> *
</span><span class="cx"> * @since 4.0.0
</span><span class="lines">@@ -995,6 +1050,13 @@
</span><span class="cx"> </span>
</span><span class="cx"> <?php
</span><span class="cx"> }
</span><ins>+
+ /**
+ * @return bool
+ */
+ function active_callback() {
+ return $this->manager->widgets->is_sidebar_rendered( $this->sidebar_id );
+ }
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -1035,5 +1097,12 @@
</span><span class="cx"> $args = wp_list_widget_controls_dynamic_sidebar( array( 0 => $args, 1 => $widget['params'][0] ) );
</span><span class="cx"> echo $this->manager->widgets->get_widget_control( $args );
</span><span class="cx"> }
</span><ins>+
+ /**
+ * @return bool
+ */
+ function active_callback() {
+ return $this->manager->widgets->is_widget_rendered( $this->widget_id );
+ }
</ins><span class="cx"> }
</span><span class="cx">
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizemanagerphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/class-wp-customize-manager.php (29050 => 29051)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/class-wp-customize-manager.php 2014-07-09 22:42:28 UTC (rev 29050)
+++ trunk/src/wp-includes/class-wp-customize-manager.php 2014-07-09 23:57:29 UTC (rev 29051)
</span><span class="lines">@@ -475,7 +475,8 @@
</span><span class="cx"> public function customize_preview_settings() {
</span><span class="cx"> $settings = array(
</span><span class="cx"> 'values' => array(),
</span><del>- 'channel' => esc_js( $_POST['customize_messenger_channel'] ),
</del><ins>+ 'channel' => wp_unslash( $_POST['customize_messenger_channel'] ),
+ 'activeControls' => array(),
</ins><span class="cx"> );
</span><span class="cx">
</span><span class="cx"> if ( 2 == $this->nonce_tick ) {
</span><span class="lines">@@ -488,6 +489,9 @@
</span><span class="cx"> foreach ( $this->settings as $id => $setting ) {
</span><span class="cx"> $settings['values'][ $id ] = $setting->js_value();
</span><span class="cx"> }
</span><ins>+ foreach ( $this->controls as $id => $control ) {
+ $settings['activeControls'][ $id ] = $control->active();
+ }
</ins><span class="cx">
</span><span class="cx"> ?>
</span><span class="cx"> <script type="text/javascript">
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizewidgetsphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/class-wp-customize-widgets.php (29050 => 29051)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/class-wp-customize-widgets.php 2014-07-09 22:42:28 UTC (rev 29050)
+++ trunk/src/wp-includes/class-wp-customize-widgets.php 2014-07-09 23:57:29 UTC (rev 29051)
</span><span class="lines">@@ -1068,10 +1068,36 @@
</span><span class="cx"> * @param array $widget Rendered widget to tally.
</span><span class="cx"> */
</span><span class="cx"> public function tally_rendered_widgets( $widget ) {
</span><del>- $this->rendered_widgets[$widget['id']] = true;
</del><ins>+ $this->rendered_widgets[ $widget['id'] ] = true;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * 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 );
+ }
+
+ /**
</ins><span class="cx"> * Tally the sidebars rendered via is_active_sidebar().
</span><span class="cx"> *
</span><span class="cx"> * Keep track of the times that is_active_sidebar() is called
</span></span></pre></div>
<a id="trunksrcwpincludesjscustomizepreviewwidgetsjs"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/js/customize-preview-widgets.js (29050 => 29051)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/js/customize-preview-widgets.js 2014-07-09 22:42:28 UTC (rev 29050)
+++ trunk/src/wp-includes/js/customize-preview-widgets.js 2014-07-09 23:57:29 UTC (rev 29051)
</span><span class="lines">@@ -23,11 +23,6 @@
</span><span class="cx"> this.buildWidgetSelectors();
</span><span class="cx"> this.highlightControls();
</span><span class="cx">
</span><del>- 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
- } );
-
</del><span class="cx"> this.preview.bind( 'highlight-widget', self.highlightWidget );
</span><span class="cx"> },
</span><span class="cx">
</span></span></pre></div>
<a id="trunksrcwpincludesjscustomizepreviewjs"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/js/customize-preview.js (29050 => 29051)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/js/customize-preview.js 2014-07-09 22:42:28 UTC (rev 29050)
+++ trunk/src/wp-includes/js/customize-preview.js 2014-07-09 23:57:29 UTC (rev 29051)
</span><span class="lines">@@ -95,7 +95,9 @@
</span><span class="cx"> preview.send( 'nonce', api.settings.nonce );
</span><span class="cx"> });
</span><span class="cx">
</span><del>- preview.send( 'ready' );
</del><ins>+ preview.send( 'ready', {
+ activeControls: api.settings.activeControls
+ } );
</ins><span class="cx">
</span><span class="cx"> /* Custom Backgrounds */
</span><span class="cx"> bg = $.map(['color', 'image', 'position_x', 'repeat', 'attachment'], function( prop ) {
</span></span></pre>
</div>
</div>
</body>
</html>