diff options
author | Daniel Weipert <code@drogueronin.de> | 2021-03-10 16:03:23 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2021-03-10 16:03:23 +0100 |
commit | 52f7203992e1c8662d421e7287e1e1e5f1c2b7cd (patch) | |
tree | a2dab363b45ca31ff678e95f14aab37a44e824b1 /src/UsesWordPressScripts.php |
Initial commitv1.0.0
Diffstat (limited to 'src/UsesWordPressScripts.php')
-rw-r--r-- | src/UsesWordPressScripts.php | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/UsesWordPressScripts.php b/src/UsesWordPressScripts.php new file mode 100644 index 0000000..e93a01e --- /dev/null +++ b/src/UsesWordPressScripts.php @@ -0,0 +1,71 @@ +<?php + +namespace Dweipert\WpEnqueueAssets; + +trait UsesWordPressScripts +{ + /** + * @var string + */ + protected $buildDir = 'build'; + + /** + * @param string $handle + * @param string $asset + * @param array $deps + * @param bool $inFooter + */ + public function enqueueScript($handle, $asset, $deps = [], $inFooter = false) + { + $assetBaseName = pathinfo($asset, PATHINFO_FILENAME); + $meta = $this->assetsMeta($assetBaseName); + + wp_enqueue_script( + $handle, + $this->assetsUrl($asset), + array_replace($meta['dependencies'] ?? [], $deps), + $meta['version'] ?? false, + $inFooter + ); + } + + /** + * @param string $handle + * @param string $asset + * @param array $deps + * @param string $media + */ + public function enqueueStyle($handle, $asset, $deps = [], $media = 'all') + { + $assetBaseName = pathinfo($asset, PATHINFO_FILENAME); + $meta = $this->assetsMeta($assetBaseName); + + wp_enqueue_style( + $handle, + $this->assetsUrl($asset), + $deps, + $meta['version'] ?? false, + $media + ); + } + + /** + * @param string $asset + * + * @return string + */ + public function assetsUrl($asset) + { + return get_stylesheet_directory_uri() . "/{$this->buildDir}/$asset"; + } + + /** + * @param string $asset + * + * @return mixed + */ + public function assetsMeta($asset) + { + return include get_stylesheet_directory() . "/{$this->buildDir}/$asset.asset.php"; + } +} |