Wordpress 2.3.3-scripts
[autoinstalls/wordpress.git] / wp-admin / admin-ajax.php
1 <?php
2 require_once('../wp-config.php');
3 require_once('includes/admin.php');
4
5 define('DOING_AJAX', true);
6
7 check_ajax_referer();
8 if ( !is_user_logged_in() )
9         die('-1');
10
11 function get_out_now() { exit; }
12 add_action( 'shutdown', 'get_out_now', -1 );
13
14 function wp_ajax_meta_row( $pid, $mid, $key, $value ) {
15         $value = attribute_escape($value);
16         $key_js = addslashes(wp_specialchars($key, 'double'));
17         $key = attribute_escape($key);
18         $r .= "<tr id='meta-$mid'><td valign='top'>";
19         $r .= "<input name='meta[$mid][key]' tabindex='6' onkeypress='return killSubmit(\"theList.ajaxUpdater(&#039;meta&#039;,&#039;meta-$mid&#039;);\",event);' type='text' size='20' value='$key' />";
20         $r .= "</td><td><textarea name='meta[$mid][value]' tabindex='6' rows='2' cols='30'>$value</textarea></td><td align='center'>";
21         $r .= "<input name='updatemeta' type='button' class='updatemeta' tabindex='6' value='".attribute_escape(__('Update'))."' onclick='return theList.ajaxUpdater(&#039;meta&#039;,&#039;meta-$mid&#039;);' /><br />";
22         $r .= "<input name='deletemeta[$mid]' type='submit' onclick=\"return deleteSomething( 'meta', $mid, '";
23         $r .= js_escape(sprintf(__("You are about to delete the '%s' custom field on this post.\n'OK' to delete, 'Cancel' to stop."), $key_js));
24         $r .= "' );\" class='deletemeta' tabindex='6' value='".attribute_escape(__('Delete'))."' /></td></tr>";
25         return $r;
26 }
27
28 $id = (int) $_POST['id'];
29 switch ( $_POST['action'] ) :
30 case 'delete-comment' :
31         if ( !$comment = get_comment( $id ) )
32                 die('0');
33         if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
34                 die('-1');
35
36         if ( wp_delete_comment( $comment->comment_ID ) )
37                 die('1');
38         else    die('0');
39         break;
40 case 'delete-comment-as-spam' :
41         if ( !$comment = get_comment( $id ) )
42                 die('0');
43         if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
44                 die('-1');
45
46         if ( wp_set_comment_status( $comment->comment_ID, 'spam' ) )
47                 die('1');
48         else    die('0');
49         break;
50 case 'delete-cat' :
51         if ( !current_user_can( 'manage_categories' ) )
52                 die('-1');
53
54         if ( wp_delete_category( $id ) )
55                 die('1');
56         else    die('0');
57         break;
58 case 'delete-link' :
59         if ( !current_user_can( 'manage_links' ) )
60                 die('-1');
61
62         if ( wp_delete_link( $id ) )
63                 die('1');
64         else    die('0');
65         break;
66 case 'delete-meta' :
67         if ( !$meta = get_post_meta_by_id( $id ) )
68                 die('0');
69         if ( !current_user_can( 'edit_post', $meta->post_id ) )
70                 die('-1');
71         if ( delete_meta( $meta->meta_id ) )
72                 die('1');
73         die('0');
74         break;
75 case 'delete-post' :
76         if ( !current_user_can( 'delete_post', $id ) )
77                 die('-1');
78
79         if ( wp_delete_post( $id ) )
80                 die('1');
81         else    die('0');
82         break;
83 case 'delete-page' :
84         if ( !current_user_can( 'delete_page', $id ) )
85                 die('-1');
86
87         if ( wp_delete_post( $id ) )
88                 die('1');
89         else    die('0');
90         break;
91 case 'dim-comment' :
92         if ( !$comment = get_comment( $id ) )
93                 die('0');
94         if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
95                 die('-1');
96         if ( !current_user_can( 'moderate_comments' ) )
97                 die('-1');
98
99         if ( 'unapproved' == wp_get_comment_status($comment->comment_ID) ) {
100                 if ( wp_set_comment_status( $comment->comment_ID, 'approve' ) )
101                         die('1');
102         } else {
103                 if ( wp_set_comment_status( $comment->comment_ID, 'hold' ) )
104                         die('1');
105         }
106         die('0');
107         break;
108 case 'add-category' : // On the Fly
109         if ( !current_user_can( 'manage_categories' ) )
110                 die('-1');
111         $names = explode(',', $_POST['newcat']);
112         $x = new WP_Ajax_Response();
113         foreach ( $names as $cat_name ) {
114                 $cat_name = trim($cat_name);
115                 if ( !$category_nicename = sanitize_title($cat_name) )
116                         die('0');
117                 if ( !$cat_id = category_exists( $cat_name ) )
118                         $cat_id = wp_create_category( $cat_name );
119                 $cat_name = wp_specialchars(stripslashes($cat_name));
120                 $x->add( array(
121                         'what' => 'category',
122                         'id' => $cat_id,
123                         'data' => "<li id='category-$cat_id'><label for='in-category-$cat_id' class='selectit'><input value='$cat_id' type='checkbox' checked='checked' name='post_category[]' id='in-category-$cat_id'/> $cat_name</label></li>"
124                 ) );
125         }
126         $x->send();
127         break;
128 case 'add-link-category' : // On the Fly
129         if ( !current_user_can( 'manage_categories' ) )
130                 die('-1');
131         $names = explode(',', $_POST['newcat']);
132         $x = new WP_Ajax_Response();
133         foreach ( $names as $cat_name ) {
134                 $cat_name = trim($cat_name);
135                 if ( !$slug = sanitize_title($cat_name) )
136                         die('0');
137                 if ( !$cat_id = is_term( $cat_name, 'link_category' ) ) {
138                         $cat_id = wp_insert_term( $cat_name, 'link_category' );
139                         $cat_id = $cat_id['term_id'];
140                 }
141                 $cat_name = wp_specialchars(stripslashes($cat_name));
142                 $x->add( array(
143                         'what' => 'link-category',
144                         'id' => $cat_id,
145                         'data' => "<li id='link-category-$cat_id'><label for='in-link-category-$cat_id' class='selectit'><input value='$cat_id' type='checkbox' checked='checked' name='link_category[]' id='in-link-category-$cat_id'/> $cat_name</label></li>"
146                 ) );
147         }
148         $x->send();
149         break;
150 case 'add-cat' : // From Manage->Categories
151         if ( !current_user_can( 'manage_categories' ) )
152                 die('-1');
153         if ( !$cat = wp_insert_category( $_POST ) )
154                 die('0');
155         if ( !$cat = get_category( $cat ) )
156                 die('0');
157         $level = 0;
158         $cat_full_name = $cat->cat_name;
159         $_cat = $cat;
160         while ( $_cat->category_parent ) {
161                 $_cat = get_category( $_cat->category_parent );
162                 $cat_full_name = $_cat->cat_name . ' &#8212; ' . $cat_full_name;
163                 $level++;
164         }
165         $cat_full_name = attribute_escape($cat_full_name);
166
167         $x = new WP_Ajax_Response( array(
168                 'what' => 'cat',
169                 'id' => $cat->cat_ID,
170                 'data' => _cat_row( $cat, $level, $cat_full_name ),
171                 'supplemental' => array('name' => $cat_full_name, 'show-link' => sprintf(__( 'Category <a href="#%s">%s</a> added' ), "cat-$cat->cat_ID", $cat_full_name))
172         ) );
173         $x->send();
174         break;
175 case 'add-comment' :
176         if ( !current_user_can( 'edit_post', $id ) )
177                 die('-1');
178         $search = isset($_POST['s']) ? $_POST['s'] : false;
179         $start = isset($_POST['page']) ? intval($_POST['page']) * 25 : 25;
180
181         list($comments, $total) = _wp_get_comment_list( $search, $start, 1 );
182
183         if ( !$comments )
184                 die('1');
185         $x = new WP_Ajax_Response();
186         foreach ( (array) $comments as $comment ) {
187                 get_comment( $comment );
188                 ob_start();
189                         _wp_comment_list_item( $comment->comment_ID );
190                         $comment_list_item = ob_get_contents();
191                 ob_end_clean();
192                 $x->add( array(
193                         'what' => 'comment',
194                         'id' => $comment->comment_ID,
195                         'data' => $comment_list_item
196                 ) );
197         }
198         $x->send();
199         break;
200 case 'add-meta' :
201         if ( !current_user_can( 'edit_post', $id ) )
202                 die('-1');
203         if ( $id < 0 ) {
204                 $now = current_time('timestamp', 1);
205                 if ( $pid = wp_insert_post( array(
206                         'post_title' => sprintf('Draft created on %s at %s', date(get_option('date_format'), $now), date(get_option('time_format'), $now))
207                 ) ) ) {
208                         if ( is_wp_error( $pid ) )
209                                 return $pid;
210                         $mid = add_meta( $pid );
211                 }
212                 else
213                         die('0');
214         } else if ( !$mid = add_meta( $id ) ) {
215                 die('0');
216         }
217
218         $meta = get_post_meta_by_id( $mid );
219         $key = $meta->meta_key;
220         $value = $meta->meta_value;
221         $pid = (int) $meta->post_id;
222
223         $x = new WP_Ajax_Response( array(
224                 'what' => 'meta',
225                 'id' => $mid,
226                 'data' => wp_ajax_meta_row( $pid, $mid, $key, $value ),
227                 'supplemental' => array('postid' => $pid)
228         ) );
229         $x->send();
230         break;
231 case 'update-meta' :
232         $mid = (int) array_pop(array_keys($_POST['meta']));
233         $key = $_POST['meta'][$mid]['key'];
234         $value = $_POST['meta'][$mid]['value'];
235         if ( !$meta = get_post_meta_by_id( $mid ) )
236                 die('0'); // if meta doesn't exist
237         if ( !current_user_can( 'edit_post', $meta->post_id ) )
238                 die('-1');
239         if ( $u = update_meta( $mid, $key, $value ) ) {
240                 $key = stripslashes($key);
241                 $value = stripslashes($value);
242                 $x = new WP_Ajax_Response( array(
243                         'what' => 'meta',
244                         'id' => $mid,
245                         'data' => wp_ajax_meta_row( $meta->post_id, $mid, $key, $value ),
246                         'supplemental' => array('postid' => $meta->post_id)
247                 ) );
248                 $x->send();
249         }
250         die('1'); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems).
251         break;
252 case 'add-user' :
253         if ( !current_user_can('edit_users') )
254                 die('-1');
255         require_once(ABSPATH . WPINC . '/registration.php');
256         if ( !$user_id = add_user() )
257                 die('0');
258         elseif ( is_wp_error( $user_id ) ) {
259                 foreach( $user_id->get_error_messages() as $message )
260                         echo "<p>$message<p>";
261                 exit;
262         }
263         $user_object = new WP_User( $user_id );
264         $x = new WP_Ajax_Response( array(
265                 'what' => 'user',
266                 'id' => $user_id,
267                 'data' => user_row( $user_object ),
268                 'supplemental' => array('show-link' => sprintf(__( 'User <a href="#%s">%s</a> added' ), "user-$user_id", $user_object->user_login))
269         ) );
270         $x->send();
271         break;
272 case 'autosave' : // The name of this action is hardcoded in edit_post()
273         $_POST['post_content'] = $_POST['content'];
274         $_POST['post_excerpt'] = $_POST['excerpt'];
275         $_POST['post_status'] = 'draft';
276         $_POST['post_category'] = explode(",", $_POST['catslist']);
277         if($_POST['post_type'] == 'page' || empty($_POST['post_category']))
278                 unset($_POST['post_category']);
279
280         if($_POST['post_ID'] < 0) {
281                 $_POST['temp_ID'] = $_POST['post_ID'];
282                 $id = wp_write_post();
283                 if( is_wp_error($id) )
284                         die($id->get_error_message());
285                 else
286                         die("$id");
287         } else {
288                 $post_ID = (int) $_POST['post_ID'];
289                 $_POST['ID'] = $post_ID;
290                 $post = get_post($post_ID);
291                 if ( 'page' == $post->post_type ) {
292                         if ( !current_user_can('edit_page', $post_ID) )
293                                 die(__('You are not allowed to edit this page.'));
294                 } else {
295                         if ( !current_user_can('edit_post', $post_ID) )
296                                 die(__('You are not allowed to edit this post.'));
297                 }
298                 wp_update_post($_POST);
299         }
300         die('0');
301 break;
302 case 'autosave-generate-nonces' :
303         $ID = (int) $_POST['post_ID'];
304         if($_POST['post_type'] == 'post') {
305                 if(current_user_can('edit_post', $ID))
306                         die(wp_create_nonce('update-post_' . $ID));
307         }
308         if($_POST['post_type'] == 'page') {
309                 if(current_user_can('edit_page', $ID)) {
310                         die(wp_create_nonce('update-page_' . $ID));
311                 }
312         }
313         die('0');
314 break;
315 default :
316         do_action( 'wp_ajax_' . $_POST['action'] );
317         die('0');
318         break;
319 endswitch;
320 ?>