[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> post-template.php (source)

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


Generated: Fri May 25 03:56:23 2012 Hosted by follow the white rabbit.