PageRenderTime 1035ms CodeModel.GetById 142ms app.highlight 734ms RepoModel.GetById 143ms app.codeStats 0ms

/wp-includes/post-template.php

https://bitbucket.org/geetharani/gordon
PHP | 1463 lines | 748 code | 194 blank | 521 comment | 212 complexity | c7daeebfdc7f3ba671de5ff71582e2ec MD5 | raw file
   1<?php
   2/**
   3 * WordPress Post Template Functions.
   4 *
   5 * Gets content for the current post in the loop.
   6 *
   7 * @package WordPress
   8 * @subpackage Template
   9 */
  10
  11/**
  12 * Display the ID of the current item in the WordPress Loop.
  13 *
  14 * @since 0.71
  15 */
  16function the_ID() {
  17	echo get_the_ID();
  18}
  19
  20/**
  21 * Retrieve the ID of the current item in the WordPress Loop.
  22 *
  23 * @since 2.1.0
  24 * @uses $post
  25 *
  26 * @return int
  27 */
  28function get_the_ID() {
  29	return get_post()->ID;
  30}
  31
  32/**
  33 * Display or retrieve the current post title with optional content.
  34 *
  35 * @since 0.71
  36 *
  37 * @param string $before Optional. Content to prepend to the title.
  38 * @param string $after Optional. Content to append to the title.
  39 * @param bool $echo Optional, default to true.Whether to display or return.
  40 * @return null|string Null on no title. String if $echo parameter is false.
  41 */
  42function the_title($before = '', $after = '', $echo = true) {
  43	$title = get_the_title();
  44
  45	if ( strlen($title) == 0 )
  46		return;
  47
  48	$title = $before . $title . $after;
  49
  50	if ( $echo )
  51		echo $title;
  52	else
  53		return $title;
  54}
  55
  56/**
  57 * Sanitize the current title when retrieving or displaying.
  58 *
  59 * Works like {@link the_title()}, except the parameters can be in a string or
  60 * an array. See the function for what can be override in the $args parameter.
  61 *
  62 * The title before it is displayed will have the tags stripped and {@link
  63 * esc_attr()} before it is passed to the user or displayed. The default
  64 * as with {@link the_title()}, is to display the title.
  65 *
  66 * @since 2.3.0
  67 *
  68 * @param string|array $args Optional. Override the defaults.
  69 * @return string|null Null on failure or display. String when echo is false.
  70 */
  71function the_title_attribute( $args = '' ) {
  72	$title = get_the_title();
  73
  74	if ( strlen($title) == 0 )
  75		return;
  76
  77	$defaults = array('before' => '', 'after' =>  '', 'echo' => true);
  78	$r = wp_parse_args($args, $defaults);
  79	extract( $r, EXTR_SKIP );
  80
  81	$title = $before . $title . $after;
  82	$title = esc_attr(strip_tags($title));
  83
  84	if ( $echo )
  85		echo $title;
  86	else
  87		return $title;
  88}
  89
  90/**
  91 * Retrieve post title.
  92 *
  93 * If the post is protected and the visitor is not an admin, then "Protected"
  94 * will be displayed before the post title. If the post is private, then
  95 * "Private" will be located before the post title.
  96 *
  97 * @since 0.71
  98 *
  99 * @param mixed $post Optional. Post ID or object.
 100 * @return string
 101 */
 102function get_the_title( $post = 0 ) {
 103	$post = get_post( $post );
 104
 105	$title = isset( $post->post_title ) ? $post->post_title : '';
 106	$id = isset( $post->ID ) ? $post->ID : 0;
 107
 108	if ( ! is_admin() ) {
 109		if ( ! empty( $post->post_password ) ) {
 110			$protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ) );
 111			$title = sprintf( $protected_title_format, $title );
 112		} else if ( isset( $post->post_status ) && 'private' == $post->post_status ) {
 113			$private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ) );
 114			$title = sprintf( $private_title_format, $title );
 115		}
 116	}
 117
 118	return apply_filters( 'the_title', $title, $id );
 119}
 120
 121/**
 122 * Display the Post Global Unique Identifier (guid).
 123 *
 124 * The guid will appear to be a link, but should not be used as an link to the
 125 * post. The reason you should not use it as a link, is because of moving the
 126 * blog across domains.
 127 *
 128 * Url is escaped to make it xml safe
 129 *
 130 * @since 1.5.0
 131 *
 132 * @param int $id Optional. Post ID.
 133 */
 134function the_guid( $id = 0 ) {
 135	echo esc_url( get_the_guid( $id ) );
 136}
 137
 138/**
 139 * Retrieve the Post Global Unique Identifier (guid).
 140 *
 141 * The guid will appear to be a link, but should not be used as an link to the
 142 * post. The reason you should not use it as a link, is because of moving the
 143 * blog across domains.
 144 *
 145 * @since 1.5.0
 146 *
 147 * @param int $id Optional. Post ID.
 148 * @return string
 149 */
 150function get_the_guid( $id = 0 ) {
 151	$post = get_post($id);
 152
 153	return apply_filters('get_the_guid', $post->guid);
 154}
 155
 156/**
 157 * Display the post content.
 158 *
 159 * @since 0.71
 160 *
 161 * @param string $more_link_text Optional. Content for when there is more text.
 162 * @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false.
 163 */
 164function the_content($more_link_text = null, $stripteaser = false) {
 165	$content = get_the_content($more_link_text, $stripteaser);
 166	$content = apply_filters('the_content', $content);
 167	$content = str_replace(']]>', ']]&gt;', $content);
 168	echo $content;
 169}
 170
 171/**
 172 * Retrieve the post content.
 173 *
 174 * @since 0.71
 175 *
 176 * @param string $more_link_text Optional. Content for when there is more text.
 177 * @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false.
 178 * @return string
 179 */
 180function get_the_content( $more_link_text = null, $stripteaser = false ) {
 181	global $more, $page, $pages, $multipage, $preview;
 182
 183	$post = get_post();
 184
 185	if ( null === $more_link_text )
 186		$more_link_text = __( '(more...)' );
 187
 188	$output = '';
 189	$hasTeaser = false;
 190
 191	// If post password required and it doesn't match the cookie.
 192	if ( post_password_required() )
 193		return get_the_password_form();
 194
 195	if ( $page > count($pages) ) // if the requested page doesn't exist
 196		$page = count($pages); // give them the highest numbered page that DOES exist
 197
 198	$content = $pages[$page-1];
 199	if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
 200		$content = explode($matches[0], $content, 2);
 201		if ( !empty($matches[1]) && !empty($more_link_text) )
 202			$more_link_text = strip_tags(wp_kses_no_null(trim($matches[1])));
 203
 204		$hasTeaser = true;
 205	} else {
 206		$content = array($content);
 207	}
 208	if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
 209		$stripteaser = true;
 210	$teaser = $content[0];
 211	if ( $more && $stripteaser && $hasTeaser )
 212		$teaser = '';
 213	$output .= $teaser;
 214	if ( count($content) > 1 ) {
 215		if ( $more ) {
 216			$output .= '<span id="more-' . $post->ID . '"></span>' . $content[1];
 217		} else {
 218			if ( ! empty($more_link_text) )
 219				$output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
 220			$output = force_balance_tags($output);
 221		}
 222
 223	}
 224	if ( $preview ) // preview fix for javascript bug with foreign languages
 225		$output =	preg_replace_callback('/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output);
 226
 227	return $output;
 228}
 229
 230/**
 231 * Preview fix for javascript bug with foreign languages
 232 *
 233 * @since 3.1.0
 234 * @access private
 235 * @param array $match Match array from preg_replace_callback
 236 * @return string
 237 */
 238function _convert_urlencoded_to_entities( $match ) {
 239	return '&#' . base_convert( $match[1], 16, 10 ) . ';';
 240}
 241
 242/**
 243 * Display the post excerpt.
 244 *
 245 * @since 0.71
 246 * @uses apply_filters() Calls 'the_excerpt' hook on post excerpt.
 247 */
 248function the_excerpt() {
 249	echo apply_filters('the_excerpt', get_the_excerpt());
 250}
 251
 252/**
 253 * Retrieve the post excerpt.
 254 *
 255 * @since 0.71
 256 *
 257 * @param mixed $deprecated Not used.
 258 * @return string
 259 */
 260function get_the_excerpt( $deprecated = '' ) {
 261	if ( !empty( $deprecated ) )
 262		_deprecated_argument( __FUNCTION__, '2.3' );
 263
 264	$post = get_post();
 265
 266	if ( post_password_required() ) {
 267		return __( 'There is no excerpt because this is a protected post.' );
 268	}
 269
 270	return apply_filters( 'get_the_excerpt', $post->post_excerpt );
 271}
 272
 273/**
 274 * Whether post has excerpt.
 275 *
 276 * @since 2.3.0
 277 *
 278 * @param int $id Optional. Post ID.
 279 * @return bool
 280 */
 281function has_excerpt( $id = 0 ) {
 282	$post = get_post( $id );
 283	return ( !empty( $post->post_excerpt ) );
 284}
 285
 286/**
 287 * Display the classes for the post div.
 288 *
 289 * @since 2.7.0
 290 *
 291 * @param string|array $class One or more classes to add to the class list.
 292 * @param int $post_id An optional post ID.
 293 */
 294function post_class( $class = '', $post_id = null ) {
 295	// Separates classes with a single space, collates classes for post DIV
 296	echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
 297}
 298
 299/**
 300 * Retrieve the classes for the post div as an array.
 301 *
 302 * The class names are add are many. If the post is a sticky, then the 'sticky'
 303 * class name. The class 'hentry' is always added to each post. For each
 304 * category, the class will be added with 'category-' with category slug is
 305 * added. The tags are the same way as the categories with 'tag-' before the tag
 306 * slug. All classes are passed through the filter, 'post_class' with the list
 307 * of classes, followed by $class parameter value, with the post ID as the last
 308 * parameter.
 309 *
 310 * @since 2.7.0
 311 *
 312 * @param string|array $class One or more classes to add to the class list.
 313 * @param int $post_id An optional post ID.
 314 * @return array Array of classes.
 315 */
 316function get_post_class( $class = '', $post_id = null ) {
 317	$post = get_post($post_id);
 318
 319	$classes = array();
 320
 321	if ( empty($post) )
 322		return $classes;
 323
 324	$classes[] = 'post-' . $post->ID;
 325	if ( ! is_admin() )
 326		$classes[] = $post->post_type;
 327	$classes[] = 'type-' . $post->post_type;
 328	$classes[] = 'status-' . $post->post_status;
 329
 330	// Post Format
 331	if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
 332		$post_format = get_post_format( $post->ID );
 333
 334		if ( $post_format && !is_wp_error($post_format) )
 335			$classes[] = 'format-' . sanitize_html_class( $post_format );
 336		else
 337			$classes[] = 'format-standard';
 338	}
 339
 340	// post requires password
 341	if ( post_password_required($post->ID) )
 342		$classes[] = 'post-password-required';
 343
 344	// sticky for Sticky Posts
 345	if ( is_sticky($post->ID) && is_home() && !is_paged() )
 346		$classes[] = 'sticky';
 347
 348	// hentry for hAtom compliance
 349	$classes[] = 'hentry';
 350
 351	// Categories
 352	if ( is_object_in_taxonomy( $post->post_type, 'category' ) ) {
 353		foreach ( (array) get_the_category($post->ID) as $cat ) {
 354			if ( empty($cat->slug ) )
 355				continue;
 356			$classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id);
 357		}
 358	}
 359
 360	// Tags
 361	if ( is_object_in_taxonomy( $post->post_type, 'post_tag' ) ) {
 362		foreach ( (array) get_the_tags($post->ID) as $tag ) {
 363			if ( empty($tag->slug ) )
 364				continue;
 365			$classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id);
 366		}
 367	}
 368
 369	if ( !empty($class) ) {
 370		if ( !is_array( $class ) )
 371			$class = preg_split('#\s+#', $class);
 372		$classes = array_merge($classes, $class);
 373	}
 374
 375	$classes = array_map('esc_attr', $classes);
 376
 377	return apply_filters('post_class', $classes, $class, $post->ID);
 378}
 379
 380/**
 381 * Display the classes for the body element.
 382 *
 383 * @since 2.8.0
 384 *
 385 * @param string|array $class One or more classes to add to the class list.
 386 */
 387function body_class( $class = '' ) {
 388	// Separates classes with a single space, collates classes for body element
 389	echo 'class="' . join( ' ', get_body_class( $class ) ) . '"';
 390}
 391
 392/**
 393 * Retrieve the classes for the body element as an array.
 394 *
 395 * @since 2.8.0
 396 *
 397 * @param string|array $class One or more classes to add to the class list.
 398 * @return array Array of classes.
 399 */
 400function get_body_class( $class = '' ) {
 401	global $wp_query, $wpdb;
 402
 403	$classes = array();
 404
 405	if ( is_rtl() )
 406		$classes[] = 'rtl';
 407
 408	if ( is_front_page() )
 409		$classes[] = 'home';
 410	if ( is_home() )
 411		$classes[] = 'blog';
 412	if ( is_archive() )
 413		$classes[] = 'archive';
 414	if ( is_date() )
 415		$classes[] = 'date';
 416	if ( is_search() ) {
 417		$classes[] = 'search';
 418		$classes[] = $wp_query->posts ? 'search-results' : 'search-no-results';
 419	}
 420	if ( is_paged() )
 421		$classes[] = 'paged';
 422	if ( is_attachment() )
 423		$classes[] = 'attachment';
 424	if ( is_404() )
 425		$classes[] = 'error404';
 426
 427	if ( is_single() ) {
 428		$post_id = $wp_query->get_queried_object_id();
 429		$post = $wp_query->get_queried_object();
 430
 431		$classes[] = 'single';
 432		if ( isset( $post->post_type ) ) {
 433			$classes[] = 'single-' . sanitize_html_class($post->post_type, $post_id);
 434			$classes[] = 'postid-' . $post_id;
 435
 436			// Post Format
 437			if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
 438				$post_format = get_post_format( $post->ID );
 439
 440				if ( $post_format && !is_wp_error($post_format) )
 441					$classes[] = 'single-format-' . sanitize_html_class( $post_format );
 442				else
 443					$classes[] = 'single-format-standard';
 444			}
 445		}
 446
 447		if ( is_attachment() ) {
 448			$mime_type = get_post_mime_type($post_id);
 449			$mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
 450			$classes[] = 'attachmentid-' . $post_id;
 451			$classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
 452		}
 453	} elseif ( is_archive() ) {
 454		if ( is_post_type_archive() ) {
 455			$classes[] = 'post-type-archive';
 456			$classes[] = 'post-type-archive-' . sanitize_html_class( get_query_var( 'post_type' ) );
 457		} else if ( is_author() ) {
 458			$author = $wp_query->get_queried_object();
 459			$classes[] = 'author';
 460			if ( isset( $author->user_nicename ) ) {
 461				$classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID );
 462				$classes[] = 'author-' . $author->ID;
 463			}
 464		} elseif ( is_category() ) {
 465			$cat = $wp_query->get_queried_object();
 466			$classes[] = 'category';
 467			if ( isset( $cat->term_id ) ) {
 468				$classes[] = 'category-' . sanitize_html_class( $cat->slug, $cat->term_id );
 469				$classes[] = 'category-' . $cat->term_id;
 470			}
 471		} elseif ( is_tag() ) {
 472			$tags = $wp_query->get_queried_object();
 473			$classes[] = 'tag';
 474			if ( isset( $tags->term_id ) ) {
 475				$classes[] = 'tag-' . sanitize_html_class( $tags->slug, $tags->term_id );
 476				$classes[] = 'tag-' . $tags->term_id;
 477			}
 478		} elseif ( is_tax() ) {
 479			$term = $wp_query->get_queried_object();
 480			if ( isset( $term->term_id ) ) {
 481				$classes[] = 'tax-' . sanitize_html_class( $term->taxonomy );
 482				$classes[] = 'term-' . sanitize_html_class( $term->slug, $term->term_id );
 483				$classes[] = 'term-' . $term->term_id;
 484			}
 485		}
 486	} elseif ( is_page() ) {
 487		$classes[] = 'page';
 488
 489		$page_id = $wp_query->get_queried_object_id();
 490
 491		$post = get_post($page_id);
 492
 493		$classes[] = 'page-id-' . $page_id;
 494
 495		if ( $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'page' AND post_status = 'publish' LIMIT 1", $page_id) ) )
 496			$classes[] = 'page-parent';
 497
 498		if ( $post->post_parent ) {
 499			$classes[] = 'page-child';
 500			$classes[] = 'parent-pageid-' . $post->post_parent;
 501		}
 502		if ( is_page_template() ) {
 503			$classes[] = 'page-template';
 504			$classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_page_template_slug( $page_id ) ) );
 505		} else {
 506			$classes[] = 'page-template-default';
 507		}
 508	}
 509
 510	if ( is_user_logged_in() )
 511		$classes[] = 'logged-in';
 512
 513	if ( is_admin_bar_showing() ) {
 514		$classes[] = 'admin-bar';
 515		$classes[] = 'no-customize-support';
 516	}
 517
 518	if ( get_theme_mod( 'background_color' ) || get_background_image() )
 519		$classes[] = 'custom-background';
 520
 521	$page = $wp_query->get( 'page' );
 522
 523	if ( !$page || $page < 2)
 524		$page = $wp_query->get( 'paged' );
 525
 526	if ( $page && $page > 1 ) {
 527		$classes[] = 'paged-' . $page;
 528
 529		if ( is_single() )
 530			$classes[] = 'single-paged-' . $page;
 531		elseif ( is_page() )
 532			$classes[] = 'page-paged-' . $page;
 533		elseif ( is_category() )
 534			$classes[] = 'category-paged-' . $page;
 535		elseif ( is_tag() )
 536			$classes[] = 'tag-paged-' . $page;
 537		elseif ( is_date() )
 538			$classes[] = 'date-paged-' . $page;
 539		elseif ( is_author() )
 540			$classes[] = 'author-paged-' . $page;
 541		elseif ( is_search() )
 542			$classes[] = 'search-paged-' . $page;
 543		elseif ( is_post_type_archive() )
 544			$classes[] = 'post-type-paged-' . $page;
 545	}
 546
 547	if ( ! empty( $class ) ) {
 548		if ( !is_array( $class ) )
 549			$class = preg_split( '#\s+#', $class );
 550		$classes = array_merge( $classes, $class );
 551	} else {
 552		// Ensure that we always coerce class to being an array.
 553		$class = array();
 554	}
 555
 556	$classes = array_map( 'esc_attr', $classes );
 557
 558	return apply_filters( 'body_class', $classes, $class );
 559}
 560
 561/**
 562 * Whether post requires password and correct password has been provided.
 563 *
 564 * @since 2.7.0
 565 *
 566 * @param int|object $post An optional post. Global $post used if not provided.
 567 * @return bool false if a password is not required or the correct password cookie is present, true otherwise.
 568 */
 569function post_password_required( $post = null ) {
 570	global $wp_hasher;
 571
 572	$post = get_post($post);
 573
 574	if ( empty( $post->post_password ) )
 575		return false;
 576
 577	if ( ! isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) )
 578		return true;
 579
 580	if ( empty( $wp_hasher ) ) {
 581		require_once( ABSPATH . 'wp-includes/class-phpass.php');
 582		// By default, use the portable hash from phpass
 583		$wp_hasher = new PasswordHash(8, true);
 584	}
 585
 586	$hash = stripslashes( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
 587
 588	return ! $wp_hasher->CheckPassword( $post->post_password, $hash );
 589}
 590
 591/**
 592 * Page Template Functions for usage in Themes
 593 *
 594 * @package WordPress
 595 * @subpackage Template
 596 */
 597
 598/**
 599 * The formatted output of a list of pages.
 600 *
 601 * Displays page links for paginated posts (i.e. includes the <!--nextpage-->.
 602 * Quicktag one or more times). This tag must be within The Loop.
 603 *
 604 * The defaults for overwriting are:
 605 * 'next_or_number' - Default is 'number' (string). Indicates whether page
 606 *      numbers should be used. Valid values are number and next.
 607 * 'nextpagelink' - Default is 'Next Page' (string). Text for link to next page.
 608 *      of the bookmark.
 609 * 'previouspagelink' - Default is 'Previous Page' (string). Text for link to
 610 *      previous page, if available.
 611 * 'pagelink' - Default is '%' (String).Format string for page numbers. The % in
 612 *      the parameter string will be replaced with the page number, so Page %
 613 *      generates "Page 1", "Page 2", etc. Defaults to %, just the page number.
 614 * 'before' - Default is '<p> Pages:' (string). The html or text to prepend to
 615 *      each bookmarks.
 616 * 'after' - Default is '</p>' (string). The html or text to append to each
 617 *      bookmarks.
 618 * 'link_before' - Default is '' (string). The html or text to prepend to each
 619 *      Pages link inside the <a> tag. Also prepended to the current item, which
 620 *      is not linked.
 621 * 'link_after' - Default is '' (string). The html or text to append to each
 622 *      Pages link inside the <a> tag. Also appended to the current item, which
 623 *      is not linked.
 624 *
 625 * @since 1.2.0
 626 * @access private
 627 *
 628 * @param string|array $args Optional. Overwrite the defaults.
 629 * @return string Formatted output in HTML.
 630 */
 631function wp_link_pages($args = '') {
 632	$defaults = array(
 633		'before' => '<p>' . __('Pages:'), 'after' => '</p>',
 634		'link_before' => '', 'link_after' => '',
 635		'next_or_number' => 'number', 'nextpagelink' => __('Next page'),
 636		'previouspagelink' => __('Previous page'), 'pagelink' => '%',
 637		'echo' => 1
 638	);
 639
 640	$r = wp_parse_args( $args, $defaults );
 641	$r = apply_filters( 'wp_link_pages_args', $r );
 642	extract( $r, EXTR_SKIP );
 643
 644	global $page, $numpages, $multipage, $more, $pagenow;
 645
 646	$output = '';
 647	if ( $multipage ) {
 648		if ( 'number' == $next_or_number ) {
 649			$output .= $before;
 650			for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
 651				$j = str_replace('%',$i,$pagelink);
 652				$output .= ' ';
 653				if ( ($i != $page) || ((!$more) && ($page==1)) ) {
 654					$output .= _wp_link_page($i);
 655				}
 656				$output .= $link_before . $j . $link_after;
 657				if ( ($i != $page) || ((!$more) && ($page==1)) )
 658					$output .= '</a>';
 659			}
 660			$output .= $after;
 661		} else {
 662			if ( $more ) {
 663				$output .= $before;
 664				$i = $page - 1;
 665				if ( $i && $more ) {
 666					$output .= _wp_link_page($i);
 667					$output .= $link_before. $previouspagelink . $link_after . '</a>';
 668				}
 669				$i = $page + 1;
 670				if ( $i <= $numpages && $more ) {
 671					$output .= _wp_link_page($i);
 672					$output .= $link_before. $nextpagelink . $link_after . '</a>';
 673				}
 674				$output .= $after;
 675			}
 676		}
 677	}
 678
 679	if ( $echo )
 680		echo $output;
 681
 682	return $output;
 683}
 684
 685/**
 686 * Helper function for wp_link_pages().
 687 *
 688 * @since 3.1.0
 689 * @access private
 690 *
 691 * @param int $i Page number.
 692 * @return string Link.
 693 */
 694function _wp_link_page( $i ) {
 695	global $wp_rewrite;
 696	$post = get_post();
 697
 698	if ( 1 == $i ) {
 699		$url = get_permalink();
 700	} else {
 701		if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
 702			$url = add_query_arg( 'page', $i, get_permalink() );
 703		elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID )
 704			$url = trailingslashit(get_permalink()) . user_trailingslashit("$wp_rewrite->pagination_base/" . $i, 'single_paged');
 705		else
 706			$url = trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged');
 707	}
 708
 709	return '<a href="' . esc_url( $url ) . '">';
 710}
 711
 712//
 713// Post-meta: Custom per-post fields.
 714//
 715
 716/**
 717 * Retrieve post custom meta data field.
 718 *
 719 * @since 1.5.0
 720 *
 721 * @param string $key Meta data key name.
 722 * @return bool|string|array Array of values or single value, if only one element exists. False will be returned if key does not exist.
 723 */
 724function post_custom( $key = '' ) {
 725	$custom = get_post_custom();
 726
 727	if ( !isset( $custom[$key] ) )
 728		return false;
 729	elseif ( 1 == count($custom[$key]) )
 730		return $custom[$key][0];
 731	else
 732		return $custom[$key];
 733}
 734
 735/**
 736 * Display list of post custom fields.
 737 *
 738 * @internal This will probably change at some point...
 739 * @since 1.2.0
 740 * @uses apply_filters() Calls 'the_meta_key' on list item HTML content, with key and value as separate parameters.
 741 */
 742function the_meta() {
 743	if ( $keys = get_post_custom_keys() ) {
 744		echo "<ul class='post-meta'>\n";
 745		foreach ( (array) $keys as $key ) {
 746			$keyt = trim($key);
 747			if ( is_protected_meta( $keyt, 'post' ) )
 748				continue;
 749			$values = array_map('trim', get_post_custom_values($key));
 750			$value = implode($values,', ');
 751			echo apply_filters('the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value);
 752		}
 753		echo "</ul>\n";
 754	}
 755}
 756
 757//
 758// Pages
 759//
 760
 761/**
 762 * Retrieve or display list of pages as a dropdown (select list).
 763 *
 764 * @since 2.1.0
 765 *
 766 * @param array|string $args Optional. Override default arguments.
 767 * @return string HTML content, if not displaying.
 768 */
 769function wp_dropdown_pages($args = '') {
 770	$defaults = array(
 771		'depth' => 0, 'child_of' => 0,
 772		'selected' => 0, 'echo' => 1,
 773		'name' => 'page_id', 'id' => '',
 774		'show_option_none' => '', 'show_option_no_change' => '',
 775		'option_none_value' => ''
 776	);
 777
 778	$r = wp_parse_args( $args, $defaults );
 779	extract( $r, EXTR_SKIP );
 780
 781	$pages = get_pages($r);
 782	$output = '';
 783	// Back-compat with old system where both id and name were based on $name argument
 784	if ( empty($id) )
 785		$id = $name;
 786
 787	if ( ! empty($pages) ) {
 788		$output = "<select name='" . esc_attr( $name ) . "' id='" . esc_attr( $id ) . "'>\n";
 789		if ( $show_option_no_change )
 790			$output .= "\t<option value=\"-1\">$show_option_no_change</option>";
 791		if ( $show_option_none )
 792			$output .= "\t<option value=\"" . esc_attr($option_none_value) . "\">$show_option_none</option>\n";
 793		$output .= walk_page_dropdown_tree($pages, $depth, $r);
 794		$output .= "</select>\n";
 795	}
 796
 797	$output = apply_filters('wp_dropdown_pages', $output);
 798
 799	if ( $echo )
 800		echo $output;
 801
 802	return $output;
 803}
 804
 805/**
 806 * Retrieve or display list of pages in list (li) format.
 807 *
 808 * @since 1.5.0
 809 *
 810 * @param array|string $args Optional. Override default arguments.
 811 * @return string HTML content, if not displaying.
 812 */
 813function wp_list_pages($args = '') {
 814	$defaults = array(
 815		'depth' => 0, 'show_date' => '',
 816		'date_format' => get_option('date_format'),
 817		'child_of' => 0, 'exclude' => '',
 818		'title_li' => __('Pages'), 'echo' => 1,
 819		'authors' => '', 'sort_column' => 'menu_order, post_title',
 820		'link_before' => '', 'link_after' => '', 'walker' => '',
 821	);
 822
 823	$r = wp_parse_args( $args, $defaults );
 824	extract( $r, EXTR_SKIP );
 825
 826	$output = '';
 827	$current_page = 0;
 828
 829	// sanitize, mostly to keep spaces out
 830	$r['exclude'] = preg_replace('/[^0-9,]/', '', $r['exclude']);
 831
 832	// Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array)
 833	$exclude_array = ( $r['exclude'] ) ? explode(',', $r['exclude']) : array();
 834	$r['exclude'] = implode( ',', apply_filters('wp_list_pages_excludes', $exclude_array) );
 835
 836	// Query pages.
 837	$r['hierarchical'] = 0;
 838	$pages = get_pages($r);
 839
 840	if ( !empty($pages) ) {
 841		if ( $r['title_li'] )
 842			$output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
 843
 844		global $wp_query;
 845		if ( is_page() || is_attachment() || $wp_query->is_posts_page )
 846			$current_page = $wp_query->get_queried_object_id();
 847		$output .= walk_page_tree($pages, $r['depth'], $current_page, $r);
 848
 849		if ( $r['title_li'] )
 850			$output .= '</ul></li>';
 851	}
 852
 853	$output = apply_filters('wp_list_pages', $output, $r);
 854
 855	if ( $r['echo'] )
 856		echo $output;
 857	else
 858		return $output;
 859}
 860
 861/**
 862 * Display or retrieve list of pages with optional home link.
 863 *
 864 * The arguments are listed below and part of the arguments are for {@link
 865 * wp_list_pages()} function. Check that function for more info on those
 866 * arguments.
 867 *
 868 * <ul>
 869 * <li><strong>sort_column</strong> - How to sort the list of pages. Defaults
 870 * to page title. Use column for posts table.</li>
 871 * <li><strong>menu_class</strong> - Class to use for the div ID which contains
 872 * the page list. Defaults to 'menu'.</li>
 873 * <li><strong>echo</strong> - Whether to echo list or return it. Defaults to
 874 * echo.</li>
 875 * <li><strong>link_before</strong> - Text before show_home argument text.</li>
 876 * <li><strong>link_after</strong> - Text after show_home argument text.</li>
 877 * <li><strong>show_home</strong> - If you set this argument, then it will
 878 * display the link to the home page. The show_home argument really just needs
 879 * to be set to the value of the text of the link.</li>
 880 * </ul>
 881 *
 882 * @since 2.7.0
 883 *
 884 * @param array|string $args
 885 * @return string html menu
 886 */
 887function wp_page_menu( $args = array() ) {
 888	$defaults = array('sort_column' => 'menu_order, post_title', 'menu_class' => 'menu', 'echo' => true, 'link_before' => '', 'link_after' => '');
 889	$args = wp_parse_args( $args, $defaults );
 890	$args = apply_filters( 'wp_page_menu_args', $args );
 891
 892	$menu = '';
 893
 894	$list_args = $args;
 895
 896	// Show Home in the menu
 897	if ( ! empty($args['show_home']) ) {
 898		if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] )
 899			$text = __('Home');
 900		else
 901			$text = $args['show_home'];
 902		$class = '';
 903		if ( is_front_page() && !is_paged() )
 904			$class = 'class="current_page_item"';
 905		$menu .= '<li ' . $class . '><a href="' . home_url( '/' ) . '" title="' . esc_attr($text) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>';
 906		// If the front page is a page, add it to the exclude list
 907		if (get_option('show_on_front') == 'page') {
 908			if ( !empty( $list_args['exclude'] ) ) {
 909				$list_args['exclude'] .= ',';
 910			} else {
 911				$list_args['exclude'] = '';
 912			}
 913			$list_args['exclude'] .= get_option('page_on_front');
 914		}
 915	}
 916
 917	$list_args['echo'] = false;
 918	$list_args['title_li'] = '';
 919	$menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) );
 920
 921	if ( $menu )
 922		$menu = '<ul>' . $menu . '</ul>';
 923
 924	$menu = '<div class="' . esc_attr($args['menu_class']) . '">' . $menu . "</div>\n";
 925	$menu = apply_filters( 'wp_page_menu', $menu, $args );
 926	if ( $args['echo'] )
 927		echo $menu;
 928	else
 929		return $menu;
 930}
 931
 932//
 933// Page helpers
 934//
 935
 936/**
 937 * Retrieve HTML list content for page list.
 938 *
 939 * @uses Walker_Page to create HTML list content.
 940 * @since 2.1.0
 941 * @see Walker_Page::walk() for parameters and return description.
 942 */
 943function walk_page_tree($pages, $depth, $current_page, $r) {
 944	if ( empty($r['walker']) )
 945		$walker = new Walker_Page;
 946	else
 947		$walker = $r['walker'];
 948
 949	$args = array($pages, $depth, $r, $current_page);
 950	return call_user_func_array(array($walker, 'walk'), $args);
 951}
 952
 953/**
 954 * Retrieve HTML dropdown (select) content for page list.
 955 *
 956 * @uses Walker_PageDropdown to create HTML dropdown content.
 957 * @since 2.1.0
 958 * @see Walker_PageDropdown::walk() for parameters and return description.
 959 */
 960function walk_page_dropdown_tree() {
 961	$args = func_get_args();
 962	if ( empty($args[2]['walker']) ) // the user's options are the third parameter
 963		$walker = new Walker_PageDropdown;
 964	else
 965		$walker = $args[2]['walker'];
 966
 967	return call_user_func_array(array($walker, 'walk'), $args);
 968}
 969
 970/**
 971 * Create HTML list of pages.
 972 *
 973 * @package WordPress
 974 * @since 2.1.0
 975 * @uses Walker
 976 */
 977class Walker_Page extends Walker {
 978	/**
 979	 * @see Walker::$tree_type
 980	 * @since 2.1.0
 981	 * @var string
 982	 */
 983	var $tree_type = 'page';
 984
 985	/**
 986	 * @see Walker::$db_fields
 987	 * @since 2.1.0
 988	 * @todo Decouple this.
 989	 * @var array
 990	 */
 991	var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
 992
 993	/**
 994	 * @see Walker::start_lvl()
 995	 * @since 2.1.0
 996	 *
 997	 * @param string $output Passed by reference. Used to append additional content.
 998	 * @param int $depth Depth of page. Used for padding.
 999	 * @param array $args
1000	 */
1001	function start_lvl( &$output, $depth = 0, $args = array() ) {
1002		$indent = str_repeat("\t", $depth);
1003		$output .= "\n$indent<ul class='children'>\n";
1004	}
1005
1006	/**
1007	 * @see Walker::end_lvl()
1008	 * @since 2.1.0
1009	 *
1010	 * @param string $output Passed by reference. Used to append additional content.
1011	 * @param int $depth Depth of page. Used for padding.
1012	 * @param array $args
1013	 */
1014	function end_lvl( &$output, $depth = 0, $args = array() ) {
1015		$indent = str_repeat("\t", $depth);
1016		$output .= "$indent</ul>\n";
1017	}
1018
1019	/**
1020	 * @see Walker::start_el()
1021	 * @since 2.1.0
1022	 *
1023	 * @param string $output Passed by reference. Used to append additional content.
1024	 * @param object $page Page data object.
1025	 * @param int $depth Depth of page. Used for padding.
1026	 * @param int $current_page Page ID.
1027	 * @param array $args
1028	 */
1029	function start_el( &$output, $page, $depth, $args, $current_page = 0 ) {
1030		if ( $depth )
1031			$indent = str_repeat("\t", $depth);
1032		else
1033			$indent = '';
1034
1035		extract($args, EXTR_SKIP);
1036		$css_class = array('page_item', 'page-item-'.$page->ID);
1037		if ( !empty($current_page) ) {
1038			$_current_page = get_post( $current_page );
1039			if ( in_array( $page->ID, $_current_page->ancestors ) )
1040				$css_class[] = 'current_page_ancestor';
1041			if ( $page->ID == $current_page )
1042				$css_class[] = 'current_page_item';
1043			elseif ( $_current_page && $page->ID == $_current_page->post_parent )
1044				$css_class[] = 'current_page_parent';
1045		} elseif ( $page->ID == get_option('page_for_posts') ) {
1046			$css_class[] = 'current_page_parent';
1047		}
1048
1049		$css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
1050
1051		$output .= $indent . '<li class="' . $css_class . '"><a href="' . get_permalink($page->ID) . '">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . '</a>';
1052
1053		if ( !empty($show_date) ) {
1054			if ( 'modified' == $show_date )
1055				$time = $page->post_modified;
1056			else
1057				$time = $page->post_date;
1058
1059			$output .= " " . mysql2date($date_format, $time);
1060		}
1061	}
1062
1063	/**
1064	 * @see Walker::end_el()
1065	 * @since 2.1.0
1066	 *
1067	 * @param string $output Passed by reference. Used to append additional content.
1068	 * @param object $page Page data object. Not used.
1069	 * @param int $depth Depth of page. Not Used.
1070	 * @param array $args
1071	 */
1072	function end_el( &$output, $page, $depth = 0, $args = array() ) {
1073		$output .= "</li>\n";
1074	}
1075
1076}
1077
1078/**
1079 * Create HTML dropdown list of pages.
1080 *
1081 * @package WordPress
1082 * @since 2.1.0
1083 * @uses Walker
1084 */
1085class Walker_PageDropdown extends Walker {
1086	/**
1087	 * @see Walker::$tree_type
1088	 * @since 2.1.0
1089	 * @var string
1090	 */
1091	var $tree_type = 'page';
1092
1093	/**
1094	 * @see Walker::$db_fields
1095	 * @since 2.1.0
1096	 * @todo Decouple this
1097	 * @var array
1098	 */
1099	var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
1100
1101	/**
1102	 * @see Walker::start_el()
1103	 * @since 2.1.0
1104	 *
1105	 * @param string $output Passed by reference. Used to append additional content.
1106	 * @param object $page Page data object.
1107	 * @param int $depth Depth of page in reference to parent pages. Used for padding.
1108	 * @param array $args Uses 'selected' argument for selected page to set selected HTML attribute for option element.
1109	 * @param int $id
1110	 */
1111	function start_el(&$output, $page, $depth, $args, $id = 0) {
1112		$pad = str_repeat('&nbsp;', $depth * 3);
1113
1114		$output .= "\t<option class=\"level-$depth\" value=\"$page->ID\"";
1115		if ( $page->ID == $args['selected'] )
1116			$output .= ' selected="selected"';
1117		$output .= '>';
1118		$title = apply_filters( 'list_pages', $page->post_title, $page );
1119		$output .= $pad . esc_html( $title );
1120		$output .= "</option>\n";
1121	}
1122}
1123
1124//
1125// Attachments
1126//
1127
1128/**
1129 * Display an attachment page link using an image or icon.
1130 *
1131 * @since 2.0.0
1132 *
1133 * @param int $id Optional. Post ID.
1134 * @param bool $fullsize Optional, default is false. Whether to use full size.
1135 * @param bool $deprecated Deprecated. Not used.
1136 * @param bool $permalink Optional, default is false. Whether to include permalink.
1137 */
1138function the_attachment_link( $id = 0, $fullsize = false, $deprecated = false, $permalink = false ) {
1139	if ( !empty( $deprecated ) )
1140		_deprecated_argument( __FUNCTION__, '2.5' );
1141
1142	if ( $fullsize )
1143		echo wp_get_attachment_link($id, 'full', $permalink);
1144	else
1145		echo wp_get_attachment_link($id, 'thumbnail', $permalink);
1146}
1147
1148/**
1149 * Retrieve an attachment page link using an image or icon, if possible.
1150 *
1151 * @since 2.5.0
1152 * @uses apply_filters() Calls 'wp_get_attachment_link' filter on HTML content with same parameters as function.
1153 *
1154 * @param int $id Optional. Post ID.
1155 * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string.
1156 * @param bool $permalink Optional, default is false. Whether to add permalink to image.
1157 * @param bool $icon Optional, default is false. Whether to include icon.
1158 * @param string|bool $text Optional, default is false. If string, then will be link text.
1159 * @return string HTML content.
1160 */
1161function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false ) {
1162	$id = intval( $id );
1163	$_post = get_post( $id );
1164
1165	if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
1166		return __( 'Missing Attachment' );
1167
1168	if ( $permalink )
1169		$url = get_attachment_link( $_post->ID );
1170
1171	$post_title = esc_attr( $_post->post_title );
1172
1173	if ( $text )
1174		$link_text = $text;
1175	elseif ( $size && 'none' != $size )
1176		$link_text = wp_get_attachment_image( $id, $size, $icon );
1177	else
1178		$link_text = '';
1179
1180	if ( trim( $link_text ) == '' )
1181		$link_text = $_post->post_title;
1182
1183	return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text );
1184}
1185
1186/**
1187 * Wrap attachment in <<p>> element before content.
1188 *
1189 * @since 2.0.0
1190 * @uses apply_filters() Calls 'prepend_attachment' hook on HTML content.
1191 *
1192 * @param string $content
1193 * @return string
1194 */
1195function prepend_attachment($content) {
1196	$post = get_post();
1197
1198	if ( empty($post->post_type) || $post->post_type != 'attachment' )
1199		return $content;
1200
1201	$p = '<p class="attachment">';
1202	// show the medium sized image representation of the attachment if available, and link to the raw file
1203	$p .= wp_get_attachment_link(0, 'medium', false);
1204	$p .= '</p>';
1205	$p = apply_filters('prepend_attachment', $p);
1206
1207	return "$p\n$content";
1208}
1209
1210//
1211// Misc
1212//
1213
1214/**
1215 * Retrieve protected post password form content.
1216 *
1217 * @since 1.0.0
1218 * @uses apply_filters() Calls 'the_password_form' filter on output.
1219 *
1220 * @return string HTML content for password form for password protected post.
1221 */
1222function get_the_password_form() {
1223	$post = get_post();
1224	$label = 'pwbox-' . ( empty($post->ID) ? rand() : $post->ID );
1225	$output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">
1226	<p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
1227	<p><label for="' . $label . '">' . __("Password:") . ' <input name="post_password" id="' . $label . '" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . esc_attr__("Submit") . '" /></p>
1228</form>
1229	';
1230	return apply_filters('the_password_form', $output);
1231}
1232
1233/**
1234 * Whether currently in a page template.
1235 *
1236 * This template tag allows you to determine if you are in a page template.
1237 * You can optionally provide a template name and then the check will be
1238 * specific to that template.
1239 *
1240 * @since 2.5.0
1241 * @uses $wp_query
1242 *
1243 * @param string $template The specific template name if specific matching is required.
1244 * @return bool False on failure, true if success.
1245 */
1246function is_page_template( $template = '' ) {
1247	if ( ! is_page() )
1248		return false;
1249
1250	$page_template = get_page_template_slug( get_queried_object_id() );
1251
1252	if ( empty( $template ) )
1253		return (bool) $page_template;
1254
1255	if ( $template == $page_template )
1256		return true;
1257
1258	if ( 'default' == $template && ! $page_template )
1259		return true;
1260
1261	return false;
1262}
1263
1264/**
1265 * Get the specific template name for a page.
1266 *
1267 * @since 3.4.0
1268 *
1269 * @param int $post_id The page ID to check. Defaults to the current post, when used in the loop.
1270 * @return string|bool Page template filename. Returns an empty string when the default page template
1271 * 	is in use. Returns false if the post is not a page.
1272 */
1273function get_page_template_slug( $post_id = null ) {
1274	$post = get_post( $post_id );
1275	if ( 'page' != $post->post_type )
1276		return false;
1277	$template = get_post_meta( $post->ID, '_wp_page_template', true );
1278	if ( ! $template || 'default' == $template )
1279		return '';
1280	return $template;
1281}
1282
1283/**
1284 * Retrieve formatted date timestamp of a revision (linked to that revisions's page).
1285 *
1286 * @package WordPress
1287 * @subpackage Post_Revisions
1288 * @since 2.6.0
1289 *
1290 * @uses date_i18n()
1291 *
1292 * @param int|object $revision Revision ID or revision object.
1293 * @param bool $link Optional, default is true. Link to revisions's page?
1294 * @return string i18n formatted datetimestamp or localized 'Current Revision'.
1295 */
1296function wp_post_revision_title( $revision, $link = true ) {
1297	if ( !$revision = get_post( $revision ) )
1298		return $revision;
1299
1300	if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
1301		return false;
1302
1303	/* translators: revision date format, see http://php.net/date */
1304	$datef = _x( 'j F, Y @ G:i', 'revision date format');
1305	/* translators: 1: date */
1306	$autosavef = __( '%1$s [Autosave]' );
1307	/* translators: 1: date */
1308	$currentf  = __( '%1$s [Current Revision]' );
1309
1310	$date = date_i18n( $datef, strtotime( $revision->post_modified ) );
1311	if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
1312		$date = "<a href='$link'>$date</a>";
1313
1314	if ( !wp_is_post_revision( $revision ) )
1315		$date = sprintf( $currentf, $date );
1316	elseif ( wp_is_post_autosave( $revision ) )
1317		$date = sprintf( $autosavef, $date );
1318
1319	return $date;
1320}
1321
1322/**
1323 * Display list of a post's revisions.
1324 *
1325 * Can output either a UL with edit links or a TABLE with diff interface, and
1326 * restore action links.
1327 *
1328 * Second argument controls parameters:
1329 *   (bool)   parent : include the parent (the "Current Revision") in the list.
1330 *   (string) format : 'list' or 'form-table'. 'list' outputs UL, 'form-table'
1331 *                     outputs TABLE with UI.
1332 *   (int)    right  : what revision is currently being viewed - used in
1333 *                     form-table format.
1334 *   (int)    left   : what revision is currently being diffed against right -
1335 *                     used in form-table format.
1336 *
1337 * @package WordPress
1338 * @subpackage Post_Revisions
1339 * @since 2.6.0
1340 *
1341 * @uses wp_get_post_revisions()
1342 * @uses wp_post_revision_title()
1343 * @uses get_edit_post_link()
1344 * @uses get_the_author_meta()
1345 *
1346 * @todo split into two functions (list, form-table) ?
1347 *
1348 * @param int|object $post_id Post ID or post object.
1349 * @param string|array $args See description {@link wp_parse_args()}.
1350 * @return null
1351 */
1352function wp_list_post_revisions( $post_id = 0, $args = null ) {
1353	if ( !$post = get_post( $post_id ) )
1354		return;
1355
1356	$defaults = array( 'parent' => false, 'right' => false, 'left' => false, 'format' => 'list', 'type' => 'all' );
1357	extract( wp_parse_args( $args, $defaults ), EXTR_SKIP );
1358
1359	switch ( $type ) {
1360		case 'autosave' :
1361			if ( !$autosave = wp_get_post_autosave( $post->ID ) )
1362				return;
1363			$revisions = array( $autosave );
1364			break;
1365		case 'revision' : // just revisions - remove autosave later
1366		case 'all' :
1367		default :
1368			if ( !$revisions = wp_get_post_revisions( $post->ID ) )
1369				return;
1370			break;
1371	}
1372
1373	/* translators: post revision: 1: when, 2: author name */
1374	$titlef = _x( '%1$s by %2$s', 'post revision' );
1375
1376	if ( $parent )
1377		array_unshift( $revisions, $post );
1378
1379	$rows = $right_checked = '';
1380	$class = false;
1381	$can_edit_post = current_user_can( 'edit_post', $post->ID );
1382	foreach ( $revisions as $revision ) {
1383		if ( !current_user_can( 'read_post', $revision->ID ) )
1384			continue;
1385		if ( 'revision' === $type && wp_is_post_autosave( $revision ) )
1386			continue;
1387
1388		$date = wp_post_revision_title( $revision );
1389		$name = get_the_author_meta( 'display_name', $revision->post_author );
1390
1391		if ( 'form-table' == $format ) {
1392			if ( $left )
1393				$left_checked = $left == $revision->ID ? ' checked="checked"' : '';
1394			else
1395				$left_checked = $right_checked ? ' checked="checked"' : ''; // [sic] (the next one)
1396			$right_checked = $right == $revision->ID ? ' checked="checked"' : '';
1397
1398			$class = $class ? '' : " class='alternate'";
1399
1400			if ( $post->ID != $revision->ID && $can_edit_post )
1401				$actions = '<a href="' . wp_nonce_url( add_query_arg( array( 'revision' => $revision->ID, 'action' => 'restore' ) ), "restore-post_$post->ID|$revision->ID" ) . '">' . __( 'Restore' ) . '</a>';
1402			else
1403				$actions = '';
1404
1405			$rows .= "<tr$class>\n";
1406			$rows .= "\t<th style='white-space: nowrap' scope='row'><input type='radio' name='left' value='$revision->ID'$left_checked /></th>\n";
1407			$rows .= "\t<th style='white-space: nowrap' scope='row'><input type='radio' name='right' value='$revision->ID'$right_checked /></th>\n";
1408			$rows .= "\t<td>$date</td>\n";
1409			$rows .= "\t<td>$name</td>\n";
1410			$rows .= "\t<td class='action-links'>$actions</td>\n";
1411			$rows .= "</tr>\n";
1412		} else {
1413			$title = sprintf( $titlef, $date, $name );
1414			$rows .= "\t<li>$title</li>\n";
1415		}
1416	}
1417
1418	if ( 'form-table' == $format ) : ?>
1419
1420<form action="revision.php" method="get">
1421
1422<div class="tablenav">
1423	<div class="alignleft">
1424		<input type="submit" class="button-secondary" value="<?php esc_attr_e( 'Compare Revisions' ); ?>" />
1425		<input type="hidden" name="action" value="diff" />
1426		<input type="hidden" name="post_type" value="<?php echo esc_attr($post->post_type); ?>" />
1427	</div>
1428</div>
1429
1430<br class="clear" />
1431
1432<table class="widefat post-revisions" cellspacing="0" id="post-revisions">
1433	<col />
1434	<col />
1435	<col style="width: 33%" />
1436	<col style="width: 33%" />
1437	<col style="width: 33%" />
1438<thead>
1439<tr>
1440	<th scope="col"><?php /* translators: column name in revisions */ _ex( 'Old', 'revisions column name' ); ?></th>
1441	<th scope="col"><?php /* translators: column name in revisions */ _ex( 'New', 'revisions column name' ); ?></th>
1442	<th scope="col"><?php /* translators: column name in revisions */ _ex( 'Date Created', 'revisions column name' ); ?></th>
1443	<th scope="col"><?php _e( 'Author' ); ?></th>
1444	<th scope="col" class="action-links"><?php _e( 'Actions' ); ?></th>
1445</tr>
1446</thead>
1447<tbody>
1448
1449<?php echo $rows; ?>
1450
1451</tbody>
1452</table>
1453
1454</form>
1455
1456<?php
1457	else :
1458		echo "<ul class='post-revisions'>\n";
1459		echo $rows;
1460		echo "</ul>";
1461	endif;
1462
1463}