<?php

/**
 * Returns the selected HTML form template
 *
 * This function returns the selected HTML
 * form template. If the specified form
 * can't be found it will fallback to a
 * default form template.
 *
 * @function add_form()
 *
 * @uses get_template_part()
 *
 * @param (array) $form[ (string) type ]
 *
 * @return (string) The HTML form template retrieved from the Theme
 *
 * @since 1.0.0 
 *
 */
function add_form( $form ) {
	
	$form = shortcode_atts( array( 'type' => 'contact' ), $form, 'form' );
	
	ob_start();
	
	get_template_part( 'templates/form', $form['type'] );
	
	return ob_get_clean();
}
add_shortcode( 'form', 'add_form' );



/**
 * Returns a unique session token
 *
 * This function returns a unique session token
 * to be used within a form. The token is sent
 * with the form _POST parameters and compared
 * to the session token on the server. This is
 * to check the form submission is from the
 * same session.
 *
 * @function session_token()
 *
 * @param (string) $form The unique id of the form
 *
 * @return (string) The session token
 *
 * @since 1.1.8 
 *
 */
function session_token( $form ) {
    
    // This is a WP Super Cache constant to prevent the newsletter page being cached.
    define( 'DONOTCACHEPAGE', true );
	// This is a Litespeed Cache constant to prevent the newsletter page being cached.
	define('LSCACHE_NO_CACHE', true);
	
	$token = md5( uniqid( microtime(), true ) );
	$_SESSION[$form . '-token'] = $token; 
	echo $token;
}



/**
 * Sets the <Sender> address in form emails
 *
 * This function sets the <Sender> address in form
 * emails to match the <From> address. This is to
 * help prevent email being marked as spam.
 * This is the equivalent of using the -f flag in
 * a regular php mail() header.
 *
 * @function email_return_path()
 *
 * @uses ow_development()
 *
 * @param (object) $phpmailer A PHP Mailer object
 *
 * @return N/A
 *
 * @since 1.0.0 
 *
 */

function email_return_path( $phpmailer ) {
	
    $phpmailer->Sender = $phpmailer->From;
}
add_action( 'phpmailer_init', 'email_return_path' );
 


/**
 * Return the email headers
 *
 * This function returns the email headers for the
 * website forms. The headers include the <From>,
 * <Reply-To>, optional <CC> and also the
 * <Content-Type> for HTML emails.
 *
 * @function get_email_headers()
 *
 * @uses ow_global_var()
 *
 * @param N/A
 *
 * @return (array) The email headers to be used in wp_mail()
 *
 * @since 1.0.0 
 *
 */

function get_email_headers() {
	
	$from = html_entity_decode( ow_global_var('accountant_name') );
	$recipient = ow_global_var( 'form_email' );
	$headers[] = 'From: ' . $from . ' <' . $recipient . '>';
	/**
	 * If the customer is having trouble with the website
	 * forms, you can test if they are being sent/recieved
	 * by adding your email as a carbon copy (CC). Your
	 * email address must be in < > tags.
	 *
	 * @example $headers[] = 'CC: <EMAIL@ADDRESS.COM>';
	 *
	 */
	$headers[] = 'Reply-To: ' . $recipient;
	$headers[] = 'Content-Type: text/html; charset=UTF-8';
	return $headers;
}



/**
 * Returns the email footer
 *
 * This function returns the email footer which
 * includes the date and time of when the email
 * was sent, and the IP address it was sent from.
 * This is to help with spam - allowing the IP to
 * be blocked if the email isn't legitimate.
 * If the email is spam, the IP address can be
 * blocked via the customer's C-Panel, or by the
 * server provider
 *
 * @function get_email_footer()
 *
 * @param N/A
 *
 * @return (string) The email footer
 *
 * @since 1.0.0 
 *
 * @updated 1.5.2
 *
 */
function get_email_footer() {
	
	$footer = '<br /><br /><hr><br />';
	$footer .= '<img src="https://developer.iriswebportal.co.uk/wp-content/uploads/2017/03/iris-wp-logo-01.png"/><br />';
	$footer .= 'The information below is to be forwarded to IRIS WebPortal in case of abuse:<br />';
	
	if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
		
		$footer .= 'IP: ' . $_SERVER['REMOTE_ADDR'] . '<br />';
	};
	date_default_timezone_set( 'Europe/London' );
	$footer .= 'Date and time the form was submitted: ' . date( 'l dS \of F Y h:i:s A' );
	return $footer;
}