Make WordPress Core


Ignore:
Timestamp:
02/24/2016 06:27:45 PM (9 years ago)
Author:
westonruter
Message:

Customize: Allow controls to be registered without any associated settings.

  • Improves parity between partials and controls. A partial or control can be settingless if instantiated with settings param as empty array (otherwise, if null, then the partial/control ID is used).
  • Eliminate need to create dummy settings that serve no purpose except to place a control in the UI.
  • Removes dummy settings for create_new_menu and new_menu_name.
  • Introduces WP_Customize_Control::$capability and WP_Customize_Partial::$capability, and if set checks them in the respective check_capabilities() methods.
  • Prevents PHP fatal error from happening when non-existing settings are provided to control: "Call to a member function check_capabilities() on a non-object".
  • Fixes issue where nav menu items and widgets were no longer working with selective refresh because cap check was failing.

See #27355.
Fixes #35926.

File:
1 edited

Legend:

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

    r36114 r36689  
    6464     */
    6565    public $setting = 'default';
     66
     67    /**
     68     * Capability required to use this control.
     69     *
     70     * Normally this is empty and the capability is derived from the capabilities
     71     * of the associated `$settings`.
     72     *
     73     * @since 4.5.0
     74     * @access public
     75     * @var string
     76     */
     77    public $capability;
    6678
    6779    /**
     
    188200
    189201        // Process settings.
    190         if ( empty( $this->settings ) ) {
     202        if ( ! isset( $this->settings ) ) {
    191203            $this->settings = $id;
    192204        }
     
    197209                $settings[ $key ] = $this->manager->get_setting( $setting );
    198210            }
    199         } else {
     211        } else if ( is_string( $this->settings ) ) {
    200212            $this->setting = $this->manager->get_setting( $this->settings );
    201213            $settings['default'] = $this->setting;
     
    300312
    301313    /**
    302      * Check if the theme supports the control and check user capabilities.
     314     * Checks if the user can use this control.
     315     *
     316     * Returns false if the user cannot manipulate one of the associated settings,
     317     * or if one of the associated settings does not exist. Also returns false if
     318     * the associated section does not exist or if its capability check returns
     319     * false.
    303320     *
    304321     * @since 3.4.0
     
    307324     */
    308325    final public function check_capabilities() {
     326        if ( ! empty( $this->capability ) && ! current_user_can( $this->capability ) ) {
     327            return false;
     328        }
     329
    309330        foreach ( $this->settings as $setting ) {
    310             if ( ! $setting->check_capabilities() )
     331            if ( ! $setting || ! $setting->check_capabilities() ) {
    311332                return false;
     333            }
    312334        }
    313335
    314336        $section = $this->manager->get_section( $this->section );
    315         if ( isset( $section ) && ! $section->check_capabilities() )
     337        if ( isset( $section ) && ! $section->check_capabilities() ) {
    316338            return false;
     339        }
    317340
    318341        return true;
Note: See TracChangeset for help on using the changeset viewer.