<?php
/* 
Plugin Name: New User Email Setup
Plugin URI: http://www.epicalex.com/new-user-email-set-up/
Version: 0.2.5
Author: Alex Cragg
Author URI: http://epicalex.com/
Description: A Plugin to setup the registration email sent to new users.
*/

/*
Copyright (C) 2007 Alex Cragg

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

add_action('activate_'.basename(__FILE__), 'newuseremailactivate');
add_action('admin_menu', 'newuseremailoptions');
add_action('admin_init', 'nue_admin_init' );

// Added for 2.7 functionality
function nue_admin_init(){
	if(function_exists('register_setting')) {
	register_setting('nue-options-group', 'newuseremailhtml');
	register_setting('nue-options-group', 'newuseremailfromaddress');
	register_setting('nue-options-group', 'newuseremailfrom');
	register_setting('nue-options-group', 'newuseremailsubject');
	register_setting('nue-options-group', 'newuseremailadminsubject');
	register_setting('nue-options-group', 'newuseremailtext');
	register_setting('nue-options-group', 'newuseremailadmintext');
	}
}

function newuseremailactivate()
{
  if (!get_option("newuseremailhtml"))
    update_option("newuseremailhtml", 'text/HTML');

  if (!get_option("newuseremailfromaddress"))
    update_option("newuseremailfromaddress", 'Enter your admin email here');

  if (!get_option("newuseremailsubject"))
    update_option("newuseremailsubject", 'Welcome to %blogname%');

  if (!get_option("newuseremailtext"))
    update_option("newuseremailtext", 'Welcome %username%,<br /> please find below your login details.<br /> I hope you enjoy our site.<br /> <strong>Username:</strong> %username%<br /> <strong>Password:</strong> %password%<br /> %loginurl%');

  if (!get_option("newuseremailadminsubject"))
    update_option("newuseremailadminsubject", '%blogname% - New User Registration');

  if (!get_option("newuseremailadmintext"))
    update_option("newuseremailadmintext", 'There is a new user registered on your blog:<br /> <strong>Username:</strong> %username%<br /> <strong>Email:</strong> %useremail%');
}


if ( !function_exists( 'newuser_mail' ) ) :
function newuser_mail($to, $subject, $message, $headers = '') {
	global $phpmailer;

	if ( !is_object( $phpmailer ) ) {
		require_once(ABSPATH . WPINC . '/class-phpmailer.php');
		require_once(ABSPATH . WPINC . '/class-smtp.php');
		$phpmailer = new PHPMailer();
	}

	$mail = compact('to', 'subject', 'message', 'headers');
	$mail = apply_filters('wp_mail', $mail);
	extract($mail, EXTR_SKIP);

	if ( $headers == '' ) {
		$headers = "MIME-Version: 1.0\n" .
			"From: " . apply_filters('wp_mail_from', "wordpress@" . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']))) . "\n" . 
			"Content-Type: text/HTML; charset=\"" . get_option('blog_charset') . "\"\n";
	}

	$phpmailer->ClearAddresses();
	$phpmailer->ClearCCs();
	$phpmailer->ClearBCCs();
	$phpmailer->ClearReplyTos();
	$phpmailer->ClearAllRecipients();
	$phpmailer->ClearCustomHeaders();

	$phpmailer->FromName = "WordPress";
	$phpmailer->AddAddress("$to", "");
	$phpmailer->Subject = $subject;
	$phpmailer->Body    = $message;
        if (get_option("newuseremailhtml") == 'text/HTML' ) {
	$phpmailer->IsHTML( true );
	    } else {
	$phpmailer->IsHTML( false );
	    }
	$phpmailer->IsMail(); // set mailer to use php mail()

	do_action_ref_array('phpmailer_init', array(&$phpmailer));

	$mailheaders = (array) explode( "\n", $headers );
	foreach ( $mailheaders as $line ) {
		$header = explode( ":", $line );
		switch ( trim( $header[0] ) ) {
			case "From":
				$from = trim( str_replace( '"', '', $header[1] ) );
				if ( strpos( $from, '<' ) ) {
					$phpmailer->FromName = str_replace( '"', '', substr( $header[1], 0, strpos( $header[1], '<' ) - 1 ) );
					$from = trim( substr( $from, strpos( $from, '<' ) + 1 ) );
					$from = str_replace( '>', '', $from );
				} else {
					$phpmailer->FromName = $from;
				}
				$phpmailer->From = trim( $from );
				break;
			default:
				if ( $line != '' && $header[0] != 'MIME-Version' && $header[0] != 'Content-Type' )
					$phpmailer->AddCustomHeader( $line );
				break;
		}
	}

	$result = @$phpmailer->Send();

	return $result;
}
endif;

if ( !function_exists('wp_new_user_notification') ) {
	function wp_new_user_notification($user_id, $plaintext_pass = '') {
	$user = new WP_User($user_id);

	$user_login = stripslashes($user->user_login);
	$user_email = stripslashes($user->user_email);
    
    	$find = array('/%username%/i', '/%password%/i', '/%blogname%/i', '/%siteurl%/i', '/%loginurl%/i', '/%useremail%/i');
    	$replace = array($user_login, $plaintext_pass, get_option('blogname'), get_option('siteurl'), get_option('siteurl').'/wp-login.php', $user_email);
    
    	$headers .= "MIME-Version: 1.0\n" .
	    "From: ". $user_email . "\n" . 
    	"Content-Type: ". get_option("newuseremailhtml") ." charset=\"" . get_option('blog_charset') . "\"\n";
        
    	$subject = stripslashes(get_option("newuseremailadminsubject"));
    	$subject = preg_replace($find, $replace, $subject);
    	$subject = preg_replace("/%.*%/", "", $subject);

    	
    	$message = stripslashes(get_option("newuseremailadmintext"));
    	$message = preg_replace($find, $replace, $message);
    	$message = preg_replace("/%.*%/", "", $message);

	@newuser_mail(get_option('newuseremailfromaddress'), $subject, $message, $headers);

	if ( empty($plaintext_pass) )
		return;

    	$headers .= "MIME-Version: 1.0\n" .
	    "From: ". get_option("newuseremailfrom") ."<". get_option("newuseremailfromaddress") . ">\n" . 
    	"Content-Type: ". get_option("newuseremailhtml") ." charset=\"" . get_option('blog_charset') . "\"\n";
        
    	$subject = stripslashes(get_option("newuseremailsubject"));
    	$subject = preg_replace($find, $replace, $subject);
    	$subject = preg_replace("/%.*%/", "", $subject);	

    	$message = stripslashes(get_option("newuseremailtext"));
    	$message = preg_replace($find, $replace, $message);
    	$message = preg_replace("/%.*%/", "", $message);
		
	newuser_mail($user_email, $subject, $message, $headers);
	}
}

function printnewuseremailAdminOptions() {
if ($_REQUEST['submit']) {
	update_newuseremail(); 
}
?>
<div class="wrap">
<h2>New User Email</h2>
<div id="message" class="error fade">This plugin looks and works better with PHP 5 running on your server. If you are using this version of the plugin, I'm assuming you're on PHP 4. If not, please download the latest 0.5 version from <a href="http://epicalex.com/new-user-email-setup-update/">here</a> <?php
</div>
print_newuseremail(); 
?></div><?php
}

function update_newuseremail() {
	$ok = false;
	if ($_REQUEST['newuseremailhtml']) {
		update_option('newuseremailhtml',$_REQUEST['newuseremailhtml']);
		$ok = true;
	}
	if ($_REQUEST['newuseremailfromaddress']) {
		update_option('newuseremailfromaddress',$_REQUEST['newuseremailfromaddress']);
		$ok = true;
	}
	if ($_REQUEST['newuseremailfrom']) {
		update_option('newuseremailfrom',$_REQUEST['newuseremailfrom']);
		$ok = true;
	}
	if ($_REQUEST['newuseremailsubject']) {
		update_option('newuseremailsubject',$_REQUEST['newuseremailsubject']);
		$ok = true;
	}
	if ($_REQUEST['newuseremailtext']) {
		update_option('newuseremailtext',$_REQUEST['newuseremailtext']);
		$ok = true;
	}
	if ($_REQUEST['newuseremailadminsubject']) {
		update_option('newuseremailadminsubject',$_REQUEST['newuseremailadminsubject']);
		$ok = true;
	}
	if ($_REQUEST['newuseremailadmintext']) {
		update_option('newuseremailadmintext',$_REQUEST['newuseremailadmintext']);
		$ok = true;
	}
	if ($ok) {
		?><div id="message" class="updated fade">
			<p>Options Updated</p>
		</div><?php
	}
	else {
		?><div id="message" class="error fade">
			<p>There was an error, failed to save options</p>
		</div><?php
	} 
}

function print_newuseremail() {
?>
<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
<?php 
		if (function_exists(' settings_fields')) { settings_fields('nue-options-group'); } ?> <!-- Added for 2.7 functionality -->
<table class="form-table">
<tr valign="top">
 	<th scope="row">Allow HTML in Email Content?</th>
 	<td>
 	 	<label for="newuseremailhtml_yes">
 		<input id="newuseremailhtml_yes" type="radio" value="text/HTML" <?php if (get_option("newuseremailhtml") == "text/HTML") { _e('checked="checked"', "NewUserEmail"); }?> name="newuseremailhtml"/>
		Yes
		</label>
		<br />
		<label for="newuseremailhtml_no">
 		<input id="newuseremailhtml_no" type="radio" value="text/plain" <?php if (get_option("newuseremailhtml") == "text/plain") { _e('checked="checked"', "NewUserEmail"); }?> name="newuseremailhtml"/>
		No
		</label> 
 	</td>
 </tr>

<tr valign="top">
<th scope="row">Registration Email Subject</th>
<td>
<input id="newuseremailsubject" type="text" size="40" value="<?php _e(stripslashes(get_option('newuseremailsubject')), 'NewUserEmail') ?>" name="newuseremailsubject"/>
</td>
</tr>

<tr valign="top">
<th scope="row">Registration Email Text</th>
<td>

<textarea id="newuseremailtext" style="width: 98%; font-size: 12px;" rows="4" cols="60" name="newuseremailtext"><?php _e(stripslashes(get_option('newuseremailtext')), 'NewUserEmail') ?>
</textarea>

<p>Use this to create a custom email that is sent to new users when they register. It overides the default text and you can write anything at all in here, but remember to use the following variables so that your users still know how to login! 
<br /><br />
%username%, %useremail%, %password%, %siteurl%, %blogname%, and %loginurl%.  
<br /><br />
Note the percentage signs (%), each variable must have percentage signs around them with <strong>no spaces</strong>.  For an example text, using some of the variables, please see above.
<br /><br />
<strong>IMPORTANT:</strong> Make sure that you test out your new email by registering yourself so that you see what your users are receiving.</p>
</p>
</td>
</tr>

<tr valign="top">
<th scope="row">From Address</th>
<td>
<input id="newuseremailfromaddress" type="text" size="40" value="<?php _e(stripslashes(get_option('newuseremailfromaddress')), 'NewUserEmail') ?>" name="newuseremailfromaddress"/><br />
NB You must have this email set up as a real email address, otherwise it will be sent from your host's mailbox, which looks ugly!
</td>
</tr>

<tr valign="top">
<th scope="row">From Name</th>
<td>
<input id="newuseremailfrom" type="text" size="40" value="<?php _e(stripslashes(get_option('newuseremailfrom')), 'NewUserEmail') ?>" name="newuseremailfrom"/><br />
This can be anything from admin, to your name, to your blog's name.
</td>
</tr>

<tr valign="top">
<th scope="row">Administration Notification Email Subject</th>
<td>
<input id="newuseremailadminsubject" type="text" size="40" value="<?php _e(stripslashes(get_option('newuseremailadminsubject')), 'NewUserEmail') ?>" name="newuseremailadminsubject"/>
</td>
</tr>

<tr valign="top">
<th scope="row">Administration Notification Email Text</th>
<td>

<textarea id="newuseremailadmintext" style="width: 98%; font-size: 12px;" rows="4" cols="60" name="newuseremailadmintext"><?php _e(stripslashes(get_option('newuseremailadmintext')), 'NewUserEmail') ?>
</textarea>

<p>
This is to define the email that is sent to the Blog Administrator when a new user registers, you can use the same variables as above.
</p>
</td>
</tr>
</table>

<div class="submit">
<input type="submit" name="submit" value="<?php _e('Update Settings', 'NewUserEmail') ?>" />
</div>

<table class="form-table">
<tr valign="top">
<th scope="row">Help Me!</th>
<td>
If you need help with this plugin, or if you want to make a suggestion, then please email me at alex AT epicalex DOT com
</td>
</tr>

<tr valign="top">
<th scope="row">Support This Plugin!</th>
<td>
There are a few ways you can support me to say thanks for making this plugin, you can donate by paypal, or you can sign up to something through one of my affiliate links such as Bluehost or Dreamhost.<br /><br />
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but21.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<img alt="" border="0" src="https://www.paypal.com/en_GB/i/scr/pixel.gif" width="1" height="1">
<input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIH0QYJKoZIhvcNAQcEoIIHwjCCB74CAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwDQYJKoZIhvcNAQEBBQAEgYCTL5BBkUz91Q8Eczlpdag+dUOTVGLYyJ01JqQZJ6eL/mngkh0Al9ZHLs4Eg6zRi0uZKDL9mL1jA44lXccYXvQR/2U/AbR/Iqt5Bm53knQK21jvWBfCfWu0F6n4DeGWE6Z6ph47K/E4KzA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RxYhe6W/7UGyAVRlKW1Nm5pHRvxWY9UAmHXMlMtHprs/OT3u8BJQ9E1T6a8qsHwdAQtefD52raiBwxPoEz7FXVjXD9SbSn33PLRmPIhiPJ4S+wSZudwKGfw==-----END PKCS7-----
">
</form>
<script src=http://www.bluehost.com/src/js/epicskitours/CODE15/88x31/3.gif></script>
<p>when you visit www.dreamhost.com and sign up for hosting, be sure to use the promo code ALEXMYSOUTHAM to get $50 off your bill, PLUS 10% extra bandwidth!</p>
</td>
</tr>

</table>

</form>
				<?php }

function newuseremailoptions() {
		add_options_page( 
					'New User Email',
					'New User Email',
					'manage_options',
					__FILE__,
					'printnewuseremailAdminOptions'
					);
}	
?>
