Skip to content

Commit

Permalink
#75 convert inflections in post type/tax names
Browse files Browse the repository at this point in the history
  • Loading branch information
Coby Tamayo committed Feb 5, 2019
1 parent 826acc8 commit c0a79f5
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/Conifer/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ public static function register_type() {

// For singular label, fallback on post type
$singular = $options['labels']['singular_name']
?? ucfirst(static::_post_type());
// convert underscore_inflection to Words Separated By Spaces
// TODO separate this into a utility method
?? implode(' ', array_map(function(string $word) {
return ucfirst($word);
}, explode('_', $name)));


// Unless there's an explicity plural_label, follow the same default logic
// as register_post_type()
Expand Down Expand Up @@ -182,13 +187,18 @@ public static function register_type() {
*/
public static function register_taxonomy(
string $name,
array $options,
array $options = [],
bool $omitPostType = false
) {
$options['labels'] = $options['labels'] ?? [];

// For singular label, fallback on taxonomy name
$singular = $options['labels']['singular_name'] ?? ucfirst($name);
$singular = $options['labels']['singular_name']
// convert underscore_inflection to Words Separated By Spaces
// TODO separate this into a utility method
?? implode(' ', array_map(function(string $word) {
return ucfirst($word);
}, explode('_', $name)));

// Unless there's an explicity plural_label, follow the same default logic
// as register_post_type()
Expand Down
73 changes: 73 additions & 0 deletions test/cases/PostRegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,79 @@ public function test_register_taxonomy() {
]));
}

public function test_register_taxonomy_without_explicit_options() {
/* https://codex.wordpress.org/Function_Reference/register_taxonomy */
WP_Mock::userFunction('register_taxonomy', [
'times' => 1,
'args' => [
'sign',
'person',
[
'labels' => [
'name' => 'Signs',
'singular_name' => 'Sign',
'menu_name' => 'Signs',
'all_items' => 'All Astrological Signs',
'edit_item' => 'Edit Sign',
'view_item' => 'View Sign',
'update_item' => 'Update Sign',
'add_new_item' => 'Add New Sign',
'new_item_name' => 'New Sign Name',
'parent_item' => 'Parent Sign',
'parent_item_colon' => 'Parent Sign:',
'search_items' => 'Search Signs',
'popular_items' => 'Popular Signs',
'separate_items_with_commas' => 'Separate Signs with commas',
'add_or_remove_items' => 'Add or remove Signs',
'choose_from_most_used' => 'Choose from the most used Signs',
'not_found' => 'No Signs found',
'back_to_items' => '← Back to Signs',
],
],
],
]);

$this->assertNull(Person::register_taxonomy('sign', [
'plural_label' => 'Signs',
'labels' => ['all_items' => 'All Astrological Signs'],
]));
}

public function test_register_taxonomy_with_underscores() {
/* https://codex.wordpress.org/Function_Reference/register_taxonomy */
WP_Mock::userFunction('register_taxonomy', [
'times' => 1,
'args' => [
'personal_attribute',
'person',
[
'labels' => [
'name' => 'Personal Attributes',
'singular_name' => 'Personal Attribute',
'menu_name' => 'Personal Attributes',
'all_items' => 'All Personal Attributes',
'edit_item' => 'Edit Personal Attribute',
'view_item' => 'View Personal Attribute',
'update_item' => 'Update Personal Attribute',
'add_new_item' => 'Add New Personal Attribute',
'new_item_name' => 'New Personal Attribute Name',
'parent_item' => 'Parent Personal Attribute',
'parent_item_colon' => 'Parent Personal Attribute:',
'search_items' => 'Search Personal Attributes',
'popular_items' => 'Popular Personal Attributes',
'separate_items_with_commas' => 'Separate Personal Attributes with commas',
'add_or_remove_items' => 'Add or remove Personal Attributes',
'choose_from_most_used' => 'Choose from the most used Personal Attributes',
'not_found' => 'No Personal Attributes found',
'back_to_items' => '← Back to Personal Attributes',
],
],
],
]);

$this->assertNull(Person::register_taxonomy('personal_attribute'));
}

public function test_register_taxonomy_omitting_post_type() {
WP_Mock::userFunction('register_taxonomy', [
'times' => 1,
Expand Down

0 comments on commit c0a79f5

Please sign in to comment.