-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |