Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ vendor/
composer.lock
.php.result.cache
.phpunit.result.cache

functions.example.php
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"intelephense.stubs": [
"wordpress",
"standard",
"json",
"Core",
"regex",
"pcre",
"iconv",
],
}
4 changes: 2 additions & 2 deletions bin/rad
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ $command = strtolower(trim($argv[1]));
$arguments = array_slice($argv, 2);

$availableCommands = [
"get-icon" => \ofc\Commands\GetIconCommand::class,
"get:icon" => \ofc\Commands\GetIconCommand::class,
'make:action' => \ofc\Commands\MakeActionCommand::class,

'make:helper' => \ofc\Commands\MakeHelperCommand::class,
// TODO: additional commands go below
// "swap-library" => \ofc\Commands\LibrarySwap::class,
];
Expand Down
84 changes: 79 additions & 5 deletions src/Commands/GetIconCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,103 @@

class GetIconCommand
{
public static function run(array $args): void

private const source_urls = [
// Bootstrap Icons - https://icons.getbootstrap.com
"bootstrap" => "https://icons.getbootstrap.com/assets/icons/",

// Hero Icons - https://heroicons.com
"heroicons_16_solid" => "https://raw.githubusercontent.com/tailwindlabs/heroicons/refs/heads/master/src/16/solid/",
"heroicons_20_solid" => "https://raw.githubusercontent.com/tailwindlabs/heroicons/refs/heads/master/src/20/solid/",
"heroicons_24_solid" => "https://raw.githubusercontent.com/tailwindlabs/heroicons/refs/heads/master/src/24/solid/",
"heroicons_24_outline" => "https://raw.githubusercontent.com/tailwindlabs/heroicons/refs/heads/master/src/24/outline/",

// Lucide Icons - https://lucide.dev/icons/
"lucide" => "https://raw.githubusercontent.com/lucide-icons/lucide/refs/heads/main/icons/",

// Material Icons - https://fonts.google.com/icons
"material" => "https://api.iconify.design/material-symbols:",
"material_round" => ["https://api.iconify.design/material-symbols:", "-rounded.svg"],
"material_outline" => ["https://api.iconify.design/material-symbols:", "-outline.svg"],
"material_sharp" => ["https://api.iconify.design/material-symbols:", "-sharp.svg"],
"material_round_outline" => ["https://api.iconify.design/material-symbols:", "-outline-rounded.svg"],

// Google Noto Emojis - https://icones.js.org/collection/noto
"noto" => "https://api.iconify.design/noto:",

// Flags - https://flagpack.xyz/docs/flag-index/
"flag" => "https://api.iconify.design/flagpack:",
];

public static function run(array $args, string $source = self::source_urls["bootstrap"]): void
{
if (!isset($args[0]) || $args[0] === '--help') {
echo self::getHelp().PHP_EOL;
exit(!isset($args[0]) || $args[0] === '--help'? 0 : 1);
}

$source_options = [
"--bootstrap" => self::source_urls["bootstrap"],
"-bs" => self::source_urls["bootstrap"],
"--heroicons" => self::source_urls["heroicons_20_solid"],
"-hi" => self::source_urls["heroicons_20_solid"],
"-hi-16" => self::source_urls["heroicons_16_solid"],
"-hi-24" => self::source_urls["heroicons_24_solid"],
"-hi-outline" => self::source_urls["heroicons_24_outline"],
"--lucide" => self::source_urls["lucide"],
"-li" => self::source_urls["lucide"],
"--material" => self::source_urls["material"],
"-md" => self::source_urls["material"],
"-md-round" => self::source_urls["material_round"],
"-md-outline" => self::source_urls["material_outline"],
"-md-sharp" => self::source_urls["material_sharp"],
"-md-round-outline" => self::source_urls["material_round_outline"],
"--emoji" => self::source_urls["noto"],
"-e" => self::source_urls["noto"],
"--flag" => self::source_urls["flag"],
"-f" => self::source_urls["flag"],
];

if ($args[0] && isset($source_options[$args[0]])) {
$source = $source_options[$args[0]];
self::run(array_slice($args, 1), $source);
return;
}

$icon = trim(strtolower($args[0]));

IconGetter::get($icon);
IconGetter::get($icon, $source);
}

public static function getHelp(): string
{
return <<<HELP
Usage: rad get-icon <icon-name>
Usage: rad get:icon [source] <icon-name>

Source:
-bs, --bootstrap Boostrap Icons (Default) - https://icons.getbootstrap.com
-hi, --heroicons Hero Icons 20px - https://heroicons.com
-hi-16 Hero Icons 16px
-hi-24 Hero Icons 24px
-hi-outline Hero Icons 24px Outline
-md, --material Material Icons - https://fonts.google.com/icons
-md-round Material Icons Rounded
-md-outline Material Icons Outlined
-md-sharp Material Icons Sharp
-md-round-outline Material Icons Round Outlined
-li, --lucide Lucide Icons - https://lucide.dev/icons/
-e, --emoji Google Noto Emojis - https://icones.js.org/collection/noto
-f, --flag Flagpack Flags (Alpha-2 Code) - https://flagpack.xyz/docs/flag-index/

Description:
Download the SVG icon from https://icons.getbootstrap.com and place it in your theme assets folder.
Download the SVG icon from the specified source and place it in your theme assets folder.
TODO: link to the docs page.

Example:
rad get-icon phone
rad get:icon phone
rad get:icon -hi phone
rad get:icon --flag uk
rad get:icon --emoji thumbs-up
HELP;
}
}
105 changes: 105 additions & 0 deletions src/Commands/MakeHelperCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace ofc\Commands;

