| 1 |
<?php |
| 2 |
/** |
| 3 |
* List Added |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\FluentCRM\Triggers\List_Added |
| 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_FluentCRM_List_Added extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'fluentcrm'; |
| 15 |
public $trigger = 'fluentcrm_list_added'; |
| 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 gets added to list', 'automatorwp' ), |
| 27 |
'select_option' => __( 'User gets added to <strong>list</strong>', 'automatorwp' ), |
| 28 |
/* translators: %1$s: List. %2$s: Number of times. */ |
| 29 |
'edit_label' => sprintf( __( 'User gets added to %1$s %2$s time(s)', 'automatorwp' ), '{list}', '{times}' ), |
| 30 |
/* translators: %1$s: List. */ |
| 31 |
'log_label' => sprintf( __( 'User gets added to %1$s', 'automatorwp' ), '{list}' ), |
| 32 |
'action' => 'fluentcrm_contact_added_to_lists', |
| 33 |
'function' => array( $this, 'listener' ), |
| 34 |
'priority' => 10, |
| 35 |
'accepted_args' => 2, |
| 36 |
'options' => array( |
| 37 |
'list' => automatorwp_utilities_ajax_selector_option( array( |
| 38 |
'field' => 'list', |
| 39 |
'name' => __( 'List:', 'automatorwp' ), |
| 40 |
'option_none_value' => 'any', |
| 41 |
'option_none_label' => __( 'any list', 'automatorwp' ), |
| 42 |
'action_cb' => 'automatorwp_fluentcrm_get_lists', |
| 43 |
'options_cb' => 'automatorwp_fluentcrm_options_cb_list', |
| 44 |
'default' => 'any' |
| 45 |
) ), |
| 46 |
'times' => automatorwp_utilities_times_option(), |
| 47 |
), |
| 48 |
'tags' => array_merge( |
| 49 |
automatorwp_fluentcrm_contact_tags(), |
| 50 |
automatorwp_utilities_times_tag() |
| 51 |
) |
| 52 |
) ); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Trigger listener |
| 58 |
* |
| 59 |
* @since 1.0.0 |
| 60 |
* |
| 61 |
* @param array $lists_ids |
| 62 |
* @param \FluentCrm\App\Models\Subscriber $subscriber |
| 63 |
*/ |
| 64 |
public function listener( $lists_ids, $subscriber ) { |
| 65 |
|
| 66 |
$user_id = automatorwp_fluentcrm_get_subscriber_user_id( $subscriber ); |
| 67 |
|
| 68 |
// Make sure subscriber has a user ID assigned |
| 69 |
if ( $user_id === 0 ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
foreach( $lists_ids as $list_id ) { |
| 74 |
// Trigger the list added |
| 75 |
automatorwp_trigger_event( array( |
| 76 |
'trigger' => $this->trigger, |
| 77 |
'user_id' => $user_id, |
| 78 |
'list_id' => $list_id, |
| 79 |
'subscriber_email' => $subscriber->email, |
| 80 |
) ); |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* User deserves check |
| 87 |
* |
| 88 |
* @since 1.0.0 |
| 89 |
* |
| 90 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 91 |
* @param stdClass $trigger The trigger object |
| 92 |
* @param int $user_id The user ID |
| 93 |
* @param array $event Event information |
| 94 |
* @param array $trigger_options The trigger's stored options |
| 95 |
* @param stdClass $automation The trigger's automation object |
| 96 |
* |
| 97 |
* @return bool True if user deserves trigger, false otherwise |
| 98 |
*/ |
| 99 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 100 |
|
| 101 |
// Don't deserve if post is not received |
| 102 |
if( ! isset( $event['list_id'] ) ) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
// Don't deserve if post doesn't match with the trigger option |
| 107 |
if( $trigger_options['list'] !== 'any' && absint( $trigger_options['list'] ) !== absint( $event['list_id'] ) ) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
return $deserves_trigger; |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Register the required hooks |
| 117 |
* |
| 118 |
* @since 1.0.0 |
| 119 |
*/ |
| 120 |
public function hooks() { |
| 121 |
|
| 122 |
// Log meta data |
| 123 |
add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); |
| 124 |
|
| 125 |
parent::hooks(); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Trigger custom log meta |
| 130 |
* |
| 131 |
* @since 1.0.0 |
| 132 |
* |
| 133 |
* @param array $log_meta Log meta data |
| 134 |
* @param stdClass $trigger The trigger object |
| 135 |
* @param int $user_id The user ID |
| 136 |
* @param array $event Event information |
| 137 |
* @param array $trigger_options The trigger's stored options |
| 138 |
* @param stdClass $automation The trigger's automation object |
| 139 |
* |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 143 |
|
| 144 |
// Bail if action type don't match this action |
| 145 |
if( $trigger->type !== $this->trigger ) { |
| 146 |
return $log_meta; |
| 147 |
} |
| 148 |
|
| 149 |
$log_meta['subscriber_email'] = ( isset( $event['subscriber_email'] ) ? $event['subscriber_email'] : '' ); |
| 150 |
|
| 151 |
return $log_meta; |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
new AutomatorWP_FluentCRM_List_Added(); |