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

See if slashing new categories helps #200

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,10 @@ 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( /[^\s\w_-]/g, '' );
}
4 changes: 1 addition & 3 deletions wp-modules/editor/js/src/utils/toKebabCase.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/** Converts a string to a kebab-case. */
export default function toKebabCase( toConvert = '' ) {
return toConvert
.replace( /[_\W]+(?=\w+)/g, '-' )
Copy link
Contributor Author

@kienstra kienstra Jun 16, 2023

Choose a reason for hiding this comment

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

Because this stripped all non-word characters, it caused a category with an apostrophe to be saved without the apostrophe, and therefore never appear in the categories react-select.

This issue appeared because with the PHP edits below, categories can now have an apostrophe.

Copy link
Contributor Author

@kienstra kienstra Jun 16, 2023

Choose a reason for hiding this comment

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

Here's what it looked like without this fix in toKebabCase.ts:

<?php
/**
 * Title: My New Pattern 2
 * Slug: altitude-pro/my-new-pattern-2
 * Description: 
 * Categories: you-re-the-best   <-- Saved without the apostrophe, it was stripped in CategoriesPanel.ts#L62
 * Keywords: 
 * Viewport Width: 1280
 * Block Types: 
 * Post Types: 
 * Inserter: true
 * Custom Categories: You're The Best
 */
// Category name and label have the apostrophe
register_block_pattern_category( 'you\'re-the-best', [ 'label' => __( 'You\'re The Best', 'altitude-pro' ), 'pm_custom' => true ] ); 
?>

.replace( /[^-\w]/g, '' )
return toConvert.replace( /[\s_]+/g, '-' )
.toLowerCase();
}
7 changes: 4 additions & 3 deletions wp-modules/pattern-data-handlers/pattern-data-handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,10 @@ function create_formatted_category_registrations( $custom_categories ) {
"\n",
array_map(
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 ]";
$slashed_category_label = addslashes( $category_label );
$category_name = strtolower( str_replace( ' ', '-', addslashes( $category_label ) ) );
$text_domain = wp_get_theme()->get( 'TextDomain' );
$label_arr = $text_domain ? "[ 'label' => __( '$slashed_category_label', '$text_domain' ), 'pm_custom' => true ]" : "[ 'label' => '$slashed_category_label', , 'pm_custom' => true ]";
return "register_block_pattern_category( '$category_name', $label_arr );";
},
$custom_categories,
Expand Down