/**
  * Saves the widget's settings.
  *
  */
 function update($new_instance, $old_instance)
 {
     $instance = $old_instance;
     $instance['title'] = strip_tags(stripslashes($new_instance['title']));
     /* BK place URL
      ** Brightkite place urls should be in the expected format,
      ** and most importantly, should contain the place ID.
      ** Only change the saved place URL if it's valid or empty.
      */
     $url_new = $new_instance['place_url'];
     $place_id = BK_extract_place_id($url_new);
     $hasPlaceID = isset($place_id) && strlen($place_id) > 0;
     if ($url_new === '' || $hasPlaceID) {
         $instance['place_url'] = $url_new === '' ? '' : BK_create_place_url($place_id);
     }
     unset($url_new, $place_id, $hasPlaceID);
     /* display settings
      ** $new_instance['display'] is an array auto-parsed by PHP from the 'display[]' form elements.
      ** this code uniquifies, sorts, and lowercases it before saving it.
      */
     $display = array_unique($new_instance['display']);
     sort($display);
     $display = array_flip(array_change_key_case(array_flip($display), CASE_LOWER));
     $instance['display_settings'] = implode(',', $display);
     unset($display);
     /* filter settings
      ** $new_instance['filter'] is an array auto-parsed by PHP from the 'filter[]' form elements.
      ** this code uniquifies, sorts, and lowercases it before saving it.
      */
     $filter = array_unique($new_instance['filter']);
     sort($filter);
     $filter = array_flip(array_change_key_case(array_flip($filter), CASE_LOWER));
     $instance['filter_settings'] = implode(',', $filter);
     unset($filter);
     /* count
      ** how many items to show.
      ** can be [0..20] - invalid inputs do not change anything.
      */
     $count = $new_instance['count'];
     $count_i = (int) $count;
     if ($count == $count_i && $count_i >= 0 && $count_i <= 20) {
         $instance['count'] = $count_i;
     }
     unset($count, $count_i);
     return $instance;
 }
function BK_construct_hierarchy(array &$hierarchy)
{
    $html = array();
    for ($i = count($hierarchy); $i > 0; --$i) {
        $place = $hierarchy[$i - 1];
        if (is_array($place) && array_key_exists('id', $place) && array_key_exists('name', $place)) {
            $html[] = '<a href="' . BK_create_place_url($place['id']) . '">' . $place['name'] . '</a>';
        }
    }
    return implode(' &raquo; ', $html);
}
    /**
     * Displays the widget on the blog
     *
     */
    function widget($args, $instance)
    {
        extract($args);
        $title = apply_filters('widget_title', empty($instance['title']) ? '&nbsp;' : $instance['title']);
        $login = empty($instance['login']) ? '' : $instance['login'];
        $display_settings = explode(',', $instance['display_settings']);
        $filter_settings = explode(',', $instance['filter_settings']);
        $count = (int) $instance['count'];
        if (strlen($login) > 0) {
            $user_url = BK_HOST . '/people/' . $login . '.json?dataset=profile';
            $stream_url = BK_HOST . '/objects.json?person_id=' . $login . '&filter=' . implode('%2C', $filter_settings) . '&limit=' . (int) $count;
            $json = wp_remote_fopen($user_url);
            $data = BK_json_decode($json);
            if (is_array($data) && array_key_exists('login', $data)) {
                /* Before the widget */
                echo $before_widget;
                /* the title */
                if ($title) {
                    echo $before_title . $title . $after_title;
                }
                /* Make the widget */
                ?>
				<div class="brightkite-person-data" id="<?php 
                echo $this->get_field_id('brightkite-person');
                ?>
">
					<div>
						<a href="<?php 
                echo BK_HOST;
                ?>
/people/<?php 
                echo $login;
                ?>
/">
							<span><?php 
                echo $login . (in_array('name', $display_settings) ? ' (' . BK_emoji_decode($data['fullname']) . ')' : '');
                ?>
</span>
	<?php 
                if (in_array('avatar', $display_settings)) {
                    ?>
							<img src="<?php 
                    echo $data['small_avatar_url'];
                    ?>
" />
	<?php 
                }
                ?>
						</a>
					</div>
	<?php 
                if (in_array('desc', $display_settings)) {
                    ?>
					<div><?php 
                    echo $data['description'];
                    ?>
</div>
	<?php 
                }
                flush();
                $json = wp_remote_fopen($stream_url);
                $objects = BK_json_decode($json);
                ?>
					<ul class="brightkite-stream">
	<?php 
                for ($i = 0; $i < count($objects); ++$i) {
                    $obj = $objects[$i];
                    ?>
					<li>
	<?php 
                    if (array_key_exists('photo', $obj)) {
                        ?>
						<div class="brightkite-object-image">
							<a rel="external" href="<?php 
                        echo BK_HOST;
                        ?>
/photos/<?php 
                        echo $obj['id'];
                        ?>
">
								<img src="<?php 
                        echo preg_replace('/(\\.(png|jpg))/', '-small.$2', $obj['photo']);
                        ?>
" />
							</a>
						</div>
	<?php 
                    }
                    if (array_key_exists('body', $obj)) {
                        ?>
						<p><?php 
                        echo BK_linkify($obj['body']);
                        ?>
</p>
	<?php 
                    }
                    if (array_key_exists('photo', $obj)) {
                        ?>
						<div class="clear"></div>
	<?php 
                    }
                    if (array_key_exists('place', $obj) && array_key_exists('name', $obj['place'])) {
                        if (array_key_exists('created_at_as_words', $obj)) {
                            $ago = ' <a rel="external" href="' . BK_HOST . '/objects/' . $obj['id'] . '" class="brightkite-time-link">' . $obj['created_at_as_words'] . ' ago</a>';
                        } else {
                            $ago = '';
                        }
                        $place = '<a href="' . BK_create_place_url($obj['place']['id']) . '">@ ' . $obj['place']['name'] . '</a>';
                        if ($obj['object_type'] === 'checkin') {
                            $place = 'checked in ' . $place;
                        }
                        ?>
						<div class="brightkite-location"><?php 
                        echo $place;
                        echo $ago;
                        ?>
</div>
	<?php 
                    }
                    ?>
					</li>
	<?php 
                }
                ?>
					</ul>
				</div>
	<?php 
                /* After the widget */
                echo $after_widget;
            }
        }
    }