events
3 years ago
class-wc-site-tracking.php
3 years ago
class-wc-tracks-client.php
4 years ago
class-wc-tracks-event.php
3 years ago
class-wc-tracks-footer-pixel.php
3 years ago
class-wc-tracks.php
3 years ago
class-wc-tracks-footer-pixel.php
128 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Send Tracks events on behalf of a user using pixel images in page footer. |
| 4 | * |
| 5 | * @package WooCommerce\Tracks |
| 6 | */ |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | /** |
| 11 | * WC_Tracks_Footer_Pixel class. |
| 12 | */ |
| 13 | class WC_Tracks_Footer_Pixel { |
| 14 | /** |
| 15 | * Singleton instance. |
| 16 | * |
| 17 | * @var WC_Tracks_Footer_Pixel |
| 18 | */ |
| 19 | protected static $instance = null; |
| 20 | |
| 21 | /** |
| 22 | * Events to send to Tracks. |
| 23 | * |
| 24 | * @var array |
| 25 | */ |
| 26 | protected $events = array(); |
| 27 | |
| 28 | /** |
| 29 | * Instantiate the singleton. |
| 30 | * |
| 31 | * @return WC_Tracks_Footer_Pixel |
| 32 | */ |
| 33 | public static function instance() { |
| 34 | if ( is_null( self::$instance ) ) { |
| 35 | self::$instance = new WC_Tracks_Footer_Pixel(); |
| 36 | } |
| 37 | |
| 38 | return self::$instance; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Constructor - attach hooks to the singleton instance. |
| 43 | */ |
| 44 | public function __construct() { |
| 45 | add_action( 'admin_footer', array( $this, 'render_tracking_pixels' ) ); |
| 46 | add_action( 'shutdown', array( $this, 'send_tracks_requests' ) ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Record a Tracks event |
| 51 | * |
| 52 | * @param array $event Array of event properties. |
| 53 | * @return bool|WP_Error True on success, WP_Error on failure. |
| 54 | */ |
| 55 | public static function record_event( $event ) { |
| 56 | if ( ! $event instanceof WC_Tracks_Event ) { |
| 57 | $event = new WC_Tracks_Event( $event ); |
| 58 | } |
| 59 | |
| 60 | if ( is_wp_error( $event ) ) { |
| 61 | return $event; |
| 62 | } |
| 63 | |
| 64 | self::instance()->add_event( $event ); |
| 65 | |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Add a Tracks event to the queue. |
| 71 | * |
| 72 | * @param WC_Tracks_Event $event Event to track. |
| 73 | */ |
| 74 | public function add_event( $event ) { |
| 75 | $this->events[] = $event; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Add events as tracking pixels to page footer. |
| 80 | */ |
| 81 | public function render_tracking_pixels() { |
| 82 | if ( empty( $this->events ) ) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | foreach ( $this->events as $event ) { |
| 87 | $pixel = $event->build_pixel_url(); |
| 88 | |
| 89 | if ( ! $pixel ) { |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | echo '<img style="position: fixed;" src="', esc_url( $pixel ), '" />'; |
| 94 | } |
| 95 | |
| 96 | $this->events = array(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Fire off API calls for events that weren't converted to pixels. |
| 101 | * |
| 102 | * This handles wp_redirect(). |
| 103 | */ |
| 104 | public function send_tracks_requests() { |
| 105 | if ( empty( $this->events ) ) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | foreach ( $this->events as $event ) { |
| 110 | WC_Tracks_Client::record_event( $event ); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get all events. |
| 116 | */ |
| 117 | public static function get_events() { |
| 118 | return self::instance()->events; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Clear all queued events. |
| 123 | */ |
| 124 | public static function clear_events() { |
| 125 | self::instance()->events = array(); |
| 126 | } |
| 127 | } |
| 128 |