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

Commit

Permalink
Update a Patternception slug in all patterns on changing a slug (#175)
Browse files Browse the repository at this point in the history
* Add a naive function to update a slug

* Use preg_replace() to update the slug
  • Loading branch information
kienstra authored May 23, 2023
1 parent 5c07334 commit 621d151
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 0 deletions.
1 change: 1 addition & 0 deletions wp-modules/editor/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ function save_metadata_to_pattern_file( $override, $post_id, $meta_key, $meta_va

if ( $name_changed ) {
delete_pattern( $pattern_name );
update_pattern_slugs( $pattern['slug'], $meta_value );
}

return update_pattern(
Expand Down
114 changes: 114 additions & 0 deletions wp-modules/editor/tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,118 @@ function wp_safe_redirect() {}
get_pm_post_ids()
);
}

/**
* Gets the data for the test of update_slug().
*
* @return array[]
*/
public function data_update_slug() {
return [
[
'',
'',
'<!-- wp:paragraph --><p>This is a paragraph block</p><!-- /wp:paragraph -->',
'<!-- wp:paragraph --><p>This is a paragraph block</p><!-- /wp:paragraph -->',
],
[
'baz',
'new',
' ',
' ',
],
[
'baz',
'new',
'<!-- wp:pattern {"slug":"foo/an-example-pattern"} /-->',
'<!-- wp:pattern {"slug":"foo/an-example-pattern"} /-->',
],
[
'foo/an-example-pattern',
'foo/renamed',
'<!-- wp:pattern {"slug":"foo/an-example-pattern"} /-->',
'<!-- wp:pattern {"slug":"foo/renamed"} /-->',
],
[
'foo/404',
'404-not-found',
'<!-- wp:pattern {"slug":"foo/404"} /-->',
'<!-- wp:pattern {"slug":"404-not-found"} /-->',
],
[
'foo/an-example-pattern',
'renamed',
'<!-- wp:pattern {"slug":"foo/an-example-pattern"} /-->',
'<!-- wp:pattern {"slug":"renamed"} /-->',
],
[
'foo/an-example-pattern',
'foo/renamed',
'<!-- wp:pattern {"syncStatus":"full","slug":"foo/an-example-pattern"} /--><!-- wp:pattern {"slug":"foo/another"} /-->',
'<!-- wp:pattern {"syncStatus":"full","slug":"foo/renamed"} /--><!-- wp:pattern {"slug":"foo/another"} /-->',
],
[
'foo/an-example-pattern',
'foo/renamed-pattern',
'<!-- wp:pattern {"syncStatus":"full","slug":"foo/an-example-pattern"} /--><!-- wp:pattern {"slug":"foo/an-example-pattern"} /-->',
'<!-- wp:pattern {"syncStatus":"full","slug":"foo/renamed-pattern"} /--><!-- wp:pattern {"slug":"foo/renamed-pattern"} /-->',
],
[
'foo/an-example-pattern',
'foo/renamed-pattern',
'<!-- wp:pattern {"slug":"foo/an-example-pattern"} /--><!-- wp:pattern {"slug":"foo/an-example-pattern"} /-->',
'<!-- wp:pattern {"slug":"foo/renamed-pattern"} /--><!-- wp:pattern {"slug":"foo/renamed-pattern"} /-->',
],
];
}

/**
* Tests update_slug.
*
* @dataProvider data_update_slug
*/
public function test_update_slug( $old_slug, $new_slug, $subject, $expected ) {
$this->assertSame(
$expected,
update_slug( $old_slug, $new_slug, $subject )
);
}

/**
* Gets the data for the test of has_pattern_block().
*
* @return array[]
*/
public function data_has_pattern_block() {
return [
[
'',
false,
],
[
'<!-- wp:paragraph --><p>This is a paragraph block</p><!-- /wp:paragraph -->',
false,
],
[
'<!-- wp:pattern {"slug":"foo/an-example-pattern"} /-->',
true,
],
[
'<!-- wp:pattern {"syncStatus":"full","slug":"foo/an-example-pattern"} /-->',
true,
],
];
}

/**
* Tests has_pattern_block.
*
* @dataProvider data_has_pattern_block
*/
public function test_has_pattern_block( $content, $expected ) {
$this->assertSame(
$expected,
has_pattern_block( $content )
);
}
}
54 changes: 54 additions & 0 deletions wp-modules/editor/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,57 @@ function edit_pattern( string $pattern_name ) {
)
);
}

/**
* Updates pattern slugs in Pattern Blocks.
*
* If a pattern changes slugs,
* and a Pattern Block references its old slug,
* it won't render.
*
* @param string $old_slug The previous slug.
* @param string $new_slug The new slug.
*/
function update_pattern_slugs( $old_slug, $new_slug ) {
$patterns = get_theme_patterns();

foreach ( $patterns as $pattern_name => $pattern ) {
if ( has_pattern_block( $pattern['content'] ) ) {
update_pattern(
array_merge(
$pattern,
[ 'content' => update_slug( $old_slug, $new_slug, $pattern['content'] ) ],
)
);
}
}
}

/**
* Updates a slug in a Pattern Block to a new slug.
*
* @param string $old_slug The previous slug.
* @param string $new_slug The new slug.
* @param string $subject What to replace the slug in.
* @return string With the updated slug.
*/
function update_slug( $old_slug, $new_slug, $subject ) {
return preg_replace(
'#(<!--\s+wp:pattern\s+{[^}]*"slug":")(' . $old_slug . ')(")#',
'${1}' . $new_slug . '${3}',
$subject
);
}

/**
* Gets whether content has a Pattern Block.
*
* @param string $content The content to examine.
* @param bool Whether the content has a pattern block.
*/
function has_pattern_block( $content ) {
return 1 === preg_match(
'#<!--\s+wp:pattern\s+{[^}]*"slug":"#',
$content
);
}

0 comments on commit 621d151

Please sign in to comment.