function wp_create_single_series($series_name)
{
    if ($id = series_exists($series_name)) {
        return $id;
    }
    return wp_insert_term($series_name, 'series');
}
/**
 * get_series_icon() -  Template tag for insertion of series-icons
 *
 * @package Organize Series WordPress Plugin
 * @since 2.0
 *
 * @uses parse_str()
 * @uses stripslaghes_gpc_arr()
 * @uses get_the_series()
 * @uses is_array()
 * @uses series_get_icons()
 * @uses seriesicons_path()
 * @uses seriesicons_url()
 * @uses get_series_link()
 * @uses getimagesize()
 * @uses series_fit_rect()
 *
 * @param int[-1] $fit_width Maximum width (or desired width if $expanded=true) of the image.
 * @param int[-1] $fit_height Macimum height (or desired height if $expanded=true) of the image.
 * @param boolean [false] $expand Whether the image should be expanded to fit the rectangle specified by fit_xxx.
 * @param int $series Series ID. If not specified, the current series is used or the current post's series.
 * @param string $prefix String to echo before the image tag. If no image, no otuput.
 * @param string $suffix String to echo after the image tag. Ignored if no image found.
 * @param string [] $class  Class attribute for the image tag.
 * @param boolean [1] $link If true the image is made a hyperlink (wrapped by anchor tag).
 * @param  boolean [1] $display If true the function will echo the image.  If false the function will return the assembled image as a string.
 *
 * @return mixed|bool|string Will return false if image is not found.  Will return string containing assembled html code for image if $display is false.  Will echo image if the $display param = true.
 */
function get_series_icon($params = '')
{
    global $orgseries;
    parse_str($params, $p);
    if (!isset($p['fit_width'])) {
        $p['fit_width'] = -1;
    }
    if (!isset($p['fit_height'])) {
        $p['fit_height'] = -1;
    }
    if (!isset($p['expand'])) {
        $p['expand'] = false;
    }
    if (!isset($p['series'])) {
        $p['series'] = get_query_var(SERIES_QUERYVAR);
    }
    if (!isset($p['prefix'])) {
        $p['prefix'] = '';
    }
    if (!isset($p['suffix'])) {
        $p['suffix'] = '';
    }
    if (!isset($p['class'])) {
        $p['class'] = 'series-icon-' . $p['series'];
    }
    if (!isset($p['link'])) {
        $p['link'] = 1;
    }
    if (!isset($p['display'])) {
        $p['display'] = 1;
    }
    stripslaghes_gpc_arr($p);
    if (empty($p['series']) && isset($GLOBALS['post'])) {
        $serieslist = get_the_series($GLOBALS['post']->ID);
        if (is_array($serieslist)) {
            $p['series'] = $serieslist[0]->term_id;
        }
    }
    $p['series'] = series_exists($p['series']);
    if (!isset($p['series'])) {
        return;
    }
    //make sure we get the id for the series (in case just the slug is given
    $icon = series_get_icons($p['series']);
    $s_name = get_series_name($p['series']);
    $file = seriesicons_path() . $icon;
    $url = seriesicons_url() . $icon;
    if ($p['link']) {
        $p['prefix'] .= '<a href="' . get_series_link($p['series']) . '">';
        $p['suffix'] = '</a>' . $p['suffix'];
    }
    if (is_file($file)) {
        list($width, $height, $type, $attr) = getimagesize($file);
        list($w, $h) = series_fit_rect($width, $height, $p['fit_width'], $p['fit_height'], $p['expand']);
        $series_icon = $p['prefix'] . '<img class="' . $p['class'] . '" src="' . $url . '" width="' . $w . '" height="' . $h . '"  alt="' . $icon . '" />' . $p['suffix'];
        if ($p['display'] == 1) {
            echo $series_icon;
        } else {
            return $series_icon;
        }
    }
    return false;
}
function admin_ajax_series()
{
    $response = array();
    if (!current_user_can('manage_series')) {
        $response['error'] = __('Sorry but you don\'t have permission to add series', 'organize-series');
    }
    if (!check_ajax_referer('add-series-nonce', 'addnonce', false)) {
        $response['error'] = 'Sorry but security check failed';
    }
    $new_nonce = wp_create_nonce('add-series-nonce');
    $name = $_POST['newseries'];
    $series_name = trim($name);
    if (!($series_nicename = sanitize_title($series_name))) {
        $response['error'] = __('The name you picked isn\'t sanitizing correctly. Try something different.', 'organize-series');
    }
    if (empty($response['error'])) {
        if (!($series_id = series_exists($series_name))) {
            $ser_id = wp_create_single_series($series_name);
            $series_id = $ser_id['term_id'];
        } else {
            $response['error'] = __('Hmm... it looks like there is already a series with that name. Try something else', 'organize-series');
        }
        $series_name = esc_html(stripslashes($series_name));
    }
    if (empty($response['error'])) {
        $response = array('id' => $series_id, 'html' => "<li id='series-{$series_id}' class='series-added-indicator'><label for='in-series-{$series_id}' class='selectit'><input value='{$series_id}' type='radio' name='post_series' id='in-series-{$series_id}' checked /><input type='hidden' name='is_series_save' value='1' /> <span class='li-series-name'>{$series_name}</span></label><span id='new_series_id' class='hidden'>{$series_id}</span></li>", 'new_nonce' => $new_nonce, 'error' => false);
    }
    echo json_encode($response);
    exit;
}