PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.14.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.14.2
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 4 years ago EasyDigitalDownloads.php 4 years ago MatomoTestEcommerce.php 4 years ago MemberPress.php 3 years ago Woocommerce.php 4 years ago
Base.php
128 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 private $ajax_tracker_calls = [];
46
47 public function __construct( AjaxTracker $tracker ) {
48 $this->logger = new Logger();
49 $this->tracker = $tracker;
50
51 // by using prefix we make sure it will be removed on unistall and make sure it's clear it belongs to us
52 $this->key_order_tracked = Settings::OPTION_PREFIX . $this->key_order_tracked;
53 }
54
55 public function register_hooks() {
56 if ( ! is_admin() ) {
57 add_action( 'wp_footer', [ $this, 'on_print_queues' ], 99999, 0 );
58 }
59 }
60
61 public function on_print_queues() {
62 // we need to queue in case there are multiple cart updates within one page load
63 if ( ! empty( $this->cart_update_queue ) ) {
64 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
65 echo $this->cart_update_queue;
66 }
67 }
68
69 protected function has_order_been_tracked_already( $order_id ) {
70 // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
71 return get_post_meta( $order_id, $this->key_order_tracked, true ) == 1;
72 }
73
74 protected function set_order_been_tracked( $order_id ) {
75 update_post_meta( $order_id, $this->key_order_tracked, 1 );
76 }
77
78 protected function should_track_background() {
79 return ( defined( 'DOING_AJAX' ) && DOING_AJAX )
80 || WpMatomo::$settings->get_global_option( 'track_mode' ) === TrackingSettings::TRACK_MODE_TAGMANAGER;
81 }
82
83 protected function make_matomo_js_tracker_call( $params ) {
84 if ( $this->should_track_background() ) {
85 $this->ajax_tracker_calls[] = $params;
86 }
87
88 return sprintf( 'window._paq = window._paq || []; window._paq.push(%s);', wp_json_encode( $params ) );
89 }
90
91 protected function wrap_script( $script ) {
92 if ( $this->should_track_background() ) {
93 foreach ( $this->ajax_tracker_calls as $call ) {
94 $methods = [
95 'addEcommerceItem' => 'addEcommerceItem',
96 'trackEcommerceOrder' => 'doTrackEcommerceOrder',
97 'trackEcommerceCartUpdate' => 'doTrackEcommerceCartUpdate',
98 ];
99 if ( ! empty( $call[0] ) && ! empty( $methods[ $call[0] ] ) ) {
100 try {
101 $tracker_method = $methods[ $call[0] ];
102 array_shift( $call );
103 call_user_func_array( [ $this->tracker, $tracker_method ], $call );
104 } catch ( Exception $e ) {
105 $this->logger->log_exception( $call[0], $e );
106 }
107 }
108 }
109 $this->ajax_tracker_calls = [];
110
111 return '';
112 }
113
114 if ( empty( $script ) ) {
115 return '';
116 }
117
118 if ( function_exists( 'wp_get_inline_script_tag' ) ) {
119 $script = wp_get_inline_script_tag( $script );
120 } else {
121 // line feed is required to match the wp_get_inline_script_tag output
122 $script = '<script >' . PHP_EOL . $script . PHP_EOL . '</script>' . PHP_EOL;
123 }
124
125 return $script;
126 }
127 }
128