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

Prepend the textdomain to the slug #180

Merged
merged 27 commits into from
May 24, 2023

Conversation

kienstra
Copy link
Contributor

@kienstra kienstra commented May 18, 2023

Summary of changes

An addition to #175

Prepends the texdomain to the slug. This will only apply on changing the title, or creating a new pattern.

All PM patterns created before this PR still won't have a textdomain prepended to the slug, unless they edit the title.

Before

* Slug: my-new-pattern

After

* Slug: twentytwentythree/my-new-pattern

How to test

  1. Create a new pattern
  2. Don't change anything, just publish it (click 'Create Pattern')
  3. Look at the pattern's PHP file
  4. Expected: The slug is <textdomain>/my-new-pattern*
<?php
/**
 * Title: My New Pattern 4
 * Slug: twentytwentythree/my-new-pattern-4
 * Description: 

@kienstra kienstra changed the base branch from main to update/patternception-slugs May 18, 2023 18:00
@kienstra kienstra changed the title Prepend a slug with the textdomain Prepend the textdomain to the slug May 18, 2023
@kienstra kienstra requested a review from mike-day May 18, 2023 18:27
@kienstra kienstra marked this pull request as ready for review May 18, 2023 18:28
@kienstra
Copy link
Contributor Author

kienstra commented May 18, 2023

If you agree with this, your call when we release it.

No need to release it in 0.2.0.

@kienstra kienstra requested a review from dreamwhisper May 18, 2023 18:29
@kienstra kienstra requested a review from johnstonphilip May 18, 2023 18:29
Copy link
Contributor

@mike-day mike-day left a comment

Choose a reason for hiding this comment

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

Great work, @kienstra! I think this would be a nice companion to #175 and #177, and it helps encourage best practices for PM users when creating pattern files.


Before updating the title, the slug had no prepended text domain:

Screenshot 2023-05-18 at 3 20 13 PM

After updating the title, the text domain is prepended:

Screenshot 2023-05-18 at 3 20 22 PM

@kienstra
Copy link
Contributor Author

kienstra commented May 19, 2023

@mike-day,
Thanks for your review, and for your idea to do this.

You put it very well:

…it helps encourage best practices for PM users when creating pattern files.

Copy link
Contributor

@dreamwhisper dreamwhisper left a comment

Choose a reason for hiding this comment

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

Looks good!
Screenshot 2023-05-19 at 2 19 46 PM

@kienstra
Copy link
Contributor Author

Hi @dreamwhisper,
Thanks a lot for testing this!

@kienstra
Copy link
Contributor Author

If it's alright, let's release this in the upcoming 0.2.0 release, with #175

Base automatically changed from update/patternception-slugs to update/patternception-saving-bug May 23, 2023 18:00
@johnstonphilip
Copy link
Contributor

@kienstra It looks like a forward slash is getting left in the slug after this change. At least, that's what the failed test is showing. I'm not sure if we need to strip that out?

@kienstra
Copy link
Contributor Author

Ah…let me see

// 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'] );
Copy link
Contributor Author

@kienstra kienstra May 23, 2023

Choose a reason for hiding this comment

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

$pattern['slug'] was '/new-pattern-originally-created-with-pattern-manager' because prepend_textdomain() added the /.

The unit tests don't have a theme textdomain, so prepend_textdomain() only prefixed / to it, instead of a textdomain.

Now, it doesn't prefix a textdomain if there is no textdomain.

function prepend_textdomain( $name ) {
return implode(
'/',
array_filter( [ wp_get_theme()->get( 'TextDomain' ), $name ] )
Copy link
Contributor Author

@kienstra kienstra May 23, 2023

Choose a reason for hiding this comment

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

Filters out an empty textdomain or $name.

Copy link
Contributor Author

@kienstra kienstra May 23, 2023

Choose a reason for hiding this comment

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

There's both a textdomain and pattern name: yourtheme/foo-pattern-name
There's no textdomain: foo-pattern-name
There's no pattern name (shouldn't happen): yourtheme

@kienstra
Copy link
Contributor Author

kienstra commented May 23, 2023

Hi @johnstonphilip,
How does this look now? Here's the fix.

@@ -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';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hope it's OK I changed this, as it's not only the slug.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you elaborate on that? What is it other than the slug?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's also the filename (the filename is saved as the ->post_name, where the pattern slug is never in the WP_Post).

Happy to revert this, it's a nitpick of mine.

public function test_prepend_textdomain() {
$this->assertStringEndsWith(
'my-new-pattern',
prepend_textdomain( 'my-new-pattern' )
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not a complete test, but it's hard to mock wp_get_theme() in prepend_textdomain(), to mock the textdomain being empty.

@kienstra
Copy link
Contributor Author

Thanks a lot, @johnstonphilip! Happy to revert #180 (comment) if you'd like.

@kienstra kienstra merged commit ac955a1 into update/patternception-saving-bug May 24, 2023
@kienstra kienstra deleted the add/slug-prefixing branch May 24, 2023 16:57
kienstra added a commit that referenced this pull request Jun 1, 2023
…#185)

* Add a naive function to update a slug

* Use preg_replace() to update the slug

* Fix the conversion, using addslashes

* Add a helper to get whether a pattern has a pattern block

* Add a function to update all patterns

* Prepend the textdomain to the slug (#180)

* Add a naive function to update a slug

* Handle multiple attributes in pattern block

* Only output the name and textdomain if they exist

---------

Co-authored-by: Phil Johnston <phil.johnston@wpengine.com>
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.

4 participants