From c0a79f59e1e937ba2ef4a973406e85d1e8834a16 Mon Sep 17 00:00:00 2001 From: Coby Tamayo Date: Mon, 4 Feb 2019 22:03:57 -0800 Subject: [PATCH] #75 convert inflections in post type/tax names --- lib/Conifer/Post/Post.php | 16 +++++-- test/cases/PostRegistrationTest.php | 73 +++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/lib/Conifer/Post/Post.php b/lib/Conifer/Post/Post.php index f745a81..e5dabb4 100644 --- a/lib/Conifer/Post/Post.php +++ b/lib/Conifer/Post/Post.php @@ -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() @@ -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() diff --git a/test/cases/PostRegistrationTest.php b/test/cases/PostRegistrationTest.php index 614531c..3caaf19 100644 --- a/test/cases/PostRegistrationTest.php +++ b/test/cases/PostRegistrationTest.php @@ -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,