| 912 | * Get adjacent post relational link. |
| 913 | * |
| 914 | * Can either be next or previous post relational link. |
| 915 | * |
| 916 | * @since 2.8.0 |
| 917 | * |
| 918 | * @param string $title Optional. Link title format. |
| 919 | * @param bool $in_same_cat Optional. Whether link should be in same category. |
| 920 | * @param string $excluded_categories Optional. Excluded categories IDs. |
| 921 | * @param bool $previous Optional, default is true. Whether display link to previous post. |
| 922 | * @return string |
| 923 | */ |
| 924 | function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) { |
| 925 | if ( $previous && is_attachment() ) |
| 926 | $post = & get_post($GLOBALS['post']->post_parent); |
| 927 | else |
| 928 | $post = get_adjacent_post($in_same_cat,$excluded_categories,$previous); |
| 929 | |
| 930 | if ( empty($post) ) |
| 931 | return; |
| 932 | |
| 933 | if ( empty($post->post_title) ) |
| 934 | $post->post_title = $previous ? __('Previous Post') : __('Next Post'); |
| 935 | |
| 936 | $date = mysql2date(get_option('date_format'), $post->post_date); |
| 937 | |
| 938 | $title = str_replace('%title', $post->post_title, $title); |
| 939 | $title = str_replace('%date', $date, $title); |
| 940 | $title = apply_filters('the_title', $title, $post); |
| 941 | |
| 942 | $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; |
| 943 | $link .= $title; |
| 944 | $link .= "' href='" . get_permalink($post) . "' />\n"; |
| 945 | |
| 946 | $adjacent = $previous ? 'previous' : 'next'; |
| 947 | return apply_filters( "{$adjacent}_post_rel_link", $link ); |
| 948 | } |
| 949 | |
| 950 | /** |
| 951 | * Display relational links for the posts adjacent to the current post. |
| 952 | * |
| 953 | * @since 2.8.0 |
| 954 | * |
| 955 | * @param string $title Optional. Link title format. |
| 956 | * @param bool $in_same_cat Optional. Whether link should be in same category. |
| 957 | * @param string $excluded_categories Optional. Excluded categories IDs. |
| 958 | */ |
| 959 | function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { |
| 960 | echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true); |
| 961 | echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false); |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * Display relational link for the next post adjacent to the current post. |
| 966 | * |
| 967 | * @since 2.8.0 |
| 968 | * |
| 969 | * @param string $title Optional. Link title format. |
| 970 | * @param bool $in_same_cat Optional. Whether link should be in same category. |
| 971 | * @param string $excluded_categories Optional. Excluded categories IDs. |
| 972 | */ |
| 973 | function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { |
| 974 | echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false); |
| 975 | } |
| 976 | |
| 977 | /** |
| 978 | * Display relational link for the previous post adjacent to the current post. |
| 979 | * |
| 980 | * @since 2.8.0 |
| 981 | * |
| 982 | * @param string $title Optional. Link title format. |
| 983 | * @param bool $in_same_cat Optional. Whether link should be in same category. |
| 984 | * @param string $excluded_categories Optional. Excluded categories IDs. |
| 985 | */ |
| 986 | function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { |
| 987 | echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true); |
| 988 | } |
| 989 | |
| 990 | /** |
| 991 | * Retrieve boundary post. |
| 992 | * |
| 993 | * Boundary being either the first or last post by publish date within the contraitns specified |
| 994 | * by in same category or excluded categories. |
| 995 | * |
| 996 | * @since 2.8.0 |
| 997 | * |
| 998 | * @param bool $in_same_cat Optional. Whether returned post should be in same category. |
| 999 | * @param string $excluded_categories Optional. Excluded categories IDs. |
| 1000 | * @param bool $previous Optional. Whether to retrieve first post. |
| 1001 | * @return object |
| 1002 | */ |
| 1003 | function get_boundary_post($in_same_cat = false, $excluded_categories = '', $start = true) { |
| 1004 | global $post, $wpdb; |
| 1005 | |
| 1006 | if( empty($post) || !is_single() || is_attachment() ) |
| 1007 | return null; |
| 1008 | |
| 1009 | $join = ''; |
| 1010 | $posts_in_ex_cats_sql = ''; |
| 1011 | if ( $in_same_cat || !empty($excluded_categories) ) { |
| 1012 | $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"; |
| 1013 | |
| 1014 | if ( $in_same_cat ) { |
| 1015 | $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids'); |
| 1016 | $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")"; |
| 1017 | } |
| 1018 | |
| 1019 | $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'"; |
| 1020 | if ( !empty($excluded_categories) ) { |
| 1021 | $excluded_categories = array_map('intval', explode(' and ', $excluded_categories)); |
| 1022 | if ( !empty($cat_array) ) { |
| 1023 | $excluded_categories = array_diff($excluded_categories, $cat_array); |
| 1024 | $posts_in_ex_cats_sql = ''; |
| 1025 | } |
| 1026 | |
| 1027 | if ( !empty($excluded_categories) ) { |
| 1028 | $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')'; |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | $boundary = $start ? 'start' : 'end'; |
| 1034 | $order = $start ? 'ASC' : 'DESC'; |
| 1035 | |
| 1036 | $join = apply_filters( "get_{$boundary}_post_join", $join, $in_same_cat, $excluded_categories ); |
| 1037 | $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 ); |
| 1038 | $sort = apply_filters( "get_{$boundary}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); |
| 1039 | |
| 1040 | return $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort"); |
| 1041 | } |
| 1042 | |
| 1043 | /** |
| 1044 | * Get boundary post relational link. |
| 1045 | * |
| 1046 | * Can either be start or end post relational link. |
| 1047 | * |
| 1048 | * @since 2.8.0 |
| 1049 | * |
| 1050 | * @param string $title Optional. Link title format. |
| 1051 | * @param bool $in_same_cat Optional. Whether link should be in same category. |
| 1052 | * @param string $excluded_categories Optional. Excluded categories IDs. |
| 1053 | * @param bool $start Optional, default is true. Whether display link to first post. |
| 1054 | * @return string |
| 1055 | */ |
| 1056 | function get_boundary_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $start = true) { |
| 1057 | $post = get_boundary_post($in_same_cat,$excluded_categories,$start); |
| 1058 | |
| 1059 | if ( empty($post) ) |
| 1060 | return; |
| 1061 | |
| 1062 | if ( empty($post->post_title) ) |
| 1063 | $post->post_title = $start ? __('First Post') : __('Last Post'); |
| 1064 | |
| 1065 | $date = mysql2date(get_option('date_format'), $post->post_date); |
| 1066 | |
| 1067 | $title = str_replace('%title', $post->post_title, $title); |
| 1068 | $title = str_replace('%date', $date, $title); |
| 1069 | $title = apply_filters('the_title', $title, $post); |
| 1070 | |
| 1071 | $link = $start ? "<link rel='start' title='" : "<link rel='end' title='"; |
| 1072 | $link .= $title; |
| 1073 | $link .= "' href='" . get_permalink($post) . "' />\n"; |
| 1074 | |
| 1075 | $boundary = $start ? 'start' : 'end'; |
| 1076 | return apply_filters( "{$boundary}_post_rel_link", $link ); |
| 1077 | } |
| 1078 | |
| 1079 | /** |
| 1080 | * Display relational link for the first post. |
| 1081 | * |
| 1082 | * @since 2.8.0 |
| 1083 | * |
| 1084 | * @param string $title Optional. Link title format. |
| 1085 | * @param bool $in_same_cat Optional. Whether link should be in same category. |
| 1086 | * @param string $excluded_categories Optional. Excluded categories IDs. |
| 1087 | */ |
| 1088 | function start_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { |
| 1089 | echo get_boundary_post_rel_link($title, $in_same_cat, $excluded_categories, true); |
| 1090 | } |
| 1091 | |
| 1092 | /** |
| 1093 | * Get site index relational link. |
| 1094 | * |
| 1095 | * @since 2.8.0 |
| 1096 | * |
| 1097 | * @return string |
| 1098 | */ |
| 1099 | function get_index_rel_link() { |
| 1100 | $link = "<link rel='index' title='" . get_bloginfo('name') . "' href='" . get_bloginfo('siteurl') . "' />\n"; |
| 1101 | return apply_filters( "index_rel_link", $link ); |
| 1102 | } |
| 1103 | |
| 1104 | /** |
| 1105 | * Display relational link for the site index. |
| 1106 | * |
| 1107 | * @since 2.8.0 |
| 1108 | */ |
| 1109 | function index_rel_link() { |
| 1110 | echo get_index_rel_link(); |
| 1111 | } |
| 1112 | |
| 1113 | /** |
| 1114 | * Get parent post relational link. |
| 1115 | * |
| 1116 | * @since 2.8.0 |
| 1117 | * |
| 1118 | * @param string $title Optional. Link title format. |
| 1119 | * @return string |
| 1120 | */ |
| 1121 | function get_parent_post_rel_link($title = '%title') { |
| 1122 | if ( ! empty( $GLOBALS['post'] ) && ! empty( $GLOBALS['post']->post_parent ) ) |
| 1123 | $post = & get_post($GLOBALS['post']->post_parent); |
| 1124 | |
| 1125 | if ( empty($post) ) |
| 1126 | return; |
| 1127 | |
| 1128 | $date = mysql2date(get_option('date_format'), $post->post_date); |
| 1129 | |
| 1130 | $title = str_replace('%title', $post->post_title, $title); |
| 1131 | $title = str_replace('%date', $date, $title); |
| 1132 | $title = apply_filters('the_title', $title, $post); |
| 1133 | |
| 1134 | $link = "<link rel='up' title='"; |
| 1135 | $link .= $title; |
| 1136 | $link .= "' href='" . get_permalink($post) . "' />\n"; |
| 1137 | |
| 1138 | return apply_filters( "parent_post_rel_link", $link ); |
| 1139 | } |
| 1140 | |
| 1141 | /** |
| 1142 | * Display relational link for parent item |
| 1143 | * |
| 1144 | * @since 2.8.0 |
| 1145 | */ |
| 1146 | function parent_post_rel_link($title = '%title') { |
| 1147 | echo get_parent_post_rel_link($title); |
| 1148 | } |
| 1149 | |
| 1150 | /** |