| @@ -1,0 +1,70 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikWP - Libraries | |
| 4 | + * @subpackage adapter.html | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. | |
| 7 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Utility class for jQuery behaviors. | |
| 16 | + * | |
| 17 | + * @since 10.0 | |
| 18 | + */ | |
| 19 | +abstract class JHtmlJQuery | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * Includes jQuery framework. | |
| 23 | + * | |
| 24 | + * @return void | |
| 25 | + */ | |
| 26 | + public static function framework() | |
| 27 | + { | |
| 28 | + // Do nothing, jQuery framework is included by default. | |
| 29 | + // Used to avoid portability errors. | |
| 30 | + } | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Auto set CSRF token to ajaxSetup so all jQuery ajax call will contains CSRF token. | |
| 34 | + * | |
| 35 | + * @return void | |
| 36 | + * | |
| 37 | + * @since 10.1.33 | |
| 38 | + */ | |
| 39 | + public static function token() | |
| 40 | + { | |
| 41 | + static $loaded = false; | |
| 42 | + | |
| 43 | + // Only load once | |
| 44 | + if ($loaded) | |
| 45 | + { | |
| 46 | + return; | |
| 47 | + } | |
| 48 | + | |
| 49 | + // get session token | |
| 50 | + JLoader::import('adapter.session.session'); | |
| 51 | + $token = JSession::getFormToken(); | |
| 52 | + | |
| 53 | + // escape token for JS usage | |
| 54 | + $token = esc_js($token); | |
| 55 | + | |
| 56 | + JFactory::getDocument()->addScriptDeclaration( | |
| 57 | +<<<JS | |
| 58 | +(function($) { | |
| 59 | + $.ajaxSetup({ | |
| 60 | + headers: { | |
| 61 | + 'X-CSRF-Token': '$token' | |
| 62 | + } | |
| 63 | + }); | |
| 64 | +})(jQuery); | |
| 65 | +JS | |
| 66 | + ); | |
| 67 | + | |
| 68 | + $loaded = true; | |
| 69 | + } | |
| 70 | +} | |