/** * Generates columns. * * @param CH_Contest $item Current contest. * @param string $column_name Current column text ID. */ function column_default($item, $column_name) { switch ($column_name) { case 'title': $headline = isset($item->ch_headline) ? esc_html($item->ch_headline) : ''; $output = '<b>' . esc_html($item->ID) . ':</b> <a href="' . admin_url('admin.php?page=' . CH_Page_Contest::page_id . '&contest=' . $item->ID) . '">' . $headline . '</a>'; $actions = array(); $actions['edit'] = '<a href="' . admin_url('admin.php?page=' . CH_Page_Contest::page_id . '&contest=' . $item->ID) . '">' . __('Edit') . '</a>'; $actions['delete'] = '<a class="confirm_delete" href="' . admin_url('admin.php?page=' . CH_Page_List::page_id . '&action=del&contest=' . $item->ID) . '">' . __('Delete') . '</a>'; $actions['dashboard'] = '<a href="' . admin_url('admin.php?page=' . CH_Page_Contest::page_id . '&contest=' . $item->ID . '&ch_page=dashboard') . '">' . __('Dashboard', 'contesthopper') . '</a>'; $actions['participants'] = '<a href="' . admin_url('admin.php?page=' . CH_Page_Participants::page_id . '&contest=' . $item->ID) . '">' . __('Participants', 'contesthopper') . '</a>'; $output .= $this->row_actions($actions); return $output; case 'participants': $part_num = CH_Participant::get_num($item->ID); $part_num_all = CH_Participant::get_num($item->ID, 'all'); $participant_num = $part_num; if ($part_num_all != $part_num) { $participant_num .= ' (' . $part_num_all . ')'; } return $participant_num; case 'date_start': $date_start = isset($item->ch_date_start) ? $item->ch_date_start : ''; return $date_start; case 'duration': $date_start = isset($item->ch_date_start) ? $item->ch_date_start : ''; $date_end = isset($item->ch_date_end) ? $item->ch_date_end : ''; $start_time = strtotime($date_start); $end_time = strtotime($date_end); $duration = $end_time - $start_time; $duration_text = ''; if ($duration < 3600 * 24) { $hrs = round($duration / 3600); $duration_text = $hrs . ' hour(s)'; } else { $days = intval(floor($duration / 86400)); $hrs = round(($duration - $days * 86400) / 3600); $duration_text = $days . ' day(s), ' . $hrs . ' hour(s)'; } $duration_text .= ' (ends ' . $item->ch_date_end . ')'; return $duration_text; case 'status': $status = isset($item->ch_status) ? $item->ch_status : ''; $isexpired = $item->is_expired(); $isstarted = $item->is_started(); $contest_status = __('Expired', 'contesthopper'); if (!$isstarted) { $contest_status = __('Not active yet', 'contesthopper'); } else { if ($isexpired && $item->ch_status == 'expired') { $contest_status = __('Expired (forced)', 'contesthopper'); } else { if ($isexpired && $item->ch_status == 'active') { $contest_status = __('Expired (automatic)', 'contesthopper'); } else { if ($isexpired && $item->ch_status == 'winners_picked') { $contest_status = __('Expired (winners picked)', 'contesthopper'); } else { if ($isstarted && !$isexpired) { $contest_status = __('Active', 'contesthopper'); } } } } } return $contest_status; default: return ''; } }
/** * Generates custom content for dashboard tab. */ protected function generate_dashboard() { $contest_status = ''; $isstarted = $this->contest->is_started(); $isexpired = $this->contest->is_expired(); $contest_status = __('Expired', 'contesthopper'); if (!$isstarted) { $contest_status = __('Not active yet', 'contesthopper'); } else { if ($isexpired && $this->contest->ch_status == 'expired') { $contest_status = __('Expired (forced)', 'contesthopper'); } else { if ($isexpired && $this->contest->ch_status == 'active') { $contest_status = __('Expired (automatic)', 'contesthopper'); } else { if ($isexpired && $this->contest->ch_status == 'winners_picked') { $contest_status = __('Expired (winners picked)', 'contesthopper'); } else { if ($isstarted && !$isexpired) { $contest_status = __('Active', 'contesthopper'); } } } } } $part_num = CH_Participant::get_num($this->contest->ID); $part_num_all = CH_Participant::get_num($this->contest->ID, 'all'); $participant_num = $part_num; if ($part_num_all != $part_num) { $participant_num .= ' (' . $part_num_all . ')'; } echo '<div style="margin-top: 5px;">'; // Dashboard buttons if (!$this->contest->is_expired() || $this->contest->ch_status == 'expired') { // display stop / resume contest $url = admin_url('admin.php?page=' . CH_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest'); echo '<form action="' . $url . '" method="post" style="float: left; margin-left: 5px"><input type="hidden" name="setexpired" value="1" />'; $text = __('Stop Contest', 'contesthopper'); if ($this->contest->ch_status == 'expired') { $text = __('Resume Contest', 'contesthopper'); } submit_button($text, 'button-secondary action', false, false); echo '</form>'; } if ($this->contest->is_expired() && $this->contest->ch_status == 'winners_picked') { // display reset contest button $url = admin_url('admin.php?page=' . CH_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest'); echo '<form action="' . $url . '" method="post" style="float: left; margin-left: 5px"><input type="hidden" name="resetcontest" value="1" />'; $text = __('Reset Contest', 'contesthopper'); submit_button($text, 'button-secondary action', false, false); echo '</form>'; } // if not all winners were picked, show pick buttons $winners_num = 1; $current_winners = $this->contest->get_current_winner_num(); if (isset($this->contest->ch_winners_num) && is_numeric($this->contest->ch_winners_num) && $this->contest->ch_winners_num > 0) { $winners_num = $this->contest->ch_winners_num; } if ($current_winners < $winners_num) { $confirm_class = ''; if ($current_winners == 0 && $this->contest->ch_status != 'winners_picked') { $confirm_class = 'confirm_setwinner'; } $text = __('Pick Random Winners', 'contesthopper'); $url = admin_url('admin.php?page=' . CH_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest'); echo '<form class="' . $confirm_class . '" action="' . $url . '" method="post" style="float: left; margin-left: 5px"><input type="hidden" name="pickwinners" value="1" />'; submit_button($text, 'button-secondary action', false, false); echo '</form>'; $text = __('Manually Pick Winners', 'contesthopper'); echo '<form action="' . $url . '" method="post" style="float: left; margin-left: 5px">'; submit_button($text, 'button-secondary action', false, false); echo '</form>'; } // if there are some winners picked, show clear winners button if ($current_winners > 0) { $url = admin_url('admin.php?page=' . CH_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest'); $text = __('Clear All Winners', 'contesthopper'); echo '<form action="' . $url . '" method="post" style="float: left; margin-left: 5px"><input type="hidden" name="clearwinners" value="1" />'; submit_button($text, 'button-secondary action', false, false); echo '</a></form>'; } echo '</div> <div id="poststuff" style="clear: both;"> <div class="postbox"> <h3>' . __('Contest Status', 'contesthopper') . '</h3> <div class="inside"> <table class="form-table"> <tbody><tr valign="top"><th class="ch_label">' . __('Status', 'contesthopper') . '</th> <td>' . $contest_status . '</td> </tr> <tr valign="top"><th class="ch_label" style="vertical-align: middle">' . __('Number of participants', 'contesthopper') . '</th> <td style="vertical-align: middle">' . $participant_num . ' <a href="' . admin_url('admin.php?page=' . CH_Page_Participants::page_id . '&contest=' . $_GET['contest']) . '" class="button">' . __('View all participants', 'contesthopper') . '</a></td> </tr> </table> </div></div>'; if ($current_winners > 0) { $winners = CH_Participant::get_all($this->contest->ID, '', '', '', '', 'winner'); echo '<div class="postbox"> <h3>' . __('Winners', 'contesthopper') . '</h3> <div class="inside"> <table class="form-table"> <tbody>'; foreach ($winners as $winner) { $url = admin_url('admin.php?page=' . CH_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest'); $text = __('Remove Winner', 'contesthopper'); echo '<tr valign="top"><th class="ch_label">' . $winner->email . ', ' . $winner->first_name . ' ' . $winner->last_name . '</th><td><form action="' . $url . '" method="post"><input type="hidden" name="removewinner" value="' . esc_attr($winner->id) . '" />'; submit_button($text, 'button-secondary action', false, false); echo '</td></tr>'; } echo ' </table> </div> </div> '; } echo '</div>'; }
/** * 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 CH_Contest(intval($_POST['contest_id'])); if ($contest->_valid == false) { echo '<div class="error">' . __('Invalid contest', 'contesthopper') . '</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['ch_ref'])) { $ref = esc_sql($_POST['ch_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 CH_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->ch_name_field == '1' && $contest->ch_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->ch_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' => CH_Participant::generate_code(), 'email' => $email, 'status' => ''); if ($double_optin) { $participant_data['status'] = 'not_confirmed'; } $participant = new CH_Participant($participant_data); $participant_id = $participant->exists(); if ($participant_id === false) { $participant_id = $participant->add(); if ($contest->ch_name_field == '1') { $participant->add_meta('first_name', $first_name); $participant->add_meta('last_name', $last_name); } if ($contest->ch_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->ch_referral_field == '1') { if (!$double_optin) { $referral = new CH_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 CH_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 CH_Widget($data); echo self::get_template('widget'); die; }