diff options
-rw-r--r-- | src/PluginUsesWordPressScripts.php | 35 | ||||
-rw-r--r-- | src/ThemeUsesWordPressScripts.php | 28 | ||||
-rw-r--r-- | src/UsesWordPressScripts.php | 20 |
3 files changed, 70 insertions, 13 deletions
diff --git a/src/PluginUsesWordPressScripts.php b/src/PluginUsesWordPressScripts.php new file mode 100644 index 0000000..3a60828 --- /dev/null +++ b/src/PluginUsesWordPressScripts.php @@ -0,0 +1,35 @@ +<?php + +namespace Dweipert\WpEnqueueAssets; + +trait PluginUsesWordPressScripts +{ + use UsesWordPressScripts; + + /** + * Assuming the main plugin class file is in a sub-folder + * + * @var string + */ + protected string $pluginDir = __DIR__; + + /** + * @param string $asset + * + * @return string + */ + public function assetsUrl(string $asset): string + { + return plugin_dir_url($this->pluginDir) . "{$this->buildDir}/$asset"; + } + + /** + * @param string $asset + * + * @return array + */ + public function assetsMeta(string $asset): array + { + return include plugin_dir_path($this->pluginDir) . "{$this->buildDir}/$asset.asset.php"; + } +} diff --git a/src/ThemeUsesWordPressScripts.php b/src/ThemeUsesWordPressScripts.php new file mode 100644 index 0000000..21cc501 --- /dev/null +++ b/src/ThemeUsesWordPressScripts.php @@ -0,0 +1,28 @@ +<?php + +namespace Dweipert\WpEnqueueAssets; + +trait ThemeUsesWordPressScripts +{ + use UsesWordPressScripts; + + /** + * @param string $asset + * + * @return string + */ + public function assetsUrl(string $asset): string + { + return get_stylesheet_directory_uri() . "/{$this->buildDir}/$asset"; + } + + /** + * @param string $asset + * + * @return array + */ + public function assetsMeta(string $asset): array + { + return include get_stylesheet_directory() . "/{$this->buildDir}/$asset.asset.php"; + } +} diff --git a/src/UsesWordPressScripts.php b/src/UsesWordPressScripts.php index e93a01e..c565575 100644 --- a/src/UsesWordPressScripts.php +++ b/src/UsesWordPressScripts.php @@ -7,7 +7,7 @@ trait UsesWordPressScripts /** * @var string */ - protected $buildDir = 'build'; + protected string $buildDir = 'build'; /** * @param string $handle @@ -15,7 +15,7 @@ trait UsesWordPressScripts * @param array $deps * @param bool $inFooter */ - public function enqueueScript($handle, $asset, $deps = [], $inFooter = false) + public function enqueueScript(string $handle, string $asset, array $deps = [], bool $inFooter = false) { $assetBaseName = pathinfo($asset, PATHINFO_FILENAME); $meta = $this->assetsMeta($assetBaseName); @@ -35,7 +35,7 @@ trait UsesWordPressScripts * @param array $deps * @param string $media */ - public function enqueueStyle($handle, $asset, $deps = [], $media = 'all') + public function enqueueStyle(string $handle, string $asset, array $deps = [], string $media = 'all') { $assetBaseName = pathinfo($asset, PATHINFO_FILENAME); $meta = $this->assetsMeta($assetBaseName); @@ -52,20 +52,14 @@ trait UsesWordPressScripts /** * @param string $asset * - * @return string + * @return string Url to asset in $buildDir */ - public function assetsUrl($asset) - { - return get_stylesheet_directory_uri() . "/{$this->buildDir}/$asset"; - } + abstract public function assetsUrl(string $asset): string; /** * @param string $asset * - * @return mixed + * @return array Data from $asset.asset.php in $buildDir */ - public function assetsMeta($asset) - { - return include get_stylesheet_directory() . "/{$this->buildDir}/$asset.asset.php"; - } + abstract public function assetsMeta(string $asset): array; } |