| 1 |
<?php |
| 2 |
/** |
| 3 |
* User List |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\FluentCRM\Actions\User_List |
| 6 |
* @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
|
| 11 |
if( !defined( 'ABSPATH' ) ) exit; |
| 12 |
|
| 13 |
class AutomatorWP_FluentCRM_User_List extends AutomatorWP_Integration_Action { |
| 14 |
|
| 15 |
public $integration = 'fluentcrm'; |
| 16 |
public $action = 'fluentcrm_user_list'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Register the trigger |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
public function register() { |
| 24 |
|
| 25 |
automatorwp_register_action( $this->action, array( |
| 26 |
'integration' => $this->integration, |
| 27 |
'label' => __( 'Add user to list', 'automatorwp' ), |
| 28 |
'select_option' => __( 'Add user to <strong>list</strong>', 'automatorwp' ), |
| 29 |
/* translators: %1$s: Tag. */ |
| 30 |
'edit_label' => sprintf( __( 'Add user to %1$s', 'automatorwp' ), '{list}' ), |
| 31 |
/* translators: %1$s: Tag. */ |
| 32 |
'log_label' => sprintf( __( 'Add user to %1$s', 'automatorwp' ), '{list}' ), |
| 33 |
'options' => array( |
| 34 |
'list' => automatorwp_utilities_ajax_selector_option( array( |
| 35 |
'field' => 'list', |
| 36 |
'option_default' => __( 'Select a list', 'automatorwp' ), |
| 37 |
'name' => __( 'List:', 'automatorwp' ), |
| 38 |
'option_custom' => true, |
| 39 |
'option_custom_desc' => __( 'List ID', 'automatorwp' ), |
| 40 |
'action_cb' => 'automatorwp_fluentcrm_get_lists', |
| 41 |
'options_cb' => 'automatorwp_fluentcrm_options_cb_list', |
| 42 |
'default' => '' |
| 43 |
) ), |
| 44 |
), |
| 45 |
) ); |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Action execution function |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
* |
| 54 |
* @param stdClass $action The action object |
| 55 |
* @param int $user_id The user ID |
| 56 |
* @param array $action_options The action's stored options (with tags already passed) |
| 57 |
* @param stdClass $automation The action's automation object |
| 58 |
*/ |
| 59 |
public function execute( $action, $user_id, $action_options, $automation ) { |
| 60 |
|
| 61 |
global $wpdb; |
| 62 |
|
| 63 |
// Shorthand |
| 64 |
$list_id = $action_options['list']; |
| 65 |
$this->result = ''; |
| 66 |
|
| 67 |
// Bail if empty list to assign |
| 68 |
if( empty( $list_id ) ) { |
| 69 |
$this->result = __( 'List field cannot be empty.', 'automatorwp' ); |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
$list = $wpdb->get_row( $wpdb->prepare( |
| 74 |
"SELECT * FROM {$wpdb->prefix}fc_lists WHERE id = %s", |
| 75 |
$list_id |
| 76 |
) ); |
| 77 |
|
| 78 |
// Bail if list not exists |
| 79 |
if( ! $list ) { |
| 80 |
$this->result = sprintf( __( 'List with %s ID cannot be found.', 'automatorwp' ), $list_id ); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$subscriber = automatorwp_fluentcrm_get_subscriber( $user_id ); |
| 85 |
|
| 86 |
// Bail if subscriber not exists |
| 87 |
if( ! $subscriber ) { |
| 88 |
$this->result = __( 'User is not a contact.', 'automatorwp' ); |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
// Add user to list |
| 93 |
$subscriber->attachLists( array( $list_id ) ); |
| 94 |
|
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Register required hooks |
| 100 |
* |
| 101 |
* @since 1.0.0 |
| 102 |
*/ |
| 103 |
public function hooks() { |
| 104 |
|
| 105 |
// Log meta data |
| 106 |
add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); |
| 107 |
|
| 108 |
// Log fields |
| 109 |
add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); |
| 110 |
|
| 111 |
parent::hooks(); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Action custom log meta |
| 117 |
* |
| 118 |
* @since 1.0.0 |
| 119 |
* |
| 120 |
* @param array $log_meta Log meta data |
| 121 |
* @param stdClass $action The action object |
| 122 |
* @param int $user_id The user ID |
| 123 |
* @param array $action_options The action's stored options (with tags already passed) |
| 124 |
* @param stdClass $automation The action's automation object |
| 125 |
* |
| 126 |
* @return array |
| 127 |
*/ |
| 128 |
public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { |
| 129 |
|
| 130 |
// Bail if action type don't match this action |
| 131 |
if( $action->type !== $this->action ) { |
| 132 |
return $log_meta; |
| 133 |
} |
| 134 |
|
| 135 |
// Store the action's result |
| 136 |
$log_meta['result'] = $this->result; |
| 137 |
|
| 138 |
return $log_meta; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Action custom log fields |
| 143 |
* |
| 144 |
* @since 1.0.0 |
| 145 |
* |
| 146 |
* @param array $log_fields The log fields |
| 147 |
* @param stdClass $log The log object |
| 148 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 149 |
* |
| 150 |
* @return array |
| 151 |
*/ |
| 152 |
public function log_fields( $log_fields, $log, $object ) { |
| 153 |
|
| 154 |
// Bail if log is not assigned to an action |
| 155 |
if( $log->type !== 'action' ) { |
| 156 |
return $log_fields; |
| 157 |
} |
| 158 |
|
| 159 |
// Bail if action type don't match this action |
| 160 |
if( $object->type !== $this->action ) { |
| 161 |
return $log_fields; |
| 162 |
} |
| 163 |
|
| 164 |
$log_fields['result'] = array( |
| 165 |
'name' => __( 'Result:', 'automatorwp-fluentcrm' ), |
| 166 |
'type' => 'text', |
| 167 |
); |
| 168 |
|
| 169 |
return $log_fields; |
| 170 |
} |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
new AutomatorWP_FluentCRM_User_List(); |