2019-02-22 19:00:38 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Send Tracks events on behalf of a user using pixel images in page footer.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Tracks
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_Tracks_Footer_Pixel class.
|
|
|
|
*/
|
|
|
|
class WC_Tracks_Footer_Pixel {
|
|
|
|
/**
|
|
|
|
* Singleton instance.
|
|
|
|
*
|
|
|
|
* @var WC_Tracks_Footer_Pixel
|
|
|
|
*/
|
|
|
|
protected static $instance = null;
|
|
|
|
|
|
|
|
/**
|
2019-03-04 16:59:49 +00:00
|
|
|
* Events to send to Tracks.
|
2019-02-22 19:00:38 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-03-04 16:59:49 +00:00
|
|
|
protected $events = array();
|
2019-02-22 19:00:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiate the singleton.
|
|
|
|
*
|
|
|
|
* @return WC_Tracks_Footer_Pixel
|
|
|
|
*/
|
|
|
|
public static function instance() {
|
|
|
|
if ( is_null( self::$instance ) ) {
|
|
|
|
self::$instance = new WC_Tracks_Footer_Pixel();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor - attach hooks to the singleton instance.
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
add_action( 'admin_footer', array( $this, 'render_tracking_pixels' ) );
|
2019-03-04 16:59:49 +00:00
|
|
|
add_action( 'shutdown', array( $this, 'send_tracks_requests' ) );
|
2019-02-22 19:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Record a Tracks event
|
|
|
|
*
|
|
|
|
* @param array $event Array of event properties.
|
2019-03-04 16:59:49 +00:00
|
|
|
* @return bool|WP_Error True on success, WP_Error on failure.
|
2019-02-22 19:00:38 +00:00
|
|
|
*/
|
|
|
|
public static function record_event( $event ) {
|
|
|
|
if ( ! $event instanceof WC_Tracks_Event ) {
|
|
|
|
$event = new WC_Tracks_Event( $event );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_wp_error( $event ) ) {
|
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
2019-03-04 16:59:49 +00:00
|
|
|
self::instance()->add_event( $event );
|
2019-02-22 19:00:38 +00:00
|
|
|
|
2019-03-04 16:59:49 +00:00
|
|
|
return true;
|
2019-02-22 19:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-04 16:59:49 +00:00
|
|
|
* Add a Tracks event to the queue.
|
2019-02-22 19:00:38 +00:00
|
|
|
*
|
2019-03-04 16:59:49 +00:00
|
|
|
* @param WC_Tracks_Event $event Event to track.
|
2019-02-22 19:00:38 +00:00
|
|
|
*/
|
2019-03-04 16:59:49 +00:00
|
|
|
public function add_event( $event ) {
|
|
|
|
$this->events[] = $event;
|
2019-02-22 19:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-04 16:59:49 +00:00
|
|
|
* Add events as tracking pixels to page footer.
|
2019-02-22 19:00:38 +00:00
|
|
|
*/
|
2019-03-04 16:59:49 +00:00
|
|
|
public function render_tracking_pixels() {
|
|
|
|
if ( empty( $this->events ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-22 19:00:38 +00:00
|
|
|
|
2019-03-04 16:59:49 +00:00
|
|
|
foreach ( $this->events as $event ) {
|
|
|
|
$pixel = $event->build_pixel_url();
|
|
|
|
|
|
|
|
if ( ! $pixel ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-18 11:05:30 +00:00
|
|
|
echo '<img style="position: fixed;" src="', esc_url( $pixel ), '" />';
|
2019-03-04 16:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->events = array();
|
2019-02-22 19:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-04 16:59:49 +00:00
|
|
|
* Fire off API calls for events that weren't converted to pixels.
|
|
|
|
*
|
|
|
|
* This handles wp_redirect().
|
2019-02-22 19:00:38 +00:00
|
|
|
*/
|
2019-03-04 16:59:49 +00:00
|
|
|
public function send_tracks_requests() {
|
|
|
|
if ( empty( $this->events ) ) {
|
2019-02-22 19:00:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-04 16:59:49 +00:00
|
|
|
foreach ( $this->events as $event ) {
|
|
|
|
WC_Tracks_Client::record_event( $event );
|
2019-02-22 19:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|