array(
"name" => "Profile Only",
"game_template" => '',
"template" => '
'
),
"profile-small" => array(
"name" => "Profile Small",
"game_template" => '',
"template" => '
'
),
"profile-games" => array(
"name" => "Profile + Games",
"game_template" => '
',
"template" => '
'
),
"games" => array(
"name" => "Games Only",
"game_template" => '
',
"template" => '
%GAMES_TWOWEEKS%
'
),
"grid" => array(
"name" => "Games Grid",
"game_template" => '
',
"template" => '
'
),
"full" => array(
"name" => "Full-page Profile",
"game_template" => '
%GAME_HOURS_TWOWEEKS% hours / two weeks
',
"template" => '
'
)
);
//these are the widget-wide default settings
private $default_settings = array(
"title" => "Currently Playing",
"preset" => "games",
"game_template" => '', //set in constructor
"template" => '', //set in constructor
"steam_id" => "",
"cache_interval" => 900
);
//constructor
function __construct() {
$widget_ops = array('classname' => 'advanced_steam_widget', 'description' => "Displays Steam gaming statistics");
parent::WP_Widget(false, $name = 'Steam Widget', $widget_ops);
$this->default_settings["game_template"] = $this->presets[$this->default_settings["preset"]]["game_template"];
$this->default_settings["template"] = $this->presets[$this->default_settings["preset"]]["template"];
}
//overrides parent function
function widget($args, $instance) {
extract($args);
//next line for cache debug
//print "\n";
//see if we can use the cache or it's time to regenerate
if ((isset($instance["cache"])) && (is_array($instance["cache"])) && (($instance["last_cached"] + $instance["cache_interval"]) > time())) {
$steam_array = $instance["cache"];
print "";
} else { //if we did not successfully use the cache, then regenerate
//see if there's any id input
$steam_id = empty($instance['steam_id']) ? 'slserpent' : $instance['steam_id'];
//decide whether we're using old or new style profile url
if (preg_match('/\A\d{17}\Z/', $steam_id)) {
$profile_url = 'http://steamcommunity.com/profiles/' . $steam_id;
} else {
$profile_url = 'http://steamcommunity.com/id/' . $steam_id;
}
$xml_url = $profile_url . '?xml=1';
//first, make sure we have good XML from Valve
if (($steam_xml = $this->get_xml_from_steam($xml_url)) === false) {
//there was an error, so fallback to cache if available
if ((isset($instance["cache"])) && (is_array($instance["cache"]))) {
$steam_array = $instance["cache"];
print "";
} else return;
} else {
//parse out some values so they're easier to store / use
$steam_array = array();
$steam_array['username'] = (string)$steam_xml->steamID;
$steam_array['ID64'] = (string)$steam_xml->steamID64;
$steam_array['profile_url'] = $profile_url;
$steam_array['avatar']['icon'] = (string)$steam_xml->avatarIcon;
$steam_array['avatar']['medium'] = (string)$steam_xml->avatarMedium;
$steam_array['avatar']['large'] = (string)$steam_xml->avatarFull;
$steam_array['hours_twoweeks'] = (string)$steam_xml->hoursPlayed2Wk;
if ($steam_xml->onlineState == "in-game") {
$steam_array['ingame'] = (string)$steam_xml->inGameInfo->gameName;
} else $steam_array['ingame'] = false;
if ($steam_xml->onlineState == "online") {
$steam_array['online'] = true;
} else $steam_array['online'] = false;
if (count($steam_xml->mostPlayedGames->mostPlayedGame) > 0) {
$k = 0;
foreach ($steam_xml->mostPlayedGames->mostPlayedGame as $game) {
if (strlen($game->gameName) < 1) continue;
$steam_array['games'][$k]['name'] = (string)$game->gameName;
$steam_array['games'][$k]['url'] = (string)$game->gameLink;
$steam_array['games'][$k]['icon'] = (string)$game->gameIcon;
$steam_array['games'][$k]['logo']['small'] = (string)$game->gameLogoSmall;
$steam_array['games'][$k]['logo']['large'] = (string)$game->gameLogo;
$steam_array['games'][$k]['hours_total'] = (string)$game->hoursOnRecord;
$steam_array['games'][$k]['hours_twoweeks'] = (string)$game->hoursPlayed;
if ($steam_array['ingame'] && $steam_array['ingame'] == $steam_array['games'][$k]['name']) {
$steam_array['games'][$k]['ingame'] = true;
} else $steam_array['games'][$k]['ingame'] = false;
if ($game->statsName) {
$steam_array['games'][$k]['stats_url'] = $profile_url . "/stats/" . (string)$game->statsName;
} else $steam_array['games'][$k]['stats_url'] = false;
$k++;
}
}
//write the cache and reset timestamp
$this->internal_update(array("cache" => $steam_array, "last_cached" => time()));
print "";
}
}
//print the widget title before we get going
$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
print $before_widget;
if (!empty($title)) print $before_title . $title . $after_title;
//replace template patterns with steam data
if (count($steam_array['games']) > 0) {
foreach ($steam_array['games'] as $game) {
$game_output_tmp = $instance["game_template"];
//ingame conditional
if ($game['ingame']) {
$game_output_tmp = preg_replace('/IF_GAME_INGAME\{([^}]*)\}(?:ELSE\{([^}]*)\})?/i', '\1', $game_output_tmp);
} else {
$game_output_tmp = preg_replace('/IF_GAME_INGAME\{([^}]*)\}(?:ELSE\{([^}]*)\})?/i', '\2', $game_output_tmp);
}
//stats conditional
if ($game['stats_url']) {
$game_output_tmp = preg_replace('/IF_GAME_STATS\{([^}]*)\}(?:ELSE\{([^}]*)\})?/i', '\1', $game_output_tmp);
} else {
$game_output_tmp = preg_replace('/IF_GAME_STATS\{([^}]*)\}(?:ELSE\{([^}]*)\})?/i', '\2', $game_output_tmp);
}
$game_output_tmp = str_ireplace("%GAME_NAME%", $game['name'], $game_output_tmp);
$game_output_tmp = str_ireplace("%GAME_URL%", $game['url'], $game_output_tmp);
$game_output_tmp = str_ireplace("%GAME_ICON%", $game['icon'], $game_output_tmp);
$game_output_tmp = str_ireplace("%GAME_LOGO_SMALL%", $game['logo']['small'], $game_output_tmp);
$game_output_tmp = str_ireplace("%GAME_LOGO%", $game['logo']['large'], $game_output_tmp);
$game_output_tmp = str_ireplace("%GAME_HOURS_TWOWEEKS%", $game['hours_twoweeks'], $game_output_tmp);
$game_output_tmp = str_ireplace("%GAME_HOURS_TOTAL%", $game['hours_total'], $game_output_tmp);
$game_output_tmp = str_ireplace("%GAME_STATS_URL%", $game['stats_url'], $game_output_tmp);
$game_output .= $game_output_tmp;
}
} else $game_output = "No Steam games played recently";
$output = $instance["template"];
$output = str_ireplace("%GAMES_TWOWEEKS%", $game_output, $output);
//status conditionals
if (($steam_array['online'])) {
$output = preg_replace('/IF_ONLINE\{([^}]*)\}(?:ELSE\{([^}]*)\})?/i', '\1', $output);
} else {
$output = preg_replace('/IF_ONLINE\{([^}]*)\}(?:ELSE\{([^}]*)\})?/i', '\2', $output);
}
if (($steam_array['ingame'])) {
$output = preg_replace('/IF_INGAME\{([^}]*)\}(?:ELSE\{([^}]*)\})?/i', '\1', $output);
} else {
$output = preg_replace('/IF_INGAME\{([^}]*)\}(?:ELSE\{([^}]*)\})?/i', '\2', $output);
}
$output = str_ireplace("%USERNAME%", $steam_array['username'], $output);
$output = str_ireplace("%ID64%", $steam_array['ID64'], $output);
$output = str_ireplace("%PROFILE_URL%", $steam_array['profile_url'], $output);
$output = str_ireplace("%AVATAR_ICON%", $steam_array['avatar']['icon'], $output);
$output = str_ireplace("%AVATAR_MEDIUM%", $steam_array['avatar']['medium'], $output);
$output = str_ireplace("%AVATAR_LARGE%", $steam_array['avatar']['large'], $output);
$output = str_ireplace("%HOURS_TWOWEEKS%", $steam_array['hours_twoweeks'], $output);
print $output . $after_widget;
}
//overrides parent function
//shows the widget settings fields in the widget editor page
function form($instance) {
$instance = wp_parse_args((array) $instance, $this->default_settings);
$title = strip_tags($instance['title']);
$steam_id = esc_attr($instance['steam_id']);
$cache_interval = $instance['cache_interval'];
$selected_preset = $instance['preset'];
$game_template = format_to_edit($instance['game_template']);
$template = format_to_edit($instance['template']);
//backwards compat for 1.5 where preset key was numeric
if (is_numeric($selected_preset)) $selected_preset = "custom";
?>
style="display: none;">
%GAME_NAME%
%GAME_URL%
%GAME_ICON%
%GAME_LOGO_SMALL%
%GAME_LOGO%
%GAME_HOURS_TWOWEEKS%
%GAME_HOURS_TOTAL%
%GAME_STATS_URL%
IF_GAME_INGAME{}ELSE{}
IF_GAME_STATS{}ELSE{}
%GAMES_TWOWEEKS%
%HOURS_TWOWEEKS%
%USERNAME%
%ID64%
%PROFILE_URL%
%AVATAR_ICON%
%AVATAR_MEDIUM%
%AVATAR_LARGE%
IF_INGAME{}ELSE{}
IF_ONLINE{}ELSE{}
number)) { ?>
Shortcode: [steam id="number; ?>"]
get_int_option($new_instance['cache_interval'], $this->default_settings['cache_interval'], 0, 86400);
if (isset($new_instance['steam_id'])) {
if (preg_match('/\A(?:STEAM_)?\d+:(\d+):(\d+)\Z/i', $new_instance['steam_id'], $matches)) {
//they used their internal steam id, so we have to convert it
$new_instance['steam_id'] = ($matches[2] * 2) + 0x0110000100000000 + $matches[1];
}
$instance['steam_id'] = $new_instance['steam_id'];
}
if (isset($new_instance['preset'])) {
$instance['preset'] = $new_instance['preset'];
if ($new_instance['preset'] != "custom") {
$instance['game_template'] = $this->presets[$instance['preset']]["game_template"];
$instance['template'] = $this->presets[$instance['preset']]["template"];
} else {
if (isset($new_instance['game_template'])) $instance['game_template'] = empty($new_instance['game_template']) ? $this->default_settings['game_template'] : $new_instance['game_template'];
if (isset($new_instance['template'])) $instance['template'] = empty($new_instance['template']) ? $this->default_settings['template'] : $new_instance['template'];
}
}
if (isset($new_instance['last_cached'])) $instance['last_cached'] = $new_instance['last_cached'];
if (isset($new_instance['cache'])) $instance['cache'] = $new_instance['cache'];
return $instance;
}
//new function to save this instance's data when not in widget editor
private function internal_update($instance) {
//get all instances of this widget
$all_instances = $this->get_settings();
//get our current instance
$old_instance = isset($all_instances[$this->number]) ? $all_instances[$this->number] : array();
//call the overriding update function on this instance
$instance = $this->update($instance, $old_instance);
//if we got something back, plug it back into the array of all instances
if ($instance !== false) $all_instances[$this->number] = $instance;
//and save all instances of this widget
$this->save_settings($all_instances);
}
private function get_int_option($request_opt, $default_opt = 0, $min_val = NULL, $max_val = NULL) {
if ((isset($request_opt)) && (is_numeric($request_opt))) {
if ((!is_null($min_val)) && ($request_opt < $min_val)) return $min_val;
if ((!is_null($max_val)) && ($request_opt > $max_val)) return $max_val;
return $request_opt;
} else {
return $default_opt;
}
}
private function get_xml_from_steam($xml_url) {
//prefer curl, so we can set a timeout
if (function_exists("curl_init")) {
//support location redirects to future-proof script
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
$max_redirs = 2;
} else $max_redirs = 0;
$ch = curl_init($xml_url);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => $max_redirs > 0,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 5,
CURLOPT_MAXREDIRS => $max_redirs,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FRESH_CONNECT => true
));
$content = curl_exec($ch);
$err = curl_errno($ch);
curl_close($ch);
//see if there were no errors
if ($err == 0) {
if (($steam_xml = @simplexml_load_string($content)) === false) return false; else return $steam_xml;
}
}
//fallback to simple xml remote open
if (($steam_xml = @simplexml_load_file($xml_url)) === false) return false; else return $steam_xml;
}
}
function AdvancedSteamWidget_register() {
register_widget('AdvancedSteamWidget');
}
add_action( 'widgets_init', 'AdvancedSteamWidget_register' );
function AdvancedSteamWidget_admin_scripts($hook) {
if( $hook != 'widget.php') return;
wp_enqueue_script('jquery');
}
add_action('admin_enqueue_scripts', 'AdvancedSteamWidget_admin_scripts');
function AdvancedSteamWidget_shortcode($attribs) {
$widget = new AdvancedSteamWidget();
if (!isset($attribs["id"])) return '';
$id = $attribs["id"];
$widget->_set($id);
$settings = $widget->get_settings();
$instance = $settings[$id];
if (!is_array($instance)) return "Invalid Steam Widget ID!";
$args = array('before_widget' => '', 'after_widget' => "
", 'before_title' => '', 'after_title' => '
');
ob_start();
$widget->widget($args, $instance);
return ob_get_clean();
}
add_shortcode('steam', 'AdvancedSteamWidget_shortcode');