]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/post.php
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-admin / includes / post.php
1 <?php
2
3 // Update an existing post with values provided in $_POST.
4 function edit_post() {
5
6         $post_ID = (int) $_POST['post_ID'];
7
8         if ( 'page' == $_POST['post_type'] ) {
9                 if ( !current_user_can( 'edit_page', $post_ID ) )
10                         wp_die( __('You are not allowed to edit this page.' ));
11         } else {
12                 if ( !current_user_can( 'edit_post', $post_ID ) )
13                         wp_die( __('You are not allowed to edit this post.' ));
14         }
15
16         // Autosave shouldn't save too soon after a real save
17         if ( 'autosave' == $_POST['action'] ) {
18                 $post =& get_post( $post_ID );
19                 $now = time();
20                 $then = strtotime($post->post_date_gmt . ' +0000');
21                 $delta = AUTOSAVE_INTERVAL / 2;
22                 if ( ($now - $then) < $delta )
23                         return $post_ID;
24         }
25
26         // Rename.
27         $_POST['ID'] = (int) $_POST['post_ID'];
28         $_POST['post_content'] = $_POST['content'];
29         $_POST['post_excerpt'] = $_POST['excerpt'];
30         $_POST['post_parent'] = isset($_POST['parent_id'])? $_POST['parent_id'] : '';
31         $_POST['to_ping'] = $_POST['trackback_url'];
32
33         if (!empty ( $_POST['post_author_override'] ) ) {
34                 $_POST['post_author'] = (int) $_POST['post_author_override'];
35         } else
36                 if (!empty ( $_POST['post_author'] ) ) {
37                         $_POST['post_author'] = (int) $_POST['post_author'];
38                 } else {
39                         $_POST['post_author'] = (int) $_POST['user_ID'];
40                 }
41
42         if ( $_POST['post_author'] != $_POST['user_ID'] ) {
43                 if ( 'page' == $_POST['post_type'] ) {
44                         if ( !current_user_can( 'edit_others_pages' ) )
45                                 wp_die( __('You are not allowed to edit pages as this user.' ));
46                 } else {
47                         if ( !current_user_can( 'edit_others_posts' ) )
48                                 wp_die( __('You are not allowed to edit posts as this user.' ));
49
50                 }
51         }
52
53         // What to do based on which button they pressed
54         if ( isset($_POST['saveasdraft']) && '' != $_POST['saveasdraft'] )
55                 $_POST['post_status'] = 'draft';
56         if ( isset($_POST['saveasprivate']) && '' != $_POST['saveasprivate'] )
57                 $_POST['post_status'] = 'private';
58         if ( isset($_POST['publish']) && ( '' != $_POST['publish'] ) && ( $_POST['post_status'] != 'private' ) )
59                 $_POST['post_status'] = 'publish';
60         if ( isset($_POST['advanced']) && '' != $_POST['advanced'] )
61                 $_POST['post_status'] = 'draft';
62
63         if ( 'page' == $_POST['post_type'] ) {
64                 if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_pages' ))
65                         $_POST['post_status'] = 'pending';
66         } else {
67                 if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_posts' ))
68                         $_POST['post_status'] = 'pending';
69         }
70
71         if (!isset( $_POST['comment_status'] ))
72                 $_POST['comment_status'] = 'closed';
73
74         if (!isset( $_POST['ping_status'] ))
75                 $_POST['ping_status'] = 'closed';
76
77         foreach ( array ('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
78                 if ( !empty( $_POST['hidden_' . $timeunit] ) && $_POST['hidden_' . $timeunit] != $_POST[$timeunit] ) {
79                         $_POST['edit_date'] = '1';
80                         break;
81                 }
82         }
83
84         if (!empty ( $_POST['edit_date'] ) ) {
85                 $aa = $_POST['aa'];
86                 $mm = $_POST['mm'];
87                 $jj = $_POST['jj'];
88                 $hh = $_POST['hh'];
89                 $mn = $_POST['mn'];
90                 $ss = $_POST['ss'];
91                 $jj = ($jj > 31 ) ? 31 : $jj;
92                 $hh = ($hh > 23 ) ? $hh -24 : $hh;
93                 $mn = ($mn > 59 ) ? $mn -60 : $mn;
94                 $ss = ($ss > 59 ) ? $ss -60 : $ss;
95                 $_POST['post_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
96                 $_POST['post_date_gmt'] = get_gmt_from_date( "$aa-$mm-$jj $hh:$mn:$ss" );
97         }
98
99         // Meta Stuff
100         if ( isset($_POST['meta']) && $_POST['meta'] ) {
101                 foreach ( $_POST['meta'] as $key => $value )
102                         update_meta( $key, $value['key'], $value['value'] );
103         }
104
105         if ( isset($_POST['deletemeta']) && $_POST['deletemeta'] ) {
106                 foreach ( $_POST['deletemeta'] as $key => $value )
107                         delete_meta( $key );
108         }
109
110         add_meta( $post_ID );
111
112         wp_update_post( $_POST );
113
114         // Reunite any orphaned attachments with their parent
115         if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
116                 $draft_ids = array();
117         if ( $draft_temp_id = (int) array_search( $post_ID, $draft_ids ) )
118                 _relocate_children( $draft_temp_id, $post_ID );
119
120         // Now that we have an ID we can fix any attachment anchor hrefs
121         _fix_attachment_links( $post_ID );
122
123         wp_set_post_lock( $post_ID, $GLOBALS['current_user']->ID );
124
125         return $post_ID;
126 }
127
128 // Default post information to use when populating the "Write Post" form.
129 function get_default_post_to_edit() {
130         if ( !empty( $_REQUEST['post_title'] ) )
131                 $post_title = wp_specialchars( stripslashes( $_REQUEST['post_title'] ));
132         else if ( !empty( $_REQUEST['popuptitle'] ) ) {
133                 $post_title = wp_specialchars( stripslashes( $_REQUEST['popuptitle'] ));
134                 $post_title = funky_javascript_fix( $post_title );
135         } else {
136                 $post_title = '';
137         }
138
139         $post_content = '';
140         if ( !empty( $_REQUEST['content'] ) )
141                 $post_content = wp_specialchars( stripslashes( $_REQUEST['content'] ));
142         else if ( !empty( $post_title ) ) {
143                 $text       = wp_specialchars( stripslashes( urldecode( $_REQUEST['text'] ) ) );
144                 $text       = funky_javascript_fix( $text);
145                 $popupurl   = clean_url($_REQUEST['popupurl']);
146         $post_content = '<a href="'.$popupurl.'">'.$post_title.'</a>'."\n$text";
147     }
148
149         if ( !empty( $_REQUEST['excerpt'] ) )
150                 $post_excerpt = wp_specialchars( stripslashes( $_REQUEST['excerpt'] ));
151         else
152                 $post_excerpt = '';
153
154         $post->ID = 0;
155         $post->post_name = '';
156         $post->post_author = '';
157         $post->post_date = '';
158         $post->post_status = 'draft';
159         $post->post_type = 'post';
160         $post->to_ping = '';
161         $post->pinged = '';
162         $post->comment_status = get_option( 'default_comment_status' );
163         $post->ping_status = get_option( 'default_ping_status' );
164         $post->post_pingback = get_option( 'default_pingback_flag' );
165         $post->post_category = get_option( 'default_category' );
166         $post->post_content = apply_filters( 'default_content', $post_content);
167         $post->post_title = apply_filters( 'default_title', $post_title );
168         $post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt);
169         $post->page_template = 'default';
170         $post->post_parent = 0;
171         $post->menu_order = 0;
172
173         return $post;
174 }
175
176 function get_default_page_to_edit() {
177         $page = get_default_post_to_edit();
178         $page->post_type = 'page';
179         return $page;
180 }
181
182 // Get an existing post and format it for editing.
183 function get_post_to_edit( $id ) {
184
185         $post = get_post( $id, OBJECT, 'edit' );
186
187         if ( $post->post_type == 'page' )
188                 $post->page_template = get_post_meta( $id, '_wp_page_template', true );
189
190         return $post;
191 }
192
193 function post_exists($title, $content = '', $post_date = '') {
194         global $wpdb;
195
196         if (!empty ($post_date))
197                 $post_date = "AND post_date = '$post_date'";
198
199         if (!empty ($title))
200                 return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date");
201         else
202                 if (!empty ($content))
203                         return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date");
204
205         return 0;
206 }
207
208 // Creates a new post from the "Write Post" form using $_POST information.
209 function wp_write_post() {
210         global $user_ID;
211
212         if ( 'page' == $_POST['post_type'] ) {
213                 if ( !current_user_can( 'edit_pages' ) )
214                         return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this blog.' ) );
215         } else {
216                 if ( !current_user_can( 'edit_posts' ) )
217                         return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this blog.' ) );
218         }
219
220
221         // Check for autosave collisions
222         $temp_id = false;
223         if ( isset($_POST['temp_ID']) ) {
224                 $temp_id = (int) $_POST['temp_ID'];
225                 if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
226                         $draft_ids = array();
227                 foreach ( $draft_ids as $temp => $real )
228                         if ( time() + $temp > 86400 ) // 1 day: $temp is equal to -1 * time( then )
229                                 unset($draft_ids[$temp]);
230
231                 if ( isset($draft_ids[$temp_id]) ) { // Edit, don't write
232                         $_POST['post_ID'] = $draft_ids[$temp_id];
233                         unset($_POST['temp_ID']);
234                         update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );
235                         return edit_post();
236                 }
237         }
238
239         // Rename.
240         $_POST['post_content'] = $_POST['content'];
241         $_POST['post_excerpt'] = $_POST['excerpt'];
242         $_POST['post_parent'] = isset($_POST['parent_id'])? $_POST['parent_id'] : '';
243         $_POST['to_ping'] = $_POST['trackback_url'];
244
245         if (!empty ( $_POST['post_author_override'] ) ) {
246                 $_POST['post_author'] = (int) $_POST['post_author_override'];
247         } else {
248                 if (!empty ( $_POST['post_author'] ) ) {
249                         $_POST['post_author'] = (int) $_POST['post_author'];
250                 } else {
251                         $_POST['post_author'] = (int) $_POST['user_ID'];
252                 }
253
254         }
255
256         if ( $_POST['post_author'] != $_POST['user_ID'] ) {
257                 if ( 'page' == $_POST['post_type'] ) {
258                         if ( !current_user_can( 'edit_others_pages' ) )
259                                 return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) );
260                 } else {
261                         if ( !current_user_can( 'edit_others_posts' ) )
262                                 return new WP_Error( 'edit_others_posts', __( 'You are not allowed to post as this user.' ) );
263
264                 }
265         }
266
267         // What to do based on which button they pressed
268         if ( isset($_POST['saveasdraft']) && '' != $_POST['saveasdraft'] )
269                 $_POST['post_status'] = 'draft';
270         if ( isset($_POST['saveasprivate']) && '' != $_POST['saveasprivate'] )
271                 $_POST['post_status'] = 'private';
272         if ( isset($_POST['publish']) && ( '' != $_POST['publish'] ) && ( $_POST['post_status'] != 'private' ) )
273                 $_POST['post_status'] = 'publish';
274         if ( isset($_POST['advanced']) && '' != $_POST['advanced'] )
275                 $_POST['post_status'] = 'draft';
276
277         if ( 'page' == $_POST['post_type'] ) {
278                 if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_pages' ) )
279                         $_POST['post_status'] = 'pending';
280         } else {
281                 if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_posts' ) )
282                         $_POST['post_status'] = 'pending';
283         }
284
285         if (!isset( $_POST['comment_status'] ))
286                 $_POST['comment_status'] = 'closed';
287
288         if (!isset( $_POST['ping_status'] ))
289                 $_POST['ping_status'] = 'closed';
290
291         foreach ( array ('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
292                 if ( !empty( $_POST['hidden_' . $timeunit] ) && $_POST['hidden_' . $timeunit] != $_POST[$timeunit] ) {
293                         $_POST['edit_date'] = '1';
294                         break;
295                 }
296         }
297
298         if (!empty ( $_POST['edit_date'] ) ) {
299                 $aa = $_POST['aa'];
300                 $mm = $_POST['mm'];
301                 $jj = $_POST['jj'];
302                 $hh = $_POST['hh'];
303                 $mn = $_POST['mn'];
304                 $ss = $_POST['ss'];
305                 $jj = ($jj > 31 ) ? 31 : $jj;
306                 $hh = ($hh > 23 ) ? $hh -24 : $hh;
307                 $mn = ($mn > 59 ) ? $mn -60 : $mn;
308                 $ss = ($ss > 59 ) ? $ss -60 : $ss;
309                 $_POST['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
310                 $_POST['post_date_gmt'] = get_gmt_from_date( $_POST['post_date'] );
311         }
312
313         // Create the post.
314         $post_ID = wp_insert_post( $_POST );
315         if ( is_wp_error( $post_ID ) )
316                 return $post_ID;
317
318         if ( empty($post_ID) )
319                 return 0;
320
321         add_meta( $post_ID );
322
323         // Reunite any orphaned attachments with their parent
324         if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
325                 $draft_ids = array();
326         if ( $draft_temp_id = (int) array_search( $post_ID, $draft_ids ) )
327                 _relocate_children( $draft_temp_id, $post_ID );
328         if ( $temp_id && $temp_id != $draft_temp_id )
329                 _relocate_children( $temp_id, $post_ID );
330
331         // Update autosave collision detection
332         if ( $temp_id ) {
333                 $draft_ids[$temp_id] = $post_ID;
334                 update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );
335         }
336
337         // Now that we have an ID we can fix any attachment anchor hrefs
338         _fix_attachment_links( $post_ID );
339
340         wp_set_post_lock( $post_ID, $GLOBALS['current_user']->ID );
341
342         return $post_ID;
343 }
344
345 function write_post() {
346         $result = wp_write_post();
347         if( is_wp_error( $result ) )
348                 wp_die( $result->get_error_message() );
349         else
350                 return $result;
351 }
352
353 //
354 // Post Meta
355 //
356
357 function add_meta( $post_ID ) {
358         global $wpdb;
359         $post_ID = (int) $post_ID;
360
361         $protected = array( '_wp_attached_file', '_wp_attachment_metadata', '_wp_old_slug', '_wp_page_template' );
362
363         $metakeyselect = $wpdb->escape( stripslashes( trim( $_POST['metakeyselect'] ) ) );
364         $metakeyinput = $wpdb->escape( stripslashes( trim( $_POST['metakeyinput'] ) ) );
365         $metavalue = maybe_serialize( stripslashes( (trim( $_POST['metavalue'] ) ) ));
366         $metavalue = $wpdb->escape( $metavalue );
367
368         if ( ('0' === $metavalue || !empty ( $metavalue ) ) && ((('#NONE#' != $metakeyselect) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput) ) ) {
369                 // We have a key/value pair. If both the select and the
370                 // input for the key have data, the input takes precedence:
371
372                 if ('#NONE#' != $metakeyselect)
373                         $metakey = $metakeyselect;
374
375                 if ( $metakeyinput)
376                         $metakey = $metakeyinput; // default
377
378                 if ( in_array($metakey, $protected) )
379                         return false;
380
381                 wp_cache_delete($post_ID, 'post_meta');
382
383                 $wpdb->query( "
384                                 INSERT INTO $wpdb->postmeta
385                                 (post_id,meta_key,meta_value )
386                                 VALUES ('$post_ID','$metakey','$metavalue' )
387                         " );
388                 return $wpdb->insert_id;
389         }
390         return false;
391 } // add_meta
392
393 function delete_meta( $mid ) {
394         global $wpdb;
395         $mid = (int) $mid;
396
397         $post_id = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = '$mid'");
398         wp_cache_delete($post_id, 'post_meta');
399
400         return $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_id = '$mid'" );
401 }
402
403 // Get a list of previously defined keys
404 function get_meta_keys() {
405         global $wpdb;
406
407         $keys = $wpdb->get_col( "
408                         SELECT meta_key
409                         FROM $wpdb->postmeta
410                         GROUP BY meta_key
411                         ORDER BY meta_key" );
412
413         return $keys;
414 }
415
416 function get_post_meta_by_id( $mid ) {
417         global $wpdb;
418         $mid = (int) $mid;
419
420         $meta = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_id = '$mid'" );
421         if ( is_serialized_string( $meta->meta_value ) )
422                 $meta->meta_value = maybe_unserialize( $meta->meta_value );
423         return $meta;
424 }
425
426 // Some postmeta stuff
427 function has_meta( $postid ) {
428         global $wpdb;
429
430         return $wpdb->get_results( "
431                         SELECT meta_key, meta_value, meta_id, post_id
432                         FROM $wpdb->postmeta
433                         WHERE post_id = '$postid'
434                         ORDER BY meta_key,meta_id", ARRAY_A );
435
436 }
437
438 function update_meta( $mid, $mkey, $mvalue ) {
439         global $wpdb;
440
441         $protected = array( '_wp_attached_file', '_wp_attachment_metadata', '_wp_old_slug', '_wp_page_template' );
442
443         if ( in_array($mkey, $protected) )
444                 return false;
445
446         $post_id = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = '$mid'");
447         wp_cache_delete($post_id, 'post_meta');
448
449         $mvalue = maybe_serialize( stripslashes( $mvalue ));
450         $mvalue = $wpdb->escape( $mvalue );
451         $mid = (int) $mid;
452         return $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '$mkey', meta_value = '$mvalue' WHERE meta_id = '$mid'" );
453 }
454
455 //
456 // Private
457 //
458
459 // Replace hrefs of attachment anchors with up-to-date permalinks.
460 function _fix_attachment_links( $post_ID ) {
461
462         $post = & get_post( $post_ID, ARRAY_A );
463
464         $search = "#<a[^>]+rel=('|\")[^'\"]*attachment[^>]*>#ie";
465
466         // See if we have any rel="attachment" links
467         if ( 0 == preg_match_all( $search, $post['post_content'], $anchor_matches, PREG_PATTERN_ORDER ) )
468                 return;
469
470         $i = 0;
471         $search = "#[\s]+rel=(\"|')(.*?)wp-att-(\d+)\\1#i";
472         foreach ( $anchor_matches[0] as $anchor ) {
473                 if ( 0 == preg_match( $search, $anchor, $id_matches ) )
474                         continue;
475
476                 $id = (int) $id_matches[3];
477
478                 // While we have the attachment ID, let's adopt any orphans.
479                 $attachment = & get_post( $id, ARRAY_A );
480                 if ( ! empty( $attachment) && ! is_object( get_post( $attachment['post_parent'] ) ) ) {
481                         $attachment['post_parent'] = $post_ID;
482                         // Escape data pulled from DB.
483                         $attachment = add_magic_quotes( $attachment);
484                         wp_update_post( $attachment);
485                 }
486
487                 $post_search[$i] = $anchor;
488                 $post_replace[$i] = preg_replace( "#href=(\"|')[^'\"]*\\1#e", "stripslashes( 'href=\\1' ).get_attachment_link( $id ).stripslashes( '\\1' )", $anchor );
489                 ++$i;
490         }
491
492         $post['post_content'] = str_replace( $post_search, $post_replace, $post['post_content'] );
493
494         // Escape data pulled from DB.
495         $post = add_magic_quotes( $post);
496
497         return wp_update_post( $post);
498 }
499
500 // Move child posts to a new parent
501 function _relocate_children( $old_ID, $new_ID ) {
502         global $wpdb;
503         $old_ID = (int) $old_ID;
504         $new_ID = (int) $new_ID;
505         return $wpdb->query( "UPDATE $wpdb->posts SET post_parent = $new_ID WHERE post_parent = $old_ID" );
506 }
507
508 function get_available_post_statuses($type = 'post') {
509         $stati = wp_count_posts($type);
510
511         return array_keys(get_object_vars($stati));
512 }
513
514 function wp_edit_posts_query( $q = false ) {
515         global $wpdb;
516         if ( false === $q )
517                 $q = $_GET;
518         $q['m']   = (int) $q['m'];
519         $q['cat'] = (int) $q['cat'];
520         $post_stati  = array(   //      array( adj, noun )
521                                 'publish' => array(__('Published'), __('Published posts'), __ngettext_noop('Published (%s)', 'Published (%s)')),
522                                 'future' => array(__('Scheduled'), __('Scheduled posts'), __ngettext_noop('Scheduled (%s)', 'Scheduled (%s)')),
523                                 'pending' => array(__('Pending Review'), __('Pending posts'), __ngettext_noop('Pending Review (%s)', 'Pending Review (%s)')),
524                                 'draft' => array(__('Draft'), _c('Drafts|manage posts header'), __ngettext_noop('Draft (%s)', 'Drafts (%s)')),
525                                 'private' => array(__('Private'), __('Private posts'), __ngettext_noop('Private (%s)', 'Private (%s)')),
526                         );
527
528         $post_stati = apply_filters('post_stati', $post_stati);
529
530         $avail_post_stati = get_available_post_statuses('post');
531
532         $post_status_q = '';
533         if ( isset($q['post_status']) && in_array( $q['post_status'], array_keys($post_stati) ) ) {
534                 $post_status_q = '&post_status=' . $q['post_status'];
535                 $post_status_q .= '&perm=readable';
536         }
537
538         if ( 'pending' === $q['post_status'] ) {
539                 $order = 'ASC';
540                 $orderby = 'modified';
541         } elseif ( 'draft' === $q['post_status'] ) {
542                 $order = 'DESC';
543                 $orderby = 'modified';
544         } else {
545                 $order = 'DESC';
546                 $orderby = 'date';
547         }
548
549         wp("post_type=post&what_to_show=posts$post_status_q&posts_per_page=15&order=$order&orderby=$orderby");
550
551         return array($post_stati, $avail_post_stati);
552 }
553
554 function get_available_post_mime_types($type = 'attachment') {
555         global $wpdb;
556
557         $types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type));
558         return $types;
559 }
560
561 function wp_edit_attachments_query( $q = false ) {
562         global $wpdb;
563         if ( false === $q )
564                 $q = $_GET;
565         $q['m']   = (int) $q['m'];
566         $q['cat'] = (int) $q['cat'];
567         $q['post_type'] = 'attachment';
568         $q['post_status'] = 'any';
569         $q['posts_per_page'] = 15;
570         $post_mime_types = array(       //      array( adj, noun )
571                                 'image' => array(__('Images'), __('Manage Images'), __ngettext_noop('Image (%s)', 'Images (%s)')),
572                                 'audio' => array(__('Audio'), __('Manage Audio'), __ngettext_noop('Audio (%s)', 'Audio (%s)')),
573                                 'video' => array(__('Video'), __('Manage Video'), __ngettext_noop('Video (%s)', 'Video (%s)')),
574                         );
575         $post_mime_types = apply_filters('post_mime_types', $post_mime_types);
576
577         $avail_post_mime_types = get_available_post_mime_types('attachment');
578
579         if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) )
580                 unset($q['post_mime_type']);
581
582         wp($q);
583
584         return array($post_mime_types, $avail_post_mime_types);
585 }
586
587 function postbox_classes( $id, $page ) {
588         $current_user = wp_get_current_user();
589         if ( $closed = get_usermeta( $current_user->ID, 'closedpostboxes_'.$page ) ) {
590                 if ( !is_array( $closed ) ) return '';
591                 return in_array( $id, $closed )? 'if-js-closed' : '';
592         } else {
593                 if ( 'tagsdiv' == $id || 'categorydiv' == $id ) return '';
594                 else return 'if-js-closed';
595         }
596 }
597
598 function get_sample_permalink($id, $title=null, $name = null) {
599         $post = &get_post($id);
600         if (!$post->ID) {
601                 return array('', '');
602         }
603         $original_status = $post->post_status;
604         $original_date = $post->post_date;
605         $original_name = $post->post_name;
606
607         // Hack: get_permalink would return ugly permalink for
608         // drafts, so we will fake, that our post is published
609         if (in_array($post->post_status, array('draft', 'pending'))) {
610                 $post->post_status = 'publish';
611                 $post->post_date = date('Y-m-d H:i:s');
612                 $post->post_name = sanitize_title($post->post_name? $post->post_name : $post->post_title, $post->ID); 
613         }
614
615         // If the user wants to set a new name -- override the current one
616         // Note: if empty name is supplied -- use the title instead, see #6072
617         if (!is_null($name)) {
618                 $post->post_name = sanitize_title($name? $name : $title, $post->ID);
619         }
620
621         $permalink = get_permalink($post, true);
622
623         // Handle page hierarchy
624         if ( 'page' == $post->post_type ) {
625                 $uri = get_page_uri($post->ID);
626                 $uri = untrailingslashit($uri);
627                 $uri = strrev( stristr( strrev( $uri ), '/' ) );
628                 $uri = untrailingslashit($uri);
629                 if ( !empty($uri) )
630                         $uri .='/';
631                 $permalink = str_replace('%pagename%', "${uri}%pagename%", $permalink);
632         }
633
634         $permalink = array($permalink, $post->post_name);
635         $post->post_status = $original_status;
636         $post->post_date = $original_date;
637         $post->post_name = $original_name;
638         $post->post_title = $original_title;
639         return $permalink;
640 }
641
642 function get_sample_permalink_html($id, $new_title=null, $new_slug=null) {
643         $post = &get_post($id);
644         list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
645         if (false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%')) {
646                 return '';
647         }
648         $title = __('Click to edit this part of the permalink');
649         if (strlen($post_name) > 30) {
650                 $post_name_abridged = substr($post_name, 0, 14). '&hellip;' . substr($post_name, -14);
651         } else {
652                 $post_name_abridged = $post_name;
653         }
654         $post_name_html = '<span id="editable-post-name" title="'.$title.'">'.$post_name_abridged.'</span><span id="editable-post-name-full">'.$post_name.'</span>';
655         $display_link = str_replace(array('%pagename%','%postname%'), $post_name_html, $permalink);
656         $return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink">' . $display_link . "</span>\n";
657         $return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug" onclick="edit_permalink(' . $id . '); return false;">' . __('Edit') . "</a></span>\n";
658         return $return;
659 }
660
661 // false: not locked or locked by current user
662 // int: user ID of user with lock
663 function wp_check_post_lock( $post_id ) {
664         global $current_user;
665
666         if ( !$post = get_post( $post_id ) )
667                 return false;
668
669         $lock = get_post_meta( $post->ID, '_edit_lock', true );
670         $last = get_post_meta( $post->ID, '_edit_last', true );
671
672         $time_window = apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 );
673
674         if ( $lock && $lock > time() - $time_window && $last != $current_user->ID )
675                 return $last;
676         return false;
677 }
678
679 function wp_set_post_lock( $post_id ) {
680         global $current_user;
681         if ( !$post = get_post( $post_id ) )
682                 return false;
683         if ( !$current_user || !$current_user->ID )
684                 return false;
685
686         $now = time();
687
688         if ( !add_post_meta( $post->ID, '_edit_lock', $now, true ) )
689                 update_post_meta( $post->ID, '_edit_lock', $now );
690         if ( !add_post_meta( $post->ID, '_edit_last', $current_user->ID, true ) )
691                 update_post_meta( $post->ID, '_edit_last', $current_user->ID );
692 }
693
694 ?>