| 1 |
<?php |
| 2 |
$chatbot_data = get_option('lws_tools_chatbot_data', []); |
| 3 |
$user_ip = isset($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['HTTP_X_REAL_IP'] : '127.0.0.1'; |
| 4 |
|
| 5 |
$user_data = isset($chatbot_data[$user_ip]) ? $chatbot_data[$user_ip] : ['amount' => 0, 'date' => time()]; |
| 6 |
|
| 7 |
// If the last reset happened 30 days ago or more, reset right now |
| 8 |
if ($user_data['date'] < time() - (30 * 24 * 60 * 60)) { |
| 9 |
$user_data['amount'] = 0; |
| 10 |
$user_data['date'] = time(); |
| 11 |
$chatbot_data[$user_ip] = $user_data; |
| 12 |
update_option('lws_tools_chatbot_data', $chatbot_data); |
| 13 |
} |
| 14 |
|
| 15 |
// Refuse access to the chatbot if the user has sent 100+ messages OR if the data in BDD has been badly altered |
| 16 |
$accessible_chatbot = true; |
| 17 |
if (!isset($user_data['amount']) || $user_data['amount'] >= 100) { |
| 18 |
$accessible_chatbot = false; |
| 19 |
} |
| 20 |
?> |
| 21 |
|
| 22 |
<?php if ($accessible_chatbot && $website_is_lws) : ?> |
| 23 |
<div id="opengpt_chatbot_element"> |
| 24 |
|
| 25 |
<iframe src="https://www.openassistantgpt.io/embed/cmdol2mhl0003mblafzjcm8c9/button?chatbox=false" |
| 26 |
style="z-index: 50; margin-right: 6px; margin-bottom: 6px; position: fixed; right: 36px; bottom: 36px; width: 60px; height: 60px; border: 0; border: 2px solid #e2e8f0; border-radius: 50%; color-scheme: none; background: none;box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);" |
| 27 |
id="openassistantgpt-chatbot-button-iframe"></iframe> |
| 28 |
|
| 29 |
|
| 30 |
<!-- This chatbot is build using https://openassistantgpt.io/ --> |
| 31 |
<iframe |
| 32 |
src="https://www.openassistantgpt.io/embed/cmdol2mhl0003mblafzjcm8c9/window?chatbox=false&withExitX=true&clientSidePrompt=<?php echo $encoded_prompt; ?>" |
| 33 |
style="z-index: 50; margin-right: 6px; margin-bottom: 98px; display: none; position: fixed; right: 0; bottom: 0; pointer-events: none; overflow: hidden; height: 65vh; border: 2px solid #e2e8f0; border-radius: 0.375rem; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06); max-width: 700px; width: stretch;" |
| 34 |
allow="clipboard-read; clipboard-write" |
| 35 |
allowfullscreen id="openassistantgpt-chatbot-iframe"> |
| 36 |
</iframe> |
| 37 |
</div> |
| 38 |
<?php elseif (!$accessible_chatbot && $website_is_lws) : ?> |
| 39 |
<div id="opengpt_chatbot_element"> |
| 40 |
<div id="opengpt_chatbot_deactivated" class="lws_tools_assistant_button" title="<?php esc_html_e('You have reached your quota (100 messages) for this month.', 'lws-tools'); ?>"> |
| 41 |
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-message-square" style="color: rgb(30, 73, 155);"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path></svg> |
| 42 |
</div> |
| 43 |
</div> |
| 44 |
<?php else : ?> |
| 45 |
<div id="opengpt_chatbot_element"> |
| 46 |
<div id="opengpt_chatbot_deactivated" class="lws_tools_assistant_button deactivated" title="<?php esc_html_e('Chatbot is only available on LWS hostings.', 'lws-tools'); ?>"> |
| 47 |
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-message-square" style="color: rgb(30, 73, 155);"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path></svg> |
| 48 |
</div> |
| 49 |
</div> |
| 50 |
<?php endif; ?> |
| 51 |
|
| 52 |
<script> |
| 53 |
window.addEventListener("message", function(t) { |
| 54 |
var e = document.getElementById("openassistantgpt-chatbot-iframe"), |
| 55 |
s = document.getElementById("openassistantgpt-chatbot-button-iframe"), |
| 56 |
block = document.getElementById("opengpt_chatbot_element"); |
| 57 |
"openChat" === t.data && (e && s |
| 58 |
? |
| 59 |
( |
| 60 |
e.contentWindow.postMessage("openChat", "*"), |
| 61 |
s.contentWindow.postMessage("openChat", "*"), |
| 62 |
e.style.pointerEvents = "auto", |
| 63 |
e.style.display = "block", |
| 64 |
window.innerWidth < 640 |
| 65 |
? |
| 66 |
( |
| 67 |
e.style.position = "fixed", |
| 68 |
e.style.width = "100%", |
| 69 |
e.style.height = "100%", |
| 70 |
e.style.top = "0", |
| 71 |
e.style.left = "0", |
| 72 |
e.style.zIndex = "9999" |
| 73 |
) |
| 74 |
: |
| 75 |
( |
| 76 |
e.style.position = "fixed", |
| 77 |
e.style.width = "stretch", |
| 78 |
e.style.maxWidth = "700px", |
| 79 |
e.style.height = "65vh", |
| 80 |
e.style.bottom = "0", |
| 81 |
e.style.right = "0", |
| 82 |
e.style.top = "", e.style.left = "" |
| 83 |
) |
| 84 |
) |
| 85 |
: console.error("iframe not found") |
| 86 |
), |
| 87 |
"closeChat" === t.data && e && s && |
| 88 |
( |
| 89 |
e.style.display = "none", |
| 90 |
e.style.pointerEvents = "none", |
| 91 |
e.contentWindow.postMessage("closeChat", "*"), |
| 92 |
s.contentWindow.postMessage("closeChat", "*") |
| 93 |
) |
| 94 |
|
| 95 |
// Action whenever the user send a message to the chatbot |
| 96 |
if (t.data === "messageSent") { |
| 97 |
let ajaxRequest = jQuery.ajax({ |
| 98 |
url: ajaxurl, |
| 99 |
type: "POST", |
| 100 |
timeout: 10000, // 10 seconds timeout |
| 101 |
context: document.body, |
| 102 |
data: { |
| 103 |
_ajax_nonce: "<?php echo esc_html(wp_create_nonce("lws_tools_ratelimit")); ?>", |
| 104 |
action: "lws_tools_on_message_sent", |
| 105 |
}, |
| 106 |
success: function(data) { |
| 107 |
if (data === null || typeof data != 'string') { |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
try { |
| 112 |
var returnData = JSON.parse(data); |
| 113 |
} catch (e) { |
| 114 |
console.error(e); |
| 115 |
returnData = { |
| 116 |
'code': "NOT_JSON", |
| 117 |
'data': "FAIL" |
| 118 |
}; |
| 119 |
} |
| 120 |
|
| 121 |
// switch (returnData['code']) { |
| 122 |
// case "SUCCESS": |
| 123 |
// break; |
| 124 |
// case "LIMIT_JUST_REACHED": |
| 125 |
// case "LIMIT": |
| 126 |
// block.innerHTML = '<div class="lws_tools_assistant_button_off" title="<?php esc_html_e('You reached your quota (100 messages) for this month.', 'lws-tools'); ?>"></div>'; |
| 127 |
// break; |
| 128 |
// default: |
| 129 |
// console.log(returnData); |
| 130 |
// break; |
| 131 |
// } |
| 132 |
}, |
| 133 |
error: function(error) { |
| 134 |
console.error(error); |
| 135 |
} |
| 136 |
}); |
| 137 |
} |
| 138 |
}); |
| 139 |
|
| 140 |
// Show tooltip when clicking on deactivated chatbot |
| 141 |
document.addEventListener('click', function(e) { |
| 142 |
if (e.target.closest('#opengpt_chatbot_deactivated')) { |
| 143 |
const element = e.target.closest('#opengpt_chatbot_deactivated'); |
| 144 |
const tooltip = document.createElement('div'); |
| 145 |
tooltip.textContent = element.getAttribute('title'); |
| 146 |
tooltip.style.cssText = 'position: fixed; background: #333; color: white; padding: 8px 12px; border-radius: 4px; font-size: 14px; z-index: 10000; pointer-events: none; max-width: 250px; height: fit-content;'; |
| 147 |
|
| 148 |
const rect = element.getBoundingClientRect(); |
| 149 |
const tooltipWidth = 250; // Approximate tooltip width |
| 150 |
const tooltipHeight = 60; // Approximate tooltip height |
| 151 |
|
| 152 |
let top = rect.top - tooltipHeight - 5; |
| 153 |
let left = rect.left; |
| 154 |
|
| 155 |
// Adjust if tooltip would go above viewport |
| 156 |
if (top < 0) { |
| 157 |
top = rect.bottom + 5; |
| 158 |
} |
| 159 |
|
| 160 |
// Adjust if tooltip would go beyond right edge |
| 161 |
if (left + tooltipWidth > window.innerWidth) { |
| 162 |
left = window.innerWidth - tooltipWidth - 50; |
| 163 |
} |
| 164 |
|
| 165 |
// Adjust if tooltip would go beyond left edge |
| 166 |
if (left < 0) { |
| 167 |
left = 10; |
| 168 |
} |
| 169 |
|
| 170 |
tooltip.style.top = top + 'px'; |
| 171 |
tooltip.style.left = left + 'px'; |
| 172 |
|
| 173 |
document.body.appendChild(tooltip); |
| 174 |
|
| 175 |
setTimeout(() => { |
| 176 |
if (tooltip.parentNode) { |
| 177 |
tooltip.parentNode.removeChild(tooltip); |
| 178 |
} |
| 179 |
}, 3000); |
| 180 |
} |
| 181 |
}); |
| 182 |
</script> |