Skip to content

Commit 4344847

Browse files
admin section improved
1 parent 5cd3503 commit 4344847

File tree

8 files changed

+299
-103
lines changed

8 files changed

+299
-103
lines changed

plugin/admin/configuration.php

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
/**
4+
* Register settings for the Cache Everything plugin.
5+
*/
6+
function cache_everything_register_settings() {
7+
// Register settings for cache times first
8+
register_setting('cache_everything_settings', 'cache_everything_max_age');
9+
register_setting('cache_everything_settings', 'cache_everything_stale_while_revalidate');
10+
11+
// Add a new section for cache times at the beginning
12+
add_settings_section(
13+
'cache_everything_cache_times_section', // Unique ID for the section
14+
'Cache Times', // Title of the section
15+
'cache_everything_cache_times_section_callback', // Callback function to render the section
16+
'cache-everything-settings' // Page on which to add the section
17+
);
18+
19+
// Add settings fields for cache times in the new section
20+
add_settings_field(
21+
'cache_everything_max_age',
22+
'Cache Max Age (seconds)',
23+
'cache_everything_max_age_callback',
24+
'cache-everything-settings',
25+
'cache_everything_cache_times_section' // Use the new section ID
26+
);
27+
28+
add_settings_field(
29+
'cache_everything_stale_while_revalidate',
30+
'Stale While Revalidate (seconds)',
31+
'cache_everything_stale_while_revalidate_callback',
32+
'cache-everything-settings',
33+
'cache_everything_cache_times_section' // Use the new section ID
34+
);
35+
// Register the new caching options
36+
register_setting('cache_everything_settings', 'cache_everything_options');
37+
38+
// Add the Caching Options section
39+
add_settings_section(
40+
'cache_everything_caching_section',
41+
'Caching Options',
42+
'cache_everything_caching_section_callback',
43+
'cache-everything-settings'
44+
);
45+
46+
// Define the caching options with descriptions
47+
$options = [
48+
'is_front_page' => ['label' => 'Front Page', 'description' => 'The main landing page of your site.'],
49+
'is_home' => ['label' => 'Home', 'description' => 'The page showing your latest posts, often the same as the front page.'],
50+
'is_singular' => ['label' => 'Singular', 'description' => 'Any single post, page, or attachment.'],
51+
'is_single' => ['label' => 'Single Post', 'description' => 'A single post. Does not include attachments or pages.'],
52+
'is_page' => ['label' => 'Page', 'description' => 'A single page. Does not include posts or attachments.'],
53+
'is_attachment' => ['label' => 'Attachment', 'description' => 'An attachment page, displaying a single attachment.'],
54+
'is_archive' => ['label' => 'Archive', 'description' => 'Any archive page, including category, tag, author, or date-based archives.'],
55+
'is_post_type_archive' => ['label' => 'Post Type Archive', 'description' => 'An archive page for a custom post type.'],
56+
'is_category' => ['label' => 'Category', 'description' => 'A category archive, showing posts from a specific category.'],
57+
'is_tag' => ['label' => 'Tag', 'description' => 'A tag archive, showing posts tagged with a specific tag.'],
58+
'is_author' => ['label' => 'Author', 'description' => 'An author archive, showing posts by a specific author.'],
59+
'is_date' => ['label' => 'Date', 'description' => 'A date-based archive, for specific years, months, or days.'],
60+
'is_year' => ['label' => 'Year', 'description' => 'A yearly archive, showing posts from a specific year.'],
61+
'is_month' => ['label' => 'Month', 'description' => 'A monthly archive, showing posts from a specific month.'],
62+
'is_day' => ['label' => 'Day', 'description' => 'A daily archive, showing posts from a specific day.'],
63+
'is_time' => ['label' => 'Time', 'description' => 'A time-based archive, often unused.'],
64+
'is_search' => ['label' => 'Search', 'description' => 'A search results page, showing posts matching a search query.'],
65+
'is_404' => ['label' => '404 Page', 'description' => 'The page shown when no content is found (error 404).'],
66+
'is_paged' => ['label' => 'Paged', 'description' => 'For pages of posts that are split into multiple pages.'],
67+
];
68+
69+
// Add fields for each caching option
70+
foreach ($options as $option => $details) {
71+
add_settings_field(
72+
$option,
73+
$details['label'],
74+
'cache_everything_checkbox_callback',
75+
'cache-everything-settings',
76+
'cache_everything_caching_section',
77+
['id' => $option, 'description' => $details['description']]
78+
);
79+
}
80+
81+
// Continue with the rest of the settings registration as before
82+
// Register a new setting for Cache Everything to store the debug mode option
83+
register_setting('cache_everything_settings', 'cache_everything_debug_mode');
84+
85+
// Add the Debug Mode section with an explanation about its functionality
86+
add_settings_section(
87+
'cache_everything_debug_section',
88+
'Debug Mode Settings',
89+
'cache_everything_debug_section_callback',
90+
'cache-everything-settings'
91+
);
92+
93+
// Add the debug mode option field to the Debug Mode section, explaining its impact on JavaScript console logging
94+
add_settings_field(
95+
'cache_everything_debug_mode',
96+
'Debug Mode',
97+
'cache_everything_debug_mode_callback',
98+
'cache-everything-settings',
99+
'cache_everything_debug_section'
100+
);
101+
}
102+
103+
// Callback for the new section
104+
function cache_everything_cache_times_section_callback() {
105+
echo '<p>Configure cache expiration times:</p>';
106+
}
107+
108+
function cache_everything_caching_section_callback() {
109+
echo '<p>Select the types of content for which you want to enable caching:</p>';
110+
}
111+
112+
function cache_everything_debug_mode_callback() {
113+
$debug_mode = get_option('cache_everything_debug_mode');
114+
echo '<input type="checkbox" id="cache_everything_debug_mode" name="cache_everything_debug_mode" value="1" ' . checked(1, $debug_mode, false) . '/>';
115+
echo '<label for="cache_everything_debug_mode">Enable Debug Mode</label>';
116+
echo '<p class="description">Enables console.log prints in JavaScript for client-side debugging.</p>';
117+
}
118+
119+
function cache_everything_checkbox_callback($args) {
120+
$options = get_option('cache_everything_options');
121+
$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>";
123+
echo "<label for='{$args['id']}'>{$args['description']}</label>";
124+
}
125+
126+
// Callback for max-age
127+
function cache_everything_max_age_callback() {
128+
$value = get_option('cache_everything_max_age', 28800); // Default to 28800 if not set
129+
// Explanation of Cloudflare minimum values by plan
130+
$explanation = <<<EXPLANATION
131+
<p>Minimum Browser Cache TTL based on Cloudflare plan:</p>
132+
<ul>
133+
<li>Free Plan: 7200 seconds (2 hours)</li>
134+
<li>Pro Plan: 3600 seconds (1 hour)</li>
135+
<li>Business Plan: 1 second</li>
136+
<li>Enterprise Plan: 1 second</li>
137+
</ul>
138+
<p>Please ensure the value respects the minimum required by your Cloudflare plan.</p>
139+
EXPLANATION;
140+
141+
echo "<input type='number' id='cache_everything_max_age' name='cache_everything_max_age' value='" . esc_attr($value) . "' />";
142+
echo $explanation;
143+
}
144+
145+
// Callback for stale-while-revalidate
146+
function cache_everything_stale_while_revalidate_callback() {
147+
$value = get_option('cache_everything_stale_while_revalidate', 86400); // Default to 86400 if not set
148+
echo "<input type='number' id='cache_everything_stale_while_revalidate' name='cache_everything_stale_while_revalidate' value='" . esc_attr($value) . "' />";
149+
echo '<p class="description">Stale While Revalidate allows the use of stale resources while new ones are being revalidated in the background, ensuring users get responses without delay.</p>';
150+
}
151+
152+
// Adjusted function to display settings
153+
function cache_everything_settings_page() {
154+
// Continue with the settings form
155+
echo '<form action="options.php" method="post">';
156+
settings_fields('cache_everything_settings');
157+
do_settings_sections('cache-everything-settings'); // Adjusted to match the settings page slug
158+
submit_button();
159+
echo '</form>';
160+
}
161+
162+
function cache_everything_debug_section_callback() {
163+
echo '<p>Enable or disable debug mode:</p>';
164+
}

