Skip to content
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

Improve support for CPT and CTT #61

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
174 changes: 140 additions & 34 deletions src/Console/bin/bones
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -589,6 +590,33 @@ namespace Bones\Traits {

return $line ?: $default;
}
/**
* Get input from console
*
* @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";
}
}
}
}

Expand Down Expand Up @@ -2508,6 +2536,47 @@ namespace Bones {
$this->line(' Created plugin/Console/Kernel.php');
}
}
protected function simplePluralize($word) {
$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
Expand All @@ -2523,30 +2592,42 @@ namespace Bones {
}

// ask className if empty
$className = $this->askClassNameIfEmpty($className);

$filename = sprintf('%s.php', $className);

$className = $this->askClassNameIfEmpty($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));
$filename = sprintf('%s.php', $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
Expand All @@ -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){
$itemsSupport=[] ;
foreach(explode(",",$content) as $item)
{
if(!empty($item))
$itemsSupport[]=trim($item);
}
return $itemsSupport;
}
private function slugify($namespace,$name,$remove=0){

$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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -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."
);
}

Expand Down
10 changes: 7 additions & 3 deletions src/Console/stubs/cpt.stub
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ use {Namespace}\WPBones\Foundation\WordPressCustomPostTypeServiceProvider as Ser
class {ClassName} extends ServiceProvider
{

protected $id = '{ID}';
protected $name = '{Name}';
protected $plural = '{Plural}';
protected $id = '{ID}';
protected $name = '{Name}';
protected $plural = '{Plural}';
protected $showInRest = {ShowInRest};
protected $showUI = {showUI};
protected $supports = [{Supports}];
protected $menuIcon = "dashicons-admin-post";

/**
* You may override this method in order to register your own actions and filters.
Expand Down
3 changes: 2 additions & 1 deletion src/Console/stubs/ctt.stub
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class {ClassName} extends ServiceProvider
protected $id = '{ID}';
protected $name = '{Name}';
protected $plural = '{Plural}';
protected $objectType = '{ObjectType}';
protected $showInRest = {ShowInRest};
protected $objectType = [{ObjectType}];

/**
* You may override this method in order to register your own actions and filters.
Expand Down
55 changes: 40 additions & 15 deletions src/Foundation/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class Plugin extends Container implements PluginContract
* @var string
*/
public $slug = '';
/**
* Array of Menu Relations generated by CTT
* @var array
*/
public $menuRelations= [];

/**
* Build in __FILE__ relative plugin.
Expand Down Expand Up @@ -265,43 +270,63 @@ public function _init()
// Custom post types Service Provider
$custom_post_types = $this->config('plugin.custom_post_types', []);
foreach ($custom_post_types as $className) {
$object = new $className($this);
$object->register();
$this->provides[$className] = $object;
try{
$object = new $className($this);
$object->register();
$this->provides[$className] = $object;
}catch(\Throwable $e){
error_log("missing class ".$className);
}
}

// Custom taxonomy type Service Provider
$custom_taxonomy_types = $this->config('plugin.custom_taxonomy_types', []);
foreach ($custom_taxonomy_types as $className) {
$object = new $className($this);
$object->register();
$this->provides[$className] = $object;
try{
$object = new $className($this);
$object->register();
$this->provides[$className] = $object;
}catch(\Throwable $e){
error_log("missing class ".$className);
}
}

// Shortcodes Service Provider
$shortcodes = $this->config('plugin.shortcodes', []);
foreach ($shortcodes as $className) {
$object = new $className($this);
$object->register();
$this->provides[$className] = $object;
try{
$object = new $className($this);
$object->register();
$this->provides[$className] = $object;
}catch(\Throwable $e){
error_log("missing class ".$className);
}
}

// Ajax Service Provider
if ($this->isAjax()) {
$ajax = $this->config('plugin.ajax', []);
foreach ($ajax as $className) {
$object = new $className($this);
$object->register();
$this->provides[$className] = $object;
try{
$object = new $className($this);
$object->register();
$this->provides[$className] = $object;
}catch(\Throwable $e){
error_log("missing class ".$className);
}
}
}

// Custom service provider
$providers = $this->config('plugin.providers', []);
foreach ($providers as $className) {
$object = new $className($this);
$object->register();
$this->provides[$className] = $object;
try{
$object = new $className($this);
$object->register();
$this->provides[$className] = $object;
}catch(\Throwable $e){
error_log("missing class ".$className);
}
}
}

Expand Down
Loading