PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.15.2
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.15.2
1.19.8 1.19.7 1.19.6 1.19.5 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.12.0 1.13.0 1.14.0 1.15.0 1.15.1 1.15.2 1.15.3 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.16.7 1.16.8 1.17.0 1.17.6 1.17.7 1.17.8 1.17.9 1.18.0 1.18.1 1.18.2 1.18.3 1.18.4 1.18.5 1.18.6 1.18.7 1.18.8 1.18.9 1.19.0 1.19.1 1.19.2 1.19.3 1.19.4 1.3.19 1.3.20 1.4.0 1.4.1 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.16 1.5.17 1.5.18 1.5.19 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.8.1 1.8.3 1.9.0 1.9.1 1.9.2
nitropack / classes / Feature / AjaxShortcodes.php
nitropack / classes / Feature Last commit date
AjaxShortcodes.php 2 years ago SubrequestCache.php 2 years ago
AjaxShortcodes.php
89 lines
1 <?php
2 namespace NitroPack\Feature;
3
4 class AjaxShortcodes {
5 const STAGE = "very_early";
6
7 private $shortcodes = array();
8 private $nitroScLoader = <<<'SCRIPT'
9 (() => {
10 document.addEventListener("DOMContentLoaded", () => {
11 let ajaxElements = document.querySelectorAll(".nitro-sc-load");
12 let reqData = new Set();
13 ajaxElements.forEach((el) => {
14 reqData.add(el.getAttribute("data-sc-meta"));
15 });
16 jQuery.ajax({
17 type: "post",
18 url: `${window.location.origin}/wp-admin/admin-ajax.php`,
19 data: {
20 action: "nitro_shortcode_ajax",
21 data: Array.from(reqData),
22 },
23 complete: function (response) {
24 let scData = JSON.parse(response.responseText).data
25 for (let key in scData) {
26 document.querySelectorAll(`.nitro-sc-load[data-sc-meta='${key}']`).forEach( el => { el.outerHTML = scData[key]; });
27 console.log(key, scData[key]);
28 }
29 },
30 });
31 });
32 })()
33 SCRIPT;
34
35 public function init($stage) {
36 if ($stage != self::STAGE) {
37 return;
38 }
39
40 if (!defined("NITROPACK_AJAX_SHORTCODES")) {
41 return;
42 }
43
44 $this->shortcodes = array_map("trim", explode(",", NITROPACK_AJAX_SHORTCODES));
45
46 add_action('wp_ajax_nitro_shortcode_ajax', array($this, 'shortcodeAjax'));
47 add_action('wp_ajax_nopriv_nitro_shortcode_ajax', array($this, 'shortcodeAjax'));
48
49 add_action('wp_enqueue_scripts', function() {
50 wp_add_inline_script('jquery', $this->nitroScLoader);
51 });
52
53 add_filter('pre_do_shortcode_tag', function($out, $tag, $attr) {
54 if (defined("NITRO_DOING_AJAX_SHORTCODES") && NITRO_DOING_AJAX_SHORTCODES) return $out;
55
56 if (in_array($tag, $this->shortcodes)) {
57 return '<span class="nitro-sc-load" data-sc-meta="' . base64_encode(json_encode(["tag" => $tag, "attr" => $attr])) . '"></span>';
58 }
59 return $out;
60 }, 10, 3);
61 }
62
63 public function shortcodeAjax() {
64 if (!defined("NITRO_DOING_AJAX_SHORTCODES")) define("NITRO_DOING_AJAX_SHORTCODES", true);
65 $this->runShortcodes();
66
67 // In case a later hook is needed, we can and an option for it and use something like this
68 //if (did_action('plugins_loaded')) {
69 // $this->runShortcodes();
70 //} else {
71 // add_action('plugins_loaded', [$this, 'runShortcodes']);
72 //}
73 }
74
75 public function runShortcodes() {
76 $shortcodes = $_POST["data"];
77 if (empty($shortcodes)) wp_send_json_error(["message" => "Invalid shortcode input"], 400);
78
79 $resp = [];
80 foreach ($shortcodes as $shortcode) {
81 $sc = json_decode(base64_decode($shortcode), true);
82 $attrFlat = is_array($sc["attr"]) ? array_map(function($k, $v) { return "$k=$v"; }, array_keys($sc["attr"]), array_values($sc["attr"])) : [];
83 $resp[$shortcode] = do_shortcode("[{$sc['tag']} " . implode(" ", $attrFlat) . "]");
84 }
85
86 wp_send_json_success($resp);
87 }
88 }
89