From 0aa515af872b74f157e27ffaf20eaef90cef593c Mon Sep 17 00:00:00 2001 From: Torben Lundsgaard Date: Wed, 17 Jan 2024 10:30:21 +0100 Subject: [PATCH] Refactor Util for extendability --- src/Common/Util.php | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Common/Util.php b/src/Common/Util.php index a4f6bad..65483ac 100644 --- a/src/Common/Util.php +++ b/src/Common/Util.php @@ -21,13 +21,31 @@ final class Util { */ public $rest_api_server; + /** + * Asset path + * + * @var string + */ + public $asset_path; + + /** + * Asset URL + * + * @var string + */ + public $asset_url; + /** * Constructor. * * @param RestAPIServer $rest_api_server Instance of RestAPIServer. + * @param string $path The plugin path. + * @param string $url The plugin URL. */ - public function __construct( RestAPIServer $rest_api_server ) { + public function __construct( RestAPIServer $rest_api_server, string $path = GTMKIT_PATH, string $url = GTMKIT_URL ) { $this->rest_api_server = $rest_api_server; + $this->asset_path = $path . 'assets/'; + $this->asset_url = $url . 'assets/'; } /** @@ -267,7 +285,7 @@ public function enqueue_script( string $handle, string $script, bool $has_asset_ $ver = $this->get_plugin_version(); if ( $has_asset_file ) { - $file = GTMKIT_PATH . 'assets/' . substr_replace( $script, '.asset.php', - strlen( '.js' ) ); + $file = $this->asset_path . substr_replace( $script, '.asset.php', - strlen( '.js' ) ); if ( file_exists( $file ) ) { $deps_file = require $file; $deps = $deps_file['dependencies']; @@ -278,6 +296,6 @@ public function enqueue_script( string $handle, string $script, bool $has_asset_ $deps[] = 'gtmkit'; $deps[] = 'gtmkit-container'; - \wp_enqueue_script( $handle, GTMKIT_URL . 'assets/' . $script, $deps, $ver, $args ); + \wp_enqueue_script( $handle, $this->asset_url . $script, $deps, $ver, $args ); } }