Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved slug when duplicating block #318

Merged
merged 6 commits into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions classes/class-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,18 +620,28 @@ public function duplicate_block( $block_id ) {
$new_post_author = $current_user->ID;

if ( isset( $post ) && $post ) {
$post_name = $post->post_name;
$post_title = $post->post_title;
$copy_postfix = esc_html__( '(Copy)', 'lazy-blocks' );

// Add (Copy) postfix to duplicated block title if needed.
if ( ! empty( $post_title ) && ! str_contains( $post_title, $copy_postfix ) ) {
$post_title = $post_title . ' ' . $copy_postfix;
}

// New post data array.
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_name' => $post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => $post->post_title,
// We must set the status to publish for the slug generation function (wp_unique_post_slug) to work correctly.
'post_status' => 'publish',
'post_title' => $post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order,
Expand All @@ -640,7 +650,7 @@ public function duplicate_block( $block_id ) {
// Insert new post.
$new_post_id = wp_insert_post( $args );

// Get all current post terms ad set them to the new post draft.
// Get all current post terms ad set them to the new post.
$taxonomies = get_object_taxonomies( $post->post_type );

foreach ( $taxonomies as $taxonomy ) {
Expand Down Expand Up @@ -685,11 +695,27 @@ public function duplicate_block( $block_id ) {

foreach ( $meta_values as $meta_value ) {
$meta_value = maybe_unserialize( $meta_value );

if ( 'lazyblocks_slug' === $meta_key ) {
// Copy new post slug to meta.
$new_post = get_post( $new_post_id );
$meta_value = $new_post->post_name;
}

add_post_meta( $new_post_id, $meta_key, wp_slash( $meta_value ) );
}
}
}

// Change the status to draft after all manipulations with the slug.
wp_update_post(
array(
'post_type' => $post->post_type,
'ID' => $new_post_id,
'post_status' => 'draft',
)
);

// Redirect.
wp_safe_redirect( admin_url( 'edit.php?post_type=lazyblocks&lazyblocks_duplicate_complete=' . $new_post_id ) );

Expand Down
Loading