-
-
Notifications
You must be signed in to change notification settings - Fork 31
Improve support for CPT and CTT #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
01fbea7
a6ba2fd
ed6a8e0
ac55a86
e47f394
5e6dc10
927e08f
f84eef6
4282bad
43d710f
ce84d62
b8ae22a
4df1c4d
8bcd29a
794b8e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -573,11 +573,12 @@ namespace Bones\Traits { | |
| * @param string $str The question to ask | ||
| * @param string|null $default The default value | ||
| */ | ||
| protected function ask(string $str, ?string $default = ''): string | ||
| protected function ask(string $str, ?string $default = '',?string $example = ''): string | ||
| { | ||
|
|
||
| $str = WPBONES_COLOR_GREEN . | ||
| "❓ $str" . | ||
| (empty($example) ? '' : " ie: {$example}") . | ||
| (empty($default) ? ': ' : " (default: {$default}): ") . | ||
| WPBONES_COLOR_RESET; | ||
|
|
||
|
|
@@ -589,6 +590,33 @@ namespace Bones\Traits { | |
|
|
||
| return $line ?: $default; | ||
| } | ||
| /** | ||
| * Get input from console | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe the doc need updating |
||
| * | ||
| * @param string $str The question to ask | ||
| * @param string|null $default The default value | ||
| */ | ||
| protected function askYesNo(string $str, bool $default = true): string | ||
| { | ||
|
|
||
| $str = WPBONES_COLOR_GREEN . | ||
| "❓ $str (".($default?"Y/n":"y/N").")" . | ||
| " (default: ".($default?"Yes":"No")."): " . | ||
| WPBONES_COLOR_RESET; | ||
|
|
||
| // Use readline to get the user input | ||
| $line = readline($str); | ||
|
|
||
| // Trim the input to remove extra spaces or newlines | ||
| $line = strtoupper(trim($line)); | ||
| if($line=="Y"||$line=="YES"){ | ||
| return "true"; | ||
| }else if($line=="N"||$line=="NO"){ | ||
| return "false"; | ||
| }else{ | ||
| return $default?"true":"false"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -2508,6 +2536,47 @@ namespace Bones { | |
| $this->line(' Created plugin/Console/Kernel.php'); | ||
| } | ||
| } | ||
| protected function simplePluralize($word) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. miss doc |
||
| $strLength = strlen($word); | ||
| $last = strtolower($word[$strLength - 1]); | ||
| $secondLast = $strLength > 1 ? strtolower($word[$strLength - 2]) : ''; | ||
|
|
||
| // Special cases (irregulars) | ||
| $irregular = [ | ||
| 'child' => 'children', | ||
| 'man' => 'men', | ||
| 'woman' => 'women', | ||
| 'tooth' => 'teeth', | ||
| 'foot' => 'feet', | ||
| 'mouse' => 'mice', | ||
| 'goose' => 'geese', | ||
| 'person' => 'people', | ||
| ]; | ||
|
|
||
| if (array_key_exists(strtolower($word), $irregular)) { | ||
| return $irregular[strtolower($word)]; | ||
| } | ||
|
|
||
| // common words | ||
| if ($last === 's' || $last === 'x' || $last === 'z' || ($secondLast . $last) === 'ch' || ($secondLast . $last) === 'sh') { | ||
| return $word . 'es'; | ||
| } | ||
|
|
||
| if ($last === 'y' && !in_array($secondLast, ['a', 'e', 'i', 'o', 'u'])) { | ||
| return substr($word, 0, $strLength - 1) . 'ies'; | ||
| } | ||
|
|
||
| if ($last === 'f' || ($secondLast . $last) === 'fe') { | ||
| $base = substr($word, 0, $strLength - ($last === 'f' ? 1 : 2)); | ||
| return $base . 'ves'; | ||
| } | ||
|
|
||
| if ($last === 'o' && in_array($secondLast, ['a', 'e', 'i', 'o', 'u']) === false) { | ||
| return $word . 'es'; | ||
| } | ||
|
|
||
| return $word . 's'; | ||
| } | ||
|
|
||
| /** | ||
| * Create a Custom Post Type controller | ||
|
|
@@ -2523,30 +2592,42 @@ namespace Bones { | |
| } | ||
|
|
||
| // ask className if empty | ||
| $className = $this->askClassNameIfEmpty($className); | ||
|
|
||
| $filename = sprintf('%s.php', $className); | ||
|
|
||
| $className = $this->askClassNameIfEmpty($className); | ||
| $filename = sprintf('%s.php', $className); | ||
| // current plugin name and namespace | ||
| [$pluginName, $namespace] = $this->getPluginNameAndNamespace(); | ||
|
|
||
| $slug = str_replace('-', '_', $this->sanitize($pluginName)); | ||
|
|
||
| $id = $this->ask('Enter a ID', $slug); | ||
| $name = $this->ask('Enter the name'); | ||
| $plural = $this->ask('Enter the plural name'); | ||
|
|
||
| $className = ucfirst($this->sanitize($className)); | ||
| $name = ($className); | ||
| $plural = ucfirst($this->simplePluralize($className)); | ||
| $slug = $this->slugify($namespace,$plural); | ||
| $id = $this->ask('Enter a ID', $slug); | ||
| $name = $this->ask('Enter the name',$name); | ||
| $plural = $this->ask('Enter the plural name',$plural); | ||
|
|
||
| $showInRest = true; | ||
| $showInRest = $this->askYesNo('Show in Rest?',$showInRest); | ||
|
|
||
| $showUI = true; | ||
| $showUI = $this->askYesNo('Show in admin sidebar?',$showUI); | ||
|
|
||
| $supports = 'title, editor, thumbnail, excerpt'; | ||
| $supports = $this->ask('Features available for the CPT',$supports); | ||
| $itemsSupport = $this->stringToArray($supports); | ||
|
|
||
| if (empty($id)) { | ||
| $id = $slug; | ||
| } | ||
|
|
||
| // stubbing | ||
| $content = $this->prepareStub('cpt', [ | ||
| '{Namespace}' => $namespace, | ||
| '{ClassName}' => $className, | ||
| '{ID}' => $id, | ||
| '{Name}' => $name, | ||
| '{Plural}' => $plural, | ||
| '{Namespace}' => $namespace, | ||
| '{ClassName}' => $className, | ||
| '{ID}' => $id, | ||
| '{Name}' => $name, | ||
| '{Plural}' => $plural, | ||
| '{ShowInRest}' => $showInRest, | ||
| '{showUI}' => $showUI, | ||
| '{Supports}' => "'".join("','",$itemsSupport)."'", | ||
| ]); | ||
|
|
||
| // Create the folder if it doesn't exist | ||
|
|
@@ -2559,9 +2640,28 @@ namespace Bones { | |
| $this->line(" Created plugin/CustomPostTypes/{$filename}"); | ||
|
|
||
| $this->info( | ||
| "Remember to add {$className} in the config/plugin.php array in the 'custom_post_types' key." | ||
| "Remember to add \\{$namespace}\CustomPostTypes\\{$className} in the config/plugin.php array in the 'custom_post_types' key." | ||
| ); | ||
| } | ||
| private function stringToArray($content){ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. miss doc
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do you think to use this version private function stringToArray($content) {
// Split the string by commas and trim whitespace from each item
$items = array_map('trim', explode(",", $content));
// Filter out any empty items
return array_filter($items, fn($item) => !empty($item));
} |
||
| $itemsSupport=[] ; | ||
| foreach(explode(",",$content) as $item) | ||
| { | ||
| if(!empty($item)) | ||
| $itemsSupport[]=$item; | ||
| } | ||
| return $itemsSupport; | ||
| } | ||
| private function slugify($namespace,$name,$remove=0){ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. miss doc
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What exactly is the purpose of this function? |
||
|
|
||
| $subNamespace = substr($namespace,0,20-$remove-strlen($name)-1); | ||
| $slug = strtolower($subNamespace.(empty($namespace)?"":"_").str_replace('-', '_', $name)); | ||
| $length = strlen($slug); | ||
| if($length>20){ | ||
| return $this->slugify($namespace,$name,$length-20); | ||
| } | ||
| return $slug; | ||
| } | ||
|
|
||
| /** | ||
| * Create a Shortcode controller | ||
|
|
@@ -2739,25 +2839,30 @@ namespace Bones { | |
| } | ||
|
|
||
| // ask className if empty | ||
| $className = $this->askClassNameIfEmpty($className); | ||
|
|
||
| $filename = sprintf('%s.php', $className); | ||
| $className = $this->askClassNameIfEmpty($className); | ||
| $filename = sprintf('%s.php', $className); | ||
|
|
||
| // current plugin name and namespace | ||
| $namespace = $this->getNamespace(); | ||
|
|
||
| $slug = $this->getPluginId(); | ||
|
|
||
| $id = $this->ask('Enter a ID', $slug); | ||
| $name = $this->ask('Enter the name'); | ||
| $plural = $this->ask('Enter the plural name'); | ||
|
|
||
| $namespace = $this->getNamespace(); | ||
| // current plugin name and namespace | ||
| $className = ucfirst($this->sanitize($className)); | ||
| $name = $className; | ||
| $plural = ucfirst($this->simplePluralize($className)); | ||
| $slug = $this->slugify("",$plural); | ||
| $id = $this->ask('Enter a ID', $slug); | ||
| $name = $this->ask('Enter the name',$name); | ||
| $plural = $this->ask('Enter the plural name',$plural); | ||
| $showInRest = true; | ||
| $showInRest = $this->askYesNo('Show in Rest?',$showInRest); | ||
|
|
||
| $this->line( | ||
| 'The object type below refers to the id of your previous Custom Post Type' | ||
| ); | ||
|
|
||
| $objectType = $this->ask('Enter the object type to bound'); | ||
|
|
||
| $objectType ="post"; | ||
| $example ="post, your_custom_post_type, etc"; | ||
| $objectType = $this->ask('Enter the object type to bound',$objectType,$example); | ||
| $objectType = $this->stringToArray($objectType); | ||
|
|
||
| if (empty($id)) { | ||
| $id = $slug; | ||
| } | ||
|
|
@@ -2768,8 +2873,9 @@ namespace Bones { | |
| '{ClassName}' => $className, | ||
| '{ID}' => $id, | ||
| '{Name}' => $name, | ||
| '{ShowInRest}' => $showInRest, | ||
| '{Plural}' => $plural, | ||
| '{ObjectType}' => $objectType, | ||
| '{ObjectType}' => "'".join("','",$objectType)."'", | ||
| ]); | ||
|
|
||
| // Create the folder if it doesn't exist | ||
|
|
@@ -2782,7 +2888,7 @@ namespace Bones { | |
| $this->line(" Created plugin/CustomTaxonomyTypes/{$filename}"); | ||
|
|
||
| $this->info( | ||
| "Remember to add {$className} in the config/plugin.php array in the 'custom_taxonomy_types' key." | ||
| "Remember to add \\{$namespace}\CustomTaxonomyTypes\\{$className} in the config/plugin.php array in the 'custom_taxonomy_types' key." | ||
| ); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add the new optional $example param in the function docs
@param string|null $example