22/**
33 * Plugin Name: Next.js Revalidation
44 * Description: Revalidates Next.js site when WordPress content changes
5- * Version: 1.0.2
5+ * Version: 1.0.3
66 * Author: 9d8
77 * Author URI: https://9d8.dev
88 */
@@ -28,9 +28,12 @@ public function __construct() {
2828 // Add admin menu
2929 add_action ('admin_menu ' , array ($ this , 'add_admin_menu ' ));
3030
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 );
3233 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 );
3437
3538 // Term changes
3639 add_action ('created_term ' , array ($ this , 'on_term_change ' ), 10 , 3 );
@@ -213,6 +216,19 @@ public function handle_manual_revalidation() {
213216 }
214217 }
215218
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+
216232 public function on_content_change ($ post_id , $ post = null , $ update = null ) {
217233 // Don't revalidate on autosave or revision
218234 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) {
224240 $ post = get_post ($ post_id );
225241 }
226242
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+ }
231246
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 );
237255 }
238256
239257 public function on_post_delete ($ post_id ) {
0 commit comments