diff --git a/on-demand-revalidation.php b/on-demand-revalidation.php index 6285005..2a37ab5 100644 --- a/on-demand-revalidation.php +++ b/on-demand-revalidation.php @@ -6,7 +6,7 @@ * Plugin URI: https://wordpress.org/plugins/on-demand-revalidation * GitHub Plugin URI: https://github.com/gdidentity/on-demand-revalidation * Description: Next.js On-Demand Revalidation on the post update, revalidate specific paths on the post update. - * Version: 1.1.2 + * Version: 1.1.3 * Author: GD IDENTITY * Author URI: https://gdidentity.sk * Text Domain: on-demand-revalidation diff --git a/src/Revalidation.php b/src/Revalidation.php index dd9d649..666d71d 100644 --- a/src/Revalidation.php +++ b/src/Revalidation.php @@ -12,6 +12,22 @@ public static function init() { add_action( 'save_post', [ self::class, 'handleSavePost' ], 10, 2 ); add_action( 'transition_post_status', [ self::class, 'handleTransitionPostStatus' ], 10, 3 ); add_action( 'on_demand_revalidation_on_post_update', [ self::class, 'revalidate' ], 10, 1 ); + add_action( 'pre_post_update', [ self::class, 'capture_old_permalink' ], 10, 3 ); + add_action( 'wp_trash_post', [ self::class, 'capture_old_permalink_before_trash' ], 10, 1 ); + } + + public static function capture_old_permalink( $post_ID, $data ) { + if ( 'trash' === $data['post_status'] ) { + return; + } + + $old_permalink = get_permalink( $post_ID ); + update_post_meta( $post_ID, '_old_permalink', $old_permalink ); + } + + public static function capture_old_permalink_before_trash( $post_ID ) { + $old_permalink = get_permalink( $post_ID ); + update_post_meta( $post_ID, '_old_permalink', $old_permalink ); } public static function handleSavePost( $post_id, $post ) { @@ -35,6 +51,7 @@ public static function handleSavePost( $post_id, $post ) { public static function handleTransitionPostStatus( $new_status, $old_status, $post ) { if ( ( ( 'draft' !== $old_status && 'trash' !== $old_status ) && 'trash' === $new_status ) || ( 'publish' === $old_status && 'draft' === $new_status ) ) { + self::revalidatePost( $post ); } } @@ -65,11 +82,23 @@ public static function revalidate( $post ) { $parse_permalink = parse_url( $post_permalink ); $page_path = '/'; - if ( isset( $parse_permalink['path'] ) ) { - $page_path = $parse_permalink['path']; + if ( isset( $parse_permalink['path'] ) && '/' !== $parse_permalink['path'] ) { + $page_path = substr( $parse_permalink['path'], -1 ) === '/' ? substr( $parse_permalink['path'], 0, -1 ) : $parse_permalink['path']; + $paths[] = $page_path; + } + + $old_permalink = get_post_meta( $post->ID, '_old_permalink', true ); + + if ( ! empty( $old_permalink ) ) { + $parse_old_permalink = parse_url( $old_permalink ); + + if ( isset( $parse_old_permalink['path'] ) && '/' !== $parse_old_permalink['path'] ) { + $old_page_path = substr( $parse_old_permalink['path'], -1 ) === '/' ? substr( $parse_old_permalink['path'], 0, -1 ) : $parse_old_permalink['path']; + $paths[] = $old_page_path; + } } - $paths[] = substr( $page_path, -1 ) === '/' ? substr( $page_path, 0, -1 ) : $page_path; + $paths = array_unique( $paths ); $revalidate_paths = trim( Settings::get( 'revalidate_paths', '', 'on_demand_revalidation_post_update_settings' ) ); $revalidate_paths = preg_split( '/\r\n|\n|\r/', $revalidate_paths );