| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 |
/** |
| 4 |
* Hello Bar: The Original Popup Software (Top Bars, Exit Intents, Sliders, & More to Grow Your Email List!) |
| 5 |
* |
| 6 |
* Easily add your HelloBar to your WordPress blog! |
| 7 |
* |
| 8 |
* @package Hello Bar: The Original Popup Software (Top Bars, Exit Intents, Sliders, & More to Grow Your Email List!) |
| 9 |
* |
| 10 |
* @author hellobar |
| 11 |
* @version 1.5.3 |
| 12 |
*/ |
| 13 |
/* |
| 14 |
Plugin Name: Hello Bar Popup Builder |
| 15 |
Plugin URI: http://www.hellobar.com/ |
| 16 |
Description: The Original Popup Software (Top Bars, Exit Intents, Sliders, & More to Grow Your Email List!) |
| 17 |
Version: 1.5.3 |
| 18 |
Tested up to: 7.0 |
| 19 |
Requires at least: 5.0 |
| 20 |
Requires PHP: 7.4 |
| 21 |
Author: hellobar |
| 22 |
Author URI: http://www.hellobar.com |
| 23 |
License: GPLv2 or later |
| 24 |
License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 25 |
|
| 26 |
Copyright 2018 Hello Bar (email:support@hellobar.com) |
| 27 |
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
| 28 |
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 29 |
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 30 |
*/ |
| 31 |
class HelloBarForWordPress |
| 32 |
{ |
| 33 |
var $longname = "Hello Bar for WordPress"; |
| 34 |
var $shortname = "HelloBar"; |
| 35 |
var $namespace = 'hellobar-for-wordpress'; |
| 36 |
var $version = '1.5.3'; |
| 37 |
var $defaults = array('hellobar_code'=>"",'load_hellobar_in'=>'footer'); |
| 38 |
var $url_path; |
| 39 |
var $option_name; |
| 40 |
var $options; |
| 41 |
public static function init() |
| 42 |
{ |
| 43 |
$class = __CLASS__; |
| 44 |
new $class; |
| 45 |
} |
| 46 |
function __construct() |
| 47 |
{ |
| 48 |
$this->url_path = plugin_dir_url(__FILE__); |
| 49 |
$this->option_name = '_'.$this->namespace.'--options'; |
| 50 |
add_action('admin_menu', array($this,'admin_menu')); |
| 51 |
if (is_admin()) { |
| 52 |
} else { |
| 53 |
if ($this->get_option('load_hellobar_in') == 'header') { |
| 54 |
add_action('wp_head', array($this,'hellobar_print_script')); |
| 55 |
add_action('wp_head', array($this,'hellobar_insert_tags')); |
| 56 |
} else { |
| 57 |
if (function_exists('wp_print_footer_scripts')) { |
| 58 |
add_action('wp_print_footer_scripts', array($this,'hellobar_print_script')); |
| 59 |
add_action('wp_print_footer_scripts', array($this,'hellobar_insert_tags')); |
| 60 |
} else { |
| 61 |
add_action('wp_footer', array($this,'hellobar_print_script')); |
| 62 |
add_action('wp_footer', array($this,'hellobar_insert_tags')); |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
/* Loading Admin CSS only for Hellobar option page */ |
| 67 |
if (isset($_GET['page']) && $_GET['page']=='hellobar') { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 68 |
add_action('admin_enqueue_scripts', array($this,'add_css_in_admin')); |
| 69 |
add_action('admin_enqueue_scripts', array($this,'add_js_in_admin')); |
| 70 |
} |
| 71 |
} |
| 72 |
public function add_css_in_admin() |
| 73 |
{ |
| 74 |
wp_register_style('hellobaradmincss', $this->url_path.'/assets/css/hellobar-admin.css', array(), $this->version); |
| 75 |
wp_enqueue_style('hellobaradmincss'); |
| 76 |
} |
| 77 |
public function add_js_in_admin() |
| 78 |
{ |
| 79 |
wp_register_script('qtipjs', $this->url_path.'/assets/js/jquery.qtip.min.js', array('jquery'), '3.0.3', false); |
| 80 |
wp_enqueue_script('qtipjs'); |
| 81 |
} |
| 82 |
public function hellobar_insert_tags() |
| 83 |
{ |
| 84 |
if (is_home()) { |
| 85 |
return; |
| 86 |
} |
| 87 |
$posttags = get_the_tags(); |
| 88 |
if ($posttags) { |
| 89 |
foreach($posttags as $tag) { |
| 90 |
echo '<script type="text/javascript">window._hellobar_wordpress_tags = window._hellobar_wordpress_tags || []; window._hellobar_wordpress_tags.push(' . json_encode(strval($tag->name)) . '); </script>'; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
public function hellobar_print_script() |
| 95 |
{ |
| 96 |
$hellobar_code = $this->get_option('hellobar_code'); |
| 97 |
$newapikey = get_option('hellobar_api_key', true); |
| 98 |
if ($newapikey) { |
| 99 |
echo '<script src="' . esc_url('https://my.hellobar.com/' . $hellobar_code . '.js') . '" type="text/javascript" charset="utf-8" async="async"></script>'; |
| 100 |
} else { |
| 101 |
if (!empty($hellobar_code)) { |
| 102 |
$hellobar_code = html_entity_decode($hellobar_code); |
| 103 |
if ($this->get_option('load_hellobar_in')=='header') { |
| 104 |
$output = preg_replace("/<noscript>(.*)<\/noscript>/ism", "", $hellobar_code); |
| 105 |
} else { |
| 106 |
$output = $hellobar_code; |
| 107 |
} |
| 108 |
echo "\n".$output; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
public function admin_menu() |
| 113 |
{ |
| 114 |
add_menu_page($this->shortname, $this->shortname, 'manage_options', 'hellobar', array($this,'admin_options_page'), ($this->url_path.'/images/icon.png')); |
| 115 |
} |
| 116 |
public function admin_options_page() |
| 117 |
{ |
| 118 |
if (!current_user_can('manage_options')) { |
| 119 |
wp_die('You do not have sufficient permissions to access this page'); |
| 120 |
} |
| 121 |
if (isset($_POST) && !empty($_POST)) { |
| 122 |
if (isset($_REQUEST[$this->namespace.'_update_wpnonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST[$this->namespace.'_update_wpnonce'])), $this->namespace.'_options')) { |
| 123 |
$data = array(); |
| 124 |
foreach ($_POST as $key => $val) { |
| 125 |
$data[$key] = $this->sanitize_data($val); |
| 126 |
} |
| 127 |
switch($data['form_action']){ |
| 128 |
case "update_options": |
| 129 |
$options = array( |
| 130 |
'hellobar_code' => (string) $this->sanitize_hellobar_code(isset($_POST['hellobar_code']) ? wp_unslash($_POST['hellobar_code']) : ''), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 131 |
'load_hellobar_in' => (string) (!empty($data['load_hellobar_in']) ? $data['load_hellobar_in'] : 'footer') |
| 132 |
); |
| 133 |
update_option($this->option_name, $options); |
| 134 |
$this->options = get_option($this->option_name); |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
$page_title = $this->longname.' Options'; |
| 140 |
$namespace = $this->namespace; |
| 141 |
$defaults = $this->defaults; |
| 142 |
$plugin_path = $this->url_path; |
| 143 |
foreach ($this->defaults as $name => $default_value) { |
| 144 |
$$name = $this->get_option($name); |
| 145 |
} |
| 146 |
include dirname(__FILE__).'/views/options.php'; |
| 147 |
} |
| 148 |
private function get_option( $option_name ) |
| 149 |
{ |
| 150 |
// Load option values if they haven't been loaded already |
| 151 |
if (!isset($this->options) || empty($this->options)) { |
| 152 |
$this->options = get_option($this->option_name, $this->defaults); |
| 153 |
} |
| 154 |
if (isset($this->options[$option_name])) { |
| 155 |
return $this->options[$option_name]; |
| 156 |
} elseif (isset($this->defaults[$option_name])) { |
| 157 |
return $this->defaults[$option_name]; |
| 158 |
} |
| 159 |
return false; |
| 160 |
} |
| 161 |
private function sanitize_data($str="") |
| 162 |
{ |
| 163 |
if (!function_exists('wp_kses')) { |
| 164 |
include_once ABSPATH.'wp-includes/kses.php'; |
| 165 |
} |
| 166 |
global $allowedposttags; |
| 167 |
global $allowedprotocols; |
| 168 |
if (is_string($str)) { |
| 169 |
$str = sanitize_text_field(stripslashes($str)); |
| 170 |
} |
| 171 |
return $str; |
| 172 |
} |
| 173 |
private function sanitize_hellobar_code($code="") |
| 174 |
{ |
| 175 |
$code = trim($code); |
| 176 |
// Old stored values may be entity-encoded — decode first so the pattern match works |
| 177 |
// html_entity_decode on a plain string is harmless |
| 178 |
$code = html_entity_decode($code, ENT_QUOTES, 'UTF-8'); |
| 179 |
// Accept a full <script> embed pointing to the hellobar CDN |
| 180 |
if (preg_match('/^<script\b[^>]*\bsrc=["\']https:\/\/my\.hellobar\.com\/[a-zA-Z0-9_-]+\.js["\'][^>]*>\s*<\/script>$/i', $code)) { |
| 181 |
return $code; |
| 182 |
} |
| 183 |
// Accept a bare API key (used by reset_old_api migration path) |
| 184 |
if (preg_match('/^[a-zA-Z0-9_-]+$/', $code)) { |
| 185 |
return $code; |
| 186 |
} |
| 187 |
return ''; |
| 188 |
} |
| 189 |
public static function is_script() |
| 190 |
{ |
| 191 |
$options = get_option('_hellobar-for-wordpress--options'); |
| 192 |
$hellobar_code = ''; |
| 193 |
if (is_array($options) && isset($options['hellobar_code'])) { |
| 194 |
$hellobar_code = html_entity_decode($options['hellobar_code']); |
| 195 |
} |
| 196 |
if ($hellobar_code) { |
| 197 |
$count = preg_match('/src=(["\'])(.*?)\1/', $hellobar_code, $match); |
| 198 |
if ($count === false) { |
| 199 |
return false; |
| 200 |
} else { |
| 201 |
if (!empty($match)) { |
| 202 |
$jsurl = $match[2]; |
| 203 |
$parts = wp_parse_url($jsurl); |
| 204 |
$path = $parts['path']; |
| 205 |
return HelloBarForWordPress::get_api_code($path); |
| 206 |
} else { |
| 207 |
return false; |
| 208 |
} |
| 209 |
} |
| 210 |
} else { |
| 211 |
return false; |
| 212 |
} |
| 213 |
} |
| 214 |
public static function get_api_code($path) |
| 215 |
{ |
| 216 |
$path1 = str_replace('/', '', $path); |
| 217 |
$path2 = str_replace('.js', '', $path1); |
| 218 |
return $path2; |
| 219 |
} |
| 220 |
public static function reset_old_api($api) |
| 221 |
{ |
| 222 |
update_option('hellobar_api_key', $api); |
| 223 |
$options = array( |
| 224 |
'hellobar_code' => $api, |
| 225 |
'load_hellobar_in' => 'footer' |
| 226 |
); |
| 227 |
update_option('_hellobar-for-wordpress--options', $options); |
| 228 |
} |
| 229 |
} |
| 230 |
/** |
| 231 |
* Load Class on Plugin Load |
| 232 |
*/ |
| 233 |
add_action('plugins_loaded', array('HelloBarForWordPress','init')); |
| 234 |
?> |
| 235 |
|