Instantly share code, notes, and snippets.

Embed
What would you like to do?
( function( api ) {
'use strict';
// Add callback for when the header_textcolor setting exists.
api( 'header_textcolor', function( setting ) {
var isHeaderTextDisplayed, linkSettingValueToControlActiveState;
/**
* Determine whether the site title and tagline should be displayed.
*
* @returns {boolean} Is displayed?
*/
isHeaderTextDisplayed = function() {
return 'blank' !== setting.get();
};
/**
* Update a control's active state according to the header_textcontrol setting's value.
*
* @param {wp.customize.Control} control Site Title or Tagline Control.
*/
linkSettingValueToControlActiveState = function( control ) {
var setActiveState = function() {
control.active.set( isHeaderTextDisplayed() );
};
// FYI: With the following we can eliminate all of our PHP active_callback code.
control.active.validate = isHeaderTextDisplayed;
// Set initial active state.
setActiveState();
/*
* Update activate state whenever the setting is changed.
* Even when the setting does have a refresh transport where the
* server-side active callback will manage the active state upon
* refresh, having this JS management of the active state will
* ensure that controls will have their visibility toggled
* immediately instead of waiting for the preview to load.
* This is especially important if the setting has a postMessage
* transport where changing the setting wouldn't normally cause
* the preview to refresh and thus the server-side active_callbacks
* would not get invoked.
*/
setting.bind( setActiveState );
};
// Call linkSettingValueToControlActiveState on the site title and tagline controls when they exist.
api.control( 'blogname', linkSettingValueToControlActiveState );
api.control( 'blogdescription', linkSettingValueToControlActiveState );
} );
}( wp.customize ) );
<?php
/**
* Plugin Name: Dependently-Contextual Site Title and Tagline Customizer Controls
* Description: Toggle whether the site title and tagline controls are active based on whether they are displayed on the site.
* Author: Weston Ruter, XWP
* Version: 0.1
* Author URI: https://make.xwp.co/
* License: GPLv2+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
* Copyright (c) 2016 XWP (https://make.xwp.co/)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 or, at
* your discretion, any later version, as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package DependentlyContextualSiteTitleAndTaglineCustomizerControls
*/
namespace DependentlyContextualSiteTitleAndTaglineCustomizerControls;
/**
* Add server-side active_callbacks for the site title and tagline controls.
*
* When the preview is refreshed, the active states for the blogname and blogdescription
* controls will be returned by `get_header_text_control_active_state` and sent from
* the preview back to the pane, causing these controls to show or hide _after_ the
* preview has finished refreshing. This server-side method sufficient is sufficient
* by itself because the header_textcolor setting has a refresh transport. If, however,
* the setting has a postMessage transport, then the active state for the blogname
* and blogdescription controls would need to be manipulated in JS as well.
* This can be seen in conditionally-contextual-site-title-and-tagline-customizer-controls.js.
*
* @param \WP_Customize_Manager $wp_customize Manager.
*/
function customize_register( \WP_Customize_Manager $wp_customize ) {
$header_text_controls = array_filter( array(
$wp_customize->get_control( 'blogname' ),
$wp_customize->get_control( 'blogdescription' ),
) );
foreach ( $header_text_controls as $header_text_control ) {
$header_text_control->active_callback = __NAMESPACE__ . '\\get_header_text_control_active_state';
}
}
add_action( 'customize_register', __NAMESPACE__ . '\\customize_register', 11 );
/**
* Get header text control active state.
*
* @param \WP_Customize_Control $control Control for site title or tagline.
* @return bool Active.
*/
function get_header_text_control_active_state( \WP_Customize_Control $control ) {
$setting = $control->manager->get_setting( 'header_textcolor' );
if ( ! $setting ) {
return true;
}
return 'blank' !== $setting->value();
}
/**
* Enqueue scripts.
*/
function customize_controls_enqueue_scripts() {
$handle = 'dependently-contextual-site-title-and-tagline-customizer-controls';
$src = plugins_url( 'dependently-contextual-site-title-and-tagline-customizer-controls.js', __FILE__ );
$deps = array( 'customize-controls' );
$ver = false;
wp_enqueue_script( $handle, $src, $deps, $ver );
}
add_action( 'customize_controls_enqueue_scripts', __NAMESPACE__ . '\\customize_controls_enqueue_scripts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment