Skip to content

Commit

Permalink
feat(directive): Add @thememod directive (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
czernika authored Aug 1, 2023
1 parent 59ef697 commit 49178d3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/usage/wordpress.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,12 @@ It comes with two assisting directives `@hassidebar` and `@endhassidebar` that c
@sidebar('sidebar-primary')
@endhassidebar
```

## @thememod

`@thememod` echoes the result of [`get_theme_mod`](https://developer.wordpress.org/reference/functions/get_theme_mod/) function accepting theme modification name and optional default value

```php
@thememod('header-bg-color')
@thememod('header-bg-color', '#fff')
```
19 changes: 19 additions & 0 deletions src/Directives/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,23 @@

return "<?php _e({$expression[0]}, {$expression[1]}); ?>";
},

/*
|---------------------------------------------------------------------
| @thememod
|---------------------------------------------------------------------
*/

'thememod' => function ($expression) {
$expression = Util::parse($expression);

$mod = $expression->get(0);
$default = $expression->get(1);

if (! empty($default)) {
return "<?php echo get_theme_mod({$mod}, {$default}); ?>";
}

return "<?php echo get_theme_mod({$mod}); ?>";
},
];
18 changes: 18 additions & 0 deletions tests/Unit/WordPressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -827,3 +827,21 @@
expect($compiled)->toBe("<?php _e('Hello World', 'sage'); ?>");
});
});

describe('@thememod', function () {
it('compiles correctly', function () {
$directive = "@thememod('mod')";

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php echo get_theme_mod('mod'); ?>");
});

it('compiles correctly with default value', function () {
$directive = "@thememod('mod', 'default')";

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php echo get_theme_mod('mod', 'default'); ?>");
});
});

0 comments on commit 49178d3

Please sign in to comment.