PageRenderTime 55ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/davodey/WordPress
PHP | 2543 lines | 1593 code | 451 blank | 499 comment | 448 complexity | f312176ae67d9eda925a2854a70fd65c MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

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

Large files files are truncated, but you can click here to view the full file