← All changes
|
integrations/autonami/includes/triggers/user-added-list.php
+177
-0
5.7.4
→
6.0.4
View file →
| @@ -1,0 +1,177 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * User Added List | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\Autonami\Triggers\User_Added_List | |
| 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_Autonami_User_Added_List extends AutomatorWP_Integration_Trigger { | |
| 13 | + | |
| 14 | + public $integration = 'autonami'; | |
| 15 | + public $trigger = 'autonami_user_added_list'; | |
| 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 list', 'automatorwp' ), | |
| 27 | + 'select_option' => __( 'User added to <strong>list</strong>', 'automatorwp' ), | |
| 28 | + /* translators: %1$s: List. %2$s: Number of times. */ | |
| 29 | + 'edit_label' => sprintf( __( 'User added to %1$s %2$s time(s)', 'automatorwp' ), '{list}', '{times}' ), | |
| 30 | + /* translators: %1$s: List. */ | |
| 31 | + 'log_label' => sprintf( __( 'User added to %1$s', 'automatorwp' ), '{list}' ), | |
| 32 | + 'action' => 'bwfan_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_autonami_get_lists', | |
| 43 | + 'options_cb' => 'automatorwp_autonami_options_cb_list', | |
| 44 | + 'default' => 'any' | |
| 45 | + ) ), | |
| 46 | + 'times' => automatorwp_utilities_times_option(), | |
| 47 | + ), | |
| 48 | + 'tags' => array_merge( | |
| 49 | + automatorwp_autonami_list_tags(), | |
| 50 | + automatorwp_autonami_email_tags(), | |
| 51 | + automatorwp_utilities_times_tag() | |
| 52 | + ) | |
| 53 | + ) ); | |
| 54 | + | |
| 55 | + } | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * Trigger listener | |
| 59 | + * | |
| 60 | + * @since 1.0.0 | |
| 61 | + * | |
| 62 | + * @param BWFCRM_Lists $lists | |
| 63 | + * @param BWFCRM_Contact $contact | |
| 64 | + */ | |
| 65 | + public function listener( $lists, $contact ) { | |
| 66 | + | |
| 67 | + // Get contact email | |
| 68 | + $email = $contact->contact->get_email(); | |
| 69 | + $user = get_user_by( 'email', $email ); | |
| 70 | + | |
| 71 | + // Make sure contact has an user ID assigned | |
| 72 | + if ( $user->ID === 0 ) { | |
| 73 | + return; | |
| 74 | + } | |
| 75 | + | |
| 76 | + if ( !is_array ( $lists ) ){ | |
| 77 | + | |
| 78 | + // Trigger the user added to list | |
| 79 | + automatorwp_trigger_event( array( | |
| 80 | + 'trigger' => $this->trigger, | |
| 81 | + 'user_id' => $user->ID, | |
| 82 | + 'list_id' => $lists->get_id(), | |
| 83 | + 'contact_email' => $email, | |
| 84 | + ) ); | |
| 85 | + | |
| 86 | + } else { | |
| 87 | + | |
| 88 | + foreach ( $lists as $list ){ | |
| 89 | + | |
| 90 | + // Trigger the user added to list | |
| 91 | + automatorwp_trigger_event( array( | |
| 92 | + 'trigger' => $this->trigger, | |
| 93 | + 'user_id' => $user->ID, | |
| 94 | + 'list_id' => $list->get_id(), | |
| 95 | + 'contact_email' => $email, | |
| 96 | + ) ); | |
| 97 | + | |
| 98 | + } | |
| 99 | + | |
| 100 | + } | |
| 101 | + | |
| 102 | + } | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * User deserves check | |
| 106 | + * | |
| 107 | + * @since 1.0.0 | |
| 108 | + * | |
| 109 | + * @param bool $deserves_trigger True if user deserves trigger, false otherwise | |
| 110 | + * @param stdClass $trigger The trigger object | |
| 111 | + * @param int $user_id The user ID | |
| 112 | + * @param array $event Event information | |
| 113 | + * @param array $trigger_options The trigger's stored options | |
| 114 | + * @param stdClass $automation The trigger's automation object | |
| 115 | + * | |
| 116 | + * @return bool True if user deserves trigger, false otherwise | |
| 117 | + */ | |
| 118 | + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 119 | + | |
| 120 | + // Don't deserve if list is not received | |
| 121 | + if( ! isset( $event['list_id'] ) ) { | |
| 122 | + return false; | |
| 123 | + } | |
| 124 | + | |
| 125 | + // Don't deserve if list doesn't match with the trigger option | |
| 126 | + if( $trigger_options['list'] !== 'any' && absint( $trigger_options['list'] ) !== absint( $event['list_id'] ) ) { | |
| 127 | + return false; | |
| 128 | + } | |
| 129 | + | |
| 130 | + return $deserves_trigger; | |
| 131 | + | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * Register the required hooks | |
| 136 | + * | |
| 137 | + * @since 1.0.0 | |
| 138 | + */ | |
| 139 | + public function hooks() { | |
| 140 | + | |
| 141 | + // Log meta data | |
| 142 | + add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); | |
| 143 | + | |
| 144 | + parent::hooks(); | |
| 145 | + } | |
| 146 | + | |
| 147 | + /** | |
| 148 | + * Trigger custom log meta | |
| 149 | + * | |
| 150 | + * @since 1.0.0 | |
| 151 | + * | |
| 152 | + * @param array $log_meta Log meta data | |
| 153 | + * @param stdClass $trigger The trigger object | |
| 154 | + * @param int $user_id The user ID | |
| 155 | + * @param array $event Event information | |
| 156 | + * @param array $trigger_options The trigger's stored options | |
| 157 | + * @param stdClass $automation The trigger's automation object | |
| 158 | + * | |
| 159 | + * @return array | |
| 160 | + */ | |
| 161 | + function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 162 | + | |
| 163 | + // Bail if action type don't match this action | |
| 164 | + if( $trigger->type !== $this->trigger ) { | |
| 165 | + return $log_meta; | |
| 166 | + } | |
| 167 | + | |
| 168 | + $log_meta['contact_email'] = ( isset( $event['contact_email'] ) ? $event['contact_email'] : '' ); | |
| 169 | + $log_meta['list_id'] = ( isset( $event['list_id'] ) ? $event['list_id'] : '' ); | |
| 170 | + | |
| 171 | + return $log_meta; | |
| 172 | + | |
| 173 | + } | |
| 174 | + | |
| 175 | +} | |
| 176 | + | |
| 177 | +new AutomatorWP_Autonami_User_Added_List(); | |