Changeset 3851

Show
Ignore:
Timestamp:
06/07/06 23:17:59 (3 years ago)
Author:
ryan
Message:

Reworg post/page/attachment functions. #2525

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • 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

    r3751 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