2
2
/**
3
3
* Plugin Name: Next.js Revalidation
4
4
* Description: Revalidates Next.js site when WordPress content changes
5
- * Version: 1.0.2
5
+ * Version: 1.0.3
6
6
* Author: 9d8
7
7
* Author URI: https://9d8.dev
8
8
*/
@@ -28,9 +28,12 @@ public function __construct() {
28
28
// Add admin menu
29
29
add_action ('admin_menu ' , array ($ this , 'add_admin_menu ' ));
30
30
31
- // Post save/update hooks
31
+ // Post save/update hooks - any post status change
32
+ add_action ('transition_post_status ' , array ($ this , 'on_post_status_change ' ), 10 , 3 );
32
33
add_action ('save_post ' , array ($ this , 'on_content_change ' ), 10 , 3 );
33
- add_action ('deleted_post ' , array ($ this , 'on_post_delete ' ), 10 );
34
+ add_action ('wp_trash_post ' , array ($ this , 'on_post_trash ' ), 10 );
35
+ add_action ('untrash_post ' , array ($ this , 'on_post_untrash ' ), 10 );
36
+ add_action ('before_delete_post ' , array ($ this , 'on_post_delete ' ), 10 );
34
37
35
38
// Term changes
36
39
add_action ('created_term ' , array ($ this , 'on_term_change ' ), 10 , 3 );
@@ -213,6 +216,19 @@ public function handle_manual_revalidation() {
213
216
}
214
217
}
215
218
219
+ // Triggered when a post changes status (draft, publish, trash, etc.)
220
+ public function on_post_status_change ($ new_status , $ old_status , $ post ) {
221
+ // Skip if it's a revision or autosave
222
+ if (wp_is_post_revision ($ post ->ID ) || wp_is_post_autosave ($ post ->ID )) {
223
+ return ;
224
+ }
225
+
226
+ // If the status is changing, we should revalidate
227
+ if ($ new_status !== $ old_status ) {
228
+ $ this ->send_revalidation_request ($ post ->post_type , $ post ->ID );
229
+ }
230
+ }
231
+
216
232
public function on_content_change ($ post_id , $ post = null , $ update = null ) {
217
233
// Don't revalidate on autosave or revision
218
234
if (wp_is_post_revision ($ post_id ) || wp_is_post_autosave ($ post_id )) {
@@ -224,16 +240,18 @@ public function on_content_change($post_id, $post = null, $update = null) {
224
240
$ post = get_post ($ post_id );
225
241
}
226
242
227
- // Only revalidate published content
228
- if ('publish ' !== $ post ->post_status ) {
229
- return ;
230
- }
243
+ // Revalidate regardless of post status
244
+ $ this ->send_revalidation_request ($ post ->post_type , $ post_id );
245
+ }
231
246
232
- // Determine content type
233
- $ content_type = $ post ->post_type ;
234
-
235
- // Revalidate the entire site
236
- $ this ->send_revalidation_request ($ content_type , $ post_id );
247
+ public function on_post_trash ($ post_id ) {
248
+ $ post_type = get_post_type ($ post_id );
249
+ $ this ->send_revalidation_request ($ post_type , $ post_id );
250
+ }
251
+
252
+ public function on_post_untrash ($ post_id ) {
253
+ $ post_type = get_post_type ($ post_id );
254
+ $ this ->send_revalidation_request ($ post_type , $ post_id );
237
255
}
238
256
239
257
public function on_post_delete ($ post_id ) {
0 commit comments