fixed wp plugin
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
/**
|
||||
* Admin functionality class
|
||||
*/
|
||||
|
||||
// Exit if accessed directly
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Palantics_Admin {
|
||||
|
||||
// Tracking server URL
|
||||
private $tracking_server_url = 'tracking1.karlbreuer.com';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
// Add settings page
|
||||
add_action('admin_menu', array($this, 'add_settings_page'));
|
||||
add_action('admin_init', array($this, 'register_settings'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add settings page
|
||||
*/
|
||||
public function add_settings_page() {
|
||||
add_options_page(
|
||||
'Palantics Tracking',
|
||||
'Palantics Tracking',
|
||||
'manage_options',
|
||||
'palantics-tracking-settings',
|
||||
array($this, 'render_settings_page')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register settings
|
||||
*/
|
||||
public function register_settings() {
|
||||
register_setting('palantics_tracking_settings', 'palantics_tracking_server_url');
|
||||
register_setting('palantics_tracking_settings', 'palantics_tracking_track_classes');
|
||||
|
||||
add_settings_section(
|
||||
'palantics_tracking_section',
|
||||
'Tracking Settings',
|
||||
array($this, 'settings_section_callback'),
|
||||
'palantics-tracking-settings'
|
||||
);
|
||||
|
||||
add_settings_field(
|
||||
'palantics_tracking_server_url',
|
||||
'Tracking Server URL',
|
||||
array($this, 'server_url_callback'),
|
||||
'palantics-tracking-settings',
|
||||
'palantics_tracking_section'
|
||||
);
|
||||
|
||||
add_settings_section(
|
||||
'palantics_tracking_auto_section',
|
||||
'Element Tracking Settings',
|
||||
array($this, 'auto_settings_section_callback'),
|
||||
'palantics-tracking-settings'
|
||||
);
|
||||
|
||||
add_settings_field(
|
||||
'palantics_tracking_track_classes',
|
||||
'Track Elements with Classes',
|
||||
array($this, 'track_classes_callback'),
|
||||
'palantics-tracking-settings',
|
||||
'palantics_tracking_auto_section'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings section callback
|
||||
*/
|
||||
public function settings_section_callback() {
|
||||
echo '<p>Configure your Palantics tracking server settings.</p>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Auto tracking settings section callback
|
||||
*/
|
||||
public function auto_settings_section_callback() {
|
||||
echo '<p>Configure which elements to track based on CSS classes.</p>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Server URL field callback
|
||||
*/
|
||||
public function server_url_callback() {
|
||||
$value = get_option('palantics_tracking_server_url', $this->tracking_server_url);
|
||||
echo '<input type="text" id="palantics_tracking_server_url" name="palantics_tracking_server_url" value="' . esc_attr($value) . '" class="regular-text">';
|
||||
echo '<p class="description">Enter the tracking server URL without https:// (e.g., tracking1.karlbreuer.com)</p>';
|
||||
echo '<p class="description">You will find this in your domain screen in the Palantics app</p>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Track classes callback
|
||||
*/
|
||||
public function track_classes_callback() {
|
||||
$value = get_option('palantics_tracking_track_classes', '');
|
||||
echo '<input type="text" id="palantics_tracking_track_classes" name="palantics_tracking_track_classes" value="' . esc_attr($value) . '" class="regular-text">';
|
||||
echo '<p class="description">Enter comma-separated class names to automatically track clicks on elements with these classes (e.g., cta-button, featured-product)</p>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Render settings page
|
||||
*/
|
||||
public function render_settings_page() {
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1><span style="color: #007cba;">Palantics</span> Tracking Settings</h1>
|
||||
<form method="post" action="options.php">
|
||||
<?php
|
||||
settings_fields('palantics_tracking_settings');
|
||||
do_settings_sections('palantics-tracking-settings');
|
||||
submit_button();
|
||||
?>
|
||||
</form>
|
||||
|
||||
<div style="background-color: #fff; padding: 20px; margin-top: 20px; border: 1px solid #ccc; border-radius: 4px;">
|
||||
<h2>Manual Tracking Methods</h2>
|
||||
|
||||
<div style="margin-bottom: 20px;">
|
||||
<h3>Method 1: Using Data Attributes</h3>
|
||||
<p>Add the <code>data-track</code> attribute to any HTML element to track clicks:</p>
|
||||
<pre style="background-color: #f5f5f5; padding: 10px; border-radius: 4px;"><button data-track="signup_button_click">Sign Up</button></pre>
|
||||
</div>
|
||||
|
||||
<div style="margin-bottom: 20px;">
|
||||
<h3>Method 2: Using Shortcode</h3>
|
||||
<p>Wrap any content with the shortcode to track clicks:</p>
|
||||
<pre style="background-color: #f5f5f5; padding: 10px; border-radius: 4px;">[track_element event="download_click" class="your-class" style="your-style"]
|
||||
Your content here (can include other shortcodes)
|
||||
[/track_element]</pre>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h3>Method 3: Using Direct JavaScript</h3>
|
||||
<p>You can also manually add <code>onclick="tE('your_event_name')"</code> to any HTML element.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* Asset loader class
|
||||
*/
|
||||
|
||||
// Exit if accessed directly
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Palantics_Loader {
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
// Enqueue frontend scripts
|
||||
add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_scripts'));
|
||||
|
||||
// Enqueue admin scripts
|
||||
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue frontend scripts
|
||||
*/
|
||||
public function enqueue_frontend_scripts() {
|
||||
// Register main tracking script
|
||||
wp_register_script(
|
||||
'palantics-tracking-script',
|
||||
PALANTICS_PLUGIN_URL . 'assets/js/tracking-script.js',
|
||||
array(),
|
||||
PALANTICS_PLUGIN_VERSION,
|
||||
false
|
||||
);
|
||||
|
||||
// Register class tracking script
|
||||
wp_register_script(
|
||||
'palantics-class-tracking',
|
||||
PALANTICS_PLUGIN_URL . 'assets/js/class-tracking.js',
|
||||
array('palantics-tracking-script'),
|
||||
PALANTICS_PLUGIN_VERSION,
|
||||
true
|
||||
);
|
||||
|
||||
// Get tracking classes from options
|
||||
$track_classes = get_option('palantics_tracking_track_classes', '');
|
||||
$track_classes_array = array_filter(array_map('trim', explode(',', $track_classes)));
|
||||
|
||||
// Only enqueue class tracking if we have classes to track
|
||||
if (!empty($track_classes_array)) {
|
||||
// Add trackClasses as a JavaScript variable
|
||||
wp_localize_script(
|
||||
'palantics-class-tracking',
|
||||
'trackClasses',
|
||||
$track_classes_array
|
||||
);
|
||||
|
||||
// Enqueue the script
|
||||
wp_enqueue_script('palantics-class-tracking');
|
||||
}
|
||||
|
||||
// Always enqueue the main tracking script
|
||||
wp_enqueue_script('palantics-tracking-script');
|
||||
|
||||
// Add server URL to JavaScript
|
||||
$server_url = esc_url(get_option('palantics_tracking_server_url', 'tracking1.karlbreuer.com'));
|
||||
wp_add_inline_script(
|
||||
'palantics-tracking-script',
|
||||
"const server = 'https://" . $server_url . "';",
|
||||
'before'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue admin scripts
|
||||
*/
|
||||
public function enqueue_admin_scripts($hook) {
|
||||
// Only load on our settings page
|
||||
if ($hook != 'settings_page_palantics-tracking-settings') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Enqueue admin styles
|
||||
wp_enqueue_style(
|
||||
'palantics-admin-style',
|
||||
PALANTICS_PLUGIN_URL . 'assets/css/admin.css',
|
||||
array(),
|
||||
PALANTICS_PLUGIN_VERSION
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<?php // silence is golden
|
||||
Reference in New Issue
Block a user