PageRenderTime 59ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/admin-ajax.php

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