PageRenderTime 434ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

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