Changeset 2667452
- Timestamp:
- 01/27/2022 06:33:52 PM (4 years ago)
- Location:
- tracking-script-manager
- Files:
-
- 6 edited
- 1 copied
-
tags/2.0.7 (copied) (copied from tracking-script-manager/trunk)
-
tags/2.0.7/js/post-edit.js (modified) (1 diff)
-
tags/2.0.7/readme.txt (modified) (2 diffs)
-
tags/2.0.7/tracking-scripts-manager.php (modified) (1 diff)
-
trunk/js/post-edit.js (modified) (1 diff)
-
trunk/readme.txt (modified) (2 diffs)
-
trunk/tracking-scripts-manager.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
tracking-script-manager/tags/2.0.7/js/post-edit.js
r2665551 r2667452 10 10 } 11 11 }); 12 13 // added class to expired row 14 $('#the-list tr td:last-child span.expired').parent().siblings().addClass('expired_inactive'); 15 12 16 13 // specific script element 17 14 $('.r8_tsm_page_select').select2({ -
tracking-script-manager/tags/2.0.7/readme.txt
r2665551 r2667452 5 5 Requires at least: 4.0 6 6 Tested up to: 5.8 7 Stable tag: 2.0. 67 Stable tag: 2.0.7 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 55 55 56 56 == Changelog == 57 58 = 2.0.7 = 59 * Fixed a bug that caused scripts to be deactivated 60 * Fixed a JS bug in admin views 61 * Admin notice added 57 62 58 63 = 2.0.6 = -
tracking-script-manager/tags/2.0.7/tracking-scripts-manager.php
r2665551 r2667452 1 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: 2.0.6 7 * Author: Red8 Interactive 8 * Author URI: http://red8interactive.com 9 * License: GPLv2 or later 10 */ 11 12 /* 13 Copyright 2019 Red8 Interactive (email : [email protected]) 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 /** 39 * @var TSM_Process_Tracking_Scripts 40 */ 41 protected $process_all; 42 43 function __construct() {} 44 45 public function initialize() { 46 47 // Constants 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 53 // Actions 54 add_action( 'init', array( $this, 'register_scripts_post_type' ) ); 55 add_action( 'save_post', array( $this, 'save_post' ) ); 56 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); 57 add_action( 'wp_head', array( $this, 'find_header_tracking_codes' ), 10 ); 58 add_action( 'wp_footer', array( $this, 'find_footer_tracking_codes'), 10 ); 59 add_action( 'admin_menu', array( $this, 'tracking_scripts_create_menu') ); 60 add_action( 'add_meta_boxes', array( $this, 'add_script_metaboxes' ) ); 61 add_action( 'wp_ajax_tracking_scripts_get_posts', array( $this, 'tracking_scripts_posts_ajax_handler' ) ); 62 add_action( 'manage_r8_tracking_scripts_posts_custom_column', array( $this, 'tracking_script_column_content' ), 10, 2 ); 63 add_action( 'wp_body_open', array( $this, 'find_page_tracking_codes' ) ); 64 add_action( 'tsm_page_scripts', array( $this, 'find_page_tracking_codes' ) ); 65 add_action( 'admin_init', array( $this, 'process_handler' ) ); 66 add_action( 'admin_notices', array( $this, 'admin_notices' ) ); 67 68 // fallback for page scripts if wp_body_open action isn't supported 69 add_action( 'get_footer', function() { 70 if ( did_action( 'wp_body_open' ) === 0 ) { 71 add_action( 'wp_footer', array( $this, 'find_page_tracking_codes' ) ); 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: 2.0.7 7 * Author: Red8 Interactive 8 * Author URI: http://red8interactive.com 9 * License: GPLv2 or later 10 */ 11 /* 12 Copyright 2019 Red8 Interactive (email : [email protected]) 13 14 This program is free software; you can redistribute it and/or 15 modify it under the terms of the GNU General Public License 16 as published by the Free Software Foundation; either version 2 17 of the License, or (at your option) any later version. 18 19 This program is distributed in the hope that it will be useful, 20 but WITHOUT ANY WARRANTY; without even the implied warranty of 21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 GNU General Public License for more details. 23 24 You should have received a copy of the GNU General Public License 25 along with this program; if not, write to the Free Software 26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 27 */ 28 if ( ! defined( 'ABSPATH' ) ) { 29 exit; // Exit if accessed directly 30 } 31 if ( ! class_exists( 'Tracking_Scripts' ) ) { 32 33 class Tracking_Scripts { 34 /** 35 * @var TSM_Process_Tracking_Scripts 36 */ 37 protected $process_all; 38 39 function __construct() { 40 } 41 42 public function initialize() { 43 44 // Constants 45 define( 'TRACKING_SCRIPT_PATH', plugins_url( ' ', __FILE__ ) ); 46 define( 'TRACKING_SCRIPT_BASENAME', plugin_basename( __FILE__ ) ); 47 define( 'TRACKING_SCRIPT_DIR_PATH', plugin_dir_path( __FILE__ ) ); 48 define( 'TRACKING_SCRIPT_TEXTDOMAIN', 'tracking-scripts-manager' ); 49 // Actions 50 add_action( 'init', array( $this, 'register_scripts_post_type' ) ); 51 add_action( 'save_post', array( $this, 'save_post' ) ); 52 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); 53 add_action( 'wp_head', array( $this, 'find_header_tracking_codes' ), 10 ); 54 add_action( 'wp_footer', array( $this, 'find_footer_tracking_codes' ), 10 ); 55 add_action( 'admin_menu', array( $this, 'tracking_scripts_create_menu' ) ); 56 add_action( 'add_meta_boxes', array( $this, 'add_script_metaboxes' ) ); 57 add_action( 'wp_ajax_tracking_scripts_get_posts', array( $this, 'tracking_scripts_posts_ajax_handler' ) ); 58 add_action( 'manage_r8_tracking_scripts_posts_custom_column', array( 59 $this, 60 'tracking_script_column_content' 61 ), 10, 2 ); 62 add_action( 'wp_body_open', array( $this, 'find_page_tracking_codes' ) ); 63 add_action( 'tsm_page_scripts', array( $this, 'find_page_tracking_codes' ) ); 64 add_action( 'admin_init', array( $this, 'process_handler' ) ); 65 add_action( 'admin_notices', array( $this, 'admin_notices' ) ); 66 add_action( 'admin_notices', array( $this, 'new_update_admin_notice' ) ); 67 add_action( 'admin_init', array( $this, "init_update_admin_notice" ) ); 68 // fallback for page scripts if wp_body_open action isn't supported 69 add_action( 'get_footer', function () { 70 if ( did_action( 'wp_body_open' ) === 0 ) { 71 add_action( 'wp_footer', array( $this, 'find_page_tracking_codes' ) ); 72 } 73 } ); 74 // Filters 75 add_filter( 'manage_r8_tracking_scripts_posts_columns', array( $this, 'add_tracking_script_columns' ) ); 76 add_filter( 'manage_edit-r8_tracking_scripts_sortable_columns', array( 77 $this, 78 'tracking_scripts_column_sort' 79 ) ); 80 // Includes 81 require_once plugin_dir_path( __FILE__ ) . 'classes/wp-async-request.php'; 82 require_once plugin_dir_path( __FILE__ ) . 'classes/wp-background-process.php'; 83 require_once plugin_dir_path( __FILE__ ) . 'classes/class-process-tracking-scripts.php'; 84 $this->process_all = new TSM_Process_Tracking_Scripts(); 85 } 86 87 /************************************************* 88 * Front End 89 **************************************************/ 90 public function process_handler() { 91 92 if ( ! isset( $_GET['tsm_update_scripts'] ) || ! isset( $_GET['_wpnonce'] ) ) { 93 return; 94 } 95 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'tsm_update_scripts' ) ) { 96 return; 97 } 98 if ( 'true' === $_GET['tsm_update_scripts'] ) { 99 update_option( 'tsm_is_processing', true ); 100 $this->handle_all(); 101 } 102 } 103 104 protected function handle_all() { 105 $scripts = $this->get_tracking_scripts(); 106 if ( ! empty( $scripts ) ) { 107 108 foreach ( $scripts as $script ) { 109 $this->process_all->push_to_queue( $script ); 110 } 111 $this->process_all->save()->dispatch(); 112 } 113 } 114 115 protected function get_tracking_scripts() { 116 117 $scripts = array(); 118 $header_scripts = get_option( 'header_tracking_script_code' ) ? unserialize( get_option( 'header_tracking_script_code' ) ) : null; 119 $page_scripts = get_option( 'page_tracking_script_code' ) ? unserialize( get_option( 'page_tracking_script_code' ) ) : null; 120 $footer_scripts = get_option( 'footer_tracking_script_code' ) ? unserialize( get_option( 'footer_tracking_script_code' ) ) : null; 121 if ( ! empty( $header_scripts ) ) { 122 $scripts = array_merge( $scripts, $header_scripts ); 123 } 124 if ( ! empty( $page_scripts ) ) { 125 $scripts = array_merge( $scripts, $page_scripts ); 126 } 127 if ( ! empty( $footer_scripts ) ) { 128 $scripts = array_merge( $scripts, $footer_scripts ); 129 } 130 131 return $scripts; 132 } 133 134 function admin_notices() { 135 $class = 'notice notice-info is-dismissible'; 136 $header_scripts = get_option( 'header_tracking_script_code' ); 137 $page_scripts = get_option( 'page_tracking_script_code' ); 138 $footer_scripts = get_option( 'footer_tracking_script_code' ); 139 $is_processing = get_option( 'tsm_is_processing' ); 140 $has_tracking_scripts = ( $header_scripts || $page_scripts || $footer_scripts ) ? true : false; 141 $is_admin = current_user_can( 'manage_options' ) ? true : false; 142 if ( $has_tracking_scripts && $is_processing && $is_admin ) { 143 $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 ); 144 $notice = sprintf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); 145 echo $notice; 146 } 147 if ( $has_tracking_scripts && ! $is_processing && $is_admin ) { 148 $url = wp_nonce_url( admin_url( 'edit.php?post_type=r8_tracking_scripts&tsm_update_scripts=true&tsm_is_processing=true' ), 'tsm_update_scripts' ); 149 $message = __( 'Tracking Scripts Manager has updated to a new version, click OK to update your scripts to the updated version.', TRACKING_SCRIPT_TEXTDOMAIN ); 150 $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 ) ); 151 echo $notice; 152 } 153 } 154 155 /** 156 * Used to track if the notice has been dismissed 157 */ 158 public function init_update_admin_notice() { 159 160 $user_id = get_current_user_id(); 161 if ( isset( $_GET['tsm_update_notice_dismissed'] ) ) { 162 add_user_meta( $user_id, 'tsm_update_notice_dismissed', true, true ); 163 } 164 } 165 166 /** 167 * A one-time admin notice to the plugin asking site administrators to review their 168 * Tracking Scripts statuses and reactivate any scripts that have been mistakenly deactivated. 169 */ 170 public function new_update_admin_notice() { 171 $user_id = get_current_user_id(); 172 if ( ! get_user_meta( $user_id, 'tsm_update_notice_dismissed' ) ) { 173 $url=admin_url()."edit.php?post_type=r8_tracking_scripts&tsm_update_notice_dismissed"; 174 ?> 175 <div class="notice notice-success "> 176 <p><?php _e( 'An error in Tracking Scripts Manager 2.0.6 may have deactivated your scripts. Please click <a href="'.$url.'">here</a> to review the status of your scripts and reactivate them as needed.', '' ); ?></p> 177 <a href="?tsm_update_notice_dismissed">Dismiss this notice</a> 178 </div> 179 <?php 180 } 181 } 182 183 public function print_tsm_scripts( $page_id, $page, $current_id, $expiry_info ) { 184 $expiry_timestamp = is_object( $expiry_info ) ? $expiry_info->date : 'Never'; 185 $expiry_date_type = is_object( $expiry_info ) ? $expiry_info->type : ''; 186 $if_expire = $this->check_expiry_script( $expiry_timestamp ); 187 if ( $expiry_date_type == 'in' ) { 188 if ( ! $if_expire ) { 189 if ( is_array( $page ) && in_array( intval( $current_id ), $page ) ) { 190 echo html_entity_decode( get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252' ); 191 } elseif ( empty( $page ) ) { 192 echo html_entity_decode( get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252' ); 72 193 } 73 } ); 74 75 // Filters 76 add_filter( 'manage_r8_tracking_scripts_posts_columns', array( $this, 'add_tracking_script_columns' ) ); 77 add_filter( 'manage_edit-r8_tracking_scripts_sortable_columns', array( $this, 'tracking_scripts_column_sort' ) ); 78 79 // Includes 80 require_once plugin_dir_path( __FILE__ ) . 'classes/wp-async-request.php'; 81 require_once plugin_dir_path( __FILE__ ) . 'classes/wp-background-process.php'; 82 require_once plugin_dir_path( __FILE__ ) . 'classes/class-process-tracking-scripts.php'; 83 84 $this->process_all = new TSM_Process_Tracking_Scripts(); 85 86 } 87 88 /************************************************* 89 * Front End 90 **************************************************/ 91 92 public function process_handler() { 93 94 if ( ! isset( $_GET['tsm_update_scripts'] ) || ! isset( $_GET['_wpnonce'] ) ) { 95 return; 96 } 97 98 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'tsm_update_scripts' ) ) { 99 return; 100 } 101 102 if ( 'true' === $_GET['tsm_update_scripts'] ) { 103 update_option( 'tsm_is_processing', true ); 104 $this->handle_all(); 105 } 106 107 } 108 109 protected function handle_all() { 110 $scripts = $this->get_tracking_scripts(); 111 112 if ( ! empty( $scripts ) ) { 113 114 foreach ( $scripts as $script ) { 115 $this->process_all->push_to_queue( $script ); 116 } 117 118 $this->process_all->save()->dispatch(); 119 120 } 121 } 122 123 protected function get_tracking_scripts() { 124 125 $scripts = array(); 126 $header_scripts = get_option( 'header_tracking_script_code' ) ? unserialize( get_option('header_tracking_script_code') ) : null; 127 $page_scripts = get_option( 'page_tracking_script_code' ) ? unserialize( get_option('page_tracking_script_code') ) : null; 128 $footer_scripts = get_option( 'footer_tracking_script_code' ) ? unserialize( get_option('footer_tracking_script_code') ) : null; 129 130 if ( ! empty( $header_scripts ) ) { 131 $scripts = array_merge( $scripts, $header_scripts ); 132 } 133 134 if ( ! empty( $page_scripts ) ) { 135 $scripts = array_merge( $scripts, $page_scripts ); 136 } 137 138 if ( ! empty( $footer_scripts ) ) { 139 $scripts = array_merge( $scripts, $footer_scripts ); 140 } 141 142 return $scripts; 143 144 } 145 146 function admin_notices() { 147 $class = 'notice notice-info is-dismissible'; 148 $header_scripts = get_option('header_tracking_script_code'); 149 $page_scripts = get_option('page_tracking_script_code'); 150 $footer_scripts = get_option('footer_tracking_script_code'); 151 $is_processing = get_option('tsm_is_processing'); 152 $has_tracking_scripts = ( $header_scripts || $page_scripts || $footer_scripts ) ? true : false; 153 $is_admin = current_user_can('manage_options') ? true : false; 154 155 if ( $has_tracking_scripts && $is_processing && $is_admin ) { 156 $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 ); 157 $notice = sprintf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); 158 echo $notice; 159 } 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 $notice; 166 } 167 } 168 169 public function print_tsm_scripts( $page_id, $page, $current_id, $expiry_info ) { 170 $expiry_timestamp = is_object( $expiry_info ) ? $expiry_info->date : ''; 171 $expiry_date_type = is_object( $expiry_info ) ? $expiry_info->type : ''; 172 $if_expire = $this->check_expiy_script( $expiry_timestamp ); 173 174 if( $expiry_date_type == 'in' ){ 175 if( !$if_expire ){ 176 if ( is_array( $page ) && in_array(intval($current_id), $page) ) { 177 echo html_entity_decode(get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252'); 178 } elseif ( empty( $page ) ) { 179 echo html_entity_decode(get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252'); 180 } 181 } 182 }else{ 183 if ( is_array( $page ) && in_array(intval($current_id), $page) ) { 184 echo html_entity_decode(get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252'); 185 } elseif ( empty( $page ) ) { 186 echo html_entity_decode(get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252'); 187 } 188 } 189 } 190 191 // Header Tracking Codes 192 function find_header_tracking_codes() { 193 194 global $wp_query; 195 $current_id = $wp_query->post->ID; 196 197 $args = array( 198 'post_type' => 'r8_tracking_scripts', 199 'post_status' => 'publish', 200 'posts_per_page' => -1, 201 'meta_key' => 'r8_tsm_script_order', 202 'orderby' => 'meta_value_num', 203 'order' => 'ASC', 204 'meta_query' => array( 205 'relation' => 'AND', 206 array( 207 'key' => 'r8_tsm_script_location', 208 'value' => 'header', 209 'compare' => '=' 210 ), 211 array( 212 'key' => 'r8_tsm_active', 213 'value' => 'active', 214 'compare' => '=' 215 ) 194 } 195 } else { 196 if ( is_array( $page ) && in_array( intval( $current_id ), $page ) ) { 197 echo html_entity_decode( get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252' ); 198 } elseif ( empty( $page ) ) { 199 echo html_entity_decode( get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252' ); 200 } 201 } 202 } 203 204 // Header Tracking Codes 205 function find_header_tracking_codes() { 206 207 global $wp_query; 208 $current_id = $wp_query->post->ID; 209 $args = array( 210 'post_type' => 'r8_tracking_scripts', 211 'post_status' => 'publish', 212 'posts_per_page' => - 1, 213 'meta_key' => 'r8_tsm_script_order', 214 'orderby' => 'meta_value_num', 215 'order' => 'ASC', 216 'meta_query' => array( 217 'relation' => 'AND', 218 array( 219 'key' => 'r8_tsm_script_location', 220 'value' => 'header', 221 'compare' => '=' 222 ), 223 array( 224 'key' => 'r8_tsm_active', 225 'value' => 'active', 226 'compare' => '=' 216 227 ) 217 ); 218 219 $header_scripts = new WP_Query($args); 220 221 if ( $header_scripts->have_posts() ) { 222 while ( $header_scripts->have_posts() ) : $header_scripts->the_post(); 223 $page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); 224 $expiry_info = get_post_meta( get_the_ID(), 'r8_tsm_script_expiry_info', true ); 225 226 $this->print_tsm_scripts( get_the_ID(), $page, $current_id, $expiry_info ); 227 228 endwhile; wp_reset_postdata(); 229 } 230 } 231 232 233 function find_page_tracking_codes() { 234 235 global $wp_query; 236 $current_id = $wp_query->post->ID; 237 238 $args = array( 239 'post_type' => 'r8_tracking_scripts', 240 'posts_per_page' => -1, 241 'post_status' => 'publish', 242 'meta_key' => 'r8_tsm_script_order', 243 'orderby' => 'meta_value_num', 244 'order' => 'ASC', 245 'meta_query' => array( 246 'relation' => 'AND', 247 array( 248 'key' => 'r8_tsm_script_location', 249 'value' => 'page', 250 'compare' => '=' 251 ), 252 array( 253 'key' => 'r8_tsm_active', 254 'value' => 'active', 255 'compare' => '=' 256 ) 257 ) 258 ); 259 260 $page_scripts = new WP_Query($args); 261 262 if ( $page_scripts->have_posts() ) { 263 while ( $page_scripts->have_posts() ) : $page_scripts->the_post(); 264 $page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); 265 $expiry_info = get_post_meta( get_the_ID(), 'r8_tsm_script_expiry_info', true ); 266 267 $this->print_tsm_scripts( get_the_ID(), $page, $current_id, $expiry_info ); 268 269 endwhile; wp_reset_postdata(); 270 } 271 272 } 273 274 function find_footer_tracking_codes() { 275 global $wp_query; 276 $current_id = $wp_query->post->ID; 277 278 $args = array( 279 'post_type' => 'r8_tracking_scripts', 280 'posts_per_page' => -1, 281 'post_status' => 'publish', 282 'meta_key' => 'r8_tsm_script_order', 283 'orderby' => 'meta_value_num', 284 'order' => 'ASC', 285 'meta_query' => array( 286 'relation' => 'AND', 287 array( 288 'key' => 'r8_tsm_script_location', 289 'value' => 'footer', 290 'compare' => '=' 291 ), 292 array( 293 'key' => 'r8_tsm_active', 294 'value' => 'active', 295 'compare' => '=' 296 ) 297 ) 298 ); 299 300 $footer_scripts = new WP_Query($args); 301 302 if ( $footer_scripts->have_posts() ) { 303 while ( $footer_scripts->have_posts() ) : $footer_scripts->the_post(); 304 $page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); 305 $expiry_info = get_post_meta( get_the_ID(), 'r8_tsm_script_expiry_info', true ); 306 307 $this->print_tsm_scripts( get_the_ID(), $page, $current_id, $expiry_info ); 308 309 endwhile; wp_reset_postdata(); 310 } 311 } 312 313 function add_tracking_script_columns($columns) { 314 315 $columns = array( 316 'cb' => '<input type="checkbox" />', 317 'title' => __( 'Script Title', TRACKING_SCRIPT_TEXTDOMAIN ), 318 'global' => __( 'Global', TRACKING_SCRIPT_TEXTDOMAIN ), 319 'location' => __( 'Location', TRACKING_SCRIPT_TEXTDOMAIN ), 320 'status' => __( 'Status', TRACKING_SCRIPT_TEXTDOMAIN ), 321 'expire' => __( 'Expires', TRACKING_SCRIPT_TEXTDOMAIN ), 322 ); 323 324 return $columns; 325 326 } 327 328 function tracking_script_column_content($column_name, $post_ID) { 329 330 if ( $column_name === 'status' ) { 331 $active = get_post_meta( $post_ID, 'r8_tsm_active', true ); 332 if ( $active === 'active' ) { 333 echo 'Active'; 334 } else { 335 echo 'Inactive'; 336 } 337 } 338 339 if ( $column_name === 'global' ) { 340 $global = get_post_meta( $post_ID, 'r8_tsm_script_page', true ); 341 342 if ( empty($global) ) { 343 echo ' ✓'; 344 } else { 345 echo ' ✗'; 346 } 347 } 348 349 if ( $column_name === 'location' ) { 350 $location = get_post_meta( $post_ID, 'r8_tsm_script_location', true ); 351 352 if ( $location ) { 353 echo ucwords($location); 354 } 355 } 356 357 if ( $column_name === 'expire' ) { 358 359 $expiry_info = get_post_meta( $post_ID, 'r8_tsm_script_expiry_info', true ); 360 $expiry_timestamp = is_object( $expiry_info ) ? $expiry_info->date : ''; 361 $expiry_date_type = is_object( $expiry_info ) ? $expiry_info->type : ''; 362 $expiry_date = $this->get_expiry_info( $expiry_date_type, $expiry_timestamp ); 363 $if_expire = $this->check_expiy_script( $expiry_timestamp ); 364 365 if( ( $expiry_date_type == 'in' ) && $if_expire ){ 366 echo '<span class="expired">Expired</span>'; 367 }else{ 368 echo $expiry_date; 369 } 370 } 371 372 } 373 374 function tracking_scripts_column_sort($columns) { 375 376 $columns['global'] = 'global'; 377 $columns['location'] = 'location'; 378 $columns['status'] = 'status'; 379 $columns['expire'] = 'expire'; 380 381 return $columns; 382 383 } 384 385 public function add_script_metaboxes() { 386 387 add_meta_box( 'r8_tsm_script_code', __( 'Script Code', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_code_metabox' ), 'r8_tracking_scripts', 'normal' ); 388 add_meta_box( 'r8_tsm_script_active', __( 'Script Status', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_active_metabox' ), 'r8_tracking_scripts', 'side' ); 389 add_meta_box( 'r8_tsm_script_expiry', __( 'Script Expires', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_expiry_metabox' ), 'r8_tracking_scripts', 'side' ); 390 add_meta_box( 'r8_tsm_script_order', __( 'Script Order', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_order_metabox' ), 'r8_tracking_scripts', 'side' ); 391 add_meta_box( 'r8_tsm_script_location', __( 'Script Location', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_location_metabox' ), 'r8_tracking_scripts', 'normal' ); 392 add_meta_box( 'r8_tsm_script_page', __( 'Specific Script Placement (Page(s) or Post(s))', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_page_metabox' ), 'r8_tracking_scripts', 'normal' ); 393 394 } 395 396 function script_code_metabox() { 397 398 global $post; 399 400 $script_code = get_post_meta( $post->ID, 'r8_tsm_script_code', true ); 401 402 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-code-metabox.php' ); 403 404 } 405 406 function script_active_metabox() { 407 408 global $post; 409 410 $active = get_post_meta( $post->ID, 'r8_tsm_active', true ); 411 412 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-active-metabox.php' ); 413 414 } 415 416 function script_expiry_metabox() { 417 global $post; 418 419 $expiry_info = get_post_meta( $post->ID, 'r8_tsm_script_expiry_info', true ); 420 $expiry_timestamp = is_object( $expiry_info ) ? $expiry_info->date : ''; 421 $expiry_date_type = is_object( $expiry_info ) ? $expiry_info->type : ''; 422 $expiry_date_num = is_object( $expiry_info ) ? $expiry_info->num : ''; 423 $expiry_date_block = is_object( $expiry_info ) ? $expiry_info->block : ''; 424 $expiry_date = $this->get_expiry_info( $expiry_date_type, $expiry_timestamp ); 425 $if_expire = $this->check_expiy_script( $expiry_timestamp ); 426 427 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-expiry-metabox.php' ); 428 429 } 430 431 function script_order_metabox() { 432 433 global $post; 434 435 $order = get_post_meta( $post->ID, 'r8_tsm_script_order', true ); 436 437 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-order-metabox.php' ); 438 439 } 440 441 function script_location_metabox() { 442 443 global $post; 444 445 $location = get_post_meta( $post->ID, 'r8_tsm_script_location', true ); 446 447 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-location-metabox.php' ); 448 449 } 450 451 function script_page_metabox() { 452 453 global $post; 454 455 $script_page = get_post_meta( $post->ID, 'r8_tsm_script_page', true ); 456 457 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-page-metabox.php' ); 458 459 } 460 461 public function get_expiry_info( $expire_date_type, $script_expiry_timestamp ) { 462 $show_date = 'Never'; 463 if ( $expire_date_type == 'never' ) { 464 $show_date = ucwords($expire_date_type); 465 } 466 467 if ( $expire_date_type == 'in' ) { 468 $expiry_timestamp = new DateTime($script_expiry_timestamp); 469 $show_date = $expiry_timestamp->format('M d, Y \a\t H:i'); 470 } 471 return $show_date; 472 } 473 474 public function set_expiry_info( $expire_date_type, $expire_date_num, $expire_date_block, $format = 'c' ) { 475 $show_date = 'Never'; 476 if ( $expire_date_type == 'never' ) { 477 $show_date = ucwords($expire_date_type); 478 } 479 480 if ( $expire_date_type == 'in' ) { 481 switch ($expire_date_block) { 482 case 'minutes': 483 $timespan = 'PT' . absint( $expire_date_num ) . 'M'; 484 break; 485 486 case 'weeks': 487 $timespan = 'P' . absint( $expire_date_num ) * 7 . 'D'; 488 break; 489 490 case 'months': 491 $timespan = 'P' . absint( $expire_date_num ) . 'M'; 492 break; 493 494 case 'years': 495 $timespan = 'P' . absint( $expire_date_num ) . 'Y'; 496 break; 497 498 default: // days 499 $timespan = 'PT' . absint( $expire_date_num ) * 24 . 'H'; 500 break; 501 } 502 $show_date = $this->get_date_time( $timespan, $format ); 503 } 504 return $show_date; 505 } 506 507 public function get_date_time( $timespan, $format ){ 508 $current_time = new DateTime(); 509 $current_time->add(new DateInterval( $timespan )); 510 $expire_time = $current_time->format($format); 511 return $expire_time; 512 } 513 514 public function check_expiy_script( $expiry_timespan ){ 515 if ( $expiry_timespan == 'Never' ) return false; 516 517 // current time 518 $current_time = new DateTime(); 519 $current_time = $current_time->format(DateTime::ATOM); 520 521 // expiry timie 522 $expire_time = new DateTime($expiry_timespan); 523 $expire_time = $expire_time->format(DateTime::ATOM); 524 // check expiry 525 if ( $current_time < $expire_time ){ 526 return false; 527 }else{ 528 $this->set_script_inactive(); 529 return true; 530 } 531 } 532 533 public function set_script_inactive() { 534 global $post; 535 if ( ! empty( $post->post_type ) ) { 536 if ( $post->post_type === 'r8_tracking_scripts' ) { 537 $active = get_post_meta( $post->ID, 'r8_tsm_active', true ); 538 if( 'active' === $active ){ 539 update_post_meta( $post->ID, 'r8_tsm_active', 'inactive' ); 540 } 541 } 542 } 543 } 544 545 public function register_scripts_post_type() { 546 547 $labels = array( 548 'name' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 549 'singular_name' => _x( 'Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 550 'menu_name' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 551 'name_admin_bar' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 552 'add_new' => _x( 'Add New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 553 'add_new_item' => __( 'Add New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 554 'new_item' => __( 'New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 555 'edit_item' => __( 'Edit Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 556 'view_item' => __( 'View Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 557 'all_items' => __( 'All Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 558 'search_items' => __( 'Search Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 559 'parent_item_colon' => __( 'Parent Tracking Scripts:', TRACKING_SCRIPT_TEXTDOMAIN ), 560 'not_found' => __( 'No Tracking Scripts found.', TRACKING_SCRIPT_TEXTDOMAIN ), 561 'not_found_in_trash' => __( 'No Tracking Scripts found in Trash.', TRACKING_SCRIPT_TEXTDOMAIN ) 562 ); 563 564 $args = array( 565 'labels' => $labels, 566 'description' => __( 'Description.', TRACKING_SCRIPT_TEXTDOMAIN ), 567 'public' => false, 568 'publicly_queryable' => false, 569 'show_ui' => true, 570 'show_in_menu' => false, 571 'query_var' => false, 572 'rewrite' => array( 'slug' => 'tracking-scripts' ), 573 'capability_type' => 'post', 574 'capabilities' => array( 575 'edit_post' => 'manage_options', 576 'read_post' => 'manage_options', 577 'delete_post' => 'manage_options', 578 'edit_posts' => 'manage_options', 579 'edit_others_posts' => 'manage_options', 580 'delete_posts' => 'manage_options', 581 'publish_posts' => 'manage_options', 582 'read_private_posts' => 'manage_options' 583 ), 584 'has_archive' => false, 585 'hierarchical' => false, 586 'menu_position' => null, 587 'supports' => array( 'title', 'script-code', 'script-active', 'script-location', 'script-order' ) 588 ); 589 590 register_post_type( 'r8_tracking_scripts', $args ); 591 592 } 593 594 /************************************************* 595 * Admin Area 596 **************************************************/ 597 598 function admin_enqueue_scripts($hook) { 599 global $post; 600 601 if ( $hook === 'post.php' || $hook === 'post-new.php' || $hook === 'edit.php' ) { 602 if ( ! empty( $post->post_type ) && ($post->post_type === 'r8_tracking_scripts') ) { 603 wp_enqueue_style( 'r8-tsm-edit-script', plugins_url('/css/tracking-script-edit.css', __FILE__ ) ); 604 wp_enqueue_style( 'r8-tsm-select2-css', plugins_url('/css/select2.min.css', __FILE__ ) ); 605 wp_enqueue_script( 'r8-tsm-select2-js', plugins_url( '/js/select2.min.js', __FILE__ ), array(), null, true ); 606 wp_enqueue_script( 'r8-tsm-post-edit-js', plugins_url( '/js/post-edit.js', __FILE__ ), array('jquery', 'r8-tsm-select2-js'), null, true ); 607 } 608 } 609 } 610 611 function save_post() { 612 613 global $post; 614 615 if ( ! empty( $post->post_type ) ) { 616 if ( $post->post_type === 'r8_tracking_scripts' ) { 617 $expiry_obj = new \stdClass(); 618 $expiry_obj->type = ''; 619 $expiry_obj->num = ''; 620 $expiry_obj->block = ''; 621 $expiry_obj->date = ''; 622 623 // if ( ! empty( $_POST['r8_tsm_script_code'] ) ) { 624 update_post_meta( $post->ID, 'r8_tsm_script_code', stripslashes(esc_textarea($_POST['r8_tsm_script_code'])) ); 625 // } 626 627 if ( ! empty( $_POST['r8_tsm_active'] ) ) { 628 update_post_meta( $post->ID, 'r8_tsm_active', sanitize_text_field( $_POST['r8_tsm_active'] ) ); 629 } 630 631 if ( ! empty( $_POST['r8_tsm_script_order'] ) ) { 632 update_post_meta( $post->ID, 'r8_tsm_script_order', intval( $_POST['r8_tsm_script_order'] ) ); 633 } 634 635 if ( ! empty( $_POST['r8_tsm_script_location'] ) ) { 636 update_post_meta( $post->ID, 'r8_tsm_script_location', sanitize_text_field( $_POST['r8_tsm_script_location'] ) ); 637 } 638 639 if ( ! empty( $_POST['r8_tsm_script_expiry'] ) || ( !empty($_POST['r8_expire_date_in_num']) && !empty($_POST['r8_expire_date_in_block']) ) ) { 640 641 $expiry_date = $this->set_expiry_info( $_POST['r8_tsm_script_expiry'], $_POST['r8_expire_date_in_num'], $_POST['r8_expire_date_in_block'] ); 642 $expiry_obj->type = $_POST['r8_tsm_script_expiry'] ?: ''; 643 $expiry_obj->num = $_POST['r8_expire_date_in_num'] ?: ''; 644 $expiry_obj->block = $_POST['r8_expire_date_in_block'] ?: ''; 645 $expiry_obj->date = $expiry_date ?: ''; 646 update_post_meta( $post->ID, 'r8_tsm_script_expiry_info', $expiry_obj ); 647 } 648 649 if ( ! empty( $_POST['r8_tsm_script_page'] ) && is_array( $_POST['r8_tsm_script_page'] ) ) { 650 update_post_meta( $post->ID, 'r8_tsm_script_page', $_POST['r8_tsm_script_page'] ); 651 } else { 652 update_post_meta( $post->ID, 'r8_tsm_script_page', array() ); 653 } 654 655 } 656 } 657 658 } 659 660 public function tracking_scripts_create_menu() { 661 add_menu_page( 'Tracking Script Manager', 'Tracking Script Manager', 'manage_options', 'edit.php?post_type=r8_tracking_scripts', null ); 662 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 ); 663 } 664 665 // Admin Scripts 666 public function tracking_scripts_admin_scripts() { 667 wp_enqueue_script('jquery'); 668 669 wp_enqueue_script( 'tracking_script_js', plugin_dir_url(__FILE__) . '/js/built.min.js', array(), '', true ); 670 wp_localize_script( 'tracking_script_js', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); 671 } 672 673 674 // Ajax Functions 675 public function tracking_scripts_posts_ajax_handler() { 676 $post_type = ($_POST['postType']) ? esc_attr($_POST['postType']) : 'post'; 677 678 $args = array( 679 'post_type' => $post_type, 680 'posts_per_page' => -1, 681 'orderby' => 'name', 682 'order' => 'ASC' 683 ); 684 685 ob_start(); 686 687 $query = new WP_Query($args); 688 echo '<option value="none" id="none">Choose '.ucwords($post_type).'</option>'; 689 while( $query->have_posts() ) : $query->the_post(); 690 echo '<option value="'.get_the_ID().'" id="'.get_the_ID().'">'.ucwords(get_the_title()).'</option>'; 228 ) 229 ); 230 $header_scripts = new WP_Query( $args ); 231 if ( $header_scripts->have_posts() ) { 232 while ( $header_scripts->have_posts() ) : $header_scripts->the_post(); 233 $page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); 234 $expiry_info = get_post_meta( get_the_ID(), 'r8_tsm_script_expiry_info', true ); 235 $this->print_tsm_scripts( get_the_ID(), $page, $current_id, $expiry_info ); 691 236 endwhile; 692 237 wp_reset_postdata(); 693 694 echo ob_get_clean(); 695 die(); 696 } 697 } 698 699 function tracking_scripts() { 700 701 // globals 702 global $tracking_scripts; 703 704 705 // initialize 706 if ( ! isset($tracking_scripts) ) { 707 $tracking_scripts = new Tracking_Scripts(); 708 $tracking_scripts->initialize(); 709 } 710 711 // return 712 return $tracking_scripts; 713 714 } 715 238 } 239 } 240 241 function find_page_tracking_codes() { 242 243 global $wp_query; 244 $current_id = $wp_query->post->ID; 245 $args = array( 246 'post_type' => 'r8_tracking_scripts', 247 'posts_per_page' => - 1, 248 'post_status' => 'publish', 249 'meta_key' => 'r8_tsm_script_order', 250 'orderby' => 'meta_value_num', 251 'order' => 'ASC', 252 'meta_query' => array( 253 'relation' => 'AND', 254 array( 255 'key' => 'r8_tsm_script_location', 256 'value' => 'page', 257 'compare' => '=' 258 ), 259 array( 260 'key' => 'r8_tsm_active', 261 'value' => 'active', 262 'compare' => '=' 263 ) 264 ) 265 ); 266 $page_scripts = new WP_Query( $args ); 267 if ( $page_scripts->have_posts() ) { 268 while ( $page_scripts->have_posts() ) : $page_scripts->the_post(); 269 $page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); 270 $expiry_info = get_post_meta( get_the_ID(), 'r8_tsm_script_expiry_info', true ); 271 $this->print_tsm_scripts( get_the_ID(), $page, $current_id, $expiry_info ); 272 endwhile; 273 wp_reset_postdata(); 274 } 275 } 276 277 function find_footer_tracking_codes() { 278 global $wp_query; 279 $current_id = $wp_query->post->ID; 280 $args = array( 281 'post_type' => 'r8_tracking_scripts', 282 'posts_per_page' => - 1, 283 'post_status' => 'publish', 284 'meta_key' => 'r8_tsm_script_order', 285 'orderby' => 'meta_value_num', 286 'order' => 'ASC', 287 'meta_query' => array( 288 'relation' => 'AND', 289 array( 290 'key' => 'r8_tsm_script_location', 291 'value' => 'footer', 292 'compare' => '=' 293 ), 294 array( 295 'key' => 'r8_tsm_active', 296 'value' => 'active', 297 'compare' => '=' 298 ) 299 ) 300 ); 301 $footer_scripts = new WP_Query( $args ); 302 if ( $footer_scripts->have_posts() ) { 303 while ( $footer_scripts->have_posts() ) : $footer_scripts->the_post(); 304 $page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); 305 $expiry_info = get_post_meta( get_the_ID(), 'r8_tsm_script_expiry_info', true ); 306 $this->print_tsm_scripts( get_the_ID(), $page, $current_id, $expiry_info ); 307 endwhile; 308 wp_reset_postdata(); 309 } 310 } 311 312 function add_tracking_script_columns( $columns ) { 313 314 $columns = array( 315 'cb' => '<input type="checkbox" />', 316 'title' => __( 'Script Title', TRACKING_SCRIPT_TEXTDOMAIN ), 317 'global' => __( 'Global', TRACKING_SCRIPT_TEXTDOMAIN ), 318 'location' => __( 'Location', TRACKING_SCRIPT_TEXTDOMAIN ), 319 'status' => __( 'Status', TRACKING_SCRIPT_TEXTDOMAIN ), 320 'expire' => __( 'Expires', TRACKING_SCRIPT_TEXTDOMAIN ), 321 ); 322 323 return $columns; 324 } 325 326 function tracking_script_column_content( $column_name, $post_ID ) { 327 328 if ( $column_name === 'status' ) { 329 $active = get_post_meta( $post_ID, 'r8_tsm_active', true ); 330 if ( $active === 'active' ) { 331 echo 'Active'; 332 } else { 333 echo 'Inactive'; 334 } 335 } 336 if ( $column_name === 'global' ) { 337 $global = get_post_meta( $post_ID, 'r8_tsm_script_page', true ); 338 if ( empty( $global ) ) { 339 echo ' ✓'; 340 } else { 341 echo ' ✗'; 342 } 343 } 344 if ( $column_name === 'location' ) { 345 $location = get_post_meta( $post_ID, 'r8_tsm_script_location', true ); 346 if ( $location ) { 347 echo ucwords( $location ); 348 } 349 } 350 if ( $column_name === 'expire' ) { 351 352 $expiry_info = get_post_meta( $post_ID, 'r8_tsm_script_expiry_info', true ); 353 $expiry_timestamp = is_object( $expiry_info ) ? $expiry_info->date : 'Never'; 354 $expiry_date_type = is_object( $expiry_info ) ? $expiry_info->type : ''; 355 $expiry_date = $this->get_expiry_info( $expiry_date_type, $expiry_timestamp ); 356 $if_expire = $this->check_expiry_script( $expiry_timestamp ); 357 if ( ( $expiry_date_type == 'in' ) && $if_expire ) { 358 echo '<span class="expired">Expired</span>'; 359 } else { 360 echo $expiry_date; 361 } 362 } 363 } 364 365 function tracking_scripts_column_sort( $columns ) { 366 367 $columns['global'] = 'global'; 368 $columns['location'] = 'location'; 369 $columns['status'] = 'status'; 370 $columns['expire'] = 'expire'; 371 372 return $columns; 373 } 374 375 public function add_script_metaboxes() { 376 377 add_meta_box( 'r8_tsm_script_code', __( 'Script Code', TRACKING_SCRIPT_TEXTDOMAIN ), array( 378 $this, 379 'script_code_metabox' 380 ), 'r8_tracking_scripts', 'normal' ); 381 add_meta_box( 'r8_tsm_script_active', __( 'Script Status', TRACKING_SCRIPT_TEXTDOMAIN ), array( 382 $this, 383 'script_active_metabox' 384 ), 'r8_tracking_scripts', 'side' ); 385 add_meta_box( 'r8_tsm_script_expiry', __( 'Script Expires', TRACKING_SCRIPT_TEXTDOMAIN ), array( 386 $this, 387 'script_expiry_metabox' 388 ), 'r8_tracking_scripts', 'side' ); 389 add_meta_box( 'r8_tsm_script_order', __( 'Script Order', TRACKING_SCRIPT_TEXTDOMAIN ), array( 390 $this, 391 'script_order_metabox' 392 ), 'r8_tracking_scripts', 'side' ); 393 add_meta_box( 'r8_tsm_script_location', __( 'Script Location', TRACKING_SCRIPT_TEXTDOMAIN ), array( 394 $this, 395 'script_location_metabox' 396 ), 'r8_tracking_scripts', 'normal' ); 397 add_meta_box( 'r8_tsm_script_page', __( 'Specific Script Placement (Page(s) or Post(s))', TRACKING_SCRIPT_TEXTDOMAIN ), array( 398 $this, 399 'script_page_metabox' 400 ), 'r8_tracking_scripts', 'normal' ); 401 } 402 403 function script_code_metabox() { 404 405 global $post; 406 $script_code = get_post_meta( $post->ID, 'r8_tsm_script_code', true ); 407 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-code-metabox.php' ); 408 } 409 410 function script_active_metabox() { 411 412 global $post; 413 $active = get_post_meta( $post->ID, 'r8_tsm_active', true ); 414 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-active-metabox.php' ); 415 } 416 417 function script_expiry_metabox() { 418 global $post; 419 $expiry_info = get_post_meta( $post->ID, 'r8_tsm_script_expiry_info', true ); 420 $expiry_timestamp = is_object( $expiry_info ) ? $expiry_info->date : 'Never'; 421 $expiry_date_type = is_object( $expiry_info ) ? $expiry_info->type : ''; 422 $expiry_date_num = is_object( $expiry_info ) ? $expiry_info->num : ''; 423 $expiry_date_block = is_object( $expiry_info ) ? $expiry_info->block : ''; 424 $expiry_date = $this->get_expiry_info( $expiry_date_type, $expiry_timestamp ); 425 $if_expire = $this->check_expiry_script( $expiry_timestamp ); 426 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-expiry-metabox.php' ); 427 } 428 429 function script_order_metabox() { 430 431 global $post; 432 $order = get_post_meta( $post->ID, 'r8_tsm_script_order', true ); 433 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-order-metabox.php' ); 434 } 435 436 function script_location_metabox() { 437 438 global $post; 439 $location = get_post_meta( $post->ID, 'r8_tsm_script_location', true ); 440 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-location-metabox.php' ); 441 } 442 443 function script_page_metabox() { 444 445 global $post; 446 $script_page = get_post_meta( $post->ID, 'r8_tsm_script_page', true ); 447 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-page-metabox.php' ); 448 } 449 450 public function get_expiry_info( $expire_date_type, $script_expiry_timestamp ) { 451 $show_date = 'Never'; 452 if ( $expire_date_type == 'never' ) { 453 $show_date = ucwords( $expire_date_type ); 454 } 455 if ( $expire_date_type == 'in' ) { 456 $expiry_timestamp = new DateTime( $script_expiry_timestamp ); 457 $show_date = $expiry_timestamp->format( 'M d, Y \a\t H:i' ); 458 } 459 460 return $show_date; 461 } 462 463 public function set_expiry_info( $expire_date_type, $expire_date_num, $expire_date_block, $format = 'c' ) { 464 $show_date = 'Never'; 465 if ( $expire_date_type == 'never' ) { 466 $show_date = ucwords( $expire_date_type ); 467 } 468 if ( $expire_date_type == 'in' ) { 469 switch ( $expire_date_block ) { 470 case 'minutes': 471 $timespan = 'PT' . absint( $expire_date_num ) . 'M'; 472 break; 473 case 'weeks': 474 $timespan = 'P' . absint( $expire_date_num ) * 7 . 'D'; 475 break; 476 case 'months': 477 $timespan = 'P' . absint( $expire_date_num ) . 'M'; 478 break; 479 case 'years': 480 $timespan = 'P' . absint( $expire_date_num ) . 'Y'; 481 break; 482 default: // days 483 $timespan = 'PT' . absint( $expire_date_num ) * 24 . 'H'; 484 break; 485 } 486 $show_date = $this->get_date_time( $timespan, $format ); 487 } 488 489 return $show_date; 490 } 491 492 public function get_date_time( $timespan, $format ) { 493 $current_time = new DateTime(); 494 $current_time->add( new DateInterval( $timespan ) ); 495 $expire_time = $current_time->format( $format ); 496 497 return $expire_time; 498 } 499 500 public function check_expiry_script( $expiry_timespan ) { 501 if ( $expiry_timespan == 'Never' ) { 502 return false; 503 } 504 $current_time = new DateTime(); 505 $expire_time = new DateTime( $expiry_timespan ); 506 // check expiry 507 if ( $current_time < $expire_time ) { 508 return false; 509 } else { 510 $this->set_script_inactive(); 511 512 return true; 513 } 514 } 515 516 public function set_script_inactive() { 517 global $post; 518 if ( ! empty( $post->post_type ) ) { 519 if ( $post->post_type === 'r8_tracking_scripts' ) { 520 $active = get_post_meta( $post->ID, 'r8_tsm_active', true ); 521 if ( 'active' === $active ) { 522 update_post_meta( $post->ID, 'r8_tsm_active', 'inactive' ); 523 } 524 } 525 } 526 } 527 528 public function register_scripts_post_type() { 529 530 $labels = array( 531 'name' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 532 'singular_name' => _x( 'Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 533 'menu_name' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 534 'name_admin_bar' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 535 'add_new' => _x( 'Add New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 536 'add_new_item' => __( 'Add New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 537 'new_item' => __( 'New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 538 'edit_item' => __( 'Edit Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 539 'view_item' => __( 'View Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 540 'all_items' => __( 'All Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 541 'search_items' => __( 'Search Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 542 'parent_item_colon' => __( 'Parent Tracking Scripts:', TRACKING_SCRIPT_TEXTDOMAIN ), 543 'not_found' => __( 'No Tracking Scripts found.', TRACKING_SCRIPT_TEXTDOMAIN ), 544 'not_found_in_trash' => __( 'No Tracking Scripts found in Trash.', TRACKING_SCRIPT_TEXTDOMAIN ) 545 ); 546 $args = array( 547 'labels' => $labels, 548 'description' => __( 'Description.', TRACKING_SCRIPT_TEXTDOMAIN ), 549 'public' => false, 550 'publicly_queryable' => false, 551 'show_ui' => true, 552 'show_in_menu' => false, 553 'query_var' => false, 554 'rewrite' => array( 'slug' => 'tracking-scripts' ), 555 'capability_type' => 'post', 556 'capabilities' => array( 557 'edit_post' => 'manage_options', 558 'read_post' => 'manage_options', 559 'delete_post' => 'manage_options', 560 'edit_posts' => 'manage_options', 561 'edit_others_posts' => 'manage_options', 562 'delete_posts' => 'manage_options', 563 'publish_posts' => 'manage_options', 564 'read_private_posts' => 'manage_options' 565 ), 566 'has_archive' => false, 567 'hierarchical' => false, 568 'menu_position' => null, 569 'supports' => array( 570 'title', 571 'script-code', 572 'script-active', 573 'script-location', 574 'script-order' 575 ) 576 ); 577 register_post_type( 'r8_tracking_scripts', $args ); 578 } 579 580 /************************************************* 581 * Admin Area 582 **************************************************/ 583 function admin_enqueue_scripts( $hook ) { 584 global $post; 585 if ( $hook === 'post.php' || $hook === 'post-new.php' ) { 586 if ( ! empty( $post->post_type ) && ( $post->post_type === 'r8_tracking_scripts' ) ) { 587 wp_enqueue_style( 'r8-tsm-edit-script', plugins_url( '/css/tracking-script-edit.css', __FILE__ ) ); 588 wp_enqueue_style( 'r8-tsm-select2-css', plugins_url( '/css/select2.min.css', __FILE__ ) ); 589 wp_enqueue_script( 'r8-tsm-select2-js', plugins_url( '/js/select2.min.js', __FILE__ ), array(), null, true ); 590 wp_enqueue_script( 'r8-tsm-post-edit-js', plugins_url( '/js/post-edit.js', __FILE__ ), array( 591 'jquery', 592 'r8-tsm-select2-js' 593 ), null, true ); 594 } 595 } 596 if ( $hook === 'post.php' || $hook === 'edit.php' ) { 597 if ( ! empty( $post->post_type ) && ( $post->post_type === 'r8_tracking_scripts' ) ) { 598 wp_enqueue_style( 'r8-tsm-post-list', plugins_url( '/css/post-list.css', __FILE__ ) ); 599 wp_enqueue_script( 'r8-tsm-post-list-js', plugins_url( '/js/post-list.js', __FILE__ ), array( 'jquery' ), null, true ); 600 } 601 } 602 } 603 604 function save_post() { 605 606 global $post; 607 if ( ! empty( $post->post_type ) ) { 608 if ( $post->post_type === 'r8_tracking_scripts' ) { 609 $expiry_obj = new \stdClass(); 610 $expiry_obj->type = ''; 611 $expiry_obj->num = ''; 612 $expiry_obj->block = ''; 613 $expiry_obj->date = ''; 614 // if ( ! empty( $_POST['r8_tsm_script_code'] ) ) { 615 update_post_meta( $post->ID, 'r8_tsm_script_code', stripslashes( esc_textarea( $_POST['r8_tsm_script_code'] ) ) ); 616 // } 617 if ( ! empty( $_POST['r8_tsm_active'] ) ) { 618 update_post_meta( $post->ID, 'r8_tsm_active', sanitize_text_field( $_POST['r8_tsm_active'] ) ); 619 } 620 if ( ! empty( $_POST['r8_tsm_script_order'] ) ) { 621 update_post_meta( $post->ID, 'r8_tsm_script_order', intval( $_POST['r8_tsm_script_order'] ) ); 622 } 623 if ( ! empty( $_POST['r8_tsm_script_location'] ) ) { 624 update_post_meta( $post->ID, 'r8_tsm_script_location', sanitize_text_field( $_POST['r8_tsm_script_location'] ) ); 625 } 626 if ( ! empty( $_POST['r8_tsm_script_expiry'] ) || ( ! empty( $_POST['r8_expire_date_in_num'] ) && ! empty( $_POST['r8_expire_date_in_block'] ) ) ) { 627 628 $expiry_date = $this->set_expiry_info( $_POST['r8_tsm_script_expiry'], $_POST['r8_expire_date_in_num'], $_POST['r8_expire_date_in_block'] ); 629 $expiry_obj->type = $_POST['r8_tsm_script_expiry'] ?: ''; 630 $expiry_obj->num = $_POST['r8_expire_date_in_num'] ?: ''; 631 $expiry_obj->block = $_POST['r8_expire_date_in_block'] ?: ''; 632 $expiry_obj->date = $expiry_date ?: ''; 633 update_post_meta( $post->ID, 'r8_tsm_script_expiry_info', $expiry_obj ); 634 } 635 if ( ! empty( $_POST['r8_tsm_script_page'] ) && is_array( $_POST['r8_tsm_script_page'] ) ) { 636 update_post_meta( $post->ID, 'r8_tsm_script_page', $_POST['r8_tsm_script_page'] ); 637 } else { 638 update_post_meta( $post->ID, 'r8_tsm_script_page', array() ); 639 } 640 } 641 } 642 } 643 644 public function tracking_scripts_create_menu() { 645 add_menu_page( 'Tracking Script Manager', 'Tracking Script Manager', 'manage_options', 'edit.php?post_type=r8_tracking_scripts', null ); 646 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 ); 647 } 648 649 // Admin Scripts 650 public function tracking_scripts_admin_scripts() { 651 wp_enqueue_script( 'jquery' ); 652 wp_enqueue_script( 'tracking_script_js', plugin_dir_url( __FILE__ ) . '/js/built.min.js', array(), '', true ); 653 wp_localize_script( 'tracking_script_js', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); 654 } 655 656 // Ajax Functions 657 public function tracking_scripts_posts_ajax_handler() { 658 $post_type = ( $_POST['postType'] ) ? esc_attr( $_POST['postType'] ) : 'post'; 659 $args = array( 660 'post_type' => $post_type, 661 'posts_per_page' => - 1, 662 'orderby' => 'name', 663 'order' => 'ASC' 664 ); 665 ob_start(); 666 $query = new WP_Query( $args ); 667 echo '<option value="none" id="none">Choose ' . ucwords( $post_type ) . '</option>'; 668 while ( $query->have_posts() ) : $query->the_post(); 669 echo '<option value="' . get_the_ID() . '" id="' . get_the_ID() . '">' . ucwords( get_the_title() ) . '</option>'; 670 endwhile; 671 wp_reset_postdata(); 672 echo ob_get_clean(); 673 die(); 674 } 675 } 676 677 function tracking_scripts() { 678 679 // globals 680 global $tracking_scripts; 716 681 // initialize 717 tracking_scripts(); 682 if ( ! isset( $tracking_scripts ) ) { 683 $tracking_scripts = new Tracking_Scripts(); 684 $tracking_scripts->initialize(); 685 } 686 687 // return 688 return $tracking_scripts; 718 689 } 719 690 720 if ( ! class_exists('Tracking_Script') ) { 721 722 class Tracking_Script { 723 public $script_name; 724 public $script_code; 725 public $active; 726 public $order; 727 public $page_id; 728 public $location; 729 public $script_id; 730 731 function __construct() { 732 733 } 734 } 735 691 // initialize 692 tracking_scripts(); 693 } 694 if ( ! class_exists( 'Tracking_Script' ) ) { 695 696 class Tracking_Script { 697 public $script_name; 698 public $script_code; 699 public $active; 700 public $order; 701 public $page_id; 702 public $location; 703 public $script_id; 704 705 function __construct() { 706 707 } 736 708 } 709 } -
tracking-script-manager/trunk/js/post-edit.js
r2665551 r2667452 10 10 } 11 11 }); 12 13 // added class to expired row 14 $('#the-list tr td:last-child span.expired').parent().siblings().addClass('expired_inactive'); 15 12 16 13 // specific script element 17 14 $('.r8_tsm_page_select').select2({ -
tracking-script-manager/trunk/readme.txt
r2665551 r2667452 5 5 Requires at least: 4.0 6 6 Tested up to: 5.8 7 Stable tag: 2.0. 67 Stable tag: 2.0.7 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 55 55 56 56 == Changelog == 57 58 = 2.0.7 = 59 * Fixed a bug that caused scripts to be deactivated 60 * Fixed a JS bug in admin views 61 * Admin notice added 57 62 58 63 = 2.0.6 = -
tracking-script-manager/trunk/tracking-scripts-manager.php
r2665551 r2667452 1 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: 2.0.6 7 * Author: Red8 Interactive 8 * Author URI: http://red8interactive.com 9 * License: GPLv2 or later 10 */ 11 12 /* 13 Copyright 2019 Red8 Interactive (email : [email protected]) 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 /** 39 * @var TSM_Process_Tracking_Scripts 40 */ 41 protected $process_all; 42 43 function __construct() {} 44 45 public function initialize() { 46 47 // Constants 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 53 // Actions 54 add_action( 'init', array( $this, 'register_scripts_post_type' ) ); 55 add_action( 'save_post', array( $this, 'save_post' ) ); 56 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); 57 add_action( 'wp_head', array( $this, 'find_header_tracking_codes' ), 10 ); 58 add_action( 'wp_footer', array( $this, 'find_footer_tracking_codes'), 10 ); 59 add_action( 'admin_menu', array( $this, 'tracking_scripts_create_menu') ); 60 add_action( 'add_meta_boxes', array( $this, 'add_script_metaboxes' ) ); 61 add_action( 'wp_ajax_tracking_scripts_get_posts', array( $this, 'tracking_scripts_posts_ajax_handler' ) ); 62 add_action( 'manage_r8_tracking_scripts_posts_custom_column', array( $this, 'tracking_script_column_content' ), 10, 2 ); 63 add_action( 'wp_body_open', array( $this, 'find_page_tracking_codes' ) ); 64 add_action( 'tsm_page_scripts', array( $this, 'find_page_tracking_codes' ) ); 65 add_action( 'admin_init', array( $this, 'process_handler' ) ); 66 add_action( 'admin_notices', array( $this, 'admin_notices' ) ); 67 68 // fallback for page scripts if wp_body_open action isn't supported 69 add_action( 'get_footer', function() { 70 if ( did_action( 'wp_body_open' ) === 0 ) { 71 add_action( 'wp_footer', array( $this, 'find_page_tracking_codes' ) ); 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: 2.0.7 7 * Author: Red8 Interactive 8 * Author URI: http://red8interactive.com 9 * License: GPLv2 or later 10 */ 11 /* 12 Copyright 2019 Red8 Interactive (email : [email protected]) 13 14 This program is free software; you can redistribute it and/or 15 modify it under the terms of the GNU General Public License 16 as published by the Free Software Foundation; either version 2 17 of the License, or (at your option) any later version. 18 19 This program is distributed in the hope that it will be useful, 20 but WITHOUT ANY WARRANTY; without even the implied warranty of 21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 GNU General Public License for more details. 23 24 You should have received a copy of the GNU General Public License 25 along with this program; if not, write to the Free Software 26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 27 */ 28 if ( ! defined( 'ABSPATH' ) ) { 29 exit; // Exit if accessed directly 30 } 31 if ( ! class_exists( 'Tracking_Scripts' ) ) { 32 33 class Tracking_Scripts { 34 /** 35 * @var TSM_Process_Tracking_Scripts 36 */ 37 protected $process_all; 38 39 function __construct() { 40 } 41 42 public function initialize() { 43 44 // Constants 45 define( 'TRACKING_SCRIPT_PATH', plugins_url( ' ', __FILE__ ) ); 46 define( 'TRACKING_SCRIPT_BASENAME', plugin_basename( __FILE__ ) ); 47 define( 'TRACKING_SCRIPT_DIR_PATH', plugin_dir_path( __FILE__ ) ); 48 define( 'TRACKING_SCRIPT_TEXTDOMAIN', 'tracking-scripts-manager' ); 49 // Actions 50 add_action( 'init', array( $this, 'register_scripts_post_type' ) ); 51 add_action( 'save_post', array( $this, 'save_post' ) ); 52 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); 53 add_action( 'wp_head', array( $this, 'find_header_tracking_codes' ), 10 ); 54 add_action( 'wp_footer', array( $this, 'find_footer_tracking_codes' ), 10 ); 55 add_action( 'admin_menu', array( $this, 'tracking_scripts_create_menu' ) ); 56 add_action( 'add_meta_boxes', array( $this, 'add_script_metaboxes' ) ); 57 add_action( 'wp_ajax_tracking_scripts_get_posts', array( $this, 'tracking_scripts_posts_ajax_handler' ) ); 58 add_action( 'manage_r8_tracking_scripts_posts_custom_column', array( 59 $this, 60 'tracking_script_column_content' 61 ), 10, 2 ); 62 add_action( 'wp_body_open', array( $this, 'find_page_tracking_codes' ) ); 63 add_action( 'tsm_page_scripts', array( $this, 'find_page_tracking_codes' ) ); 64 add_action( 'admin_init', array( $this, 'process_handler' ) ); 65 add_action( 'admin_notices', array( $this, 'admin_notices' ) ); 66 add_action( 'admin_notices', array( $this, 'new_update_admin_notice' ) ); 67 add_action( 'admin_init', array( $this, "init_update_admin_notice" ) ); 68 // fallback for page scripts if wp_body_open action isn't supported 69 add_action( 'get_footer', function () { 70 if ( did_action( 'wp_body_open' ) === 0 ) { 71 add_action( 'wp_footer', array( $this, 'find_page_tracking_codes' ) ); 72 } 73 } ); 74 // Filters 75 add_filter( 'manage_r8_tracking_scripts_posts_columns', array( $this, 'add_tracking_script_columns' ) ); 76 add_filter( 'manage_edit-r8_tracking_scripts_sortable_columns', array( 77 $this, 78 'tracking_scripts_column_sort' 79 ) ); 80 // Includes 81 require_once plugin_dir_path( __FILE__ ) . 'classes/wp-async-request.php'; 82 require_once plugin_dir_path( __FILE__ ) . 'classes/wp-background-process.php'; 83 require_once plugin_dir_path( __FILE__ ) . 'classes/class-process-tracking-scripts.php'; 84 $this->process_all = new TSM_Process_Tracking_Scripts(); 85 } 86 87 /************************************************* 88 * Front End 89 **************************************************/ 90 public function process_handler() { 91 92 if ( ! isset( $_GET['tsm_update_scripts'] ) || ! isset( $_GET['_wpnonce'] ) ) { 93 return; 94 } 95 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'tsm_update_scripts' ) ) { 96 return; 97 } 98 if ( 'true' === $_GET['tsm_update_scripts'] ) { 99 update_option( 'tsm_is_processing', true ); 100 $this->handle_all(); 101 } 102 } 103 104 protected function handle_all() { 105 $scripts = $this->get_tracking_scripts(); 106 if ( ! empty( $scripts ) ) { 107 108 foreach ( $scripts as $script ) { 109 $this->process_all->push_to_queue( $script ); 110 } 111 $this->process_all->save()->dispatch(); 112 } 113 } 114 115 protected function get_tracking_scripts() { 116 117 $scripts = array(); 118 $header_scripts = get_option( 'header_tracking_script_code' ) ? unserialize( get_option( 'header_tracking_script_code' ) ) : null; 119 $page_scripts = get_option( 'page_tracking_script_code' ) ? unserialize( get_option( 'page_tracking_script_code' ) ) : null; 120 $footer_scripts = get_option( 'footer_tracking_script_code' ) ? unserialize( get_option( 'footer_tracking_script_code' ) ) : null; 121 if ( ! empty( $header_scripts ) ) { 122 $scripts = array_merge( $scripts, $header_scripts ); 123 } 124 if ( ! empty( $page_scripts ) ) { 125 $scripts = array_merge( $scripts, $page_scripts ); 126 } 127 if ( ! empty( $footer_scripts ) ) { 128 $scripts = array_merge( $scripts, $footer_scripts ); 129 } 130 131 return $scripts; 132 } 133 134 function admin_notices() { 135 $class = 'notice notice-info is-dismissible'; 136 $header_scripts = get_option( 'header_tracking_script_code' ); 137 $page_scripts = get_option( 'page_tracking_script_code' ); 138 $footer_scripts = get_option( 'footer_tracking_script_code' ); 139 $is_processing = get_option( 'tsm_is_processing' ); 140 $has_tracking_scripts = ( $header_scripts || $page_scripts || $footer_scripts ) ? true : false; 141 $is_admin = current_user_can( 'manage_options' ) ? true : false; 142 if ( $has_tracking_scripts && $is_processing && $is_admin ) { 143 $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 ); 144 $notice = sprintf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); 145 echo $notice; 146 } 147 if ( $has_tracking_scripts && ! $is_processing && $is_admin ) { 148 $url = wp_nonce_url( admin_url( 'edit.php?post_type=r8_tracking_scripts&tsm_update_scripts=true&tsm_is_processing=true' ), 'tsm_update_scripts' ); 149 $message = __( 'Tracking Scripts Manager has updated to a new version, click OK to update your scripts to the updated version.', TRACKING_SCRIPT_TEXTDOMAIN ); 150 $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 ) ); 151 echo $notice; 152 } 153 } 154 155 /** 156 * Used to track if the notice has been dismissed 157 */ 158 public function init_update_admin_notice() { 159 160 $user_id = get_current_user_id(); 161 if ( isset( $_GET['tsm_update_notice_dismissed'] ) ) { 162 add_user_meta( $user_id, 'tsm_update_notice_dismissed', true, true ); 163 } 164 } 165 166 /** 167 * A one-time admin notice to the plugin asking site administrators to review their 168 * Tracking Scripts statuses and reactivate any scripts that have been mistakenly deactivated. 169 */ 170 public function new_update_admin_notice() { 171 $user_id = get_current_user_id(); 172 if ( ! get_user_meta( $user_id, 'tsm_update_notice_dismissed' ) ) { 173 $url=admin_url()."edit.php?post_type=r8_tracking_scripts&tsm_update_notice_dismissed"; 174 ?> 175 <div class="notice notice-success "> 176 <p><?php _e( 'An error in Tracking Scripts Manager 2.0.6 may have deactivated your scripts. Please click <a href="'.$url.'">here</a> to review the status of your scripts and reactivate them as needed.', '' ); ?></p> 177 <a href="?tsm_update_notice_dismissed">Dismiss this notice</a> 178 </div> 179 <?php 180 } 181 } 182 183 public function print_tsm_scripts( $page_id, $page, $current_id, $expiry_info ) { 184 $expiry_timestamp = is_object( $expiry_info ) ? $expiry_info->date : 'Never'; 185 $expiry_date_type = is_object( $expiry_info ) ? $expiry_info->type : ''; 186 $if_expire = $this->check_expiry_script( $expiry_timestamp ); 187 if ( $expiry_date_type == 'in' ) { 188 if ( ! $if_expire ) { 189 if ( is_array( $page ) && in_array( intval( $current_id ), $page ) ) { 190 echo html_entity_decode( get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252' ); 191 } elseif ( empty( $page ) ) { 192 echo html_entity_decode( get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252' ); 72 193 } 73 } ); 74 75 // Filters 76 add_filter( 'manage_r8_tracking_scripts_posts_columns', array( $this, 'add_tracking_script_columns' ) ); 77 add_filter( 'manage_edit-r8_tracking_scripts_sortable_columns', array( $this, 'tracking_scripts_column_sort' ) ); 78 79 // Includes 80 require_once plugin_dir_path( __FILE__ ) . 'classes/wp-async-request.php'; 81 require_once plugin_dir_path( __FILE__ ) . 'classes/wp-background-process.php'; 82 require_once plugin_dir_path( __FILE__ ) . 'classes/class-process-tracking-scripts.php'; 83 84 $this->process_all = new TSM_Process_Tracking_Scripts(); 85 86 } 87 88 /************************************************* 89 * Front End 90 **************************************************/ 91 92 public function process_handler() { 93 94 if ( ! isset( $_GET['tsm_update_scripts'] ) || ! isset( $_GET['_wpnonce'] ) ) { 95 return; 96 } 97 98 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'tsm_update_scripts' ) ) { 99 return; 100 } 101 102 if ( 'true' === $_GET['tsm_update_scripts'] ) { 103 update_option( 'tsm_is_processing', true ); 104 $this->handle_all(); 105 } 106 107 } 108 109 protected function handle_all() { 110 $scripts = $this->get_tracking_scripts(); 111 112 if ( ! empty( $scripts ) ) { 113 114 foreach ( $scripts as $script ) { 115 $this->process_all->push_to_queue( $script ); 116 } 117 118 $this->process_all->save()->dispatch(); 119 120 } 121 } 122 123 protected function get_tracking_scripts() { 124 125 $scripts = array(); 126 $header_scripts = get_option( 'header_tracking_script_code' ) ? unserialize( get_option('header_tracking_script_code') ) : null; 127 $page_scripts = get_option( 'page_tracking_script_code' ) ? unserialize( get_option('page_tracking_script_code') ) : null; 128 $footer_scripts = get_option( 'footer_tracking_script_code' ) ? unserialize( get_option('footer_tracking_script_code') ) : null; 129 130 if ( ! empty( $header_scripts ) ) { 131 $scripts = array_merge( $scripts, $header_scripts ); 132 } 133 134 if ( ! empty( $page_scripts ) ) { 135 $scripts = array_merge( $scripts, $page_scripts ); 136 } 137 138 if ( ! empty( $footer_scripts ) ) { 139 $scripts = array_merge( $scripts, $footer_scripts ); 140 } 141 142 return $scripts; 143 144 } 145 146 function admin_notices() { 147 $class = 'notice notice-info is-dismissible'; 148 $header_scripts = get_option('header_tracking_script_code'); 149 $page_scripts = get_option('page_tracking_script_code'); 150 $footer_scripts = get_option('footer_tracking_script_code'); 151 $is_processing = get_option('tsm_is_processing'); 152 $has_tracking_scripts = ( $header_scripts || $page_scripts || $footer_scripts ) ? true : false; 153 $is_admin = current_user_can('manage_options') ? true : false; 154 155 if ( $has_tracking_scripts && $is_processing && $is_admin ) { 156 $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 ); 157 $notice = sprintf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); 158 echo $notice; 159 } 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 $notice; 166 } 167 } 168 169 public function print_tsm_scripts( $page_id, $page, $current_id, $expiry_info ) { 170 $expiry_timestamp = is_object( $expiry_info ) ? $expiry_info->date : ''; 171 $expiry_date_type = is_object( $expiry_info ) ? $expiry_info->type : ''; 172 $if_expire = $this->check_expiy_script( $expiry_timestamp ); 173 174 if( $expiry_date_type == 'in' ){ 175 if( !$if_expire ){ 176 if ( is_array( $page ) && in_array(intval($current_id), $page) ) { 177 echo html_entity_decode(get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252'); 178 } elseif ( empty( $page ) ) { 179 echo html_entity_decode(get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252'); 180 } 181 } 182 }else{ 183 if ( is_array( $page ) && in_array(intval($current_id), $page) ) { 184 echo html_entity_decode(get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252'); 185 } elseif ( empty( $page ) ) { 186 echo html_entity_decode(get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252'); 187 } 188 } 189 } 190 191 // Header Tracking Codes 192 function find_header_tracking_codes() { 193 194 global $wp_query; 195 $current_id = $wp_query->post->ID; 196 197 $args = array( 198 'post_type' => 'r8_tracking_scripts', 199 'post_status' => 'publish', 200 'posts_per_page' => -1, 201 'meta_key' => 'r8_tsm_script_order', 202 'orderby' => 'meta_value_num', 203 'order' => 'ASC', 204 'meta_query' => array( 205 'relation' => 'AND', 206 array( 207 'key' => 'r8_tsm_script_location', 208 'value' => 'header', 209 'compare' => '=' 210 ), 211 array( 212 'key' => 'r8_tsm_active', 213 'value' => 'active', 214 'compare' => '=' 215 ) 194 } 195 } else { 196 if ( is_array( $page ) && in_array( intval( $current_id ), $page ) ) { 197 echo html_entity_decode( get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252' ); 198 } elseif ( empty( $page ) ) { 199 echo html_entity_decode( get_post_meta( $page_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252' ); 200 } 201 } 202 } 203 204 // Header Tracking Codes 205 function find_header_tracking_codes() { 206 207 global $wp_query; 208 $current_id = $wp_query->post->ID; 209 $args = array( 210 'post_type' => 'r8_tracking_scripts', 211 'post_status' => 'publish', 212 'posts_per_page' => - 1, 213 'meta_key' => 'r8_tsm_script_order', 214 'orderby' => 'meta_value_num', 215 'order' => 'ASC', 216 'meta_query' => array( 217 'relation' => 'AND', 218 array( 219 'key' => 'r8_tsm_script_location', 220 'value' => 'header', 221 'compare' => '=' 222 ), 223 array( 224 'key' => 'r8_tsm_active', 225 'value' => 'active', 226 'compare' => '=' 216 227 ) 217 ); 218 219 $header_scripts = new WP_Query($args); 220 221 if ( $header_scripts->have_posts() ) { 222 while ( $header_scripts->have_posts() ) : $header_scripts->the_post(); 223 $page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); 224 $expiry_info = get_post_meta( get_the_ID(), 'r8_tsm_script_expiry_info', true ); 225 226 $this->print_tsm_scripts( get_the_ID(), $page, $current_id, $expiry_info ); 227 228 endwhile; wp_reset_postdata(); 229 } 230 } 231 232 233 function find_page_tracking_codes() { 234 235 global $wp_query; 236 $current_id = $wp_query->post->ID; 237 238 $args = array( 239 'post_type' => 'r8_tracking_scripts', 240 'posts_per_page' => -1, 241 'post_status' => 'publish', 242 'meta_key' => 'r8_tsm_script_order', 243 'orderby' => 'meta_value_num', 244 'order' => 'ASC', 245 'meta_query' => array( 246 'relation' => 'AND', 247 array( 248 'key' => 'r8_tsm_script_location', 249 'value' => 'page', 250 'compare' => '=' 251 ), 252 array( 253 'key' => 'r8_tsm_active', 254 'value' => 'active', 255 'compare' => '=' 256 ) 257 ) 258 ); 259 260 $page_scripts = new WP_Query($args); 261 262 if ( $page_scripts->have_posts() ) { 263 while ( $page_scripts->have_posts() ) : $page_scripts->the_post(); 264 $page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); 265 $expiry_info = get_post_meta( get_the_ID(), 'r8_tsm_script_expiry_info', true ); 266 267 $this->print_tsm_scripts( get_the_ID(), $page, $current_id, $expiry_info ); 268 269 endwhile; wp_reset_postdata(); 270 } 271 272 } 273 274 function find_footer_tracking_codes() { 275 global $wp_query; 276 $current_id = $wp_query->post->ID; 277 278 $args = array( 279 'post_type' => 'r8_tracking_scripts', 280 'posts_per_page' => -1, 281 'post_status' => 'publish', 282 'meta_key' => 'r8_tsm_script_order', 283 'orderby' => 'meta_value_num', 284 'order' => 'ASC', 285 'meta_query' => array( 286 'relation' => 'AND', 287 array( 288 'key' => 'r8_tsm_script_location', 289 'value' => 'footer', 290 'compare' => '=' 291 ), 292 array( 293 'key' => 'r8_tsm_active', 294 'value' => 'active', 295 'compare' => '=' 296 ) 297 ) 298 ); 299 300 $footer_scripts = new WP_Query($args); 301 302 if ( $footer_scripts->have_posts() ) { 303 while ( $footer_scripts->have_posts() ) : $footer_scripts->the_post(); 304 $page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); 305 $expiry_info = get_post_meta( get_the_ID(), 'r8_tsm_script_expiry_info', true ); 306 307 $this->print_tsm_scripts( get_the_ID(), $page, $current_id, $expiry_info ); 308 309 endwhile; wp_reset_postdata(); 310 } 311 } 312 313 function add_tracking_script_columns($columns) { 314 315 $columns = array( 316 'cb' => '<input type="checkbox" />', 317 'title' => __( 'Script Title', TRACKING_SCRIPT_TEXTDOMAIN ), 318 'global' => __( 'Global', TRACKING_SCRIPT_TEXTDOMAIN ), 319 'location' => __( 'Location', TRACKING_SCRIPT_TEXTDOMAIN ), 320 'status' => __( 'Status', TRACKING_SCRIPT_TEXTDOMAIN ), 321 'expire' => __( 'Expires', TRACKING_SCRIPT_TEXTDOMAIN ), 322 ); 323 324 return $columns; 325 326 } 327 328 function tracking_script_column_content($column_name, $post_ID) { 329 330 if ( $column_name === 'status' ) { 331 $active = get_post_meta( $post_ID, 'r8_tsm_active', true ); 332 if ( $active === 'active' ) { 333 echo 'Active'; 334 } else { 335 echo 'Inactive'; 336 } 337 } 338 339 if ( $column_name === 'global' ) { 340 $global = get_post_meta( $post_ID, 'r8_tsm_script_page', true ); 341 342 if ( empty($global) ) { 343 echo ' ✓'; 344 } else { 345 echo ' ✗'; 346 } 347 } 348 349 if ( $column_name === 'location' ) { 350 $location = get_post_meta( $post_ID, 'r8_tsm_script_location', true ); 351 352 if ( $location ) { 353 echo ucwords($location); 354 } 355 } 356 357 if ( $column_name === 'expire' ) { 358 359 $expiry_info = get_post_meta( $post_ID, 'r8_tsm_script_expiry_info', true ); 360 $expiry_timestamp = is_object( $expiry_info ) ? $expiry_info->date : ''; 361 $expiry_date_type = is_object( $expiry_info ) ? $expiry_info->type : ''; 362 $expiry_date = $this->get_expiry_info( $expiry_date_type, $expiry_timestamp ); 363 $if_expire = $this->check_expiy_script( $expiry_timestamp ); 364 365 if( ( $expiry_date_type == 'in' ) && $if_expire ){ 366 echo '<span class="expired">Expired</span>'; 367 }else{ 368 echo $expiry_date; 369 } 370 } 371 372 } 373 374 function tracking_scripts_column_sort($columns) { 375 376 $columns['global'] = 'global'; 377 $columns['location'] = 'location'; 378 $columns['status'] = 'status'; 379 $columns['expire'] = 'expire'; 380 381 return $columns; 382 383 } 384 385 public function add_script_metaboxes() { 386 387 add_meta_box( 'r8_tsm_script_code', __( 'Script Code', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_code_metabox' ), 'r8_tracking_scripts', 'normal' ); 388 add_meta_box( 'r8_tsm_script_active', __( 'Script Status', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_active_metabox' ), 'r8_tracking_scripts', 'side' ); 389 add_meta_box( 'r8_tsm_script_expiry', __( 'Script Expires', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_expiry_metabox' ), 'r8_tracking_scripts', 'side' ); 390 add_meta_box( 'r8_tsm_script_order', __( 'Script Order', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_order_metabox' ), 'r8_tracking_scripts', 'side' ); 391 add_meta_box( 'r8_tsm_script_location', __( 'Script Location', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_location_metabox' ), 'r8_tracking_scripts', 'normal' ); 392 add_meta_box( 'r8_tsm_script_page', __( 'Specific Script Placement (Page(s) or Post(s))', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_page_metabox' ), 'r8_tracking_scripts', 'normal' ); 393 394 } 395 396 function script_code_metabox() { 397 398 global $post; 399 400 $script_code = get_post_meta( $post->ID, 'r8_tsm_script_code', true ); 401 402 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-code-metabox.php' ); 403 404 } 405 406 function script_active_metabox() { 407 408 global $post; 409 410 $active = get_post_meta( $post->ID, 'r8_tsm_active', true ); 411 412 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-active-metabox.php' ); 413 414 } 415 416 function script_expiry_metabox() { 417 global $post; 418 419 $expiry_info = get_post_meta( $post->ID, 'r8_tsm_script_expiry_info', true ); 420 $expiry_timestamp = is_object( $expiry_info ) ? $expiry_info->date : ''; 421 $expiry_date_type = is_object( $expiry_info ) ? $expiry_info->type : ''; 422 $expiry_date_num = is_object( $expiry_info ) ? $expiry_info->num : ''; 423 $expiry_date_block = is_object( $expiry_info ) ? $expiry_info->block : ''; 424 $expiry_date = $this->get_expiry_info( $expiry_date_type, $expiry_timestamp ); 425 $if_expire = $this->check_expiy_script( $expiry_timestamp ); 426 427 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-expiry-metabox.php' ); 428 429 } 430 431 function script_order_metabox() { 432 433 global $post; 434 435 $order = get_post_meta( $post->ID, 'r8_tsm_script_order', true ); 436 437 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-order-metabox.php' ); 438 439 } 440 441 function script_location_metabox() { 442 443 global $post; 444 445 $location = get_post_meta( $post->ID, 'r8_tsm_script_location', true ); 446 447 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-location-metabox.php' ); 448 449 } 450 451 function script_page_metabox() { 452 453 global $post; 454 455 $script_page = get_post_meta( $post->ID, 'r8_tsm_script_page', true ); 456 457 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-page-metabox.php' ); 458 459 } 460 461 public function get_expiry_info( $expire_date_type, $script_expiry_timestamp ) { 462 $show_date = 'Never'; 463 if ( $expire_date_type == 'never' ) { 464 $show_date = ucwords($expire_date_type); 465 } 466 467 if ( $expire_date_type == 'in' ) { 468 $expiry_timestamp = new DateTime($script_expiry_timestamp); 469 $show_date = $expiry_timestamp->format('M d, Y \a\t H:i'); 470 } 471 return $show_date; 472 } 473 474 public function set_expiry_info( $expire_date_type, $expire_date_num, $expire_date_block, $format = 'c' ) { 475 $show_date = 'Never'; 476 if ( $expire_date_type == 'never' ) { 477 $show_date = ucwords($expire_date_type); 478 } 479 480 if ( $expire_date_type == 'in' ) { 481 switch ($expire_date_block) { 482 case 'minutes': 483 $timespan = 'PT' . absint( $expire_date_num ) . 'M'; 484 break; 485 486 case 'weeks': 487 $timespan = 'P' . absint( $expire_date_num ) * 7 . 'D'; 488 break; 489 490 case 'months': 491 $timespan = 'P' . absint( $expire_date_num ) . 'M'; 492 break; 493 494 case 'years': 495 $timespan = 'P' . absint( $expire_date_num ) . 'Y'; 496 break; 497 498 default: // days 499 $timespan = 'PT' . absint( $expire_date_num ) * 24 . 'H'; 500 break; 501 } 502 $show_date = $this->get_date_time( $timespan, $format ); 503 } 504 return $show_date; 505 } 506 507 public function get_date_time( $timespan, $format ){ 508 $current_time = new DateTime(); 509 $current_time->add(new DateInterval( $timespan )); 510 $expire_time = $current_time->format($format); 511 return $expire_time; 512 } 513 514 public function check_expiy_script( $expiry_timespan ){ 515 if ( $expiry_timespan == 'Never' ) return false; 516 517 // current time 518 $current_time = new DateTime(); 519 $current_time = $current_time->format(DateTime::ATOM); 520 521 // expiry timie 522 $expire_time = new DateTime($expiry_timespan); 523 $expire_time = $expire_time->format(DateTime::ATOM); 524 // check expiry 525 if ( $current_time < $expire_time ){ 526 return false; 527 }else{ 528 $this->set_script_inactive(); 529 return true; 530 } 531 } 532 533 public function set_script_inactive() { 534 global $post; 535 if ( ! empty( $post->post_type ) ) { 536 if ( $post->post_type === 'r8_tracking_scripts' ) { 537 $active = get_post_meta( $post->ID, 'r8_tsm_active', true ); 538 if( 'active' === $active ){ 539 update_post_meta( $post->ID, 'r8_tsm_active', 'inactive' ); 540 } 541 } 542 } 543 } 544 545 public function register_scripts_post_type() { 546 547 $labels = array( 548 'name' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 549 'singular_name' => _x( 'Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 550 'menu_name' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 551 'name_admin_bar' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 552 'add_new' => _x( 'Add New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 553 'add_new_item' => __( 'Add New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 554 'new_item' => __( 'New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 555 'edit_item' => __( 'Edit Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 556 'view_item' => __( 'View Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 557 'all_items' => __( 'All Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 558 'search_items' => __( 'Search Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 559 'parent_item_colon' => __( 'Parent Tracking Scripts:', TRACKING_SCRIPT_TEXTDOMAIN ), 560 'not_found' => __( 'No Tracking Scripts found.', TRACKING_SCRIPT_TEXTDOMAIN ), 561 'not_found_in_trash' => __( 'No Tracking Scripts found in Trash.', TRACKING_SCRIPT_TEXTDOMAIN ) 562 ); 563 564 $args = array( 565 'labels' => $labels, 566 'description' => __( 'Description.', TRACKING_SCRIPT_TEXTDOMAIN ), 567 'public' => false, 568 'publicly_queryable' => false, 569 'show_ui' => true, 570 'show_in_menu' => false, 571 'query_var' => false, 572 'rewrite' => array( 'slug' => 'tracking-scripts' ), 573 'capability_type' => 'post', 574 'capabilities' => array( 575 'edit_post' => 'manage_options', 576 'read_post' => 'manage_options', 577 'delete_post' => 'manage_options', 578 'edit_posts' => 'manage_options', 579 'edit_others_posts' => 'manage_options', 580 'delete_posts' => 'manage_options', 581 'publish_posts' => 'manage_options', 582 'read_private_posts' => 'manage_options' 583 ), 584 'has_archive' => false, 585 'hierarchical' => false, 586 'menu_position' => null, 587 'supports' => array( 'title', 'script-code', 'script-active', 'script-location', 'script-order' ) 588 ); 589 590 register_post_type( 'r8_tracking_scripts', $args ); 591 592 } 593 594 /************************************************* 595 * Admin Area 596 **************************************************/ 597 598 function admin_enqueue_scripts($hook) { 599 global $post; 600 601 if ( $hook === 'post.php' || $hook === 'post-new.php' || $hook === 'edit.php' ) { 602 if ( ! empty( $post->post_type ) && ($post->post_type === 'r8_tracking_scripts') ) { 603 wp_enqueue_style( 'r8-tsm-edit-script', plugins_url('/css/tracking-script-edit.css', __FILE__ ) ); 604 wp_enqueue_style( 'r8-tsm-select2-css', plugins_url('/css/select2.min.css', __FILE__ ) ); 605 wp_enqueue_script( 'r8-tsm-select2-js', plugins_url( '/js/select2.min.js', __FILE__ ), array(), null, true ); 606 wp_enqueue_script( 'r8-tsm-post-edit-js', plugins_url( '/js/post-edit.js', __FILE__ ), array('jquery', 'r8-tsm-select2-js'), null, true ); 607 } 608 } 609 } 610 611 function save_post() { 612 613 global $post; 614 615 if ( ! empty( $post->post_type ) ) { 616 if ( $post->post_type === 'r8_tracking_scripts' ) { 617 $expiry_obj = new \stdClass(); 618 $expiry_obj->type = ''; 619 $expiry_obj->num = ''; 620 $expiry_obj->block = ''; 621 $expiry_obj->date = ''; 622 623 // if ( ! empty( $_POST['r8_tsm_script_code'] ) ) { 624 update_post_meta( $post->ID, 'r8_tsm_script_code', stripslashes(esc_textarea($_POST['r8_tsm_script_code'])) ); 625 // } 626 627 if ( ! empty( $_POST['r8_tsm_active'] ) ) { 628 update_post_meta( $post->ID, 'r8_tsm_active', sanitize_text_field( $_POST['r8_tsm_active'] ) ); 629 } 630 631 if ( ! empty( $_POST['r8_tsm_script_order'] ) ) { 632 update_post_meta( $post->ID, 'r8_tsm_script_order', intval( $_POST['r8_tsm_script_order'] ) ); 633 } 634 635 if ( ! empty( $_POST['r8_tsm_script_location'] ) ) { 636 update_post_meta( $post->ID, 'r8_tsm_script_location', sanitize_text_field( $_POST['r8_tsm_script_location'] ) ); 637 } 638 639 if ( ! empty( $_POST['r8_tsm_script_expiry'] ) || ( !empty($_POST['r8_expire_date_in_num']) && !empty($_POST['r8_expire_date_in_block']) ) ) { 640 641 $expiry_date = $this->set_expiry_info( $_POST['r8_tsm_script_expiry'], $_POST['r8_expire_date_in_num'], $_POST['r8_expire_date_in_block'] ); 642 $expiry_obj->type = $_POST['r8_tsm_script_expiry'] ?: ''; 643 $expiry_obj->num = $_POST['r8_expire_date_in_num'] ?: ''; 644 $expiry_obj->block = $_POST['r8_expire_date_in_block'] ?: ''; 645 $expiry_obj->date = $expiry_date ?: ''; 646 update_post_meta( $post->ID, 'r8_tsm_script_expiry_info', $expiry_obj ); 647 } 648 649 if ( ! empty( $_POST['r8_tsm_script_page'] ) && is_array( $_POST['r8_tsm_script_page'] ) ) { 650 update_post_meta( $post->ID, 'r8_tsm_script_page', $_POST['r8_tsm_script_page'] ); 651 } else { 652 update_post_meta( $post->ID, 'r8_tsm_script_page', array() ); 653 } 654 655 } 656 } 657 658 } 659 660 public function tracking_scripts_create_menu() { 661 add_menu_page( 'Tracking Script Manager', 'Tracking Script Manager', 'manage_options', 'edit.php?post_type=r8_tracking_scripts', null ); 662 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 ); 663 } 664 665 // Admin Scripts 666 public function tracking_scripts_admin_scripts() { 667 wp_enqueue_script('jquery'); 668 669 wp_enqueue_script( 'tracking_script_js', plugin_dir_url(__FILE__) . '/js/built.min.js', array(), '', true ); 670 wp_localize_script( 'tracking_script_js', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); 671 } 672 673 674 // Ajax Functions 675 public function tracking_scripts_posts_ajax_handler() { 676 $post_type = ($_POST['postType']) ? esc_attr($_POST['postType']) : 'post'; 677 678 $args = array( 679 'post_type' => $post_type, 680 'posts_per_page' => -1, 681 'orderby' => 'name', 682 'order' => 'ASC' 683 ); 684 685 ob_start(); 686 687 $query = new WP_Query($args); 688 echo '<option value="none" id="none">Choose '.ucwords($post_type).'</option>'; 689 while( $query->have_posts() ) : $query->the_post(); 690 echo '<option value="'.get_the_ID().'" id="'.get_the_ID().'">'.ucwords(get_the_title()).'</option>'; 228 ) 229 ); 230 $header_scripts = new WP_Query( $args ); 231 if ( $header_scripts->have_posts() ) { 232 while ( $header_scripts->have_posts() ) : $header_scripts->the_post(); 233 $page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); 234 $expiry_info = get_post_meta( get_the_ID(), 'r8_tsm_script_expiry_info', true ); 235 $this->print_tsm_scripts( get_the_ID(), $page, $current_id, $expiry_info ); 691 236 endwhile; 692 237 wp_reset_postdata(); 693 694 echo ob_get_clean(); 695 die(); 696 } 697 } 698 699 function tracking_scripts() { 700 701 // globals 702 global $tracking_scripts; 703 704 705 // initialize 706 if ( ! isset($tracking_scripts) ) { 707 $tracking_scripts = new Tracking_Scripts(); 708 $tracking_scripts->initialize(); 709 } 710 711 // return 712 return $tracking_scripts; 713 714 } 715 238 } 239 } 240 241 function find_page_tracking_codes() { 242 243 global $wp_query; 244 $current_id = $wp_query->post->ID; 245 $args = array( 246 'post_type' => 'r8_tracking_scripts', 247 'posts_per_page' => - 1, 248 'post_status' => 'publish', 249 'meta_key' => 'r8_tsm_script_order', 250 'orderby' => 'meta_value_num', 251 'order' => 'ASC', 252 'meta_query' => array( 253 'relation' => 'AND', 254 array( 255 'key' => 'r8_tsm_script_location', 256 'value' => 'page', 257 'compare' => '=' 258 ), 259 array( 260 'key' => 'r8_tsm_active', 261 'value' => 'active', 262 'compare' => '=' 263 ) 264 ) 265 ); 266 $page_scripts = new WP_Query( $args ); 267 if ( $page_scripts->have_posts() ) { 268 while ( $page_scripts->have_posts() ) : $page_scripts->the_post(); 269 $page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); 270 $expiry_info = get_post_meta( get_the_ID(), 'r8_tsm_script_expiry_info', true ); 271 $this->print_tsm_scripts( get_the_ID(), $page, $current_id, $expiry_info ); 272 endwhile; 273 wp_reset_postdata(); 274 } 275 } 276 277 function find_footer_tracking_codes() { 278 global $wp_query; 279 $current_id = $wp_query->post->ID; 280 $args = array( 281 'post_type' => 'r8_tracking_scripts', 282 'posts_per_page' => - 1, 283 'post_status' => 'publish', 284 'meta_key' => 'r8_tsm_script_order', 285 'orderby' => 'meta_value_num', 286 'order' => 'ASC', 287 'meta_query' => array( 288 'relation' => 'AND', 289 array( 290 'key' => 'r8_tsm_script_location', 291 'value' => 'footer', 292 'compare' => '=' 293 ), 294 array( 295 'key' => 'r8_tsm_active', 296 'value' => 'active', 297 'compare' => '=' 298 ) 299 ) 300 ); 301 $footer_scripts = new WP_Query( $args ); 302 if ( $footer_scripts->have_posts() ) { 303 while ( $footer_scripts->have_posts() ) : $footer_scripts->the_post(); 304 $page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); 305 $expiry_info = get_post_meta( get_the_ID(), 'r8_tsm_script_expiry_info', true ); 306 $this->print_tsm_scripts( get_the_ID(), $page, $current_id, $expiry_info ); 307 endwhile; 308 wp_reset_postdata(); 309 } 310 } 311 312 function add_tracking_script_columns( $columns ) { 313 314 $columns = array( 315 'cb' => '<input type="checkbox" />', 316 'title' => __( 'Script Title', TRACKING_SCRIPT_TEXTDOMAIN ), 317 'global' => __( 'Global', TRACKING_SCRIPT_TEXTDOMAIN ), 318 'location' => __( 'Location', TRACKING_SCRIPT_TEXTDOMAIN ), 319 'status' => __( 'Status', TRACKING_SCRIPT_TEXTDOMAIN ), 320 'expire' => __( 'Expires', TRACKING_SCRIPT_TEXTDOMAIN ), 321 ); 322 323 return $columns; 324 } 325 326 function tracking_script_column_content( $column_name, $post_ID ) { 327 328 if ( $column_name === 'status' ) { 329 $active = get_post_meta( $post_ID, 'r8_tsm_active', true ); 330 if ( $active === 'active' ) { 331 echo 'Active'; 332 } else { 333 echo 'Inactive'; 334 } 335 } 336 if ( $column_name === 'global' ) { 337 $global = get_post_meta( $post_ID, 'r8_tsm_script_page', true ); 338 if ( empty( $global ) ) { 339 echo ' ✓'; 340 } else { 341 echo ' ✗'; 342 } 343 } 344 if ( $column_name === 'location' ) { 345 $location = get_post_meta( $post_ID, 'r8_tsm_script_location', true ); 346 if ( $location ) { 347 echo ucwords( $location ); 348 } 349 } 350 if ( $column_name === 'expire' ) { 351 352 $expiry_info = get_post_meta( $post_ID, 'r8_tsm_script_expiry_info', true ); 353 $expiry_timestamp = is_object( $expiry_info ) ? $expiry_info->date : 'Never'; 354 $expiry_date_type = is_object( $expiry_info ) ? $expiry_info->type : ''; 355 $expiry_date = $this->get_expiry_info( $expiry_date_type, $expiry_timestamp ); 356 $if_expire = $this->check_expiry_script( $expiry_timestamp ); 357 if ( ( $expiry_date_type == 'in' ) && $if_expire ) { 358 echo '<span class="expired">Expired</span>'; 359 } else { 360 echo $expiry_date; 361 } 362 } 363 } 364 365 function tracking_scripts_column_sort( $columns ) { 366 367 $columns['global'] = 'global'; 368 $columns['location'] = 'location'; 369 $columns['status'] = 'status'; 370 $columns['expire'] = 'expire'; 371 372 return $columns; 373 } 374 375 public function add_script_metaboxes() { 376 377 add_meta_box( 'r8_tsm_script_code', __( 'Script Code', TRACKING_SCRIPT_TEXTDOMAIN ), array( 378 $this, 379 'script_code_metabox' 380 ), 'r8_tracking_scripts', 'normal' ); 381 add_meta_box( 'r8_tsm_script_active', __( 'Script Status', TRACKING_SCRIPT_TEXTDOMAIN ), array( 382 $this, 383 'script_active_metabox' 384 ), 'r8_tracking_scripts', 'side' ); 385 add_meta_box( 'r8_tsm_script_expiry', __( 'Script Expires', TRACKING_SCRIPT_TEXTDOMAIN ), array( 386 $this, 387 'script_expiry_metabox' 388 ), 'r8_tracking_scripts', 'side' ); 389 add_meta_box( 'r8_tsm_script_order', __( 'Script Order', TRACKING_SCRIPT_TEXTDOMAIN ), array( 390 $this, 391 'script_order_metabox' 392 ), 'r8_tracking_scripts', 'side' ); 393 add_meta_box( 'r8_tsm_script_location', __( 'Script Location', TRACKING_SCRIPT_TEXTDOMAIN ), array( 394 $this, 395 'script_location_metabox' 396 ), 'r8_tracking_scripts', 'normal' ); 397 add_meta_box( 'r8_tsm_script_page', __( 'Specific Script Placement (Page(s) or Post(s))', TRACKING_SCRIPT_TEXTDOMAIN ), array( 398 $this, 399 'script_page_metabox' 400 ), 'r8_tracking_scripts', 'normal' ); 401 } 402 403 function script_code_metabox() { 404 405 global $post; 406 $script_code = get_post_meta( $post->ID, 'r8_tsm_script_code', true ); 407 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-code-metabox.php' ); 408 } 409 410 function script_active_metabox() { 411 412 global $post; 413 $active = get_post_meta( $post->ID, 'r8_tsm_active', true ); 414 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-active-metabox.php' ); 415 } 416 417 function script_expiry_metabox() { 418 global $post; 419 $expiry_info = get_post_meta( $post->ID, 'r8_tsm_script_expiry_info', true ); 420 $expiry_timestamp = is_object( $expiry_info ) ? $expiry_info->date : 'Never'; 421 $expiry_date_type = is_object( $expiry_info ) ? $expiry_info->type : ''; 422 $expiry_date_num = is_object( $expiry_info ) ? $expiry_info->num : ''; 423 $expiry_date_block = is_object( $expiry_info ) ? $expiry_info->block : ''; 424 $expiry_date = $this->get_expiry_info( $expiry_date_type, $expiry_timestamp ); 425 $if_expire = $this->check_expiry_script( $expiry_timestamp ); 426 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-expiry-metabox.php' ); 427 } 428 429 function script_order_metabox() { 430 431 global $post; 432 $order = get_post_meta( $post->ID, 'r8_tsm_script_order', true ); 433 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-order-metabox.php' ); 434 } 435 436 function script_location_metabox() { 437 438 global $post; 439 $location = get_post_meta( $post->ID, 'r8_tsm_script_location', true ); 440 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-location-metabox.php' ); 441 } 442 443 function script_page_metabox() { 444 445 global $post; 446 $script_page = get_post_meta( $post->ID, 'r8_tsm_script_page', true ); 447 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-page-metabox.php' ); 448 } 449 450 public function get_expiry_info( $expire_date_type, $script_expiry_timestamp ) { 451 $show_date = 'Never'; 452 if ( $expire_date_type == 'never' ) { 453 $show_date = ucwords( $expire_date_type ); 454 } 455 if ( $expire_date_type == 'in' ) { 456 $expiry_timestamp = new DateTime( $script_expiry_timestamp ); 457 $show_date = $expiry_timestamp->format( 'M d, Y \a\t H:i' ); 458 } 459 460 return $show_date; 461 } 462 463 public function set_expiry_info( $expire_date_type, $expire_date_num, $expire_date_block, $format = 'c' ) { 464 $show_date = 'Never'; 465 if ( $expire_date_type == 'never' ) { 466 $show_date = ucwords( $expire_date_type ); 467 } 468 if ( $expire_date_type == 'in' ) { 469 switch ( $expire_date_block ) { 470 case 'minutes': 471 $timespan = 'PT' . absint( $expire_date_num ) . 'M'; 472 break; 473 case 'weeks': 474 $timespan = 'P' . absint( $expire_date_num ) * 7 . 'D'; 475 break; 476 case 'months': 477 $timespan = 'P' . absint( $expire_date_num ) . 'M'; 478 break; 479 case 'years': 480 $timespan = 'P' . absint( $expire_date_num ) . 'Y'; 481 break; 482 default: // days 483 $timespan = 'PT' . absint( $expire_date_num ) * 24 . 'H'; 484 break; 485 } 486 $show_date = $this->get_date_time( $timespan, $format ); 487 } 488 489 return $show_date; 490 } 491 492 public function get_date_time( $timespan, $format ) { 493 $current_time = new DateTime(); 494 $current_time->add( new DateInterval( $timespan ) ); 495 $expire_time = $current_time->format( $format ); 496 497 return $expire_time; 498 } 499 500 public function check_expiry_script( $expiry_timespan ) { 501 if ( $expiry_timespan == 'Never' ) { 502 return false; 503 } 504 $current_time = new DateTime(); 505 $expire_time = new DateTime( $expiry_timespan ); 506 // check expiry 507 if ( $current_time < $expire_time ) { 508 return false; 509 } else { 510 $this->set_script_inactive(); 511 512 return true; 513 } 514 } 515 516 public function set_script_inactive() { 517 global $post; 518 if ( ! empty( $post->post_type ) ) { 519 if ( $post->post_type === 'r8_tracking_scripts' ) { 520 $active = get_post_meta( $post->ID, 'r8_tsm_active', true ); 521 if ( 'active' === $active ) { 522 update_post_meta( $post->ID, 'r8_tsm_active', 'inactive' ); 523 } 524 } 525 } 526 } 527 528 public function register_scripts_post_type() { 529 530 $labels = array( 531 'name' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 532 'singular_name' => _x( 'Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 533 'menu_name' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 534 'name_admin_bar' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 535 'add_new' => _x( 'Add New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 536 'add_new_item' => __( 'Add New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 537 'new_item' => __( 'New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 538 'edit_item' => __( 'Edit Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 539 'view_item' => __( 'View Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), 540 'all_items' => __( 'All Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 541 'search_items' => __( 'Search Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), 542 'parent_item_colon' => __( 'Parent Tracking Scripts:', TRACKING_SCRIPT_TEXTDOMAIN ), 543 'not_found' => __( 'No Tracking Scripts found.', TRACKING_SCRIPT_TEXTDOMAIN ), 544 'not_found_in_trash' => __( 'No Tracking Scripts found in Trash.', TRACKING_SCRIPT_TEXTDOMAIN ) 545 ); 546 $args = array( 547 'labels' => $labels, 548 'description' => __( 'Description.', TRACKING_SCRIPT_TEXTDOMAIN ), 549 'public' => false, 550 'publicly_queryable' => false, 551 'show_ui' => true, 552 'show_in_menu' => false, 553 'query_var' => false, 554 'rewrite' => array( 'slug' => 'tracking-scripts' ), 555 'capability_type' => 'post', 556 'capabilities' => array( 557 'edit_post' => 'manage_options', 558 'read_post' => 'manage_options', 559 'delete_post' => 'manage_options', 560 'edit_posts' => 'manage_options', 561 'edit_others_posts' => 'manage_options', 562 'delete_posts' => 'manage_options', 563 'publish_posts' => 'manage_options', 564 'read_private_posts' => 'manage_options' 565 ), 566 'has_archive' => false, 567 'hierarchical' => false, 568 'menu_position' => null, 569 'supports' => array( 570 'title', 571 'script-code', 572 'script-active', 573 'script-location', 574 'script-order' 575 ) 576 ); 577 register_post_type( 'r8_tracking_scripts', $args ); 578 } 579 580 /************************************************* 581 * Admin Area 582 **************************************************/ 583 function admin_enqueue_scripts( $hook ) { 584 global $post; 585 if ( $hook === 'post.php' || $hook === 'post-new.php' ) { 586 if ( ! empty( $post->post_type ) && ( $post->post_type === 'r8_tracking_scripts' ) ) { 587 wp_enqueue_style( 'r8-tsm-edit-script', plugins_url( '/css/tracking-script-edit.css', __FILE__ ) ); 588 wp_enqueue_style( 'r8-tsm-select2-css', plugins_url( '/css/select2.min.css', __FILE__ ) ); 589 wp_enqueue_script( 'r8-tsm-select2-js', plugins_url( '/js/select2.min.js', __FILE__ ), array(), null, true ); 590 wp_enqueue_script( 'r8-tsm-post-edit-js', plugins_url( '/js/post-edit.js', __FILE__ ), array( 591 'jquery', 592 'r8-tsm-select2-js' 593 ), null, true ); 594 } 595 } 596 if ( $hook === 'post.php' || $hook === 'edit.php' ) { 597 if ( ! empty( $post->post_type ) && ( $post->post_type === 'r8_tracking_scripts' ) ) { 598 wp_enqueue_style( 'r8-tsm-post-list', plugins_url( '/css/post-list.css', __FILE__ ) ); 599 wp_enqueue_script( 'r8-tsm-post-list-js', plugins_url( '/js/post-list.js', __FILE__ ), array( 'jquery' ), null, true ); 600 } 601 } 602 } 603 604 function save_post() { 605 606 global $post; 607 if ( ! empty( $post->post_type ) ) { 608 if ( $post->post_type === 'r8_tracking_scripts' ) { 609 $expiry_obj = new \stdClass(); 610 $expiry_obj->type = ''; 611 $expiry_obj->num = ''; 612 $expiry_obj->block = ''; 613 $expiry_obj->date = ''; 614 // if ( ! empty( $_POST['r8_tsm_script_code'] ) ) { 615 update_post_meta( $post->ID, 'r8_tsm_script_code', stripslashes( esc_textarea( $_POST['r8_tsm_script_code'] ) ) ); 616 // } 617 if ( ! empty( $_POST['r8_tsm_active'] ) ) { 618 update_post_meta( $post->ID, 'r8_tsm_active', sanitize_text_field( $_POST['r8_tsm_active'] ) ); 619 } 620 if ( ! empty( $_POST['r8_tsm_script_order'] ) ) { 621 update_post_meta( $post->ID, 'r8_tsm_script_order', intval( $_POST['r8_tsm_script_order'] ) ); 622 } 623 if ( ! empty( $_POST['r8_tsm_script_location'] ) ) { 624 update_post_meta( $post->ID, 'r8_tsm_script_location', sanitize_text_field( $_POST['r8_tsm_script_location'] ) ); 625 } 626 if ( ! empty( $_POST['r8_tsm_script_expiry'] ) || ( ! empty( $_POST['r8_expire_date_in_num'] ) && ! empty( $_POST['r8_expire_date_in_block'] ) ) ) { 627 628 $expiry_date = $this->set_expiry_info( $_POST['r8_tsm_script_expiry'], $_POST['r8_expire_date_in_num'], $_POST['r8_expire_date_in_block'] ); 629 $expiry_obj->type = $_POST['r8_tsm_script_expiry'] ?: ''; 630 $expiry_obj->num = $_POST['r8_expire_date_in_num'] ?: ''; 631 $expiry_obj->block = $_POST['r8_expire_date_in_block'] ?: ''; 632 $expiry_obj->date = $expiry_date ?: ''; 633 update_post_meta( $post->ID, 'r8_tsm_script_expiry_info', $expiry_obj ); 634 } 635 if ( ! empty( $_POST['r8_tsm_script_page'] ) && is_array( $_POST['r8_tsm_script_page'] ) ) { 636 update_post_meta( $post->ID, 'r8_tsm_script_page', $_POST['r8_tsm_script_page'] ); 637 } else { 638 update_post_meta( $post->ID, 'r8_tsm_script_page', array() ); 639 } 640 } 641 } 642 } 643 644 public function tracking_scripts_create_menu() { 645 add_menu_page( 'Tracking Script Manager', 'Tracking Script Manager', 'manage_options', 'edit.php?post_type=r8_tracking_scripts', null ); 646 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 ); 647 } 648 649 // Admin Scripts 650 public function tracking_scripts_admin_scripts() { 651 wp_enqueue_script( 'jquery' ); 652 wp_enqueue_script( 'tracking_script_js', plugin_dir_url( __FILE__ ) . '/js/built.min.js', array(), '', true ); 653 wp_localize_script( 'tracking_script_js', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); 654 } 655 656 // Ajax Functions 657 public function tracking_scripts_posts_ajax_handler() { 658 $post_type = ( $_POST['postType'] ) ? esc_attr( $_POST['postType'] ) : 'post'; 659 $args = array( 660 'post_type' => $post_type, 661 'posts_per_page' => - 1, 662 'orderby' => 'name', 663 'order' => 'ASC' 664 ); 665 ob_start(); 666 $query = new WP_Query( $args ); 667 echo '<option value="none" id="none">Choose ' . ucwords( $post_type ) . '</option>'; 668 while ( $query->have_posts() ) : $query->the_post(); 669 echo '<option value="' . get_the_ID() . '" id="' . get_the_ID() . '">' . ucwords( get_the_title() ) . '</option>'; 670 endwhile; 671 wp_reset_postdata(); 672 echo ob_get_clean(); 673 die(); 674 } 675 } 676 677 function tracking_scripts() { 678 679 // globals 680 global $tracking_scripts; 716 681 // initialize 717 tracking_scripts(); 682 if ( ! isset( $tracking_scripts ) ) { 683 $tracking_scripts = new Tracking_Scripts(); 684 $tracking_scripts->initialize(); 685 } 686 687 // return 688 return $tracking_scripts; 718 689 } 719 690 720 if ( ! class_exists('Tracking_Script') ) { 721 722 class Tracking_Script { 723 public $script_name; 724 public $script_code; 725 public $active; 726 public $order; 727 public $page_id; 728 public $location; 729 public $script_id; 730 731 function __construct() { 732 733 } 734 } 735 691 // initialize 692 tracking_scripts(); 693 } 694 if ( ! class_exists( 'Tracking_Script' ) ) { 695 696 class Tracking_Script { 697 public $script_name; 698 public $script_code; 699 public $active; 700 public $order; 701 public $page_id; 702 public $location; 703 public $script_id; 704 705 function __construct() { 706 707 } 736 708 } 709 }
Note: See TracChangeset
for help on using the changeset viewer.