Refresh nonce via heartbeats [MAILPOET-2839]

This commit is contained in:
wxa
2020-05-22 00:11:05 +03:00
committed by Veljko V
parent 2b4afbcda9
commit 70e3d1e459
2 changed files with 19 additions and 0 deletions

View File

@ -15,6 +15,14 @@ function requestFailed(errorMessage, xhr) {
};
}
// Renew MailPoet nonce via heartbeats to keep auth
// for AJAX requests on long-open pages
jQuery(document).on('heartbeat-tick.mailpoet-ajax', (event, data) => {
if (data.mailpoet_token) {
window.mailpoet_token = data.mailpoet_token;
}
});
MailPoet.Ajax = {
version: 0.5,
options: {},

View File

@ -84,6 +84,12 @@ class API {
'wp_ajax_nopriv_mailpoet',
[$this, 'setupAjax']
);
// nonce refreshing via heartbeats
WPFunctions::get()->addAction(
'wp_refresh_nonces',
[$this, 'addTokenToHeartbeatResponse']
);
}
public function setupAjax() {
@ -234,6 +240,11 @@ class API {
);
}
public function addTokenToHeartbeatResponse($response) {
$response['mailpoet_token'] = Security::generateToken();
return $response;
}
public function addEndpointNamespace($namespace, $version) {
if (!empty($this->endpointNamespaces[$version][$namespace])) return;
$this->endpointNamespaces[$version][] = $namespace;