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 ' );
0 commit comments