Skip to content

Commit

Permalink
Added get_asset_file_uri helper
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkla committed Aug 1, 2022
1 parent 2b804c1 commit 1491e56
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
11 changes: 5 additions & 6 deletions public/themes/wordplate/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@

<?php if (
wp_get_environment_type() === 'local' &&
is_array(wp_remote_get('http://' . $_SERVER['REMOTE_ADDR'] . ':5173'))
is_array(wp_remote_get('http://localhost:5173/'))
) : ?>
<script type="module" src="http://<?= $_SERVER['REMOTE_ADDR'] ?>:5173/@vite/client"></script>
<script type="module" src="http://<?= $_SERVER['REMOTE_ADDR'] ?>:5173/resources/scripts/index.js"></script>
<script type="module" src="http://localhost:5173/@vite/client"></script>
<script type="module" src="http://localhost:5173/resources/scripts/index.js"></script>
<?php else : ?>
<?php $manifest = json_decode(file_get_contents(get_theme_file_path('assets/manifest.json')), true); ?>
<script type="module" src="<?= get_theme_file_uri('assets/' . $manifest['resources/scripts/index.js']['file']) ?>" defer></script>
<link rel="stylesheet" href="<?= get_theme_file_uri('assets/' . $manifest['resources/scripts/index.js']['css'][0]) ?>">
<script type="module" src="<?= get_asset_file_uri('index.js') ?>" defer></script>
<link rel="stylesheet" href="<?= get_asset_file_uri('index.css') ?>">
<?php endif; ?>

<?php wp_head(); ?>
Expand Down
14 changes: 14 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,17 @@ function env(string $key, mixed $default = null): mixed
return $value;
}
}

if (!function_exists('get_asset_file_uri')) {
function get_asset_file_uri(string $file = ''): string
{
$path = get_theme_file_path('assets/' . $file);
$url = get_theme_file_uri('assets/' . $file);

if (file_exists($path)) {
return $url . '?v=' . hash('fnv1a32', (string) filemtime($path));
}

return $url;
}
}

0 comments on commit 1491e56

Please sign in to comment.