WP_SENDEMAIL

Class WP_SENDEMAIL


Source Source

File: src/integrations/wp/actions/wp-sendemail.php

class WP_SENDEMAIL {
	use Recipe\Actions;
	/**
	 * @var false
	 */
	private $key_generated;
	/**
	 * @var null
	 */
	private $key;
	/**
	 * WP_SENDEMAIL constructor.
	 *
	 * @return void.
	 */
	public function __construct() {
		$this->key_generated = false;
		$this->key           = null;
		$this->setup_action();
	}
	/**
	 * Setup SENDEMAIL Automator Action.
	 *
	 * @return void.
	 */
	protected function setup_action() {
		$this->set_integration( 'WP' );
		$this->set_action_code( 'SENDEMAIL' );
		$this->set_action_meta( 'EMAILTO' );
		/* translators: Action - WordPress */
		$this->set_sentence( sprintf( esc_attr__( 'Send an email to {{email address:%1$s}}', 'uncanny-automator' ), $this->get_action_meta() ) );
		/* translators: Action - WordPress */
		$this->set_readable_sentence( esc_attr__( 'Send an {{email}}', 'uncanny-automator' ) );
		$options_group = array(
			$this->get_action_meta() => array(
				// Email From Field.
				Automator()->helpers->recipe->field->text(
					array(
						'option_code' => 'EMAILFROM',
						/* translators: Email field */
						'label'       => esc_attr__( 'From', 'uncanny-automator' ),
						'input_type'  => 'email',
						'default'     => '{{admin_email}}',
					)
				),
				// Email From Field.
				Automator()->helpers->recipe->field->text(
					array(
						'option_code' => 'EMAILFROMNAME',
						/* translators: Email field */
						'label'       => esc_attr__( 'From name', 'uncanny-automator' ),
						'input_type'  => 'text',
						'default'     => '{{site_name}}',
					)
				),
				// Email To Field.
				Automator()->helpers->recipe->field->text(
					array(
						'option_code' => 'EMAILTO',
						/* translators: Email field */
						'label'       => esc_attr__( 'To', 'uncanny-automator' ),
						'input_type'  => 'email',
						'default'     => '{{user_email}}',
					)
				),
				// Email CC field.
				Automator()->helpers->recipe->field->text(
					array(
						'option_code' => 'EMAILCC',
						/* translators: Email field */
						'label'       => esc_attr__( 'CC', 'uncanny-automator' ),
						'input_type'  => 'email',
						'required'    => false,
					)
				),
				// Email BCC field.
				Automator()->helpers->recipe->field->text(
					array(
						'option_code' => 'EMAILBCC',
						/* translators: Email field */
						'label'       => esc_attr__( 'BCC', 'uncanny-automator' ),
						'input_type'  => 'email',
						'required'    => false,
					)
				),
				// Email Subject field.
				Automator()->helpers->recipe->field->text(
					array(
						'option_code' => 'EMAILSUBJECT',
						/* translators: Email field */
						'label'       => esc_attr__( 'Subject', 'uncanny-automator' ),
						'required'    => true,
					)
				),
				// Email Content Field.
				Automator()->helpers->recipe->field->text(
					array(
						'option_code' => 'EMAILBODY',
						/* translators: Email field */
						'label'       => esc_attr__( 'Body', 'uncanny-automator' ),
						'input_type'  => 'textarea',
					)
				),
			),
		);
		$this->set_options_group( $options_group );
		$this->register_action();
	}

	/**
	 * @param int $user_id
	 * @param array $action_data
	 * @param int $recipe_id
	 * @param array $args
	 * @param $parsed
	 *
	 * @return void.
	 */
	protected function process_action( int $user_id, array $action_data, int $recipe_id, array $args, $parsed ) {
		$body_text = $parsed['EMAILBODY'] ?? '';
		if ( false !== strpos( $body_text, '{{reset_pass_link}}' ) ) {
			$reset_pass = ! is_null( $this->key ) ? $this->key : Automator()->parse->generate_reset_token( $user_id );
			$body       = str_replace( '{{reset_pass_link}}', $reset_pass, $body_text );
		} else {
			$body = $body_text;
		}
		$data = array(
			'to'        => $parsed['EMAILTO'] ?? '',
			'from'      => $parsed['EMAILFROM'] ?? '',
			'from_name' => $parsed['EMAILFROMNAME'] ?? '',
			'cc'        => $parsed['EMAILCC'] ?? '',
			'bcc'       => $parsed['EMAILBCC'] ?? '',
			'subject'   => $parsed['EMAILSUBJECT'] ?? '',
			'body'      => $body,
			'content'   => $this->get_content_type(),
			'charset'   => $this->get_charset(),
		);
		$this->set_mail_values( $data );
		$mailed = $this->send_email();
		// Set $this->set_error_message(); and complete the action automatically. May be use return true / false.
		if ( false === $mailed && ! empty( $this->get_error_message() ) ) {
			$error_message                       = $this->get_error_message();
			$action_data['complete_with_errors'] = true;
			Automator()->complete->action( $user_id, $action_data, $recipe_id, $error_message );
		}
		Automator()->complete->action( $user_id, $action_data, $recipe_id );
	}
}

Methods Methods