| @@ -1,520 +1,864 @@ | ||
| 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.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 ( ! 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', 'manage_options', __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 | -?> | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Plugin Name: Tracking Script Manager | |
| 5 | + * Plugin URI: http://wordpress.org/plugins/tracking-script-manager/ | |
| 6 | + * Description: A plugin that allows you to add tracking scripts to your site. | |
| 7 | + * Version: 2.0.15 | |
| 8 | + * Author: Red8 Interactive | |
| 9 | + * Author URI: http://red8interactive.com | |
| 10 | + * License: GPLv2 or later | |
| 11 | + */ | |
| 12 | +/* | |
| 13 | + Copyright 2019 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 | +if (! defined('ABSPATH')) { | |
| 30 | + exit; // Exit if accessed directly | |
| 31 | +} | |
| 32 | +if (! class_exists('Tracking_Scripts')) { | |
| 33 | + | |
| 34 | + class Tracking_Scripts | |
| 35 | + { | |
| 36 | + /** | |
| 37 | + * @var TSM_Process_Tracking_Scripts | |
| 38 | + */ | |
| 39 | + protected $process_all; | |
| 40 | + | |
| 41 | + function __construct() {} | |
| 42 | + | |
| 43 | + public function initialize() | |
| 44 | + { | |
| 45 | + | |
| 46 | + // Constants | |
| 47 | + define('TRACKING_SCRIPT_VERSION', '2.0.15'); | |
| 48 | + define('TRACKING_SCRIPT_PATH', plugins_url(' ', __FILE__)); | |
| 49 | + define('TRACKING_SCRIPT_BASENAME', plugin_basename(__FILE__)); | |
| 50 | + define('TRACKING_SCRIPT_DIR_PATH', plugin_dir_path(__FILE__)); | |
| 51 | + define('TRACKING_SCRIPT_TEXTDOMAIN', 'tracking-scripts-manager'); | |
| 52 | + // Actions | |
| 53 | + add_action('init', array($this, 'register_scripts_post_type')); | |
| 54 | + add_action('save_post', array($this, 'save_post')); | |
| 55 | + add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); | |
| 56 | + add_action('wp_head', array($this, 'find_header_tracking_codes'), 10); | |
| 57 | + add_action('wp_footer', array($this, 'find_footer_tracking_codes'), 10); | |
| 58 | + add_action('admin_menu', array($this, 'tracking_scripts_create_menu')); | |
| 59 | + add_action('add_meta_boxes', array($this, 'add_script_metaboxes')); | |
| 60 | + add_action('wp_ajax_tracking_scripts_get_posts', array($this, 'tracking_scripts_posts_ajax_handler')); | |
| 61 | + add_action( | |
| 62 | + 'manage_r8_tracking_scripts_posts_custom_column', | |
| 63 | + array( | |
| 64 | + $this, | |
| 65 | + 'tracking_script_column_content', | |
| 66 | + ), | |
| 67 | + 10, | |
| 68 | + 2 | |
| 69 | + ); | |
| 70 | + add_action('wp_body_open', array($this, 'find_page_tracking_codes')); | |
| 71 | + add_action('tsm_page_scripts', array($this, 'find_page_tracking_codes')); | |
| 72 | + add_action('admin_init', array($this, 'process_handler')); | |
| 73 | + add_action('admin_notices', array($this, 'admin_notices')); | |
| 74 | + // fallback for page scripts if wp_body_open action isn't supported | |
| 75 | + add_action( | |
| 76 | + 'get_footer', | |
| 77 | + function () { | |
| 78 | + if (did_action('wp_body_open') === 0) { | |
| 79 | + add_action('wp_footer', array($this, 'find_page_tracking_codes')); | |
| 80 | + } | |
| 81 | + } | |
| 82 | + ); | |
| 83 | + // Filters | |
| 84 | + add_filter('manage_r8_tracking_scripts_posts_columns', array($this, 'add_tracking_script_columns')); | |
| 85 | + add_filter( | |
| 86 | + 'manage_edit-r8_tracking_scripts_sortable_columns', | |
| 87 | + array( | |
| 88 | + $this, | |
| 89 | + 'tracking_scripts_column_sort', | |
| 90 | + ) | |
| 91 | + ); | |
| 92 | + // Includes | |
| 93 | + require_once plugin_dir_path(__FILE__) . 'classes/wp-async-request.php'; | |
| 94 | + require_once plugin_dir_path(__FILE__) . 'classes/wp-background-process.php'; | |
| 95 | + require_once plugin_dir_path(__FILE__) . 'classes/class-process-tracking-scripts.php'; | |
| 96 | + $this->process_all = new TSM_Process_Tracking_Scripts(); | |
| 97 | + } | |
| 98 | + | |
| 99 | + /************************************************* | |
| 100 | + * Front End | |
| 101 | + **************************************************/ | |
| 102 | + public function process_handler() | |
| 103 | + { | |
| 104 | + if (! isset($_GET['tsm_update_scripts']) || ! isset($_GET['_wpnonce'])) { | |
| 105 | + return; | |
| 106 | + } | |
| 107 | + if (! wp_verify_nonce(sanitize_key(wp_unslash($_GET['_wpnonce'])), 'tsm_update_scripts')) { | |
| 108 | + return; | |
| 109 | + } | |
| 110 | + if ('true' === $_GET['tsm_update_scripts']) { | |
| 111 | + update_option('tsm_is_processing', true); | |
| 112 | + $this->handle_all(); | |
| 113 | + } | |
| 114 | + } | |
| 115 | + | |
| 116 | + protected function handle_all() | |
| 117 | + { | |
| 118 | + $scripts = $this->get_tracking_scripts(); | |
| 119 | + if (! empty($scripts)) { | |
| 120 | + foreach ($scripts as $script) { | |
| 121 | + $this->process_all->push_to_queue($script); | |
| 122 | + } | |
| 123 | + $this->process_all->save()->dispatch(); | |
| 124 | + } | |
| 125 | + } | |
| 126 | + | |
| 127 | + | |
| 128 | + protected function get_tracking_scripts() | |
| 129 | + { | |
| 130 | + $scripts = array(); | |
| 131 | + $header_scripts = get_option('header_tracking_script_code') ? json_decode(get_option('header_tracking_script_code')) : null; | |
| 132 | + $page_scripts = get_option('page_tracking_script_code') ? json_decode(get_option('page_tracking_script_code')) : null; | |
| 133 | + $footer_scripts = get_option('footer_tracking_script_code') ? json_decode(get_option('footer_tracking_script_code')) : null; | |
| 134 | + if (! empty($header_scripts)) { | |
| 135 | + $scripts = array_merge($scripts, $header_scripts); | |
| 136 | + } | |
| 137 | + if (! empty($page_scripts)) { | |
| 138 | + $scripts = array_merge($scripts, $page_scripts); | |
| 139 | + } | |
| 140 | + if (! empty($footer_scripts)) { | |
| 141 | + $scripts = array_merge($scripts, $footer_scripts); | |
| 142 | + } | |
| 143 | + | |
| 144 | + return $scripts; | |
| 145 | + } | |
| 146 | + | |
| 147 | + function admin_notices() | |
| 148 | + { | |
| 149 | + $class = 'notice notice-info is-dismissible'; | |
| 150 | + $header_scripts = get_option('header_tracking_script_code'); | |
| 151 | + $page_scripts = get_option('page_tracking_script_code'); | |
| 152 | + $footer_scripts = get_option('footer_tracking_script_code'); | |
| 153 | + $is_processing = get_option('tsm_is_processing'); | |
| 154 | + $has_tracking_scripts = $header_scripts || $page_scripts || $footer_scripts; | |
| 155 | + $is_admin = current_user_can('manage_options'); | |
| 156 | + if ($has_tracking_scripts && $is_processing && $is_admin) { | |
| 157 | + $message = __('Your scripts are currently processing. This may take several minutes. If you don’t see all of your scripts please wait a moment and refresh the page.', TRACKING_SCRIPT_TEXTDOMAIN); | |
| 158 | + $notice = sprintf('<div class="%1$s"><p>%2$s</p></div>', esc_attr($class), esc_html($message)); | |
| 159 | + echo esc_html($notice); | |
| 160 | + } | |
| 161 | + if ($has_tracking_scripts && ! $is_processing && $is_admin) { | |
| 162 | + $url = wp_nonce_url(admin_url('edit.php?post_type=r8_tracking_scripts&tsm_update_scripts=true&tsm_is_processing=true'), 'tsm_update_scripts'); | |
| 163 | + $message = __('Tracking Scripts Manager has updated to a new version, click OK to update your scripts to the updated version.', TRACKING_SCRIPT_TEXTDOMAIN); | |
| 164 | + $notice = sprintf('<div class="%1$s"><p>%2$s</p><a class="button button-primary" href="%3$s" style="margin-bottom: .5em;">OK</a></div>', esc_attr($class), esc_html($message), esc_url($url)); | |
| 165 | + echo esc_html($notice); | |
| 166 | + } | |
| 167 | + } | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + public function print_tsm_scripts($script_id, $page, $page_id, $expiry_info) | |
| 172 | + { | |
| 173 | + $expiry_data = $this->expiry_data($expiry_info); | |
| 174 | + $if_expire = $this->check_expiry_script($expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date'], $script_id); | |
| 175 | + $script = get_post_meta($script_id, 'r8_tsm_script_code', true); | |
| 176 | + | |
| 177 | + $encoded_save = get_post_meta($script_id, 'r8_tsm_encoded_save', true); | |
| 178 | + if (!$encoded_save) { | |
| 179 | + $script = base64_encode($script); | |
| 180 | + $this->save_script($script_id, $script); | |
| 181 | + } | |
| 182 | + | |
| 183 | + $page_script = $this->esc_script($script); | |
| 184 | + | |
| 185 | + | |
| 186 | + // Check if this is the right page | |
| 187 | + if ((is_array($page) && in_array(intval($page_id), $page, true)) || empty($page)) { | |
| 188 | + // Is it scheduled and not expired or set never to expire? | |
| 189 | + if ('Schedule' === $expiry_data['type'] && ! $if_expire || 'Never' === $expiry_data['type']) { | |
| 190 | + // Render script | |
| 191 | + echo ($page_script); | |
| 192 | + } | |
| 193 | + } | |
| 194 | + } | |
| 195 | + | |
| 196 | + // Header Tracking Codes | |
| 197 | + function find_header_tracking_codes() | |
| 198 | + { | |
| 199 | + global $wp_query; | |
| 200 | + $page_id = $wp_query->post->ID; | |
| 201 | + $args = array( | |
| 202 | + 'post_type' => 'r8_tracking_scripts', | |
| 203 | + 'post_status' => 'publish', | |
| 204 | + 'posts_per_page' => -1, | |
| 205 | + 'meta_key' => 'r8_tsm_script_order', | |
| 206 | + 'orderby' => 'meta_value_num', | |
| 207 | + 'order' => 'ASC', | |
| 208 | + 'meta_query' => array( | |
| 209 | + 'relation' => 'AND', | |
| 210 | + array( | |
| 211 | + 'key' => 'r8_tsm_script_location', | |
| 212 | + 'value' => 'header', | |
| 213 | + 'compare' => '=', | |
| 214 | + ), | |
| 215 | + array( | |
| 216 | + 'key' => 'r8_tsm_active', | |
| 217 | + 'value' => 'active', | |
| 218 | + 'compare' => '=', | |
| 219 | + ), | |
| 220 | + ), | |
| 221 | + ); | |
| 222 | + $header_scripts = new WP_Query($args); | |
| 223 | + | |
| 224 | + | |
| 225 | + if ($header_scripts->have_posts()) { | |
| 226 | + while ($header_scripts->have_posts()) : | |
| 227 | + $header_scripts->the_post(); | |
| 228 | + $page = get_post_meta(get_the_ID(), 'r8_tsm_script_page', true); | |
| 229 | + $expiry_info = get_post_meta(get_the_ID(), 'r8_tsm_script_expiry_info', true); | |
| 230 | + $this->print_tsm_scripts(get_the_ID(), $page, $page_id, $expiry_info); | |
| 231 | + endwhile; | |
| 232 | + wp_reset_postdata(); | |
| 233 | + } | |
| 234 | + } | |
| 235 | + | |
| 236 | + function find_page_tracking_codes() | |
| 237 | + { | |
| 238 | + global $wp_query; | |
| 239 | + $page_id = $wp_query->post->ID; | |
| 240 | + $args = array( | |
| 241 | + 'post_type' => 'r8_tracking_scripts', | |
| 242 | + 'posts_per_page' => -1, | |
| 243 | + 'post_status' => 'publish', | |
| 244 | + 'meta_key' => 'r8_tsm_script_order', | |
| 245 | + 'orderby' => 'meta_value_num', | |
| 246 | + 'order' => 'ASC', | |
| 247 | + 'meta_query' => array( | |
| 248 | + 'relation' => 'AND', | |
| 249 | + array( | |
| 250 | + 'key' => 'r8_tsm_script_location', | |
| 251 | + 'value' => 'page', | |
| 252 | + 'compare' => '=', | |
| 253 | + ), | |
| 254 | + array( | |
| 255 | + 'key' => 'r8_tsm_active', | |
| 256 | + 'value' => 'active', | |
| 257 | + 'compare' => '=', | |
| 258 | + ), | |
| 259 | + ), | |
| 260 | + ); | |
| 261 | + $page_scripts = new WP_Query($args); | |
| 262 | + if ($page_scripts->have_posts()) { | |
| 263 | + while ($page_scripts->have_posts()) : | |
| 264 | + $page_scripts->the_post(); | |
| 265 | + $page = get_post_meta(get_the_ID(), 'r8_tsm_script_page', true); | |
| 266 | + $expiry_info = get_post_meta(get_the_ID(), 'r8_tsm_script_expiry_info', true); | |
| 267 | + $this->print_tsm_scripts(get_the_ID(), $page, $page_id, $expiry_info); | |
| 268 | + endwhile; | |
| 269 | + wp_reset_postdata(); | |
| 270 | + } | |
| 271 | + } | |
| 272 | + | |
| 273 | + function find_footer_tracking_codes() | |
| 274 | + { | |
| 275 | + global $wp_query; | |
| 276 | + $page_id = $wp_query->post->ID; | |
| 277 | + $args = array( | |
| 278 | + 'post_type' => 'r8_tracking_scripts', | |
| 279 | + 'posts_per_page' => -1, | |
| 280 | + 'post_status' => 'publish', | |
| 281 | + 'meta_key' => 'r8_tsm_script_order', | |
| 282 | + 'orderby' => 'meta_value_num', | |
| 283 | + 'order' => 'ASC', | |
| 284 | + 'meta_query' => array( | |
| 285 | + 'relation' => 'AND', | |
| 286 | + array( | |
| 287 | + 'key' => 'r8_tsm_script_location', | |
| 288 | + 'value' => 'footer', | |
| 289 | + 'compare' => '=', | |
| 290 | + ), | |
| 291 | + array( | |
| 292 | + 'key' => 'r8_tsm_active', | |
| 293 | + 'value' => 'active', | |
| 294 | + 'compare' => '=', | |
| 295 | + ), | |
| 296 | + ), | |
| 297 | + ); | |
| 298 | + $footer_scripts = new WP_Query($args); | |
| 299 | + if ($footer_scripts->have_posts()) { | |
| 300 | + while ($footer_scripts->have_posts()) : | |
| 301 | + $footer_scripts->the_post(); | |
| 302 | + $page = get_post_meta(get_the_ID(), 'r8_tsm_script_page', true); | |
| 303 | + $expiry_info = get_post_meta(get_the_ID(), 'r8_tsm_script_expiry_info', true); | |
| 304 | + $this->print_tsm_scripts(get_the_ID(), $page, $page_id, $expiry_info); | |
| 305 | + endwhile; | |
| 306 | + wp_reset_postdata(); | |
| 307 | + } | |
| 308 | + } | |
| 309 | + | |
| 310 | + function add_tracking_script_columns($columns) | |
| 311 | + { | |
| 312 | + $columns = array( | |
| 313 | + 'cb' => '<input type="checkbox" />', | |
| 314 | + 'title' => __('Script Title', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 315 | + 'global' => __('Global', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 316 | + 'location' => __('Location', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 317 | + 'status' => __('Status', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 318 | + 'schedule' => __('Schedule', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 319 | + ); | |
| 320 | + | |
| 321 | + return $columns; | |
| 322 | + } | |
| 323 | + | |
| 324 | + function tracking_script_column_content($column_name, $post_ID) | |
| 325 | + { | |
| 326 | + $expiry_info = get_post_meta($post_ID, 'r8_tsm_script_expiry_info', true); | |
| 327 | + $expiry_data = $this->expiry_data($expiry_info); | |
| 328 | + $if_expire = $this->check_expiry_script($expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date'], $post_ID); | |
| 329 | + $scheduled_status = $this->scheduled_status($if_expire, $expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date']); | |
| 330 | + | |
| 331 | + if ($column_name === 'status') { | |
| 332 | + $active = get_post_meta($post_ID, 'r8_tsm_active', true); | |
| 333 | + echo ($active === 'inactive') ? '<span class="expired">' : '<span>'; | |
| 334 | + if ($active === 'active') { | |
| 335 | + echo 'Active'; | |
| 336 | + } else { | |
| 337 | + echo 'Inactive'; | |
| 338 | + } | |
| 339 | + echo esc_attr($scheduled_status); | |
| 340 | + echo '</span>'; | |
| 341 | + } | |
| 342 | + | |
| 343 | + if ($column_name === 'global') { | |
| 344 | + $global = get_post_meta($post_ID, 'r8_tsm_script_page', true); | |
| 345 | + if (empty($global)) { | |
| 346 | + echo ' ✓'; | |
| 347 | + } else { | |
| 348 | + echo ' ✗'; | |
| 349 | + } | |
| 350 | + } | |
| 351 | + if ($column_name === 'location') { | |
| 352 | + $location = get_post_meta($post_ID, 'r8_tsm_script_location', true); | |
| 353 | + if ($location) { | |
| 354 | + echo esc_html(ucwords($location)); | |
| 355 | + } | |
| 356 | + } | |
| 357 | + if ($column_name === 'schedule') { | |
| 358 | + if ($expiry_data['type'] === 'Schedule') { | |
| 359 | + echo esc_html( | |
| 360 | + sprintf( | |
| 361 | + __('Scheduled <b>%1$s</b> to <b>%2$s</b>', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 362 | + ($expiry_data['start_date']), | |
| 363 | + ($expiry_data['end_date']) | |
| 364 | + ) | |
| 365 | + ); | |
| 366 | + } else { | |
| 367 | + esc_html_e('Never expires', TRACKING_SCRIPT_TEXTDOMAIN); | |
| 368 | + } | |
| 369 | + } | |
| 370 | + } | |
| 371 | + | |
| 372 | + function tracking_scripts_column_sort($columns) | |
| 373 | + { | |
| 374 | + $columns['global'] = 'global'; | |
| 375 | + $columns['location'] = 'location'; | |
| 376 | + $columns['status'] = 'status'; | |
| 377 | + $columns['schedule'] = 'schedule'; | |
| 378 | + | |
| 379 | + return $columns; | |
| 380 | + } | |
| 381 | + | |
| 382 | + public function add_script_metaboxes() | |
| 383 | + { | |
| 384 | + add_meta_box( | |
| 385 | + 'r8_tsm_script_code_wrapper', | |
| 386 | + __('Script Code', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 387 | + array( | |
| 388 | + $this, | |
| 389 | + 'script_code_metabox', | |
| 390 | + ), | |
| 391 | + 'r8_tracking_scripts', | |
| 392 | + 'normal' | |
| 393 | + ); | |
| 394 | + add_meta_box( | |
| 395 | + 'r8_tsm_script_active', | |
| 396 | + __('Script Status', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 397 | + array( | |
| 398 | + $this, | |
| 399 | + 'script_active_metabox', | |
| 400 | + ), | |
| 401 | + 'r8_tracking_scripts', | |
| 402 | + 'side' | |
| 403 | + ); | |
| 404 | + add_meta_box( | |
| 405 | + 'r8_tsm_script_expiry', | |
| 406 | + __('Schedule', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 407 | + array( | |
| 408 | + $this, | |
| 409 | + 'script_expiry_metabox', | |
| 410 | + ), | |
| 411 | + 'r8_tracking_scripts', | |
| 412 | + 'side' | |
| 413 | + ); | |
| 414 | + add_meta_box( | |
| 415 | + 'r8_tsm_script_order', | |
| 416 | + __('Script Order', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 417 | + array( | |
| 418 | + $this, | |
| 419 | + 'script_order_metabox', | |
| 420 | + ), | |
| 421 | + 'r8_tracking_scripts', | |
| 422 | + 'side' | |
| 423 | + ); | |
| 424 | + add_meta_box( | |
| 425 | + 'r8_tsm_script_location', | |
| 426 | + __('Script Location', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 427 | + array( | |
| 428 | + $this, | |
| 429 | + 'script_location_metabox', | |
| 430 | + ), | |
| 431 | + 'r8_tracking_scripts', | |
| 432 | + 'normal' | |
| 433 | + ); | |
| 434 | + add_meta_box( | |
| 435 | + 'r8_tsm_script_page', | |
| 436 | + __('Specific Script Placement (Page(s) or Post(s))', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 437 | + array( | |
| 438 | + $this, | |
| 439 | + 'script_page_metabox', | |
| 440 | + ), | |
| 441 | + 'r8_tracking_scripts', | |
| 442 | + 'normal' | |
| 443 | + ); | |
| 444 | + } | |
| 445 | + | |
| 446 | + function script_code_metabox() | |
| 447 | + { | |
| 448 | + global $post; | |
| 449 | + $script_code = get_post_meta($post->ID, 'r8_tsm_script_code', true); | |
| 450 | + /** | |
| 451 | + * Check if script was saved using base64 encode | |
| 452 | + */ | |
| 453 | + $encoded_save = get_post_meta($post->ID, 'r8_tsm_encoded_save', true); | |
| 454 | + if (!$encoded_save) { | |
| 455 | + $script_code = base64_encode($script_code); | |
| 456 | + $this->save_script($post->ID, $script_code); | |
| 457 | + } | |
| 458 | + | |
| 459 | + if ($this->is_file_modification_allowed()) { | |
| 460 | +?> | |
| 461 | + <div class="red8_script_notice" style=" padding: 1rem; border: 1px solid lightcoral; box-shadow: 0 2px 6px rgb(0 0 0 / 25%); border-radius: 11px;}"> | |
| 462 | + <h1>Heads up!</h1> | |
| 463 | + <p> | |
| 464 | + Adding custom scripts is not recommended and could break your site. | |
| 465 | + </p> | |
| 466 | + <p> | |
| 467 | + Please double check that the code you are adding is secure and make sure your WordPress site is backed up | |
| 468 | + in the likely event that something breaks.</p> | |
| 469 | + | |
| 470 | + <p> | |
| 471 | + <button type="button" class="button button-primary consent">I understand</button> | |
| 472 | + </p> | |
| 473 | + | |
| 474 | + </div> | |
| 475 | + <script type="text/javascript"> | |
| 476 | + jQuery(function($) { | |
| 477 | + $(".red8_script_notice button.consent").on("click", function() { | |
| 478 | + $(".red8_script_notice").hide(); | |
| 479 | + $("#red8_code_editor_wrapper") | |
| 480 | + .css("opacity", 1) | |
| 481 | + .css('height', 'auto'); | |
| 482 | + | |
| 483 | + }) | |
| 484 | + }) | |
| 485 | + </script> | |
| 486 | + | |
| 487 | + <div id="red8_code_editor_wrapper" style="opacity: 0; height: 0;"> | |
| 488 | + <textarea name="r8_tsm_script_code" id="r8_tsm_script_code" rows="5"><?php | |
| 489 | + if ($script_code) { | |
| 490 | + echo stripslashes(html_entity_decode(base64_decode($script_code), ENT_QUOTES, 'cp1252')); | |
| 491 | + } | |
| 492 | + ?></textarea> | |
| 493 | + </div> | |
| 494 | + | |
| 495 | + <?php | |
| 496 | + } else { | |
| 497 | + ?> | |
| 498 | + <div class="notice notice-error "> | |
| 499 | + <p>File modification & custom scripts have been disallowed by your WordPress config.</p> | |
| 500 | + </div> | |
| 501 | +<?php | |
| 502 | + } | |
| 503 | + } | |
| 504 | + | |
| 505 | + function script_active_metabox() | |
| 506 | + { | |
| 507 | + global $post; | |
| 508 | + $active = get_post_meta($post->ID, 'r8_tsm_active', true); | |
| 509 | + $expiry_info = get_post_meta($post->ID, 'r8_tsm_script_expiry_info', true); | |
| 510 | + $expiry_data = $this->expiry_data($expiry_info); | |
| 511 | + $if_expire = $this->check_expiry_script($expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date'], $post->ID); | |
| 512 | + $scheduled_status = $this->scheduled_status($if_expire, $expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date']); | |
| 513 | + | |
| 514 | + include_once TRACKING_SCRIPT_DIR_PATH . '/templates/script-active-metabox.php'; | |
| 515 | + } | |
| 516 | + | |
| 517 | + function script_expiry_metabox() | |
| 518 | + { | |
| 519 | + global $post; | |
| 520 | + $expiry_info = get_post_meta($post->ID, 'r8_tsm_script_expiry_info', true); | |
| 521 | + $expiry_data = $this->expiry_data($expiry_info); | |
| 522 | + $if_expire = $this->check_expiry_script($expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date'], $post->ID); | |
| 523 | + $scheduled_status = $this->scheduled_status($if_expire, $expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date']); | |
| 524 | + include_once TRACKING_SCRIPT_DIR_PATH . '/templates/script-expiry-metabox.php'; | |
| 525 | + } | |
| 526 | + | |
| 527 | + function script_order_metabox() | |
| 528 | + { | |
| 529 | + global $post; | |
| 530 | + $order = get_post_meta($post->ID, 'r8_tsm_script_order', true); | |
| 531 | + include_once TRACKING_SCRIPT_DIR_PATH . '/templates/script-order-metabox.php'; | |
| 532 | + } | |
| 533 | + | |
| 534 | + function script_location_metabox() | |
| 535 | + { | |
| 536 | + global $post; | |
| 537 | + $location = get_post_meta($post->ID, 'r8_tsm_script_location', true); | |
| 538 | + include_once TRACKING_SCRIPT_DIR_PATH . '/templates/script-location-metabox.php'; | |
| 539 | + } | |
| 540 | + | |
| 541 | + function script_page_metabox() | |
| 542 | + { | |
| 543 | + global $post; | |
| 544 | + $script_page = get_post_meta($post->ID, 'r8_tsm_script_page', true); | |
| 545 | + | |
| 546 | + include_once TRACKING_SCRIPT_DIR_PATH . '/templates/script-page-metabox.php'; | |
| 547 | + } | |
| 548 | + | |
| 549 | + public function get_date_time($timespan, $format) | |
| 550 | + { | |
| 551 | + $current_time = new DateTime(); | |
| 552 | + $current_time->add(new DateInterval($timespan)); | |
| 553 | + $expire_time = $current_time->format($format); | |
| 554 | + | |
| 555 | + return $expire_time; | |
| 556 | + } | |
| 557 | + | |
| 558 | + public function check_expiry_script($expiry_date_type, $expiry_start_date, $expiry_end_date, $script_id) | |
| 559 | + { | |
| 560 | + $result = false; | |
| 561 | + if ($expiry_date_type === 'Never') { | |
| 562 | + return $result; | |
| 563 | + } | |
| 564 | + if (empty($expiry_start_date)) { | |
| 565 | + return $result; | |
| 566 | + } | |
| 567 | + if (empty($expiry_end_date)) { | |
| 568 | + return $result; | |
| 569 | + } | |
| 570 | + | |
| 571 | + $date_range = array(); | |
| 572 | + $start_time = $expiry_start_date; | |
| 573 | + $interval = new DateInterval('P1D'); | |
| 574 | + $end_time = new DateTime($expiry_end_date); | |
| 575 | + $end_time->add($interval); | |
| 576 | + $period = new DatePeriod(new DateTime($start_time), $interval, $end_time); | |
| 577 | + $today = new DateTime(); | |
| 578 | + $today_date = $today->format('Y-m-d'); | |
| 579 | + foreach ($period as $key => $value) { | |
| 580 | + $array[] = $value->format('Y-m-d'); | |
| 581 | + } | |
| 582 | + if (! in_array($today_date, $array, true)) { | |
| 583 | + $result = true; | |
| 584 | + } | |
| 585 | + $this->set_script_status($script_id, $result); | |
| 586 | + return $result; | |
| 587 | + } | |
| 588 | + | |
| 589 | + public function set_script_status($script_id, $result) | |
| 590 | + { | |
| 591 | + global $post; | |
| 592 | + if (! empty($post->post_type)) { | |
| 593 | + if ($post->post_type === 'r8_tracking_scripts') { | |
| 594 | + if ($script_id === $post->ID) { | |
| 595 | + $active = get_post_meta($post->ID, 'r8_tsm_active', true); | |
| 596 | + if ($result === true) { // expire true | |
| 597 | + if ('active' === $active) { | |
| 598 | + update_post_meta($post->ID, 'r8_tsm_active', 'inactive'); | |
| 599 | + } | |
| 600 | + } else { // expire false | |
| 601 | + if ('inactive' === $active) { | |
| 602 | + update_post_meta($post->ID, 'r8_tsm_active', 'active'); | |
| 603 | + } | |
| 604 | + } | |
| 605 | + } | |
| 606 | + } | |
| 607 | + } | |
| 608 | + } | |
| 609 | + | |
| 610 | + public function expiry_data($expiry_info) | |
| 611 | + { | |
| 612 | + $type = is_object($expiry_info) ? $expiry_info->type : 'Never'; | |
| 613 | + $start_date = is_object($expiry_info) ? $expiry_info->schedule_start : ''; | |
| 614 | + $end_date = is_object($expiry_info) ? $expiry_info->schedule_end : ''; | |
| 615 | + return array( | |
| 616 | + 'type' => $type, | |
| 617 | + 'start_date' => $start_date, | |
| 618 | + 'end_date' => $end_date, | |
| 619 | + ); | |
| 620 | + } | |
| 621 | + | |
| 622 | + public function scheduled_status($if_expire, $expiry_date_type, $expiry_start_date, $expiry_end_date) | |
| 623 | + { | |
| 624 | + $status = ''; | |
| 625 | + $start = new DateTime($expiry_start_date); | |
| 626 | + $end = new DateTime($expiry_end_date); | |
| 627 | + $today = new DateTime(); | |
| 628 | + if ($expiry_date_type === 'Schedule') { | |
| 629 | + if (! $if_expire) { | |
| 630 | + $status = ''; | |
| 631 | + } else { | |
| 632 | + if ($today < $start) { | |
| 633 | + $diff = strtotime($today->format('y-m-d')) - strtotime($start->format('y-m-d')); | |
| 634 | + $count = abs(round($diff / 86400)); | |
| 635 | + $next_date = sprintf(_n('tomorrow', 'in %s days', $count, 'tracking-scripts-manager'), $count); | |
| 636 | + $status = sprintf('(Starting %s) ', $next_date); | |
| 637 | + } | |
| 638 | + if ($today > $end) { | |
| 639 | + $status = ' (Expired)'; | |
| 640 | + } | |
| 641 | + } | |
| 642 | + } | |
| 643 | + return $status; | |
| 644 | + } | |
| 645 | + | |
| 646 | + public function register_scripts_post_type() | |
| 647 | + { | |
| 648 | + $labels = array( | |
| 649 | + 'name' => _x('Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 650 | + 'singular_name' => _x('Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 651 | + 'menu_name' => _x('Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 652 | + 'name_admin_bar' => _x('Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 653 | + 'add_new' => _x('Add New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 654 | + 'add_new_item' => __('Add New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 655 | + 'new_item' => __('New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 656 | + 'edit_item' => __('Edit Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 657 | + 'view_item' => __('View Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 658 | + 'all_items' => __('All Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 659 | + 'search_items' => __('Search Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 660 | + 'parent_item_colon' => __('Parent Tracking Scripts:', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 661 | + 'not_found' => __('No Tracking Scripts found.', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 662 | + 'not_found_in_trash' => __('No Tracking Scripts found in Trash.', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 663 | + ); | |
| 664 | + $args = array( | |
| 665 | + 'labels' => $labels, | |
| 666 | + 'description' => __('Description.', TRACKING_SCRIPT_TEXTDOMAIN), | |
| 667 | + 'public' => false, | |
| 668 | + 'publicly_queryable' => false, | |
| 669 | + 'show_ui' => true, | |
| 670 | + 'show_in_menu' => false, | |
| 671 | + 'query_var' => false, | |
| 672 | + 'rewrite' => array('slug' => 'tracking-scripts'), | |
| 673 | + 'capability_type' => 'post', | |
| 674 | + 'capabilities' => array( | |
| 675 | + 'edit_post' => 'manage_options', | |
| 676 | + 'read_post' => 'manage_options', | |
| 677 | + 'delete_post' => 'manage_options', | |
| 678 | + 'edit_posts' => 'manage_options', | |
| 679 | + 'edit_others_posts' => 'manage_options', | |
| 680 | + 'delete_posts' => 'manage_options', | |
| 681 | + 'publish_posts' => 'manage_options', | |
| 682 | + 'read_private_posts' => 'manage_options', | |
| 683 | + ), | |
| 684 | + 'has_archive' => false, | |
| 685 | + 'hierarchical' => false, | |
| 686 | + 'menu_position' => null, | |
| 687 | + 'supports' => array( | |
| 688 | + 'title', | |
| 689 | + 'script-code', | |
| 690 | + 'script-active', | |
| 691 | + 'script-location', | |
| 692 | + 'script-order', | |
| 693 | + ), | |
| 694 | + ); | |
| 695 | + register_post_type('r8_tracking_scripts', $args); | |
| 696 | + } | |
| 697 | + | |
| 698 | + /************************************************* | |
| 699 | + * Admin Area | |
| 700 | + **************************************************/ | |
| 701 | + function admin_enqueue_scripts($hook) | |
| 702 | + { | |
| 703 | + global $post; | |
| 704 | + if ($hook === 'post.php' || $hook === 'post-new.php') { | |
| 705 | + if (! empty($post->post_type) && ($post->post_type === 'r8_tracking_scripts')) { | |
| 706 | + wp_enqueue_style('r8-tsm-edit-script', plugins_url('/css/tracking-script-edit.css', __FILE__), array(), TRACKING_SCRIPT_VERSION); | |
| 707 | + wp_enqueue_style('r8-tsm-select2-css', plugins_url('/css/select2.min.css', __FILE__), array(), TRACKING_SCRIPT_VERSION); | |
| 708 | + wp_enqueue_script('r8-tsm-select2-js', plugins_url('/js/select2.min.js', __FILE__), array(), TRACKING_SCRIPT_VERSION, true); | |
| 709 | + wp_enqueue_script( | |
| 710 | + 'r8-tsm-post-edit-js', | |
| 711 | + plugins_url('/js/post-edit.js', __FILE__), | |
| 712 | + array( | |
| 713 | + 'jquery', | |
| 714 | + 'r8-tsm-select2-js', | |
| 715 | + ), | |
| 716 | + TRACKING_SCRIPT_VERSION, | |
| 717 | + true | |
| 718 | + ); | |
| 719 | + wp_enqueue_style('jquery-ui-css', 'https://code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css'); | |
| 720 | + wp_enqueue_script('jquery-ui-datepicker'); | |
| 721 | + } | |
| 722 | + } | |
| 723 | + if ($hook === 'post.php' || $hook === 'edit.php') { | |
| 724 | + if (! empty($post->post_type) && ($post->post_type === 'r8_tracking_scripts')) { | |
| 725 | + wp_enqueue_style('r8-tsm-post-list', plugins_url('/css/post-list.css', __FILE__), array(), TRACKING_SCRIPT_VERSION); | |
| 726 | + wp_enqueue_script('r8-tsm-post-list-js', plugins_url('/js/post-list.js', __FILE__), array('jquery'), TRACKING_SCRIPT_VERSION, true); | |
| 727 | + } | |
| 728 | + } | |
| 729 | + if (! empty($post->post_type) && ($post->post_type === 'r8_tracking_scripts')) { | |
| 730 | + // code editor support | |
| 731 | + $html_editor = wp_enqueue_code_editor(array('type' => 'text/html')); | |
| 732 | + if (false !== $html_editor) { | |
| 733 | + wp_add_inline_script( | |
| 734 | + 'code-editor', | |
| 735 | + sprintf( | |
| 736 | + 'jQuery( function() { wp.codeEditor.initialize( "r8_tsm_script_code", %s ); } );', | |
| 737 | + wp_json_encode($html_editor) | |
| 738 | + ) | |
| 739 | + ); | |
| 740 | + } | |
| 741 | + } | |
| 742 | + } | |
| 743 | + | |
| 744 | + private function esc_script($script) | |
| 745 | + { | |
| 746 | + return stripslashes(html_entity_decode(base64_decode($script), ENT_QUOTES, 'cp1252')); | |
| 747 | + } | |
| 748 | + | |
| 749 | + private function save_script($post_id, $script_code) | |
| 750 | + { | |
| 751 | + $script_code = stripslashes(wp_unslash($script_code)); | |
| 752 | + update_post_meta($post_id, 'r8_tsm_script_code', $script_code); | |
| 753 | + update_post_meta($post_id, 'r8_tsm_encoded_save', true); | |
| 754 | + } | |
| 755 | + | |
| 756 | + | |
| 757 | + private function is_file_modification_allowed() | |
| 758 | + { | |
| 759 | + if (defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS) { | |
| 760 | + return false; | |
| 761 | + } | |
| 762 | + return true; | |
| 763 | + } | |
| 764 | + | |
| 765 | + function save_post() | |
| 766 | + { | |
| 767 | + global $post; | |
| 768 | + if (! empty($post->post_type)) { | |
| 769 | + if ($post->post_type === 'r8_tracking_scripts') { | |
| 770 | + $expiry_obj = new \stdClass(); | |
| 771 | + $expiry_obj->schedule_start = ''; | |
| 772 | + $expiry_obj->schedule_end = ''; | |
| 773 | + $expiry_obj->type = ''; | |
| 774 | + if (! empty($_POST['r8_tsm_script_code'])) { | |
| 775 | + $script_code = base64_encode($_POST['r8_tsm_script_code']); | |
| 776 | + $this->save_script($post->ID, $script_code); | |
| 777 | + } | |
| 778 | + if (! empty($_POST['r8_tsm_active'])) { | |
| 779 | + $tsm_active = sanitize_text_field(wp_unslash($_POST['r8_tsm_active'])); | |
| 780 | + update_post_meta($post->ID, 'r8_tsm_active', $tsm_active); | |
| 781 | + } | |
| 782 | + if (! empty($_POST['r8_tsm_script_order'])) { | |
| 783 | + update_post_meta($post->ID, 'r8_tsm_script_order', intval($_POST['r8_tsm_script_order'])); | |
| 784 | + } | |
| 785 | + if (! empty($_POST['r8_tsm_script_location'])) { | |
| 786 | + update_post_meta($post->ID, 'r8_tsm_script_location', sanitize_text_field(wp_unslash($_POST['r8_tsm_script_location']))); | |
| 787 | + } | |
| 788 | + if (! empty($_POST['r8_tsm_script_expiry']) || (! empty($_POST['schedule_start']) && ! empty($_POST['schedule_end']))) { | |
| 789 | + $expiry_obj->type = sanitize_text_field(wp_unslash($_POST['r8_tsm_script_expiry'])) ?: 'Never'; | |
| 790 | + $expiry_obj->schedule_start = sanitize_text_field(wp_unslash($_POST['schedule_start'])) ?: ''; | |
| 791 | + $expiry_obj->schedule_end = sanitize_text_field(wp_unslash($_POST['schedule_end'])) ?: ''; | |
| 792 | + update_post_meta($post->ID, 'r8_tsm_script_expiry_info', $expiry_obj); | |
| 793 | + // status updated based on schedule | |
| 794 | + if ($expiry_obj->type === 'Schedule') { | |
| 795 | + $this->check_expiry_script($expiry_obj->type, $expiry_obj->schedule_start, $expiry_obj->schedule_end, $post->ID); | |
| 796 | + } | |
| 797 | + } | |
| 798 | + if (! empty($_POST['r8_tsm_script_page']) && is_array($_POST['r8_tsm_script_page'])) { | |
| 799 | + | |
| 800 | + $script_pages = array_map('intval', wp_unslash($_POST['r8_tsm_script_page'])); | |
| 801 | + | |
| 802 | + update_post_meta($post->ID, 'r8_tsm_script_page', $script_pages); | |
| 803 | + } else { | |
| 804 | + update_post_meta($post->ID, 'r8_tsm_script_page', array()); | |
| 805 | + } | |
| 806 | + } | |
| 807 | + } | |
| 808 | + } | |
| 809 | + | |
| 810 | + public function tracking_scripts_create_menu() | |
| 811 | + { | |
| 812 | + add_menu_page('Tracking Script Manager', 'Tracking Script Manager', 'manage_options', 'edit.php?post_type=r8_tracking_scripts', null); | |
| 813 | + add_submenu_page('edit.php?post_type=r8_tracking_scripts', 'Add New Tracking Script', 'Add New Tracking Script', 'manage_options', 'post-new.php?post_type=r8_tracking_scripts', null); | |
| 814 | + } | |
| 815 | + | |
| 816 | + // Admin Scripts | |
| 817 | + public function tracking_scripts_admin_scripts() | |
| 818 | + { | |
| 819 | + wp_enqueue_script('jquery'); | |
| 820 | + wp_enqueue_script('tracking_script_js', plugin_dir_url(__FILE__) . '/js/built.min.js', array(), md5_file(plugin_dir_url(__FILE__) . '/js/built.min.js'), true); | |
| 821 | + wp_localize_script('tracking_script_js', 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php'))); | |
| 822 | + } | |
| 823 | + | |
| 824 | + // Ajax Functions | |
| 825 | + public function tracking_scripts_posts_ajax_handler() | |
| 826 | + { | |
| 827 | + $post_type = isset($_POST['postType']) ? sanitize_text_field(wp_unslash($_POST['postType'])) : 'post'; | |
| 828 | + $args = array( | |
| 829 | + 'post_type' => $post_type, | |
| 830 | + 'posts_per_page' => -1, | |
| 831 | + 'orderby' => 'name', | |
| 832 | + 'order' => 'ASC', | |
| 833 | + ); | |
| 834 | + ob_start(); | |
| 835 | + $query = new WP_Query($args); | |
| 836 | + echo '<option value="none" id="none">Choose ' . esc_html(ucwords($post_type)) . '</option>'; | |
| 837 | + while ($query->have_posts()) : | |
| 838 | + $query->the_post(); | |
| 839 | + echo '<option value="' . esc_attr(get_the_ID()) . '" id="' . esc_attr(get_the_ID()) . '">' . esc_html(ucwords(get_the_title())) . '</option>'; | |
| 840 | + endwhile; | |
| 841 | + wp_reset_postdata(); | |
| 842 | + echo esc_html(ob_get_clean()); | |
| 843 | + die(); | |
| 844 | + } | |
| 845 | + } | |
| 846 | + | |
| 847 | + function tracking_scripts() | |
| 848 | + { | |
| 849 | + | |
| 850 | + // globals | |
| 851 | + global $tracking_scripts; | |
| 852 | + // initialize | |
| 853 | + if (! isset($tracking_scripts)) { | |
| 854 | + $tracking_scripts = new Tracking_Scripts(); | |
| 855 | + $tracking_scripts->initialize(); | |
| 856 | + } | |
| 857 | + | |
| 858 | + // return | |
| 859 | + return $tracking_scripts; | |
| 860 | + } | |
| 861 | + | |
| 862 | + // initialize | |
| 863 | + tracking_scripts(); | |
| 864 | +} | |