Plugin Directory

Changeset 2707828


Ignore:
Timestamp:
04/11/2022 07:15:54 AM (4 years ago)
Author:
melibo
Message:
  • add API-Key checks
  • add admin_notices
Location:
melibo-chatbot
Files:
27 added
3 edited

Legend:

Unmodified
Added
Removed
  • melibo-chatbot/trunk/admin/MeliboChatbot.class.php

    r2706435 r2707828  
    33class MeliboChatbot  {
    44
     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
    510    function __construct() {
    611        add_action( 'admin_init', array($this, 'initSettings'));
    712        add_action( 'admin_menu', array($this, 'adminPage'));
     13        add_action( 'admin_notices', array( $this, 'displayNotice'));
    814        add_action( 'wp_enqueue_scripts', array($this, 'showChatbot'));
    915
     
    2531            )
    2632        );
    27         register_setting('meliboplugin', 'melibo_api_key', array('sanitize_callback' => 'sanitize_text_field', 'default' => ''));
     33        register_setting('meliboplugin', 'melibo_api_key', array($this, 'apiKeyValidation'));
    2834
    2935        add_settings_field(
     
    3844            )
    3945        );
    40         register_setting('meliboplugin', 'melibo_activate', array('sanitize_callback' => 'sanitize_text_field', 'default' => '0'));
     46        register_setting('meliboplugin', 'melibo_activate', array($this, 'activateValidation'));
    4147    }
    4248
     
    8288    function showChatbot($content) {
    8389        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); ?>
    8591            <melibo-webchat api-key="<?php echo esc_attr(get_option('melibo_api_key')); ?>"></melibo-webchat>
    8692        <?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;
    87149    }
    88150
     
    103165        return $links;
    104166    }
     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    }
    105184 }
  • melibo-chatbot/trunk/melibo-chatbot.php

    r2706435 r2707828  
    44 * Plugin Name: melibo Chatbot
    55 * Description: add your melibo chatbot to your WordPress for free
    6  * Version: 1.1.0
     6 * Version: 1.2.0
    77 * Author: Melibo
    88 * Author URI: www.melibo.de
    99 */
     10define( 'MELIBO_VERSION', '1.2.0');
    1011
    11  require_once __DIR__ . '/admin/MeliboChatbot.class.php';
     12require_once( plugin_dir_path( __FILE__ ) . '/admin/MeliboView.php' );
     13require_once( plugin_dir_path( __FILE__ ) . '/admin/MeliboChatbot.class.php' );
    1214
    13  $meliboChatbot = new MeliboChatbot();
     15$meliboChatbot = new MeliboChatbot();
  • melibo-chatbot/trunk/readme.txt

    r2706435 r2707828  
    33Tags: chat, live chat, chatbot, support, livechat, bot, melibo, Helpdesk, E-Commerce, messenger, AI, widget,
    44Tested up to: 5.9.2
    5 Stable tag: 1.1.0
     5Stable tag: 1.2.0
    66License: GPLv2
    77License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    137137
    138138- 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.