| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Userlike |
| 4 |
Plugin URI: http://userlike.com |
| 5 |
Description: Userlike live chat integration for Wordpress |
| 6 |
Version: 1.2 |
| 7 |
Author: Sven Gebhardt <sven@devcores.com> |
| 8 |
Author URI: http://devcores.com |
| 9 |
License: GPL2 |
| 10 |
|
| 11 |
Copyright 2012 Sven Gebhardt <sven@devcores.com> |
| 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_secret"); |
| 34 |
load_plugin_textdomain('userlike', false, dirname(plugin_basename(__FILE__))); |
| 35 |
if(get_option("userlike_secret") && strlen(get_option("userlike_secret")) > 30 && !preg_match("/[^a-f0-9]/", get_option("userlike_secret"))) |
| 36 |
self::$is_enabled = true; |
| 37 |
else |
| 38 |
self::$is_enabled = false; |
| 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 |
$secret = get_option("userlike_secret"); |
| 56 |
echo "<script src=\"http://widgets.userlike.com/".$secret.".js\"></script>"; |
| 57 |
} |
| 58 |
|
| 59 |
function admin_notice(){ |
| 60 |
if(!self::$is_enabled) |
| 61 |
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>'; |
| 62 |
} |
| 63 |
|
| 64 |
function add_settings_page(){ |
| 65 |
add_action('admin_init', array(__CLASS__, 'register_settings')); |
| 66 |
add_submenu_page('options-general.php', 'Userlike', 'Userlike', 'manage_options', 'userlike', array(__CLASS__, 'settings_page')); |
| 67 |
} |
| 68 |
|
| 69 |
/* Helper Functions */ |
| 70 |
|
| 71 |
function register_settings(){ |
| 72 |
register_setting('userlike', 'userlike_secret'); |
| 73 |
add_settings_section('userlike', 'Userlike', '', 'userlike'); |
| 74 |
} |
| 75 |
|
| 76 |
function register_actions($links, $file){ |
| 77 |
$this_plugin = plugin_basename(__FILE__); |
| 78 |
if($file == $this_plugin && function_exists('admin_url')){ |
| 79 |
$settings_link = '<a href="'.admin_url('options-general.php?page=userlike').'">'.__('Settings', 'userlike').'</a>'; |
| 80 |
array_unshift($links, $settings_link); |
| 81 |
} |
| 82 |
return($links); |
| 83 |
} |
| 84 |
|
| 85 |
function settings_page(){ |
| 86 |
if(get_option("userlike_secret") && !self::$is_enabled){ |
| 87 |
echo '<div id="setting-error-settings_error" class="error settings-error"><p><strong>Your credentials don\'t look like they are correct. Please make sure that your credentials are correct! <a href="#" onClick="return userlikeChat();">Click here for live help</a></strong></p></div>'; |
| 88 |
} |
| 89 |
$plugin_dir = self::$plugin_dir; |
| 90 |
$in_userlike = true; |
| 91 |
require_once "userlike.admin.php"; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
add_action('init', array('UserLike', 'init')); |
| 96 |
|