| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: VWO |
| 4 |
Plugin URI: https://vwo.com/ |
| 5 |
Description: VWO is the all-in-one platform that helps you conduct visitor research, build an optimization roadmap, and run continuous experimentation. Simply enable the plugin and start running tests on your Wordpress website without doing any other code changes. Visit <a href="https://vwo.com/">VWO</a> for more details. |
| 6 |
Author: VWO |
| 7 |
Version: 3.6 |
| 8 |
visual-website-optimizer.php |
| 9 |
Author URI: https://vwo.com/ |
| 10 |
|
| 11 |
This relies on the actions being present in the themes header.php and footer.php |
| 12 |
* header.php code before the closing </head> tag |
| 13 |
* wp_head(); |
| 14 |
* |
| 15 |
*/ |
| 16 |
|
| 17 |
//------------------------------------------------------------------------// |
| 18 |
//---Config---------------------------------------------------------------// |
| 19 |
//------------------------------------------------------------------------// |
| 20 |
|
| 21 |
/** |
| 22 |
* Generate Common Code |
| 23 |
* @param type $vwo_id integer |
| 24 |
* @return type string |
| 25 |
*/ |
| 26 |
function get_vwo_clhf_script_common_code($vwo_clicks = 10) { |
| 27 |
ob_start(); |
| 28 |
?> |
| 29 |
<!-- Start VWO Common Smartcode --> |
| 30 |
<script <?php echo vwo_clhf_ignore_js_attr(); ?> type='text/javascript'> |
| 31 |
var _vwo_clicks = <?php echo $vwo_clicks ?>; |
| 32 |
</script> |
| 33 |
<!-- End VWO Common Smartcode --> |
| 34 |
<?php |
| 35 |
$script_code = ob_get_clean(); |
| 36 |
return $script_code; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get ignore js field setting |
| 41 |
* @return type boolean |
| 42 |
*/ |
| 43 |
function get_vwo_clhf_ignore_js(){ |
| 44 |
$ignore_js = get_option('ignore_js'); |
| 45 |
$ignore_js = ($ignore_js == '1') ? true : false; |
| 46 |
return $ignore_js; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get ignore js script attribute value |
| 51 |
* @return type boolean |
| 52 |
*/ |
| 53 |
function vwo_clhf_ignore_js_attr(){ |
| 54 |
$ignore_js = get_vwo_clhf_ignore_js(); |
| 55 |
$js_attr = ''; |
| 56 |
if(function_exists('get_vwo_clhf_ignore_js') && $ignore_js){ |
| 57 |
$js_attr = 'data-cfasync="false"'; |
| 58 |
} |
| 59 |
return $js_attr; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Generate Synchronous Code |
| 64 |
* @param type $vwo_id integer |
| 65 |
* @return type string |
| 66 |
*/ |
| 67 |
function get_vwo_clhf_script_sync_code($vwo_id = 0) { |
| 68 |
ob_start(); |
| 69 |
?> |
| 70 |
<!-- Start VWO Smartcode --> |
| 71 |
<script <?php echo vwo_clhf_ignore_js_attr();?> src="https://dev.visualwebsiteoptimizer.com/lib/<?php echo $vwo_id ?>.js"></script> |
| 72 |
<!-- End VWO Smartcode --> |
| 73 |
<?php |
| 74 |
$sync_script = ob_get_clean(); |
| 75 |
return $sync_script; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Generate Asynchronous Code |
| 80 |
* @param type $vwo_id integer |
| 81 |
* @param type $settings_tolerance integer |
| 82 |
* @param type $library_tolerance integer |
| 83 |
* @param type $use_existing_jquery string |
| 84 |
* @return type string |
| 85 |
*/ |
| 86 |
function get_vwo_clhf_script_async_code($vwo_id = 0, $settings_tolerance, $library_tolerance, $use_existing_jquery) { |
| 87 |
ob_start(); |
| 88 |
?> |
| 89 |
<!-- Start VWO Async Smartcode --> |
| 90 |
<script <?php echo vwo_clhf_ignore_js_attr(); ?> type='text/javascript'> |
| 91 |
window._vwo_code = window._vwo_code || (function(){ |
| 92 |
var account_id= <?php echo $vwo_id ?>, |
| 93 |
settings_tolerance= <?php echo $settings_tolerance ?>, |
| 94 |
library_tolerance= <?php echo $library_tolerance ?>, |
| 95 |
use_existing_jquery= <?php echo ($use_existing_jquery) ? 'true' : 'false'; ?>, |
| 96 |
is_spa=1, |
| 97 |
hide_element='body', |
| 98 |
/* DO NOT EDIT BELOW THIS LINE */ |
| 99 |
f=false,d=document,code={use_existing_jquery:function(){return use_existing_jquery;},library_tolerance:function(){return library_tolerance;},finish:function(){if(!f){f=true;var a=d.getElementById('_vis_opt_path_hides');if(a)a.parentNode.removeChild(a);}},finished:function(){return f;},load:function(a){var b=d.createElement('script');b.src=a;b.type='text/javascript';b.innerText;b.onerror=function(){_vwo_code.finish();};d.getElementsByTagName('head')[0].appendChild(b);},init:function(){ |
| 100 |
window.settings_timer=setTimeout('_vwo_code.finish()',settings_tolerance);var a=d.createElement('style'),b=hide_element?hide_element+'{opacity:0 !important;filter:alpha(opacity=0) !important;background:none !important;}':'',h=d.getElementsByTagName('head')[0];a.setAttribute('id','_vis_opt_path_hides');a.setAttribute('type','text/css');if(a.styleSheet)a.styleSheet.cssText=b;else a.appendChild(d.createTextNode(b));h.appendChild(a);this.load('https://dev.visualwebsiteoptimizer.com/j.php?a='+account_id+'&u='+encodeURIComponent(d.URL)+'&f='+(+is_spa)+'&r='+Math.random());return settings_timer; }};window._vwo_settings_timer = code.init(); return code; }()); |
| 101 |
</script> |
| 102 |
<!-- End VWO Async Smartcode --> |
| 103 |
<?php |
| 104 |
$async_script = ob_get_clean(); |
| 105 |
return $async_script; |
| 106 |
} |
| 107 |
|
| 108 |
//------------------------------------------------------------------------// |
| 109 |
//---Hook-----------------------------------------------------------------// |
| 110 |
//------------------------------------------------------------------------// |
| 111 |
add_action( 'wp_head', 'vwo_clhf_headercode',1 ); |
| 112 |
add_action( 'admin_menu', 'vwo_clhf_plugin_menu' ); |
| 113 |
add_action( 'admin_init', 'vwo_clhf_register_mysettings' ); |
| 114 |
add_action( 'admin_notices','vwo_clhf_warn_nosettings'); |
| 115 |
|
| 116 |
|
| 117 |
//------------------------------------------------------------------------// |
| 118 |
//---Functions------------------------------------------------------------// |
| 119 |
//------------------------------------------------------------------------// |
| 120 |
// options page link |
| 121 |
function vwo_clhf_plugin_menu() { |
| 122 |
add_options_page('Visual Website Optimizer', 'VWO', 'create_users', 'clhf_vwo_options', 'vwo_clhf_plugin_options'); |
| 123 |
} |
| 124 |
|
| 125 |
// whitelist settings |
| 126 |
function vwo_clhf_register_mysettings(){ |
| 127 |
register_setting('clhf_vwo_options','vwo_id','intval'); |
| 128 |
register_setting('clhf_vwo_options','code_type'); |
| 129 |
register_setting('clhf_vwo_options','vwo_clicks'); |
| 130 |
register_setting('clhf_vwo_options','ignore_js','boolval'); |
| 131 |
register_setting('clhf_vwo_options','settings_tolerance','intval'); |
| 132 |
register_setting('clhf_vwo_options','library_tolerance','intval'); |
| 133 |
register_setting('clhf_vwo_options','use_existing_jquery','boolval'); |
| 134 |
} |
| 135 |
|
| 136 |
//------------------------------------------------------------------------// |
| 137 |
//---Output Functions-----------------------------------------------------// |
| 138 |
//------------------------------------------------------------------------// |
| 139 |
function vwo_clhf_headercode() { |
| 140 |
// runs in the header |
| 141 |
$vwo_id = get_option('vwo_id'); |
| 142 |
$code_type = get_option('code_type'); |
| 143 |
|
| 144 |
if ($vwo_id) { |
| 145 |
if(empty(get_option('vwo_clicks'))){ |
| 146 |
update_option('vwo_clicks', '10' ); |
| 147 |
} |
| 148 |
$vwo_clicks = get_option('vwo_clicks'); |
| 149 |
//common script code |
| 150 |
echo get_vwo_clhf_script_common_code($vwo_clicks); |
| 151 |
if ($code_type == 'SYNC') { |
| 152 |
//sync script code |
| 153 |
echo get_vwo_clhf_script_sync_code($vwo_id); // only output if options were saved |
| 154 |
} else { |
| 155 |
|
| 156 |
$settings_tolerance = get_option('settings_tolerance'); |
| 157 |
if (!is_numeric($settings_tolerance)) |
| 158 |
$settings_tolerance = 2000; |
| 159 |
|
| 160 |
$library_tolerance = get_option('library_tolerance'); |
| 161 |
if (!is_numeric($library_tolerance)) |
| 162 |
$library_tolerance = 2500; |
| 163 |
|
| 164 |
$use_existing_jquery = get_option('use_existing_jquery'); |
| 165 |
$use_existing_jquery = ($use_existing_jquery == 1) ? true : false; |
| 166 |
|
| 167 |
//async script code |
| 168 |
echo get_vwo_clhf_script_async_code($vwo_id, $settings_tolerance, $library_tolerance, $use_existing_jquery); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
//------------------------------------------------------------------------// |
| 174 |
//---Page Output Functions------------------------------------------------// |
| 175 |
//------------------------------------------------------------------------// |
| 176 |
// options page |
| 177 |
function vwo_clhf_plugin_options() { |
| 178 |
//Set value for checkbox by default |
| 179 |
$ignore_js_options = get_option('ignore_js'); |
| 180 |
$show_ignore_js = $ignore_js_options; |
| 181 |
if( $ignore_js_options === false ) { |
| 182 |
// nothing is set, so apply the default here |
| 183 |
$show_ignore_js = 1; |
| 184 |
update_option('ignore_js', '1'); |
| 185 |
} |
| 186 |
|
| 187 |
echo '<div class="wrap">';?> |
| 188 |
<h2>VWO</h2> |
| 189 |
<p>You need to have a <a href="https://vwo.com/">VWO</a> account in order to use this plugin. This plugin inserts the neccessary code into your Wordpress site automatically without you having to touch anything. In order to use the plugin, you need to enter your VWO Account ID:</p> |
| 190 |
<form method="post" action="options.php"> |
| 191 |
<?php settings_fields( 'clhf_vwo_options' ); ?> |
| 192 |
<table class="form-table"> |
| 193 |
<tr valign="top"> |
| 194 |
<th scope="row">Your VWO Account ID</th> |
| 195 |
<td><input type="text" name="vwo_id" value="<?php echo get_option('vwo_id'); ?>" /></td> |
| 196 |
</tr> |
| 197 |
<tr valign="top"> |
| 198 |
<th scope="row">No. of Heatmap Clicks</th> |
| 199 |
<td><input type="number" name="vwo_clicks" value="<?php echo get_option('vwo_clicks') ? get_option('vwo_clicks'):10 ; ?>" min="3"/></td> |
| 200 |
</tr> |
| 201 |
<tr valign="top"> |
| 202 |
<th scope="row">Code (default: Asynchronous)</th> |
| 203 |
<td> |
| 204 |
<input style="vertical-align: text-top;" type="radio" onclick="selectCodeType();" name="code_type" id="code_type_async" value="ASYNC" <?php if(get_option('code_type')!='SYNC') echo "checked"; ?> /> Asynchronous |
| 205 |
<input style="vertical-align: text-top;" type="radio" onclick="selectCodeType();" name="code_type" id="code_type_sync" value="SYNC" <?php if(get_option('code_type')=='SYNC') echo "checked"; ?> /> Synchronous |
| 206 |
<a href="https://vwo.com/blog/asynchronous-code" target="_blank">[Help]</a> |
| 207 |
</td> |
| 208 |
</tr> |
| 209 |
<tr valign="top" id='asyncOnly1' <?php if(get_option('code_type')=='SYNC') echo "style='display:none;'" ?>> |
| 210 |
<th scope="row">Settings Timeout</th> |
| 211 |
<td style="vertical-align: middle;"><input type="text" name="settings_tolerance" value="<?php echo get_option('settings_tolerance')?get_option('settings_tolerance'):2000; ?>" />ms (default: 2000)</td> |
| 212 |
</tr> |
| 213 |
<tr valign="top" id='asyncOnly2' <?php if(get_option('code_type')=='SYNC') echo "style='display:none;'" ?>> |
| 214 |
<th scope="row">Library Timeout</th> |
| 215 |
<td style="vertical-align: middle;"><input type="text" name="library_tolerance" value="<?php echo get_option('library_tolerance')?get_option('library_tolerance'):2500; ?>" />ms (default: 2500)</td> |
| 216 |
</tr> |
| 217 |
<tr valign="top" id='asyncOnly3' <?php if(get_option('code_type')=='SYNC') echo "style='display:none;'" ?>> |
| 218 |
<th scope="row">Use Existing jQuery</th> |
| 219 |
<td style="vertical-align: middle;"><input type="checkbox" name="use_existing_jquery" value="1" <?php if(get_option('use_existing_jquery') == true) echo "checked"; ?>/>Yes</td> |
| 220 |
</tr> |
| 221 |
<tr valign="top"> |
| 222 |
<th scope="row">Skip Deferred Execution</th> |
| 223 |
<td style="vertical-align: middle;"><input type="checkbox" name="ignore_js" value="1" <?php if($show_ignore_js == true) echo "checked"; ?>/>Yes</td> |
| 224 |
</tr> |
| 225 |
</table> |
| 226 |
|
| 227 |
<script type="text/javascript"> |
| 228 |
function selectCodeType() { |
| 229 |
var code_type = 'ASYNC'; |
| 230 |
if(document.getElementById('code_type_sync').checked) |
| 231 |
code_type = 'SYNC'; |
| 232 |
|
| 233 |
if(code_type == 'ASYNC') { |
| 234 |
document.getElementById('asyncOnly1').style.display = 'table-row'; |
| 235 |
document.getElementById('asyncOnly2').style.display = 'table-row'; |
| 236 |
document.getElementById('asyncOnly3').style.display = 'table-row'; |
| 237 |
} |
| 238 |
else { |
| 239 |
document.getElementById('asyncOnly1').style.display = 'none'; |
| 240 |
document.getElementById('asyncOnly2').style.display = 'none'; |
| 241 |
document.getElementById('asyncOnly3').style.display = 'none'; |
| 242 |
} |
| 243 |
} |
| 244 |
</script> |
| 245 |
<p class="submit"><input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /></p> |
| 246 |
<p>Your Account ID (a number) can be found in <i>Settings</i> area (top-right) after you <a href="https://app.vwo.com">login</a> into your VWO account.</p> |
| 247 |
<?php |
| 248 |
echo '</div>'; |
| 249 |
} |
| 250 |
|
| 251 |
function vwo_clhf_warn_nosettings(){ |
| 252 |
if (!is_admin()) |
| 253 |
return; |
| 254 |
|
| 255 |
$clhf_option = get_option("vwo_id"); |
| 256 |
if (!$clhf_option || $clhf_option < 1){ |
| 257 |
echo "<div id='vwo-warning' class='updated fade'><p><strong>VWO is almost ready.</strong> You must <a href=\"options-general.php?page=clhf_vwo_options\">enter your Account ID</a> for it to work.</p></div>"; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'vwo_clhf_add_plugin_page_settings_link'); |
| 263 |
/** |
| 264 |
* Function to add Settings Links on Plugin page |
| 265 |
* @param array $links |
| 266 |
* @return string |
| 267 |
*/ |
| 268 |
function vwo_clhf_add_plugin_page_settings_link( $links ) { |
| 269 |
$links[] = '<a href="' . |
| 270 |
admin_url( 'options-general.php?page=clhf_vwo_options' ) . |
| 271 |
'">' . __('Settings') . '</a>'; |
| 272 |
return $links; |
| 273 |
} |
| 274 |
|
| 275 |
?> |