Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Fix renaming bug where there were 2 patterns #61

Merged
merged 3 commits into from
Feb 21, 2023

Conversation

kienstra
Copy link
Contributor

@kienstra kienstra commented Feb 20, 2023

Before

Renaming (changing the title) of a pattern resulted in 2 patterns. One pattern with the new name, and a pattern with the old name.

After

This deletes the pattern with the old name.


Fixes part of #57 (Renaming a pattern creates a new copy)

How to test

  1. Create a new pattern
  2. Add some content
  3. Click 'Update Pattern': Screenshot 2023-02-20 at 12 55 46 PM
  4. Change the title of the pattern
  5. Click 'Update Pattern'
  6. Expected: The theme's patterns/ directory only has a pattern with the new name, not the old name:

Screenshot 2023-02-20 at 12 58 28 PM

@@ -110,8 +109,8 @@ function save_metadata_to_pattern_file( $override, $post_id, $meta_key, $meta_va
]
);

if ( $previous_value && $previous_value !== $meta_value ) {
delete_pattern( $meta_value );
if ( $pattern_name !== $meta_value ) {
Copy link
Contributor Author

@kienstra kienstra Feb 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$pattern_name is a more reliable previous pattern name. Every time we're in the editor, there will be a pm_pattern post with a ->post_title (the pattern name).

@@ -173,16 +172,13 @@ function redirect_pattern_actions() {
}

if ( 'edit-pattern' === filter_input( INPUT_GET, 'action' ) ) {
// Prevent the hook from overwriting the file when this post is created.
remove_action( 'rest_after_insert_pm_pattern', __NAMESPACE__ . '\save_pattern_to_file' );
Copy link
Contributor Author

@kienstra kienstra Feb 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but removes a hack I added that's not needed anymore, thanks to Phil's rest_insert_ hook.

if ( $previous_value && $previous_value !== $meta_value ) {
delete_pattern( $meta_value );
if ( $pattern_name !== $meta_value ) {
delete_pattern( $pattern_name );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the bug (which I created).

$meta_value was the new pattern name, where it should have deleted the old name.

@kienstra kienstra marked this pull request as ready for review February 20, 2023 19:07
@kienstra kienstra mentioned this pull request Feb 20, 2023
11 tasks
@kienstra
Copy link
Contributor Author

Thanks a lot, @johnstonphilip!

@kienstra kienstra merged commit 0b962bf into main Feb 21, 2023
@kienstra kienstra deleted the fix/renaming-pattern-duplication branch February 21, 2023 17:05
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants