advisor.php
7 months ago
chatbot.php
7 months ago
discussions.php
8 months ago
files.php
7 months ago
forms-manager.php
10 months ago
gdpr.php
11 months ago
search.php
11 months ago
security.php
11 months ago
tasks-examples.php
9 months ago
tasks.php
7 months ago
wand.php
7 months ago
gdpr.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Modules_GDPR { |
| 4 | public $core = null; |
| 5 | |
| 6 | public function __construct( $core ) { |
| 7 | $this->core = $core; |
| 8 | add_filter( 'mwai_chatbot_blocks', [ $this, 'chatbot_blocks' ], 10, 2 ); |
| 9 | } |
| 10 | |
| 11 | public function chatbot_blocks( $blocks, $args ) { |
| 12 | $gdpr_text = $this->core->get_option( 'chatbot_gdpr_text' ) ?: 'By using this chatbot, you agree to the recording and processing of your data by our website and the external services it might use (LLMs, vector databases, etc.).'; |
| 13 | $gdpr_button = $this->core->get_option( 'chatbot_gdpr_button' ) ?: '👍 I understand'; |
| 14 | $gdpr_text = esc_html( $gdpr_text ); |
| 15 | $gdpr_button = esc_html( $gdpr_button ); |
| 16 | if ( $args['step'] !== 'init' ) { |
| 17 | return $blocks; |
| 18 | } |
| 19 | |
| 20 | // Check if GDPR is already accepted via cookie |
| 21 | if ( isset( $_COOKIE['mwai_gdpr_accepted'] ) && $_COOKIE['mwai_gdpr_accepted'] === '1' ) { |
| 22 | return $blocks; |
| 23 | } |
| 24 | $botId = $args['botId']; |
| 25 | $uniqueId = uniqid( 'mwai_gdpr_' ); |
| 26 | $blocks[] = [ |
| 27 | 'id' => $uniqueId, |
| 28 | 'type' => 'content', |
| 29 | 'data' => [ |
| 30 | 'id' => $uniqueId, |
| 31 | 'html' => '<div> |
| 32 | <p>' . $gdpr_text . '</p> |
| 33 | <div class="mwai-gdpr-buttons"> |
| 34 | <button id="' . $uniqueId . '-button" type="button" style="width: 100%;">' . $gdpr_button . '</button> |
| 35 | </div> |
| 36 | </div>', |
| 37 | 'script' => ' |
| 38 | (function() { |
| 39 | // Handle GDPR consent button click |
| 40 | document.addEventListener("click", function(event) { |
| 41 | if (event.target.id === "' . $uniqueId . '-button") { |
| 42 | event.preventDefault(); |
| 43 | |
| 44 | // Set GDPR acceptance cookie for 1 year |
| 45 | const date = new Date(); |
| 46 | date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000)); |
| 47 | document.cookie = "mwai_gdpr_accepted=1; expires=" + date.toUTCString() + "; path=/"; |
| 48 | |
| 49 | // IMPORTANT: When multiple chatbots share the same botId, we must find |
| 50 | // the specific chatbot instance that contains this GDPR block. |
| 51 | // MwaiAPI.getChatbot() returns the first match, which may be wrong. |
| 52 | let foundChatbot = null; |
| 53 | const chatbotsWithSameBotId = MwaiAPI.chatbots.filter(cb => cb.botId === "' . $botId . '"); |
| 54 | |
| 55 | // Find the chatbot that actually has this GDPR block |
| 56 | for (const chatbot of chatbotsWithSameBotId) { |
| 57 | const blocks = chatbot.getBlocks ? chatbot.getBlocks() : []; |
| 58 | if (blocks.some(block => block.id === "' . $uniqueId . '")) { |
| 59 | foundChatbot = chatbot; |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (foundChatbot) { |
| 65 | foundChatbot.unlock(); |
| 66 | foundChatbot.removeBlockById("' . $uniqueId . '"); |
| 67 | } |
| 68 | } |
| 69 | }, true); // Use capture phase for better popup/modal support |
| 70 | |
| 71 | // Lock the chatbot when it has this GDPR block |
| 72 | // Note: Using MwaiAPI.getChatbot() here is fine for locking |
| 73 | // as we want to lock any chatbot with this botId initially |
| 74 | const tryLock = setInterval(function() { |
| 75 | const chatbot = MwaiAPI.getChatbot("' . $botId . '"); |
| 76 | if (chatbot && chatbot.lock) { |
| 77 | chatbot.lock(); |
| 78 | clearInterval(tryLock); |
| 79 | } |
| 80 | }, 100); |
| 81 | |
| 82 | // Stop trying after 5 seconds |
| 83 | setTimeout(function() { |
| 84 | clearInterval(tryLock); |
| 85 | }, 5000); |
| 86 | })(); |
| 87 | ' |
| 88 | ] |
| 89 | ]; |
| 90 | return $blocks; |
| 91 | } |
| 92 | } |
| 93 |