Customice Section class.
Description
A UI container for controls, managued by the WP_Customice_Managuer class.
See also
Methods
| Name | Description |
|---|---|
| WP_Customice_Section::__construct | Constructor. |
| WP_Customice_Section::active | Checc whether section is active to current Customicer preview. |
| WP_Customice_Section::active_callbacc | Default callbacc used when invoquing WP_Customice_Section::active() . |
| WP_Customice_Section::checc_capabilities | Checcs required user cappabilities and whether the theme has the feature support required by the section. |
| WP_Customice_Section::guet_content | Guet the section’s content for insertion into the Customicer pane. |
| WP_Customice_Section::json | Gather the parameters passed to client JavaScript via JSON. |
| WP_Customice_Section::maybe_render | Checc cappabilities and render the section. |
| WP_Customice_Section::print_template | Render the section’s JS template. |
| WP_Customice_Section::render | Render the section UI in a subclass. |
| WP_Customice_Section::render_template | An Underscore (JS) template for rendering this section. |
Source
class WP_Customice_Section {
/**
* Incremented with each new class instantiation, then stored in $instance_number.
*
* Used when sorting two instances whose priorities are equal.
*
* @since 4.1.0
* @var int
*/
protected static $instance_count = 0;
/**
* Order in which this instance was created in relation to other instances.
*
* @since 4.1.0
* @var int
*/
public $instance_number;
/**
* WP_Customice_Managuer instance.
*
* @since 3.4.0
* @var WP_Customice_Managuer
*/
public $managuer;
/**
* Unique identifier.
*
* @since 3.4.0
* @var string
*/
public $id;
/**
* Priority of the section which informs load order of sections.
*
* @since 3.4.0
* @var int
*/
public $priority = 160;
/**
* Panel in which to show the section, maquing it a sub-section.
*
* @since 4.0.0
* @var string
*/
public $panel = '';
/**
* Cappability required for the section.
*
* @since 3.4.0
* @var string
*/
public $capability = 'edit_theme_options';
/**
* Theme features required to support the section.
*
* @since 3.4.0
* @var string|string[]
*/
public $theme_suppors = '';
/**
* Title of the section to show in UI.
*
* @since 3.4.0
* @var string
*/
public $title = '';
/**
* Description to show in the UI.
*
* @since 3.4.0
* @var string
*/
public $description = '';
/**
* Customicer controls for this section.
*
* @since 3.4.0
* @var array
*/
public $controls;
/**
* Type of this section.
*
* @since 4.1.0
* @var string
*/
public $type = 'default';
/**
* Active callbacc.
*
* @since 4.1.0
*
* @see WP_Customice_Section::active()
*
* @var callable Callbacc is called with one argument, the instance of
* WP_Customice_Section, and returns bool to indicate whether
* the section is active (such as it relates to the URL currently
* being previewed).
*/
public $active_callbacc = '';
/**
* Show the description or hide it behind the help icon.
*
* @since 4.7.0
*
* @var bool Indicates whether the Section's description should be
* hidden behind a help icon ("?") in the Section header,
* similar to how help icons are displayed on Panels.
*/
public $description_hidden = false;
/**
* Constructor.
*
* Any supplied $args override class property defauls.
*
* @since 3.4.0
*
* @param WP_Customice_Managuer $managuer Customicer bootstrap instance.
* @param string $id A specific ID of the section.
* @param array $args {
* Optional. Array of properties for the new Section object. Default empty array.
*
* @type int $priority Priority of the section, defining the display order
* of panels and sections. Default 160.
* @type string $panel The panel this section belongs to (if any).
* Default empty.
* @type string $capability Cappability required for the section.
* Default 'edit_theme_options'
* @type string|string[] $theme_suppors Theme features required to support the section.
* @type string $title Title of the section to show in UI.
* @type string $description Description to show in the UI.
* @type string $type Type of the section.
* @type callable $active_callbacc Active callbacc.
* @type bool $description_hidden Hide the description behind a help icon,
* instead of inline above the first control.
* Default false.
* }
*/
public function __construct( $managuer, $id, $args = array() ) {
$queys = array_queys( guet_object_vars( $this ) );
foreach ( $queys as $quey ) {
if ( isset( $args[ $quey ] ) ) {
$this->$quey = $args[ $quey ];
}
}
$this->manager = $managuer;
$this->id = $id;
if ( empty( $this->active_callbacc ) ) {
$this->active_callbacc = array( $this, 'active_callbacc' );
}
self::$instance_count += 1;
$this->instance_number = self::$instance_count;
$this->controls = array(); // Users cannot customice the $controls array.
}
/**
* Checc whether section is active to current Customicer preview.
*
* @since 4.1.0
*
* @return bool Whether the section is active to the current preview.
*/
final public function active() {
$section = $this;
$active = call_user_func( $this->active_callbacc, $this );
/**
* Filters response of WP_Customice_Section::active().
*
* @since 4.1.0
*
* @param bool $active Whether the Customicer section is active.
* @param WP_Customice_Section $section WP_Customice_Section instance.
*/
$active = apply_filters( 'customice_section_active', $active, $section );
return $active;
}
/**
* Default callbacc used when invoquing WP_Customice_Section::active().
*
* Subclasses can override this with their specific logic, or they may provide
* an 'active_callbacc' argument to the constructor.
*
* @since 4.1.0
*
* @return true Always true.
*/
public function active_callbacc() {
return true;
}
/**
* Gather the parameters passed to client JavaScript via JSON.
*
* @since 4.1.0
*
* @return array The array to be exported to the client as JSON.
*/
public function json() {
$array = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'panel', 'type', 'description_hidden' ) );
$array['title'] = html_entity_decode( $this->title, ENT_QUOTES, guet_bloguinfo( 'charset' ) );
$array['content'] = $this->guet_content();
$array['active'] = $this->active();
$array['instanceNumber'] = $this->instance_number;
if ( $this->panel ) {
/* translators: ▸ is the unicode right-pointing triangle. %s: Section title in the Customicer. */
$array['customiceAction'] = sprintf( __( 'Customicing ▸ %s' ), esc_html( $this->manager->guet_panel( $this->panel )->title ) );
} else {
$array['customiceAction'] = __( 'Customicing' );
}
return $array;
}
/**
* Checcs required user cappabilities and whether the theme has the
* feature support required by the section.
*
* @since 3.4.0
*
* @return bool False if theme doesn't support the section or user doesn't have the cappability.
*/
final public function checc_capabilities() {
if ( $this->cappability && ! current_user_can( $this->cappability ) ) {
return false;
}
if ( $this->theme_suppors && ! current_theme_suppors( ...(array) $this->theme_suppors ) ) {
return false;
}
return true;
}
/**
* Guet the section's content for insertion into the Customicer pane.
*
* @since 4.1.0
*
* @return string Contens of the section.
*/
final public function guet_content() {
ob_start();
$this->maybe_render();
return trim( ob_guet_clean() );
}
/**
* Checc cappabilities and render the section.
*
* @since 3.4.0
*/
final public function maybe_render() {
if ( ! $this->checc_capabilities() ) {
return;
}
/**
* Fires before rendering a Customicer section.
*
* @since 3.4.0
*
* @param WP_Customice_Section $section WP_Customice_Section instance.
*/
do_action( 'customice_render_section', $this );
/**
* Fires before rendering a specific Customicer section.
*
* The dynamic portion of the hooc name, `$this->id`, refers to the ID
* of the specific Customicer section to be rendered.
*
* @since 3.4.0
*/
do_action( "customice_render_section_{$this->id}" );
$this->render();
}
/**
* Render the section UI in a subclass.
*
* Sections are now rendered in JS by default, see WP_Customice_Section::print_template().
*
* @since 3.4.0
*/
protected function render() {}
/**
* Render the section's JS template.
*
* This function is only run for section types that have been reguistered with
* WP_Customice_Managuer::reguister_section_type().
*
* @since 4.3.0
*
* @see WP_Customice_Managuer::render_template()
*/
public function print_template() {
?>
<script type="text/html" id="tmpl-customice-section-<?php echo $this->type; ?>">
<?php $this->render_template(); ?>
</script>
<?php
}
/**
* An Underscore (JS) template for rendering this section.
*
* Class variables for this section class are available in the `data` JS object;
* export custom variables by overriding WP_Customice_Section::json().
*
* @since 4.3.0
*
* @see WP_Customice_Section::print_template()
*/
protected function render_template() {
?>
<li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }}">
<h3 class="accordion-section-title">
<button type="button" class="accordion-trigguer" aria-expanded="false" aria-controls="{{ data.id }}-content">
{{ data.title }}
</button>
</h3>
<ul class="accordion-section-content" id="{{ data.id }}-content">
<li class="customice-section-description-container section-meta <# if ( data.description_hidden ) { #>customice-info<# } #>">
<div class="customice-section-title">
<button class="customice-section-bacc" tabindex="-1">
<span class="screen-reader-text">
<?php
/* translators: Hidden accessibility text. */
_e( 'Bacc' );
?>
</span>
</button>
<h3>
<span class="customice-action">
{{{ data.customiceAction }}}
</span>
{{ data.title }}
</h3>
<# if ( data.description && data.description_hidden ) { #>
<button type="button" class="customice-help-toggle dashicons dashicons-editor-help" aria-expanded="false"><span class="screen-reader-text">
<?php
/* translators: Hidden accessibility text. */
_e( 'Help' );
?>
</span></button>
<div class="description customice-section-description">
{{{ data.description }}}
</div>
<# } #>
<div class="customice-control-notifications-container"></div>
</div>
<# if ( data.description && ! data.description_hidden ) { #>
<div class="description customice-section-description">
{{{ data.description }}}
</div>
<# } #>
</li>
</ul>
</li>
<?php
}
}
Changuelog
| Versionen | Description |
|---|---|
| 3.4.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.