PluginProbe
Tracking Script Manager / 1.0.1
Tracking Script Manager v1.0.1
trunk 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 All 33 releases
tracking-script-manager / tracking-scripts.php

tracking-scripts.php in Tracking Script Manager 1.0.1, at tracking-scripts.php

361 lines 14.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Tracking Scripts
4 * Plugin URI: http://red8interactive.com
5 * Description: A plugin that allows you to add tracking scripts to your site.
6 * Version: 1.0
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( !class_exists('Tracking_Scripts') ) {
31
32 class Tracking_Scripts {
33
34 function __construct() {
35 self::define_constants();
36 self::load_hooks();
37 }
38
39 /**
40 * Defines plugin constants
41 */
42 public static function define_constants() {
43 define('TRACKING_SCRIPT_PATH', plugins_url( ' ', __FILE__ ) );
44
45 define('WP_TRACKING_SCRIPTS_OPTION_GROUP', 'tracking_scripts_options' );
46 define('WP_HEADER_TRACKING_SCRIPT', 'header_tracking_script_code' );
47 define('WP_FOOTER_TRACKING_SCRIPT', 'footer_tracking_script_code' );
48 define('WP_NEW_HEADER_TRACKING_SCRIPT', 'new_header_tracking_script_code' );
49 define('WP_NEW_FOOTER_TRACKING_SCRIPT', 'new_footer_tracking_script_code' );
50 }
51
52 public static function load_hooks() {
53 add_action('wp_head', array(__CLASS__, 'find_header_tracking_codes'));
54
55 add_action('wp_footer', array(__CLASS__, 'find_footer_tracking_codes'));
56
57 add_action('admin_menu', array(__CLASS__, 'tracking_scripts_create_menu'));
58
59 add_action('admin_enqueue_scripts', array(__CLASS__, 'tracking_scripts_admin_scripts'));
60
61 add_action('admin_init', array(__CLASS__, 'initialize_admin_posts'));
62 }
63
64 /*************************************************
65 * Front End
66 **************************************************/
67
68 /**
69 *
70 *
71 * @param int $post_id The ID of the post being saved.
72 */
73 public static function find_header_tracking_codes() {
74 $header_scripts = unserialize(get_option(WP_HEADER_TRACKING_SCRIPT));
75
76 if($header_scripts) {
77 foreach($header_scripts as $script) {
78 if($script->active) {
79 echo iconv('cp1252', 'UTF-8', html_entity_decode(esc_attr($script->script_code), ENT_QUOTES, 'cp1252'));
80 }
81 }
82 }
83 }
84
85 public static function find_footer_tracking_codes() {
86 $footer_scripts = unserialize(get_option(WP_FOOTER_TRACKING_SCRIPT));
87
88 if($footer_scripts) {
89 foreach($footer_scripts as $script) {
90 if($script->active) {
91 echo iconv('cp1252', 'UTF-8', html_entity_decode(esc_attr($script->script_code), ENT_QUOTES, 'cp1252'));
92 }
93 }
94 }
95 }
96
97 /*************************************************
98 * Admin Area
99 **************************************************/
100
101 public static function tracking_scripts_create_menu() {
102 add_menu_page('Tracking Scripts', 'Tracking Scripts', 'administrator', __FILE__, array(__CLASS__, 'tracking_options'), '');
103 add_action('admin_init', array(__CLASS__, 'register_tracking_scripts_settings'));
104 }
105
106 public static function register_tracking_scripts_settings() {
107 register_setting( WP_TRACKING_SCRIPTS_OPTION_GROUP, WP_HEADER_TRACKING_SCRIPT, 'esc_textarea' );
108 register_setting( WP_TRACKING_SCRIPTS_OPTION_GROUP, WP_FOOTER_TRACKING_SCRIPT, 'esc_textarea' );
109 }
110
111 public static function tracking_scripts_admin_tabs( $current = 'add_new' ) {
112 $tabs = array('add_new' => 'Add New', 'existing' => 'Existing');
113 echo '<div id="tracking_scripts_admin"><br></div>';
114 echo '<h2 style="font-size: 22px; font-weight: bold; margin: 10px 0 40px;">Tracking Scripts</h2>';
115 echo '<h2 class="nav-tab-wrapper">';
116 foreach($tabs as $tab => $name) {
117 $class = ($tab == $current) ? ' nav-tab-active' : '';
118 echo "<a class='nav-tab$class' href='?page=tracking-scripts/tracking-scripts.php&tab=$tab'>$name</a>";
119 }
120 echo '</h2>';
121 }
122
123 public static function tracking_options() {
124 global $pagenow;
125 $settings = get_option('tracking_scripts_settings');
126
127 if(isset($_GET['tab'])) {
128 self::tracking_scripts_admin_tabs($_GET['tab']);
129 $pagenow = $_GET['tab'];
130 } else {
131 self::tracking_scripts_admin_tabs('add_new');
132 $pagenow = 'add_new';
133 }
134 ?>
135 <div class="wrap tracking_scripts_wrap">
136 <form method="post" action="<?php echo get_admin_url(); ?>admin-post.php">
137 <?php settings_fields(WP_TRACKING_SCRIPTS_OPTION_GROUP); ?>
138 <?php do_settings_sections(WP_TRACKING_SCRIPTS_OPTION_GROUP); ?>
139 <?php if($pagenow == 'add_new') { ?>
140 <div class="script_section">
141 <h2>Header Scripts</h2>
142 <div class="add_tracking_scripts">
143 <label>Name:</label>
144 <input type="text" name="new_header_script_name"/>
145 <textarea rows="5" cols="40" name="<?php echo WP_NEW_HEADER_TRACKING_SCRIPT; ?>" style="font-weight: normal;"></textarea>
146 </div>
147 </div>
148 <div class="script_section">
149 <h2>Footer Scripts</h2>
150 <div class="add_tracking_scripts">
151 <label>Name:</label>
152 <input type="text" name="new_footer_script_name"/>
153 <textarea rows="5" cols="40" name="<?php echo WP_NEW_FOOTER_TRACKING_SCRIPT; ?>" style="font-weight: normal;"></textarea>
154 </div>
155 </div>
156 <?php submit_button('Add Scripts', 'primary', 'save_new_tracking_codes'); ?>
157 <input type="hidden" name="action" value="save_new_tracking_codes">
158 <?php } else { ?>
159 <div class="script_section">
160 <h2>Header Scripts</h2>
161 <?php $header_scripts = unserialize(get_option(WP_HEADER_TRACKING_SCRIPT)); $i = 1; ?>
162 <div class="tracking_scripts">
163 <?php foreach($header_scripts as $script) { ?>
164 <div class="tracking_script">
165 <i class="fa fa-sort" title="Drag to Sort"></i>
166 <p><?php echo $i; ?></p>
167 <div class="script_info">
168 <input type="text" name="header_script_<?php echo $i; ?>_name" value="<?php echo $script->script_name; ?>" readonly="readonly">
169 <input type="text" name="header_script_<?php echo $i; ?>_code" value="<?php echo $script->script_code; ?>" readonly="readonly">
170 </div>
171 <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>
172 <i class="edit_tracking fa fa-edit" title="Edit Script"></i>
173 <i class="delete_tracking fa fa-times" title="Delete Script"></i>
174 <input type="hidden" class="script_order" name="header_script_<?php echo $i; ?>_order" value="<?php echo $i; ?>">
175 <input type="hidden" class="script_active" name="header_script_<?php echo $i; ?>_active" value="<?php if($script->active === true) { echo 'true'; } else { echo 'false'; } ?>">
176 <input type="hidden" class="script_exists" name="header_script_<?php echo $i; ?>_exists" value="true">
177 </div>
178 <?php $i++; } ?>
179 </div>
180 </div>
181 <div class="script_section">
182 <h2>Footer Scripts</h2>
183 <?php $footer_scripts = unserialize(get_option(WP_FOOTER_TRACKING_SCRIPT)); $i = 1; ?>
184 <div class="tracking_scripts">
185 <?php foreach($footer_scripts as $script) { ?>
186 <div class="tracking_script">
187 <i class="fa fa-sort" title="Drag to Sort"></i>
188 <p><?php echo $i; ?></p>
189 <div class="script_info">
190 <input type="text" name="footer_script_<?php echo $i; ?>_name" value="<?php echo $script->script_name; ?>" readonly="readonly">
191 <input type="text" name="footer_script_<?php echo $i; ?>_code" value="<?php echo $script->script_code; ?>" readonly="readonly">
192 </div>
193 <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>
194 <i class="edit_tracking fa fa-edit" title="Edit Script"></i>
195 <i class="delete_tracking fa fa-times" title="Delete Script"></i>
196 <input type="hidden" class="script_order" name="footer_script_<?php echo $i; ?>_order" value="<?php echo $i; ?>">
197 <input type="hidden" class="script_active" name="footer_script_<?php echo $i; ?>_active" value="<?php if($script->active === true) { echo 'true'; } else { echo 'false'; } ?>">
198 <input type="hidden" class="script_exists" name="footer_script_<?php echo $i; ?>_exists" value="true">
199 </div>
200 <?php $i++; } ?>
201 </div>
202 </div>
203 <?php submit_button('Update Scripts', 'primary', 'update_tracking_codes'); ?>
204 <input type="hidden" name="action" value="update_tracking_codes">
205 <?php } ?>
206 </form>
207 </div>
208 <?php
209
210 }
211
212 public static function tracking_scripts_admin_scripts() {
213 wp_enqueue_script('jquery');
214 wp_enqueue_script('jquery-ui-sortable');
215
216 wp_register_style('tracking-scripts-main', plugins_url('/css/main.css', __FILE__));
217 wp_enqueue_style('tracking-scripts-main');
218
219 wp_enqueue_script( 'tracking_script_js', plugin_dir_url(__FILE__) . '/js/main.js', array(), '', true );
220 }
221
222
223 public static function initialize_admin_posts() {
224 add_action('admin_post_save_new_tracking_codes', array(__CLASS__, 'save_new_tracking_codes')); // If the user is logged in
225 add_action('admin_post_nopriv_save_new_tracking_codes', array(__CLASS__, 'save_new_tracking_codes')); // If the user in not logged in
226
227 add_action('admin_post_update_tracking_codes', array(__CLASS__, 'update_tracking_codes')); // If the user is logged in
228 add_action('admin_post_nopriv_update_tracking_codes', array(__CLASS__, 'update_tracking_codes')); // If the user in not logged in
229 }
230
231
232 public static function save_new_tracking_codes() {
233 $header_scripts = unserialize(get_option(WP_HEADER_TRACKING_SCRIPT));
234 $footer_scripts = unserialize(get_option(WP_FOOTER_TRACKING_SCRIPT));
235
236 if(!$header_scripts) {
237 $header_scripts = array();
238 }
239
240 if(!$footer_scripts) {
241 $footer_scripts = array();
242 }
243
244 if($_POST['new_header_script_name']) {
245 $tracking = new Tracking_Script();
246 $tracking->script_name = sanitize_text_field($_POST['new_header_script_name']);
247 $tracking->script_code = stripslashes(esc_textarea($_POST[WP_NEW_HEADER_TRACKING_SCRIPT]));
248 $tracking->active = true;
249 $tracking->order = count($header_scripts);
250 $header_scripts[] = $tracking;
251 update_option(WP_HEADER_TRACKING_SCRIPT, serialize($header_scripts));
252 }
253
254 if($_POST['new_footer_script_name']) {
255 $tracking = new Tracking_Script();
256 $tracking->script_name = sanitize_text_field($_POST['new_footer_script_name']);
257 $tracking->script_code = stripslashes(esc_textarea($_POST[WP_NEW_FOOTER_TRACKING_SCRIPT]));
258 $tracking->active = true;
259 $tracking->order = count($footer_scripts);
260 $footer_scripts[] = $tracking;
261 update_option(WP_FOOTER_TRACKING_SCRIPT, serialize($footer_scripts));
262 }
263
264 wp_redirect(get_admin_url().'admin.php?page=tracking-scripts/tracking-scripts.php&tab=existing');
265 exit();
266 }
267
268 function update_tracking_codes() {
269 $header_scripts = unserialize(get_option(WP_HEADER_TRACKING_SCRIPT));
270 $footer_scripts = unserialize(get_option(WP_FOOTER_TRACKING_SCRIPT));
271
272 $i = 1;
273 foreach($header_scripts as $script) {
274 if($_POST['header_script_'.$i.'_name']) {
275 $script->script_name = sanitize_text_field($_POST['header_script_'.$i.'_name']);
276 }
277 if($_POST['header_script_'.$i.'_code']) {
278 $script->script_code = stripslashes(esc_textarea($_POST['header_script_'.$i.'_code']));
279 }
280 if($_POST['header_script_'.$i.'_active']) {
281 if($_POST['header_script_'.$i.'_active'] === 'false') {
282 $script->active = false;
283 } else {
284 $script->active = true;
285 }
286 }
287 if($_POST['header_script_'.$i.'_order']) {
288 $order = filter_input(INPUT_POST, 'header_script_'.$i.'_order', FILTER_VALIDATE_INT);
289 if(is_int($order)) {
290 $script->order = $order;
291 }
292 }
293 if($_POST['header_script_'.$i.'_exists']) {
294 if($_POST['header_script_'.$i.'_exists'] === 'false') {
295 unset($header_scripts[$i-1]);
296 }
297 }
298
299 $i++;
300 }
301
302 $i = 1;
303 foreach($footer_scripts as $script) {
304 if($_POST['footer_script_'.$i.'_name']) {
305 $script->script_name = sanitize_text_field($_POST['footer_script_'.$i.'_name']);
306 }
307 if($_POST['footer_script_'.$i.'_code']) {
308 $script->script_code = stripslashes(esc_textarea($_POST['footer_script_'.$i.'_code']));
309 }
310 if($_POST['footer_script_'.$i.'_active']) {
311 if($_POST['footer_script_'.$i.'_active'] === 'false') {
312 $script->active = false;
313 } else {
314 $script->active = true;
315 }
316 }
317 if($_POST['footer_script_'.$i.'_order']) {
318 $order = filter_input(INPUT_POST, 'footer_script_'.$i.'_order', FILTER_VALIDATE_INT);
319 if(is_int($order)) {
320 $script->order = $order;
321 }
322 }
323 if($_POST['footer_script_'.$i.'_exists']) {
324 if($_POST['footer_script_'.$i.'_exists'] === 'false') {
325 unset($footer_scripts[$i-1]);
326 }
327 }
328
329 $i++;
330 }
331
332 usort($header_scripts, array(__CLASS__, 'compare_order'));
333 usort($footer_scripts, array(__CLASS__, 'compare_order'));
334
335
336 update_option(WP_HEADER_TRACKING_SCRIPT, serialize($header_scripts));
337 update_option(WP_FOOTER_TRACKING_SCRIPT, serialize($footer_scripts));
338
339 wp_redirect(get_admin_url().'admin.php?page=tracking-scripts/tracking-scripts.php&tab=existing');
340 exit();
341 }
342
343 public static function compare_order($a, $b) {
344 return ($a->order < $b->order) ? -1 : 1;
345 }
346 }
347
348 $class['Tracking_Scripts'] = new Tracking_Scripts();
349 }
350
351 class Tracking_Script {
352 public $script_name;
353 public $script_code;
354 public $active;
355 public $order;
356
357 function __construct() {
358
359 }
360 }
361 ?>