From 52f7203992e1c8662d421e7287e1e1e5f1c2b7cd Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 10 Mar 2021 16:03:23 +0100 Subject: Initial commit --- .gitignore | 1 + composer.json | 20 +++++++++++++ src/UsesWordPressScripts.php | 71 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/UsesWordPressScripts.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61ead86 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..864507c --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "dweipert/wp-enqueue-assets", + "type": "library", + "description": "Helper functions to use with @wordpress/scripts js module", + "license": "GPL-2.0-or-later", + "authors": [ + { + "name": "Daniel Weipert", + "email": "code@drogueronin.de" + } + ], + "require": { + "php": ">=7.4" + }, + "autoload": { + "psr-4": { + "Dweipert\\WpEnqueueAssets\\": "src/" + } + } +} 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 @@ +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"; + } +} -- cgit v1.2.3