PHP Cross Reference of WordPress Subversion HEAD |
| [ Index ] [ Classes ] [ Functions ] [ Variables ] [ Constants ] |
[Summary view] [Print] [Text view]
1 <?php 2 3 4 function the_permalink() { 5 echo apply_filters('the_permalink', get_permalink()); 6 } 7 8 9 /** 10 * Conditionally adds a trailing slash if the permalink structure 11 * has a trailing slash, strips the trailing slash if not 12 * @global object Uses $wp_rewrite 13 * @param $string string a URL with or without a trailing slash 14 * @param $type_of_url string the type of URL being considered (e.g. single, category, etc) for use in the filter 15 * @return string 16 */ 17 function user_trailingslashit($string, $type_of_url = '') { 18 global $wp_rewrite; 19 if ( $wp_rewrite->use_trailing_slashes ) 20 $string = trailingslashit($string); 21 else 22 $string = untrailingslashit($string); 23 24 // Note that $type_of_url can be one of following: 25 // single, single_trackback, single_feed, single_paged, feed, category, page, year, month, day, paged 26 $string = apply_filters('user_trailingslashit', $string, $type_of_url); 27 return $string; 28 } 29 30 31 function permalink_anchor($mode = 'id') { 32 global $post; 33 switch ( strtolower($mode) ) { 34 case 'title': 35 $title = sanitize_title($post->post_title) . '-' . $id; 36 echo '<a id="'.$title.'"></a>'; 37 break; 38 case 'id': 39 default: 40 echo '<a id="post-' . $post->ID . '"></a>'; 41 break; 42 } 43 } 44 45 46 function get_permalink($id = 0) { 47 $rewritecode = array( 48 '%year%', 49 '%monthnum%', 50 '%day%', 51 '%hour%', 52 '%minute%', 53 '%second%', 54 '%postname%', 55 '%post_id%', 56 '%category%', 57 '%author%', 58 '%pagename%' 59 ); 60 61 $post = &get_post($id); 62 63 if ( empty($post->ID) ) return FALSE; 64 65 if ( $post->post_type == 'page' ) 66 return get_page_link($post->ID); 67 elseif ($post->post_type == 'attachment') 68 return get_attachment_link($post->ID); 69 70 $permalink = get_option('permalink_structure'); 71 72 if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending')) ) { 73 $unixtime = strtotime($post->post_date); 74 75 $category = ''; 76 if ( strpos($permalink, '%category%') !== false ) { 77 $cats = get_the_category($post->ID); 78 if ( $cats ) 79 usort($cats, '_usort_terms_by_ID'); // order by ID 80 $category = $cats[0]->slug; 81 if ( $parent=$cats[0]->parent ) 82 $category = get_category_parents($parent, FALSE, '/', TRUE) . $category; 83 } 84 85 $author = ''; 86 if ( strpos($permalink, '%author%') !== false ) { 87 $authordata = get_userdata($post->post_author); 88 $author = $authordata->user_nicename; 89 } 90 91 $date = explode(" ",date('Y m d H i s', $unixtime)); 92 $rewritereplace = 93 array( 94 $date[0], 95 $date[1], 96 $date[2], 97 $date[3], 98 $date[4], 99 $date[5], 100 $post->post_name, 101 $post->ID, 102 $category, 103 $author, 104 $post->post_name, 105 ); 106 $permalink = get_option('home') . str_replace($rewritecode, $rewritereplace, $permalink); 107 $permalink = user_trailingslashit($permalink, 'single'); 108 return apply_filters('post_link', $permalink, $post); 109 } else { // if they're not using the fancy permalink option 110 $permalink = get_option('home') . '/?p=' . $post->ID; 111 return apply_filters('post_link', $permalink, $post); 112 } 113 } 114 115 // get permalink from post ID 116 function post_permalink($post_id = 0, $deprecated = '') { 117 return get_permalink($post_id); 118 } 119 120 // Respects page_on_front. Use this one. 121 function get_page_link($id = false) { 122 global $post; 123 124 $id = (int) $id; 125 if ( !$id ) 126 $id = (int) $post->ID; 127 128 if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) 129 $link = get_option('home'); 130 else 131 $link = _get_page_link( $id ); 132 133 return apply_filters('page_link', $link, $id); 134 } 135 136 // Ignores page_on_front. Internal use only. 137 function _get_page_link( $id = false ) { 138 global $post, $wp_rewrite; 139 140 if ( !$id ) 141 $id = (int) $post->ID; 142 143 $pagestruct = $wp_rewrite->get_page_permastruct(); 144 145 if ( '' != $pagestruct && 'draft' != $post->post_status ) { 146 $link = get_page_uri($id); 147 $link = str_replace('%pagename%', $link, $pagestruct); 148 $link = get_option('home') . "/$link"; 149 $link = user_trailingslashit($link, 'page'); 150 } else { 151 $link = get_option('home') . "/?page_id=$id"; 152 } 153 154 return apply_filters( '_get_page_link', $link, $id ); 155 } 156 157 function get_attachment_link($id = false) { 158 global $post, $wp_rewrite; 159 160 $link = false; 161 162 if (! $id) { 163 $id = (int) $post->ID; 164 } 165 166 $object = get_post($id); 167 if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) { 168 $parent = get_post($object->post_parent); 169 if ( 'page' == $parent->post_type ) 170 $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front 171 else 172 $parentlink = get_permalink( $object->post_parent ); 173 if (strpos($parentlink, '?') === false) 174 $link = trailingslashit($parentlink) . $object->post_name . '/'; 175 } 176 177 if (! $link ) { 178 $link = get_bloginfo('url') . "/?attachment_id=$id"; 179 } 180 181 return apply_filters('attachment_link', $link, $id); 182 } 183 184 function get_year_link($year) { 185 global $wp_rewrite; 186 if ( !$year ) 187 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600)); 188 $yearlink = $wp_rewrite->get_year_permastruct(); 189 if ( !empty($yearlink) ) { 190 $yearlink = str_replace('%year%', $year, $yearlink); 191 return apply_filters('year_link', get_option('home') . user_trailingslashit($yearlink, 'year'), $year); 192 } else { 193 return apply_filters('year_link', get_option('home') . '/?m=' . $year, $year); 194 } 195 } 196 197 function get_month_link($year, $month) { 198 global $wp_rewrite; 199 if ( !$year ) 200 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600)); 201 if ( !$month ) 202 $month = gmdate('m', time()+(get_option('gmt_offset') * 3600)); 203 $monthlink = $wp_rewrite->get_month_permastruct(); 204 if ( !empty($monthlink) ) { 205 $monthlink = str_replace('%year%', $year, $monthlink); 206 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink); 207 return apply_filters('month_link', get_option('home') . user_trailingslashit($monthlink, 'month'), $year, $month); 208 } else { 209 return apply_filters('month_link', get_option('home') . '/?m=' . $year . zeroise($month, 2), $year, $month); 210 } 211 } 212 213 function get_day_link($year, $month, $day) { 214 global $wp_rewrite; 215 if ( !$year ) 216 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600)); 217 if ( !$month ) 218 $month = gmdate('m', time()+(get_option('gmt_offset') * 3600)); 219 if ( !$day ) 220 $day = gmdate('j', time()+(get_option('gmt_offset') * 3600)); 221 222 $daylink = $wp_rewrite->get_day_permastruct(); 223 if ( !empty($daylink) ) { 224 $daylink = str_replace('%year%', $year, $daylink); 225 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink); 226 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink); 227 return apply_filters('day_link', get_option('home') . user_trailingslashit($daylink, 'day'), $year, $month, $day); 228 } else { 229 return apply_filters('day_link', get_option('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day); 230 } 231 } 232 233 function get_feed_link($feed='rss2') { 234 global $wp_rewrite; 235 $do_perma = 0; 236 $feed_url = get_option('siteurl'); 237 $comment_feed_url = $feed_url; 238 239 $permalink = $wp_rewrite->get_feed_permastruct(); 240 if ( '' != $permalink ) { 241 if ( false !== strpos($feed, 'comments_') ) { 242 $feed = str_replace('comments_', '', $feed); 243 $permalink = $wp_rewrite->get_comment_feed_permastruct(); 244 } 245 246 if ( 'rss2' == $feed ) 247 $feed = ''; 248 249 $permalink = str_replace('%feed%', $feed, $permalink); 250 $permalink = preg_replace('#/+#', '/', "/$permalink"); 251 $output = get_option('home') . user_trailingslashit($permalink, 'feed'); 252 } else { 253 if ( false !== strpos($feed, 'comments_') ) 254 $feed = str_replace('comments_', 'comments-', $feed); 255 256 $output = get_option('home') . "/?feed={$feed}"; 257 } 258 259 return apply_filters('feed_link', $output, $feed); 260 } 261 262 function get_post_comments_feed_link($post_id = '', $feed = 'rss2') { 263 global $id; 264 265 if ( empty($post_id) ) 266 $post_id = (int) $id; 267 268 if ( '' != get_option('permalink_structure') ) { 269 $url = trailingslashit( get_permalink($post_id) ) . 'feed'; 270 if ( 'rss2' != $feed ) 271 $url .= "/$feed"; 272 $url = user_trailingslashit($url, 'single_feed'); 273 } else { 274 $type = get_post_field('post_type', $post_id); 275 if ( 'page' == $type ) 276 $url = get_option('home') . "/?feed=$feed&page_id=$post_id"; 277 else 278 $url = get_option('home') . "/?feed=$feed&p=$post_id"; 279 } 280 281 return apply_filters('post_comments_feed_link', $url); 282 } 283 284 function get_edit_post_link( $id = 0 ) { 285 $post = &get_post( $id ); 286 287 if ( $post->post_type == 'attachment' ) { 288 return; 289 } elseif ( $post->post_type == 'page' ) { 290 if ( !current_user_can( 'edit_page', $post->ID ) ) 291 return; 292 293 $file = 'page'; 294 } else { 295 if ( !current_user_can( 'edit_post', $post->ID ) ) 296 return; 297 298 $file = 'post'; 299 } 300 301 return apply_filters( 'get_edit_post_link', get_bloginfo( 'wpurl' ) . '/wp-admin/' . $file . '.php?action=edit&post=' . $post->ID, $post->ID ); 302 } 303 304 function edit_post_link( $link = 'Edit This', $before = '', $after = '' ) { 305 global $post; 306 307 if ( $post->post_type == 'attachment' ) { 308 return; 309 } elseif ( $post->post_type == 'page' ) { 310 if ( !current_user_can( 'edit_page', $post->ID ) ) 311 return; 312 313 $file = 'page'; 314 } else { 315 if ( !current_user_can( 'edit_post', $post->ID ) ) 316 return; 317 318 $file = 'post'; 319 } 320 321 $link = '<a href="' . get_edit_post_link( $post->ID ) . '" title="' . __( 'Edit post' ) . '">' . $link . '</a>'; 322 echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after; 323 } 324 325 function get_edit_comment_link( $comment_id = 0 ) { 326 $comment = &get_comment( $comment_id ); 327 $post = &get_post( $comment->comment_post_ID ); 328 329 if ( $post->post_type == 'attachment' ) { 330 return; 331 } elseif ( $post->post_type == 'page' ) { 332 if ( !current_user_can( 'edit_page', $post->ID ) ) 333 return; 334 } else { 335 if ( !current_user_can( 'edit_post', $post->ID ) ) 336 return; 337 } 338 339 $location = get_bloginfo( 'wpurl' ) . '/wp-admin/comment.php?action=editcomment&c=' . $comment->comment_ID; 340 return apply_filters( 'get_edit_comment_link', $location ); 341 } 342 343 function edit_comment_link( $link = 'Edit This', $before = '', $after = '' ) { 344 global $comment, $post; 345 346 if ( $post->post_type == 'attachment' ) { 347 return; 348 } elseif ( $post->post_type == 'page' ) { 349 if ( !current_user_can( 'edit_page', $post->ID ) ) 350 return; 351 } else { 352 if ( !current_user_can( 'edit_post', $post->ID ) ) 353 return; 354 } 355 356 $link = '<a href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . __( 'Edit comment' ) . '">' . $link . '</a>'; 357 echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after; 358 } 359 360 // Navigation links 361 362 function get_previous_post($in_same_cat = false, $excluded_categories = '') { 363 return get_adjacent_post($in_same_cat, $excluded_categories); 364 } 365 366 function get_next_post($in_same_cat = false, $excluded_categories = '') { 367 return get_adjacent_post($in_same_cat, $excluded_categories, false); 368 } 369 370 function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) { 371 global $post, $wpdb; 372 373 if( empty($post) || !is_single() || is_attachment() ) 374 return null; 375 376 $current_post_date = $post->post_date; 377 378 $join = ''; 379 if ( $in_same_cat || !empty($excluded_categories) ) { 380 $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"; 381 382 if ( $in_same_cat ) { 383 $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids'); 384 $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode($cat_array, ',') . ')'; 385 } 386 387 $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'"; 388 if ( !empty($excluded_categories) ) { 389 $excluded_categories = array_map('intval', explode(' and ', $excluded_categories)); 390 if ( !empty($cat_array) ) { 391 $excluded_categories = array_diff($excluded_categories, $cat_array); 392 $posts_in_ex_cats_sql = ''; 393 } 394 395 if ( !empty($excluded_categories) ) { 396 $posts_in_ex_cats_sql = " AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')'; 397 } 398 } 399 } 400 401 $adjacent = $previous ? 'previous' : 'next'; 402 $op = $previous ? '<' : '>'; 403 $order = $previous ? 'DESC' : 'ASC'; 404 405 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories ); 406 $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date), $in_same_cat, $excluded_categories ); 407 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); 408 409 return $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort"); 410 } 411 412 function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') { 413 adj