Changeset 2707828
- Timestamp:
- 04/11/2022 07:15:54 AM (4 years ago)
- Location:
- melibo-chatbot
- Files:
-
- 27 added
- 3 edited
-
tags/1.2.0 (added)
-
tags/1.2.0/admin (added)
-
tags/1.2.0/admin/MeliboChatbot.class.php (added)
-
tags/1.2.0/admin/MeliboView.php (added)
-
tags/1.2.0/admin/inc (added)
-
tags/1.2.0/admin/inc/img (added)
-
tags/1.2.0/admin/inc/img/logo_transparent128x128.png (added)
-
tags/1.2.0/admin/inc/notice.php (added)
-
tags/1.2.0/admin/inc/styles (added)
-
tags/1.2.0/admin/inc/styles/notice.css (added)
-
tags/1.2.0/includes (added)
-
tags/1.2.0/includes/notice.php (added)
-
tags/1.2.0/melibo-chatbot.php (added)
-
tags/1.2.0/readme.txt (added)
-
tags/1.2.0/uninstall.php (added)
-
trunk/admin/MeliboChatbot.class.php (modified) (5 diffs)
-
trunk/admin/MeliboView.php (added)
-
trunk/admin/View.php (added)
-
trunk/admin/inc (added)
-
trunk/admin/inc/img (added)
-
trunk/admin/inc/img/logo_transparent128x128.png (added)
-
trunk/admin/inc/notice.php (added)
-
trunk/admin/inc/styles (added)
-
trunk/admin/inc/styles/notice.css (added)
-
trunk/includes/notice.php (added)
-
trunk/languages (added)
-
trunk/languages/melibo-chatbot-de_DE.mo (added)
-
trunk/languages/melibo-chatbot-de_DE.po (added)
-
trunk/melibo-chatbot.php (modified) (1 diff)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
melibo-chatbot/trunk/admin/MeliboChatbot.class.php
r2706435 r2707828 3 3 class MeliboChatbot { 4 4 5 private $API_KEY_CHECK_URL = 'https://api.melibo.de/stingray/settings/widget/{apiKey}/public'; 6 private $CDN_URL = 'https://cdn.melibo.de/bundle.js'; 7 private $SPACE_LETTER = ' '; 8 private $apiKeyValid = false; 9 5 10 function __construct() { 6 11 add_action( 'admin_init', array($this, 'initSettings')); 7 12 add_action( 'admin_menu', array($this, 'adminPage')); 13 add_action( 'admin_notices', array( $this, 'displayNotice')); 8 14 add_action( 'wp_enqueue_scripts', array($this, 'showChatbot')); 9 15 … … 25 31 ) 26 32 ); 27 register_setting('meliboplugin', 'melibo_api_key', array( 'sanitize_callback' => 'sanitize_text_field', 'default' => ''));33 register_setting('meliboplugin', 'melibo_api_key', array($this, 'apiKeyValidation')); 28 34 29 35 add_settings_field( … … 38 44 ) 39 45 ); 40 register_setting('meliboplugin', 'melibo_activate', array( 'sanitize_callback' => 'sanitize_text_field', 'default' => '0'));46 register_setting('meliboplugin', 'melibo_activate', array($this, 'activateValidation')); 41 47 } 42 48 … … 82 88 function showChatbot($content) { 83 89 if(get_option('melibo_activate', '1') AND (is_page() OR is_front_page() OR is_single())) { 84 wp_enqueue_script('qgjs', 'https://cdn.melibo.de/bundle.js'); ?>90 wp_enqueue_script('qgjs', $this->CDN_URL); ?> 85 91 <melibo-webchat api-key="<?php echo esc_attr(get_option('melibo_api_key')); ?>"></melibo-webchat> 86 92 <?php } 93 } 94 95 function apiKeyValidation($input) { 96 $fieldName = 'melibo_api_key'; 97 $oldFieldValue = get_option($fieldName); 98 99 if(empty($input)) { 100 $input = $oldFieldValue; 101 $this->apiKeyValid = false; 102 add_settings_error($fieldName, $fieldName . '_error', 'Der API-Key darf nicht leer sein.', 'error'); 103 } else if (strpos($input, $this->SPACE_LETTER) !== FALSE) { 104 $input = $oldFieldValue; 105 $this->apiKeyValid = false; 106 add_settings_error($fieldName, $fieldName . '_error', 'Der API-Key darf keine Leerzeichen enthalten.', 'error'); 107 } else { 108 $input = $this->checkAPIKeyViaMeliboRequest($input); 109 } 110 111 return $input; 112 } 113 114 private function checkAPIKeyViaMeliboRequest($apiKey) { 115 $url = str_replace('{apiKey}', $apiKey, $this->API_KEY_CHECK_URL); 116 $ch = curl_init($url); 117 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 118 $jsonResponse = json_decode(curl_exec($ch)); 119 120 if(is_object($jsonResponse) == FALSE) { 121 add_settings_error($fieldName, $fieldName . '_error', 'Beim Prüfen des API-Keys ist ein unerwarteter Fehler aufgetreten', 'error'); 122 $this->apiKeyValid = false; 123 return $apiKey; 124 } 125 126 if(property_exists($jsonResponse, 'chatbotKey')) { 127 $this->apiKeyValid = true; 128 return $apiKey; 129 } 130 131 if(property_exists($jsonResponse, 'statusCode')) { 132 $statusCode = $jsonResponse->statusCode; 133 $apiKey = $oldFieldValue; 134 $this->apiKeyValid = false; 135 if ($statusCode == '500') { 136 add_settings_error($fieldName, $fieldName . '_error', 'Der eingetragene API-Key ist nicht gültig', 'error'); 137 } 138 return $apiKey; 139 } 140 141 curl_close($ch); 142 } 143 144 function activateValidation($input) { 145 if($this->apiKeyValid == FALSE) { 146 $input = '0'; 147 } 148 return $input; 87 149 } 88 150 … … 103 165 return $links; 104 166 } 167 168 function displayNotice() { 169 global $hook_suffix; 170 171 if( in_array( $hook_suffix, array( 172 'index.php', # dashboard 173 'plugins.php', 174 'settings_page_melibo-settings-page' 175 ))) { 176 wp_register_style( 'notice.css', plugin_dir_url( __FILE__ ) . 'inc/styles/notice.css', array(), MELIBO_VERSION ); 177 wp_enqueue_style( 'notice.css'); 178 179 if(empty(trim(get_option('melibo_api_key')))) { 180 MeliboView::loadFile('notice', array('type' => 'no-api-key')); 181 } 182 } 183 } 105 184 } -
melibo-chatbot/trunk/melibo-chatbot.php
r2706435 r2707828 4 4 * Plugin Name: melibo Chatbot 5 5 * Description: add your melibo chatbot to your WordPress for free 6 * Version: 1. 1.06 * Version: 1.2.0 7 7 * Author: Melibo 8 8 * Author URI: www.melibo.de 9 9 */ 10 define( 'MELIBO_VERSION', '1.2.0'); 10 11 11 require_once __DIR__ . '/admin/MeliboChatbot.class.php'; 12 require_once( plugin_dir_path( __FILE__ ) . '/admin/MeliboView.php' ); 13 require_once( plugin_dir_path( __FILE__ ) . '/admin/MeliboChatbot.class.php' ); 12 14 13 $meliboChatbot = new MeliboChatbot();15 $meliboChatbot = new MeliboChatbot(); -
melibo-chatbot/trunk/readme.txt
r2706435 r2707828 3 3 Tags: chat, live chat, chatbot, support, livechat, bot, melibo, Helpdesk, E-Commerce, messenger, AI, widget, 4 4 Tested up to: 5.9.2 5 Stable tag: 1. 1.05 Stable tag: 1.2.0 6 6 License: GPLv2 7 7 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 137 137 138 138 - add settings link in plugin overview 139 140 = 1.2.0 = 141 142 - add API-Key checks 143 - add admin_notices
Note: See TracChangeset
for help on using the changeset viewer.