PageRenderTime 67ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/admin-ajax.php

https://github.com/coffeant/wordpress
PHP | 1538 lines | 1244 code | 255 blank | 39 comment | 335 complexity | 59d326c93653249dccac9089946fdba2 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * WordPress AJAX Process Execution.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Executing AJAX process.
  10. *
  11. * @since 2.1.0
  12. */
  13. define('DOING_AJAX', true);
  14. define('WP_ADMIN', true);
  15. if ( ! isset( $_REQUEST['action'] ) )
  16. die('-1');
  17. require_once('../wp-load.php');
  18. require_once('./includes/admin.php');
  19. @header('Content-Type: text/html; charset=' . get_option('blog_charset'));
  20. send_nosniff_header();
  21. do_action('admin_init');
  22. if ( ! is_user_logged_in() ) {
  23. if ( isset( $_POST['action'] ) && $_POST['action'] == 'autosave' ) {
  24. $id = isset($_POST['post_ID'])? (int) $_POST['post_ID'] : 0;
  25. if ( ! $id )
  26. die('-1');
  27. $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() );
  28. $x = new WP_Ajax_Response( array(
  29. 'what' => 'autosave',
  30. 'id' => $id,
  31. 'data' => $message
  32. ) );
  33. $x->send();
  34. }
  35. if ( !empty( $_REQUEST['action'] ) )
  36. do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
  37. die('-1');
  38. }
  39. if ( isset( $_GET['action'] ) ) :
  40. switch ( $action = $_GET['action'] ) :
  41. case 'fetch-list' :
  42. $list_class = $_GET['list_args']['class'];
  43. check_ajax_referer( "fetch-list-$list_class", '_ajax_fetch_list_nonce' );
  44. $current_screen = (object) $_GET['list_args']['screen'];
  45. //TODO fix this in a better way see #15336
  46. $current_screen->is_network = 'false' === $current_screen->is_network ? false : true;
  47. $current_screen->is_user = 'false' === $current_screen->is_user ? false : true;
  48. define( 'WP_NETWORK_ADMIN', $current_screen->is_network );
  49. define( 'WP_USER_ADMIN', $current_screen->is_user );
  50. $wp_list_table = _get_list_table( $list_class );
  51. if ( ! $wp_list_table )
  52. die( '0' );
  53. if ( ! $wp_list_table->ajax_user_can() )
  54. die( '-1' );
  55. $wp_list_table->ajax_response();
  56. die( '0' );
  57. break;
  58. case 'ajax-tag-search' :
  59. if ( isset( $_GET['tax'] ) ) {
  60. $taxonomy = sanitize_key( $_GET['tax'] );
  61. $tax = get_taxonomy( $taxonomy );
  62. if ( ! $tax )
  63. die( '0' );
  64. if ( ! current_user_can( $tax->cap->assign_terms ) )
  65. die( '-1' );
  66. } else {
  67. die('0');
  68. }
  69. $s = stripslashes( $_GET['q'] );
  70. if ( false !== strpos( $s, ',' ) ) {
  71. $s = explode( ',', $s );
  72. $s = $s[count( $s ) - 1];
  73. }
  74. $s = trim( $s );
  75. if ( strlen( $s ) < 2 )
  76. die; // require 2 chars for matching
  77. $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 ) . '%' ) );
  78. echo join( $results, "\n" );
  79. die;
  80. break;
  81. case 'wp-compression-test' :
  82. if ( !current_user_can( 'manage_options' ) )
  83. die('-1');
  84. if ( ini_get('zlib.output_compression') || 'ob_gzhandler' == ini_get('output_handler') ) {
  85. update_site_option('can_compress_scripts', 0);
  86. die('0');
  87. }
  88. if ( isset($_GET['test']) ) {
  89. header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
  90. header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
  91. header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
  92. header( 'Pragma: no-cache' );
  93. header('Content-Type: application/x-javascript; charset=UTF-8');
  94. $force_gzip = ( defined('ENFORCE_GZIP') && ENFORCE_GZIP );
  95. $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."';
  96. if ( 1 == $_GET['test'] ) {
  97. echo $test_str;
  98. die;
  99. } elseif ( 2 == $_GET['test'] ) {
  100. if ( !isset($_SERVER['HTTP_ACCEPT_ENCODING']) )
  101. die('-1');
  102. if ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {
  103. header('Content-Encoding: deflate');
  104. $out = gzdeflate( $test_str, 1 );
  105. } elseif ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') && function_exists('gzencode') ) {
  106. header('Content-Encoding: gzip');
  107. $out = gzencode( $test_str, 1 );
  108. } else {
  109. die('-1');
  110. }
  111. echo $out;
  112. die;
  113. } elseif ( 'no' == $_GET['test'] ) {
  114. update_site_option('can_compress_scripts', 0);
  115. } elseif ( 'yes' == $_GET['test'] ) {
  116. update_site_option('can_compress_scripts', 1);
  117. }
  118. }
  119. die('0');
  120. break;
  121. case 'imgedit-preview' :
  122. $post_id = intval($_GET['postid']);
  123. if ( empty($post_id) || !current_user_can('edit_post', $post_id) )
  124. die('-1');
  125. check_ajax_referer( "image_editor-$post_id" );
  126. include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
  127. if ( ! stream_preview_image($post_id) )
  128. die('-1');
  129. die();
  130. break;
  131. case 'menu-quick-search':
  132. if ( ! current_user_can( 'edit_theme_options' ) )
  133. die('-1');
  134. require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
  135. _wp_ajax_menu_quick_search( $_REQUEST );
  136. exit;
  137. break;
  138. case 'oembed-cache' :
  139. $return = ( $wp_embed->cache_oembed( $_GET['post'] ) ) ? '1' : '0';
  140. die( $return );
  141. break;
  142. default :
  143. do_action( 'wp_ajax_' . $_GET['action'] );
  144. die('0');
  145. break;
  146. endswitch;
  147. endif;
  148. /**
  149. * Sends back current comment total and new page links if they need to be updated.
  150. *
  151. * Contrary to normal success AJAX response ("1"), die with time() on success.
  152. *
  153. * @since 2.7
  154. *
  155. * @param int $comment_id
  156. * @return die
  157. */
  158. function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) {
  159. $total = (int) @$_POST['_total'];
  160. $per_page = (int) @$_POST['_per_page'];
  161. $page = (int) @$_POST['_page'];
  162. $url = esc_url_raw( @$_POST['_url'] );
  163. // JS didn't send us everything we need to know. Just die with success message
  164. if ( !$total || !$per_page || !$page || !$url )
  165. die( (string) time() );
  166. $total += $delta;
  167. if ( $total < 0 )
  168. $total = 0;
  169. // Only do the expensive stuff on a page-break, and about 1 other time per page
  170. if ( 0 == $total % $per_page || 1 == mt_rand( 1, $per_page ) ) {
  171. $post_id = 0;
  172. $status = 'total_comments'; // What type of comment count are we looking for?
  173. $parsed = parse_url( $url );
  174. if ( isset( $parsed['query'] ) ) {
  175. parse_str( $parsed['query'], $query_vars );
  176. if ( !empty( $query_vars['comment_status'] ) )
  177. $status = $query_vars['comment_status'];
  178. if ( !empty( $query_vars['p'] ) )
  179. $post_id = (int) $query_vars['p'];
  180. }
  181. $comment_count = wp_count_comments($post_id);
  182. if ( isset( $comment_count->$status ) ) // We're looking for a known type of comment count
  183. $total = $comment_count->$status;
  184. // else use the decremented value from above
  185. }
  186. $time = time(); // The time since the last comment count
  187. $x = new WP_Ajax_Response( array(
  188. 'what' => 'comment',
  189. 'id' => $comment_id, // here for completeness - not used
  190. 'supplemental' => array(
  191. 'total_items_i18n' => sprintf( _n( '1 item', '%s items', $total ), number_format_i18n( $total ) ),
  192. 'total_pages' => ceil( $total / $per_page ),
  193. 'total_pages_i18n' => number_format_i18n( ceil( $total / $per_page ) ),
  194. 'total' => $total,
  195. 'time' => $time
  196. )
  197. ) );
  198. $x->send();
  199. }
  200. function _wp_ajax_add_hierarchical_term() {
  201. $action = $_POST['action'];
  202. $taxonomy = get_taxonomy(substr($action, 4));
  203. check_ajax_referer( $action, '_ajax_nonce-add-' . $taxonomy->name );
  204. if ( !current_user_can( $taxonomy->cap->edit_terms ) )
  205. die('-1');
  206. $names = explode(',', $_POST['new'.$taxonomy->name]);
  207. $parent = isset($_POST['new'.$taxonomy->name.'_parent']) ? (int) $_POST['new'.$taxonomy->name.'_parent'] : 0;
  208. if ( 0 > $parent )
  209. $parent = 0;
  210. if ( $taxonomy->name == 'category' )
  211. $post_category = isset($_POST['post_category']) ? (array) $_POST['post_category'] : array();
  212. else
  213. $post_category = ( isset($_POST['tax_input']) && isset($_POST['tax_input'][$taxonomy->name]) ) ? (array) $_POST['tax_input'][$taxonomy->name] : array();
  214. $checked_categories = array_map( 'absint', (array) $post_category );
  215. $popular_ids = wp_popular_terms_checklist($taxonomy->name, 0, 10, false);
  216. foreach ( $names as $cat_name ) {
  217. $cat_name = trim($cat_name);
  218. $category_nicename = sanitize_title($cat_name);
  219. if ( '' === $category_nicename )
  220. continue;
  221. if ( !($cat_id = term_exists($cat_name, $taxonomy->name, $parent)) ) {
  222. $new_term = wp_insert_term($cat_name, $taxonomy->name, array('parent' => $parent));
  223. $cat_id = $new_term['term_id'];
  224. }
  225. $checked_categories[] = $cat_id;
  226. if ( $parent ) // Do these all at once in a second
  227. continue;
  228. $category = get_term( $cat_id, $taxonomy->name );
  229. ob_start();
  230. wp_terms_checklist( 0, array( 'taxonomy' => $taxonomy->name, 'descendants_and_self' => $cat_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids ));
  231. $data = ob_get_contents();
  232. ob_end_clean();
  233. $add = array(
  234. 'what' => $taxonomy->name,
  235. 'id' => $cat_id,
  236. 'data' => str_replace( array("\n", "\t"), '', $data),
  237. 'position' => -1
  238. );
  239. }
  240. if ( $parent ) { // Foncy - replace the parent and all its children
  241. $parent = get_term( $parent, $taxonomy->name );
  242. $term_id = $parent->term_id;
  243. while ( $parent->parent ) { // get the top parent
  244. $parent = &get_term( $parent->parent, $taxonomy->name );
  245. if ( is_wp_error( $parent ) )
  246. break;
  247. $term_id = $parent->term_id;
  248. }
  249. ob_start();
  250. wp_terms_checklist( 0, array('taxonomy' => $taxonomy->name, 'descendants_and_self' => $term_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids));
  251. $data = ob_get_contents();
  252. ob_end_clean();
  253. $add = array(
  254. 'what' => $taxonomy->name,
  255. 'id' => $term_id,
  256. 'data' => str_replace( array("\n", "\t"), '', $data),
  257. 'position' => -1
  258. );
  259. }
  260. ob_start();
  261. wp_dropdown_categories( array(
  262. 'taxonomy' => $taxonomy->name, 'hide_empty' => 0, 'name' => 'new'.$taxonomy->name.'_parent', 'orderby' => 'name',
  263. 'hierarchical' => 1, 'show_option_none' => '&mdash; '.$taxonomy->labels->parent_item.' &mdash;'
  264. ) );
  265. $sup = ob_get_contents();
  266. ob_end_clean();
  267. $add['supplemental'] = array( 'newcat_parent' => $sup );
  268. $x = new WP_Ajax_Response( $add );
  269. $x->send();
  270. }
  271. $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
  272. switch ( $action = $_POST['action'] ) :
  273. case 'delete-comment' : // On success, die with time() instead of 1
  274. if ( !$comment = get_comment( $id ) )
  275. die( (string) time() );
  276. if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
  277. die('-1');
  278. check_ajax_referer( "delete-comment_$id" );
  279. $status = wp_get_comment_status( $comment->comment_ID );
  280. $delta = -1;
  281. if ( isset($_POST['trash']) && 1 == $_POST['trash'] ) {
  282. if ( 'trash' == $status )
  283. die( (string) time() );
  284. $r = wp_trash_comment( $comment->comment_ID );
  285. } elseif ( isset($_POST['untrash']) && 1 == $_POST['untrash'] ) {
  286. if ( 'trash' != $status )
  287. die( (string) time() );
  288. $r = wp_untrash_comment( $comment->comment_ID );
  289. if ( ! isset( $_POST['comment_status'] ) || $_POST['comment_status'] != 'trash' ) // undo trash, not in trash
  290. $delta = 1;
  291. } elseif ( isset($_POST['spam']) && 1 == $_POST['spam'] ) {
  292. if ( 'spam' == $status )
  293. die( (string) time() );
  294. $r = wp_spam_comment( $comment->comment_ID );
  295. } elseif ( isset($_POST['unspam']) && 1 == $_POST['unspam'] ) {
  296. if ( 'spam' != $status )
  297. die( (string) time() );
  298. $r = wp_unspam_comment( $comment->comment_ID );
  299. if ( ! isset( $_POST['comment_status'] ) || $_POST['comment_status'] != 'spam' ) // undo spam, not in spam
  300. $delta = 1;
  301. } elseif ( isset($_POST['delete']) && 1 == $_POST['delete'] ) {
  302. $r = wp_delete_comment( $comment->comment_ID );
  303. } else {
  304. die('-1');
  305. }
  306. if ( $r ) // Decide if we need to send back '1' or a more complicated response including page links and comment counts
  307. _wp_ajax_delete_comment_response( $comment->comment_ID, $delta );
  308. die( '0' );
  309. break;
  310. case 'delete-tag' :
  311. $tag_id = (int) $_POST['tag_ID'];
  312. check_ajax_referer( "delete-tag_$tag_id" );
  313. $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag';
  314. $tax = get_taxonomy($taxonomy);
  315. if ( !current_user_can( $tax->cap->delete_terms ) )
  316. die('-1');
  317. $tag = get_term( $tag_id, $taxonomy );
  318. if ( !$tag || is_wp_error( $tag ) )
  319. die('1');
  320. if ( wp_delete_term($tag_id, $taxonomy))
  321. die('1');
  322. else
  323. die('0');
  324. break;
  325. case 'delete-link' :
  326. check_ajax_referer( "delete-bookmark_$id" );
  327. if ( !current_user_can( 'manage_links' ) )
  328. die('-1');
  329. $link = get_bookmark( $id );
  330. if ( !$link || is_wp_error( $link ) )
  331. die('1');
  332. if ( wp_delete_link( $id ) )
  333. die('1');
  334. else
  335. die('0');
  336. break;
  337. case 'delete-meta' :
  338. check_ajax_referer( "delete-meta_$id" );
  339. if ( !$meta = get_post_meta_by_id( $id ) )
  340. die('1');
  341. if ( !current_user_can( 'edit_post', $meta->post_id ) )
  342. die('-1');
  343. if ( delete_meta( $meta->meta_id ) )
  344. die('1');
  345. die('0');
  346. break;
  347. case 'delete-post' :
  348. check_ajax_referer( "{$action}_$id" );
  349. if ( !current_user_can( 'delete_post', $id ) )
  350. die('-1');
  351. if ( !get_post( $id ) )
  352. die('1');
  353. if ( wp_delete_post( $id ) )
  354. die('1');
  355. else
  356. die('0');
  357. break;
  358. case 'trash-post' :
  359. case 'untrash-post' :
  360. check_ajax_referer( "{$action}_$id" );
  361. if ( !current_user_can( 'delete_post', $id ) )
  362. die('-1');
  363. if ( !get_post( $id ) )
  364. die('1');
  365. if ( 'trash-post' == $action )
  366. $done = wp_trash_post( $id );
  367. else
  368. $done = wp_untrash_post( $id );
  369. if ( $done )
  370. die('1');
  371. die('0');
  372. break;
  373. case 'delete-page' :
  374. check_ajax_referer( "{$action}_$id" );
  375. if ( !current_user_can( 'delete_page', $id ) )
  376. die('-1');
  377. if ( !get_page( $id ) )
  378. die('1');
  379. if ( wp_delete_post( $id ) )
  380. die('1');
  381. else
  382. die('0');
  383. break;
  384. case 'dim-comment' : // On success, die with time() instead of 1
  385. if ( !$comment = get_comment( $id ) ) {
  386. $x = new WP_Ajax_Response( array(
  387. 'what' => 'comment',
  388. 'id' => new WP_Error('invalid_comment', sprintf(__('Comment %d does not exist'), $id))
  389. ) );
  390. $x->send();
  391. }
  392. if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) && !current_user_can( 'moderate_comments' ) )
  393. die('-1');
  394. $current = wp_get_comment_status( $comment->comment_ID );
  395. if ( $_POST['new'] == $current )
  396. die( (string) time() );
  397. check_ajax_referer( "approve-comment_$id" );
  398. if ( in_array( $current, array( 'unapproved', 'spam' ) ) )
  399. $result = wp_set_comment_status( $comment->comment_ID, 'approve', true );
  400. else
  401. $result = wp_set_comment_status( $comment->comment_ID, 'hold', true );
  402. if ( is_wp_error($result) ) {
  403. $x = new WP_Ajax_Response( array(
  404. 'what' => 'comment',
  405. 'id' => $result
  406. ) );
  407. $x->send();
  408. }
  409. // Decide if we need to send back '1' or a more complicated response including page links and comment counts
  410. _wp_ajax_delete_comment_response( $comment->comment_ID );
  411. die( '0' );
  412. break;
  413. case 'add-link-category' : // On the Fly
  414. check_ajax_referer( $action );
  415. if ( !current_user_can( 'manage_categories' ) )
  416. die('-1');
  417. $names = explode(',', $_POST['newcat']);
  418. $x = new WP_Ajax_Response();
  419. foreach ( $names as $cat_name ) {
  420. $cat_name = trim($cat_name);
  421. $slug = sanitize_title($cat_name);
  422. if ( '' === $slug )
  423. continue;
  424. if ( !$cat_id = term_exists( $cat_name, 'link_category' ) ) {
  425. $cat_id = wp_insert_term( $cat_name, 'link_category' );
  426. }
  427. $cat_id = $cat_id['term_id'];
  428. $cat_name = esc_html(stripslashes($cat_name));
  429. $x->add( array(
  430. 'what' => 'link-category',
  431. 'id' => $cat_id,
  432. '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>",
  433. 'position' => -1
  434. ) );
  435. }
  436. $x->send();
  437. break;
  438. case 'add-tag' :
  439. check_ajax_referer( 'add-tag' );
  440. $post_type = !empty($_POST['post_type']) ? $_POST['post_type'] : 'post';
  441. $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag';
  442. $tax = get_taxonomy($taxonomy);
  443. if ( !current_user_can( $tax->cap->edit_terms ) )
  444. die('-1');
  445. $x = new WP_Ajax_Response();
  446. $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST );
  447. if ( !$tag || is_wp_error($tag) || (!$tag = get_term( $tag['term_id'], $taxonomy )) ) {
  448. $message = __('An error has occurred. Please reload the page and try again.');
  449. if ( is_wp_error($tag) && $tag->get_error_message() )
  450. $message = $tag->get_error_message();
  451. $x->add( array(
  452. 'what' => 'taxonomy',
  453. 'data' => new WP_Error('error', $message )
  454. ) );
  455. $x->send();
  456. }
  457. set_current_screen( $_POST['screen'] );
  458. $wp_list_table = _get_list_table('WP_Terms_List_Table');
  459. $level = 0;
  460. if ( is_taxonomy_hierarchical($taxonomy) ) {
  461. $level = count( get_ancestors( $tag->term_id, $taxonomy ) );
  462. ob_start();
  463. $wp_list_table->single_row( $tag, $level );
  464. $noparents = ob_get_clean();
  465. }
  466. ob_start();
  467. $wp_list_table->single_row( $tag );
  468. $parents = ob_get_clean();
  469. $x->add( array(
  470. 'what' => 'taxonomy',
  471. 'supplemental' => compact('parents', 'noparents')
  472. ) );
  473. $x->add( array(
  474. 'what' => 'term',
  475. 'position' => $level,
  476. 'supplemental' => (array) $tag
  477. ) );
  478. $x->send();
  479. break;
  480. case 'get-tagcloud' :
  481. if ( isset( $_POST['tax'] ) ) {
  482. $taxonomy = sanitize_key( $_POST['tax'] );
  483. $tax = get_taxonomy( $taxonomy );
  484. if ( ! $tax )
  485. die( '0' );
  486. if ( ! current_user_can( $tax->cap->assign_terms ) )
  487. die( '-1' );
  488. } else {
  489. die('0');
  490. }
  491. $tags = get_terms( $taxonomy, array( 'number' => 45, 'orderby' => 'count', 'order' => 'DESC' ) );
  492. if ( empty( $tags ) )
  493. die( isset( $tax->no_tagcloud ) ? $tax->no_tagcloud : __('No tags found!') );
  494. if ( is_wp_error( $tags ) )
  495. die( $tags->get_error_message() );
  496. foreach ( $tags as $key => $tag ) {
  497. $tags[ $key ]->link = '#';
  498. $tags[ $key ]->id = $tag->term_id;
  499. }
  500. // We need raw tag names here, so don't filter the output
  501. $return = wp_generate_tag_cloud( $tags, array('filter' => 0) );
  502. if ( empty($return) )
  503. die('0');
  504. echo $return;
  505. exit;
  506. break;
  507. case 'get-comments' :
  508. check_ajax_referer( $action );
  509. set_current_screen( 'edit-comments' );
  510. $wp_list_table = _get_list_table('WP_Post_Comments_List_Table');
  511. if ( !current_user_can( 'edit_post', $post_id ) )
  512. die('-1');
  513. $wp_list_table->prepare_items();
  514. if ( !$wp_list_table->has_items() )
  515. die('1');
  516. $comment_list_item = '';
  517. $x = new WP_Ajax_Response();
  518. foreach ( $wp_list_table->items as $comment ) {
  519. get_comment( $comment );
  520. ob_start();
  521. $wp_list_table->single_row( $comment );
  522. $comment_list_item .= ob_get_contents();
  523. ob_end_clean();
  524. }
  525. $x->add( array(
  526. 'what' => 'comments',
  527. 'data' => $comment_list_item
  528. ) );
  529. $x->send();
  530. break;
  531. case 'replyto-comment' :
  532. check_ajax_referer( $action, '_ajax_nonce-replyto-comment' );
  533. set_current_screen( 'edit-comments' );
  534. $comment_post_ID = (int) $_POST['comment_post_ID'];
  535. if ( !current_user_can( 'edit_post', $comment_post_ID ) )
  536. die('-1');
  537. $status = $wpdb->get_var( $wpdb->prepare("SELECT post_status FROM $wpdb->posts WHERE ID = %d", $comment_post_ID) );
  538. if ( empty($status) )
  539. die('1');
  540. elseif ( in_array($status, array('draft', 'pending', 'trash') ) )
  541. die( __('Error: you are replying to a comment on a draft post.') );
  542. $user = wp_get_current_user();
  543. if ( $user->ID ) {
  544. $comment_author = $wpdb->escape($user->display_name);
  545. $comment_author_email = $wpdb->escape($user->user_email);
  546. $comment_author_url = $wpdb->escape($user->user_url);
  547. $comment_content = trim($_POST['content']);
  548. if ( current_user_can('unfiltered_html') ) {
  549. if ( wp_create_nonce('unfiltered-html-comment_' . $comment_post_ID) != $_POST['_wp_unfiltered_html_comment'] ) {
  550. kses_remove_filters(); // start with a clean slate
  551. kses_init_filters(); // set up the filters
  552. }
  553. }
  554. } else {
  555. die( __('Sorry, you must be logged in to reply to a comment.') );
  556. }
  557. if ( '' == $comment_content )
  558. die( __('Error: please type a comment.') );
  559. $comment_parent = absint($_POST['comment_ID']);
  560. $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID');
  561. $comment_id = wp_new_comment( $commentdata );
  562. $comment = get_comment($comment_id);
  563. if ( ! $comment ) die('1');
  564. $position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1';
  565. $x = new WP_Ajax_Response();
  566. ob_start();
  567. if ( 'dashboard' == $_REQUEST['mode'] ) {
  568. require_once( ABSPATH . 'wp-admin/includes/dashboard.php' );
  569. _wp_dashboard_recent_comments_row( $comment );
  570. } else {
  571. if ( 'single' == $_REQUEST['mode'] ) {
  572. $wp_list_table = _get_list_table('WP_Post_Comments_List_Table');
  573. } else {
  574. $wp_list_table = _get_list_table('WP_Comments_List_Table');
  575. }
  576. $wp_list_table->single_row( $comment );
  577. }
  578. $comment_list_item = ob_get_contents();
  579. ob_end_clean();
  580. $x->add( array(
  581. 'what' => 'comment',
  582. 'id' => $comment->comment_ID,
  583. 'data' => $comment_list_item,
  584. 'position' => $position
  585. ));
  586. $x->send();
  587. break;
  588. case 'edit-comment' :
  589. check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' );
  590. set_current_screen( 'edit-comments' );
  591. $comment_post_ID = (int) $_POST['comment_post_ID'];
  592. if ( ! current_user_can( 'edit_post', $comment_post_ID ) )
  593. die('-1');
  594. if ( '' == $_POST['content'] )
  595. die( __('Error: please type a comment.') );
  596. $comment_id = (int) $_POST['comment_ID'];
  597. $_POST['comment_status'] = $_POST['status'];
  598. edit_comment();
  599. $position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1';
  600. $comments_status = isset($_POST['comments_listing']) ? $_POST['comments_listing'] : '';
  601. $checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0;
  602. $wp_list_table = _get_list_table( $checkbox ? 'WP_Comments_List_Table' : 'WP_Post_Comments_List_Table' );
  603. ob_start();
  604. $wp_list_table->single_row( get_comment( $comment_id ) );
  605. $comment_list_item = ob_get_contents();
  606. ob_end_clean();
  607. $x = new WP_Ajax_Response();
  608. $x->add( array(
  609. 'what' => 'edit_comment',
  610. 'id' => $comment->comment_ID,
  611. 'data' => $comment_list_item,
  612. 'position' => $position
  613. ));
  614. $x->send();
  615. break;
  616. case 'add-menu-item' :
  617. if ( ! current_user_can( 'edit_theme_options' ) )
  618. die('-1');
  619. check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' );
  620. require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
  621. // For performance reasons, we omit some object properties from the checklist.
  622. // The following is a hacky way to restore them when adding non-custom items.
  623. $menu_items_data = array();
  624. foreach ( (array) $_POST['menu-item'] as $menu_item_data ) {
  625. if (
  626. ! empty( $menu_item_data['menu-item-type'] ) &&
  627. 'custom' != $menu_item_data['menu-item-type'] &&
  628. ! empty( $menu_item_data['menu-item-object-id'] )
  629. ) {
  630. switch( $menu_item_data['menu-item-type'] ) {
  631. case 'post_type' :
  632. $_object = get_post( $menu_item_data['menu-item-object-id'] );
  633. break;
  634. case 'taxonomy' :
  635. $_object = get_term( $menu_item_data['menu-item-object-id'], $menu_item_data['menu-item-object'] );
  636. break;
  637. }
  638. $_menu_items = array_map( 'wp_setup_nav_menu_item', array( $_object ) );
  639. $_menu_item = array_shift( $_menu_items );
  640. // Restore the missing menu item properties
  641. $menu_item_data['menu-item-description'] = $_menu_item->description;
  642. }
  643. $menu_items_data[] = $menu_item_data;
  644. }
  645. $item_ids = wp_save_nav_menu_items( 0, $menu_items_data );
  646. if ( is_wp_error( $item_ids ) )
  647. die('-1');
  648. foreach ( (array) $item_ids as $menu_item_id ) {
  649. $menu_obj = get_post( $menu_item_id );
  650. if ( ! empty( $menu_obj->ID ) ) {
  651. $menu_obj = wp_setup_nav_menu_item( $menu_obj );
  652. $menu_obj->label = $menu_obj->title; // don't show "(pending)" in ajax-added items
  653. $menu_items[] = $menu_obj;
  654. }
  655. }
  656. if ( ! empty( $menu_items ) ) {
  657. $args = array(
  658. 'after' => '',
  659. 'before' => '',
  660. 'link_after' => '',
  661. 'link_before' => '',
  662. 'walker' => new Walker_Nav_Menu_Edit,
  663. );
  664. echo walk_nav_menu_tree( $menu_items, 0, (object) $args );
  665. }
  666. break;
  667. case 'add-meta' :
  668. check_ajax_referer( 'add-meta', '_ajax_nonce-add-meta' );
  669. $c = 0;
  670. $pid = (int) $_POST['post_id'];
  671. $post = get_post( $pid );
  672. if ( isset($_POST['metakeyselect']) || isset($_POST['metakeyinput']) ) {
  673. if ( !current_user_can( 'edit_post', $pid ) )
  674. die('-1');
  675. if ( isset($_POST['metakeyselect']) && '#NONE#' == $_POST['metakeyselect'] && empty($_POST['metakeyinput']) )
  676. die('1');
  677. if ( $post->post_status == 'auto-draft' ) {
  678. $save_POST = $_POST; // Backup $_POST
  679. $_POST = array(); // Make it empty for edit_post()
  680. $_POST['action'] = 'draft'; // Warning fix
  681. $_POST['post_ID'] = $pid;
  682. $_POST['post_type'] = $post->post_type;
  683. $_POST['post_status'] = 'draft';
  684. $now = current_time('timestamp', 1);
  685. $_POST['post_title'] = sprintf('Draft created on %s at %s', date(get_option('date_format'), $now), date(get_option('time_format'), $now));
  686. if ( $pid = edit_post() ) {
  687. if ( is_wp_error( $pid ) ) {
  688. $x = new WP_Ajax_Response( array(
  689. 'what' => 'meta',
  690. 'data' => $pid
  691. ) );
  692. $x->send();
  693. }
  694. $_POST = $save_POST; // Now we can restore original $_POST again
  695. if ( !$mid = add_meta( $pid ) )
  696. die(__('Please provide a custom field value.'));
  697. } else {
  698. die('0');
  699. }
  700. } else if ( !$mid = add_meta( $pid ) ) {
  701. die(__('Please provide a custom field value.'));
  702. }
  703. $meta = get_post_meta_by_id( $mid );
  704. $pid = (int) $meta->post_id;
  705. $meta = get_object_vars( $meta );
  706. $x = new WP_Ajax_Response( array(
  707. 'what' => 'meta',
  708. 'id' => $mid,
  709. 'data' => _list_meta_row( $meta, $c ),
  710. 'position' => 1,
  711. 'supplemental' => array('postid' => $pid)
  712. ) );
  713. } else { // Update?
  714. $mid = (int) array_pop( $var_by_ref = array_keys($_POST['meta']) );
  715. $key = $_POST['meta'][$mid]['key'];
  716. $value = $_POST['meta'][$mid]['value'];
  717. if ( '' == trim($key) )
  718. die(__('Please provide a custom field name.'));
  719. if ( '' == trim($value) )
  720. die(__('Please provide a custom field value.'));
  721. if ( !$meta = get_post_meta_by_id( $mid ) )
  722. die('0'); // if meta doesn't exist
  723. if ( !current_user_can( 'edit_post', $meta->post_id ) )
  724. die('-1');
  725. if ( $meta->meta_value != stripslashes($value) || $meta->meta_key != stripslashes($key) ) {
  726. if ( !$u = update_meta( $mid, $key, $value ) )
  727. die('0'); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems).
  728. }
  729. $key = stripslashes($key);
  730. $value = stripslashes($value);
  731. $x = new WP_Ajax_Response( array(
  732. 'what' => 'meta',
  733. 'id' => $mid, 'old_id' => $mid,
  734. 'data' => _list_meta_row( array(
  735. 'meta_key' => $key,
  736. 'meta_value' => $value,
  737. 'meta_id' => $mid
  738. ), $c ),
  739. 'position' => 0,
  740. 'supplemental' => array('postid' => $meta->post_id)
  741. ) );
  742. }
  743. $x->send();
  744. break;
  745. case 'add-user' :
  746. check_ajax_referer( $action );
  747. if ( !current_user_can('create_users') )
  748. die('-1');
  749. if ( !$user_id = add_user() )
  750. die('0');
  751. elseif ( is_wp_error( $user_id ) ) {
  752. $x = new WP_Ajax_Response( array(
  753. 'what' => 'user',
  754. 'id' => $user_id
  755. ) );
  756. $x->send();
  757. }
  758. $user_object = new WP_User( $user_id );
  759. $wp_list_table = _get_list_table('WP_Users_List_Table');
  760. $x = new WP_Ajax_Response( array(
  761. 'what' => 'user',
  762. 'id' => $user_id,
  763. 'data' => $wp_list_table->single_row( $user_object, '', $user_object->roles[0] ),
  764. 'supplemental' => array(
  765. 'show-link' => sprintf(__( 'User <a href="#%s">%s</a> added' ), "user-$user_id", $user_object->user_login),
  766. 'role' => $user_object->roles[0]
  767. )
  768. ) );
  769. $x->send();
  770. break;
  771. case 'autosave' : // The name of this action is hardcoded in edit_post()
  772. define( 'DOING_AUTOSAVE', true );
  773. $nonce_age = check_ajax_referer( 'autosave', 'autosavenonce' );
  774. $_POST['post_category'] = explode(",", $_POST['catslist']);
  775. if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
  776. unset($_POST['post_category']);
  777. $do_autosave = (bool) $_POST['autosave'];
  778. $do_lock = true;
  779. $data = $alert = '';
  780. /* translators: draft saved date format, see http://php.net/date */
  781. $draft_saved_date_format = __('g:i:s a');
  782. /* translators: %s: date and time */
  783. $message = sprintf( __('Draft saved at %s.'), date_i18n( $draft_saved_date_format ) );
  784. $supplemental = array();
  785. if ( isset($login_grace_period) )
  786. $alert .= sprintf( __('Your login has expired. Please open a new browser window and <a href="%s" target="_blank">login again</a>. '), add_query_arg( 'interim-login', 1, wp_login_url() ) );
  787. $id = $revision_id = 0;
  788. $post_ID = (int) $_POST['post_ID'];
  789. $_POST['ID'] = $post_ID;
  790. $post = get_post($post_ID);
  791. if ( 'auto-draft' == $post->post_status )
  792. $_POST['post_status'] = 'draft';
  793. if ( $last = wp_check_post_lock( $post->ID ) ) {
  794. $do_autosave = $do_lock = false;
  795. $last_user = get_userdata( $last );
  796. $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
  797. $data = __( 'Autosave disabled.' );
  798. $supplemental['disable_autosave'] = 'disable';
  799. $alert .= sprintf( __( '%s is currently editing this article. If you update it, you will overwrite the changes.' ), esc_html( $last_user_name ) );
  800. }
  801. if ( 'page' == $post->post_type ) {
  802. if ( !current_user_can('edit_page', $post_ID) )
  803. die(__('You are not allowed to edit this page.'));
  804. } else {
  805. if ( !current_user_can('edit_post', $post_ID) )
  806. die(__('You are not allowed to edit this post.'));
  807. }
  808. if ( $do_autosave ) {
  809. // Drafts and auto-drafts are just overwritten by autosave
  810. if ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) {
  811. $id = edit_post();
  812. } else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
  813. $revision_id = wp_create_post_autosave( $post->ID );
  814. if ( is_wp_error($revision_id) )
  815. $id = $revision_id;
  816. else
  817. $id = $post->ID;
  818. }
  819. $data = $message;
  820. } else {
  821. if ( isset( $_POST['auto_draft'] ) && '1' == $_POST['auto_draft'] )
  822. $id = 0; // This tells us it didn't actually save
  823. else
  824. $id = $post->ID;
  825. }
  826. if ( $do_lock && ( isset( $_POST['auto_draft'] ) && ( $_POST['auto_draft'] != '1' ) ) && $id && is_numeric($id) )
  827. wp_set_post_lock( $id );
  828. if ( $nonce_age == 2 ) {
  829. $supplemental['replace-autosavenonce'] = wp_create_nonce('autosave');
  830. $supplemental['replace-getpermalinknonce'] = wp_create_nonce('getpermalink');
  831. $supplemental['replace-samplepermalinknonce'] = wp_create_nonce('samplepermalink');
  832. $supplemental['replace-closedpostboxesnonce'] = wp_create_nonce('closedpostboxes');
  833. if ( $id ) {
  834. if ( $_POST['post_type'] == 'post' )
  835. $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id);
  836. elseif ( $_POST['post_type'] == 'page' )
  837. $supplemental['replace-_wpnonce'] = wp_create_nonce('update-page_' . $id);
  838. }
  839. }
  840. if ( ! empty($alert) )
  841. $supplemental['alert'] = $alert;
  842. $x = new WP_Ajax_Response( array(
  843. 'what' => 'autosave',
  844. 'id' => $id,
  845. 'data' => $id ? $data : '',
  846. 'supplemental' => $supplemental
  847. ) );
  848. $x->send();
  849. break;
  850. case 'closed-postboxes' :
  851. check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' );
  852. $closed = isset( $_POST['closed'] ) ? explode( ',', $_POST['closed']) : array();
  853. $closed = array_filter($closed);
  854. $hidden = isset( $_POST['hidden'] ) ? explode( ',', $_POST['hidden']) : array();
  855. $hidden = array_filter($hidden);
  856. $page = isset( $_POST['page'] ) ? $_POST['page'] : '';
  857. if ( !preg_match( '/^[a-z_-]+$/', $page ) )
  858. die('-1');
  859. if ( ! $user = wp_get_current_user() )
  860. die('-1');
  861. if ( is_array($closed) )
  862. update_user_option($user->ID, "closedpostboxes_$page", $closed, true);
  863. if ( is_array($hidden) ) {
  864. $hidden = array_diff( $hidden, array('submitdiv', 'linksubmitdiv', 'manage-menu', 'create-menu') ); // postboxes that are always shown
  865. update_user_option($user->ID, "metaboxhidden_$page", $hidden, true);
  866. }
  867. die('1');
  868. break;
  869. case 'hidden-columns' :
  870. check_ajax_referer( 'screen-options-nonce', 'screenoptionnonce' );
  871. $hidden = isset( $_POST['hidden'] ) ? $_POST['hidden'] : '';
  872. $hidden = explode( ',', $_POST['hidden'] );
  873. $page = isset( $_POST['page'] ) ? $_POST['page'] : '';
  874. if ( !preg_match( '/^[a-z_-]+$/', $page ) )
  875. die('-1');
  876. if ( ! $user = wp_get_current_user() )
  877. die('-1');
  878. if ( is_array($hidden) )
  879. update_user_option($user->ID, "manage{$page}columnshidden", $hidden, true);
  880. die('1');
  881. break;
  882. case 'menu-get-metabox' :
  883. if ( ! current_user_can( 'edit_theme_options' ) )
  884. die('-1');
  885. require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
  886. if ( isset( $_POST['item-type'] ) && 'post_type' == $_POST['item-type'] ) {
  887. $type = 'posttype';
  888. $callback = 'wp_nav_menu_item_post_type_meta_box';
  889. $items = (array) get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
  890. } elseif ( isset( $_POST['item-type'] ) && 'taxonomy' == $_POST['item-type'] ) {
  891. $type = 'taxonomy';
  892. $callback = 'wp_nav_menu_item_taxonomy_meta_box';
  893. $items = (array) get_taxonomies( array( 'show_ui' => true ), 'object' );
  894. }
  895. if ( ! empty( $_POST['item-object'] ) && isset( $items[$_POST['item-object']] ) ) {
  896. $item = apply_filters( 'nav_menu_meta_box_object', $items[ $_POST['item-object'] ] );
  897. ob_start();
  898. call_user_func_array($callback, array(
  899. null,
  900. array(
  901. 'id' => 'add-' . $item->name,
  902. 'title' => $item->labels->name,
  903. 'callback' => $callback,
  904. 'args' => $item,
  905. )
  906. ));
  907. $markup = ob_get_clean();
  908. echo json_encode(array(
  909. 'replace-id' => $type . '-' . $item->name,
  910. 'markup' => $markup,
  911. ));
  912. }
  913. exit;
  914. break;
  915. case 'menu-quick-search':
  916. if ( ! current_user_can( 'edit_theme_options' ) )
  917. die('-1');
  918. require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
  919. _wp_ajax_menu_quick_search( $_REQUEST );
  920. exit;
  921. break;
  922. case 'wp-link-ajax':
  923. require_once ABSPATH . 'wp-admin/includes/internal-linking.php';
  924. check_ajax_referer( 'internal-linking', '_ajax_linking_nonce' );
  925. $args = array();
  926. if ( isset( $_POST['search'] ) )
  927. $args['s'] = stripslashes( $_POST['search'] );
  928. $args['pagenum'] = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1;
  929. $results = wp_link_query( $args );
  930. if ( ! isset( $results ) )
  931. die( '0' );
  932. echo json_encode( $results );
  933. echo "\n";
  934. exit;
  935. break;
  936. case 'menu-locations-save':
  937. if ( ! current_user_can( 'edit_theme_options' ) )
  938. die('-1');
  939. check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' );
  940. if ( ! isset( $_POST['menu-locations'] ) )
  941. die('0');
  942. set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_POST['menu-locations'] ) );
  943. die('1');
  944. break;
  945. case 'meta-box-order':
  946. check_ajax_referer( 'meta-box-order' );
  947. $order = isset( $_POST['order'] ) ? (array) $_POST['order'] : false;
  948. $page_columns = isset( $_POST['page_columns'] ) ? (int) $_POST['page_columns'] : 0;
  949. $page = isset( $_POST['page'] ) ? $_POST['page'] : '';
  950. if ( !preg_match( '/^[a-z_-]+$/', $page ) )
  951. die('-1');
  952. if ( ! $user = wp_get_current_user() )
  953. die('-1');
  954. if ( $order )
  955. update_user_option($user->ID, "meta-box-order_$page", $order, true);
  956. if ( $page_columns )
  957. update_user_option($user->ID, "screen_layout_$page", $page_columns, true);
  958. die('1');
  959. break;
  960. case 'get-permalink':
  961. check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
  962. $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
  963. die(add_query_arg(array('preview' => 'true'), get_permalink($post_id)));
  964. break;
  965. case 'sample-permalink':
  966. check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
  967. $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
  968. $title = isset($_POST['new_title'])? $_POST['new_title'] : '';
  969. $slug = isset($_POST['new_slug'])? $_POST['new_slug'] : null;
  970. die(get_sample_permalink_html($post_id, $title, $slug));
  971. break;
  972. case 'inline-save':
  973. check_ajax_referer( 'inlineeditnonce', '_inline_edit' );
  974. if ( ! isset($_POST['post_ID']) || ! ( $post_ID = (int) $_POST['post_ID'] ) )
  975. exit;
  976. if ( 'page' == $_POST['post_type'] ) {
  977. if ( ! current_user_can( 'edit_page', $post_ID ) )
  978. die( __('You are not allowed to edit this page.') );
  979. } else {
  980. if ( ! current_user_can( 'edit_post', $post_ID ) )
  981. die( __('You are not allowed to edit this post.') );
  982. }
  983. set_current_screen( $_POST['screen'] );
  984. if ( $last = wp_check_post_lock( $post_ID ) ) {
  985. $last_user = get_userdata( $last );
  986. $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
  987. 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 ) );
  988. exit;
  989. }
  990. $data = &$_POST;
  991. $post = get_post( $post_ID, ARRAY_A );
  992. $post = add_magic_quotes($post); //since it is from db
  993. $data['content'] = $post['post_content'];
  994. $data['excerpt'] = $post['post_excerpt'];
  995. // rename
  996. $data['user_ID'] = $GLOBALS['user_ID'];
  997. if ( isset($data['post_parent']) )
  998. $data['parent_id'] = $data['post_parent'];
  999. // status
  1000. if ( isset($data['keep_private']) && 'private' == $data['keep_private'] )
  1001. $data['post_status'] = 'private';
  1002. else
  1003. $data['post_status'] = $data['_status'];
  1004. if ( empty($data['comment_status']) )
  1005. $data['comment_status'] = 'closed';
  1006. if ( empty($data['ping_status']) )
  1007. $data['ping_status'] = 'closed';
  1008. // update the post
  1009. edit_post();
  1010. $wp_list_table = _get_list_table('WP_Posts_List_Table');
  1011. $mode = $_POST['post_view'];
  1012. $wp_list_table->display_rows( array( get_post( $_POST['post_ID'] ) ) );
  1013. exit;
  1014. break;
  1015. case 'inline-save-tax':
  1016. check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' );
  1017. $taxonomy = sanitize_key( $_POST['taxonomy'] );
  1018. $tax = get_taxonomy( $taxonomy );
  1019. if ( ! $tax )
  1020. die( '0' );
  1021. if ( ! current_user_can( $tax->cap->edit_terms ) )
  1022. die( '-1' );
  1023. set_current_screen( 'edit-' . $taxonomy );
  1024. $wp_list_table = _get_list_table('WP_Terms_List_Table');
  1025. if ( ! isset($_POST['tax_ID']) || ! ( $id = (int) $_POST['tax_ID'] ) )
  1026. die(-1);
  1027. $tag = get_term( $id, $taxonomy );
  1028. $_POST['description'] = $tag->description;
  1029. $updated = wp_update_term($id, $taxonomy, $_POST);
  1030. if ( $updated && !is_wp_error($updated) ) {
  1031. $tag = get_term( $updated['term_id'], $taxonomy );
  1032. if ( !$tag || is_wp_error( $tag ) ) {
  1033. if ( is_wp_error($tag) && $tag->get_error_message() )
  1034. die( $tag->get_error_message() );
  1035. die( __('Item not updated.') );
  1036. }
  1037. echo $wp_list_table->single_row( $tag );
  1038. } else {
  1039. if ( is_wp_error($updated) && $updated->get_error_message() )
  1040. die( $updated->get_error_message() );
  1041. die( __('Item not updated.') );
  1042. }
  1043. exit;
  1044. break;
  1045. case 'find_posts':
  1046. check_ajax_referer( 'find-posts' );
  1047. if ( empty($_POST['ps']) )
  1048. exit;
  1049. if ( !empty($_POST['post_type']) && in_array( $_POST['post_type'], get_post_types() ) )
  1050. $what = $_POST['post_type'];
  1051. else
  1052. $what = 'post';
  1053. $s = stripslashes($_POST['ps']);
  1054. preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches);
  1055. $search_terms = array_map('_search_terms_tidy', $matches[0]);
  1056. $searchand = $search = '';
  1057. foreach ( (array) $search_terms as $term ) {
  1058. $term = esc_sql( like_escape( $term ) );
  1059. $search .= "{$searchand}(($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%'))";
  1060. $searchand = ' AND ';
  1061. }
  1062. $term = esc_sql( like_escape( $s ) );
  1063. if ( count($search_terms) > 1 && $search_terms[0] != $s )
  1064. $search .= " OR ($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%')";
  1065. $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" );
  1066. if ( ! $posts ) {
  1067. $posttype = get_post_type_object($what);
  1068. exit($posttype->labels->not_found);
  1069. }
  1070. $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>';
  1071. foreach ( $posts as $post ) {
  1072. switch ( $post->post_status ) {
  1073. case 'publish' :
  1074. case 'private' :
  1075. $stat = __('Published');
  1076. break;
  1077. case 'future' :
  1078. $stat = __('Scheduled');
  1079. break;
  1080. case 'pending' :
  1081. $stat = __('Pending Review');
  1082. break;
  1083. case 'draft' :
  1084. $stat = __('Draft');
  1085. break;
  1086. }
  1087. if ( '0000-00-00 00:00:00' == $post->post_date ) {
  1088. $time = '';
  1089. } else {
  1090. /* translators: date format in table columns, see http://php.net/date */
  1091. $time = mysql2date(__('Y/m/d'), $post->post_date);
  1092. }
  1093. $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>';
  1094. $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";
  1095. }
  1096. $html .= '</tbody></table>';
  1097. $x = new WP_Ajax_Response();
  1098. $x->add( array(
  1099. 'what' => $what,
  1100. 'data' => $html
  1101. ));
  1102. $x->send();
  1103. break;
  1104. case 'widgets-order' :
  1105. check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' );
  1106. if ( !current_user_can('edit_theme_options') )
  1107. die('-1');
  1108. unset( $_POST['savewidgets'], $_POST['action'] );
  1109. // save widgets order for all sidebars
  1110. if ( is_array($_POST['sidebars']) ) {
  1111. $sidebars = array();
  1112. foreach ( $_POST['sidebars'] as $key => $val ) {
  1113. $sb = array();
  1114. if ( !empty($val) ) {
  1115. $val = explode(',', $val);
  1116. foreach ( $val as $k => $v ) {
  1117. if ( strpos($v, 'widget-') === false )
  1118. continue;
  1119. $sb[$k] = substr($v, strpos($v, '_') + 1);
  1120. }
  1121. }
  1122. $sidebars[$key] = $sb;
  1123. }
  1124. wp_set_sidebars_widgets($sidebars);
  1125. die('1');
  1126. }
  1127. die('-1');
  1128. break;
  1129. case 'save-widget' :
  1130. check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' );
  1131. if ( !current_user_can('edit_theme_options') || !isset($_POST['id_base']) )
  1132. die('-1');
  1133. unset( $_POST['savewidgets'], $_POST['action'] );
  1134. do_action('load-widgets.php');
  1135. do_action('widgets.php');
  1136. do_action('sidebar_admin_setup');
  1137. $id_base = $_POST['id_base'];
  1138. $widget_id = $_POST['widget-id'];
  1139. $sidebar_id = $_POST['sidebar'];
  1140. $multi_number = !empty($_POST['multi_number']) ? (int) $_POST['multi_number'] : 0;
  1141. $settings = isset($_POST['widget-' . $id_base]) && is_array($_POST['widget-' . $id_base]) ? $_POST['widget-' . $id_base] : false;
  1142. $error = '<p>' . __('An error has occurred. Please reload the page and try again.') . '</p>';
  1143. $sidebars = wp_get_sidebars_widgets();
  1144. $sidebar = isset($sidebars[$sidebar_id]) ? $sidebars[$sidebar_id] : array();
  1145. // delete
  1146. if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) {
  1147. if ( !isset($wp_registered_widgets[$widget_id]) )
  1148. die($error);
  1149. $sidebar = array_diff( $sidebar, array($widget_id) );
  1150. $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1');
  1151. } elseif ( $settings && preg_match( '/__i__|%i%/', key($settings) ) ) {
  1152. if ( !$multi_number )
  1153. die($error);
  1154. $_POST['widget-' . $id_base] = array( $multi_number => array_shift($settings) );
  1155. $widget_id = $id_base . '-' . $multi_number;
  1156. $sidebar[] = $widget_id;
  1157. }
  1158. $_POST['widget-id'] = $sidebar;
  1159. foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
  1160. if ( $name == $id_base ) {
  1161. if ( !is_callable( $control['callback'] ) )
  1162. continue;
  1163. ob_start();
  1164. call_user_func_array( $control['callback'], $control['params'] );
  1165. ob_end_clean();
  1166. break;
  1167. }
  1168. }
  1169. if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) {
  1170. $sidebars[$sidebar_id] = $sidebar;
  1171. wp_set_sidebars_widgets($sidebars);
  1172. echo "deleted:$widget_id";
  1173. die();
  1174. }
  1175. if ( !empty($_POST['add_new']) )
  1176. die();
  1177. if ( $form = $wp_registered_widget_controls[$widget_id] )
  1178. call_user_func_array( $form['callback'], $form['params'] );
  1179. die();
  1180. break;
  1181. case 'image-editor':
  1182. $attachment_id = intval($_POST['postid']);
  1183. if ( empty($attachment_id) || !current_user_can('edit_post', $attachment_id) )
  1184. die('-1');
  1185. check_ajax_referer( "image_editor-$attachment_id" );
  1186. include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
  1187. $msg = false;
  1188. switch ( $_POST['do'] ) {
  1189. case 'save' :
  1190. $msg = wp_save_image($attachment_id);
  1191. $msg = json_encode($msg);
  1192. die($msg);
  1193. break;
  1194. case 'scale' :
  1195. $msg = wp_save_image($attachment_id);
  1196. break;
  1197. case 'restore' :
  1198. $msg = wp_restore_image($attachment_id);
  1199. break;
  1200. }
  1201. wp_image_editor($attachment_id, $msg);
  1202. die();
  1203. break;
  1204. case 'set-post-thumbnail':
  1205. $post_ID = intval( $_POST['post_id'] );
  1206. if ( !current_user_can( 'edit_post', $post_ID ) )
  1207. die( '-1' );
  1208. $thumbnail_id = intval( $_POST['thumbnail_id'] );
  1209. check_ajax_referer( "set_post_thumbnail-$post_ID" );
  1210. if ( $thumbnail_id == '-1' ) {
  1211. delete_post_meta( $post_ID, '_thumbnail_id' );
  1212. die( _wp_post_thumbnail_html() );
  1213. }
  1214. if ( set_post_thumbnail( $post_ID, $thumbnail_id ) )
  1215. die( _wp_post_thumbnail_html( $thumbnail_id ) );
  1216. die( '0' );
  1217. break;
  1218. case 'date_format' :
  1219. die( date_i18n( sanitize_option( 'date_format', $_POST['date'] ) ) );
  1220. break;
  1221. case 'time_format' :
  1222. die( date_i18n( sanitize_option( 'time_format', $_POST['date'] ) ) );
  1223. break;
  1224. case 'wp-fullscreen-save-post' :
  1225. if ( isset($_POST['post_ID']) )
  1226. $post_id = (int) $_POST['post_ID'];
  1227. else
  1228. $post_id = 0;
  1229. $post = null;
  1230. $post_type_object = null;
  1231. $post_type = null;
  1232. if ( $post_id ) {
  1233. $post = get_post($post_id);
  1234. if ( $post ) {
  1235. $post_type_object = get_post_type_object($post->post_type);
  1236. if ( $post_type_object ) {
  1237. $post_type = $post->post_type;
  1238. $current_screen->post_type = $post->post_type;
  1239. $current_screen->id = $current_screen->post_type;
  1240. }
  1241. }
  1242. } elseif ( isset($_POST['post_type']) ) {
  1243. $post_type_object = get_post_type_object($_POST['post_type']);
  1244. if ( $post_type_object ) {
  1245. $post_type = $post_type_object->name;
  1246. $current_screen->post_type = $post_type;
  1247. $current_screen->id = $current_screen->post_type;
  1248. }
  1249. }
  1250. check_ajax_referer('update-' . $post_type . '_' . $post_id, '_wpnonce');
  1251. $post_id = edit_post();
  1252. if ( is_wp_error($post_id) ) {
  1253. if ( $post_id->get_error_message() )
  1254. $message = $post_id->get_error_message();
  1255. else
  1256. $message = __('Save failed');
  1257. echo json_encode( array( 'message' => $message, 'last_edited' => '' ) );
  1258. die();
  1259. } else {
  1260. $message = __('Saved.');
  1261. }
  1262. if ( $post ) {
  1263. $last_date = mysql2date( get_option('date_format'), $post->post_modified );
  1264. $last_time = mysql2date( get_option('time_format'), $post->post_modified );
  1265. } else {
  1266. $last_date = date_i18n( get_option('date_format') );
  1267. $last_time = date_i18n( get_option('time_format') );
  1268. }
  1269. if ( $last_id = get_post_meta($post_id, '_edit_last', true) ) {
  1270. $last_user = get_userdata($last_id);
  1271. $last_edited = sprintf( __('Last edited by %1$s on %2$s at %3$s'), esc_html( $last_user->display_name ), $last_date, $last_time );
  1272. } else {
  1273. $last_edited = sprintf( __('Last edited on %1$s at %2$s'), $last_date, $last_time );
  1274. }
  1275. echo json_encode( array( 'message' => $message, 'last_edited' => $last_edited ) );
  1276. die();
  1277. break;
  1278. default :
  1279. do_action( 'wp_ajax_' . $_POST['action'] );
  1280. die('0');
  1281. break;
  1282. endswitch;
  1283. ?>