PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.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 / MemberPress.php
matomo / classes / WpMatomo / Ecommerce Last commit date
Base.php 10 months ago EasyDigitalDownloads.php 4 years ago MatomoTestEcommerce.php 10 months ago MemberPress.php 3 months ago ServerSideVisitorId.php 1 year ago Woocommerce.php 4 months ago
MemberPress.php
154 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 MeprProduct;
13 use MeprTransaction;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // if accessed directly
17 }
18
19 class MemberPress extends Base {
20 public function register_hooks() {
21 if ( ! is_admin() ) {
22 parent::register_hooks();
23
24 add_action( 'template_redirect', [ $this, 'on_product_view' ], 99999, 0 );
25 add_action( 'wp_footer', [ $this, 'on_order' ], 99999, 2 );
26 add_action( 'mepr-signup', [ $this, 'on_cart_update' ], 99999, 1 );
27 }
28 }
29
30 /**
31 * @param MeprTransaction $transaction
32 */
33 public function on_cart_update( $transaction ) {
34 $tracking_code = '';
35 $sku = $transaction->id;
36 $product = $transaction->product();
37 $params = [
38 'addEcommerceItem',
39 $sku,
40 $product->post_title,
41 $categories = [],
42 $transaction->amount,
43 1,
44 ];
45 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
46
47 $total = $transaction->total;
48 $tracking_code .= $this->make_matomo_js_tracker_call( [ 'trackEcommerceCartUpdate', $total ] );
49
50 // we can't echo directly as we wouldn't know where in the template rendering stage we are and whether
51 // we're supposed to print or not etc
52 $this->cart_update_queue = $this->wrap_script( $tracking_code );
53 $this->logger->log( 'Tracked ecommerce cart update: ' );
54 }
55
56 public function on_product_view() {
57 if ( ! is_singular( 'memberpressproduct' ) ) {
58 return;
59 }
60
61 $product_id = get_the_ID();
62
63 if ( empty( $product_id ) ) {
64 return;
65 }
66
67 if ( ! class_exists( '\MeprProduct' ) ) {
68 return;
69 }
70
71 $product = new MeprProduct( $product_id );
72
73 $sku = $product_id;
74
75 $params = [
76 'setEcommerceView',
77 '' . $sku,
78 $product->post_title,
79 $categories = [],
80 $product->price,
81 ];
82 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
83 echo $this->wrap_script( $this->make_matomo_js_tracker_call( $params ) );
84 }
85
86 public function on_order() {
87 if ( isset( $_GET['membership'] )
88 && ( isset( $_GET['trans_num'] ) || isset( $_GET['transaction_id'] ) )
89 && class_exists( '\MeprTransaction' )
90 ) {
91 $txn = null;
92 if ( isset( $_GET['trans_num'] ) ) {
93 $txn = MeprTransaction::get_one_by_trans_num( sanitize_text_field( wp_unslash( $_GET['trans_num'] ) ) );
94 } else {
95 if ( isset( $_GET['transaction_id'] ) ) {
96 $txn = MeprTransaction::get_one( sanitize_text_field( wp_unslash( $_GET['transaction_id'] ) ) );
97 }
98 }
99
100 if ( ! $txn || ! isset( $txn->id ) || $txn->id <= 0 ) {
101 return;
102 }
103
104 if ( ! $txn->user_id || get_current_user_id() !== (int) $txn->user_id ) {
105 $this->logger->log(
106 sprintf(
107 'Current user ID = %s did not match MemberPress transaction user ID = %s, not tracking.',
108 get_current_user_id(),
109 $txn->user_id
110 )
111 );
112 return;
113 }
114
115 if ( $this->has_order_been_tracked_already( $txn->id ) ) {
116 return;
117 }
118
119 $this->set_order_been_tracked( $txn->id );
120 $transaction = new MeprTransaction( $txn->id );
121 $order_id_to_track = $txn->trans_num;
122 $product = $transaction->product();
123
124 $discount = 0;
125
126 if ( $product && $transaction->coupon() ) {
127 $discount = $product->price - $txn->amount;
128 }
129 $tracking_code = '';
130 $params = [
131 'addEcommerceItem',
132 '' . $product->ID,
133 $product->post_title,
134 [],
135 $txn->amount,
136 1,
137 ];
138 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
139 $params = [
140 'trackEcommerceOrder',
141 '' . $order_id_to_track,
142 $txn->total,
143 $txn->amount,
144 $txn->tax_amount,
145 $shipping = 0,
146 $discount,
147 ];
148 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
149 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
150 echo $this->wrap_script( $tracking_code );
151 }
152 }
153 }
154