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