Plugin Directory

Changeset 3327525


Ignore:
Timestamp:
07/14/2025 12:25:31 PM (9 months ago)
Author:
inboundhorizons
Message:

1.0.9 - 2025-07-14

  • FIX: Added code to set tracking info by JS if not set via HTTP. Required for cached pages.
Location:
universal-wp-lead-tracking
Files:
14 added
2 edited

Legend:

Unmodified
Added
Removed
  • universal-wp-lead-tracking/trunk/readme.txt

    r3324508 r3327525  
    66License: MIT
    77Tested up to: 6.8
    8 Stable tag: 1.0.8
     8Stable tag: 1.0.9
    99
    1010Adds lead tracking information to e-mails coming from Contact Form 7, Gravity Forms, Ninja Forms, or Elementor PRO form submissions.
     
    5252== Changelog ==
    5353
     54= 1.0.9 - 2025-07-14 =
     55* FIX: Added code to set tracking info by JS if not set via HTTP. Required for cached pages.
     56
    5457= 1.0.8 - 2025-07-08 =
    5558* FIX: Landing URLs could sometimes be set to /wp-json/ or /wp-admin/admin-ajax.php which is incorrect. Added a check to exclude ADMIN pages, AJAX requests, REST requests, and WP-CRON requests.
  • universal-wp-lead-tracking/trunk/universal-wp-lead-tracking.php

    r3324508 r3327525  
    33    Plugin Name: Universal WP Lead Tracking
    44    Description: Adds tracking info to outgoing emails from contact forms by using the [tracking-info] shortcode. The tracking info includes the Form Page URL, Original Referrer, Landing Page, User IP, Browser. Compatible with Contact Form 7, Gravity Forms, Ninja Forms, and Elementor PRO forms!
    5     Version: 1.0.8
     5    Version: 1.0.9
    66    Author: Inbound Horizons
    77    Author URI: https://www.inboundhorizons.com/
     
    4343           
    4444           
     45           
     46           
    4547            // Set landing information immediately (using priority 0 to ensure it runs first)
    46             add_action('init', 'UWPLT_SetLandingInfo', 0);
     48            add_action('init', array($this, 'UWPLT_SetLandingInfo'), 1);
     49           
     50            // Use JS to set tracking info if needed
     51            add_action('wp_head', array($this, 'UWPLT_SetTrackingJS'));
     52            //add_action('admin_head', array($this, 'UWPLT_SetTrackingJS'));    // For dev purposes
     53           
     54            add_action('wp_ajax_nopriv_UWPLT_SET_LANDING_INFO', array($this, 'AJAX_SetLandingInfo'));
     55            //add_action('wp_ajax_UWPLT_SET_LANDING_INFO', array($this, 'AJAX_SetLandingInfo'));    // For dev purposes
    4756           
    4857        }
     
    646655
    647656
     657       
     658       
    648659
    649660        public static function GetReferringURL() {
     
    666677   
    667678       
    668        
    669        
     679        public function AJAX_SetLandingInfo() {
     680           
     681            $response_data = array();
     682           
     683            $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
     684            $nonce_verified = wp_verify_nonce($nonce, $this->NONCE_ADMIN);
     685           
     686            if ($nonce_verified) {
     687           
     688                // Get the data
     689                $referring_url = isset($_POST['referring_url']) ? sanitize_text_field(wp_unslash($_POST['referring_url'])) : '';
     690                $current_url = isset($_POST['current_url']) ? sanitize_text_field(wp_unslash($_POST['current_url'])) : '';
     691               
     692                // Save the landing info
     693                $session_key = $this->UWPLT_SaveLandingInfo($referring_url, $current_url);
     694               
     695                // Pass the session key to the response data
     696                $response_data['session_key'] = $session_key;
     697            }
     698           
     699            wp_send_json_success($response_data);
     700        }
     701       
     702       
     703        public function UWPLT_SetTrackingJS() {
     704            global $UWPLT_COOKIE;
     705       
     706            $nonce = wp_create_nonce($this->NONCE_ADMIN);
     707           
     708            $js = '
     709               
     710                UWPLT_SetTrackingInfo();
     711               
     712                function UWPLT_SetTrackingInfo() {
     713               
     714                    // If the cookie is not set then POST the landing info
     715                    if (!document.cookie.includes("'.esc_js($UWPLT_COOKIE).'")) {
     716               
     717                        var data = new FormData();
     718                        data.append("action", "UWPLT_SET_LANDING_INFO");
     719                        data.append("nonce", "'.esc_js($nonce).'");
     720                        data.append("referring_url", document.referrer);
     721                        data.append("current_url", window.location.href);
     722                       
     723                        fetch("'.esc_js(admin_url('admin-ajax.php')).'", {
     724                            method: "POST",
     725                            body: data,
     726                        });
     727                    }
     728                }
     729               
     730            ';
     731           
     732            wp_register_script('uwplt-tracking', '', [], '', true);
     733            wp_enqueue_script('uwplt-tracking');
     734            wp_add_inline_script('uwplt-tracking', $js);
     735           
     736        }
     737       
     738        public function UWPLT_SetLandingInfo() {
     739            global $UWPLT_COOKIE;
     740           
     741           
     742            // Exclude ADMIN pages
     743            if (function_exists('is_admin') && is_admin()) {
     744                return;
     745            }
     746           
     747            // Exclude AJAX requests
     748            if (defined('DOING_AJAX') && DOING_AJAX) {
     749                return;
     750            }
     751           
     752            // Exclude REST requests
     753            if (defined('REST_REQUEST') && REST_REQUEST) {
     754                return;
     755            }
     756           
     757            // Exclude WP-CRON page requests
     758            // phpcs:ignore WordPress.Security.NonceVerification.Recommended
     759            if (isset($_GET['doing_wp_cron']) || (defined('DOING_CRON') && DOING_CRON)) {
     760                return;
     761            }
     762           
     763           
     764            // Get the values to save
     765            $original_ref = UWPLT::GetReferringURL();
     766            $landing_page = UWPLT::GetCurrentURL();
     767       
     768            // Save the landing info
     769            $session_key = $this->UWPLT_SaveLandingInfo($original_ref, $landing_page);
     770        }
     771       
     772        public function UWPLT_SaveLandingInfo($original_ref, $landing_page) {
     773            global $UWPLT_COOKIE;
     774           
     775            $session_key = false;
     776           
     777            // Check if the cookie has already been set
     778            if (!isset($_COOKIE[$UWPLT_COOKIE])) {
     779               
     780                // Package the values into an array
     781                $session = array(
     782                    'OriginalRef' => $original_ref,
     783                    'LandingPage' => $landing_page,
     784                );
     785               
     786                // Save the values
     787                $session_key = UWPLT_InsertSession($session);
     788               
     789                // Set the session cookie with the DB session key
     790                if (!headers_sent()) {  // Only if no headers sent to avoid an error
     791                    if ($session_key && ($UWPLT_COOKIE != '')) {
     792                        $_COOKIE[$UWPLT_COOKIE] = $session_key;
     793                       
     794                        setcookie($UWPLT_COOKIE, $session_key, array(
     795                            'expires' => time() + 21600,    // 21600 = 6 hours
     796                            'path' => '/',
     797                            'secure' => true,      // only over HTTPS
     798                            'httponly' => false,    // Ensure we can access with JS
     799                            'samesite' => 'Lax',
     800                        ));
     801                    }
     802                }
     803                else {
     804                    //error_log("UWPLT: Unable to set cookie because headers already sent.");
     805                    //error_log("GetCurrentURL: ".$landing_page);
     806                    //error_log("GetReferringURL: ".$original_ref);
     807                }
     808            }
     809           
     810            return ($session_key);
     811        }
    670812       
    671813       
     
    829971
    830972
    831 function UWPLT_SetLandingInfo() {
    832     global $UWPLT_COOKIE;
    833    
    834    
    835     // Exclude ADMIN pages
    836     if (function_exists('is_admin') && is_admin()) {
    837         return;
    838     }
    839    
    840     // Exclude AJAX requests
    841     if (defined('DOING_AJAX') && DOING_AJAX) {
    842         return;
    843     }
    844    
    845     // Exclude REST requests
    846     if (defined('REST_REQUEST') && REST_REQUEST) {
    847         return;
    848     }
    849    
    850     // Exclude WP-CRON page requests
    851     // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    852     if (isset($_GET['doing_wp_cron']) || (defined('DOING_CRON') && DOING_CRON)) {
    853         return;
    854     }
    855    
    856    
    857 
    858     if (!isset($_COOKIE[$UWPLT_COOKIE])) {
    859        
    860         // Get the values to save
    861         $original_ref = UWPLT::GetReferringURL();
    862         $landing_page = UWPLT::GetCurrentURL();
    863        
    864        
    865         // Package the values into an array
    866         $session = array(
    867             'OriginalRef' => $original_ref,
    868             'LandingPage' => $landing_page,
    869         );
    870        
    871        
    872         // Save the values
    873         $session_key = UWPLT_InsertSession($session);
    874        
    875        
    876         // Set the session cookie with the DB session key
    877         if (!headers_sent()) {  // Only if no headers sent to avoid an error
    878             if ($session_key && ($UWPLT_COOKIE != '')) {
    879                 $_COOKIE[$UWPLT_COOKIE] = $session_key;
    880                
    881                 setcookie($UWPLT_COOKIE, $session_key, array(
    882                     'expires' => time() + 21600,    // 21600 = 6 hours
    883                     'path' => '/',
    884                     'secure' => true,      // only over HTTPS
    885                     'httponly' => true,    // inaccessible to JavaScript
    886                     'samesite' => 'Lax',
    887                 ));
    888             }
    889         }
    890     }
    891 }
    892 
    893973add_action('init', 'UWPLT_SetShortcode');
    894974function UWPLT_SetShortcode() {
Note: See TracChangeset for help on using the changeset viewer.