function wp_set_post_series($post_ID = 0, $post, $update, $series_id = array(), $dont_skip = false, $is_published = false)
{
    $post_series = null;
    $post_shorttitle = array();
    if (!is_bool($update)) {
        return;
    }
    //safety check for users on earlier version of WP (so existing series don't get messed up)
    //fix for the revisions feature in WP 2.6+  && bulk-edit stuff.
    if ($post->post_type == 'revision' || isset($_GET['bulk_edit_series']) && $_GET['bulk_edit_series'] == 'bulk' || !isset($_REQUEST['is_series_save'])) {
        return;
    }
    $post_ID = (int) $post_ID;
    $old_series = wp_get_post_series($post_ID);
    if (empty($series_id)) {
        $post_series = isset($_REQUEST['post_series']) && is_array($_REQUEST['post_series']) ? $_REQUEST['post_series'] : array($_REQUEST['post_series']);
    } else {
        $post_series = is_array($series_id) ? $series_id : array($series_id);
    }
    $post_series = os_strarr_to_intarr($post_series);
    if (empty($post_series) || count($post_series) >= count($old_series)) {
        $match = false;
    } else {
        $match = array_diff($old_series, $post_series);
    }
    if (empty($post_series) || count($post_series) == 1 && $post_series[0] == 0) {
        $post_series = array();
    }
    if (isset($_POST) || isset($_GET)) {
        if (isset($_POST['series_part'])) {
            $series_part = is_array($_POST['series_part']) ? $_POST['series_part'] : array($_POST['series_part']);
        }
        if (isset($_GET['series_part'])) {
            $series_part = is_array($_GET['series_part']) ? $_GET['series_part'] : array($_GET['series_part']);
        }
        //The "short" title of the post that will be displayed  in the OrgSeries widget.
        if (isset($_POST['serie_post_shorttitle'])) {
            $post_shorttitle = $_POST['serie_post_shorttitle'];
        }
        if (isset($_GET['serie_post_shorttitle'])) {
            $post_shorttitle = $_GET['serie_post_shorttitle'];
        }
        $st_ser_id = is_array($post_series) && isset($post_series[0]) ? (int) $post_series[0] : '';
        $post_shorttitle = is_array($post_shorttitle) && isset($post_shorttitle[$st_ser_id]) ? trim($post_shorttitle[$st_ser_id]) : '';
        update_post_meta($post->ID, SPOST_SHORTTITLE_KEY, $post_shorttitle);
        //if we don't have any changes in the series or series part info (or series post status) then let's get out and save time.
        $p_status = $post->post_status;
        if ($p_status != 'draft' && $p_status != 'future' && $p_status != 'pending') {
            $is_published = TRUE;
        }
        $count = count($post_series);
        $c_chk = 0;
        foreach ($post_series as $ser) {
            if (in_array($ser, $old_series) && $series_part[$ser] == wp_series_part($post_ID, $ser) && !$dont_skip) {
                $c_chk++;
                continue;
            } else {
                $p_ser_edit[] = $ser;
                //these are the series we need to set the parts for (leave the rest alone when we get to this section).
            }
        }
        if ($c_chk == $count && !empty($post_series) && count($post_series) == count($old_series) && !$dont_skip) {
            return;
        }
        //there are no changes so let's just skip the rest
    }
    if (empty($post_series)) {
        foreach ($old_series as $o_ser) {
            $part_key = apply_filters('orgseries_part_key', SERIES_PART_KEY, $o_ser);
            delete_post_meta($post_ID, $part_key);
        }
    }
    foreach ($old_series as $os_id) {
        if (!in_array($os_id, $post_series)) {
            wp_delete_post_series_relationship($post_ID, $os_id);
        }
    }
    if (!empty($match) && $match) {
        foreach ($match as $part_reset_id) {
            wp_reset_series_order_meta_cache($post_ID, $part_reset_id);
        }
    }
    $success = wp_set_object_terms($post_ID, $post_series, 'series');
    if (empty($p_ser_edit)) {
        return;
    }
    //let's get out we've done everything we need to do.
    if ($success) {
        if ($p_status != 'draft' && $p_status != 'future' && $p_status != 'pending') {
            $is_published = TRUE;
        }
        foreach ($p_ser_edit as $ser_id) {
            if (empty($series_part[$ser_id])) {
                $s_pt = wp_series_part($post_ID, $ser_id);
                if (!$series_part) {
                    $series_part = 0;
                }
            } elseif (!$is_published) {
                $s_pt = $series_part[$ser_id];
            } else {
                if (isset($_GET['submit'])) {
                    $set_spart = $_GET['series_part'];
                } else {
                    $set_spart = $_POST['series_part'];
                }
                $s_pt = $set_spart[$ser_id];
            }
            set_series_order($post_ID, $s_pt, $ser_id, $is_published);
        }
        return;
    } else {
        return FALSE;
    }
}
/** 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;
}