<?php
/*
Plugin Name: optincrusher
Plugin URI: http://www.danielwatrous.com/optin-footer-plugin
Description: Build your email list fast(er) by adding an animated optin footer to your WordPress blog.  Works with virtually any email service, including Aweber, iContact, ConstantContact, GetResponse, MailChimp, FeedBurner, RapidResponse and many others.
Version: 1.3
Author: Daniel Watrous
Author URI: http://www.danielwatrous.com
License: Copyright 2010 Daniel Watrous, All Rights Reserved (see README.txt in the plugin directory)
*/

require_once("optincrusher_settings.php");
require_once("optincrusher_templates.php");
require_once("optincrusher_functions.php");
require_once("optincrusher_formisolation.php");
require_once(dirname(__FILE__).'../../../../wp-includes/pluggable.php');	// needed to check if logged in
// include this for AJAX use
add_action('wp_ajax_process_optin_form', 'process_optin_form_callback');
add_action('admin_head', 'process_optin_form_javascript');

// return the path to where this plugin is currently installed
function get_plugin_url() {
	// WP < 2.6
	if ( !function_exists('plugins_url') )
		return get_option('siteurl') . '/wp-content/plugins/' . plugin_basename(dirname(__FILE__));

	return plugins_url(plugin_basename(dirname(__FILE__)));
}

// check for non-functional clients
$ie6Client = detect_ie();
$mobileclient = detect_mobile_browser();
if ($ie6Client || $mobileclient) $displayfooter = false;
else $displayfooter = true;
// determine whether to show to logged in users
if (is_user_logged_in()) {
	$hideloggedin = (get_option('optincrusher_showforadmin')==0) ? true:false;
} else $hideloggedin = false;
// check for cookie and decide whether to hide for subscribed
if (isset($_COOKIE['oc_subscribed'])) {
	// check for preference on subscribed users
	$hidesubscribed = (get_option('optincrusher_hidesubscribers')==1) ? true:false;
} else $hidesubscribed = false;

// generate and print optincrusher components that need to be inside the <head>
function optincrusher_header () {
	global $iPhoneClient;
	global $optincrusherStylesheet, $optincrusherJavascript, $optincrusherIntro, $optincrusherClose;
	global $post;

	$optincrusherJavascript = str_replace ( "DISPLAY_DELAY" , get_option('optincrusher_displayDelay') , $optincrusherJavascript);
	$optincrusherJavascript = str_replace ( "SLIDE_SPEED" , get_option('optincrusher_slidespeed') , $optincrusherJavascript);
	$optincrusherJavascript = str_replace ( "SLIDE_STEP" , get_option('optincrusher_slidestep') , $optincrusherJavascript);
	$optincrusherJavascript = str_replace ( "REMEMBER_SHOW_HIDE_STATE" , (get_option('optincrusher_remembershowhidestate') == null || get_option('optincrusher_remembershowhidestate') == 'false') ? 'false':'true' , $optincrusherJavascript);
	$optincrusherJavascript = str_replace ( "ATTRIBUTION_OFFSET" , (get_option("optincrusher_attribution") == null || get_option("optincrusher_attribution") == 'false') ? '23':'0', $optincrusherJavascript);
	$optincrusherJavascript = str_replace ( "PLUGIN_URL" , get_plugin_url() , $optincrusherJavascript);
	$optincrusherJavascript = str_replace ( "SHOW_HIDE_STATE" , get_option('optincrusher_remembershowhidestate') , $optincrusherJavascript);	

	$optincrusherStylesheet = str_replace ( "THUMBNAIL_URL", get_option('optincrusher_thumbnail'), $optincrusherStylesheet);
	$optincrusherStylesheet = str_replace ( "ARROWIMG_URL", get_plugin_url().'/images/arrow.gif', $optincrusherStylesheet);
	$optincrusherStylesheet = str_replace ( "SUBMITBGIMG_URL", get_plugin_url().'/images/submit_gradient.png', $optincrusherStylesheet);
	$optincrusherStylesheet = str_replace ( "XRUNNER_URL", get_option('optincrusher_xrunner'), $optincrusherStylesheet);
	$optincrusherStylesheet = str_replace ( "PITCH_FONT_COLOR", get_option('optincrusher_pitchfontcolor'), $optincrusherStylesheet);
	$optincrusherStylesheet = str_replace ( "PITCH_FONT_FAMILY", get_option('optincrusher_pitchfontfamily'), $optincrusherStylesheet);
	$optincrusherStylesheet = str_replace ( "PITCH_FONT_SIZE", get_option('optincrusher_pitchfontsize'), $optincrusherStylesheet);
	$optincrusherStylesheet = str_replace ( "PITCH_FONT_WEIGHT", get_option('optincrusher_pitchfontweight'), $optincrusherStylesheet);
	$optincrusherStylesheet = str_replace ( "PITCH_TOP_OFFSET", get_option('optincrusher_pitchtopoffset')+31, $optincrusherStylesheet);
	$optincrusherStylesheet = str_replace ( "PITCH_LEFT_OFFSET", get_option('optincrusher_pitchleftoffset')+365, $optincrusherStylesheet);
	$optincrusherStylesheet = str_replace ( "OPTIN_FONT_COLOR", get_option('optincrusher_optinfontcolor'), $optincrusherStylesheet);
	$optincrusherStylesheet = str_replace ( "OPTIN_FONT_FAMILY", get_option('optincrusher_optinfontfamily'), $optincrusherStylesheet);
	$optincrusherStylesheet = str_replace ( "OPTIN_FONT_SIZE", get_option('optincrusher_optinfontsize'), $optincrusherStylesheet);
	$optincrusherStylesheet = str_replace ( "OPTIN_FONT_WEIGHT", get_option('optincrusher_optinfontweight'), $optincrusherStylesheet);
	$optincrusherStylesheet = str_replace ( "LIGHTBOX_BACKGROUND_COLOR", get_option('optincrusher_lightboxbgcolor'), $optincrusherStylesheet);
	$optincrusherStylesheet = str_replace ( "PLUGIN_URL", get_plugin_url(), $optincrusherStylesheet);

	if(!exclude_page($post->ID)){
		echo $optincrusherIntro;
		echo $optincrusherStylesheet;
		echo $optincrusherJavascript;
		echo $optincrusherClose;
	}
}

