Make WordPress Core


Ignore:
Timestamp:
07/09/2014 11:57:29 PM (11 years ago)
Author:
SergeyBiryukov
Message:

Customizer: Introduce WP_Customize_Control::active() method to determine whether the control is relevant to the current context (i.e. to the current URL being previewed).

Control can indicate its active state by a subclass overriding the 'active_callback' method, by supplying a callable 'active_callback' argument into the control's constructor, or by filtering 'customize_control_active'.

props westonruter.
see #27993.

File:
1 edited

Legend:

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

    r28980 r29051  
    8686    public $type = 'text';
    8787
     88    /**
     89     * Callback
     90     *
     91     * @since 4.0.0
     92     *
     93     * @access public
     94     * @see WP_Customize_Control::active()
     95     * @var callable  Callback is called with one argument, the instance of
     96     *                WP_Customize_Control, and returns bool to indicate whether
     97     *                the control is active (such as it relates to the URL
     98     *                currently being previewed).
     99     */
     100    public $active_callback = '';
    88101
    89102    /**
     
    103116        $keys = array_keys( get_object_vars( $this ) );
    104117        foreach ( $keys as $key ) {
    105             if ( isset( $args[ $key ] ) )
     118            if ( isset( $args[ $key ] ) ) {
    106119                $this->$key = $args[ $key ];
     120            }
    107121        }
    108122
    109123        $this->manager = $manager;
    110124        $this->id = $id;
     125        if ( empty( $this->active_callback ) ) {
     126            $this->active_callback = array( $this, 'active_callback' );
     127        }
    111128
    112129        // Process settings.
    113         if ( empty( $this->settings ) )
     130        if ( empty( $this->settings ) ) {
    114131            $this->settings = $id;
     132        }
    115133
    116134        $settings = array();
     
    133151    public function enqueue() {}
    134152
     153    /**
     154     * Check whether control is active to current customizer preview.
     155     *
     156     * @since 4.0.0
     157     *
     158     * @return bool
     159     */
     160    public final function active() {
     161        $control = $this;
     162        $active = call_user_func( $this->active_callback, $this );
     163
     164        /**
     165         * Filter response of WP_Customize_Control::active().
     166         *
     167         * @since 4.0.0
     168         *
     169         * @param bool $active
     170         * @param WP_Customize_Control $control
     171         */
     172        $active = apply_filters( 'customize_control_active', $active, $control );
     173
     174        return $active;
     175    }
     176
     177    /**
     178     * Default callback used when invoking WP_Customize_Control::active().
     179     *
     180     * Subclasses can override this with their specific logic, or they may
     181     * provide an 'active_callback' argument to the constructor.
     182     *
     183     * @return bool
     184     */
     185    public function active_callback() {
     186        return true;
     187    }
    135188
    136189    /**
     
    144197     */
    145198    public final function value( $setting_key = 'default' ) {
    146         if ( isset( $this->settings[ $setting_key ] ) )
     199        if ( isset( $this->settings[ $setting_key ] ) ) {
    147200            return $this->settings[ $setting_key ]->value();
     201        }
    148202    }
    149203
     
    160214
    161215        $this->json['type'] = $this->type;
     216        $this->json['active'] = $this->active();
    162217    }
    163218
     
    257312    }
    258313
    259     /**
     314    /**
    260315     * Render the custom attributes for the control's input element.
    261316     *
     
    9961051        <?php
    9971052    }
     1053
     1054    /**
     1055     * @return bool
     1056     */
     1057    function active_callback() {
     1058        return $this->manager->widgets->is_sidebar_rendered( $this->sidebar_id );
     1059    }
    9981060}
    9991061
     
    10361098        echo $this->manager->widgets->get_widget_control( $args );
    10371099    }
     1100
     1101    /**
     1102     * @return bool
     1103     */
     1104    function active_callback() {
     1105        return $this->manager->widgets->is_widget_rendered( $this->widget_id );
     1106    }
    10381107}
    10391108
Note: See TracChangeset for help on using the changeset viewer.