Adds `tainacan-admin-extra-request-options` PHP filter to receive extra params to every request header.

This commit is contained in:
mateuswetah 2022-10-18 14:19:58 -03:00
parent 090f65a467
commit 13b0a366dc
2 changed files with 14 additions and 4 deletions

View File

@ -10,7 +10,7 @@ const i18nGet = function (key) {
};
const tainacanErrorHandler = function(error) {
console.log(error)
console.error(error)
if (error.response && error.response.status) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
@ -65,10 +65,10 @@ const tainacanErrorHandler = function(error) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log('Tainacan Error Handler: ', error.request);
console.error('Tainacan Error Handler: ', error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Tainacan Error Handler: ', error.message);
console.error('Tainacan Error Handler: ', error.message);
}
return Promise.reject(error);
}
@ -80,6 +80,11 @@ export const tainacan = axios.create({
if (tainacan_plugin.nonce) {
tainacan.defaults.headers.common['X-WP-Nonce'] = tainacan_plugin.nonce;
}
if (tainacan_plugin.admin_request_options) {
Object.keys(tainacan_plugin.admin_request_options).forEach(requestOption => {
tainacan.defaults.headers[requestOption] = tainacan_plugin.admin_request_options[requestOption];
});
}
tainacan.interceptors.response.use(
(response) => response,
(error) => tainacanErrorHandler(error)

View File

@ -374,6 +374,11 @@ class Admin {
$settings['wp_post_types'] = $wp_post_types;
// Key-valued array with extra options to be passed to every request in the admin (goes the header)
$admin_request_options = [];
$admin_request_options = apply_filters('tainacan-admin-extra-request-options', $admin_request_options);
$settings['admin_request_options'] = $admin_request_options;
return $settings;
}
@ -394,7 +399,7 @@ class Admin {
$admin_options = apply_filters('set_tainacan_admin_options', $_GET);
$admin_options = apply_filters('tainacan-admin-ui-options', $_GET);
$admin_options = json_encode($admin_options);
// TODO move it to a separate file and start the Vue project
echo "<div id='tainacan-admin-app' data-module='admin' data-options='$admin_options'></div>";
}