| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) exit; |
| 3 |
|
| 4 |
class Revolution_slider { |
| 5 |
function __construct() { |
| 6 |
// add_filter('berqwp_before_script_optimization', [$this, 'load_slider']); |
| 7 |
} |
| 8 |
|
| 9 |
function load_slider($buffer) { |
| 10 |
$script = " |
| 11 |
<script> |
| 12 |
document.querySelectorAll('rs-module').forEach(function(item){ |
| 13 |
let id = item.getAttribute('id') |
| 14 |
if (id) { |
| 15 |
window.revapi4 = window.revapi4 === undefined || window.revapi4 === null || window.revapi4.length === 0 ? document.getElementById(id) : window.revapi4; |
| 16 |
window.revapi4 = jQuery(window.revapi4); |
| 17 |
let args = bwp_get_rev_args(); |
| 18 |
revapi4.revolutionInit(args); |
| 19 |
|
| 20 |
function bwp_get_rev_args() { |
| 21 |
// Get the script tag by ID |
| 22 |
let scriptTag = document.getElementById('rs-initialisation-scripts'); |
| 23 |
|
| 24 |
if (scriptTag) { |
| 25 |
// Extract the content of the script tag |
| 26 |
var scriptContent = scriptTag.innerHTML; |
| 27 |
|
| 28 |
// Use a regular expression to find the argument object |
| 29 |
var regex = /revapi4\.revolutionInit\((\{[\s\S]*?\})\);/; |
| 30 |
var match = scriptContent.match(regex); |
| 31 |
|
| 32 |
if (match && match[1]) { |
| 33 |
// Extract the JSON string |
| 34 |
var jsonString = match[1] |
| 35 |
.replace(/(\w+)\s*:/g, '\"$1\":') // Add double quotes around property names |
| 36 |
.replace(/'/g, '\"') // Replace single quotes with double quotes |
| 37 |
.replace(/,\s*}/g, '}') // Remove trailing commas before closing brace |
| 38 |
.replace(/,\s*]/g, ']'); // Remove trailing commas before closing bracket |
| 39 |
|
| 40 |
try { |
| 41 |
// Parse the JSON string to an object |
| 42 |
var initArgs = JSON.parse(jsonString); |
| 43 |
|
| 44 |
return initArgs; |
| 45 |
|
| 46 |
// Now you can use initArgs as needed |
| 47 |
} catch (e) { |
| 48 |
console.log('Error parsing JSON:', e); |
| 49 |
} |
| 50 |
} else { |
| 51 |
console.log('Initialization arguments not found.'); |
| 52 |
} |
| 53 |
} else { |
| 54 |
console.log('Script tag with ID \"rs-initialisation-scripts\" not found.'); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
}) |
| 60 |
</script> |
| 61 |
"; |
| 62 |
|
| 63 |
if (str_contains($buffer, '<rs-module')) { |
| 64 |
$buffer = str_replace('</body>', $script . '</body>', $buffer); |
| 65 |
} |
| 66 |
|
| 67 |
return $buffer; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
new Revolution_slider(); |