Skip to content

Commit

Permalink
v1.0.7
Browse files Browse the repository at this point in the history
Add Sage 10 support 🎈 
`@sub` and `@hassub` can now accept a third parameter for deeper nested arrays (Fixes #12)
New `@permalink`, `@category`, `@term`, `@role`, and `@endrole` directives.
Change `get()` to a protected function.
Fix a few typos in the docs.
  • Loading branch information
Log1x committed Apr 29, 2019
1 parent 8dfa3f5 commit 2fd945b
Show file tree
Hide file tree
Showing 6 changed files with 374 additions and 37 deletions.
22 changes: 19 additions & 3 deletions docs/usage/acf.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ To echo a field for a specific post that is also an array, you can pass the post
<ul>
@fields('list')
<li>@sub('item')</li>
@fields
@endfields
</ul>
@endfield
```
Expand Down Expand Up @@ -142,7 +142,7 @@ To retrieve fields for a specific post, you can pass a post ID as a second param
</ul>
```

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
<ul class="slider">
Expand All @@ -154,6 +154,16 @@ If the sub field is an array, you can pass the key as a second parameter:
</ul>
```

```php
<ul class="slider">
@fields('slides')
<li class="slide">
<img src="@sub('image', 'sizes', 'thumbnail')" alt="@sub('image', 'alt')" />
</li>
@endfields
</ul>
```

More usage of `@sub` can be found alongside the examples of the repeatable fields listed above.

## @hassub
Expand All @@ -166,14 +176,20 @@ 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')
<img src="@sub('image', 'url')" alt="@sub('image', 'alt')" />
@endsub
```

```php
@hassub('image', 'sizes', 'thumbnail')
<img src="@sub('image', 'sizes', 'thumbnail')" alt="@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`.
Expand Down
146 changes: 142 additions & 4 deletions docs/usage/wordpress.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
<img src="@thumbnail(false)" alt="My Image" />
<img src="@thumbnail(1, false)" alt="Post 1" />
<img src="@thumbnail(get_post(1), false)" alt="Post 1" />
<img src="@thumbnail('full', false)" alt="Full Image" />
<img src="@thumbnail(1, 'full', false)" alt="Post 1's Full Image" />
<img src="@thumbnail(get_post(1), 'full', false)" alt="Post 1's Full Image" />
```

## @author

`@author` echo's the author of the current posts display name.
Expand All @@ -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)
Expand All @@ -134,10 +183,10 @@ To echo the display name of a specific author, you can pass the author's ID as a
</span>
```

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
<a href="@authorurl">@author</a>
<a href="@authorurl(2)">@author</a>
```

## @published
Expand Down Expand Up @@ -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/).
Expand All @@ -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`.
Expand Down
58 changes: 38 additions & 20 deletions src/Directives.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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')) {
Expand Down
8 changes: 8 additions & 0 deletions src/Directives/ACF.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@
if (str_contains($expression, ',')) {
$expression = Util::parse($expression);

if (! empty($expression->get(2))) {
return "<?= get_sub_field({$expression->get(0)})[{$expression->get(1)}][{$expression->get(2)}]; ?>";
}

return "<?= get_sub_field({$expression->get(0)})[{$expression->get(1)}]; ?>";
}

Expand All @@ -121,6 +125,10 @@
if (str_contains($expression, ',')) {
$expression = Util::parse($expression);

if (! empty($expression->get(2))) {
return "<?php if (get_sub_field({$expression->get(0)})[{$expression->get(1)}][{$expression->get(2)}]) : ?>";
}

return "<?php if (get_sub_field({$expression->get(0)})[{$expression->get(1)}]) : ?>";
}

Expand Down
Loading

0 comments on commit 2fd945b

Please sign in to comment.