PageRenderTime 118ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 0ms

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

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