| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Tracking Script Manager |
| 4 |
* Plugin URI: http://wordpress.org/plugins/tracking-script-manager/ |
| 5 |
* Description: A plugin that allows you to add tracking scripts to your site. |
| 6 |
* Version: 1.1.3 |
| 7 |
* Author: Red8 Interactive |
| 8 |
* Author URI: http://red8interactive.com |
| 9 |
* License: GPL2 |
| 10 |
*/ |
| 11 |
|
| 12 |
/* |
| 13 |
Copyright 2014 Red8 Interactive (email : james@red8interactive.com) |
| 14 |
|
| 15 |
This program is free software; you can redistribute it and/or |
| 16 |
modify it under the terms of the GNU General Public License |
| 17 |
as published by the Free Software Foundation; either version 2 |
| 18 |
of the License, or (at your option) any later version. |
| 19 |
|
| 20 |
This program is distributed in the hope that it will be useful, |
| 21 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
GNU General Public License for more details. |
| 24 |
|
| 25 |
You should have received a copy of the GNU General Public License |
| 26 |
along with this program; if not, write to the Free Software |
| 27 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 28 |
*/ |
| 29 |
|
| 30 |
if ( ! defined( 'ABSPATH' ) ) { |
| 31 |
exit; // Exit if accessed directly |
| 32 |
} |
| 33 |
|
| 34 |
if( !class_exists('Tracking_Scripts') ) { |
| 35 |
|
| 36 |
class Tracking_Scripts { |
| 37 |
|
| 38 |
function __construct() { |
| 39 |
self::define_constants(); |
| 40 |
self::load_hooks(); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Defines plugin constants |
| 45 |
*/ |
| 46 |
public static function define_constants() { |
| 47 |
define('TRACKING_SCRIPT_PATH', plugins_url( ' ', __FILE__ ) ); |
| 48 |
define('TRACKING_SCRIPT_BASENAME', plugin_basename( __FILE__ )); |
| 49 |
|
| 50 |
define('WP_TRACKING_SCRIPTS_OPTION_GROUP', 'tracking_scripts_options' ); |
| 51 |
define('WP_HEADER_TRACKING_SCRIPT', 'header_tracking_script_code' ); |
| 52 |
define('WP_FOOTER_TRACKING_SCRIPT', 'footer_tracking_script_code' ); |
| 53 |
define('WP_PAGE_TRACKING_SCRIPT', 'page_tracking_script_code' ); |
| 54 |
define('WP_NEW_HEADER_TRACKING_SCRIPT', 'new_header_tracking_script_code' ); |
| 55 |
define('WP_NEW_FOOTER_TRACKING_SCRIPT', 'new_footer_tracking_script_code' ); |
| 56 |
define('WP_NEW_PAGE_TRACKING_SCRIPT', 'new_page_tracking_script_code' ); |
| 57 |
define('WP_NEW_PAGE_TRACKING_SCRIPT_ID', 'new_page_tracking_script_code_id' ); |
| 58 |
define('WP_NEW_PAGE_TRACKING_SCRIPT_LOCATION', 'new_page_tracking_script_code_location' ); |
| 59 |
define('WP_NEW_PAGE_TRACKING_SCRIPT_GLOBAL', 'new_page_tracking_script_code_global' ); |
| 60 |
define('WP_PAGE_TRACKING_SCRIPT_COUNT', 'page_tracking_script_count'); |
| 61 |
} |
| 62 |
|
| 63 |
public static function load_hooks() { |
| 64 |
add_action('wp_head', array(__CLASS__, 'find_header_tracking_codes')); |
| 65 |
|
| 66 |
add_action('wp_footer', array(__CLASS__, 'find_footer_tracking_codes')); |
| 67 |
|
| 68 |
add_action('admin_menu', array(__CLASS__, 'tracking_scripts_create_menu')); |
| 69 |
|
| 70 |
add_action('admin_init', array(__CLASS__, 'initialize_admin_posts')); |
| 71 |
|
| 72 |
add_action( 'wp_ajax_tracking_scripts_get_posts', array(__CLASS__, 'tracking_scripts_posts_ajax_handler') ); |
| 73 |
|
| 74 |
add_action( 'wp_ajax_tracking_scripts_get_post_content', array(__CLASS__, 'tracking_scripts_post_content_ajax_handler') ); |
| 75 |
} |
| 76 |
|
| 77 |
/************************************************* |
| 78 |
* Front End |
| 79 |
**************************************************/ |
| 80 |
|
| 81 |
// Header Tracking Codes |
| 82 |
public static function find_header_tracking_codes() { |
| 83 |
$header_scripts = unserialize(get_option(WP_HEADER_TRACKING_SCRIPT)); |
| 84 |
|
| 85 |
if($header_scripts) { |
| 86 |
foreach($header_scripts as $script) { |
| 87 |
if($script->active) { |
| 88 |
echo html_entity_decode(esc_attr($script->script_code), ENT_QUOTES, 'cp1252'); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
$page_scripts = unserialize(get_option(WP_PAGE_TRACKING_SCRIPT)); |
| 94 |
|
| 95 |
if($page_scripts) { |
| 96 |
global $wp_query; |
| 97 |
$post_id = $wp_query->post->ID; |
| 98 |
foreach($page_scripts as $script) { |
| 99 |
if($script->active && $script->location == 'header' && $script->page_id == $post_id) { |
| 100 |
echo html_entity_decode(esc_attr($script->script_code), ENT_QUOTES, 'cp1252'); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
// Footer Tracking Codes |
| 108 |
public static function find_footer_tracking_codes() { |
| 109 |
$footer_scripts = unserialize(get_option(WP_FOOTER_TRACKING_SCRIPT)); |
| 110 |
|
| 111 |
if($footer_scripts) { |
| 112 |
foreach($footer_scripts as $script) { |
| 113 |
if($script->active) { |
| 114 |
echo html_entity_decode(esc_attr($script->script_code), ENT_QUOTES, 'cp1252'); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
$page_scripts = unserialize(get_option(WP_PAGE_TRACKING_SCRIPT)); |
| 120 |
|
| 121 |
if($page_scripts) { |
| 122 |
global $wp_query; |
| 123 |
$post_id = $wp_query->post->ID; |
| 124 |
foreach($page_scripts as $script) { |
| 125 |
if($script->active && $script->location == 'footer' && $script->page_id == $post_id) { |
| 126 |
echo html_entity_decode(esc_attr($script->script_code), ENT_QUOTES, 'cp1252'); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/************************************************* |
| 133 |
* Admin Area |
| 134 |
**************************************************/ |
| 135 |
|
| 136 |
public static function tracking_scripts_create_menu() { |
| 137 |
add_menu_page('Tracking Scripts Manager', 'Tracking Scripts Manager', 'activate_plugins', __FILE__, array(__CLASS__, 'tracking_options'), ''); |
| 138 |
add_action('admin_init', array(__CLASS__, 'register_tracking_scripts_settings')); |
| 139 |
} |
| 140 |
|
| 141 |
public static function register_tracking_scripts_settings() { |
| 142 |
register_setting( WP_TRACKING_SCRIPTS_OPTION_GROUP, WP_HEADER_TRACKING_SCRIPT, 'esc_textarea' ); |
| 143 |
register_setting( WP_TRACKING_SCRIPTS_OPTION_GROUP, WP_FOOTER_TRACKING_SCRIPT, 'esc_textarea' ); |
| 144 |
} |
| 145 |
|
| 146 |
public static function tracking_scripts_admin_tabs( $current = 'add_new' ) { |
| 147 |
$tabs = array('add_new' => 'Add New', 'global' => 'Global', 'pages' => 'Specific Location'); |
| 148 |
echo '<h2 style="font-size: 22px; font-weight: bold; margin: 10px 0 40px;">Tracking Scripts Manager</h2>'; |
| 149 |
echo '<h2 class="nav-tab-wrapper">'; |
| 150 |
foreach($tabs as $tab => $name) { |
| 151 |
$class = ($tab == $current) ? ' nav-tab-active' : ''; |
| 152 |
echo "<a class='nav-tab$class' href='?page=".TRACKING_SCRIPT_BASENAME."&tab=$tab'>$name</a>"; |
| 153 |
} |
| 154 |
echo '</h2>'; |
| 155 |
} |
| 156 |
|
| 157 |
public static function tracking_options() { |
| 158 |
self::tracking_scripts_admin_scripts(); |
| 159 |
|
| 160 |
global $pagenow; |
| 161 |
$settings = get_option('tracking_scripts_settings'); |
| 162 |
|
| 163 |
if(isset($_GET['tab'])) { |
| 164 |
self::tracking_scripts_admin_tabs($_GET['tab']); |
| 165 |
$pagenow = $_GET['tab']; |
| 166 |
} else { |
| 167 |
self::tracking_scripts_admin_tabs('add_new'); |
| 168 |
$pagenow = 'add_new'; |
| 169 |
} |
| 170 |
?> |
| 171 |
<div class="wrap tracking_scripts_wrap"> |
| 172 |
<form method="post" action="<?php echo get_admin_url(); ?>admin-post.php"> |
| 173 |
<?php settings_fields(WP_TRACKING_SCRIPTS_OPTION_GROUP); ?> |
| 174 |
<?php do_settings_sections(WP_TRACKING_SCRIPTS_OPTION_GROUP); ?> |
| 175 |
<?php if($pagenow == 'add_new') { ?> |
| 176 |
<?php include_once('templates/tracking-scripts-manager-pro-add-new.php'); ?> |
| 177 |
<?php } else if($pagenow == 'global') { ?> |
| 178 |
<?php include_once('templates/tracking-scripts-manager-pro-edit-global.php'); ?> |
| 179 |
<?php } else { ?> |
| 180 |
<?php include_once('templates/tracking-scripts-manager-pro-edit-single-post.php'); ?> |
| 181 |
<?php } ?> |
| 182 |
</form> |
| 183 |
</div> |
| 184 |
<?php |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
// Admin Scripts |
| 190 |
public static function tracking_scripts_admin_scripts() { |
| 191 |
wp_enqueue_script('jquery'); |
| 192 |
wp_enqueue_script('jquery-ui-sortable'); |
| 193 |
|
| 194 |
wp_register_style('tracking-scripts-main', plugins_url('/css/main.css', __FILE__)); |
| 195 |
wp_enqueue_style('tracking-scripts-main'); |
| 196 |
|
| 197 |
wp_enqueue_script( 'tracking_script_js', plugin_dir_url(__FILE__) . '/js/built.min.js', array(), '', true ); |
| 198 |
wp_localize_script( 'tracking_script_js', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
// Ajax Functions |
| 203 |
public static function tracking_scripts_posts_ajax_handler() { |
| 204 |
$post_type = ($_POST['postType']) ? esc_attr($_POST['postType']) : 'post'; |
| 205 |
|
| 206 |
$args = array( |
| 207 |
'post_type' => $post_type, |
| 208 |
'posts_per_page' => -1, |
| 209 |
'orderby' => 'name', |
| 210 |
'order' => 'ASC' |
| 211 |
); |
| 212 |
|
| 213 |
ob_start(); |
| 214 |
|
| 215 |
$query = new WP_Query($args); |
| 216 |
echo '<option value="none" id="none">Choose '.ucwords($post_type).'</option>'; |
| 217 |
while($query->have_posts()) : $query->the_post(); |
| 218 |
echo '<option value="'.get_the_ID().'" id="'.get_the_ID().'">'.ucwords(get_the_title()).'</option>'; |
| 219 |
endwhile; |
| 220 |
wp_reset_postdata(); |
| 221 |
|
| 222 |
echo ob_get_clean(); |
| 223 |
die(); |
| 224 |
} |
| 225 |
|
| 226 |
public static function tracking_scripts_post_content_ajax_handler() { |
| 227 |
$current_page_id = ($_POST['postID']) ? esc_attr($_POST['postID']) : null; |
| 228 |
|
| 229 |
if($current_page_id) { |
| 230 |
$page_scripts = unserialize(get_option(WP_PAGE_TRACKING_SCRIPT)); $i = 1; |
| 231 |
$page_scripts_array = array(); |
| 232 |
foreach($page_scripts as $script) { |
| 233 |
if($script->page_id == $current_page_id) { |
| 234 |
$page_scripts_array[$script->location][] = $script; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
ob_start(); |
| 239 |
?> |
| 240 |
<div id="tracking_scripts_<?php echo $current_page_id; ?>" class="tracking_scripts"> |
| 241 |
<?php |
| 242 |
$page_header_scripts = (array_key_exists('header', $page_scripts_array)) ? $page_scripts_array['header'] : null; |
| 243 |
$page_footer_scripts = (array_key_exists('footer', $page_scripts_array)) ? $page_scripts_array['footer'] : null; |
| 244 |
?> |
| 245 |
<h3>Header</h3> |
| 246 |
<?php if($page_header_scripts) { ?> |
| 247 |
<ul class="tracking_script_list"> |
| 248 |
<?php $i = 1; ?> |
| 249 |
<?php foreach($page_header_scripts as $script) { ?> |
| 250 |
<div class="tracking_script"> |
| 251 |
<i class="fa fa-sort" title="Drag to Sort"></i> |
| 252 |
<p><?php echo $i; ?></p> |
| 253 |
<div class="script_info"> |
| 254 |
<input type="text" name="page_script_<?php echo $script->script_id; ?>_name" value="<?php echo $script->script_name; ?>" readonly="readonly"> |
| 255 |
<input type="text" name="page_script_<?php echo $script->script_id; ?>_code" value="<?php echo $script->script_code; ?>" readonly="readonly"> |
| 256 |
</div> |
| 257 |
<i class="active_tracking fa <?php if($script->active === true) { echo 'fa-check-circle'; } else { echo 'fa-circle-o'; } ?>" title="<?php if($script->active === true) { echo 'Deactivate Script'; } else { echo 'Activate Script'; } ?>"></i> |
| 258 |
<i class="edit_tracking fa fa-edit" title="Edit Script"></i> |
| 259 |
<i class="delete_tracking fa fa-times" title="Delete Script"></i> |
| 260 |
<input type="hidden" class="script_order" name="page_script_<?php echo $script->script_id; ?>_order" value="<?php echo $i; ?>"> |
| 261 |
<input type="hidden" class="script_active" name="page_script_<?php echo $script->script_id; ?>_active" value="<?php if($script->active === true) { echo 'true'; } else { echo 'false'; } ?>"> |
| 262 |
<input type="hidden" class="script_exists" name="page_script_<?php echo $script->script_id; ?>_exists" value="true"> |
| 263 |
</div> |
| 264 |
<?php $i++; ?> |
| 265 |
<?php } ?> |
| 266 |
</ul> |
| 267 |
<?php } ?> |
| 268 |
<h3>Footer</h3> |
| 269 |
<?php if($page_footer_scripts) { ?> |
| 270 |
<ul class="tracking_script_list"> |
| 271 |
<?php foreach($page_footer_scripts as $script) { ?> |
| 272 |
<div class="tracking_script"> |
| 273 |
<i class="fa fa-sort" title="Drag to Sort"></i> |
| 274 |
<p><?php echo $i; ?></p> |
| 275 |
<div class="script_info"> |
| 276 |
<input type="text" name="page_script_<?php echo $script->script_id; ?>_name" value="<?php echo $script->script_name; ?>" readonly="readonly"> |
| 277 |
<input type="text" name="page_script_<?php echo $script->script_id; ?>_code" value="<?php echo $script->script_code; ?>" readonly="readonly"> |
| 278 |
</div> |
| 279 |
<i class="active_tracking fa <?php if($script->active === true) { echo 'fa-check-circle'; } else { echo 'fa-circle-o'; } ?>" title="<?php if($script->active === true) { echo 'Deactivate Script'; } else { echo 'Activate Script'; } ?>"></i> |
| 280 |
<i class="edit_tracking fa fa-edit" title="Edit Script"></i> |
| 281 |
<i class="delete_tracking fa fa-times" title="Delete Script"></i> |
| 282 |
<input type="hidden" class="script_order" name="page_script_<?php echo $script->script_id; ?>_order" value="<?php echo $i; ?>"> |
| 283 |
<input type="hidden" class="script_active" name="page_script_<?php echo $script->script_id; ?>_active" value="<?php if($script->active === true) { echo 'true'; } else { echo 'false'; } ?>"> |
| 284 |
<input type="hidden" class="script_exists" name="page_script_<?php echo $script->script_id; ?>_exists" value="true"> |
| 285 |
</div> |
| 286 |
<?php $i++; ?> |
| 287 |
<?php } ?> |
| 288 |
</ul> |
| 289 |
<?php } ?> |
| 290 |
</div> |
| 291 |
<?php |
| 292 |
echo ob_get_clean(); |
| 293 |
die(); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
// Admin Hooks |
| 298 |
public static function initialize_admin_posts() { |
| 299 |
add_action('admin_post_save_new_tracking_codes', array(__CLASS__, 'save_new_tracking_codes')); // If the user is logged in |
| 300 |
add_action('admin_post_update_tracking_codes', array(__CLASS__, 'update_tracking_codes')); // If the user is logged in |
| 301 |
add_action('admin_post_update_page_tracking_codes', array(__CLASS__, 'update_page_tracking_codes')); // If the user is logged in |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
// Save New Tracking Codes |
| 307 |
public static function save_new_tracking_codes() { |
| 308 |
$header_scripts = unserialize(get_option(WP_HEADER_TRACKING_SCRIPT)); |
| 309 |
$footer_scripts = unserialize(get_option(WP_FOOTER_TRACKING_SCRIPT)); |
| 310 |
$page_scripts = unserialize(get_option(WP_PAGE_TRACKING_SCRIPT)); |
| 311 |
|
| 312 |
if(!$header_scripts) { |
| 313 |
$header_scripts = array(); |
| 314 |
} |
| 315 |
|
| 316 |
if(!$footer_scripts) { |
| 317 |
$footer_scripts = array(); |
| 318 |
} |
| 319 |
|
| 320 |
if($_POST[WP_NEW_PAGE_TRACKING_SCRIPT_GLOBAL] && $_POST[WP_NEW_PAGE_TRACKING_SCRIPT_GLOBAL] == 'yes') { |
| 321 |
$tracking_location = esc_attr($_POST[WP_NEW_PAGE_TRACKING_SCRIPT_LOCATION]); |
| 322 |
|
| 323 |
if($tracking_location == 'header') { |
| 324 |
$tracking = new Tracking_Script(); |
| 325 |
$tracking->script_name = sanitize_text_field($_POST['new_page_script_name']); |
| 326 |
$tracking->script_code = stripslashes(esc_textarea($_POST[WP_NEW_PAGE_TRACKING_SCRIPT])); |
| 327 |
$tracking->active = true; |
| 328 |
$tracking->order = count($header_scripts); |
| 329 |
$tracking->location = 'header'; |
| 330 |
$header_scripts[] = $tracking; |
| 331 |
update_option(WP_HEADER_TRACKING_SCRIPT, serialize($header_scripts)); |
| 332 |
} else if($tracking_location == 'footer') { |
| 333 |
$tracking = new Tracking_Script(); |
| 334 |
$tracking->script_name = sanitize_text_field($_POST['new_page_script_name']); |
| 335 |
$tracking->script_code = stripslashes(esc_textarea($_POST[WP_NEW_PAGE_TRACKING_SCRIPT])); |
| 336 |
$tracking->active = true; |
| 337 |
$tracking->order = count($footer_scripts); |
| 338 |
$tracking->location = 'footer'; |
| 339 |
$footer_scripts[] = $tracking; |
| 340 |
update_option(WP_FOOTER_TRACKING_SCRIPT, serialize($footer_scripts)); |
| 341 |
} |
| 342 |
} else { |
| 343 |
if($_POST[WP_NEW_PAGE_TRACKING_SCRIPT_ID] && $_POST[WP_NEW_PAGE_TRACKING_SCRIPT]) { |
| 344 |
$tracking_pagecount = unserialize(get_option(WP_PAGE_TRACKING_SCRIPT_COUNT)); |
| 345 |
if(!$tracking_pagecount) { |
| 346 |
$tracking_pagecount = 1; |
| 347 |
} else { |
| 348 |
$tracking_pagecount++; |
| 349 |
} |
| 350 |
update_option(WP_PAGE_TRACKING_SCRIPT_COUNT, serialize($tracking_pagecount)); |
| 351 |
|
| 352 |
$tracking = new Tracking_Script(); |
| 353 |
$tracking->script_name = sanitize_text_field($_POST['new_page_script_name']); |
| 354 |
$tracking->script_code = stripslashes(esc_textarea($_POST[WP_NEW_PAGE_TRACKING_SCRIPT])); |
| 355 |
$tracking->active = true; |
| 356 |
$tracking->page_id = esc_attr($_POST[WP_NEW_PAGE_TRACKING_SCRIPT_ID]); |
| 357 |
$tracking->location = esc_attr($_POST[WP_NEW_PAGE_TRACKING_SCRIPT_LOCATION]); |
| 358 |
$tracking->script_id = $tracking_pagecount; |
| 359 |
$page_scripts[] = $tracking; |
| 360 |
update_option(WP_PAGE_TRACKING_SCRIPT, serialize($page_scripts)); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
$redirect_url = get_admin_url().'admin.php?page='.TRACKING_SCRIPT_BASENAME; |
| 365 |
if($_POST[WP_NEW_PAGE_TRACKING_SCRIPT_GLOBAL] == 'yes') { |
| 366 |
$redirect_url = get_admin_url().'admin.php?page='.TRACKING_SCRIPT_BASENAME.'&tab=global'; |
| 367 |
} else if($_POST[WP_NEW_PAGE_TRACKING_SCRIPT_ID] && $_POST[WP_NEW_PAGE_TRACKING_SCRIPT]) { |
| 368 |
$redirect_url = get_admin_url().'admin.php?page='.TRACKING_SCRIPT_BASENAME.'&tab=pages'; |
| 369 |
} |
| 370 |
|
| 371 |
wp_redirect($redirect_url); |
| 372 |
exit(); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
// Update Global Codes |
| 377 |
public static function update_tracking_codes() { |
| 378 |
$header_scripts = unserialize(get_option(WP_HEADER_TRACKING_SCRIPT)); |
| 379 |
$footer_scripts = unserialize(get_option(WP_FOOTER_TRACKING_SCRIPT)); |
| 380 |
|
| 381 |
$i = 1; |
| 382 |
foreach($header_scripts as $script) { |
| 383 |
if(isset($_POST['header_script_'.$i.'_name'])) { |
| 384 |
$script->script_name = sanitize_text_field($_POST['header_script_'.$i.'_name']); |
| 385 |
} |
| 386 |
if(isset($_POST['header_script_'.$i.'_code'])) { |
| 387 |
$script->script_code = stripslashes(esc_textarea($_POST['header_script_'.$i.'_code'])); |
| 388 |
} |
| 389 |
if(isset($_POST['header_script_'.$i.'_active'])) { |
| 390 |
if($_POST['header_script_'.$i.'_active'] === 'false') { |
| 391 |
$script->active = false; |
| 392 |
} else { |
| 393 |
$script->active = true; |
| 394 |
} |
| 395 |
} |
| 396 |
if(isset($_POST['header_script_'.$i.'_order'])) { |
| 397 |
$order = filter_input(INPUT_POST, 'header_script_'.$i.'_order', FILTER_VALIDATE_INT); |
| 398 |
if(is_int($order)) { |
| 399 |
$script->order = $order; |
| 400 |
} |
| 401 |
} |
| 402 |
if(isset($_POST['header_script_'.$i.'_exists'])) { |
| 403 |
if($_POST['header_script_'.$i.'_exists'] === 'false') { |
| 404 |
unset($header_scripts[$i-1]); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
$i++; |
| 409 |
} |
| 410 |
|
| 411 |
$i = 1; |
| 412 |
foreach($footer_scripts as $script) { |
| 413 |
if(isset($_POST['footer_script_'.$i.'_name'])) { |
| 414 |
$script->script_name = sanitize_text_field($_POST['footer_script_'.$i.'_name']); |
| 415 |
} |
| 416 |
if(isset($_POST['footer_script_'.$i.'_code'])) { |
| 417 |
$script->script_code = stripslashes(esc_textarea($_POST['footer_script_'.$i.'_code'])); |
| 418 |
} |
| 419 |
if(isset($_POST['footer_script_'.$i.'_active'])) { |
| 420 |
if($_POST['footer_script_'.$i.'_active'] === 'false') { |
| 421 |
$script->active = false; |
| 422 |
} else { |
| 423 |
$script->active = true; |
| 424 |
} |
| 425 |
} |
| 426 |
if(isset($_POST['footer_script_'.$i.'_order'])) { |
| 427 |
$order = filter_input(INPUT_POST, 'footer_script_'.$i.'_order', FILTER_VALIDATE_INT); |
| 428 |
if(is_int($order)) { |
| 429 |
$script->order = $order; |
| 430 |
} |
| 431 |
} |
| 432 |
if(isset($_POST['footer_script_'.$i.'_exists'])) { |
| 433 |
if($_POST['footer_script_'.$i.'_exists'] === 'false') { |
| 434 |
unset($footer_scripts[$i-1]); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
$i++; |
| 439 |
} |
| 440 |
|
| 441 |
usort($header_scripts, array(__CLASS__, 'compare_order')); |
| 442 |
usort($footer_scripts, array(__CLASS__, 'compare_order')); |
| 443 |
|
| 444 |
|
| 445 |
update_option(WP_HEADER_TRACKING_SCRIPT, serialize($header_scripts)); |
| 446 |
update_option(WP_FOOTER_TRACKING_SCRIPT, serialize($footer_scripts)); |
| 447 |
|
| 448 |
wp_redirect(get_admin_url().'admin.php?page='.TRACKING_SCRIPT_BASENAME.'&tab=global'); |
| 449 |
exit(); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
// Update Page Specific Codes |
| 454 |
public static function update_page_tracking_codes() { |
| 455 |
$page_scripts = unserialize(get_option(WP_PAGE_TRACKING_SCRIPT)); |
| 456 |
|
| 457 |
$index = 0; |
| 458 |
foreach($page_scripts as $script) { |
| 459 |
$script_id = $script->script_id; |
| 460 |
if(isset($_POST['page_script_'.$script_id.'_name'])) { |
| 461 |
$script->script_name = sanitize_text_field($_POST['page_script_'.$script_id.'_name']); |
| 462 |
} |
| 463 |
if(isset($_POST['page_script_'.$script_id.'_code'])) { |
| 464 |
$script->script_code = stripslashes(esc_textarea($_POST['page_script_'.$script_id.'_code'])); |
| 465 |
} |
| 466 |
if(isset($_POST['page_script_'.$script_id.'_active'])) { |
| 467 |
if($_POST['page_script_'.$script_id.'_active'] === 'false') { |
| 468 |
$script->active = false; |
| 469 |
} else { |
| 470 |
$script->active = true; |
| 471 |
} |
| 472 |
} |
| 473 |
if(isset($_POST['page_script_'.$script_id.'_order'])) { |
| 474 |
$order = filter_input(INPUT_POST, 'page_script_'.$script_id.'_order', FILTER_VALIDATE_INT); |
| 475 |
if(is_int($order)) { |
| 476 |
$script->order = $order; |
| 477 |
} |
| 478 |
} |
| 479 |
if(isset($_POST['page_script_'.$script_id.'_exists'])) { |
| 480 |
if($_POST['page_script_'.$script_id.'_exists'] === 'false') { |
| 481 |
unset($page_scripts[$index]); |
| 482 |
} |
| 483 |
} |
| 484 |
$index++; |
| 485 |
} |
| 486 |
|
| 487 |
usort($page_scripts, array(__CLASS__, 'compare_order')); |
| 488 |
|
| 489 |
update_option(WP_PAGE_TRACKING_SCRIPT, serialize($page_scripts)); |
| 490 |
|
| 491 |
wp_redirect(get_admin_url().'admin.php?page='.TRACKING_SCRIPT_BASENAME.'&tab=pages'); |
| 492 |
exit(); |
| 493 |
} |
| 494 |
|
| 495 |
public static function compare_order($a, $b) { |
| 496 |
return ($a->order < $b->order) ? -1 : 1; |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
$class['Tracking_Scripts'] = new Tracking_Scripts(); |
| 501 |
} |
| 502 |
|
| 503 |
if( !class_exists('Tracking_Script') ) { |
| 504 |
|
| 505 |
class Tracking_Script { |
| 506 |
public $script_name; |
| 507 |
public $script_code; |
| 508 |
public $active; |
| 509 |
public $order; |
| 510 |
public $page_id; |
| 511 |
public $location; |
| 512 |
public $script_id; |
| 513 |
|
| 514 |
function __construct() { |
| 515 |
|
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
} |
| 520 |
?> |