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

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