Make WordPress Core


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

Reworg post/page/attachment functions. #2525

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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(']]>', ']]>', $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?>
Note: See TracChangeset for help on using the changeset viewer.