PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.12.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.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 4 years ago EasyDigitalDownloads.php 4 years ago MatomoTestEcommerce.php 4 years ago MemberPress.php 4 years ago Woocommerce.php 4 years ago
MemberPress.php
131 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'] )
89 && class_exists( '\MeprTransaction' ) ) {
90 $txn = MeprTransaction::get_one_by_trans_num( sanitize_text_field( wp_unslash( $_GET['trans_num'] ) ) );
91 if ( isset( $txn->id ) && $txn->id > 0 ) {
92 if ( $this->has_order_been_tracked_already( $txn->id ) ) {
93 return;
94 }
95 $this->set_order_been_tracked( $txn->id );
96 $transaction = new MeprTransaction( $txn->id );
97 $order_id_to_track = $txn->trans_num;
98 $product = $transaction->product();
99
100 $discount = 0;
101
102 if ( $product && $transaction->coupon() ) {
103 $discount = $product->price - $txn->amount;
104 }
105 $tracking_code = '';
106 $params = [
107 'addEcommerceItem',
108 '' . $product->ID,
109 $product->post_title,
110 [],
111 $txn->amount,
112 1,
113 ];
114 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
115 $params = [
116 'trackEcommerceOrder',
117 '' . $order_id_to_track,
118 $txn->total,
119 $txn->amount,
120 $txn->tax_amount,
121 $shipping = 0,
122 $discount,
123 ];
124 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
125 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
126 echo $this->wrap_script( $tracking_code );
127 }
128 }
129 }
130 }
131