| 981 | * Get adjacent post relational link. |
| 982 | * |
| 983 | * Can either be next or previous post relational link. |
| 984 | * |
| 985 | * @since 2.8.0 |
| 986 | * |
| 987 | * @param string $title Optional. Link title format. |
| 988 | * @param bool $in_same_cat Optional. Whether link should be in same category. |
| 989 | * @param string $excluded_categories Optional. Excluded categories IDs. |
| 990 | * @param bool $previous Optional, default is true. Whether display link to previous post. |
| 991 | * @return string |
| 992 | */ |
| 993 | function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) { |
| 994 | if ( $previous && is_attachment() ) |
| 995 | $post = & get_post($GLOBALS['post']->post_parent); |
| 996 | else |
| 997 | $post = get_adjacent_post($in_same_cat,$excluded_categories,$previous); |
| 998 | |
| 999 | if ( !$post ) |
| 1000 | return; |
| 1001 | |
| 1002 | if ( empty($post->post_title) ) |
| 1003 | $post->post_title = $previous ? __('Previous Post') : __('Next Post'); |
| 1004 | |
| 1005 | $date = mysql2date(get_option('date_format'), $post->post_date); |
| 1006 | |
| 1007 | $title = str_replace('%title', $post->post_title, $title); |
| 1008 | $title = str_replace('%date', $date, $title); |
| 1009 | $title = apply_filters('the_title', $title, $post); |
| 1010 | |
| 1011 | $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; |
| 1012 | $link .= $title; |
| 1013 | $link .= "' href='" . get_permalink($post) . "' />\n"; |
| 1014 | |
| 1015 | $adjacent = $previous ? 'previous' : 'next'; |
| 1016 | return apply_filters( "{$adjacent}_post_rel_link", $link ); |
| 1017 | } |
| 1018 | |
| 1019 | /** |
| 1020 | * Display relational links for the posts adjacent to the current post. |
| 1021 | * |
| 1022 | * @since 2.8.0 |
| 1023 | * |
| 1024 | * @param string $title Optional. Link title format. |
| 1025 | * @param bool $in_same_cat Optional. Whether link should be in same category. |
| 1026 | * @param string $excluded_categories Optional. Excluded categories IDs. |
| 1027 | */ |
| 1028 | function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { |
| 1029 | echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true); |
| 1030 | echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false); |
| 1031 | } |
| 1032 | |
| 1033 | /** |
| 1034 | * Display relational link for the next post adjacent to the current post. |
| 1035 | * |
| 1036 | * @since 2.8.0 |
| 1037 | * |
| 1038 | * @param string $title Optional. Link title format. |
| 1039 | * @param bool $in_same_cat Optional. Whether link should be in same category. |
| 1040 | * @param string $excluded_categories Optional. Excluded categories IDs. |
| 1041 | */ |
| 1042 | function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { |
| 1043 | echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false); |
| 1044 | } |
| 1045 | |
| 1046 | /** |
| 1047 | * Display relational link for the previous post adjacent to the current post. |
| 1048 | * |
| 1049 | * @since 2.8.0 |
| 1050 | * |
| 1051 | * @param string $title Optional. Link title format. |
| 1052 | * @param bool $in_same_cat Optional. Whether link should be in same category. |
| 1053 | * @param string $excluded_categories Optional. Excluded categories IDs. |
| 1054 | */ |
| 1055 | function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { |
| 1056 | echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true); |
| 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * Retrieve boundary post. |
| 1061 | * |
| 1062 | * Boundary being either the first or last post by publish date within the contraitns specified |
| 1063 | * by in same category or excluded categories. |
| 1064 | * |
| 1065 | * @since 2.8.0 |
| 1066 | * |
| 1067 | * @param bool $in_same_cat Optional. Whether returned post should be in same category. |
| 1068 | * @param string $excluded_categories Optional. Excluded categories IDs. |
| 1069 | * @param bool $previous Optional. Whether to retrieve first post. |
| 1070 | * @return object |
| 1071 | */ |
| 1072 | function get_boundary_post($in_same_cat = false, $excluded_categories = '', $start = true) { |
| 1073 | global $post, $wpdb; |
| 1074 | |
| 1075 | if( empty($post) || !is_single() || is_attachment() ) |
| 1076 | return null; |
| 1077 | |
| 1078 | $join = ''; |
| 1079 | $posts_in_ex_cats_sql = ''; |
| 1080 | if ( $in_same_cat || !empty($excluded_categories) ) { |
| 1081 | $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id"; |
| 1082 | |
| 1083 | if ( $in_same_cat ) { |
| 1084 | $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids'); |
| 1085 | $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")"; |
| 1086 | } |
| 1087 | |
| 1088 | $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'"; |
| 1089 | if ( !empty($excluded_categories) ) { |
| 1090 | $excluded_categories = array_map('intval', explode(' and ', $excluded_categories)); |
| 1091 | if ( !empty($cat_array) ) { |
| 1092 | $excluded_categories = array_diff($excluded_categories, $cat_array); |
| 1093 | $posts_in_ex_cats_sql = ''; |
| 1094 | } |
| 1095 | |
| 1096 | if ( !empty($excluded_categories) ) { |
| 1097 | $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')'; |
| 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | $boundary = $start ? 'start' : 'end'; |
| 1103 | $order = $start ? 'ASC' : 'DESC'; |
| 1104 | |
| 1105 | $join = apply_filters( "get_{$boundary}_post_join", $join, $in_same_cat, $excluded_categories ); |
| 1106 | $where = apply_filters( "get_{$boundary}_post_where", "WHERE p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql", $in_same_cat, $excluded_categories ); |
| 1107 | $sort = apply_filters( "get_{$boundary}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); |
| 1108 | |
| 1109 | return $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort"); |
| 1110 | } |
| 1111 | |
| 1112 | /** |
| 1113 | * Get boundary post relational link. |
| 1114 | * |
| 1115 | * Can either be start or end post relational link. |
| 1116 | * |
| 1117 | * @since 2.8.0 |
| 1118 | * |
| 1119 | * @param string $title Optional. Link title format. |
| 1120 | * @param bool $in_same_cat Optional. Whether link should be in same category. |
| 1121 | * @param string $excluded_categories Optional. Excluded categories IDs. |
| 1122 | * @param bool $start Optional, default is true. Whether display link to first post. |
| 1123 | * @return string |
| 1124 | */ |
| 1125 | function get_boundary_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $start = true) { |
| 1126 | $post = get_boundary_post($in_same_cat,$excluded_categories,$start); |
| 1127 | |
| 1128 | if ( !$post ) |
| 1129 | return; |
| 1130 | |
| 1131 | if ( empty($post->post_title) ) |
| 1132 | $post->post_title = $start ? __('First Post') : __('Last Post'); |
| 1133 | |
| 1134 | $date = mysql2date(get_option('date_format'), $post->post_date); |
| 1135 | |
| 1136 | $title = str_replace('%title', $post->post_title, $title); |
| 1137 | $title = str_replace('%date', $date, $title); |
| 1138 | $title = apply_filters('the_title', $title, $post); |
| 1139 | |
| 1140 | $link = $start ? "<link rel='start' title='" : "<link rel='end' title='"; |
| 1141 | $link .= $title; |
| 1142 | $link .= "' href='" . get_permalink($post) . "' />\n"; |
| 1143 | |
| 1144 | $boundary = $start ? 'start' : 'end'; |
| 1145 | return apply_filters( "{$boundary}_post_rel_link", $link ); |
| 1146 | } |
| 1147 | |
| 1148 | /** |
| 1149 | * Display relational link for the first post. |
| 1150 | * |
| 1151 | * @since 2.8.0 |
| 1152 | * |
| 1153 | * @param string $title Optional. Link title format. |
| 1154 | * @param bool $in_same_cat Optional. Whether link should be in same category. |
| 1155 | * @param string $excluded_categories Optional. Excluded categories IDs. |
| 1156 | */ |
| 1157 | function start_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { |
| 1158 | echo get_boundary_post_rel_link($title, $in_same_cat, $excluded_categories, true); |
| 1159 | } |
| 1160 | |
| 1161 | /** |
| 1162 | * Get site index relational link. |
| 1163 | * |
| 1164 | * @since 2.8.0 |
| 1165 | * |
| 1166 | * @return string |
| 1167 | */ |
| 1168 | function get_index_rel_link() { |
| 1169 | return "<link rel='index' title='" . get_bloginfo('name') . "' href='" . get_bloginfo('siteurl') . "' />\n"; |
| 1170 | } |
| 1171 | |
| 1172 | /** |
| 1173 | * Display relational link for the site index. |
| 1174 | * |
| 1175 | * @since 2.8.0 |
| 1176 | */ |
| 1177 | function index_rel_link() { |
| 1178 | echo get_index_rel_link(); |
| 1179 | } |
| 1180 | |
| 1181 | /** |