function widget($args, $instance) { wp_enqueue_script('wp-clanwars-tabs'); extract($args); $now = Utils::current_time_fixed('timestamp'); $instance = wp_parse_args((array) $instance, $this->default_settings); $title = apply_filters('widget_title', empty($instance['title']) ? __('ClanWars', WP_CLANWARS_TEXTDOMAIN) : $instance['title']); $matches = array(); $games = array(); $options = array('id' => empty($instance['visible_games']) ? 'all' : $instance['visible_games'], 'orderby' => 'title', 'order' => 'asc'); $_games = \WP_Clanwars\Games::get_game($options); $from_date = 0; if (isset($this->newer_than_options[$instance['hide_older_than']])) { $age = (int) $this->newer_than_options[$instance['hide_older_than']]['value']; if ($instance['hide_older_than'] === 'custom') { // custom $age = (int) $instance['custom_hide_duration'] * self::ONE_DAY; if ($age > 0) { $from_date = $now - $age; } } else { if ($age > 0) { // 0 means show all matches $from_date = $now - $age; } } } foreach ($_games as $game) { $options = array('from_date' => $from_date, 'game_id' => $game->id, 'limit' => $instance['show_limit'], 'order' => 'desc', 'orderby' => 'date', 'sum_tickets' => true); $matchResult = \WP_Clanwars\Matches::get_match($options); if ($matchResult->count()) { $games[] = $game; $matches = array_merge($matches, $matchResult->getArrayCopy()); } } usort($matches, array($this, '_sort_games')); ?> <?php echo $before_widget; ?> <?php if ($title && !$instance['hide_title']) { echo $before_title . $title . $after_title; } ?> <ul class="clanwar-list<?php if ($instance['display_game_icon']) { echo ' shows-game-icon'; } ?> "> <?php if (sizeof($games) > 1) { ?> <li> <ul class="tabs"> <?php $obj = new stdClass(); $obj->id = 0; $obj->title = __('All', WP_CLANWARS_TEXTDOMAIN); $obj->abbr = __('All'); $obj->icon = 0; array_unshift($games, $obj); for ($i = 0; $i < sizeof($games); $i++) { $game = $games[$i]; $link = $game->id == 0 ? 'all' : 'game-' . $game->id; ?> <li<?php if ($i == 0) { echo ' class="selected"'; } ?> ><a href="#<?php echo $link; ?> " title="<?php echo esc_attr($game->title); ?> "><?php echo esc_html($game->abbr); ?> </a></li> <?php } ?> </ul> </li> <?php } ?> <?php foreach ($matches as $i => $match) { $is_upcoming = false; $t1 = $match->team1_tickets; $t2 = $match->team2_tickets; $wld_class = $t1 == $t2 ? 'draw' : ($t1 > $t2 ? 'win' : 'loss'); $date = mysql2date(get_option('date_format') . ', ' . get_option('time_format'), $match->date); $timestamp = mysql2date('U', $match->date); $game_icon = wp_get_attachment_url($match->game_icon); $is_upcoming = $timestamp > $now; $is_playing = $now > $timestamp && $now < $timestamp + 3600 && ($t1 == 0 && $t2 == 0); $item_classes = ['clanwar-item', 'game-' . $match->game_id]; if ($i % 2 != 0) { $item_classes[] = 'alt'; } ?> <li class="<?php esc_attr_e(join(' ', $item_classes)); ?> "> <?php if ($is_upcoming) { ?> <div class="upcoming"><?php _e('Upcoming', WP_CLANWARS_TEXTDOMAIN); ?> </div> <?php } elseif ($is_playing) { ?> <div class="playing"><?php _e('Playing', WP_CLANWARS_TEXTDOMAIN); ?> </div> <?php } else { ?> <div class="scores <?php echo $wld_class; ?> "><?php echo sprintf(__('%d:%d', WP_CLANWARS_TEXTDOMAIN), $t1, $t2); ?> </div> <?php } ?> <div class="opponent-team"> <?php if ($instance['display_game_icon'] && $game_icon !== false) { ?> <img src="<?php echo $game_icon; ?> " alt="<?php echo esc_attr($match->game_title); ?> " class="icon" /> <?php } ?> <?php $team1_flag = \WP_Clanwars\Utils::get_country_flag($match->team1_country); $team2_flag = \WP_Clanwars\Utils::get_country_flag($match->team2_country); if ($instance['display_both_teams']) { $team_title = sprintf('%s %s vs. %s %s', $team1_flag, esc_html($match->team1_title), $team2_flag, esc_html($match->team2_title)); } else { $team_title = sprintf('%s %s', $team2_flag, esc_html($match->team2_title)); } if ($match->post_id != 0) { echo sprintf('<a href="%s" title="%s">%s</a>', get_permalink($match->post_id), esc_attr($match->title), $team_title); } else { echo $team_title; } ?> </div> <div class="date"><?php echo $date; ?> </div> </li> <?php } ?> </ul> <?php echo $after_widget; ?> <?php }
function prepare_items() { $per_page = $this->get_items_per_page(static::PER_PAGE_OPTION, static::PER_PAGE_DEFAULT); $current_page = $this->get_pagenum(); $orderby = isset($_REQUEST['orderby']) ? $_REQUEST['orderby'] : 'date'; $order = isset($_REQUEST['order']) ? $_REQUEST['order'] : 'desc'; $offset = ($current_page - 1) * $per_page; $limit = $per_page; $game_filter = \WP_Clanwars\ACL::user_can('which_games'); $args = array('id' => 'all', 'game_id' => $game_filter, 'sum_tickets' => true, 'orderby' => $orderby, 'order' => $order, 'limit' => $limit, 'offset' => $limit * ($current_page - 1)); $matches = \WP_Clanwars\Matches::get_match($args); $pagination = $matches->get_pagination(); $this->set_pagination_args(array('total_pages' => $pagination->get_num_pages(), 'total_items' => $pagination->get_num_rows(), 'per_page' => $per_page)); $this->items = $matches->getArrayCopy(); }
static function delete_game($id) { global $wpdb; $table = self::table(); if (!is_array($id)) { $id = array($id); } $id = array_map('intval', $id); \WP_Clanwars\Maps::delete_map_by_game($id); \WP_Clanwars\Matches::delete_match_by_game($id); $id_list = implode(',', $id); return $wpdb->query("DELETE FROM `{$table}` WHERE id IN({$id_list})"); }
function on_manage_matches() { $act = isset($_GET['act']) ? $_GET['act'] : ''; $current_page = isset($_GET['paged']) ? $_GET['paged'] : 1; $limit = 10; $game_filter = \WP_Clanwars\ACL::user_can('which_games'); if ($act === 'add') { return $this->on_add_match(); } else { if ($act === 'edit') { return $this->on_edit_match(); } } $condition = array('id' => 'all', 'game_id' => $game_filter, 'sum_tickets' => true, 'orderby' => 'date', 'order' => 'desc', 'limit' => $limit, 'offset' => $limit * ($current_page - 1)); $matches = \WP_Clanwars\Matches::get_match($condition); $pagination = $matches->get_pagination(); $match_statuses = $this->match_status; // populate games with urls for icons foreach ($matches as $match) { $match->game_icon_url = wp_get_attachment_url($match->game_icon); } $page_links = paginate_links(array('base' => add_query_arg('paged', '%#%'), 'format' => '', 'prev_text' => __('«'), 'next_text' => __('»'), 'total' => $pagination->get_num_pages(), 'current' => $current_page)); $wp_list_table = $this->match_table; $view = new View('match_table'); $view->add_helper('print_table_header', array($this, 'print_table_header')); $view->add_helper('get_country_flag', array('\\WP_Clanwars\\Utils', 'get_country_flag')); $context = compact('table_columns', 'page_links_text', 'matches', 'match_statuses', 'wp_list_table'); $view->render($context); }
static function most_popular_countries() { global $wpdb; static $cache = false; $limit = 10; if ($cache !== false) { return $cache; } $teams_table = \WP_Clanwars\Teams::table(); $matches_table = \WP_Clanwars\Matches::table(); $query = $wpdb->prepare("(SELECT t1.country, COUNT(t2.id) AS cnt\n\t\t\tFROM {$teams_table} AS t1, {$matches_table} AS t2\n\t\t\tWHERE t1.id = t2.team1\n\t\t\tGROUP BY t1.country\n\t\t\tLIMIT %d)\n\t\t\tUNION\n\t\t\t(SELECT t1.country, COUNT(t2.id) AS cnt\n\t\t\tFROM {$teams_table} AS t1, {$matches_table} AS t2\n\t\t\tWHERE t1.id = t2.team2\n\t\t\tGROUP BY t1.country\n\t\t\tLIMIT %d)\n\t\t\tORDER BY cnt DESC\n\t\t\tLIMIT %d", $limit, $limit, $limit); $cache = $wpdb->get_results($query, ARRAY_A); return $cache; }