Created
June 10, 2025 13:36
-
-
Save mlbd/d4e8662ee40b49857476aefb47d5ef1f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Duplicate custom permalink structure from original post to its revision post. | |
* | |
* @param int $new_post_id The ID of the new revision post. | |
* @param WP_Post $post The original post object. | |
* | |
* @since 3.2.0 | |
*/ | |
function owf_handle_custom_permalink_on_duplicate( $new_post_id, $post ) { | |
// Check if the post exists and is not an attachment | |
if ( ! $post || ! isset( $post->ID ) || $post->post_type == 'attachment' ) { | |
return; | |
} | |
// check if post is a revision | |
$original_post_id = get_post_meta( $new_post_id, '_oasis_original', true ); | |
if ( empty( $original_post_id ) ) { // we are dealing with a original post | |
return; | |
} | |
// Duplicate the custom permalink structure | |
$custom_permalinks = get_post_meta( $post->ID, 'custom_permalink', false ); | |
$older_permalink = ! empty( $custom_permalinks ) && is_array( $custom_permalinks ) ? $custom_permalinks[0] : $custom_permalinks; | |
$prefix = ''; | |
$suffix = ''; | |
$new_post_author = ''; | |
if ( $post->post_type != 'attachment' ) { | |
$prefix = get_option( 'oasiswf_doc_revision_title_prefix' ); | |
$suffix = get_option( 'oasiswf_doc_revision_title_suffix' ); | |
if ( ! empty( $prefix ) ) { | |
$prefix .= " "; | |
} | |
if ( ! empty( $suffix ) ) { | |
$suffix = " " . $suffix; | |
} | |
} | |
$old_slug = basename( untrailingslashit( $post->post_name ) ); | |
$new_slug = sanitize_title( $prefix . $post->post_title . $suffix ); | |
if ( $older_permalink ) { | |
// Replace old slug with new one in the original custom permalink | |
$new_permalink = str_replace( $old_slug, $new_slug, $older_permalink ); | |
} else { | |
// Build permalink from post hierarchy (fallback) | |
$ancestors = get_post_ancestors( $post ); | |
$ancestors = array_reverse( $ancestors ); | |
$parts = []; | |
foreach ( $ancestors as $ancestor_id ) { | |
$parts[] = get_post_field( 'post_name', $ancestor_id ); | |
} | |
$parts[] = $new_slug; | |
$new_permalink = implode( '/', $parts ); | |
} | |
// delete old custom permalink meta | |
delete_post_meta( $new_post_id, 'custom_permalink' ); | |
// Save it to new post | |
update_post_meta( $new_post_id, 'custom_permalink', $new_permalink ); | |
update_post_meta( $new_post_id, 'original_custom_permalink', $custom_permalinks ); | |
} | |
add_action( 'owf_duplicate_page', 'owf_handle_custom_permalink_on_duplicate', 50, 2 ); | |
add_action( 'owf_duplicate_post', 'owf_handle_custom_permalink_on_duplicate', 50, 2 ); | |
/** | |
* Action hook function for `owf_revision_workflow_complete` action hook. | |
* | |
* Purpose is to update the custom permalink for the original post when the revision is merged. | |
* | |
* It first deletes the custom permalink meta for the original post because when it merges the revision, it will added revision custom permalink value in "custom_permalink" meta | |
* so we need to delete it first. | |
* | |
* Then it updates the custom permalink for the original post. | |
* | |
* @param int $post_id The ID of the post that has just been merged. | |
* | |
* @since 1.0.0 | |
*/ | |
function owf_revision_workflow_complete_cb( $post_id ) { | |
$original_post_id = get_post_meta( $post_id, '_oasis_original', true ); | |
// If the original post ID is not empty, we can proceed to update the custom permalink | |
if ( ! empty( $original_post_id ) ) { | |
// get original post meta "custom_permalink" | |
$original_custom_permalink = get_post_meta( $post_id, 'original_custom_permalink', true ); | |
// now update "custom_permalink" meta for the revision post | |
if ( ! empty( $original_custom_permalink ) ) { | |
// first delete custom permalink meta from the original post because when it merges the revision, it will added revision custom permalink value in "custom_permalink" meta | |
// so we need to delete it first | |
delete_post_meta( $original_post_id, 'custom_permalink' ); | |
// skip adding original_custom_permalink to the original post, so delete it, because it's already added when revision merged. | |
delete_post_meta( $original_post_id, 'original_custom_permalink' ); | |
// update the custom permalink for the original post | |
// this is needed because when the revision is merged, it will update the custom permalink for the original post | |
if( ! empty( $original_custom_permalink ) && is_array( $original_custom_permalink ) ) { | |
foreach ( $original_custom_permalink as $permalink ) { | |
// update the custom permalink for the original post | |
add_post_meta( $original_post_id, 'custom_permalink', $permalink ); | |
} | |
} elseif ( ! empty( $original_custom_permalink ) && ! is_array( $original_custom_permalink ) ) { | |
// if it's not an array, just update the custom permalink for the original post | |
add_post_meta( $original_post_id, 'custom_permalink', $original_custom_permalink ); | |
} | |
} | |
} | |
} | |
add_action('owf_revision_workflow_complete', 'owf_revision_workflow_complete_cb', 20, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment