| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Userlike |
| 4 |
Plugin URI: https://www.userlike.com |
| 5 |
Description: Userlike Live Chat integration for Wordpress |
| 6 |
Version: 2.5 |
| 7 |
Author: David Voswinkel <david.voswinkel@userlike.com> |
| 8 |
Author URI: https://www.userlike.com |
| 9 |
License: GPL2 |
| 10 |
|
| 11 |
This program is free software; you can redistribute it and/or modify |
| 12 |
it under the terms of the GNU General Public License, version 2, as |
| 13 |
published by the Free Software Foundation. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 |
*/ |
| 24 |
|
| 25 |
class UserLike { |
| 26 |
static $is_enabled = false; |
| 27 |
static $json; |
| 28 |
static $plugin_dir; |
| 29 |
|
| 30 |
public static function init(){ |
| 31 |
add_option("userlike_secret"); |
| 32 |
load_plugin_textdomain('userlike', false, dirname(plugin_basename(__FILE__))); |
| 33 |
if(get_option("userlike_secret") && strlen(get_option("userlike_secret")) > 30 && !preg_match("/[^a-f0-9]/", get_option("userlike_secret"))) |
| 34 |
self::$is_enabled = true; |
| 35 |
else |
| 36 |
self::$is_enabled = false; |
| 37 |
self::$plugin_dir = get_option('siteurl').'/'.PLUGINDIR.'/userlike/'; |
| 38 |
if(function_exists('current_user_can') && current_user_can('manage_options')){ |
| 39 |
add_action('admin_menu', array(__CLASS__, 'add_settings_page')); |
| 40 |
add_filter('plugin_action_links', array(__CLASS__, 'register_actions'), 10, 2); |
| 41 |
} |
| 42 |
|
| 43 |
if(self::$is_enabled){ |
| 44 |
add_action('wp_footer', array(__CLASS__, 'insert_code')); |
| 45 |
} else { |
| 46 |
add_action('admin_notices', array(__CLASS__, 'admin_notice')); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
public static function insert_code(){ |
| 51 |
/* Insert Userlike javascript code into the page */ |
| 52 |
if(!self::$is_enabled) return false; |
| 53 |
$secret = get_option("userlike_secret"); |
| 54 |
echo "<script async type=\"text/javascript\" src=\"https://s3-eu-west-1.amazonaws.com/userlike-cdn-widgets/".$secret.".js\"></script>"; |
| 55 |
} |
| 56 |
|
| 57 |
public static function admin_notice(){ |
| 58 |
if(!self::$is_enabled) |
| 59 |
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>'; |
| 60 |
} |
| 61 |
|
| 62 |
public static function add_settings_page(){ |
| 63 |
add_action('admin_init', array(__CLASS__, 'register_settings')); |
| 64 |
add_submenu_page('options-general.php', 'Userlike', 'Userlike', 'manage_options', 'userlike', array(__CLASS__, 'settings_page')); |
| 65 |
} |
| 66 |
|
| 67 |
/* Helper Functions */ |
| 68 |
|
| 69 |
public static function register_settings(){ |
| 70 |
register_setting('userlike', 'userlike_secret'); |
| 71 |
add_settings_section('userlike', 'Userlike', '', 'userlike'); |
| 72 |
} |
| 73 |
|
| 74 |
public static function register_actions($links, $file){ |
| 75 |
$this_plugin = plugin_basename(__FILE__); |
| 76 |
if($file == $this_plugin && function_exists('admin_url')){ |
| 77 |
$settings_link = '<a href="'.admin_url('options-general.php?page=userlike').'">'.__('Settings', 'userlike').'</a>'; |
| 78 |
array_unshift($links, $settings_link); |
| 79 |
} |
| 80 |
return($links); |
| 81 |
} |
| 82 |
|
| 83 |
public static function settings_page(){ |
| 84 |
if(get_option("userlike_secret") && !self::$is_enabled){ |
| 85 |
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 userlikeStartChat();">Click here for live help</a></strong></p></div>'; |
| 86 |
} |
| 87 |
$plugin_dir = self::$plugin_dir; |
| 88 |
$in_userlike = true; |
| 89 |
require_once "userlike.admin.php"; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
add_action('init', array('UserLike', 'init')); |
| 94 |
|