[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * WordPress Customize Section classes
   4   *
   5   * @package WordPress
   6   * @subpackage Customize
   7   * @since 3.4.0
   8   */
   9  
  10  /**
  11   * Customize Section class.
  12   *
  13   * A UI container for controls, managed by the WP_Customize_Manager class.
  14   *
  15   * @since 3.4.0
  16   *
  17   * @see WP_Customize_Manager
  18   */
  19  class WP_Customize_Section {
  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 3.4.0
  43       * @var WP_Customize_Manager
  44       */
  45      public $manager;
  46  
  47      /**
  48       * Unique identifier.
  49       *
  50       * @since 3.4.0
  51       * @var string
  52       */
  53      public $id;
  54  
  55      /**
  56       * Priority of the section which informs load order of sections.
  57       *
  58       * @since 3.4.0
  59       * @var int
  60       */
  61      public $priority = 160;
  62  
  63      /**
  64       * Panel in which to show the section, making it a sub-section.
  65       *
  66       * @since 4.0.0
  67       * @var string
  68       */
  69      public $panel = '';
  70  
  71      /**
  72       * Capability required for the section.
  73       *
  74       * @since 3.4.0
  75       * @var string
  76       */
  77      public $capability = 'edit_theme_options';
  78  
  79      /**
  80       * Theme features required to support the section.
  81       *
  82       * @since 3.4.0
  83       * @var string|string[]
  84       */
  85      public $theme_supports = '';
  86  
  87      /**
  88       * Title of the section to show in UI.
  89       *
  90       * @since 3.4.0
  91       * @var string
  92       */
  93      public $title = '';
  94  
  95      /**
  96       * Description to show in the UI.
  97       *
  98       * @since 3.4.0
  99       * @var string
 100       */
 101      public $description = '';
 102  
 103      /**
 104       * Customizer controls for this section.
 105       *
 106       * @since 3.4.0
 107       * @var array
 108       */
 109      public $controls;
 110  
 111      /**
 112       * Type of this section.
 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       * Show the description or hide it behind the help icon.
 135       *
 136       * @since 4.7.0
 137       *
 138       * @var bool Indicates whether the Section's description should be
 139       *           hidden behind a help icon ("?") in the Section header,
 140       *           similar to how help icons are displayed on Panels.
 141       */
 142      public $description_hidden = false;
 143  
 144      /**
 145       * Constructor.
 146       *
 147       * Any supplied $args override class property defaults.
 148       *
 149       * @since 3.4.0
 150       *
 151       * @param WP_Customize_Manager $manager Customizer bootstrap instance.
 152       * @param string               $id      A specific ID of the section.
 153       * @param array                $args    {
 154       *     Optional. Array of properties for the new Section object. Default empty array.
 155       *
 156       *     @type int             $priority           Priority of the section, defining the display order
 157       *                                               of panels and sections. Default 160.
 158       *     @type string          $panel              The panel this section belongs to (if any).
 159       *                                               Default empty.
 160       *     @type string          $capability         Capability required for the section.
 161       *                                               Default 'edit_theme_options'
 162       *     @type string|string[] $theme_supports     Theme features required to support the section.
 163       *     @type string          $title              Title of the section to show in UI.
 164       *     @type string          $description        Description to show in the UI.
 165       *     @type string          $type               Type of the section.
 166       *     @type callable        $active_callback    Active callback.
 167       *     @type bool            $description_hidden Hide the description behind a help icon,
 168       *                                               instead of inline above the first control.
 169       *                                               Default false.
 170       * }
 171       */
 172  	public function __construct( $manager, $id, $args = array() ) {
 173          $keys = array_keys( get_object_vars( $this ) );
 174          foreach ( $keys as $key ) {
 175              if ( isset( $args[ $key ] ) ) {
 176                  $this->$key = $args[ $key ];
 177              }
 178          }
 179  
 180          $this->manager = $manager;
 181          $this->id      = $id;
 182          if ( empty( $this->active_callback ) ) {
 183              $this->active_callback = array( $this, 'active_callback' );
 184          }
 185          self::$instance_count += 1;
 186          $this->instance_number = self::$instance_count;
 187  
 188          $this->controls = array(); // Users cannot customize the $controls array.
 189      }
 190  
 191      /**
 192       * Check whether section is active to current Customizer preview.
 193       *
 194       * @since 4.1.0
 195       *
 196       * @return bool Whether the section is active to the current preview.
 197       */
 198  	final public function active() {
 199          $section = $this;
 200          $active  = call_user_func( $this->active_callback, $this );
 201  
 202          /**
 203           * Filters response of WP_Customize_Section::active().
 204           *
 205           * @since 4.1.0
 206           *
 207           * @param bool                 $active  Whether the Customizer section is active.
 208           * @param WP_Customize_Section $section WP_Customize_Section instance.
 209           */
 210          $active = apply_filters( 'customize_section_active', $active, $section );
 211  
 212          return $active;
 213      }
 214  
 215      /**
 216       * Default callback used when invoking WP_Customize_Section::active().
 217       *
 218       * Subclasses can override this with their specific logic, or they may provide
 219       * an 'active_callback' argument to the constructor.
 220       *
 221       * @since 4.1.0
 222       *
 223       * @return true Always true.
 224       */
 225  	public function active_callback() {
 226          return true;
 227      }
 228  
 229      /**
 230       * Gather the parameters passed to client JavaScript via JSON.
 231       *
 232       * @since 4.1.0
 233       *
 234       * @return array The array to be exported to the client as JSON.
 235       */
 236  	public function json() {
 237          $array                   = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'panel', 'type', 'description_hidden' ) );
 238          $array['title']          = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) );
 239          $array['content']        = $this->get_content();
 240          $array['active']         = $this->active();
 241          $array['instanceNumber'] = $this->instance_number;
 242  
 243          if ( $this->panel ) {
 244              /* translators: &#9656; is the unicode right-pointing triangle. %s: Section title in the Customizer. */
 245              $array['customizeAction'] = sprintf( __( 'Customizing &#9656; %s' ), esc_html( $this->manager->get_panel( $this->panel )->title ) );
 246          } else {
 247              $array['customizeAction'] = __( 'Customizing' );
 248          }
 249  
 250          return $array;
 251      }
 252  
 253      /**
 254       * Checks required user capabilities and whether the theme has the
 255       * feature support required by the section.
 256       *
 257       * @since 3.4.0
 258       *
 259       * @return bool False if theme doesn't support the section or user doesn't have the capability.
 260       */
 261  	final public function check_capabilities() {
 262          if ( $this->capability && ! current_user_can( $this->capability ) ) {
 263              return false;
 264          }
 265  
 266          if ( $this->theme_supports && ! current_theme_supports( ... (array) $this->theme_supports ) ) {
 267              return false;
 268          }
 269  
 270          return true;
 271      }
 272  
 273      /**
 274       * Get the section's content for insertion into the Customizer pane.
 275       *
 276       * @since 4.1.0
 277       *
 278       * @return string Contents of the section.
 279       */
 280  	final public function get_content() {
 281          ob_start();
 282          $this->maybe_render();
 283          return trim( ob_get_clean() );
 284      }
 285  
 286      /**
 287       * Check capabilities and render the section.
 288       *
 289       * @since 3.4.0
 290       */
 291  	final public function maybe_render() {
 292          if ( ! $this->check_capabilities() ) {
 293              return;
 294          }
 295  
 296          /**
 297           * Fires before rendering a Customizer section.
 298           *
 299           * @since 3.4.0
 300           *
 301           * @param WP_Customize_Section $section WP_Customize_Section instance.
 302           */
 303          do_action( 'customize_render_section', $this );
 304          /**
 305           * Fires before rendering a specific Customizer section.
 306           *
 307           * The dynamic portion of the hook name, `$this->id`, refers to the ID
 308           * of the specific Customizer section to be rendered.
 309           *
 310           * @since 3.4.0
 311           */
 312          do_action( "customize_render_section_{$this->id}" );
 313  
 314          $this->render();
 315      }
 316  
 317      /**
 318       * Render the section UI in a subclass.
 319       *
 320       * Sections are now rendered in JS by default, see WP_Customize_Section::print_template().
 321       *
 322       * @since 3.4.0
 323       */
 324  	protected function render() {}
 325  
 326      /**
 327       * Render the section's JS template.
 328       *
 329       * This function is only run for section types that have been registered with
 330       * WP_Customize_Manager::register_section_type().
 331       *
 332       * @since 4.3.0
 333       *
 334       * @see WP_Customize_Manager::render_template()
 335       */
 336  	public function print_template() {
 337          ?>
 338          <script type="text/html" id="tmpl-customize-section-<?php echo $this->type; ?>">
 339              <?php $this->render_template(); ?>
 340          </script>
 341          <?php
 342      }
 343  
 344      /**
 345       * An Underscore (JS) template for rendering this section.
 346       *
 347       * Class variables for this section class are available in the `data` JS object;
 348       * export custom variables by overriding WP_Customize_Section::json().
 349       *
 350       * @since 4.3.0
 351       *
 352       * @see WP_Customize_Section::print_template()
 353       */
 354  	protected function render_template() {
 355          ?>
 356          <li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }}">
 357              <h3 class="accordion-section-title" tabindex="0">
 358                  {{ data.title }}
 359                  <span class="screen-reader-text"><?php _e( 'Press return or enter to open this section' ); ?></span>
 360              </h3>
 361              <ul class="accordion-section-content">
 362                  <li class="customize-section-description-container section-meta <# if ( data.description_hidden ) { #>customize-info<# } #>">
 363                      <div class="customize-section-title">
 364                          <button class="customize-section-back" tabindex="-1">
 365                              <span class="screen-reader-text"><?php _e( 'Back' ); ?></span>
 366                          </button>
 367                          <h3>
 368                              <span class="customize-action">
 369                                  {{{ data.customizeAction }}}
 370                              </span>
 371                              {{ data.title }}
 372                          </h3>
 373                          <# if ( data.description && data.description_hidden ) { #>
 374                              <button type="button" class="customize-help-toggle dashicons dashicons-editor-help" aria-expanded="false"><span class="screen-reader-text"><?php _e( 'Help' ); ?></span></button>
 375                              <div class="description customize-section-description">
 376                                  {{{ data.description }}}
 377                              </div>
 378                          <# } #>
 379  
 380                          <div class="customize-control-notifications-container"></div>
 381                      </div>
 382  
 383                      <# if ( data.description && ! data.description_hidden ) { #>
 384                          <div class="description customize-section-description">
 385                              {{{ data.description }}}
 386                          </div>
 387                      <# } #>
 388                  </li>
 389              </ul>
 390          </li>
 391          <?php
 392      }
 393  }
 394  
 395  /** WP_Customize_Themes_Section class */
 396  require_once  ABSPATH . WPINC . '/customize/class-wp-customize-themes-section.php';
 397  
 398  /** WP_Customize_Sidebar_Section class */
 399  require_once  ABSPATH . WPINC . '/customize/class-wp-customize-sidebar-section.php';
 400  
 401  /** WP_Customize_Nav_Menu_Section class */
 402  require_once  ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-section.php';


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1