editor.js
108 lines
| 1 | ( function ( wp, React ) { |
| 2 | var registerPlugin = wp.plugins.registerPlugin; |
| 3 | var PluginDocumentSettingPanel = wp.editPost.PluginDocumentSettingPanel; |
| 4 | var Button = wp.components.Button; |
| 5 | // var CheckboxControl= wp.components.CheckboxControl; |
| 6 | |
| 7 | |
| 8 | var el = wp.element.createElement; |
| 9 | var __ = wp.i18n.__; |
| 10 | // var PluginSidebar = wp.editPost.PluginSidebar; |
| 11 | |
| 12 | |
| 13 | function MyDocumentSettingPlugin() { |
| 14 | return el( |
| 15 | |
| 16 | PluginDocumentSettingPanel, |
| 17 | { |
| 18 | name: 'my-document-setting-plugin', |
| 19 | title: 'Aruba HiSpeed Cache', |
| 20 | icon: 'database', |
| 21 | }, |
| 22 | /*__( 'My Document Setting Panel Content', 'aruba-hispeed-cache' ),*/ |
| 23 | |
| 24 | el( |
| 25 | Button, |
| 26 | { |
| 27 | className: 'ahsc-cleaner', |
| 28 | variant: 'secondary', |
| 29 | onClick: ahscBtnPurger, |
| 30 | }, |
| 31 | __( 'Cancella Cache', 'aruba-hispeed-cache' ), |
| 32 | ), |
| 33 | el('div',{ className: 'ahsc-loader'},"") |
| 34 | |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | registerPlugin( 'my-document-setting-plugin', { |
| 39 | render: MyDocumentSettingPlugin, |
| 40 | } ); |
| 41 | |
| 42 | const ahscBtnPurger = async () => { |
| 43 | if (typeof AHSC_TOOLBAR.ahsc_nonce == "undefined") { |
| 44 | console.warn("No nonce is set for this action. This action has been aborted."); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | document.getElementsByClassName('ahsc-loader')[0].style.fontWeight="Bold"; |
| 49 | //loader.style.display = "block"; |
| 50 | document.getElementsByClassName('ahsc-loader')[0].insertAdjacentHTML("beforeend", __( 'Cancellazione in corso', 'aruba-hispeed-cache' ) ) |
| 51 | let to_purge = "current-url" === AHSC_TOOLBAR.ahsc_topurge ? window.location.pathname : "all"; |
| 52 | |
| 53 | const data = new FormData(); |
| 54 | data.append("action", "ahcs_clear_cache"); |
| 55 | data.append("ahsc_nonce", AHSC_TOOLBAR.ahsc_nonce); |
| 56 | data.append("ahsc_to_purge", encodeURIComponent(to_purge)); |
| 57 | |
| 58 | const request = await fetch(AHSC_TOOLBAR.ahsc_ajax_url, { |
| 59 | method: "POST", |
| 60 | credentials: "same-origin", |
| 61 | body: data, |
| 62 | }) |
| 63 | .then((r) => r.json()) |
| 64 | .then((result) => { |
| 65 | if (result.code >= 200) { |
| 66 | let style = ""; |
| 67 | let message_color=""; |
| 68 | switch (result.type) { |
| 69 | case "success": |
| 70 | style = "color:green"; |
| 71 | message_color="green"; |
| 72 | break; |
| 73 | case "error": |
| 74 | style = "color:red"; |
| 75 | message_color="red"; |
| 76 | break; |
| 77 | default: |
| 78 | style = "color:blue"; |
| 79 | message_color="blue"; |
| 80 | break; |
| 81 | } |
| 82 | console.log(`%c${result.message}`, style); |
| 83 | document.getElementsByClassName('ahsc-loader')[0].innerHTML=""; |
| 84 | document.getElementsByClassName('ahsc-loader')[0].style.color=message_color; |
| 85 | document.getElementsByClassName('ahsc-loader')[0].insertAdjacentHTML("beforeend", __( result.message, 'aruba-hispeed-cache' ) ); |
| 86 | setTimeout(function() { |
| 87 | document.getElementsByClassName('ahsc-loader')[0].innerHTML=""; |
| 88 | }, 3000); // <-- time in milliseconds |
| 89 | |
| 90 | } |
| 91 | }) |
| 92 | .catch((error) => { |
| 93 | console.log("[Aruba HiSpeed Cache Plugin]"); |
| 94 | console.error(error); |
| 95 | document.getElementsByClassName('ahsc-loader')[0].innerHTML=""; |
| 96 | document.getElementsByClassName('ahsc-loader')[0].style.color=message_color; |
| 97 | document.getElementsByClassName('ahsc-loader')[0].insertAdjacentHTML("beforeend",error); |
| 98 | }); |
| 99 | |
| 100 | return; |
| 101 | }; |
| 102 | |
| 103 | /* The line `window.ahscBtnPurger = ahscBtnPurger;` is assigning the `ahscBtnPurger` function to the |
| 104 | `ahscBtnPurger` property of the `window` object. This makes the function accessible globally in the |
| 105 | browser environment. */ |
| 106 | window.ahscBtnPurger = ahscBtnPurger; |
| 107 | |
| 108 | } )( window.wp, window.React ); |