Skip to content

Instantly share code, notes, and snippets.

@qutek
Last active March 14, 2018 07:31
Show Gist options
  • Save qutek/f645e67e616ac355f6e705513ac78458 to your computer and use it in GitHub Desktop.
Save qutek/f645e67e616ac355f6e705513ac78458 to your computer and use it in GitHub Desktop.
[Debug Email] Simple script to change all email recipients for debugging / development purpose #wordpress #email #development-tools
<?php
/**
* Plugin Name: Debug Email
* Description: Simple script to change all email recipients for debugging / development purpose
* Version: 0.1
* Author: Lafif Astahdziq
* Author URI: https://lafif.me
*/
add_action( 'init', 'test_send_email' );
function test_send_email(){
if(!isset($_GET['send_email'])){
return;
}
$email = !empty($_GET['send_email']) ? $_GET['send_email'] : '[email protected]';
$x = wp_mail( $email, 'Subject', 'message' );
var_dump($x);
exit();
}
add_action( 'wp_ajax_save_debug_email', 'save_debug_email' );
function save_debug_email(){
$response = array(
'success' => false,
'message' => 'Failed to save email debug',
);
try {
if(!isset($_POST['email']) || !is_email( $_POST['email'] ))
throw new Exception('Email not valid');
if(!isset($_POST['nonce']) || !wp_verify_nonce( $_POST['nonce'], 'save_debug_email' ))
throw new Exception('You are not allowed.');
$save = update_option( 'debug_email', sanitize_email( $_POST['email'] ) );
if($save === false)
throw new Exception('Failed to save.');
$response = array(
'success' => true,
'message' => sprintf('Email Debug: %s', sanitize_email( $_POST['email'] ) ),
);
} catch (Exception $e) {
$response['message'] = $e->getMessage();
}
wp_send_json( $response );
}
add_filter( 'wp_mail', 'change_email_args', 100, 1 );
function change_email_args($args){
$debug_email = get_option( 'debug_email', '' );
if(empty($debug_email)){
return $args;
}
$to = $args['to'];
$args['to'] = $debug_email;
$args['subject'] = '['.$to.'] ' . $args['subject'];
return $args;
}
add_action('admin_bar_menu', 'add_toolbar_items', 100);
function add_toolbar_items($admin_bar){
if(!current_user_can( 'manage_options' ))
return;
$debug_email = get_option( 'debug_email', '' );
$title = !empty($debug_email) ? sprintf('Email Debug: %s', $debug_email) : 'Debug email disabled: Click to set';
$admin_bar->add_menu( array(
'id' => 'debug-email-recipient',
'title' => $title,
'href' => '#',
'meta' => array(
'title' => !empty($debug_email) ? sprintf('All email will be sent to: %s (Click to change)', $debug_email) : 'Click to change',
),
));
?>
<style>
#wp-admin-bar-debug-email-recipient a.state-input {
display: block;
overflow: hidden;
padding: 0px;
width: 100%;
}
#wp-admin-bar-debug-email-recipient a.state-input button {
background: #d1500a;
color: #fff;
padding: 0 5px;
cursor: pointer;
}
</style>
<script>
(function($){
var DebugEmail = {
form_open: false,
text: $('#wp-admin-bar-debug-email-recipient a').html(),
init: function(){
$(document).on('click', '#wp-admin-bar-debug-email-recipient a', function(e){
e.preventDefault();
if($(this).hasClass('state-input')){
return false;
}
DebugEmail.form_open = true;
DebugEmail.text = $(this).html();
// alert(DebugEmail.text);
$(this).addClass('state-input').html('<input type="text" id="debug_email" placeholder="Your email.."><button id="save_debug_email">Save</button>');
});
$(document).on('click', '#save_debug_email', function(e){
e.preventDefault();
$(this).text('Saving..');
$.ajax({
type: 'POST',
data: {
action: 'save_debug_email',
nonce: '<?php echo wp_create_nonce( 'save_debug_email' ); ?>',
email: $('#debug_email').val()
},
dataType: 'json',
url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
xhrFields: {
withCredentials: true
},
success: function (response) {
// alert('ok');
// console.log(response);
if(response.success){
DebugEmail.text = response.message;
} else {
alert(response.message);
}
},
complete: function(){
$('#wp-admin-bar-debug-email-recipient a').removeClass('state-input').html(DebugEmail.text);
DebugEmail.form_open = false;
}
});
});
$(document).click(function (e) {
var container = $('#wp-admin-bar-debug-email-recipient');
if ( DebugEmail.form_open
&& !container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.find('a').removeClass('state-input').html(DebugEmail.text);
DebugEmail.form_open = false;
}
});
}
};
DebugEmail.init();
})(jQuery);
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment