<?php
/**
 * Simple Giveaways Email that is sent to a subscriber when a user subscribes from their links.
 */

/**
 * Class SG_Email_Referrer
 */
class SG_Email_Referrer extends SG_Email {

	/**
	 * Process the Email.
	 * This is method is used for custom email types to send their custom emails.
	 * This method should utilize the send() method.
	 *
	 * @since 2.18.0
	 *
	 * @return mixed
	 */
	public function process() {
		$referrer = $this->data['referrer'];
		$user     = $this->data['user'];
		$entries  = $this->data['entries_received'];

		if ( ! $referrer ) {
			return new WP_Error( 'no-referrer', __( 'No Referrer found', 'giveasap' ) );
		}

		if ( ! $user ) {
			return new WP_Error( 'no-subscriber', __( 'No Subscriber found', 'giveasap' ) );
		}

		$settings = giveasap_get_settings();
		$content  = isset( $settings['referrer_email'] ) ? $settings['referrer_email']  : __( 'Great Job! {{USER_EMAIL}} has subscribed using your link.<br/>You have received {{ENTRIES_RECEIVED}} entries.<br/>You now have in total {{ENTRIES_TOTAL}} entries.', 'giveasap' );
		$subject  = isset( $settings['referrer_email_subject'] ) ? $settings['referrer_email_subject'] : __( 'Someone has subscribed from your link.', 'giveasap' );

		if( ! $content ) {
			return new WP_Error( 'no-email-text', __( 'There is no Email text set.', 'giveasap' ) );
		}

		$content = str_replace('{{USER_EMAIL}}', $user->get_email(), $content );
		$content = str_replace('{{ENTRIES_RECEIVED}}', $entries, $content );
		$content = str_replace('{{ENTRIES_TOTAL}}', $referrer->get_entries(), $content );

		$email = $referrer->get_email();
		if( ! $email ) {
			return new WP_Error( 'no-ref-email', __( 'No Referrer Email found.', 'giveasap' ) );
		}

		return $this->send( $email,  $subject, $content );
	}
}