| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: onWebChat Live Chat |
| 4 |
Plugin URI: https://www.onwebchat.com/wp_plugin.php |
| 5 |
Description: onWebChat is a live chat system, that helps you communicate with your website's visitors. |
| 6 |
Author: onWebChat |
| 7 |
Version: 1.0.1 |
| 8 |
Author URI: https://www.onwebchat.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// get logo url |
| 12 |
define('ONWEBCHAT_SMALL_LOGO', plugins_url( 'images/onwebchat-logo.png' , __FILE__ )); |
| 13 |
|
| 14 |
// call function on hook admin_menu |
| 15 |
add_action('admin_menu','onwebchat_setup_menu'); |
| 16 |
|
| 17 |
// define the settings |
| 18 |
add_action('admin_init','onwebchat_register_setttings'); |
| 19 |
|
| 20 |
// add widget plugin to the footer |
| 21 |
add_action('wp_footer', 'onwebchat_add_script_at_footer'); |
| 22 |
|
| 23 |
// add menu page on admin menu with function onwebchat_init_menu_page() |
| 24 |
function onwebchat_setup_menu() { |
| 25 |
add_menu_page( 'onWebChat Settings', 'onWebChat', 'administrator', 'onwebchat_settings', 'onwebchat_init_menu_page', ONWEBCHAT_SMALL_LOGO); |
| 26 |
} |
| 27 |
|
| 28 |
// display admin option page |
| 29 |
function onwebchat_init_menu_page() { |
| 30 |
?> |
| 31 |
<div> |
| 32 |
<h2>onWebChat Settings Page</h2> |
| 33 |
</br> |
| 34 |
<form action="options.php" method="post"> |
| 35 |
<?php settings_fields('onwebchat_plugin_option'); ?> |
| 36 |
<!-- prints all settings inputs we need --> |
| 37 |
<?php do_settings_sections('onwebchat_plugin'); ?> |
| 38 |
<input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" /> |
| 39 |
</form> |
| 40 |
</div> |
| 41 |
<?php |
| 42 |
} |
| 43 |
|
| 44 |
// register the settings to store data |
| 45 |
function onwebchat_register_setttings() { |
| 46 |
register_setting( 'onwebchat_plugin_option', 'onwebchat_plugin_option', 'plugin_options_validate' ); |
| 47 |
//* plugin |
| 48 |
add_settings_section('plugin_main', 'To install Chat Widget:', 'onwebchat_section_text', 'onwebchat_plugin'); |
| 49 |
//* plugin |
| 50 |
add_settings_field('plugin_text_string', 'Chat Id:', 'onwebchat_setting_field_text', 'onwebchat_plugin', 'plugin_main'); |
| 51 |
} |
| 52 |
|
| 53 |
// prints the text of sections |
| 54 |
function onwebchat_section_text() { |
| 55 |
echo '<p>If you don\'t have an account on onWebChat live chat service create one <a href="https://www.onwebchat.com/signup.php" target="_blank">here</a></p>'. |
| 56 |
'<p>To install onWebChat on your Wordpress site please insert the Chat Id to the following field.</p>'. |
| 57 |
'<p>You will find Chat Id in settings-page of onWebChat admin, login <a href="https://www.onwebchat.com/login.php" target="_blank">here</a> </p>'. |
| 58 |
'<p>[You can customize chat widget appearance from settings admin page]</p>'; |
| 59 |
} |
| 60 |
|
| 61 |
// print the text of section field |
| 62 |
function onwebchat_setting_field_text() { |
| 63 |
$options = get_option('onwebchat_plugin_option'); |
| 64 |
echo "<input id='plugin_text_string' name='onwebchat_plugin_option[text_string]' size='40' type='text' value='{$options['text_string']}' />"; |
| 65 |
} |
| 66 |
|
| 67 |
// validate data input |
| 68 |
function plugin_options_validate($input) { |
| 69 |
$newinput['text_string'] = trim($input['text_string']); |
| 70 |
|
| 71 |
if(!preg_match('/^[a-f0-9\\/]{35,50}$/i', $newinput['text_string'])) { |
| 72 |
$newinput['text_string'] = ''; |
| 73 |
} |
| 74 |
|
| 75 |
return $newinput; |
| 76 |
} |
| 77 |
|
| 78 |
// print chat widget code |
| 79 |
function onwebchat_add_script_at_footer() { |
| 80 |
|
| 81 |
$options = get_option('onwebchat_plugin_option'); |
| 82 |
$chatId = $options['text_string']; |
| 83 |
|
| 84 |
$widgetCode = "<script type='text/javascript'> |
| 85 |
var onWebChat={ar:[], set: function(a,b){if (typeof onWebChat_==='undefined'){this.ar. |
| 86 |
push([a,b]);}else{onWebChat_.set(a,b);}},get:function(a){return(onWebChat_.get(a));},w |
| 87 |
:(function(){ var ga=document.createElement('script'); ga.type = 'text/javascript';ga. |
| 88 |
async=1;ga.src='//www.onwebchat.com/clientchat/$chatId'; |
| 89 |
var s=document.getElementsByTagName('script')[0];s.parentNode.insertBefore(ga,s);})()} |
| 90 |
</script>"; |
| 91 |
|
| 92 |
|
| 93 |
//check if user is logged if yes get username, email (all users registered have them) |
| 94 |
if ( is_user_logged_in() ) { |
| 95 |
|
| 96 |
global $current_user; |
| 97 |
get_currentuserinfo(); |
| 98 |
|
| 99 |
// get user name |
| 100 |
$onwebchat_user = $current_user->user_login; |
| 101 |
|
| 102 |
// get user email |
| 103 |
$onwebchat_email = $current_user->user_email; |
| 104 |
|
| 105 |
//add api info |
| 106 |
$widgetCode = $widgetCode."<script type='text/javascript'>onWebChat.set('name','$onwebchat_user');onWebChat.set('email','$onwebchat_email'); </script>"; |
| 107 |
} |
| 108 |
|
| 109 |
echo $widgetCode; |
| 110 |
} |
| 111 |
?> |