if ($displayfooter && !$hideloggedin && !$hidesubscribed) add_action('wp_head', 'optincrusher_header');


function optincrusher_html () {
	global $optincrusherHTML;
	global $post;
	$optincrusherHTML = $optincrusherHTML;

	$optincrusherHTML = str_replace ("PLUGIN_URL" , get_plugin_url() , $optincrusherHTML);
	$optincrusherHTML = str_replace ("ARROW_IMG", get_plugin_url().'/images/arrow.gif', $optincrusherHTML);
	$optincrusherHTML = str_replace ("CALL_TO_ACTION_BUTTON", get_option('optincrusher_calltoactionbutton'), $optincrusherHTML);
	$optincrusherHTML = str_replace ("CALL_TO_ACTION", get_option('optincrusher_calltoaction'), $optincrusherHTML);
	$optincrusherHTML = str_replace ("LIGHTBOXMESSAGE", get_option('optincrusher_lightboxmessage'), $optincrusherHTML);
	$optincrusherHTML = str_replace ("OPTIN_FORM", get_option('optincrusher_processedoptinform'), $optincrusherHTML);
	$optincrusherHTML = str_replace ("TITLE_ON_HOVER", get_option('optincrusher_titleonhover'), $optincrusherHTML);
	
	if(!exclude_page($post->ID)) echo $optincrusherHTML;
		
}

if ($displayfooter && !$hideloggedin && !$hidesubscribed) add_action('wp_footer', 'optincrusher_html');

// *********************************************
// SHORTCODE FOR SUBSCRIBER MEMORY
// *********************************************
function optincrusher_thankyou_filter($atts) {
	// output javascript to create a cookie for this subscriber
	$thankyou_cookie = <<<OCTHANKYOU
	<script>
		setCookie('oc_subscribed', 1, '/', 365);
	</script>
OCTHANKYOU;
	return $thankyou_cookie;
}

add_shortcode('optincrusher_thankyou', 'optincrusher_thankyou_filter');


// *********************************************
// SETTINGS MENU BELOW
// *********************************************

if (!function_exists('is_vector')) {
   function is_vector( &$array ) {
      if ( !is_array($array) || empty($array) ) {
         return -1;
      }
      $next = 0;
      foreach ( $array as $k => $v ) {
         if ( $k !== $next ) return false;
         $next++;
      }
      return true;
   }
}

// Add the link to the settings page in the settings sub-header
function myplugin_menu_setup() {
   add_options_page('optincrusher Settings', 'optincrusher', 10, __FILE__, 'myplugin_menu');
}

// Footer link option
function optincrusher_footer_attribution() {
	if (get_option("optincrusher_attribution") == null || get_option("optincrusher_attribution") == 'true') echo "<div style=\"text-align: center; font-size: 8pt;\"><a target=\"blank\" href=\"http://www.danielwatrous.com/optin-footer-plugin\">Build an optin email list in WordPress [Free Software]</a><div>";
}

// Add in the footer
add_action('wp_footer', 'optincrusher_footer_attribution');
add_action('admin_menu', 'myplugin_menu_setup');

?>
