PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / classes / WpMatomo / Ecommerce / Base.php
matomo / classes / WpMatomo / Ecommerce Last commit date
Base.php 6 years ago EasyDigitalDownloads.php 6 years ago MemberPress.php 6 years ago Woocommerce.php 6 years ago
Base.php
112 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 * @package matomo
8 */
9
10 namespace WpMatomo\Ecommerce;
11
12 use WpMatomo\Logger;
13 use WpMatomo\Settings;
14 use WpMatomo\AjaxTracker;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit; // if accessed directly
18 }
19
20 class Base {
21 protected $key_order_tracked = 'order-tracked';
22
23 /**
24 * @var Logger
25 */
26 protected $logger;
27
28 /**
29 * @var AjaxTracker
30 */
31 protected $tracker;
32
33 /**
34 * We can't echo cart updates directly as we wouldn't know where in the template rendering stage we are and whether
35 * we're supposed to print or not etc. Also there might be multiple cart updates triggered during one page load so
36 * we want to make sure to print only the most recent tracking code
37 *
38 * @var string
39 */
40 protected $cart_update_queue = '';
41
42 private $ajax_tracker_calls = array();
43
44 public function __construct( AjaxTracker $tracker ) {
45 $this->logger = new Logger();
46 $this->tracker = $tracker;
47
48 // by using prefix we make sure it will be removed on unistall and make sure it's clear it belongs to us
49 $this->key_order_tracked = Settings::OPTION_PREFIX . $this->key_order_tracked;
50 }
51
52 public function register_hooks() {
53 if ( ! is_admin() ) {
54 add_action( 'wp_footer', array( $this, 'on_print_queues' ), 99999, 0 );
55 }
56 }
57
58 public function on_print_queues() {
59 // we need to queue in case there are multiple cart updates within one page load
60 if ( ! empty( $this->cart_update_queue ) ) {
61 echo $this->cart_update_queue;
62 }
63 }
64
65 protected function has_order_been_tracked_already( $order_id ) {
66 return get_post_meta( $order_id, $this->key_order_tracked, true ) == 1;
67 }
68
69 protected function set_order_been_tracked( $order_id ) {
70 update_post_meta( $order_id, $this->key_order_tracked, 1 );
71 }
72
73 protected function is_doing_ajax() {
74 return defined( 'DOING_AJAX' ) && DOING_AJAX;
75 }
76
77 protected function make_matomo_js_tracker_call( $params ) {
78 if ( $this->is_doing_ajax() ) {
79 $this->ajax_tracker_calls[] = $params;
80 }
81
82 return sprintf( 'window._paq = window._paq || []; window._paq.push(%s);', wp_json_encode( $params ) );
83 }
84
85 protected function wrap_script( $script ) {
86 if ( $this->is_doing_ajax() ) {
87 foreach ( $this->ajax_tracker_calls as $call ) {
88 $methods = array(
89 'addEcommerceItem' => 'addEcommerceItem',
90 'trackEcommerceOrder' => 'doTrackEcommerceOrder',
91 'trackEcommerceCartUpdate' => 'doTrackEcommerceCartUpdate',
92 );
93 if ( ! empty( $call[0] ) && ! empty( $methods[ $call[0] ] ) ) {
94 $tracker_method = $methods[ $call[0] ];
95 array_shift( $call );
96 call_user_func_array( array( $this->tracker, $tracker_method ), $call );
97 }
98 }
99 $this->ajax_tracker_calls = array();
100
101 return '';
102 }
103
104 if ( empty( $script ) ) {
105 return '';
106 }
107
108 return '<script type="text/javascript">' . $script . '</script>';
109 }
110
111 }
112