PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.0.23
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.0.23
4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 1.9.3 1.9.4 1.9.5 1.9.6 All 170 releases
searchpro / admin / class-berqPageRules.php

class-berqPageRules.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 4.0.23, at admin/class-berqPageRules.php

235 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined("ABSPATH")) exit;
4
5 class berqPageRules
6 {
7 function __construct()
8 {
9 add_action('add_meta_boxes', function () {
10 $bwp_post_types = get_option('berqwp_optimize_post_types');
11 add_meta_box(
12 'berqwp_page_rules',
13 'BerqWP Cache Rules',
14 [$this, 'page_rules_meta_box_cb'],
15 $bwp_post_types,
16 'advanced',
17 'default'
18 );
19 });
20
21 add_action('save_post', [$this, 'save_rules']);
22 add_action('save_post', [$this, 'apply_rules'], 10, 3);
23 }
24
25 function page_rules_meta_box_cb($post)
26 {
27 wp_nonce_field('berqwp_rules_save', 'berqwp_rules_nonce');
28
29 $rules = get_option('berqwp_cache_rules');
30 if (!is_array($rules)) $rules = [];
31 $rules = $rules[$post->ID] ?? [];
32
33 $actions = [
34 'flush_cache' => 'Flush page cache',
35 ];
36
37 $events = [
38 'on_update_post_type' => 'On publish/update any post in post type',
39 ];
40
41 $post_types = get_post_types(['public' => true], 'objects');
42
43 ?>
44 <style>
45 table.berqwp-rules-table {
46 width: 100%;
47 border-collapse: collapse;
48 margin-bottom: 10px;
49 }
50
51 .berqwp-rules-table th,
52 .berqwp-rules-table td {
53 border: 1px solid #ddd;
54 padding: 6px 8px;
55 text-align: left;
56 }
57
58 .berqwp-rules-table th {
59 background: #f7f7f7;
60 }
61
62 .berqwp-rules-table td select {
63 width: 100%;
64 }
65
66 .berqwp-add-rule {
67 margin-top: 10px;
68 }
69 </style>
70
71 <p>Create rules to automatically manage BerqWP cache for this page.</p>
72
73 <div class="berqwp-add-rule">
74 <button type="button" class="button button-primary" id="berqwp-add-rule">+ Add New Rule</button>
75 </div>
76
77 <p></p>
78 <table class="berqwp-rules-table">
79 <thead>
80 <tr>
81 <th>Action</th>
82 <th>Event</th>
83 <th>Post Type</th>
84 <th>Delete</th>
85 </tr>
86 </thead>
87 <tbody id="berqwp-rules-body">
88 <?php foreach ($rules as $index => $rule): ?>
89 <tr>
90 <td>
91 <select name="berqwp_rules[<?php echo $index; ?>][action]">
92 <option value=""></option>
93 <?php foreach ($actions as $key => $label): ?>
94 <option value="<?php echo esc_attr($key); ?>" <?php selected($rule['action'] ?? '', $key); ?>>
95 <?php echo esc_html($label); ?>
96 </option>
97 <?php endforeach; ?>
98 </select>
99 </td>
100 <td>
101 <select name="berqwp_rules[<?php echo $index; ?>][event]" class="berqwp-event">
102 <option value=""></option>
103 <?php foreach ($events as $key => $label): ?>
104 <option value="<?php echo esc_attr($key); ?>" <?php selected($rule['event'] ?? '', $key); ?>>
105 <?php echo esc_html($label); ?>
106 </option>
107 <?php endforeach; ?>
108 </select>
109 </td>
110 <td>
111 <select name="berqwp_rules[<?php echo $index; ?>][post_type]" class="berqwp-post-type">
112 <option value=""></option>
113 <?php foreach ($post_types as $key => $type): ?>
114 <option value="<?php echo esc_attr($key); ?>" <?php selected($rule['post_type'] ?? '', $key); ?>>
115 <?php echo esc_html($type->labels->singular_name); ?>
116 </option>
117 <?php endforeach; ?>
118 </select>
119 </td>
120
121 <td><button type="button" class="button berqwp-delete-rule">×</button></td>
122 </tr>
123 <?php endforeach; ?>
124 </tbody>
125 </table>
126
127 <script>
128 (function($) {
129 $('#berqwp-add-rule').on('click', function() {
130 let index = $('#berqwp-rules-body tr').length;
131 let newRow = `
132 <tr>
133 <td>
134 <select name="berqwp_rules[${index}][action]">
135 <option value="">—</option>
136 <?php foreach ($actions as $key => $label): ?>
137 <option value="<?php echo esc_attr($key); ?>"><?php echo esc_html($label); ?></option>
138 <?php endforeach; ?>
139 </select>
140 </td>
141 <td>
142 <select name="berqwp_rules[${index}][event]" class="berqwp-event">
143 <option value="">—</option>
144 <?php foreach ($events as $key => $label): ?>
145 <option value="<?php echo esc_attr($key); ?>"><?php echo esc_html($label); ?></option>
146 <?php endforeach; ?>
147 </select>
148 </td>
149 <td>
150 <select name="berqwp_rules[${index}][post_type]" class="berqwp-post-type">
151 <option value="">—</option>
152 <?php foreach ($post_types as $key => $type): ?>
153 <option value="<?php echo esc_attr($key); ?>"><?php echo esc_html($type->labels->singular_name); ?></option>
154 <?php endforeach; ?>
155 </select>
156 </td>
157 <td><button type="button" class="button berqwp-delete-rule">×</button></td>
158 </tr>`;
159 $('#berqwp-rules-body').append(newRow);
160 });
161
162 $(document).on('click', '.berqwp-delete-rule', function() {
163 $(this).closest('tr').remove();
164 });
165 })(jQuery);
166 </script>
167 <?php
168 }
169
170 function save_rules($post_id)
171 {
172 if (!isset($_POST['berqwp_rules_nonce']) || !wp_verify_nonce($_POST['berqwp_rules_nonce'], 'berqwp_rules_save')) return;
173
174 $rules = get_option('berqwp_cache_rules');
175 if (!is_array($rules)) $rules = [];
176
177 if (!empty($_POST['berqwp_rules'])) {
178 $clean_rules = array_map(function ($rule) {
179 return [
180 'action' => sanitize_text_field($rule['action'] ?? ''),
181 'event' => sanitize_text_field($rule['event'] ?? ''),
182 'post_type' => sanitize_text_field($rule['post_type'] ?? ''),
183 ];
184 }, $_POST['berqwp_rules']);
185 $rules[$post_id] = $clean_rules;
186
187 // update_post_meta($post_id, '_berqwp_cache_rules', $clean_rules);
188 update_option('berqwp_cache_rules', $rules, false);
189 } else {
190 unset($rules[$post_id]);
191 update_option('berqwp_cache_rules', $rules, false);
192 }
193 }
194
195 function apply_rules($post_id, $post, $update)
196 {
197 // Ignore auto-saves, revisions, etc.
198 if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id)) return;
199
200 global $berq_log;
201
202 $page_url = get_permalink($post_id);
203 $rules = get_option('berqwp_cache_rules');
204 if (!is_array($rules)) $rules = [];
205
206 if (empty($rules)) {
207 return;
208 }
209
210 foreach ($rules as $key => $value) {
211 $rules_matched_post_type = array_filter($value, function ($item) use ($post) {
212 return $item['post_type'] === $post->post_type;
213 });
214
215 foreach ($rules_matched_post_type as $rule) {
216
217 if ($rule['event'] === 'on_update_post_type') {
218
219 if ($rule['post_type'] === $post->post_type) {
220
221 if ($rule['action'] === 'flush_cache') {
222 berqCache::purge_page($page_url, true);
223 $berq_log->info("Rule triggered - event: {$rule['event']} post type: {$rule['post_type']} action: {$rule['action']}");
224 }
225 }
226 }
227 }
228
229 }
230
231 }
232 }
233
234 new berqPageRules();
235