Skip to content

Commit

Permalink
added make:shortcode Command
Browse files Browse the repository at this point in the history
  • Loading branch information
suomato committed Nov 5, 2017
1 parent 6a1533b commit bcb174b
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 3 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "suomato/luna",
"description": "Command-line interface for Base Camp theme",
"version": "0.1.0",
"license": "MIT",
"type": "library",
"minimum-stability": "stable",
Expand Down
44 changes: 44 additions & 0 deletions src/Commands/MakeShortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Suomato\Commands;

use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use function Suomato\build_shortcode;

class MakeShortcode extends Command
{
protected function configure()
{
$this->setName('make:shortcode')
->setDescription('Create a new Shortcode')
->addArgument('name', InputArgument::REQUIRED, 'Shortcode name');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$filename = "{$name}.php";
$shortcodes_location = new Local(__DIR__ . '/../../../../../app/config/wp/shortcodes');
$boilerplate_location = new Local(__DIR__ . '/../templates');
$shortcodes = new Filesystem($shortcodes_location);
$boilerplate = new Filesystem($boilerplate_location);
$template = $boilerplate->read('shortcode.php');

// If file already exists
if ($shortcodes->has($filename)) {
$output->writeln("<error>{$filename} already exists!</error>");

return 0;
}

$content = build_shortcode($template, $name);
$shortcodes->write("{$name}.php", $content);

$output->writeln("<info>Shortcode created successfully.</info>");
}
}
8 changes: 6 additions & 2 deletions src/Luna.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

use Suomato\Commands\MakeCustomPostType;
use Suomato\Commands\MakeRoute;
use Suomato\Commands\MakeShortcode;

class Luna
{
public static $version = '1.1.0';

public static function commands()
{
return [
new MakeCustomPostType(),
new MakeRoute()
new MakeRoute(),
new MakeShortcode(),
];
}
}
}
43 changes: 43 additions & 0 deletions src/Utils/Shortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Suomato\Utils;

abstract class Shortcode
{
/**
* @var string Shortcode name
*/
protected $shortcode;

/**
* @var array|string An associative array of attributes, or an empty string if no attributes are given
*/
protected $attributes = '';

/**
* Return template of shortcode
*
* @param $attr An associative array of attributes
* @param $content Enclosed content
*
* @return mixed
*/
protected abstract function template($attr, $content);

/**
* Shortcode constructor.
*/
public function __construct()
{
if ($this->shortcode !== '') {
add_shortcode($this->shortcode, function ($attr, $content = null) {
if ( ! empty($this->attributes)) {
$attr = shortcode_atts($this->attributes, $attr, $this->shortcode);
}

return $this->template($attr, $content);
});
}
}
}

5 changes: 5 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ function build_custom_post_type($template, $name, $plural)
$content = str_replace('movie', $singular, $content);

return $content;
}

function build_shortcode($template, $name)
{
return str_replace('{PLACEHOLDER}', $name, $template);
}
35 changes: 35 additions & 0 deletions src/templates/shortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Shortcodes;

use Suomato\Utils\Shortcode;

class {PLACEHOLDER} extends Shortcode
{
/**
* @var string Shortcode name
*/
protected $shortcode = '';

/**
* @var array|string An associative array of attributes
*/
protected $attributes = [];

/**
* Return template of shortcode
*
* @param $attr An associative array of attributes
* @param $content Enclosed content
*
* @return mixed
*/
protected function template($attr, $content)
{
return '';
}

}

// Initialize the shortcode
new {PLACEHOLDER};

0 comments on commit bcb174b

Please sign in to comment.