| 1 |
<?php |
| 2 |
/** |
| 3 |
* New Customer |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\BookingPress\Triggers\New_Customer |
| 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_BookingPress_New_Customer extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'bookingpress'; |
| 15 |
public $trigger = 'bookingpress_new_customer'; |
| 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 is registered as a customer', 'automatorwp' ), |
| 27 |
'select_option' => __( 'User is registered as a <strong>customer</strong>', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Number of times. */ |
| 29 |
'edit_label' => sprintf( __( 'User is registered as a customer %1$s time(s)', 'automatorwp' ), '{times}' ), |
| 30 |
'log_label' => __( 'User is registered as a customer', 'automatorwp' ), |
| 31 |
'action' => 'bookingpress_after_create_customer', |
| 32 |
'function' => array( $this, 'listener' ), |
| 33 |
'priority' => 10, |
| 34 |
'accepted_args' => 1, |
| 35 |
'options' => array( |
| 36 |
'times' => automatorwp_utilities_times_option(), |
| 37 |
), |
| 38 |
'tags' => array_merge( |
| 39 |
automatorwp_bookingpress_get_customer_tags(), |
| 40 |
automatorwp_utilities_times_tag() |
| 41 |
) |
| 42 |
) ); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Trigger listener |
| 48 |
* |
| 49 |
* @since 1.0.0 |
| 50 |
* |
| 51 |
* @param int $customer_id BookingPress customer ID |
| 52 |
*/ |
| 53 |
public function listener( $customer_id ) { |
| 54 |
|
| 55 |
$user_id = automatorwp_bookingpress_get_wp_user_id_from_customer_id( $customer_id ); |
| 56 |
|
| 57 |
if( empty( $user_id ) ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
automatorwp_trigger_event( array( |
| 62 |
'trigger' => $this->trigger, |
| 63 |
'user_id' => $user_id, |
| 64 |
'customer_id' => $customer_id, |
| 65 |
) ); |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Register the required hooks |
| 71 |
* |
| 72 |
* @since 1.0.0 |
| 73 |
*/ |
| 74 |
public function hooks() { |
| 75 |
|
| 76 |
// Log meta data |
| 77 |
add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); |
| 78 |
|
| 79 |
parent::hooks(); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Trigger custom log meta |
| 84 |
* |
| 85 |
* @since 1.0.0 |
| 86 |
* |
| 87 |
* @param array $log_meta Log meta data |
| 88 |
* @param stdClass $trigger The trigger object |
| 89 |
* @param int $user_id The user ID |
| 90 |
* @param array $event Event information |
| 91 |
* @param array $trigger_options The trigger's stored options |
| 92 |
* @param stdClass $automation The trigger's automation object |
| 93 |
* |
| 94 |
* @return array |
| 95 |
*/ |
| 96 |
function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 97 |
|
| 98 |
// Bail if action type don't match this action |
| 99 |
if( $trigger->type !== $this->trigger ) { |
| 100 |
return $log_meta; |
| 101 |
} |
| 102 |
|
| 103 |
$log_meta['customer_id'] = ( isset( $event['customer_id'] ) ? $event['customer_id'] : '' ); |
| 104 |
|
| 105 |
return $log_meta; |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
new AutomatorWP_BookingPress_New_Customer(); |
| 112 |
|