PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.5
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.5
8.0.5 8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 All 47 releases
← All changes | integrations/woocommerce/includes/triggers.php +840 -0 7.8.8 → 8.0.5 View file →
@@ -1,0 +1,840 @@
1 +<?php
2 +/**
3 + * Triggers
4 + *
5 + * @package GamiPress\WooCommerce\Triggers
6 + * @since 1.0.0
7 + */
8 +
9 +// Exit if accessed directly
10 +if( !defined( 'ABSPATH' ) ) exit;
11 +
12 +/**
13 + * Register WooCommerce activity triggers
14 + *
15 + * @param array $triggers
16 + * @return mixed
17 + */
18 +function gamipress_wc_activity_triggers( $triggers ) {
19 +
20 + $triggers[__( 'WooCommerce', 'gamipress' )] = array(
21 +
22 + 'gamipress_publish_product' => __( 'Publish a new product', 'gamipress' ), // Internal GamiPress listener
23 +
24 + // Purchase
25 + 'gamipress_wc_new_purchase' => __( 'Make a new purchase', 'gamipress' ),
26 + 'gamipress_wc_new_purchase_total' => __( 'Complete a purchase where total is equal, less or greater than a value', 'gamipress' ),
27 +
28 + 'gamipress_wc_new_product_purchase' => __( 'Purchase a product', 'gamipress' ),
29 + 'gamipress_wc_specific_product_purchase' => __( 'Purchase a specific product', 'gamipress' ),
30 + 'gamipress_wc_product_variation_purchase' => __( 'Purchase a specific product variation', 'gamipress' ),
31 +
32 + 'gamipress_wc_product_category_purchase' => __( 'Purchase a product of a specific category', 'gamipress' ),
33 + 'gamipress_wc_product_tag_purchase' => __( 'Purchase a product of a specific tag', 'gamipress' ),
34 + 'gamipress_wc_lifetime_value' => __( 'Lifetime value is equal, less or greater than a value', 'gamipress' ),
35 +
36 + // Reviews
37 + 'gamipress_wc_new_review' => __( 'Review a product', 'gamipress' ),
38 + 'gamipress_wc_specific_new_review' => __( 'Review a specific product', 'gamipress' ),
39 + 'gamipress_wc_get_review' => __( 'Vendor gets a review on a product', 'gamipress' ),
40 + 'gamipress_wc_get_specific_review' => __( 'Vendor gets a review on a specific product', 'gamipress' ),
41 +
42 + 'gamipress_wc_new_sale' => __( 'Vendor gets a new sale', 'gamipress' ),
43 +
44 + // Refund
45 + 'gamipress_wc_purchase_refund' => __( 'Refund a purchase', 'gamipress' ),
46 + 'gamipress_wc_product_refund' => __( 'Refund a product', 'gamipress' ),
47 + 'gamipress_wc_specific_product_refund' => __( 'Refund a specific product', 'gamipress' ),
48 + 'gamipress_wc_product_variation_refund' => __( 'Refund a specific product variation', 'gamipress' ),
49 +
50 + 'gamipress_wc_product_category_refund' => __( 'Refund a product of a specific category', 'gamipress' ),
51 + 'gamipress_wc_product_tag_refund' => __( 'Refund a product of a specific tag', 'gamipress' ),
52 +
53 + 'gamipress_wc_user_product_refund' => __( 'Vendor gets a product refunded', 'gamipress' ),
54 + );
55 +
56 + $triggers[__( 'WooCommerce Subscriptions', 'gamipress' )] = array(
57 + // Subscriptions
58 + 'gamipress_wc_subscription_purchase' => __( 'Purchase a subscription product', 'gamipress' ),
59 + 'gamipress_wc_specific_subscription_purchase' => __( 'Purchase a specific subscription product', 'gamipress' ),
60 + 'gamipress_wc_specific_subscription_variation_purchase' => __( 'Purchase a specific subscription variation of a product', 'gamipress' ),
61 + 'gamipress_wc_subscription_renewal' => __( 'Renew a subscription product', 'gamipress' ),
62 + 'gamipress_wc_specific_subscription_renewal' => __( 'Renew a specific subscription product', 'gamipress' ),
63 + 'gamipress_wc_specific_subscription_variation_renewal' => __( 'Renew a specific subscription variation of a product', 'gamipress' ),
64 +
65 + 'gamipress_wc_subscription_months_active' => __( 'Subscription active for a number of months', 'gamipress' ),
66 + 'gamipress_wc_specific_subscription_months_active' => __( 'Specific subscription active for a number of months', 'gamipress' ),
67 +
68 + 'gamipress_wc_subscription_cancelled' => __( 'Cancel a subscription product', 'gamipress' ),
69 + 'gamipress_wc_specific_subscription_cancelled' => __( 'Cancel a specific subscription product', 'gamipress' ),
70 + 'gamipress_wc_specific_subscription_variation_cancelled' => __( 'Cancel a specific subscription variation of a product', 'gamipress' ),
71 + 'gamipress_wc_subscription_expired' => __( 'Subscription product expires', 'gamipress' ),
72 + 'gamipress_wc_specific_subscription_expired' => __( 'Specific subscription product expires', 'gamipress' ),
73 + 'gamipress_wc_specific_subscription_variation_expired' => __( 'Specific subscription variation of a product expires', 'gamipress' ),
74 + );
75 +
76 + $triggers[__( 'WooCommerce Memberships', 'gamipress' )] = array(
77 + // Memberships
78 + 'gamipress_wc_membership_added' => __( 'Get added to a membership', 'gamipress' ),
79 + 'gamipress_wc_specific_membership_added' => __( 'Get added to a specific membership', 'gamipress' ),
80 + 'gamipress_wc_membership_cancelled' => __( 'Cancel a membership', 'gamipress' ),
81 + 'gamipress_wc_specific_membership_cancelled' => __( 'Cancel a specific membership', 'gamipress' ),
82 + 'gamipress_wc_membership_expired' => __( 'Membership expires', 'gamipress' ),
83 + 'gamipress_wc_specific_membership_expired' => __( 'Specific membership expires', 'gamipress' ),
84 + );
85 +
86 + return $triggers;
87 +
88 +}
89 +add_filter( 'gamipress_activity_triggers', 'gamipress_wc_activity_triggers' );
90 +
91 +/**
92 + * Register WooCommerce specific activity triggers
93 + *
94 + * @since 1.0.0
95 + *
96 + * @param array $specific_activity_triggers
97 + * @return array
98 + */
99 +function gamipress_wc_specific_activity_triggers( $specific_activity_triggers ) {
100 +
101 + // Purchase
102 + $specific_activity_triggers['gamipress_wc_specific_product_purchase'] = array( 'product' );
103 + $specific_activity_triggers['gamipress_wc_product_variation_purchase'] = array( 'product' );
104 + // Reviews
105 + $specific_activity_triggers['gamipress_wc_specific_new_review'] = array( 'product' );
106 + $specific_activity_triggers['gamipress_wc_get_specific_review'] = array( 'product' );
107 + // Refund
108 + $specific_activity_triggers['gamipress_wc_specific_product_refund'] = array( 'product' );
109 + $specific_activity_triggers['gamipress_wc_product_variation_refund'] = array( 'product' );
110 +
111 + // Subscriptions
112 + $specific_activity_triggers['gamipress_wc_specific_subscription_purchase'] = array( 'product' );
113 + $specific_activity_triggers['gamipress_wc_specific_subscription_renewal'] = array( 'product' );
114 + $specific_activity_triggers['gamipress_wc_specific_subscription_cancelled'] = array( 'product' );
115 + $specific_activity_triggers['gamipress_wc_specific_subscription_expired'] = array( 'product' );
116 + $specific_activity_triggers['gamipress_wc_specific_subscription_variation_purchase'] = array( 'product' );
117 + $specific_activity_triggers['gamipress_wc_specific_subscription_variation_renewal'] = array( 'product' );
118 + $specific_activity_triggers['gamipress_wc_specific_subscription_months_active'] = array( 'product' );
119 + $specific_activity_triggers['gamipress_wc_specific_subscription_variation_cancelled'] = array( 'product' );
120 + $specific_activity_triggers['gamipress_wc_specific_subscription_variation_expired'] = array( 'product' );
121 +
122 + // Memberships
123 + $specific_activity_triggers['gamipress_wc_specific_membership_added'] = array( 'wc_membership_plan' );
124 + $specific_activity_triggers['gamipress_wc_specific_membership_cancelled'] = array( 'wc_membership_plan' );
125 + $specific_activity_triggers['gamipress_wc_specific_membership_expired'] = array( 'wc_membership_plan' );
126 +
127 + return $specific_activity_triggers;
128 +}
129 +add_filter( 'gamipress_specific_activity_triggers', 'gamipress_wc_specific_activity_triggers' );
130 +
131 +/**
132 + * Build custom activity trigger label
133 + *
134 + * @param string $title
135 + * @param integer $requirement_id
136 + * @param array $requirement
137 + *
138 + * @return string
139 + */
140 +function gamipress_wc_activity_trigger_label( $title, $requirement_id, $requirement ) {
141 +
142 + switch( $requirement['trigger_type'] ) {
143 +
144 + // Product variation
145 + case 'gamipress_wc_product_variation_purchase':
146 + case 'gamipress_wc_product_variation_refund':
147 + case 'gamipress_wc_specific_subscription_variation_purchase':
148 + case 'gamipress_wc_specific_subscription_variation_renewal':
149 + case 'gamipress_wc_specific_subscription_variation_cancelled':
150 + case 'gamipress_wc_specific_subscription_variation_expired':
151 + $variation_id = ( isset( $requirement['wc_variation_id'] ) ) ? absint( $requirement['wc_variation_id'] ) : 0;
152 +
153 + if( $variation_id !== 0 ) {
154 +
155 + // Setup the pattern based on trigger type given
156 + $pattern = __( 'Purchase %s', 'gamipress' );
157 +
158 + if( $requirement['trigger_type'] === 'gamipress_wc_product_variation_refund' ) {
159 + $pattern = __( 'Refund %s', 'gamipress' );
160 + }
161 +
162 + if( $requirement['trigger_type'] === 'gamipress_wc_specific_subscription_variation_renewal' ) {
163 + $pattern = __( 'Renewal %s', 'gamipress' );
164 + }
165 +
166 + if( $requirement['trigger_type'] === 'gamipress_wc_specific_subscription_variation_cancelled' ) {
167 + $pattern = __( 'Cancel %s', 'gamipress' );
168 + }
169 +
170 + if( $requirement['trigger_type'] === 'gamipress_wc_specific_subscription_variation_expired' ) {
171 + $pattern = __( 'Expire %s', 'gamipress' );
172 + }
173 +
174 + // Return the custom title
175 + return sprintf( $pattern, gamipress_wc_get_product_variation_title( $requirement['achievement_post'], $variation_id, $requirement['achievement_post_site_id'] ) );
176 + }
177 + break;
178 + // Category
179 + case 'gamipress_wc_product_category_purchase':
180 + case 'gamipress_wc_product_category_refund':
181 + $category_id = ( isset( $requirement['wc_category_id'] ) ) ? absint( $requirement['wc_category_id'] ) : 0;
182 +
183 + if( $category_id !== 0 ) {
184 +
185 + // Setup the pattern based on trigger type given
186 + $pattern = __( 'Purchase a product of "%s" category', 'gamipress' );
187 +
188 + if( $requirement['trigger_type'] === 'gamipress_wc_product_variation_refund' ) {
189 + $pattern = __( 'Refund a product of "%s" category', 'gamipress' );
190 + }
191 +
192 + $category = get_term_by( 'term_id', $category_id, 'product_cat' );
193 +
194 + // Return the custom title
195 + return sprintf( $pattern, $category->name );
196 + }
197 + break;
198 + // Tag
199 + case 'gamipress_wc_product_tag_purchase':
200 + case 'gamipress_wc_product_tag_refund':
201 + $tag_id = ( isset( $requirement['wc_tag_id'] ) ) ? absint( $requirement['wc_tag_id'] ) : 0;
202 +
203 + if( $tag_id !== 0 ) {
204 +
205 + // Setup the pattern based on trigger type given
206 + $pattern = __( 'Purchase a product with "%s" tag', 'gamipress' );
207 +
208 + if( $requirement['trigger_type'] === 'gamipress_wc_product_variation_refund' ) {
209 + $pattern = __( 'Refund a product with "%s" tag', 'gamipress' );
210 + }
211 +
212 + $tag = get_term_by( 'term_id', $tag_id, 'product_tag' );
213 +
214 + // Return the custom title
215 + return sprintf( $pattern, $tag->name );
216 + }
217 + break;
218 +
219 + case 'gamipress_wc_new_purchase_total':
220 + $purchase_total = ( isset( $requirement['wc_purchase_total'] ) ) ? floatval( $requirement['wc_purchase_total'] ) : 0;
221 + $condition = ( isset( $requirement['wc_purchase_total_condition'] ) ) ? $requirement['wc_purchase_total_condition'] : 'equal';
222 + $conditions = gamipress_number_condition_options();
223 +
224 + // Complete a purchase where total is equal, less or greater than a value
225 + return sprintf( __( 'Complete a purchase where total is %s %s', 'gamipress' ), $conditions[$condition], $purchase_total );
226 + break;
227 + case 'gamipress_wc_lifetime_value':
228 + $lifetime = ( isset( $requirement['wc_lifetime'] ) ) ? floatval( $requirement['wc_lifetime'] ) : 0;
229 + $condition = ( isset( $requirement['wc_lifetime_condition'] ) ) ? $requirement['wc_lifetime_condition'] : 'equal';
230 + $conditions = gamipress_number_condition_options();
231 +
232 + // Lifetime value is equal, less or greater than a value
233 + return sprintf( __( 'Lifetime value %s %s', 'gamipress' ), $conditions[$condition], $lifetime );
234 + break;
235 + case 'gamipress_wc_subscription_months_active':
236 + case 'gamipress_wc_specific_subscription_months_active':
237 + $active_time = ( isset( $requirement['wc_subscription_months'] ) ) ? floatval( $requirement['wc_subscription_months'] ) : 0;
238 +
239 + return sprintf( __( 'Subscription active for %s months', 'gamipress' ), $active_time );
240 + break;
241 +
242 + }
243 +
244 + return $title;
245 +}
246 +add_filter( 'gamipress_activity_trigger_label', 'gamipress_wc_activity_trigger_label', 10, 3 );
247 +
248 +/**
249 + * Register WooCommerce specific activity triggers labels
250 + *
251 + * @since 1.0.0
252 + *
253 + * @param array $specific_activity_trigger_labels
254 + * @return array
255 + */
256 +function gamipress_wc_specific_activity_trigger_label( $specific_activity_trigger_labels ) {
257 +
258 + // Purchase
259 + $specific_activity_trigger_labels['gamipress_wc_specific_product_purchase'] = __( 'Purchase %s', 'gamipress' );
260 + $specific_activity_trigger_labels['gamipress_wc_product_variation_purchase'] = __( 'Purchase %s', 'gamipress' );
261 + // Reviews
262 + $specific_activity_trigger_labels['gamipress_wc_specific_new_review'] = __( 'Review %s', 'gamipress' );
263 + $specific_activity_trigger_labels['gamipress_wc_get_specific_review'] = __( 'Vendor gets a review on %s', 'gamipress' );
264 + // Refund
265 + $specific_activity_trigger_labels['gamipress_wc_specific_product_refund'] = __( 'Refund %s', 'gamipress' );
266 + $specific_activity_trigger_labels['gamipress_wc_product_variation_refund'] = __( 'Refund %s', 'gamipress' );
267 +
268 + // Subscriptions
269 + $specific_activity_trigger_labels['gamipress_wc_specific_subscription_purchase'] = __( 'Purchase %s subscription', 'gamipress' );
270 + $specific_activity_trigger_labels['gamipress_wc_specific_subscription_renewal'] = __( 'Renew %s subscription', 'gamipress' );
271 + $specific_activity_trigger_labels['gamipress_wc_specific_subscription_cancelled'] = __( 'Cancel %s subscription', 'gamipress' );
272 + $specific_activity_trigger_labels['gamipress_wc_specific_subscription_expired'] = __( '%s subscription expires', 'gamipress' );
273 + $specific_activity_trigger_labels['gamipress_wc_specific_subscription_variation_purchase'] = __( 'Purchase %s subscription variation', 'gamipress' );
274 + $specific_activity_trigger_labels['gamipress_wc_specific_subscription_variation_renewal'] = __( 'Renew %s subscription variation', 'gamipress' );
275 + $specific_activity_trigger_labels['gamipress_wc_specific_subscription_variation_cancelled'] = __( 'Cancel %s subscription variation', 'gamipress' );
276 + $specific_activity_trigger_labels['gamipress_wc_specific_subscription_variation_expired'] = __( '%s subscription variation expires', 'gamipress' );
277 + $specific_activity_trigger_labels['gamipress_wc_specific_subscription_months_active'] = __( 'Subscription %s active', 'gamipress' );
278 +
279 + // Memberships
280 + $specific_activity_trigger_labels['gamipress_wc_specific_membership_added'] = __( 'Get added to %s membership', 'gamipress' );
281 + $specific_activity_trigger_labels['gamipress_wc_specific_membership_cancelled'] = __( 'Cancel %s membership', 'gamipress' );
282 + $specific_activity_trigger_labels['gamipress_wc_specific_membership_expires'] = __( '%s membership expires', 'gamipress' );
283 +
284 + return $specific_activity_trigger_labels;
285 +}
286 +add_filter( 'gamipress_specific_activity_trigger_label', 'gamipress_wc_specific_activity_trigger_label' );
287 +
288 +
289 +/**
290 + * Get user for a given trigger action.
291 + *
292 + * @since 1.0.0
293 + *
294 + * @param integer $user_id user ID to override.
295 + * @param string $trigger Trigger name.
296 + * @param array $args Passed trigger args.
297 + * @return integer User ID.
298 + */
299 +function gamipress_wc_trigger_get_user_id( $user_id, $trigger, $args ) {
300 +
301 + switch ( $trigger ) {
302 + case 'gamipress_publish_product': // Internal GamiPress listener
303 + // Purchase
304 + case 'gamipress_wc_new_purchase':
305 + case 'gamipress_wc_new_purchase_total':
306 + case 'gamipress_wc_new_product_purchase':
307 + case 'gamipress_wc_specific_product_purchase':
308 + case 'gamipress_wc_product_variation_purchase':
309 + case 'gamipress_wc_product_category_purchase':
310 + case 'gamipress_wc_product_tag_purchase':
311 + case 'gamipress_wc_lifetime_value':
312 + // Reviews
313 + case 'gamipress_wc_new_review':
314 + case 'gamipress_wc_specific_new_review':
315 + case 'gamipress_wc_get_review':
316 + case 'gamipress_wc_get_specific_review':
317 + case 'gamipress_wc_new_sale':
318 + // Refund
319 + case 'gamipress_wc_purchase_refund':
320 + case 'gamipress_wc_product_refund':
321 + case 'gamipress_wc_specific_product_refund':
322 + case 'gamipress_wc_product_variation_refund':
323 + case 'gamipress_wc_product_category_refund':
324 + case 'gamipress_wc_product_tag_refund':
325 + case 'gamipress_wc_user_product_refund':
326 + // Subscriptions
327 + case 'gamipress_wc_subscription_purchase':
328 + case 'gamipress_wc_specific_subscription_purchase':
329 + case 'gamipress_wc_subscription_renewal':
330 + case 'gamipress_wc_specific_subscription_renewal':
331 + case 'gamipress_wc_subscription_cancelled':
332 + case 'gamipress_wc_specific_subscription_cancelled':
333 + case 'gamipress_wc_subscription_expired':
334 + case 'gamipress_wc_specific_subscription_expired':
335 + case 'gamipress_wc_specific_subscription_variation_purchase':
336 + case 'gamipress_wc_specific_subscription_variation_renewal':
337 + case 'gamipress_wc_specific_subscription_variation_cancelled':
338 + case 'gamipress_wc_specific_subscription_variation_expired':
339 + case 'gamipress_wc_subscription_months_active':
340 + case 'gamipress_wc_specific_subscription_months_active':
341 + // Memberships
342 + case 'gamipress_wc_membership_added':
343 + case 'gamipress_wc_specific_membership_added':
344 + case 'gamipress_wc_membership_cancelled':
345 + case 'gamipress_wc_specific_membership_cancelled':
346 + case 'gamipress_wc_membership_expired':
347 + case 'gamipress_wc_specific_membership_expired':
348 + $user_id = $args[1];
349 + break;
350 + }
351 +
352 + return $user_id;
353 +
354 +}
355 +add_filter( 'gamipress_trigger_get_user_id', 'gamipress_wc_trigger_get_user_id', 10, 3 );
356 +
357 +/**
358 + * Get the id for a given specific trigger action.
359 + *
360 + * @since 1.0.1
361 + *
362 + * @param integer $specific_id Specific ID to override.
363 + * @param string $trigger Trigger name.
364 + * @param array $args Passed trigger args.
365 + *
366 + * @return integer Specific ID.
367 + */
368 +function gamipress_wc_specific_trigger_get_id( $specific_id, $trigger, $args ) {
369 +
370 + switch ( $trigger ) {
371 + // Purchase
372 + case 'gamipress_wc_specific_product_purchase':
373 + case 'gamipress_wc_product_variation_purchase':
374 + case 'gamipress_wc_specific_subscription_purchase':
375 + case 'gamipress_wc_specific_subscription_expired':
376 + // Refund
377 + case 'gamipress_wc_specific_product_refund':
378 + case 'gamipress_wc_product_variation_refund':
379 + // Memberships
380 + case 'gamipress_wc_specific_membership_added':
381 + case 'gamipress_wc_specific_membership_cancelled':
382 + case 'gamipress_wc_specific_membership_expired':
383 + $specific_id = $args[0];
384 + break;
385 + // Reviews
386 + case 'gamipress_wc_specific_new_review':
387 + case 'gamipress_wc_get_specific_review':
388 + $specific_id = $args[2];
389 + break;
390 + // Subscriptions
391 + case 'gamipress_wc_specific_subscription_renewal':
392 + case 'gamipress_wc_specific_subscription_cancelled':
393 + case 'gamipress_wc_specific_subscription_purchase':
394 + case 'gamipress_wc_specific_subscription_expired':
395 + case 'gamipress_wc_specific_subscription_variation_purchase':
396 + case 'gamipress_wc_specific_subscription_variation_renewal':
397 + case 'gamipress_wc_specific_subscription_variation_cancelled':
398 + case 'gamipress_wc_specific_subscription_variation_expired':
399 + case 'gamipress_wc_specific_subscription_months_active':
400 + $specific_id = $args[0];
401 + break;
402 + }
403 +
404 + return $specific_id;
405 +
406 +}
407 +
408 +add_filter( 'gamipress_specific_trigger_get_id', 'gamipress_wc_specific_trigger_get_id', 10, 3 );
409 +
410 +/**
411 + * Extended meta data for event trigger logging
412 + *
413 + * @since 1.0.2
414 + *
415 + * @param array $log_meta
416 + * @param integer $user_id
417 + * @param string $trigger
418 + * @param integer $site_id
419 + * @param array $args
420 + *
421 + * @return array
422 + */
423 +function gamipress_wc_log_event_trigger_meta_data( $log_meta, $user_id, $trigger, $site_id, $args ) {
424 +
425 + switch ( $trigger ) {
426 + case 'gamipress_publish_product': // Internal GamiPress listener
427 + // Add the product ID
428 + $log_meta['product_id'] = $args[0];
429 + $log_meta['post_id'] = $args[0]; // Post ID added to make column visible on logs list
430 + break;
431 + // Purchase
432 + case 'gamipress_wc_new_product_purchase':
433 + case 'gamipress_wc_specific_product_purchase':
434 + case 'gamipress_wc_new_sale':
435 + // Refund
436 + case 'gamipress_wc_product_refund':
437 + case 'gamipress_wc_specific_product_refund':
438 + case 'gamipress_wc_user_product_refund':
439 + // Subscriptions
440 + case 'gamipress_wc_subscription_purchase':
441 + case 'gamipress_wc_specific_subscription_purchase':
442 + case 'gamipress_wc_subscription_renewal':
443 + case 'gamipress_wc_specific_subscription_renewal':
444 + case 'gamipress_wc_subscription_cancelled':
445 + case 'gamipress_wc_specific_subscription_cancelled':
446 + case 'gamipress_wc_subscription_expired':
447 + case 'gamipress_wc_specific_subscription_expired':
448 + case 'gamipress_wc_subscription_months_active':
449 + case 'gamipress_wc_specific_subscription_months_active':
450 + // Add the product and order IDs
451 + $log_meta['product_id'] = $args[0];
452 + $log_meta['post_id'] = $args[0]; // Post ID added to make column visible on logs list
453 + $log_meta['order_id'] = $args[2];
454 + break;
455 + case 'gamipress_wc_product_variation_purchase':
456 + case 'gamipress_wc_product_variation_refund':
457 + case 'gamipress_wc_specific_subscription_variation_purchase':
458 + case 'gamipress_wc_specific_subscription_variation_renewal':
459 + case 'gamipress_wc_specific_subscription_variation_cancelled':
460 + case 'gamipress_wc_specific_subscription_variation_expired':
461 + // Add the product, variation and order IDs
462 + $log_meta['product_id'] = $args[0];
463 + $log_meta['post_id'] = $args[0]; // Post ID added to make column visible on logs list
464 + $log_meta['variation_id'] = $args[2];
465 + $log_meta['order_id'] = $args[3];
466 + break;
467 + case 'gamipress_wc_product_category_purchase':
468 + case 'gamipress_wc_product_category_refund':
469 + // Add the product, category and order IDs
470 + $log_meta['product_id'] = $args[0];
471 + $log_meta['post_id'] = $args[0]; // Post ID added to make column visible on logs list
472 + $log_meta['category_id'] = $args[2];
473 + $log_meta['order_id'] = $args[3];
474 + break;
475 + case 'gamipress_wc_product_tag_purchase':
476 + case 'gamipress_wc_product_tag_refund':
477 + // Add the product, tag and order IDs
478 + $log_meta['product_id'] = $args[0];
479 + $log_meta['post_id'] = $args[0]; // Post ID added to make column visible on logs list
480 + $log_meta['tag_id'] = $args[2];
481 + $log_meta['order_id'] = $args[3];
482 + break;
483 + case 'gamipress_wc_new_purchase':
484 + case 'gamipress_wc_purchase_refund':
485 + // Add the order ID
486 + $log_meta['order_id'] = $args[0];
487 + break;
488 + // Purchase total
489 + case 'gamipress_wc_new_purchase_total':
490 + $log_meta['order_id'] = $args[0];
491 + $log_meta['purchase_total'] = $args[2];
492 + break;
493 + // Reviews
494 + case 'gamipress_wc_new_review':
495 + case 'gamipress_wc_specific_new_review':
496 + // Add the comment and product IDs
497 + $log_meta['comment_id'] = $args[0];
498 + $log_meta['product_id'] = $args[2];
499 + break;
500 + case 'gamipress_wc_get_review':
501 + case 'gamipress_wc_get_specific_review':
502 + // Add the comment, reviewer and product IDs
503 + $log_meta['comment_id'] = $args[0];
504 + $log_meta['product_id'] = $args[2];
505 + $log_meta['reviewer_id'] = $args[3];
506 + break;
507 + // Memberships
508 + case 'gamipress_wc_membership_added':
509 + case 'gamipress_wc_specific_membership_added':
510 + case 'gamipress_wc_membership_cancelled':
511 + case 'gamipress_wc_specific_membership_cancelled':
512 + case 'gamipress_wc_membership_expired':
513 + case 'gamipress_wc_specific_membership_expired':
514 + // Add the membership and order IDs
515 + $log_meta['post_id'] = $args[0]; // Post ID added to make column visible on logs list
516 + $log_meta['order_id'] = $args[2];
517 + break;
518 + // Lifetime
519 + case 'gamipress_wc_lifetime_value':
520 + $log_meta['lifetime_value'] = $args[0];
521 +
522 +
523 + }
524 +
525 + return $log_meta;
526 +}
527 +add_filter( 'gamipress_log_event_trigger_meta_data', 'gamipress_wc_log_event_trigger_meta_data', 10, 5 );
528 +
529 +/**
530 + * Override the meta data to filter the logs count
531 + *
532 + * @since 1.1.5
533 + *
534 + * @param array $log_meta The meta data to filter the logs count
535 + * @param int $user_id The given user's ID
536 + * @param string $trigger The given trigger we're checking
537 + * @param int $since The since timestamp where retrieve the logs
538 + * @param int $site_id The desired Site ID to check
539 + * @param array $args The triggered args or requirement object
540 + *
541 + * @return array The meta data to filter the logs count
542 + */
543 +function gamipress_wc_get_user_trigger_count_log_meta( $log_meta, $user_id, $trigger, $since, $site_id, $args ) {
544 +
545 + switch( $trigger ) {
546 + // Variation
547 + case 'gamipress_wc_product_variation_purchase':
548 + case 'gamipress_wc_product_variation_refund':
549 + case 'gamipress_wc_specific_subscription_variation_purchase':
550 + case 'gamipress_wc_specific_subscription_variation_renewal':
551 + case 'gamipress_wc_specific_subscription_variation_cancelled':
552 + case 'gamipress_wc_specific_subscription_variation_expired':
553 + if( isset( $args[0] ) && isset( $args[2] ) ) {
554 + // Add the product and variation IDs
555 + $log_meta['product_id'] = absint( $args[0] );
556 + $log_meta['variation_id'] = absint( $args[2] );
557 + }
558 +
559 + // $args could be a requirement object
560 + if( isset( $args['wc_variation_id'] ) && isset( $args['achievement_post'] ) ) {
561 + // Add the product and variation IDs
562 + $log_meta['product_id'] = absint( $args['achievement_post'] );
563 + $log_meta['variation_id'] = absint( $args['wc_variation_id'] );
564 + }
565 + break;
566 + // Category
567 + case 'gamipress_wc_product_category_purchase':
568 + case 'gamipress_wc_product_category_refund':
569 + if( isset( $args[2] ) ) {
570 + // Add the product category ID
571 + $log_meta['category_id'] = $args[2];
572 + }
573 +
574 + // $args could be a requirement object
575 + if( isset( $args['wc_category_id'] ) ) {
576 + // Add the product category ID
577 + $log_meta['category_id'] = $args['wc_category_id'];
578 + }
579 + break;
580 + // Tag
581 + case 'gamipress_wc_product_tag_purchase':
582 + case 'gamipress_wc_product_tag_refund':
583 + if( isset( $args[2] ) ) {
584 + // Add the product tag ID
585 + $log_meta['tag_id'] = $args[2];
586 + }
587 +
588 + // $args could be a requirement object
589 + if( isset( $args['wc_tag_id'] ) ) {
590 + // Add the product tag ID
591 + $log_meta['tag_id'] = $args['wc_tag_id'];
592 + }
593 + break;
594 + }
595 +
596 + return $log_meta;
597 +
598 +}
599 +add_filter( 'gamipress_get_user_trigger_count_log_meta', 'gamipress_wc_get_user_trigger_count_log_meta', 10, 6 );
600 +
601 +/**
602 + * Extra data fields
603 + *
604 + * @since 1.1.3
605 + *
606 + * @param array $fields
607 + * @param int $log_id
608 + * @param string $type
609 + *
610 + * @return array
611 + */
612 +function gamipress_wc_log_extra_data_fields( $fields, $log_id, $type ) {
613 +
614 + $prefix = '_gamipress_';
615 +
616 + $log = ct_get_object( $log_id );
617 + $trigger = $log->trigger_type;
618 +
619 + if( $type !== 'event_trigger' ) {
620 + return $fields;
621 + }
622 +
623 + switch( $trigger ) {
624 + // Product variation
625 + case 'gamipress_wc_product_variation_purchase':
626 + case 'gamipress_wc_product_variation_refund':
627 + case 'gamipress_wc_specific_subscription_variation_purchase':
628 + case 'gamipress_wc_specific_subscription_variation_renewal':
629 + case 'gamipress_wc_specific_subscription_variation_cancelled':
630 + case 'gamipress_wc_specific_subscription_variation_expired':
631 +
632 + // TODO: Important! call to wc_get_product() causes a value lost on all extra data fields
633 +
634 + $variation_id = ct_get_object_meta( $log_id, $prefix . 'variation_id', true );
635 +
636 + $variation_attributes = wc_get_product_variation_attributes( $variation_id );
637 + $attributes = array();
638 +
639 + foreach( $variation_attributes as $attribute ) {
640 + if( ! empty( $attribute ) ) {
641 + $attributes[] = $attribute;
642 + }
643 + }
644 +
645 + $fields[] = array(
646 + 'name' => __( 'Variation', 'gamipress' ),
647 + 'desc' => __( 'Variation attached to this log.', 'gamipress' ),
648 + 'id' => $prefix . 'variation_id',
649 + 'type' => 'select',
650 + 'options' => array(
651 + $variation_id => ( ! empty( $attributes ) ? implode( ', ', $attributes ) . ' (#' . $variation_id . ')' : '' )
652 + )
653 + );
654 + break;
655 + // Category
656 + case 'gamipress_wc_product_category_purchase':
657 + case 'gamipress_wc_product_category_refund':
658 +
659 + // Get categories stored and turn them into an array of options
660 + $categories = get_terms( array(
661 + 'taxonomy' => 'product_cat',
662 + 'hide_empty' => false,
663 + ) );
664 +
665 + $options = array();
666 +
667 + foreach( $categories as $category ) {
668 + $options[$category->term_id] = $category->name;
669 + }
670 +
671 + $fields[] = array(
672 + 'name' => __( 'Category', 'gamipress' ),
673 + 'desc' => __( 'Category attached to this log.', 'gamipress' ),
674 + 'id' => $prefix . 'category_id',
675 + 'type' => 'select',
676 + 'options' => $options,
677 + );
678 + break;
679 + // Tag
680 + case 'gamipress_wc_product_tag_purchase':
681 + case 'gamipress_wc_product_tag_refund':
682 +
683 + // Get tags stored and turn them into an array of options
684 + $tags = get_terms( array(
685 + 'taxonomy' => 'product_tag',
686 + 'hide_empty' => false,
687 + ) );
688 +
689 + $options = array();
690 +
691 + foreach( $tags as $tag ) {
692 + $options[$tag->term_id] = $tag->name;
693 + }
694 +
695 + $fields[] = array(
696 + 'name' => __( 'Tag', 'gamipress' ),
697 + 'desc' => __( 'Tag attached to this log.', 'gamipress' ),
698 + 'id' => $prefix . 'tag_id',
699 + 'type' => 'select',
700 + 'options' => $options,
701 + );
702 + break;
703 + }
704 +
705 + return $fields;
706 +
707 +}
708 +add_filter( 'gamipress_log_extra_data_fields', 'gamipress_wc_log_extra_data_fields', 10, 3 );
709 +
710 +/**
711 + * Extra filter to check duplicated activity
712 + *
713 + * @since 1.0.2
714 + *
715 + * @param bool $return
716 + * @param integer $user_id
717 + * @param string $trigger
718 + * @param integer $site_id
719 + * @param array $args
720 + *
721 + * @return bool True if user deserves trigger, else false
722 + */
723 +function gamipress_wc_trigger_duplicity_check( $return, $user_id, $trigger, $site_id, $args ) {
724 +
725 + // If user doesn't deserves trigger, then bail to prevent grant access
726 + if( ! $return )
727 + return $return;
728 +
729 + $log_meta = array(
730 + 'type' => 'event_trigger',
731 + 'trigger_type' => $trigger,
732 + );
733 +
734 + switch ( $trigger ) {
735 + case 'gamipress_publish_product': // Internal GamiPress listener
736 + // User can not create same product more times, so check it
737 + $log_meta['post_id'] = gamipress_get_event_arg( $args, 'post_id', 0 );
738 + $return = (bool) ( gamipress_get_user_last_log( $user_id, $log_meta ) === false );
739 + break;
740 + case 'gamipress_wc_new_review':
741 + case 'gamipress_wc_specific_new_review':
742 + case 'gamipress_wc_get_review':
743 + case 'gamipress_wc_get_specific_review':
744 + // User can not create same comment more times, so check it
745 + $log_meta['comment_id'] = $args[0];
746 + $return = (bool) ( gamipress_get_user_last_log( $user_id, $log_meta ) === false );
747 + break;
748 + // Purchase
749 + case 'gamipress_wc_new_product_purchase':
750 + case 'gamipress_wc_specific_product_purchase':
751 + case 'gamipress_wc_new_sale':
752 + // Refund
753 + case 'gamipress_wc_product_refund':
754 + case 'gamipress_wc_specific_product_refund':
755 + case 'gamipress_wc_user_product_refund':
756 + // Subscriptions
757 + case 'gamipress_wc_subscription_purchase':
758 + case 'gamipress_wc_specific_subscription_purchase':
759 + case 'gamipress_wc_subscription_renewal':
760 + case 'gamipress_wc_specific_subscription_renewal':
761 + case 'gamipress_wc_subscription_cancelled':
762 + case 'gamipress_wc_specific_subscription_cancelled':
763 + case 'gamipress_wc_subscription_expired':
764 + case 'gamipress_wc_specific_subscription_expired':
765 +
766 + $quantity = isset( $args[3] ) ? $args[3] : 1;
767 +
768 + // Don't perform duplicity check if quantity is higher than 1
769 + if( $quantity > 1 ) {
770 + break;
771 + }
772 +
773 + // User can not place same product and order IDs more times, so check it
774 + $log_meta['product_id'] = $args[0];
775 + $log_meta['order_id'] = $args[2];
776 + $return = (bool) ( gamipress_get_user_last_log( $user_id, $log_meta ) === false );
777 + break;
778 + // Variation
779 + case 'gamipress_wc_product_variation_purchase':
780 + case 'gamipress_wc_product_variation_refund':
781 +
782 + $quantity = isset( $args[4] ) ? $args[4] : 1;
783 +
784 + // Don't perform duplicity check if quantity is higher than 1
785 + if( $quantity > 1 ) {
786 + break;
787 + }
788 +
789 + // User can not place same product and order ID more times, so check it
790 + $log_meta['product_id'] = $args[0];
791 + $log_meta['variation_id'] = $args[2];
792 + $log_meta['order_id'] = $args[3];
793 + $return = (bool) ( gamipress_get_user_last_log( $user_id, $log_meta ) === false );
794 + break;
795 + // Category
796 + case 'gamipress_wc_product_category_purchase':
797 + case 'gamipress_wc_product_category_refund':
798 +
799 + $quantity = isset( $args[4] ) ? $args[4] : 1;
800 +
801 + // Don't perform duplicity check if quantity is higher than 1
802 + if( $quantity > 1 ) {
803 + break;
804 + }
805 +
806 + // User can not place same product, category and order ID more times, so check it
807 + $log_meta['product_id'] = $args[0];
808 + $log_meta['category_id'] = $args[2];
809 + $log_meta['order_id'] = $args[3];
810 + $return = (bool) ( gamipress_get_user_last_log( $user_id, $log_meta ) === false );
811 + break;
812 + // Tag
813 + case 'gamipress_wc_product_tag_purchase':
814 + case 'gamipress_wc_product_tag_refund':
815 +
816 + $quantity = isset( $args[4] ) ? $args[4] : 1;
817 +
818 + // Don't perform duplicity check if quantity is higher than 1
819 + if( $quantity > 1 ) {
820 + break;
821 + }
822 +
823 + // User can not place same product, tag and order ID more times, so check it
824 + $log_meta['product_id'] = $args[0];
825 + $log_meta['tag_id'] = $args[2];
826 + $log_meta['order_id'] = $args[3];
827 + $return = (bool) ( gamipress_get_user_last_log( $user_id, $log_meta ) === false );
828 + break;
829 + case 'gamipress_wc_new_purchase':
830 + case 'gamipress_wc_purchase_refund':
831 + // User can not place or refund same order ID more times, so check it
832 + $log_meta['order_id'] = $args[0];
833 + $return = (bool) ( gamipress_get_user_last_log( $user_id, $log_meta ) === false );
834 + break;
835 + }
836 +
837 + return $return;
838 +
839 +}
840 +add_filter( 'gamipress_user_deserves_trigger', 'gamipress_wc_trigger_duplicity_check', 10, 5 );