Changeset 2760412
- Timestamp:
- 07/22/2022 05:37:31 PM (4 years ago)
- Location:
- tracking-script-manager/trunk
- Files:
-
- 2 edited
-
classes/class-process-tracking-scripts.php (modified) (1 diff)
-
tracking-scripts-manager.php (modified) (33 diffs)
Legend:
- Unmodified
- Added
- Removed
-
tracking-script-manager/trunk/classes/class-process-tracking-scripts.php
r2717706 r2760412 1 <?php 1 <?php 2 2 3 if ( ! class_exists( 'TSM_Process_Tracking_Scripts' ) ) { 3 if ( ! class_exists( 'TSM_Process_Tracking_Scripts' ) ) { 4 4 5 class TSM_Process_Tracking_Scripts extends WP_Background_Process {5 class TSM_Process_Tracking_Scripts extends WP_Background_Process { 6 6 7 /** 8 * @var string 9 */ 10 protected $action = 'process_tracking_scripts'; 11 /** 12 * Task 13 * 14 * Override this method to perform any actions required on each 15 * queue item. Return the modified item for further processing 16 * in the next pass through. Or, return false to remove the 17 * item from the queue. 18 * 19 * @param mixed $item Queue item to iterate over 20 * 21 * @return mixed 22 */ 23 protected function task( $script ) { 7 /** 8 * @var string 9 */ 10 protected $action = 'process_tracking_scripts'; 11 /** 12 * Task 13 * 14 * Override this method to perform any actions required on each 15 * queue item. Return the modified item for further processing 16 * in the next pass through. Or, return false to remove the 17 * item from the queue. 18 * 19 * @param mixed $item Queue item to iterate over 20 * 21 * @return mixed 22 */ 23 protected function task( $script ) { 24 $script_post = array( 25 'post_type' => 'r8_tracking_scripts', 26 'post_title' => $script->script_name, 27 'post_content' => '', 28 'post_status' => 'publish', 29 'meta_input' => array( 30 'r8_tsm_script_code' => $script->script_code, 31 'r8_tsm_active' => $script->active ? 'active' : 'inactive', 32 'r8_tsm_script_order' => $script->order ? $script->order : 1, 33 'r8_tsm_script_location' => $script->location, 34 'r8_tsm_script_page' => $script->page_id ? array( $script->page_id ) : array(), 35 ), 36 ); 24 37 25 $script_post = array( 26 'post_type' => 'r8_tracking_scripts', 27 'post_title' => $script->script_name, 28 'post_content' => '', 29 'post_status' => 'publish', 30 'meta_input' => array( 31 'r8_tsm_script_code' => $script->script_code, 32 'r8_tsm_active' => $script->active ? 'active' : 'inactive', 33 'r8_tsm_script_order' => $script->order ? $script->order : 1, 34 'r8_tsm_script_location' => $script->location, 35 'r8_tsm_script_page' => $script->page_id ? array($script->page_id) : array() 36 ), 37 ); 38 $post_id = wp_insert_post( $script_post ); 38 39 39 $post_id = wp_insert_post( $script_post ); 40 return false; 41 } 40 42 41 return false; 42 } 43 /** 44 * Complete 45 * 46 * Override if applicable, but ensure that the below actions are 47 * performed, or, call parent::complete(). 48 */ 49 protected function complete() { 50 parent::complete(); 51 // Show notice to user or perform some other arbitrary task... 43 52 44 /** 45 * Complete 46 * 47 * Override if applicable, but ensure that the below actions are 48 * performed, or, call parent::complete(). 49 */ 50 protected function complete() { 51 parent::complete(); 52 // Show notice to user or perform some other arbitrary task... 53 54 delete_option( 'header_tracking_script_code' ); 55 delete_option( 'page_tracking_script_code' ); 56 delete_option( 'footer_tracking_script_code' ); 57 delete_option( 'tsm_is_processing' ); 58 } 53 delete_option( 'header_tracking_script_code' ); 54 delete_option( 'page_tracking_script_code' ); 55 delete_option( 'footer_tracking_script_code' ); 56 delete_option( 'tsm_is_processing' ); 57 } 59 58 60 }59 } 61 60 62 61 } -
tracking-script-manager/trunk/tracking-scripts-manager.php
r2717706 r2760412 56 56 add_action( 'add_meta_boxes', array( $this, 'add_script_metaboxes' ) ); 57 57 add_action( 'wp_ajax_tracking_scripts_get_posts', array( $this, 'tracking_scripts_posts_ajax_handler' ) ); 58 add_action( 'manage_r8_tracking_scripts_posts_custom_column', array( 59 $this, 60 'tracking_script_column_content' 61 ), 10, 2 ); 58 add_action( 59 'manage_r8_tracking_scripts_posts_custom_column', 60 array( 61 $this, 62 'tracking_script_column_content', 63 ), 64 10, 65 2 66 ); 62 67 add_action( 'wp_body_open', array( $this, 'find_page_tracking_codes' ) ); 63 68 add_action( 'tsm_page_scripts', array( $this, 'find_page_tracking_codes' ) ); … … 65 70 add_action( 'admin_notices', array( $this, 'admin_notices' ) ); 66 71 add_action( 'admin_notices', array( $this, 'new_update_admin_notice' ) ); 67 add_action( 'admin_init', array( $this, "init_update_admin_notice") );72 add_action( 'admin_init', array( $this, 'init_update_admin_notice' ) ); 68 73 // fallback for page scripts if wp_body_open action isn't supported 69 add_action( 'get_footer', function () { 70 if ( did_action( 'wp_body_open' ) === 0 ) { 71 add_action( 'wp_footer', array( $this, 'find_page_tracking_codes' ) ); 72 } 73 } ); 74 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 ); 74 82 // Filters 75 83 add_filter( 'manage_r8_tracking_scripts_posts_columns', array( $this, 'add_tracking_script_columns' ) ); 76 add_filter( 'manage_edit-r8_tracking_scripts_sortable_columns', array( 77 $this, 78 'tracking_scripts_column_sort' 79 ) ); 84 add_filter( 85 'manage_edit-r8_tracking_scripts_sortable_columns', 86 array( 87 $this, 88 'tracking_scripts_column_sort', 89 ) 90 ); 80 91 // Includes 81 92 require_once plugin_dir_path( __FILE__ ) . 'classes/wp-async-request.php'; … … 89 100 **************************************************/ 90 101 public function process_handler() { 91 92 102 if ( ! isset( $_GET['tsm_update_scripts'] ) || ! isset( $_GET['_wpnonce'] ) ) { 93 103 return; 94 104 } 95 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'tsm_update_scripts' ) ) {105 if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_GET['_wpnonce'] ) ), 'tsm_update_scripts' ) ) { 96 106 return; 97 107 } … … 105 115 $scripts = $this->get_tracking_scripts(); 106 116 if ( ! empty( $scripts ) ) { 107 108 117 foreach ( $scripts as $script ) { 109 118 $this->process_all->push_to_queue( $script ); … … 113 122 } 114 123 124 115 125 protected function get_tracking_scripts() { 116 117 126 $scripts = array(); 118 $header_scripts = get_option( 'header_tracking_script_code' ) ? unserialize( get_option( 'header_tracking_script_code' ) ) : null;119 $page_scripts = get_option( 'page_tracking_script_code' ) ? unserialize( get_option( 'page_tracking_script_code' ) ) : null;120 $footer_scripts = get_option( 'footer_tracking_script_code' ) ? unserialize( get_option( 'footer_tracking_script_code' ) ) : null;127 $header_scripts = get_option( 'header_tracking_script_code' ) ? json_decode( get_option( 'header_tracking_script_code' ) ) : null; 128 $page_scripts = get_option( 'page_tracking_script_code' ) ? json_decode( get_option( 'page_tracking_script_code' ) ) : null; 129 $footer_scripts = get_option( 'footer_tracking_script_code' ) ? json_decode( get_option( 'footer_tracking_script_code' ) ) : null; 121 130 if ( ! empty( $header_scripts ) ) { 122 131 $scripts = array_merge( $scripts, $header_scripts ); … … 138 147 $footer_scripts = get_option( 'footer_tracking_script_code' ); 139 148 $is_processing = get_option( 'tsm_is_processing' ); 140 $has_tracking_scripts = ( $header_scripts || $page_scripts || $footer_scripts ) ? true : false;141 $is_admin = current_user_can( 'manage_options' ) ? true : false;149 $has_tracking_scripts = $header_scripts || $page_scripts || $footer_scripts; 150 $is_admin = current_user_can( 'manage_options' ); 142 151 if ( $has_tracking_scripts && $is_processing && $is_admin ) { 143 152 $message = __( 'Your scripts are currently processing. This may take several minutes. If you don’t see all of your scripts please wait a moment and refresh the page.', TRACKING_SCRIPT_TEXTDOMAIN ); 144 153 $notice = sprintf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); 145 echo $notice;154 echo esc_html( $notice ); 146 155 } 147 156 if ( $has_tracking_scripts && ! $is_processing && $is_admin ) { … … 149 158 $message = __( 'Tracking Scripts Manager has updated to a new version, click OK to update your scripts to the updated version.', TRACKING_SCRIPT_TEXTDOMAIN ); 150 159 $notice = sprintf( '<div class="%1$s"><p>%2$s</p><a class="button button-primary" href="%3$s" style="margin-bottom: .5em;">OK</a></div>', esc_attr( $class ), esc_html( $message ), esc_url( $url ) ); 151 echo $notice;160 echo esc_html( $notice ); 152 161 } 153 162 } … … 157 166 */ 158 167 public function init_update_admin_notice() { 159 160 168 $user_id = get_current_user_id(); 161 169 if ( isset( $_GET['tsm_update_notice_dismissed'] ) ) { … … 171 179 $user_id = get_current_user_id(); 172 180 if ( ! get_user_meta( $user_id, 'tsm_update_notice_dismissed' ) ) { 173 $url =admin_url()."edit.php?post_type=r8_tracking_scripts&tsm_update_notice_dismissed";181 $url = admin_url() . 'edit.php?post_type=r8_tracking_scripts&tsm_update_notice_dismissed'; 174 182 ?> 175 183 <div class="notice notice-success "> 176 <p><?php _e( 'An error in Tracking Scripts Manager 2.0.6 may have deactivated your scripts. Please click <a href="'.$url.'">here</a> to review the status of your scripts and reactivate them as needed.', ''); ?></p>184 <p><?php esc_html_e( 'An error in Tracking Scripts Manager 2.0.6 may have deactivated your scripts. Please click <a href="' . esc_url( $url ) . '">here</a> to review the status of your scripts and reactivate them as needed.', TRACKING_SCRIPT_TEXTDOMAIN ); ?></p> 177 185 <a href="?tsm_update_notice_dismissed">Dismiss this notice</a> 178 186 </div> … … 181 189 } 182 190 183 public function print_tsm_scripts( $script_id, $page, $page_id, $expiry_info ) { 184 $expiry_data = $this->expiry_data( $expiry_info ); 185 $if_expire = $this->check_expiry_script( $expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date'], $script_id ); 186 187 if ( $expiry_data['type'] == 'Schedule' ) { 188 if ( ! $if_expire ) { 189 if ( is_array( $page ) && in_array( intval( $page_id ), $page ) ) { 190 echo html_entity_decode( get_post_meta( $script_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252' ); 191 } elseif ( empty( $page ) ) { 192 echo html_entity_decode( get_post_meta( $script_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252' ); 193 } 194 } 195 } else { 196 if ( is_array( $page ) && in_array( intval( $page_id ), $page ) ) { 197 echo html_entity_decode( get_post_meta( $script_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252' ); 198 } elseif ( empty( $page ) ) { 199 echo html_entity_decode( get_post_meta( $script_id, 'r8_tsm_script_code', true ), ENT_QUOTES, 'cp1252' ); 191 192 193 public function print_tsm_scripts( $script_id, $page, $page_id, $expiry_info ) { 194 $expiry_data = $this->expiry_data( $expiry_info ); 195 $if_expire = $this->check_expiry_script( $expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date'], $script_id ); 196 $script = get_post_meta( $script_id, 'r8_tsm_script_code', true ); 197 198 $encoded_save = get_post_meta( $script_id, 'r8_tsm_encoded_save', true ); 199 if(!$encoded_save){ 200 $script=base64_encode($script); 201 $this->save_script($script_id,$script); 202 } 203 204 $page_script = $this->esc_script( $script ); 205 206 207 // Check if this is the right page 208 if ( ( is_array( $page ) && in_array( intval( $page_id ), $page, true ) ) || empty( $page ) ) { 209 // Is it scheduled and not expired or set never to expire? 210 if ( 'Schedule' === $expiry_data['type'] && ! $if_expire || 'Never' === $expiry_data['type'] ) { 211 // Render script 212 echo( $page_script ); 200 213 } 201 214 } … … 204 217 // Header Tracking Codes 205 218 function find_header_tracking_codes() { 206 207 219 global $wp_query; 208 $page_id = $wp_query->post->ID;209 $args = array(220 $page_id = $wp_query->post->ID; 221 $args = array( 210 222 'post_type' => 'r8_tracking_scripts', 211 223 'post_status' => 'publish', … … 219 231 'key' => 'r8_tsm_script_location', 220 232 'value' => 'header', 221 'compare' => '=' 233 'compare' => '=', 222 234 ), 223 235 array( 224 236 'key' => 'r8_tsm_active', 225 237 'value' => 'active', 226 'compare' => '=' 227 ) 228 ) 238 'compare' => '=', 239 ), 240 ), 229 241 ); 230 242 $header_scripts = new WP_Query( $args ); 231 243 if ( $header_scripts->have_posts() ) { 232 while ( $header_scripts->have_posts() ) : $header_scripts->the_post(); 244 while ( $header_scripts->have_posts() ) : 245 $header_scripts->the_post(); 233 246 $page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); 234 247 $expiry_info = get_post_meta( get_the_ID(), 'r8_tsm_script_expiry_info', true ); … … 240 253 241 254 function find_page_tracking_codes() { 242 243 255 global $wp_query; 244 $page_id = $wp_query->post->ID;245 $args = array(256 $page_id = $wp_query->post->ID; 257 $args = array( 246 258 'post_type' => 'r8_tracking_scripts', 247 259 'posts_per_page' => - 1, … … 255 267 'key' => 'r8_tsm_script_location', 256 268 'value' => 'page', 257 'compare' => '=' 269 'compare' => '=', 258 270 ), 259 271 array( 260 272 'key' => 'r8_tsm_active', 261 273 'value' => 'active', 262 'compare' => '=' 263 ) 264 ) 274 'compare' => '=', 275 ), 276 ), 265 277 ); 266 278 $page_scripts = new WP_Query( $args ); 267 279 if ( $page_scripts->have_posts() ) { 268 while ( $page_scripts->have_posts() ) : $page_scripts->the_post(); 280 while ( $page_scripts->have_posts() ) : 281 $page_scripts->the_post(); 269 282 $page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); 270 283 $expiry_info = get_post_meta( get_the_ID(), 'r8_tsm_script_expiry_info', true ); … … 277 290 function find_footer_tracking_codes() { 278 291 global $wp_query; 279 $page_id = $wp_query->post->ID;280 $args = array(292 $page_id = $wp_query->post->ID; 293 $args = array( 281 294 'post_type' => 'r8_tracking_scripts', 282 295 'posts_per_page' => - 1, … … 290 303 'key' => 'r8_tsm_script_location', 291 304 'value' => 'footer', 292 'compare' => '=' 305 'compare' => '=', 293 306 ), 294 307 array( 295 308 'key' => 'r8_tsm_active', 296 309 'value' => 'active', 297 'compare' => '=' 298 ) 299 ) 310 'compare' => '=', 311 ), 312 ), 300 313 ); 301 314 $footer_scripts = new WP_Query( $args ); 302 315 if ( $footer_scripts->have_posts() ) { 303 while ( $footer_scripts->have_posts() ) : $footer_scripts->the_post(); 316 while ( $footer_scripts->have_posts() ) : 317 $footer_scripts->the_post(); 304 318 $page = get_post_meta( get_the_ID(), 'r8_tsm_script_page', true ); 305 319 $expiry_info = get_post_meta( get_the_ID(), 'r8_tsm_script_expiry_info', true ); … … 311 325 312 326 function add_tracking_script_columns( $columns ) { 313 314 327 $columns = array( 315 328 'cb' => '<input type="checkbox" />', … … 318 331 'location' => __( 'Location', TRACKING_SCRIPT_TEXTDOMAIN ), 319 332 'status' => __( 'Status', TRACKING_SCRIPT_TEXTDOMAIN ), 320 'schedule' => __( 'Schedule', TRACKING_SCRIPT_TEXTDOMAIN ),333 'schedule' => __( 'Schedule', TRACKING_SCRIPT_TEXTDOMAIN ), 321 334 ); 322 335 … … 325 338 326 339 function tracking_script_column_content( $column_name, $post_ID ) { 327 $expiry_info = get_post_meta( $post_ID, 'r8_tsm_script_expiry_info', true );328 $expiry_data = $this->expiry_data( $expiry_info );340 $expiry_info = get_post_meta( $post_ID, 'r8_tsm_script_expiry_info', true ); 341 $expiry_data = $this->expiry_data( $expiry_info ); 329 342 $if_expire = $this->check_expiry_script( $expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date'], $post_ID ); 330 $scheduled_status = $this->scheduled_status( $if_expire, $expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date']);331 343 $scheduled_status = $this->scheduled_status( $if_expire, $expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date'] ); 344 332 345 if ( $column_name === 'status' ) { 333 346 $active = get_post_meta( $post_ID, 'r8_tsm_active', true ); 334 echo ( $active == 'inactive') ? '<span class="expired">' : '<span>';335 if ( $active === 'active' ) {336 echo 'Active';337 } else {338 echo 'Inactive';339 }340 echo $scheduled_status;347 echo ( $active === 'inactive' ) ? '<span class="expired">' : '<span>'; 348 if ( $active === 'active' ) { 349 echo 'Active'; 350 } else { 351 echo 'Inactive'; 352 } 353 echo esc_attr( $scheduled_status ); 341 354 echo '</span>'; 342 355 } … … 353 366 $location = get_post_meta( $post_ID, 'r8_tsm_script_location', true ); 354 367 if ( $location ) { 355 echo ucwords( $location);368 echo esc_html( ucwords( $location ) ); 356 369 } 357 370 } 358 371 if ( $column_name === 'schedule' ) { 359 360 if ( $expiry_data['type'] == 'Schedule' ) { 361 echo sprintf( __( 'Scheduled <b>%s</b> to <b>%s</b>', TRACKING_SCRIPT_TEXTDOMAIN ), $expiry_data['start_date'], $expiry_data['end_date'] ); 372 if ( $expiry_data['type'] === 'Schedule' ) { 373 echo esc_html( 374 sprintf( 375 __( 'Scheduled <b>%1$s</b> to <b>%2$s</b>', TRACKING_SCRIPT_TEXTDOMAIN ), 376 ( $expiry_data['start_date'] ), 377 ( $expiry_data['end_date'] ) 378 ) 379 ); 362 380 } else { 363 e cho __('Never expires', TRACKING_SCRIPT_TEXTDOMAIN);364 } 381 esc_html_e( 'Never expires', TRACKING_SCRIPT_TEXTDOMAIN ); 382 } 365 383 } 366 384 } 367 385 368 386 function tracking_scripts_column_sort( $columns ) { 369 370 387 $columns['global'] = 'global'; 371 388 $columns['location'] = 'location'; 372 389 $columns['status'] = 'status'; 373 $columns['schedule'] = 'schedule';390 $columns['schedule'] = 'schedule'; 374 391 375 392 return $columns; … … 377 394 378 395 public function add_script_metaboxes() { 379 380 add_meta_box( 'r8_tsm_script_code_wrapper', __( 'Script Code', TRACKING_SCRIPT_TEXTDOMAIN ), array( 381 $this, 382 'script_code_metabox' 383 ), 'r8_tracking_scripts', 'normal' ); 384 add_meta_box( 'r8_tsm_script_active', __( 'Script Status', TRACKING_SCRIPT_TEXTDOMAIN ), array( 385 $this, 386 'script_active_metabox' 387 ), 'r8_tracking_scripts', 'side' ); 388 add_meta_box( 'r8_tsm_script_expiry', __( 'Schedule', TRACKING_SCRIPT_TEXTDOMAIN ), array( 389 $this, 390 'script_expiry_metabox' 391 ), 'r8_tracking_scripts', 'side' ); 392 add_meta_box( 'r8_tsm_script_order', __( 'Script Order', TRACKING_SCRIPT_TEXTDOMAIN ), array( 393 $this, 394 'script_order_metabox' 395 ), 'r8_tracking_scripts', 'side' ); 396 add_meta_box( 'r8_tsm_script_location', __( 'Script Location', TRACKING_SCRIPT_TEXTDOMAIN ), array( 397 $this, 398 'script_location_metabox' 399 ), 'r8_tracking_scripts', 'normal' ); 400 add_meta_box( 'r8_tsm_script_page', __( 'Specific Script Placement (Page(s) or Post(s))', TRACKING_SCRIPT_TEXTDOMAIN ), array( 401 $this, 402 'script_page_metabox' 403 ), 'r8_tracking_scripts', 'normal' ); 396 add_meta_box( 397 'r8_tsm_script_code_wrapper', 398 __( 'Script Code', TRACKING_SCRIPT_TEXTDOMAIN ), 399 array( 400 $this, 401 'script_code_metabox', 402 ), 403 'r8_tracking_scripts', 404 'normal' 405 ); 406 add_meta_box( 407 'r8_tsm_script_active', 408 __( 'Script Status', TRACKING_SCRIPT_TEXTDOMAIN ), 409 array( 410 $this, 411 'script_active_metabox', 412 ), 413 'r8_tracking_scripts', 414 'side' 415 ); 416 add_meta_box( 417 'r8_tsm_script_expiry', 418 __( 'Schedule', TRACKING_SCRIPT_TEXTDOMAIN ), 419 array( 420 $this, 421 'script_expiry_metabox', 422 ), 423 'r8_tracking_scripts', 424 'side' 425 ); 426 add_meta_box( 427 'r8_tsm_script_order', 428 __( 'Script Order', TRACKING_SCRIPT_TEXTDOMAIN ), 429 array( 430 $this, 431 'script_order_metabox', 432 ), 433 'r8_tracking_scripts', 434 'side' 435 ); 436 add_meta_box( 437 'r8_tsm_script_location', 438 __( 'Script Location', TRACKING_SCRIPT_TEXTDOMAIN ), 439 array( 440 $this, 441 'script_location_metabox', 442 ), 443 'r8_tracking_scripts', 444 'normal' 445 ); 446 add_meta_box( 447 'r8_tsm_script_page', 448 __( 'Specific Script Placement (Page(s) or Post(s))', TRACKING_SCRIPT_TEXTDOMAIN ), 449 array( 450 $this, 451 'script_page_metabox', 452 ), 453 'r8_tracking_scripts', 454 'normal' 455 ); 404 456 } 405 457 406 458 function script_code_metabox() { 407 408 459 global $post; 409 460 $script_code = get_post_meta( $post->ID, 'r8_tsm_script_code', true ); 410 include_once( TRACKING_SCRIPT_DIR_PATH . '/templates/script-code-metabox.php' ); 411 } 412 413 function script_active_metabox() { 461 /** 462 * Check if script was saved using base64 encode 463 */ 464 $encoded_save = get_post_meta( $post->ID, 'r8_tsm_encoded_save', true ); 465 if ( !$encoded_save ) { 466 $script_code=base64_encode($script_code); 467 $this->save_script($post->ID,$script_code); 468 } 469 470 if($this->is_file_modification_allowed()){ 471 ?> 472 <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;}"> 473 <h1>Heads up!</h1> 474 <p> 475 Adding custom scripts is not recommended and could break your site. 476 </p> 477 <p> 478 Please double check that the code you are adding is secure and make sure your WordPress site is backed up 479 in the likely event that something breaks.</p> 480 481 <p> 482 <button type="button" class="button button-primary consent">I understand</button> 483 </p> 484 485 </div> 486 <script type="text/javascript"> 487 jQuery(function($){ 488 $(".red8_script_notice button.consent").on("click",function(){ 489 $(".red8_script_notice").hide(); 490 $("#red8_code_editor_wrapper") 491 .css("opacity",1) 492 .css('height','auto'); 493 494 }) 495 }) 496 </script> 497 498 <div id="red8_code_editor_wrapper" style="opacity: 0; height: 0;"> 499 <textarea name="r8_tsm_script_code" id="r8_tsm_script_code" rows="5" ><?php 500 if ( $script_code ) { 501 echo stripslashes(html_entity_decode( base64_decode($script_code), ENT_QUOTES, 'cp1252' )); 502 } 503 ?></textarea> 504 </div> 505 506 <?php 507 } 508 else{ 509 ?> 510 <div class="notice notice-error "> 511 <p>File modification & custom scripts have been disallowed by your WordPress config.</p> 512 </div> 513 <?php 514 } 515 516 } 517 518 function script_active_metabox() { 414 519 global $post; 415 $active = get_post_meta( $post->ID, 'r8_tsm_active', true );416 $expiry_info = get_post_meta( $post->ID, 'r8_tsm_script_expiry_info', true );417 $expiry_data = $this->expiry_data( $expiry_info );520 $active = get_post_meta( $post->ID, 'r8_tsm_active', true ); 521 $expiry_info = get_post_meta( $post->ID, 'r8_tsm_script_expiry_info', true ); 522 $expiry_data = $this->expiry_data( $expiry_info ); 418 523 $if_expire = $this->check_expiry_script( $expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date'], $post->ID ); 419 $scheduled_status = $this->scheduled_status( $if_expire, $expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date']);420 421 include_once ( TRACKING_SCRIPT_DIR_PATH . '/templates/script-active-metabox.php' );524 $scheduled_status = $this->scheduled_status( $if_expire, $expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date'] ); 525 526 include_once TRACKING_SCRIPT_DIR_PATH . '/templates/script-active-metabox.php'; 422 527 } 423 528 424 529 function script_expiry_metabox() { 425 530 global $post; 426 $expiry_info = get_post_meta( $post->ID, 'r8_tsm_script_expiry_info', true );427 $expiry_data = $this->expiry_data( $expiry_info );531 $expiry_info = get_post_meta( $post->ID, 'r8_tsm_script_expiry_info', true ); 532 $expiry_data = $this->expiry_data( $expiry_info ); 428 533 $if_expire = $this->check_expiry_script( $expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date'], $post->ID ); 429 $scheduled_status = $this->scheduled_status( $if_expire, $expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date']);430 include_once ( TRACKING_SCRIPT_DIR_PATH . '/templates/script-expiry-metabox.php' );534 $scheduled_status = $this->scheduled_status( $if_expire, $expiry_data['type'], $expiry_data['start_date'], $expiry_data['end_date'] ); 535 include_once TRACKING_SCRIPT_DIR_PATH . '/templates/script-expiry-metabox.php'; 431 536 } 432 537 433 538 function script_order_metabox() { 434 435 539 global $post; 436 540 $order = get_post_meta( $post->ID, 'r8_tsm_script_order', true ); 437 include_once ( TRACKING_SCRIPT_DIR_PATH . '/templates/script-order-metabox.php' );541 include_once TRACKING_SCRIPT_DIR_PATH . '/templates/script-order-metabox.php'; 438 542 } 439 543 440 544 function script_location_metabox() { 441 442 545 global $post; 443 546 $location = get_post_meta( $post->ID, 'r8_tsm_script_location', true ); 444 include_once ( TRACKING_SCRIPT_DIR_PATH . '/templates/script-location-metabox.php' );547 include_once TRACKING_SCRIPT_DIR_PATH . '/templates/script-location-metabox.php'; 445 548 } 446 549 447 550 function script_page_metabox() { 448 449 551 global $post; 450 552 $script_page = get_post_meta( $post->ID, 'r8_tsm_script_page', true ); 451 include_once ( TRACKING_SCRIPT_DIR_PATH . '/templates/script-page-metabox.php' );452 } 553 include_once TRACKING_SCRIPT_DIR_PATH . '/templates/script-page-metabox.php'; 554 } 453 555 454 556 public function get_date_time( $timespan, $format ) { … … 460 562 } 461 563 462 public function check_expiry_script( $expiry_date_type, $expiry_start_date, $expiry_end_date, $script_id ) { 564 public function check_expiry_script( $expiry_date_type, $expiry_start_date, $expiry_end_date, $script_id ) { 463 565 $result = false; 464 if ( $expiry_date_type == 'Never' ) {566 if ( $expiry_date_type === 'Never' ) { 465 567 return $result; 466 } 467 if ( empty( $expiry_start_date) ) {568 } 569 if ( empty( $expiry_start_date ) ) { 468 570 return $result; 469 } 470 if ( empty( $expiry_end_date) ) {571 } 572 if ( empty( $expiry_end_date ) ) { 471 573 return $result; 472 } 473 474 $date_range = [];574 } 575 576 $date_range = array(); 475 577 $start_time = $expiry_start_date; 476 $interval = new DateInterval('P1D');477 $end_time = new DateTime( $expiry_end_date );478 $end_time->add( $interval);479 $period = new DatePeriod(new DateTime($start_time), $interval, $end_time);480 $today = new DateTime();481 $today_date = $today->format( 'Y-m-d');482 foreach ( $period as $key => $value) {483 $array[] = $value->format( 'Y-m-d');484 } 485 if ( ! in_array( $today_date, $array ) ) {578 $interval = new DateInterval( 'P1D' ); 579 $end_time = new DateTime( $expiry_end_date ); 580 $end_time->add( $interval ); 581 $period = new DatePeriod( new DateTime( $start_time ), $interval, $end_time ); 582 $today = new DateTime(); 583 $today_date = $today->format( 'Y-m-d' ); 584 foreach ( $period as $key => $value ) { 585 $array[] = $value->format( 'Y-m-d' ); 586 } 587 if ( ! in_array( $today_date, $array, true ) ) { 486 588 $result = true; 487 589 } 488 $this->set_script_status( $script_id, $result ); 590 $this->set_script_status( $script_id, $result ); 489 591 return $result; 490 592 } … … 494 596 if ( ! empty( $post->post_type ) ) { 495 597 if ( $post->post_type === 'r8_tracking_scripts' ) { 496 if ( $script_id == $post->ID ){598 if ( $script_id === $post->ID ) { 497 599 $active = get_post_meta( $post->ID, 'r8_tsm_active', true ); 498 if ( $result == true ){ // expire true600 if ( $result === true ) { // expire true 499 601 if ( 'active' === $active ) { 500 602 update_post_meta( $post->ID, 'r8_tsm_active', 'inactive' ); 501 603 } 502 } else{ // expire false604 } else { // expire false 503 605 if ( 'inactive' === $active ) { 504 606 update_post_meta( $post->ID, 'r8_tsm_active', 'active' ); 505 607 } 506 608 } 507 508 } 509 } 510 } 511 } 512 513 public function expiry_data($expiry_info){ 514 $type = is_object( $expiry_info ) ? $expiry_info->type : 'Never'; 515 $start_date = is_object( $expiry_info ) ? $expiry_info->schedule_start : ''; 516 $end_date = is_object( $expiry_info ) ? $expiry_info->schedule_end : ''; 517 return [ 518 'type' => $type, 609 } 610 } 611 } 612 } 613 614 public function expiry_data( $expiry_info ) { 615 $type = is_object( $expiry_info ) ? $expiry_info->type : 'Never'; 616 $start_date = is_object( $expiry_info ) ? $expiry_info->schedule_start : ''; 617 $end_date = is_object( $expiry_info ) ? $expiry_info->schedule_end : ''; 618 return array( 619 'type' => $type, 519 620 'start_date' => $start_date, 520 'end_date' => $end_date,521 ];522 } 523 524 public function scheduled_status( $if_expire, $expiry_date_type, $expiry_start_date, $expiry_end_date){621 'end_date' => $end_date, 622 ); 623 } 624 625 public function scheduled_status( $if_expire, $expiry_date_type, $expiry_start_date, $expiry_end_date ) { 525 626 $status = ''; 526 $start = new DateTime($expiry_start_date);527 $end = new DateTime($expiry_end_date);528 $today = new DateTime();529 if ( $expiry_date_type == 'Schedule' ) {530 if ( !$if_expire ){627 $start = new DateTime( $expiry_start_date ); 628 $end = new DateTime( $expiry_end_date ); 629 $today = new DateTime(); 630 if ( $expiry_date_type === 'Schedule' ) { 631 if ( ! $if_expire ) { 531 632 $status = ''; 532 }else{ 533 if( $today < $start ) { 534 $diff = strtotime($today->format("y-m-d")) - strtotime($start->format("y-m-d")); 535 $count = abs(round($diff / 86400)); 536 $status = ' (Starting ' . sprintf( _n( 'tomorrow', 'in %s days', $count, 'text-domain' ), number_format_i18n( $count ) ) . ') '; 537 } 538 if( $today > $end ) $status = " (Expired)"; 633 } else { 634 if ( $today < $start ) { 635 $diff = strtotime( $today->format( 'y-m-d' ) ) - strtotime( $start->format( 'y-m-d' ) ); 636 $count = abs( round( $diff / 86400 ) ); 637 $next_date = sprintf( _n( 'tomorrow', 'in %s days', $count, 'tracking-scripts-manager' ), $count ); 638 $status = sprintf( '(Starting %s) ', $next_date ); 639 } 640 if ( $today > $end ) { 641 $status = ' (Expired)'; 642 } 539 643 } 540 644 } … … 543 647 544 648 public function register_scripts_post_type() { 545 546 649 $labels = array( 547 650 'name' => _x( 'Tracking Scripts', TRACKING_SCRIPT_TEXTDOMAIN ), … … 558 661 'parent_item_colon' => __( 'Parent Tracking Scripts:', TRACKING_SCRIPT_TEXTDOMAIN ), 559 662 'not_found' => __( 'No Tracking Scripts found.', TRACKING_SCRIPT_TEXTDOMAIN ), 560 'not_found_in_trash' => __( 'No Tracking Scripts found in Trash.', TRACKING_SCRIPT_TEXTDOMAIN ) 561 ); 562 $args = array(663 'not_found_in_trash' => __( 'No Tracking Scripts found in Trash.', TRACKING_SCRIPT_TEXTDOMAIN ), 664 ); 665 $args = array( 563 666 'labels' => $labels, 564 667 'description' => __( 'Description.', TRACKING_SCRIPT_TEXTDOMAIN ), … … 578 681 'delete_posts' => 'manage_options', 579 682 'publish_posts' => 'manage_options', 580 'read_private_posts' => 'manage_options' 683 'read_private_posts' => 'manage_options', 581 684 ), 582 685 'has_archive' => false, … … 588 691 'script-active', 589 692 'script-location', 590 'script-order' 591 ) 693 'script-order', 694 ), 592 695 ); 593 696 register_post_type( 'r8_tracking_scripts', $args ); … … 601 704 if ( $hook === 'post.php' || $hook === 'post-new.php' ) { 602 705 if ( ! empty( $post->post_type ) && ( $post->post_type === 'r8_tracking_scripts' ) ) { 603 wp_enqueue_style( 'r8-tsm-edit-script', plugins_url( '/css/tracking-script-edit.css', __FILE__ ), [], md5_file( plugins_url( '/css/tracking-script-edit.css', __FILE__ ) ) ); 604 wp_enqueue_style( 'r8-tsm-select2-css', plugins_url( '/css/select2.min.css', __FILE__ ), [], md5_file( plugins_url( '/css/select2.min.css', __FILE__ ) ) ); 605 wp_enqueue_script( 'r8-tsm-select2-js', plugins_url( '/js/select2.min.js', __FILE__ ), [], md5_file( plugins_url( '/js/select2.min.js', __FILE__ ) ), true ); 606 wp_enqueue_script( 'r8-tsm-post-edit-js', plugins_url( '/js/post-edit.js', __FILE__ ), [ 607 'jquery', 608 'r8-tsm-select2-js' 609 ], md5_file( plugins_url( '/js/post-edit.js', __FILE__ ) ), true ); 706 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__ ) ) ); 707 wp_enqueue_style( 'r8-tsm-select2-css', plugins_url( '/css/select2.min.css', __FILE__ ), array(), md5_file( plugins_url( '/css/select2.min.css', __FILE__ ) ) ); 708 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 ); 709 wp_enqueue_script( 710 'r8-tsm-post-edit-js', 711 plugins_url( '/js/post-edit.js', __FILE__ ), 712 array( 713 'jquery', 714 'r8-tsm-select2-js', 715 ), 716 md5_file( plugins_url( '/js/post-edit.js', __FILE__ ) ), 717 true 718 ); 610 719 wp_enqueue_style( 'jquery-ui-css', 'https://code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css' ); 611 720 wp_enqueue_script( 'jquery-ui-datepicker' ); … … 614 723 if ( $hook === 'post.php' || $hook === 'edit.php' ) { 615 724 if ( ! empty( $post->post_type ) && ( $post->post_type === 'r8_tracking_scripts' ) ) { 616 wp_enqueue_style( 'r8-tsm-post-list', plugins_url( '/css/post-list.css', __FILE__ ), [], md5_file( plugins_url( '/css/post-list.css', __FILE__ ) ) );617 wp_enqueue_script( 'r8-tsm-post-list-js', plugins_url( '/js/post-list.js', __FILE__ ), ['jquery'], md5_file( plugins_url( '/js/post-list.js', __FILE__ ) ), true );725 wp_enqueue_style( 'r8-tsm-post-list', plugins_url( '/css/post-list.css', __FILE__ ), array(), md5_file( plugins_url( '/css/post-list.css', __FILE__ ) ) ); 726 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 ); 618 727 } 619 728 } 620 729 if ( ! empty( $post->post_type ) && ( $post->post_type === 'r8_tracking_scripts' ) ) { 621 // code eidtor support 622 $html_editor = wp_enqueue_code_editor( array( 'type' => 'text/html' ) ); 623 if (false !== $html_editor) { 624 wp_add_inline_script( 625 'code-editor', 626 sprintf( 627 'jQuery( function() { wp.codeEditor.initialize( "r8_tsm_script_code", %s ); } );', 628 wp_json_encode( $html_editor ) 629 ) 630 ); 631 } 632 } 730 // code editor support 731 $html_editor = wp_enqueue_code_editor( array( 'type' => 'text/html' ) ); 732 if ( false !== $html_editor ) { 733 wp_add_inline_script( 734 'code-editor', 735 sprintf( 736 'jQuery( function() { wp.codeEditor.initialize( "r8_tsm_script_code", %s ); } );', 737 wp_json_encode( $html_editor ) 738 ) 739 ); 740 } 741 } 742 } 743 744 private function esc_script( $script ) { 745 return stripslashes(html_entity_decode( base64_decode($script), ENT_QUOTES, 'cp1252' )); 746 } 747 748 private function save_script($post_id,$script_code){ 749 $script_code = stripslashes( wp_unslash($script_code)); 750 update_post_meta( $post_id, 'r8_tsm_script_code', $script_code ); 751 update_post_meta( $post_id, 'r8_tsm_encoded_save', true ); 752 } 753 754 755 private function is_file_modification_allowed(){ 756 if (defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS){ 757 return false; 758 } 759 return true; 633 760 } 634 761 635 762 function save_post() { 636 637 763 global $post; 638 764 if ( ! empty( $post->post_type ) ) { 639 765 if ( $post->post_type === 'r8_tracking_scripts' ) { 640 $expiry_obj = new \stdClass(); 641 $expiry_obj->schedule_start = ''; 642 $expiry_obj->schedule_end = ''; 643 $expiry_obj->type = ''; 644 // if ( ! empty( $_POST['r8_tsm_script_code'] ) ) { 645 update_post_meta( $post->ID, 'r8_tsm_script_code', stripslashes( esc_textarea( $_POST['r8_tsm_script_code'] ) ) ); 646 // } 766 $expiry_obj = new \stdClass(); 767 $expiry_obj->schedule_start = ''; 768 $expiry_obj->schedule_end = ''; 769 $expiry_obj->type = ''; 770 if ( ! empty( $_POST['r8_tsm_script_code'] ) ) { 771 $script_code = base64_encode($_POST['r8_tsm_script_code'] ); 772 $this->save_script($post->ID,$script_code); 773 774 } 647 775 if ( ! empty( $_POST['r8_tsm_active'] ) ) { 648 update_post_meta( $post->ID, 'r8_tsm_active', sanitize_text_field( $_POST['r8_tsm_active'] ) ); 776 $tsm_active = sanitize_text_field( wp_unslash( $_POST['r8_tsm_active'] ) ); 777 update_post_meta( $post->ID, 'r8_tsm_active', $tsm_active ); 649 778 } 650 779 if ( ! empty( $_POST['r8_tsm_script_order'] ) ) { … … 652 781 } 653 782 if ( ! empty( $_POST['r8_tsm_script_location'] ) ) { 654 update_post_meta( $post->ID, 'r8_tsm_script_location', sanitize_text_field( $_POST['r8_tsm_script_location']) );655 } 656 if ( ! empty( $_POST['r8_tsm_script_expiry'] ) || ( ! empty( $_POST['schedule_start'] ) && ! empty( $_POST['schedule_end'] ) ) ) { 657 $expiry_obj->type = $_POST['r8_tsm_script_expiry'] ?: 'Never';658 $expiry_obj->schedule_start = $_POST['schedule_start']?: '';659 $expiry_obj->schedule_end = $_POST['schedule_end'] ?: '';783 update_post_meta( $post->ID, 'r8_tsm_script_location', sanitize_text_field( wp_unslash( $_POST['r8_tsm_script_location'] ) ) ); 784 } 785 if ( ! empty( $_POST['r8_tsm_script_expiry'] ) || ( ! empty( $_POST['schedule_start'] ) && ! empty( $_POST['schedule_end'] ) ) ) { 786 $expiry_obj->type = sanitize_text_field( wp_unslash( $_POST['r8_tsm_script_expiry'] ) ) ? : 'Never'; 787 $expiry_obj->schedule_start = sanitize_text_field( wp_unslash( $_POST['schedule_start'] ) ) ?: ''; 788 $expiry_obj->schedule_end = sanitize_text_field( wp_unslash( $_POST['schedule_end'] ) ) ?: ''; 660 789 update_post_meta( $post->ID, 'r8_tsm_script_expiry_info', $expiry_obj ); 661 790 // status updated based on schedule 662 if ($expiry_obj->type == 'Schedule'){791 if ( $expiry_obj->type === 'Schedule' ) { 663 792 $this->check_expiry_script( $expiry_obj->type, $expiry_obj->schedule_start, $expiry_obj->schedule_end, $post->ID ); 664 793 } 665 794 } 666 795 if ( ! empty( $_POST['r8_tsm_script_page'] ) && is_array( $_POST['r8_tsm_script_page'] ) ) { 667 update_post_meta( $post->ID, 'r8_tsm_script_page', $_POST['r8_tsm_script_page']);796 update_post_meta( $post->ID, 'r8_tsm_script_page', sanitize_text_field( wp_unslash( $_POST['r8_tsm_script_page'] ) ) ); 668 797 } else { 669 798 update_post_meta( $post->ID, 'r8_tsm_script_page', array() ); … … 681 810 public function tracking_scripts_admin_scripts() { 682 811 wp_enqueue_script( 'jquery' ); 683 wp_enqueue_script( 'tracking_script_js', plugin_dir_url( __FILE__ ) . '/js/built.min.js', [], md5_file( plugin_dir_url( __FILE__ ) . '/js/built.min.js' ), true );812 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 ); 684 813 wp_localize_script( 'tracking_script_js', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); 685 814 } … … 687 816 // Ajax Functions 688 817 public function tracking_scripts_posts_ajax_handler() { 689 $post_type = ( $_POST['postType'] ) ? esc_attr( $_POST['postType']) : 'post';690 $args = array(818 $post_type = isset( $_POST['postType'] ) ? sanitize_text_field( wp_unslash( $_POST['postType'] ) ) : 'post'; 819 $args = array( 691 820 'post_type' => $post_type, 692 821 'posts_per_page' => - 1, 693 822 'orderby' => 'name', 694 'order' => 'ASC' 823 'order' => 'ASC', 695 824 ); 696 825 ob_start(); 697 826 $query = new WP_Query( $args ); 698 echo '<option value="none" id="none">Choose ' . ucwords( $post_type ) . '</option>'; 699 while ( $query->have_posts() ) : $query->the_post(); 700 echo '<option value="' . get_the_ID() . '" id="' . get_the_ID() . '">' . ucwords( get_the_title() ) . '</option>'; 827 echo '<option value="none" id="none">Choose ' . esc_html( ucwords( $post_type ) ) . '</option>'; 828 while ( $query->have_posts() ) : 829 $query->the_post(); 830 echo '<option value="' . esc_attr( get_the_ID() ) . '" id="' . esc_attr( get_the_ID() ) . '">' . esc_html( ucwords( get_the_title() ) ) . '</option>'; 701 831 endwhile; 702 832 wp_reset_postdata(); 703 echo ob_get_clean();833 echo esc_html( ob_get_clean() ); 704 834 die(); 705 835 } … … 723 853 tracking_scripts(); 724 854 } 725 // if ( ! class_exists( 'Tracking_Script' ) ) {726 727 // class Tracking_Script {728 // public $script_name;729 // public $script_code;730 // public $active;731 // public $order;732 // public $script_id;733 // public $location;734 // public $script_id;735 736 // function __construct() {737 738 // }739 // }740 // }
Note: See TracChangeset
for help on using the changeset viewer.