Skip to content

Commit

Permalink
fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
moldcraft committed Sep 1, 2016
1 parent 03abc7b commit 8a6c808
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 21 deletions.
22 changes: 16 additions & 6 deletions class-fw-extension-mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,21 @@ public function _action_extension_settings_form_render()
);
}

public function send($to, $subject, $message, $data = array())
/**
* @param string $to
* @param string $subject
* @param string $message
* @param array $data
* @param array $settings Use this settings instead of db settings | Since 1.2.7
* @return array {status: 0, message: '...'}
*/
public function send($to, $subject, $message, $data = array(), $settings = array())
{
$send_method = $this->get_send_method(
$this->get_db_settings_option('method')
);
if (empty($settings)) {
$settings = $this->get_db_settings_option();
}

$send_method = $this->get_send_method($settings['method']);

if (!$send_method) {
return array(
Expand All @@ -51,7 +61,7 @@ public function send($to, $subject, $message, $data = array())

if (is_wp_error(
$send_method_configuration = $send_method->prepare_settings_options_values(
$this->get_db_settings_option($send_method->get_id())
fw_akg($send_method->get_id(), $settings)
)
)) {
return array(
Expand All @@ -71,7 +81,7 @@ public function send($to, $subject, $message, $data = array())

$result = $send_method->send(
$email,
$this->get_db_settings_option($send_method->get_id()),
fw_akg($send_method->get_id(), $settings),
$data
);

Expand Down
39 changes: 37 additions & 2 deletions includes/option-type-mailer/class-fw-option-type-mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class FW_Option_Type_Mailer extends FW_Option_Type {
/**
* @internal
*/
public function _init() {}
public function _init() {
add_action('wp_ajax_fw_ext_mailer_test_connection', array($this, '_action_ajax_test_connection'));
}

public function get_type() {
return 'mailer';
Expand Down Expand Up @@ -101,13 +103,16 @@ private function get_inner_options() {
)
),
),
'text-connection-container' => array(
'test-connection' => array(
'label' => false,
'desc' => false,
'type' => 'multi',
'inner-options' => array(
'test-connection' => array(
'type' => 'html-fixed',
'attr' => array(
'class' => 'test-connection-wrapper'
),
'html' =>
'<div class="test-connection">'.
/**/'<div>'.
Expand Down Expand Up @@ -175,6 +180,36 @@ protected function _get_value_from_input( $option, $input_value ) {
? fw_get_options_values_from_input($this->get_inner_options(), $input_value)
: $option['value'];
}

/**
* @internal
*/
public function _action_ajax_test_connection() {
if (!current_user_can('manage_options')) {
return wp_send_json_error(new WP_Error('forbidden', __('Forbidden', 'fw')));
} elseif (!is_email($to = FW_Request::POST('to'))) {
return wp_send_json_error(new WP_Error('forbidden', __('Invalid email', 'fw')));
} elseif (!is_array($settings = FW_Request::POST('settings'))) {
return wp_send_json_error(new WP_Error('forbidden', __('Invalid settings', 'fw')));
}

/** @var FW_Extension_Mailer $ext */
$ext = fw_ext('mailer');

$result = $ext->send(
$to,
__('Test Subject', 'fw'),
'<strong>'. __('Test Message', 'fw') .'</strong>',
array(),
fw_get_options_values_from_input($this->get_inner_options(), $settings)
);

if ($result['status']) {
wp_send_json_success();
} else {
wp_send_json_error(new WP_Error('fail', $result['message']));
}
}
}

FW_Option_Type::register( 'FW_Option_Type_Mailer' );
60 changes: 59 additions & 1 deletion includes/option-type-mailer/static/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,65 @@
.trigger('change');

$option.on('click', '.test-connection button', function(){
console.log('OK');
var $to = $option.find('.test-connection input[type="email"]:first'),
to = $.trim($to.val());

if (!to.length) {
return $to.focus();
}

var $button = $(this).attr('disabled', 'disabled');

{ // <input name="{prefix}...">
var namePrefix = $option.find('.test-connection-wrapper > input:first').attr('name');

namePrefix = namePrefix.split('][');
namePrefix.pop();
namePrefix.pop();
namePrefix = namePrefix.join('][');
namePrefix += ']';
}

var vars = [
{name: 'action', value: 'fw_ext_mailer_test_connection'},
{name: 'to', value: to}
];

$.each($option.find('[name^="'+ namePrefix +'"]').serializeArray(), function(i, v) {
v.name = v.name.split(namePrefix);
v.name.shift();
v.name = v.name.join(namePrefix);
v.name = 'settings'+ v.name;

vars.push(v);
});

$.ajax({
url: ajaxurl,
data: vars,
method: 'post',
dataType: 'json'
}).done(function (r) {
if (r.success) {
fw.soleModal.show(
'fw-option-mailer',
'<span style="font-size: 7em;">&#10004;</span>',
{}
);
} else {
try {
alert(r.data[0].message);
} catch (e) {
alert('Request failed');
}
}
}).fail(function (jqXHR, textStatus, errorThrown) {
alert('AJAX error: '+ String(errorThrown));
}).always(function () {
setTimeout(function () { // prevent user to click too often
$button.removeAttr('disabled');
}, 3000);
});
});
})
.addClass('initialized');
Expand Down
28 changes: 16 additions & 12 deletions includes/send-methods/class-fw-ext-mailer-send-method-smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,23 @@ public function send(FW_Ext_Mailer_Email $email, $settings_options_values, $data

//$mailer->SMTPDebug = true;

$result = $mailer->send();

$mailer->ClearAddresses();
$mailer->ClearAllRecipients();

unset($mailer);

return $result
? true
: new WP_Error(
try {
return $mailer->send()
? true
: new WP_Error(
'failed',
__('Could not send the email', 'fw')
);
} catch (phpmailerException $e) {
return new WP_Error(
'failed',
__('Could not send the email', 'fw')
$e->errorMessage()
);
} catch (Exception $e) {
return new WP_Error(
'failed',
$e->getMessage()
);
}
}

}

0 comments on commit 8a6c808

Please sign in to comment.