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 / EasyDigitalDownloads.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
EasyDigitalDownloads.php
227 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 EDD_Download;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit; // if accessed directly
16 }
17
18 class EasyDigitalDownloads extends Base {
19 public function register_hooks() {
20 if ( ! is_admin() ) {
21 add_action( 'template_redirect', [ $this, 'on_product_view' ], 99999, 0 );
22 }
23
24 parent::register_hooks();
25
26 // these actions may be triggered in admin when ajax is used
27 add_action( 'edd_payment_receipt_after_table', [ $this, 'on_order' ], 99999, 2 );
28 add_action( 'edd_post_remove_from_cart', [ $this, 'on_cart_update' ], 99999, 0 );
29 add_action( 'edd_post_add_to_cart', [ $this, 'on_cart_update' ], 99999, 0 );
30 add_action( 'edd_cart_discounts_removed', [ $this, 'on_cart_update' ], 99999, 0 );
31 add_action( 'edd_after_set_cart_item_quantity', [ $this, 'on_cart_update' ], 99999, 0 );
32 add_action( 'edd_cart_discount_set', [ $this, 'on_cart_update' ], 99999, 0 );
33 }
34
35 public function on_cart_update() {
36 if ( ! function_exists( 'EDD' )
37 || ! class_exists( '\EDD_Download' ) ) {
38 return;
39 }
40
41 $cart = EDD()->cart;
42
43 $contents = $cart->get_contents();
44
45 $tracking_code = '';
46 foreach ( $contents as $key => $item ) {
47 $download = new EDD_Download( $item['id'] );
48
49 // If the item is not a download or it's status has changed since it was added to the cart.
50 if ( empty( $download->ID ) || ! $download->can_purchase() ) {
51 unset( $cart[ $key ] );
52 }
53
54 $name = $download->get_name();
55
56 $price_id = edd_get_cart_item_price_id( $item );
57 $price = $download->get_price();
58
59 if ( isset( $price_id ) ) {
60 // variation
61 $name .= ' - ' . edd_get_price_option_name( $item['id'], $price_id );
62 $price = edd_get_price_option_amount( $download->ID, $price_id );
63 }
64 $sku = $this->get_sku( $download, $item['id'] );
65 $categories = $this->get_product_categories( $download->ID );
66 $quantity = isset( $item['quantity'] ) ? $item['quantity'] : 0;
67
68 $params = [ 'addEcommerceItem', $sku, $name, $categories, $price, $quantity ];
69 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
70 }
71
72 $total = 0;
73 if ( ! empty( $cart->get_total_fees() ) ) {
74 $total = $cart->get_total_fees();
75 } elseif ( ! empty( $cart->get_total() ) ) {
76 $total = $cart->get_total();
77 }
78
79 $tracking_code .= $this->make_matomo_js_tracker_call( [ 'trackEcommerceCartUpdate', $total ] );
80
81 // we can't echo directly as we wouldn't know where in the template rendering stage we are and whether
82 // we're supposed to print or not etc
83 $this->cart_update_queue = $this->wrap_script( $tracking_code );
84 $this->logger->log( 'Tracked ecommerce cart update: ' );
85 }
86
87 private function get_product_categories( $download_id ) {
88 $categories = (array) get_the_terms( $download_id, 'download_category' );
89
90 return array_values( array_filter( wp_list_pluck( $categories, 'name' ) ) );
91 }
92
93 /**
94 * @param EDD_Download $download
95 *
96 * @return mixed
97 */
98 private function get_sku( $download, $download_id ) {
99 $sku = $download->get_sku();
100 if ( ! edd_use_skus() || empty( $sku ) || '-' === $sku ) {
101 $sku = $download_id;
102 }
103
104 return '' . $sku;
105 }
106
107 public function on_product_view() {
108 if ( ! is_singular( 'download' ) ) {
109 return;
110 }
111
112 $download_id = get_the_ID();
113
114 if ( empty( $download_id ) ) {
115 return;
116 }
117
118 if ( ! class_exists( '\EDD_Download' ) ) {
119 return;
120 }
121
122 $download = new EDD_Download( $download_id );
123
124 $sku = $this->get_sku( $download, $download_id );
125
126 $params = [
127 'setEcommerceView',
128 $sku,
129 $download->get_name(),
130 $this->get_product_categories( $download_id ),
131 $download->get_price(),
132 ];
133 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
134 echo $this->wrap_script( $this->make_matomo_js_tracker_call( $params ) );
135 }
136
137 public function on_order( $payment, $edd_receipt_args ) {
138 if ( $edd_receipt_args['payment_id'] ) {
139 if ( 'publish' !== $payment->post_status
140 && 'complete' !== $payment->post_status
141 && 'edd_subscription' !== $payment->post_status ) {
142 return;
143 }
144 // Use a meta value so we only send the beacon once.
145 if ( $this->has_order_been_tracked_already( $payment->ID ) ) {
146 return;
147 }
148
149 if ( ! class_exists( '\EDD_Download' ) ) {
150 return;
151 }
152
153 if ( ! function_exists( 'edd_get_payment_meta_cart_details' ) ) {
154 return;
155 }
156
157 $this->set_order_been_tracked( $payment->ID );
158
159 if ( function_exists( 'edd_get_payment_number' ) ) {
160 $order_id_to_track = edd_get_payment_number( $payment->ID );
161 } else {
162 $order_id_to_track = $payment->ID;
163 }
164
165 $tracking_code = '';
166
167 if ( ! empty( $edd_receipt_args['products'] ) ) {
168 $cart = edd_get_payment_meta_cart_details( $payment->ID, true );
169 if ( $cart ) {
170 foreach ( $cart as $key => $item ) {
171 if ( empty( $item['in_bundle'] ) ) {
172 $price_id = edd_get_cart_item_price_id( $item );
173 $name = $item['name'];
174 if ( isset( $price_id ) ) {
175 // variation
176 $name .= ' - ' . edd_get_price_option_name( $item['id'], $price_id );
177 }
178
179 $download = new EDD_Download( $item['id'] );
180 $sku = $this->get_sku( $download, $item['id'] );
181
182 $price = 0;
183 if ( isset( $item['item_price'] ) && is_numeric( $item['item_price'] ) ) {
184 $price = $item['item_price'];
185 }
186
187 $params = [
188 'addEcommerceItem',
189 $sku,
190 $name,
191 $this->get_product_categories( $item['id'] ),
192 $price,
193 $item['quantity'],
194 ];
195 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
196 }
197 }
198 }
199 }
200
201 $grand_total = edd_get_payment_amount( $payment->ID );
202
203 $payment_meta = edd_get_payment_meta( $payment->ID );
204 $discount = 0;
205 if ( ! empty( $payment_meta['user_info']['discount'] )
206 && 'none' !== $payment_meta['user_info']['discount'] ) {
207 $discount = $payment_meta['user_info']['discount'];
208 $discount = explode( ',', $discount );
209 $discount = reset( $discount );
210 }
211
212 $params = [
213 'trackEcommerceOrder',
214 '' . $order_id_to_track,
215 $grand_total ? $grand_total : 0,
216 edd_payment_subtotal( $payment->ID ),
217 edd_use_taxes() ? edd_get_payment_tax( $payment->ID, $payment_meta ) : '0',
218 $shipping = 0,
219 $discount,
220 ];
221 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
222 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
223 echo $this->wrap_script( $tracking_code );
224 }
225 }
226 }
227