Make WordPress Core

Changeset 20248


Ignore:
Timestamp:
03/21/2012 10:55:43 PM (13 years ago)
Author:
koopersmith
Message:

Theme Customizer: Numerous API refinements and bugfixes. Add a theme_supports check for header_textcolor. see #19910.

  • prepare_controls() now removes any settings and sections that return false for check_capabilities().
  • Added maybe_render() methods to both settings and sections that call the protected render() methods.
  • Stop firing front-end preview functionality when rendering the controls.
  • Merged the WP_Customize_Setting->_render_type() method into WP_Customize_Setting->render().
  • Removed the 'customize_render_control-' hook; use 'customize_render_setting' instead.
  • Added a property to sections and settings so they no longer rely on the global. Hooray for dependency injection.
  • Shifted calls to WP_Customize_Setting->enqueue() to the 'customize_controls_enqueue_scripts' action.
  • Added a theme_supports check for the header_textcolor setting.
Location:
trunk/wp-includes
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-wp-customize-section.php

    r20232 r20248  
    99
    1010class WP_Customize_Section {
     11    public $manager;
    1112    public $id;
    1213    public $priority       = 10;
     
    2526     * @param array $args Section arguments.
    2627     */
    27     function __construct( $id, $args = array() ) {
    28         $this->id = $id;
    29 
     28    function __construct( $manager, $id, $args = array() ) {
    3029        $keys = array_keys( get_class_vars( __CLASS__ ) );
    3130        foreach ( $keys as $key ) {
     
    3332                $this->$key = $args[ $key ];
    3433        }
     34
     35        $this->manager = $manager;
     36        $this->id = $id;
    3537
    3638        $this->settings = array(); // Users cannot customize the $settings array.
     
    4648     * @return bool False if theme doesn't support the section or user doesn't have the capability.
    4749     */
    48     function check_capabilities() {
     50    public final function check_capabilities() {
    4951        if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
    5052            return false;
     
    5759
    5860    /**
     61     * Check capabiliites and render the section.
     62     *
     63     * @since 3.4.0
     64     */
     65    public final function maybe_render() {
     66        if ( ! $this->check_capabilities() )
     67            return;
     68
     69        do_action( 'customize_render_section', $this );
     70        do_action( 'customize_render_section_' . $this->id );
     71
     72        $this->render();
     73    }
     74
     75
     76    /**
    5977     * Render the section.
    6078     *
    6179     * @since 3.4.0
    6280     */
    63     function render() {
    64         if ( ! $this->check_capabilities() )
    65             return;
     81    protected function render() {
    6682        ?>
    6783        <li id="customize-section-<?php echo esc_attr( $this->id ); ?>" class="control-section customize-section">
     
    7086                <?php foreach ( $this->settings as $setting ) : ?>
    7187                <li id="customize-control-<?php echo esc_attr( $setting->id ); ?>" class="customize-control customize-control-<?php echo esc_attr( $setting->control ); ?>">
    72                     <?php $setting->_render(); ?>
     88                    <?php $setting->maybe_render(); ?>
    7389                </li>
    7490                <?php endforeach; ?>
  • trunk/wp-includes/class-wp-customize-setting.php

    r20232 r20248  
    99
    1010class WP_Customize_Setting {
     11    public $manager;
    1112    public $id;
    1213    public $priority          = 10;
     
    3637     * @param array $args Setting arguments.
    3738     */
    38     function __construct( $id, $args = array() ) {
     39    function __construct( $manager, $id, $args = array() ) {
    3940        $keys = array_keys( get_class_vars( __CLASS__ ) );
    4041        foreach ( $keys as $key ) {
     
    4344        }
    4445
     46        $this->manager = $manager;
    4547        $this->id = $id;
    4648
     
    266268     */
    267269    public final function check_capabilities() {
    268         global $customize;
    269 
    270270        if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
    271271            return false;
     
    274274            return false;
    275275
    276         $section = $customize->get_section( $this->section );
     276        $section = $this->manager->get_section( $this->section );
    277277        if ( isset( $section ) && ! $section->check_capabilities() )
    278278            return false;
     
    282282
    283283    /**
    284      * Render the control.
    285      *
    286      * @since 3.4.0
    287      */
    288     public final function _render() {
     284     * Check capabiliites and render the control.
     285     *
     286     * @since 3.4.0
     287     */
     288    public final function maybe_render() {
    289289        if ( ! $this->check_capabilities() )
    290290            return;
    291291
    292         do_action( 'customize_render_' . $this->id );
     292        do_action( 'customize_render_setting', $this );
     293        do_action( 'customize_render_setting_' . $this->id, $this );
    293294
    294295        $this->render();
     
    301302     */
    302303    protected function render() {
    303         $this->_render_type();
    304     }
    305 
    306     /**
    307      * Retrieve the name attribute for an input.
    308      *
    309      * @since 3.4.0
    310      *
    311      * @return string The name.
    312      */
    313     public final function get_name() {
    314         return self::name_prefix . esc_attr( $this->id );
    315     }
    316 
    317     /**
    318      * Echo the HTML name attribute for an input.
    319      *
    320      * @since 3.4.0
    321      *
    322      * @return string The HTML name attribute.
    323      */
    324     public final function name() {
    325         echo 'name="' . $this->get_name() . '"';
    326     }
    327 
    328     /**
    329      * Render the control type.
    330      *
    331      * @todo Improve value and checked attributes.
    332      *
    333      * @since 3.4.0
    334      */
    335     public final function _render_type() {
    336304        switch( $this->control ) {
    337305            case 'text':
     
    415383                <?php
    416384                break;
    417             default:
    418                 do_action( 'customize_render_control-' . $this->control, $this );
    419 
    420         }
     385        }
     386    }
     387
     388    /**
     389     * Retrieve the name attribute for an input.
     390     *
     391     * @since 3.4.0
     392     *
     393     * @return string The name.
     394     */
     395    public final function get_name() {
     396        return self::name_prefix . esc_attr( $this->id );
     397    }
     398
     399    /**
     400     * Echo the HTML name attribute for an input.
     401     *
     402     * @since 3.4.0
     403     *
     404     * @return string The HTML name attribute.
     405     */
     406    public final function name() {
     407        echo 'name="' . $this->get_name() . '"';
    421408    }
    422409
  • trunk/wp-includes/class-wp-customize.php

    r20212 r20248  
    3030        add_action( 'admin_footer', array( $this, 'admin_footer' ) );
    3131
    32         add_action( 'customize_previewing',    array( $this, 'customize_previewing' ) );
    33         add_action( 'customize_register',      array( $this, 'register_controls' ) );
    34         add_action( 'customize_controls_init', array( $this, 'prepare_controls' ) );
     32        add_action( 'customize_previewing',               array( $this, 'customize_previewing' ) );
     33        add_action( 'customize_register',                 array( $this, 'register_controls' ) );
     34        add_action( 'customize_controls_init',            array( $this, 'prepare_controls' ) );
     35        add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ) );
    3536    }
    3637
     
    103104        do_action( 'customize_register' );
    104105
    105         if ( ! $this->is_preview() )
    106             return;
     106        if ( $this->is_preview() )
     107            add_action( 'template_redirect', array( $this, 'customize_preview_init' ) );
     108    }
     109
     110    /**
     111     * Print javascript settings.
     112     *
     113     * @since 3.4.0
     114     */
     115    public function customize_preview_init() {
     116        $this->prepare_controls();
    107117
    108118        wp_enqueue_script( 'customize-preview' );
     
    112122            $setting->preview();
    113123        }
    114     }
     124
     125        do_action( 'customize_preview_init' );
     126    }
     127
    115128
    116129    /**
     
    350363     */
    351364    public function add_setting( $id, $args = array() ) {
    352         $setting = new WP_Customize_Setting( $id, $args );
     365        $setting = new WP_Customize_Setting( $this, $id, $args );
    353366
    354367        $this->settings[ $setting->id ] = $setting;
     
    388401     */
    389402    public function add_section( $id, $args = array() ) {
    390         $section = new WP_Customize_Section( $id, $args );
     403        $section = new WP_Customize_Section( $this, $id, $args );
    391404
    392405        $this->sections[ $section->id ] = $section;
     
    425438     * @param object $b Object B.
    426439     */
    427     protected function _cmp_priority( $a, $b ) {
     440    protected final function _cmp_priority( $a, $b ) {
    428441        $ap = $a->priority;
    429442        $bp = $b->priority;
     
    435448
    436449    /**
    437      * Prepare settings and sections. Also enqueue needed scripts/styles.
     450     * Prepare settings and sections.
    438451     *
    439452     * @since 3.4.0
    440453     */
    441454    public function prepare_controls() {
     455        // Prepare settings
    442456        // Reversing makes uasort sort by time added when conflicts occur.
    443457
     458        $this->settings = array_reverse( $this->settings );
     459        $settings = array();
     460
     461        foreach ( $this->settings as $id => $setting ) {
     462            if ( ! isset( $this->sections[ $setting->section ] ) || ! $setting->check_capabilities() )
     463                continue;
     464
     465            $this->sections[ $setting->section ]->settings[] = $setting;
     466            $settings[ $id ] = $setting;
     467        }
     468        $this->settings = $settings;
     469
     470        // Prepare sections
    444471        $this->sections = array_reverse( $this->sections );
    445472        uasort( $this->sections, array( $this, '_cmp_priority' ) );
    446 
    447         $this->settings = array_reverse( $this->settings );
     473        $sections = array();
     474
     475        foreach ( $this->sections as $section ) {
     476            if ( ! $section->check_capabilities() )
     477                continue;
     478
     479            usort( $section->settings, array( $this, '_cmp_priority' ) );
     480            $sections[] = $section;
     481        }
     482        $this->sections = $sections;
     483    }
     484
     485    /**
     486     * Enqueue scripts for customize controls.
     487     *
     488     * @since 3.4.0
     489     */
     490    public function enqueue_control_scripts() {
    448491        foreach ( $this->settings as $setting ) {
    449             if ( ! isset( $this->sections[ $setting->section ] ) )
    450                 continue;
    451 
    452             $this->sections[ $setting->section ]->settings[] = $setting;
    453 
    454             if ( $setting->check_capabilities() )
    455                 $setting->enqueue();
    456         }
    457 
    458         foreach ( $this->sections as $section ) {
    459             usort( $section->settings, array( $this, '_cmp_priority' ) );
     492            $setting->enqueue();
    460493        }
    461494    }
     
    480513            'sanitize_callback' => 'sanitize_hexcolor',
    481514            'control'           => 'color',
     515            'theme_supports'    => array( 'custom-header', 'header-text' ),
    482516            'default'           => get_theme_support( 'custom-header', 'default-text-color' ),
    483517        ) );
  • trunk/wp-includes/customize-controls.php

    r20133 r20248  
    7575            <?php
    7676            foreach ( $this->sections as $section )
    77                 $section->render();
     77                $section->maybe_render();
    7878            ?>
    7979        </ul></div>
Note: See TracChangeset for help on using the changeset viewer.