|
| 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 | +} |
0 commit comments