PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.0.7
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.0.7
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 2 years ago EasyDigitalDownloads.php 4 years ago MatomoTestEcommerce.php 4 years ago MemberPress.php 3 years ago Woocommerce.php 2 years ago
Base.php
141 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 Exception;
13 use WpMatomo;
14 use WpMatomo\Admin\TrackingSettings;
15 use WpMatomo\AjaxTracker;
16 use WpMatomo\Logger;
17 use WpMatomo\Settings;
18
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit; // if accessed directly
21 }
22
23 class Base {
24 protected $key_order_tracked = 'order-tracked';
25
26 /**
27 * @var Logger
28 */
29 protected $logger;
30
31 /**
32 * @var AjaxTracker
33 */
34 protected $tracker;
35
36 /**
37 * We can't echo cart updates directly as we wouldn't know where in the template rendering stage we are and whether
38 * we're supposed to print or not etc. Also there might be multiple cart updates triggered during one page load so
39 * we want to make sure to print only the most recent tracking code
40 *
41 * @var string
42 */
43 protected $cart_update_queue = '';
44
45 /**
46 * @var Settings
47 */
48 protected $settings;
49
50 private $ajax_tracker_calls = [];
51
52 public function __construct( AjaxTracker $tracker, Settings $settings ) {
53 $this->logger = new Logger();
54 $this->tracker = $tracker;
55 $this->settings = $settings;
56
57 // by using prefix we make sure it will be removed on unistall and make sure it's clear it belongs to us
58 $this->key_order_tracked = Settings::OPTION_PREFIX . $this->key_order_tracked;
59 }
60
61 public function register_hooks() {
62 if ( ! is_admin() ) {
63 add_action( 'wp_footer', [ $this, 'on_print_queues' ], 99999, 0 );
64 }
65 }
66
67 public function on_print_queues() {
68 // we need to queue in case there are multiple cart updates within one page load
69 if ( ! empty( $this->cart_update_queue ) ) {
70 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
71 echo $this->cart_update_queue;
72 }
73 }
74
75 protected function has_order_been_tracked_already( $order_id ) {
76 // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
77 return get_post_meta( $order_id, $this->key_order_tracked, true ) == 1;
78 }
79
80 protected function set_order_been_tracked( $order_id ) {
81 update_post_meta( $order_id, $this->key_order_tracked, 1 );
82 }
83
84 protected function should_track_background() {
85 return ( defined( 'DOING_AJAX' ) && DOING_AJAX )
86 || ( defined( 'REST_REQUEST' ) && REST_REQUEST )
87 || $this->settings->get_global_option( 'track_mode' ) === TrackingSettings::TRACK_MODE_TAGMANAGER;
88 }
89
90 protected function make_matomo_js_tracker_call( $params ) {
91 if ( $this->should_track_background() ) {
92 $this->ajax_tracker_calls[] = $params;
93 }
94
95 $code = 'window._paq = window._paq || [];';
96 if ( $this->settings->get_global_option( 'disable_cookies' ) ) {
97 $code .= ' ' . WpMatomo\TrackingCode\TrackingCodeGenerator::get_disable_cookies_partial();
98 }
99 $code .= sprintf( ' window._paq.push(%s);', wp_json_encode( $params ) );
100
101 return $code;
102 }
103
104 protected function wrap_script( $script ) {
105 if ( $this->should_track_background() ) {
106 foreach ( $this->ajax_tracker_calls as $call ) {
107 $methods = [
108 'addEcommerceItem' => 'addEcommerceItem',
109 'trackEcommerceOrder' => 'doTrackEcommerceOrder',
110 'trackEcommerceCartUpdate' => 'doTrackEcommerceCartUpdate',
111 ];
112 if ( ! empty( $call[0] ) && ! empty( $methods[ $call[0] ] ) ) {
113 try {
114 $tracker_method = $methods[ $call[0] ];
115 array_shift( $call );
116 call_user_func_array( [ $this->tracker, $tracker_method ], $call );
117 } catch ( Exception $e ) {
118 $this->logger->log_exception( $call[0], $e );
119 }
120 }
121 }
122 $this->ajax_tracker_calls = [];
123
124 return '';
125 }
126
127 if ( empty( $script ) ) {
128 return '';
129 }
130
131 if ( function_exists( 'wp_get_inline_script_tag' ) ) {
132 $script = wp_get_inline_script_tag( $script );
133 } else {
134 // line feed is required to match the wp_get_inline_script_tag output
135 $script = '<script >' . PHP_EOL . $script . PHP_EOL . '</script>' . PHP_EOL;
136 }
137
138 return $script;
139 }
140 }
141