Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions assets/js/tours.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,28 @@ import Shepherd from 'shepherd.js';
},
});

const markTourFinished = function() {

$.ajax({
url: ajaxurl,
data: {
action: 'wu_mark_tour_as_finished',
tour_id,
nonce: wu_tours_vars.nonce,
},
});
const markTourFinished = function() {

const data = new URLSearchParams({
action: 'wu_mark_tour_as_finished',
tour_id,
nonce: wu_tours_vars.nonce,
});

};
/*
* sendBeacon() queues the request at the OS/browser level and
* survives page navigation — so the setting is saved even if the
* user refreshes immediately after closing the tour (which would
* cancel an in-flight $.ajax() request). Falls back to $.ajax()
* for browsers without sendBeacon support.
*/
if (navigator.sendBeacon) {
navigator.sendBeacon(ajaxurl, data);
} else {
$.ajax({ url: ajaxurl, data: Object.fromEntries(data) });
}

};

window[ tour_id ].on('complete', markTourFinished);
window[ tour_id ].on('cancel', markTourFinished);
Expand Down
2 changes: 1 addition & 1 deletion assets/js/tours.min.js

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

57 changes: 29 additions & 28 deletions inc/ui/class-tours.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,38 +114,39 @@ public function enqueue_scripts(): void {

if ($this->has_tours()) {
/*
* We cannot use wp_localize_script() on a module script (wu-tours), and
* we cannot rely on wu-admin being enqueued on every admin page — since
* PR #433 it is only enqueued on WP Ultimo pages. The network dashboard
* (index.php, hook suffix dashboard-network) is not a WP Ultimo page, so
* wu-admin is absent there and localizing onto it silently does nothing,
* leaving wu_tours undefined when tours.js executes.
* We cannot use wp_localize_script() on a module script (wu-tours).
*
* Fix: use wp_add_inline_script() on 'underscore', which is a WordPress
* core script always present in the admin. This injects wu_tours and
* wu_tours_vars as globals immediately after underscore loads, making them
* available to the wu-tours module regardless of whether wu-admin is
* enqueued. See https://core.trac.wordpress.org/ticket/60234.
* We also cannot use wp_add_inline_script() on 'underscore' here,
* because enqueue_scripts() is hooked to in_admin_footer — by the time
* that hook fires, the <head> scripts (including underscore) have
* already been printed, so wp_add_inline_script() is silently ignored,
* leaving wu_tours undefined when tours.min.js executes.
*
* Fix: call wp_print_inline_script_tag() directly in in_admin_footer.
* That hook fires before admin_footer, which is where WordPress prints
* enqueued script modules, so wu_tours is guaranteed to be defined
* before the wu-tours module runs. wp_print_inline_script_tag() is
* available since WP 5.7; this code path already requires WP 6.5+
* (for wp_enqueue_script_module), so no version guard is needed.
*/
wp_enqueue_script('underscore');

$inline_data = sprintf(
'var wu_tours = %s; var wu_tours_vars = %s;',
wp_json_encode($this->tours),
wp_json_encode(
[
'ajaxurl' => wu_ajax_url(),
'nonce' => wp_create_nonce('wu_tour_finished'),
'i18n' => [
'next' => __('Next', 'ultimate-multisite'),
'finish' => __('Close', 'ultimate-multisite'),
],
]
)
wp_print_inline_script_tag(
sprintf(
'var wu_tours = %s; var wu_tours_vars = %s;',
wp_json_encode($this->tours),
wp_json_encode(
[
'ajaxurl' => wu_ajax_url(),
'nonce' => wp_create_nonce('wu_tour_finished'),
'i18n' => [
'next' => __('Next', 'ultimate-multisite'),
'finish' => __('Close', 'ultimate-multisite'),
],
]
)
),
['id' => 'wu-tours-data']
);

wp_add_inline_script('underscore', $inline_data, 'after');

wp_enqueue_script_module('wu-tours');
wp_enqueue_style('shepherd');
}
Expand Down
Loading