function quick_pick_team($title, $country) { $team = \WP_Clanwars\Teams::get_team(array('title' => $title, 'limit' => 1)); $team_id = 0; if (empty($team)) { $new_team_id = \WP_Clanwars\Teams::add_team(array('title' => $title, 'country' => $country)); if ($new_team_id !== false) { $team_id = $new_team_id; } } else { $team_id = $team[0]->id; } return $team_id; }
static function get_match($args) { global $wpdb; $defaults = array('from_date' => 0, 'id' => false, 'game_id' => false, 'sum_tickets' => false, 'limit' => 0, 'offset' => 0, 'orderby' => 'id', 'order' => 'asc'); $args = \WP_Clanwars\Utils::extract_args($args, $defaults); extract($args); $where_query = ''; $limit_query = ''; $order_query = ''; $order = strtolower($order); if ($order != 'asc' && $order != 'desc') { $order = 'asc'; } $order_query = 'ORDER BY t1.`' . $orderby . '` ' . $order; if ($id != 'all' && $id !== false) { if (!is_array($id)) { $id = array($id); } $id = array_map('intval', $id); $where_query[] = 't1.id IN (' . join(', ', $id) . ')'; } if ($game_id != 'all' && $game_id !== false) { if (!is_array($game_id)) { $game_id = array($game_id); } $game_id = array_map('intval', $game_id); $where_query[] = 't1.game_id IN (' . join(', ', $game_id) . ')'; } if ($from_date > 0) { $where_query[] = 't1.date >= FROM_UNIXTIME(' . intval($from_date) . ')'; } if ($limit > 0) { $limit_query = $wpdb->prepare('LIMIT %d, %d', $offset, $limit); } if (!empty($where_query)) { $where_query = 'WHERE ' . join(' AND ', $where_query); } $rounds_table = \WP_Clanwars\Rounds::table(); $games_table = \WP_Clanwars\Games::table(); $teams_table = \WP_Clanwars\Teams::table(); $matches_table = static::table(); if ($sum_tickets) { $subquery = <<<SQL \t \t\t\t( \t\t\t\tSELECT SUM(sumt1.tickets1) \t\t\t\tFROM `{$rounds_table}` AS sumt1 \t\t\t\tWHERE sumt1.match_id = t1.id \t\t\t) AS team1_tickets, \t\t\t( \t\t\t\tSELECT SUM(sumt2.tickets2) \t\t\t\tFROM `{$rounds_table}` AS sumt2 \t\t\t\tWHERE sumt2.match_id = t1.id \t\t\t) AS team2_tickets SQL; $subquery = trim($subquery); } else { $subquery = 'NULL'; } $query = <<<SQL \t\tSELECT \t\t\tt1.*, \t\t\tt2.title AS game_title, \t\t\tt2.abbr AS game_abbr, \t\t\tt2.icon AS game_icon, \t\t\ttt1.title AS team1_title, \t\t\ttt2.title AS team2_title, \t\t\ttt1.country AS team1_country, \t\t\ttt2.country AS team2_country, \t\t\t{$subquery} \t\tFROM `{$matches_table}` AS t1 \t\t\tLEFT JOIN `{$games_table}` AS t2 ON t1.game_id = t2.id \t\t\tLEFT JOIN `{$teams_table}` AS tt1 ON t1.team1 = tt1.id \t\t\tLEFT JOIN `{$teams_table}` AS tt2 ON t1.team2 = tt2.id \t\t{$where_query} \t\t{$order_query} \t\t{$limit_query} SQL; return \WP_Clanwars\DB::get_results($query); }
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; }
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'] : 'id'; $order = isset($_REQUEST['order']) ? $_REQUEST['order'] : 'desc'; $offset = ($current_page - 1) * $per_page; $limit = $per_page; $args = array('id' => 'all', 'order' => $order, 'orderby' => $orderby, 'order' => $order, 'limit' => $limit, 'offset' => $limit * ($current_page - 1)); $teams = \WP_Clanwars\Teams::get_team($args); $pagination = $teams->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 = $teams->getArrayCopy(); }
static function html_country_select_helper($p = array(), $print = true) { $all_countries = self::all_countries(); extract(\WP_Clanwars\Utils::extract_args($p, array('select' => '', 'name' => '', 'id' => '', 'class' => '', 'show_popular' => false))); ob_start(); $attrs = array(); if (!empty($id)) { $attrs[] = 'id="' . esc_attr($id) . '"'; } if (!empty($name)) { $attrs[] = 'name="' . esc_attr($name) . '"'; } if (!empty($class)) { $attrs[] = 'class="' . esc_attr($class) . '"'; } $attrstr = implode(' ', $attrs); if (!empty($attrstr)) { $attrstr = ' ' . $attrstr; } echo '<select' . $attrstr . '>'; if ($show_popular) { $popular = \WP_Clanwars\Teams::most_popular_countries(); if (!empty($popular)) { foreach ($popular as $i => $data) { $abbr = $data['country']; $title = isset($all_countries[$abbr]) ? $all_countries[$abbr] : $abbr; echo '<option value="' . esc_attr($abbr) . '">' . esc_html($title) . '</option>'; } echo '<optgroup label="-----------------" style="font-family: monospace;"></optgroup>'; } } // copy array with array_merge so we don't sort global array $sorted_countries = array_merge(array(), $all_countries); asort($sorted_countries); foreach ($sorted_countries as $abbr => $title) { echo '<option value="' . esc_attr($abbr) . '"' . selected($abbr, $select, false) . '>' . esc_html($title) . '</option>'; } echo '</select>'; $output = ob_get_clean(); if ($print) { echo $output; return; } return $output; }