/** * Init method, called when accessing the page. */ function init() { add_action('admin_footer', array(&$this, 'list_scripts')); if (isset($_GET['action']) && isset($_GET['contest']) && $_GET['action'] == 'del') { $contest = new CH_Contest($_GET['contest']); if ($contest->_valid) { $contest->delete(true); } wp_safe_redirect(admin_url('admin.php?page=' . self::page_id)); } }
/** * 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>'; }
/** * Contest autoexpiry method. * Called on <i>contesthopper_autoexpiry</i> hook, that was registered by <i>wp_schedule_event()</i> on plugin install. * Method checks for active contests that have <i>ch_autoselect_winner</i> attribute set, calls * contest object methods to pick the desired number of winners and sets the contest to <i>winners_picked</i> * status. */ function process_autoexpiry() { $contests = CH_Contest::get_all('', '', '', '', 'active'); foreach ($contests as $contest) { if ($contest->is_expired()) { if (isset($contest->ch_autoselect_winner) && $contest->ch_autoselect_winner == '1') { $winners_num = 1; if (isset($contest->ch_winners_num) && is_numeric($contest->ch_winners_num) && $contest->ch_winners_num > 0) { $winners_num = $contest->ch_winners_num; } $current_winners = $contest->get_current_winner_num(); $pick_num = intval($winners_num) - intval($current_winners); if ($pick_num > 0) { $contest->pick_winners($pick_num); } $contest->set_status('winners_picked'); } } } }
/** * Init method, called when accessing the page. Handles processing of participant and contest $_POST and $_GET requests. */ function init() { $contest = ''; if (isset($_GET['contest'])) { $contest = $_GET['contest']; } if (isset($_GET['export'])) { CH_Table_Participants::export_csv($contest); } if (isset($_GET['togglevalid'])) { $participant = new CH_Participant($_GET['togglevalid']); if ($participant->_valid) { if ($participant->status == 'not_valid') { $participant->set_status(''); } else { if ($participant->status != 'not_confirmed') { $participant->set_status('not_valid'); } } } $url = 'admin.php?page=' . self::page_id; if (isset($_GET['contest'])) { $url .= '&contest=' . $_GET['contest']; } wp_safe_redirect(admin_url($url)); die; } if (isset($_GET['del'])) { $participant = new CH_Participant($_GET['del']); if ($participant->_valid) { $participant->delete(); } $url = 'admin.php?page=' . self::page_id; if (isset($_GET['contest'])) { $url .= '&contest=' . $_GET['contest']; } wp_safe_redirect(admin_url($url)); die; } if (isset($_GET['contest'])) { $contest = new CH_Contest($_GET['contest']); if ($contest->_valid != true) { wp_safe_redirect(admin_url('admin.php?page=' . CH_Page_Participants::page_id)); die; } if (isset($_POST['resetcontest'])) { if ($contest->ch_status == 'winners_picked') { $contest->set_status('active'); $contest->clear_winners(); } if (isset($_GET['redirect']) && $_GET['redirect'] == 'contest') { wp_safe_redirect(admin_url('admin.php?page=' . CH_Page_Contest::page_id . '&contest=' . $contest->ID . '&ch_page=dashboard')); } else { wp_safe_redirect(admin_url('admin.php?page=' . self::page_id . '&contest=' . $contest->ID)); } die; } if (isset($_POST['setexpired'])) { if ($contest->ch_status == 'expired') { $contest->set_status('active'); } else { if ($contest->ch_status == 'active' || $contest->ch_status == '') { $contest->set_status('expired'); } } if (isset($_GET['redirect']) && $_GET['redirect'] == 'contest') { wp_safe_redirect(admin_url('admin.php?page=' . CH_Page_Contest::page_id . '&contest=' . $contest->ID . '&ch_page=dashboard')); } else { wp_safe_redirect(admin_url('admin.php?page=' . self::page_id . '&contest=' . $contest->ID)); } die; } if (isset($_POST['pickwinners'])) { $winners_num = 1; if (isset($contest->ch_winners_num) && is_numeric($contest->ch_winners_num) && $contest->ch_winners_num > 0) { $winners_num = $contest->ch_winners_num; } $current_winners = $contest->get_current_winner_num(); $pick_num = intval($winners_num) - intval($current_winners); if ($pick_num > 0) { $contest->pick_winners($pick_num); } if (isset($_GET['redirect']) && $_GET['redirect'] == 'contest') { wp_safe_redirect(admin_url('admin.php?page=' . CH_Page_Contest::page_id . '&contest=' . $contest->ID . '&ch_page=dashboard')); } else { wp_safe_redirect(admin_url('admin.php?page=' . self::page_id . '&contest=' . $contest->ID)); } die; } if (isset($_POST['clearwinners'])) { $contest->clear_winners(); if (isset($_GET['redirect']) && $_GET['redirect'] == 'contest') { wp_safe_redirect(admin_url('admin.php?page=' . CH_Page_Contest::page_id . '&contest=' . $contest->ID . '&ch_page=dashboard')); } else { wp_safe_redirect(admin_url('admin.php?page=' . self::page_id . '&contest=' . $contest->ID)); } die; } if (isset($_POST['pickwinner'])) { $winners_num = 1; if (isset($contest->ch_winners_num) && is_numeric($contest->ch_winners_num) && $contest->ch_winners_num > 0) { $winners_num = $contest->ch_winners_num; } if ($contest->get_current_winner_num() < $winners_num) { $participant = new CH_Participant($_POST['pickwinner']); if ($participant->_valid) { $participant->set_status('winner'); $contest->set_status('winners_picked'); } } if (isset($_GET['redirect']) && $_GET['redirect'] == 'contest') { wp_safe_redirect(admin_url('admin.php?page=' . CH_Page_Contest::page_id . '&contest=' . $contest->ID . '&ch_page=dashboard')); } else { wp_safe_redirect(admin_url('admin.php?page=' . self::page_id . '&contest=' . $contest->ID)); } die; } if (isset($_POST['removewinner'])) { $participant = new CH_Participant($_POST['removewinner']); if ($participant->_valid) { if (substr($participant->status, 0, 6) == 'winner') { $participant->set_status(''); } } if (isset($_GET['redirect']) && $_GET['redirect'] == 'contest') { wp_safe_redirect(admin_url('admin.php?page=' . CH_Page_Contest::page_id . '&contest=' . $contest->ID . '&ch_page=dashboard')); } else { wp_safe_redirect(admin_url('admin.php?page=' . self::page_id . '&contest=' . $contest->ID)); } die; } } add_action('admin_footer', array(&$this, 'list_scripts')); }
/** * 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 ''; } }
/** * Sets a cookie to remember the participant. * @param CH_Contest $contest * @param CH_Participant $participant */ static function process_cookie($contest, $participant) { $now = current_time('timestamp', 1); $end = $contest->get_utc_time(); $res = setcookie('contesthopper_' . $contest->ID, $participant->code, time() + ($end - $now), '/'); }
/** * Generates extra table navigation - contest filter. */ function extra_tablenav() { if (!isset($_GET['contest']) || empty($_GET['contest'])) { $selected = ' selected="selected"'; } echo '<form style="float: left;" action="admin.php" method="get"> <input type="hidden" name="page" value="' . CH_Page_Participants::page_id . '" /> <select name="contest">'; echo '<option' . $selected . ' value="">' . __('All Contests', 'contesthopper') . '</option>'; foreach ($this->extra_nav as $name => $title) { $selected = ''; if (isset($_GET['contest']) && $name == $_GET['contest']) { $selected = ' selected="selected"'; } echo '<option value="' . $name . '"' . $selected . '>' . $name . ': ' . $title . '</option>'; } echo '</select>'; submit_button(__('Apply'), 'button-secondary action', false, false); echo '</form>'; $url = add_query_arg('export', '1'); echo '<form action="' . $url . '" method="post" style="margin-left: 25px; float: left">'; submit_button(__('Export to CVS', 'contesthopper'), 'button-secondary action', false, false); echo '</a></form>'; if ($this->contest != '') { if (!$this->contest->is_expired() || $this->contest->ch_status == 'expired') { // display stop / resume contest $url = add_query_arg('setexpired', '1'); $url = admin_url('admin.php?page=' . CH_Page_Participants::page_id . '&contest=' . $_GET['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']); 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 random pick button $winners_num = 1; 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; } $confirm_class = ''; if ($this->current_winner_num == 0 && $this->contest->ch_status != 'winners_picked') { $confirm_class = 'confirm_setwinner'; } if ($this->current_winner_num < $winners_num) { $text = __('Pick Random Winners', 'contesthopper'); $url = admin_url('admin.php?page=' . CH_Page_Participants::page_id . '&contest=' . $_GET['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>'; } // if there are some winners picked, show clear winners button if ($this->current_winner_num > 0) { $url = admin_url('admin.php?page=' . CH_Page_Participants::page_id . '&contest=' . $_GET['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 '</form>'; } } }