<?php
/*
Plugin Name: Social Intents – AI-Powered Live Chat
Plugin URI: https://www.socialintents.com
Description: Easily chat with your website visitors using AI-powered chatbots or live agents right from Microsoft Teams, Slack, Google Chat, or Zoom. Train your chatbot on your own content in minutes and pick from ChatGPT, Gemini, or Claude AI models. To get started, go to your Plugin Settings and register for a Free Trial or grab your API key from your account.
Version: 1.6.19
Author: Social Intents
Author URI: https://www.socialintents.com/
*/

$silc_domain = plugins_url();
add_action('init', 'silc_init');
add_action('admin_notices', 'silc_notice');
add_filter('plugin_action_links', 'silc_plugin_actions', 10, 2);
add_action('wp_footer', 'silc_insert',4);
add_action('admin_footer', 'siRedirect');

define('SI_DASHBOARD_URL', "https://www.socialintents.com/webInbox.do");
define('SI_SMALL_LOGO',plugin_dir_url( __FILE__ ).'si-small.png');

function silc_init() {
    if(function_exists('current_user_can') && current_user_can('manage_options')) {
        add_action('admin_menu', 'silc_add_settings_page');
        add_action('admin_menu', 'silc_create_menu');
    }
}

function silc_insert() {
    if(strlen(get_option('silc_widgetID')) == 32 ) {
        echo("\n\n<!-- www.socialintents.com -->\n<script type=\"text/javascript\">\n");
        echo("(function() {function socialintents(){\n");
        echo("    var siJsHost = ((\"https:\" === document.location.protocol) ? \"https://\" : \"http://\");\n");
        echo("    var s = document.createElement('script');s.type = 'text/javascript';s.async = true;s.src = siJsHost+'www.socialintents.com/api/chat/socialintents.1.4.js#" . esc_js(get_option('silc_widgetID')) . "';\n");
        echo("    var x = document.getElementsByTagName('script')[0];x.parentNode.insertBefore(s, x);};\n");
        echo("if (window.attachEvent)window.attachEvent('onload', socialintents);else window.addEventListener('load', socialintents, false);})();\n");
        echo("</script>\n");
    }
}

function silc_notice() {
    if(!get_option('silc_widgetID')) {
        echo('<div class="error"><p><strong>'.sprintf(__('Your Social Intents Plugin is disabled. Please go to the <a href="%s">plugin settings</a> to register.  ' ), admin_url('options-general.php?page=live-chat-support-by-socialintents')).'</strong></p></div>');
    }
}

function silc_plugin_actions($links, $file) {
    static $this_plugin;
    $silc_domain = plugins_url();
    if(!$this_plugin) $this_plugin = plugin_basename(__FILE__);
    if($file == $this_plugin && function_exists('admin_url')) {
        $settings_link = '<a href="'.admin_url('options-general.php?page=live-chat-support-by-socialintents').'">'.__('Settings', $silc_domain).'</a>';
        array_unshift($links, $settings_link);
    }
    return($links);
}

