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

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

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