[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/ -> class-wp-customize-panel.php (source)

   1  <?php
   2  /**
   3   * WordPress Customize Panel classes
   4   *
   5   * @package WordPress
   6   * @subpackage Customize
   7   * @since 4.0.0
   8   */
   9  
  10  /**
  11   * Customize Panel class.
  12   *
  13   * A UI container for sections, managed by the WP_Customize_Manager.
  14   *
  15   * @since 4.0.0
  16   *
  17   * @see WP_Customize_Manager
  18   */
  19  class WP_Customize_Panel {
  20  
  21      /**
  22       * Incremented with each new class instantiation, then stored in $instance_number.
  23       *
  24       * Used when sorting two instances whose priorities are equal.
  25       *
  26       * @since 4.1.0
  27       * @var int
  28       */
  29      protected static $instance_count = 0;
  30  
  31      /**
  32       * Order in which this instance was created in relation to other instances.
  33       *
  34       * @since 4.1.0
  35       * @var int
  36       */
  37      public $instance_number;
  38  
  39      /**
  40       * WP_Customize_Manager instance.
  41       *
  42       * @since 4.0.0
  43       * @var WP_Customize_Manager
  44       */
  45      public $manager;
  46  
  47      /**
  48       * Unique identifier.
  49       *
  50       * @since 4.0.0
  51       * @var string
  52       */
  53      public $id;
  54  
  55      /**
  56       * Priority of the panel, defining the display order of panels and sections.
  57       *
  58       * @since 4.0.0
  59       * @var integer
  60       */
  61      public $priority = 160;
  62  
  63      /**
  64       * Capability required for the panel.
  65       *
  66       * @since 4.0.0
  67       * @var string
  68       */
  69      public $capability = 'edit_theme_options';
  70  
  71      /**
  72       * Theme feature support for the panel.
  73       *
  74       * @since 4.0.0
  75       * @var string|array
  76       */
  77      public $theme_supports = '';
  78  
  79      /**
  80       * Title of the panel to show in UI.
  81       *
  82       * @since 4.0.0
  83       * @var string
  84       */
  85      public $title = '';
  86  
  87      /**
  88       * Description to show in the UI.
  89       *
  90       * @since 4.0.0
  91       * @var string
  92       */
  93      public $description = '';
  94  
  95      /**
  96       * Auto-expand a section in a panel when the panel is expanded when the panel only has the one section.
  97       *
  98       * @since 4.7.4
  99       * @var bool
 100       */
 101      public $auto_expand_sole_section = false;
 102  
 103      /**
 104       * Customizer sections for this panel.
 105       *
 106       * @since 4.0.0
 107       * @var array
 108       */
 109      public $sections;
 110  
 111      /**
 112       * Type of this panel.
 113       *
 114       * @since 4.1.0
 115       * @var string
 116       */
 117      public $type = 'default';
 118  
 119      /**
 120       * Active callback.
 121       *
 122       * @since 4.1.0
 123       *
 124       * @see WP_Customize_Section::active()
 125       *
 126       * @var callable Callback is called with one argument, the instance of
 127       *               WP_Customize_Section, and returns bool to indicate whether
 128       *               the section is active (such as it relates to the URL currently
 129       *               being previewed).
 130       */
 131      public $active_callback = '';
 132  
 133      /**
 134       * Constructor.
 135       *
 136       * Any supplied $args override class property defaults.
 137       *
 138       * @since 4.0.0
 139       *
 140       * @param WP_Customize_Manager $manager Customizer bootstrap instance.
 141       * @param string               $id      An specific ID for the panel.
 142       * @param array                $args    Panel arguments.
 143       */
 144  	public function __construct( $manager, $id, $args = array() ) {
 145          $keys = array_keys( get_object_vars( $this ) );
 146          foreach ( $keys as $key ) {
 147              if ( isset( $args[ $key ] ) ) {
 148                  $this->$key = $args[ $key ];
 149              }
 150          }
 151  
 152          $this->manager = $manager;
 153          $this->id      = $id;
 154          if ( empty( $this->active_callback ) ) {
 155              $this->active_callback = array( $this, 'active_callback' );
 156          }
 157          self::$instance_count += 1;
 158          $this->instance_number = self::$instance_count;
 159  
 160          $this->sections = array(); // Users cannot customize the $sections array.
 161      }
 162  
 163      /**
 164       * Check whether panel is active to current Customizer preview.
 165       *
 166       * @since 4.1.0
 167       *
 168       * @return bool Whether the panel is active to the current preview.
 169       */
 170  	final public function active() {
 171          $panel  = $this;
 172          $active = call_user_func( $this->active_callback, $this );
 173  
 174          /**
 175           * Filters response of WP_Customize_Panel::active().
 176           *
 177           * @since 4.1.0
 178           *
 179           * @param bool               $active Whether the Customizer panel is active.
 180           * @param WP_Customize_Panel $panel  WP_Customize_Panel instance.
 181           */
 182          $active = apply_filters( 'customize_panel_active', $active, $panel );
 183  
 184          return $active;
 185      }
 186  
 187      /**
 188       * Default callback used when invoking WP_Customize_Panel::active().
 189       *
 190       * Subclasses can override this with their specific logic, or they may
 191       * provide an 'active_callback' argument to the constructor.
 192       *
 193       * @since 4.1.0
 194       *
 195       * @return bool Always true.
 196       */
 197  	public function active_callback() {
 198          return true;
 199      }
 200  
 201      /**
 202       * Gather the parameters passed to client JavaScript via JSON.
 203       *
 204       * @since 4.1.0
 205       *
 206       * @return array The array to be exported to the client as JSON.
 207       */
 208  	public function json() {
 209          $array                          = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'type' ) );
 210          $array['title']                 = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) );
 211          $array['content']               = $this->get_content();
 212          $array['active']                = $this->active();
 213          $array['instanceNumber']        = $this->instance_number;
 214          $array['autoExpandSoleSection'] = $this->auto_expand_sole_section;
 215          return $array;
 216      }
 217  
 218      /**
 219       * Checks required user capabilities and whether the theme has the
 220       * feature support required by the panel.
 221       *
 222       * @since 4.0.0
 223       *
 224       * @return bool False if theme doesn't support the panel or the user doesn't have the capability.
 225       */
 226  	final public function check_capabilities() {
 227          if ( $this->capability && ! current_user_can( $this->capability ) ) {
 228              return false;
 229          }
 230  
 231          if ( $this->theme_supports && ! current_theme_supports( ... (array) $this->theme_supports ) ) {
 232              return false;
 233          }
 234  
 235          return true;
 236      }
 237  
 238      /**
 239       * Get the panel's content template for insertion into the Customizer pane.
 240       *
 241       * @since 4.1.0
 242       *
 243       * @return string Content for the panel.
 244       */
 245  	final public function get_content() {
 246          ob_start();
 247          $this->maybe_render();
 248          return trim( ob_get_clean() );
 249      }
 250  
 251      /**
 252       * Check capabilities and render the panel.
 253       *
 254       * @since 4.0.0
 255       */
 256  	final public function maybe_render() {
 257          if ( ! $this->check_capabilities() ) {
 258              return;
 259          }
 260  
 261          /**
 262           * Fires before rendering a Customizer panel.
 263           *
 264           * @since 4.0.0
 265           *
 266           * @param WP_Customize_Panel $this WP_Customize_Panel instance.
 267           */
 268          do_action( 'customize_render_panel', $this );
 269  
 270          /**
 271           * Fires before rendering a specific Customizer panel.
 272           *
 273           * The dynamic portion of the hook name, `$this->id`, refers to
 274           * the ID of the specific Customizer panel to be rendered.
 275           *
 276           * @since 4.0.0
 277           */
 278          do_action( "customize_render_panel_{$this->id}" );
 279  
 280          $this->render();
 281      }
 282  
 283      /**
 284       * Render the panel container, and then its contents (via `this->render_content()`) in a subclass.
 285       *
 286       * Panel containers are now rendered in JS by default, see WP_Customize_Panel::print_template().
 287       *
 288       * @since 4.0.0
 289       */
 290  	protected function render() {}
 291  
 292      /**
 293       * Render the panel UI in a subclass.
 294       *
 295       * Panel contents are now rendered in JS by default, see WP_Customize_Panel::print_template().
 296       *
 297       * @since 4.1.0
 298       */
 299  	protected function render_content() {}
 300  
 301      /**
 302       * Render the panel's JS templates.
 303       *
 304       * This function is only run for panel types that have been registered with
 305       * WP_Customize_Manager::register_panel_type().
 306       *
 307       * @since 4.3.0
 308       *
 309       * @see WP_Customize_Manager::register_panel_type()
 310       */
 311  	public function print_template() {
 312          ?>
 313          <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>-content">
 314              <?php $this->content_template(); ?>
 315          </script>
 316          <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>">
 317              <?php $this->render_template(); ?>
 318          </script>
 319          <?php
 320      }
 321  
 322      /**
 323       * An Underscore (JS) template for rendering this panel's container.
 324       *
 325       * Class variables for this panel class are available in the `data` JS object;
 326       * export custom variables by overriding WP_Customize_Panel::json().
 327       *
 328       * @see WP_Customize_Panel::print_template()
 329       *
 330       * @since 4.3.0
 331       */
 332  	protected function render_template() {
 333          ?>
 334          <li id="accordion-panel-{{ data.id }}" class="accordion-section control-section control-panel control-panel-{{ data.type }}">
 335              <h3 class="accordion-section-title" tabindex="0">
 336                  {{ data.title }}
 337                  <span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span>
 338              </h3>
 339              <ul class="accordion-sub-container control-panel-content"></ul>
 340          </li>
 341          <?php
 342      }
 343  
 344      /**
 345       * An Underscore (JS) template for this panel's content (but not its container).
 346       *
 347       * Class variables for this panel class are available in the `data` JS object;
 348       * export custom variables by overriding WP_Customize_Panel::json().
 349       *
 350       * @see WP_Customize_Panel::print_template()
 351       *
 352       * @since 4.3.0
 353       */
 354  	protected function content_template() {
 355          ?>
 356          <li class="panel-meta customize-info accordion-section <# if ( ! data.description ) { #> cannot-expand<# } #>">
 357              <button class="customize-panel-back" tabindex="-1"><span class="screen-reader-text"><?php _e( 'Back' ); ?></span></button>
 358              <div class="accordion-section-title">
 359                  <span class="preview-notice">
 360                  <?php
 361                      /* translators: %s: The site/panel title in the Customizer. */
 362                      echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">{{ data.title }}</strong>' );
 363                  ?>
 364                  </span>
 365                  <# if ( data.description ) { #>
 366                      <button type="button" class="customize-help-toggle dashicons dashicons-editor-help" aria-expanded="false"><span class="screen-reader-text"><?php _e( 'Help' ); ?></span></button>
 367                  <# } #>
 368              </div>
 369              <# if ( data.description ) { #>
 370                  <div class="description customize-panel-description">
 371                      {{{ data.description }}}
 372                  </div>
 373              <# } #>
 374  
 375              <div class="customize-control-notifications-container"></div>
 376          </li>
 377          <?php
 378      }
 379  }
 380  
 381  /** WP_Customize_Nav_Menus_Panel class */
 382  require_once ( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menus-panel.php' );


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7