File "class-wc-tracks-event.php"

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

Download   Open   Edit   Advanced Editor   Back

<?php

use Automattic\Jetpack\Constants;
defined('ABSPATH') || exit;
class WC_Tracks_Event
{
    const EVENT_NAME_REGEX = '/^(([a-z0-9]+)_){2}([a-z0-9_]+)$/';
    const PROP_NAME_REGEX = '/^[a-z_][a-z0-9_]*$/';
    public $error;
    public function __construct($event)
    {
        $_event = self::validate_and_sanitize($event);
        if (is_wp_error($_event)) {
            $this->error = $_event;
            return;
        }
        foreach ($_event as $key => $value) {
            $this->{$key} = $value;
        }
    }
    public function record()
    {
        if (wp_doing_ajax() || Constants::is_true('REST_REQUEST')) {
            return WC_Tracks_Client::record_event($this);
        }
        return WC_Tracks_Footer_Pixel::record_event($this);
    }
    public static function validate_and_sanitize($event)
    {
        $event = (object) $event;
        if (!$event->_en) {
            return new WP_Error('invalid_event', 'A valid event must be specified via `_en`', 400);
        }
        if (property_exists($event, '_via_ip') && preg_match('/^192\\.168|^10\\./', $event->_via_ip)) {
            unset($event->_via_ip);
        }
        $validated = array('browser_type' => WC_Tracks_Client::BROWSER_TYPE);
        $_event = (object) array_merge((array) $event, $validated);
        if (!isset($_event->_ts)) {
            $_event->_ts = WC_Tracks_Client::build_timestamp();
        }
        return $_event;
    }
    public function build_pixel_url()
    {
        if ($this->error) {
            return '';
        }
        $args = get_object_vars($this);
        unset($args['_rt'], $args['_']);
        $validated = self::validate_and_sanitize($args);
        if (is_wp_error($validated)) {
            return '';
        }
        return esc_url_raw(WC_Tracks_Client::PIXEL . '?' . http_build_query($validated));
    }
    public static function event_name_is_valid($name)
    {
        return preg_match(self::EVENT_NAME_REGEX, $name);
    }
    public static function prop_name_is_valid($name)
    {
        return preg_match(self::PROP_NAME_REGEX, $name);
    }
    public static function scrutinize_event_names($event)
    {
        if (!self::event_name_is_valid($event->_en)) {
            return;
        }
        $allowed_key_names = array('anonId', 'Browser_Type');
        foreach (array_keys((array) $event) as $key) {
            if (in_array($key, $allowed_key_names, true)) {
                continue;
            }
            if (!self::prop_name_is_valid($key)) {
                return;
            }
        }
    }
}