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

/wp-admin/includes/ajax-actions.php

https://gitlab.com/Gashler/sg
PHP | 1780 lines | 1127 code | 296 blank | 357 comment | 315 complexity | 23b720273a37848f34be947b8bf56523 MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress Core Ajax Handlers.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. //
  9. // No-privilege Ajax handlers.
  10. //
  11. /**
  12. * Ajax handler for the Heartbeat API in
  13. * the no-privilege context.
  14. *
  15. * Runs when the user is not logged in.
  16. *
  17. * @since 3.6.0
  18. */
  19. function wp_ajax_nopriv_heartbeat() {
  20. $response = array();
  21. // screen_id is the same as $current_screen->id and the JS global 'pagenow'.
  22. if ( ! empty($_POST['screen_id']) )
  23. $screen_id = sanitize_key($_POST['screen_id']);
  24. else
  25. $screen_id = 'front';
  26. if ( ! empty($_POST['data']) ) {
  27. $data = wp_unslash( (array) $_POST['data'] );
  28. /**
  29. * Filter Heartbeat AJAX response in no-privilege environments.
  30. *
  31. * @since 3.6.0
  32. *
  33. * @param array|object $response The no-priv Heartbeat response object or array.
  34. * @param array $data An array of data passed via $_POST.
  35. * @param string $screen_id The screen id.
  36. */
  37. $response = apply_filters( 'heartbeat_nopriv_received', $response, $data, $screen_id );
  38. }
  39. /**
  40. * Filter Heartbeat AJAX response when no data is passed.
  41. *
  42. * @since 3.6.0
  43. *
  44. * @param array|object $response The Heartbeat response object or array.
  45. * @param string $screen_id The screen id.
  46. */
  47. $response = apply_filters( 'heartbeat_nopriv_send', $response, $screen_id );
  48. /**
  49. * Fires when Heartbeat ticks in no-privilege environments.
  50. *
  51. * Allows the transport to be easily replaced with long-polling.
  52. *
  53. * @since 3.6.0
  54. *
  55. * @param array|object $response The no-priv Heartbeat response.
  56. * @param string $screen_id The screen id.
  57. */
  58. do_action( 'heartbeat_nopriv_tick', $response, $screen_id );
  59. // Send the current time according to the server.
  60. $response['server_time'] = time();
  61. wp_send_json($response);
  62. }
  63. //
  64. // GET-based Ajax handlers.
  65. //
  66. /**
  67. * Ajax handler for fetching a list table.
  68. *
  69. * @since 3.1.0
  70. *
  71. * @global WP_List_Table $wp_list_table
  72. */
  73. function wp_ajax_fetch_list() {
  74. global $wp_list_table;
  75. $list_class = $_GET['list_args']['class'];
  76. check_ajax_referer( "fetch-list-$list_class", '_ajax_fetch_list_nonce' );
  77. $wp_list_table = _get_list_table( $list_class, array( 'screen' => $_GET['list_args']['screen']['id'] ) );
  78. if ( ! $wp_list_table )
  79. wp_die( 0 );
  80. if ( ! $wp_list_table->ajax_user_can() )
  81. wp_die( -1 );
  82. $wp_list_table->ajax_response();
  83. wp_die( 0 );
  84. }
  85. /**
  86. * Ajax handler for tag search.
  87. *
  88. * @since 3.1.0
  89. */
  90. function wp_ajax_ajax_tag_search() {
  91. if ( ! isset( $_GET['tax'] ) ) {
  92. wp_die( 0 );
  93. }
  94. $taxonomy = sanitize_key( $_GET['tax'] );
  95. $tax = get_taxonomy( $taxonomy );
  96. if ( ! $tax ) {
  97. wp_die( 0 );
  98. }
  99. if ( ! current_user_can( $tax->cap->assign_terms ) ) {
  100. wp_die( -1 );
  101. }
  102. $s = wp_unslash( $_GET['q'] );
  103. $comma = _x( ',', 'tag delimiter' );
  104. if ( ',' !== $comma )
  105. $s = str_replace( $comma, ',', $s );
  106. if ( false !== strpos( $s, ',' ) ) {
  107. $s = explode( ',', $s );
  108. $s = $s[count( $s ) - 1];
  109. }
  110. $s = trim( $s );
  111. /**
  112. * Filter the minimum number of characters required to fire a tag search via AJAX.
  113. *
  114. * @since 4.0.0
  115. *
  116. * @param int $characters The minimum number of characters required. Default 2.
  117. * @param object $tax The taxonomy object.
  118. * @param string $s The search term.
  119. */
  120. $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s );
  121. /*
  122. * Require $term_search_min_chars chars for matching (default: 2)
  123. * ensure it's a non-negative, non-zero integer.
  124. */
  125. if ( ( $term_search_min_chars == 0 ) || ( strlen( $s ) < $term_search_min_chars ) ){
  126. wp_die();
  127. }
  128. $results = get_terms( $taxonomy, array( 'name__like' => $s, 'fields' => 'names', 'hide_empty' => false ) );
  129. echo join( $results, "\n" );
  130. wp_die();
  131. }
  132. /**
  133. * Ajax handler for compression testing.
  134. *
  135. * @since 3.1.0
  136. */
  137. function wp_ajax_wp_compression_test() {
  138. if ( !current_user_can( 'manage_options' ) )
  139. wp_die( -1 );
  140. if ( ini_get('zlib.output_compression') || 'ob_gzhandler' == ini_get('output_handler') ) {
  141. update_site_option('can_compress_scripts', 0);
  142. wp_die( 0 );
  143. }
  144. if ( isset($_GET['test']) ) {
  145. header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
  146. header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
  147. header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
  148. header( 'Pragma: no-cache' );
  149. header('Content-Type: application/javascript; charset=UTF-8');
  150. $force_gzip = ( defined('ENFORCE_GZIP') && ENFORCE_GZIP );
  151. $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."';
  152. if ( 1 == $_GET['test'] ) {
  153. echo $test_str;
  154. wp_die();
  155. } elseif ( 2 == $_GET['test'] ) {
  156. if ( !isset($_SERVER['HTTP_ACCEPT_ENCODING']) )
  157. wp_die( -1 );
  158. if ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {
  159. header('Content-Encoding: deflate');
  160. $out = gzdeflate( $test_str, 1 );
  161. } elseif ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') && function_exists('gzencode') ) {
  162. header('Content-Encoding: gzip');
  163. $out = gzencode( $test_str, 1 );
  164. } else {
  165. wp_die( -1 );
  166. }
  167. echo $out;
  168. wp_die();
  169. } elseif ( 'no' == $_GET['test'] ) {
  170. update_site_option('can_compress_scripts', 0);
  171. } elseif ( 'yes' == $_GET['test'] ) {
  172. update_site_option('can_compress_scripts', 1);
  173. }
  174. }
  175. wp_die( 0 );
  176. }
  177. /**
  178. * Ajax handler for image editor previews.
  179. *
  180. * @since 3.1.0
  181. */
  182. function wp_ajax_imgedit_preview() {
  183. $post_id = intval($_GET['postid']);
  184. if ( empty($post_id) || !current_user_can('edit_post', $post_id) )
  185. wp_die( -1 );
  186. check_ajax_referer( "image_editor-$post_id" );
  187. include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
  188. if ( ! stream_preview_image($post_id) )
  189. wp_die( -1 );
  190. wp_die();
  191. }
  192. /**
  193. * Ajax handler for oEmbed caching.
  194. *
  195. * @since 3.1.0
  196. *
  197. * @global WP_Embed $wp_embed
  198. */
  199. function wp_ajax_oembed_cache() {
  200. $GLOBALS['wp_embed']->cache_oembed( $_GET['post'] );
  201. wp_die( 0 );
  202. }
  203. /**
  204. * Ajax handler for user autocomplete.
  205. *
  206. * @since 3.4.0
  207. */
  208. function wp_ajax_autocomplete_user() {
  209. if ( ! is_multisite() || ! current_user_can( 'promote_users' ) || wp_is_large_network( 'users' ) )
  210. wp_die( -1 );
  211. /** This filter is documented in wp-admin/user-new.php */
  212. if ( ! is_super_admin() && ! apply_filters( 'autocomplete_users_for_site_admins', false ) )
  213. wp_die( -1 );
  214. $return = array();
  215. // Check the type of request
  216. // Current allowed values are `add` and `search`
  217. if ( isset( $_REQUEST['autocomplete_type'] ) && 'search' === $_REQUEST['autocomplete_type'] ) {
  218. $type = $_REQUEST['autocomplete_type'];
  219. } else {
  220. $type = 'add';
  221. }
  222. // Check the desired field for value
  223. // Current allowed values are `user_email` and `user_login`
  224. if ( isset( $_REQUEST['autocomplete_field'] ) && 'user_email' === $_REQUEST['autocomplete_field'] ) {
  225. $field = $_REQUEST['autocomplete_field'];
  226. } else {
  227. $field = 'user_login';
  228. }
  229. // Exclude current users of this blog
  230. if ( isset( $_REQUEST['site_id'] ) ) {
  231. $id = absint( $_REQUEST['site_id'] );
  232. } else {
  233. $id = get_current_blog_id();
  234. }
  235. $include_blog_users = ( $type == 'search' ? get_users( array( 'blog_id' => $id, 'fields' => 'ID' ) ) : array() );
  236. $exclude_blog_users = ( $type == 'add' ? get_users( array( 'blog_id' => $id, 'fields' => 'ID' ) ) : array() );
  237. $users = get_users( array(
  238. 'blog_id' => false,
  239. 'search' => '*' . $_REQUEST['term'] . '*',
  240. 'include' => $include_blog_users,
  241. 'exclude' => $exclude_blog_users,
  242. 'search_columns' => array( 'user_login', 'user_nicename', 'user_email' ),
  243. ) );
  244. foreach ( $users as $user ) {
  245. $return[] = array(
  246. /* translators: 1: user_login, 2: user_email */
  247. 'label' => sprintf( __( '%1$s (%2$s)' ), $user->user_login, $user->user_email ),
  248. 'value' => $user->$field,
  249. );
  250. }
  251. wp_die( wp_json_encode( $return ) );
  252. }
  253. /**
  254. * Ajax handler for dashboard widgets.
  255. *
  256. * @since 3.4.0
  257. */
  258. function wp_ajax_dashboard_widgets() {
  259. require_once ABSPATH . 'wp-admin/includes/dashboard.php';
  260. $pagenow = $_GET['pagenow'];
  261. if ( $pagenow === 'dashboard-user' || $pagenow === 'dashboard-network' || $pagenow === 'dashboard' ) {
  262. set_current_screen( $pagenow );
  263. }
  264. switch ( $_GET['widget'] ) {
  265. case 'dashboard_primary' :
  266. wp_dashboard_primary();
  267. break;
  268. }
  269. wp_die();
  270. }
  271. /**
  272. * Ajax handler for Customizer preview logged-in status.
  273. *
  274. * @since 3.4.0
  275. */
  276. function wp_ajax_logged_in() {
  277. wp_die( 1 );
  278. }
  279. //
  280. // Ajax helpers.
  281. //
  282. /**
  283. * Sends back current comment total and new page links if they need to be updated.
  284. *
  285. * Contrary to normal success AJAX response ("1"), die with time() on success.
  286. *
  287. * @since 2.7.0
  288. *
  289. * @param int $comment_id
  290. * @param int $delta
  291. */
  292. function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) {
  293. $total = isset( $_POST['_total'] ) ? (int) $_POST['_total'] : 0;
  294. $per_page = isset( $_POST['_per_page'] ) ? (int) $_POST['_per_page'] : 0;
  295. $page = isset( $_POST['_page'] ) ? (int) $_POST['_page'] : 0;
  296. $url = isset( $_POST['_url'] ) ? esc_url_raw( $_POST['_url'] ) : '';
  297. // JS didn't send us everything we need to know. Just die with success message
  298. if ( !$total || !$per_page || !$page || !$url )
  299. wp_die( time() );
  300. $total += $delta;
  301. if ( $total < 0 )
  302. $total = 0;
  303. // Only do the expensive stuff on a page-break, and about 1 other time per page
  304. if ( 0 == $total % $per_page || 1 == mt_rand( 1, $per_page ) ) {
  305. $post_id = 0;
  306. $status = 'total_comments'; // What type of comment count are we looking for?
  307. $parsed = parse_url( $url );
  308. if ( isset( $parsed['query'] ) ) {
  309. parse_str( $parsed['query'], $query_vars );
  310. if ( !empty( $query_vars['comment_status'] ) )
  311. $status = $query_vars['comment_status'];
  312. if ( !empty( $query_vars['p'] ) )
  313. $post_id = (int) $query_vars['p'];
  314. }
  315. $comment_count = wp_count_comments($post_id);
  316. // We're looking for a known type of comment count.
  317. if ( isset( $comment_count->$status ) )
  318. $total = $comment_count->$status;
  319. // Else use the decremented value from above.
  320. }
  321. // The time since the last comment count.
  322. $time = time();
  323. $x = new WP_Ajax_Response( array(
  324. 'what' => 'comment',
  325. // Here for completeness - not used.
  326. 'id' => $comment_id,
  327. 'supplemental' => array(
  328. 'total_items_i18n' => sprintf( _n( '%s item', '%s items', $total ), number_format_i18n( $total ) ),
  329. 'total_pages' => ceil( $total / $per_page ),
  330. 'total_pages_i18n' => number_format_i18n( ceil( $total / $per_page ) ),
  331. 'total' => $total,
  332. 'time' => $time
  333. )
  334. ) );
  335. $x->send();
  336. }
  337. //
  338. // POST-based Ajax handlers.
  339. //
  340. /**
  341. * Ajax handler for adding a hierarchical term.
  342. *
  343. * @since 3.1.0
  344. */
  345. function _wp_ajax_add_hierarchical_term() {
  346. $action = $_POST['action'];
  347. $taxonomy = get_taxonomy(substr($action, 4));
  348. check_ajax_referer( $action, '_ajax_nonce-add-' . $taxonomy->name );
  349. if ( !current_user_can( $taxonomy->cap->edit_terms ) )
  350. wp_die( -1 );
  351. $names = explode(',', $_POST['new'.$taxonomy->name]);
  352. $parent = isset($_POST['new'.$taxonomy->name.'_parent']) ? (int) $_POST['new'.$taxonomy->name.'_parent'] : 0;
  353. if ( 0 > $parent )
  354. $parent = 0;
  355. if ( $taxonomy->name == 'category' )
  356. $post_category = isset($_POST['post_category']) ? (array) $_POST['post_category'] : array();
  357. else
  358. $post_category = ( isset($_POST['tax_input']) && isset($_POST['tax_input'][$taxonomy->name]) ) ? (array) $_POST['tax_input'][$taxonomy->name] : array();
  359. $checked_categories = array_map( 'absint', (array) $post_category );
  360. $popular_ids = wp_popular_terms_checklist($taxonomy->name, 0, 10, false);
  361. foreach ( $names as $cat_name ) {
  362. $cat_name = trim($cat_name);
  363. $category_nicename = sanitize_title($cat_name);
  364. if ( '' === $category_nicename )
  365. continue;
  366. if ( !$cat_id = term_exists( $cat_name, $taxonomy->name, $parent ) )
  367. $cat_id = wp_insert_term( $cat_name, $taxonomy->name, array( 'parent' => $parent ) );
  368. if ( is_wp_error( $cat_id ) ) {
  369. continue;
  370. } elseif ( is_array( $cat_id ) ) {
  371. $cat_id = $cat_id['term_id'];
  372. }
  373. $checked_categories[] = $cat_id;
  374. if ( $parent ) // Do these all at once in a second
  375. continue;
  376. ob_start();
  377. wp_terms_checklist( 0, array( 'taxonomy' => $taxonomy->name, 'descendants_and_self' => $cat_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids ));
  378. $data = ob_get_clean();
  379. $add = array(
  380. 'what' => $taxonomy->name,
  381. 'id' => $cat_id,
  382. 'data' => str_replace( array("\n", "\t"), '', $data),
  383. 'position' => -1
  384. );
  385. }
  386. if ( $parent ) { // Foncy - replace the parent and all its children
  387. $parent = get_term( $parent, $taxonomy->name );
  388. $term_id = $parent->term_id;
  389. while ( $parent->parent ) { // get the top parent
  390. $parent = get_term( $parent->parent, $taxonomy->name );
  391. if ( is_wp_error( $parent ) )
  392. break;
  393. $term_id = $parent->term_id;
  394. }
  395. ob_start();
  396. wp_terms_checklist( 0, array('taxonomy' => $taxonomy->name, 'descendants_and_self' => $term_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids));
  397. $data = ob_get_clean();
  398. $add = array(
  399. 'what' => $taxonomy->name,
  400. 'id' => $term_id,
  401. 'data' => str_replace( array("\n", "\t"), '', $data),
  402. 'position' => -1
  403. );
  404. }
  405. ob_start();
  406. wp_dropdown_categories( array(
  407. 'taxonomy' => $taxonomy->name, 'hide_empty' => 0, 'name' => 'new'.$taxonomy->name.'_parent', 'orderby' => 'name',
  408. 'hierarchical' => 1, 'show_option_none' => '&mdash; '.$taxonomy->labels->parent_item.' &mdash;'
  409. ) );
  410. $sup = ob_get_clean();
  411. $add['supplemental'] = array( 'newcat_parent' => $sup );
  412. $x = new WP_Ajax_Response( $add );
  413. $x->send();
  414. }
  415. /**
  416. * Ajax handler for deleting a comment.
  417. *
  418. * @since 3.1.0
  419. */
  420. function wp_ajax_delete_comment() {
  421. $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
  422. if ( !$comment = get_comment( $id ) )
  423. wp_die( time() );
  424. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) )
  425. wp_die( -1 );
  426. check_ajax_referer( "delete-comment_$id" );
  427. $status = wp_get_comment_status( $comment->comment_ID );
  428. $delta = -1;
  429. if ( isset($_POST['trash']) && 1 == $_POST['trash'] ) {
  430. if ( 'trash' == $status )
  431. wp_die( time() );
  432. $r = wp_trash_comment( $comment->comment_ID );
  433. } elseif ( isset($_POST['untrash']) && 1 == $_POST['untrash'] ) {
  434. if ( 'trash' != $status )
  435. wp_die( time() );
  436. $r = wp_untrash_comment( $comment->comment_ID );
  437. if ( ! isset( $_POST['comment_status'] ) || $_POST['comment_status'] != 'trash' ) // undo trash, not in trash
  438. $delta = 1;
  439. } elseif ( isset($_POST['spam']) && 1 == $_POST['spam'] ) {
  440. if ( 'spam' == $status )
  441. wp_die( time() );
  442. $r = wp_spam_comment( $comment->comment_ID );
  443. } elseif ( isset($_POST['unspam']) && 1 == $_POST['unspam'] ) {
  444. if ( 'spam' != $status )
  445. wp_die( time() );
  446. $r = wp_unspam_comment( $comment->comment_ID );
  447. if ( ! isset( $_POST['comment_status'] ) || $_POST['comment_status'] != 'spam' ) // undo spam, not in spam
  448. $delta = 1;
  449. } elseif ( isset($_POST['delete']) && 1 == $_POST['delete'] ) {
  450. $r = wp_delete_comment( $comment->comment_ID );
  451. } else {
  452. wp_die( -1 );
  453. }
  454. if ( $r ) // Decide if we need to send back '1' or a more complicated response including page links and comment counts
  455. _wp_ajax_delete_comment_response( $comment->comment_ID, $delta );
  456. wp_die( 0 );
  457. }
  458. /**
  459. * Ajax handler for deleting a tag.
  460. *
  461. * @since 3.1.0
  462. */
  463. function wp_ajax_delete_tag() {
  464. $tag_id = (int) $_POST['tag_ID'];
  465. check_ajax_referer( "delete-tag_$tag_id" );
  466. $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag';
  467. $tax = get_taxonomy($taxonomy);
  468. if ( !current_user_can( $tax->cap->delete_terms ) )
  469. wp_die( -1 );
  470. $tag = get_term( $tag_id, $taxonomy );
  471. if ( !$tag || is_wp_error( $tag ) )
  472. wp_die( 1 );
  473. if ( wp_delete_term($tag_id, $taxonomy))
  474. wp_die( 1 );
  475. else
  476. wp_die( 0 );
  477. }
  478. /**
  479. * Ajax handler for deleting a link.
  480. *
  481. * @since 3.1.0
  482. */
  483. function wp_ajax_delete_link() {
  484. $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
  485. check_ajax_referer( "delete-bookmark_$id" );
  486. if ( !current_user_can( 'manage_links' ) )
  487. wp_die( -1 );
  488. $link = get_bookmark( $id );
  489. if ( !$link || is_wp_error( $link ) )
  490. wp_die( 1 );
  491. if ( wp_delete_link( $id ) )
  492. wp_die( 1 );
  493. else
  494. wp_die( 0 );
  495. }
  496. /**
  497. * Ajax handler for deleting meta.
  498. *
  499. * @since 3.1.0
  500. */
  501. function wp_ajax_delete_meta() {
  502. $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
  503. check_ajax_referer( "delete-meta_$id" );
  504. if ( !$meta = get_metadata_by_mid( 'post', $id ) )
  505. wp_die( 1 );
  506. if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $meta->post_id, $meta->meta_key ) )
  507. wp_die( -1 );
  508. if ( delete_meta( $meta->meta_id ) )
  509. wp_die( 1 );
  510. wp_die( 0 );
  511. }
  512. /**
  513. * Ajax handler for deleting a post.
  514. *
  515. * @since 3.1.0
  516. *
  517. * @param string $action Action to perform.
  518. */
  519. function wp_ajax_delete_post( $action ) {
  520. if ( empty( $action ) )
  521. $action = 'delete-post';
  522. $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
  523. check_ajax_referer( "{$action}_$id" );
  524. if ( !current_user_can( 'delete_post', $id ) )
  525. wp_die( -1 );
  526. if ( !get_post( $id ) )
  527. wp_die( 1 );
  528. if ( wp_delete_post( $id ) )
  529. wp_die( 1 );
  530. else
  531. wp_die( 0 );
  532. }
  533. /**
  534. * Ajax handler for sending a post to the trash.
  535. *
  536. * @since 3.1.0
  537. *
  538. * @param string $action Action to perform.
  539. */
  540. function wp_ajax_trash_post( $action ) {
  541. if ( empty( $action ) )
  542. $action = 'trash-post';
  543. $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
  544. check_ajax_referer( "{$action}_$id" );
  545. if ( !current_user_can( 'delete_post', $id ) )
  546. wp_die( -1 );
  547. if ( !get_post( $id ) )
  548. wp_die( 1 );
  549. if ( 'trash-post' == $action )
  550. $done = wp_trash_post( $id );
  551. else
  552. $done = wp_untrash_post( $id );
  553. if ( $done )
  554. wp_die( 1 );
  555. wp_die( 0 );
  556. }
  557. /**
  558. * Ajax handler to restore a post from the trash.
  559. *
  560. * @since 3.1.0
  561. *
  562. * @param string $action Action to perform.
  563. */
  564. function wp_ajax_untrash_post( $action ) {
  565. if ( empty( $action ) )
  566. $action = 'untrash-post';
  567. wp_ajax_trash_post( $action );
  568. }
  569. /**
  570. * @since 3.1.0
  571. *
  572. * @param string $action
  573. */
  574. function wp_ajax_delete_page( $action ) {
  575. if ( empty( $action ) )
  576. $action = 'delete-page';
  577. $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
  578. check_ajax_referer( "{$action}_$id" );
  579. if ( !current_user_can( 'delete_page', $id ) )
  580. wp_die( -1 );
  581. if ( ! get_post( $id ) )
  582. wp_die( 1 );
  583. if ( wp_delete_post( $id ) )
  584. wp_die( 1 );
  585. else
  586. wp_die( 0 );
  587. }
  588. /**
  589. * Ajax handler to dim a comment.
  590. *
  591. * @since 3.1.0
  592. */
  593. function wp_ajax_dim_comment() {
  594. $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
  595. if ( !$comment = get_comment( $id ) ) {
  596. $x = new WP_Ajax_Response( array(
  597. 'what' => 'comment',
  598. 'id' => new WP_Error('invalid_comment', sprintf(__('Comment %d does not exist'), $id))
  599. ) );
  600. $x->send();
  601. }
  602. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && ! current_user_can( 'moderate_comments' ) )
  603. wp_die( -1 );
  604. $current = wp_get_comment_status( $comment->comment_ID );
  605. if ( isset( $_POST['new'] ) && $_POST['new'] == $current )
  606. wp_die( time() );
  607. check_ajax_referer( "approve-comment_$id" );
  608. if ( in_array( $current, array( 'unapproved', 'spam' ) ) )
  609. $result = wp_set_comment_status( $comment->comment_ID, 'approve', true );
  610. else
  611. $result = wp_set_comment_status( $comment->comment_ID, 'hold', true );
  612. if ( is_wp_error($result) ) {
  613. $x = new WP_Ajax_Response( array(
  614. 'what' => 'comment',
  615. 'id' => $result
  616. ) );
  617. $x->send();
  618. }
  619. // Decide if we need to send back '1' or a more complicated response including page links and comment counts
  620. _wp_ajax_delete_comment_response( $comment->comment_ID );
  621. wp_die( 0 );
  622. }
  623. /**
  624. * Ajax handler for deleting a link category.
  625. *
  626. * @since 3.1.0
  627. *
  628. * @param string $action Action to perform.
  629. */
  630. function wp_ajax_add_link_category( $action ) {
  631. if ( empty( $action ) )
  632. $action = 'add-link-category';
  633. check_ajax_referer( $action );
  634. if ( !current_user_can( 'manage_categories' ) )
  635. wp_die( -1 );
  636. $names = explode(',', wp_unslash( $_POST['newcat'] ) );
  637. $x = new WP_Ajax_Response();
  638. foreach ( $names as $cat_name ) {
  639. $cat_name = trim($cat_name);
  640. $slug = sanitize_title($cat_name);
  641. if ( '' === $slug )
  642. continue;
  643. if ( !$cat_id = term_exists( $cat_name, 'link_category' ) )
  644. $cat_id = wp_insert_term( $cat_name, 'link_category' );
  645. if ( is_wp_error( $cat_id ) ) {
  646. continue;
  647. } elseif ( is_array( $cat_id ) ) {
  648. $cat_id = $cat_id['term_id'];
  649. }
  650. $cat_name = esc_html( $cat_name );
  651. $x->add( array(
  652. 'what' => 'link-category',
  653. 'id' => $cat_id,
  654. '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>",
  655. 'position' => -1
  656. ) );
  657. }
  658. $x->send();
  659. }
  660. /**
  661. * Ajax handler to add a tag.
  662. *
  663. * @since 3.1.0
  664. *
  665. * @global WP_List_Table $wp_list_table
  666. */
  667. function wp_ajax_add_tag() {
  668. global $wp_list_table;
  669. check_ajax_referer( 'add-tag', '_wpnonce_add-tag' );
  670. $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag';
  671. $tax = get_taxonomy($taxonomy);
  672. if ( !current_user_can( $tax->cap->edit_terms ) )
  673. wp_die( -1 );
  674. $x = new WP_Ajax_Response();
  675. $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST );
  676. if ( !$tag || is_wp_error($tag) || (!$tag = get_term( $tag['term_id'], $taxonomy )) ) {
  677. $message = __('An error has occurred. Please reload the page and try again.');
  678. if ( is_wp_error($tag) && $tag->get_error_message() )
  679. $message = $tag->get_error_message();
  680. $x->add( array(
  681. 'what' => 'taxonomy',
  682. 'data' => new WP_Error('error', $message )
  683. ) );
  684. $x->send();
  685. }
  686. $wp_list_table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => $_POST['screen'] ) );
  687. $level = 0;
  688. if ( is_taxonomy_hierarchical($taxonomy) ) {
  689. $level = count( get_ancestors( $tag->term_id, $taxonomy, 'taxonomy' ) );
  690. ob_start();
  691. $wp_list_table->single_row( $tag, $level );
  692. $noparents = ob_get_clean();
  693. }
  694. ob_start();
  695. $wp_list_table->single_row( $tag );
  696. $parents = ob_get_clean();
  697. $x->add( array(
  698. 'what' => 'taxonomy',
  699. 'supplemental' => compact('parents', 'noparents')
  700. ) );
  701. $x->add( array(
  702. 'what' => 'term',
  703. 'position' => $level,
  704. 'supplemental' => (array) $tag
  705. ) );
  706. $x->send();
  707. }
  708. /**
  709. * Ajax handler for getting a tagcloud.
  710. *
  711. * @since 3.1.0
  712. */
  713. function wp_ajax_get_tagcloud() {
  714. if ( ! isset( $_POST['tax'] ) ) {
  715. wp_die( 0 );
  716. }
  717. $taxonomy = sanitize_key( $_POST['tax'] );
  718. $tax = get_taxonomy( $taxonomy );
  719. if ( ! $tax ) {
  720. wp_die( 0 );
  721. }
  722. if ( ! current_user_can( $tax->cap->assign_terms ) ) {
  723. wp_die( -1 );
  724. }
  725. $tags = get_terms( $taxonomy, array( 'number' => 45, 'orderby' => 'count', 'order' => 'DESC' ) );
  726. if ( empty( $tags ) )
  727. wp_die( $tax->labels->not_found );
  728. if ( is_wp_error( $tags ) )
  729. wp_die( $tags->get_error_message() );
  730. foreach ( $tags as $key => $tag ) {
  731. $tags[ $key ]->link = '#';
  732. $tags[ $key ]->id = $tag->term_id;
  733. }
  734. // We need raw tag names here, so don't filter the output
  735. $return = wp_generate_tag_cloud( $tags, array('filter' => 0) );
  736. if ( empty($return) )
  737. wp_die( 0 );
  738. echo $return;
  739. wp_die();
  740. }
  741. /**
  742. * Ajax handler for getting comments.
  743. *
  744. * @since 3.1.0
  745. *
  746. * @global WP_List_Table $wp_list_table
  747. * @global int $post_id
  748. *
  749. * @param string $action Action to perform.
  750. */
  751. function wp_ajax_get_comments( $action ) {
  752. global $wp_list_table, $post_id;
  753. if ( empty( $action ) )
  754. $action = 'get-comments';
  755. check_ajax_referer( $action );
  756. if ( empty( $post_id ) && ! empty( $_REQUEST['p'] ) ) {
  757. $id = absint( $_REQUEST['p'] );
  758. if ( ! empty( $id ) )
  759. $post_id = $id;
  760. }
  761. if ( empty( $post_id ) )
  762. wp_die( -1 );
  763. $wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
  764. if ( ! current_user_can( 'edit_post', $post_id ) )
  765. wp_die( -1 );
  766. $wp_list_table->prepare_items();
  767. if ( !$wp_list_table->has_items() )
  768. wp_die( 1 );
  769. $x = new WP_Ajax_Response();
  770. ob_start();
  771. foreach ( $wp_list_table->items as $comment ) {
  772. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) )
  773. continue;
  774. get_comment( $comment );
  775. $wp_list_table->single_row( $comment );
  776. }
  777. $comment_list_item = ob_get_clean();
  778. $x->add( array(
  779. 'what' => 'comments',
  780. 'data' => $comment_list_item
  781. ) );
  782. $x->send();
  783. }
  784. /**
  785. * Ajax handler for replying to a comment.
  786. *
  787. * @since 3.1.0
  788. *
  789. * @global WP_List_Table $wp_list_table
  790. *
  791. * @param string $action Action to perform.
  792. */
  793. function wp_ajax_replyto_comment( $action ) {
  794. global $wp_list_table;
  795. if ( empty( $action ) )
  796. $action = 'replyto-comment';
  797. check_ajax_referer( $action, '_ajax_nonce-replyto-comment' );
  798. $comment_post_ID = (int) $_POST['comment_post_ID'];
  799. $post = get_post( $comment_post_ID );
  800. if ( ! $post )
  801. wp_die( -1 );
  802. if ( !current_user_can( 'edit_post', $comment_post_ID ) )
  803. wp_die( -1 );
  804. if ( empty( $post->post_status ) )
  805. wp_die( 1 );
  806. elseif ( in_array($post->post_status, array('draft', 'pending', 'trash') ) )
  807. wp_die( __('ERROR: you are replying to a comment on a draft post.') );
  808. $user = wp_get_current_user();
  809. if ( $user->exists() ) {
  810. $user_ID = $user->ID;
  811. $comment_author = wp_slash( $user->display_name );
  812. $comment_author_email = wp_slash( $user->user_email );
  813. $comment_author_url = wp_slash( $user->user_url );
  814. $comment_content = trim( $_POST['content'] );
  815. $comment_type = isset( $_POST['comment_type'] ) ? trim( $_POST['comment_type'] ) : '';
  816. if ( current_user_can( 'unfiltered_html' ) ) {
  817. if ( ! isset( $_POST['_wp_unfiltered_html_comment'] ) )
  818. $_POST['_wp_unfiltered_html_comment'] = '';
  819. if ( wp_create_nonce( 'unfiltered-html-comment' ) != $_POST['_wp_unfiltered_html_comment'] ) {
  820. kses_remove_filters(); // start with a clean slate
  821. kses_init_filters(); // set up the filters
  822. }
  823. }
  824. } else {
  825. wp_die( __( 'Sorry, you must be logged in to reply to a comment.' ) );
  826. }
  827. if ( '' == $comment_content )
  828. wp_die( __( 'ERROR: please type a comment.' ) );
  829. $comment_parent = 0;
  830. if ( isset( $_POST['comment_ID'] ) )
  831. $comment_parent = absint( $_POST['comment_ID'] );
  832. $comment_auto_approved = false;
  833. $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID');
  834. // Automatically approve parent comment.
  835. if ( !empty($_POST['approve_parent']) ) {
  836. $parent = get_comment( $comment_parent );
  837. if ( $parent && $parent->comment_approved === '0' && $parent->comment_post_ID == $comment_post_ID ) {
  838. if ( wp_set_comment_status( $parent->comment_ID, 'approve' ) )
  839. $comment_auto_approved = true;
  840. }
  841. }
  842. $comment_id = wp_new_comment( $commentdata );
  843. $comment = get_comment($comment_id);
  844. if ( ! $comment ) wp_die( 1 );
  845. $position = ( isset($_POST['position']) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1';
  846. ob_start();
  847. if ( isset( $_REQUEST['mode'] ) && 'dashboard' == $_REQUEST['mode'] ) {
  848. require_once( ABSPATH . 'wp-admin/includes/dashboard.php' );
  849. _wp_dashboard_recent_comments_row( $comment );
  850. } else {
  851. if ( isset( $_REQUEST['mode'] ) && 'single' == $_REQUEST['mode'] ) {
  852. $wp_list_table = _get_list_table('WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
  853. } else {
  854. $wp_list_table = _get_list_table('WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
  855. }
  856. $wp_list_table->single_row( $comment );
  857. }
  858. $comment_list_item = ob_get_clean();
  859. $response = array(
  860. 'what' => 'comment',
  861. 'id' => $comment->comment_ID,
  862. 'data' => $comment_list_item,
  863. 'position' => $position
  864. );
  865. if ( $comment_auto_approved )
  866. $response['supplemental'] = array( 'parent_approved' => $parent->comment_ID );
  867. $x = new WP_Ajax_Response();
  868. $x->add( $response );
  869. $x->send();
  870. }
  871. /**
  872. * Ajax handler for editing a comment.
  873. *
  874. * @since 3.1.0
  875. *
  876. * @global WP_List_Table $wp_list_table
  877. */
  878. function wp_ajax_edit_comment() {
  879. global $wp_list_table;
  880. check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' );
  881. $comment_id = (int) $_POST['comment_ID'];
  882. if ( ! current_user_can( 'edit_comment', $comment_id ) )
  883. wp_die( -1 );
  884. if ( '' == $_POST['content'] )
  885. wp_die( __( 'ERROR: please type a comment.' ) );
  886. if ( isset( $_POST['status'] ) )
  887. $_POST['comment_status'] = $_POST['status'];
  888. edit_comment();
  889. $position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1';
  890. $checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0;
  891. $wp_list_table = _get_list_table( $checkbox ? 'WP_Comments_List_Table' : 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
  892. $comment = get_comment( $comment_id );
  893. if ( empty( $comment->comment_ID ) )
  894. wp_die( -1 );
  895. ob_start();
  896. $wp_list_table->single_row( $comment );
  897. $comment_list_item = ob_get_clean();
  898. $x = new WP_Ajax_Response();
  899. $x->add( array(
  900. 'what' => 'edit_comment',
  901. 'id' => $comment->comment_ID,
  902. 'data' => $comment_list_item,
  903. 'position' => $position
  904. ));
  905. $x->send();
  906. }
  907. /**
  908. * Ajax handler for adding a menu item.
  909. *
  910. * @since 3.1.0
  911. */
  912. function wp_ajax_add_menu_item() {
  913. check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' );
  914. if ( ! current_user_can( 'edit_theme_options' ) )
  915. wp_die( -1 );
  916. require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
  917. // For performance reasons, we omit some object properties from the checklist.
  918. // The following is a hacky way to restore them when adding non-custom items.
  919. $menu_items_data = array();
  920. foreach ( (array) $_POST['menu-item'] as $menu_item_data ) {
  921. if (
  922. ! empty( $menu_item_data['menu-item-type'] ) &&
  923. 'custom' != $menu_item_data['menu-item-type'] &&
  924. ! empty( $menu_item_data['menu-item-object-id'] )
  925. ) {
  926. switch( $menu_item_data['menu-item-type'] ) {
  927. case 'post_type' :
  928. $_object = get_post( $menu_item_data['menu-item-object-id'] );
  929. break;
  930. case 'taxonomy' :
  931. $_object = get_term( $menu_item_data['menu-item-object-id'], $menu_item_data['menu-item-object'] );
  932. break;
  933. }
  934. $_menu_items = array_map( 'wp_setup_nav_menu_item', array( $_object ) );
  935. $_menu_item = reset( $_menu_items );
  936. // Restore the missing menu item properties
  937. $menu_item_data['menu-item-description'] = $_menu_item->description;
  938. }
  939. $menu_items_data[] = $menu_item_data;
  940. }
  941. $item_ids = wp_save_nav_menu_items( 0, $menu_items_data );
  942. if ( is_wp_error( $item_ids ) )
  943. wp_die( 0 );
  944. $menu_items = array();
  945. foreach ( (array) $item_ids as $menu_item_id ) {
  946. $menu_obj = get_post( $menu_item_id );
  947. if ( ! empty( $menu_obj->ID ) ) {
  948. $menu_obj = wp_setup_nav_menu_item( $menu_obj );
  949. $menu_obj->label = $menu_obj->title; // don't show "(pending)" in ajax-added items
  950. $menu_items[] = $menu_obj;
  951. }
  952. }
  953. /** This filter is documented in wp-admin/includes/nav-menu.php */
  954. $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $_POST['menu'] );
  955. if ( ! class_exists( $walker_class_name ) )
  956. wp_die( 0 );
  957. if ( ! empty( $menu_items ) ) {
  958. $args = array(
  959. 'after' => '',
  960. 'before' => '',
  961. 'link_after' => '',
  962. 'link_before' => '',
  963. 'walker' => new $walker_class_name,
  964. );
  965. echo walk_nav_menu_tree( $menu_items, 0, (object) $args );
  966. }
  967. wp_die();
  968. }
  969. /**
  970. * Ajax handler for adding meta.
  971. *
  972. * @since 3.1.0
  973. */
  974. function wp_ajax_add_meta() {
  975. check_ajax_referer( 'add-meta', '_ajax_nonce-add-meta' );
  976. $c = 0;
  977. $pid = (int) $_POST['post_id'];
  978. $post = get_post( $pid );
  979. if ( isset($_POST['metakeyselect']) || isset($_POST['metakeyinput']) ) {
  980. if ( !current_user_can( 'edit_post', $pid ) )
  981. wp_die( -1 );
  982. if ( isset($_POST['metakeyselect']) && '#NONE#' == $_POST['metakeyselect'] && empty($_POST['metakeyinput']) )
  983. wp_die( 1 );
  984. // If the post is an autodraft, save the post as a draft and then attempt to save the meta.
  985. if ( $post->post_status == 'auto-draft' ) {
  986. $save_POST = $_POST; // Backup $_POST
  987. $_POST = array(); // Make it empty for edit_post()
  988. $_POST['action'] = 'draft'; // Warning fix
  989. $_POST['post_ID'] = $pid;
  990. $_POST['post_type'] = $post->post_type;
  991. $_POST['post_status'] = 'draft';
  992. $now = current_time('timestamp', 1);
  993. $_POST['post_title'] = sprintf( __( 'Draft created on %1$s at %2$s' ), date( get_option( 'date_format' ), $now ), date( get_option( 'time_format' ), $now ) );
  994. if ( $pid = edit_post() ) {
  995. if ( is_wp_error( $pid ) ) {
  996. $x = new WP_Ajax_Response( array(
  997. 'what' => 'meta',
  998. 'data' => $pid
  999. ) );
  1000. $x->send();
  1001. }
  1002. $_POST = $save_POST; // Now we can restore original $_POST again
  1003. if ( !$mid = add_meta( $pid ) )
  1004. wp_die( __( 'Please provide a custom field value.' ) );
  1005. } else {
  1006. wp_die( 0 );
  1007. }
  1008. } elseif ( ! $mid = add_meta( $pid ) ) {
  1009. wp_die( __( 'Please provide a custom field value.' ) );
  1010. }
  1011. $meta = get_metadata_by_mid( 'post', $mid );
  1012. $pid = (int) $meta->post_id;
  1013. $meta = get_object_vars( $meta );
  1014. $x = new WP_Ajax_Response( array(
  1015. 'what' => 'meta',
  1016. 'id' => $mid,
  1017. 'data' => _list_meta_row( $meta, $c ),
  1018. 'position' => 1,
  1019. 'supplemental' => array('postid' => $pid)
  1020. ) );
  1021. } else { // Update?
  1022. $mid = (int) key( $_POST['meta'] );
  1023. $key = wp_unslash( $_POST['meta'][$mid]['key'] );
  1024. $value = wp_unslash( $_POST['meta'][$mid]['value'] );
  1025. if ( '' == trim($key) )
  1026. wp_die( __( 'Please provide a custom field name.' ) );
  1027. if ( '' == trim($value) )
  1028. wp_die( __( 'Please provide a custom field value.' ) );
  1029. if ( ! $meta = get_metadata_by_mid( 'post', $mid ) )
  1030. wp_die( 0 ); // if meta doesn't exist
  1031. if ( is_protected_meta( $meta->meta_key, 'post' ) || is_protected_meta( $key, 'post' ) ||
  1032. ! current_user_can( 'edit_post_meta', $meta->post_id, $meta->meta_key ) ||
  1033. ! current_user_can( 'edit_post_meta', $meta->post_id, $key ) )
  1034. wp_die( -1 );
  1035. if ( $meta->meta_value != $value || $meta->meta_key != $key ) {
  1036. if ( !$u = update_metadata_by_mid( 'post', $mid, $value, $key ) )
  1037. wp_die( 0 ); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems).
  1038. }
  1039. $x = new WP_Ajax_Response( array(
  1040. 'what' => 'meta',
  1041. 'id' => $mid, 'old_id' => $mid,
  1042. 'data' => _list_meta_row( array(
  1043. 'meta_key' => $key,
  1044. 'meta_value' => $value,
  1045. 'meta_id' => $mid
  1046. ), $c ),
  1047. 'position' => 0,
  1048. 'supplemental' => array('postid' => $meta->post_id)
  1049. ) );
  1050. }
  1051. $x->send();
  1052. }
  1053. /**
  1054. * Ajax handler for adding a user.
  1055. *
  1056. * @since 3.1.0
  1057. *
  1058. * @global WP_List_Table $wp_list_table
  1059. *
  1060. * @param string $action Action to perform.
  1061. */
  1062. function wp_ajax_add_user( $action ) {
  1063. global $wp_list_table;
  1064. if ( empty( $action ) )
  1065. $action = 'add-user';
  1066. check_ajax_referer( $action );
  1067. if ( ! current_user_can('create_users') )
  1068. wp_die( -1 );
  1069. if ( ! $user_id = edit_user() ) {
  1070. wp_die( 0 );
  1071. } elseif ( is_wp_error( $user_id ) ) {
  1072. $x = new WP_Ajax_Response( array(
  1073. 'what' => 'user',
  1074. 'id' => $user_id
  1075. ) );
  1076. $x->send();
  1077. }
  1078. $user_object = get_userdata( $user_id );
  1079. $wp_list_table = _get_list_table('WP_Users_List_Table');
  1080. $role = current( $user_object->roles );
  1081. $x = new WP_Ajax_Response( array(
  1082. 'what' => 'user',
  1083. 'id' => $user_id,
  1084. 'data' => $wp_list_table->single_row( $user_object, '', $role ),
  1085. 'supplemental' => array(
  1086. 'show-link' => sprintf(__( 'User <a href="#%s">%s</a> added' ), "user-$user_id", $user_object->user_login),
  1087. 'role' => $role,
  1088. )
  1089. ) );
  1090. $x->send();
  1091. }
  1092. /**
  1093. * Ajax handler for closed post boxes.
  1094. *
  1095. * @since 3.1.0
  1096. */
  1097. function wp_ajax_closed_postboxes() {
  1098. check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' );
  1099. $closed = isset( $_POST['closed'] ) ? explode( ',', $_POST['closed']) : array();
  1100. $closed = array_filter($closed);
  1101. $hidden = isset( $_POST['hidden'] ) ? explode( ',', $_POST['hidden']) : array();
  1102. $hidden = array_filter($hidden);
  1103. $page = isset( $_POST['page'] ) ? $_POST['page'] : '';
  1104. if ( $page != sanitize_key( $page ) )
  1105. wp_die( 0 );
  1106. if ( ! $user = wp_get_current_user() )
  1107. wp_die( -1 );
  1108. if ( is_array($closed) )
  1109. update_user_option($user->ID, "closedpostboxes_$page", $closed, true);
  1110. if ( is_array($hidden) ) {
  1111. $hidden = array_diff( $hidden, array('submitdiv', 'linksubmitdiv', 'manage-menu', 'create-menu') ); // postboxes that are always shown
  1112. update_user_option($user->ID, "metaboxhidden_$page", $hidden, true);
  1113. }
  1114. wp_die( 1 );
  1115. }
  1116. /**
  1117. * Ajax handler for hidden columns.
  1118. *
  1119. * @since 3.1.0
  1120. */
  1121. function wp_ajax_hidden_columns() {
  1122. check_ajax_referer( 'screen-options-nonce', 'screenoptionnonce' );
  1123. $page = isset( $_POST['page'] ) ? $_POST['page'] : '';
  1124. if ( $page != sanitize_key( $page ) )
  1125. wp_die( 0 );
  1126. if ( ! $user = wp_get_current_user() )
  1127. wp_die( -1 );
  1128. $hidden = ! empty( $_POST['hidden'] ) ? explode( ',', $_POST['hidden'] ) : array();
  1129. update_user_option( $user->ID, "manage{$page}columnshidden", $hidden, true );
  1130. wp_die( 1 );
  1131. }
  1132. /**
  1133. * Ajax handler for updating whether to display the welcome panel.
  1134. *
  1135. * @since 3.1.0
  1136. */
  1137. function wp_ajax_update_welcome_panel() {
  1138. check_ajax_referer( 'welcome-panel-nonce', 'welcomepanelnonce' );
  1139. if ( ! current_user_can( 'edit_theme_options' ) )
  1140. wp_die( -1 );
  1141. update_user_meta( get_current_user_id(), 'show_welcome_panel', empty( $_POST['visible'] ) ? 0 : 1 );
  1142. wp_die( 1 );
  1143. }
  1144. /**
  1145. * Ajax handler for retrieving menu meta boxes.
  1146. *
  1147. * @since 3.1.0
  1148. */
  1149. function wp_ajax_menu_get_metabox() {
  1150. if ( ! current_user_can( 'edit_theme_options' ) )
  1151. wp_die( -1 );
  1152. require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
  1153. if ( isset( $_POST['item-type'] ) && 'post_type' == $_POST['item-type'] ) {
  1154. $type = 'posttype';
  1155. $callback = 'wp_nav_menu_item_post_type_meta_box';
  1156. $items = (array) get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
  1157. } elseif ( isset( $_POST['item-type'] ) && 'taxonomy' == $_POST['item-type'] ) {
  1158. $type = 'taxonomy';
  1159. $callback = 'wp_nav_menu_item_taxonomy_meta_box';
  1160. $items = (array) get_taxonomies( array( 'show_ui' => true ), 'object' );
  1161. }
  1162. if ( ! empty( $_POST['item-object'] ) && isset( $items[$_POST['item-object']] ) ) {
  1163. $menus_meta_box_object = $items[ $_POST['item-object'] ];
  1164. /** This filter is documented in wp-admin/includes/nav-menu.php */
  1165. $item = apply_filters( 'nav_menu_meta_box_object', $menus_meta_box_object );
  1166. ob_start();
  1167. call_user_func_array($callback, array(
  1168. null,
  1169. array(
  1170. 'id' => 'add-' . $item->name,
  1171. 'title' => $item->labels->name,
  1172. 'callback' => $callback,
  1173. 'args' => $item,
  1174. )
  1175. ));
  1176. $markup = ob_get_clean();
  1177. echo wp_json_encode(array(
  1178. 'replace-id' => $type . '-' . $item->name,
  1179. 'markup' => $markup,
  1180. ));
  1181. }
  1182. wp_die();
  1183. }
  1184. /**
  1185. * Ajax handler for internal linking.
  1186. *
  1187. * @since 3.1.0
  1188. */
  1189. function wp_ajax_wp_link_ajax() {
  1190. check_ajax_referer( 'internal-linking', '_ajax_linking_nonce' );
  1191. $args = array();
  1192. if ( isset( $_POST['search'] ) )
  1193. $args['s'] = wp_unslash( $_POST['search'] );
  1194. $args['pagenum'] = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1;
  1195. require(ABSPATH . WPINC . '/class-wp-editor.php');
  1196. $results = _WP_Editors::wp_link_query( $args );
  1197. if ( ! isset( $results ) )
  1198. wp_die( 0 );
  1199. echo wp_json_encode( $results );
  1200. echo "\n";
  1201. wp_die();
  1202. }
  1203. /**
  1204. * Ajax handler for menu locations save.
  1205. *
  1206. * @since 3.1.0
  1207. */
  1208. function wp_ajax_menu_locations_save() {
  1209. if ( ! current_user_can( 'edit_theme_options' ) )
  1210. wp_die( -1 );
  1211. check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' );
  1212. if ( ! isset( $_POST['menu-locations'] ) )
  1213. wp_die( 0 );
  1214. set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_POST['menu-locations'] ) );
  1215. wp_die( 1 );
  1216. }
  1217. /**
  1218. * Ajax handler for saving the meta box order.
  1219. *
  1220. * @since 3.1.0
  1221. */
  1222. function wp_ajax_meta_box_order() {
  1223. check_ajax_referer( 'meta-box-order' );
  1224. $order = isset( $_POST['order'] ) ? (array) $_POST['order'] : false;
  1225. $page_columns = isset( $_POST['page_columns'] ) ? $_POST['page_columns'] : 'auto';
  1226. if ( $page_columns != 'auto' )
  1227. $page_columns = (int) $page_columns;
  1228. $page = isset( $_POST['page'] ) ? $_POST['page'] : '';
  1229. if ( $page != sanitize_key( $page ) )
  1230. wp_die( 0 );
  1231. if ( ! $user = wp_get_current_user() )
  1232. wp_die( -1 );
  1233. if ( $order )
  1234. update_user_option($user->ID, "meta-box-order_$page", $order, true);
  1235. if ( $page_columns )
  1236. update_user_option($user->ID, "screen_layout_$page", $page_columns, true);
  1237. wp_die( 1 );
  1238. }
  1239. /**
  1240. * Ajax handler for menu quick searching.
  1241. *
  1242. * @since 3.1.0
  1243. */
  1244. function wp_ajax_menu_quick_search() {
  1245. if ( ! current_user_can( 'edit_theme_options' ) )
  1246. wp_die( -1 );
  1247. require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
  1248. _wp_ajax_menu_quick_search( $_POST );
  1249. wp_die();
  1250. }
  1251. /**
  1252. * Ajax handler to retrieve a permalink.
  1253. *
  1254. * @since 3.1.0
  1255. */
  1256. function wp_ajax_get_permalink() {
  1257. check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
  1258. $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
  1259. wp_die( add_query_arg( array( 'preview' => 'true' ), get_permalink( $post_id ) ) );
  1260. }
  1261. /**
  1262. * Ajax handler to retrieve a sample permalink.
  1263. *
  1264. * @since 3.1.0
  1265. */
  1266. function wp_ajax_sample_permalink() {
  1267. check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
  1268. $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
  1269. $title = isset($_POST['new_title'])? $_POST['new_title'] : '';
  1270. $slug = isset($_POST['new_slug'])? $_POST['new_slug'] : null;
  1271. wp_die( get_sample_permalink_html( $post_id, $title, $slug ) );
  1272. }
  1273. /**
  1274. * Ajax handler for Quick Edit saving a post from a list table.
  1275. *
  1276. * @since 3.1.0
  1277. *
  1278. * @global WP_List_Table $wp_list_table
  1279. */
  1280. function wp_ajax_inline_save() {
  1281. global $wp_list_table;
  1282. check_ajax_referer( 'inlineeditnonce', '_inline_edit' );
  1283. if ( ! isset($_POST['post_ID']) || ! ( $post_ID = (int) $_POST['post_ID'] ) )
  1284. wp_die();
  1285. if ( 'page' == $_POST['post_type'] ) {
  1286. if ( ! current_user_can( 'edit_page', $post_ID ) )
  1287. wp_die( __( 'You are not allowed to edit this page.' ) );
  1288. } else {
  1289. if ( ! current_user_can( 'edit_post', $post_ID ) )
  1290. wp_die( __( 'You are not allowed to edit this post.' ) );
  1291. }
  1292. if ( $last = wp_check_post_lock( $post_ID ) ) {
  1293. $last_user = get_userdata( $last );
  1294. $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
  1295. 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 ) );
  1296. wp_die();
  1297. }
  1298. $data = &$_POST;
  1299. $post = get_post( $post_ID, ARRAY_A );
  1300. // Since it's coming from the database.
  1301. $post = wp_slash($post);
  1302. $data['content'] = $post['post_content'];
  1303. $data['excerpt'] = $post['post_excerpt'];
  1304. // Rename.
  1305. $data['user_ID'] = get_current_user_id();
  1306. if ( isset($data['post_parent']) )
  1307. $data['parent_id'] = $data['post_parent'];
  1308. // Status.
  1309. if ( isset( $data['keep_private'] ) && 'private' == $data['keep_private'] ) {
  1310. $data['visibility'] = 'private';
  1311. $data['post_status'] = 'private';
  1312. } else {
  1313. $data['post_status'] = $data['_status'];
  1314. }
  1315. if ( empty($data['comment_status']) )
  1316. $data['comment_status'] = 'closed';
  1317. if ( empty($data['ping_status']) )
  1318. $data['ping_status'] = 'closed';
  1319. // Exclude terms from taxonomies that are not supposed to appear in Quick Edit.
  1320. if ( ! empty( $data['tax_input'] ) ) {
  1321. foreach ( $data['tax_input'] as $taxonomy => $terms ) {
  1322. $tax_object = get_taxonomy( $taxonomy );
  1323. /** This filter is documented in wp-admin/includes/class-wp-posts-list-table.php */
  1324. if ( ! apply_filters( 'quick_edit_show_taxonomy', $tax_object->show_in_quick_edit, $taxonomy, $post['post_type'] ) ) {
  1325. unset( $data['tax_input'][ $taxonomy ] );
  1326. }
  1327. }
  1328. }
  1329. // Hack: wp_unique_post_slug() doesn't work for drafts, so we will fake that our post is published.
  1330. if ( ! empty( $data['post_name'] ) && in_array( $post['post_status'], array( 'draft', 'pending' ) ) ) {
  1331. $post['post_status'] = 'publish';
  1332. $data['post_name'] = wp_unique_post_slug( $data['post_name'], $post['ID'], $post['post_status'], $post['post_type'], $post['post_parent'] );
  1333. }
  1334. // Update the post.
  1335. edit_post();
  1336. $wp_list_table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => $_POST['screen'] ) );
  1337. $level = 0;
  1338. $request_post = array( get_post( $_POST['post_ID'] ) );
  1339. $parent = $request_post[0]->post_parent;
  1340. while ( $parent > 0 ) {
  1341. $parent_post = get_post( $parent );
  1342. $parent = $parent_post->post_parent;
  1343. $level++;
  1344. }
  1345. $wp_list_table->display_rows( array( get_post( $_POST['post_ID'] ) ), $level );
  1346. wp_die();
  1347. }
  1348. /**
  1349. * Ajax handler for quick edit saving for a term.
  1350. *
  1351. * @since 3.1.0
  1352. *
  1353. * @global WP_List_Table $wp_list_table
  1354. */
  1355. function wp_ajax_inline_save_tax() {
  1356. global $wp_list_table;
  1357. check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' );
  1358. $taxonomy = sanitize_key( $_POST['taxonomy'] );
  1359. $tax = get_taxonomy( $taxonomy );
  1360. if ( ! $tax )
  1361. wp_die( 0 );
  1362. if ( ! current_user_can( $tax->cap->edit_terms ) )
  1363. wp_die( -1 );
  1364. $wp_list_table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => 'edit-' . $taxonomy ) );
  1365. if ( ! isset($_POST['tax_ID']) || ! ( $id = (int) $_POST['tax_ID'] ) )
  1366. wp_die( -1 );
  1367. $tag = get_term( $id, $taxonomy );
  1368. $_POST['description'] = $tag->description;
  1369. $updated = wp_update_term($id, $taxonomy, $_POST);
  1370. if ( $updated && !is_wp_error($updated) ) {
  1371. $tag = get_term( $updated['term_id'], $taxonomy );
  1372. if ( !$tag || is_wp_error( $tag ) ) {
  1373. if ( is_wp_error($tag) && $tag->get_error_message() )
  1374. wp_die( $tag->get_error_message() );
  1375. wp_die( __( 'Item not updated.' ) );
  1376. }
  1377. } else {
  1378. if ( is_wp_error($updated) && $updated->get_error_message() )
  1379. wp_die( $updated->get_error_message() );
  1380. wp_die( __( 'Item not updated.' ) );
  1381. }
  1382. $level = 0;
  1383. $parent = $tag->parent;
  1384. while ( $parent > 0 ) {
  1385. $parent_tag = get_term( $parent, $taxonomy );
  1386. $parent = $parent_tag->parent;
  1387. $level++;
  1388. }
  1389. $wp_list_table->single_row( $tag, $level );
  1390. wp_die();
  1391. }
  1392. /**
  1393. * Ajax handler for querying posts for the Find Posts modal.
  1394. *
  1395. * @see window.findPosts
  1396. *
  1397. * @since 3.1.0
  1398. */
  1399. function wp_ajax_find_posts() {
  1400. check_ajax_referer( 'find-posts' );
  1401. $post_types = get_post_types( array( 'public' => true ), 'objects' );
  1402. unset( $post_types['attachment'] );
  1403. $s = wp_unslash( $_POST['ps'] );
  1404. $args = array(
  1405. 'post_type' => array_keys( $post_types ),
  1406. 'post_status' => 'any',
  1407. 'posts_per_page' => 50,
  1408. );
  1409. if ( '' !== $s )
  1410. $args['s'] = $s;
  1411. $posts = get_posts( $args );
  1412. if ( ! $posts ) {
  1413. wp_send_json_error( __( 'No items found.' ) );
  1414. }
  1415. $html = '<table class="widefat"><thead><tr><th class="found-radio"><br /></th><th>'.__('Title').'</th><th class="no-break">'.__('Type').'</th><th class="no-break">'.__('Date').'</th><th class="no-break">'.__('Status').'</th></tr></thead><tbody>';
  1416. $alt = '';
  1417. foreach ( $posts as $post ) {
  1418. $title = trim( $post->post_title ) ? $post->post_title : __( '(no title)' );
  1419. $alt = ( 'alternate' == $alt ) ? '' : 'alternate';
  1420. switch ( $post->post_status ) {
  1421. case 'publish' :
  1422. case 'private' :
  1423. $stat = __('Published');
  1424. break;
  1425. case 'future' :
  1426. $stat = __('Scheduled');
  1427. break;
  1428. case 'pending' :
  1429. $stat = __('Pending Review');
  1430. break;
  1431. case 'draft' :
  1432. $stat = __('Draft');
  1433. break;
  1434. }
  1435. if ( '0000-00-00 00:00:00' == $post->post_date ) {
  1436. $time = '';
  1437. } else {
  1438. /* translators: date format in table columns, see http://php.net/date */
  1439. $time = mysql2date(__('Y/m/d'), $post->post_date);
  1440. }
  1441. $html .= '<tr class="' . trim( 'found-posts ' . $alt ) . '"><td class="found-radio"><input type="radio" id="found-'.$post->ID.'" name="found_post_id" value="' . esc_attr($post->ID) . '"></td>';
  1442. $html .= '<td><label for="found-'.$post->ID.'">' . esc_html( $title ) . '</label></td><td class="no-break">' . esc_html( $post_types[$post->post_type]->labels->singular_name ) . '</td><td class="no-break">'.esc_html( $time ) . '</td><td class="no-break">' . esc_html( $stat ). ' </td></tr>' . "\n\n";
  1443. }
  1444. $html .= '</tbody></table>';
  1445. wp_send_json_success( $html );
  1446. }
  1447. /**
  1448. * Ajax handler for saving the widgets order.
  1449. *
  1450. * @since 3.1.0
  1451. */
  1452. function wp_ajax_widgets_order() {
  1453. check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' );
  1454. if ( !current_user_can('edit_theme_options') )
  1455. wp_die( -1 );
  1456. unset( $_POST['savewidgets'], $_POST['action'] );
  1457. // Save widgets order for all sidebars.
  1458. if ( is_array($_POST['sidebars']) ) {
  1459. $sidebars = array();
  1460. foreach ( $_POST['sidebars'] as $key => $val ) {
  1461. $sb = array();
  1462. if ( !empty($val) ) {
  1463. $val = explode(',', $val);
  1464. foreach ( $val as $k => $v ) {
  1465. if ( strpos($v, 'widget-') === false )
  1466. continue;
  1467. $sb[$k] = substr($v, strpos($v, '_') + 1);
  1468. }
  1469. }
  1470. $sidebars[$key] = $sb;
  1471. }
  1472. wp_set_sidebars_widgets($sidebars);
  1473. wp_die( 1 );
  1474. }
  1475. wp_die( -1 );
  1476. }
  1477. /**
  1478. * Ajax handler for saving a widget.
  1479. *
  1480. * @since 3.1.0
  1481. *
  1482. * @global array $wp_registered_widgets
  1483. * @global array $wp_registered_widget_controls
  1484. * @global array $wp_registered_widget