PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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