Make WordPress Core


Ignore:
Timestamp:
03/28/2012 04:14:09 AM (15 years ago)
Author:
koopersmith
Message:

Create WP_Customize_Control to separate the process of rendering a control from fetching, previewing, and saving its values. see #19910.

Many-to-many mapping between settings and controls.

  • Settings and controls have been separated in both the PHP (WP_Customize_Setting, WP_Customize_Control) and the JS (wp.customize.Setting, wp.customize.Control).
  • While most settings are tied to a single control, some require multiple controls. The 'header_textcolor' control is a good example: to hide the header text, header_textcolor is set to 'blank'.

Add 'Display Header Text' control.

A handful of miscellaneous bugfixes along the way.

Notes:

  • Controls should be separated out a bit more; juggling type-specific arguments in the switch statement is rather inelegant.
  • Page dropdowns are currently inactive and need to be re-linked.
File:
1 edited

Legend:

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

    r20290 r20295  
    1515        protected $settings = array();
    1616        protected $sections = array();
     17        protected $controls = array();
    1718
    1819        /**
    … …  
    2425                require( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
    2526                require( ABSPATH . WPINC . '/class-wp-customize-section.php' );
     27                require( ABSPATH . WPINC . '/class-wp-customize-control.php' );
    2628
    2729                add_action( 'setup_theme',  array( $this, 'setup_theme' ) );
    … …  
    358360         * @since 3.4.0
    359361         *
    360          * @param string $id An specific ID of the setting. Can be a
     362         * @param string $id A specific ID of the setting. Can be a
    361363         *                   theme mod or option name.
    362364         * @param array $args Setting arguments.
    363365         */
    364366        public function add_setting( $id, $args = array() ) {
    365                 $setting = new WP_Customize_Setting( $this, $id, $args );
     367                if ( is_a( $id, 'WP_Customize_Setting' ) )
     368                        $setting = $id;
     369                else
     370                        $setting = new WP_Customize_Setting( $this, $id, $args );
    366371
    367372                $this->settings[ $setting->id ] = $setting;
    … …  
    373378         * @since 3.4.0
    374379         *
    375          * @param string $id An specific ID of the setting.
     380         * @param string $id A specific ID of the setting.
    376381         * @return object The settings object.
    377382         */
    … …  
    386391         * @since 3.4.0
    387392         *
    388          * @param string $id An specific ID of the setting.
     393         * @param string $id A specific ID of the setting.
    389394         */
    390395        public function remove_setting( $id ) {
    … …  
    397402         * @since 3.4.0
    398403         *
    399          * @param string $id An specific ID of the section.
     404         * @param string $id A specific ID of the section.
    400405         * @param array $args Section arguments.
    401406         */
    402407        public function add_section( $id, $args = array() ) {
    403                 $section = new WP_Customize_Section( $this, $id, $args );
     408                if ( is_a( $id, 'WP_Customize_Section' ) )
     409                        $section = $id;
     410                else
     411                        $section = new WP_Customize_Section( $this, $id, $args );
    404412
    405413                $this->sections[ $section->id ] = $section;
    … …  
    411419         * @since 3.4.0
    412420         *
    413          * @param string $id An specific ID of the section.
     421         * @param string $id A specific ID of the section.
    414422         * @return object The section object.
    415423         */
    … …  
    424432         * @since 3.4.0
    425433         *
    426          * @param string $id An specific ID of the section.
     434         * @param string $id A specific ID of the section.
    427435         */
    428436        public function remove_section( $id ) {
    429437                unset( $this->sections[ $id ] );
     438        }
     439
     440        /**
     441         * Add a customize control.
     442         *
     443         * @since 3.4.0
     444         *
     445         * @param string $id A specific ID of the control.
     446         * @param array $args Setting arguments.
     447         */
     448        public function add_control( $id, $args = array() ) {
     449                if ( is_a( $id, 'WP_Customize_Control' ) )
     450                        $control = $id;
     451                else
     452                        $control = new WP_Customize_Control( $this, $id, $args );
     453
     454                $this->controls[ $control->id ] = $control;
     455        }
     456
     457        /**
     458         * Retrieve a customize control.
     459         *
     460         * @since 3.4.0
     461         *
     462         * @param string $id A specific ID of the control.
     463         * @return object The settings object.
     464         */
     465        public function get_control( $id ) {
     466                if ( isset( $this->controls[ $id ] ) )
     467                        return $this->controls[ $id ];
     468        }
     469
     470        /**
     471         * Remove a customize setting.
     472         *
     473         * @since 3.4.0
     474         *
     475         * @param string $id A specific ID of the control.
     476         */
     477        public function remove_control( $id ) {
     478                unset( $this->controls[ $id ] );
    430479        }
    431480
    … …  
    453502         */
    454503        public function prepare_controls() {
    455                 // Prepare settings
     504                // Prepare controls
    456505                // Reversing makes uasort sort by time added when conflicts occur.
    457506
    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() )
     507                $this->controls = array_reverse( $this->controls );
     508                $controls = array();
     509
     510                foreach ( $this->controls as $id => $control ) {
     511                        if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() )
    463512                                continue;
    464513
    465                         $this->sections[ $setting->section ]->settings[] = $setting;
    466                         $settings[ $id ] = $setting;
    467                 }
    468                 $this->settings = $settings;
     514                        $this->sections[ $control->section ]->controls[] = $control;
     515                        $controls[ $id ] = $control;
     516                }
     517                $this->controls = $controls;
    469518
    470519                // Prepare sections
    … …  
    474523
    475524                foreach ( $this->sections as $section ) {
    476                         if ( ! $section->check_capabilities() || ! $section->settings )
     525                        if ( ! $section->check_capabilities() || ! $section->controls )
    477526                                continue;
    478527
    479                         usort( $section->settings, array( $this, '_cmp_priority' ) );
     528                        usort( $section->controls, array( $this, '_cmp_priority' ) );
    480529                        $sections[] = $section;
    481530                }
    … …  
    489538         */
    490539        public function enqueue_control_scripts() {
    491                 foreach ( $this->settings as $setting ) {
    492                         $setting->enqueue();
     540                foreach ( $this->controls as $control ) {
     541                        $control->enqueue();
    493542                }
    494543        }
    … …  
    509558
    510559                $this->add_setting( 'header_textcolor', array(
    511                         'label'             => 'Text Color',
    512                         'section'           => 'header',
    513                         'sanitize_callback' => 'sanitize_hexcolor',
    514                         'control'           => 'color',
    515                         'theme_supports'    => array( 'custom-header', 'header-text' ),
    516                         'default'           => get_theme_support( 'custom-header', 'default-text-color' ),
    517                 ) );
    518 
    519                 /*
    520                 $this->add_setting( 'display_header', array(
    521                         'label'   => 'Display Text',
     560                        // @todo: replace with a new accept() setting method
     561                        // 'sanitize_callback' => 'sanitize_hexcolor',
     562                        'control'        => 'color',
     563                        'theme_supports' => array( 'custom-header', 'header-text' ),
     564                        'default'        => get_theme_support( 'custom-header', 'default-text-color' ),
     565                ) );
     566
     567                $this->add_control( 'display_header_text', array(
     568                        'settings' => 'header_textcolor',
     569                        'label'    => __( 'Display Header Text' ),
     570                        'section'  => 'header',
     571                        'type'     => 'checkbox',
     572                ) );
     573
     574                $this->add_control( 'header_textcolor', array(
     575                        'label'   => __( 'Text Color' ),
    522576                        'section' => 'header',
    523                         'type'    => 'radio',
    524                         'choices' => array(
    525                                 'show'  => 'Yes',
    526                                 'hide'  => 'No'
    527                         ),
    528                         // Showing header text is actually done by setting header_textcolor to 'blank'.
    529                         // @todo: Do some JS magic to make this work (since we'll be hiding the textcolor input).
    530                         'theme_mod' => false,
    531                 ) );
    532                 */
     577                        'type'    => 'color',
     578                ) );
    533579
    534580                // Input type: checkbox
    535581                // With custom value
    536582                $this->add_setting( 'header_image', array(
     583                        'default'        => get_theme_support( 'custom-header', 'default-image' ),
     584                        'theme_supports' => 'custom-header',
     585                ) );
     586
     587                $this->add_control( 'header_image', array(
    537588                        'label'          => 'Header Image',
    538589                        'section'        => 'header',
    539                         'control'        => 'image',
    540                         'default'        => get_theme_support( 'custom-header', 'default-image' ),
     590                        'type'           => 'image',
    541591                        'control_params' => array(
    542592                                'context'        => 'custom-header',
    … …  
    560610                // With sanitize_callback
    561611                $this->add_setting( 'background_color', array(
    562                         'label'             => 'Background Color',
    563                         'section'           => 'background',
    564                         'control'           => 'color',
    565612                        'default'           => get_theme_support( 'custom-background', 'default-color' ),
    566613                        'sanitize_callback' => 'sanitize_hexcolor',
     614                        'theme_supports'    => 'custom-background',
     615                ) );
     616
     617                $this->add_control( 'background_color', array(
     618                        'label'   => __( 'Background Color' ),
     619                        'section' => 'background',
     620                        'type'    => 'color',
    567621                ) );
    568622
    569623                $this->add_setting( 'background_image', array(
    570                         'label'          => 'Background Image',
     624                        'default'        => get_theme_support( 'custom-background', 'default-image' ),
     625                        'theme_supports' => 'custom-background',
     626                ) );
     627
     628                $this->add_control( 'background_image', array(
     629                        'label'          => __( 'Background Image' ),
    571630                        'section'        => 'background',
    572                         'control'        => 'upload',
    573                         'default'        => get_theme_support( 'custom-background', 'default-image' ),
     631                        'type'           => 'upload',
    574632                        'control_params' => array(
    575633                                'context'        => 'custom-background',
    … …  
    578636
    579637                $this->add_setting( 'background_repeat', array(
    580                         'label'      => 'Background Repeat',
     638                        'default'        => 'repeat',
     639                        'theme_supports' => 'custom-background',
     640                ) );
     641
     642                $this->add_control( 'background_repeat', array(
     643                        'label'      => __( 'Background Repeat' ),
    581644                        'section'    => 'background',
    582645                        'visibility' => 'background_image',
    583                         'control'    => 'radio',
     646                        'type'       => 'radio',
    584647                        'choices'    => array(
    585648                                'no-repeat'  => __('No Repeat'),
    … …  
    588651                                'repeat-y'   => __('Tile Vertically'),
    589652                        ),
    590                         'default'    => 'repeat',
    591653                ) );
    592654
    593655                $this->add_setting( 'background_position_x', array(
    594                         'label'      => 'Background Position',
     656                        'default'        => 'left',
     657                        'theme_supports' => 'custom-background',
     658                ) );
     659
     660                $this->add_control( 'background_position_x', array(
     661                        'label'      => __( 'Background Position' ),
    595662                        'section'    => 'background',
    596663                        'visibility' => 'background_image',
    597                         'control'    => 'radio',
     664                        'type'       => 'radio',
    598665                        'choices'    => array(
    599666                                'left'       => __('Left'),
    … …  
    601668                                'right'      => __('Right'),
    602669                        ),
    603                         'default'    => 'left',
    604670                ) );
    605671
    606672                $this->add_setting( 'background_attachment', array(
    607                         'label'      => 'Background Attachment',
     673                        'default'        => 'fixed',
     674                        'theme_supports' => 'custom-background',
     675                ) );
     676
     677                $this->add_control( 'background_attachment', array(
     678                        'label'      => __( 'Background Attachment' ),
    608679                        'section'    => 'background',
    609680                        'visibility' => 'background_image',
    610                         'control'    => 'radio',
     681                        'type'       => 'radio',
    611682                        'choices'    => array(
    612683                                'fixed'      => __('Fixed'),
    613684                                'scroll'     => __('Scroll'),
    614685                        ),
    615                         'default'    => 'fixed',
    616686                ) );
    617687
    … …  
    637707                        }
    638708
    639                         $this->add_setting( "nav_menu_locations[{$location}]", array(
    640                                 'label'             => $description,
    641                                 'theme_supports'    => 'menus', // Todo: Needs also widgets -- array( 'menus', 'widgets' )
    642                                 'section'           => 'nav',
    643                                 'control'           => 'select',
    644                                 'choices'           => $choices,
     709                        $menu_setting_id = "nav_menu_locations[{$location}]";
     710
     711                        $this->add_setting( $menu_setting_id, array(
    645712                                'sanitize_callback' => 'absint',
     713                                'theme_supports'    => 'menus',
     714                        ) );
     715
     716                        $this->add_control( $menu_setting_id, array(
     717                                'label'   => $description,
     718                                'section' => 'nav',
     719                                'type'    => 'select',
     720                                'choices' => $choices,
    646721                        ) );
    647722                }
    … …  
    661736
    662737                $this->add_setting( 'show_on_front', array(
    663                         'label'          => __( 'Front page displays' ),
     738                        'default'        => get_option( 'show_on_front' ),
     739                        'capability'     => 'manage_options',
     740                        'type'           => 'option',
    664741                //      'theme_supports' => 'static-front-page',
    665                         'section'        => 'static_front_page',
    666                         'control'        => 'radio',
    667                         'choices'        => $choices,
    668                         'default'        => get_option( 'show_on_front' ),
     742                ) );
     743
     744                $this->add_control( 'show_on_front', array(
     745                        'label'   => __( 'Front page displays' ),
     746                        'section' => 'static_front_page',
     747                        'type'    => 'radio',
     748                        'choices' => $choices,
     749                ) );
     750
     751                $this->add_setting( 'page_on_front', array(
     752                        'type'       => 'option',
     753                        'capability' => 'manage_options',
     754                //      'theme_supports' => 'static-front-page',
     755                ) );
     756
     757                $this->add_control( 'page_on_front', array(
     758                        'label'      => __( 'Front page' ),
     759                        'section'    => 'static_front_page',
     760                        'type'       => 'dropdown-pages',
     761                        'visibility' => array( 'show_on_front', 'page' ),
     762                ) );
     763
     764                $this->add_setting( 'page_for_posts', array(
    669765                        'type'           => 'option',
    670766                        'capability'     => 'manage_options',
    671                 ) );
    672 
    673                 $this->add_setting( 'page_on_front', array(
    674                         'label'          => __( 'Front page' ),
    675767                //      'theme_supports' => 'static-front-page',
    676                         'section'        => 'static_front_page',
    677                         'control'        => 'dropdown-pages',
    678                         'type'           => 'option',
    679                         'capability'     => 'manage_options',
    680                         'visibility'     => array( 'show_on_front', 'page' ),
    681                 ) );
    682 
    683                 $this->add_setting( 'page_for_posts', array(
    684                         'label'          => __( 'Posts page' ),
    685                 //      'theme_supports' => 'static-front-page',
    686                         'section'        => 'static_front_page',
    687                         'control'        => 'dropdown-pages',
    688                         'type'           => 'option',
    689                         'capability'     => 'manage_options',
    690                         'visibility'     => array( 'show_on_front', 'page' ),
     768                ) );
     769
     770                $this->add_control( 'page_for_posts', array(
     771                        'label'      => __( 'Posts page' ),
     772                        'section'    => 'static_front_page',
     773                        'type'       => 'dropdown-pages',
     774                        'visibility' => array( 'show_on_front', 'page' ),
    691775                ) );
    692776
    … …  
    698782
    699783                $this->add_setting( 'blogname', array(
    700                         'label'          => __( 'Site Title' ),
    701                         'section'        => 'strings',
    702                         'default'        => get_option( 'blogname' ),
    703                         'type'           => 'option',
    704                         'capability'     => 'manage_options',
     784                        'default'    => get_option( 'blogname' ),
     785                        'type'       => 'option',
     786                        'capability' => 'manage_options',
     787                ) );
     788
     789                $this->add_control( 'blogname', array(
     790                        'label'      => __( 'Site Title' ),
     791                        'section'    => 'strings',
    705792                ) );
    706793
    707794                $this->add_setting( 'blogdescription', array(
    708                         'label'          => __( 'Tagline' ),
    709                         'section'        => 'strings',
    710                         'default'        => get_option( 'blogdescription' ),
    711                         'type'           => 'option',
    712                         'capability'     => 'manage_options',
     795                        'default'    => get_option( 'blogdescription' ),
     796                        'type'       => 'option',
     797                        'capability' => 'manage_options',
     798                ) );
     799
     800                $this->add_control( 'blogdescription', array(
     801                        'label'      => __( 'Tagline' ),
     802                        'section'    => 'strings',
    713803                ) );
    714804        }
Note: See TracChangeset for help on using the changeset viewer.