| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Userlike |
| 4 |
Plugin URI: http://userlike.com |
| 5 |
Description: Userlike live chat integration for Wordpress |
| 6 |
Version: 1.0 |
| 7 |
Author: Sven Gebhardt |
| 8 |
Author URI: http://userlike.com |
| 9 |
License: GPL2 |
| 10 |
|
| 11 |
Copyright 2012 Sven Gebhardt <sg@unkreativ.org> |
| 12 |
|
| 13 |
This program is free software; you can redistribute it and/or modify |
| 14 |
it under the terms of the GNU General Public License, version 2, as |
| 15 |
published by the Free Software Foundation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, |
| 18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
GNU General Public License for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License |
| 23 |
along with this program; if not, write to the Free Software |
| 24 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
*/ |
| 26 |
|
| 27 |
class UserLike { |
| 28 |
static $is_enabled = false; |
| 29 |
static $json; |
| 30 |
static $plugin_dir; |
| 31 |
|
| 32 |
function init(){ |
| 33 |
add_option("userlike_app_key"); |
| 34 |
add_option("userlike_secret"); |
| 35 |
add_option("userlike_json"); |
| 36 |
add_option("userlike_json_expire"); |
| 37 |
load_plugin_textdomain('userlike', false, dirname(plugin_basename(__FILE__))); |
| 38 |
self::$is_enabled = (get_option("userlike_app_key") && get_option("userlike_secret")); |
| 39 |
self::$plugin_dir = get_option('siteurl').'/'.PLUGINDIR.'/userlike/'; |
| 40 |
if(function_exists('current_user_can') && current_user_can('manage_options')){ |
| 41 |
add_action('admin_menu', array(__CLASS__, 'add_settings_page')); |
| 42 |
add_filter('plugin_action_links', array(__CLASS__, 'register_actions'), 10, 2); |
| 43 |
} |
| 44 |
|
| 45 |
if(self::$is_enabled){ |
| 46 |
add_action('wp_footer', array(__CLASS__, 'insert_code')); |
| 47 |
} else { |
| 48 |
add_action('admin_notices', array(__CLASS__, 'admin_notice')); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
function insert_code(){ |
| 53 |
/* Insert Userlike javascript code into the page */ |
| 54 |
if(!self::$is_enabled) return false; |
| 55 |
$app_key = get_option("userlike_app_key"); |
| 56 |
$secret = get_option("userlike_secret"); |
| 57 |
$json = self::get_json(); |
| 58 |
echo "<script src=\"//userlike.com/static/chat/javascripts/userlike.min.js\"></script><script>userlikeInit('".$app_key."','".$secret."','".$json."');</script>"; |
| 59 |
} |
| 60 |
|
| 61 |
function admin_notice(){ |
| 62 |
if(!self::$is_enabled) |
| 63 |
echo '<div class="error"><p><strong>'.sprintf(__('Userlike integration has not been set up. Please go to the <a href="%s">plugin page</a> and enter your Userlike credentials to enable it.' ), admin_url('options-general.php?page=userlike')).'</strong></p></div>'; |
| 64 |
} |
| 65 |
|
| 66 |
function add_settings_page(){ |
| 67 |
add_action('admin_init', array(__CLASS__, 'register_settings')); |
| 68 |
add_submenu_page('options-general.php', 'Userlike', 'Userlike', 'manage_options', 'userlike', array(__CLASS__, 'settings_page')); |
| 69 |
} |
| 70 |
|
| 71 |
/* Helper Functions */ |
| 72 |
function get_json($force=false){ |
| 73 |
/* |
| 74 |
Get the Userlike JSON data from cache, DB or Userlike servers (in that order). |
| 75 |
Set force to true if you want this function to fetch new data regardless of the cached value. |
| 76 |
Note that it will then fail if a JSON store cannot be received. |
| 77 |
*/ |
| 78 |
if(self::$json && !$force) return self::$json; |
| 79 |
if(time() < get_option("userlike_json_expire") && !$force) |
| 80 |
$json = get_option("userlike_json"); |
| 81 |
else |
| 82 |
$json = false; |
| 83 |
if($json && !$force){ |
| 84 |
self::$json = $json; |
| 85 |
return $json; |
| 86 |
} else { |
| 87 |
$secret = get_option("userlike_secret"); |
| 88 |
$ch = curl_init(); |
| 89 |
$timeout = 5; |
| 90 |
curl_setopt($ch,CURLOPT_URL,'http://userlike.com/api/chat/widget/config?widget_key='.$secret); |
| 91 |
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); |
| 92 |
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); |
| 93 |
$data = curl_exec($ch); |
| 94 |
curl_close($ch); |
| 95 |
if(preg_match("/NOT FOUND/i", $data)){ |
| 96 |
if($force) |
| 97 |
return false; |
| 98 |
else |
| 99 |
return get_option("userlike_json"); |
| 100 |
} |
| 101 |
self::$json = base64_encode($data); |
| 102 |
update_option("userlike_json", self::$json); |
| 103 |
update_option("userlike_json_expire", time()+21600); // expires every 6 hours |
| 104 |
return self::$json; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
function register_settings(){ |
| 109 |
register_setting('userlike', 'userlike_app_key'); |
| 110 |
register_setting('userlike', 'userlike_secret'); |
| 111 |
add_settings_section('userlike', 'Userlike', '', 'userlike'); |
| 112 |
} |
| 113 |
|
| 114 |
function register_actions($links, $file){ |
| 115 |
$this_plugin = plugin_basename(__FILE__); |
| 116 |
if($file == $this_plugin && function_exists('admin_url')){ |
| 117 |
$settings_link = '<a href="'.admin_url('options-general.php?page=userlike').'">'.__('Settings', 'userlike').'</a>'; |
| 118 |
array_unshift($links, $settings_link); |
| 119 |
} |
| 120 |
return($links); |
| 121 |
} |
| 122 |
|
| 123 |
function settings_page(){ |
| 124 |
if(self::$is_enabled){ |
| 125 |
if(!self::get_json(true)){ |
| 126 |
echo '<div id="setting-error-settings_error" class="error settings-error"><p><strong>Could not fetch configuration from Userlike. Please make sure that your credentials are correct! <a href="#" onClick="return userlikeChat();">Click here for live help</a></strong></p></div>'; |
| 127 |
} |
| 128 |
} |
| 129 |
$plugin_dir = self::$plugin_dir; |
| 130 |
$in_userlike = true; |
| 131 |
require_once "userlike.admin.php"; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
add_action('init', array('UserLike', 'init')); |