PageRenderTime 71ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/admin-ajax.php

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