Make WordPress Core


Ignore:
Timestamp:
10/29/2014 10:50:21 PM (10 years ago)
Author:
ocean90
Message:

Improve/introduce Customizer JavaScript models for Controls, Sections, and Panels.

  • Introduce models for panels and sections.
  • Introduce API to expand and focus a control, section or panel.
  • Allow deep-linking to panels, sections, and controls inside of the Customizer.
  • Clean up accordion.js, removing all Customizer-specific logic.
  • Add initial unit tests for wp.customize.Class in customize-base.js.

https://make.wordpress.org/core/2014/10/27/toward-a-complete-javascript-api-for-the-customizer/ provides an overview of how to use the JavaScript API.

props westonruter, celloexpressions, ryankienstra.
see #28032, #28579, #28580, #28650, #28709, #29758.
fixes #29529.

File:
1 edited

Legend:

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

    r29950 r30102  
    8484
    8585    /**
     86     * @since 4.1.0
     87     * @access public
     88     * @var string
     89     */
     90    public $type;
     91
     92    /**
     93     * Callback.
     94     *
     95     * @since 4.1.0
     96     * @access public
     97     *
     98     * @see WP_Customize_Section::active()
     99     *
     100     * @var callable Callback is called with one argument, the instance of
     101     *               WP_Customize_Section, and returns bool to indicate whether
     102     *               the section is active (such as it relates to the URL
     103     *               currently being previewed).
     104     */
     105    public $active_callback = '';
     106
     107    /**
    86108     * Constructor.
    87109     *
     
    104126        $this->manager = $manager;
    105127        $this->id = $id;
     128        if ( empty( $this->active_callback ) ) {
     129            $this->active_callback = array( $this, 'active_callback' );
     130        }
    106131
    107132        $this->sections = array(); // Users cannot customize the $sections array.
    108133
    109134        return $this;
     135    }
     136
     137    /**
     138     * Check whether panel is active to current Customizer preview.
     139     *
     140     * @since 4.1.0
     141     * @access public
     142     *
     143     * @return bool Whether the panel is active to the current preview.
     144     */
     145    public final function active() {
     146        $panel = $this;
     147        $active = call_user_func( $this->active_callback, $this );
     148
     149        /**
     150         * Filter response of WP_Customize_Panel::active().
     151         *
     152         * @since 4.1.0
     153         *
     154         * @param bool                 $active  Whether the Customizer panel is active.
     155         * @param WP_Customize_Panel $panel WP_Customize_Panel instance.
     156         */
     157        $active = apply_filters( 'customize_panel_active', $active, $panel );
     158
     159        return $active;
     160    }
     161
     162    /**
     163     * Default callback used when invoking WP_Customize_Panel::active().
     164     *
     165     * Subclasses can override this with their specific logic, or they may
     166     * provide an 'active_callback' argument to the constructor.
     167     *
     168     * @since 4.1.0
     169     * @access public
     170     *
     171     * @return bool Always true.
     172     */
     173    public function active_callback() {
     174        return true;
     175    }
     176
     177    /**
     178     * Gather the parameters passed to client JavaScript via JSON.
     179     *
     180     * @since 4.1.0
     181     *
     182     * @return array The array to be exported to the client as JSON
     183     */
     184    public function json() {
     185        $array = wp_array_slice_assoc( (array) $this, array( 'title', 'description', 'priority', 'type' ) );
     186        $array['content'] = $this->get_content();
     187        $array['active'] = $this->active();
     188        return $array;
    110189    }
    111190
     
    128207
    129208        return true;
     209    }
     210
     211    /**
     212     * Get the panel's content template for insertion into the Customizer pane.
     213     *
     214     * @since 4.1.0
     215     *
     216     * @return string
     217     */
     218    public final function get_content() {
     219        ob_start();
     220        $this->maybe_render();
     221        $template = trim( ob_get_contents() );
     222        ob_end_clean();
     223        return $template;
    130224    }
    131225
     
    190284    protected function render_content() {
    191285        ?>
    192         <li class="accordion-section control-section<?php if ( empty( $this->description ) ) echo ' cannot-expand'; ?>">
     286        <li class="panel-meta accordion-section control-section<?php if ( empty( $this->description ) ) { echo ' cannot-expand'; } ?>">
    193287            <div class="accordion-section-title" tabindex="0">
    194288                <span class="preview-notice"><?php
     
    204298        </li>
    205299        <?php
    206         foreach ( $this->sections as $section ) {
    207             $section->maybe_render();
    208         }
    209300    }
    210301}
Note: See TracChangeset for help on using the changeset viewer.