PageRenderTime 56ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/admin-ajax.php

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