diff --git a/docs/usage/acf.md b/docs/usage/acf.md index 886a9da..b8e04f8 100644 --- a/docs/usage/acf.md +++ b/docs/usage/acf.md @@ -37,7 +37,7 @@ To echo a field for a specific post that is also an array, you can pass the post @endfield ``` @@ -142,7 +142,7 @@ To retrieve fields for a specific post, you can pass a post ID as a second param ``` -If the sub field is an array, you can pass the key as a second parameter: +If the sub field is an array, you can pass the key(s) as additional parameters: ```php ``` +```php + +``` + More usage of `@sub` can be found alongside the examples of the repeatable fields listed above. ## @hassub @@ -166,7 +176,7 @@ More usage of `@sub` can be found alongside the examples of the repeatable field @endsub ``` -If the sub field you are checking against is an array, you can pass the array key as a second parameter: +If the sub field you are checking against is an array, you can pass the key(s) as additional parameters: ```php @hassub('image', 'url') @@ -174,6 +184,12 @@ If the sub field you are checking against is an array, you can pass the array ke @endsub ``` +```php +@hassub('image', 'sizes', 'thumbnail') + @sub('image', 'alt') +@endsub +``` + ## @issub `@issub` is a simple conditional for checking if your sub field equals a specified value. It can be closed using `@endsub`. diff --git a/docs/usage/wordpress.md b/docs/usage/wordpress.md index cde00f5..8db636f 100644 --- a/docs/usage/wordpress.md +++ b/docs/usage/wordpress.md @@ -85,7 +85,7 @@ If `@query` is not used and an argument is not passed to `@posts`, it will use t @title ``` -To echo the title of a specific post, you can pass the post ID or a `WP_Post` instance as a second parameter: +To echo the title of a specific post, you can pass the post ID or a `WP_Post` instance as a parameter: ```php @title(1) @@ -108,6 +108,55 @@ To echo the title of a specific post, you can pass the post ID or a `WP_Post` in @excerpt ``` +## @permalink + +`@permalink` echo's the current posts URL using [`get_permalink()`](https://developer.wordpress.org/reference/functions/get_permalink/). + +```php +@permalink +``` + +To echo the URL of a specific post, you can pass the post ID or a `WP_Post` instance as a parameter: + +```php +@permalink(1) +@permalink(get_post(1)) +``` + +## @thumbnail + +`@thumbnail` echo's the current posts featured image using [`get_the_post_thumbnail`](https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/). By default, it passes `thumbnail` as the size. + +```php +@thumbnail +``` + +To echo the featured image of a specific post, you can pass the post ID or a `WP_Post` instance as the first parameter: + +```php +@thumbnail(1) +@thumbnail(get_post(1)) +``` + +To echo the featured image with a specific size, you can either pass it as a parameter by it's self, or as the second parameter when a post ID or `WP_Post` instance is present: + +```php +@thumbnail('full') +@thumbnail(1, 'full') +@thumbnail(get_post(1), 'full') +``` + +To echo the featured image URL (without img markup), you can pass `false` as the last parameter on any of the above options: + +```php +My Image +Post 1 +Post 1 +Full Image +Post 1's Full Image +Post 1's Full Image +``` + ## @author `@author` echo's the author of the current posts display name. @@ -116,7 +165,7 @@ To echo the title of a specific post, you can pass the post ID or a `WP_Post` in @author ``` -To echo the display name of a specific author, you can pass the author's ID as a second parameter: +To echo the display name of a specific author, you can pass the author's ID as a parameter: ```php @author(1) @@ -134,10 +183,10 @@ To echo the display name of a specific author, you can pass the author's ID as a ``` -To echo the URL of a specific author, you can pass the author's ID as a second parameter: +To echo the URL of a specific author, you can pass the author's ID as a parameter: ```php -@author +@author ``` ## @published @@ -203,6 +252,85 @@ To format the modified date of a specific post, you can pass the format as the f @modified('F j, Y', get_post(1)) ``` +## @category + +`@category` echo's the first category of the current post. + +```php +@category +``` + +To echo the category as a link, pass `true` as a parameter: + +```php +@category(true) +``` + +To echo the category of a specific post, pass the post ID as a parameter: + +```php +@category(1) +``` + +To echo the category of a specific post as a link, pass the post ID as the first parameter, and `true` as the second parameter: + +```php +@category(1, true) +``` + +## @categories + +`@categories` echo's a comma seperated list of the current post's categories. + +```php +@categories +``` + +To echo the categories of a specific post, pass the post ID as the first parameter: + +```php +@categories(1) +``` + +Similar to `@category`, if you would like to return the categories as links, pass `true` as the first parameter when by it's self, or as the second parameter when a post ID is present: + +```php +@categories(true) +@categories(1, true) +``` + +## @term + +`@term` echo's the taxonomy term of the current post. If multiple terms are present, it will echo the first term returned in the array. + +```php +@term('genre') +``` + +Similar to `@category`, if you would like to return the terms of a specific post or as links, you can follow the same syntax, except keeping the taxonomy name as the first parameter: + +```php +@term('genre', 1) +@term('genre', 1, false) +@term('genre', false) +``` + +## @terms + +`@terms` echo's a comma seperated list of the taxonomy terms of the current post. + +```php +@terms('genre') +``` + +It accepts the same parameters as `@term`: + +```php +@terms('genre', 1) +@terms('genre', 1, false) +@terms('genre', false) +``` + ## @shortcode `@shortcode` echo's the specified shortcode using [`do_shortcode()`](https://developer.wordpress.org/reference/functions/do_shortcode/). @@ -211,6 +339,16 @@ To format the modified date of a specific post, you can pass the format as the f @shortcode('[my-shortcode]') ``` +## @role + +`@role` is a simple conditional that allows you to display specific content only to users who are logged in and have a specific role. With [`wp_get_current_user()->roles`](https://codex.wordpress.org/Function_Reference/wp_get_current_user) returning an array of roles in all lowercase, the passed role is automatically lowercased using `strtolower`. It can be closed using `@endrole`. + +```php +@role('author') + This content is only displayed to Authors. +@endrole +``` + ## @user `@user` is a simple `is_user_logged_in()` conditional to display specific content only when a user is logged in. It can be closed using `@enduser`. diff --git a/src/Directives.php b/src/Directives.php index d7e8c76..1c1f51a 100644 --- a/src/Directives.php +++ b/src/Directives.php @@ -30,28 +30,13 @@ class Directives */ public function __construct() { - /** - * Collect Directives into a flattened array. - */ - $directives = collect($this->directives) - ->flatMap(function ($directive) { - if ($directive === 'ACF' && ! function_exists('acf')) { - return; - } - - return $this->get($directive); - }); - - /** - * Register Directives with Blade - */ - add_action('after_setup_theme', function () use ($directives) { - if (! function_exists('App\sage')) { + add_action('after_setup_theme', function () { + if (! function_exists('App\sage') && ! function_exists('Roots\app')) { return; } - collect($directives)->each(function ($directive, $function) { - \App\sage('blade')->compiler()->directive($function, $directive); + collect($this->directives())->each(function ($directive, $function) { + $this->blade()->directive($function, $directive); }); }, 20); } @@ -62,12 +47,45 @@ public function __construct() * @param string $name * @return array */ - public function get($name) + protected function get($name) { if (file_exists($directives = __DIR__.'/Directives/'.$name.'.php')) { return require_once($directives); } } + + /** + * Returns a collection of directives. + * + * @return \Illuminate\Support\Collection + */ + protected function directives() + { + return collect($this->directives) + ->flatMap(function ($directive) { + if ($directive === 'ACF' && ! function_exists('acf')) { + return; + } + + return $this->get($directive); + }); + } + + /** + * Returns the Blade compiler. + * + * @return \Illuminate\Support\Facades\Blade + */ + protected function blade() + { + if (function_exists('App\sage')) { + return \App\sage('blade')->compiler(); + } + + if (function_exists('Roots\app')) { + return \Roots\app()['view']->getEngineResolver()->resolve('blade')->getCompiler(); + } + } } if (function_exists('add_action')) { diff --git a/src/Directives/ACF.php b/src/Directives/ACF.php index 1016b90..ea4d0e1 100644 --- a/src/Directives/ACF.php +++ b/src/Directives/ACF.php @@ -111,6 +111,10 @@ if (str_contains($expression, ',')) { $expression = Util::parse($expression); + if (! empty($expression->get(2))) { + return "get(0)})[{$expression->get(1)}][{$expression->get(2)}]; ?>"; + } + return "get(0)})[{$expression->get(1)}]; ?>"; } @@ -121,6 +125,10 @@ if (str_contains($expression, ',')) { $expression = Util::parse($expression); + if (! empty($expression->get(2))) { + return "get(0)})[{$expression->get(1)}][{$expression->get(2)}]) : ?>"; + } + return "get(0)})[{$expression->get(1)}]) : ?>"; } diff --git a/src/Directives/WordPress.php b/src/Directives/WordPress.php index 0a803ce..bdb5471 100644 --- a/src/Directives/WordPress.php +++ b/src/Directives/WordPress.php @@ -63,7 +63,7 @@ /* |--------------------------------------------------------------------- - | @title / @content / @excerpt + | @title / @content / @excerpt / @permalink / @thumbnail |--------------------------------------------------------------------- */ @@ -83,6 +83,46 @@ return ""; }, + 'permalink' => function ($expression) { + return ""; + }, + + 'thumbnail' => function ($expression) { + if (! empty($expression)) { + $expression = Util::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)}); ?>"; + } + + return "get(0)}, is_numeric({$expression->get(1)}) ? [{$expression->get(1)}, {$expression->get(1)}] : {$expression->get(1)}); ?>"; + } + + if (! empty($expression->get(1))) { + if ($expression->get(1) === 'false') { + return "get(0)}, 'thumbnail'); ?>"; + } + + return "get(0)}, is_numeric({$expression->get(1)}) ? [{$expression->get(1)}, {$expression->get(1)}] : {$expression->get(1)}); ?>"; + } + + if (! empty($expression->get(0))) { + if ($expression->get(0) === 'false') { + return ""; + } + + if (is_numeric($expression->get(0))) { + return "get(0)}, 'thumbnail'); ?>"; + } + + return "get(0)}); ?>"; + } + } + + return ""; + }, + /* |--------------------------------------------------------------------- | @author / @authorurl / @published / @modified @@ -107,11 +147,11 @@ 'published' => function ($expression) { if (! empty($expression)) { - return "". + return "". "". - "". - - ""; + "". + "". + ""; } return ""; @@ -121,9 +161,9 @@ if (! empty($expression)) { return "". "". - "". - - ""; + "". + "". + ""; } return ""; @@ -131,10 +171,127 @@ /* |--------------------------------------------------------------------- - | @user / @enduser / @guest / @endguest + | @category / @categories / @term / @terms + |--------------------------------------------------------------------- + */ + + 'category' => function ($expression) { + $expression = Util::parse($expression); + + if ($expression->get(1) === 'true') { + return "get(0)}))->isNotEmpty()) : ?>". + "get(0)}))->shift()->cat_ID); ?>\">". + "get(0)}))->shift()->name; ?>". + "". + ""; + } + + if (! empty($expression->get(0))) { + if ($expression->get(0) === 'true') { + return "isNotEmpty()) : ?>". + "shift()->cat_ID); ?>\">". + "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))) { + return "get(1)}, {$expression->get(0)}))->isNotEmpty()) : ?>". + "get(1)}, {$expression->get(0)}))->shift()->term_ID); ?>\">". + "get(1)}, {$expression->get(0)}))->shift()->name(); ?>". + "". + ""; + } + + if (! empty($expression->get(1))) { + if ($expression->get(1) === 'true') { + return "get(0)}))->isNotEmpty()) : ?>". + "get(0)}))->shift()->term_ID); ?>\">". + "get(0)}))->shift()->name(); ?>". + "". + ""; + } + + return "get(1)}, {$expression->get(0)}))->isNotEmpty()) : ?>". + "get(1)}, {$expression->get(0)}))->shift()->name(); ?>". + ""; + } + + if (! empty($expression->get(0))) { + return "get(0)}))->isNotEmpty()) : ?>". + "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)}, '', ', ')); ?>"; + } + }, + + /* + |--------------------------------------------------------------------- + | @role / @endrole / @user / @enduser / @guest / @endguest |--------------------------------------------------------------------- */ + 'role' => function ($expression) { + $expression = Util::parse($expression); + + return "get(0)}), (array) wp_get_current_user()->roles)) : ?>"; + }, + + 'endrole' => function () { + return ""; + }, + 'user' => function () { return ""; }, diff --git a/yarn.lock b/yarn.lock index b840883..a1bdb52 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5317,7 +5317,7 @@ merge2@^1.2.3: resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.2.3.tgz#7ee99dbd69bb6481689253f018488a1b902b0ed5" integrity sha512-gdUU1Fwj5ep4kplwcmftruWofEFt6lfpkkr3h860CXbAB9c3hGb55EOL2ali0Td5oebvW0E1+3Sr+Ur7XfKpRA== -"metalsmith-start@github:rstacruz/metalsmith-start#e88a7cd": +metalsmith-start@rstacruz/metalsmith-start#e88a7cd: version "2.0.1" resolved "https://codeload.github.com/rstacruz/metalsmith-start/tar.gz/e88a7cdbb20aac1db176c121b429008073d8e9eb" dependencies: