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

Commit

Permalink
See if slashing pattern categories helps
Browse files Browse the repository at this point in the history
  • Loading branch information
kienstra committed Jun 15, 2023
1 parent 9dc9099 commit f5fcdcf
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PanelRow, TextControl } from '@wordpress/components';
import { RichText } from '@wordpress/block-editor';

import toKebabCase from '../../utils/toKebabCase';
import stripIllegalChars from '../../utils/stripIllegalChars';

import type { AdditionalSidebarProps, BaseSidebarProps } from './types';
import type { Pattern } from '../../types';
Expand All @@ -15,7 +16,7 @@ function isTitleTaken(
currentName: string,
patternNames: Array< Pattern[ 'name' ] >
) {
const newName = toKebabCase( patternTitle );
const newName = toKebabCase( stripIllegalChars( patternTitle ) );
return patternNames.includes( newName ) && newName !== currentName;
}

Expand Down Expand Up @@ -47,7 +48,7 @@ export default function TitlePanel( {
value={ title }
onChange={ ( newTitle: typeof title ) => {
editPost( { title: newTitle } );
handleChange( 'name', toKebabCase( newTitle ) );
handleChange( 'name', toKebabCase( stripIllegalChars( newTitle ) ) );

if ( ! newTitle ) {
lockPostSaving();
Expand Down
3 changes: 3 additions & 0 deletions wp-modules/editor/js/src/utils/stripIllegalChars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function stripIllegalChars( toConvert: string ) {
return toConvert.replace( /[^-\w]/g, '' )
}
1 change: 0 additions & 1 deletion wp-modules/editor/js/src/utils/toKebabCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
export default function toKebabCase( toConvert = '' ) {
return toConvert
.replace( /[_\W]+(?=\w+)/g, '-' )
.replace( /[^-\w]/g, '' )
.toLowerCase();
}
4 changes: 2 additions & 2 deletions wp-modules/pattern-data-handlers/pattern-data-handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ function create_formatted_category_registrations( $custom_categories ) {
function ( $category_label ) {
$category_name = strtolower( str_replace( ' ', '-', $category_label ) );
$text_domain = wp_get_theme()->get( 'TextDomain' );
$label_arr = $text_domain ? "[ 'label' => __( '$category_label', '$text_domain' ), 'pm_custom' => true ]" : "[ 'label' => '$category_label', , 'pm_custom' => true ]";
return "register_block_pattern_category( '$category_name', $label_arr );";
$label_arr = $text_domain ? sprintf( "[ 'label' => __( '%s', '$text_domain' ), 'pm_custom' => true ]", addslashes( $category_label ) ) : sprintf( "[ 'label' => '%s', , 'pm_custom' => true ]", addslashes( $category_label ) );
return sprintf( "register_block_pattern_category( '%s', $label_arr );", addslashes( $category_name ) );
},
$custom_categories,
)
Expand Down

0 comments on commit f5fcdcf

Please sign in to comment.