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 / MemberPress.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
MemberPress.php
139 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 $txn = null;
91 if ( isset( $_GET['trans_num'] ) ) {
92 $txn = MeprTransaction::get_one_by_trans_num( sanitize_text_field( wp_unslash( $_GET['trans_num'] ) ) );
93 } else {
94 if ( isset( $_GET['transaction_id'] ) ) {
95 $txn = MeprTransaction::get_one( sanitize_text_field( wp_unslash( $_GET['transaction_id'] ) ) );
96 }
97 }
98
99 if ( $txn && isset( $txn->id ) && $txn->id > 0 ) {
100 if ( $this->has_order_been_tracked_already( $txn->id ) ) {
101 return;
102 }
103 $this->set_order_been_tracked( $txn->id );
104 $transaction = new MeprTransaction( $txn->id );
105 $order_id_to_track = $txn->trans_num;
106 $product = $transaction->product();
107
108 $discount = 0;
109
110 if ( $product && $transaction->coupon() ) {
111 $discount = $product->price - $txn->amount;
112 }
113 $tracking_code = '';
114 $params = [
115 'addEcommerceItem',
116 '' . $product->ID,
117 $product->post_title,
118 [],
119 $txn->amount,
120 1,
121 ];
122 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
123 $params = [
124 'trackEcommerceOrder',
125 '' . $order_id_to_track,
126 $txn->total,
127 $txn->amount,
128 $txn->tax_amount,
129 $shipping = 0,
130 $discount,
131 ];
132 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
133 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
134 echo $this->wrap_script( $tracking_code );
135 }
136 }
137 }
138 }
139