Skip to content

Commit

Permalink
Refactor Util for extendability
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed Jan 17, 2024
1 parent a2e0aba commit 0aa515a
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/Common/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
}

/**
Expand Down Expand Up @@ -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'];
Expand All @@ -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 );
}
}

0 comments on commit 0aa515a

Please sign in to comment.