<?php
/*
Plugin Name: Drip Email
Plugin URI: http://www.activeconvert.com
Description: Beautiful automated marketing emails and email capture forms to convert visitors into customers. To get started: 1) Click the "Activate" link to the left of this description, 2) Go to your Drip Email Marketing plugin Settings page, and click Get My API Key.
Version: 1.1.4
Author: ActiveConvert
Author URI: http://www.activeconvert.com/
*/

$emwp_domain = plugins_url();
add_action('init', 'emwp_init');
add_action('admin_notices', 'emwp_notice');
add_filter('plugin_action_links', 'emwp_plugin_actions', 10, 2);
add_action('wp_footer', 'emwp_insert',4);
add_action('admin_footer', 'acwpRedirect');
define('emwp_DASHBOARD_URL', "https://www.activeconvert.com/dashboard.do?wp=true");
define('emwp_SMALL_LOGO',plugin_dir_url( __FILE__ ).'ac-small-white.png');
function emwp_init() {
    if(function_exists('current_user_can') && current_user_can('manage_options')) {
        add_action('admin_menu', 'emwp_add_settings_page');
        add_action('admin_menu', 'emwp_create_menu');
    }
}
function emwp_insert() {

    global $current_user;
    if(strlen(get_option('emwp_widgetID')) == 32 ) {
	echo("\n<!-- Email Marketing by ActiveConvert (www.activeconvert.com) -->\n<script src='//www.activeconvert.com/api/activeconvert.1.0.js#".get_option('emwp_widgetID')."' async='async'></script>\n");
    }
}

function emwp_notice() {
    if(!get_option('emwp_widgetID')) echo('<div class="error" style="padding:10px;"><p><strong><a style="text-decoration:none;border-radius:3px;color:white;padding:10px; ;background:#029dd6;border-color:#06b9fd #029dd6 #029dd6;margin-right:20px;"'.sprintf(__('href="%s">Activate your Email Marketing by ActiveConvert account</a></strong>  Almost done - activate your account and say hello to beautiful email sequences.' ), admin_url('options-general.php?page=email-marketing-by-activeconvert')).'</p></div>');
}

function emwp_plugin_actions($links, $file) {
    static $this_plugin;
    $emwp_domain = plugins_url();
    if(!$this_plugin) $this_plugin = plugin_basename(__FILE__);
    if($file == $this_plugin && function_exists('admin_url')) {
        $settings_link = '<a href="'.admin_url('options-general.php?page=email-marketing-by-activeconvert').'">'.__('Settings', $emwp_domain).'</a>';
        array_unshift($links, $settings_link);
    }
    return($links);
}

function emwp_add_settings_page() {
    function emwp_settings_page() {
        global $emwp_domain ?>
	<div class="wrap">
        <p style="margin-left:4px;font-size:18px;"><strong>Email Marketing</strong> by <?php wp_nonce_field('update-options') ?>
		<a href="http://www.activeconvert.com" target="_blank" title="ActiveConvert"><?php echo '<img src="'.plugins_url( 'activeconvert.png' , __FILE__ ).'" height="17" style="margin-bottom:-1px;"/>';?></a> to send drip email sequences and convert visitors into customers.</p>
 		
	<div id="emwp_register" class="inside" style="padding: -30px 10px"></p>	
        	<div class="postbox" style="max-width:600px;height:50px;padding:30px;">
            	
		<div style="float:left">	
			<b>Activate Email Marketing by ActiveConvert</b> <br>
			<p>Login or sign up now for a free trial.</p>
		</div>
		<div><a href='https://www.activeconvert.com/emailMarketing.jsp' class="right button button-primary" target="_blank">Get My API Key</a></div>
		</div>

   		<div class="postbox" style="max-width:600px;height:50px;padding:30px;">
            	<div style="float:left">
			<b>Enter Your API Key</b> <br>
			<p>If you already know your API Key.</p>
		</div>
		<div class="">
		<form id="saveSettings" method="post" action="options.php">
                   <?php wp_nonce_field('update-options') ?>
			<div style="float:right">
			<input type="text" name="emwp_widgetID" id="emwp_widgetID" placeholder="Your API Key" value="<?php echo(get_option('emwp_widgetID')) ?>" style="margin-right:10px;" />
                        <input type="hidden" name="page_options" value="emwp_widgetID" />
			<input type="hidden" name="action" value="update" />
                        <input type="submit" class="right button button-primary" name="emwp_submit" id="emwp_submit" value="<?php _e('Save Key', $emwp_domain) ?>" /> 
			</div>
                </form>
		</div>
               
            	
	</div>
	</div>
	

	<div id="emwp_registerComplete" class="inside" style="padding: -20px 10px;display:none;">
	<div class="postbox" style="max-width:600px;height:250px;padding:30px;padding-top:5px">
<h3 class=""><span id="emwp_noAccountSpan">Email Marketing Settings</span></h3>
		<p>Customize your email popup and marketing emails by selecting 'Customize' below. By default your popup is triggered when a visitor is leaving your site.  You can change this to trigger immediately, or after a configurable amount of time.</p>
		<p>
		<div style="text-align:center">
		
		<a href='https://www.activeconvert.com/dashboard.do?em=true' class="button button-primary" target="_ac">Dashboard</a>&nbsp;
<a href='https://www.activeconvert.com/campaigns.do' class="button button-primary" target="_ac">Customize</a>&nbsp;
		
<a href='https://www.activeconvert.com/wppreview.do?wid=<?php echo(get_option('emwp_widgetID')) ?>' class="button button-primary" target="_ac">Popup Preview</a>&nbsp;
		<br><br><a id="changeWidget" class="" target="_blank">Enter Different API Key</a>&nbsp;
		</div>
		</p>* The popup is only triggered once per browser session.  Open a new browser window to test multiple times.

</div>
</div>
<script>
jQuery(document).ready(function($) {

var emwp_wid= $('#emwp_widgetID').val();
if (emwp_wid=='') 
{}
else
{

	$( "#emwp_enterwidget" ).hide();
	$( "#emwp_register" ).hide();
	$( "#emwp_registerComplete" ).show();
	$( "#emwp_noAccountSpan" ).html("Email Marketing Plugin Settings");

}

$(document).on("click", "#emwp_inputSaveSettings", function () {
	$( "#saveDetailSettings" ).submit();
});

$(document).on("click", "#changeWidget", function () {
$( "#emwp_register" ).show();
$( "#emwp_inputSaveSettings" ).hide();
});


});
</script>
<?php }
$emwp_domain = plugins_url();
add_submenu_page('options-general.php', __('Email Marketing', $emwp_domain), __('Email Marketing', $emwp_domain), 'manage_options', 'email-marketing-by-activeconvert', 'emwp_settings_page');
}
function addacemLink() {
$dir = plugin_dir_path(__FILE__);
include $dir . 'options.php';
}
function emwp_create_menu() {
  $optionPage = add_menu_page('Email Marketing', 'Email Marketing', 'administrator', 'emwp_dashboard', 'addacemLink', plugins_url('ac-small-white.png', __FILE__));
}
function acwpRedirect() {
$redirectUrl = "https://www.activeconvert.com/dashboard.do?wp=true";
echo "<script> jQuery('a[href=\"admin.php?page=emwp_dashboard\"]').attr('href', '".$redirectUrl."').attr('target', '_blank') </script>";}
?>