File "class-wc-tracks-footer-pixel.php"

Full path: /home/kosmetik/public_html/wp-content/plugins/woocommerce/includes/tracks/class-wc-tracks-footer-pixel.php
File size: 1.51 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

defined('ABSPATH') || exit;
class WC_Tracks_Footer_Pixel
{
    protected static $instance = null;
    protected $events = array();
    public static function instance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new WC_Tracks_Footer_Pixel();
        }
        return self::$instance;
    }
    public function __construct()
    {
        add_action('admin_footer', array($this, 'render_tracking_pixels'));
        add_action('shutdown', array($this, 'send_tracks_requests'));
    }
    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;
        }
        self::instance()->add_event($event);
        return true;
    }
    public function add_event($event)
    {
        $this->events[] = $event;
    }
    public function render_tracking_pixels()
    {
        if (empty($this->events)) {
            return;
        }
        foreach ($this->events as $event) {
            $pixel = $event->build_pixel_url();
            if (!$pixel) {
                continue;
            }
            echo '<img style="position: fixed;" src="', esc_url($pixel), '" />';
        }
        $this->events = array();
    }
    public function send_tracks_requests()
    {
        if (empty($this->events)) {
            return;
        }
        foreach ($this->events as $event) {
            WC_Tracks_Client::record_event($event);
        }
    }
}