<?php

/**
* Default double opt-in (after registering for a contest and it has double opt-in) template called by main widget.php template.
* @package chTemplate
*/
$widget = cf_Widget::current_widget();
$text = nl2br(esc_attr($widget->contest->cf_double_optin_message));
echo <<<HTML
<div class="double_optin_message">
{$text}
</div>
HTML
;
 /**
  * Generates custom content for preview tab.
  */
 protected function generate_preview()
 {
     wp_enqueue_style('cf_css_base');
     wp_enqueue_script('jquery-ui-resizable');
     echo '<h3>' . __('Before submit preview:', 'contestfriend') . '</h3>';
     echo '<div id="preview_before" class="cf_widget_preview"><p>' . __('Resize this block to preview various widget sizes.', 'contestfriend') . '</p>';
     $url = add_query_arg('contestfriend_preview', $this->contest->ID, get_site_url());
     echo cf_Widget::html(array('contest' => $this->contest->ID, 'preview' => 'before_submit'));
     echo '</div>';
     $widget = cf_Widget::current_widget();
     $widget_id_before = $widget->widget_id;
     echo '<h3>' . __('After submit preview:', 'contestfriend') . '</h3>';
     echo '<div id="preview_after" class="cf_widget_preview"><p>' . __('Resize this block to preview various widget sizes.', 'contestfriend') . '</p>';
     $url = add_query_arg('contestfriend_preview', $this->contest->ID, get_site_url());
     echo cf_Widget::html(array('contest' => $this->contest->ID, 'preview' => 'after_submit'));
     echo '</div>';
     $widget = cf_Widget::current_widget();
     $widget_id_after = $widget->widget_id;
     if ($this->contest->cf_double_optin == '1') {
         echo '<div id="preview_doubleoptin" class="cf_widget_preview"><p>' . __('Resize this block to preview various widget sizes.', 'contestfriend') . '</p>';
         $url = add_query_arg('contestfriend_preview', $this->contest->ID, get_site_url());
         echo cf_Widget::html(array('contest' => $this->contest->ID, 'preview' => 'doubleoptin'));
         echo '</div>';
         $widget = cf_Widget::current_widget();
         $widget_id_doubleoptin = $widget->widget_id;
     }
     echo '<script type="text/javascript">jQuery(document).ready(function() { 
     jQuery(\'#preview_before\').resizable({ resize: function(event, ui) { ui.size.height = jQuery(\'#' . $widget_id_before . '\').height()+50; } }); 
     jQuery(\'#preview_after\').resizable({ resize: function(event, ui) { ui.size.height = jQuery(\'#' . $widget_id_after . '\').height()+50; } });';
     if ($this->contest->cf_double_optin == '1') {
         echo 'jQuery(\'#preview_doubleoptin\').resizable({ resize: function(event, ui) { ui.size.height = jQuery(\'#' . $widget_id_doubleoptin . '\').height()+50; } });';
     }
     echo '});</script>';
 }
 /**
  * Initialize method, called on <i>init</i> action.
  * <ul>
  * <li>setting up all ajax hooks</li>
  * <li>adding shortcodes</li>
  * <li>registering post types</li>
  * <li>registering textdomain for i18n</li>
  * <li>registering and enqueueing styles and scripts</li>
  * <li>catching and processing contest double-optin based on $_GET parameters</li>
  * </ul>
  */
 function init()
 {
     // ajax hooks
     add_action('wp_ajax_contestfriend_process', array('cf_Widget', 'process'));
     add_action('wp_ajax_nopriv_contestfriend_process', array('cf_Widget', 'process'));
     add_action('wp_ajax_cf_mailchimp_list', array('cf_Page_Contest', 'field_mailchimp_list_ajax'));
     add_action('wp_ajax_cf_campaignmonitor_client', array('cf_Page_Contest', 'field_campaignmonitor_client_ajax'));
     add_action('wp_ajax_cf_campaignmonitor_list', array('cf_Page_Contest', 'field_campaignmonitor_list_ajax'));
     add_action('wp_ajax_cf_aweber_code', array('cf_Page_Contest', 'field_aweber_code_ajax'));
     add_action('wp_ajax_cf_aweber_list', array('cf_Page_Contest', 'field_aweber_list_ajax'));
     add_action('wp_ajax_cf_getresponse_list', array('cf_Page_Contest', 'field_getresponse_list_ajax'));
     // register shortcode
     add_shortcode('contestfriend', array('cf_Widget', 'html'));
     // register post type
     $array = array('public' => false, 'exclude_from_search' => true);
     register_post_type(self::post_type, $array);
     // register i18n domain
     load_plugin_textdomain('contestfriend', false, dirname(plugin_basename(__FILE__)) . '/languages/');
     // register styles & scripts
     wp_enqueue_script('jquery');
     wp_register_style('cf_css_base', self::$plugin_url . '/css/cf_base.css');
     wp_enqueue_style('cf_css_base');
     wp_register_style('cf_css_jquery_ui', self::$plugin_url . '/css/jquery-ui-1.9.1.custom.min.css');
     wp_register_script('cf_js_widget', self::$plugin_url . '/js/cf_widget.js', array('jquery'));
     wp_register_script('cf_js_datetimepicker', self::$plugin_url . '/js/jquery-ui-timepicker-addon.min.js', array('jquery', 'jquery-ui-core', 'jquery-ui-slider', 'jquery-ui-datepicker'));
     // catch and process double optin
     if (isset($_GET['contestfriend_confirm'])) {
         cf_Widget::process_optin($_GET['contestfriend_confirm']);
         $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"];
         $url = remove_query_arg('contestfriend_confirm', $url);
         wp_safe_redirect($url);
         die;
     }
 }
    echo $media . $description;
}
echo '<div class="cf_actions cf_clear" style="border-top-color: ' . $border_color . '">
    <div class="cf_actions_inner">';
if (!$contest->is_expired() && $contest->is_started()) {
    if (!empty($participant) && $participant->status != 'not_confirmed') {
        echo cf_Widget::get_template('actions_submit');
    } else {
        if (!empty($participant) && $participant->status == 'not_confirmed') {
            echo cf_Widget::get_template('double_optin');
        } else {
            echo cf_Widget::get_template('actions');
        }
    }
    if ($contest->cf_countdown_field == '1') {
        echo cf_Widget::get_template('countdown');
    }
    if (!empty($participant) && $participant->status != 'not_confirmed') {
        echo '<div class="cf_contact_message">' . __('Winner(s) will be contacted by email.', 'contestfriend') . '</div>';
    }
} else {
    if (!$contest->is_started()) {
        echo '<div class="cf_error">' . __('Contest has not yet started.', 'contestfriend') . '</div>';
    } else {
        echo '<div class="cf_error">' . __('This contest expired.', 'contestfriend') . '</div>';
    }
}
echo '</div></div>
<div class="cf_footer">
        <span class="cf_rules_disclaimer">';
if ($contest->cf_disclaimer_rules_type != 'none') {
 /**
  * Main contest widget process method.
  * Called by AJAX script on contest registration form submit.
  */
 public static function process()
 {
     global $wpdb;
     // check for valid contest
     $contest = new cf_Contest(intval($_POST['contest_id']));
     if ($contest->_valid == false) {
         echo '<div class="error">' . __('Invalid contest', 'contestfriend') . '</div>';
         die;
     }
     // get POST data
     $widget_id = esc_attr($_POST['div_id']);
     $url = urldecode($_POST['url']);
     $email = esc_sql($_POST['email']);
     $first_name = '';
     if (!empty($_POST['first_name'])) {
         $first_name = esc_sql($_POST['first_name']);
     }
     $last_name = '';
     if (!empty($_POST['last_name'])) {
         $last_name = esc_sql($_POST['last_name']);
     }
     $ref = '';
     if (!empty($_POST['cf_ref'])) {
         $ref = esc_sql($_POST['cf_ref']);
     }
     $do_process = true;
     // flag to indicate invalid input ~ do not process any data, just print the widget
     // init widget data
     $data = array();
     $data['contest'] =& $contest;
     $data['widget_id'] = $widget_id;
     $data['url'] = $url;
     $data['ref'] = $ref;
     $data['participant'] = '';
     $data['error'] = array();
     $data['status'] = '';
     // if the contest did not start yet or has already ended, just echo the original widget (started/expired error messages are handled in the template)
     if (!$contest->is_started() || $contest->is_expired()) {
         self::$current_widget = new cf_Widget($data);
         echo self::get_template('widget');
         die;
     }
     // validate email
     $pattern = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$/';
     if (preg_match($pattern, $email) != 1) {
         $data['error']['email'] = 1;
         $do_process = false;
     }
     // validate names
     if ($contest->cf_name_field == '1' && $contest->cf_name_field_req == '1') {
         if (empty($first_name)) {
             $data['error']['first_name'] = 1;
             $do_process = false;
         }
         if (empty($last_name)) {
             $data['error']['last_name'] = 1;
             $do_process = false;
         }
     }
     if ($do_process) {
         $double_optin = false;
         if ($contest->cf_double_optin == '1') {
             $double_optin = true;
         }
         // prepare participant data
         $participant_data = array('ip' => $_SERVER['REMOTE_ADDR'], 'date_gmt' => current_time('mysql', 1), 'contest_id' => $contest->ID, 'code' => cf_Participant::generate_code(), 'email' => $email, 'status' => '');
         if ($double_optin) {
             $participant_data['status'] = 'not_confirmed';
         }
         $participant = new cf_Participant($participant_data);
         $participant_id = $participant->exists();
         if ($participant_id === false) {
             $participant_id = $participant->add();
             if ($contest->cf_name_field == '1') {
                 $participant->add_meta('first_name', $first_name);
                 $participant->add_meta('last_name', $last_name);
             }
             if ($contest->cf_referral_field == '1') {
                 $ref_url = add_query_arg($contest->ref_variable, $participant->code, $url);
                 $ref_url_short = self::process_shorten_url($contest, $participant, $ref_url);
                 if (!empty($ref_url_short)) {
                     $participant->add_meta('short_ref', $ref_url_short);
                 }
             }
             if (!empty($ref) && $contest->cf_referral_field == '1') {
                 if (!$double_optin) {
                     $referral = new cf_Participant($ref, true);
                     if ($referral->_valid && !empty($participant_id)) {
                         $referral->add_meta('referral_to', $participant_id);
                     }
                     // store that participant was referred by $referral
                 } else {
                     // if is double-optin, can't store referral until participant confirms his email
                     $participant->add_meta('tmp_referral', $ref);
                 }
             }
             if (!$double_optin) {
                 // process stuff
                 self::process_email($contest, $participant);
                 self::process_integration($contest, $participant);
                 self::process_cookie($contest, $participant);
             } else {
                 // if double_optin, process different stuff
                 self::process_optin_email($contest, $participant, $url);
             }
         } else {
             $participant = new cf_Participant($participant_id);
             if ($participant->status != 'not_confirmed') {
                 self::process_cookie($contest, $participant);
                 // reset his cookie
             } else {
                 // if existing participant is not_confirmed (did not confirm the double_optin)
                 self::process_optin_email($contest, $participant, $url);
                 // resend the confirmation email
             }
         }
     }
     $data['participant'] = $participant;
     self::$current_widget = new cf_Widget($data);
     echo self::get_template('widget');
     die;
 }