Make WordPress Core

source: trunk/wp-includes/functions-post.php @ 1608

Last change on this file since 1608 was 1604, checked in by saxmatt, 21 years ago

Set GUID when posting.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
1<?php
2
3/**** DB Functions ****/
4
5/*
6 * generic function for inserting data into the posts table.
7 */
8function wp_insert_post($postarr = array()) {
9        global $wpdb, $post_default_category;
10       
11        // export array as variables
12        extract($postarr);
13       
14        // Do some escapes for safety
15        $post_title = $wpdb->escape($post_title);
16        $post_name = sanitize_title($post_title);
17        $post_excerpt = $wpdb->escape($post_excerpt);
18        $post_content = $wpdb->escape($post_content);
19        $post_author = (int) $post_author;
20
21        // Make sure we set a valid category
22        if (0 == count($post_category) || !is_array($post_category)) {
23                $post_category = array($post_default_category);
24        }
25
26        $post_cat = $post_category[0];
27       
28        if (empty($post_date))
29                $post_date = current_time('mysql');
30        // Make sure we have a good gmt date:
31        if (empty($post_date_gmt)) 
32                $post_date_gmt = get_gmt_from_date($post_date);
33        if (empty($comment_status))
34                $comment_status = get_settings('default_comment_status');
35        if (empty($ping_status))
36                $ping_status = get_settings('default_ping_status');
37       
38        $sql = "INSERT INTO $wpdb->posts 
39                (post_author, post_date, post_date_gmt, post_modified, post_modified_gmt, post_content, post_title, post_excerpt, post_category, post_status, post_name, comment_status, ping_status)
40                VALUES ('$post_author', '$post_date', '$post_date_gmt', '$post_date', '$post_date_gmt', '$post_content', '$post_title', '$post_excerpt', '$post_cat', '$post_status', '$post_name', '$comment_status', '$ping_status')";
41       
42        $result = $wpdb->query($sql);
43        $post_ID = $wpdb->insert_id;
44
45        // Set GUID
46        $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
47       
48        wp_set_post_cats('', $post_ID, $post_category);
49       
50        if ($post_status == 'publish') {
51                do_action('publish_post', $post_ID);
52        }
53
54        pingback($content, $post_ID);
55
56        // Return insert_id if we got a good result, otherwise return zero.
57        return $result ? $post_ID : 0;
58}
59
60function wp_get_single_post($postid = 0, $mode = OBJECT) {
61        global $wpdb;
62
63        $sql = "SELECT * FROM $wpdb->posts WHERE ID=$postid";
64        $result = $wpdb->get_row($sql, $mode);
65       
66        // Set categories
67        $result['post_category'] = wp_get_post_cats('',$postid);
68
69        return $result;
70}
71
72function wp_get_recent_posts($num = 10) {
73        global $wpdb;
74
75        // Set the limit clause, if we got a limit
76        if ($num) {
77                $limit = "LIMIT $num";
78        }
79
80        $sql = "SELECT * FROM $wpdb->posts ORDER BY post_date DESC $limit";
81        $result = $wpdb->get_results($sql,ARRAY_A);
82
83        return $result?$result:array();
84}
85
86function wp_update_post($postarr = array()) {
87        global $wpdb;
88
89        // First get all of the original fields
90        extract(wp_get_single_post($postarr['ID'],ARRAY_A));   
91
92        // Now overwrite any changed values being passed in
93        extract($postarr);
94       
95        // Make sure we set a valid category
96        if (0 == count($post_category) || !is_array($post_category)) {
97                $post_category = array($post_default_category);
98        }
99
100        // Do some escapes for safety
101        $post_title = $wpdb->escape($post_title);
102        $post_excerpt = $wpdb->escape($post_excerpt);
103        $post_content = $wpdb->escape($post_content);
104
105        $post_modified = current_time('mysql');
106        $post_modified_gmt = current_time('mysql', 1);
107
108        $sql = "UPDATE $wpdb->posts 
109                SET post_content = '$post_content',
110                post_title = '$post_title',
111                post_category = $post_category[0],
112                post_status = '$post_status',
113                post_date = '$post_date',
114                post_date_gmt = '$post_date_gmt',
115                post_modified = '$post_modified',
116                post_modified_gmt = '$post_modified_gmt',
117                post_excerpt = '$post_excerpt',
118                ping_status = '$ping_status',
119                comment_status = '$comment_status'
120                WHERE ID = $ID";
121               
122        $result = $wpdb->query($sql);
123
124        wp_set_post_cats('',$ID,$post_category);
125       
126        return $wpdb->rows_affected;
127}
128
129function wp_get_post_cats($blogid = '1', $post_ID = 0) {
130        global $wpdb;
131       
132        $sql = "SELECT category_id
133                FROM $wpdb->post2cat 
134                WHERE post_id = $post_ID 
135                ORDER BY category_id";
136
137        $result = $wpdb->get_col($sql);
138
139        return array_unique($result);
140}
141
142function wp_set_post_cats($blogid = '1', $post_ID = 0, $post_categories = array()) {
143        global $wpdb;
144        // If $post_categories isn't already an array, make it one:
145        if (!is_array($post_categories)) {
146                if (!$post_categories) {
147                        $post_categories = 1;
148                }
149                $post_categories = array($post_categories);
150        }
151
152        $post_categories = array_unique($post_categories);
153
154        // First the old categories
155        $old_categories = $wpdb->get_col("
156                SELECT category_id
157                FROM $wpdb->post2cat 
158                WHERE post_id = $post_ID");
159       
160        if (!$old_categories) {
161                $old_categories = array();
162        } else {
163                $old_categories = array_unique($old_categories);
164        }
165
166
167        $oldies = print_r($old_categories,1);
168        $newbies = print_r($post_categories,1);
169
170        logio("O","Old: $oldies\nNew: $newbies\n");
171
172        // Delete any?
173        $delete_cats = array_diff($old_categories,$post_categories);
174
175        logio("O","Delete: " . print_r($delete_cats,1));
176               
177        if ($delete_cats) {
178                foreach ($delete_cats as $del) {
179                        $wpdb->query("
180                                DELETE FROM $wpdb->post2cat 
181                                WHERE category_id = $del 
182                                        AND post_id = $post_ID 
183                                ");
184
185                        logio("O","deleting post/cat: $post_ID, $del");
186                }
187        }
188
189        // Add any?
190        $add_cats = array_diff($post_categories, $old_categories);
191
192        logio("O","Add: " . print_r($add_cats,1));
193               
194        if ($add_cats) {
195                foreach ($add_cats as $new_cat) {
196                        $wpdb->query("
197                                INSERT INTO $wpdb->post2cat (post_id, category_id)
198                                VALUES ($post_ID, $new_cat)");
199
200                                logio("O","adding post/cat: $post_ID, $new_cat");
201                }
202        }
203}       // wp_set_post_cats()
204
205function wp_delete_post($postid = 0) {
206        global $wpdb;
207       
208        $sql = "DELETE FROM $wpdb->post2cat WHERE post_id = $postid";
209        $wpdb->query($sql);
210               
211        $sql = "DELETE FROM $wpdb->posts WHERE ID = $postid";
212       
213        $wpdb->query($sql);
214
215        $result = $wpdb->rows_affected;
216       
217        return $result;
218}
219
220/**** /DB Functions ****/
221
222/**** Misc ****/
223
224// get permalink from post ID
225function post_permalink($post_id = 0, $mode = '') { // $mode legacy
226        return get_permalink($post_id);
227}
228
229// Get the name of a category from its ID
230function get_cat_name($cat_id) {
231        global $wpdb;
232       
233        $cat_id -= 0;   // force numeric
234        $name = $wpdb->get_var("SELECT cat_name FROM $wpdb->categories WHERE cat_ID=$cat_id");
235       
236        return $name;
237}
238
239// Get the ID of a category from its name
240function get_cat_ID($cat_name='General') {
241        global $wpdb;
242       
243        $cid = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'");
244
245        return $cid?$cid:1;     // default to cat 1
246}
247
248// Get author's preferred display name
249function get_author_name($auth_id) {
250        $authordata = get_userdata($auth_id);
251
252        switch($authordata["user_idmode"]) {
253                case "nickname":
254                        $authorname = $authordata["user_nickname"];
255
256                case "login":
257                        $authorname = $authordata["user_login"];
258                        break;
259       
260                case "firstname":
261                        $authorname = $authordata["user_firstname"];
262                        break;
263
264                case "lastname":
265                        $authorname = $authordata["user_lastname"];
266                        break;
267
268                case "namefl":
269                        $authorname = $authordata["user_firstname"]." ".$authordata["user_lastname"];
270                        break;
271
272                case "namelf":
273                        $authorname = $authordata["user_lastname"]." ".$authordata["user_firstname"];
274                        break;
275
276                default:
277                        $authorname = $authordata["user_nickname"];
278                        break;
279        }
280
281        return $authorname;
282}
283
284// get extended entry info (<!--more-->)
285function get_extended($post) {
286        list($main,$extended) = explode('<!--more-->',$post);
287
288        // Strip leading and trailing whitespace
289        $main = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$main);
290        $extended = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$extended);
291
292        return array('main' => $main, 'extended' => $extended);
293}
294
295// do trackbacks for a list of urls
296// borrowed from edit.php
297// accepts a comma-separated list of trackback urls and a post id
298function trackback_url_list($tb_list, $post_id) {
299        if (!empty($tb_list)) {
300                // get post data
301                $postdata = wp_get_single_post($post_id, ARRAY_A);
302
303                // import postdata as variables
304                extract($postdata);
305               
306                // form an excerpt
307                $excerpt = strip_tags($post_excerpt?$post_excerpt:$post_content);
308               
309                if (strlen($excerpt) > 255) {
310                        $excerpt = substr($excerpt,0,252) . '...';
311                }
312               
313                $trackback_urls = explode(',', $tb_list);
314                foreach($trackback_urls as $tb_url) {
315                    $tb_url = trim($tb_url);
316                    trackback($tb_url, stripslashes($post_title), $excerpt, $post_id);
317                }
318    }
319}
320
321
322// query user capabilities
323
324/* returns true if a given $user_id can edit a given $post_id.
325   note: optional $blog_id for future usage?                   */
326function user_can_edit_post($user_id, $post_id, $blog_id = 1) {
327        $author_data = get_userdata($user_id);
328        $post_data   = get_postdata($post_id);
329        $post_author_data = get_userdata($post_data['Author_ID']);
330
331        if ( ($user_id == $post_author_data->ID)
332             || ($author_data->user_level > $post_author_data->user_level) ) {
333                return true;
334        } else {
335                return false;
336        }
337}
338
339/* returns true if a given $user_id can delete a given $post_id.
340   note: optional $blog_id for future usage?                   */
341function user_can_delete_post($user_id, $post_id, $blog_id = 1) {
342        // right now if one can edit, one can delete
343        return user_can_edit_post($user_id, $post_id, $blog_id);
344}
345
346?>
Note: See TracBrowser for help on using the repository browser.