diff options
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"; + } +} |