Make WordPress Core

Changeset 3851


Ignore:
Timestamp:
06/07/2006 11:17:59 PM (19 years ago)
Author:
ryan
Message:

Reworg post/page/attachment functions. #2525

Location:
trunk
Files:
6 edited
2 moved

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/comment.php

    r3740 r3851  
    196196}
    197197
     198function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
     199    global $wpdb;
     200
     201    do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
     202
     203    if ( preg_match_all('/&#(\d+);/', $comment . $author . $url, $chars) ) {
     204        foreach ($chars[1] as $char) {
     205            // If it's an encoded char in the normal ASCII set, reject
     206            if ( 38 == $char )
     207                continue; // Unless it's &
     208            if ($char < 128)
     209                return true;
     210        }
     211    }
     212
     213    $mod_keys = trim( get_settings('blacklist_keys') );
     214    if ('' == $mod_keys )
     215        return false; // If moderation keys are empty
     216    $words = explode("\n", $mod_keys );
     217
     218    foreach ($words as $word) {
     219        $word = trim($word);
     220
     221        // Skip empty lines
     222        if ( empty($word) ) { continue; }
     223
     224        // Do some escaping magic so that '#' chars in the
     225        // spam words don't break things:
     226        $word = preg_quote($word, '#');
     227
     228        $pattern = "#$word#i";
     229        if ( preg_match($pattern, $author    ) ) return true;
     230        if ( preg_match($pattern, $email     ) ) return true;
     231        if ( preg_match($pattern, $url       ) ) return true;
     232        if ( preg_match($pattern, $comment   ) ) return true;
     233        if ( preg_match($pattern, $user_ip   ) ) return true;
     234        if ( preg_match($pattern, $user_agent) ) return true;
     235    }
     236
     237    if ( isset($_SERVER['REMOTE_ADDR']) ) {
     238        if ( wp_proxy_check($_SERVER['REMOTE_ADDR']) ) return true;
     239    }
     240
     241    return false;
     242}
     243
    198244function wp_delete_comment($comment_id) {
    199245    global $wpdb;
  • trunk/wp-includes/functions.php

    r3843 r3851  
    339339    wp_cache_delete($name, 'options');
    340340    return true;
    341 }
    342 
    343 function add_post_meta($post_id, $key, $value, $unique = false) {
    344     global $wpdb, $post_meta_cache;
    345 
    346     if ( $unique ) {
    347         if ( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
    348 = '$key' AND post_id = '$post_id'") ) {
    349             return false;
    350         }
    351     }
    352 
    353     $original = $value;
    354     if ( is_array($value) || is_object($value) )
    355         $value = $wpdb->escape(serialize($value));
    356 
    357     $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_id','$key','$value')");
    358 
    359     $post_meta_cache['$post_id'][$key][] = $original;
    360 
    361     return true;
    362 }
    363 
    364 function delete_post_meta($post_id, $key, $value = '') {
    365     global $wpdb, $post_meta_cache;
    366 
    367     if ( empty($value) ) {
    368         $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
    369 post_id = '$post_id' AND meta_key = '$key'");
    370     } else {
    371         $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
    372 post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
    373     }
    374 
    375     if ( !$meta_id )
    376         return false;
    377 
    378     if ( empty($value) ) {
    379         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
    380 AND meta_key = '$key'");
    381         unset($post_meta_cache['$post_id'][$key]);
    382     } else {
    383         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
    384 AND meta_key = '$key' AND meta_value = '$value'");
    385         $cache_key = $post_meta_cache['$post_id'][$key];
    386         if ($cache_key) foreach ( $cache_key as $index => $data )
    387             if ( $data == $value )
    388                 unset($post_meta_cache['$post_id'][$key][$index]);
    389     }
    390 
    391     unset($post_meta_cache['$post_id'][$key]);
    392 
    393     return true;
    394 }
    395 
    396 function get_post_meta($post_id, $key, $single = false) {
    397     global $wpdb, $post_meta_cache;
    398 
    399     if ( isset($post_meta_cache[$post_id][$key]) ) {
    400         if ( $single ) {
    401             return maybe_unserialize( $post_meta_cache[$post_id][$key][0] );
    402         } else {
    403             return maybe_unserialize( $post_meta_cache[$post_id][$key] );
    404         }
    405     }
    406 
    407     $metalist = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'", ARRAY_N);
    408 
    409     $values = array();
    410     if ( $metalist ) {
    411         foreach ($metalist as $metarow) {
    412             $values[] = $metarow[0];
    413         }
    414     }
    415 
    416     if ( $single ) {
    417         if ( count($values) ) {
    418             $return = maybe_unserialize( $values[0] );
    419         } else {
    420             return '';
    421         }
    422     } else {
    423         $return = $values;
    424     }
    425 
    426     return maybe_unserialize($return);
    427 }
    428 
    429 function update_post_meta($post_id, $key, $value, $prev_value = '') {
    430     global $wpdb, $post_meta_cache;
    431 
    432     $original_value = $value;
    433     if ( is_array($value) || is_object($value) )
    434         $value = $wpdb->escape(serialize($value));
    435 
    436     $original_prev = $prev_value;
    437     if ( is_array($prev_value) || is_object($prev_value) )
    438         $prev_value = $wpdb->escape(serialize($prev_value));
    439 
    440     if (! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
    441 = '$key' AND post_id = '$post_id'") ) {
    442         return false;
    443     }
    444 
    445     if ( empty($prev_value) ) {
    446         $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
    447 meta_key = '$key' AND post_id = '$post_id'");
    448         $cache_key = $post_meta_cache['$post_id'][$key];
    449         if ( !empty($cache_key) )
    450             foreach ($cache_key as $index => $data)
    451                 $post_meta_cache['$post_id'][$key][$index] = $original_value;
    452     } else {
    453         $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
    454 meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'");
    455         $cache_key = $post_meta_cache['$post_id'][$key];
    456         if ( !empty($cache_key) )
    457             foreach ($cache_key as $index => $data)
    458                 if ( $data == $original_prev )
    459                     $post_meta_cache['$post_id'][$key][$index] = $original_value;
    460     }
    461 
    462     return true;
    463 }
    464 
    465 // Retrieves post data given a post ID or post object.
    466 // Handles post caching.
    467 function &get_post(&$post, $output = OBJECT) {
    468     global $post_cache, $wpdb;
    469 
    470     if ( empty($post) ) {
    471         if ( isset($GLOBALS['post']) )
    472             $_post = & $GLOBALS['post'];
    473         else
    474             $_post = null;
    475     } elseif ( is_object($post) ) {
    476         if ( 'page' == $post->post_type )
    477             return get_page($post, $output);
    478         if ( !isset($post_cache[$post->ID]) )
    479             $post_cache[$post->ID] = &$post;
    480         $_post = & $post_cache[$post->ID];
    481     } else {
    482         if ( $_post = wp_cache_get($post, 'pages') )
    483             return get_page($_post, $output);
    484         elseif ( isset($post_cache[$post]) )
    485             $_post = & $post_cache[$post];
    486         else {
    487             $query = "SELECT * FROM $wpdb->posts WHERE ID = '$post' LIMIT 1";
    488             $_post = & $wpdb->get_row($query);
    489             if ( 'page' == $_post->post_type )
    490                 return get_page($_post, $output);
    491             $post_cache[$post] = & $_post;
    492         }
    493     }
    494 
    495     if ( defined(WP_IMPORTING) )
    496         unset($post_cache);
    497 
    498     if ( $output == OBJECT ) {
    499         return $_post;
    500     } elseif ( $output == ARRAY_A ) {
    501         return get_object_vars($_post);
    502     } elseif ( $output == ARRAY_N ) {
    503         return array_values(get_object_vars($_post));
    504     } else {
    505         return $_post;
    506     }
    507 }
    508 
    509 function &get_children($post = 0, $output = OBJECT) {
    510     global $post_cache, $wpdb;
    511 
    512     if ( empty($post) ) {
    513         if ( isset($GLOBALS['post']) )
    514             $post_parent = & $GLOBALS['post']->post_parent;
    515         else
    516             return false;
    517     } elseif ( is_object($post) ) {
    518         $post_parent = $post->post_parent;
    519     } else {
    520         $post_parent = $post;
    521     }
    522 
    523     $post_parent = (int) $post_parent;
    524 
    525     $query = "SELECT * FROM $wpdb->posts WHERE post_parent = $post_parent";
    526 
    527     $children = $wpdb->get_results($query);
    528 
    529     if ( $children ) {
    530         foreach ( $children as $key => $child ) {
    531             $post_cache[$child->ID] =& $children[$key];
    532             $kids[$child->ID] =& $children[$key];
    533         }
    534     } else {
    535         return false;
    536     }
    537 
    538     if ( $output == OBJECT ) {
    539         return $kids;
    540     } elseif ( $output == ARRAY_A ) {
    541         foreach ( $kids as $kid )
    542             $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
    543         return $weeuns;
    544     } elseif ( $output == ARRAY_N ) {
    545         foreach ( $kids as $kid )
    546             $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
    547         return $babes;
    548     } else {
    549         return $kids;
    550     }
    551 }
    552 
    553 function get_page_by_path($page_path, $output = OBJECT) {
    554     global $wpdb;
    555     $page_path = rawurlencode(urldecode($page_path));
    556     $page_path = str_replace('%2F', '/', $page_path);
    557     $page_path = str_replace('%20', ' ', $page_path);
    558     $page_paths = '/' . trim($page_path, '/');
    559     $leaf_path  = sanitize_title(basename($page_paths));
    560     $page_paths = explode('/', $page_paths);
    561     foreach($page_paths as $pathdir)
    562         $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
    563 
    564     $pages = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = '$leaf_path' AND post_type='page'");
    565 
    566     if ( empty($pages) )
    567         return NULL;
    568 
    569     foreach ($pages as $page) {
    570         $path = '/' . $leaf_path;
    571         $curpage = $page;
    572         while ($curpage->post_parent != 0) {
    573             $curpage = $wpdb->get_row("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = '$curpage->post_parent' and post_type='page'");
    574             $path = '/' . $curpage->post_name . $path;
    575         }
    576 
    577         if ( $path == $full_path )
    578             return get_page($page->ID, $output);
    579     }
    580 
    581     return NULL;
    582 }
    583 
    584 // Retrieves page data given a page ID or page object.
    585 // Handles page caching.
    586 function &get_page(&$page, $output = OBJECT) {
    587     global $wpdb;
    588 
    589     if ( empty($page) ) {
    590         if ( isset($GLOBALS['page']) ) {
    591             $_page = & $GLOBALS['page'];
    592             wp_cache_add($_page->ID, $_page, 'pages');
    593         } else {
    594             $_page = null;
    595         }
    596     } elseif ( is_object($page) ) {
    597         if ( 'post' == $page->post_type )
    598             return get_post($page, $output);
    599         wp_cache_add($page->ID, $page, 'pages');
    600         $_page = $page;
    601     } else {
    602         if ( isset($GLOBALS['page']) && ($page == $GLOBALS['page']->ID) ) {
    603             $_page = & $GLOBALS['page'];
    604             wp_cache_add($_page->ID, $_page, 'pages');
    605         } elseif ( $_page = $GLOBALS['post_cache'][$page] ) {
    606             return get_post($page, $output);
    607         } elseif ( $_page = wp_cache_get($page, 'pages') ) {
    608             // Got it.
    609         } else {
    610             $query = "SELECT * FROM $wpdb->posts WHERE ID= '$page' LIMIT 1";
    611             $_page = & $wpdb->get_row($query);
    612             if ( 'post' == $_page->post_type )
    613                 return get_post($_page, $output);
    614             wp_cache_add($_page->ID, $_page, 'pages');
    615         }
    616     }
    617 
    618     if ( $output == OBJECT ) {
    619         return $_page;
    620     } elseif ( $output == ARRAY_A ) {
    621         return get_object_vars($_page);
    622     } elseif ( $output == ARRAY_N ) {
    623         return array_values(get_object_vars($_page));
    624     } else {
    625         return $_page;
    626     }
    627 }
    628 
    629 function get_all_page_ids() {
    630     global $wpdb;
    631 
    632     if ( ! $page_ids = wp_cache_get('all_page_ids', 'pages') ) {
    633         $page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'page'");
    634         wp_cache_add('all_page_ids', $page_ids, 'pages');
    635     }
    636 
    637     return $page_ids;
    638341}
    639342
     
    1275978}
    1276979
    1277 function wp_head() {
    1278     do_action('wp_head');
    1279 }
    1280 
    1281 function wp_footer() {
    1282     do_action('wp_footer');
    1283 }
    1284 
    1285980/*
    1286981add_query_arg: Returns a modified querystring by adding
     
    16011296}
    16021297
     1298function wp_mkdir_p($target) {
     1299    // from php.net/mkdir user contributed notes
     1300    if (file_exists($target)) {
     1301        if (! @ is_dir($target))
     1302            return false;
     1303        else
     1304            return true;
     1305    }
     1306
     1307    // Attempting to create the directory may clutter up our display.
     1308    if (@ mkdir($target)) {
     1309        $stat = @ stat(dirname($target));
     1310        $dir_perms = $stat['mode'] & 0007777;  // Get the permission bits.
     1311        @ chmod($target, $dir_perms);
     1312        return true;
     1313    } else {
     1314        if ( is_dir(dirname($target)) )
     1315            return false;
     1316    }
     1317
     1318    // If the above failed, attempt to create the parent node, then try again.
     1319    if (wp_mkdir_p(dirname($target)))
     1320        return wp_mkdir_p($target);
     1321
     1322    return false;
     1323}
     1324
     1325// Returns an array containing the current upload directory's path and url, or an error message.
     1326function wp_upload_dir() {
     1327    $siteurl = get_settings('siteurl');
     1328    //prepend ABSPATH to $dir and $siteurl to $url if they're not already there
     1329    $path = str_replace(ABSPATH, '', trim(get_settings('upload_path')));
     1330    $dir = ABSPATH . $path;
     1331    $url = trailingslashit($siteurl) . $path;
     1332
     1333    if ( $dir == ABSPATH ) { //the option was empty
     1334        $dir = ABSPATH . 'wp-content/uploads';
     1335    }
     1336
     1337    if ( defined('UPLOADS') ) {
     1338        $dir = ABSPATH . UPLOADS;
     1339        $url = trailingslashit($siteurl) . UPLOADS;
     1340    }
     1341
     1342    if ( get_settings('uploads_use_yearmonth_folders')) {
     1343        // Generate the yearly and monthly dirs
     1344        $time = current_time( 'mysql' );
     1345        $y = substr( $time, 0, 4 );
     1346        $m = substr( $time, 5, 2 );
     1347        $dir = $dir . "/$y/$m";
     1348        $url = $url . "/$y/$m";
     1349    }
     1350
     1351    // Make sure we have an uploads dir
     1352    if ( ! wp_mkdir_p( $dir ) ) {
     1353        $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), $dir);
     1354        return array('error' => $message);
     1355    }
     1356
     1357    $uploads = array('path' => $dir, 'url' => $url, 'error' => false);
     1358    return apply_filters('upload_dir', $uploads);
     1359}
     1360
     1361function wp_upload_bits($name, $type, $bits) {
     1362    if ( empty($name) )
     1363        return array('error' => "Empty filename");
     1364
     1365    $upload = wp_upload_dir();
     1366
     1367    if ( $upload['error'] !== false )
     1368        return $upload;
     1369
     1370    $number = '';
     1371    $filename = $name;
     1372    $path_parts = pathinfo($filename);
     1373    $ext = $path_parts['extension'];
     1374    if ( empty($ext) )
     1375        $ext = '';
     1376    else
     1377        $ext = ".$ext";
     1378    while ( file_exists($upload['path'] . "/$filename") ) {
     1379        if ( '' == "$number$ext" )
     1380            $filename = $filename . ++$number . $ext;
     1381        else
     1382            $filename = str_replace("$number$ext", ++$number . $ext, $filename);
     1383    }
     1384
     1385    $new_file = $upload['path'] . "/$filename";
     1386    if ( ! wp_mkdir_p( dirname($new_file) ) ) {
     1387        $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), dirname($new_file));
     1388        return array('error' => $message);
     1389    }
     1390
     1391    $ifp = @ fopen($new_file, 'wb');
     1392    if ( ! $ifp )
     1393        return array('error' => "Could not write file $new_file.");
     1394
     1395    $success = @ fwrite($ifp, $bits);
     1396    fclose($ifp);
     1397    // Set correct file permissions
     1398    $stat = @ stat(dirname($new_file));
     1399    $perms = $stat['mode'] & 0007777;
     1400    $perms = $perms & 0000666;
     1401    @ chmod($new_file, $perms);
     1402
     1403    // Compute the URL
     1404    $url = $upload['url'] . "/$filename";
     1405
     1406    return array('file' => $new_file, 'url' => $url, 'error' => false);
     1407}
     1408
     1409function do_trackbacks($post_id) {
     1410    global $wpdb;
     1411
     1412    $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $post_id");
     1413    $to_ping = get_to_ping($post_id);
     1414    $pinged  = get_pung($post_id);
     1415    if ( empty($to_ping) ) {
     1416        $wpdb->query("UPDATE $wpdb->posts SET to_ping = '' WHERE ID = '$post_id'");
     1417        return;
     1418    }
     1419
     1420    if (empty($post->post_excerpt))
     1421        $excerpt = apply_filters('the_content', $post->post_content);
     1422    else
     1423        $excerpt = apply_filters('the_excerpt', $post->post_excerpt);
     1424    $excerpt = str_replace(']]>', ']]&gt;', $excerpt);
     1425    $excerpt = strip_tags($excerpt);
     1426    if ( function_exists('mb_strcut') ) // For international trackbacks
     1427        $excerpt = mb_strcut($excerpt, 0, 252, get_settings('blog_charset')) . '...';
     1428    else
     1429        $excerpt = substr($excerpt, 0, 252) . '...';
     1430
     1431    $post_title = apply_filters('the_title', $post->post_title);
     1432    $post_title = strip_tags($post_title);
     1433
     1434    if ($to_ping) : foreach ($to_ping as $tb_ping) :
     1435        $tb_ping = trim($tb_ping);
     1436        if ( !in_array($tb_ping, $pinged) ) {
     1437            trackback($tb_ping, $post_title, $excerpt, $post_id);
     1438            $pinged[] = $tb_ping;
     1439        } else {
     1440            $wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = '$post_id'");
     1441        }
     1442    endforeach; endif;
     1443}
     1444
     1445function do_all_pings() {
     1446    global $wpdb;
     1447
     1448    // Do pingbacks
     1449    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")) {
     1450        $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme';");
     1451        pingback($ping->post_content, $ping->ID);
     1452    }
     1453   
     1454    // Do Enclosures
     1455    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")) {
     1456        $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$enclosure->ID} AND meta_key = '_encloseme';");
     1457        do_enclose($enclosure->post_content, $enclosure->ID);
     1458    }
     1459
     1460    // Do Trackbacks
     1461    $trackbacks = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE CHAR_LENGTH(TRIM(to_ping)) > 7 AND post_status = 'publish'");
     1462    if ( is_array($trackbacks) ) {
     1463        foreach ( $trackbacks as $trackback ) {
     1464            do_trackbacks($trackback->ID);
     1465        }
     1466    }
     1467
     1468    //Do Update Services/Generic Pings
     1469    generic_ping();
     1470}
     1471
     1472function wp_proxy_check($ipnum) {
     1473    if ( get_option('open_proxy_check') && isset($ipnum) ) {
     1474        $rev_ip = implode( '.', array_reverse( explode( '.', $ipnum ) ) );
     1475        $lookup = $rev_ip . '.opm.blitzed.org.';
     1476        if ( $lookup != gethostbyname( $lookup ) )
     1477            return true;
     1478    }
     1479
     1480    return false;
     1481}
     1482
    16031483?>
  • trunk/wp-includes/post-template.php

    r3845 r3851  
    11<?php
    22
    3 function get_the_password_form() {
    4     $output = '<form action="' . get_settings('siteurl') . '/wp-pass.php" method="post">
    5     <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
    6     <p><label>' . __("Password:") . ' <input name="post_password" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . __("Submit") . '" /></p>
    7     </form>
    8     ';
    9     return $output;
    10 }
    11 
     3//
     4// "The Loop" post functions
     5//
    126
    137function the_ID() {
     
    1610}
    1711
     12
    1813function get_the_ID() {
    1914    global $id;
    2015    return $id;
    2116}
     17
    2218
    2319function the_title($before = '', $after = '', $echo = true) {
     
    4339}
    4440
     41function the_guid( $id = 0 ) {
     42    echo get_the_guid($id);
     43}
    4544
    4645function get_the_guid( $id = 0 ) {
     
    4948    return apply_filters('get_the_guid', $post->guid);
    5049}
    51 
    52 
    53 function the_guid( $id = 0 ) {
    54     echo get_the_guid($id);
    55 }
    56 
    5750
    5851function the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
     
    191184
    192185
    193 /*
    194 Post-meta: Custom per-post fields.
    195 */
    196 
    197 
    198 function get_post_custom( $post_id = 0 ) {
    199     global $id, $post_meta_cache, $wpdb;
    200 
    201     if ( ! $post_id )
    202         $post_id = $id;
    203 
    204     if ( isset($post_meta_cache[$post_id]) )
    205         return $post_meta_cache[$post_id];
    206 
    207     if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' ORDER BY post_id, meta_key", ARRAY_A) ) {
    208         // Change from flat structure to hierarchical:
    209         $post_meta_cache = array();
    210         foreach ( $meta_list as $metarow ) {
    211             $mpid = $metarow['post_id'];
    212             $mkey = $metarow['meta_key'];
    213             $mval = $metarow['meta_value'];
    214 
    215             // Force subkeys to be array type:
    216             if ( !isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]) )
    217                 $post_meta_cache[$mpid] = array();
    218 
    219             if ( !isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]) )
    220                 $post_meta_cache[$mpid]["$mkey"] = array();
    221 
    222             // Add a value to the current pid/key:
    223             $post_meta_cache[$mpid][$mkey][] = $mval;
    224         }
    225         return $post_meta_cache[$mpid];
    226     }
    227 }
    228 
    229 
    230 function get_post_custom_keys() {
    231     $custom = get_post_custom();
    232 
    233     if ( ! is_array($custom) )
    234         return;
    235 
    236     if ( $keys = array_keys($custom) )
    237         return $keys;
    238 }
    239 
    240 
    241 function get_post_custom_values( $key = '' ) {
    242     $custom = get_post_custom();
    243 
    244     return $custom[$key];
    245 }
     186//
     187// Post-meta: Custom per-post fields.
     188//
    246189
    247190
     
    275218
    276219
    277 /*
    278 Pages
    279 */
     220//
     221// Pages
     222//
     223
     224function wp_dropdown_pages($args = '') {
     225    if ( is_array($args) )
     226        $r = &$args;
     227    else
     228        parse_str($args, $r);
     229
     230    $defaults = array('depth' => 0, 'child_of' => 0, 'selected' => 0, 'echo' => 1,
     231        'name' => 'page_id');
     232    $r = array_merge($defaults, $r);
     233    extract($r);
     234
     235    $pages = get_pages($r);
     236    $output = '';
     237
     238    if ( ! empty($pages) ) {
     239        $output = "<select name='$name'>\n";
     240        $output .= walk_page_dropdown_tree($pages, $depth, $r);
     241        $output .= "</select>\n";
     242    }
     243
     244    $output = apply_filters('wp_dropdown_pages', $output);
     245
     246    if ( $echo )
     247        echo $output;
     248
     249    return $output;
     250}
     251
     252function wp_list_pages($args = '') {
     253    if ( is_array($args) )
     254        $r = &$args;
     255    else
     256        parse_str($args, $r);
     257
     258    $defaults = array('depth' => 0, 'show_date' => '', 'date_format' => get_settings('date_format'),
     259        'child_of' => 0, 'title_li' => __('Pages'), 'echo' => 1);
     260    $r = array_merge($defaults, $r);
     261
     262    $output = '';
     263
     264    // Query pages.
     265    $pages = get_pages($r);
     266
     267    if ( !empty($pages) ) {
     268        if ( $r['title_li'] )
     269            $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
     270
     271        global $wp_query;
     272        $current_page = $wp_query->get_queried_object_id();
     273        $output .= walk_page_tree($pages, $r['depth'], $current_page, $r['show_date'], $r['date_format']);
     274
     275        if ( $r['title_li'] )
     276            $output .= '</ul></li>';
     277    }
     278
     279    $output = apply_filters('wp_list_pages', $output);
     280
     281    if ( $r['echo'] )
     282        echo $output;
     283    else
     284        return $output;
     285}
     286
     287//
     288// Page helpers
     289//
    280290
    281291function walk_page_tree() {
     
    291301}
    292302
    293 function &get_page_children($page_id, $pages) {
    294     global $page_cache;
    295 
    296     if ( empty($pages) )
    297         $pages = &$page_cache;
    298 
    299     $page_list = array();
    300     foreach ( $pages as $page ) {
    301         if ( $page->post_parent == $page_id ) {
    302             $page_list[] = $page;
    303             if ( $children = get_page_children($page->ID, $pages) )
    304                 $page_list = array_merge($page_list, $children);
    305         }
    306     }
    307     return $page_list;
    308 }
    309 
    310 
    311 function &get_pages($args = '') {
    312     global $wpdb;
    313 
    314     if ( is_array($args) )
    315         $r = &$args;
    316     else
    317         parse_str($args, $r);
    318 
    319     $defaults = array('child_of' => 0, 'sort_order' => 'ASC', 'sort_column' => 'post_title',
    320                 'hierarchical' => 1, 'exclude' => '', 'include' => '', 'meta_key' => '', 'meta_value' => '');
    321     $r = array_merge($defaults, $r);
    322     extract($r);
    323 
    324     $inclusions = '';
    325     if ( !empty($include) ) {
    326         $child_of = 0; //ignore child_of, exclude, meta_key, and meta_value params if using include
    327         $exclude = ''; 
    328         $meta_key = '';
    329         $meta_value = '';
    330         $incpages = preg_split('/[\s,]+/',$include);
    331         if ( count($incpages) ) {
    332             foreach ( $incpages as $incpage ) {
    333                 if (empty($inclusions))
    334                     $inclusions = ' AND ( ID = ' . intval($incpage) . ' ';
    335                 else
    336                     $inclusions .= ' OR ID = ' . intval($incpage) . ' ';
    337             }
    338         }
    339     }
    340     if (!empty($inclusions))
    341         $inclusions .= ')';
    342 
    343     $exclusions = '';
    344     if ( !empty($exclude) ) {
    345         $expages = preg_split('/[\s,]+/',$exclude);
    346         if ( count($expages) ) {
    347             foreach ( $expages as $expage ) {
    348                 if (empty($exclusions))
    349                     $exclusions = ' AND ( ID <> ' . intval($expage) . ' ';
    350                 else
    351                     $exclusions .= ' AND ID <> ' . intval($expage) . ' ';
    352             }
    353         }
    354     }
    355     if (!empty($exclusions))
    356         $exclusions .= ')';
    357 
    358     $query = "SELECT * FROM $wpdb->posts " ;
    359     $query .= ( empty( $meta_key ) ? "" : ", $wpdb->postmeta " ) ;
    360     $query .= " WHERE (post_type = 'page' AND post_status = 'publish') $exclusions $inclusions " ;
    361     $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' )" ) ;
    362     $query .= " ORDER BY " . $sort_column . " " . $sort_order ;
    363 
    364     $pages = $wpdb->get_results($query);
    365     $pages = apply_filters('get_pages', $pages, $r);
    366 
    367     if ( empty($pages) )
    368         return array();
    369 
    370     // Update cache.
    371     update_page_cache($pages);
    372 
    373     if ( $child_of || $hierarchical )
    374         $pages = & get_page_children($child_of, $pages);
    375 
    376     return $pages;
    377 }
    378 
    379 function wp_dropdown_pages($args = '') {
    380     if ( is_array($args) )
    381         $r = &$args;
    382     else
    383         parse_str($args, $r);
    384 
    385     $defaults = array('depth' => 0, 'child_of' => 0, 'selected' => 0, 'echo' => 1,
    386         'name' => 'page_id');
    387     $r = array_merge($defaults, $r);
    388     extract($r);
    389 
    390     $pages = get_pages($r);
    391     $output = '';
    392 
    393     if ( ! empty($pages) ) {
    394         $output = "<select name='$name'>\n";
    395         $output .= walk_page_dropdown_tree($pages, $depth, $r);
    396         $output .= "</select>\n";
    397     }
    398 
    399     $output = apply_filters('wp_dropdown_pages', $output);
    400 
    401     if ( $echo )
    402         echo $output;
    403 
    404     return $output;
    405 }
    406 
    407 function wp_list_pages($args = '') {
    408     if ( is_array($args) )
    409         $r = &$args;
    410     else
    411         parse_str($args, $r);
    412 
    413     $defaults = array('depth' => 0, 'show_date' => '', 'date_format' => get_settings('date_format'),
    414         'child_of' => 0, 'title_li' => __('Pages'), 'echo' => 1);
    415     $r = array_merge($defaults, $r);
    416 
    417     $output = '';
    418 
    419     // Query pages.
    420     $pages = get_pages($r);
    421 
    422     if ( !empty($pages) ) {
    423         if ( $r['title_li'] )
    424             $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
    425 
    426         global $wp_query;
    427         $current_page = $wp_query->get_queried_object_id();
    428         $output .= walk_page_tree($pages, $r['depth'], $current_page, $r['show_date'], $r['date_format']);
    429 
    430         if ( $r['title_li'] )
    431             $output .= '</ul></li>';
    432     }
    433 
    434     $output = apply_filters('wp_list_pages', $output);
    435 
    436     if ( $r['echo'] )
    437         echo $output;
    438     else
    439         return $output;
    440 }
     303//
     304// Attachments
     305//
    441306
    442307function the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
     
    562427}
    563428
     429//
     430// Misc
     431//
     432
     433function get_the_password_form() {
     434    $output = '<form action="' . get_settings('siteurl') . '/wp-pass.php" method="post">
     435    <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
     436    <p><label>' . __("Password:") . ' <input name="post_password" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . __("Submit") . '" /></p>
     437    </form>
     438    ';
     439    return $output;
     440}
     441
    564442?>
  • trunk/wp-includes/post.php

    r3849 r3851  
    11<?php
    22
    3 /**** DB Functions ****/
    4 
    5 /*
    6  * generic function for inserting data into the posts table.
    7  */
     3//
     4// Post functions
     5//
     6
     7function get_attached_file($attachment_id) {
     8    return get_post_meta($attachment_id, '_wp_attached_file', true);
     9}
     10
     11function &get_children($post = 0, $output = OBJECT) {
     12    global $post_cache, $wpdb;
     13
     14    if ( empty($post) ) {
     15        if ( isset($GLOBALS['post']) )
     16            $post_parent = & $GLOBALS['post']->post_parent;
     17        else
     18            return false;
     19    } elseif ( is_object($post) ) {
     20        $post_parent = $post->post_parent;
     21    } else {
     22        $post_parent = $post;
     23    }
     24
     25    $post_parent = (int) $post_parent;
     26
     27    $query = "SELECT * FROM $wpdb->posts WHERE post_parent = $post_parent";
     28
     29    $children = $wpdb->get_results($query);
     30
     31    if ( $children ) {
     32        foreach ( $children as $key => $child ) {
     33            $post_cache[$child->ID] =& $children[$key];
     34            $kids[$child->ID] =& $children[$key];
     35        }
     36    } else {
     37        return false;
     38    }
     39
     40    if ( $output == OBJECT ) {
     41        return $kids;
     42    } elseif ( $output == ARRAY_A ) {
     43        foreach ( $kids as $kid )
     44            $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
     45        return $weeuns;
     46    } elseif ( $output == ARRAY_N ) {
     47        foreach ( $kids as $kid )
     48            $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
     49        return $babes;
     50    } else {
     51        return $kids;
     52    }
     53}
     54
     55// get extended entry info (<!--more-->)
     56function get_extended($post) {
     57    list($main,$extended) = explode('<!--more-->', $post, 2);
     58
     59    // Strip leading and trailing whitespace
     60    $main = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$main);
     61    $extended = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$extended);
     62
     63    return array('main' => $main, 'extended' => $extended);
     64}
     65
     66// Retrieves post data given a post ID or post object.
     67// Handles post caching.
     68function &get_post(&$post, $output = OBJECT) {
     69    global $post_cache, $wpdb;
     70
     71    if ( empty($post) ) {
     72        if ( isset($GLOBALS['post']) )
     73            $_post = & $GLOBALS['post'];
     74        else
     75            $_post = null;
     76    } elseif ( is_object($post) ) {
     77        if ( 'page' == $post->post_type )
     78            return get_page($post, $output);
     79        if ( !isset($post_cache[$post->ID]) )
     80            $post_cache[$post->ID] = &$post;
     81        $_post = & $post_cache[$post->ID];
     82    } else {
     83        if ( $_post = wp_cache_get($post, 'pages') )
     84            return get_page($_post, $output);
     85        elseif ( isset($post_cache[$post]) )
     86            $_post = & $post_cache[$post];
     87        else {
     88            $query = "SELECT * FROM $wpdb->posts WHERE ID = '$post' LIMIT 1";
     89            $_post = & $wpdb->get_row($query);
     90            if ( 'page' == $_post->post_type )
     91                return get_page($_post, $output);
     92            $post_cache[$post] = & $_post;
     93        }
     94    }
     95
     96    if ( defined(WP_IMPORTING) )
     97        unset($post_cache);
     98
     99    if ( $output == OBJECT ) {
     100        return $_post;
     101    } elseif ( $output == ARRAY_A ) {
     102        return get_object_vars($_post);
     103    } elseif ( $output == ARRAY_N ) {
     104        return array_values(get_object_vars($_post));
     105    } else {
     106        return $_post;
     107    }
     108}
     109
     110// Takes a post ID, returns its mime type.
     111function get_post_mime_type($ID = '') {
     112    $post = & get_post($ID);
     113
     114    if ( is_object($post) )
     115        return $post->post_mime_type;
     116
     117    return false;
     118}
     119
     120function get_post_status($ID = '') {
     121    $post = get_post($ID);
     122
     123    if ( is_object($post) ) {
     124        if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) )
     125            return get_post_status($post->post_parent);
     126        else
     127            return $post->post_status;
     128    }
     129
     130    return false;
     131}
     132
     133function get_post_type($post = false) {
     134    global $wpdb, $posts;
     135
     136    if ( false === $post )
     137        $post = $posts[0];
     138    elseif ( (int) $post )
     139        $post = get_post($post, OBJECT);
     140
     141    if ( is_object($post) )
     142        return $post->post_type;
     143
     144    return false;
     145}
     146
     147
     148//
     149// Post meta functions
     150//
     151
     152function add_post_meta($post_id, $key, $value, $unique = false) {
     153    global $wpdb, $post_meta_cache;
     154
     155    if ( $unique ) {
     156        if ( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
     157= '$key' AND post_id = '$post_id'") ) {
     158            return false;
     159        }
     160    }
     161
     162    $original = $value;
     163    if ( is_array($value) || is_object($value) )
     164        $value = $wpdb->escape(serialize($value));
     165
     166    $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_id','$key','$value')");
     167
     168    $post_meta_cache['$post_id'][$key][] = $original;
     169
     170    return true;
     171}
     172
     173function delete_post_meta($post_id, $key, $value = '') {
     174    global $wpdb, $post_meta_cache;
     175
     176    if ( empty($value) ) {
     177        $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
     178post_id = '$post_id' AND meta_key = '$key'");
     179    } else {
     180        $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
     181post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
     182    }
     183
     184    if ( !$meta_id )
     185        return false;
     186
     187    if ( empty($value) ) {
     188        $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
     189AND meta_key = '$key'");
     190        unset($post_meta_cache['$post_id'][$key]);
     191    } else {
     192        $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
     193AND meta_key = '$key' AND meta_value = '$value'");
     194        $cache_key = $post_meta_cache['$post_id'][$key];
     195        if ($cache_key) foreach ( $cache_key as $index => $data )
     196            if ( $data == $value )
     197                unset($post_meta_cache['$post_id'][$key][$index]);
     198    }
     199
     200    unset($post_meta_cache['$post_id'][$key]);
     201
     202    return true;
     203}
     204
     205function get_post_meta($post_id, $key, $single = false) {
     206    global $wpdb, $post_meta_cache;
     207
     208    if ( isset($post_meta_cache[$post_id][$key]) ) {
     209        if ( $single ) {
     210            return maybe_unserialize( $post_meta_cache[$post_id][$key][0] );
     211        } else {
     212            return maybe_unserialize( $post_meta_cache[$post_id][$key] );
     213        }
     214    }
     215
     216    $metalist = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'", ARRAY_N);
     217
     218    $values = array();
     219    if ( $metalist ) {
     220        foreach ($metalist as $metarow) {
     221            $values[] = $metarow[0];
     222        }
     223    }
     224
     225    if ( $single ) {
     226        if ( count($values) ) {
     227            $return = maybe_unserialize( $values[0] );
     228        } else {
     229            return '';
     230        }
     231    } else {
     232        $return = $values;
     233    }
     234
     235    return maybe_unserialize($return);
     236}
     237
     238function update_post_meta($post_id, $key, $value, $prev_value = '') {
     239    global $wpdb, $post_meta_cache;
     240
     241    $original_value = $value;
     242    if ( is_array($value) || is_object($value) )
     243        $value = $wpdb->escape(serialize($value));
     244
     245    $original_prev = $prev_value;
     246    if ( is_array($prev_value) || is_object($prev_value) )
     247        $prev_value = $wpdb->escape(serialize($prev_value));
     248
     249    if (! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
     250= '$key' AND post_id = '$post_id'") ) {
     251        return false;
     252    }
     253
     254    if ( empty($prev_value) ) {
     255        $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
     256meta_key = '$key' AND post_id = '$post_id'");
     257        $cache_key = $post_meta_cache['$post_id'][$key];
     258        if ( !empty($cache_key) )
     259            foreach ($cache_key as $index => $data)
     260                $post_meta_cache['$post_id'][$key][$index] = $original_value;
     261    } else {
     262        $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
     263meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'");
     264        $cache_key = $post_meta_cache['$post_id'][$key];
     265        if ( !empty($cache_key) )
     266            foreach ($cache_key as $index => $data)
     267                if ( $data == $original_prev )
     268                    $post_meta_cache['$post_id'][$key][$index] = $original_value;
     269    }
     270
     271    return true;
     272}
     273
     274
     275function get_post_custom( $post_id = 0 ) {
     276    global $id, $post_meta_cache, $wpdb;
     277
     278    if ( ! $post_id )
     279        $post_id = $id;
     280
     281    if ( isset($post_meta_cache[$post_id]) )
     282        return $post_meta_cache[$post_id];
     283
     284    if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' ORDER BY post_id, meta_key", ARRAY_A) ) {
     285        // Change from flat structure to hierarchical:
     286        $post_meta_cache = array();
     287        foreach ( $meta_list as $metarow ) {
     288            $mpid = $metarow['post_id'];
     289            $mkey = $metarow['meta_key'];
     290            $mval = $metarow['meta_value'];
     291
     292            // Force subkeys to be array type:
     293            if ( !isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]) )
     294                $post_meta_cache[$mpid] = array();
     295
     296            if ( !isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]) )
     297                $post_meta_cache[$mpid]["$mkey"] = array();
     298
     299            // Add a value to the current pid/key:
     300            $post_meta_cache[$mpid][$mkey][] = $mval;
     301        }
     302        return $post_meta_cache[$mpid];
     303    }
     304}
     305
     306function get_post_custom_keys( $post_id = 0 ) {
     307    $custom = get_post_custom( $post_id );
     308
     309    if ( ! is_array($custom) )
     310        return;
     311
     312    if ( $keys = array_keys($custom) )
     313        return $keys;
     314}
     315
     316
     317function get_post_custom_values( $key = '', $post_id = 0 ) {
     318    $custom = get_post_custom($post_id);
     319
     320    return $custom[$key];
     321}
     322
     323function wp_delete_post($postid = 0) {
     324    global $wpdb, $wp_rewrite;
     325    $postid = (int) $postid;
     326
     327    if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $postid") )
     328        return $post;
     329
     330    if ( 'attachment' == $post->post_type )
     331        return wp_delete_attachment($postid);
     332
     333    do_action('delete_post', $postid);
     334
     335    if ( 'publish' == $post->post_status && 'post' == $post->post_type ) {
     336        $categories = wp_get_post_categories($post->ID);
     337        if( is_array( $categories ) ) {
     338            foreach ( $categories as $cat_id ) {
     339                $wpdb->query("UPDATE $wpdb->categories SET category_count = category_count - 1 WHERE cat_ID = '$cat_id'");
     340                wp_cache_delete($cat_id, 'category');
     341            }
     342        }
     343    }
     344
     345    if ( 'page' == $post->post_type )
     346        $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_type = 'page'");
     347
     348    $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = $postid");
     349
     350    $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid");
     351
     352    $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = $postid");
     353
     354    $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = $postid");
     355
     356    if ( 'page' == $post->type ) {
     357        wp_cache_delete('all_page_ids', 'pages');
     358        $wp_rewrite->flush_rules();
     359    }
     360
     361    return $post;
     362}
     363
     364function wp_get_post_categories($post_ID = 0) {
     365    global $wpdb;
     366
     367    $post_ID = (int) $post_ID;
     368
     369    $sql = "SELECT category_id
     370        FROM $wpdb->post2cat
     371        WHERE post_id = '$post_ID'
     372        ORDER BY category_id";
     373
     374    $result = $wpdb->get_col($sql);
     375
     376    if ( !$result )
     377        $result = array();
     378
     379    return array_unique($result);
     380}
     381
     382function wp_get_recent_posts($num = 10) {
     383    global $wpdb;
     384
     385    // Set the limit clause, if we got a limit
     386    if ($num) {
     387        $limit = "LIMIT $num";
     388    }
     389
     390    $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' ORDER BY post_date DESC $limit";
     391    $result = $wpdb->get_results($sql,ARRAY_A);
     392
     393    return $result?$result:array();
     394}
     395
     396function wp_get_single_post($postid = 0, $mode = OBJECT) {
     397    global $wpdb;
     398
     399    $post = get_post($postid, $mode);
     400
     401    // Set categories
     402    if($mode == OBJECT) {
     403        $post->post_category = wp_get_post_categories($postid);
     404    }
     405    else {
     406        $post['post_category'] = wp_get_post_categories($postid);
     407    }
     408
     409    return $post;
     410}
     411
    8412function wp_insert_post($postarr = array()) {
    9413    global $wpdb, $wp_rewrite, $allowedtags, $user_ID;
     
    223627    return $post_ID;
    224628}
     629
     630function wp_update_post($postarr = array()) {
     631    global $wpdb;
     632
     633    if ( is_object($postarr) )
     634        $postarr = get_object_vars($postarr);
     635
     636    // First, get all of the original fields
     637    $post = wp_get_single_post($postarr['ID'], ARRAY_A);
     638
     639    // Escape data pulled from DB.
     640    $post = add_magic_quotes($post);
     641
     642    // Passed post category list overwrites existing category list if not empty.
     643    if ( isset($postarr['post_category']) && is_array($postarr['post_category'])
     644             && 0 != count($postarr['post_category']) )
     645        $post_cats = $postarr['post_category'];
     646    else
     647        $post_cats = $post['post_category'];
     648
     649    // Drafts shouldn't be assigned a date unless explicitly done so by the user
     650    if ( 'draft' == $post['post_status'] && empty($postarr['edit_date']) && empty($postarr['post_date']) &&
     651         ('0000-00-00 00:00:00' == $post['post_date']) )
     652        $clear_date = true;
     653    else
     654        $clear_date = false;
     655
     656    // Merge old and new fields with new fields overwriting old ones.
     657    $postarr = array_merge($post, $postarr);
     658    $postarr['post_category'] = $post_cats;
     659    if ( $clear_date ) {
     660        $postarr['post_date'] = '';
     661        $postarr['post_date_gmt'] = '';
     662    }
     663
     664    if ($postarr['post_type'] == 'attachment')
     665        return wp_insert_attachment($postarr);
     666
     667    return wp_insert_post($postarr);
     668}
     669
     670function wp_publish_post($post_id) {
     671    $post = get_post($post_id);
     672
     673    if ( empty($post) )
     674        return;
     675
     676    if ( 'publish' == $post->post_status )
     677        return;
     678
     679    return wp_update_post(array('post_status' => 'publish', 'ID' => $post_id));
     680}
     681
     682function wp_set_post_categories($post_ID = 0, $post_categories = array()) {
     683    global $wpdb;
     684    // If $post_categories isn't already an array, make it one:
     685    if (!is_array($post_categories) || 0 == count($post_categories))
     686        $post_categories = array(get_option('default_category'));
     687
     688    $post_categories = array_unique($post_categories);
     689
     690    // First the old categories
     691    $old_categories = $wpdb->get_col("
     692        SELECT category_id
     693        FROM $wpdb->post2cat
     694        WHERE post_id = $post_ID");
     695
     696    if (!$old_categories) {
     697        $old_categories = array();
     698    } else {
     699        $old_categories = array_unique($old_categories);
     700    }
     701
     702    // Delete any?
     703    $delete_cats = array_diff($old_categories,$post_categories);
     704
     705    if ($delete_cats) {
     706        foreach ($delete_cats as $del) {
     707            $wpdb->query("
     708                DELETE FROM $wpdb->post2cat
     709                WHERE category_id = $del
     710                    AND post_id = $post_ID
     711                ");
     712        }
     713    }
     714
     715    // Add any?
     716    $add_cats = array_diff($post_categories, $old_categories);
     717
     718    if ($add_cats) {
     719        foreach ($add_cats as $new_cat) {
     720            $wpdb->query("
     721                INSERT INTO $wpdb->post2cat (post_id, category_id)
     722                VALUES ($post_ID, $new_cat)");
     723        }
     724    }
     725
     726    // Update category counts.
     727    $all_affected_cats = array_unique(array_merge($post_categories, $old_categories));
     728    foreach ( $all_affected_cats as $cat_id ) {
     729        $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'");
     730        $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'");
     731        wp_cache_delete($cat_id, 'category');
     732    }
     733}   // wp_set_post_categories()
     734
     735//
     736// Trackback and ping functions
     737//
     738
     739function add_ping($post_id, $uri) { // Add a URI to those already pung
     740    global $wpdb;
     741    $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
     742    $pung = trim($pung);
     743    $pung = preg_split('/\s/', $pung);
     744    $pung[] = $uri;
     745    $new = implode("\n", $pung);
     746    $new = apply_filters('add_ping', $new);
     747    return $wpdb->query("UPDATE $wpdb->posts SET pinged = '$new' WHERE ID = $post_id");
     748}
     749
     750function get_enclosed($post_id) { // Get enclosures already enclosed for a post
     751    global $wpdb;
     752    $custom_fields = get_post_custom( $post_id );
     753    $pung = array();
     754    if ( !is_array( $custom_fields ) )
     755        return $pung;
     756
     757    foreach ( $custom_fields as $key => $val ) {
     758        if ( 'enclosure' != $key || !is_array( $val ) )
     759            continue;
     760        foreach( $val as $enc ) {
     761            $enclosure = split( "\n", $enc );
     762            $pung[] = trim( $enclosure[ 0 ] );
     763        }
     764    }
     765    $pung = apply_filters('get_enclosed', $pung);
     766    return $pung;
     767}
     768
     769function get_pung($post_id) { // Get URIs already pung for a post
     770    global $wpdb;
     771    $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
     772    $pung = trim($pung);
     773    $pung = preg_split('/\s/', $pung);
     774    $pung = apply_filters('get_pung', $pung);
     775    return $pung;
     776}
     777
     778function get_to_ping($post_id) { // Get any URIs in the todo list
     779    global $wpdb;
     780    $to_ping = $wpdb->get_var("SELECT to_ping FROM $wpdb->posts WHERE ID = $post_id");
     781    $to_ping = trim($to_ping);
     782    $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY);
     783    $to_ping = apply_filters('get_to_ping',  $to_ping);
     784    return $to_ping;
     785}
     786
     787// do trackbacks for a list of urls
     788// accepts a comma-separated list of trackback urls and a post id
     789function trackback_url_list($tb_list, $post_id) {
     790    if (!empty($tb_list)) {
     791        // get post data
     792        $postdata = wp_get_single_post($post_id, ARRAY_A);
     793
     794        // import postdata as variables
     795        extract($postdata);
     796
     797        // form an excerpt
     798        $excerpt = strip_tags($post_excerpt?$post_excerpt:$post_content);
     799
     800        if (strlen($excerpt) > 255) {
     801            $excerpt = substr($excerpt,0,252) . '...';
     802        }
     803
     804        $trackback_urls = explode(',', $tb_list);
     805        foreach($trackback_urls as $tb_url) {
     806            $tb_url = trim($tb_url);
     807            trackback($tb_url, stripslashes($post_title), $excerpt, $post_id);
     808        }
     809    }
     810}
     811
     812//
     813// Page functions
     814//
     815
     816function get_all_page_ids() {
     817    global $wpdb;
     818
     819    if ( ! $page_ids = wp_cache_get('all_page_ids', 'pages') ) {
     820        $page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'page'");
     821        wp_cache_add('all_page_ids', $page_ids, 'pages');
     822    }
     823
     824    return $page_ids;
     825}
     826
     827
     828// Retrieves page data given a page ID or page object.
     829// Handles page caching.
     830function &get_page(&$page, $output = OBJECT) {
     831    global $wpdb;
     832
     833    if ( empty($page) ) {
     834        if ( isset($GLOBALS['page']) ) {
     835            $_page = & $GLOBALS['page'];
     836            wp_cache_add($_page->ID, $_page, 'pages');
     837        } else {
     838            $_page = null;
     839        }
     840    } elseif ( is_object($page) ) {
     841        if ( 'post' == $page->post_type )
     842            return get_post($page, $output);
     843        wp_cache_add($page->ID, $page, 'pages');
     844        $_page = $page;
     845    } else {
     846        if ( isset($GLOBALS['page']) && ($page == $GLOBALS['page']->ID) ) {
     847            $_page = & $GLOBALS['page'];
     848            wp_cache_add($_page->ID, $_page, 'pages');
     849        } elseif ( $_page = $GLOBALS['post_cache'][$page] ) {
     850            return get_post($page, $output);
     851        } elseif ( $_page = wp_cache_get($page, 'pages') ) {
     852            // Got it.
     853        } else {
     854            $query = "SELECT * FROM $wpdb->posts WHERE ID= '$page' LIMIT 1";
     855            $_page = & $wpdb->get_row($query);
     856            if ( 'post' == $_page->post_type )
     857                return get_post($_page, $output);
     858            wp_cache_add($_page->ID, $_page, 'pages');
     859        }
     860    }
     861
     862    if ( $output == OBJECT ) {
     863        return $_page;
     864    } elseif ( $output == ARRAY_A ) {
     865        return get_object_vars($_page);
     866    } elseif ( $output == ARRAY_N ) {
     867        return array_values(get_object_vars($_page));
     868    } else {
     869        return $_page;
     870    }
     871}
     872
     873function get_page_by_path($page_path, $output = OBJECT) {
     874    global $wpdb;
     875    $page_path = rawurlencode(urldecode($page_path));
     876    $page_path = str_replace('%2F', '/', $page_path);
     877    $page_path = str_replace('%20', ' ', $page_path);
     878    $page_paths = '/' . trim($page_path, '/');
     879    $leaf_path  = sanitize_title(basename($page_paths));
     880    $page_paths = explode('/', $page_paths);
     881    foreach($page_paths as $pathdir)
     882        $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
     883
     884    $pages = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = '$leaf_path' AND post_type='page'");
     885
     886    if ( empty($pages) )
     887        return NULL;
     888
     889    foreach ($pages as $page) {
     890        $path = '/' . $leaf_path;
     891        $curpage = $page;
     892        while ($curpage->post_parent != 0) {
     893            $curpage = $wpdb->get_row("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = '$curpage->post_parent' and post_type='page'");
     894            $path = '/' . $curpage->post_name . $path;
     895        }
     896
     897        if ( $path == $full_path )
     898            return get_page($page->ID, $output);
     899    }
     900
     901    return NULL;
     902}
     903
     904function &get_page_children($page_id, $pages) {
     905    global $page_cache;
     906
     907    if ( empty($pages) )
     908        $pages = &$page_cache;
     909
     910    $page_list = array();
     911    foreach ( $pages as $page ) {
     912        if ( $page->post_parent == $page_id ) {
     913            $page_list[] = $page;
     914            if ( $children = get_page_children($page->ID, $pages) )
     915                $page_list = array_merge($page_list, $children);
     916        }
     917    }
     918    return $page_list;
     919}
     920
     921//fetches the pages returned as a FLAT list, but arranged in order of their hierarchy, i.e., child parents
     922//immediately follow their parents
     923function get_page_hierarchy($posts, $parent = 0) {
     924    $result = array ( );
     925    if ($posts) { foreach ($posts as $post) {
     926        if ($post->post_parent == $parent) {
     927            $result[$post->ID] = $post->post_name;
     928            $children = get_page_hierarchy($posts, $post->ID);
     929            $result += $children; //append $children to $result
     930        }
     931    } }
     932    return $result;
     933}
     934
     935function &get_pages($args = '') {
     936    global $wpdb;
     937
     938    if ( is_array($args) )
     939        $r = &$args;
     940    else
     941        parse_str($args, $r);
     942
     943    $defaults = array('child_of' => 0, 'sort_order' => 'ASC', 'sort_column' => 'post_title',
     944                'hierarchical' => 1, 'exclude' => '', 'include' => '', 'meta_key' => '', 'meta_value' => '');
     945    $r = array_merge($defaults, $r);
     946    extract($r);
     947
     948    $inclusions = '';
     949    if ( !empty($include) ) {
     950        $child_of = 0; //ignore child_of, exclude, meta_key, and meta_value params if using include
     951        $exclude = ''; 
     952        $meta_key = '';
     953        $meta_value = '';
     954        $incpages = preg_split('/[\s,]+/',$include);
     955        if ( count($incpages) ) {
     956            foreach ( $incpages as $incpage ) {
     957                if (empty($inclusions))
     958                    $inclusions = ' AND ( ID = ' . intval($incpage) . ' ';
     959                else
     960                    $inclusions .= ' OR ID = ' . intval($incpage) . ' ';
     961            }
     962        }
     963    }
     964    if (!empty($inclusions))
     965        $inclusions .= ')';
     966
     967    $exclusions = '';
     968    if ( !empty($exclude) ) {
     969        $expages = preg_split('/[\s,]+/',$exclude);
     970        if ( count($expages) ) {
     971            foreach ( $expages as $expage ) {
     972                if (empty($exclusions))
     973                    $exclusions = ' AND ( ID <> ' . intval($expage) . ' ';
     974                else
     975                    $exclusions .= ' AND ID <> ' . intval($expage) . ' ';
     976            }
     977        }
     978    }
     979    if (!empty($exclusions))
     980        $exclusions .= ')';
     981
     982    $query = "SELECT * FROM $wpdb->posts " ;
     983    $query .= ( empty( $meta_key ) ? "" : ", $wpdb->postmeta " ) ;
     984    $query .= " WHERE (post_type = 'page' AND post_status = 'publish') $exclusions $inclusions " ;
     985    $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' )" ) ;
     986    $query .= " ORDER BY " . $sort_column . " " . $sort_order ;
     987
     988    $pages = $wpdb->get_results($query);
     989    $pages = apply_filters('get_pages', $pages, $r);
     990
     991    if ( empty($pages) )
     992        return array();
     993
     994    // Update cache.
     995    update_page_cache($pages);
     996
     997    if ( $child_of || $hierarchical )
     998        $pages = & get_page_children($child_of, $pages);
     999
     1000    return $pages;
     1001}
     1002
     1003function generate_page_uri_index() {
     1004    global $wpdb;
     1005
     1006    //get pages in order of hierarchy, i.e. children after parents
     1007    $posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page'"));
     1008    //now reverse it, because we need parents after children for rewrite rules to work properly
     1009    $posts = array_reverse($posts, true);
     1010
     1011    $page_uris = array();
     1012    $page_attachment_uris = array();
     1013
     1014    if ($posts) {
     1015
     1016        foreach ($posts as $id => $post) {
     1017
     1018            // URI => page name
     1019            $uri = get_page_uri($id);
     1020            $attachments = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = '$id'");
     1021            if ( $attachments ) {
     1022                foreach ( $attachments as $attachment ) {
     1023                    $attach_uri = get_page_uri($attachment->ID);
     1024                    $page_attachment_uris[$attach_uri] = $attachment->ID;
     1025                }
     1026            }
     1027
     1028            $page_uris[$uri] = $id;
     1029        }
     1030
     1031        update_option('page_uris', $page_uris);
     1032
     1033        if ( $page_attachment_uris )
     1034            update_option('page_attachment_uris', $page_attachment_uris);
     1035    }
     1036}
     1037
     1038//
     1039// Attachment functions
     1040//
    2251041
    2261042function wp_insert_attachment($object, $file = false, $post_parent = 0) {
     
    4051221}
    4061222
    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 
    10891223?>
  • trunk/wp-includes/template-functions-author.php

    r3848 r3851  
    156156}
    157157
     158// Get author's preferred display name
     159function get_author_name( $auth_id ) {
     160    $authordata = get_userdata( $auth_id );
     161
     162    return $authordata->display_name;
     163}
     164
    158165function wp_list_authors($args = '') {
    159166    if ( is_array($args) )
  • trunk/wp-includes/template-functions-general.php

    r3740 r3851  
    695695}
    696696
     697function wp_head() {
     698    do_action('wp_head');
     699}
     700
     701function wp_footer() {
     702    do_action('wp_footer');
     703}
     704
    697705function rsd_link() {
    698706    echo '<link rel="EditURI" type="application/rsd+xml" title="RSD" href="' . get_bloginfo('wpurl') . "/xmlrpc.php?rsd\" />\n";
     
    704712        echo '<meta name="robots" content="noindex,nofollow" />' . "\n";
    705713}
     714
     715/**
     716 * Places a textarea according to the current user's preferences, filled with $content.
     717 * Also places a script block that enables tabbing between Title and Content.
     718 *
     719 * @param string Editor contents
     720 * @param string (optional) Previous form field's ID (for tabbing support)
     721 */
     722function the_editor($content, $id = 'content', $prev_id = 'title') {
     723    $rows = get_settings('default_post_edit_rows');
     724    if (($rows < 3) || ($rows > 100))
     725        $rows = 12;
     726
     727    $rows = "rows='$rows'";
     728
     729    the_quicktags();
     730
     731    if ( user_can_richedit() )
     732        add_filter('the_editor_content', 'wp_richedit_pre');
     733
     734    $the_editor = apply_filters('the_editor', "<div><textarea class='mceEditor' $rows cols='40' name='$id' tabindex='2' id='$id'>%s</textarea></div>\n");
     735    $the_editor_content = apply_filters('the_editor_content', $content);
     736
     737    printf($the_editor, $the_editor_content);
     738
     739    ?>
     740    <script type="text/javascript">
     741    //<!--
     742    edCanvas = document.getElementById('<?php echo $id; ?>');
     743    <?php if ( user_can_richedit() ) : ?>
     744    // This code is meant to allow tabbing from Title to Post (TinyMCE).
     745    if ( tinyMCE.isMSIE )
     746        document.getElementById('<?php echo $prev_id; ?>').onkeydown = function (e)
     747            {
     748                e = e ? e : window.event;
     749                if (e.keyCode == 9 && !e.shiftKey && !e.controlKey && !e.altKey) {
     750                    var i = tinyMCE.selectedInstance;
     751                    if(typeof i ==  'undefined')
     752                        return true;
     753                                    tinyMCE.execCommand("mceStartTyping");
     754                    this.blur();
     755                    i.contentWindow.focus();
     756                    e.returnValue = false;
     757                    return false;
     758                }
     759            }
     760    else
     761        document.getElementById('<?php echo $prev_id; ?>').onkeypress = function (e)
     762            {
     763                e = e ? e : window.event;
     764                if (e.keyCode == 9 && !e.shiftKey && !e.controlKey && !e.altKey) {
     765                    var i = tinyMCE.selectedInstance;
     766                    if(typeof i ==  'undefined')
     767                        return true;
     768                                    tinyMCE.execCommand("mceStartTyping");
     769                    this.blur();
     770                    i.contentWindow.focus();
     771                    e.returnValue = false;
     772                    return false;
     773                }
     774            }
     775    <?php endif; ?>
     776    //-->
     777    </script>
     778    <?php
     779}
     780
    706781?>
  • trunk/wp-includes/template-functions-links.php

    r3708 r3851  
    8585}
    8686
     87// get permalink from post ID
     88function post_permalink($post_id = 0, $mode = '') { // $mode legacy
     89    return get_permalink($post_id);
     90}
     91
    8792function get_page_link($id = false) {
    8893    global $post, $wp_rewrite;
  • trunk/wp-settings.php

    r3845 r3851  
    125125
    126126require (ABSPATH . WPINC . '/functions-formatting.php');
    127 require (ABSPATH . WPINC . '/functions-post.php');
    128127require (ABSPATH . WPINC . '/capabilities.php');
    129128require (ABSPATH . WPINC . '/classes.php');
     
    133132require (ABSPATH . WPINC . '/template-functions-links.php');
    134133require (ABSPATH . WPINC . '/template-functions-author.php');
    135 require (ABSPATH . WPINC . '/template-functions-post.php');
     134require (ABSPATH . WPINC . '/post.php');
     135require (ABSPATH . WPINC . '/post-template.php');
    136136require (ABSPATH . WPINC . '/category.php');
    137137require (ABSPATH . WPINC . '/category-template.php');
Note: See TracChangeset for help on using the changeset viewer.