Merge pull request #36 from darkwing/better-suggest
Adds method to recommend file
This commit is contained in:
Коммит
054c116e09
|
@ -68,6 +68,27 @@ class SW_Cache_Admin {
|
|||
echo '<div class="update-nag"><p>', __('You\'ve changed themes; please update your WP ServiceWorker Cache options.', 'swpswcache'), '</p></div>';
|
||||
}
|
||||
|
||||
function determine_file_recommendation($file_info, $all_files) {
|
||||
/*
|
||||
TODO:
|
||||
- Detect if a "positive" file has a "-min" version; if so, choose the -min and not the standard file
|
||||
|
||||
*/
|
||||
|
||||
|
||||
// Standard CSS file
|
||||
if($file_info['name'] === 'style.css') {
|
||||
return sprintf(__('%s is a standard WordPress theme file.'), 'style.css');
|
||||
}
|
||||
|
||||
// "Main level" CSS, JS, and image files are likely important for small theme_files
|
||||
if(in_array($file_info['category'], array('css', 'js', 'image')) && strpos($file_info['name'], '/') === false) {
|
||||
return __('Main level assets are likely important in small themes');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function options() {
|
||||
$submitted = $this->process_options();
|
||||
|
||||
|
@ -85,7 +106,8 @@ class SW_Cache_Admin {
|
|||
}
|
||||
|
||||
.wp-sw-cache-suggest-file,
|
||||
.wp-sw-cache-toggle-all {
|
||||
.wp-sw-cache-toggle-all,
|
||||
.wp-sw-cache-clear-all {
|
||||
float: right;
|
||||
margin-left: 10px !important;
|
||||
}
|
||||
|
@ -97,6 +119,26 @@ class SW_Cache_Admin {
|
|||
padding: 10px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.wp-sw-cache-suggest-file span {
|
||||
font-size: smaller;
|
||||
color: #fc0;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.wp-sw-cache-file-size,
|
||||
.wp-sw-cache-file-recommended {
|
||||
font-size: smaller;
|
||||
color: #999;
|
||||
font-size: italic;
|
||||
display: inline-block;
|
||||
margin-left: 20px;
|
||||
}
|
||||
|
||||
.wp-sw-cache-file-recommended {
|
||||
color: green;
|
||||
}
|
||||
|
||||
</style>
|
||||
<div class="wrap">
|
||||
|
||||
|
@ -139,22 +181,22 @@ class SW_Cache_Admin {
|
|||
<p>
|
||||
<?php _e('Select theme assets (typically JavaScript, CSS, fonts, and image files) that are used on a majority of pages.', 'wpswcache'); ?>
|
||||
<button type="button" class="button button-primary wp-sw-cache-toggle-all"><?php _e('Select All Files'); ?></button>
|
||||
<button type="button" class="button button-primary wp-sw-cache-suggest-file" data-suggested-text="<?php echo esc_attr__('Files Suggested: '); ?>"><?php _e('Suggest More Files'); ?></button>
|
||||
<button type="button" class="button button-primary wp-sw-cache-clear-all"><?php _e('Clear All Files'); ?></button>
|
||||
<button type="button" class="button button-primary wp-sw-cache-suggest-file" data-suggested-text="<?php echo esc_attr__('Files Suggested: '); ?>"><?php _e('Suggest Files'); ?> <span>(<?php _e('beta'); ?>)</span></button>
|
||||
</p>
|
||||
|
||||
<?php /* <pre><?php print_r($selected_files); ?></pre> */ ?>
|
||||
<div class="wp-sw-cache-file-list">
|
||||
|
||||
<?php
|
||||
$template_abs_path = get_template_directory();
|
||||
$theme_files = wp_get_theme()->get_files(null, 10); // 10 is arbitrary
|
||||
|
||||
$categories = array(
|
||||
array('title' => __('CSS Files', 'wpswcache'), 'extensions' => array('css'), 'files' => array()),
|
||||
array('title' => __('JavaScript Files', 'wpswcache'), 'extensions' => array('js'), 'files' => array()),
|
||||
array('title' => __('Font Files', 'wpswcache'), 'extensions' => array('woff', 'woff2', 'ttf'), 'files' => array()),
|
||||
array('title' => __('Image Files', 'wpswcache'), 'extensions' => array('svg', 'jpg', 'jpeg', 'gif', 'png', 'webp'), 'files' => array()),
|
||||
array('title' => __('Other Files', 'wpswcache'), 'extensions' => array('*'), 'files' => array()) // Needs to be last
|
||||
array('key' => 'css', 'title' => __('CSS Files', 'wpswcache'), 'extensions' => array('css'), 'files' => array()),
|
||||
array('key' => 'js', 'title' => __('JavaScript Files', 'wpswcache'), 'extensions' => array('js'), 'files' => array()),
|
||||
array('key' => 'font', 'title' => __('Font Files', 'wpswcache'), 'extensions' => array('woff', 'woff2', 'ttf', 'eot'), 'files' => array()),
|
||||
array('key' => 'image', 'title' => __('Image Files', 'wpswcache'), 'extensions' => array('svg', 'jpg', 'jpeg', 'gif', 'png', 'webp'), 'files' => array()),
|
||||
array('key' => 'other', 'title' => __('Other Files', 'wpswcache'), 'extensions' => array('*'), 'files' => array()) // Needs to be last
|
||||
);
|
||||
|
||||
// Sort the files and place them in their baskets
|
||||
|
@ -163,9 +205,23 @@ class SW_Cache_Admin {
|
|||
$path_info = pathinfo($file_relative);
|
||||
$file_category_found = false;
|
||||
|
||||
// Build array to represent file
|
||||
$file_info = array(
|
||||
'name' => $file_relative,
|
||||
'size' => filesize($abs_path),
|
||||
'absolute' => $abs_path,
|
||||
'path' => $path_info
|
||||
);
|
||||
|
||||
// Categorize the file, use "other" as the fallback
|
||||
foreach($categories as $index=>$category) {
|
||||
if(in_array(strtolower($path_info['extension']), $category['extensions']) || ($file_category_found === false && $category['extensions'][0] === '*')) {
|
||||
$categories[$index]['files'][] = $file_relative;
|
||||
if(in_array(strtolower($path_info['extension']), $category['extensions']) || ($file_category_found === false && $category['key'] === 'other')) {
|
||||
// Store the category to help determine recommendation
|
||||
$file_info['category'] = $category['key'];
|
||||
// Determine if we should recommend this file be cached
|
||||
$file_info['recommended'] = self::determine_file_recommendation($file_info, $theme_files);
|
||||
// Add to category array
|
||||
$categories[$index]['files'][] = $file_info;
|
||||
$file_category_found = true;
|
||||
}
|
||||
}
|
||||
|
@ -174,15 +230,26 @@ class SW_Cache_Admin {
|
|||
$file_id = 0;
|
||||
foreach($categories as $category) { ?>
|
||||
<h3><?php echo $category['title']; ?> (<?php echo implode(', ', $category['extensions']); ?>)</h3>
|
||||
|
||||
<?php if($category['key'] === 'other') { ?>
|
||||
<p><em><?php _e('The following assets, especially <code>.php</code> files, have no value in being cached directly by service workers.'); ?></em></p>
|
||||
<?php } ?>
|
||||
|
||||
<?php if(count($category['files'])) { ?>
|
||||
<table id="files-list">
|
||||
<table class="files-list">
|
||||
<?php foreach($category['files'] as $file) { $file_id++; ?>
|
||||
<tr>
|
||||
<td style="width: 30px;">
|
||||
<input type="checkbox" name="wp_sw_cache_files[]" id="wp_sw_cache_files['file_<?php echo $file_id; ?>']" value="<?php echo urlencode($file); ?>" <?php if(in_array($file, $selected_files)) { echo 'checked'; } ?> />
|
||||
<input type="checkbox" class="<?php if($file['recommended']) { echo 'recommended'; } ?>" name="wp_sw_cache_files[]" id="wp_sw_cache_files['file_<?php echo $file_id; ?>']" value="<?php echo urlencode($file['name']); ?>" <?php if(in_array($file['name'], $selected_files)) { echo 'checked'; } ?> />
|
||||
</td>
|
||||
<td>
|
||||
<label for="wp_sw_cache_files['file_<?php echo $file_id; ?>']"><?php echo $file; ?></label>
|
||||
<label for="wp_sw_cache_files['file_<?php echo $file_id; ?>']">
|
||||
<?php echo $file['name']; ?>
|
||||
<span class="wp-sw-cache-file-size"><?php echo ($file['size'] > 0 ? round($file['size']/1024, 2) : 0).'kb'; ?></span>
|
||||
<?php if($file['recommended']) { ?>
|
||||
<span class="wp-sw-cache-file-recommended">✔ <?php echo $file['recommended']; ?></span>
|
||||
<?php } ?>
|
||||
</label>
|
||||
</td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
|
@ -208,23 +275,24 @@ class SW_Cache_Admin {
|
|||
var suggestedCounter = 0;
|
||||
|
||||
// Suggest main level CSS and JS files
|
||||
jQuery([
|
||||
'#files-list input[type="checkbox"][value$=".css"]:not([checked]):not([value*="/"])',
|
||||
'#files-list input[type="checkbox"][value$=".js"]:not([checked]):not([value*="/"])'
|
||||
].join(',')).each(function() {
|
||||
this.checked = true;
|
||||
jQuery(this).closest('tr').addClass('wp-sw-cache-suggested');
|
||||
suggestedCounter++;
|
||||
});
|
||||
var $recommended = jQuery('.files-list input[type="checkbox"].recommended:not(:checked)')
|
||||
.prop('checked', 'checked')
|
||||
.closest('tr').addClass('wp-sw-cache-suggested');
|
||||
|
||||
$this.text($this.data('suggested-text') + ' ' + suggestedCounter);
|
||||
this.disabled = true;
|
||||
// Update sugget button now that the process is done
|
||||
$this
|
||||
.text($this.data('suggested-text') + ' ' + $recommended.length)
|
||||
.prop('disabled', 'disabled');
|
||||
});
|
||||
|
||||
jQuery('.wp-sw-cache-toggle-all').on('click', function() {
|
||||
jQuery('#files-list input[type="checkbox"]').prop('checked', 'checked');
|
||||
});
|
||||
|
||||
jQuery('.wp-sw-cache-clear-all').on('click', function() {
|
||||
jQuery('#files-list input[type="checkbox"]').prop('checked', '');
|
||||
});
|
||||
|
||||
jQuery('.wp-sw-cache-clear-caches-button').on('click', function() {
|
||||
var clearedCounter = 0;
|
||||
var $button = jQuery(this);
|
||||
|
|
Загрузка…
Ссылка в новой задаче