/**
* Auto set CSRF token to ajaxSetup so all jQuery ajax call will contains CSRF token.
*
* @param string $name The CSRF meta tag name.
*
* @return void
*
* @throws \InvalidArgumentException
*
* @since 3.8.0
*/
public static function token($name = 'csrf.token')
{
// Only load once
if (!empty(static::$loaded[__METHOD__][$name])) {
return;
}
static::framework();
HTMLHelper::_('form.csrf', $name);
$doc = Factory::getDocument();
$doc->addScriptDeclaration(<<<JS
;(function (\$) {
\t\$.ajaxSetup({
\t\theaders: {
\t\t\t'X-CSRF-Token': Joomla.getOptions('{$name}')
\t\t}
\t});
})(jQuery);
JS
);
static::$loaded[__METHOD__][$name] = true;
}