plugin/admin/css-classes.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* Display the CSS Classes page content.
5+
*/
6+
function cache_everything_display_css_classes() {
7+
// Retrieve the CSS classes based on roles
8+
$css_classes = generate_css_classes_based_on_roles();
9+
10+
?>
11+
<div class="wrap">
12+
<h2>Available CSS Classes</h2>
13+
<p>Below are the CSS classes available for use, based on your site and user roles, including generic guest and user status.</p>
14+
<p>Elements with the following CSS classes will be hidden FOR ALL USERS on the front end UNLESS the user's role matches the class specified. Each element can only have one of these classes applied; mixing multiple classes on a single element is not supported.</p>
15+
<ul>
16+
<?php
17+
foreach ($css_classes as $class) {
18+
echo "<li>{$class}</li>";
19+
}
20+
echo '</ul>';
21+
echo '</div>';
22+
}

plugin/admin/menu.php

Lines changed: 13 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
function cache_everything_activate() {
44
add_option('cache_everything_debug_mode', '0'); // Default value set to '0' (debug mode off)
5+
add_option('cache_everything_options', []); // Initialize caching options
6+
add_option('cache_everything_max_age', '28800'); // Default cache max age set to '28800' seconds
7+
add_option('cache_everything_stale_while_revalidate', '86400'); // Default stale while revalidate set to '86400' seconds
58
}
69

