| @@ -1,0 +1,144 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Triggers | |
| 4 | + * | |
| 5 | + * @package GamiPress\SureCart\Triggers | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Register SureCart activity triggers | |
| 14 | + * | |
| 15 | + * @param array $triggers | |
| 16 | + * @return mixed | |
| 17 | + */ | |
| 18 | +function gamipress_surecart_activity_triggers( $triggers ) { | |
| 19 | + | |
| 20 | + $triggers[__( 'SureCart', 'gamipress' )] = array( | |
| 21 | + | |
| 22 | + 'gamipress_surecart_new_purchase' => __( 'Make a new purchase', 'gamipress' ), | |
| 23 | + 'gamipress_surecart_new_product_purchase' => __( 'Purchase a product', 'gamipress' ), | |
| 24 | + 'gamipress_surecart_specific_product_purchase' => __( 'Purchase a specific product', 'gamipress' ), | |
| 25 | + | |
| 26 | + ); | |
| 27 | + | |
| 28 | + return $triggers; | |
| 29 | + | |
| 30 | +} | |
| 31 | +add_filter( 'gamipress_activity_triggers', 'gamipress_surecart_activity_triggers' ); | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * Build custom activity trigger label | |
| 35 | + * | |
| 36 | + * @since 1.0.0 | |
| 37 | + * | |
| 38 | + * @param string $title | |
| 39 | + * @param integer $requirement_id | |
| 40 | + * @param array $requirement | |
| 41 | + * | |
| 42 | + * @return string | |
| 43 | + */ | |
| 44 | +function gamipress_surecart_activity_trigger_label( $title, $requirement_id, $requirement ) { | |
| 45 | + | |
| 46 | + $product_id = ( isset( $requirement['surecart_product'] ) ) ? $requirement['surecart_product'] : ''; | |
| 47 | + | |
| 48 | + switch( $requirement['trigger_type'] ) { | |
| 49 | + case 'gamipress_surecart_specific_product_purchase': | |
| 50 | + | |
| 51 | + // Get the products | |
| 52 | + $products = SureCart\Models\Product::get(); | |
| 53 | + | |
| 54 | + foreach ( $products as $product ) { | |
| 55 | + if ( $product->id === $product_id ) { | |
| 56 | + $product_name = $product->name; | |
| 57 | + break; | |
| 58 | + } | |
| 59 | + } | |
| 60 | + | |
| 61 | + return sprintf( __( 'Purchase %s', 'gamipress' ), $product_name ); | |
| 62 | + break; | |
| 63 | + | |
| 64 | + } | |
| 65 | + | |
| 66 | + return $title; | |
| 67 | +} | |
| 68 | +add_filter( 'gamipress_activity_trigger_label', 'gamipress_surecart_activity_trigger_label', 10, 3 ); | |
| 69 | + | |
| 70 | + | |
| 71 | +/** | |
| 72 | + * Register SureCart specific activity triggers labels | |
| 73 | + * | |
| 74 | + * @since 1.0.0 | |
| 75 | + * | |
| 76 | + * @param array $specific_activity_trigger_labels | |
| 77 | + * @return array | |
| 78 | + */ | |
| 79 | +function gamipress_surecart_specific_activity_trigger_label( $specific_activity_trigger_labels ) { | |
| 80 | + | |
| 81 | + // Purchase | |
| 82 | + $specific_activity_trigger_labels['gamipress_surecart_specific_product_purchase'] = __( 'Purchase %s', 'gamipress' ); | |
| 83 | + | |
| 84 | + return $specific_activity_trigger_labels; | |
| 85 | +} | |
| 86 | +add_filter( 'gamipress_specific_activity_trigger_label', 'gamipress_surecart_specific_activity_trigger_label' ); | |
| 87 | + | |
| 88 | + | |
| 89 | +/** | |
| 90 | + * Get user for a given trigger action. | |
| 91 | + * | |
| 92 | + * @since 1.0.0 | |
| 93 | + * | |
| 94 | + * @param integer $user_id user ID to override. | |
| 95 | + * @param string $trigger Trigger name. | |
| 96 | + * @param array $args Passed trigger args. | |
| 97 | + * @return integer User ID. | |
| 98 | + */ | |
| 99 | +function gamipress_surecart_trigger_get_user_id( $user_id, $trigger, $args ) { | |
| 100 | + | |
| 101 | + switch ( $trigger ) { | |
| 102 | + | |
| 103 | + // Purchase | |
| 104 | + case 'gamipress_surecart_new_purchase': | |
| 105 | + case 'gamipress_surecart_new_product_purchase': | |
| 106 | + $user_id = $args[1]; | |
| 107 | + break; | |
| 108 | + } | |
| 109 | + | |
| 110 | + return $user_id; | |
| 111 | + | |
| 112 | +} | |
| 113 | +add_filter( 'gamipress_trigger_get_user_id', 'gamipress_surecart_trigger_get_user_id', 10, 3 ); | |
| 114 | + | |
| 115 | +/** | |
| 116 | + * Extended meta data for event trigger logging | |
| 117 | + * | |
| 118 | + * @since 1.0.2 | |
| 119 | + * | |
| 120 | + * @param array $log_meta | |
| 121 | + * @param integer $user_id | |
| 122 | + * @param string $trigger | |
| 123 | + * @param integer $site_id | |
| 124 | + * @param array $args | |
| 125 | + * | |
| 126 | + * @return array | |
| 127 | + */ | |
| 128 | +function gamipress_surecart_log_event_trigger_meta_data( $log_meta, $user_id, $trigger, $site_id, $args ) { | |
| 129 | + | |
| 130 | + switch ( $trigger ) { | |
| 131 | + // Purchase | |
| 132 | + case 'gamipress_surecart_new_product_purchase': | |
| 133 | + case 'gamipress_surecart_specific_product_purchase': | |
| 134 | + // Add the product and order IDs | |
| 135 | + $log_meta['product_id'] = $args[0]; | |
| 136 | + $log_meta['order_id'] = $args[2]; | |
| 137 | + break; | |
| 138 | + | |
| 139 | + | |
| 140 | + } | |
| 141 | + | |
| 142 | + return $log_meta; | |
| 143 | +} | |
| 144 | +add_filter( 'gamipress_log_event_trigger_meta_data', 'gamipress_surecart_log_event_trigger_meta_data', 10, 5 ); | |