PageRenderTime 54ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

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