261 lines
8.5 KiB
PHP
261 lines
8.5 KiB
PHP
<?php
|
|
/**
|
|
* Tracking functionality class
|
|
*/
|
|
|
|
// Exit if accessed directly
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class Palantics_Tracking {
|
|
|
|
// Tracking server URL
|
|
private $tracking_server_url = 'tracking1.karlbreuer.com';
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
public function __construct() {
|
|
// Add the script to the header
|
|
add_action('wp_head', array($this, 'add_tracking_script'), 10);
|
|
|
|
// Register REST API endpoint
|
|
add_action('rest_api_init', array($this, 'register_rest_endpoint'));
|
|
|
|
// Add focused tracking for elements with specific classes
|
|
add_action('wp_footer', array($this, 'add_class_tracking_script'), 99);
|
|
|
|
// Add shortcode for manual tracking
|
|
add_shortcode('track_element', array($this, 'tracking_shortcode'));
|
|
|
|
// Register custom block for Gutenberg
|
|
add_action('init', array($this, 'register_tracking_block'));
|
|
}
|
|
|
|
/**
|
|
* Add tracking script to header
|
|
*/
|
|
public function add_tracking_script() {
|
|
// This is now handled by the Loader class using wp_enqueue_scripts
|
|
|
|
// Fallback for backward compatibility - will call pL() function
|
|
?>
|
|
<!-- Page Load Tracking Initialization -->
|
|
<script>
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
if (typeof pL === 'function') {
|
|
try {
|
|
pL();
|
|
} catch (error) {
|
|
console.error("Error in page load tracking:", error);
|
|
}
|
|
}
|
|
});
|
|
</script>
|
|
<?php
|
|
}
|
|
|
|
/**
|
|
* Add JavaScript to track elements with specific classes
|
|
*/
|
|
public function add_class_tracking_script() {
|
|
// This is now handled by the Loader class using wp_enqueue_scripts
|
|
// The function is kept for backward compatibility but doesn't output anything
|
|
}
|
|
|
|
/**
|
|
* Register REST API endpoint for tracking
|
|
*/
|
|
public function register_rest_endpoint() {
|
|
register_rest_route('palantics/v1', '/sun', array(
|
|
'methods' => 'POST',
|
|
'callback' => array($this, 'handle_rest_request'),
|
|
'permission_callback' => '__return_true', // Allow unauthenticated access
|
|
));
|
|
|
|
// Also register GET method for easier testing and compatibility
|
|
register_rest_route('palantics/v1', '/sun', array(
|
|
'methods' => 'GET',
|
|
'callback' => array($this, 'handle_rest_request'),
|
|
'permission_callback' => '__return_true', // Allow unauthenticated access
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Handle REST API request for tracking
|
|
*/
|
|
public function handle_rest_request($request) {
|
|
// Get all parameters from the request
|
|
$params = $request->get_params();
|
|
|
|
// Add IP address to the parameters
|
|
$params['ip'] = $this->get_real_ip();
|
|
|
|
// Forward tracking data to the tracking server
|
|
$this->forward_to_tracker($params, $request);
|
|
|
|
// Return an empty success response
|
|
return new WP_REST_Response(array('status' => 'success'), 200);
|
|
}
|
|
|
|
/**
|
|
* Shortcode for tracking elements
|
|
*/
|
|
public function tracking_shortcode($atts, $content = null) {
|
|
$atts = shortcode_atts(array(
|
|
'event' => 'tracked_element',
|
|
'class' => '',
|
|
'style' => '',
|
|
), $atts, 'track_element');
|
|
|
|
// Sanitize attributes
|
|
$event = esc_attr($atts['event']);
|
|
$class = esc_attr($atts['class']);
|
|
$style = esc_attr($atts['style']);
|
|
|
|
// Return the div with content and onclick attribute
|
|
return sprintf(
|
|
'<div class="palantics-tracking-wrapper %s" style="%s" onclick="tE(\'%s\')">%s</div>',
|
|
$class,
|
|
$style,
|
|
$event,
|
|
do_shortcode($content)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Register Gutenberg tracking block
|
|
*/
|
|
public function register_tracking_block() {
|
|
// Only register block if Gutenberg is available
|
|
if (!function_exists('register_block_type')) {
|
|
return;
|
|
}
|
|
|
|
// Register block script
|
|
wp_register_script(
|
|
'palantics-tracking-block-editor',
|
|
PALANTICS_PLUGIN_URL . 'assets/js/block.js',
|
|
array(
|
|
'wp-blocks',
|
|
'wp-element',
|
|
'wp-block-editor',
|
|
'wp-components',
|
|
'wp-i18n'
|
|
),
|
|
PALANTICS_PLUGIN_VERSION
|
|
);
|
|
|
|
// Register block styles
|
|
wp_register_style(
|
|
'palantics-tracking-block-editor-style',
|
|
PALANTICS_PLUGIN_URL . 'assets/css/tracking-block.css',
|
|
array(),
|
|
PALANTICS_PLUGIN_VERSION
|
|
);
|
|
|
|
// Register the block
|
|
register_block_type('palantics-tracking/track-element', array(
|
|
'editor_script' => 'palantics-tracking-block-editor',
|
|
'editor_style' => 'palantics-tracking-block-editor-style',
|
|
'attributes' => array(
|
|
'eventName' => array(
|
|
'type' => 'string',
|
|
'default' => 'tracked_element',
|
|
),
|
|
),
|
|
));
|
|
|
|
// Add inline script to ensure tE function is available in editor
|
|
wp_add_inline_script('palantics-tracking-block-editor', "
|
|
// Mock tE function for the editor if it doesn't exist
|
|
if (typeof window.tE === 'undefined') {
|
|
window.tE = function(eventName) {
|
|
console.log('Tracking event: ' + eventName);
|
|
};
|
|
}
|
|
");
|
|
}
|
|
|
|
/**
|
|
* Forward the request to the tracking server
|
|
*/
|
|
public function forward_to_tracker($params, $request) {
|
|
// Get tracking server URL from settings
|
|
$tracking_server_url = get_option('palantics_tracking_server_url', $this->tracking_server_url);
|
|
$forward_url = 'https://' . $tracking_server_url . '/spur';
|
|
|
|
// Create a new cURL resource
|
|
$ch = curl_init();
|
|
|
|
// Set URL and other appropriate options
|
|
curl_setopt($ch, CURLOPT_URL, $forward_url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->get_method());
|
|
|
|
// Build query string from parameters
|
|
$query_string = http_build_query($params);
|
|
curl_setopt($ch, CURLOPT_URL, $forward_url . '?' . $query_string);
|
|
|
|
// Prepare headers
|
|
$headers = array();
|
|
|
|
// Get all request headers
|
|
$request_headers = $request->get_headers();
|
|
|
|
// Check for final destination header
|
|
if (isset($request_headers['x_final_destination']) && !empty($request_headers['x_final_destination'][0])) {
|
|
// If a custom destination is provided in the header, use it
|
|
$final_destination = $request_headers['x_final_destination'][0];
|
|
$forward_url = 'https://' . $final_destination . '/spur';
|
|
curl_setopt($ch, CURLOPT_URL, $forward_url . '?' . $query_string);
|
|
|
|
// Add the header to the forwarded request
|
|
$headers[] = "X-Final-Destination: " . $final_destination;
|
|
}
|
|
|
|
// Forward all other headers
|
|
foreach ($request_headers as $name => $values) {
|
|
if ($name !== 'x_final_destination') {
|
|
foreach ($values as $value) {
|
|
$headers[] = sprintf("%s: %s", str_replace('_', '-', ucwords($name, '_')), $value);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Set headers for cURL request
|
|
if (!empty($headers)) {
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
}
|
|
|
|
// Execute request
|
|
$response = curl_exec($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
if (curl_errno($ch)) {
|
|
error_log('Palantics tracking error: ' . curl_error($ch));
|
|
$http_code = 500;
|
|
}
|
|
|
|
// Close cURL resource
|
|
curl_close($ch);
|
|
|
|
return $http_code;
|
|
}
|
|
|
|
/**
|
|
* Get the real IP address
|
|
*/
|
|
private function get_real_ip() {
|
|
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
|
$ip = $_SERVER['HTTP_CLIENT_IP'];
|
|
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
} else {
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
}
|
|
return $ip;
|
|
}
|
|
}
|