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

  1. <?php 
  2. /** 
  3. * Administration API: Core Ajax handlers 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Administration 
  7. * @since 2.1.0 
  8. */ 
  9.  
  10. // 
  11. // No-privilege Ajax handlers. 
  12. // 
  13.   
  14. /** 
  15. * Ajax handler for the Heartbeat API in 
  16. * the no-privilege context. 
  17. * 
  18. * Runs when the user is not logged in. 
  19. * 
  20. * @since 3.6.0 
  21. */ 
  22. function wp_ajax_nopriv_heartbeat() { 
  23. $response = array(); 
  24.  
  25. // screen_id is the same as $current_screen->id and the JS global 'pagenow'. 
  26. if ( ! empty($_POST['screen_id']) ) 
  27. $screen_id = sanitize_key($_POST['screen_id']); 
  28. else 
  29. $screen_id = 'front'; 
  30.  
  31. if ( ! empty($_POST['data']) ) { 
  32. $data = wp_unslash( (array) $_POST['data'] ); 
  33.  
  34. /** 
  35. * Filters Heartbeat Ajax response in no-privilege environments. 
  36. * 
  37. * @since 3.6.0 
  38. * 
  39. * @param array|object $response The no-priv Heartbeat response object or array. 
  40. * @param array $data An array of data passed via $_POST. 
  41. * @param string $screen_id The screen id. 
  42. */ 
  43. $response = apply_filters( 'heartbeat_nopriv_received', $response, $data, $screen_id ); 
  44.  
  45. /** 
  46. * Filters Heartbeat Ajax response when no data is passed. 
  47. * 
  48. * @since 3.6.0 
  49. * 
  50. * @param array|object $response The Heartbeat response object or array. 
  51. * @param string $screen_id The screen id. 
  52. */ 
  53. $response = apply_filters( 'heartbeat_nopriv_send', $response, $screen_id ); 
  54.  
  55. /** 
  56. * Fires when Heartbeat ticks in no-privilege environments. 
  57. * 
  58. * Allows the transport to be easily replaced with long-polling. 
  59. * 
  60. * @since 3.6.0 
  61. * 
  62. * @param array|object $response The no-priv Heartbeat response. 
  63. * @param string $screen_id The screen id. 
  64. */ 
  65. do_action( 'heartbeat_nopriv_tick', $response, $screen_id ); 
  66.  
  67. // Send the current time according to the server. 
  68. $response['server_time'] = time(); 
  69.  
  70. wp_send_json($response); 
  71.  
  72. // 
  73.  // GET-based Ajax handlers. 
  74. // 
  75.   
  76. /** 
  77. * Ajax handler for fetching a list table. 
  78. * 
  79. * @since 3.1.0 
  80. */ 
  81. function wp_ajax_fetch_list() { 
  82. $list_class = $_GET['list_args']['class']; 
  83. check_ajax_referer( "fetch-list-$list_class", '_ajax_fetch_list_nonce' ); 
  84.  
  85. $wp_list_table = _get_list_table( $list_class, array( 'screen' => $_GET['list_args']['screen']['id'] ) ); 
  86. if ( ! $wp_list_table ) { 
  87. wp_die( 0 ); 
  88.  
  89. if ( ! $wp_list_table->ajax_user_can() ) { 
  90. wp_die( -1 ); 
  91.  
  92. $wp_list_table->ajax_response(); 
  93.  
  94. wp_die( 0 ); 
  95.  
  96. /** 
  97. * Ajax handler for tag search. 
  98. * 
  99. * @since 3.1.0 
  100. */ 
  101. function wp_ajax_ajax_tag_search() { 
  102. if ( ! isset( $_GET['tax'] ) ) { 
  103. wp_die( 0 ); 
  104.  
  105. $taxonomy = sanitize_key( $_GET['tax'] ); 
  106. $tax = get_taxonomy( $taxonomy ); 
  107. if ( ! $tax ) { 
  108. wp_die( 0 ); 
  109.  
  110. if ( ! current_user_can( $tax->cap->assign_terms ) ) { 
  111. wp_die( -1 ); 
  112.  
  113. $s = wp_unslash( $_GET['q'] ); 
  114.  
  115. $comma = _x( ', ', 'tag delimiter' ); 
  116. if ( ', ' !== $comma ) 
  117. $s = str_replace( $comma, ', ', $s ); 
  118. if ( false !== strpos( $s, ', ' ) ) { 
  119. $s = explode( ', ', $s ); 
  120. $s = $s[count( $s ) - 1]; 
  121. $s = trim( $s ); 
  122.  
  123. /** 
  124. * Filters the minimum number of characters required to fire a tag search via Ajax. 
  125. * 
  126. * @since 4.0.0 
  127. * 
  128. * @param int $characters The minimum number of characters required. Default 2. 
  129. * @param WP_Taxonomy $tax The taxonomy object. 
  130. * @param string $s The search term. 
  131. */ 
  132. $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s ); 
  133.  
  134. /** 
  135. * Require $term_search_min_chars chars for matching (default: 2) 
  136. * ensure it's a non-negative, non-zero integer. 
  137. */ 
  138. if ( ( $term_search_min_chars == 0 ) || ( strlen( $s ) < $term_search_min_chars ) ) { 
  139. wp_die(); 
  140.  
  141. $results = get_terms( $taxonomy, array( 'name__like' => $s, 'fields' => 'names', 'hide_empty' => false ) ); 
  142.  
  143. echo join( $results, "\n" ); 
  144. wp_die(); 
  145.  
  146. /** 
  147. * Ajax handler for compression testing. 
  148. * 
  149. * @since 3.1.0 
  150. */ 
  151. function wp_ajax_wp_compression_test() { 
  152. if ( !current_user_can( 'manage_options' ) ) 
  153. wp_die( -1 ); 
  154.  
  155. if ( ini_get('zlib.output_compression') || 'ob_gzhandler' == ini_get('output_handler') ) { 
  156. update_site_option('can_compress_scripts', 0); 
  157. wp_die( 0 ); 
  158.  
  159. if ( isset($_GET['test']) ) { 
  160. header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' ); 
  161. header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' ); 
  162. header( 'Cache-Control: no-cache, must-revalidate, max-age=0' ); 
  163. header('Content-Type: application/javascript; charset=UTF-8'); 
  164. $force_gzip = ( defined('ENFORCE_GZIP') && ENFORCE_GZIP ); 
  165. $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."'; 
  166.  
  167. if ( 1 == $_GET['test'] ) { 
  168. echo $test_str; 
  169. wp_die(); 
  170. } elseif ( 2 == $_GET['test'] ) { 
  171. if ( !isset($_SERVER['HTTP_ACCEPT_ENCODING']) ) 
  172. wp_die( -1 ); 
  173. if ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) { 
  174. header('Content-Encoding: deflate'); 
  175. $out = gzdeflate( $test_str, 1 ); 
  176. } elseif ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') && function_exists('gzencode') ) { 
  177. header('Content-Encoding: gzip'); 
  178. $out = gzencode( $test_str, 1 ); 
  179. } else { 
  180. wp_die( -1 ); 
  181. echo $out; 
  182. wp_die(); 
  183. } elseif ( 'no' == $_GET['test'] ) { 
  184. check_ajax_referer( 'update_can_compress_scripts' ); 
  185. update_site_option('can_compress_scripts', 0); 
  186. } elseif ( 'yes' == $_GET['test'] ) { 
  187. check_ajax_referer( 'update_can_compress_scripts' ); 
  188. update_site_option('can_compress_scripts', 1); 
  189.  
  190. wp_die( 0 ); 
  191.  
  192. /** 
  193. * Ajax handler for image editor previews. 
  194. * 
  195. * @since 3.1.0 
  196. */ 
  197. function wp_ajax_imgedit_preview() { 
  198. $post_id = intval($_GET['postid']); 
  199. if ( empty($post_id) || !current_user_can('edit_post', $post_id) ) 
  200. wp_die( -1 ); 
  201.  
  202. check_ajax_referer( "image_editor-$post_id" ); 
  203.  
  204. include_once( ABSPATH . 'wp-admin/includes/image-edit.php' ); 
  205. if ( ! stream_preview_image($post_id) ) 
  206. wp_die( -1 ); 
  207.  
  208. wp_die(); 
  209.  
  210. /** 
  211. * Ajax handler for oEmbed caching. 
  212. * 
  213. * @since 3.1.0 
  214. * 
  215. * @global WP_Embed $wp_embed 
  216. */ 
  217. function wp_ajax_oembed_cache() { 
  218. $GLOBALS['wp_embed']->cache_oembed( $_GET['post'] ); 
  219. wp_die( 0 ); 
  220.  
  221. /** 
  222. * Ajax handler for user autocomplete. 
  223. * 
  224. * @since 3.4.0 
  225. */ 
  226. function wp_ajax_autocomplete_user() { 
  227. if ( ! is_multisite() || ! current_user_can( 'promote_users' ) || wp_is_large_network( 'users' ) ) 
  228. wp_die( -1 ); 
  229.  
  230. /** This filter is documented in wp-admin/user-new.php */ 
  231. if ( ! is_super_admin() && ! apply_filters( 'autocomplete_users_for_site_admins', false ) ) 
  232. wp_die( -1 ); 
  233.  
  234. $return = array(); 
  235.  
  236. // Check the type of request 
  237. // Current allowed values are `add` and `search` 
  238. if ( isset( $_REQUEST['autocomplete_type'] ) && 'search' === $_REQUEST['autocomplete_type'] ) { 
  239. $type = $_REQUEST['autocomplete_type']; 
  240. } else { 
  241. $type = 'add'; 
  242.  
  243. // Check the desired field for value 
  244. // Current allowed values are `user_email` and `user_login` 
  245. if ( isset( $_REQUEST['autocomplete_field'] ) && 'user_email' === $_REQUEST['autocomplete_field'] ) { 
  246. $field = $_REQUEST['autocomplete_field']; 
  247. } else { 
  248. $field = 'user_login'; 
  249.  
  250. // Exclude current users of this blog 
  251. if ( isset( $_REQUEST['site_id'] ) ) { 
  252. $id = absint( $_REQUEST['site_id'] ); 
  253. } else { 
  254. $id = get_current_blog_id(); 
  255.  
  256. $include_blog_users = ( $type == 'search' ? get_users( array( 'blog_id' => $id, 'fields' => 'ID' ) ) : array() ); 
  257. $exclude_blog_users = ( $type == 'add' ? get_users( array( 'blog_id' => $id, 'fields' => 'ID' ) ) : array() ); 
  258.  
  259. $users = get_users( array( 
  260. 'blog_id' => false,  
  261. 'search' => '*' . $_REQUEST['term'] . '*',  
  262. 'include' => $include_blog_users,  
  263. 'exclude' => $exclude_blog_users,  
  264. 'search_columns' => array( 'user_login', 'user_nicename', 'user_email' ),  
  265. ) ); 
  266.  
  267. foreach ( $users as $user ) { 
  268. $return[] = array( 
  269. /** translators: 1: user_login, 2: user_email */ 
  270. 'label' => sprintf( _x( '%1$s (%2$s)', 'user autocomplete result' ), $user->user_login, $user->user_email ),  
  271. 'value' => $user->$field,  
  272. ); 
  273.  
  274. wp_die( wp_json_encode( $return ) ); 
  275.  
  276. /** 
  277. * Ajax handler for dashboard widgets. 
  278. * 
  279. * @since 3.4.0 
  280. */ 
  281. function wp_ajax_dashboard_widgets() { 
  282. require_once ABSPATH . 'wp-admin/includes/dashboard.php'; 
  283.  
  284. $pagenow = $_GET['pagenow']; 
  285. if ( $pagenow === 'dashboard-user' || $pagenow === 'dashboard-network' || $pagenow === 'dashboard' ) { 
  286. set_current_screen( $pagenow ); 
  287.  
  288. switch ( $_GET['widget'] ) { 
  289. case 'dashboard_primary' : 
  290. wp_dashboard_primary(); 
  291. break; 
  292. wp_die(); 
  293.  
  294. /** 
  295. * Ajax handler for Customizer preview logged-in status. 
  296. * 
  297. * @since 3.4.0 
  298. */ 
  299. function wp_ajax_logged_in() { 
  300. wp_die( 1 ); 
  301.  
  302. // 
  303.  // Ajax helpers. 
  304. // 
  305.   
  306. /** 
  307. * Sends back current comment total and new page links if they need to be updated. 
  308. * 
  309. * Contrary to normal success Ajax response ("1"), die with time() on success. 
  310. * 
  311. * @access private 
  312. * @since 2.7.0 
  313. * 
  314. * @param int $comment_id 
  315. * @param int $delta 
  316. */ 
  317. function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) { 
  318. $total = isset( $_POST['_total'] ) ? (int) $_POST['_total'] : 0; 
  319. $per_page = isset( $_POST['_per_page'] ) ? (int) $_POST['_per_page'] : 0; 
  320. $page = isset( $_POST['_page'] ) ? (int) $_POST['_page'] : 0; 
  321. $url = isset( $_POST['_url'] ) ? esc_url_raw( $_POST['_url'] ) : ''; 
  322.  
  323. // JS didn't send us everything we need to know. Just die with success message 
  324. if ( ! $total || ! $per_page || ! $page || ! $url ) { 
  325. $time = time(); 
  326. $comment = get_comment( $comment_id ); 
  327. $comment_status = ''; 
  328. $comment_link = ''; 
  329.  
  330. if ( $comment ) { 
  331. $comment_status = $comment->comment_approved; 
  332.  
  333. if ( 1 === (int) $comment_status ) { 
  334. $comment_link = get_comment_link( $comment ); 
  335.  
  336. $counts = wp_count_comments(); 
  337.  
  338. $x = new WP_Ajax_Response( array( 
  339. 'what' => 'comment',  
  340. // Here for completeness - not used. 
  341. 'id' => $comment_id,  
  342. 'supplemental' => array( 
  343. 'status' => $comment_status,  
  344. 'postId' => $comment ? $comment->comment_post_ID : '',  
  345. 'time' => $time,  
  346. 'in_moderation' => $counts->moderated,  
  347. 'i18n_comments_text' => sprintf( 
  348. _n( '%s Comment', '%s Comments', $counts->approved ),  
  349. number_format_i18n( $counts->approved ) 
  350. ),  
  351. 'i18n_moderation_text' => sprintf( 
  352. _nx( '%s in moderation', '%s in moderation', $counts->moderated, 'comments' ),  
  353. number_format_i18n( $counts->moderated ) 
  354. ),  
  355. 'comment_link' => $comment_link,  
  356. ) ); 
  357. $x->send(); 
  358.  
  359. $total += $delta; 
  360. if ( $total < 0 ) 
  361. $total = 0; 
  362.  
  363. // Only do the expensive stuff on a page-break, and about 1 other time per page 
  364. if ( 0 == $total % $per_page || 1 == mt_rand( 1, $per_page ) ) { 
  365. $post_id = 0; 
  366. // What type of comment count are we looking for? 
  367. $status = 'all'; 
  368. $parsed = parse_url( $url ); 
  369. if ( isset( $parsed['query'] ) ) { 
  370. parse_str( $parsed['query'], $query_vars ); 
  371. if ( !empty( $query_vars['comment_status'] ) ) 
  372. $status = $query_vars['comment_status']; 
  373. if ( !empty( $query_vars['p'] ) ) 
  374. $post_id = (int) $query_vars['p']; 
  375. if ( ! empty( $query_vars['comment_type'] ) ) 
  376. $type = $query_vars['comment_type']; 
  377.  
  378. if ( empty( $type ) ) { 
  379. // Only use the comment count if not filtering by a comment_type. 
  380. $comment_count = wp_count_comments($post_id); 
  381.  
  382. // We're looking for a known type of comment count. 
  383. if ( isset( $comment_count->$status ) ) { 
  384. $total = $comment_count->$status; 
  385. // Else use the decremented value from above. 
  386.  
  387. // The time since the last comment count. 
  388. $time = time(); 
  389. $comment = get_comment( $comment_id ); 
  390.  
  391. $x = new WP_Ajax_Response( array( 
  392. 'what' => 'comment',  
  393. // Here for completeness - not used. 
  394. 'id' => $comment_id,  
  395. 'supplemental' => array( 
  396. 'status' => $comment ? $comment->comment_approved : '',  
  397. 'postId' => $comment ? $comment->comment_post_ID : '',  
  398. 'total_items_i18n' => sprintf( _n( '%s item', '%s items', $total ), number_format_i18n( $total ) ),  
  399. 'total_pages' => ceil( $total / $per_page ),  
  400. 'total_pages_i18n' => number_format_i18n( ceil( $total / $per_page ) ),  
  401. 'total' => $total,  
  402. 'time' => $time 
  403. ) ); 
  404. $x->send(); 
  405.  
  406. // 
  407.  // POST-based Ajax handlers. 
  408. // 
  409.   
  410. /** 
  411. * Ajax handler for adding a hierarchical term. 
  412. * 
  413. * @access private 
  414. * @since 3.1.0 
  415. */ 
  416. function _wp_ajax_add_hierarchical_term() { 
  417. $action = $_POST['action']; 
  418. $taxonomy = get_taxonomy(substr($action, 4)); 
  419. check_ajax_referer( $action, '_ajax_nonce-add-' . $taxonomy->name ); 
  420. if ( !current_user_can( $taxonomy->cap->edit_terms ) ) 
  421. wp_die( -1 ); 
  422. $names = explode(', ', $_POST['new'.$taxonomy->name]); 
  423. $parent = isset($_POST['new'.$taxonomy->name.'_parent']) ? (int) $_POST['new'.$taxonomy->name.'_parent'] : 0; 
  424. if ( 0 > $parent ) 
  425. $parent = 0; 
  426. if ( $taxonomy->name == 'category' ) 
  427. $post_category = isset($_POST['post_category']) ? (array) $_POST['post_category'] : array(); 
  428. else 
  429. $post_category = ( isset($_POST['tax_input']) && isset($_POST['tax_input'][$taxonomy->name]) ) ? (array) $_POST['tax_input'][$taxonomy->name] : array(); 
  430. $checked_categories = array_map( 'absint', (array) $post_category ); 
  431. $popular_ids = wp_popular_terms_checklist($taxonomy->name, 0, 10, false); 
  432.  
  433. foreach ( $names as $cat_name ) { 
  434. $cat_name = trim($cat_name); 
  435. $category_nicename = sanitize_title($cat_name); 
  436. if ( '' === $category_nicename ) 
  437. continue; 
  438. if ( !$cat_id = term_exists( $cat_name, $taxonomy->name, $parent ) ) 
  439. $cat_id = wp_insert_term( $cat_name, $taxonomy->name, array( 'parent' => $parent ) ); 
  440. if ( is_wp_error( $cat_id ) ) { 
  441. continue; 
  442. } elseif ( is_array( $cat_id ) ) { 
  443. $cat_id = $cat_id['term_id']; 
  444. $checked_categories[] = $cat_id; 
  445. if ( $parent ) // Do these all at once in a second 
  446. continue; 
  447.  
  448. ob_start(); 
  449.  
  450. wp_terms_checklist( 0, array( 'taxonomy' => $taxonomy->name, 'descendants_and_self' => $cat_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids )); 
  451.  
  452. $data = ob_get_clean(); 
  453.  
  454. $add = array( 
  455. 'what' => $taxonomy->name,  
  456. 'id' => $cat_id,  
  457. 'data' => str_replace( array("\n", "\t"), '', $data),  
  458. 'position' => -1 
  459. ); 
  460.  
  461. if ( $parent ) { // Foncy - replace the parent and all its children 
  462. $parent = get_term( $parent, $taxonomy->name ); 
  463. $term_id = $parent->term_id; 
  464.  
  465. while ( $parent->parent ) { // get the top parent 
  466. $parent = get_term( $parent->parent, $taxonomy->name ); 
  467. if ( is_wp_error( $parent ) ) 
  468. break; 
  469. $term_id = $parent->term_id; 
  470.  
  471. ob_start(); 
  472.  
  473. wp_terms_checklist( 0, array('taxonomy' => $taxonomy->name, 'descendants_and_self' => $term_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids)); 
  474.  
  475. $data = ob_get_clean(); 
  476.  
  477. $add = array( 
  478. 'what' => $taxonomy->name,  
  479. 'id' => $term_id,  
  480. 'data' => str_replace( array("\n", "\t"), '', $data),  
  481. 'position' => -1 
  482. ); 
  483.  
  484. ob_start(); 
  485.  
  486. wp_dropdown_categories( array( 
  487. 'taxonomy' => $taxonomy->name, 'hide_empty' => 0, 'name' => 'new'.$taxonomy->name.'_parent', 'orderby' => 'name',  
  488. 'hierarchical' => 1, 'show_option_none' => '— '.$taxonomy->labels->parent_item.' —' 
  489. ) ); 
  490.  
  491. $sup = ob_get_clean(); 
  492.  
  493. $add['supplemental'] = array( 'newcat_parent' => $sup ); 
  494.  
  495. $x = new WP_Ajax_Response( $add ); 
  496. $x->send(); 
  497.  
  498. /** 
  499. * Ajax handler for deleting a comment. 
  500. * 
  501. * @since 3.1.0 
  502. */ 
  503. function wp_ajax_delete_comment() { 
  504. $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0; 
  505.  
  506. if ( !$comment = get_comment( $id ) ) 
  507. wp_die( time() ); 
  508. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) 
  509. wp_die( -1 ); 
  510.  
  511. check_ajax_referer( "delete-comment_$id" ); 
  512. $status = wp_get_comment_status( $comment ); 
  513.  
  514. $delta = -1; 
  515. if ( isset($_POST['trash']) && 1 == $_POST['trash'] ) { 
  516. if ( 'trash' == $status ) 
  517. wp_die( time() ); 
  518. $r = wp_trash_comment( $comment ); 
  519. } elseif ( isset($_POST['untrash']) && 1 == $_POST['untrash'] ) { 
  520. if ( 'trash' != $status ) 
  521. wp_die( time() ); 
  522. $r = wp_untrash_comment( $comment ); 
  523. if ( ! isset( $_POST['comment_status'] ) || $_POST['comment_status'] != 'trash' ) // undo trash, not in trash 
  524. $delta = 1; 
  525. } elseif ( isset($_POST['spam']) && 1 == $_POST['spam'] ) { 
  526. if ( 'spam' == $status ) 
  527. wp_die( time() ); 
  528. $r = wp_spam_comment( $comment ); 
  529. } elseif ( isset($_POST['unspam']) && 1 == $_POST['unspam'] ) { 
  530. if ( 'spam' != $status ) 
  531. wp_die( time() ); 
  532. $r = wp_unspam_comment( $comment ); 
  533. if ( ! isset( $_POST['comment_status'] ) || $_POST['comment_status'] != 'spam' ) // undo spam, not in spam 
  534. $delta = 1; 
  535. } elseif ( isset($_POST['delete']) && 1 == $_POST['delete'] ) { 
  536. $r = wp_delete_comment( $comment ); 
  537. } else { 
  538. wp_die( -1 ); 
  539.  
  540. if ( $r ) // Decide if we need to send back '1' or a more complicated response including page links and comment counts 
  541. _wp_ajax_delete_comment_response( $comment->comment_ID, $delta ); 
  542. wp_die( 0 ); 
  543.  
  544. /** 
  545. * Ajax handler for deleting a tag. 
  546. * 
  547. * @since 3.1.0 
  548. */ 
  549. function wp_ajax_delete_tag() { 
  550. $tag_id = (int) $_POST['tag_ID']; 
  551. check_ajax_referer( "delete-tag_$tag_id" ); 
  552.  
  553. if ( ! current_user_can( 'delete_term', $tag_id ) ) { 
  554. wp_die( -1 ); 
  555.  
  556. $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag'; 
  557. $tag = get_term( $tag_id, $taxonomy ); 
  558. if ( !$tag || is_wp_error( $tag ) ) 
  559. wp_die( 1 ); 
  560.  
  561. if ( wp_delete_term($tag_id, $taxonomy)) 
  562. wp_die( 1 ); 
  563. else 
  564. wp_die( 0 ); 
  565.  
  566. /** 
  567. * Ajax handler for deleting a link. 
  568. * 
  569. * @since 3.1.0 
  570. */ 
  571. function wp_ajax_delete_link() { 
  572. $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0; 
  573.  
  574. check_ajax_referer( "delete-bookmark_$id" ); 
  575. if ( !current_user_can( 'manage_links' ) ) 
  576. wp_die( -1 ); 
  577.  
  578. $link = get_bookmark( $id ); 
  579. if ( !$link || is_wp_error( $link ) ) 
  580. wp_die( 1 ); 
  581.  
  582. if ( wp_delete_link( $id ) ) 
  583. wp_die( 1 ); 
  584. else 
  585. wp_die( 0 ); 
  586.  
  587. /** 
  588. * Ajax handler for deleting meta. 
  589. * 
  590. * @since 3.1.0 
  591. */ 
  592. function wp_ajax_delete_meta() { 
  593. $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0; 
  594.  
  595. check_ajax_referer( "delete-meta_$id" ); 
  596. if ( !$meta = get_metadata_by_mid( 'post', $id ) ) 
  597. wp_die( 1 ); 
  598.  
  599. if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $meta->post_id, $meta->meta_key ) ) 
  600. wp_die( -1 ); 
  601. if ( delete_meta( $meta->meta_id ) ) 
  602. wp_die( 1 ); 
  603. wp_die( 0 ); 
  604.  
  605. /** 
  606. * Ajax handler for deleting a post. 
  607. * 
  608. * @since 3.1.0 
  609. * 
  610. * @param string $action Action to perform. 
  611. */ 
  612. function wp_ajax_delete_post( $action ) { 
  613. if ( empty( $action ) ) 
  614. $action = 'delete-post'; 
  615. $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0; 
  616.  
  617. check_ajax_referer( "{$action}_$id" ); 
  618. if ( !current_user_can( 'delete_post', $id ) ) 
  619. wp_die( -1 ); 
  620.  
  621. if ( !get_post( $id ) ) 
  622. wp_die( 1 ); 
  623.  
  624. if ( wp_delete_post( $id ) ) 
  625. wp_die( 1 ); 
  626. else 
  627. wp_die( 0 ); 
  628.  
  629. /** 
  630. * Ajax handler for sending a post to the trash. 
  631. * 
  632. * @since 3.1.0 
  633. * 
  634. * @param string $action Action to perform. 
  635. */ 
  636. function wp_ajax_trash_post( $action ) { 
  637. if ( empty( $action ) ) 
  638. $action = 'trash-post'; 
  639. $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0; 
  640.  
  641. check_ajax_referer( "{$action}_$id" ); 
  642. if ( !current_user_can( 'delete_post', $id ) ) 
  643. wp_die( -1 ); 
  644.  
  645. if ( !get_post( $id ) ) 
  646. wp_die( 1 ); 
  647.  
  648. if ( 'trash-post' == $action ) 
  649. $done = wp_trash_post( $id ); 
  650. else 
  651. $done = wp_untrash_post( $id ); 
  652.  
  653. if ( $done ) 
  654. wp_die( 1 ); 
  655.  
  656. wp_die( 0 ); 
  657.  
  658. /** 
  659. * Ajax handler to restore a post from the trash. 
  660. * 
  661. * @since 3.1.0 
  662. * 
  663. * @param string $action Action to perform. 
  664. */ 
  665. function wp_ajax_untrash_post( $action ) { 
  666. if ( empty( $action ) ) 
  667. $action = 'untrash-post'; 
  668. wp_ajax_trash_post( $action ); 
  669.  
  670. /** 
  671. * @since 3.1.0 
  672. * 
  673. * @param string $action 
  674. */ 
  675. function wp_ajax_delete_page( $action ) { 
  676. if ( empty( $action ) ) 
  677. $action = 'delete-page'; 
  678. $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0; 
  679.  
  680. check_ajax_referer( "{$action}_$id" ); 
  681. if ( !current_user_can( 'delete_page', $id ) ) 
  682. wp_die( -1 ); 
  683.  
  684. if ( ! get_post( $id ) ) 
  685. wp_die( 1 ); 
  686.  
  687. if ( wp_delete_post( $id ) ) 
  688. wp_die( 1 ); 
  689. else 
  690. wp_die( 0 ); 
  691.  
  692. /** 
  693. * Ajax handler to dim a comment. 
  694. * 
  695. * @since 3.1.0 
  696. */ 
  697. function wp_ajax_dim_comment() { 
  698. $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0; 
  699.  
  700. if ( !$comment = get_comment( $id ) ) { 
  701. $x = new WP_Ajax_Response( array( 
  702. 'what' => 'comment',  
  703. 'id' => new WP_Error('invalid_comment', sprintf(__('Comment %d does not exist'), $id)) 
  704. ) ); 
  705. $x->send(); 
  706.  
  707. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && ! current_user_can( 'moderate_comments' ) ) 
  708. wp_die( -1 ); 
  709.  
  710. $current = wp_get_comment_status( $comment ); 
  711. if ( isset( $_POST['new'] ) && $_POST['new'] == $current ) 
  712. wp_die( time() ); 
  713.  
  714. check_ajax_referer( "approve-comment_$id" ); 
  715. if ( in_array( $current, array( 'unapproved', 'spam' ) ) ) { 
  716. $result = wp_set_comment_status( $comment, 'approve', true ); 
  717. } else { 
  718. $result = wp_set_comment_status( $comment, 'hold', true ); 
  719.  
  720. if ( is_wp_error($result) ) { 
  721. $x = new WP_Ajax_Response( array( 
  722. 'what' => 'comment',  
  723. 'id' => $result 
  724. ) ); 
  725. $x->send(); 
  726.  
  727. // Decide if we need to send back '1' or a more complicated response including page links and comment counts 
  728. _wp_ajax_delete_comment_response( $comment->comment_ID ); 
  729. wp_die( 0 ); 
  730.  
  731. /** 
  732. * Ajax handler for adding a link category. 
  733. * 
  734. * @since 3.1.0 
  735. * 
  736. * @param string $action Action to perform. 
  737. */ 
  738. function wp_ajax_add_link_category( $action ) { 
  739. if ( empty( $action ) ) 
  740. $action = 'add-link-category'; 
  741. check_ajax_referer( $action ); 
  742. $tax = get_taxonomy( 'link_category' ); 
  743. if ( ! current_user_can( $tax->cap->manage_terms ) ) { 
  744. wp_die( -1 ); 
  745. $names = explode(', ', wp_unslash( $_POST['newcat'] ) ); 
  746. $x = new WP_Ajax_Response(); 
  747. foreach ( $names as $cat_name ) { 
  748. $cat_name = trim($cat_name); 
  749. $slug = sanitize_title($cat_name); 
  750. if ( '' === $slug ) 
  751. continue; 
  752. if ( !$cat_id = term_exists( $cat_name, 'link_category' ) ) 
  753. $cat_id = wp_insert_term( $cat_name, 'link_category' ); 
  754. if ( is_wp_error( $cat_id ) ) { 
  755. continue; 
  756. } elseif ( is_array( $cat_id ) ) { 
  757. $cat_id = $cat_id['term_id']; 
  758. $cat_name = esc_html( $cat_name ); 
  759. $x->add( array( 
  760. 'what' => 'link-category',  
  761. 'id' => $cat_id,  
  762. '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>",  
  763. 'position' => -1 
  764. ) ); 
  765. $x->send(); 
  766.  
  767. /** 
  768. * Ajax handler to add a tag. 
  769. * 
  770. * @since 3.1.0 
  771. */ 
  772. function wp_ajax_add_tag() { 
  773. check_ajax_referer( 'add-tag', '_wpnonce_add-tag' ); 
  774. $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag'; 
  775. $tax = get_taxonomy($taxonomy); 
  776.  
  777. if ( !current_user_can( $tax->cap->edit_terms ) ) 
  778. wp_die( -1 ); 
  779.  
  780. $x = new WP_Ajax_Response(); 
  781.  
  782. $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST ); 
  783.  
  784. if ( !$tag || is_wp_error($tag) || (!$tag = get_term( $tag['term_id'], $taxonomy )) ) { 
  785. $message = __('An error has occurred. Please reload the page and try again.'); 
  786. if ( is_wp_error($tag) && $tag->get_error_message() ) 
  787. $message = $tag->get_error_message(); 
  788.  
  789. $x->add( array( 
  790. 'what' => 'taxonomy',  
  791. 'data' => new WP_Error('error', $message ) 
  792. ) ); 
  793. $x->send(); 
  794.  
  795. $wp_list_table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => $_POST['screen'] ) ); 
  796.  
  797. $level = 0; 
  798. if ( is_taxonomy_hierarchical($taxonomy) ) { 
  799. $level = count( get_ancestors( $tag->term_id, $taxonomy, 'taxonomy' ) ); 
  800. ob_start(); 
  801. $wp_list_table->single_row( $tag, $level ); 
  802. $noparents = ob_get_clean(); 
  803.  
  804. ob_start(); 
  805. $wp_list_table->single_row( $tag ); 
  806. $parents = ob_get_clean(); 
  807.  
  808. $x->add( array( 
  809. 'what' => 'taxonomy',  
  810. 'supplemental' => compact('parents', 'noparents') 
  811. ) ); 
  812. $x->add( array( 
  813. 'what' => 'term',  
  814. 'position' => $level,  
  815. 'supplemental' => (array) $tag 
  816. ) ); 
  817. $x->send(); 
  818.  
  819. /** 
  820. * Ajax handler for getting a tagcloud. 
  821. * 
  822. * @since 3.1.0 
  823. */ 
  824. function wp_ajax_get_tagcloud() { 
  825. if ( ! isset( $_POST['tax'] ) ) { 
  826. wp_die( 0 ); 
  827.  
  828. $taxonomy = sanitize_key( $_POST['tax'] ); 
  829. $tax = get_taxonomy( $taxonomy ); 
  830. if ( ! $tax ) { 
  831. wp_die( 0 ); 
  832.  
  833. if ( ! current_user_can( $tax->cap->assign_terms ) ) { 
  834. wp_die( -1 ); 
  835.  
  836. $tags = get_terms( $taxonomy, array( 'number' => 45, 'orderby' => 'count', 'order' => 'DESC' ) ); 
  837.  
  838. if ( empty( $tags ) ) 
  839. wp_die( $tax->labels->not_found ); 
  840.  
  841. if ( is_wp_error( $tags ) ) 
  842. wp_die( $tags->get_error_message() ); 
  843.  
  844. foreach ( $tags as $key => $tag ) { 
  845. $tags[ $key ]->link = '#'; 
  846. $tags[ $key ]->id = $tag->term_id; 
  847.  
  848. // We need raw tag names here, so don't filter the output 
  849. $return = wp_generate_tag_cloud( $tags, array('filter' => 0) ); 
  850.  
  851. if ( empty($return) ) 
  852. wp_die( 0 ); 
  853.  
  854. echo $return; 
  855.  
  856. wp_die(); 
  857.  
  858. /** 
  859. * Ajax handler for getting comments. 
  860. * 
  861. * @since 3.1.0 
  862. * 
  863. * @global int $post_id 
  864. * 
  865. * @param string $action Action to perform. 
  866. */ 
  867. function wp_ajax_get_comments( $action ) { 
  868. global $post_id; 
  869. if ( empty( $action ) ) { 
  870. $action = 'get-comments'; 
  871. check_ajax_referer( $action ); 
  872.  
  873. if ( empty( $post_id ) && ! empty( $_REQUEST['p'] ) ) { 
  874. $id = absint( $_REQUEST['p'] ); 
  875. if ( ! empty( $id ) ) { 
  876. $post_id = $id; 
  877.  
  878. if ( empty( $post_id ) ) { 
  879. wp_die( -1 ); 
  880.  
  881. $wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); 
  882.  
  883. if ( ! current_user_can( 'edit_post', $post_id ) ) { 
  884. wp_die( -1 ); 
  885.  
  886. $wp_list_table->prepare_items(); 
  887.  
  888. if ( ! $wp_list_table->has_items() ) { 
  889. wp_die( 1 ); 
  890.  
  891. $x = new WP_Ajax_Response(); 
  892. ob_start(); 
  893. foreach ( $wp_list_table->items as $comment ) { 
  894. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && 0 === $comment->comment_approved ) 
  895. continue; 
  896. get_comment( $comment ); 
  897. $wp_list_table->single_row( $comment ); 
  898. $comment_list_item = ob_get_clean(); 
  899.  
  900. $x->add( array( 
  901. 'what' => 'comments',  
  902. 'data' => $comment_list_item 
  903. ) ); 
  904. $x->send(); 
  905.  
  906. /** 
  907. * Ajax handler for replying to a comment. 
  908. * 
  909. * @since 3.1.0 
  910. * 
  911. * @param string $action Action to perform. 
  912. */ 
  913. function wp_ajax_replyto_comment( $action ) { 
  914. if ( empty( $action ) ) 
  915. $action = 'replyto-comment'; 
  916.  
  917. check_ajax_referer( $action, '_ajax_nonce-replyto-comment' ); 
  918.  
  919. $comment_post_ID = (int) $_POST['comment_post_ID']; 
  920. $post = get_post( $comment_post_ID ); 
  921. if ( ! $post ) 
  922. wp_die( -1 ); 
  923.  
  924. if ( !current_user_can( 'edit_post', $comment_post_ID ) ) 
  925. wp_die( -1 ); 
  926.  
  927. if ( empty( $post->post_status ) ) 
  928. wp_die( 1 ); 
  929. elseif ( in_array($post->post_status, array('draft', 'pending', 'trash') ) ) 
  930. wp_die( __('ERROR: you are replying to a comment on a draft post.') ); 
  931.  
  932. $user = wp_get_current_user(); 
  933. if ( $user->exists() ) { 
  934. $user_ID = $user->ID; 
  935. $comment_author = wp_slash( $user->display_name ); 
  936. $comment_author_email = wp_slash( $user->user_email ); 
  937. $comment_author_url = wp_slash( $user->user_url ); 
  938. $comment_content = trim( $_POST['content'] ); 
  939. $comment_type = isset( $_POST['comment_type'] ) ? trim( $_POST['comment_type'] ) : ''; 
  940. if ( current_user_can( 'unfiltered_html' ) ) { 
  941. if ( ! isset( $_POST['_wp_unfiltered_html_comment'] ) ) 
  942. $_POST['_wp_unfiltered_html_comment'] = ''; 
  943.  
  944. if ( wp_create_nonce( 'unfiltered-html-comment' ) != $_POST['_wp_unfiltered_html_comment'] ) { 
  945. kses_remove_filters(); // start with a clean slate 
  946. kses_init_filters(); // set up the filters 
  947. } else { 
  948. wp_die( __( 'Sorry, you must be logged in to reply to a comment.' ) ); 
  949.  
  950. if ( '' == $comment_content ) 
  951. wp_die( __( 'ERROR: please type a comment.' ) ); 
  952.  
  953. $comment_parent = 0; 
  954. if ( isset( $_POST['comment_ID'] ) ) 
  955. $comment_parent = absint( $_POST['comment_ID'] ); 
  956. $comment_auto_approved = false; 
  957. $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID'); 
  958.  
  959. // Automatically approve parent comment. 
  960. if ( !empty($_POST['approve_parent']) ) { 
  961. $parent = get_comment( $comment_parent ); 
  962.  
  963. if ( $parent && $parent->comment_approved === '0' && $parent->comment_post_ID == $comment_post_ID ) { 
  964. if ( ! current_user_can( 'edit_comment', $parent->comment_ID ) ) { 
  965. wp_die( -1 ); 
  966.  
  967. if ( wp_set_comment_status( $parent, 'approve' ) ) 
  968. $comment_auto_approved = true; 
  969.  
  970. $comment_id = wp_new_comment( $commentdata ); 
  971. $comment = get_comment($comment_id); 
  972. if ( ! $comment ) wp_die( 1 ); 
  973.  
  974. $position = ( isset($_POST['position']) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1'; 
  975.  
  976. ob_start(); 
  977. if ( isset( $_REQUEST['mode'] ) && 'dashboard' == $_REQUEST['mode'] ) { 
  978. require_once( ABSPATH . 'wp-admin/includes/dashboard.php' ); 
  979. _wp_dashboard_recent_comments_row( $comment ); 
  980. } else { 
  981. if ( isset( $_REQUEST['mode'] ) && 'single' == $_REQUEST['mode'] ) { 
  982. $wp_list_table = _get_list_table('WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); 
  983. } else { 
  984. $wp_list_table = _get_list_table('WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); 
  985. $wp_list_table->single_row( $comment ); 
  986. $comment_list_item = ob_get_clean(); 
  987.  
  988. $response = array( 
  989. 'what' => 'comment',  
  990. 'id' => $comment->comment_ID,  
  991. 'data' => $comment_list_item,  
  992. 'position' => $position 
  993. ); 
  994.  
  995. $counts = wp_count_comments(); 
  996. $response['supplemental'] = array( 
  997. 'in_moderation' => $counts->moderated,  
  998. 'i18n_comments_text' => sprintf( 
  999. _n( '%s Comment', '%s Comments', $counts->approved ),  
  1000. number_format_i18n( $counts->approved ) 
  1001. ),  
  1002. 'i18n_moderation_text' => sprintf( 
  1003. _nx( '%s in moderation', '%s in moderation', $counts->moderated, 'comments' ),  
  1004. number_format_i18n( $counts->moderated ) 
  1005. ); 
  1006.  
  1007. if ( $comment_auto_approved ) { 
  1008. $response['supplemental']['parent_approved'] = $parent->comment_ID; 
  1009. $response['supplemental']['parent_post_id'] = $parent->comment_post_ID; 
  1010.  
  1011. $x = new WP_Ajax_Response(); 
  1012. $x->add( $response ); 
  1013. $x->send(); 
  1014.  
  1015. /** 
  1016. * Ajax handler for editing a comment. 
  1017. * 
  1018. * @since 3.1.0 
  1019. */ 
  1020. function wp_ajax_edit_comment() { 
  1021. check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' ); 
  1022.  
  1023. $comment_id = (int) $_POST['comment_ID']; 
  1024. if ( ! current_user_can( 'edit_comment', $comment_id ) ) 
  1025. wp_die( -1 ); 
  1026.  
  1027. if ( '' == $_POST['content'] ) 
  1028. wp_die( __( 'ERROR: please type a comment.' ) ); 
  1029.  
  1030. if ( isset( $_POST['status'] ) ) 
  1031. $_POST['comment_status'] = $_POST['status']; 
  1032. edit_comment(); 
  1033.  
  1034. $position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1'; 
  1035. $checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0; 
  1036. $wp_list_table = _get_list_table( $checkbox ? 'WP_Comments_List_Table' : 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); 
  1037.  
  1038. $comment = get_comment( $comment_id ); 
  1039. if ( empty( $comment->comment_ID ) ) 
  1040. wp_die( -1 ); 
  1041.  
  1042. ob_start(); 
  1043. $wp_list_table->single_row( $comment ); 
  1044. $comment_list_item = ob_get_clean(); 
  1045.  
  1046. $x = new WP_Ajax_Response(); 
  1047.  
  1048. $x->add( array( 
  1049. 'what' => 'edit_comment',  
  1050. 'id' => $comment->comment_ID,  
  1051. 'data' => $comment_list_item,  
  1052. 'position' => $position 
  1053. )); 
  1054.  
  1055. $x->send(); 
  1056.  
  1057. /** 
  1058. * Ajax handler for adding a menu item. 
  1059. * 
  1060. * @since 3.1.0 
  1061. */ 
  1062. function wp_ajax_add_menu_item() { 
  1063. check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' ); 
  1064.  
  1065. if ( ! current_user_can( 'edit_theme_options' ) ) 
  1066. wp_die( -1 ); 
  1067.  
  1068. require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; 
  1069.  
  1070. // For performance reasons, we omit some object properties from the checklist. 
  1071. // The following is a hacky way to restore them when adding non-custom items. 
  1072.  
  1073. $menu_items_data = array(); 
  1074. foreach ( (array) $_POST['menu-item'] as $menu_item_data ) { 
  1075. if ( 
  1076. ! empty( $menu_item_data['menu-item-type'] ) && 
  1077. 'custom' != $menu_item_data['menu-item-type'] && 
  1078. ! empty( $menu_item_data['menu-item-object-id'] ) 
  1079. ) { 
  1080. switch( $menu_item_data['menu-item-type'] ) { 
  1081. case 'post_type' : 
  1082. $_object = get_post( $menu_item_data['menu-item-object-id'] ); 
  1083. break; 
  1084.  
  1085. case 'post_type_archive' : 
  1086. $_object = get_post_type_object( $menu_item_data['menu-item-object'] ); 
  1087. break; 
  1088.  
  1089. case 'taxonomy' : 
  1090. $_object = get_term( $menu_item_data['menu-item-object-id'], $menu_item_data['menu-item-object'] ); 
  1091. break; 
  1092.  
  1093. $_menu_items = array_map( 'wp_setup_nav_menu_item', array( $_object ) ); 
  1094. $_menu_item = reset( $_menu_items ); 
  1095.  
  1096. // Restore the missing menu item properties 
  1097. $menu_item_data['menu-item-description'] = $_menu_item->description; 
  1098.  
  1099. $menu_items_data[] = $menu_item_data; 
  1100.  
  1101. $item_ids = wp_save_nav_menu_items( 0, $menu_items_data ); 
  1102. if ( is_wp_error( $item_ids ) ) 
  1103. wp_die( 0 ); 
  1104.  
  1105. $menu_items = array(); 
  1106.  
  1107. foreach ( (array) $item_ids as $menu_item_id ) { 
  1108. $menu_obj = get_post( $menu_item_id ); 
  1109. if ( ! empty( $menu_obj->ID ) ) { 
  1110. $menu_obj = wp_setup_nav_menu_item( $menu_obj ); 
  1111. $menu_obj->label = $menu_obj->title; // don't show "(pending)" in ajax-added items 
  1112. $menu_items[] = $menu_obj; 
  1113.  
  1114. /** This filter is documented in wp-admin/includes/nav-menu.php */ 
  1115. $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $_POST['menu'] ); 
  1116.  
  1117. if ( ! class_exists( $walker_class_name ) ) 
  1118. wp_die( 0 ); 
  1119.  
  1120. if ( ! empty( $menu_items ) ) { 
  1121. $args = array( 
  1122. 'after' => '',  
  1123. 'before' => '',  
  1124. 'link_after' => '',  
  1125. 'link_before' => '',  
  1126. 'walker' => new $walker_class_name,  
  1127. ); 
  1128. echo walk_nav_menu_tree( $menu_items, 0, (object) $args ); 
  1129. wp_die(); 
  1130.  
  1131. /** 
  1132. * Ajax handler for adding meta. 
  1133. * 
  1134. * @since 3.1.0 
  1135. */ 
  1136. function wp_ajax_add_meta() { 
  1137. check_ajax_referer( 'add-meta', '_ajax_nonce-add-meta' ); 
  1138. $c = 0; 
  1139. $pid = (int) $_POST['post_id']; 
  1140. $post = get_post( $pid ); 
  1141.  
  1142. if ( isset($_POST['metakeyselect']) || isset($_POST['metakeyinput']) ) { 
  1143. if ( !current_user_can( 'edit_post', $pid ) ) 
  1144. wp_die( -1 ); 
  1145. if ( isset($_POST['metakeyselect']) && '#NONE#' == $_POST['metakeyselect'] && empty($_POST['metakeyinput']) ) 
  1146. wp_die( 1 ); 
  1147.  
  1148. // If the post is an autodraft, save the post as a draft and then attempt to save the meta. 
  1149. if ( $post->post_status == 'auto-draft' ) { 
  1150. $post_data = array(); 
  1151. $post_data['action'] = 'draft'; // Warning fix 
  1152. $post_data['post_ID'] = $pid; 
  1153. $post_data['post_type'] = $post->post_type; 
  1154. $post_data['post_status'] = 'draft'; 
  1155. $now = current_time('timestamp', 1); 
  1156. /** translators: 1: Post creation date, 2: Post creation time */ 
  1157. $post_data['post_title'] = sprintf( __( 'Draft created on %1$s at %2$s' ), date( __( 'F j, Y' ), $now ), date( __( 'g:i a' ), $now ) ); 
  1158.  
  1159. $pid = edit_post( $post_data ); 
  1160. if ( $pid ) { 
  1161. if ( is_wp_error( $pid ) ) { 
  1162. $x = new WP_Ajax_Response( array( 
  1163. 'what' => 'meta',  
  1164. 'data' => $pid 
  1165. ) ); 
  1166. $x->send(); 
  1167.  
  1168. if ( !$mid = add_meta( $pid ) ) 
  1169. wp_die( __( 'Please provide a custom field value.' ) ); 
  1170. } else { 
  1171. wp_die( 0 ); 
  1172. } elseif ( ! $mid = add_meta( $pid ) ) { 
  1173. wp_die( __( 'Please provide a custom field value.' ) ); 
  1174.  
  1175. $meta = get_metadata_by_mid( 'post', $mid ); 
  1176. $pid = (int) $meta->post_id; 
  1177. $meta = get_object_vars( $meta ); 
  1178. $x = new WP_Ajax_Response( array( 
  1179. 'what' => 'meta',  
  1180. 'id' => $mid,  
  1181. 'data' => _list_meta_row( $meta, $c ),  
  1182. 'position' => 1,  
  1183. 'supplemental' => array('postid' => $pid) 
  1184. ) ); 
  1185. } else { // Update? 
  1186. $mid = (int) key( $_POST['meta'] ); 
  1187. $key = wp_unslash( $_POST['meta'][$mid]['key'] ); 
  1188. $value = wp_unslash( $_POST['meta'][$mid]['value'] ); 
  1189. if ( '' == trim($key) ) 
  1190. wp_die( __( 'Please provide a custom field name.' ) ); 
  1191. if ( '' == trim($value) ) 
  1192. wp_die( __( 'Please provide a custom field value.' ) ); 
  1193. if ( ! $meta = get_metadata_by_mid( 'post', $mid ) ) 
  1194. wp_die( 0 ); // if meta doesn't exist 
  1195. if ( is_protected_meta( $meta->meta_key, 'post' ) || is_protected_meta( $key, 'post' ) || 
  1196. ! current_user_can( 'edit_post_meta', $meta->post_id, $meta->meta_key ) || 
  1197. ! current_user_can( 'edit_post_meta', $meta->post_id, $key ) ) 
  1198. wp_die( -1 ); 
  1199. if ( $meta->meta_value != $value || $meta->meta_key != $key ) { 
  1200. if ( !$u = update_metadata_by_mid( 'post', $mid, $value, $key ) ) 
  1201. wp_die( 0 ); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems). 
  1202.  
  1203. $x = new WP_Ajax_Response( array( 
  1204. 'what' => 'meta',  
  1205. 'id' => $mid, 'old_id' => $mid,  
  1206. 'data' => _list_meta_row( array( 
  1207. 'meta_key' => $key,  
  1208. 'meta_value' => $value,  
  1209. 'meta_id' => $mid 
  1210. ), $c ),  
  1211. 'position' => 0,  
  1212. 'supplemental' => array('postid' => $meta->post_id) 
  1213. ) ); 
  1214. $x->send(); 
  1215.  
  1216. /** 
  1217. * Ajax handler for adding a user. 
  1218. * 
  1219. * @since 3.1.0 
  1220. * 
  1221. * @param string $action Action to perform. 
  1222. */ 
  1223. function wp_ajax_add_user( $action ) { 
  1224. if ( empty( $action ) ) { 
  1225. $action = 'add-user'; 
  1226.  
  1227. check_ajax_referer( $action ); 
  1228. if ( ! current_user_can('create_users') ) 
  1229. wp_die( -1 ); 
  1230. if ( ! $user_id = edit_user() ) { 
  1231. wp_die( 0 ); 
  1232. } elseif ( is_wp_error( $user_id ) ) { 
  1233. $x = new WP_Ajax_Response( array( 
  1234. 'what' => 'user',  
  1235. 'id' => $user_id 
  1236. ) ); 
  1237. $x->send(); 
  1238. $user_object = get_userdata( $user_id ); 
  1239.  
  1240. $wp_list_table = _get_list_table('WP_Users_List_Table'); 
  1241.  
  1242. $role = current( $user_object->roles ); 
  1243.  
  1244. $x = new WP_Ajax_Response( array( 
  1245. 'what' => 'user',  
  1246. 'id' => $user_id,  
  1247. 'data' => $wp_list_table->single_row( $user_object, '', $role ),  
  1248. 'supplemental' => array( 
  1249. 'show-link' => sprintf( 
  1250. /** translators: %s: the new user */ 
  1251. __( 'User %s added' ),  
  1252. '<a href="#user-' . $user_id . '">' . $user_object->user_login . '</a>' 
  1253. ),  
  1254. 'role' => $role,  
  1255. ) ); 
  1256. $x->send(); 
  1257.  
  1258. /** 
  1259. * Ajax handler for closed post boxes. 
  1260. * 
  1261. * @since 3.1.0 
  1262. */ 
  1263. function wp_ajax_closed_postboxes() { 
  1264. check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' ); 
  1265. $closed = isset( $_POST['closed'] ) ? explode( ', ', $_POST['closed']) : array(); 
  1266. $closed = array_filter($closed); 
  1267.  
  1268. $hidden = isset( $_POST['hidden'] ) ? explode( ', ', $_POST['hidden']) : array(); 
  1269. $hidden = array_filter($hidden); 
  1270.  
  1271. $page = isset( $_POST['page'] ) ? $_POST['page'] : ''; 
  1272.  
  1273. if ( $page != sanitize_key( $page ) ) 
  1274. wp_die( 0 ); 
  1275.  
  1276. if ( ! $user = wp_get_current_user() ) 
  1277. wp_die( -1 ); 
  1278.  
  1279. if ( is_array($closed) ) 
  1280. update_user_option($user->ID, "closedpostboxes_$page", $closed, true); 
  1281.  
  1282. if ( is_array($hidden) ) { 
  1283. $hidden = array_diff( $hidden, array('submitdiv', 'linksubmitdiv', 'manage-menu', 'create-menu') ); // postboxes that are always shown 
  1284. update_user_option($user->ID, "metaboxhidden_$page", $hidden, true); 
  1285.  
  1286. wp_die( 1 ); 
  1287.  
  1288. /** 
  1289. * Ajax handler for hidden columns. 
  1290. * 
  1291. * @since 3.1.0 
  1292. */ 
  1293. function wp_ajax_hidden_columns() { 
  1294. check_ajax_referer( 'screen-options-nonce', 'screenoptionnonce' ); 
  1295. $page = isset( $_POST['page'] ) ? $_POST['page'] : ''; 
  1296.  
  1297. if ( $page != sanitize_key( $page ) ) 
  1298. wp_die( 0 ); 
  1299.  
  1300. if ( ! $user = wp_get_current_user() ) 
  1301. wp_die( -1 ); 
  1302.  
  1303. $hidden = ! empty( $_POST['hidden'] ) ? explode( ', ', $_POST['hidden'] ) : array(); 
  1304. update_user_option( $user->ID, "manage{$page}columnshidden", $hidden, true ); 
  1305.  
  1306. wp_die( 1 ); 
  1307.  
  1308. /** 
  1309. * Ajax handler for updating whether to display the welcome panel. 
  1310. * 
  1311. * @since 3.1.0 
  1312. */ 
  1313. function wp_ajax_update_welcome_panel() { 
  1314. check_ajax_referer( 'welcome-panel-nonce', 'welcomepanelnonce' ); 
  1315.  
  1316. if ( ! current_user_can( 'edit_theme_options' ) ) 
  1317. wp_die( -1 ); 
  1318.  
  1319. update_user_meta( get_current_user_id(), 'show_welcome_panel', empty( $_POST['visible'] ) ? 0 : 1 ); 
  1320.  
  1321. wp_die( 1 ); 
  1322.  
  1323. /** 
  1324. * Ajax handler for retrieving menu meta boxes. 
  1325. * 
  1326. * @since 3.1.0 
  1327. */ 
  1328. function wp_ajax_menu_get_metabox() { 
  1329. if ( ! current_user_can( 'edit_theme_options' ) ) 
  1330. wp_die( -1 ); 
  1331.  
  1332. require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; 
  1333.  
  1334. if ( isset( $_POST['item-type'] ) && 'post_type' == $_POST['item-type'] ) { 
  1335. $type = 'posttype'; 
  1336. $callback = 'wp_nav_menu_item_post_type_meta_box'; 
  1337. $items = (array) get_post_types( array( 'show_in_nav_menus' => true ), 'object' ); 
  1338. } elseif ( isset( $_POST['item-type'] ) && 'taxonomy' == $_POST['item-type'] ) { 
  1339. $type = 'taxonomy'; 
  1340. $callback = 'wp_nav_menu_item_taxonomy_meta_box'; 
  1341. $items = (array) get_taxonomies( array( 'show_ui' => true ), 'object' ); 
  1342.  
  1343. if ( ! empty( $_POST['item-object'] ) && isset( $items[$_POST['item-object']] ) ) { 
  1344. $menus_meta_box_object = $items[ $_POST['item-object'] ]; 
  1345.  
  1346. /** This filter is documented in wp-admin/includes/nav-menu.php */ 
  1347. $item = apply_filters( 'nav_menu_meta_box_object', $menus_meta_box_object ); 
  1348. ob_start(); 
  1349. call_user_func_array($callback, array( 
  1350. null,  
  1351. array( 
  1352. 'id' => 'add-' . $item->name,  
  1353. 'title' => $item->labels->name,  
  1354. 'callback' => $callback,  
  1355. 'args' => $item,  
  1356. )); 
  1357.  
  1358. $markup = ob_get_clean(); 
  1359.  
  1360. echo wp_json_encode(array( 
  1361. 'replace-id' => $type . '-' . $item->name,  
  1362. 'markup' => $markup,  
  1363. )); 
  1364.  
  1365. wp_die(); 
  1366.  
  1367. /** 
  1368. * Ajax handler for internal linking. 
  1369. * 
  1370. * @since 3.1.0 
  1371. */ 
  1372. function wp_ajax_wp_link_ajax() { 
  1373. check_ajax_referer( 'internal-linking', '_ajax_linking_nonce' ); 
  1374.  
  1375. $args = array(); 
  1376.  
  1377. if ( isset( $_POST['search'] ) ) { 
  1378. $args['s'] = wp_unslash( $_POST['search'] ); 
  1379.  
  1380. if ( isset( $_POST['term'] ) ) { 
  1381. $args['s'] = wp_unslash( $_POST['term'] ); 
  1382.  
  1383. $args['pagenum'] = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1; 
  1384.  
  1385. require(ABSPATH . WPINC . '/class-wp-editor.php'); 
  1386. $results = _WP_Editors::wp_link_query( $args ); 
  1387.  
  1388. if ( ! isset( $results ) ) 
  1389. wp_die( 0 ); 
  1390.  
  1391. echo wp_json_encode( $results ); 
  1392. echo "\n"; 
  1393.  
  1394. wp_die(); 
  1395.  
  1396. /** 
  1397. * Ajax handler for menu locations save. 
  1398. * 
  1399. * @since 3.1.0 
  1400. */ 
  1401. function wp_ajax_menu_locations_save() { 
  1402. if ( ! current_user_can( 'edit_theme_options' ) ) 
  1403. wp_die( -1 ); 
  1404. check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' ); 
  1405. if ( ! isset( $_POST['menu-locations'] ) ) 
  1406. wp_die( 0 ); 
  1407. set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_POST['menu-locations'] ) ); 
  1408. wp_die( 1 ); 
  1409.  
  1410. /** 
  1411. * Ajax handler for saving the meta box order. 
  1412. * 
  1413. * @since 3.1.0 
  1414. */ 
  1415. function wp_ajax_meta_box_order() { 
  1416. check_ajax_referer( 'meta-box-order' ); 
  1417. $order = isset( $_POST['order'] ) ? (array) $_POST['order'] : false; 
  1418. $page_columns = isset( $_POST['page_columns'] ) ? $_POST['page_columns'] : 'auto'; 
  1419.  
  1420. if ( $page_columns != 'auto' ) 
  1421. $page_columns = (int) $page_columns; 
  1422.  
  1423. $page = isset( $_POST['page'] ) ? $_POST['page'] : ''; 
  1424.  
  1425. if ( $page != sanitize_key( $page ) ) 
  1426. wp_die( 0 ); 
  1427.  
  1428. if ( ! $user = wp_get_current_user() ) 
  1429. wp_die( -1 ); 
  1430.  
  1431. if ( $order ) 
  1432. update_user_option($user->ID, "meta-box-order_$page", $order, true); 
  1433.  
  1434. if ( $page_columns ) 
  1435. update_user_option($user->ID, "screen_layout_$page", $page_columns, true); 
  1436.  
  1437. wp_die( 1 ); 
  1438.  
  1439. /** 
  1440. * Ajax handler for menu quick searching. 
  1441. * 
  1442. * @since 3.1.0 
  1443. */ 
  1444. function wp_ajax_menu_quick_search() { 
  1445. if ( ! current_user_can( 'edit_theme_options' ) ) 
  1446. wp_die( -1 ); 
  1447.  
  1448. require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; 
  1449.  
  1450. _wp_ajax_menu_quick_search( $_POST ); 
  1451.  
  1452. wp_die(); 
  1453.  
  1454. /** 
  1455. * Ajax handler to retrieve a permalink. 
  1456. * 
  1457. * @since 3.1.0 
  1458. */ 
  1459. function wp_ajax_get_permalink() { 
  1460. check_ajax_referer( 'getpermalink', 'getpermalinknonce' ); 
  1461. $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0; 
  1462. wp_die( get_preview_post_link( $post_id ) ); 
  1463.  
  1464. /** 
  1465. * Ajax handler to retrieve a sample permalink. 
  1466. * 
  1467. * @since 3.1.0 
  1468. */ 
  1469. function wp_ajax_sample_permalink() { 
  1470. check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' ); 
  1471. $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0; 
  1472. $title = isset($_POST['new_title'])? $_POST['new_title'] : ''; 
  1473. $slug = isset($_POST['new_slug'])? $_POST['new_slug'] : null; 
  1474. wp_die( get_sample_permalink_html( $post_id, $title, $slug ) ); 
  1475.  
  1476. /** 
  1477. * Ajax handler for Quick Edit saving a post from a list table. 
  1478. * 
  1479. * @since 3.1.0 
  1480. */ 
  1481. function wp_ajax_inline_save() { 
  1482. global $mode; 
  1483.  
  1484. check_ajax_referer( 'inlineeditnonce', '_inline_edit' ); 
  1485.  
  1486. if ( ! isset($_POST['post_ID']) || ! ( $post_ID = (int) $_POST['post_ID'] ) ) 
  1487. wp_die(); 
  1488.  
  1489. if ( 'page' == $_POST['post_type'] ) { 
  1490. if ( ! current_user_can( 'edit_page', $post_ID ) ) 
  1491. wp_die( __( 'Sorry, you are not allowed to edit this page.' ) ); 
  1492. } else { 
  1493. if ( ! current_user_can( 'edit_post', $post_ID ) ) 
  1494. wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); 
  1495.  
  1496. if ( $last = wp_check_post_lock( $post_ID ) ) { 
  1497. $last_user = get_userdata( $last ); 
  1498. $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' ); 
  1499. 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 ) ); 
  1500. wp_die(); 
  1501.  
  1502. $data = &$_POST; 
  1503.  
  1504. $post = get_post( $post_ID, ARRAY_A ); 
  1505.  
  1506. // Since it's coming from the database. 
  1507. $post = wp_slash($post); 
  1508.  
  1509. $data['content'] = $post['post_content']; 
  1510. $data['excerpt'] = $post['post_excerpt']; 
  1511.  
  1512. // Rename. 
  1513. $data['user_ID'] = get_current_user_id(); 
  1514.  
  1515. if ( isset($data['post_parent']) ) 
  1516. $data['parent_id'] = $data['post_parent']; 
  1517.  
  1518. // Status. 
  1519. if ( isset( $data['keep_private'] ) && 'private' == $data['keep_private'] ) { 
  1520. $data['visibility'] = 'private'; 
  1521. $data['post_status'] = 'private'; 
  1522. } else { 
  1523. $data['post_status'] = $data['_status']; 
  1524.  
  1525. if ( empty($data['comment_status']) ) 
  1526. $data['comment_status'] = 'closed'; 
  1527. if ( empty($data['ping_status']) ) 
  1528. $data['ping_status'] = 'closed'; 
  1529.  
  1530. // Exclude terms from taxonomies that are not supposed to appear in Quick Edit. 
  1531. if ( ! empty( $data['tax_input'] ) ) { 
  1532. foreach ( $data['tax_input'] as $taxonomy => $terms ) { 
  1533. $tax_object = get_taxonomy( $taxonomy ); 
  1534. /** This filter is documented in wp-admin/includes/class-wp-posts-list-table.php */ 
  1535. if ( ! apply_filters( 'quick_edit_show_taxonomy', $tax_object->show_in_quick_edit, $taxonomy, $post['post_type'] ) ) { 
  1536. unset( $data['tax_input'][ $taxonomy ] ); 
  1537.  
  1538. // Hack: wp_unique_post_slug() doesn't work for drafts, so we will fake that our post is published. 
  1539. if ( ! empty( $data['post_name'] ) && in_array( $post['post_status'], array( 'draft', 'pending' ) ) ) { 
  1540. $post['post_status'] = 'publish'; 
  1541. $data['post_name'] = wp_unique_post_slug( $data['post_name'], $post['ID'], $post['post_status'], $post['post_type'], $post['post_parent'] ); 
  1542.  
  1543. // Update the post. 
  1544. edit_post(); 
  1545.  
  1546. $wp_list_table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => $_POST['screen'] ) ); 
  1547.  
  1548. $mode = $_POST['post_view'] === 'excerpt' ? 'excerpt' : 'list'; 
  1549.  
  1550. $level = 0; 
  1551. if ( is_post_type_hierarchical( $wp_list_table->screen->post_type ) ) { 
  1552. $request_post = array( get_post( $_POST['post_ID'] ) ); 
  1553. $parent = $request_post[0]->post_parent; 
  1554.  
  1555. while ( $parent > 0 ) { 
  1556. $parent_post = get_post( $parent ); 
  1557. $parent = $parent_post->post_parent; 
  1558. $level++; 
  1559.  
  1560. $wp_list_table->display_rows( array( get_post( $_POST['post_ID'] ) ), $level ); 
  1561.  
  1562. wp_die(); 
  1563.  
  1564. /** 
  1565. * Ajax handler for quick edit saving for a term. 
  1566. * 
  1567. * @since 3.1.0 
  1568. */ 
  1569. function wp_ajax_inline_save_tax() { 
  1570. check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' ); 
  1571.  
  1572. $taxonomy = sanitize_key( $_POST['taxonomy'] ); 
  1573. $tax = get_taxonomy( $taxonomy ); 
  1574. if ( ! $tax ) 
  1575. wp_die( 0 ); 
  1576.  
  1577. if ( ! isset( $_POST['tax_ID'] ) || ! ( $id = (int) $_POST['tax_ID'] ) ) { 
  1578. wp_die( -1 ); 
  1579.  
  1580. if ( ! current_user_can( 'edit_term', $id ) ) { 
  1581. wp_die( -1 ); 
  1582.  
  1583. $wp_list_table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => 'edit-' . $taxonomy ) ); 
  1584.  
  1585. $tag = get_term( $id, $taxonomy ); 
  1586. $_POST['description'] = $tag->description; 
  1587.  
  1588. $updated = wp_update_term($id, $taxonomy, $_POST); 
  1589. if ( $updated && !is_wp_error($updated) ) { 
  1590. $tag = get_term( $updated['term_id'], $taxonomy ); 
  1591. if ( !$tag || is_wp_error( $tag ) ) { 
  1592. if ( is_wp_error($tag) && $tag->get_error_message() ) 
  1593. wp_die( $tag->get_error_message() ); 
  1594. wp_die( __( 'Item not updated.' ) ); 
  1595. } else { 
  1596. if ( is_wp_error($updated) && $updated->get_error_message() ) 
  1597. wp_die( $updated->get_error_message() ); 
  1598. wp_die( __( 'Item not updated.' ) ); 
  1599. $level = 0; 
  1600. $parent = $tag->parent; 
  1601. while ( $parent > 0 ) { 
  1602. $parent_tag = get_term( $parent, $taxonomy ); 
  1603. $parent = $parent_tag->parent; 
  1604. $level++; 
  1605. $wp_list_table->single_row( $tag, $level ); 
  1606. wp_die(); 
  1607.  
  1608. /** 
  1609. * Ajax handler for querying posts for the Find Posts modal. 
  1610. * 
  1611. * @see window.findPosts 
  1612. * 
  1613. * @since 3.1.0 
  1614. */ 
  1615. function wp_ajax_find_posts() { 
  1616. check_ajax_referer( 'find-posts' ); 
  1617.  
  1618. $post_types = get_post_types( array( 'public' => true ), 'objects' ); 
  1619. unset( $post_types['attachment'] ); 
  1620.  
  1621. $s = wp_unslash( $_POST['ps'] ); 
  1622. $args = array( 
  1623. 'post_type' => array_keys( $post_types ),  
  1624. 'post_status' => 'any',  
  1625. 'posts_per_page' => 50,  
  1626. ); 
  1627. if ( '' !== $s ) 
  1628. $args['s'] = $s; 
  1629.  
  1630. $posts = get_posts( $args ); 
  1631.  
  1632. if ( ! $posts ) { 
  1633. wp_send_json_error( __( 'No items found.' ) ); 
  1634.  
  1635. $html = '<table class="widefat"><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>'; 
  1636. $alt = ''; 
  1637. foreach ( $posts as $post ) { 
  1638. $title = trim( $post->post_title ) ? $post->post_title : __( '(no title)' ); 
  1639. $alt = ( 'alternate' == $alt ) ? '' : 'alternate'; 
  1640.  
  1641. switch ( $post->post_status ) { 
  1642. case 'publish' : 
  1643. case 'private' : 
  1644. $stat = __('Published'); 
  1645. break; 
  1646. case 'future' : 
  1647. $stat = __('Scheduled'); 
  1648. break; 
  1649. case 'pending' : 
  1650. $stat = __('Pending Review'); 
  1651. break; 
  1652. case 'draft' : 
  1653. $stat = __('Draft'); 
  1654. break; 
  1655.  
  1656. if ( '0000-00-00 00:00:00' == $post->post_date ) { 
  1657. $time = ''; 
  1658. } else { 
  1659. /** translators: date format in table columns, see https://secure.php.net/date */ 
  1660. $time = mysql2date(__('Y/m/d'), $post->post_date); 
  1661.  
  1662. $html .= '<tr class="' . trim( 'found-posts ' . $alt ) . '"><td class="found-radio"><input type="radio" id="found-'.$post->ID.'" name="found_post_id" value="' . esc_attr($post->ID) . '"></td>'; 
  1663. $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"; 
  1664.  
  1665. $html .= '</tbody></table>'; 
  1666.  
  1667. wp_send_json_success( $html ); 
  1668.  
  1669. /** 
  1670. * Ajax handler for saving the widgets order. 
  1671. * 
  1672. * @since 3.1.0 
  1673. */ 
  1674. function wp_ajax_widgets_order() { 
  1675. check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' ); 
  1676.  
  1677. if ( !current_user_can('edit_theme_options') ) 
  1678. wp_die( -1 ); 
  1679.  
  1680. unset( $_POST['savewidgets'], $_POST['action'] ); 
  1681.  
  1682. // Save widgets order for all sidebars. 
  1683. if ( is_array($_POST['sidebars']) ) { 
  1684. $sidebars = array(); 
  1685. foreach ( $_POST['sidebars'] as $key => $val ) { 
  1686. $sb = array(); 
  1687. if ( !empty($val) ) { 
  1688. $val = explode(', ', $val); 
  1689. foreach ( $val as $k => $v ) { 
  1690. if ( strpos($v, 'widget-') === false ) 
  1691. continue; 
  1692.  
  1693. $sb[$k] = substr($v, strpos($v, '_') + 1); 
  1694. $sidebars[$key] = $sb; 
  1695. wp_set_sidebars_widgets($sidebars); 
  1696. wp_die( 1 ); 
  1697.  
  1698. wp_die( -1 ); 
  1699.  
  1700. /** 
  1701. * Ajax handler for saving a widget. 
  1702. * 
  1703. * @since 3.1.0 
  1704. * 
  1705. * @global array $wp_registered_widgets 
  1706. * @global array $wp_registered_widget_controls 
  1707. * @global array $wp_registered_widget_updates 
  1708. */ 
  1709. function wp_ajax_save_widget() { 
  1710. global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates; 
  1711.  
  1712. check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' ); 
  1713.  
  1714. if ( !current_user_can('edit_theme_options') || !isset($_POST['id_base']) ) 
  1715. wp_die( -1 ); 
  1716.  
  1717. unset( $_POST['savewidgets'], $_POST['action'] ); 
  1718.  
  1719. /** 
  1720. * Fires early when editing the widgets displayed in sidebars. 
  1721. * 
  1722. * @since 2.8.0 
  1723. */ 
  1724. do_action( 'load-widgets.php' ); 
  1725.  
  1726. /** 
  1727. * Fires early when editing the widgets displayed in sidebars. 
  1728. * 
  1729. * @since 2.8.0 
  1730. */ 
  1731. do_action( 'widgets.php' ); 
  1732.  
  1733. /** This action is documented in wp-admin/widgets.php */ 
  1734. do_action( 'sidebar_admin_setup' ); 
  1735.  
  1736. $id_base = $_POST['id_base']; 
  1737. $widget_id = $_POST['widget-id']; 
  1738. $sidebar_id = $_POST['sidebar']; 
  1739. $multi_number = !empty($_POST['multi_number']) ? (int) $_POST['multi_number'] : 0; 
  1740. $settings = isset($_POST['widget-' . $id_base]) && is_array($_POST['widget-' . $id_base]) ? $_POST['widget-' . $id_base] : false; 
  1741. $error = '<p>' . __('An error has occurred. Please reload the page and try again.') . '</p>'; 
  1742.  
  1743. $sidebars = wp_get_sidebars_widgets(); 
  1744. $sidebar = isset($sidebars[$sidebar_id]) ? $sidebars[$sidebar_id] : array(); 
  1745.  
  1746. // Delete. 
  1747. if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) { 
  1748.  
  1749. if ( !isset($wp_registered_widgets[$widget_id]) ) 
  1750. wp_die( $error ); 
  1751.  
  1752. $sidebar = array_diff( $sidebar, array($widget_id) ); 
  1753. $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1'); 
  1754.  
  1755. /** This action is documented in wp-admin/widgets.php */ 
  1756. do_action( 'delete_widget', $widget_id, $sidebar_id, $id_base ); 
  1757.  
  1758. } elseif ( $settings && preg_match( '/__i__|%i%/', key($settings) ) ) { 
  1759. if ( !$multi_number ) 
  1760. wp_die( $error ); 
  1761.  
  1762. $_POST[ 'widget-' . $id_base ] = array( $multi_number => reset( $settings ) ); 
  1763. $widget_id = $id_base . '-' . $multi_number; 
  1764. $sidebar[] = $widget_id; 
  1765. $_POST['widget-id'] = $sidebar; 
  1766.  
  1767. foreach ( (array) $wp_registered_widget_updates as $name => $control ) { 
  1768.  
  1769. if ( $name == $id_base ) { 
  1770. if ( !is_callable( $control['callback'] ) ) 
  1771. continue; 
  1772.  
  1773. ob_start(); 
  1774. call_user_func_array( $control['callback'], $control['params'] ); 
  1775. ob_end_clean(); 
  1776. break; 
  1777.  
  1778. if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) { 
  1779. $sidebars[$sidebar_id] = $sidebar; 
  1780. wp_set_sidebars_widgets($sidebars); 
  1781. echo "deleted:$widget_id"; 
  1782. wp_die(); 
  1783.  
  1784. if ( !empty($_POST['add_new']) ) 
  1785. wp_die(); 
  1786.  
  1787. if ( $form = $wp_registered_widget_controls[$widget_id] ) 
  1788. call_user_func_array( $form['callback'], $form['params'] ); 
  1789.  
  1790. wp_die(); 
  1791.  
  1792. /** 
  1793. * Ajax handler for saving a widget. 
  1794. * 
  1795. * @since 3.9.0 
  1796. * 
  1797. * @global WP_Customize_Manager $wp_customize 
  1798. */ 
  1799. function wp_ajax_update_widget() { 
  1800. global $wp_customize; 
  1801. $wp_customize->widgets->wp_ajax_update_widget(); 
  1802.  
  1803. /** 
  1804. * Ajax handler for removing inactive widgets. 
  1805. * 
  1806. * @since 4.4.0 
  1807. */ 
  1808. function wp_ajax_delete_inactive_widgets() { 
  1809. check_ajax_referer( 'remove-inactive-widgets', 'removeinactivewidgets' ); 
  1810.  
  1811. if ( ! current_user_can( 'edit_theme_options' ) ) { 
  1812. wp_die( -1 ); 
  1813.  
  1814. unset( $_POST['removeinactivewidgets'], $_POST['action'] ); 
  1815.  
  1816. do_action( 'load-widgets.php' ); 
  1817. do_action( 'widgets.php' ); 
  1818. do_action( 'sidebar_admin_setup' ); 
  1819.  
  1820. $sidebars_widgets = wp_get_sidebars_widgets(); 
  1821.  
  1822. foreach ( $sidebars_widgets['wp_inactive_widgets'] as $key => $widget_id ) { 
  1823. $pieces = explode( '-', $widget_id ); 
  1824. $multi_number = array_pop( $pieces ); 
  1825. $id_base = implode( '-', $pieces ); 
  1826. $widget = get_option( 'widget_' . $id_base ); 
  1827. unset( $widget[$multi_number] ); 
  1828. update_option( 'widget_' . $id_base, $widget ); 
  1829. unset( $sidebars_widgets['wp_inactive_widgets'][$key] ); 
  1830.  
  1831. wp_set_sidebars_widgets( $sidebars_widgets ); 
  1832.  
  1833. wp_die(); 
  1834.  
  1835. /** 
  1836. * Ajax handler for uploading attachments 
  1837. * 
  1838. * @since 3.3.0 
  1839. */ 
  1840. function wp_ajax_upload_attachment() { 
  1841. check_ajax_referer( 'media-form' ); 
  1842. /** 
  1843. * This function does not use wp_send_json_success() / wp_send_json_error() 
  1844. * as the html4 Plupload handler requires a text/html content-type for older IE. 
  1845. * See https://core.trac.wordpress.org/ticket/31037 
  1846. */ 
  1847.  
  1848. if ( ! current_user_can( 'upload_files' ) ) { 
  1849. echo wp_json_encode( array( 
  1850. 'success' => false,  
  1851. 'data' => array( 
  1852. 'message' => __( 'Sorry, you are not allowed to upload files.' ),  
  1853. 'filename' => $_FILES['async-upload']['name'],  
  1854. ) ); 
  1855.  
  1856. wp_die(); 
  1857.  
  1858. if ( isset( $_REQUEST['post_id'] ) ) { 
  1859. $post_id = $_REQUEST['post_id']; 
  1860. if ( ! current_user_can( 'edit_post', $post_id ) ) { 
  1861. echo wp_json_encode( array( 
  1862. 'success' => false,  
  1863. 'data' => array( 
  1864. 'message' => __( 'Sorry, you are not allowed to attach files to this post.' ),  
  1865. 'filename' => $_FILES['async-upload']['name'],  
  1866. ) ); 
  1867.  
  1868. wp_die(); 
  1869. } else { 
  1870. $post_id = null; 
  1871.  
  1872. $post_data = isset( $_REQUEST['post_data'] ) ? $_REQUEST['post_data'] : array(); 
  1873.  
  1874. // If the context is custom header or background, make sure the uploaded file is an image. 
  1875. if ( isset( $post_data['context'] ) && in_array( $post_data['context'], array( 'custom-header', 'custom-background' ) ) ) { 
  1876. $wp_filetype = wp_check_filetype_and_ext( $_FILES['async-upload']['tmp_name'], $_FILES['async-upload']['name'] ); 
  1877. if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) { 
  1878. echo wp_json_encode( array( 
  1879. 'success' => false,  
  1880. 'data' => array( 
  1881. 'message' => __( 'The uploaded file is not a valid image. Please try again.' ),  
  1882. 'filename' => $_FILES['async-upload']['name'],  
  1883. ) ); 
  1884.  
  1885. wp_die(); 
  1886.  
  1887. $attachment_id = media_handle_upload( 'async-upload', $post_id, $post_data ); 
  1888.  
  1889. if ( is_wp_error( $attachment_id ) ) { 
  1890. echo wp_json_encode( array( 
  1891. 'success' => false,  
  1892. 'data' => array( 
  1893. 'message' => $attachment_id->get_error_message(),  
  1894. 'filename' => $_FILES['async-upload']['name'],  
  1895. ) ); 
  1896.  
  1897. wp_die(); 
  1898.  
  1899. if ( isset( $post_data['context'] ) && isset( $post_data['theme'] ) ) { 
  1900. if ( 'custom-background' === $post_data['context'] ) 
  1901. update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', $post_data['theme'] ); 
  1902.  
  1903. if ( 'custom-header' === $post_data['context'] ) 
  1904. update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', $post_data['theme'] ); 
  1905.  
  1906. if ( ! $attachment = wp_prepare_attachment_for_js( $attachment_id ) ) 
  1907. wp_die(); 
  1908.  
  1909. echo wp_json_encode( array( 
  1910. 'success' => true,  
  1911. 'data' => $attachment,  
  1912. ) ); 
  1913.  
  1914. wp_die(); 
  1915.  
  1916. /** 
  1917. * Ajax handler for image editing. 
  1918. * 
  1919. * @since 3.1.0 
  1920. */ 
  1921. function wp_ajax_image_editor() { 
  1922. $attachment_id = intval($_POST['postid']); 
  1923. if ( empty($attachment_id) || !current_user_can('edit_post', $attachment_id) ) 
  1924. wp_die( -1 ); 
  1925.  
  1926. check_ajax_referer( "image_editor-$attachment_id" ); 
  1927. include_once( ABSPATH . 'wp-admin/includes/image-edit.php' ); 
  1928.  
  1929. $msg = false; 
  1930. switch ( $_POST['do'] ) { 
  1931. case 'save' : 
  1932. $msg = wp_save_image($attachment_id); 
  1933. $msg = wp_json_encode($msg); 
  1934. wp_die( $msg ); 
  1935. break; 
  1936. case 'scale' : 
  1937. $msg = wp_save_image($attachment_id); 
  1938. break; 
  1939. case 'restore' : 
  1940. $msg = wp_restore_image($attachment_id); 
  1941. break; 
  1942.  
  1943. wp_image_editor($attachment_id, $msg); 
  1944. wp_die(); 
  1945.  
  1946. /** 
  1947. * Ajax handler for setting the featured image. 
  1948. * 
  1949. * @since 3.1.0 
  1950. */ 
  1951. function wp_ajax_set_post_thumbnail() { 
  1952. $json = ! empty( $_REQUEST['json'] ); // New-style request 
  1953.  
  1954. $post_ID = intval( $_POST['post_id'] ); 
  1955. if ( ! current_user_can( 'edit_post', $post_ID ) ) 
  1956. wp_die( -1 ); 
  1957.  
  1958. $thumbnail_id = intval( $_POST['thumbnail_id'] ); 
  1959.  
  1960. if ( $json ) 
  1961. check_ajax_referer( "update-post_$post_ID" ); 
  1962. else 
  1963. check_ajax_referer( "set_post_thumbnail-$post_ID" ); 
  1964.  
  1965. if ( $thumbnail_id == '-1' ) { 
  1966. if ( delete_post_thumbnail( $post_ID ) ) { 
  1967. $return = _wp_post_thumbnail_html( null, $post_ID ); 
  1968. $json ? wp_send_json_success( $return ) : wp_die( $return ); 
  1969. } else { 
  1970. wp_die( 0 ); 
  1971.  
  1972. if ( set_post_thumbnail( $post_ID, $thumbnail_id ) ) { 
  1973. $return = _wp_post_thumbnail_html( $thumbnail_id, $post_ID ); 
  1974. $json ? wp_send_json_success( $return ) : wp_die( $return ); 
  1975.  
  1976. wp_die( 0 ); 
  1977.  
  1978. /** 
  1979. * Ajax handler for retrieving HTML for the featured image. 
  1980. * 
  1981. * @since 4.6.0 
  1982. */ 
  1983. function wp_ajax_get_post_thumbnail_html() { 
  1984. $post_ID = intval( $_POST['post_id'] ); 
  1985.  
  1986. check_ajax_referer( "update-post_$post_ID" ); 
  1987.  
  1988. if ( ! current_user_can( 'edit_post', $post_ID ) ) { 
  1989. wp_die( -1 ); 
  1990.  
  1991. $thumbnail_id = intval( $_POST['thumbnail_id'] ); 
  1992.  
  1993. // For backward compatibility, -1 refers to no featured image. 
  1994. if ( -1 === $thumbnail_id ) { 
  1995. $thumbnail_id = null; 
  1996.  
  1997. $return = _wp_post_thumbnail_html( $thumbnail_id, $post_ID ); 
  1998. wp_send_json_success( $return ); 
  1999.  
  2000. /** 
  2001. * Ajax handler for setting the featured image for an attachment. 
  2002. * 
  2003. * @since 4.0.0 
  2004. * 
  2005. * @see set_post_thumbnail() 
  2006. */ 
  2007. function wp_ajax_set_attachment_thumbnail() { 
  2008. if ( empty( $_POST['urls'] ) || ! is_array( $_POST['urls'] ) ) { 
  2009. wp_send_json_error(); 
  2010.  
  2011. $thumbnail_id = (int) $_POST['thumbnail_id']; 
  2012. if ( empty( $thumbnail_id ) ) { 
  2013. wp_send_json_error(); 
  2014.  
  2015. $post_ids = array(); 
  2016. // For each URL, try to find its corresponding post ID. 
  2017. foreach ( $_POST['urls'] as $url ) { 
  2018. $post_id = attachment_url_to_postid( $url ); 
  2019. if ( ! empty( $post_id ) ) { 
  2020. $post_ids[] = $post_id; 
  2021.  
  2022. if ( empty( $post_ids ) ) { 
  2023. wp_send_json_error(); 
  2024.  
  2025. $success = 0; 
  2026. // For each found attachment, set its thumbnail. 
  2027. foreach ( $post_ids as $post_id ) { 
  2028. if ( ! current_user_can( 'edit_post', $post_id ) ) { 
  2029. continue; 
  2030.  
  2031. if ( set_post_thumbnail( $post_id, $thumbnail_id ) ) { 
  2032. $success++; 
  2033.  
  2034. if ( 0 === $success ) { 
  2035. wp_send_json_error(); 
  2036. } else { 
  2037. wp_send_json_success(); 
  2038.  
  2039. wp_send_json_error(); 
  2040.  
  2041. /** 
  2042. * Ajax handler for date formatting. 
  2043. * 
  2044. * @since 3.1.0 
  2045. */ 
  2046. function wp_ajax_date_format() { 
  2047. wp_die( date_i18n( sanitize_option( 'date_format', wp_unslash( $_POST['date'] ) ) ) ); 
  2048.  
  2049. /** 
  2050. * Ajax handler for time formatting. 
  2051. * 
  2052. * @since 3.1.0 
  2053. */ 
  2054. function wp_ajax_time_format() { 
  2055. wp_die( date_i18n( sanitize_option( 'time_format', wp_unslash( $_POST['date'] ) ) ) ); 
  2056.  
  2057. /** 
  2058. * Ajax handler for saving posts from the fullscreen editor. 
  2059. * 
  2060. * @since 3.1.0 
  2061. * @deprecated 4.3.0 
  2062. */ 
  2063. function wp_ajax_wp_fullscreen_save_post() { 
  2064. $post_id = isset( $_POST['post_ID'] ) ? (int) $_POST['post_ID'] : 0; 
  2065.  
  2066. $post = null; 
  2067.  
  2068. if ( $post_id ) 
  2069. $post = get_post( $post_id ); 
  2070.  
  2071. check_ajax_referer('update-post_' . $post_id, '_wpnonce'); 
  2072.  
  2073. $post_id = edit_post(); 
  2074.  
  2075. if ( is_wp_error( $post_id ) ) { 
  2076. wp_send_json_error(); 
  2077.  
  2078. if ( $post ) { 
  2079. $last_date = mysql2date( __( 'F j, Y' ), $post->post_modified ); 
  2080. $last_time = mysql2date( __( 'g:i a' ), $post->post_modified ); 
  2081. } else { 
  2082. $last_date = date_i18n( __( 'F j, Y' ) ); 
  2083. $last_time = date_i18n( __( 'g:i a' ) ); 
  2084.  
  2085. if ( $last_id = get_post_meta( $post_id, '_edit_last', true ) ) { 
  2086. $last_user = get_userdata( $last_id ); 
  2087. $last_edited = sprintf( __('Last edited by %1$s on %2$s at %3$s'), esc_html( $last_user->display_name ), $last_date, $last_time ); 
  2088. } else { 
  2089. $last_edited = sprintf( __('Last edited on %1$s at %2$s'), $last_date, $last_time ); 
  2090.  
  2091. wp_send_json_success( array( 'last_edited' => $last_edited ) ); 
  2092.  
  2093. /** 
  2094. * Ajax handler for removing a post lock. 
  2095. * 
  2096. * @since 3.1.0 
  2097. */ 
  2098. function wp_ajax_wp_remove_post_lock() { 
  2099. if ( empty( $_POST['post_ID'] ) || empty( $_POST['active_post_lock'] ) ) 
  2100. wp_die( 0 ); 
  2101. $post_id = (int) $_POST['post_ID']; 
  2102. if ( ! $post = get_post( $post_id ) ) 
  2103. wp_die( 0 ); 
  2104.  
  2105. check_ajax_referer( 'update-post_' . $post_id ); 
  2106.  
  2107. if ( ! current_user_can( 'edit_post', $post_id ) ) 
  2108. wp_die( -1 ); 
  2109.  
  2110. $active_lock = array_map( 'absint', explode( ':', $_POST['active_post_lock'] ) ); 
  2111. if ( $active_lock[1] != get_current_user_id() ) 
  2112. wp_die( 0 ); 
  2113.  
  2114. /** 
  2115. * Filters the post lock window duration. 
  2116. * 
  2117. * @since 3.3.0 
  2118. * 
  2119. * @param int $interval The interval in seconds the post lock duration 
  2120. * should last, plus 5 seconds. Default 150. 
  2121. */ 
  2122. $new_lock = ( time() - apply_filters( 'wp_check_post_lock_window', 150 ) + 5 ) . ':' . $active_lock[1]; 
  2123. update_post_meta( $post_id, '_edit_lock', $new_lock, implode( ':', $active_lock ) ); 
  2124. wp_die( 1 ); 
  2125.  
  2126. /** 
  2127. * Ajax handler for dismissing a WordPress pointer. 
  2128. * 
  2129. * @since 3.1.0 
  2130. */ 
  2131. function wp_ajax_dismiss_wp_pointer() { 
  2132. $pointer = $_POST['pointer']; 
  2133. if ( $pointer != sanitize_key( $pointer ) ) 
  2134. wp_die( 0 ); 
  2135.  
  2136. // check_ajax_referer( 'dismiss-pointer_' . $pointer ); 
  2137.  
  2138. $dismissed = array_filter( explode( ', ', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ) ); 
  2139.  
  2140. if ( in_array( $pointer, $dismissed ) ) 
  2141. wp_die( 0 ); 
  2142.  
  2143. $dismissed[] = $pointer; 
  2144. $dismissed = implode( ', ', $dismissed ); 
  2145.  
  2146. update_user_meta( get_current_user_id(), 'dismissed_wp_pointers', $dismissed ); 
  2147. wp_die( 1 ); 
  2148.  
  2149. /** 
  2150. * Ajax handler for getting an attachment. 
  2151. * 
  2152. * @since 3.5.0 
  2153. */ 
  2154. function wp_ajax_get_attachment() { 
  2155. if ( ! isset( $_REQUEST['id'] ) ) 
  2156. wp_send_json_error(); 
  2157.  
  2158. if ( ! $id = absint( $_REQUEST['id'] ) ) 
  2159. wp_send_json_error(); 
  2160.  
  2161. if ( ! $post = get_post( $id ) ) 
  2162. wp_send_json_error(); 
  2163.  
  2164. if ( 'attachment' != $post->post_type ) 
  2165. wp_send_json_error(); 
  2166.  
  2167. if ( ! current_user_can( 'upload_files' ) ) 
  2168. wp_send_json_error(); 
  2169.  
  2170. if ( ! $attachment = wp_prepare_attachment_for_js( $id ) ) 
  2171. wp_send_json_error(); 
  2172.  
  2173. wp_send_json_success( $attachment ); 
  2174.  
  2175. /** 
  2176. * Ajax handler for querying attachments. 
  2177. * 
  2178. * @since 3.5.0 
  2179. */ 
  2180. function wp_ajax_query_attachments() { 
  2181. if ( ! current_user_can( 'upload_files' ) ) 
  2182. wp_send_json_error(); 
  2183.  
  2184. $query = isset( $_REQUEST['query'] ) ? (array) $_REQUEST['query'] : array(); 
  2185. $keys = array( 
  2186. 's', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type',  
  2187. 'post_parent', 'post__in', 'post__not_in', 'year', 'monthnum' 
  2188. ); 
  2189. foreach ( get_taxonomies_for_attachments( 'objects' ) as $t ) { 
  2190. if ( $t->query_var && isset( $query[ $t->query_var ] ) ) { 
  2191. $keys[] = $t->query_var; 
  2192.  
  2193. $query = array_intersect_key( $query, array_flip( $keys ) ); 
  2194. $query['post_type'] = 'attachment'; 
  2195. if ( MEDIA_TRASH 
  2196. && ! empty( $_REQUEST['query']['post_status'] ) 
  2197. && 'trash' === $_REQUEST['query']['post_status'] ) { 
  2198. $query['post_status'] = 'trash'; 
  2199. } else { 
  2200. $query['post_status'] = 'inherit'; 
  2201.  
  2202. if ( current_user_can( get_post_type_object( 'attachment' )->cap->read_private_posts ) ) 
  2203. $query['post_status'] .= ', private'; 
  2204.  
  2205. // Filter query clauses to include filenames. 
  2206. if ( isset( $query['s'] ) ) { 
  2207. add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); 
  2208.  
  2209. /** 
  2210. * Filters the arguments passed to WP_Query during an Ajax 
  2211. * call for querying attachments. 
  2212. * 
  2213. * @since 3.7.0 
  2214. * 
  2215. * @see WP_Query::parse_query() 
  2216. * 
  2217. * @param array $query An array of query variables. 
  2218. */ 
  2219. $query = apply_filters( 'ajax_query_attachments_args', $query ); 
  2220. $query = new WP_Query( $query ); 
  2221.  
  2222. $posts = array_map( 'wp_prepare_attachment_for_js', $query->posts ); 
  2223. $posts = array_filter( $posts ); 
  2224.  
  2225. wp_send_json_success( $posts ); 
  2226.  
  2227. /** 
  2228. * Ajax handler for updating attachment attributes. 
  2229. * 
  2230. * @since 3.5.0 
  2231. */ 
  2232. function wp_ajax_save_attachment() { 
  2233. if ( ! isset( $_REQUEST['id'] ) || ! isset( $_REQUEST['changes'] ) ) 
  2234. wp_send_json_error(); 
  2235.  
  2236. if ( ! $id = absint( $_REQUEST['id'] ) ) 
  2237. wp_send_json_error(); 
  2238.  
  2239. check_ajax_referer( 'update-post_' . $id, 'nonce' ); 
  2240.  
  2241. if ( ! current_user_can( 'edit_post', $id ) ) 
  2242. wp_send_json_error(); 
  2243.  
  2244. $changes = $_REQUEST['changes']; 
  2245. $post = get_post( $id, ARRAY_A ); 
  2246.  
  2247. if ( 'attachment' != $post['post_type'] ) 
  2248. wp_send_json_error(); 
  2249.  
  2250. if ( isset( $changes['parent'] ) ) 
  2251. $post['post_parent'] = $changes['parent']; 
  2252.  
  2253. if ( isset( $changes['title'] ) ) 
  2254. $post['post_title'] = $changes['title']; 
  2255.  
  2256. if ( isset( $changes['caption'] ) ) 
  2257. $post['post_excerpt'] = $changes['caption']; 
  2258.  
  2259. if ( isset( $changes['description'] ) ) 
  2260. $post['post_content'] = $changes['description']; 
  2261.  
  2262. if ( MEDIA_TRASH && isset( $changes['status'] ) ) 
  2263. $post['post_status'] = $changes['status']; 
  2264.  
  2265. if ( isset( $changes['alt'] ) ) { 
  2266. $alt = wp_unslash( $changes['alt'] ); 
  2267. if ( $alt != get_post_meta( $id, '_wp_attachment_image_alt', true ) ) { 
  2268. $alt = wp_strip_all_tags( $alt, true ); 
  2269. update_post_meta( $id, '_wp_attachment_image_alt', wp_slash( $alt ) ); 
  2270.  
  2271. if ( wp_attachment_is( 'audio', $post['ID'] ) ) { 
  2272. $changed = false; 
  2273. $id3data = wp_get_attachment_metadata( $post['ID'] ); 
  2274. if ( ! is_array( $id3data ) ) { 
  2275. $changed = true; 
  2276. $id3data = array(); 
  2277. foreach ( wp_get_attachment_id3_keys( (object) $post, 'edit' ) as $key => $label ) { 
  2278. if ( isset( $changes[ $key ] ) ) { 
  2279. $changed = true; 
  2280. $id3data[ $key ] = sanitize_text_field( wp_unslash( $changes[ $key ] ) ); 
  2281.  
  2282. if ( $changed ) { 
  2283. wp_update_attachment_metadata( $id, $id3data ); 
  2284.  
  2285. if ( MEDIA_TRASH && isset( $changes['status'] ) && 'trash' === $changes['status'] ) { 
  2286. wp_delete_post( $id ); 
  2287. } else { 
  2288. wp_update_post( $post ); 
  2289.  
  2290. wp_send_json_success(); 
  2291.  
  2292. /** 
  2293. * Ajax handler for saving backward compatible attachment attributes. 
  2294. * 
  2295. * @since 3.5.0 
  2296. */ 
  2297. function wp_ajax_save_attachment_compat() { 
  2298. if ( ! isset( $_REQUEST['id'] ) ) 
  2299. wp_send_json_error(); 
  2300.  
  2301. if ( ! $id = absint( $_REQUEST['id'] ) ) 
  2302. wp_send_json_error(); 
  2303.  
  2304. if ( empty( $_REQUEST['attachments'] ) || empty( $_REQUEST['attachments'][ $id ] ) ) 
  2305. wp_send_json_error(); 
  2306. $attachment_data = $_REQUEST['attachments'][ $id ]; 
  2307.  
  2308. check_ajax_referer( 'update-post_' . $id, 'nonce' ); 
  2309.  
  2310. if ( ! current_user_can( 'edit_post', $id ) ) 
  2311. wp_send_json_error(); 
  2312.  
  2313. $post = get_post( $id, ARRAY_A ); 
  2314.  
  2315. if ( 'attachment' != $post['post_type'] ) 
  2316. wp_send_json_error(); 
  2317.  
  2318. /** This filter is documented in wp-admin/includes/media.php */ 
  2319. $post = apply_filters( 'attachment_fields_to_save', $post, $attachment_data ); 
  2320.  
  2321. if ( isset( $post['errors'] ) ) { 
  2322. $errors = $post['errors']; // @todo return me and display me! 
  2323. unset( $post['errors'] ); 
  2324.  
  2325. wp_update_post( $post ); 
  2326.  
  2327. foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) { 
  2328. if ( isset( $attachment_data[ $taxonomy ] ) ) 
  2329. wp_set_object_terms( $id, array_map( 'trim', preg_split( '/, +/', $attachment_data[ $taxonomy ] ) ), $taxonomy, false ); 
  2330.  
  2331. if ( ! $attachment = wp_prepare_attachment_for_js( $id ) ) 
  2332. wp_send_json_error(); 
  2333.  
  2334. wp_send_json_success( $attachment ); 
  2335.  
  2336. /** 
  2337. * Ajax handler for saving the attachment order. 
  2338. * 
  2339. * @since 3.5.0 
  2340. */ 
  2341. function wp_ajax_save_attachment_order() { 
  2342. if ( ! isset( $_REQUEST['post_id'] ) ) 
  2343. wp_send_json_error(); 
  2344.  
  2345. if ( ! $post_id = absint( $_REQUEST['post_id'] ) ) 
  2346. wp_send_json_error(); 
  2347.  
  2348. if ( empty( $_REQUEST['attachments'] ) ) 
  2349. wp_send_json_error(); 
  2350.  
  2351. check_ajax_referer( 'update-post_' . $post_id, 'nonce' ); 
  2352.  
  2353. $attachments = $_REQUEST['attachments']; 
  2354.  
  2355. if ( ! current_user_can( 'edit_post', $post_id ) ) 
  2356. wp_send_json_error(); 
  2357.  
  2358. foreach ( $attachments as $attachment_id => $menu_order ) { 
  2359. if ( ! current_user_can( 'edit_post', $attachment_id ) ) 
  2360. continue; 
  2361. if ( ! $attachment = get_post( $attachment_id ) ) 
  2362. continue; 
  2363. if ( 'attachment' != $attachment->post_type ) 
  2364. continue; 
  2365.  
  2366. wp_update_post( array( 'ID' => $attachment_id, 'menu_order' => $menu_order ) ); 
  2367.  
  2368. wp_send_json_success(); 
  2369.  
  2370. /** 
  2371. * Ajax handler for sending an attachment to the editor. 
  2372. * 
  2373. * Generates the HTML to send an attachment to the editor. 
  2374. * Backward compatible with the {@see 'media_send_to_editor'} filter 
  2375. * and the chain of filters that follow. 
  2376. * 
  2377. * @since 3.5.0 
  2378. */ 
  2379. function wp_ajax_send_attachment_to_editor() { 
  2380. check_ajax_referer( 'media-send-to-editor', 'nonce' ); 
  2381.  
  2382. $attachment = wp_unslash( $_POST['attachment'] ); 
  2383.  
  2384. $id = intval( $attachment['id'] ); 
  2385.  
  2386. if ( ! $post = get_post( $id ) ) 
  2387. wp_send_json_error(); 
  2388.  
  2389. if ( 'attachment' != $post->post_type ) 
  2390. wp_send_json_error(); 
  2391.  
  2392. if ( current_user_can( 'edit_post', $id ) ) { 
  2393. // If this attachment is unattached, attach it. Primarily a back compat thing. 
  2394. if ( 0 == $post->post_parent && $insert_into_post_id = intval( $_POST['post_id'] ) ) { 
  2395. wp_update_post( array( 'ID' => $id, 'post_parent' => $insert_into_post_id ) ); 
  2396.  
  2397. $url = empty( $attachment['url'] ) ? '' : $attachment['url']; 
  2398. $rel = ( strpos( $url, 'attachment_id') || get_attachment_link( $id ) == $url ); 
  2399.  
  2400. remove_filter( 'media_send_to_editor', 'image_media_send_to_editor' ); 
  2401.  
  2402. if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) { 
  2403. $align = isset( $attachment['align'] ) ? $attachment['align'] : 'none'; 
  2404. $size = isset( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium'; 
  2405. $alt = isset( $attachment['image_alt'] ) ? $attachment['image_alt'] : ''; 
  2406.  
  2407. // No whitespace-only captions. 
  2408. $caption = isset( $attachment['post_excerpt'] ) ? $attachment['post_excerpt'] : ''; 
  2409. if ( '' === trim( $caption ) ) { 
  2410. $caption = ''; 
  2411.  
  2412. $title = ''; // We no longer insert title tags into <img> tags, as they are redundant. 
  2413. $html = get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ); 
  2414. } elseif ( wp_attachment_is( 'video', $post ) || wp_attachment_is( 'audio', $post ) ) { 
  2415. $html = stripslashes_deep( $_POST['html'] ); 
  2416. } else { 
  2417. $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : ''; 
  2418. $rel = $rel ? ' rel="attachment wp-att-' . $id . '"' : ''; // Hard-coded string, $id is already sanitized 
  2419.  
  2420. if ( ! empty( $url ) ) { 
  2421. $html = '<a href="' . esc_url( $url ) . '"' . $rel . '>' . $html . '</a>'; 
  2422.  
  2423. /** This filter is documented in wp-admin/includes/media.php */ 
  2424. $html = apply_filters( 'media_send_to_editor', $html, $id, $attachment ); 
  2425.  
  2426. wp_send_json_success( $html ); 
  2427.  
  2428. /** 
  2429. * Ajax handler for sending a link to the editor. 
  2430. * 
  2431. * Generates the HTML to send a non-image embed link to the editor. 
  2432. * 
  2433. * Backward compatible with the following filters: 
  2434. * - file_send_to_editor_url 
  2435. * - audio_send_to_editor_url 
  2436. * - video_send_to_editor_url 
  2437. * 
  2438. * @since 3.5.0 
  2439. * 
  2440. * @global WP_Post $post 
  2441. * @global WP_Embed $wp_embed 
  2442. */ 
  2443. function wp_ajax_send_link_to_editor() { 
  2444. global $post, $wp_embed; 
  2445.  
  2446. check_ajax_referer( 'media-send-to-editor', 'nonce' ); 
  2447.  
  2448. if ( ! $src = wp_unslash( $_POST['src'] ) ) 
  2449. wp_send_json_error(); 
  2450.  
  2451. if ( ! strpos( $src, '://' ) ) 
  2452. $src = 'http://' . $src; 
  2453.  
  2454. if ( ! $src = esc_url_raw( $src ) ) 
  2455. wp_send_json_error(); 
  2456.  
  2457. if ( ! $link_text = trim( wp_unslash( $_POST['link_text'] ) ) ) 
  2458. $link_text = wp_basename( $src ); 
  2459.  
  2460. $post = get_post( isset( $_POST['post_id'] ) ? $_POST['post_id'] : 0 ); 
  2461.  
  2462. // Ping WordPress for an embed. 
  2463. $check_embed = $wp_embed->run_shortcode( '' ); 
  2464.  
  2465. // Fallback that WordPress creates when no oEmbed was found. 
  2466. $fallback = $wp_embed->maybe_make_link( $src ); 
  2467.  
  2468. if ( $check_embed !== $fallback ) { 
  2469. // TinyMCE view for '; 
  2470. } elseif ( $link_text ) { 
  2471. $html = '<a href="' . esc_url( $src ) . '">' . $link_text . '</a>'; 
  2472. } else { 
  2473. $html = ''; 
  2474.  
  2475. // Figure out what filter to run: 
  2476. $type = 'file'; 
  2477. if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) ) 
  2478. && ( 'audio' == $ext_type || 'video' == $ext_type ) ) 
  2479. $type = $ext_type; 
  2480.  
  2481. /** This filter is documented in wp-admin/includes/media.php */ 
  2482. $html = apply_filters( $type . '_send_to_editor_url', $html, $src, $link_text ); 
  2483.  
  2484. wp_send_json_success( $html ); 
  2485.  
  2486. /** 
  2487. * Ajax handler for the Heartbeat API. 
  2488. * 
  2489. * Runs when the user is logged in. 
  2490. * 
  2491. * @since 3.6.0 
  2492. */ 
  2493. function wp_ajax_heartbeat() { 
  2494. if ( empty( $_POST['_nonce'] ) ) { 
  2495. wp_send_json_error(); 
  2496.  
  2497. $response = $data = array(); 
  2498. $nonce_state = wp_verify_nonce( $_POST['_nonce'], 'heartbeat-nonce' ); 
  2499.  
  2500. // screen_id is the same as $current_screen->id and the JS global 'pagenow'. 
  2501. if ( ! empty( $_POST['screen_id'] ) ) { 
  2502. $screen_id = sanitize_key($_POST['screen_id']); 
  2503. } else { 
  2504. $screen_id = 'front'; 
  2505.  
  2506. if ( ! empty( $_POST['data'] ) ) { 
  2507. $data = wp_unslash( (array) $_POST['data'] ); 
  2508.  
  2509. if ( 1 !== $nonce_state ) { 
  2510. $response = apply_filters( 'wp_refresh_nonces', $response, $data, $screen_id ); 
  2511.  
  2512. if ( false === $nonce_state ) { 
  2513. // User is logged in but nonces have expired. 
  2514. $response['nonces_expired'] = true; 
  2515. wp_send_json( $response ); 
  2516.  
  2517. if ( ! empty( $data ) ) { 
  2518. /** 
  2519. * Filters the Heartbeat response received. 
  2520. * 
  2521. * @since 3.6.0 
  2522. * 
  2523. * @param array $response The Heartbeat response. 
  2524. * @param array $data The $_POST data sent. 
  2525. * @param string $screen_id The screen id. 
  2526. */ 
  2527. $response = apply_filters( 'heartbeat_received', $response, $data, $screen_id ); 
  2528.  
  2529. /** 
  2530. * Filters the Heartbeat response sent. 
  2531. * 
  2532. * @since 3.6.0 
  2533. * 
  2534. * @param array $response The Heartbeat response. 
  2535. * @param string $screen_id The screen id. 
  2536. */ 
  2537. $response = apply_filters( 'heartbeat_send', $response, $screen_id ); 
  2538.  
  2539. /** 
  2540. * Fires when Heartbeat ticks in logged-in environments. 
  2541. * 
  2542. * Allows the transport to be easily replaced with long-polling. 
  2543. * 
  2544. * @since 3.6.0 
  2545. * 
  2546. * @param array $response The Heartbeat response. 
  2547. * @param string $screen_id The screen id. 
  2548. */ 
  2549. do_action( 'heartbeat_tick', $response, $screen_id ); 
  2550.  
  2551. // Send the current time according to the server 
  2552. $response['server_time'] = time(); 
  2553.  
  2554. wp_send_json( $response ); 
  2555.  
  2556. /** 
  2557. * Ajax handler for getting revision diffs. 
  2558. * 
  2559. * @since 3.6.0 
  2560. */ 
  2561. function wp_ajax_get_revision_diffs() { 
  2562. require ABSPATH . 'wp-admin/includes/revision.php'; 
  2563.  
  2564. if ( ! $post = get_post( (int) $_REQUEST['post_id'] ) ) 
  2565. wp_send_json_error(); 
  2566.  
  2567. if ( ! current_user_can( 'edit_post', $post->ID ) ) 
  2568. wp_send_json_error(); 
  2569.  
  2570. // Really just pre-loading the cache here. 
  2571. if ( ! $revisions = wp_get_post_revisions( $post->ID, array( 'check_enabled' => false ) ) ) 
  2572. wp_send_json_error(); 
  2573.  
  2574. $return = array(); 
  2575. @set_time_limit( 0 ); 
  2576.  
  2577. foreach ( $_REQUEST['compare'] as $compare_key ) { 
  2578. list( $compare_from, $compare_to ) = explode( ':', $compare_key ); // from:to 
  2579.  
  2580. $return[] = array( 
  2581. 'id' => $compare_key,  
  2582. 'fields' => wp_get_revision_ui_diff( $post, $compare_from, $compare_to ),  
  2583. ); 
  2584. wp_send_json_success( $return ); 
  2585.  
  2586. /** 
  2587. * Ajax handler for auto-saving the selected color scheme for 
  2588. * a user's own profile. 
  2589. * 
  2590. * @since 3.8.0 
  2591. * 
  2592. * @global array $_wp_admin_css_colors 
  2593. */ 
  2594. function wp_ajax_save_user_color_scheme() { 
  2595. global $_wp_admin_css_colors; 
  2596.  
  2597. check_ajax_referer( 'save-color-scheme', 'nonce' ); 
  2598.  
  2599. $color_scheme = sanitize_key( $_POST['color_scheme'] ); 
  2600.  
  2601. if ( ! isset( $_wp_admin_css_colors[ $color_scheme ] ) ) { 
  2602. wp_send_json_error(); 
  2603.  
  2604. $previous_color_scheme = get_user_meta( get_current_user_id(), 'admin_color', true ); 
  2605. update_user_meta( get_current_user_id(), 'admin_color', $color_scheme ); 
  2606.  
  2607. wp_send_json_success( array( 
  2608. 'previousScheme' => 'admin-color-' . $previous_color_scheme,  
  2609. 'currentScheme' => 'admin-color-' . $color_scheme 
  2610. ) ); 
  2611.  
  2612. /** 
  2613. * Ajax handler for getting themes from themes_api(). 
  2614. * 
  2615. * @since 3.9.0 
  2616. * 
  2617. * @global array $themes_allowedtags 
  2618. * @global array $theme_field_defaults 
  2619. */ 
  2620. function wp_ajax_query_themes() { 
  2621. global $themes_allowedtags, $theme_field_defaults; 
  2622.  
  2623. if ( ! current_user_can( 'install_themes' ) ) { 
  2624. wp_send_json_error(); 
  2625.  
  2626. $args = wp_parse_args( wp_unslash( $_REQUEST['request'] ), array( 
  2627. 'per_page' => 20,  
  2628. 'fields' => $theme_field_defaults 
  2629. ) ); 
  2630.  
  2631. if ( isset( $args['browse'] ) && 'favorites' === $args['browse'] && ! isset( $args['user'] ) ) { 
  2632. $user = get_user_option( 'wporg_favorites' ); 
  2633. if ( $user ) { 
  2634. $args['user'] = $user; 
  2635.  
  2636. $old_filter = isset( $args['browse'] ) ? $args['browse'] : 'search'; 
  2637.  
  2638. /** This filter is documented in wp-admin/includes/class-wp-theme-install-list-table.php */ 
  2639. $args = apply_filters( 'install_themes_table_api_args_' . $old_filter, $args ); 
  2640.  
  2641. $api = themes_api( 'query_themes', $args ); 
  2642.  
  2643. if ( is_wp_error( $api ) ) { 
  2644. wp_send_json_error(); 
  2645.  
  2646. $update_php = network_admin_url( 'update.php?action=install-theme' ); 
  2647. foreach ( $api->themes as &$theme ) { 
  2648. $theme->install_url = add_query_arg( array( 
  2649. 'theme' => $theme->slug,  
  2650. '_wpnonce' => wp_create_nonce( 'install-theme_' . $theme->slug ) 
  2651. ), $update_php ); 
  2652.  
  2653. if ( current_user_can( 'switch_themes' ) ) { 
  2654. if ( is_multisite() ) { 
  2655. $theme->activate_url = add_query_arg( array( 
  2656. 'action' => 'enable',  
  2657. '_wpnonce' => wp_create_nonce( 'enable-theme_' . $theme->slug ),  
  2658. 'theme' => $theme->slug,  
  2659. ), network_admin_url( 'themes.php' ) ); 
  2660. } else { 
  2661. $theme->activate_url = add_query_arg( array( 
  2662. 'action' => 'activate',  
  2663. '_wpnonce' => wp_create_nonce( 'switch-theme_' . $theme->slug ),  
  2664. 'stylesheet' => $theme->slug,  
  2665. ), admin_url( 'themes.php' ) ); 
  2666.  
  2667. if ( ! is_multisite() && current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { 
  2668. $theme->customize_url = add_query_arg( array( 
  2669. 'return' => urlencode( network_admin_url( 'theme-install.php', 'relative' ) ),  
  2670. ), wp_customize_url( $theme->slug ) ); 
  2671.  
  2672. $theme->name = wp_kses( $theme->name, $themes_allowedtags ); 
  2673. $theme->author = wp_kses( $theme->author, $themes_allowedtags ); 
  2674. $theme->version = wp_kses( $theme->version, $themes_allowedtags ); 
  2675. $theme->description = wp_kses( $theme->description, $themes_allowedtags ); 
  2676. $theme->stars = wp_star_rating( array( 'rating' => $theme->rating, 'type' => 'percent', 'number' => $theme->num_ratings, 'echo' => false ) ); 
  2677. $theme->num_ratings = number_format_i18n( $theme->num_ratings ); 
  2678. $theme->preview_url = set_url_scheme( $theme->preview_url ); 
  2679.  
  2680. wp_send_json_success( $api ); 
  2681.  
  2682. /** 
  2683. * Apply Ajax handlers to a string. 
  2684. * 
  2685. * @since 4.0.0 
  2686. * 
  2687. * @global WP_Post $post Global $post. 
  2688. * @global WP_Embed $wp_embed Embed API instance. 
  2689. * @global WP_Scripts $wp_scripts 
  2690. */ 
  2691. function wp_ajax_parse_embed() { 
  2692. global $post, $wp_embed; 
  2693.  
  2694. if ( ! $post = get_post( (int) $_POST['post_ID'] ) ) { 
  2695. wp_send_json_error(); 
  2696.  
  2697. if ( empty( $_POST['shortcode'] ) || ! current_user_can( 'edit_post', $post->ID ) ) { 
  2698. wp_send_json_error(); 
  2699.  
  2700. $shortcode = wp_unslash( $_POST['shortcode'] ); 
  2701.  
  2702. preg_match( '/' . get_shortcode_regex() . '/s', $shortcode, $matches ); 
  2703. $atts = shortcode_parse_atts( $matches[3] ); 
  2704. if ( ! empty( $matches[5] ) ) { 
  2705. $url = $matches[5]; 
  2706. } elseif ( ! empty( $atts['src'] ) ) { 
  2707. $url = $atts['src']; 
  2708. } else { 
  2709. $url = ''; 
  2710.  
  2711. $parsed = false; 
  2712. setup_postdata( $post ); 
  2713.  
  2714. $wp_embed->return_false_on_fail = true; 
  2715.  
  2716. if ( is_ssl() && 0 === strpos( $url, 'http://' ) )
  2717. // Admin is ssl and the user pasted non-ssl URL. 
  2718. // Check if the provider supports ssl embeds and use that for the preview. 
  2719. $ssl_shortcode = preg_replace( '%^(\\]*\\])http://%i', '$1https://', $shortcode ); 
  2720. $parsed = $wp_embed->run_shortcode( $ssl_shortcode ); 
  2721.  
  2722. if ( ! $parsed ) { 
  2723. $no_ssl_support = true; 
  2724.  
  2725. if ( $url && ! $parsed ) { 
  2726. $parsed = $wp_embed->run_shortcode( $shortcode ); 
  2727.  
  2728. if ( ! $parsed ) { 
  2729. wp_send_json_error( array( 
  2730. 'type' => 'not-embeddable',  
  2731. 'message' => sprintf( __( '%s failed to embed.' ), '<code>' . esc_html( $url ) . '</code>' ),  
  2732. ) ); 
  2733.  
  2734. if ( has_shortcode( $parsed, 'audio' ) || has_shortcode( $parsed, 'video' ) ) { 
  2735. $styles = ''; 
  2736. $mce_styles = wpview_media_sandbox_styles(); 
  2737. foreach ( $mce_styles as $style ) { 
  2738. $styles .= sprintf( '<link rel="stylesheet" href="%s"/>', $style ); 
  2739.  
  2740. $html = do_shortcode( $parsed ); 
  2741.  
  2742. global $wp_scripts; 
  2743. if ( ! empty( $wp_scripts ) ) { 
  2744. $wp_scripts->done = array(); 
  2745. ob_start(); 
  2746. wp_print_scripts( 'wp-mediaelement' ); 
  2747. $scripts = ob_get_clean(); 
  2748.  
  2749. $parsed = $styles . $html . $scripts; 
  2750.  
  2751.  
  2752. if ( ! empty( $no_ssl_support ) || ( is_ssl() && ( preg_match( '%<(iframe|script|embed) [^>]*src="http://%', $parsed ) || 
  2753. preg_match( '%<link [^>]*href="http://%', $parsed ) ) ) ) { 
  2754. // Admin is ssl and the embed is not. Iframes, scripts, and other "active content" will be blocked. 
  2755. wp_send_json_error( array( 
  2756. 'type' => 'not-ssl',  
  2757. 'message' => __( 'This preview is unavailable in the editor.' ),  
  2758. ) ); 
  2759.  
  2760. wp_send_json_success( array( 
  2761. 'body' => $parsed,  
  2762. 'attr' => $wp_embed->last_attr 
  2763. ) ); 
  2764.  
  2765. /** 
  2766. * @since 4.0.0 
  2767. * 
  2768. * @global WP_Post $post 
  2769. * @global WP_Scripts $wp_scripts 
  2770. */ 
  2771. function wp_ajax_parse_media_shortcode() { 
  2772. global $post, $wp_scripts; 
  2773.  
  2774. if ( empty( $_POST['shortcode'] ) ) { 
  2775. wp_send_json_error(); 
  2776.  
  2777. $shortcode = wp_unslash( $_POST['shortcode'] ); 
  2778.  
  2779. if ( ! empty( $_POST['post_ID'] ) ) { 
  2780. $post = get_post( (int) $_POST['post_ID'] ); 
  2781.  
  2782. // the embed shortcode requires a post 
  2783. if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) { 
  2784. if ( 'embed' === $shortcode ) { 
  2785. wp_send_json_error(); 
  2786. } else { 
  2787. setup_postdata( $post ); 
  2788.  
  2789. $parsed = do_shortcode( $shortcode ); 
  2790.  
  2791. if ( empty( $parsed ) ) { 
  2792. wp_send_json_error( array( 
  2793. 'type' => 'no-items',  
  2794. 'message' => __( 'No items found.' ),  
  2795. ) ); 
  2796.  
  2797. $head = ''; 
  2798. $styles = wpview_media_sandbox_styles(); 
  2799.  
  2800. foreach ( $styles as $style ) { 
  2801. $head .= '<link type="text/css" rel="stylesheet" href="' . $style . '">'; 
  2802.  
  2803. if ( ! empty( $wp_scripts ) ) { 
  2804. $wp_scripts->done = array(); 
  2805.  
  2806. ob_start(); 
  2807.  
  2808. echo $parsed; 
  2809.  
  2810. if ( 'playlist' === $_REQUEST['type'] ) { 
  2811. wp_underscore_playlist_templates(); 
  2812.  
  2813. wp_print_scripts( 'wp-playlist' ); 
  2814. } else { 
  2815. wp_print_scripts( array( 'froogaloop', 'wp-mediaelement' ) ); 
  2816.  
  2817. wp_send_json_success( array( 
  2818. 'head' => $head,  
  2819. 'body' => ob_get_clean() 
  2820. ) ); 
  2821.  
  2822. /** 
  2823. * Ajax handler for destroying multiple open sessions for a user. 
  2824. * 
  2825. * @since 4.1.0 
  2826. */ 
  2827. function wp_ajax_destroy_sessions() { 
  2828. $user = get_userdata( (int) $_POST['user_id'] ); 
  2829. if ( $user ) { 
  2830. if ( ! current_user_can( 'edit_user', $user->ID ) ) { 
  2831. $user = false; 
  2832. } elseif ( ! wp_verify_nonce( $_POST['nonce'], 'update-user_' . $user->ID ) ) { 
  2833. $user = false; 
  2834.  
  2835. if ( ! $user ) { 
  2836. wp_send_json_error( array( 
  2837. 'message' => __( 'Could not log out user sessions. Please try again.' ),  
  2838. ) ); 
  2839.  
  2840. $sessions = WP_Session_Tokens::get_instance( $user->ID ); 
  2841.  
  2842. if ( $user->ID === get_current_user_id() ) { 
  2843. $sessions->destroy_others( wp_get_session_token() ); 
  2844. $message = __( 'You are now logged out everywhere else.' ); 
  2845. } else { 
  2846. $sessions->destroy_all(); 
  2847. /** translators: 1: User's display name. */ 
  2848. $message = sprintf( __( '%s has been logged out.' ), $user->display_name ); 
  2849.  
  2850. wp_send_json_success( array( 'message' => $message ) ); 
  2851.  
  2852. /** 
  2853. * Ajax handler for saving a post from Press This. 
  2854. * 
  2855. * @since 4.2.0 
  2856. */ 
  2857. function wp_ajax_press_this_save_post() { 
  2858. include( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' ); 
  2859. $wp_press_this = new WP_Press_This(); 
  2860. $wp_press_this->save_post(); 
  2861.  
  2862. /** 
  2863. * Ajax handler for creating new category from Press This. 
  2864. * 
  2865. * @since 4.2.0 
  2866. */ 
  2867. function wp_ajax_press_this_add_category() { 
  2868. include( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' ); 
  2869. $wp_press_this = new WP_Press_This(); 
  2870. $wp_press_this->add_category(); 
  2871.  
  2872. /** 
  2873. * Ajax handler for cropping an image. 
  2874. * 
  2875. * @since 4.3.0 
  2876. */ 
  2877. function wp_ajax_crop_image() { 
  2878. $attachment_id = absint( $_POST['id'] ); 
  2879.  
  2880. check_ajax_referer( 'image_editor-' . $attachment_id, 'nonce' ); 
  2881. if ( ! current_user_can( 'customize' ) ) { 
  2882. wp_send_json_error(); 
  2883.  
  2884. $context = str_replace( '_', '-', $_POST['context'] ); 
  2885. $data = array_map( 'absint', $_POST['cropDetails'] ); 
  2886. $cropped = wp_crop_image( $attachment_id, $data['x1'], $data['y1'], $data['width'], $data['height'], $data['dst_width'], $data['dst_height'] ); 
  2887.  
  2888. if ( ! $cropped || is_wp_error( $cropped ) ) { 
  2889. wp_send_json_error( array( 'message' => __( 'Image could not be processed.' ) ) ); 
  2890.  
  2891. switch ( $context ) { 
  2892. case 'site-icon': 
  2893. require_once ABSPATH . '/wp-admin/includes/class-wp-site-icon.php'; 
  2894. $wp_site_icon = new WP_Site_Icon(); 
  2895.  
  2896. // Skip creating a new attachment if the attachment is a Site Icon. 
  2897. if ( get_post_meta( $attachment_id, '_wp_attachment_context', true ) == $context ) { 
  2898.  
  2899. // Delete the temporary cropped file, we don't need it. 
  2900. wp_delete_file( $cropped ); 
  2901.  
  2902. // Additional sizes in wp_prepare_attachment_for_js(). 
  2903. add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) ); 
  2904. break; 
  2905.  
  2906. /** This filter is documented in wp-admin/custom-header.php */ 
  2907. $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication. 
  2908. $object = $wp_site_icon->create_attachment_object( $cropped, $attachment_id ); 
  2909. unset( $object['ID'] ); 
  2910.  
  2911. // Update the attachment. 
  2912. add_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) ); 
  2913. $attachment_id = $wp_site_icon->insert_attachment( $object, $cropped ); 
  2914. remove_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) ); 
  2915.  
  2916. // Additional sizes in wp_prepare_attachment_for_js(). 
  2917. add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) ); 
  2918. break; 
  2919.  
  2920. default: 
  2921.  
  2922. /** 
  2923. * Fires before a cropped image is saved. 
  2924. * 
  2925. * Allows to add filters to modify the way a cropped image is saved. 
  2926. * 
  2927. * @since 4.3.0 
  2928. * 
  2929. * @param string $context The Customizer control requesting the cropped image. 
  2930. * @param int $attachment_id The attachment ID of the original image. 
  2931. * @param string $cropped Path to the cropped image file. 
  2932. */ 
  2933. do_action( 'wp_ajax_crop_image_pre_save', $context, $attachment_id, $cropped ); 
  2934.  
  2935. /** This filter is documented in wp-admin/custom-header.php */ 
  2936. $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication. 
  2937.  
  2938. $parent_url = wp_get_attachment_url( $attachment_id ); 
  2939. $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url ); 
  2940.  
  2941. $size = @getimagesize( $cropped ); 
  2942. $image_type = ( $size ) ? $size['mime'] : 'image/jpeg'; 
  2943.  
  2944. $object = array( 
  2945. 'post_title' => basename( $cropped ),  
  2946. 'post_content' => $url,  
  2947. 'post_mime_type' => $image_type,  
  2948. 'guid' => $url,  
  2949. 'context' => $context,  
  2950. ); 
  2951.  
  2952. $attachment_id = wp_insert_attachment( $object, $cropped ); 
  2953. $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped ); 
  2954.  
  2955. /** 
  2956. * Filters the cropped image attachment metadata. 
  2957. * 
  2958. * @since 4.3.0 
  2959. * 
  2960. * @see wp_generate_attachment_metadata() 
  2961. * 
  2962. * @param array $metadata Attachment metadata. 
  2963. */ 
  2964. $metadata = apply_filters( 'wp_ajax_cropped_attachment_metadata', $metadata ); 
  2965. wp_update_attachment_metadata( $attachment_id, $metadata ); 
  2966.  
  2967. /** 
  2968. * Filters the attachment ID for a cropped image. 
  2969. * 
  2970. * @since 4.3.0 
  2971. * 
  2972. * @param int $attachment_id The attachment ID of the cropped image. 
  2973. * @param string $context The Customizer control requesting the cropped image. 
  2974. */ 
  2975. $attachment_id = apply_filters( 'wp_ajax_cropped_attachment_id', $attachment_id, $context ); 
  2976.  
  2977. wp_send_json_success( wp_prepare_attachment_for_js( $attachment_id ) ); 
  2978.  
  2979. /** 
  2980. * Ajax handler for generating a password. 
  2981. * 
  2982. * @since 4.4.0 
  2983. */ 
  2984. function wp_ajax_generate_password() { 
  2985. wp_send_json_success( wp_generate_password( 24 ) ); 
  2986.  
  2987. /** 
  2988. * Ajax handler for saving the user's WordPress.org username. 
  2989. * 
  2990. * @since 4.4.0 
  2991. */ 
  2992. function wp_ajax_save_wporg_username() { 
  2993. if ( ! current_user_can( 'install_themes' ) && ! current_user_can( 'install_plugins' ) ) { 
  2994. wp_send_json_error(); 
  2995.  
  2996. check_ajax_referer( 'save_wporg_username_' . get_current_user_id() ); 
  2997.  
  2998. $username = isset( $_REQUEST['username'] ) ? wp_unslash( $_REQUEST['username'] ) : false; 
  2999.  
  3000. if ( ! $username ) { 
  3001. wp_send_json_error(); 
  3002.  
  3003. wp_send_json_success( update_user_meta( get_current_user_id(), 'wporg_favorites', $username ) ); 
  3004.  
  3005. /** 
  3006. * Ajax handler for installing a theme. 
  3007. * 
  3008. * @since 4.6.0 
  3009. * 
  3010. * @see Theme_Upgrader 
  3011. */ 
  3012. function wp_ajax_install_theme() { 
  3013. check_ajax_referer( 'updates' ); 
  3014.  
  3015. if ( empty( $_POST['slug'] ) ) { 
  3016. wp_send_json_error( array( 
  3017. 'slug' => '',  
  3018. 'errorCode' => 'no_theme_specified',  
  3019. 'errorMessage' => __( 'No theme specified.' ),  
  3020. ) ); 
  3021.  
  3022. $slug = sanitize_key( wp_unslash( $_POST['slug'] ) ); 
  3023.  
  3024. $status = array( 
  3025. 'install' => 'theme',  
  3026. 'slug' => $slug,  
  3027. ); 
  3028.  
  3029. if ( ! current_user_can( 'install_themes' ) ) { 
  3030. $status['errorMessage'] = __( 'Sorry, you are not allowed to install themes on this site.' ); 
  3031. wp_send_json_error( $status ); 
  3032.  
  3033. include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); 
  3034. include_once( ABSPATH . 'wp-admin/includes/theme.php' ); 
  3035.  
  3036. $api = themes_api( 'theme_information', array( 
  3037. 'slug' => $slug,  
  3038. 'fields' => array( 'sections' => false ),  
  3039. ) ); 
  3040.  
  3041. if ( is_wp_error( $api ) ) { 
  3042. $status['errorMessage'] = $api->get_error_message(); 
  3043. wp_send_json_error( $status ); 
  3044.  
  3045. $skin = new WP_Ajax_Upgrader_Skin(); 
  3046. $upgrader = new Theme_Upgrader( $skin ); 
  3047. $result = $upgrader->install( $api->download_link ); 
  3048.  
  3049. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 
  3050. $status['debug'] = $skin->get_upgrade_messages(); 
  3051.  
  3052. if ( is_wp_error( $result ) ) { 
  3053. $status['errorCode'] = $result->get_error_code(); 
  3054. $status['errorMessage'] = $result->get_error_message(); 
  3055. wp_send_json_error( $status ); 
  3056. } elseif ( is_wp_error( $skin->result ) ) { 
  3057. $status['errorCode'] = $skin->result->get_error_code(); 
  3058. $status['errorMessage'] = $skin->result->get_error_message(); 
  3059. wp_send_json_error( $status ); 
  3060. } elseif ( $skin->get_errors()->get_error_code() ) { 
  3061. $status['errorMessage'] = $skin->get_error_messages(); 
  3062. wp_send_json_error( $status ); 
  3063. } elseif ( is_null( $result ) ) { 
  3064. global $wp_filesystem; 
  3065.  
  3066. $status['errorCode'] = 'unable_to_connect_to_filesystem'; 
  3067. $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 
  3068.  
  3069. // Pass through the error from WP_Filesystem if one was raised. 
  3070. if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { 
  3071. $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 
  3072.  
  3073. wp_send_json_error( $status ); 
  3074.  
  3075. $status['themeName'] = wp_get_theme( $slug )->get( 'Name' ); 
  3076.  
  3077. if ( current_user_can( 'switch_themes' ) ) { 
  3078. if ( is_multisite() ) { 
  3079. $status['activateUrl'] = add_query_arg( array( 
  3080. 'action' => 'enable',  
  3081. '_wpnonce' => wp_create_nonce( 'enable-theme_' . $slug ),  
  3082. 'theme' => $slug,  
  3083. ), network_admin_url( 'themes.php' ) ); 
  3084. } else { 
  3085. $status['activateUrl'] = add_query_arg( array( 
  3086. 'action' => 'activate',  
  3087. '_wpnonce' => wp_create_nonce( 'switch-theme_' . $slug ),  
  3088. 'stylesheet' => $slug,  
  3089. ), admin_url( 'themes.php' ) ); 
  3090.  
  3091. if ( ! is_multisite() && current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { 
  3092. $status['customizeUrl'] = add_query_arg( array( 
  3093. 'return' => urlencode( network_admin_url( 'theme-install.php', 'relative' ) ),  
  3094. ), wp_customize_url( $slug ) ); 
  3095.  
  3096. /** 
  3097. * See WP_Theme_Install_List_Table::_get_theme_status() if we wanted to check 
  3098. * on post-install status. 
  3099. */ 
  3100. wp_send_json_success( $status ); 
  3101.  
  3102. /** 
  3103. * Ajax handler for updating a theme. 
  3104. * 
  3105. * @since 4.6.0 
  3106. * 
  3107. * @see Theme_Upgrader 
  3108. */ 
  3109. function wp_ajax_update_theme() { 
  3110. check_ajax_referer( 'updates' ); 
  3111.  
  3112. if ( empty( $_POST['slug'] ) ) { 
  3113. wp_send_json_error( array( 
  3114. 'slug' => '',  
  3115. 'errorCode' => 'no_theme_specified',  
  3116. 'errorMessage' => __( 'No theme specified.' ),  
  3117. ) ); 
  3118.  
  3119. $stylesheet = preg_replace( '/[^A-z0-9_\-]/', '', wp_unslash( $_POST['slug'] ) ); 
  3120. $status = array( 
  3121. 'update' => 'theme',  
  3122. 'slug' => $stylesheet,  
  3123. 'newVersion' => '',  
  3124. ); 
  3125.  
  3126. if ( ! current_user_can( 'update_themes' ) ) { 
  3127. $status['errorMessage'] = __( 'Sorry, you are not allowed to update themes for this site.' ); 
  3128. wp_send_json_error( $status ); 
  3129.  
  3130. include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); 
  3131.  
  3132. $current = get_site_transient( 'update_themes' ); 
  3133. if ( empty( $current ) ) { 
  3134. wp_update_themes(); 
  3135.  
  3136. $skin = new WP_Ajax_Upgrader_Skin(); 
  3137. $upgrader = new Theme_Upgrader( $skin ); 
  3138. $result = $upgrader->bulk_upgrade( array( $stylesheet ) ); 
  3139.  
  3140. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 
  3141. $status['debug'] = $skin->get_upgrade_messages(); 
  3142.  
  3143. if ( is_wp_error( $skin->result ) ) { 
  3144. $status['errorCode'] = $skin->result->get_error_code(); 
  3145. $status['errorMessage'] = $skin->result->get_error_message(); 
  3146. wp_send_json_error( $status ); 
  3147. } elseif ( $skin->get_errors()->get_error_code() ) { 
  3148. $status['errorMessage'] = $skin->get_error_messages(); 
  3149. wp_send_json_error( $status ); 
  3150. } elseif ( is_array( $result ) && ! empty( $result[ $stylesheet ] ) ) { 
  3151.  
  3152. // Theme is already at the latest version. 
  3153. if ( true === $result[ $stylesheet ] ) { 
  3154. $status['errorMessage'] = $upgrader->strings['up_to_date']; 
  3155. wp_send_json_error( $status ); 
  3156.  
  3157. $theme = wp_get_theme( $stylesheet ); 
  3158. if ( $theme->get( 'Version' ) ) { 
  3159. $status['newVersion'] = $theme->get( 'Version' ); 
  3160.  
  3161. wp_send_json_success( $status ); 
  3162. } elseif ( false === $result ) { 
  3163. global $wp_filesystem; 
  3164.  
  3165. $status['errorCode'] = 'unable_to_connect_to_filesystem'; 
  3166. $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 
  3167.  
  3168. // Pass through the error from WP_Filesystem if one was raised. 
  3169. if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { 
  3170. $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 
  3171.  
  3172. wp_send_json_error( $status ); 
  3173.  
  3174. // An unhandled error occurred. 
  3175. $status['errorMessage'] = __( 'Update failed.' ); 
  3176. wp_send_json_error( $status ); 
  3177.  
  3178. /** 
  3179. * Ajax handler for deleting a theme. 
  3180. * 
  3181. * @since 4.6.0 
  3182. * 
  3183. * @see delete_theme() 
  3184. */ 
  3185. function wp_ajax_delete_theme() { 
  3186. check_ajax_referer( 'updates' ); 
  3187.  
  3188. if ( empty( $_POST['slug'] ) ) { 
  3189. wp_send_json_error( array( 
  3190. 'slug' => '',  
  3191. 'errorCode' => 'no_theme_specified',  
  3192. 'errorMessage' => __( 'No theme specified.' ),  
  3193. ) ); 
  3194.  
  3195. $stylesheet = preg_replace( '/[^A-z0-9_\-]/', '', wp_unslash( $_POST['slug'] ) ); 
  3196. $status = array( 
  3197. 'delete' => 'theme',  
  3198. 'slug' => $stylesheet,  
  3199. ); 
  3200.  
  3201. if ( ! current_user_can( 'delete_themes' ) ) { 
  3202. $status['errorMessage'] = __( 'Sorry, you are not allowed to delete themes on this site.' ); 
  3203. wp_send_json_error( $status ); 
  3204.  
  3205. if ( ! wp_get_theme( $stylesheet )->exists() ) { 
  3206. $status['errorMessage'] = __( 'The requested theme does not exist.' ); 
  3207. wp_send_json_error( $status ); 
  3208.  
  3209. // Check filesystem credentials. `delete_theme()` will bail otherwise. 
  3210. $url = wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet ); 
  3211. ob_start(); 
  3212. $credentials = request_filesystem_credentials( $url ); 
  3213. ob_end_clean(); 
  3214. if ( false === $credentials || ! WP_Filesystem( $credentials ) ) { 
  3215. global $wp_filesystem; 
  3216.  
  3217. $status['errorCode'] = 'unable_to_connect_to_filesystem'; 
  3218. $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 
  3219.  
  3220. // Pass through the error from WP_Filesystem if one was raised. 
  3221. if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { 
  3222. $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 
  3223.  
  3224. wp_send_json_error( $status ); 
  3225.  
  3226. include_once( ABSPATH . 'wp-admin/includes/theme.php' ); 
  3227.  
  3228. $result = delete_theme( $stylesheet ); 
  3229.  
  3230. if ( is_wp_error( $result ) ) { 
  3231. $status['errorMessage'] = $result->get_error_message(); 
  3232. wp_send_json_error( $status ); 
  3233. } elseif ( false === $result ) { 
  3234. $status['errorMessage'] = __( 'Theme could not be deleted.' ); 
  3235. wp_send_json_error( $status ); 
  3236.  
  3237. wp_send_json_success( $status ); 
  3238.  
  3239. /** 
  3240. * Ajax handler for installing a plugin. 
  3241. * 
  3242. * @since 4.6.0 
  3243. * 
  3244. * @see Plugin_Upgrader 
  3245. */ 
  3246. function wp_ajax_install_plugin() { 
  3247. check_ajax_referer( 'updates' ); 
  3248.  
  3249. if ( empty( $_POST['slug'] ) ) { 
  3250. wp_send_json_error( array( 
  3251. 'slug' => '',  
  3252. 'errorCode' => 'no_plugin_specified',  
  3253. 'errorMessage' => __( 'No plugin specified.' ),  
  3254. ) ); 
  3255.  
  3256. $status = array( 
  3257. 'install' => 'plugin',  
  3258. 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),  
  3259. ); 
  3260.  
  3261. if ( ! current_user_can( 'install_plugins' ) ) { 
  3262. $status['errorMessage'] = __( 'Sorry, you are not allowed to install plugins on this site.' ); 
  3263. wp_send_json_error( $status ); 
  3264.  
  3265. include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); 
  3266. include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); 
  3267.  
  3268. $api = plugins_api( 'plugin_information', array( 
  3269. 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),  
  3270. 'fields' => array( 
  3271. 'sections' => false,  
  3272. ),  
  3273. ) ); 
  3274.  
  3275. if ( is_wp_error( $api ) ) { 
  3276. $status['errorMessage'] = $api->get_error_message(); 
  3277. wp_send_json_error( $status ); 
  3278.  
  3279. $status['pluginName'] = $api->name; 
  3280.  
  3281. $skin = new WP_Ajax_Upgrader_Skin(); 
  3282. $upgrader = new Plugin_Upgrader( $skin ); 
  3283. $result = $upgrader->install( $api->download_link ); 
  3284.  
  3285. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 
  3286. $status['debug'] = $skin->get_upgrade_messages(); 
  3287.  
  3288. if ( is_wp_error( $result ) ) { 
  3289. $status['errorCode'] = $result->get_error_code(); 
  3290. $status['errorMessage'] = $result->get_error_message(); 
  3291. wp_send_json_error( $status ); 
  3292. } elseif ( is_wp_error( $skin->result ) ) { 
  3293. $status['errorCode'] = $skin->result->get_error_code(); 
  3294. $status['errorMessage'] = $skin->result->get_error_message(); 
  3295. wp_send_json_error( $status ); 
  3296. } elseif ( $skin->get_errors()->get_error_code() ) { 
  3297. $status['errorMessage'] = $skin->get_error_messages(); 
  3298. wp_send_json_error( $status ); 
  3299. } elseif ( is_null( $result ) ) { 
  3300. global $wp_filesystem; 
  3301.  
  3302. $status['errorCode'] = 'unable_to_connect_to_filesystem'; 
  3303. $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 
  3304.  
  3305. // Pass through the error from WP_Filesystem if one was raised. 
  3306. if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { 
  3307. $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 
  3308.  
  3309. wp_send_json_error( $status ); 
  3310.  
  3311. $install_status = install_plugin_install_status( $api ); 
  3312. $pagenow = isset( $_POST['pagenow'] ) ? sanitize_key( $_POST['pagenow'] ) : ''; 
  3313.  
  3314. // If install request is coming from import page, do not return network activation link. 
  3315. $plugins_url = ( 'import' === $pagenow ) ? admin_url( 'plugins.php' ) : network_admin_url( 'plugins.php' ); 
  3316.  
  3317. if ( current_user_can( 'activate_plugins' ) && is_plugin_inactive( $install_status['file'] ) ) { 
  3318. $status['activateUrl'] = add_query_arg( array( 
  3319. '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $install_status['file'] ),  
  3320. 'action' => 'activate',  
  3321. 'plugin' => $install_status['file'],  
  3322. ), $plugins_url ); 
  3323.  
  3324. if ( is_multisite() && current_user_can( 'manage_network_plugins' ) && 'import' !== $pagenow ) { 
  3325. $status['activateUrl'] = add_query_arg( array( 'networkwide' => 1 ), $status['activateUrl'] ); 
  3326.  
  3327. wp_send_json_success( $status ); 
  3328.  
  3329. /** 
  3330. * Ajax handler for updating a plugin. 
  3331. * 
  3332. * @since 4.2.0 
  3333. * 
  3334. * @see Plugin_Upgrader 
  3335. */ 
  3336. function wp_ajax_update_plugin() { 
  3337. check_ajax_referer( 'updates' ); 
  3338.  
  3339. if ( empty( $_POST['plugin'] ) || empty( $_POST['slug'] ) ) { 
  3340. wp_send_json_error( array( 
  3341. 'slug' => '',  
  3342. 'errorCode' => 'no_plugin_specified',  
  3343. 'errorMessage' => __( 'No plugin specified.' ),  
  3344. ) ); 
  3345.  
  3346. $plugin = plugin_basename( sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) ); 
  3347.  
  3348. $status = array( 
  3349. 'update' => 'plugin',  
  3350. 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),  
  3351. 'oldVersion' => '',  
  3352. 'newVersion' => '',  
  3353. ); 
  3354.  
  3355. if ( ! current_user_can( 'update_plugins' ) || 0 !== validate_file( $plugin ) ) { 
  3356. $status['errorMessage'] = __( 'Sorry, you are not allowed to update plugins for this site.' ); 
  3357. wp_send_json_error( $status ); 
  3358.  
  3359. $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); 
  3360. $status['plugin'] = $plugin; 
  3361. $status['pluginName'] = $plugin_data['Name']; 
  3362.  
  3363. if ( $plugin_data['Version'] ) { 
  3364. /** translators: %s: Plugin version */ 
  3365. $status['oldVersion'] = sprintf( __( 'Version %s' ), $plugin_data['Version'] ); 
  3366.  
  3367. include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); 
  3368.  
  3369. wp_update_plugins(); 
  3370.  
  3371. $skin = new WP_Ajax_Upgrader_Skin(); 
  3372. $upgrader = new Plugin_Upgrader( $skin ); 
  3373. $result = $upgrader->bulk_upgrade( array( $plugin ) ); 
  3374.  
  3375. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 
  3376. $status['debug'] = $skin->get_upgrade_messages(); 
  3377.  
  3378. if ( is_wp_error( $skin->result ) ) { 
  3379. $status['errorCode'] = $skin->result->get_error_code(); 
  3380. $status['errorMessage'] = $skin->result->get_error_message(); 
  3381. wp_send_json_error( $status ); 
  3382. } elseif ( $skin->get_errors()->get_error_code() ) { 
  3383. $status['errorMessage'] = $skin->get_error_messages(); 
  3384. wp_send_json_error( $status ); 
  3385. } elseif ( is_array( $result ) && ! empty( $result[ $plugin ] ) ) { 
  3386. $plugin_update_data = current( $result ); 
  3387.  
  3388. /** 
  3389. * If the `update_plugins` site transient is empty (e.g. when you update 
  3390. * two plugins in quick succession before the transient repopulates),  
  3391. * this may be the return. 
  3392. * 
  3393. * Preferably something can be done to ensure `update_plugins` isn't empty. 
  3394. * For now, surface some sort of error here. 
  3395. */ 
  3396. if ( true === $plugin_update_data ) { 
  3397. $status['errorMessage'] = __( 'Plugin update failed.' ); 
  3398. wp_send_json_error( $status ); 
  3399.  
  3400. $plugin_data = get_plugins( '/' . $result[ $plugin ]['destination_name'] ); 
  3401. $plugin_data = reset( $plugin_data ); 
  3402.  
  3403. if ( $plugin_data['Version'] ) { 
  3404. /** translators: %s: Plugin version */ 
  3405. $status['newVersion'] = sprintf( __( 'Version %s' ), $plugin_data['Version'] ); 
  3406. wp_send_json_success( $status ); 
  3407. } elseif ( false === $result ) { 
  3408. global $wp_filesystem; 
  3409.  
  3410. $status['errorCode'] = 'unable_to_connect_to_filesystem'; 
  3411. $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 
  3412.  
  3413. // Pass through the error from WP_Filesystem if one was raised. 
  3414. if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { 
  3415. $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 
  3416.  
  3417. wp_send_json_error( $status ); 
  3418.  
  3419. // An unhandled error occurred. 
  3420. $status['errorMessage'] = __( 'Plugin update failed.' ); 
  3421. wp_send_json_error( $status ); 
  3422.  
  3423. /** 
  3424. * Ajax handler for deleting a plugin. 
  3425. * 
  3426. * @since 4.6.0 
  3427. * 
  3428. * @see delete_plugins() 
  3429. */ 
  3430. function wp_ajax_delete_plugin() { 
  3431. check_ajax_referer( 'updates' ); 
  3432.  
  3433. if ( empty( $_POST['slug'] ) || empty( $_POST['plugin'] ) ) { 
  3434. wp_send_json_error( array( 
  3435. 'slug' => '',  
  3436. 'errorCode' => 'no_plugin_specified',  
  3437. 'errorMessage' => __( 'No plugin specified.' ),  
  3438. ) ); 
  3439.  
  3440. $plugin = plugin_basename( sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) ); 
  3441.  
  3442. $status = array( 
  3443. 'delete' => 'plugin',  
  3444. 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),  
  3445. ); 
  3446.  
  3447. if ( ! current_user_can( 'delete_plugins' ) || 0 !== validate_file( $plugin ) ) { 
  3448. $status['errorMessage'] = __( 'Sorry, you are not allowed to delete plugins for this site.' ); 
  3449. wp_send_json_error( $status ); 
  3450.  
  3451. $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); 
  3452. $status['plugin'] = $plugin; 
  3453. $status['pluginName'] = $plugin_data['Name']; 
  3454.  
  3455. if ( is_plugin_active( $plugin ) ) { 
  3456. $status['errorMessage'] = __( 'You cannot delete a plugin while it is active on the main site.' ); 
  3457. wp_send_json_error( $status ); 
  3458.  
  3459. // Check filesystem credentials. `delete_plugins()` will bail otherwise. 
  3460. $url = wp_nonce_url( 'plugins.php?action=delete-selected&verify-delete=1&checked[]=' . $plugin, 'bulk-plugins' ); 
  3461. ob_start(); 
  3462. $credentials = request_filesystem_credentials( $url ); 
  3463. ob_end_clean(); 
  3464. if ( false === $credentials || ! WP_Filesystem( $credentials ) ) { 
  3465. global $wp_filesystem; 
  3466.  
  3467. $status['errorCode'] = 'unable_to_connect_to_filesystem'; 
  3468. $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 
  3469.  
  3470. // Pass through the error from WP_Filesystem if one was raised. 
  3471. if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { 
  3472. $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 
  3473.  
  3474. wp_send_json_error( $status ); 
  3475.  
  3476. $result = delete_plugins( array( $plugin ) ); 
  3477.  
  3478. if ( is_wp_error( $result ) ) { 
  3479. $status['errorMessage'] = $result->get_error_message(); 
  3480. wp_send_json_error( $status ); 
  3481. } elseif ( false === $result ) { 
  3482. $status['errorMessage'] = __( 'Plugin could not be deleted.' ); 
  3483. wp_send_json_error( $status ); 
  3484.  
  3485. wp_send_json_success( $status ); 
  3486.  
  3487. /** 
  3488. * Ajax handler for searching plugins. 
  3489. * 
  3490. * @since 4.6.0 
  3491. * 
  3492. * @global string $s Search term. 
  3493. */ 
  3494. function wp_ajax_search_plugins() { 
  3495. check_ajax_referer( 'updates' ); 
  3496.  
  3497. $pagenow = isset( $_POST['pagenow'] ) ? sanitize_key( $_POST['pagenow'] ) : ''; 
  3498. if ( 'plugins-network' === $pagenow || 'plugins' === $pagenow ) { 
  3499. set_current_screen( $pagenow ); 
  3500.  
  3501. /** @var WP_Plugins_List_Table $wp_list_table */ 
  3502. $wp_list_table = _get_list_table( 'WP_Plugins_List_Table', array( 
  3503. 'screen' => get_current_screen(),  
  3504. ) ); 
  3505.  
  3506. $status = array(); 
  3507.  
  3508. if ( ! $wp_list_table->ajax_user_can() ) { 
  3509. $status['errorMessage'] = __( 'Sorry, you are not allowed to manage plugins for this site.' ); 
  3510. wp_send_json_error( $status ); 
  3511.  
  3512. // Set the correct requester, so pagination works. 
  3513. $_SERVER['REQUEST_URI'] = add_query_arg( array_diff_key( $_POST, array( 
  3514. '_ajax_nonce' => null,  
  3515. 'action' => null,  
  3516. ) ), network_admin_url( 'plugins.php', 'relative' ) ); 
  3517.  
  3518. $GLOBALS['s'] = wp_unslash( $_POST['s'] ); 
  3519.  
  3520. $wp_list_table->prepare_items(); 
  3521.  
  3522. ob_start(); 
  3523. $wp_list_table->display(); 
  3524. $status['count'] = count( $wp_list_table->items ); 
  3525. $status['items'] = ob_get_clean(); 
  3526.  
  3527. wp_send_json_success( $status ); 
  3528.  
  3529. /** 
  3530. * Ajax handler for searching plugins to install. 
  3531. * 
  3532. * @since 4.6.0 
  3533. */ 
  3534. function wp_ajax_search_install_plugins() { 
  3535. check_ajax_referer( 'updates' ); 
  3536.  
  3537. $pagenow = isset( $_POST['pagenow'] ) ? sanitize_key( $_POST['pagenow'] ) : ''; 
  3538. if ( 'plugin-install-network' === $pagenow || 'plugin-install' === $pagenow ) { 
  3539. set_current_screen( $pagenow ); 
  3540.  
  3541. /** @var WP_Plugin_Install_List_Table $wp_list_table */ 
  3542. $wp_list_table = _get_list_table( 'WP_Plugin_Install_List_Table', array( 
  3543. 'screen' => get_current_screen(),  
  3544. ) ); 
  3545.  
  3546. $status = array(); 
  3547.  
  3548. if ( ! $wp_list_table->ajax_user_can() ) { 
  3549. $status['errorMessage'] = __( 'Sorry, you are not allowed to manage plugins for this site.' ); 
  3550. wp_send_json_error( $status ); 
  3551.  
  3552. // Set the correct requester, so pagination works. 
  3553. $_SERVER['REQUEST_URI'] = add_query_arg( array_diff_key( $_POST, array( 
  3554. '_ajax_nonce' => null,  
  3555. 'action' => null,  
  3556. ) ), network_admin_url( 'plugin-install.php', 'relative' ) ); 
  3557.  
  3558. $wp_list_table->prepare_items(); 
  3559.  
  3560. ob_start(); 
  3561. $wp_list_table->display(); 
  3562. $status['count'] = (int) $wp_list_table->get_pagination_arg( 'total_items' ); 
  3563. $status['items'] = ob_get_clean(); 
  3564.  
  3565. wp_send_json_success( $status ); 
.