Skip to content

Commit 4ef17c7

Browse files
added prefetch option
1 parent 9c34e5c commit 4ef17c7

6 files changed

Lines changed: 320 additions & 48 deletions

File tree

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

33
/**
4-
* Register settings for the Cache Everything plugin.
4+
* Register cache settings for the Cache Everything plugin.
55
*/
6-
function cache_everything_register_settings() {
6+
function cache_everything_register_cache_settings() {
77
// Register settings for cache times first
88
register_setting('cache_everything_settings', 'cache_everything_max_age');
99
register_setting('cache_everything_settings', 'cache_everything_stale_while_revalidate');
@@ -33,7 +33,7 @@ function cache_everything_register_settings() {
3333
'cache_everything_cache_times_section' // Use the new section ID
3434
);
3535
// Register the new caching options
36-
register_setting('cache_everything_settings', 'cache_everything_options');
36+
register_setting('cache_everything_settings', 'cache_everything_cache_options');
3737

3838
// Add the Caching Options section
3939
add_settings_section(
@@ -117,9 +117,9 @@ function cache_everything_debug_mode_callback() {
117117
}
118118

119119
function cache_everything_checkbox_callback($args) {
120-
$options = get_option('cache_everything_options');
120+
$options = get_option('cache_everything_cache_options');
121121
$checked = isset($options[$args['id']]) ? checked($options[$args['id']], 1, false) : '';
122-
echo "<input type='checkbox' id='{$args['id']}' name='cache_everything_options[{$args['id']}]' value='1' $checked>";
122+
echo "<input type='checkbox' id='{$args['id']}' name='cache_everything_cache_options[{$args['id']}]' value='1' $checked>";
123123
echo "<label for='{$args['id']}'>{$args['description']}</label>";
124124
}
125125

@@ -150,7 +150,7 @@ function cache_everything_stale_while_revalidate_callback() {
150150
}
151151

152152
// Adjusted function to display settings
153-
function cache_everything_settings_page() {
153+
function cache_everything_cache_settings_page() {
154154
// Continue with the settings form
155155
echo '<form action="options.php" method="post">';
156156
settings_fields('cache_everything_settings');

admin/menu.php

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
11
<?php
22

3+
function cache_everything_settings_wrapper_page() {
4+
// Determine the current tab. Default to 'settings' if not set.
5+
$current_tab = isset($_GET['tab']) ? $_GET['tab'] : 'cache_settings';
6+
7+
// Output the tabs
8+
cache_everything_admin_tabs($current_tab);
9+
10+
// Switch to determine which content to load based on the current tab
11+
switch ($current_tab) {
12+
case 'cache_settings':
13+
cache_everything_cache_settings_page();
14+
break;
15+
case 'css_classes':
16+
cache_everything_display_css_classes();
17+
break;
18+
case 'readme':
19+
cache_everything_display_readme();
20+
break;
21+
case 'prefetch_settings': // New case for the new tab
22+
cache_everything_display_prefetch_settings();
23+
break;
24+
default:
25+
cache_everything_cache_settings_page(); // Default to settings if the tab is unrecognized
26+
break;
27+
}
28+
}
29+
330
function cache_everything_activate() {
431
add_option('cache_everything_debug_mode', '0'); // Default value set to '0' (debug mode off)
5-
add_option('cache_everything_options', []); // Initialize caching options
32+
add_option('cache_everything_cache_options', []); // Initialize caching options
633
add_option('cache_everything_max_age', '28800'); // Default cache max age set to '28800' seconds
734
add_option('cache_everything_stale_while_revalidate', '86400'); // Default stale while revalidate set to '86400' seconds
35+
add_option('cache_everything_prefetch_enabled', 'no');
36+
837
}
938

1039
/**
@@ -20,7 +49,7 @@ function cache_everything_add_admin_menu() {
2049
'cache_everything_settings_wrapper_page' // Function to display the wrapper settings page
2150
);
2251

23-
add_action('admin_init', 'cache_everything_register_settings');
52+
add_action('admin_init', 'cache_everything_register_cache_settings');
2453
}
2554

2655
add_action('admin_menu', 'cache_everything_add_admin_menu');
@@ -30,7 +59,8 @@ function cache_everything_admin_tabs( $current = 'settings' ) {
3059
echo '<h1>Cache Everything v' . CACHE_EVERYTHING_VERSION . '</h1>';
3160

3261
$tabs = array(
33-
'settings' => 'Settings',
62+
'cache_settings' => 'Cache Settings',
63+
'prefetch_settings' => 'Prefetch Settings',
3464
'css_classes' => 'CSS Classes',
3565
'readme' => 'Readme'
3666
);
@@ -42,30 +72,6 @@ function cache_everything_admin_tabs( $current = 'settings' ) {
4272
echo '</h2>';
4373
}
4474

45-
function cache_everything_settings_wrapper_page() {
46-
// Determine the current tab. Default to 'settings' if not set.
47-
$current_tab = isset($_GET['tab']) ? $_GET['tab'] : 'settings';
48-
49-
// Output the tabs
50-
cache_everything_admin_tabs($current_tab);
51-
52-
// Switch to determine which content to load based on the current tab
53-
switch ($current_tab) {
54-
case 'settings':
55-
cache_everything_settings_page();
56-
break;
57-
case 'css_classes':
58-
cache_everything_display_css_classes();
59-
break;
60-
case 'readme':
61-
cache_everything_display_readme();
62-
break;
63-
default:
64-
cache_everything_settings_page(); // Default to settings if the tab is unrecognized
65-
break;
66-
}
67-
}
68-
6975
function cache_everything_enqueue_admin_styles() {
7076
wp_enqueue_style('wp-admin');
7177
}

admin/prefetch-settings.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
function cache_everything_display_prefetch_settings() {
4+
?>
5+
<div>
6+
<form method="post" action="options.php">
7+
<?php
8+
settings_fields('cache_everything_prefetch_options');
9+
do_settings_sections('cache_everything_prefetch');
10+
submit_button();
11+
?>
12+
</form>
13+
</div>
14+
<?php
15+
}
16+
17+
function cache_everything_register_prefetch_settings() {
18+
// Register a new setting for "Cache Everything" options
19+
register_setting('cache_everything_prefetch_options', 'cache_everything_prefetch_enabled');
20+
register_setting('cache_everything_prefetch_options', 'cache_everything_prefetch_patterns', 'cache_everything_sanitize_patterns');
21+
22+
// Add a new settings section within the "Prefetch Settings" page
23+
add_settings_section(
24+
'cache_everything_prefetch_settings_section',
25+
'Prefetch Settings',
26+
'cache_everything_prefetch_settings_section_callback',
27+
'cache_everything_prefetch'
28+
);
29+
30+
// Add a new settings field for the enable/disable toggle
31+
add_settings_field(
32+
'cache_everything_prefetch_enable',
33+
'Enable Prefetching',
34+
'cache_everything_prefetch_enable_callback',
35+
'cache_everything_prefetch',
36+
'cache_everything_prefetch_settings_section'
37+
);
38+
39+
add_settings_field(
40+
'cache_everything_prefetch_patterns',
41+
'Prefetch Exclusion Patterns',
42+
'cache_everything_prefetch_patterns_callback',
43+
'cache_everything_prefetch',
44+
'cache_everything_prefetch_settings_section'
45+
);
46+
}
47+
48+
function cache_everything_sanitize_patterns($input) {
49+
$existing_patterns = get_option('cache_everything_prefetch_patterns', []);
50+
if (!is_array($existing_patterns)) {
51+
$existing_patterns = [];
52+
}
53+
54+
// Handle deletion of patterns
55+
if (isset($input['delete']) && is_array($input['delete'])) {
56+
foreach ($input['delete'] as $index => $should_delete) {
57+
if ($should_delete) {
58+
unset($existing_patterns[$index]);
59+
}
60+
}
61+
$existing_patterns = array_values($existing_patterns); // Reindex array
62+
}
63+
64+
// Check if the new pattern is set and not empty
65+
if (isset($input['new']) && !empty($input['new']['pattern'])) {
66+
$new_pattern = [
67+
'pattern' => sanitize_text_field($input['new']['pattern']),
68+
'operator' => sanitize_text_field($input['new']['operator']),
69+
'comment' => sanitize_text_field($input['new']['comment'])
70+
];
71+
$existing_patterns[] = $new_pattern;
72+
}
73+
return $existing_patterns;
74+
}
75+
76+
function cache_everything_prefetch_settings_section_callback() {
77+
echo '<p>Enable or disable prefetching for your site.</p>';
78+
}
79+
80+
function cache_everything_prefetch_enable_callback() {
81+
$option = get_option('cache_everything_prefetch_enabled');
82+
$checked = ($option === 'yes') ? 'checked' : '';
83+
echo '<input type="checkbox" id="cache_everything_prefetch_enable" name="cache_everything_prefetch_enabled" value="yes" ' . $checked . '>';
84+
echo '<label for="cache_everything_prefetch_enable">Activate Prefetching</label>';
85+
}
86+
87+
function cache_everything_prefetch_patterns_callback() {
88+
$patterns = get_option('cache_everything_prefetch_patterns', []);
89+
if (!is_array($patterns)) {
90+
$patterns = [];
91+
}
92+
?>
93+
<table>
94+
<tr>
95+
<th>Pattern</th>
96+
<th>Operator</th>
97+
<th>Comment</th>
98+
<th>Delete</th>
99+
</tr>
100+
<?php foreach ($patterns as $index => $pattern): ?>
101+
<tr>
102+
<td><?php echo esc_html($pattern['pattern']); ?></td>
103+
<td><?php echo esc_html($pattern['operator']); ?></td>
104+
<td><?php echo esc_html($pattern['comment']); ?></td>
105+
<td><input type="checkbox" name="cache_everything_prefetch_patterns[delete][<?php echo $index; ?>]" value="1"></td>
106+
</tr>
107+
<?php endforeach; ?>
108+
</table>
109+
<h3>Add New Pattern</h3>
110+
<input type="text" name="cache_everything_prefetch_patterns[new][pattern]" placeholder="Pattern" title="Enter a regular expression pattern. Patterns are evaluated using JavaScript's ECMAScript-based regex engine.">
111+
<select name="cache_everything_prefetch_patterns[new][operator]">
112+
<option value="starts_with">Starts with</option>
113+
<option value="contains">Contains</option>
114+
<option value="regex">Regular Expression</option>
115+
</select>
116+
<input type="text" name="cache_everything_prefetch_patterns[new][comment]" placeholder="Comment">
117+
<div class="operator-descriptions">
118+
<p><strong>Starts with:</strong> Use this option to match URLs that begin with the specified pattern.</p>
119+
<p><strong>Contains:</strong> Use this option to match URLs that contain the specified pattern anywhere in the URL.</p>
120+
<p><strong>Regular Expression:</strong> Use this option for complex patterns. Patterns are evaluated using JavaScript's ECMAScript-based regex engine, allowing for advanced matching conditions.</p>
121+
</div>
122+
<?php
123+
}
124+
125+
// Hook into the admin_init action to register the settings
126+
add_action('admin_init', 'cache_everything_register_prefetch_settings');

cache-everything.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
require_once(plugin_dir_path(__FILE__) . 'handle-css-request.php');
1616
require_once(plugin_dir_path(__FILE__) . 'helpers.php');
1717
require_once(plugin_dir_path(__FILE__) . 'admin/menu.php');
18-
require_once(plugin_dir_path(__FILE__) . 'admin/configuration.php');
18+
require_once(plugin_dir_path(__FILE__) . 'admin/cache-settings.php');
19+
require_once(plugin_dir_path(__FILE__) . 'admin/prefetch-settings.php');
1920
require_once(plugin_dir_path(__FILE__) . 'admin/readme.php');
2021
require_once(plugin_dir_path(__FILE__) . 'admin/css-classes.php');
2122

@@ -77,27 +78,50 @@ function cache_everything_enqueue_scripts() {
7778
// Append a trailing slash if the site uses trailing slashes in the permalink structure
7879
$js_full_url = $site_url . '/' . CACHE_EVERYTHING_JS_URL . ($use_trailing_slashes ? '/' : '');
7980

80-
// Combine site URL with the CACHE_EVERYTHIN_CSS_URL to send a full URL without risking a double slash
81+
// Combine site URL with the CACHE_EVERYTHING_CSS_URL to send a full URL without risking a double slash
8182
// Append a trailing slash if the site uses trailing slashes in the permalink structure
8283
$css_full_url = $site_url . '/' . CACHE_EVERYTHING_CSS_URL . ($use_trailing_slashes ? '/' : '');
8384

84-
// Enqueue the dynamic CSS file
8585
wp_enqueue_style('cache-everything', $css_full_url, array(), null, 'all');
8686

87-
// Localize the script with the roles data, the full JS and CSS URLs, and the site prefix
8887
$site_prefix = get_site_prefix(); // Retrieve the site prefix using the helper function
8988

90-
// Retrieve the debug mode setting from WordPress options
9189
$debug_mode = get_option('cache_everything_debug_mode', '0'); // Default to '0' if not set
9290

91+
// Retrieve the prefetching options
92+
$prefetch_enabled = get_option('cache_everything_prefetch_enable', '0');
93+
$prefetch_patterns = get_option('cache_everything_prefetch_patterns', array());
94+
95+
$patterns_starts_with = array();
96+
$patterns_contains = array();
97+
$patterns_regex = array();
98+
99+
foreach ($prefetch_patterns as $pattern_config) {
100+
switch ($pattern_config['operator']) {
101+
case 'starts_with':
102+
$patterns_starts_with[] = $pattern_config['pattern'];
103+
break;
104+
case 'contains':
105+
$patterns_contains[] = $pattern_config['pattern'];
106+
break;
107+
case 'regex':
108+
$patterns_regex[] = $pattern_config['pattern'];
109+
break;
110+
}
111+
}
112+
93113
wp_localize_script('cache-everything', 'wce_Data', array(
94114
'roles' => $all_roles,
95115
'jsUrl' => $js_full_url,
96116
'cssUrl' => $css_full_url,
97-
'sitePrefix' => $site_prefix, // Add the site prefix to the localized script data
98-
'debugMode' => $debug_mode // Add the debug mode status to the localized script data
117+
'sitePrefix' => $site_prefix,
118+
'debugMode' => $debug_mode,
119+
'prefetchEnabled' => $prefetch_enabled,
120+
'prefetchStartsWith' => $patterns_starts_with,
121+
'prefetchContains' => $patterns_contains,
122+
'prefetchRegex' => $patterns_regex,
99123
));
100-
124+
101125
}
102126
add_action('wp_enqueue_scripts', 'cache_everything_enqueue_scripts');
103127

@@ -108,7 +132,7 @@ function cache_everything_modify_headers() {
108132
}
109133

110134
// Retrieve the options with a default to an empty array if not set
111-
$options = get_option('cache_everything_options', []);
135+
$options = get_option('cache_everything_cache_options', []);
112136

113137
// Retrieve cache times with defaults
114138
$max_age = get_option('cache_everything_max_age', 28800);

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ services:
1414
WORDPRESS_DB_USER: wordpress
1515
WORDPRESS_DB_PASSWORD: ${WORDPRESS_DB_PASSWORD}
1616
WORDPRESS_DB_NAME: wordpress
17+
WORDPRESS_DEBUG: 'true' # Enable WP_DEBUG mode
1718
volumes:
1819
- wce_wordpress_data:/var/www/html
1920
- ./:/var/www/html/wp-content/plugins/cache-everything # Mount the plugin directory

0 commit comments

Comments
 (0)