$b['part'] ) return 1; elseif ( $a['part'] < $b['part'] ) return -1; else return 0; } //This function is used to create an array of posts in a series including the order the posts are in the series. Then it will sort the array so it is keyed in the order the posts are in. Will return the array. function get_series_order ($posts, $postid = 0, $skip = TRUE) { if (!isset($posts)) return false; //don't have the posts object so can't do anything. if ( !is_array( $posts ) ) $posts = array($posts); $series_posts = array(); $key = 0; foreach ($posts as $spost) { if (array_key_exists('object_id', $spost)) { $spost_id = $spost['object_id']; } else { $spost_id = $spost; } if ($skip && $spost_id == $postid) continue; $currentpart = get_post_meta($spost_id, SERIES_PART_KEY, true); $series_posts[$key]['id'] = $spost_id; $series_posts[$key]['part'] = $currentpart; $key++; } if (count($series_posts) > 1) usort( $series_posts, '_usort_series_by_part' ); return $series_posts; } /* functions referenced by other files */ function &get_series($args = '') { global $wpdb; $key = md5( serialize($args) ); if ( $cache = wp_cache_get('get_series','series') ) if ( isset( $cache[ $key ] ) ) return apply_filters('get_series', $cache[$key],$args); $series = get_terms('series', $args); if ( empty($series) ) return array(); $cache[ $key ] = $series; wp_cache_set( 'get_series', $cache, 'series' ); $series = apply_filters('get_series', $series, $args); return $series; } function &get_orgserial($orgserial, $output = OBJECT, $filter = 'raw') { return get_term($orgserial, 'series', $output, $filter); } //permalinks , rewrite rules etc.// function get_series_permastruct() { global $wp_rewrite; if (empty($wp_rewrite->permalink_structure)) { $series_structure = ''; return false; } $series_token = '%' . SERIES_QUERYVAR . '%'; $series_structure = $wp_rewrite->front . SERIES_QUERYVAR . "/$series_token"; return $series_structure; } function series_createRewriteRules($rules) { global $wp_rewrite; $series_token = '%' . SERIES_QUERYVAR . '%'; $wp_rewrite->add_rewrite_tag($series_token, '(.+)', SERIES_QUERYVAR . '='); //without trailing slash $series_structure = $wp_rewrite->root . SERIES_QUERYVAR . "/$series_token"; $rewrite = $wp_rewrite->generate_rewrite_rules($series_structure); //return $series_structure; return ( $rules + $rewrite ); } function series_init() { global $wp_rewrite; if (isset($wp_rewrite) && $wp_rewrite->using_permalinks()) { define('SERIES_REWRITEON', '1'); //pretty permalinks please! } else { define('SERIES_REWRITEON', '0'); //old school links } //generate rewrite rules for series queries if (SERIES_REWRITEON && SERIES_REWRITERULES) add_filter('rewrite_rules_array', 'series_createRewriteRules'); //setting up the series_toc_page redirect $settings = get_option('org_series_options'); $series_toc_url = $settings['series_toc_url']; if ($series_toc_url && (strpos($_SERVER['REQUEST_URI'], $series_toc_url) === 0) && (strlen($_SERVER['REQUEST_URI']) == strlen($series_toc_url))) { status_header(200); add_filter('request', 'orgSeries_request'); add_action('parse_query', 'orgSeries_parse_query'); add_action('template_redirect', 'orgSeries_toc_template'); } //$wp_rewrite->flush_rules(); //TODO: (NEEDS TESTING) I THINK THIS MIGHT MAKE IT SO USERS DON'T HAVE TO UPDATE THE PERMALINKS MANUALLY. Probably should be added to the plugin_activation hook. } function orgSeries_parse_query($wp_query) { $wp_query->is_404 = false; } function orgSeries_request($query_vars) { $query_vars['error'] = false; return $query_vars; } function orgSeries_toc_template() { if (file_exists(TEMPLATEPATH . '/seriestoc.php')) { $template = TEMPLATEPATH . '/seriestoc.php'; } else { $template = ABSPATH . 'wp-content/plugins/orgSeries/seriestoc.php'; } if ($template) { load_template($template); exit; } return; } add_action('init', 'series_init'); /* ---------- SERIES TEMPLATE TAGS (maybe add to a series-template.php file?) --------------------------*/ //url constructor/function template tags. function get_series_link( $series_id ) { $series_token = '%' . SERIES_QUERYVAR . '%'; $serieslink = get_series_permastruct(); $series = &get_term($series_id, 'series'); if (is_wp_error( $series ) ) return $series; $slug = $series->slug; $id = $series->term_id; if ( empty($serieslink) ) { $file = get_option('home') . '/'; $serieslink = $file . '?series=' . $id; } else { $serieslink = str_replace($series_token, $slug, $serieslink); $serieslink = get_settings('home') . user_trailingslashit($serieslink, 'series'); } return $serieslink; //return apply_filters('series_link', $serieslink, $series_id); } function get_the_series( $id = false ) { global $post, $tem_cache, $blog_id; $id = (int) $id; if ( !$id ) $id = (int) $post->ID; $series = get_object_term_cache($id, 'series'); if (false === $series ) $series = wp_get_object_terms($id, 'series'); $series = apply_filters('get_the_series', $series); //adds a new filter for users to hook into if ( !empty($series) ) usort($series, '_usort_terms_by_name'); return $series; } function get_the_series_by_ID( $series_ID ) { $series_ID = (int) $series_ID; $series = &get_orgserial($series_ID); if ( is_wp_error( $series ) ) return $series; return $series->name; } function get_the_series_list( $before = '', $sep = '', $after = '') { //This prepares a display lists of all series associated with a particular post and can choose the surrounding tags. Probably should modify this so the surrounding tags can be set in options? This particular function only returns the values. the_series() will echo the values by default (and calls this function). $series = get_the_series(); if ( empty( $series ) ) return false; $series_list = $before; foreach ( $series as $orgSerial ) { $link = get_series_link($orgSerial->term_id); if ( is_wp_error( $link ) ) return $link; $series_links[] = '' . $orgSerial->name . ''; } $series_links = join( $sep, $series_links); $series_links = apply_filters ( 'the_series', $series_links ); $series_list .= $tag_links; $series_list .= $after; return $series_list; } function the_series( $before = 'Series: ', $sep = ', ', $after = '') { //This function will echo the results from get_the_series_list with the modification of what shows up surrounding each series $return = get_the_series_list($before, $sep, $after); if ( is_wp_error( $return ) ) return false; else echo $return; } function in_series( $series_term ) { //check if the current post is in the given series global $post, $blog_id; $series = get_object_term_cache($post->ID, 'series'); if ( false === $series ) $series = wp_get_object_terms($post->ID, 'series'); if ( array_key_exists($series_term, $series)) return true; else return false; } function get_series_name($series_id) { $series_id = (int) $series_id; $series = get_orgserial($series_id); return $series->name; } function the_series_title($series_id=0, $linked=TRUE, $display=FALSE) { if( 0==$series_id ) return false; $series_id = (int) $series_id ; if ( !empty($series_id) ) { $series_name = get_series_name($series_id); if ( is_wp_error( $series_name ) ) return false; $prefix = ''; $suffix = ''; if ( !empty($series_name) ) { if ( $linked ) { $series_link = get_series_link($series_id); $prefix = ''; $suffix = ''; } $result = $prefix . $series_name . $suffix; if ( $display ) echo $result; else return $result; } } return false; } function series_description($series_id = 0) { global $series; if ( !$series_id ) $series_id = $series; return get_term_field('description', $series_id, 'series'); } function series_post_title($post_ID, $linked=TRUE) { global $post; if (!isset($post_ID)) $post_ID = (int)$post->ID; $title = get_the_title($post_ID); if ($linked) { $link = get_permalink($post_ID); $return = '' . $title . ''; } else { $return = $title; } return $return; } /** Replaces tokens (set in orgSeries options) with the relevant values **/ /** NOTE: %postcontent% is NOT replaced with this function...it happens in the content filter function **/ function token_replace($replace, $referral = 'other', $id = 0) { global $post; $p_id = $post->ID; $settings = get_option('org_series_options'); 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($id), $replace); } else { $replace = str_replace('%total_posts_in_series%', wp_postlist_count(), $replace); } if( stristr($replace, '%series_icon%') ) $replace = str_replace('%series_icon%', get_series_icon('fit_width=' . $ser_width . '&link=0&series=' . $id . '&display=0'), $replace); if( stristr($replace, '%series_icon_linked%') ) $replace = str_replace('%series_icon_linked%', get_series_icon('fit_width= ' . $ser_width . '&series=' . $id . '&display=0'), $replace); if( stristr($replace, '%series_title%') ) $replace = str_replace('%series_title%', the_series_title($id, FALSE), $replace); if( stristr($replace, '%series_title_linked%') ) $replace = str_replace('%series_title_linked%', the_series_title($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), $replace); if( stristr($replace, '%series_description%') ) $replace = str_replace('%series_description%', series_description($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); return $replace; } /*----------------------POST RELATED FUNCTIONS (i.e. query etc. see post.php)--------------------*/ //will have to add the following function for deleting the series relationship when a post is deleted. function delete_series_post_relationship($postid) { wp_delete_object_term_relationships($postid, 'series'); } //call up series post is associated with -- needed for the post-edit panel specificaly. function wp_get_post_series( $post_id = 0, $args = array() ) { $post_id = (int) $post_id; $defaults = array('fields' => 'ids'); $args = wp_parse_args( $args, $defaults); $series = wp_get_object_terms($post_id, 'series', $args); return $series; } //TODO ? have to figure out how to get this added to the wp_get_single_post call (post.php - line 577) function wp_get_single_post_series($postid = 0, $mode = OBJECT) { global $wpdb; $postid = (int) $postid; $post = get_post($postid, $mode); //set series if($mode == OBJECT) { $post->series = wp_get_post_series($postid, array('fields' => 'names')); } else { $post['series'] = wp_get_post_series($postid, array('fields' => 'names')); } return $post; } //function to set the order that the post is in a series. function set_series_order($postid = 0, $series_part = 0, $series_id) { if ( !isset($series_id) ) return false; // if post doesn't belong to a series yet. $post_ids_in_series = get_objects_in_term($series_id, 'series'); $total_posts = count($post_ids_in_series); if (!isset($total_posts) || ($total_posts < $series_part) || $series_part == 0 || $total_posts == 1) { if ($total_posts >=1) $series_part = $total_posts; } $series_posts = array(); $series_posts = get_series_order($post_ids_in_series, $postid); $ticker = 1; $count = count($series_posts); if ($count >= 1) { foreach ($series_posts as $sposts) { $currentpart = $sposts['part']; $spostid = $sposts['id']; if (( $ticker >= 1) && ( $series_part > 2 ) && ( ($series_part - $currentpart) >= 1) && $drop ) { $newpart = ($currentpart - 1); $drop = TRUE; } if ( ( $ticker == 1 ) && ( $currentpart == 2 ) && ($series_part != $currentpart) && ($count >= 2 ) && !$rise ) { $newpart = ($currentpart - 1); $drop = TRUE; } if ( ( $ticker == 1 ) && ( $series_part == $currentpart ) && ( $series_part == 2 ) && !$rise ) { $newpart = ($currentpart - 1); $drop = TRUE; } if ( ($series_part == $currentpart) && ( $series_part <= $count ) && ( $series_part > 1 ) && ($series_part != 2 ) && $drop ) $newpart = ($currentpart - 1); if ( ( ($series_part == 1 ) && ($series_part >= $currentpart) ) || ( ( $series_part == $currentpart ) && !$drop && ($currentpart - $oldpart) < 2 ) || ( ( $series_part < $currentpart ) && ( $currentpart == $oldpart ) && !$drop ) ) { $newpart = ($currentpart + 1); $rise = TRUE; } if ( ($series_part == $currentpart) && ($series_part > ( $count - 2 ) ) && ($series_part != 1) && !$drop && !$rise ) { $newpart = ($currentpart - 1); $drop = TRUE; } if ( ($series_part == $currentpart) && ($series_part > ( $count - 2 ) ) && ($ticker == $count ) && ($series_part != 1) && !$rise ) { $newpart = ($currentpart - 1); $drop = TRUE; } if (!isset($newpart)) $newpart = $currentpart; if ( isset($oldpart) && ($newpart - $oldpart) > 1 && !$drop && !$rise && ($newpart != ($count + 1) ) ) { $newpart = ($currentpart - 1); $drop = TRUE; } delete_post_meta($spostid, SERIES_PART_KEY); add_post_meta($spostid, SERIES_PART_KEY, $newpart); $ticker++; $oldpart = $newpart; unset($newpart); } } delete_post_meta($postid, SERIES_PART_KEY); add_post_meta($postid, SERIES_PART_KEY, $series_part); return true; } function wp_reset_series_order_meta_cache ($post_id = 0, $series_id = 0, $reset = FALSE) { if ( 0 == $series_id ) return false; //post is not a part of a series so no need to waste cycles. $post_ids_in_series = get_objects_in_term($series_id, 'series'); $addvalue = 1; $series_posts = get_series_order($post_ids_in_series, $post_id); if ($reset) { foreach ($post_ids_in_series as $spost) { delete_post_meta($spost['object_id'], SERIES_PART_KEY); } return true; } foreach ($series_posts as $spost) { $newpart = $addvalue; delete_post_meta($spost['id'], SERIES_PART_KEY); add_post_meta($spost['id'], SERIES_PART_KEY, $newpart); $addvalue++; } return true; } function add_series_wp_title( $title ) { global $wpdb, $wp_locale, $wp_query; $series = get_query_var(SERIES_QUERYVAR); $title = ''; if ( !empty($series) ) { $series = get_term($series,'series', OBJECT, 'display'); if ( is_wp_error($series) ) return $series; if ( ! empty($series->name) ) $title = apply_filters('single_series_title', $series->name); } return $title; } add_filter('wp_title', 'add_series_wp_title'); function single_series_title($prefix = '', $display = true) { $series_id = get_query_var(SERIES_QUERYVAR); $serchk = is_term( $series_id, SERIES_QUERYVAR ); if ( !empty($serchk) ) { $series_id = $serchk['term_id']; } if ( !empty($series_id) ) { $my_series = &get_term($series_id, 'series', OBJECT, 'display'); if ( is_wp_error( $my_series ) ) return false; $my_series_name = apply_filters('single_tag_title', $my_series->name); if ( !empty($my_series_name) ) { if ( $display ) echo $prefix, $my_series_name; else return $my_series_name; } } } //following is modified from wp_dropdown_categories() function wp_dropdown_series($args = '') { $defaults = array( 'show_option_all' => '', 'show_option_none' => '', 'orderby' => 'ID', 'order' => 'ASC', 'show_last_update' => 0, 'show_count' => 0, 'hide_empty' => 1, 'exclude' => '', 'echo' => 1, 'selected' => 0, 'name' => 'series', 'class' => 'postform' ); $defaults['selected'] = ( is_series() ) ? get_query_var('series') : 0; $r = wp_parse_args( $args, $defaults ); $r['include_last_update_time'] = $r['show_last_update']; extract( $r ); $serieslist = get_series($r); $output = ''; if ( ! empty($serieslist) ) { $output = "\n"; } if ( empty( $serieslist ) ) { $output = '\n"; } $output = apply_filters('wp_dropdown_series', $output); if ( $echo ) echo $output; return $output; } function walk_series_dropdown_tree($serieslist, $r) { $series_name = apply_filters('list_series', $serieslist->name, $serieslist); $output .= "\t\n"; return $output; } function wp_list_series($args = '') { $defaults = array( 'show_option_all' => '', 'orderby' => 'name', 'order' => 'ASC', 'show_last_update' => 0, 'style' => 'list', 'show_count' => 0, 'hide_empty' => 1, 'use_desc_for_title' => 1, 'feed' => '', 'feed_image' => '', 'exclude' => '', 'title_li' => __('Series'), 'echo' => 1 ); $r = wp_parse_args( $args, $defaults ); if ( isset( $r['show_date'] ) ) { $r['include_last_update_time'] = $r['show_date']; } extract( $r ); $serieslist = get_series($r); $output = ''; if ( $title_li && 'list' == $style ) $output = '