+ $adjacent = $previous ? 'previous' : 'next';
+ $op = $previous ? '<' : '>';
+ $order = $previous ? 'DESC' : 'ASC';
+
+ $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
+ $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_terms_sql", $current_post_date, $post->post_type), $in_same_term, $excluded_terms );
+ $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
+
+ $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
+ $query_key = 'adjacent_post_' . md5( $query );
+ $result = wp_cache_get( $query_key, 'counts' );
+ if ( false !== $result ) {
+ if ( $result )
+ $result = get_post( $result );
+ return $result;
+ }
+
+ $result = $wpdb->get_var( $query );
+ if ( null === $result )
+ $result = '';
+
+ wp_cache_set( $query_key, $result, 'counts' );
+
+ if ( $result )
+ $result = get_post( $result );
+
+ return $result;
+}
+
+/**
+ * Get adjacent post relational link.
+ *
+ * Can either be next or previous post relational link.
+ *
+ * @since 2.8.0
+ *
+ * @param string $title Optional. Link title format.
+ * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
+ * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ * @return string
+ */
+function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
+ if ( $previous && is_attachment() && $post = get_post() )
+ $post = get_post( $post->post_parent );
+ else
+ $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
+
+ if ( empty( $post ) )
+ return;
+
+ $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
+
+ if ( empty( $post_title ) )
+ $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
+
+ $date = mysql2date( get_option( 'date_format' ), $post->post_date );
+
+ $title = str_replace( '%title', $post_title, $title );
+ $title = str_replace( '%date', $date, $title );
+
+ $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
+ $link .= esc_attr( $title );
+ $link .= "' href='" . get_permalink( $post ) . "' />\n";
+
+ $adjacent = $previous ? 'previous' : 'next';
+ return apply_filters( "{$adjacent}_post_rel_link", $link );
+}
+
+/**
+ * Display relational links for the posts adjacent to the current post.
+ *
+ * @since 2.8.0
+ *
+ * @param string $title Optional. Link title format.
+ * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ */
+function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
+ echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy );
+ echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy );
+}
+
+/**
+ * Display relational links for the posts adjacent to the current post for single post pages.
+ *
+ * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
+ * @since 3.0.0
+ *
+ */
+function adjacent_posts_rel_link_wp_head() {
+ if ( !is_singular() || is_attachment() )
+ return;
+ adjacent_posts_rel_link();
+}
+
+/**
+ * Display relational link for the next post adjacent to the current post.
+ *
+ * @since 2.8.0
+ *
+ * @param string $title Optional. Link title format.
+ * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ */
+function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
+ echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy );
+}
+
+/**
+ * Display relational link for the previous post adjacent to the current post.
+ *
+ * @since 2.8.0
+ *
+ * @param string $title Optional. Link title format.
+ * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ */
+function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
+ echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy );
+}
+
+/**
+ * Retrieve boundary post.
+ *
+ * Boundary being either the first or last post by publish date within the constraints specified
+ * by $in_same_term or $excluded_terms.
+ *
+ * @since 2.8.0
+ *
+ * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
+ * @param bool $start Optional. Whether to retrieve first or last post.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ * @return object
+ */
+function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
+ $post = get_post();
+ if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
+ return null;
+
+ $query_args = array(
+ 'posts_per_page' => 1,
+ 'order' => $start ? 'ASC' : 'DESC',
+ 'update_post_term_cache' => false,
+ 'update_post_meta_cache' => false
+ );
+
+ $term_array = array();
+
+ if ( ! is_array( $excluded_terms ) ) {
+ if ( ! empty( $excluded_terms ) )
+ $excluded_terms = explode( ',', $excluded_terms );
+ else
+ $excluded_terms = array();
+ }
+
+ if ( $in_same_term || ! empty( $excluded_terms ) ) {
+ if ( $in_same_term )
+ $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
+
+ if ( ! empty( $excluded_terms ) ) {
+ $excluded_terms = array_map( 'intval', $excluded_terms );
+ $excluded_terms = array_diff( $excluded_terms, $term_array );
+
+ $inverse_terms = array();
+ foreach ( $excluded_terms as $excluded_term )
+ $inverse_terms[] = $excluded_term * -1;
+ $excluded_terms = $inverse_terms;
+ }
+
+ $query_args[ 'tax_query' ] = array( array(
+ 'taxonomy' => $taxonomy,
+ 'terms' => array_merge( $term_array, $excluded_terms )
+ ) );
+ }
+
+ return get_posts( $query_args );
+}
+
+/*
+ * Get previous post link that is adjacent to the current post.
+ *
+ * @since 3.7.0
+ *
+ * @param string $format Optional. Link anchor format.
+ * @param string $link Optional. Link permalink format.
+ * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ * @return string
+ */
+function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
+ return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, true, $taxonomy );
+}
+
+/**
+ * Display previous post link that is adjacent to the current post.
+ *
+ * @since 1.5.0
+ * @see get_previous_post_link()
+ *
+ * @param string $format Optional. Link anchor format.
+ * @param string $link Optional. Link permalink format.
+ * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ */
+function previous_post_link( $format = '« %link', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
+ echo get_previous_post_link( $format, $link, $in_same_cat, $excluded_terms, $taxonomy );
+}
+
+/**
+ * Get next post link that is adjacent to the current post.
+ *
+ * @since 3.7.0
+ *
+ * @param string $format Optional. Link anchor format.
+ * @param string $link Optional. Link permalink format.
+ * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ * @return string
+ */
+function get_next_post_link( $format = '%link »', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
+ return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, false, $taxonomy );
+}
+
+/**
+ * Display next post link that is adjacent to the current post.
+ *
+ * @since 1.5.0
+ * @see get_next_post_link()
+ *
+ * @param string $format Optional. Link anchor format.
+ * @param string $link Optional. Link permalink format.
+ * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ */
+function next_post_link( $format = '%link »', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
+ echo get_next_post_link( $format, $link, $in_same_cat, $excluded_terms, $taxonomy );
+}
+
+/**
+ * Get adjacent post link.
+ *
+ * Can be either next post link or previous.
+ *
+ * @since 3.7.0
+ *
+ * @param string $format Link anchor format.
+ * @param string $link Link permalink format.
+ * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs.
+ * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ * @return string
+ */
+function get_adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
+ if ( $previous && is_attachment() )
+ $post = get_post( get_post()->post_parent );
+ else
+ $post = get_adjacent_post( $in_same_cat, $excluded_terms, $previous, $taxonomy );
+
+ if ( ! $post ) {
+ $output = '';
+ } else {
+ $title = $post->post_title;
+
+ if ( empty( $post->post_title ) )
+ $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
+
+ /** This filter is documented in wp-includes/post-template.php */
+ $title = apply_filters( 'the_title', $title, $post->ID );
+ $date = mysql2date( get_option( 'date_format' ), $post->post_date );
+ $rel = $previous ? 'prev' : 'next';
+
+ $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
+ $inlink = str_replace( '%title', $title, $link );
+ $inlink = str_replace( '%date', $date, $inlink );
+ $inlink = $string . $inlink . '</a>';
+
+ $output = str_replace( '%link', $inlink, $format );
+ }
+
+ $adjacent = $previous ? 'previous' : 'next';
+
+ return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post );
+}
+
+/**
+ * Display adjacent post link.
+ *
+ * Can be either next post link or previous.
+ *
+ * @since 2.5.0
+ * @uses get_adjacent_post_link()
+ *
+ * @param string $format Link anchor format.
+ * @param string $link Link permalink format.
+ * @param bool $in_same_cat Optional. Whether link should be in a same category.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs.
+ * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ * @return string
+ */
+function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
+ echo get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, $previous, $taxonomy );
+}
+
+/**
+ * Retrieve links for page numbers.
+ *
+ * @since 1.5.0
+ *
+ * @param int $pagenum Optional. Page ID.
+ * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
+* Otherwise, prepares the URL with esc_url_raw().
+ * @return string
+ */
+function get_pagenum_link($pagenum = 1, $escape = true ) {
+ global $wp_rewrite;
+
+ $pagenum = (int) $pagenum;
+
+ $request = remove_query_arg( 'paged' );
+
+ $home_root = parse_url(home_url());
+ $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
+ $home_root = preg_quote( $home_root, '|' );
+
+ $request = preg_replace('|^'. $home_root . '|i', '', $request);
+ $request = preg_replace('|^/+|', '', $request);
+
+ if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
+ $base = trailingslashit( get_bloginfo( 'url' ) );
+
+ if ( $pagenum > 1 ) {
+ $result = add_query_arg( 'paged', $pagenum, $base . $request );
+ } else {
+ $result = $base . $request;
+ }
+ } else {
+ $qs_regex = '|\?.*?$|';
+ preg_match( $qs_regex, $request, $qs_match );
+
+ if ( !empty( $qs_match[0] ) ) {
+ $query_string = $qs_match[0];
+ $request = preg_replace( $qs_regex, '', $request );
+ } else {
+ $query_string = '';
+ }
+
+ $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
+ $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
+ $request = ltrim($request, '/');
+
+ $base = trailingslashit( get_bloginfo( 'url' ) );
+
+ if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
+ $base .= $wp_rewrite->index . '/';
+
+ if ( $pagenum > 1 ) {
+ $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
+ }
+
+ $result = $base . $request . $query_string;
+ }
+
+ $result = apply_filters('get_pagenum_link', $result);
+
+ if ( $escape )
+ return esc_url( $result );
+ else
+ return esc_url_raw( $result );
+}
+
+/**
+ * Retrieve next posts page link.
+ *
+ * Backported from 2.1.3 to 2.0.10.
+ *
+ * @since 2.0.10
+ *
+ * @param int $max_page Optional. Max pages.
+ * @return string
+ */
+function get_next_posts_page_link($max_page = 0) {
+ global $paged;
+
+ if ( !is_single() ) {
+ if ( !$paged )
+ $paged = 1;
+ $nextpage = intval($paged) + 1;
+ if ( !$max_page || $max_page >= $nextpage )
+ return get_pagenum_link($nextpage);
+ }
+}
+
+/**
+ * Display or return the next posts page link.
+ *
+ * @since 0.71
+ *
+ * @param int $max_page Optional. Max pages.
+ * @param boolean $echo Optional. Echo or return;
+ */
+function next_posts( $max_page = 0, $echo = true ) {
+ $output = esc_url( get_next_posts_page_link( $max_page ) );
+
+ if ( $echo )
+ echo $output;
+ else
+ return $output;
+}
+
+/**
+ * Return the next posts page link.
+ *
+ * @since 2.7.0
+ *
+ * @param string $label Content for link text.
+ * @param int $max_page Optional. Max pages.
+ * @return string|null
+ */
+function get_next_posts_link( $label = null, $max_page = 0 ) {
+ global $paged, $wp_query;
+
+ if ( !$max_page )
+ $max_page = $wp_query->max_num_pages;
+
+ if ( !$paged )
+ $paged = 1;
+
+ $nextpage = intval($paged) + 1;
+
+ if ( null === $label )
+ $label = __( 'Next Page »' );
+
+ if ( !is_single() && ( $nextpage <= $max_page ) ) {
+ $attr = apply_filters( 'next_posts_link_attributes', '' );
+ return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';
+ }
+}
+
+/**
+ * Display the next posts page link.
+ *
+ * @since 0.71
+ * @uses get_next_posts_link()
+ *
+ * @param string $label Content for link text.
+ * @param int $max_page Optional. Max pages.
+ */
+function next_posts_link( $label = null, $max_page = 0 ) {
+ echo get_next_posts_link( $label, $max_page );
+}
+
+/**
+ * Retrieve previous posts page link.
+ *
+ * Will only return string, if not on a single page or post.
+ *
+ * Backported to 2.0.10 from 2.1.3.
+ *
+ * @since 2.0.10
+ *
+ * @return string|null
+ */
+function get_previous_posts_page_link() {
+ global $paged;
+
+ if ( !is_single() ) {
+ $nextpage = intval($paged) - 1;
+ if ( $nextpage < 1 )
+ $nextpage = 1;
+ return get_pagenum_link($nextpage);
+ }
+}
+
+/**
+ * Display or return the previous posts page link.
+ *
+ * @since 0.71
+ *
+ * @param boolean $echo Optional. Echo or return;
+ */
+function previous_posts( $echo = true ) {
+ $output = esc_url( get_previous_posts_page_link() );
+
+ if ( $echo )
+ echo $output;
+ else
+ return $output;
+}
+
+/**
+ * Return the previous posts page link.
+ *
+ * @since 2.7.0
+ *
+ * @param string $label Optional. Previous page link text.
+ * @return string|null
+ */
+function get_previous_posts_link( $label = null ) {
+ global $paged;
+
+ if ( null === $label )
+ $label = __( '« Previous Page' );
+
+ if ( !is_single() && $paged > 1 ) {
+ $attr = apply_filters( 'previous_posts_link_attributes', '' );
+ return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) .'</a>';
+ }
+}
+
+/**
+ * Display the previous posts page link.
+ *
+ * @since 0.71
+ * @uses get_previous_posts_link()
+ *
+ * @param string $label Optional. Previous page link text.
+ */
+function previous_posts_link( $label = null ) {
+ echo get_previous_posts_link( $label );
+}
+
+/**
+ * Return post pages link navigation for previous and next pages.
+ *
+ * @since 2.8
+ *
+ * @param string|array $args Optional args.
+ * @return string The posts link navigation.
+ */
+function get_posts_nav_link( $args = array() ) {
+ global $wp_query;
+
+ $return = '';
+
+ if ( !is_singular() ) {
+ $defaults = array(
+ 'sep' => ' — ',
+ 'prelabel' => __('« Previous Page'),
+ 'nxtlabel' => __('Next Page »'),
+ );
+ $args = wp_parse_args( $args, $defaults );
+
+ $max_num_pages = $wp_query->max_num_pages;
+ $paged = get_query_var('paged');
+
+ //only have sep if there's both prev and next results
+ if ($paged < 2 || $paged >= $max_num_pages) {
+ $args['sep'] = '';
+ }
+
+ if ( $max_num_pages > 1 ) {
+ $return = get_previous_posts_link($args['prelabel']);
+ $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $args['sep']);
+ $return .= get_next_posts_link($args['nxtlabel']);
+ }
+ }
+ return $return;
+
+}
+
+/**
+ * Display post pages link navigation for previous and next pages.
+ *
+ * @since 0.71
+ *
+ * @param string $sep Optional. Separator for posts navigation links.
+ * @param string $prelabel Optional. Label for previous pages.
+ * @param string $nxtlabel Optional Label for next pages.
+ */
+function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
+ $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
+ echo get_posts_nav_link($args);
+}
+
+/**
+ * Retrieve comments page number link.
+ *
+ * @since 2.7.0
+ *
+ * @param int $pagenum Optional. Page number.
+ * @return string
+ */
+function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
+ global $wp_rewrite;
+
+ $pagenum = (int) $pagenum;
+
+ $result = get_permalink();
+
+ if ( 'newest' == get_option('default_comments_page') ) {
+ if ( $pagenum != $max_page ) {
+ if ( $wp_rewrite->using_permalinks() )
+ $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
+ else
+ $result = add_query_arg( 'cpage', $pagenum, $result );
+ }
+ } elseif ( $pagenum > 1 ) {
+ if ( $wp_rewrite->using_permalinks() )
+ $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
+ else
+ $result = add_query_arg( 'cpage', $pagenum, $result );
+ }
+
+ $result .= '#comments';
+
+ $result = apply_filters('get_comments_pagenum_link', $result);
+
+ return $result;
+}
+
+/**
+ * Return the link to next comments page.
+ *
+ * @since 2.7.1
+ *
+ * @param string $label Optional. Label for link text.
+ * @param int $max_page Optional. Max page.
+ * @return string|null
+ */
+function get_next_comments_link( $label = '', $max_page = 0 ) {
+ global $wp_query;
+
+ if ( !is_singular() || !get_option('page_comments') )
+ return;
+
+ $page = get_query_var('cpage');
+
+ $nextpage = intval($page) + 1;
+
+ if ( empty($max_page) )
+ $max_page = $wp_query->max_num_comment_pages;
+
+ if ( empty($max_page) )
+ $max_page = get_comment_pages_count();
+
+ if ( $nextpage > $max_page )
+ return;
+
+ if ( empty($label) )
+ $label = __('Newer Comments »');
+
+ return '<a href="' . esc_url( get_comments_pagenum_link( $nextpage, $max_page ) ) . '" ' . apply_filters( 'next_comments_link_attributes', '' ) . '>'. preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) .'</a>';
+}
+
+/**
+ * Display the link to next comments page.
+ *
+ * @since 2.7.0
+ *
+ * @param string $label Optional. Label for link text.
+ * @param int $max_page Optional. Max page.
+ */
+function next_comments_link( $label = '', $max_page = 0 ) {
+ echo get_next_comments_link( $label, $max_page );
+}
+
+/**
+ * Return the previous comments page link.
+ *
+ * @since 2.7.1
+ *
+ * @param string $label Optional. Label for comments link text.
+ * @return string|null
+ */
+function get_previous_comments_link( $label = '' ) {
+ if ( !is_singular() || !get_option('page_comments') )
+ return;
+
+ $page = get_query_var('cpage');
+
+ if ( intval($page) <= 1 )
+ return;
+
+ $prevpage = intval($page) - 1;
+
+ if ( empty($label) )
+ $label = __('« Older Comments');
+
+ return '<a href="' . esc_url( get_comments_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_comments_link_attributes', '' ) . '>' . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) .'</a>';
+}
+
+/**
+ * Display the previous comments page link.
+ *
+ * @since 2.7.0
+ *
+ * @param string $label Optional. Label for comments link text.
+ */
+function previous_comments_link( $label = '' ) {
+ echo get_previous_comments_link( $label );
+}
+
+/**
+ * Create pagination links for the comments on the current post.
+ *
+ * @see paginate_links()
+ * @since 2.7.0
+ *
+ * @param string|array $args Optional args. See paginate_links().
+ * @return string Markup for pagination links.
+*/
+function paginate_comments_links($args = array()) {
+ global $wp_rewrite;
+
+ if ( !is_singular() || !get_option('page_comments') )
+ return;
+
+ $page = get_query_var('cpage');
+ if ( !$page )
+ $page = 1;
+ $max_page = get_comment_pages_count();
+ $defaults = array(
+ 'base' => add_query_arg( 'cpage', '%#%' ),
+ 'format' => '',
+ 'total' => $max_page,
+ 'current' => $page,
+ 'echo' => true,
+ 'add_fragment' => '#comments'
+ );
+ if ( $wp_rewrite->using_permalinks() )
+ $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . 'comment-page-%#%', 'commentpaged');
+
+ $args = wp_parse_args( $args, $defaults );
+ $page_links = paginate_links( $args );
+
+ if ( $args['echo'] )
+ echo $page_links;
+ else
+ return $page_links;
+}
+
+/**
+ * Retrieve the Press This bookmarklet link.
+ *
+ * Use this in 'a' element 'href' attribute.
+ *
+ * @since 2.6.0
+ *
+ * @return string
+ */
+function get_shortcut_link() {
+ // In case of breaking changes, version this. #WP20071
+ $link = "javascript:
+ var d=document,
+ w=window,
+ e=w.getSelection,
+ k=d.getSelection,
+ x=d.selection,
+ s=(e?e():(k)?k():(x?x.createRange().text:0)),
+ f='" . admin_url('press-this.php') . "',
+ l=d.location,
+ e=encodeURIComponent,
+ u=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=4';
+ a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'))l.href=u;};
+ if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();
+ void(0)";
+
+ $link = str_replace(array("\r", "\n", "\t"), '', $link);
+
+ return apply_filters('shortcut_link', $link);
+}
+
+/**
+ * Retrieve the home url for the current site.
+ *
+ * Returns the 'home' option with the appropriate protocol, 'https' if
+ * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
+ * overridden.
+ *
+ * @package WordPress
+ * @since 3.0.0
+ *
+ * @uses get_home_url()
+ *
+ * @param string $path (optional) Path relative to the home url.
+ * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
+ * @return string Home url link with optional path appended.
+*/
+function home_url( $path = '', $scheme = null ) {
+ return get_home_url( null, $path, $scheme );
+}
+
+/**
+ * Retrieve the home url for a given site.
+ *
+ * Returns the 'home' option with the appropriate protocol, 'https' if
+ * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
+ * overridden.
+ *
+ * @package WordPress
+ * @since 3.0.0
+ *
+ * @param int $blog_id (optional) Blog ID. Defaults to current blog.
+ * @param string $path (optional) Path relative to the home url.
+ * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
+ * @return string Home url link with optional path appended.
+*/
+function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
+ $orig_scheme = $scheme;
+
+ if ( empty( $blog_id ) || !is_multisite() ) {
+ $url = get_option( 'home' );
+ } else {
+ switch_to_blog( $blog_id );
+ $url = get_option( 'home' );
+ restore_current_blog();
+ }
+
+ if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
+ if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] )
+ $scheme = 'https';
+ else
+ $scheme = parse_url( $url, PHP_URL_SCHEME );
+ }
+
+ $url = set_url_scheme( $url, $scheme );