wp/wp-includes/class-wp-customize-section.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 3.4.0
    44 	 * @since 3.4.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 3.4.0
    52 	 * @since 3.4.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 section which informs load order of sections.
    58 	 * Priority of the section which informs load order of sections.
    61 	 *
    59 	 *
    62 	 * @since 3.4.0
    60 	 * @since 3.4.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 	 * Panel in which to show the section, making it a sub-section.
    66 	 * Panel in which to show the section, making it a sub-section.
    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 $panel = '';
    71 	public $panel = '';
    76 
    72 
    77 	/**
    73 	/**
    78 	 * Capability required for the section.
    74 	 * Capability required for the section.
    79 	 *
    75 	 *
    80 	 * @since 3.4.0
    76 	 * @since 3.4.0
    81 	 * @access public
       
    82 	 * @var string
    77 	 * @var string
    83 	 */
    78 	 */
    84 	public $capability = 'edit_theme_options';
    79 	public $capability = 'edit_theme_options';
    85 
    80 
    86 	/**
    81 	/**
    87 	 * Theme feature support for the section.
    82 	 * Theme feature support for the section.
    88 	 *
    83 	 *
    89 	 * @since 3.4.0
    84 	 * @since 3.4.0
    90 	 * @access public
       
    91 	 * @var string|array
    85 	 * @var string|array
    92 	 */
    86 	 */
    93 	public $theme_supports = '';
    87 	public $theme_supports = '';
    94 
    88 
    95 	/**
    89 	/**
    96 	 * Title of the section to show in UI.
    90 	 * Title of the section to show in UI.
    97 	 *
    91 	 *
    98 	 * @since 3.4.0
    92 	 * @since 3.4.0
    99 	 * @access public
       
   100 	 * @var string
    93 	 * @var string
   101 	 */
    94 	 */
   102 	public $title = '';
    95 	public $title = '';
   103 
    96 
   104 	/**
    97 	/**
   105 	 * Description to show in the UI.
    98 	 * Description to show in the UI.
   106 	 *
    99 	 *
   107 	 * @since 3.4.0
   100 	 * @since 3.4.0
   108 	 * @access public
       
   109 	 * @var string
   101 	 * @var string
   110 	 */
   102 	 */
   111 	public $description = '';
   103 	public $description = '';
   112 
   104 
   113 	/**
   105 	/**
   114 	 * Customizer controls for this section.
   106 	 * Customizer controls for this section.
   115 	 *
   107 	 *
   116 	 * @since 3.4.0
   108 	 * @since 3.4.0
   117 	 * @access public
       
   118 	 * @var array
   109 	 * @var array
   119 	 */
   110 	 */
   120 	public $controls;
   111 	public $controls;
   121 
   112 
   122 	/**
   113 	/**
   123 	 * Type of this section.
   114 	 * Type of this section.
   124 	 *
   115 	 *
   125 	 * @since 4.1.0
   116 	 * @since 4.1.0
   126 	 * @access public
       
   127 	 * @var string
   117 	 * @var string
   128 	 */
   118 	 */
   129 	public $type = 'default';
   119 	public $type = 'default';
   130 
   120 
   131 	/**
   121 	/**
   132 	 * Active callback.
   122 	 * Active callback.
   133 	 *
   123 	 *
   134 	 * @since 4.1.0
   124 	 * @since 4.1.0
   135 	 * @access public
       
   136 	 *
   125 	 *
   137 	 * @see WP_Customize_Section::active()
   126 	 * @see WP_Customize_Section::active()
   138 	 *
   127 	 *
   139 	 * @var callable Callback is called with one argument, the instance of
   128 	 * @var callable Callback is called with one argument, the instance of
   140 	 *               {@see WP_Customize_Section}, and returns bool to indicate
   129 	 *               WP_Customize_Section, and returns bool to indicate whether
   141 	 *               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
   142 	 *               currently being previewed).
   131 	 *               being previewed).
   143 	 */
   132 	 */
   144 	public $active_callback = '';
   133 	public $active_callback = '';
       
   134 
       
   135 	/**
       
   136 	 * Show the description or hide it behind the help icon.
       
   137 	 *
       
   138 	 * @since 4.7.0
       
   139 	 *
       
   140 	 * @var bool Indicates whether the Section's description should be
       
   141 	 *           hidden behind a help icon ("?") in the Section header,
       
   142 	 *           similar to how help icons are displayed on Panels.
       
   143 	 */
       
   144 	public $description_hidden = false;
   145 
   145 
   146 	/**
   146 	/**
   147 	 * Constructor.
   147 	 * Constructor.
   148 	 *
   148 	 *
   149 	 * Any supplied $args override class property defaults.
   149 	 * Any supplied $args override class property defaults.
   175 
   175 
   176 	/**
   176 	/**
   177 	 * Check whether section is active to current Customizer preview.
   177 	 * Check whether section is active to current Customizer preview.
   178 	 *
   178 	 *
   179 	 * @since 4.1.0
   179 	 * @since 4.1.0
   180 	 * @access public
       
   181 	 *
   180 	 *
   182 	 * @return bool Whether the section is active to the current preview.
   181 	 * @return bool Whether the section is active to the current preview.
   183 	 */
   182 	 */
   184 	final public function active() {
   183 	final public function active() {
   185 		$section = $this;
   184 		$section = $this;
   186 		$active = call_user_func( $this->active_callback, $this );
   185 		$active = call_user_func( $this->active_callback, $this );
   187 
   186 
   188 		/**
   187 		/**
   189 		 * Filter response of {@see WP_Customize_Section::active()}.
   188 		 * Filters response of WP_Customize_Section::active().
   190 		 *
   189 		 *
   191 		 * @since 4.1.0
   190 		 * @since 4.1.0
   192 		 *
   191 		 *
   193 		 * @param bool                 $active  Whether the Customizer section is active.
   192 		 * @param bool                 $active  Whether the Customizer section is active.
   194 		 * @param WP_Customize_Section $section {@see WP_Customize_Section} instance.
   193 		 * @param WP_Customize_Section $section WP_Customize_Section instance.
   195 		 */
   194 		 */
   196 		$active = apply_filters( 'customize_section_active', $active, $section );
   195 		$active = apply_filters( 'customize_section_active', $active, $section );
   197 
   196 
   198 		return $active;
   197 		return $active;
   199 	}
   198 	}
   200 
   199 
   201 	/**
   200 	/**
   202 	 * Default callback used when invoking {@see WP_Customize_Section::active()}.
   201 	 * Default callback used when invoking WP_Customize_Section::active().
   203 	 *
   202 	 *
   204 	 * Subclasses can override this with their specific logic, or they may provide
   203 	 * Subclasses can override this with their specific logic, or they may provide
   205 	 * an 'active_callback' argument to the constructor.
   204 	 * an 'active_callback' argument to the constructor.
   206 	 *
   205 	 *
   207 	 * @since 4.1.0
   206 	 * @since 4.1.0
   208 	 * @access public
   207 	 *
   209 	 *
   208 	 * @return true Always true.
   210 	 * @return bool Always true.
       
   211 	 */
   209 	 */
   212 	public function active_callback() {
   210 	public function active_callback() {
   213 		return true;
   211 		return true;
   214 	}
   212 	}
   215 
   213 
   219 	 * @since 4.1.0
   217 	 * @since 4.1.0
   220 	 *
   218 	 *
   221 	 * @return array The array to be exported to the client as JSON.
   219 	 * @return array The array to be exported to the client as JSON.
   222 	 */
   220 	 */
   223 	public function json() {
   221 	public function json() {
   224 		$array = wp_array_slice_assoc( (array) $this, array( 'title', 'description', 'priority', 'panel', 'type' ) );
   222 		$array = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'panel', 'type', 'description_hidden' ) );
       
   223 		$array['title'] = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) );
   225 		$array['content'] = $this->get_content();
   224 		$array['content'] = $this->get_content();
   226 		$array['active'] = $this->active();
   225 		$array['active'] = $this->active();
   227 		$array['instanceNumber'] = $this->instance_number;
   226 		$array['instanceNumber'] = $this->instance_number;
       
   227 
       
   228 		if ( $this->panel ) {
       
   229 			/* translators: ▸ is the unicode right-pointing triangle, and %s is the section title in the Customizer */
       
   230 			$array['customizeAction'] = sprintf( __( 'Customizing ▸ %s' ), esc_html( $this->manager->get_panel( $this->panel )->title ) );
       
   231 		} else {
       
   232 			$array['customizeAction'] = __( 'Customizing' );
       
   233 		}
       
   234 
   228 		return $array;
   235 		return $array;
   229 	}
   236 	}
   230 
   237 
   231 	/**
   238 	/**
   232 	 * Checks required user capabilities and whether the theme has the
   239 	 * Checks required user capabilities and whether the theme has the
   247 
   254 
   248 		return true;
   255 		return true;
   249 	}
   256 	}
   250 
   257 
   251 	/**
   258 	/**
   252 	 * Get the section's content template for insertion into the Customizer pane.
   259 	 * Get the section's content for insertion into the Customizer pane.
   253 	 *
   260 	 *
   254 	 * @since 4.1.0
   261 	 * @since 4.1.0
   255 	 *
   262 	 *
   256 	 * @return string Contents of the section.
   263 	 * @return string Contents of the section.
   257 	 */
   264 	 */
   258 	final public function get_content() {
   265 	final public function get_content() {
   259 		ob_start();
   266 		ob_start();
   260 		$this->maybe_render();
   267 		$this->maybe_render();
   261 		$template = trim( ob_get_contents() );
   268 		return trim( ob_get_clean() );
   262 		ob_end_clean();
       
   263 		return $template;
       
   264 	}
   269 	}
   265 
   270 
   266 	/**
   271 	/**
   267 	 * Check capabilities and render the section.
   272 	 * Check capabilities and render the section.
   268 	 *
   273 	 *
   293 
   298 
   294 		$this->render();
   299 		$this->render();
   295 	}
   300 	}
   296 
   301 
   297 	/**
   302 	/**
   298 	 * Render the section, and the controls that have been added to it.
   303 	 * Render the section UI in a subclass.
   299 	 *
   304 	 *
   300 	 * @since 3.4.0
   305 	 * Sections are now rendered in JS by default, see WP_Customize_Section::print_template().
   301 	 */
   306 	 *
   302 	protected function render() {
   307 	 * @since 3.4.0
   303 		$classes = 'accordion-section control-section control-section-' . $this->type;
   308 	 */
       
   309 	protected function render() {}
       
   310 
       
   311 	/**
       
   312 	 * Render the section's JS template.
       
   313 	 *
       
   314 	 * This function is only run for section types that have been registered with
       
   315 	 * WP_Customize_Manager::register_section_type().
       
   316 	 *
       
   317 	 * @since 4.3.0
       
   318 	 *
       
   319 	 * @see WP_Customize_Manager::render_template()
       
   320 	 */
       
   321 	public function print_template() {
   304 		?>
   322 		?>
   305 		<li id="accordion-section-<?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>">
   323 		<script type="text/html" id="tmpl-customize-section-<?php echo $this->type; ?>">
       
   324 			<?php $this->render_template(); ?>
       
   325 		</script>
       
   326 		<?php
       
   327 	}
       
   328 
       
   329 	/**
       
   330 	 * An Underscore (JS) template for rendering this section.
       
   331 	 *
       
   332 	 * Class variables for this section class are available in the `data` JS object;
       
   333 	 * export custom variables by overriding WP_Customize_Section::json().
       
   334 	 *
       
   335 	 * @since 4.3.0
       
   336 	 *
       
   337 	 * @see WP_Customize_Section::print_template()
       
   338 	 */
       
   339 	protected function render_template() {
       
   340 		?>
       
   341 		<li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }}">
   306 			<h3 class="accordion-section-title" tabindex="0">
   342 			<h3 class="accordion-section-title" tabindex="0">
   307 				<?php echo esc_html( $this->title ); ?>
   343 				{{ data.title }}
   308 				<span class="screen-reader-text"><?php _e( 'Press return or enter to expand' ); ?></span>
   344 				<span class="screen-reader-text"><?php _e( 'Press return or enter to open this section' ); ?></span>
   309 			</h3>
   345 			</h3>
   310 			<ul class="accordion-section-content">
   346 			<ul class="accordion-section-content">
   311 				<?php if ( ! empty( $this->description ) ) : ?>
   347 				<li class="customize-section-description-container section-meta <# if ( data.description_hidden ) { #>customize-info<# } #>">
   312 					<li class="customize-section-description-container">
   348 					<div class="customize-section-title">
   313 						<p class="description customize-section-description"><?php echo $this->description; ?></p>
   349 						<button class="customize-section-back" tabindex="-1">
   314 					</li>
   350 							<span class="screen-reader-text"><?php _e( 'Back' ); ?></span>
   315 				<?php endif; ?>
   351 						</button>
       
   352 						<h3>
       
   353 							<span class="customize-action">
       
   354 								{{{ data.customizeAction }}}
       
   355 							</span>
       
   356 							{{ data.title }}
       
   357 						</h3>
       
   358 						<# if ( data.description && data.description_hidden ) { #>
       
   359 							<button type="button" class="customize-help-toggle dashicons dashicons-editor-help" aria-expanded="false"><span class="screen-reader-text"><?php _e( 'Help' ); ?></span></button>
       
   360 							<div class="description customize-section-description">
       
   361 								{{{ data.description }}}
       
   362 							</div>
       
   363 						<# } #>
       
   364 
       
   365 						<div class="customize-control-notifications-container"></div>
       
   366 					</div>
       
   367 
       
   368 					<# if ( data.description && ! data.description_hidden ) { #>
       
   369 						<div class="description customize-section-description">
       
   370 							{{{ data.description }}}
       
   371 						</div>
       
   372 					<# } #>
       
   373 				</li>
   316 			</ul>
   374 			</ul>
   317 		</li>
   375 		</li>
   318 		<?php
   376 		<?php
   319 	}
   377 	}
   320 }
   378 }
   321 
   379 
       
   380 /** WP_Customize_Themes_Section class */
       
   381 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-themes-section.php' );
       
   382 
       
   383 /** WP_Customize_Sidebar_Section class */
       
   384 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-sidebar-section.php' );
       
   385 
       
   386 /** WP_Customize_Nav_Menu_Section class */
       
   387 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-section.php' );
       
   388 
   322 /**
   389 /**
   323  * Customize Themes Section class.
   390  * WP_Customize_New_Menu_Section class
   324  *
   391  *
   325  * A UI container for theme controls, which behaves like a backwards Panel.
   392  * As this file is deprecated, it will trigger a deprecation notice if instantiated. In a subsequent
   326  *
   393  * release, the require_once() here will be removed and _deprecated_file() will be called if file is
   327  * @since 4.2.0
   394  * required at all.
   328  *
   395  *
   329  * @see WP_Customize_Section
   396  * @deprecated 4.9.0 This file is no longer used due to new menu creation UX.
   330  */
   397  */
   331 class WP_Customize_Themes_Section extends WP_Customize_Section {
   398 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-new-menu-section.php' );
   332 
       
   333 	/**
       
   334 	 * Customize section type.
       
   335 	 *
       
   336 	 * @since 4.2.0
       
   337 	 * @access public
       
   338 	 * @var string
       
   339 	 */
       
   340 	public $type = 'themes';
       
   341 
       
   342 	/**
       
   343 	 * Render the themes section, which behaves like a panel.
       
   344 	 *
       
   345 	 * @since 4.2.0
       
   346 	 * @access protected
       
   347 	 */
       
   348 	protected function render() {
       
   349 		$classes = 'accordion-section control-section control-section-' . $this->type;
       
   350 		?>
       
   351 		<li id="accordion-section-<?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>">
       
   352 			<h3 class="accordion-section-title">
       
   353 				<?php
       
   354 				if ( $this->manager->is_theme_active() ) {
       
   355 					/* translators: %s: theme name */
       
   356 					printf( __( '<span>Active theme</span> %s' ), $this->title );
       
   357 				} else {
       
   358 					/* translators: %s: theme name */
       
   359 					printf( __( '<span>Previewing theme</span> %s' ), $this->title );
       
   360 				}
       
   361 				?>
       
   362 
       
   363 				<button type="button" class="button change-theme"><?php _ex( 'Change', 'theme' ); ?></button>
       
   364 			</h3>
       
   365 			<div class="customize-themes-panel control-panel-content themes-php">
       
   366 				<h2>
       
   367 					<?php _e( 'Themes' ); ?>
       
   368 					<span class="title-count theme-count"><?php echo count( $this->controls ) + 1 /* Active theme */; ?></span>
       
   369 				</h2>
       
   370 
       
   371 				<h3 class="accordion-section-title customize-section-title">
       
   372 					<?php
       
   373 					if ( $this->manager->is_theme_active() ) {
       
   374 						/* translators: %s: theme name */
       
   375 						printf( __( '<span>Active theme</span> %s' ), $this->title );
       
   376 					} else {
       
   377 						/* translators: %s: theme name */
       
   378 						printf( __( '<span>Previewing theme</span> %s' ), $this->title );
       
   379 					}
       
   380 					?>
       
   381 					<button type="button" class="button customize-theme"><?php _e( 'Customize' ); ?></button>
       
   382 				</h3>
       
   383 
       
   384 				<div class="theme-overlay" tabindex="0" role="dialog" aria-label="<?php esc_attr_e( 'Theme Details' ); ?>"></div>
       
   385 
       
   386 				<div id="customize-container"></div>
       
   387 				<?php if ( count( $this->controls ) > 4 ) : ?>
       
   388 					<p><label for="themes-filter">
       
   389 						<span class="screen-reader-text"><?php _e( 'Search installed themes...' ); ?></span>
       
   390 						<input type="text" id="themes-filter" placeholder="<?php esc_attr_e( 'Search installed themes...' ); ?>" />
       
   391 					</label></p>
       
   392 				<?php endif; ?>
       
   393 				<div class="theme-browser rendered">
       
   394 					<ul class="themes accordion-section-content">
       
   395 					</ul>
       
   396 				</div>
       
   397 			</div>
       
   398 		</li>
       
   399 <?php }
       
   400 }
       
   401 
       
   402 /**
       
   403  * Customizer section representing widget area (sidebar).
       
   404  *
       
   405  * @since 4.1.0
       
   406  *
       
   407  * @see WP_Customize_Section
       
   408  */
       
   409 class WP_Customize_Sidebar_Section extends WP_Customize_Section {
       
   410 
       
   411 	/**
       
   412 	 * Type of this section.
       
   413 	 *
       
   414 	 * @since 4.1.0
       
   415 	 * @access public
       
   416 	 * @var string
       
   417 	 */
       
   418 	public $type = 'sidebar';
       
   419 
       
   420 	/**
       
   421 	 * Unique identifier.
       
   422 	 *
       
   423 	 * @since 4.1.0
       
   424 	 * @access public
       
   425 	 * @var string
       
   426 	 */
       
   427 	public $sidebar_id;
       
   428 
       
   429 	/**
       
   430 	 * Gather the parameters passed to client JavaScript via JSON.
       
   431 	 *
       
   432 	 * @since 4.1.0
       
   433 	 *
       
   434 	 * @return array The array to be exported to the client as JSON.
       
   435 	 */
       
   436 	public function json() {
       
   437 		$json = parent::json();
       
   438 		$json['sidebarId'] = $this->sidebar_id;
       
   439 		return $json;
       
   440 	}
       
   441 
       
   442 	/**
       
   443 	 * Whether the current sidebar is rendered on the page.
       
   444 	 *
       
   445 	 * @since 4.1.0
       
   446 	 * @access public
       
   447 	 *
       
   448 	 * @return bool Whether sidebar is rendered.
       
   449 	 */
       
   450 	public function active_callback() {
       
   451 		return $this->manager->widgets->is_sidebar_rendered( $this->sidebar_id );
       
   452 	}
       
   453 }