function silc_add_settings_page() {
    function silc_settings_page() {
        global $silc_domain;
        $widget_id = esc_attr(get_option('silc_widgetID'));
        ?>
<div class="wrap">
    <h2><?php _e('Live Chat by Social Intents', $silc_domain) ?></h2>

    <div class="metabox-holder meta-box-sortables ui-sortable pointer">

    <div class="postbox" style="float:left;width:35em;margin-right:20px;">
        <h3 class="hndle"><span id="silc_noAccountSpan"><?php _e('Social Intents Free Trial', $silc_domain) ?></span></h3>
        <div id="silc_register" class="inside" style="padding: -30px 10px">
            <p style="text-align:center">
                <?php wp_nonce_field('update-options'); ?>
                <a href="https://www.socialintents.com/" title="AI-Powered Live Chat to help grow your business">
                    <img src="<?php echo esc_url(plugins_url('socialintents.png', __FILE__)); ?>" height="94" />
                </a>
            </p>
            <p><?php printf(__('Join 55,000+ companies using our platform improve customer service and sell more. Visit %1$sSocial Intents%2$s.', $silc_domain), '<a href="https://www.socialintents.com" target="_blank">', '', '</a>'); ?></p>
            <div style='text-align:center'>
                <a href='https://www.socialintents.com/signup.do?wptype=wpchat' class="button button-primary" target="_blank">Register For Free Now!</a>
            </div>
        </div>
        <div id="silc_registerComplete" class="inside" style="padding: -20px 10px;display:none;">
            <p>View previous chats and respond to chats from your Inbox.</p>
            <p><a href='https://help.socialintents.com' target="_blank">Visit our Knowledge Base</a></p>
            <p>
                <a href='https://www.socialintents.com/webInbox.do' class="button button-primary" target="_blank">Inbox</a>
                <a href='https://www.socialintents.com/webWidget.do?id=<?php echo urlencode($widget_id); ?>' class="button button-primary" target="_blank">Settings</a>
                <a href='https://www.socialintents.com/preview.do?wid=<?php echo urlencode($widget_id); ?>' class="button button-primary" target="_blank">Preview</a>
            </p><br>
            <div style="text-align:center">
                <a id="changeWidget" class="" target="_blank">Enter Different App Key</a>
            </div>
        </div>
    </div>

    <div id="addAppKey" class="postbox" style="float:left;width:35em;margin-right:20px">
        <h3 class="hndle"><span><?php _e('Already Registered?  Enter your API Key', $silc_domain) ?></span></h3>
        <div class="inside" style="padding: 0 10px">
            <form id="saveSettings" method="post" action="options.php">
                <?php wp_nonce_field('update-options'); ?>
                <p><label for="silc_widgetID">Enter your App Key below to activate your plugin.</label><br />
                    <input type="text" name="silc_widgetID" id="silc_widgetID" placeholder="Your API Key" value="<?php echo esc_attr($widget_id); ?>" style="width:100%" />
                </p>
                <p class="submit" style="padding:0">
                    <input type="hidden" name="action" value="update" />
                    <input type="hidden" name="page_options" value="silc_widgetID" />
                    <input type="submit" name="silc_submit" id="silc_submit" value="<?php _e('Save Settings', $silc_domain) ?>" class="button-primary" />
                    <a href='https://www.socialintents.com/webApps.do' class="button button-primary" target="_blank">Find My API Key</a>
                </p>
                <br><img src="<?php echo esc_url(plugins_url('app-key.png', __FILE__)); ?>" width="250" />
            </form>
        </div>
    </div>

    </div>
</div>
<script>
jQuery(document).ready(function($) {
    var silc_wid = $('#silc_widgetID').val();
    if (silc_wid !== '') {
        $("#silc_register").hide();
        $("#addAppKey").hide();
        $("#silc_registerComplete").show();
        $("#silc_noAccountSpan").html("Social Intents Plugin Settings");
    }
    $(document).on("click", "#changeWidget", function () {
        $("#addAppKey").show();
    });

    $(document).on("click", "#silc_inputSaveSettings", function () {
        var silc_wid = encodeURIComponent($('#silc_widgetID').val());
        var silc_tt  = encodeURIComponent($('#silc_tab_text').val() || '');
        var silc_ht  = encodeURIComponent($('#silc_header_text').val() || '');
        var silc_to  = encodeURIComponent($('#silc_tab_offline_text').val() || '');
        var silc_tc  = encodeURIComponent($('#silc_tab_color').val() || '');
        var silc_top = encodeURIComponent($('#silc_time_on_page').val() || '');

        var url = 'https://www.socialintents.com/json/jsonSaveChatSettings.jsp?' +
                  'tc=' + silc_tc + '&tt=' + silc_tt + '&ht=' + silc_ht +
                  '&wid=' + silc_wid + '&to=' + silc_to + '&top=' + silc_top + '&callback=?';

        sessionStorage.removeItem("settings");

        $.ajax({
            type: 'GET',
            url: url,
            async: false,
            jsonpCallback: 'jsonCallBack',
            contentType: "application/json",
            dataType: 'jsonp',
            success: function(json) {
                $('#silc_widgetID').val(json.key);
                sessionStorage.removeItem("settings");
                sessionStorage.removeItem("socialintents_vs_chat");
                sessionStorage.setItem("hasSeenPopup", "false");
                $("#saveDetailSettings").submit();
            },
            error: function(e) {
                console.error("Settings save failed:", e);
            }
        });
    });
});
</script>
<?php
    }
    $silc_domain = plugins_url();
    add_submenu_page('options-general.php', __('Live Chat', $silc_domain), __('Live Chat', $silc_domain), 'manage_options', 'live-chat-support-by-socialintents', 'silc_settings_page');
}

function addSilcLink() {
    $dir = plugin_dir_path(__FILE__);
    include $dir . 'options.php';
}

function silc_create_menu() {
    add_menu_page('Live Chat', 'Live Chat', 'administrator', 'silc_dashboard', 'addSilcLink', plugins_url('live-chat-support-by-social-intents/si-small.png'));
}

function siRedirect() {
    $redirectUrl = "https://www.socialintents.com/webInbox.do";
    echo "<script> jQuery('a[href=\"admin.php?page=silc_dashboard\"]').attr('href', '" . esc_url($redirectUrl) . "').attr('target', '_blank') </script>";
}
?>