Skip to content

Commit

Permalink
Cache busting the enqueued JS files with a file hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
ardouglass committed Apr 23, 2020
1 parent 8bd63b7 commit cc88657
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 28 deletions.
2 changes: 1 addition & 1 deletion address-mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Requires at least: 5.4
* Tested up to: 5.4
* Version: 1.0.0
* Version: 1.0.1
*/

// If this file is called directly, abort.
Expand Down
61 changes: 40 additions & 21 deletions backend/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,42 @@ public function html() {
public function enqueue_scripts() {
$current_screen_id = get_current_screen()->id;
if ($current_screen_id == 'toplevel_page_address_mapper') {
wp_enqueue_script(
'address-mapper-admin-scripts',
Config::$plugin_base_url . 'dist/address-mapper.min.js',
[],
null,
true
);
$dirJS = new \DirectoryIterator(realpath(dirname(__FILE__) . '/../dist'));

$this->hydrate_scripts();
foreach ($dirJS as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) === 'js') {
$fullName = basename($file);
$name = substr(
basename($fullName),
0,
strpos(basename($fullName), '.')
);

switch ($name) {
case 'address-mapper':
$deps = null; // array of dependency file basenames to add
break;

default:
$deps = null;
break;
}

wp_enqueue_script(
$name,
Config::$plugin_base_url . 'dist/' . $fullName,
$deps,
null,
true
);
}
}

$this->hydrate_scripts('address-mapper');
}
}

private function hydrate_scripts() {
private function hydrate_scripts($script_name) {
$base_url = '/wp-json/' . Config::$endpoints_base;
$nonce = wp_create_nonce('wp_rest');
$google_maps_api_key = Settings::get_google_maps_api_key()['data'][
Expand All @@ -73,18 +96,14 @@ private function hydrate_scripts() {
$updated_by = get_option('address_mapper_updated_by', null);
$updated_at = get_option('address_mapper_updated_at', null);

wp_localize_script(
'address-mapper-admin-scripts',
'addressMapperApiSettings',
[
'baseUrl' => $base_url,
'nonce' => $nonce,
'googleMapsApiKey' => $google_maps_api_key,
'existingIds' => $points_ids,
'lastUpdatedDate' => $updated_at,
'lastUpdatedUser' => $updated_by
]
);
wp_localize_script($script_name, 'addressMapperApiSettings', [
'baseUrl' => $base_url,
'nonce' => $nonce,
'googleMapsApiKey' => $google_maps_api_key,
'existingIds' => $points_ids,
'lastUpdatedDate' => $updated_at,
'lastUpdatedUser' => $updated_by
]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ardouglass/address-mapper",
"version": "1.0.0",
"version": "1.0.1",
"description": "A WordPress plugin that allows users to upload CSVs of addresses, and then query them using a custom endpoint of the WordPress REST API.",
"type": "wordpress-plugin",
"require": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

219 changes: 218 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cc88657

Please sign in to comment.