| 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.0 |
| 7 |
* Author: Red8 Interactive |
| 8 |
* Author URI: http://red8interactive.com |
| 9 |
* License: GPL2 |
| 10 |
*/ |
| 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 |
|
| 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 |
// Filters |
| 69 |
add_filter( 'manage_r8_tracking_scripts_posts_columns', array( $this, 'add_tracking_script_columns' ) ); |
| 70 |
add_filter( 'manage_edit-r8_tracking_scripts_sortable_columns', array( $this, 'tracking_scripts_column_sort' ) ); |
| 71 |
|
| 72 |
// Includes |
| 73 |
require_once plugin_dir_path( __FILE__ ) . 'classes/wp-async-request.php'; |
| 74 |
require_once plugin_dir_path( __FILE__ ) . 'classes/wp-background-process.php'; |
| 75 |
require_once plugin_dir_path( __FILE__ ) . 'classes/class-process-tracking-scripts.php'; |
| 76 |
|
| 77 |
$this->process_all = new TSM_Process_Tracking_Scripts(); |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
/************************************************* |
| 82 |
* Front End |
| 83 |
**************************************************/ |
| 84 |
|
| 85 |
public function process_handler() { |
| 86 |
|
| 87 |
if ( ! isset( $_GET['tsm_update_scripts'] ) || ! isset( $_GET['_wpnonce'] ) ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'tsm_update_scripts' ) ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
if ( 'true' === $_GET['tsm_update_scripts'] ) { |
| 96 |
update_option( 'tsm_is_processing', true ); |
| 97 |
$this->handle_all(); |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
protected function handle_all() { |
| 103 |
$scripts = $this->get_tracking_scripts(); |
| 104 |
|
| 105 |
if ( ! empty( $scripts ) ) { |
| 106 |
|
| 107 |
foreach ( $scripts as $script ) { |
| 108 |
$this->process_all->push_to_queue( $script ); |
| 109 |
} |
| 110 |
|
| 111 |
$this->process_all->save()->dispatch(); |
| 112 |
|
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
protected function get_tracking_scripts() { |
| 117 |
|
| 118 |
$scripts = array(); |
| 119 |
$header_scripts = get_option( 'header_tracking_script_code' ) ? unserialize( get_option('header_tracking_script_code') ) : null; |
| 120 |
$page_scripts = get_option( 'page_tracking_script_code' ) ? unserialize( get_option('page_tracking_script_code') ) : null; |
| 121 |
$footer_scripts = get_option( 'footer_tracking_script_code' ) ? unserialize( get_option('footer_tracking_script_code') ) : null; |
| 122 |
|
| 123 |
if ( ! empty( $header_scripts ) ) { |
| 124 |
$scripts = array_merge( $scripts, $header_scripts ); |
| 125 |
} |
| 126 |
|
| 127 |
if ( ! empty( $page_scripts ) ) { |
| 128 |
$scripts = array_merge( $scripts, $page_scripts ); |
| 129 |
} |
| 130 |
|
| 131 |
if ( ! empty( $footer_scripts ) ) { |
| 132 |
$scripts = array_merge( $scripts, $footer_scripts ); |
| 133 |
} |
| 134 |
|
| 135 |
return $scripts; |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
function admin_notices() { |
| 140 |
$class = 'notice notice-info is-dismissible'; |
| 141 |
$header_scripts = get_option('header_tracking_script_code'); |
| 142 |
$page_scripts = get_option('page_tracking_script_code'); |
| 143 |
$footer_scripts = get_option('footer_tracking_script_code'); |
| 144 |
$is_processing = get_option('tsm_is_processing'); |
| 145 |
$has_tracking_scripts = ( $header_scripts || $page_scripts || $footer_scripts ) ? true : false; |
| 146 |
|
| 147 |
if ( $has_tracking_scripts && $is_processing ) { |
| 148 |
$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 ); |
| 149 |
$notice = sprintf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); |
| 150 |
echo $notice; |
| 151 |
} |
| 152 |
|
| 153 |
if ( $has_tracking_scripts && ! $is_processing ) { |
| 154 |
$url = wp_nonce_url( admin_url('edit.php?post_type=r8_tracking_scripts&tsm_update_scripts=true&tsm_is_processing=true'), 'tsm_update_scripts' ); |
| 155 |
$message = __( 'Tracking Scripts Manager has updated to a new version, click OK to update your scripts to the updated version.', TRACKING_SCRIPT_TEXTDOMAIN ); |
| 156 |
$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 ) ); |
| 157 |
echo $notice; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// Header Tracking Codes |
| 162 |
function find_header_tracking_codes() { |
| 163 |
|
| 164 |
global $wp_query; |
| 165 |
$current_id = $wp_query->post->ID; |
| 166 |
|
| 167 |
$args = array( |
| 168 |
'post_type' => 'r8_tracking_scripts', |
| 169 |
'post_status' => 'publish', |
| 170 |
'posts_per_page' => -1, |
| 171 |
'meta_key' => 'r8_tsm_script_order', |
| 172 |
'orderby' => 'meta_value_num', |
| 173 |
'order' => 'ASC', |
| 174 |
'meta_query' => array( |
| 175 |
'relation' => 'AND', |
| 176 |
array( |
| 177 |
'key' => 'r8_tsm_script_location', |
| 178 |
'value' => 'header', |
| 179 |
'compare' => '=' |
| 180 |
), |
| 181 |
array( |
| 182 |
'key' => 'r8_tsm_active', |
| 183 |
'value' => 'active', |
| 184 |
'compare' => '=' |
| 185 |
) |
| 186 |
) |
| 187 |
); |
| 188 |
|
| 189 |
$header_scripts = new WP_Query($args); |
| 190 |
|
| 191 |
if ( $header_scripts->have_posts() ) { |
| 192 |
while ( $header_scripts->have_posts() ) : $header_scripts->the_post(); |
| 193 |
$page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); |
| 194 |
|
| 195 |
if ( is_array( $page ) && in_array(intval($current_id), $page) ) { |
| 196 |
echo html_entity_decode(get_post_meta( get_the_ID(), 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252'); |
| 197 |
} elseif ( empty( $page ) ) { |
| 198 |
echo html_entity_decode( get_post_meta( get_the_ID(), 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252' ); |
| 199 |
} |
| 200 |
|
| 201 |
endwhile; wp_reset_postdata(); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
function find_page_tracking_codes() { |
| 206 |
|
| 207 |
global $wp_query; |
| 208 |
$current_id = $wp_query->post->ID; |
| 209 |
|
| 210 |
$args = array( |
| 211 |
'post_type' => 'r8_tracking_scripts', |
| 212 |
'posts_per_page' => -1, |
| 213 |
'post_status' => 'publish', |
| 214 |
'meta_key' => 'r8_tsm_script_order', |
| 215 |
'orderby' => 'meta_value_num', |
| 216 |
'order' => 'ASC', |
| 217 |
'meta_query' => array( |
| 218 |
'relation' => 'AND', |
| 219 |
array( |
| 220 |
'key' => 'r8_tsm_script_location', |
| 221 |
'value' => 'page', |
| 222 |
'compare' => '=' |
| 223 |
), |
| 224 |
array( |
| 225 |
'key' => 'r8_tsm_active', |
| 226 |
'value' => 'active', |
| 227 |
'compare' => '=' |
| 228 |
) |
| 229 |
) |
| 230 |
); |
| 231 |
|
| 232 |
$page_scripts = new WP_Query($args); |
| 233 |
|
| 234 |
if ( $page_scripts->have_posts() ) { |
| 235 |
while ( $page_scripts->have_posts() ) : $page_scripts->the_post(); |
| 236 |
$page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); |
| 237 |
|
| 238 |
if ( is_array( $page ) && in_array(intval($current_id), $page) ) { |
| 239 |
echo html_entity_decode(get_post_meta( get_the_ID(), 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252'); |
| 240 |
} elseif ( empty( $page ) ) { |
| 241 |
echo html_entity_decode(get_post_meta( get_the_ID(), 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252'); |
| 242 |
} |
| 243 |
|
| 244 |
endwhile; wp_reset_postdata(); |
| 245 |
} |
| 246 |
|
| 247 |
} |
| 248 |
|
| 249 |
function find_footer_tracking_codes() { |
| 250 |
global $wp_query; |
| 251 |
$current_id = $wp_query->post->ID; |
| 252 |
|
| 253 |
$args = array( |
| 254 |
'post_type' => 'r8_tracking_scripts', |
| 255 |
'posts_per_page' => -1, |
| 256 |
'post_status' => 'publish', |
| 257 |
'meta_key' => 'r8_tsm_script_order', |
| 258 |
'orderby' => 'meta_value_num', |
| 259 |
'order' => 'ASC', |
| 260 |
'meta_query' => array( |
| 261 |
'relation' => 'AND', |
| 262 |
array( |
| 263 |
'key' => 'r8_tsm_script_location', |
| 264 |
'value' => 'footer', |
| 265 |
'compare' => '=' |
| 266 |
), |
| 267 |
array( |
| 268 |
'key' => 'r8_tsm_active', |
| 269 |
'value' => 'active', |
| 270 |
'compare' => '=' |
| 271 |
) |
| 272 |
) |
| 273 |
); |
| 274 |
|
| 275 |
$footer_scripts = new WP_Query($args); |
| 276 |
|
| 277 |
if ( $footer_scripts->have_posts() ) { |
| 278 |
while ( $footer_scripts->have_posts() ) : $footer_scripts->the_post(); |
| 279 |
$page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); |
| 280 |
|
| 281 |
if ( is_array( $page ) && in_array(intval($current_id), $page) ) { |
| 282 |
echo html_entity_decode(get_post_meta( get_the_ID(), 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252'); |
| 283 |
} elseif ( empty( $page ) ) { |
| 284 |
echo html_entity_decode(get_post_meta( get_the_ID(), 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252'); |
| 285 |
} |
| 286 |
|
| 287 |
endwhile; wp_reset_postdata(); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
function add_tracking_script_columns($columns) { |
| 292 |
|
| 293 |
$columns = array( |
| 294 |
'cb' => '<input type="checkbox" />', |
| 295 |
'title' => __( 'Script Title', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 296 |
'global' => __( 'Global', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 297 |
'location' => __( 'Location', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 298 |
'status' => __( 'Status', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 299 |
); |
| 300 |
|
| 301 |
return $columns; |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
function tracking_script_column_content($column_name, $post_ID) { |
| 306 |
|
| 307 |
if ( $column_name === 'status' ) { |
| 308 |
$active = get_post_meta( $post_ID, 'r8_tsm_active', true ); |
| 309 |
if ( $active === 'active' ) { |
| 310 |
echo 'Active'; |
| 311 |
} else { |
| 312 |
echo 'Inactive'; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
if ( $column_name === 'global' ) { |
| 317 |
$global = get_post_meta( $post_ID, 'r8_tsm_script_page', true ); |
| 318 |
|
| 319 |
if ( empty($global) ) { |
| 320 |
echo ' ✓'; |
| 321 |
} else { |
| 322 |
echo ' ✗'; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
if ( $column_name === 'location' ) { |
| 327 |
$location = get_post_meta( $post_ID, 'r8_tsm_script_location', true ); |
| 328 |
|
| 329 |
if ( $location ) { |
| 330 |
echo ucwords($location); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
function tracking_scripts_column_sort($columns) { |
| 337 |
|
| 338 |
$columns['global'] = 'global'; |
| 339 |
$columns['location'] = 'location'; |
| 340 |
$columns['status'] = 'status'; |
| 341 |
|
| 342 |
return $columns; |
| 343 |
|
| 344 |
} |
| 345 |
|
| 346 |
public function add_script_metaboxes() { |
| 347 |
|
| 348 |
add_meta_box( 'r8_tsm_script_code', __( 'Script Code', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_code_metabox' ), 'r8_tracking_scripts', 'normal' ); |
| 349 |
add_meta_box( 'r8_tsm_script_active', __( 'Script Status', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_active_metabox' ), 'r8_tracking_scripts', 'side' ); |
| 350 |
add_meta_box( 'r8_tsm_script_order', __( 'Script Order', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_order_metabox' ), 'r8_tracking_scripts', 'side' ); |
| 351 |
add_meta_box( 'r8_tsm_script_location', __( 'Script Location', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_location_metabox' ), 'r8_tracking_scripts', 'normal' ); |
| 352 |
add_meta_box( 'r8_tsm_script_page', __( 'Script Page(s)', TRACKING_SCRIPT_TEXTDOMAIN ), array( $this, 'script_page_metabox' ), 'r8_tracking_scripts', 'normal' ); |
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
function script_code_metabox() { |
| 357 |
|
| 358 |
global $post; |
| 359 |
|
| 360 |
$script_code = get_post_meta( $post->ID, 'r8_tsm_script_code', true ); |
| 361 |
|
| 362 |
include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-code-metabox.php' ); |
| 363 |
|
| 364 |
} |
| 365 |
|
| 366 |
function script_active_metabox() { |
| 367 |
|
| 368 |
global $post; |
| 369 |
|
| 370 |
$active = get_post_meta( $post->ID, 'r8_tsm_active', true ); |
| 371 |
|
| 372 |
include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-active-metabox.php' ); |
| 373 |
|
| 374 |
} |
| 375 |
|
| 376 |
function script_order_metabox() { |
| 377 |
|
| 378 |
global $post; |
| 379 |
|
| 380 |
$order = get_post_meta( $post->ID, 'r8_tsm_script_order', true ); |
| 381 |
|
| 382 |
include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-order-metabox.php' ); |
| 383 |
|
| 384 |
} |
| 385 |
|
| 386 |
function script_location_metabox() { |
| 387 |
|
| 388 |
global $post; |
| 389 |
|
| 390 |
$location = get_post_meta( $post->ID, 'r8_tsm_script_location', true ); |
| 391 |
|
| 392 |
include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-location-metabox.php' ); |
| 393 |
|
| 394 |
} |
| 395 |
|
| 396 |
function script_page_metabox() { |
| 397 |
|
| 398 |
global $post; |
| 399 |
|
| 400 |
$script_page = get_post_meta( $post->ID, 'r8_tsm_script_page', true ); |
| 401 |
|
| 402 |
include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-page-metabox.php' ); |
| 403 |
|
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
public function register_scripts_post_type() { |
| 408 |
|
| 409 |
$labels = array( |
| 410 |
'name' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 411 |
'singular_name' => _x( 'Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 412 |
'menu_name' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 413 |
'name_admin_bar' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 414 |
'add_new' => _x( 'Add New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 415 |
'add_new_item' => __( 'Add New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 416 |
'new_item' => __( 'New Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 417 |
'edit_item' => __( 'Edit Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 418 |
'view_item' => __( 'View Tracking Script', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 419 |
'all_items' => __( 'All Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 420 |
'search_items' => __( 'Search Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 421 |
'parent_item_colon' => __( 'Parent Tracking Scripts:', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 422 |
'not_found' => __( 'No Tracking Scripts found.', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 423 |
'not_found_in_trash' => __( 'No Tracking Scripts found in Trash.', TRACKING_SCRIPT_TEXTDOMAIN ) |
| 424 |
); |
| 425 |
|
| 426 |
$args = array( |
| 427 |
'labels' => $labels, |
| 428 |
'description' => __( 'Description.', TRACKING_SCRIPT_TEXTDOMAIN ), |
| 429 |
'public' => false, |
| 430 |
'publicly_queryable' => false, |
| 431 |
'show_ui' => true, |
| 432 |
'show_in_menu' => false, |
| 433 |
'query_var' => false, |
| 434 |
'rewrite' => array( 'slug' => 'tracking-scripts' ), |
| 435 |
'capability_type' => 'post', |
| 436 |
'has_archive' => false, |
| 437 |
'hierarchical' => false, |
| 438 |
'menu_position' => null, |
| 439 |
'supports' => array( 'title', 'script-code', 'script-active', 'script-location', 'script-order' ) |
| 440 |
); |
| 441 |
|
| 442 |
register_post_type( 'r8_tracking_scripts', $args ); |
| 443 |
|
| 444 |
} |
| 445 |
|
| 446 |
/************************************************* |
| 447 |
* Admin Area |
| 448 |
**************************************************/ |
| 449 |
|
| 450 |
function admin_enqueue_scripts($hook) { |
| 451 |
global $post; |
| 452 |
|
| 453 |
if ( $hook === 'post.php' || $hook === 'post-new.php' ) { |
| 454 |
if ( $post->post_type === 'r8_tracking_scripts' ) { |
| 455 |
wp_enqueue_style( 'r8-tsm-edit-script', plugins_url('/css/tracking-script-edit.css', __FILE__ ) ); |
| 456 |
wp_enqueue_style( 'r8-tsm-select2-css', plugins_url('/css/select2.min.css', __FILE__ ) ); |
| 457 |
wp_enqueue_script( 'r8-tsm-select2-js', plugins_url( '/js/select2.min.js', __FILE__ ), array(), null, true ); |
| 458 |
wp_enqueue_script( 'r8-tsm-post-edit-js', plugins_url( '/js/post-edit.js', __FILE__ ), array('jquery', 'r8-tsm-select2-js'), null, true ); |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
function save_post() { |
| 464 |
|
| 465 |
global $post; |
| 466 |
|
| 467 |
if ( ! empty( $post->post_type ) ) { |
| 468 |
if ( $post->post_type === 'r8_tracking_scripts' ) { |
| 469 |
|
| 470 |
if ( ! empty( $_POST['r8_tsm_script_code'] ) ) { |
| 471 |
update_post_meta( $post->ID, 'r8_tsm_script_code', stripslashes(esc_textarea($_POST['r8_tsm_script_code'])) ); |
| 472 |
} |
| 473 |
|
| 474 |
if ( ! empty( $_POST['r8_tsm_active'] ) ) { |
| 475 |
update_post_meta( $post->ID, 'r8_tsm_active', sanitize_text_field( $_POST['r8_tsm_active'] ) ); |
| 476 |
} |
| 477 |
|
| 478 |
if ( ! empty( $_POST['r8_tsm_script_order'] ) ) { |
| 479 |
update_post_meta( $post->ID, 'r8_tsm_script_order', intval( $_POST['r8_tsm_script_order'] ) ); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
if ( ! empty( $_POST['r8_tsm_script_location'] ) ) { |
| 484 |
update_post_meta( $post->ID, 'r8_tsm_script_location', sanitize_text_field( $_POST['r8_tsm_script_location'] ) ); |
| 485 |
} |
| 486 |
|
| 487 |
if ( ! empty( $_POST['r8_tsm_script_page'] ) && is_array( $_POST['r8_tsm_script_page'] ) ) { |
| 488 |
update_post_meta( $post->ID, 'r8_tsm_script_page', $_POST['r8_tsm_script_page'] ); |
| 489 |
} |
| 490 |
|
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
} |
| 495 |
|
| 496 |
public function tracking_scripts_create_menu() { |
| 497 |
add_menu_page( 'Tracking Script Manager', 'Tracking Script Manager', 'manage_options', 'edit.php?post_type=r8_tracking_scripts', null ); |
| 498 |
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 ); |
| 499 |
} |
| 500 |
|
| 501 |
// Admin Scripts |
| 502 |
public function tracking_scripts_admin_scripts() { |
| 503 |
wp_enqueue_script('jquery'); |
| 504 |
|
| 505 |
wp_enqueue_script( 'tracking_script_js', plugin_dir_url(__FILE__) . '/js/built.min.js', array(), '', true ); |
| 506 |
wp_localize_script( 'tracking_script_js', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
// Ajax Functions |
| 511 |
public function tracking_scripts_posts_ajax_handler() { |
| 512 |
$post_type = ($_POST['postType']) ? esc_attr($_POST['postType']) : 'post'; |
| 513 |
|
| 514 |
$args = array( |
| 515 |
'post_type' => $post_type, |
| 516 |
'posts_per_page' => -1, |
| 517 |
'orderby' => 'name', |
| 518 |
'order' => 'ASC' |
| 519 |
); |
| 520 |
|
| 521 |
ob_start(); |
| 522 |
|
| 523 |
$query = new WP_Query($args); |
| 524 |
echo '<option value="none" id="none">Choose '.ucwords($post_type).'</option>'; |
| 525 |
while( $query->have_posts() ) : $query->the_post(); |
| 526 |
echo '<option value="'.get_the_ID().'" id="'.get_the_ID().'">'.ucwords(get_the_title()).'</option>'; |
| 527 |
endwhile; |
| 528 |
wp_reset_postdata(); |
| 529 |
|
| 530 |
echo ob_get_clean(); |
| 531 |
die(); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
function tracking_scripts() { |
| 536 |
|
| 537 |
// globals |
| 538 |
global $tracking_scripts; |
| 539 |
|
| 540 |
|
| 541 |
// initialize |
| 542 |
if ( ! isset($tracking_scripts) ) { |
| 543 |
$tracking_scripts = new Tracking_Scripts(); |
| 544 |
$tracking_scripts->initialize(); |
| 545 |
} |
| 546 |
|
| 547 |
// return |
| 548 |
return $tracking_scripts; |
| 549 |
|
| 550 |
} |
| 551 |
|
| 552 |
// initialize |
| 553 |
tracking_scripts(); |
| 554 |
} |