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" => ' ' ), "grid" => array( "name" => "Games Grid", "game_template" => 'Last Updated: ago
Last Updated:
number))) !== false) { ?>Next Update:
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' => '", 'before_title' => '