Changeset 2478 for trunk/wp-includes/functions.php
- Timestamp:
- 03/27/2005 08:45:01 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/functions.php
r2477 r2478 539 539 } 540 540 541 // Retrieves post data given a post ID or post object. 542 // Handles post caching. 543 function &get_post(&$post, $output = OBJECT) { 544 global $post_cache, $wpdb; 545 546 if ( empty($post) ) { 547 if ( isset($GLOBALS['post']) ) 548 $post = & $GLOBALS['post']; 549 else 550 $post = null; 551 } elseif (is_object($post) ) { 552 if (! isset($post_cache[$post->ID])) 553 $post_cache[$post->ID] = &$post; 554 $post = & $post_cache[$post->ID]; 555 } else { 556 if ( isset($GLOBALS['post']) && ($post == $GLOBALS['post']->ID) ) 557 $post = & $GLOBALS['post']; 558 elseif (isset($post_cache[$post])) 559 $post = & $post_cache[$post]; 560 else { 561 $query = "SELECT * FROM $wpdb->posts WHERE ID=$post"; 562 $post_cache[$post] = & $wpdb->get_row($query); 563 $post = & $post_cache[$post]; 564 } 565 } 566 567 if ( $output == OBJECT ) { 568 return $post; 569 } elseif ( $output == ARRAY_A ) { 570 return get_object_vars($post); 571 } elseif ( $output == ARRAY_N ) { 572 return array_values(get_object_vars($post)); 573 } else { 574 return $post; 575 } 576 } 577 578 // Retrieves category data given a category ID or category object. 579 // The category cache is fully populated by the blog header, so we don't 580 // have to worry with managing it here. 581 function &get_category(&$category, $output = OBJECT) { 582 global $cache_categories; 583 if (is_object($category)) 584 $category = & $cache_categories[$category->cat_ID]; 585 else 586 $category = & $cache_categories[$category]; 587 588 if ( $output == OBJECT ) { 589 return $category; 590 } elseif ( $output == ARRAY_A ) { 591 return get_object_vars($category); 592 } elseif ( $output == ARRAY_N ) { 593 return array_values(get_object_vars($category)); 594 } else { 595 return $category; 596 } 597 } 598 541 599 function get_catname($cat_ID) { 542 global $cache_catnames, $wpdb; 543 if ( !$cache_catnames ) { 544 $results = $wpdb->get_results("SELECT * FROM $wpdb->categories") or die('Oops, couldn\'t query the db for categories.'); 545 foreach ($results as $post) { 546 $cache_catnames[$post->cat_ID] = $post->cat_name; 547 } 548 } 549 $cat_name = $cache_catnames[$cat_ID]; 550 return $cat_name; 600 $category = &get_category($cat_ID); 601 return $category->cat_name; 551 602 } 552 603 … … 996 1047 997 1048 function get_page_uri($page_id) { 998 global $wpdb, $cache_pages; 999 1000 $dates = ",UNIX_TIMESTAMP(post_modified) AS time_modified"; 1001 $dates .= ",UNIX_TIMESTAMP(post_date) AS time_created"; 1002 1003 if (!isset($cache_pages[$page_id])) { 1004 $cache_pages[$page_id] = $wpdb->get_row("SELECT ID, post_title, post_name, post_parent $dates FROM $wpdb->posts WHERE ID = '$page_id'"); 1005 } 1006 1007 $page = $cache_pages[$page_id]; 1049 $page = get_post($page_id); 1008 1050 $uri = urldecode($page->post_name); 1009 1051 … … 1012 1054 return $uri; 1013 1055 } 1014 1056 1015 1057 while ($page->post_parent != 0) { 1016 if (isset($cache_pages[$page->post_parent])) { 1017 $page = $cache_pages[$page->post_parent]; 1018 } else { 1019 $page = $wpdb->get_row("SELECT ID, post_title, post_name, post_parent $dates FROM $wpdb->posts WHERE ID = '$page->post_parent'"); 1020 $cache_pages[$page->ID] = $page; 1021 } 1058 $page = get_post($page->post_parent); 1022 1059 $uri = urldecode($page->post_name) . "/" . $uri; 1023 1060 } … … 1050 1087 } 1051 1088 1052 function query_posts($query) {1089 function &query_posts($query) { 1053 1090 global $wp_query; 1054 1091 return $wp_query->query($query); 1055 1092 } 1056 1093 1057 function update_post_caches($posts) { 1058 global $category_cache, $comment_count_cache, $post_meta_cache; 1094 function update_post_cache(&$posts) { 1095 global $post_cache; 1096 1097 if ( !$posts ) 1098 return; 1099 1100 for ($i = 0; $i < count($posts); $i++) { 1101 $post_cache[$posts[$i]->ID] = &$posts[$i]; 1102 } 1103 } 1104 1105 function update_post_category_cache($post_ids) { 1106 global $wpdb, $category_cache, $cache_categories; 1107 1108 if (empty($post_ids)) 1109 return; 1110 1111 if (is_array($post_ids)) 1112 $post_ids = implode(',', $post_ids); 1113 1114 $dogs = $wpdb->get_results("SELECT DISTINCT 1115 post_id, category_id FROM $wpdb->categories, $wpdb->post2cat 1116 WHERE category_id = cat_ID AND post_id IN ($post_ids)"); 1117 1118 if ( !empty($dogs) ) { 1119 foreach ($dogs as $catt) { 1120 $category_cache[$catt->post_id][$catt->category_id] = &$cache_categories[$catt->category_id]; 1121 } 1122 } 1123 } 1124 1125 function update_post_caches(&$posts) { 1126 global $post_cache, $category_cache, $comment_count_cache, $post_meta_cache; 1059 1127 global $wpdb; 1060 1128 … … 1064 1132 1065 1133 // Get the categories for all the posts 1066 foreach ($posts as $post) 1067 $post_id_list[] = $post->ID; 1134 for ($i = 0; $i < count($posts); $i++) { 1135 $post_id_list[] = $posts[$i]->ID; 1136 $post_cache[$posts[$i]->ID] = &$posts[$i]; 1137 } 1138 1068 1139 $post_id_list = implode(',', $post_id_list); 1069 1070 $dogs = $wpdb->get_results("SELECT DISTINCT 1071 post_id, category_id, cat_name, category_nicename, category_description, category_parent 1072 FROM $wpdb->categories, $wpdb->post2cat 1073 WHERE category_id = cat_ID AND post_id IN ($post_id_list)"); 1074 1075 if ( !empty($dogs) ) { 1076 foreach ($dogs as $catt) { 1077 $category_cache[$catt->post_id][$catt->category_id] = $catt; 1078 } 1079 } 1140 1141 update_post_category_cache($post_id_list); 1080 1142 1081 1143 // Do the same for comment numbers … … 1115 1177 global $cache_categories, $wpdb; 1116 1178 $dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories"); 1117 foreach ($dogs as $catt) 1179 foreach ($dogs as $catt) { 1180 $catt->category_id = $catt->cat_ID; // Alias. 1118 1181 $cache_categories[$catt->cat_ID] = $catt; 1182 } 1119 1183 } 1120 1184
Note: See TracChangeset
for help on using the changeset viewer.