summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2021-03-10 16:03:23 +0100
committerDaniel Weipert <code@drogueronin.de>2021-03-10 16:03:23 +0100
commit52f7203992e1c8662d421e7287e1e1e5f1c2b7cd (patch)
treea2dab363b45ca31ff678e95f14aab37a44e824b1
Initial commitv1.0.0
-rw-r--r--.gitignore1
-rw-r--r--composer.json20
-rw-r--r--src/UsesWordPressScripts.php71
3 files changed, 92 insertions, 0 deletions
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 @@
+<?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";
+ }
+}