| 1 |
<?php |
| 2 |
/** |
| 3 |
* User Add Membership |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\ARMember\Triggers\User_Add_Membership |
| 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_ARMember_User_Add_Membership_Trigger extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'armember'; |
| 15 |
public $trigger = 'armember_user_add_membership'; |
| 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 added to a membership plan', 'automatorwp' ), |
| 27 |
'select_option' => __( 'User added to <strong>a membership</strong> plan', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Post title. %2$s: Number of times. */ |
| 29 |
'edit_label' => sprintf( __( 'User added to %1$s %2$s time(s)', 'automatorwp' ), '{plan}', '{times}' ), |
| 30 |
/* translators: %1$s: Post title. */ |
| 31 |
'log_label' => sprintf( __( 'User added to %1$s', 'automatorwp' ), '{plan}' ), |
| 32 |
'action' => array( 'arm_after_user_plan_change_by_admin', 'arm_after_user_plan_change' ), |
| 33 |
'function' => array( $this, 'listener' ), |
| 34 |
'priority' => 10, |
| 35 |
'accepted_args' => 2, |
| 36 |
'options' => array( |
| 37 |
'plan' => automatorwp_utilities_ajax_selector_option( array( |
| 38 |
'field' => 'plan', |
| 39 |
'name' => __( 'Plan:', 'automatorwp' ), |
| 40 |
'option_none_value' => 'any', |
| 41 |
'option_none_label' => __( 'any plan', 'automatorwp' ), |
| 42 |
'action_cb' => 'automatorwp_armember_get_plans', |
| 43 |
'options_cb' => 'automatorwp_armember_options_cb_plan', |
| 44 |
'default' => 'any' |
| 45 |
) ), |
| 46 |
'times' => automatorwp_utilities_times_option(), |
| 47 |
), |
| 48 |
'tags' => array_merge( |
| 49 |
automatorwp_utilities_post_tags(), |
| 50 |
automatorwp_utilities_times_tag() |
| 51 |
) |
| 52 |
) ); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Trigger listener |
| 58 |
* |
| 59 |
* @since 1.0.0 |
| 60 |
* |
| 61 |
* @param int $user_id User ID. |
| 62 |
* @param int $plan_id Membership plan ID. |
| 63 |
*/ |
| 64 |
public function listener( $user_id, $plan_id ) { |
| 65 |
|
| 66 |
// Trigger the cancel membership |
| 67 |
automatorwp_trigger_event( array( |
| 68 |
'trigger' => $this->trigger, |
| 69 |
'user_id' => $user_id, |
| 70 |
'plan_id' => $plan_id, |
| 71 |
) ); |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* User deserves check |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
* |
| 80 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 81 |
* @param stdClass $trigger The trigger object |
| 82 |
* @param int $user_id The user ID |
| 83 |
* @param array $event Event information |
| 84 |
* @param array $trigger_options The trigger's stored options |
| 85 |
* @param stdClass $automation The trigger's automation object |
| 86 |
* |
| 87 |
* @return bool True if user deserves trigger, false otherwise |
| 88 |
*/ |
| 89 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 90 |
|
| 91 |
// Don't deserve if plan is not received |
| 92 |
if( ! isset( $event['plan_id'] ) ) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
// Don't deserve if plan doesn't match with the trigger option |
| 97 |
if( $trigger_options['plan'] !== 'any' && absint( $event['plan_id'] ) !== absint( $trigger_options['plan'] ) ) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
return $deserves_trigger; |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
new AutomatorWP_ARMember_User_Add_Membership_Trigger(); |