710
/**
@@ -15,10 +18,19 @@ function cache_everything_add_admin_menu() {
1518
'manage_options', // Capability
1619
'cache-everything-settings', // Menu slug for settings
1720
'cache_everything_settings_page', // Function to display the settings page
18-
'dashicons-admin-generic', // Icon URL
21+
'dashicons-cloud', // Icon URL
1922
6 // Position
2023
);
2124

25+
add_submenu_page(
26+
'cache-everything-settings', // Parent slug
27+
'Available CSS Classes', // Page title
28+
'CSS Classes', // Menu title
29+
'manage_options', // Capability
30+
'cache-everything-css-classes', // Menu slug for CSS Classes
31+
'cache_everything_display_css_classes' // Function to display the CSS Classes page
32+
);
33+
2234
// Readme Page
2335
add_submenu_page(
2436
'cache-everything-settings', // Parent slug
@@ -33,76 +45,3 @@ function cache_everything_add_admin_menu() {
3345
}
3446

3547
add_action('admin_menu', 'cache_everything_add_admin_menu');
36-
37-
/**
38-
* Register settings for the Cache Everything plugin.
39-
*/
40-
function cache_everything_register_settings() {
41-
// Register a new setting for Cache Everything to store the debug mode option
42-
register_setting('cache_everything_settings', 'cache_everything_debug_mode');
43-
44-
// Add a new section to the Cache Everything admin page
45-
add_settings_section(
46-
'cache_everything_settings_section', // ID
47-
'Cache Everything Settings', // Title
48-
'cache_everything_settings_section_callback', // Callback
49-
'cache-everything-settings' // Page
50-
);
51-
52-
// Add the debug mode option field to the new section
53-
add_settings_field(
54-
'cache_everything_debug_mode', // ID
55-
'Debug Mode', // Title
56-
'cache_everything_debug_mode_callback', // Callback
57-
'cache-everything-settings', // Page
58-
'cache_everything_settings_section' // Section
59-
);
60-
}
61-
62-
/**
63-
* Settings section callback function.
64-
*/
65-
function cache_everything_settings_section_callback() {
66-
echo '<p>Adjust the settings for Cache Everything.</p>';
67-
}
68-
69-
/**
70-
* Debug mode field callback function.
71-
*/
72-
function cache_everything_debug_mode_callback() {
73-
$debug_mode = get_option('cache_everything_debug_mode');
74-
echo '<input type="checkbox" id="cache_everything_debug_mode" name="cache_everything_debug_mode" value="1" ' . checked(1, $debug_mode, false) . '/>';
75-
echo '<label for="cache_everything_debug_mode">Enable Debug Mode</label>';
76-
}
77-
78-
// Adjusted function to display settings
79-
function cache_everything_settings_page() {
80-
echo '<form action="options.php" method="post">';
81-
settings_fields('cache_everything_settings');
82-
do_settings_sections('cache-everything-settings'); // Adjusted to match the settings page slug
83-
submit_button();
84-
echo '</form>';
85-
}
86-
87-
// Adjusted function to only display the readme content
88-
function cache_everything_display_readme() {
89-
$readme_path = plugin_dir_path(__FILE__) . '../readme.md';
90-
if (file_exists($readme_path)) {
91-
$readme_content = file_get_contents($readme_path);
92-
// Escape the content for JavaScript
93-
$readme_content_js = json_encode($readme_content);
94-
echo '<div id="readmeContent" class="wce_wrap"></div>'; // Container for the converted HTML
95-
echo "<script src=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/showdown.min.js\"></script>";
96-
echo "<script>
97-
document.addEventListener('DOMContentLoaded', function() {
98-
var converter = new showdown.Converter(),
99-
text = $readme_content_js,
100-
html = converter.makeHtml(text);
101-
document.getElementById('readmeContent').innerHTML = html;
102-
});
103-
</script>";
104-
} else {
105-
// Print out the full path attempted for debugging purposes
106-
echo '<div class="wrap"><h1>Readme File Not Found</h1><p>Attempted path: ' . esc_html($readme_path) . '</p></div>';
107-
}
108-
}

