PluginProbe
Tracking Script Manager / 1.0.8
Tracking Script Manager v1.0.8
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.8, at tracking-scripts.php

369 lines 14.9 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 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.0.9
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 define('TRACKING_SCRIPT_BASENAME', plugin_basename( __FILE__ ));
45
46 define('WP_TRACKING_SCRIPTS_OPTION_GROUP', 'tracking_scripts_options' );
47 define('WP_HEADER_TRACKING_SCRIPT', 'header_tracking_script_code' );
48 define('WP_FOOTER_TRACKING_SCRIPT', 'footer_tracking_script_code' );
49 define('WP_NEW_HEADER_TRACKING_SCRIPT', 'new_header_tracking_script_code' );
50 define('WP_NEW_FOOTER_TRACKING_SCRIPT', 'new_footer_tracking_script_code' );
51 }
52
53 public static function load_hooks() {
54 add_action('wp_head', array(__CLASS__, 'find_header_tracking_codes'));
55
56 add_action('wp_footer', array(__CLASS__, 'find_footer_tracking_codes'));
57
58 add_action('admin_menu', array(__CLASS__, 'tracking_scripts_create_menu'));
59
60 add_action('admin_init', array(__CLASS__, 'initialize_admin_posts'));
61 }
62
63 /*************************************************
64 * Front End
65 **************************************************/
66
67 /**
68 *
69 *
70 * @param int $post_id The ID of the post being saved.
71 */
72 public static function find_header_tracking_codes() {
73 $header_scripts = unserialize(get_option(WP_HEADER_TRACKING_SCRIPT));
74
75 if($header_scripts) {
76 foreach($header_scripts as $script) {
77 if($script->active) {
78 echo html_entity_decode(esc_attr($script->script_code), ENT_QUOTES, 'cp1252');
79 }
80 }
81 }
82 }
83
84 public static function find_footer_tracking_codes() {
85 $footer_scripts = unserialize(get_option(WP_FOOTER_TRACKING_SCRIPT));
86
87 if($footer_scripts) {
88 foreach($footer_scripts as $script) {
89 if($script->active) {
90 echo html_entity_decode(esc_attr($script->script_code), ENT_QUOTES, 'cp1252');
91 }
92 }
93 }
94 }
95
96 /*************************************************
97 * Admin Area
98 **************************************************/
99
100 public static function tracking_scripts_create_menu() {
101 add_menu_page('Tracking Scripts', 'Tracking Scripts', 'administrator', __FILE__, array(__CLASS__, 'tracking_options'), '');
102 add_action('admin_init', array(__CLASS__, 'register_tracking_scripts_settings'));
103 }
104
105 public static function register_tracking_scripts_settings() {
106 register_setting( WP_TRACKING_SCRIPTS_OPTION_GROUP, WP_HEADER_TRACKING_SCRIPT, 'esc_textarea' );
107 register_setting( WP_TRACKING_SCRIPTS_OPTION_GROUP, WP_FOOTER_TRACKING_SCRIPT, 'esc_textarea' );
108 }
109
110 public static function tracking_scripts_admin_tabs( $current = 'add_new' ) {
111 $tabs = array('add_new' => 'Add New', 'existing' => 'Existing');
112 echo '<h2 style="font-size: 22px; font-weight: bold; margin: 10px 0 40px;">Tracking Scripts</h2>';
113 echo '<h2 class="nav-tab-wrapper">';
114 foreach($tabs as $tab => $name) {
115 $class = ($tab == $current) ? ' nav-tab-active' : '';
116 echo "<a class='nav-tab$class' href='?page=".TRACKING_SCRIPT_BASENAME."&tab=$tab'>$name</a>";
117 }
118 echo '</h2>';
119 }
120
121 public static function tracking_options() {
122 self::tracking_scripts_admin_scripts();
123
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 if($header_scripts) { ?>
164 <?php foreach($header_scripts as $script) { ?>
165 <div class="tracking_script">
166 <i class="fa fa-sort" title="Drag to Sort"></i>
167 <p><?php echo $i; ?></p>
168 <div class="script_info">
169 <input type="text" name="header_script_<?php echo $i; ?>_name" value="<?php echo $script->script_name; ?>" readonly="readonly">
170 <input type="text" name="header_script_<?php echo $i; ?>_code" value="<?php echo $script->script_code; ?>" readonly="readonly">
171 </div>
172 <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>
173 <i class="edit_tracking fa fa-edit" title="Edit Script"></i>
174 <i class="delete_tracking fa fa-times" title="Delete Script"></i>
175 <input type="hidden" class="script_order" name="header_script_<?php echo $i; ?>_order" value="<?php echo $i; ?>">
176 <input type="hidden" class="script_active" name="header_script_<?php echo $i; ?>_active" value="<?php if($script->active === true) { echo 'true'; } else { echo 'false'; } ?>">
177 <input type="hidden" class="script_exists" name="header_script_<?php echo $i; ?>_exists" value="true">
178 </div>
179 <?php $i++; } ?>
180 <?php } ?>
181 </div>
182 </div>
183 <div class="script_section">
184 <h2>Footer Scripts</h2>
185 <?php $footer_scripts = unserialize(get_option(WP_FOOTER_TRACKING_SCRIPT)); $i = 1; ?>
186 <div class="tracking_scripts">
187 <?php if($footer_scripts) { ?>
188 <?php foreach($footer_scripts as $script) { ?>
189 <div class="tracking_script">
190 <i class="fa fa-sort" title="Drag to Sort"></i>
191 <p><?php echo $i; ?></p>
192 <div class="script_info">
193 <input type="text" name="footer_script_<?php echo $i; ?>_name" value="<?php echo $script->script_name; ?>" readonly="readonly">
194 <input type="text" name="footer_script_<?php echo $i; ?>_code" value="<?php echo $script->script_code; ?>" readonly="readonly">
195 </div>
196 <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>
197 <i class="edit_tracking fa fa-edit" title="Edit Script"></i>
198 <i class="delete_tracking fa fa-times" title="Delete Script"></i>
199 <input type="hidden" class="script_order" name="footer_script_<?php echo $i; ?>_order" value="<?php echo $i; ?>">
200 <input type="hidden" class="script_active" name="footer_script_<?php echo $i; ?>_active" value="<?php if($script->active === true) { echo 'true'; } else { echo 'false'; } ?>">
201 <input type="hidden" class="script_exists" name="footer_script_<?php echo $i; ?>_exists" value="true">
202 </div>
203 <?php $i++; } ?>
204 <?php } ?>
205 </div>
206 </div>
207 <?php submit_button('Update Scripts', 'primary', 'update_tracking_codes'); ?>
208 <input type="hidden" name="action" value="update_tracking_codes">
209 <?php } ?>
210 </form>
211 </div>
212 <?php
213
214 }
215
216 public static function tracking_scripts_admin_scripts() {
217 wp_enqueue_script('jquery');
218 wp_enqueue_script('jquery-ui-sortable');
219
220 wp_register_style('tracking-scripts-main', plugins_url('/css/main.css', __FILE__));
221 wp_enqueue_style('tracking-scripts-main');
222
223 wp_enqueue_script( 'tracking_script_js', plugin_dir_url(__FILE__) . '/js/main.js', array(), '', true );
224 }
225
226
227 public static function initialize_admin_posts() {
228 add_action('admin_post_save_new_tracking_codes', array(__CLASS__, 'save_new_tracking_codes')); // If the user is logged in
229 add_action('admin_post_nopriv_save_new_tracking_codes', array(__CLASS__, 'save_new_tracking_codes')); // If the user in not logged in
230
231 add_action('admin_post_update_tracking_codes', array(__CLASS__, 'update_tracking_codes')); // If the user is logged in
232 add_action('admin_post_nopriv_update_tracking_codes', array(__CLASS__, 'update_tracking_codes')); // If the user in not logged in
233 }
234
235
236 public static function save_new_tracking_codes() {
237 $header_scripts = unserialize(get_option(WP_HEADER_TRACKING_SCRIPT));
238 $footer_scripts = unserialize(get_option(WP_FOOTER_TRACKING_SCRIPT));
239
240 if(!$header_scripts) {
241 $header_scripts = array();
242 }
243
244 if(!$footer_scripts) {
245 $footer_scripts = array();
246 }
247
248 if($_POST['new_header_script_name']) {
249 $tracking = new Tracking_Script();
250 $tracking->script_name = sanitize_text_field($_POST['new_header_script_name']);
251 $tracking->script_code = stripslashes(esc_textarea($_POST[WP_NEW_HEADER_TRACKING_SCRIPT]));
252 $tracking->active = true;
253 $tracking->order = count($header_scripts);
254 $header_scripts[] = $tracking;
255 update_option(WP_HEADER_TRACKING_SCRIPT, serialize($header_scripts));
256 }
257
258 if($_POST['new_footer_script_name']) {
259 $tracking = new Tracking_Script();
260 $tracking->script_name = sanitize_text_field($_POST['new_footer_script_name']);
261 $tracking->script_code = stripslashes(esc_textarea($_POST[WP_NEW_FOOTER_TRACKING_SCRIPT]));
262 $tracking->active = true;
263 $tracking->order = count($footer_scripts);
264 $footer_scripts[] = $tracking;
265 update_option(WP_FOOTER_TRACKING_SCRIPT, serialize($footer_scripts));
266 }
267
268 wp_redirect(get_admin_url().'admin.php?page='.TRACKING_SCRIPT_BASENAME.'&tab=existing');
269 exit();
270 }
271
272 function update_tracking_codes() {
273 $header_scripts = unserialize(get_option(WP_HEADER_TRACKING_SCRIPT));
274 $footer_scripts = unserialize(get_option(WP_FOOTER_TRACKING_SCRIPT));
275
276 $i = 1;
277 if($header_scripts) {
278 foreach($header_scripts as $script) {
279 if($_POST['header_script_'.$i.'_name']) {
280 $script->script_name = sanitize_text_field($_POST['header_script_'.$i.'_name']);
281 }
282 if($_POST['header_script_'.$i.'_code']) {
283 $script->script_code = stripslashes(esc_textarea($_POST['header_script_'.$i.'_code']));
284 }
285 if($_POST['header_script_'.$i.'_active']) {
286 if($_POST['header_script_'.$i.'_active'] === 'false') {
287 $script->active = false;
288 } else {
289 $script->active = true;
290 }
291 }
292 if($_POST['header_script_'.$i.'_order']) {
293 $order = filter_input(INPUT_POST, 'header_script_'.$i.'_order', FILTER_VALIDATE_INT);
294 if(is_int($order)) {
295 $script->order = $order;
296 }
297 }
298 if($_POST['header_script_'.$i.'_exists']) {
299 if($_POST['header_script_'.$i.'_exists'] === 'false') {
300 unset($header_scripts[$i-1]);
301 }
302 }
303
304 $i++;
305 }
306 }
307
308 $i = 1;
309 if($footer_scripts) {
310 foreach($footer_scripts as $script) {
311 if($_POST['footer_script_'.$i.'_name']) {
312 $script->script_name = sanitize_text_field($_POST['footer_script_'.$i.'_name']);
313 }
314 if($_POST['footer_script_'.$i.'_code']) {
315 $script->script_code = stripslashes(esc_textarea($_POST['footer_script_'.$i.'_code']));
316 }
317 if($_POST['footer_script_'.$i.'_active']) {
318 if($_POST['footer_script_'.$i.'_active'] === 'false') {
319 $script->active = false;
320 } else {
321 $script->active = true;
322 }
323 }
324 if($_POST['footer_script_'.$i.'_order']) {
325 $order = filter_input(INPUT_POST, 'footer_script_'.$i.'_order', FILTER_VALIDATE_INT);
326 if(is_int($order)) {
327 $script->order = $order;
328 }
329 }
330 if($_POST['footer_script_'.$i.'_exists']) {
331 if($_POST['footer_script_'.$i.'_exists'] === 'false') {
332 unset($footer_scripts[$i-1]);
333 }
334 }
335
336 $i++;
337 }
338 }
339
340 usort($header_scripts, array(__CLASS__, 'compare_order'));
341 usort($footer_scripts, array(__CLASS__, 'compare_order'));
342
343
344 update_option(WP_HEADER_TRACKING_SCRIPT, serialize($header_scripts));
345 update_option(WP_FOOTER_TRACKING_SCRIPT, serialize($footer_scripts));
346
347 wp_redirect(get_admin_url().'admin.php?page='.TRACKING_SCRIPT_BASENAME.'&tab=existing');
348 exit();
349 }
350
351 public static function compare_order($a, $b) {
352 return ($a->order < $b->order) ? -1 : 1;
353 }
354 }
355
356 $class['Tracking_Scripts'] = new Tracking_Scripts();
357 }
358
359 class Tracking_Script {
360 public $script_name;
361 public $script_code;
362 public $active;
363 public $order;
364
365 function __construct() {
366
367 }
368 }
369 ?>