2022-07-27 18:01:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A class for logging tracked events.
|
|
|
|
*
|
|
|
|
* @package WC_Beta_Tester
|
|
|
|
*/
|
|
|
|
|
2023-08-09 00:20:34 +00:00
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
2022-07-27 18:01:18 +00:00
|
|
|
/**
|
|
|
|
* Class Tracks_Debug_Log.
|
|
|
|
*/
|
|
|
|
class Tracks_Debug_Log {
|
|
|
|
/**
|
|
|
|
* Logger class to use.
|
|
|
|
*
|
|
|
|
* @var WC_Logger_Interface|null
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logger source.
|
|
|
|
*
|
|
|
|
* @var string logger source.
|
|
|
|
*/
|
|
|
|
private $source = 'tracks';
|
|
|
|
|
2022-12-06 18:18:52 +00:00
|
|
|
/**
|
|
|
|
* Get the logger instance.
|
|
|
|
*
|
|
|
|
* @return WC_Logger_Interface|null
|
|
|
|
*/
|
|
|
|
private function get_logger() {
|
|
|
|
// Sometimes between installations of WC versions the logger will not be available.
|
|
|
|
if ( ! $this->logger && function_exists( 'wc_get_logger' ) ) {
|
|
|
|
$this->logger = wc_get_logger();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->logger;
|
|
|
|
}
|
|
|
|
|
2022-07-27 18:01:18 +00:00
|
|
|
/**
|
|
|
|
* Initialize hooks.
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
2022-12-06 18:18:52 +00:00
|
|
|
// WooCommerce might not be installed/activated between installs of WC versions.
|
2023-08-04 04:29:07 +00:00
|
|
|
if ( defined( 'WC_ABSPATH' ) ) {
|
2022-12-06 18:18:52 +00:00
|
|
|
include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks-client.php';
|
|
|
|
include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks-footer-pixel.php';
|
2022-07-27 18:01:18 +00:00
|
|
|
|
2022-12-06 18:18:52 +00:00
|
|
|
add_action( 'admin_footer', array( $this, 'log_footer_pixels' ), 5 );
|
|
|
|
add_action( 'pre_http_request', array( $this, 'log_remote_pixels' ), 10, 3 );
|
|
|
|
}
|
2022-07-27 18:01:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log the event.
|
|
|
|
*
|
|
|
|
* @param string $event_name Event name.
|
|
|
|
* @param array $properties Event properties.
|
|
|
|
*/
|
|
|
|
public function log_event( $event_name, $properties ) {
|
2022-12-06 18:18:52 +00:00
|
|
|
$logger = $this->get_logger();
|
|
|
|
|
|
|
|
if ( $logger ) {
|
|
|
|
$logger->debug(
|
|
|
|
$event_name,
|
2022-07-27 18:01:18 +00:00
|
|
|
array( 'source' => $this->source )
|
|
|
|
);
|
|
|
|
}
|
2022-12-06 18:18:52 +00:00
|
|
|
|
|
|
|
foreach ( $properties as $key => $property ) {
|
2023-08-28 19:44:27 +00:00
|
|
|
if ( is_array( $property ) ) {
|
|
|
|
$property = print_r( $property, true );
|
|
|
|
}
|
2022-12-06 18:18:52 +00:00
|
|
|
if ( $logger ) {
|
|
|
|
$logger->debug(
|
|
|
|
" - {$key}: {$property}",
|
|
|
|
array( 'source' => $this->source )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-07-27 18:01:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log events passed as footer pixels.
|
|
|
|
*/
|
|
|
|
public function log_footer_pixels() {
|
|
|
|
$events = WC_Tracks_Footer_Pixel::get_events();
|
|
|
|
foreach ( $events as $event ) {
|
|
|
|
$this->log_event( $event->_en, $event );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log events that are retrieved by remote request.
|
|
|
|
*
|
|
|
|
* @param false|array|WP_Error $preempt A preemptive return value of an HTTP request. Default false.
|
|
|
|
* @param array $parsed_args HTTP request arguments.
|
|
|
|
* @param string $url The request URL.
|
|
|
|
*/
|
|
|
|
public function log_remote_pixels( $preempt, $parsed_args, $url ) {
|
|
|
|
if ( strpos( $url, WC_Tracks_Client::PIXEL ) === 0 ) {
|
|
|
|
$parsed_url = wp_parse_url( $url );
|
|
|
|
parse_str( $parsed_url['query'], $params );
|
|
|
|
$this->log_event( $params['_en'], $params );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $preempt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new Tracks_Debug_Log();
|