Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

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

{

$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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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";
}
}
}
}

Expand Down Expand Up @@ -2508,6 +2536,47 @@ namespace Bones {
$this->line(' Created plugin/Console/Kernel.php');
}
}
protected function simplePluralize($word) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
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);
$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
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){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

miss doc

Copy link
Collaborator

Choose a reason for hiding this comment

The 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){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

miss doc

Copy link
Collaborator

Choose a reason for hiding this comment

The 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
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
14 changes: 14 additions & 0 deletions src/Foundation/WordPressCustomPostTypeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ public function register()
$this->boot();

// Register custom post type

register_post_type($this->id, $this->optionalArgs());

$this->initHooks();
Expand Down Expand Up @@ -619,6 +620,7 @@ protected function initHooks()

// Hook save post
add_action('save_post_' . $this->id, [$this, 'save_post'], 10, 2);
add_action('trash_' . $this->id, [$this, 'trash_post'], 10, 3);

// Manage columns @since 1.9.0
add_filter("manage_{$this->id}_posts_columns", [$this, '_manage_posts_columns']);
Expand Down Expand Up @@ -917,6 +919,18 @@ public function save_post($post_id, $post = null)
}
}

/**
* Override this method when your post was trashed.
* This method is called by hook action trashed_{post_type}`
*
* @param int|string $post_id Post ID
* @param object $post Optional. Post object
*
*/
public function trash_post($post_id, $post, $old_status ){

}

/**
* Override this method to save/update your custom data.
* This method is called by hook action save_post_{post_type}`
Expand Down
Loading