Skip to content

Commit

Permalink
Merge pull request #9 from Dexerto/feat/handle-draft-permalinks
Browse files Browse the repository at this point in the history
Add old permalink tracking to revalidation process
  • Loading branch information
bebjakub authored Jun 21, 2023
2 parents 574563b + 99b93b6 commit 172ebcc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion on-demand-revalidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 32 additions & 3 deletions src/Revalidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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 );
}
}
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit 172ebcc

Please sign in to comment.