[ Index ]

PHP Cross Reference of WordPress Trunk (Latest)

title

Body

[close]

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

   1  <?php
   2  
   3  /**** DB Functions ****/
   4  
   5  /*
   6   * generic function for inserting data into the posts table.
   7   */
   8  function wp_insert_post($postarr = array()) {
   9      global $wpdb, $wp_rewrite, $allowedtags, $user_ID;
  10  
  11      if ( is_object($postarr) )
  12          $postarr = get_object_vars($postarr);
  13  
  14      // export array as variables
  15      extract($postarr);
  16  
  17      // Are we updating or creating?
  18      $update = false;
  19      if ( !empty($ID) ) {
  20          $update = true;
  21          $post = & get_post($ID);
  22          $previous_status = $post->post_status;
  23      }
  24  
  25      // Get the basics.
  26      $post_content    = apply_filters('content_save_pre',   $post_content);
  27      $post_excerpt    = apply_filters('excerpt_save_pre',   $post_excerpt);
  28      $post_title      = apply_filters('title_save_pre',     $post_title);
  29      $post_category   = apply_filters('category_save_pre',  $post_category);
  30      $post_status     = apply_filters('status_save_pre',    $post_status);
  31      $post_name       = apply_filters('name_save_pre',      $post_name);
  32      $comment_status  = apply_filters('comment_status_pre', $comment_status);
  33      $ping_status     = apply_filters('ping_status_pre',    $ping_status);
  34  
  35      // Make sure we set a valid category
  36      if (0 == count($post_category) || !is_array($post_category)) {
  37          $post_category = array(get_option('default_category'));
  38      }
  39      $post_cat = $post_category[0];
  40  
  41      if ( empty($post_author) )
  42          $post_author = $user_ID;
  43  
  44      if ( empty($post_status) )
  45          $post_status = 'draft';
  46  
  47      if ( empty($post_type) )
  48          $post_type = 'post';
  49  
  50      // Get the post ID.
  51      if ( $update )
  52          $post_ID = $ID;
  53  
  54      // Create a valid post name.  Drafts are allowed to have an empty
  55      // post name.
  56      if ( empty($post_name) ) {
  57          if ( 'draft' != $post_status )
  58              $post_name = sanitize_title($post_title);
  59      } else {
  60          $post_name = sanitize_title($post_name);
  61      }
  62  
  63  
  64      // If the post date is empty (due to having been new or a draft) and status is not 'draft', set date to now
  65      if (empty($post_date)) {
  66          if ( 'draft' != $post_status )
  67              $post_date = current_time('mysql');
  68      }
  69  
  70      if (empty($post_date_gmt)) {
  71          if ( 'draft' != $post_status )
  72              $post_date_gmt = get_gmt_from_date($post_date);
  73      }
  74  
  75      if ( 'publish' == $post_status ) {
  76          $now = gmdate('Y-m-d H:i:59');
  77          if ( mysql2date('U', $post_date_gmt) > mysql2date('U', $now) )
  78              $post_status = 'future';
  79      }
  80  
  81      if ( empty($comment_status) ) {
  82          if ( $update )
  83              $comment_status = 'closed';
  84          else
  85              $comment_status = get_settings('default_comment_status');
  86      }
  87      if ( empty($ping_status) )
  88          $ping_status = get_settings('default_ping_status');
  89      if ( empty($post_pingback) )
  90          $post_pingback = get_option('default_pingback_flag');
  91  
  92      if ( isset($to_ping) )
  93          $to_ping = preg_replace('|\s+|', "\n", $to_ping);
  94      else
  95          $to_ping = '';
  96  
  97      if ( ! isset($pinged) )
  98          $pinged = '';
  99  
 100      if ( isset($post_parent) )
 101          $post_parent = (int) $post_parent;
 102      else
 103          $post_parent = 0;
 104  
 105      if ( isset($menu_order) )
 106          $menu_order = (int) $menu_order;
 107      else
 108          $menu_order = 0;
 109  
 110      if ( !isset($post_password) )
 111          $post_password = '';
 112  
 113      if ( 'draft' != $post_status ) {
 114          $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_type = '$post_type' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1");
 115  
 116          if ($post_name_check) {
 117              $suffix = 2;
 118              while ($post_name_check) {
 119                  $alt_post_name = $post_name . "-$suffix";
 120                  $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_type = '$post_type' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1");
 121                  $suffix++;
 122              }
 123              $post_name = $alt_post_name;
 124          }
 125      }
 126  
 127      if ($update) {
 128          $wpdb->query(
 129              "UPDATE IGNORE $wpdb->posts SET
 130              post_author = '$post_author',
 131              post_date = '$post_date',
 132              post_date_gmt = '$post_date_gmt',
 133              post_content = '$post_content',
 134              post_content_filtered = '$post_content_filtered',
 135              post_title = '$post_title',
 136              post_excerpt = '$post_excerpt',
 137              post_status = '$post_status',
 138              post_type = '$post_type',
 139              comment_status = '$comment_status',
 140              ping_status = '$ping_status',
 141              post_password = '$post_password',
 142              post_name = '$post_name',
 143              to_ping = '$to_ping',
 144              pinged = '$pinged',
 145              post_modified = '".current_time('mysql')."',
 146              post_modified_gmt = '".current_time('mysql',1)."',
 147              post_parent = '$post_parent',
 148              menu_order = '$menu_order'
 149              WHERE ID = $post_ID");
 150      } else {
 151          $wpdb->query(
 152              "INSERT IGNORE INTO $wpdb->posts
 153              (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_modified, post_modified_gmt, post_parent, menu_order, post_mime_type)
 154              VALUES
 155              ('$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')");
 156              $post_ID = $wpdb->insert_id;
 157      }
 158  
 159      if ( empty($post_name) && 'draft' != $post_status ) {
 160          $post_name = sanitize_title($post_title, $post_ID);
 161          $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" );
 162      }
 163  
 164      wp_set_post_categories($post_ID, $post_category);
 165  
 166      if ( 'page' == $post_type ) {
 167          clean_page_cache($post_ID);
 168          wp_cache_delete($post_ID, 'pages');
 169      } else {
 170          clean_post_cache($post_ID);
 171      }
 172  
 173      // Set GUID
 174      if ( ! $update )
 175          $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
 176  
 177      if ( $update) {
 178          if ($previous_status != 'publish' && $post_status == 'publish') {
 179              // Reset GUID if transitioning to publish.
 180              $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
 181              do_action('private_to_published', $post_ID);
 182          }
 183  
 184          do_action('edit_post', $post_ID);
 185      }
 186  
 187      if ($post_status == 'publish' && $post_type == 'post') {
 188          do_action('publish_post', $post_ID);
 189  
 190          if ( !defined('WP_IMPORTING') ) {
 191              if ( $post_pingback )
 192                  $result = $wpdb->query("
 193                      INSERT INTO $wpdb->postmeta 
 194                      (post_id,meta_key,meta_value) 
 195                      VALUES ('$post_ID','_pingme','1')
 196                  ");
 197              $result = $wpdb->query("
 198                  INSERT INTO $wpdb->postmeta 
 199                  (post_id,meta_key,meta_value) 
 200                  VALUES ('$post_ID','_encloseme','1')
 201              ");
 202              wp_schedule_single_event(time(), 'do_pings');
 203          }
 204      } else if ($post_type == 'page') {
 205          wp_cache_delete('all_page_ids', 'pages');
 206          $wp_rewrite->flush_rules();
 207  
 208          if ( !empty($page_template) )
 209              if ( ! update_post_meta($post_ID, '_wp_page_template',  $page_template))
 210                  add_post_meta($post_ID, '_wp_page_template',  $page_template, true);
 211                  
 212          if ( $post_status == 'publish' )
 213              do_action('publish_page', $post_ID);
 214      }
 215  
 216      if ( 'future' == $post_status ) {
 217          wp_schedule_single_event(mysql2date('U', $post_date), 'publish_future_post', $post_ID);
 218      }
 219          
 220      do_action('save_post', $post_ID);
 221      do_action('wp_insert_post', $post_ID);
 222  
 223      return $post_ID;
 224  }
 225  
 226  function wp_insert_attachment($object, $file = false, $post_parent = 0) {
 227      global $wpdb, $user_ID;
 228  
 229      if ( is_object($object) )
 230          $object = get_object_vars($object);
 231  
 232      // Export array as variables
 233      extract($object);
 234  
 235      // Get the basics.
 236      $post_content    = apply_filters('content_save_pre',   $post_content);
 237      $post_excerpt    = apply_filters('excerpt_save_pre',   $post_excerpt);
 238      $post_title      = apply_filters('title_save_pre',     $post_title);
 239      $post_category   = apply_filters('category_save_pre',  $post_category);
 240      $post_name       = apply_filters('name_save_pre',      $post_name);
 241      $comment_status  = apply_filters('comment_status_pre', $comment_status);
 242      $ping_status     = apply_filters('ping_status_pre',    $ping_status);
 243      $post_mime_type  = apply_filters('post_mime_type_pre', $post_mime_type);
 244  
 245      // Make sure we set a valid category
 246      if (0 == count($post_category) || !is_array($post_category)) {
 247          $post_category = array(get_option('default_category'));
 248      }
 249      $post_cat = $post_category[0];
 250  
 251      if ( empty($post_author) )
 252          $post_author = $user_ID;
 253  
 254      $post_type = 'attachment';
 255      $post_status = 'inherit';
 256  
 257      // Are we updating or creating?
 258      $update = false;
 259      if ( !empty($ID) ) {
 260          $update = true;
 261          $post_ID = $ID;
 262      }
 263  
 264      // Create a valid post name.
 265      if ( empty($post_name) )
 266          $post_name = sanitize_title($post_title);
 267      else
 268          $post_name = sanitize_title($post_name);
 269  
 270      if (empty($post_date))
 271          $post_date = current_time('mysql');
 272      if (empty($post_date_gmt)) 
 273          $post_date_gmt = current_time('mysql', 1);
 274  
 275      if ( empty($comment_status) ) {
 276          if ( $update )
 277              $comment_status = 'closed';
 278          else
 279              $comment_status = get_settings('default_comment_status');
 280      }
 281      if ( empty($ping_status) )
 282          $ping_status = get_settings('default_ping_status');
 283      if ( empty($post_pingback) )
 284          $post_pingback = get_option('default_pingback_flag');
 285  
 286      if ( isset($to_ping) )
 287          $to_ping = preg_replace('|\s+|', "\n", $to_ping);
 288      else
 289          $to_ping = '';
 290  
 291      if ( isset($post_parent) )
 292          $post_parent = (int) $post_parent;
 293      else
 294          $post_parent = 0;
 295  
 296      if ( isset($menu_order) )
 297          $menu_order = (int) $menu_order;
 298      else
 299          $menu_order = 0;
 300  
 301      if ( !isset($post_password) )
 302          $post_password = '';
 303  
 304      if ( isset($to_ping) )
 305          $to_ping = preg_replace('|\s+|', "\n", $to_ping);
 306      else
 307          $to_ping = '';
 308  
 309      if ( ! isset($pinged) )
 310          $pinged = '';
 311  
 312      if ($update) {
 313          $wpdb->query(
 314              "UPDATE $wpdb->posts SET
 315              post_author = '$post_author',
 316              post_date = '$post_date',
 317              post_date_gmt = '$post_date_gmt',
 318              post_content = '$post_content',
 319              post_content_filtered = '$post_content_filtered',
 320              post_title = '$post_title',
 321              post_excerpt = '$post_excerpt',
 322              post_status = '$post_status',
 323              post_type = '$post_type',
 324              comment_status = '$comment_status',
 325              ping_status = '$ping_status',
 326              post_password = '$post_password',
 327              post_name = '$post_name',
 328              to_ping = '$to_ping',
 329              pinged = '$pinged',
 330              post_modified = '".current_time('mysql')."',
 331              post_modified_gmt = '".current_time('mysql',1)."',
 332              post_parent = '$post_parent',
 333              menu_order = '$menu_order',
 334              post_mime_type = '$post_mime_type',
 335              guid = '$guid'
 336              WHERE ID = $post_ID");
 337      } else {
 338          $wpdb->query(
 339              "INSERT INTO $wpdb->posts
 340              (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_modified, post_modified_gmt, post_parent, menu_order, post_mime_type, guid)
 341              VALUES
 342              ('$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', '$guid')");
 343              $post_ID = $wpdb->insert_id;
 344      }
 345  
 346      if ( empty($post_name) ) {
 347          $post_name = sanitize_title($post_title, $post_ID);
 348          $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" );
 349      }
 350  
 351      wp_set_post_categories($post_ID, $post_category);
 352  
 353      if ( $file )
 354          add_post_meta($post_ID, '_wp_attached_file', $file );
 355  
 356      clean_post_cache($post_ID);
 357  
 358      if ( $update) {
 359          do_action('edit_attachment', $post_ID);
 360      } else {
 361          do_action('add_attachment', $post_ID);
 362      }
 363  
 364      return $post_ID;
 365  }
 366  
 367  function wp_delete_attachment($postid) {
 368      global $wpdb;
 369      $postid = (int) $postid;
 370  
 371      if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = '$postid'") )
 372          return $post;
 373  
 374      if ( 'attachment' != $post->post_type )
 375          return false;
 376  
 377      $meta = get_post_meta($postid, '_wp_attachment_metadata', true);
 378      $file = get_post_meta($postid, '_wp_attached_file', true);
 379  
 380      $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = '$postid'");
 381  
 382      $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = '$postid'");
 383  
 384      $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = '$postid'");
 385  
 386      $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$postid'");
 387  
 388      if ( ! empty($meta['thumb']) ) {
 389          // Don't delete the thumb if another attachment uses it
 390          if (! $wpdb->get_row("SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE '%".$wpdb->escape($meta['thumb'])."%' AND post_id <> $postid")) {
 391              $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
 392              $thumbfile = apply_filters('wp_delete_file', $thumbfile);
 393              @ unlink($thumbfile);
 394          }
 395      }
 396  
 397      $file = apply_filters('wp_delete_file', $file);
 398  
 399      if ( ! empty($file) )
 400          @ unlink($file);
 401  
 402      do_action('delete_attachment', $postid);
 403  
 404      return $post;
 405  }
 406  
 407  function wp_get_single_post($postid = 0, $mode = OBJECT) {
 408      global $wpdb;
 409  
 410      $post = get_post($postid, $mode);
 411  
 412      // Set categories
 413      if($mode == OBJECT) {
 414          $post->post_category = wp_get_post_categories($postid);
 415      } 
 416      else {
 417          $post['post_category'] = wp_get_post_categories($postid);
 418      }
 419  
 420      return $post;
 421  }
 422  
 423  function wp_get_recent_posts($num = 10) {
 424      global $wpdb;
 425  
 426      // Set the limit clause, if we got a limit
 427      if ($num) {
 428          $limit = "LIMIT $num";
 429      }
 430  
 431      $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' ORDER BY post_date DESC $limit";
 432      $result = $wpdb->get_results($sql,ARRAY_A);
 433  
 434      return $result?$result:array();
 435  }
 436  
 437  function wp_update_post($postarr = array()) {
 438      global $wpdb;
 439  
 440      if ( is_object($postarr) )
 441          $postarr = get_object_vars($postarr);
 442  
 443      // First, get all of the original fields
 444      $post = wp_get_single_post($postarr['ID'], ARRAY_A);
 445  
 446      // Escape data pulled from DB.
 447      $post = add_magic_quotes($post);
 448  
 449      // Passed post category list overwrites existing category list if not empty.
 450       if ( isset($postarr['post_category']) && is_array($postarr['post_category'])
 451               && 0 != count($postarr['post_category']) )
 452           $post_cats = $postarr['post_category'];
 453       else 
 454           $post_cats = $post['post_category'];
 455  
 456      // Drafts shouldn't be assigned a date unless explicitly done so by the user
 457      if ( 'draft' == $post['post_status'] && empty($postarr['edit_date']) && empty($postarr['post_date']) && 
 458           ('0000-00-00 00:00:00' == $post['post_date']) )
 459          $clear_date = true;
 460      else
 461          $clear_date = false;
 462  
 463       // Merge old and new fields with new fields overwriting old ones.
 464       $postarr = array_merge($post, $postarr);
 465       $postarr['post_category'] = $post_cats;
 466      if ( $clear_date ) {
 467          $postarr['post_date'] = '';
 468          $postarr['post_date_gmt'] = '';
 469      }
 470  
 471      if ($postarr['post_type'] == 'attachment')
 472          return wp_insert_attachment($postarr);
 473  
 474      return wp_insert_post($postarr);
 475  }
 476  
 477  function wp_publish_post($post_id) {
 478      $post = get_post($post_id);
 479  
 480      if ( empty($post) )
 481          return;
 482  
 483      if ( 'publish' == $post->post_status )
 484          return;
 485  
 486      return wp_update_post(array('post_status' => 'publish', 'ID' => $post_id));    
 487  }
 488  
 489  function wp_get_post_categories($post_ID = 0) {
 490      global $wpdb;
 491  
 492      $post_ID = (int) $post_ID;
 493  
 494      $sql = "SELECT category_id 
 495          FROM $wpdb->post2cat 
 496          WHERE post_id = '$post_ID' 
 497          ORDER BY category_id";
 498  
 499      $result = $wpdb->get_col($sql);
 500  
 501      if ( !$result )
 502          $result = array();
 503  
 504      return array_unique($result);
 505  }
 506  
 507  function wp_set_post_categories($post_ID = 0, $post_categories = array()) {
 508      global $wpdb;
 509      // If $post_categories isn't already an array, make it one:
 510      if (!is_array($post_categories) || 0 == count($post_categories))
 511          $post_categories = array(get_option('default_category'));
 512  
 513      $post_categories = array_unique($post_categories);
 514  
 515      // First the old categories
 516      $old_categories = $wpdb->get_col("
 517          SELECT category_id 
 518          FROM $wpdb->post2cat 
 519          WHERE post_id = $post_ID");
 520  
 521      if (!$old_categories) {
 522          $old_categories = array();
 523      } else {
 524          $old_categories = array_unique($old_categories);
 525      }
 526  
 527      // Delete any?
 528      $delete_cats = array_diff($old_categories,$post_categories);
 529  
 530      if ($delete_cats) {
 531          foreach ($delete_cats as $del) {
 532              $wpdb->query("
 533                  DELETE FROM $wpdb->post2cat 
 534                  WHERE category_id = $del 
 535                      AND post_id = $post_ID 
 536                  ");
 537          }
 538      }
 539  
 540      // Add any?
 541      $add_cats = array_diff($post_categories, $old_categories);
 542  
 543      if ($add_cats) {
 544          foreach ($add_cats as $new_cat) {
 545              $wpdb->query("
 546                  INSERT INTO $wpdb->post2cat (post_id, category_id) 
 547                  VALUES ($post_ID, $new_cat)");
 548          }
 549      }
 550  
 551      // Update category counts.
 552      $all_affected_cats = array_unique(array_merge($post_categories, $old_categories));
 553      foreach ( $all_affected_cats as $cat_id ) {
 554          $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status = 'publish' AND post_type = 'post' AND category_id = '$cat_id'");
 555          $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'");
 556          wp_cache_delete($cat_id, 'category');
 557      }
 558  }    // wp_set_post_categories()
 559  
 560  function wp_delete_post($postid = 0) {
 561      global $wpdb, $wp_rewrite;
 562      $postid = (int) $postid;
 563  
 564      if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $postid") )
 565          return $post;
 566  
 567      if ( 'attachment' == $post->post_type )
 568          return wp_delete_attachment($postid);
 569  
 570      do_action('delete_post', $postid);
 571  
 572      if ( 'publish' == $post->post_status && 'post' == $post->post_type ) {
 573          $categories = wp_get_post_categories($post->ID);
 574          if( is_array( $categories ) ) {
 575              foreach ( $categories as $cat_id ) {
 576                  $wpdb->query("UPDATE $wpdb->categories SET category_count = category_count - 1 WHERE cat_ID = '$cat_id'");
 577                  wp_cache_delete($cat_id, 'category');
 578              }
 579          }
 580      }
 581  
 582      if ( 'page' == $post->post_type )
 583          $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_type = 'page'");
 584  
 585      $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = $postid");
 586  
 587      $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid");
 588  
 589      $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = $postid");
 590  
 591      $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = $postid");
 592  
 593      if ( 'page' == $post->type ) {
 594          wp_cache_delete('all_page_ids', 'pages');
 595          $wp_rewrite->flush_rules();
 596      }
 597  
 598      return $post;
 599  }
 600  
 601  /**** /DB Functions ****/
 602  
 603  /**** Misc ****/
 604  
 605  // get permalink from post ID
 606  function post_permalink($post_id = 0, $mode = '') { // $mode legacy
 607      return get_permalink($post_id);
 608  }
 609  
 610  // Get author's preferred display name
 611  function get_author_name( $auth_id ) {
 612      $authordata = get_userdata( $auth_id );
 613  
 614      return $authordata->display_name;
 615  }
 616  
 617  // get extended entry info (<!--more-->)
 618  function get_extended($post) {
 619      list($main,$extended) = explode('<!--more-->', $post, 2);
 620  
 621      // Strip leading and trailing whitespace
 622      $main = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$main);
 623      $extended = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$extended);
 624  
 625      return array('main' => $main, 'extended' => $extended);
 626  }
 627  
 628  // do trackbacks for a list of urls
 629  // borrowed from edit.php
 630  // accepts a comma-separated list of trackback urls and a post id
 631  function trackback_url_list($tb_list, $post_id) {
 632      if (!empty($tb_list)) {
 633          // get post data
 634          $postdata = wp_get_single_post($post_id, ARRAY_A);
 635  
 636          // import postdata as variables
 637          extract($postdata);
 638  
 639          // form an excerpt
 640          $excerpt = strip_tags($post_excerpt?$post_excerpt:$post_content);
 641  
 642          if (strlen($excerpt) > 255) {
 643              $excerpt = substr($excerpt,0,252) . '...';
 644          }
 645  
 646          $trackback_urls = explode(',', $tb_list);
 647          foreach($trackback_urls as $tb_url) {
 648              $tb_url = trim($tb_url);
 649              trackback($tb_url, stripslashes($post_title), $excerpt, $post_id);
 650          }
 651      }
 652  }
 653  
 654  function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
 655      global $wpdb;
 656  
 657      do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
 658  
 659      if ( preg_match_all('/&#(\d+);/', $comment . $author . $url, $chars) ) {
 660          foreach ($chars[1] as $char) {
 661              // If it's an encoded char in the normal ASCII set, reject
 662              if ( 38 == $char )
 663                  continue; // Unless it's &
 664              if ($char < 128)
 665                  return true;
 666          }
 667      }
 668  
 669      $mod_keys = trim( get_settings('blacklist_keys') );
 670      if ('' == $mod_keys )
 671          return false; // If moderation keys are empty
 672      $words = explode("\n", $mod_keys );
 673  
 674      foreach ($words as $word) {
 675          $word = trim($word);
 676  
 677          // Skip empty lines
 678          if ( empty($word) ) { continue; }
 679  
 680          // Do some escaping magic so that '#' chars in the 
 681          // spam words don't break things:
 682          $word = preg_quote($word, '#');
 683  
 684          $pattern = "#$word#i"; 
 685          if ( preg_match($pattern, $author    ) ) return true;
 686          if ( preg_match($pattern, $email     ) ) return true;
 687          if ( preg_match($pattern, $url       ) ) return true;
 688          if ( preg_match($pattern, $comment   ) ) return true;
 689          if ( preg_match($pattern, $user_ip   ) ) return true;
 690          if ( preg_match($pattern, $user_agent) ) return true;
 691      }
 692  
 693      if ( isset($_SERVER['REMOTE_ADDR']) ) {
 694          if ( wp_proxy_check($_SERVER['REMOTE_ADDR']) ) return true;
 695      }
 696  
 697      return false;
 698  }
 699  
 700  function wp_proxy_check($ipnum) {
 701      if ( get_option('open_proxy_check') && isset($ipnum) ) {
 702          $rev_ip = implode( '.', array_reverse( explode( '.', $ipnum ) ) );
 703          $lookup = $rev_ip . '.opm.blitzed.org.';
 704          if ( $lookup != gethostbyname( $lookup ) )
 705              return true;
 706      }
 707  
 708      return false;
 709  }
 710  
 711  function do_trackbacks($post_id) {
 712      global $wpdb;
 713  
 714      $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $post_id");
 715      $to_ping = get_to_ping($post_id);
 716      $pinged  = get_pung($post_id);
 717      if ( empty($to_ping) ) {
 718          $wpdb->query("UPDATE $wpdb->posts SET to_ping = '' WHERE ID = '$post_id'");
 719          return;
 720      }
 721  
 722      if (empty($post->post_excerpt))
 723          $excerpt = apply_filters('the_content', $post->post_content);
 724      else
 725          $excerpt = apply_filters('the_excerpt', $post->post_excerpt);
 726      $excerpt = str_replace(']]>', ']]&gt;', $excerpt);
 727      $excerpt = strip_tags($excerpt);
 728      if ( function_exists('mb_strcut') ) // For international trackbacks
 729          $excerpt = mb_strcut($excerpt, 0, 252, get_settings('blog_charset')) . '...';
 730      else
 731          $excerpt = substr($excerpt, 0, 252) . '...';
 732  
 733      $post_title = apply_filters('the_title', $post->post_title);
 734      $post_title = strip_tags($post_title);
 735  
 736      if ($to_ping) : foreach ($to_ping as $tb_ping) :
 737          $tb_ping = trim($tb_ping);
 738          if ( !in_array($tb_ping, $pinged) ) {
 739              trackback($tb_ping, $post_title, $excerpt, $post_id);
 740              $pinged[] = $tb_ping;
 741          } else {
 742              $wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = '$post_id'");
 743          }
 744      endforeach; endif;
 745  }
 746  
 747  function get_pung($post_id) { // Get URIs already pung for a post
 748      global $wpdb;
 749      $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
 750      $pung = trim($pung);
 751      $pung = preg_split('/\s/', $pung);
 752      $pung = apply_filters('get_pung', $pung);
 753      return $pung;
 754  }
 755  
 756  function get_enclosed($post_id) { // Get enclosures already enclosed for a post
 757      global $wpdb;
 758      $custom_fields = get_post_custom( $post_id );
 759      $pung = array();
 760      if ( !is_array( $custom_fields ) )
 761          return $pung;
 762  
 763      foreach ( $custom_fields as $key => $val ) {
 764          if ( 'enclosure' != $key || !is_array( $val ) )
 765              continue;
 766          foreach( $val as $enc ) {
 767              $enclosure = split( "\n", $enc );
 768              $pung[] = trim( $enclosure[ 0 ] );
 769          }
 770      }
 771      $pung = apply_filters('get_enclosed', $pung);
 772      return $pung;
 773  }
 774  
 775  function get_to_ping($post_id) { // Get any URIs in the todo list
 776      global $wpdb;
 777      $to_ping = $wpdb->get_var("SELECT to_ping FROM $wpdb->posts WHERE ID = $post_id");
 778      $to_ping = trim($to_ping);
 779      $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY);
 780      $to_ping = apply_filters('get_to_ping',  $to_ping);
 781      return $to_ping;
 782  }
 783  
 784  function add_ping($post_id, $uri) { // Add a URI to those already pung
 785      global $wpdb;
 786      $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
 787      $pung = trim($pung);
 788      $pung = preg_split('/\s/', $pung);
 789      $pung[] = $uri;
 790      $new = implode("\n", $pung);
 791      $new = apply_filters('add_ping', $new);
 792      return $wpdb->query("UPDATE $wpdb->posts SET pinged = '$new' WHERE ID = $post_id");
 793  }
 794  
 795  //fetches the pages returned as a FLAT list, but arranged in order of their hierarchy, i.e., child parents
 796  //immediately follow their parents
 797  function get_page_hierarchy($posts, $parent = 0) {
 798      $result = array ( );
 799      if ($posts) { foreach ($posts as $post) {
 800          if ($post->post_parent == $parent) {
 801              $result[$post->ID] = $post->post_name;
 802              $children = get_page_hierarchy($posts, $post->ID);
 803              $result += $children; //append $children to $result
 804          }
 805      } }
 806      return $result;
 807  }
 808  
 809  function generate_page_uri_index() {
 810      global $wpdb;
 811  
 812      //get pages in order of hierarchy, i.e. children after parents
 813      $posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page'"));
 814      //now reverse it, because we need parents after children for rewrite rules to work properly
 815      $posts = array_reverse($posts, true);
 816  
 817      $page_uris = array();
 818      $page_attachment_uris = array();
 819  
 820      if ($posts) {
 821  
 822          foreach ($posts as $id => $post) {
 823  
 824              // URI => page name
 825              $uri = get_page_uri($id);
 826              $attachments = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = '$id'");
 827              if ( $attachments ) {
 828                  foreach ( $attachments as $attachment ) {
 829                      $attach_uri = get_page_uri($attachment->ID);
 830                      $page_attachment_uris[$attach_uri] = $attachment->ID;
 831                  }
 832              }
 833  
 834              $page_uris[$uri] = $id;
 835          }
 836  
 837          update_option('page_uris', $page_uris);
 838  
 839          if ( $page_attachment_uris )
 840              update_option('page_attachment_uris', $page_attachment_uris);
 841      }
 842  }
 843  
 844  function get_post_status($ID = '') {
 845      $post = get_post($ID);
 846  
 847      if ( is_object($post) ) {
 848          if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) )
 849              return get_post_status($post->post_parent);
 850          else
 851              return $post->post_status;
 852      }
 853  
 854      return false;
 855  }
 856  
 857  function get_post_type($post = false) {
 858      global $wpdb, $posts;
 859  
 860      if ( false === $post )
 861          $post = $posts[0];
 862      elseif ( (int) $post )
 863          $post = get_post($post, OBJECT);
 864  
 865      if ( is_object($post) )
 866          return $post->post_type;
 867  
 868      return false;
 869  }
 870  
 871  // Takes a post ID, returns its mime type.
 872  function get_post_mime_type($ID = '') {
 873      $post = & get_post($ID);
 874  
 875      if ( is_object($post) )
 876          return $post->post_mime_type;
 877  
 878      return false;
 879  }
 880  
 881  function get_attached_file($attachment_id) {
 882      return get_post_meta($attachment_id, '_wp_attached_file', true);
 883  }
 884  
 885  function wp_mkdir_p($target) {
 886      // from php.net/mkdir user contributed notes
 887      if (file_exists($target)) {
 888          if (! @ is_dir($target))
 889              return false;
 890          else
 891              return true;
 892      }
 893  
 894      // Attempting to create the directory may clutter up our display.
 895      if (@ mkdir($target)) {
 896          $stat = @ stat(dirname($target));
 897          $dir_perms = $stat['mode'] & 0007777;  // Get the permission bits.
 898          @ chmod($target, $dir_perms);
 899          return true;
 900      } else {
 901          if ( is_dir(dirname($target)) )
 902              return false;
 903      }
 904  
 905      // If the above failed, attempt to create the parent node, then try again.
 906      if (wp_mkdir_p(dirname($target)))
 907          return wp_mkdir_p($target);
 908  
 909      return false;
 910  }
 911  
 912  // Returns an array containing the current upload directory's path and url, or an error message.
 913  function wp_upload_dir() {
 914      $siteurl = get_settings('siteurl');
 915      //prepend ABSPATH to $dir and $siteurl to $url if they're not already there
 916      $path = str_replace(ABSPATH, '', trim(get_settings('upload_path')));
 917      $dir = ABSPATH . $path;
 918      $url = trailingslashit($siteurl) . $path;
 919  
 920      if ( $dir == ABSPATH ) { //the option was empty
 921          $dir = ABSPATH . 'wp-content/uploads';
 922      }
 923  
 924      if ( defined('UPLOADS') ) {
 925          $dir = ABSPATH . UPLOADS;
 926          $url = trailingslashit($siteurl) . UPLOADS;
 927      }
 928  
 929      if ( get_settings('uploads_use_yearmonth_folders')) {
 930          // Generate the yearly and monthly dirs
 931          $time = current_time( 'mysql' );
 932          $y = substr( $time, 0, 4 );
 933          $m = substr( $time, 5, 2 );
 934          $dir = $dir . "/$y/$m";
 935          $url = $url . "/$y/$m";
 936      }
 937  
 938      // Make sure we have an uploads dir
 939      if ( ! wp_mkdir_p( $dir ) ) {
 940          $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), $dir);
 941          return array('error' => $message);
 942      }
 943  
 944      $uploads = array('path' => $dir, 'url' => $url, 'error' => false);
 945      return apply_filters('upload_dir', $uploads);
 946  }
 947  
 948  function wp_upload_bits($name, $type, $bits) {
 949      if ( empty($name) )
 950          return array('error' => "Empty filename");
 951  
 952      $upload = wp_upload_dir();
 953  
 954      if ( $upload['error'] !== false )
 955          return $upload;
 956  
 957      $number = '';
 958      $filename = $name;
 959      $path_parts = pathinfo($filename);
 960      $ext = $path_parts['extension'];
 961      if ( empty($ext) )
 962          $ext = '';
 963      else
 964          $ext = ".$ext";
 965      while ( file_exists($upload['path'] . "/$filename") ) {
 966          if ( '' == "$number$ext" )
 967              $filename = $filename . ++$number . $ext;
 968          else
 969              $filename = str_replace("$number$ext", ++$number . $ext, $filename);
 970      }
 971  
 972      $new_file = $upload['path'] . "/$filename";
 973      if ( ! wp_mkdir_p( dirname($new_file) ) ) {
 974          $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), dirname($new_file));
 975          return array('error' => $message);
 976      }
 977  
 978      $ifp = @ fopen($new_file, 'wb');
 979      if ( ! $ifp )
 980          return array('error' => "Could not write file $new_file.");
 981  
 982      $success = @ fwrite($ifp, $bits);
 983      fclose($ifp);
 984      // Set correct file permissions
 985      $stat = @ stat(dirname($new_file));
 986      $perms = $stat['mode'] & 0007777;
 987      $perms = $perms & 0000666;
 988      @ chmod($new_file, $perms);
 989  
 990      // Compute the URL
 991      $url = $upload['url'] . "/$filename";
 992  
 993      return array('file' => $new_file, 'url' => $url, 'error' => false);
 994  }
 995  
 996  function do_all_pings() {
 997      global $wpdb;
 998  
 999      // Do pingbacks
1000      while ($ping = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pingme' LIMIT 1")) {
1001          $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme';");
1002          pingback($ping->post_content, $ping->ID);
1003      }
1004      
1005      // Do Enclosures
1006      while ($enclosure = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1")) {
1007          $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$enclosure->ID} AND meta_key = '_encloseme';");
1008          do_enclose($enclosure->post_content, $enclosure->ID);
1009      }
1010  
1011      // Do Trackbacks
1012      $trackbacks = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE CHAR_LENGTH(TRIM(to_ping)) > 7 AND post_status = 'publish'");
1013      if ( is_array($trackbacks) ) {
1014          foreach ( $trackbacks as $trackback ) {
1015              do_trackbacks($trackback->ID);
1016          }
1017      }
1018  
1019      //Do Update Services/Generic Pings
1020      generic_ping();
1021  }
1022  
1023  /**
1024   * Places a textarea according to the current user's preferences, filled with $content.
1025   * Also places a script block that enables tabbing between Title and Content.
1026   *
1027   * @param string Editor contents
1028   * @param string (optional) Previous form field's ID (for tabbing support)
1029   */
1030  function the_editor($content, $id = 'content', $prev_id = 'title') {
1031      $rows = get_settings('default_post_edit_rows');
1032      if (($rows < 3) || ($rows > 100))
1033          $rows = 12;
1034  
1035      $rows = "rows='$rows'";
1036  
1037      the_quicktags();
1038  
1039      if ( user_can_richedit() )
1040          add_filter('the_editor_content', 'wp_richedit_pre');
1041  
1042      $the_editor = apply_filters('the_editor', "<div><textarea class='mceEditor' $rows cols='40' name='$id' tabindex='2' id='$id'>%s</textarea></div>\n");
1043      $the_editor_content = apply_filters('the_editor_content', $content);
1044  
1045      printf($the_editor, $the_editor_content);
1046  
1047      ?>
1048      <script type="text/javascript">
1049      //<!--
1050      edCanvas = document.getElementById('<?php echo $id; ?>');
1051      <?php if ( user_can_richedit() ) : ?>
1052      // This code is meant to allow tabbing from Title to Post (TinyMCE).
1053      if ( tinyMCE.isMSIE )
1054          document.getElementById('<?php echo $prev_id; ?>').onkeydown = function (e)
1055              {
1056                  e = e ? e : window.event;
1057                  if (e.keyCode == 9 && !e.shiftKey && !e.controlKey && !e.altKey) {
1058                      var i = tinyMCE.selectedInstance;
1059                      if(typeof i ==  'undefined')
1060                          return true;
1061                                      tinyMCE.execCommand("mceStartTyping");
1062                      this.blur();
1063                      i.contentWindow.focus();
1064                      e.returnValue = false;
1065                      return false;
1066                  }
1067              }
1068      else
1069          document.getElementById('<?php echo $prev_id; ?>').onkeypress = function (e)
1070              {
1071                  e = e ? e : window.event;
1072                  if (e.keyCode == 9 && !e.shiftKey && !e.controlKey && !e.altKey) {
1073                      var i = tinyMCE.selectedInstance;
1074                      if(typeof i ==  'undefined')
1075                          return true;
1076                                      tinyMCE.execCommand("mceStartTyping");
1077                      this.blur();
1078                      i.contentWindow.focus();
1079                      e.returnValue = false;
1080                      return false;
1081                  }
1082              }
1083      <?php endif; ?>
1084      //-->
1085      </script>
1086      <?php
1087  }
1088  
1089  ?>

Your comment here...

Name: Location:
Comments:


List: Classes | Functions | Variables | Constants | Tables

Generated: Wed Jun 7 12:10:26 2006 Courtesy of Taragana