wp/wp-includes/class-wp-customize-panel.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
    22 	 * Incremented with each new class instantiation, then stored in $instance_number.
    22 	 * Incremented with each new class instantiation, then stored in $instance_number.
    23 	 *
    23 	 *
    24 	 * Used when sorting two instances whose priorities are equal.
    24 	 * Used when sorting two instances whose priorities are equal.
    25 	 *
    25 	 *
    26 	 * @since 4.1.0
    26 	 * @since 4.1.0
    27 	 * @access protected
    27 	 *
       
    28 	 * @static
    28 	 * @var int
    29 	 * @var int
    29 	 */
    30 	 */
    30 	protected static $instance_count = 0;
    31 	protected static $instance_count = 0;
    31 
    32 
    32 	/**
    33 	/**
    33 	 * Order in which this instance was created in relation to other instances.
    34 	 * Order in which this instance was created in relation to other instances.
    34 	 *
    35 	 *
    35 	 * @since 4.1.0
    36 	 * @since 4.1.0
    36 	 * @access public
       
    37 	 * @var int
    37 	 * @var int
    38 	 */
    38 	 */
    39 	public $instance_number;
    39 	public $instance_number;
    40 
    40 
    41 	/**
    41 	/**
    42 	 * WP_Customize_Manager instance.
    42 	 * WP_Customize_Manager instance.
    43 	 *
    43 	 *
    44 	 * @since 4.0.0
    44 	 * @since 4.0.0
    45 	 * @access public
       
    46 	 * @var WP_Customize_Manager
    45 	 * @var WP_Customize_Manager
    47 	 */
    46 	 */
    48 	public $manager;
    47 	public $manager;
    49 
    48 
    50 	/**
    49 	/**
    51 	 * Unique identifier.
    50 	 * Unique identifier.
    52 	 *
    51 	 *
    53 	 * @since 4.0.0
    52 	 * @since 4.0.0
    54 	 * @access public
       
    55 	 * @var string
    53 	 * @var string
    56 	 */
    54 	 */
    57 	public $id;
    55 	public $id;
    58 
    56 
    59 	/**
    57 	/**
    60 	 * Priority of the panel, defining the display order of panels and sections.
    58 	 * Priority of the panel, defining the display order of panels and sections.
    61 	 *
    59 	 *
    62 	 * @since 4.0.0
    60 	 * @since 4.0.0
    63 	 * @access public
       
    64 	 * @var integer
    61 	 * @var integer
    65 	 */
    62 	 */
    66 	public $priority = 160;
    63 	public $priority = 160;
    67 
    64 
    68 	/**
    65 	/**
    69 	 * Capability required for the panel.
    66 	 * Capability required for the panel.
    70 	 *
    67 	 *
    71 	 * @since 4.0.0
    68 	 * @since 4.0.0
    72 	 * @access public
       
    73 	 * @var string
    69 	 * @var string
    74 	 */
    70 	 */
    75 	public $capability = 'edit_theme_options';
    71 	public $capability = 'edit_theme_options';
    76 
    72 
    77 	/**
    73 	/**
    78 	 * Theme feature support for the panel.
    74 	 * Theme feature support for the panel.
    79 	 *
    75 	 *
    80 	 * @since 4.0.0
    76 	 * @since 4.0.0
    81 	 * @access public
       
    82 	 * @var string|array
    77 	 * @var string|array
    83 	 */
    78 	 */
    84 	public $theme_supports = '';
    79 	public $theme_supports = '';
    85 
    80 
    86 	/**
    81 	/**
    87 	 * Title of the panel to show in UI.
    82 	 * Title of the panel to show in UI.
    88 	 *
    83 	 *
    89 	 * @since 4.0.0
    84 	 * @since 4.0.0
    90 	 * @access public
       
    91 	 * @var string
    85 	 * @var string
    92 	 */
    86 	 */
    93 	public $title = '';
    87 	public $title = '';
    94 
    88 
    95 	/**
    89 	/**
    96 	 * Description to show in the UI.
    90 	 * Description to show in the UI.
    97 	 *
    91 	 *
    98 	 * @since 4.0.0
    92 	 * @since 4.0.0
    99 	 * @access public
       
   100 	 * @var string
    93 	 * @var string
   101 	 */
    94 	 */
   102 	public $description = '';
    95 	public $description = '';
   103 
    96 
   104 	/**
    97 	/**
       
    98 	 * Auto-expand a section in a panel when the panel is expanded when the panel only has the one section.
       
    99 	 *
       
   100 	 * @since 4.7.4
       
   101 	 * @var bool
       
   102 	 */
       
   103 	public $auto_expand_sole_section = false;
       
   104 
       
   105 	/**
   105 	 * Customizer sections for this panel.
   106 	 * Customizer sections for this panel.
   106 	 *
   107 	 *
   107 	 * @since 4.0.0
   108 	 * @since 4.0.0
   108 	 * @access public
       
   109 	 * @var array
   109 	 * @var array
   110 	 */
   110 	 */
   111 	public $sections;
   111 	public $sections;
   112 
   112 
   113 	/**
   113 	/**
   114 	 * Type of this panel.
   114 	 * Type of this panel.
   115 	 *
   115 	 *
   116 	 * @since 4.1.0
   116 	 * @since 4.1.0
   117 	 * @access public
       
   118 	 * @var string
   117 	 * @var string
   119 	 */
   118 	 */
   120 	public $type = 'default';
   119 	public $type = 'default';
   121 
   120 
   122 	/**
   121 	/**
   123 	 * Active callback.
   122 	 * Active callback.
   124 	 *
   123 	 *
   125 	 * @since 4.1.0
   124 	 * @since 4.1.0
   126 	 * @access public
       
   127 	 *
   125 	 *
   128 	 * @see WP_Customize_Section::active()
   126 	 * @see WP_Customize_Section::active()
   129 	 *
   127 	 *
   130 	 * @var callable Callback is called with one argument, the instance of
   128 	 * @var callable Callback is called with one argument, the instance of
   131 	 *               {@see WP_Customize_Section}, and returns bool to indicate
   129 	 *               WP_Customize_Section, and returns bool to indicate whether
   132 	 *               whether the section is active (such as it relates to the URL
   130 	 *               the section is active (such as it relates to the URL currently
   133 	 *               currently being previewed).
   131 	 *               being previewed).
   134 	 */
   132 	 */
   135 	public $active_callback = '';
   133 	public $active_callback = '';
   136 
   134 
   137 	/**
   135 	/**
   138 	 * Constructor.
   136 	 * Constructor.
   166 
   164 
   167 	/**
   165 	/**
   168 	 * Check whether panel is active to current Customizer preview.
   166 	 * Check whether panel is active to current Customizer preview.
   169 	 *
   167 	 *
   170 	 * @since 4.1.0
   168 	 * @since 4.1.0
   171 	 * @access public
       
   172 	 *
   169 	 *
   173 	 * @return bool Whether the panel is active to the current preview.
   170 	 * @return bool Whether the panel is active to the current preview.
   174 	 */
   171 	 */
   175 	final public function active() {
   172 	final public function active() {
   176 		$panel = $this;
   173 		$panel = $this;
   177 		$active = call_user_func( $this->active_callback, $this );
   174 		$active = call_user_func( $this->active_callback, $this );
   178 
   175 
   179 		/**
   176 		/**
   180 		 * Filter response of WP_Customize_Panel::active().
   177 		 * Filters response of WP_Customize_Panel::active().
   181 		 *
   178 		 *
   182 		 * @since 4.1.0
   179 		 * @since 4.1.0
   183 		 *
   180 		 *
   184 		 * @param bool               $active  Whether the Customizer panel is active.
   181 		 * @param bool               $active Whether the Customizer panel is active.
   185 		 * @param WP_Customize_Panel $panel   {@see WP_Customize_Panel} instance.
   182 		 * @param WP_Customize_Panel $panel  WP_Customize_Panel instance.
   186 		 */
   183 		 */
   187 		$active = apply_filters( 'customize_panel_active', $active, $panel );
   184 		$active = apply_filters( 'customize_panel_active', $active, $panel );
   188 
   185 
   189 		return $active;
   186 		return $active;
   190 	}
   187 	}
   191 
   188 
   192 	/**
   189 	/**
   193 	 * Default callback used when invoking {@see WP_Customize_Panel::active()}.
   190 	 * Default callback used when invoking WP_Customize_Panel::active().
   194 	 *
   191 	 *
   195 	 * Subclasses can override this with their specific logic, or they may
   192 	 * Subclasses can override this with their specific logic, or they may
   196 	 * provide an 'active_callback' argument to the constructor.
   193 	 * provide an 'active_callback' argument to the constructor.
   197 	 *
   194 	 *
   198 	 * @since 4.1.0
   195 	 * @since 4.1.0
   199 	 * @access public
       
   200 	 *
   196 	 *
   201 	 * @return bool Always true.
   197 	 * @return bool Always true.
   202 	 */
   198 	 */
   203 	public function active_callback() {
   199 	public function active_callback() {
   204 		return true;
   200 		return true;
   210 	 * @since 4.1.0
   206 	 * @since 4.1.0
   211 	 *
   207 	 *
   212 	 * @return array The array to be exported to the client as JSON.
   208 	 * @return array The array to be exported to the client as JSON.
   213 	 */
   209 	 */
   214 	public function json() {
   210 	public function json() {
   215 		$array = wp_array_slice_assoc( (array) $this, array( 'title', 'description', 'priority', 'type' ) );
   211 		$array = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'type' ) );
       
   212 		$array['title'] = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) );
   216 		$array['content'] = $this->get_content();
   213 		$array['content'] = $this->get_content();
   217 		$array['active'] = $this->active();
   214 		$array['active'] = $this->active();
   218 		$array['instanceNumber'] = $this->instance_number;
   215 		$array['instanceNumber'] = $this->instance_number;
       
   216 		$array['autoExpandSoleSection'] = $this->auto_expand_sole_section;
   219 		return $array;
   217 		return $array;
   220 	}
   218 	}
   221 
   219 
   222 	/**
   220 	/**
   223 	 * Checks required user capabilities and whether the theme has the
   221 	 * Checks required user capabilities and whether the theme has the
   247 	 * @return string Content for the panel.
   245 	 * @return string Content for the panel.
   248 	 */
   246 	 */
   249 	final public function get_content() {
   247 	final public function get_content() {
   250 		ob_start();
   248 		ob_start();
   251 		$this->maybe_render();
   249 		$this->maybe_render();
   252 		$template = trim( ob_get_contents() );
   250 		return trim( ob_get_clean() );
   253 		ob_end_clean();
       
   254 		return $template;
       
   255 	}
   251 	}
   256 
   252 
   257 	/**
   253 	/**
   258 	 * Check capabilities and render the panel.
   254 	 * Check capabilities and render the panel.
   259 	 *
   255 	 *
   285 
   281 
   286 		$this->render();
   282 		$this->render();
   287 	}
   283 	}
   288 
   284 
   289 	/**
   285 	/**
   290 	 * Render the panel container, and then its contents.
   286 	 * Render the panel container, and then its contents (via `this->render_content()`) in a subclass.
   291 	 *
   287 	 *
   292 	 * @since 4.0.0
   288 	 * Panel containers are now rendered in JS by default, see WP_Customize_Panel::print_template().
   293 	 * @access protected
   289 	 *
   294 	 */
   290 	 * @since 4.0.0
   295 	protected function render() {
   291 	 */
   296 		$classes = 'accordion-section control-section control-panel control-panel-' . $this->type;
   292 	protected function render() {}
       
   293 
       
   294 	/**
       
   295 	 * Render the panel UI in a subclass.
       
   296 	 *
       
   297 	 * Panel contents are now rendered in JS by default, see WP_Customize_Panel::print_template().
       
   298 	 *
       
   299 	 * @since 4.1.0
       
   300 	 */
       
   301 	protected function render_content() {}
       
   302 
       
   303 	/**
       
   304 	 * Render the panel's JS templates.
       
   305 	 *
       
   306 	 * This function is only run for panel types that have been registered with
       
   307 	 * WP_Customize_Manager::register_panel_type().
       
   308 	 *
       
   309 	 * @since 4.3.0
       
   310 	 *
       
   311 	 * @see WP_Customize_Manager::register_panel_type()
       
   312 	 */
       
   313 	public function print_template() {
   297 		?>
   314 		?>
   298 		<li id="accordion-panel-<?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>">
   315 		<script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>-content">
       
   316 			<?php $this->content_template(); ?>
       
   317 		</script>
       
   318 		<script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>">
       
   319 			<?php $this->render_template(); ?>
       
   320 		</script>
       
   321         <?php
       
   322 	}
       
   323 
       
   324 	/**
       
   325 	 * An Underscore (JS) template for rendering this panel's container.
       
   326 	 *
       
   327 	 * Class variables for this panel class are available in the `data` JS object;
       
   328 	 * export custom variables by overriding WP_Customize_Panel::json().
       
   329 	 *
       
   330 	 * @see WP_Customize_Panel::print_template()
       
   331 	 *
       
   332 	 * @since 4.3.0
       
   333 	 */
       
   334 	protected function render_template() {
       
   335 		?>
       
   336 		<li id="accordion-panel-{{ data.id }}" class="accordion-section control-section control-panel control-panel-{{ data.type }}">
   299 			<h3 class="accordion-section-title" tabindex="0">
   337 			<h3 class="accordion-section-title" tabindex="0">
   300 				<?php echo esc_html( $this->title ); ?>
   338 				{{ data.title }}
   301 				<span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span>
   339 				<span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span>
   302 			</h3>
   340 			</h3>
   303 			<ul class="accordion-sub-container control-panel-content">
   341 			<ul class="accordion-sub-container control-panel-content"></ul>
   304 				<?php $this->render_content(); ?>
       
   305 			</ul>
       
   306 		</li>
   342 		</li>
   307 		<?php
   343 		<?php
   308 	}
   344 	}
   309 
   345 
   310 	/**
   346 	/**
   311 	 * Render the sections that have been added to the panel.
   347 	 * An Underscore (JS) template for this panel's content (but not its container).
   312 	 *
   348 	 *
   313 	 * @since 4.1.0
   349 	 * Class variables for this panel class are available in the `data` JS object;
   314 	 * @access protected
   350 	 * export custom variables by overriding WP_Customize_Panel::json().
   315 	 */
   351 	 *
   316 	protected function render_content() {
   352 	 * @see WP_Customize_Panel::print_template()
       
   353 	 *
       
   354 	 * @since 4.3.0
       
   355 	 */
       
   356 	protected function content_template() {
   317 		?>
   357 		?>
   318 		<li class="panel-meta accordion-section control-section<?php if ( empty( $this->description ) ) { echo ' cannot-expand'; } ?>">
   358 		<li class="panel-meta customize-info accordion-section <# if ( ! data.description ) { #> cannot-expand<# } #>">
   319 			<div class="accordion-section-title" tabindex="0">
   359 			<button class="customize-panel-back" tabindex="-1"><span class="screen-reader-text"><?php _e( 'Back' ); ?></span></button>
       
   360 			<div class="accordion-section-title">
   320 				<span class="preview-notice"><?php
   361 				<span class="preview-notice"><?php
   321 					/* translators: %s is the site/panel title in the Customizer */
   362 					/* translators: %s: the site/panel title in the Customizer */
   322 					echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">' . esc_html( $this->title ) . '</strong>' );
   363 					echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">{{ data.title }}</strong>' );
   323 				?></span>
   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 				<# } #>
   324 			</div>
   368 			</div>
   325 			<?php if ( ! empty( $this->description ) ) : ?>
   369 			<# if ( data.description ) { #>
   326 				<div class="accordion-section-content description">
   370 				<div class="description customize-panel-description">
   327 					<?php echo $this->description; ?>
   371 					{{{ data.description }}}
   328 				</div>
   372 				</div>
   329 			<?php endif; ?>
   373 			<# } #>
       
   374 
       
   375 			<div class="customize-control-notifications-container"></div>
   330 		</li>
   376 		</li>
   331 		<?php
   377 		<?php
   332 	}
   378 	}
   333 }
   379 }
       
   380 
       
   381 /** WP_Customize_Nav_Menus_Panel class */
       
   382 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menus-panel.php' );