PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/admin-ajax.php

https://github.com/muskmelon/Greemo
PHP | 1555 lines | 1256 code | 259 blank | 40 comment | 344 complexity | 1ca59095c4a7e365761c2289aa7841ad MD5 | raw file
  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 ) || is_protected_meta( $meta->meta_key ) )
  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', '_wpnonce_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. $x = new WP_Ajax_Response();
  517. ob_start();
  518. foreach ( $wp_list_table->items as $comment ) {
  519. get_comment( $comment );
  520. $wp_list_table->single_row( $comment );
  521. }
  522. $comment_list_item = ob_get_contents();
  523. ob_end_clean();
  524. $x->add( array(
  525. 'what' => 'comments',
  526. 'data' => $comment_list_item
  527. ) );
  528. $x->send();
  529. break;
  530. case 'replyto-comment' :
  531. check_ajax_referer( $action, '_ajax_nonce-replyto-comment' );
  532. set_current_screen( 'edit-comments' );
  533. $comment_post_ID = (int) $_POST['comment_post_ID'];
  534. if ( !current_user_can( 'edit_post', $comment_post_ID ) )
  535. die('-1');
  536. $status = $wpdb->get_var( $wpdb->prepare("SELECT post_status FROM $wpdb->posts WHERE ID = %d", $comment_post_ID) );
  537. if ( empty($status) )
  538. die('1');
  539. elseif ( in_array($status, array('draft', 'pending', 'trash') ) )
  540. die( __('Error: you are replying to a comment on a draft post.') );
  541. $user = wp_get_current_user();
  542. if ( $user->ID ) {
  543. $comment_author = $wpdb->escape($user->display_name);
  544. $comment_author_email = $wpdb->escape($user->user_email);
  545. $comment_author_url = $wpdb->escape($user->user_url);
  546. $comment_content = trim($_POST['content']);
  547. if ( current_user_can('unfiltered_html') ) {
  548. if ( wp_create_nonce('unfiltered-html-comment_' . $comment_post_ID) != $_POST['_wp_unfiltered_html_comment'] ) {
  549. kses_remove_filters(); // start with a clean slate
  550. kses_init_filters(); // set up the filters
  551. }
  552. }
  553. } else {
  554. die( __('Sorry, you must be logged in to reply to a comment.') );
  555. }
  556. if ( '' == $comment_content )
  557. die( __('Error: please type a comment.') );
  558. $comment_parent = absint($_POST['comment_ID']);
  559. $comment_auto_approved = false;
  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. // automatically approve parent comment
  566. if ( !empty($_POST['approve_parent']) ) {
  567. $parent = get_comment( $comment_parent );
  568. if ( $parent && $parent->comment_approved === '0' && $parent->comment_post_ID == $comment_post_ID ) {
  569. if ( wp_set_comment_status( $parent->comment_ID, 'approve' ) )
  570. $comment_auto_approved = true;
  571. }
  572. }
  573. ob_start();
  574. if ( 'dashboard' == $_REQUEST['mode'] ) {
  575. require_once( ABSPATH . 'wp-admin/includes/dashboard.php' );
  576. _wp_dashboard_recent_comments_row( $comment );
  577. } else {
  578. if ( 'single' == $_REQUEST['mode'] ) {
  579. $wp_list_table = _get_list_table('WP_Post_Comments_List_Table');
  580. } else {
  581. $wp_list_table = _get_list_table('WP_Comments_List_Table');
  582. }
  583. $wp_list_table->single_row( $comment );
  584. }
  585. $comment_list_item = ob_get_contents();
  586. ob_end_clean();
  587. $response = array(
  588. 'what' => 'comment',
  589. 'id' => $comment->comment_ID,
  590. 'data' => $comment_list_item,
  591. 'position' => $position
  592. );
  593. if ( $comment_auto_approved )
  594. $response['supplemental'] = array( 'parent_approved' => $parent->comment_ID );
  595. $x = new WP_Ajax_Response();
  596. $x->add( $response );
  597. $x->send();
  598. break;
  599. case 'edit-comment' :
  600. check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' );
  601. set_current_screen( 'edit-comments' );
  602. $comment_post_ID = (int) $_POST['comment_post_ID'];
  603. if ( ! current_user_can( 'edit_post', $comment_post_ID ) )
  604. die('-1');
  605. if ( '' == $_POST['content'] )
  606. die( __('Error: please type a comment.') );
  607. $comment_id = (int) $_POST['comment_ID'];
  608. $_POST['comment_status'] = $_POST['status'];
  609. edit_comment();
  610. $position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1';
  611. $comments_status = isset($_POST['comments_listing']) ? $_POST['comments_listing'] : '';
  612. $checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0;
  613. $wp_list_table = _get_list_table( $checkbox ? 'WP_Comments_List_Table' : 'WP_Post_Comments_List_Table' );
  614. ob_start();
  615. $wp_list_table->single_row( get_comment( $comment_id ) );
  616. $comment_list_item = ob_get_contents();
  617. ob_end_clean();
  618. $x = new WP_Ajax_Response();
  619. $x->add( array(
  620. 'what' => 'edit_comment',
  621. 'id' => $comment->comment_ID,
  622. 'data' => $comment_list_item,
  623. 'position' => $position
  624. ));
  625. $x->send();
  626. break;
  627. case 'add-menu-item' :
  628. if ( ! current_user_can( 'edit_theme_options' ) )
  629. die('-1');
  630. check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' );
  631. require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
  632. // For performance reasons, we omit some object properties from the checklist.
  633. // The following is a hacky way to restore them when adding non-custom items.
  634. $menu_items_data = array();
  635. foreach ( (array) $_POST['menu-item'] as $menu_item_data ) {
  636. if (
  637. ! empty( $menu_item_data['menu-item-type'] ) &&
  638. 'custom' != $menu_item_data['menu-item-type'] &&
  639. ! empty( $menu_item_data['menu-item-object-id'] )
  640. ) {
  641. switch( $menu_item_data['menu-item-type'] ) {
  642. case 'post_type' :
  643. $_object = get_post( $menu_item_data['menu-item-object-id'] );
  644. break;
  645. case 'taxonomy' :
  646. $_object = get_term( $menu_item_data['menu-item-object-id'], $menu_item_data['menu-item-object'] );
  647. break;
  648. }
  649. $_menu_items = array_map( 'wp_setup_nav_menu_item', array( $_object ) );
  650. $_menu_item = array_shift( $_menu_items );
  651. // Restore the missing menu item properties
  652. $menu_item_data['menu-item-description'] = $_menu_item->description;
  653. }
  654. $menu_items_data[] = $menu_item_data;
  655. }
  656. $item_ids = wp_save_nav_menu_items( 0, $menu_items_data );
  657. if ( is_wp_error( $item_ids ) )
  658. die('-1');
  659. foreach ( (array) $item_ids as $menu_item_id ) {
  660. $menu_obj = get_post( $menu_item_id );
  661. if ( ! empty( $menu_obj->ID ) ) {
  662. $menu_obj = wp_setup_nav_menu_item( $menu_obj );
  663. $menu_obj->label = $menu_obj->title; // don't show "(pending)" in ajax-added items
  664. $menu_items[] = $menu_obj;
  665. }
  666. }
  667. if ( ! empty( $menu_items ) ) {
  668. $args = array(
  669. 'after' => '',
  670. 'before' => '',
  671. 'link_after' => '',
  672. 'link_before' => '',
  673. 'walker' => new Walker_Nav_Menu_Edit,
  674. );
  675. echo walk_nav_menu_tree( $menu_items, 0, (object) $args );
  676. }
  677. break;
  678. case 'add-meta' :
  679. check_ajax_referer( 'add-meta', '_ajax_nonce-add-meta' );
  680. $c = 0;
  681. $pid = (int) $_POST['post_id'];
  682. $post = get_post( $pid );
  683. if ( isset($_POST['metakeyselect']) || isset($_POST['metakeyinput']) ) {
  684. if ( !current_user_can( 'edit_post', $pid ) )
  685. die('-1');
  686. if ( isset($_POST['metakeyselect']) && '#NONE#' == $_POST['metakeyselect'] && empty($_POST['metakeyinput']) )
  687. die('1');
  688. if ( $post->post_status == 'auto-draft' ) {
  689. $save_POST = $_POST; // Backup $_POST
  690. $_POST = array(); // Make it empty for edit_post()
  691. $_POST['action'] = 'draft'; // Warning fix
  692. $_POST['post_ID'] = $pid;
  693. $_POST['post_type'] = $post->post_type;
  694. $_POST['post_status'] = 'draft';
  695. $now = current_time('timestamp', 1);
  696. $_POST['post_title'] = sprintf('Draft created on %s at %s', date(get_option('date_format'), $now), date(get_option('time_format'), $now));
  697. if ( $pid = edit_post() ) {
  698. if ( is_wp_error( $pid ) ) {
  699. $x = new WP_Ajax_Response( array(
  700. 'what' => 'meta',
  701. 'data' => $pid
  702. ) );
  703. $x->send();
  704. }
  705. $_POST = $save_POST; // Now we can restore original $_POST again
  706. if ( !$mid = add_meta( $pid ) )
  707. die(__('Please provide a custom field value.'));
  708. } else {
  709. die('0');
  710. }
  711. } else if ( !$mid = add_meta( $pid ) ) {
  712. die(__('Please provide a custom field value.'));
  713. }
  714. $meta = get_post_meta_by_id( $mid );
  715. $pid = (int) $meta->post_id;
  716. $meta = get_object_vars( $meta );
  717. $x = new WP_Ajax_Response( array(
  718. 'what' => 'meta',
  719. 'id' => $mid,
  720. 'data' => _list_meta_row( $meta, $c ),
  721. 'position' => 1,
  722. 'supplemental' => array('postid' => $pid)
  723. ) );
  724. } else { // Update?
  725. $mid = (int) array_pop( array_keys($_POST['meta']) );
  726. $key = $_POST['meta'][$mid]['key'];
  727. $value = $_POST['meta'][$mid]['value'];
  728. if ( '' == trim($key) )
  729. die(__('Please provide a custom field name.'));
  730. if ( '' == trim($value) )
  731. die(__('Please provide a custom field value.'));
  732. if ( !$meta = get_post_meta_by_id( $mid ) )
  733. die('0'); // if meta doesn't exist
  734. if ( !current_user_can( 'edit_post', $meta->post_id ) )
  735. die('-1');
  736. if ( is_protected_meta( $meta->meta_key ) )
  737. die('-1');
  738. if ( $meta->meta_value != stripslashes($value) || $meta->meta_key != stripslashes($key) ) {
  739. if ( !$u = update_meta( $mid, $key, $value ) )
  740. die('0'); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems).
  741. }
  742. $key = stripslashes($key);
  743. $value = stripslashes($value);
  744. $x = new WP_Ajax_Response( array(
  745. 'what' => 'meta',
  746. 'id' => $mid, 'old_id' => $mid,
  747. 'data' => _list_meta_row( array(
  748. 'meta_key' => $key,
  749. 'meta_value' => $value,
  750. 'meta_id' => $mid
  751. ), $c ),
  752. 'position' => 0,
  753. 'supplemental' => array('postid' => $meta->post_id)
  754. ) );
  755. }
  756. $x->send();
  757. break;
  758. case 'add-user' :
  759. check_ajax_referer( $action );
  760. if ( !current_user_can('create_users') )
  761. die('-1');
  762. if ( !$user_id = add_user() )
  763. die('0');
  764. elseif ( is_wp_error( $user_id ) ) {
  765. $x = new WP_Ajax_Response( array(
  766. 'what' => 'user',
  767. 'id' => $user_id
  768. ) );
  769. $x->send();
  770. }
  771. $user_object = new WP_User( $user_id );
  772. $wp_list_table = _get_list_table('WP_Users_List_Table');
  773. $x = new WP_Ajax_Response( array(
  774. 'what' => 'user',
  775. 'id' => $user_id,
  776. 'data' => $wp_list_table->single_row( $user_object, '', $user_object->roles[0] ),
  777. 'supplemental' => array(
  778. 'show-link' => sprintf(__( 'User <a href="#%s">%s</a> added' ), "user-$user_id", $user_object->user_login),
  779. 'role' => $user_object->roles[0]
  780. )
  781. ) );
  782. $x->send();
  783. break;
  784. case 'autosave' : // The name of this action is hardcoded in edit_post()
  785. define( 'DOING_AUTOSAVE', true );
  786. $nonce_age = check_ajax_referer( 'autosave', 'autosavenonce' );
  787. $_POST['post_category'] = explode(",", $_POST['catslist']);
  788. if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
  789. unset($_POST['post_category']);
  790. $do_autosave = (bool) $_POST['autosave'];
  791. $do_lock = true;
  792. $data = $alert = '';
  793. /* translators: draft saved date format, see http://php.net/date */
  794. $draft_saved_date_format = __('g:i:s a');
  795. /* translators: %s: date and time */
  796. $message = sprintf( __('Draft saved at %s.'), date_i18n( $draft_saved_date_format ) );
  797. $supplemental = array();
  798. if ( isset($login_grace_period) )
  799. $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() ) );
  800. $id = $revision_id = 0;
  801. $post_ID = (int) $_POST['post_ID'];
  802. $_POST['ID'] = $post_ID;
  803. $post = get_post($post_ID);
  804. if ( 'auto-draft' == $post->post_status )
  805. $_POST['post_status'] = 'draft';
  806. if ( $last = wp_check_post_lock( $post->ID ) ) {
  807. $do_autosave = $do_lock = false;
  808. $last_user = get_userdata( $last );
  809. $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
  810. $data = __( 'Autosave disabled.' );
  811. $supplemental['disable_autosave'] = 'disable';
  812. $alert .= sprintf( __( '%s is currently editing this article. If you update it, you will overwrite the changes.' ), esc_html( $last_user_name ) );
  813. }
  814. if ( 'page' == $post->post_type ) {
  815. if ( !current_user_can('edit_page', $post_ID) )
  816. die(__('You are not allowed to edit this page.'));
  817. } else {
  818. if ( !current_user_can('edit_post', $post_ID) )
  819. die(__('You are not allowed to edit this post.'));
  820. }
  821. if ( $do_autosave ) {
  822. // Drafts and auto-drafts are just overwritten by autosave
  823. if ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) {
  824. $id = edit_post();
  825. } else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
  826. $revision_id = wp_create_post_autosave( $post->ID );
  827. if ( is_wp_error($revision_id) )
  828. $id = $revision_id;
  829. else
  830. $id = $post->ID;
  831. }
  832. $data = $message;
  833. } else {
  834. if ( isset( $_POST['auto_draft'] ) && '1' == $_POST['auto_draft'] )
  835. $id = 0; // This tells us it didn't actually save
  836. else
  837. $id = $post->ID;
  838. }
  839. if ( $do_lock && ( isset( $_POST['auto_draft'] ) && ( $_POST['auto_draft'] != '1' ) ) && $id && is_numeric($id) )
  840. wp_set_post_lock( $id );
  841. if ( $nonce_age == 2 ) {
  842. $supplemental['replace-autosavenonce'] = wp_create_nonce('autosave');
  843. $supplemental['replace-getpermalinknonce'] = wp_create_nonce('getpermalink');
  844. $supplemental['replace-samplepermalinknonce'] = wp_create_nonce('samplepermalink');
  845. $supplemental['replace-closedpostboxesnonce'] = wp_create_nonce('closedpostboxes');
  846. if ( $id ) {
  847. if ( $_POST['post_type'] == 'post' )
  848. $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id);
  849. elseif ( $_POST['post_type'] == 'page' )
  850. $supplemental['replace-_wpnonce'] = wp_create_nonce('update-page_' . $id);
  851. }
  852. }
  853. if ( ! empty($alert) )
  854. $supplemental['alert'] = $alert;
  855. $x = new WP_Ajax_Response( array(
  856. 'what' => 'autosave',
  857. 'id' => $id,
  858. 'data' => $id ? $data : '',
  859. 'supplemental' => $supplemental
  860. ) );
  861. $x->send();
  862. break;
  863. case 'closed-postboxes' :
  864. check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' );
  865. $closed = isset( $_POST['closed'] ) ? explode( ',', $_POST['closed']) : array();
  866. $closed = array_filter($closed);
  867. $hidden = isset( $_POST['hidden'] ) ? explode( ',', $_POST['hidden']) : array();
  868. $hidden = array_filter($hidden);
  869. $page = isset( $_POST['page'] ) ? $_POST['page'] : '';
  870. if ( !preg_match( '/^[a-z_-]+$/', $page ) )
  871. die('-1');
  872. if ( ! $user = wp_get_current_user() )
  873. die('-1');
  874. if ( is_array($closed) )
  875. update_user_option($user->ID, "closedpostboxes_$page", $closed, true);
  876. if ( is_array($hidden) ) {
  877. $hidden = array_diff( $hidden, array('submitdiv', 'linksubmitdiv', 'manage-menu', 'create-menu') ); // postboxes that are always shown
  878. update_user_option($user->ID, "metaboxhidden_$page", $hidden, true);
  879. }
  880. die('1');
  881. break;
  882. case 'hidden-columns' :
  883. check_ajax_referer( 'screen-options-nonce', 'screenoptionnonce' );
  884. $hidden = isset( $_POST['hidden'] ) ? $_POST['hidden'] : '';
  885. $hidden = explode( ',', $_POST['hidden'] );
  886. $page = isset( $_POST['page'] ) ? $_POST['page'] : '';
  887. if ( !preg_match( '/^[a-z_-]+$/', $page ) )
  888. die('-1');
  889. if ( ! $user = wp_get_current_user() )
  890. die('-1');
  891. if ( is_array($hidden) )
  892. update_user_option($user->ID, "manage{$page}columnshidden", $hidden, true);
  893. die('1');
  894. break;
  895. case 'menu-get-metabox' :
  896. if ( ! current_user_can( 'edit_theme_options' ) )
  897. die('-1');
  898. require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
  899. if ( isset( $_POST['item-type'] ) && 'post_type' == $_POST['item-type'] ) {
  900. $type = 'posttype';
  901. $callback = 'wp_nav_menu_item_post_type_meta_box';
  902. $items = (array) get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
  903. } elseif ( isset( $_POST['item-type'] ) && 'taxonomy' == $_POST['item-type'] ) {
  904. $type = 'taxonomy';
  905. $callback = 'wp_nav_menu_item_taxonomy_meta_box';
  906. $items = (array) get_taxonomies( array( 'show_ui' => true ), 'object' );
  907. }
  908. if ( ! empty( $_POST['item-object'] ) && isset( $items[$_POST['item-object']] ) ) {
  909. $item = apply_filters( 'nav_menu_meta_box_object', $items[ $_POST['item-object'] ] );
  910. ob_start();
  911. call_user_func_array($callback, array(
  912. null,
  913. array(
  914. 'id' => 'add-' . $item->name,
  915. 'title' => $item->labels->name,
  916. 'callback' => $callback,
  917. 'args' => $item,
  918. )
  919. ));
  920. $markup = ob_get_clean();
  921. echo json_encode(array(
  922. 'replace-id' => $type . '-' . $item->name,
  923. 'markup' => $markup,
  924. ));
  925. }
  926. exit;
  927. break;
  928. case 'menu-quick-search':
  929. if ( ! current_user_can( 'edit_theme_options' ) )
  930. die('-1');
  931. require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
  932. _wp_ajax_menu_quick_search( $_REQUEST );
  933. exit;
  934. break;
  935. case 'wp-link-ajax':
  936. require_once ABSPATH . 'wp-admin/includes/internal-linking.php';
  937. check_ajax_referer( 'internal-linking', '_ajax_linking_nonce' );
  938. $args = array();
  939. if ( isset( $_POST['search'] ) )
  940. $args['s'] = stripslashes( $_POST['search'] );
  941. $args['pagenum'] = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1;
  942. $results = wp_link_query( $args );
  943. if ( ! isset( $results ) )
  944. die( '0' );
  945. echo json_encode( $results );
  946. echo "\n";
  947. exit;
  948. break;
  949. case 'menu-locations-save':
  950. if ( ! current_user_can( 'edit_theme_options' ) )
  951. die('-1');
  952. check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' );
  953. if ( ! isset( $_POST['menu-locations'] ) )
  954. die('0');
  955. set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_POST['menu-locations'] ) );
  956. die('1');
  957. break;
  958. case 'meta-box-order':
  959. check_ajax_referer( 'meta-box-order' );
  960. $order = isset( $_POST['order'] ) ? (array) $_POST['order'] : false;
  961. $page_columns = isset( $_POST['page_columns'] ) ? (int) $_POST['page_columns'] : 0;
  962. $page = isset( $_POST['page'] ) ? $_POST['page'] : '';
  963. if ( !preg_match( '/^[a-z_-]+$/', $page ) )
  964. die('-1');
  965. if ( ! $user = wp_get_current_user() )
  966. die('-1');
  967. if ( $order )
  968. update_user_option($user->ID, "meta-box-order_$page", $order, true);
  969. if ( $page_columns )
  970. update_user_option($user->ID, "screen_layout_$page", $page_columns, true);
  971. die('1');
  972. break;
  973. case 'get-permalink':
  974. check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
  975. $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
  976. die(add_query_arg(array('preview' => 'true'), get_permalink($post_id)));
  977. break;
  978. case 'sample-permalink':
  979. check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
  980. $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
  981. $title = isset($_POST['new_title'])? $_POST['new_title'] : '';
  982. $slug = isset($_POST['new_slug'])? $_POST['new_slug'] : null;
  983. die(get_sample_permalink_html($post_id, $title, $slug));
  984. break;
  985. case 'inline-save':
  986. check_ajax_referer( 'inlineeditnonce', '_inline_edit' );
  987. if ( ! isset($_POST['post_ID']) || ! ( $post_ID = (int) $_POST['post_ID'] ) )
  988. exit;
  989. if ( 'page' == $_POST['post_type'] ) {
  990. if ( ! current_user_can( 'edit_page', $post_ID ) )
  991. die( __('You are not allowed to edit this page.') );
  992. } else {
  993. if ( ! current_user_can( 'edit_post', $post_ID ) )
  994. die( __('You are not allowed to edit this post.') );
  995. }
  996. set_current_screen( $_POST['screen'] );
  997. if ( $last = wp_check_post_lock( $post_ID ) ) {
  998. $last_user = get_userdata( $last );
  999. $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
  1000. 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 ) );
  1001. exit;
  1002. }
  1003. $data = &$_POST;
  1004. $post = get_post( $post_ID, ARRAY_A );
  1005. $post = add_magic_quotes($post); //since it is from db
  1006. $data['content'] = $post['post_content'];
  1007. $data['excerpt'] = $post['post_excerpt'];
  1008. // rename
  1009. $data['user_ID'] = $GLOBALS['user_ID'];
  1010. if ( isset($data['post_parent']) )
  1011. $data['parent_id'] = $data['post_parent'];
  1012. // status
  1013. if ( isset($data['keep_private']) && 'private' == $data['keep_private'] )
  1014. $data['post_status'] = 'private';
  1015. else
  1016. $data['post_status'] = $data['_status'];
  1017. if ( empty($data['comment_status']) )
  1018. $data['comment_status'] = 'closed';
  1019. if ( empty($data['ping_status']) )
  1020. $data['ping_status'] = 'closed';
  1021. // update the post
  1022. edit_post();
  1023. $wp_list_table = _get_list_table('WP_Posts_List_Table');
  1024. $mode = $_POST['post_view'];
  1025. $wp_list_table->display_rows( array( get_post( $_POST['post_ID'] ) ) );
  1026. exit;
  1027. break;
  1028. case 'inline-save-tax':
  1029. check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' );
  1030. $taxonomy = sanitize_key( $_POST['taxonomy'] );
  1031. $tax = get_taxonomy( $taxonomy );
  1032. if ( ! $tax )
  1033. die( '0' );
  1034. if ( ! current_user_can( $tax->cap->edit_terms ) )
  1035. die( '-1' );
  1036. set_current_screen( 'edit-' . $taxonomy );
  1037. $wp_list_table = _get_list_table('WP_Terms_List_Table');
  1038. if ( ! isset($_POST['tax_ID']) || ! ( $id = (int) $_POST['tax_ID'] ) )
  1039. die(-1);
  1040. $tag = get_term( $id, $taxonomy );
  1041. $_POST['description'] = $tag->description;
  1042. $updated = wp_update_term($id, $taxonomy, $_POST);
  1043. if ( $updated && !is_wp_error($updated) ) {
  1044. $tag = get_term( $updated['term_id'], $taxonomy );
  1045. if ( !$tag || is_wp_error( $tag ) ) {
  1046. if ( is_wp_error($tag) && $tag->get_error_message() )
  1047. die( $tag->get_error_message() );
  1048. die( __('Item not updated.') );
  1049. }
  1050. echo $wp_list_table->single_row( $tag );
  1051. } else {
  1052. if ( is_wp_error($updated) && $updated->get_error_message() )
  1053. die( $updated->get_error_message() );
  1054. die( __('Item not updated.') );
  1055. }
  1056. exit;
  1057. break;
  1058. case 'find_posts':
  1059. check_ajax_referer( 'find-posts' );
  1060. if ( empty($_POST['ps']) )
  1061. exit;
  1062. if ( !empty($_POST['post_type']) && in_array( $_POST['post_type'], get_post_types() ) )
  1063. $what = $_POST['post_type'];
  1064. else
  1065. $what = 'post';
  1066. $s = stripslashes($_POST['ps']);
  1067. preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches);
  1068. $search_terms = array_map('_search_terms_tidy', $matches[0]);
  1069. $searchand = $search = '';
  1070. foreach ( (array) $search_terms as $term ) {
  1071. $term = esc_sql( like_escape( $term ) );
  1072. $search .= "{$searchand}(($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%'))";
  1073. $searchand = ' AND ';
  1074. }
  1075. $term = esc_sql( like_escape( $s ) );
  1076. if ( count($search_terms) > 1 && $search_terms[0] != $s )
  1077. $search .= " OR ($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%')";
  1078. $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" );
  1079. if ( ! $posts ) {
  1080. $posttype = get_post_type_object($what);
  1081. exit($posttype->labels->not_found);
  1082. }
  1083. $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>';
  1084. foreach ( $posts as $post ) {
  1085. switch ( $post->post_status ) {
  1086. case 'publish' :
  1087. case 'private' :
  1088. $stat = __('Published');
  1089. break;
  1090. case 'future' :
  1091. $stat = __('Scheduled');
  1092. break;
  1093. case 'pending' :
  1094. $stat = __('Pending Review');
  1095. break;
  1096. case 'draft' :
  1097. $stat = __('Draft');
  1098. break;
  1099. }
  1100. if ( '0000-00-00 00:00:00' == $post->post_date ) {
  1101. $time = '';
  1102. } else {
  1103. /* translators: date format in table columns, see http://php.net/date */
  1104. $time = mysql2date(__('Y/m/d'), $post->post_date);
  1105. }
  1106. $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>';
  1107. $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";
  1108. }
  1109. $html .= '</tbody></table>';
  1110. $x = new WP_Ajax_Response();
  1111. $x->add( array(
  1112. 'what' => $what,
  1113. 'data' => $html
  1114. ));
  1115. $x->send();
  1116. break;
  1117. case 'widgets-order' :
  1118. check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' );
  1119. if ( !current_user_can('edit_theme_options') )
  1120. die('-1');
  1121. unset( $_POST['savewidgets'], $_POST['action'] );
  1122. // save widgets order for all sidebars
  1123. if ( is_array($_POST['sidebars']) ) {
  1124. $sidebars = array();
  1125. foreach ( $_POST['sidebars'] as $key => $val ) {
  1126. $sb = array();
  1127. if ( !empty($val) ) {
  1128. $val = explode(',', $val);
  1129. foreach ( $val as $k => $v ) {
  1130. if ( strpos($v, 'widget-') === false )
  1131. continue;
  1132. $sb[$k] = substr($v, strpos($v, '_') + 1);
  1133. }
  1134. }
  1135. $sidebars[$key] = $sb;
  1136. }
  1137. wp_set_sidebars_widgets($sidebars);
  1138. die('1');
  1139. }
  1140. die('-1');
  1141. break;
  1142. case 'save-widget' :
  1143. check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' );
  1144. if ( !current_user_can('edit_theme_options') || !isset($_POST['id_base']) )
  1145. die('-1');
  1146. unset( $_POST['savewidgets'], $_POST['action'] );
  1147. do_action('load-widgets.php');
  1148. do_action('widgets.php');
  1149. do_action('sidebar_admin_setup');
  1150. $id_base = $_POST['id_base'];
  1151. $widget_id = $_POST['widget-id'];
  1152. $sidebar_id = $_POST['sidebar'];
  1153. $multi_number = !empty($_POST['multi_number']) ? (int) $_POST['multi_number'] : 0;
  1154. $settings = isset($_POST['widget-' . $id_base]) && is_array($_POST['widget-' . $id_base]) ? $_POST['widget-' . $id_base] : false;
  1155. $error = '<p>' . __('An error has occurred. Please reload the page and try again.') . '</p>';
  1156. $sidebars = wp_get_sidebars_widgets();
  1157. $sidebar = isset($sidebars[$sidebar_id]) ? $sidebars[$sidebar_id] : array();
  1158. // delete
  1159. if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) {
  1160. if ( !isset($wp_registered_widgets[$widget_id]) )
  1161. die($error);
  1162. $sidebar = array_diff( $sidebar, array($widget_id) );
  1163. $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1');
  1164. } elseif ( $settings && preg_match( '/__i__|%i%/', key($settings) ) ) {
  1165. if ( !$multi_number )
  1166. die($error);
  1167. $_POST['widget-' . $id_base] = array( $multi_number => array_shift($settings) );
  1168. $widget_id = $id_base . '-' . $multi_number;
  1169. $sidebar[] = $widget_id;
  1170. }
  1171. $_POST['widget-id'] = $sidebar;
  1172. foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
  1173. if ( $name == $id_base ) {
  1174. if ( !is_callable( $control['callback'] ) )
  1175. continue;
  1176. ob_start();
  1177. call_user_func_array( $control['callback'], $control['params'] );
  1178. ob_end_clean();
  1179. break;
  1180. }
  1181. }
  1182. if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) {
  1183. $sidebars[$sidebar_id] = $sidebar;
  1184. wp_set_sidebars_widgets($sidebars);
  1185. echo "deleted:$widget_id";
  1186. die();
  1187. }
  1188. if ( !empty($_POST['add_new']) )
  1189. die();
  1190. if ( $form = $wp_registered_widget_controls[$widget_id] )
  1191. call_user_func_array( $form['callback'], $form['params'] );
  1192. die();
  1193. break;
  1194. case 'image-editor':
  1195. $attachment_id = intval($_POST['postid']);
  1196. if ( empty($attachment_id) || !current_user_can('edit_post', $attachment_id) )
  1197. die('-1');
  1198. check_ajax_referer( "image_editor-$attachment_id" );
  1199. include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
  1200. $msg = false;
  1201. switch ( $_POST['do'] ) {
  1202. case 'save' :
  1203. $msg = wp_save_image($attachment_id);
  1204. $msg = json_encode($msg);
  1205. die($msg);
  1206. break;
  1207. case 'scale' :
  1208. $msg = wp_save_image($attachment_id);
  1209. break;
  1210. case 'restore' :
  1211. $msg = wp_restore_image($attachment_id);
  1212. break;
  1213. }
  1214. wp_image_editor($attachment_id, $msg);
  1215. die();
  1216. break;
  1217. case 'set-post-thumbnail':
  1218. $post_ID = intval( $_POST['post_id'] );
  1219. if ( !current_user_can( 'edit_post', $post_ID ) )
  1220. die( '-1' );
  1221. $thumbnail_id = intval( $_POST['thumbnail_id'] );
  1222. check_ajax_referer( "set_post_thumbnail-$post_ID" );
  1223. if ( $thumbnail_id == '-1' ) {
  1224. delete_post_meta( $post_ID, '_thumbnail_id' );
  1225. die( _wp_post_thumbnail_html() );
  1226. }
  1227. if ( set_post_thumbnail( $post_ID, $thumbnail_id ) )
  1228. die( _wp_post_thumbnail_html( $thumbnail_id ) );
  1229. die( '0' );
  1230. break;
  1231. case 'date_format' :
  1232. die( date_i18n( sanitize_option( 'date_format', $_POST['date'] ) ) );
  1233. break;
  1234. case 'time_format' :
  1235. die( date_i18n( sanitize_option( 'time_format', $_POST['date'] ) ) );
  1236. break;
  1237. case 'wp-fullscreen-save-post' :
  1238. if ( isset($_POST['post_ID']) )
  1239. $post_id = (int) $_POST['post_ID'];
  1240. else
  1241. $post_id = 0;
  1242. $post = null;
  1243. $post_type_object = null;
  1244. $post_type = null;
  1245. if ( $post_id ) {
  1246. $post = get_post($post_id);
  1247. if ( $post ) {
  1248. $post_type_object = get_post_type_object($post->post_type);
  1249. if ( $post_type_object ) {
  1250. $post_type = $post->post_type;
  1251. $current_screen->post_type = $post->post_type;
  1252. $current_screen->id = $current_screen->post_type;
  1253. }
  1254. }
  1255. } elseif ( isset($_POST['post_type']) ) {
  1256. $post_type_object = get_post_type_object($_POST['post_type']);
  1257. if ( $post_type_object ) {
  1258. $post_type = $post_type_object->name;
  1259. $current_screen->post_type = $post_type;
  1260. $current_screen->id = $current_screen->post_type;
  1261. }
  1262. }
  1263. check_ajax_referer('update-' . $post_type . '_' . $post_id, '_wpnonce');
  1264. $post_id = edit_post();
  1265. if ( is_wp_error($post_id) ) {
  1266. if ( $post_id->get_error_message() )
  1267. $message = $post_id->get_error_message();
  1268. else
  1269. $message = __('Save failed');
  1270. echo json_encode( array( 'message' => $message, 'last_edited' => '' ) );
  1271. die();
  1272. } else {
  1273. $message = __('Saved.');
  1274. }
  1275. if ( $post ) {
  1276. $last_date = mysql2date( get_option('date_format'), $post->post_modified );
  1277. $last_time = mysql2date( get_option('time_format'), $post->post_modified );
  1278. } else {
  1279. $last_date = date_i18n( get_option('date_format') );
  1280. $last_time = date_i18n( get_option('time_format') );
  1281. }
  1282. if ( $last_id = get_post_meta($post_id, '_edit_last', true) ) {
  1283. $last_user = get_userdata($last_id);
  1284. $last_edited = sprintf( __('Last edited by %1$s on %2$s at %3$s'), esc_html( $last_user->display_name ), $last_date, $last_time );
  1285. } else {
  1286. $last_edited = sprintf( __('Last edited on %1$s at %2$s'), $last_date, $last_time );
  1287. }
  1288. echo json_encode( array( 'message' => $message, 'last_edited' => $last_edited ) );
  1289. die();
  1290. break;
  1291. default :
  1292. do_action( 'wp_ajax_' . $_POST['action'] );
  1293. die('0');
  1294. break;
  1295. endswitch;
  1296. ?>