| 1 | <?php |
| 2 | |
| 3 | add_action('wp_ajax_fetch-list', 'wp_ajax_fetch_list'); |
| 4 | function wp_ajax_fetch_list() { |
| 5 | $list_class = $_GET['list_args']['class']; |
| 6 | check_ajax_referer( "fetch-list-$list_class", '_ajax_fetch_list_nonce' ); |
| 7 | |
| 8 | $current_screen = convert_to_screen( $_GET['list_args']['screen']['id'] ); |
| 9 | |
| 10 | define( 'WP_NETWORK_ADMIN', $current_screen->is_network ); |
| 11 | define( 'WP_USER_ADMIN', $current_screen->is_user ); |
| 12 | |
| 13 | $wp_list_table = _get_list_table( $list_class ); |
| 14 | if ( ! $wp_list_table ) |
| 15 | die( '0' ); |
| 16 | |
| 17 | if ( ! $wp_list_table->ajax_user_can() ) |
| 18 | die( '-1' ); |
| 19 | |
| 20 | $wp_list_table->ajax_response(); |
| 21 | |
| 22 | die( '0' ); |
| 23 | } |
| 24 | |
| 25 | add_action('wp_ajax_ajax-tag-search', 'wp_ajax_tag_search'); |
| 26 | function wp_ajax_tag_search() { |
| 27 | global $wpdb; |
| 28 | if ( isset( $_GET['tax'] ) ) { |
| 29 | $taxonomy = sanitize_key( $_GET['tax'] ); |
| 30 | $tax = get_taxonomy( $taxonomy ); |
| 31 | if ( ! $tax ) |
| 32 | die( '0' ); |
| 33 | if ( ! current_user_can( $tax->cap->assign_terms ) ) |
| 34 | die( '-1' ); |
| 35 | } else { |
| 36 | die('0'); |
| 37 | } |
| 38 | |
| 39 | $s = stripslashes( $_GET['q'] ); |
| 40 | |
| 41 | if ( false !== strpos( $s, ',' ) ) { |
| 42 | $s = explode( ',', $s ); |
| 43 | $s = $s[count( $s ) - 1]; |
| 44 | } |
| 45 | $s = trim( $s ); |
| 46 | if ( strlen( $s ) < 2 ) |
| 47 | die; // require 2 chars for matching |
| 48 | |
| 49 | $results = $wpdb->get_col( $wpdb->prepare( "SELECT t.name FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.name LIKE (%s)", $taxonomy, '%' . like_escape( $s ) . '%' ) ); |
| 50 | |
| 51 | echo join( $results, "\n" ); |
| 52 | die; |
| 53 | } |
| 54 | |
| 55 | add_action('wp_ajax_compression-test', 'wp_ajax_compression_test'); |
| 56 | function wp_ajax_compression_test() { |
| 57 | if ( !current_user_can( 'manage_options' ) ) |
| 58 | die('-1'); |
| 59 | |
| 60 | if ( ini_get('zlib.output_compression') || 'ob_gzhandler' == ini_get('output_handler') ) { |
| 61 | update_site_option('can_compress_scripts', 0); |
| 62 | die('0'); |
| 63 | } |
| 64 | |
| 65 | if ( isset($_GET['test']) ) { |
| 66 | header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' ); |
| 67 | header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' ); |
| 68 | header( 'Cache-Control: no-cache, must-revalidate, max-age=0' ); |
| 69 | header( 'Pragma: no-cache' ); |
| 70 | header('Content-Type: application/x-javascript; charset=UTF-8'); |
| 71 | $force_gzip = ( defined('ENFORCE_GZIP') && ENFORCE_GZIP ); |
| 72 | $test_str = '"wpCompressionTest Lorem ipsum dolor sit amet consectetuer mollis sapien urna ut a. Eu nonummy condimentum fringilla tempor pretium platea vel nibh netus Maecenas. Hac molestie amet justo quis pellentesque est ultrices interdum nibh Morbi. Cras mattis pretium Phasellus ante ipsum ipsum ut sociis Suspendisse Lorem. Ante et non molestie. Porta urna Vestibulum egestas id congue nibh eu risus gravida sit. Ac augue auctor Ut et non a elit massa id sodales. Elit eu Nulla at nibh adipiscing mattis lacus mauris at tempus. Netus nibh quis suscipit nec feugiat eget sed lorem et urna. Pellentesque lacus at ut massa consectetuer ligula ut auctor semper Pellentesque. Ut metus massa nibh quam Curabitur molestie nec mauris congue. Volutpat molestie elit justo facilisis neque ac risus Ut nascetur tristique. Vitae sit lorem tellus et quis Phasellus lacus tincidunt nunc Fusce. Pharetra wisi Suspendisse mus sagittis libero lacinia Integer consequat ac Phasellus. Et urna ac cursus tortor aliquam Aliquam amet tellus volutpat Vestibulum. Justo interdum condimentum In augue congue tellus sollicitudin Quisque quis nibh."'; |
| 73 | |
| 74 | if ( 1 == $_GET['test'] ) { |
| 75 | echo $test_str; |
| 76 | die; |
| 77 | } elseif ( 2 == $_GET['test'] ) { |
| 78 | if ( !isset($_SERVER['HTTP_ACCEPT_ENCODING']) ) |
| 79 | die('-1'); |
| 80 | if ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) { |
| 81 | header('Content-Encoding: deflate'); |
| 82 | $out = gzdeflate( $test_str, 1 ); |
| 83 | } elseif ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') && function_exists('gzencode') ) { |
| 84 | header('Content-Encoding: gzip'); |
| 85 | $out = gzencode( $test_str, 1 ); |
| 86 | } else { |
| 87 | die('-1'); |
| 88 | } |
| 89 | echo $out; |
| 90 | die; |
| 91 | } elseif ( 'no' == $_GET['test'] ) { |
| 92 | update_site_option('can_compress_scripts', 0); |
| 93 | } elseif ( 'yes' == $_GET['test'] ) { |
| 94 | update_site_option('can_compress_scripts', 1); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | die('0'); |
| 99 | } |
| 100 | |
| 101 | add_action('wp_ajax_imgedit-preview', 'wp_ajax_imgedit_preview'); |
| 102 | function wp_ajax_imgedit_preview() { |
| 103 | $post_id = intval($_GET['postid']); |
| 104 | if ( empty($post_id) || !current_user_can('edit_post', $post_id) ) |
| 105 | die('-1'); |
| 106 | |
| 107 | check_ajax_referer( "image_editor-$post_id" ); |
| 108 | |
| 109 | include_once( ABSPATH . 'wp-admin/includes/image-edit.php' ); |
| 110 | if ( ! stream_preview_image($post_id) ) |
| 111 | die('-1'); |
| 112 | |
| 113 | die(); |
| 114 | } |
| 115 | |
| 116 | add_action('wp_ajax_menu-quick-search', 'wp_ajax_menu_quick_search'); |
| 117 | function wp_ajax_menu_quick_search() { |
| 118 | if ( ! current_user_can( 'edit_theme_options' ) ) |
| 119 | die('-1'); |
| 120 | |
| 121 | require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; |
| 122 | |
| 123 | _wp_ajax_menu_quick_search( $_REQUEST ); |
| 124 | |
| 125 | exit; |
| 126 | } |
| 127 | |
| 128 | add_action('wp_ajax_oembed-cache', 'wp_oembed_cache'); |
| 129 | function wp_oembed_cache() { |
| 130 | global $wp_embed; |
| 131 | $return = ( $wp_embed->cache_oembed( $_GET['post'] ) ) ? '1' : '0'; |
| 132 | die( $return ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Sends back current comment total and new page links if they need to be updated. |
| 137 | * |
| 138 | * Contrary to normal success AJAX response ("1"), die with time() on success. |
| 139 | * |
| 140 | * @since 2.7 |
| 141 | * |
| 142 | * @param int $comment_id |
| 143 | * @return die |
| 144 | */ |
| 145 | function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) { |
| 146 | $total = (int) @$_POST['_total']; |
| 147 | $per_page = (int) @$_POST['_per_page']; |
| 148 | $page = (int) @$_POST['_page']; |
| 149 | $url = esc_url_raw( @$_POST['_url'] ); |
| 150 | // JS didn't send us everything we need to know. Just die with success message |
| 151 | if ( !$total || !$per_page || !$page || !$url ) |
| 152 | die( (string) time() ); |
| 153 | |
| 154 | $total += $delta; |
| 155 | if ( $total < 0 ) |
| 156 | $total = 0; |
| 157 | |
| 158 | // Only do the expensive stuff on a page-break, and about 1 other time per page |
| 159 | if ( 0 == $total % $per_page || 1 == mt_rand( 1, $per_page ) ) { |
| 160 | $post_id = 0; |
| 161 | $status = 'total_comments'; // What type of comment count are we looking for? |
| 162 | $parsed = parse_url( $url ); |
| 163 | if ( isset( $parsed['query'] ) ) { |
| 164 | parse_str( $parsed['query'], $query_vars ); |
| 165 | if ( !empty( $query_vars['comment_status'] ) ) |
| 166 | $status = $query_vars['comment_status']; |
| 167 | if ( !empty( $query_vars['p'] ) ) |
| 168 | $post_id = (int) $query_vars['p']; |
| 169 | } |
| 170 | |
| 171 | $comment_count = wp_count_comments($post_id); |
| 172 | |
| 173 | if ( isset( $comment_count->$status ) ) // We're looking for a known type of comment count |
| 174 | $total = $comment_count->$status; |
| 175 | // else use the decremented value from above |
| 176 | } |
| 177 | |
| 178 | $time = time(); // The time since the last comment count |
| 179 | |
| 180 | $x = new WP_Ajax_Response( array( |
| 181 | 'what' => 'comment', |
| 182 | 'id' => $comment_id, // here for completeness - not used |
| 183 | 'supplemental' => array( |
| 184 | 'total_items_i18n' => sprintf( _n( '1 item', '%s items', $total ), number_format_i18n( $total ) ), |
| 185 | 'total_pages' => ceil( $total / $per_page ), |
| 186 | 'total_pages_i18n' => number_format_i18n( ceil( $total / $per_page ) ), |
| 187 | 'total' => $total, |
| 188 | 'time' => $time |
| 189 | ) |
| 190 | ) ); |
| 191 | $x->send(); |
| 192 | } |
| 193 | |
| 194 | function _wp_ajax_add_hierarchical_term() { |
| 195 | $action = $_POST['action']; |
| 196 | $taxonomy = get_taxonomy(substr($action, 4)); |
| 197 | check_ajax_referer( $action, '_ajax_nonce-add-' . $taxonomy->name ); |
| 198 | if ( !current_user_can( $taxonomy->cap->edit_terms ) ) |
| 199 | die('-1'); |
| 200 | $names = explode(',', $_POST['new'.$taxonomy->name]); |
| 201 | $parent = isset($_POST['new'.$taxonomy->name.'_parent']) ? (int) $_POST['new'.$taxonomy->name.'_parent'] : 0; |
| 202 | if ( 0 > $parent ) |
| 203 | $parent = 0; |
| 204 | if ( $taxonomy->name == 'category' ) |
| 205 | $post_category = isset($_POST['post_category']) ? (array) $_POST['post_category'] : array(); |
| 206 | else |
| 207 | $post_category = ( isset($_POST['tax_input']) && isset($_POST['tax_input'][$taxonomy->name]) ) ? (array) $_POST['tax_input'][$taxonomy->name] : array(); |
| 208 | $checked_categories = array_map( 'absint', (array) $post_category ); |
| 209 | $popular_ids = wp_popular_terms_checklist($taxonomy->name, 0, 10, false); |
| 210 | |
| 211 | foreach ( $names as $cat_name ) { |
| 212 | $cat_name = trim($cat_name); |
| 213 | $category_nicename = sanitize_title($cat_name); |
| 214 | if ( '' === $category_nicename ) |
| 215 | continue; |
| 216 | if ( !($cat_id = term_exists($cat_name, $taxonomy->name, $parent)) ) { |
| 217 | $new_term = wp_insert_term($cat_name, $taxonomy->name, array('parent' => $parent)); |
| 218 | $cat_id = $new_term['term_id']; |
| 219 | } |
| 220 | $checked_categories[] = $cat_id; |
| 221 | if ( $parent ) // Do these all at once in a second |
| 222 | continue; |
| 223 | $category = get_term( $cat_id, $taxonomy->name ); |
| 224 | ob_start(); |
| 225 | wp_terms_checklist( 0, array( 'taxonomy' => $taxonomy->name, 'descendants_and_self' => $cat_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids )); |
| 226 | $data = ob_get_contents(); |
| 227 | ob_end_clean(); |
| 228 | $add = array( |
| 229 | 'what' => $taxonomy->name, |
| 230 | 'id' => $cat_id, |
| 231 | 'data' => str_replace( array("\n", "\t"), '', $data), |
| 232 | 'position' => -1 |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | if ( $parent ) { // Foncy - replace the parent and all its children |
| 237 | $parent = get_term( $parent, $taxonomy->name ); |
| 238 | $term_id = $parent->term_id; |
| 239 | |
| 240 | while ( $parent->parent ) { // get the top parent |
| 241 | $parent = &get_term( $parent->parent, $taxonomy->name ); |
| 242 | if ( is_wp_error( $parent ) ) |
| 243 | break; |
| 244 | $term_id = $parent->term_id; |
| 245 | } |
| 246 | |
| 247 | ob_start(); |
| 248 | wp_terms_checklist( 0, array('taxonomy' => $taxonomy->name, 'descendants_and_self' => $term_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids)); |
| 249 | $data = ob_get_contents(); |
| 250 | ob_end_clean(); |
| 251 | $add = array( |
| 252 | 'what' => $taxonomy->name, |
| 253 | 'id' => $term_id, |
| 254 | 'data' => str_replace( array("\n", "\t"), '', $data), |
| 255 | 'position' => -1 |
| 256 | ); |
| 257 | } |
| 258 | |
| 259 | ob_start(); |
| 260 | wp_dropdown_categories( array( |
| 261 | 'taxonomy' => $taxonomy->name, 'hide_empty' => 0, 'name' => 'new'.$taxonomy->name.'_parent', 'orderby' => 'name', |
| 262 | 'hierarchical' => 1, 'show_option_none' => '— '.$taxonomy->labels->parent_item.' —' |
| 263 | ) ); |
| 264 | $sup = ob_get_contents(); |
| 265 | ob_end_clean(); |
| 266 | $add['supplemental'] = array( 'newcat_parent' => $sup ); |
| 267 | |
| 268 | $x = new WP_Ajax_Response( $add ); |
| 269 | $x->send(); |
| 270 | } |
| 271 | |
| 272 | add_action('wp_ajax_image-editor', 'wp_ajax_image_editor'); |
| 273 | function wp_ajax_image_editor() { |
| 274 | $attachment_id = intval($_POST['postid']); |
| 275 | if ( empty($attachment_id) || !current_user_can('edit_post', $attachment_id) ) |
| 276 | die('-1'); |
| 277 | |
| 278 | check_ajax_referer( "image_editor-$attachment_id" ); |
| 279 | include_once( ABSPATH . 'wp-admin/includes/image-edit.php' ); |
| 280 | |
| 281 | $msg = false; |
| 282 | switch ( $_POST['do'] ) { |
| 283 | case 'save' : |
| 284 | $msg = wp_save_image($attachment_id); |
| 285 | $msg = json_encode($msg); |
| 286 | die($msg); |
| 287 | break; |
| 288 | case 'scale' : |
| 289 | $msg = wp_save_image($attachment_id); |
| 290 | break; |
| 291 | case 'restore' : |
| 292 | $msg = wp_restore_image($attachment_id); |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | wp_image_editor($attachment_id, $msg); |
| 297 | die(); |
| 298 | } |
| 299 | |
| 300 | add_action('wp_ajax_delete-comment', 'wp_ajax_delete_comment'); |
| 301 | /* |
| 302 | * On success, die with time() instead of 1 |
| 303 | */ |
| 304 | function wp_ajax_delete_comment() { |
| 305 | $id = isset($_POST['id'])? (int) $_POST['id'] : 0; |
| 306 | if ( !$comment = get_comment( $id ) ) |
| 307 | die( (string) time() ); |
| 308 | if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) |
| 309 | die('-1'); |
| 310 | |
| 311 | check_ajax_referer( "delete-comment_$id" ); |
| 312 | $status = wp_get_comment_status( $comment->comment_ID ); |
| 313 | |
| 314 | $delta = -1; |
| 315 | if ( isset($_POST['trash']) && 1 == $_POST['trash'] ) { |
| 316 | if ( 'trash' == $status ) |
| 317 | die( (string) time() ); |
| 318 | $r = wp_trash_comment( $comment->comment_ID ); |
| 319 | } elseif ( isset($_POST['untrash']) && 1 == $_POST['untrash'] ) { |
| 320 | if ( 'trash' != $status ) |
| 321 | die( (string) time() ); |
| 322 | $r = wp_untrash_comment( $comment->comment_ID ); |
| 323 | if ( ! isset( $_POST['comment_status'] ) || $_POST['comment_status'] != 'trash' ) // undo trash, not in trash |
| 324 | $delta = 1; |
| 325 | } elseif ( isset($_POST['spam']) && 1 == $_POST['spam'] ) { |
| 326 | if ( 'spam' == $status ) |
| 327 | die( (string) time() ); |
| 328 | $r = wp_spam_comment( $comment->comment_ID ); |
| 329 | } elseif ( isset($_POST['unspam']) && 1 == $_POST['unspam'] ) { |
| 330 | if ( 'spam' != $status ) |
| 331 | die( (string) time() ); |
| 332 | $r = wp_unspam_comment( $comment->comment_ID ); |
| 333 | if ( ! isset( $_POST['comment_status'] ) || $_POST['comment_status'] != 'spam' ) // undo spam, not in spam |
| 334 | $delta = 1; |
| 335 | } elseif ( isset($_POST['delete']) && 1 == $_POST['delete'] ) { |
| 336 | $r = wp_delete_comment( $comment->comment_ID ); |
| 337 | } else { |
| 338 | die('-1'); |
| 339 | } |
| 340 | |
| 341 | if ( $r ) // Decide if we need to send back '1' or a more complicated response including page links and comment counts |
| 342 | _wp_ajax_delete_comment_response( $comment->comment_ID, $delta ); |
| 343 | die( '0' ); |
| 344 | } |
| 345 | |
| 346 | add_action('wp_ajax_delete-tag', 'wp_ajax_delete_tag'); |
| 347 | function wp_ajax_delete_tag() { |
| 348 | $tag_id = (int) $_POST['tag_ID']; |
| 349 | check_ajax_referer( "delete-tag_$tag_id" ); |
| 350 | |
| 351 | $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag'; |
| 352 | $tax = get_taxonomy($taxonomy); |
| 353 | |
| 354 | if ( !current_user_can( $tax->cap->delete_terms ) ) |
| 355 | die('-1'); |
| 356 | |
| 357 | $tag = get_term( $tag_id, $taxonomy ); |
| 358 | if ( !$tag || is_wp_error( $tag ) ) |
| 359 | die('1'); |
| 360 | |
| 361 | if ( wp_delete_term($tag_id, $taxonomy)) |
| 362 | die('1'); |
| 363 | else |
| 364 | die('0'); |
| 365 | } |
| 366 | |
| 367 | add_action('wp_ajax_delete-link', 'wp_ajax_delete_link'); |
| 368 | function wp_ajax_delete_link() { |
| 369 | $id = isset($_POST['id'])? (int) $_POST['id'] : 0; |
| 370 | check_ajax_referer( "delete-bookmark_$id" ); |
| 371 | if ( !current_user_can( 'manage_links' ) ) |
| 372 | die('-1'); |
| 373 | |
| 374 | $link = get_bookmark( $id ); |
| 375 | if ( !$link || is_wp_error( $link ) ) |
| 376 | die('1'); |
| 377 | |
| 378 | if ( wp_delete_link( $id ) ) |
| 379 | die('1'); |
| 380 | else |
| 381 | die('0'); |
| 382 | } |
| 383 | |
| 384 | add_action('wp_ajax_delete-meta', 'wp_ajax_delete_meta'); |
| 385 | function wp_ajax_delete_meta() { |
| 386 | $id = isset($_POST['id'])? (int) $_POST['id'] : 0; |
| 387 | check_ajax_referer( "delete-meta_$id" ); |
| 388 | if ( !$meta = get_metadata_by_mid( 'post', $id ) ) |
| 389 | die('1'); |
| 390 | |
| 391 | if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $meta->post_id, $meta->meta_key ) ) |
| 392 | die('-1'); |
| 393 | if ( delete_meta( $meta->meta_id ) ) |
| 394 | die('1'); |
| 395 | die('0'); |
| 396 | } |
| 397 | |
| 398 | add_action('wp_ajax_delete-post', 'wp_ajax_delete_post'); |
| 399 | function wp_ajax_delete_post() { |
| 400 | $id = isset($_POST['id'])? (int) $_POST['id'] : 0; |
| 401 | $action = $_POST['action']; |
| 402 | check_ajax_referer( "{$action}_$id" ); |
| 403 | if ( !current_user_can( 'delete_post', $id ) ) |
| 404 | die('-1'); |
| 405 | |
| 406 | if ( !get_post( $id ) ) |
| 407 | die('1'); |
| 408 | |
| 409 | if ( wp_delete_post( $id ) ) |
| 410 | die('1'); |
| 411 | else |
| 412 | die('0'); |
| 413 | } |
| 414 | |
| 415 | add_action('wp_ajax_trash-post', 'wp_ajax_trash_post'); |
| 416 | function wp_ajax_trash_post() { |
| 417 | $id = isset($_POST['id'])? (int) $_POST['id'] : 0; |
| 418 | $action = $_POST['action']; |
| 419 | check_ajax_referer( "{$action}_$id" ); |
| 420 | if ( !current_user_can( 'delete_post', $id ) ) |
| 421 | die('-1'); |
| 422 | |
| 423 | if ( !get_post( $id ) ) |
| 424 | die('1'); |
| 425 | |
| 426 | $done = wp_trash_post( $id ); |
| 427 | if ( $done ) |
| 428 | die('1'); |
| 429 | |
| 430 | die('0'); |
| 431 | } |
| 432 | |
| 433 | add_action('wp_ajax_untrash-post', 'wp_ajax_untrash_post'); |
| 434 | function wp_ajax_untrash_post() { |
| 435 | $id = isset($_POST['id'])? (int) $_POST['id'] : 0; |
| 436 | $action = $_POST['action']; |
| 437 | check_ajax_referer( "{$action}_$id" ); |
| 438 | if ( !current_user_can( 'delete_post', $id ) ) |
| 439 | die('-1'); |
| 440 | |
| 441 | if ( !get_post( $id ) ) |
| 442 | die('1'); |
| 443 | |
| 444 | $done = wp_untrash_post( $id ); |
| 445 | |
| 446 | if ( $done ) |
| 447 | die('1'); |
| 448 | |
| 449 | die('0'); |
| 450 | } |
| 451 | |
| 452 | add_action('wp_ajax_delete-page', 'wp_ajax_delete_page'); |
| 453 | function wp_ajax_delete_page() { |
| 454 | $id = isset($_POST['id'])? (int) $_POST['id'] : 0; |
| 455 | $action = $_POST['action']; |
| 456 | check_ajax_referer( "{$action}_$id" ); |
| 457 | if ( !current_user_can( 'delete_page', $id ) ) |
| 458 | die('-1'); |
| 459 | |
| 460 | if ( !get_page( $id ) ) |
| 461 | die('1'); |
| 462 | |
| 463 | if ( wp_delete_post( $id ) ) |
| 464 | die('1'); |
| 465 | else |
| 466 | die('0'); |
| 467 | } |
| 468 | |
| 469 | add_action('wp_ajax_dim-comment', 'wp_ajax_dim_comment'); |
| 470 | function wp_ajax_dim_comment() { |
| 471 | $id = isset($_POST['id'])? (int) $_POST['id'] : 0; |
| 472 | if ( !$comment = get_comment( $id ) ) { |
| 473 | $x = new WP_Ajax_Response( array( |
| 474 | 'what' => 'comment', |
| 475 | 'id' => new WP_Error('invalid_comment', sprintf(__('Comment %d does not exist'), $id)) |
| 476 | ) ); |
| 477 | $x->send(); |
| 478 | } |
| 479 | |
| 480 | if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && ! current_user_can( 'moderate_comments' ) ) |
| 481 | die('-1'); |
| 482 | |
| 483 | $current = wp_get_comment_status( $comment->comment_ID ); |
| 484 | if ( $_POST['new'] == $current ) |
| 485 | die( (string) time() ); |
| 486 | |
| 487 | check_ajax_referer( "approve-comment_$id" ); |
| 488 | if ( in_array( $current, array( 'unapproved', 'spam' ) ) ) |
| 489 | $result = wp_set_comment_status( $comment->comment_ID, 'approve', true ); |
| 490 | else |
| 491 | $result = wp_set_comment_status( $comment->comment_ID, 'hold', true ); |
| 492 | |
| 493 | if ( is_wp_error($result) ) { |
| 494 | $x = new WP_Ajax_Response( array( |
| 495 | 'what' => 'comment', |
| 496 | 'id' => $result |
| 497 | ) ); |
| 498 | $x->send(); |
| 499 | } |
| 500 | |
| 501 | // Decide if we need to send back '1' or a more complicated response including page links and comment counts |
| 502 | _wp_ajax_delete_comment_response( $comment->comment_ID ); |
| 503 | die( '0' ); |
| 504 | } |
| 505 | |
| 506 | add_action('wp_ajax_add-link-category', 'wp_ajax_add_link_category'); |
| 507 | function wp_ajax_add_link_category() { |
| 508 | $action = $_POST['action']; |
| 509 | check_ajax_referer( $action ); |
| 510 | if ( !current_user_can( 'manage_categories' ) ) |
| 511 | die('-1'); |
| 512 | $names = explode(',', $_POST['newcat']); |
| 513 | $x = new WP_Ajax_Response(); |
| 514 | foreach ( $names as $cat_name ) { |
| 515 | $cat_name = trim($cat_name); |
| 516 | $slug = sanitize_title($cat_name); |
| 517 | if ( '' === $slug ) |
| 518 | continue; |
| 519 | if ( !$cat_id = term_exists( $cat_name, 'link_category' ) ) { |
| 520 | $cat_id = wp_insert_term( $cat_name, 'link_category' ); |
| 521 | } |
| 522 | $cat_id = $cat_id['term_id']; |
| 523 | $cat_name = esc_html(stripslashes($cat_name)); |
| 524 | $x->add( array( |
| 525 | 'what' => 'link-category', |
| 526 | 'id' => $cat_id, |
| 527 | 'data' => "<li id='link-category-$cat_id'><label for='in-link-category-$cat_id' class='selectit'><input value='" . esc_attr($cat_id) . "' type='checkbox' checked='checked' name='link_category[]' id='in-link-category-$cat_id'/> $cat_name</label></li>", |
| 528 | 'position' => -1 |
| 529 | ) ); |
| 530 | } |
| 531 | $x->send(); |
| 532 | } |
| 533 | |
| 534 | add_action('wp_ajax_add-tag', 'wp_ajax_add_tag'); |
| 535 | function wp_ajax_add_tag() { |
| 536 | check_ajax_referer( 'add-tag', '_wpnonce_add-tag' ); |
| 537 | $post_type = !empty($_POST['post_type']) ? $_POST['post_type'] : 'post'; |
| 538 | $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag'; |
| 539 | $tax = get_taxonomy($taxonomy); |
| 540 | |
| 541 | if ( !current_user_can( $tax->cap->edit_terms ) ) |
| 542 | die('-1'); |
| 543 | |
| 544 | $x = new WP_Ajax_Response(); |
| 545 | |
| 546 | $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST ); |
| 547 | |
| 548 | if ( !$tag || is_wp_error($tag) || (!$tag = get_term( $tag['term_id'], $taxonomy )) ) { |
| 549 | $message = __('An error has occurred. Please reload the page and try again.'); |
| 550 | if ( is_wp_error($tag) && $tag->get_error_message() ) |
| 551 | $message = $tag->get_error_message(); |
| 552 | |
| 553 | $x->add( array( |
| 554 | 'what' => 'taxonomy', |
| 555 | 'data' => new WP_Error('error', $message ) |
| 556 | ) ); |
| 557 | $x->send(); |
| 558 | } |
| 559 | |
| 560 | set_current_screen( $_POST['screen'] ); |
| 561 | |
| 562 | $wp_list_table = _get_list_table('WP_Terms_List_Table'); |
| 563 | |
| 564 | $level = 0; |
| 565 | if ( is_taxonomy_hierarchical($taxonomy) ) { |
| 566 | $level = count( get_ancestors( $tag->term_id, $taxonomy ) ); |
| 567 | ob_start(); |
| 568 | $wp_list_table->single_row( $tag, $level ); |
| 569 | $noparents = ob_get_clean(); |
| 570 | } |
| 571 | |
| 572 | ob_start(); |
| 573 | $wp_list_table->single_row( $tag ); |
| 574 | $parents = ob_get_clean(); |
| 575 | |
| 576 | $x->add( array( |
| 577 | 'what' => 'taxonomy', |
| 578 | 'supplemental' => compact('parents', 'noparents') |
| 579 | ) ); |
| 580 | $x->add( array( |
| 581 | 'what' => 'term', |
| 582 | 'position' => $level, |
| 583 | 'supplemental' => (array) $tag |
| 584 | ) ); |
| 585 | $x->send(); |
| 586 | } |
| 587 | |
| 588 | add_action('wp_ajax_get-tagcloud', 'wp_ajax_get-tagcloud'); |
| 589 | function wp_ajax_get_tagcloud() { |
| 590 | if ( isset( $_POST['tax'] ) ) { |
| 591 | $taxonomy = sanitize_key( $_POST['tax'] ); |
| 592 | $tax = get_taxonomy( $taxonomy ); |
| 593 | if ( ! $tax ) |
| 594 | die( '0' ); |
| 595 | if ( ! current_user_can( $tax->cap->assign_terms ) ) |
| 596 | die( '-1' ); |
| 597 | } else { |
| 598 | die('0'); |
| 599 | } |
| 600 | |
| 601 | $tags = get_terms( $taxonomy, array( 'number' => 45, 'orderby' => 'count', 'order' => 'DESC' ) ); |
| 602 | |
| 603 | if ( empty( $tags ) ) |
| 604 | die( isset( $tax->no_tagcloud ) ? $tax->no_tagcloud : __('No tags found!') ); |
| 605 | |
| 606 | if ( is_wp_error( $tags ) ) |
| 607 | die( $tags->get_error_message() ); |
| 608 | |
| 609 | foreach ( $tags as $key => $tag ) { |
| 610 | $tags[ $key ]->link = '#'; |
| 611 | $tags[ $key ]->id = $tag->term_id; |
| 612 | } |
| 613 | |
| 614 | // We need raw tag names here, so don't filter the output |
| 615 | $return = wp_generate_tag_cloud( $tags, array('filter' => 0) ); |
| 616 | |
| 617 | if ( empty($return) ) |
| 618 | die('0'); |
| 619 | |
| 620 | echo $return; |
| 621 | |
| 622 | exit; |
| 623 | } |
| 624 | |
| 625 | add_action('wp_ajax_get-comments', 'wp_ajax_get_comments'); |
| 626 | function wp_ajax_get_comments() { |
| 627 | global $post_id; |
| 628 | $action = $_POST['action']; |
| 629 | check_ajax_referer( $action ); |
| 630 | |
| 631 | set_current_screen( 'edit-comments' ); |
| 632 | |
| 633 | $wp_list_table = _get_list_table('WP_Post_Comments_List_Table'); |
| 634 | |
| 635 | if ( !current_user_can( 'edit_post', $post_id ) ) |
| 636 | die('-1'); |
| 637 | |
| 638 | $wp_list_table->prepare_items(); |
| 639 | |
| 640 | if ( !$wp_list_table->has_items() ) |
| 641 | die('1'); |
| 642 | |
| 643 | $x = new WP_Ajax_Response(); |
| 644 | ob_start(); |
| 645 | foreach ( $wp_list_table->items as $comment ) { |
| 646 | if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) |
| 647 | continue; |
| 648 | get_comment( $comment ); |
| 649 | $wp_list_table->single_row( $comment ); |
| 650 | } |
| 651 | $comment_list_item = ob_get_contents(); |
| 652 | ob_end_clean(); |
| 653 | |
| 654 | $x->add( array( |
| 655 | 'what' => 'comments', |
| 656 | 'data' => $comment_list_item |
| 657 | ) ); |
| 658 | $x->send(); |
| 659 | } |
| 660 | |
| 661 | add_action('wp_ajax_replyto-comment', 'wp_ajax_replyto_comment'); |
| 662 | function wp_ajax_replyto_comment() { |
| 663 | $action = $_POST['action']; |
| 664 | check_ajax_referer( $action, '_ajax_nonce-replyto-comment' ); |
| 665 | |
| 666 | set_current_screen( 'edit-comments' ); |
| 667 | |
| 668 | $comment_post_ID = (int) $_POST['comment_post_ID']; |
| 669 | if ( !current_user_can( 'edit_post', $comment_post_ID ) ) |
| 670 | die('-1'); |
| 671 | |
| 672 | $status = $wpdb->get_var( $wpdb->prepare("SELECT post_status FROM $wpdb->posts WHERE ID = %d", $comment_post_ID) ); |
| 673 | |
| 674 | if ( empty($status) ) |
| 675 | die('1'); |
| 676 | elseif ( in_array($status, array('draft', 'pending', 'trash') ) ) |
| 677 | die( __('ERROR: you are replying to a comment on a draft post.') ); |
| 678 | |
| 679 | $user = wp_get_current_user(); |
| 680 | if ( $user->ID ) { |
| 681 | $comment_author = $wpdb->escape($user->display_name); |
| 682 | $comment_author_email = $wpdb->escape($user->user_email); |
| 683 | $comment_author_url = $wpdb->escape($user->user_url); |
| 684 | $comment_content = trim($_POST['content']); |
| 685 | if ( current_user_can( 'unfiltered_html' ) ) { |
| 686 | if ( wp_create_nonce( 'unfiltered-html-comment' ) != $_POST['_wp_unfiltered_html_comment'] ) { |
| 687 | kses_remove_filters(); // start with a clean slate |
| 688 | kses_init_filters(); // set up the filters |
| 689 | } |
| 690 | } |
| 691 | } else { |
| 692 | die( __('Sorry, you must be logged in to reply to a comment.') ); |
| 693 | } |
| 694 | |
| 695 | if ( '' == $comment_content ) |
| 696 | die( __('ERROR: please type a comment.') ); |
| 697 | |
| 698 | $comment_parent = absint($_POST['comment_ID']); |
| 699 | $comment_auto_approved = false; |
| 700 | $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID'); |
| 701 | |
| 702 | $comment_id = wp_new_comment( $commentdata ); |
| 703 | $comment = get_comment($comment_id); |
| 704 | if ( ! $comment ) die('1'); |
| 705 | |
| 706 | $position = ( isset($_POST['position']) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1'; |
| 707 | |
| 708 | // automatically approve parent comment |
| 709 | if ( !empty($_POST['approve_parent']) ) { |
| 710 | $parent = get_comment( $comment_parent ); |
| 711 | |
| 712 | if ( $parent && $parent->comment_approved === '0' && $parent->comment_post_ID == $comment_post_ID ) { |
| 713 | if ( wp_set_comment_status( $parent->comment_ID, 'approve' ) ) |
| 714 | $comment_auto_approved = true; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | ob_start(); |
| 719 | if ( 'dashboard' == $_REQUEST['mode'] ) { |
| 720 | require_once( ABSPATH . 'wp-admin/includes/dashboard.php' ); |
| 721 | _wp_dashboard_recent_comments_row( $comment ); |
| 722 | } else { |
| 723 | if ( 'single' == $_REQUEST['mode'] ) { |
| 724 | $wp_list_table = _get_list_table('WP_Post_Comments_List_Table'); |
| 725 | } else { |
| 726 | $wp_list_table = _get_list_table('WP_Comments_List_Table'); |
| 727 | } |
| 728 | $wp_list_table->single_row( $comment ); |
| 729 | } |
| 730 | $comment_list_item = ob_get_contents(); |
| 731 | ob_end_clean(); |
| 732 | |
| 733 | $response = array( |
| 734 | 'what' => 'comment', |
| 735 | 'id' => $comment->comment_ID, |
| 736 | 'data' => $comment_list_item, |
| 737 | 'position' => $position |
| 738 | ); |
| 739 | |
| 740 | if ( $comment_auto_approved ) |
| 741 | $response['supplemental'] = array( 'parent_approved' => $parent->comment_ID ); |
| 742 | |
| 743 | $x = new WP_Ajax_Response(); |
| 744 | $x->add( $response ); |
| 745 | $x->send(); |
| 746 | } |
| 747 | |
| 748 | add_action('wp_ajax_edit-comment', 'wp_ajax_edit_comment'); |
| 749 | function wp_ajax_edit_comment() { |
| 750 | global $comment; |
| 751 | check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' ); |
| 752 | |
| 753 | set_current_screen( 'edit-comments' ); |
| 754 | |
| 755 | $comment_id = (int) $_POST['comment_ID']; |
| 756 | if ( ! current_user_can( 'edit_comment', $comment_id ) ) |
| 757 | die('-1'); |
| 758 | |
| 759 | if ( '' == $_POST['content'] ) |
| 760 | die( __('ERROR: please type a comment.') ); |
| 761 | |
| 762 | $_POST['comment_status'] = $_POST['status']; |
| 763 | edit_comment(); |
| 764 | |
| 765 | $position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1'; |
| 766 | $comments_status = isset($_POST['comments_listing']) ? $_POST['comments_listing'] : ''; |
| 767 | |
| 768 | $checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0; |
| 769 | $wp_list_table = _get_list_table( $checkbox ? 'WP_Comments_List_Table' : 'WP_Post_Comments_List_Table' ); |
| 770 | |
| 771 | ob_start(); |
| 772 | $wp_list_table->single_row( get_comment( $comment_id ) ); |
| 773 | $comment_list_item = ob_get_contents(); |
| 774 | ob_end_clean(); |
| 775 | |
| 776 | $x = new WP_Ajax_Response(); |
| 777 | |
| 778 | $x->add( array( |
| 779 | 'what' => 'edit_comment', |
| 780 | 'id' => $comment->comment_ID, |
| 781 | 'data' => $comment_list_item, |
| 782 | 'position' => $position |
| 783 | )); |
| 784 | |
| 785 | $x->send(); |
| 786 | } |
| 787 | |
| 788 | add_action('wp_ajax_add-menu-item', 'wp_ajax_add_menu_item'); |
| 789 | function wp_ajax_add_menu_item() { |
| 790 | if ( ! current_user_can( 'edit_theme_options' ) ) |
| 791 | die('-1'); |
| 792 | |
| 793 | check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' ); |
| 794 | |
| 795 | require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; |
| 796 | |
| 797 | // For performance reasons, we omit some object properties from the checklist. |
| 798 | // The following is a hacky way to restore them when adding non-custom items. |
| 799 | |
| 800 | $menu_items_data = array(); |
| 801 | foreach ( (array) $_POST['menu-item'] as $menu_item_data ) { |
| 802 | if ( |
| 803 | ! empty( $menu_item_data['menu-item-type'] ) && |
| 804 | 'custom' != $menu_item_data['menu-item-type'] && |
| 805 | ! empty( $menu_item_data['menu-item-object-id'] ) |
| 806 | ) { |
| 807 | switch( $menu_item_data['menu-item-type'] ) { |
| 808 | case 'post_type' : |
| 809 | $_object = get_post( $menu_item_data['menu-item-object-id'] ); |
| 810 | break; |
| 811 | |
| 812 | case 'taxonomy' : |
| 813 | $_object = get_term( $menu_item_data['menu-item-object-id'], $menu_item_data['menu-item-object'] ); |
| 814 | break; |
| 815 | } |
| 816 | |
| 817 | $_menu_items = array_map( 'wp_setup_nav_menu_item', array( $_object ) ); |
| 818 | $_menu_item = array_shift( $_menu_items ); |
| 819 | |
| 820 | // Restore the missing menu item properties |
| 821 | $menu_item_data['menu-item-description'] = $_menu_item->description; |
| 822 | } |
| 823 | |
| 824 | $menu_items_data[] = $menu_item_data; |
| 825 | } |
| 826 | |
| 827 | $item_ids = wp_save_nav_menu_items( 0, $menu_items_data ); |
| 828 | if ( is_wp_error( $item_ids ) ) |
| 829 | die('-1'); |
| 830 | |
| 831 | foreach ( (array) $item_ids as $menu_item_id ) { |
| 832 | $menu_obj = get_post( $menu_item_id ); |
| 833 | if ( ! empty( $menu_obj->ID ) ) { |
| 834 | $menu_obj = wp_setup_nav_menu_item( $menu_obj ); |
| 835 | $menu_obj->label = $menu_obj->title; // don't show "(pending)" in ajax-added items |
| 836 | $menu_items[] = $menu_obj; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | if ( ! empty( $menu_items ) ) { |
| 841 | $args = array( |
| 842 | 'after' => '', |
| 843 | 'before' => '', |
| 844 | 'link_after' => '', |
| 845 | 'link_before' => '', |
| 846 | 'walker' => new Walker_Nav_Menu_Edit, |
| 847 | ); |
| 848 | echo walk_nav_menu_tree( $menu_items, 0, (object) $args ); |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | add_action('wp_ajax_add-meta', 'wp_ajax_add_meta'); |
| 853 | function wp_ajax_add_meta() { |
| 854 | check_ajax_referer( 'add-meta', '_ajax_nonce-add-meta' ); |
| 855 | $c = 0; |
| 856 | $pid = (int) $_POST['post_id']; |
| 857 | $post = get_post( $pid ); |
| 858 | |
| 859 | if ( isset($_POST['metakeyselect']) || isset($_POST['metakeyinput']) ) { |
| 860 | if ( !current_user_can( 'edit_post', $pid ) ) |
| 861 | die('-1'); |
| 862 | if ( isset($_POST['metakeyselect']) && '#NONE#' == $_POST['metakeyselect'] && empty($_POST['metakeyinput']) ) |
| 863 | die('1'); |
| 864 | if ( $post->post_status == 'auto-draft' ) { |
| 865 | $save_POST = $_POST; // Backup $_POST |
| 866 | $_POST = array(); // Make it empty for edit_post() |
| 867 | $_POST['action'] = 'draft'; // Warning fix |
| 868 | $_POST['post_ID'] = $pid; |
| 869 | $_POST['post_type'] = $post->post_type; |
| 870 | $_POST['post_status'] = 'draft'; |
| 871 | $now = current_time('timestamp', 1); |
| 872 | $_POST['post_title'] = sprintf('Draft created on %s at %s', date(get_option('date_format'), $now), date(get_option('time_format'), $now)); |
| 873 | |
| 874 | if ( $pid = edit_post() ) { |
| 875 | if ( is_wp_error( $pid ) ) { |
| 876 | $x = new WP_Ajax_Response( array( |
| 877 | 'what' => 'meta', |
| 878 | 'data' => $pid |
| 879 | ) ); |
| 880 | $x->send(); |
| 881 | } |
| 882 | $_POST = $save_POST; // Now we can restore original $_POST again |
| 883 | if ( !$mid = add_meta( $pid ) ) |
| 884 | die(__('Please provide a custom field value.')); |
| 885 | } else { |
| 886 | die('0'); |
| 887 | } |
| 888 | } else if ( !$mid = add_meta( $pid ) ) { |
| 889 | die(__('Please provide a custom field value.')); |
| 890 | } |
| 891 | |
| 892 | $meta = get_metadata_by_mid( 'post', $mid ); |
| 893 | $pid = (int) $meta->post_id; |
| 894 | $meta = get_object_vars( $meta ); |
| 895 | $x = new WP_Ajax_Response( array( |
| 896 | 'what' => 'meta', |
| 897 | 'id' => $mid, |
| 898 | 'data' => _list_meta_row( $meta, $c ), |
| 899 | 'position' => 1, |
| 900 | 'supplemental' => array('postid' => $pid) |
| 901 | ) ); |
| 902 | } else { // Update? |
| 903 | $mid = (int) key( $_POST['meta'] ); |
| 904 | $key = stripslashes( $_POST['meta'][$mid]['key'] ); |
| 905 | $value = stripslashes( $_POST['meta'][$mid]['value'] ); |
| 906 | if ( '' == trim($key) ) |
| 907 | die(__('Please provide a custom field name.')); |
| 908 | if ( '' == trim($value) ) |
| 909 | die(__('Please provide a custom field value.')); |
| 910 | if ( ! $meta = get_metadata_by_mid( 'post', $mid ) ) |
| 911 | die('0'); // if meta doesn't exist |
| 912 | if ( is_protected_meta( $meta->meta_key, 'post' ) || is_protected_meta( $key, 'post' ) || |
| 913 | ! current_user_can( 'edit_post_meta', $meta->post_id, $meta->meta_key ) || |
| 914 | ! current_user_can( 'edit_post_meta', $meta->post_id, $key ) ) |
| 915 | die('-1'); |
| 916 | if ( $meta->meta_value != $value || $meta->meta_key != $key ) { |
| 917 | if ( !$u = update_metadata_by_mid( 'post', $mid, $value, $key ) ) |
| 918 | die('0'); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems). |
| 919 | } |
| 920 | |
| 921 | $x = new WP_Ajax_Response( array( |
| 922 | 'what' => 'meta', |
| 923 | 'id' => $mid, 'old_id' => $mid, |
| 924 | 'data' => _list_meta_row( array( |
| 925 | 'meta_key' => $key, |
| 926 | 'meta_value' => $value, |
| 927 | 'meta_id' => $mid |
| 928 | ), $c ), |
| 929 | 'position' => 0, |
| 930 | 'supplemental' => array('postid' => $meta->post_id) |
| 931 | ) ); |
| 932 | } |
| 933 | $x->send(); |
| 934 | } |
| 935 | |
| 936 | add_action('wp_ajax_add-user', 'wp_ajax_add_user'); |
| 937 | function wp_ajax_add_user() { |
| 938 | $action = $_POST['action']; |
| 939 | check_ajax_referer( $action ); |
| 940 | if ( ! current_user_can('create_users') ) |
| 941 | die('-1'); |
| 942 | if ( ! $user_id = edit_user() ) { |
| 943 | die('0'); |
| 944 | } elseif ( is_wp_error( $user_id ) ) { |
| 945 | $x = new WP_Ajax_Response( array( |
| 946 | 'what' => 'user', |
| 947 | 'id' => $user_id |
| 948 | ) ); |
| 949 | $x->send(); |
| 950 | } |
| 951 | $user_object = new WP_User( $user_id ); |
| 952 | |
| 953 | $wp_list_table = _get_list_table('WP_Users_List_Table'); |
| 954 | |
| 955 | $x = new WP_Ajax_Response( array( |
| 956 | 'what' => 'user', |
| 957 | 'id' => $user_id, |
| 958 | 'data' => $wp_list_table->single_row( $user_object, '', $user_object->roles[0] ), |
| 959 | 'supplemental' => array( |
| 960 | 'show-link' => sprintf(__( 'User <a href="#%s">%s</a> added' ), "user-$user_id", $user_object->user_login), |
| 961 | 'role' => $user_object->roles[0] |
| 962 | ) |
| 963 | ) ); |
| 964 | $x->send(); |
| 965 | } |
| 966 | |
| 967 | add_action('wp_ajax_nopriv_autosave', 'wp_ajax_nopriv_autosave'); |
| 968 | function wp_ajax_nopriv_autosave() { |
| 969 | $id = isset($_POST['post_ID'])? (int) $_POST['post_ID'] : 0; |
| 970 | |
| 971 | if ( ! $id ) |
| 972 | die('-1'); |
| 973 | |
| 974 | $message = sprintf( __('<strong>ALERT: You are logged out!</strong> Could not save draft. <a href="%s" target="_blank">Please log in again.</a>'), wp_login_url() ); |
| 975 | $x = new WP_Ajax_Response( array( |
| 976 | 'what' => 'autosave', |
| 977 | 'id' => $id, |
| 978 | 'data' => $message |
| 979 | ) ); |
| 980 | $x->send(); |
| 981 | } |
| 982 | |
| 983 | add_action('wp_ajax_autosave', 'wp_ajax_autosave'); |
| 984 | /** |
| 985 | * The name of this action is hardcoded in edit_post() |
| 986 | */ |
| 987 | function wp_ajax_autosave() { |
| 988 | global $login_grace_period; |
| 989 | define( 'DOING_AUTOSAVE', true ); |
| 990 | |
| 991 | $nonce_age = check_ajax_referer( 'autosave', 'autosavenonce' ); |
| 992 | |
| 993 | $_POST['post_category'] = explode(",", $_POST['catslist']); |
| 994 | if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) ) |
| 995 | unset($_POST['post_category']); |
| 996 | |
| 997 | $do_autosave = (bool) $_POST['autosave']; |
| 998 | $do_lock = true; |
| 999 | |
| 1000 | $data = $alert = ''; |
| 1001 | /* translators: draft saved date format, see http://php.net/date */ |
| 1002 | $draft_saved_date_format = __('g:i:s a'); |
| 1003 | /* translators: %s: date and time */ |
| 1004 | $message = sprintf( __('Draft saved at %s.'), date_i18n( $draft_saved_date_format ) ); |
| 1005 | |
| 1006 | $supplemental = array(); |
| 1007 | if ( isset($login_grace_period) ) |
| 1008 | $alert .= sprintf( __('Your login has expired. Please open a new browser window and <a href="%s" target="_blank">log in again</a>. '), add_query_arg( 'interim-login', 1, wp_login_url() ) ); |
| 1009 | |
| 1010 | $id = $revision_id = 0; |
| 1011 | |
| 1012 | $post_ID = (int) $_POST['post_ID']; |
| 1013 | $_POST['ID'] = $post_ID; |
| 1014 | $post = get_post($post_ID); |
| 1015 | if ( 'auto-draft' == $post->post_status ) |
| 1016 | $_POST['post_status'] = 'draft'; |
| 1017 | |
| 1018 | if ( $last = wp_check_post_lock( $post->ID ) ) { |
| 1019 | $do_autosave = $do_lock = false; |
| 1020 | |
| 1021 | $last_user = get_userdata( $last ); |
| 1022 | $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' ); |
| 1023 | $data = __( 'Autosave disabled.' ); |
| 1024 | |
| 1025 | $supplemental['disable_autosave'] = 'disable'; |
| 1026 | $alert .= sprintf( __( '%s is currently editing this article. If you update it, you will overwrite the changes.' ), esc_html( $last_user_name ) ); |
| 1027 | } |
| 1028 | |
| 1029 | if ( 'page' == $post->post_type ) { |
| 1030 | if ( !current_user_can('edit_page', $post_ID) ) |
| 1031 | die(__('You are not allowed to edit this page.')); |
| 1032 | } else { |
| 1033 | if ( !current_user_can('edit_post', $post_ID) ) |
| 1034 | die(__('You are not allowed to edit this post.')); |
| 1035 | } |
| 1036 | |
| 1037 | if ( $do_autosave ) { |
| 1038 | // Drafts and auto-drafts are just overwritten by autosave |
| 1039 | if ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) { |
| 1040 | $id = edit_post(); |
| 1041 | } else { // Non drafts are not overwritten. The autosave is stored in a special post revision. |
| 1042 | $revision_id = wp_create_post_autosave( $post->ID ); |
| 1043 | if ( is_wp_error($revision_id) ) |
| 1044 | $id = $revision_id; |
| 1045 | else |
| 1046 | $id = $post->ID; |
| 1047 | } |
| 1048 | $data = $message; |
| 1049 | } else { |
| 1050 | if ( ! empty( $_POST['auto_draft'] ) ) |
| 1051 | $id = 0; // This tells us it didn't actually save |
| 1052 | else |
| 1053 | $id = $post->ID; |
| 1054 | } |
| 1055 | |
| 1056 | if ( $do_lock && empty( $_POST['auto_draft'] ) && $id && is_numeric( $id ) ) { |
| 1057 | $lock_result = wp_set_post_lock( $id ); |
| 1058 | $supplemental['active-post-lock'] = implode( ':', $lock_result ); |
| 1059 | } |
| 1060 | |
| 1061 | if ( $nonce_age == 2 ) { |
| 1062 | $supplemental['replace-autosavenonce'] = wp_create_nonce('autosave'); |
| 1063 | $supplemental['replace-getpermalinknonce'] = wp_create_nonce('getpermalink'); |
| 1064 | $supplemental['replace-samplepermalinknonce'] = wp_create_nonce('samplepermalink'); |
| 1065 | $supplemental['replace-closedpostboxesnonce'] = wp_create_nonce('closedpostboxes'); |
| 1066 | $supplemental['replace-_ajax_linking_nonce'] = wp_create_nonce( 'internal-linking' ); |
| 1067 | if ( $id ) { |
| 1068 | if ( $_POST['post_type'] == 'post' ) |
| 1069 | $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id); |
| 1070 | elseif ( $_POST['post_type'] == 'page' ) |
| 1071 | $supplemental['replace-_wpnonce'] = wp_create_nonce('update-page_' . $id); |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | if ( ! empty($alert) ) |
| 1076 | $supplemental['alert'] = $alert; |
| 1077 | |
| 1078 | $x = new WP_Ajax_Response( array( |
| 1079 | 'what' => 'autosave', |
| 1080 | 'id' => $id, |
| 1081 | 'data' => $id ? $data : '', |
| 1082 | 'supplemental' => $supplemental |
| 1083 | ) ); |
| 1084 | $x->send(); |
| 1085 | } |
| 1086 | |
| 1087 | add_action('wp_ajax_closed-postboxes', 'wp_ajax_closed_postboxes'); |
| 1088 | function wp_ajax_closed_postboxes() { |
| 1089 | check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' ); |
| 1090 | $closed = isset( $_POST['closed'] ) ? explode( ',', $_POST['closed']) : array(); |
| 1091 | $closed = array_filter($closed); |
| 1092 | |
| 1093 | $hidden = isset( $_POST['hidden'] ) ? explode( ',', $_POST['hidden']) : array(); |
| 1094 | $hidden = array_filter($hidden); |
| 1095 | |
| 1096 | $page = isset( $_POST['page'] ) ? $_POST['page'] : ''; |
| 1097 | |
| 1098 | if ( $page != sanitize_key( $page ) ) |
| 1099 | die('0'); |
| 1100 | |
| 1101 | if ( ! $user = wp_get_current_user() ) |
| 1102 | die('-1'); |
| 1103 | |
| 1104 | if ( is_array($closed) ) |
| 1105 | update_user_option($user->ID, "closedpostboxes_$page", $closed, true); |
| 1106 | |
| 1107 | if ( is_array($hidden) ) { |
| 1108 | $hidden = array_diff( $hidden, array('submitdiv', 'linksubmitdiv', 'manage-menu', 'create-menu') ); // postboxes that are always shown |
| 1109 | update_user_option($user->ID, "metaboxhidden_$page", $hidden, true); |
| 1110 | } |
| 1111 | |
| 1112 | die('1'); |
| 1113 | } |
| 1114 | |
| 1115 | add_action('wp_ajax_hidden-columns', 'wp_ajax_hidden_columns'); |
| 1116 | function wp_ajax_hidden_columns() { |
| 1117 | check_ajax_referer( 'screen-options-nonce', 'screenoptionnonce' ); |
| 1118 | $hidden = isset( $_POST['hidden'] ) ? $_POST['hidden'] : ''; |
| 1119 | $hidden = explode( ',', $_POST['hidden'] ); |
| 1120 | $page = isset( $_POST['page'] ) ? $_POST['page'] : ''; |
| 1121 | |
| 1122 | if ( $page != sanitize_key( $page ) ) |
| 1123 | die('0'); |
| 1124 | |
| 1125 | if ( ! $user = wp_get_current_user() ) |
| 1126 | die('-1'); |
| 1127 | |
| 1128 | if ( is_array($hidden) ) |
| 1129 | update_user_option($user->ID, "manage{$page}columnshidden", $hidden, true); |
| 1130 | |
| 1131 | die('1'); |
| 1132 | } |
| 1133 | |
| 1134 | add_action('wp_ajax_update-welcome-panel', 'wp_ajax_update_welcome_panel'); |
| 1135 | function wp_ajax_update_welcome_panel() { |
| 1136 | check_ajax_referer( 'welcome-panel-nonce', 'welcomepanelnonce' ); |
| 1137 | |
| 1138 | error_log('smeeer'); |
| 1139 | |
| 1140 | if ( ! current_user_can( 'edit_theme_options' ) ) { |
| 1141 | error_log('smaaa'); |
| 1142 | die('-1'); |
| 1143 | } |
| 1144 | |
| 1145 | update_user_meta( get_current_user_id(), 'show_welcome_panel', empty( $_POST['visible'] ) ? 0 : 1 ); |
| 1146 | error_log('amsiquia'); |
| 1147 | |
| 1148 | die('1'); |
| 1149 | } |
| 1150 | |
| 1151 | add_action('wp_ajax_menu-get-metabox', 'wp_ajax_menu_get_metabox'); |
| 1152 | function wp_ajax_menu_get_metabox() { |
| 1153 | if ( ! current_user_can( 'edit_theme_options' ) ) |
| 1154 | die('-1'); |
| 1155 | |
| 1156 | require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; |
| 1157 | |
| 1158 | if ( isset( $_POST['item-type'] ) && 'post_type' == $_POST['item-type'] ) { |
| 1159 | $type = 'posttype'; |
| 1160 | $callback = 'wp_nav_menu_item_post_type_meta_box'; |
| 1161 | $items = (array) get_post_types( array( 'show_in_nav_menus' => true ), 'object' ); |
| 1162 | } elseif ( isset( $_POST['item-type'] ) && 'taxonomy' == $_POST['item-type'] ) { |
| 1163 | $type = 'taxonomy'; |
| 1164 | $callback = 'wp_nav_menu_item_taxonomy_meta_box'; |
| 1165 | $items = (array) get_taxonomies( array( 'show_ui' => true ), 'object' ); |
| 1166 | } |
| 1167 | |
| 1168 | if ( ! empty( $_POST['item-object'] ) && isset( $items[$_POST['item-object']] ) ) { |
| 1169 | $item = apply_filters( 'nav_menu_meta_box_object', $items[ $_POST['item-object'] ] ); |
| 1170 | ob_start(); |
| 1171 | call_user_func_array($callback, array( |
| 1172 | null, |
| 1173 | array( |
| 1174 | 'id' => 'add-' . $item->name, |
| 1175 | 'title' => $item->labels->name, |
| 1176 | 'callback' => $callback, |
| 1177 | 'args' => $item, |
| 1178 | ) |
| 1179 | )); |
| 1180 | |
| 1181 | $markup = ob_get_clean(); |
| 1182 | |
| 1183 | echo json_encode(array( |
| 1184 | 'replace-id' => $type . '-' . $item->name, |
| 1185 | 'markup' => $markup, |
| 1186 | )); |
| 1187 | } |
| 1188 | |
| 1189 | exit; |
| 1190 | } |
| 1191 | |
| 1192 | add_action('wp_ajax_wp-link-ajax', 'wp_ajax_wp_link_ajax'); |
| 1193 | function wp_ajax_wp_link_ajax() { |
| 1194 | check_ajax_referer( 'internal-linking', '_ajax_linking_nonce' ); |
| 1195 | |
| 1196 | $args = array(); |
| 1197 | |
| 1198 | if ( isset( $_POST['search'] ) ) |
| 1199 | $args['s'] = stripslashes( $_POST['search'] ); |
| 1200 | $args['pagenum'] = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1; |
| 1201 | |
| 1202 | require(ABSPATH . WPINC . '/class-wp-editor.php'); |
| 1203 | $results = _WP_Editor::wp_link_query( $args ); |
| 1204 | |
| 1205 | if ( ! isset( $results ) ) |
| 1206 | die( '0' ); |
| 1207 | |
| 1208 | echo json_encode( $results ); |
| 1209 | echo "\n"; |
| 1210 | |
| 1211 | exit; |
| 1212 | } |
| 1213 | |
| 1214 | add_action('wp_ajax_menu-locations-save', 'wp_ajax_menu_locations_save'); |
| 1215 | function wp_ajax_menu_locations_save() { |
| 1216 | if ( ! current_user_can( 'edit_theme_options' ) ) |
| 1217 | die('-1'); |
| 1218 | check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' ); |
| 1219 | if ( ! isset( $_POST['menu-locations'] ) ) |
| 1220 | die('0'); |
| 1221 | set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_POST['menu-locations'] ) ); |
| 1222 | die('1'); |
| 1223 | } |
| 1224 | |
| 1225 | add_action('wp_ajax_meta-box-order', 'wp_ajax_meta_box_order'); |
| 1226 | function wp_ajax_meta_box_order() { |
| 1227 | check_ajax_referer( 'meta-box-order' ); |
| 1228 | $order = isset( $_POST['order'] ) ? (array) $_POST['order'] : false; |
| 1229 | $page_columns = isset( $_POST['page_columns'] ) ? $_POST['page_columns'] : 'auto'; |
| 1230 | |
| 1231 | if ( $page_columns != 'auto' ) |
| 1232 | $page_columns = (int) $page_columns; |
| 1233 | |
| 1234 | $page = isset( $_POST['page'] ) ? $_POST['page'] : ''; |
| 1235 | |
| 1236 | if ( $page != sanitize_key( $page ) ) |
| 1237 | die('0'); |
| 1238 | |
| 1239 | if ( ! $user = wp_get_current_user() ) |
| 1240 | die('-1'); |
| 1241 | |
| 1242 | if ( $order ) |
| 1243 | update_user_option($user->ID, "meta-box-order_$page", $order, true); |
| 1244 | |
| 1245 | if ( $page_columns ) |
| 1246 | update_user_option($user->ID, "screen_layout_$page", $page_columns, true); |
| 1247 | |
| 1248 | die('1'); |
| 1249 | } |
| 1250 | |
| 1251 | add_action('wp_ajax_get-permalink', 'wp_ajax_get_permalink'); |
| 1252 | function wp_ajax_get_permalink() { |
| 1253 | check_ajax_referer( 'getpermalink', 'getpermalinknonce' ); |
| 1254 | $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0; |
| 1255 | die(add_query_arg(array('preview' => 'true'), get_permalink($post_id))); |
| 1256 | } |
| 1257 | |
| 1258 | add_action('wp_ajax_sample-permalink', 'wp_ajax_sample_permalink'); |
| 1259 | function wp_ajax_sample_permalink() { |
| 1260 | check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' ); |
| 1261 | $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0; |
| 1262 | $title = isset($_POST['new_title'])? $_POST['new_title'] : ''; |
| 1263 | $slug = isset($_POST['new_slug'])? $_POST['new_slug'] : null; |
| 1264 | die(get_sample_permalink_html($post_id, $title, $slug)); |
| 1265 | } |
| 1266 | |
| 1267 | add_action('wp_ajax_inline-save', 'wp_ajax_inline_save'); |
| 1268 | function wp_ajax_inline_save() { |
| 1269 | check_ajax_referer( 'inlineeditnonce', '_inline_edit' ); |
| 1270 | |
| 1271 | if ( ! isset($_POST['post_ID']) || ! ( $post_ID = (int) $_POST['post_ID'] ) ) |
| 1272 | exit; |
| 1273 | |
| 1274 | if ( 'page' == $_POST['post_type'] ) { |
| 1275 | if ( ! current_user_can( 'edit_page', $post_ID ) ) |
| 1276 | die( __('You are not allowed to edit this page.') ); |
| 1277 | } else { |
| 1278 | if ( ! current_user_can( 'edit_post', $post_ID ) ) |
| 1279 | die( __('You are not allowed to edit this post.') ); |
| 1280 | } |
| 1281 | |
| 1282 | set_current_screen( $_POST['screen'] ); |
| 1283 | |
| 1284 | if ( $last = wp_check_post_lock( $post_ID ) ) { |
| 1285 | $last_user = get_userdata( $last ); |
| 1286 | $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' ); |
| 1287 | printf( $_POST['post_type'] == 'page' ? __( 'Saving is disabled: %s is currently editing this page.' ) : __( 'Saving is disabled: %s is currently editing this post.' ), esc_html( $last_user_name ) ); |
| 1288 | exit; |
| 1289 | } |
| 1290 | |
| 1291 | $data = &$_POST; |
| 1292 | |
| 1293 | $post = get_post( $post_ID, ARRAY_A ); |
| 1294 | $post = add_magic_quotes($post); //since it is from db |
| 1295 | |
| 1296 | $data['content'] = $post['post_content']; |
| 1297 | $data['excerpt'] = $post['post_excerpt']; |
| 1298 | |
| 1299 | // rename |
| 1300 | $data['user_ID'] = $GLOBALS['user_ID']; |
| 1301 | |
| 1302 | if ( isset($data['post_parent']) ) |
| 1303 | $data['parent_id'] = $data['post_parent']; |
| 1304 | |
| 1305 | // status |
| 1306 | if ( isset($data['keep_private']) && 'private' == $data['keep_private'] ) |
| 1307 | $data['post_status'] = 'private'; |
| 1308 | else |
| 1309 | $data['post_status'] = $data['_status']; |
| 1310 | |
| 1311 | if ( empty($data['comment_status']) ) |
| 1312 | $data['comment_status'] = 'closed'; |
| 1313 | if ( empty($data['ping_status']) ) |
| 1314 | $data['ping_status'] = 'closed'; |
| 1315 | |
| 1316 | // update the post |
| 1317 | edit_post(); |
| 1318 | |
| 1319 | $wp_list_table = _get_list_table('WP_Posts_List_Table'); |
| 1320 | |
| 1321 | $mode = $_POST['post_view']; |
| 1322 | $wp_list_table->display_rows( array( get_post( $_POST['post_ID'] ) ) ); |
| 1323 | |
| 1324 | exit; |
| 1325 | } |
| 1326 | |
| 1327 | add_action('wp_ajax_inline-save-tax', 'wp_ajax_inline_save_tax'); |
| 1328 | function wp_ajax_inline_save_tax() { |
| 1329 | check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' ); |
| 1330 | |
| 1331 | $taxonomy = sanitize_key( $_POST['taxonomy'] ); |
| 1332 | $tax = get_taxonomy( $taxonomy ); |
| 1333 | if ( ! $tax ) |
| 1334 | die( '0' ); |
| 1335 | |
| 1336 | if ( ! current_user_can( $tax->cap->edit_terms ) ) |
| 1337 | die( '-1' ); |
| 1338 | |
| 1339 | set_current_screen( 'edit-' . $taxonomy ); |
| 1340 | |
| 1341 | $wp_list_table = _get_list_table('WP_Terms_List_Table'); |
| 1342 | |
| 1343 | if ( ! isset($_POST['tax_ID']) || ! ( $id = (int) $_POST['tax_ID'] ) ) |
| 1344 | die(-1); |
| 1345 | |
| 1346 | $tag = get_term( $id, $taxonomy ); |
| 1347 | $_POST['description'] = $tag->description; |
| 1348 | |
| 1349 | $updated = wp_update_term($id, $taxonomy, $_POST); |
| 1350 | if ( $updated && !is_wp_error($updated) ) { |
| 1351 | $tag = get_term( $updated['term_id'], $taxonomy ); |
| 1352 | if ( !$tag || is_wp_error( $tag ) ) { |
| 1353 | if ( is_wp_error($tag) && $tag->get_error_message() ) |
| 1354 | die( $tag->get_error_message() ); |
| 1355 | die( __('Item not updated.') ); |
| 1356 | } |
| 1357 | |
| 1358 | echo $wp_list_table->single_row( $tag ); |
| 1359 | } else { |
| 1360 | if ( is_wp_error($updated) && $updated->get_error_message() ) |
| 1361 | die( $updated->get_error_message() ); |
| 1362 | die( __('Item not updated.') ); |
| 1363 | } |
| 1364 | |
| 1365 | exit; |
| 1366 | } |
| 1367 | |
| 1368 | add_action('wp_ajax_find_posts', 'wp_ajax_find_posts'); |
| 1369 | function wp_ajax_find_posts() { |
| 1370 | check_ajax_referer( 'find-posts' ); |
| 1371 | |
| 1372 | if ( empty($_POST['ps']) ) |
| 1373 | exit; |
| 1374 | |
| 1375 | if ( !empty($_POST['post_type']) && in_array( $_POST['post_type'], get_post_types() ) ) |
| 1376 | $what = $_POST['post_type']; |
| 1377 | else |
| 1378 | $what = 'post'; |
| 1379 | |
| 1380 | $s = stripslashes($_POST['ps']); |
| 1381 | preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches); |
| 1382 | $search_terms = array_map('_search_terms_tidy', $matches[0]); |
| 1383 | |
| 1384 | $searchand = $search = ''; |
| 1385 | foreach ( (array) $search_terms as $term ) { |
| 1386 | $term = esc_sql( like_escape( $term ) ); |
| 1387 | $search .= "{$searchand}(($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%'))"; |
| 1388 | $searchand = ' AND '; |
| 1389 | } |
| 1390 | $term = esc_sql( like_escape( $s ) ); |
| 1391 | if ( count($search_terms) > 1 && $search_terms[0] != $s ) |
| 1392 | $search .= " OR ($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%')"; |
| 1393 | |
| 1394 | $posts = $wpdb->get_results( "SELECT ID, post_title, post_status, post_date FROM $wpdb->posts WHERE post_type = '$what' AND post_status IN ('draft', 'publish') AND ($search) ORDER BY post_date_gmt DESC LIMIT 50" ); |
| 1395 | |
| 1396 | if ( ! $posts ) { |
| 1397 | $posttype = get_post_type_object($what); |
| 1398 | exit($posttype->labels->not_found); |
| 1399 | } |
| 1400 | |
| 1401 | $html = '<table class="widefat" cellspacing="0"><thead><tr><th class="found-radio"><br /></th><th>'.__('Title').'</th><th>'.__('Date').'</th><th>'.__('Status').'</th></tr></thead><tbody>'; |
| 1402 | foreach ( $posts as $post ) { |
| 1403 | |
| 1404 | switch ( $post->post_status ) { |
| 1405 | case 'publish' : |
| 1406 | case 'private' : |
| 1407 | $stat = __('Published'); |
| 1408 | break; |
| 1409 | case 'future' : |
| 1410 | $stat = __('Scheduled'); |
| 1411 | break; |
| 1412 | case 'pending' : |
| 1413 | $stat = __('Pending Review'); |
| 1414 | break; |
| 1415 | case 'draft' : |
| 1416 | $stat = __('Draft'); |
| 1417 | break; |
| 1418 | } |
| 1419 | |
| 1420 | if ( '0000-00-00 00:00:00' == $post->post_date ) { |
| 1421 | $time = ''; |
| 1422 | } else { |
| 1423 | /* translators: date format in table columns, see http://php.net/date */ |
| 1424 | $time = mysql2date(__('Y/m/d'), $post->post_date); |
| 1425 | } |
| 1426 | |
| 1427 | $html .= '<tr class="found-posts"><td class="found-radio"><input type="radio" id="found-'.$post->ID.'" name="found_post_id" value="' . esc_attr($post->ID) . '"></td>'; |
| 1428 | $html .= '<td><label for="found-'.$post->ID.'">'.esc_html( $post->post_title ).'</label></td><td>'.esc_html( $time ).'</td><td>'.esc_html( $stat ).'</td></tr>'."\n\n"; |
| 1429 | } |
| 1430 | $html .= '</tbody></table>'; |
| 1431 | |
| 1432 | $x = new WP_Ajax_Response(); |
| 1433 | $x->add( array( |
| 1434 | 'what' => $what, |
| 1435 | 'data' => $html |
| 1436 | )); |
| 1437 | $x->send(); |
| 1438 | } |
| 1439 | |
| 1440 | add_action('wp_ajax_widgets-order', 'wp_ajax_widgets_order'); |
| 1441 | function wp_ajax_widgets_order() { |
| 1442 | check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' ); |
| 1443 | |
| 1444 | if ( !current_user_can('edit_theme_options') ) |
| 1445 | die('-1'); |
| 1446 | |
| 1447 | unset( $_POST['savewidgets'], $_POST['action'] ); |
| 1448 | |
| 1449 | // save widgets order for all sidebars |
| 1450 | if ( is_array($_POST['sidebars']) ) { |
| 1451 | $sidebars = array(); |
| 1452 | foreach ( $_POST['sidebars'] as $key => $val ) { |
| 1453 | $sb = array(); |
| 1454 | if ( !empty($val) ) { |
| 1455 | $val = explode(',', $val); |
| 1456 | foreach ( $val as $k => $v ) { |
| 1457 | if ( strpos($v, 'widget-') === false ) |
| 1458 | continue; |
| 1459 | |
| 1460 | $sb[$k] = substr($v, strpos($v, '_') + 1); |
| 1461 | } |
| 1462 | } |
| 1463 | $sidebars[$key] = $sb; |
| 1464 | } |
| 1465 | wp_set_sidebars_widgets($sidebars); |
| 1466 | die('1'); |
| 1467 | } |
| 1468 | |
| 1469 | die('-1'); |
| 1470 | } |
| 1471 | |
| 1472 | add_action('wp_ajax_save-widget', 'wp_ajax_save_widget'); |
| 1473 | function wp_ajax_save_widget() { |
| 1474 | global $wp_registered_widgets, $wp_registered_widget_updates; |
| 1475 | global $wp_registered_widget_controls; |
| 1476 | |
| 1477 | check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' ); |
| 1478 | |
| 1479 | if ( !current_user_can('edit_theme_options') || !isset($_POST['id_base']) ) |
| 1480 | die('-1'); |
| 1481 | |
| 1482 | unset( $_POST['savewidgets'], $_POST['action'] ); |
| 1483 | |
| 1484 | do_action('load-widgets.php'); |
| 1485 | do_action('widgets.php'); |
| 1486 | do_action('sidebar_admin_setup'); |
| 1487 | |
| 1488 | $id_base = $_POST['id_base']; |
| 1489 | $widget_id = $_POST['widget-id']; |
| 1490 | $sidebar_id = $_POST['sidebar']; |
| 1491 | $multi_number = !empty($_POST['multi_number']) ? (int) $_POST['multi_number'] : 0; |
| 1492 | $settings = isset($_POST['widget-' . $id_base]) && is_array($_POST['widget-' . $id_base]) ? $_POST['widget-' . $id_base] : false; |
| 1493 | $error = '<p>' . __('An error has occurred. Please reload the page and try again.') . '</p>'; |
| 1494 | |
| 1495 | $sidebars = wp_get_sidebars_widgets(); |
| 1496 | $sidebar = isset($sidebars[$sidebar_id]) ? $sidebars[$sidebar_id] : array(); |
| 1497 | |
| 1498 | // delete |
| 1499 | if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) { |
| 1500 | |
| 1501 | if ( !isset($wp_registered_widgets[$widget_id]) ) |
| 1502 | die($error); |
| 1503 | |
| 1504 | $sidebar = array_diff( $sidebar, array($widget_id) ); |
| 1505 | $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1'); |
| 1506 | } elseif ( $settings && preg_match( '/__i__|%i%/', key($settings) ) ) { |
| 1507 | if ( !$multi_number ) |
| 1508 | die($error); |
| 1509 | |
| 1510 | $_POST['widget-' . $id_base] = array( $multi_number => array_shift($settings) ); |
| 1511 | $widget_id = $id_base . '-' . $multi_number; |
| 1512 | $sidebar[] = $widget_id; |
| 1513 | } |
| 1514 | $_POST['widget-id'] = $sidebar; |
| 1515 | |
| 1516 | foreach ( (array) $wp_registered_widget_updates as $name => $control ) { |
| 1517 | |
| 1518 | if ( $name == $id_base ) { |
| 1519 | if ( !is_callable( $control['callback'] ) ) |
| 1520 | continue; |
| 1521 | |
| 1522 | ob_start(); |
| 1523 | call_user_func_array( $control['callback'], $control['params'] ); |
| 1524 | ob_end_clean(); |
| 1525 | break; |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) { |
| 1530 | $sidebars[$sidebar_id] = $sidebar; |
| 1531 | wp_set_sidebars_widgets($sidebars); |
| 1532 | echo "deleted:$widget_id"; |
| 1533 | die(); |
| 1534 | } |
| 1535 | |
| 1536 | if ( !empty($_POST['add_new']) ) |
| 1537 | die(); |
| 1538 | |
| 1539 | if ( $form = $wp_registered_widget_controls[$widget_id] ) |
| 1540 | call_user_func_array( $form['callback'], $form['params'] ); |
| 1541 | |
| 1542 | die(); |
| 1543 | } |
| 1544 | |
| 1545 | add_action('wp_ajax_set-post-thumbnail', 'wp_ajax_set_post_thumbnail'); |
| 1546 | function wp_ajax_set_thumbnail() { |
| 1547 | $post_ID = intval( $_POST['post_id'] ); |
| 1548 | if ( !current_user_can( 'edit_post', $post_ID ) ) |
| 1549 | die( '-1' ); |
| 1550 | $thumbnail_id = intval( $_POST['thumbnail_id'] ); |
| 1551 | |
| 1552 | check_ajax_referer( "set_post_thumbnail-$post_ID" ); |
| 1553 | |
| 1554 | if ( $thumbnail_id == '-1' ) { |
| 1555 | if ( delete_post_thumbnail( $post_ID ) ) |
| 1556 | die( _wp_post_thumbnail_html() ); |
| 1557 | else |
| 1558 | die( '0' ); |
| 1559 | } |
| 1560 | |
| 1561 | if ( set_post_thumbnail( $post_ID, $thumbnail_id ) ) |
| 1562 | die( _wp_post_thumbnail_html( $thumbnail_id ) ); |
| 1563 | die( '0' ); |
| 1564 | } |
| 1565 | |
| 1566 | add_action('wp_ajax_date_format', 'wp_ajax_date_format'); |
| 1567 | function wp_ajax_date_format() { |
| 1568 | die( date_i18n( sanitize_option( 'date_format', $_POST['date'] ) ) ); |
| 1569 | } |
| 1570 | |
| 1571 | add_action('wp_ajax_time_format', 'wp_ajax_time_format'); |
| 1572 | function wp_ajax_time_format() { |
| 1573 | die( date_i18n( sanitize_option( 'time_format', $_POST['date'] ) ) ); |
| 1574 | } |
| 1575 | |
| 1576 | add_action('wp_ajax_wp-fullscreen-save-post', 'wp_ajax_wp_fullscreen_save_post'); |
| 1577 | function wp_ajax_wp_fullscreen_save_post() { |
| 1578 | $post_id = isset( $_POST['post_ID'] ) ? (int) $_POST['post_ID'] : 0; |
| 1579 | |
| 1580 | $post = $post_type = null; |
| 1581 | |
| 1582 | if ( $post_id ) |
| 1583 | $post = get_post( $post_id ); |
| 1584 | |
| 1585 | if ( $post ) |
| 1586 | $post_type = $post->post_type; |
| 1587 | elseif ( isset( $_POST['post_type'] ) && post_type_exists( $_POST['post_type'] ) ) |
| 1588 | $post_type = $_POST['post_type']; |
| 1589 | |
| 1590 | check_ajax_referer('update-' . $post_type . '_' . $post_id, '_wpnonce'); |
| 1591 | |
| 1592 | $post_id = edit_post(); |
| 1593 | |
| 1594 | if ( is_wp_error($post_id) ) { |
| 1595 | if ( $post_id->get_error_message() ) |
| 1596 | $message = $post_id->get_error_message(); |
| 1597 | else |
| 1598 | $message = __('Save failed'); |
| 1599 | |
| 1600 | echo json_encode( array( 'message' => $message, 'last_edited' => '' ) ); |
| 1601 | die(); |
| 1602 | } else { |
| 1603 | $message = __('Saved.'); |
| 1604 | } |
| 1605 | |
| 1606 | if ( $post ) { |
| 1607 | $last_date = mysql2date( get_option('date_format'), $post->post_modified ); |
| 1608 | $last_time = mysql2date( get_option('time_format'), $post->post_modified ); |
| 1609 | } else { |
| 1610 | $last_date = date_i18n( get_option('date_format') ); |
| 1611 | $last_time = date_i18n( get_option('time_format') ); |
| 1612 | } |
| 1613 | |
| 1614 | if ( $last_id = get_post_meta($post_id, '_edit_last', true) ) { |
| 1615 | $last_user = get_userdata($last_id); |
| 1616 | $last_edited = sprintf( __('Last edited by %1$s on %2$s at %3$s'), esc_html( $last_user->display_name ), $last_date, $last_time ); |
| 1617 | } else { |
| 1618 | $last_edited = sprintf( __('Last edited on %1$s at %2$s'), $last_date, $last_time ); |
| 1619 | } |
| 1620 | |
| 1621 | echo json_encode( array( 'message' => $message, 'last_edited' => $last_edited ) ); |
| 1622 | die(); |
| 1623 | } |
| 1624 | |
| 1625 | add_action('wp_ajax_wp-remove-post-lock', 'wp_ajax_wp_remove_post_lock'); |
| 1626 | function wp_ajax_wp_remove_post_lock() { |
| 1627 | if ( empty( $_POST['post_ID'] ) || empty( $_POST['active_post_lock'] ) ) |
| 1628 | die( '0' ); |
| 1629 | $post_id = (int) $_POST['post_ID']; |
| 1630 | if ( ! $post = get_post( $post_id ) ) |
| 1631 | die( '0' ); |
| 1632 | |
| 1633 | check_ajax_referer( 'update-' . $post->post_type . '_' . $post_id ); |
| 1634 | |
| 1635 | if ( ! current_user_can( 'edit_post', $post_id ) ) |
| 1636 | die( '-1' ); |
| 1637 | |
| 1638 | $active_lock = array_map( 'absint', explode( ':', $_POST['active_post_lock'] ) ); |
| 1639 | if ( $active_lock[1] != get_current_user_id() ) |
| 1640 | die( '0' ); |
| 1641 | |
| 1642 | $new_lock = ( time() - apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 ) + 5 ) . ':' . $active_lock[1]; |
| 1643 | update_post_meta( $post_id, '_edit_lock', $new_lock, implode( ':', $active_lock ) ); |
| 1644 | die( '1' ); |
| 1645 | } |
| 1646 | |
| 1647 | add_action('wp_ajax_dismiss-wp-pointer', 'wp_ajax_dismiss-wp-pointer'); |
| 1648 | function wp_ajax_dismiss_wp_pointer() { |
| 1649 | $pointer = $_POST['pointer']; |
| 1650 | if ( $pointer != sanitize_key( $pointer ) ) |
| 1651 | die( '0' ); |
| 1652 | |
| 1653 | // check_ajax_referer( 'dismiss-pointer_' . $pointer ); |
| 1654 | |
| 1655 | $dismissed = array_filter( explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ) ); |
| 1656 | |
| 1657 | if ( in_array( $pointer, $dismissed ) ) |
| 1658 | die( '0' ); |
| 1659 | |
| 1660 | $dismissed[] = $pointer; |
| 1661 | $dismissed = implode( ',', $dismissed ); |
| 1662 | |
| 1663 | update_user_meta( get_current_user_id(), 'dismissed_wp_pointers', $dismissed ); |
| 1664 | die( '1' ); |
| 1665 | } |