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

[generate:dashboard:widgets] New command #35

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions config/services/wp-console/generate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,10 @@ services:
console.generate_settings_page:
class: WP\Console\Command\Generate\SettingsPageCommand
arguments: ['@console.settings_page_generator', '@console.extension_manager', '@console.validator', '@console.string_converter', '@console.wordpress_api']
tags:
- { name: wordpress.command }
console.generate_dashboard_widgets:
class: WP\Console\Command\Generate\DashboardWidgetsCommand
arguments: ['@console.dashboard_widgets_generator', '@console.extension_manager', '@console.validator', '@console.string_converter']
tags:
- { name: wordpress.command }
5 changes: 5 additions & 0 deletions config/services/wp-console/generator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,10 @@ services:
console.settings_page_generator:
class: WP\Console\Generator\SettingsPageGenerator
arguments: ['@console.extension_manager']
tags:
- { name: wordpress.generator }
console.dashboard_widgets_generator:
class: WP\Console\Generator\DashboardWidgetsGenerator
arguments: ['@console.extension_manager']
tags:
- { name: wordpress.generator }
26 changes: 26 additions & 0 deletions config/translations/en/generate.dashboard.widgets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
description: 'Generate a new custom dashboard widgets.'
help: 'The <info>generate:dashboard:widgets</info> command helps you generates a new dashboard widgets.'
welcome: 'Welcome to the Wordpress dashboard widgets generator'
options:
plugin: 'The Plugin name.'
class-name: 'The dashboard widget class name'
id: 'ID used in the code identifying the widget.'
title: 'The title that will be displayed in its heading.'
render-function: 'The function that displays the widget content.'
submission-function: 'The function that handles widget submission.'
callback-arguments: 'Arguments array passed to the callback function. ["1" => "foo", "2" => "bar"]'
text-domain: 'Translations file.'
questions:
plugin: 'The plugin name'
class-name: 'Enter dashboard widget class name'
id: 'Enter the widget id'
title: 'Enter the widget name'
render-function: 'Enter the function name to display the actual contents of your widget'
submission-function: 'Do you want add function to handle submission of widget options (configuration) forms, and will also display the form elements.'
callback-arguments: 'Enter the arguments to pass into your callback function. Follow this example: "1" => "foo", "2" => "bar"'
text-domain: 'Enter the text-domain to translation file'
warnings:
plugin-unavailable: 'Warning The following plugin are not already available in your local environment "%s"'
errors:
directory-exists: 'The target directory "%s" is not empty.'
interval-invalid: 'The interval must be a number'
267 changes: 267 additions & 0 deletions src/Command/Generate/DashboardWidgetsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
<?php

/**
* @file
* Contains \WP\Console\Command\Generate\DashboardWidgetsCommand.
*/

namespace WP\Console\Command\Generate;

use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use WP\Console\Command\Shared\ConfirmationTrait;
use WP\Console\Command\Shared\PluginTrait;
use WP\Console\Core\Command\Command;
use WP\Console\Extension\Manager;
use WP\Console\Generator\DashboardWidgetsGenerator;
use WP\Console\Core\Style\WPStyle;
use WP\Console\Utils\Validator;
use WP\Console\Core\Utils\StringConverter;