plugin/admin/readme.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
// Adjusted function to only display the readme content
4+
function cache_everything_display_readme() {
5+
$readme_path = plugin_dir_path(__FILE__) . '../readme.md';
6+
if (file_exists($readme_path)) {
7+
$readme_content = file_get_contents($readme_path);
8+
// Escape the content for JavaScript
9+
$readme_content_js = json_encode($readme_content);
10+
echo '<div id="readmeContent" class="wce_wrap"></div>'; // Container for the converted HTML
11+
echo "<script src=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/showdown.min.js\"></script>";
12+
echo "<script>
13+
document.addEventListener('DOMContentLoaded', function() {
14+
var converter = new showdown.Converter(),
15+
text = $readme_content_js,
16+
html = converter.makeHtml(text);
17+
document.getElementById('readmeContent').innerHTML = html;
18+
});
19+
</script>";
20+
} else {
21+
// Print out the full path attempted for debugging purposes
22+
echo '<div class="wrap"><h1>Readme File Not Found</h1><p>Attempted path: ' . esc_html($readme_path) . '</p></div>';
23+
}
24+
}

plugin/cache-everything.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
* Plugin Name: Cache Everything
44
* Plugin URI: https://github.com/AsyncAlchemist
55
* Description: A simple plugin to cache everything in Wordpress.
6-
* Version: 0.19
6+
* Version: 0.20
77
* Author: Taylor Selden
88
* Author URI: https://github.com/AsyncAlchemist
99
*/
1010
define('CACHE_EVERYTHING_JS_URL', 'wp-content/plugins/cache-everything/js');
1111
define('CACHE_EVERYTHING_CSS_URL', 'wp-content/plugins/cache-everything/css');
12-
define('CACHE_EVERYTHING_VERSION', '0.19');
12+
define('CACHE_EVERYTHING_VERSION', '0.20');
1313

1414
require_once(plugin_dir_path(__FILE__) . 'handle-js-request.php');
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-
18+
require_once(plugin_dir_path(__FILE__) . 'admin/configuration.php');
19+
require_once(plugin_dir_path(__FILE__) . 'admin/readme.php');
20+
require_once(plugin_dir_path(__FILE__) . 'admin/css-classes.php');
1921

2022
/**
2123
* Flushes rewrite rules on plugin activation and deactivation.
@@ -97,4 +99,32 @@ function cache_everything_enqueue_scripts() {
9799
));
98100

99101
}
100-
add_action('wp_enqueue_scripts', 'cache_everything_enqueue_scripts');
102+
add_action('wp_enqueue_scripts', 'cache_everything_enqueue_scripts');
103+
104+
function cache_everything_modify_headers() {
105+
// Check if the admin bar is showing, and if so, return early to avoid caching
106+
if (is_admin_bar_showing()) {
107+
return;
108+
}
109+
110+
// Retrieve the options with a default to an empty array if not set
111+
$options = get_option('cache_everything_options', []);
112+
113+
// Retrieve cache times with defaults
114+
$max_age = get_option('cache_everything_max_age', 28800);
115+
$stale_while_revalidate = get_option('cache_everything_stale_while_revalidate', 86400);
116+
117+
// Calculate the Expires header value
118+
$expires_time = gmdate('D, d M Y H:i:s', time() + $max_age) . ' GMT';
119+
120+
// Loop through each option and apply headers if enabled
121+
foreach ($options as $option => $value) {
122+
if ($value && function_exists($option) && call_user_func($option)) {
123+
header("Cache-Control: public, max-age=$max_age, stale-while-revalidate=$stale_while_revalidate");
124+
header("Expires: $expires_time");
125+
header("X-WCE-Cache: public, max-age=$max_age, stale-while-revalidate=$stale_while_revalidate");
126+
break; // Stop the loop once a match is found and header is set
127+
}
128+
}
129+
}
130+
add_action('template_redirect', 'cache_everything_modify_headers');

plugin/handle-css-request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function cache_everything_handle_css_request() {
2626
echo "body.elementor-editor-active .$site_prefix-$slug { position: relative; }\n";
2727

2828
// Apply the rule specifically when the Elementor editor is active
29-
echo "body.elementor-editor-active .$site_prefix-$slug::after {
29+
echo "body.elementor-editor-active .$site_prefix-$slug::after {
3030
content: '👁️';
3131
position: absolute;
3232
top: 0;

0 commit comments

Comments
 (0)