<?php
//shortcode Deprecated
add_shortcode( 'subscribe-sendy-form', 'return_sendy_subscribe_form' );


/* Function

*Shortcode  [sendy] Deprecated
*returns  the form
*/

function return_sendy_subscribe_form( $attr ) {
	if( !isset($attr['id'])){
		return 'Please add list id in shortcode';

	}
	$ids=explode( ',', $attr['id'] );

	$translated = get_option( 'sendy_translation_settings' );
	$redirect_url = get_option( 'sendy_redirect_url' );

	$form_html="<script type='text/javascript'>


	jQuery(function(){

		jQuery('body').on('submit','.shortcode-subscribe-form',function(){

			var formFields= jQuery(this).serialize();

			var emailId=document.getElementById('sendy-subscriber-email').value;
			var redirect_url = '".$redirect_url."';
			var filter = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;

			valid= String(emailId).search (filter) != -1;

			if(!valid){

				alert('".$translated['email_error']."');
				return false;
			}

			var terms=jQuery( '#shortcode_sendy_terms' )
			if(terms.length>0){
				if(! terms.prop( 'checked' )){

					alert('Please accept terms & conditions');
					return false;
				}}

				jQuery.ajax({
					type:'post',
					url:'".admin_url( "admin-ajax.php", "http" )."',
					data:{action:'add_sendy_user',formdata:formFields}
				}).done(function(result){
					if ( result ==  1 ) {
						jQuery('.shortcode_sendy_error').slideUp();
						jQuery('.shortcode_sendy_success').text( \"".$translated['success_message']."\" ).slideDown();
						if(redirect_url !=''){
										window.location.href = redirect_url;
							}
						document.getElementById('sendy_siimage').src = '".SENDY_WIDGET_PRO_URL."/inc/captcha/securimage_show.php?sid='+ Math.random();
						jQuery('#captcha_short_code').val('');
					}
					else if(result == 'captcha_error'){

						jQuery('.shortcode_sendy_error').text( '".$translated['captcha_error']."' ).slideDown();
					} else {
						jQuery('.sendy_success').slideUp();
						if ( result == 'Already subscribed.' ) {
							jQuery('.shortcode_sendy_error').text( '". $translated['already_subscribed'] ."' ).slideDown();
						} else {
							jQuery('.shortcode_sendy_error').text( result ).slideDown();
						}

						document.getElementById('sendy_siimage').src = '".SENDY_WIDGET_PRO_URL."/inc/captcha/securimage_show.php?sid='+ Math.random();
						jQuery('#captcha_short_code').val('');
					}
				}).fail(function(){

				})
return false;
})
})

</script>";

	$form_html.='<form  class="shortcode-subscribe-form" action="'.get_option( "sendy_url" ).'/subscribe" method="POST" accept-charset="utf-8">';

	if ( $attr['name']=='true' ) {

		$form_html.='<div class="name-cont"><label for="name">'.$translated['name'].'</label>
	<input type="text" name="name" id="subscriber-name"/></div>
	';


	}

	$form_html.='<div class="email-cont"><label for="email">'.$translated['email'].'</label>
<input type="text" name="email" id="sendy-subscriber-email"/></div>
<div>';

	$sendy_url=get_option( 'sendy_url' );
	$sendy_api_key=get_option( 'sendy_api_key' );
	$sendy_lists=get_option( 'sendy_lists' );

	$count=0;
	if ( $sendy_lists ) {
		$url=$sendy_url.'/api/subscribers/active-subscriber-count.php';

	}

	if ( $sendy_lists && !empty( $ids ) ) {

		foreach ( $ids as $key => $list_id ) {

			if ( array_key_exists( $list_id, $sendy_lists ) ) {

				$response= wp_remote_post( $url, array( 'body'=>array( 'api_key'=>$sendy_api_key, 'list_id'=>$list_id ) ) );

				if ( is_wp_error( $response ) ) {

					$error_message = $response->get_error_message();

					$count=0;

				} else {

					$count+=$response['body'];
				}

				$list_field = apply_filters( 'swp_list_field_type', '<input type="hidden" class="list" name="list[]" value="'.$list_id.'"/>', $list_id, $sendy_lists );

				$form_html.=$list_field;

			}

		}

	}else {

		$form_html.= '<div style="color:red">Error: Please select a list from widget</div>';

	}


	if ( $attr['subscribers']=='true' ) {
		$form_html.= '<div class="sendy-subscriber-count">';
		$form_html.=  $count.' '.$translated['subscriber_counter'].'
	</div>';
	}



	$form_html.= '</div>';


	if ( $attr['captcha']=='true' ) {
		$form_html.= '<div class="sendy-form-row-captcha">

	<div><img id="sendy_siimage" class="captcha-image"  src="'.SENDY_WIDGET_PRO_URL.'/inc/captcha/securimage_show.php?sid='.md5( uniqid() ).'" alt="CAPTCHA Image" ></div>

	<div>
		Code: <input type="text" id="captcha_short_code" class="captcha-input" name="captcha_code" size="10" maxlength="6" />


		<a tabindex="-1" href="#" title="Refresh Image" onclick="document.getElementById(\'sendy_siimage\').src = \''.SENDY_WIDGET_PRO_URL.'/inc/captcha/securimage_show.php?sid=\'+ Math.random(); this.blur(); return false">

			<img class="img-captcha-ref" style="vertical-align:middle" src="'.SENDY_WIDGET_PRO_URL.'/inc/captcha/images/refresh.png" alt="Reload Image" onclick="this.blur()"  border="0"></a>
		</div>
	</div>';
	}



	$terms_link = get_option( 'sendy_terms_link' );
	if ( isset( $attr['terms'] ) ) {
		if ( $attr['terms']=='true'  && $terms_link ) {

			$form_html.= '<div class="sendy_terms">
	<input type="checkbox" id="shortcode_sendy_terms" > <a target="_blank" href="'.$terms_link.'">'.$translated['terms_text'].'</a>
</div>';
		}
	}



	$form_html.= '<div class="shortcode_sendy_error" style="display:none;color:red"></div>
<div class="shortcode_sendy_success" style="display:none;color:green"></div>
<input type="submit" name="sub-submit" value="'.$translated["subscribe_button"].'" class="sendy-submit-btn"  />
<div class="resp"></div>
</form>';

	return $form_html;

}// function ends
