PluginProbe
Tracking Script Manager / 1.0.4
Tracking Script Manager v1.0.4
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.4, at tracking-scripts.php

363 lines 14.8 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.4
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-script-manager/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
224
225 public static function initialize_admin_posts() {
226 add_action('admin_post_save_new_tracking_codes', array(__CLASS__, 'save_new_tracking_codes')); // If the user is logged in
227 add_action('admin_post_nopriv_save_new_tracking_codes', array(__CLASS__, 'save_new_tracking_codes')); // If the user in not logged in
228
229 add_action('admin_post_update_tracking_codes', array(__CLASS__, 'update_tracking_codes')); // If the user is logged in
230 add_action('admin_post_nopriv_update_tracking_codes', array(__CLASS__, 'update_tracking_codes')); // If the user in not logged in
231 }
232
233
234 public static function save_new_tracking_codes() {
235 $header_scripts = unserialize(get_option(WP_HEADER_TRACKING_SCRIPT));
236 $footer_scripts = unserialize(get_option(WP_FOOTER_TRACKING_SCRIPT));
237
238 if(!$header_scripts) {
239 $header_scripts = array();
240 }
241
242 if(!$footer_scripts) {
243 $footer_scripts = array();
244 }
245
246 if($_POST['new_header_script_name']) {
247 $tracking = new Tracking_Script();
248 $tracking->script_name = sanitize_text_field($_POST['new_header_script_name']);
249 $tracking->script_code = stripslashes(esc_textarea($_POST[WP_NEW_HEADER_TRACKING_SCRIPT]));
250 $tracking->active = true;
251 $tracking->order = count($header_scripts);
252 $header_scripts[] = $tracking;
253 update_option(WP_HEADER_TRACKING_SCRIPT, serialize($header_scripts));
254 }
255
256 if($_POST['new_footer_script_name']) {
257 $tracking = new Tracking_Script();
258 $tracking->script_name = sanitize_text_field($_POST['new_footer_script_name']);
259 $tracking->script_code = stripslashes(esc_textarea($_POST[WP_NEW_FOOTER_TRACKING_SCRIPT]));
260 $tracking->active = true;
261 $tracking->order = count($footer_scripts);
262 $footer_scripts[] = $tracking;
263 update_option(WP_FOOTER_TRACKING_SCRIPT, serialize($footer_scripts));
264 }
265
266 wp_redirect(get_admin_url().'admin.php?page=tracking-script-manager/tracking-scripts.php&tab=existing');
267 exit();
268 }
269
270 function update_tracking_codes() {
271 $header_scripts = unserialize(get_option(WP_HEADER_TRACKING_SCRIPT));
272 $footer_scripts = unserialize(get_option(WP_FOOTER_TRACKING_SCRIPT));
273
274 $i = 1;
275 foreach($header_scripts as $script) {
276 if($_POST['header_script_'.$i.'_name']) {
277 $script->script_name = sanitize_text_field($_POST['header_script_'.$i.'_name']);
278 }
279 if($_POST['header_script_'.$i.'_code']) {
280 $script->script_code = stripslashes(esc_textarea($_POST['header_script_'.$i.'_code']));
281 }
282 if($_POST['header_script_'.$i.'_active']) {
283 if($_POST['header_script_'.$i.'_active'] === 'false') {
284 $script->active = false;
285 } else {
286 $script->active = true;
287 }
288 }
289 if($_POST['header_script_'.$i.'_order']) {
290 $order = filter_input(INPUT_POST, 'header_script_'.$i.'_order', FILTER_VALIDATE_INT);
291 if(is_int($order)) {
292 $script->order = $order;
293 }
294 }
295 if($_POST['header_script_'.$i.'_exists']) {
296 if($_POST['header_script_'.$i.'_exists'] === 'false') {
297 unset($header_scripts[$i-1]);
298 }
299 }
300
301 $i++;
302 }
303
304 $i = 1;
305 foreach($footer_scripts as $script) {
306 if($_POST['footer_script_'.$i.'_name']) {
307 $script->script_name = sanitize_text_field($_POST['footer_script_'.$i.'_name']);
308 }
309 if($_POST['footer_script_'.$i.'_code']) {
310 $script->script_code = stripslashes(esc_textarea($_POST['footer_script_'.$i.'_code']));
311 }
312 if($_POST['footer_script_'.$i.'_active']) {
313 if($_POST['footer_script_'.$i.'_active'] === 'false') {
314 $script->active = false;
315 } else {
316 $script->active = true;
317 }
318 }
319 if($_POST['footer_script_'.$i.'_order']) {
320 $order = filter_input(INPUT_POST, 'footer_script_'.$i.'_order', FILTER_VALIDATE_INT);
321 if(is_int($order)) {
322 $script->order = $order;
323 }
324 }
325 if($_POST['footer_script_'.$i.'_exists']) {
326 if($_POST['footer_script_'.$i.'_exists'] === 'false') {
327 unset($footer_scripts[$i-1]);
328 }
329 }
330
331 $i++;
332 }
333
334 usort($header_scripts, array(__CLASS__, 'compare_order'));
335 usort($footer_scripts, array(__CLASS__, 'compare_order'));
336
337
338 update_option(WP_HEADER_TRACKING_SCRIPT, serialize($header_scripts));
339 update_option(WP_FOOTER_TRACKING_SCRIPT, serialize($footer_scripts));
340
341 wp_redirect(get_admin_url().'admin.php?page=tracking-script-manager/tracking-scripts.php&tab=existing');
342 exit();
343 }
344
345 public static function compare_order($a, $b) {
346 return ($a->order < $b->order) ? -1 : 1;
347 }
348 }
349
350 $class['Tracking_Scripts'] = new Tracking_Scripts();
351 }
352
353 class Tracking_Script {
354 public $script_name;
355 public $script_code;
356 public $active;
357 public $order;
358
359 function __construct() {
360
361 }
362 }
363 ?>