[ Index ]

PHP Cross Reference of WordPress 3.0 beta 1

[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

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


Generated: Mon Apr 5 14:26:09 2010 Cross-referenced by PHPXref 0.7