| 1 |
<?php |
| 2 |
/** |
| 3 |
* Purchase Ticket |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\Eventin\Triggers\Purchase_Ticket |
| 6 |
* @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class AutomatorWP_Eventin_Purchase_Ticket extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'eventin'; |
| 15 |
public $trigger = 'eventin_purchase_ticket'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Register the trigger |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
public function register() { |
| 23 |
|
| 24 |
automatorwp_register_trigger( $this->trigger, array( |
| 25 |
'integration' => $this->integration, |
| 26 |
'label' => __( 'User purchases a ticket for an event', 'automatorwp' ), |
| 27 |
'select_option' => __( 'User purchases a <strong>ticket</strong> for an event', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Event. %2$s: Number of times. */ |
| 29 |
'edit_label' => sprintf( __( 'User purchases a ticket for %1$s %2$s time(s)', 'automatorwp' ), '{post}', '{times}' ), |
| 30 |
'log_label' => sprintf( __( 'User purchases a ticket for %1$s', 'automatorwp' ), '{post}' ), |
| 31 |
'action' => 'eventin_order_completed', |
| 32 |
'function' => array( $this, 'listener' ), |
| 33 |
'priority' => 10, |
| 34 |
'accepted_args' => 1, |
| 35 |
'options' => array( |
| 36 |
'post' => automatorwp_utilities_post_option( array( |
| 37 |
'name' => __( 'Event:', 'automatorwp' ), |
| 38 |
'option_none_label' => __( 'any event', 'automatorwp' ), |
| 39 |
'post_type' => 'etn' |
| 40 |
) ), |
| 41 |
'times' => automatorwp_utilities_times_option(), |
| 42 |
), |
| 43 |
'tags' => array_merge( |
| 44 |
automatorwp_eventin_get_order_tags(), |
| 45 |
automatorwp_utilities_post_tags(), |
| 46 |
automatorwp_utilities_times_tag() |
| 47 |
) |
| 48 |
) ); |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Trigger listener |
| 54 |
* |
| 55 |
* @since 1.0.0 |
| 56 |
* |
| 57 |
* @param OrderModel $order $order data |
| 58 |
*/ |
| 59 |
public function listener( $order ) { |
| 60 |
|
| 61 |
$user_id = get_current_user_id( ); |
| 62 |
|
| 63 |
// Bail if user is not logged |
| 64 |
if ( $user_id === 0 ){ |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
// Get the event related to order |
| 69 |
$event_id = get_post_meta( $order->id, 'event_id', true ); |
| 70 |
|
| 71 |
// Trigger purchase ticket |
| 72 |
automatorwp_trigger_event( array( |
| 73 |
'trigger' => $this->trigger, |
| 74 |
'user_id' => $user_id, |
| 75 |
'post_id' => $event_id, |
| 76 |
'order_id' => $order->id, |
| 77 |
) ); |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* User deserves check |
| 83 |
* |
| 84 |
* @since 1.0.0 |
| 85 |
* |
| 86 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 87 |
* @param stdClass $trigger The trigger object |
| 88 |
* @param int $user_id The user ID |
| 89 |
* @param array $event Event information |
| 90 |
* @param array $trigger_options The trigger's stored options |
| 91 |
* @param stdClass $automation The trigger's automation object |
| 92 |
* |
| 93 |
* @return bool True if user deserves trigger, false otherwise |
| 94 |
*/ |
| 95 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 96 |
|
| 97 |
// Don't deserve if post is not received |
| 98 |
if( ! isset( $event['post_id'] ) ) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
// Don't deserve if post doesn't match with the trigger option |
| 103 |
if( $trigger_options['post'] !== 'any' && absint( $trigger_options['post'] ) !== absint( $event['post_id'] ) ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
return $deserves_trigger; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Register the required hooks |
| 113 |
* |
| 114 |
* @since 1.0.0 |
| 115 |
*/ |
| 116 |
public function hooks() { |
| 117 |
|
| 118 |
// Log meta data |
| 119 |
add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); |
| 120 |
|
| 121 |
parent::hooks(); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Trigger custom log meta |
| 126 |
* |
| 127 |
* @since 1.0.0 |
| 128 |
* |
| 129 |
* @param array $log_meta Log meta data |
| 130 |
* @param stdClass $trigger The trigger object |
| 131 |
* @param int $user_id The user ID |
| 132 |
* @param array $event Event information |
| 133 |
* @param array $trigger_options The trigger's stored options |
| 134 |
* @param stdClass $automation The trigger's automation object |
| 135 |
* |
| 136 |
* @return array |
| 137 |
*/ |
| 138 |
function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 139 |
|
| 140 |
// Bail if action type don't match this action |
| 141 |
if( $trigger->type !== $this->trigger ) { |
| 142 |
return $log_meta; |
| 143 |
} |
| 144 |
|
| 145 |
$log_meta['order_id'] = ( isset( $event['order_id'] ) ? $event['order_id'] : '' ); |
| 146 |
|
| 147 |
return $log_meta; |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
new AutomatorWP_Eventin_Purchase_Ticket(); |