class MakeHelperCommand
{
public static function run(array $args): void
{
if (!isset($args[0]) || $args[0] === '--help') {
echo self::getHelp().PHP_EOL;
exit(!isset($args[0]) || $args[0] === '--help'? 0 : 1);
}

// Optional flag to update the config.php file to include the new helper
$updateConfigFlag = false;
if ($args[0] === '--update-config' || $args[0] === '-u') {
$updateConfigFlag = true;
$args = array_slice($args, 1);
}

$helperName = preg_replace('/[^A-Za-z]/', '', $args[0]);

$templateRoot = getcwd();
$helpersDir = $templateRoot . DIRECTORY_SEPARATOR . 'helpers';
$filePath = $helpersDir . DIRECTORY_SEPARATOR . "{$helperName}Helper.php";

if (!is_dir($helpersDir) && !mkdir($helpersDir, 0775, true)) {
fwrite(STDERR, "Error: Could not create directory: $helpersDir\n");
exit(1);
}

if (file_exists($filePath)) {
fwrite(STDERR, "Error: Helper already exists at $filePath\n");
exit(1);
}

$stub = <<<PHP
<?php

namespace Helpers;

class {$helperName}Helper
{

/**
* Your helper callback.
*/
public static function callback()
{
return function (\$template, \$context, \$args, \$source) {
//
return '';
};
}
}

PHP;

if (file_put_contents($filePath, $stub) === false) {
fwrite(STDERR, "Error: Unable to write file: $filePath\n");
exit(1);
}

// Experimental: update the config.php file to include the new helper
// TODO: Maybe there's a library that could do this in a better way
$configFile = $templateRoot . DIRECTORY_SEPARATOR . 'config.php';
if ($updateConfigFlag && file_exists($configFile)) {
$configContents = file_get_contents($configFile);
$configRegex = '/\A(\X+(?:"handlebars"|\'handlebars\') +=> +\[\X+(?:"additional-helpers"|\'additional-helpers\') +=> +\[[^\]]*)()(\]\X+)$/';

// Splits the config file into before and after exactly where the new helper should be inserted
preg_match_all($configRegex, $configContents, $matches, PREG_SET_ORDER, 0);

if (count($matches[0]) == 4) {
// Put it all back together
$configContents = $matches[0][1] . PHP_EOL . ' "' . $helperName . '" => \\Helpers\\' . $helperName . 'Helper::callback(),' . PHP_EOL . ' ' . $matches[0][3];
file_put_contents($configFile, $configContents);
}
}

echo "Helper created: $filePath".PHP_EOL;
}

public static function getHelp(): string
{
return <<<HELP
Usage:
rad make:helper [options] <helperName>

Options:
-u, --update-config
(Experimental) Update the config.php file to include the new helper.

Description:
Creates a new Handlebars Helper class inside your current WordPress theme:
{theme}/helpers/<helperName>Helper.php

Docs:
https://rad-theme-engine.ofco.cloud/docs/guides/helpers/

Example:
rad make:helper TaxonomyButtons
HELP;
}
}
Loading