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

Commit

Permalink
Prepend the textdomain to the slug (#180)
Browse files Browse the repository at this point in the history
* Add a naive function to update a slug

* Handle multiple attributes in pattern block

* Only output the name and textdomain if they exist
  • Loading branch information
kienstra authored May 24, 2023
1 parent 621d151 commit ac955a1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
"test:js": "cd ../../wpps-scripts; sh test-js.sh $npm_package_wpps_options -p \"${OLDPWD}\";",
"zip": "cd ../../wpps-scripts; sh zip.sh $npm_package_wpps_options -p \"${OLDPWD}\";"
}
}
}
10 changes: 6 additions & 4 deletions wp-modules/editor/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function save_pattern_to_file( WP_Post $post ) {
array_merge(
// Only set the slug to the name for new patterns.
// Patterns created without PM might have a different slug and name.
$pattern ? $pattern : [ 'slug' => $name ],
$pattern ? $pattern : [ 'slug' => prepend_textdomain( $name ) ],
[
'content' => $post->post_content,
'name' => $name,
Expand Down Expand Up @@ -122,19 +122,21 @@ function save_metadata_to_pattern_file( $override, $post_id, $meta_key, $meta_va
);
}

$slug = prepend_textdomain( $name_changed ? $meta_value : $pattern_name );

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

return update_pattern(
array_merge(
get_pattern_defaults(),
$pattern ? $pattern : [
'title' => $post->post_title,
'slug' => $pattern_name,
'slug' => $slug,
],
$name_changed ? [ 'slug' => $meta_value ] : [],
$name_changed ? [ 'slug' => $slug ] : [],
[
'name' => $pattern_name,
$meta_key => $meta_value,
Expand Down
8 changes: 4 additions & 4 deletions wp-modules/editor/tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public function test_new_pattern_title_matches_slug() {
// Get the contents of the file that was saved.
$pattern = \PatternManager\PatternDataHandlers\get_pattern_by_name( $post->post_name );

// Make sure the post_name (aka "slug") of the post and the slug in the file match.
$this->assertSame( $post->post_name, $pattern['slug'] );
// Make sure the ->post_name is the same as the slug, other than the prefixed textdomain.
$this->assertStringEndsWith( $post->post_name, $pattern['slug'] );

// Make sure the post_name (aka "slug") of the post and the filename match.
$this->assertSame( $post->post_name, $pattern['name'] );
Expand All @@ -128,7 +128,7 @@ public function test_slug_and_filename_stay_the_same_after_content_update() {
$post->post_title = 'This title remains the same after content changes.';
$post->post_content = 'test pattern content';
$post->post_status = 'publish';
$post->post_name = 'this-slug-remains-the-same-after-content-changes';
$post->post_name = 'remains-the-same-after-content-changes';
$post->post_type = get_pattern_post_type();
$post->filter = 'raw';

Expand Down Expand Up @@ -172,7 +172,7 @@ public function test_slug_and_filename_stay_the_same_after_content_update() {
$content_modified_pattern = \PatternManager\PatternDataHandlers\get_pattern_by_name( 'mismatched-name' );

// Make sure the slug does not get changed when only the content is modified.
$this->assertSame( 'this-slug-remains-the-same-after-content-changes', $content_modified_pattern['slug'] );
$this->assertStringEndsWith( 'remains-the-same-after-content-changes', $content_modified_pattern['slug'] );

// Make sure the title does not get changed when only the content is modified.
$this->assertSame( 'This title remains the same after content changes.', $content_modified_pattern['title'] );
Expand Down
10 changes: 10 additions & 0 deletions wp-modules/editor/tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,14 @@ public function test_has_pattern_block( $content, $expected ) {
has_pattern_block( $content )
);
}

/**
* Tests prepend_textdomain.
*/
public function test_prepend_textdomain() {
$this->assertStringEndsWith(
'my-new-pattern',
prepend_textdomain( 'my-new-pattern' )
);
}
}
16 changes: 15 additions & 1 deletion wp-modules/editor/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,25 @@ function update_slug( $old_slug, $new_slug, $subject ) {
* Gets whether content has a Pattern Block.
*
* @param string $content The content to examine.
* @param bool Whether the content has a pattern block.
* @return bool Whether the content has a pattern block.
*/
function has_pattern_block( $content ) {
return 1 === preg_match(
'#<!--\s+wp:pattern\s+{[^}]*"slug":"#',
$content
);
}

/**
* Prepends the theme textdomain to a pattern name.
* <textdomain>/<pattern-name>
*
* @param string $name The pattern name.
* @return string
*/
function prepend_textdomain( $name ) {
return implode(
'/',
array_filter( [ wp_get_theme()->get( 'TextDomain' ), $name ] )
);
}

0 comments on commit ac955a1

Please sign in to comment.