From ba18ed9176af89d22c144aacc1b68834e8ab36b7 Mon Sep 17 00:00:00 2001 From: Brandon Date: Tue, 26 Mar 2024 08:37:05 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20parsing=20when=20using=20t?= =?UTF-8?q?he=20`@image`=20directive=20(Fixes=20#135,=20Fixes=20#117)=20?= =?UTF-8?q?=E2=99=BB=20Refactor=20directives=20into=20classes=20?= =?UTF-8?q?=F0=9F=8E=A8=20Improve=20directive=20registration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Contracts/Directives.php | 11 + src/Directives/Acf.php | 525 ++++++----- src/Directives/Directives.php | 155 ++++ src/Directives/Helpers.php | 566 ++++++------ src/Directives/WordPress.php | 1164 ++++++++++++------------- src/SageDirectivesServiceProvider.php | 32 +- src/Util.php | 176 ---- 7 files changed, 1299 insertions(+), 1330 deletions(-) create mode 100644 src/Contracts/Directives.php create mode 100644 src/Directives/Directives.php delete mode 100644 src/Util.php diff --git a/src/Contracts/Directives.php b/src/Contracts/Directives.php new file mode 100644 index 0000000..8551d88 --- /dev/null +++ b/src/Contracts/Directives.php @@ -0,0 +1,11 @@ + function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); - - return "get(0)}, {$expression->get(1)})) : ?>". - "get(0)}, {$expression->get(1)})) : the_row(); ?>"; - } - - return "". - ""; - }, - - 'endfields' => function () { - return ''; - }, - - 'hasfields' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); - - return "get(0)}, {$expression->get(1)})) : ?>"; - } - - return ""; - }, - - 'endhasfields' => function () { - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @field / @hasfield / @isfield / @endfield - |--------------------------------------------------------------------- - */ - - 'field' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); - - if (Util::isIdentifier($expression->get(2))) { - if (empty($expression->get(3))) { - $expression->put(3, 'true'); +class Acf extends Directives +{ + /** + * The Advanced Custom Fields directives. + */ + public function directives(): array + { + return [ + /* + |--------------------------------------------------------------------- + | @fields / @endfields / @hasfields / @endhasfields + |--------------------------------------------------------------------- + */ + + 'fields' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); + + return "get(0)}, {$expression->get(1)})) : ?>". + "get(0)}, {$expression->get(1)})) : the_row(); ?>"; } - return "get(0)}, {$expression->get(2)}, {$expression->get(3)})[{$expression->get(1)}]; ?>"; - } + return "". + ""; + }, - if (Util::isIdentifier($expression->get(1))) { - if (empty($expression->get(2))) { - $expression->put(2, 'true'); + 'endfields' => function () { + return ''; + }, + + 'hasfields' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); + + return "get(0)}, {$expression->get(1)})) : ?>"; + } + + return ""; + }, + + 'endhasfields' => function () { + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @field / @hasfield / @isfield / @endfield + |--------------------------------------------------------------------- + */ + + 'field' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); + + if ($this->isToken($expression->get(2))) { + if (empty($expression->get(3))) { + $expression->put(3, 'true'); + } + + return "get(0)}, {$expression->get(2)}, {$expression->get(3)})[{$expression->get(1)}]; ?>"; + } + + if ($this->isToken($expression->get(1))) { + if (empty($expression->get(2))) { + $expression->put(2, 'true'); + } + + return "get(0)}, {$expression->get(1)}, {$expression->get(2)}); ?>"; + } + + if (empty($expression->get(2))) { + $expression->put(2, 'true'); + } + + return "get(0)}, null, {$expression->get(2)})[{$expression->get(1)}]; ?>"; } - return "get(0)}, {$expression->get(1)}, {$expression->get(2)}); ?>"; - } + return ""; + }, - if (empty($expression->get(2))) { - $expression->put(2, 'true'); - } + 'hasfield' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); - return "get(0)}, null, {$expression->get(2)})[{$expression->get(1)}]; ?>"; - } + if ($this->isToken($expression->get(2))) { + return "get(0)}, {$expression->get(2)})[{$expression->get(1)}]) : ?>"; + } - return ""; - }, + if ($this->isToken($expression->get(1))) { + return "get(0)}, {$expression->get(1)})) : ?>"; + } - 'hasfield' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); + return "get(0)})[{$expression->get(1)}]) : ?>"; + } - if (Util::isIdentifier($expression->get(2))) { - return "get(0)}, {$expression->get(2)})[{$expression->get(1)}]) : ?>"; - } + return ""; + }, - if (Util::isIdentifier($expression->get(1))) { - return "get(0)}, {$expression->get(1)})) : ?>"; - } + 'isfield' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); - return "get(0)})[{$expression->get(1)}]) : ?>"; - } + if ($this->isToken($expression->get(3))) { + return "get(0)}, {$expression->get(3)})[{$expression->get(1)}] === {$expression->get(2)}) : ?>"; // phpcs:ignore + } - return ""; - }, + if ($this->isToken($expression->get(2))) { + return "get(0)}, {$expression->get(2)}) === {$expression->get(1)}) : ?>"; // phpcs:ignore + } - 'isfield' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); + if (! empty($expression->get(2)) && ! $this->isToken($expression->get(2))) { + return "get(0)})[{$expression->get(1)}] === {$expression->get(2)}) : ?>"; // phpcs:ignore + } - if (Util::isIdentifier($expression->get(3))) { - return "get(0)}, {$expression->get(3)})[{$expression->get(1)}] === {$expression->get(2)}) : ?>"; // phpcs:ignore - } + return "get(0)}) === {$expression->get(1)}) : ?>"; + } + }, - if (Util::isIdentifier($expression->get(2))) { - return "get(0)}, {$expression->get(2)}) === {$expression->get(1)}) : ?>"; // phpcs:ignore - } + 'endfield' => function () { + return ''; + }, - if (! empty($expression->get(2)) && ! Util::isIdentifier($expression->get(2))) { - return "get(0)})[{$expression->get(1)}] === {$expression->get(2)}) : ?>"; // phpcs:ignore - } + /* + |--------------------------------------------------------------------- + | @sub / @hassub / @issub / @endsub + |--------------------------------------------------------------------- + */ - return "get(0)}) === {$expression->get(1)}) : ?>"; - } - }, + 'sub' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); - 'endfield' => function () { - return ''; - }, + if (! empty($expression->get(2))) { + return "get(0)})[{$expression->get(1)}][{$expression->get(2)}]; ?>"; + } - /* - |--------------------------------------------------------------------- - | @sub / @hassub / @issub / @endsub - |--------------------------------------------------------------------- - */ + return "get(0)})[{$expression->get(1)}]; ?>"; + } - 'sub' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); + return ""; + }, - if (! empty($expression->get(2))) { - return "get(0)})[{$expression->get(1)}][{$expression->get(2)}]; ?>"; - } + 'hassub' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); - return "get(0)})[{$expression->get(1)}]; ?>"; - } + if (! empty($expression->get(2))) { + return "get(0)})[{$expression->get(1)}][{$expression->get(2)}]) : ?>"; // phpcs:ignore + } - return ""; - }, + return "get(0)})[{$expression->get(1)}]) : ?>"; + } - 'hassub' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); + return ""; + }, - if (! empty($expression->get(2))) { - return "get(0)})[{$expression->get(1)}][{$expression->get(2)}]) : ?>"; // phpcs:ignore - } + 'issub' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); - return "get(0)})[{$expression->get(1)}]) : ?>"; - } + if (! empty($expression->get(2))) { + return "get(0)})[{$expression->get(1)}] === {$expression->get(2)}) : ?>"; // phpcs:ignore + } - return ""; - }, + return "get(0)}) === {$expression->get(1)}) : ?>"; + } + }, + + 'endsub' => function () { + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @layouts / @endlayouts + |--------------------------------------------------------------------- + */ - 'issub' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); + 'layouts' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); + + return "get(0)}, {$expression->get(1)})) : ?>". + "get(0)}, {$expression->get(1)})) : the_row(); ?>"; + } - if (! empty($expression->get(2))) { - return "get(0)})[{$expression->get(1)}] === {$expression->get(2)}) : ?>"; // phpcs:ignore - } + return "". + ""; + }, - return "get(0)}) === {$expression->get(1)}) : ?>"; - } - }, - - 'endsub' => function () { - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @layouts / @endlayouts - |--------------------------------------------------------------------- - */ - - 'layouts' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); - - return "get(0)}, {$expression->get(1)})) : ?>". - "get(0)}, {$expression->get(1)})) : the_row(); ?>"; - } - - return "". - ""; - }, - - 'endlayouts' => function () { - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @layout / @endlayout - |--------------------------------------------------------------------- - */ - - 'layout' => function ($expression) { - return ""; - }, - - 'endlayout' => function () { - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @group / @endgroup - |--------------------------------------------------------------------- - */ + 'endlayouts' => function () { + return ''; + }, - 'group' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); + /* + |--------------------------------------------------------------------- + | @layout / @endlayout + |--------------------------------------------------------------------- + */ - return "get(0)}, {$expression->get(1)})) : ?>". - "get(0)}, {$expression->get(1)})) : the_row(); ?>"; - } + 'layout' => function ($expression) { + return ""; + }, - return "". - ""; - }, + 'endlayout' => function () { + return ''; + }, - 'endgroup' => function () { - return ''; - }, + /* + |--------------------------------------------------------------------- + | @group / @endgroup + |--------------------------------------------------------------------- + */ - /* - |--------------------------------------------------------------------- - | @options / @endoptions / @hasoptions / @endhasoptions - |--------------------------------------------------------------------- - */ + 'group' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); - 'options' => function ($expression) { - return "". - ""; - }, - - 'endoptions' => function () { - return ''; - }, - - 'hasoptions' => function ($expression) { - return ""; - }, - - 'endhasoptions' => function () { - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @option / @hasoption / @isoption / @endoption - |--------------------------------------------------------------------- - */ - - 'option' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); - - return "get(0)}, 'option')[{$expression->get(1)}]; ?>"; - } - - return ""; - }, - - 'hasoption' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); - - return "get(0)}, 'option')[{$expression->get(1)}]) : ?>"; - } - - return ""; - }, - - 'isoption' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); - - if (! empty($expression->get(2))) { - return "get(0)}, 'option')[{$expression->get(1)}] === {$expression->get(2)}) : ?>"; // phpcs:ignore - } - - return "get(0)}, 'option') === {$expression->get(1)}) : ?>"; - } - }, - - 'endoption' => function () { - return ''; - }, - -]; + return "get(0)}, {$expression->get(1)})) : ?>". + "get(0)}, {$expression->get(1)})) : the_row(); ?>"; + } + + return "". + ""; + }, + + 'endgroup' => function () { + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @options / @endoptions / @hasoptions / @endhasoptions + |--------------------------------------------------------------------- + */ + + 'options' => function ($expression) { + return "". + ""; + }, + + 'endoptions' => function () { + return ''; + }, + + 'hasoptions' => function ($expression) { + return ""; + }, + + 'endhasoptions' => function () { + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @option / @hasoption / @isoption / @endoption + |--------------------------------------------------------------------- + */ + + 'option' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); + + return "get(0)}, 'option')[{$expression->get(1)}]; ?>"; + } + + return ""; + }, + + 'hasoption' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); + + return "get(0)}, 'option')[{$expression->get(1)}]) : ?>"; + } + + return ""; + }, + + 'isoption' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); + + if (! empty($expression->get(2))) { + return "get(0)}, 'option')[{$expression->get(1)}] === {$expression->get(2)}) : ?>"; // phpcs:ignore + } + + return "get(0)}, 'option') === {$expression->get(1)}) : ?>"; + } + }, + + 'endoption' => function () { + return ''; + }, + ]; + } +} diff --git a/src/Directives/Directives.php b/src/Directives/Directives.php new file mode 100644 index 0000000..c6c1989 --- /dev/null +++ b/src/Directives/Directives.php @@ -0,0 +1,155 @@ +registered) { + return; + } + + foreach ($this->directives() as $function => $directive) { + Blade::directive($function, $directive); + } + + $this->registered = true; + } + + /** + * Parse the expression into a collection. + */ + public function parse(string $expression, int $limit = PHP_INT_MAX, string $delimiter = '__comma__'): Collection + { + $expression = preg_replace_callback( + '/\'(.*?)\'|"(.*?)"/', + fn ($matches) => str_replace(',', $delimiter, $matches[0]), + $expression + ); + + return Collection::make(explode(',', $expression, $limit)) + ->map(function ($item) use ($delimiter) { + $item = Str::of($item) + ->replace($delimiter, ',') + ->trim() + ->toString(); + + return ! is_numeric($item) ? $item : (int) $item; + }); + } + + /** + * Determine if the expression should be parsed. + */ + public function shouldParse(?string $expression = ''): bool + { + return Str::contains($expression, ','); + } + + /** + * Determine if the expression is a potential token. + */ + public function isToken(?string $expression = ''): bool + { + $expression = $this->strip($expression); + + return ! empty($expression) && (is_numeric($expression) || Str::startsWith($expression, ['$', 'get_'])); + } + + /** + * Strip single quotes from the expression. + */ + public function strip(?string $expression = '', array $characters = ["'", '"']): string + { + return str_replace($characters, '', $expression ?? ''); + } + + /** + * Wraps the passed string in single quotes if they are not already present. + * + * @param string $value + * @return string + */ + public function wrap($value) + { + $value = Str::start($value, "'"); + $value = Str::finish($value, "'"); + + return $value; + } + + /** + * Unwraps the passed string from the passed delimiter. + */ + public function unwrap(?string $value = '', string $delimiter = "'"): string + { + if (Str::startsWith($value, $delimiter)) { + $value = Str::replaceFirst($delimiter, '', $value); + } + + if (Str::endsWith($value, $delimiter)) { + $value = Str::replaceLast($delimiter, '', $value); + } + + return $value; + } + + /** + * Convert an array expression to a string. + */ + public function toString(string|array|null $expression = '', bool $single = false): string + { + if (! is_array($expression)) { + return $this->wrap($expression); + } + + $keys = ''; + + foreach ($expression as $key => $value) { + $keys .= $single ? + $this->wrap($value).',' : + $this->wrap($key).' => '.$this->wrap($value).', '; + } + + $keys = trim(Str::replaceLast(',', '', $keys)); + + if (! $single) { + $keys = Str::start($keys, '['); + $keys = Str::finish($keys, ']'); + } + + return $keys; + } + + /** + * Determine if the expression looks like an array. + */ + public function isArray(?string $expression = ''): bool + { + $expression = $this->unwrap($expression); + + return Str::startsWith($expression, '[') && Str::endsWith($expression, ']'); + } +} diff --git a/src/Directives/Helpers.php b/src/Directives/Helpers.php index 6d06829..8a96db1 100644 --- a/src/Directives/Helpers.php +++ b/src/Directives/Helpers.php @@ -3,287 +3,285 @@ namespace Log1x\SageDirectives\Directives; use Illuminate\Support\Str; -use Log1x\SageDirectives\Util; - -return [ - - /* - |-------------------------------------------------------------------------- - | Helper Directives - |-------------------------------------------------------------------------- - | - | Simple helper directives for various functions used in views. - | - */ - - /* - |--------------------------------------------------------------------- - | @istrue / @isfalse - |--------------------------------------------------------------------- - */ - - 'istrue' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); - - return "get(0)}) && (bool) {$expression->get(0)} === true) : ?>". - "get(1)}; ?>". - ''; - } - - return ""; - }, - - 'endistrue' => function () { - return ''; - }, - - 'isfalse' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); - - return "get(0)}) && (bool) {$expression->get(0)} === false) : ?>". - "get(1)}; ?>". - ''; - } - - return ""; - }, - - 'endisfalse' => function () { - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @isnull / @endisnull / @isnotnull / @endisnotnull - |--------------------------------------------------------------------- - */ - - 'isnull' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); - - return "get(0)})) : ?>". - "get(1)}; ?>". - ''; - } - - return ""; - }, - - 'endisnull' => function () { - return ''; - }, - - 'isnotnull' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); - - return "get(0)})) : ?>". - "get(1)}; ?>". - ''; - } - - return ""; - }, - - 'endisnotnull' => function () { - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @notempty / @endnotempty - |--------------------------------------------------------------------- - */ - - 'notempty' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); - - return "get(0)})) : ?>". - "get(1)}; ?>". - ''; - } - - return ""; - }, - - 'endnotempty' => function () { - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @instanceof / @endinstanceof - |--------------------------------------------------------------------- - */ - - 'instanceof' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); - - return "get(0)}, {$expression->get(1)})) : ?>"; - } - }, - - 'endinstanceof' => function () { - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @typeof / @endtypeof - |--------------------------------------------------------------------- - */ - - 'typeof' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); - - return "get(0)}) === {$expression->get(1)}) : ?>"; - } - }, - - 'endtypeof' => function () { - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @global - |--------------------------------------------------------------------- - */ - - 'global' => function ($expression) { - return ""; - }, - - /* - |--------------------------------------------------------------------- - | @set / @unset - |--------------------------------------------------------------------- - */ - - 'set' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression, 2); - - return "get(0)} = {$expression->get(1)}; ?>"; - } - }, - - 'unset' => function ($expression) { - return ""; - }, - - /* - |--------------------------------------------------------------------- - | @extract / @implode - |--------------------------------------------------------------------- - */ - - 'extract' => function ($expression) { - return ""; - }, - - 'implode' => function ($expression) { - if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); - - return "get(0)}, {$expression->get(1)}); ?>"; - } - }, - - /* - |--------------------------------------------------------------------- - | @repeat / @endrepeat - |--------------------------------------------------------------------- - */ - - 'repeat' => function ($expression) { - $initLoop = "\$__currentLoopData = range(1, {$expression}); \$__env->addLoop(\$__currentLoopData);"; - $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();'; - - return ""; - }, - - 'endrepeat' => function () { - return 'popLoop(); $loop = $__env->getLastLoop(); ?>'; - }, - - /* - |--------------------------------------------------------------------- - | @stylesheet / @endstylesheet - |--------------------------------------------------------------------- - */ - - 'stylesheet' => function ($expression) { - if (! empty($expression)) { - return ''; - } - - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @script / @endscript - |--------------------------------------------------------------------- - */ - - 'script' => function ($expression) { - if (! empty($expression)) { - return ''; - } - - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @js - |--------------------------------------------------------------------- - */ - - 'js' => function ($expression) { - $expression = Util::parse($expression); - $variable = Util::strip($expression->get(0)); - - return "'; - }, - - /* - |--------------------------------------------------------------------- - | @inline - |--------------------------------------------------------------------- - */ - - 'inline' => function ($expression) { - $path = Util::strip($expression); - - $output = ""; - - if (Str::endsWith($path, '.css')) { - return ""; - } - - if (Str::endsWith($path, '.js')) { - return ""; - } - - return $output; - }, - -]; + +class Helpers extends Directives +{ + /** + * The Helper directives. + */ + public function directives(): array + { + return [ + /* + |--------------------------------------------------------------------- + | @istrue / @isfalse + |--------------------------------------------------------------------- + */ + + 'istrue' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); + + return "get(0)}) && (bool) {$expression->get(0)} === true) : ?>". + "get(1)}; ?>". + ''; + } + + return ""; + }, + + 'endistrue' => function () { + return ''; + }, + + 'isfalse' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); + + return "get(0)}) && (bool) {$expression->get(0)} === false) : ?>". + "get(1)}; ?>". + ''; + } + + return ""; + }, + + 'endisfalse' => function () { + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @isnull / @endisnull / @isnotnull / @endisnotnull + |--------------------------------------------------------------------- + */ + + 'isnull' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); + + return "get(0)})) : ?>". + "get(1)}; ?>". + ''; + } + + return ""; + }, + + 'endisnull' => function () { + return ''; + }, + + 'isnotnull' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); + + return "get(0)})) : ?>". + "get(1)}; ?>". + ''; + } + + return ""; + }, + + 'endisnotnull' => function () { + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @notempty / @endnotempty + |--------------------------------------------------------------------- + */ + + 'notempty' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); + + return "get(0)})) : ?>". + "get(1)}; ?>". + ''; + } + + return ""; + }, + + 'endnotempty' => function () { + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @instanceof / @endinstanceof + |--------------------------------------------------------------------- + */ + + 'instanceof' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); + + return "get(0)}, {$expression->get(1)})) : ?>"; + } + }, + + 'endinstanceof' => function () { + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @typeof / @endtypeof + |--------------------------------------------------------------------- + */ + + 'typeof' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); + + return "get(0)}) === {$expression->get(1)}) : ?>"; + } + }, + + 'endtypeof' => function () { + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @global + |--------------------------------------------------------------------- + */ + + 'global' => function ($expression) { + return ""; + }, + + /* + |--------------------------------------------------------------------- + | @set / @unset + |--------------------------------------------------------------------- + */ + + 'set' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression, 2); + + return "get(0)} = {$expression->get(1)}; ?>"; + } + }, + + 'unset' => function ($expression) { + return ""; + }, + + /* + |--------------------------------------------------------------------- + | @extract / @implode + |--------------------------------------------------------------------- + */ + + 'extract' => function ($expression) { + return ""; + }, + + 'implode' => function ($expression) { + if ($this->shouldParse($expression)) { + $expression = $this->parse($expression); + + return "get(0)}, {$expression->get(1)}); ?>"; + } + }, + + /* + |--------------------------------------------------------------------- + | @repeat / @endrepeat + |--------------------------------------------------------------------- + */ + + 'repeat' => function ($expression) { + $initLoop = "\$__currentLoopData = range(1, {$expression}); \$__env->addLoop(\$__currentLoopData);"; + $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();'; + + return ""; + }, + + 'endrepeat' => function () { + return 'popLoop(); $loop = $__env->getLastLoop(); ?>'; + }, + + /* + |--------------------------------------------------------------------- + | @stylesheet / @endstylesheet + |--------------------------------------------------------------------- + */ + + 'stylesheet' => function ($expression) { + if (! empty($expression)) { + return ''; + } + + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @script / @endscript + |--------------------------------------------------------------------- + */ + + 'script' => function ($expression) { + if (! empty($expression)) { + return ''; + } + + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @js + |--------------------------------------------------------------------- + */ + + 'js' => function ($expression) { + $expression = $this->parse($expression); + $variable = $this->strip($expression->get(0)); + + return "'; + }, + + /* + |--------------------------------------------------------------------- + | @inline + |--------------------------------------------------------------------- + */ + + 'inline' => function ($expression) { + $path = $this->strip($expression); + + $output = ""; + + if (Str::endsWith($path, '.css')) { + return ""; + } + + if (Str::endsWith($path, '.js')) { + return ""; + } + + return $output; + }, + + ]; + } +} diff --git a/src/Directives/WordPress.php b/src/Directives/WordPress.php index fc518a5..1622f01 100644 --- a/src/Directives/WordPress.php +++ b/src/Directives/WordPress.php @@ -3,638 +3,634 @@ namespace Log1x\SageDirectives\Directives; use Illuminate\Support\Str; -use Log1x\SageDirectives\Util; - -return [ - - /* - |-------------------------------------------------------------------------- - | WordPress Directives - |-------------------------------------------------------------------------- - | - | Directives for various WordPress use-cases. - | - */ - - /* - |--------------------------------------------------------------------- - | @query / @posts / @endposts - |--------------------------------------------------------------------- - */ - - 'query' => function ($expression) { - return ''. - ""; - }, - - 'posts' => function ($expression) { - $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();'; - - if (! empty($expression)) { - return ''. - - "". - "put('p', is_a({$expression}, 'WP_Post') ? ({$expression})->ID : {$expression}); ?>". - ''. - - "". - "put('ignore_sticky_posts', true) - ->put('posts_per_page', -1) - ->put('post__in', collect({$expression}) - ->map(function (\$post) { - return is_a(\$post, 'WP_Post') ? \$post->ID : \$post; - })->all()) - ->put('orderby', 'post__in'); - ?>". - ''. - - "isNotEmpty() ? new WP_Query(\$posts->all()) : {$expression}; ?>". - "have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : {$iterateLoop} \$query->the_post(); ?>"; - } - - $handleQuery = ''. - ''. - ''. - ''; - - return "{$handleQuery} have_posts()) : ?>". - "post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : {$iterateLoop} \$query->the_post(); ?>"; - }, - - 'endposts' => function () { - return 'popLoop(); $loop = $__env->getLastLoop(); endif; ?>'; - }, - - /* - |--------------------------------------------------------------------- - | @hasposts / @endhasposts / @noposts / @endnoposts - |--------------------------------------------------------------------- - */ - - 'hasposts' => function ($expression) { - if (! empty($expression)) { - return ''. - - "". - "put('p', is_a({$expression}, 'WP_Post') ? ({$expression})->ID : {$expression}); ?>". - ''. - - "". - "put('ignore_sticky_posts', true) - ->put('posts_per_page', -1) - ->put('post__in', collect({$expression}) - ->map(function (\$post) { - return is_a(\$post, 'WP_Post') ? \$post->ID : \$post; - })->all()) - ->put('orderby', 'post__in'); - ?>". - ''. - - "isNotEmpty() ? new WP_Query(\$posts->all()) : {$expression}; ?>". - 'have_posts()) : ?>'; - } - - return ''. - ''. - ''. - ''. - - 'have_posts()) : ?>'; - }, - - 'endhasposts' => function () { - return ''; - }, - - 'noposts' => function ($expression) { - if (! empty($expression)) { - return ''. - - "". - "put('p', is_a({$expression}, 'WP_Post') ? ({$expression})->ID : {$expression}); ?>". - ''. - - "". - "put('ignore_sticky_posts', true) - ->put('posts_per_page', -1) - ->put('post__in', collect({$expression}) - ->map(function (\$post) { - return is_a(\$post, 'WP_Post') ? \$post->ID : \$post; - })->all()) - ->put('orderby', 'post__in'); - ?>". - ''. - - "isNotEmpty() ? new WP_Query(\$posts->all()) : {$expression}; ?>". - 'have_posts()) : ?>'; - } - - return ''. - ''. - ''. - ''. - - 'have_posts()) : ?>'; - }, - - 'endnoposts' => function () { - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @postmeta - |--------------------------------------------------------------------- - */ - - 'postmeta' => function ($expression) { - if (! empty($expression)) { - $expression = Util::parse($expression); - - if (Util::isIdentifier($expression->get(0))) { - if (empty($expression->get(1))) { - return "get(0)}); ?>"; + +class WordPress extends Directives +{ + /** + * The WordPress directives. + */ + public function directives(): array + { + return [ + /* + |--------------------------------------------------------------------- + | @query / @posts / @endposts + |--------------------------------------------------------------------- + */ + + 'query' => function ($expression) { + return ''. + ""; + }, + + 'posts' => function ($expression) { + $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();'; + + if (! empty($expression)) { + return ''. + + "". + "put('p', is_a({$expression}, 'WP_Post') ? ({$expression})->ID : {$expression}); ?>". + ''. + + "". + "put('ignore_sticky_posts', true) + ->put('posts_per_page', -1) + ->put('post__in', collect({$expression}) + ->map(fn (\$post) => is_a(\$post, 'WP_Post') ? \$post->ID : \$post)->all() + ) + ->put('orderby', 'post__in'); + ?>". + ''. + + "isNotEmpty() ? new WP_Query(\$posts->all()) : {$expression}; ?>". + "have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : {$iterateLoop} \$query->the_post(); ?>"; + } + + $handleQuery = ''. + ''. + ''. + ''; + + return "{$handleQuery} have_posts()) : ?>". + "post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : {$iterateLoop} \$query->the_post(); ?>"; + }, + + 'endposts' => function () { + return 'popLoop(); $loop = $__env->getLastLoop(); endif; ?>'; + }, + + /* + |--------------------------------------------------------------------- + | @hasposts / @endhasposts / @noposts / @endnoposts + |--------------------------------------------------------------------- + */ + + 'hasposts' => function ($expression) { + if (! empty($expression)) { + return ''. + + "". + "put('p', is_a({$expression}, 'WP_Post') ? ({$expression})->ID : {$expression}); ?>". + ''. + + "". + "put('ignore_sticky_posts', true) + ->put('posts_per_page', -1) + ->put('post__in', collect({$expression}) + ->map(fn (\$post) => is_a(\$post, 'WP_Post') ? \$post->ID : \$post)->all() + ) + ->put('orderby', 'post__in'); + ?>". + ''. + + "isNotEmpty() ? new WP_Query(\$posts->all()) : {$expression}; ?>". + 'have_posts()) : ?>'; + } + + return ''. + ''. + ''. + ''. + + 'have_posts()) : ?>'; + }, + + 'endhasposts' => function () { + return ''; + }, + + 'noposts' => function ($expression) { + if (! empty($expression)) { + return ''. + + "". + "put('p', is_a({$expression}, 'WP_Post') ? ({$expression})->ID : {$expression}); ?>". + ''. + + "". + "put('ignore_sticky_posts', true) + ->put('posts_per_page', -1) + ->put('post__in', collect({$expression}) + ->map(fn (\$post) => is_a(\$post, 'WP_Post') ? \$post->ID : \$post)->all() + ) + ->put('orderby', 'post__in'); + ?>". + ''. + + "isNotEmpty() ? new WP_Query(\$posts->all()) : {$expression}; ?>". + 'have_posts()) : ?>'; + } + + return ''. + ''. + ''. + ''. + + 'have_posts()) : ?>'; + }, + + 'endnoposts' => function () { + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @postmeta + |--------------------------------------------------------------------- + */ + + 'postmeta' => function ($expression) { + if (! empty($expression)) { + $expression = $this->parse($expression); + + if ($this->isToken($expression->get(0))) { + if (empty($expression->get(1))) { + return "get(0)}); ?>"; + } + + if (empty($expression->get(2))) { + $expression->put(2, 'false'); + } + + return "get(0)}, {$expression->get(1)}, {$expression->get(2)}); ?>"; + } + + if ($this->isToken($expression->get(1))) { + if (empty($expression->get(2))) { + $expression->put(2, 'false'); + } + + return "get(1)}, {$expression->get(0)}, {$expression->get(2)}); ?>"; + } + + if (empty($expression->get(1))) { + $expression->put(1, 'false'); + } + + if (! $this->isToken($expression->get(0))) { + return "get(0)}, {$expression->get(1)}); ?>"; + } + + return "get(0)}, {$expression->get(1)}); ?>"; } - if (empty($expression->get(2))) { - $expression->put(2, 'false'); + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @title / @content / @excerpt / @permalink / @thumbnail + |--------------------------------------------------------------------- + */ + + 'title' => function ($expression) { + if (! empty($expression)) { + return ""; } - return "get(0)}, {$expression->get(1)}, {$expression->get(2)}); ?>"; - } + return ''; + }, + + 'content' => function () { + return ''; + }, + + 'excerpt' => function () { + return ''; + }, + + 'permalink' => function ($expression) { + return ""; + }, + + 'thumbnail' => function ($expression) { + if (! empty($expression)) { + $expression = $this->parse($expression); + + if (! empty($expression->get(2))) { + if ($expression->get(2) === 'false') { + return "get(0)}, is_numeric({$expression->get(1)}) ? [{$expression->get(1)}, {$expression->get(1)}] : {$expression->get(1)}); ?>"; // phpcs:ignore + } - if (Util::isIdentifier($expression->get(1))) { - if (empty($expression->get(2))) { - $expression->put(2, 'false'); + return "get(0)}, is_numeric({$expression->get(1)}) ? [{$expression->get(1)}, {$expression->get(1)}] : {$expression->get(1)}); ?>"; // phpcs:ignore + } + + if (! empty($expression->get(1))) { + if ($expression->get(1) === 'false') { + if ($this->isToken($expression->get(0))) { + return "get(0)}, 'thumbnail'); ?>"; + } + + return "get(0)}); ?>"; + } + + return "get(0)}, is_numeric({$expression->get(1)}) ? [{$expression->get(1)}, {$expression->get(1)}] : {$expression->get(1)}); ?>"; // phpcs:ignore + } + + if (! empty($expression->get(0))) { + if ($expression->get(0) === 'false') { + return ""; + } + + if ($this->isToken($expression->get(0))) { + return "get(0)}, 'thumbnail'); ?>"; + } + + return "get(0)}); ?>"; + } } - return "get(1)}, {$expression->get(0)}, {$expression->get(2)}); ?>"; - } + return ""; + }, + + /* + |--------------------------------------------------------------------- + | @author / @authorurl / @published / @modified + |--------------------------------------------------------------------- + */ - if (empty($expression->get(1))) { - $expression->put(1, 'false'); - } + 'author' => function ($expression) { + if (! empty($expression)) { + return ""; + } - if (! Util::isIdentifier($expression->get(0))) { - return "get(0)}, {$expression->get(1)}); ?>"; - } + return ""; + }, - return "get(0)}, {$expression->get(1)}); ?>"; - } + 'authorurl' => function ($expression) { + if (! empty($expression)) { + return ""; + } - return ''; - }, + return ""; + }, - /* - |--------------------------------------------------------------------- - | @title / @content / @excerpt / @permalink / @thumbnail - |--------------------------------------------------------------------- - */ + 'published' => function ($expression) { + if (! empty($expression)) { + $expression = $this->parse($expression); - 'title' => function ($expression) { - if (! empty($expression)) { - return ""; - } + if ($this->isToken($expression->get(0))) { + return "get(0)}); ?>"; + } + + if (! $this->isToken($expression->get(0)) && empty($expression->get(1))) { + return "get(0)}); ?>"; + } - return ''; - }, + return "get(0)}, {$expression->get(1)}); ?>"; + } - 'content' => function () { - return ''; - }, + return ''; + }, - 'excerpt' => function () { - return ''; - }, + 'modified' => function ($expression) { + if (! empty($expression)) { + $expression = $this->parse($expression); - 'permalink' => function ($expression) { - return ""; - }, + if ($this->isToken($expression->get(0))) { + return "get(0)}); ?>"; + } - 'thumbnail' => function ($expression) { - if (! empty($expression)) { - $expression = Util::parse($expression); + if ($this->isToken($expression->get(1))) { + return "get(0)}, {$expression->get(1)}); ?>"; + } - if (! empty($expression->get(2))) { - if ($expression->get(2) === 'false') { - return "get(0)}, is_numeric({$expression->get(1)}) ? [{$expression->get(1)}, {$expression->get(1)}] : {$expression->get(1)}); ?>"; // phpcs:ignore + return "get(0)}); ?>"; } - return "get(0)}, is_numeric({$expression->get(1)}) ? [{$expression->get(1)}, {$expression->get(1)}] : {$expression->get(1)}); ?>"; // phpcs:ignore - } + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @category / @categories / @term / @terms + |--------------------------------------------------------------------- + */ + + 'category' => function ($expression) { + $expression = $this->parse($expression); - if (! empty($expression->get(1))) { - if ($expression->get(1) === 'false') { - if (Util::isIdentifier($expression->get(0))) { - return "get(0)}, 'thumbnail'); ?>"; + if ($expression->get(1) === 'true') { + return "get(0)}))->isNotEmpty()) : ?>". + "get(0)}))->shift()->cat_ID); ?>\">". // phpcs:ignore + "get(0)}))->shift()->name; ?>". + ''. + ''; + } + + if (! empty($expression->get(0))) { + if ($expression->get(0) === 'true') { + return 'isNotEmpty()) : ?>'. + ''. + 'shift()->name; ?>'. + ''. + ''; } - return "get(0)}); ?>"; + return "get(0)}))->isNotEmpty()) : ?>". + "get(0)}))->shift()->name; ?>". + ''; + } + + return 'isNotEmpty()) : ?>'. + 'shift()->name; ?>'. + ''; + }, + + 'categories' => function ($expression) { + $expression = $this->parse($expression); + + if ($expression->get(1) === 'true') { + return "get(0)}); ?>"; + } + + if ($expression->get(0) === 'true') { + return ""; + } + + if (is_numeric($expression->get(0))) { + return "get(0)})); ?>"; + } + + return ""; + }, + + 'term' => function ($expression) { + $expression = $this->parse($expression); + + if (! empty($expression->get(2)) && $expression->get(2) === 'true') { + return "get(1)}, {$expression->get(0)})) : ?>". // phpcs:ignore + "get(1)}, {$expression->get(0)}))->shift()->term_id); ?>\">". // phpcs:ignore + "get(1)}, {$expression->get(0)}))->shift()->name; ?>". + ''. + ''; } - return "get(0)}, is_numeric({$expression->get(1)}) ? [{$expression->get(1)}, {$expression->get(1)}] : {$expression->get(1)}); ?>"; // phpcs:ignore - } + if (! empty($expression->get(1))) { + if ($expression->get(1) === 'true') { + return "get(0)})) : ?>". + "get(0)}))->shift()->term_id); ?>\">". // phpcs:ignore + "get(0)}))->shift()->name; ?>". + ''. + ''; + } - if (! empty($expression->get(0))) { - if ($expression->get(0) === 'false') { - return ""; + return "get(1)}, {$expression->get(0)})) : ?>". // phpcs:ignore + "get(1)}, {$expression->get(0)}))->shift()->name; ?>". + ''; } - if (Util::isIdentifier($expression->get(0))) { - return "get(0)}, 'thumbnail'); ?>"; + if (! empty($expression->get(0))) { + return "get(0)})) : ?>". + "get(0)}))->shift()->name; ?>". + ''; } + }, - return "get(0)}); ?>"; - } - } + 'terms' => function ($expression) { + $expression = $this->parse($expression); - return ""; - }, + if ($expression->get(2) === 'true') { + return "get(1)}, {$expression->get(0)}, '', ', '); ?>"; + } - /* - |--------------------------------------------------------------------- - | @author / @authorurl / @published / @modified - |--------------------------------------------------------------------- - */ + if (! empty($expression->get(1))) { + if ($expression->get(1) === 'true') { + return "get(0)}, '', ', '); ?>"; + } - 'author' => function ($expression) { - if (! empty($expression)) { - return ""; - } + return "get(1)}, {$expression->get(0)}, '', ', ')); ?>"; + } - return ""; - }, + if (! empty($expression->get(0))) { + return "get(0)}, '', ', ')); ?>"; + } + }, - 'authorurl' => function ($expression) { - if (! empty($expression)) { - return ""; - } + /* + |--------------------------------------------------------------------- + | @image + |--------------------------------------------------------------------- + */ - return ""; - }, + 'image' => function ($expression) { + $expression = $this->parse($expression); + $output = "get(0)}; ?>"; - 'published' => function ($expression) { - if (! empty($expression)) { - $expression = Util::parse($expression); + if (! $this->isToken($expression->get(0))) { + $output .= ""; - if (Util::isIdentifier($expression->get(0))) { - return "get(0)}); ?>"; - } + $output .= ""; + } - if (! Util::isIdentifier($expression->get(0)) && empty($expression->get(1))) { - return "get(0)}); ?>"; - } + if ($this->strip($expression->get(1)) == 'raw') { + return $output.''; + } - return "get(0)}, {$expression->get(1)}); ?>"; - } + if (! empty($expression->get(3))) { + $expression = $expression->put(2, $this->unwrap($this->toString($expression->slice(2)->all(), true))); + } - return ''; - }, + if (! empty($expression->get(2)) && ! $this->isArray($expression->get(2))) { + $expression = $expression->put(2, $this->toString(['alt' => $this->strip($expression->get(2))])); + } - 'modified' => function ($expression) { - if (! empty($expression)) { - $expression = Util::parse($expression); + if ($expression->get(1)) { + if ($expression->get(2)) { + return $output."get(1)}, + false, + {$expression->get(2)} + ); ?>"; + } - if (Util::isIdentifier($expression->get(0))) { - return "get(0)}); ?>"; - } + return $output."get(1)}, false); ?>"; + } - if (Util::isIdentifier($expression->get(1))) { - return "get(0)}, {$expression->get(1)}); ?>"; - } + if ($expression->get(2)) { + return $output."get(2)} + ); ?>"; + } - return "get(0)}); ?>"; - } + return $output.""; + }, - return ''; - }, + /* + |--------------------------------------------------------------------- + | @role / @endrole / @user / @enduser / @guest / @endguest + |--------------------------------------------------------------------- + */ - /* - |--------------------------------------------------------------------- - | @category / @categories / @term / @terms - |--------------------------------------------------------------------- - */ + 'role' => function ($expression) { + $expression = $this->parse($expression); + $condition = []; - 'category' => function ($expression) { - $expression = Util::parse($expression); + foreach ($expression as $value) { + $condition[] = "in_array(strtolower({$value}), (array) wp_get_current_user()->roles) ||"; + } - if ($expression->get(1) === 'true') { - return "get(0)}))->isNotEmpty()) : ?>". - "get(0)}))->shift()->cat_ID); ?>\">". // phpcs:ignore - "get(0)}))->shift()->name; ?>". - ''. - ''; - } + $conditions = implode(' ', $condition); + + $conditions = Str::beforeLast($conditions, ' ||'); + + return ""; + }, + + 'endrole' => function () { + return ''; + }, + + 'user' => function () { + return ''; + }, + + 'enduser' => function () { + return ''; + }, + + 'guest' => function () { + return ''; + }, + + 'endguest' => function () { + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @shortcode + |--------------------------------------------------------------------- + */ + + 'shortcode' => function ($expression) { + return ""; + }, + + /* + |--------------------------------------------------------------------- + | @wpautop / @wpautokp + |--------------------------------------------------------------------- + */ + + 'wpautop' => function ($expression) { + return ""; + }, + + 'wpautokp' => function ($expression) { + return ""; + }, + + /* + |--------------------------------------------------------------------- + | @action / @filter + |--------------------------------------------------------------------- + */ + + 'action' => function ($expression) { + return ""; + }, + + 'filter' => function ($expression) { + return ""; + }, + + /* + |--------------------------------------------------------------------- + | @wphead / @wpfooter + |--------------------------------------------------------------------- + */ + + 'wphead' => function () { + return ''; + }, + + 'wpfooter' => function () { + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @bodyclass / @wpbodyopen + |--------------------------------------------------------------------- + */ + + 'bodyclass' => function ($expression) { + return ""; + }, + + 'wpbodyopen' => function () { + return ""; + }, + + /* + |--------------------------------------------------------------------- + | @postclass + |--------------------------------------------------------------------- + */ + + 'postclass' => function ($expression) { + return ""; + }, + + /* + |--------------------------------------------------------------------- + | @sidebar / @hassidebar / @endhassidebar + |--------------------------------------------------------------------- + */ + + 'sidebar' => function ($expression) { + return ""; + }, + + 'hassidebar' => function ($expression) { + return ""; + }, + + 'endhassidebar' => function () { + return ''; + }, + + /* + |--------------------------------------------------------------------- + | @__ + |--------------------------------------------------------------------- + */ + + '__' => function ($expression) { + $expression = $this->parse($expression); + + return ""; + }, + + /* + |--------------------------------------------------------------------- + | @thememod + |--------------------------------------------------------------------- + */ + + 'thememod' => function ($expression) { + $expression = $this->parse($expression); + + $mod = $expression->get(0); + $default = $expression->get(1); + + if (! empty($default)) { + return ""; + } - if (! empty($expression->get(0))) { - if ($expression->get(0) === 'true') { - return 'isNotEmpty()) : ?>'. - ''. - 'shift()->name; ?>'. - ''. - ''; - } - - return "get(0)}))->isNotEmpty()) : ?>". - "get(0)}))->shift()->name; ?>". - ''; - } - - return 'isNotEmpty()) : ?>'. - 'shift()->name; ?>'. - ''; - }, - - 'categories' => function ($expression) { - $expression = Util::parse($expression); - - if ($expression->get(1) === 'true') { - return "get(0)}); ?>"; - } - - if ($expression->get(0) === 'true') { - return ""; - } - - if (is_numeric($expression->get(0))) { - return "get(0)})); ?>"; - } - - return ""; - }, - - 'term' => function ($expression) { - $expression = Util::parse($expression); - - if (! empty($expression->get(2)) && $expression->get(2) === 'true') { - return "get(1)}, {$expression->get(0)})) : ?>". // phpcs:ignore - "get(1)}, {$expression->get(0)}))->shift()->term_id); ?>\">". // phpcs:ignore - "get(1)}, {$expression->get(0)}))->shift()->name; ?>". - ''. - ''; - } - - if (! empty($expression->get(1))) { - if ($expression->get(1) === 'true') { - return "get(0)})) : ?>". - "get(0)}))->shift()->term_id); ?>\">". // phpcs:ignore - "get(0)}))->shift()->name; ?>". - ''. - ''; - } - - return "get(1)}, {$expression->get(0)})) : ?>". // phpcs:ignore - "get(1)}, {$expression->get(0)}))->shift()->name; ?>". - ''; - } - - if (! empty($expression->get(0))) { - return "get(0)})) : ?>". - "get(0)}))->shift()->name; ?>". - ''; - } - }, - - 'terms' => function ($expression) { - $expression = Util::parse($expression); - - if ($expression->get(2) === 'true') { - return "get(1)}, {$expression->get(0)}, '', ', '); ?>"; - } - - if (! empty($expression->get(1))) { - if ($expression->get(1) === 'true') { - return "get(0)}, '', ', '); ?>"; - } - - return "get(1)}, {$expression->get(0)}, '', ', ')); ?>"; - } - - if (! empty($expression->get(0))) { - return "get(0)}, '', ', ')); ?>"; - } - }, - - /* - |--------------------------------------------------------------------- - | @image - |--------------------------------------------------------------------- - */ - - 'image' => function ($expression) { - $expression = Util::parse($expression); - $image = Util::strip($expression->get(0)); - - if ( - ! Util::isIdentifier($image) && - $image = Util::field($image) - ) { - $expression = $expression->put(0, is_array($image) && ! empty($image['id']) ? $image['id'] : $image); - } - - if (Util::strip($expression->get(1)) == 'raw') { - return "get(0)}); ?>"; - } - - if (! empty($expression->get(3))) { - $expression = $expression->put(2, Util::clean($expression->slice(2)->all())); - } - - if (! empty($expression->get(2)) && ! Util::isArray($expression->get(2))) { - $expression = $expression->put(2, Util::toString(['alt' => $expression->get(2)])); - } - - if ($expression->get(1)) { - if ($expression->get(2)) { - return "get(0)}, - {$expression->get(1)}, - false, - {$expression->get(2)} - ); ?>"; - } - - return "get(0)}, {$expression->get(1)}, false); ?>"; - } - - if ($expression->get(2)) { - return "get(0)}, - 'full', - false, - {$expression->get(2)} - ); ?>"; - } - - return "get(0)}, 'thumbnail', false); ?>"; - }, - - /* - |--------------------------------------------------------------------- - | @role / @endrole / @user / @enduser / @guest / @endguest - |--------------------------------------------------------------------- - */ - - 'role' => function ($expression) { - $expression = Util::parse($expression); - $condition = []; - - foreach ($expression as $value) { - $condition[] = "in_array(strtolower({$value}), (array) wp_get_current_user()->roles) ||"; - } - - $conditions = implode(' ', $condition); - - $conditions = Str::beforeLast($conditions, ' ||'); - - return ""; - }, - - 'endrole' => function () { - return ''; - }, - - 'user' => function () { - return ''; - }, - - 'enduser' => function () { - return ''; - }, - - 'guest' => function () { - return ''; - }, - - 'endguest' => function () { - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @shortcode - |--------------------------------------------------------------------- - */ - - 'shortcode' => function ($expression) { - return ""; - }, - - /* - |--------------------------------------------------------------------- - | @wpautop / @wpautokp - |--------------------------------------------------------------------- - */ - - 'wpautop' => function ($expression) { - return ""; - }, - - 'wpautokp' => function ($expression) { - return ""; - }, - - /* - |--------------------------------------------------------------------- - | @action / @filter - |--------------------------------------------------------------------- - */ - - 'action' => function ($expression) { - return ""; - }, - - 'filter' => function ($expression) { - return ""; - }, - - /* - |--------------------------------------------------------------------- - | @wphead / @wpfooter - |--------------------------------------------------------------------- - */ - - 'wphead' => function () { - return ''; - }, - - 'wpfooter' => function () { - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @bodyclass / @wpbodyopen - |--------------------------------------------------------------------- - */ - - 'bodyclass' => function ($expression) { - return ""; - }, - - 'wpbodyopen' => function () { - return ""; - }, - - /* - |--------------------------------------------------------------------- - | @postclass - |--------------------------------------------------------------------- - */ - - 'postclass' => function ($expression) { - return ""; - }, - - /* - |--------------------------------------------------------------------- - | @sidebar / @hassidebar / @endhassidebar - |--------------------------------------------------------------------- - */ - - 'sidebar' => function ($expression) { - return ""; - }, - - 'hassidebar' => function ($expression) { - return ""; - }, - - 'endhassidebar' => function () { - return ''; - }, - - /* - |--------------------------------------------------------------------- - | @__ - |--------------------------------------------------------------------- - */ - - '__' => function ($expression) { - $expression = Util::parse($expression); - - return ""; - }, - - /* - |--------------------------------------------------------------------- - | @thememod - |--------------------------------------------------------------------- - */ - - 'thememod' => function ($expression) { - $expression = Util::parse($expression); - - $mod = $expression->get(0); - $default = $expression->get(1); - - if (! empty($default)) { - return ""; - } - - return ""; - }, -]; + return ""; + }, + ]; + } +} diff --git a/src/SageDirectivesServiceProvider.php b/src/SageDirectivesServiceProvider.php index c4f2d69..7e608ec 100644 --- a/src/SageDirectivesServiceProvider.php +++ b/src/SageDirectivesServiceProvider.php @@ -2,36 +2,26 @@ namespace Log1x\SageDirectives; -use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; class SageDirectivesServiceProvider extends ServiceProvider { /** - * Bootstrap any application services. - * - * @return void + * The directive providers. */ - public function boot() - { - $this->directives() - ->each(function ($directive, $function) { - Blade::directive($function, $directive); - }); - } + protected array $directives = [ + Directives\Acf::class, + Directives\Helpers::class, + Directives\WordPress::class, + ]; /** - * Get the Blade directives. - * - * @return array + * Bootstrap any application services. */ - public function directives() + public function boot(): void { - return collect(['Acf', 'Helpers', 'WordPress']) - ->flatMap(function ($directive) { - if (file_exists($directives = __DIR__.'/Directives/'.$directive.'.php')) { - return require_once $directives; - } - }); + collect($this->directives) + ->filter(fn ($directive) => is_subclass_of($directive, Directives\Directives::class)) + ->each(fn ($directive) => $directive::make()->register()); } } diff --git a/src/Util.php b/src/Util.php deleted file mode 100644 index 831500b..0000000 --- a/src/Util.php +++ /dev/null @@ -1,176 +0,0 @@ -map(function ($item) use ($delimiter) { - $item = str_replace($delimiter, ',', $item); - $item = trim($item); - - if (is_numeric($item)) { - return (int) $item; - } - - return $item; - }); - } - - /** - * Determine if the string is a valid identifier. - * - * @param string $expression - * @return bool - */ - public static function isIdentifier($expression = null) - { - return ! empty($expression) && (is_numeric($expression) || Str::startsWith($expression, '$') || Str::startsWith($expression, 'get_')); - } - - /** - * Strip single quotes from expression. - * - * @param string $expression - * @return string - */ - public static function strip($expression = null) - { - return ! empty($expression) ? str_replace(["'", '"'], '', $expression) : $expression; - } - - /** - * Wraps the passed string in single quotes if they are not already present. - * - * @param string $value - * @return string - */ - public static function wrap($value) - { - $value = Str::start($value, "'"); - $value = Str::finish($value, "'"); - - return $value; - } - - /** - * Unwraps the passed string from the passed delimiter. - * - * @param string $value - * @param string $delimiter - * @return string - */ - public static function unwrap($value, $delimiter = "'") - { - if (Str::startsWith($value, $delimiter)) { - $value = Str::replaceFirst($delimiter, '', $value); - } - - if (Str::endsWith($value, $delimiter)) { - $value = Str::replaceLast($delimiter, '', $value); - } - - return $value; - } - - /** - * Combine and clean a malformed array formed from a parsed expression. - * - * @param array $expression - * @return string - */ - public static function clean($expression) - { - return Util::unwrap( - Util::toString($expression, true) - ); - } - - /** - * Dives for an ACF field or sub field and returns the value if it exists. - * - * @param string $field - * @param int $id - * @return mixed - */ - public static function field($field, $id = null) - { - if (! function_exists('acf')) { - return; - } - - if (! empty(get_field($field, $id))) { - return get_field($field, $id); - } - - if (! empty(get_sub_field($field))) { - return get_sub_field($field); - } - - if (! empty(get_field($field, 'option'))) { - return get_field($field, 'option'); - } - - return false; - } - - /** - * Convert expression to a string. - * - * @param mixed $expression - * @param bool $single - * @return string - */ - public static function toString($expression, $single = false) - { - if (! is_array($expression)) { - return self::wrap($expression); - } - - $keys = ''; - - foreach ($expression as $key => $value) { - $keys .= $single ? - self::wrap($value).',' : - self::wrap($key).' => '.self::wrap($value).', '; - } - - $keys = trim(Str::replaceLast(',', '', $keys)); - - if (! $single) { - $keys = Str::start($keys, '['); - $keys = Str::finish($keys, ']'); - } - - return $keys; - } - - /** - * Determine if the expression looks like an array. - * - * @param mixed $expression - * @return bool - */ - public static function isArray($expression) - { - $expression = self::strip($expression); - - return Str::startsWith($expression, '[') && Str::endsWith($expression, ']'); - } -}