array( "name" => "Profile Only", "game_template" => '', "template" => '
%HOURS_TWOWEEKS% hrs / 2 wks
Add to Friends
' ), "profile-small" => array( "name" => "Profile Small", "game_template" => '', "template" => '
%HOURS_TWOWEEKS% hrs / 2 wks
' ), "profile-games" => array( "name" => "Profile + Games", "game_template" => '
%GAME_NAME%
IF_GAME_STATS{%GAME_HOURS_TWOWEEKS% hrs}ELSE{%GAME_HOURS_TWOWEEKS% hrs} / two weeks
', "template" => '
%HOURS_TWOWEEKS% hrs / 2 wks
IF_INGAME{In-game}ELSE{IF_ONLINE{Online}ELSE{Offline}}
%GAMES_TWOWEEKS%
' ), "games" => array( "name" => "Games Only", "game_template" => '
%GAME_NAME%
IF_GAME_STATS{%GAME_HOURS_TWOWEEKS% hrs}ELSE{%GAME_HOURS_TWOWEEKS% hrs} / two weeks
', "template" => '
%GAMES_TWOWEEKS%
' ), "grid" => array( "name" => "Games Grid", "game_template" => ' ', "template" => '
%GAMES_TWOWEEKS%
' ), "full" => array( "name" => "Full-page Profile", "game_template" => '
%GAME_NAME%
%GAME_HOURS_TWOWEEKS% hours / two weeks
IF_GAME_STATS{View Stats}
', "template" => '
IF_INGAME{In-game}ELSE{IF_ONLINE{Online}ELSE{Offline}}
%HOURS_TWOWEEKS% hours / two weeks
Add to Friends
%GAMES_TWOWEEKS%
' ) ); //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 //is not run per-instance, only per-widget-type! function __construct() { parent::__construct(false, $name = 'Steam Widget', array( 'classname' => 'advanced_steam_widget', 'description' => "Displays Steam gaming statistics") ); $this->default_settings["game_template"] = $this->presets[$this->default_settings["preset"]]["game_template"]; $this->default_settings["template"] = $this->presets[$this->default_settings["preset"]]["template"]; //settings for cron must be delayed until the widget is registered add_action('wp_loaded', array(&$this, 'setup_cron')); //hook for cleanup on widget deletion add_action('sidebar_admin_setup', array(&$this, 'delete_widget')); } //overrides parent function //displays the widget on the frontend function widget($args, $instance) { extract($args); if (!isset($instance) || !is_array($instance) || !isset($instance["cache"])) return; $steam_array = $instance["cache"]; 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; if (empty($instance["template"])) { $output = $this->presets[$this->default_settings["preset"]]["template"]; } else $output = $instance["template"]; //replace template patterns with steam data //if there are games played and the games pattern is present if (isset($steam_array['games']) && count($steam_array['games']) > 0 && stristr($output, "%GAMES_TWOWEEKS%") !== false) { $game_output = ""; foreach ($steam_array['games'] as $game) { if (empty($instance["game_template"])) { $game_output_tmp = $this->presets[$this->default_settings["preset"]]["game_template"]; } else $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 = 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; //make sure cron is set //sets up the cron for plugin-updating users if (!wp_next_scheduled('advanced_steam_widget_update', array($this->number))) { wp_schedule_event(time(), 'advanced_steam_widget_interval_' . $this->number, 'advanced_steam_widget_update', array($this->number)); } } //overrides parent function //shows the widget settings fields in the widget option 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"; ?>

Last Updated: ago

Last Updated:

number))) !== false) { ?>

Next Update:

style="display: none;">

Toggle Show Patterns

Toggle Show Patterns
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 new option is empty, use default 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']; //update steam data $instance = $this->update_data($instance); //hook for things that need to be done after the instance is updated add_filter('widget_form_callback', array(&$this, 'post_update')); return $instance; } //makes sure the requested value is valid and numeric //and clamped between an optional minimum and maximum value //if not, returns default, min, or max value 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; } } //updates steam data //must be passed the widget instance's settings //returns widget settings modified with new steam data function update_data($instance) { try { //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'; //get XML from Valve //prefer curl, so we can set a timeout if (function_exists("curl_init")) { //support location redirects to future-proof script if (ini_get('safe_mode' == 'On' && ini_get('open_basedir') != '')) { $max_redirs = 0; } else $max_redirs = 2; $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 => 15, CURLOPT_TIMEOUT => 20, CURLOPT_MAXREDIRS => $max_redirs, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_FRESH_CONNECT => true, CURLINFO_HEADER_OUT => true )); $content = curl_exec($ch); $err_code = curl_errno($ch); //DEBUG //$request_info = curl_getinfo($ch); //print print_r($request_info); curl_close($ch); //see if there were no errors if ($err_code == 0) { if (($steam_xml = @simplexml_load_string($content)) === false) throw new Exception("Error loading XML data."); } else throw new Exception("Error downloading XML data. CURL error code: " . $err_code); } //fallback to simple xml remote open if (!$steam_xml) { if (($steam_xml = @simplexml_load_file($xml_url)) === false) throw new Exception("Error downloading or parsing XML data."); } if (isset($steam_xml->error)) throw new Exception("XML Error: " . (string)$steam_xml->error); if ((string)$steam_xml->privacyState != "public") throw new Exception("Profile is not public."); //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) { //workaround for steam no 2 wks played bug $cumulative_hours_from_games = false; if ($steam_array['hours_twoweeks'] == "0.0") { $steam_array['hours_twoweeks'] = 0; $cumulative_hours_from_games = true; } $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; //see if stats name exists and is valid, i.e. not just the app id if (isset($game->statsName) && !preg_match('/\d+/', $game->statsName)) { $steam_array['games'][$k]['stats_url'] = $profile_url . "/stats/" . (string)$game->statsName; } else $steam_array['games'][$k]['stats_url'] = false; if ($cumulative_hours_from_games === true) { $steam_array['hours_twoweeks'] += (float)$game->hoursPlayed; } $k++; } } //write the cache and timestamp $instance['cache'] = $steam_array; $instance['last_cached'] = current_time('timestamp'); $instance['last_update_status'] = true; return $instance; } catch (Exception $err) { $instance['last_update_status'] = $err->getMessage(); return $instance; } } //updates steam data given a specific widget id //used for cron target function update_and_save_data($id) { //get the selected instances of this widget $all_instances = $this->get_settings(); if (!isset($all_instances[$id])) return; //call the update $all_instances[$id] = $this->update_data($all_instances[$id]); //save all instances of this widget $this->save_settings($all_instances); } //called on deletion of a widget from the widget options page function delete_widget() { if (isset($_POST['delete_widget']) && $_POST['delete_widget'] && isset($_POST['widget_number'])) { //remove all crons for this instance wp_clear_scheduled_hook('advanced_steam_widget_update', array($_POST['widget_number'])); } } //called after update has been saved function post_update($instance) { //always reset the cron, because we just updated and the interval should start from now wp_clear_scheduled_hook('advanced_steam_widget_update', array($this->number)); wp_reschedule_event(time(), "advanced_steam_widget_interval_" . $this->number, 'advanced_steam_widget_update', array($this->number)); return $instance; } //callback for changing the wp cron for the feed update function cron_add_interval($schedules) { //must be done for each instance so they're always available whenever called foreach ($this->get_settings() as $id => $instance) { $interval = $this->get_int_option($instance['cache_interval'], $this->default_settings['cache_interval'], 0, 86400); $schedules['advanced_steam_widget_interval_' . $id] = array( 'interval' => $interval, 'display' => 'Advanced Steam Widget ' . $id ); } return $schedules; } //fired once for all widgets function setup_cron() { //hook for the custom cron interval add_filter('cron_schedules', array(&$this, 'cron_add_interval')); //action for cron add_action('advanced_steam_widget_update', array(&$this, 'update_and_save_data')); } } //adds our widget to available ones function AdvancedSteamWidget_register() { register_widget('AdvancedSteamWidget'); } add_action('widgets_init', 'AdvancedSteamWidget_register'); //cleanup for plugin deactivation function AdvancedSteamWidget_deactivate($plugin) { if ($plugin == "advanced-steam-widget/steam_widget.php") { //remove all crons for this plugin AdvancedSteamWidget_remove_crons(); } } add_action('deactivated_plugin', 'AdvancedSteamWidget_deactivate'); //cleanup for plugin uninstall function AdvancedSteamWidget_uninstall() { //remove all crons for this plugin AdvancedSteamWidget_remove_crons(); //delete all settings delete_option("widget_advancedsteamwidget"); //no need to remove widgets from sidebars //this is done next time widget options are modified } register_uninstall_hook("advanced-steam-widget/steam_widget.php", "AdvancedSteamWidget_uninstall"); //removes all crons for this plugin function AdvancedSteamWidget_remove_crons() { $widget_settings = get_option("widget_advancedsteamwidget"); unset($widget_settings['_multiwidget']); if (is_array($widget_settings) && count($widget_settings)) { foreach ($widget_settings as $id => $settings) { wp_clear_scheduled_hook('advanced_steam_widget_update', array($id)); } } } //adds jquery to the widget options page function AdvancedSteamWidget_admin_scripts($hook) { if($hook != 'widget.php') return; wp_enqueue_script('jquery'); } add_action('admin_enqueue_scripts', 'AdvancedSteamWidget_admin_scripts'); //handler for the shortcode version of our widget //ex: [steam id=1] function AdvancedSteamWidget_shortcode($attribs) { if (!isset($attribs["id"])) return "No Steam Widget ID!"; $widget = new AdvancedSteamWidget(); //set the instance id $id = $attribs["id"]; $widget->_set($id); //get instance settings $settings = $widget->get_settings(); if (!isset($settings[$id]) || !is_array($settings[$id])) return "Invalid Steam Widget ID!"; $instance = $settings[$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');