Skip to content

Commit 3b6bb45

Browse files
authored
Enhance accuracy of guessing the default plural label (#143)
1 parent 2957398 commit 3b6bb45

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

includes/manager/src/components/cpt-settings-panel.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ import {
99
import { useEntityProp } from '@wordpress/core-data';
1010
import { POST_TYPE_NAME } from '../constants';
1111

12+
function getPlural( singular ) {
13+
if ( singular.endsWith( 'y' ) ) {
14+
return `${ singular.slice( 0, -1 ) }ies`;
15+
}
16+
if ( singular.endsWith( 's' ) || singular.endsWith( 'ch' ) ) {
17+
return `${ singular }es`;
18+
}
19+
return `${ singular }s`;
20+
}
21+
1222
export const CPTSettingsPanel = function () {
1323
const [ meta, setMeta ] = useEntityProp(
1424
'postType',
@@ -27,7 +37,7 @@ export const CPTSettingsPanel = function () {
2737
useLayoutEffect( () => {
2838
if ( title !== lastTitle.current ) {
2939
lastTitle.current = title;
30-
setMeta( { ...meta, plural_label: `${ title }s` } );
40+
setMeta( { ...meta, plural_label: getPlural( title ) } );
3141
}
3242
}, [ title, meta, setMeta ] );
3343

includes/runtime/class-content-model.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function __construct( WP_Post $content_model_post ) {
111111
* @return string The plural label.
112112
*/
113113
public function get_plural_label() {
114-
return $this->get_model_meta( 'plural_label' ) ?? "{$this->title}s";
114+
return $this->get_model_meta( 'plural_label' ) ?? $this->get_plural( $this->title );
115115
}
116116

117117
/**
@@ -143,6 +143,22 @@ private function get_model_meta( $key ) {
143143
return null;
144144
}
145145

146+
/**
147+
* Attempts to get the plural label based on the given singular label.
148+
*
149+
* @param string $singular The singular label.
150+
* @return string The plural label (best guess based on common English grammar rules).
151+
*/
152+
private function get_plural( $singular ) {
153+
if ( str_ends_with( $singular, 'y' ) ) {
154+
return substr( $singular, 0, -1 ) . 'ies';
155+
}
156+
if ( str_ends_with( $singular, 's' ) || str_ends_with( $singular, 'ch' ) ) {
157+
return "{$singular}es";
158+
}
159+
return "{$singular}s";
160+
}
161+
146162
/**
147163
* Registers the custom post type for the content model.
148164
*

0 commit comments

Comments
 (0)