PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.12.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.12.1
5.12.1 5.12.0 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 1 week ago EasyDigitalDownloads.php 1 week ago MatomoTestEcommerce.php 11 months ago MemberPress.php 1 week ago ServerSideVisitorId.php 1 year ago Woocommerce.php 1 week ago
MemberPress.php
152 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 } elseif ( isset( $_GET['transaction_id'] ) ) {
95 $txn = MeprTransaction::get_one( sanitize_text_field( wp_unslash( $_GET['transaction_id'] ) ) );
96 }
97
98 if ( ! $txn || ! isset( $txn->id ) || $txn->id <= 0 ) {
99 return;
100 }
101
102 if ( ! $txn->user_id || get_current_user_id() !== (int) $txn->user_id ) {
103 $this->logger->log(
104 sprintf(
105 'Current user ID = %s did not match MemberPress transaction user ID = %s, not tracking.',
106 get_current_user_id(),
107 $txn->user_id
108 )
109 );
110 return;
111 }
112
113 if ( $this->has_order_been_tracked_already( $txn->id ) ) {
114 return;
115 }
116
117 $this->set_order_been_tracked( $txn->id );
118 $transaction = new MeprTransaction( $txn->id );
119 $order_id_to_track = $txn->trans_num;
120 $product = $transaction->product();
121
122 $discount = 0;
123
124 if ( $product && $transaction->coupon() ) {
125 $discount = $product->price - $txn->amount;
126 }
127 $tracking_code = '';
128 $params = [
129 'addEcommerceItem',
130 '' . $product->ID,
131 $product->post_title,
132 [],
133 $txn->amount,
134 1,
135 ];
136 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
137 $params = [
138 'trackEcommerceOrder',
139 '' . $order_id_to_track,
140 $txn->total,
141 $txn->amount,
142 $txn->tax_amount,
143 $shipping = 0,
144 $discount,
145 ];
146 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
147 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
148 echo $this->wrap_script( $tracking_code );
149 }
150 }
151 }
152