WordPress/wp-includes/customize/class-wp-customize-themes-s...

209 lines
6.9 KiB
PHP
Raw Normal View History

<?php
/**
* Customize API: WP_Customize_Themes_Section class
*
* @package WordPress
* @subpackage Customize
* @since 4.4.0
*/
/**
* Customize Themes Section class.
*
* A UI container for theme controls, which are displayed within sections.
*
* @since 4.2.0
*
* @see WP_Customize_Section
*/
class WP_Customize_Themes_Section extends WP_Customize_Section {
/**
* Section type.
*
* @since 4.2.0
* @var string
*/
public $type = 'themes';
/**
* Theme section action.
*
* Defines the type of themes to load (installed, wporg, etc.).
*
* @since 4.9.0
* @var string
*/
public $action = '';
Customize: Improve behavior and extensibility of theme loading and searching. * Introduce `WP_Customize_Themes_Section::$filter_type`, which has built-in functionality for `local` and `remote` filtering. When this set to `local`, all themes are assumed to be loaded from Ajax when the section is first loaded, and subsequent searching/filtering is applied to the loaded collection of themes within the section. This is how the core "Installed" section behaves - third-party sources with limited numbers of themes may consider leveraging this implementation. When this is set to `remote`, searching and filtering always triggers a new remote query via Ajax. The core "WordPress.org" section uses this approach, as it has over 5000 themes to search. * Refactor `filterSearch()` to accept a raw term string as input. This enables a feature filter to be used on a section where `filter_type` is `local`. * Refactor `filter()` on a theme control to check for an array of terms. Also sort the results by the number of matches. Rather than searching for an exact match, this will now search for each word in a search distinctly, allowing things like tags to rank in search results more accurately. * Split `loadControls()` into two functions for themes section JS: `loadThemes()` to initiate and manage an Ajax request and `loadControls()` to create theme controls based on the results of the Ajax call. If third-party sections need to change the way controls are loaded, such as by using a custom control subclass of `WP_Customize_Theme_Control`, this allows them to use the core logic for managing the Ajax call and only override the actual control-creation process. * Introduce `customize_load_themes` filter to facilitate loading themes from third-party sources (or modifying the results of the core sections). * Bring significant improvements to the installed themes search filter. Props celloexpressions. Amends [41648]. See #37661. Fixes #42049. Built from https://develop.svn.wordpress.org/trunk@41807 git-svn-id: http://core.svn.wordpress.org/trunk@41641 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-10 03:09:52 -04:00
/**
* Theme section filter type.
*
* Determines whether filters are applied to loaded (local) themes or by initiating a new remote query (remote).
* When filtering is local, the initial themes query is not paginated by default.
*
* @since 4.9.0
* @var string
*/
public $filter_type = 'local';
/**
* Get section parameters for JS.
*
* @since 4.9.0
* @return array Exported parameters.
*/
public function json() {
$exported = parent::json();
$exported['action'] = $this->action;
Customize: Improve behavior and extensibility of theme loading and searching. * Introduce `WP_Customize_Themes_Section::$filter_type`, which has built-in functionality for `local` and `remote` filtering. When this set to `local`, all themes are assumed to be loaded from Ajax when the section is first loaded, and subsequent searching/filtering is applied to the loaded collection of themes within the section. This is how the core "Installed" section behaves - third-party sources with limited numbers of themes may consider leveraging this implementation. When this is set to `remote`, searching and filtering always triggers a new remote query via Ajax. The core "WordPress.org" section uses this approach, as it has over 5000 themes to search. * Refactor `filterSearch()` to accept a raw term string as input. This enables a feature filter to be used on a section where `filter_type` is `local`. * Refactor `filter()` on a theme control to check for an array of terms. Also sort the results by the number of matches. Rather than searching for an exact match, this will now search for each word in a search distinctly, allowing things like tags to rank in search results more accurately. * Split `loadControls()` into two functions for themes section JS: `loadThemes()` to initiate and manage an Ajax request and `loadControls()` to create theme controls based on the results of the Ajax call. If third-party sections need to change the way controls are loaded, such as by using a custom control subclass of `WP_Customize_Theme_Control`, this allows them to use the core logic for managing the Ajax call and only override the actual control-creation process. * Introduce `customize_load_themes` filter to facilitate loading themes from third-party sources (or modifying the results of the core sections). * Bring significant improvements to the installed themes search filter. Props celloexpressions. Amends [41648]. See #37661. Fixes #42049. Built from https://develop.svn.wordpress.org/trunk@41807 git-svn-id: http://core.svn.wordpress.org/trunk@41641 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-10 03:09:52 -04:00
$exported['filter_type'] = $this->filter_type;
return $exported;
}
/**
* Render a themes section as a JS template.
*
* The template is only rendered by PHP once, so all actions are prepared at once on the server side.
*
* @since 4.9.0
*/
protected function render_template() {
?>
<li id="accordion-section-{{ data.id }}" class="theme-section">
<button type="button" class="customize-themes-section-title themes-section-{{ data.id }}">{{ data.title }}</button>
<?php if ( current_user_can( 'install_themes' ) || is_multisite() ) : // @todo Upload support. ?>
<?php endif; ?>
<div class="customize-themes-section themes-section-{{ data.id }} control-section-content themes-php">
<div class="theme-overlay" tabindex="0" role="dialog" aria-label="<?php esc_attr_e( 'Theme Details' ); ?>"></div>
<div class="theme-browser rendered">
<div class="customize-preview-header themes-filter-bar">
<?php $this->filter_bar_content_template(); ?>
</div>
<?php $this->filter_drawer_content_template(); ?>
<div class="error unexpected-error" style="display: none; ">
<p>
<?php
printf(
/* translators: %s: Support forums URL. */
__( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
__( 'https://wordpress.org/support/forums/' )
);
?>
</p>
</div>
<ul class="themes">
</ul>
<p class="no-themes"><?php _e( 'No themes found. Try a different search.' ); ?></p>
<p class="no-themes-local">
<?php
printf(
/* translators: %s: "Search WordPress.org themes" button text. */
__( 'No themes found. Try a different search, or %s.' ),
sprintf( '<button type="button" class="button-link search-dotorg-themes">%s</button>', __( 'Search WordPress.org themes' ) )
);
?>
</p>
<p class="spinner"></p>
</div>
</div>
</li>
<?php
}
/**
* Render the filter bar portion of a themes section as a JS template.
*
* The template is only rendered by PHP once, so all actions are prepared at once on the server side.
* The filter bar container is rendered by @see `render_template()`.
*
* @since 4.9.0
*/
protected function filter_bar_content_template() {
?>
<button type="button" class="button button-primary customize-section-back customize-themes-mobile-back"><?php _e( 'Go to theme sources' ); ?></button>
<# if ( 'wporg' === data.action ) { #>
<div class="search-form">
<label for="wp-filter-search-input-{{ data.id }}" class="screen-reader-text">
<?php
/* translators: Hidden accessibility text. */
_e( 'Search themes&hellip;' );
?>
</label>
<input type="search" id="wp-filter-search-input-{{ data.id }}" placeholder="<?php esc_attr_e( 'Search themes&hellip;' ); ?>" aria-describedby="{{ data.id }}-live-search-desc" class="wp-filter-search">
<div class="search-icon" aria-hidden="true"></div>
<span id="{{ data.id }}-live-search-desc" class="screen-reader-text">
<?php
/* translators: Hidden accessibility text. */
_e( 'The search results will be updated as you type.' );
?>
</span>
</div>
<button type="button" class="button feature-filter-toggle">
<span class="filter-count-0"><?php _e( 'Filter themes' ); ?></span><span class="filter-count-filters">
<?php
/* translators: %s: Number of filters selected. */
printf( __( 'Filter themes (%s)' ), '<span class="theme-filter-count">0</span>' );
?>
</span>
</button>
<# } else { #>
<div class="themes-filter-container">
<label for="{{ data.id }}-themes-filter" class="screen-reader-text">
<?php
/* translators: Hidden accessibility text. */
_e( 'Search themes&hellip;' );
?>
</label>
<input type="search" id="{{ data.id }}-themes-filter" placeholder="<?php esc_attr_e( 'Search themes&hellip;' ); ?>" aria-describedby="{{ data.id }}-live-search-desc" class="wp-filter-search wp-filter-search-themes" />
<div class="search-icon" aria-hidden="true"></div>
<span id="{{ data.id }}-live-search-desc" class="screen-reader-text">
<?php
/* translators: Hidden accessibility text. */
_e( 'The search results will be updated as you type.' );
?>
</span>
</div>
<# } #>
<div class="filter-themes-count">
<span class="themes-displayed">
<?php
/* translators: %s: Number of themes displayed. */
printf( __( '%s themes' ), '<span class="theme-count">0</span>' );
?>
</span>
</div>
<?php
}
/**
* Render the filter drawer portion of a themes section as a JS template.
*
* The filter bar container is rendered by @see `render_template()`.
*
* @since 4.9.0
*/
protected function filter_drawer_content_template() {
// @todo Use the .org API instead of the local core feature list.
// The .org API is currently outdated and will be reconciled when the .org themes directory is next redesigned.
$feature_list = get_theme_feature_list( false );
?>
<# if ( 'wporg' === data.action ) { #>
<div class="filter-drawer filter-details">
<?php foreach ( $feature_list as $feature_name => $features ) : ?>
<fieldset class="filter-group">
<legend><?php echo esc_html( $feature_name ); ?></legend>
<div class="filter-group-feature">
<?php foreach ( $features as $feature => $feature_name ) : ?>
<input type="checkbox" id="filter-id-<?php echo esc_attr( $feature ); ?>" value="<?php echo esc_attr( $feature ); ?>" />
<label for="filter-id-<?php echo esc_attr( $feature ); ?>"><?php echo esc_html( $feature_name ); ?></label>
<?php endforeach; ?>
</div>
</fieldset>
<?php endforeach; ?>
</div>
<# } #>
<?php
}
}