Skip to content

Commit

Permalink
Template save and format slugify: Use remove_accents() to convert all…
Browse files Browse the repository at this point in the history
… accent characters to ASCII characters, before creating slug with sanitize_title_with_dashes()

- Resolves #96
- See https://developer.wordpress.org/reference/functions/remove_accents/
  • Loading branch information
eliot-akira committed Mar 22, 2024
1 parent 782a1c4 commit a7af4a1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
17 changes: 9 additions & 8 deletions admin/template-post/save.php
Expand Up @@ -87,10 +87,10 @@

$post = get_post( $result );

$post_name = sanitize_title_with_dashes(
$post_name = sanitize_title_with_dashes(remove_accents(
! empty( $name ) ? $name
: ( ! empty( $title ) ? $title : 'no-title' ), '', 'save'
);
: ( ! empty( $title ) ? $title : 'no-title' )
), '', 'save');

$plugin->save_unique_template_post_slug( $post, $post_name );

Expand All @@ -115,11 +115,12 @@

// Update template slug

$post_name = sanitize_title_with_dashes( ! empty( $_POST['name'] ) ? $_POST['name']
: ( ! empty( $post->post_title ) ? $post->post_title
: 'no-title'
), '', 'save'
);
$post_name = sanitize_title_with_dashes(remove_accents(
! empty( $_POST['name'] ) ? $_POST['name']
: ( ! empty( $post->post_title ) ? $post->post_title
: 'no-title'
)
), '', 'save');

$plugin->save_unique_template_post_slug( $post, $post_name );

Expand Down
2 changes: 1 addition & 1 deletion framework/format/index.php
Expand Up @@ -110,7 +110,7 @@ function php_to_js_keys($src, $options = []) {
* @return array Converted string
*/
function slugify($string) {
return sanitize_title_with_dashes($string, null, 'save');
return sanitize_title_with_dashes(remove_accents($string), null, 'save');
};

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/admin/template-save.php
@@ -0,0 +1,37 @@
<?php
namespace Tests\Admin;
use tangible\template_system;
use tangible\format;

class Template_Save_TestCase extends \WP_UnitTestCase {

/**
* Generate template slug from title
*
* @see /admin/template-post/save.php
*/
function test_template_slug() {

$plugin = tangible_template_system();

$title = 'Test - éùæøŸ';

$post_id = self::factory()->post->create_object([
'post_type' => 'post',
'post_status' => 'publish',
'post_title' => $title,
// Important: wp_insert_post() throws error if these are not defined
'post_content' => '',
'post_excerpt' => '',
]);

$expected = 'test-euaeoy';

$this->assertEquals($expected, format\slugify($title));

$post = get_post($post_id);

$this->assertEquals($expected, $post->post_name);

}
}

0 comments on commit a7af4a1

Please sign in to comment.