function widget($args, $instance)
 {
     global $orgseries, $wp_query;
     extract($args, EXTR_SKIP);
     $c = $instance['show-count'] ? '1' : '0';
     $e = $instance['hide-empty'] ? '1' : '0';
     $showpostlist = $instance['postlistdisplay-toggle'] ? '1' : '0';
     $showseriestoc = $instance['seriestocdisplay-toggle'] ? '1' : '0';
     $series_args = $args = apply_filters('widget_seriestoc_args', array('orderby' => 'name', 'show_count' => $c, 'hide_empty' => $e, 'echo' => false));
     $title = $instance['title'];
     if (isset($instance['serieswidget-title']) && count($instance['serieswidget-title']) > 0) {
         $widget_title = $instance['serieswidget-title'];
     } else {
         $widget_title = false;
     }
     $out = '';
     if ($showseriestoc) {
         if ($instance['list-type'] == 'list') {
             $out .= '<ul>';
             $series_args['title_li'] = '';
             $out .= wp_list_series($series_args);
             $out .= '</ul>';
         } elseif ($instance['list-type'] == 'dropdown') {
             $series_args['show_option_all'] = __('Select Series', 'organize-series');
             $series_args['name'] = 'orgseries_dropdown';
             $out = wp_dropdown_series($series_args);
         }
     }
     if ($showpostlist) {
         if ($wp_query->is_single && $showpostlist && ($series = get_the_series())) {
             if ($showseriestoc) {
                 $out .= '<br /><br />';
             }
             $out .= get_series_posts('', 'widget', false, $widget_title);
         }
     }
     if (!empty($out)) {
         echo $before_widget;
         if ($title) {
             echo $before_title . $title . $after_title;
         }
         echo $out;
         echo $after_widget;
     }
 }
/** NOTE: %postcontent% is NOT replaced with this function...it happens in the content filter function **/
function token_replace($replace, $referral = 'other', $id = 0, $ser_ID = 0)
{
    global $post, $orgseries;
    $p_id = $id == 0 ? $post->ID : $id;
    $ser_id = $ser_ID == 0 ? $id : $ser_ID;
    //$p_id = (empty($post->ID) || $post->ID == '') ? $id : $post->ID;
    $settings = $orgseries->settings;
    $replace = apply_filters('pre_orgseries_token_replace', $replace, $referral, $id, $p_id, $ser_id);
    if ('post-list' == $referral) {
        $ser_width = $settings['series_icon_width_post_page'];
    } elseif ('latest_series' == $referral) {
        $ser_width = $settings['series_icon_width_latest_series'];
    } else {
        $ser_width = $settings['series_icon_width_series_page'];
    }
    if ('series-toc' == $referral || 'latest_series' == $referral) {
        $replace = str_replace('%total_posts_in_series%', wp_postlist_count($ser_id), $replace);
    } else {
        $replace = str_replace('%total_posts_in_series%', wp_postlist_count($ser_id), $replace);
    }
    if (stristr($replace, '%series_icon%')) {
        $replace = str_replace('%series_icon%', get_series_icon('fit_width=' . $ser_width . '&link=0&series=' . $ser_id . '&display=0'), $replace);
    }
    if (stristr($replace, '%series_icon_linked%')) {
        $replace = str_replace('%series_icon_linked%', get_series_icon('fit_width= ' . $ser_width . '&series=' . $ser_id . '&display=0'), $replace);
    }
    if (stristr($replace, '%series_title%')) {
        $replace = str_replace('%series_title%', the_series_title($ser_id, FALSE), $replace);
    }
    if (stristr($replace, '%series_title_linked%')) {
        $replace = str_replace('%series_title_linked%', the_series_title($ser_id), $replace);
    }
    if (stristr($replace, '%post_title_list%')) {
        $replace = str_replace('%post_title_list%', get_series_posts($id, $referral), $replace);
    }
    if (stristr($replace, '%post_title%')) {
        $replace = str_replace('%post_title%', series_post_title($id, FALSE), $replace);
    }
    if (stristr($replace, '%post_title_linked%')) {
        $replace = str_replace('%post_title_linked%', series_post_title($id), $replace);
    }
    if (stristr($replace, '%series_part%')) {
        $replace = str_replace('%series_part%', wp_series_part($p_id, $ser_id), $replace);
    }
    if (stristr($replace, '%series_description%')) {
        $replace = str_replace('%series_description%', series_description($ser_id), $replace);
    }
    if (stristr($replace, '%next_post%')) {
        $replace = str_replace('%next_post%', wp_series_nav($id), $replace);
    }
    if (stristr($replace, '%previous_post%')) {
        $replace = str_replace('%previous_post%', wp_series_nav($id, FALSE), $replace);
    }
    if (stristr($replace, '%next_post_custom%')) {
        $replace = str_replace('%next_post_custom%', wp_series_nav($id, TRUE, TRUE), $replace);
    }
    if (stristr($replace, '%previous_post_custom%')) {
        $replace = str_replace('%previous_post_custom%', wp_series_nav($id, FALSE, TRUE), $replace);
    }
    $replace = apply_filters('post_orgseries_token_replace', $replace, $referral, $id, $p_id, $ser_id);
    return $replace;
}