Front to Back, take 2, step 01

Poking at the Customizer bear.

Getting to know the Theme Customizer API

I admit I’ve not used the Theme Customizer API as much as I’ve used other WordPress components but find myself amazed by its simplicity.
I’ve came up with some trial code to test the waters and see what can be done with it and its components: panels, sections and controls.
I’m leveraging the active_callback on each component to hide one and show one; from previous experiments a component that does not contain any other component will not show.

<?php
/**
 * Plugin Name: Front to Back
 * Plugin URI: http://theAverageDev.com
 * Description: Easy page templating for developers.
 * Version: 1.0
 * Author: theAverageDev
 * Author URI: http://theAverageDev.com
 * License: GPL 2.0
 */

function ftb_panel_active_callback(WP_Customize_Panel $panel)
{
    return true;
}

function ftb_section_active_callback(WP_Customize_Section $section)
{
    return true;
}

function ftb_control_active_callback(WP_Customize_Control $control)
{
    return true;
}

add_action('customize_register', function (WP_Customize_Manager $wp_customize) {
    $wp_customize->add_setting('ftb-first-setting', array('transport' => 'refresh'));
    $wp_customize->add_setting('ftb-second-setting', array('transport' => 'refresh'));

    $wp_customize->add_panel(new WP_Customize_Panel($wp_customize, 'ftb-first-panel', array(
        'title' => 'FTB First Panel',
        'active_callback' => 'ftb_panel_active_callback',
    )));

    $wp_customize->add_panel(new WP_Customize_Panel($wp_customize, 'ftb-second-panel', array(
        'title' => 'FTB Second Panel',
        'active_callback' => '__return_false',
    )));

    $wp_customize->add_section('ftb-first-section', array(
        'title' => 'FTB First Section',
        'active_callback' => 'ftb_section_active_callback',
        'panel' => 'ftb-first-panel',
    ));

    $wp_customize->add_section('ftb-second-section', array(
        'title' => 'FTB Second Section',
        'active_callback' => '__return_false',
        'panel' => 'ftb-second-panel',
    ));

    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'color-one', array(
        'label' => 'FTB First Control',
        'section' => 'ftb-first-section',
        'settings' => 'ftb-first-setting',
        'active_callback' => 'ftb_control_active_callback',
    )));

    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'color-two', array(
        'label' => 'FTB Second Control',
        'section' => 'ftb-first-section',
        'settings' => 'ftb-second-setting',
        'active_callback' => '__return_false',
    )));
});

So panels will contain sections and sections will contain controls, controls will in turn control settings.

Information, I need information

Before any other code is written I need to nail the base of how to show and hide Theme Customizer components on a single page base.
While there are template tags in place to absolve the task of spotting the front page (is_front_page), the homepage (is_home) and so on there is no template tag in place to spot a particular page; e.g. there is no is_about_page to know if I’m on the “About” page.
Running the code in an XDebug session will show that the WP_Customize_Panel, WP_Customize_Section and WP_Customize_Control objects are not aware of “their surroundings” and do not carry any information about the currently previewed screen.
But using the afore mentioned template tags into the functions will yield usable results; e.g. modifying the first function to

function ftb_panel_active_callback(WP_Customize_Panel $panel)
{
    $is_home = is_home();
    $is_singular = is_singular();

    return true;
}

will see the function being called again as the user navigates the site while previewing in the Theme Customizer and the return values will change as the user navigates the site.

Next

Now I know I can rely on template tags to show/hide Theme Customizer components it’s time to think about a way to discern single pages in the active_callback callback functions.