class DashboardWidgetsCommand extends Command
{
use PluginTrait;
use ConfirmationTrait;

/**
* @var DashboardWidgetsGenerator
*/
protected $generator;

/**
* @var Validator
*/
protected $validator;

/**
* @var Manager
*/
protected $extensionManager;

/**
* @var StringConverter
*/
protected $stringConverter;

/**
* DashboardWidgetsCommand constructor.
*
* @param DashboardWidgetsGenerator $generator
* @param Manager $extensionManager
* @param Validator $validator
* @param StringConverter $stringConverter
*/
public function __construct(
DashboardWidgetsGenerator $generator,
Manager $extensionManager,
Validator $validator,
StringConverter $stringConverter
) {
$this->generator = $generator;
$this->extensionManager = $extensionManager;
$this->validator = $validator;
$this->stringConverter = $stringConverter;
parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('generate:dashboard:widgets')
->setDescription($this->trans('commands.generate.dashboard.widgets.description'))
->setHelp($this->trans('commands.generate.dashboard.widgets.help'))
->addOption(
'plugin',
'',
InputOption::VALUE_REQUIRED,
$this->trans('commands.common.options.plugin')
)
->addOption(
'class-name',
'',
InputOption::VALUE_REQUIRED,
$this->trans('commands.generate.dashboard.widgets.options.class-name')
)
->addOption(
'id',
'',
InputOption::VALUE_REQUIRED,
$this->trans('commands.generate.dashboard.widgets.options.id')
)
->addOption(
'title',
'',
InputOption::VALUE_REQUIRED,
$this->trans('commands.generate.dashboard.widgets.options.title')
)
->addOption(
'render-function',
'',
InputOption::VALUE_REQUIRED,
$this->trans('commands.generate.dashboard.widgets.options.render-function')
)
->addOption(
'submission-function',
'',
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.dashboard.widgets.options.submission-function')
)
->addOption(
'callback-arguments',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.dashboard.widgets.options.callback-arguments')
)
->addOption(
'text-domain',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.dashboard.widgets.options.text-domain')
)
->setAliases(['gdw']);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new WPStyle($input, $output);

// @see use WP\Console\Command\Shared\ConfirmationTrait::confirmGeneration
if (!$this->confirmGeneration($io)) {
return;
}

$plugin = $plugin = $this->validator->validatePluginName($input->getOption('plugin'));
$class_name = $this->validator->validateClassName($input->getOption('class-name'));
$id = $this->validator->validateMachineName($input->getOption('id'));
$title = $input->getOption('title');
$render_function = $this->validator->validateFunctionName($input->getOption('render-function'));
$submission_function = $input->getOption('submission-function');
$callback_arguments = $input->getOption('callback-arguments');
$text_domain = $input->getOption('text-domain');



$this->generator->generate(
$plugin,
$class_name,
$id,
$title,
$render_function,
$submission_function,
$callback_arguments,
$text_domain
);
}

/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$io = new WPStyle($input, $output);

// --plugin
$plugin = $input->getOption('plugin');
if (!$plugin) {
$plugin = $this->pluginQuestion($io);
$input->setOption('plugin', $plugin);
}

// --class name
$class_name = $input->getOption('class-name');
if (!$class_name) {
$class_name = $io->ask(
$this->trans('commands.generate.dashboard.widgets.questions.class-name'),
'CustomDashboardWidgets',
function ($value) {
if (!strlen(trim($value))) {
throw new \Exception('The Class name can not be empty');
}
return $this->stringConverter->humanToCamelCase($value);
}
);
$input->setOption('class-name', $class_name);
}

// --id
$id = $input->getOption('id');
if (!$id) {
$id = $io->ask(
$this->trans('commands.generate.dashboard.widgets.questions.id'),
strtolower($class_name),
function ($value) {
return $this->stringConverter->createMachineName($value);
}
);
}
$input->setOption('id', $id);

// --title
$title = $input->getOption('title');
if (!$title) {
$title = $io->ask(
$this->trans('commands.generate.dashboard.widgets.questions.title'),
'Example Dashboard Widget'
);
}
$input->setOption('title', $title);

// --render function
$render_function = $input->getOption('render-function');
if (!$render_function) {
$render_function = $io->ask(
$this->trans('commands.generate.dashboard.widgets.questions.render-function'),
$id.'_callback',
function ($value) {
return $this->validator->validateFunctionName($value);
}
);
}
$input->setOption('render-function', $render_function);

// --submission function
$submission_function = $input->getOption('submission-function');
if (!$submission_function) {
$submission_function = $io->confirm(
$this->trans('commands.generate.dashboard.widgets.questions.submission-function'),
false
);
}
$input->setOption('submission-function', $submission_function);

// --callback arguments
$callback_arguments = $input->getOption('callback-arguments');
if (!$callback_arguments) {
$callback_arguments = $io->askEmpty(
$this->trans('commands.generate.dashboard.widgets.questions.callback-arguments'),
function ($value) {
if (!empty($value)) {
$validate = array($value);
if (!is_array($validate)) {
throw new \Exception('Do not have the correct format');
}
}
return $value;
}
);
}
$input->setOption('callback-arguments', $callback_arguments);

// --text domain
$text_domain = $input->getOption('text-domain');
if (!$text_domain) {
$text_domain = $io->askEmpty(
$this->trans('commands.generate.dashboard.widgets.questions.text-domain')
);
}
$input->setOption('text-domain', $text_domain);
}
}
Loading