| 1 |
<?php |
| 2 |
/** |
| 3 |
* Send Email |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\WordPress\Actions\Send_Email |
| 6 |
* @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class AutomatorWP_WordPress_Send_Email extends AutomatorWP_Integration_Action { |
| 13 |
|
| 14 |
/** |
| 15 |
* Initialize the trigger |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
public function __construct( $integration ) { |
| 20 |
|
| 21 |
$this->integration = $integration; |
| 22 |
$this->action = $integration . '_send_email'; |
| 23 |
|
| 24 |
parent::__construct(); |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Store the action result |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
* |
| 33 |
* @var bool $result |
| 34 |
*/ |
| 35 |
public $result = false; |
| 36 |
|
| 37 |
/** |
| 38 |
* Register required hooks |
| 39 |
* |
| 40 |
* @since 1.0.0 |
| 41 |
*/ |
| 42 |
public function hooks() { |
| 43 |
|
| 44 |
// Log meta data |
| 45 |
add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); |
| 46 |
|
| 47 |
// Log fields |
| 48 |
add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); |
| 49 |
|
| 50 |
parent::hooks(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Register the trigger |
| 55 |
* |
| 56 |
* @since 1.0.0 |
| 57 |
*/ |
| 58 |
public function register() { |
| 59 |
|
| 60 |
automatorwp_register_action( $this->action, array( |
| 61 |
'integration' => $this->integration, |
| 62 |
'label' => __( 'Send an email', 'automatorwp' ), |
| 63 |
'select_option' => __( 'Send <strong>an email</strong>', 'automatorwp' ), |
| 64 |
/* translators: %1$s: Email. */ |
| 65 |
'edit_label' => sprintf( __( 'Send an email to %1$s', 'automatorwp' ), '{email}' ), |
| 66 |
/* translators: %1$s: Email. */ |
| 67 |
'log_label' => sprintf( __( 'Send an email to %1$s', 'automatorwp' ), '{email}' ), |
| 68 |
'options' => array( |
| 69 |
'email' => array( |
| 70 |
'from' => 'to', |
| 71 |
'default' => __( 'user', 'automatorwp' ), |
| 72 |
'fields' => array( |
| 73 |
'from' => array( |
| 74 |
'name' => __( 'From:', 'automatorwp' ), |
| 75 |
'desc' => __( 'Leave empty to use default WordPress email settings.', 'automatorwp' ), |
| 76 |
'type' => 'text', |
| 77 |
'attributes' => array( |
| 78 |
'placeholder' => __( 'sample@email.com', 'automatorwp' ), |
| 79 |
), |
| 80 |
'classes' => 'automatorwp-col-6', |
| 81 |
'default' => '', |
| 82 |
), |
| 83 |
'from_name' => array( |
| 84 |
'name' => __( 'From Name:', 'automatorwp' ), |
| 85 |
'desc' => __( 'Leave empty to use default WordPress email settings.', 'automatorwp' ), |
| 86 |
'type' => 'text', |
| 87 |
'attributes' => array( |
| 88 |
'placeholder' => __( 'John Doe', 'automatorwp' ), |
| 89 |
), |
| 90 |
'classes' => 'automatorwp-col-6', |
| 91 |
'default' => '', |
| 92 |
), |
| 93 |
'to' => array( |
| 94 |
'name' => __( 'To:', 'automatorwp' ), |
| 95 |
'desc' => __( 'Email address(es) to send the email. Accepts single or comma-separated list of emails.', 'automatorwp' ) |
| 96 |
. '<br>' . __( 'Leave empty to use the email of the user that completes the automation.', 'automatorwp' ), |
| 97 |
'type' => 'text', |
| 98 |
'attributes' => array( |
| 99 |
'placeholder' => __( 'sample@email.com', 'automatorwp' ), |
| 100 |
), |
| 101 |
'default' => '' |
| 102 |
), |
| 103 |
'cc' => array( |
| 104 |
'name' => __( 'CC:', 'automatorwp' ), |
| 105 |
'desc' => __( 'Email address(es) that will receive a copy of this email. Accepts single or comma-separated list of emails.', 'automatorwp' ), |
| 106 |
'type' => 'text', |
| 107 |
'attributes' => array( |
| 108 |
'placeholder' => __( 'email1@email.com, email2@email.com, email3@email.com, ...', 'automatorwp' ), |
| 109 |
), |
| 110 |
'default' => '' |
| 111 |
), |
| 112 |
'bcc' => array( |
| 113 |
'name' => __( 'BCC:', 'automatorwp' ), |
| 114 |
'desc' => __( 'Email address(es) that will receive a copy of this email. Accepts single or comma-separated list of emails.', 'automatorwp' ), |
| 115 |
'type' => 'text', |
| 116 |
'attributes' => array( |
| 117 |
'placeholder' => __( 'email1@email.com, email2@email.com, email3@email.com, ...', 'automatorwp' ), |
| 118 |
), |
| 119 |
'default' => '' |
| 120 |
), |
| 121 |
'subject' => array( |
| 122 |
'name' => __( 'Subject:', 'automatorwp' ), |
| 123 |
'desc' => __( 'Email\'s subject.', 'automatorwp' ), |
| 124 |
'type' => 'text', |
| 125 |
'required' => true, |
| 126 |
'default' => '' |
| 127 |
), |
| 128 |
'content' => array( |
| 129 |
'name' => __( 'Content:', 'automatorwp' ), |
| 130 |
'desc' => __( 'Email\'s content.', 'automatorwp' ), |
| 131 |
'type' => 'wysiwyg', |
| 132 |
'required' => true, |
| 133 |
'default' => '' |
| 134 |
), |
| 135 |
) |
| 136 |
) |
| 137 |
), |
| 138 |
) ); |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Action execution function |
| 144 |
* |
| 145 |
* @since 1.0.0 |
| 146 |
* |
| 147 |
* @param stdClass $action The action object |
| 148 |
* @param int $user_id The user ID |
| 149 |
* @param array $action_options The action's stored options (with tags already passed) |
| 150 |
* @param stdClass $automation The action's automation object |
| 151 |
*/ |
| 152 |
public function execute( $action, $user_id, $action_options, $automation ) { |
| 153 |
|
| 154 |
// Shorthand |
| 155 |
$to = $action_options['to']; |
| 156 |
|
| 157 |
// Setup to |
| 158 |
if( empty( $to ) ) { |
| 159 |
$user = get_userdata( $user_id ); |
| 160 |
$to = $user->user_email; |
| 161 |
} |
| 162 |
|
| 163 |
// Send the email |
| 164 |
$this->result = automatorwp_send_email( array( |
| 165 |
// Email parameters |
| 166 |
'from' => $action_options['from'], |
| 167 |
'from_name' => ( isset( $action_options['from_name'] ) ? $action_options['from_name'] : '' ), |
| 168 |
'to' => $to, |
| 169 |
'cc' => $action_options['cc'], |
| 170 |
'bcc' => $action_options['bcc'], |
| 171 |
'subject' => $action_options['subject'], |
| 172 |
'message' => $action_options['content'], |
| 173 |
// Custom parameters |
| 174 |
'action' => $action, |
| 175 |
'user_id' => $user_id, |
| 176 |
'action_options' => $action_options, |
| 177 |
'automation' => $automation |
| 178 |
) ); |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Action custom log meta |
| 184 |
* |
| 185 |
* @since 1.0.0 |
| 186 |
* |
| 187 |
* @param array $log_meta Log meta data |
| 188 |
* @param stdClass $action The action object |
| 189 |
* @param int $user_id The user ID |
| 190 |
* @param array $action_options The action's stored options (with tags already passed) |
| 191 |
* @param stdClass $automation The action's automation object |
| 192 |
* |
| 193 |
* @return array |
| 194 |
*/ |
| 195 |
public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { |
| 196 |
|
| 197 |
// Bail if action type don't match this action |
| 198 |
if( $action->type !== $this->action ) { |
| 199 |
return $log_meta; |
| 200 |
} |
| 201 |
|
| 202 |
$log_meta['from'] = $action_options['from']; |
| 203 |
$log_meta['to'] = $action_options['to']; |
| 204 |
$log_meta['cc'] = $action_options['cc']; |
| 205 |
$log_meta['bcc'] = $action_options['bcc']; |
| 206 |
$log_meta['subject'] = $action_options['subject']; |
| 207 |
$log_meta['content'] = $action_options['content']; |
| 208 |
$log_meta['result'] = ( $this->result ? __( 'Sent', 'automatorwp' ) : __( 'Not sent', 'automatorwp' ) ); |
| 209 |
|
| 210 |
return $log_meta; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Action custom log fields |
| 215 |
* |
| 216 |
* @since 1.0.0 |
| 217 |
* |
| 218 |
* @param array $log_fields The log fields |
| 219 |
* @param stdClass $log The log object |
| 220 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 221 |
* |
| 222 |
* @return array |
| 223 |
*/ |
| 224 |
public function log_fields( $log_fields, $log, $object ) { |
| 225 |
|
| 226 |
// Bail if log is not assigned to an action |
| 227 |
if( $log->type !== 'action' || $object->type !== $this->action ) |
| 228 |
return $log_fields; |
| 229 |
|
| 230 |
$log_fields['email_info'] = array( |
| 231 |
'name' => __( 'Sending Information', 'automatorwp' ), |
| 232 |
'desc' => __( 'Information about email sent.', 'automatorwp' ), |
| 233 |
'type' => 'title', |
| 234 |
); |
| 235 |
|
| 236 |
$log_fields['from'] = array( |
| 237 |
'name' => __( 'From:', 'automatorwp' ), |
| 238 |
'desc' => __( 'Email sender.', 'automatorwp' ), |
| 239 |
'type' => 'text', |
| 240 |
); |
| 241 |
|
| 242 |
$log_fields['to'] = array( |
| 243 |
'name' => __( 'To:', 'automatorwp' ), |
| 244 |
'desc' => __( 'Email recipient.', 'automatorwp' ), |
| 245 |
'type' => 'text', |
| 246 |
); |
| 247 |
|
| 248 |
$log_fields['cc'] = array( |
| 249 |
'name' => __( 'CC:', 'automatorwp' ), |
| 250 |
'desc' => __( 'Carbon copy emails.', 'automatorwp' ), |
| 251 |
'type' => 'text', |
| 252 |
); |
| 253 |
|
| 254 |
$log_fields['bcc'] = array( |
| 255 |
'name' => __( 'BCC:', 'automatorwp' ), |
| 256 |
'desc' => __( 'Blind carbon copy emails.', 'automatorwp' ), |
| 257 |
'type' => 'text', |
| 258 |
); |
| 259 |
|
| 260 |
$log_fields['subject'] = array( |
| 261 |
'name' => __( 'Subject:', 'automatorwp' ), |
| 262 |
'desc' => __( 'Email\'s subject.', 'automatorwp' ), |
| 263 |
'type' => 'text', |
| 264 |
); |
| 265 |
|
| 266 |
$log_fields['content'] = array( |
| 267 |
'name' => __( 'Content:', 'automatorwp' ), |
| 268 |
'desc' => __( 'Email\'s content.', 'automatorwp' ), |
| 269 |
'type' => 'text', |
| 270 |
'wpautop' => true, |
| 271 |
); |
| 272 |
|
| 273 |
$log_fields['result'] = array( |
| 274 |
'name' => __( 'Sending result:', 'automatorwp' ), |
| 275 |
'desc' => __( 'If sending result is "Not sent" you need to check if your server\'s wp_mail() function is correctly configured.', 'automatorwp' ), |
| 276 |
'type' => 'text', |
| 277 |
); |
| 278 |
|
| 279 |
return $log_fields; |
| 280 |
} |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
new AutomatorWP_WordPress_Send_Email( 'wordpress' ); |
| 285 |
new AutomatorWP_WordPress_Send_Email( 'emails' ); |