PluginProbe
Tracking Script Manager / 2.0.14
Tracking Script Manager v2.0.14
trunk 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 All 33 releases
tracking-script-manager / tracking-scripts-manager.php

tracking-scripts-manager.php in Tracking Script Manager 2.0.14, at tracking-scripts-manager.php

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