PHP Cross Reference of WordPress Subversion HEAD

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

title

Body

[close]

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

   1  <?php
   2  
   3  //
   4  // Post functions
   5  //
   6  
   7  function get_attached_file( $attachment_id, $unfiltered = false ) {
   8      $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
   9      if ( $unfiltered )
  10          return $file;
  11      return apply_filters( 'get_attached_file', $file, $attachment_id );
  12  }
  13  
  14  function update_attached_file( $attachment_id, $file ) {
  15      if ( !get_post( $attachment_id ) )
  16          return false;
  17  
  18      $old_file = get_attached_file( $attachment_id, true );
  19  
  20      $file = apply_filters( 'update_attached_file', $file, $attachment_id );
  21  
  22      if ( $old_file )
  23          return update_post_meta( $attachment_id, '_wp_attached_file', $file, $old_file );
  24      else
  25          return add_post_meta( $attachment_id, '_wp_attached_file', $file );
  26  }
  27  
  28  function &get_children($args = '', $output = OBJECT) {
  29      global $wpdb;
  30  
  31      if ( empty( $args ) ) {
  32          if ( isset( $GLOBALS['post'] ) ) {
  33              $args = 'post_parent=' . (int) $GLOBALS['post']->post_parent;
  34          } else {
  35              return false;
  36          }
  37      } elseif ( is_object( $args ) ) {
  38          $args = 'post_parent=' . (int) $args->post_parent;
  39      } elseif ( is_numeric( $args ) ) {
  40          $args = 'post_parent=' . (int) $args;
  41      }
  42  
  43      $defaults = array(
  44          'numberposts' => -1, 'post_type' => '',
  45          'post_status' => '', 'post_parent' => 0
  46      );
  47  
  48      $r = wp_parse_args( $args, $defaults );
  49  
  50      $children = get_posts( $r );
  51  
  52      if ( !$children )
  53          return false;
  54  
  55      update_post_cache($children);
  56  
  57      foreach ( $children as $key => $child )
  58          $kids[$child->ID] =& $children[$key];
  59  
  60      if ( $output == OBJECT ) {
  61          return $kids;
  62      } elseif ( $output == ARRAY_A ) {
  63          foreach ( $kids as $kid )
  64              $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
  65          return $weeuns;
  66      } elseif ( $output == ARRAY_N ) {
  67          foreach ( $kids as $kid )
  68              $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
  69          return $babes;
  70      } else {
  71          return $kids;
  72      }
  73  }
  74  
  75  // get extended entry info (<!--more-->)
  76  function get_extended($post) {
  77      //Match the new style more links
  78      if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
  79          list($main, $extended) = explode($matches[0], $post, 2);
  80      } else {
  81          $main = $post;
  82          $extended = '';
  83      }
  84  
  85      // Strip leading and trailing whitespace
  86      $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
  87      $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
  88  
  89      return array('main' => $main, 'extended' => $extended);
  90  }
  91  
  92  // Retrieves post data given a post ID or post object.
  93  // Handles post caching.
  94  function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
  95      global $wpdb;
  96  
  97      if ( empty($post) ) {
  98          if ( isset($GLOBALS['post']) )
  99              $_post = & $GLOBALS['post'];
 100          else
 101              return null;
 102      } elseif ( is_object($post) ) {
 103          wp_cache_add($post->ID, $post, 'posts');
 104          $_post = &$post;
 105      } else {
 106          $post = (int) $post;
 107          if ( ! $_post = wp_cache_get($post, 'posts') ) {
 108              $_post = & $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post));
 109              wp_cache_add($_post->ID, $_post, 'posts');
 110          }
 111      }
 112  
 113      $_post = sanitize_post($_post, $filter);
 114  
 115      if ( $output == OBJECT ) {
 116          return $_post;
 117      } elseif ( $output == ARRAY_A ) {
 118          return get_object_vars($_post);
 119      } elseif ( $output == ARRAY_N ) {
 120          return array_values(get_object_vars($_post));
 121      } else {
 122          return $_post;
 123      }
 124  }
 125  
 126  function get_post_field( $field, $post, $context = 'display' ) {
 127      $post = (int) $post;
 128      $post = get_post( $post );
 129  
 130      if ( is_wp_error($post) )
 131          return $post;
 132  
 133      if ( !is_object($post) )
 134          return '';
 135  
 136      if ( !isset($post->$field) )
 137          return '';
 138  
 139      return sanitize_post_field($field, $post->$field, $post->ID, $context);
 140  }
 141  
 142  // Takes a post ID, returns its mime type.
 143  function get_post_mime_type($ID = '') {
 144      $post = & get_post($ID);
 145  
 146      if ( is_object($post) )
 147          return $post->post_mime_type;
 148  
 149      return false;
 150  }
 151  
 152  function get_post_status($ID = '') {
 153      $post = get_post($ID);
 154  
 155      if ( is_object($post) ) {
 156          if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) )
 157              return get_post_status($post->post_parent);
 158          else
 159              return $post->post_status;
 160      }
 161  
 162      return false;
 163  }
 164  
 165  function get_post_type($post = false) {
 166      global $wpdb, $posts;
 167  
 168      if ( false === $post )
 169          $post = $posts[0];
 170      elseif ( (int) $post )
 171          $post = get_post($post, OBJECT);
 172  
 173      if ( is_object($post) )
 174          return $post->post_type;
 175  
 176      return false;
 177  }
 178  
 179  function get_posts($args) {
 180      global $wpdb;
 181  
 182      $defaults = array(
 183          'numberposts' => 5, 'offset' => 0,
 184          'category' => 0, 'orderby' => 'post_date',
 185          'order' => 'DESC', 'include' => '',
 186          'exclude' => '', 'meta_key' => '',
 187          'meta_value' =>'', 'post_type' => 'post',
 188          'post_status' => 'publish', 'post_parent' => 0
 189      );
 190  
 191      $r = wp_parse_args( $args, $defaults );
 192      extract( $r, EXTR_SKIP );
 193  
 194      $numberposts = (int) $numberposts;
 195      $offset = (int) $offset;
 196      $category = (int) $category;
 197      $post_parent = (int) $post_parent;
 198  
 199      $inclusions = '';
 200      if ( !empty($include) ) {
 201          $offset = 0;    //ignore offset, category, exclude, meta_key, and meta_value, post_parent if using include
 202          $category = 0;
 203          $exclude = '';
 204          $meta_key = '';
 205          $meta_value = '';
 206          $post_parent = 0;
 207          $incposts = preg_split('/[\s,]+/',$include);
 208          $numberposts = count($incposts);  // only the number of posts included
 209          if ( count($incposts) ) {
 210              foreach ( $incposts as $incpost ) {
 211                  if (empty($inclusions))
 212                      $inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpost);
 213                  else
 214                      $inclusions .= $wpdb->prepare(' OR ID = %d ', $incpost);
 215              }
 216          }
 217      }
 218      if (!empty($inclusions))
 219          $inclusions .= ')';
 220  
 221      $exclusions = '';
 222      if ( !empty($exclude) ) {
 223          $exposts = preg_split('/[\s,]+/',$exclude);
 224          if ( count($exposts) ) {
 225              foreach ( $exposts as $expost ) {
 226                  if (empty($exclusions))
 227                      $exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expost);
 228                  else
 229                      $exclusions .= $wpdb->prepare(' AND ID <> %d ', $expost);
 230              }
 231          }
 232      }
 233      if (!empty($exclusions))
 234          $exclusions .= ')';
 235  
 236      $query  = "SELECT DISTINCT * FROM $wpdb->posts ";
 237      $query .= empty( $category ) ? '' : ", $wpdb->term_relationships, $wpdb->term_taxonomy  ";
 238      $query .= empty( $meta_key ) ? '' : ", $wpdb->postmeta ";
 239      $query .= " WHERE 1=1 ";
 240      $query .= empty( $post_type ) ? '' : $wpdb->prepare("AND post_type = %s ", $post_type);
 241      $query .= empty( $post_status ) ? '' : $wpdb->prepare("AND post_status = %s ", $post_status);
 242      $query .= "$exclusions $inclusions " ;
 243      $query .= empty( $category ) ? '' : $wpdb->prepare("AND ($wpdb->posts.ID = $wpdb->term_relationships.object_id AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.term_id = %d) ", $category);
 244      $query .= empty( $post_parent ) ? '' : $wpdb->prepare("AND $wpdb->posts.post_parent = %d ", $post_parent);
 245      // expected_slashed ($meta_key, $meta_value) -- Also, this looks really funky, doesn't seem like it works
 246      $query .= empty( $meta_key ) | empty($meta_value)  ? '' : " AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '$meta_key' AND $wpdb->postmeta.meta_value = '$meta_value' )";
 247      $query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . ' ' . $order;
 248      if ( 0 < $numberposts )
 249          $query .= $wpdb->prepare(" LIMIT %d,%d", $offset, $numberposts);
 250  
 251      $posts = $wpdb->get_results($query);
 252  
 253      update_post_caches($posts);
 254  
 255      return $posts;
 256  }
 257  
 258  //
 259  // Post meta functions
 260  //
 261  
 262  function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) {
 263      global $wpdb;
 264  
 265      // expected_slashed ($meta_key)
 266      $meta_key = stripslashes($meta_key);
 267  
 268      if ( $unique && $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post_id ) ) )
 269          return false;
 270  
 271      $cache = wp_cache_get($post_id, 'post_meta');
 272      if ( ! is_array($cache) )
 273          $cache = array();
 274      // expected_slashed ($meta_key)
 275      $cache[$wpdb->escape($meta_key)][] = $meta_value;
 276  
 277      wp_cache_set($post_id, $cache, 'post_meta');
 278  
 279      $meta_value = maybe_serialize($meta_value);
 280  
 281      $wpdb->insert( $wpdb->postmeta, compact( 'post_id', 'meta_key', 'meta_value' ) );
 282      return true;
 283  }
 284  
 285  function delete_post_meta($post_id, $key, $value = '') {
 286      global $wpdb;
 287  
 288      $post_id = absint( $post_id );
 289  
 290      // expected_slashed ($key, $value)
 291      $key = stripslashes( $key );
 292      $value = stripslashes( $value );
 293  
 294      if ( empty( $value ) )
 295          $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $key ) );
 296      else
 297          $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $post_id, $key, $value ) );
 298  
 299      if ( !$meta_id )
 300          return false;
 301  
 302      if ( empty( $value ) )
 303          $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $key ) );
 304      else
 305          $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $post_id, $key, $value ) );
 306  
 307      wp_cache_delete($post_id, 'post_meta');
 308  
 309      return true;
 310  }
 311  
 312  function get_post_meta($post_id, $key, $single = false) {
 313      global $wpdb;
 314  
 315      $post_id = (int) $post_id;
 316  
 317      $meta_cache = wp_cache_get($post_id, 'post_meta');
 318  
 319      if ( isset($meta_cache[$key]) ) {
 320          if ( $single ) {
 321              return maybe_unserialize( $meta_cache[$key][0] );
 322          } else {
 323              return maybe_unserialize( $meta_cache[$key] );
 324          }
 325      }
 326  
 327      if ( !$meta_cache ) {
 328          update_postmeta_cache($post_id);
 329          $meta_cache = wp_cache_get($post_id, 'post_meta');
 330      }
 331  
 332      if ( $single ) {
 333          if ( isset($meta_cache[$key][0]) )
 334              return maybe_unserialize($meta_cache[$key][0]);
 335          else
 336              return '';
 337      } else {
 338          return maybe_unserialize($meta_cache[$key]);
 339      }
 340  }
 341  
 342  function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') {
 343      global $wpdb;
 344  
 345      $original_value = $meta_value;
 346      $meta_value = maybe_serialize($meta_value);
 347  
 348      $original_prev = $prev_value;
 349      $prev_value = maybe_serialize($prev_value);
 350  
 351      // expected_slashed ($meta_key)
 352      $meta_key = stripslashes($meta_key);
 353  
 354      if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post_id ) ) )
 355          return false;
 356  
 357      $data  = compact( 'meta_value' );
 358      $where = compact( 'meta_key', 'post_id' );
 359  
 360      if ( !empty( $prev_value ) )
 361          $where['meta_value'] = $prev_value;
 362  
 363      $wpdb->update( $wpdb->postmeta, $data, $where );
 364      wp_cache_delete($post_id, 'post_meta');
 365      return true;
 366  }
 367  
 368  
 369  function delete_post_meta_by_key($post_meta_key) {
 370      global $wpdb;
 371      if ( $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key)) ) {
 372          // TODO Get post_ids and delete cache
 373          // wp_cache_delete($post_id, 'post_meta');
 374          return true;
 375      }
 376      return false;
 377  }
 378  
 379  
 380  function get_post_custom($post_id = 0) {
 381      global $id, $wpdb;
 382  
 383      if ( !$post_id )
 384          $post_id = (int) $id;
 385  
 386      $post_id = (int) $post_id;
 387  
 388      if ( ! wp_cache_get($post_id, 'post_meta') )
 389          update_postmeta_cache($post_id);
 390  
 391      return wp_cache_get($post_id, 'post_meta');
 392  }
 393  
 394  function get_post_custom_keys( $post_id = 0 ) {
 395      $custom = get_post_custom( $post_id );
 396  
 397      if ( !is_array($custom) )
 398          return;
 399  
 400      if ( $keys = array_keys($custom) )
 401          return $keys;
 402  }
 403  
 404  
 405  function get_post_custom_values( $key = '', $post_id = 0 ) {
 406      $custom = get_post_custom($post_id);
 407  
 408      return $custom[$key];
 409  }
 410  
 411  function sanitize_post($post, $context = 'display') {
 412  
 413      if ( 'raw' == $context )
 414          return $post;
 415  
 416      // TODO: Use array keys instead of hard coded list
 417      $fields = array('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_date', 'post_date_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'post_category');
 418  
 419      $do_object = false;
 420      if ( is_object($post) )
 421          $do_object = true;
 422  
 423      foreach ( $fields as $field ) {
 424          if ( $do_object )
 425              $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context);
 426          else
 427              $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
 428      }
 429  
 430      return $post;
 431  }
 432  
 433  function sanitize_post_field($field, $value, $post_id, $context) {
 434      $int_fields