PageRenderTime 761ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

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

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