PageRenderTime 54ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/markjaquith/WordPress
PHP | 5465 lines | 3482 code | 991 blank | 992 comment | 717 complexity | 1ea7ebb1fa879f592c3c1b3ef3bcd9f2 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

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

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

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