PageRenderTime 74ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

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

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