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